[clang] [llvm] [ConstantInt] Disable implicit truncation in ConstantInt::get() (PR #171456)
Nikita Popov via cfe-commits
cfe-commits at lists.llvm.org
Tue Dec 16 02:59:28 PST 2025
https://github.com/nikic updated https://github.com/llvm/llvm-project/pull/171456
>From 0fa9770eee2ab28471bf12361b1a528360f025dc Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Tue, 9 Dec 2025 12:04:38 +0100
Subject: [PATCH 01/26] [IR] Disable implicit truncation in ConstantInt::get()
by default
---
llvm/include/llvm/IR/Constants.h | 6 ++----
1 file changed, 2 insertions(+), 4 deletions(-)
diff --git a/llvm/include/llvm/IR/Constants.h b/llvm/include/llvm/IR/Constants.h
index 39a556abe935b..6aa79846f10f5 100644
--- a/llvm/include/llvm/IR/Constants.h
+++ b/llvm/include/llvm/IR/Constants.h
@@ -113,9 +113,8 @@ class ConstantInt final : public ConstantData {
/// If Ty is a vector type, return a Constant with a splat of the given
/// value. Otherwise return a ConstantInt for the given value.
/// \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);
+ bool ImplicitTrunc = false);
/// 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
@@ -123,10 +122,9 @@ class ConstantInt final : public ConstantData {
/// be interpreted as a 64-bit signed integer and sign-extended to fit
/// the type.
/// \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 ImplicitTrunc = true);
+ bool ImplicitTrunc = false);
/// Return a ConstantInt with the specified value for the specified type. The
/// value V will be canonicalized to an unsigned APInt. Accessing it with
>From f1d774b65cfaeb2d4b636f4d1df2a6a7a3628151 Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Tue, 9 Dec 2025 12:18:21 +0100
Subject: [PATCH 02/26] [SDAG] Allow implicit trunc in BUILD_VECTOR
legalization
---
llvm/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/llvm/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp b/llvm/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp
index 1630dd783e821..fc3e9ff0d6238 100644
--- a/llvm/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp
@@ -2051,7 +2051,8 @@ SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
// we don't want a v16i8 to become a v16i32 for example.
const ConstantInt *CI = V->getConstantIntValue();
CV.push_back(ConstantInt::get(EltVT.getTypeForEVT(*DAG.getContext()),
- CI->getZExtValue()));
+ CI->getZExtValue(), /*IsSigned=*/false,
+ /*ImplicitTrunc=*/true));
}
} else {
assert(Node->getOperand(i).isUndef());
>From b94c8cb630eb5da3d1107d41a08806aab3318dce Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Tue, 9 Dec 2025 12:22:32 +0100
Subject: [PATCH 03/26] [SCEV] Allow implicit truncation for now, to reduce
scope
---
llvm/lib/Analysis/ScalarEvolution.cpp | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
index e1f90264be7a2..770de89eccfb9 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -485,7 +485,10 @@ const SCEV *ScalarEvolution::getConstant(const APInt &Val) {
const SCEV *
ScalarEvolution::getConstant(Type *Ty, uint64_t V, bool isSigned) {
IntegerType *ITy = cast<IntegerType>(getEffectiveSCEVType(Ty));
- return getConstant(ConstantInt::get(ITy, V, isSigned));
+ // TODO: Avoid implicit trunc?
+ // See https://github.com/llvm/llvm-project/issues/112510.
+ return getConstant(
+ ConstantInt::get(ITy, V, isSigned, /*ImplicitTrunc=*/true));
}
const SCEV *ScalarEvolution::getVScale(Type *Ty) {
>From 3d2d98ed9a95930a755f250d4d8247796173c644 Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Tue, 9 Dec 2025 12:43:29 +0100
Subject: [PATCH 04/26] [MachineIRBuilder] Allow implicit trunc for now
To limit scope.
---
llvm/lib/CodeGen/GlobalISel/MachineIRBuilder.cpp | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/llvm/lib/CodeGen/GlobalISel/MachineIRBuilder.cpp b/llvm/lib/CodeGen/GlobalISel/MachineIRBuilder.cpp
index 3906b311addf0..f4779d9a122f0 100644
--- a/llvm/lib/CodeGen/GlobalISel/MachineIRBuilder.cpp
+++ b/llvm/lib/CodeGen/GlobalISel/MachineIRBuilder.cpp
@@ -363,7 +363,10 @@ MachineInstrBuilder MachineIRBuilder::buildConstant(const DstOp &Res,
int64_t Val) {
auto IntN = IntegerType::get(getMF().getFunction().getContext(),
Res.getLLTTy(*getMRI()).getScalarSizeInBits());
- ConstantInt *CI = ConstantInt::get(IntN, Val, true);
+ // TODO: Avoid implicit trunc?
+ // See https://github.com/llvm/llvm-project/issues/112510.
+ ConstantInt *CI =
+ ConstantInt::get(IntN, Val, /*isSigned=*/true, /*implicitTrunc=*/true);
return buildConstant(Res, *CI);
}
>From 51a80781eda0d6ac0b8a7b644747795f590beff5 Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Tue, 9 Dec 2025 14:51:18 +0100
Subject: [PATCH 05/26] [FastISel] Allow implicit trunc in FastISel for now
To reduce scope.
---
llvm/lib/CodeGen/SelectionDAG/FastISel.cpp | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/llvm/lib/CodeGen/SelectionDAG/FastISel.cpp b/llvm/lib/CodeGen/SelectionDAG/FastISel.cpp
index 51391f1aeecde..03727b77b3e1a 100644
--- a/llvm/lib/CodeGen/SelectionDAG/FastISel.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/FastISel.cpp
@@ -1943,7 +1943,10 @@ Register FastISel::fastEmit_ri_(MVT VT, unsigned Opcode, Register Op0,
// fast-isel, which would be very slow.
IntegerType *ITy =
IntegerType::get(FuncInfo.Fn->getContext(), VT.getSizeInBits());
- MaterialReg = getRegForValue(ConstantInt::get(ITy, Imm));
+ // TODO: Avoid implicit trunc?
+ // See https://github.com/llvm/llvm-project/issues/112510.
+ MaterialReg = getRegForValue(
+ ConstantInt::get(ITy, Imm, /*IsSigned=*/false, /*ImplicitTrunc=*/true));
if (!MaterialReg)
return Register();
}
>From e04b7fd77c3af113ad2d8a1d2146cc7a1f5d7a32 Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Tue, 9 Dec 2025 15:11:05 +0100
Subject: [PATCH 06/26] [SLSR] Allow implicit truncation for element size
For now, we should implicitly truncate overflowing allocation
sizes when expanding GEPs, like we do in other places.
---
llvm/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/llvm/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp b/llvm/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp
index 2ad6f7e2e27cd..9591cd983df15 100644
--- a/llvm/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp
+++ b/llvm/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp
@@ -1104,7 +1104,9 @@ void StraightLineStrengthReduce::allocateCandidatesAndFindBasisForGEP(
Value *ArrayIdx = GEP->getOperand(I);
uint64_t ElementSize = GTI.getSequentialElementStride(*DL);
IntegerType *PtrIdxTy = cast<IntegerType>(DL->getIndexType(GEP->getType()));
- ConstantInt *ElementSizeIdx = ConstantInt::get(PtrIdxTy, ElementSize, true);
+ // If the element size overflow the type, truncate.
+ ConstantInt *ElementSizeIdx =
+ ConstantInt::get(PtrIdxTy, ElementSize, true, /*ImplicitTrunc=*/true);
if (ArrayIdx->getType()->getIntegerBitWidth() <=
DL->getIndexSizeInBits(GEP->getAddressSpace())) {
// Skip factoring if ArrayIdx is wider than the index size, because
>From 438b1ba421efc842e184ef69a1e7e4b3988006cd Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Mon, 15 Dec 2025 12:19:31 +0100
Subject: [PATCH 07/26] [MemorySanitizer] Allow implicit truncation for now
---
llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp b/llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp
index 32ee16c89b4fe..9909037ba8ec5 100644
--- a/llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp
+++ b/llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp
@@ -1791,7 +1791,10 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
constToIntPtr(VectTy->getElementType(), C));
}
assert(IntPtrTy == MS.IntptrTy);
- return ConstantInt::get(MS.IntptrTy, C);
+ // TODO: Avoid implicit trunc?
+ // See https://github.com/llvm/llvm-project/issues/112510.
+ return ConstantInt::get(MS.IntptrTy, C, /*IsSigned=*/false,
+ /*ImplicitTrunc=*/true);
}
/// Returns the integer shadow offset that corresponds to a given
>From f53166e3a5205ec9d7943f97572f87f27b3782d3 Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Mon, 15 Dec 2025 12:21:37 +0100
Subject: [PATCH 08/26] [SPIRV] Allow implicit truncation for now
---
llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp b/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp
index 5d96a67500dff..7325dee73d8f2 100644
--- a/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp
@@ -436,7 +436,10 @@ Register SPIRVGlobalRegistry::buildConstantInt(uint64_t Val,
assert(SpvType);
auto &MF = MIRBuilder.getMF();
const IntegerType *Ty = cast<IntegerType>(getTypeForSPIRVType(SpvType));
- auto *const CI = ConstantInt::get(const_cast<IntegerType *>(Ty), Val);
+ // TODO: Avoid implicit trunc?
+ // See https://github.com/llvm/llvm-project/issues/112510.
+ auto *const CI = ConstantInt::get(const_cast<IntegerType *>(Ty), Val,
+ /*IsSigned=*/false, /*ImplicitTrunc=*/true);
Register Res = find(CI, &MF);
if (Res.isValid())
return Res;
>From 35b82e8141831932e433a225453bc5e075aeb660 Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Mon, 15 Dec 2025 12:22:59 +0100
Subject: [PATCH 09/26] [LSR] Allow implicit truncation for now
---
llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp b/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
index e12caa2136962..7f28ea6f5c875 100644
--- a/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
+++ b/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
@@ -331,7 +331,10 @@ class Immediate : public details::FixedOrScalableQuantity<Immediate, int64_t> {
}
const SCEV *getUnknownSCEV(ScalarEvolution &SE, Type *Ty) const {
- const SCEV *SU = SE.getUnknown(ConstantInt::getSigned(Ty, Quantity));
+ // TODO: Avoid implicit trunc?
+ // See https://github.com/llvm/llvm-project/issues/112510.
+ const SCEV *SU = SE.getUnknown(ConstantInt::get(
+ Ty, Quantity, /*IsSigned=*/true, /*ImplicitTrunc=*/true));
if (Scalable)
SU = SE.getMulExpr(SU, SE.getVScale(SU->getType()));
return SU;
>From ffd9918012d0768d9efd9a6773c90b15cbfe53ea Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Mon, 15 Dec 2025 12:31:55 +0100
Subject: [PATCH 10/26] [CGP] Allow implicit truncation for now
---
llvm/lib/CodeGen/CodeGenPrepare.cpp | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/llvm/lib/CodeGen/CodeGenPrepare.cpp b/llvm/lib/CodeGen/CodeGenPrepare.cpp
index 587c1372b19cb..7ec6113aaeafc 100644
--- a/llvm/lib/CodeGen/CodeGenPrepare.cpp
+++ b/llvm/lib/CodeGen/CodeGenPrepare.cpp
@@ -6881,7 +6881,10 @@ bool CodeGenPrepare::splitLargeGEPOffsets() {
}
IRBuilder<> NewBaseBuilder(NewBaseInsertBB, NewBaseInsertPt);
// Create a new base.
- Value *BaseIndex = ConstantInt::get(PtrIdxTy, BaseOffset);
+ // TODO: Avoid implicit trunc?
+ // See https://github.com/llvm/llvm-project/issues/112510.
+ Value *BaseIndex = ConstantInt::get(
+ PtrIdxTy, BaseOffset, /*IsSigned=*/true, /*ImplicitTrunc=*/true);
NewBaseGEP = OldBase;
if (NewBaseGEP->getType() != I8PtrTy)
NewBaseGEP = NewBaseBuilder.CreatePointerCast(NewBaseGEP, I8PtrTy);
>From be5506921370d44afe25bd74012c2ce6cd694aa9 Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Mon, 15 Dec 2025 12:41:31 +0100
Subject: [PATCH 11/26] [CGBuilder] Use getSigned() for CharUnits
---
clang/lib/CodeGen/CGBuilder.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/lib/CodeGen/CGBuilder.h b/clang/lib/CodeGen/CGBuilder.h
index 090f75d3b5d3c..a8d71b6f393b4 100644
--- a/clang/lib/CodeGen/CGBuilder.h
+++ b/clang/lib/CodeGen/CGBuilder.h
@@ -101,7 +101,7 @@ class CGBuilderTy : public CGBuilderBaseTy {
: CGBuilderBaseTy(BB), TypeCache(TypeCache) {}
llvm::ConstantInt *getSize(CharUnits N) {
- return llvm::ConstantInt::get(TypeCache.SizeTy, N.getQuantity());
+ return llvm::ConstantInt::getSigned(TypeCache.SizeTy, N.getQuantity());
}
llvm::ConstantInt *getSize(uint64_t N) {
return llvm::ConstantInt::get(TypeCache.SizeTy, N);
>From ebdf884f239bbbfedbd5f2653339753e8c97a033 Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Mon, 15 Dec 2025 12:58:20 +0100
Subject: [PATCH 12/26] [CGOpenMP] Use getSigned() for NumTeams constant
This value may be -1.
---
clang/lib/CodeGen/CGOpenMPRuntime.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/lib/CodeGen/CGOpenMPRuntime.cpp b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
index 43ec1572aaf90..39cf3052011e1 100644
--- a/clang/lib/CodeGen/CGOpenMPRuntime.cpp
+++ b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
@@ -6477,7 +6477,7 @@ llvm::Value *CGOpenMPRuntime::emitNumTeamsForTargetDirective(
}
assert(MinNT == MaxNT && "Num threads ranges require handling here.");
- return llvm::ConstantInt::get(CGF.Int32Ty, MinNT);
+ return llvm::ConstantInt::getSigned(CGF.Int32Ty, MinNT);
}
/// Check for a num threads constant value (stored in \p DefaultVal), or
>From 0f2fe6b2c929963f4fc87fd2277e6adaa40551c8 Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Mon, 15 Dec 2025 13:01:35 +0100
Subject: [PATCH 13/26] [CodeGen] Use getSigned() for negative value
---
clang/lib/CodeGen/ABIInfoImpl.cpp | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/clang/lib/CodeGen/ABIInfoImpl.cpp b/clang/lib/CodeGen/ABIInfoImpl.cpp
index 1e3ac2e31870f..8ad77ee515d49 100644
--- a/clang/lib/CodeGen/ABIInfoImpl.cpp
+++ b/clang/lib/CodeGen/ABIInfoImpl.cpp
@@ -153,7 +153,8 @@ llvm::Value *CodeGen::emitRoundPointerUpToAlignment(CodeGenFunction &CGF,
CGF.Builder.getInt8Ty(), Ptr, Align.getQuantity() - 1);
return CGF.Builder.CreateIntrinsic(
llvm::Intrinsic::ptrmask, {Ptr->getType(), CGF.IntPtrTy},
- {RoundUp, llvm::ConstantInt::get(CGF.IntPtrTy, -Align.getQuantity())},
+ {RoundUp,
+ llvm::ConstantInt::getSigned(CGF.IntPtrTy, -Align.getQuantity())},
nullptr, Ptr->getName() + ".aligned");
}
>From 88a3f0c4e56dccafa5f72a6db934bc6a18544221 Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Mon, 15 Dec 2025 13:02:52 +0100
Subject: [PATCH 14/26] [ItaniumCXXABI] Use getSigned() for negative constant
---
clang/lib/CodeGen/ItaniumCXXABI.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/lib/CodeGen/ItaniumCXXABI.cpp b/clang/lib/CodeGen/ItaniumCXXABI.cpp
index c5db314575810..7b88d35fa09e0 100644
--- a/clang/lib/CodeGen/ItaniumCXXABI.cpp
+++ b/clang/lib/CodeGen/ItaniumCXXABI.cpp
@@ -1627,7 +1627,7 @@ llvm::Value *ItaniumCXXABI::EmitTypeid(CodeGenFunction &CGF,
// Load the type info.
Value = CGF.Builder.CreateCall(
CGM.getIntrinsic(llvm::Intrinsic::load_relative, {CGM.Int32Ty}),
- {Value, llvm::ConstantInt::get(CGM.Int32Ty, -4)});
+ {Value, llvm::ConstantInt::getSigned(CGM.Int32Ty, -4)});
} else {
// Load the type info.
Value =
>From 0e4736e39d84f97518a65121ab96d4a6656f6af8 Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Mon, 15 Dec 2025 13:04:34 +0100
Subject: [PATCH 15/26] [ItaniumCXXABI] Use getSigned() for signed offset
---
clang/lib/CodeGen/ItaniumCXXABI.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/lib/CodeGen/ItaniumCXXABI.cpp b/clang/lib/CodeGen/ItaniumCXXABI.cpp
index 7b88d35fa09e0..10cdf4e479ccf 100644
--- a/clang/lib/CodeGen/ItaniumCXXABI.cpp
+++ b/clang/lib/CodeGen/ItaniumCXXABI.cpp
@@ -4556,7 +4556,7 @@ void ItaniumRTTIBuilder::BuildVMIClassTypeInfo(const CXXRecordDecl *RD) {
if (Base.getAccessSpecifier() == AS_public)
OffsetFlags |= BCTI_Public;
- Fields.push_back(llvm::ConstantInt::get(OffsetFlagsLTy, OffsetFlags));
+ Fields.push_back(llvm::ConstantInt::getSigned(OffsetFlagsLTy, OffsetFlags));
}
}
>From d2741c34ae361bfd6342a58a11e7e776d2291a78 Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Mon, 15 Dec 2025 13:08:16 +0100
Subject: [PATCH 16/26] [CGVtables] Use getSigned() for signed offset
---
clang/lib/CodeGen/CGVTables.cpp | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/clang/lib/CodeGen/CGVTables.cpp b/clang/lib/CodeGen/CGVTables.cpp
index 040290cb345ef..f0d7e7003f2d9 100644
--- a/clang/lib/CodeGen/CGVTables.cpp
+++ b/clang/lib/CodeGen/CGVTables.cpp
@@ -732,14 +732,14 @@ static void AddPointerLayoutOffset(const CodeGenModule &CGM,
ConstantArrayBuilder &builder,
CharUnits offset) {
builder.add(llvm::ConstantExpr::getIntToPtr(
- llvm::ConstantInt::get(CGM.PtrDiffTy, offset.getQuantity()),
+ llvm::ConstantInt::getSigned(CGM.PtrDiffTy, offset.getQuantity()),
CGM.GlobalsInt8PtrTy));
}
static void AddRelativeLayoutOffset(const CodeGenModule &CGM,
ConstantArrayBuilder &builder,
CharUnits offset) {
- builder.add(llvm::ConstantInt::get(CGM.Int32Ty, offset.getQuantity()));
+ builder.add(llvm::ConstantInt::getSigned(CGM.Int32Ty, offset.getQuantity()));
}
void CodeGenVTables::addVTableComponent(ConstantArrayBuilder &builder,
>From a8f57bd6420fa619d44b22099c4174e8dfeca99e Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Mon, 15 Dec 2025 13:12:10 +0100
Subject: [PATCH 17/26] [PatternInit] Explicitly allow implicit truncation
(NFC)
It's okay if the pattern value gets truncated here, it's a splat.
---
clang/lib/CodeGen/PatternInit.cpp | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/clang/lib/CodeGen/PatternInit.cpp b/clang/lib/CodeGen/PatternInit.cpp
index 4400bc4436882..b5716e72d678d 100644
--- a/clang/lib/CodeGen/PatternInit.cpp
+++ b/clang/lib/CodeGen/PatternInit.cpp
@@ -37,7 +37,8 @@ llvm::Constant *clang::CodeGen::initializationPatternFor(CodeGenModule &CGM,
unsigned BitWidth =
cast<llvm::IntegerType>(Ty->getScalarType())->getBitWidth();
if (BitWidth <= 64)
- return llvm::ConstantInt::get(Ty, IntValue);
+ return llvm::ConstantInt::get(Ty, IntValue, /*IsSigned=*/false,
+ /*ImplicitTrunc=*/true);
return llvm::ConstantInt::get(
Ty, llvm::APInt::getSplat(BitWidth, llvm::APInt(64, IntValue)));
}
@@ -48,7 +49,8 @@ llvm::Constant *clang::CodeGen::initializationPatternFor(CodeGenModule &CGM,
if (PtrWidth > 64)
llvm_unreachable("pattern initialization of unsupported pointer width");
llvm::Type *IntTy = llvm::IntegerType::get(CGM.getLLVMContext(), PtrWidth);
- auto *Int = llvm::ConstantInt::get(IntTy, IntValue);
+ auto *Int = llvm::ConstantInt::get(IntTy, IntValue, /*IsSigned=*/false,
+ /*ImplicitTrunc=*/true);
return llvm::ConstantExpr::getIntToPtr(Int, PtrTy);
}
if (Ty->isFPOrFPVectorTy()) {
>From 82ee96c73fcf1dd9d6a4091586862cc3f92fe3d9 Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Mon, 15 Dec 2025 14:30:16 +0100
Subject: [PATCH 18/26] ms abi UNRELATED
---
clang/lib/CodeGen/MicrosoftCXXABI.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/lib/CodeGen/MicrosoftCXXABI.cpp b/clang/lib/CodeGen/MicrosoftCXXABI.cpp
index 484890062efa8..7c0f40f861b2d 100644
--- a/clang/lib/CodeGen/MicrosoftCXXABI.cpp
+++ b/clang/lib/CodeGen/MicrosoftCXXABI.cpp
@@ -2641,7 +2641,7 @@ struct ResetGuardBit final : EHScopeStack::Cleanup {
CGBuilderTy &Builder = CGF.Builder;
llvm::LoadInst *LI = Builder.CreateLoad(Guard);
llvm::ConstantInt *Mask =
- llvm::ConstantInt::get(CGF.IntTy, ~(1ULL << GuardNum));
+ llvm::ConstantInt::getSigned(CGF.IntTy, ~(1ULL << GuardNum));
Builder.CreateStore(Builder.CreateAnd(LI, Mask), Guard);
}
};
>From 6917ffda207f7bcb97d0c870d7329903f01079d0 Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Mon, 15 Dec 2025 14:32:05 +0100
Subject: [PATCH 19/26] [CGExpr] Use getSigned() for negative constant
---
clang/lib/CodeGen/CGExpr.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp
index e511c66835dd0..3f2789871cffc 100644
--- a/clang/lib/CodeGen/CGExpr.cpp
+++ b/clang/lib/CodeGen/CGExpr.cpp
@@ -6588,7 +6588,7 @@ RValue CodeGenFunction::EmitCall(QualType CalleeType,
if (CGM.getTriple().isARM() || CGM.getTriple().isThumb()) {
llvm::Value *CalleeAddress =
Builder.CreatePtrToInt(CalleePtr, IntPtrTy);
- llvm::Value *Mask = llvm::ConstantInt::get(IntPtrTy, ~1);
+ llvm::Value *Mask = llvm::ConstantInt::getSigned(IntPtrTy, ~1);
llvm::Value *AlignedCalleeAddress =
Builder.CreateAnd(CalleeAddress, Mask);
AlignedCalleePtr =
>From 679b8cbc98d8c27d65ae1bd5f1e08a52f50bf9cc Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Mon, 15 Dec 2025 14:40:36 +0100
Subject: [PATCH 20/26] [CGObjCGNU] Set isSigned for negative value
---
clang/lib/CodeGen/CGObjCGNU.cpp | 10 ++++++----
1 file changed, 6 insertions(+), 4 deletions(-)
diff --git a/clang/lib/CodeGen/CGObjCGNU.cpp b/clang/lib/CodeGen/CGObjCGNU.cpp
index 06643d4bdc211..457e64429e370 100644
--- a/clang/lib/CodeGen/CGObjCGNU.cpp
+++ b/clang/lib/CodeGen/CGObjCGNU.cpp
@@ -1826,10 +1826,12 @@ class CGObjCGNUstep2 : public CGObjCGNUstep {
// Instance size is negative for classes that have not yet had their ivar
// layout calculated.
classFields.addInt(
- LongTy, 0 - (Context.getASTObjCInterfaceLayout(OID->getClassInterface())
- .getSize()
- .getQuantity() -
- superInstanceSize));
+ LongTy,
+ 0 - (Context.getASTObjCInterfaceLayout(OID->getClassInterface())
+ .getSize()
+ .getQuantity() -
+ superInstanceSize),
+ /*isSigned=*/true);
if (classDecl->all_declared_ivar_begin() == nullptr)
classFields.addNullPointer(PtrTy);
>From 681a20584bb9ca99861a88a4436156b22ac0a0c2 Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Mon, 15 Dec 2025 14:44:25 +0100
Subject: [PATCH 21/26] [CGObjCGNU] Use getSigned() for instanceSize
For non-fragile this is a negative value.
---
clang/lib/CodeGen/CGObjCGNU.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/lib/CodeGen/CGObjCGNU.cpp b/clang/lib/CodeGen/CGObjCGNU.cpp
index 457e64429e370..a81c4217495c0 100644
--- a/clang/lib/CodeGen/CGObjCGNU.cpp
+++ b/clang/lib/CodeGen/CGObjCGNU.cpp
@@ -3885,7 +3885,7 @@ void CGObjCGNU::GenerateClass(const ObjCImplementationDecl *OID) {
// Generate the class structure
llvm::Constant *ClassStruct = GenerateClassStructure(
MetaClassStruct, SuperClass, 0x11L, ClassName.c_str(), nullptr,
- llvm::ConstantInt::get(LongTy, instanceSize), IvarList, MethodList,
+ llvm::ConstantInt::getSigned(LongTy, instanceSize), IvarList, MethodList,
GenerateProtocolList(Protocols), IvarOffsetArray, Properties,
StrongIvarBitmap, WeakIvarBitmap);
CGM.setGVProperties(cast<llvm::GlobalValue>(ClassStruct),
>From 6c32f22eb5cdc9d4a1facb9f6bf9224677aeae6f Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Mon, 15 Dec 2025 14:46:24 +0100
Subject: [PATCH 22/26] [AArch64] Use getSigned() for negative value
---
clang/lib/CodeGen/Targets/AArch64.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/lib/CodeGen/Targets/AArch64.cpp b/clang/lib/CodeGen/Targets/AArch64.cpp
index 77b947e81f5fc..963b74927036a 100644
--- a/clang/lib/CodeGen/Targets/AArch64.cpp
+++ b/clang/lib/CodeGen/Targets/AArch64.cpp
@@ -971,7 +971,7 @@ RValue AArch64ABIInfo::EmitAAPCSVAArg(Address VAListAddr, QualType Ty,
reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, Align - 1),
"align_regoffs");
reg_offs = CGF.Builder.CreateAnd(
- reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, -Align),
+ reg_offs, llvm::ConstantInt::getSigned(CGF.Int32Ty, -Align),
"aligned_regoffs");
}
>From ea1d60a93256b546b53bff07d2a4523774c9bf6b Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Mon, 15 Dec 2025 14:47:45 +0100
Subject: [PATCH 23/26] [CGHLSLRuntime] Use getSigned() for total array size
This may be -1 for incompete array types.
---
clang/lib/CodeGen/CGHLSLRuntime.cpp | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/clang/lib/CodeGen/CGHLSLRuntime.cpp b/clang/lib/CodeGen/CGHLSLRuntime.cpp
index f485fdd49e43f..2e9602d1b3793 100644
--- a/clang/lib/CodeGen/CGHLSLRuntime.cpp
+++ b/clang/lib/CodeGen/CGHLSLRuntime.cpp
@@ -1279,8 +1279,8 @@ std::optional<LValue> CGHLSLRuntime::emitResourceArraySubscriptExpr(
AggValueSlot::DoesNotOverlap);
// Calculate total array size (= range size).
- llvm::Value *Range =
- llvm::ConstantInt::get(CGM.IntTy, getTotalArraySize(AST, ResArrayTy));
+ llvm::Value *Range = llvm::ConstantInt::getSigned(
+ CGM.IntTy, getTotalArraySize(AST, ResArrayTy));
// If the result of the subscript operation is a single resource, call the
// constructor.
>From 14643dd85efad8be696e3efa7c3919a4cc48b23d Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Mon, 15 Dec 2025 14:51:01 +0100
Subject: [PATCH 24/26] [ARM] Use getSigned() for signed value
---
clang/lib/CodeGen/TargetBuiltins/ARM.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/lib/CodeGen/TargetBuiltins/ARM.cpp b/clang/lib/CodeGen/TargetBuiltins/ARM.cpp
index 7a8ea7ce0b563..05dff01a71b9a 100644
--- a/clang/lib/CodeGen/TargetBuiltins/ARM.cpp
+++ b/clang/lib/CodeGen/TargetBuiltins/ARM.cpp
@@ -489,7 +489,7 @@ llvm::Value *CodeGenFunction::EmitFP8NeonFMLACall(
Value *CodeGenFunction::EmitNeonShiftVector(Value *V, llvm::Type *Ty,
bool neg) {
int SV = cast<ConstantInt>(V)->getSExtValue();
- return ConstantInt::get(Ty, neg ? -SV : SV);
+ return ConstantInt::getSigned(Ty, neg ? -SV : SV);
}
Value *CodeGenFunction::EmitFP8NeonCvtCall(unsigned IID, llvm::Type *Ty0,
>From 9e2cacea71e412b42104b9698007d8eb1f8404d1 Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Mon, 15 Dec 2025 14:52:19 +0100
Subject: [PATCH 25/26] [CGExprScalar] Use getSigned() for signed value
---
clang/lib/CodeGen/CGExprScalar.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp
index 9b6497fca829a..96274cf7f8220 100644
--- a/clang/lib/CodeGen/CGExprScalar.cpp
+++ b/clang/lib/CodeGen/CGExprScalar.cpp
@@ -3435,7 +3435,7 @@ ScalarExprEmitter::EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV,
CharUnits size = CGF.getContext().getTypeSizeInChars(OPT->getObjectType());
if (!isInc) size = -size;
llvm::Value *sizeValue =
- llvm::ConstantInt::get(CGF.SizeTy, size.getQuantity());
+ llvm::ConstantInt::getSigned(CGF.SizeTy, size.getQuantity());
if (CGF.getLangOpts().PointerOverflowDefined)
value = Builder.CreateGEP(CGF.Int8Ty, value, sizeValue, "incdec.objptr");
>From 89747f0cb571373195e4420eaf57419e2807f4d8 Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Tue, 16 Dec 2025 11:55:48 +0100
Subject: [PATCH 26/26] [CGExprScalar] Allow implicit truncation for
CharacterLiteral
The value is always stored as an unsigned number, even if the
char type is signed, so we have to allow truncation here.
---
clang/lib/CodeGen/CGExprScalar.cpp | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp
index 96274cf7f8220..b4daaef95bb8d 100644
--- a/clang/lib/CodeGen/CGExprScalar.cpp
+++ b/clang/lib/CodeGen/CGExprScalar.cpp
@@ -510,7 +510,10 @@ class ScalarExprEmitter
return llvm::ConstantFP::get(VMContext, E->getValue());
}
Value *VisitCharacterLiteral(const CharacterLiteral *E) {
- return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue());
+ // Character literals are always stored in an unsigned (even for signed
+ // char), so allow implicit truncation here.
+ return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue(),
+ /*IsSigned=*/false, /*ImplicitTrunc=*/true);
}
Value *VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *E) {
return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue());
More information about the cfe-commits
mailing list