@@ -556,17 +556,17 @@ struct BasicAAResult::DecomposedGEP {
556
556
// Scaled variable (non-constant) indices.
557
557
SmallVector<VariableGEPIndex, 4 > VarIndices;
558
558
// Nowrap flags common to all GEP operations involved in expression.
559
- // (std::nullopt iff expression doesn't involve any geps)
560
- std::optional<GEPNoWrapFlags> NWFlags;
559
+ GEPNoWrapFlags NWFlags = GEPNoWrapFlags::none();
561
560
562
561
void dump () const {
563
562
print (dbgs ());
564
563
dbgs () << " \n " ;
565
564
}
566
565
void print (raw_ostream &OS) const {
567
- OS << " (DecomposedGEP Base=" << Base->getName () << " , Offset=" << Offset
568
- << " , nuw=" << ((NWFlags && NWFlags->hasNoUnsignedWrap ()) ? " 1" : " 0" )
569
- << " , VarIndices=[" ;
566
+ OS << " (DecomposedGEP Base=" << Base->getName ()
567
+ << " , inbounds=" << (NWFlags.isInBounds () ? " 1" : " 0" )
568
+ << " , nuw=" << (NWFlags.hasNoUnsignedWrap () ? " 1" : " 0" )
569
+ << " , Offset=" << Offset << " , VarIndices=[" ;
570
570
for (size_t i = 0 ; i < VarIndices.size (); i++) {
571
571
if (i != 0 )
572
572
OS << " , " ;
@@ -592,6 +592,8 @@ BasicAAResult::DecomposeGEPExpression(const Value *V, const DataLayout &DL,
592
592
SearchTimes++;
593
593
const Instruction *CxtI = dyn_cast<Instruction>(V);
594
594
595
+ bool SeenGEPNWFlags = false ;
596
+
595
597
unsigned MaxIndexSize = DL.getMaxIndexSizeInBits ();
596
598
DecomposedGEP Decomposed;
597
599
Decomposed.Offset = APInt (MaxIndexSize, 0 );
@@ -645,10 +647,12 @@ BasicAAResult::DecomposeGEPExpression(const Value *V, const DataLayout &DL,
645
647
}
646
648
647
649
// Track the common nowrap flags for all GEPs we see.
648
- if (Decomposed.NWFlags == std::nullopt)
650
+ if (SeenGEPNWFlags) {
651
+ Decomposed.NWFlags &= GEPOp->getNoWrapFlags ();
652
+ } else {
649
653
Decomposed.NWFlags = GEPOp->getNoWrapFlags ();
650
- else
651
- *Decomposed. NWFlags &= GEPOp-> getNoWrapFlags ();
654
+ SeenGEPNWFlags = true ;
655
+ }
652
656
653
657
assert (GEPOp->getSourceElementType ()->isSized () && " GEP must be sized" );
654
658
@@ -1126,16 +1130,17 @@ AliasResult BasicAAResult::aliasGEP(
1126
1130
// for the two to alias, then we can assume noalias.
1127
1131
// TODO: Remove !isScalable() once BasicAA fully support scalable location
1128
1132
// size
1129
- if (DecompGEP1.NWFlags && DecompGEP1.NWFlags ->isInBounds () &&
1130
- DecompGEP1.VarIndices .empty () && V2Size.hasValue () &&
1131
- !V2Size.isScalable () && DecompGEP1.Offset .sge (V2Size.getValue ()) &&
1133
+
1134
+ if (DecompGEP1.NWFlags .isInBounds () && DecompGEP1.VarIndices .empty () &&
1135
+ V2Size.hasValue () && !V2Size.isScalable () &&
1136
+ DecompGEP1.Offset .sge (V2Size.getValue ()) &&
1132
1137
isBaseOfObject (DecompGEP2.Base ))
1133
1138
return AliasResult::NoAlias;
1134
1139
1135
1140
// 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 ()) &&
1141
+ if (DecompGEP2.NWFlags . isInBounds () && DecompGEP1. VarIndices . empty () &&
1142
+ V1Size. hasValue () && ! V1Size.isScalable () &&
1143
+ DecompGEP1.Offset .sle (-V1Size.getValue ()) &&
1139
1144
isBaseOfObject (DecompGEP1.Base ))
1140
1145
return AliasResult::NoAlias;
1141
1146
@@ -1250,11 +1255,11 @@ AliasResult BasicAAResult::aliasGEP(
1250
1255
// + + +
1251
1256
// | BaseOffset | +<nuw> Indices |
1252
1257
// ---------------->|-------------------->|
1253
- // |-->VLeftSize | |-------> VRightSize
1258
+ // |-->V2Size | |-------> V1Size
1254
1259
// LHS RHS
1255
1260
if (!DecompGEP1.VarIndices .empty () &&
1256
- DecompGEP1.NWFlags -> hasNoUnsignedWrap () && V2Size.hasValue () &&
1257
- !V2Size.isScalable () && DecompGEP1.Offset .sge (V2Size.getValue ()))
1261
+ DecompGEP1.NWFlags . hasNoUnsignedWrap () && V2Size.hasValue () &&
1262
+ !V2Size.isScalable () && DecompGEP1.Offset .uge (V2Size.getValue ()))
1258
1263
return AliasResult::NoAlias;
1259
1264
1260
1265
// Bail on analysing scalable LocationSize
@@ -1864,7 +1869,7 @@ void BasicAAResult::subtractDecomposedGEPs(DecomposedGEP &DestGEP,
1864
1869
// Drop nuw flag from GEP if subtraction of constant offsets overflows in an
1865
1870
// unsigned sense.
1866
1871
if (DestGEP.Offset .ult (SrcGEP.Offset ))
1867
- DestGEP.NWFlags = DestGEP.NWFlags -> withoutNoUnsignedWrap ();
1872
+ DestGEP.NWFlags = DestGEP.NWFlags . withoutNoUnsignedWrap ();
1868
1873
1869
1874
DestGEP.Offset -= SrcGEP.Offset ;
1870
1875
for (const VariableGEPIndex &Src : SrcGEP.VarIndices ) {
@@ -1891,7 +1896,7 @@ void BasicAAResult::subtractDecomposedGEPs(DecomposedGEP &DestGEP,
1891
1896
// Drop nuw flag from GEP if subtraction of V's Scale overflows in an
1892
1897
// unsigned sense.
1893
1898
if (Dest.Scale .ult (Src.Scale ))
1894
- DestGEP.NWFlags = DestGEP.NWFlags -> withoutNoUnsignedWrap ();
1899
+ DestGEP.NWFlags = DestGEP.NWFlags . withoutNoUnsignedWrap ();
1895
1900
1896
1901
Dest.Scale -= Src.Scale ;
1897
1902
Dest.IsNSW = false ;
@@ -1909,7 +1914,7 @@ void BasicAAResult::subtractDecomposedGEPs(DecomposedGEP &DestGEP,
1909
1914
DestGEP.VarIndices .push_back (Entry);
1910
1915
1911
1916
// Drop nuw flag when we have unconsumed variable indices from SrcGEP.
1912
- DestGEP.NWFlags = DestGEP.NWFlags -> withoutNoUnsignedWrap ();
1917
+ DestGEP.NWFlags = DestGEP.NWFlags . withoutNoUnsignedWrap ();
1913
1918
}
1914
1919
}
1915
1920
}
0 commit comments