aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Transforms/Utils/Local.cpp
diff options
context:
space:
mode:
authorNikita Popov <npopov@redhat.com>2022-04-22 10:53:43 +0200
committerNikita Popov <npopov@redhat.com>2022-05-02 10:52:58 +0200
commitaae5f8115a7c9ab2e323cae8a1528d38fc3652f4 (patch)
tree5279a3351e5136a6a61cef7c83a59a63ec6afdc4 /llvm/lib/Transforms/Utils/Local.cpp
parenta60fda59dc6b1dda25cad26214b02d1f630319e7 (diff)
downloadllvm-aae5f8115a7c9ab2e323cae8a1528d38fc3652f4.zip
llvm-aae5f8115a7c9ab2e323cae8a1528d38fc3652f4.tar.gz
llvm-aae5f8115a7c9ab2e323cae8a1528d38fc3652f4.tar.bz2
[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
Diffstat (limited to 'llvm/lib/Transforms/Utils/Local.cpp')
-rw-r--r--llvm/lib/Transforms/Utils/Local.cpp7
1 files changed, 7 insertions, 0 deletions
diff --git a/llvm/lib/Transforms/Utils/Local.cpp b/llvm/lib/Transforms/Utils/Local.cpp
index 7a9a272..e72e3ce 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;
}