llvm.org GIT mirror llvm / c1679d6
[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. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@373505 91177308-0d34-0410-b5e6-96231b3b80d8 Alina Sbirlea 1 year, 19 days ago
2 changed file(s) with 86 addition(s) and 41 deletion(s). Raw diff Collapse all Expand all
346346
347347 // If this is the first def in the block and this insert is in an arbitrary
348348 // place, compute IDF and place phis.
349 SmallPtrSet DefiningBlocks;
350
351 // If this is the last Def in the block, also compute IDF based on MD, since
352 // this may a new Def added, and we may need additional Phis.
349353 auto Iter = MD->getDefsIterator();
350354 ++Iter;
351355 auto IterEnd = MSSA->getBlockDefs(MD->getBlock())->end();
352 if (Iter == IterEnd) {
353 SmallPtrSet DefiningBlocks;
356 if (Iter == IterEnd)
354357 DefiningBlocks.insert(MD->getBlock());
355 for (const auto &VH : InsertedPHIs)
356 if (const auto *RealPHI = cast_or_null(VH))
357 DefiningBlocks.insert(RealPHI->getBlock());
358 ForwardIDFCalculator IDFs(*MSSA->DT);
359 SmallVector IDFBlocks;
360 IDFs.setDefiningBlocks(DefiningBlocks);
361 IDFs.calculate(IDFBlocks);
362 SmallVector, 4> NewInsertedPHIs;
363 for (auto *BBIDF : IDFBlocks) {
364 auto *MPhi = MSSA->getMemoryAccess(BBIDF);
365 if (!MPhi) {
366 MPhi = MSSA->createMemoryPhi(BBIDF);
367 NewInsertedPHIs.push_back(MPhi);
368 }
369 // Add the phis created into the IDF blocks to NonOptPhis, so they are
370 // not optimized out as trivial by the call to getPreviousDefFromEnd
371 // below. Once they are complete, all these Phis are added to the
372 // FixupList, and removed from NonOptPhis inside fixupDefs(). Existing
373 // Phis in IDF may need fixing as well, and potentially be trivial
374 // before this insertion, hence add all IDF Phis. See PR43044.
375 NonOptPhis.insert(MPhi);
376 }
377 for (auto &MPhi : NewInsertedPHIs) {
378 auto *BBIDF = MPhi->getBlock();
379 for (auto *Pred : predecessors(BBIDF)) {
380 DenseMap> CachedPreviousDef;
381 MPhi->addIncoming(getPreviousDefFromEnd(Pred, CachedPreviousDef),
382 Pred);
383 }
384 }
385
386 // Re-take the index where we're adding the new phis, because the above
387 // call to getPreviousDefFromEnd, may have inserted into InsertedPHIs.
388 NewPhiIndex = InsertedPHIs.size();
389 for (auto &MPhi : NewInsertedPHIs) {
390 InsertedPHIs.push_back(&*MPhi);
391 FixupList.push_back(&*MPhi);
392 }
393 }
358
359 for (const auto &VH : InsertedPHIs)
360 if (const auto *RealPHI = cast_or_null(VH))
361 DefiningBlocks.insert(RealPHI->getBlock());
362 ForwardIDFCalculator IDFs(*MSSA->DT);
363 SmallVector IDFBlocks;
364 IDFs.setDefiningBlocks(DefiningBlocks);
365 IDFs.calculate(IDFBlocks);
366 SmallVector, 4> NewInsertedPHIs;
367 for (auto *BBIDF : IDFBlocks) {
368 auto *MPhi = MSSA->getMemoryAccess(BBIDF);
369 if (!MPhi) {
370 MPhi = MSSA->createMemoryPhi(BBIDF);
371 NewInsertedPHIs.push_back(MPhi);
372 }
373 // Add the phis created into the IDF blocks to NonOptPhis, so they are not
374 // optimized out as trivial by the call to getPreviousDefFromEnd below.
375 // Once they are complete, all these Phis are added to the FixupList, and
376 // removed from NonOptPhis inside fixupDefs(). Existing Phis in IDF may
377 // need fixing as well, and potentially be trivial before this insertion,
378 // hence add all IDF Phis. See PR43044.
379 NonOptPhis.insert(MPhi);
380 }
381 for (auto &MPhi : NewInsertedPHIs) {
382 auto *BBIDF = MPhi->getBlock();
383 for (auto *Pred : predecessors(BBIDF)) {
384 DenseMap> CachedPreviousDef;
385 MPhi->addIncoming(getPreviousDefFromEnd(Pred, CachedPreviousDef), Pred);
386 }
387 }
388
389 // Re-take the index where we're adding the new phis, because the above call
390 // to getPreviousDefFromEnd, may have inserted into InsertedPHIs.
391 NewPhiIndex = InsertedPHIs.size();
392 for (auto &MPhi : NewInsertedPHIs) {
393 InsertedPHIs.push_back(&*MPhi);
394 FixupList.push_back(&*MPhi);
395 }
396
394397 FixupList.push_back(MD);
395398 }
396399
0 ; RUN: opt -disable-output -licm -print-memoryssa -enable-mssa-loop-dependency=true < %s 2>&1 | FileCheck %s
1
2 ; CHECK-LABEL: @f()
3 ; CHECK: 8 = MemoryPhi(
4 ; CHECK: 7 = MemoryPhi(
5 ; CHECK: 9 = MemoryPhi(
6 define void @f() {
7 entry:
8 %e = alloca i16, align 1
9 br label %lbl1
10
11 lbl1: ; preds = %if.else, %cleanup, %entry
12 store i16 undef, i16* %e, align 1
13 call void @g()
14 br i1 undef, label %for.end, label %if.else
15
16 for.end: ; preds = %lbl1
17 br i1 undef, label %lbl3, label %lbl2
18
19 lbl2: ; preds = %lbl3, %for.end
20 br label %lbl3
21
22 lbl3: ; preds = %lbl2, %for.end
23 br i1 undef, label %lbl2, label %cleanup
24
25 cleanup: ; preds = %lbl3
26 %cleanup.dest = load i32, i32* undef, align 1
27 %switch = icmp ult i32 %cleanup.dest, 1
28 br i1 %switch, label %cleanup.cont, label %lbl1
29
30 cleanup.cont: ; preds = %cleanup
31 call void @llvm.lifetime.end.p0i8(i64 1, i8* null)
32 ret void
33
34 if.else: ; preds = %lbl1
35 br label %lbl1
36 }
37
38 declare void @g()
39
40 ; Function Attrs: argmemonly nounwind willreturn
41 declare void @llvm.lifetime.end.p0i8(i64 immarg, i8* nocapture)