Skip to content

Commit f014972

Browse files
committed
[Attributor][NFC] Cleanup some AAMemoryLocation code
This is the first step to resolve a TODO in AAMemoryLocation and to fix a bug we have when handling `byval` arguments of `readnone` call sites. No functional change intended.
1 parent 0cc9c02 commit f014972

File tree

1 file changed

+46
-36
lines changed

1 file changed

+46
-36
lines changed

llvm/lib/Transforms/IPO/AttributorAttributes.cpp

Lines changed: 46 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6090,7 +6090,8 @@ struct AAMemoryLocationImpl : public AAMemoryLocation {
60906090
Instruction *I = dyn_cast<Instruction>(&getAssociatedValue());
60916091
for (MemoryLocationsKind CurMLK = 1; CurMLK < NO_LOCATIONS; CurMLK *= 2)
60926092
if (!(CurMLK & KnownMLK))
6093-
updateStateAndAccessesMap(getState(), CurMLK, I, nullptr, Changed);
6093+
updateStateAndAccessesMap(getState(), CurMLK, I, nullptr, Changed,
6094+
getAccessKindFromInst(I));
60946095
return AAMemoryLocation::indicatePessimisticFixpoint();
60956096
}
60966097

@@ -6131,24 +6132,29 @@ struct AAMemoryLocationImpl : public AAMemoryLocation {
61316132
AAMemoryLocation::MemoryLocationsKind
61326133
categorizeAccessedLocations(Attributor &A, Instruction &I, bool &Changed);
61336134

6135+
/// Return the access kind as determined by \p I.
6136+
AccessKind getAccessKindFromInst(const Instruction *I) {
6137+
AccessKind AK = READ_WRITE;
6138+
if (I) {
6139+
AK = I->mayReadFromMemory() ? READ : NONE;
6140+
AK = AccessKind(AK | (I->mayWriteToMemory() ? WRITE : NONE));
6141+
}
6142+
return AK;
6143+
}
6144+
61346145
/// Update the state \p State and the AccessKind2Accesses given that \p I is
6135-
/// an access to a \p MLK memory location with the access pointer \p Ptr.
6146+
/// an access of kind \p AK to a \p MLK memory location with the access
6147+
/// pointer \p Ptr.
61366148
void updateStateAndAccessesMap(AAMemoryLocation::StateType &State,
61376149
MemoryLocationsKind MLK, const Instruction *I,
6138-
const Value *Ptr, bool &Changed) {
6139-
// TODO: The kind should be determined at the call sites based on the
6140-
// information we have there.
6141-
AccessKind Kind = READ_WRITE;
6142-
if (I) {
6143-
Kind = I->mayReadFromMemory() ? READ : NONE;
6144-
Kind = AccessKind(Kind | (I->mayWriteToMemory() ? WRITE : NONE));
6145-
}
6150+
const Value *Ptr, bool &Changed,
6151+
AccessKind AK = READ_WRITE) {
61466152

61476153
assert(isPowerOf2_32(MLK) && "Expected a single location set!");
61486154
auto *&Accesses = AccessKind2Accesses[llvm::Log2_32(MLK)];
61496155
if (!Accesses)
61506156
Accesses = new (Allocator) AccessSet();
6151-
Changed |= Accesses->insert(AccessInfo{I, Ptr, Kind}).second;
6157+
Changed |= Accesses->insert(AccessInfo{I, Ptr, AK}).second;
61526158
State.removeAssumedBits(MLK);
61536159
}
61546160

@@ -6187,37 +6193,36 @@ void AAMemoryLocationImpl::categorizePtrValue(
61876193
auto VisitValueCB = [&](Value &V, const Instruction *,
61886194
AAMemoryLocation::StateType &T,
61896195
bool Stripped) -> bool {
6196+
MemoryLocationsKind MLK = NO_LOCATIONS;
61906197
assert(!isa<GEPOperator>(V) && "GEPs should have been stripped.");
61916198
if (isa<UndefValue>(V))
61926199
return true;
61936200
if (auto *Arg = dyn_cast<Argument>(&V)) {
61946201
if (Arg->hasByValAttr())
6195-
updateStateAndAccessesMap(T, NO_LOCAL_MEM, &I, &V, Changed);
6202+
MLK = NO_LOCAL_MEM;
61966203
else
6197-
updateStateAndAccessesMap(T, NO_ARGUMENT_MEM, &I, &V, Changed);
6198-
return true;
6199-
}
6200-
if (auto *GV = dyn_cast<GlobalValue>(&V)) {
6204+
MLK = NO_ARGUMENT_MEM;
6205+
} else if (auto *GV = dyn_cast<GlobalValue>(&V)) {
62016206
if (GV->hasLocalLinkage())
6202-
updateStateAndAccessesMap(T, NO_GLOBAL_INTERNAL_MEM, &I, &V, Changed);
6207+
MLK = NO_GLOBAL_INTERNAL_MEM;
62036208
else
6204-
updateStateAndAccessesMap(T, NO_GLOBAL_EXTERNAL_MEM, &I, &V, Changed);
6205-
return true;
6206-
}
6207-
if (isa<AllocaInst>(V)) {
6208-
updateStateAndAccessesMap(T, NO_LOCAL_MEM, &I, &V, Changed);
6209-
return true;
6210-
}
6211-
if (const auto *CB = dyn_cast<CallBase>(&V)) {
6209+
MLK = NO_GLOBAL_EXTERNAL_MEM;
6210+
} else if (isa<AllocaInst>(V))
6211+
MLK = NO_LOCAL_MEM;
6212+
else if (const auto *CB = dyn_cast<CallBase>(&V)) {
62126213
const auto &NoAliasAA =
62136214
A.getAAFor<AANoAlias>(*this, IRPosition::callsite_returned(*CB));
6214-
if (NoAliasAA.isAssumedNoAlias()) {
6215-
updateStateAndAccessesMap(T, NO_MALLOCED_MEM, &I, &V, Changed);
6216-
return true;
6217-
}
6215+
if (NoAliasAA.isAssumedNoAlias())
6216+
MLK = NO_MALLOCED_MEM;
6217+
else
6218+
MLK = NO_UNKOWN_MEM;
6219+
} else {
6220+
MLK = NO_UNKOWN_MEM;
62186221
}
62196222

6220-
updateStateAndAccessesMap(T, NO_UNKOWN_MEM, &I, &V, Changed);
6223+
assert(MLK != NO_LOCATIONS && "No location specified!");
6224+
updateStateAndAccessesMap(T, MLK, &I, &V, Changed,
6225+
getAccessKindFromInst(&I));
62216226
LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Ptr value cannot be categorized: "
62226227
<< V << " -> " << getMemoryLocationsAsStr(T.getAssumed())
62236228
<< "\n");
@@ -6229,7 +6234,8 @@ void AAMemoryLocationImpl::categorizePtrValue(
62296234
/* MaxValues */ 32, StripGEPCB)) {
62306235
LLVM_DEBUG(
62316236
dbgs() << "[AAMemoryLocation] Pointer locations not categorized\n");
6232-
updateStateAndAccessesMap(State, NO_UNKOWN_MEM, &I, nullptr, Changed);
6237+
updateStateAndAccessesMap(State, NO_UNKOWN_MEM, &I, nullptr, Changed,
6238+
getAccessKindFromInst(&I));
62336239
} else {
62346240
LLVM_DEBUG(
62356241
dbgs()
@@ -6260,7 +6266,7 @@ AAMemoryLocationImpl::categorizeAccessedLocations(Attributor &A, Instruction &I,
62606266

62616267
if (CBMemLocationAA.isAssumedInaccessibleMemOnly()) {
62626268
updateStateAndAccessesMap(AccessedLocs, NO_INACCESSIBLE_MEM, &I, nullptr,
6263-
Changed);
6269+
Changed, getAccessKindFromInst(&I));
62646270
return AccessedLocs.getAssumed();
62656271
}
62666272

@@ -6274,7 +6280,8 @@ AAMemoryLocationImpl::categorizeAccessedLocations(Attributor &A, Instruction &I,
62746280
for (MemoryLocationsKind CurMLK = 1; CurMLK < NO_LOCATIONS; CurMLK *= 2) {
62756281
if (CBAssumedNotAccessedLocsNoArgMem & CurMLK)
62766282
continue;
6277-
updateStateAndAccessesMap(AccessedLocs, CurMLK, &I, nullptr, Changed);
6283+
updateStateAndAccessesMap(AccessedLocs, CurMLK, &I, nullptr, Changed,
6284+
getAccessKindFromInst(&I));
62786285
}
62796286

62806287
// Now handle global memory if it might be accessed. This is slightly tricky
@@ -6283,7 +6290,8 @@ AAMemoryLocationImpl::categorizeAccessedLocations(Attributor &A, Instruction &I,
62836290
if (HasGlobalAccesses) {
62846291
auto AccessPred = [&](const Instruction *, const Value *Ptr,
62856292
AccessKind Kind, MemoryLocationsKind MLK) {
6286-
updateStateAndAccessesMap(AccessedLocs, MLK, &I, Ptr, Changed);
6293+
updateStateAndAccessesMap(AccessedLocs, MLK, &I, Ptr, Changed,
6294+
getAccessKindFromInst(&I));
62876295
return true;
62886296
};
62896297
if (!CBMemLocationAA.checkForAllAccessesToMemoryKind(
@@ -6337,7 +6345,8 @@ AAMemoryLocationImpl::categorizeAccessedLocations(Attributor &A, Instruction &I,
63376345

63386346
LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Failed to categorize instruction: "
63396347
<< I << "\n");
6340-
updateStateAndAccessesMap(AccessedLocs, NO_UNKOWN_MEM, &I, nullptr, Changed);
6348+
updateStateAndAccessesMap(AccessedLocs, NO_UNKOWN_MEM, &I, nullptr, Changed,
6349+
getAccessKindFromInst(&I));
63416350
return AccessedLocs.getAssumed();
63426351
}
63436352

@@ -6419,7 +6428,8 @@ struct AAMemoryLocationCallSite final : AAMemoryLocationImpl {
64196428
bool Changed = false;
64206429
auto AccessPred = [&](const Instruction *I, const Value *Ptr,
64216430
AccessKind Kind, MemoryLocationsKind MLK) {
6422-
updateStateAndAccessesMap(getState(), MLK, I, Ptr, Changed);
6431+
updateStateAndAccessesMap(getState(), MLK, I, Ptr, Changed,
6432+
getAccessKindFromInst(I));
64236433
return true;
64246434
};
64256435
if (!FnAA.checkForAllAccessesToMemoryKind(AccessPred, ALL_LOCATIONS))

0 commit comments

Comments
 (0)