[PATCH] D107397: [GlobalOpt] Fix the load types when OptimizeGlobalAddressOfMalloc

Shimin Cui via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 3 13:14:35 PDT 2021


scui created this revision.
scui added reviewers: efriedma, vsk, fhahn.
Herald added subscribers: ormris, hiraditya.
scui requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Currently, in OptimizeGlobalAddressOfMalloc, the transformation for global loads assumes that they have the same Type. With the support of ConstantExpr (https://reviews.llvm.org/D106589), this may not be true any more (as seen in the test case), and we miss the code to handle this, This is to fix that.

Hopefully, this is the problem that @saugustine reported in https://reviews.llvm.org/D106589. The test case I added here has the similar msg you have. Please verify. Thanks.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D107397

Files:
  llvm/lib/Transforms/IPO/GlobalOpt.cpp
  llvm/test/Transforms/GlobalOpt/2021-08-03-StoreOnceLoadMultiCasts.ll


Index: llvm/test/Transforms/GlobalOpt/2021-08-03-StoreOnceLoadMultiCasts.ll
===================================================================
--- /dev/null
+++ llvm/test/Transforms/GlobalOpt/2021-08-03-StoreOnceLoadMultiCasts.ll
@@ -0,0 +1,44 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt -globalopt -S < %s | FileCheck %s
+; RUN: opt -passes=globalopt -S < %s | FileCheck %s
+
+ at g = internal global i32* null, align 8
+
+define signext i32 @f() local_unnamed_addr {
+; CHECK-LABEL: @f(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    call void @f1()
+; CHECK-NEXT:    store i32 1, i32* @g.body, align 4
+; CHECK-NEXT:    call void @f1()
+; CHECK-NEXT:    store i8 2, i8* bitcast (i32* @g.body to i8*), align 4
+; CHECK-NEXT:    ret i32 1
+;
+entry:
+  %call = call i8* @malloc(i64 4)
+  %b = bitcast i8* %call to i32*
+  store i32* %b, i32** @g, align 8
+  call void @f1()
+  %0 = load i32*, i32** @g, align 8
+  store i32 1, i32* %0, align 4
+  call void @f1()
+  %1 = load i8*, i8** bitcast (i32** @g to i8**), align 8
+  store i8 2, i8* %1, align 4
+  ret i32 1
+}
+
+define signext i32 @main() {
+; CHECK-LABEL: @main(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    [[CALL:%.*]] = call signext i32 @f()
+; CHECK-NEXT:    [[TMP0:%.*]] = load i32, i32* @g.body, align 4
+; CHECK-NEXT:    ret i32 [[TMP0]]
+;
+entry:
+  %call = call signext i32 @f()
+  %0 = load i32*, i32** @g, align 8
+  %1 = load i32, i32* %0, align 4
+  ret i32 %1
+}
+
+declare noalias align 16 i8* @malloc(i64)
+declare void @f1()
Index: llvm/lib/Transforms/IPO/GlobalOpt.cpp
===================================================================
--- llvm/lib/Transforms/IPO/GlobalOpt.cpp
+++ llvm/lib/Transforms/IPO/GlobalOpt.cpp
@@ -997,11 +997,11 @@
     }
 
     LoadInst *LI = cast<LoadInst>(U);
-    while (!LI->use_empty()) {
-      Use &LoadUse = *LI->use_begin();
-      ICmpInst *ICI = dyn_cast<ICmpInst>(LoadUse.getUser());
+    for (Use &LU : llvm::make_early_inc_range(LI->uses())) {
+      ICmpInst *ICI = dyn_cast<ICmpInst>(LU.getUser());
       if (!ICI) {
-        LoadUse = RepValue;
+        // Simply replace the use by the NewGV.
+        LU.set(ConstantExpr::getBitCast(NewGV, LI->getType()));
         continue;
       }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D107397.363848.patch
Type: text/x-patch
Size: 2251 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210803/368f0fdb/attachment.bin>


More information about the llvm-commits mailing list