[PATCH] D66604: Fix for bug #20811

Shreyansh Chouhan via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 22 10:46:37 PDT 2019


BK1603 updated this revision to Diff 216667.
BK1603 added a comment.

Added test


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D66604/new/

https://reviews.llvm.org/D66604

Files:
  llvm/lib/Transforms/Scalar/GVN.cpp
  llvm/test/Transforms/GVN/lifetime-end.ll


Index: llvm/test/Transforms/GVN/lifetime-end.ll
===================================================================
--- /dev/null
+++ llvm/test/Transforms/GVN/lifetime-end.ll
@@ -0,0 +1,13 @@
+; RUN: opt < %s -basicaa -gvn -S | FileCheck %s
+
+define i8 @test(i8* %P) nounwind {
+; CHECK: lifetime.end
+; CHECK-NOT: load
+; CHECK:  ret i8 undef
+entry:
+  call void @llvm.lifetime.end(i64 32, i8* %P)
+  %0 = load i8, i8* %P
+  ret i8 %0
+}
+
+declare void @llvm.lifetime.end(i64 %S, i8* nocapture %P)
Index: llvm/lib/Transforms/Scalar/GVN.cpp
===================================================================
--- llvm/lib/Transforms/Scalar/GVN.cpp
+++ llvm/lib/Transforms/Scalar/GVN.cpp
@@ -822,6 +822,12 @@
   return false;
 }
 
+static bool isLifetimeEnd(const Instruction *Inst) {
+  if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst))
+    return II->getIntrinsicID() == Intrinsic::lifetime_end;
+  return false;
+}
+
 /// Try to locate the three instruction involved in a missed
 /// load-elimination case that is due to an intervening store.
 static void reportMayClobberedLoad(LoadInst *LI, MemDepResult DepInfo,
@@ -864,6 +870,15 @@
   const DataLayout &DL = LI->getModule()->getDataLayout();
 
   Instruction *DepInst = DepInfo.getInst();
+
+  // Loading the allocation -> undef.
+  if (isa<AllocaInst>(DepInst) || isMallocLikeFn(DepInst, TLI) ||
+      // Loading immediately after lifetime begin or end -> undef.
+      isLifetimeStart(DepInst) || isLifetimeEnd(DepInst)) {
+    Res = AvailableValue::get(UndefValue::get(LI->getType()));
+    return true;
+  }
+
   if (DepInfo.isClobber()) {
     // If the dependence is to a store that writes to a superset of the bits
     // read by the load, we can extract the bits we need for the load from the
@@ -923,14 +938,6 @@
   }
   assert(DepInfo.isDef() && "follows from above");
 
-  // Loading the allocation -> undef.
-  if (isa<AllocaInst>(DepInst) || isMallocLikeFn(DepInst, TLI) ||
-      // Loading immediately after lifetime begin -> undef.
-      isLifetimeStart(DepInst)) {
-    Res = AvailableValue::get(UndefValue::get(LI->getType()));
-    return true;
-  }
-
   // Loading from calloc (which zero initializes memory) -> zero
   if (isCallocLikeFn(DepInst, TLI)) {
     Res = AvailableValue::get(Constant::getNullValue(LI->getType()));


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D66604.216667.patch
Type: text/x-patch
Size: 2321 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190822/99855dec/attachment.bin>


More information about the llvm-commits mailing list