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
4 ; Recognize CTTZ builtin pattern.
5 ; Here it will replace the loop -
6 ; assume builtin is always profitable.
8 ; int cttz_zero_check(int n)
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
22 ; Function Attrs: norecurse nounwind readnone uwtable
23 define i32 @cttz_zero_check(i32 %n) {
25 %tobool4 = icmp eq i32 %n, 0
26 br i1 %tobool4, label %while.end, label %while.body.preheader
28 while.body.preheader: ; preds = %entry
31 while.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
39 while.end.loopexit: ; preds = %while.body
42 while.end: ; preds = %while.end.loopexit, %entry
43 %i.0.lcssa = phi i32 [ 0, %entry ], [ %inc, %while.end.loopexit ]
47 ; Recognize CTTZ builtin pattern.
48 ; Here it will replace the loop -
49 ; assume builtin is always profitable.
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
66 ; Function Attrs: norecurse nounwind readnone uwtable
67 define i32 @cttz(i32 %n) {
71 while.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
79 while.end: ; preds = %while.cond