[PATCH] D133486: [LICM] Consider sret as writable object

Nikita Popov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 8 06:00:38 PDT 2022


nikic created this revision.
nikic added reviewers: reames, fhahn, asbirlea, efriedma.
Herald added a subscriber: hiraditya.
Herald added a project: All.
nikic requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

LangRef explicitly guarantees that sret memory can be both read and written (which makes sense, given how the whole point of sret is that it will be written to):

> This pointer must be guaranteed by the caller to be valid: loads and stores to the structure may be assumed by the callee not to trap and to be properly aligned.

Together with the noalias attribute this makes store promotion on sret memory legal, even if there are no unconditional stores.


https://reviews.llvm.org/D133486

Files:
  llvm/lib/Transforms/Scalar/LICM.cpp
  llvm/test/Transforms/LICM/scalar-promote.ll


Index: llvm/test/Transforms/LICM/scalar-promote.ll
===================================================================
--- llvm/test/Transforms/LICM/scalar-promote.ll
+++ llvm/test/Transforms/LICM/scalar-promote.ll
@@ -883,7 +883,6 @@
   ret void
 }
 
-; TODO: The store can be promoted, as sret memory is writable.
 define void @sret_cond_store(i32* sret(i32) noalias %ptr) {
 ; CHECK-LABEL: @sret_cond_store(
 ; CHECK-NEXT:    [[PTR_PROMOTED:%.*]] = load i32, i32* [[PTR:%.*]], align 4
@@ -894,9 +893,10 @@
 ; CHECK-NEXT:    br i1 [[C]], label [[LOOP_LATCH]], label [[EXIT:%.*]]
 ; CHECK:       loop.latch:
 ; CHECK-NEXT:    [[V_INC]] = add i32 [[V_INC1]], 1
-; CHECK-NEXT:    store i32 [[V_INC]], i32* [[PTR]], align 4
 ; CHECK-NEXT:    br label [[LOOP]]
 ; CHECK:       exit:
+; CHECK-NEXT:    [[V_INC1_LCSSA:%.*]] = phi i32 [ [[V_INC1]], [[LOOP]] ]
+; CHECK-NEXT:    store i32 [[V_INC1_LCSSA]], i32* [[PTR]], align 4
 ; CHECK-NEXT:    ret void
 ;
   br label %loop
Index: llvm/lib/Transforms/Scalar/LICM.cpp
===================================================================
--- llvm/lib/Transforms/Scalar/LICM.cpp
+++ llvm/lib/Transforms/Scalar/LICM.cpp
@@ -1896,9 +1896,8 @@
   if (isa<AllocaInst>(Object))
     return true;
 
-  // TODO: Also handle sret.
   if (auto *A = dyn_cast<Argument>(Object))
-    return A->hasByValAttr();
+    return A->hasByValAttr() || A->hasStructRetAttr();
 
   // TODO: Noalias has nothing to do with writability, this should check for
   // an allocator function.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D133486.458712.patch
Type: text/x-patch
Size: 1506 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220908/a6d975de/attachment.bin>


More information about the llvm-commits mailing list