[PATCH] D138641: [reg2mem] Add special handling to CatchSwitchInst

Zhang via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 24 00:54:45 PST 2022


Naville updated this revision to Diff 477699.

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D138641/new/

https://reviews.llvm.org/D138641

Files:
  llvm/lib/Transforms/Utils/DemoteRegToStack.cpp


Index: llvm/lib/Transforms/Utils/DemoteRegToStack.cpp
===================================================================
--- llvm/lib/Transforms/Utils/DemoteRegToStack.cpp
+++ llvm/lib/Transforms/Utils/DemoteRegToStack.cpp
@@ -92,8 +92,19 @@
   BasicBlock::iterator InsertPt;
   if (!I.isTerminator()) {
     InsertPt = ++I.getIterator();
+    // Don't insert before PHI nodes or landingpad instrs.
     for (; isa<PHINode>(InsertPt) || InsertPt->isEHPad(); ++InsertPt)
-      /* empty */;   // Don't insert before PHI nodes or landingpad instrs.
+      if (isa<CatchSwitchInst>(InsertPt))
+        break;
+    if (CatchSwitchInst *CSI = dyn_cast<CatchSwitchInst>(InsertPt)) {
+      for (BasicBlock *Handler : CSI->handlers()) {
+        new StoreInst(&I, Slot, &*Handler->getFirstInsertionPt());
+      }
+      if (CSI->hasUnwindDest()) {
+        new StoreInst(&I, Slot, &*CSI->getUnwindDest()->getFirstInsertionPt());
+      }
+      return Slot;
+    }
   } else {
     InvokeInst &II = cast<InvokeInst>(I);
     InsertPt = II.getNormalDest()->getFirstInsertionPt();
@@ -138,14 +149,27 @@
 
   // Insert a load in place of the PHI and replace all uses.
   BasicBlock::iterator InsertPt = P->getIterator();
-
+  // Don't insert before PHI nodes or landingpad instrs.
   for (; isa<PHINode>(InsertPt) || InsertPt->isEHPad(); ++InsertPt)
-    /* empty */;   // Don't insert before PHI nodes or landingpad instrs.
-
-  Value *V =
-      new LoadInst(P->getType(), Slot, P->getName() + ".reload", &*InsertPt);
-  P->replaceAllUsesWith(V);
-
+    if (isa<CatchSwitchInst>(InsertPt))
+      break;
+  if (CatchSwitchInst *CSI = dyn_cast<CatchSwitchInst>(InsertPt)) {
+    // We need a separate load before each actual use of the PHI
+    SmallVector<Instruction *, 4> users;
+    for (User *U : P->users()) {
+      Instruction *User = cast<Instruction>(U);
+      users.push_back(User);
+    }
+    for (Instruction *User : users) {
+      Value *V =
+          new LoadInst(P->getType(), Slot, P->getName() + ".reload", User);
+      User->replaceUsesOfWith(P, V);
+    }
+  } else {
+    Value *V =
+        new LoadInst(P->getType(), Slot, P->getName() + ".reload", &*InsertPt);
+    P->replaceAllUsesWith(V);
+  }
   // Delete PHI.
   P->eraseFromParent();
   return Slot;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D138641.477699.patch
Type: text/x-patch
Size: 2278 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221124/c6f93b57/attachment.bin>


More information about the llvm-commits mailing list