[llvm] [ConstraintElim] Add missing check to make sure the bound is loop-invariant (PR #70555)
Yingwei Zheng via llvm-commits
llvm-commits at lists.llvm.org
Thu Nov 2 06:27:46 PDT 2023
https://github.com/dtcxzyw updated https://github.com/llvm/llvm-project/pull/70555
>From 17f2d015bef0fb1a09757241678165c06c808801 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Thu, 2 Nov 2023 03:06:54 +0800
Subject: [PATCH 1/3] [ConstraintElim] Add pre-commit tests for PR70510. NFC.
---
.../ConstraintElimination/pr70510.ll | 92 +++++++++++++++++++
1 file changed, 92 insertions(+)
create mode 100644 llvm/test/Transforms/ConstraintElimination/pr70510.ll
diff --git a/llvm/test/Transforms/ConstraintElimination/pr70510.ll b/llvm/test/Transforms/ConstraintElimination/pr70510.ll
new file mode 100644
index 000000000000000..dac9cd136c7e41a
--- /dev/null
+++ b/llvm/test/Transforms/ConstraintElimination/pr70510.ll
@@ -0,0 +1,92 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 3
+; RUN: opt -passes=constraint-elimination -S %s | FileCheck %s
+
+define i32 @main() {
+; CHECK-LABEL: define i32 @main() {
+; CHECK-NEXT: entry:
+; CHECK-NEXT: br label [[FOR_HEADER:%.*]]
+; CHECK: for.header:
+; CHECK-NEXT: [[IND1:%.*]] = phi i64 [ [[IND1_I:%.*]], [[FOR_LATCH:%.*]] ], [ 0, [[ENTRY:%.*]] ]
+; CHECK-NEXT: [[IND2:%.*]] = phi i64 [ [[IND2_I:%.*]], [[FOR_LATCH]] ], [ 5, [[ENTRY]] ]
+; CHECK-NEXT: [[REM:%.*]] = srem i64 [[IND2]], 4
+; CHECK-NEXT: [[CMP6:%.*]] = icmp eq i64 [[IND1]], [[REM]]
+; CHECK-NEXT: br i1 [[CMP6]], label [[EXIT1:%.*]], label [[FOR_LATCH]]
+; CHECK: for.latch:
+; CHECK-NEXT: [[IND2_I]] = add i64 [[IND2]], 1
+; CHECK-NEXT: [[IND1_I]] = add i64 [[IND1]], 1
+; CHECK-NEXT: [[COND:%.*]] = icmp eq i64 [[IND1_I]], 8
+; CHECK-NEXT: br i1 [[COND]], label [[EXIT2:%.*]], label [[FOR_HEADER]]
+; CHECK: exit2:
+; CHECK-NEXT: [[RET:%.*]] = zext i1 false to i32
+; CHECK-NEXT: ret i32 [[RET]]
+; CHECK: exit1:
+; CHECK-NEXT: ret i32 0
+;
+entry:
+ br label %for.header
+
+for.header:
+ %ind1 = phi i64 [ %ind1.i, %for.latch ], [ 0, %entry ]
+ %ind2 = phi i64 [ %ind2.i, %for.latch ], [ 5, %entry ]
+ %rem = srem i64 %ind2, 4
+ %cmp6 = icmp eq i64 %ind1, %rem
+ br i1 %cmp6, label %exit1, label %for.latch
+
+for.latch:
+ %ind2.i = add i64 %ind2, 1
+ %ind1.i = add i64 %ind1, 1
+ %cond = icmp eq i64 %ind1.i, 8
+ br i1 %cond, label %exit2, label %for.header
+
+exit2:
+ %cmp = icmp eq i64 %rem, 0
+ %ret = zext i1 %cmp to i32
+ ret i32 %ret
+
+exit1:
+ ret i32 0
+}
+
+declare i64 @callee()
+
+define i32 @test2() {
+; CHECK-LABEL: define i32 @test2() {
+; CHECK-NEXT: entry:
+; CHECK-NEXT: br label [[FOR_HEADER:%.*]]
+; CHECK: for.header:
+; CHECK-NEXT: [[IND1:%.*]] = phi i64 [ [[IND1_I:%.*]], [[FOR_LATCH:%.*]] ], [ 0, [[ENTRY:%.*]] ]
+; CHECK-NEXT: [[VAL:%.*]] = call i64 @callee()
+; CHECK-NEXT: [[CMP6:%.*]] = icmp eq i64 [[IND1]], [[VAL]]
+; CHECK-NEXT: br i1 [[CMP6]], label [[EXIT1:%.*]], label [[FOR_LATCH]]
+; CHECK: for.latch:
+; CHECK-NEXT: [[IND1_I]] = add i64 [[IND1]], 1
+; CHECK-NEXT: [[COND:%.*]] = icmp eq i64 [[IND1_I]], 8
+; CHECK-NEXT: br i1 [[COND]], label [[EXIT2:%.*]], label [[FOR_HEADER]]
+; CHECK: exit2:
+; CHECK-NEXT: [[RET:%.*]] = zext i1 false to i32
+; CHECK-NEXT: ret i32 [[RET]]
+; CHECK: exit1:
+; CHECK-NEXT: ret i32 0
+;
+entry:
+ br label %for.header
+
+for.header:
+ %ind1 = phi i64 [ %ind1.i, %for.latch ], [ 0, %entry ]
+ %val = call i64 @callee()
+ %cmp6 = icmp eq i64 %ind1, %val
+ br i1 %cmp6, label %exit1, label %for.latch
+
+for.latch:
+ %ind1.i = add i64 %ind1, 1
+ %cond = icmp eq i64 %ind1.i, 8
+ br i1 %cond, label %exit2, label %for.header
+
+exit2:
+ %cmp = icmp eq i64 %val, 0
+ %ret = zext i1 %cmp to i32
+ ret i32 %ret
+
+exit1:
+ ret i32 0
+}
>From 5ad186f8c42cb4aee78218e254fcf16959f7092f Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Thu, 2 Nov 2023 03:08:28 +0800
Subject: [PATCH 2/3] [ConstraintElim] Add missing check to make sure the bound
is loop-invariant
---
llvm/lib/Transforms/Scalar/ConstraintElimination.cpp | 4 ++++
llvm/test/Transforms/ConstraintElimination/pr70510.ll | 6 ++++--
2 files changed, 8 insertions(+), 2 deletions(-)
diff --git a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
index 37f720ec40f4e54..2d829ee017be6e4 100644
--- a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
+++ b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
@@ -899,6 +899,10 @@ void State::addInfoForInductions(BasicBlock &BB) {
else
return;
+ // Make sure the bound B is loop-invariant.
+ if (!L->isLoopInvariant(B))
+ return;
+
// Handle negative steps.
if (StepOffset.isNegative()) {
// TODO: Extend to allow steps > -1.
diff --git a/llvm/test/Transforms/ConstraintElimination/pr70510.ll b/llvm/test/Transforms/ConstraintElimination/pr70510.ll
index dac9cd136c7e41a..84cefdde17bfb39 100644
--- a/llvm/test/Transforms/ConstraintElimination/pr70510.ll
+++ b/llvm/test/Transforms/ConstraintElimination/pr70510.ll
@@ -17,7 +17,8 @@ define i32 @main() {
; CHECK-NEXT: [[COND:%.*]] = icmp eq i64 [[IND1_I]], 8
; CHECK-NEXT: br i1 [[COND]], label [[EXIT2:%.*]], label [[FOR_HEADER]]
; CHECK: exit2:
-; CHECK-NEXT: [[RET:%.*]] = zext i1 false to i32
+; CHECK-NEXT: [[CMP:%.*]] = icmp eq i64 [[REM]], 0
+; CHECK-NEXT: [[RET:%.*]] = zext i1 [[CMP]] to i32
; CHECK-NEXT: ret i32 [[RET]]
; CHECK: exit1:
; CHECK-NEXT: ret i32 0
@@ -63,7 +64,8 @@ define i32 @test2() {
; CHECK-NEXT: [[COND:%.*]] = icmp eq i64 [[IND1_I]], 8
; CHECK-NEXT: br i1 [[COND]], label [[EXIT2:%.*]], label [[FOR_HEADER]]
; CHECK: exit2:
-; CHECK-NEXT: [[RET:%.*]] = zext i1 false to i32
+; CHECK-NEXT: [[CMP:%.*]] = icmp eq i64 [[VAL]], 0
+; CHECK-NEXT: [[RET:%.*]] = zext i1 [[CMP]] to i32
; CHECK-NEXT: ret i32 [[RET]]
; CHECK: exit1:
; CHECK-NEXT: ret i32 0
>From d9355f0894357c7c1398d7fb5cd1900e795ffdcc Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Thu, 2 Nov 2023 21:27:08 +0800
Subject: [PATCH 3/3] fixup! [ConstraintElim] Add missing check to make sure
the bound is loop-invariant
---
llvm/test/Transforms/ConstraintElimination/pr70510.ll | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/llvm/test/Transforms/ConstraintElimination/pr70510.ll b/llvm/test/Transforms/ConstraintElimination/pr70510.ll
index 84cefdde17bfb39..32427d7a0474cdd 100644
--- a/llvm/test/Transforms/ConstraintElimination/pr70510.ll
+++ b/llvm/test/Transforms/ConstraintElimination/pr70510.ll
@@ -1,8 +1,8 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 3
; RUN: opt -passes=constraint-elimination -S %s | FileCheck %s
-define i32 @main() {
-; CHECK-LABEL: define i32 @main() {
+define i32 @test_loop_varying_bound_add_rec() {
+; CHECK-LABEL: define i32 @test_loop_varying_bound_add_rec() {
; CHECK-NEXT: entry:
; CHECK-NEXT: br label [[FOR_HEADER:%.*]]
; CHECK: for.header:
@@ -50,8 +50,8 @@ exit1:
declare i64 @callee()
-define i32 @test2() {
-; CHECK-LABEL: define i32 @test2() {
+define i32 @test_loop_varying_bound_call() {
+; CHECK-LABEL: define i32 @test_loop_varying_bound_call() {
; CHECK-NEXT: entry:
; CHECK-NEXT: br label [[FOR_HEADER:%.*]]
; CHECK: for.header:
More information about the llvm-commits
mailing list