1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py 2; RUN: opt -loop-unroll -S < %s | FileCheck %s 3 4declare void @bar() 5 6define void @test1() { 7; CHECK-LABEL: @test1( 8; CHECK-NEXT: entry: 9; CHECK-NEXT: br label [[LOOP:%.*]] 10; CHECK: loop: 11; CHECK-NEXT: call void @bar() 12; CHECK-NEXT: br label [[LATCH:%.*]] 13; CHECK: latch: 14; CHECK-NEXT: call void @bar() 15; CHECK-NEXT: call void @bar() 16; CHECK-NEXT: br label [[LATCH_1:%.*]] 17; CHECK: exit: 18; CHECK-NEXT: ret void 19; CHECK: latch.1: 20; CHECK-NEXT: call void @bar() 21; CHECK-NEXT: call void @bar() 22; CHECK-NEXT: br label [[LATCH_2:%.*]] 23; CHECK: latch.2: 24; CHECK-NEXT: call void @bar() 25; CHECK-NEXT: call void @bar() 26; CHECK-NEXT: br label [[LATCH_3:%.*]] 27; CHECK: latch.3: 28; CHECK-NEXT: call void @bar() 29; CHECK-NEXT: call void @bar() 30; CHECK-NEXT: br label [[LATCH_4:%.*]] 31; CHECK: latch.4: 32; CHECK-NEXT: call void @bar() 33; CHECK-NEXT: call void @bar() 34; CHECK-NEXT: br label [[LATCH_5:%.*]] 35; CHECK: latch.5: 36; CHECK-NEXT: call void @bar() 37; CHECK-NEXT: call void @bar() 38; CHECK-NEXT: br label [[LATCH_6:%.*]] 39; CHECK: latch.6: 40; CHECK-NEXT: call void @bar() 41; CHECK-NEXT: call void @bar() 42; CHECK-NEXT: br label [[LATCH_7:%.*]] 43; CHECK: latch.7: 44; CHECK-NEXT: call void @bar() 45; CHECK-NEXT: call void @bar() 46; CHECK-NEXT: br label [[LATCH_8:%.*]] 47; CHECK: latch.8: 48; CHECK-NEXT: call void @bar() 49; CHECK-NEXT: call void @bar() 50; CHECK-NEXT: br label [[LATCH_9:%.*]] 51; CHECK: latch.9: 52; CHECK-NEXT: call void @bar() 53; CHECK-NEXT: call void @bar() 54; CHECK-NEXT: br i1 false, label [[LATCH_10:%.*]], label [[EXIT:%.*]] 55; CHECK: latch.10: 56; CHECK-NEXT: call void @bar() 57; CHECK-NEXT: br label [[EXIT]] 58; 59entry: 60 br label %loop 61loop: 62 %iv = phi i64 [0, %entry], [%iv.next, %latch] 63 %iv.next = add i64 %iv, 1 64 call void @bar() 65 %cmp1 = icmp ult i64 %iv, 10 66 br i1 %cmp1, label %latch, label %exit 67latch: 68 call void @bar() 69 %cmp2 = icmp ult i64 %iv, 20 70 br i1 %cmp2, label %loop, label %exit 71exit: 72 ret void 73} 74 75; Fully unroll this loop by 10, but leave the unrolled latch 76; tests since we don't know if %N < 10, and break the backedge. 77define void @test2(i64 %N) { 78; CHECK-LABEL: @test2( 79; CHECK-NEXT: entry: 80; CHECK-NEXT: br label [[LOOP:%.*]] 81; CHECK: loop: 82; CHECK-NEXT: call void @bar() 83; CHECK-NEXT: br label [[LATCH:%.*]] 84; CHECK: latch: 85; CHECK-NEXT: call void @bar() 86; CHECK-NEXT: br i1 true, label [[LOOP_1:%.*]], label [[EXIT:%.*]] 87; CHECK: exit: 88; CHECK-NEXT: ret void 89; CHECK: loop.1: 90; CHECK-NEXT: call void @bar() 91; CHECK-NEXT: br label [[LATCH_1:%.*]] 92; CHECK: latch.1: 93; CHECK-NEXT: call void @bar() 94; CHECK-NEXT: [[CMP2_1:%.*]] = icmp ule i64 1, [[N:%.*]] 95; CHECK-NEXT: br i1 [[CMP2_1]], label [[LOOP_2:%.*]], label [[EXIT]] 96; CHECK: loop.2: 97; CHECK-NEXT: call void @bar() 98; CHECK-NEXT: br label [[LATCH_2:%.*]] 99; CHECK: latch.2: 100; CHECK-NEXT: call void @bar() 101; CHECK-NEXT: [[CMP2_2:%.*]] = icmp ule i64 2, [[N]] 102; CHECK-NEXT: br i1 [[CMP2_2]], label [[LOOP_3:%.*]], label [[EXIT]] 103; CHECK: loop.3: 104; CHECK-NEXT: call void @bar() 105; CHECK-NEXT: br label [[LATCH_3:%.*]] 106; CHECK: latch.3: 107; CHECK-NEXT: call void @bar() 108; CHECK-NEXT: [[CMP2_3:%.*]] = icmp ule i64 3, [[N]] 109; CHECK-NEXT: br i1 [[CMP2_3]], label [[LOOP_4:%.*]], label [[EXIT]] 110; CHECK: loop.4: 111; CHECK-NEXT: call void @bar() 112; CHECK-NEXT: br label [[LATCH_4:%.*]] 113; CHECK: latch.4: 114; CHECK-NEXT: call void @bar() 115; CHECK-NEXT: [[CMP2_4:%.*]] = icmp ule i64 4, [[N]] 116; CHECK-NEXT: br i1 [[CMP2_4]], label [[LOOP_5:%.*]], label [[EXIT]] 117; CHECK: loop.5: 118; CHECK-NEXT: call void @bar() 119; CHECK-NEXT: br label [[LATCH_5:%.*]] 120; CHECK: latch.5: 121; CHECK-NEXT: call void @bar() 122; CHECK-NEXT: [[CMP2_5:%.*]] = icmp ule i64 5, [[N]] 123; CHECK-NEXT: br i1 [[CMP2_5]], label [[LOOP_6:%.*]], label [[EXIT]] 124; CHECK: loop.6: 125; CHECK-NEXT: call void @bar() 126; CHECK-NEXT: br label [[LATCH_6:%.*]] 127; CHECK: latch.6: 128; CHECK-NEXT: call void @bar() 129; CHECK-NEXT: [[CMP2_6:%.*]] = icmp ule i64 6, [[N]] 130; CHECK-NEXT: br i1 [[CMP2_6]], label [[LOOP_7:%.*]], label [[EXIT]] 131; CHECK: loop.7: 132; CHECK-NEXT: call void @bar() 133; CHECK-NEXT: br label [[LATCH_7:%.*]] 134; CHECK: latch.7: 135; CHECK-NEXT: call void @bar() 136; CHECK-NEXT: [[CMP2_7:%.*]] = icmp ule i64 7, [[N]] 137; CHECK-NEXT: br i1 [[CMP2_7]], label [[LOOP_8:%.*]], label [[EXIT]] 138; CHECK: loop.8: 139; CHECK-NEXT: call void @bar() 140; CHECK-NEXT: br label [[LATCH_8:%.*]] 141; CHECK: latch.8: 142; CHECK-NEXT: call void @bar() 143; CHECK-NEXT: [[CMP2_8:%.*]] = icmp ule i64 8, [[N]] 144; CHECK-NEXT: br i1 [[CMP2_8]], label [[LOOP_9:%.*]], label [[EXIT]] 145; CHECK: loop.9: 146; CHECK-NEXT: call void @bar() 147; CHECK-NEXT: br label [[LATCH_9:%.*]] 148; CHECK: latch.9: 149; CHECK-NEXT: call void @bar() 150; CHECK-NEXT: [[CMP2_9:%.*]] = icmp ule i64 9, [[N]] 151; CHECK-NEXT: br i1 [[CMP2_9]], label [[LOOP_10:%.*]], label [[EXIT]] 152; CHECK: loop.10: 153; CHECK-NEXT: call void @bar() 154; CHECK-NEXT: br label [[LATCH_10:%.*]] 155; CHECK: latch.10: 156; CHECK-NEXT: call void @bar() 157; CHECK-NEXT: [[CMP2_10:%.*]] = icmp ule i64 10, [[N]] 158; CHECK-NEXT: br i1 [[CMP2_10]], label [[LOOP_11:%.*]], label [[EXIT]] 159; CHECK: loop.11: 160; CHECK-NEXT: call void @bar() 161; CHECK-NEXT: br i1 false, label [[LATCH_11:%.*]], label [[EXIT]] 162; CHECK: latch.11: 163; CHECK-NEXT: call void @bar() 164; CHECK-NEXT: br label [[EXIT]] 165; 166entry: 167 br label %loop 168loop: 169 %iv = phi i64 [0, %entry], [%iv.next, %latch] 170 %iv.next = add i64 %iv, 1 171 call void @bar() 172 %cmp1 = icmp ule i64 %iv, 10 173 br i1 %cmp1, label %latch, label %exit 174latch: 175 call void @bar() 176 %cmp2 = icmp ule i64 %iv, %N 177 br i1 %cmp2, label %loop, label %exit 178exit: 179 ret void 180} 181 182 183; TODO: We could partially unroll this by 2. 184define void @test3(i64 %N, i64 %M) { 185; CHECK-LABEL: @test3( 186; CHECK-NEXT: entry: 187; CHECK-NEXT: [[N_MASKED:%.*]] = and i64 [[N:%.*]], 65520 188; CHECK-NEXT: [[M_MASKED:%.*]] = and i64 [[M:%.*]], 65520 189; CHECK-NEXT: br label [[LOOP:%.*]] 190; CHECK: loop: 191; CHECK-NEXT: [[IV:%.*]] = phi i64 [ 0, [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[LATCH:%.*]] ] 192; CHECK-NEXT: [[IV_NEXT]] = add i64 [[IV]], 1 193; CHECK-NEXT: call void @bar() 194; CHECK-NEXT: [[CMP1:%.*]] = icmp ule i64 [[IV]], [[N_MASKED]] 195; CHECK-NEXT: br i1 [[CMP1]], label [[LATCH]], label [[EXIT:%.*]] 196; CHECK: latch: 197; CHECK-NEXT: call void @bar() 198; CHECK-NEXT: [[CMP2:%.*]] = icmp ule i64 [[IV]], [[M_MASKED]] 199; CHECK-NEXT: br i1 [[CMP2]], label [[LOOP]], label [[EXIT]] 200; CHECK: exit: 201; CHECK-NEXT: ret void 202; 203entry: 204 %N.masked = and i64 %N, 65520 ; 0xfff0 205 %M.masked = and i64 %M, 65520 ; 0xfff0 206 br label %loop 207loop: 208 %iv = phi i64 [0, %entry], [%iv.next, %latch] 209 %iv.next = add i64 %iv, 1 210 call void @bar() 211 %cmp1 = icmp ule i64 %iv, %N.masked 212 br i1 %cmp1, label %latch, label %exit 213latch: 214 call void @bar() 215 %cmp2 = icmp ule i64 %iv, %M.masked 216 br i1 %cmp2, label %loop, label %exit 217exit: 218 ret void 219} 220