[PATCH] D99845: [GlobalOpt] Delete write-only constants with cast users

Johannes Doerfert via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Apr 3 13:07:20 PDT 2021


jdoerfert created this revision.
jdoerfert added reviewers: arsenm, fhahn, lebedev.ri.
Herald added a subscriber: hiraditya.
Herald added a reviewer: bollu.
jdoerfert requested review of this revision.
Herald added a subscriber: wdng.
Herald added a project: LLVM.

We handled constant bit/addrspace-casts and GEPs but no
bit/addrspace-cast instructions before.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D99845

Files:
  llvm/lib/Transforms/IPO/GlobalOpt.cpp
  llvm/test/Transforms/GlobalOpt/writeonly-internal-bitcast.ll


Index: llvm/test/Transforms/GlobalOpt/writeonly-internal-bitcast.ll
===================================================================
--- /dev/null
+++ llvm/test/Transforms/GlobalOpt/writeonly-internal-bitcast.ll
@@ -0,0 +1,28 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt -globalopt %s -S -o - | FileCheck %s
+
+%struct.S = type { i32, [2048 x i8] }
+
+ at DirectAccess = internal global %struct.S undef, align 4
+ at CastAccess = internal global %struct.S undef, align 4
+ at CastAccessConst = internal global %struct.S undef, align 4
+ at ASCastAccess = internal addrspace(3) global %struct.S undef, align 4
+
+define dso_local void @f(i32 %I32)  {
+; CHECK-LABEL: @f(
+; CHECK-NEXT:    ret void
+;
+  %g0 = getelementptr inbounds %struct.S, %struct.S* @DirectAccess, i32 0, i32 1, i32 %I32
+  store i8 0, i8* %g0, align 4
+  %g1 = getelementptr inbounds %struct.S, %struct.S* @CastAccess, i32 0, i32 1, i32 %I32
+  %bc1 = bitcast i8* %g1 to i32*
+  store i32 0, i32* %bc1, align 4
+  %g2 = getelementptr inbounds %struct.S, %struct.S* @CastAccessConst, i32 0, i32 1, i32 42
+  %bc2 = bitcast i8* %g2 to i32*
+  store i32 0, i32* %bc2, align 4
+  %as3 = addrspacecast %struct.S addrspace(3)* @ASCastAccess to %struct.S*
+  %g3 = getelementptr inbounds %struct.S, %struct.S* %as3, i32 0, i32 1, i32 %I32
+  %bc3 = bitcast i8* %g3 to i32*
+  store i32 0, i32* %bc3, align 4
+  ret void
+}
Index: llvm/lib/Transforms/IPO/GlobalOpt.cpp
===================================================================
--- llvm/lib/Transforms/IPO/GlobalOpt.cpp
+++ llvm/lib/Transforms/IPO/GlobalOpt.cpp
@@ -315,6 +315,7 @@
                   CE->getType()->isPointerTy()) ||
                  CE->getOpcode() == Instruction::AddrSpaceCast) {
         // Pointer cast, delete any stores and memsets to the global.
+        // TODO: We should try to cast the Init value to the new type.
         Changed |= CleanupConstantGlobalUsers(CE, nullptr, DL, GetTLI);
       }
 
@@ -322,6 +323,15 @@
         CE->destroyConstant();
         Changed = true;
       }
+    } else if (CastInst *CI = dyn_cast<CastInst>(U)) {
+      assert(CI->getType()->isPointerTy() && "Expected a pointer cast!");
+      // Pointer cast, delete any stores and memsets to the global.
+      // TODO: We should try to cast the Init value to the new type.
+      Changed |= CleanupConstantGlobalUsers(CI, nullptr, DL, GetTLI);
+      if (CI->use_empty()) {
+        CI->eraseFromParent();
+        Changed = true;
+      }
     } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(U)) {
       // Do not transform "gepinst (gep constexpr (GV))" here, because forming
       // "gepconstexpr (gep constexpr (GV))" will cause the two gep's to fold


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D99845.335102.patch
Type: text/x-patch
Size: 2745 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210403/cdcb3586/attachment.bin>


More information about the llvm-commits mailing list