312 lines · plain
1; RUN: opt < %s -S -passes=loop-unroll -unroll-runtime=true -unroll-runtime-epilog=true | FileCheck %s -check-prefixes=EPILOG,COMMON2; RUN: opt < %s -S -passes=loop-unroll -unroll-runtime=true -unroll-runtime-epilog=false | FileCheck %s -check-prefixes=PROLOG,COMMON3;4; RUN: opt < %s -S -passes='require<opt-remark-emit>,loop-unroll' -unroll-runtime=true -unroll-runtime-epilog=true | FileCheck %s -check-prefixes=EPILOG,COMMON5; RUN: opt < %s -S -passes='require<opt-remark-emit>,loop-unroll' -unroll-runtime=true -unroll-runtime-epilog=false | FileCheck %s -check-prefixes=PROLOG,COMMON6;7; Restricted versions of unroll (unroll<peeling;noruntime>, unroll-full) should not be doing runtime unrolling8; even if it is globally enabled through -unroll-runtime option9;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,COMMON11; 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,COMMON12; 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,COMMON13; 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,COMMON14 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 counts18 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 %n25; EPILOG: br i1 %1, label %for.body.epil.preheader, 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, 029; 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 %n32 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 %n38; PROLOG: %lcmp.mod = icmp ne i32 %xtraiter, 039; 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 %n42 43; EPILOG: for.body.epil:44; EPILOG: %indvars.iv.epil = phi i64 [ %indvars.iv.next.epil, %for.body.epil ], [ %indvars.iv.epil.init, %for.body.epil.preheader ]45; EPILOG: %epil.iter.next = add i32 %epil.iter, 146; EPILOG: %epil.iter.cmp = icmp ne i32 %epil.iter.next, %xtraiter47; 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, 155; PROLOG: %prol.iter.cmp = icmp ne i32 %prol.iter.next, %xtraiter56; 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, 068 br i1 %cmp1, label %for.end, label %for.body, !prof !369 70for.body: ; preds = %for.body, %entry71 %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.iv74 %0 = load i32, ptr %arrayidx, align 475 %add = add nsw i32 %0, %sum.0276 %indvars.iv.next = add i64 %indvars.iv, 177 %lftr.wideiv = trunc i64 %indvars.iv.next to i3278 %exitcond = icmp eq i32 %lftr.wideiv, %n79 br i1 %exitcond, label %for.end, label %for.body, !prof !480 81for.end: ; preds = %for.body, %entry82 %sum.0.lcssa = phi i32 [ 0, %entry ], [ %add, %for.body ]83 ret i32 %sum.0.lcssa84}85 86 87; Still try to completely unroll loops with compile-time trip counts88; even if the -unroll-runtime is specified89 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.body98 99for.body: ; preds = %for.body, %entry100 %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.iv103 %0 = load i32, ptr %arrayidx, align 4104 %add = add nsw i32 %0, %sum.01105 %indvars.iv.next = add i64 %indvars.iv, 1106 %lftr.wideiv = trunc i64 %indvars.iv.next to i32107 %exitcond = icmp eq i32 %lftr.wideiv, 5108 br i1 %exitcond, label %for.end, label %for.body109 110for.end: ; preds = %for.body111 ret i32 %add112}113 114; This is test 2007-05-09-UnknownTripCount.ll which can be unrolled now115; if the -unroll-runtime option is turned on116 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.outer126 127cond_true.outer:128 %indvar1.ph = phi i32 [ 0, %entry ], [ %indvar.next2, %bb72 ]129 br label %bb72130 131bb72:132 %indvar.next2 = add i32 %indvar1.ph, 1133 %exitcond3 = icmp eq i32 %indvar.next2, %trips134 br i1 %exitcond3, label %cond_true138, label %cond_true.outer135 136cond_true138:137 ret void138}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-lcssa146 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.loopexit152 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, 0159 br i1 %cmp2, label %for.end, label %for.body160 161for.body: ; preds = %for.body, %entry162 %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 1166 %0 = load i16, ptr %p.addr.05, align 2167 %conv = zext i16 %0 to i32168 %add = add i32 %conv, %res.03169 %sub = add nsw i32 %len.addr.04, -2170 %cmp = icmp eq i32 %sub, 0171 br i1 %cmp, label %for.cond.for.end_crit_edge, label %for.body172 173for.cond.for.end_crit_edge: ; preds = %for.body174 %phitmp = trunc i32 %add to i16175 br label %for.end176 177for.end: ; preds = %for.cond.for.end_crit_edge, %entry178 %res.0.lcssa = phi i16 [ %phitmp, %for.cond.for.end_crit_edge ], [ 0, %entry ]179 ret i16 %res.0.lcssa180}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, 0200 br i1 %cmp2, label %for.end, label %for.body201 202for.body: ; preds = %for.body, %entry203 %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 1207 %0 = load i16, ptr %p.addr.05, align 2208 %conv = zext i16 %0 to i32209 %add = add i32 %conv, %res.03210 %sub = add nsw i32 %len.addr.04, -2211 %cmp = icmp eq i32 %sub, 0212 br i1 %cmp, label %for.cond.for.end_crit_edge, label %for.body, !llvm.loop !0213 214for.cond.for.end_crit_edge: ; preds = %for.body215 %phitmp = trunc i32 %add to i16216 br label %for.end217 218for.end: ; preds = %for.cond.for.end_crit_edge, %entry219 %res.0.lcssa = phi i16 [ %phitmp, %for.cond.for.end_crit_edge ], [ 0, %entry ]220 ret i16 %res.0.lcssa221}222 223; dont unroll loop with multiple exit/exiting blocks, unless224; -runtime-unroll-multi-exit=true225; single exit, multiple exiting blocks.226define void @unique_exit(i32 %arg) {227; COMMON-LABEL: @unique_exit(228; COMMON-NOT: .unr229 230entry:231 %tmp = icmp sgt i32 undef, %arg232 br i1 %tmp, label %preheader, label %returnblock233 234preheader: ; preds = %entry235 br label %header236 237LoopExit: ; preds = %header, %latch238 %tmp2.ph = phi i32 [ %tmp4, %header ], [ -1, %latch ]239 br label %returnblock240 241returnblock: ; preds = %LoopExit, %entry242 %tmp2 = phi i32 [ -1, %entry ], [ %tmp2.ph, %LoopExit ]243 ret void244 245header: ; preds = %preheader, %latch246 %tmp4 = phi i32 [ %inc, %latch ], [ %arg, %preheader ]247 %inc = add nsw i32 %tmp4, 1248 br i1 true, label %LoopExit, label %latch249 250latch: ; preds = %header251 %cmp = icmp slt i32 %inc, undef252 br i1 %cmp, label %header, label %LoopExit253}254 255; multiple exit blocks. don't unroll256define void @multi_exit(i64 %trip, i1 %cond) {257; COMMON-LABEL: @multi_exit(258; COMMON-NOT: .unr259 260entry:261 br label %loop_header262 263loop_header:264 %iv = phi i64 [ 0, %entry ], [ %iv_next, %loop_latch ]265 br i1 %cond, label %loop_latch, label %loop_exiting_bb1266 267loop_exiting_bb1:268 br i1 false, label %loop_exiting_bb2, label %exit1269 270loop_exiting_bb2:271 br i1 false, label %loop_latch, label %exit3272 273exit3:274 ret void275 276loop_latch:277 %iv_next = add i64 %iv, 1278 %cmp = icmp ne i64 %iv_next, %trip279 br i1 %cmp, label %loop_header, label %exit2.loopexit280 281exit1:282 ret void283 284exit2.loopexit:285 ret void286}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 matching295; COMMON-LABEL: {{^}}!0 =296 297; EPILOG: [[EPILOG_PROF_0]] = !{!"branch_weights", i32 1, i32 11}298; EPILOG: [[EPILOG_PROF_1]] = !{!"branch_weights", i32 326124004, i32 1821359644}299; EPILOG: [[EPILOG_PROF_2]] = !{!"branch_weights", i32 1856428066, i32 291055582}300; EPILOG: [[EPILOG_PROF_3]] = !{!"branch_weights", i32 1597681585, i32 549802063}301 302; EPILOG: [[EPILOG_LOOP]] = distinct !{[[EPILOG_LOOP]], [[EPILOG_TC:![0-9]+]], [[EPILOG_LOOP_1:![0-9]+]]}303; EPILOG: [[EPILOG_TC]] = !{!"llvm.loop.estimated_trip_count", i32 3}304; EPILOG: [[EPILOG_LOOP_1]] = !{!"llvm.loop.unroll.disable"}305 306; PROLOG: [[PROLOG_PROF_0]] = !{!"branch_weights", i32 1, i32 11}307; PROLOG: [[PROLOG_PROF_1]] = !{!"branch_weights", i32 1, i32 127}308; PROLOG: [[PROLOG_PROF_2]] = !{!"branch_weights", i32 3, i32 1}309 310; PROLOG: distinct !{[[PROLOG_LOOP]], [[PROLOG_LOOP_1:![0-9]+]]}311; PROLOG: [[PROLOG_LOOP_1]] = !{!"llvm.loop.unroll.disable"}312