summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlexandre Rames <alexandre.rames@arm.com>2015-03-11 16:48:16 +0000
committerAlexandre Rames <alexandre.rames@arm.com>2015-03-11 16:48:16 +0000
commitb2fd7bca70b580921eebf7c45769c39d2dfd8a5a (patch)
treec5dae29519df73f889ba14495eb79d545cd7d6e5
parent356286f989941ac495417195e4129aaceaf36a83 (diff)
downloadart-b2fd7bca70b580921eebf7c45769c39d2dfd8a5a.zip
art-b2fd7bca70b580921eebf7c45769c39d2dfd8a5a.tar.gz
art-b2fd7bca70b580921eebf7c45769c39d2dfd8a5a.tar.bz2
Opt compiler: Basic simplification for arithmetic operations.
The optimisations in this patch do not look further than the inputs of each operation. Change-Id: Iddd0ab6b360b9e7bb042db22086d51a31be85530
-rw-r--r--compiler/optimizing/constant_folding.cc180
-rw-r--r--compiler/optimizing/instruction_simplifier.cc265
-rw-r--r--compiler/optimizing/nodes.cc33
-rw-r--r--compiler/optimizing/nodes.h48
-rw-r--r--runtime/primitive.h4
-rw-r--r--runtime/utils.h6
-rw-r--r--test/442-checker-constant-folding/src/Main.java230
-rw-r--r--test/458-checker-instruction-simplification/expected.txt0
-rw-r--r--test/458-checker-instruction-simplification/info.txt1
-rw-r--r--test/458-checker-instruction-simplification/src/Main.java300
10 files changed, 1039 insertions, 28 deletions
diff --git a/compiler/optimizing/constant_folding.cc b/compiler/optimizing/constant_folding.cc
index fca9933..ec0cc3e 100644
--- a/compiler/optimizing/constant_folding.cc
+++ b/compiler/optimizing/constant_folding.cc
@@ -18,7 +18,28 @@
namespace art {
+// This visitor tries to simplify operations that yield a constant. For example
+// `input * 0` is replaced by a null constant.
+class InstructionWithAbsorbingInputSimplifier : public HGraphVisitor {
+ public:
+ explicit InstructionWithAbsorbingInputSimplifier(HGraph* graph) : HGraphVisitor(graph) {}
+
+ private:
+ void VisitShift(HBinaryOperation* shift);
+
+ void VisitAnd(HAnd* instruction) OVERRIDE;
+ void VisitMul(HMul* instruction) OVERRIDE;
+ void VisitOr(HOr* instruction) OVERRIDE;
+ void VisitRem(HRem* instruction) OVERRIDE;
+ void VisitShl(HShl* instruction) OVERRIDE;
+ void VisitShr(HShr* instruction) OVERRIDE;
+ void VisitSub(HSub* instruction) OVERRIDE;
+ void VisitUShr(HUShr* instruction) OVERRIDE;
+ void VisitXor(HXor* instruction) OVERRIDE;
+};
+
void HConstantFolding::Run() {
+ InstructionWithAbsorbingInputSimplifier simplifier(graph_);
// Process basic blocks in reverse post-order in the dominator tree,
// so that an instruction turned into a constant, used as input of
// another instruction, may possibly be used to turn that second
@@ -38,6 +59,8 @@ void HConstantFolding::Run() {
inst->AsBinaryOperation()->TryStaticEvaluation();
if (constant != nullptr) {
inst->GetBlock()->ReplaceAndRemoveInstructionWith(inst, constant);
+ } else {
+ inst->Accept(&simplifier);
}
} else if (inst->IsUnaryOperation()) {
// Constant folding: replace `op(a)' with a constant at compile
@@ -47,9 +70,166 @@ void HConstantFolding::Run() {
if (constant != nullptr) {
inst->GetBlock()->ReplaceAndRemoveInstructionWith(inst, constant);
}
+ } else if (inst->IsDivZeroCheck()) {
+ // We can safely remove the check if the input is a non-null constant.
+ HDivZeroCheck* check = inst->AsDivZeroCheck();
+ HInstruction* check_input = check->InputAt(0);
+ if (check_input->IsConstant() && !check_input->AsConstant()->IsZero()) {
+ check->ReplaceWith(check_input);
+ check->GetBlock()->RemoveInstruction(check);
+ }
}
}
}
}
+void InstructionWithAbsorbingInputSimplifier::VisitShift(HBinaryOperation* instruction) {
+ DCHECK(instruction->IsShl() || instruction->IsShr() || instruction->IsUShr());
+ HInstruction* left = instruction->GetLeft();
+ if (left->IsConstant() && left->AsConstant()->IsZero()) {
+ // Replace code looking like
+ // SHL dst, 0, shift_amount
+ // with
+ // CONSTANT 0
+ instruction->ReplaceWith(left);
+ instruction->GetBlock()->RemoveInstruction(instruction);
+ }
+}
+
+void InstructionWithAbsorbingInputSimplifier::VisitAnd(HAnd* instruction) {
+ HConstant* input_cst = instruction->GetConstantRight();
+ if ((input_cst != nullptr) && input_cst->IsZero()) {
+ // Replace code looking like
+ // AND dst, src, 0
+ // with
+ // CONSTANT 0
+ instruction->ReplaceWith(input_cst);
+ instruction->GetBlock()->RemoveInstruction(instruction);
+ }
+}
+
+void InstructionWithAbsorbingInputSimplifier::VisitMul(HMul* instruction) {
+ HConstant* input_cst = instruction->GetConstantRight();
+ Primitive::Type type = instruction->GetType();
+ if (Primitive::IsIntOrLongType(type) &&
+ (input_cst != nullptr) && input_cst->IsZero()) {
+ // Replace code looking like
+ // MUL dst, src, 0
+ // with
+ // CONSTANT 0
+ // Integral multiplication by zero always yields zero, but floating-point
+ // multiplication by zero does not always do. For example `Infinity * 0.0`
+ // should yield a NaN.
+ instruction->ReplaceWith(input_cst);
+ instruction->GetBlock()->RemoveInstruction(instruction);
+ }
+}
+
+void InstructionWithAbsorbingInputSimplifier::VisitOr(HOr* instruction) {
+ HConstant* input_cst = instruction->GetConstantRight();
+
+ if (input_cst == nullptr) {
+ return;
+ }
+
+ if (Int64FromConstant(input_cst) == -1) {
+ // Replace code looking like
+ // OR dst, src, 0xFFF...FF
+ // with
+ // CONSTANT 0xFFF...FF
+ instruction->ReplaceWith(input_cst);
+ instruction->GetBlock()->RemoveInstruction(instruction);
+ }
+}
+
+void InstructionWithAbsorbingInputSimplifier::VisitRem(HRem* instruction) {
+ Primitive::Type type = instruction->GetType();
+
+ if (!Primitive::IsIntegralType(type)) {
+ return;
+ }
+
+ HBasicBlock* block = instruction->GetBlock();
+
+ if (instruction->GetLeft()->IsConstant() &&
+ instruction->GetLeft()->AsConstant()->IsZero()) {
+ // Replace code looking like
+ // REM dst, 0, src
+ // with
+ // CONSTANT 0
+ instruction->ReplaceWith(instruction->GetLeft());
+ block->RemoveInstruction(instruction);
+ }
+
+ HConstant* cst_right = instruction->GetRight()->AsConstant();
+ if (((cst_right != nullptr) &&
+ (cst_right->IsOne() || cst_right->IsMinusOne())) ||
+ (instruction->GetLeft() == instruction->GetRight())) {
+ // Replace code looking like
+ // REM dst, src, 1
+ // or
+ // REM dst, src, -1
+ // or
+ // REM dst, src, src
+ // with
+ // CONSTANT 0
+ ArenaAllocator* allocator = GetGraph()->GetArena();
+ block->ReplaceAndRemoveInstructionWith(instruction,
+ HConstant::NewConstant(allocator, type, 0));
+ }
+}
+
+void InstructionWithAbsorbingInputSimplifier::VisitShl(HShl* instruction) {
+ VisitShift(instruction);
+}
+
+void InstructionWithAbsorbingInputSimplifier::VisitShr(HShr* instruction) {
+ VisitShift(instruction);
+}
+
+void InstructionWithAbsorbingInputSimplifier::VisitSub(HSub* instruction) {
+ Primitive::Type type = instruction->GetType();
+
+ if (!Primitive::IsIntegralType(type)) {
+ return;
+ }
+
+ HBasicBlock* block = instruction->GetBlock();
+ ArenaAllocator* allocator = GetGraph()->GetArena();
+
+ // We assume that GVN has run before, so we only perform a pointer
+ // comparison. If for some reason the values are equal but the pointers are
+ // different, we are still correct and only miss an optimisation
+ // opportunity.
+ if (instruction->GetLeft() == instruction->GetRight()) {
+ // Replace code looking like
+ // SUB dst, src, src
+ // with
+ // CONSTANT 0
+ // Note that we cannot optimise `x - x` to `0` for floating-point. It does
+ // not work when `x` is an infinity.
+ block->ReplaceAndRemoveInstructionWith(instruction,
+ HConstant::NewConstant(allocator, type, 0));
+ }
+}
+
+void InstructionWithAbsorbingInputSimplifier::VisitUShr(HUShr* instruction) {
+ VisitShift(instruction);
+}
+
+void InstructionWithAbsorbingInputSimplifier::VisitXor(HXor* instruction) {
+ if (instruction->GetLeft() == instruction->GetRight()) {
+ // Replace code looking like
+ // XOR dst, src, src
+ // with
+ // CONSTANT 0
+ Primitive::Type type = instruction->GetType();
+ HBasicBlock* block = instruction->GetBlock();
+ ArenaAllocator* allocator = GetGraph()->GetArena();
+
+ block->ReplaceAndRemoveInstructionWith(instruction,
+ HConstant::NewConstant(allocator, type, 0));
+ }
+}
+
} // namespace art
diff --git a/compiler/optimizing/instruction_simplifier.cc b/compiler/optimizing/instruction_simplifier.cc
index fd99070..2ef19b9 100644
--- a/compiler/optimizing/instruction_simplifier.cc
+++ b/compiler/optimizing/instruction_simplifier.cc
@@ -27,6 +27,8 @@ class InstructionSimplifierVisitor : public HGraphVisitor {
: HGraphVisitor(graph), stats_(stats) {}
private:
+ void VisitShift(HBinaryOperation* shift);
+
void VisitSuspendCheck(HSuspendCheck* check) OVERRIDE;
void VisitEqual(HEqual* equal) OVERRIDE;
void VisitArraySet(HArraySet* equal) OVERRIDE;
@@ -34,6 +36,16 @@ class InstructionSimplifierVisitor : public HGraphVisitor {
void VisitNullCheck(HNullCheck* instruction) OVERRIDE;
void VisitArrayLength(HArrayLength* instruction) OVERRIDE;
void VisitCheckCast(HCheckCast* instruction) OVERRIDE;
+ void VisitAdd(HAdd* instruction) OVERRIDE;
+ void VisitAnd(HAnd* instruction) OVERRIDE;
+ void VisitDiv(HDiv* instruction) OVERRIDE;
+ void VisitMul(HMul* instruction) OVERRIDE;
+ void VisitOr(HOr* instruction) OVERRIDE;
+ void VisitShl(HShl* instruction) OVERRIDE;
+ void VisitShr(HShr* instruction) OVERRIDE;
+ void VisitSub(HSub* instruction) OVERRIDE;
+ void VisitUShr(HUShr* instruction) OVERRIDE;
+ void VisitXor(HXor* instruction) OVERRIDE;
OptimizingCompilerStats* stats_;
};
@@ -43,6 +55,29 @@ void InstructionSimplifier::Run() {
visitor.VisitInsertionOrder();
}
+namespace {
+
+bool AreAllBitsSet(HConstant* constant) {
+ return Int64FromConstant(constant) == -1;
+}
+
+} // namespace
+
+void InstructionSimplifierVisitor::VisitShift(HBinaryOperation* instruction) {
+ DCHECK(instruction->IsShl() || instruction->IsShr() || instruction->IsUShr());
+ HConstant* input_cst = instruction->GetConstantRight();
+ HInstruction* input_other = instruction->GetLeastConstantLeft();
+
+ if ((input_cst != nullptr) && input_cst->IsZero()) {
+ // Replace code looking like
+ // SHL dst, src, 0
+ // with
+ // src
+ instruction->ReplaceWith(input_other);
+ instruction->GetBlock()->RemoveInstruction(instruction);
+ }
+}
+
void InstructionSimplifierVisitor::VisitNullCheck(HNullCheck* null_check) {
HInstruction* obj = null_check->InputAt(0);
if (!obj->CanBeNull()) {
@@ -137,4 +172,234 @@ void InstructionSimplifierVisitor::VisitTypeConversion(HTypeConversion* instruct
}
}
+void InstructionSimplifierVisitor::VisitAdd(HAdd* instruction) {
+ HConstant* input_cst = instruction->GetConstantRight();
+ HInstruction* input_other = instruction->GetLeastConstantLeft();
+ if ((input_cst != nullptr) && input_cst->IsZero()) {
+ // Replace code looking like
+ // ADD dst, src, 0
+ // with
+ // src
+ instruction->ReplaceWith(input_other);
+ instruction->GetBlock()->RemoveInstruction(instruction);
+ }
+}
+
+void InstructionSimplifierVisitor::VisitAnd(HAnd* instruction) {
+ HConstant* input_cst = instruction->GetConstantRight();
+ HInstruction* input_other = instruction->GetLeastConstantLeft();
+
+ if ((input_cst != nullptr) && AreAllBitsSet(input_cst)) {
+ // Replace code looking like
+ // AND dst, src, 0xFFF...FF
+ // with
+ // src
+ instruction->ReplaceWith(input_other);
+ instruction->GetBlock()->RemoveInstruction(instruction);
+ return;
+ }
+
+ // We assume that GVN has run before, so we only perform a pointer comparison.
+ // If for some reason the values are equal but the pointers are different, we
+ // are still correct and only miss an optimisation opportunity.
+ if (instruction->GetLeft() == instruction->GetRight()) {
+ // Replace code looking like
+ // AND dst, src, src
+ // with
+ // src
+ instruction->ReplaceWith(instruction->GetLeft());
+ instruction->GetBlock()->RemoveInstruction(instruction);
+ }
+}
+
+void InstructionSimplifierVisitor::VisitDiv(HDiv* instruction) {
+ HConstant* input_cst = instruction->GetConstantRight();
+ HInstruction* input_other = instruction->GetLeastConstantLeft();
+ Primitive::Type type = instruction->GetType();
+
+ if ((input_cst != nullptr) && input_cst->IsOne()) {
+ // Replace code looking like
+ // DIV dst, src, 1
+ // with
+ // src
+ instruction->ReplaceWith(input_other);
+ instruction->GetBlock()->RemoveInstruction(instruction);
+ return;
+ }
+
+ if ((input_cst != nullptr) && input_cst->IsMinusOne() &&
+ (Primitive::IsFloatingPointType(type) || Primitive::IsIntOrLongType(type))) {
+ // Replace code looking like
+ // DIV dst, src, -1
+ // with
+ // NEG dst, src
+ instruction->GetBlock()->ReplaceAndRemoveInstructionWith(
+ instruction, (new (GetGraph()->GetArena()) HNeg(type, input_other)));
+ }
+}
+
+void InstructionSimplifierVisitor::VisitMul(HMul* instruction) {
+ HConstant* input_cst = instruction->GetConstantRight();
+ HInstruction* input_other = instruction->GetLeastConstantLeft();
+ Primitive::Type type = instruction->GetType();
+ HBasicBlock* block = instruction->GetBlock();
+ ArenaAllocator* allocator = GetGraph()->GetArena();
+
+ if (input_cst == nullptr) {
+ return;
+ }
+
+ if (input_cst->IsOne()) {
+ // Replace code looking like
+ // MUL dst, src, 1
+ // with
+ // src
+ instruction->ReplaceWith(input_other);
+ instruction->GetBlock()->RemoveInstruction(instruction);
+ return;
+ }
+
+ if (input_cst->IsMinusOne() &&
+ (Primitive::IsFloatingPointType(type) || Primitive::IsIntOrLongType(type))) {
+ // Replace code looking like
+ // MUL dst, src, -1
+ // with
+ // NEG dst, src
+ HNeg* neg = new (allocator) HNeg(type, input_other);
+ block->ReplaceAndRemoveInstructionWith(instruction, neg);
+ return;
+ }
+
+ if (Primitive::IsFloatingPointType(type) &&
+ ((input_cst->IsFloatConstant() && input_cst->AsFloatConstant()->GetValue() == 2.0f) ||
+ (input_cst->IsDoubleConstant() && input_cst->AsDoubleConstant()->GetValue() == 2.0))) {
+ // Replace code looking like
+ // FP_MUL dst, src, 2.0
+ // with
+ // FP_ADD dst, src, src
+ // The 'int' and 'long' cases are handled below.
+ block->ReplaceAndRemoveInstructionWith(instruction,
+ new (allocator) HAdd(type, input_other, input_other));
+ return;
+ }
+
+ if (Primitive::IsIntOrLongType(type)) {
+ int64_t factor = Int64FromConstant(input_cst);
+ // We expect the `0` case to have been handled in the constant folding pass.
+ DCHECK_NE(factor, 0);
+ if (IsPowerOfTwo(factor)) {
+ // Replace code looking like
+ // MUL dst, src, pow_of_2
+ // with
+ // SHL dst, src, log2(pow_of_2)
+ HIntConstant* shift = new (allocator) HIntConstant(WhichPowerOf2(factor));
+ block->InsertInstructionBefore(shift, instruction);
+ HShl* shl = new(allocator) HShl(type, input_other, shift);
+ block->ReplaceAndRemoveInstructionWith(instruction, shl);
+ }
+ }
+}
+
+void InstructionSimplifierVisitor::VisitOr(HOr* instruction) {
+ HConstant* input_cst = instruction->GetConstantRight();
+ HInstruction* input_other = instruction->GetLeastConstantLeft();
+
+ if ((input_cst != nullptr) && input_cst->IsZero()) {
+ // Replace code looking like
+ // OR dst, src, 0
+ // with
+ // src
+ instruction->ReplaceWith(input_other);
+ instruction->GetBlock()->RemoveInstruction(instruction);
+ return;
+ }
+
+ // We assume that GVN has run before, so we only perform a pointer comparison.
+ // If for some reason the values are equal but the pointers are different, we
+ // are still correct and only miss an optimisation opportunity.
+ if (instruction->GetLeft() == instruction->GetRight()) {
+ // Replace code looking like
+ // OR dst, src, src
+ // with
+ // src
+ instruction->ReplaceWith(instruction->GetLeft());
+ instruction->GetBlock()->RemoveInstruction(instruction);
+ }
+}
+
+void InstructionSimplifierVisitor::VisitShl(HShl* instruction) {
+ VisitShift(instruction);
+}
+
+void InstructionSimplifierVisitor::VisitShr(HShr* instruction) {
+ VisitShift(instruction);
+}
+
+void InstructionSimplifierVisitor::VisitSub(HSub* instruction) {
+ HConstant* input_cst = instruction->GetConstantRight();
+ HInstruction* input_other = instruction->GetLeastConstantLeft();
+
+ if ((input_cst != nullptr) && input_cst->IsZero()) {
+ // Replace code looking like
+ // SUB dst, src, 0
+ // with
+ // src
+ instruction->ReplaceWith(input_other);
+ instruction->GetBlock()->RemoveInstruction(instruction);
+ return;
+ }
+
+ Primitive::Type type = instruction->GetType();
+ if (!Primitive::IsIntegralType(type)) {
+ return;
+ }
+
+ HBasicBlock* block = instruction->GetBlock();
+ ArenaAllocator* allocator = GetGraph()->GetArena();
+
+ if (instruction->GetLeft()->IsConstant()) {
+ int64_t left = Int64FromConstant(instruction->GetLeft()->AsConstant());
+ if (left == 0) {
+ // Replace code looking like
+ // SUB dst, 0, src
+ // with
+ // NEG dst, src
+ // Note that we cannot optimise `0.0 - x` to `-x` for floating-point. When
+ // `x` is `0.0`, the former expression yields `0.0`, while the later
+ // yields `-0.0`.
+ HNeg* neg = new (allocator) HNeg(type, instruction->GetRight());
+ block->ReplaceAndRemoveInstructionWith(instruction, neg);
+ }
+ }
+}
+
+void InstructionSimplifierVisitor::VisitUShr(HUShr* instruction) {
+ VisitShift(instruction);
+}
+
+void InstructionSimplifierVisitor::VisitXor(HXor* instruction) {
+ HConstant* input_cst = instruction->GetConstantRight();
+ HInstruction* input_other = instruction->GetLeastConstantLeft();
+
+ if ((input_cst != nullptr) && input_cst->IsZero()) {
+ // Replace code looking like
+ // XOR dst, src, 0
+ // with
+ // src
+ instruction->ReplaceWith(input_other);
+ instruction->GetBlock()->RemoveInstruction(instruction);
+ return;
+ }
+
+ if ((input_cst != nullptr) && AreAllBitsSet(input_cst)) {
+ // Replace code looking like
+ // XOR dst, src, 0xFFF...FF
+ // with
+ // NOT dst, src
+ HNot* bitwise_not = new (GetGraph()->GetArena()) HNot(instruction->GetType(), input_other);
+ instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, bitwise_not);
+ return;
+ }
+}
+
} // namespace art
diff --git a/compiler/optimizing/nodes.cc b/compiler/optimizing/nodes.cc
index e51bbc3..adcadf5 100644
--- a/compiler/optimizing/nodes.cc
+++ b/compiler/optimizing/nodes.cc
@@ -673,10 +673,43 @@ HConstant* HBinaryOperation::TryStaticEvaluation() const {
return nullptr;
}
+HConstant* HBinaryOperation::GetConstantRight() const {
+ if (GetRight()->IsConstant()) {
+ return GetRight()->AsConstant();
+ } else if (IsCommutative() && GetLeft()->IsConstant()) {
+ return GetLeft()->AsConstant();
+ } else {
+ return nullptr;
+ }
+}
+
+// If `GetConstantRight()` returns one of the input, this returns the other
+// one. Otherwise it returns nullptr.
+HInstruction* HBinaryOperation::GetLeastConstantLeft() const {
+ HInstruction* most_constant_right = GetConstantRight();
+ if (most_constant_right == nullptr) {
+ return nullptr;
+ } else if (most_constant_right == GetLeft()) {
+ return GetRight();
+ } else {
+ return GetLeft();
+ }
+}
+
bool HCondition::IsBeforeWhenDisregardMoves(HIf* if_) const {
return this == if_->GetPreviousDisregardingMoves();
}
+HConstant* HConstant::NewConstant(ArenaAllocator* allocator, Primitive::Type type, int64_t val) {
+ if (type == Primitive::kPrimInt) {
+ DCHECK(IsInt<32>(val));
+ return new (allocator) HIntConstant(val);
+ } else {
+ DCHECK_EQ(type, Primitive::kPrimLong);
+ return new (allocator) HLongConstant(val);
+ }
+}
+
bool HInstruction::Equals(HInstruction* other) const {
if (!InstructionTypeEquals(other)) return false;
DCHECK_EQ(GetKind(), other->GetKind());
diff --git a/compiler/optimizing/nodes.h b/compiler/optimizing/nodes.h
index d4498a6..c4f6f6d 100644
--- a/compiler/optimizing/nodes.h
+++ b/compiler/optimizing/nodes.h
@@ -1569,6 +1569,14 @@ class HBinaryOperation : public HExpression<2> {
virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
+ // Returns an input that can legally be used as the right input and is
+ // constant, or nullptr.
+ HConstant* GetConstantRight() const;
+
+ // If `GetConstantRight()` returns one of the input, this returns the other
+ // one. Otherwise it returns nullptr.
+ HInstruction* GetLeastConstantLeft() const;
+
DECLARE_INSTRUCTION(BinaryOperation);
private:
@@ -1840,6 +1848,12 @@ class HConstant : public HExpression<0> {
bool CanBeMoved() const OVERRIDE { return true; }
+ virtual bool IsMinusOne() const { return false; }
+ virtual bool IsZero() const { return false; }
+ virtual bool IsOne() const { return false; }
+
+ static HConstant* NewConstant(ArenaAllocator* allocator, Primitive::Type type, int64_t val);
+
DECLARE_INSTRUCTION(Constant);
private:
@@ -1859,6 +1873,16 @@ class HFloatConstant : public HConstant {
size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
+ bool IsMinusOne() const OVERRIDE {
+ return bit_cast<uint32_t>(AsFloatConstant()->GetValue()) == bit_cast<uint32_t>((-1.0f));
+ }
+ bool IsZero() const OVERRIDE {
+ return AsFloatConstant()->GetValue() == 0.0f;
+ }
+ bool IsOne() const OVERRIDE {
+ return bit_cast<uint32_t>(AsFloatConstant()->GetValue()) == bit_cast<uint32_t>(1.0f);
+ }
+
DECLARE_INSTRUCTION(FloatConstant);
private:
@@ -1880,6 +1904,16 @@ class HDoubleConstant : public HConstant {
size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
+ bool IsMinusOne() const OVERRIDE {
+ return bit_cast<uint64_t>(AsDoubleConstant()->GetValue()) == bit_cast<uint64_t>((-1.0));
+ }
+ bool IsZero() const OVERRIDE {
+ return AsDoubleConstant()->GetValue() == 0.0;
+ }
+ bool IsOne() const OVERRIDE {
+ return bit_cast<uint64_t>(AsDoubleConstant()->GetValue()) == bit_cast<uint64_t>(1.0);
+ }
+
DECLARE_INSTRUCTION(DoubleConstant);
private:
@@ -1925,6 +1959,10 @@ class HIntConstant : public HConstant {
// method is an workaround until we fix the above.
bool ActAsNullConstant() const OVERRIDE { return value_ == 0; }
+ bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
+ bool IsZero() const OVERRIDE { return GetValue() == 0; }
+ bool IsOne() const OVERRIDE { return GetValue() == 1; }
+
DECLARE_INSTRUCTION(IntConstant);
private:
@@ -1945,6 +1983,10 @@ class HLongConstant : public HConstant {
size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
+ bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
+ bool IsZero() const OVERRIDE { return GetValue() == 0; }
+ bool IsOne() const OVERRIDE { return GetValue() == 1; }
+
DECLARE_INSTRUCTION(LongConstant);
private:
@@ -3473,6 +3515,12 @@ class HBlocksInLoopIterator : public ValueObject {
DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
};
+inline int64_t Int64FromConstant(HConstant* constant) {
+ DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
+ return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
+ : constant->AsLongConstant()->GetValue();
+}
+
} // namespace art
#endif // ART_COMPILER_OPTIMIZING_NODES_H_
diff --git a/runtime/primitive.h b/runtime/primitive.h
index 9dda144..2d6b6b3 100644
--- a/runtime/primitive.h
+++ b/runtime/primitive.h
@@ -165,6 +165,10 @@ class Primitive {
}
}
+ static bool IsIntOrLongType(Type type) {
+ return type == kPrimInt || type == kPrimLong;
+ }
+
static bool Is64BitType(Type type) {
return type == kPrimLong || type == kPrimDouble;
}
diff --git a/runtime/utils.h b/runtime/utils.h
index d294f4b..7a96672 100644
--- a/runtime/utils.h
+++ b/runtime/utils.h
@@ -271,6 +271,12 @@ static constexpr int CTZ(T x) {
}
template<typename T>
+static inline int WhichPowerOf2(T x) {
+ DCHECK((x != 0) && IsPowerOfTwo(x));
+ return CTZ(x);
+}
+
+template<typename T>
static constexpr int POPCOUNT(T x) {
return (sizeof(T) == sizeof(uint32_t))
? __builtin_popcount(x)
diff --git a/test/442-checker-constant-folding/src/Main.java b/test/442-checker-constant-folding/src/Main.java
index de2c5c7..6b21fed 100644
--- a/test/442-checker-constant-folding/src/Main.java
+++ b/test/442-checker-constant-folding/src/Main.java
@@ -16,6 +16,18 @@
public class Main {
+ public static void assertIntEquals(int expected, int result) {
+ if (expected != result) {
+ throw new Error("Expected: " + expected + ", found: " + result);
+ }
+ }
+
+ public static void assertLongEquals(long expected, long result) {
+ if (expected != result) {
+ throw new Error("Expected: " + expected + ", found: " + result);
+ }
+ }
+
/**
* Tiny three-register program exercising int constant folding
* on negation.
@@ -219,41 +231,203 @@ public class Main {
return c;
}
- public static void main(String[] args) {
- if (IntNegation() != -42) {
- throw new Error();
- }
+ /**
+ * Test optimizations of arithmetic identities yielding a constant result.
+ */
- if (IntAddition1() != 3) {
- throw new Error();
- }
+ // CHECK-START: int Main.And0(int) constant_folding (before)
+ // CHECK-DAG: [[Arg:i\d+]] ParameterValue
+ // CHECK-DAG: [[Const0:i\d+]] IntConstant 0
+ // CHECK-DAG: [[And:i\d+]] And [ [[Arg]] [[Const0]] ]
+ // CHECK-DAG: Return [ [[And]] ]
- if (IntAddition2() != 14) {
- throw new Error();
- }
+ // CHECK-START: int Main.And0(int) constant_folding (after)
+ // CHECK-DAG: [[Arg:i\d+]] ParameterValue
+ // CHECK-DAG: [[Const0:i\d+]] IntConstant 0
+ // CHECK-NOT: And
+ // CHECK-DAG: Return [ [[Const0]] ]
- if (IntSubtraction() != 4) {
- throw new Error();
- }
+ public static int And0(int arg) {
+ return arg & 0;
+ }
- if (LongAddition() != 3L) {
- throw new Error();
- }
+ // CHECK-START: long Main.Mul0(long) constant_folding (before)
+ // CHECK-DAG: [[Arg:j\d+]] ParameterValue
+ // CHECK-DAG: [[Const0:j\d+]] LongConstant 0
+ // CHECK-DAG: [[Mul:j\d+]] Mul [ [[Arg]] [[Const0]] ]
+ // CHECK-DAG: Return [ [[Mul]] ]
- if (LongSubtraction() != 4L) {
- throw new Error();
- }
+ // CHECK-START: long Main.Mul0(long) constant_folding (after)
+ // CHECK-DAG: [[Arg:j\d+]] ParameterValue
+ // CHECK-DAG: [[Const0:j\d+]] LongConstant 0
+ // CHECK-NOT: Mul
+ // CHECK-DAG: Return [ [[Const0]] ]
- if (StaticCondition() != 5) {
- throw new Error();
- }
+ public static long Mul0(long arg) {
+ return arg * 0;
+ }
- if (JumpsAndConditionals(true) != 7) {
- throw new Error();
- }
+ // CHECK-START: int Main.OrAllOnes(int) constant_folding (before)
+ // CHECK-DAG: [[Arg:i\d+]] ParameterValue
+ // CHECK-DAG: [[ConstF:i\d+]] IntConstant -1
+ // CHECK-DAG: [[Or:i\d+]] Or [ [[Arg]] [[ConstF]] ]
+ // CHECK-DAG: Return [ [[Or]] ]
- if (JumpsAndConditionals(false) != 3) {
- throw new Error();
- }
+ // CHECK-START: int Main.OrAllOnes(int) constant_folding (after)
+ // CHECK-DAG: [[ConstF:i\d+]] IntConstant -1
+ // CHECK-NOT: Or
+ // CHECK-DAG: Return [ [[ConstF]] ]
+
+ public static int OrAllOnes(int arg) {
+ return arg | -1;
+ }
+
+ // CHECK-START: long Main.Rem0(long) constant_folding (before)
+ // CHECK-DAG: [[Arg:j\d+]] ParameterValue
+ // CHECK-DAG: [[Const0:j\d+]] LongConstant 0
+ // CHECK-DAG: [[DivZeroCheck:j\d+]] DivZeroCheck [ [[Arg]] ]
+ // CHECK-DAG: [[Rem:j\d+]] Rem [ [[Const0]] [[DivZeroCheck]] ]
+ // CHECK-DAG: Return [ [[Rem]] ]
+
+ // CHECK-START: long Main.Rem0(long) constant_folding (after)
+ // CHECK-DAG: [[Const0:j\d+]] LongConstant 0
+ // CHECK-NOT: Rem
+ // CHECK-DAG: Return [ [[Const0]] ]
+
+ public static long Rem0(long arg) {
+ return 0 % arg;
+ }
+
+ // CHECK-START: int Main.Rem1(int) constant_folding (before)
+ // CHECK-DAG: [[Arg:i\d+]] ParameterValue
+ // CHECK-DAG: [[Const1:i\d+]] IntConstant 1
+ // CHECK-DAG: [[Rem:i\d+]] Rem [ [[Arg]] [[Const1]] ]
+ // CHECK-DAG: Return [ [[Rem]] ]
+
+ // CHECK-START: int Main.Rem1(int) constant_folding (after)
+ // CHECK-DAG: [[Const0:i\d+]] IntConstant 0
+ // CHECK-NOT: Rem
+ // CHECK-DAG: Return [ [[Const0]] ]
+
+ public static int Rem1(int arg) {
+ return arg % 1;
+ }
+
+ // CHECK-START: long Main.RemN1(long) constant_folding (before)
+ // CHECK-DAG: [[Arg:j\d+]] ParameterValue
+ // CHECK-DAG: [[ConstN1:j\d+]] LongConstant -1
+ // CHECK-DAG: [[DivZeroCheck:j\d+]] DivZeroCheck [ [[Arg]] ]
+ // CHECK-DAG: [[Rem:j\d+]] Rem [ [[Arg]] [[DivZeroCheck]] ]
+ // CHECK-DAG: Return [ [[Rem]] ]
+
+ // CHECK-START: long Main.RemN1(long) constant_folding (after)
+ // CHECK-DAG: [[Const0:j\d+]] LongConstant 0
+ // CHECK-NOT: Rem
+ // CHECK-DAG: Return [ [[Const0]] ]
+
+ public static long RemN1(long arg) {
+ return arg % -1;
+ }
+
+ // CHECK-START: int Main.Shl0(int) constant_folding (before)
+ // CHECK-DAG: [[Arg:i\d+]] ParameterValue
+ // CHECK-DAG: [[Const0:i\d+]] IntConstant 0
+ // CHECK-DAG: [[Shl:i\d+]] Shl [ [[Const0]] [[Arg]] ]
+ // CHECK-DAG: Return [ [[Shl]] ]
+
+ // CHECK-START: int Main.Shl0(int) constant_folding (after)
+ // CHECK-DAG: [[Arg:i\d+]] ParameterValue
+ // CHECK-DAG: [[Const0:i\d+]] IntConstant 0
+ // CHECK-NOT: Shl
+ // CHECK-DAG: Return [ [[Const0]] ]
+
+ public static int Shl0(int arg) {
+ return 0 << arg;
+ }
+
+ // CHECK-START: long Main.Shr0(int) constant_folding (before)
+ // CHECK-DAG: [[Arg:i\d+]] ParameterValue
+ // CHECK-DAG: [[Const0:j\d+]] LongConstant 0
+ // CHECK-DAG: [[Shr:j\d+]] Shr [ [[Const0]] [[Arg]] ]
+ // CHECK-DAG: Return [ [[Shr]] ]
+
+ // CHECK-START: long Main.Shr0(int) constant_folding (after)
+ // CHECK-DAG: [[Arg:i\d+]] ParameterValue
+ // CHECK-DAG: [[Const0:j\d+]] LongConstant 0
+ // CHECK-NOT: Shr
+ // CHECK-DAG: Return [ [[Const0]] ]
+
+ public static long Shr0(int arg) {
+ return (long)0 >> arg;
+ }
+
+ // CHECK-START: long Main.SubSameLong(long) constant_folding (before)
+ // CHECK-DAG: [[Arg:j\d+]] ParameterValue
+ // CHECK-DAG: [[Sub:j\d+]] Sub [ [[Arg]] [[Arg]] ]
+ // CHECK-DAG: Return [ [[Sub]] ]
+
+ // CHECK-START: long Main.SubSameLong(long) constant_folding (after)
+ // CHECK-DAG: [[Arg:j\d+]] ParameterValue
+ // CHECK-DAG: [[Const0:j\d+]] LongConstant 0
+ // CHECK-NOT: Sub
+ // CHECK-DAG: Return [ [[Const0]] ]
+
+ public static long SubSameLong(long arg) {
+ return arg - arg;
+ }
+
+ // CHECK-START: int Main.UShr0(int) constant_folding (before)
+ // CHECK-DAG: [[Arg:i\d+]] ParameterValue
+ // CHECK-DAG: [[Const0:i\d+]] IntConstant 0
+ // CHECK-DAG: [[UShr:i\d+]] UShr [ [[Const0]] [[Arg]] ]
+ // CHECK-DAG: Return [ [[UShr]] ]
+
+ // CHECK-START: int Main.UShr0(int) constant_folding (after)
+ // CHECK-DAG: [[Arg:i\d+]] ParameterValue
+ // CHECK-DAG: [[Const0:i\d+]] IntConstant 0
+ // CHECK-NOT: UShr
+ // CHECK-DAG: Return [ [[Const0]] ]
+
+ public static int UShr0(int arg) {
+ return 0 >>> arg;
+ }
+
+ // CHECK-START: int Main.XorSameInt(int) constant_folding (before)
+ // CHECK-DAG: [[Arg:i\d+]] ParameterValue
+ // CHECK-DAG: [[Xor:i\d+]] Xor [ [[Arg]] [[Arg]] ]
+ // CHECK-DAG: Return [ [[Xor]] ]
+
+ // CHECK-START: int Main.XorSameInt(int) constant_folding (after)
+ // CHECK-DAG: [[Arg:i\d+]] ParameterValue
+ // CHECK-DAG: [[Const0:i\d+]] IntConstant 0
+ // CHECK-NOT: Xor
+ // CHECK-DAG: Return [ [[Const0]] ]
+
+ public static int XorSameInt(int arg) {
+ return arg ^ arg;
+ }
+
+ public static void main(String[] args) {
+ assertIntEquals(IntNegation(), -42);
+ assertIntEquals(IntAddition1(), 3);
+ assertIntEquals(IntAddition2(), 14);
+ assertIntEquals(IntSubtraction(), 4);
+ assertLongEquals(LongAddition(), 3L);
+ assertLongEquals(LongSubtraction(), 4L);
+ assertIntEquals(StaticCondition(), 5);
+ assertIntEquals(JumpsAndConditionals(true), 7);
+ assertIntEquals(JumpsAndConditionals(false), 3);
+ int random = 123456; // Chosen randomly.
+ assertIntEquals(And0(random), 0);
+ assertLongEquals(Mul0(random), 0);
+ assertIntEquals(OrAllOnes(random), -1);
+ assertLongEquals(Rem0(random), 0);
+ assertIntEquals(Rem1(random), 0);
+ assertLongEquals(RemN1(random), 0);
+ assertIntEquals(Shl0(random), 0);
+ assertLongEquals(Shr0(random), 0);
+ assertLongEquals(SubSameLong(random), 0);
+ assertIntEquals(UShr0(random), 0);
+ assertIntEquals(XorSameInt(random), 0);
}
}
diff --git a/test/458-checker-instruction-simplification/expected.txt b/test/458-checker-instruction-simplification/expected.txt
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/test/458-checker-instruction-simplification/expected.txt
diff --git a/test/458-checker-instruction-simplification/info.txt b/test/458-checker-instruction-simplification/info.txt
new file mode 100644
index 0000000..09da84b
--- /dev/null
+++ b/test/458-checker-instruction-simplification/info.txt
@@ -0,0 +1 @@
+Tests arithmetic identities optimizations in the optimizing compiler.
diff --git a/test/458-checker-instruction-simplification/src/Main.java b/test/458-checker-instruction-simplification/src/Main.java
new file mode 100644
index 0000000..ef6428d
--- /dev/null
+++ b/test/458-checker-instruction-simplification/src/Main.java
@@ -0,0 +1,300 @@
+/*
+* Copyright (C) 2015 The Android Open Source Project
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+
+public class Main {
+
+ public static void assertIntEquals(int expected, int result) {
+ if (expected != result) {
+ throw new Error("Expected: " + expected + ", found: " + result);
+ }
+ }
+
+ public static void assertLongEquals(long expected, long result) {
+ if (expected != result) {
+ throw new Error("Expected: " + expected + ", found: " + result);
+ }
+ }
+
+ /**
+ * Tiny programs exercising optimizations of arithmetic identities.
+ */
+
+ // CHECK-START: long Main.Add0(long) instruction_simplifier (before)
+ // CHECK-DAG: [[Arg:j\d+]] ParameterValue
+ // CHECK-DAG: [[Const0:j\d+]] LongConstant 0
+ // CHECK-DAG: [[Add:j\d+]] Add [ [[Const0]] [[Arg]] ]
+ // CHECK-DAG: Return [ [[Add]] ]
+
+ // CHECK-START: long Main.Add0(long) instruction_simplifier (after)
+ // CHECK-DAG: [[Arg:j\d+]] ParameterValue
+ // CHECK-NOT: Add
+ // CHECK-DAG: Return [ [[Arg]] ]
+
+ public static long Add0(long arg) {
+ return 0 + arg;
+ }
+
+ // CHECK-START: int Main.AndAllOnes(int) instruction_simplifier (before)
+ // CHECK-DAG: [[Arg:i\d+]] ParameterValue
+ // CHECK-DAG: [[ConstF:i\d+]] IntConstant -1
+ // CHECK-DAG: [[And:i\d+]] And [ [[Arg]] [[ConstF]] ]
+ // CHECK-DAG: Return [ [[And]] ]
+
+ // CHECK-START: int Main.AndAllOnes(int) instruction_simplifier (after)
+ // CHECK-DAG: [[Arg:i\d+]] ParameterValue
+ // CHECK-NOT: And
+ // CHECK-DAG: Return [ [[Arg]] ]
+
+ public static int AndAllOnes(int arg) {
+ return arg & -1;
+ }
+
+ // CHECK-START: long Main.Div1(long) instruction_simplifier (before)
+ // CHECK-DAG: [[Arg:j\d+]] ParameterValue
+ // CHECK-DAG: [[Const1:j\d+]] LongConstant 1
+ // CHECK-DAG: [[Div:j\d+]] Div [ [[Arg]] [[Const1]] ]
+ // CHECK-DAG: Return [ [[Div]] ]
+
+ // CHECK-START: long Main.Div1(long) instruction_simplifier (after)
+ // CHECK-DAG: [[Arg:j\d+]] ParameterValue
+ // CHECK-NOT: Div
+ // CHECK-DAG: Return [ [[Arg]] ]
+
+ public static long Div1(long arg) {
+ return arg / 1;
+ }
+
+ // CHECK-START: int Main.DivN1(int) instruction_simplifier (before)
+ // CHECK-DAG: [[Arg:i\d+]] ParameterValue
+ // CHECK-DAG: [[ConstN1:i\d+]] IntConstant -1
+ // CHECK-DAG: [[Div:i\d+]] Div [ [[Arg]] [[ConstN1]] ]
+ // CHECK-DAG: Return [ [[Div]] ]
+
+ // CHECK-START: int Main.DivN1(int) instruction_simplifier (after)
+ // CHECK-DAG: [[Arg:i\d+]] ParameterValue
+ // CHECK-DAG: [[Neg:i\d+]] Neg [ [[Arg]] ]
+ // CHECK-NOT: Div
+ // CHECK-DAG: Return [ [[Neg]] ]
+
+ public static int DivN1(int arg) {
+ return arg / -1;
+ }
+
+ // CHECK-START: long Main.Mul1(long) instruction_simplifier (before)
+ // CHECK-DAG: [[Arg:j\d+]] ParameterValue
+ // CHECK-DAG: [[Const1:j\d+]] LongConstant 1
+ // CHECK-DAG: [[Mul:j\d+]] Mul [ [[Arg]] [[Const1]] ]
+ // CHECK-DAG: Return [ [[Mul]] ]
+
+ // CHECK-START: long Main.Mul1(long) instruction_simplifier (after)
+ // CHECK-DAG: [[Arg:j\d+]] ParameterValue
+ // CHECK-NOT: Mul
+ // CHECK-DAG: Return [ [[Arg]] ]
+
+ public static long Mul1(long arg) {
+ return arg * 1;
+ }
+
+ // CHECK-START: int Main.MulN1(int) instruction_simplifier (before)
+ // CHECK-DAG: [[Arg:i\d+]] ParameterValue
+ // CHECK-DAG: [[ConstN1:i\d+]] IntConstant -1
+ // CHECK-DAG: [[Mul:i\d+]] Mul [ [[Arg]] [[ConstN1]] ]
+ // CHECK-DAG: Return [ [[Mul]] ]
+
+ // CHECK-START: int Main.MulN1(int) instruction_simplifier (after)
+ // CHECK-DAG: [[Arg:i\d+]] ParameterValue
+ // CHECK-DAG: [[Neg:i\d+]] Neg [ [[Arg]] ]
+ // CHECK-NOT: Mul
+ // CHECK-DAG: Return [ [[Neg]] ]
+
+ public static int MulN1(int arg) {
+ return arg * -1;
+ }
+
+ // CHECK-START: long Main.MulPowerOfTwo128(long) instruction_simplifier (before)
+ // CHECK-DAG: [[Arg:j\d+]] ParameterValue
+ // CHECK-DAG: [[Const128:j\d+]] LongConstant 128
+ // CHECK-DAG: [[Mul:j\d+]] Mul [ [[Arg]] [[Const128]] ]
+ // CHECK-DAG: Return [ [[Mul]] ]
+
+ // CHECK-START: long Main.MulPowerOfTwo128(long) instruction_simplifier (after)
+ // CHECK-DAG: [[Arg:j\d+]] ParameterValue
+ // CHECK-DAG: [[Const7:i\d+]] IntConstant 7
+ // CHECK-DAG: [[Shl:j\d+]] Shl [ [[Arg]] [[Const7]] ]
+ // CHECK-NOT: Mul
+ // CHECK-DAG: Return [ [[Shl]] ]
+
+ public static long MulPowerOfTwo128(long arg) {
+ return arg * 128;
+ }
+
+ // CHECK-START: int Main.Or0(int) instruction_simplifier (before)
+ // CHECK-DAG: [[Arg:i\d+]] ParameterValue
+ // CHECK-DAG: [[Const0:i\d+]] IntConstant 0
+ // CHECK-DAG: [[Or:i\d+]] Or [ [[Arg]] [[Const0]] ]
+ // CHECK-DAG: Return [ [[Or]] ]
+
+ // CHECK-START: int Main.Or0(int) instruction_simplifier (after)
+ // CHECK-DAG: [[Arg:i\d+]] ParameterValue
+ // CHECK-NOT: Or
+ // CHECK-DAG: Return [ [[Arg]] ]
+
+ public static int Or0(int arg) {
+ return arg | 0;
+ }
+
+ // CHECK-START: long Main.OrSame(long) instruction_simplifier (before)
+ // CHECK-DAG: [[Arg:j\d+]] ParameterValue
+ // CHECK-DAG: [[Or:j\d+]] Or [ [[Arg]] [[Arg]] ]
+ // CHECK-DAG: Return [ [[Or]] ]
+
+ // CHECK-START: long Main.OrSame(long) instruction_simplifier (after)
+ // CHECK-DAG: [[Arg:j\d+]] ParameterValue
+ // CHECK-NOT: Or
+ // CHECK-DAG: Return [ [[Arg]] ]
+
+ public static long OrSame(long arg) {
+ return arg | arg;
+ }
+
+ // CHECK-START: int Main.Shl0(int) instruction_simplifier (before)
+ // CHECK-DAG: [[Arg:i\d+]] ParameterValue
+ // CHECK-DAG: [[Const0:i\d+]] IntConstant 0
+ // CHECK-DAG: [[Shl:i\d+]] Shl [ [[Arg]] [[Const0]] ]
+ // CHECK-DAG: Return [ [[Shl]] ]
+
+ // CHECK-START: int Main.Shl0(int) instruction_simplifier (after)
+ // CHECK-DAG: [[Arg:i\d+]] ParameterValue
+ // CHECK-NOT: Shl
+ // CHECK-DAG: Return [ [[Arg]] ]
+
+ public static int Shl0(int arg) {
+ return arg << 0;
+ }
+
+ // CHECK-START: long Main.Shr0(long) instruction_simplifier (before)
+ // CHECK-DAG: [[Arg:j\d+]] ParameterValue
+ // CHECK-DAG: [[Const0:i\d+]] IntConstant 0
+ // CHECK-DAG: [[Shr:j\d+]] Shr [ [[Arg]] [[Const0]] ]
+ // CHECK-DAG: Return [ [[Shr]] ]
+
+ // CHECK-START: long Main.Shr0(long) instruction_simplifier (after)
+ // CHECK-DAG: [[Arg:j\d+]] ParameterValue
+ // CHECK-NOT: Shr
+ // CHECK-DAG: Return [ [[Arg]] ]
+
+ public static long Shr0(long arg) {
+ return arg >> 0;
+ }
+
+ // CHECK-START: long Main.Sub0(long) instruction_simplifier (before)
+ // CHECK-DAG: [[Arg:j\d+]] ParameterValue
+ // CHECK-DAG: [[Const0:j\d+]] LongConstant 0
+ // CHECK-DAG: [[Sub:j\d+]] Sub [ [[Arg]] [[Const0]] ]
+ // CHECK-DAG: Return [ [[Sub]] ]
+
+ // CHECK-START: long Main.Sub0(long) instruction_simplifier (after)
+ // CHECK-DAG: [[Arg:j\d+]] ParameterValue
+ // CHECK-NOT: Sub
+ // CHECK-DAG: Return [ [[Arg]] ]
+
+ public static long Sub0(long arg) {
+ return arg - 0;
+ }
+
+ // CHECK-START: int Main.SubAliasNeg(int) instruction_simplifier (before)
+ // CHECK-DAG: [[Arg:i\d+]] ParameterValue
+ // CHECK-DAG: [[Const0:i\d+]] IntConstant 0
+ // CHECK-DAG: [[Sub:i\d+]] Sub [ [[Const0]] [[Arg]] ]
+ // CHECK-DAG: Return [ [[Sub]] ]
+
+ // CHECK-START: int Main.SubAliasNeg(int) instruction_simplifier (after)
+ // CHECK-DAG: [[Arg:i\d+]] ParameterValue
+ // CHECK-DAG: [[Neg:i\d+]] Neg [ [[Arg]] ]
+ // CHECK-NOT: Sub
+ // CHECK-DAG: Return [ [[Neg]] ]
+
+ public static int SubAliasNeg(int arg) {
+ return 0 - arg;
+ }
+
+ // CHECK-START: long Main.UShr0(long) instruction_simplifier (before)
+ // CHECK-DAG: [[Arg:j\d+]] ParameterValue
+ // CHECK-DAG: [[Const0:i\d+]] IntConstant 0
+ // CHECK-DAG: [[UShr:j\d+]] UShr [ [[Arg]] [[Const0]] ]
+ // CHECK-DAG: Return [ [[UShr]] ]
+
+ // CHECK-START: long Main.UShr0(long) instruction_simplifier (after)
+ // CHECK-DAG: [[Arg:j\d+]] ParameterValue
+ // CHECK-NOT: UShr
+ // CHECK-DAG: Return [ [[Arg]] ]
+
+ public static long UShr0(long arg) {
+ return arg >>> 0;
+ }
+
+ // CHECK-START: int Main.Xor0(int) instruction_simplifier (before)
+ // CHECK-DAG: [[Arg:i\d+]] ParameterValue
+ // CHECK-DAG: [[Const0:i\d+]] IntConstant 0
+ // CHECK-DAG: [[Xor:i\d+]] Xor [ [[Arg]] [[Const0]] ]
+ // CHECK-DAG: Return [ [[Xor]] ]
+
+ // CHECK-START: int Main.Xor0(int) instruction_simplifier (after)
+ // CHECK-DAG: [[Arg:i\d+]] ParameterValue
+ // CHECK-NOT: Xor
+ // CHECK-DAG: Return [ [[Arg]] ]
+
+ public static int Xor0(int arg) {
+ return arg ^ 0;
+ }
+
+ // CHECK-START: int Main.XorAllOnes(int) instruction_simplifier (before)
+ // CHECK-DAG: [[Arg:i\d+]] ParameterValue
+ // CHECK-DAG: [[ConstF:i\d+]] IntConstant -1
+ // CHECK-DAG: [[Xor:i\d+]] Xor [ [[Arg]] [[ConstF]] ]
+ // CHECK-DAG: Return [ [[Xor]] ]
+
+ // CHECK-START: int Main.XorAllOnes(int) instruction_simplifier (after)
+ // CHECK-DAG: [[Arg:i\d+]] ParameterValue
+ // CHECK-DAG: [[Not:i\d+]] Not [ [[Arg]] ]
+ // CHECK-NOT: Xor
+ // CHECK-DAG: Return [ [[Not]] ]
+
+ public static int XorAllOnes(int arg) {
+ return arg ^ -1;
+ }
+
+ public static void main(String[] args) {
+ int arg = 123456;
+
+ assertLongEquals(Add0(arg), arg);
+ assertIntEquals(AndAllOnes(arg), arg);
+ assertLongEquals(Div1(arg), arg);
+ assertIntEquals(DivN1(arg), -arg);
+ assertLongEquals(Mul1(arg), arg);
+ assertIntEquals(MulN1(arg), -arg);
+ assertLongEquals(MulPowerOfTwo128(arg), (128 * arg));
+ assertIntEquals(Or0(arg), arg);
+ assertLongEquals(OrSame(arg), arg);
+ assertIntEquals(Shl0(arg), arg);
+ assertLongEquals(Shr0(arg), arg);
+ assertLongEquals(Sub0(arg), arg);
+ assertIntEquals(SubAliasNeg(arg), -arg);
+ assertLongEquals(UShr0(arg), arg);
+ assertIntEquals(Xor0(arg), arg);
+ assertIntEquals(XorAllOnes(arg), ~arg);
+ }
+}