summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNikita Popov <npopov@redhat.com>2022-01-11 16:00:13 +0100
committerNikita Popov <npopov@redhat.com>2022-01-11 16:02:35 +0100
commit94d62633914b8dcda2eac5c8e947172e0ddac93d (patch)
treec363cca4eed46c2d7fca92b457b4eb68aad14049
parent138d5c750ba278108ff64b5543644b722642cabc (diff)
downloadllvm-94d62633914b8dcda2eac5c8e947172e0ddac93d.tar.gz
[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.
-rw-r--r--llvm/lib/Transforms/Utils/GlobalStatus.cpp4
-rw-r--r--llvm/test/Transforms/GlobalOpt/stored-once-through-gep.ll30
2 files changed, 31 insertions, 3 deletions
diff --git a/llvm/lib/Transforms/Utils/GlobalStatus.cpp b/llvm/lib/Transforms/Utils/GlobalStatus.cpp
index 9bfc73e4ba6c..440ac63b7b69 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 000000000000..dae4435f445b
--- /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 }
+
+@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
+}