]>
Commit | Line | Data |
---|---|---|
20f06221 | 1 | /* Analysis Utilities for Loop Vectorization. |
818ab71a | 2 | Copyright (C) 2006-2016 Free Software Foundation, Inc. |
20f06221 DN |
3 | Contributed by Dorit Nuzman <dorit@il.ibm.com> |
4 | ||
5 | This file is part of GCC. | |
6 | ||
7 | GCC is free software; you can redistribute it and/or modify it under | |
8 | the terms of the GNU General Public License as published by the Free | |
9dcd6f09 | 9 | Software Foundation; either version 3, or (at your option) any later |
20f06221 DN |
10 | version. |
11 | ||
12 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY | |
13 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 | for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
9dcd6f09 NC |
18 | along with GCC; see the file COPYING3. If not see |
19 | <http://www.gnu.org/licenses/>. */ | |
20f06221 DN |
20 | |
21 | #include "config.h" | |
22 | #include "system.h" | |
23 | #include "coretypes.h" | |
c7131fb2 | 24 | #include "backend.h" |
957060b5 | 25 | #include "rtl.h" |
20f06221 | 26 | #include "tree.h" |
c7131fb2 | 27 | #include "gimple.h" |
c7131fb2 | 28 | #include "ssa.h" |
957060b5 AM |
29 | #include "expmed.h" |
30 | #include "optabs-tree.h" | |
31 | #include "insn-config.h" | |
32 | #include "recog.h" /* FIXME: for insn_data */ | |
40e23961 | 33 | #include "fold-const.h" |
d8a2d370 | 34 | #include "stor-layout.h" |
2fb9a547 | 35 | #include "tree-eh.h" |
45b0be94 | 36 | #include "gimplify.h" |
5be5c238 | 37 | #include "gimple-iterator.h" |
20f06221 | 38 | #include "cfgloop.h" |
20f06221 | 39 | #include "tree-vectorizer.h" |
7ee2468b | 40 | #include "dumpfile.h" |
9b2b7279 | 41 | #include "builtins.h" |
b4e5bc47 | 42 | #include "internal-fn.h" |
7a31e5ef | 43 | #include "case-cfn-macros.h" |
20f06221 | 44 | |
20f06221 | 45 | /* Pattern recognition functions */ |
355fe088 | 46 | static gimple *vect_recog_widen_sum_pattern (vec<gimple *> *, tree *, |
51312233 | 47 | tree *); |
355fe088 | 48 | static gimple *vect_recog_widen_mult_pattern (vec<gimple *> *, tree *, |
51312233 | 49 | tree *); |
355fe088 | 50 | static gimple *vect_recog_dot_prod_pattern (vec<gimple *> *, tree *, |
51312233 | 51 | tree *); |
355fe088 | 52 | static gimple *vect_recog_sad_pattern (vec<gimple *> *, tree *, |
79d652a5 | 53 | tree *); |
355fe088 TS |
54 | static gimple *vect_recog_pow_pattern (vec<gimple *> *, tree *, tree *); |
55 | static gimple *vect_recog_over_widening_pattern (vec<gimple *> *, tree *, | |
1107f3ae | 56 | tree *); |
355fe088 | 57 | static gimple *vect_recog_widen_shift_pattern (vec<gimple *> *, |
36ba4aae | 58 | tree *, tree *); |
355fe088 TS |
59 | static gimple *vect_recog_rotate_pattern (vec<gimple *> *, tree *, tree *); |
60 | static gimple *vect_recog_vector_vector_shift_pattern (vec<gimple *> *, | |
732a0ad3 | 61 | tree *, tree *); |
355fe088 | 62 | static gimple *vect_recog_divmod_pattern (vec<gimple *> *, |
079c527f | 63 | tree *, tree *); |
47486460 | 64 | |
355fe088 | 65 | static gimple *vect_recog_mult_pattern (vec<gimple *> *, |
47486460 VK |
66 | tree *, tree *); |
67 | ||
355fe088 | 68 | static gimple *vect_recog_mixed_size_cond_pattern (vec<gimple *> *, |
69d2aade | 69 | tree *, tree *); |
355fe088 | 70 | static gimple *vect_recog_bool_pattern (vec<gimple *> *, tree *, tree *); |
e6f5c25d | 71 | static gimple *vect_recog_mask_conversion_pattern (vec<gimple *> *, tree *, tree *); |
0bee0ef9 RB |
72 | |
73 | struct vect_recog_func | |
74 | { | |
75 | vect_recog_func_ptr fn; | |
76 | const char *name; | |
77 | }; | |
5b723b68 RB |
78 | |
79 | /* Note that ordering matters - the first pattern matching on a stmt | |
80 | is taken which means usually the more complex one needs to preceed | |
81 | the less comples onex (widen_sum only after dot_prod or sad for example). */ | |
0bee0ef9 RB |
82 | static vect_recog_func vect_vect_recog_func_ptrs[NUM_PATTERNS] = { |
83 | { vect_recog_widen_mult_pattern, "widen_mult" }, | |
0bee0ef9 RB |
84 | { vect_recog_dot_prod_pattern, "dot_prod" }, |
85 | { vect_recog_sad_pattern, "sad" }, | |
5b723b68 | 86 | { vect_recog_widen_sum_pattern, "widen_sum" }, |
0bee0ef9 RB |
87 | { vect_recog_pow_pattern, "pow" }, |
88 | { vect_recog_widen_shift_pattern, "widen_shift" }, | |
89 | { vect_recog_over_widening_pattern, "over_widening" }, | |
90 | { vect_recog_rotate_pattern, "rotate" }, | |
91 | { vect_recog_vector_vector_shift_pattern, "vector_vector_shift" }, | |
92 | { vect_recog_divmod_pattern, "divmod" }, | |
93 | { vect_recog_mult_pattern, "mult" }, | |
94 | { vect_recog_mixed_size_cond_pattern, "mixed_size_cond" }, | |
95 | { vect_recog_bool_pattern, "bool" }, | |
96 | { vect_recog_mask_conversion_pattern, "mask_conversion" } | |
97 | }; | |
20f06221 | 98 | |
083481d8 | 99 | static inline void |
355fe088 | 100 | append_pattern_def_seq (stmt_vec_info stmt_info, gimple *stmt) |
083481d8 | 101 | { |
a1a6c5b2 JJ |
102 | gimple_seq_add_stmt_without_update (&STMT_VINFO_PATTERN_DEF_SEQ (stmt_info), |
103 | stmt); | |
083481d8 JJ |
104 | } |
105 | ||
106 | static inline void | |
355fe088 | 107 | new_pattern_def_seq (stmt_vec_info stmt_info, gimple *stmt) |
083481d8 JJ |
108 | { |
109 | STMT_VINFO_PATTERN_DEF_SEQ (stmt_info) = NULL; | |
110 | append_pattern_def_seq (stmt_info, stmt); | |
111 | } | |
112 | ||
f71cf56a UW |
113 | /* Check whether STMT2 is in the same loop or basic block as STMT1. |
114 | Which of the two applies depends on whether we're currently doing | |
115 | loop-based or basic-block-based vectorization, as determined by | |
116 | the vinfo_for_stmt for STMT1 (which must be defined). | |
117 | ||
118 | If this returns true, vinfo_for_stmt for STMT2 is guaranteed | |
119 | to be defined as well. */ | |
120 | ||
121 | static bool | |
355fe088 | 122 | vect_same_loop_or_bb_p (gimple *stmt1, gimple *stmt2) |
f71cf56a UW |
123 | { |
124 | stmt_vec_info stmt_vinfo = vinfo_for_stmt (stmt1); | |
61d371eb | 125 | return vect_stmt_in_region_p (stmt_vinfo->vinfo, stmt2); |
f71cf56a UW |
126 | } |
127 | ||
9a7a4398 UW |
128 | /* If the LHS of DEF_STMT has a single use, and that statement is |
129 | in the same loop or basic block, return it. */ | |
130 | ||
355fe088 TS |
131 | static gimple * |
132 | vect_single_imm_use (gimple *def_stmt) | |
9a7a4398 UW |
133 | { |
134 | tree lhs = gimple_assign_lhs (def_stmt); | |
135 | use_operand_p use_p; | |
355fe088 | 136 | gimple *use_stmt; |
9a7a4398 UW |
137 | |
138 | if (!single_imm_use (lhs, &use_p, &use_stmt)) | |
139 | return NULL; | |
140 | ||
141 | if (!vect_same_loop_or_bb_p (def_stmt, use_stmt)) | |
142 | return NULL; | |
143 | ||
144 | return use_stmt; | |
145 | } | |
146 | ||
bc4fb355 | 147 | /* Check whether NAME, an ssa-name used in USE_STMT, |
79d652a5 | 148 | is a result of a type promotion, such that: |
20f06221 | 149 | DEF_STMT: NAME = NOP (name0) |
383d9c83 IR |
150 | If CHECK_SIGN is TRUE, check that either both types are signed or both are |
151 | unsigned. */ | |
20f06221 DN |
152 | |
153 | static bool | |
355fe088 TS |
154 | type_conversion_p (tree name, gimple *use_stmt, bool check_sign, |
155 | tree *orig_type, gimple **def_stmt, bool *promotion) | |
20f06221 | 156 | { |
355fe088 | 157 | gimple *dummy_gimple; |
20f06221 | 158 | stmt_vec_info stmt_vinfo; |
20f06221 DN |
159 | tree type = TREE_TYPE (name); |
160 | tree oprnd0; | |
161 | enum vect_def_type dt; | |
20f06221 DN |
162 | |
163 | stmt_vinfo = vinfo_for_stmt (use_stmt); | |
81c40241 | 164 | if (!vect_is_simple_use (name, stmt_vinfo->vinfo, def_stmt, &dt)) |
20f06221 DN |
165 | return false; |
166 | ||
8644a673 IR |
167 | if (dt != vect_internal_def |
168 | && dt != vect_external_def && dt != vect_constant_def) | |
20f06221 DN |
169 | return false; |
170 | ||
bc4fb355 | 171 | if (!*def_stmt) |
20f06221 DN |
172 | return false; |
173 | ||
2a2b8f64 JJ |
174 | if (dt == vect_internal_def) |
175 | { | |
176 | stmt_vec_info def_vinfo = vinfo_for_stmt (*def_stmt); | |
177 | if (STMT_VINFO_IN_PATTERN_P (def_vinfo)) | |
178 | return false; | |
179 | } | |
180 | ||
726a989a | 181 | if (!is_gimple_assign (*def_stmt)) |
20f06221 DN |
182 | return false; |
183 | ||
bc4fb355 | 184 | if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (*def_stmt))) |
20f06221 DN |
185 | return false; |
186 | ||
726a989a | 187 | oprnd0 = gimple_assign_rhs1 (*def_stmt); |
20f06221 | 188 | |
bc4fb355 IR |
189 | *orig_type = TREE_TYPE (oprnd0); |
190 | if (!INTEGRAL_TYPE_P (type) || !INTEGRAL_TYPE_P (*orig_type) | |
191 | || ((TYPE_UNSIGNED (type) != TYPE_UNSIGNED (*orig_type)) && check_sign)) | |
192 | return false; | |
193 | ||
194 | if (TYPE_PRECISION (type) >= (TYPE_PRECISION (*orig_type) * 2)) | |
195 | *promotion = true; | |
bc4fb355 | 196 | else |
79d652a5 | 197 | *promotion = false; |
20f06221 | 198 | |
81c40241 | 199 | if (!vect_is_simple_use (oprnd0, stmt_vinfo->vinfo, &dummy_gimple, &dt)) |
20f06221 DN |
200 | return false; |
201 | ||
20f06221 DN |
202 | return true; |
203 | } | |
204 | ||
726a989a RB |
205 | /* Helper to return a new temporary for pattern of TYPE for STMT. If STMT |
206 | is NULL, the caller must set SSA_NAME_DEF_STMT for the returned SSA var. */ | |
207 | ||
208 | static tree | |
355fe088 | 209 | vect_recog_temp_ssa_var (tree type, gimple *stmt) |
726a989a | 210 | { |
83d5977e | 211 | return make_temp_ssa_name (type, stmt, "patt"); |
726a989a | 212 | } |
20f06221 DN |
213 | |
214 | /* Function vect_recog_dot_prod_pattern | |
215 | ||
216 | Try to find the following pattern: | |
217 | ||
218 | type x_t, y_t; | |
219 | TYPE1 prod; | |
220 | TYPE2 sum = init; | |
221 | loop: | |
222 | sum_0 = phi <init, sum_1> | |
223 | S1 x_t = ... | |
224 | S2 y_t = ... | |
225 | S3 x_T = (TYPE1) x_t; | |
226 | S4 y_T = (TYPE1) y_t; | |
227 | S5 prod = x_T * y_T; | |
228 | [S6 prod = (TYPE2) prod; #optional] | |
229 | S7 sum_1 = prod + sum_0; | |
230 | ||
b8698a0f L |
231 | where 'TYPE1' is exactly double the size of type 'type', and 'TYPE2' is the |
232 | same size of 'TYPE1' or bigger. This is a special case of a reduction | |
20f06221 | 233 | computation. |
b8698a0f | 234 | |
20f06221 DN |
235 | Input: |
236 | ||
51312233 IR |
237 | * STMTS: Contains a stmt from which the pattern search begins. In the |
238 | example, when this function is called with S7, the pattern {S3,S4,S5,S6,S7} | |
239 | will be detected. | |
20f06221 DN |
240 | |
241 | Output: | |
242 | ||
243 | * TYPE_IN: The type of the input arguments to the pattern. | |
244 | ||
245 | * TYPE_OUT: The type of the output of this pattern. | |
246 | ||
247 | * Return value: A new stmt that will be used to replace the sequence of | |
248 | stmts that constitute the pattern. In this case it will be: | |
249 | WIDEN_DOT_PRODUCT <x_t, y_t, sum_0> | |
d29de1bf DN |
250 | |
251 | Note: The dot-prod idiom is a widening reduction pattern that is | |
252 | vectorized without preserving all the intermediate results. It | |
253 | produces only N/2 (widened) results (by summing up pairs of | |
254 | intermediate results) rather than all N results. Therefore, we | |
255 | cannot allow this pattern when we want to get all the results and in | |
256 | the correct order (as is the case when this computation is in an | |
257 | inner-loop nested in an outer-loop that us being vectorized). */ | |
20f06221 | 258 | |
355fe088 TS |
259 | static gimple * |
260 | vect_recog_dot_prod_pattern (vec<gimple *> *stmts, tree *type_in, | |
51312233 | 261 | tree *type_out) |
20f06221 | 262 | { |
355fe088 | 263 | gimple *stmt, *last_stmt = (*stmts)[0]; |
20f06221 DN |
264 | tree oprnd0, oprnd1; |
265 | tree oprnd00, oprnd01; | |
51312233 | 266 | stmt_vec_info stmt_vinfo = vinfo_for_stmt (last_stmt); |
20f06221 | 267 | tree type, half_type; |
355fe088 | 268 | gimple *pattern_stmt; |
20f06221 | 269 | tree prod_type; |
d29de1bf | 270 | loop_vec_info loop_info = STMT_VINFO_LOOP_VINFO (stmt_vinfo); |
f5709183 | 271 | struct loop *loop; |
f471fe72 | 272 | tree var; |
bc4fb355 | 273 | bool promotion; |
20f06221 | 274 | |
f5709183 IR |
275 | if (!loop_info) |
276 | return NULL; | |
277 | ||
278 | loop = LOOP_VINFO_LOOP (loop_info); | |
279 | ||
328dc477 RB |
280 | /* We don't allow changing the order of the computation in the inner-loop |
281 | when doing outer-loop vectorization. */ | |
282 | if (loop && nested_in_vect_loop_p (loop, last_stmt)) | |
283 | return NULL; | |
284 | ||
51312233 | 285 | if (!is_gimple_assign (last_stmt)) |
20f06221 DN |
286 | return NULL; |
287 | ||
51312233 | 288 | type = gimple_expr_type (last_stmt); |
20f06221 | 289 | |
b8698a0f | 290 | /* Look for the following pattern |
20f06221 DN |
291 | DX = (TYPE1) X; |
292 | DY = (TYPE1) Y; | |
b8698a0f | 293 | DPROD = DX * DY; |
20f06221 DN |
294 | DDPROD = (TYPE2) DPROD; |
295 | sum_1 = DDPROD + sum_0; | |
b8698a0f | 296 | In which |
20f06221 DN |
297 | - DX is double the size of X |
298 | - DY is double the size of Y | |
299 | - DX, DY, DPROD all have the same type | |
300 | - sum is the same size of DPROD or bigger | |
301 | - sum has been recognized as a reduction variable. | |
302 | ||
303 | This is equivalent to: | |
304 | DPROD = X w* Y; #widen mult | |
305 | sum_1 = DPROD w+ sum_0; #widen summation | |
306 | or | |
307 | DPROD = X w* Y; #widen mult | |
308 | sum_1 = DPROD + sum_0; #summation | |
309 | */ | |
310 | ||
311 | /* Starting from LAST_STMT, follow the defs of its uses in search | |
312 | of the above pattern. */ | |
313 | ||
51312233 | 314 | if (gimple_assign_rhs_code (last_stmt) != PLUS_EXPR) |
20f06221 DN |
315 | return NULL; |
316 | ||
317 | if (STMT_VINFO_IN_PATTERN_P (stmt_vinfo)) | |
318 | { | |
319 | /* Has been detected as widening-summation? */ | |
320 | ||
321 | stmt = STMT_VINFO_RELATED_STMT (stmt_vinfo); | |
726a989a RB |
322 | type = gimple_expr_type (stmt); |
323 | if (gimple_assign_rhs_code (stmt) != WIDEN_SUM_EXPR) | |
20f06221 | 324 | return NULL; |
726a989a RB |
325 | oprnd0 = gimple_assign_rhs1 (stmt); |
326 | oprnd1 = gimple_assign_rhs2 (stmt); | |
20f06221 DN |
327 | half_type = TREE_TYPE (oprnd0); |
328 | } | |
329 | else | |
330 | { | |
355fe088 | 331 | gimple *def_stmt; |
20f06221 | 332 | |
b308d872 RB |
333 | if (STMT_VINFO_DEF_TYPE (stmt_vinfo) != vect_reduction_def |
334 | && ! STMT_VINFO_GROUP_FIRST_ELEMENT (stmt_vinfo)) | |
335 | return NULL; | |
51312233 IR |
336 | oprnd0 = gimple_assign_rhs1 (last_stmt); |
337 | oprnd1 = gimple_assign_rhs2 (last_stmt); | |
9600efe1 MM |
338 | if (!types_compatible_p (TREE_TYPE (oprnd0), type) |
339 | || !types_compatible_p (TREE_TYPE (oprnd1), type)) | |
20f06221 | 340 | return NULL; |
51312233 | 341 | stmt = last_stmt; |
20f06221 | 342 | |
bc4fb355 | 343 | if (type_conversion_p (oprnd0, stmt, true, &half_type, &def_stmt, |
2a2b8f64 JJ |
344 | &promotion) |
345 | && promotion) | |
20f06221 DN |
346 | { |
347 | stmt = def_stmt; | |
726a989a | 348 | oprnd0 = gimple_assign_rhs1 (stmt); |
20f06221 DN |
349 | } |
350 | else | |
351 | half_type = type; | |
352 | } | |
353 | ||
51312233 | 354 | /* So far so good. Since last_stmt was detected as a (summation) reduction, |
20f06221 DN |
355 | we know that oprnd1 is the reduction variable (defined by a loop-header |
356 | phi), and oprnd0 is an ssa-name defined by a stmt in the loop body. | |
357 | Left to check that oprnd0 is defined by a (widen_)mult_expr */ | |
ba02d3bc RG |
358 | if (TREE_CODE (oprnd0) != SSA_NAME) |
359 | return NULL; | |
20f06221 DN |
360 | |
361 | prod_type = half_type; | |
362 | stmt = SSA_NAME_DEF_STMT (oprnd0); | |
3cb35c12 CF |
363 | |
364 | /* It could not be the dot_prod pattern if the stmt is outside the loop. */ | |
75264e61 | 365 | if (!gimple_bb (stmt) || !flow_bb_inside_loop_p (loop, gimple_bb (stmt))) |
3cb35c12 CF |
366 | return NULL; |
367 | ||
b8698a0f | 368 | /* FORNOW. Can continue analyzing the def-use chain when this stmt in a phi |
8665227f | 369 | inside the loop (in case we are analyzing an outer-loop). */ |
726a989a | 370 | if (!is_gimple_assign (stmt)) |
b8698a0f | 371 | return NULL; |
20f06221 DN |
372 | stmt_vinfo = vinfo_for_stmt (stmt); |
373 | gcc_assert (stmt_vinfo); | |
8644a673 | 374 | if (STMT_VINFO_DEF_TYPE (stmt_vinfo) != vect_internal_def) |
b3130586 | 375 | return NULL; |
726a989a | 376 | if (gimple_assign_rhs_code (stmt) != MULT_EXPR) |
20f06221 DN |
377 | return NULL; |
378 | if (STMT_VINFO_IN_PATTERN_P (stmt_vinfo)) | |
379 | { | |
380 | /* Has been detected as a widening multiplication? */ | |
381 | ||
382 | stmt = STMT_VINFO_RELATED_STMT (stmt_vinfo); | |
726a989a | 383 | if (gimple_assign_rhs_code (stmt) != WIDEN_MULT_EXPR) |
20f06221 DN |
384 | return NULL; |
385 | stmt_vinfo = vinfo_for_stmt (stmt); | |
386 | gcc_assert (stmt_vinfo); | |
8644a673 | 387 | gcc_assert (STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_internal_def); |
726a989a RB |
388 | oprnd00 = gimple_assign_rhs1 (stmt); |
389 | oprnd01 = gimple_assign_rhs2 (stmt); | |
56f8faae CH |
390 | STMT_VINFO_PATTERN_DEF_SEQ (vinfo_for_stmt (last_stmt)) |
391 | = STMT_VINFO_PATTERN_DEF_SEQ (stmt_vinfo); | |
20f06221 DN |
392 | } |
393 | else | |
394 | { | |
395 | tree half_type0, half_type1; | |
355fe088 | 396 | gimple *def_stmt; |
20f06221 DN |
397 | tree oprnd0, oprnd1; |
398 | ||
726a989a RB |
399 | oprnd0 = gimple_assign_rhs1 (stmt); |
400 | oprnd1 = gimple_assign_rhs2 (stmt); | |
9600efe1 MM |
401 | if (!types_compatible_p (TREE_TYPE (oprnd0), prod_type) |
402 | || !types_compatible_p (TREE_TYPE (oprnd1), prod_type)) | |
20f06221 | 403 | return NULL; |
bc4fb355 | 404 | if (!type_conversion_p (oprnd0, stmt, true, &half_type0, &def_stmt, |
2a2b8f64 JJ |
405 | &promotion) |
406 | || !promotion) | |
20f06221 | 407 | return NULL; |
726a989a | 408 | oprnd00 = gimple_assign_rhs1 (def_stmt); |
181f5f3e | 409 | if (!type_conversion_p (oprnd1, stmt, true, &half_type1, &def_stmt, |
2a2b8f64 JJ |
410 | &promotion) |
411 | || !promotion) | |
20f06221 | 412 | return NULL; |
726a989a | 413 | oprnd01 = gimple_assign_rhs1 (def_stmt); |
9600efe1 | 414 | if (!types_compatible_p (half_type0, half_type1)) |
20f06221 DN |
415 | return NULL; |
416 | if (TYPE_PRECISION (prod_type) != TYPE_PRECISION (half_type0) * 2) | |
417 | return NULL; | |
418 | } | |
419 | ||
420 | half_type = TREE_TYPE (oprnd00); | |
421 | *type_in = half_type; | |
422 | *type_out = type; | |
b8698a0f | 423 | |
20f06221 | 424 | /* Pattern detected. Create a stmt to be used to replace the pattern: */ |
726a989a | 425 | var = vect_recog_temp_ssa_var (type, NULL); |
0d0e4a03 JJ |
426 | pattern_stmt = gimple_build_assign (var, DOT_PROD_EXPR, |
427 | oprnd00, oprnd01, oprnd1); | |
b8698a0f | 428 | |
73fbfcad | 429 | if (dump_enabled_p ()) |
20f06221 | 430 | { |
ccb3ad87 | 431 | dump_printf_loc (MSG_NOTE, vect_location, |
78c60e3d | 432 | "vect_recog_dot_prod_pattern: detected: "); |
ccb3ad87 | 433 | dump_gimple_stmt (MSG_NOTE, TDF_SLIM, pattern_stmt, 0); |
20f06221 | 434 | } |
d29de1bf | 435 | |
726a989a | 436 | return pattern_stmt; |
20f06221 | 437 | } |
b8698a0f | 438 | |
51312233 | 439 | |
79d652a5 CH |
440 | /* Function vect_recog_sad_pattern |
441 | ||
442 | Try to find the following Sum of Absolute Difference (SAD) pattern: | |
443 | ||
444 | type x_t, y_t; | |
445 | signed TYPE1 diff, abs_diff; | |
446 | TYPE2 sum = init; | |
447 | loop: | |
448 | sum_0 = phi <init, sum_1> | |
449 | S1 x_t = ... | |
450 | S2 y_t = ... | |
451 | S3 x_T = (TYPE1) x_t; | |
452 | S4 y_T = (TYPE1) y_t; | |
453 | S5 diff = x_T - y_T; | |
454 | S6 abs_diff = ABS_EXPR <diff>; | |
455 | [S7 abs_diff = (TYPE2) abs_diff; #optional] | |
456 | S8 sum_1 = abs_diff + sum_0; | |
457 | ||
458 | where 'TYPE1' is at least double the size of type 'type', and 'TYPE2' is the | |
459 | same size of 'TYPE1' or bigger. This is a special case of a reduction | |
460 | computation. | |
461 | ||
462 | Input: | |
463 | ||
464 | * STMTS: Contains a stmt from which the pattern search begins. In the | |
465 | example, when this function is called with S8, the pattern | |
466 | {S3,S4,S5,S6,S7,S8} will be detected. | |
467 | ||
468 | Output: | |
469 | ||
470 | * TYPE_IN: The type of the input arguments to the pattern. | |
471 | ||
472 | * TYPE_OUT: The type of the output of this pattern. | |
473 | ||
474 | * Return value: A new stmt that will be used to replace the sequence of | |
475 | stmts that constitute the pattern. In this case it will be: | |
476 | SAD_EXPR <x_t, y_t, sum_0> | |
477 | */ | |
478 | ||
355fe088 TS |
479 | static gimple * |
480 | vect_recog_sad_pattern (vec<gimple *> *stmts, tree *type_in, | |
79d652a5 CH |
481 | tree *type_out) |
482 | { | |
355fe088 | 483 | gimple *last_stmt = (*stmts)[0]; |
79d652a5 CH |
484 | tree sad_oprnd0, sad_oprnd1; |
485 | stmt_vec_info stmt_vinfo = vinfo_for_stmt (last_stmt); | |
486 | tree half_type; | |
487 | loop_vec_info loop_info = STMT_VINFO_LOOP_VINFO (stmt_vinfo); | |
488 | struct loop *loop; | |
489 | bool promotion; | |
490 | ||
491 | if (!loop_info) | |
492 | return NULL; | |
493 | ||
494 | loop = LOOP_VINFO_LOOP (loop_info); | |
495 | ||
328dc477 RB |
496 | /* We don't allow changing the order of the computation in the inner-loop |
497 | when doing outer-loop vectorization. */ | |
498 | if (loop && nested_in_vect_loop_p (loop, last_stmt)) | |
499 | return NULL; | |
500 | ||
79d652a5 CH |
501 | if (!is_gimple_assign (last_stmt)) |
502 | return NULL; | |
503 | ||
504 | tree sum_type = gimple_expr_type (last_stmt); | |
505 | ||
506 | /* Look for the following pattern | |
507 | DX = (TYPE1) X; | |
508 | DY = (TYPE1) Y; | |
509 | DDIFF = DX - DY; | |
510 | DAD = ABS_EXPR <DDIFF>; | |
511 | DDPROD = (TYPE2) DPROD; | |
512 | sum_1 = DAD + sum_0; | |
513 | In which | |
514 | - DX is at least double the size of X | |
515 | - DY is at least double the size of Y | |
516 | - DX, DY, DDIFF, DAD all have the same type | |
517 | - sum is the same size of DAD or bigger | |
518 | - sum has been recognized as a reduction variable. | |
519 | ||
520 | This is equivalent to: | |
521 | DDIFF = X w- Y; #widen sub | |
522 | DAD = ABS_EXPR <DDIFF>; | |
523 | sum_1 = DAD w+ sum_0; #widen summation | |
524 | or | |
525 | DDIFF = X w- Y; #widen sub | |
526 | DAD = ABS_EXPR <DDIFF>; | |
527 | sum_1 = DAD + sum_0; #summation | |
528 | */ | |
529 | ||
530 | /* Starting from LAST_STMT, follow the defs of its uses in search | |
531 | of the above pattern. */ | |
532 | ||
533 | if (gimple_assign_rhs_code (last_stmt) != PLUS_EXPR) | |
534 | return NULL; | |
535 | ||
536 | tree plus_oprnd0, plus_oprnd1; | |
537 | ||
538 | if (STMT_VINFO_IN_PATTERN_P (stmt_vinfo)) | |
539 | { | |
540 | /* Has been detected as widening-summation? */ | |
541 | ||
355fe088 | 542 | gimple *stmt = STMT_VINFO_RELATED_STMT (stmt_vinfo); |
79d652a5 CH |
543 | sum_type = gimple_expr_type (stmt); |
544 | if (gimple_assign_rhs_code (stmt) != WIDEN_SUM_EXPR) | |
545 | return NULL; | |
546 | plus_oprnd0 = gimple_assign_rhs1 (stmt); | |
547 | plus_oprnd1 = gimple_assign_rhs2 (stmt); | |
548 | half_type = TREE_TYPE (plus_oprnd0); | |
549 | } | |
550 | else | |
551 | { | |
355fe088 | 552 | gimple *def_stmt; |
79d652a5 | 553 | |
b308d872 RB |
554 | if (STMT_VINFO_DEF_TYPE (stmt_vinfo) != vect_reduction_def |
555 | && ! STMT_VINFO_GROUP_FIRST_ELEMENT (stmt_vinfo)) | |
556 | return NULL; | |
79d652a5 CH |
557 | plus_oprnd0 = gimple_assign_rhs1 (last_stmt); |
558 | plus_oprnd1 = gimple_assign_rhs2 (last_stmt); | |
559 | if (!types_compatible_p (TREE_TYPE (plus_oprnd0), sum_type) | |
560 | || !types_compatible_p (TREE_TYPE (plus_oprnd1), sum_type)) | |
561 | return NULL; | |
562 | ||
563 | /* The type conversion could be promotion, demotion, | |
564 | or just signed -> unsigned. */ | |
565 | if (type_conversion_p (plus_oprnd0, last_stmt, false, | |
566 | &half_type, &def_stmt, &promotion)) | |
567 | plus_oprnd0 = gimple_assign_rhs1 (def_stmt); | |
568 | else | |
569 | half_type = sum_type; | |
570 | } | |
571 | ||
572 | /* So far so good. Since last_stmt was detected as a (summation) reduction, | |
573 | we know that plus_oprnd1 is the reduction variable (defined by a loop-header | |
574 | phi), and plus_oprnd0 is an ssa-name defined by a stmt in the loop body. | |
575 | Then check that plus_oprnd0 is defined by an abs_expr. */ | |
576 | ||
577 | if (TREE_CODE (plus_oprnd0) != SSA_NAME) | |
578 | return NULL; | |
579 | ||
580 | tree abs_type = half_type; | |
355fe088 | 581 | gimple *abs_stmt = SSA_NAME_DEF_STMT (plus_oprnd0); |
79d652a5 CH |
582 | |
583 | /* It could not be the sad pattern if the abs_stmt is outside the loop. */ | |
584 | if (!gimple_bb (abs_stmt) || !flow_bb_inside_loop_p (loop, gimple_bb (abs_stmt))) | |
585 | return NULL; | |
586 | ||
587 | /* FORNOW. Can continue analyzing the def-use chain when this stmt in a phi | |
588 | inside the loop (in case we are analyzing an outer-loop). */ | |
589 | if (!is_gimple_assign (abs_stmt)) | |
590 | return NULL; | |
591 | ||
592 | stmt_vec_info abs_stmt_vinfo = vinfo_for_stmt (abs_stmt); | |
593 | gcc_assert (abs_stmt_vinfo); | |
594 | if (STMT_VINFO_DEF_TYPE (abs_stmt_vinfo) != vect_internal_def) | |
595 | return NULL; | |
596 | if (gimple_assign_rhs_code (abs_stmt) != ABS_EXPR) | |
597 | return NULL; | |
598 | ||
599 | tree abs_oprnd = gimple_assign_rhs1 (abs_stmt); | |
600 | if (!types_compatible_p (TREE_TYPE (abs_oprnd), abs_type)) | |
601 | return NULL; | |
602 | if (TYPE_UNSIGNED (abs_type)) | |
603 | return NULL; | |
604 | ||
605 | /* We then detect if the operand of abs_expr is defined by a minus_expr. */ | |
606 | ||
607 | if (TREE_CODE (abs_oprnd) != SSA_NAME) | |
608 | return NULL; | |
609 | ||
355fe088 | 610 | gimple *diff_stmt = SSA_NAME_DEF_STMT (abs_oprnd); |
79d652a5 CH |
611 | |
612 | /* It could not be the sad pattern if the diff_stmt is outside the loop. */ | |
613 | if (!gimple_bb (diff_stmt) | |
614 | || !flow_bb_inside_loop_p (loop, gimple_bb (diff_stmt))) | |
615 | return NULL; | |
616 | ||
617 | /* FORNOW. Can continue analyzing the def-use chain when this stmt in a phi | |
618 | inside the loop (in case we are analyzing an outer-loop). */ | |
619 | if (!is_gimple_assign (diff_stmt)) | |
620 | return NULL; | |
621 | ||
622 | stmt_vec_info diff_stmt_vinfo = vinfo_for_stmt (diff_stmt); | |
623 | gcc_assert (diff_stmt_vinfo); | |
624 | if (STMT_VINFO_DEF_TYPE (diff_stmt_vinfo) != vect_internal_def) | |
625 | return NULL; | |
626 | if (gimple_assign_rhs_code (diff_stmt) != MINUS_EXPR) | |
627 | return NULL; | |
628 | ||
629 | tree half_type0, half_type1; | |
355fe088 | 630 | gimple *def_stmt; |
79d652a5 CH |
631 | |
632 | tree minus_oprnd0 = gimple_assign_rhs1 (diff_stmt); | |
633 | tree minus_oprnd1 = gimple_assign_rhs2 (diff_stmt); | |
634 | ||
635 | if (!types_compatible_p (TREE_TYPE (minus_oprnd0), abs_type) | |
636 | || !types_compatible_p (TREE_TYPE (minus_oprnd1), abs_type)) | |
637 | return NULL; | |
638 | if (!type_conversion_p (minus_oprnd0, diff_stmt, false, | |
639 | &half_type0, &def_stmt, &promotion) | |
640 | || !promotion) | |
641 | return NULL; | |
642 | sad_oprnd0 = gimple_assign_rhs1 (def_stmt); | |
643 | ||
644 | if (!type_conversion_p (minus_oprnd1, diff_stmt, false, | |
645 | &half_type1, &def_stmt, &promotion) | |
646 | || !promotion) | |
647 | return NULL; | |
648 | sad_oprnd1 = gimple_assign_rhs1 (def_stmt); | |
649 | ||
650 | if (!types_compatible_p (half_type0, half_type1)) | |
651 | return NULL; | |
652 | if (TYPE_PRECISION (abs_type) < TYPE_PRECISION (half_type0) * 2 | |
653 | || TYPE_PRECISION (sum_type) < TYPE_PRECISION (half_type0) * 2) | |
654 | return NULL; | |
655 | ||
656 | *type_in = TREE_TYPE (sad_oprnd0); | |
657 | *type_out = sum_type; | |
658 | ||
659 | /* Pattern detected. Create a stmt to be used to replace the pattern: */ | |
660 | tree var = vect_recog_temp_ssa_var (sum_type, NULL); | |
355fe088 TS |
661 | gimple *pattern_stmt = gimple_build_assign (var, SAD_EXPR, sad_oprnd0, |
662 | sad_oprnd1, plus_oprnd1); | |
79d652a5 CH |
663 | |
664 | if (dump_enabled_p ()) | |
665 | { | |
666 | dump_printf_loc (MSG_NOTE, vect_location, | |
667 | "vect_recog_sad_pattern: detected: "); | |
668 | dump_gimple_stmt (MSG_NOTE, TDF_SLIM, pattern_stmt, 0); | |
79d652a5 CH |
669 | } |
670 | ||
79d652a5 CH |
671 | return pattern_stmt; |
672 | } | |
673 | ||
674 | ||
36ba4aae IR |
675 | /* Handle widening operation by a constant. At the moment we support MULT_EXPR |
676 | and LSHIFT_EXPR. | |
677 | ||
678 | For MULT_EXPR we check that CONST_OPRND fits HALF_TYPE, and for LSHIFT_EXPR | |
679 | we check that CONST_OPRND is less or equal to the size of HALF_TYPE. | |
51312233 IR |
680 | |
681 | Otherwise, if the type of the result (TYPE) is at least 4 times bigger than | |
36ba4aae IR |
682 | HALF_TYPE, and there is an intermediate type (2 times smaller than TYPE) |
683 | that satisfies the above restrictions, we can perform a widening opeartion | |
684 | from the intermediate type to TYPE and replace a_T = (TYPE) a_t; | |
566d377a | 685 | with a_it = (interm_type) a_t; Store such operation in *WSTMT. */ |
51312233 IR |
686 | |
687 | static bool | |
355fe088 | 688 | vect_handle_widen_op_by_const (gimple *stmt, enum tree_code code, |
36ba4aae | 689 | tree const_oprnd, tree *oprnd, |
355fe088 TS |
690 | gimple **wstmt, tree type, |
691 | tree *half_type, gimple *def_stmt) | |
51312233 | 692 | { |
83d5977e | 693 | tree new_type, new_oprnd; |
51312233 | 694 | |
36ba4aae IR |
695 | if (code != MULT_EXPR && code != LSHIFT_EXPR) |
696 | return false; | |
697 | ||
698 | if (((code == MULT_EXPR && int_fits_type_p (const_oprnd, *half_type)) | |
699 | || (code == LSHIFT_EXPR | |
700 | && compare_tree_int (const_oprnd, TYPE_PRECISION (*half_type)) | |
701 | != 1)) | |
702 | && TYPE_PRECISION (type) == (TYPE_PRECISION (*half_type) * 2)) | |
51312233 IR |
703 | { |
704 | /* CONST_OPRND is a constant of HALF_TYPE. */ | |
705 | *oprnd = gimple_assign_rhs1 (def_stmt); | |
706 | return true; | |
707 | } | |
708 | ||
f71cf56a UW |
709 | if (TYPE_PRECISION (type) < (TYPE_PRECISION (*half_type) * 4)) |
710 | return false; | |
711 | ||
712 | if (!vect_same_loop_or_bb_p (stmt, def_stmt)) | |
51312233 IR |
713 | return false; |
714 | ||
36ba4aae | 715 | /* TYPE is 4 times bigger than HALF_TYPE, try widening operation for |
51312233 IR |
716 | a type 2 times bigger than HALF_TYPE. */ |
717 | new_type = build_nonstandard_integer_type (TYPE_PRECISION (type) / 2, | |
718 | TYPE_UNSIGNED (type)); | |
36ba4aae IR |
719 | if ((code == MULT_EXPR && !int_fits_type_p (const_oprnd, new_type)) |
720 | || (code == LSHIFT_EXPR | |
721 | && compare_tree_int (const_oprnd, TYPE_PRECISION (new_type)) == 1)) | |
51312233 IR |
722 | return false; |
723 | ||
566d377a RB |
724 | /* Use NEW_TYPE for widening operation and create a_T = (NEW_TYPE) a_t; */ |
725 | *oprnd = gimple_assign_rhs1 (def_stmt); | |
726 | new_oprnd = make_ssa_name (new_type); | |
727 | *wstmt = gimple_build_assign (new_oprnd, NOP_EXPR, *oprnd); | |
728 | *oprnd = new_oprnd; | |
51312233 IR |
729 | |
730 | *half_type = new_type; | |
731 | return true; | |
732 | } | |
733 | ||
734 | ||
20f06221 DN |
735 | /* Function vect_recog_widen_mult_pattern |
736 | ||
737 | Try to find the following pattern: | |
738 | ||
d367387c CH |
739 | type1 a_t; |
740 | type2 b_t; | |
20f06221 DN |
741 | TYPE a_T, b_T, prod_T; |
742 | ||
743 | S1 a_t = ; | |
744 | S2 b_t = ; | |
745 | S3 a_T = (TYPE) a_t; | |
746 | S4 b_T = (TYPE) b_t; | |
747 | S5 prod_T = a_T * b_T; | |
748 | ||
d367387c | 749 | where type 'TYPE' is at least double the size of type 'type1' and 'type2'. |
20f06221 | 750 | |
d47657bd | 751 | Also detect unsigned cases: |
383d9c83 | 752 | |
d367387c CH |
753 | unsigned type1 a_t; |
754 | unsigned type2 b_t; | |
383d9c83 IR |
755 | unsigned TYPE u_prod_T; |
756 | TYPE a_T, b_T, prod_T; | |
757 | ||
758 | S1 a_t = ; | |
759 | S2 b_t = ; | |
760 | S3 a_T = (TYPE) a_t; | |
761 | S4 b_T = (TYPE) b_t; | |
762 | S5 prod_T = a_T * b_T; | |
763 | S6 u_prod_T = (unsigned TYPE) prod_T; | |
764 | ||
765 | and multiplication by constants: | |
766 | ||
767 | type a_t; | |
768 | TYPE a_T, prod_T; | |
769 | ||
770 | S1 a_t = ; | |
771 | S3 a_T = (TYPE) a_t; | |
772 | S5 prod_T = a_T * CONST; | |
773 | ||
51312233 IR |
774 | A special case of multiplication by constants is when 'TYPE' is 4 times |
775 | bigger than 'type', but CONST fits an intermediate type 2 times smaller | |
776 | than 'TYPE'. In that case we create an additional pattern stmt for S3 | |
777 | to create a variable of the intermediate type, and perform widen-mult | |
778 | on the intermediate type as well: | |
779 | ||
780 | type a_t; | |
781 | interm_type a_it; | |
782 | TYPE a_T, prod_T, prod_T'; | |
783 | ||
784 | S1 a_t = ; | |
785 | S3 a_T = (TYPE) a_t; | |
786 | '--> a_it = (interm_type) a_t; | |
787 | S5 prod_T = a_T * CONST; | |
788 | '--> prod_T' = a_it w* CONST; | |
20f06221 | 789 | |
51312233 IR |
790 | Input/Output: |
791 | ||
792 | * STMTS: Contains a stmt from which the pattern search begins. In the | |
793 | example, when this function is called with S5, the pattern {S3,S4,S5,(S6)} | |
794 | is detected. In case of unsigned widen-mult, the original stmt (S5) is | |
795 | replaced with S6 in STMTS. In case of multiplication by a constant | |
796 | of an intermediate type (the last case above), STMTS also contains S3 | |
797 | (inserted before S5). | |
20f06221 DN |
798 | |
799 | Output: | |
800 | ||
801 | * TYPE_IN: The type of the input arguments to the pattern. | |
802 | ||
383d9c83 | 803 | * TYPE_OUT: The type of the output of this pattern. |
20f06221 DN |
804 | |
805 | * Return value: A new stmt that will be used to replace the sequence of | |
383d9c83 | 806 | stmts that constitute the pattern. In this case it will be: |
20f06221 | 807 | WIDEN_MULT <a_t, b_t> |
d367387c CH |
808 | If the result of WIDEN_MULT needs to be converted to a larger type, the |
809 | returned stmt will be this type conversion stmt. | |
20f06221 DN |
810 | */ |
811 | ||
355fe088 TS |
812 | static gimple * |
813 | vect_recog_widen_mult_pattern (vec<gimple *> *stmts, | |
51312233 | 814 | tree *type_in, tree *type_out) |
20f06221 | 815 | { |
355fe088 TS |
816 | gimple *last_stmt = stmts->pop (); |
817 | gimple *def_stmt0, *def_stmt1; | |
89d67cca DN |
818 | tree oprnd0, oprnd1; |
819 | tree type, half_type0, half_type1; | |
355fe088 | 820 | gimple *new_stmt = NULL, *pattern_stmt = NULL; |
d367387c | 821 | tree vectype, vecitype; |
726a989a | 822 | tree var; |
89d67cca | 823 | enum tree_code dummy_code; |
5d593372 | 824 | int dummy_int; |
9771b263 | 825 | vec<tree> dummy_vec; |
36ba4aae | 826 | bool op1_ok; |
bc4fb355 | 827 | bool promotion; |
89d67cca | 828 | |
51312233 | 829 | if (!is_gimple_assign (last_stmt)) |
89d67cca DN |
830 | return NULL; |
831 | ||
51312233 | 832 | type = gimple_expr_type (last_stmt); |
89d67cca DN |
833 | |
834 | /* Starting from LAST_STMT, follow the defs of its uses in search | |
835 | of the above pattern. */ | |
836 | ||
51312233 | 837 | if (gimple_assign_rhs_code (last_stmt) != MULT_EXPR) |
89d67cca DN |
838 | return NULL; |
839 | ||
51312233 IR |
840 | oprnd0 = gimple_assign_rhs1 (last_stmt); |
841 | oprnd1 = gimple_assign_rhs2 (last_stmt); | |
9600efe1 MM |
842 | if (!types_compatible_p (TREE_TYPE (oprnd0), type) |
843 | || !types_compatible_p (TREE_TYPE (oprnd1), type)) | |
89d67cca DN |
844 | return NULL; |
845 | ||
383d9c83 | 846 | /* Check argument 0. */ |
bc4fb355 IR |
847 | if (!type_conversion_p (oprnd0, last_stmt, false, &half_type0, &def_stmt0, |
848 | &promotion) | |
849 | || !promotion) | |
850 | return NULL; | |
383d9c83 | 851 | /* Check argument 1. */ |
bc4fb355 IR |
852 | op1_ok = type_conversion_p (oprnd1, last_stmt, false, &half_type1, |
853 | &def_stmt1, &promotion); | |
89d67cca | 854 | |
bc4fb355 | 855 | if (op1_ok && promotion) |
383d9c83 IR |
856 | { |
857 | oprnd0 = gimple_assign_rhs1 (def_stmt0); | |
858 | oprnd1 = gimple_assign_rhs1 (def_stmt1); | |
859 | } | |
36ba4aae | 860 | else |
383d9c83 | 861 | { |
51312233 | 862 | if (TREE_CODE (oprnd1) == INTEGER_CST |
383d9c83 | 863 | && TREE_CODE (half_type0) == INTEGER_TYPE |
36ba4aae | 864 | && vect_handle_widen_op_by_const (last_stmt, MULT_EXPR, oprnd1, |
566d377a | 865 | &oprnd0, &new_stmt, type, |
36ba4aae | 866 | &half_type0, def_stmt0)) |
bfdeda2c JJ |
867 | { |
868 | half_type1 = half_type0; | |
869 | oprnd1 = fold_convert (half_type1, oprnd1); | |
870 | } | |
383d9c83 IR |
871 | else |
872 | return NULL; | |
873 | } | |
874 | ||
d367387c CH |
875 | /* If the two arguments have different sizes, convert the one with |
876 | the smaller type into the larger type. */ | |
877 | if (TYPE_PRECISION (half_type0) != TYPE_PRECISION (half_type1)) | |
878 | { | |
566d377a RB |
879 | /* If we already used up the single-stmt slot give up. */ |
880 | if (new_stmt) | |
881 | return NULL; | |
882 | ||
d367387c | 883 | tree* oprnd = NULL; |
355fe088 | 884 | gimple *def_stmt = NULL; |
d367387c CH |
885 | |
886 | if (TYPE_PRECISION (half_type0) < TYPE_PRECISION (half_type1)) | |
887 | { | |
888 | def_stmt = def_stmt0; | |
889 | half_type0 = half_type1; | |
890 | oprnd = &oprnd0; | |
891 | } | |
892 | else | |
893 | { | |
894 | def_stmt = def_stmt1; | |
895 | half_type1 = half_type0; | |
896 | oprnd = &oprnd1; | |
897 | } | |
898 | ||
2a2b8f64 JJ |
899 | tree old_oprnd = gimple_assign_rhs1 (def_stmt); |
900 | tree new_oprnd = make_ssa_name (half_type0); | |
901 | new_stmt = gimple_build_assign (new_oprnd, NOP_EXPR, old_oprnd); | |
902 | *oprnd = new_oprnd; | |
d367387c CH |
903 | } |
904 | ||
383d9c83 IR |
905 | /* Handle unsigned case. Look for |
906 | S6 u_prod_T = (unsigned TYPE) prod_T; | |
907 | Use unsigned TYPE as the type for WIDEN_MULT_EXPR. */ | |
908 | if (TYPE_UNSIGNED (type) != TYPE_UNSIGNED (half_type0)) | |
909 | { | |
355fe088 | 910 | gimple *use_stmt; |
9a7a4398 | 911 | tree use_lhs; |
383d9c83 IR |
912 | tree use_type; |
913 | ||
914 | if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (half_type1)) | |
915 | return NULL; | |
916 | ||
9a7a4398 UW |
917 | use_stmt = vect_single_imm_use (last_stmt); |
918 | if (!use_stmt || !is_gimple_assign (use_stmt) | |
625a9766 | 919 | || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (use_stmt))) |
383d9c83 IR |
920 | return NULL; |
921 | ||
922 | use_lhs = gimple_assign_lhs (use_stmt); | |
923 | use_type = TREE_TYPE (use_lhs); | |
924 | if (!INTEGRAL_TYPE_P (use_type) | |
925 | || (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (use_type)) | |
926 | || (TYPE_PRECISION (type) != TYPE_PRECISION (use_type))) | |
927 | return NULL; | |
928 | ||
929 | type = use_type; | |
51312233 | 930 | last_stmt = use_stmt; |
383d9c83 | 931 | } |
89d67cca | 932 | |
9600efe1 | 933 | if (!types_compatible_p (half_type0, half_type1)) |
89d67cca DN |
934 | return NULL; |
935 | ||
d367387c CH |
936 | /* If TYPE is more than twice larger than HALF_TYPE, we use WIDEN_MULT |
937 | to get an intermediate result of type ITYPE. In this case we need | |
938 | to build a statement to convert this intermediate result to type TYPE. */ | |
939 | tree itype = type; | |
940 | if (TYPE_PRECISION (type) > TYPE_PRECISION (half_type0) * 2) | |
941 | itype = build_nonstandard_integer_type | |
942 | (GET_MODE_BITSIZE (TYPE_MODE (half_type0)) * 2, | |
943 | TYPE_UNSIGNED (type)); | |
944 | ||
89d67cca | 945 | /* Pattern detected. */ |
73fbfcad | 946 | if (dump_enabled_p ()) |
ccb3ad87 | 947 | dump_printf_loc (MSG_NOTE, vect_location, |
e645e942 | 948 | "vect_recog_widen_mult_pattern: detected:\n"); |
89d67cca DN |
949 | |
950 | /* Check target support */ | |
951 | vectype = get_vectype_for_scalar_type (half_type0); | |
d367387c | 952 | vecitype = get_vectype_for_scalar_type (itype); |
03d3e953 | 953 | if (!vectype |
d367387c | 954 | || !vecitype |
51312233 | 955 | || !supportable_widening_operation (WIDEN_MULT_EXPR, last_stmt, |
d367387c | 956 | vecitype, vectype, |
a86ec597 RH |
957 | &dummy_code, &dummy_code, |
958 | &dummy_int, &dummy_vec)) | |
89d67cca DN |
959 | return NULL; |
960 | ||
961 | *type_in = vectype; | |
d367387c | 962 | *type_out = get_vectype_for_scalar_type (type); |
89d67cca DN |
963 | |
964 | /* Pattern supported. Create a stmt to be used to replace the pattern: */ | |
d367387c | 965 | var = vect_recog_temp_ssa_var (itype, NULL); |
0d0e4a03 | 966 | pattern_stmt = gimple_build_assign (var, WIDEN_MULT_EXPR, oprnd0, oprnd1); |
726a989a | 967 | |
d367387c | 968 | stmt_vec_info stmt_vinfo = vinfo_for_stmt (last_stmt); |
d367387c CH |
969 | STMT_VINFO_PATTERN_DEF_SEQ (stmt_vinfo) = NULL; |
970 | ||
971 | /* If the original two operands have different sizes, we may need to convert | |
972 | the smaller one into the larget type. If this is the case, at this point | |
973 | the new stmt is already built. */ | |
974 | if (new_stmt) | |
975 | { | |
976 | append_pattern_def_seq (stmt_vinfo, new_stmt); | |
977 | stmt_vec_info new_stmt_info | |
310213d4 | 978 | = new_stmt_vec_info (new_stmt, stmt_vinfo->vinfo); |
d367387c CH |
979 | set_vinfo_for_stmt (new_stmt, new_stmt_info); |
980 | STMT_VINFO_VECTYPE (new_stmt_info) = vectype; | |
981 | } | |
982 | ||
983 | /* If ITYPE is not TYPE, we need to build a type convertion stmt to convert | |
984 | the result of the widen-mult operation into type TYPE. */ | |
985 | if (itype != type) | |
986 | { | |
987 | append_pattern_def_seq (stmt_vinfo, pattern_stmt); | |
988 | stmt_vec_info pattern_stmt_info | |
310213d4 | 989 | = new_stmt_vec_info (pattern_stmt, stmt_vinfo->vinfo); |
d367387c CH |
990 | set_vinfo_for_stmt (pattern_stmt, pattern_stmt_info); |
991 | STMT_VINFO_VECTYPE (pattern_stmt_info) = vecitype; | |
0d0e4a03 JJ |
992 | pattern_stmt = gimple_build_assign (vect_recog_temp_ssa_var (type, NULL), |
993 | NOP_EXPR, | |
994 | gimple_assign_lhs (pattern_stmt)); | |
d367387c CH |
995 | } |
996 | ||
73fbfcad | 997 | if (dump_enabled_p ()) |
78c60e3d | 998 | dump_gimple_stmt_loc (MSG_NOTE, vect_location, TDF_SLIM, pattern_stmt, 0); |
726a989a | 999 | |
9771b263 | 1000 | stmts->safe_push (last_stmt); |
726a989a | 1001 | return pattern_stmt; |
20f06221 DN |
1002 | } |
1003 | ||
1004 | ||
0b2229b0 RG |
1005 | /* Function vect_recog_pow_pattern |
1006 | ||
1007 | Try to find the following pattern: | |
1008 | ||
1009 | x = POW (y, N); | |
1010 | ||
1011 | with POW being one of pow, powf, powi, powif and N being | |
1012 | either 2 or 0.5. | |
1013 | ||
1014 | Input: | |
1015 | ||
1016 | * LAST_STMT: A stmt from which the pattern search begins. | |
1017 | ||
1018 | Output: | |
1019 | ||
1020 | * TYPE_IN: The type of the input arguments to the pattern. | |
1021 | ||
1022 | * TYPE_OUT: The type of the output of this pattern. | |
1023 | ||
1024 | * Return value: A new stmt that will be used to replace the sequence of | |
1025 | stmts that constitute the pattern. In this case it will be: | |
726a989a | 1026 | x = x * x |
0b2229b0 | 1027 | or |
726a989a | 1028 | x = sqrt (x) |
0b2229b0 RG |
1029 | */ |
1030 | ||
355fe088 TS |
1031 | static gimple * |
1032 | vect_recog_pow_pattern (vec<gimple *> *stmts, tree *type_in, | |
51312233 | 1033 | tree *type_out) |
0b2229b0 | 1034 | { |
355fe088 | 1035 | gimple *last_stmt = (*stmts)[0]; |
7a31e5ef | 1036 | tree base, exp = NULL; |
355fe088 | 1037 | gimple *stmt; |
726a989a | 1038 | tree var; |
0b2229b0 | 1039 | |
51312233 | 1040 | if (!is_gimple_call (last_stmt) || gimple_call_lhs (last_stmt) == NULL) |
0b2229b0 RG |
1041 | return NULL; |
1042 | ||
7a31e5ef | 1043 | switch (gimple_call_combined_fn (last_stmt)) |
0b2229b0 | 1044 | { |
7a31e5ef RS |
1045 | CASE_CFN_POW: |
1046 | CASE_CFN_POWI: | |
51312233 IR |
1047 | base = gimple_call_arg (last_stmt, 0); |
1048 | exp = gimple_call_arg (last_stmt, 1); | |
0b2229b0 RG |
1049 | if (TREE_CODE (exp) != REAL_CST |
1050 | && TREE_CODE (exp) != INTEGER_CST) | |
726a989a | 1051 | return NULL; |
0b2229b0 RG |
1052 | break; |
1053 | ||
726a989a RB |
1054 | default: |
1055 | return NULL; | |
0b2229b0 RG |
1056 | } |
1057 | ||
1058 | /* We now have a pow or powi builtin function call with a constant | |
1059 | exponent. */ | |
1060 | ||
0b2229b0 RG |
1061 | *type_out = NULL_TREE; |
1062 | ||
1063 | /* Catch squaring. */ | |
9541ffee | 1064 | if ((tree_fits_shwi_p (exp) |
9439e9a1 | 1065 | && tree_to_shwi (exp) == 2) |
0b2229b0 | 1066 | || (TREE_CODE (exp) == REAL_CST |
624d31fe | 1067 | && real_equal (&TREE_REAL_CST (exp), &dconst2))) |
c6b1b49b RG |
1068 | { |
1069 | *type_in = TREE_TYPE (base); | |
726a989a RB |
1070 | |
1071 | var = vect_recog_temp_ssa_var (TREE_TYPE (base), NULL); | |
0d0e4a03 | 1072 | stmt = gimple_build_assign (var, MULT_EXPR, base, base); |
726a989a | 1073 | return stmt; |
c6b1b49b | 1074 | } |
0b2229b0 RG |
1075 | |
1076 | /* Catch square root. */ | |
1077 | if (TREE_CODE (exp) == REAL_CST | |
624d31fe | 1078 | && real_equal (&TREE_REAL_CST (exp), &dconsthalf)) |
0b2229b0 | 1079 | { |
c6b1b49b | 1080 | *type_in = get_vectype_for_scalar_type (TREE_TYPE (base)); |
d95ab70a RS |
1081 | if (*type_in |
1082 | && direct_internal_fn_supported_p (IFN_SQRT, *type_in, | |
1083 | OPTIMIZE_FOR_SPEED)) | |
c6b1b49b | 1084 | { |
b4e5bc47 RS |
1085 | gcall *stmt = gimple_build_call_internal (IFN_SQRT, 1, base); |
1086 | var = vect_recog_temp_ssa_var (TREE_TYPE (base), stmt); | |
1087 | gimple_call_set_lhs (stmt, var); | |
1088 | return stmt; | |
c6b1b49b | 1089 | } |
0b2229b0 RG |
1090 | } |
1091 | ||
726a989a | 1092 | return NULL; |
0b2229b0 RG |
1093 | } |
1094 | ||
1095 | ||
20f06221 DN |
1096 | /* Function vect_recog_widen_sum_pattern |
1097 | ||
1098 | Try to find the following pattern: | |
1099 | ||
b8698a0f | 1100 | type x_t; |
20f06221 DN |
1101 | TYPE x_T, sum = init; |
1102 | loop: | |
1103 | sum_0 = phi <init, sum_1> | |
1104 | S1 x_t = *p; | |
1105 | S2 x_T = (TYPE) x_t; | |
1106 | S3 sum_1 = x_T + sum_0; | |
1107 | ||
b8698a0f | 1108 | where type 'TYPE' is at least double the size of type 'type', i.e - we're |
20f06221 | 1109 | summing elements of type 'type' into an accumulator of type 'TYPE'. This is |
917f1b7e | 1110 | a special case of a reduction computation. |
20f06221 DN |
1111 | |
1112 | Input: | |
1113 | ||
1114 | * LAST_STMT: A stmt from which the pattern search begins. In the example, | |
1115 | when this function is called with S3, the pattern {S2,S3} will be detected. | |
b8698a0f | 1116 | |
20f06221 | 1117 | Output: |
b8698a0f | 1118 | |
20f06221 DN |
1119 | * TYPE_IN: The type of the input arguments to the pattern. |
1120 | ||
1121 | * TYPE_OUT: The type of the output of this pattern. | |
1122 | ||
1123 | * Return value: A new stmt that will be used to replace the sequence of | |
1124 | stmts that constitute the pattern. In this case it will be: | |
1125 | WIDEN_SUM <x_t, sum_0> | |
d29de1bf | 1126 | |
b8698a0f | 1127 | Note: The widening-sum idiom is a widening reduction pattern that is |
d29de1bf | 1128 | vectorized without preserving all the intermediate results. It |
b8698a0f L |
1129 | produces only N/2 (widened) results (by summing up pairs of |
1130 | intermediate results) rather than all N results. Therefore, we | |
1131 | cannot allow this pattern when we want to get all the results and in | |
1132 | the correct order (as is the case when this computation is in an | |
d29de1bf | 1133 | inner-loop nested in an outer-loop that us being vectorized). */ |
20f06221 | 1134 | |
355fe088 TS |
1135 | static gimple * |
1136 | vect_recog_widen_sum_pattern (vec<gimple *> *stmts, tree *type_in, | |
51312233 | 1137 | tree *type_out) |
20f06221 | 1138 | { |
355fe088 | 1139 | gimple *stmt, *last_stmt = (*stmts)[0]; |
20f06221 | 1140 | tree oprnd0, oprnd1; |
51312233 | 1141 | stmt_vec_info stmt_vinfo = vinfo_for_stmt (last_stmt); |
20f06221 | 1142 | tree type, half_type; |
355fe088 | 1143 | gimple *pattern_stmt; |
d29de1bf | 1144 | loop_vec_info loop_info = STMT_VINFO_LOOP_VINFO (stmt_vinfo); |
f5709183 | 1145 | struct loop *loop; |
726a989a | 1146 | tree var; |
bc4fb355 | 1147 | bool promotion; |
20f06221 | 1148 | |
f5709183 IR |
1149 | if (!loop_info) |
1150 | return NULL; | |
1151 | ||
1152 | loop = LOOP_VINFO_LOOP (loop_info); | |
1153 | ||
328dc477 RB |
1154 | /* We don't allow changing the order of the computation in the inner-loop |
1155 | when doing outer-loop vectorization. */ | |
1156 | if (loop && nested_in_vect_loop_p (loop, last_stmt)) | |
1157 | return NULL; | |
1158 | ||
51312233 | 1159 | if (!is_gimple_assign (last_stmt)) |
20f06221 DN |
1160 | return NULL; |
1161 | ||
51312233 | 1162 | type = gimple_expr_type (last_stmt); |
20f06221 DN |
1163 | |
1164 | /* Look for the following pattern | |
1165 | DX = (TYPE) X; | |
1166 | sum_1 = DX + sum_0; | |
1167 | In which DX is at least double the size of X, and sum_1 has been | |
1168 | recognized as a reduction variable. | |
1169 | */ | |
1170 | ||
1171 | /* Starting from LAST_STMT, follow the defs of its uses in search | |
1172 | of the above pattern. */ | |
1173 | ||
51312233 | 1174 | if (gimple_assign_rhs_code (last_stmt) != PLUS_EXPR) |
20f06221 DN |
1175 | return NULL; |
1176 | ||
b308d872 RB |
1177 | if (STMT_VINFO_DEF_TYPE (stmt_vinfo) != vect_reduction_def |
1178 | && ! STMT_VINFO_GROUP_FIRST_ELEMENT (stmt_vinfo)) | |
1179 | return NULL; | |
1180 | ||
51312233 IR |
1181 | oprnd0 = gimple_assign_rhs1 (last_stmt); |
1182 | oprnd1 = gimple_assign_rhs2 (last_stmt); | |
9600efe1 MM |
1183 | if (!types_compatible_p (TREE_TYPE (oprnd0), type) |
1184 | || !types_compatible_p (TREE_TYPE (oprnd1), type)) | |
20f06221 DN |
1185 | return NULL; |
1186 | ||
51312233 | 1187 | /* So far so good. Since last_stmt was detected as a (summation) reduction, |
20f06221 DN |
1188 | we know that oprnd1 is the reduction variable (defined by a loop-header |
1189 | phi), and oprnd0 is an ssa-name defined by a stmt in the loop body. | |
1190 | Left to check that oprnd0 is defined by a cast from type 'type' to type | |
1191 | 'TYPE'. */ | |
1192 | ||
bc4fb355 IR |
1193 | if (!type_conversion_p (oprnd0, last_stmt, true, &half_type, &stmt, |
1194 | &promotion) | |
1195 | || !promotion) | |
1196 | return NULL; | |
20f06221 | 1197 | |
726a989a | 1198 | oprnd0 = gimple_assign_rhs1 (stmt); |
20f06221 DN |
1199 | *type_in = half_type; |
1200 | *type_out = type; | |
1201 | ||
1202 | /* Pattern detected. Create a stmt to be used to replace the pattern: */ | |
726a989a | 1203 | var = vect_recog_temp_ssa_var (type, NULL); |
0d0e4a03 | 1204 | pattern_stmt = gimple_build_assign (var, WIDEN_SUM_EXPR, oprnd0, oprnd1); |
726a989a | 1205 | |
73fbfcad | 1206 | if (dump_enabled_p ()) |
20f06221 | 1207 | { |
ccb3ad87 | 1208 | dump_printf_loc (MSG_NOTE, vect_location, |
78c60e3d | 1209 | "vect_recog_widen_sum_pattern: detected: "); |
ccb3ad87 | 1210 | dump_gimple_stmt (MSG_NOTE, TDF_SLIM, pattern_stmt, 0); |
20f06221 | 1211 | } |
d29de1bf | 1212 | |
726a989a | 1213 | return pattern_stmt; |
20f06221 DN |
1214 | } |
1215 | ||
1216 | ||
1107f3ae IR |
1217 | /* Return TRUE if the operation in STMT can be performed on a smaller type. |
1218 | ||
1219 | Input: | |
1220 | STMT - a statement to check. | |
1221 | DEF - we support operations with two operands, one of which is constant. | |
1222 | The other operand can be defined by a demotion operation, or by a | |
1223 | previous statement in a sequence of over-promoted operations. In the | |
1224 | later case DEF is used to replace that operand. (It is defined by a | |
1225 | pattern statement we created for the previous statement in the | |
1226 | sequence). | |
1227 | ||
1228 | Input/output: | |
1229 | NEW_TYPE - Output: a smaller type that we are trying to use. Input: if not | |
1230 | NULL, it's the type of DEF. | |
1231 | STMTS - additional pattern statements. If a pattern statement (type | |
1232 | conversion) is created in this function, its original statement is | |
1233 | added to STMTS. | |
1234 | ||
1235 | Output: | |
1236 | OP0, OP1 - if the operation fits a smaller type, OP0 and OP1 are the new | |
1237 | operands to use in the new pattern statement for STMT (will be created | |
1238 | in vect_recog_over_widening_pattern ()). | |
1239 | NEW_DEF_STMT - in case DEF has to be promoted, we create two pattern | |
1240 | statements for STMT: the first one is a type promotion and the second | |
1241 | one is the operation itself. We return the type promotion statement | |
363477c0 | 1242 | in NEW_DEF_STMT and further store it in STMT_VINFO_PATTERN_DEF_SEQ of |
1107f3ae IR |
1243 | the second pattern statement. */ |
1244 | ||
1245 | static bool | |
355fe088 TS |
1246 | vect_operation_fits_smaller_type (gimple *stmt, tree def, tree *new_type, |
1247 | tree *op0, tree *op1, gimple **new_def_stmt, | |
1248 | vec<gimple *> *stmts) | |
1107f3ae IR |
1249 | { |
1250 | enum tree_code code; | |
1251 | tree const_oprnd, oprnd; | |
83d5977e | 1252 | tree interm_type = NULL_TREE, half_type, new_oprnd, type; |
355fe088 | 1253 | gimple *def_stmt, *new_stmt; |
1107f3ae | 1254 | bool first = false; |
bc4fb355 | 1255 | bool promotion; |
f5709183 | 1256 | |
d6e1acf6 JJ |
1257 | *op0 = NULL_TREE; |
1258 | *op1 = NULL_TREE; | |
1107f3ae IR |
1259 | *new_def_stmt = NULL; |
1260 | ||
1261 | if (!is_gimple_assign (stmt)) | |
1262 | return false; | |
1263 | ||
1264 | code = gimple_assign_rhs_code (stmt); | |
1265 | if (code != LSHIFT_EXPR && code != RSHIFT_EXPR | |
1266 | && code != BIT_IOR_EXPR && code != BIT_XOR_EXPR && code != BIT_AND_EXPR) | |
1267 | return false; | |
1268 | ||
1269 | oprnd = gimple_assign_rhs1 (stmt); | |
1270 | const_oprnd = gimple_assign_rhs2 (stmt); | |
1271 | type = gimple_expr_type (stmt); | |
1272 | ||
1273 | if (TREE_CODE (oprnd) != SSA_NAME | |
1274 | || TREE_CODE (const_oprnd) != INTEGER_CST) | |
1275 | return false; | |
1276 | ||
9ef7adc0 RG |
1277 | /* If oprnd has other uses besides that in stmt we cannot mark it |
1278 | as being part of a pattern only. */ | |
1279 | if (!has_single_use (oprnd)) | |
1280 | return false; | |
1281 | ||
1107f3ae IR |
1282 | /* If we are in the middle of a sequence, we use DEF from a previous |
1283 | statement. Otherwise, OPRND has to be a result of type promotion. */ | |
1284 | if (*new_type) | |
1285 | { | |
1286 | half_type = *new_type; | |
1287 | oprnd = def; | |
1288 | } | |
1289 | else | |
1290 | { | |
1291 | first = true; | |
bc4fb355 | 1292 | if (!type_conversion_p (oprnd, stmt, false, &half_type, &def_stmt, |
f71cf56a UW |
1293 | &promotion) |
1294 | || !promotion | |
1295 | || !vect_same_loop_or_bb_p (stmt, def_stmt)) | |
1107f3ae IR |
1296 | return false; |
1297 | } | |
1298 | ||
1299 | /* Can we perform the operation on a smaller type? */ | |
1300 | switch (code) | |
1301 | { | |
1302 | case BIT_IOR_EXPR: | |
1303 | case BIT_XOR_EXPR: | |
1304 | case BIT_AND_EXPR: | |
1305 | if (!int_fits_type_p (const_oprnd, half_type)) | |
1306 | { | |
1307 | /* HALF_TYPE is not enough. Try a bigger type if possible. */ | |
1308 | if (TYPE_PRECISION (type) < (TYPE_PRECISION (half_type) * 4)) | |
1309 | return false; | |
1310 | ||
1311 | interm_type = build_nonstandard_integer_type ( | |
1312 | TYPE_PRECISION (half_type) * 2, TYPE_UNSIGNED (type)); | |
1313 | if (!int_fits_type_p (const_oprnd, interm_type)) | |
1314 | return false; | |
1315 | } | |
1316 | ||
1317 | break; | |
1318 | ||
1319 | case LSHIFT_EXPR: | |
1320 | /* Try intermediate type - HALF_TYPE is not enough for sure. */ | |
1321 | if (TYPE_PRECISION (type) < (TYPE_PRECISION (half_type) * 4)) | |
1322 | return false; | |
1323 | ||
1324 | /* Check that HALF_TYPE size + shift amount <= INTERM_TYPE size. | |
1325 | (e.g., if the original value was char, the shift amount is at most 8 | |
1326 | if we want to use short). */ | |
1327 | if (compare_tree_int (const_oprnd, TYPE_PRECISION (half_type)) == 1) | |
1328 | return false; | |
1329 | ||
1330 | interm_type = build_nonstandard_integer_type ( | |
1331 | TYPE_PRECISION (half_type) * 2, TYPE_UNSIGNED (type)); | |
1332 | ||
1333 | if (!vect_supportable_shift (code, interm_type)) | |
1334 | return false; | |
1335 | ||
1336 | break; | |
1337 | ||
1338 | case RSHIFT_EXPR: | |
1339 | if (vect_supportable_shift (code, half_type)) | |
1340 | break; | |
1341 | ||
1342 | /* Try intermediate type - HALF_TYPE is not supported. */ | |
1343 | if (TYPE_PRECISION (type) < (TYPE_PRECISION (half_type) * 4)) | |
1344 | return false; | |
1345 | ||
1346 | interm_type = build_nonstandard_integer_type ( | |
1347 | TYPE_PRECISION (half_type) * 2, TYPE_UNSIGNED (type)); | |
1348 | ||
1349 | if (!vect_supportable_shift (code, interm_type)) | |
1350 | return false; | |
1351 | ||
1352 | break; | |
1353 | ||
1354 | default: | |
1355 | gcc_unreachable (); | |
1356 | } | |
1357 | ||
1358 | /* There are four possible cases: | |
1359 | 1. OPRND is defined by a type promotion (in that case FIRST is TRUE, it's | |
1360 | the first statement in the sequence) | |
1361 | a. The original, HALF_TYPE, is not enough - we replace the promotion | |
1362 | from HALF_TYPE to TYPE with a promotion to INTERM_TYPE. | |
1363 | b. HALF_TYPE is sufficient, OPRND is set as the RHS of the original | |
1364 | promotion. | |
1365 | 2. OPRND is defined by a pattern statement we created. | |
1366 | a. Its type is not sufficient for the operation, we create a new stmt: | |
1367 | a type conversion for OPRND from HALF_TYPE to INTERM_TYPE. We store | |
1368 | this statement in NEW_DEF_STMT, and it is later put in | |
363477c0 | 1369 | STMT_VINFO_PATTERN_DEF_SEQ of the pattern statement for STMT. |
1107f3ae IR |
1370 | b. OPRND is good to use in the new statement. */ |
1371 | if (first) | |
1372 | { | |
1373 | if (interm_type) | |
1374 | { | |
1375 | /* Replace the original type conversion HALF_TYPE->TYPE with | |
1376 | HALF_TYPE->INTERM_TYPE. */ | |
1377 | if (STMT_VINFO_RELATED_STMT (vinfo_for_stmt (def_stmt))) | |
1378 | { | |
1379 | new_stmt = STMT_VINFO_RELATED_STMT (vinfo_for_stmt (def_stmt)); | |
1380 | /* Check if the already created pattern stmt is what we need. */ | |
1381 | if (!is_gimple_assign (new_stmt) | |
625a9766 | 1382 | || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (new_stmt)) |
1107f3ae IR |
1383 | || TREE_TYPE (gimple_assign_lhs (new_stmt)) != interm_type) |
1384 | return false; | |
1385 | ||
9771b263 | 1386 | stmts->safe_push (def_stmt); |
1107f3ae IR |
1387 | oprnd = gimple_assign_lhs (new_stmt); |
1388 | } | |
1389 | else | |
1390 | { | |
1391 | /* Create NEW_OPRND = (INTERM_TYPE) OPRND. */ | |
1392 | oprnd = gimple_assign_rhs1 (def_stmt); | |
b731b390 | 1393 | new_oprnd = make_ssa_name (interm_type); |
0d0e4a03 | 1394 | new_stmt = gimple_build_assign (new_oprnd, NOP_EXPR, oprnd); |
1107f3ae | 1395 | STMT_VINFO_RELATED_STMT (vinfo_for_stmt (def_stmt)) = new_stmt; |
9771b263 | 1396 | stmts->safe_push (def_stmt); |
1107f3ae IR |
1397 | oprnd = new_oprnd; |
1398 | } | |
1399 | } | |
1400 | else | |
1401 | { | |
1402 | /* Retrieve the operand before the type promotion. */ | |
1403 | oprnd = gimple_assign_rhs1 (def_stmt); | |
1404 | } | |
1405 | } | |
1406 | else | |
1407 | { | |
1408 | if (interm_type) | |
1409 | { | |
1410 | /* Create a type conversion HALF_TYPE->INTERM_TYPE. */ | |
b731b390 | 1411 | new_oprnd = make_ssa_name (interm_type); |
0d0e4a03 | 1412 | new_stmt = gimple_build_assign (new_oprnd, NOP_EXPR, oprnd); |
1107f3ae IR |
1413 | oprnd = new_oprnd; |
1414 | *new_def_stmt = new_stmt; | |
1415 | } | |
1416 | ||
1417 | /* Otherwise, OPRND is already set. */ | |
1418 | } | |
1419 | ||
1420 | if (interm_type) | |
1421 | *new_type = interm_type; | |
1422 | else | |
1423 | *new_type = half_type; | |
1424 | ||
1425 | *op0 = oprnd; | |
1426 | *op1 = fold_convert (*new_type, const_oprnd); | |
1427 | ||
1428 | return true; | |
1429 | } | |
1430 | ||
1431 | ||
1432 | /* Try to find a statement or a sequence of statements that can be performed | |
1433 | on a smaller type: | |
1434 | ||
1435 | type x_t; | |
1436 | TYPE x_T, res0_T, res1_T; | |
1437 | loop: | |
1438 | S1 x_t = *p; | |
1439 | S2 x_T = (TYPE) x_t; | |
1440 | S3 res0_T = op (x_T, C0); | |
1441 | S4 res1_T = op (res0_T, C1); | |
1442 | S5 ... = () res1_T; - type demotion | |
1443 | ||
1444 | where type 'TYPE' is at least double the size of type 'type', C0 and C1 are | |
1445 | constants. | |
1446 | Check if S3 and S4 can be done on a smaller type than 'TYPE', it can either | |
1447 | be 'type' or some intermediate type. For now, we expect S5 to be a type | |
71c92d17 | 1448 | demotion operation. We also check that S3 and S4 have only one use. */ |
1107f3ae | 1449 | |
355fe088 TS |
1450 | static gimple * |
1451 | vect_recog_over_widening_pattern (vec<gimple *> *stmts, | |
1107f3ae IR |
1452 | tree *type_in, tree *type_out) |
1453 | { | |
355fe088 TS |
1454 | gimple *stmt = stmts->pop (); |
1455 | gimple *pattern_stmt = NULL, *new_def_stmt, *prev_stmt = NULL, | |
1456 | *use_stmt = NULL; | |
9a7a4398 | 1457 | tree op0, op1, vectype = NULL_TREE, use_lhs, use_type; |
83d5977e | 1458 | tree var = NULL_TREE, new_type = NULL_TREE, new_oprnd; |
1107f3ae | 1459 | bool first; |
b2a1a74d | 1460 | tree type = NULL; |
1107f3ae IR |
1461 | |
1462 | first = true; | |
1463 | while (1) | |
1464 | { | |
1465 | if (!vinfo_for_stmt (stmt) | |
1466 | || STMT_VINFO_IN_PATTERN_P (vinfo_for_stmt (stmt))) | |
1467 | return NULL; | |
1468 | ||
1469 | new_def_stmt = NULL; | |
1470 | if (!vect_operation_fits_smaller_type (stmt, var, &new_type, | |
1471 | &op0, &op1, &new_def_stmt, | |
1472 | stmts)) | |
1473 | { | |
1474 | if (first) | |
1475 | return NULL; | |
1476 | else | |
1477 | break; | |
1478 | } | |
1479 | ||
1480 | /* STMT can be performed on a smaller type. Check its uses. */ | |
9a7a4398 UW |
1481 | use_stmt = vect_single_imm_use (stmt); |
1482 | if (!use_stmt || !is_gimple_assign (use_stmt)) | |
1107f3ae IR |
1483 | return NULL; |
1484 | ||
1485 | /* Create pattern statement for STMT. */ | |
1486 | vectype = get_vectype_for_scalar_type (new_type); | |
1487 | if (!vectype) | |
1488 | return NULL; | |
1489 | ||
1490 | /* We want to collect all the statements for which we create pattern | |
1491 | statetments, except for the case when the last statement in the | |
1492 | sequence doesn't have a corresponding pattern statement. In such | |
1493 | case we associate the last pattern statement with the last statement | |
36ba4aae | 1494 | in the sequence. Therefore, we only add the original statement to |
1107f3ae IR |
1495 | the list if we know that it is not the last. */ |
1496 | if (prev_stmt) | |
9771b263 | 1497 | stmts->safe_push (prev_stmt); |
1107f3ae IR |
1498 | |
1499 | var = vect_recog_temp_ssa_var (new_type, NULL); | |
62371b92 | 1500 | pattern_stmt |
0d0e4a03 | 1501 | = gimple_build_assign (var, gimple_assign_rhs_code (stmt), op0, op1); |
1107f3ae | 1502 | STMT_VINFO_RELATED_STMT (vinfo_for_stmt (stmt)) = pattern_stmt; |
083481d8 | 1503 | new_pattern_def_seq (vinfo_for_stmt (stmt), new_def_stmt); |
1107f3ae | 1504 | |
73fbfcad | 1505 | if (dump_enabled_p ()) |
1107f3ae | 1506 | { |
ccb3ad87 | 1507 | dump_printf_loc (MSG_NOTE, vect_location, |
78c60e3d | 1508 | "created pattern stmt: "); |
ccb3ad87 | 1509 | dump_gimple_stmt (MSG_NOTE, TDF_SLIM, pattern_stmt, 0); |
1107f3ae IR |
1510 | } |
1511 | ||
b2a1a74d | 1512 | type = gimple_expr_type (stmt); |
1107f3ae IR |
1513 | prev_stmt = stmt; |
1514 | stmt = use_stmt; | |
1515 | ||
1516 | first = false; | |
1517 | } | |
1518 | ||
1519 | /* We got a sequence. We expect it to end with a type demotion operation. | |
1520 | Otherwise, we quit (for now). There are three possible cases: the | |
1521 | conversion is to NEW_TYPE (we don't do anything), the conversion is to | |
1522 | a type bigger than NEW_TYPE and/or the signedness of USE_TYPE and | |
1523 | NEW_TYPE differs (we create a new conversion statement). */ | |
1524 | if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (use_stmt))) | |
1525 | { | |
1526 | use_lhs = gimple_assign_lhs (use_stmt); | |
1527 | use_type = TREE_TYPE (use_lhs); | |
82db3d43 | 1528 | /* Support only type demotion or signedess change. */ |
1107f3ae | 1529 | if (!INTEGRAL_TYPE_P (use_type) |
82db3d43 | 1530 | || TYPE_PRECISION (type) <= TYPE_PRECISION (use_type)) |
1107f3ae IR |
1531 | return NULL; |
1532 | ||
82db3d43 IR |
1533 | /* Check that NEW_TYPE is not bigger than the conversion result. */ |
1534 | if (TYPE_PRECISION (new_type) > TYPE_PRECISION (use_type)) | |
1535 | return NULL; | |
1536 | ||
1107f3ae IR |
1537 | if (TYPE_UNSIGNED (new_type) != TYPE_UNSIGNED (use_type) |
1538 | || TYPE_PRECISION (new_type) != TYPE_PRECISION (use_type)) | |
1539 | { | |
1540 | /* Create NEW_TYPE->USE_TYPE conversion. */ | |
b731b390 | 1541 | new_oprnd = make_ssa_name (use_type); |
0d0e4a03 | 1542 | pattern_stmt = gimple_build_assign (new_oprnd, NOP_EXPR, var); |
1107f3ae IR |
1543 | STMT_VINFO_RELATED_STMT (vinfo_for_stmt (use_stmt)) = pattern_stmt; |
1544 | ||
1545 | *type_in = get_vectype_for_scalar_type (new_type); | |
1546 | *type_out = get_vectype_for_scalar_type (use_type); | |
1547 | ||
1548 | /* We created a pattern statement for the last statement in the | |
1549 | sequence, so we don't need to associate it with the pattern | |
1550 | statement created for PREV_STMT. Therefore, we add PREV_STMT | |
1551 | to the list in order to mark it later in vect_pattern_recog_1. */ | |
1552 | if (prev_stmt) | |
9771b263 | 1553 | stmts->safe_push (prev_stmt); |
1107f3ae IR |
1554 | } |
1555 | else | |
1556 | { | |
1557 | if (prev_stmt) | |
363477c0 JJ |
1558 | STMT_VINFO_PATTERN_DEF_SEQ (vinfo_for_stmt (use_stmt)) |
1559 | = STMT_VINFO_PATTERN_DEF_SEQ (vinfo_for_stmt (prev_stmt)); | |
1107f3ae IR |
1560 | |
1561 | *type_in = vectype; | |
1562 | *type_out = NULL_TREE; | |
1563 | } | |
1564 | ||
9771b263 | 1565 | stmts->safe_push (use_stmt); |
1107f3ae IR |
1566 | } |
1567 | else | |
1568 | /* TODO: support general case, create a conversion to the correct type. */ | |
1569 | return NULL; | |
1570 | ||
1571 | /* Pattern detected. */ | |
73fbfcad | 1572 | if (dump_enabled_p ()) |
1107f3ae | 1573 | { |
ccb3ad87 | 1574 | dump_printf_loc (MSG_NOTE, vect_location, |
78c60e3d | 1575 | "vect_recog_over_widening_pattern: detected: "); |
ccb3ad87 | 1576 | dump_gimple_stmt (MSG_NOTE, TDF_SLIM, pattern_stmt, 0); |
1107f3ae IR |
1577 | } |
1578 | ||
1579 | return pattern_stmt; | |
1580 | } | |
1581 | ||
36ba4aae IR |
1582 | /* Detect widening shift pattern: |
1583 | ||
1584 | type a_t; | |
1585 | TYPE a_T, res_T; | |
1586 | ||
1587 | S1 a_t = ; | |
1588 | S2 a_T = (TYPE) a_t; | |
1589 | S3 res_T = a_T << CONST; | |
1590 | ||
1591 | where type 'TYPE' is at least double the size of type 'type'. | |
1592 | ||
33018845 UW |
1593 | Also detect cases where the shift result is immediately converted |
1594 | to another type 'result_type' that is no larger in size than 'TYPE'. | |
1595 | In those cases we perform a widen-shift that directly results in | |
1596 | 'result_type', to avoid a possible over-widening situation: | |
36ba4aae | 1597 | |
33018845 | 1598 | type a_t; |
36ba4aae | 1599 | TYPE a_T, res_T; |
33018845 | 1600 | result_type res_result; |
36ba4aae IR |
1601 | |
1602 | S1 a_t = ; | |
1603 | S2 a_T = (TYPE) a_t; | |
1604 | S3 res_T = a_T << CONST; | |
33018845 UW |
1605 | S4 res_result = (result_type) res_T; |
1606 | '--> res_result' = a_t w<< CONST; | |
36ba4aae IR |
1607 | |
1608 | And a case when 'TYPE' is 4 times bigger than 'type'. In that case we | |
1609 | create an additional pattern stmt for S2 to create a variable of an | |
1610 | intermediate type, and perform widen-shift on the intermediate type: | |
1611 | ||
1612 | type a_t; | |
1613 | interm_type a_it; | |
1614 | TYPE a_T, res_T, res_T'; | |
1615 | ||
1616 | S1 a_t = ; | |
1617 | S2 a_T = (TYPE) a_t; | |
1618 | '--> a_it = (interm_type) a_t; | |
1619 | S3 res_T = a_T << CONST; | |
1620 | '--> res_T' = a_it <<* CONST; | |
1621 | ||
1622 | Input/Output: | |
1623 | ||
1624 | * STMTS: Contains a stmt from which the pattern search begins. | |
1625 | In case of unsigned widen-shift, the original stmt (S3) is replaced with S4 | |
1626 | in STMTS. When an intermediate type is used and a pattern statement is | |
1627 | created for S2, we also put S2 here (before S3). | |
1628 | ||
1629 | Output: | |
1630 | ||
1631 | * TYPE_IN: The type of the input arguments to the pattern. | |
1632 | ||
1633 | * TYPE_OUT: The type of the output of this pattern. | |
1634 | ||
1635 | * Return value: A new stmt that will be used to replace the sequence of | |
1636 | stmts that constitute the pattern. In this case it will be: | |
1637 | WIDEN_LSHIFT_EXPR <a_t, CONST>. */ | |
1638 | ||
355fe088 TS |
1639 | static gimple * |
1640 | vect_recog_widen_shift_pattern (vec<gimple *> *stmts, | |
36ba4aae IR |
1641 | tree *type_in, tree *type_out) |
1642 | { | |
355fe088 TS |
1643 | gimple *last_stmt = stmts->pop (); |
1644 | gimple *def_stmt0; | |
36ba4aae IR |
1645 | tree oprnd0, oprnd1; |
1646 | tree type, half_type0; | |
355fe088 | 1647 | gimple *pattern_stmt; |
36ba4aae | 1648 | tree vectype, vectype_out = NULL_TREE; |
36ba4aae IR |
1649 | tree var; |
1650 | enum tree_code dummy_code; | |
1651 | int dummy_int; | |
9771b263 | 1652 | vec<tree> dummy_vec; |
355fe088 | 1653 | gimple *use_stmt; |
bc4fb355 | 1654 | bool promotion; |
36ba4aae IR |
1655 | |
1656 | if (!is_gimple_assign (last_stmt) || !vinfo_for_stmt (last_stmt)) | |
1657 | return NULL; | |
1658 | ||
36ba4aae | 1659 | if (STMT_VINFO_IN_PATTERN_P (vinfo_for_stmt (last_stmt))) |
33018845 | 1660 | return NULL; |
36ba4aae IR |
1661 | |
1662 | if (gimple_assign_rhs_code (last_stmt) != LSHIFT_EXPR) | |
1663 | return NULL; | |
1664 | ||
1665 | oprnd0 = gimple_assign_rhs1 (last_stmt); | |
1666 | oprnd1 = gimple_assign_rhs2 (last_stmt); | |
1667 | if (TREE_CODE (oprnd0) != SSA_NAME || TREE_CODE (oprnd1) != INTEGER_CST) | |
1668 | return NULL; | |
1669 | ||
1670 | /* Check operand 0: it has to be defined by a type promotion. */ | |
bc4fb355 | 1671 | if (!type_conversion_p (oprnd0, last_stmt, false, &half_type0, &def_stmt0, |
566d377a | 1672 | &promotion) |
bc4fb355 IR |
1673 | || !promotion) |
1674 | return NULL; | |
36ba4aae IR |
1675 | |
1676 | /* Check operand 1: has to be positive. We check that it fits the type | |
1677 | in vect_handle_widen_op_by_const (). */ | |
1678 | if (tree_int_cst_compare (oprnd1, size_zero_node) <= 0) | |
1679 | return NULL; | |
1680 | ||
1681 | oprnd0 = gimple_assign_rhs1 (def_stmt0); | |
1682 | type = gimple_expr_type (last_stmt); | |
1683 | ||
33018845 UW |
1684 | /* Check for subsequent conversion to another type. */ |
1685 | use_stmt = vect_single_imm_use (last_stmt); | |
1686 | if (use_stmt && is_gimple_assign (use_stmt) | |
1687 | && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (use_stmt)) | |
1688 | && !STMT_VINFO_IN_PATTERN_P (vinfo_for_stmt (use_stmt))) | |
1689 | { | |
1690 | tree use_lhs = gimple_assign_lhs (use_stmt); | |
1691 | tree use_type = TREE_TYPE (use_lhs); | |
1692 | ||
1693 | if (INTEGRAL_TYPE_P (use_type) | |
1694 | && TYPE_PRECISION (use_type) <= TYPE_PRECISION (type)) | |
1695 | { | |
1696 | last_stmt = use_stmt; | |
1697 | type = use_type; | |
1698 | } | |
1699 | } | |
1700 | ||
36ba4aae | 1701 | /* Check if this a widening operation. */ |
355fe088 | 1702 | gimple *wstmt = NULL; |
36ba4aae | 1703 | if (!vect_handle_widen_op_by_const (last_stmt, LSHIFT_EXPR, oprnd1, |
566d377a | 1704 | &oprnd0, &wstmt, |
36ba4aae IR |
1705 | type, &half_type0, def_stmt0)) |
1706 | return NULL; | |
1707 | ||
36ba4aae | 1708 | /* Pattern detected. */ |
73fbfcad | 1709 | if (dump_enabled_p ()) |
ccb3ad87 | 1710 | dump_printf_loc (MSG_NOTE, vect_location, |
e645e942 | 1711 | "vect_recog_widen_shift_pattern: detected:\n"); |
36ba4aae IR |
1712 | |
1713 | /* Check target support. */ | |
1714 | vectype = get_vectype_for_scalar_type (half_type0); | |
1715 | vectype_out = get_vectype_for_scalar_type (type); | |
1716 | ||
1717 | if (!vectype | |
1718 | || !vectype_out | |
1719 | || !supportable_widening_operation (WIDEN_LSHIFT_EXPR, last_stmt, | |
1720 | vectype_out, vectype, | |
a86ec597 RH |
1721 | &dummy_code, &dummy_code, |
1722 | &dummy_int, &dummy_vec)) | |
36ba4aae IR |
1723 | return NULL; |
1724 | ||
1725 | *type_in = vectype; | |
1726 | *type_out = vectype_out; | |
1727 | ||
1728 | /* Pattern supported. Create a stmt to be used to replace the pattern. */ | |
1729 | var = vect_recog_temp_ssa_var (type, NULL); | |
1730 | pattern_stmt = | |
0d0e4a03 | 1731 | gimple_build_assign (var, WIDEN_LSHIFT_EXPR, oprnd0, oprnd1); |
566d377a RB |
1732 | if (wstmt) |
1733 | { | |
1734 | stmt_vec_info stmt_vinfo = vinfo_for_stmt (last_stmt); | |
566d377a RB |
1735 | new_pattern_def_seq (stmt_vinfo, wstmt); |
1736 | stmt_vec_info new_stmt_info | |
310213d4 | 1737 | = new_stmt_vec_info (wstmt, stmt_vinfo->vinfo); |
566d377a RB |
1738 | set_vinfo_for_stmt (wstmt, new_stmt_info); |
1739 | STMT_VINFO_VECTYPE (new_stmt_info) = vectype; | |
1740 | } | |
36ba4aae | 1741 | |
73fbfcad | 1742 | if (dump_enabled_p ()) |
78c60e3d | 1743 | dump_gimple_stmt_loc (MSG_NOTE, vect_location, TDF_SLIM, pattern_stmt, 0); |
7e9a3abb JJ |
1744 | |
1745 | stmts->safe_push (last_stmt); | |
1746 | return pattern_stmt; | |
1747 | } | |
1748 | ||
1749 | /* Detect a rotate pattern wouldn't be otherwise vectorized: | |
1750 | ||
1751 | type a_t, b_t, c_t; | |
1752 | ||
1753 | S0 a_t = b_t r<< c_t; | |
1754 | ||
1755 | Input/Output: | |
1756 | ||
1757 | * STMTS: Contains a stmt from which the pattern search begins, | |
1758 | i.e. the shift/rotate stmt. The original stmt (S0) is replaced | |
1759 | with a sequence: | |
1760 | ||
1761 | S1 d_t = -c_t; | |
1762 | S2 e_t = d_t & (B - 1); | |
1763 | S3 f_t = b_t << c_t; | |
1764 | S4 g_t = b_t >> e_t; | |
1765 | S0 a_t = f_t | g_t; | |
1766 | ||
1767 | where B is element bitsize of type. | |
1768 | ||
1769 | Output: | |
1770 | ||
1771 | * TYPE_IN: The type of the input arguments to the pattern. | |
1772 | ||
1773 | * TYPE_OUT: The type of the output of this pattern. | |
1774 | ||
1775 | * Return value: A new stmt that will be used to replace the rotate | |
1776 | S0 stmt. */ | |
1777 | ||
355fe088 TS |
1778 | static gimple * |
1779 | vect_recog_rotate_pattern (vec<gimple *> *stmts, tree *type_in, tree *type_out) | |
7e9a3abb | 1780 | { |
355fe088 | 1781 | gimple *last_stmt = stmts->pop (); |
7e9a3abb | 1782 | tree oprnd0, oprnd1, lhs, var, var1, var2, vectype, type, stype, def, def2; |
355fe088 | 1783 | gimple *pattern_stmt, *def_stmt; |
7e9a3abb JJ |
1784 | enum tree_code rhs_code; |
1785 | stmt_vec_info stmt_vinfo = vinfo_for_stmt (last_stmt); | |
310213d4 | 1786 | vec_info *vinfo = stmt_vinfo->vinfo; |
7e9a3abb JJ |
1787 | enum vect_def_type dt; |
1788 | optab optab1, optab2; | |
68119618 | 1789 | edge ext_def = NULL; |
7e9a3abb JJ |
1790 | |
1791 | if (!is_gimple_assign (last_stmt)) | |
1792 | return NULL; | |
1793 | ||
1794 | rhs_code = gimple_assign_rhs_code (last_stmt); | |
1795 | switch (rhs_code) | |
1796 | { | |
1797 | case LROTATE_EXPR: | |
1798 | case RROTATE_EXPR: | |
1799 | break; | |
1800 | default: | |
1801 | return NULL; | |
1802 | } | |
1803 | ||
1804 | if (STMT_VINFO_IN_PATTERN_P (stmt_vinfo)) | |
1805 | return NULL; | |
1806 | ||
1807 | lhs = gimple_assign_lhs (last_stmt); | |
1808 | oprnd0 = gimple_assign_rhs1 (last_stmt); | |
1809 | type = TREE_TYPE (oprnd0); | |
1810 | oprnd1 = gimple_assign_rhs2 (last_stmt); | |
1811 | if (TREE_CODE (oprnd0) != SSA_NAME | |
1812 | || TYPE_PRECISION (TREE_TYPE (lhs)) != TYPE_PRECISION (type) | |
1813 | || !INTEGRAL_TYPE_P (type) | |
1814 | || !TYPE_UNSIGNED (type)) | |
1815 | return NULL; | |
1816 | ||
81c40241 | 1817 | if (!vect_is_simple_use (oprnd1, vinfo, &def_stmt, &dt)) |
7e9a3abb JJ |
1818 | return NULL; |
1819 | ||
1820 | if (dt != vect_internal_def | |
1821 | && dt != vect_constant_def | |
1822 | && dt != vect_external_def) | |
1823 | return NULL; | |
1824 | ||
1825 | vectype = get_vectype_for_scalar_type (type); | |
1826 | if (vectype == NULL_TREE) | |
1827 | return NULL; | |
1828 | ||
1829 | /* If vector/vector or vector/scalar rotate is supported by the target, | |
1830 | don't do anything here. */ | |
1831 | optab1 = optab_for_tree_code (rhs_code, vectype, optab_vector); | |
1832 | if (optab1 | |
1833 | && optab_handler (optab1, TYPE_MODE (vectype)) != CODE_FOR_nothing) | |
1834 | return NULL; | |
1835 | ||
310213d4 | 1836 | if (is_a <bb_vec_info> (vinfo) || dt != vect_internal_def) |
7e9a3abb JJ |
1837 | { |
1838 | optab2 = optab_for_tree_code (rhs_code, vectype, optab_scalar); | |
1839 | if (optab2 | |
1840 | && optab_handler (optab2, TYPE_MODE (vectype)) != CODE_FOR_nothing) | |
1841 | return NULL; | |
1842 | } | |
1843 | ||
1844 | /* If vector/vector or vector/scalar shifts aren't supported by the target, | |
1845 | don't do anything here either. */ | |
1846 | optab1 = optab_for_tree_code (LSHIFT_EXPR, vectype, optab_vector); | |
1847 | optab2 = optab_for_tree_code (RSHIFT_EXPR, vectype, optab_vector); | |
1848 | if (!optab1 | |
1849 | || optab_handler (optab1, TYPE_MODE (vectype)) == CODE_FOR_nothing | |
1850 | || !optab2 | |
1851 | || optab_handler (optab2, TYPE_MODE (vectype)) == CODE_FOR_nothing) | |
1852 | { | |
310213d4 | 1853 | if (! is_a <bb_vec_info> (vinfo) && dt == vect_internal_def) |
7e9a3abb JJ |
1854 | return NULL; |
1855 | optab1 = optab_for_tree_code (LSHIFT_EXPR, vectype, optab_scalar); | |
1856 | optab2 = optab_for_tree_code (RSHIFT_EXPR, vectype, optab_scalar); | |
1857 | if (!optab1 | |
1858 | || optab_handler (optab1, TYPE_MODE (vectype)) == CODE_FOR_nothing | |
1859 | || !optab2 | |
1860 | || optab_handler (optab2, TYPE_MODE (vectype)) == CODE_FOR_nothing) | |
1861 | return NULL; | |
1862 | } | |
1863 | ||
1864 | *type_in = vectype; | |
1865 | *type_out = vectype; | |
1866 | if (*type_in == NULL_TREE) | |
1867 | return NULL; | |
1868 | ||
68119618 JJ |
1869 | if (dt == vect_external_def |
1870 | && TREE_CODE (oprnd1) == SSA_NAME | |
310213d4 | 1871 | && is_a <loop_vec_info> (vinfo)) |
68119618 | 1872 | { |
310213d4 | 1873 | struct loop *loop = as_a <loop_vec_info> (vinfo)->loop; |
68119618 JJ |
1874 | ext_def = loop_preheader_edge (loop); |
1875 | if (!SSA_NAME_IS_DEFAULT_DEF (oprnd1)) | |
1876 | { | |
1877 | basic_block bb = gimple_bb (SSA_NAME_DEF_STMT (oprnd1)); | |
1878 | if (bb == NULL | |
1879 | || !dominated_by_p (CDI_DOMINATORS, ext_def->dest, bb)) | |
1880 | ext_def = NULL; | |
1881 | } | |
1882 | } | |
1883 | ||
7e9a3abb JJ |
1884 | def = NULL_TREE; |
1885 | if (TREE_CODE (oprnd1) == INTEGER_CST | |
1886 | || TYPE_MODE (TREE_TYPE (oprnd1)) == TYPE_MODE (type)) | |
1887 | def = oprnd1; | |
1888 | else if (def_stmt && gimple_assign_cast_p (def_stmt)) | |
1889 | { | |
1890 | tree rhs1 = gimple_assign_rhs1 (def_stmt); | |
1891 | if (TYPE_MODE (TREE_TYPE (rhs1)) == TYPE_MODE (type) | |
1892 | && TYPE_PRECISION (TREE_TYPE (rhs1)) | |
1893 | == TYPE_PRECISION (type)) | |
1894 | def = rhs1; | |
1895 | } | |
1896 | ||
1897 | STMT_VINFO_PATTERN_DEF_SEQ (stmt_vinfo) = NULL; | |
1898 | if (def == NULL_TREE) | |
1899 | { | |
1900 | def = vect_recog_temp_ssa_var (type, NULL); | |
0d0e4a03 | 1901 | def_stmt = gimple_build_assign (def, NOP_EXPR, oprnd1); |
68119618 JJ |
1902 | if (ext_def) |
1903 | { | |
1904 | basic_block new_bb | |
1905 | = gsi_insert_on_edge_immediate (ext_def, def_stmt); | |
1906 | gcc_assert (!new_bb); | |
1907 | } | |
1908 | else | |
1909 | append_pattern_def_seq (stmt_vinfo, def_stmt); | |
7e9a3abb JJ |
1910 | } |
1911 | stype = TREE_TYPE (def); | |
1912 | ||
1913 | if (TREE_CODE (def) == INTEGER_CST) | |
1914 | { | |
cc269bb6 | 1915 | if (!tree_fits_uhwi_p (def) |
7d362f6c | 1916 | || tree_to_uhwi (def) >= GET_MODE_PRECISION (TYPE_MODE (type)) |
7e9a3abb JJ |
1917 | || integer_zerop (def)) |
1918 | return NULL; | |
1919 | def2 = build_int_cst (stype, | |
1920 | GET_MODE_PRECISION (TYPE_MODE (type)) | |
ae7e9ddd | 1921 | - tree_to_uhwi (def)); |
7e9a3abb JJ |
1922 | } |
1923 | else | |
1924 | { | |
1925 | tree vecstype = get_vectype_for_scalar_type (stype); | |
1926 | stmt_vec_info def_stmt_vinfo; | |
1927 | ||
1928 | if (vecstype == NULL_TREE) | |
1929 | return NULL; | |
1930 | def2 = vect_recog_temp_ssa_var (stype, NULL); | |
0d0e4a03 | 1931 | def_stmt = gimple_build_assign (def2, NEGATE_EXPR, def); |
68119618 JJ |
1932 | if (ext_def) |
1933 | { | |
1934 | basic_block new_bb | |
1935 | = gsi_insert_on_edge_immediate (ext_def, def_stmt); | |
1936 | gcc_assert (!new_bb); | |
1937 | } | |
1938 | else | |
1939 | { | |
310213d4 | 1940 | def_stmt_vinfo = new_stmt_vec_info (def_stmt, vinfo); |
68119618 JJ |
1941 | set_vinfo_for_stmt (def_stmt, def_stmt_vinfo); |
1942 | STMT_VINFO_VECTYPE (def_stmt_vinfo) = vecstype; | |
1943 | append_pattern_def_seq (stmt_vinfo, def_stmt); | |
1944 | } | |
7e9a3abb JJ |
1945 | |
1946 | def2 = vect_recog_temp_ssa_var (stype, NULL); | |
1947 | tree mask | |
1948 | = build_int_cst (stype, GET_MODE_PRECISION (TYPE_MODE (stype)) - 1); | |
0d0e4a03 JJ |
1949 | def_stmt = gimple_build_assign (def2, BIT_AND_EXPR, |
1950 | gimple_assign_lhs (def_stmt), mask); | |
68119618 JJ |
1951 | if (ext_def) |
1952 | { | |
1953 | basic_block new_bb | |
1954 | = gsi_insert_on_edge_immediate (ext_def, def_stmt); | |
1955 | gcc_assert (!new_bb); | |
1956 | } | |
1957 | else | |
1958 | { | |
310213d4 | 1959 | def_stmt_vinfo = new_stmt_vec_info (def_stmt, vinfo); |
68119618 JJ |
1960 | set_vinfo_for_stmt (def_stmt, def_stmt_vinfo); |
1961 | STMT_VINFO_VECTYPE (def_stmt_vinfo) = vecstype; | |
1962 | append_pattern_def_seq (stmt_vinfo, def_stmt); | |
1963 | } | |
7e9a3abb JJ |
1964 | } |
1965 | ||
1966 | var1 = vect_recog_temp_ssa_var (type, NULL); | |
0d0e4a03 JJ |
1967 | def_stmt = gimple_build_assign (var1, rhs_code == LROTATE_EXPR |
1968 | ? LSHIFT_EXPR : RSHIFT_EXPR, | |
1969 | oprnd0, def); | |
7e9a3abb JJ |
1970 | append_pattern_def_seq (stmt_vinfo, def_stmt); |
1971 | ||
1972 | var2 = vect_recog_temp_ssa_var (type, NULL); | |
0d0e4a03 JJ |
1973 | def_stmt = gimple_build_assign (var2, rhs_code == LROTATE_EXPR |
1974 | ? RSHIFT_EXPR : LSHIFT_EXPR, | |
1975 | oprnd0, def2); | |
7e9a3abb JJ |
1976 | append_pattern_def_seq (stmt_vinfo, def_stmt); |
1977 | ||
1978 | /* Pattern detected. */ | |
1979 | if (dump_enabled_p ()) | |
1980 | dump_printf_loc (MSG_NOTE, vect_location, | |
e645e942 | 1981 | "vect_recog_rotate_pattern: detected:\n"); |
7e9a3abb JJ |
1982 | |
1983 | /* Pattern supported. Create a stmt to be used to replace the pattern. */ | |
1984 | var = vect_recog_temp_ssa_var (type, NULL); | |
0d0e4a03 | 1985 | pattern_stmt = gimple_build_assign (var, BIT_IOR_EXPR, var1, var2); |
7e9a3abb JJ |
1986 | |
1987 | if (dump_enabled_p ()) | |
1988 | dump_gimple_stmt_loc (MSG_NOTE, vect_location, TDF_SLIM, pattern_stmt, 0); | |
36ba4aae | 1989 | |
9771b263 | 1990 | stmts->safe_push (last_stmt); |
36ba4aae IR |
1991 | return pattern_stmt; |
1992 | } | |
1107f3ae | 1993 | |
732a0ad3 JJ |
1994 | /* Detect a vector by vector shift pattern that wouldn't be otherwise |
1995 | vectorized: | |
1996 | ||
1997 | type a_t; | |
1998 | TYPE b_T, res_T; | |
1999 | ||
2000 | S1 a_t = ; | |
2001 | S2 b_T = ; | |
2002 | S3 res_T = b_T op a_t; | |
2003 | ||
2004 | where type 'TYPE' is a type with different size than 'type', | |
2005 | and op is <<, >> or rotate. | |
2006 | ||
2007 | Also detect cases: | |
2008 | ||
2009 | type a_t; | |
2010 | TYPE b_T, c_T, res_T; | |
2011 | ||
2012 | S0 c_T = ; | |
2013 | S1 a_t = (type) c_T; | |
2014 | S2 b_T = ; | |
2015 | S3 res_T = b_T op a_t; | |
2016 | ||
2017 | Input/Output: | |
2018 | ||
2019 | * STMTS: Contains a stmt from which the pattern search begins, | |
2020 | i.e. the shift/rotate stmt. The original stmt (S3) is replaced | |
2021 | with a shift/rotate which has same type on both operands, in the | |
2022 | second case just b_T op c_T, in the first case with added cast | |
363477c0 | 2023 | from a_t to c_T in STMT_VINFO_PATTERN_DEF_SEQ. |
732a0ad3 JJ |
2024 | |
2025 | Output: | |
2026 | ||
2027 | * TYPE_IN: The type of the input arguments to the pattern. | |
2028 | ||
2029 | * TYPE_OUT: The type of the output of this pattern. | |
2030 | ||
2031 | * Return value: A new stmt that will be used to replace the shift/rotate | |
2032 | S3 stmt. */ | |
2033 | ||
355fe088 TS |
2034 | static gimple * |
2035 | vect_recog_vector_vector_shift_pattern (vec<gimple *> *stmts, | |
732a0ad3 JJ |
2036 | tree *type_in, tree *type_out) |
2037 | { | |
355fe088 | 2038 | gimple *last_stmt = stmts->pop (); |
732a0ad3 | 2039 | tree oprnd0, oprnd1, lhs, var; |
355fe088 | 2040 | gimple *pattern_stmt, *def_stmt; |
732a0ad3 JJ |
2041 | enum tree_code rhs_code; |
2042 | stmt_vec_info stmt_vinfo = vinfo_for_stmt (last_stmt); | |
310213d4 | 2043 | vec_info *vinfo = stmt_vinfo->vinfo; |
732a0ad3 | 2044 | enum vect_def_type dt; |
732a0ad3 JJ |
2045 | |
2046 | if (!is_gimple_assign (last_stmt)) | |
2047 | return NULL; | |
2048 | ||
2049 | rhs_code = gimple_assign_rhs_code (last_stmt); | |
2050 | switch (rhs_code) | |
2051 | { | |
2052 | case LSHIFT_EXPR: | |
2053 | case RSHIFT_EXPR: | |
2054 | case LROTATE_EXPR: | |
2055 | case RROTATE_EXPR: | |
2056 | break; | |
2057 | default: | |
2058 | return NULL; | |
2059 | } | |
2060 | ||
2061 | if (STMT_VINFO_IN_PATTERN_P (stmt_vinfo)) | |
2062 | return NULL; | |
2063 | ||
2064 | lhs = gimple_assign_lhs (last_stmt); | |
2065 | oprnd0 = gimple_assign_rhs1 (last_stmt); | |
2066 | oprnd1 = gimple_assign_rhs2 (last_stmt); | |
2067 | if (TREE_CODE (oprnd0) != SSA_NAME | |
2068 | || TREE_CODE (oprnd1) != SSA_NAME | |
2069 | || TYPE_MODE (TREE_TYPE (oprnd0)) == TYPE_MODE (TREE_TYPE (oprnd1)) | |
2070 | || TYPE_PRECISION (TREE_TYPE (oprnd1)) | |
2071 | != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (oprnd1))) | |
2072 | || TYPE_PRECISION (TREE_TYPE (lhs)) | |
2073 | != TYPE_PRECISION (TREE_TYPE (oprnd0))) | |
2074 | return NULL; | |
2075 | ||
81c40241 | 2076 | if (!vect_is_simple_use (oprnd1, vinfo, &def_stmt, &dt)) |
732a0ad3 JJ |
2077 | return NULL; |
2078 | ||
2079 | if (dt != vect_internal_def) | |
2080 | return NULL; | |
2081 | ||
2082 | *type_in = get_vectype_for_scalar_type (TREE_TYPE (oprnd0)); | |
2083 | *type_out = *type_in; | |
2084 | if (*type_in == NULL_TREE) | |
2085 | return NULL; | |
2086 | ||
81c40241 | 2087 | tree def = NULL_TREE; |
97ecdb46 JJ |
2088 | stmt_vec_info def_vinfo = vinfo_for_stmt (def_stmt); |
2089 | if (!STMT_VINFO_IN_PATTERN_P (def_vinfo) && gimple_assign_cast_p (def_stmt)) | |
732a0ad3 JJ |
2090 | { |
2091 | tree rhs1 = gimple_assign_rhs1 (def_stmt); | |
2092 | if (TYPE_MODE (TREE_TYPE (rhs1)) == TYPE_MODE (TREE_TYPE (oprnd0)) | |
2093 | && TYPE_PRECISION (TREE_TYPE (rhs1)) | |
2094 | == TYPE_PRECISION (TREE_TYPE (oprnd0))) | |
0179520a JJ |
2095 | { |
2096 | if (TYPE_PRECISION (TREE_TYPE (oprnd1)) | |
2097 | >= TYPE_PRECISION (TREE_TYPE (rhs1))) | |
2098 | def = rhs1; | |
2099 | else | |
2100 | { | |
2101 | tree mask | |
2102 | = build_low_bits_mask (TREE_TYPE (rhs1), | |
2103 | TYPE_PRECISION (TREE_TYPE (oprnd1))); | |
2104 | def = vect_recog_temp_ssa_var (TREE_TYPE (rhs1), NULL); | |
2105 | def_stmt = gimple_build_assign (def, BIT_AND_EXPR, rhs1, mask); | |
2106 | new_pattern_def_seq (stmt_vinfo, def_stmt); | |
2107 | } | |
2108 | } | |
732a0ad3 JJ |
2109 | } |
2110 | ||
2111 | if (def == NULL_TREE) | |
2112 | { | |
2113 | def = vect_recog_temp_ssa_var (TREE_TYPE (oprnd0), NULL); | |
0d0e4a03 | 2114 | def_stmt = gimple_build_assign (def, NOP_EXPR, oprnd1); |
083481d8 | 2115 | new_pattern_def_seq (stmt_vinfo, def_stmt); |
732a0ad3 JJ |
2116 | } |
2117 | ||
2118 | /* Pattern detected. */ | |
73fbfcad | 2119 | if (dump_enabled_p ()) |
ccb3ad87 | 2120 | dump_printf_loc (MSG_NOTE, vect_location, |
e645e942 | 2121 | "vect_recog_vector_vector_shift_pattern: detected:\n"); |
732a0ad3 JJ |
2122 | |
2123 | /* Pattern supported. Create a stmt to be used to replace the pattern. */ | |
2124 | var = vect_recog_temp_ssa_var (TREE_TYPE (oprnd0), NULL); | |
0d0e4a03 | 2125 | pattern_stmt = gimple_build_assign (var, rhs_code, oprnd0, def); |
732a0ad3 | 2126 | |
73fbfcad | 2127 | if (dump_enabled_p ()) |
78c60e3d | 2128 | dump_gimple_stmt_loc (MSG_NOTE, vect_location, TDF_SLIM, pattern_stmt, 0); |
732a0ad3 | 2129 | |
9771b263 | 2130 | stmts->safe_push (last_stmt); |
732a0ad3 JJ |
2131 | return pattern_stmt; |
2132 | } | |
2133 | ||
47486460 VK |
2134 | /* Detect multiplication by constant which are postive or negatives of power 2, |
2135 | and convert them to shift patterns. | |
2136 | ||
2137 | Mult with constants that are postive power of two. | |
2138 | type a_t; | |
2139 | type b_t | |
2140 | S1: b_t = a_t * n | |
2141 | ||
2142 | or | |
2143 | ||
2144 | Mult with constants that are negative power of two. | |
2145 | S2: b_t = a_t * -n | |
2146 | ||
2147 | Input/Output: | |
2148 | ||
2149 | STMTS: Contains a stmt from which the pattern search begins, | |
2150 | i.e. the mult stmt. Convert the mult operation to LSHIFT if | |
2151 | constant operand is a power of 2. | |
2152 | type a_t, b_t | |
2153 | S1': b_t = a_t << log2 (n) | |
2154 | ||
2155 | Convert the mult operation to LSHIFT and followed by a NEGATE | |
2156 | if constant operand is a negative power of 2. | |
2157 | type a_t, b_t, res_T; | |
2158 | S2': b_t = a_t << log2 (n) | |
2159 | S3': res_T = - (b_t) | |
2160 | ||
2161 | Output: | |
2162 | ||
2163 | * TYPE_IN: The type of the input arguments to the pattern. | |
2164 | ||
2165 | * TYPE_OUT: The type of the output of this pattern. | |
2166 | ||
2167 | * Return value: A new stmt that will be used to replace the multiplication | |
2168 | S1 or S2 stmt. */ | |
2169 | ||
355fe088 TS |
2170 | static gimple * |
2171 | vect_recog_mult_pattern (vec<gimple *> *stmts, | |
47486460 VK |
2172 | tree *type_in, tree *type_out) |
2173 | { | |
355fe088 | 2174 | gimple *last_stmt = stmts->pop (); |
47486460 | 2175 | tree oprnd0, oprnd1, vectype, itype; |
355fe088 | 2176 | gimple *pattern_stmt, *def_stmt; |
47486460 VK |
2177 | optab optab; |
2178 | stmt_vec_info stmt_vinfo = vinfo_for_stmt (last_stmt); | |
2179 | int power2_val, power2_neg_val; | |
2180 | tree shift; | |
2181 | ||
2182 | if (!is_gimple_assign (last_stmt)) | |
2183 | return NULL; | |
2184 | ||
2185 | if (gimple_assign_rhs_code (last_stmt) != MULT_EXPR) | |
2186 | return NULL; | |
2187 | ||
2188 | oprnd0 = gimple_assign_rhs1 (last_stmt); | |
2189 | oprnd1 = gimple_assign_rhs2 (last_stmt); | |
2190 | itype = TREE_TYPE (oprnd0); | |
2191 | ||
2192 | if (TREE_CODE (oprnd0) != SSA_NAME | |
2193 | || TREE_CODE (oprnd1) != INTEGER_CST | |
2194 | || !INTEGRAL_TYPE_P (itype) | |
2195 | || TYPE_PRECISION (itype) != GET_MODE_PRECISION (TYPE_MODE (itype))) | |
2196 | return NULL; | |
2197 | ||
2198 | vectype = get_vectype_for_scalar_type (itype); | |
2199 | if (vectype == NULL_TREE) | |
2200 | return NULL; | |
2201 | ||
2202 | /* If the target can handle vectorized multiplication natively, | |
2203 | don't attempt to optimize this. */ | |
2204 | optab = optab_for_tree_code (MULT_EXPR, vectype, optab_default); | |
2205 | if (optab != unknown_optab) | |
2206 | { | |
2207 | machine_mode vec_mode = TYPE_MODE (vectype); | |
2208 | int icode = (int) optab_handler (optab, vec_mode); | |
2209 | if (icode != CODE_FOR_nothing) | |
2210 | return NULL; | |
2211 | } | |
2212 | ||
2213 | /* If target cannot handle vector left shift then we cannot | |
2214 | optimize and bail out. */ | |
2215 | optab = optab_for_tree_code (LSHIFT_EXPR, vectype, optab_vector); | |
2216 | if (!optab | |
2217 | || optab_handler (optab, TYPE_MODE (vectype)) == CODE_FOR_nothing) | |
2218 | return NULL; | |
2219 | ||
2220 | power2_val = wi::exact_log2 (oprnd1); | |
2221 | power2_neg_val = wi::exact_log2 (wi::neg (oprnd1)); | |
2222 | ||
2223 | /* Handle constant operands that are postive or negative powers of 2. */ | |
2224 | if (power2_val != -1) | |
2225 | { | |
2226 | shift = build_int_cst (itype, power2_val); | |
2227 | pattern_stmt | |
2228 | = gimple_build_assign (vect_recog_temp_ssa_var (itype, NULL), | |
2229 | LSHIFT_EXPR, oprnd0, shift); | |
2230 | } | |
2231 | else if (power2_neg_val != -1) | |
2232 | { | |
2233 | /* If the target cannot handle vector NEGATE then we cannot | |
2234 | do the optimization. */ | |
2235 | optab = optab_for_tree_code (NEGATE_EXPR, vectype, optab_vector); | |
2236 | if (!optab | |
2237 | || optab_handler (optab, TYPE_MODE (vectype)) == CODE_FOR_nothing) | |
2238 | return NULL; | |
2239 | ||
2240 | shift = build_int_cst (itype, power2_neg_val); | |
2241 | def_stmt | |
2242 | = gimple_build_assign (vect_recog_temp_ssa_var (itype, NULL), | |
2243 | LSHIFT_EXPR, oprnd0, shift); | |
2244 | new_pattern_def_seq (stmt_vinfo, def_stmt); | |
2245 | pattern_stmt | |
2246 | = gimple_build_assign (vect_recog_temp_ssa_var (itype, NULL), | |
2247 | NEGATE_EXPR, gimple_assign_lhs (def_stmt)); | |
2248 | } | |
2249 | else | |
2250 | return NULL; | |
2251 | ||
2252 | /* Pattern detected. */ | |
2253 | if (dump_enabled_p ()) | |
2254 | dump_printf_loc (MSG_NOTE, vect_location, | |
2255 | "vect_recog_mult_pattern: detected:\n"); | |
2256 | ||
2257 | if (dump_enabled_p ()) | |
2258 | dump_gimple_stmt_loc (MSG_NOTE, vect_location, TDF_SLIM, | |
2259 | pattern_stmt,0); | |
2260 | ||
2261 | stmts->safe_push (last_stmt); | |
2262 | *type_in = vectype; | |
2263 | *type_out = vectype; | |
2264 | ||
2265 | return pattern_stmt; | |
2266 | } | |
2267 | ||
079c527f | 2268 | /* Detect a signed division by a constant that wouldn't be |
363477c0 JJ |
2269 | otherwise vectorized: |
2270 | ||
2271 | type a_t, b_t; | |
2272 | ||
2273 | S1 a_t = b_t / N; | |
2274 | ||
079c527f | 2275 | where type 'type' is an integral type and N is a constant. |
363477c0 | 2276 | |
079c527f | 2277 | Similarly handle modulo by a constant: |
363477c0 JJ |
2278 | |
2279 | S4 a_t = b_t % N; | |
2280 | ||
2281 | Input/Output: | |
2282 | ||
2283 | * STMTS: Contains a stmt from which the pattern search begins, | |
079c527f JJ |
2284 | i.e. the division stmt. S1 is replaced by if N is a power |
2285 | of two constant and type is signed: | |
363477c0 JJ |
2286 | S3 y_t = b_t < 0 ? N - 1 : 0; |
2287 | S2 x_t = b_t + y_t; | |
2288 | S1' a_t = x_t >> log2 (N); | |
2289 | ||
079c527f JJ |
2290 | S4 is replaced if N is a power of two constant and |
2291 | type is signed by (where *_T temporaries have unsigned type): | |
363477c0 JJ |
2292 | S9 y_T = b_t < 0 ? -1U : 0U; |
2293 | S8 z_T = y_T >> (sizeof (type_t) * CHAR_BIT - log2 (N)); | |
2294 | S7 z_t = (type) z_T; | |
2295 | S6 w_t = b_t + z_t; | |
2296 | S5 x_t = w_t & (N - 1); | |
2297 | S4' a_t = x_t - z_t; | |
2298 | ||
2299 | Output: | |
2300 | ||
2301 | * TYPE_IN: The type of the input arguments to the pattern. | |
2302 | ||
2303 | * TYPE_OUT: The type of the output of this pattern. | |
2304 | ||
2305 | * Return value: A new stmt that will be used to replace the division | |
2306 | S1 or modulo S4 stmt. */ | |
2307 | ||
355fe088 TS |
2308 | static gimple * |
2309 | vect_recog_divmod_pattern (vec<gimple *> *stmts, | |
079c527f | 2310 | tree *type_in, tree *type_out) |
363477c0 | 2311 | { |
355fe088 | 2312 | gimple *last_stmt = stmts->pop (); |
5deb57cb | 2313 | tree oprnd0, oprnd1, vectype, itype, cond; |
355fe088 | 2314 | gimple *pattern_stmt, *def_stmt; |
363477c0 JJ |
2315 | enum tree_code rhs_code; |
2316 | stmt_vec_info stmt_vinfo = vinfo_for_stmt (last_stmt); | |
310213d4 | 2317 | vec_info *vinfo = stmt_vinfo->vinfo; |
363477c0 | 2318 | optab optab; |
00f07b86 | 2319 | tree q; |
079c527f | 2320 | int dummy_int, prec; |
079c527f | 2321 | stmt_vec_info def_stmt_vinfo; |
363477c0 JJ |
2322 | |
2323 | if (!is_gimple_assign (last_stmt)) | |
2324 | return NULL; | |
2325 | ||
2326 | rhs_code = gimple_assign_rhs_code (last_stmt); | |
2327 | switch (rhs_code) | |
2328 | { | |
2329 | case TRUNC_DIV_EXPR: | |
2330 | case TRUNC_MOD_EXPR: | |
2331 | break; | |
2332 | default: | |
2333 | return NULL; | |
2334 | } | |
2335 | ||
2336 | if (STMT_VINFO_IN_PATTERN_P (stmt_vinfo)) | |
2337 | return NULL; | |
2338 | ||
2339 | oprnd0 = gimple_assign_rhs1 (last_stmt); | |
2340 | oprnd1 = gimple_assign_rhs2 (last_stmt); | |
2341 | itype = TREE_TYPE (oprnd0); | |
2342 | if (TREE_CODE (oprnd0) != SSA_NAME | |
2343 | || TREE_CODE (oprnd1) != INTEGER_CST | |
2344 | || TREE_CODE (itype) != INTEGER_TYPE | |
079c527f | 2345 | || TYPE_PRECISION (itype) != GET_MODE_PRECISION (TYPE_MODE (itype))) |
363477c0 JJ |
2346 | return NULL; |
2347 | ||
2348 | vectype = get_vectype_for_scalar_type (itype); | |
2349 | if (vectype == NULL_TREE) | |
2350 | return NULL; | |
2351 | ||
2352 | /* If the target can handle vectorized division or modulo natively, | |
2353 | don't attempt to optimize this. */ | |
2354 | optab = optab_for_tree_code (rhs_code, vectype, optab_default); | |
2225b9f2 | 2355 | if (optab != unknown_optab) |
363477c0 | 2356 | { |
ef4bddc2 | 2357 | machine_mode vec_mode = TYPE_MODE (vectype); |
363477c0 | 2358 | int icode = (int) optab_handler (optab, vec_mode); |
e6d4f8f5 | 2359 | if (icode != CODE_FOR_nothing) |
363477c0 JJ |
2360 | return NULL; |
2361 | } | |
2362 | ||
079c527f JJ |
2363 | prec = TYPE_PRECISION (itype); |
2364 | if (integer_pow2p (oprnd1)) | |
363477c0 | 2365 | { |
079c527f JJ |
2366 | if (TYPE_UNSIGNED (itype) || tree_int_cst_sgn (oprnd1) != 1) |
2367 | return NULL; | |
363477c0 | 2368 | |
079c527f | 2369 | /* Pattern detected. */ |
73fbfcad | 2370 | if (dump_enabled_p ()) |
ccb3ad87 | 2371 | dump_printf_loc (MSG_NOTE, vect_location, |
e645e942 | 2372 | "vect_recog_divmod_pattern: detected:\n"); |
079c527f JJ |
2373 | |
2374 | cond = build2 (LT_EXPR, boolean_type_node, oprnd0, | |
2375 | build_int_cst (itype, 0)); | |
2376 | if (rhs_code == TRUNC_DIV_EXPR) | |
2377 | { | |
2378 | tree var = vect_recog_temp_ssa_var (itype, NULL); | |
2379 | tree shift; | |
2380 | def_stmt | |
0d0e4a03 JJ |
2381 | = gimple_build_assign (var, COND_EXPR, cond, |
2382 | fold_build2 (MINUS_EXPR, itype, oprnd1, | |
2383 | build_int_cst (itype, 1)), | |
2384 | build_int_cst (itype, 0)); | |
079c527f JJ |
2385 | new_pattern_def_seq (stmt_vinfo, def_stmt); |
2386 | var = vect_recog_temp_ssa_var (itype, NULL); | |
2387 | def_stmt | |
0d0e4a03 JJ |
2388 | = gimple_build_assign (var, PLUS_EXPR, oprnd0, |
2389 | gimple_assign_lhs (def_stmt)); | |
079c527f JJ |
2390 | append_pattern_def_seq (stmt_vinfo, def_stmt); |
2391 | ||
2392 | shift = build_int_cst (itype, tree_log2 (oprnd1)); | |
2393 | pattern_stmt | |
0d0e4a03 JJ |
2394 | = gimple_build_assign (vect_recog_temp_ssa_var (itype, NULL), |
2395 | RSHIFT_EXPR, var, shift); | |
079c527f JJ |
2396 | } |
2397 | else | |
2398 | { | |
2399 | tree signmask; | |
2400 | STMT_VINFO_PATTERN_DEF_SEQ (stmt_vinfo) = NULL; | |
2401 | if (compare_tree_int (oprnd1, 2) == 0) | |
2402 | { | |
2403 | signmask = vect_recog_temp_ssa_var (itype, NULL); | |
0d0e4a03 JJ |
2404 | def_stmt = gimple_build_assign (signmask, COND_EXPR, cond, |
2405 | build_int_cst (itype, 1), | |
2406 | build_int_cst (itype, 0)); | |
079c527f JJ |
2407 | append_pattern_def_seq (stmt_vinfo, def_stmt); |
2408 | } | |
2409 | else | |
2410 | { | |
2411 | tree utype | |
2412 | = build_nonstandard_integer_type (prec, 1); | |
2413 | tree vecutype = get_vectype_for_scalar_type (utype); | |
2414 | tree shift | |
2415 | = build_int_cst (utype, GET_MODE_BITSIZE (TYPE_MODE (itype)) | |
2416 | - tree_log2 (oprnd1)); | |
2417 | tree var = vect_recog_temp_ssa_var (utype, NULL); | |
2418 | ||
0d0e4a03 JJ |
2419 | def_stmt = gimple_build_assign (var, COND_EXPR, cond, |
2420 | build_int_cst (utype, -1), | |
2421 | build_int_cst (utype, 0)); | |
310213d4 | 2422 | def_stmt_vinfo = new_stmt_vec_info (def_stmt, vinfo); |
079c527f JJ |
2423 | set_vinfo_for_stmt (def_stmt, def_stmt_vinfo); |
2424 | STMT_VINFO_VECTYPE (def_stmt_vinfo) = vecutype; | |
2425 | append_pattern_def_seq (stmt_vinfo, def_stmt); | |
2426 | var = vect_recog_temp_ssa_var (utype, NULL); | |
0d0e4a03 JJ |
2427 | def_stmt = gimple_build_assign (var, RSHIFT_EXPR, |
2428 | gimple_assign_lhs (def_stmt), | |
2429 | shift); | |
310213d4 | 2430 | def_stmt_vinfo = new_stmt_vec_info (def_stmt, vinfo); |
079c527f JJ |
2431 | set_vinfo_for_stmt (def_stmt, def_stmt_vinfo); |
2432 | STMT_VINFO_VECTYPE (def_stmt_vinfo) = vecutype; | |
2433 | append_pattern_def_seq (stmt_vinfo, def_stmt); | |
2434 | signmask = vect_recog_temp_ssa_var (itype, NULL); | |
2435 | def_stmt | |
0d0e4a03 | 2436 | = gimple_build_assign (signmask, NOP_EXPR, var); |
079c527f JJ |
2437 | append_pattern_def_seq (stmt_vinfo, def_stmt); |
2438 | } | |
2439 | def_stmt | |
0d0e4a03 JJ |
2440 | = gimple_build_assign (vect_recog_temp_ssa_var (itype, NULL), |
2441 | PLUS_EXPR, oprnd0, signmask); | |
079c527f JJ |
2442 | append_pattern_def_seq (stmt_vinfo, def_stmt); |
2443 | def_stmt | |
0d0e4a03 JJ |
2444 | = gimple_build_assign (vect_recog_temp_ssa_var (itype, NULL), |
2445 | BIT_AND_EXPR, gimple_assign_lhs (def_stmt), | |
2446 | fold_build2 (MINUS_EXPR, itype, oprnd1, | |
2447 | build_int_cst (itype, 1))); | |
079c527f JJ |
2448 | append_pattern_def_seq (stmt_vinfo, def_stmt); |
2449 | ||
2450 | pattern_stmt | |
0d0e4a03 JJ |
2451 | = gimple_build_assign (vect_recog_temp_ssa_var (itype, NULL), |
2452 | MINUS_EXPR, gimple_assign_lhs (def_stmt), | |
2453 | signmask); | |
079c527f JJ |
2454 | } |
2455 | ||
73fbfcad | 2456 | if (dump_enabled_p ()) |
78c60e3d SS |
2457 | dump_gimple_stmt_loc (MSG_NOTE, vect_location, TDF_SLIM, pattern_stmt, |
2458 | 0); | |
079c527f | 2459 | |
9771b263 | 2460 | stmts->safe_push (last_stmt); |
079c527f JJ |
2461 | |
2462 | *type_in = vectype; | |
2463 | *type_out = vectype; | |
2464 | return pattern_stmt; | |
363477c0 | 2465 | } |
079c527f | 2466 | |
6b58915b RS |
2467 | if (prec > HOST_BITS_PER_WIDE_INT |
2468 | || integer_zerop (oprnd1)) | |
079c527f JJ |
2469 | return NULL; |
2470 | ||
00f07b86 RH |
2471 | if (!can_mult_highpart_p (TYPE_MODE (vectype), TYPE_UNSIGNED (itype))) |
2472 | return NULL; | |
079c527f JJ |
2473 | |
2474 | STMT_VINFO_PATTERN_DEF_SEQ (stmt_vinfo) = NULL; | |
2475 | ||
2476 | if (TYPE_UNSIGNED (itype)) | |
363477c0 | 2477 | { |
079c527f JJ |
2478 | unsigned HOST_WIDE_INT mh, ml; |
2479 | int pre_shift, post_shift; | |
6b58915b RS |
2480 | unsigned HOST_WIDE_INT d = (TREE_INT_CST_LOW (oprnd1) |
2481 | & GET_MODE_MASK (TYPE_MODE (itype))); | |
5deb57cb | 2482 | tree t1, t2, t3, t4; |
079c527f JJ |
2483 | |
2484 | if (d >= ((unsigned HOST_WIDE_INT) 1 << (prec - 1))) | |
2485 | /* FIXME: Can transform this into oprnd0 >= oprnd1 ? 1 : 0. */ | |
2486 | return NULL; | |
2487 | ||
2488 | /* Find a suitable multiplier and right shift count | |
2489 | instead of multiplying with D. */ | |
2490 | mh = choose_multiplier (d, prec, prec, &ml, &post_shift, &dummy_int); | |
2491 | ||
2492 | /* If the suggested multiplier is more than SIZE bits, we can do better | |
2493 | for even divisors, using an initial right shift. */ | |
2494 | if (mh != 0 && (d & 1) == 0) | |
363477c0 | 2495 | { |
079c527f JJ |
2496 | pre_shift = floor_log2 (d & -d); |
2497 | mh = choose_multiplier (d >> pre_shift, prec, prec - pre_shift, | |
2498 | &ml, &post_shift, &dummy_int); | |
2499 | gcc_assert (!mh); | |
2500 | } | |
2501 | else | |
2502 | pre_shift = 0; | |
2503 | ||
2504 | if (mh != 0) | |
2505 | { | |
2506 | if (post_shift - 1 >= prec) | |
2507 | return NULL; | |
2508 | ||
5deb57cb JJ |
2509 | /* t1 = oprnd0 h* ml; |
2510 | t2 = oprnd0 - t1; | |
2511 | t3 = t2 >> 1; | |
2512 | t4 = t1 + t3; | |
2513 | q = t4 >> (post_shift - 1); */ | |
2514 | t1 = vect_recog_temp_ssa_var (itype, NULL); | |
0d0e4a03 JJ |
2515 | def_stmt = gimple_build_assign (t1, MULT_HIGHPART_EXPR, oprnd0, |
2516 | build_int_cst (itype, ml)); | |
079c527f | 2517 | append_pattern_def_seq (stmt_vinfo, def_stmt); |
079c527f | 2518 | |
5deb57cb | 2519 | t2 = vect_recog_temp_ssa_var (itype, NULL); |
079c527f | 2520 | def_stmt |
0d0e4a03 | 2521 | = gimple_build_assign (t2, MINUS_EXPR, oprnd0, t1); |
083481d8 | 2522 | append_pattern_def_seq (stmt_vinfo, def_stmt); |
079c527f JJ |
2523 | |
2524 | t3 = vect_recog_temp_ssa_var (itype, NULL); | |
2525 | def_stmt | |
0d0e4a03 | 2526 | = gimple_build_assign (t3, RSHIFT_EXPR, t2, integer_one_node); |
079c527f JJ |
2527 | append_pattern_def_seq (stmt_vinfo, def_stmt); |
2528 | ||
5deb57cb | 2529 | t4 = vect_recog_temp_ssa_var (itype, NULL); |
079c527f | 2530 | def_stmt |
0d0e4a03 | 2531 | = gimple_build_assign (t4, PLUS_EXPR, t1, t3); |
079c527f JJ |
2532 | |
2533 | if (post_shift != 1) | |
2534 | { | |
2535 | append_pattern_def_seq (stmt_vinfo, def_stmt); | |
2536 | ||
5deb57cb | 2537 | q = vect_recog_temp_ssa_var (itype, NULL); |
079c527f | 2538 | pattern_stmt |
0d0e4a03 JJ |
2539 | = gimple_build_assign (q, RSHIFT_EXPR, t4, |
2540 | build_int_cst (itype, post_shift - 1)); | |
079c527f JJ |
2541 | } |
2542 | else | |
2543 | { | |
5deb57cb | 2544 | q = t4; |
079c527f JJ |
2545 | pattern_stmt = def_stmt; |
2546 | } | |
363477c0 JJ |
2547 | } |
2548 | else | |
2549 | { | |
079c527f JJ |
2550 | if (pre_shift >= prec || post_shift >= prec) |
2551 | return NULL; | |
2552 | ||
2553 | /* t1 = oprnd0 >> pre_shift; | |
5deb57cb JJ |
2554 | t2 = t1 h* ml; |
2555 | q = t2 >> post_shift; */ | |
079c527f JJ |
2556 | if (pre_shift) |
2557 | { | |
2558 | t1 = vect_recog_temp_ssa_var (itype, NULL); | |
2559 | def_stmt | |
0d0e4a03 JJ |
2560 | = gimple_build_assign (t1, RSHIFT_EXPR, oprnd0, |
2561 | build_int_cst (NULL, pre_shift)); | |
079c527f JJ |
2562 | append_pattern_def_seq (stmt_vinfo, def_stmt); |
2563 | } | |
2564 | else | |
2565 | t1 = oprnd0; | |
363477c0 | 2566 | |
5deb57cb | 2567 | t2 = vect_recog_temp_ssa_var (itype, NULL); |
0d0e4a03 JJ |
2568 | def_stmt = gimple_build_assign (t2, MULT_HIGHPART_EXPR, t1, |
2569 | build_int_cst (itype, ml)); | |
079c527f | 2570 | |
5deb57cb JJ |
2571 | if (post_shift) |
2572 | { | |
2573 | append_pattern_def_seq (stmt_vinfo, def_stmt); | |
079c527f | 2574 | |
5deb57cb JJ |
2575 | q = vect_recog_temp_ssa_var (itype, NULL); |
2576 | def_stmt | |
0d0e4a03 JJ |
2577 | = gimple_build_assign (q, RSHIFT_EXPR, t2, |
2578 | build_int_cst (itype, post_shift)); | |
5deb57cb JJ |
2579 | } |
2580 | else | |
2581 | q = t2; | |
2582 | ||
2583 | pattern_stmt = def_stmt; | |
079c527f JJ |
2584 | } |
2585 | } | |
2586 | else | |
2587 | { | |
2588 | unsigned HOST_WIDE_INT ml; | |
4ee4c52c | 2589 | int post_shift; |
6b58915b | 2590 | HOST_WIDE_INT d = TREE_INT_CST_LOW (oprnd1); |
079c527f JJ |
2591 | unsigned HOST_WIDE_INT abs_d; |
2592 | bool add = false; | |
5deb57cb | 2593 | tree t1, t2, t3, t4; |
079c527f JJ |
2594 | |
2595 | /* Give up for -1. */ | |
2596 | if (d == -1) | |
2597 | return NULL; | |
2598 | ||
079c527f JJ |
2599 | /* Since d might be INT_MIN, we have to cast to |
2600 | unsigned HOST_WIDE_INT before negating to avoid | |
2601 | undefined signed overflow. */ | |
2602 | abs_d = (d >= 0 | |
2603 | ? (unsigned HOST_WIDE_INT) d | |
2604 | : - (unsigned HOST_WIDE_INT) d); | |
2605 | ||
2606 | /* n rem d = n rem -d */ | |
2607 | if (rhs_code == TRUNC_MOD_EXPR && d < 0) | |
2608 | { | |
2609 | d = abs_d; | |
2610 | oprnd1 = build_int_cst (itype, abs_d); | |
2611 | } | |
2612 | else if (HOST_BITS_PER_WIDE_INT >= prec | |
2613 | && abs_d == (unsigned HOST_WIDE_INT) 1 << (prec - 1)) | |
2614 | /* This case is not handled correctly below. */ | |
2615 | return NULL; | |
2616 | ||
4ee4c52c | 2617 | choose_multiplier (abs_d, prec, prec - 1, &ml, &post_shift, &dummy_int); |
079c527f JJ |
2618 | if (ml >= (unsigned HOST_WIDE_INT) 1 << (prec - 1)) |
2619 | { | |
2620 | add = true; | |
2621 | ml |= (~(unsigned HOST_WIDE_INT) 0) << (prec - 1); | |
2622 | } | |
2623 | if (post_shift >= prec) | |
2624 | return NULL; | |
2625 | ||
7abed779 | 2626 | /* t1 = oprnd0 h* ml; */ |
5deb57cb | 2627 | t1 = vect_recog_temp_ssa_var (itype, NULL); |
0d0e4a03 JJ |
2628 | def_stmt = gimple_build_assign (t1, MULT_HIGHPART_EXPR, oprnd0, |
2629 | build_int_cst (itype, ml)); | |
079c527f JJ |
2630 | |
2631 | if (add) | |
2632 | { | |
5deb57cb | 2633 | /* t2 = t1 + oprnd0; */ |
7abed779 | 2634 | append_pattern_def_seq (stmt_vinfo, def_stmt); |
5deb57cb | 2635 | t2 = vect_recog_temp_ssa_var (itype, NULL); |
0d0e4a03 | 2636 | def_stmt = gimple_build_assign (t2, PLUS_EXPR, t1, oprnd0); |
079c527f JJ |
2637 | } |
2638 | else | |
5deb57cb | 2639 | t2 = t1; |
079c527f | 2640 | |
5deb57cb | 2641 | if (post_shift) |
079c527f | 2642 | { |
5deb57cb | 2643 | /* t3 = t2 >> post_shift; */ |
7abed779 | 2644 | append_pattern_def_seq (stmt_vinfo, def_stmt); |
5deb57cb | 2645 | t3 = vect_recog_temp_ssa_var (itype, NULL); |
0d0e4a03 JJ |
2646 | def_stmt = gimple_build_assign (t3, RSHIFT_EXPR, t2, |
2647 | build_int_cst (itype, post_shift)); | |
363477c0 | 2648 | } |
079c527f | 2649 | else |
5deb57cb | 2650 | t3 = t2; |
079c527f | 2651 | |
807e902e | 2652 | wide_int oprnd0_min, oprnd0_max; |
7abed779 JJ |
2653 | int msb = 1; |
2654 | if (get_range_info (oprnd0, &oprnd0_min, &oprnd0_max) == VR_RANGE) | |
2655 | { | |
807e902e | 2656 | if (!wi::neg_p (oprnd0_min, TYPE_SIGN (itype))) |
7abed779 | 2657 | msb = 0; |
807e902e | 2658 | else if (wi::neg_p (oprnd0_max, TYPE_SIGN (itype))) |
7abed779 JJ |
2659 | msb = -1; |
2660 | } | |
079c527f | 2661 | |
7abed779 JJ |
2662 | if (msb == 0 && d >= 0) |
2663 | { | |
2664 | /* q = t3; */ | |
2665 | q = t3; | |
2666 | pattern_stmt = def_stmt; | |
2667 | } | |
2668 | else | |
2669 | { | |
2670 | /* t4 = oprnd0 >> (prec - 1); | |
2671 | or if we know from VRP that oprnd0 >= 0 | |
2672 | t4 = 0; | |
2673 | or if we know from VRP that oprnd0 < 0 | |
2674 | t4 = -1; */ | |
2675 | append_pattern_def_seq (stmt_vinfo, def_stmt); | |
2676 | t4 = vect_recog_temp_ssa_var (itype, NULL); | |
2677 | if (msb != 1) | |
0d0e4a03 JJ |
2678 | def_stmt = gimple_build_assign (t4, INTEGER_CST, |
2679 | build_int_cst (itype, msb)); | |
7abed779 | 2680 | else |
0d0e4a03 JJ |
2681 | def_stmt = gimple_build_assign (t4, RSHIFT_EXPR, oprnd0, |
2682 | build_int_cst (itype, prec - 1)); | |
7abed779 JJ |
2683 | append_pattern_def_seq (stmt_vinfo, def_stmt); |
2684 | ||
2685 | /* q = t3 - t4; or q = t4 - t3; */ | |
2686 | q = vect_recog_temp_ssa_var (itype, NULL); | |
0d0e4a03 JJ |
2687 | pattern_stmt = gimple_build_assign (q, MINUS_EXPR, d < 0 ? t4 : t3, |
2688 | d < 0 ? t3 : t4); | |
7abed779 | 2689 | } |
079c527f JJ |
2690 | } |
2691 | ||
2692 | if (rhs_code == TRUNC_MOD_EXPR) | |
2693 | { | |
2694 | tree r, t1; | |
2695 | ||
2696 | /* We divided. Now finish by: | |
2697 | t1 = q * oprnd1; | |
2698 | r = oprnd0 - t1; */ | |
2699 | append_pattern_def_seq (stmt_vinfo, pattern_stmt); | |
2700 | ||
2701 | t1 = vect_recog_temp_ssa_var (itype, NULL); | |
0d0e4a03 | 2702 | def_stmt = gimple_build_assign (t1, MULT_EXPR, q, oprnd1); |
083481d8 | 2703 | append_pattern_def_seq (stmt_vinfo, def_stmt); |
363477c0 | 2704 | |
079c527f | 2705 | r = vect_recog_temp_ssa_var (itype, NULL); |
0d0e4a03 | 2706 | pattern_stmt = gimple_build_assign (r, MINUS_EXPR, oprnd0, t1); |
363477c0 JJ |
2707 | } |
2708 | ||
079c527f | 2709 | /* Pattern detected. */ |
73fbfcad | 2710 | if (dump_enabled_p ()) |
78c60e3d | 2711 | { |
ccb3ad87 | 2712 | dump_printf_loc (MSG_NOTE, vect_location, |
78c60e3d | 2713 | "vect_recog_divmod_pattern: detected: "); |
ccb3ad87 | 2714 | dump_gimple_stmt (MSG_NOTE, TDF_SLIM, pattern_stmt, 0); |
78c60e3d | 2715 | } |
363477c0 | 2716 | |
9771b263 | 2717 | stmts->safe_push (last_stmt); |
363477c0 JJ |
2718 | |
2719 | *type_in = vectype; | |
2720 | *type_out = vectype; | |
2721 | return pattern_stmt; | |
2722 | } | |
2723 | ||
69d2aade JJ |
2724 | /* Function vect_recog_mixed_size_cond_pattern |
2725 | ||
2726 | Try to find the following pattern: | |
2727 | ||
2728 | type x_t, y_t; | |
2729 | TYPE a_T, b_T, c_T; | |
2730 | loop: | |
2731 | S1 a_T = x_t CMP y_t ? b_T : c_T; | |
2732 | ||
2733 | where type 'TYPE' is an integral type which has different size | |
bc4fb355 | 2734 | from 'type'. b_T and c_T are either constants (and if 'TYPE' is wider |
69d2aade | 2735 | than 'type', the constants need to fit into an integer type |
bc4fb355 | 2736 | with the same width as 'type') or results of conversion from 'type'. |
69d2aade JJ |
2737 | |
2738 | Input: | |
2739 | ||
2740 | * LAST_STMT: A stmt from which the pattern search begins. | |
2741 | ||
2742 | Output: | |
2743 | ||
2744 | * TYPE_IN: The type of the input arguments to the pattern. | |
2745 | ||
2746 | * TYPE_OUT: The type of the output of this pattern. | |
2747 | ||
2748 | * Return value: A new stmt that will be used to replace the pattern. | |
2749 | Additionally a def_stmt is added. | |
2750 | ||
2751 | a_it = x_t CMP y_t ? b_it : c_it; | |
2752 | a_T = (TYPE) a_it; */ | |
2753 | ||
355fe088 TS |
2754 | static gimple * |
2755 | vect_recog_mixed_size_cond_pattern (vec<gimple *> *stmts, tree *type_in, | |
69d2aade JJ |
2756 | tree *type_out) |
2757 | { | |
355fe088 | 2758 | gimple *last_stmt = (*stmts)[0]; |
69d2aade JJ |
2759 | tree cond_expr, then_clause, else_clause; |
2760 | stmt_vec_info stmt_vinfo = vinfo_for_stmt (last_stmt), def_stmt_info; | |
bc4fb355 | 2761 | tree type, vectype, comp_vectype, itype = NULL_TREE, vecitype; |
355fe088 | 2762 | gimple *pattern_stmt, *def_stmt; |
310213d4 | 2763 | vec_info *vinfo = stmt_vinfo->vinfo; |
bc4fb355 | 2764 | tree orig_type0 = NULL_TREE, orig_type1 = NULL_TREE; |
355fe088 | 2765 | gimple *def_stmt0 = NULL, *def_stmt1 = NULL; |
bc4fb355 IR |
2766 | bool promotion; |
2767 | tree comp_scalar_type; | |
69d2aade JJ |
2768 | |
2769 | if (!is_gimple_assign (last_stmt) | |
2770 | || gimple_assign_rhs_code (last_stmt) != COND_EXPR | |
2771 | || STMT_VINFO_DEF_TYPE (stmt_vinfo) != vect_internal_def) | |
2772 | return NULL; | |
2773 | ||
2774 | cond_expr = gimple_assign_rhs1 (last_stmt); | |
2775 | then_clause = gimple_assign_rhs2 (last_stmt); | |
2776 | else_clause = gimple_assign_rhs3 (last_stmt); | |
2777 | ||
87aab9b2 JJ |
2778 | if (!COMPARISON_CLASS_P (cond_expr)) |
2779 | return NULL; | |
2780 | ||
bc4fb355 IR |
2781 | comp_scalar_type = TREE_TYPE (TREE_OPERAND (cond_expr, 0)); |
2782 | comp_vectype = get_vectype_for_scalar_type (comp_scalar_type); | |
87aab9b2 | 2783 | if (comp_vectype == NULL_TREE) |
69d2aade JJ |
2784 | return NULL; |
2785 | ||
2786 | type = gimple_expr_type (last_stmt); | |
bc4fb355 IR |
2787 | if (types_compatible_p (type, comp_scalar_type) |
2788 | || ((TREE_CODE (then_clause) != INTEGER_CST | |
2789 | || TREE_CODE (else_clause) != INTEGER_CST) | |
2790 | && !INTEGRAL_TYPE_P (comp_scalar_type)) | |
2791 | || !INTEGRAL_TYPE_P (type)) | |
2792 | return NULL; | |
2793 | ||
2794 | if ((TREE_CODE (then_clause) != INTEGER_CST | |
2795 | && !type_conversion_p (then_clause, last_stmt, false, &orig_type0, | |
2796 | &def_stmt0, &promotion)) | |
2797 | || (TREE_CODE (else_clause) != INTEGER_CST | |
2798 | && !type_conversion_p (else_clause, last_stmt, false, &orig_type1, | |
2799 | &def_stmt1, &promotion))) | |
2800 | return NULL; | |
2801 | ||
2802 | if (orig_type0 && orig_type1 | |
2803 | && !types_compatible_p (orig_type0, orig_type1)) | |
2804 | return NULL; | |
2805 | ||
2806 | if (orig_type0) | |
2807 | { | |
2808 | if (!types_compatible_p (orig_type0, comp_scalar_type)) | |
2809 | return NULL; | |
2810 | then_clause = gimple_assign_rhs1 (def_stmt0); | |
2811 | itype = orig_type0; | |
2812 | } | |
2813 | ||
2814 | if (orig_type1) | |
2815 | { | |
2816 | if (!types_compatible_p (orig_type1, comp_scalar_type)) | |
2817 | return NULL; | |
2818 | else_clause = gimple_assign_rhs1 (def_stmt1); | |
2819 | itype = orig_type1; | |
2820 | } | |
2821 | ||
69d2aade | 2822 | |
6c825cd4 DS |
2823 | HOST_WIDE_INT cmp_mode_size |
2824 | = GET_MODE_UNIT_BITSIZE (TYPE_MODE (comp_vectype)); | |
2825 | ||
2826 | if (GET_MODE_BITSIZE (TYPE_MODE (type)) == cmp_mode_size) | |
69d2aade JJ |
2827 | return NULL; |
2828 | ||
2829 | vectype = get_vectype_for_scalar_type (type); | |
2830 | if (vectype == NULL_TREE) | |
2831 | return NULL; | |
2832 | ||
2833 | if (expand_vec_cond_expr_p (vectype, comp_vectype)) | |
2834 | return NULL; | |
2835 | ||
bc4fb355 | 2836 | if (itype == NULL_TREE) |
6c825cd4 | 2837 | itype = build_nonstandard_integer_type (cmp_mode_size, |
bc4fb355 IR |
2838 | TYPE_UNSIGNED (type)); |
2839 | ||
69d2aade | 2840 | if (itype == NULL_TREE |
6c825cd4 | 2841 | || GET_MODE_BITSIZE (TYPE_MODE (itype)) != cmp_mode_size) |
69d2aade JJ |
2842 | return NULL; |
2843 | ||
2844 | vecitype = get_vectype_for_scalar_type (itype); | |
2845 | if (vecitype == NULL_TREE) | |
2846 | return NULL; | |
2847 | ||
2848 | if (!expand_vec_cond_expr_p (vecitype, comp_vectype)) | |
2849 | return NULL; | |
2850 | ||
6c825cd4 | 2851 | if (GET_MODE_BITSIZE (TYPE_MODE (type)) > cmp_mode_size) |
69d2aade | 2852 | { |
bc4fb355 IR |
2853 | if ((TREE_CODE (then_clause) == INTEGER_CST |
2854 | && !int_fits_type_p (then_clause, itype)) | |
2855 | || (TREE_CODE (else_clause) == INTEGER_CST | |
2856 | && !int_fits_type_p (else_clause, itype))) | |
69d2aade JJ |
2857 | return NULL; |
2858 | } | |
2859 | ||
0d0e4a03 JJ |
2860 | def_stmt = gimple_build_assign (vect_recog_temp_ssa_var (itype, NULL), |
2861 | COND_EXPR, unshare_expr (cond_expr), | |
2862 | fold_convert (itype, then_clause), | |
2863 | fold_convert (itype, else_clause)); | |
2864 | pattern_stmt = gimple_build_assign (vect_recog_temp_ssa_var (type, NULL), | |
2865 | NOP_EXPR, gimple_assign_lhs (def_stmt)); | |
69d2aade | 2866 | |
083481d8 | 2867 | new_pattern_def_seq (stmt_vinfo, def_stmt); |
310213d4 | 2868 | def_stmt_info = new_stmt_vec_info (def_stmt, vinfo); |
69d2aade JJ |
2869 | set_vinfo_for_stmt (def_stmt, def_stmt_info); |
2870 | STMT_VINFO_VECTYPE (def_stmt_info) = vecitype; | |
2871 | *type_in = vecitype; | |
2872 | *type_out = vectype; | |
2873 | ||
73fbfcad | 2874 | if (dump_enabled_p ()) |
ccb3ad87 | 2875 | dump_printf_loc (MSG_NOTE, vect_location, |
e645e942 | 2876 | "vect_recog_mixed_size_cond_pattern: detected:\n"); |
f5709183 | 2877 | |
69d2aade JJ |
2878 | return pattern_stmt; |
2879 | } | |
2880 | ||
2881 | ||
71c92d17 | 2882 | /* Helper function of vect_recog_bool_pattern. Called recursively, return |
42fd8198 IE |
2883 | true if bool VAR can and should be optimized that way. Assume it shouldn't |
2884 | in case it's a result of a comparison which can be directly vectorized into | |
fa2c9034 RB |
2885 | a vector comparison. Fills in STMTS with all stmts visited during the |
2886 | walk. */ | |
71c92d17 JJ |
2887 | |
2888 | static bool | |
fa2c9034 | 2889 | check_bool_pattern (tree var, vec_info *vinfo, hash_set<gimple *> &stmts) |
71c92d17 | 2890 | { |
355fe088 | 2891 | gimple *def_stmt; |
71c92d17 | 2892 | enum vect_def_type dt; |
81c40241 | 2893 | tree rhs1; |
71c92d17 JJ |
2894 | enum tree_code rhs_code; |
2895 | ||
81c40241 | 2896 | if (!vect_is_simple_use (var, vinfo, &def_stmt, &dt)) |
71c92d17 JJ |
2897 | return false; |
2898 | ||
2899 | if (dt != vect_internal_def) | |
2900 | return false; | |
2901 | ||
2902 | if (!is_gimple_assign (def_stmt)) | |
2903 | return false; | |
2904 | ||
fa2c9034 RB |
2905 | if (stmts.contains (def_stmt)) |
2906 | return true; | |
71c92d17 JJ |
2907 | |
2908 | rhs1 = gimple_assign_rhs1 (def_stmt); | |
2909 | rhs_code = gimple_assign_rhs_code (def_stmt); | |
2910 | switch (rhs_code) | |
2911 | { | |
2912 | case SSA_NAME: | |
fa2c9034 RB |
2913 | if (! check_bool_pattern (rhs1, vinfo, stmts)) |
2914 | return false; | |
2915 | break; | |
71c92d17 JJ |
2916 | |
2917 | CASE_CONVERT: | |
2918 | if ((TYPE_PRECISION (TREE_TYPE (rhs1)) != 1 | |
2919 | || !TYPE_UNSIGNED (TREE_TYPE (rhs1))) | |
2920 | && TREE_CODE (TREE_TYPE (rhs1)) != BOOLEAN_TYPE) | |
2921 | return false; | |
fa2c9034 RB |
2922 | if (! check_bool_pattern (rhs1, vinfo, stmts)) |
2923 | return false; | |
2924 | break; | |
71c92d17 JJ |
2925 | |
2926 | case BIT_NOT_EXPR: | |
fa2c9034 RB |
2927 | if (! check_bool_pattern (rhs1, vinfo, stmts)) |
2928 | return false; | |
2929 | break; | |
71c92d17 JJ |
2930 | |
2931 | case BIT_AND_EXPR: | |
2932 | case BIT_IOR_EXPR: | |
2933 | case BIT_XOR_EXPR: | |
fa2c9034 RB |
2934 | if (! check_bool_pattern (rhs1, vinfo, stmts) |
2935 | || ! check_bool_pattern (gimple_assign_rhs2 (def_stmt), vinfo, stmts)) | |
71c92d17 | 2936 | return false; |
fa2c9034 | 2937 | break; |
71c92d17 JJ |
2938 | |
2939 | default: | |
2940 | if (TREE_CODE_CLASS (rhs_code) == tcc_comparison) | |
2941 | { | |
fa2c9034 | 2942 | tree vecitype, comp_vectype; |
71c92d17 | 2943 | |
2f326699 JJ |
2944 | /* If the comparison can throw, then is_gimple_condexpr will be |
2945 | false and we can't make a COND_EXPR/VEC_COND_EXPR out of it. */ | |
2946 | if (stmt_could_throw_p (def_stmt)) | |
2947 | return false; | |
2948 | ||
71c92d17 JJ |
2949 | comp_vectype = get_vectype_for_scalar_type (TREE_TYPE (rhs1)); |
2950 | if (comp_vectype == NULL_TREE) | |
2951 | return false; | |
2952 | ||
fa2c9034 | 2953 | tree mask_type = get_mask_type_for_scalar_type (TREE_TYPE (rhs1)); |
42fd8198 IE |
2954 | if (mask_type |
2955 | && expand_vec_cmp_expr_p (comp_vectype, mask_type)) | |
2956 | return false; | |
2957 | ||
71c92d17 JJ |
2958 | if (TREE_CODE (TREE_TYPE (rhs1)) != INTEGER_TYPE) |
2959 | { | |
ef4bddc2 | 2960 | machine_mode mode = TYPE_MODE (TREE_TYPE (rhs1)); |
71c92d17 | 2961 | tree itype |
ab0ef706 | 2962 | = build_nonstandard_integer_type (GET_MODE_BITSIZE (mode), 1); |
71c92d17 JJ |
2963 | vecitype = get_vectype_for_scalar_type (itype); |
2964 | if (vecitype == NULL_TREE) | |
2965 | return false; | |
2966 | } | |
2967 | else | |
2968 | vecitype = comp_vectype; | |
fa2c9034 RB |
2969 | if (! expand_vec_cond_expr_p (vecitype, comp_vectype)) |
2970 | return false; | |
71c92d17 | 2971 | } |
fa2c9034 RB |
2972 | else |
2973 | return false; | |
2974 | break; | |
71c92d17 | 2975 | } |
fa2c9034 RB |
2976 | |
2977 | bool res = stmts.add (def_stmt); | |
2978 | /* We can't end up recursing when just visiting SSA defs but not PHIs. */ | |
2979 | gcc_assert (!res); | |
2980 | ||
2981 | return true; | |
71c92d17 JJ |
2982 | } |
2983 | ||
2984 | ||
2985 | /* Helper function of adjust_bool_pattern. Add a cast to TYPE to a previous | |
fa2c9034 RB |
2986 | stmt (SSA_NAME_DEF_STMT of VAR) adding a cast to STMT_INFOs |
2987 | pattern sequence. */ | |
71c92d17 JJ |
2988 | |
2989 | static tree | |
fa2c9034 | 2990 | adjust_bool_pattern_cast (tree type, tree var, stmt_vec_info stmt_info) |
71c92d17 | 2991 | { |
fa2c9034 RB |
2992 | gimple *cast_stmt = gimple_build_assign (vect_recog_temp_ssa_var (type, NULL), |
2993 | NOP_EXPR, var); | |
2994 | stmt_vec_info patt_vinfo = new_stmt_vec_info (cast_stmt, stmt_info->vinfo); | |
2995 | set_vinfo_for_stmt (cast_stmt, patt_vinfo); | |
2996 | STMT_VINFO_VECTYPE (patt_vinfo) = get_vectype_for_scalar_type (type); | |
2997 | append_pattern_def_seq (stmt_info, cast_stmt); | |
71c92d17 JJ |
2998 | return gimple_assign_lhs (cast_stmt); |
2999 | } | |
3000 | ||
fa2c9034 RB |
3001 | /* Helper function of vect_recog_bool_pattern. Do the actual transformations. |
3002 | VAR is an SSA_NAME that should be transformed from bool to a wider integer | |
3003 | type, OUT_TYPE is the desired final integer type of the whole pattern. | |
3004 | STMT_INFO is the info of the pattern root and is where pattern stmts should | |
3005 | be associated with. DEFS is a map of pattern defs. */ | |
71c92d17 | 3006 | |
fa2c9034 RB |
3007 | static void |
3008 | adjust_bool_pattern (tree var, tree out_type, | |
3009 | stmt_vec_info stmt_info, hash_map <tree, tree> &defs) | |
71c92d17 | 3010 | { |
355fe088 | 3011 | gimple *stmt = SSA_NAME_DEF_STMT (var); |
71c92d17 JJ |
3012 | enum tree_code rhs_code, def_rhs_code; |
3013 | tree itype, cond_expr, rhs1, rhs2, irhs1, irhs2; | |
3014 | location_t loc; | |
355fe088 | 3015 | gimple *pattern_stmt, *def_stmt; |
fa2c9034 | 3016 | tree trueval = NULL_TREE; |
71c92d17 JJ |
3017 | |
3018 | rhs1 = gimple_assign_rhs1 (stmt); | |
3019 | rhs2 = gimple_assign_rhs2 (stmt); | |
3020 | rhs_code = gimple_assign_rhs_code (stmt); | |
3021 | loc = gimple_location (stmt); | |
3022 | switch (rhs_code) | |
3023 | { | |
3024 | case SSA_NAME: | |
3025 | CASE_CONVERT: | |
fa2c9034 | 3026 | irhs1 = *defs.get (rhs1); |
71c92d17 JJ |
3027 | itype = TREE_TYPE (irhs1); |
3028 | pattern_stmt | |
0d0e4a03 JJ |
3029 | = gimple_build_assign (vect_recog_temp_ssa_var (itype, NULL), |
3030 | SSA_NAME, irhs1); | |
71c92d17 JJ |
3031 | break; |
3032 | ||
3033 | case BIT_NOT_EXPR: | |
fa2c9034 | 3034 | irhs1 = *defs.get (rhs1); |
71c92d17 JJ |
3035 | itype = TREE_TYPE (irhs1); |
3036 | pattern_stmt | |
0d0e4a03 JJ |
3037 | = gimple_build_assign (vect_recog_temp_ssa_var (itype, NULL), |
3038 | BIT_XOR_EXPR, irhs1, build_int_cst (itype, 1)); | |
71c92d17 JJ |
3039 | break; |
3040 | ||
3041 | case BIT_AND_EXPR: | |
3042 | /* Try to optimize x = y & (a < b ? 1 : 0); into | |
3043 | x = (a < b ? y : 0); | |
3044 | ||
3045 | E.g. for: | |
3046 | bool a_b, b_b, c_b; | |
3047 | TYPE d_T; | |
3048 | ||
3049 | S1 a_b = x1 CMP1 y1; | |
3050 | S2 b_b = x2 CMP2 y2; | |
3051 | S3 c_b = a_b & b_b; | |
3052 | S4 d_T = (TYPE) c_b; | |
3053 | ||
3054 | we would normally emit: | |
3055 | ||
3056 | S1' a_T = x1 CMP1 y1 ? 1 : 0; | |
3057 | S2' b_T = x2 CMP2 y2 ? 1 : 0; | |
3058 | S3' c_T = a_T & b_T; | |
3059 | S4' d_T = c_T; | |
3060 | ||
3061 | but we can save one stmt by using the | |
3062 | result of one of the COND_EXPRs in the other COND_EXPR and leave | |
3063 | BIT_AND_EXPR stmt out: | |
3064 | ||
3065 | S1' a_T = x1 CMP1 y1 ? 1 : 0; | |
3066 | S3' c_T = x2 CMP2 y2 ? a_T : 0; | |
3067 | S4' f_T = c_T; | |
3068 | ||
3069 | At least when VEC_COND_EXPR is implemented using masks | |
3070 | cond ? 1 : 0 is as expensive as cond ? var : 0, in both cases it | |
3071 | computes the comparison masks and ands it, in one case with | |
3072 | all ones vector, in the other case with a vector register. | |
3073 | Don't do this for BIT_IOR_EXPR, because cond ? 1 : var; is | |
3074 | often more expensive. */ | |
3075 | def_stmt = SSA_NAME_DEF_STMT (rhs2); | |
3076 | def_rhs_code = gimple_assign_rhs_code (def_stmt); | |
3077 | if (TREE_CODE_CLASS (def_rhs_code) == tcc_comparison) | |
3078 | { | |
fa2c9034 | 3079 | irhs1 = *defs.get (rhs1); |
71c92d17 | 3080 | tree def_rhs1 = gimple_assign_rhs1 (def_stmt); |
71c92d17 JJ |
3081 | if (TYPE_PRECISION (TREE_TYPE (irhs1)) |
3082 | == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (def_rhs1)))) | |
3083 | { | |
fa2c9034 RB |
3084 | rhs_code = def_rhs_code; |
3085 | rhs1 = def_rhs1; | |
3086 | rhs2 = gimple_assign_rhs2 (def_stmt); | |
3087 | trueval = irhs1; | |
3088 | goto do_compare; | |
71c92d17 JJ |
3089 | } |
3090 | else | |
fa2c9034 | 3091 | irhs2 = *defs.get (rhs2); |
71c92d17 JJ |
3092 | goto and_ior_xor; |
3093 | } | |
3094 | def_stmt = SSA_NAME_DEF_STMT (rhs1); | |
3095 | def_rhs_code = gimple_assign_rhs_code (def_stmt); | |
3096 | if (TREE_CODE_CLASS (def_rhs_code) == tcc_comparison) | |
3097 | { | |
fa2c9034 | 3098 | irhs2 = *defs.get (rhs2); |
71c92d17 | 3099 | tree def_rhs1 = gimple_assign_rhs1 (def_stmt); |
71c92d17 JJ |
3100 | if (TYPE_PRECISION (TREE_TYPE (irhs2)) |
3101 | == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (def_rhs1)))) | |
3102 | { | |
fa2c9034 RB |
3103 | rhs_code = def_rhs_code; |
3104 | rhs1 = def_rhs1; | |
3105 | rhs2 = gimple_assign_rhs2 (def_stmt); | |
3106 | trueval = irhs2; | |
3107 | goto do_compare; | |
71c92d17 JJ |
3108 | } |
3109 | else | |
fa2c9034 | 3110 | irhs1 = *defs.get (rhs1); |
71c92d17 JJ |
3111 | goto and_ior_xor; |
3112 | } | |
3113 | /* FALLTHRU */ | |
3114 | case BIT_IOR_EXPR: | |
3115 | case BIT_XOR_EXPR: | |
fa2c9034 RB |
3116 | irhs1 = *defs.get (rhs1); |
3117 | irhs2 = *defs.get (rhs2); | |
71c92d17 JJ |
3118 | and_ior_xor: |
3119 | if (TYPE_PRECISION (TREE_TYPE (irhs1)) | |
3120 | != TYPE_PRECISION (TREE_TYPE (irhs2))) | |
3121 | { | |
3122 | int prec1 = TYPE_PRECISION (TREE_TYPE (irhs1)); | |
3123 | int prec2 = TYPE_PRECISION (TREE_TYPE (irhs2)); | |
3124 | int out_prec = TYPE_PRECISION (out_type); | |
3125 | if (absu_hwi (out_prec - prec1) < absu_hwi (out_prec - prec2)) | |
fa2c9034 RB |
3126 | irhs2 = adjust_bool_pattern_cast (TREE_TYPE (irhs1), irhs2, |
3127 | stmt_info); | |
71c92d17 | 3128 | else if (absu_hwi (out_prec - prec1) > absu_hwi (out_prec - prec2)) |
fa2c9034 RB |
3129 | irhs1 = adjust_bool_pattern_cast (TREE_TYPE (irhs2), irhs1, |
3130 | stmt_info); | |
71c92d17 JJ |
3131 | else |
3132 | { | |
fa2c9034 RB |
3133 | irhs1 = adjust_bool_pattern_cast (out_type, irhs1, stmt_info); |
3134 | irhs2 = adjust_bool_pattern_cast (out_type, irhs2, stmt_info); | |
71c92d17 JJ |
3135 | } |
3136 | } | |
3137 | itype = TREE_TYPE (irhs1); | |
3138 | pattern_stmt | |
0d0e4a03 JJ |
3139 | = gimple_build_assign (vect_recog_temp_ssa_var (itype, NULL), |
3140 | rhs_code, irhs1, irhs2); | |
71c92d17 JJ |
3141 | break; |
3142 | ||
3143 | default: | |
fa2c9034 | 3144 | do_compare: |
71c92d17 JJ |
3145 | gcc_assert (TREE_CODE_CLASS (rhs_code) == tcc_comparison); |
3146 | if (TREE_CODE (TREE_TYPE (rhs1)) != INTEGER_TYPE | |
e6a21dd2 JJ |
3147 | || !TYPE_UNSIGNED (TREE_TYPE (rhs1)) |
3148 | || (TYPE_PRECISION (TREE_TYPE (rhs1)) | |
3149 | != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (rhs1))))) | |
71c92d17 | 3150 | { |
ef4bddc2 | 3151 | machine_mode mode = TYPE_MODE (TREE_TYPE (rhs1)); |
71c92d17 | 3152 | itype |
ab0ef706 | 3153 | = build_nonstandard_integer_type (GET_MODE_BITSIZE (mode), 1); |
71c92d17 JJ |
3154 | } |
3155 | else | |
3156 | itype = TREE_TYPE (rhs1); | |
3157 | cond_expr = build2_loc (loc, rhs_code, itype, rhs1, rhs2); | |
3158 | if (trueval == NULL_TREE) | |
3159 | trueval = build_int_cst (itype, 1); | |
3160 | else | |
3161 | gcc_checking_assert (useless_type_conversion_p (itype, | |
3162 | TREE_TYPE (trueval))); | |
3163 | pattern_stmt | |
0d0e4a03 JJ |
3164 | = gimple_build_assign (vect_recog_temp_ssa_var (itype, NULL), |
3165 | COND_EXPR, cond_expr, trueval, | |
3166 | build_int_cst (itype, 0)); | |
71c92d17 JJ |
3167 | break; |
3168 | } | |
3169 | ||
71c92d17 | 3170 | gimple_set_location (pattern_stmt, loc); |
fa2c9034 RB |
3171 | /* ??? Why does vect_mark_pattern_stmts set the vector type on all |
3172 | pattern def seq stmts instead of just letting auto-detection do | |
3173 | its work? */ | |
3174 | stmt_vec_info patt_vinfo = new_stmt_vec_info (pattern_stmt, stmt_info->vinfo); | |
3175 | set_vinfo_for_stmt (pattern_stmt, patt_vinfo); | |
3176 | STMT_VINFO_VECTYPE (patt_vinfo) = get_vectype_for_scalar_type (itype); | |
3177 | append_pattern_def_seq (stmt_info, pattern_stmt); | |
3178 | defs.put (var, gimple_assign_lhs (pattern_stmt)); | |
3179 | } | |
3180 | ||
3181 | /* Comparison function to qsort a vector of gimple stmts after UID. */ | |
3182 | ||
3183 | static int | |
3184 | sort_after_uid (const void *p1, const void *p2) | |
3185 | { | |
3186 | const gimple *stmt1 = *(const gimple * const *)p1; | |
3187 | const gimple *stmt2 = *(const gimple * const *)p2; | |
3188 | return gimple_uid (stmt1) - gimple_uid (stmt2); | |
71c92d17 JJ |
3189 | } |
3190 | ||
fa2c9034 RB |
3191 | /* Create pattern stmts for all stmts participating in the bool pattern |
3192 | specified by BOOL_STMT_SET and its root STMT with the desired type | |
3193 | OUT_TYPE. Return the def of the pattern root. */ | |
3194 | ||
3195 | static tree | |
3196 | adjust_bool_stmts (hash_set <gimple *> &bool_stmt_set, | |
3197 | tree out_type, gimple *stmt) | |
3198 | { | |
3199 | /* Gather original stmts in the bool pattern in their order of appearance | |
3200 | in the IL. */ | |
3201 | auto_vec<gimple *> bool_stmts (bool_stmt_set.elements ()); | |
3202 | for (hash_set <gimple *>::iterator i = bool_stmt_set.begin (); | |
3203 | i != bool_stmt_set.end (); ++i) | |
3204 | bool_stmts.quick_push (*i); | |
3205 | bool_stmts.qsort (sort_after_uid); | |
3206 | ||
3207 | /* Now process them in that order, producing pattern stmts. */ | |
3208 | hash_map <tree, tree> defs; | |
3209 | for (unsigned i = 0; i < bool_stmts.length (); ++i) | |
3210 | adjust_bool_pattern (gimple_assign_lhs (bool_stmts[i]), | |
3211 | out_type, vinfo_for_stmt (stmt), defs); | |
3212 | ||
3213 | /* Pop the last pattern seq stmt and install it as pattern root for STMT. */ | |
3214 | gimple *pattern_stmt | |
3215 | = gimple_seq_last_stmt (STMT_VINFO_PATTERN_DEF_SEQ (vinfo_for_stmt (stmt))); | |
3216 | return gimple_assign_lhs (pattern_stmt); | |
3217 | } | |
71c92d17 | 3218 | |
42fd8198 IE |
3219 | /* Return the proper type for converting bool VAR into |
3220 | an integer value or NULL_TREE if no such type exists. | |
3221 | The type is chosen so that converted value has the | |
3222 | same number of elements as VAR's vector type. */ | |
3223 | ||
3224 | static tree | |
3225 | search_type_for_mask (tree var, vec_info *vinfo) | |
3226 | { | |
3227 | gimple *def_stmt; | |
3228 | enum vect_def_type dt; | |
3229 | tree rhs1; | |
3230 | enum tree_code rhs_code; | |
e6f5c25d | 3231 | tree res = NULL_TREE, res2; |
42fd8198 IE |
3232 | |
3233 | if (TREE_CODE (var) != SSA_NAME) | |
3234 | return NULL_TREE; | |
3235 | ||
3236 | if ((TYPE_PRECISION (TREE_TYPE (var)) != 1 | |
3237 | || !TYPE_UNSIGNED (TREE_TYPE (var))) | |
3238 | && TREE_CODE (TREE_TYPE (var)) != BOOLEAN_TYPE) | |
3239 | return NULL_TREE; | |
3240 | ||
3241 | if (!vect_is_simple_use (var, vinfo, &def_stmt, &dt)) | |
3242 | return NULL_TREE; | |
3243 | ||
3244 | if (dt != vect_internal_def) | |
3245 | return NULL_TREE; | |
3246 | ||
3247 | if (!is_gimple_assign (def_stmt)) | |
3248 | return NULL_TREE; | |
3249 | ||
3250 | rhs_code = gimple_assign_rhs_code (def_stmt); | |
3251 | rhs1 = gimple_assign_rhs1 (def_stmt); | |
3252 | ||
3253 | switch (rhs_code) | |
3254 | { | |
3255 | case SSA_NAME: | |
3256 | case BIT_NOT_EXPR: | |
3257 | CASE_CONVERT: | |
3258 | res = search_type_for_mask (rhs1, vinfo); | |
3259 | break; | |
3260 | ||
3261 | case BIT_AND_EXPR: | |
3262 | case BIT_IOR_EXPR: | |
3263 | case BIT_XOR_EXPR: | |
e6f5c25d IE |
3264 | res = search_type_for_mask (rhs1, vinfo); |
3265 | res2 = search_type_for_mask (gimple_assign_rhs2 (def_stmt), vinfo); | |
3266 | if (!res || (res2 && TYPE_PRECISION (res) > TYPE_PRECISION (res2))) | |
3267 | res = res2; | |
42fd8198 IE |
3268 | break; |
3269 | ||
3270 | default: | |
3271 | if (TREE_CODE_CLASS (rhs_code) == tcc_comparison) | |
3272 | { | |
e6f5c25d IE |
3273 | tree comp_vectype, mask_type; |
3274 | ||
af3cdd34 IE |
3275 | if (TREE_CODE (TREE_TYPE (rhs1)) == BOOLEAN_TYPE) |
3276 | { | |
3277 | res = search_type_for_mask (rhs1, vinfo); | |
3278 | res2 = search_type_for_mask (gimple_assign_rhs2 (def_stmt), vinfo); | |
3279 | if (!res || (res2 && TYPE_PRECISION (res) > TYPE_PRECISION (res2))) | |
3280 | res = res2; | |
3281 | break; | |
3282 | } | |
3283 | ||
e6f5c25d IE |
3284 | comp_vectype = get_vectype_for_scalar_type (TREE_TYPE (rhs1)); |
3285 | if (comp_vectype == NULL_TREE) | |
3286 | return NULL_TREE; | |
3287 | ||
3288 | mask_type = get_mask_type_for_scalar_type (TREE_TYPE (rhs1)); | |
3289 | if (!mask_type | |
3290 | || !expand_vec_cmp_expr_p (comp_vectype, mask_type)) | |
3291 | return NULL_TREE; | |
3292 | ||
42fd8198 IE |
3293 | if (TREE_CODE (TREE_TYPE (rhs1)) != INTEGER_TYPE |
3294 | || !TYPE_UNSIGNED (TREE_TYPE (rhs1))) | |
3295 | { | |
3296 | machine_mode mode = TYPE_MODE (TREE_TYPE (rhs1)); | |
3297 | res = build_nonstandard_integer_type (GET_MODE_BITSIZE (mode), 1); | |
3298 | } | |
3299 | else | |
3300 | res = TREE_TYPE (rhs1); | |
3301 | } | |
3302 | } | |
3303 | ||
3304 | return res; | |
3305 | } | |
3306 | ||
3307 | ||
71c92d17 JJ |
3308 | /* Function vect_recog_bool_pattern |
3309 | ||
3310 | Try to find pattern like following: | |
3311 | ||
3312 | bool a_b, b_b, c_b, d_b, e_b; | |
3313 | TYPE f_T; | |
3314 | loop: | |
3315 | S1 a_b = x1 CMP1 y1; | |
3316 | S2 b_b = x2 CMP2 y2; | |
3317 | S3 c_b = a_b & b_b; | |
3318 | S4 d_b = x3 CMP3 y3; | |
3319 | S5 e_b = c_b | d_b; | |
3320 | S6 f_T = (TYPE) e_b; | |
3321 | ||
52264dbf RB |
3322 | where type 'TYPE' is an integral type. Or a similar pattern |
3323 | ending in | |
3324 | ||
3325 | S6 f_Y = e_b ? r_Y : s_Y; | |
3326 | ||
3327 | as results from if-conversion of a complex condition. | |
71c92d17 JJ |
3328 | |
3329 | Input: | |
3330 | ||
3331 | * LAST_STMT: A stmt at the end from which the pattern | |
3332 | search begins, i.e. cast of a bool to | |
3333 | an integer type. | |
3334 | ||
3335 | Output: | |
3336 | ||
3337 | * TYPE_IN: The type of the input arguments to the pattern. | |
3338 | ||
3339 | * TYPE_OUT: The type of the output of this pattern. | |
3340 | ||
3341 | * Return value: A new stmt that will be used to replace the pattern. | |
3342 | ||
3343 | Assuming size of TYPE is the same as size of all comparisons | |
3344 | (otherwise some casts would be added where needed), the above | |
3345 | sequence we create related pattern stmts: | |
3346 | S1' a_T = x1 CMP1 y1 ? 1 : 0; | |
3347 | S3' c_T = x2 CMP2 y2 ? a_T : 0; | |
3348 | S4' d_T = x3 CMP3 y3 ? 1 : 0; | |
3349 | S5' e_T = c_T | d_T; | |
3350 | S6' f_T = e_T; | |
3351 | ||
3352 | Instead of the above S3' we could emit: | |
3353 | S2' b_T = x2 CMP2 y2 ? 1 : 0; | |
3354 | S3' c_T = a_T | b_T; | |
3355 | but the above is more efficient. */ | |
3356 | ||
355fe088 TS |
3357 | static gimple * |
3358 | vect_recog_bool_pattern (vec<gimple *> *stmts, tree *type_in, | |
71c92d17 JJ |
3359 | tree *type_out) |
3360 | { | |
355fe088 | 3361 | gimple *last_stmt = stmts->pop (); |
71c92d17 JJ |
3362 | enum tree_code rhs_code; |
3363 | tree var, lhs, rhs, vectype; | |
3364 | stmt_vec_info stmt_vinfo = vinfo_for_stmt (last_stmt); | |
42fd8198 | 3365 | stmt_vec_info new_stmt_info; |
310213d4 | 3366 | vec_info *vinfo = stmt_vinfo->vinfo; |
355fe088 | 3367 | gimple *pattern_stmt; |
71c92d17 JJ |
3368 | |
3369 | if (!is_gimple_assign (last_stmt)) | |
3370 | return NULL; | |
3371 | ||
3372 | var = gimple_assign_rhs1 (last_stmt); | |
3373 | lhs = gimple_assign_lhs (last_stmt); | |
3374 | ||
3375 | if ((TYPE_PRECISION (TREE_TYPE (var)) != 1 | |
3376 | || !TYPE_UNSIGNED (TREE_TYPE (var))) | |
3377 | && TREE_CODE (TREE_TYPE (var)) != BOOLEAN_TYPE) | |
3378 | return NULL; | |
3379 | ||
fa2c9034 RB |
3380 | hash_set<gimple *> bool_stmts; |
3381 | ||
71c92d17 JJ |
3382 | rhs_code = gimple_assign_rhs_code (last_stmt); |
3383 | if (CONVERT_EXPR_CODE_P (rhs_code)) | |
3384 | { | |
78048b1c JJ |
3385 | if (TREE_CODE (TREE_TYPE (lhs)) != INTEGER_TYPE |
3386 | || TYPE_PRECISION (TREE_TYPE (lhs)) == 1) | |
71c92d17 JJ |
3387 | return NULL; |
3388 | vectype = get_vectype_for_scalar_type (TREE_TYPE (lhs)); | |
3389 | if (vectype == NULL_TREE) | |
3390 | return NULL; | |
3391 | ||
fa2c9034 | 3392 | if (check_bool_pattern (var, vinfo, bool_stmts)) |
42fd8198 | 3393 | { |
fa2c9034 | 3394 | rhs = adjust_bool_stmts (bool_stmts, TREE_TYPE (lhs), last_stmt); |
42fd8198 IE |
3395 | lhs = vect_recog_temp_ssa_var (TREE_TYPE (lhs), NULL); |
3396 | if (useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (rhs))) | |
3397 | pattern_stmt = gimple_build_assign (lhs, SSA_NAME, rhs); | |
3398 | else | |
3399 | pattern_stmt | |
3400 | = gimple_build_assign (lhs, NOP_EXPR, rhs); | |
3401 | } | |
71c92d17 | 3402 | else |
42fd8198 IE |
3403 | { |
3404 | tree type = search_type_for_mask (var, vinfo); | |
a414c77f | 3405 | tree cst0, cst1, tmp; |
42fd8198 IE |
3406 | |
3407 | if (!type) | |
3408 | return NULL; | |
3409 | ||
3410 | /* We may directly use cond with narrowed type to avoid | |
3411 | multiple cond exprs with following result packing and | |
3412 | perform single cond with packed mask instead. In case | |
3413 | of widening we better make cond first and then extract | |
3414 | results. */ | |
3415 | if (TYPE_MODE (type) == TYPE_MODE (TREE_TYPE (lhs))) | |
3416 | type = TREE_TYPE (lhs); | |
3417 | ||
3418 | cst0 = build_int_cst (type, 0); | |
3419 | cst1 = build_int_cst (type, 1); | |
3420 | tmp = vect_recog_temp_ssa_var (type, NULL); | |
a414c77f | 3421 | pattern_stmt = gimple_build_assign (tmp, COND_EXPR, var, cst1, cst0); |
42fd8198 IE |
3422 | |
3423 | if (!useless_type_conversion_p (type, TREE_TYPE (lhs))) | |
3424 | { | |
3425 | tree new_vectype = get_vectype_for_scalar_type (type); | |
3426 | new_stmt_info = new_stmt_vec_info (pattern_stmt, vinfo); | |
3427 | set_vinfo_for_stmt (pattern_stmt, new_stmt_info); | |
3428 | STMT_VINFO_VECTYPE (new_stmt_info) = new_vectype; | |
3429 | new_pattern_def_seq (stmt_vinfo, pattern_stmt); | |
3430 | ||
3431 | lhs = vect_recog_temp_ssa_var (TREE_TYPE (lhs), NULL); | |
3432 | pattern_stmt = gimple_build_assign (lhs, CONVERT_EXPR, tmp); | |
3433 | } | |
3434 | } | |
3435 | ||
71c92d17 JJ |
3436 | *type_out = vectype; |
3437 | *type_in = vectype; | |
9771b263 | 3438 | stmts->safe_push (last_stmt); |
73fbfcad | 3439 | if (dump_enabled_p ()) |
ccb3ad87 | 3440 | dump_printf_loc (MSG_NOTE, vect_location, |
e645e942 | 3441 | "vect_recog_bool_pattern: detected:\n"); |
f5709183 | 3442 | |
52264dbf RB |
3443 | return pattern_stmt; |
3444 | } | |
3445 | else if (rhs_code == COND_EXPR | |
3446 | && TREE_CODE (var) == SSA_NAME) | |
3447 | { | |
3448 | vectype = get_vectype_for_scalar_type (TREE_TYPE (lhs)); | |
3449 | if (vectype == NULL_TREE) | |
3450 | return NULL; | |
3451 | ||
3452 | /* Build a scalar type for the boolean result that when | |
3453 | vectorized matches the vector type of the result in | |
3454 | size and number of elements. */ | |
3455 | unsigned prec | |
3456 | = wi::udiv_trunc (TYPE_SIZE (vectype), | |
3457 | TYPE_VECTOR_SUBPARTS (vectype)).to_uhwi (); | |
3458 | tree type | |
3459 | = build_nonstandard_integer_type (prec, | |
3460 | TYPE_UNSIGNED (TREE_TYPE (var))); | |
3461 | if (get_vectype_for_scalar_type (type) == NULL_TREE) | |
3462 | return NULL; | |
3463 | ||
fa2c9034 | 3464 | if (!check_bool_pattern (var, vinfo, bool_stmts)) |
a414c77f IE |
3465 | return NULL; |
3466 | ||
fa2c9034 | 3467 | rhs = adjust_bool_stmts (bool_stmts, type, last_stmt); |
52264dbf | 3468 | |
52264dbf RB |
3469 | lhs = vect_recog_temp_ssa_var (TREE_TYPE (lhs), NULL); |
3470 | pattern_stmt | |
a414c77f IE |
3471 | = gimple_build_assign (lhs, COND_EXPR, |
3472 | build2 (NE_EXPR, boolean_type_node, | |
3473 | rhs, build_int_cst (type, 0)), | |
0d0e4a03 JJ |
3474 | gimple_assign_rhs2 (last_stmt), |
3475 | gimple_assign_rhs3 (last_stmt)); | |
52264dbf RB |
3476 | *type_out = vectype; |
3477 | *type_in = vectype; | |
3478 | stmts->safe_push (last_stmt); | |
3479 | if (dump_enabled_p ()) | |
3480 | dump_printf_loc (MSG_NOTE, vect_location, | |
3481 | "vect_recog_bool_pattern: detected:\n"); | |
3482 | ||
71c92d17 JJ |
3483 | return pattern_stmt; |
3484 | } | |
ab0ef706 JJ |
3485 | else if (rhs_code == SSA_NAME |
3486 | && STMT_VINFO_DATA_REF (stmt_vinfo)) | |
3487 | { | |
3488 | stmt_vec_info pattern_stmt_info; | |
3489 | vectype = STMT_VINFO_VECTYPE (stmt_vinfo); | |
3490 | gcc_assert (vectype != NULL_TREE); | |
78336739 JJ |
3491 | if (!VECTOR_MODE_P (TYPE_MODE (vectype))) |
3492 | return NULL; | |
ab0ef706 | 3493 | |
fa2c9034 RB |
3494 | if (check_bool_pattern (var, vinfo, bool_stmts)) |
3495 | rhs = adjust_bool_stmts (bool_stmts, TREE_TYPE (vectype), last_stmt); | |
42fd8198 IE |
3496 | else |
3497 | { | |
3498 | tree type = search_type_for_mask (var, vinfo); | |
a414c77f | 3499 | tree cst0, cst1, new_vectype; |
42fd8198 IE |
3500 | |
3501 | if (!type) | |
3502 | return NULL; | |
3503 | ||
3504 | if (TYPE_MODE (type) == TYPE_MODE (TREE_TYPE (vectype))) | |
3505 | type = TREE_TYPE (vectype); | |
3506 | ||
3507 | cst0 = build_int_cst (type, 0); | |
3508 | cst1 = build_int_cst (type, 1); | |
3509 | new_vectype = get_vectype_for_scalar_type (type); | |
3510 | ||
3511 | rhs = vect_recog_temp_ssa_var (type, NULL); | |
a414c77f | 3512 | pattern_stmt = gimple_build_assign (rhs, COND_EXPR, var, cst1, cst0); |
42fd8198 IE |
3513 | |
3514 | pattern_stmt_info = new_stmt_vec_info (pattern_stmt, vinfo); | |
3515 | set_vinfo_for_stmt (pattern_stmt, pattern_stmt_info); | |
3516 | STMT_VINFO_VECTYPE (pattern_stmt_info) = new_vectype; | |
3517 | append_pattern_def_seq (stmt_vinfo, pattern_stmt); | |
3518 | } | |
3519 | ||
ab0ef706 JJ |
3520 | lhs = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (vectype), lhs); |
3521 | if (!useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (rhs))) | |
3522 | { | |
3523 | tree rhs2 = vect_recog_temp_ssa_var (TREE_TYPE (lhs), NULL); | |
355fe088 | 3524 | gimple *cast_stmt = gimple_build_assign (rhs2, NOP_EXPR, rhs); |
42fd8198 | 3525 | append_pattern_def_seq (stmt_vinfo, cast_stmt); |
ab0ef706 JJ |
3526 | rhs = rhs2; |
3527 | } | |
0d0e4a03 | 3528 | pattern_stmt = gimple_build_assign (lhs, SSA_NAME, rhs); |
310213d4 | 3529 | pattern_stmt_info = new_stmt_vec_info (pattern_stmt, vinfo); |
ab0ef706 JJ |
3530 | set_vinfo_for_stmt (pattern_stmt, pattern_stmt_info); |
3531 | STMT_VINFO_DATA_REF (pattern_stmt_info) | |
3532 | = STMT_VINFO_DATA_REF (stmt_vinfo); | |
3533 | STMT_VINFO_DR_BASE_ADDRESS (pattern_stmt_info) | |
3534 | = STMT_VINFO_DR_BASE_ADDRESS (stmt_vinfo); | |
3535 | STMT_VINFO_DR_INIT (pattern_stmt_info) = STMT_VINFO_DR_INIT (stmt_vinfo); | |
3536 | STMT_VINFO_DR_OFFSET (pattern_stmt_info) | |
3537 | = STMT_VINFO_DR_OFFSET (stmt_vinfo); | |
3538 | STMT_VINFO_DR_STEP (pattern_stmt_info) = STMT_VINFO_DR_STEP (stmt_vinfo); | |
3539 | STMT_VINFO_DR_ALIGNED_TO (pattern_stmt_info) | |
3540 | = STMT_VINFO_DR_ALIGNED_TO (stmt_vinfo); | |
78048b1c | 3541 | DR_STMT (STMT_VINFO_DATA_REF (stmt_vinfo)) = pattern_stmt; |
ab0ef706 JJ |
3542 | *type_out = vectype; |
3543 | *type_in = vectype; | |
9771b263 | 3544 | stmts->safe_push (last_stmt); |
73fbfcad | 3545 | if (dump_enabled_p ()) |
ccb3ad87 | 3546 | dump_printf_loc (MSG_NOTE, vect_location, |
e645e942 | 3547 | "vect_recog_bool_pattern: detected:\n"); |
ab0ef706 JJ |
3548 | return pattern_stmt; |
3549 | } | |
71c92d17 JJ |
3550 | else |
3551 | return NULL; | |
3552 | } | |
3553 | ||
3554 | ||
e6f5c25d IE |
3555 | /* A helper for vect_recog_mask_conversion_pattern. Build |
3556 | conversion of MASK to a type suitable for masking VECTYPE. | |
3557 | Built statement gets required vectype and is appended to | |
3558 | a pattern sequence of STMT_VINFO. | |
3559 | ||
3560 | Return converted mask. */ | |
3561 | ||
3562 | static tree | |
3563 | build_mask_conversion (tree mask, tree vectype, stmt_vec_info stmt_vinfo, | |
3564 | vec_info *vinfo) | |
3565 | { | |
3566 | gimple *stmt; | |
3567 | tree masktype, tmp; | |
3568 | stmt_vec_info new_stmt_info; | |
3569 | ||
3570 | masktype = build_same_sized_truth_vector_type (vectype); | |
3571 | tmp = vect_recog_temp_ssa_var (TREE_TYPE (masktype), NULL); | |
3572 | stmt = gimple_build_assign (tmp, CONVERT_EXPR, mask); | |
3573 | new_stmt_info = new_stmt_vec_info (stmt, vinfo); | |
3574 | set_vinfo_for_stmt (stmt, new_stmt_info); | |
3575 | STMT_VINFO_VECTYPE (new_stmt_info) = masktype; | |
3576 | append_pattern_def_seq (stmt_vinfo, stmt); | |
3577 | ||
3578 | return tmp; | |
3579 | } | |
3580 | ||
3581 | ||
3582 | /* Function vect_recog_mask_conversion_pattern | |
3583 | ||
3584 | Try to find statements which require boolean type | |
3585 | converison. Additional conversion statements are | |
3586 | added to handle such cases. For example: | |
3587 | ||
3588 | bool m_1, m_2, m_3; | |
3589 | int i_4, i_5; | |
3590 | double d_6, d_7; | |
3591 | char c_1, c_2, c_3; | |
3592 | ||
3593 | S1 m_1 = i_4 > i_5; | |
3594 | S2 m_2 = d_6 < d_7; | |
3595 | S3 m_3 = m_1 & m_2; | |
3596 | S4 c_1 = m_3 ? c_2 : c_3; | |
3597 | ||
3598 | Will be transformed into: | |
3599 | ||
3600 | S1 m_1 = i_4 > i_5; | |
3601 | S2 m_2 = d_6 < d_7; | |
3602 | S3'' m_2' = (_Bool[bitsize=32])m_2 | |
3603 | S3' m_3' = m_1 & m_2'; | |
3604 | S4'' m_3'' = (_Bool[bitsize=8])m_3' | |
3605 | S4' c_1' = m_3'' ? c_2 : c_3; */ | |
3606 | ||
3607 | static gimple * | |
3608 | vect_recog_mask_conversion_pattern (vec<gimple *> *stmts, tree *type_in, | |
3609 | tree *type_out) | |
3610 | { | |
3611 | gimple *last_stmt = stmts->pop (); | |
3612 | enum tree_code rhs_code; | |
310aba3b ML |
3613 | tree lhs = NULL_TREE, rhs1, rhs2, tmp, rhs1_type, rhs2_type; |
3614 | tree vectype1, vectype2; | |
e6f5c25d IE |
3615 | stmt_vec_info stmt_vinfo = vinfo_for_stmt (last_stmt); |
3616 | stmt_vec_info pattern_stmt_info; | |
3617 | vec_info *vinfo = stmt_vinfo->vinfo; | |
3618 | gimple *pattern_stmt; | |
3619 | ||
3620 | /* Check for MASK_LOAD ans MASK_STORE calls requiring mask conversion. */ | |
3621 | if (is_gimple_call (last_stmt) | |
3622 | && gimple_call_internal_p (last_stmt) | |
3623 | && (gimple_call_internal_fn (last_stmt) == IFN_MASK_STORE | |
3624 | || gimple_call_internal_fn (last_stmt) == IFN_MASK_LOAD)) | |
3625 | { | |
3626 | bool load = (gimple_call_internal_fn (last_stmt) == IFN_MASK_LOAD); | |
3627 | ||
3628 | if (load) | |
3629 | { | |
3630 | lhs = gimple_call_lhs (last_stmt); | |
3631 | vectype1 = get_vectype_for_scalar_type (TREE_TYPE (lhs)); | |
3632 | } | |
3633 | else | |
3634 | { | |
3635 | rhs2 = gimple_call_arg (last_stmt, 3); | |
3636 | vectype1 = get_vectype_for_scalar_type (TREE_TYPE (rhs2)); | |
3637 | } | |
3638 | ||
3639 | rhs1 = gimple_call_arg (last_stmt, 2); | |
3640 | rhs1_type = search_type_for_mask (rhs1, vinfo); | |
3641 | if (!rhs1_type) | |
3642 | return NULL; | |
3643 | vectype2 = get_mask_type_for_scalar_type (rhs1_type); | |
3644 | ||
3645 | if (!vectype1 || !vectype2 | |
3646 | || TYPE_VECTOR_SUBPARTS (vectype1) == TYPE_VECTOR_SUBPARTS (vectype2)) | |
3647 | return NULL; | |
3648 | ||
3649 | tmp = build_mask_conversion (rhs1, vectype1, stmt_vinfo, vinfo); | |
3650 | ||
3651 | if (load) | |
3652 | { | |
3653 | lhs = vect_recog_temp_ssa_var (TREE_TYPE (lhs), NULL); | |
3654 | pattern_stmt | |
3655 | = gimple_build_call_internal (IFN_MASK_LOAD, 3, | |
3656 | gimple_call_arg (last_stmt, 0), | |
3657 | gimple_call_arg (last_stmt, 1), | |
3658 | tmp); | |
3659 | gimple_call_set_lhs (pattern_stmt, lhs); | |
3660 | } | |
3661 | else | |
3662 | pattern_stmt | |
3663 | = gimple_build_call_internal (IFN_MASK_STORE, 4, | |
3664 | gimple_call_arg (last_stmt, 0), | |
3665 | gimple_call_arg (last_stmt, 1), | |
3666 | tmp, | |
3667 | gimple_call_arg (last_stmt, 3)); | |
3668 | ||
3669 | ||
3670 | pattern_stmt_info = new_stmt_vec_info (pattern_stmt, vinfo); | |
3671 | set_vinfo_for_stmt (pattern_stmt, pattern_stmt_info); | |
3672 | STMT_VINFO_DATA_REF (pattern_stmt_info) | |
3673 | = STMT_VINFO_DATA_REF (stmt_vinfo); | |
3674 | STMT_VINFO_DR_BASE_ADDRESS (pattern_stmt_info) | |
3675 | = STMT_VINFO_DR_BASE_ADDRESS (stmt_vinfo); | |
3676 | STMT_VINFO_DR_INIT (pattern_stmt_info) = STMT_VINFO_DR_INIT (stmt_vinfo); | |
3677 | STMT_VINFO_DR_OFFSET (pattern_stmt_info) | |
3678 | = STMT_VINFO_DR_OFFSET (stmt_vinfo); | |
3679 | STMT_VINFO_DR_STEP (pattern_stmt_info) = STMT_VINFO_DR_STEP (stmt_vinfo); | |
3680 | STMT_VINFO_DR_ALIGNED_TO (pattern_stmt_info) | |
3681 | = STMT_VINFO_DR_ALIGNED_TO (stmt_vinfo); | |
3682 | DR_STMT (STMT_VINFO_DATA_REF (stmt_vinfo)) = pattern_stmt; | |
3683 | ||
3684 | *type_out = vectype1; | |
3685 | *type_in = vectype1; | |
3686 | stmts->safe_push (last_stmt); | |
3687 | if (dump_enabled_p ()) | |
3688 | dump_printf_loc (MSG_NOTE, vect_location, | |
3689 | "vect_recog_mask_conversion_pattern: detected:\n"); | |
3690 | ||
3691 | return pattern_stmt; | |
3692 | } | |
3693 | ||
3694 | if (!is_gimple_assign (last_stmt)) | |
3695 | return NULL; | |
3696 | ||
3697 | lhs = gimple_assign_lhs (last_stmt); | |
3698 | rhs1 = gimple_assign_rhs1 (last_stmt); | |
3699 | rhs_code = gimple_assign_rhs_code (last_stmt); | |
3700 | ||
3701 | /* Check for cond expression requiring mask conversion. */ | |
3702 | if (rhs_code == COND_EXPR) | |
3703 | { | |
3704 | /* vect_recog_mixed_size_cond_pattern could apply. | |
3705 | Do nothing then. */ | |
3706 | if (STMT_VINFO_IN_PATTERN_P (stmt_vinfo)) | |
3707 | return NULL; | |
3708 | ||
3709 | vectype1 = get_vectype_for_scalar_type (TREE_TYPE (lhs)); | |
3710 | ||
3711 | if (TREE_CODE (rhs1) == SSA_NAME) | |
3712 | { | |
3713 | rhs1_type = search_type_for_mask (rhs1, vinfo); | |
3714 | if (!rhs1_type) | |
3715 | return NULL; | |
3716 | } | |
dea60b59 | 3717 | else if (COMPARISON_CLASS_P (rhs1)) |
e6f5c25d | 3718 | rhs1_type = TREE_TYPE (TREE_OPERAND (rhs1, 0)); |
dea60b59 JJ |
3719 | else |
3720 | return NULL; | |
e6f5c25d IE |
3721 | |
3722 | vectype2 = get_mask_type_for_scalar_type (rhs1_type); | |
3723 | ||
3724 | if (!vectype1 || !vectype2 | |
3725 | || TYPE_VECTOR_SUBPARTS (vectype1) == TYPE_VECTOR_SUBPARTS (vectype2)) | |
3726 | return NULL; | |
3727 | ||
3728 | /* If rhs1 is a comparison we need to move it into a | |
3729 | separate statement. */ | |
3730 | if (TREE_CODE (rhs1) != SSA_NAME) | |
3731 | { | |
3732 | tmp = vect_recog_temp_ssa_var (TREE_TYPE (rhs1), NULL); | |
3733 | pattern_stmt = gimple_build_assign (tmp, rhs1); | |
3734 | rhs1 = tmp; | |
3735 | ||
3736 | pattern_stmt_info = new_stmt_vec_info (pattern_stmt, vinfo); | |
3737 | set_vinfo_for_stmt (pattern_stmt, pattern_stmt_info); | |
3738 | STMT_VINFO_VECTYPE (pattern_stmt_info) = vectype2; | |
3739 | append_pattern_def_seq (stmt_vinfo, pattern_stmt); | |
3740 | } | |
3741 | ||
3742 | tmp = build_mask_conversion (rhs1, vectype1, stmt_vinfo, vinfo); | |
3743 | ||
3744 | lhs = vect_recog_temp_ssa_var (TREE_TYPE (lhs), NULL); | |
3745 | pattern_stmt = gimple_build_assign (lhs, COND_EXPR, tmp, | |
3746 | gimple_assign_rhs2 (last_stmt), | |
3747 | gimple_assign_rhs3 (last_stmt)); | |
3748 | ||
3749 | *type_out = vectype1; | |
3750 | *type_in = vectype1; | |
3751 | stmts->safe_push (last_stmt); | |
3752 | if (dump_enabled_p ()) | |
3753 | dump_printf_loc (MSG_NOTE, vect_location, | |
3754 | "vect_recog_mask_conversion_pattern: detected:\n"); | |
3755 | ||
3756 | return pattern_stmt; | |
3757 | } | |
3758 | ||
3759 | /* Now check for binary boolean operations requiring conversion for | |
3760 | one of operands. */ | |
3761 | if (TREE_CODE (TREE_TYPE (lhs)) != BOOLEAN_TYPE) | |
3762 | return NULL; | |
3763 | ||
3764 | if (rhs_code != BIT_IOR_EXPR | |
3765 | && rhs_code != BIT_XOR_EXPR | |
49e76ff1 IE |
3766 | && rhs_code != BIT_AND_EXPR |
3767 | && TREE_CODE_CLASS (rhs_code) != tcc_comparison) | |
e6f5c25d IE |
3768 | return NULL; |
3769 | ||
3770 | rhs2 = gimple_assign_rhs2 (last_stmt); | |
3771 | ||
3772 | rhs1_type = search_type_for_mask (rhs1, vinfo); | |
3773 | rhs2_type = search_type_for_mask (rhs2, vinfo); | |
3774 | ||
3775 | if (!rhs1_type || !rhs2_type | |
3776 | || TYPE_PRECISION (rhs1_type) == TYPE_PRECISION (rhs2_type)) | |
3777 | return NULL; | |
3778 | ||
3779 | if (TYPE_PRECISION (rhs1_type) < TYPE_PRECISION (rhs2_type)) | |
3780 | { | |
3781 | vectype1 = get_mask_type_for_scalar_type (rhs1_type); | |
3782 | if (!vectype1) | |
3783 | return NULL; | |
3784 | rhs2 = build_mask_conversion (rhs2, vectype1, stmt_vinfo, vinfo); | |
3785 | } | |
3786 | else | |
3787 | { | |
3788 | vectype1 = get_mask_type_for_scalar_type (rhs2_type); | |
3789 | if (!vectype1) | |
3790 | return NULL; | |
3791 | rhs1 = build_mask_conversion (rhs1, vectype1, stmt_vinfo, vinfo); | |
3792 | } | |
3793 | ||
3794 | lhs = vect_recog_temp_ssa_var (TREE_TYPE (lhs), NULL); | |
3795 | pattern_stmt = gimple_build_assign (lhs, rhs_code, rhs1, rhs2); | |
3796 | ||
3797 | *type_out = vectype1; | |
3798 | *type_in = vectype1; | |
3799 | stmts->safe_push (last_stmt); | |
3800 | if (dump_enabled_p ()) | |
3801 | dump_printf_loc (MSG_NOTE, vect_location, | |
3802 | "vect_recog_mask_conversion_pattern: detected:\n"); | |
3803 | ||
3804 | return pattern_stmt; | |
3805 | } | |
3806 | ||
3807 | ||
1107f3ae IR |
3808 | /* Mark statements that are involved in a pattern. */ |
3809 | ||
3810 | static inline void | |
355fe088 | 3811 | vect_mark_pattern_stmts (gimple *orig_stmt, gimple *pattern_stmt, |
1107f3ae IR |
3812 | tree pattern_vectype) |
3813 | { | |
3814 | stmt_vec_info pattern_stmt_info, def_stmt_info; | |
3815 | stmt_vec_info orig_stmt_info = vinfo_for_stmt (orig_stmt); | |
310213d4 | 3816 | vec_info *vinfo = orig_stmt_info->vinfo; |
355fe088 | 3817 | gimple *def_stmt; |
1107f3ae | 3818 | |
1107f3ae | 3819 | pattern_stmt_info = vinfo_for_stmt (pattern_stmt); |
ab0ef706 JJ |
3820 | if (pattern_stmt_info == NULL) |
3821 | { | |
310213d4 | 3822 | pattern_stmt_info = new_stmt_vec_info (pattern_stmt, vinfo); |
ab0ef706 JJ |
3823 | set_vinfo_for_stmt (pattern_stmt, pattern_stmt_info); |
3824 | } | |
3825 | gimple_set_bb (pattern_stmt, gimple_bb (orig_stmt)); | |
1107f3ae IR |
3826 | |
3827 | STMT_VINFO_RELATED_STMT (pattern_stmt_info) = orig_stmt; | |
3828 | STMT_VINFO_DEF_TYPE (pattern_stmt_info) | |
ab0ef706 | 3829 | = STMT_VINFO_DEF_TYPE (orig_stmt_info); |
1107f3ae IR |
3830 | STMT_VINFO_VECTYPE (pattern_stmt_info) = pattern_vectype; |
3831 | STMT_VINFO_IN_PATTERN_P (orig_stmt_info) = true; | |
3832 | STMT_VINFO_RELATED_STMT (orig_stmt_info) = pattern_stmt; | |
363477c0 JJ |
3833 | STMT_VINFO_PATTERN_DEF_SEQ (pattern_stmt_info) |
3834 | = STMT_VINFO_PATTERN_DEF_SEQ (orig_stmt_info); | |
3835 | if (STMT_VINFO_PATTERN_DEF_SEQ (pattern_stmt_info)) | |
1107f3ae | 3836 | { |
363477c0 JJ |
3837 | gimple_stmt_iterator si; |
3838 | for (si = gsi_start (STMT_VINFO_PATTERN_DEF_SEQ (pattern_stmt_info)); | |
3839 | !gsi_end_p (si); gsi_next (&si)) | |
69d2aade | 3840 | { |
363477c0 JJ |
3841 | def_stmt = gsi_stmt (si); |
3842 | def_stmt_info = vinfo_for_stmt (def_stmt); | |
3843 | if (def_stmt_info == NULL) | |
3844 | { | |
310213d4 | 3845 | def_stmt_info = new_stmt_vec_info (def_stmt, vinfo); |
363477c0 JJ |
3846 | set_vinfo_for_stmt (def_stmt, def_stmt_info); |
3847 | } | |
3848 | gimple_set_bb (def_stmt, gimple_bb (orig_stmt)); | |
3849 | STMT_VINFO_RELATED_STMT (def_stmt_info) = orig_stmt; | |
56f8faae | 3850 | STMT_VINFO_DEF_TYPE (def_stmt_info) = vect_internal_def; |
363477c0 JJ |
3851 | if (STMT_VINFO_VECTYPE (def_stmt_info) == NULL_TREE) |
3852 | STMT_VINFO_VECTYPE (def_stmt_info) = pattern_vectype; | |
69d2aade | 3853 | } |
1107f3ae IR |
3854 | } |
3855 | } | |
3856 | ||
b8698a0f | 3857 | /* Function vect_pattern_recog_1 |
20f06221 DN |
3858 | |
3859 | Input: | |
3860 | PATTERN_RECOG_FUNC: A pointer to a function that detects a certain | |
3861 | computation pattern. | |
3862 | STMT: A stmt from which the pattern search should start. | |
3863 | ||
3864 | If PATTERN_RECOG_FUNC successfully detected the pattern, it creates an | |
b8698a0f L |
3865 | expression that computes the same functionality and can be used to |
3866 | replace the sequence of stmts that are involved in the pattern. | |
20f06221 DN |
3867 | |
3868 | Output: | |
b8698a0f L |
3869 | This function checks if the expression returned by PATTERN_RECOG_FUNC is |
3870 | supported in vector form by the target. We use 'TYPE_IN' to obtain the | |
3871 | relevant vector type. If 'TYPE_IN' is already a vector type, then this | |
20f06221 DN |
3872 | indicates that target support had already been checked by PATTERN_RECOG_FUNC. |
3873 | If 'TYPE_OUT' is also returned by PATTERN_RECOG_FUNC, we check that it fits | |
3874 | to the available target pattern. | |
3875 | ||
b8698a0f | 3876 | This function also does some bookkeeping, as explained in the documentation |
20f06221 DN |
3877 | for vect_recog_pattern. */ |
3878 | ||
0bee0ef9 RB |
3879 | static bool |
3880 | vect_pattern_recog_1 (vect_recog_func *recog_func, | |
92aea285 | 3881 | gimple_stmt_iterator si, |
355fe088 | 3882 | vec<gimple *> *stmts_to_replace) |
20f06221 | 3883 | { |
355fe088 | 3884 | gimple *stmt = gsi_stmt (si), *pattern_stmt; |
383d9c83 | 3885 | stmt_vec_info stmt_info; |
383d9c83 | 3886 | loop_vec_info loop_vinfo; |
20f06221 DN |
3887 | tree pattern_vectype; |
3888 | tree type_in, type_out; | |
20f06221 | 3889 | enum tree_code code; |
b5aeb3bb | 3890 | int i; |
355fe088 | 3891 | gimple *next; |
20f06221 | 3892 | |
9771b263 DN |
3893 | stmts_to_replace->truncate (0); |
3894 | stmts_to_replace->quick_push (stmt); | |
0bee0ef9 | 3895 | pattern_stmt = recog_func->fn (stmts_to_replace, &type_in, &type_out); |
726a989a | 3896 | if (!pattern_stmt) |
0bee0ef9 | 3897 | return false; |
b8698a0f | 3898 | |
9771b263 | 3899 | stmt = stmts_to_replace->last (); |
383d9c83 IR |
3900 | stmt_info = vinfo_for_stmt (stmt); |
3901 | loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info); | |
3902 | ||
e6f5c25d IE |
3903 | if (VECTOR_BOOLEAN_TYPE_P (type_in) |
3904 | || VECTOR_MODE_P (TYPE_MODE (type_in))) | |
b8698a0f L |
3905 | { |
3906 | /* No need to check target support (already checked by the pattern | |
3907 | recognition function). */ | |
b690cc0f | 3908 | pattern_vectype = type_out ? type_out : type_in; |
20f06221 DN |
3909 | } |
3910 | else | |
3911 | { | |
ef4bddc2 | 3912 | machine_mode vec_mode; |
20f06221 DN |
3913 | enum insn_code icode; |
3914 | optab optab; | |
3915 | ||
3916 | /* Check target support */ | |
b690cc0f RG |
3917 | type_in = get_vectype_for_scalar_type (type_in); |
3918 | if (!type_in) | |
0bee0ef9 | 3919 | return false; |
b690cc0f RG |
3920 | if (type_out) |
3921 | type_out = get_vectype_for_scalar_type (type_out); | |
3922 | else | |
3923 | type_out = type_in; | |
15bbc165 | 3924 | if (!type_out) |
0bee0ef9 | 3925 | return false; |
b690cc0f | 3926 | pattern_vectype = type_out; |
03d3e953 | 3927 | |
726a989a RB |
3928 | if (is_gimple_assign (pattern_stmt)) |
3929 | code = gimple_assign_rhs_code (pattern_stmt); | |
3930 | else | |
3931 | { | |
3932 | gcc_assert (is_gimple_call (pattern_stmt)); | |
3933 | code = CALL_EXPR; | |
3934 | } | |
3935 | ||
b690cc0f RG |
3936 | optab = optab_for_tree_code (code, type_in, optab_default); |
3937 | vec_mode = TYPE_MODE (type_in); | |
20f06221 | 3938 | if (!optab |
947131ba | 3939 | || (icode = optab_handler (optab, vec_mode)) == CODE_FOR_nothing |
b690cc0f | 3940 | || (insn_data[icode].operand[0].mode != TYPE_MODE (type_out))) |
0bee0ef9 | 3941 | return false; |
20f06221 DN |
3942 | } |
3943 | ||
3944 | /* Found a vectorizable pattern. */ | |
73fbfcad | 3945 | if (dump_enabled_p ()) |
20f06221 | 3946 | { |
ccb3ad87 | 3947 | dump_printf_loc (MSG_NOTE, vect_location, |
0bee0ef9 | 3948 | "%s pattern recognized: ", recog_func->name); |
ccb3ad87 | 3949 | dump_gimple_stmt (MSG_NOTE, TDF_SLIM, pattern_stmt, 0); |
20f06221 | 3950 | } |
b8698a0f | 3951 | |
726a989a | 3952 | /* Mark the stmts that are involved in the pattern. */ |
1107f3ae | 3953 | vect_mark_pattern_stmts (stmt, pattern_stmt, pattern_vectype); |
20f06221 | 3954 | |
b5aeb3bb IR |
3955 | /* Patterns cannot be vectorized using SLP, because they change the order of |
3956 | computation. */ | |
f5709183 | 3957 | if (loop_vinfo) |
9771b263 | 3958 | FOR_EACH_VEC_ELT (LOOP_VINFO_REDUCTIONS (loop_vinfo), i, next) |
f5709183 | 3959 | if (next == stmt) |
9771b263 | 3960 | LOOP_VINFO_REDUCTIONS (loop_vinfo).ordered_remove (i); |
51312233 | 3961 | |
1107f3ae IR |
3962 | /* It is possible that additional pattern stmts are created and inserted in |
3963 | STMTS_TO_REPLACE. We create a stmt_info for each of them, and mark the | |
3964 | relevant statements. */ | |
9771b263 DN |
3965 | for (i = 0; stmts_to_replace->iterate (i, &stmt) |
3966 | && (unsigned) i < (stmts_to_replace->length () - 1); | |
51312233 IR |
3967 | i++) |
3968 | { | |
3969 | stmt_info = vinfo_for_stmt (stmt); | |
3970 | pattern_stmt = STMT_VINFO_RELATED_STMT (stmt_info); | |
73fbfcad | 3971 | if (dump_enabled_p ()) |
51312233 | 3972 | { |
ccb3ad87 | 3973 | dump_printf_loc (MSG_NOTE, vect_location, |
78c60e3d | 3974 | "additional pattern stmt: "); |
ccb3ad87 | 3975 | dump_gimple_stmt (MSG_NOTE, TDF_SLIM, pattern_stmt, 0); |
51312233 IR |
3976 | } |
3977 | ||
1107f3ae | 3978 | vect_mark_pattern_stmts (stmt, pattern_stmt, NULL_TREE); |
51312233 | 3979 | } |
0bee0ef9 RB |
3980 | |
3981 | return true; | |
20f06221 DN |
3982 | } |
3983 | ||
3984 | ||
3985 | /* Function vect_pattern_recog | |
3986 | ||
3987 | Input: | |
3988 | LOOP_VINFO - a struct_loop_info of a loop in which we want to look for | |
3989 | computation idioms. | |
3990 | ||
9d5e7640 IR |
3991 | Output - for each computation idiom that is detected we create a new stmt |
3992 | that provides the same functionality and that can be vectorized. We | |
20f06221 DN |
3993 | also record some information in the struct_stmt_info of the relevant |
3994 | stmts, as explained below: | |
3995 | ||
3996 | At the entry to this function we have the following stmts, with the | |
3997 | following initial value in the STMT_VINFO fields: | |
3998 | ||
3999 | stmt in_pattern_p related_stmt vec_stmt | |
4000 | S1: a_i = .... - - - | |
4001 | S2: a_2 = ..use(a_i).. - - - | |
4002 | S3: a_1 = ..use(a_2).. - - - | |
4003 | S4: a_0 = ..use(a_1).. - - - | |
4004 | S5: ... = ..use(a_0).. - - - | |
4005 | ||
4006 | Say the sequence {S1,S2,S3,S4} was detected as a pattern that can be | |
9d5e7640 IR |
4007 | represented by a single stmt. We then: |
4008 | - create a new stmt S6 equivalent to the pattern (the stmt is not | |
4009 | inserted into the code) | |
20f06221 DN |
4010 | - fill in the STMT_VINFO fields as follows: |
4011 | ||
4012 | in_pattern_p related_stmt vec_stmt | |
b8698a0f | 4013 | S1: a_i = .... - - - |
20f06221 DN |
4014 | S2: a_2 = ..use(a_i).. - - - |
4015 | S3: a_1 = ..use(a_2).. - - - | |
20f06221 | 4016 | S4: a_0 = ..use(a_1).. true S6 - |
9d5e7640 | 4017 | '---> S6: a_new = .... - S4 - |
20f06221 DN |
4018 | S5: ... = ..use(a_0).. - - - |
4019 | ||
4020 | (the last stmt in the pattern (S4) and the new pattern stmt (S6) point | |
9d5e7640 | 4021 | to each other through the RELATED_STMT field). |
20f06221 DN |
4022 | |
4023 | S6 will be marked as relevant in vect_mark_stmts_to_be_vectorized instead | |
4024 | of S4 because it will replace all its uses. Stmts {S1,S2,S3} will | |
4025 | remain irrelevant unless used by stmts other than S4. | |
4026 | ||
4027 | If vectorization succeeds, vect_transform_stmt will skip over {S1,S2,S3} | |
9d5e7640 | 4028 | (because they are marked as irrelevant). It will vectorize S6, and record |
83197f37 IR |
4029 | a pointer to the new vector stmt VS6 from S6 (as usual). |
4030 | S4 will be skipped, and S5 will be vectorized as usual: | |
20f06221 DN |
4031 | |
4032 | in_pattern_p related_stmt vec_stmt | |
4033 | S1: a_i = .... - - - | |
4034 | S2: a_2 = ..use(a_i).. - - - | |
4035 | S3: a_1 = ..use(a_2).. - - - | |
4036 | > VS6: va_new = .... - - - | |
20f06221 | 4037 | S4: a_0 = ..use(a_1).. true S6 VS6 |
9d5e7640 | 4038 | '---> S6: a_new = .... - S4 VS6 |
20f06221 DN |
4039 | > VS5: ... = ..vuse(va_new).. - - - |
4040 | S5: ... = ..use(a_0).. - - - | |
4041 | ||
9d5e7640 | 4042 | DCE could then get rid of {S1,S2,S3,S4,S5} (if their defs are not used |
20f06221 DN |
4043 | elsewhere), and we'll end up with: |
4044 | ||
b8698a0f | 4045 | VS6: va_new = .... |
83197f37 IR |
4046 | VS5: ... = ..vuse(va_new).. |
4047 | ||
4048 | In case of more than one pattern statements, e.g., widen-mult with | |
4049 | intermediate type: | |
4050 | ||
4051 | S1 a_t = ; | |
4052 | S2 a_T = (TYPE) a_t; | |
4053 | '--> S3: a_it = (interm_type) a_t; | |
4054 | S4 prod_T = a_T * CONST; | |
4055 | '--> S5: prod_T' = a_it w* CONST; | |
4056 | ||
4057 | there may be other users of a_T outside the pattern. In that case S2 will | |
4058 | be marked as relevant (as well as S3), and both S2 and S3 will be analyzed | |
4059 | and vectorized. The vector stmt VS2 will be recorded in S2, and VS3 will | |
4060 | be recorded in S3. */ | |
20f06221 DN |
4061 | |
4062 | void | |
310213d4 | 4063 | vect_pattern_recog (vec_info *vinfo) |
20f06221 | 4064 | { |
f5709183 | 4065 | struct loop *loop; |
772e61e1 | 4066 | basic_block *bbs; |
f5709183 | 4067 | unsigned int nbbs; |
726a989a | 4068 | gimple_stmt_iterator si; |
20f06221 | 4069 | unsigned int i, j; |
355fe088 TS |
4070 | auto_vec<gimple *, 1> stmts_to_replace; |
4071 | gimple *stmt; | |
20f06221 | 4072 | |
73fbfcad | 4073 | if (dump_enabled_p ()) |
78c60e3d | 4074 | dump_printf_loc (MSG_NOTE, vect_location, |
e645e942 | 4075 | "=== vect_pattern_recog ===\n"); |
20f06221 | 4076 | |
310213d4 | 4077 | if (loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo)) |
f5709183 IR |
4078 | { |
4079 | loop = LOOP_VINFO_LOOP (loop_vinfo); | |
4080 | bbs = LOOP_VINFO_BBS (loop_vinfo); | |
4081 | nbbs = loop->num_nodes; | |
61d371eb RB |
4082 | |
4083 | /* Scan through the loop stmts, applying the pattern recognition | |
4084 | functions starting at each stmt visited: */ | |
4085 | for (i = 0; i < nbbs; i++) | |
4086 | { | |
4087 | basic_block bb = bbs[i]; | |
4088 | for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si)) | |
4089 | { | |
4090 | /* Scan over all generic vect_recog_xxx_pattern functions. */ | |
4091 | for (j = 0; j < NUM_PATTERNS; j++) | |
0bee0ef9 RB |
4092 | if (vect_pattern_recog_1 (&vect_vect_recog_func_ptrs[j], si, |
4093 | &stmts_to_replace)) | |
4094 | break; | |
61d371eb RB |
4095 | } |
4096 | } | |
f5709183 IR |
4097 | } |
4098 | else | |
4099 | { | |
61d371eb RB |
4100 | bb_vec_info bb_vinfo = as_a <bb_vec_info> (vinfo); |
4101 | for (si = bb_vinfo->region_begin; | |
4102 | gsi_stmt (si) != gsi_stmt (bb_vinfo->region_end); gsi_next (&si)) | |
4103 | { | |
4104 | if ((stmt = gsi_stmt (si)) | |
f5709183 IR |
4105 | && vinfo_for_stmt (stmt) |
4106 | && !STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (stmt))) | |
61d371eb | 4107 | continue; |
f5709183 | 4108 | |
61d371eb RB |
4109 | /* Scan over all generic vect_recog_xxx_pattern functions. */ |
4110 | for (j = 0; j < NUM_PATTERNS; j++) | |
0bee0ef9 RB |
4111 | if (vect_pattern_recog_1 (&vect_vect_recog_func_ptrs[j], si, |
4112 | &stmts_to_replace)) | |
4113 | break; | |
61d371eb | 4114 | } |
20f06221 DN |
4115 | } |
4116 | } |