1; RUN: opt -loop-idiom -mtriple=x86_64 -mcpu=core-avx2 < %s -S | FileCheck --check-prefix=ALL %s 2; RUN: opt -loop-idiom -mtriple=x86_64 -mcpu=corei7 < %s -S | FileCheck --check-prefix=ALL %s 3 4; Recognize CTTZ builtin pattern. 5; Here it will replace the loop - 6; assume builtin is always profitable. 7; 8; int cttz_zero_check(int n) 9; { 10; int i = 0; 11; while(n) { 12; n <<= 1; 13; i++; 14; } 15; return i; 16; } 17; 18; ALL-LABEL: @cttz_zero_check 19; ALL: %0 = call i32 @llvm.cttz.i32(i32 %n, i1 true) 20; ALL-NEXT: %1 = sub i32 32, %0 21; 22; Function Attrs: norecurse nounwind readnone uwtable 23define i32 @cttz_zero_check(i32 %n) { 24entry: 25 %tobool4 = icmp eq i32 %n, 0 26 br i1 %tobool4, label %while.end, label %while.body.preheader 27 28while.body.preheader: ; preds = %entry 29 br label %while.body 30 31while.body: ; preds = %while.body.preheader, %while.body 32 %i.06 = phi i32 [ %inc, %while.body ], [ 0, %while.body.preheader ] 33 %n.addr.05 = phi i32 [ %shl, %while.body ], [ %n, %while.body.preheader ] 34 %shl = shl i32 %n.addr.05, 1 35 %inc = add nsw i32 %i.06, 1 36 %tobool = icmp eq i32 %shl, 0 37 br i1 %tobool, label %while.end.loopexit, label %while.body 38 39while.end.loopexit: ; preds = %while.body 40 br label %while.end 41 42while.end: ; preds = %while.end.loopexit, %entry 43 %i.0.lcssa = phi i32 [ 0, %entry ], [ %inc, %while.end.loopexit ] 44 ret i32 %i.0.lcssa 45} 46 47; Recognize CTTZ builtin pattern. 48; Here it will replace the loop - 49; assume builtin is always profitable. 50; 51; int cttz(int n) 52; { 53; int i = 0; 54; while(n <<= 1) { 55; i++; 56; } 57; return i; 58; } 59; 60; ALL-LABEL: @cttz 61; ALL: %0 = shl i32 %n, 1 62; ALL-NEXT: %1 = call i32 @llvm.cttz.i32(i32 %0, i1 false) 63; ALL-NEXT: %2 = sub i32 32, %1 64; ALL-NEXT: %3 = add i32 %2, 1 65; 66; Function Attrs: norecurse nounwind readnone uwtable 67define i32 @cttz(i32 %n) { 68entry: 69 br label %while.cond 70 71while.cond: ; preds = %while.cond, %entry 72 %n.addr.0 = phi i32 [ %n, %entry ], [ %shl, %while.cond ] 73 %i.0 = phi i32 [ 0, %entry ], [ %inc, %while.cond ] 74 %shl = shl i32 %n.addr.0, 1 75 %tobool = icmp eq i32 %shl, 0 76 %inc = add nsw i32 %i.0, 1 77 br i1 %tobool, label %while.end, label %while.cond 78 79while.end: ; preds = %while.cond 80 ret i32 %i.0 81} 82 83