[llvm] aae5f81 - [Local] Consider atomic loads from constant global as dead

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Mon May 2 01:53:08 PDT 2022


Author: Nikita Popov
Date: 2022-05-02T10:52:58+02:00
New Revision: aae5f8115a7c9ab2e323cae8a1528d38fc3652f4

URL: https://github.com/llvm/llvm-project/commit/aae5f8115a7c9ab2e323cae8a1528d38fc3652f4
DIFF: https://github.com/llvm/llvm-project/commit/aae5f8115a7c9ab2e323cae8a1528d38fc3652f4.diff

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

Per the guidance in
https://llvm.org/docs/Atomics.html#atomics-and-ir-optimization,
an atomic load from a constant global can be dropped, as there can
be no stores to synchronize with. Any write to the constant 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.

Differential Revision: https://reviews.llvm.org/D124241

Added: 
    

Modified: 
    llvm/lib/Transforms/Utils/Local.cpp
    llvm/test/CodeGen/PowerPC/atomics-constant.ll
    llvm/test/Transforms/InstCombine/atomic.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Utils/Local.cpp b/llvm/lib/Transforms/Utils/Local.cpp
index 7a9a272691b3d..e72e3cee1d4b9 100644
--- a/llvm/lib/Transforms/Utils/Local.cpp
+++ b/llvm/lib/Transforms/Utils/Local.cpp
@@ -500,6 +500,13 @@ bool llvm::wouldInstructionBeTriviallyDead(Instruction *I,
     if (isMathLibCallNoop(Call, TLI))
       return true;
 
+  // Non-volatile 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;
 }
 

diff  --git a/llvm/test/CodeGen/PowerPC/atomics-constant.ll b/llvm/test/CodeGen/PowerPC/atomics-constant.ll
index 80f84d89f5fa4..3a7da6f9c748f 100644
--- a/llvm/test/CodeGen/PowerPC/atomics-constant.ll
+++ b/llvm/test/CodeGen/PowerPC/atomics-constant.ll
@@ -3,16 +3,14 @@
 
 target triple = "powerpc64le-unknown-linux-gnu"
 
- at a = dso_local constant i64 zeroinitializer
+ at a = dso_local global i64 zeroinitializer
 
 define i64 @foo() {
 ; CHECK-LABEL: foo:
 ; CHECK:       # %bb.0: # %entry
-; CHECK-NEXT:    li 4, 0
 ; CHECK-NEXT:    addis 3, 2, a at toc@ha
 ; CHECK-NEXT:    ld 3, a at toc@l(3)
-; CHECK-NEXT:    cmpd 7, 4, 4
-; CHECK-NEXT:    li 3, 0
+; CHECK-NEXT:    cmpd 7, 3, 3
 ; CHECK-NEXT:    bne- 7, .+4
 ; CHECK-NEXT:    isync
 ; CHECK-NEXT:    blr

diff  --git a/llvm/test/Transforms/InstCombine/atomic.ll b/llvm/test/Transforms/InstCombine/atomic.ll
index ec79891af11e5..e3afffeb9b4a8 100644
--- a/llvm/test/Transforms/InstCombine/atomic.ll
+++ b/llvm/test/Transforms/InstCombine/atomic.ll
@@ -426,7 +426,6 @@ define void @no_atomic_vector_store(<2 x float> %p, i8* %p2) {
 
 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
@@ -435,7 +434,6 @@ define i32 @atomic_load_from_constant_global() {
 
 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


        


More information about the llvm-commits mailing list