Skip to content

Commit 280f38e

Browse files
nikicakiramenai
authored andcommitted
Assert correct values in APInt constructor
If the uint64_t constructor is used, assert that the value is actuall a signed or unsigned N-bit integer depending on whether the isSigned flag is set. Currently, we allow values to be silently truncated, which is a constant source of subtle bugs -- a particularly common mistake is to create -1 values without setting the isSigned flag, which will work fine for all common bit widths (<= 64-bit) and miscompile for larger integers.
1 parent f73463c commit 280f38e

16 files changed

+49
-41
lines changed

llvm/lib/Analysis/ConstantFolding.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3600,8 +3600,9 @@ ConstantFoldScalarFrexpCall(Constant *Op, Type *IntTy) {
36003600

36013601
// The exponent is an "unspecified value" for inf/nan. We use zero to avoid
36023602
// using undef.
3603-
Constant *Result1 = FrexpMant.isFinite() ? ConstantInt::get(IntTy, FrexpExp)
3604-
: ConstantInt::getNullValue(IntTy);
3603+
Constant *Result1 = FrexpMant.isFinite()
3604+
? ConstantInt::getSigned(IntTy, FrexpExp)
3605+
: ConstantInt::getNullValue(IntTy);
36053606
return {Result0, Result1};
36063607
}
36073608

llvm/lib/Analysis/MemoryBuiltins.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,7 @@ Value *llvm::lowerObjectSizeCall(
670670
if (!MustSucceed)
671671
return nullptr;
672672

673-
return ConstantInt::get(ResultType, MaxVal ? -1ULL : 0);
673+
return ConstantInt::get(ResultType, MaxVal ? -1ULL : 0, true);
674674
}
675675

676676
STATISTIC(ObjectVisitorArgument,

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1455,7 +1455,7 @@ bool ScalarEvolution::proveNoWrapByVaryingStart(const SCEV *Start,
14551455

14561456
APInt StartAI = StartC->getAPInt();
14571457

1458-
for (unsigned Delta : {-2, -1, 1, 2}) {
1458+
for (int Delta : {-2, -1, 1, 2}) {
14591459
const SCEV *PreStart = getConstant(StartAI - Delta);
14601460

14611461
FoldingSetNodeID ID;
@@ -1470,7 +1470,7 @@ bool ScalarEvolution::proveNoWrapByVaryingStart(const SCEV *Start,
14701470
// Give up if we don't already have the add recurrence we need because
14711471
// actually constructing an add recurrence is relatively expensive.
14721472
if (PreAR && PreAR->getNoWrapFlags(WrapType)) { // proves (2)
1473-
const SCEV *DeltaS = getConstant(StartC->getType(), Delta);
1473+
const SCEV *DeltaS = getConstant(StartC->getType(), Delta, true);
14741474
ICmpInst::Predicate Pred = ICmpInst::BAD_ICMP_PREDICATE;
14751475
const SCEV *Limit = ExtendOpTraits<ExtendOpTy>::getOverflowLimitForStep(
14761476
DeltaS, &Pred, this);

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8719,7 +8719,7 @@ static void setLimitForFPToI(const Instruction *I, APInt &Lower, APInt &Upper) {
87198719
if (!I->getOperand(0)->getType()->getScalarType()->isHalfTy())
87208720
return;
87218721
if (isa<FPToSIInst>(I) && BitWidth >= 17) {
8722-
Lower = APInt(BitWidth, -65504);
8722+
Lower = APInt(BitWidth, -65504, true);
87238723
Upper = APInt(BitWidth, 65505);
87248724
}
87258725

llvm/lib/Bitcode/Reader/BitcodeReader.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3023,7 +3023,7 @@ Error BitcodeReader::parseConstants() {
30233023
case bitc::CST_CODE_INTEGER: // INTEGER: [intval]
30243024
if (!CurTy->isIntegerTy() || Record.empty())
30253025
return error("Invalid integer const record");
3026-
V = ConstantInt::get(CurTy, decodeSignRotatedValue(Record[0]));
3026+
V = ConstantInt::getSigned(CurTy, decodeSignRotatedValue(Record[0]));
30273027
break;
30283028
case bitc::CST_CODE_WIDE_INTEGER: {// WIDE_INTEGER: [n x intval]
30293029
if (!CurTy->isIntegerTy() || Record.empty())

llvm/lib/CodeGen/CodeGenPrepare.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1584,7 +1584,7 @@ static bool matchUAddWithOverflowConstantEdgeCases(CmpInst *Cmp,
15841584
if (Pred == ICmpInst::ICMP_EQ && match(B, m_AllOnes()))
15851585
B = ConstantInt::get(B->getType(), 1);
15861586
else if (Pred == ICmpInst::ICMP_NE && match(B, m_ZeroInt()))
1587-
B = ConstantInt::get(B->getType(), -1);
1587+
B = Constant::getAllOnesValue(B->getType());
15881588
else
15891589
return false;
15901590

llvm/lib/CodeGen/ExpandMemCmp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ void MemCmpExpansion::emitMemCmpResultBlock() {
537537
ResBlock.PhiSrc2);
538538

539539
Value *Res =
540-
Builder.CreateSelect(Cmp, ConstantInt::get(Builder.getInt32Ty(), -1),
540+
Builder.CreateSelect(Cmp, Constant::getAllOnesValue(Builder.getInt32Ty()),
541541
ConstantInt::get(Builder.getInt32Ty(), 1));
542542

543543
PhiRes->addIncoming(Res, ResBlock.BB);

llvm/lib/Transforms/IPO/WholeProgramDevirt.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1615,17 +1615,20 @@ Constant *DevirtModule::importConstant(VTableSlot Slot, ArrayRef<uint64_t> Args,
16151615
if (GV->hasMetadata(LLVMContext::MD_absolute_symbol))
16161616
return C;
16171617

1618-
auto SetAbsRange = [&](uint64_t Min, uint64_t Max) {
1618+
auto SetAbsRange = [&](const APInt &Min, const APInt &Max) {
16191619
auto *MinC = ConstantAsMetadata::get(ConstantInt::get(IntPtrTy, Min));
16201620
auto *MaxC = ConstantAsMetadata::get(ConstantInt::get(IntPtrTy, Max));
16211621
GV->setMetadata(LLVMContext::MD_absolute_symbol,
16221622
MDNode::get(M.getContext(), {MinC, MaxC}));
16231623
};
16241624
unsigned AbsWidth = IntTy->getBitWidth();
1625-
if (AbsWidth == IntPtrTy->getBitWidth())
1626-
SetAbsRange(~0ull, ~0ull); // Full set.
1625+
unsigned IntPtrWidth = IntPtrTy->getBitWidth();
1626+
if (AbsWidth == IntPtrWidth)
1627+
// Full set.
1628+
SetAbsRange(APInt::getAllOnes(IntPtrWidth), APInt::getAllOnes(IntPtrWidth));
16271629
else
1628-
SetAbsRange(0, 1ull << AbsWidth);
1630+
SetAbsRange(APInt::getZero(IntPtrWidth),
1631+
APInt::getOneBitSet(IntPtrWidth, AbsWidth));
16291632
return C;
16301633
}
16311634

@@ -1832,7 +1835,7 @@ bool DevirtModule::tryVirtualConstProp(
18321835
}
18331836

18341837
// Rewrite each call to a load from OffsetByte/OffsetBit.
1835-
Constant *ByteConst = ConstantInt::get(Int32Ty, OffsetByte);
1838+
Constant *ByteConst = ConstantInt::get(Int32Ty, OffsetByte, true);
18361839
Constant *BitConst = ConstantInt::get(Int8Ty, 1ULL << OffsetBit);
18371840
applyVirtualConstProp(CSByConstantArg.second,
18381841
TargetsForSlot[0].Fn->getName(), ByteConst, BitConst);

llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,8 +306,8 @@ Instruction *InstCombinerImpl::SimplifyAnyMemSet(AnyMemSetInst *MI) {
306306
Dest = Builder.CreateBitCast(Dest, NewDstPtrTy);
307307

308308
// Extract the fill value and store.
309-
const uint64_t Fill = FillC->getZExtValue()*0x0101010101010101ULL;
310-
Constant *FillVal = ConstantInt::get(ITy, Fill);
309+
Constant *FillVal = ConstantInt::get(
310+
MI->getContext(), APInt::getSplat(Len * 8, FillC->getValue()));
311311
StoreInst *S = Builder.CreateStore(FillVal, Dest, MI->isVolatile());
312312
S->copyMetadata(*MI, LLVMContext::MD_DIAssignID);
313313
for (auto *DAI : at::getAssignmentMarkers(S)) {

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ Instruction *InstCombinerImpl::foldCmpLoadFromIndexedGlobal(
305305
DL.getTypeAllocSize(Init->getType()->getArrayElementType());
306306
auto MaskIdx = [&](Value *Idx) {
307307
if (!GEP->isInBounds() && llvm::countr_zero(ElementSize) != 0) {
308-
Value *Mask = ConstantInt::get(Idx->getType(), -1);
308+
Value *Mask = Constant::getAllOnesValue(Idx->getType());
309309
Mask = Builder.CreateLShr(Mask, llvm::countr_zero(ElementSize));
310310
Idx = Builder.CreateAnd(Idx, Mask);
311311
}

0 commit comments

Comments
 (0)