1; RUN: opt < %s -S -passes=loop-unroll -unroll-runtime=true -unroll-runtime-epilog=true | FileCheck %s -check-prefixes=EPILOG,COMMON 2; RUN: opt < %s -S -passes=loop-unroll -unroll-runtime=true -unroll-runtime-epilog=false | FileCheck %s -check-prefixes=PROLOG,COMMON 3; 4; RUN: opt < %s -S -passes='require<opt-remark-emit>,loop-unroll' -unroll-runtime=true -unroll-runtime-epilog=true | FileCheck %s -check-prefixes=EPILOG,COMMON 5; RUN: opt < %s -S -passes='require<opt-remark-emit>,loop-unroll' -unroll-runtime=true -unroll-runtime-epilog=false | FileCheck %s -check-prefixes=PROLOG,COMMON 6; 7; Restricted versions of unroll (unroll<peeling;noruntime>, unroll-full) should not be doing runtime unrolling 8; even if it is globally enabled through -unroll-runtime option 9; 10; RUN: opt < %s -S -passes='require<opt-remark-emit>,loop-unroll<peeling;no-runtime>' -unroll-runtime=true -unroll-runtime-epilog=true | FileCheck %s -check-prefixes=NOEPILOG,COMMON 11; RUN: opt < %s -S -passes='require<opt-remark-emit>,loop-unroll<peeling;no-runtime>' -unroll-runtime=true -unroll-runtime-epilog=false | FileCheck %s -check-prefixes=NOPROLOG,COMMON 12; RUN: opt < %s -S -passes='require<opt-remark-emit>,loop(loop-unroll-full)' -unroll-runtime=true -unroll-runtime-epilog=true | FileCheck %s -check-prefixes=NOEPILOG,COMMON 13; RUN: opt < %s -S -passes='require<opt-remark-emit>,loop(loop-unroll-full)' -unroll-runtime=true -unroll-runtime-epilog=false | FileCheck %s -check-prefixes=NOPROLOG,COMMON 14 15target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128" 16 17; Tests for unrolling loops with run-time trip counts 18 19; COMMON-LABEL: @test( 20 21; EPILOG: entry: 22; EPILOG: br i1 %cmp1, label %for.end, label %for.body.preheader, !prof [[EPILOG_PROF_0:![0-9]+]] 23; EPILOG: for.body.preheader: 24; EPILOG: %xtraiter = and i32 %n 25; EPILOG: br i1 %1, label %for.end.loopexit.unr-lcssa, label %for.body.preheader.new, !prof [[EPILOG_PROF_1:![0-9]+]] 26 27; EPILOG: for.end.loopexit.unr-lcssa: 28; EPILOG: %lcmp.mod = icmp ne i32 %xtraiter, 0 29; EPILOG: br i1 %lcmp.mod, label %for.body.epil.preheader, label %for.end.loopexit, !prof [[EPILOG_PROF_2:![0-9]+]] 30 31; NOEPILOG-NOT: %xtraiter = and i32 %n 32 33; PROLOG: entry: 34; PROLOG: br i1 %cmp1, label %for.end, label %for.body.preheader, !prof [[PROLOG_PROF_0:![0-9]+]] 35 36; PROLOG: for.body.preheader: 37; PROLOG: %xtraiter = and i32 %n 38; PROLOG: %lcmp.mod = icmp ne i32 %xtraiter, 0 39; PROLOG: br i1 %lcmp.mod, label %for.body.prol.preheader, label %for.body.prol.loopexit, !prof [[PROLOG_PROF_1:![0-9]+]] 40 41; NOPROLOG-NOT: %xtraiter = and i32 %n 42 43; EPILOG: for.body.epil: 44; EPILOG: %indvars.iv.epil = phi i64 [ %indvars.iv.next.epil, %for.body.epil ], [ %indvars.iv.unr, %for.body.epil.preheader ] 45; EPILOG: %epil.iter.next = add i32 %epil.iter, 1 46; EPILOG: %epil.iter.cmp = icmp ne i32 %epil.iter.next, %xtraiter 47; EPILOG: br i1 %epil.iter.cmp, label %for.body.epil, label %for.end.loopexit.epilog-lcssa, !prof [[EPILOG_PROF_3:![0-9]+]], !llvm.loop [[EPILOG_LOOP:![0-9]+]] 48 49; NOEPILOG: for.body: 50; NOEPILOG-NOT: for.body.epil: 51 52; PROLOG: for.body.prol: 53; PROLOG: %indvars.iv.prol = phi i64 [ %indvars.iv.next.prol, %for.body.prol ], [ 0, %for.body.prol.preheader ] 54; PROLOG: %prol.iter.next = add i32 %prol.iter, 1 55; PROLOG: %prol.iter.cmp = icmp ne i32 %prol.iter.next, %xtraiter 56; PROLOG: br i1 %prol.iter.cmp, label %for.body.prol, label %for.body.prol.loopexit.unr-lcssa, !prof [[PROLOG_PROF_2:![0-9]+]], !llvm.loop [[PROLOG_LOOP:![0-9]+]] 57 58; PROLOG: for.body.prol.loopexit: 59; PROLOG: br i1 %2, label %for.end.loopexit, label %for.body.preheader.new, !prof [[PROLOG_PROF_1:![0-9]+]] 60 61; NOPROLOG: for.body: 62; NOPROLOG-NOT: for.body.prol: 63 64 65define i32 @test(ptr nocapture %a, i32 %n) nounwind uwtable readonly !prof !2 { 66entry: 67 %cmp1 = icmp eq i32 %n, 0 68 br i1 %cmp1, label %for.end, label %for.body, !prof !3 69 70for.body: ; preds = %for.body, %entry 71 %indvars.iv = phi i64 [ %indvars.iv.next, %for.body ], [ 0, %entry ] 72 %sum.02 = phi i32 [ %add, %for.body ], [ 0, %entry ] 73 %arrayidx = getelementptr inbounds i32, ptr %a, i64 %indvars.iv 74 %0 = load i32, ptr %arrayidx, align 4 75 %add = add nsw i32 %0, %sum.02 76 %indvars.iv.next = add i64 %indvars.iv, 1 77 %lftr.wideiv = trunc i64 %indvars.iv.next to i32 78 %exitcond = icmp eq i32 %lftr.wideiv, %n 79 br i1 %exitcond, label %for.end, label %for.body, !prof !4 80 81for.end: ; preds = %for.body, %entry 82 %sum.0.lcssa = phi i32 [ 0, %entry ], [ %add, %for.body ] 83 ret i32 %sum.0.lcssa 84} 85 86 87; Still try to completely unroll loops with compile-time trip counts 88; even if the -unroll-runtime is specified 89 90; COMMON-LABEL: @test1( 91; COMMON: for.body: 92; COMMON-NOT: for.body.epil: 93; COMMON-NOT: for.body.prol: 94 95define i32 @test1(ptr nocapture %a) nounwind uwtable readonly { 96entry: 97 br label %for.body 98 99for.body: ; preds = %for.body, %entry 100 %indvars.iv = phi i64 [ 0, %entry ], [ %indvars.iv.next, %for.body ] 101 %sum.01 = phi i32 [ 0, %entry ], [ %add, %for.body ] 102 %arrayidx = getelementptr inbounds i32, ptr %a, i64 %indvars.iv 103 %0 = load i32, ptr %arrayidx, align 4 104 %add = add nsw i32 %0, %sum.01 105 %indvars.iv.next = add i64 %indvars.iv, 1 106 %lftr.wideiv = trunc i64 %indvars.iv.next to i32 107 %exitcond = icmp eq i32 %lftr.wideiv, 5 108 br i1 %exitcond, label %for.end, label %for.body 109 110for.end: ; preds = %for.body 111 ret i32 %add 112} 113 114; This is test 2007-05-09-UnknownTripCount.ll which can be unrolled now 115; if the -unroll-runtime option is turned on 116 117; COMMON-LABEL: @foo( 118; EPILOG: bb72.2: 119; PROLOG: bb72.2: 120; NOEPILOG-NOT: bb72.2: 121; NOPROLOG-NOT: bb72.2: 122 123define void @foo(i32 %trips) { 124entry: 125 br label %cond_true.outer 126 127cond_true.outer: 128 %indvar1.ph = phi i32 [ 0, %entry ], [ %indvar.next2, %bb72 ] 129 br label %bb72 130 131bb72: 132 %indvar.next2 = add i32 %indvar1.ph, 1 133 %exitcond3 = icmp eq i32 %indvar.next2, %trips 134 br i1 %exitcond3, label %cond_true138, label %cond_true.outer 135 136cond_true138: 137 ret void 138} 139 140 141; Test run-time unrolling for a loop that counts down by -2. 142 143; COMMON-LABEL: @down( 144; EPILOG: for.body.epil: 145; EPILOG: br i1 %epil.iter.cmp, label %for.body.epil, label %for.cond.for.end_crit_edge.epilog-lcssa 146 147; NOEPILOG: for.body: 148; NOEPILOG-NOT: for.body.epil: 149 150; PROLOG: for.body.prol: 151; PROLOG: br i1 %prol.iter.cmp, label %for.body.prol, label %for.body.prol.loopexit 152 153; NOPROLOG: for.body: 154; NOPROLOG-NOT: for.body.prol: 155 156define zeroext i16 @down(ptr nocapture %p, i32 %len) nounwind uwtable readonly { 157entry: 158 %cmp2 = icmp eq i32 %len, 0 159 br i1 %cmp2, label %for.end, label %for.body 160 161for.body: ; preds = %for.body, %entry 162 %p.addr.05 = phi ptr [ %incdec.ptr, %for.body ], [ %p, %entry ] 163 %len.addr.04 = phi i32 [ %sub, %for.body ], [ %len, %entry ] 164 %res.03 = phi i32 [ %add, %for.body ], [ 0, %entry ] 165 %incdec.ptr = getelementptr inbounds i16, ptr %p.addr.05, i64 1 166 %0 = load i16, ptr %p.addr.05, align 2 167 %conv = zext i16 %0 to i32 168 %add = add i32 %conv, %res.03 169 %sub = add nsw i32 %len.addr.04, -2 170 %cmp = icmp eq i32 %sub, 0 171 br i1 %cmp, label %for.cond.for.end_crit_edge, label %for.body 172 173for.cond.for.end_crit_edge: ; preds = %for.body 174 %phitmp = trunc i32 %add to i16 175 br label %for.end 176 177for.end: ; preds = %for.cond.for.end_crit_edge, %entry 178 %res.0.lcssa = phi i16 [ %phitmp, %for.cond.for.end_crit_edge ], [ 0, %entry ] 179 ret i16 %res.0.lcssa 180} 181 182; Test run-time unrolling disable metadata. 183; COMMON-LABEL: @test2( 184 185; EPILOG: for.body: 186; EPILOG-NOT: for.body.epil: 187 188; NOEPILOG: for.body: 189; NOEPILOG-NOT: for.body.epil: 190 191; PROLOG: for.body: 192; PROLOG-NOT: for.body.prol: 193 194; NOPROLOG: for.body: 195; NOPROLOG-NOT: for.body.prol: 196 197define zeroext i16 @test2(ptr nocapture %p, i32 %len) nounwind uwtable readonly { 198entry: 199 %cmp2 = icmp eq i32 %len, 0 200 br i1 %cmp2, label %for.end, label %for.body 201 202for.body: ; preds = %for.body, %entry 203 %p.addr.05 = phi ptr [ %incdec.ptr, %for.body ], [ %p, %entry ] 204 %len.addr.04 = phi i32 [ %sub, %for.body ], [ %len, %entry ] 205 %res.03 = phi i32 [ %add, %for.body ], [ 0, %entry ] 206 %incdec.ptr = getelementptr inbounds i16, ptr %p.addr.05, i64 1 207 %0 = load i16, ptr %p.addr.05, align 2 208 %conv = zext i16 %0 to i32 209 %add = add i32 %conv, %res.03 210 %sub = add nsw i32 %len.addr.04, -2 211 %cmp = icmp eq i32 %sub, 0 212 br i1 %cmp, label %for.cond.for.end_crit_edge, label %for.body, !llvm.loop !0 213 214for.cond.for.end_crit_edge: ; preds = %for.body 215 %phitmp = trunc i32 %add to i16 216 br label %for.end 217 218for.end: ; preds = %for.cond.for.end_crit_edge, %entry 219 %res.0.lcssa = phi i16 [ %phitmp, %for.cond.for.end_crit_edge ], [ 0, %entry ] 220 ret i16 %res.0.lcssa 221} 222 223; dont unroll loop with multiple exit/exiting blocks, unless 224; -runtime-unroll-multi-exit=true 225; single exit, multiple exiting blocks. 226define void @unique_exit(i32 %arg) { 227; COMMON-LABEL: @unique_exit( 228; COMMON-NOT: .unr 229 230entry: 231 %tmp = icmp sgt i32 undef, %arg 232 br i1 %tmp, label %preheader, label %returnblock 233 234preheader: ; preds = %entry 235 br label %header 236 237LoopExit: ; preds = %header, %latch 238 %tmp2.ph = phi i32 [ %tmp4, %header ], [ -1, %latch ] 239 br label %returnblock 240 241returnblock: ; preds = %LoopExit, %entry 242 %tmp2 = phi i32 [ -1, %entry ], [ %tmp2.ph, %LoopExit ] 243 ret void 244 245header: ; preds = %preheader, %latch 246 %tmp4 = phi i32 [ %inc, %latch ], [ %arg, %preheader ] 247 %inc = add nsw i32 %tmp4, 1 248 br i1 true, label %LoopExit, label %latch 249 250latch: ; preds = %header 251 %cmp = icmp slt i32 %inc, undef 252 br i1 %cmp, label %header, label %LoopExit 253} 254 255; multiple exit blocks. don't unroll 256define void @multi_exit(i64 %trip, i1 %cond) { 257; COMMON-LABEL: @multi_exit( 258; COMMON-NOT: .unr 259 260entry: 261 br label %loop_header 262 263loop_header: 264 %iv = phi i64 [ 0, %entry ], [ %iv_next, %loop_latch ] 265 br i1 %cond, label %loop_latch, label %loop_exiting_bb1 266 267loop_exiting_bb1: 268 br i1 false, label %loop_exiting_bb2, label %exit1 269 270loop_exiting_bb2: 271 br i1 false, label %loop_latch, label %exit3 272 273exit3: 274 ret void 275 276loop_latch: 277 %iv_next = add i64 %iv, 1 278 %cmp = icmp ne i64 %iv_next, %trip 279 br i1 %cmp, label %loop_header, label %exit2.loopexit 280 281exit1: 282 ret void 283 284exit2.loopexit: 285 ret void 286} 287 288!0 = distinct !{!0, !1} 289!1 = !{!"llvm.loop.unroll.runtime.disable"} 290!2 = !{!"function_entry_count", i64 1} 291!3 = !{!"branch_weights", i32 1, i32 11} 292!4 = !{!"branch_weights", i32 1, i32 42} 293 294; need to use LABEL here to separate function IR matching from metadata matching 295; COMMON-LABEL: {{^}}!0 = 296 297; EPILOG: [[EPILOG_PROF_0]] = !{!"branch_weights", i32 1, i32 11} 298; EPILOG: [[EPILOG_PROF_1]] = !{!"branch_weights", i32 1, i32 127} 299; EPILOG: [[EPILOG_PROF_2]] = !{!"branch_weights", i32 1, i32 7} 300; EPILOG: [[EPILOG_PROF_3]] = !{!"branch_weights", i32 3, i32 1} 301 302; EPILOG: [[EPILOG_LOOP]] = distinct !{[[EPILOG_LOOP]], [[EPILOG_LOOP_1:![0-9]+]]} 303; EPILOG: [[EPILOG_LOOP_1]] = !{!"llvm.loop.unroll.disable"} 304 305; PROLOG: [[PROLOG_PROF_0]] = !{!"branch_weights", i32 1, i32 11} 306; PROLOG: [[PROLOG_PROF_1]] = !{!"branch_weights", i32 1, i32 127} 307; PROLOG: [[PROLOG_PROF_2]] = !{!"branch_weights", i32 3, i32 1} 308 309; PROLOG: distinct !{[[PROLOG_LOOP]], [[PROLOG_LOOP_1:![0-9]+]]} 310; PROLOG: [[PROLOG_LOOP_1]] = !{!"llvm.loop.unroll.disable"} 311