Skip to content

Commit b4afade

Browse files
committed
[InstCombine] Avoid use of ConstantExpr::getZExtOrBitcast() (NFC)
Use the constant folding API instead. In the second case using IR builder should also work, but the way the instructions are created an inserted there is very unusual, so I've left it alone.
1 parent 512739e commit b4afade

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4246,7 +4246,12 @@ foldShiftIntoShiftInAnotherHandOfAndInICmp(ICmpInst &I, const SimplifyQuery SQ,
42464246
/*isNUW=*/false, SQ.getWithInstruction(&I)));
42474247
if (!NewShAmt)
42484248
return nullptr;
4249-
NewShAmt = ConstantExpr::getZExtOrBitCast(NewShAmt, WidestTy);
4249+
if (NewShAmt->getType() != WidestTy) {
4250+
NewShAmt =
4251+
ConstantFoldCastOperand(Instruction::ZExt, NewShAmt, WidestTy, SQ.DL);
4252+
if (!NewShAmt)
4253+
return nullptr;
4254+
}
42504255
unsigned WidestBitWidth = WidestTy->getScalarSizeInBits();
42514256

42524257
// Is the new shift amount smaller than the bit width?

llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,14 @@ Value *InstCombinerImpl::reassociateShiftAmtsOfTwoSameDirectionShifts(
136136

137137
assert(IdenticalShOpcodes && "Should not get here with different shifts.");
138138

139-
// All good, we can do this fold.
140-
NewShAmt = ConstantExpr::getZExtOrBitCast(NewShAmt, X->getType());
139+
if (NewShAmt->getType() != X->getType()) {
140+
NewShAmt = ConstantFoldCastOperand(Instruction::ZExt, NewShAmt,
141+
X->getType(), SQ.DL);
142+
if (!NewShAmt)
143+
return nullptr;
144+
}
141145

146+
// All good, we can do this fold.
142147
BinaryOperator *NewShift = BinaryOperator::Create(ShiftOpcode, X, NewShAmt);
143148

144149
// The flags can only be propagated if there wasn't a trunc.

0 commit comments

Comments
 (0)