File: cttz.ll

package info (click to toggle)
llvm-toolchain-14 1%3A14.0.6-12
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,496,180 kB
  • sloc: cpp: 5,593,972; ansic: 986,872; asm: 585,869; python: 184,223; objc: 72,530; lisp: 31,119; f90: 27,793; javascript: 9,780; pascal: 9,762; sh: 9,482; perl: 7,468; ml: 5,432; awk: 3,523; makefile: 2,538; xml: 953; cs: 573; fortran: 567
file content (227 lines) | stat: -rw-r--r-- 8,878 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt -loop-idiom -mtriple=x86_64 -mcpu=core-avx2 < %s -S | FileCheck --check-prefix=ALL %s
; RUN: opt -loop-idiom -mtriple=x86_64 -mcpu=corei7 < %s -S | FileCheck --check-prefix=ALL %s

; Recognize CTTZ builtin pattern.
; Here it will replace the loop -
; assume builtin is always profitable.
;
; int cttz_zero_check(int n)
; {
;   int i = 0;
;   while(n) {
;     n <<= 1;
;     i++;
;   }
;   return i;
; }
;
define i32 @cttz_zero_check(i32 %n) {
; ALL-LABEL: @cttz_zero_check(
; ALL-NEXT:  entry:
; ALL-NEXT:    [[TOBOOL4:%.*]] = icmp eq i32 [[N:%.*]], 0
; ALL-NEXT:    br i1 [[TOBOOL4]], label [[WHILE_END:%.*]], label [[WHILE_BODY_PREHEADER:%.*]]
; ALL:       while.body.preheader:
; ALL-NEXT:    [[TMP0:%.*]] = call i32 @llvm.cttz.i32(i32 [[N]], i1 true)
; ALL-NEXT:    [[TMP1:%.*]] = sub i32 32, [[TMP0]]
; ALL-NEXT:    br label [[WHILE_BODY:%.*]]
; ALL:       while.body:
; ALL-NEXT:    [[TCPHI:%.*]] = phi i32 [ [[TMP1]], [[WHILE_BODY_PREHEADER]] ], [ [[TCDEC:%.*]], [[WHILE_BODY]] ]
; ALL-NEXT:    [[I_06:%.*]] = phi i32 [ [[INC:%.*]], [[WHILE_BODY]] ], [ 0, [[WHILE_BODY_PREHEADER]] ]
; ALL-NEXT:    [[N_ADDR_05:%.*]] = phi i32 [ [[SHL:%.*]], [[WHILE_BODY]] ], [ [[N]], [[WHILE_BODY_PREHEADER]] ]
; ALL-NEXT:    [[SHL]] = shl i32 [[N_ADDR_05]], 1
; ALL-NEXT:    [[INC]] = add nsw i32 [[I_06]], 1
; ALL-NEXT:    [[TCDEC]] = sub nsw i32 [[TCPHI]], 1
; ALL-NEXT:    [[TOBOOL:%.*]] = icmp eq i32 [[TCDEC]], 0
; ALL-NEXT:    br i1 [[TOBOOL]], label [[WHILE_END_LOOPEXIT:%.*]], label [[WHILE_BODY]]
; ALL:       while.end.loopexit:
; ALL-NEXT:    [[INC_LCSSA:%.*]] = phi i32 [ [[TMP1]], [[WHILE_BODY]] ]
; ALL-NEXT:    br label [[WHILE_END]]
; ALL:       while.end:
; ALL-NEXT:    [[I_0_LCSSA:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[INC_LCSSA]], [[WHILE_END_LOOPEXIT]] ]
; ALL-NEXT:    ret i32 [[I_0_LCSSA]]
;
entry:
  %tobool4 = icmp eq i32 %n, 0
  br i1 %tobool4, label %while.end, label %while.body.preheader

while.body.preheader:                             ; preds = %entry
  br label %while.body

while.body:                                       ; preds = %while.body.preheader, %while.body
  %i.06 = phi i32 [ %inc, %while.body ], [ 0, %while.body.preheader ]
  %n.addr.05 = phi i32 [ %shl, %while.body ], [ %n, %while.body.preheader ]
  %shl = shl i32 %n.addr.05, 1
  %inc = add nsw i32 %i.06, 1
  %tobool = icmp eq i32 %shl, 0
  br i1 %tobool, label %while.end.loopexit, label %while.body

while.end.loopexit:                               ; preds = %while.body
  br label %while.end

while.end:                                        ; preds = %while.end.loopexit, %entry
  %i.0.lcssa = phi i32 [ 0, %entry ], [ %inc, %while.end.loopexit ]
  ret i32 %i.0.lcssa
}

; Recognize CTTZ builtin pattern.
; Here it will replace the loop -
; assume builtin is always profitable.
;
; int cttz(int n)
; {
;   int i = 0;
;   while(n <<= 1) {
;     i++;
;   }
;   return i;
; }
;
define i32 @cttz(i32 %n) {
; ALL-LABEL: @cttz(
; ALL-NEXT:  entry:
; ALL-NEXT:    [[TMP0:%.*]] = shl i32 [[N:%.*]], 1
; ALL-NEXT:    [[TMP1:%.*]] = call i32 @llvm.cttz.i32(i32 [[TMP0]], i1 false)
; ALL-NEXT:    [[TMP2:%.*]] = sub i32 32, [[TMP1]]
; ALL-NEXT:    [[TMP3:%.*]] = add i32 [[TMP2]], 1
; ALL-NEXT:    br label [[WHILE_COND:%.*]]
; ALL:       while.cond:
; ALL-NEXT:    [[TCPHI:%.*]] = phi i32 [ [[TMP3]], [[ENTRY:%.*]] ], [ [[TCDEC:%.*]], [[WHILE_COND]] ]
; ALL-NEXT:    [[N_ADDR_0:%.*]] = phi i32 [ [[N]], [[ENTRY]] ], [ [[SHL:%.*]], [[WHILE_COND]] ]
; ALL-NEXT:    [[I_0:%.*]] = phi i32 [ 0, [[ENTRY]] ], [ [[INC:%.*]], [[WHILE_COND]] ]
; ALL-NEXT:    [[SHL]] = shl i32 [[N_ADDR_0]], 1
; ALL-NEXT:    [[TCDEC]] = sub nsw i32 [[TCPHI]], 1
; ALL-NEXT:    [[TOBOOL:%.*]] = icmp eq i32 [[TCDEC]], 0
; ALL-NEXT:    [[INC]] = add nsw i32 [[I_0]], 1
; ALL-NEXT:    br i1 [[TOBOOL]], label [[WHILE_END:%.*]], label [[WHILE_COND]]
; ALL:       while.end:
; ALL-NEXT:    [[I_0_LCSSA:%.*]] = phi i32 [ [[TMP2]], [[WHILE_COND]] ]
; ALL-NEXT:    ret i32 [[I_0_LCSSA]]
;
entry:
  br label %while.cond

while.cond:                                       ; preds = %while.cond, %entry
  %n.addr.0 = phi i32 [ %n, %entry ], [ %shl, %while.cond ]
  %i.0 = phi i32 [ 0, %entry ], [ %inc, %while.cond ]
  %shl = shl i32 %n.addr.0, 1
  %tobool = icmp eq i32 %shl, 0
  %inc = add nsw i32 %i.0, 1
  br i1 %tobool, label %while.end, label %while.cond

while.end:                                        ; preds = %while.cond
  ret i32 %i.0
}

; Recognize CTTZ builtin pattern.
; Here it will replace the loop -
; assume builtin is always profitable.
;
; int ctlz_decrement(int n)
; {
;   int i = 32;
;   while(n) {
;     n <<= 1;
;     i--;
;   }
;   return i;
; }
;
define i32 @cttz_decrement(i32 %n) {
; ALL-LABEL: @cttz_decrement(
; ALL-NEXT:  entry:
; ALL-NEXT:    [[TOBOOL4:%.*]] = icmp eq i32 [[N:%.*]], 0
; ALL-NEXT:    br i1 [[TOBOOL4]], label [[WHILE_END:%.*]], label [[WHILE_BODY_PREHEADER:%.*]]
; ALL:       while.body.preheader:
; ALL-NEXT:    [[TMP0:%.*]] = call i32 @llvm.cttz.i32(i32 [[N]], i1 true)
; ALL-NEXT:    [[TMP1:%.*]] = sub i32 32, [[TMP0]]
; ALL-NEXT:    [[TMP2:%.*]] = sub i32 32, [[TMP1]]
; ALL-NEXT:    br label [[WHILE_BODY:%.*]]
; ALL:       while.body:
; ALL-NEXT:    [[TCPHI:%.*]] = phi i32 [ [[TMP1]], [[WHILE_BODY_PREHEADER]] ], [ [[TCDEC:%.*]], [[WHILE_BODY]] ]
; ALL-NEXT:    [[I_06:%.*]] = phi i32 [ [[INC:%.*]], [[WHILE_BODY]] ], [ 32, [[WHILE_BODY_PREHEADER]] ]
; ALL-NEXT:    [[N_ADDR_05:%.*]] = phi i32 [ [[SHL:%.*]], [[WHILE_BODY]] ], [ [[N]], [[WHILE_BODY_PREHEADER]] ]
; ALL-NEXT:    [[SHL]] = shl i32 [[N_ADDR_05]], 1
; ALL-NEXT:    [[INC]] = add nsw i32 [[I_06]], -1
; ALL-NEXT:    [[TCDEC]] = sub nsw i32 [[TCPHI]], 1
; ALL-NEXT:    [[TOBOOL:%.*]] = icmp eq i32 [[TCDEC]], 0
; ALL-NEXT:    br i1 [[TOBOOL]], label [[WHILE_END_LOOPEXIT:%.*]], label [[WHILE_BODY]]
; ALL:       while.end.loopexit:
; ALL-NEXT:    [[INC_LCSSA:%.*]] = phi i32 [ [[TMP2]], [[WHILE_BODY]] ]
; ALL-NEXT:    br label [[WHILE_END]]
; ALL:       while.end:
; ALL-NEXT:    [[I_0_LCSSA:%.*]] = phi i32 [ 32, [[ENTRY:%.*]] ], [ [[INC_LCSSA]], [[WHILE_END_LOOPEXIT]] ]
; ALL-NEXT:    ret i32 [[I_0_LCSSA]]
;
entry:
  %tobool4 = icmp eq i32 %n, 0
  br i1 %tobool4, label %while.end, label %while.body.preheader

while.body.preheader:                             ; preds = %entry
  br label %while.body

while.body:                                       ; preds = %while.body.preheader, %while.body
  %i.06 = phi i32 [ %inc, %while.body ], [ 32, %while.body.preheader ]
  %n.addr.05 = phi i32 [ %shl, %while.body ], [ %n, %while.body.preheader ]
  %shl = shl i32 %n.addr.05, 1
  %inc = add nsw i32 %i.06, -1
  %tobool = icmp eq i32 %shl, 0
  br i1 %tobool, label %while.end.loopexit, label %while.body

while.end.loopexit:                               ; preds = %while.body
  br label %while.end

while.end:                                        ; preds = %while.end.loopexit, %entry
  %i.0.lcssa = phi i32 [ 32, %entry ], [ %inc, %while.end.loopexit ]
  ret i32 %i.0.lcssa
}

; Recognize CTTZ builtin pattern.
; Here it will replace the loop -
; assume builtin is always profitable.
;
; int cttz_shl_decrement(int n)
; {
;   int i = 31;
;   while(n <<= 1) {
;     i--;
;   }
;   return i;
; }
;
define i32 @cttz_shl_decrement(i32 %n) {
; ALL-LABEL: @cttz_shl_decrement(
; ALL-NEXT:  entry:
; ALL-NEXT:    [[TMP0:%.*]] = shl i32 [[N:%.*]], 1
; ALL-NEXT:    [[TMP1:%.*]] = call i32 @llvm.cttz.i32(i32 [[TMP0]], i1 false)
; ALL-NEXT:    [[TMP2:%.*]] = sub i32 32, [[TMP1]]
; ALL-NEXT:    [[TMP3:%.*]] = add i32 [[TMP2]], 1
; ALL-NEXT:    [[TMP4:%.*]] = sub i32 31, [[TMP2]]
; ALL-NEXT:    br label [[WHILE_COND:%.*]]
; ALL:       while.cond:
; ALL-NEXT:    [[TCPHI:%.*]] = phi i32 [ [[TMP3]], [[ENTRY:%.*]] ], [ [[TCDEC:%.*]], [[WHILE_COND]] ]
; ALL-NEXT:    [[N_ADDR_0:%.*]] = phi i32 [ [[N]], [[ENTRY]] ], [ [[SHL:%.*]], [[WHILE_COND]] ]
; ALL-NEXT:    [[I_0:%.*]] = phi i32 [ 31, [[ENTRY]] ], [ [[INC:%.*]], [[WHILE_COND]] ]
; ALL-NEXT:    [[SHL]] = shl i32 [[N_ADDR_0]], 1
; ALL-NEXT:    [[TCDEC]] = sub nsw i32 [[TCPHI]], 1
; ALL-NEXT:    [[TOBOOL:%.*]] = icmp eq i32 [[TCDEC]], 0
; ALL-NEXT:    [[INC]] = add nsw i32 [[I_0]], -1
; ALL-NEXT:    br i1 [[TOBOOL]], label [[WHILE_END:%.*]], label [[WHILE_COND]]
; ALL:       while.end:
; ALL-NEXT:    [[I_0_LCSSA:%.*]] = phi i32 [ [[TMP4]], [[WHILE_COND]] ]
; ALL-NEXT:    ret i32 [[I_0_LCSSA]]
;
entry:
  br label %while.cond

while.cond:                                       ; preds = %while.cond, %entry
  %n.addr.0 = phi i32 [ %n, %entry ], [ %shl, %while.cond ]
  %i.0 = phi i32 [ 31, %entry ], [ %inc, %while.cond ]
  %shl = shl i32 %n.addr.0, 1
  %tobool = icmp eq i32 %shl, 0
  %inc = add nsw i32 %i.0, -1
  br i1 %tobool, label %while.end, label %while.cond

while.end:                                        ; preds = %while.cond
  ret i32 %i.0
}