[llvm] [LoopUnroll] Remove unused lifetime marker pairs (PR #106858)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 10 00:20:02 PDT 2024


https://github.com/dtcxzyw updated https://github.com/llvm/llvm-project/pull/106858

>From 5702950a4489e8e548defeef5f2283190c1e8b2c Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Sat, 31 Aug 2024 23:57:14 +0800
Subject: [PATCH] [LoopUnroll] Remove unused lifetime marker pairs

---
 llvm/lib/Transforms/Utils/LoopUnroll.cpp | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/llvm/lib/Transforms/Utils/LoopUnroll.cpp b/llvm/lib/Transforms/Utils/LoopUnroll.cpp
index a0406111ecbf3b..049395800e378a 100644
--- a/llvm/lib/Transforms/Utils/LoopUnroll.cpp
+++ b/llvm/lib/Transforms/Utils/LoopUnroll.cpp
@@ -375,7 +375,12 @@ void llvm::simplifyLoopAfterUnroll(Loop *L, bool SimplifyIVs, LoopInfo *LI,
   // At this point, the code is well formed.  Perform constprop, instsimplify,
   // and dce.
   const DataLayout &DL = L->getHeader()->getDataLayout();
+  Function *F = L->getHeader()->getParent();
   SmallVector<WeakTrackingVH, 16> DeadInsts;
+  bool DoNotOptimizeLifetimeMarkers =
+      F->hasFnAttribute(Attribute::SanitizeAddress) ||
+      F->hasFnAttribute(Attribute::SanitizeMemory) ||
+      F->hasFnAttribute(Attribute::SanitizeHWAddress);
   for (BasicBlock *BB : L->getBlocks()) {
     // Remove repeated debug instructions after loop unrolling.
     if (BB->getParent()->getSubprogram())
@@ -387,6 +392,22 @@ void llvm::simplifyLoopAfterUnroll(Loop *L, bool SimplifyIVs, LoopInfo *LI,
           Inst.replaceAllUsesWith(V);
       if (isInstructionTriviallyDead(&Inst))
         DeadInsts.emplace_back(&Inst);
+      if (!DoNotOptimizeLifetimeMarkers) {
+        if (auto *II = dyn_cast<IntrinsicInst>(&Inst)) {
+          if (II->getIntrinsicID() == Intrinsic::lifetime_start) {
+            if (auto *PrevII =
+                    dyn_cast_or_null<IntrinsicInst>(II->getPrevNode()))
+              if (PrevII->getIntrinsicID() == Intrinsic::lifetime_end) {
+                if (PrevII->getArgOperand(0) == II->getArgOperand(0) &&
+                    PrevII->getArgOperand(1) == II->getArgOperand(1)) {
+                  PrevII->eraseFromParent();
+                  II->eraseFromParent();
+                  continue;
+                }
+              }
+          }
+        }
+      }
 
       // Fold ((add X, C1), C2) to (add X, C1+C2). This is very common in
       // unrolled loops, and handling this early allows following code to



More information about the llvm-commits mailing list