From aae5f8115a7c9ab2e323cae8a1528d38fc3652f4 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Fri, 22 Apr 2022 10:53:43 +0200 Subject: [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 --- llvm/lib/Transforms/Utils/Local.cpp | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'llvm/lib/Transforms/Utils/Local.cpp') 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(I)) + if (auto *GV = dyn_cast( + LI->getPointerOperand()->stripPointerCasts())) + if (!LI->isVolatile() && GV->isConstant()) + return true; + return false; } -- cgit v1.1