mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2024-12-26 21:32:10 +00:00
For completeness, generalize the (X + Y) - Y -> X transform and add X - (X + 1) -> -1.
These were not recommended by my auto-simplifier since they don't fire often enough. However they do fire from time to time, for example they remove one subtraction from the final bitcode for 483.xalancbmk. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@123755 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
fe02c69f84
commit
b2f3c383ec
@ -586,31 +586,68 @@ static Value *SimplifySubInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
|
|||||||
if (Op0 == Op1)
|
if (Op0 == Op1)
|
||||||
return Constant::getNullValue(Op0->getType());
|
return Constant::getNullValue(Op0->getType());
|
||||||
|
|
||||||
// (X + Y) - Y -> X
|
|
||||||
// (Y + X) - Y -> X
|
|
||||||
Value *X = 0;
|
|
||||||
if (match(Op0, m_Add(m_Value(X), m_Specific(Op1))) ||
|
|
||||||
match(Op0, m_Add(m_Specific(Op1), m_Value(X))))
|
|
||||||
return X;
|
|
||||||
|
|
||||||
// (X*2) - X -> X
|
// (X*2) - X -> X
|
||||||
// (X<<1) - X -> X
|
// (X<<1) - X -> X
|
||||||
|
Value *X = 0;
|
||||||
if (match(Op0, m_Mul(m_Specific(Op1), m_ConstantInt<2>())) ||
|
if (match(Op0, m_Mul(m_Specific(Op1), m_ConstantInt<2>())) ||
|
||||||
match(Op0, m_Shl(m_Specific(Op1), m_One())))
|
match(Op0, m_Shl(m_Specific(Op1), m_One())))
|
||||||
return Op1;
|
return Op1;
|
||||||
|
|
||||||
// i1 sub -> xor.
|
// (X + Y) - Z -> X + (Y - Z) or Y + (X - Z) if everything simplifies.
|
||||||
if (MaxRecurse && Op0->getType()->isIntegerTy(1))
|
// For example, (X + Y) - Y -> X; (Y + X) - Y -> X
|
||||||
if (Value *V = SimplifyXorInst(Op0, Op1, TD, DT, MaxRecurse-1))
|
Value *Y = 0, *Z = Op1;
|
||||||
return V;
|
if (MaxRecurse && match(Op0, m_Add(m_Value(X), m_Value(Y)))) { // (X + Y) - Z
|
||||||
|
// See if "V === Y - Z" simplifies.
|
||||||
|
if (Value *V = SimplifyBinOp(Instruction::Sub, Y, Z, TD, DT, MaxRecurse-1))
|
||||||
|
// It does! Now see if "X + V" simplifies.
|
||||||
|
if (Value *W = SimplifyBinOp(Instruction::Add, X, V, TD, DT,
|
||||||
|
MaxRecurse-1)) {
|
||||||
|
// It does, we successfully reassociated!
|
||||||
|
++NumReassoc;
|
||||||
|
return W;
|
||||||
|
}
|
||||||
|
// See if "V === X - Z" simplifies.
|
||||||
|
if (Value *V = SimplifyBinOp(Instruction::Sub, X, Z, TD, DT, MaxRecurse-1))
|
||||||
|
// It does! Now see if "Y + V" simplifies.
|
||||||
|
if (Value *W = SimplifyBinOp(Instruction::Add, Y, V, TD, DT,
|
||||||
|
MaxRecurse-1)) {
|
||||||
|
// It does, we successfully reassociated!
|
||||||
|
++NumReassoc;
|
||||||
|
return W;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// X - (X - Y) -> Y. More generally Z - (X - Y) -> (Z - X) + Y if everything
|
// X - (Y + Z) -> (X - Y) - Z or (X - Z) - Y if everything simplifies.
|
||||||
// simplifies.
|
// For example, X - (X + 1) -> -1
|
||||||
Value *Y = 0, *Z = Op0;
|
X = Op0;
|
||||||
|
if (MaxRecurse && match(Op1, m_Add(m_Value(Y), m_Value(Z)))) { // X - (Y + Z)
|
||||||
|
// See if "V === X - Y" simplifies.
|
||||||
|
if (Value *V = SimplifyBinOp(Instruction::Sub, X, Y, TD, DT, MaxRecurse-1))
|
||||||
|
// It does! Now see if "V - Z" simplifies.
|
||||||
|
if (Value *W = SimplifyBinOp(Instruction::Sub, V, Z, TD, DT,
|
||||||
|
MaxRecurse-1)) {
|
||||||
|
// It does, we successfully reassociated!
|
||||||
|
++NumReassoc;
|
||||||
|
return W;
|
||||||
|
}
|
||||||
|
// See if "V === X - Z" simplifies.
|
||||||
|
if (Value *V = SimplifyBinOp(Instruction::Sub, X, Z, TD, DT, MaxRecurse-1))
|
||||||
|
// It does! Now see if "V - Y" simplifies.
|
||||||
|
if (Value *W = SimplifyBinOp(Instruction::Sub, V, Y, TD, DT,
|
||||||
|
MaxRecurse-1)) {
|
||||||
|
// It does, we successfully reassociated!
|
||||||
|
++NumReassoc;
|
||||||
|
return W;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Z - (X - Y) -> (Z - X) + Y if everything simplifies.
|
||||||
|
// For example, X - (X - Y) -> Y.
|
||||||
|
Z = Op0;
|
||||||
if (MaxRecurse && match(Op1, m_Sub(m_Value(X), m_Value(Y)))) // Z - (X - Y)
|
if (MaxRecurse && match(Op1, m_Sub(m_Value(X), m_Value(Y)))) // Z - (X - Y)
|
||||||
// See if "V === Z - X" simplifies.
|
// See if "V === Z - X" simplifies.
|
||||||
if (Value *V = SimplifyBinOp(Instruction::Sub, Z, X, TD, DT, MaxRecurse-1))
|
if (Value *V = SimplifyBinOp(Instruction::Sub, Z, X, TD, DT, MaxRecurse-1))
|
||||||
// It does! Now see if "W === V + Y" simplifies.
|
// It does! Now see if "V + Y" simplifies.
|
||||||
if (Value *W = SimplifyBinOp(Instruction::Add, V, Y, TD, DT,
|
if (Value *W = SimplifyBinOp(Instruction::Add, V, Y, TD, DT,
|
||||||
MaxRecurse-1)) {
|
MaxRecurse-1)) {
|
||||||
// It does, we successfully reassociated!
|
// It does, we successfully reassociated!
|
||||||
@ -623,6 +660,11 @@ static Value *SimplifySubInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
|
|||||||
TD, DT, MaxRecurse))
|
TD, DT, MaxRecurse))
|
||||||
return V;
|
return V;
|
||||||
|
|
||||||
|
// i1 sub -> xor.
|
||||||
|
if (MaxRecurse && Op0->getType()->isIntegerTy(1))
|
||||||
|
if (Value *V = SimplifyXorInst(Op0, Op1, TD, DT, MaxRecurse-1))
|
||||||
|
return V;
|
||||||
|
|
||||||
// Threading Sub over selects and phi nodes is pointless, so don't bother.
|
// Threading Sub over selects and phi nodes is pointless, so don't bother.
|
||||||
// Threading over the select in "A - select(cond, B, C)" means evaluating
|
// Threading over the select in "A - select(cond, B, C)" means evaluating
|
||||||
// "A-B" and "A-C" and seeing if they are equal; but they are equal if and
|
// "A-B" and "A-C" and seeing if they are equal; but they are equal if and
|
||||||
|
@ -70,3 +70,23 @@ define i32 @sub1(i32 %x, i32 %y) {
|
|||||||
ret i32 %r
|
ret i32 %r
|
||||||
; CHECK: ret i32 %y
|
; CHECK: ret i32 %y
|
||||||
}
|
}
|
||||||
|
|
||||||
|
define i32 @sub2(i32 %x) {
|
||||||
|
; CHECK: @sub2
|
||||||
|
; X - (X + 1) -> -1
|
||||||
|
%xp1 = add i32 %x, 1
|
||||||
|
%r = sub i32 %x, %xp1
|
||||||
|
ret i32 %r
|
||||||
|
; CHECK: ret i32 -1
|
||||||
|
}
|
||||||
|
|
||||||
|
define i32 @sub3(i32 %x, i32 %y) {
|
||||||
|
; CHECK: @sub3
|
||||||
|
; ((X + 1) + Y) - (Y + 1) -> X
|
||||||
|
%xp1 = add i32 %x, 1
|
||||||
|
%lhs = add i32 %xp1, %y
|
||||||
|
%rhs = add i32 %y, 1
|
||||||
|
%r = sub i32 %lhs, %rhs
|
||||||
|
ret i32 %r
|
||||||
|
; CHECK: ret i32 %x
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user