diff options
| author | Reid Kleckner <rnk@google.com> | 2017-03-01 22:41:12 +0000 |
|---|---|---|
| committer | Reid Kleckner <rnk@google.com> | 2017-03-01 22:41:12 +0000 |
| commit | d80b69fa3bbb8571285775798b78c8babd547823 (patch) | |
| tree | e06836f180cd90c9cc6596235a04aadd9b01074a | |
| parent | 683fdd62bb499d6863de47a72847ecd135724d03 (diff) | |
| download | bcm5719-llvm-d80b69fa3bbb8571285775798b78c8babd547823.tar.gz bcm5719-llvm-d80b69fa3bbb8571285775798b78c8babd547823.zip | |
[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
llvm-svn: 296698
| -rw-r--r-- | llvm/lib/Transforms/Scalar/ConstantHoisting.cpp | 12 | ||||
| -rw-r--r-- | llvm/test/Transforms/ConstantHoisting/X86/ehpad.ll | 62 |
2 files changed, 72 insertions, 2 deletions
diff --git a/llvm/lib/Transforms/Scalar/ConstantHoisting.cpp b/llvm/lib/Transforms/Scalar/ConstantHoisting.cpp index ebe35aac098..ee6333e8871 100644 --- a/llvm/lib/Transforms/Scalar/ConstantHoisting.cpp +++ b/llvm/lib/Transforms/Scalar/ConstantHoisting.cpp @@ -136,8 +136,16 @@ Instruction *ConstantHoistingPass::findMatInsertPt(Instruction *Inst, if (Idx != ~0U && isa<PHINode>(Inst)) return cast<PHINode>(Inst)->getIncomingBlock(Idx)->getTerminator(); - BasicBlock *IDom = DT->getNode(Inst->getParent())->getIDom()->getBlock(); - return IDom->getTerminator(); + // This must be an EH pad. Iterate over immediate dominators until we find a + // non-EH pad. We need to skip over catchswitch blocks, which are both EH pads + // and terminators. + auto IDom = DT->getNode(Inst->getParent())->getIDom(); + while (IDom->getBlock()->isEHPad()) { + assert(Entry != IDom->getBlock() && "eh pad in entry block"); + IDom = IDom->getIDom(); + } + + return IDom->getBlock()->getTerminator(); } /// \brief Find an insertion point that dominates all uses. diff --git a/llvm/test/Transforms/ConstantHoisting/X86/ehpad.ll b/llvm/test/Transforms/ConstantHoisting/X86/ehpad.ll new file mode 100644 index 00000000000..3178e87f754 --- /dev/null +++ b/llvm/test/Transforms/ConstantHoisting/X86/ehpad.ll @@ -0,0 +1,62 @@ +; RUN: opt -S -consthoist < %s | FileCheck %s + +; FIXME: The catchpad doesn't even use the constant, so a better fix would be to +; insert the bitcast in the catchpad block. + +target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-pc-windows-msvc" + +; CHECK-LABEL: define i32 @main +; CHECK: %tobool = icmp eq i32 %argc, 0 +; CHECK-NEXT: bitcast i64 9209618997431186100 to i64 +; CHECK-NEXT: br i1 %tobool + +; Function Attrs: norecurse +define i32 @main(i32 %argc, i8** nocapture readnone %argv) local_unnamed_addr #0 personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) { + %call = tail call i64 @fn(i64 0) + %call1 = tail call i64 @fn(i64 1) + %tobool = icmp eq i32 %argc, 0 + br i1 %tobool, label %2, label %1 + +; <label>:1: ; preds = %0 + %call2 = invoke i64 @fn(i64 %call) + to label %6 unwind label %catch.dispatch + +; <label>:2: ; preds = %0 + %call3 = invoke i64 @fn(i64 %call1) + to label %6 unwind label %catch.dispatch + +catch.dispatch: ; preds = %2, %1 + %z.0 = phi i64 [ %call, %1 ], [ %call1, %2 ] + %3 = catchswitch within none [label %4] unwind to caller + +; <label>:4: ; preds = %catch.dispatch + %5 = catchpad within %3 [i8* null, i32 64, i8* null] + br i1 %tobool, label %then, label %else + +then: + %call4 = tail call i64 @fn(i64 %z.0) [ "funclet"(token %5) ] + %add = add i64 %call4, 9209618997431186100 + br label %endif + +else: + %call5 = tail call i64 @fn(i64 0) [ "funclet"(token %5) ] + %add6 = add i64 %call5, 9209618997431186100 + br label %endif + +endif: + %v = phi i64 [ %add, %then ], [ %add6, %else ] + %call7 = tail call i64 @fn(i64 %v) [ "funclet"(token %5) ] + %call8 = tail call i64 @fn(i64 %call7) [ "funclet"(token %5) ] + catchret from %5 to label %6 + +; <label>:6: ; preds = %1, %2, %4 + ret i32 0 +} + +declare i64 @fn(i64) local_unnamed_addr #1 + +declare i32 @__CxxFrameHandler3(...) + +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" } +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" } |

