[llvm] r253902 - [WinEH] Fix problem where CodeGenPrepare incorrectly sinks a bitcast into an EH pad.

Quentin Colombet via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 30 16:27:19 PST 2015


Thanks for the pointer Andy!

Q.
> On Nov 30, 2015, at 4:12 PM, Kaylor, Andrew <andrew.kaylor at intel.com> wrote:
> 
> The Windows exception handling mechanism needs to have well-defined entry and exit points for exception handling code.  The new EH pad instructions have been defined in the language reference in such a way that the EH pad instruction is a terminator but must also be the first non-PHI instruction in its block as a way of preventing optimizations from placing instructions into locations that are ambiguous with regard to whether they are meant to be inside or outside of the exception handling code. 
> 
> There's a bit more discussion here:
> 
> http://llvm.org/docs/ExceptionHandling.html#exception-handling-using-the-windows-runtime
> 
> -Andy
> 
> -----Original Message-----
> From: Quentin Colombet [mailto:qcolombet at apple.com] 
> Sent: Monday, November 30, 2015 3:47 PM
> To: Kaylor, Andrew <andrew.kaylor at intel.com>
> Cc: llvm-commits at lists.llvm.org
> Subject: Re: [llvm] r253902 - [WinEH] Fix problem where CodeGenPrepare incorrectly sinks a bitcast into an EH pad.
> 
> Hi Andrew,
> 
> Out of curiosity, what is the problem of having non-phi instructions in those blocks?
> 
> Cheers,
> -Quentin
> 
>> On Nov 23, 2015, at 11:16 AM, Andrew Kaylor via llvm-commits <llvm-commits at lists.llvm.org> wrote:
>> 
>> Author: akaylor
>> Date: Mon Nov 23 13:16:15 2015
>> New Revision: 253902
>> 
>> URL: http://llvm.org/viewvc/llvm-project?rev=253902&view=rev
>> Log:
>> [WinEH] Fix problem where CodeGenPrepare incorrectly sinks a bitcast into an EH pad.
>> 
>> Differential Revision: http://reviews.llvm.org/D14842
>> 
>> 
>> Added:
>>   llvm/trunk/test/Transforms/CodeGenPrepare/catchpad-phi-cast.ll
>> Modified:
>>   llvm/trunk/lib/CodeGen/CodeGenPrepare.cpp
>> 
>> Modified: llvm/trunk/lib/CodeGen/CodeGenPrepare.cpp
>> URL: 
>> http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/CodeGenPrep
>> are.cpp?rev=253902&r1=253901&r2=253902&view=diff
>> ======================================================================
>> ========
>> --- llvm/trunk/lib/CodeGen/CodeGenPrepare.cpp (original)
>> +++ llvm/trunk/lib/CodeGen/CodeGenPrepare.cpp Mon Nov 23 13:16:15 2015
>> @@ -730,6 +730,12 @@ static bool SinkCast(CastInst *CI) {
>>    // Preincrement use iterator so we don't invalidate it.
>>    ++UI;
>> 
>> +    // If the block selected to receive the cast is an EH pad that does not
>> +    // allow non-PHI instructions before the terminator, we can't sink the
>> +    // cast.
>> +    if (UserBB->getTerminator()->isEHPad())
>> +      continue;
>> +
>>    // If this user is in the same block as the cast, don't change the cast.
>>    if (UserBB == DefBB) continue;
>> 
>> 
>> Added: llvm/trunk/test/Transforms/CodeGenPrepare/catchpad-phi-cast.ll
>> URL: 
>> http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/CodeGen
>> Prepare/catchpad-phi-cast.ll?rev=253902&view=auto
>> ======================================================================
>> ========
>> --- llvm/trunk/test/Transforms/CodeGenPrepare/catchpad-phi-cast.ll 
>> (added)
>> +++ llvm/trunk/test/Transforms/CodeGenPrepare/catchpad-phi-cast.ll Mon 
>> +++ Nov 23 13:16:15 2015
>> @@ -0,0 +1,59 @@
>> +; RUN: opt -codegenprepare -S < %s | FileCheck %s
>> +
>> +; The following target lines are needed for the test to exercise what it should.
>> +; Without these lines, CodeGenPrepare does not try to sink the bitcasts.
>> +target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128"
>> +target triple = "x86_64-pc-windows-msvc"
>> +
>> +declare i32 @__CxxFrameHandler3(...)
>> +
>> +declare void @f()
>> +
>> +declare void @g(i8*)
>> +
>> +; CodeGenPrepare will want to sink these bitcasts, but it selects the 
>> +catchpad ; blocks as the place to which the bitcast should be sunk.  
>> +Since catchpads ; do not allow non-phi instructions before the terminator, this isn't possible.
>> +
>> +; CHECK-LABEL: @test(
>> +define void @test(i32* %addr) personality i32 (...)* 
>> + at __CxxFrameHandler3 { ; CHECK: entry:
>> +; CHECK-NEXT: %x = getelementptr i32, i32* %addr, i32 1 ; CHECK-NEXT: 
>> +%p1 = bitcast i32* %x to i8*
>> +entry:
>> +  %x = getelementptr i32, i32* %addr, i32 1
>> +  %p1 = bitcast i32* %x to i8*
>> +  invoke void @f()
>> +          to label %invoke.cont unwind label %catch1
>> +
>> +; CHECK: invoke.cont:
>> +; CHECK-NEXT: %y = getelementptr i32, i32* %addr, i32 2 ; CHECK-NEXT: 
>> +%p2 = bitcast i32* %y to i8*
>> +invoke.cont:
>> +  %y = getelementptr i32, i32* %addr, i32 2
>> +  %p2 = bitcast i32* %y to i8*
>> +  invoke void @f()
>> +          to label %done unwind label %catch2
>> +
>> +done:
>> +  ret void
>> +
>> +catch1:
>> +  %cp1 = catchpad [] to label %catch.dispatch unwind label %catchend1
>> +
>> +catch2:
>> +  %cp2 = catchpad [] to label %catch.dispatch unwind label %catchend2
>> +
>> +; CHECK: catch.dispatch:
>> +; CHECK-NEXT: %p = phi i8* [ %p1, %catch1 ], [ %p2, %catch2 ]
>> +catch.dispatch:
>> +  %p = phi i8* [ %p1, %catch1 ], [ %p2, %catch2 ]
>> +  call void @g(i8* %p)
>> +  unreachable
>> +
>> +catchend1:
>> +  catchendpad unwind to caller
>> +
>> +catchend2:
>> +  catchendpad unwind to caller
>> +}
>> 
>> 
>> _______________________________________________
>> llvm-commits mailing list
>> llvm-commits at lists.llvm.org
>> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits
> 



More information about the llvm-commits mailing list