Skip to content

[ARM][LSR] Exclude uses outside the loop when favoring postinc. #67090

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 25, 2023

Conversation

davemgreen
Copy link
Collaborator

Extra uses for variables outside the loop can mess with the generation of postinc variables. This patch alters the collection of loop invariant fixups in LSR when the target is optimizing for PostInc, to exclude the collection of these extra uses. It is expected that the variable can be rematerialized, which will lead to a more optimal sequence of instructions in the loop.

Extra uses for variables outside the loop can mess with the generation of
postinc variables. This patch alters the collection of loop invariant fixups in
LSR when the target is optimizing for PostInc, to exclude the collection of
these extra uses. It is expected that the variable can be rematerialized, which
will lead to a more optimal sequence of instructions in the loop.
@llvmbot
Copy link
Member

llvmbot commented Sep 22, 2023

@llvm/pr-subscribers-llvm-transforms

Changes

Extra uses for variables outside the loop can mess with the generation of postinc variables. This patch alters the collection of loop invariant fixups in LSR when the target is optimizing for PostInc, to exclude the collection of these extra uses. It is expected that the variable can be rematerialized, which will lead to a more optimal sequence of instructions in the loop.


Full diff: https://github.com/llvm/llvm-project/pull/67090.diff

2 Files Affected:

  • (modified) llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp (+5)
  • (modified) llvm/test/CodeGen/Thumb2/mve-useafterloop.ll (+9-14)
diff --git a/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp b/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
index 3cf188ddb70c7a6..5070b8da8e77d35 100644
--- a/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
+++ b/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
@@ -3482,6 +3482,11 @@ LSRInstance::CollectLoopInvariantFixupsAndFormulae() {
   SmallVector<const SCEV *, 8> Worklist(RegUses.begin(), RegUses.end());
   SmallPtrSet<const SCEV *, 32> Visited;
 
+  // Don't collect outside uses if we are favoring postinc - the instructions in
+  // the loop are more important than the ones outside of it.
+  if (AMK == TTI::AMK_PostIndexed)
+    return;
+
   while (!Worklist.empty()) {
     const SCEV *S = Worklist.pop_back_val();
 
diff --git a/llvm/test/CodeGen/Thumb2/mve-useafterloop.ll b/llvm/test/CodeGen/Thumb2/mve-useafterloop.ll
index 5f2d356a6d11951..17daa7d54e391aa 100644
--- a/llvm/test/CodeGen/Thumb2/mve-useafterloop.ll
+++ b/llvm/test/CodeGen/Thumb2/mve-useafterloop.ll
@@ -7,19 +7,16 @@ define nonnull ptr @useafterloop(ptr nocapture noundef readonly %pSrcA, ptr noca
 ; CHECK-NEXT:    .save {r7, lr}
 ; CHECK-NEXT:    push {r7, lr}
 ; CHECK-NEXT:    mov.w lr, #64
-; CHECK-NEXT:    mov r12, r2
-; CHECK-NEXT:    movs r3, #0
+; CHECK-NEXT:    mov r3, r2
 ; CHECK-NEXT:  .LBB0_1: @ %while.body
 ; CHECK-NEXT:    @ =>This Inner Loop Header: Depth=1
 ; CHECK-NEXT:    vldrw.u32 q0, [r0], #16
 ; CHECK-NEXT:    vldrw.u32 q1, [r1], #16
-; CHECK-NEXT:    add.w r2, r12, r3
-; CHECK-NEXT:    adds r3, #16
 ; CHECK-NEXT:    vadd.f32 q0, q1, q0
-; CHECK-NEXT:    vstrw.32 q0, [r2]
+; CHECK-NEXT:    vstrb.8 q0, [r3], #16
 ; CHECK-NEXT:    le lr, .LBB0_1
 ; CHECK-NEXT:  @ %bb.2: @ %while.end
-; CHECK-NEXT:    mov r0, r12
+; CHECK-NEXT:    mov r0, r2
 ; CHECK-NEXT:    pop {r7, pc}
 entry:
   br label %while.body
@@ -92,17 +89,15 @@ define nofpclass(nan inf) float @manyusesafterloop(ptr nocapture noundef readonl
 ; CHECK-NEXT:    .save {r4, lr}
 ; CHECK-NEXT:    push {r4, lr}
 ; CHECK-NEXT:    mov.w lr, #64
-; CHECK-NEXT:    movs r3, #0
+; CHECK-NEXT:    mov r12, r0
+; CHECK-NEXT:    mov r3, r1
+; CHECK-NEXT:    mov r4, r2
 ; CHECK-NEXT:  .LBB2_1: @ %while.body
 ; CHECK-NEXT:    @ =>This Inner Loop Header: Depth=1
-; CHECK-NEXT:    add.w r12, r0, r3
-; CHECK-NEXT:    adds r4, r1, r3
-; CHECK-NEXT:    vldrw.u32 q1, [r4]
-; CHECK-NEXT:    vldrw.u32 q0, [r12]
-; CHECK-NEXT:    adds r4, r2, r3
-; CHECK-NEXT:    adds r3, #16
+; CHECK-NEXT:    vldrw.u32 q0, [r12], #16
+; CHECK-NEXT:    vldrw.u32 q1, [r3], #16
 ; CHECK-NEXT:    vadd.f32 q0, q1, q0
-; CHECK-NEXT:    vstrw.32 q0, [r4]
+; CHECK-NEXT:    vstrb.8 q0, [r4], #16
 ; CHECK-NEXT:    le lr, .LBB2_1
 ; CHECK-NEXT:  @ %bb.2: @ %while.end
 ; CHECK-NEXT:    vldr s0, [r2]

@davemgreen davemgreen merged commit 54e5de0 into llvm:main Sep 25, 2023
@davemgreen davemgreen deleted the gh-mve-lsrpostincuses branch September 25, 2023 09:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants