Skip to content

Commit 7b65219

Browse files
authored
[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.
1 parent 3d24efd commit 7b65219

File tree

3 files changed

+19
-11
lines changed

3 files changed

+19
-11
lines changed

llvm/include/llvm/IR/Constants.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,19 +112,24 @@ class ConstantInt final : public ConstantData {
112112

113113
/// If Ty is a vector type, return a Constant with a splat of the given
114114
/// value. Otherwise return a ConstantInt for the given value.
115-
LLVM_ABI static Constant *get(Type *Ty, uint64_t V, bool IsSigned = false);
115+
/// \param ImplicitTrunc Whether to allow implicit truncation of the value.
116+
// TODO: Make ImplicitTrunc default to false.
117+
LLVM_ABI static Constant *get(Type *Ty, uint64_t V, bool IsSigned = false,
118+
bool ImplicitTrunc = true);
116119

117120
/// Return a ConstantInt with the specified integer value for the specified
118121
/// type. If the type is wider than 64 bits, the value will be zero-extended
119122
/// to fit the type, unless IsSigned is true, in which case the value will
120123
/// be interpreted as a 64-bit signed integer and sign-extended to fit
121124
/// the type.
122-
/// Get a ConstantInt for a specific value.
125+
/// \param ImplicitTrunc Whether to allow implicit truncation of the value.
126+
// TODO: Make ImplicitTrunc default to false.
123127
LLVM_ABI static ConstantInt *get(IntegerType *Ty, uint64_t V,
124-
bool IsSigned = false);
128+
bool IsSigned = false,
129+
bool ImplicitTrunc = true);
125130

126131
/// Return a ConstantInt with the specified value for the specified type. The
127-
/// value V will be canonicalized to a an unsigned APInt. Accessing it with
132+
/// value V will be canonicalized to an unsigned APInt. Accessing it with
128133
/// either getSExtValue() or getZExtValue() will yield a correctly sized and
129134
/// signed value for the type Ty.
130135
/// Get a ConstantInt for a specific signed value.

llvm/lib/IR/Constants.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -960,8 +960,10 @@ ConstantInt *ConstantInt::get(LLVMContext &Context, ElementCount EC,
960960
return Slot.get();
961961
}
962962

963-
Constant *ConstantInt::get(Type *Ty, uint64_t V, bool isSigned) {
964-
Constant *C = get(cast<IntegerType>(Ty->getScalarType()), V, isSigned);
963+
Constant *ConstantInt::get(Type *Ty, uint64_t V, bool IsSigned,
964+
bool ImplicitTrunc) {
965+
Constant *C =
966+
get(cast<IntegerType>(Ty->getScalarType()), V, IsSigned, ImplicitTrunc);
965967

966968
// For vectors, broadcast the value.
967969
if (VectorType *VTy = dyn_cast<VectorType>(Ty))
@@ -970,11 +972,10 @@ Constant *ConstantInt::get(Type *Ty, uint64_t V, bool isSigned) {
970972
return C;
971973
}
972974

973-
ConstantInt *ConstantInt::get(IntegerType *Ty, uint64_t V, bool isSigned) {
974-
// TODO: Avoid implicit trunc?
975-
// See https://github.com/llvm/llvm-project/issues/112510.
975+
ConstantInt *ConstantInt::get(IntegerType *Ty, uint64_t V, bool IsSigned,
976+
bool ImplicitTrunc) {
976977
return get(Ty->getContext(),
977-
APInt(Ty->getBitWidth(), V, isSigned, /*implicitTrunc=*/true));
978+
APInt(Ty->getBitWidth(), V, IsSigned, ImplicitTrunc));
978979
}
979980

980981
Constant *ConstantInt::get(Type *Ty, const APInt& V) {

llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3811,7 +3811,9 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
38113811
if (VecToReduceCount.isFixed()) {
38123812
unsigned VectorSize = VecToReduceCount.getFixedValue();
38133813
return BinaryOperator::CreateMul(
3814-
Splat, ConstantInt::get(Splat->getType(), VectorSize));
3814+
Splat,
3815+
ConstantInt::get(Splat->getType(), VectorSize, /*IsSigned=*/false,
3816+
/*ImplicitTrunc=*/true));
38153817
}
38163818
}
38173819
}

0 commit comments

Comments
 (0)