Skip to content

Commit 6cd5eb1

Browse files
committed
[InstCombine] Avoid some uses of ConstantExpr::getZExt() (NFC)
Add helpers getLosslessUnsignedTrunc/getLosslessSignedTrunc for this common pattern.
1 parent cc24455 commit 6cd5eb1

File tree

5 files changed

+38
-26
lines changed

5 files changed

+38
-26
lines changed

llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1669,7 +1669,7 @@ bool InstCombinerImpl::shouldOptimizeCast(CastInst *CI) {
16691669

16701670
/// Fold {and,or,xor} (cast X), C.
16711671
static Instruction *foldLogicCastConstant(BinaryOperator &Logic, CastInst *Cast,
1672-
InstCombiner::BuilderTy &Builder) {
1672+
InstCombinerImpl &IC) {
16731673
Constant *C = dyn_cast<Constant>(Logic.getOperand(1));
16741674
if (!C)
16751675
return nullptr;
@@ -1684,21 +1684,17 @@ static Instruction *foldLogicCastConstant(BinaryOperator &Logic, CastInst *Cast,
16841684
// instruction may be cheaper (particularly in the case of vectors).
16851685
Value *X;
16861686
if (match(Cast, m_OneUse(m_ZExt(m_Value(X))))) {
1687-
Constant *TruncC = ConstantExpr::getTrunc(C, SrcTy);
1688-
Constant *ZextTruncC = ConstantExpr::getZExt(TruncC, DestTy);
1689-
if (ZextTruncC == C) {
1687+
if (Constant *TruncC = IC.getLosslessUnsignedTrunc(C, SrcTy)) {
16901688
// LogicOpc (zext X), C --> zext (LogicOpc X, C)
1691-
Value *NewOp = Builder.CreateBinOp(LogicOpc, X, TruncC);
1689+
Value *NewOp = IC.Builder.CreateBinOp(LogicOpc, X, TruncC);
16921690
return new ZExtInst(NewOp, DestTy);
16931691
}
16941692
}
16951693

16961694
if (match(Cast, m_OneUse(m_SExt(m_Value(X))))) {
1697-
Constant *TruncC = ConstantExpr::getTrunc(C, SrcTy);
1698-
Constant *SextTruncC = ConstantExpr::getSExt(TruncC, DestTy);
1699-
if (SextTruncC == C) {
1695+
if (Constant *TruncC = IC.getLosslessSignedTrunc(C, SrcTy)) {
17001696
// LogicOpc (sext X), C --> sext (LogicOpc X, C)
1701-
Value *NewOp = Builder.CreateBinOp(LogicOpc, X, TruncC);
1697+
Value *NewOp = IC.Builder.CreateBinOp(LogicOpc, X, TruncC);
17021698
return new SExtInst(NewOp, DestTy);
17031699
}
17041700
}
@@ -1756,7 +1752,7 @@ Instruction *InstCombinerImpl::foldCastedBitwiseLogic(BinaryOperator &I) {
17561752
if (!SrcTy->isIntOrIntVectorTy())
17571753
return nullptr;
17581754

1759-
if (Instruction *Ret = foldLogicCastConstant(I, Cast0, Builder))
1755+
if (Instruction *Ret = foldLogicCastConstant(I, Cast0, *this))
17601756
return Ret;
17611757

17621758
CastInst *Cast1 = dyn_cast<CastInst>(Op1);

llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1600,8 +1600,7 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
16001600
Constant *C;
16011601
if (match(I0, m_ZExt(m_Value(X))) && match(I1, m_Constant(C)) &&
16021602
I0->hasOneUse()) {
1603-
Constant *NarrowC = ConstantExpr::getTrunc(C, X->getType());
1604-
if (ConstantExpr::getZExt(NarrowC, II->getType()) == C) {
1603+
if (Constant *NarrowC = getLosslessUnsignedTrunc(C, X->getType())) {
16051604
Value *NarrowMaxMin = Builder.CreateBinaryIntrinsic(IID, X, NarrowC);
16061605
return CastInst::Create(Instruction::ZExt, NarrowMaxMin, II->getType());
16071606
}
@@ -1623,8 +1622,7 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
16231622
Constant *C;
16241623
if (match(I0, m_SExt(m_Value(X))) && match(I1, m_Constant(C)) &&
16251624
I0->hasOneUse()) {
1626-
Constant *NarrowC = ConstantExpr::getTrunc(C, X->getType());
1627-
if (ConstantExpr::getSExt(NarrowC, II->getType()) == C) {
1625+
if (Constant *NarrowC = getLosslessSignedTrunc(C, X->getType())) {
16281626
Value *NarrowMaxMin = Builder.CreateBinaryIntrinsic(IID, X, NarrowC);
16291627
return CastInst::Create(Instruction::SExt, NarrowMaxMin, II->getType());
16301628
}

llvm/lib/Transforms/InstCombine/InstCombineInternal.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,24 @@ class LLVM_LIBRARY_VISIBILITY InstCombinerImpl final
219219
bool fmulByZeroIsZero(Value *MulVal, FastMathFlags FMF,
220220
const Instruction *CtxI) const;
221221

222+
Constant *getLosslessUnsignedTrunc(Constant *C, Type *TruncTy) {
223+
Constant *TruncC = ConstantExpr::getTrunc(C, TruncTy);
224+
Constant *ExtTruncC =
225+
ConstantFoldCastOperand(Instruction::ZExt, TruncC, C->getType(), DL);
226+
if (ExtTruncC && ExtTruncC == C)
227+
return TruncC;
228+
return nullptr;
229+
}
230+
231+
Constant *getLosslessSignedTrunc(Constant *C, Type *TruncTy) {
232+
Constant *TruncC = ConstantExpr::getTrunc(C, TruncTy);
233+
Constant *ExtTruncC =
234+
ConstantFoldCastOperand(Instruction::SExt, TruncC, C->getType(), DL);
235+
if (ExtTruncC && ExtTruncC == C)
236+
return TruncC;
237+
return nullptr;
238+
}
239+
222240
private:
223241
bool annotateAnyAllocSite(CallBase &Call, const TargetLibraryInfo *TLI);
224242
bool isDesirableIntType(unsigned BitWidth) const;

llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,7 +1268,7 @@ static Value *takeLog2(IRBuilderBase &Builder, Value *Op, unsigned Depth,
12681268
/// If we have zero-extended operands of an unsigned div or rem, we may be able
12691269
/// to narrow the operation (sink the zext below the math).
12701270
static Instruction *narrowUDivURem(BinaryOperator &I,
1271-
InstCombiner::BuilderTy &Builder) {
1271+
InstCombinerImpl &IC) {
12721272
Instruction::BinaryOps Opcode = I.getOpcode();
12731273
Value *N = I.getOperand(0);
12741274
Value *D = I.getOperand(1);
@@ -1278,32 +1278,32 @@ static Instruction *narrowUDivURem(BinaryOperator &I,
12781278
X->getType() == Y->getType() && (N->hasOneUse() || D->hasOneUse())) {
12791279
// udiv (zext X), (zext Y) --> zext (udiv X, Y)
12801280
// urem (zext X), (zext Y) --> zext (urem X, Y)
1281-
Value *NarrowOp = Builder.CreateBinOp(Opcode, X, Y);
1281+
Value *NarrowOp = IC.Builder.CreateBinOp(Opcode, X, Y);
12821282
return new ZExtInst(NarrowOp, Ty);
12831283
}
12841284

12851285
Constant *C;
12861286
if (isa<Instruction>(N) && match(N, m_OneUse(m_ZExt(m_Value(X)))) &&
12871287
match(D, m_Constant(C))) {
12881288
// If the constant is the same in the smaller type, use the narrow version.
1289-
Constant *TruncC = ConstantExpr::getTrunc(C, X->getType());
1290-
if (ConstantExpr::getZExt(TruncC, Ty) != C)
1289+
Constant *TruncC = IC.getLosslessUnsignedTrunc(C, X->getType());
1290+
if (!TruncC)
12911291
return nullptr;
12921292

12931293
// udiv (zext X), C --> zext (udiv X, C')
12941294
// urem (zext X), C --> zext (urem X, C')
1295-
return new ZExtInst(Builder.CreateBinOp(Opcode, X, TruncC), Ty);
1295+
return new ZExtInst(IC.Builder.CreateBinOp(Opcode, X, TruncC), Ty);
12961296
}
12971297
if (isa<Instruction>(D) && match(D, m_OneUse(m_ZExt(m_Value(X)))) &&
12981298
match(N, m_Constant(C))) {
12991299
// If the constant is the same in the smaller type, use the narrow version.
1300-
Constant *TruncC = ConstantExpr::getTrunc(C, X->getType());
1301-
if (ConstantExpr::getZExt(TruncC, Ty) != C)
1300+
Constant *TruncC = IC.getLosslessUnsignedTrunc(C, X->getType());
1301+
if (!TruncC)
13021302
return nullptr;
13031303

13041304
// udiv C, (zext X) --> zext (udiv C', X)
13051305
// urem C, (zext X) --> zext (urem C', X)
1306-
return new ZExtInst(Builder.CreateBinOp(Opcode, TruncC, X), Ty);
1306+
return new ZExtInst(IC.Builder.CreateBinOp(Opcode, TruncC, X), Ty);
13071307
}
13081308

13091309
return nullptr;
@@ -1351,7 +1351,7 @@ Instruction *InstCombinerImpl::visitUDiv(BinaryOperator &I) {
13511351
return CastInst::CreateZExtOrBitCast(Cmp, Ty);
13521352
}
13531353

1354-
if (Instruction *NarrowDiv = narrowUDivURem(I, Builder))
1354+
if (Instruction *NarrowDiv = narrowUDivURem(I, *this))
13551355
return NarrowDiv;
13561356

13571357
// If the udiv operands are non-overflowing multiplies with a common operand,
@@ -1941,7 +1941,7 @@ Instruction *InstCombinerImpl::visitURem(BinaryOperator &I) {
19411941
if (Instruction *common = commonIRemTransforms(I))
19421942
return common;
19431943

1944-
if (Instruction *NarrowRem = narrowUDivURem(I, Builder))
1944+
if (Instruction *NarrowRem = narrowUDivURem(I, *this))
19451945
return NarrowRem;
19461946

19471947
// X urem Y -> X and Y-1, where Y is a power of 2,

llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -825,8 +825,8 @@ Instruction *InstCombinerImpl::foldPHIArgZextsIntoPHI(PHINode &Phi) {
825825
NumZexts++;
826826
} else if (auto *C = dyn_cast<Constant>(V)) {
827827
// Make sure that constants can fit in the new type.
828-
Constant *Trunc = ConstantExpr::getTrunc(C, NarrowType);
829-
if (ConstantExpr::getZExt(Trunc, C->getType()) != C)
828+
Constant *Trunc = getLosslessUnsignedTrunc(C, NarrowType);
829+
if (!Trunc)
830830
return nullptr;
831831
NewIncoming.push_back(Trunc);
832832
NumConsts++;

0 commit comments

Comments
 (0)