[llvm] [WinEHPrepare] Preserve PHI values introduced by spill stores (PR #230942)
via llvm-commits
llvm-commits at lists.llvm.org
Sun Oct 11 15:26:04 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-backend-webassembly
Author: Colen Garoutte-Carson (Colengc)
<details>
<summary>Changes</summary>
Fixes: https://github.com/llvm/llvm-project/issues/230940
Nested catch/rethrow can lose an EH PHI value during WinEHPrepare. For example,
an inner catch records the iteration that threw and rethrows to an outer catch;
the recorded value remains -1 instead of the throwing iteration's index.
The failure reproduces on x64 Windows with clang-cl 23.1.3 and with unpatched
LLVM main at 25a8bb510a51.
insertPHILoads skips EH-pad PHI users, but demoting another PHI can subsequently
introduce an ordinary store using the skipped PHI. Final removal then replaces
that store's operand with poison. Collect the PHIs first and revisit them while
new spill stores are introduced, retaining each spill slot and inserting its
stores only once. Reload values used by stores at block entry so cyclic PHI
dependencies read the current value before stores for a subsequent EH transfer
overwrite the source slot.
Add regressions for nested rethrow, reordered blocks, an inner PHI with an
existing ordinary use, and a cycle through catchret and normal re-entry.
Validation:
- Built clang-cl, opt, llc, and FileCheck in Release mode with assertions
enabled and X86.
- New regression checks fail with unpatched LLVM main and pass with the fix.
- All 21 llvm/test/CodeGen/WinEH tests pass.
- Two standalone C++ reductions pass at all four throw positions using the
original optimized IR emitted as an object by patched llc and linked with
the MSVC runtime.
- The standalone C++ repro passes with the complete patched clang-cl at /O2.
The full LLVM suite was not run.
Assisted-by: OpenAI Codex
---
Patch is 21.38 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/230942.diff
4 Files Affected:
- (modified) llvm/lib/CodeGen/WinEHPrepare.cpp (+82-54)
- (modified) llvm/test/CodeGen/WebAssembly/wineh-prepare-wasm-personality.ll (+1-1)
- (added) llvm/test/CodeGen/WinEH/wineh-demotion-snapshot.ll (+47)
- (modified) llvm/test/CodeGen/WinEH/wineh-demotion.ll (+230)
``````````diff
diff --git a/llvm/lib/CodeGen/WinEHPrepare.cpp b/llvm/lib/CodeGen/WinEHPrepare.cpp
index e31982b2e416d..c59853d9c1228 100644
--- a/llvm/lib/CodeGen/WinEHPrepare.cpp
+++ b/llvm/lib/CodeGen/WinEHPrepare.cpp
@@ -19,6 +19,7 @@
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/MapVector.h"
#include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/CodeGen/MachineBasicBlock.h"
#include "llvm/CodeGen/Passes.h"
#include "llvm/CodeGen/WinEHFuncInfo.h"
@@ -116,13 +117,12 @@ class WinEHPrepareImpl {
bool runOnFunction(Function &Fn);
private:
- void insertPHIStores(PHINode *OriginalPHI, AllocaInst *SpillSlot);
void
- insertPHIStore(BasicBlock *PredBlock, Value *PredVal, AllocaInst *SpillSlot,
- SmallVectorImpl<std::pair<BasicBlock *, Value *>> &Worklist);
- AllocaInst *insertPHILoads(PHINode *PN, Function &F);
- void replaceUseWithLoad(Value *V, Use &U, AllocaInst *&SpillSlot,
- DenseMap<BasicBlock *, Value *> &Loads, Function &F);
+ collectPHIStores(PHINode *OriginalPHI,
+ SmallVectorImpl<std::pair<BasicBlock *, Value *>> &Stores);
+ void insertPHILoads(PHINode *PN, AllocaInst *SpillSlot);
+ void replaceUseWithLoad(Value *V, Use &U, AllocaInst *SpillSlot,
+ DenseMap<BasicBlock *, Value *> &Loads);
bool prepareExplicitEH(Function &F);
void colorFunclets(Function &F);
@@ -922,8 +922,6 @@ void WinEHPrepareImpl::colorFunclets(Function &F) {
bool WinEHPrepareImpl::demotePHIsOnFunclets(Function &F,
bool DemoteCatchSwitchPHIOnly) {
- bool Changed = false;
-
// Strip PHI nodes off of EH pads.
SmallVector<PHINode *, 16> PHINodes;
for (BasicBlock &BB : make_early_inc_range(F)) {
@@ -953,23 +951,69 @@ bool WinEHPrepareImpl::demotePHIsOnFunclets(Function &F,
break;
}
- Changed = true;
-
- AllocaInst *SpillSlot = insertPHILoads(PN, F);
- if (SpillSlot)
- insertPHIStores(PN, SpillSlot);
-
PHINodes.push_back(PN);
}
}
+ // Find the PHIs that need spill slots before changing any uses. A PHI on a
+ // non-terminator pad needs a reload in its own block. A catchswitch PHI only
+ // needs a slot if it has a use outside another EH PHI, including uses in the
+ // stores that will be inserted when spilling other PHIs.
+ SmallPtrSet<PHINode *, 16> PHISet(PHINodes.begin(), PHINodes.end());
+ DenseMap<PHINode *, SmallVector<std::pair<BasicBlock *, Value *>, 4>>
+ PHIStores;
+ SmallVector<PHINode *, 16> Worklist;
+ for (PHINode *PN : PHINodes) {
+ if (!PN->getParent()->getFirstNonPHIIt()->isTerminator() ||
+ llvm::any_of(PN->users(), [](User *U) {
+ auto *UsingPHI = dyn_cast<PHINode>(U);
+ return !UsingPHI || !UsingPHI->getParent()->isEHPad();
+ })) {
+ PHIStores.try_emplace(PN);
+ Worklist.push_back(PN);
+ }
+ }
+ while (!Worklist.empty()) {
+ PHINode *PN = Worklist.pop_back_val();
+ SmallVector<std::pair<BasicBlock *, Value *>, 4> Stores;
+ collectPHIStores(PN, Stores);
+ for (const auto &Store : Stores) {
+ auto *IncomingPHI = dyn_cast<PHINode>(Store.second);
+ if (IncomingPHI && PHISet.contains(IncomingPHI) &&
+ PHIStores.try_emplace(IncomingPHI).second)
+ Worklist.push_back(IncomingPHI);
+ }
+ PHIStores[PN] = std::move(Stores);
+ }
+
+ // Create all slots and stores before replacing uses with loads, so that a
+ // store of another EH PHI is handled just like any other use of that PHI.
+ DenseMap<PHINode *, AllocaInst *> SpillSlots;
+ for (PHINode *PN : PHINodes) {
+ if (!PHIStores.contains(PN))
+ continue;
+ SpillSlots[PN] = new AllocaInst(
+ PN->getType(), DL->getAllocaAddrSpace(), nullptr,
+ Twine(PN->getName(), ".wineh.spillslot"), F.getEntryBlock().begin());
+ }
+ for (PHINode *PN : PHINodes) {
+ AllocaInst *SpillSlot = SpillSlots.lookup(PN);
+ if (!SpillSlot)
+ continue;
+ for (auto [BB, V] : PHIStores[PN])
+ new StoreInst(V, SpillSlot, BB->getTerminator()->getIterator());
+ }
+ for (PHINode *PN : PHINodes)
+ if (AllocaInst *SpillSlot = SpillSlots.lookup(PN))
+ insertPHILoads(PN, SpillSlot);
+
for (auto *PN : PHINodes) {
// There may be lingering uses on other EH PHIs being removed
PN->replaceAllUsesWith(PoisonValue::get(PN->getType()));
PN->eraseFromParent();
}
- return Changed;
+ return !PHINodes.empty();
}
bool WinEHPrepareImpl::removeMalformedCatchswitch(Value *FuncletToken) {
@@ -1378,22 +1422,18 @@ bool WinEHPrepareImpl::prepareExplicitEH(Function &F) {
// TODO: Share loads when one use dominates another, or when a catchpad exit
// dominates uses (needs dominators).
-AllocaInst *WinEHPrepareImpl::insertPHILoads(PHINode *PN, Function &F) {
+void WinEHPrepareImpl::insertPHILoads(PHINode *PN, AllocaInst *SpillSlot) {
BasicBlock *PHIBlock = PN->getParent();
- AllocaInst *SpillSlot = nullptr;
Instruction *EHPad = &*PHIBlock->getFirstNonPHIIt();
if (!EHPad->isTerminator()) {
// If the EHPad isn't a terminator, then we can insert a load in this block
// that will dominate all uses.
- SpillSlot = new AllocaInst(PN->getType(), DL->getAllocaAddrSpace(), nullptr,
- Twine(PN->getName(), ".wineh.spillslot"),
- F.getEntryBlock().begin());
Value *V = new LoadInst(PN->getType(), SpillSlot,
Twine(PN->getName(), ".wineh.reload"),
PHIBlock->getFirstInsertionPt());
PN->replaceAllUsesWith(V);
- return SpillSlot;
+ return;
}
// Otherwise, we have a PHI on a terminator EHPad, and we give up and insert
@@ -1406,23 +1446,30 @@ AllocaInst *WinEHPrepareImpl::insertPHILoads(PHINode *PN, Function &F) {
// stores for it separately.
continue;
}
- replaceUseWithLoad(PN, U, SpillSlot, Loads, F);
+ replaceUseWithLoad(PN, U, SpillSlot, Loads);
}
- return SpillSlot;
}
// TODO: improve store placement. Inserting at def is probably good, but need
// to be careful not to introduce interfering stores (needs liveness analysis).
// TODO: identify related phi nodes that can share spill slots, and share them
// (also needs liveness).
-void WinEHPrepareImpl::insertPHIStores(PHINode *OriginalPHI,
- AllocaInst *SpillSlot) {
+void WinEHPrepareImpl::collectPHIStores(
+ PHINode *OriginalPHI,
+ SmallVectorImpl<std::pair<BasicBlock *, Value *>> &Stores) {
// Use a worklist of (Block, Value) pairs -- the given Value needs to be
// stored to the spill slot by the end of the given Block.
SmallVector<std::pair<BasicBlock *, Value *>, 4> Worklist;
Worklist.push_back({OriginalPHI->getParent(), OriginalPHI});
+ auto CollectStore = [&](BasicBlock *BB, Value *V) {
+ if (BB->isEHPad() && BB->getFirstNonPHIIt()->isTerminator())
+ Worklist.push_back({BB, V});
+ else
+ Stores.push_back({BB, V});
+ };
+
while (!Worklist.empty()) {
BasicBlock *EHBlock;
Value *InVal;
@@ -1440,41 +1487,21 @@ void WinEHPrepareImpl::insertPHIStores(PHINode *OriginalPHI,
if (isa<UndefValue>(PredVal))
continue;
- insertPHIStore(PN->getIncomingBlock(i), PredVal, SpillSlot, Worklist);
+ CollectStore(PN->getIncomingBlock(i), PredVal);
}
} else {
// We need to store InVal, which dominates EHBlock, but can't put a store
// in EHBlock, so need to put stores in each predecessor.
for (BasicBlock *PredBlock : predecessors(EHBlock)) {
- insertPHIStore(PredBlock, InVal, SpillSlot, Worklist);
+ CollectStore(PredBlock, InVal);
}
}
}
}
-void WinEHPrepareImpl::insertPHIStore(
- BasicBlock *PredBlock, Value *PredVal, AllocaInst *SpillSlot,
- SmallVectorImpl<std::pair<BasicBlock *, Value *>> &Worklist) {
-
- if (PredBlock->isEHPad() && PredBlock->getFirstNonPHIIt()->isTerminator()) {
- // Pred is unsplittable, so we need to queue it on the worklist.
- Worklist.push_back({PredBlock, PredVal});
- return;
- }
-
- // Otherwise, insert the store at the end of the basic block.
- new StoreInst(PredVal, SpillSlot, PredBlock->getTerminator()->getIterator());
-}
-
void WinEHPrepareImpl::replaceUseWithLoad(
- Value *V, Use &U, AllocaInst *&SpillSlot,
- DenseMap<BasicBlock *, Value *> &Loads, Function &F) {
- // Lazilly create the spill slot.
- if (!SpillSlot)
- SpillSlot = new AllocaInst(V->getType(), DL->getAllocaAddrSpace(), nullptr,
- Twine(V->getName(), ".wineh.spillslot"),
- F.getEntryBlock().begin());
-
+ Value *V, Use &U, AllocaInst *SpillSlot,
+ DenseMap<BasicBlock *, Value *> &Loads) {
auto *UsingInst = cast<Instruction>(U.getUser());
if (auto *UsingPHI = dyn_cast<PHINode>(UsingInst)) {
// If this is a PHI node, we can't insert a load of the value before
@@ -1532,14 +1559,15 @@ void WinEHPrepareImpl::replaceUseWithLoad(
if (!Load)
Load = new LoadInst(
V->getType(), SpillSlot, Twine(V->getName(), ".wineh.reload"),
- /*isVolatile=*/false, IncomingBlock->getTerminator()->getIterator());
+ /*isVolatile=*/false, IncomingBlock->getFirstInsertionPt());
U.set(Load);
} else {
- // Reload right before the old use.
- auto *Load = new LoadInst(V->getType(), SpillSlot,
- Twine(V->getName(), ".wineh.reload"),
- /*isVolatile=*/false, UsingInst->getIterator());
+ // PHI stores can overwrite the spill slot at the end of this block. Load
+ // the value on entry, before any of those stores can overwrite it.
+ auto *Load = new LoadInst(
+ V->getType(), SpillSlot, Twine(V->getName(), ".wineh.reload"),
+ /*isVolatile=*/false, UsingInst->getParent()->getFirstInsertionPt());
U.set(Load);
}
}
diff --git a/llvm/test/CodeGen/WebAssembly/wineh-prepare-wasm-personality.ll b/llvm/test/CodeGen/WebAssembly/wineh-prepare-wasm-personality.ll
index 53bde9cd9b8fd..7fe4386c0b5ad 100644
--- a/llvm/test/CodeGen/WebAssembly/wineh-prepare-wasm-personality.ll
+++ b/llvm/test/CodeGen/WebAssembly/wineh-prepare-wasm-personality.ll
@@ -43,9 +43,9 @@ define void @wasm_phi() personality ptr @__gxx_wasm_personality_v0 {
; CHECK-NEXT: [[CS2:%.*]] = catchswitch within none [label %[[CATCH_START6:.*]]] unwind to caller
; CHECK: [[CATCH_START6]]:
; CHECK-NEXT: [[CP2:%.*]] = catchpad within [[CS2]] [ptr null]
+; CHECK-NEXT: [[NUM_1_WINEH_RELOAD:%.*]] = load i32, ptr [[NUM_1_WINEH_SPILLSLOT]], align 4
; CHECK-NEXT: [[EXN2:%.*]] = call ptr @llvm.wasm.get.exception(token [[CP2]])
; CHECK-NEXT: [[SEL2:%.*]] = call i32 @llvm.wasm.get.ehselector(token [[CP2]])
-; CHECK-NEXT: [[NUM_1_WINEH_RELOAD:%.*]] = load i32, ptr [[NUM_1_WINEH_SPILLSLOT]], align 4
; CHECK-NEXT: call void @bar(i32 [[NUM_1_WINEH_RELOAD]]) [ "funclet"(token [[CP2]]) ]
; CHECK-NEXT: catchret from [[CP2]] to label %[[TRY_CONT10]]
; CHECK: [[TRY_CONT10]]:
diff --git a/llvm/test/CodeGen/WinEH/wineh-demotion-snapshot.ll b/llvm/test/CodeGen/WinEH/wineh-demotion-snapshot.ll
new file mode 100644
index 0000000000000..a61d8750aab0d
--- /dev/null
+++ b/llvm/test/CodeGen/WinEH/wineh-demotion-snapshot.ll
@@ -0,0 +1,47 @@
+; RUN: opt -mtriple=x86_64-pc-windows-msvc -S -win-eh-prepare < %s | FileCheck %s
+; RUN: opt -mtriple=x86_64-pc-windows-msvc -S -passes=win-eh-prepare < %s | FileCheck %s
+
+declare i32 @__CxxFrameHandler3(...)
+declare void @f()
+declare void @h(i32)
+
+; Both the invoke argument and the PHI on its normal successor use the current
+; value of the EH PHI. Reload it before the spill store for the next unwind
+; overwrites its slot, even though both uses occur at the end of reenter.
+; CHECK-LABEL: @snapshot_before_unwind_store(
+define void @snapshot_before_unwind_store() personality ptr @__CxxFrameHandler3 {
+entry:
+ ; CHECK: entry:
+ ; CHECK: [[Slot:%[^ ]+]] = alloca i32
+ ; CHECK-NEXT: store i32 0, ptr [[Slot]]
+ ; CHECK-NEXT: invoke void @f()
+ invoke void @f()
+ to label %normal unwind label %dispatch
+
+dispatch:
+ ; CHECK: dispatch:
+ ; CHECK-NEXT: %cs = catchswitch
+ %old = phi i32 [ 0, %entry ], [ 1, %reenter ]
+ %cs = catchswitch within none [label %catch] unwind to caller
+
+catch:
+ %cp = catchpad within %cs [ptr null, i32 64, ptr null]
+ catchret from %cp to label %reenter
+
+reenter:
+ ; CHECK: reenter:
+ ; CHECK-NEXT: load i32, ptr [[Slot]]
+ ; CHECK-NEXT: load i32, ptr [[Slot]]
+ ; CHECK-NEXT: store i32 1, ptr [[Slot]]
+ ; CHECK-NEXT: invoke void @h(i32 %{{old.wineh.reload[0-9]*}})
+ invoke void @h(i32 %old)
+ to label %normal unwind label %dispatch
+
+normal:
+ ; CHECK: normal:
+ ; CHECK-NEXT: %result = phi i32 [ 7, %entry ], [ %{{old.wineh.reload[0-9]*}}, %reenter ]
+ ; CHECK-NEXT: call void @h(i32 %result)
+ %result = phi i32 [ 7, %entry ], [ %old, %reenter ]
+ call void @h(i32 %result)
+ ret void
+}
diff --git a/llvm/test/CodeGen/WinEH/wineh-demotion.ll b/llvm/test/CodeGen/WinEH/wineh-demotion.ll
index 39ac51adeef23..d488543dcd141 100644
--- a/llvm/test/CodeGen/WinEH/wineh-demotion.ll
+++ b/llvm/test/CodeGen/WinEH/wineh-demotion.ll
@@ -357,3 +357,233 @@ cleanupexit:
call void @llvm.bar()
cleanupret from %cp0 unwind label %cleanup2
}
+
+; Demoting the outer PHI inserts a store of the inner PHI in catch.inner.
+; That new use must be demoted too, even if the inner PHI was visited first.
+; CHECK-LABEL: @rethrow_phi(
+define void @rethrow_phi() personality ptr @__CxxFrameHandler3 {
+entry:
+ ; CHECK: entry:
+ ; CHECK-DAG: [[InnerSlot:%phi.inner.wineh.spillslot]] = alloca i32
+ ; CHECK-DAG: [[OuterSlot:%phi.outer.wineh.spillslot]] = alloca i32
+ ; CHECK-DAG: store i32 0, ptr [[InnerSlot]]
+ ; CHECK-DAG: store i32 -1, ptr [[OuterSlot]]
+ ; CHECK: invoke void @f()
+ invoke void @f()
+ to label %second unwind label %dispatch.inner
+
+second:
+ ; CHECK: second:
+ ; CHECK-DAG: store i32 1, ptr [[InnerSlot]]
+ ; CHECK-DAG: store i32 -1, ptr [[OuterSlot]]
+ ; CHECK: invoke void @f()
+ invoke void @f()
+ to label %exit unwind label %dispatch.inner
+
+dispatch.inner:
+ ; CHECK: dispatch.inner:
+ ; CHECK-NEXT: %cs.inner = catchswitch
+ %phi.inner = phi i32 [ 0, %entry ], [ 1, %second ]
+ %cs.inner = catchswitch within none [label %catch.inner] unwind label %dispatch.outer
+
+catch.inner:
+ ; CHECK: catch.inner:
+ ; CHECK-NEXT: %cp.inner = catchpad
+ ; CHECK-NEXT: [[InnerReload:%[^ ]+]] = load i32, ptr [[InnerSlot]]
+ ; CHECK-NEXT: store i32 [[InnerReload]], ptr [[OuterSlot]]
+ ; CHECK-NEXT: invoke void @f()
+ %cp.inner = catchpad within %cs.inner [ptr null, i32 64, ptr null]
+ invoke void @f() [ "funclet"(token %cp.inner) ]
+ to label %unreachable unwind label %dispatch.outer
+
+unreachable:
+ unreachable
+
+dispatch.outer:
+ ; CHECK: dispatch.outer:
+ ; CHECK-NEXT: %cs.outer = catchswitch
+ %phi.outer = phi i32 [ -1, %dispatch.inner ], [ %phi.inner, %catch.inner ]
+ %cs.outer = catchswitch within none [label %catch.outer] unwind to caller
+
+catch.outer:
+ ; CHECK: catch.outer:
+ ; CHECK-NEXT: %cp.outer = catchpad
+ ; CHECK-NEXT: [[OuterReload:%[^ ]+]] = load i32, ptr [[OuterSlot]]
+ ; CHECK-NEXT: call void @h(i32 [[OuterReload]])
+ %cp.outer = catchpad within %cs.outer [ptr null, i32 0, ptr null]
+ call void @h(i32 %phi.outer) [ "funclet"(token %cp.outer) ]
+ catchret from %cp.outer to label %exit
+
+exit:
+ ret void
+}
+
+; Changing the block order must not change how the PHIs are demoted.
+; CHECK-LABEL: @rethrow_phi_reordered(
+define void @rethrow_phi_reordered() personality ptr @__CxxFrameHandler3 {
+entry:
+ ; CHECK: entry:
+ ; CHECK-DAG: [[InnerSlot:%phi.inner.wineh.spillslot]] = alloca i32
+ ; CHECK-DAG: [[OuterSlot:%phi.outer.wineh.spillslot]] = alloca i32
+ ; CHECK-DAG: store i32 0, ptr [[InnerSlot]]
+ ; CHECK-DAG: store i32 -1, ptr [[OuterSlot]]
+ ; CHECK: invoke void @f()
+ invoke void @f()
+ to label %second unwind label %dispatch.inner
+
+second:
+ ; CHECK: second:
+ ; CHECK-DAG: store i32 1, ptr [[InnerSlot]]
+ ; CHECK-DAG: store i32 -1, ptr [[OuterSlot]]
+ ; CHECK: invoke void @f()
+ invoke void @f()
+ to label %exit unwind label %dispatch.inner
+
+dispatch.outer:
+ ; CHECK: dispatch.outer:
+ ; CHECK-NEXT: %cs.outer = catchswitch
+ %phi.outer = phi i32 [ -1, %dispatch.inner ], [ %phi.inner, %catch.inner ]
+ %cs.outer = catchswitch within none [label %catch.outer] unwind to caller
+
+catch.outer:
+ ; CHECK: catch.outer:
+ ; CHECK-NEXT: %cp.outer = catchpad
+ ; CHECK-NEXT: [[OuterReload:%[^ ]+]] = load i32, ptr [[OuterSlot]]
+ ; CHECK-NEXT: call void @h(i32 [[OuterReload]])
+ %cp.outer = catchpad within %cs.outer [ptr null, i32 0, ptr null]
+ call void @h(i32 %phi.outer) [ "funclet"(token %cp.outer) ]
+ catchret from %cp.outer to label %exit
+
+dispatch.inner:
+ ; CHECK: dispatch.inner:
+ ; CHECK-NEXT: %cs.inner = catchswitch
+ %phi.inner = phi i32 [ 0, %entry ], [ 1, %second ]
+ %cs.inner = catchswitch within none [label %catch.inner] unwind label %dispatch.outer
+
+catch.inner:
+ ; CHECK: catch.inner:
+ ; CHECK-NEXT: %cp.inner = catchpad
+ ; CHECK-NEXT: [[InnerReload:%[^ ]+]] = load i32, ptr [[InnerSlot]]
+ ; CHECK-NEXT: store i32 [[InnerReload]], ptr [[OuterSlot]]
+ ; CHECK-NEXT: invoke void @f()
+ %cp.inner = catchpad within %cs.inner [ptr null, i32 64, ptr null]
+ invoke void @f() [ "funclet"(token %cp.inner) ]
+ to label %unreachable unwind label %dispatch.outer
+
+unreachable:
+ unreachable
+
+exit:
+ ret void
+}
+
+; A PHI with both ordinary and EH PHI uses needs only one spill slot.
+; CHECK-LABEL: @rethrow_phi_used(
+define void @rethrow_phi_used() personality ptr @__CxxFrameHandler3 {
+entry:
+ ; CHECK: entry:
+ ; CHECK-DAG: [[InnerSlot:%phi.inner.wineh.spillslot]] = alloca i32
+ ; CHECK-DAG: [[OuterSlot:%phi.outer.wineh.spillslot]] = alloca i32
+ ; CHECK-DAG: store i32 0, ptr [[InnerSlot]]
+ ; CHECK-DAG: store i32 -1, ptr [[OuterSlot]]
+ ; CHECK: invoke void @f()
+ invoke void @f()
+ to label %second unwind label %dispatch.inner
+
+second:
+ ; CHECK: second:
+ ; CHECK-DAG: store i32 1, ptr [[InnerSlot]]
+ ; CHECK-DAG: store i32 -1, ptr [[OuterSlot]]
+ ; CHECK: invoke void @f()
+ invoke void @f()
+ to label %exit unwind label %dispatch.inner
+
+dispatch.inner:
+ %phi.inner = phi i32 [ 0, %entry ], [ 1, %second ]
+ %cs.inner = catchswitch within none [label %catch.inner] unwind label %dispatch.outer
+
+catch.inner:
+ ; CHECK: catch.inner:
+ ; CHECK-NEXT: %cp.inner = catchpad
+ ; CHECK-NEXT: [[InnerReload:%[^ ]+]] = load i32, ptr [[InnerSlot]]
+ ; CHECK-NEXT: [[StoreReload:%[^ ]+]] = load i32, ptr [[InnerSlot]]
+ ; CHECK-NEXT: call void @h(i32 [[InnerReload]])
+ ; CHECK-NEXT: store i32 [[StoreReload]], ptr [[OuterSlot]]
+ ; CHECK-NEXT: invoke void @f()
+ %cp.inner = catchpad within %cs.inner [ptr null, i32 64, ptr null]
+ call void @h(i32 %phi.inner) [ "funclet"(token %cp.inner) ]
+ invoke void @f() [ "funclet"(token %cp.inner) ]
+ to label %unreachable unwind label %dispatch.outer
+
+unreachable:
+ unreachable
+
+dispatch.outer:
+ %phi.outer = phi i32 [ -1, %dispatch.inner ], [ %phi.inner, %catch.inner ]
+ %cs.outer = catchswitch within none [label %catch.outer] unwind to caller
+
+catch.outer:
+ ; CHECK: catch.outer:
+ ; CHECK-NEXT: %cp.outer = catchpad
+ ; CHECK-NEXT: [[OuterReload:%[^ ]+]] = load i32, ptr [[OuterSlot]]
+ ; CHECK-NEXT: call void @h(i32 [[OuterReload]])
+ %cp.outer = catchpad within %cs.outer [ptr null, i32 0, ptr null]
+ call void @h(i32 %phi.outer) [ "funclet"(token %cp.outer) ]
+ catchret from %cp.outer to label %exit
+
+exit:
+ ret void
+}
+
+; EH PHIs can depend on each other through a catchret back to the root funclet.
+; Snapshot the outer PHI before overwriting its spill slot on the backedge.
+; CHECK-LABEL: @rethrow_phi_cycle(
+define void @rethrow_phi_cycle() personality ptr @__CxxFrameHandler3 {
+entry:
+ ; CHECK: entry:
+ ; CHECK-DAG: [[InnerSlot:%phi.inner.wineh.spillslot]] = alloca i32
+ ; CHECK-DAG: [[OuterSlot:%phi.outer.wineh.spillslot]] = alloca i32
+ ; CHECK-DAG: store i32 0, ptr [[InnerSlot]]
+ ; CHECK-DAG: store i32 -1, ptr [[OuterSlot]]
+ ; CHECK: invoke void @f(...
[truncated]
``````````
</details>
https://github.com/llvm/llvm-project/pull/230942
More information about the llvm-commits
mailing list