[llvm] 94d6263 - [GlobalStatus] Look through non-constexpr casts
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Tue Jan 11 07:02:44 PST 2022
Author: Nikita Popov
Date: 2022-01-11T16:02:35+01:00
New Revision: 94d62633914b8dcda2eac5c8e947172e0ddac93d
URL: https://github.com/llvm/llvm-project/commit/94d62633914b8dcda2eac5c8e947172e0ddac93d
DIFF: https://github.com/llvm/llvm-project/commit/94d62633914b8dcda2eac5c8e947172e0ddac93d.diff
LOG: [GlobalStatus] Look through non-constexpr casts
analyzeGlobal() looks through non-constexpr cast instructions when
looking for users. However, this particular place only strips the
casts again if they are constexprs. We should be looking through all
casts here.
Added:
llvm/test/Transforms/GlobalOpt/stored-once-through-gep.ll
Modified:
llvm/lib/Transforms/Utils/GlobalStatus.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/Utils/GlobalStatus.cpp b/llvm/lib/Transforms/Utils/GlobalStatus.cpp
index 9bfc73e4ba6cf..440ac63b7b69e 100644
--- a/llvm/lib/Transforms/Utils/GlobalStatus.cpp
+++ b/llvm/lib/Transforms/Utils/GlobalStatus.cpp
@@ -105,9 +105,7 @@ static bool analyzeGlobalAux(const Value *V, GlobalStatus &GS,
// value, not an aggregate), keep more specific information about
// stores.
if (GS.StoredType != GlobalStatus::Stored) {
- const Value *Ptr = SI->getPointerOperand();
- if (isa<ConstantExpr>(Ptr))
- Ptr = Ptr->stripPointerCasts();
+ const Value *Ptr = SI->getPointerOperand()->stripPointerCasts();
if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Ptr)) {
Value *StoredVal = SI->getOperand(0);
diff --git a/llvm/test/Transforms/GlobalOpt/stored-once-through-gep.ll b/llvm/test/Transforms/GlobalOpt/stored-once-through-gep.ll
new file mode 100644
index 0000000000000..dae4435f445be
--- /dev/null
+++ b/llvm/test/Transforms/GlobalOpt/stored-once-through-gep.ll
@@ -0,0 +1,30 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt -S -globalopt < %s | FileCheck %s
+
+; The global is stored once through a trivial GEP instruction (rather than
+; GEP constant expression) here. We should still be able to optimize it.
+
+%s = type { i32 }
+
+ at g = internal unnamed_addr global i32 undef
+
+; CHECK-NOT: @g =
+
+define void @store() {
+; CHECK-LABEL: @store(
+; CHECK-NEXT: ret void
+;
+ %addr = getelementptr inbounds %s, %s* bitcast (i32* @g to %s*), i64 0, i32 0
+ store i32 1, i32* %addr, align 4
+ ret void
+}
+
+define i32 @load() {
+; CHECK-LABEL: @load(
+; CHECK-NEXT: call fastcc void @store()
+; CHECK-NEXT: ret i32 1
+;
+ call fastcc void @store()
+ %v = load i32, i32* @g
+ ret i32 %v
+}
More information about the llvm-commits
mailing list