llvm.org GIT mirror llvm / 40f8f62
PR5207: Change APInt methods trunc(), sext(), zext(), sextOrTrunc() and zextOrTrunc(), and APSInt methods extend(), extOrTrunc() and new method trunc(), to be const and to return a new value instead of modifying the object in place. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@121120 91177308-0d34-0410-b5e6-96231b3b80d8 Jay Foad 9 years ago
25 changed file(s) with 227 addition(s) and 259 deletion(s). Raw diff Collapse all Expand all
341341
342342 if (isSingleWord())
343343 return isUIntN(N, VAL);
344 APInt Tmp(N, getNumWords(), pVal);
345 Tmp.zext(getBitWidth());
346 return Tmp == (*this);
344 return APInt(N, getNumWords(), pVal).zext(getBitWidth()) == (*this);
347345 }
348346
349347 /// @brief Check if this APInt has an N-bits signed integer value.
10121010 /// Truncate the APInt to a specified width. It is an error to specify a width
10131011 /// that is greater than or equal to the current width.
10141012 /// @brief Truncate to new width.
1015 APInt &trunc(unsigned width);
1013 APInt trunc(unsigned width) const;
10161014
10171015 /// This operation sign extends the APInt to a new width. If the high order
10181016 /// bit is set, the fill on the left will be done with 1 bits, otherwise zero.
10191017 /// It is an error to specify a width that is less than or equal to the
10201018 /// current width.
10211019 /// @brief Sign extend to a new width.
1022 APInt &sext(unsigned width);
1020 APInt sext(unsigned width) const;
10231021
10241022 /// This operation zero extends the APInt to a new width. The high order bits
10251023 /// are filled with 0 bits. It is an error to specify a width that is less
10261024 /// than or equal to the current width.
10271025 /// @brief Zero extend to a new width.
1028 APInt &zext(unsigned width);
1026 APInt zext(unsigned width) const;
10291027
10301028 /// Make this APInt have the bit width given by \p width. The value is sign
10311029 /// extended, truncated, or left alone to make it that width.
10321030 /// @brief Sign extend or truncate to width
1033 APInt &sextOrTrunc(unsigned width);
1031 APInt sextOrTrunc(unsigned width) const;
10341032
10351033 /// Make this APInt have the bit width given by \p width. The value is zero
10361034 /// extended, truncated, or left alone to make it that width.
10371035 /// @brief Zero extend or truncate to width
1038 APInt &zextOrTrunc(unsigned width);
1036 APInt zextOrTrunc(unsigned width) const;
10391037
10401038 /// @}
10411039 /// @name Bit Manipulation Operators
6767 }
6868 using APInt::toString;
6969
70 APSInt& extend(uint32_t width) {
70 APSInt trunc(uint32_t width) const {
71 return APSInt(APInt::trunc(width), IsUnsigned);
72 }
73
74 APSInt extend(uint32_t width) const {
7175 if (IsUnsigned)
72 zext(width);
76 return APSInt(zext(width), IsUnsigned);
7377 else
74 sext(width);
75 return *this;
76 }
77
78 APSInt& extOrTrunc(uint32_t width) {
78 return APSInt(sext(width), IsUnsigned);
79 }
80
81 APSInt extOrTrunc(uint32_t width) const {
7982 if (IsUnsigned)
80 zextOrTrunc(width);
83 return APSInt(zextOrTrunc(width), IsUnsigned);
8184 else
82 sextOrTrunc(width);
83 return *this;
85 return APSInt(sextOrTrunc(width), IsUnsigned);
8486 }
8587
8688 const APSInt &operator%=(const APSInt &RHS) {
205205 Value *CastOp = cast(V)->getOperand(0);
206206 unsigned OldWidth = Scale.getBitWidth();
207207 unsigned SmallWidth = CastOp->getType()->getPrimitiveSizeInBits();
208 Scale.trunc(SmallWidth);
209 Offset.trunc(SmallWidth);
208 Scale = Scale.trunc(SmallWidth);
209 Offset = Offset.trunc(SmallWidth);
210210 Extension = isa(V) ? EK_SignExt : EK_ZeroExt;
211211
212212 Value *Result = GetLinearExpression(CastOp, Scale, Offset, Extension,
213213 TD, Depth+1);
214 Scale.zext(OldWidth);
215 Offset.zext(OldWidth);
214 Scale = Scale.zext(OldWidth);
215 Offset = Offset.zext(OldWidth);
216216
217217 return Result;
218218 }
609609 APInt BasePtr(BitWidth, 0);
610610 if (ConstantExpr *CE = dyn_cast(Ptr))
611611 if (CE->getOpcode() == Instruction::IntToPtr)
612 if (ConstantInt *Base = dyn_cast(CE->getOperand(0))) {
613 BasePtr = Base->getValue();
614 BasePtr.zextOrTrunc(BitWidth);
615 }
612 if (ConstantInt *Base = dyn_cast(CE->getOperand(0)))
613 BasePtr = Base->getValue().zextOrTrunc(BitWidth);
616614 if (Ptr->isNullValue() || BasePtr != 0) {
617615 Constant *C = ConstantInt::get(Ptr->getContext(), Offset+BasePtr);
618616 return ConstantExpr::getIntToPtr(C, ResultTy);
33663366 // If C is a single bit, it may be in the sign-bit position
33673367 // before the zero-extend. In this case, represent the xor
33683368 // using an add, which is equivalent, and re-apply the zext.
3369 APInt Trunc = APInt(CI->getValue()).trunc(Z0TySize);
3370 if (APInt(Trunc).zext(getTypeSizeInBits(UTy)) == CI->getValue() &&
3369 APInt Trunc = CI->getValue().trunc(Z0TySize);
3370 if (Trunc.zext(getTypeSizeInBits(UTy)) == CI->getValue() &&
33713371 Trunc.isSignBit())
33723372 return getZeroExtendExpr(getAddExpr(Z0, getConstant(Trunc)),
33733373 UTy);
254254 else
255255 SrcBitWidth = SrcTy->getScalarSizeInBits();
256256
257 APInt MaskIn(Mask);
258 MaskIn.zextOrTrunc(SrcBitWidth);
259 KnownZero.zextOrTrunc(SrcBitWidth);
260 KnownOne.zextOrTrunc(SrcBitWidth);
257 APInt MaskIn = Mask.zextOrTrunc(SrcBitWidth);
258 KnownZero = KnownZero.zextOrTrunc(SrcBitWidth);
259 KnownOne = KnownOne.zextOrTrunc(SrcBitWidth);
261260 ComputeMaskedBits(I->getOperand(0), MaskIn, KnownZero, KnownOne, TD,
262261 Depth+1);
263 KnownZero.zextOrTrunc(BitWidth);
264 KnownOne.zextOrTrunc(BitWidth);
262 KnownZero = KnownZero.zextOrTrunc(BitWidth);
263 KnownOne = KnownOne.zextOrTrunc(BitWidth);
265264 // Any top bits are known to be zero.
266265 if (BitWidth > SrcBitWidth)
267266 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth);
283282 // Compute the bits in the result that are not present in the input.
284283 unsigned SrcBitWidth = I->getOperand(0)->getType()->getScalarSizeInBits();
285284
286 APInt MaskIn(Mask);
287 MaskIn.trunc(SrcBitWidth);
288 KnownZero.trunc(SrcBitWidth);
289 KnownOne.trunc(SrcBitWidth);
285 APInt MaskIn = Mask.trunc(SrcBitWidth);
286 KnownZero = KnownZero.trunc(SrcBitWidth);
287 KnownOne = KnownOne.trunc(SrcBitWidth);
290288 ComputeMaskedBits(I->getOperand(0), MaskIn, KnownZero, KnownOne, TD,
291289 Depth+1);
292290 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
293 KnownZero.zext(BitWidth);
294 KnownOne.zext(BitWidth);
291 KnownZero = KnownZero.zext(BitWidth);
292 KnownOne = KnownOne.zext(BitWidth);
295293
296294 // If the sign bit of the input is known set or clear, then we know the
297295 // top bits of the result.
681681 APInt Tmp(bits, StringRef(TokStart+3, len), 16);
682682 uint32_t activeBits = Tmp.getActiveBits();
683683 if (activeBits > 0 && activeBits < bits)
684 Tmp.trunc(activeBits);
684 Tmp = Tmp.trunc(activeBits);
685685 APSIntVal = APSInt(Tmp, TokStart[0] == 'u');
686686 return lltok::APSInt;
687687 }
808808 if (TokStart[0] == '-') {
809809 uint32_t minBits = Tmp.getMinSignedBits();
810810 if (minBits > 0 && minBits < numBits)
811 Tmp.trunc(minBits);
811 Tmp = Tmp.trunc(minBits);
812812 APSIntVal = APSInt(Tmp, false);
813813 } else {
814814 uint32_t activeBits = Tmp.getActiveBits();
815815 if (activeBits > 0 && activeBits < numBits)
816 Tmp.trunc(activeBits);
816 Tmp = Tmp.trunc(activeBits);
817817 APSIntVal = APSInt(Tmp, true);
818818 }
819819 return lltok::APSInt;
25742574 case ValID::t_APSInt:
25752575 if (!Ty->isIntegerTy())
25762576 return Error(ID.Loc, "integer constant must have integer type");
2577 ID.APSIntVal.extOrTrunc(Ty->getPrimitiveSizeInBits());
2577 ID.APSIntVal = ID.APSIntVal.extOrTrunc(Ty->getPrimitiveSizeInBits());
25782578 V = ConstantInt::get(Context, ID.APSIntVal);
25792579 return false;
25802580 case ValID::t_APFloat:
21322132 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
21332133 SDValue N0Op0 = N0.getOperand(0);
21342134 APInt Mask = ~N1C->getAPIntValue();
2135 Mask.trunc(N0Op0.getValueSizeInBits());
2135 Mask = Mask.trunc(N0Op0.getValueSizeInBits());
21362136 if (DAG.MaskedValueIsZero(N0Op0, Mask)) {
21372137 SDValue Zext = DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(),
21382138 N0.getValueType(), N0Op0);
28652865 EVT TruncVT = N1.getValueType();
28662866 SDValue N100 = N1.getOperand(0).getOperand(0);
28672867 APInt TruncC = N101C->getAPIntValue();
2868 TruncC.trunc(TruncVT.getSizeInBits());
2868 TruncC = TruncC.trunc(TruncVT.getSizeInBits());
28692869 return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0,
28702870 DAG.getNode(ISD::AND, N->getDebugLoc(), TruncVT,
28712871 DAG.getNode(ISD::TRUNCATE,
30213021 EVT TruncVT = N1.getValueType();
30223022 SDValue N100 = N1.getOperand(0).getOperand(0);
30233023 APInt TruncC = N101C->getAPIntValue();
3024 TruncC.trunc(TruncVT.getScalarType().getSizeInBits());
3024 TruncC = TruncC.trunc(TruncVT.getScalarType().getSizeInBits());
30253025 return DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, N0,
30263026 DAG.getNode(ISD::AND, N->getDebugLoc(),
30273027 TruncVT,
31623162 EVT TruncVT = N1.getValueType();
31633163 SDValue N100 = N1.getOperand(0).getOperand(0);
31643164 APInt TruncC = N101C->getAPIntValue();
3165 TruncC.trunc(TruncVT.getSizeInBits());
3165 TruncC = TruncC.trunc(TruncVT.getSizeInBits());
31663166 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0,
31673167 DAG.getNode(ISD::AND, N->getDebugLoc(),
31683168 TruncVT,
37053705 X = DAG.getNode(ISD::TRUNCATE, X.getDebugLoc(), VT, X);
37063706 }
37073707 APInt Mask = cast(N0.getOperand(1))->getAPIntValue();
3708 Mask.zext(VT.getSizeInBits());
3708 Mask = Mask.zext(VT.getSizeInBits());
37093709 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
37103710 X, DAG.getConstant(Mask, VT));
37113711 }
39063906 X = DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, X);
39073907 }
39083908 APInt Mask = cast(N0.getOperand(1))->getAPIntValue();
3909 Mask.zext(VT.getSizeInBits());
3909 Mask = Mask.zext(VT.getSizeInBits());
39103910 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
39113911 X, DAG.getConstant(Mask, VT));
39123912 }
46134613 if (Op.getOpcode() == ISD::UNDEF) continue;
46144614 EltIsUndef = false;
46154615
4616 NewBits |= APInt(cast(Op)->getAPIntValue()).
4616 NewBits |= cast(Op)->getAPIntValue().
46174617 zextOrTrunc(SrcBitSize).zext(DstBitSize);
46184618 }
46194619
46434643 continue;
46444644 }
46454645
4646 APInt OpVal = APInt(cast(BV->getOperand(i))->
4647 getAPIntValue()).zextOrTrunc(SrcBitSize);
4646 APInt OpVal = cast(BV->getOperand(i))->
4647 getAPIntValue().zextOrTrunc(SrcBitSize);
46484648
46494649 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
4650 APInt ThisVal = APInt(OpVal).trunc(DstBitSize);
4650 APInt ThisVal = OpVal.trunc(DstBitSize);
46514651 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
4652 if (isS2V && i == 0 && j == 0 && APInt(ThisVal).zext(SrcBitSize) == OpVal)
4652 if (isS2V && i == 0 && j == 0 && ThisVal.zext(SrcBitSize) == OpVal)
46534653 // Simply turn this into a SCALAR_TO_VECTOR of the new type.
46544654 return DAG.getNode(ISD::SCALAR_TO_VECTOR, BV->getDebugLoc(), VT,
46554655 Ops[0]);
748748 // stores. If the target supports neither 32- nor 64-bits, this
749749 // xform is certainly not worth it.
750750 const APInt &IntVal =CFP->getValueAPF().bitcastToAPInt();
751 SDValue Lo = DAG.getConstant(APInt(IntVal).trunc(32), MVT::i32);
751 SDValue Lo = DAG.getConstant(IntVal.trunc(32), MVT::i32);
752752 SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), MVT::i32);
753753 if (TLI.isBigEndian()) std::swap(Lo, Hi);
754754
14341434 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
14351435 unsigned NBitWidth = NVT.getSizeInBits();
14361436 const APInt &Cst = cast(N)->getAPIntValue();
1437 Lo = DAG.getConstant(APInt(Cst).trunc(NBitWidth), NVT);
1437 Lo = DAG.getConstant(Cst.trunc(NBitWidth), NVT);
14381438 Hi = DAG.getConstant(Cst.lshr(NBitWidth).trunc(NBitWidth), NVT);
14391439 }
14401440
18051805
18061806 // If the sign extended bits are demanded, we know that the sign
18071807 // bit is demanded.
1808 InSignBit.zext(BitWidth);
1808 InSignBit = InSignBit.zext(BitWidth);
18091809 if (NewBits.getBoolValue())
18101810 InputDemandedBits |= InSignBit;
18111811
18481848 EVT InVT = Op.getOperand(0).getValueType();
18491849 unsigned InBits = InVT.getScalarType().getSizeInBits();
18501850 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1851 APInt InMask = Mask;
1852 InMask.trunc(InBits);
1853 KnownZero.trunc(InBits);
1854 KnownOne.trunc(InBits);
1851 APInt InMask = Mask.trunc(InBits);
1852 KnownZero = KnownZero.trunc(InBits);
1853 KnownOne = KnownOne.trunc(InBits);
18551854 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
1856 KnownZero.zext(BitWidth);
1857 KnownOne.zext(BitWidth);
1855 KnownZero = KnownZero.zext(BitWidth);
1856 KnownOne = KnownOne.zext(BitWidth);
18581857 KnownZero |= NewBits;
18591858 return;
18601859 }
18631862 unsigned InBits = InVT.getScalarType().getSizeInBits();
18641863 APInt InSignBit = APInt::getSignBit(InBits);
18651864 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1866 APInt InMask = Mask;
1867 InMask.trunc(InBits);
1865 APInt InMask = Mask.trunc(InBits);
18681866
18691867 // If any of the sign extended bits are demanded, we know that the sign
18701868 // bit is demanded. Temporarily set this bit in the mask for our callee.
18711869 if (NewBits.getBoolValue())
18721870 InMask |= InSignBit;
18731871
1874 KnownZero.trunc(InBits);
1875 KnownOne.trunc(InBits);
1872 KnownZero = KnownZero.trunc(InBits);
1873 KnownOne = KnownOne.trunc(InBits);
18761874 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
18771875
18781876 // Note if the sign bit is known to be zero or one.
18841882 // If the sign bit wasn't actually demanded by our caller, we don't
18851883 // want it set in the KnownZero and KnownOne result values. Reset the
18861884 // mask and reapply it to the result values.
1887 InMask = Mask;
1888 InMask.trunc(InBits);
1885 InMask = Mask.trunc(InBits);
18891886 KnownZero &= InMask;
18901887 KnownOne &= InMask;
18911888
1892 KnownZero.zext(BitWidth);
1893 KnownOne.zext(BitWidth);
1889 KnownZero = KnownZero.zext(BitWidth);
1890 KnownOne = KnownOne.zext(BitWidth);
18941891
18951892 // If the sign bit is known zero or one, the top bits match.
18961893 if (SignBitKnownZero)
19021899 case ISD::ANY_EXTEND: {
19031900 EVT InVT = Op.getOperand(0).getValueType();
19041901 unsigned InBits = InVT.getScalarType().getSizeInBits();
1905 APInt InMask = Mask;
1906 InMask.trunc(InBits);
1907 KnownZero.trunc(InBits);
1908 KnownOne.trunc(InBits);
1902 APInt InMask = Mask.trunc(InBits);
1903 KnownZero = KnownZero.trunc(InBits);
1904 KnownOne = KnownOne.trunc(InBits);
19091905 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
1910 KnownZero.zext(BitWidth);
1911 KnownOne.zext(BitWidth);
1906 KnownZero = KnownZero.zext(BitWidth);
1907 KnownOne = KnownOne.zext(BitWidth);
19121908 return;
19131909 }
19141910 case ISD::TRUNCATE: {
19151911 EVT InVT = Op.getOperand(0).getValueType();
19161912 unsigned InBits = InVT.getScalarType().getSizeInBits();
1917 APInt InMask = Mask;
1918 InMask.zext(InBits);
1919 KnownZero.zext(InBits);
1920 KnownOne.zext(InBits);
1913 APInt InMask = Mask.zext(InBits);
1914 KnownZero = KnownZero.zext(InBits);
1915 KnownOne = KnownOne.zext(InBits);
19211916 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
19221917 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1923 KnownZero.trunc(BitWidth);
1924 KnownOne.trunc(BitWidth);
1918 KnownZero = KnownZero.trunc(BitWidth);
1919 KnownOne = KnownOne.trunc(BitWidth);
19251920 break;
19261921 }
19271922 case ISD::AssertZext: {
23482343 switch (Opcode) {
23492344 default: break;
23502345 case ISD::SIGN_EXTEND:
2351 return getConstant(APInt(Val).sextOrTrunc(VT.getSizeInBits()), VT);
2346 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), VT);
23522347 case ISD::ANY_EXTEND:
23532348 case ISD::ZERO_EXTEND:
23542349 case ISD::TRUNCATE:
2355 return getConstant(APInt(Val).zextOrTrunc(VT.getSizeInBits()), VT);
2350 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), VT);
23562351 case ISD::UINT_TO_FP:
23572352 case ISD::SINT_TO_FP: {
23582353 // No compile time operations on ppcf128.
64176412 if (OpVal.getOpcode() == ISD::UNDEF)
64186413 SplatUndef |= APInt::getBitsSet(sz, BitPos, BitPos + EltBitSize);
64196414 else if (ConstantSDNode *CN = dyn_cast(OpVal))
6420 SplatValue |= APInt(CN->getAPIntValue()).zextOrTrunc(EltBitSize).
6415 SplatValue |= CN->getAPIntValue().zextOrTrunc(EltBitSize).
64216416 zextOrTrunc(sz) << BitPos;
64226417 else if (ConstantFPSDNode *CN = dyn_cast(OpVal))
64236418 SplatValue |= CN->getValueAPF().bitcastToAPInt().zextOrTrunc(sz) <
64326427 while (sz > 8) {
64336428
64346429 unsigned HalfSize = sz / 2;
6435 APInt HighValue = APInt(SplatValue).lshr(HalfSize).trunc(HalfSize);
6436 APInt LowValue = APInt(SplatValue).trunc(HalfSize);
6437 APInt HighUndef = APInt(SplatUndef).lshr(HalfSize).trunc(HalfSize);
6438 APInt LowUndef = APInt(SplatUndef).trunc(HalfSize);
6430 APInt HighValue = SplatValue.lshr(HalfSize).trunc(HalfSize);
6431 APInt LowValue = SplatValue.trunc(HalfSize);
6432 APInt HighUndef = SplatUndef.lshr(HalfSize).trunc(HalfSize);
6433 APInt LowUndef = SplatUndef.trunc(HalfSize);
64396434
64406435 // If the two halves do not match (ignoring undef bits), stop here.
64416436 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
18651865 }
18661866
18671867 static APInt ComputeRange(const APInt &First, const APInt &Last) {
1868 APInt LastExt(Last), FirstExt(First);
18691868 uint32_t BitWidth = std::max(Last.getBitWidth(), First.getBitWidth()) + 1;
1870 LastExt.sext(BitWidth); FirstExt.sext(BitWidth);
1869 APInt LastExt = Last.sext(BitWidth), FirstExt = First.sext(BitWidth);
18711870 return (LastExt - FirstExt + 1ULL);
18721871 }
18731872
15191519 if ((NewBits & NewMask) == 0)
15201520 return TLO.CombineTo(Op, Op.getOperand(0));
15211521
1522 APInt InSignBit = APInt::getSignBit(EVT.getScalarType().getSizeInBits());
1523 InSignBit.zext(BitWidth);
1522 APInt InSignBit =
1523 APInt::getSignBit(EVT.getScalarType().getSizeInBits()).zext(BitWidth);
15241524 APInt InputDemandedBits =
15251525 APInt::getLowBitsSet(BitWidth,
15261526 EVT.getScalarType().getSizeInBits()) &
15551555 case ISD::ZERO_EXTEND: {
15561556 unsigned OperandBitWidth =
15571557 Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
1558 APInt InMask = NewMask;
1559 InMask.trunc(OperandBitWidth);
1558 APInt InMask = NewMask.trunc(OperandBitWidth);
15601559
15611560 // If none of the top bits are demanded, convert this into an any_extend.
15621561 APInt NewBits =
15701569 KnownZero, KnownOne, TLO, Depth+1))
15711570 return true;
15721571 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1573 KnownZero.zext(BitWidth);
1574 KnownOne.zext(BitWidth);
1572 KnownZero = KnownZero.zext(BitWidth);
1573 KnownOne = KnownOne.zext(BitWidth);
15751574 KnownZero |= NewBits;
15761575 break;
15771576 }
15921591 // bit is demanded.
15931592 APInt InDemandedBits = InMask & NewMask;
15941593 InDemandedBits |= InSignBit;
1595 InDemandedBits.trunc(InBits);
1594 InDemandedBits = InDemandedBits.trunc(InBits);
15961595
15971596 if (SimplifyDemandedBits(Op.getOperand(0), InDemandedBits, KnownZero,
15981597 KnownOne, TLO, Depth+1))
15991598 return true;
1600 KnownZero.zext(BitWidth);
1601 KnownOne.zext(BitWidth);
1599 KnownZero = KnownZero.zext(BitWidth);
1600 KnownOne = KnownOne.zext(BitWidth);
16021601
16031602 // If the sign bit is known zero, convert this to a zero extend.
16041603 if (KnownZero.intersects(InSignBit))
16191618 case ISD::ANY_EXTEND: {
16201619 unsigned OperandBitWidth =
16211620 Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
1622 APInt InMask = NewMask;
1623 InMask.trunc(OperandBitWidth);
1621 APInt InMask = NewMask.trunc(OperandBitWidth);
16241622 if (SimplifyDemandedBits(Op.getOperand(0), InMask,
16251623 KnownZero, KnownOne, TLO, Depth+1))
16261624 return true;
16271625 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1628 KnownZero.zext(BitWidth);
1629 KnownOne.zext(BitWidth);
1626 KnownZero = KnownZero.zext(BitWidth);
1627 KnownOne = KnownOne.zext(BitWidth);
16301628 break;
16311629 }
16321630 case ISD::TRUNCATE: {
16341632 // zero/one bits live out.
16351633 unsigned OperandBitWidth =
16361634 Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
1637 APInt TruncMask = NewMask;
1638 TruncMask.zext(OperandBitWidth);
1635 APInt TruncMask = NewMask.zext(OperandBitWidth);
16391636 if (SimplifyDemandedBits(Op.getOperand(0), TruncMask,
16401637 KnownZero, KnownOne, TLO, Depth+1))
16411638 return true;
1642 KnownZero.trunc(BitWidth);
1643 KnownOne.trunc(BitWidth);
1639 KnownZero = KnownZero.trunc(BitWidth);
1640 KnownOne = KnownOne.trunc(BitWidth);
16441641
16451642 // If the input is only used by this truncate, see if we can shrink it based
16461643 // on the known demanded bits.
16611658 break;
16621659 APInt HighBits = APInt::getHighBitsSet(OperandBitWidth,
16631660 OperandBitWidth - BitWidth);
1664 HighBits = HighBits.lshr(ShAmt->getZExtValue());
1665 HighBits.trunc(BitWidth);
1661 HighBits = HighBits.lshr(ShAmt->getZExtValue()).trunc(BitWidth);
16661662
16671663 if (ShAmt->getZExtValue() < BitWidth && !(HighBits & NewMask)) {
16681664 // None of the shifted in bits are needed. Add a truncate of the
19681964 (isOperationLegal(ISD::SETCC, newVT) &&
19691965 getCondCodeAction(Cond, newVT)==Legal))
19701966 return DAG.getSetCC(dl, VT, N0.getOperand(0),
1971 DAG.getConstant(APInt(C1).trunc(InSize), newVT),
1967 DAG.getConstant(C1.trunc(InSize), newVT),
19721968 Cond);
19731969 break;
19741970 }
33093309 // Truncate the significand down to its active bit count, but
33103310 // don't try to drop below 32.
33113311 unsigned newPrecision = std::max(32U, significand.getActiveBits());
3312 significand.trunc(newPrecision);
3312 significand = significand.trunc(newPrecision);
33133313 }
33143314
33153315
34143414 // Nothing to do.
34153415 } else if (exp > 0) {
34163416 // Just shift left.
3417 significand.zext(semantics->precision + exp);
3417 significand = significand.zext(semantics->precision + exp);
34183418 significand <<= exp;
34193419 exp = 0;
34203420 } else { /* exp < 0 */
34333433
34343434 // Multiply significand by 5^e.
34353435 // N * 5^0101 == N * 5^(1*1) * 5^(0*2) * 5^(1*4) * 5^(0*8)
3436 significand.zext(precision);
3436 significand = significand.zext(precision);
34373437 APInt five_to_the_i(precision, 5);
34383438 while (true) {
34393439 if (texp & 1) significand *= five_to_the_i;
994994 }
995995
996996 // Truncate to new width.
997 APInt &APInt::trunc(unsigned width) {
997 APInt APInt::trunc(unsigned width) const {
998998 assert(width < BitWidth && "Invalid APInt Truncate request");
999999 assert(width && "Can't truncate to 0 bits");
1000 unsigned wordsBefore = getNumWords();
1001 BitWidth = width;
1002 unsigned wordsAfter = getNumWords();
1003 if (wordsBefore != wordsAfter) {
1004 if (wordsAfter == 1) {
1005 uint64_t *tmp = pVal;
1006 VAL = pVal[0];
1007 delete [] tmp;
1008 } else {
1009 uint64_t *newVal = getClearedMemory(wordsAfter);
1010 for (unsigned i = 0; i < wordsAfter; ++i)
1011 newVal[i] = pVal[i];
1012 delete [] pVal;
1013 pVal = newVal;
1014 }
1015 }
1016 return clearUnusedBits();
1000
1001 if (width <= APINT_BITS_PER_WORD)
1002 return APInt(width, getRawData()[0]);
1003
1004 APInt Result(getMemory(getNumWords(width)), width);
1005
1006 // Copy full words.
1007 unsigned i;
1008 for (i = 0; i != width / APINT_BITS_PER_WORD; i++)
1009 Result.pVal[i] = pVal[i];
1010
1011 // Truncate and copy any partial word.
1012 unsigned bits = (0 - width) % APINT_BITS_PER_WORD;
1013 if (bits != 0)
1014 Result.pVal[i] = pVal[i] << bits >> bits;
1015
1016 return Result;
10171017 }
10181018
10191019 // Sign extend to a new width.
1020 APInt &APInt::sext(unsigned width) {
1020 APInt APInt::sext(unsigned width) const {
10211021 assert(width > BitWidth && "Invalid APInt SignExtend request");
1022 // If the sign bit isn't set, this is the same as zext.
1023 if (!isNegative()) {
1024 zext(width);
1025 return *this;
1026 }
1027
1028 // The sign bit is set. First, get some facts
1029 unsigned wordsBefore = getNumWords();
1030 unsigned wordBits = BitWidth % APINT_BITS_PER_WORD;
1031 BitWidth = width;
1032 unsigned wordsAfter = getNumWords();
1033
1034 // Mask the high order word appropriately
1035 if (wordsBefore == wordsAfter) {
1036 unsigned newWordBits = width % APINT_BITS_PER_WORD;
1037 // The extension is contained to the wordsBefore-1th word.
1038 uint64_t mask = ~0ULL;
1039 if (newWordBits)
1040 mask >>= APINT_BITS_PER_WORD - newWordBits;
1041 mask <<= wordBits;
1042 if (wordsBefore == 1)
1043 VAL |= mask;
1044 else
1045 pVal[wordsBefore-1] |= mask;
1046 return clearUnusedBits();
1047 }
1048
1049 uint64_t mask = wordBits == 0 ? 0 : ~0ULL << wordBits;
1050 uint64_t *newVal = getMemory(wordsAfter);
1051 if (wordsBefore == 1)
1052 newVal[0] = VAL | mask;
1053 else {
1054 for (unsigned i = 0; i < wordsBefore; ++i)
1055 newVal[i] = pVal[i];
1056 newVal[wordsBefore-1] |= mask;
1057 }
1058 for (unsigned i = wordsBefore; i < wordsAfter; i++)
1059 newVal[i] = -1ULL;
1060 if (wordsBefore != 1)
1061 delete [] pVal;
1062 pVal = newVal;
1063 return clearUnusedBits();
1022
1023 if (width <= APINT_BITS_PER_WORD) {
1024 uint64_t val = VAL << (APINT_BITS_PER_WORD - BitWidth);
1025 val = (int64_t)val >> (width - BitWidth);
1026 return APInt(width, val >> (APINT_BITS_PER_WORD - width));
1027 }
1028
1029 APInt Result(getMemory(getNumWords(width)), width);
1030
1031 // Copy full words.
1032 unsigned i;
1033 uint64_t word = 0;
1034 for (i = 0; i != BitWidth / APINT_BITS_PER_WORD; i++) {
1035 word = getRawData()[i];
1036 Result.pVal[i] = word;
1037 }
1038
1039 // Read and sign-extend any partial word.
1040 unsigned bits = (0 - BitWidth) % APINT_BITS_PER_WORD;
1041 if (bits != 0)
1042 word = (int64_t)getRawData()[i] << bits >> bits;
1043 else
1044 word = (int64_t)word >> (APINT_BITS_PER_WORD - 1);
1045
1046 // Write remaining full words.
1047 for (; i != width / APINT_BITS_PER_WORD; i++) {
1048 Result.pVal[i] = word;
1049 word = (int64_t)word >> (APINT_BITS_PER_WORD - 1);
1050 }
1051
1052 // Write any partial word.
1053 bits = (0 - width) % APINT_BITS_PER_WORD;
1054 if (bits != 0)
1055 Result.pVal[i] = word << bits >> bits;
1056
1057 return Result;
10641058 }
10651059
10661060 // Zero extend to a new width.
1067 APInt &APInt::zext(unsigned width) {
1061 APInt APInt::zext(unsigned width) const {
10681062 assert(width > BitWidth && "Invalid APInt ZeroExtend request");
1069 unsigned wordsBefore = getNumWords();
1070 BitWidth = width;
1071 unsigned wordsAfter = getNumWords();
1072 if (wordsBefore != wordsAfter) {
1073 uint64_t *newVal = getClearedMemory(wordsAfter);
1074 if (wordsBefore == 1)
1075 newVal[0] = VAL;
1076 else
1077 for (unsigned i = 0; i < wordsBefore; ++i)
1078 newVal[i] = pVal[i];
1079 if (wordsBefore != 1)
1080 delete [] pVal;
1081 pVal = newVal;
1082 }
1083 return *this;
1084 }
1085
1086 APInt &APInt::zextOrTrunc(unsigned width) {
1063
1064 if (width <= APINT_BITS_PER_WORD)
1065 return APInt(width, VAL);
1066
1067 APInt Result(getMemory(getNumWords(width)), width);
1068
1069 // Copy words.
1070 unsigned i;
1071 for (i = 0; i != getNumWords(); i++)
1072 Result.pVal[i] = getRawData()[i];
1073
1074 // Zero remaining words.
1075 memset(&Result.pVal[i], 0, (Result.getNumWords() - i) * APINT_WORD_SIZE);
1076
1077 return Result;
1078 }
1079
1080 APInt APInt::zextOrTrunc(unsigned width) const {
10871081 if (BitWidth < width)
10881082 return zext(width);
10891083 if (BitWidth > width)
10911085 return *this;
10921086 }
10931087
1094 APInt &APInt::sextOrTrunc(unsigned width) {
1088 APInt APInt::sextOrTrunc(unsigned width) const {
10951089 if (BitWidth < width)
10961090 return sext(width);
10971091 if (BitWidth > width)
441441 // Change into [0, 1 << src bit width)
442442 return ConstantRange(APInt(DstTySize,0), APInt(DstTySize,1).shl(SrcTySize));
443443
444 APInt L = Lower; L.zext(DstTySize);
445 APInt U = Upper; U.zext(DstTySize);
446 return ConstantRange(L, U);
444 return ConstantRange(Lower.zext(DstTySize), Upper.zext(DstTySize));
447445 }
448446
449447 /// signExtend - Return a new range in the specified integer type, which must
460458 APInt::getLowBitsSet(DstTySize, SrcTySize-1) + 1);
461459 }
462460
463 APInt L = Lower; L.sext(DstTySize);
464 APInt U = Upper; U.sext(DstTySize);
465 return ConstantRange(L, U);
461 return ConstantRange(Lower.sext(DstTySize), Upper.sext(DstTySize));
466462 }
467463
468464 /// truncate - Return a new range in the specified integer type, which must be
476472 if (isFullSet() || getSetSize().ugt(Size))
477473 return ConstantRange(DstTySize, /*isFullSet=*/true);
478474
479 APInt L = Lower; L.trunc(DstTySize);
480 APInt U = Upper; U.trunc(DstTySize);
481 return ConstantRange(L, U);
475 return ConstantRange(Lower.trunc(DstTySize), Upper.trunc(DstTySize));
482476 }
483477
484478 /// zextOrTrunc - make this range have the bit width given by \p DstTySize. The
370370 if (BitWidth < Result.getBitWidth())
371371 BitWidth = Result.getBitWidth(); // don't shrink the result
372372 else
373 Result.zext(BitWidth);
373 Result = Result.zext(BitWidth);
374374
375375 APInt RadixAP, CharAP; // unused unless !IsPowerOf2Radix
376376 if (!IsPowerOf2Radix) {
40054005 for (unsigned i = 0; i != NumElts; ++i) {
40064006 ConstantSDNode *C = cast(N->getOperand(i));
40074007 const APInt &CInt = C->getAPIntValue();
4008 Ops.push_back(DAG.getConstant(APInt(CInt).trunc(EltSize), TruncVT));
4008 Ops.push_back(DAG.getConstant(CInt.trunc(EltSize), TruncVT));
40094009 }
40104010 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
40114011 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts);
938938 // If all the high bits are known, we can do this xform.
939939 if ((KnownZero|KnownOne).countLeadingOnes() >= SrcBits-DstBits) {
940940 // Pull in the high bits from known-ones set.
941 APInt NewRHS(RHS->getValue());
942 NewRHS.zext(SrcBits);
941 APInt NewRHS = RHS->getValue().zext(SrcBits);
943942 NewRHS |= KnownOne;
944943 return new ICmpInst(ICI.getPredicate(), LHSI->getOperand(0),
945944 ConstantInt::get(ICI.getContext(), NewRHS));
10211020 (AndCST->getValue().isNonNegative() && RHSV.isNonNegative()))) {
10221021 uint32_t BitWidth =
10231022 cast(Cast->getOperand(0)->getType())->getBitWidth();
1024 APInt NewCST = AndCST->getValue();
1025 NewCST.zext(BitWidth);
1026 APInt NewCI = RHSV;
1027 NewCI.zext(BitWidth);
1023 APInt NewCST = AndCST->getValue().zext(BitWidth);
1024 APInt NewCI = RHSV.zext(BitWidth);
10281025 Value *NewAnd =
10291026 Builder->CreateAnd(Cast->getOperand(0),
10301027 ConstantInt::get(ICI.getContext(), NewCST),
2828 uint32_t W = C1->getBitWidth();
2929 APInt LHSExt = C1->getValue(), RHSExt = C2->getValue();
3030 if (sign) {
31 LHSExt.sext(W * 2);
32 RHSExt.sext(W * 2);
31 LHSExt = LHSExt.sext(W * 2);
32 RHSExt = RHSExt.sext(W * 2);
3333 } else {
34 LHSExt.zext(W * 2);
35 RHSExt.zext(W * 2);
34 LHSExt = LHSExt.zext(W * 2);
35 RHSExt = RHSExt.zext(W * 2);
3636 }
3737
3838 APInt MulExt = LHSExt * RHSExt;
3333 if (!OpC) return false;
3434
3535 // If there are no bits set that aren't demanded, nothing to do.
36 Demanded.zextOrTrunc(OpC->getValue().getBitWidth());
36 Demanded = Demanded.zextOrTrunc(OpC->getValue().getBitWidth());
3737 if ((~Demanded & OpC->getValue()) == 0)
3838 return false;
3939
387387 break;
388388 case Instruction::Trunc: {
389389 unsigned truncBf = I->getOperand(0)->getType()->getScalarSizeInBits();
390 DemandedMask.zext(truncBf);
391 KnownZero.zext(truncBf);
392 KnownOne.zext(truncBf);
390 DemandedMask = DemandedMask.zext(truncBf);
391 KnownZero = KnownZero.zext(truncBf);
392 KnownOne = KnownOne.zext(truncBf);
393393 if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMask,
394394 KnownZero, KnownOne, Depth+1))
395395 return I;
396 DemandedMask.trunc(BitWidth);
397 KnownZero.trunc(BitWidth);
398 KnownOne.trunc(BitWidth);
396 DemandedMask = DemandedMask.trunc(BitWidth);
397 KnownZero = KnownZero.trunc(BitWidth);
398 KnownOne = KnownOne.trunc(BitWidth);
399399 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
400400 break;
401401 }
425425 // Compute the bits in the result that are not present in the input.
426426 unsigned SrcBitWidth =I->getOperand(0)->getType()->getScalarSizeInBits();
427427
428 DemandedMask.trunc(SrcBitWidth);
429 KnownZero.trunc(SrcBitWidth);
430 KnownOne.trunc(SrcBitWidth);
428 DemandedMask = DemandedMask.trunc(SrcBitWidth);
429 KnownZero = KnownZero.trunc(SrcBitWidth);
430 KnownOne = KnownOne.trunc(SrcBitWidth);
431431 if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMask,
432432 KnownZero, KnownOne, Depth+1))
433433 return I;
434 DemandedMask.zext(BitWidth);
435 KnownZero.zext(BitWidth);
436 KnownOne.zext(BitWidth);
434 DemandedMask = DemandedMask.zext(BitWidth);
435 KnownZero = KnownZero.zext(BitWidth);
436 KnownOne = KnownOne.zext(BitWidth);
437437 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
438438 // The top bits are known to be zero.
439439 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth);
452452 if ((NewBits & DemandedMask) != 0)
453453 InputDemandedBits.setBit(SrcBitWidth-1);
454454
455 InputDemandedBits.trunc(SrcBitWidth);
456 KnownZero.trunc(SrcBitWidth);
457 KnownOne.trunc(SrcBitWidth);
455 InputDemandedBits = InputDemandedBits.trunc(SrcBitWidth);
456 KnownZero = KnownZero.trunc(SrcBitWidth);
457 KnownOne = KnownOne.trunc(SrcBitWidth);
458458 if (SimplifyDemandedBits(I->getOperandUse(0), InputDemandedBits,
459459 KnownZero, KnownOne, Depth+1))
460460 return I;
461 InputDemandedBits.zext(BitWidth);
462 KnownZero.zext(BitWidth);
463 KnownOne.zext(BitWidth);
461 InputDemandedBits = InputDemandedBits.zext(BitWidth);
462 KnownZero = KnownZero.zext(BitWidth);
463 KnownOne = KnownOne.zext(BitWidth);
464464 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
465465
466466 // If the sign bit of the input is known set or clear, then we know the
6262 while (Val.getBitWidth() != 8) {
6363 unsigned NextWidth = Val.getBitWidth()/2;
6464 Val2 = Val.lshr(NextWidth);
65 Val2.trunc(Val.getBitWidth()/2);
66 Val.trunc(Val.getBitWidth()/2);
65 Val2 = Val2.trunc(Val.getBitWidth()/2);
66 Val = Val.trunc(Val.getBitWidth()/2);
6767
6868 // If the top/bottom halves aren't the same, reject it.
6969 if (Val != Val2)
201201 APInt V = CI->getValue();
202202 if (ByteStart)
203203 V = V.lshr(ByteStart*8);
204 V.trunc(ByteSize*8);
204 V = V.trunc(ByteSize*8);
205205 return ConstantInt::get(CI->getContext(), V);
206206 }
207207
646646 case Instruction::ZExt:
647647 if (ConstantInt *CI = dyn_cast(V)) {
648648 uint32_t BitWidth = cast(DestTy)->getBitWidth();
649 APInt Result(CI->getValue());
650 Result.zext(BitWidth);
651 return ConstantInt::get(V->getContext(), Result);
649 return ConstantInt::get(V->getContext(),
650 CI->getValue().zext(BitWidth));
652651 }
653652 return 0;
654653 case Instruction::SExt:
655654 if (ConstantInt *CI = dyn_cast(V)) {
656655 uint32_t BitWidth = cast(DestTy)->getBitWidth();
657 APInt Result(CI->getValue());
658 Result.sext(BitWidth);
659 return ConstantInt::get(V->getContext(), Result);
656 return ConstantInt::get(V->getContext(),
657 CI->getValue().sext(BitWidth));
660658 }
661659 return 0;
662660 case Instruction::Trunc: {
663661 uint32_t DestBitWidth = cast(DestTy)->getBitWidth();
664662 if (ConstantInt *CI = dyn_cast(V)) {
665 APInt Result(CI->getValue());
666 Result.trunc(DestBitWidth);
667 return ConstantInt::get(V->getContext(), Result);
663 return ConstantInt::get(V->getContext(),
664 CI->getValue().trunc(DestBitWidth));
668665 }
669666
670667 // The input must be a constantexpr. See if we can simplify this based on
170170 ConstantRange TWrap = Wrap.truncate(10);
171171 EXPECT_TRUE(TFull.isFullSet());
172172 EXPECT_TRUE(TEmpty.isEmptySet());
173 EXPECT_EQ(TOne, ConstantRange(APInt(One.getLower()).trunc(10),
174 APInt(One.getUpper()).trunc(10)));
173 EXPECT_EQ(TOne, ConstantRange(One.getLower().trunc(10),
174 One.getUpper().trunc(10)));
175175 EXPECT_TRUE(TSome.isFullSet());
176176 }
177177
183183 ConstantRange ZWrap = Wrap.zeroExtend(20);
184184 EXPECT_EQ(ZFull, ConstantRange(APInt(20, 0), APInt(20, 0x10000)));
185185 EXPECT_TRUE(ZEmpty.isEmptySet());
186 EXPECT_EQ(ZOne, ConstantRange(APInt(One.getLower()).zext(20),
187 APInt(One.getUpper()).zext(20)));
188 EXPECT_EQ(ZSome, ConstantRange(APInt(Some.getLower()).zext(20),
189 APInt(Some.getUpper()).zext(20)));
186 EXPECT_EQ(ZOne, ConstantRange(One.getLower().zext(20),
187 One.getUpper().zext(20)));
188 EXPECT_EQ(ZSome, ConstantRange(Some.getLower().zext(20),
189 Some.getUpper().zext(20)));
190190 EXPECT_EQ(ZWrap, ConstantRange(APInt(20, 0), APInt(20, 0x10000)));
191191 }
192192
199199 EXPECT_EQ(SFull, ConstantRange(APInt(20, (uint64_t)INT16_MIN, true),
200200 APInt(20, INT16_MAX + 1, true)));
201201 EXPECT_TRUE(SEmpty.isEmptySet());
202 EXPECT_EQ(SOne, ConstantRange(APInt(One.getLower()).sext(20),
203 APInt(One.getUpper()).sext(20)));
204 EXPECT_EQ(SSome, ConstantRange(APInt(Some.getLower()).sext(20),
205 APInt(Some.getUpper()).sext(20)));
202 EXPECT_EQ(SOne, ConstantRange(One.getLower().sext(20),
203 One.getUpper().sext(20)));
204 EXPECT_EQ(SSome, ConstantRange(Some.getLower().sext(20),
205 Some.getUpper().sext(20)));
206206 EXPECT_EQ(SWrap, ConstantRange(APInt(20, (uint64_t)INT16_MIN, true),
207207 APInt(20, INT16_MAX + 1, true)));
208208