Skip to content

Commit aae5f81

Browse files
committed
[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
1 parent a60fda5 commit aae5f81

File tree

3 files changed

+9
-6
lines changed

3 files changed

+9
-6
lines changed

llvm/lib/Transforms/Utils/Local.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,13 @@ bool llvm::wouldInstructionBeTriviallyDead(Instruction *I,
500500
if (isMathLibCallNoop(Call, TLI))
501501
return true;
502502

503+
// Non-volatile atomic loads from constants can be removed.
504+
if (auto *LI = dyn_cast<LoadInst>(I))
505+
if (auto *GV = dyn_cast<GlobalVariable>(
506+
LI->getPointerOperand()->stripPointerCasts()))
507+
if (!LI->isVolatile() && GV->isConstant())
508+
return true;
509+
503510
return false;
504511
}
505512

llvm/test/CodeGen/PowerPC/atomics-constant.ll

+2-4
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,14 @@
33

44
target triple = "powerpc64le-unknown-linux-gnu"
55

6-
@a = dso_local constant i64 zeroinitializer
6+
@a = dso_local global i64 zeroinitializer
77

88
define i64 @foo() {
99
; CHECK-LABEL: foo:
1010
; CHECK: # %bb.0: # %entry
11-
; CHECK-NEXT: li 4, 0
1211
; CHECK-NEXT: addis 3, 2, a@toc@ha
1312
; CHECK-NEXT: ld 3, a@toc@l(3)
14-
; CHECK-NEXT: cmpd 7, 4, 4
15-
; CHECK-NEXT: li 3, 0
13+
; CHECK-NEXT: cmpd 7, 3, 3
1614
; CHECK-NEXT: bne- 7, .+4
1715
; CHECK-NEXT: isync
1816
; CHECK-NEXT: blr

llvm/test/Transforms/InstCombine/atomic.ll

-2
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,6 @@ define void @no_atomic_vector_store(<2 x float> %p, i8* %p2) {
426426

427427
define i32 @atomic_load_from_constant_global() {
428428
; CHECK-LABEL: @atomic_load_from_constant_global(
429-
; CHECK-NEXT: [[V:%.*]] = load atomic i32, i32* @c seq_cst, align 4
430429
; CHECK-NEXT: ret i32 42
431430
;
432431
%v = load atomic i32, i32* @c seq_cst, align 4
@@ -435,7 +434,6 @@ define i32 @atomic_load_from_constant_global() {
435434

436435
define i8 @atomic_load_from_constant_global_bitcast() {
437436
; CHECK-LABEL: @atomic_load_from_constant_global_bitcast(
438-
; CHECK-NEXT: [[V:%.*]] = load atomic i8, i8* bitcast (i32* @c to i8*) seq_cst, align 1
439437
; CHECK-NEXT: ret i8 42
440438
;
441439
%v = load atomic i8, i8* bitcast (i32* @c to i8*) seq_cst, align 1

0 commit comments

Comments
 (0)