[GlobalStatus] Look through non-constexpr casts
authorNikita Popov <npopov@redhat.com>
Tue, 11 Jan 2022 15:00:13 +0000 (16:00 +0100)
committerNikita Popov <npopov@redhat.com>
Tue, 11 Jan 2022 15:02:35 +0000 (16:02 +0100)
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.

llvm/lib/Transforms/Utils/GlobalStatus.cpp
llvm/test/Transforms/GlobalOpt/stored-once-through-gep.ll [new file with mode: 0644]

index 9bfc73e..440ac63 100644 (file)
@@ -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 (file)
index 0000000..dae4435
--- /dev/null
@@ -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
+}