Two more patterns for instruction simplifier
This patch allows the instruction simplifier to cover
two more cases:
x = a + b and x = a - b
y = x - b y = x + b
In both cases y can be replaced with a, if the types
of arguments are integral.
Change-Id: I4735737f471e31057f1df76ddace5d4d1c79cde6
Test: New tests in 458-checker-instruction-simplification.
Signed-off-by: Max Kazantsev <maxim.kazantsev@intel.com>
diff --git a/compiler/optimizing/instruction_simplifier.cc b/compiler/optimizing/instruction_simplifier.cc
index e0410dc..4ca0600 100644
--- a/compiler/optimizing/instruction_simplifier.cc
+++ b/compiler/optimizing/instruction_simplifier.cc
@@ -920,6 +920,7 @@
void InstructionSimplifierVisitor::VisitAdd(HAdd* instruction) {
HConstant* input_cst = instruction->GetConstantRight();
HInstruction* input_other = instruction->GetLeastConstantLeft();
+ bool integral_type = Primitive::IsIntegralType(instruction->GetType());
if ((input_cst != nullptr) && input_cst->IsArithmeticZero()) {
// Replace code looking like
// ADD dst, src, 0
@@ -928,7 +929,7 @@
// Note that we cannot optimize `x + 0.0` to `x` for floating-point. When
// `x` is `-0.0`, the former expression yields `0.0`, while the later
// yields `-0.0`.
- if (Primitive::IsIntegralType(instruction->GetType())) {
+ if (integral_type) {
instruction->ReplaceWith(input_other);
instruction->GetBlock()->RemoveInstruction(instruction);
RecordSimplification();
@@ -974,10 +975,31 @@
// so no need to return.
TryHandleAssociativeAndCommutativeOperation(instruction);
- if ((instruction->GetLeft()->IsSub() || instruction->GetRight()->IsSub()) &&
+ if ((left->IsSub() || right->IsSub()) &&
TrySubtractionChainSimplification(instruction)) {
return;
}
+
+ if (integral_type) {
+ // Replace code patterns looking like
+ // SUB dst1, x, y SUB dst1, x, y
+ // ADD dst2, dst1, y ADD dst2, y, dst1
+ // with
+ // SUB dst1, x, y
+ // ADD instruction is not needed in this case, we may use
+ // one of inputs of SUB instead.
+ if (left->IsSub() && left->InputAt(1) == right) {
+ instruction->ReplaceWith(left->InputAt(0));
+ RecordSimplification();
+ instruction->GetBlock()->RemoveInstruction(instruction);
+ return;
+ } else if (right->IsSub() && right->InputAt(1) == left) {
+ instruction->ReplaceWith(right->InputAt(0));
+ RecordSimplification();
+ instruction->GetBlock()->RemoveInstruction(instruction);
+ return;
+ }
+ }
}
void InstructionSimplifierVisitor::VisitAnd(HAnd* instruction) {
@@ -1511,6 +1533,29 @@
if (TrySubtractionChainSimplification(instruction)) {
return;
}
+
+ if (left->IsAdd()) {
+ // Replace code patterns looking like
+ // ADD dst1, x, y ADD dst1, x, y
+ // SUB dst2, dst1, y SUB dst2, dst1, x
+ // with
+ // ADD dst1, x, y
+ // SUB instruction is not needed in this case, we may use
+ // one of inputs of ADD instead.
+ // It is applicable to integral types only.
+ DCHECK(Primitive::IsIntegralType(type));
+ if (left->InputAt(1) == right) {
+ instruction->ReplaceWith(left->InputAt(0));
+ RecordSimplification();
+ instruction->GetBlock()->RemoveInstruction(instruction);
+ return;
+ } else if (left->InputAt(0) == right) {
+ instruction->ReplaceWith(left->InputAt(1));
+ RecordSimplification();
+ instruction->GetBlock()->RemoveInstruction(instruction);
+ return;
+ }
+ }
}
void InstructionSimplifierVisitor::VisitUShr(HUShr* instruction) {
diff --git a/test/458-checker-instruction-simplification/src/Main.java b/test/458-checker-instruction-simplification/src/Main.java
index c717eaa..359d521 100644
--- a/test/458-checker-instruction-simplification/src/Main.java
+++ b/test/458-checker-instruction-simplification/src/Main.java
@@ -1971,8 +1971,165 @@
return (value >> temp) + temp;
}
-public static void main(String[] args) {
+ /// CHECK-START: int Main.$noinline$intAddSubSimplifyArg1(int, int) instruction_simplifier (before)
+ /// CHECK: <<X:i\d+>> ParameterValue
+ /// CHECK: <<Y:i\d+>> ParameterValue
+ /// CHECK-DAG: <<Sum:i\d+>> Add [<<X>>,<<Y>>]
+ /// CHECK-DAG: <<Res:i\d+>> Sub [<<Sum>>,<<X>>]
+ /// CHECK-DAG: Return [<<Res>>]
+
+ /// CHECK-START: int Main.$noinline$intAddSubSimplifyArg1(int, int) instruction_simplifier (after)
+ /// CHECK: <<X:i\d+>> ParameterValue
+ /// CHECK: <<Y:i\d+>> ParameterValue
+ /// CHECK-DAG: <<Sum:i\d+>> Add [<<X>>,<<Y>>]
+ /// CHECK-DAG: Return [<<Y>>]
+
+ public static int $noinline$intAddSubSimplifyArg1(int x, int y) {
+ if (doThrow) { throw new Error(); }
+ int sum = x + y;
+ return sum - x;
+ }
+
+ /// CHECK-START: int Main.$noinline$intAddSubSimplifyArg2(int, int) instruction_simplifier (before)
+ /// CHECK: <<X:i\d+>> ParameterValue
+ /// CHECK: <<Y:i\d+>> ParameterValue
+ /// CHECK-DAG: <<Sum:i\d+>> Add [<<X>>,<<Y>>]
+ /// CHECK-DAG: <<Res:i\d+>> Sub [<<Sum>>,<<Y>>]
+ /// CHECK-DAG: Return [<<Res>>]
+
+ /// CHECK-START: int Main.$noinline$intAddSubSimplifyArg2(int, int) instruction_simplifier (after)
+ /// CHECK: <<X:i\d+>> ParameterValue
+ /// CHECK: <<Y:i\d+>> ParameterValue
+ /// CHECK-DAG: <<Sum:i\d+>> Add [<<X>>,<<Y>>]
+ /// CHECK-DAG: Return [<<X>>]
+
+ public static int $noinline$intAddSubSimplifyArg2(int x, int y) {
+ if (doThrow) { throw new Error(); }
+ int sum = x + y;
+ return sum - y;
+ }
+
+ /// CHECK-START: int Main.$noinline$intSubAddSimplifyLeft(int, int) instruction_simplifier (before)
+ /// CHECK: <<X:i\d+>> ParameterValue
+ /// CHECK: <<Y:i\d+>> ParameterValue
+ /// CHECK-DAG: <<Sub:i\d+>> Sub [<<X>>,<<Y>>]
+ /// CHECK-DAG: <<Res:i\d+>> Add [<<Sub>>,<<Y>>]
+ /// CHECK-DAG: Return [<<Res>>]
+
+ /// CHECK-START: int Main.$noinline$intSubAddSimplifyLeft(int, int) instruction_simplifier (after)
+ /// CHECK: <<X:i\d+>> ParameterValue
+ /// CHECK: <<Y:i\d+>> ParameterValue
+ /// CHECK-DAG: <<Sub:i\d+>> Sub [<<X>>,<<Y>>]
+ /// CHECK-DAG: Return [<<X>>]
+
+ public static int $noinline$intSubAddSimplifyLeft(int x, int y) {
+ if (doThrow) { throw new Error(); }
+ int sub = x - y;
+ return sub + y;
+ }
+
+ /// CHECK-START: int Main.$noinline$intSubAddSimplifyRight(int, int) instruction_simplifier (before)
+ /// CHECK: <<X:i\d+>> ParameterValue
+ /// CHECK: <<Y:i\d+>> ParameterValue
+ /// CHECK-DAG: <<Sub:i\d+>> Sub [<<X>>,<<Y>>]
+ /// CHECK-DAG: <<Res:i\d+>> Add [<<Y>>,<<Sub>>]
+ /// CHECK-DAG: Return [<<Res>>]
+
+ /// CHECK-START: int Main.$noinline$intSubAddSimplifyRight(int, int) instruction_simplifier (after)
+ /// CHECK: <<X:i\d+>> ParameterValue
+ /// CHECK: <<Y:i\d+>> ParameterValue
+ /// CHECK-DAG: <<Sub:i\d+>> Sub [<<X>>,<<Y>>]
+ /// CHECK-DAG: Return [<<X>>]
+
+ public static int $noinline$intSubAddSimplifyRight(int x, int y) {
+ if (doThrow) { throw new Error(); }
+ int sub = x - y;
+ return y + sub;
+ }
+
+ /// CHECK-START: float Main.$noinline$floatAddSubSimplifyArg1(float, float) instruction_simplifier (before)
+ /// CHECK: <<X:f\d+>> ParameterValue
+ /// CHECK: <<Y:f\d+>> ParameterValue
+ /// CHECK-DAG: <<Sum:f\d+>> Add [<<X>>,<<Y>>]
+ /// CHECK-DAG: <<Res:f\d+>> Sub [<<Sum>>,<<X>>]
+ /// CHECK-DAG: Return [<<Res>>]
+
+ /// CHECK-START: float Main.$noinline$floatAddSubSimplifyArg1(float, float) instruction_simplifier (after)
+ /// CHECK: <<X:f\d+>> ParameterValue
+ /// CHECK: <<Y:f\d+>> ParameterValue
+ /// CHECK-DAG: <<Sum:f\d+>> Add [<<X>>,<<Y>>]
+ /// CHECK-DAG: <<Res:f\d+>> Sub [<<Sum>>,<<X>>]
+ /// CHECK-DAG: Return [<<Res>>]
+
+ public static float $noinline$floatAddSubSimplifyArg1(float x, float y) {
+ if (doThrow) { throw new Error(); }
+ float sum = x + y;
+ return sum - x;
+ }
+
+ /// CHECK-START: float Main.$noinline$floatAddSubSimplifyArg2(float, float) instruction_simplifier (before)
+ /// CHECK: <<X:f\d+>> ParameterValue
+ /// CHECK: <<Y:f\d+>> ParameterValue
+ /// CHECK-DAG: <<Sum:f\d+>> Add [<<X>>,<<Y>>]
+ /// CHECK-DAG: <<Res:f\d+>> Sub [<<Sum>>,<<Y>>]
+ /// CHECK-DAG: Return [<<Res>>]
+
+ /// CHECK-START: float Main.$noinline$floatAddSubSimplifyArg2(float, float) instruction_simplifier (after)
+ /// CHECK: <<X:f\d+>> ParameterValue
+ /// CHECK: <<Y:f\d+>> ParameterValue
+ /// CHECK-DAG: <<Sum:f\d+>> Add [<<X>>,<<Y>>]
+ /// CHECK-DAG: <<Res:f\d+>> Sub [<<Sum>>,<<Y>>]
+ /// CHECK-DAG: Return [<<Res>>]
+
+ public static float $noinline$floatAddSubSimplifyArg2(float x, float y) {
+ if (doThrow) { throw new Error(); }
+ float sum = x + y;
+ return sum - y;
+ }
+
+ /// CHECK-START: float Main.$noinline$floatSubAddSimplifyLeft(float, float) instruction_simplifier (before)
+ /// CHECK: <<X:f\d+>> ParameterValue
+ /// CHECK: <<Y:f\d+>> ParameterValue
+ /// CHECK-DAG: <<Sub:f\d+>> Sub [<<X>>,<<Y>>]
+ /// CHECK-DAG: <<Res:f\d+>> Add [<<Sub>>,<<Y>>]
+ /// CHECK-DAG: Return [<<Res>>]
+
+ /// CHECK-START: float Main.$noinline$floatSubAddSimplifyLeft(float, float) instruction_simplifier (after)
+ /// CHECK: <<X:f\d+>> ParameterValue
+ /// CHECK: <<Y:f\d+>> ParameterValue
+ /// CHECK-DAG: <<Sub:f\d+>> Sub [<<X>>,<<Y>>]
+ /// CHECK-DAG: <<Res:f\d+>> Add [<<Sub>>,<<Y>>]
+ /// CHECK-DAG: Return [<<Res>>]
+
+ public static float $noinline$floatSubAddSimplifyLeft(float x, float y) {
+ if (doThrow) { throw new Error(); }
+ float sub = x - y;
+ return sub + y;
+ }
+
+ /// CHECK-START: float Main.$noinline$floatSubAddSimplifyRight(float, float) instruction_simplifier (before)
+ /// CHECK: <<X:f\d+>> ParameterValue
+ /// CHECK: <<Y:f\d+>> ParameterValue
+ /// CHECK-DAG: <<Sub:f\d+>> Sub [<<X>>,<<Y>>]
+ /// CHECK-DAG: <<Res:f\d+>> Add [<<Y>>,<<Sub>>]
+ /// CHECK-DAG: Return [<<Res>>]
+
+ /// CHECK-START: float Main.$noinline$floatSubAddSimplifyRight(float, float) instruction_simplifier (after)
+ /// CHECK: <<X:f\d+>> ParameterValue
+ /// CHECK: <<Y:f\d+>> ParameterValue
+ /// CHECK-DAG: <<Sub:f\d+>> Sub [<<X>>,<<Y>>]
+ /// CHECK-DAG: <<Res:f\d+>> Add [<<Y>>,<<Sub>>]
+ /// CHECK-DAG: Return [<<Res>>]
+
+ public static float $noinline$floatSubAddSimplifyRight(float x, float y) {
+ if (doThrow) { throw new Error(); }
+ float sub = x - y;
+ return y + sub;
+ }
+
+ public static void main(String[] args) {
int arg = 123456;
+ float floatArg = 123456.125f;
assertLongEquals(arg, $noinline$Add0(arg));
assertIntEquals(5, $noinline$AddAddSubAddConst(1));
@@ -2143,6 +2300,15 @@
assertLongEquals(0xaf37bc048d159e24L, $noinline$longSmallerShiftMasking(0xabcdef0123456789L, 2 + 256));
assertIntEquals(0xfffd5e7c, $noinline$otherUseOfUnnecessaryShiftMasking(0xabcdef01, 13));
assertIntEquals(0xfffd5e7c, $noinline$otherUseOfUnnecessaryShiftMasking(0xabcdef01, 13 + 512));
+
+ assertIntEquals(654321, $noinline$intAddSubSimplifyArg1(arg, 654321));
+ assertIntEquals(arg, $noinline$intAddSubSimplifyArg2(arg, 654321));
+ assertIntEquals(arg, $noinline$intSubAddSimplifyLeft(arg, 654321));
+ assertIntEquals(arg, $noinline$intSubAddSimplifyRight(arg, 654321));
+ assertFloatEquals(654321.125f, $noinline$floatAddSubSimplifyArg1(floatArg, 654321.125f));
+ assertFloatEquals(floatArg, $noinline$floatAddSubSimplifyArg2(floatArg, 654321.125f));
+ assertFloatEquals(floatArg, $noinline$floatSubAddSimplifyLeft(floatArg, 654321.125f));
+ assertFloatEquals(floatArg, $noinline$floatSubAddSimplifyRight(floatArg, 654321.125f));
}
private static boolean $inline$true() { return true; }