diff options
6 files changed, 29 insertions, 34 deletions
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp index 2f64c655c08..c47ebb34087 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp @@ -964,7 +964,7 @@ Instruction *InstCombiner::foldAddWithConstant(BinaryOperator &Add) { if (!match(Op1, m_Constant(Op1C))) return nullptr; - if (Instruction *NV = foldOpWithConstantIntoOperand(Add)) + if (Instruction *NV = foldBinOpIntoSelectOrPhi(Add)) return NV; Value *X; @@ -1304,9 +1304,8 @@ Instruction *InstCombiner::visitFAdd(BinaryOperator &I) { SQ.getWithInstruction(&I))) return replaceInstUsesWith(I, V); - if (isa<Constant>(RHS)) - if (Instruction *FoldedFAdd = foldOpWithConstantIntoOperand(I)) - return FoldedFAdd; + if (Instruction *FoldedFAdd = foldBinOpIntoSelectOrPhi(I)) + return FoldedFAdd; // -A + B --> B - A if (Value *LHSV = dyn_castFNegVal(LHS)) diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp index 2f4e97cb92a..44d11030eed 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp @@ -1359,9 +1359,8 @@ Instruction *InstCombiner::visitAnd(BinaryOperator &I) { if (Instruction *Z = narrowMaskedBinOp(I)) return Z; - if (isa<Constant>(Op1)) - if (Instruction *FoldedLogic = foldOpWithConstantIntoOperand(I)) - return FoldedLogic; + if (Instruction *FoldedLogic = foldBinOpIntoSelectOrPhi(I)) + return FoldedLogic; if (Instruction *DeMorgan = matchDeMorgansLaws(I, Builder)) return DeMorgan; @@ -1850,9 +1849,8 @@ Instruction *InstCombiner::visitOr(BinaryOperator &I) { if (Value *V = SimplifyBSwap(I, Builder)) return replaceInstUsesWith(I, V); - if (isa<Constant>(Op1)) - if (Instruction *FoldedLogic = foldOpWithConstantIntoOperand(I)) - return FoldedLogic; + if (Instruction *FoldedLogic = foldBinOpIntoSelectOrPhi(I)) + return FoldedLogic; // Given an OR instruction, check to see if this is a bswap. if (Instruction *BSwap = MatchBSwap(I)) @@ -2377,9 +2375,8 @@ Instruction *InstCombiner::visitXor(BinaryOperator &I) { } } - if (isa<Constant>(Op1)) - if (Instruction *FoldedLogic = foldOpWithConstantIntoOperand(I)) - return FoldedLogic; + if (Instruction *FoldedLogic = foldBinOpIntoSelectOrPhi(I)) + return FoldedLogic; { Value *A, *B; diff --git a/llvm/lib/Transforms/InstCombine/InstCombineInternal.h b/llvm/lib/Transforms/InstCombine/InstCombineInternal.h index 3dfaad3722d..3cda8e9f2cc 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineInternal.h +++ b/llvm/lib/Transforms/InstCombine/InstCombineInternal.h @@ -692,7 +692,7 @@ private: Instruction *FoldOpIntoSelect(Instruction &Op, SelectInst *SI); /// This is a convenience wrapper function for the above two functions. - Instruction *foldOpWithConstantIntoOperand(BinaryOperator &I); + Instruction *foldBinOpIntoSelectOrPhi(BinaryOperator &I); Instruction *foldAddWithConstant(BinaryOperator &Add); diff --git a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp index 815af57fc7a..bd319ef83df 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp @@ -251,22 +251,20 @@ Instruction *InstCombiner::visitMul(BinaryOperator &I) { } } + if (Instruction *FoldedMul = foldBinOpIntoSelectOrPhi(I)) + return FoldedMul; + // Simplify mul instructions with a constant RHS. if (isa<Constant>(Op1)) { - if (Instruction *FoldedMul = foldOpWithConstantIntoOperand(I)) - return FoldedMul; - // Canonicalize (X+C1)*CI -> X*CI+C1*CI. - { - Value *X; - Constant *C1; - if (match(Op0, m_OneUse(m_Add(m_Value(X), m_Constant(C1))))) { - Value *Mul = Builder.CreateMul(C1, Op1); - // Only go forward with the transform if C1*CI simplifies to a tidier - // constant. - if (!match(Mul, m_Mul(m_Value(), m_Value()))) - return BinaryOperator::CreateAdd(Builder.CreateMul(X, Op1), Mul); - } + Value *X; + Constant *C1; + if (match(Op0, m_OneUse(m_Add(m_Value(X), m_Constant(C1))))) { + Value *Mul = Builder.CreateMul(C1, Op1); + // Only go forward with the transform if C1*CI simplifies to a tidier + // constant. + if (!match(Mul, m_Mul(m_Value(), m_Value()))) + return BinaryOperator::CreateAdd(Builder.CreateMul(X, Op1), Mul); } } @@ -555,11 +553,11 @@ Instruction *InstCombiner::visitFMul(BinaryOperator &I) { bool AllowReassociate = I.isFast(); + if (Instruction *FoldedMul = foldBinOpIntoSelectOrPhi(I)) + return FoldedMul; + // Simplify mul instructions with a constant RHS. if (auto *C = dyn_cast<Constant>(Op1)) { - if (Instruction *FoldedMul = foldOpWithConstantIntoOperand(I)) - return FoldedMul; - // -X * C --> X * -C Value *X; if (match(Op0, m_FNeg(m_Value(X)))) @@ -900,7 +898,7 @@ Instruction *InstCombiner::commonIDivTransforms(BinaryOperator &I) { } if (!C2->isNullValue()) // avoid X udiv 0 - if (Instruction *FoldedDiv = foldOpWithConstantIntoOperand(I)) + if (Instruction *FoldedDiv = foldBinOpIntoSelectOrPhi(I)) return FoldedDiv; } diff --git a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp index a04a3cec09e..4b5c96ccbf1 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp @@ -370,7 +370,7 @@ Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, Constant *Op1, assert(!Op1C->uge(TypeBits) && "Shift over the type width should have been removed already"); - if (Instruction *FoldedShift = foldOpWithConstantIntoOperand(I)) + if (Instruction *FoldedShift = foldBinOpIntoSelectOrPhi(I)) return FoldedShift; // Fold shift2(trunc(shift1(x,c1)), c2) -> trunc(shift2(shift1(x,c1),c2)) diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp index 8ac2325932d..a3b2fe9e5d2 100644 --- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp +++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp @@ -1089,8 +1089,9 @@ Instruction *InstCombiner::foldOpIntoPhi(Instruction &I, PHINode *PN) { return replaceInstUsesWith(I, NewPN); } -Instruction *InstCombiner::foldOpWithConstantIntoOperand(BinaryOperator &I) { - assert(isa<Constant>(I.getOperand(1)) && "Unexpected operand type"); +Instruction *InstCombiner::foldBinOpIntoSelectOrPhi(BinaryOperator &I) { + if (!isa<Constant>(I.getOperand(1))) + return nullptr; if (auto *Sel = dyn_cast<SelectInst>(I.getOperand(0))) { if (Instruction *NewSel = FoldOpIntoSelect(I, Sel)) |