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