Skip to content

Commit 6ce7461

Browse files
committed
[InstCombine] Avoid uses of ConstantExpr::getCast()
Add a generalized getLosslessTrunc() helper to simplify this.
1 parent 5d7672b commit 6ce7461

File tree

4 files changed

+24
-28
lines changed

4 files changed

+24
-28
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5588,25 +5588,20 @@ Instruction *InstCombinerImpl::foldICmpWithZextOrSext(ICmpInst &ICmp) {
55885588
if (!C)
55895589
return nullptr;
55905590

5591-
// Compute the constant that would happen if we truncated to SrcTy then
5592-
// re-extended to DestTy.
5591+
// If a lossless truncate is possible...
55935592
Type *SrcTy = CastOp0->getSrcTy();
5594-
Type *DestTy = CastOp0->getDestTy();
5595-
Constant *Res1 = ConstantExpr::getTrunc(C, SrcTy);
5596-
Constant *Res2 = ConstantExpr::getCast(CastOp0->getOpcode(), Res1, DestTy);
5597-
5598-
// If the re-extended constant didn't change...
5599-
if (Res2 == C) {
5593+
Constant *Res = getLosslessTrunc(C, SrcTy, CastOp0->getOpcode());
5594+
if (Res) {
56005595
if (ICmp.isEquality())
5601-
return new ICmpInst(ICmp.getPredicate(), X, Res1);
5596+
return new ICmpInst(ICmp.getPredicate(), X, Res);
56025597

56035598
// A signed comparison of sign extended values simplifies into a
56045599
// signed comparison.
56055600
if (IsSignedExt && IsSignedCmp)
5606-
return new ICmpInst(ICmp.getPredicate(), X, Res1);
5601+
return new ICmpInst(ICmp.getPredicate(), X, Res);
56075602

56085603
// The other three cases all fold into an unsigned comparison.
5609-
return new ICmpInst(ICmp.getUnsignedPredicate(), X, Res1);
5604+
return new ICmpInst(ICmp.getUnsignedPredicate(), X, Res);
56105605
}
56115606

56125607
// The re-extended constant changed, partly changed (in the case of a vector),

llvm/lib/Transforms/InstCombine/InstCombineInternal.h

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -219,22 +219,21 @@ 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) {
222+
Constant *getLosslessTrunc(Constant *C, Type *TruncTy, unsigned ExtOp) {
223223
Constant *TruncC = ConstantExpr::getTrunc(C, TruncTy);
224224
Constant *ExtTruncC =
225-
ConstantFoldCastOperand(Instruction::ZExt, TruncC, C->getType(), DL);
225+
ConstantFoldCastOperand(ExtOp, TruncC, C->getType(), DL);
226226
if (ExtTruncC && ExtTruncC == C)
227227
return TruncC;
228228
return nullptr;
229229
}
230230

231+
Constant *getLosslessUnsignedTrunc(Constant *C, Type *TruncTy) {
232+
return getLosslessTrunc(C, TruncTy, Instruction::ZExt);
233+
}
234+
231235
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;
236+
return getLosslessTrunc(C, TruncTy, Instruction::SExt);
238237
}
239238

240239
private:

llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2083,9 +2083,8 @@ Instruction *InstCombinerImpl::foldSelectExtConst(SelectInst &Sel) {
20832083
// If the constant is the same after truncation to the smaller type and
20842084
// extension to the original type, we can narrow the select.
20852085
Type *SelType = Sel.getType();
2086-
Constant *TruncC = ConstantExpr::getTrunc(C, SmallType);
2087-
Constant *ExtC = ConstantExpr::getCast(ExtOpcode, TruncC, SelType);
2088-
if (ExtC == C && ExtInst->hasOneUse()) {
2086+
Constant *TruncC = getLosslessTrunc(C, SmallType, ExtOpcode);
2087+
if (TruncC && ExtInst->hasOneUse()) {
20892088
Value *TruncCVal = cast<Value>(TruncC);
20902089
if (ExtInst == Sel.getFalseValue())
20912090
std::swap(X, TruncCVal);
@@ -2103,7 +2102,8 @@ Instruction *InstCombinerImpl::foldSelectExtConst(SelectInst &Sel) {
21032102
// select X, (sext X), C --> select X, -1, C
21042103
// select X, (zext X), C --> select X, 1, C
21052104
Constant *One = ConstantInt::getTrue(SmallType);
2106-
Constant *AllOnesOrOne = ConstantExpr::getCast(ExtOpcode, One, SelType);
2105+
Value *AllOnesOrOne =
2106+
Builder.CreateCast((Instruction::CastOps)ExtOpcode, One, SelType);
21072107
return SelectInst::Create(Cond, AllOnesOrOne, C, "", nullptr, &Sel);
21082108
} else {
21092109
// select X, C, (sext X) --> select X, C, 0

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -345,10 +345,12 @@ static bool simplifyAssocCastAssoc(BinaryOperator *BinOp1,
345345

346346
// Fold the constants together in the destination type:
347347
// (op (cast (op X, C2)), C1) --> (op (cast X), FoldedC)
348+
const DataLayout &DL = IC.getDataLayout();
348349
Type *DestTy = C1->getType();
349-
Constant *CastC2 = ConstantExpr::getCast(CastOpcode, C2, DestTy);
350-
Constant *FoldedC =
351-
ConstantFoldBinaryOpOperands(AssocOpcode, C1, CastC2, IC.getDataLayout());
350+
Constant *CastC2 = ConstantFoldCastOperand(CastOpcode, C2, DestTy, DL);
351+
if (!CastC2)
352+
return false;
353+
Constant *FoldedC = ConstantFoldBinaryOpOperands(AssocOpcode, C1, CastC2, DL);
352354
if (!FoldedC)
353355
return false;
354356

@@ -1898,8 +1900,8 @@ Instruction *InstCombinerImpl::narrowMathIfNoOverflow(BinaryOperator &BO) {
18981900
Constant *WideC;
18991901
if (!Op0->hasOneUse() || !match(Op1, m_Constant(WideC)))
19001902
return nullptr;
1901-
Constant *NarrowC = ConstantExpr::getTrunc(WideC, X->getType());
1902-
if (ConstantExpr::getCast(CastOpc, NarrowC, BO.getType()) != WideC)
1903+
Constant *NarrowC = getLosslessTrunc(WideC, X->getType(), CastOpc);
1904+
if (!NarrowC)
19031905
return nullptr;
19041906
Y = NarrowC;
19051907
}

0 commit comments

Comments
 (0)