[PATCH] D13305: [InstCombine] Remove trivially empty lifetime start/end ranges.

Arnaud A. de Grandmaison via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 30 12:54:38 PDT 2015


aadg created this revision.
aadg added a reviewer: reames.
aadg added a subscriber: llvm-commits.

Some passes may open up opportunities for optimizations, leaving empty
lifetime start/end ranges. For example, with the following code:
    
    void foo(char *, char *);
    void bar(int Size, bool flag) {
      for (int i = 0; i < Size; ++i) {
        char text[1];
        char buff[1];
        if (flag)
          foo(text, buff); // BBFoo
      }
    }
    
the loop unswitch pass will create 2 versions of the loop, one with
flag==true, and the other one with flag==false, but always leaving
the BBFoo basic block, with lifetime ranges covering the scope of the for
loop. Simplify CFG will then remove BBFoo in the case where flag==false,
but will leave the lifetime markers.

This patch teaches InstCombine to remove trivially empty lifetime marker
ranges, that is ranges ending right after they were started (ignoring
debug info in the range).

This fixes PR24598: excessive compile time after r234581.

http://reviews.llvm.org/D13305

Files:
  lib/Transforms/InstCombine/InstCombineCalls.cpp
  test/Transforms/InstCombine/lifetime.ll

Index: test/Transforms/InstCombine/lifetime.ll
===================================================================
--- /dev/null
+++ test/Transforms/InstCombine/lifetime.ll
@@ -0,0 +1,43 @@
+; RUN: opt < %s -instcombine -S | FileCheck %s
+; RUN: opt < %s -instcombine -S | grep lifetime | count 6
+
+declare void @llvm.lifetime.start(i64, i8* nocapture)
+declare void @llvm.lifetime.end(i64, i8* nocapture)
+declare void @bar(i8* nocapture, i8* nocapture)
+
+define void @foo(i1 %flag) {
+entry:
+; CHECK-LABEL: @foo(
+; CHECK: %[[T:[^ ]+]] = getelementptr inbounds [1 x i8], [1 x i8]* %t
+; CHECK: %[[B:[^ ]+]] = getelementptr inbounds [1 x i8], [1 x i8]* %b
+; CHECK: call void @llvm.lifetime.start(i64 1, i8* %[[T]])
+; CHECK-NEXT: call void @llvm.lifetime.start(i64 1, i8* %[[B]])
+; CHECK-NEXT: call void @bar(i8* %[[B]], i8* %[[T]])
+; CHECK-NEXT: call void @llvm.lifetime.end(i64 1, i8* %[[B]])
+; CHECK-NEXT: call void @llvm.lifetime.end(i64 1, i8* %[[T]])
+  %t = alloca [1 x i8], align 1
+  %b = alloca [1 x i8], align 1
+  %0 = getelementptr inbounds [1 x i8], [1 x i8]* %t, i64 0, i64 0
+  %1 = getelementptr inbounds [1 x i8], [1 x i8]* %b, i64 0, i64 0
+  br i1 %flag, label %if, label %else
+
+if:
+  call void @llvm.lifetime.start(i64 1, i8* %0)
+  call void @llvm.lifetime.start(i64 1, i8* %1)
+  call void @llvm.lifetime.end(i64 1, i8* %1)
+  call void @llvm.lifetime.end(i64 1, i8* %0)
+  br label %fin
+
+else:
+  call void @llvm.lifetime.start(i64 1, i8* %0)
+  call void @llvm.lifetime.start(i64 1, i8* %1)
+  call void @bar(i8* %1, i8* %0)
+  call void @llvm.lifetime.end(i64 1, i8* %1)
+  call void @llvm.lifetime.end(i64 1, i8* %0)
+  br  label %fin
+
+fin:
+  ret void
+}
+
+
Index: lib/Transforms/InstCombine/InstCombineCalls.cpp
===================================================================
--- lib/Transforms/InstCombine/InstCombineCalls.cpp
+++ lib/Transforms/InstCombine/InstCombineCalls.cpp
@@ -1392,6 +1392,24 @@
       return EraseInstFromFunction(CI);
     break;
   }
+  case Intrinsic::lifetime_start: {
+    // Remove trivially empty lifetime_start/end ranges, i.e. a start
+    // immediately followed by an end (ignoring debuginfo in between).
+    BasicBlock::iterator BI = II, BE = II->getParent()->end();
+    for (++BI; BI != BE; ++BI) {
+      if (isa<DbgInfoIntrinsic>(BI))
+        continue;
+      if (IntrinsicInst *LTE = dyn_cast<IntrinsicInst>(BI))
+        if (LTE->getIntrinsicID() == Intrinsic::lifetime_end &&
+            II->getOperand(0) == LTE->getOperand(0) &&
+            II->getOperand(1) == LTE->getOperand(1)) {
+          EraseInstFromFunction(*LTE);
+          return EraseInstFromFunction(*II);
+        }
+      break;
+    }
+    break;
+  }
   case Intrinsic::assume: {
     // Canonicalize assume(a && b) -> assume(a); assume(b);
     // Note: New assumption intrinsics created here are registered by


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D13305.36141.patch
Type: text/x-patch
Size: 2879 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150930/a607c452/attachment.bin>


More information about the llvm-commits mailing list