192ce29eeSNikita Popov; NOTE: Assertions have been autogenerated by utils/update_test_checks.py 2cee313d2SEric Christopher; RUN: opt < %s -S -loop-unroll -unroll-runtime=true -unroll-count=16 | FileCheck --check-prefix=UNROLL-16 %s 3cee313d2SEric Christopher; RUN: opt < %s -S -loop-unroll -unroll-runtime=true -unroll-count=4 | FileCheck --check-prefix=UNROLL-4 %s 4cee313d2SEric Christopher 5a95796a3SArthur Eubanks; RUN: opt < %s -S -passes='require<opt-remark-emit>,loop-unroll' -unroll-runtime=true -unroll-count=16 | FileCheck --check-prefix=UNROLL-16 %s 6a95796a3SArthur Eubanks; RUN: opt < %s -S -passes='require<opt-remark-emit>,loop-unroll' -unroll-runtime=true -unroll-count=4 | FileCheck --check-prefix=UNROLL-4 %s 7cee313d2SEric Christopher 8cee313d2SEric Christopher; Given that the trip-count of this loop is a 3-bit value, we cannot 9cee313d2SEric Christopher; safely unroll it with a count of anything more than 8. 10cee313d2SEric Christopher 11cee313d2SEric Christopherdefine i3 @test(i3* %a, i3 %n) { 12cee313d2SEric Christopher; UNROLL-16-LABEL: @test( 1392ce29eeSNikita Popov; UNROLL-16-NEXT: entry: 1492ce29eeSNikita Popov; UNROLL-16-NEXT: [[CMP1:%.*]] = icmp eq i3 [[N:%.*]], 0 1592ce29eeSNikita Popov; UNROLL-16-NEXT: br i1 [[CMP1]], label [[FOR_END:%.*]], label [[FOR_BODY_PREHEADER:%.*]] 1692ce29eeSNikita Popov; UNROLL-16: for.body.preheader: 1792ce29eeSNikita Popov; UNROLL-16-NEXT: br label [[FOR_BODY:%.*]] 1892ce29eeSNikita Popov; UNROLL-16: for.body: 198fdd7c2fSNikita Popov; UNROLL-16-NEXT: [[TMP0:%.*]] = load i3, i3* [[A:%.*]], align 1 2092ce29eeSNikita Popov; UNROLL-16-NEXT: [[EXITCOND:%.*]] = icmp eq i3 1, [[N]] 2192ce29eeSNikita Popov; UNROLL-16-NEXT: br i1 [[EXITCOND]], label [[FOR_END_LOOPEXIT:%.*]], label [[FOR_BODY_1:%.*]] 22de2fed61SPhilip Reames; UNROLL-16: for.body.1: 23de2fed61SPhilip Reames; UNROLL-16-NEXT: [[ARRAYIDX_1:%.*]] = getelementptr inbounds i3, i3* [[A]], i64 1 24de2fed61SPhilip Reames; UNROLL-16-NEXT: [[TMP1:%.*]] = load i3, i3* [[ARRAYIDX_1]], align 1 25de2fed61SPhilip Reames; UNROLL-16-NEXT: [[ADD_1:%.*]] = add nsw i3 [[TMP1]], [[TMP0]] 26de2fed61SPhilip Reames; UNROLL-16-NEXT: [[EXITCOND_1:%.*]] = icmp eq i3 2, [[N]] 27de2fed61SPhilip Reames; UNROLL-16-NEXT: br i1 [[EXITCOND_1]], label [[FOR_END_LOOPEXIT]], label [[FOR_BODY_2:%.*]] 28de2fed61SPhilip Reames; UNROLL-16: for.body.2: 29de2fed61SPhilip Reames; UNROLL-16-NEXT: [[ARRAYIDX_2:%.*]] = getelementptr inbounds i3, i3* [[A]], i64 2 30de2fed61SPhilip Reames; UNROLL-16-NEXT: [[TMP2:%.*]] = load i3, i3* [[ARRAYIDX_2]], align 1 31de2fed61SPhilip Reames; UNROLL-16-NEXT: [[ADD_2:%.*]] = add nsw i3 [[TMP2]], [[ADD_1]] 32de2fed61SPhilip Reames; UNROLL-16-NEXT: [[EXITCOND_2:%.*]] = icmp eq i3 3, [[N]] 33de2fed61SPhilip Reames; UNROLL-16-NEXT: br i1 [[EXITCOND_2]], label [[FOR_END_LOOPEXIT]], label [[FOR_BODY_3:%.*]] 34de2fed61SPhilip Reames; UNROLL-16: for.body.3: 35de2fed61SPhilip Reames; UNROLL-16-NEXT: [[ARRAYIDX_3:%.*]] = getelementptr inbounds i3, i3* [[A]], i64 3 36de2fed61SPhilip Reames; UNROLL-16-NEXT: [[TMP3:%.*]] = load i3, i3* [[ARRAYIDX_3]], align 1 37de2fed61SPhilip Reames; UNROLL-16-NEXT: [[ADD_3:%.*]] = add nsw i3 [[TMP3]], [[ADD_2]] 38de2fed61SPhilip Reames; UNROLL-16-NEXT: [[EXITCOND_3:%.*]] = icmp eq i3 -4, [[N]] 39de2fed61SPhilip Reames; UNROLL-16-NEXT: br i1 [[EXITCOND_3]], label [[FOR_END_LOOPEXIT]], label [[FOR_BODY_4:%.*]] 40de2fed61SPhilip Reames; UNROLL-16: for.body.4: 41de2fed61SPhilip Reames; UNROLL-16-NEXT: [[ARRAYIDX_4:%.*]] = getelementptr inbounds i3, i3* [[A]], i64 4 42de2fed61SPhilip Reames; UNROLL-16-NEXT: [[TMP4:%.*]] = load i3, i3* [[ARRAYIDX_4]], align 1 43de2fed61SPhilip Reames; UNROLL-16-NEXT: [[ADD_4:%.*]] = add nsw i3 [[TMP4]], [[ADD_3]] 44de2fed61SPhilip Reames; UNROLL-16-NEXT: [[EXITCOND_4:%.*]] = icmp eq i3 -3, [[N]] 45de2fed61SPhilip Reames; UNROLL-16-NEXT: br i1 [[EXITCOND_4]], label [[FOR_END_LOOPEXIT]], label [[FOR_BODY_5:%.*]] 46de2fed61SPhilip Reames; UNROLL-16: for.body.5: 47de2fed61SPhilip Reames; UNROLL-16-NEXT: [[ARRAYIDX_5:%.*]] = getelementptr inbounds i3, i3* [[A]], i64 5 48de2fed61SPhilip Reames; UNROLL-16-NEXT: [[TMP5:%.*]] = load i3, i3* [[ARRAYIDX_5]], align 1 49de2fed61SPhilip Reames; UNROLL-16-NEXT: [[ADD_5:%.*]] = add nsw i3 [[TMP5]], [[ADD_4]] 50de2fed61SPhilip Reames; UNROLL-16-NEXT: [[EXITCOND_5:%.*]] = icmp eq i3 -2, [[N]] 51de2fed61SPhilip Reames; UNROLL-16-NEXT: br i1 [[EXITCOND_5]], label [[FOR_END_LOOPEXIT]], label [[FOR_BODY_6:%.*]] 52de2fed61SPhilip Reames; UNROLL-16: for.body.6: 53de2fed61SPhilip Reames; UNROLL-16-NEXT: [[ARRAYIDX_6:%.*]] = getelementptr inbounds i3, i3* [[A]], i64 6 54de2fed61SPhilip Reames; UNROLL-16-NEXT: [[TMP6:%.*]] = load i3, i3* [[ARRAYIDX_6]], align 1 55de2fed61SPhilip Reames; UNROLL-16-NEXT: [[ADD_6:%.*]] = add nsw i3 [[TMP6]], [[ADD_5]] 56de2fed61SPhilip Reames; UNROLL-16-NEXT: br label [[FOR_END_LOOPEXIT]] 5792ce29eeSNikita Popov; UNROLL-16: for.end.loopexit: 58de2fed61SPhilip Reames; UNROLL-16-NEXT: [[ADD_LCSSA:%.*]] = phi i3 [ [[TMP0]], [[FOR_BODY]] ], [ [[ADD_1]], [[FOR_BODY_1]] ], [ [[ADD_2]], [[FOR_BODY_2]] ], [ [[ADD_3]], [[FOR_BODY_3]] ], [ [[ADD_4]], [[FOR_BODY_4]] ], [ [[ADD_5]], [[FOR_BODY_5]] ], [ [[ADD_6]], [[FOR_BODY_6]] ] 5992ce29eeSNikita Popov; UNROLL-16-NEXT: br label [[FOR_END]] 6092ce29eeSNikita Popov; UNROLL-16: for.end: 6192ce29eeSNikita Popov; UNROLL-16-NEXT: [[SUM_0_LCSSA:%.*]] = phi i3 [ 0, [[ENTRY:%.*]] ], [ [[ADD_LCSSA]], [[FOR_END_LOOPEXIT]] ] 6292ce29eeSNikita Popov; UNROLL-16-NEXT: ret i3 [[SUM_0_LCSSA]] 6392ce29eeSNikita Popov; 64cee313d2SEric Christopher; UNROLL-4-LABEL: @test( 6592ce29eeSNikita Popov; UNROLL-4-NEXT: entry: 6692ce29eeSNikita Popov; UNROLL-4-NEXT: [[CMP1:%.*]] = icmp eq i3 [[N:%.*]], 0 6792ce29eeSNikita Popov; UNROLL-4-NEXT: br i1 [[CMP1]], label [[FOR_END:%.*]], label [[FOR_BODY_PREHEADER:%.*]] 6892ce29eeSNikita Popov; UNROLL-4: for.body.preheader: 6992ce29eeSNikita Popov; UNROLL-4-NEXT: [[TMP0:%.*]] = add i3 [[N]], -1 7092ce29eeSNikita Popov; UNROLL-4-NEXT: [[XTRAITER:%.*]] = and i3 [[N]], 3 7192ce29eeSNikita Popov; UNROLL-4-NEXT: [[TMP1:%.*]] = icmp ult i3 [[TMP0]], 3 7292ce29eeSNikita Popov; UNROLL-4-NEXT: br i1 [[TMP1]], label [[FOR_END_LOOPEXIT_UNR_LCSSA:%.*]], label [[FOR_BODY_PREHEADER_NEW:%.*]] 7392ce29eeSNikita Popov; UNROLL-4: for.body.preheader.new: 7492ce29eeSNikita Popov; UNROLL-4-NEXT: [[UNROLL_ITER:%.*]] = sub i3 [[N]], [[XTRAITER]] 7592ce29eeSNikita Popov; UNROLL-4-NEXT: br label [[FOR_BODY:%.*]] 7692ce29eeSNikita Popov; UNROLL-4: for.body: 7792ce29eeSNikita Popov; UNROLL-4-NEXT: [[INDVARS_IV:%.*]] = phi i64 [ 0, [[FOR_BODY_PREHEADER_NEW]] ], [ [[INDVARS_IV_NEXT_3:%.*]], [[FOR_BODY]] ] 7892ce29eeSNikita Popov; UNROLL-4-NEXT: [[SUM_02:%.*]] = phi i3 [ 0, [[FOR_BODY_PREHEADER_NEW]] ], [ [[ADD_3:%.*]], [[FOR_BODY]] ] 7937ead201SPhilip Reames; UNROLL-4-NEXT: [[NITER:%.*]] = phi i3 [ 0, [[FOR_BODY_PREHEADER_NEW]] ], [ [[NITER_NEXT_3:%.*]], [[FOR_BODY]] ] 8092ce29eeSNikita Popov; UNROLL-4-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds i3, i3* [[A:%.*]], i64 [[INDVARS_IV]] 8192ce29eeSNikita Popov; UNROLL-4-NEXT: [[TMP2:%.*]] = load i3, i3* [[ARRAYIDX]], align 1 8292ce29eeSNikita Popov; UNROLL-4-NEXT: [[ADD:%.*]] = add nsw i3 [[TMP2]], [[SUM_02]] 8392ce29eeSNikita Popov; UNROLL-4-NEXT: [[INDVARS_IV_NEXT:%.*]] = add nuw nsw i64 [[INDVARS_IV]], 1 8437ead201SPhilip Reames; UNROLL-4-NEXT: [[NITER_NEXT:%.*]] = add nuw nsw i3 [[NITER]], 1 8592ce29eeSNikita Popov; UNROLL-4-NEXT: [[ARRAYIDX_1:%.*]] = getelementptr inbounds i3, i3* [[A]], i64 [[INDVARS_IV_NEXT]] 8692ce29eeSNikita Popov; UNROLL-4-NEXT: [[TMP3:%.*]] = load i3, i3* [[ARRAYIDX_1]], align 1 8792ce29eeSNikita Popov; UNROLL-4-NEXT: [[ADD_1:%.*]] = add nsw i3 [[TMP3]], [[ADD]] 8892ce29eeSNikita Popov; UNROLL-4-NEXT: [[INDVARS_IV_NEXT_1:%.*]] = add nuw nsw i64 [[INDVARS_IV_NEXT]], 1 8937ead201SPhilip Reames; UNROLL-4-NEXT: [[NITER_NEXT_1:%.*]] = add nuw nsw i3 [[NITER_NEXT]], 1 9092ce29eeSNikita Popov; UNROLL-4-NEXT: [[ARRAYIDX_2:%.*]] = getelementptr inbounds i3, i3* [[A]], i64 [[INDVARS_IV_NEXT_1]] 9192ce29eeSNikita Popov; UNROLL-4-NEXT: [[TMP4:%.*]] = load i3, i3* [[ARRAYIDX_2]], align 1 9292ce29eeSNikita Popov; UNROLL-4-NEXT: [[ADD_2:%.*]] = add nsw i3 [[TMP4]], [[ADD_1]] 9392ce29eeSNikita Popov; UNROLL-4-NEXT: [[INDVARS_IV_NEXT_2:%.*]] = add nuw nsw i64 [[INDVARS_IV_NEXT_1]], 1 9437ead201SPhilip Reames; UNROLL-4-NEXT: [[NITER_NEXT_2:%.*]] = add nuw nsw i3 [[NITER_NEXT_1]], 1 9592ce29eeSNikita Popov; UNROLL-4-NEXT: [[ARRAYIDX_3:%.*]] = getelementptr inbounds i3, i3* [[A]], i64 [[INDVARS_IV_NEXT_2]] 9692ce29eeSNikita Popov; UNROLL-4-NEXT: [[TMP5:%.*]] = load i3, i3* [[ARRAYIDX_3]], align 1 9792ce29eeSNikita Popov; UNROLL-4-NEXT: [[ADD_3]] = add nsw i3 [[TMP5]], [[ADD_2]] 98*8d85e945SPhilip Reames; UNROLL-4-NEXT: [[INDVARS_IV_NEXT_3]] = add nuw nsw i64 [[INDVARS_IV_NEXT_2]], 1 9937ead201SPhilip Reames; UNROLL-4-NEXT: [[NITER_NEXT_3]] = add i3 [[NITER_NEXT_2]], 1 10037ead201SPhilip Reames; UNROLL-4-NEXT: [[NITER_NCMP_3:%.*]] = icmp eq i3 [[NITER_NEXT_3]], [[UNROLL_ITER]] 10192ce29eeSNikita Popov; UNROLL-4-NEXT: br i1 [[NITER_NCMP_3]], label [[FOR_END_LOOPEXIT_UNR_LCSSA_LOOPEXIT:%.*]], label [[FOR_BODY]], !llvm.loop [[LOOP0:![0-9]+]] 10292ce29eeSNikita Popov; UNROLL-4: for.end.loopexit.unr-lcssa.loopexit: 10392ce29eeSNikita Popov; UNROLL-4-NEXT: [[ADD_LCSSA_PH_PH:%.*]] = phi i3 [ [[ADD_3]], [[FOR_BODY]] ] 10492ce29eeSNikita Popov; UNROLL-4-NEXT: [[INDVARS_IV_UNR_PH:%.*]] = phi i64 [ [[INDVARS_IV_NEXT_3]], [[FOR_BODY]] ] 10592ce29eeSNikita Popov; UNROLL-4-NEXT: [[SUM_02_UNR_PH:%.*]] = phi i3 [ [[ADD_3]], [[FOR_BODY]] ] 10692ce29eeSNikita Popov; UNROLL-4-NEXT: br label [[FOR_END_LOOPEXIT_UNR_LCSSA]] 10792ce29eeSNikita Popov; UNROLL-4: for.end.loopexit.unr-lcssa: 10892ce29eeSNikita Popov; UNROLL-4-NEXT: [[ADD_LCSSA_PH:%.*]] = phi i3 [ undef, [[FOR_BODY_PREHEADER]] ], [ [[ADD_LCSSA_PH_PH]], [[FOR_END_LOOPEXIT_UNR_LCSSA_LOOPEXIT]] ] 10992ce29eeSNikita Popov; UNROLL-4-NEXT: [[INDVARS_IV_UNR:%.*]] = phi i64 [ 0, [[FOR_BODY_PREHEADER]] ], [ [[INDVARS_IV_UNR_PH]], [[FOR_END_LOOPEXIT_UNR_LCSSA_LOOPEXIT]] ] 11092ce29eeSNikita Popov; UNROLL-4-NEXT: [[SUM_02_UNR:%.*]] = phi i3 [ 0, [[FOR_BODY_PREHEADER]] ], [ [[SUM_02_UNR_PH]], [[FOR_END_LOOPEXIT_UNR_LCSSA_LOOPEXIT]] ] 11192ce29eeSNikita Popov; UNROLL-4-NEXT: [[LCMP_MOD:%.*]] = icmp ne i3 [[XTRAITER]], 0 11292ce29eeSNikita Popov; UNROLL-4-NEXT: br i1 [[LCMP_MOD]], label [[FOR_BODY_EPIL_PREHEADER:%.*]], label [[FOR_END_LOOPEXIT:%.*]] 11392ce29eeSNikita Popov; UNROLL-4: for.body.epil.preheader: 11492ce29eeSNikita Popov; UNROLL-4-NEXT: br label [[FOR_BODY_EPIL:%.*]] 11592ce29eeSNikita Popov; UNROLL-4: for.body.epil: 11692ce29eeSNikita Popov; UNROLL-4-NEXT: [[INDVARS_IV_EPIL:%.*]] = phi i64 [ [[INDVARS_IV_NEXT_EPIL:%.*]], [[FOR_BODY_EPIL]] ], [ [[INDVARS_IV_UNR]], [[FOR_BODY_EPIL_PREHEADER]] ] 11792ce29eeSNikita Popov; UNROLL-4-NEXT: [[SUM_02_EPIL:%.*]] = phi i3 [ [[ADD_EPIL:%.*]], [[FOR_BODY_EPIL]] ], [ [[SUM_02_UNR]], [[FOR_BODY_EPIL_PREHEADER]] ] 11837ead201SPhilip Reames; UNROLL-4-NEXT: [[EPIL_ITER:%.*]] = phi i3 [ 0, [[FOR_BODY_EPIL_PREHEADER]] ], [ [[EPIL_ITER_NEXT:%.*]], [[FOR_BODY_EPIL]] ] 11992ce29eeSNikita Popov; UNROLL-4-NEXT: [[ARRAYIDX_EPIL:%.*]] = getelementptr inbounds i3, i3* [[A]], i64 [[INDVARS_IV_EPIL]] 12092ce29eeSNikita Popov; UNROLL-4-NEXT: [[TMP6:%.*]] = load i3, i3* [[ARRAYIDX_EPIL]], align 1 12192ce29eeSNikita Popov; UNROLL-4-NEXT: [[ADD_EPIL]] = add nsw i3 [[TMP6]], [[SUM_02_EPIL]] 12292ce29eeSNikita Popov; UNROLL-4-NEXT: [[INDVARS_IV_NEXT_EPIL]] = add i64 [[INDVARS_IV_EPIL]], 1 12392ce29eeSNikita Popov; UNROLL-4-NEXT: [[LFTR_WIDEIV_EPIL:%.*]] = trunc i64 [[INDVARS_IV_NEXT_EPIL]] to i3 12492ce29eeSNikita Popov; UNROLL-4-NEXT: [[EXITCOND_EPIL:%.*]] = icmp eq i3 [[LFTR_WIDEIV_EPIL]], [[N]] 12537ead201SPhilip Reames; UNROLL-4-NEXT: [[EPIL_ITER_NEXT]] = add i3 [[EPIL_ITER]], 1 12637ead201SPhilip Reames; UNROLL-4-NEXT: [[EPIL_ITER_CMP:%.*]] = icmp ne i3 [[EPIL_ITER_NEXT]], [[XTRAITER]] 12792ce29eeSNikita Popov; UNROLL-4-NEXT: br i1 [[EPIL_ITER_CMP]], label [[FOR_BODY_EPIL]], label [[FOR_END_LOOPEXIT_EPILOG_LCSSA:%.*]], !llvm.loop [[LOOP2:![0-9]+]] 12892ce29eeSNikita Popov; UNROLL-4: for.end.loopexit.epilog-lcssa: 12992ce29eeSNikita Popov; UNROLL-4-NEXT: [[ADD_LCSSA_PH1:%.*]] = phi i3 [ [[ADD_EPIL]], [[FOR_BODY_EPIL]] ] 13092ce29eeSNikita Popov; UNROLL-4-NEXT: br label [[FOR_END_LOOPEXIT]] 13192ce29eeSNikita Popov; UNROLL-4: for.end.loopexit: 13292ce29eeSNikita Popov; UNROLL-4-NEXT: [[ADD_LCSSA:%.*]] = phi i3 [ [[ADD_LCSSA_PH]], [[FOR_END_LOOPEXIT_UNR_LCSSA]] ], [ [[ADD_LCSSA_PH1]], [[FOR_END_LOOPEXIT_EPILOG_LCSSA]] ] 13392ce29eeSNikita Popov; UNROLL-4-NEXT: br label [[FOR_END]] 13492ce29eeSNikita Popov; UNROLL-4: for.end: 13592ce29eeSNikita Popov; UNROLL-4-NEXT: [[SUM_0_LCSSA:%.*]] = phi i3 [ 0, [[ENTRY:%.*]] ], [ [[ADD_LCSSA]], [[FOR_END_LOOPEXIT]] ] 13692ce29eeSNikita Popov; UNROLL-4-NEXT: ret i3 [[SUM_0_LCSSA]] 13792ce29eeSNikita Popov; 138cee313d2SEric Christopherentry: 139cee313d2SEric Christopher %cmp1 = icmp eq i3 %n, 0 140cee313d2SEric Christopher br i1 %cmp1, label %for.end, label %for.body 141cee313d2SEric Christopher 142cee313d2SEric Christopherfor.body: ; preds = %for.body, %entry 143cee313d2SEric Christopher %indvars.iv = phi i64 [ %indvars.iv.next, %for.body ], [ 0, %entry ] 144cee313d2SEric Christopher %sum.02 = phi i3 [ %add, %for.body ], [ 0, %entry ] 145cee313d2SEric Christopher %arrayidx = getelementptr inbounds i3, i3* %a, i64 %indvars.iv 146cee313d2SEric Christopher %0 = load i3, i3* %arrayidx 147cee313d2SEric Christopher %add = add nsw i3 %0, %sum.02 148cee313d2SEric Christopher %indvars.iv.next = add i64 %indvars.iv, 1 149cee313d2SEric Christopher %lftr.wideiv = trunc i64 %indvars.iv.next to i3 150cee313d2SEric Christopher %exitcond = icmp eq i3 %lftr.wideiv, %n 151cee313d2SEric Christopher br i1 %exitcond, label %for.end, label %for.body 152cee313d2SEric Christopher 153cee313d2SEric Christopherfor.end: ; preds = %for.body, %entry 154cee313d2SEric Christopher %sum.0.lcssa = phi i3 [ 0, %entry ], [ %add, %for.body ] 155cee313d2SEric Christopher ret i3 %sum.0.lcssa 156cee313d2SEric Christopher} 157