[llvm] d9f36c4 - [ConstantFolding] Add ConstantFoldIntegerCast helper
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Wed Nov 1 03:13:19 PDT 2023
Author: Nikita Popov
Date: 2023-11-01T11:13:10+01:00
New Revision: d9f36c45da85b2086781a921606ea5f828b045ff
URL: https://github.com/llvm/llvm-project/commit/d9f36c45da85b2086781a921606ea5f828b045ff
DIFF: https://github.com/llvm/llvm-project/commit/d9f36c45da85b2086781a921606ea5f828b045ff.diff
LOG: [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.
Added:
Modified:
llvm/include/llvm/Analysis/ConstantFolding.h
llvm/lib/Analysis/ConstantFolding.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/Analysis/ConstantFolding.h b/llvm/include/llvm/Analysis/ConstantFolding.h
index 169a1d0c48e6501..1b194b07e86781c 100644
--- a/llvm/include/llvm/Analysis/ConstantFolding.h
+++ b/llvm/include/llvm/Analysis/ConstantFolding.h
@@ -114,6 +114,11 @@ Constant *ConstantFoldSelectInstruction(Constant *Cond, Constant *V1,
Constant *ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy,
const DataLayout &DL);
+/// Constant fold a zext, sext or trunc, depending on IsSigned and whether the
+/// DestTy is wider or narrower than C. Returns nullptr on failure.
+Constant *ConstantFoldIntegerCast(Constant *C, Type *DestTy, bool IsSigned,
+ const DataLayout &DL);
+
/// ConstantFoldInsertValueInstruction - Attempt to constant fold an insertvalue
/// instruction with the specified operands and indices. The constant result is
/// returned if successful; if not, null is returned.
diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index 2f164e80f24060a..353cf733a428c04 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -1217,10 +1217,11 @@ Constant *llvm::ConstantFoldCompareInstOperands(
Type *IntPtrTy = DL.getIntPtrType(CE0->getType());
// Convert the integer value to the right size to ensure we get the
// proper extension or truncation.
- Constant *C = ConstantExpr::getIntegerCast(CE0->getOperand(0),
- IntPtrTy, false);
- Constant *Null = Constant::getNullValue(C->getType());
- return ConstantFoldCompareInstOperands(Predicate, C, Null, DL, TLI);
+ if (Constant *C = ConstantFoldIntegerCast(CE0->getOperand(0), IntPtrTy,
+ /*IsSigned*/ false, DL)) {
+ Constant *Null = Constant::getNullValue(C->getType());
+ return ConstantFoldCompareInstOperands(Predicate, C, Null, DL, TLI);
+ }
}
// Only do this transformation if the int is intptrty in size, otherwise
@@ -1242,11 +1243,12 @@ Constant *llvm::ConstantFoldCompareInstOperands(
// Convert the integer value to the right size to ensure we get the
// proper extension or truncation.
- Constant *C0 = ConstantExpr::getIntegerCast(CE0->getOperand(0),
- IntPtrTy, false);
- Constant *C1 = ConstantExpr::getIntegerCast(CE1->getOperand(0),
- IntPtrTy, false);
- return ConstantFoldCompareInstOperands(Predicate, C0, C1, DL, TLI);
+ Constant *C0 = ConstantFoldIntegerCast(CE0->getOperand(0), IntPtrTy,
+ /*IsSigned*/ false, DL);
+ Constant *C1 = ConstantFoldIntegerCast(CE1->getOperand(0), IntPtrTy,
+ /*IsSigned*/ false, DL);
+ if (C0 && C1)
+ return ConstantFoldCompareInstOperands(Predicate, C0, C1, DL, TLI);
}
// Only do this transformation if the int is intptrty in size, otherwise
@@ -1401,9 +1403,9 @@ Constant *llvm::ConstantFoldCastOperand(unsigned Opcode, Constant *C,
// the width of a pointer, so it can't be done in ConstantExpr::getCast.
if (CE->getOpcode() == Instruction::IntToPtr) {
// zext/trunc the inttoptr to pointer size.
- FoldedValue = ConstantExpr::getIntegerCast(
- CE->getOperand(0), DL.getIntPtrType(CE->getType()),
- /*IsSigned=*/false);
+ FoldedValue = ConstantFoldIntegerCast(CE->getOperand(0),
+ DL.getIntPtrType(CE->getType()),
+ /*IsSigned=*/false, DL);
} else if (auto *GEP = dyn_cast<GEPOperator>(CE)) {
// If we have GEP, we can perform the following folds:
// (ptrtoint (gep null, x)) -> x
@@ -1431,8 +1433,8 @@ Constant *llvm::ConstantFoldCastOperand(unsigned Opcode, Constant *C,
}
if (FoldedValue) {
// Do a zext or trunc to get to the ptrtoint dest size.
- return ConstantExpr::getIntegerCast(FoldedValue, DestTy,
- /*IsSigned=*/false);
+ return ConstantFoldIntegerCast(FoldedValue, DestTy, /*IsSigned=*/false,
+ DL);
}
}
break;
@@ -1475,6 +1477,18 @@ Constant *llvm::ConstantFoldCastOperand(unsigned Opcode, Constant *C,
return ConstantFoldCastInstruction(Opcode, C, DestTy);
}
+Constant *llvm::ConstantFoldIntegerCast(Constant *C, Type *DestTy,
+ bool IsSigned, const DataLayout &DL) {
+ Type *SrcTy = C->getType();
+ if (SrcTy == DestTy)
+ return C;
+ if (SrcTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits())
+ return ConstantFoldCastOperand(Instruction::Trunc, C, DestTy, DL);
+ if (IsSigned)
+ return ConstantFoldCastOperand(Instruction::SExt, C, DestTy, DL);
+ return ConstantFoldCastOperand(Instruction::ZExt, C, DestTy, DL);
+}
+
//===----------------------------------------------------------------------===//
// Constant Folding for Calls
//
More information about the llvm-commits
mailing list