[llvm] [CodeGenPrepare] Make sure that `AddOffset` is also a loop invariant (PR #150625)
Yingwei Zheng via llvm-commits
llvm-commits at lists.llvm.org
Fri Jul 25 07:44:08 PDT 2025
https://github.com/dtcxzyw updated https://github.com/llvm/llvm-project/pull/150625
>From 3ce67f13a8402047b2cc0e46832554089fcdc185 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Fri, 25 Jul 2025 21:59:26 +0800
Subject: [PATCH 1/2] [CodeGenPrepare] Make sure that AddOffset is also a loop
invariant
---
llvm/lib/CodeGen/CodeGenPrepare.cpp | 4 ++++
.../CodeGenPrepare/X86/fold-loop-of-urem.ll | 18 ++++++++++++++++++
2 files changed, 22 insertions(+)
diff --git a/llvm/lib/CodeGen/CodeGenPrepare.cpp b/llvm/lib/CodeGen/CodeGenPrepare.cpp
index c21058ca51344..416c56d5a36f8 100644
--- a/llvm/lib/CodeGen/CodeGenPrepare.cpp
+++ b/llvm/lib/CodeGen/CodeGenPrepare.cpp
@@ -2095,6 +2095,10 @@ static bool isRemOfLoopIncrementWithLoopInvariant(
if (!L->isLoopInvariant(RemAmt))
return false;
+ // Only works if the AddOffset is a loop invaraint
+ if (AddOffset && !L->isLoopInvariant(AddOffset))
+ return false;
+
// Is the PHI a loop increment?
auto LoopIncrInfo = getIVIncrement(PN, LI);
if (!LoopIncrInfo)
diff --git a/llvm/test/Transforms/CodeGenPrepare/X86/fold-loop-of-urem.ll b/llvm/test/Transforms/CodeGenPrepare/X86/fold-loop-of-urem.ll
index 7abc32e4f1cd8..a03ab6ea01dde 100644
--- a/llvm/test/Transforms/CodeGenPrepare/X86/fold-loop-of-urem.ll
+++ b/llvm/test/Transforms/CodeGenPrepare/X86/fold-loop-of-urem.ll
@@ -1065,3 +1065,21 @@ for.body:
%exitcond.not = icmp eq i32 %inc, %N
br i1 %exitcond.not, label %for.cond.cleanup, label %for.body
}
+
+define i64 @pr150611_add_offset_is_not_loop_invariant(i1 %cond) {
+entry:
+ %remamt = select i1 %cond, i64 2, i64 0
+ br label %for.body
+
+for.body:
+ %indvars = phi i64 [ 0, %entry ], [ %indvars.next, %for.body ]
+ %add.offset = zext i1 %cond to i64
+ %add = add nuw i64 %indvars, %add.offset
+ %rem = urem i64 %add, %remamt
+ %indvars.next = add nuw i64 %indvars, 1
+ %exitcond = icmp eq i64 %indvars.next, 3
+ br i1 %exitcond, label %for.exit, label %for.body
+
+for.exit:
+ ret i64 %rem
+}
>From b553fa6268c2c21d0b63e49fbce4556f72f1f5d9 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Fri, 25 Jul 2025 22:34:00 +0800
Subject: [PATCH 2/2] [CodeGenPrepare] Add check lines. NFC.
---
.../CodeGenPrepare/X86/fold-loop-of-urem.ll | 16 ++++++++++++++++
1 file changed, 16 insertions(+)
diff --git a/llvm/test/Transforms/CodeGenPrepare/X86/fold-loop-of-urem.ll b/llvm/test/Transforms/CodeGenPrepare/X86/fold-loop-of-urem.ll
index a03ab6ea01dde..f53127f015391 100644
--- a/llvm/test/Transforms/CodeGenPrepare/X86/fold-loop-of-urem.ll
+++ b/llvm/test/Transforms/CodeGenPrepare/X86/fold-loop-of-urem.ll
@@ -1067,6 +1067,22 @@ for.body:
}
define i64 @pr150611_add_offset_is_not_loop_invariant(i1 %cond) {
+; CHECK-LABEL: define i64 @pr150611_add_offset_is_not_loop_invariant(
+; CHECK-SAME: i1 [[COND:%.*]]) {
+; CHECK-NEXT: [[ENTRY:.*]]:
+; CHECK-NEXT: [[REMAMT:%.*]] = select i1 [[COND]], i64 2, i64 0
+; CHECK-NEXT: br label %[[FOR_BODY:.*]]
+; CHECK: [[FOR_BODY]]:
+; CHECK-NEXT: [[INDVARS:%.*]] = phi i64 [ 0, %[[ENTRY]] ], [ [[INDVARS_NEXT:%.*]], %[[FOR_BODY]] ]
+; CHECK-NEXT: [[ADD_OFFSET:%.*]] = zext i1 [[COND]] to i64
+; CHECK-NEXT: [[ADD:%.*]] = add nuw i64 [[INDVARS]], [[ADD_OFFSET]]
+; CHECK-NEXT: [[REM:%.*]] = urem i64 [[ADD]], [[REMAMT]]
+; CHECK-NEXT: [[INDVARS_NEXT]] = add nuw i64 [[INDVARS]], 1
+; CHECK-NEXT: [[EXITCOND:%.*]] = icmp eq i64 [[INDVARS_NEXT]], 3
+; CHECK-NEXT: br i1 [[EXITCOND]], label %[[FOR_EXIT:.*]], label %[[FOR_BODY]]
+; CHECK: [[FOR_EXIT]]:
+; CHECK-NEXT: ret i64 [[REM]]
+;
entry:
%remamt = select i1 %cond, i64 2, i64 0
br label %for.body
More information about the llvm-commits
mailing list