Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit 3b2360f

Browse files
committed
Merging r296698:
------------------------------------------------------------------------ r296698 | rnk | 2017-03-01 17:41:12 -0500 (Wed, 01 Mar 2017) | 10 lines [Constant Hoisting] Avoid inserting instructions before EH pads Now that terminators can be EH pads, this code needs to iterate over the immediate dominators of the EH pad to find a valid insertion point. Fix for PR32107 Patch by Robert Olliff! Differential Revision: https://reviews.llvm.org/D30511 ------------------------------------------------------------------------ git-svn-id: https://llvm.org/svn/llvm-project/llvm/branches/release_40@299670 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent c1a0cfc commit 3b2360f

File tree

2 files changed

+72
-2
lines changed

2 files changed

+72
-2
lines changed

lib/Transforms/Scalar/ConstantHoisting.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,16 @@ Instruction *ConstantHoistingPass::findMatInsertPt(Instruction *Inst,
136136
if (Idx != ~0U && isa<PHINode>(Inst))
137137
return cast<PHINode>(Inst)->getIncomingBlock(Idx)->getTerminator();
138138

139-
BasicBlock *IDom = DT->getNode(Inst->getParent())->getIDom()->getBlock();
140-
return IDom->getTerminator();
139+
// This must be an EH pad. Iterate over immediate dominators until we find a
140+
// non-EH pad. We need to skip over catchswitch blocks, which are both EH pads
141+
// and terminators.
142+
auto IDom = DT->getNode(Inst->getParent())->getIDom();
143+
while (IDom->getBlock()->isEHPad()) {
144+
assert(Entry != IDom->getBlock() && "eh pad in entry block");
145+
IDom = IDom->getIDom();
146+
}
147+
148+
return IDom->getBlock()->getTerminator();
141149
}
142150

143151
/// \brief Find an insertion point that dominates all uses.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
; RUN: opt -S -consthoist < %s | FileCheck %s
2+
3+
; FIXME: The catchpad doesn't even use the constant, so a better fix would be to
4+
; insert the bitcast in the catchpad block.
5+
6+
target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128"
7+
target triple = "x86_64-pc-windows-msvc"
8+
9+
; CHECK-LABEL: define i32 @main
10+
; CHECK: %tobool = icmp eq i32 %argc, 0
11+
; CHECK-NEXT: bitcast i64 9209618997431186100 to i64
12+
; CHECK-NEXT: br i1 %tobool
13+
14+
; Function Attrs: norecurse
15+
define i32 @main(i32 %argc, i8** nocapture readnone %argv) local_unnamed_addr #0 personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) {
16+
%call = tail call i64 @fn(i64 0)
17+
%call1 = tail call i64 @fn(i64 1)
18+
%tobool = icmp eq i32 %argc, 0
19+
br i1 %tobool, label %2, label %1
20+
21+
; <label>:1: ; preds = %0
22+
%call2 = invoke i64 @fn(i64 %call)
23+
to label %6 unwind label %catch.dispatch
24+
25+
; <label>:2: ; preds = %0
26+
%call3 = invoke i64 @fn(i64 %call1)
27+
to label %6 unwind label %catch.dispatch
28+
29+
catch.dispatch: ; preds = %2, %1
30+
%z.0 = phi i64 [ %call, %1 ], [ %call1, %2 ]
31+
%3 = catchswitch within none [label %4] unwind to caller
32+
33+
; <label>:4: ; preds = %catch.dispatch
34+
%5 = catchpad within %3 [i8* null, i32 64, i8* null]
35+
br i1 %tobool, label %then, label %else
36+
37+
then:
38+
%call4 = tail call i64 @fn(i64 %z.0) [ "funclet"(token %5) ]
39+
%add = add i64 %call4, 9209618997431186100
40+
br label %endif
41+
42+
else:
43+
%call5 = tail call i64 @fn(i64 0) [ "funclet"(token %5) ]
44+
%add6 = add i64 %call5, 9209618997431186100
45+
br label %endif
46+
47+
endif:
48+
%v = phi i64 [ %add, %then ], [ %add6, %else ]
49+
%call7 = tail call i64 @fn(i64 %v) [ "funclet"(token %5) ]
50+
%call8 = tail call i64 @fn(i64 %call7) [ "funclet"(token %5) ]
51+
catchret from %5 to label %6
52+
53+
; <label>:6: ; preds = %1, %2, %4
54+
ret i32 0
55+
}
56+
57+
declare i64 @fn(i64) local_unnamed_addr #1
58+
59+
declare i32 @__CxxFrameHandler3(...)
60+
61+
attributes #0 = { norecurse "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "stack-protector-buffer-size"="8" "target-features"="+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }
62+
attributes #1 = { "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "stack-protector-buffer-size"="8" "target-features"="+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }

0 commit comments

Comments
 (0)