[PATCH] D124241: [Local] Consider atomic loads from constant global as dead

Nikita Popov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 22 02:19:31 PDT 2022


nikic created this revision.
nikic added reviewers: fhahn, kpn, reames, efriedma.
Herald added a subscriber: hiraditya.
Herald added a project: All.
nikic requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Per the guidance in https://llvm.org/docs/Atomics.html#atomics-and-ir-optimization, an atomic load from a global can be dropped, as it is not observable. Any write to the global would be UB.

IPSCCP will already drop such loads, but the main helper in Local doesn't recognize this currently. This is motivated by D118387 <https://reviews.llvm.org/D118387>.


https://reviews.llvm.org/D124241

Files:
  llvm/lib/Transforms/Utils/Local.cpp
  llvm/test/Transforms/InstCombine/atomic.ll


Index: llvm/test/Transforms/InstCombine/atomic.ll
===================================================================
--- llvm/test/Transforms/InstCombine/atomic.ll
+++ llvm/test/Transforms/InstCombine/atomic.ll
@@ -425,7 +425,6 @@
 
 define i32 @atomic_load_from_constant_global() {
 ; CHECK-LABEL: @atomic_load_from_constant_global(
-; CHECK-NEXT:    [[V:%.*]] = load atomic i32, i32* @c seq_cst, align 4
 ; CHECK-NEXT:    ret i32 42
 ;
   %v = load atomic i32, i32* @c seq_cst, align 4
@@ -434,7 +433,6 @@
 
 define i8 @atomic_load_from_constant_global_bitcast() {
 ; CHECK-LABEL: @atomic_load_from_constant_global_bitcast(
-; CHECK-NEXT:    [[V:%.*]] = load atomic i8, i8* bitcast (i32* @c to i8*) seq_cst, align 1
 ; CHECK-NEXT:    ret i8 42
 ;
   %v = load atomic i8, i8* bitcast (i32* @c to i8*) seq_cst, align 1
Index: llvm/lib/Transforms/Utils/Local.cpp
===================================================================
--- llvm/lib/Transforms/Utils/Local.cpp
+++ llvm/lib/Transforms/Utils/Local.cpp
@@ -500,6 +500,13 @@
     if (isMathLibCallNoop(Call, TLI))
       return true;
 
+  // Atomic loads from constants can be removed.
+  if (auto *LI = dyn_cast<LoadInst>(I))
+    if (auto *GV = dyn_cast<GlobalVariable>(
+            LI->getPointerOperand()->stripPointerCasts()))
+      if (!LI->isVolatile() && GV->isConstant())
+        return true;
+
   return false;
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D124241.424413.patch
Type: text/x-patch
Size: 1386 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220422/fdb63318/attachment.bin>


More information about the llvm-commits mailing list