[llvm-commits] [llvm] r164866 - in /llvm/trunk: lib/Transforms/Scalar/CodeGenPrepare.cpp test/CodeGen/X86/2012-09-28-CGPBug.ll

Benjamin Kramer benny.kra at gmail.com
Sun Oct 7 11:20:23 PDT 2012


On 07.10.2012, at 20:06, Evan Cheng <evan.cheng at apple.com> wrote:

> In global JT, he addresses of other blocks are used. If they are merged into other blocks, then you end up with invalid values like this:
> _JT:
>        .long   Ltmp2-Ltmp0
>        .long   Ltmp1-Ltmp0
>        .long   1-Ltmp2  
>        .long   1-Ltmp1

Taking the address of a block that's not used in an indirectbr is undefined behavior (except for comparisons against null). There are many passes that can discard dead blocks and cause this kind of code to be emitted and I think the problem here is that the assembler chokes on "1-label", I see a couple of ways to fix this but none is really satisfying:

- Turn the dead blockaddrs into references to the containing function. This is tricky to implement because you can't take the address of an entry block, and was discarded as a bad idea before.
- Turn the dead blockaddrs into undef. I think that could work but I'm not really sure about the implications.
- Teach the assembler to deal with this kind of thing, probably not worth it.

- Ben
> 
> Evan
> 
> On Oct 5, 2012, at 1:05 PM, Rafael EspĂ­ndola <rafael.espindola at gmail.com> wrote:
> 
>> Sorry, what was the problem? The indirectbr in the testcase can only
>> jump to label %17 and label %18, so it should be fine to remove the
>> other basic blocks.
>> 
>> On 28 September 2012 19:58, Evan Cheng <evan.cheng at apple.com> wrote:
>>> Author: evancheng
>>> Date: Fri Sep 28 18:58:57 2012
>>> New Revision: 164866
>>> 
>>> URL: http://llvm.org/viewvc/llvm-project?rev=164866&view=rev
>>> Log:
>>> Do not delete BBs if their addresses are taken. rdar://12396696
>>> 
>>> Added:
>>>   llvm/trunk/test/CodeGen/X86/2012-09-28-CGPBug.ll
>>> Modified:
>>>   llvm/trunk/lib/Transforms/Scalar/CodeGenPrepare.cpp
>>> 
>>> Modified: llvm/trunk/lib/Transforms/Scalar/CodeGenPrepare.cpp
>>> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/CodeGenPrepare.cpp?rev=164866&r1=164865&r2=164866&view=diff
>>> ==============================================================================
>>> --- llvm/trunk/lib/Transforms/Scalar/CodeGenPrepare.cpp (original)
>>> +++ llvm/trunk/lib/Transforms/Scalar/CodeGenPrepare.cpp Fri Sep 28 18:58:57 2012
>>> @@ -226,7 +226,8 @@
>>>    // edge, just collapse it.
>>>    BasicBlock *SinglePred = BB->getSinglePredecessor();
>>> 
>>> -    if (!SinglePred || SinglePred == BB) continue;
>>> +    // Don't merge if BB's address is taken.
>>> +    if (!SinglePred || SinglePred == BB || BB->hasAddressTaken()) continue;
>>> 
>>>    BranchInst *Term = dyn_cast<BranchInst>(SinglePred->getTerminator());
>>>    if (Term && !Term->isConditional()) {
>>> @@ -788,7 +789,7 @@
>>>  }
>>> 
>>>  // If we eliminated all predecessors of the block, delete the block now.
>>> -  if (Changed && pred_begin(BB) == pred_end(BB))
>>> +  if (Changed && !BB->hasAddressTaken() && pred_begin(BB) == pred_end(BB))
>>>    BB->eraseFromParent();
>>> 
>>>  return Changed;
>>> 
>>> Added: llvm/trunk/test/CodeGen/X86/2012-09-28-CGPBug.ll
>>> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/X86/2012-09-28-CGPBug.ll?rev=164866&view=auto
>>> ==============================================================================
>>> --- llvm/trunk/test/CodeGen/X86/2012-09-28-CGPBug.ll (added)
>>> +++ llvm/trunk/test/CodeGen/X86/2012-09-28-CGPBug.ll Fri Sep 28 18:58:57 2012
>>> @@ -0,0 +1,53 @@
>>> +; RUN: llc -mtriple=i386-apple-macosx < %s | FileCheck %s
>>> +; rdar://12396696
>>> +
>>> + at JT = global [4 x i32] [i32 sub (i32 ptrtoint (i8* blockaddress(@h, %18) to i32), i32 ptrtoint (i8* blockaddress(@h, %11) to i32)), i32 sub (i32 ptrtoint (i8* blockaddress(@h, %17) to i32), i32 ptrtoint (i8* blockaddress(@h, %11) to i32)), i32 sub (i32 ptrtoint (i8* blockaddress(@h, %22) to i32), i32 ptrtoint (i8* blockaddress(@h, %18) to i32)), i32 sub (i32 ptrtoint (i8* blockaddress(@h, %22) to i32), i32 ptrtoint (i8* blockaddress(@h, %17) to i32))]
>>> + at gGlobalLock = external global i8*
>>> + at .str40 = external global [35 x i8]
>>> +
>>> +; CHECK: _JT:
>>> +; CHECK-NOT: .long Ltmp{{[0-9]+}}-1
>>> +; CHECK-NOT: .long 1-Ltmp{{[0-9]+}}
>>> +; CHECK: .long Ltmp{{[0-9]+}}-Ltmp{{[0-9]+}}
>>> +; CHECK: .long Ltmp{{[0-9]+}}-Ltmp{{[0-9]+}}
>>> +; CHECK: .long Ltmp{{[0-9]+}}-Ltmp{{[0-9]+}}
>>> +; CHECK: .long Ltmp{{[0-9]+}}-Ltmp{{[0-9]+}}
>>> +
>>> +define void @h(i8*) nounwind ssp {
>>> +  %2 = alloca i8*
>>> +  store i8* %0, i8** %2
>>> +  %3 = load i8** %2
>>> +  %4 = bitcast i8* %3 to { i32, i32 }*
>>> +  %5 = getelementptr { i32, i32 }* %4, i32 0, i32 0
>>> +  %6 = load i32* %5
>>> +  %7 = srem i32 %6, 2
>>> +  %8 = icmp slt i32 %6, 2
>>> +  %9 = select i1 %8, i32 %6, i32 %7
>>> +  %10 = icmp eq i32 %9, 0
>>> +  br label %11
>>> +
>>> +; <label>:11                                      ; preds = %1
>>> +  %12 = zext i1 %10 to i32
>>> +  %13 = getelementptr [4 x i32]* @JT, i32 0, i32 %12
>>> +  %14 = load i32* %13
>>> +  %15 = add i32 %14, ptrtoint (i8* blockaddress(@h, %11) to i32)
>>> +  %16 = inttoptr i32 %15 to i8*
>>> +  indirectbr i8* %16, [label %17, label %18]
>>> +
>>> +; <label>:17                                      ; preds = %11
>>> +  tail call void (i8*, ...)* @g(i8* getelementptr inbounds ([35 x i8]* @.str40, i32 0, i32 0))
>>> +  br label %22
>>> +
>>> +; <label>:18                                      ; preds = %11
>>> +  %19 = call i32 @f(i32 -1037694186) nounwind
>>> +  %20 = inttoptr i32 %19 to i32 (i8**)*
>>> +  %21 = tail call i32 %20(i8** @gGlobalLock)
>>> +  br label %22
>>> +
>>> +; <label>:22                                      ; preds = %18, %17
>>> +  ret void
>>> +}
>>> +
>>> +declare i32 @f(i32)
>>> +
>>> +declare void @g(i8*, ...)
>>> 
>>> 
>>> _______________________________________________
>>> llvm-commits mailing list
>>> llvm-commits at cs.uiuc.edu
>>> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
> 
> 
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits





More information about the llvm-commits mailing list