[PATCH] SimplifyCFG: Avoid miscompilations due to removed lifetime intrinsics

Björn Steinbrink bsteinbr at gmail.com
Mon Jul 28 13:51:39 PDT 2014


Hi rnk, rafael,

For example in a loop, just removing a lifetime.end intrinsic can
interact badly with loop rotation, which might move the corresponding
lifetime.start intrinsic to the end of the loop.

So with the lifetime.end intrinsic removed you can end up with something
like:

    block:
        store i8 %foo, i8* %bar
        call void @llvm.lifetime.start(i64 1, i8* %bar)

Without a corresponding lifetime.end, meaning that the store is invalid.
If the lifetime.end intrinsic is kept, we get this instead:

    block:
        store i8 %foo, i8* %bar
        call void @llvm.lifetime.end(i64 1, i8* %bar)
        call void @llvm.lifetime.start(i64 1, i8* %bar)

Which is fine, since the store is within a valid lifetime region.

http://reviews.llvm.org/D4699

Files:
  lib/Transforms/Utils/SimplifyCFG.cpp
  test/Transforms/SimplifyCFG/lifetime.ll

Index: lib/Transforms/Utils/SimplifyCFG.cpp
===================================================================
--- lib/Transforms/Utils/SimplifyCFG.cpp
+++ lib/Transforms/Utils/SimplifyCFG.cpp
@@ -4008,7 +4008,7 @@
     return true;
 
   // If the Terminator is the only non-phi instruction, simplify the block.
-  BasicBlock::iterator I = BB->getFirstNonPHIOrDbgOrLifetime();
+  BasicBlock::iterator I = BB->getFirstNonPHIOrDbg();
   if (I->isTerminator() && BB != &BB->getParent()->getEntryBlock() &&
       TryToSimplifyUncondBranchFromEmptyBlock(BB))
     return true;
Index: test/Transforms/SimplifyCFG/lifetime.ll
===================================================================
--- test/Transforms/SimplifyCFG/lifetime.ll
+++ test/Transforms/SimplifyCFG/lifetime.ll
@@ -1,11 +1,11 @@
 ; RUN: opt < %s -simplifycfg -S | FileCheck %s
 
-; Test that a lifetime intrinsic doesn't prevent us from simplifying this.
+; Test that a lifetime intrinsic isn't removed because that would change semantics
 
 ; CHECK: foo
 ; CHECK: entry:
-; CHECK-NOT: bb0:
-; CHECK-NOT: bb1:
+; CHECK: bb0:
+; CHECK: bb1:
 ; CHECK: ret
 define void @foo(i1 %x) {
 entry:
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D4699.11955.patch
Type: text/x-patch
Size: 1155 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20140728/8f08bd5f/attachment.bin>


More information about the llvm-commits mailing list