1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt -loop-idiom -mtriple=x86_64 -mcpu=core-avx2 < %s -S | FileCheck --check-prefix=ALL %s
3 ; RUN: opt -loop-idiom -mtriple=x86_64 -mcpu=corei7 < %s -S | FileCheck --check-prefix=ALL %s
5 ; Recognize CTTZ builtin pattern.
6 ; Here it will replace the loop -
7 ; assume builtin is always profitable.
9 ; int cttz_zero_check(int n)
19 define i32 @cttz_zero_check(i32 %n) {
20 ; ALL-LABEL: @cttz_zero_check(
22 ; ALL-NEXT: [[TOBOOL4:%.*]] = icmp eq i32 [[N:%.*]], 0
23 ; ALL-NEXT: br i1 [[TOBOOL4]], label [[WHILE_END:%.*]], label [[WHILE_BODY_PREHEADER:%.*]]
24 ; ALL: while.body.preheader:
25 ; ALL-NEXT: [[TMP0:%.*]] = call i32 @llvm.cttz.i32(i32 [[N]], i1 true)
26 ; ALL-NEXT: [[TMP1:%.*]] = sub i32 32, [[TMP0]]
27 ; ALL-NEXT: br label [[WHILE_BODY:%.*]]
29 ; ALL-NEXT: [[TCPHI:%.*]] = phi i32 [ [[TMP1]], [[WHILE_BODY_PREHEADER]] ], [ [[TCDEC:%.*]], [[WHILE_BODY]] ]
30 ; ALL-NEXT: [[I_06:%.*]] = phi i32 [ [[INC:%.*]], [[WHILE_BODY]] ], [ 0, [[WHILE_BODY_PREHEADER]] ]
31 ; ALL-NEXT: [[N_ADDR_05:%.*]] = phi i32 [ [[SHL:%.*]], [[WHILE_BODY]] ], [ [[N]], [[WHILE_BODY_PREHEADER]] ]
32 ; ALL-NEXT: [[SHL]] = shl i32 [[N_ADDR_05]], 1
33 ; ALL-NEXT: [[INC]] = add nsw i32 [[I_06]], 1
34 ; ALL-NEXT: [[TCDEC]] = sub nsw i32 [[TCPHI]], 1
35 ; ALL-NEXT: [[TOBOOL:%.*]] = icmp eq i32 [[TCDEC]], 0
36 ; ALL-NEXT: br i1 [[TOBOOL]], label [[WHILE_END_LOOPEXIT:%.*]], label [[WHILE_BODY]]
37 ; ALL: while.end.loopexit:
38 ; ALL-NEXT: [[INC_LCSSA:%.*]] = phi i32 [ [[TMP1]], [[WHILE_BODY]] ]
39 ; ALL-NEXT: br label [[WHILE_END]]
41 ; ALL-NEXT: [[I_0_LCSSA:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[INC_LCSSA]], [[WHILE_END_LOOPEXIT]] ]
42 ; ALL-NEXT: ret i32 [[I_0_LCSSA]]
45 %tobool4 = icmp eq i32 %n, 0
46 br i1 %tobool4, label %while.end, label %while.body.preheader
48 while.body.preheader: ; preds = %entry
51 while.body: ; preds = %while.body.preheader, %while.body
52 %i.06 = phi i32 [ %inc, %while.body ], [ 0, %while.body.preheader ]
53 %n.addr.05 = phi i32 [ %shl, %while.body ], [ %n, %while.body.preheader ]
54 %shl = shl i32 %n.addr.05, 1
55 %inc = add nsw i32 %i.06, 1
56 %tobool = icmp eq i32 %shl, 0
57 br i1 %tobool, label %while.end.loopexit, label %while.body
59 while.end.loopexit: ; preds = %while.body
62 while.end: ; preds = %while.end.loopexit, %entry
63 %i.0.lcssa = phi i32 [ 0, %entry ], [ %inc, %while.end.loopexit ]
67 ; Recognize CTTZ builtin pattern.
68 ; Here it will replace the loop -
69 ; assume builtin is always profitable.
80 define i32 @cttz(i32 %n) {
83 ; ALL-NEXT: [[TMP0:%.*]] = shl i32 [[N:%.*]], 1
84 ; ALL-NEXT: [[TMP1:%.*]] = call i32 @llvm.cttz.i32(i32 [[TMP0]], i1 false)
85 ; ALL-NEXT: [[TMP2:%.*]] = sub i32 32, [[TMP1]]
86 ; ALL-NEXT: [[TMP3:%.*]] = add i32 [[TMP2]], 1
87 ; ALL-NEXT: br label [[WHILE_COND:%.*]]
89 ; ALL-NEXT: [[TCPHI:%.*]] = phi i32 [ [[TMP3]], [[ENTRY:%.*]] ], [ [[TCDEC:%.*]], [[WHILE_COND]] ]
90 ; ALL-NEXT: [[N_ADDR_0:%.*]] = phi i32 [ [[N]], [[ENTRY]] ], [ [[SHL:%.*]], [[WHILE_COND]] ]
91 ; ALL-NEXT: [[I_0:%.*]] = phi i32 [ 0, [[ENTRY]] ], [ [[INC:%.*]], [[WHILE_COND]] ]
92 ; ALL-NEXT: [[SHL]] = shl i32 [[N_ADDR_0]], 1
93 ; ALL-NEXT: [[TCDEC]] = sub nsw i32 [[TCPHI]], 1
94 ; ALL-NEXT: [[TOBOOL:%.*]] = icmp eq i32 [[TCDEC]], 0
95 ; ALL-NEXT: [[INC]] = add nsw i32 [[I_0]], 1
96 ; ALL-NEXT: br i1 [[TOBOOL]], label [[WHILE_END:%.*]], label [[WHILE_COND]]
98 ; ALL-NEXT: [[I_0_LCSSA:%.*]] = phi i32 [ [[TMP2]], [[WHILE_COND]] ]
99 ; ALL-NEXT: ret i32 [[I_0_LCSSA]]
104 while.cond: ; preds = %while.cond, %entry
105 %n.addr.0 = phi i32 [ %n, %entry ], [ %shl, %while.cond ]
106 %i.0 = phi i32 [ 0, %entry ], [ %inc, %while.cond ]
107 %shl = shl i32 %n.addr.0, 1
108 %tobool = icmp eq i32 %shl, 0
109 %inc = add nsw i32 %i.0, 1
110 br i1 %tobool, label %while.end, label %while.cond
112 while.end: ; preds = %while.cond
116 ; Recognize CTTZ builtin pattern.
117 ; Here it will replace the loop -
118 ; assume builtin is always profitable.
120 ; int ctlz_decrement(int n)
130 define i32 @cttz_decrement(i32 %n) {
131 ; ALL-LABEL: @cttz_decrement(
133 ; ALL-NEXT: [[TOBOOL4:%.*]] = icmp eq i32 [[N:%.*]], 0
134 ; ALL-NEXT: br i1 [[TOBOOL4]], label [[WHILE_END:%.*]], label [[WHILE_BODY_PREHEADER:%.*]]
135 ; ALL: while.body.preheader:
136 ; ALL-NEXT: [[TMP0:%.*]] = call i32 @llvm.cttz.i32(i32 [[N]], i1 true)
137 ; ALL-NEXT: [[TMP1:%.*]] = sub i32 32, [[TMP0]]
138 ; ALL-NEXT: [[TMP2:%.*]] = sub i32 32, [[TMP1]]
139 ; ALL-NEXT: br label [[WHILE_BODY:%.*]]
141 ; ALL-NEXT: [[TCPHI:%.*]] = phi i32 [ [[TMP1]], [[WHILE_BODY_PREHEADER]] ], [ [[TCDEC:%.*]], [[WHILE_BODY]] ]
142 ; ALL-NEXT: [[I_06:%.*]] = phi i32 [ [[INC:%.*]], [[WHILE_BODY]] ], [ 32, [[WHILE_BODY_PREHEADER]] ]
143 ; ALL-NEXT: [[N_ADDR_05:%.*]] = phi i32 [ [[SHL:%.*]], [[WHILE_BODY]] ], [ [[N]], [[WHILE_BODY_PREHEADER]] ]
144 ; ALL-NEXT: [[SHL]] = shl i32 [[N_ADDR_05]], 1
145 ; ALL-NEXT: [[INC]] = add nsw i32 [[I_06]], -1
146 ; ALL-NEXT: [[TCDEC]] = sub nsw i32 [[TCPHI]], 1
147 ; ALL-NEXT: [[TOBOOL:%.*]] = icmp eq i32 [[TCDEC]], 0
148 ; ALL-NEXT: br i1 [[TOBOOL]], label [[WHILE_END_LOOPEXIT:%.*]], label [[WHILE_BODY]]
149 ; ALL: while.end.loopexit:
150 ; ALL-NEXT: [[INC_LCSSA:%.*]] = phi i32 [ [[TMP2]], [[WHILE_BODY]] ]
151 ; ALL-NEXT: br label [[WHILE_END]]
153 ; ALL-NEXT: [[I_0_LCSSA:%.*]] = phi i32 [ 32, [[ENTRY:%.*]] ], [ [[INC_LCSSA]], [[WHILE_END_LOOPEXIT]] ]
154 ; ALL-NEXT: ret i32 [[I_0_LCSSA]]
157 %tobool4 = icmp eq i32 %n, 0
158 br i1 %tobool4, label %while.end, label %while.body.preheader
160 while.body.preheader: ; preds = %entry
163 while.body: ; preds = %while.body.preheader, %while.body
164 %i.06 = phi i32 [ %inc, %while.body ], [ 32, %while.body.preheader ]
165 %n.addr.05 = phi i32 [ %shl, %while.body ], [ %n, %while.body.preheader ]
166 %shl = shl i32 %n.addr.05, 1
167 %inc = add nsw i32 %i.06, -1
168 %tobool = icmp eq i32 %shl, 0
169 br i1 %tobool, label %while.end.loopexit, label %while.body
171 while.end.loopexit: ; preds = %while.body
174 while.end: ; preds = %while.end.loopexit, %entry
175 %i.0.lcssa = phi i32 [ 32, %entry ], [ %inc, %while.end.loopexit ]
179 ; Recognize CTTZ builtin pattern.
180 ; Here it will replace the loop -
181 ; assume builtin is always profitable.
183 ; int cttz_shl_decrement(int n)
192 define i32 @cttz_shl_decrement(i32 %n) {
193 ; ALL-LABEL: @cttz_shl_decrement(
195 ; ALL-NEXT: [[TMP0:%.*]] = shl i32 [[N:%.*]], 1
196 ; ALL-NEXT: [[TMP1:%.*]] = call i32 @llvm.cttz.i32(i32 [[TMP0]], i1 false)
197 ; ALL-NEXT: [[TMP2:%.*]] = sub i32 32, [[TMP1]]
198 ; ALL-NEXT: [[TMP3:%.*]] = add i32 [[TMP2]], 1
199 ; ALL-NEXT: [[TMP4:%.*]] = sub i32 31, [[TMP2]]
200 ; ALL-NEXT: br label [[WHILE_COND:%.*]]
202 ; ALL-NEXT: [[TCPHI:%.*]] = phi i32 [ [[TMP3]], [[ENTRY:%.*]] ], [ [[TCDEC:%.*]], [[WHILE_COND]] ]
203 ; ALL-NEXT: [[N_ADDR_0:%.*]] = phi i32 [ [[N]], [[ENTRY]] ], [ [[SHL:%.*]], [[WHILE_COND]] ]
204 ; ALL-NEXT: [[I_0:%.*]] = phi i32 [ 31, [[ENTRY]] ], [ [[INC:%.*]], [[WHILE_COND]] ]
205 ; ALL-NEXT: [[SHL]] = shl i32 [[N_ADDR_0]], 1
206 ; ALL-NEXT: [[TCDEC]] = sub nsw i32 [[TCPHI]], 1
207 ; ALL-NEXT: [[TOBOOL:%.*]] = icmp eq i32 [[TCDEC]], 0
208 ; ALL-NEXT: [[INC]] = add nsw i32 [[I_0]], -1
209 ; ALL-NEXT: br i1 [[TOBOOL]], label [[WHILE_END:%.*]], label [[WHILE_COND]]
211 ; ALL-NEXT: [[I_0_LCSSA:%.*]] = phi i32 [ [[TMP4]], [[WHILE_COND]] ]
212 ; ALL-NEXT: ret i32 [[I_0_LCSSA]]
217 while.cond: ; preds = %while.cond, %entry
218 %n.addr.0 = phi i32 [ %n, %entry ], [ %shl, %while.cond ]
219 %i.0 = phi i32 [ 31, %entry ], [ %inc, %while.cond ]
220 %shl = shl i32 %n.addr.0, 1
221 %tobool = icmp eq i32 %shl, 0
222 %inc = add nsw i32 %i.0, -1
223 br i1 %tobool, label %while.end, label %while.cond
225 while.end: ; preds = %while.cond