Skip to content

Commit bbaf121

Browse files
committed
Address review comments
- Remove the std::optional for GEPNoWrapFlags - Reorder `nuw` in DecomposedGEP.print(), and add inbounds - Refactor comment to use same names as Size variable names - Use uge for comparison
1 parent 81ce23d commit bbaf121

File tree

1 file changed

+25
-20
lines changed

1 file changed

+25
-20
lines changed

llvm/lib/Analysis/BasicAliasAnalysis.cpp

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -556,17 +556,17 @@ struct BasicAAResult::DecomposedGEP {
556556
// Scaled variable (non-constant) indices.
557557
SmallVector<VariableGEPIndex, 4> VarIndices;
558558
// 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();
561560

562561
void dump() const {
563562
print(dbgs());
564563
dbgs() << "\n";
565564
}
566565
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=[";
570570
for (size_t i = 0; i < VarIndices.size(); i++) {
571571
if (i != 0)
572572
OS << ", ";
@@ -592,6 +592,8 @@ BasicAAResult::DecomposeGEPExpression(const Value *V, const DataLayout &DL,
592592
SearchTimes++;
593593
const Instruction *CxtI = dyn_cast<Instruction>(V);
594594

595+
bool SeenGEPNWFlags = false;
596+
595597
unsigned MaxIndexSize = DL.getMaxIndexSizeInBits();
596598
DecomposedGEP Decomposed;
597599
Decomposed.Offset = APInt(MaxIndexSize, 0);
@@ -645,10 +647,12 @@ BasicAAResult::DecomposeGEPExpression(const Value *V, const DataLayout &DL,
645647
}
646648

647649
// 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 {
649653
Decomposed.NWFlags = GEPOp->getNoWrapFlags();
650-
else
651-
*Decomposed.NWFlags &= GEPOp->getNoWrapFlags();
654+
SeenGEPNWFlags = true;
655+
}
652656

653657
assert(GEPOp->getSourceElementType()->isSized() && "GEP must be sized");
654658

@@ -1126,16 +1130,17 @@ AliasResult BasicAAResult::aliasGEP(
11261130
// for the two to alias, then we can assume noalias.
11271131
// TODO: Remove !isScalable() once BasicAA fully support scalable location
11281132
// 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()) &&
11321137
isBaseOfObject(DecompGEP2.Base))
11331138
return AliasResult::NoAlias;
11341139

11351140
// 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()) &&
11391144
isBaseOfObject(DecompGEP1.Base))
11401145
return AliasResult::NoAlias;
11411146

@@ -1250,11 +1255,11 @@ AliasResult BasicAAResult::aliasGEP(
12501255
// + + +
12511256
// | BaseOffset | +<nuw> Indices |
12521257
// ---------------->|-------------------->|
1253-
// |-->VLeftSize | |-------> VRightSize
1258+
// |-->V2Size | |-------> V1Size
12541259
// LHS RHS
12551260
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()))
12581263
return AliasResult::NoAlias;
12591264

12601265
// Bail on analysing scalable LocationSize
@@ -1864,7 +1869,7 @@ void BasicAAResult::subtractDecomposedGEPs(DecomposedGEP &DestGEP,
18641869
// Drop nuw flag from GEP if subtraction of constant offsets overflows in an
18651870
// unsigned sense.
18661871
if (DestGEP.Offset.ult(SrcGEP.Offset))
1867-
DestGEP.NWFlags = DestGEP.NWFlags->withoutNoUnsignedWrap();
1872+
DestGEP.NWFlags = DestGEP.NWFlags.withoutNoUnsignedWrap();
18681873

18691874
DestGEP.Offset -= SrcGEP.Offset;
18701875
for (const VariableGEPIndex &Src : SrcGEP.VarIndices) {
@@ -1891,7 +1896,7 @@ void BasicAAResult::subtractDecomposedGEPs(DecomposedGEP &DestGEP,
18911896
// Drop nuw flag from GEP if subtraction of V's Scale overflows in an
18921897
// unsigned sense.
18931898
if (Dest.Scale.ult(Src.Scale))
1894-
DestGEP.NWFlags = DestGEP.NWFlags->withoutNoUnsignedWrap();
1899+
DestGEP.NWFlags = DestGEP.NWFlags.withoutNoUnsignedWrap();
18951900

18961901
Dest.Scale -= Src.Scale;
18971902
Dest.IsNSW = false;
@@ -1909,7 +1914,7 @@ void BasicAAResult::subtractDecomposedGEPs(DecomposedGEP &DestGEP,
19091914
DestGEP.VarIndices.push_back(Entry);
19101915

19111916
// Drop nuw flag when we have unconsumed variable indices from SrcGEP.
1912-
DestGEP.NWFlags = DestGEP.NWFlags->withoutNoUnsignedWrap();
1917+
DestGEP.NWFlags = DestGEP.NWFlags.withoutNoUnsignedWrap();
19131918
}
19141919
}
19151920
}

0 commit comments

Comments
 (0)