[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
This commit is contained in:
Nikita Popov 2022-04-22 10:53:43 +02:00
parent a60fda59dc
commit aae5f8115a
3 changed files with 9 additions and 6 deletions

View File

@ -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;
}

View File

@ -3,16 +3,14 @@
target triple = "powerpc64le-unknown-linux-gnu"
@a = dso_local constant i64 zeroinitializer
@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@toc@ha
; CHECK-NEXT: ld 3, a@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

View File

@ -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