[IR] Add ImplicitTrunc argument to ConstantInt::get() (#170865)
Add an ImplicitTrunc argument to ConstantInt::get(), which allows
controlling whether implicit truncation of the value is permitted.
This argument currently defaults to true, but will be switched to false
in the future to guard against signed/unsigned confusion, similar to
what has already happened for APInt.
The argument gives an opt-out for cases where the truncation is
intended. The patch contains one illustrative example where this
happens.diff --git a/llvm/include/llvm/IR/Constants.h b/llvm/include/llvm/IR/Constants.h
index e3f2eb9..39a556a 100644
--- a/llvm/include/llvm/IR/Constants.h
+++ b/llvm/include/llvm/IR/Constants.h
@@ -112,19 +112,24 @@
/// If Ty is a vector type, return a Constant with a splat of the given
/// value. Otherwise return a ConstantInt for the given value.
- LLVM_ABI static Constant *get(Type *Ty, uint64_t V, bool IsSigned = false);
+ /// \param ImplicitTrunc Whether to allow implicit truncation of the value.
+ // TODO: Make ImplicitTrunc default to false.
+ LLVM_ABI static Constant *get(Type *Ty, uint64_t V, bool IsSigned = false,
+ bool ImplicitTrunc = true);
/// Return a ConstantInt with the specified integer value for the specified
/// type. If the type is wider than 64 bits, the value will be zero-extended
/// to fit the type, unless IsSigned is true, in which case the value will
/// be interpreted as a 64-bit signed integer and sign-extended to fit
/// the type.
- /// Get a ConstantInt for a specific value.
+ /// \param ImplicitTrunc Whether to allow implicit truncation of the value.
+ // TODO: Make ImplicitTrunc default to false.
LLVM_ABI static ConstantInt *get(IntegerType *Ty, uint64_t V,
- bool IsSigned = false);
+ bool IsSigned = false,
+ bool ImplicitTrunc = true);
/// Return a ConstantInt with the specified value for the specified type. The
- /// value V will be canonicalized to a an unsigned APInt. Accessing it with
+ /// value V will be canonicalized to an unsigned APInt. Accessing it with
/// either getSExtValue() or getZExtValue() will yield a correctly sized and
/// signed value for the type Ty.
/// Get a ConstantInt for a specific signed value.
diff --git a/llvm/lib/IR/Constants.cpp b/llvm/lib/IR/Constants.cpp
index 6b82da1..2f02027 100644
--- a/llvm/lib/IR/Constants.cpp
+++ b/llvm/lib/IR/Constants.cpp
@@ -960,8 +960,10 @@
return Slot.get();
}
-Constant *ConstantInt::get(Type *Ty, uint64_t V, bool isSigned) {
- Constant *C = get(cast<IntegerType>(Ty->getScalarType()), V, isSigned);
+Constant *ConstantInt::get(Type *Ty, uint64_t V, bool IsSigned,
+ bool ImplicitTrunc) {
+ Constant *C =
+ get(cast<IntegerType>(Ty->getScalarType()), V, IsSigned, ImplicitTrunc);
// For vectors, broadcast the value.
if (VectorType *VTy = dyn_cast<VectorType>(Ty))
@@ -970,11 +972,10 @@
return C;
}
-ConstantInt *ConstantInt::get(IntegerType *Ty, uint64_t V, bool isSigned) {
- // TODO: Avoid implicit trunc?
- // See https://github.com/llvm/llvm-project/issues/112510.
+ConstantInt *ConstantInt::get(IntegerType *Ty, uint64_t V, bool IsSigned,
+ bool ImplicitTrunc) {
return get(Ty->getContext(),
- APInt(Ty->getBitWidth(), V, isSigned, /*implicitTrunc=*/true));
+ APInt(Ty->getBitWidth(), V, IsSigned, ImplicitTrunc));
}
Constant *ConstantInt::get(Type *Ty, const APInt& V) {
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
index 743c4f5..8e7282a 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
@@ -3811,7 +3811,9 @@
if (VecToReduceCount.isFixed()) {
unsigned VectorSize = VecToReduceCount.getFixedValue();
return BinaryOperator::CreateMul(
- Splat, ConstantInt::get(Splat->getType(), VectorSize));
+ Splat,
+ ConstantInt::get(Splat->getType(), VectorSize, /*IsSigned=*/false,
+ /*ImplicitTrunc=*/true));
}
}
}