[llvm] [SCEVExpander] Relax hoisting condition for AddRec start (PR #75916)

via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 2 09:09:01 PST 2024


averine wrote:

@nikic here are the tests failing when I replace canExpand() by isSafeToExpand():
```
  LLVM :: Transforms/IndVarSimplify/X86/pr35406.ll
  LLVM :: Transforms/IndVarSimplify/pr30806-phi-scev.ll
  LLVM :: Transforms/IndVarSimplify/pr64891.ll
  LLVM :: Transforms/LoopVectorize/pr38697.ll
```
For these tests, diffs of the output of the "opt -passes=indvars" line are below.
Having a second look there is no diff in division hoisting.

```
--- IndVarSimplify/pr30806-phi-scev.ll.canExpand.opt	2024-01-02 17:55:43
+++ IndVarSimplify/pr30806-phi-scev.ll.isSafeToExpand.opt	2024-01-02 17:55:03
@@ -20 +19,0 @@
-  %indvars.iv = phi i64 [ %indvars.iv.next, %while.body ], [ 0, %while.body.lr.ph ]
@@ -22,5 +21,6 @@
-  %2 = sext i32 %div to i64
-  %indvars.iv.next = add i64 %indvars.iv, %2
-  %3 = load i32, ptr @theSize, align 4
-  store i32 %3, ptr %i, align 4
-  call void @bar(ptr nonnull %i, i64 %indvars.iv.next)
+  %inx.06 = phi i32 [ 0, %while.body.lr.ph ], [ %add, %while.body ]
+  %add = add nsw i32 %inx.06, %div
+  %2 = load i32, ptr @theSize, align 4
+  store i32 %2, ptr %i, align 4
+  %conv = sext i32 %add to i64
+  call void @bar(ptr nonnull %i, i64 %conv)
@@ -28,2 +28,2 @@
-  %4 = load i32, ptr %flag, align 4
-  %tobool = icmp eq i32 %4, 0
+  %3 = load i32, ptr %flag, align 4
+  %tobool = icmp eq i32 %3, 0
--- IndVarSimplify/X86/pr35406.ll.canExpand.opt	2024-01-02 17:22:33
+++ IndVarSimplify/X86/pr35406.ll.isSafeToExpand.opt	2024-01-02 17:23:57
@@ -11,2 +11,2 @@
-  %indvars.iv = phi i64 [ %indvars.iv.next, %loop2.exit ], [ 8, %entry ]
-  %exitcond = icmp eq i64 %indvars.iv, 15
+  %local_0_ = phi i32 [ 8, %entry ], [ %i9, %loop2.exit ]
+  %exitcond = icmp eq i32 %local_0_, 15
@@ -31 +31 @@
-  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
+  %i9 = add nuw nsw i32 %local_0_, 1
--- LoopVectorize/pr38697.ll.canExpand.opt	2024-01-02 17:22:33
+++ LoopVectorize/pr38697.ll.isSafeToExpand.opt	2024-01-02 17:23:57
@@ -111 +111 @@
-  %0 = zext i32 %start1 to i64
+  %umax = call i32 @llvm.umax.i32(i32 %start1, i32 100)
@@ -115 +114,0 @@
-  %indvars.iv = phi i64 [ %indvars.iv.next, %for.end ], [ %0, %entry ]
@@ -117,3 +116,3 @@
-  %indvars3 = trunc i64 %indvars.iv to i32
-  %cmp = icmp ult i32 %indvars3, 100
-  br i1 %cmp, label %for.body, label %for.end10
+  %counter1.0 = phi i32 [ %start1, %entry ], [ %inc9, %for.end ]
+  %exitcond1 = icmp ne i32 %counter1.0, %umax
+  br i1 %exitcond1, label %for.body, label %for.end10
@@ -131 +130 @@
-  %indvars.iv1 = phi i64 [ %indvars.iv.next2, %for.body3 ], [ 0, %for.body3.lr.ph ]
+  %index.027 = phi i32 [ 0, %for.body3.lr.ph ], [ %add4, %for.body3 ]
@@ -134,3 +133,4 @@
-  %1 = udiv i64 16, %indvars.iv
-  %indvars.iv.next2 = add nuw nsw i64 %indvars.iv1, %1
-  %arrayidx6 = getelementptr inbounds i32, ptr %ptr, i64 %indvars.iv.next2
+  %div = udiv i32 16, %counter1.0
+  %add4 = add nuw nsw i32 %div, %index.027
+  %idxprom5 = zext i32 %add4 to i64
+  %arrayidx6 = getelementptr inbounds i32, ptr %ptr, i64 %idxprom5
@@ -149 +149 @@
-  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
+  %inc9 = add i32 %counter1.0, 1
@@ -252,0 +253,3 @@
+; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
+declare i32 @llvm.umax.i32(i32, i32) #1
+
@@ -253,0 +257 @@
+attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
--- IndVarSimplify/pr64891.ll.canExpand.opt	2024-01-02 17:22:33
+++ IndVarSimplify/pr64891.ll.isSafeToExpand.opt	2024-01-02 17:23:57
@@ -6,0 +7,2 @@
+  %div = sdiv i32 1, 0
+  %trunc = trunc i32 %div to i16
@@ -9,0 +12,4 @@
+  %phi = phi i16 [ 0, %entry ], [ %or, %loop ]
+  %or = or disjoint i16 %phi, %trunc
+  %phi.ext = sext i16 %phi to i64
+  %add.ptr = getelementptr i8, ptr null, i64 %phi.ext
```

https://github.com/llvm/llvm-project/pull/75916


More information about the llvm-commits mailing list