[InstCombine] Don't insert an instruction after a terminator
authorDavid Majnemer <david.majnemer@gmail.com>
Fri, 6 Nov 2015 23:59:23 +0000 (23:59 +0000)
committerDavid Majnemer <david.majnemer@gmail.com>
Fri, 6 Nov 2015 23:59:23 +0000 (23:59 +0000)
We tried to insert a cast of a phi in a block whose terminator is an
EHPad.  This is invalid.  Do not attempt the transform in these
circumstances.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@252370 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/InstCombine/InstCombinePHI.cpp
test/Transforms/InstCombine/token.ll

index 1d3b837f124d547f2806d83bc034875f0f78a7eb..a9f8f49d2f1ee05fcb661c6fae8debb69d5da313 100644 (file)
@@ -469,6 +469,12 @@ Instruction *InstCombiner::FoldPHIArgZextsIntoPHI(PHINode &Phi) {
 /// only used by the PHI, PHI together their inputs, and do the operation once,
 /// to the result of the PHI.
 Instruction *InstCombiner::FoldPHIArgOpIntoPHI(PHINode &PN) {
+  // We cannot create a new instruction after the PHI if the terminator is an
+  // EHPad because there is no valid insertion point.
+  if (TerminatorInst *TI = PN.getParent()->getTerminator())
+    if (TI->isEHPad())
+      return nullptr;
+
   Instruction *FirstInst = cast<Instruction>(PN.getIncomingValue(0));
 
   if (isa<GetElementPtrInst>(FirstInst))
index bb35d156064d2e85fa60586cf012e56afcc2bc4d..c8f1dc1bc312ef7207fbfeff19201a4dea33bc0e 100644 (file)
@@ -4,7 +4,7 @@ target triple = "x86_64-pc-windows-msvc18.0.0"
 
 declare i32 @__CxxFrameHandler3(...)
 
-define i8* @f() personality i32 (...)* @__CxxFrameHandler3 {
+define void @test1() personality i32 (...)* @__CxxFrameHandler3 {
 bb:
   unreachable
 
@@ -13,9 +13,45 @@ unreachable:
   cleanupret %cl unwind to caller
 }
 
+; CHECK-LABEL: define void @test1(
 ; CHECK: unreachable:
 ; CHECK:   %cl = cleanuppad []
 ; CHECK:   cleanupret %cl unwind to caller
 
+define void @test2(i8 %A, i8 %B) personality i32 (...)* @__CxxFrameHandler3 {
+bb:
+  %X = zext i8 %A to i32
+  invoke void @g(i32 0)
+    to label %cont
+    unwind label %catch
+
+cont:
+  %Y = zext i8 %B to i32
+  invoke void @g(i32 0)
+    to label %unreachable
+    unwind label %catch
+
+catch:
+  %phi = phi i32 [ %X, %bb ], [ %Y, %cont ]
+  %cl = catchpad []
+   to label %doit
+   unwind label %endpad
+
+doit:
+  call void @g(i32 %phi)
+  unreachable
+
+unreachable:
+  unreachable
+
+endpad:
+  catchendpad unwind to caller
+}
+
+
+; CHECK-LABEL: define void @test2(
+; CHECK:  %X = zext i8 %A to i32
+; CHECK:  %Y = zext i8 %B to i32
+; CHECK:  %phi = phi i32 [ %X, %bb ], [ %Y, %cont ]
 
-declare void @g(i8*)
+declare void @g(i32)