Skip to content

Commit b22ffc7

Browse files
committed
[CaptureTracking] Ignore ephemeral values in EarliestEscapeInfo
And thread DSE's ephemeral values to EarliestEscapeInfo. This allows more precise analysis in DSEState::isReadClobber() via BatchAA. Followup to D123162. Reviewed By: nikic Differential Revision: https://reviews.llvm.org/D123342
1 parent e07dfa5 commit b22ffc7

File tree

6 files changed

+29
-16
lines changed

6 files changed

+29
-16
lines changed

llvm/include/llvm/Analysis/AliasAnalysis.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ class LoopInfo;
6363
class PreservedAnalyses;
6464
class TargetLibraryInfo;
6565
class Value;
66+
template <typename> class SmallPtrSetImpl;
6667

6768
/// The possible results of an alias query.
6869
///
@@ -412,8 +413,12 @@ class EarliestEscapeInfo final : public CaptureInfo {
412413
/// This is used for cache invalidation purposes.
413414
DenseMap<Instruction *, TinyPtrVector<const Value *>> Inst2Obj;
414415

416+
const SmallPtrSetImpl<const Value *> &EphValues;
417+
415418
public:
416-
EarliestEscapeInfo(DominatorTree &DT, const LoopInfo &LI) : DT(DT), LI(LI) {}
419+
EarliestEscapeInfo(DominatorTree &DT, const LoopInfo &LI,
420+
const SmallPtrSetImpl<const Value *> &EphValues)
421+
: DT(DT), LI(LI), EphValues(EphValues) {}
417422

418423
bool isNotCapturedBeforeOrAt(const Value *Object,
419424
const Instruction *I) override;

llvm/include/llvm/Analysis/CaptureTracking.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,11 @@ namespace llvm {
8080
// nullptr is returned. Note that the caller of the function has to ensure
8181
// that the instruction the result value is compared against is not in a
8282
// cycle.
83-
Instruction *FindEarliestCapture(const Value *V, Function &F,
84-
bool ReturnCaptures, bool StoreCaptures,
85-
const DominatorTree &DT,
86-
unsigned MaxUsesToExplore = 0);
83+
Instruction *
84+
FindEarliestCapture(const Value *V, Function &F, bool ReturnCaptures,
85+
bool StoreCaptures, const DominatorTree &DT,
86+
const SmallPtrSetImpl<const Value *> &EphValues,
87+
unsigned MaxUsesToExplore = 0);
8788

8889
/// This callback is used in conjunction with PointerMayBeCaptured. In
8990
/// addition to the interface here, you'll need to provide your own getters

llvm/lib/Analysis/BasicAliasAnalysis.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ bool EarliestEscapeInfo::isNotCapturedBeforeOrAt(const Value *Object,
232232
if (Iter.second) {
233233
Instruction *EarliestCapture = FindEarliestCapture(
234234
Object, *const_cast<Function *>(I->getFunction()),
235-
/*ReturnCaptures=*/false, /*StoreCaptures=*/true, DT);
235+
/*ReturnCaptures=*/false, /*StoreCaptures=*/true, DT, EphValues);
236236
if (EarliestCapture) {
237237
auto Ins = Inst2Obj.insert({EarliestCapture, {}});
238238
Ins.first->second.push_back(Object);

llvm/lib/Analysis/CaptureTracking.cpp

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,9 @@ namespace {
162162
// escape are not in a cycle.
163163
struct EarliestCaptures : public CaptureTracker {
164164

165-
EarliestCaptures(bool ReturnCaptures, Function &F, const DominatorTree &DT)
166-
: DT(DT), ReturnCaptures(ReturnCaptures), F(F) {}
165+
EarliestCaptures(bool ReturnCaptures, Function &F, const DominatorTree &DT,
166+
const SmallPtrSetImpl<const Value *> &EphValues)
167+
: EphValues(EphValues), DT(DT), ReturnCaptures(ReturnCaptures), F(F) {}
167168

168169
void tooManyUses() override {
169170
Captured = true;
@@ -175,6 +176,9 @@ namespace {
175176
if (isa<ReturnInst>(I) && !ReturnCaptures)
176177
return false;
177178

179+
if (EphValues.contains(I))
180+
return false;
181+
178182
if (!EarliestCapture) {
179183
EarliestCapture = I;
180184
} else if (EarliestCapture->getParent() == I->getParent()) {
@@ -201,6 +205,8 @@ namespace {
201205
return false;
202206
}
203207

208+
const SmallPtrSetImpl<const Value *> &EphValues;
209+
204210
Instruction *EarliestCapture = nullptr;
205211

206212
const DominatorTree &DT;
@@ -284,14 +290,16 @@ bool llvm::PointerMayBeCapturedBefore(const Value *V, bool ReturnCaptures,
284290
return CB.Captured;
285291
}
286292

287-
Instruction *llvm::FindEarliestCapture(const Value *V, Function &F,
288-
bool ReturnCaptures, bool StoreCaptures,
289-
const DominatorTree &DT,
290-
unsigned MaxUsesToExplore) {
293+
Instruction *
294+
llvm::FindEarliestCapture(const Value *V, Function &F, bool ReturnCaptures,
295+
bool StoreCaptures, const DominatorTree &DT,
296+
297+
const SmallPtrSetImpl<const Value *> &EphValues,
298+
unsigned MaxUsesToExplore) {
291299
assert(!isa<GlobalValue>(V) &&
292300
"It doesn't make sense to ask whether a global is captured.");
293301

294-
EarliestCaptures CB(ReturnCaptures, F, DT);
302+
EarliestCaptures CB(ReturnCaptures, F, DT, EphValues);
295303
PointerMayBeCaptured(V, &CB, MaxUsesToExplore);
296304
if (CB.Captured)
297305
++NumCapturedBefore;

llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -783,8 +783,8 @@ struct DSEState {
783783
DSEState(Function &F, AliasAnalysis &AA, MemorySSA &MSSA, DominatorTree &DT,
784784
PostDominatorTree &PDT, AssumptionCache &AC,
785785
const TargetLibraryInfo &TLI, const LoopInfo &LI)
786-
: F(F), AA(AA), EI(DT, LI), BatchAA(AA, &EI), MSSA(MSSA), DT(DT),
787-
PDT(PDT), TLI(TLI), DL(F.getParent()->getDataLayout()), LI(LI) {
786+
: F(F), AA(AA), EI(DT, LI, EphValues), BatchAA(AA, &EI), MSSA(MSSA),
787+
DT(DT), PDT(PDT), TLI(TLI), DL(F.getParent()->getDataLayout()), LI(LI) {
788788
// Collect blocks with throwing instructions not modeled in MemorySSA and
789789
// alloc-like objects.
790790
unsigned PO = 0;

llvm/test/Transforms/DeadStoreElimination/assume.ll

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ define void @f2() {
2222
; CHECK-NEXT: [[TMP1:%.*]] = call noalias i8* @_Znwm(i64 32)
2323
; CHECK-NEXT: [[TMP2:%.*]] = icmp ugt i8* [[TMP1]], @global
2424
; CHECK-NEXT: call void @llvm.assume(i1 [[TMP2]])
25-
; CHECK-NEXT: store i8 0, i8* [[TMP1]], align 1
2625
; CHECK-NEXT: call void @quux(i8* @global)
2726
; CHECK-NEXT: ret void
2827
;

0 commit comments

Comments
 (0)