1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -S -unroll-runtime -unroll-count=2 -passes=loop-unroll -unroll-runtime-epilog=true | FileCheck %s -check-prefix=EPILOG
3 ; RUN: opt < %s -S -unroll-runtime -unroll-count=2 -passes=loop-unroll -unroll-runtime-epilog=false | FileCheck %s -check-prefix=PROLOG
4 target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
6 ; This test case documents how runtime loop unrolling handles the case
7 ; when the backedge-count is -1.
9 ; If %N, the backedge-taken count, is -1 then %0 unsigned-overflows
10 ; and is 0. %xtraiter too is 0, signifying that the total trip-count
11 ; is divisible by 2. The prologue then branches to the unrolled loop
12 ; and executes the 2^32 iterations there, in groups of 2.
14 define i32 @foo(i32 %N) {
17 ; EPILOG-NEXT: [[TMP0:%.*]] = add i32 [[N:%.*]], 1
18 ; EPILOG-NEXT: [[XTRAITER:%.*]] = and i32 [[TMP0]], 1
19 ; EPILOG-NEXT: [[TMP1:%.*]] = icmp ult i32 [[N]], 1
20 ; EPILOG-NEXT: br i1 [[TMP1]], label [[WHILE_END_UNR_LCSSA:%.*]], label [[ENTRY_NEW:%.*]]
22 ; EPILOG-NEXT: [[UNROLL_ITER:%.*]] = sub i32 [[TMP0]], [[XTRAITER]]
23 ; EPILOG-NEXT: br label [[WHILE_BODY:%.*]]
25 ; EPILOG-NEXT: [[I:%.*]] = phi i32 [ 0, [[ENTRY_NEW]] ], [ [[INC_1:%.*]], [[WHILE_BODY]] ]
26 ; EPILOG-NEXT: [[NITER:%.*]] = phi i32 [ 0, [[ENTRY_NEW]] ], [ [[NITER_NEXT_1:%.*]], [[WHILE_BODY]] ]
27 ; EPILOG-NEXT: [[INC:%.*]] = add nuw nsw i32 [[I]], 1
28 ; EPILOG-NEXT: [[INC_1]] = add i32 [[I]], 2
29 ; EPILOG-NEXT: [[NITER_NEXT_1]] = add i32 [[NITER]], 2
30 ; EPILOG-NEXT: [[NITER_NCMP_1:%.*]] = icmp eq i32 [[NITER_NEXT_1]], [[UNROLL_ITER]]
31 ; EPILOG-NEXT: br i1 [[NITER_NCMP_1]], label [[WHILE_END_UNR_LCSSA_LOOPEXIT:%.*]], label [[WHILE_BODY]], !llvm.loop [[LOOP0:![0-9]+]]
32 ; EPILOG: while.end.unr-lcssa.loopexit:
33 ; EPILOG-NEXT: [[I_LCSSA_PH_PH:%.*]] = phi i32 [ [[INC]], [[WHILE_BODY]] ]
34 ; EPILOG-NEXT: [[I_UNR_PH:%.*]] = phi i32 [ [[INC_1]], [[WHILE_BODY]] ]
35 ; EPILOG-NEXT: br label [[WHILE_END_UNR_LCSSA]]
36 ; EPILOG: while.end.unr-lcssa:
37 ; EPILOG-NEXT: [[I_LCSSA_PH:%.*]] = phi i32 [ undef, [[ENTRY:%.*]] ], [ [[I_LCSSA_PH_PH]], [[WHILE_END_UNR_LCSSA_LOOPEXIT]] ]
38 ; EPILOG-NEXT: [[I_UNR:%.*]] = phi i32 [ 0, [[ENTRY]] ], [ [[I_UNR_PH]], [[WHILE_END_UNR_LCSSA_LOOPEXIT]] ]
39 ; EPILOG-NEXT: [[LCMP_MOD:%.*]] = icmp ne i32 [[XTRAITER]], 0
40 ; EPILOG-NEXT: br i1 [[LCMP_MOD]], label [[WHILE_BODY_EPIL_PREHEADER:%.*]], label [[WHILE_END:%.*]]
41 ; EPILOG: while.body.epil.preheader:
42 ; EPILOG-NEXT: br label [[WHILE_BODY_EPIL:%.*]]
43 ; EPILOG: while.body.epil:
44 ; EPILOG-NEXT: br label [[WHILE_END]]
46 ; EPILOG-NEXT: [[I_LCSSA:%.*]] = phi i32 [ [[I_LCSSA_PH]], [[WHILE_END_UNR_LCSSA]] ], [ [[I_UNR]], [[WHILE_BODY_EPIL]] ]
47 ; EPILOG-NEXT: ret i32 [[I_LCSSA]]
51 ; PROLOG-NEXT: [[TMP0:%.*]] = add i32 [[N:%.*]], 1
52 ; PROLOG-NEXT: [[XTRAITER:%.*]] = and i32 [[TMP0]], 1
53 ; PROLOG-NEXT: [[LCMP_MOD:%.*]] = icmp ne i32 [[XTRAITER]], 0
54 ; PROLOG-NEXT: br i1 [[LCMP_MOD]], label [[WHILE_BODY_PROL_PREHEADER:%.*]], label [[WHILE_BODY_PROL_LOOPEXIT:%.*]]
55 ; PROLOG: while.body.prol.preheader:
56 ; PROLOG-NEXT: br label [[WHILE_BODY_PROL:%.*]]
57 ; PROLOG: while.body.prol:
58 ; PROLOG-NEXT: br label [[WHILE_BODY_PROL_LOOPEXIT]]
59 ; PROLOG: while.body.prol.loopexit:
60 ; PROLOG-NEXT: [[I_LCSSA_UNR:%.*]] = phi i32 [ undef, [[ENTRY:%.*]] ], [ 0, [[WHILE_BODY_PROL]] ]
61 ; PROLOG-NEXT: [[I_UNR:%.*]] = phi i32 [ 0, [[ENTRY]] ], [ 1, [[WHILE_BODY_PROL]] ]
62 ; PROLOG-NEXT: [[TMP1:%.*]] = icmp ult i32 [[N]], 1
63 ; PROLOG-NEXT: br i1 [[TMP1]], label [[WHILE_END:%.*]], label [[ENTRY_NEW:%.*]]
65 ; PROLOG-NEXT: br label [[WHILE_BODY:%.*]]
67 ; PROLOG-NEXT: [[I:%.*]] = phi i32 [ [[I_UNR]], [[ENTRY_NEW]] ], [ [[INC_1:%.*]], [[WHILE_BODY]] ]
68 ; PROLOG-NEXT: [[INC:%.*]] = add i32 [[I]], 1
69 ; PROLOG-NEXT: [[CMP_1:%.*]] = icmp eq i32 [[INC]], [[N]]
70 ; PROLOG-NEXT: [[INC_1]] = add i32 [[I]], 2
71 ; PROLOG-NEXT: br i1 [[CMP_1]], label [[WHILE_END_UNR_LCSSA:%.*]], label [[WHILE_BODY]], !llvm.loop [[LOOP0:![0-9]+]]
72 ; PROLOG: while.end.unr-lcssa:
73 ; PROLOG-NEXT: [[I_LCSSA_PH:%.*]] = phi i32 [ [[INC]], [[WHILE_BODY]] ]
74 ; PROLOG-NEXT: br label [[WHILE_END]]
76 ; PROLOG-NEXT: [[I_LCSSA:%.*]] = phi i32 [ [[I_LCSSA_UNR]], [[WHILE_BODY_PROL_LOOPEXIT]] ], [ [[I_LCSSA_PH]], [[WHILE_END_UNR_LCSSA]] ]
77 ; PROLOG-NEXT: ret i32 [[I_LCSSA]]
82 while.body: ; preds = %while.body, %entry
83 %i = phi i32 [ 0, %entry ], [ %inc, %while.body ]
84 %cmp = icmp eq i32 %i, %N
86 br i1 %cmp, label %while.end, label %while.body
88 while.end: ; preds = %while.body