Skip to content

Commit f5cc0bf

Browse files
committed
[APInt] 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 7f09aa9 commit f5cc0bf

37 files changed

+388
-348
lines changed

llvm/include/llvm/ADT/APFixedPoint.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,9 @@ class APFixedPoint {
160160
}
161161

162162
APFixedPoint(uint64_t Val, const FixedPointSemantics &Sema)
163-
: APFixedPoint(APInt(Sema.getWidth(), Val, Sema.isSigned()), Sema) {}
163+
: APFixedPoint(APInt(Sema.getWidth(), Val, Sema.isSigned(),
164+
/*implicitTrunc*/ true),
165+
Sema) {}
164166

165167
// Zero initialization.
166168
APFixedPoint(const FixedPointSemantics &Sema) : APFixedPoint(0, Sema) {}

llvm/include/llvm/ADT/APInt.h

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,26 @@ class [[nodiscard]] APInt {
107107
/// \param numBits the bit width of the constructed APInt
108108
/// \param val the initial value of the APInt
109109
/// \param isSigned how to treat signedness of val
110-
APInt(unsigned numBits, uint64_t val, bool isSigned = false)
110+
/// \param implicitTrunc allow implicit truncation of non-zero/sign bits of
111+
/// val beyond the range of numBits
112+
APInt(unsigned numBits, uint64_t val, bool isSigned = false,
113+
bool implicitTrunc = false)
111114
: BitWidth(numBits) {
115+
if (!implicitTrunc) {
116+
if (BitWidth == 0) {
117+
assert(val == 0 && "Value must be zero for 0-bit APInt");
118+
} else if (isSigned) {
119+
assert(llvm::isIntN(BitWidth, val) &&
120+
"Value is not an N-bit signed value");
121+
} else {
122+
assert(llvm::isUIntN(BitWidth, val) &&
123+
"Value is not an N-bit unsigned value");
124+
}
125+
}
112126
if (isSingleWord()) {
113127
U.VAL = val;
114-
clearUnusedBits();
128+
if (implicitTrunc || isSigned)
129+
clearUnusedBits();
115130
} else {
116131
initSlowCase(val, isSigned);
117132
}

llvm/lib/Analysis/ConstantFolding.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -889,7 +889,8 @@ Constant *SymbolicallyEvaluateGEP(const GEPOperator *GEP,
889889
APInt Offset = APInt(
890890
BitWidth,
891891
DL.getIndexedOffsetInType(
892-
SrcElemTy, ArrayRef((Value *const *)Ops.data() + 1, Ops.size() - 1)));
892+
SrcElemTy, ArrayRef((Value *const *)Ops.data() + 1, Ops.size() - 1)),
893+
/*isSigned*/ true, /*implicitTrunc*/ true);
893894

894895
std::optional<ConstantRange> InRange = GEP->getInRange();
895896
if (InRange)
@@ -3401,8 +3402,9 @@ ConstantFoldScalarFrexpCall(Constant *Op, Type *IntTy) {
34013402

34023403
// The exponent is an "unspecified value" for inf/nan. We use zero to avoid
34033404
// using undef.
3404-
Constant *Result1 = FrexpMant.isFinite() ? ConstantInt::get(IntTy, FrexpExp)
3405-
: ConstantInt::getNullValue(IntTy);
3405+
Constant *Result1 = FrexpMant.isFinite()
3406+
? ConstantInt::getSigned(IntTy, FrexpExp)
3407+
: ConstantInt::getNullValue(IntTy);
34063408
return {Result0, Result1};
34073409
}
34083410

llvm/lib/Analysis/MemoryBuiltins.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,7 @@ Value *llvm::lowerObjectSizeCall(
675675
if (!MustSucceed)
676676
return nullptr;
677677

678-
return ConstantInt::get(ResultType, MaxVal ? -1ULL : 0);
678+
return ConstantInt::get(ResultType, MaxVal ? -1ULL : 0, true);
679679
}
680680

681681
STATISTIC(ObjectVisitorArgument,

llvm/lib/Analysis/ScalarEvolution.cpp

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

14611461
APInt StartAI = StartC->getAPInt();
14621462

1463-
for (unsigned Delta : {-2, -1, 1, 2}) {
1463+
for (int Delta : {-2, -1, 1, 2}) {
14641464
const SCEV *PreStart = getConstant(StartAI - Delta);
14651465

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

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9303,7 +9303,7 @@ static ConstantRange getRangeForIntrinsic(const IntrinsicInst &II) {
93039303
case Intrinsic::cttz:
93049304
// Maximum of set/clear bits is the bit width.
93059305
return ConstantRange::getNonEmpty(APInt::getZero(Width),
9306-
APInt(Width, Width + 1));
9306+
APInt(Width, Width) + 1);
93079307
case Intrinsic::uadd_sat:
93089308
// uadd.sat(x, C) produces [C, UINT_MAX].
93099309
if (match(II.getOperand(0), m_APInt(C)) ||
@@ -9454,7 +9454,7 @@ static void setLimitForFPToI(const Instruction *I, APInt &Lower, APInt &Upper) {
94549454
if (!I->getOperand(0)->getType()->getScalarType()->isHalfTy())
94559455
return;
94569456
if (isa<FPToSIInst>(I) && BitWidth >= 17) {
9457-
Lower = APInt(BitWidth, -65504);
9457+
Lower = APInt(BitWidth, -65504, true);
94589458
Upper = APInt(BitWidth, 65505);
94599459
}
94609460

llvm/lib/Bitcode/Reader/BitcodeReader.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3147,7 +3147,7 @@ Error BitcodeReader::parseConstants() {
31473147
case bitc::CST_CODE_INTEGER: // INTEGER: [intval]
31483148
if (!CurTy->isIntOrIntVectorTy() || Record.empty())
31493149
return error("Invalid integer const record");
3150-
V = ConstantInt::get(CurTy, decodeSignRotatedValue(Record[0]));
3150+
V = ConstantInt::getSigned(CurTy, decodeSignRotatedValue(Record[0]));
31513151
break;
31523152
case bitc::CST_CODE_WIDE_INTEGER: {// WIDE_INTEGER: [n x intval]
31533153
if (!CurTy->isIntOrIntVectorTy() || Record.empty())

llvm/lib/CodeGen/CodeGenPrepare.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1643,7 +1643,7 @@ static bool matchUAddWithOverflowConstantEdgeCases(CmpInst *Cmp,
16431643
if (Pred == ICmpInst::ICMP_EQ && match(B, m_AllOnes()))
16441644
B = ConstantInt::get(B->getType(), 1);
16451645
else if (Pred == ICmpInst::ICMP_NE && match(B, m_ZeroInt()))
1646-
B = ConstantInt::get(B->getType(), -1);
1646+
B = Constant::getAllOnesValue(B->getType());
16471647
else
16481648
return false;
16491649

llvm/lib/CodeGen/ExpandMemCmp.cpp

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

592592
Value *Res =
593-
Builder.CreateSelect(Cmp, ConstantInt::get(Builder.getInt32Ty(), -1),
593+
Builder.CreateSelect(Cmp, Constant::getAllOnesValue(Builder.getInt32Ty()),
594594
ConstantInt::get(Builder.getInt32Ty(), 1));
595595

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

llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1625,7 +1625,10 @@ SDValue SelectionDAG::getConstant(uint64_t Val, const SDLoc &DL, EVT VT,
16251625
assert((EltVT.getSizeInBits() >= 64 ||
16261626
(uint64_t)((int64_t)Val >> EltVT.getSizeInBits()) + 1 < 2) &&
16271627
"getConstant with a uint64_t value that doesn't fit in the type!");
1628-
return getConstant(APInt(EltVT.getSizeInBits(), Val), DL, VT, isT, isO);
1628+
// TODO: Avoid implicit trunc?
1629+
return getConstant(
1630+
APInt(EltVT.getSizeInBits(), Val, false, /*implicitTrunc*/ true), DL, VT,
1631+
isT, isO);
16291632
}
16301633

16311634
SDValue SelectionDAG::getConstant(const APInt &Val, const SDLoc &DL, EVT VT,

llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2171,7 +2171,9 @@ ScheduleDAGSDNodes *SelectionDAGISel::CreateScheduler() {
21712171
bool SelectionDAGISel::CheckAndMask(SDValue LHS, ConstantSDNode *RHS,
21722172
int64_t DesiredMaskS) const {
21732173
const APInt &ActualMask = RHS->getAPIntValue();
2174-
const APInt &DesiredMask = APInt(LHS.getValueSizeInBits(), DesiredMaskS);
2174+
// TODO: Avoid implicit trunc?
2175+
const APInt &DesiredMask = APInt(LHS.getValueSizeInBits(), DesiredMaskS,
2176+
false, /*implicitTrunc*/ true);
21752177

21762178
// If the actual mask exactly matches, success!
21772179
if (ActualMask == DesiredMask)

llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4656,7 +4656,7 @@ OpenMPIRBuilder::createTargetInit(const LocationDescription &Loc, bool IsSPMD,
46564656
Builder.CreateCall(Fn, {KernelEnvironment, KernelLaunchEnvironment});
46574657

46584658
Value *ExecUserCode = Builder.CreateICmpEQ(
4659-
ThreadKind, ConstantInt::get(ThreadKind->getType(), -1),
4659+
ThreadKind, Constant::getAllOnesValue(ThreadKind->getType()),
46604660
"exec_user_code");
46614661

46624662
// ThreadKind = __kmpc_target_init(...)

llvm/lib/FuzzMutate/OpDescriptor.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ void fuzzerop::makeConstantsWithType(Type *T, std::vector<Constant *> &Cs) {
2222
uint64_t W = IntTy->getBitWidth();
2323
Cs.push_back(ConstantInt::get(IntTy, 0));
2424
Cs.push_back(ConstantInt::get(IntTy, 1));
25-
Cs.push_back(ConstantInt::get(IntTy, 42));
25+
Cs.push_back(ConstantInt::get(
26+
IntTy, APInt(W, 42, /*isSigned*/ false, /*implicitTrunc*/ true)));
2627
Cs.push_back(ConstantInt::get(IntTy, APInt::getMaxValue(W)));
2728
Cs.push_back(ConstantInt::get(IntTy, APInt::getMinValue(W)));
2829
Cs.push_back(ConstantInt::get(IntTy, APInt::getSignedMaxValue(W)));

llvm/lib/IR/ConstantRange.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1797,7 +1797,7 @@ ConstantRange ConstantRange::ctlz(bool ZeroIsPoison) const {
17971797
// Zero is either safe or not in the range. The output range is composed by
17981798
// the result of countLeadingZero of the two extremes.
17991799
return getNonEmpty(APInt(getBitWidth(), getUnsignedMax().countl_zero()),
1800-
APInt(getBitWidth(), getUnsignedMin().countl_zero() + 1));
1800+
APInt(getBitWidth(), getUnsignedMin().countl_zero()) + 1);
18011801
}
18021802

18031803
static ConstantRange getUnsignedCountTrailingZerosRange(const APInt &Lower,
@@ -1856,7 +1856,7 @@ ConstantRange ConstantRange::cttz(bool ZeroIsPoison) const {
18561856
}
18571857

18581858
if (isFullSet())
1859-
return getNonEmpty(Zero, APInt(BitWidth, BitWidth + 1));
1859+
return getNonEmpty(Zero, APInt(BitWidth, BitWidth) + 1);
18601860
if (!isWrappedSet())
18611861
return getUnsignedCountTrailingZerosRange(Lower, Upper);
18621862
// The range is wrapped. We decompose it into two ranges, [0, Upper) and
@@ -1901,7 +1901,7 @@ ConstantRange ConstantRange::ctpop() const {
19011901
unsigned BitWidth = getBitWidth();
19021902
APInt Zero = APInt::getZero(BitWidth);
19031903
if (isFullSet())
1904-
return getNonEmpty(Zero, APInt(BitWidth, BitWidth + 1));
1904+
return getNonEmpty(Zero, APInt(BitWidth, BitWidth) + 1);
19051905
if (!isWrappedSet())
19061906
return getUnsignedPopCountRange(Lower, Upper);
19071907
// The range is wrapped. We decompose it into two ranges, [0, Upper) and

llvm/lib/Support/APInt.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,8 @@ APInt& APInt::operator-=(uint64_t RHS) {
234234
APInt APInt::operator*(const APInt& RHS) const {
235235
assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
236236
if (isSingleWord())
237-
return APInt(BitWidth, U.VAL * RHS.U.VAL);
237+
return APInt(BitWidth, U.VAL * RHS.U.VAL, /*isSigned*/ false,
238+
/*implicitTrunc*/ true);
238239

239240
APInt Result(getMemory(getNumWords()), getBitWidth());
240241
tcMultiply(Result.U.pVal, U.pVal, RHS.U.pVal, getNumWords());
@@ -455,15 +456,17 @@ APInt APInt::extractBits(unsigned numBits, unsigned bitPosition) const {
455456
"Illegal bit extraction");
456457

457458
if (isSingleWord())
458-
return APInt(numBits, U.VAL >> bitPosition);
459+
return APInt(numBits, U.VAL >> bitPosition, /*isSigned*/ false,
460+
/*implicitTrunc*/ true);
459461

460462
unsigned loBit = whichBit(bitPosition);
461463
unsigned loWord = whichWord(bitPosition);
462464
unsigned hiWord = whichWord(bitPosition + numBits - 1);
463465

464466
// Single word result extracting bits from a single word source.
465467
if (loWord == hiWord)
466-
return APInt(numBits, U.pVal[loWord] >> loBit);
468+
return APInt(numBits, U.pVal[loWord] >> loBit, /*isSigned*/ false,
469+
/*implicitTrunc*/ true);
467470

468471
// Extracting bits that start on a source word boundary can be done
469472
// as a fast memory copy.
@@ -907,7 +910,8 @@ APInt APInt::trunc(unsigned width) const {
907910
assert(width <= BitWidth && "Invalid APInt Truncate request");
908911

909912
if (width <= APINT_BITS_PER_WORD)
910-
return APInt(width, getRawData()[0]);
913+
return APInt(width, getRawData()[0], /*isSigned*/ false,
914+
/*implicitTrunc*/ true);
911915

912916
if (width == BitWidth)
913917
return *this;
@@ -955,7 +959,7 @@ APInt APInt::sext(unsigned Width) const {
955959
assert(Width >= BitWidth && "Invalid APInt SignExtend request");
956960

957961
if (Width <= APINT_BITS_PER_WORD)
958-
return APInt(Width, SignExtend64(U.VAL, BitWidth));
962+
return APInt(Width, SignExtend64(U.VAL, BitWidth), /*isSigned*/ true);
959963

960964
if (Width == BitWidth)
961965
return *this;

llvm/lib/Transforms/IPO/WholeProgramDevirt.cpp

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

1672-
auto SetAbsRange = [&](uint64_t Min, uint64_t Max) {
1672+
auto SetAbsRange = [&](const APInt &Min, const APInt &Max) {
16731673
auto *MinC = ConstantAsMetadata::get(ConstantInt::get(IntPtrTy, Min));
16741674
auto *MaxC = ConstantAsMetadata::get(ConstantInt::get(IntPtrTy, Max));
16751675
GV->setMetadata(LLVMContext::MD_absolute_symbol,
16761676
MDNode::get(M.getContext(), {MinC, MaxC}));
16771677
};
16781678
unsigned AbsWidth = IntTy->getBitWidth();
1679-
if (AbsWidth == IntPtrTy->getBitWidth())
1680-
SetAbsRange(~0ull, ~0ull); // Full set.
1679+
unsigned IntPtrWidth = IntPtrTy->getBitWidth();
1680+
if (AbsWidth == IntPtrWidth)
1681+
// Full set.
1682+
SetAbsRange(APInt::getAllOnes(IntPtrWidth), APInt::getAllOnes(IntPtrWidth));
16811683
else
1682-
SetAbsRange(0, 1ull << AbsWidth);
1684+
SetAbsRange(APInt::getZero(IntPtrWidth),
1685+
APInt::getOneBitSet(IntPtrWidth, AbsWidth));
16831686
return C;
16841687
}
16851688

@@ -1884,7 +1887,7 @@ bool DevirtModule::tryVirtualConstProp(
18841887
}
18851888

18861889
// Rewrite each call to a load from OffsetByte/OffsetBit.
1887-
Constant *ByteConst = ConstantInt::get(Int32Ty, OffsetByte);
1890+
Constant *ByteConst = ConstantInt::get(Int32Ty, OffsetByte, true);
18881891
Constant *BitConst = ConstantInt::get(Int8Ty, 1ULL << OffsetBit);
18891892
applyVirtualConstProp(CSByConstantArg.second,
18901893
TargetsForSlot[0].Fn->getName(), ByteConst, BitConst);

llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,13 +260,11 @@ Instruction *InstCombinerImpl::SimplifyAnyMemSet(AnyMemSetInst *MI) {
260260

261261
// memset(s,c,n) -> store s, c (for n=1,2,4,8)
262262
if (Len <= 8 && isPowerOf2_32((uint32_t)Len)) {
263-
Type *ITy = IntegerType::get(MI->getContext(), Len*8); // n=1 -> i8.
264-
265263
Value *Dest = MI->getDest();
266264

267265
// Extract the fill value and store.
268-
const uint64_t Fill = FillC->getZExtValue()*0x0101010101010101ULL;
269-
Constant *FillVal = ConstantInt::get(ITy, Fill);
266+
Constant *FillVal = ConstantInt::get(
267+
MI->getContext(), APInt::getSplat(Len * 8, FillC->getValue()));
270268
StoreInst *S = Builder.CreateStore(FillVal, Dest, MI->isVolatile());
271269
S->copyMetadata(*MI, LLVMContext::MD_DIAssignID);
272270
auto replaceOpForAssignmentMarkers = [FillC, FillVal](auto *DbgAssign) {

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ Instruction *InstCombinerImpl::foldCmpLoadFromIndexedGlobal(
312312
DL.getTypeAllocSize(Init->getType()->getArrayElementType());
313313
auto MaskIdx = [&](Value *Idx) {
314314
if (!GEP->isInBounds() && llvm::countr_zero(ElementSize) != 0) {
315-
Value *Mask = ConstantInt::get(Idx->getType(), -1);
315+
Value *Mask = Constant::getAllOnesValue(Idx->getType());
316316
Mask = Builder.CreateLShr(Mask, llvm::countr_zero(ElementSize));
317317
Idx = Builder.CreateAnd(Idx, Mask);
318318
}

llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -675,11 +675,11 @@ static Value *foldSelectICmpLshrAshr(const ICmpInst *IC, Value *TrueVal,
675675
Value *X, *Y;
676676
unsigned Bitwidth = CmpRHS->getType()->getScalarSizeInBits();
677677
if ((Pred != ICmpInst::ICMP_SGT ||
678-
!match(CmpRHS,
679-
m_SpecificInt_ICMP(ICmpInst::ICMP_SGE, APInt(Bitwidth, -1)))) &&
678+
!match(CmpRHS, m_SpecificInt_ICMP(ICmpInst::ICMP_SGE,
679+
APInt::getAllOnes(Bitwidth)))) &&
680680
(Pred != ICmpInst::ICMP_SLT ||
681-
!match(CmpRHS,
682-
m_SpecificInt_ICMP(ICmpInst::ICMP_SGE, APInt(Bitwidth, 0)))))
681+
!match(CmpRHS, m_SpecificInt_ICMP(ICmpInst::ICMP_SGE,
682+
APInt::getZero(Bitwidth)))))
683683
return nullptr;
684684

685685
// Canonicalize so that ashr is in FalseVal.

llvm/lib/Transforms/Scalar/ConstraintElimination.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -874,7 +874,7 @@ void ConstraintInfo::transferToOtherSystem(
874874
addFact(CmpInst::ICMP_ULT, A, B, NumIn, NumOut, DFSInStack);
875875
break;
876876
case CmpInst::ICMP_SGT: {
877-
if (doesHold(CmpInst::ICMP_SGE, B, ConstantInt::get(B->getType(), -1)))
877+
if (doesHold(CmpInst::ICMP_SGE, B, Constant::getAllOnesValue(B->getType())))
878878
addFact(CmpInst::ICMP_UGE, A, ConstantInt::get(B->getType(), 0), NumIn,
879879
NumOut, DFSInStack);
880880
if (IsKnownNonNegative(B))

llvm/lib/Transforms/Scalar/IndVarSimplify.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -357,19 +357,19 @@ bool IndVarSimplify::handleFloatingPointIV(Loop *L, PHINode *PN) {
357357
// Insert new integer induction variable.
358358
PHINode *NewPHI =
359359
PHINode::Create(Int32Ty, 2, PN->getName() + ".int", PN->getIterator());
360-
NewPHI->addIncoming(ConstantInt::get(Int32Ty, InitValue),
360+
NewPHI->addIncoming(ConstantInt::getSigned(Int32Ty, InitValue),
361361
PN->getIncomingBlock(IncomingEdge));
362362
NewPHI->setDebugLoc(PN->getDebugLoc());
363363

364-
Instruction *NewAdd =
365-
BinaryOperator::CreateAdd(NewPHI, ConstantInt::get(Int32Ty, IncValue),
366-
Incr->getName() + ".int", Incr->getIterator());
364+
Instruction *NewAdd = BinaryOperator::CreateAdd(
365+
NewPHI, ConstantInt::getSigned(Int32Ty, IncValue),
366+
Incr->getName() + ".int", Incr->getIterator());
367367
NewAdd->setDebugLoc(Incr->getDebugLoc());
368368
NewPHI->addIncoming(NewAdd, PN->getIncomingBlock(BackEdge));
369369

370-
ICmpInst *NewCompare =
371-
new ICmpInst(TheBr->getIterator(), NewPred, NewAdd,
372-
ConstantInt::get(Int32Ty, ExitValue), Compare->getName());
370+
ICmpInst *NewCompare = new ICmpInst(
371+
TheBr->getIterator(), NewPred, NewAdd,
372+
ConstantInt::getSigned(Int32Ty, ExitValue), Compare->getName());
373373
NewCompare->setDebugLoc(Compare->getDebugLoc());
374374

375375
// In the following deletions, PN may become dead and may be deleted.

0 commit comments

Comments
 (0)