aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Transforms/Utils/DemoteRegToStack.cpp
diff options
context:
space:
mode:
authorDimitry Andric <dim@FreeBSD.org>2023-02-11 12:38:04 +0000
committerDimitry Andric <dim@FreeBSD.org>2023-02-11 12:38:11 +0000
commite3b557809604d036af6e00c60f012c2025b59a5e (patch)
tree8a11ba2269a3b669601e2fd41145b174008f4da8 /llvm/lib/Transforms/Utils/DemoteRegToStack.cpp
parent08e8dd7b9db7bb4a9de26d44c1cbfd24e869c014 (diff)
Diffstat (limited to 'llvm/lib/Transforms/Utils/DemoteRegToStack.cpp')
-rw-r--r--llvm/lib/Transforms/Utils/DemoteRegToStack.cpp36
1 files changed, 28 insertions, 8 deletions
diff --git a/llvm/lib/Transforms/Utils/DemoteRegToStack.cpp b/llvm/lib/Transforms/Utils/DemoteRegToStack.cpp
index f6f80540ad95..086ea088dc5e 100644
--- a/llvm/lib/Transforms/Utils/DemoteRegToStack.cpp
+++ b/llvm/lib/Transforms/Utils/DemoteRegToStack.cpp
@@ -92,8 +92,15 @@ AllocaInst *llvm::DemoteRegToStack(Instruction &I, bool VolatileLoads,
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 (isa<CatchSwitchInst>(InsertPt)) {
+ for (BasicBlock *Handler : successors(&*InsertPt))
+ new StoreInst(&I, Slot, &*Handler->getFirstInsertionPt());
+ return Slot;
+ }
} else {
InvokeInst &II = cast<InvokeInst>(I);
InsertPt = II.getNormalDest()->getFirstInsertionPt();
@@ -138,14 +145,27 @@ AllocaInst *llvm::DemotePHIToStack(PHINode *P, Instruction *AllocaPoint) {
// 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 (isa<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;