diff options
author | Duncan Sands <baldrick@free.fr> | 2011-01-14 15:26:10 +0000 |
---|---|---|
committer | Duncan Sands <baldrick@free.fr> | 2011-01-14 15:26:10 +0000 |
commit | c087e20331c8a4c3b77d96a73588e80c06d89e3d (patch) | |
tree | 94ac52a66a86f73e2f0331a997aae1964ed3aacc /lib/Analysis/InstructionSimplify.cpp | |
parent | cf80bc1d4ac741620641c2bf48c25993bd478bd0 (diff) |
Turn X-(X-Y) into Y. According to my auto-simplifier this is the most common
simplification present in fully optimized code (I think instcombine fails to
transform some of these when "X-Y" has more than one use). Fires here and
there all over the test-suite, for example it eliminates 8 subtractions in
the final IR for 445.gobmk, 2 subs in 447.dealII, 2 in paq8p etc.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@123442 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Analysis/InstructionSimplify.cpp')
-rw-r--r-- | lib/Analysis/InstructionSimplify.cpp | 16 |
1 files changed, 15 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)) |