@@ -530,8 +530,6 @@ struct VariableGEPIndex {
530
530
return Scale == Other.Scale ;
531
531
}
532
532
533
- bool isSubtracted () const { return IsNegated || Scale.isNegative (); }
534
-
535
533
void dump () const {
536
534
print (dbgs ());
537
535
dbgs () << " \n " ;
@@ -1113,6 +1111,13 @@ AliasResult BasicAAResult::aliasGEP(
1113
1111
if (DecompGEP1.Base == GEP1 && DecompGEP2.Base == V2)
1114
1112
return AliasResult::MayAlias;
1115
1113
1114
+ // Swap GEP1 and GEP2 if GEP2 has more variable indices.
1115
+ if (DecompGEP1.VarIndices .size () < DecompGEP2.VarIndices .size ()) {
1116
+ std::swap (DecompGEP1, DecompGEP2);
1117
+ std::swap (V1Size, V2Size);
1118
+ std::swap (UnderlyingV1, UnderlyingV2);
1119
+ }
1120
+
1116
1121
// Subtract the GEP2 pointer from the GEP1 pointer to find out their
1117
1122
// symbolic difference.
1118
1123
subtractDecomposedGEPs (DecompGEP1, DecompGEP2, AAQI);
@@ -1121,20 +1126,18 @@ AliasResult BasicAAResult::aliasGEP(
1121
1126
// for the two to alias, then we can assume noalias.
1122
1127
// TODO: Remove !isScalable() once BasicAA fully support scalable location
1123
1128
// size
1124
- if (DecompGEP1.NWFlags -> isInBounds () && DecompGEP1.VarIndices . empty () &&
1125
- V2Size. hasValue () && ! V2Size.isScalable () &&
1126
- DecompGEP1.Offset .sge (V2Size.getValue ()) &&
1129
+ if (DecompGEP1.NWFlags && DecompGEP1.NWFlags -> isInBounds () &&
1130
+ DecompGEP1. VarIndices . empty () && V2Size.hasValue () &&
1131
+ !V2Size. isScalable () && DecompGEP1.Offset .sge (V2Size.getValue ()) &&
1127
1132
isBaseOfObject (DecompGEP2.Base ))
1128
1133
return AliasResult::NoAlias;
1129
1134
1130
- if (isa<GEPOperator>(V2)) {
1131
- // Symmetric case to above.
1132
- if (DecompGEP2.NWFlags ->isInBounds () && DecompGEP1.VarIndices .empty () &&
1133
- V1Size.hasValue () && !V1Size.isScalable () &&
1134
- DecompGEP1.Offset .sle (-V1Size.getValue ()) &&
1135
- isBaseOfObject (DecompGEP1.Base ))
1136
- return AliasResult::NoAlias;
1137
- }
1135
+ // Symmetric case to above.
1136
+ if (DecompGEP2.NWFlags && DecompGEP2.NWFlags ->isInBounds () &&
1137
+ DecompGEP1.VarIndices .empty () && V1Size.hasValue () &&
1138
+ !V1Size.isScalable () && DecompGEP1.Offset .sle (-V1Size.getValue ()) &&
1139
+ isBaseOfObject (DecompGEP1.Base ))
1140
+ return AliasResult::NoAlias;
1138
1141
1139
1142
// For GEPs with identical offsets, we can preserve the size and AAInfo
1140
1143
// when performing the alias check on the underlying objects.
@@ -1250,20 +1253,9 @@ AliasResult BasicAAResult::aliasGEP(
1250
1253
// |-->VLeftSize | |-------> VRightSize
1251
1254
// LHS RHS
1252
1255
if (!DecompGEP1.VarIndices .empty () &&
1253
- llvm::all_of (DecompGEP1.VarIndices , [&](const VariableGEPIndex &V) {
1254
- return V.isSubtracted () == DecompGEP1.VarIndices .front ().isSubtracted ();
1255
- })) {
1256
- const APInt &Off = DecompGEP1.Offset ;
1257
- bool Swapped = Off.isNegative ();
1258
- LocationSize VLeftSize = Swapped ? V1Size : V2Size;
1259
- const DecomposedGEP &DecompRight = Swapped ? DecompGEP2 : DecompGEP1;
1260
-
1261
- bool IndicesAdded = DecompGEP1.VarIndices .front ().isSubtracted () == Swapped;
1262
- if (IndicesAdded && DecompRight.NWFlags ->hasNoUnsignedWrap ())
1263
- if (VLeftSize.hasValue () && !VLeftSize.isScalable () &&
1264
- Off.abs ().uge (VLeftSize.getValue ()))
1265
- return AliasResult::NoAlias;
1266
- }
1256
+ DecompGEP1.NWFlags ->hasNoUnsignedWrap () && V2Size.hasValue () &&
1257
+ !V2Size.isScalable () && DecompGEP1.Offset .sge (V2Size.getValue ()))
1258
+ return AliasResult::NoAlias;
1267
1259
1268
1260
// Bail on analysing scalable LocationSize
1269
1261
if (V1Size.isScalable () || V2Size.isScalable ())
@@ -1867,14 +1859,12 @@ bool BasicAAResult::isValueEqualInPotentialCycles(const Value *V,
1867
1859
1868
1860
// / Computes the symbolic difference between two de-composed GEPs.
1869
1861
void BasicAAResult::subtractDecomposedGEPs (DecomposedGEP &DestGEP,
1870
- DecomposedGEP &SrcGEP,
1862
+ const DecomposedGEP &SrcGEP,
1871
1863
const AAQueryInfo &AAQI) {
1872
1864
// Drop nuw flag from GEP if subtraction of constant offsets overflows in an
1873
1865
// unsigned sense.
1874
1866
if (DestGEP.Offset .ult (SrcGEP.Offset ))
1875
1867
DestGEP.NWFlags = DestGEP.NWFlags ->withoutNoUnsignedWrap ();
1876
- else if (SrcGEP.Offset .ult (DestGEP.Offset ) && SrcGEP.NWFlags )
1877
- SrcGEP.NWFlags = SrcGEP.NWFlags ->withoutNoUnsignedWrap ();
1878
1868
1879
1869
DestGEP.Offset -= SrcGEP.Offset ;
1880
1870
for (const VariableGEPIndex &Src : SrcGEP.VarIndices ) {
@@ -1902,8 +1892,6 @@ void BasicAAResult::subtractDecomposedGEPs(DecomposedGEP &DestGEP,
1902
1892
// unsigned sense.
1903
1893
if (Dest.Scale .ult (Src.Scale ))
1904
1894
DestGEP.NWFlags = DestGEP.NWFlags ->withoutNoUnsignedWrap ();
1905
- else if (SrcGEP.NWFlags )
1906
- SrcGEP.NWFlags = SrcGEP.NWFlags ->withoutNoUnsignedWrap ();
1907
1895
1908
1896
Dest.Scale -= Src.Scale ;
1909
1897
Dest.IsNSW = false ;
@@ -1919,6 +1907,9 @@ void BasicAAResult::subtractDecomposedGEPs(DecomposedGEP &DestGEP,
1919
1907
VariableGEPIndex Entry = {Src.Val , Src.Scale , Src.CxtI , Src.IsNSW ,
1920
1908
/* IsNegated */ true };
1921
1909
DestGEP.VarIndices .push_back (Entry);
1910
+
1911
+ // Drop nuw flag when we have unconsumed variable indices from SrcGEP.
1912
+ DestGEP.NWFlags = DestGEP.NWFlags ->withoutNoUnsignedWrap ();
1922
1913
}
1923
1914
}
1924
1915
}
0 commit comments