[PATCH] D75462: [mem2reg] Enhance to ignore @llvm.assume(icmp ne null) uses.

Richard Diamond via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 2 08:29:59 PST 2020


DiamondLovesYou created this revision.
DiamondLovesYou added a reviewer: chandlerc.
Herald added subscribers: llvm-commits, hiraditya.
Herald added a project: LLVM.

These checks should be dead after promotion, so don't block promotion because of them.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D75462

Files:
  llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp
  llvm/test/Transforms/Mem2Reg/ignore-assume-nonnull.ll


Index: llvm/test/Transforms/Mem2Reg/ignore-assume-nonnull.ll
===================================================================
--- /dev/null
+++ llvm/test/Transforms/Mem2Reg/ignore-assume-nonnull.ll
@@ -0,0 +1,13 @@
+; RUN: opt -mem2reg -S -o - < %s | FileCheck %s
+
+declare void @llvm.assume(i1)
+
+define void @test1() {
+; CHECK: test1
+; CHECK-NOT: alloca
+  %A = alloca i32
+  %b = icmp ne i32* %A, null
+  call void @llvm.assume(i1 %b)
+  store i32 1, i32* %A
+  ret void
+}
Index: llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp
===================================================================
--- llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp
+++ llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp
@@ -95,6 +95,28 @@
         return false;
       if (!onlyUsedByLifetimeMarkers(GEPI))
         return false;
+    } else if (const auto *Cmp = dyn_cast<ICmpInst>(U)) {
+      // check if this is a @llvm.assume(icmp ne null), which can be deleted
+      // while promoting.
+      if (Cmp->getPredicate() != CmpInst::ICMP_NE) {
+        return false;
+      }
+      unsigned OtherOperandIdx = 1;
+      if (Cmp->getOperand(0) != AI) {
+        OtherOperandIdx = 0;
+      }
+      auto *Lhs = Cmp->getOperand(OtherOperandIdx)->stripPointerCasts();
+      if (!isa<ConstantPointerNull>(Lhs)) {
+        return false;
+      }
+      for (const User *ICmpU : Cmp->users()) {
+        if (const auto *II = dyn_cast<IntrinsicInst>(ICmpU)) {
+          if (II->getIntrinsicID() == Intrinsic::assume) {
+            continue;
+          }
+        }
+        return false;
+      }
     } else {
       return false;
     }
@@ -312,7 +334,7 @@
   AC->registerAssumption(CI);
 }
 
-static void removeLifetimeIntrinsicUsers(AllocaInst *AI) {
+static void removeLifetimeAndAssumeIntrinsicUsers(AllocaInst *AI) {
   // Knowing that this alloca is promotable, we know that it's safe to kill all
   // instructions except for load and store.
 
@@ -323,7 +345,7 @@
       continue;
 
     if (!I->getType()->isVoidTy()) {
-      // The only users of this bitcast/GEP instruction are lifetime intrinsics.
+      // Remove lifetime/assume intrinsics.
       // Follow the use/def chain to erase them now instead of leaving it for
       // dead code elimination later.
       for (auto UUI = I->user_begin(), UUE = I->user_end(); UUI != UUE;) {
@@ -544,7 +566,7 @@
     assert(AI->getParent()->getParent() == &F &&
            "All allocas should be in the same function, which is same as DF!");
 
-    removeLifetimeIntrinsicUsers(AI);
+    removeLifetimeAndAssumeIntrinsicUsers(AI);
 
     if (AI->use_empty()) {
       // If there are no uses of the alloca, just delete it now.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D75462.247656.patch
Type: text/x-patch
Size: 2696 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200302/ccbfc698/attachment.bin>


More information about the llvm-commits mailing list