[llvm] [NFC][Analysis] Add more SCEV tests for ptr inductions (PR #108210)
Eli Friedman via llvm-commits
llvm-commits at lists.llvm.org
Thu Sep 12 17:16:53 PDT 2024
================
@@ -1595,6 +1595,145 @@ exit:
ret i32 0
}
+define void @ptr_induction_eq_1(ptr %a, ptr %b) {
+; CHECK-LABEL: 'ptr_induction_eq_1'
+; CHECK-NEXT: Classifying expressions for: @ptr_induction_eq_1
+; CHECK-NEXT: %ptr.iv = phi ptr [ %ptr.iv.next, %loop ], [ %a, %entry ]
+; CHECK-NEXT: --> {%a,+,8}<nuw><%loop> U: full-set S: full-set Exits: ((8 * ((-8 + (-1 * (ptrtoint ptr %a to i64)) + (ptrtoint ptr %b to i64)) /u 8))<nuw> + %a) LoopDispositions: { %loop: Computable }
+; CHECK-NEXT: %ptr.iv.next = getelementptr inbounds i8, ptr %ptr.iv, i64 8
+; CHECK-NEXT: --> {(8 + %a),+,8}<nuw><%loop> U: full-set S: full-set Exits: (8 + (8 * ((-8 + (-1 * (ptrtoint ptr %a to i64)) + (ptrtoint ptr %b to i64)) /u 8))<nuw> + %a) LoopDispositions: { %loop: Computable }
+; CHECK-NEXT: Determining loop execution counts for: @ptr_induction_eq_1
+; CHECK-NEXT: Loop %loop: backedge-taken count is ((-8 + (-1 * (ptrtoint ptr %a to i64)) + (ptrtoint ptr %b to i64)) /u 8)
+; CHECK-NEXT: Loop %loop: constant max backedge-taken count is i64 2305843009213693951
+; CHECK-NEXT: Loop %loop: symbolic max backedge-taken count is ((-8 + (-1 * (ptrtoint ptr %a to i64)) + (ptrtoint ptr %b to i64)) /u 8)
+; CHECK-NEXT: Loop %loop: Trip multiple is 1
+;
+entry:
+ %cmp = icmp eq ptr %a, %b
+ br i1 %cmp, label %exit, label %loop
+
+loop:
+ %ptr.iv = phi ptr [ %ptr.iv.next, %loop ], [ %a, %entry ]
+ %ptr.iv.next = getelementptr inbounds i8, ptr %ptr.iv, i64 8
+ %exitcond = icmp eq ptr %ptr.iv.next, %b
+ br i1 %exitcond, label %exit, label %loop
+
+exit:
+ ret void
+}
+
+define void @ptr_induction_eq_2(ptr %a, i64 %n) {
+; CHECK-LABEL: 'ptr_induction_eq_2'
+; CHECK-NEXT: Classifying expressions for: @ptr_induction_eq_2
+; CHECK-NEXT: %b = getelementptr inbounds ptr, ptr %a, i64 %n
+; CHECK-NEXT: --> ((8 * %n)<nsw> + %a) U: full-set S: full-set
+; CHECK-NEXT: %ptr.iv = phi ptr [ %ptr.iv.next, %loop ], [ %a, %entry ]
+; CHECK-NEXT: --> {%a,+,8}<nuw><%loop> U: full-set S: full-set Exits: ((8 * ((-8 + (8 * %n)<nsw>) /u 8))<nuw> + %a) LoopDispositions: { %loop: Computable }
+; CHECK-NEXT: %ptr.iv.next = getelementptr inbounds i8, ptr %ptr.iv, i64 8
+; CHECK-NEXT: --> {(8 + %a),+,8}<nuw><%loop> U: full-set S: full-set Exits: (8 + (8 * ((-8 + (8 * %n)<nsw>) /u 8))<nuw> + %a) LoopDispositions: { %loop: Computable }
+; CHECK-NEXT: Determining loop execution counts for: @ptr_induction_eq_2
+; CHECK-NEXT: Loop %loop: backedge-taken count is ((-8 + (8 * %n)<nsw>) /u 8)
+; CHECK-NEXT: Loop %loop: constant max backedge-taken count is i64 2305843009213693951
+; CHECK-NEXT: Loop %loop: symbolic max backedge-taken count is ((-8 + (8 * %n)<nsw>) /u 8)
+; CHECK-NEXT: Loop %loop: Trip multiple is 1
+;
+entry:
+ %b = getelementptr inbounds ptr, ptr %a, i64 %n
+ %cmp = icmp eq ptr %a, %b
+ br i1 %cmp, label %exit, label %loop
+
+loop:
+ %ptr.iv = phi ptr [ %ptr.iv.next, %loop ], [ %a, %entry ]
+ %ptr.iv.next = getelementptr inbounds i8, ptr %ptr.iv, i64 8
+ %exitcond = icmp eq ptr %ptr.iv.next, %b
+ br i1 %exitcond, label %exit, label %loop
+
+exit:
+ ret void
+}
+
+; TODO: It feels like we should be able to calculate the symbolic max
+; exit count for the loop.inc block here, in the same way as
+; ptr_induction_eq_1. The problem seems to be in howFarToZero when the
+; ControlsOnlyExit is set to false.
----------------
efriedma-quic wrote:
For ptr_induction_eq_1, we prove that %b-%a is divisible by 8, so the trip count is (b-a)/8. (If difference isn't divisible by 8, we eventually branch on poison.) Here, we can't prove that: as long as there's another exit, the loop can go on indefinitely (at least, until we read past the end of the allocation). So we can't do any useful math.
If you tell the compiler the pointers are aligned (for example, using an "align" attribute), we do the computation.
Alternatively, we could compute a count assuming the loop exits via a particular branch; see https://github.com/llvm/llvm-project/pull/83052#pullrequestreview-2013190120 .
https://github.com/llvm/llvm-project/pull/108210
More information about the llvm-commits
mailing list