Skip to content

[BasicAA] Make isNotCapturedBeforeOrAt() check for calls more precise #69931

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions llvm/include/llvm/Analysis/AliasAnalysis.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,11 @@ raw_ostream &operator<<(raw_ostream &OS, AliasResult AR);
/// Virtual base class for providers of capture information.
struct CaptureInfo {
virtual ~CaptureInfo() = 0;
virtual bool isNotCapturedBeforeOrAt(const Value *Object,
const Instruction *I) = 0;

/// Check whether Object is not captured before instruction I. If OrAt is
/// true, captures by instruction I itself are also considered.
virtual bool isNotCapturedBefore(const Value *Object, const Instruction *I,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While you are here it would be good to document the function

bool OrAt) = 0;
};

/// Context-free CaptureInfo provider, which computes and caches whether an
Expand All @@ -163,8 +166,8 @@ class SimpleCaptureInfo final : public CaptureInfo {
SmallDenseMap<const Value *, bool, 8> IsCapturedCache;

public:
bool isNotCapturedBeforeOrAt(const Value *Object,
const Instruction *I) override;
bool isNotCapturedBefore(const Value *Object, const Instruction *I,
bool OrAt) override;
};

/// Context-sensitive CaptureInfo provider, which computes and caches the
Expand All @@ -188,8 +191,8 @@ class EarliestEscapeInfo final : public CaptureInfo {
EarliestEscapeInfo(DominatorTree &DT, const LoopInfo *LI = nullptr)
: DT(DT), LI(LI) {}

bool isNotCapturedBeforeOrAt(const Value *Object,
const Instruction *I) override;
bool isNotCapturedBefore(const Value *Object, const Instruction *I,
bool OrAt) override;

void removeInstruction(Instruction *I);
};
Expand Down
38 changes: 23 additions & 15 deletions llvm/lib/Analysis/BasicAliasAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,21 @@ static bool isObjectSize(const Value *V, TypeSize Size, const DataLayout &DL,

CaptureInfo::~CaptureInfo() = default;

bool SimpleCaptureInfo::isNotCapturedBeforeOrAt(const Value *Object,
const Instruction *I) {
bool SimpleCaptureInfo::isNotCapturedBefore(const Value *Object,
const Instruction *I, bool OrAt) {
return isNonEscapingLocalObject(Object, &IsCapturedCache);
}

bool EarliestEscapeInfo::isNotCapturedBeforeOrAt(const Value *Object,
const Instruction *I) {
static bool isNotInCycle(const Instruction *I, const DominatorTree &DT,
const LoopInfo *LI) {
BasicBlock *BB = const_cast<BasicBlock *>(I->getParent());
SmallVector<BasicBlock *> Succs(successors(BB));
return Succs.empty() ||
!isPotentiallyReachableFromMany(Succs, BB, nullptr, &DT, LI);
}

bool EarliestEscapeInfo::isNotCapturedBefore(const Value *Object,
const Instruction *I, bool OrAt) {
if (!isIdentifiedFunctionLocal(Object))
return false;

Expand All @@ -220,8 +228,13 @@ bool EarliestEscapeInfo::isNotCapturedBeforeOrAt(const Value *Object,
if (!Iter.first->second)
return true;

return I != Iter.first->second &&
!isPotentiallyReachable(Iter.first->second, I, nullptr, &DT, LI);
if (I == Iter.first->second) {
if (OrAt)
return false;
return isNotInCycle(I, DT, LI);
}

return !isPotentiallyReachable(Iter.first->second, I, nullptr, &DT, LI);
}

void EarliestEscapeInfo::removeInstruction(Instruction *I) {
Expand Down Expand Up @@ -887,7 +900,7 @@ ModRefInfo BasicAAResult::getModRefInfo(const CallBase *Call,
// Make sure the object has not escaped here, and then check that none of the
// call arguments alias the object below.
if (!isa<Constant>(Object) && Call != Object &&
AAQI.CI->isNotCapturedBeforeOrAt(Object, Call)) {
AAQI.CI->isNotCapturedBefore(Object, Call, /*OrAt*/ false)) {

// Optimistically assume that call doesn't touch Object and check this
// assumption in the following loop.
Expand Down Expand Up @@ -1512,10 +1525,10 @@ AliasResult BasicAAResult::aliasCheck(const Value *V1, LocationSize V1Size,
// location if that memory location doesn't escape. Or it may pass a
// nocapture value to other functions as long as they don't capture it.
if (isEscapeSource(O1) &&
AAQI.CI->isNotCapturedBeforeOrAt(O2, cast<Instruction>(O1)))
AAQI.CI->isNotCapturedBefore(O2, cast<Instruction>(O1), /*OrAt*/ true))
return AliasResult::NoAlias;
if (isEscapeSource(O2) &&
AAQI.CI->isNotCapturedBeforeOrAt(O1, cast<Instruction>(O2)))
AAQI.CI->isNotCapturedBefore(O1, cast<Instruction>(O2), /*OrAt*/ true))
return AliasResult::NoAlias;
}

Expand Down Expand Up @@ -1708,12 +1721,7 @@ bool BasicAAResult::isValueEqualInPotentialCycles(const Value *V,
if (!Inst || Inst->getParent()->isEntryBlock())
return true;

// Check whether the instruction is part of a cycle, by checking whether the
// block can (non-trivially) reach itself.
BasicBlock *BB = const_cast<BasicBlock *>(Inst->getParent());
SmallVector<BasicBlock *> Succs(successors(BB));
return !Succs.empty() &&
!isPotentiallyReachableFromMany(Succs, BB, nullptr, DT);
return isNotInCycle(Inst, *DT, /*LI*/ nullptr);
}

/// Computes the symbolic difference between two de-composed GEPs.
Expand Down
3 changes: 1 addition & 2 deletions llvm/test/Transforms/GVN/captured-before.ll
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ define i32 @test_capture_readonly() {
; CHECK-NEXT: [[A:%.*]] = alloca i32, align 4
; CHECK-NEXT: store i32 123, ptr [[A]], align 4
; CHECK-NEXT: call void @capture(ptr readonly [[A]])
; CHECK-NEXT: [[V:%.*]] = load i32, ptr [[A]], align 4
; CHECK-NEXT: ret i32 [[V]]
; CHECK-NEXT: ret i32 123
;
%a = alloca i32
store i32 123, ptr %a
Expand Down