Skip to content

Commit d9f36c4

Browse files
committed
[ConstantFolding] Add ConstantFoldIntegerCast helper
This is intended as the replacement for ConstantExpr::getIntegerCast(), which does not require availability of the corresponding constant expressions. It just forwards to ConstantFoldCastOperand with the correct opcode.
1 parent 432e114 commit d9f36c4

File tree

2 files changed

+33
-14
lines changed

2 files changed

+33
-14
lines changed

llvm/include/llvm/Analysis/ConstantFolding.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ Constant *ConstantFoldSelectInstruction(Constant *Cond, Constant *V1,
114114
Constant *ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy,
115115
const DataLayout &DL);
116116

117+
/// Constant fold a zext, sext or trunc, depending on IsSigned and whether the
118+
/// DestTy is wider or narrower than C. Returns nullptr on failure.
119+
Constant *ConstantFoldIntegerCast(Constant *C, Type *DestTy, bool IsSigned,
120+
const DataLayout &DL);
121+
117122
/// ConstantFoldInsertValueInstruction - Attempt to constant fold an insertvalue
118123
/// instruction with the specified operands and indices. The constant result is
119124
/// returned if successful; if not, null is returned.

llvm/lib/Analysis/ConstantFolding.cpp

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,10 +1217,11 @@ Constant *llvm::ConstantFoldCompareInstOperands(
12171217
Type *IntPtrTy = DL.getIntPtrType(CE0->getType());
12181218
// Convert the integer value to the right size to ensure we get the
12191219
// proper extension or truncation.
1220-
Constant *C = ConstantExpr::getIntegerCast(CE0->getOperand(0),
1221-
IntPtrTy, false);
1222-
Constant *Null = Constant::getNullValue(C->getType());
1223-
return ConstantFoldCompareInstOperands(Predicate, C, Null, DL, TLI);
1220+
if (Constant *C = ConstantFoldIntegerCast(CE0->getOperand(0), IntPtrTy,
1221+
/*IsSigned*/ false, DL)) {
1222+
Constant *Null = Constant::getNullValue(C->getType());
1223+
return ConstantFoldCompareInstOperands(Predicate, C, Null, DL, TLI);
1224+
}
12241225
}
12251226

12261227
// Only do this transformation if the int is intptrty in size, otherwise
@@ -1242,11 +1243,12 @@ Constant *llvm::ConstantFoldCompareInstOperands(
12421243

12431244
// Convert the integer value to the right size to ensure we get the
12441245
// proper extension or truncation.
1245-
Constant *C0 = ConstantExpr::getIntegerCast(CE0->getOperand(0),
1246-
IntPtrTy, false);
1247-
Constant *C1 = ConstantExpr::getIntegerCast(CE1->getOperand(0),
1248-
IntPtrTy, false);
1249-
return ConstantFoldCompareInstOperands(Predicate, C0, C1, DL, TLI);
1246+
Constant *C0 = ConstantFoldIntegerCast(CE0->getOperand(0), IntPtrTy,
1247+
/*IsSigned*/ false, DL);
1248+
Constant *C1 = ConstantFoldIntegerCast(CE1->getOperand(0), IntPtrTy,
1249+
/*IsSigned*/ false, DL);
1250+
if (C0 && C1)
1251+
return ConstantFoldCompareInstOperands(Predicate, C0, C1, DL, TLI);
12501252
}
12511253

12521254
// Only do this transformation if the int is intptrty in size, otherwise
@@ -1401,9 +1403,9 @@ Constant *llvm::ConstantFoldCastOperand(unsigned Opcode, Constant *C,
14011403
// the width of a pointer, so it can't be done in ConstantExpr::getCast.
14021404
if (CE->getOpcode() == Instruction::IntToPtr) {
14031405
// zext/trunc the inttoptr to pointer size.
1404-
FoldedValue = ConstantExpr::getIntegerCast(
1405-
CE->getOperand(0), DL.getIntPtrType(CE->getType()),
1406-
/*IsSigned=*/false);
1406+
FoldedValue = ConstantFoldIntegerCast(CE->getOperand(0),
1407+
DL.getIntPtrType(CE->getType()),
1408+
/*IsSigned=*/false, DL);
14071409
} else if (auto *GEP = dyn_cast<GEPOperator>(CE)) {
14081410
// If we have GEP, we can perform the following folds:
14091411
// (ptrtoint (gep null, x)) -> x
@@ -1431,8 +1433,8 @@ Constant *llvm::ConstantFoldCastOperand(unsigned Opcode, Constant *C,
14311433
}
14321434
if (FoldedValue) {
14331435
// Do a zext or trunc to get to the ptrtoint dest size.
1434-
return ConstantExpr::getIntegerCast(FoldedValue, DestTy,
1435-
/*IsSigned=*/false);
1436+
return ConstantFoldIntegerCast(FoldedValue, DestTy, /*IsSigned=*/false,
1437+
DL);
14361438
}
14371439
}
14381440
break;
@@ -1475,6 +1477,18 @@ Constant *llvm::ConstantFoldCastOperand(unsigned Opcode, Constant *C,
14751477
return ConstantFoldCastInstruction(Opcode, C, DestTy);
14761478
}
14771479

1480+
Constant *llvm::ConstantFoldIntegerCast(Constant *C, Type *DestTy,
1481+
bool IsSigned, const DataLayout &DL) {
1482+
Type *SrcTy = C->getType();
1483+
if (SrcTy == DestTy)
1484+
return C;
1485+
if (SrcTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits())
1486+
return ConstantFoldCastOperand(Instruction::Trunc, C, DestTy, DL);
1487+
if (IsSigned)
1488+
return ConstantFoldCastOperand(Instruction::SExt, C, DestTy, DL);
1489+
return ConstantFoldCastOperand(Instruction::ZExt, C, DestTy, DL);
1490+
}
1491+
14781492
//===----------------------------------------------------------------------===//
14791493
// Constant Folding for Calls
14801494
//

0 commit comments

Comments
 (0)