diff --git a/llvm/lib/Transforms/Utils/GlobalStatus.cpp b/llvm/lib/Transforms/Utils/GlobalStatus.cpp index 9bfc73e4ba6cfe..440ac63b7b69e7 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(Ptr)) - Ptr = Ptr->stripPointerCasts(); + const Value *Ptr = SI->getPointerOperand()->stripPointerCasts(); if (const GlobalVariable *GV = dyn_cast(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 00000000000000..dae4435f445be3 --- /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 +}