1 ; RUN: opt -loop-idiom -mtriple=r600-- -mcpu=SI -S < %s | FileCheck %s 2 3 ; Mostly copied from x86 version. 4 5 ;To recognize this pattern: 6 ;int popcount(unsigned long long a) { 7 ; int c = 0; 8 ; while (a) { 9 ; c++; 10 ; a &= a - 1; 11 ; } 12 ; return c; 13 ;} 14 ; 15 16 ; CHECK-LABEL: @popcount_i64 17 ; CHECK: entry 18 ; CHECK: llvm.ctpop.i64 19 ; CHECK: ret 20 define i32 @popcount_i64(i64 %a) nounwind uwtable readnone ssp { 21 entry: 22 %tobool3 = icmp eq i64 %a, 0 23 br i1 %tobool3, label %while.end, label %while.body 24 25 while.body: ; preds = %entry, %while.body 26 %c.05 = phi i32 [ %inc, %while.body ], [ 0, %entry ] 27 %a.addr.04 = phi i64 [ %and, %while.body ], [ %a, %entry ] 28 %inc = add nsw i32 %c.05, 1 29 %sub = add i64 %a.addr.04, -1 30 %and = and i64 %sub, %a.addr.04 31 %tobool = icmp eq i64 %and, 0 32 br i1 %tobool, label %while.end, label %while.body 33 34 while.end: ; preds = %while.body, %entry 35 %c.0.lcssa = phi i32 [ 0, %entry ], [ %inc, %while.body ] 36 ret i32 %c.0.lcssa 37 } 38 39 ; CHECK-LABEL: @popcount_i32 40 ; CHECK: entry 41 ; CHECK: llvm.ctpop.i32 42 ; CHECK: ret 43 define i32 @popcount_i32(i32 %a) nounwind uwtable readnone ssp { 44 entry: 45 %tobool3 = icmp eq i32 %a, 0 46 br i1 %tobool3, label %while.end, label %while.body 47 48 while.body: ; preds = %entry, %while.body 49 %c.05 = phi i32 [ %inc, %while.body ], [ 0, %entry ] 50 %a.addr.04 = phi i32 [ %and, %while.body ], [ %a, %entry ] 51 %inc = add nsw i32 %c.05, 1 52 %sub = add i32 %a.addr.04, -1 53 %and = and i32 %sub, %a.addr.04 54 %tobool = icmp eq i32 %and, 0 55 br i1 %tobool, label %while.end, label %while.body 56 57 while.end: ; preds = %while.body, %entry 58 %c.0.lcssa = phi i32 [ 0, %entry ], [ %inc, %while.body ] 59 ret i32 %c.0.lcssa 60 } 61 62 ; To recognize this pattern: 63 ;int popcount(unsigned long long a, int mydata1, int mydata2) { 64 ; int c = 0; 65 ; while (a) { 66 ; c++; 67 ; a &= a - 1; 68 ; mydata1 *= c; 69 ; mydata2 *= (int)a; 70 ; } 71 ; return c + mydata1 + mydata2; 72 ;} 73 74 ; CHECK-LABEL: @popcount2 75 ; CHECK: entry 76 ; CHECK: llvm.ctpop.i64 77 ; CHECK: ret 78 define i32 @popcount2(i64 %a, i32 %mydata1, i32 %mydata2) nounwind uwtable readnone ssp { 79 entry: 80 %tobool9 = icmp eq i64 %a, 0 81 br i1 %tobool9, label %while.end, label %while.body 82 83 while.body: ; preds = %entry, %while.body 84 %c.013 = phi i32 [ %inc, %while.body ], [ 0, %entry ] 85 %mydata2.addr.012 = phi i32 [ %mul1, %while.body ], [ %mydata2, %entry ] 86 %mydata1.addr.011 = phi i32 [ %mul, %while.body ], [ %mydata1, %entry ] 87 %a.addr.010 = phi i64 [ %and, %while.body ], [ %a, %entry ] 88 %inc = add nsw i32 %c.013, 1 89 %sub = add i64 %a.addr.010, -1 90 %and = and i64 %sub, %a.addr.010 91 %mul = mul nsw i32 %inc, %mydata1.addr.011 92 %conv = trunc i64 %and to i32 93 %mul1 = mul nsw i32 %conv, %mydata2.addr.012 94 %tobool = icmp eq i64 %and, 0 95 br i1 %tobool, label %while.end, label %while.body 96 97 while.end: ; preds = %while.body, %entry 98 %c.0.lcssa = phi i32 [ 0, %entry ], [ %inc, %while.body ] 99 %mydata2.addr.0.lcssa = phi i32 [ %mydata2, %entry ], [ %mul1, %while.body ] 100 %mydata1.addr.0.lcssa = phi i32 [ %mydata1, %entry ], [ %mul, %while.body ] 101 %add = add i32 %mydata2.addr.0.lcssa, %mydata1.addr.0.lcssa 102 %add2 = add i32 %add, %c.0.lcssa 103 ret i32 %add2 104 } 105