Skip to content

Commit c3a68c5

Browse files
committed
[SROA] Bail out on PHIs in catchswitch BBs
In the process of rewriting `alloca`s and `phi`s that use them, the SROA pass can try to insert a non-PHI instruction by calling `getFirstInsertionPt()`, which is not possible in a catchswitch BB. This CL makes we bail out on these cases. Reviewed By: dschuff Differential Revision: https://reviews.llvm.org/D117168
1 parent 636a1cf commit c3a68c5

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

llvm/lib/Transforms/Scalar/SROA.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,6 +1008,13 @@ class AllocaSlices::SliceBuilder : public PtrUseVisitor<SliceBuilder> {
10081008
if (I.use_empty())
10091009
return markAsDead(I);
10101010

1011+
// If this is a PHI node before a catchswitch, we cannot insert any non-PHI
1012+
// instructions in this BB, which may be required during rewriting. Bail out
1013+
// on these cases.
1014+
if (isa<PHINode>(I) &&
1015+
I.getParent()->getFirstInsertionPt() == I.getParent()->end())
1016+
return PI.setAborted(&I);
1017+
10111018
// TODO: We could use SimplifyInstruction here to fold PHINodes and
10121019
// SelectInsts. However, doing so requires to change the current
10131020
// dead-operand-tracking mechanism. For instance, suppose neither loading
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
; RUN: opt < %s -sroa -S | FileCheck %s
2+
3+
target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
4+
target triple = "wasm32-unknown-unknown"
5+
6+
%struct.foo = type { i32 }
7+
declare i32 @__gxx_wasm_personality_v0(...)
8+
declare void @foo()
9+
10+
; Tests if the SROA pass correctly bails out on rewriting PHIs in a catchswitch
11+
; BB.
12+
; CHECK-LABEL: @test_phi_catchswitch
13+
define void @test_phi_catchswitch() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
14+
entry:
15+
; CHECK: alloca
16+
%tmp = alloca %struct.foo, align 4
17+
%tmp2 = getelementptr inbounds %struct.foo, %struct.foo* %tmp, i32 0, i32 0
18+
invoke void @foo()
19+
to label %bb3 unwind label %bb10
20+
21+
bb3: ; preds = %entry
22+
invoke void @foo()
23+
to label %bb9 unwind label %catch.dispatch
24+
25+
catch.dispatch: ; preds = %bb3
26+
; While rewriting the alloca in the entry BB, the SROA pass tries to insert a
27+
; non-PHI instruction in this BB by calling getFirstInsertionPt(), which is
28+
; not possible in a catchswitch BB. This test checks if we correctly bail out
29+
; on these cases.
30+
%tmp5 = phi i32* [ %tmp2, %bb3 ]
31+
%tmp6 = catchswitch within none [label %catch.start] unwind label %bb10
32+
33+
catch.start: ; preds = %catch.dispatch
34+
%tmp8 = catchpad within %tmp6 [i8* null]
35+
unreachable
36+
37+
bb9: ; preds = %bb3
38+
unreachable
39+
40+
bb10: ; preds = %catch.dispatch, %entry
41+
%tmp11 = phi i32* [ %tmp2, %entry ], [ %tmp5, %catch.dispatch ]
42+
%tmp12 = cleanuppad within none []
43+
store i32 0, i32* %tmp11, align 4
44+
unreachable
45+
}

0 commit comments

Comments
 (0)