Skip to content

Commit 24ae5ce

Browse files
committed
[MemorySSA] Update Phi creation when inserting a Def.
MemoryPhis should be added in the IDF of the blocks newly gaining Defs. This includes the blocks that gained a Phi and the block gaining a Def, if the block did not have one before. Resolves PR43427. llvm-svn: 373505
1 parent 340406a commit 24ae5ce

File tree

2 files changed

+82
-37
lines changed

2 files changed

+82
-37
lines changed

llvm/lib/Analysis/MemorySSAUpdater.cpp

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -347,51 +347,54 @@ void MemorySSAUpdater::insertDef(MemoryDef *MD, bool RenameUses) {
347347

348348
// If this is the first def in the block and this insert is in an arbitrary
349349
// place, compute IDF and place phis.
350+
SmallPtrSet<BasicBlock *, 2> DefiningBlocks;
351+
352+
// If this is the last Def in the block, also compute IDF based on MD, since
353+
// this may a new Def added, and we may need additional Phis.
350354
auto Iter = MD->getDefsIterator();
351355
++Iter;
352356
auto IterEnd = MSSA->getBlockDefs(MD->getBlock())->end();
353-
if (Iter == IterEnd) {
354-
SmallPtrSet<BasicBlock *, 2> DefiningBlocks;
357+
if (Iter == IterEnd)
355358
DefiningBlocks.insert(MD->getBlock());
356-
for (const auto &VH : InsertedPHIs)
357-
if (const auto *RealPHI = cast_or_null<MemoryPhi>(VH))
358-
DefiningBlocks.insert(RealPHI->getBlock());
359-
ForwardIDFCalculator IDFs(*MSSA->DT);
360-
SmallVector<BasicBlock *, 32> IDFBlocks;
361-
IDFs.setDefiningBlocks(DefiningBlocks);
362-
IDFs.calculate(IDFBlocks);
363-
SmallVector<AssertingVH<MemoryPhi>, 4> NewInsertedPHIs;
364-
for (auto *BBIDF : IDFBlocks) {
365-
auto *MPhi = MSSA->getMemoryAccess(BBIDF);
366-
if (!MPhi) {
367-
MPhi = MSSA->createMemoryPhi(BBIDF);
368-
NewInsertedPHIs.push_back(MPhi);
369-
}
370-
// Add the phis created into the IDF blocks to NonOptPhis, so they are
371-
// not optimized out as trivial by the call to getPreviousDefFromEnd
372-
// below. Once they are complete, all these Phis are added to the
373-
// FixupList, and removed from NonOptPhis inside fixupDefs(). Existing
374-
// Phis in IDF may need fixing as well, and potentially be trivial
375-
// before this insertion, hence add all IDF Phis. See PR43044.
376-
NonOptPhis.insert(MPhi);
359+
360+
for (const auto &VH : InsertedPHIs)
361+
if (const auto *RealPHI = cast_or_null<MemoryPhi>(VH))
362+
DefiningBlocks.insert(RealPHI->getBlock());
363+
ForwardIDFCalculator IDFs(*MSSA->DT);
364+
SmallVector<BasicBlock *, 32> IDFBlocks;
365+
IDFs.setDefiningBlocks(DefiningBlocks);
366+
IDFs.calculate(IDFBlocks);
367+
SmallVector<AssertingVH<MemoryPhi>, 4> NewInsertedPHIs;
368+
for (auto *BBIDF : IDFBlocks) {
369+
auto *MPhi = MSSA->getMemoryAccess(BBIDF);
370+
if (!MPhi) {
371+
MPhi = MSSA->createMemoryPhi(BBIDF);
372+
NewInsertedPHIs.push_back(MPhi);
377373
}
378-
for (auto &MPhi : NewInsertedPHIs) {
379-
auto *BBIDF = MPhi->getBlock();
380-
for (auto *Pred : predecessors(BBIDF)) {
381-
DenseMap<BasicBlock *, TrackingVH<MemoryAccess>> CachedPreviousDef;
382-
MPhi->addIncoming(getPreviousDefFromEnd(Pred, CachedPreviousDef),
383-
Pred);
384-
}
374+
// Add the phis created into the IDF blocks to NonOptPhis, so they are not
375+
// optimized out as trivial by the call to getPreviousDefFromEnd below.
376+
// Once they are complete, all these Phis are added to the FixupList, and
377+
// removed from NonOptPhis inside fixupDefs(). Existing Phis in IDF may
378+
// need fixing as well, and potentially be trivial before this insertion,
379+
// hence add all IDF Phis. See PR43044.
380+
NonOptPhis.insert(MPhi);
381+
}
382+
for (auto &MPhi : NewInsertedPHIs) {
383+
auto *BBIDF = MPhi->getBlock();
384+
for (auto *Pred : predecessors(BBIDF)) {
385+
DenseMap<BasicBlock *, TrackingVH<MemoryAccess>> CachedPreviousDef;
386+
MPhi->addIncoming(getPreviousDefFromEnd(Pred, CachedPreviousDef), Pred);
385387
}
388+
}
386389

387-
// Re-take the index where we're adding the new phis, because the above
388-
// call to getPreviousDefFromEnd, may have inserted into InsertedPHIs.
389-
NewPhiIndex = InsertedPHIs.size();
390-
for (auto &MPhi : NewInsertedPHIs) {
391-
InsertedPHIs.push_back(&*MPhi);
392-
FixupList.push_back(&*MPhi);
393-
}
390+
// Re-take the index where we're adding the new phis, because the above call
391+
// to getPreviousDefFromEnd, may have inserted into InsertedPHIs.
392+
NewPhiIndex = InsertedPHIs.size();
393+
for (auto &MPhi : NewInsertedPHIs) {
394+
InsertedPHIs.push_back(&*MPhi);
395+
FixupList.push_back(&*MPhi);
394396
}
397+
395398
FixupList.push_back(MD);
396399
}
397400

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
; RUN: opt -disable-output -licm -print-memoryssa -enable-mssa-loop-dependency=true < %s 2>&1 | FileCheck %s
2+
3+
; CHECK-LABEL: @f()
4+
; CHECK: 8 = MemoryPhi(
5+
; CHECK: 7 = MemoryPhi(
6+
; CHECK: 9 = MemoryPhi(
7+
define void @f() {
8+
entry:
9+
%e = alloca i16, align 1
10+
br label %lbl1
11+
12+
lbl1: ; preds = %if.else, %cleanup, %entry
13+
store i16 undef, i16* %e, align 1
14+
call void @g()
15+
br i1 undef, label %for.end, label %if.else
16+
17+
for.end: ; preds = %lbl1
18+
br i1 undef, label %lbl3, label %lbl2
19+
20+
lbl2: ; preds = %lbl3, %for.end
21+
br label %lbl3
22+
23+
lbl3: ; preds = %lbl2, %for.end
24+
br i1 undef, label %lbl2, label %cleanup
25+
26+
cleanup: ; preds = %lbl3
27+
%cleanup.dest = load i32, i32* undef, align 1
28+
%switch = icmp ult i32 %cleanup.dest, 1
29+
br i1 %switch, label %cleanup.cont, label %lbl1
30+
31+
cleanup.cont: ; preds = %cleanup
32+
call void @llvm.lifetime.end.p0i8(i64 1, i8* null)
33+
ret void
34+
35+
if.else: ; preds = %lbl1
36+
br label %lbl1
37+
}
38+
39+
declare void @g()
40+
41+
; Function Attrs: argmemonly nounwind willreturn
42+
declare void @llvm.lifetime.end.p0i8(i64 immarg, i8* nocapture)

0 commit comments

Comments
 (0)