@@ -107,40 +107,13 @@ struct Query {
107
107
// provide it currently.
108
108
OptimizationRemarkEmitter *ORE;
109
109
110
- // / Set of assumptions that should be excluded from further queries.
111
- // / This is because of the potential for mutual recursion to cause
112
- // / computeKnownBits to repeatedly visit the same assume intrinsic. The
113
- // / classic case of this is assume(x = y), which will attempt to determine
114
- // / bits in x from bits in y, which will attempt to determine bits in y from
115
- // / bits in x, etc. Regarding the mutual recursion, computeKnownBits can call
116
- // / isKnownNonZero, which calls computeKnownBits and isKnownToBeAPowerOfTwo
117
- // / (all of which can call computeKnownBits), and so on.
118
- std::array<const Value *, MaxAnalysisRecursionDepth> Excluded;
119
-
120
110
// / If true, it is safe to use metadata during simplification.
121
111
InstrInfoQuery IIQ;
122
112
123
- unsigned NumExcluded = 0 ;
124
-
125
113
Query (const DataLayout &DL, AssumptionCache *AC, const Instruction *CxtI,
126
114
const DominatorTree *DT, bool UseInstrInfo,
127
115
OptimizationRemarkEmitter *ORE = nullptr )
128
116
: DL(DL), AC(AC), CxtI(CxtI), DT(DT), ORE(ORE), IIQ(UseInstrInfo) {}
129
-
130
- Query (const Query &Q, const Value *NewExcl)
131
- : DL(Q.DL), AC(Q.AC), CxtI(Q.CxtI), DT(Q.DT), ORE(Q.ORE), IIQ(Q.IIQ),
132
- NumExcluded (Q.NumExcluded) {
133
- Excluded = Q.Excluded ;
134
- Excluded[NumExcluded++] = NewExcl;
135
- assert (NumExcluded <= Excluded.size ());
136
- }
137
-
138
- bool isExcluded (const Value *Value) const {
139
- if (NumExcluded == 0 )
140
- return false ;
141
- auto End = Excluded.begin () + NumExcluded;
142
- return std::find (Excluded.begin (), End, Value) != End;
143
- }
144
117
};
145
118
146
119
} // end anonymous namespace
@@ -632,8 +605,6 @@ static bool isKnownNonZeroFromAssume(const Value *V, const Query &Q) {
632
605
CallInst *I = cast<CallInst>(AssumeVH);
633
606
assert (I->getFunction () == Q.CxtI ->getFunction () &&
634
607
" Got assumption for the wrong function!" );
635
- if (Q.isExcluded (I))
636
- continue ;
637
608
638
609
// Warning: This loop can end up being somewhat performance sensitive.
639
610
// We're running this loop for once for each value queried resulting in a
@@ -681,8 +652,6 @@ static void computeKnownBitsFromAssume(const Value *V, KnownBits &Known,
681
652
CallInst *I = cast<CallInst>(AssumeVH);
682
653
assert (I->getParent ()->getParent () == Q.CxtI ->getParent ()->getParent () &&
683
654
" Got assumption for the wrong function!" );
684
- if (Q.isExcluded (I))
685
- continue ;
686
655
687
656
// Warning: This loop can end up being somewhat performance sensitive.
688
657
// We're running this loop for once for each value queried resulting in a
@@ -713,6 +682,15 @@ static void computeKnownBitsFromAssume(const Value *V, KnownBits &Known,
713
682
if (!Cmp)
714
683
continue ;
715
684
685
+ // We are attempting to compute known bits for the operands of an assume.
686
+ // Do not try to use other assumptions for those recursive calls because
687
+ // that can lead to mutual recursion and a compile-time explosion.
688
+ // An example of the mutual recursion: computeKnownBits can call
689
+ // isKnownNonZero which calls computeKnownBitsFromAssume (this function)
690
+ // and so on.
691
+ Query QueryNoAC = Q;
692
+ QueryNoAC.AC = nullptr ;
693
+
716
694
// Note that ptrtoint may change the bitwidth.
717
695
Value *A, *B;
718
696
auto m_V = m_CombineOr (m_Specific (V), m_PtrToInt (m_Specific (V)));
@@ -727,17 +705,17 @@ static void computeKnownBitsFromAssume(const Value *V, KnownBits &Known,
727
705
if (match (Cmp, m_c_ICmp (Pred, m_V, m_Value (A))) &&
728
706
isValidAssumeForContext (I, Q.CxtI , Q.DT )) {
729
707
KnownBits RHSKnown =
730
- computeKnownBits (A, Depth+1 , Query (Q, I) ).anyextOrTrunc (BitWidth);
708
+ computeKnownBits (A, Depth+1 , QueryNoAC ).anyextOrTrunc (BitWidth);
731
709
Known.Zero |= RHSKnown.Zero ;
732
710
Known.One |= RHSKnown.One ;
733
711
// assume(v & b = a)
734
712
} else if (match (Cmp,
735
713
m_c_ICmp (Pred, m_c_And (m_V, m_Value (B)), m_Value (A))) &&
736
714
isValidAssumeForContext (I, Q.CxtI , Q.DT )) {
737
715
KnownBits RHSKnown =
738
- computeKnownBits (A, Depth+1 , Query (Q, I) ).anyextOrTrunc (BitWidth);
716
+ computeKnownBits (A, Depth+1 , QueryNoAC ).anyextOrTrunc (BitWidth);
739
717
KnownBits MaskKnown =
740
- computeKnownBits (B, Depth+1 , Query (Q, I) ).anyextOrTrunc (BitWidth);
718
+ computeKnownBits (B, Depth+1 , QueryNoAC ).anyextOrTrunc (BitWidth);
741
719
742
720
// For those bits in the mask that are known to be one, we can propagate
743
721
// known bits from the RHS to V.
@@ -748,9 +726,9 @@ static void computeKnownBitsFromAssume(const Value *V, KnownBits &Known,
748
726
m_Value (A))) &&
749
727
isValidAssumeForContext (I, Q.CxtI , Q.DT )) {
750
728
KnownBits RHSKnown =
751
- computeKnownBits (A, Depth+1 , Query (Q, I) ).anyextOrTrunc (BitWidth);
729
+ computeKnownBits (A, Depth+1 , QueryNoAC ).anyextOrTrunc (BitWidth);
752
730
KnownBits MaskKnown =
753
- computeKnownBits (B, Depth+1 , Query (Q, I) ).anyextOrTrunc (BitWidth);
731
+ computeKnownBits (B, Depth+1 , QueryNoAC ).anyextOrTrunc (BitWidth);
754
732
755
733
// For those bits in the mask that are known to be one, we can propagate
756
734
// inverted known bits from the RHS to V.
@@ -761,9 +739,9 @@ static void computeKnownBitsFromAssume(const Value *V, KnownBits &Known,
761
739
m_c_ICmp (Pred, m_c_Or (m_V, m_Value (B)), m_Value (A))) &&
762
740
isValidAssumeForContext (I, Q.CxtI , Q.DT )) {
763
741
KnownBits RHSKnown =
764
- computeKnownBits (A, Depth+1 , Query (Q, I) ).anyextOrTrunc (BitWidth);
742
+ computeKnownBits (A, Depth+1 , QueryNoAC ).anyextOrTrunc (BitWidth);
765
743
KnownBits BKnown =
766
- computeKnownBits (B, Depth+1 , Query (Q, I) ).anyextOrTrunc (BitWidth);
744
+ computeKnownBits (B, Depth+1 , QueryNoAC ).anyextOrTrunc (BitWidth);
767
745
768
746
// For those bits in B that are known to be zero, we can propagate known
769
747
// bits from the RHS to V.
@@ -774,9 +752,9 @@ static void computeKnownBitsFromAssume(const Value *V, KnownBits &Known,
774
752
m_Value (A))) &&
775
753
isValidAssumeForContext (I, Q.CxtI , Q.DT )) {
776
754
KnownBits RHSKnown =
777
- computeKnownBits (A, Depth+1 , Query (Q, I) ).anyextOrTrunc (BitWidth);
755
+ computeKnownBits (A, Depth+1 , QueryNoAC ).anyextOrTrunc (BitWidth);
778
756
KnownBits BKnown =
779
- computeKnownBits (B, Depth+1 , Query (Q, I) ).anyextOrTrunc (BitWidth);
757
+ computeKnownBits (B, Depth+1 , QueryNoAC ).anyextOrTrunc (BitWidth);
780
758
781
759
// For those bits in B that are known to be zero, we can propagate
782
760
// inverted known bits from the RHS to V.
@@ -787,9 +765,9 @@ static void computeKnownBitsFromAssume(const Value *V, KnownBits &Known,
787
765
m_c_ICmp (Pred, m_c_Xor (m_V, m_Value (B)), m_Value (A))) &&
788
766
isValidAssumeForContext (I, Q.CxtI , Q.DT )) {
789
767
KnownBits RHSKnown =
790
- computeKnownBits (A, Depth+1 , Query (Q, I) ).anyextOrTrunc (BitWidth);
768
+ computeKnownBits (A, Depth+1 , QueryNoAC ).anyextOrTrunc (BitWidth);
791
769
KnownBits BKnown =
792
- computeKnownBits (B, Depth+1 , Query (Q, I) ).anyextOrTrunc (BitWidth);
770
+ computeKnownBits (B, Depth+1 , QueryNoAC ).anyextOrTrunc (BitWidth);
793
771
794
772
// For those bits in B that are known to be zero, we can propagate known
795
773
// bits from the RHS to V. For those bits in B that are known to be one,
@@ -803,9 +781,9 @@ static void computeKnownBitsFromAssume(const Value *V, KnownBits &Known,
803
781
m_Value (A))) &&
804
782
isValidAssumeForContext (I, Q.CxtI , Q.DT )) {
805
783
KnownBits RHSKnown =
806
- computeKnownBits (A, Depth+1 , Query (Q, I) ).anyextOrTrunc (BitWidth);
784
+ computeKnownBits (A, Depth+1 , QueryNoAC ).anyextOrTrunc (BitWidth);
807
785
KnownBits BKnown =
808
- computeKnownBits (B, Depth+1 , Query (Q, I) ).anyextOrTrunc (BitWidth);
786
+ computeKnownBits (B, Depth+1 , QueryNoAC ).anyextOrTrunc (BitWidth);
809
787
810
788
// For those bits in B that are known to be zero, we can propagate
811
789
// inverted known bits from the RHS to V. For those bits in B that are
@@ -819,7 +797,7 @@ static void computeKnownBitsFromAssume(const Value *V, KnownBits &Known,
819
797
m_Value (A))) &&
820
798
isValidAssumeForContext (I, Q.CxtI , Q.DT ) && C < BitWidth) {
821
799
KnownBits RHSKnown =
822
- computeKnownBits (A, Depth+1 , Query (Q, I) ).anyextOrTrunc (BitWidth);
800
+ computeKnownBits (A, Depth+1 , QueryNoAC ).anyextOrTrunc (BitWidth);
823
801
824
802
// For those bits in RHS that are known, we can propagate them to known
825
803
// bits in V shifted to the right by C.
@@ -832,7 +810,7 @@ static void computeKnownBitsFromAssume(const Value *V, KnownBits &Known,
832
810
m_Value (A))) &&
833
811
isValidAssumeForContext (I, Q.CxtI , Q.DT ) && C < BitWidth) {
834
812
KnownBits RHSKnown =
835
- computeKnownBits (A, Depth+1 , Query (Q, I) ).anyextOrTrunc (BitWidth);
813
+ computeKnownBits (A, Depth+1 , QueryNoAC ).anyextOrTrunc (BitWidth);
836
814
// For those bits in RHS that are known, we can propagate them inverted
837
815
// to known bits in V shifted to the right by C.
838
816
RHSKnown.One .lshrInPlace (C);
@@ -844,7 +822,7 @@ static void computeKnownBitsFromAssume(const Value *V, KnownBits &Known,
844
822
m_Value (A))) &&
845
823
isValidAssumeForContext (I, Q.CxtI , Q.DT ) && C < BitWidth) {
846
824
KnownBits RHSKnown =
847
- computeKnownBits (A, Depth+1 , Query (Q, I) ).anyextOrTrunc (BitWidth);
825
+ computeKnownBits (A, Depth+1 , QueryNoAC ).anyextOrTrunc (BitWidth);
848
826
// For those bits in RHS that are known, we can propagate them to known
849
827
// bits in V shifted to the right by C.
850
828
Known.Zero |= RHSKnown.Zero << C;
@@ -854,7 +832,7 @@ static void computeKnownBitsFromAssume(const Value *V, KnownBits &Known,
854
832
m_Value (A))) &&
855
833
isValidAssumeForContext (I, Q.CxtI , Q.DT ) && C < BitWidth) {
856
834
KnownBits RHSKnown =
857
- computeKnownBits (A, Depth+1 , Query (Q, I) ).anyextOrTrunc (BitWidth);
835
+ computeKnownBits (A, Depth+1 , QueryNoAC ).anyextOrTrunc (BitWidth);
858
836
// For those bits in RHS that are known, we can propagate them inverted
859
837
// to known bits in V shifted to the right by C.
860
838
Known.Zero |= RHSKnown.One << C;
@@ -866,7 +844,7 @@ static void computeKnownBitsFromAssume(const Value *V, KnownBits &Known,
866
844
if (match (Cmp, m_ICmp (Pred, m_V, m_Value (A))) &&
867
845
isValidAssumeForContext (I, Q.CxtI , Q.DT )) {
868
846
KnownBits RHSKnown =
869
- computeKnownBits (A, Depth + 1 , Query (Q, I) ).anyextOrTrunc (BitWidth);
847
+ computeKnownBits (A, Depth + 1 , QueryNoAC ).anyextOrTrunc (BitWidth);
870
848
871
849
if (RHSKnown.isNonNegative ()) {
872
850
// We know that the sign bit is zero.
@@ -879,7 +857,7 @@ static void computeKnownBitsFromAssume(const Value *V, KnownBits &Known,
879
857
if (match (Cmp, m_ICmp (Pred, m_V, m_Value (A))) &&
880
858
isValidAssumeForContext (I, Q.CxtI , Q.DT )) {
881
859
KnownBits RHSKnown =
882
- computeKnownBits (A, Depth + 1 , Query (Q, I) ).anyextOrTrunc (BitWidth);
860
+ computeKnownBits (A, Depth + 1 , QueryNoAC ).anyextOrTrunc (BitWidth);
883
861
884
862
if (RHSKnown.isAllOnes () || RHSKnown.isNonNegative ()) {
885
863
// We know that the sign bit is zero.
@@ -892,7 +870,7 @@ static void computeKnownBitsFromAssume(const Value *V, KnownBits &Known,
892
870
if (match (Cmp, m_ICmp (Pred, m_V, m_Value (A))) &&
893
871
isValidAssumeForContext (I, Q.CxtI , Q.DT )) {
894
872
KnownBits RHSKnown =
895
- computeKnownBits (A, Depth + 1 , Query (Q, I) ).anyextOrTrunc (BitWidth);
873
+ computeKnownBits (A, Depth + 1 , QueryNoAC ).anyextOrTrunc (BitWidth);
896
874
897
875
if (RHSKnown.isNegative ()) {
898
876
// We know that the sign bit is one.
@@ -905,7 +883,7 @@ static void computeKnownBitsFromAssume(const Value *V, KnownBits &Known,
905
883
if (match (Cmp, m_ICmp (Pred, m_V, m_Value (A))) &&
906
884
isValidAssumeForContext (I, Q.CxtI , Q.DT )) {
907
885
KnownBits RHSKnown =
908
- computeKnownBits (A, Depth+1 , Query (Q, I) ).anyextOrTrunc (BitWidth);
886
+ computeKnownBits (A, Depth+1 , QueryNoAC ).anyextOrTrunc (BitWidth);
909
887
910
888
if (RHSKnown.isZero () || RHSKnown.isNegative ()) {
911
889
// We know that the sign bit is one.
@@ -918,7 +896,7 @@ static void computeKnownBitsFromAssume(const Value *V, KnownBits &Known,
918
896
if (match (Cmp, m_ICmp (Pred, m_V, m_Value (A))) &&
919
897
isValidAssumeForContext (I, Q.CxtI , Q.DT )) {
920
898
KnownBits RHSKnown =
921
- computeKnownBits (A, Depth+1 , Query (Q, I) ).anyextOrTrunc (BitWidth);
899
+ computeKnownBits (A, Depth+1 , QueryNoAC ).anyextOrTrunc (BitWidth);
922
900
923
901
// Whatever high bits in c are zero are known to be zero.
924
902
Known.Zero .setHighBits (RHSKnown.countMinLeadingZeros ());
@@ -929,7 +907,7 @@ static void computeKnownBitsFromAssume(const Value *V, KnownBits &Known,
929
907
if (match (Cmp, m_ICmp (Pred, m_V, m_Value (A))) &&
930
908
isValidAssumeForContext (I, Q.CxtI , Q.DT )) {
931
909
KnownBits RHSKnown =
932
- computeKnownBits (A, Depth+1 , Query (Q, I) ).anyextOrTrunc (BitWidth);
910
+ computeKnownBits (A, Depth+1 , QueryNoAC ).anyextOrTrunc (BitWidth);
933
911
934
912
// If the RHS is known zero, then this assumption must be wrong (nothing
935
913
// is unsigned less than zero). Signal a conflict and get out of here.
@@ -941,7 +919,7 @@ static void computeKnownBitsFromAssume(const Value *V, KnownBits &Known,
941
919
942
920
// Whatever high bits in c are zero are known to be zero (if c is a power
943
921
// of 2, then one more).
944
- if (isKnownToBeAPowerOfTwo (A, false , Depth + 1 , Query (Q, I) ))
922
+ if (isKnownToBeAPowerOfTwo (A, false , Depth + 1 , QueryNoAC ))
945
923
Known.Zero .setHighBits (RHSKnown.countMinLeadingZeros () + 1 );
946
924
else
947
925
Known.Zero .setHighBits (RHSKnown.countMinLeadingZeros ());
0 commit comments