diff --git a/include/llvm/Support/KnownBits.h b/include/llvm/Support/KnownBits.h index 1d926bd3041..d92037bb66c 100644 --- a/include/llvm/Support/KnownBits.h +++ b/include/llvm/Support/KnownBits.h @@ -122,39 +122,48 @@ public: return ~Zero; } - /// Truncate the underlying known Zero and One bits. This is equivalent - /// to truncating the value we're tracking. + /// Return known bits for a truncation of the value we're tracking. KnownBits trunc(unsigned BitWidth) const { return KnownBits(Zero.trunc(BitWidth), One.trunc(BitWidth)); } - /// Extends the underlying known Zero and One bits. - /// By setting ExtendedBitsAreKnownZero=true this will be equivalent to - /// zero extending the value we're tracking. - /// With ExtendedBitsAreKnownZero=false the extended bits are set to unknown. - KnownBits zext(unsigned BitWidth, bool ExtendedBitsAreKnownZero) const { + /// Return known bits for an "any" extension of the value we're tracking, + /// where we don't know anything about the extended bits. + KnownBits anyext(unsigned BitWidth) const { + return KnownBits(Zero.zext(BitWidth), One.zext(BitWidth)); + } + + /// Return known bits for a zero extension of the value we're tracking. + KnownBits zext(unsigned BitWidth) const { unsigned OldBitWidth = getBitWidth(); APInt NewZero = Zero.zext(BitWidth); - if (ExtendedBitsAreKnownZero) - NewZero.setBitsFrom(OldBitWidth); + NewZero.setBitsFrom(OldBitWidth); return KnownBits(NewZero, One.zext(BitWidth)); } - /// Sign extends the underlying known Zero and One bits. This is equivalent - /// to sign extending the value we're tracking. + /// Return known bits for a sign extension of the value we're tracking. KnownBits sext(unsigned BitWidth) const { return KnownBits(Zero.sext(BitWidth), One.sext(BitWidth)); } - /// Extends or truncates the underlying known Zero and One bits. When - /// extending the extended bits can either be set as known zero (if - /// ExtendedBitsAreKnownZero=true) or as unknown (if - /// ExtendedBitsAreKnownZero=false). - KnownBits zextOrTrunc(unsigned BitWidth, - bool ExtendedBitsAreKnownZero) const { + /// Return known bits for an "any" extension or truncation of the value we're + /// tracking. + KnownBits anyextOrTrunc(unsigned BitWidth) const { if (BitWidth > getBitWidth()) - return zext(BitWidth, ExtendedBitsAreKnownZero); - return KnownBits(Zero.zextOrTrunc(BitWidth), One.zextOrTrunc(BitWidth)); + return anyext(BitWidth); + if (BitWidth < getBitWidth()) + return trunc(BitWidth); + return *this; + } + + /// Return known bits for a zero extension or truncation of the value we're + /// tracking. + KnownBits zextOrTrunc(unsigned BitWidth) const { + if (BitWidth > getBitWidth()) + return zext(BitWidth); + if (BitWidth < getBitWidth()) + return trunc(BitWidth); + return *this; } /// Return a KnownBits with the extracted bits diff --git a/lib/Analysis/ScalarEvolution.cpp b/lib/Analysis/ScalarEvolution.cpp index 36a9e7d0988..56c1c514ed8 100644 --- a/lib/Analysis/ScalarEvolution.cpp +++ b/lib/Analysis/ScalarEvolution.cpp @@ -5736,7 +5736,7 @@ ScalarEvolution::getRangeRef(const SCEV *S, // For a SCEVUnknown, ask ValueTracking. KnownBits Known = computeKnownBits(U->getValue(), DL, 0, &AC, nullptr, &DT); if (Known.getBitWidth() != BitWidth) - Known = Known.zextOrTrunc(BitWidth, true); + Known = Known.zextOrTrunc(BitWidth); // If Known does not result in full-set, intersect with it. if (Known.getMinValue() != Known.getMaxValue() + 1) ConservativeResult = ConservativeResult.intersectWith( diff --git a/lib/Analysis/ValueTracking.cpp b/lib/Analysis/ValueTracking.cpp index 3d3c9124215..3a76a699171 100644 --- a/lib/Analysis/ValueTracking.cpp +++ b/lib/Analysis/ValueTracking.cpp @@ -1254,9 +1254,9 @@ static void computeKnownBitsFromOperator(const Operator *I, Q.DL.getTypeSizeInBits(ScalarTy); assert(SrcBitWidth && "SrcBitWidth can't be zero"); - Known = Known.zextOrTrunc(SrcBitWidth, false); + Known = Known.anyextOrTrunc(SrcBitWidth); computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); - Known = Known.zextOrTrunc(BitWidth, true /* ExtendedBitsAreKnownZero */); + Known = Known.zextOrTrunc(BitWidth); break; } case Instruction::BitCast: { diff --git a/lib/CodeGen/GlobalISel/GISelKnownBits.cpp b/lib/CodeGen/GlobalISel/GISelKnownBits.cpp index b180efbe6ad..04b29948b5e 100644 --- a/lib/CodeGen/GlobalISel/GISelKnownBits.cpp +++ b/lib/CodeGen/GlobalISel/GISelKnownBits.cpp @@ -294,7 +294,7 @@ void GISelKnownBits::computeKnownBitsImpl(Register R, KnownBits &Known, case TargetOpcode::G_ANYEXT: { computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts, Depth + 1); - Known = Known.zext(BitWidth, true /* ExtendedBitsAreKnownZero */); + Known = Known.zext(BitWidth); break; } case TargetOpcode::G_LOAD: { @@ -360,9 +360,9 @@ void GISelKnownBits::computeKnownBitsImpl(Register R, KnownBits &Known, ? DL.getIndexSizeInBits(SrcTy.getAddressSpace()) : SrcTy.getSizeInBits(); assert(SrcBitWidth && "SrcBitWidth can't be zero"); - Known = Known.zextOrTrunc(SrcBitWidth, true); + Known = Known.zextOrTrunc(SrcBitWidth); computeKnownBitsImpl(SrcReg, Known, DemandedElts, Depth + 1); - Known = Known.zextOrTrunc(BitWidth, true); + Known = Known.zextOrTrunc(BitWidth); if (BitWidth > SrcBitWidth) Known.Zero.setBitsFrom(SrcBitWidth); break; diff --git a/lib/CodeGen/SelectionDAG/FunctionLoweringInfo.cpp b/lib/CodeGen/SelectionDAG/FunctionLoweringInfo.cpp index 09e4af1b901..a69f1e53428 100644 --- a/lib/CodeGen/SelectionDAG/FunctionLoweringInfo.cpp +++ b/lib/CodeGen/SelectionDAG/FunctionLoweringInfo.cpp @@ -407,7 +407,7 @@ FunctionLoweringInfo::GetLiveOutRegInfo(unsigned Reg, unsigned BitWidth) { if (BitWidth > LOI->Known.getBitWidth()) { LOI->NumSignBits = 1; - LOI->Known = LOI->Known.zext(BitWidth, false /* => any extend */); + LOI->Known = LOI->Known.anyext(BitWidth); } return LOI; diff --git a/lib/CodeGen/SelectionDAG/SelectionDAG.cpp b/lib/CodeGen/SelectionDAG/SelectionDAG.cpp index 706d3534d70..4c4c58c7afd 100644 --- a/lib/CodeGen/SelectionDAG/SelectionDAG.cpp +++ b/lib/CodeGen/SelectionDAG/SelectionDAG.cpp @@ -3061,12 +3061,12 @@ KnownBits SelectionDAG::computeKnownBits(SDValue Op, const APInt &DemandedElts, EVT InVT = Op.getOperand(0).getValueType(); APInt InDemandedElts = DemandedElts.zextOrSelf(InVT.getVectorNumElements()); Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1); - Known = Known.zext(BitWidth, true /* ExtendedBitsAreKnownZero */); + Known = Known.zext(BitWidth); break; } case ISD::ZERO_EXTEND: { Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1); - Known = Known.zext(BitWidth, true /* ExtendedBitsAreKnownZero */); + Known = Known.zext(BitWidth); break; } case ISD::SIGN_EXTEND_VECTOR_INREG: { @@ -3089,12 +3089,12 @@ KnownBits SelectionDAG::computeKnownBits(SDValue Op, const APInt &DemandedElts, EVT InVT = Op.getOperand(0).getValueType(); APInt InDemandedElts = DemandedElts.zextOrSelf(InVT.getVectorNumElements()); Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1); - Known = Known.zext(BitWidth, false /* ExtendedBitsAreKnownZero */); + Known = Known.anyext(BitWidth); break; } case ISD::ANY_EXTEND: { Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1); - Known = Known.zext(BitWidth, false /* ExtendedBitsAreKnownZero */); + Known = Known.anyext(BitWidth); break; } case ISD::TRUNCATE: { @@ -3257,7 +3257,7 @@ KnownBits SelectionDAG::computeKnownBits(SDValue Op, const APInt &DemandedElts, Known = computeKnownBits(InVec, DemandedSrcElts, Depth + 1); if (BitWidth > EltBitWidth) - Known = Known.zext(BitWidth, false /* => any extend */); + Known = Known.anyext(BitWidth); break; } case ISD::INSERT_VECTOR_ELT: { diff --git a/lib/CodeGen/SelectionDAG/TargetLowering.cpp b/lib/CodeGen/SelectionDAG/TargetLowering.cpp index f41bdf09929..9455797310f 100644 --- a/lib/CodeGen/SelectionDAG/TargetLowering.cpp +++ b/lib/CodeGen/SelectionDAG/TargetLowering.cpp @@ -864,7 +864,7 @@ bool TargetLowering::SimplifyDemandedBits( APInt SrcDemandedBits = DemandedBits.zextOrSelf(SrcBitWidth); if (SimplifyDemandedBits(Src, SrcDemandedBits, SrcKnown, TLO, Depth + 1)) return true; - Known = SrcKnown.zextOrTrunc(BitWidth, false); + Known = SrcKnown.anyextOrTrunc(BitWidth); break; } case ISD::BUILD_VECTOR: @@ -910,7 +910,7 @@ bool TargetLowering::SimplifyDemandedBits( if (SimplifyDemandedBits(Scl, DemandedSclBits, KnownScl, TLO, Depth + 1)) return true; - Known = KnownScl.zextOrTrunc(BitWidth, false); + Known = KnownScl.anyextOrTrunc(BitWidth); KnownBits KnownVec; if (SimplifyDemandedBits(Vec, DemandedBits, DemandedVecElts, KnownVec, TLO, @@ -1765,7 +1765,7 @@ bool TargetLowering::SimplifyDemandedBits( return true; assert(!Known.hasConflict() && "Bits known to be one AND zero?"); assert(Known.getBitWidth() == InBits && "Src width has changed?"); - Known = Known.zext(BitWidth, true /* ExtendedBitsAreKnownZero */); + Known = Known.zext(BitWidth); break; } case ISD::SIGN_EXTEND: @@ -1838,7 +1838,7 @@ bool TargetLowering::SimplifyDemandedBits( return true; assert(!Known.hasConflict() && "Bits known to be one AND zero?"); assert(Known.getBitWidth() == InBits && "Src width has changed?"); - Known = Known.zext(BitWidth, false /* => any extend */); + Known = Known.anyext(BitWidth); // Attempt to avoid multi-use ops if we don't need anything from them. if (SDValue NewSrc = SimplifyMultipleUseDemandedBits( @@ -1952,7 +1952,7 @@ bool TargetLowering::SimplifyDemandedBits( Known = Known2; if (BitWidth > EltBitWidth) - Known = Known.zext(BitWidth, false /* => any extend */); + Known = Known.anyext(BitWidth); break; } case ISD::BITCAST: { diff --git a/lib/Target/ARM/ARMISelLowering.cpp b/lib/Target/ARM/ARMISelLowering.cpp index bd82c8f6440..f168eb19e91 100644 --- a/lib/Target/ARM/ARMISelLowering.cpp +++ b/lib/Target/ARM/ARMISelLowering.cpp @@ -15853,7 +15853,7 @@ void ARMTargetLowering::computeKnownBitsForTargetNode(const SDValue Op, if (Op.getOpcode() == ARMISD::VGETLANEs) Known = Known.sext(DstSz); else { - Known = Known.zext(DstSz, true /* extended bits are known zero */); + Known = Known.zext(DstSz); } assert(DstSz == Known.getBitWidth()); break; diff --git a/lib/Target/SystemZ/SystemZISelLowering.cpp b/lib/Target/SystemZ/SystemZISelLowering.cpp index 8cb01709e9e..224b8dde84d 100644 --- a/lib/Target/SystemZ/SystemZISelLowering.cpp +++ b/lib/Target/SystemZ/SystemZISelLowering.cpp @@ -6607,7 +6607,7 @@ SystemZTargetLowering::computeKnownBitsForTargetNode(const SDValue Op, APInt SrcDemE = getDemandedSrcElements(Op, DemandedElts, 0); Known = DAG.computeKnownBits(SrcOp, SrcDemE, Depth + 1); if (IsLogical) { - Known = Known.zext(BitWidth, true); + Known = Known.zext(BitWidth); } else Known = Known.sext(BitWidth); break; @@ -6636,7 +6636,7 @@ SystemZTargetLowering::computeKnownBitsForTargetNode(const SDValue Op, // Known has the width of the source operand(s). Adjust if needed to match // the passed bitwidth. if (Known.getBitWidth() != BitWidth) - Known = Known.zextOrTrunc(BitWidth, false); + Known = Known.anyextOrTrunc(BitWidth); } static unsigned computeNumSignBitsBinOp(SDValue Op, const APInt &DemandedElts, diff --git a/lib/Target/X86/X86ISelLowering.cpp b/lib/Target/X86/X86ISelLowering.cpp index 1b089540aee..e4639516996 100644 --- a/lib/Target/X86/X86ISelLowering.cpp +++ b/lib/Target/X86/X86ISelLowering.cpp @@ -32945,7 +32945,7 @@ void X86TargetLowering::computeKnownBitsForTargetNode(const SDValue Op, APInt DemandedElt = APInt::getOneBitSet(SrcVT.getVectorNumElements(), Op.getConstantOperandVal(1)); Known = DAG.computeKnownBits(Src, DemandedElt, Depth + 1); - Known = Known.zextOrTrunc(BitWidth, false); + Known = Known.anyextOrTrunc(BitWidth); Known.Zero.setBitsFrom(SrcVT.getScalarSizeInBits()); break; } @@ -33059,7 +33059,7 @@ void X86TargetLowering::computeKnownBitsForTargetNode(const SDValue Op, if ((Shift + Length) <= BitWidth) { Known = DAG.computeKnownBits(Op0, Depth + 1); Known = Known.extractBits(Length, Shift); - Known = Known.zextOrTrunc(BitWidth, true /* ExtBitsAreKnownZero */); + Known = Known.zextOrTrunc(BitWidth); } } break; @@ -36484,7 +36484,7 @@ bool X86TargetLowering::SimplifyDemandedBitsForTargetNode( return TLO.CombineTo( Op, TLO.DAG.getNode(Opc, SDLoc(Op), VT, V, Op.getOperand(1))); - Known = KnownVec.zext(BitWidth, true); + Known = KnownVec.zext(BitWidth); return false; } break; diff --git a/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp b/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp index 3e8764b0111..bf91d99edf2 100644 --- a/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp +++ b/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp @@ -396,8 +396,7 @@ Value *InstCombiner::SimplifyDemandedUseBits(Value *V, APInt DemandedMask, if (SimplifyDemandedBits(I, 0, InputDemandedMask, InputKnown, Depth + 1)) return I; assert(InputKnown.getBitWidth() == SrcBitWidth && "Src width changed?"); - Known = InputKnown.zextOrTrunc(BitWidth, - true /* ExtendedBitsAreKnownZero */); + Known = InputKnown.zextOrTrunc(BitWidth); assert(!Known.hasConflict() && "Bits known to be one AND zero?"); break; }