[PATCH] D49156: [MemorySSA] Add API to update MemoryPhis, following CFG changes.

Alina Sbirlea via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 10 15:25:39 PDT 2018


asbirlea updated this revision to Diff 154899.
asbirlea added a comment.

Consider multiple edges between blocks. MemoryPhis have an entry for each edge in this case. Move all edges to the new phi to preserve number of edges equal to number of phi entries.


Repository:
  rL LLVM

https://reviews.llvm.org/D49156

Files:
  include/llvm/Analysis/MemorySSAUpdater.h
  lib/Analysis/MemorySSAUpdater.cpp


Index: lib/Analysis/MemorySSAUpdater.cpp
===================================================================
--- lib/Analysis/MemorySSAUpdater.cpp
+++ lib/Analysis/MemorySSAUpdater.cpp
@@ -494,6 +494,35 @@
   return MA;
 }
 
+void MemorySSAUpdater::wireOldPredecessorsToNewImmediatePredecessor(
+    BasicBlock *Old, BasicBlock *New, ArrayRef<BasicBlock *> Preds) {
+  assert(MSSA->getWritableBlockAccesses(New) == nullptr &&
+         "Access list should be null for a new block.");
+  MemoryPhi *Phi = MSSA->getMemoryAccess(Old);
+  if (!Phi)
+    return;
+  if (pred_size(Old) == 1 && pred_size(New) == Preds.size())
+    MSSA->moveTo(Phi, New, MemorySSA::Beginning);
+  else {
+    assert(Preds.size() > 0 && "Must be moving at least one predecessor to the "
+                               "new immediate predecessor.");
+    MemoryPhi *NewPhi = MSSA->createMemoryPhi(New);
+    for (auto *Pred : Preds) {
+      int Idx = Phi->getBasicBlockIndex(Pred);
+      assert(Idx >= 0 && "Predecessor must have an incoming value in Phi.");
+      // There may be multiple edges, in case of switches. Move all over.
+      while (Idx >= 0) {
+        NewPhi->addIncoming(Phi->getIncomingValue(Idx), Pred);
+        Phi->unorderedDeleteIncoming(Idx);
+        Idx = Phi->getBasicBlockIndex(Pred);
+      }
+    }
+    Phi->addIncoming(NewPhi, New);
+    if (onlySingleValue(NewPhi))
+      removeMemoryAccess(NewPhi);
+  }
+}
+
 void MemorySSAUpdater::removeMemoryAccess(MemoryAccess *MA) {
   assert(!MSSA->isLiveOnEntryDef(MA) &&
          "Trying to remove the live on entry def");
Index: include/llvm/Analysis/MemorySSAUpdater.h
===================================================================
--- include/llvm/Analysis/MemorySSAUpdater.h
+++ include/llvm/Analysis/MemorySSAUpdater.h
@@ -120,6 +120,14 @@
   /// |------|        |------|
   void moveAllAfterMergeBlocks(BasicBlock *From, BasicBlock *To,
                                Instruction *Start);
+  /// BasicBlock Old had New, an empty BasicBlock, added directly before it,
+  /// and the predecessors in Preds that used to point to Old, now point to
+  /// New. If New is the only predecessor, move Old's Phi, if present, to New.
+  /// Otherwise, add a new Phi in New with appropriate incoming values, and
+  /// update the incoming values in Old's Phi node too, if present.
+  void
+  wireOldPredecessorsToNewImmediatePredecessor(BasicBlock *Old, BasicBlock *New,
+                                               ArrayRef<BasicBlock *> Preds);
 
   // The below are utility functions. Other than creation of accesses to pass
   // to insertDef, and removeAccess to remove accesses, you should generally


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D49156.154899.patch
Type: text/x-patch
Size: 2667 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180710/b14b7820/attachment-0001.bin>


More information about the llvm-commits mailing list