14178c153SChen Zheng; RUN: llc < %s -mtriple=powerpc64le-unknown-linux-gnu -verify-machineinstrs -mcpu=pwr8 | FileCheck %s --check-prefixes=CHECK,CHECK-PWR8
20724fea2SLei Huang
30724fea2SLei Huang; Verify that we do NOT generate the mtctr instruction for loop trip counts < 4
40724fea2SLei Huang; The latency of the mtctr is only justified if there are more than 4 comparisons that are removed as a result.
50724fea2SLei Huang
6*216be996SJinsong Ji@a = local_unnamed_addr global i32 0, align 4
7*216be996SJinsong Ji@b = local_unnamed_addr global i32 0, align 4
8*216be996SJinsong Ji@c = local_unnamed_addr global i32 0, align 4
9*216be996SJinsong Ji@d = local_unnamed_addr global i32 0, align 4
10*216be996SJinsong Ji@e = local_unnamed_addr global i32 0, align 4
11*216be996SJinsong Ji@f = local_unnamed_addr global i32 0, align 4
12*216be996SJinsong Ji@arr = local_unnamed_addr global [5 x i32] zeroinitializer, align 4
130724fea2SLei Huang
140724fea2SLei Huang; Function Attrs: norecurse nounwind readonly
150724fea2SLei Huangdefine signext i32 @testTripCount2(i32 signext %a) {
160724fea2SLei Huang
170724fea2SLei Huang; CHECK-LABEL: testTripCount2:
180724fea2SLei Huang; CHECK-NOT: mtctr
190724fea2SLei Huang; CHECK: blr
200724fea2SLei Huang
210724fea2SLei Huangentry:
220724fea2SLei Huang  br label %for.body
230724fea2SLei Huang
240724fea2SLei Huangfor.cond.cleanup:                                 ; preds = %for.body
250724fea2SLei Huang  ret i32 %add
260724fea2SLei Huang
270724fea2SLei Huangfor.body:                                         ; preds = %entry, %for.body
280724fea2SLei Huang  %indvars.iv = phi i64 [ 1, %entry ], [ %indvars.iv.next, %for.body ]
290724fea2SLei Huang  %Sum.05 = phi i32 [ 0, %entry ], [ %add, %for.body ]
300724fea2SLei Huang  %arrayidx = getelementptr inbounds [5 x i32], [5 x i32]* @arr, i64 0, i64 %indvars.iv
310724fea2SLei Huang  %0 = load i32, i32* %arrayidx, align 4
320724fea2SLei Huang  %add = add nsw i32 %0, %Sum.05
330724fea2SLei Huang  %indvars.iv.next = add nsw i64 %indvars.iv, -1
340724fea2SLei Huang  %tobool = icmp eq i64 %indvars.iv, 0
350724fea2SLei Huang  br i1 %tobool, label %for.cond.cleanup, label %for.body
360724fea2SLei Huang}
370724fea2SLei Huang
380724fea2SLei Huang; Function Attrs: norecurse nounwind readonly
390724fea2SLei Huangdefine signext i32 @testTripCount3(i32 signext %a) {
400724fea2SLei Huang
410724fea2SLei Huang; CHECK-LABEL: testTripCount3:
420724fea2SLei Huang; CHECK-NOT: mtctr
430724fea2SLei Huang; CHECK: blr
440724fea2SLei Huang
450724fea2SLei Huangentry:
460724fea2SLei Huang  br label %for.body
470724fea2SLei Huang
480724fea2SLei Huangfor.cond.cleanup:                                 ; preds = %for.body
490724fea2SLei Huang  ret i32 %add
500724fea2SLei Huang
510724fea2SLei Huangfor.body:                                         ; preds = %entry, %for.body
520724fea2SLei Huang  %indvars.iv = phi i64 [ 2, %entry ], [ %indvars.iv.next, %for.body ]
530724fea2SLei Huang  %Sum.05 = phi i32 [ 0, %entry ], [ %add, %for.body ]
540724fea2SLei Huang  %arrayidx = getelementptr inbounds [5 x i32], [5 x i32]* @arr, i64 0, i64 %indvars.iv
550724fea2SLei Huang  %0 = load i32, i32* %arrayidx, align 4
560724fea2SLei Huang  %add = add nsw i32 %0, %Sum.05
570724fea2SLei Huang  %indvars.iv.next = add nsw i64 %indvars.iv, -1
580724fea2SLei Huang  %tobool = icmp eq i64 %indvars.iv, 0
590724fea2SLei Huang  br i1 %tobool, label %for.cond.cleanup, label %for.body
600724fea2SLei Huang}
610724fea2SLei Huang
620724fea2SLei Huang; Function Attrs: norecurse nounwind readonly
630724fea2SLei Huang
640724fea2SLei Huangdefine signext i32 @testTripCount4(i32 signext %a) {
650724fea2SLei Huang
660724fea2SLei Huang; CHECK-LABEL: testTripCount4:
670724fea2SLei Huang; CHECK: mtctr
680724fea2SLei Huang; CHECK: bdnz
690724fea2SLei Huang
700724fea2SLei Huangentry:
710724fea2SLei Huang  br label %for.body
720724fea2SLei Huang
730724fea2SLei Huangfor.cond.cleanup:                                 ; preds = %for.body
740724fea2SLei Huang  ret i32 %add
750724fea2SLei Huang
760724fea2SLei Huangfor.body:                                         ; preds = %entry, %for.body
770724fea2SLei Huang  %indvars.iv = phi i64 [ 3, %entry ], [ %indvars.iv.next, %for.body ]
780724fea2SLei Huang  %Sum.05 = phi i32 [ 0, %entry ], [ %add, %for.body ]
790724fea2SLei Huang  %arrayidx = getelementptr inbounds [5 x i32], [5 x i32]* @arr, i64 0, i64 %indvars.iv
800724fea2SLei Huang  %0 = load i32, i32* %arrayidx, align 4
810724fea2SLei Huang  %add = add nsw i32 %0, %Sum.05
820724fea2SLei Huang  %indvars.iv.next = add nsw i64 %indvars.iv, -1
830724fea2SLei Huang  %tobool = icmp eq i64 %indvars.iv, 0
840724fea2SLei Huang  br i1 %tobool, label %for.cond.cleanup, label %for.body
850724fea2SLei Huang}
860724fea2SLei Huang
870724fea2SLei Huang; Function Attrs: norecurse nounwind
880724fea2SLei Huangdefine signext i32 @testTripCount2NonSmallLoop() {
890724fea2SLei Huang; CHECK-LABEL: testTripCount2NonSmallLoop:
90dfdccbb2SChen Zheng; CHECK-PWR8-NOT: mtctr
910724fea2SLei Huang; CHECK: blr
920724fea2SLei Huang
930724fea2SLei Huangentry:
940724fea2SLei Huang  %.pre = load i32, i32* @a, align 4
950724fea2SLei Huang  br label %for.body
960724fea2SLei Huang
970724fea2SLei Huangfor.body:                                         ; preds = %entry, %if.end
980724fea2SLei Huang  %0 = phi i32 [ %.pre, %entry ], [ %1, %if.end ]
990724fea2SLei Huang  %dec4 = phi i32 [ 1, %entry ], [ %dec, %if.end ]
1000724fea2SLei Huang  %b.03 = phi i8 [ 0, %entry ], [ %b.1, %if.end ]
1010724fea2SLei Huang  %tobool1 = icmp eq i32 %0, 0
1020724fea2SLei Huang  br i1 %tobool1, label %if.end, label %if.then
1030724fea2SLei Huang
1040724fea2SLei Huangif.then:                                          ; preds = %for.body
1050724fea2SLei Huang  store i32 2, i32* @a, align 4
1060724fea2SLei Huang  br label %if.end
1070724fea2SLei Huang
1080724fea2SLei Huangif.end:                                           ; preds = %for.body, %if.then
1090724fea2SLei Huang  %1 = phi i32 [ 2, %if.then ], [ 0, %for.body ]
1100724fea2SLei Huang  %b.1 = phi i8 [ 2, %if.then ], [ %b.03, %for.body ]
1110724fea2SLei Huang  %dec = add nsw i32 %dec4, -1
1120724fea2SLei Huang  %tobool = icmp eq i32 %dec4, 0
1130724fea2SLei Huang  br i1 %tobool, label %for.end, label %for.body
1140724fea2SLei Huang
1150724fea2SLei Huangfor.end:                                          ; preds = %if.end
1160724fea2SLei Huang  %conv = zext i8 %b.1 to i32
1170724fea2SLei Huang  ret i32 %conv
1180724fea2SLei Huang}
1190724fea2SLei Huang
1204178c153SChen Zhengdefine signext i32 @testTripCount5() {
1214178c153SChen Zheng; CHECK-LABEL: testTripCount5:
12219ce6719SChen Zheng; CHECK-PWR8-NOT: mtctr
1234178c153SChen Zheng
1244178c153SChen Zhengentry:
1254178c153SChen Zheng  %.prea = load i32, i32* @a, align 4
1264178c153SChen Zheng  %.preb = load i32, i32* @b, align 4
1274178c153SChen Zheng  %.prec = load i32, i32* @c, align 4
1284178c153SChen Zheng  %.pred = load i32, i32* @d, align 4
1294178c153SChen Zheng  %.pree = load i32, i32* @e, align 4
1304178c153SChen Zheng  %.pref = load i32, i32* @f, align 4
1314178c153SChen Zheng  br label %for.body
1324178c153SChen Zheng
1334178c153SChen Zhengfor.body:                                 ; preds = %entry, %for.body
1344178c153SChen Zheng  %indvars.iv = phi i64 [ 2, %entry ], [ %indvars.iv.next, %for.body ]
1354178c153SChen Zheng  %0 = phi i32 [ %.prea, %entry ], [ %6, %for.body ]
1364178c153SChen Zheng  %1 = phi i32 [ %.preb, %entry ], [ %7, %for.body ]
1374178c153SChen Zheng  %2 = phi i32 [ %.prec, %entry ], [ %8, %for.body ]
1384178c153SChen Zheng  %3 = phi i32 [ %.pred, %entry ], [ %9, %for.body ]
1394178c153SChen Zheng  %4 = phi i32 [ %.pree, %entry ], [ %10, %for.body ]
1404178c153SChen Zheng  %5 = phi i32 [ %.pref, %entry ], [ %11, %for.body ]
1414178c153SChen Zheng  %6 = add i32 %0, 1
1424178c153SChen Zheng  %7 = add i32 %1, 1
1434178c153SChen Zheng  %8 = add i32 %2, 1
1444178c153SChen Zheng  %9 = add i32 %3, 1
1454178c153SChen Zheng  %10 = add i32 %4, 1
1464178c153SChen Zheng  %11 = add i32 %5, 1
1474178c153SChen Zheng  %indvars.iv.next = add nsw i64 %indvars.iv, -1
1484178c153SChen Zheng  %tobool = icmp eq i64 %indvars.iv, 0
1494178c153SChen Zheng  br i1 %tobool, label %for.end, label %for.body
1504178c153SChen Zheng
1514178c153SChen Zhengfor.end:                                 ; preds = %for.body
1524178c153SChen Zheng  store i32 %6, i32* @a, align 4
1534178c153SChen Zheng  store i32 %7, i32* @b, align 4
1544178c153SChen Zheng  store i32 %8, i32* @c, align 4
1554178c153SChen Zheng  store i32 %9, i32* @d, align 4
1564178c153SChen Zheng  store i32 %10, i32* @e, align 4
1574178c153SChen Zheng  store i32 %11, i32* @f, align 4
1584178c153SChen Zheng  ret i32 0
1594178c153SChen Zheng}
1604178c153SChen Zheng
161