aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp')
-rw-r--r--llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp63
1 files changed, 39 insertions, 24 deletions
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
index eb1b8a29cfc5..0598f751febe 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
@@ -519,13 +519,6 @@ Value *FAddCombine::simplifyFAdd(AddendVect& Addends, unsigned InstrQuota) {
unsigned NextTmpIdx = 0;
FAddend TmpResult[3];
- // Points to the constant addend of the resulting simplified expression.
- // If the resulting expr has constant-addend, this constant-addend is
- // desirable to reside at the top of the resulting expression tree. Placing
- // constant close to supper-expr(s) will potentially reveal some optimization
- // opportunities in super-expr(s).
- const FAddend *ConstAdd = nullptr;
-
// Simplified addends are placed <SimpVect>.
AddendVect SimpVect;
@@ -541,6 +534,14 @@ Value *FAddCombine::simplifyFAdd(AddendVect& Addends, unsigned InstrQuota) {
}
Value *Val = ThisAddend->getSymVal();
+
+ // If the resulting expr has constant-addend, this constant-addend is
+ // desirable to reside at the top of the resulting expression tree. Placing
+ // constant close to super-expr(s) will potentially reveal some
+ // optimization opportunities in super-expr(s). Here we do not implement
+ // this logic intentionally and rely on SimplifyAssociativeOrCommutative
+ // call later.
+
unsigned StartIdx = SimpVect.size();
SimpVect.push_back(ThisAddend);
@@ -569,14 +570,8 @@ Value *FAddCombine::simplifyFAdd(AddendVect& Addends, unsigned InstrQuota) {
// Pop all addends being folded and push the resulting folded addend.
SimpVect.resize(StartIdx);
- if (Val) {
- if (!R.isZero()) {
- SimpVect.push_back(&R);
- }
- } else {
- // Don't push constant addend at this time. It will be the last element
- // of <SimpVect>.
- ConstAdd = &R;
+ if (!R.isZero()) {
+ SimpVect.push_back(&R);
}
}
}
@@ -584,9 +579,6 @@ Value *FAddCombine::simplifyFAdd(AddendVect& Addends, unsigned InstrQuota) {
assert((NextTmpIdx <= array_lengthof(TmpResult) + 1) &&
"out-of-bound access");
- if (ConstAdd)
- SimpVect.push_back(ConstAdd);
-
Value *Result;
if (!SimpVect.empty())
Result = createNaryFAdd(SimpVect, InstrQuota);
@@ -1296,6 +1288,9 @@ Instruction *InstCombinerImpl::visitAdd(BinaryOperator &I) {
if (Instruction *X = foldVectorBinop(I))
return X;
+ if (Instruction *Phi = foldBinopWithPhiOperands(I))
+ return Phi;
+
// (A*B)+(A*C) -> A*(B+C) etc
if (Value *V = SimplifyUsingDistributiveLaws(I))
return replaceInstUsesWith(I, V);
@@ -1498,15 +1493,18 @@ static Instruction *factorizeFAddFSub(BinaryOperator &I,
return Lerp;
Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
+ if (!Op0->hasOneUse() || !Op1->hasOneUse())
+ return nullptr;
+
Value *X, *Y, *Z;
bool IsFMul;
- if ((match(Op0, m_OneUse(m_FMul(m_Value(X), m_Value(Z)))) &&
- match(Op1, m_OneUse(m_c_FMul(m_Value(Y), m_Specific(Z))))) ||
- (match(Op0, m_OneUse(m_FMul(m_Value(Z), m_Value(X)))) &&
- match(Op1, m_OneUse(m_c_FMul(m_Value(Y), m_Specific(Z))))))
+ if ((match(Op0, m_FMul(m_Value(X), m_Value(Z))) &&
+ match(Op1, m_c_FMul(m_Value(Y), m_Specific(Z)))) ||
+ (match(Op0, m_FMul(m_Value(Z), m_Value(X))) &&
+ match(Op1, m_c_FMul(m_Value(Y), m_Specific(Z)))))
IsFMul = true;
- else if (match(Op0, m_OneUse(m_FDiv(m_Value(X), m_Value(Z)))) &&
- match(Op1, m_OneUse(m_FDiv(m_Value(Y), m_Specific(Z)))))
+ else if (match(Op0, m_FDiv(m_Value(X), m_Value(Z))) &&
+ match(Op1, m_FDiv(m_Value(Y), m_Specific(Z))))
IsFMul = false;
else
return nullptr;
@@ -1541,6 +1539,9 @@ Instruction *InstCombinerImpl::visitFAdd(BinaryOperator &I) {
if (Instruction *X = foldVectorBinop(I))
return X;
+ if (Instruction *Phi = foldBinopWithPhiOperands(I))
+ return Phi;
+
if (Instruction *FoldedFAdd = foldBinOpIntoSelectOrPhi(I))
return FoldedFAdd;
@@ -1654,6 +1655,14 @@ Instruction *InstCombinerImpl::visitFAdd(BinaryOperator &I) {
{X->getType()}, {NewStartC, X}, &I));
}
+ // (X * MulC) + X --> X * (MulC + 1.0)
+ Constant *MulC;
+ if (match(&I, m_c_FAdd(m_FMul(m_Value(X), m_ImmConstant(MulC)),
+ m_Deferred(X)))) {
+ MulC = ConstantExpr::getFAdd(MulC, ConstantFP::get(I.getType(), 1.0));
+ return BinaryOperator::CreateFMulFMF(X, MulC, &I);
+ }
+
if (Value *V = FAddCombine(Builder).simplify(&I))
return replaceInstUsesWith(I, V);
}
@@ -1748,6 +1757,9 @@ Instruction *InstCombinerImpl::visitSub(BinaryOperator &I) {
if (Instruction *X = foldVectorBinop(I))
return X;
+ if (Instruction *Phi = foldBinopWithPhiOperands(I))
+ return Phi;
+
Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
// If this is a 'B = x-(-A)', change to B = x+A.
@@ -2310,6 +2322,9 @@ Instruction *InstCombinerImpl::visitFSub(BinaryOperator &I) {
if (Instruction *X = foldVectorBinop(I))
return X;
+ if (Instruction *Phi = foldBinopWithPhiOperands(I))
+ return Phi;
+
// Subtraction from -0.0 is the canonical form of fneg.
// fsub -0.0, X ==> fneg X
// fsub nsz 0.0, X ==> fneg nsz X