From 95f49c524d94eb8af2ed305a27531f5c67b07962 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Thu, 1 Feb 2024 17:27:46 +0100 Subject: [PATCH] [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. --- llvm/include/llvm/ADT/APFixedPoint.h | 4 +- llvm/include/llvm/ADT/APInt.h | 19 +- llvm/lib/Analysis/ConstantFolding.cpp | 8 +- llvm/lib/Analysis/ValueTracking.cpp | 4 +- llvm/lib/Bitcode/Reader/BitcodeReader.cpp | 2 +- llvm/lib/CodeGen/CodeGenPrepare.cpp | 2 +- llvm/lib/CodeGen/ExpandMemCmp.cpp | 2 +- .../lib/CodeGen/SelectionDAG/SelectionDAG.cpp | 5 +- .../CodeGen/SelectionDAG/SelectionDAGISel.cpp | 4 +- llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp | 2 +- llvm/lib/FuzzMutate/OpDescriptor.cpp | 3 +- llvm/lib/IR/ConstantRange.cpp | 6 +- llvm/lib/Support/APInt.cpp | 14 +- .../InstCombine/InstCombineCalls.cpp | 6 +- .../InstCombine/InstCombineCompares.cpp | 2 +- .../InstCombine/InstCombineSelect.cpp | 8 +- .../Scalar/ConstraintElimination.cpp | 2 +- .../Transforms/Scalar/LoopStrengthReduce.cpp | 11 +- llvm/lib/Transforms/Utils/SimplifyCFG.cpp | 4 +- .../lib/Transforms/Utils/SimplifyLibCalls.cpp | 10 +- .../lib/Transforms/Vectorize/VPlanRecipes.cpp | 2 +- llvm/unittests/ADT/APFixedPointTest.cpp | 9 +- llvm/unittests/ADT/APIntTest.cpp | 171 +++++++-------- llvm/unittests/ADT/APSIntTest.cpp | 8 +- llvm/unittests/ADT/StringExtrasTest.cpp | 10 +- .../Analysis/ScalarEvolutionTest.cpp | 2 +- .../FuzzMutate/RandomIRBuilderTest.cpp | 4 +- llvm/unittests/IR/ConstantRangeTest.cpp | 144 +++++++------ llvm/unittests/IR/ConstantsTest.cpp | 5 +- llvm/unittests/IR/InstructionsTest.cpp | 4 +- llvm/unittests/IR/MetadataTest.cpp | 22 +- llvm/unittests/IR/PatternMatch.cpp | 202 +++++++++--------- .../Support/DivisionByConstantTest.cpp | 2 +- 33 files changed, 370 insertions(+), 333 deletions(-) diff --git a/llvm/include/llvm/ADT/APFixedPoint.h b/llvm/include/llvm/ADT/APFixedPoint.h index b0c510865f444e..77aff33bc0774f 100644 --- a/llvm/include/llvm/ADT/APFixedPoint.h +++ b/llvm/include/llvm/ADT/APFixedPoint.h @@ -160,7 +160,9 @@ class APFixedPoint { } APFixedPoint(uint64_t Val, const FixedPointSemantics &Sema) - : APFixedPoint(APInt(Sema.getWidth(), Val, Sema.isSigned()), Sema) {} + : APFixedPoint(APInt(Sema.getWidth(), Val, Sema.isSigned(), + /*implicitTrunc*/ true), + Sema) {} // Zero initialization. APFixedPoint(const FixedPointSemantics &Sema) : APFixedPoint(0, Sema) {} diff --git a/llvm/include/llvm/ADT/APInt.h b/llvm/include/llvm/ADT/APInt.h index 6f2f25548cc84b..4b8a5f9ee4c991 100644 --- a/llvm/include/llvm/ADT/APInt.h +++ b/llvm/include/llvm/ADT/APInt.h @@ -106,11 +106,26 @@ class [[nodiscard]] APInt { /// \param numBits the bit width of the constructed APInt /// \param val the initial value of the APInt /// \param isSigned how to treat signedness of val - APInt(unsigned numBits, uint64_t val, bool isSigned = false) + /// \param implicitTrunc allow implicit truncation of non-zero/sign bits of + /// val beyond the range of numBits + APInt(unsigned numBits, uint64_t val, bool isSigned = false, + bool implicitTrunc = false) : BitWidth(numBits) { + if (!implicitTrunc) { + if (BitWidth == 0) { + assert(val == 0 && "Value must be zero for 0-bit APInt"); + } else if (isSigned) { + assert(llvm::isIntN(BitWidth, val) && + "Value is not an N-bit signed value"); + } else { + assert(llvm::isUIntN(BitWidth, val) && + "Value is not an N-bit unsigned value"); + } + } if (isSingleWord()) { U.VAL = val; - clearUnusedBits(); + if (implicitTrunc || isSigned) + clearUnusedBits(); } else { initSlowCase(val, isSigned); } diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp index 90da3390eab324..556edce32579d4 100644 --- a/llvm/lib/Analysis/ConstantFolding.cpp +++ b/llvm/lib/Analysis/ConstantFolding.cpp @@ -889,7 +889,8 @@ Constant *SymbolicallyEvaluateGEP(const GEPOperator *GEP, APInt Offset = APInt( BitWidth, DL.getIndexedOffsetInType( - SrcElemTy, ArrayRef((Value *const *)Ops.data() + 1, Ops.size() - 1))); + SrcElemTy, ArrayRef((Value *const *)Ops.data() + 1, Ops.size() - 1)), + /*isSigned*/ true, /*implicitTrunc*/ true); // If this is a GEP of a GEP, fold it all into a single GEP. while (auto *GEP = dyn_cast(Ptr)) { @@ -3322,8 +3323,9 @@ ConstantFoldScalarFrexpCall(Constant *Op, Type *IntTy) { // The exponent is an "unspecified value" for inf/nan. We use zero to avoid // using undef. - Constant *Result1 = FrexpMant.isFinite() ? ConstantInt::get(IntTy, FrexpExp) - : ConstantInt::getNullValue(IntTy); + Constant *Result1 = FrexpMant.isFinite() + ? ConstantInt::getSigned(IntTy, FrexpExp) + : ConstantInt::getNullValue(IntTy); return {Result0, Result1}; } diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp index f7388bbb4a47ac..9a2b57af495d79 100644 --- a/llvm/lib/Analysis/ValueTracking.cpp +++ b/llvm/lib/Analysis/ValueTracking.cpp @@ -8701,7 +8701,7 @@ static ConstantRange getRangeForIntrinsic(const IntrinsicInst &II) { case Intrinsic::cttz: // Maximum of set/clear bits is the bit width. return ConstantRange::getNonEmpty(APInt::getZero(Width), - APInt(Width, Width + 1)); + APInt(Width, Width) + 1); case Intrinsic::uadd_sat: // uadd.sat(x, C) produces [C, UINT_MAX]. if (match(II.getOperand(0), m_APInt(C)) || @@ -8852,7 +8852,7 @@ static void setLimitForFPToI(const Instruction *I, APInt &Lower, APInt &Upper) { if (!I->getOperand(0)->getType()->getScalarType()->isHalfTy()) return; if (isa(I) && BitWidth >= 17) { - Lower = APInt(BitWidth, -65504); + Lower = APInt(BitWidth, -65504, true); Upper = APInt(BitWidth, 65505); } diff --git a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp index 5b233fb365fe27..af9befa5b9dc3a 100644 --- a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp +++ b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp @@ -3062,7 +3062,7 @@ Error BitcodeReader::parseConstants() { case bitc::CST_CODE_INTEGER: // INTEGER: [intval] if (!CurTy->isIntegerTy() || Record.empty()) return error("Invalid integer const record"); - V = ConstantInt::get(CurTy, decodeSignRotatedValue(Record[0])); + V = ConstantInt::getSigned(CurTy, decodeSignRotatedValue(Record[0])); break; case bitc::CST_CODE_WIDE_INTEGER: {// WIDE_INTEGER: [n x intval] if (!CurTy->isIntegerTy() || Record.empty()) diff --git a/llvm/lib/CodeGen/CodeGenPrepare.cpp b/llvm/lib/CodeGen/CodeGenPrepare.cpp index 5383b15c1c7f5d..162d6edc0c364c 100644 --- a/llvm/lib/CodeGen/CodeGenPrepare.cpp +++ b/llvm/lib/CodeGen/CodeGenPrepare.cpp @@ -1645,7 +1645,7 @@ static bool matchUAddWithOverflowConstantEdgeCases(CmpInst *Cmp, if (Pred == ICmpInst::ICMP_EQ && match(B, m_AllOnes())) B = ConstantInt::get(B->getType(), 1); else if (Pred == ICmpInst::ICMP_NE && match(B, m_ZeroInt())) - B = ConstantInt::get(B->getType(), -1); + B = Constant::getAllOnesValue(B->getType()); else return false; diff --git a/llvm/lib/CodeGen/ExpandMemCmp.cpp b/llvm/lib/CodeGen/ExpandMemCmp.cpp index bb84813569f4d5..459200fd18d9dd 100644 --- a/llvm/lib/CodeGen/ExpandMemCmp.cpp +++ b/llvm/lib/CodeGen/ExpandMemCmp.cpp @@ -590,7 +590,7 @@ void MemCmpExpansion::emitMemCmpResultBlock() { ResBlock.PhiSrc2); Value *Res = - Builder.CreateSelect(Cmp, ConstantInt::get(Builder.getInt32Ty(), -1), + Builder.CreateSelect(Cmp, Constant::getAllOnesValue(Builder.getInt32Ty()), ConstantInt::get(Builder.getInt32Ty(), 1)); PhiRes->addIncoming(Res, ResBlock.BB); diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp index 3c1343836187a9..1bd839291acac5 100644 --- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp @@ -1603,7 +1603,10 @@ SDValue SelectionDAG::getConstant(uint64_t Val, const SDLoc &DL, EVT VT, assert((EltVT.getSizeInBits() >= 64 || (uint64_t)((int64_t)Val >> EltVT.getSizeInBits()) + 1 < 2) && "getConstant with a uint64_t value that doesn't fit in the type!"); - return getConstant(APInt(EltVT.getSizeInBits(), Val), DL, VT, isT, isO); + // TODO: Avoid implicit trunc? + return getConstant( + APInt(EltVT.getSizeInBits(), Val, false, /*implicitTrunc*/ true), DL, VT, + isT, isO); } SDValue SelectionDAG::getConstant(const APInt &Val, const SDLoc &DL, EVT VT, diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp index ad5a3302230c86..629254ca2ff233 100644 --- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp @@ -2054,7 +2054,9 @@ ScheduleDAGSDNodes *SelectionDAGISel::CreateScheduler() { bool SelectionDAGISel::CheckAndMask(SDValue LHS, ConstantSDNode *RHS, int64_t DesiredMaskS) const { const APInt &ActualMask = RHS->getAPIntValue(); - const APInt &DesiredMask = APInt(LHS.getValueSizeInBits(), DesiredMaskS); + // TODO: Avoid implicit trunc? + const APInt &DesiredMask = APInt(LHS.getValueSizeInBits(), DesiredMaskS, + false, /*implicitTrunc*/ true); // If the actual mask exactly matches, success! if (ActualMask == DesiredMask) diff --git a/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp b/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp index 8eb8a13686dd36..212392432d2b19 100644 --- a/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp +++ b/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp @@ -4542,7 +4542,7 @@ OpenMPIRBuilder::createTargetInit(const LocationDescription &Loc, bool IsSPMD, Builder.CreateCall(Fn, {KernelEnvironment, KernelLaunchEnvironment}); Value *ExecUserCode = Builder.CreateICmpEQ( - ThreadKind, ConstantInt::get(ThreadKind->getType(), -1), + ThreadKind, Constant::getAllOnesValue(ThreadKind->getType()), "exec_user_code"); // ThreadKind = __kmpc_target_init(...) diff --git a/llvm/lib/FuzzMutate/OpDescriptor.cpp b/llvm/lib/FuzzMutate/OpDescriptor.cpp index 4baf45284de105..247ac03713b29b 100644 --- a/llvm/lib/FuzzMutate/OpDescriptor.cpp +++ b/llvm/lib/FuzzMutate/OpDescriptor.cpp @@ -17,7 +17,8 @@ void fuzzerop::makeConstantsWithType(Type *T, std::vector &Cs) { uint64_t W = IntTy->getBitWidth(); Cs.push_back(ConstantInt::get(IntTy, 0)); Cs.push_back(ConstantInt::get(IntTy, 1)); - Cs.push_back(ConstantInt::get(IntTy, 42)); + Cs.push_back(ConstantInt::get( + IntTy, APInt(W, 42, /*isSigned*/ false, /*implicitTrunc*/ true))); Cs.push_back(ConstantInt::get(IntTy, APInt::getMaxValue(W))); Cs.push_back(ConstantInt::get(IntTy, APInt::getMinValue(W))); Cs.push_back(ConstantInt::get(IntTy, APInt::getSignedMaxValue(W))); diff --git a/llvm/lib/IR/ConstantRange.cpp b/llvm/lib/IR/ConstantRange.cpp index cbb64b299e648e..778921479794eb 100644 --- a/llvm/lib/IR/ConstantRange.cpp +++ b/llvm/lib/IR/ConstantRange.cpp @@ -1743,7 +1743,7 @@ ConstantRange ConstantRange::ctlz(bool ZeroIsPoison) const { // Zero is either safe or not in the range. The output range is composed by // the result of countLeadingZero of the two extremes. return getNonEmpty(APInt(getBitWidth(), getUnsignedMax().countl_zero()), - APInt(getBitWidth(), getUnsignedMin().countl_zero() + 1)); + APInt(getBitWidth(), getUnsignedMin().countl_zero()) + 1); } static ConstantRange getUnsignedCountTrailingZerosRange(const APInt &Lower, @@ -1802,7 +1802,7 @@ ConstantRange ConstantRange::cttz(bool ZeroIsPoison) const { } if (isFullSet()) - return getNonEmpty(Zero, APInt(BitWidth, BitWidth + 1)); + return getNonEmpty(Zero, APInt(BitWidth, BitWidth) + 1); if (!isWrappedSet()) return getUnsignedCountTrailingZerosRange(Lower, Upper); // The range is wrapped. We decompose it into two ranges, [0, Upper) and @@ -1847,7 +1847,7 @@ ConstantRange ConstantRange::ctpop() const { unsigned BitWidth = getBitWidth(); APInt Zero = APInt::getZero(BitWidth); if (isFullSet()) - return getNonEmpty(Zero, APInt(BitWidth, BitWidth + 1)); + return getNonEmpty(Zero, APInt(BitWidth, BitWidth) + 1); if (!isWrappedSet()) return getUnsignedPopCountRange(Lower, Upper); // The range is wrapped. We decompose it into two ranges, [0, Upper) and diff --git a/llvm/lib/Support/APInt.cpp b/llvm/lib/Support/APInt.cpp index 05b1526da95ff7..08b9ad6ce8e94b 100644 --- a/llvm/lib/Support/APInt.cpp +++ b/llvm/lib/Support/APInt.cpp @@ -234,7 +234,8 @@ APInt& APInt::operator-=(uint64_t RHS) { APInt APInt::operator*(const APInt& RHS) const { assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); if (isSingleWord()) - return APInt(BitWidth, U.VAL * RHS.U.VAL); + return APInt(BitWidth, U.VAL * RHS.U.VAL, /*isSigned*/ false, + /*implicitTrunc*/ true); APInt Result(getMemory(getNumWords()), getBitWidth()); tcMultiply(Result.U.pVal, U.pVal, RHS.U.pVal, getNumWords()); @@ -455,7 +456,8 @@ APInt APInt::extractBits(unsigned numBits, unsigned bitPosition) const { "Illegal bit extraction"); if (isSingleWord()) - return APInt(numBits, U.VAL >> bitPosition); + return APInt(numBits, U.VAL >> bitPosition, /*isSigned*/ false, + /*implicitTrunc*/ true); unsigned loBit = whichBit(bitPosition); unsigned loWord = whichWord(bitPosition); @@ -463,7 +465,8 @@ APInt APInt::extractBits(unsigned numBits, unsigned bitPosition) const { // Single word result extracting bits from a single word source. if (loWord == hiWord) - return APInt(numBits, U.pVal[loWord] >> loBit); + return APInt(numBits, U.pVal[loWord] >> loBit, /*isSigned*/ false, + /*implicitTrunc*/ true); // Extracting bits that start on a source word boundary can be done // as a fast memory copy. @@ -907,7 +910,8 @@ APInt APInt::trunc(unsigned width) const { assert(width <= BitWidth && "Invalid APInt Truncate request"); if (width <= APINT_BITS_PER_WORD) - return APInt(width, getRawData()[0]); + return APInt(width, getRawData()[0], /*isSigned*/ false, + /*implicitTrunc*/ true); if (width == BitWidth) return *this; @@ -955,7 +959,7 @@ APInt APInt::sext(unsigned Width) const { assert(Width >= BitWidth && "Invalid APInt SignExtend request"); if (Width <= APINT_BITS_PER_WORD) - return APInt(Width, SignExtend64(U.VAL, BitWidth)); + return APInt(Width, SignExtend64(U.VAL, BitWidth), /*isSigned*/ true); if (Width == BitWidth) return *this; diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp index b7e73856f40606..4eaaae5044c827 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp @@ -273,13 +273,11 @@ Instruction *InstCombinerImpl::SimplifyAnyMemSet(AnyMemSetInst *MI) { // memset(s,c,n) -> store s, c (for n=1,2,4,8) if (Len <= 8 && isPowerOf2_32((uint32_t)Len)) { - Type *ITy = IntegerType::get(MI->getContext(), Len*8); // n=1 -> i8. - Value *Dest = MI->getDest(); // Extract the fill value and store. - const uint64_t Fill = FillC->getZExtValue()*0x0101010101010101ULL; - Constant *FillVal = ConstantInt::get(ITy, Fill); + Constant *FillVal = ConstantInt::get( + MI->getContext(), APInt::getSplat(Len * 8, FillC->getValue())); StoreInst *S = Builder.CreateStore(FillVal, Dest, MI->isVolatile()); S->copyMetadata(*MI, LLVMContext::MD_DIAssignID); auto replaceOpForAssignmentMarkers = [FillC, FillVal](auto *DbgAssign) { diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp index d295853798b808..b3b425cc1ac42b 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -308,7 +308,7 @@ Instruction *InstCombinerImpl::foldCmpLoadFromIndexedGlobal( DL.getTypeAllocSize(Init->getType()->getArrayElementType()); auto MaskIdx = [&](Value *Idx) { if (!GEP->isInBounds() && llvm::countr_zero(ElementSize) != 0) { - Value *Mask = ConstantInt::get(Idx->getType(), -1); + Value *Mask = Constant::getAllOnesValue(Idx->getType()); Mask = Builder.CreateLShr(Mask, llvm::countr_zero(ElementSize)); Idx = Builder.CreateAnd(Idx, Mask); } diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp index 453e4d788705f3..efc3b99e836060 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp @@ -665,11 +665,11 @@ static Value *foldSelectICmpLshrAshr(const ICmpInst *IC, Value *TrueVal, Value *X, *Y; unsigned Bitwidth = CmpRHS->getType()->getScalarSizeInBits(); if ((Pred != ICmpInst::ICMP_SGT || - !match(CmpRHS, - m_SpecificInt_ICMP(ICmpInst::ICMP_SGE, APInt(Bitwidth, -1)))) && + !match(CmpRHS, m_SpecificInt_ICMP(ICmpInst::ICMP_SGE, + APInt::getAllOnes(Bitwidth)))) && (Pred != ICmpInst::ICMP_SLT || - !match(CmpRHS, - m_SpecificInt_ICMP(ICmpInst::ICMP_SGE, APInt(Bitwidth, 0))))) + !match(CmpRHS, m_SpecificInt_ICMP(ICmpInst::ICMP_SGE, + APInt::getZero(Bitwidth))))) return nullptr; // Canonicalize so that ashr is in FalseVal. diff --git a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp index ae8e2292519cb7..b14a43a0beb932 100644 --- a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp +++ b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp @@ -859,7 +859,7 @@ void ConstraintInfo::transferToOtherSystem( addFact(CmpInst::ICMP_ULT, A, B, NumIn, NumOut, DFSInStack); break; case CmpInst::ICMP_SGT: { - if (doesHold(CmpInst::ICMP_SGE, B, ConstantInt::get(B->getType(), -1))) + if (doesHold(CmpInst::ICMP_SGE, B, Constant::getAllOnesValue(B->getType()))) addFact(CmpInst::ICMP_UGE, A, ConstantInt::get(B->getType(), 0), NumIn, NumOut, DFSInStack); if (IsKnownNonNegative(B)) diff --git a/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp b/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp index 831911ab21c224..3768fedee71eb2 100644 --- a/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp +++ b/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp @@ -4034,7 +4034,7 @@ void LSRInstance::GenerateICmpZeroScales(LSRUse &LU, unsigned LUIdx, // Compensate for the use having MinOffset built into it. F.BaseOffset = (uint64_t)F.BaseOffset + Offset - LU.MinOffset; - const SCEV *FactorS = SE.getConstant(IntTy, Factor); + const SCEV *FactorS = SE.getConstant(IntTy, Factor, /*isSigned*/ true); // Check that multiplying with each base register doesn't overflow. for (size_t i = 0, e = F.BaseRegs.size(); i != e; ++i) { @@ -4110,7 +4110,7 @@ void LSRInstance::GenerateScales(LSRUse &LU, unsigned LUIdx, Formula Base) { for (size_t i = 0, e = Base.BaseRegs.size(); i != e; ++i) { const SCEVAddRecExpr *AR = dyn_cast(Base.BaseRegs[i]); if (AR && (AR->getLoop() == L || LU.AllFixupsOutsideLoop)) { - const SCEV *FactorS = SE.getConstant(IntTy, Factor); + const SCEV *FactorS = SE.getConstant(IntTy, Factor, true); if (FactorS->isZero()) continue; // Divide out the factor, ignoring high bits, since we'll be @@ -4344,7 +4344,8 @@ void LSRInstance::GenerateCrossUseConstantOffsets() { const SCEV *OrigReg = WI.OrigReg; Type *IntTy = SE.getEffectiveSCEVType(OrigReg->getType()); - const SCEV *NegImmS = SE.getSCEV(ConstantInt::get(IntTy, -(uint64_t)Imm)); + const SCEV *NegImmS = + SE.getSCEV(ConstantInt::getSigned(IntTy, -(uint64_t)Imm)); unsigned BitWidth = SE.getTypeSizeInBits(IntTy); // TODO: Use a more targeted data structure. @@ -4359,8 +4360,8 @@ void LSRInstance::GenerateCrossUseConstantOffsets() { if (F.ScaledReg == OrigReg) { int64_t Offset = (uint64_t)F.BaseOffset + Imm * (uint64_t)F.Scale; // Don't create 50 + reg(-50). - if (F.referencesReg(SE.getSCEV( - ConstantInt::get(IntTy, -(uint64_t)Offset)))) + if (F.referencesReg( + SE.getSCEV(ConstantInt::getSigned(IntTy, -(uint64_t)Offset)))) continue; Formula NewF = F; NewF.BaseOffset = Offset; diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp index 254795ec244534..e66cbc006b24b4 100644 --- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp +++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp @@ -6908,7 +6908,7 @@ static bool ReduceSwitchRange(SwitchInst *SI, IRBuilder<> &Builder, auto *Ty = cast(SI->getCondition()->getType()); Builder.SetInsertPoint(SI); Value *Sub = - Builder.CreateSub(SI->getCondition(), ConstantInt::get(Ty, Base)); + Builder.CreateSub(SI->getCondition(), ConstantInt::getSigned(Ty, Base)); Value *Rot = Builder.CreateIntrinsic( Ty, Intrinsic::fshl, {Sub, Sub, ConstantInt::get(Ty, Ty->getBitWidth() - Shift)}); @@ -6916,7 +6916,7 @@ static bool ReduceSwitchRange(SwitchInst *SI, IRBuilder<> &Builder, for (auto Case : SI->cases()) { auto *Orig = Case.getCaseValue(); - auto Sub = Orig->getValue() - APInt(Ty->getBitWidth(), Base); + auto Sub = Orig->getValue() - APInt(Ty->getBitWidth(), Base, true); Case.setValue(cast(ConstantInt::get(Ty, Sub.lshr(Shift)))); } return true; diff --git a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp index 52eef9ab58a4d9..eba1c89047af95 100644 --- a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp +++ b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp @@ -224,7 +224,7 @@ static Value *convertStrToInt(CallInst *CI, StringRef &Str, Value *EndPtr, // Unsigned negation doesn't overflow. Result = -Result; - return ConstantInt::get(RetTy, Result); + return ConstantInt::get(RetTy, Result, AsSigned); } static bool isOnlyUsedInComparisonWithZero(Value *V) { @@ -553,7 +553,8 @@ Value *LibCallSimplifier::optimizeStrCmp(CallInst *CI, IRBuilderBase &B) { // strcmp(x, y) -> cnst (if both x and y are constant strings) if (HasStr1 && HasStr2) return ConstantInt::get(CI->getType(), - std::clamp(Str1.compare(Str2), -1, 1)); + std::clamp(Str1.compare(Str2), -1, 1), + /*isSigned*/ true); if (HasStr1 && Str1.empty()) // strcmp("", x) -> -*x return B.CreateNeg(B.CreateZExt( @@ -638,7 +639,8 @@ Value *LibCallSimplifier::optimizeStrNCmp(CallInst *CI, IRBuilderBase &B) { StringRef SubStr1 = substr(Str1, Length); StringRef SubStr2 = substr(Str2, Length); return ConstantInt::get(CI->getType(), - std::clamp(SubStr1.compare(SubStr2), -1, 1)); + std::clamp(SubStr1.compare(SubStr2), -1, 1), + /*isSigned*/ true); } if (HasStr1 && Str1.empty()) // strncmp("", x, n) -> -*x @@ -1518,7 +1520,7 @@ static Value *optimizeMemCmpVarSize(CallInst *CI, Value *LHS, Value *RHS, int IRes = UChar(LStr[Pos]) < UChar(RStr[Pos]) ? -1 : 1; Value *MaxSize = ConstantInt::get(Size->getType(), Pos); Value *Cmp = B.CreateICmp(ICmpInst::ICMP_ULE, Size, MaxSize); - Value *Res = ConstantInt::get(CI->getType(), IRes); + Value *Res = ConstantInt::get(CI->getType(), IRes, /*isSigned*/ true); return B.CreateSelect(Cmp, Zero, Res); } diff --git a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp index e51184b0dd1fe5..a1db6abac31e4e 100644 --- a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp +++ b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp @@ -1312,7 +1312,7 @@ void VPVectorPointerRecipe ::execute(VPTransformState &State) { Value *RunTimeVF = getRuntimeVF(Builder, IndexTy, State.VF); // NumElt = -Part * RunTimeVF Value *NumElt = Builder.CreateMul( - ConstantInt::get(IndexTy, -(int64_t)Part), RunTimeVF); + ConstantInt::getSigned(IndexTy, -(int64_t)Part), RunTimeVF); // LastLane = 1 - RunTimeVF Value *LastLane = Builder.CreateSub(ConstantInt::get(IndexTy, 1), RunTimeVF); diff --git a/llvm/unittests/ADT/APFixedPointTest.cpp b/llvm/unittests/ADT/APFixedPointTest.cpp index ecb89fbf76c8bb..7d04140df4850e 100644 --- a/llvm/unittests/ADT/APFixedPointTest.cpp +++ b/llvm/unittests/ADT/APFixedPointTest.cpp @@ -240,19 +240,20 @@ void CheckIntPart(const FixedPointSemantics &Sema, int64_t IntPart) { APFixedPoint ValWithFract( APInt(Sema.getWidth(), relativeShr(IntPart, Sema.getLsbWeight()) + FullFactPart, - Sema.isSigned()), + Sema.isSigned(), /*implicitTrunc*/ true), Sema); ASSERT_EQ(ValWithFract.getIntPart(), IntPart); // Just fraction - APFixedPoint JustFract(APInt(Sema.getWidth(), FullFactPart, Sema.isSigned()), + APFixedPoint JustFract(APInt(Sema.getWidth(), FullFactPart, Sema.isSigned(), + /*implicitTrunc*/ true), Sema); ASSERT_EQ(JustFract.getIntPart(), 0); // Whole number APFixedPoint WholeNum(APInt(Sema.getWidth(), relativeShr(IntPart, Sema.getLsbWeight()), - Sema.isSigned()), + Sema.isSigned(), /*implicitTrunc*/ true), Sema); ASSERT_EQ(WholeNum.getIntPart(), IntPart); @@ -260,7 +261,7 @@ void CheckIntPart(const FixedPointSemantics &Sema, int64_t IntPart) { if (Sema.isSigned()) { APFixedPoint Negative(APInt(Sema.getWidth(), relativeShr(IntPart, Sema.getLsbWeight()), - Sema.isSigned()), + Sema.isSigned(), /*implicitTrunc*/ true), Sema); ASSERT_EQ(Negative.getIntPart(), IntPart); } diff --git a/llvm/unittests/ADT/APIntTest.cpp b/llvm/unittests/ADT/APIntTest.cpp index 3b909f8f7d14e2..3762d21b6edc0d 100644 --- a/llvm/unittests/ADT/APIntTest.cpp +++ b/llvm/unittests/ADT/APIntTest.cpp @@ -218,11 +218,12 @@ TEST(APIntTest, i256) { } TEST(APIntTest, i1) { - const APInt neg_two(1, static_cast(-2), true); + const APInt neg_two(1, static_cast(-2), true, + /*implicitTrunc*/ true); const APInt neg_one(1, static_cast(-1), true); const APInt zero(1, 0); const APInt one(1, 1); - const APInt two(1, 2); + const APInt two(1, 2, false, /*implicitTrunc*/ true); EXPECT_EQ(0, neg_two.getSExtValue()); EXPECT_EQ(-1, neg_one.getSExtValue()); @@ -1108,11 +1109,11 @@ TEST(APIntTest, fromString) { EXPECT_EQ(APInt(32, 3), APInt(32, "+11", 2)); EXPECT_EQ(APInt(32, 4), APInt(32, "+100", 2)); - EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32, "-0", 2)); - EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32, "-1", 2)); - EXPECT_EQ(APInt(32, uint64_t(-2LL)), APInt(32, "-10", 2)); - EXPECT_EQ(APInt(32, uint64_t(-3LL)), APInt(32, "-11", 2)); - EXPECT_EQ(APInt(32, uint64_t(-4LL)), APInt(32, "-100", 2)); + EXPECT_EQ(APInt(32, uint32_t(-0LL)), APInt(32, "-0", 2)); + EXPECT_EQ(APInt(32, uint32_t(-1LL)), APInt(32, "-1", 2)); + EXPECT_EQ(APInt(32, uint32_t(-2LL)), APInt(32, "-10", 2)); + EXPECT_EQ(APInt(32, uint32_t(-3LL)), APInt(32, "-11", 2)); + EXPECT_EQ(APInt(32, uint32_t(-4LL)), APInt(32, "-100", 2)); EXPECT_EQ(APInt(32, 0), APInt(32, "0", 8)); EXPECT_EQ(APInt(32, 1), APInt(32, "1", 8)); @@ -1128,12 +1129,12 @@ TEST(APIntTest, fromString) { EXPECT_EQ(APInt(32, +15), APInt(32, "+17", 8)); EXPECT_EQ(APInt(32, +16), APInt(32, "+20", 8)); - EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32, "-0", 8)); - EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32, "-1", 8)); - EXPECT_EQ(APInt(32, uint64_t(-7LL)), APInt(32, "-7", 8)); - EXPECT_EQ(APInt(32, uint64_t(-8LL)), APInt(32, "-10", 8)); - EXPECT_EQ(APInt(32, uint64_t(-15LL)), APInt(32, "-17", 8)); - EXPECT_EQ(APInt(32, uint64_t(-16LL)), APInt(32, "-20", 8)); + EXPECT_EQ(APInt(32, uint32_t(-0LL)), APInt(32, "-0", 8)); + EXPECT_EQ(APInt(32, uint32_t(-1LL)), APInt(32, "-1", 8)); + EXPECT_EQ(APInt(32, uint32_t(-7LL)), APInt(32, "-7", 8)); + EXPECT_EQ(APInt(32, uint32_t(-8LL)), APInt(32, "-10", 8)); + EXPECT_EQ(APInt(32, uint32_t(-15LL)), APInt(32, "-17", 8)); + EXPECT_EQ(APInt(32, uint32_t(-16LL)), APInt(32, "-20", 8)); EXPECT_EQ(APInt(32, 0), APInt(32, "0", 10)); EXPECT_EQ(APInt(32, 1), APInt(32, "1", 10)); @@ -1142,12 +1143,12 @@ TEST(APIntTest, fromString) { EXPECT_EQ(APInt(32, 19), APInt(32, "19", 10)); EXPECT_EQ(APInt(32, 20), APInt(32, "20", 10)); - EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32, "-0", 10)); - EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32, "-1", 10)); - EXPECT_EQ(APInt(32, uint64_t(-9LL)), APInt(32, "-9", 10)); - EXPECT_EQ(APInt(32, uint64_t(-10LL)), APInt(32, "-10", 10)); - EXPECT_EQ(APInt(32, uint64_t(-19LL)), APInt(32, "-19", 10)); - EXPECT_EQ(APInt(32, uint64_t(-20LL)), APInt(32, "-20", 10)); + EXPECT_EQ(APInt(32, uint32_t(-0LL)), APInt(32, "-0", 10)); + EXPECT_EQ(APInt(32, uint32_t(-1LL)), APInt(32, "-1", 10)); + EXPECT_EQ(APInt(32, uint32_t(-9LL)), APInt(32, "-9", 10)); + EXPECT_EQ(APInt(32, uint32_t(-10LL)), APInt(32, "-10", 10)); + EXPECT_EQ(APInt(32, uint32_t(-19LL)), APInt(32, "-19", 10)); + EXPECT_EQ(APInt(32, uint32_t(-20LL)), APInt(32, "-20", 10)); EXPECT_EQ(APInt(32, 0), APInt(32, "0", 16)); EXPECT_EQ(APInt(32, 1), APInt(32, "1", 16)); @@ -1156,12 +1157,12 @@ TEST(APIntTest, fromString) { EXPECT_EQ(APInt(32, 31), APInt(32, "1F", 16)); EXPECT_EQ(APInt(32, 32), APInt(32, "20", 16)); - EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32, "-0", 16)); - EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32, "-1", 16)); - EXPECT_EQ(APInt(32, uint64_t(-15LL)), APInt(32, "-F", 16)); - EXPECT_EQ(APInt(32, uint64_t(-16LL)), APInt(32, "-10", 16)); - EXPECT_EQ(APInt(32, uint64_t(-31LL)), APInt(32, "-1F", 16)); - EXPECT_EQ(APInt(32, uint64_t(-32LL)), APInt(32, "-20", 16)); + EXPECT_EQ(APInt(32, uint32_t(-0LL)), APInt(32, "-0", 16)); + EXPECT_EQ(APInt(32, uint32_t(-1LL)), APInt(32, "-1", 16)); + EXPECT_EQ(APInt(32, uint32_t(-15LL)), APInt(32, "-F", 16)); + EXPECT_EQ(APInt(32, uint32_t(-16LL)), APInt(32, "-10", 16)); + EXPECT_EQ(APInt(32, uint32_t(-31LL)), APInt(32, "-1F", 16)); + EXPECT_EQ(APInt(32, uint32_t(-32LL)), APInt(32, "-20", 16)); EXPECT_EQ(APInt(32, 0), APInt(32, "0", 36)); EXPECT_EQ(APInt(32, 1), APInt(32, "1", 36)); @@ -1170,12 +1171,12 @@ TEST(APIntTest, fromString) { EXPECT_EQ(APInt(32, 71), APInt(32, "1Z", 36)); EXPECT_EQ(APInt(32, 72), APInt(32, "20", 36)); - EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32, "-0", 36)); - EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32, "-1", 36)); - EXPECT_EQ(APInt(32, uint64_t(-35LL)), APInt(32, "-Z", 36)); - EXPECT_EQ(APInt(32, uint64_t(-36LL)), APInt(32, "-10", 36)); - EXPECT_EQ(APInt(32, uint64_t(-71LL)), APInt(32, "-1Z", 36)); - EXPECT_EQ(APInt(32, uint64_t(-72LL)), APInt(32, "-20", 36)); + EXPECT_EQ(APInt(32, uint32_t(-0LL)), APInt(32, "-0", 36)); + EXPECT_EQ(APInt(32, uint32_t(-1LL)), APInt(32, "-1", 36)); + EXPECT_EQ(APInt(32, uint32_t(-35LL)), APInt(32, "-Z", 36)); + EXPECT_EQ(APInt(32, uint32_t(-36LL)), APInt(32, "-10", 36)); + EXPECT_EQ(APInt(32, uint32_t(-71LL)), APInt(32, "-1Z", 36)); + EXPECT_EQ(APInt(32, uint32_t(-72LL)), APInt(32, "-20", 36)); } TEST(APIntTest, SaturatingMath) { @@ -1199,10 +1200,10 @@ TEST(APIntTest, SaturatingMath) { EXPECT_EQ(APInt(6, 31), AP_42.truncSSat(6)); EXPECT_EQ(APInt(5, 15), AP_42.truncSSat(5)); - EXPECT_EQ(APInt(8, -56), AP_200.truncSSat(8)); - EXPECT_EQ(APInt(7, -56), AP_200.truncSSat(7)); - EXPECT_EQ(APInt(6, -32), AP_200.truncSSat(6)); - EXPECT_EQ(APInt(5, -16), AP_200.truncSSat(5)); + EXPECT_EQ(APInt(8, -56, true), AP_200.truncSSat(8)); + EXPECT_EQ(APInt(7, -56, true), AP_200.truncSSat(7)); + EXPECT_EQ(APInt(6, -32, true), AP_200.truncSSat(6)); + EXPECT_EQ(APInt(5, -16, true), AP_200.truncSSat(5)); EXPECT_EQ(APInt(8, 200), AP_100.uadd_sat(AP_100)); EXPECT_EQ(APInt(8, 255), AP_100.uadd_sat(AP_200)); @@ -1210,52 +1211,53 @@ TEST(APIntTest, SaturatingMath) { EXPECT_EQ(APInt(8, 110), AP_10.sadd_sat(AP_100)); EXPECT_EQ(APInt(8, 127), AP_100.sadd_sat(AP_100)); - EXPECT_EQ(APInt(8, -128), (-AP_100).sadd_sat(-AP_100)); - EXPECT_EQ(APInt(8, -128), APInt(8, -128).sadd_sat(APInt(8, -128))); + EXPECT_EQ(APInt(8, -128, true), (-AP_100).sadd_sat(-AP_100)); + EXPECT_EQ(APInt(8, -128, true), + APInt(8, -128, true).sadd_sat(APInt(8, -128, true))); EXPECT_EQ(APInt(8, 90), AP_100.usub_sat(AP_10)); EXPECT_EQ(APInt(8, 0), AP_100.usub_sat(AP_200)); EXPECT_EQ(APInt(8, 0), APInt(8, 0).usub_sat(APInt(8, 255))); - EXPECT_EQ(APInt(8, -90), AP_10.ssub_sat(AP_100)); + EXPECT_EQ(APInt(8, -90, true), AP_10.ssub_sat(AP_100)); EXPECT_EQ(APInt(8, 127), AP_100.ssub_sat(-AP_100)); - EXPECT_EQ(APInt(8, -128), (-AP_100).ssub_sat(AP_100)); - EXPECT_EQ(APInt(8, -128), APInt(8, -128).ssub_sat(APInt(8, 127))); + EXPECT_EQ(APInt(8, -128, true), (-AP_100).ssub_sat(AP_100)); + EXPECT_EQ(APInt(8, -128, true), APInt(8, -128, true).ssub_sat(APInt(8, 127))); EXPECT_EQ(APInt(8, 250), APInt(8, 50).umul_sat(APInt(8, 5))); EXPECT_EQ(APInt(8, 255), APInt(8, 50).umul_sat(APInt(8, 6))); - EXPECT_EQ(APInt(8, 255), APInt(8, -128).umul_sat(APInt(8, 3))); - EXPECT_EQ(APInt(8, 255), APInt(8, 3).umul_sat(APInt(8, -128))); - EXPECT_EQ(APInt(8, 255), APInt(8, -128).umul_sat(APInt(8, -128))); + EXPECT_EQ(APInt(8, 255), APInt(8, -128, true).umul_sat(APInt(8, 3))); + EXPECT_EQ(APInt(8, 255), APInt(8, 3).umul_sat(APInt(8, -128, true))); + EXPECT_EQ(APInt(8, 255), APInt(8, -128, true).umul_sat(APInt(8, -128, true))); EXPECT_EQ(APInt(8, 125), APInt(8, 25).smul_sat(APInt(8, 5))); EXPECT_EQ(APInt(8, 127), APInt(8, 25).smul_sat(APInt(8, 6))); EXPECT_EQ(APInt(8, 127), APInt(8, 127).smul_sat(APInt(8, 127))); - EXPECT_EQ(APInt(8, -125), APInt(8, -25).smul_sat(APInt(8, 5))); - EXPECT_EQ(APInt(8, -125), APInt(8, 25).smul_sat(APInt(8, -5))); - EXPECT_EQ(APInt(8, 125), APInt(8, -25).smul_sat(APInt(8, -5))); + EXPECT_EQ(APInt(8, -125, true), APInt(8, -25, true).smul_sat(APInt(8, 5))); + EXPECT_EQ(APInt(8, -125, true), APInt(8, 25).smul_sat(APInt(8, -5, true))); + EXPECT_EQ(APInt(8, 125), APInt(8, -25, true).smul_sat(APInt(8, -5, true))); EXPECT_EQ(APInt(8, 125), APInt(8, 25).smul_sat(APInt(8, 5))); - EXPECT_EQ(APInt(8, -128), APInt(8, -25).smul_sat(APInt(8, 6))); - EXPECT_EQ(APInt(8, -128), APInt(8, 25).smul_sat(APInt(8, -6))); - EXPECT_EQ(APInt(8, 127), APInt(8, -25).smul_sat(APInt(8, -6))); + EXPECT_EQ(APInt(8, -128, true), APInt(8, -25, true).smul_sat(APInt(8, 6))); + EXPECT_EQ(APInt(8, -128, true), APInt(8, 25).smul_sat(APInt(8, -6, true))); + EXPECT_EQ(APInt(8, 127), APInt(8, -25, true).smul_sat(APInt(8, -6, true))); EXPECT_EQ(APInt(8, 127), APInt(8, 25).smul_sat(APInt(8, 6))); EXPECT_EQ(APInt(8, 128), APInt(8, 4).ushl_sat(APInt(8, 5))); EXPECT_EQ(APInt(8, 255), APInt(8, 4).ushl_sat(APInt(8, 6))); EXPECT_EQ(APInt(8, 128), APInt(8, 1).ushl_sat(APInt(8, 7))); EXPECT_EQ(APInt(8, 255), APInt(8, 1).ushl_sat(APInt(8, 8))); - EXPECT_EQ(APInt(8, 255), APInt(8, -128).ushl_sat(APInt(8, 2))); + EXPECT_EQ(APInt(8, 255), APInt(8, -128, true).ushl_sat(APInt(8, 2))); EXPECT_EQ(APInt(8, 255), APInt(8, 64).ushl_sat(APInt(8, 2))); - EXPECT_EQ(APInt(8, 255), APInt(8, 64).ushl_sat(APInt(8, -2))); + EXPECT_EQ(APInt(8, 255), APInt(8, 64).ushl_sat(APInt(8, -2, true))); EXPECT_EQ(APInt(8, 64), APInt(8, 4).sshl_sat(APInt(8, 4))); EXPECT_EQ(APInt(8, 127), APInt(8, 4).sshl_sat(APInt(8, 5))); EXPECT_EQ(APInt(8, 127), APInt(8, 1).sshl_sat(APInt(8, 8))); - EXPECT_EQ(APInt(8, -64), APInt(8, -4).sshl_sat(APInt(8, 4))); - EXPECT_EQ(APInt(8, -128), APInt(8, -4).sshl_sat(APInt(8, 5))); - EXPECT_EQ(APInt(8, -128), APInt(8, -4).sshl_sat(APInt(8, 6))); - EXPECT_EQ(APInt(8, -128), APInt(8, -1).sshl_sat(APInt(8, 7))); - EXPECT_EQ(APInt(8, -128), APInt(8, -1).sshl_sat(APInt(8, 8))); + EXPECT_EQ(APInt(8, -64, true), APInt(8, -4, true).sshl_sat(APInt(8, 4))); + EXPECT_EQ(APInt(8, -128, true), APInt(8, -4, true).sshl_sat(APInt(8, 5))); + EXPECT_EQ(APInt(8, -128, true), APInt(8, -4, true).sshl_sat(APInt(8, 6))); + EXPECT_EQ(APInt(8, -128, true), APInt(8, -1, true).sshl_sat(APInt(8, 7))); + EXPECT_EQ(APInt(8, -128, true), APInt(8, -1, true).sshl_sat(APInt(8, 8))); } TEST(APIntTest, FromArray) { @@ -1380,39 +1382,39 @@ TEST(APIntTest, toString) { S.clear(); isSigned = false; - APInt(8, 255, isSigned).toString(S, 2, isSigned, true); + APInt(8, 255).toString(S, 2, isSigned, true); EXPECT_EQ(std::string(S), "0b11111111"); S.clear(); - APInt(8, 255, isSigned).toString(S, 8, isSigned, true); + APInt(8, 255).toString(S, 8, isSigned, true); EXPECT_EQ(std::string(S), "0377"); S.clear(); - APInt(8, 255, isSigned).toString(S, 10, isSigned, true); + APInt(8, 255).toString(S, 10, isSigned, true); EXPECT_EQ(std::string(S), "255"); S.clear(); - APInt(8, 255, isSigned).toString(S, 16, isSigned, true, /*UpperCase=*/false); + APInt(8, 255).toString(S, 16, isSigned, true, /*UpperCase=*/false); EXPECT_EQ(std::string(S), "0xff"); S.clear(); - APInt(8, 255, isSigned).toString(S, 16, isSigned, true); + APInt(8, 255).toString(S, 16, isSigned, true); EXPECT_EQ(std::string(S), "0xFF"); S.clear(); - APInt(8, 255, isSigned).toString(S, 36, isSigned, false); + APInt(8, 255).toString(S, 36, isSigned, false); EXPECT_EQ(std::string(S), "73"); S.clear(); isSigned = true; - APInt(8, 255, isSigned).toString(S, 2, isSigned, true); + APInt(8, 255).toString(S, 2, isSigned, true); EXPECT_EQ(std::string(S), "-0b1"); S.clear(); - APInt(8, 255, isSigned).toString(S, 8, isSigned, true); + APInt(8, 255).toString(S, 8, isSigned, true); EXPECT_EQ(std::string(S), "-01"); S.clear(); - APInt(8, 255, isSigned).toString(S, 10, isSigned, true); + APInt(8, 255).toString(S, 10, isSigned, true); EXPECT_EQ(std::string(S), "-1"); S.clear(); - APInt(8, 255, isSigned).toString(S, 16, isSigned, true); + APInt(8, 255).toString(S, 16, isSigned, true); EXPECT_EQ(std::string(S), "-0x1"); S.clear(); - APInt(8, 255, isSigned).toString(S, 36, isSigned, false); + APInt(8, 255).toString(S, 36, isSigned, false); EXPECT_EQ(std::string(S), "-1"); S.clear(); } @@ -1477,7 +1479,7 @@ TEST(APIntTest, Rotate) { EXPECT_EQ(APInt(32, 2), APInt(32, 1).rotl(APInt(33, 33))); EXPECT_EQ(APInt(32, (1 << 8)), APInt(32, 1).rotl(APInt(32, 40))); EXPECT_EQ(APInt(32, (1 << 30)), APInt(32, 1).rotl(APInt(31, 30))); - EXPECT_EQ(APInt(32, (1 << 31)), APInt(32, 1).rotl(APInt(31, 31))); + EXPECT_EQ(APInt(32, (1u << 31)), APInt(32, 1).rotl(APInt(31, 31))); EXPECT_EQ(APInt(32, 1), APInt(32, 1).rotl(APInt(1, 0))); EXPECT_EQ(APInt(32, 2), APInt(32, 1).rotl(APInt(1, 1))); @@ -1504,24 +1506,24 @@ TEST(APIntTest, Rotate) { EXPECT_EQ(APInt(8, 16), APInt(8, 1).rotr(4)); EXPECT_EQ(APInt(8, 1), APInt(8, 1).rotr(8)); - EXPECT_EQ(APInt(32, (1 << 31)), APInt(32, 1).rotr(33)); - EXPECT_EQ(APInt(32, (1 << 31)), APInt(32, 1).rotr(APInt(32, 33))); + EXPECT_EQ(APInt(32, (1u << 31)), APInt(32, 1).rotr(33)); + EXPECT_EQ(APInt(32, (1u << 31)), APInt(32, 1).rotr(APInt(32, 33))); - EXPECT_EQ(APInt(32, (1 << 31)), APInt(32, 1).rotr(33)); - EXPECT_EQ(APInt(32, (1 << 31)), APInt(32, 1).rotr(APInt(32, 33))); - EXPECT_EQ(APInt(32, (1 << 31)), APInt(32, 1).rotr(APInt(33, 33))); + EXPECT_EQ(APInt(32, (1u << 31)), APInt(32, 1).rotr(33)); + EXPECT_EQ(APInt(32, (1u << 31)), APInt(32, 1).rotr(APInt(32, 33))); + EXPECT_EQ(APInt(32, (1u << 31)), APInt(32, 1).rotr(APInt(33, 33))); EXPECT_EQ(APInt(32, (1 << 24)), APInt(32, 1).rotr(APInt(32, 40))); EXPECT_EQ(APInt(32, (1 << 2)), APInt(32, 1).rotr(APInt(31, 30))); EXPECT_EQ(APInt(32, (1 << 1)), APInt(32, 1).rotr(APInt(31, 31))); EXPECT_EQ(APInt(32, 1), APInt(32, 1).rotr(APInt(1, 0))); - EXPECT_EQ(APInt(32, (1 << 31)), APInt(32, 1).rotr(APInt(1, 1))); + EXPECT_EQ(APInt(32, (1u << 31)), APInt(32, 1).rotr(APInt(1, 1))); EXPECT_EQ(APInt(32, (1 << 28)), APInt(32, 1).rotr(APInt(3, 4))); EXPECT_EQ(APInt(32, 1), APInt(32, 1).rotr(APInt(64, 64))); - EXPECT_EQ(APInt(32, (1 << 31)), APInt(32, 1).rotr(APInt(64, 65))); + EXPECT_EQ(APInt(32, (1u << 31)), APInt(32, 1).rotr(APInt(64, 65))); EXPECT_EQ(APInt(7, 48), APInt(7, 3).rotr(APInt(7, 3))); EXPECT_EQ(APInt(7, 48), APInt(7, 3).rotr(APInt(7, 10))); @@ -1543,7 +1545,7 @@ TEST(APIntTest, Splat) { APInt ValB(3, 5); EXPECT_EQ(APInt(4, 0xD), APInt::getSplat(4, ValB)); - EXPECT_EQ(APInt(15, 0xDB6D), APInt::getSplat(15, ValB)); + EXPECT_EQ(APInt(15, 0x5B6D), APInt::getSplat(15, ValB)); } TEST(APIntTest, tcDecrement) { @@ -2687,7 +2689,7 @@ TEST(APIntTest, sext) { EXPECT_EQ(31U, i32_min.countr_zero()); EXPECT_EQ(32U, i32_min.popcount()); - APInt i32_neg1(APInt(32, ~uint64_t(0)).sext(63)); + APInt i32_neg1(APInt(32, ~uint32_t(0)).sext(63)); EXPECT_EQ(i32_neg1, i32_neg1.sext(63)); EXPECT_EQ(63U, i32_neg1.countl_one()); EXPECT_EQ(0U, i32_neg1.countr_zero()); @@ -2765,7 +2767,7 @@ TEST(APIntTest, RoundingUDiv) { TEST(APIntTest, RoundingSDiv) { for (int64_t Ai = -128; Ai <= 127; Ai++) { - APInt A(8, Ai); + APInt A(8, Ai, true); if (Ai != 0) { APInt Zero(8, 0); @@ -2778,7 +2780,7 @@ TEST(APIntTest, RoundingSDiv) { if (Bi == 0) continue; - APInt B(8, Bi); + APInt B(8, Bi, true); APInt QuoTowardZero = A.sdiv(B); { APInt Quo = APIntOps::RoundingSDiv(A, B, APInt::Rounding::UP); @@ -2930,7 +2932,8 @@ TEST(APIntTest, SolveQuadraticEquationWrap) { for (int B = Low; B != High; ++B) { for (int C = Low; C != High; ++C) { std::optional S = APIntOps::SolveQuadraticEquationWrap( - APInt(Width, A), APInt(Width, B), APInt(Width, C), Width); + APInt(Width, A, true), APInt(Width, B, true), + APInt(Width, C, true), Width); if (S) Validate(A, B, C, Width, S->getSExtValue()); } @@ -3035,10 +3038,10 @@ TEST(APIntTest, GetMostSignificantDifferentBitExaustive) { } TEST(APIntTest, SignbitZeroChecks) { - EXPECT_TRUE(APInt(8, -1).isNegative()); - EXPECT_FALSE(APInt(8, -1).isNonNegative()); - EXPECT_FALSE(APInt(8, -1).isStrictlyPositive()); - EXPECT_TRUE(APInt(8, -1).isNonPositive()); + EXPECT_TRUE(APInt(8, -1, true).isNegative()); + EXPECT_FALSE(APInt(8, -1, true).isNonNegative()); + EXPECT_FALSE(APInt(8, -1, true).isStrictlyPositive()); + EXPECT_TRUE(APInt(8, -1, true).isNonPositive()); EXPECT_FALSE(APInt(8, 0).isNegative()); EXPECT_TRUE(APInt(8, 0).isNonNegative()); diff --git a/llvm/unittests/ADT/APSIntTest.cpp b/llvm/unittests/ADT/APSIntTest.cpp index f804eba3ca83eb..2d2a64433da94d 100644 --- a/llvm/unittests/ADT/APSIntTest.cpp +++ b/llvm/unittests/ADT/APSIntTest.cpp @@ -74,14 +74,14 @@ TEST(APSIntTest, getExtValue) { EXPECT_TRUE(APSInt(APInt(3, 7), false).isSigned()); EXPECT_TRUE(APSInt(APInt(4, 7), true).isUnsigned()); EXPECT_TRUE(APSInt(APInt(4, 7), false).isSigned()); - EXPECT_TRUE(APSInt(APInt(4, -7), true).isUnsigned()); - EXPECT_TRUE(APSInt(APInt(4, -7), false).isSigned()); + EXPECT_TRUE(APSInt(APInt(4, -7, true), true).isUnsigned()); + EXPECT_TRUE(APSInt(APInt(4, -7, true), false).isSigned()); EXPECT_EQ(7, APSInt(APInt(3, 7), true).getExtValue()); EXPECT_EQ(-1, APSInt(APInt(3, 7), false).getExtValue()); EXPECT_EQ(7, APSInt(APInt(4, 7), true).getExtValue()); EXPECT_EQ(7, APSInt(APInt(4, 7), false).getExtValue()); - EXPECT_EQ(9, APSInt(APInt(4, -7), true).getExtValue()); - EXPECT_EQ(-7, APSInt(APInt(4, -7), false).getExtValue()); + EXPECT_EQ(9, APSInt(APInt(4, -7, true), true).getExtValue()); + EXPECT_EQ(-7, APSInt(APInt(4, -7, true), false).getExtValue()); } TEST(APSIntTest, tryExtValue) { ASSERT_EQ(-7, APSInt(APInt(64, -7), false).tryExtValue().value_or(42)); diff --git a/llvm/unittests/ADT/StringExtrasTest.cpp b/llvm/unittests/ADT/StringExtrasTest.cpp index 1fb1fea6577911..eb202bad865d30 100644 --- a/llvm/unittests/ADT/StringExtrasTest.cpp +++ b/llvm/unittests/ADT/StringExtrasTest.cpp @@ -296,11 +296,11 @@ TEST(StringExtrasTest, toStringAPInt) { EXPECT_EQ(toString(APInt(8, 255, isSigned), 36, isSigned, false), "73"); isSigned = true; - EXPECT_EQ(toString(APInt(8, 255, isSigned), 2, isSigned, true), "-0b1"); - EXPECT_EQ(toString(APInt(8, 255, isSigned), 8, isSigned, true), "-01"); - EXPECT_EQ(toString(APInt(8, 255, isSigned), 10, isSigned, true), "-1"); - EXPECT_EQ(toString(APInt(8, 255, isSigned), 16, isSigned, true), "-0x1"); - EXPECT_EQ(toString(APInt(8, 255, isSigned), 36, isSigned, false), "-1"); + EXPECT_EQ(toString(APInt(8, -1, isSigned), 2, isSigned, true), "-0b1"); + EXPECT_EQ(toString(APInt(8, -1, isSigned), 8, isSigned, true), "-01"); + EXPECT_EQ(toString(APInt(8, -1, isSigned), 10, isSigned, true), "-1"); + EXPECT_EQ(toString(APInt(8, -1, isSigned), 16, isSigned, true), "-0x1"); + EXPECT_EQ(toString(APInt(8, -1, isSigned), 36, isSigned, false), "-1"); } TEST(StringExtrasTest, toStringAPSInt) { diff --git a/llvm/unittests/Analysis/ScalarEvolutionTest.cpp b/llvm/unittests/Analysis/ScalarEvolutionTest.cpp index a7b3c5c404ab75..386a59d3a85a1e 100644 --- a/llvm/unittests/Analysis/ScalarEvolutionTest.cpp +++ b/llvm/unittests/Analysis/ScalarEvolutionTest.cpp @@ -982,7 +982,7 @@ TEST_F(ScalarEvolutionsTest, SCEVAddRecFromPHIwithLargeConstantAccum) { // entry: BranchInst::Create(LoopBB, EntryBB); // loop: - auto *MinInt32 = ConstantInt::get(Context, APInt(32, 0x80000000U, true)); + auto *MinInt32 = ConstantInt::get(Context, APInt(32, 0x80000000U)); auto *Int32_16 = ConstantInt::get(Context, APInt(32, 16)); auto *Br = BranchInst::Create( LoopBB, ExitBB, UndefValue::get(Type::getInt1Ty(Context)), LoopBB); diff --git a/llvm/unittests/FuzzMutate/RandomIRBuilderTest.cpp b/llvm/unittests/FuzzMutate/RandomIRBuilderTest.cpp index 3ce85f5d7be23e..10797b12b17e71 100644 --- a/llvm/unittests/FuzzMutate/RandomIRBuilderTest.cpp +++ b/llvm/unittests/FuzzMutate/RandomIRBuilderTest.cpp @@ -591,7 +591,9 @@ TEST(RandomIRBuilderTest, SrcAndSinkWOrphanBlock) { std::vector Constants; for (Type *IntTy : IntTys) { for (size_t v : {1, 42}) { - Constants.push_back(ConstantInt::get(IntTy, v)); + Constants.push_back( + ConstantInt::get(IntTy, APInt(IntTy->getIntegerBitWidth(), v, false, + /*implicitTrunc*/ true))); } } for (int i = 0; i < 10; i++) { diff --git a/llvm/unittests/IR/ConstantRangeTest.cpp b/llvm/unittests/IR/ConstantRangeTest.cpp index e505af5d3275ef..c1adb30ab96017 100644 --- a/llvm/unittests/IR/ConstantRangeTest.cpp +++ b/llvm/unittests/IR/ConstantRangeTest.cpp @@ -362,10 +362,10 @@ TEST_F(ConstantRangeTest, GetMinsAndMaxes) { EXPECT_EQ(Some.getSignedMax(), APInt(16, 0xaa9)); EXPECT_EQ(Wrap.getSignedMax(), APInt(16, INT16_MAX)); - EXPECT_EQ(Full.getSignedMin(), APInt(16, (uint64_t)INT16_MIN)); + EXPECT_EQ(Full.getSignedMin(), APInt(16, (uint16_t)INT16_MIN)); EXPECT_EQ(One.getSignedMin(), APInt(16, 0xa)); EXPECT_EQ(Some.getSignedMin(), APInt(16, 0xa)); - EXPECT_EQ(Wrap.getSignedMin(), APInt(16, (uint64_t)INT16_MIN)); + EXPECT_EQ(Wrap.getSignedMin(), APInt(16, (uint16_t)INT16_MIN)); // Found by Klee EXPECT_EQ(ConstantRange(APInt(4, 7), APInt(4, 0)).getSignedMax(), @@ -477,7 +477,7 @@ TEST_F(ConstantRangeTest, SExt) { APInt(20, INT16_MAX + 1, true))); EXPECT_EQ(ConstantRange(APInt(8, 120), APInt(8, 140)).signExtend(16), - ConstantRange(APInt(16, -128), APInt(16, 128))); + ConstantRange(APInt(16, -128, true), APInt(16, 128))); EXPECT_EQ(ConstantRange(APInt(16, 0x0200), APInt(16, 0x8000)).signExtend(19), ConstantRange(APInt(19, 0x0200), APInt(19, 0x8000))); @@ -506,7 +506,7 @@ TEST_F(ConstantRangeTest, IntersectWith) { EXPECT_TRUE(LHS.intersectWith(RHS) == LHS); // previous bug: intersection of [min, 3) and [2, max) should be 2 - LHS = ConstantRange(APInt(32, -2147483646), APInt(32, 3)); + LHS = ConstantRange(APInt(32, (uint32_t)-2147483646), APInt(32, 3)); RHS = ConstantRange(APInt(32, 2), APInt(32, 2147483646)); EXPECT_EQ(LHS.intersectWith(RHS), ConstantRange(APInt(32, 2))); @@ -994,17 +994,17 @@ TEST_F(ConstantRangeTest, Multiply) { ConstantRange(APInt(8, 250), APInt(8, 253))); // TODO: This should be return [-2, 0] - EXPECT_EQ(ConstantRange(APInt(8, -2)).multiply( + EXPECT_EQ(ConstantRange(APInt(8, -2, true)).multiply( ConstantRange(APInt(8, 0), APInt(8, 2))), - ConstantRange(APInt(8, -2), APInt(8, 1))); + ConstantRange(APInt(8, -2, true), APInt(8, 1))); // Multiplication by -1 should give precise results. - EXPECT_EQ(ConstantRange(APInt(8, 3), APInt(8, -11)) - .multiply(ConstantRange(APInt(8, -1))), - ConstantRange(APInt(8, 12), APInt(8, -2))); - EXPECT_EQ(ConstantRange(APInt(8, -1)) - .multiply(ConstantRange(APInt(8, 3), APInt(8, -11))), - ConstantRange(APInt(8, 12), APInt(8, -2))); + EXPECT_EQ(ConstantRange(APInt(8, 3), APInt(8, -11, true)) + .multiply(ConstantRange(APInt(8, -1, true))), + ConstantRange(APInt(8, 12), APInt(8, -2, true))); + EXPECT_EQ(ConstantRange(APInt(8, -1, true)) + .multiply(ConstantRange(APInt(8, 3), APInt(8, -11, true))), + ConstantRange(APInt(8, 12), APInt(8, -2, true))); TestBinaryOpExhaustive( [](const ConstantRange &CR1, const ConstantRange &CR2) { @@ -1074,10 +1074,10 @@ TEST_F(ConstantRangeTest, SMax) { EXPECT_EQ(Empty.smax(One), Empty); EXPECT_EQ(Some.smax(Some), Some); EXPECT_EQ(Some.smax(Wrap), ConstantRange(APInt(16, 0xa), - APInt(16, (uint64_t)INT16_MIN))); + APInt(16, (uint16_t)INT16_MIN))); EXPECT_EQ(Some.smax(One), Some); EXPECT_EQ(Wrap.smax(One), ConstantRange(APInt(16, 0xa), - APInt(16, (uint64_t)INT16_MIN))); + APInt(16, (uint16_t)INT16_MIN))); EXPECT_EQ(One.smax(One), One); TestBinaryOpExhaustive( @@ -1119,20 +1119,20 @@ TEST_F(ConstantRangeTest, UMin) { TEST_F(ConstantRangeTest, SMin) { EXPECT_EQ(Full.smin(Full), Full); EXPECT_EQ(Full.smin(Empty), Empty); - EXPECT_EQ(Full.smin(Some), ConstantRange(APInt(16, (uint64_t)INT16_MIN), - APInt(16, 0xaaa))); + EXPECT_EQ(Full.smin(Some), + ConstantRange(APInt(16, (uint16_t)INT16_MIN), APInt(16, 0xaaa))); EXPECT_EQ(Full.smin(Wrap), Full); EXPECT_EQ(Empty.smin(Empty), Empty); EXPECT_EQ(Empty.smin(Some), Empty); EXPECT_EQ(Empty.smin(Wrap), Empty); EXPECT_EQ(Empty.smin(One), Empty); EXPECT_EQ(Some.smin(Some), Some); - EXPECT_EQ(Some.smin(Wrap), ConstantRange(APInt(16, (uint64_t)INT16_MIN), - APInt(16, 0xaaa))); + EXPECT_EQ(Some.smin(Wrap), + ConstantRange(APInt(16, (uint16_t)INT16_MIN), APInt(16, 0xaaa))); EXPECT_EQ(Some.smin(One), One); EXPECT_EQ(Wrap.smin(Wrap), Wrap); - EXPECT_EQ(Wrap.smin(One), ConstantRange(APInt(16, (uint64_t)INT16_MIN), - APInt(16, 0xb))); + EXPECT_EQ(Wrap.smin(One), + ConstantRange(APInt(16, (uint16_t)INT16_MIN), APInt(16, 0xb))); EXPECT_EQ(One.smin(One), One); TestBinaryOpExhaustive( @@ -1208,8 +1208,8 @@ TEST_F(ConstantRangeTest, SDiv) { } // If there is a non-full signed envelope, that should be the result. - APInt SMin(Bits, Results.find_first() - Bias); - APInt SMax(Bits, Results.find_last() - Bias); + APInt SMin(Bits, Results.find_first() - Bias, true); + APInt SMax(Bits, Results.find_last() - Bias, true); ConstantRange Envelope = ConstantRange::getNonEmpty(SMin, SMax + 1); if (!Envelope.isFullSet()) { EXPECT_EQ(Envelope, CR); @@ -1228,8 +1228,8 @@ TEST_F(ConstantRangeTest, SDiv) { --LastPos; } - APInt WMax(Bits, LastNeg); - APInt WMin(Bits, LastPos); + APInt WMax(Bits, LastNeg, true); + APInt WMin(Bits, LastPos, true); ConstantRange Wrapped = ConstantRange::getNonEmpty(WMin, WMax + 1); EXPECT_EQ(Wrapped, CR); }); @@ -1282,8 +1282,8 @@ TEST_F(ConstantRangeTest, SRem) { EXPECT_EQ(Full.srem(Full), ConstantRange(APInt::getSignedMinValue(16) + 1, APInt::getSignedMinValue(16))); - ConstantRange PosMod(APInt(16, 10), APInt(16, 21)); // [10, 20] - ConstantRange NegMod(APInt(16, -20), APInt(16, -9)); // [-20, -10] + ConstantRange PosMod(APInt(16, 10), APInt(16, 21)); // [10, 20] + ConstantRange NegMod(APInt(16, -20, true), APInt(16, -9, true)); // [-20, -10] ConstantRange IntMinMod(APInt::getSignedMinValue(16)); ConstantRange Expected(16, true); @@ -1293,12 +1293,12 @@ TEST_F(ConstantRangeTest, SRem) { Expected = ConstantRange(APInt(16, 0), APInt(16, 20)); EXPECT_EQ(PosLargeLHS.srem(PosMod), Expected); EXPECT_EQ(PosLargeLHS.srem(NegMod), Expected); - ConstantRange NegLargeLHS(APInt(16, -40), APInt(16, 1)); - Expected = ConstantRange(APInt(16, -19), APInt(16, 1)); + ConstantRange NegLargeLHS(APInt(16, -40, true), APInt(16, 1)); + Expected = ConstantRange(APInt(16, -19, true), APInt(16, 1)); EXPECT_EQ(NegLargeLHS.srem(PosMod), Expected); EXPECT_EQ(NegLargeLHS.srem(NegMod), Expected); - ConstantRange PosNegLargeLHS(APInt(16, -32), APInt(16, 38)); - Expected = ConstantRange(APInt(16, -19), APInt(16, 20)); + ConstantRange PosNegLargeLHS(APInt(16, -32, true), APInt(16, 38)); + Expected = ConstantRange(APInt(16, -19, true), APInt(16, 20)); EXPECT_EQ(PosNegLargeLHS.srem(PosMod), Expected); EXPECT_EQ(PosNegLargeLHS.srem(NegMod), Expected); @@ -1307,11 +1307,11 @@ TEST_F(ConstantRangeTest, SRem) { EXPECT_EQ(PosLHS.srem(PosMod), PosLHS); EXPECT_EQ(PosLHS.srem(NegMod), PosLHS); EXPECT_EQ(PosLHS.srem(IntMinMod), PosLHS); - ConstantRange NegLHS(APInt(16, -15), APInt(16, 1)); + ConstantRange NegLHS(APInt(16, -15, true), APInt(16, 1)); EXPECT_EQ(NegLHS.srem(PosMod), NegLHS); EXPECT_EQ(NegLHS.srem(NegMod), NegLHS); EXPECT_EQ(NegLHS.srem(IntMinMod), NegLHS); - ConstantRange PosNegLHS(APInt(16, -12), APInt(16, 18)); + ConstantRange PosNegLHS(APInt(16, -12, true), APInt(16, 18)); EXPECT_EQ(PosNegLHS.srem(PosMod), PosNegLHS); EXPECT_EQ(PosNegLHS.srem(NegMod), PosNegLHS); EXPECT_EQ(PosNegLHS.srem(IntMinMod), PosNegLHS); @@ -1321,11 +1321,11 @@ TEST_F(ConstantRangeTest, SRem) { EXPECT_EQ(PosSmallLHS.srem(PosMod), PosSmallLHS); EXPECT_EQ(PosSmallLHS.srem(NegMod), PosSmallLHS); EXPECT_EQ(PosSmallLHS.srem(IntMinMod), PosSmallLHS); - ConstantRange NegSmallLHS(APInt(16, -7), APInt(16, -2)); + ConstantRange NegSmallLHS(APInt(16, -7, true), APInt(16, -2, true)); EXPECT_EQ(NegSmallLHS.srem(PosMod), NegSmallLHS); EXPECT_EQ(NegSmallLHS.srem(NegMod), NegSmallLHS); EXPECT_EQ(NegSmallLHS.srem(IntMinMod), NegSmallLHS); - ConstantRange PosNegSmallLHS(APInt(16, -3), APInt(16, 8)); + ConstantRange PosNegSmallLHS(APInt(16, -3, true), APInt(16, 8)); EXPECT_EQ(PosNegSmallLHS.srem(PosMod), PosNegSmallLHS); EXPECT_EQ(PosNegSmallLHS.srem(NegMod), PosNegSmallLHS); EXPECT_EQ(PosNegSmallLHS.srem(IntMinMod), PosNegSmallLHS); @@ -1439,9 +1439,9 @@ TEST_F(ConstantRangeTest, Ashr) { APInt(16, (0xaaa >> 0xa) + 1))); EXPECT_EQ(Some.ashr(Wrap), ConstantRange(APInt(16, 0), APInt(16, 0xaaa))); EXPECT_EQ(Wrap.ashr(Wrap), Full); - ConstantRange Neg(APInt(16, 0xf3f0, true), APInt(16, 0xf7f8, true)); - EXPECT_EQ(Neg.ashr(Small), ConstantRange(APInt(16, 0xfffc, true), - APInt(16, 0xfffe, true))); + ConstantRange Neg(APInt(16, 0xf3f0), APInt(16, 0xf7f8)); + EXPECT_EQ(Neg.ashr(Small), + ConstantRange(APInt(16, 0xfffc), APInt(16, 0xfffe))); } TEST(ConstantRange, MakeAllowedICmpRegion) { @@ -1484,23 +1484,23 @@ TEST(ConstantRange, MakeSatisfyingICmpRegion) { UnsignedSample), ConstantRange(APInt(8, 199), APInt(8, 0))); - ConstantRange SignedSample(APInt(8, -5), APInt(8, 5)); + ConstantRange SignedSample(APInt(8, -5, true), APInt(8, 5)); EXPECT_EQ( ConstantRange::makeSatisfyingICmpRegion(ICmpInst::ICMP_SLT, SignedSample), - ConstantRange(APInt(8, -128), APInt(8, -5))); + ConstantRange(APInt(8, -128, true), APInt(8, -5, true))); EXPECT_EQ( ConstantRange::makeSatisfyingICmpRegion(ICmpInst::ICMP_SLE, SignedSample), - ConstantRange(APInt(8, -128), APInt(8, -4))); + ConstantRange(APInt(8, -128, true), APInt(8, -4, true))); EXPECT_EQ( ConstantRange::makeSatisfyingICmpRegion(ICmpInst::ICMP_SGT, SignedSample), - ConstantRange(APInt(8, 5), APInt(8, -128))); + ConstantRange(APInt(8, 5), APInt(8, -128, true))); EXPECT_EQ( ConstantRange::makeSatisfyingICmpRegion(ICmpInst::ICMP_SGE, SignedSample), - ConstantRange(APInt(8, 4), APInt(8, -128))); + ConstantRange(APInt(8, 4), APInt(8, -128, true))); } void ICmpTestImpl(CmpInst::Predicate Pred) { @@ -1522,7 +1522,7 @@ TEST(ConstantRange, ICmp) { } TEST(ConstantRange, MakeGuaranteedNoWrapRegion) { - const int IntMin4Bits = 8; + const int IntMin4Bits = -8; const int IntMax4Bits = 7; typedef OverflowingBinaryOperator OBO; @@ -1631,7 +1631,7 @@ TEST(ConstantRange, MakeGuaranteedNoWrapRegion) { Instruction::Sub, OneToFive, OBO::NoUnsignedWrap), ConstantRange(APInt::getMinValue(32) + 5, APInt::getMinValue(32))); - ConstantRange MinusFiveToMinusTwo(APInt(32, -5), APInt(32, -1)); + ConstantRange MinusFiveToMinusTwo(APInt(32, -5, true), APInt(32, -1, true)); EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion( Instruction::Add, MinusFiveToMinusTwo, OBO::NoSignedWrap), ConstantRange(APInt::getSignedMinValue(32) + 5, @@ -1645,10 +1645,9 @@ TEST(ConstantRange, MakeGuaranteedNoWrapRegion) { APInt::getSignedMaxValue(32) - 4)); EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion( Instruction::Sub, MinusFiveToMinusTwo, OBO::NoUnsignedWrap), - ConstantRange(APInt::getMaxValue(32) - 1, - APInt::getMinValue(32))); + ConstantRange(APInt::getMaxValue(32) - 1, APInt::getMinValue(32))); - ConstantRange MinusOneToOne(APInt(32, -1), APInt(32, 2)); + ConstantRange MinusOneToOne(APInt(32, -1, true), APInt(32, 2)); EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion( Instruction::Add, MinusOneToOne, OBO::NoSignedWrap), ConstantRange(APInt::getSignedMinValue(32) + 1, @@ -1696,7 +1695,7 @@ TEST(ConstantRange, MakeGuaranteedNoWrapRegion) { ConstantRange(APInt(32, 0), APInt(32, 1) + 1)); EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion( Instruction::Shl, UpToBitWidth, OBO::NoSignedWrap), - ConstantRange(APInt(32, -1), APInt(32, 0) + 1)); + ConstantRange(APInt(32, -1, true), APInt(32, 0) + 1)); EXPECT_EQ( ConstantRange::makeGuaranteedNoWrapRegion( @@ -1717,34 +1716,36 @@ TEST(ConstantRange, MakeGuaranteedNoWrapRegion) { Instruction::Shl, IllegalShAmt, OBO::NoSignedWrap), ConstantRange::getFull(32)); - EXPECT_EQ( - ConstantRange::makeGuaranteedNoWrapRegion( - Instruction::Shl, ConstantRange(APInt(32, -32), APInt(32, 16) + 1), - OBO::NoUnsignedWrap), - ConstantRange::makeGuaranteedNoWrapRegion( - Instruction::Shl, ConstantRange(APInt(32, 0), APInt(32, 16) + 1), - OBO::NoUnsignedWrap)); - EXPECT_EQ( - ConstantRange::makeGuaranteedNoWrapRegion( - Instruction::Shl, ConstantRange(APInt(32, -32), APInt(32, 16) + 1), - OBO::NoSignedWrap), - ConstantRange::makeGuaranteedNoWrapRegion( - Instruction::Shl, ConstantRange(APInt(32, 0), APInt(32, 16) + 1), - OBO::NoSignedWrap)); + EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion( + Instruction::Shl, + ConstantRange(APInt(32, -32, true), APInt(32, 16) + 1), + OBO::NoUnsignedWrap), + ConstantRange::makeGuaranteedNoWrapRegion( + Instruction::Shl, + ConstantRange(APInt(32, 0), APInt(32, 16) + 1), + OBO::NoUnsignedWrap)); + EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion( + Instruction::Shl, + ConstantRange(APInt(32, -32, true), APInt(32, 16) + 1), + OBO::NoSignedWrap), + ConstantRange::makeGuaranteedNoWrapRegion( + Instruction::Shl, + ConstantRange(APInt(32, 0), APInt(32, 16) + 1), + OBO::NoSignedWrap)); EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion( Instruction::Shl, - ConstantRange(APInt(32, -32), APInt(32, 16) + 1), + ConstantRange(APInt(32, -32, true), APInt(32, 16) + 1), OBO::NoUnsignedWrap), ConstantRange(APInt(32, 0), APInt(32, 65535) + 1)); EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion( Instruction::Shl, - ConstantRange(APInt(32, -32), APInt(32, 16) + 1), + ConstantRange(APInt(32, -32, true), APInt(32, 16) + 1), OBO::NoSignedWrap), - ConstantRange(APInt(32, -32768), APInt(32, 32767) + 1)); + ConstantRange(APInt(32, -32768, true), APInt(32, 32767) + 1)); } -template +template void TestNoWrapRegionExhaustive(Instruction::BinaryOps BinOp, unsigned NoWrapKind, Fn OverflowFn) { for (unsigned Bits : {1, 5}) { @@ -1909,14 +1910,15 @@ TEST(ConstantRange, GetEquivalentICmp) { EXPECT_EQ(Pred, CmpInst::ICMP_NE); EXPECT_EQ(RHS, APInt(32, 0)); - EXPECT_TRUE(ConstantRange(APInt(32, -1)).getEquivalentICmp(Pred, RHS)); + EXPECT_TRUE(ConstantRange(APInt(32, -1, true)).getEquivalentICmp(Pred, RHS)); EXPECT_EQ(Pred, CmpInst::ICMP_EQ); - EXPECT_EQ(RHS, APInt(32, -1)); + EXPECT_EQ(RHS, APInt(32, -1, true)); - EXPECT_TRUE( - ConstantRange(APInt(32, -1)).inverse().getEquivalentICmp(Pred, RHS)); + EXPECT_TRUE(ConstantRange(APInt(32, -1, true)) + .inverse() + .getEquivalentICmp(Pred, RHS)); EXPECT_EQ(Pred, CmpInst::ICMP_NE); - EXPECT_EQ(RHS, APInt(32, -1)); + EXPECT_EQ(RHS, APInt(32, -1, true)); EnumerateInterestingConstantRanges([](const ConstantRange &CR) { unsigned Bits = CR.getBitWidth(); diff --git a/llvm/unittests/IR/ConstantsTest.cpp b/llvm/unittests/IR/ConstantsTest.cpp index 314264b77a7101..d1bb9a95f7cb4e 100644 --- a/llvm/unittests/IR/ConstantsTest.cpp +++ b/llvm/unittests/IR/ConstantsTest.cpp @@ -24,7 +24,7 @@ namespace { TEST(ConstantsTest, Integer_i1) { LLVMContext Context; IntegerType *Int1 = IntegerType::get(Context, 1); - Constant *One = ConstantInt::get(Int1, 1, true); + Constant *One = ConstantInt::get(Int1, 1); Constant *Zero = ConstantInt::get(Int1, 0); Constant *NegOne = ConstantInt::get(Int1, static_cast(-1), true); EXPECT_EQ(NegOne, ConstantInt::getSigned(Int1, -1)); @@ -102,9 +102,6 @@ TEST(ConstantsTest, IntSigns) { EXPECT_EQ(-50, ConstantInt::get(Int8Ty, 206)->getSExtValue()); EXPECT_EQ(-50, ConstantInt::getSigned(Int8Ty, -50)->getSExtValue()); EXPECT_EQ(206U, ConstantInt::getSigned(Int8Ty, -50)->getZExtValue()); - - // Overflow is handled by truncation. - EXPECT_EQ(0x3b, ConstantInt::get(Int8Ty, 0x13b)->getSExtValue()); } TEST(ConstantsTest, PointerCast) { diff --git a/llvm/unittests/IR/InstructionsTest.cpp b/llvm/unittests/IR/InstructionsTest.cpp index b03f45e571238a..cbd121e4cedf62 100644 --- a/llvm/unittests/IR/InstructionsTest.cpp +++ b/llvm/unittests/IR/InstructionsTest.cpp @@ -51,7 +51,7 @@ TEST(InstructionsTest, ReturnInst) { EXPECT_EQ(r0->op_begin(), r0->op_end()); IntegerType* Int1 = IntegerType::get(C, 1); - Constant* One = ConstantInt::get(Int1, 1, true); + Constant* One = ConstantInt::get(Int1, 1); const ReturnInst* r1 = ReturnInst::Create(C, One); EXPECT_EQ(1U, r1->getNumOperands()); User::const_op_iterator b(r1->op_begin()); @@ -150,7 +150,7 @@ TEST(InstructionsTest, BranchInst) { EXPECT_EQ(b0->op_end(), std::next(b0->op_begin())); IntegerType* Int1 = IntegerType::get(C, 1); - Constant* One = ConstantInt::get(Int1, 1, true); + Constant* One = ConstantInt::get(Int1, 1); // Conditional BranchInst BranchInst* b1 = BranchInst::Create(bb0, bb1, One); diff --git a/llvm/unittests/IR/MetadataTest.cpp b/llvm/unittests/IR/MetadataTest.cpp index 767dd1a59d2b9e..3a2644b5c9568d 100644 --- a/llvm/unittests/IR/MetadataTest.cpp +++ b/llvm/unittests/IR/MetadataTest.cpp @@ -3517,16 +3517,18 @@ TEST_F(DIExpressionTest, foldConstant) { DIExpression *Expr; DIExpression *NewExpr; -#define EXPECT_FOLD_CONST(StartWidth, StartValue, EndWidth, EndValue, NumElts) \ - Int = ConstantInt::get(Context, APInt(StartWidth, StartValue)); \ - std::tie(NewExpr, NewInt) = Expr->constantFold(Int); \ - ASSERT_EQ(NewInt->getBitWidth(), EndWidth##u); \ - EXPECT_EQ(NewInt->getValue(), APInt(EndWidth, EndValue)); \ +#define EXPECT_FOLD_CONST(StartWidth, StartValue, StartIsSigned, EndWidth, \ + EndValue, EndIsSigned, NumElts) \ + Int = \ + ConstantInt::get(Context, APInt(StartWidth, StartValue, StartIsSigned)); \ + std::tie(NewExpr, NewInt) = Expr->constantFold(Int); \ + ASSERT_EQ(NewInt->getBitWidth(), EndWidth##u); \ + EXPECT_EQ(NewInt->getValue(), APInt(EndWidth, EndValue, EndIsSigned)); \ EXPECT_EQ(NewExpr->getNumElements(), NumElts##u) // Unfoldable expression should return the original unmodified Int/Expr. Expr = DIExpression::get(Context, {dwarf::DW_OP_deref}); - EXPECT_FOLD_CONST(32, 117, 32, 117, 1); + EXPECT_FOLD_CONST(32, 117, false, 32, 117, false, 1); EXPECT_EQ(NewExpr, Expr); EXPECT_EQ(NewInt, Int); EXPECT_TRUE(NewExpr->startsWithDeref()); @@ -3534,18 +3536,18 @@ TEST_F(DIExpressionTest, foldConstant) { // One unsigned bit-width conversion. Expr = DIExpression::get( Context, {dwarf::DW_OP_LLVM_convert, 72, dwarf::DW_ATE_unsigned}); - EXPECT_FOLD_CONST(8, 12, 72, 12, 0); + EXPECT_FOLD_CONST(8, 12, false, 72, 12, false, 0); // Two unsigned bit-width conversions (mask truncation). Expr = DIExpression::get( Context, {dwarf::DW_OP_LLVM_convert, 8, dwarf::DW_ATE_unsigned, dwarf::DW_OP_LLVM_convert, 16, dwarf::DW_ATE_unsigned}); - EXPECT_FOLD_CONST(32, -1, 16, 0xff, 0); + EXPECT_FOLD_CONST(32, -1, true, 16, 0xff, false, 0); // Sign extension. Expr = DIExpression::get( Context, {dwarf::DW_OP_LLVM_convert, 32, dwarf::DW_ATE_signed}); - EXPECT_FOLD_CONST(16, -1, 32, -1, 0); + EXPECT_FOLD_CONST(16, -1, true, 32, -1, true, 0); // Get non-foldable operations back in the new Expr. uint64_t Elements[] = {dwarf::DW_OP_deref, dwarf::DW_OP_stack_value}; @@ -3554,7 +3556,7 @@ TEST_F(DIExpressionTest, foldConstant) { Context, {dwarf::DW_OP_LLVM_convert, 32, dwarf::DW_ATE_signed}); Expr = DIExpression::append(Expr, Expected); ASSERT_EQ(Expr->getNumElements(), 5u); - EXPECT_FOLD_CONST(16, -1, 32, -1, 2); + EXPECT_FOLD_CONST(16, -1, true, 32, -1, true, 2); EXPECT_EQ(NewExpr->getElements(), Expected); #undef EXPECT_FOLD_CONST diff --git a/llvm/unittests/IR/PatternMatch.cpp b/llvm/unittests/IR/PatternMatch.cpp index 9e9e41b8fbad0d..ea9c2e13bec701 100644 --- a/llvm/unittests/IR/PatternMatch.cpp +++ b/llvm/unittests/IR/PatternMatch.cpp @@ -71,7 +71,7 @@ TEST_F(PatternMatchTest, SpecificIntEQ) { Value *Zero = ConstantInt::get(IntTy, 0); Value *One = ConstantInt::get(IntTy, 1); - Value *NegOne = ConstantInt::get(IntTy, -1); + Value *NegOne = Constant::getAllOnesValue(IntTy); EXPECT_TRUE( m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ, APInt(BitWidth, 0)) @@ -93,15 +93,15 @@ TEST_F(PatternMatchTest, SpecificIntEQ) { m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ, APInt(BitWidth, 1)) .match(NegOne)); - EXPECT_FALSE( - m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ, APInt(BitWidth, -1)) - .match(Zero)); - EXPECT_FALSE( - m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ, APInt(BitWidth, -1)) - .match(One)); - EXPECT_TRUE( - m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ, APInt(BitWidth, -1)) - .match(NegOne)); + EXPECT_FALSE(m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ, + APInt::getAllOnes(BitWidth)) + .match(Zero)); + EXPECT_FALSE(m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ, + APInt::getAllOnes(BitWidth)) + .match(One)); + EXPECT_TRUE(m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ, + APInt::getAllOnes(BitWidth)) + .match(NegOne)); } TEST_F(PatternMatchTest, SpecificIntNE) { @@ -110,7 +110,7 @@ TEST_F(PatternMatchTest, SpecificIntNE) { Value *Zero = ConstantInt::get(IntTy, 0); Value *One = ConstantInt::get(IntTy, 1); - Value *NegOne = ConstantInt::get(IntTy, -1); + Value *NegOne = Constant::getAllOnesValue(IntTy); EXPECT_FALSE( m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE, APInt(BitWidth, 0)) @@ -132,15 +132,15 @@ TEST_F(PatternMatchTest, SpecificIntNE) { m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE, APInt(BitWidth, 1)) .match(NegOne)); - EXPECT_TRUE( - m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE, APInt(BitWidth, -1)) - .match(Zero)); - EXPECT_TRUE( - m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE, APInt(BitWidth, -1)) - .match(One)); - EXPECT_FALSE( - m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE, APInt(BitWidth, -1)) - .match(NegOne)); + EXPECT_TRUE(m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE, + APInt::getAllOnes(BitWidth)) + .match(Zero)); + EXPECT_TRUE(m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE, + APInt::getAllOnes(BitWidth)) + .match(One)); + EXPECT_FALSE(m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE, + APInt::getAllOnes(BitWidth)) + .match(NegOne)); } TEST_F(PatternMatchTest, SpecificIntUGT) { @@ -149,7 +149,7 @@ TEST_F(PatternMatchTest, SpecificIntUGT) { Value *Zero = ConstantInt::get(IntTy, 0); Value *One = ConstantInt::get(IntTy, 1); - Value *NegOne = ConstantInt::get(IntTy, -1); + Value *NegOne = Constant::getAllOnesValue(IntTy); EXPECT_FALSE( m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT, APInt(BitWidth, 0)) @@ -171,15 +171,15 @@ TEST_F(PatternMatchTest, SpecificIntUGT) { m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT, APInt(BitWidth, 1)) .match(NegOne)); - EXPECT_FALSE( - m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT, APInt(BitWidth, -1)) - .match(Zero)); - EXPECT_FALSE( - m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT, APInt(BitWidth, -1)) - .match(One)); - EXPECT_FALSE( - m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT, APInt(BitWidth, -1)) - .match(NegOne)); + EXPECT_FALSE(m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT, + APInt::getAllOnes(BitWidth)) + .match(Zero)); + EXPECT_FALSE(m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT, + APInt::getAllOnes(BitWidth)) + .match(One)); + EXPECT_FALSE(m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT, + APInt::getAllOnes(BitWidth)) + .match(NegOne)); } TEST_F(PatternMatchTest, SignbitZeroChecks) { @@ -187,7 +187,7 @@ TEST_F(PatternMatchTest, SignbitZeroChecks) { Value *Zero = ConstantInt::get(IntTy, 0); Value *One = ConstantInt::get(IntTy, 1); - Value *NegOne = ConstantInt::get(IntTy, -1); + Value *NegOne = Constant::getAllOnesValue(IntTy); EXPECT_TRUE(m_Negative().match(NegOne)); EXPECT_FALSE(m_NonNegative().match(NegOne)); @@ -211,7 +211,7 @@ TEST_F(PatternMatchTest, SpecificIntUGE) { Value *Zero = ConstantInt::get(IntTy, 0); Value *One = ConstantInt::get(IntTy, 1); - Value *NegOne = ConstantInt::get(IntTy, -1); + Value *NegOne = Constant::getAllOnesValue(IntTy); EXPECT_TRUE( m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, APInt(BitWidth, 0)) @@ -233,15 +233,15 @@ TEST_F(PatternMatchTest, SpecificIntUGE) { m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, APInt(BitWidth, 1)) .match(NegOne)); - EXPECT_FALSE( - m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, APInt(BitWidth, -1)) - .match(Zero)); - EXPECT_FALSE( - m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, APInt(BitWidth, -1)) - .match(One)); - EXPECT_TRUE( - m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, APInt(BitWidth, -1)) - .match(NegOne)); + EXPECT_FALSE(m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, + APInt::getAllOnes(BitWidth)) + .match(Zero)); + EXPECT_FALSE(m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, + APInt::getAllOnes(BitWidth)) + .match(One)); + EXPECT_TRUE(m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, + APInt::getAllOnes(BitWidth)) + .match(NegOne)); } TEST_F(PatternMatchTest, SpecificIntULT) { @@ -250,7 +250,7 @@ TEST_F(PatternMatchTest, SpecificIntULT) { Value *Zero = ConstantInt::get(IntTy, 0); Value *One = ConstantInt::get(IntTy, 1); - Value *NegOne = ConstantInt::get(IntTy, -1); + Value *NegOne = Constant::getAllOnesValue(IntTy); EXPECT_FALSE( m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, APInt(BitWidth, 0)) @@ -272,15 +272,15 @@ TEST_F(PatternMatchTest, SpecificIntULT) { m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, APInt(BitWidth, 1)) .match(NegOne)); - EXPECT_TRUE( - m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, APInt(BitWidth, -1)) - .match(Zero)); - EXPECT_TRUE( - m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, APInt(BitWidth, -1)) - .match(One)); - EXPECT_FALSE( - m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, APInt(BitWidth, -1)) - .match(NegOne)); + EXPECT_TRUE(m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, + APInt::getAllOnes(BitWidth)) + .match(Zero)); + EXPECT_TRUE(m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, + APInt::getAllOnes(BitWidth)) + .match(One)); + EXPECT_FALSE(m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, + APInt::getAllOnes(BitWidth)) + .match(NegOne)); } TEST_F(PatternMatchTest, SpecificIntULE) { @@ -289,7 +289,7 @@ TEST_F(PatternMatchTest, SpecificIntULE) { Value *Zero = ConstantInt::get(IntTy, 0); Value *One = ConstantInt::get(IntTy, 1); - Value *NegOne = ConstantInt::get(IntTy, -1); + Value *NegOne = Constant::getAllOnesValue(IntTy); EXPECT_TRUE( m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, APInt(BitWidth, 0)) @@ -311,15 +311,15 @@ TEST_F(PatternMatchTest, SpecificIntULE) { m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, APInt(BitWidth, 1)) .match(NegOne)); - EXPECT_TRUE( - m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, APInt(BitWidth, -1)) - .match(Zero)); - EXPECT_TRUE( - m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, APInt(BitWidth, -1)) - .match(One)); - EXPECT_TRUE( - m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, APInt(BitWidth, -1)) - .match(NegOne)); + EXPECT_TRUE(m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, + APInt::getAllOnes(BitWidth)) + .match(Zero)); + EXPECT_TRUE(m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, + APInt::getAllOnes(BitWidth)) + .match(One)); + EXPECT_TRUE(m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, + APInt::getAllOnes(BitWidth)) + .match(NegOne)); } TEST_F(PatternMatchTest, SpecificIntSGT) { @@ -328,7 +328,7 @@ TEST_F(PatternMatchTest, SpecificIntSGT) { Value *Zero = ConstantInt::get(IntTy, 0); Value *One = ConstantInt::get(IntTy, 1); - Value *NegOne = ConstantInt::get(IntTy, -1); + Value *NegOne = Constant::getAllOnesValue(IntTy); EXPECT_FALSE( m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, APInt(BitWidth, 0)) @@ -350,15 +350,15 @@ TEST_F(PatternMatchTest, SpecificIntSGT) { m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, APInt(BitWidth, 1)) .match(NegOne)); - EXPECT_TRUE( - m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, APInt(BitWidth, -1)) - .match(Zero)); - EXPECT_TRUE( - m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, APInt(BitWidth, -1)) - .match(One)); - EXPECT_FALSE( - m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, APInt(BitWidth, -1)) - .match(NegOne)); + EXPECT_TRUE(m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, + APInt::getAllOnes(BitWidth)) + .match(Zero)); + EXPECT_TRUE(m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, + APInt::getAllOnes(BitWidth)) + .match(One)); + EXPECT_FALSE(m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, + APInt::getAllOnes(BitWidth)) + .match(NegOne)); } TEST_F(PatternMatchTest, SpecificIntSGE) { @@ -367,7 +367,7 @@ TEST_F(PatternMatchTest, SpecificIntSGE) { Value *Zero = ConstantInt::get(IntTy, 0); Value *One = ConstantInt::get(IntTy, 1); - Value *NegOne = ConstantInt::get(IntTy, -1); + Value *NegOne = Constant::getAllOnesValue(IntTy); EXPECT_TRUE( m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, APInt(BitWidth, 0)) @@ -389,15 +389,15 @@ TEST_F(PatternMatchTest, SpecificIntSGE) { m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, APInt(BitWidth, 1)) .match(NegOne)); - EXPECT_TRUE( - m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, APInt(BitWidth, -1)) - .match(Zero)); - EXPECT_TRUE( - m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, APInt(BitWidth, -1)) - .match(One)); - EXPECT_TRUE( - m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, APInt(BitWidth, -1)) - .match(NegOne)); + EXPECT_TRUE(m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, + APInt::getAllOnes(BitWidth)) + .match(Zero)); + EXPECT_TRUE(m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, + APInt::getAllOnes(BitWidth)) + .match(One)); + EXPECT_TRUE(m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, + APInt::getAllOnes(BitWidth)) + .match(NegOne)); } TEST_F(PatternMatchTest, SpecificIntSLT) { @@ -406,7 +406,7 @@ TEST_F(PatternMatchTest, SpecificIntSLT) { Value *Zero = ConstantInt::get(IntTy, 0); Value *One = ConstantInt::get(IntTy, 1); - Value *NegOne = ConstantInt::get(IntTy, -1); + Value *NegOne = Constant::getAllOnesValue(IntTy); EXPECT_FALSE( m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, APInt(BitWidth, 0)) @@ -428,15 +428,15 @@ TEST_F(PatternMatchTest, SpecificIntSLT) { m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, APInt(BitWidth, 1)) .match(NegOne)); - EXPECT_FALSE( - m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, APInt(BitWidth, -1)) - .match(Zero)); - EXPECT_FALSE( - m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, APInt(BitWidth, -1)) - .match(One)); - EXPECT_FALSE( - m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, APInt(BitWidth, -1)) - .match(NegOne)); + EXPECT_FALSE(m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, + APInt::getAllOnes(BitWidth)) + .match(Zero)); + EXPECT_FALSE(m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, + APInt::getAllOnes(BitWidth)) + .match(One)); + EXPECT_FALSE(m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, + APInt::getAllOnes(BitWidth)) + .match(NegOne)); } TEST_F(PatternMatchTest, SpecificIntSLE) { @@ -445,7 +445,7 @@ TEST_F(PatternMatchTest, SpecificIntSLE) { Value *Zero = ConstantInt::get(IntTy, 0); Value *One = ConstantInt::get(IntTy, 1); - Value *NegOne = ConstantInt::get(IntTy, -1); + Value *NegOne = Constant::getAllOnesValue(IntTy); EXPECT_TRUE( m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, APInt(BitWidth, 0)) @@ -467,15 +467,15 @@ TEST_F(PatternMatchTest, SpecificIntSLE) { m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, APInt(BitWidth, 1)) .match(NegOne)); - EXPECT_FALSE( - m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, APInt(BitWidth, -1)) - .match(Zero)); - EXPECT_FALSE( - m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, APInt(BitWidth, -1)) - .match(One)); - EXPECT_TRUE( - m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, APInt(BitWidth, -1)) - .match(NegOne)); + EXPECT_FALSE(m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, + APInt::getAllOnes(BitWidth)) + .match(Zero)); + EXPECT_FALSE(m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, + APInt::getAllOnes(BitWidth)) + .match(One)); + EXPECT_TRUE(m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, + APInt::getAllOnes(BitWidth)) + .match(NegOne)); } TEST_F(PatternMatchTest, Unless) { diff --git a/llvm/unittests/Support/DivisionByConstantTest.cpp b/llvm/unittests/Support/DivisionByConstantTest.cpp index 2b17f98bb75b2f..715dded68ff017 100644 --- a/llvm/unittests/Support/DivisionByConstantTest.cpp +++ b/llvm/unittests/Support/DivisionByConstantTest.cpp @@ -32,7 +32,7 @@ APInt SignedDivideUsingMagic(APInt Numerator, APInt Divisor, unsigned Bits = Numerator.getBitWidth(); APInt Factor(Bits, 0); - APInt ShiftMask(Bits, -1); + APInt ShiftMask(Bits, -1, true); if (Divisor.isOne() || Divisor.isAllOnes()) { // If d is +1/-1, we just multiply the numerator by +1/-1. Factor = Divisor.getSExtValue();