llvm.org GIT mirror llvm / ace8b39
[KnownBits] Add wrapper methods for setting and clear all bits in the underlying APInts in KnownBits. This adds routines for reseting KnownBits to unknown, making the value all zeros or all ones. It also adds methods for querying if the value is zero, all ones or unknown. Differential Revision: https://reviews.llvm.org/D32637 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@302262 91177308-0d34-0410-b5e6-96231b3b80d8 Craig Topper 3 years ago
15 changed file(s) with 70 addition(s) and 54 deletion(s). Raw diff Collapse all Expand all
248248 void AddLiveOutRegInfo(unsigned Reg, unsigned NumSignBits,
249249 const KnownBits &Known) {
250250 // Only install this information if it tells us something.
251 if (NumSignBits == 1 && Known.Zero == 0 && Known.One == 0)
251 if (NumSignBits == 1 && Known.isUnknown())
252252 return;
253253
254254 LiveOutRegInfo.grow(Reg);
5858 return One;
5959 }
6060
61 /// Returns true if we don't know any bits.
62 bool isUnknown() const { return Zero.isNullValue() && One.isNullValue(); }
63
64 /// Resets the known state of all bits.
65 void resetAll() {
66 Zero.clearAllBits();
67 One.clearAllBits();
68 }
69
70 /// Returns true if value is all zero.
71 bool isZero() const {
72 assert(!hasConflict() && "KnownBits conflict!");
73 return Zero.isAllOnesValue();
74 }
75
76 /// Returns true if value is all one bits.
77 bool isAllOnes() const {
78 assert(!hasConflict() && "KnownBits conflict!");
79 return One.isAllOnesValue();
80 }
81
82 /// Make all bits known to be zero and discard any previous information.
83 void setAllZero() {
84 Zero.setAllBits();
85 One.clearAllBits();
86 }
87
88 /// Make all bits known to be one and discard any previous information.
89 void setAllOnes() {
90 Zero.clearAllBits();
91 One.setAllBits();
92 }
93
6194 /// Returns true if this value is known to be negative.
6295 bool isNegative() const { return One.isSignBitSet(); }
6396
536536 unsigned BitWidth = V->getType()->getIntegerBitWidth();
537537 KnownBits Known(BitWidth);
538538 computeKnownBits(V, Known, DL, 0, AC, dyn_cast(V), DT);
539 return Known.Zero.isAllOnesValue();
539 return Known.isZero();
540540 }
541541
542542 // Per-component check doesn't work with zeroinitializer
557557
558558 KnownBits Known(BitWidth);
559559 computeKnownBits(Elem, Known, DL);
560 if (Known.Zero.isAllOnesValue())
560 if (Known.isZero())
561561 return true;
562562 }
563563
341341 // Also compute a conservative estimate for high known-0 bits.
342342 // More trickiness is possible, but this is sufficient for the
343343 // interesting case of alignment computation.
344 Known.One.clearAllBits();
345344 unsigned TrailZ = Known.Zero.countTrailingOnes() +
346345 Known2.Zero.countTrailingOnes();
347346 unsigned LeadZ = std::max(Known.Zero.countLeadingOnes() +
350349
351350 TrailZ = std::min(TrailZ, BitWidth);
352351 LeadZ = std::min(LeadZ, BitWidth);
353 Known.Zero.clearAllBits();
352 Known.resetAll();
354353 Known.Zero.setLowBits(TrailZ);
355354 Known.Zero.setHighBits(LeadZ);
356355
528527
529528 if (Arg == V && isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
530529 assert(BitWidth == 1 && "assume operand is not i1?");
531 Known.Zero.clearAllBits();
532 Known.One.setAllBits();
530 Known.setAllOnes();
533531 return;
534532 }
535533 if (match(Arg, m_Not(m_Specific(V))) &&
536534 isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
537535 assert(BitWidth == 1 && "assume operand is not i1?");
538 Known.Zero.setAllBits();
539 Known.One.clearAllBits();
536 Known.setAllZero();
540537 return;
541538 }
542539
718715 KnownBits RHSKnown(BitWidth);
719716 computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I));
720717
721 if (RHSKnown.One.isAllOnesValue() || RHSKnown.isNonNegative()) {
718 if (RHSKnown.isAllOnes() || RHSKnown.isNonNegative()) {
722719 // We know that the sign bit is zero.
723720 Known.makeNonNegative();
724721 }
740737 KnownBits RHSKnown(BitWidth);
741738 computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I));
742739
743 if (RHSKnown.Zero.isAllOnesValue() || RHSKnown.isNegative()) {
740 if (RHSKnown.isZero() || RHSKnown.isNegative()) {
744741 // We know that the sign bit is one.
745742 Known.makeNegative();
746743 }
775772 // behavior, or we might have a bug in the compiler. We can't assert/crash, so
776773 // clear out the known bits, try to warn the user, and hope for the best.
777774 if (Known.Zero.intersects(Known.One)) {
778 Known.Zero.clearAllBits();
779 Known.One.clearAllBits();
775 Known.resetAll();
780776
781777 if (Q.ORE) {
782778 auto *CxtI = const_cast(Q.CxtI);
812808 // If there is conflict between Known.Zero and Known.One, this must be an
813809 // overflowing left shift, so the shift result is undefined. Clear Known
814810 // bits so that other code could propagate this undef.
815 if ((Known.Zero & Known.One) != 0) {
816 Known.Zero.clearAllBits();
817 Known.One.clearAllBits();
818 }
811 if ((Known.Zero & Known.One) != 0)
812 Known.resetAll();
819813
820814 return;
821815 }
825819 // If the shift amount could be greater than or equal to the bit-width of the LHS, the
826820 // value could be undef, so we don't know anything about it.
827821 if ((~Known.Zero).uge(BitWidth)) {
828 Known.Zero.clearAllBits();
829 Known.One.clearAllBits();
822 Known.resetAll();
830823 return;
831824 }
832825
838831
839832 // It would be more-clearly correct to use the two temporaries for this
840833 // calculation. Reusing the APInts here to prevent unnecessary allocations.
841 Known.Zero.clearAllBits();
842 Known.One.clearAllBits();
834 Known.resetAll();
843835
844836 // If we know the shifter operand is nonzero, we can sometimes infer more
845837 // known bits. However this is expensive to compute, so be lazy about it and
885877 // return anything we'd like, but we need to make sure the sets of known bits
886878 // stay disjoint (it should be better for some other code to actually
887879 // propagate the undef than to pick a value here using known bits).
888 if (Known.Zero.intersects(Known.One)) {
889 Known.Zero.clearAllBits();
890 Known.One.clearAllBits();
891 }
880 if (Known.Zero.intersects(Known.One))
881 Known.resetAll();
892882 }
893883
894884 static void computeKnownBitsFromOperator(const Operator *I, KnownBits &Known,
923913 m_Value(Y))) ||
924914 match(I->getOperand(1), m_Add(m_Specific(I->getOperand(0)),
925915 m_Value(Y))))) {
926 Known2.Zero.clearAllBits(); Known2.One.clearAllBits();
916 Known2.resetAll();
927917 computeKnownBits(Y, Known2, Depth + 1, Q);
928918 if (Known2.One.countTrailingOnes() > 0)
929919 Known.Zero.setBit(0);
964954 computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
965955 unsigned LeadZ = Known2.Zero.countLeadingOnes();
966956
967 Known2.One.clearAllBits();
968 Known2.Zero.clearAllBits();
957 Known2.resetAll();
969958 computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);
970959 unsigned RHSUnknownLeadingOnes = Known2.One.countLeadingZeros();
971960 if (RHSUnknownLeadingOnes != BitWidth)
11971186
11981187 unsigned Leaders = std::max(Known.Zero.countLeadingOnes(),
11991188 Known2.Zero.countLeadingOnes());
1200 Known.One.clearAllBits();
1201 Known.Zero.clearAllBits();
1189 Known.resetAll();
12021190 Known.Zero.setHighBits(Leaders);
12031191 break;
12041192 }
14991487 }
15001488 // Null and aggregate-zero are all-zeros.
15011489 if (isa(V) || isa(V)) {
1502 Known.One.clearAllBits();
1503 Known.Zero.setAllBits();
1490 Known.setAllZero();
15041491 return;
15051492 }
15061493 // Handle a constant vector by taking the intersection of the known bits of
15271514 Constant *Element = CV->getAggregateElement(i);
15281515 auto *ElementCI = dyn_cast_or_null(Element);
15291516 if (!ElementCI) {
1530 Known.Zero.clearAllBits();
1531 Known.One.clearAllBits();
1517 Known.resetAll();
15321518 return;
15331519 }
15341520 Elt = ElementCI->getValue();
15391525 }
15401526
15411527 // Start out not knowing anything.
1542 Known.Zero.clearAllBits(); Known.One.clearAllBits();
1528 Known.resetAll();
15431529
15441530 // We can't imply anything about undefs.
15451531 if (isa(V))
20432043 if (M < 0) {
20442044 // For UNDEF elements, we don't know anything about the common state of
20452045 // the shuffle result.
2046 Known.One.clearAllBits();
2047 Known.Zero.clearAllBits();
2046 Known.resetAll();
20482047 DemandedLHS.clearAllBits();
20492048 DemandedRHS.clearAllBits();
20502049 break;
22172216 // Also compute a conservative estimate for high known-0 bits.
22182217 // More trickiness is possible, but this is sufficient for the
22192218 // interesting case of alignment computation.
2220 Known.One.clearAllBits();
22212219 unsigned TrailZ = Known.Zero.countTrailingOnes() +
22222220 Known2.Zero.countTrailingOnes();
22232221 unsigned LeadZ = std::max(Known.Zero.countLeadingOnes() +
22242222 Known2.Zero.countLeadingOnes(),
22252223 BitWidth) - BitWidth;
22262224
2227 Known.Zero.clearAllBits();
2225 Known.resetAll();
22282226 Known.Zero.setLowBits(std::min(TrailZ, BitWidth));
22292227 Known.Zero.setHighBits(std::min(LeadZ, BitWidth));
22302228 break;
25972595
25982596 uint32_t Leaders = std::max(Known.Zero.countLeadingOnes(),
25992597 Known2.Zero.countLeadingOnes());
2600 Known.One.clearAllBits();
2601 Known.Zero.clearAllBits();
2598 Known.resetAll();
26022599 Known.Zero.setHighBits(Leaders);
26032600 break;
26042601 }
13021302 Op.getOpcode() == ISD::INTRINSIC_VOID) &&
13031303 "Should use MaskedValueIsZero if you don't know whether Op"
13041304 " is a target node!");
1305 Known.Zero.clearAllBits(); Known.One.clearAllBits();
1305 Known.resetAll();
13061306 }
13071307
13081308 /// This method can be implemented by targets that want to expose additional
35793579 const SDValue Op, KnownBits &Known,
35803580 const APInt &DemandedElts, const SelectionDAG &DAG, unsigned Depth) const {
35813581
3582 Known.Zero.clearAllBits(); Known.One.clearAllBits(); // Don't know anything.
3582 Known.resetAll(); // Don't know anything.
35833583
35843584 KnownBits Known2;
35853585 unsigned Opc = Op.getOpcode();
1263912639 const SelectionDAG &DAG,
1264012640 unsigned Depth) const {
1264112641 unsigned BitWidth = Known.getBitWidth();
12642 Known.Zero.clearAllBits(); Known.One.clearAllBits();
12642 Known.resetAll();
1264312643 switch (Op.getOpcode()) {
1264412644 default: break;
1264512645 case ARMISD::ADDC:
1265412654 case ARMISD::CMOV: {
1265512655 // Bits are known zero/one if known on the LHS and RHS.
1265612656 DAG.computeKnownBits(Op.getOperand(0), Known, Depth+1);
12657 if (Known.Zero == 0 && Known.One == 0) return;
12657 if (Known.isUnknown())
12658 return;
1265812659
1265912660 KnownBits KnownRHS;
1266012661 DAG.computeKnownBits(Op.getOperand(1), KnownRHS, Depth+1);
1203012030 const APInt &DemandedElts,
1203112031 const SelectionDAG &DAG,
1203212032 unsigned Depth) const {
12033 Known.Zero.clearAllBits(); Known.One.clearAllBits();
12033 Known.resetAll();
1203412034 switch (Op.getOpcode()) {
1203512035 default: break;
1203612036 case PPCISD::LBRX: {
18801880 const SelectionDAG &DAG,
18811881 unsigned Depth) const {
18821882 KnownBits Known2;
1883 Known.Zero.clearAllBits(); Known.One.clearAllBits();
1883 Known.resetAll();
18841884
18851885 switch (Op.getOpcode()) {
18861886 default: break;
2661326613 "Should use MaskedValueIsZero if you don't know whether Op"
2661426614 " is a target node!");
2661526615
26616 Known.Zero.clearAllBits(); Known.One.clearAllBits();
26616 Known.resetAll();
2661726617 switch (Opc) {
2661826618 default: break;
2661926619 case X86ISD::ADD:
2664326643 case X86ISD::VSRLI: {
2664426644 if (auto *ShiftImm = dyn_cast(Op.getOperand(1))) {
2664526645 if (ShiftImm->getAPIntValue().uge(VT.getScalarSizeInBits())) {
26646 Known.Zero.setAllBits();
26646 Known.setAllZero();
2664726647 break;
2664826648 }
2664926649
18241824 const APInt &DemandedElts,
18251825 const SelectionDAG &DAG,
18261826 unsigned Depth) const {
1827 Known.Zero.clearAllBits(); Known.One.clearAllBits();
1827 Known.resetAll();
18281828 switch (Op.getOpcode()) {
18291829 default: break;
18301830 case XCoreISD::LADD:
36183618 // then this one is redundant, and should be removed.
36193619 KnownBits Known(1);
36203620 computeKnownBits(IIOperand, Known, 0, II);
3621 if (Known.One.isAllOnesValue())
3621 if (Known.isAllOnes())
36223622 return eraseInstFromFunction(*II);
36233623
36243624 // Update the cache of affected values for this assumption (we might be
40494049 // is set. If the comparison is against zero, then this is a check to see if
40504050 // *that* bit is set.
40514051 APInt Op0KnownZeroInverted = ~Op0Known.Zero;
4052 if (~Op1Known.Zero == 0) {
4052 if (Op1Known.isZero()) {
40534053 // If the LHS is an AND with the same constant, look through it.
40544054 Value *LHS = nullptr;
40554055 const APInt *LHSC;
119119 return nullptr;
120120 }
121121
122 Known.Zero.clearAllBits();
123 Known.One.clearAllBits();
122 Known.resetAll();
124123 if (DemandedMask == 0) // Not demanding any bits from V.
125124 return UndefValue::get(VTy);
126125