@@ -347,51 +347,54 @@ void MemorySSAUpdater::insertDef(MemoryDef *MD, bool RenameUses) {
347
347
348
348
// If this is the first def in the block and this insert is in an arbitrary
349
349
// 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.
350
354
auto Iter = MD->getDefsIterator ();
351
355
++Iter;
352
356
auto IterEnd = MSSA->getBlockDefs (MD->getBlock ())->end ();
353
- if (Iter == IterEnd) {
354
- SmallPtrSet<BasicBlock *, 2 > DefiningBlocks;
357
+ if (Iter == IterEnd)
355
358
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);
377
373
}
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);
385
387
}
388
+ }
386
389
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);
394
396
}
397
+
395
398
FixupList.push_back (MD);
396
399
}
397
400
0 commit comments