diff --git a/llvm/lib/Transforms/IPO/GlobalOpt.cpp b/llvm/lib/Transforms/IPO/GlobalOpt.cpp index bdda4980c1005..d7edd1288309b 100644 --- a/llvm/lib/Transforms/IPO/GlobalOpt.cpp +++ b/llvm/lib/Transforms/IPO/GlobalOpt.cpp @@ -1133,9 +1133,6 @@ static bool optimizeOnceStoredGlobal(GlobalVariable *GV, Value *StoredOnceVal, const DataLayout &DL, function_ref GetTLI) { - // Ignore no-op GEPs and bitcasts. - StoredOnceVal = StoredOnceVal->stripPointerCasts(); - // If we are dealing with a pointer global that is initialized to null and // only has one (non-null) value stored into it, then we can optimize any // users of the loaded value (often calls and loads) that would trap if the diff --git a/llvm/test/Transforms/GlobalOpt/stored-once-addrspacecast.ll b/llvm/test/Transforms/GlobalOpt/stored-once-addrspacecast.ll new file mode 100644 index 0000000000000..35678598032dc --- /dev/null +++ b/llvm/test/Transforms/GlobalOpt/stored-once-addrspacecast.ll @@ -0,0 +1,42 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals +; RUN: opt -passes=globalopt < %s -S | FileCheck %s + +; Test that we do not fold away addresscasts when optimizing once-stored +; globals, as these may be runtime operations. + +@g1 = internal global ptr null +@g2 = addrspace(1) global i32 0 + +;. +; CHECK: @g1 = internal unnamed_addr global ptr null +; CHECK: @g2 = addrspace(1) global i32 0 +; CHECK: @g4 = local_unnamed_addr addrspace(1) global i32 0 +;. +define i64 @test1() { +; CHECK-LABEL: @test1( +; CHECK-NEXT: [[TMP1:%.*]] = addrspacecast ptr addrspace(1) @g2 to ptr +; CHECK-NEXT: store ptr [[TMP1]], ptr @g1, align 8 +; CHECK-NEXT: [[TMP2:%.*]] = load ptr, ptr @g1, align 8 +; CHECK-NEXT: [[TMP3:%.*]] = load i64, ptr [[TMP2]], align 8 +; CHECK-NEXT: ret i64 [[TMP3]] +; + %l1 = addrspacecast ptr addrspace(1) @g2 to ptr + store ptr %l1, ptr @g1, align 8 + %l2 = load ptr, ptr @g1, align 8 + %l3 = load i64, ptr %l2, align 8 + ret i64 %l3 +} + +@g3 = internal global ptr null +@g4 = addrspace(1) global i32 0 + +define i64 @test2() { +; CHECK-LABEL: @test2( +; CHECK-NEXT: [[TMP1:%.*]] = load i64, ptr addrspacecast (ptr addrspace(1) @g4 to ptr), align 8 +; CHECK-NEXT: ret i64 [[TMP1]] +; + store ptr addrspacecast (ptr addrspace(1) @g4 to ptr), ptr @g3, align 8 + %l1 = load ptr, ptr @g3, align 8 + %l2 = load i64, ptr %l1, align 8 + ret i64 %l2 +}