[PATCH] D30511: [Constant Hoisting] ConstantHoisting pass illegally modifies EHPad

Robert Olliff via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 1 14:14:06 PST 2017


Robby created this revision.

Fix for PR32107


https://reviews.llvm.org/D30511

Files:
  lib/Transforms/Scalar/ConstantHoisting.cpp
  test/Transforms/ConstantHoisting/X86/ehpad.ll


Index: test/Transforms/ConstantHoisting/X86/ehpad.ll
===================================================================
--- test/Transforms/ConstantHoisting/X86/ehpad.ll
+++ test/Transforms/ConstantHoisting/X86/ehpad.ll
@@ -0,0 +1,45 @@
+; RUN: opt -S -consthoist < %s 
+; CHECK that verifier succeeds
+
+target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-pc-windows-msvc"
+
+; 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]
+  %call4 = tail call i64 @fn(i64 %z.0) [ "funclet"(token %5) ]
+  %add = add i64 %call4, 9209618997431186100
+  %call5 = tail call i64 @fn(i64 %add) [ "funclet"(token %5) ]
+  %add6 = add i64 %call5, 9209618997431186100
+  %call7 = tail call i64 @fn(i64 %add6) [ "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" }
Index: lib/Transforms/Scalar/ConstantHoisting.cpp
===================================================================
--- lib/Transforms/Scalar/ConstantHoisting.cpp
+++ lib/Transforms/Scalar/ConstantHoisting.cpp
@@ -135,9 +135,22 @@
   assert(Entry != Inst->getParent() && "PHI or landing pad in entry block!");
   if (Idx != ~0U && isa<PHINode>(Inst))
     return cast<PHINode>(Inst)->getIncomingBlock(Idx)->getTerminator();
-
-  BasicBlock *IDom = DT->getNode(Inst->getParent())->getIDom()->getBlock();
-  return IDom->getTerminator();
+  // We start with what we already know is an EHPad Block
+  auto EHPadBlock = Inst->getParent();
+
+  // As already noted, we can't insert before an ehpad instruction, 
+  // so climb the tree to the next idom.
+  auto IDom = DT->getNode(EHPadBlock)->getIDom();
+
+  // It is possible that IDom is an EHPad as well (i.e catchswitch).
+  // Keep climbing until a non-EHPad is found.
+  while(IDom->getBlock()->isEHPad())
+  {
+      assert(Entry != IDom->getBlock() && "landing pad in entry block");
+      IDom = IDom->getIDom();
+  }
+
+  return IDom->getBlock()->getTerminator();
 }
 
 /// \brief Find an insertion point that dominates all uses.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D30511.90226.patch
Type: text/x-patch
Size: 3801 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170301/49da0468/attachment.bin>


More information about the llvm-commits mailing list