<div dir="ltr">David ended up solving the problem a different way (powering down BranchFolding), so I guess I won't pursue this.</div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Oct 1, 2015 at 10:18 AM, NAKAMURA Takumi <span dir="ltr"><<a href="mailto:geek4civic@gmail.com" target="_blank">geek4civic@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Reverted in r249032. It broke the builder, <a href="http://lab.llvm.org:8011/builders/clang-x64-ninja-win7/builds/5516" target="_blank">http://lab.llvm.org:8011/builders/clang-x64-ninja-win7/builds/5516</a></div><div class="HOEnZb"><div class="h5"><br><div class="gmail_quote"><div dir="ltr">On Thu, Oct 1, 2015 at 8:11 AM Reid Kleckner via llvm-commits <<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: rnk<br>
Date: Wed Sep 30 18:09:23 2015<br>
New Revision: 248959<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=248959&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=248959&view=rev</a><br>
Log:<br>
[WinEH] Emit int3 after noreturn calls on Win64<br>
<br>
The Win64 unwinder disassembles forwards from each PC to try to<br>
determine if this PC is in an epilogue. If so, it skips calling the EH<br>
personality function for that frame. Typically, this means you cannot<br>
catch an exception in the same frame that you threw it, because 'throw'<br>
calls a noreturn runtime function.<br>
<br>
Previously we avoided this problem with the TrapUnreachable<br>
TargetOption, but that's a much bigger hammer than we need. All we need<br>
is a 1 byte non-epilogue instruction right after the call.  Instead,<br>
what we got was an unconditional branch to a shared block containing the<br>
ud2, potentially 7 bytes instead of 1. So, this reverts r206684, which<br>
added TrapUnreachable, and replaces it with something better.<br>
<br>
The new code pattern matches for invoke/call followed by unreachable and<br>
inserts an int3 into the DAG. To be 100% watertight, we would need to<br>
insert SEH_Epilogue instructions into all basic blocks ending in a call<br>
with no terminators or successors, but in practice this is unlikely to<br>
come up.<br>
<br>
Added:<br>
    llvm/trunk/test/CodeGen/X86/win-catchpad-rethrow.ll<br>
Modified:<br>
    llvm/trunk/include/llvm/Target/TargetOptions.h<br>
    llvm/trunk/lib/CodeGen/SelectionDAG/FastISel.cpp<br>
    llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp<br>
    llvm/trunk/lib/Target/X86/X86ISelLowering.cpp<br>
    llvm/trunk/lib/Target/X86/X86TargetMachine.cpp<br>
    llvm/trunk/test/CodeGen/X86/br-fold.ll<br>
    llvm/trunk/test/CodeGen/X86/win64_call_epi.ll<br>
<br>
Modified: llvm/trunk/include/llvm/Target/TargetOptions.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Target/TargetOptions.h?rev=248959&r1=248958&r2=248959&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Target/TargetOptions.h?rev=248959&r1=248958&r2=248959&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/include/llvm/Target/TargetOptions.h (original)<br>
+++ llvm/trunk/include/llvm/Target/TargetOptions.h Wed Sep 30 18:09:23 2015<br>
@@ -71,7 +71,7 @@ namespace llvm {<br>
           EnableFastISel(false), PositionIndependentExecutable(false),<br>
           UseInitArray(false), DisableIntegratedAS(false),<br>
           CompressDebugSections(false), FunctionSections(false),<br>
-          DataSections(false), UniqueSectionNames(true), TrapUnreachable(false),<br>
+          DataSections(false), UniqueSectionNames(true),<br>
           EmulatedTLS(false), FloatABIType(FloatABI::Default),<br>
           AllowFPOpFusion(FPOpFusion::Standard), Reciprocals(TargetRecip()),<br>
           JTType(JumpTable::Single),<br>
@@ -169,9 +169,6 @@ namespace llvm {<br>
<br>
     unsigned UniqueSectionNames : 1;<br>
<br>
-    /// Emit target-specific trap instruction for 'unreachable' IR instructions.<br>
-    unsigned TrapUnreachable : 1;<br>
-<br>
     /// EmulatedTLS - This flag enables emulated TLS model, using emutls<br>
     /// function in the runtime library..<br>
     unsigned EmulatedTLS : 1;<br>
@@ -234,7 +231,6 @@ inline bool operator==(const TargetOptio<br>
     ARE_EQUAL(EnableFastISel) &&<br>
     ARE_EQUAL(PositionIndependentExecutable) &&<br>
     ARE_EQUAL(UseInitArray) &&<br>
-    ARE_EQUAL(TrapUnreachable) &&<br>
     ARE_EQUAL(EmulatedTLS) &&<br>
     ARE_EQUAL(FloatABIType) &&<br>
     ARE_EQUAL(AllowFPOpFusion) &&<br>
<br>
Modified: llvm/trunk/lib/CodeGen/SelectionDAG/FastISel.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/SelectionDAG/FastISel.cpp?rev=248959&r1=248958&r2=248959&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/SelectionDAG/FastISel.cpp?rev=248959&r1=248958&r2=248959&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/CodeGen/SelectionDAG/FastISel.cpp (original)<br>
+++ llvm/trunk/lib/CodeGen/SelectionDAG/FastISel.cpp Wed Sep 30 18:09:23 2015<br>
@@ -1569,10 +1569,8 @@ bool FastISel::selectOperator(const User<br>
   }<br>
<br>
   case Instruction::Unreachable:<br>
-    if (TM.Options.TrapUnreachable)<br>
-      return fastEmit_(MVT::Other, MVT::Other, ISD::TRAP) != 0;<br>
-    else<br>
-      return true;<br>
+    // Nothing to emit.<br>
+    return true;<br>
<br>
   case Instruction::Alloca:<br>
     // FunctionLowering has the static-sized case covered.<br>
<br>
Modified: llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp?rev=248959&r1=248958&r2=248959&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp?rev=248959&r1=248958&r2=248959&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp (original)<br>
+++ llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp Wed Sep 30 18:09:23 2015<br>
@@ -2205,10 +2205,7 @@ void SelectionDAGBuilder::visitIndirectB<br>
                           getValue(I.getAddress())));<br>
 }<br>
<br>
-void SelectionDAGBuilder::visitUnreachable(const UnreachableInst &I) {<br>
-  if (DAG.getTarget().Options.TrapUnreachable)<br>
-    DAG.setRoot(DAG.getNode(ISD::TRAP, getCurSDLoc(), MVT::Other, DAG.getRoot()));<br>
-}<br>
+void SelectionDAGBuilder::visitUnreachable(const UnreachableInst &I) {}<br>
<br>
 void SelectionDAGBuilder::visitFSub(const User &I) {<br>
   // -0.0 - X --> fneg<br>
<br>
Modified: llvm/trunk/lib/Target/X86/X86ISelLowering.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/X86/X86ISelLowering.cpp?rev=248959&r1=248958&r2=248959&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/X86/X86ISelLowering.cpp?rev=248959&r1=248958&r2=248959&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Target/X86/X86ISelLowering.cpp (original)<br>
+++ llvm/trunk/lib/Target/X86/X86ISelLowering.cpp Wed Sep 30 18:09:23 2015<br>
@@ -2945,6 +2945,20 @@ static SDValue getMOVL(SelectionDAG &DAG<br>
   return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);<br>
 }<br>
<br>
+/// Check if the fall through instruction after a call site is unreachable.<br>
+/// FIXME: This will fail if there are interesting non-code generating IR<br>
+/// instructions between the call and the unreachable (lifetime.end). In<br>
+/// practice, this should be rare because optimizations like to delete non-call<br>
+/// code before unreachable.<br>
+static bool isCallFollowedByUnreachable(ImmutableCallSite CS) {<br>
+  const Instruction *NextInst;<br>
+  if (auto *II = dyn_cast<InvokeInst>(CS.getInstruction()))<br>
+    NextInst = II->getNormalDest()->getFirstNonPHIOrDbg();<br>
+  else<br>
+    NextInst = CS.getInstruction()->getNextNode();<br>
+  return isa<UnreachableInst>(NextInst);<br>
+}<br>
+<br>
 SDValue<br>
 X86TargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,<br>
                              SmallVectorImpl<SDValue> &InVals) const {<br>
@@ -3450,6 +3464,15 @@ X86TargetLowering::LowerCall(TargetLower<br>
     InFlag = Chain.getValue(1);<br>
   }<br>
<br>
+  if (Subtarget->isTargetWin64() && CLI.CS) {<br>
+    // Look for a call followed by unreachable. On Win64, we need to ensure that<br>
+    // the call does not accidentally fall through to something that looks like<br>
+    // an epilogue. We do this by inserting a DEBUGTRAP, which lowers to int3,<br>
+    // which is what MSVC emits after noreturn calls.<br>
+    if (isCallFollowedByUnreachable(*CLI.CS))<br>
+      Chain = DAG.getNode(ISD::DEBUGTRAP, dl, MVT::Other, Chain);<br>
+  }<br>
+<br>
   // Handle result values, copying them out of physregs into vregs that we<br>
   // return.<br>
   return LowerCallResult(Chain, InFlag, CallConv, isVarArg,<br>
<br>
Modified: llvm/trunk/lib/Target/X86/X86TargetMachine.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/X86/X86TargetMachine.cpp?rev=248959&r1=248958&r2=248959&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/X86/X86TargetMachine.cpp?rev=248959&r1=248958&r2=248959&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Target/X86/X86TargetMachine.cpp (original)<br>
+++ llvm/trunk/lib/Target/X86/X86TargetMachine.cpp Wed Sep 30 18:09:23 2015<br>
@@ -110,13 +110,6 @@ X86TargetMachine::X86TargetMachine(const<br>
                         OL),<br>
       TLOF(createTLOF(getTargetTriple())),<br>
       Subtarget(TT, CPU, FS, *this, Options.StackAlignmentOverride) {<br>
-  // Windows stack unwinder gets confused when execution flow "falls through"<br>
-  // after a call to 'noreturn' function.<br>
-  // To prevent that, we emit a trap for 'unreachable' IR instructions.<br>
-  // (which on X86, happens to be the 'ud2' instruction)<br>
-  if (Subtarget.isTargetWin64())<br>
-    this->Options.TrapUnreachable = true;<br>
-<br>
   // By default (and when -ffast-math is on), enable estimate codegen for<br>
   // everything except scalar division. By default, use 1 refinement step for<br>
   // all operations. Defaults may be overridden by using command-line options.<br>
<br>
Modified: llvm/trunk/test/CodeGen/X86/br-fold.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/X86/br-fold.ll?rev=248959&r1=248958&r2=248959&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/X86/br-fold.ll?rev=248959&r1=248958&r2=248959&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/test/CodeGen/X86/br-fold.ll (original)<br>
+++ llvm/trunk/test/CodeGen/X86/br-fold.ll Wed Sep 30 18:09:23 2015<br>
@@ -10,10 +10,10 @@<br>
 ; X64_LINUX-NEXT: %bb8.i329<br>
<br>
 ; X64_WINDOWS: orq %rax, %rcx<br>
-; X64_WINDOWS-NEXT: ud2<br>
+; X64_WINDOWS-NEXT: %bb8.i329<br>
<br>
 ; X64_WINDOWS_GNU: orq %rax, %rcx<br>
-; X64_WINDOWS_GNU-NEXT: ud2<br>
+; X64_WINDOWS_GNU-NEXT: %bb8.i329<br>
<br>
 @_ZN11xercesc_2_513SchemaSymbols21fgURI_SCHEMAFORSCHEMAE = external constant [33 x i16], align 32 ; <[33 x i16]*> [#uses=1]<br>
 @_ZN11xercesc_2_56XMLUni16fgNotationStringE = external constant [9 x i16], align 16 ; <[9 x i16]*> [#uses=1]<br>
<br>
Added: llvm/trunk/test/CodeGen/X86/win-catchpad-rethrow.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/X86/win-catchpad-rethrow.ll?rev=248959&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/X86/win-catchpad-rethrow.ll?rev=248959&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/test/CodeGen/X86/win-catchpad-rethrow.ll (added)<br>
+++ llvm/trunk/test/CodeGen/X86/win-catchpad-rethrow.ll Wed Sep 30 18:09:23 2015<br>
@@ -0,0 +1,103 @@<br>
+; RUN: llc -mtriple=x86_64-pc-windows-msvc < %s | FileCheck %s<br>
+<br>
+; C++ EH rethrows are interesting, because they are calls to noreturn<br>
+; functions. There *must* be some code after the call instruction that doesn't<br>
+; look like an epilogue. We use int3 to be consistent with MSVC.<br>
+<br>
+; Based on this C++ source:<br>
+; int main() {<br>
+;   try {<br>
+;     throw 42;<br>
+;   } catch (int) {<br>
+;     try {<br>
+;       throw;<br>
+;     } catch (int) {<br>
+;     }<br>
+;   }<br>
+;   return 0;<br>
+; }<br>
+<br>
+; ModuleID = 't.cpp'<br>
+target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128"<br>
+target triple = "x86_64-pc-windows-msvc"<br>
+<br>
+%rtti.TypeDescriptor2 = type { i8**, i8*, [3 x i8] }<br>
+%eh.CatchableType = type { i32, i32, i32, i32, i32, i32, i32 }<br>
+%eh.CatchableTypeArray.1 = type { i32, [1 x i32] }<br>
+%eh.ThrowInfo = type { i32, i32, i32, i32 }<br>
+<br>
+$"\01??_R0H@8" = comdat any<br>
+<br>
+$"_CT??_R0H@84" = comdat any<br>
+<br>
+$_CTA1H = comdat any<br>
+<br>
+$_TI1H = comdat any<br>
+<br>
+@"\01??_7type_info@@6B@" = external constant i8*<br>
+@"\01??_R0H@8" = linkonce_odr global %rtti.TypeDescriptor2 { i8** @"\01??_7type_info@@6B@", i8* null, [3 x i8] c".H\00" }, comdat<br>
+@__ImageBase = external constant i8<br>
+@"_CT??_R0H@84" = linkonce_odr unnamed_addr constant %eh.CatchableType { i32 1, i32 trunc (i64 sub nuw nsw (i64 ptrtoint (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i64), i64 ptrtoint (i8* @__ImageBase to i64)) to i32), i32 0, i32 -1, i32 0, i32 4, i32 0 }, section ".xdata", comdat<br>
+@_CTA1H = linkonce_odr unnamed_addr constant %eh.CatchableTypeArray.1 { i32 1, [1 x i32] [i32 trunc (i64 sub nuw nsw (i64 ptrtoint (%eh.CatchableType* @"_CT??_R0H@84" to i64), i64 ptrtoint (i8* @__ImageBase to i64)) to i32)] }, section ".xdata", comdat<br>
+@_TI1H = linkonce_odr unnamed_addr constant %eh.ThrowInfo { i32 0, i32 0, i32 0, i32 trunc (i64 sub nuw nsw (i64 ptrtoint (%eh.CatchableTypeArray.1* @_CTA1H to i64), i64 ptrtoint (i8* @__ImageBase to i64)) to i32) }, section ".xdata", comdat<br>
+<br>
+define i32 @main() #0 personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) {<br>
+entry:<br>
+  %tmp = alloca i32, align 4<br>
+  store i32 42, i32* %tmp, align 4<br>
+  %0 = bitcast i32* %tmp to i8*<br>
+  invoke void @_CxxThrowException(i8* %0, %eh.ThrowInfo* nonnull @_TI1H) #1<br>
+          to label %unreachable unwind label %catch.dispatch<br>
+<br>
+catch.dispatch:                                   ; preds = %entry<br>
+  %1 = catchpad [%rtti.TypeDescriptor2* @"\01??_R0H@8", i32 0, i8* null]<br>
+          to label %catch unwind label %catchendblock<br>
+<br>
+catch:                                            ; preds = %catch.dispatch<br>
+  invoke void @_CxxThrowException(i8* null, %eh.ThrowInfo* null) #1<br>
+          to label %unreachable unwind label %catch.dispatch.1<br>
+<br>
+catch.dispatch.1:                                 ; preds = %catch<br>
+  %2 = catchpad [%rtti.TypeDescriptor2* @"\01??_R0H@8", i32 0, i8* null]<br>
+          to label %catch.3 unwind label %catchendblock.2<br>
+<br>
+catch.3:                                          ; preds = %catch.dispatch.1<br>
+  catchret %2 to label %try.cont<br>
+<br>
+try.cont:                                         ; preds = %catch.3<br>
+  catchret %1 to label %try.cont.5<br>
+<br>
+try.cont.5:                                       ; preds = %try.cont<br>
+  ret i32 0<br>
+<br>
+catchendblock.2:                                  ; preds = %catch.dispatch.1<br>
+  catchendpad unwind label %catchendblock<br>
+<br>
+catchendblock:                                    ; preds = %catchendblock.2, %catch.dispatch<br>
+  catchendpad unwind to caller<br>
+<br>
+unreachable:                                      ; preds = %catch, %entry<br>
+  unreachable<br>
+}<br>
+<br>
+declare void @_CxxThrowException(i8*, %eh.ThrowInfo*)<br>
+<br>
+declare i32 @__CxxFrameHandler3(...)<br>
+<br>
+attributes #0 = { "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "target-features"="+sse,+sse2" "unsafe-fp-math"="false" "use-soft-float"="false" }<br>
+attributes #1 = { noreturn }<br>
+<br>
+; CHECK: main:<br>
+; CHECK: .seh_proc main<br>
+; CHECK: movl $42,<br>
+; CHECK-DAG: leaq {{.*}}, %rcx<br>
+; CHECK-DAG: leaq _TI1H(%rip), %rdx<br>
+; CHECK: callq _CxxThrowException<br>
+; CHECK-NEXT: int3<br>
+<br>
+; CHECK: "?catch$1@?0?main@4HA":<br>
+; CHECK: .seh_proc "?catch$1@?0?main@4HA"<br>
+; CHECK-DAG: xorl %ecx, %ecx<br>
+; CHECK-DAG: xorl %edx, %edx<br>
+; CHECK: callq _CxxThrowException<br>
+; CHECK-NEXT: int3<br>
<br>
Modified: llvm/trunk/test/CodeGen/X86/win64_call_epi.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/X86/win64_call_epi.ll?rev=248959&r1=248958&r2=248959&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/X86/win64_call_epi.ll?rev=248959&r1=248958&r2=248959&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/test/CodeGen/X86/win64_call_epi.ll (original)<br>
+++ llvm/trunk/test/CodeGen/X86/win64_call_epi.ll Wed Sep 30 18:09:23 2015<br>
@@ -24,9 +24,9 @@ catch:<br>
 ; WIN64: nop<br>
 ; WIN64: addq ${{[0-9]+}}, %rsp<br>
 ; WIN64: retq<br>
-; Check for 'ud2' after noreturn call<br>
+; Check for 'int3' after noreturn call<br>
 ; WIN64: callq _Unwind_Resume<br>
-; WIN64-NEXT: ud2<br>
+; WIN64-NEXT: int3<br>
 ; WIN64: .seh_endproc<br>
<br>
<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits</a><br>
</blockquote></div>
</div></div></blockquote></div><br></div>