aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/Analysis/InstructionSimplify.cpp16
-rw-r--r--test/Transforms/InstSimplify/2010-12-20-Reassociate.ll8
2 files changed, 23 insertions, 1 deletions
diff --git a/lib/Analysis/InstructionSimplify.cpp b/lib/Analysis/InstructionSimplify.cpp
index d5cf626e60..1d4ff0fc9e 100644
--- a/lib/Analysis/InstructionSimplify.cpp
+++ b/lib/Analysis/InstructionSimplify.cpp
@@ -593,11 +593,25 @@ static Value *SimplifySubInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
match(Op0, m_Add(m_Specific(Op1), m_Value(X))))
return X;
- /// i1 sub -> xor.
+ // i1 sub -> xor.
if (MaxRecurse && Op0->getType()->isIntegerTy(1))
if (Value *V = SimplifyXorInst(Op0, Op1, TD, DT, MaxRecurse-1))
return V;
+ // X - (X - Y) -> Y. More generally Z - (X - Y) -> (Z - X) + Y if everything
+ // simplifies.
+ Value *Y = 0, *Z = Op0;
+ if (MaxRecurse && match(Op1, m_Sub(m_Value(X), m_Value(Y)))) // Z - (X - Y)
+ // See if "V === Z - X" simplifies.
+ if (Value *V = SimplifyBinOp(Instruction::Sub, Z, X, TD, DT, MaxRecurse-1))
+ // It does! Now see if "W === V + Y" simplifies.
+ if (Value *W = SimplifyBinOp(Instruction::Add, V, Y, TD, DT,
+ MaxRecurse-1)) {
+ // It does, we successfully reassociated!
+ ++NumReassoc;
+ return W;
+ }
+
// Mul distributes over Sub. Try some generic simplifications based on this.
if (Value *V = FactorizeBinOp(Instruction::Sub, Op0, Op1, Instruction::Mul,
TD, DT, MaxRecurse))
diff --git a/test/Transforms/InstSimplify/2010-12-20-Reassociate.ll b/test/Transforms/InstSimplify/2010-12-20-Reassociate.ll
index 87dfdda910..03d5c33f87 100644
--- a/test/Transforms/InstSimplify/2010-12-20-Reassociate.ll
+++ b/test/Transforms/InstSimplify/2010-12-20-Reassociate.ll
@@ -62,3 +62,11 @@ define i32 @xor2(i32 %x, i32 %y) {
ret i32 %l
; CHECK: ret i32 %y
}
+
+define i32 @sub1(i32 %x, i32 %y) {
+; CHECK: @sub1
+ %d = sub i32 %x, %y
+ %r = sub i32 %x, %d
+ ret i32 %r
+; CHECK: ret i32 %y
+}