[llvm] r335258 - [InstCombine] simplify binops before trying other folds
Sanjay Patel via llvm-commits
llvm-commits at lists.llvm.org
Thu Jun 21 10:06:37 PDT 2018
Author: spatel
Date: Thu Jun 21 10:06:36 2018
New Revision: 335258
URL: http://llvm.org/viewvc/llvm-project?rev=335258&view=rev
Log:
[InstCombine] simplify binops before trying other folds
This is outwardly NFC from what I can tell, but it should be more efficient
to simplify first (despite the name, SimplifyAssociativeOrCommutative does
not actually simplify as InstSimplify does - it creates/morphs instructions).
This should make it easier to refactor duplicated code that runs for all binops.
Modified:
llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp
llvm/trunk/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
llvm/trunk/lib/Transforms/InstCombine/InstCombineShifts.cpp
Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp?rev=335258&r1=335257&r2=335258&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp Thu Jun 21 10:06:36 2018
@@ -1121,13 +1121,12 @@ static Instruction *canonicalizeLowbitMa
}
Instruction *InstCombiner::visitAdd(BinaryOperator &I) {
- bool Changed = SimplifyAssociativeOrCommutative(I);
- Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
- if (Value *V =
- SimplifyAddInst(LHS, RHS, I.hasNoSignedWrap(), I.hasNoUnsignedWrap(),
- SQ.getWithInstruction(&I)))
+ if (Value *V = SimplifyAddInst(I.getOperand(0), I.getOperand(1),
+ I.hasNoSignedWrap(), I.hasNoUnsignedWrap(),
+ SQ.getWithInstruction(&I)))
return replaceInstUsesWith(I, V);
+ bool Changed = SimplifyAssociativeOrCommutative(I);
if (Instruction *X = foldShuffledBinop(I))
return X;
@@ -1140,6 +1139,7 @@ Instruction *InstCombiner::visitAdd(Bina
// FIXME: This should be moved into the above helper function to allow these
// transforms for general constant or constant splat vectors.
+ Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
Type *Ty = I.getType();
if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
Value *XorLHS = nullptr; ConstantInt *XorRHS = nullptr;
@@ -1378,18 +1378,19 @@ Instruction *InstCombiner::visitAdd(Bina
}
Instruction *InstCombiner::visitFAdd(BinaryOperator &I) {
- bool Changed = SimplifyAssociativeOrCommutative(I);
- Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
- if (Value *V = SimplifyFAddInst(LHS, RHS, I.getFastMathFlags(),
+ if (Value *V = SimplifyFAddInst(I.getOperand(0), I.getOperand(1),
+ I.getFastMathFlags(),
SQ.getWithInstruction(&I)))
return replaceInstUsesWith(I, V);
+ bool Changed = SimplifyAssociativeOrCommutative(I);
if (Instruction *X = foldShuffledBinop(I))
return X;
if (Instruction *FoldedFAdd = foldBinOpIntoSelectOrPhi(I))
return FoldedFAdd;
+ Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
Value *X;
// (-X) + Y --> Y - X
if (match(LHS, m_FNeg(m_Value(X))))
@@ -1555,10 +1556,9 @@ Value *InstCombiner::OptimizePointerDiff
}
Instruction *InstCombiner::visitSub(BinaryOperator &I) {
- Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
- if (Value *V =
- SimplifySubInst(Op0, Op1, I.hasNoSignedWrap(), I.hasNoUnsignedWrap(),
- SQ.getWithInstruction(&I)))
+ if (Value *V = SimplifySubInst(I.getOperand(0), I.getOperand(1),
+ I.hasNoSignedWrap(), I.hasNoUnsignedWrap(),
+ SQ.getWithInstruction(&I)))
return replaceInstUsesWith(I, V);
if (Instruction *X = foldShuffledBinop(I))
@@ -1569,6 +1569,7 @@ Instruction *InstCombiner::visitSub(Bina
return replaceInstUsesWith(I, V);
// If this is a 'B = x-(-A)', change to B = x+A.
+ Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
if (Value *V = dyn_castNegVal(Op1)) {
BinaryOperator *Res = BinaryOperator::CreateAdd(Op0, V);
@@ -1808,8 +1809,8 @@ Instruction *InstCombiner::visitSub(Bina
}
Instruction *InstCombiner::visitFSub(BinaryOperator &I) {
- Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
- if (Value *V = SimplifyFSubInst(Op0, Op1, I.getFastMathFlags(),
+ if (Value *V = SimplifyFSubInst(I.getOperand(0), I.getOperand(1),
+ I.getFastMathFlags(),
SQ.getWithInstruction(&I)))
return replaceInstUsesWith(I, V);
@@ -1818,6 +1819,7 @@ Instruction *InstCombiner::visitFSub(Bin
// Subtraction from -0.0 is the canonical form of fneg.
// fsub nsz 0, X ==> fsub nsz -0.0, X
+ Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
if (I.hasNoSignedZeros() && match(Op0, m_PosZeroFP()))
return BinaryOperator::CreateFNegFMF(Op1, &I);
Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp?rev=335258&r1=335257&r2=335258&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp Thu Jun 21 10:06:36 2018
@@ -1401,11 +1401,11 @@ Instruction *InstCombiner::narrowMaskedB
// here. We should standardize that construct where it is needed or choose some
// other way to ensure that commutated variants of patterns are not missed.
Instruction *InstCombiner::visitAnd(BinaryOperator &I) {
- bool Changed = SimplifyAssociativeOrCommutative(I);
- Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
- if (Value *V = SimplifyAndInst(Op0, Op1, SQ.getWithInstruction(&I)))
+ if (Value *V = SimplifyAndInst(I.getOperand(0), I.getOperand(1),
+ SQ.getWithInstruction(&I)))
return replaceInstUsesWith(I, V);
+ bool Changed = SimplifyAssociativeOrCommutative(I);
if (Instruction *X = foldShuffledBinop(I))
return X;
@@ -1425,6 +1425,7 @@ Instruction *InstCombiner::visitAnd(Bina
if (Value *V = SimplifyBSwap(I, Builder))
return replaceInstUsesWith(I, V);
+ Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
const APInt *C;
if (match(Op1, m_APInt(C))) {
Value *X, *Y;
@@ -2015,11 +2016,11 @@ Value *InstCombiner::foldOrOfICmps(ICmpI
// here. We should standardize that construct where it is needed or choose some
// other way to ensure that commutated variants of patterns are not missed.
Instruction *InstCombiner::visitOr(BinaryOperator &I) {
- bool Changed = SimplifyAssociativeOrCommutative(I);
- Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
- if (Value *V = SimplifyOrInst(Op0, Op1, SQ.getWithInstruction(&I)))
+ if (Value *V = SimplifyOrInst(I.getOperand(0), I.getOperand(1),
+ SQ.getWithInstruction(&I)))
return replaceInstUsesWith(I, V);
+ bool Changed = SimplifyAssociativeOrCommutative(I);
if (Instruction *X = foldShuffledBinop(I))
return X;
@@ -2046,6 +2047,7 @@ Instruction *InstCombiner::visitOr(Binar
if (Instruction *BSwap = MatchBSwap(I))
return BSwap;
+ Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
{
Value *A;
const APInt *C;
@@ -2468,11 +2470,11 @@ static Instruction *visitMaskedMerge(Bin
// here. We should standardize that construct where it is needed or choose some
// other way to ensure that commutated variants of patterns are not missed.
Instruction *InstCombiner::visitXor(BinaryOperator &I) {
- bool Changed = SimplifyAssociativeOrCommutative(I);
- Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
- if (Value *V = SimplifyXorInst(Op0, Op1, SQ.getWithInstruction(&I)))
+ if (Value *V = SimplifyXorInst(I.getOperand(0), I.getOperand(1),
+ SQ.getWithInstruction(&I)))
return replaceInstUsesWith(I, V);
+ bool Changed = SimplifyAssociativeOrCommutative(I);
if (Instruction *X = foldShuffledBinop(I))
return X;
@@ -2492,6 +2494,7 @@ Instruction *InstCombiner::visitXor(Bina
return replaceInstUsesWith(I, V);
// A^B --> A|B iff A and B have no bits set in common.
+ Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
if (haveNoCommonBitsSet(Op0, Op1, DL, &AC, &I, &DT))
return BinaryOperator::CreateOr(Op0, Op1);
Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp?rev=335258&r1=335257&r2=335258&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp Thu Jun 21 10:06:36 2018
@@ -126,11 +126,11 @@ static Constant *getLogBase2(Type *Ty, C
}
Instruction *InstCombiner::visitMul(BinaryOperator &I) {
- bool Changed = SimplifyAssociativeOrCommutative(I);
- Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
- if (Value *V = SimplifyMulInst(Op0, Op1, SQ.getWithInstruction(&I)))
+ if (Value *V = SimplifyMulInst(I.getOperand(0), I.getOperand(1),
+ SQ.getWithInstruction(&I)))
return replaceInstUsesWith(I, V);
+ bool Changed = SimplifyAssociativeOrCommutative(I);
if (Instruction *X = foldShuffledBinop(I))
return X;
@@ -138,6 +138,7 @@ Instruction *InstCombiner::visitMul(Bina
return replaceInstUsesWith(I, V);
// X * -1 == 0 - X
+ Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
if (match(Op1, m_AllOnes())) {
BinaryOperator *BO = BinaryOperator::CreateNeg(Op0, I.getName());
if (I.hasNoSignedWrap())
@@ -406,12 +407,12 @@ Instruction *InstCombiner::visitMul(Bina
}
Instruction *InstCombiner::visitFMul(BinaryOperator &I) {
- bool Changed = SimplifyAssociativeOrCommutative(I);
- Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
- if (Value *V = SimplifyFMulInst(Op0, Op1, I.getFastMathFlags(),
+ if (Value *V = SimplifyFMulInst(I.getOperand(0), I.getOperand(1),
+ I.getFastMathFlags(),
SQ.getWithInstruction(&I)))
return replaceInstUsesWith(I, V);
+ bool Changed = SimplifyAssociativeOrCommutative(I);
if (Instruction *X = foldShuffledBinop(I))
return X;
@@ -419,6 +420,7 @@ Instruction *InstCombiner::visitFMul(Bin
return FoldedMul;
// X * -1.0 --> -X
+ Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
if (match(Op1, m_SpecificFP(-1.0)))
return BinaryOperator::CreateFNegFMF(Op0, &I);
@@ -936,8 +938,8 @@ static Instruction *narrowUDivURem(Binar
}
Instruction *InstCombiner::visitUDiv(BinaryOperator &I) {
- Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
- if (Value *V = SimplifyUDivInst(Op0, Op1, SQ.getWithInstruction(&I)))
+ if (Value *V = SimplifyUDivInst(I.getOperand(0), I.getOperand(1),
+ SQ.getWithInstruction(&I)))
return replaceInstUsesWith(I, V);
if (Instruction *X = foldShuffledBinop(I))
@@ -948,6 +950,7 @@ Instruction *InstCombiner::visitUDiv(Bin
return Common;
// (x lshr C1) udiv C2 --> x udiv (C2 << C1)
+ Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
{
Value *X;
const APInt *C1, *C2;
@@ -1004,8 +1007,8 @@ Instruction *InstCombiner::visitUDiv(Bin
}
Instruction *InstCombiner::visitSDiv(BinaryOperator &I) {
- Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
- if (Value *V = SimplifySDivInst(Op0, Op1, SQ.getWithInstruction(&I)))
+ if (Value *V = SimplifySDivInst(I.getOperand(0), I.getOperand(1),
+ SQ.getWithInstruction(&I)))
return replaceInstUsesWith(I, V);
if (Instruction *X = foldShuffledBinop(I))
@@ -1015,6 +1018,7 @@ Instruction *InstCombiner::visitSDiv(Bin
if (Instruction *Common = commonIDivTransforms(I))
return Common;
+ Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
const APInt *Op1C;
if (match(Op1, m_APInt(Op1C))) {
// sdiv X, -1 == -X
@@ -1147,8 +1151,8 @@ static Instruction *foldFDivConstantDivi
}
Instruction *InstCombiner::visitFDiv(BinaryOperator &I) {
- Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
- if (Value *V = SimplifyFDivInst(Op0, Op1, I.getFastMathFlags(),
+ if (Value *V = SimplifyFDivInst(I.getOperand(0), I.getOperand(1),
+ I.getFastMathFlags(),
SQ.getWithInstruction(&I)))
return replaceInstUsesWith(I, V);
@@ -1161,6 +1165,7 @@ Instruction *InstCombiner::visitFDiv(Bin
if (Instruction *R = foldFDivConstantDividend(I))
return R;
+ Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
if (isa<Constant>(Op0))
if (SelectInst *SI = dyn_cast<SelectInst>(Op1))
if (Instruction *R = FoldOpIntoSelect(I, SI))
@@ -1276,8 +1281,8 @@ Instruction *InstCombiner::commonIRemTra
}
Instruction *InstCombiner::visitURem(BinaryOperator &I) {
- Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
- if (Value *V = SimplifyURemInst(Op0, Op1, SQ.getWithInstruction(&I)))
+ if (Value *V = SimplifyURemInst(I.getOperand(0), I.getOperand(1),
+ SQ.getWithInstruction(&I)))
return replaceInstUsesWith(I, V);
if (Instruction *X = foldShuffledBinop(I))
@@ -1290,6 +1295,7 @@ Instruction *InstCombiner::visitURem(Bin
return NarrowRem;
// X urem Y -> X and Y-1, where Y is a power of 2,
+ Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
if (isKnownToBeAPowerOfTwo(Op1, /*OrZero*/ true, 0, &I)) {
Constant *N1 = Constant::getAllOnesValue(I.getType());
Value *Add = Builder.CreateAdd(Op1, N1);
@@ -1314,8 +1320,8 @@ Instruction *InstCombiner::visitURem(Bin
}
Instruction *InstCombiner::visitSRem(BinaryOperator &I) {
- Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
- if (Value *V = SimplifySRemInst(Op0, Op1, SQ.getWithInstruction(&I)))
+ if (Value *V = SimplifySRemInst(I.getOperand(0), I.getOperand(1),
+ SQ.getWithInstruction(&I)))
return replaceInstUsesWith(I, V);
if (Instruction *X = foldShuffledBinop(I))
@@ -1325,6 +1331,7 @@ Instruction *InstCombiner::visitSRem(Bin
if (Instruction *Common = commonIRemTransforms(I))
return Common;
+ Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
{
const APInt *Y;
// X % -Y -> X % Y
@@ -1386,8 +1393,8 @@ Instruction *InstCombiner::visitSRem(Bin
}
Instruction *InstCombiner::visitFRem(BinaryOperator &I) {
- Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
- if (Value *V = SimplifyFRemInst(Op0, Op1, I.getFastMathFlags(),
+ if (Value *V = SimplifyFRemInst(I.getOperand(0), I.getOperand(1),
+ I.getFastMathFlags(),
SQ.getWithInstruction(&I)))
return replaceInstUsesWith(I, V);
Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineShifts.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineShifts.cpp?rev=335258&r1=335257&r2=335258&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineShifts.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineShifts.cpp Thu Jun 21 10:06:36 2018
@@ -588,10 +588,9 @@ Instruction *InstCombiner::FoldShiftByCo
}
Instruction *InstCombiner::visitShl(BinaryOperator &I) {
- Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
- if (Value *V =
- SimplifyShlInst(Op0, Op1, I.hasNoSignedWrap(), I.hasNoUnsignedWrap(),
- SQ.getWithInstruction(&I)))
+ if (Value *V = SimplifyShlInst(I.getOperand(0), I.getOperand(1),
+ I.hasNoSignedWrap(), I.hasNoUnsignedWrap(),
+ SQ.getWithInstruction(&I)))
return replaceInstUsesWith(I, V);
if (Instruction *X = foldShuffledBinop(I))
@@ -600,11 +599,12 @@ Instruction *InstCombiner::visitShl(Bina
if (Instruction *V = commonShiftTransforms(I))
return V;
+ Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Type *Ty = I.getType();
const APInt *ShAmtAPInt;
if (match(Op1, m_APInt(ShAmtAPInt))) {
unsigned ShAmt = ShAmtAPInt->getZExtValue();
- unsigned BitWidth = I.getType()->getScalarSizeInBits();
+ unsigned BitWidth = Ty->getScalarSizeInBits();
// shl (zext X), ShAmt --> zext (shl X, ShAmt)
// This is only valid if X would have zeros shifted out.
@@ -693,9 +693,8 @@ Instruction *InstCombiner::visitShl(Bina
}
Instruction *InstCombiner::visitLShr(BinaryOperator &I) {
- Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
- if (Value *V =
- SimplifyLShrInst(Op0, Op1, I.isExact(), SQ.getWithInstruction(&I)))
+ if (Value *V = SimplifyLShrInst(I.getOperand(0), I.getOperand(1), I.isExact(),
+ SQ.getWithInstruction(&I)))
return replaceInstUsesWith(I, V);
if (Instruction *X = foldShuffledBinop(I))
@@ -704,6 +703,7 @@ Instruction *InstCombiner::visitLShr(Bin
if (Instruction *R = commonShiftTransforms(I))
return R;
+ Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Type *Ty = I.getType();
const APInt *ShAmtAPInt;
if (match(Op1, m_APInt(ShAmtAPInt))) {
@@ -821,9 +821,8 @@ Instruction *InstCombiner::visitLShr(Bin
}
Instruction *InstCombiner::visitAShr(BinaryOperator &I) {
- Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
- if (Value *V =
- SimplifyAShrInst(Op0, Op1, I.isExact(), SQ.getWithInstruction(&I)))
+ if (Value *V = SimplifyAShrInst(I.getOperand(0), I.getOperand(1), I.isExact(),
+ SQ.getWithInstruction(&I)))
return replaceInstUsesWith(I, V);
if (Instruction *X = foldShuffledBinop(I))
@@ -832,6 +831,7 @@ Instruction *InstCombiner::visitAShr(Bin
if (Instruction *R = commonShiftTransforms(I))
return R;
+ Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Type *Ty = I.getType();
unsigned BitWidth = Ty->getScalarSizeInBits();
const APInt *ShAmtAPInt;
More information about the llvm-commits
mailing list