[PATCH] D128128: [GlobalOpt] Perform store->dominated load forwarding for stored once globals

Arthur Eubanks via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Jun 18 16:17:12 PDT 2022


aeubanks created this revision.
aeubanks added a reviewer: nikic.
Herald added subscribers: ormris, hiraditya.
Herald added a project: All.
aeubanks requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Slight increase in lencod LTO compile time, otherwise neutral:
https://llvm-compile-time-tracker.com/compare.php?from=07b9937d0c5373a51fa24bf6b3efd8d7d74de050&to=079dbbfa97e4b281430c6cb0001fac742bfd05b0&stat=instructions


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D128128

Files:
  llvm/lib/Transforms/IPO/GlobalOpt.cpp
  llvm/test/Transforms/GlobalOpt/stored-once-forward-value.ll


Index: llvm/test/Transforms/GlobalOpt/stored-once-forward-value.ll
===================================================================
--- llvm/test/Transforms/GlobalOpt/stored-once-forward-value.ll
+++ llvm/test/Transforms/GlobalOpt/stored-once-forward-value.ll
@@ -10,10 +10,8 @@
 
 define i1 @dom_const() {
 ; CHECK-LABEL: @dom_const(
-; CHECK-NEXT:    store i1 true, ptr @g1, align 1
 ; CHECK-NEXT:    call void @b()
-; CHECK-NEXT:    [[R:%.*]] = load i1, ptr @g1, align 1
-; CHECK-NEXT:    ret i1 [[R]]
+; CHECK-NEXT:    ret i1 true
 ;
   store i1 true, ptr @g1
   call void @b()
@@ -23,10 +21,8 @@
 
 define i32 @dom_arg(i32 %a) {
 ; CHECK-LABEL: @dom_arg(
-; CHECK-NEXT:    store i32 [[A:%.*]], ptr @g2, align 4
 ; CHECK-NEXT:    call void @b()
-; CHECK-NEXT:    [[R:%.*]] = load i32, ptr @g2, align 4
-; CHECK-NEXT:    ret i32 [[R]]
+; CHECK-NEXT:    ret i32 [[A:%.*]]
 ;
   store i32 %a, ptr @g2
   call void @b()
Index: llvm/lib/Transforms/IPO/GlobalOpt.cpp
===================================================================
--- llvm/lib/Transforms/IPO/GlobalOpt.cpp
+++ llvm/lib/Transforms/IPO/GlobalOpt.cpp
@@ -1598,6 +1598,33 @@
     if (optimizeOnceStoredGlobal(GV, StoredOnceValue, GS.Ordering, DL, GetTLI))
       return true;
 
+    // If the store dominates any loads, those loads will always load the
+    // stored value (as opposed to the initializer), even in the presence of
+    // recursion. Restrict to the case where all loads/stores of the global are
+    // in the same function.
+    if (!GS.HasMultipleAccessingFunctions && GS.AccessingFunction) {
+      SmallVector<LoadInst *> Loads;
+      for (User *U : GV->users()) {
+        if (auto *LI = dyn_cast<LoadInst>(U))
+          Loads.push_back(LI);
+      }
+      // Only compute DT if we have any loads to examine.
+      bool MadeChange = false;
+      if (!Loads.empty()) {
+        auto &DT = LookupDomTree(*const_cast<Function *>(GS.AccessingFunction));
+        for (auto *LI : Loads) {
+          if (LI->getType() == StoredOnceValue->getType() &&
+              DT.dominates(GS.StoredOnceStore, LI)) {
+            LI->replaceAllUsesWith(StoredOnceValue);
+            LI->eraseFromParent();
+            MadeChange = true;
+          }
+        }
+      }
+      if (MadeChange)
+        return true;
+    }
+
     // Otherwise, if the global was not a boolean, we can shrink it to be a
     // boolean. Skip this optimization for AS that doesn't allow an initializer.
     if (SOVConstant && GS.Ordering == AtomicOrdering::NotAtomic &&


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D128128.438159.patch
Type: text/x-patch
Size: 2532 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220618/a7905d9b/attachment-0001.bin>


More information about the llvm-commits mailing list