Skip to content

Commit 0bf4795

Browse files
rnkzmodem
authored andcommitted
[WinEH] Fix inttoptr+phi optimization in presence of catchswitch
getFirstInsertionPt's return value must be checked for validity before casting it to Instruction*. Don't attempt to insert casts after a phi in a catchswitch block. Fixes PR45033, introduced in D37832. Reviewed By: davidxl, hfinkel Differential Revision: https://reviews.llvm.org/D75381 (cherry picked from commit 1adbe86)
1 parent 99e5b2f commit 0bf4795

File tree

2 files changed

+91
-4
lines changed

2 files changed

+91
-4
lines changed

llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,13 +218,21 @@ Instruction *InstCombiner::FoldIntegerTypedPHI(PHINode &PN) {
218218
return nullptr;
219219

220220
// If any of the operand that requires casting is a terminator
221-
// instruction, do not do it.
221+
// instruction, do not do it. Similarly, do not do the transform if the value
222+
// is PHI in a block with no insertion point, for example, a catchswitch
223+
// block, since we will not be able to insert a cast after the PHI.
222224
if (any_of(AvailablePtrVals, [&](Value *V) {
223225
if (V->getType() == IntToPtr->getType())
224226
return false;
225-
226227
auto *Inst = dyn_cast<Instruction>(V);
227-
return Inst && Inst->isTerminator();
228+
if (!Inst)
229+
return false;
230+
if (Inst->isTerminator())
231+
return true;
232+
auto *BB = Inst->getParent();
233+
if (isa<PHINode>(Inst) && BB->getFirstInsertionPt() == BB->end())
234+
return true;
235+
return false;
228236
}))
229237
return nullptr;
230238

@@ -264,8 +272,10 @@ Instruction *InstCombiner::FoldIntegerTypedPHI(PHINode &PN) {
264272
if (auto *IncomingI = dyn_cast<Instruction>(IncomingVal)) {
265273
BasicBlock::iterator InsertPos(IncomingI);
266274
InsertPos++;
275+
BasicBlock *BB = IncomingI->getParent();
267276
if (isa<PHINode>(IncomingI))
268-
InsertPos = IncomingI->getParent()->getFirstInsertionPt();
277+
InsertPos = BB->getFirstInsertionPt();
278+
assert(InsertPos != BB->end() && "should have checked above");
269279
InsertNewInstBefore(CI, *InsertPos);
270280
} else {
271281
auto *InsertBB = &IncomingBB->getParent()->getEntryBlock();
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
; RUN: opt < %s -instcombine -S | FileCheck %s
2+
3+
; PR45033: Don't try to insert a cast into a catchswich block.
4+
5+
target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
6+
target triple = "x86_64-pc-windows-msvc"
7+
8+
%struct.intrusive_ptr = type { %struct.C* }
9+
%struct.C = type { %struct.intrusive_ref_counter }
10+
%struct.intrusive_ref_counter = type { i32 }
11+
12+
declare dso_local %struct.C* @"?mk@@YAPEAUC@@XZ"() #3
13+
declare dso_local void @"?intrusive_ptr_release@@YAXPEBUintrusive_ref_counter@@@Z"(%struct.intrusive_ref_counter*) #3
14+
declare dso_local void @"?terminate@@YAXXZ"()
15+
declare dso_local i32 @__CxxFrameHandler3(...)
16+
17+
define dso_local void @"?crash@@YAXXZ"() local_unnamed_addr #0 personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) {
18+
entry:
19+
%call1 = invoke %struct.C* @"?mk@@YAPEAUC@@XZ"()
20+
to label %invoke.cont2 unwind label %catch.dispatch
21+
22+
invoke.cont2: ; preds = %entry
23+
%0 = ptrtoint %struct.C* %call1 to i64
24+
%call5 = invoke %struct.C* @"?mk@@YAPEAUC@@XZ"()
25+
to label %try.cont unwind label %catch.dispatch
26+
27+
catch.dispatch: ; preds = %invoke.cont2, %entry
28+
%a.sroa.0.0 = phi i64 [ %0, %invoke.cont2 ], [ 0, %entry ]
29+
%1 = catchswitch within none [label %catch] unwind label %ehcleanup
30+
31+
catch: ; preds = %catch.dispatch
32+
%2 = catchpad within %1 [i8* null, i32 64, i8* null]
33+
catchret from %2 to label %try.cont
34+
35+
try.cont: ; preds = %invoke.cont2, %catch
36+
%a.sroa.0.1 = phi i64 [ %0, %invoke.cont2 ], [ %a.sroa.0.0, %catch ]
37+
%3 = inttoptr i64 %a.sroa.0.1 to %struct.C*
38+
%tobool.i3 = icmp eq %struct.C* %3, null
39+
br i1 %tobool.i3, label %"??1?$intrusive_ptr@UC@@@@[email protected]", label %if.then.i4
40+
41+
if.then.i4: ; preds = %try.cont
42+
%4 = getelementptr %struct.C, %struct.C* %3, i64 0, i32 0
43+
invoke void @"?intrusive_ptr_release@@YAXPEBUintrusive_ref_counter@@@Z"(%struct.intrusive_ref_counter* %4)
44+
to label %"??1?$intrusive_ptr@UC@@@@[email protected]" unwind label %terminate.i5
45+
46+
terminate.i5: ; preds = %if.then.i4
47+
%5 = cleanuppad within none []
48+
call void @"?terminate@@YAXXZ"() #4 [ "funclet"(token %5) ]
49+
unreachable
50+
51+
"??1?$intrusive_ptr@UC@@@@[email protected]": ; preds = %try.cont, %if.then.i4
52+
ret void
53+
54+
ehcleanup: ; preds = %catch.dispatch
55+
%6 = cleanuppad within none []
56+
%7 = inttoptr i64 %a.sroa.0.0 to %struct.C*
57+
%tobool.i = icmp eq %struct.C* %7, null
58+
br i1 %tobool.i, label %"??1?$intrusive_ptr@UC@@@@[email protected]", label %if.then.i
59+
60+
if.then.i: ; preds = %ehcleanup
61+
%8 = getelementptr %struct.C, %struct.C* %7, i64 0, i32 0
62+
invoke void @"?intrusive_ptr_release@@YAXPEBUintrusive_ref_counter@@@Z"(%struct.intrusive_ref_counter* %8) [ "funclet"(token %6) ]
63+
to label %"??1?$intrusive_ptr@UC@@@@[email protected]" unwind label %terminate.i
64+
65+
terminate.i: ; preds = %if.then.i
66+
%9 = cleanuppad within %6 []
67+
call void @"?terminate@@YAXXZ"() #4 [ "funclet"(token %9) ]
68+
unreachable
69+
70+
"??1?$intrusive_ptr@UC@@@@[email protected]": ; preds = %ehcleanup, %if.then.i
71+
cleanupret from %6 unwind to caller
72+
}
73+
74+
; CHECK-LABEL: define dso_local void @"?crash@@YAXXZ"
75+
; CHECK: catch.dispatch:
76+
; CHECK-NEXT: %a.sroa.0.0 = phi i64
77+
; CHECK-NEXT: catchswitch within none [label %catch] unwind label %ehcleanup

0 commit comments

Comments
 (0)