]>
Commit | Line | Data |
---|---|---|
ebfd146a | 1 | /* SLP - Basic Block Vectorization |
cbe34bb5 | 2 | Copyright (C) 2007-2017 Free Software Foundation, Inc. |
b8698a0f | 3 | Contributed by Dorit Naishlos <dorit@il.ibm.com> |
ebfd146a IR |
4 | and Ira Rosen <irar@il.ibm.com> |
5 | ||
6 | This file is part of GCC. | |
7 | ||
8 | GCC is free software; you can redistribute it and/or modify it under | |
9 | the terms of the GNU General Public License as published by the Free | |
10 | Software Foundation; either version 3, or (at your option) any later | |
11 | version. | |
12 | ||
13 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY | |
14 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 | for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with GCC; see the file COPYING3. If not see | |
20 | <http://www.gnu.org/licenses/>. */ | |
21 | ||
22 | #include "config.h" | |
23 | #include "system.h" | |
24 | #include "coretypes.h" | |
c7131fb2 | 25 | #include "backend.h" |
957060b5 AM |
26 | #include "target.h" |
27 | #include "rtl.h" | |
ebfd146a | 28 | #include "tree.h" |
c7131fb2 | 29 | #include "gimple.h" |
957060b5 | 30 | #include "tree-pass.h" |
c7131fb2 | 31 | #include "ssa.h" |
957060b5 AM |
32 | #include "optabs-tree.h" |
33 | #include "insn-config.h" | |
34 | #include "recog.h" /* FIXME: for insn_data */ | |
957060b5 | 35 | #include "params.h" |
40e23961 | 36 | #include "fold-const.h" |
d8a2d370 | 37 | #include "stor-layout.h" |
5be5c238 | 38 | #include "gimple-iterator.h" |
ebfd146a | 39 | #include "cfgloop.h" |
ebfd146a | 40 | #include "tree-vectorizer.h" |
2635892a | 41 | #include "langhooks.h" |
642fce57 | 42 | #include "gimple-walk.h" |
428db0ba | 43 | #include "dbgcnt.h" |
a70d6342 IR |
44 | |
45 | ||
ebfd146a IR |
46 | /* Recursively free the memory allocated for the SLP tree rooted at NODE. */ |
47 | ||
48 | static void | |
49 | vect_free_slp_tree (slp_tree node) | |
50 | { | |
d092494c | 51 | int i; |
d755c7ef | 52 | slp_tree child; |
d092494c | 53 | |
9771b263 | 54 | FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child) |
d755c7ef | 55 | vect_free_slp_tree (child); |
b8698a0f | 56 | |
78810bd3 RB |
57 | gimple *stmt; |
58 | FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt) | |
59 | /* After transform some stmts are removed and thus their vinfo is gone. */ | |
60 | if (vinfo_for_stmt (stmt)) | |
61 | { | |
62 | gcc_assert (STMT_VINFO_NUM_SLP_USES (vinfo_for_stmt (stmt)) > 0); | |
63 | STMT_VINFO_NUM_SLP_USES (vinfo_for_stmt (stmt))--; | |
64 | } | |
65 | ||
9771b263 DN |
66 | SLP_TREE_CHILDREN (node).release (); |
67 | SLP_TREE_SCALAR_STMTS (node).release (); | |
68 | SLP_TREE_VEC_STMTS (node).release (); | |
01d8bf07 | 69 | SLP_TREE_LOAD_PERMUTATION (node).release (); |
ebfd146a IR |
70 | |
71 | free (node); | |
72 | } | |
73 | ||
74 | ||
75 | /* Free the memory allocated for the SLP instance. */ | |
76 | ||
77 | void | |
78 | vect_free_slp_instance (slp_instance instance) | |
79 | { | |
80 | vect_free_slp_tree (SLP_INSTANCE_TREE (instance)); | |
9771b263 | 81 | SLP_INSTANCE_LOADS (instance).release (); |
c7e62a26 | 82 | free (instance); |
ebfd146a IR |
83 | } |
84 | ||
85 | ||
d092494c IR |
86 | /* Create an SLP node for SCALAR_STMTS. */ |
87 | ||
88 | static slp_tree | |
355fe088 | 89 | vect_create_new_slp_node (vec<gimple *> scalar_stmts) |
d092494c | 90 | { |
d3cfd39e | 91 | slp_tree node; |
355fe088 | 92 | gimple *stmt = scalar_stmts[0]; |
d092494c IR |
93 | unsigned int nops; |
94 | ||
95 | if (is_gimple_call (stmt)) | |
96 | nops = gimple_call_num_args (stmt); | |
97 | else if (is_gimple_assign (stmt)) | |
f7e531cf IR |
98 | { |
99 | nops = gimple_num_ops (stmt) - 1; | |
100 | if (gimple_assign_rhs_code (stmt) == COND_EXPR) | |
101 | nops++; | |
102 | } | |
d092494c IR |
103 | else |
104 | return NULL; | |
105 | ||
d3cfd39e | 106 | node = XNEW (struct _slp_tree); |
d092494c | 107 | SLP_TREE_SCALAR_STMTS (node) = scalar_stmts; |
9771b263 DN |
108 | SLP_TREE_VEC_STMTS (node).create (0); |
109 | SLP_TREE_CHILDREN (node).create (nops); | |
01d8bf07 | 110 | SLP_TREE_LOAD_PERMUTATION (node) = vNULL; |
6876e5bc | 111 | SLP_TREE_TWO_OPERATORS (node) = false; |
603cca93 | 112 | SLP_TREE_DEF_TYPE (node) = vect_internal_def; |
d092494c | 113 | |
78810bd3 RB |
114 | unsigned i; |
115 | FOR_EACH_VEC_ELT (scalar_stmts, i, stmt) | |
116 | STMT_VINFO_NUM_SLP_USES (vinfo_for_stmt (stmt))++; | |
117 | ||
d092494c IR |
118 | return node; |
119 | } | |
120 | ||
121 | ||
ddf56386 RB |
122 | /* This structure is used in creation of an SLP tree. Each instance |
123 | corresponds to the same operand in a group of scalar stmts in an SLP | |
124 | node. */ | |
125 | typedef struct _slp_oprnd_info | |
126 | { | |
127 | /* Def-stmts for the operands. */ | |
128 | vec<gimple *> def_stmts; | |
129 | /* Information about the first statement, its vector def-type, type, the | |
130 | operand itself in case it's constant, and an indication if it's a pattern | |
131 | stmt. */ | |
132 | enum vect_def_type first_dt; | |
133 | tree first_op_type; | |
134 | bool first_pattern; | |
135 | bool second_pattern; | |
136 | } *slp_oprnd_info; | |
137 | ||
138 | ||
d092494c IR |
139 | /* Allocate operands info for NOPS operands, and GROUP_SIZE def-stmts for each |
140 | operand. */ | |
9771b263 | 141 | static vec<slp_oprnd_info> |
d092494c IR |
142 | vect_create_oprnd_info (int nops, int group_size) |
143 | { | |
144 | int i; | |
145 | slp_oprnd_info oprnd_info; | |
9771b263 | 146 | vec<slp_oprnd_info> oprnds_info; |
d092494c | 147 | |
9771b263 | 148 | oprnds_info.create (nops); |
d092494c IR |
149 | for (i = 0; i < nops; i++) |
150 | { | |
151 | oprnd_info = XNEW (struct _slp_oprnd_info); | |
9771b263 | 152 | oprnd_info->def_stmts.create (group_size); |
d092494c | 153 | oprnd_info->first_dt = vect_uninitialized_def; |
793d9a16 | 154 | oprnd_info->first_op_type = NULL_TREE; |
d092494c | 155 | oprnd_info->first_pattern = false; |
effb52da | 156 | oprnd_info->second_pattern = false; |
9771b263 | 157 | oprnds_info.quick_push (oprnd_info); |
d092494c IR |
158 | } |
159 | ||
160 | return oprnds_info; | |
161 | } | |
162 | ||
163 | ||
d3cfd39e JJ |
164 | /* Free operands info. */ |
165 | ||
d092494c | 166 | static void |
9771b263 | 167 | vect_free_oprnd_info (vec<slp_oprnd_info> &oprnds_info) |
d092494c IR |
168 | { |
169 | int i; | |
170 | slp_oprnd_info oprnd_info; | |
171 | ||
9771b263 | 172 | FOR_EACH_VEC_ELT (oprnds_info, i, oprnd_info) |
d3cfd39e | 173 | { |
9771b263 | 174 | oprnd_info->def_stmts.release (); |
d3cfd39e JJ |
175 | XDELETE (oprnd_info); |
176 | } | |
d092494c | 177 | |
9771b263 | 178 | oprnds_info.release (); |
d092494c IR |
179 | } |
180 | ||
181 | ||
d755c7ef RB |
182 | /* Find the place of the data-ref in STMT in the interleaving chain that starts |
183 | from FIRST_STMT. Return -1 if the data-ref is not a part of the chain. */ | |
184 | ||
185 | static int | |
355fe088 | 186 | vect_get_place_in_interleaving_chain (gimple *stmt, gimple *first_stmt) |
d755c7ef | 187 | { |
355fe088 | 188 | gimple *next_stmt = first_stmt; |
d755c7ef RB |
189 | int result = 0; |
190 | ||
191 | if (first_stmt != GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt))) | |
192 | return -1; | |
193 | ||
194 | do | |
195 | { | |
196 | if (next_stmt == stmt) | |
197 | return result; | |
d755c7ef | 198 | next_stmt = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next_stmt)); |
c8047699 RB |
199 | if (next_stmt) |
200 | result += GROUP_GAP (vinfo_for_stmt (next_stmt)); | |
d755c7ef RB |
201 | } |
202 | while (next_stmt); | |
203 | ||
204 | return -1; | |
205 | } | |
206 | ||
207 | ||
d092494c IR |
208 | /* Get the defs for the rhs of STMT (collect them in OPRNDS_INFO), check that |
209 | they are of a valid type and that they match the defs of the first stmt of | |
4cecd659 BC |
210 | the SLP group (stored in OPRNDS_INFO). This function tries to match stmts |
211 | by swapping operands of STMT when possible. Non-zero *SWAP indicates swap | |
212 | is required for cond_expr stmts. Specifically, *SWAP is 1 if STMT is cond | |
213 | and operands of comparison need to be swapped; *SWAP is 2 if STMT is cond | |
214 | and code of comparison needs to be inverted. If there is any operand swap | |
215 | in this function, *SWAP is set to non-zero value. | |
216 | If there was a fatal error return -1; if the error could be corrected by | |
217 | swapping operands of father node of this one, return 1; if everything is | |
218 | ok return 0. */ | |
ebfd146a | 219 | |
4cecd659 BC |
220 | static int |
221 | vect_get_and_check_slp_defs (vec_info *vinfo, unsigned char *swap, | |
355fe088 | 222 | gimple *stmt, unsigned stmt_num, |
4cecd659 | 223 | vec<slp_oprnd_info> *oprnds_info) |
ebfd146a IR |
224 | { |
225 | tree oprnd; | |
226 | unsigned int i, number_of_oprnds; | |
355fe088 | 227 | gimple *def_stmt; |
d092494c | 228 | enum vect_def_type dt = vect_uninitialized_def; |
d092494c | 229 | bool pattern = false; |
abf9bfbc | 230 | slp_oprnd_info oprnd_info; |
b0b4483e RB |
231 | int first_op_idx = 1; |
232 | bool commutative = false; | |
233 | bool first_op_cond = false; | |
effb52da RB |
234 | bool first = stmt_num == 0; |
235 | bool second = stmt_num == 1; | |
b8698a0f | 236 | |
d092494c | 237 | if (is_gimple_call (stmt)) |
190c2236 JJ |
238 | { |
239 | number_of_oprnds = gimple_call_num_args (stmt); | |
b0b4483e | 240 | first_op_idx = 3; |
190c2236 | 241 | } |
f7e531cf IR |
242 | else if (is_gimple_assign (stmt)) |
243 | { | |
b0b4483e | 244 | enum tree_code code = gimple_assign_rhs_code (stmt); |
f7e531cf | 245 | number_of_oprnds = gimple_num_ops (stmt) - 1; |
4cecd659 BC |
246 | /* Swap can only be done for cond_expr if asked to, otherwise we |
247 | could result in different comparison code to the first stmt. */ | |
a414c77f IE |
248 | if (gimple_assign_rhs_code (stmt) == COND_EXPR |
249 | && COMPARISON_CLASS_P (gimple_assign_rhs1 (stmt))) | |
b0b4483e RB |
250 | { |
251 | first_op_cond = true; | |
b0b4483e RB |
252 | number_of_oprnds++; |
253 | } | |
254 | else | |
255 | commutative = commutative_tree_code (code); | |
f7e531cf | 256 | } |
d092494c | 257 | else |
b0b4483e | 258 | return -1; |
ebfd146a | 259 | |
4cecd659 BC |
260 | bool swapped = (*swap != 0); |
261 | gcc_assert (!swapped || first_op_cond); | |
ebfd146a IR |
262 | for (i = 0; i < number_of_oprnds; i++) |
263 | { | |
b0b4483e RB |
264 | again: |
265 | if (first_op_cond) | |
f7e531cf | 266 | { |
4cecd659 BC |
267 | /* Map indicating how operands of cond_expr should be swapped. */ |
268 | int maps[3][4] = {{0, 1, 2, 3}, {1, 0, 2, 3}, {0, 1, 3, 2}}; | |
269 | int *map = maps[*swap]; | |
270 | ||
271 | if (i < 2) | |
272 | oprnd = TREE_OPERAND (gimple_op (stmt, first_op_idx), map[i]); | |
b0b4483e | 273 | else |
4cecd659 | 274 | oprnd = gimple_op (stmt, map[i]); |
f7e531cf IR |
275 | } |
276 | else | |
4cecd659 | 277 | oprnd = gimple_op (stmt, first_op_idx + (swapped ? !i : i)); |
f7e531cf | 278 | |
9771b263 | 279 | oprnd_info = (*oprnds_info)[i]; |
ebfd146a | 280 | |
81c40241 | 281 | if (!vect_is_simple_use (oprnd, vinfo, &def_stmt, &dt)) |
ebfd146a | 282 | { |
73fbfcad | 283 | if (dump_enabled_p ()) |
ebfd146a | 284 | { |
78c60e3d | 285 | dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location, |
3fc356dc | 286 | "Build SLP failed: can't analyze def for "); |
78c60e3d | 287 | dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, oprnd); |
e645e942 | 288 | dump_printf (MSG_MISSED_OPTIMIZATION, "\n"); |
ebfd146a IR |
289 | } |
290 | ||
b0b4483e | 291 | return -1; |
ebfd146a IR |
292 | } |
293 | ||
a70d6342 | 294 | /* Check if DEF_STMT is a part of a pattern in LOOP and get the def stmt |
ff802fa1 | 295 | from the pattern. Check that all the stmts of the node are in the |
ebfd146a | 296 | pattern. */ |
f5709183 | 297 | if (def_stmt && gimple_bb (def_stmt) |
61d371eb | 298 | && vect_stmt_in_region_p (vinfo, def_stmt) |
ebfd146a | 299 | && vinfo_for_stmt (def_stmt) |
83197f37 | 300 | && STMT_VINFO_IN_PATTERN_P (vinfo_for_stmt (def_stmt)) |
f5709183 IR |
301 | && !STMT_VINFO_RELEVANT (vinfo_for_stmt (def_stmt)) |
302 | && !STMT_VINFO_LIVE_P (vinfo_for_stmt (def_stmt))) | |
ebfd146a | 303 | { |
d092494c | 304 | pattern = true; |
effb52da RB |
305 | if (!first && !oprnd_info->first_pattern |
306 | /* Allow different pattern state for the defs of the | |
307 | first stmt in reduction chains. */ | |
308 | && (oprnd_info->first_dt != vect_reduction_def | |
309 | || (!second && !oprnd_info->second_pattern))) | |
d092494c | 310 | { |
b0b4483e RB |
311 | if (i == 0 |
312 | && !swapped | |
313 | && commutative) | |
314 | { | |
315 | swapped = true; | |
316 | goto again; | |
317 | } | |
318 | ||
73fbfcad | 319 | if (dump_enabled_p ()) |
d092494c | 320 | { |
78c60e3d SS |
321 | dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location, |
322 | "Build SLP failed: some of the stmts" | |
323 | " are in a pattern, and others are not "); | |
324 | dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, oprnd); | |
e645e942 | 325 | dump_printf (MSG_MISSED_OPTIMIZATION, "\n"); |
d092494c | 326 | } |
ebfd146a | 327 | |
b0b4483e | 328 | return 1; |
ebfd146a IR |
329 | } |
330 | ||
331 | def_stmt = STMT_VINFO_RELATED_STMT (vinfo_for_stmt (def_stmt)); | |
d092494c | 332 | dt = STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def_stmt)); |
ebfd146a | 333 | |
f7e531cf | 334 | if (dt == vect_unknown_def_type) |
ebfd146a | 335 | { |
73fbfcad | 336 | if (dump_enabled_p ()) |
78c60e3d | 337 | dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location, |
e645e942 | 338 | "Unsupported pattern.\n"); |
b0b4483e | 339 | return -1; |
ebfd146a IR |
340 | } |
341 | ||
342 | switch (gimple_code (def_stmt)) | |
343 | { | |
81c40241 RB |
344 | case GIMPLE_PHI: |
345 | case GIMPLE_ASSIGN: | |
346 | break; | |
347 | ||
348 | default: | |
349 | if (dump_enabled_p ()) | |
350 | dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location, | |
351 | "unsupported defining stmt:\n"); | |
352 | return -1; | |
ebfd146a IR |
353 | } |
354 | } | |
355 | ||
effb52da RB |
356 | if (second) |
357 | oprnd_info->second_pattern = pattern; | |
358 | ||
d092494c | 359 | if (first) |
ebfd146a | 360 | { |
d092494c IR |
361 | oprnd_info->first_dt = dt; |
362 | oprnd_info->first_pattern = pattern; | |
793d9a16 | 363 | oprnd_info->first_op_type = TREE_TYPE (oprnd); |
ebfd146a | 364 | } |
ebfd146a IR |
365 | else |
366 | { | |
d092494c IR |
367 | /* Not first stmt of the group, check that the def-stmt/s match |
368 | the def-stmt/s of the first stmt. Allow different definition | |
369 | types for reduction chains: the first stmt must be a | |
370 | vect_reduction_def (a phi node), and the rest | |
371 | vect_internal_def. */ | |
372 | if (((oprnd_info->first_dt != dt | |
373 | && !(oprnd_info->first_dt == vect_reduction_def | |
793d9a16 RB |
374 | && dt == vect_internal_def) |
375 | && !((oprnd_info->first_dt == vect_external_def | |
376 | || oprnd_info->first_dt == vect_constant_def) | |
377 | && (dt == vect_external_def | |
378 | || dt == vect_constant_def))) | |
379 | || !types_compatible_p (oprnd_info->first_op_type, | |
380 | TREE_TYPE (oprnd)))) | |
ebfd146a | 381 | { |
b0b4483e RB |
382 | /* Try swapping operands if we got a mismatch. */ |
383 | if (i == 0 | |
384 | && !swapped | |
385 | && commutative) | |
386 | { | |
387 | swapped = true; | |
388 | goto again; | |
389 | } | |
390 | ||
abf9bfbc RB |
391 | if (dump_enabled_p ()) |
392 | dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location, | |
e645e942 | 393 | "Build SLP failed: different types\n"); |
d092494c | 394 | |
b0b4483e | 395 | return 1; |
ebfd146a IR |
396 | } |
397 | } | |
398 | ||
399 | /* Check the types of the definitions. */ | |
d092494c | 400 | switch (dt) |
ebfd146a IR |
401 | { |
402 | case vect_constant_def: | |
8644a673 | 403 | case vect_external_def: |
d092494c | 404 | case vect_reduction_def: |
ebfd146a | 405 | break; |
b8698a0f | 406 | |
8644a673 | 407 | case vect_internal_def: |
abf9bfbc | 408 | oprnd_info->def_stmts.quick_push (def_stmt); |
ebfd146a IR |
409 | break; |
410 | ||
411 | default: | |
412 | /* FORNOW: Not supported. */ | |
73fbfcad | 413 | if (dump_enabled_p ()) |
ebfd146a | 414 | { |
78c60e3d SS |
415 | dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location, |
416 | "Build SLP failed: illegal type of def "); | |
81c40241 | 417 | dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, oprnd); |
e645e942 | 418 | dump_printf (MSG_MISSED_OPTIMIZATION, "\n"); |
ebfd146a IR |
419 | } |
420 | ||
b0b4483e | 421 | return -1; |
ebfd146a IR |
422 | } |
423 | } | |
424 | ||
b0b4483e RB |
425 | /* Swap operands. */ |
426 | if (swapped) | |
427 | { | |
78810bd3 RB |
428 | /* If there are already uses of this stmt in a SLP instance then |
429 | we've committed to the operand order and can't swap it. */ | |
430 | if (STMT_VINFO_NUM_SLP_USES (vinfo_for_stmt (stmt)) != 0) | |
431 | { | |
432 | if (dump_enabled_p ()) | |
433 | { | |
434 | dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location, | |
435 | "Build SLP failed: cannot swap operands of " | |
436 | "shared stmt "); | |
437 | dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0); | |
438 | } | |
439 | return -1; | |
440 | } | |
441 | ||
b0b4483e RB |
442 | if (first_op_cond) |
443 | { | |
444 | tree cond = gimple_assign_rhs1 (stmt); | |
4cecd659 BC |
445 | enum tree_code code = TREE_CODE (cond); |
446 | ||
447 | /* Swap. */ | |
448 | if (*swap == 1) | |
449 | { | |
450 | swap_ssa_operands (stmt, &TREE_OPERAND (cond, 0), | |
451 | &TREE_OPERAND (cond, 1)); | |
452 | TREE_SET_CODE (cond, swap_tree_comparison (code)); | |
453 | } | |
454 | /* Invert. */ | |
455 | else | |
456 | { | |
457 | swap_ssa_operands (stmt, gimple_assign_rhs2_ptr (stmt), | |
458 | gimple_assign_rhs3_ptr (stmt)); | |
459 | bool honor_nans = HONOR_NANS (TREE_OPERAND (cond, 0)); | |
460 | code = invert_tree_comparison (TREE_CODE (cond), honor_nans); | |
461 | gcc_assert (code != ERROR_MARK); | |
462 | TREE_SET_CODE (cond, code); | |
463 | } | |
b0b4483e RB |
464 | } |
465 | else | |
466 | swap_ssa_operands (stmt, gimple_assign_rhs1_ptr (stmt), | |
467 | gimple_assign_rhs2_ptr (stmt)); | |
78810bd3 RB |
468 | if (dump_enabled_p ()) |
469 | { | |
470 | dump_printf_loc (MSG_NOTE, vect_location, | |
471 | "swapped operands to match def types in "); | |
472 | dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0); | |
473 | } | |
b0b4483e RB |
474 | } |
475 | ||
4cecd659 | 476 | *swap = swapped; |
b0b4483e | 477 | return 0; |
ebfd146a IR |
478 | } |
479 | ||
480 | ||
6983e6b5 RB |
481 | /* Verify if the scalar stmts STMTS are isomorphic, require data |
482 | permutation or are of unsupported types of operation. Return | |
483 | true if they are, otherwise return false and indicate in *MATCHES | |
484 | which stmts are not isomorphic to the first one. If MATCHES[0] | |
485 | is false then this indicates the comparison could not be | |
4cecd659 BC |
486 | carried out or the stmts will never be vectorized by SLP. |
487 | ||
488 | Note COND_EXPR is possibly ismorphic to another one after swapping its | |
489 | operands. Set SWAP[i] to 1 if stmt I is COND_EXPR and isomorphic to | |
490 | the first stmt by swapping the two operands of comparison; set SWAP[i] | |
491 | to 2 if stmt I is isormorphic to the first stmt by inverting the code | |
492 | of comparison. Take A1 >= B1 ? X1 : Y1 as an exmple, it can be swapped | |
493 | to (B1 <= A1 ? X1 : Y1); or be inverted to (A1 < B1) ? Y1 : X1. */ | |
ebfd146a IR |
494 | |
495 | static bool | |
4cecd659 | 496 | vect_build_slp_tree_1 (vec_info *vinfo, unsigned char *swap, |
355fe088 | 497 | vec<gimple *> stmts, unsigned int group_size, |
6983e6b5 | 498 | unsigned nops, unsigned int *max_nunits, |
97a1a642 | 499 | bool *matches, bool *two_operators) |
ebfd146a | 500 | { |
ebfd146a | 501 | unsigned int i; |
355fe088 | 502 | gimple *first_stmt = stmts[0], *stmt = stmts[0]; |
6876e5bc RB |
503 | enum tree_code first_stmt_code = ERROR_MARK; |
504 | enum tree_code alt_stmt_code = ERROR_MARK; | |
505 | enum tree_code rhs_code = ERROR_MARK; | |
f7e531cf | 506 | enum tree_code first_cond_code = ERROR_MARK; |
ebfd146a | 507 | tree lhs; |
6983e6b5 | 508 | bool need_same_oprnds = false; |
e00cdb8a | 509 | tree vectype = NULL_TREE, scalar_type, first_op1 = NULL_TREE; |
ebfd146a IR |
510 | optab optab; |
511 | int icode; | |
ef4bddc2 RS |
512 | machine_mode optab_op2_mode; |
513 | machine_mode vec_mode; | |
ebfd146a | 514 | HOST_WIDE_INT dummy; |
355fe088 | 515 | gimple *first_load = NULL, *prev_first_load = NULL; |
d092494c | 516 | |
ebfd146a | 517 | /* For every stmt in NODE find its def stmt/s. */ |
9771b263 | 518 | FOR_EACH_VEC_ELT (stmts, i, stmt) |
ebfd146a | 519 | { |
4cecd659 | 520 | swap[i] = 0; |
6983e6b5 RB |
521 | matches[i] = false; |
522 | ||
73fbfcad | 523 | if (dump_enabled_p ()) |
ebfd146a | 524 | { |
78c60e3d SS |
525 | dump_printf_loc (MSG_NOTE, vect_location, "Build SLP for "); |
526 | dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0); | |
ebfd146a IR |
527 | } |
528 | ||
4b5caab7 IR |
529 | /* Fail to vectorize statements marked as unvectorizable. */ |
530 | if (!STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (stmt))) | |
531 | { | |
73fbfcad | 532 | if (dump_enabled_p ()) |
4b5caab7 | 533 | { |
78c60e3d SS |
534 | dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location, |
535 | "Build SLP failed: unvectorizable statement "); | |
536 | dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0); | |
4b5caab7 | 537 | } |
6983e6b5 RB |
538 | /* Fatal mismatch. */ |
539 | matches[0] = false; | |
4b5caab7 IR |
540 | return false; |
541 | } | |
542 | ||
ebfd146a IR |
543 | lhs = gimple_get_lhs (stmt); |
544 | if (lhs == NULL_TREE) | |
545 | { | |
73fbfcad | 546 | if (dump_enabled_p ()) |
ebfd146a | 547 | { |
78c60e3d SS |
548 | dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location, |
549 | "Build SLP failed: not GIMPLE_ASSIGN nor " | |
550 | "GIMPLE_CALL "); | |
551 | dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0); | |
ebfd146a | 552 | } |
6983e6b5 RB |
553 | /* Fatal mismatch. */ |
554 | matches[0] = false; | |
ebfd146a IR |
555 | return false; |
556 | } | |
557 | ||
b8698a0f | 558 | scalar_type = vect_get_smallest_scalar_type (stmt, &dummy, &dummy); |
ebfd146a IR |
559 | vectype = get_vectype_for_scalar_type (scalar_type); |
560 | if (!vectype) | |
561 | { | |
73fbfcad | 562 | if (dump_enabled_p ()) |
ebfd146a | 563 | { |
78c60e3d SS |
564 | dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location, |
565 | "Build SLP failed: unsupported data-type "); | |
566 | dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, | |
567 | scalar_type); | |
e645e942 | 568 | dump_printf (MSG_MISSED_OPTIMIZATION, "\n"); |
ebfd146a | 569 | } |
6983e6b5 RB |
570 | /* Fatal mismatch. */ |
571 | matches[0] = false; | |
ebfd146a IR |
572 | return false; |
573 | } | |
b8698a0f | 574 | |
dfc55d30 RB |
575 | /* If populating the vector type requires unrolling then fail |
576 | before adjusting *max_nunits for basic-block vectorization. */ | |
310213d4 | 577 | if (is_a <bb_vec_info> (vinfo) |
dfc55d30 RB |
578 | && TYPE_VECTOR_SUBPARTS (vectype) > group_size) |
579 | { | |
580 | dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location, | |
581 | "Build SLP failed: unrolling required " | |
582 | "in basic block SLP\n"); | |
583 | /* Fatal mismatch. */ | |
584 | matches[0] = false; | |
585 | return false; | |
586 | } | |
587 | ||
4ef69dfc IR |
588 | /* In case of multiple types we need to detect the smallest type. */ |
589 | if (*max_nunits < TYPE_VECTOR_SUBPARTS (vectype)) | |
97a1a642 | 590 | *max_nunits = TYPE_VECTOR_SUBPARTS (vectype); |
b8698a0f | 591 | |
538dd0b7 | 592 | if (gcall *call_stmt = dyn_cast <gcall *> (stmt)) |
190c2236 JJ |
593 | { |
594 | rhs_code = CALL_EXPR; | |
538dd0b7 DM |
595 | if (gimple_call_internal_p (call_stmt) |
596 | || gimple_call_tail_p (call_stmt) | |
597 | || gimple_call_noreturn_p (call_stmt) | |
598 | || !gimple_call_nothrow_p (call_stmt) | |
599 | || gimple_call_chain (call_stmt)) | |
190c2236 | 600 | { |
73fbfcad | 601 | if (dump_enabled_p ()) |
190c2236 | 602 | { |
78c60e3d SS |
603 | dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location, |
604 | "Build SLP failed: unsupported call type "); | |
538dd0b7 DM |
605 | dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, |
606 | call_stmt, 0); | |
190c2236 | 607 | } |
6983e6b5 RB |
608 | /* Fatal mismatch. */ |
609 | matches[0] = false; | |
190c2236 JJ |
610 | return false; |
611 | } | |
612 | } | |
ebfd146a IR |
613 | else |
614 | rhs_code = gimple_assign_rhs_code (stmt); | |
615 | ||
616 | /* Check the operation. */ | |
617 | if (i == 0) | |
618 | { | |
619 | first_stmt_code = rhs_code; | |
620 | ||
b8698a0f | 621 | /* Shift arguments should be equal in all the packed stmts for a |
ebfd146a IR |
622 | vector shift with scalar shift operand. */ |
623 | if (rhs_code == LSHIFT_EXPR || rhs_code == RSHIFT_EXPR | |
624 | || rhs_code == LROTATE_EXPR | |
625 | || rhs_code == RROTATE_EXPR) | |
626 | { | |
627 | vec_mode = TYPE_MODE (vectype); | |
628 | ||
629 | /* First see if we have a vector/vector shift. */ | |
630 | optab = optab_for_tree_code (rhs_code, vectype, | |
631 | optab_vector); | |
632 | ||
633 | if (!optab | |
947131ba | 634 | || optab_handler (optab, vec_mode) == CODE_FOR_nothing) |
ebfd146a IR |
635 | { |
636 | /* No vector/vector shift, try for a vector/scalar shift. */ | |
637 | optab = optab_for_tree_code (rhs_code, vectype, | |
638 | optab_scalar); | |
639 | ||
640 | if (!optab) | |
641 | { | |
73fbfcad | 642 | if (dump_enabled_p ()) |
78c60e3d | 643 | dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location, |
e645e942 | 644 | "Build SLP failed: no optab.\n"); |
6983e6b5 RB |
645 | /* Fatal mismatch. */ |
646 | matches[0] = false; | |
ebfd146a IR |
647 | return false; |
648 | } | |
947131ba | 649 | icode = (int) optab_handler (optab, vec_mode); |
ebfd146a IR |
650 | if (icode == CODE_FOR_nothing) |
651 | { | |
73fbfcad | 652 | if (dump_enabled_p ()) |
78c60e3d SS |
653 | dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location, |
654 | "Build SLP failed: " | |
e645e942 | 655 | "op not supported by target.\n"); |
6983e6b5 RB |
656 | /* Fatal mismatch. */ |
657 | matches[0] = false; | |
ebfd146a IR |
658 | return false; |
659 | } | |
660 | optab_op2_mode = insn_data[icode].operand[2].mode; | |
661 | if (!VECTOR_MODE_P (optab_op2_mode)) | |
662 | { | |
663 | need_same_oprnds = true; | |
664 | first_op1 = gimple_assign_rhs2 (stmt); | |
665 | } | |
666 | } | |
667 | } | |
36ba4aae IR |
668 | else if (rhs_code == WIDEN_LSHIFT_EXPR) |
669 | { | |
670 | need_same_oprnds = true; | |
671 | first_op1 = gimple_assign_rhs2 (stmt); | |
672 | } | |
ebfd146a IR |
673 | } |
674 | else | |
675 | { | |
6876e5bc RB |
676 | if (first_stmt_code != rhs_code |
677 | && alt_stmt_code == ERROR_MARK) | |
678 | alt_stmt_code = rhs_code; | |
ebfd146a IR |
679 | if (first_stmt_code != rhs_code |
680 | && (first_stmt_code != IMAGPART_EXPR | |
681 | || rhs_code != REALPART_EXPR) | |
682 | && (first_stmt_code != REALPART_EXPR | |
69f11a13 | 683 | || rhs_code != IMAGPART_EXPR) |
6876e5bc RB |
684 | /* Handle mismatches in plus/minus by computing both |
685 | and merging the results. */ | |
686 | && !((first_stmt_code == PLUS_EXPR | |
687 | || first_stmt_code == MINUS_EXPR) | |
688 | && (alt_stmt_code == PLUS_EXPR | |
689 | || alt_stmt_code == MINUS_EXPR) | |
690 | && rhs_code == alt_stmt_code) | |
0d0293ac | 691 | && !(STMT_VINFO_GROUPED_ACCESS (vinfo_for_stmt (stmt)) |
69f11a13 | 692 | && (first_stmt_code == ARRAY_REF |
38000232 | 693 | || first_stmt_code == BIT_FIELD_REF |
69f11a13 IR |
694 | || first_stmt_code == INDIRECT_REF |
695 | || first_stmt_code == COMPONENT_REF | |
696 | || first_stmt_code == MEM_REF))) | |
ebfd146a | 697 | { |
73fbfcad | 698 | if (dump_enabled_p ()) |
ebfd146a | 699 | { |
78c60e3d SS |
700 | dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location, |
701 | "Build SLP failed: different operation " | |
702 | "in stmt "); | |
703 | dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0); | |
6876e5bc RB |
704 | dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location, |
705 | "original stmt "); | |
706 | dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, | |
707 | first_stmt, 0); | |
ebfd146a | 708 | } |
6983e6b5 RB |
709 | /* Mismatch. */ |
710 | continue; | |
ebfd146a | 711 | } |
b8698a0f L |
712 | |
713 | if (need_same_oprnds | |
ebfd146a IR |
714 | && !operand_equal_p (first_op1, gimple_assign_rhs2 (stmt), 0)) |
715 | { | |
73fbfcad | 716 | if (dump_enabled_p ()) |
ebfd146a | 717 | { |
78c60e3d SS |
718 | dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location, |
719 | "Build SLP failed: different shift " | |
720 | "arguments in "); | |
721 | dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0); | |
ebfd146a | 722 | } |
6983e6b5 RB |
723 | /* Mismatch. */ |
724 | continue; | |
ebfd146a | 725 | } |
190c2236 JJ |
726 | |
727 | if (rhs_code == CALL_EXPR) | |
728 | { | |
355fe088 | 729 | gimple *first_stmt = stmts[0]; |
190c2236 JJ |
730 | if (gimple_call_num_args (stmt) != nops |
731 | || !operand_equal_p (gimple_call_fn (first_stmt), | |
732 | gimple_call_fn (stmt), 0) | |
733 | || gimple_call_fntype (first_stmt) | |
734 | != gimple_call_fntype (stmt)) | |
735 | { | |
73fbfcad | 736 | if (dump_enabled_p ()) |
190c2236 | 737 | { |
78c60e3d SS |
738 | dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location, |
739 | "Build SLP failed: different calls in "); | |
740 | dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, | |
741 | stmt, 0); | |
190c2236 | 742 | } |
6983e6b5 RB |
743 | /* Mismatch. */ |
744 | continue; | |
190c2236 JJ |
745 | } |
746 | } | |
ebfd146a IR |
747 | } |
748 | ||
0d0293ac MM |
749 | /* Grouped store or load. */ |
750 | if (STMT_VINFO_GROUPED_ACCESS (vinfo_for_stmt (stmt))) | |
ebfd146a IR |
751 | { |
752 | if (REFERENCE_CLASS_P (lhs)) | |
753 | { | |
754 | /* Store. */ | |
6983e6b5 | 755 | ; |
ebfd146a | 756 | } |
b5aeb3bb IR |
757 | else |
758 | { | |
759 | /* Load. */ | |
e14c1050 | 760 | first_load = GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)); |
b5aeb3bb IR |
761 | if (prev_first_load) |
762 | { | |
763 | /* Check that there are no loads from different interleaving | |
6983e6b5 RB |
764 | chains in the same node. */ |
765 | if (prev_first_load != first_load) | |
78c60e3d | 766 | { |
73fbfcad | 767 | if (dump_enabled_p ()) |
b5aeb3bb | 768 | { |
78c60e3d SS |
769 | dump_printf_loc (MSG_MISSED_OPTIMIZATION, |
770 | vect_location, | |
771 | "Build SLP failed: different " | |
772 | "interleaving chains in one node "); | |
773 | dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, | |
774 | stmt, 0); | |
b5aeb3bb | 775 | } |
6983e6b5 RB |
776 | /* Mismatch. */ |
777 | continue; | |
b5aeb3bb IR |
778 | } |
779 | } | |
780 | else | |
781 | prev_first_load = first_load; | |
ebfd146a | 782 | } |
0d0293ac | 783 | } /* Grouped access. */ |
ebfd146a IR |
784 | else |
785 | { | |
786 | if (TREE_CODE_CLASS (rhs_code) == tcc_reference) | |
787 | { | |
0d0293ac | 788 | /* Not grouped load. */ |
73fbfcad | 789 | if (dump_enabled_p ()) |
ebfd146a | 790 | { |
78c60e3d SS |
791 | dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location, |
792 | "Build SLP failed: not grouped load "); | |
793 | dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0); | |
ebfd146a IR |
794 | } |
795 | ||
0d0293ac | 796 | /* FORNOW: Not grouped loads are not supported. */ |
6983e6b5 RB |
797 | /* Fatal mismatch. */ |
798 | matches[0] = false; | |
ebfd146a IR |
799 | return false; |
800 | } | |
801 | ||
802 | /* Not memory operation. */ | |
803 | if (TREE_CODE_CLASS (rhs_code) != tcc_binary | |
f7e531cf | 804 | && TREE_CODE_CLASS (rhs_code) != tcc_unary |
effb52da | 805 | && TREE_CODE_CLASS (rhs_code) != tcc_expression |
42fd8198 | 806 | && TREE_CODE_CLASS (rhs_code) != tcc_comparison |
190c2236 | 807 | && rhs_code != CALL_EXPR) |
ebfd146a | 808 | { |
73fbfcad | 809 | if (dump_enabled_p ()) |
ebfd146a | 810 | { |
78c60e3d SS |
811 | dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location, |
812 | "Build SLP failed: operation"); | |
813 | dump_printf (MSG_MISSED_OPTIMIZATION, " unsupported "); | |
814 | dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0); | |
ebfd146a | 815 | } |
6983e6b5 RB |
816 | /* Fatal mismatch. */ |
817 | matches[0] = false; | |
ebfd146a IR |
818 | return false; |
819 | } | |
820 | ||
4cecd659 BC |
821 | if (rhs_code == COND_EXPR) |
822 | { | |
823 | tree cond_expr = gimple_assign_rhs1 (stmt); | |
824 | enum tree_code cond_code = TREE_CODE (cond_expr); | |
825 | enum tree_code swap_code = ERROR_MARK; | |
826 | enum tree_code invert_code = ERROR_MARK; | |
f7e531cf IR |
827 | |
828 | if (i == 0) | |
829 | first_cond_code = TREE_CODE (cond_expr); | |
4cecd659 BC |
830 | else if (TREE_CODE_CLASS (cond_code) == tcc_comparison) |
831 | { | |
832 | bool honor_nans = HONOR_NANS (TREE_OPERAND (cond_expr, 0)); | |
833 | swap_code = swap_tree_comparison (cond_code); | |
834 | invert_code = invert_tree_comparison (cond_code, honor_nans); | |
835 | } | |
836 | ||
837 | if (first_cond_code == cond_code) | |
838 | ; | |
839 | /* Isomorphic can be achieved by swapping. */ | |
840 | else if (first_cond_code == swap_code) | |
841 | swap[i] = 1; | |
842 | /* Isomorphic can be achieved by inverting. */ | |
843 | else if (first_cond_code == invert_code) | |
844 | swap[i] = 2; | |
845 | else | |
846 | { | |
847 | if (dump_enabled_p ()) | |
848 | { | |
849 | dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location, | |
78c60e3d SS |
850 | "Build SLP failed: different" |
851 | " operation"); | |
4cecd659 | 852 | dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, |
78c60e3d | 853 | stmt, 0); |
4cecd659 | 854 | } |
6983e6b5 RB |
855 | /* Mismatch. */ |
856 | continue; | |
f7e531cf | 857 | } |
4cecd659 | 858 | } |
ebfd146a | 859 | } |
6983e6b5 RB |
860 | |
861 | matches[i] = true; | |
862 | } | |
863 | ||
864 | for (i = 0; i < group_size; ++i) | |
865 | if (!matches[i]) | |
866 | return false; | |
867 | ||
6876e5bc RB |
868 | /* If we allowed a two-operation SLP node verify the target can cope |
869 | with the permute we are going to use. */ | |
870 | if (alt_stmt_code != ERROR_MARK | |
871 | && TREE_CODE_CLASS (alt_stmt_code) != tcc_reference) | |
872 | { | |
873 | unsigned char *sel | |
874 | = XALLOCAVEC (unsigned char, TYPE_VECTOR_SUBPARTS (vectype)); | |
875 | for (i = 0; i < TYPE_VECTOR_SUBPARTS (vectype); ++i) | |
876 | { | |
877 | sel[i] = i; | |
878 | if (gimple_assign_rhs_code (stmts[i % group_size]) == alt_stmt_code) | |
879 | sel[i] += TYPE_VECTOR_SUBPARTS (vectype); | |
880 | } | |
881 | if (!can_vec_perm_p (TYPE_MODE (vectype), false, sel)) | |
882 | { | |
883 | for (i = 0; i < group_size; ++i) | |
884 | if (gimple_assign_rhs_code (stmts[i]) == alt_stmt_code) | |
885 | { | |
886 | matches[i] = false; | |
887 | if (dump_enabled_p ()) | |
888 | { | |
889 | dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location, | |
890 | "Build SLP failed: different operation " | |
891 | "in stmt "); | |
892 | dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, | |
893 | stmts[i], 0); | |
894 | dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location, | |
895 | "original stmt "); | |
896 | dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, | |
897 | first_stmt, 0); | |
898 | } | |
899 | } | |
900 | return false; | |
901 | } | |
902 | *two_operators = true; | |
903 | } | |
904 | ||
6983e6b5 RB |
905 | return true; |
906 | } | |
907 | ||
908 | /* Recursively build an SLP tree starting from NODE. | |
909 | Fail (and return a value not equal to zero) if def-stmts are not | |
910 | isomorphic, require data permutation or are of unsupported types of | |
911 | operation. Otherwise, return 0. | |
912 | The value returned is the depth in the SLP tree where a mismatch | |
913 | was found. */ | |
914 | ||
e403d17e | 915 | static slp_tree |
310213d4 | 916 | vect_build_slp_tree (vec_info *vinfo, |
e403d17e | 917 | vec<gimple *> stmts, unsigned int group_size, |
6983e6b5 RB |
918 | unsigned int *max_nunits, |
919 | vec<slp_tree> *loads, | |
1428105c RB |
920 | bool *matches, unsigned *npermutes, unsigned *tree_size, |
921 | unsigned max_tree_size) | |
6983e6b5 | 922 | { |
e403d17e | 923 | unsigned nops, i, this_tree_size = 0, this_max_nunits = *max_nunits; |
355fe088 | 924 | gimple *stmt; |
e403d17e | 925 | slp_tree node; |
6983e6b5 | 926 | |
6983e6b5 RB |
927 | matches[0] = false; |
928 | ||
e403d17e | 929 | stmt = stmts[0]; |
6983e6b5 RB |
930 | if (is_gimple_call (stmt)) |
931 | nops = gimple_call_num_args (stmt); | |
932 | else if (is_gimple_assign (stmt)) | |
933 | { | |
934 | nops = gimple_num_ops (stmt) - 1; | |
935 | if (gimple_assign_rhs_code (stmt) == COND_EXPR) | |
936 | nops++; | |
ebfd146a | 937 | } |
6983e6b5 | 938 | else |
e403d17e | 939 | return NULL; |
6983e6b5 | 940 | |
6876e5bc | 941 | bool two_operators = false; |
4cecd659 BC |
942 | unsigned char *swap = XALLOCAVEC (unsigned char, group_size); |
943 | if (!vect_build_slp_tree_1 (vinfo, swap, | |
e403d17e RB |
944 | stmts, group_size, nops, |
945 | &this_max_nunits, matches, &two_operators)) | |
946 | return NULL; | |
ebfd146a | 947 | |
6983e6b5 RB |
948 | /* If the SLP node is a load, terminate the recursion. */ |
949 | if (STMT_VINFO_GROUPED_ACCESS (vinfo_for_stmt (stmt)) | |
950 | && DR_IS_READ (STMT_VINFO_DATA_REF (vinfo_for_stmt (stmt)))) | |
ebfd146a | 951 | { |
e403d17e RB |
952 | *max_nunits = this_max_nunits; |
953 | node = vect_create_new_slp_node (stmts); | |
954 | loads->safe_push (node); | |
955 | return node; | |
ebfd146a IR |
956 | } |
957 | ||
6983e6b5 RB |
958 | /* Get at the operands, verifying they are compatible. */ |
959 | vec<slp_oprnd_info> oprnds_info = vect_create_oprnd_info (nops, group_size); | |
960 | slp_oprnd_info oprnd_info; | |
e403d17e | 961 | FOR_EACH_VEC_ELT (stmts, i, stmt) |
6983e6b5 | 962 | { |
4cecd659 BC |
963 | int res = vect_get_and_check_slp_defs (vinfo, &swap[i], |
964 | stmt, i, &oprnds_info); | |
965 | if (res != 0) | |
966 | matches[(res == -1) ? 0 : i] = false; | |
967 | if (!matches[0]) | |
968 | break; | |
6983e6b5 | 969 | } |
b0b4483e RB |
970 | for (i = 0; i < group_size; ++i) |
971 | if (!matches[i]) | |
972 | { | |
973 | vect_free_oprnd_info (oprnds_info); | |
e403d17e | 974 | return NULL; |
b0b4483e | 975 | } |
6983e6b5 | 976 | |
e403d17e RB |
977 | auto_vec<slp_tree, 4> children; |
978 | auto_vec<slp_tree> this_loads; | |
979 | ||
980 | stmt = stmts[0]; | |
6983e6b5 | 981 | |
b8698a0f | 982 | /* Create SLP_TREE nodes for the definition node/s. */ |
9771b263 | 983 | FOR_EACH_VEC_ELT (oprnds_info, i, oprnd_info) |
ebfd146a | 984 | { |
d092494c | 985 | slp_tree child; |
e403d17e RB |
986 | unsigned old_nloads = this_loads.length (); |
987 | unsigned old_tree_size = this_tree_size; | |
988 | unsigned int j; | |
b8698a0f | 989 | |
d092494c IR |
990 | if (oprnd_info->first_dt != vect_internal_def) |
991 | continue; | |
ebfd146a | 992 | |
1428105c RB |
993 | if (++this_tree_size > max_tree_size) |
994 | { | |
e403d17e RB |
995 | FOR_EACH_VEC_ELT (children, j, child) |
996 | vect_free_slp_tree (child); | |
1428105c | 997 | vect_free_oprnd_info (oprnds_info); |
e403d17e | 998 | return NULL; |
1428105c RB |
999 | } |
1000 | ||
e403d17e RB |
1001 | if ((child = vect_build_slp_tree (vinfo, oprnd_info->def_stmts, |
1002 | group_size, &this_max_nunits, | |
1003 | &this_loads, matches, npermutes, | |
1004 | &this_tree_size, | |
1005 | max_tree_size)) != NULL) | |
6983e6b5 | 1006 | { |
3fc356dc RB |
1007 | /* If we have all children of child built up from scalars then just |
1008 | throw that away and build it up this node from scalars. */ | |
995b6fe0 RB |
1009 | if (!SLP_TREE_CHILDREN (child).is_empty () |
1010 | /* ??? Rejecting patterns this way doesn't work. We'd have to | |
1011 | do extra work to cancel the pattern so the uses see the | |
1012 | scalar version. */ | |
1013 | && !is_pattern_stmt_p | |
1014 | (vinfo_for_stmt (SLP_TREE_SCALAR_STMTS (child)[0]))) | |
3fc356dc | 1015 | { |
3fc356dc RB |
1016 | slp_tree grandchild; |
1017 | ||
1018 | FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (child), j, grandchild) | |
603cca93 | 1019 | if (SLP_TREE_DEF_TYPE (grandchild) == vect_internal_def) |
3fc356dc RB |
1020 | break; |
1021 | if (!grandchild) | |
1022 | { | |
1023 | /* Roll back. */ | |
e403d17e RB |
1024 | this_loads.truncate (old_nloads); |
1025 | this_tree_size = old_tree_size; | |
3fc356dc | 1026 | FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (child), j, grandchild) |
603cca93 | 1027 | vect_free_slp_tree (grandchild); |
3fc356dc RB |
1028 | SLP_TREE_CHILDREN (child).truncate (0); |
1029 | ||
1030 | dump_printf_loc (MSG_NOTE, vect_location, | |
1031 | "Building parent vector operands from " | |
1032 | "scalars instead\n"); | |
1033 | oprnd_info->def_stmts = vNULL; | |
603cca93 | 1034 | SLP_TREE_DEF_TYPE (child) = vect_external_def; |
e403d17e | 1035 | children.safe_push (child); |
3fc356dc RB |
1036 | continue; |
1037 | } | |
1038 | } | |
1039 | ||
6983e6b5 | 1040 | oprnd_info->def_stmts = vNULL; |
e403d17e | 1041 | children.safe_push (child); |
6983e6b5 RB |
1042 | continue; |
1043 | } | |
1044 | ||
90dd6e3d RB |
1045 | /* If the SLP build failed fatally and we analyze a basic-block |
1046 | simply treat nodes we fail to build as externally defined | |
1047 | (and thus build vectors from the scalar defs). | |
1048 | The cost model will reject outright expensive cases. | |
1049 | ??? This doesn't treat cases where permutation ultimatively | |
1050 | fails (or we don't try permutation below). Ideally we'd | |
1051 | even compute a permutation that will end up with the maximum | |
1052 | SLP tree size... */ | |
310213d4 | 1053 | if (is_a <bb_vec_info> (vinfo) |
90dd6e3d RB |
1054 | && !matches[0] |
1055 | /* ??? Rejecting patterns this way doesn't work. We'd have to | |
1056 | do extra work to cancel the pattern so the uses see the | |
1057 | scalar version. */ | |
1058 | && !is_pattern_stmt_p (vinfo_for_stmt (stmt))) | |
1059 | { | |
1060 | dump_printf_loc (MSG_NOTE, vect_location, | |
1061 | "Building vector operands from scalars\n"); | |
e403d17e | 1062 | child = vect_create_new_slp_node (oprnd_info->def_stmts); |
603cca93 | 1063 | SLP_TREE_DEF_TYPE (child) = vect_external_def; |
e403d17e RB |
1064 | children.safe_push (child); |
1065 | oprnd_info->def_stmts = vNULL; | |
90dd6e3d RB |
1066 | continue; |
1067 | } | |
1068 | ||
6983e6b5 RB |
1069 | /* If the SLP build for operand zero failed and operand zero |
1070 | and one can be commutated try that for the scalar stmts | |
1071 | that failed the match. */ | |
1072 | if (i == 0 | |
1073 | /* A first scalar stmt mismatch signals a fatal mismatch. */ | |
1074 | && matches[0] | |
1075 | /* ??? For COND_EXPRs we can swap the comparison operands | |
1076 | as well as the arms under some constraints. */ | |
1077 | && nops == 2 | |
1078 | && oprnds_info[1]->first_dt == vect_internal_def | |
1079 | && is_gimple_assign (stmt) | |
1080 | && commutative_tree_code (gimple_assign_rhs_code (stmt)) | |
e403d17e | 1081 | && ! two_operators |
6983e6b5 RB |
1082 | /* Do so only if the number of not successful permutes was nor more |
1083 | than a cut-ff as re-trying the recursive match on | |
1084 | possibly each level of the tree would expose exponential | |
1085 | behavior. */ | |
1086 | && *npermutes < 4) | |
1087 | { | |
78810bd3 RB |
1088 | /* Verify if we can safely swap or if we committed to a specific |
1089 | operand order already. */ | |
1090 | for (j = 0; j < group_size; ++j) | |
1091 | if (!matches[j] | |
4cecd659 BC |
1092 | && (swap[j] != 0 |
1093 | || STMT_VINFO_NUM_SLP_USES (vinfo_for_stmt (stmts[j])))) | |
78810bd3 RB |
1094 | { |
1095 | if (dump_enabled_p ()) | |
1096 | { | |
1097 | dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location, | |
1098 | "Build SLP failed: cannot swap operands " | |
1099 | "of shared stmt "); | |
1100 | dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, | |
1101 | stmts[j], 0); | |
1102 | } | |
1103 | goto fail; | |
1104 | } | |
1105 | ||
6983e6b5 | 1106 | /* Swap mismatched definition stmts. */ |
b0b4483e RB |
1107 | dump_printf_loc (MSG_NOTE, vect_location, |
1108 | "Re-trying with swapped operands of stmts "); | |
e72baed7 | 1109 | for (j = 0; j < group_size; ++j) |
6983e6b5 RB |
1110 | if (!matches[j]) |
1111 | { | |
6b4db501 MM |
1112 | std::swap (oprnds_info[0]->def_stmts[j], |
1113 | oprnds_info[1]->def_stmts[j]); | |
b0b4483e | 1114 | dump_printf (MSG_NOTE, "%d ", j); |
6983e6b5 | 1115 | } |
b0b4483e | 1116 | dump_printf (MSG_NOTE, "\n"); |
74574669 RB |
1117 | /* And try again with scratch 'matches' ... */ |
1118 | bool *tem = XALLOCAVEC (bool, group_size); | |
e403d17e RB |
1119 | if ((child = vect_build_slp_tree (vinfo, oprnd_info->def_stmts, |
1120 | group_size, &this_max_nunits, | |
1121 | &this_loads, tem, npermutes, | |
1122 | &this_tree_size, | |
1123 | max_tree_size)) != NULL) | |
6983e6b5 | 1124 | { |
60f2b864 RB |
1125 | /* ... so if successful we can apply the operand swapping |
1126 | to the GIMPLE IL. This is necessary because for example | |
1127 | vect_get_slp_defs uses operand indexes and thus expects | |
1128 | canonical operand order. This is also necessary even | |
1129 | if we end up building the operand from scalars as | |
1130 | we'll continue to process swapped operand two. */ | |
1131 | for (j = 0; j < group_size; ++j) | |
f47cda24 | 1132 | { |
e403d17e | 1133 | gimple *stmt = stmts[j]; |
f47cda24 RB |
1134 | gimple_set_plf (stmt, GF_PLF_1, false); |
1135 | } | |
1136 | for (j = 0; j < group_size; ++j) | |
1137 | { | |
e403d17e | 1138 | gimple *stmt = stmts[j]; |
f47cda24 RB |
1139 | if (!matches[j]) |
1140 | { | |
1141 | /* Avoid swapping operands twice. */ | |
1142 | if (gimple_plf (stmt, GF_PLF_1)) | |
1143 | continue; | |
1144 | swap_ssa_operands (stmt, gimple_assign_rhs1_ptr (stmt), | |
1145 | gimple_assign_rhs2_ptr (stmt)); | |
1146 | gimple_set_plf (stmt, GF_PLF_1, true); | |
1147 | } | |
1148 | } | |
1149 | /* Verify we swap all duplicates or none. */ | |
1150 | if (flag_checking) | |
1151 | for (j = 0; j < group_size; ++j) | |
60f2b864 | 1152 | { |
e403d17e | 1153 | gimple *stmt = stmts[j]; |
f47cda24 | 1154 | gcc_assert (gimple_plf (stmt, GF_PLF_1) == ! matches[j]); |
60f2b864 RB |
1155 | } |
1156 | ||
85c69b0b RB |
1157 | /* If we have all children of child built up from scalars then |
1158 | just throw that away and build it up this node from scalars. */ | |
995b6fe0 RB |
1159 | if (!SLP_TREE_CHILDREN (child).is_empty () |
1160 | /* ??? Rejecting patterns this way doesn't work. We'd have | |
1161 | to do extra work to cancel the pattern so the uses see the | |
1162 | scalar version. */ | |
1163 | && !is_pattern_stmt_p | |
1164 | (vinfo_for_stmt (SLP_TREE_SCALAR_STMTS (child)[0]))) | |
85c69b0b RB |
1165 | { |
1166 | unsigned int j; | |
1167 | slp_tree grandchild; | |
1168 | ||
1169 | FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (child), j, grandchild) | |
603cca93 | 1170 | if (SLP_TREE_DEF_TYPE (grandchild) == vect_internal_def) |
85c69b0b RB |
1171 | break; |
1172 | if (!grandchild) | |
1173 | { | |
1174 | /* Roll back. */ | |
e403d17e RB |
1175 | this_loads.truncate (old_nloads); |
1176 | this_tree_size = old_tree_size; | |
85c69b0b RB |
1177 | FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (child), j, grandchild) |
1178 | vect_free_slp_tree (grandchild); | |
1179 | SLP_TREE_CHILDREN (child).truncate (0); | |
1180 | ||
1181 | dump_printf_loc (MSG_NOTE, vect_location, | |
1182 | "Building parent vector operands from " | |
1183 | "scalars instead\n"); | |
1184 | oprnd_info->def_stmts = vNULL; | |
603cca93 | 1185 | SLP_TREE_DEF_TYPE (child) = vect_external_def; |
e403d17e | 1186 | children.safe_push (child); |
85c69b0b RB |
1187 | continue; |
1188 | } | |
1189 | } | |
1190 | ||
6983e6b5 | 1191 | oprnd_info->def_stmts = vNULL; |
e403d17e | 1192 | children.safe_push (child); |
6983e6b5 RB |
1193 | continue; |
1194 | } | |
1195 | ||
1196 | ++*npermutes; | |
1197 | } | |
1198 | ||
78810bd3 | 1199 | fail: |
e403d17e RB |
1200 | gcc_assert (child == NULL); |
1201 | FOR_EACH_VEC_ELT (children, j, child) | |
1202 | vect_free_slp_tree (child); | |
6983e6b5 | 1203 | vect_free_oprnd_info (oprnds_info); |
e403d17e | 1204 | return NULL; |
ebfd146a IR |
1205 | } |
1206 | ||
e403d17e RB |
1207 | vect_free_oprnd_info (oprnds_info); |
1208 | ||
1428105c RB |
1209 | if (tree_size) |
1210 | *tree_size += this_tree_size; | |
e403d17e RB |
1211 | *max_nunits = this_max_nunits; |
1212 | loads->safe_splice (this_loads); | |
1428105c | 1213 | |
e403d17e RB |
1214 | node = vect_create_new_slp_node (stmts); |
1215 | SLP_TREE_TWO_OPERATORS (node) = two_operators; | |
1216 | SLP_TREE_CHILDREN (node).splice (children); | |
1217 | return node; | |
ebfd146a IR |
1218 | } |
1219 | ||
78c60e3d | 1220 | /* Dump a slp tree NODE using flags specified in DUMP_KIND. */ |
ebfd146a IR |
1221 | |
1222 | static void | |
c2a12ca0 | 1223 | vect_print_slp_tree (int dump_kind, location_t loc, slp_tree node) |
ebfd146a IR |
1224 | { |
1225 | int i; | |
355fe088 | 1226 | gimple *stmt; |
d755c7ef | 1227 | slp_tree child; |
ebfd146a | 1228 | |
603cca93 RB |
1229 | dump_printf_loc (dump_kind, loc, "node%s\n", |
1230 | SLP_TREE_DEF_TYPE (node) != vect_internal_def | |
1231 | ? " (external)" : ""); | |
9771b263 | 1232 | FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt) |
ebfd146a | 1233 | { |
c2a12ca0 | 1234 | dump_printf_loc (dump_kind, loc, "\tstmt %d ", i); |
78c60e3d | 1235 | dump_gimple_stmt (dump_kind, TDF_SLIM, stmt, 0); |
ebfd146a | 1236 | } |
9771b263 | 1237 | FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child) |
c2a12ca0 | 1238 | vect_print_slp_tree (dump_kind, loc, child); |
ebfd146a IR |
1239 | } |
1240 | ||
1241 | ||
b8698a0f L |
1242 | /* Mark the tree rooted at NODE with MARK (PURE_SLP or HYBRID). |
1243 | If MARK is HYBRID, it refers to a specific stmt in NODE (the stmt at index | |
ff802fa1 | 1244 | J). Otherwise, MARK is PURE_SLP and J is -1, which indicates that all the |
ebfd146a IR |
1245 | stmts in NODE are to be marked. */ |
1246 | ||
1247 | static void | |
1248 | vect_mark_slp_stmts (slp_tree node, enum slp_vect_type mark, int j) | |
1249 | { | |
1250 | int i; | |
355fe088 | 1251 | gimple *stmt; |
d755c7ef | 1252 | slp_tree child; |
ebfd146a | 1253 | |
603cca93 | 1254 | if (SLP_TREE_DEF_TYPE (node) != vect_internal_def) |
ebfd146a IR |
1255 | return; |
1256 | ||
9771b263 | 1257 | FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt) |
ebfd146a IR |
1258 | if (j < 0 || i == j) |
1259 | STMT_SLP_TYPE (vinfo_for_stmt (stmt)) = mark; | |
1260 | ||
9771b263 | 1261 | FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child) |
d755c7ef | 1262 | vect_mark_slp_stmts (child, mark, j); |
ebfd146a IR |
1263 | } |
1264 | ||
1265 | ||
a70d6342 IR |
1266 | /* Mark the statements of the tree rooted at NODE as relevant (vect_used). */ |
1267 | ||
1268 | static void | |
1269 | vect_mark_slp_stmts_relevant (slp_tree node) | |
1270 | { | |
1271 | int i; | |
355fe088 | 1272 | gimple *stmt; |
a70d6342 | 1273 | stmt_vec_info stmt_info; |
d755c7ef | 1274 | slp_tree child; |
a70d6342 | 1275 | |
603cca93 | 1276 | if (SLP_TREE_DEF_TYPE (node) != vect_internal_def) |
a70d6342 IR |
1277 | return; |
1278 | ||
9771b263 | 1279 | FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt) |
a70d6342 IR |
1280 | { |
1281 | stmt_info = vinfo_for_stmt (stmt); | |
b8698a0f | 1282 | gcc_assert (!STMT_VINFO_RELEVANT (stmt_info) |
a70d6342 IR |
1283 | || STMT_VINFO_RELEVANT (stmt_info) == vect_used_in_scope); |
1284 | STMT_VINFO_RELEVANT (stmt_info) = vect_used_in_scope; | |
1285 | } | |
1286 | ||
9771b263 | 1287 | FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child) |
d755c7ef | 1288 | vect_mark_slp_stmts_relevant (child); |
a70d6342 IR |
1289 | } |
1290 | ||
1291 | ||
b5aeb3bb IR |
1292 | /* Rearrange the statements of NODE according to PERMUTATION. */ |
1293 | ||
1294 | static void | |
1295 | vect_slp_rearrange_stmts (slp_tree node, unsigned int group_size, | |
01d8bf07 | 1296 | vec<unsigned> permutation) |
b5aeb3bb | 1297 | { |
355fe088 TS |
1298 | gimple *stmt; |
1299 | vec<gimple *> tmp_stmts; | |
d755c7ef RB |
1300 | unsigned int i; |
1301 | slp_tree child; | |
b5aeb3bb | 1302 | |
9771b263 | 1303 | FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child) |
d755c7ef | 1304 | vect_slp_rearrange_stmts (child, group_size, permutation); |
b5aeb3bb | 1305 | |
9771b263 DN |
1306 | gcc_assert (group_size == SLP_TREE_SCALAR_STMTS (node).length ()); |
1307 | tmp_stmts.create (group_size); | |
d755c7ef | 1308 | tmp_stmts.quick_grow_cleared (group_size); |
b5aeb3bb | 1309 | |
9771b263 | 1310 | FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt) |
d755c7ef | 1311 | tmp_stmts[permutation[i]] = stmt; |
b5aeb3bb | 1312 | |
9771b263 | 1313 | SLP_TREE_SCALAR_STMTS (node).release (); |
b5aeb3bb IR |
1314 | SLP_TREE_SCALAR_STMTS (node) = tmp_stmts; |
1315 | } | |
1316 | ||
1317 | ||
b266b968 RB |
1318 | /* Attempt to reorder stmts in a reduction chain so that we don't |
1319 | require any load permutation. Return true if that was possible, | |
1320 | otherwise return false. */ | |
1321 | ||
1322 | static bool | |
1323 | vect_attempt_slp_rearrange_stmts (slp_instance slp_instn) | |
1324 | { | |
1325 | unsigned int group_size = SLP_INSTANCE_GROUP_SIZE (slp_instn); | |
1326 | unsigned int i, j; | |
b266b968 RB |
1327 | unsigned int lidx; |
1328 | slp_tree node, load; | |
1329 | ||
1330 | /* Compare all the permutation sequences to the first one. We know | |
1331 | that at least one load is permuted. */ | |
1332 | node = SLP_INSTANCE_LOADS (slp_instn)[0]; | |
1333 | if (!node->load_permutation.exists ()) | |
1334 | return false; | |
1335 | for (i = 1; SLP_INSTANCE_LOADS (slp_instn).iterate (i, &load); ++i) | |
1336 | { | |
1337 | if (!load->load_permutation.exists ()) | |
1338 | return false; | |
1339 | FOR_EACH_VEC_ELT (load->load_permutation, j, lidx) | |
1340 | if (lidx != node->load_permutation[j]) | |
1341 | return false; | |
1342 | } | |
1343 | ||
1344 | /* Check that the loads in the first sequence are different and there | |
1345 | are no gaps between them. */ | |
7ba9e72d | 1346 | auto_sbitmap load_index (group_size); |
b266b968 RB |
1347 | bitmap_clear (load_index); |
1348 | FOR_EACH_VEC_ELT (node->load_permutation, i, lidx) | |
1349 | { | |
41eefe13 | 1350 | if (lidx >= group_size) |
7ba9e72d | 1351 | return false; |
b266b968 | 1352 | if (bitmap_bit_p (load_index, lidx)) |
7ba9e72d TS |
1353 | return false; |
1354 | ||
b266b968 RB |
1355 | bitmap_set_bit (load_index, lidx); |
1356 | } | |
1357 | for (i = 0; i < group_size; i++) | |
1358 | if (!bitmap_bit_p (load_index, i)) | |
7ba9e72d | 1359 | return false; |
b266b968 RB |
1360 | |
1361 | /* This permutation is valid for reduction. Since the order of the | |
1362 | statements in the nodes is not important unless they are memory | |
1363 | accesses, we can rearrange the statements in all the nodes | |
1364 | according to the order of the loads. */ | |
1365 | vect_slp_rearrange_stmts (SLP_INSTANCE_TREE (slp_instn), group_size, | |
1366 | node->load_permutation); | |
1367 | ||
1368 | /* We are done, no actual permutations need to be generated. */ | |
c4e360f4 | 1369 | unsigned int unrolling_factor = SLP_INSTANCE_UNROLLING_FACTOR (slp_instn); |
b266b968 | 1370 | FOR_EACH_VEC_ELT (SLP_INSTANCE_LOADS (slp_instn), i, node) |
c4e360f4 RB |
1371 | { |
1372 | gimple *first_stmt = SLP_TREE_SCALAR_STMTS (node)[0]; | |
1373 | first_stmt = GROUP_FIRST_ELEMENT (vinfo_for_stmt (first_stmt)); | |
1374 | /* But we have to keep those permutations that are required because | |
1375 | of handling of gaps. */ | |
1376 | if (unrolling_factor == 1 | |
1377 | || (group_size == GROUP_SIZE (vinfo_for_stmt (first_stmt)) | |
1378 | && GROUP_GAP (vinfo_for_stmt (first_stmt)) == 0)) | |
1379 | SLP_TREE_LOAD_PERMUTATION (node).release (); | |
cbd400b4 RB |
1380 | else |
1381 | for (j = 0; j < SLP_TREE_LOAD_PERMUTATION (node).length (); ++j) | |
1382 | SLP_TREE_LOAD_PERMUTATION (node)[j] = j; | |
c4e360f4 RB |
1383 | } |
1384 | ||
b266b968 RB |
1385 | return true; |
1386 | } | |
1387 | ||
01d8bf07 RB |
1388 | /* Check if the required load permutations in the SLP instance |
1389 | SLP_INSTN are supported. */ | |
ebfd146a IR |
1390 | |
1391 | static bool | |
01d8bf07 | 1392 | vect_supported_load_permutation_p (slp_instance slp_instn) |
ebfd146a | 1393 | { |
01d8bf07 RB |
1394 | unsigned int group_size = SLP_INSTANCE_GROUP_SIZE (slp_instn); |
1395 | unsigned int i, j, k, next; | |
6983e6b5 | 1396 | slp_tree node; |
a5b50aa1 | 1397 | gimple *stmt, *load, *next_load; |
ebfd146a | 1398 | |
73fbfcad | 1399 | if (dump_enabled_p ()) |
ebfd146a | 1400 | { |
78c60e3d | 1401 | dump_printf_loc (MSG_NOTE, vect_location, "Load permutation "); |
01d8bf07 RB |
1402 | FOR_EACH_VEC_ELT (SLP_INSTANCE_LOADS (slp_instn), i, node) |
1403 | if (node->load_permutation.exists ()) | |
1404 | FOR_EACH_VEC_ELT (node->load_permutation, j, next) | |
1405 | dump_printf (MSG_NOTE, "%d ", next); | |
1406 | else | |
bddc974e TJ |
1407 | for (k = 0; k < group_size; ++k) |
1408 | dump_printf (MSG_NOTE, "%d ", k); | |
e645e942 | 1409 | dump_printf (MSG_NOTE, "\n"); |
ebfd146a IR |
1410 | } |
1411 | ||
b5aeb3bb IR |
1412 | /* In case of reduction every load permutation is allowed, since the order |
1413 | of the reduction statements is not important (as opposed to the case of | |
0d0293ac | 1414 | grouped stores). The only condition we need to check is that all the |
b5aeb3bb IR |
1415 | load nodes are of the same size and have the same permutation (and then |
1416 | rearrange all the nodes of the SLP instance according to this | |
1417 | permutation). */ | |
1418 | ||
1419 | /* Check that all the load nodes are of the same size. */ | |
01d8bf07 | 1420 | /* ??? Can't we assert this? */ |
9771b263 | 1421 | FOR_EACH_VEC_ELT (SLP_INSTANCE_LOADS (slp_instn), i, node) |
6983e6b5 RB |
1422 | if (SLP_TREE_SCALAR_STMTS (node).length () != (unsigned) group_size) |
1423 | return false; | |
2200fc49 | 1424 | |
b5aeb3bb | 1425 | node = SLP_INSTANCE_TREE (slp_instn); |
9771b263 | 1426 | stmt = SLP_TREE_SCALAR_STMTS (node)[0]; |
b5aeb3bb | 1427 | |
b010117a | 1428 | /* Reduction (there are no data-refs in the root). |
b266b968 | 1429 | In reduction chain the order of the loads is not important. */ |
b010117a IR |
1430 | if (!STMT_VINFO_DATA_REF (vinfo_for_stmt (stmt)) |
1431 | && !GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt))) | |
c4e360f4 | 1432 | vect_attempt_slp_rearrange_stmts (slp_instn); |
b5aeb3bb | 1433 | |
6aa904c4 IR |
1434 | /* In basic block vectorization we allow any subchain of an interleaving |
1435 | chain. | |
1436 | FORNOW: not supported in loop SLP because of realignment compications. */ | |
01d8bf07 | 1437 | if (STMT_VINFO_BB_VINFO (vinfo_for_stmt (stmt))) |
6aa904c4 | 1438 | { |
240a94da RB |
1439 | /* Check whether the loads in an instance form a subchain and thus |
1440 | no permutation is necessary. */ | |
9771b263 | 1441 | FOR_EACH_VEC_ELT (SLP_INSTANCE_LOADS (slp_instn), i, node) |
6aa904c4 | 1442 | { |
9626d143 RB |
1443 | if (!SLP_TREE_LOAD_PERMUTATION (node).exists ()) |
1444 | continue; | |
240a94da | 1445 | bool subchain_p = true; |
6aa904c4 | 1446 | next_load = NULL; |
9771b263 | 1447 | FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), j, load) |
6aa904c4 | 1448 | { |
5b5826c4 RB |
1449 | if (j != 0 |
1450 | && (next_load != load | |
1451 | || GROUP_GAP (vinfo_for_stmt (load)) != 1)) | |
240a94da RB |
1452 | { |
1453 | subchain_p = false; | |
1454 | break; | |
1455 | } | |
6aa904c4 IR |
1456 | next_load = GROUP_NEXT_ELEMENT (vinfo_for_stmt (load)); |
1457 | } | |
240a94da RB |
1458 | if (subchain_p) |
1459 | SLP_TREE_LOAD_PERMUTATION (node).release (); | |
1460 | else | |
1461 | { | |
fe73a332 RB |
1462 | stmt_vec_info group_info |
1463 | = vinfo_for_stmt (SLP_TREE_SCALAR_STMTS (node)[0]); | |
1464 | group_info = vinfo_for_stmt (GROUP_FIRST_ELEMENT (group_info)); | |
1465 | unsigned nunits | |
1466 | = TYPE_VECTOR_SUBPARTS (STMT_VINFO_VECTYPE (group_info)); | |
1467 | unsigned k, maxk = 0; | |
1468 | FOR_EACH_VEC_ELT (SLP_TREE_LOAD_PERMUTATION (node), j, k) | |
1469 | if (k > maxk) | |
1470 | maxk = k; | |
1471 | /* In BB vectorization we may not actually use a loaded vector | |
1472 | accessing elements in excess of GROUP_SIZE. */ | |
1473 | if (maxk >= (GROUP_SIZE (group_info) & ~(nunits - 1))) | |
1474 | { | |
1475 | dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location, | |
1476 | "BB vectorization with gaps at the end of " | |
1477 | "a load is not supported\n"); | |
1478 | return false; | |
1479 | } | |
1480 | ||
240a94da RB |
1481 | /* Verify the permutation can be generated. */ |
1482 | vec<tree> tem; | |
29afecdf | 1483 | unsigned n_perms; |
240a94da | 1484 | if (!vect_transform_slp_perm_load (node, tem, NULL, |
29afecdf | 1485 | 1, slp_instn, true, &n_perms)) |
240a94da RB |
1486 | { |
1487 | dump_printf_loc (MSG_MISSED_OPTIMIZATION, | |
1488 | vect_location, | |
1489 | "unsupported load permutation\n"); | |
1490 | return false; | |
1491 | } | |
1492 | } | |
6aa904c4 | 1493 | } |
01d8bf07 | 1494 | return true; |
6aa904c4 IR |
1495 | } |
1496 | ||
9b999e8c | 1497 | /* For loop vectorization verify we can generate the permutation. */ |
29afecdf | 1498 | unsigned n_perms; |
01d8bf07 RB |
1499 | FOR_EACH_VEC_ELT (SLP_INSTANCE_LOADS (slp_instn), i, node) |
1500 | if (node->load_permutation.exists () | |
1501 | && !vect_transform_slp_perm_load | |
1502 | (node, vNULL, NULL, | |
29afecdf RB |
1503 | SLP_INSTANCE_UNROLLING_FACTOR (slp_instn), slp_instn, true, |
1504 | &n_perms)) | |
01d8bf07 | 1505 | return false; |
9b999e8c | 1506 | |
01d8bf07 | 1507 | return true; |
ebfd146a IR |
1508 | } |
1509 | ||
1510 | ||
e4a707c4 | 1511 | /* Find the last store in SLP INSTANCE. */ |
ff802fa1 | 1512 | |
64900538 | 1513 | gimple * |
2e8ab70c | 1514 | vect_find_last_scalar_stmt_in_slp (slp_tree node) |
e4a707c4 | 1515 | { |
355fe088 | 1516 | gimple *last = NULL, *stmt; |
e4a707c4 | 1517 | |
2e8ab70c RB |
1518 | for (int i = 0; SLP_TREE_SCALAR_STMTS (node).iterate (i, &stmt); i++) |
1519 | { | |
1520 | stmt_vec_info stmt_vinfo = vinfo_for_stmt (stmt); | |
1521 | if (is_pattern_stmt_p (stmt_vinfo)) | |
1522 | last = get_later_stmt (STMT_VINFO_RELATED_STMT (stmt_vinfo), last); | |
1523 | else | |
1524 | last = get_later_stmt (stmt, last); | |
1525 | } | |
e4a707c4 | 1526 | |
2e8ab70c | 1527 | return last; |
e4a707c4 IR |
1528 | } |
1529 | ||
23847df4 RB |
1530 | /* Compute the cost for the SLP node NODE in the SLP instance INSTANCE. */ |
1531 | ||
1532 | static void | |
1a4b99c1 | 1533 | vect_analyze_slp_cost_1 (slp_instance instance, slp_tree node, |
23847df4 | 1534 | stmt_vector_for_cost *prologue_cost_vec, |
1a4b99c1 | 1535 | stmt_vector_for_cost *body_cost_vec, |
23847df4 RB |
1536 | unsigned ncopies_for_cost) |
1537 | { | |
603cca93 | 1538 | unsigned i, j; |
23847df4 | 1539 | slp_tree child; |
8155f4d8 | 1540 | gimple *stmt; |
23847df4 RB |
1541 | stmt_vec_info stmt_info; |
1542 | tree lhs; | |
23847df4 RB |
1543 | |
1544 | /* Recurse down the SLP tree. */ | |
1545 | FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child) | |
603cca93 | 1546 | if (SLP_TREE_DEF_TYPE (child) == vect_internal_def) |
1a4b99c1 RB |
1547 | vect_analyze_slp_cost_1 (instance, child, prologue_cost_vec, |
1548 | body_cost_vec, ncopies_for_cost); | |
23847df4 RB |
1549 | |
1550 | /* Look at the first scalar stmt to determine the cost. */ | |
1551 | stmt = SLP_TREE_SCALAR_STMTS (node)[0]; | |
1552 | stmt_info = vinfo_for_stmt (stmt); | |
1553 | if (STMT_VINFO_GROUPED_ACCESS (stmt_info)) | |
1554 | { | |
2de001ee RS |
1555 | vect_memory_access_type memory_access_type |
1556 | = (STMT_VINFO_STRIDED_P (stmt_info) | |
1557 | ? VMAT_STRIDED_SLP | |
1558 | : VMAT_CONTIGUOUS); | |
23847df4 | 1559 | if (DR_IS_WRITE (STMT_VINFO_DATA_REF (stmt_info))) |
2de001ee RS |
1560 | vect_model_store_cost (stmt_info, ncopies_for_cost, |
1561 | memory_access_type, vect_uninitialized_def, | |
23847df4 RB |
1562 | node, prologue_cost_vec, body_cost_vec); |
1563 | else | |
1564 | { | |
23847df4 | 1565 | gcc_checking_assert (DR_IS_READ (STMT_VINFO_DATA_REF (stmt_info))); |
52eab378 RB |
1566 | if (SLP_TREE_LOAD_PERMUTATION (node).exists ()) |
1567 | { | |
8155f4d8 RB |
1568 | /* If the load is permuted then the alignment is determined by |
1569 | the first group element not by the first scalar stmt DR. */ | |
52eab378 RB |
1570 | stmt = GROUP_FIRST_ELEMENT (stmt_info); |
1571 | stmt_info = vinfo_for_stmt (stmt); | |
8155f4d8 | 1572 | /* Record the cost for the permutation. */ |
29afecdf RB |
1573 | unsigned n_perms; |
1574 | vect_transform_slp_perm_load (node, vNULL, NULL, | |
1575 | ncopies_for_cost, instance, true, | |
1576 | &n_perms); | |
1577 | record_stmt_cost (body_cost_vec, n_perms, vec_perm, | |
8155f4d8 | 1578 | stmt_info, 0, vect_body); |
8155f4d8 RB |
1579 | unsigned nunits |
1580 | = TYPE_VECTOR_SUBPARTS (STMT_VINFO_VECTYPE (stmt_info)); | |
29afecdf RB |
1581 | /* And adjust the number of loads performed. This handles |
1582 | redundancies as well as loads that are later dead. */ | |
1583 | auto_sbitmap perm (GROUP_SIZE (stmt_info)); | |
1584 | bitmap_clear (perm); | |
1585 | for (i = 0; i < SLP_TREE_LOAD_PERMUTATION (node).length (); ++i) | |
1586 | bitmap_set_bit (perm, SLP_TREE_LOAD_PERMUTATION (node)[i]); | |
1587 | ncopies_for_cost = 0; | |
1588 | bool load_seen = false; | |
1589 | for (i = 0; i < GROUP_SIZE (stmt_info); ++i) | |
1590 | { | |
1591 | if (i % nunits == 0) | |
1592 | { | |
1593 | if (load_seen) | |
1594 | ncopies_for_cost++; | |
1595 | load_seen = false; | |
1596 | } | |
1597 | if (bitmap_bit_p (perm, i)) | |
1598 | load_seen = true; | |
1599 | } | |
1600 | if (load_seen) | |
1601 | ncopies_for_cost++; | |
1602 | gcc_assert (ncopies_for_cost | |
1603 | <= (GROUP_SIZE (stmt_info) - GROUP_GAP (stmt_info) | |
1604 | + nunits - 1) / nunits); | |
8155f4d8 | 1605 | ncopies_for_cost *= SLP_INSTANCE_UNROLLING_FACTOR (instance); |
52eab378 | 1606 | } |
8155f4d8 | 1607 | /* Record the cost for the vector loads. */ |
2de001ee RS |
1608 | vect_model_load_cost (stmt_info, ncopies_for_cost, |
1609 | memory_access_type, node, prologue_cost_vec, | |
1610 | body_cost_vec); | |
89483f99 | 1611 | return; |
23847df4 RB |
1612 | } |
1613 | } | |
89483f99 | 1614 | else |
6876e5bc RB |
1615 | { |
1616 | record_stmt_cost (body_cost_vec, ncopies_for_cost, vector_stmt, | |
1617 | stmt_info, 0, vect_body); | |
89483f99 RB |
1618 | if (SLP_TREE_TWO_OPERATORS (node)) |
1619 | { | |
1620 | record_stmt_cost (body_cost_vec, ncopies_for_cost, vector_stmt, | |
1621 | stmt_info, 0, vect_body); | |
1622 | record_stmt_cost (body_cost_vec, ncopies_for_cost, vec_perm, | |
1623 | stmt_info, 0, vect_body); | |
1624 | } | |
6876e5bc | 1625 | } |
23847df4 | 1626 | |
603cca93 RB |
1627 | /* Push SLP node def-type to stmts. */ |
1628 | FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child) | |
1629 | if (SLP_TREE_DEF_TYPE (child) != vect_internal_def) | |
1630 | FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (child), j, stmt) | |
1631 | STMT_VINFO_DEF_TYPE (vinfo_for_stmt (stmt)) = SLP_TREE_DEF_TYPE (child); | |
1632 | ||
23847df4 RB |
1633 | /* Scan operands and account for prologue cost of constants/externals. |
1634 | ??? This over-estimates cost for multiple uses and should be | |
1635 | re-engineered. */ | |
603cca93 | 1636 | stmt = SLP_TREE_SCALAR_STMTS (node)[0]; |
23847df4 RB |
1637 | lhs = gimple_get_lhs (stmt); |
1638 | for (i = 0; i < gimple_num_ops (stmt); ++i) | |
1639 | { | |
81c40241 | 1640 | tree op = gimple_op (stmt, i); |
355fe088 | 1641 | gimple *def_stmt; |
23847df4 RB |
1642 | enum vect_def_type dt; |
1643 | if (!op || op == lhs) | |
1644 | continue; | |
81c40241 | 1645 | if (vect_is_simple_use (op, stmt_info->vinfo, &def_stmt, &dt)) |
2e8ab70c RB |
1646 | { |
1647 | /* Without looking at the actual initializer a vector of | |
1648 | constants can be implemented as load from the constant pool. | |
1649 | ??? We need to pass down stmt_info for a vector type | |
1650 | even if it points to the wrong stmt. */ | |
1651 | if (dt == vect_constant_def) | |
1652 | record_stmt_cost (prologue_cost_vec, 1, vector_load, | |
1653 | stmt_info, 0, vect_prologue); | |
1654 | else if (dt == vect_external_def) | |
1655 | record_stmt_cost (prologue_cost_vec, 1, vec_construct, | |
1656 | stmt_info, 0, vect_prologue); | |
1657 | } | |
23847df4 | 1658 | } |
603cca93 RB |
1659 | |
1660 | /* Restore stmt def-types. */ | |
1661 | FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child) | |
1662 | if (SLP_TREE_DEF_TYPE (child) != vect_internal_def) | |
1663 | FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (child), j, stmt) | |
1664 | STMT_VINFO_DEF_TYPE (vinfo_for_stmt (stmt)) = vect_internal_def; | |
23847df4 RB |
1665 | } |
1666 | ||
1667 | /* Compute the cost for the SLP instance INSTANCE. */ | |
1668 | ||
1669 | static void | |
1a4b99c1 | 1670 | vect_analyze_slp_cost (slp_instance instance, void *data) |
23847df4 RB |
1671 | { |
1672 | stmt_vector_for_cost body_cost_vec, prologue_cost_vec; | |
1673 | unsigned ncopies_for_cost; | |
1674 | stmt_info_for_cost *si; | |
1675 | unsigned i; | |
1676 | ||
b939ea86 RB |
1677 | if (dump_enabled_p ()) |
1678 | dump_printf_loc (MSG_NOTE, vect_location, | |
1679 | "=== vect_analyze_slp_cost ===\n"); | |
1680 | ||
23847df4 RB |
1681 | /* Calculate the number of vector stmts to create based on the unrolling |
1682 | factor (number of vectors is 1 if NUNITS >= GROUP_SIZE, and is | |
1683 | GROUP_SIZE / NUNITS otherwise. */ | |
1684 | unsigned group_size = SLP_INSTANCE_GROUP_SIZE (instance); | |
1a4b99c1 RB |
1685 | slp_tree node = SLP_INSTANCE_TREE (instance); |
1686 | stmt_vec_info stmt_info = vinfo_for_stmt (SLP_TREE_SCALAR_STMTS (node)[0]); | |
1687 | /* Adjust the group_size by the vectorization factor which is always one | |
1688 | for basic-block vectorization. */ | |
1689 | if (STMT_VINFO_LOOP_VINFO (stmt_info)) | |
1690 | group_size *= LOOP_VINFO_VECT_FACTOR (STMT_VINFO_LOOP_VINFO (stmt_info)); | |
1691 | unsigned nunits = TYPE_VECTOR_SUBPARTS (STMT_VINFO_VECTYPE (stmt_info)); | |
1692 | /* For reductions look at a reduction operand in case the reduction | |
1693 | operation is widening like DOT_PROD or SAD. */ | |
1694 | if (!STMT_VINFO_GROUPED_ACCESS (stmt_info)) | |
1695 | { | |
355fe088 | 1696 | gimple *stmt = SLP_TREE_SCALAR_STMTS (node)[0]; |
1a4b99c1 RB |
1697 | switch (gimple_assign_rhs_code (stmt)) |
1698 | { | |
1699 | case DOT_PROD_EXPR: | |
1700 | case SAD_EXPR: | |
1701 | nunits = TYPE_VECTOR_SUBPARTS (get_vectype_for_scalar_type | |
1702 | (TREE_TYPE (gimple_assign_rhs1 (stmt)))); | |
1703 | break; | |
1704 | default:; | |
1705 | } | |
1706 | } | |
23847df4 RB |
1707 | ncopies_for_cost = least_common_multiple (nunits, group_size) / nunits; |
1708 | ||
1709 | prologue_cost_vec.create (10); | |
1710 | body_cost_vec.create (10); | |
1a4b99c1 RB |
1711 | vect_analyze_slp_cost_1 (instance, SLP_INSTANCE_TREE (instance), |
1712 | &prologue_cost_vec, &body_cost_vec, | |
1713 | ncopies_for_cost); | |
23847df4 RB |
1714 | |
1715 | /* Record the prologue costs, which were delayed until we were | |
1a4b99c1 | 1716 | sure that SLP was successful. */ |
23847df4 RB |
1717 | FOR_EACH_VEC_ELT (prologue_cost_vec, i, si) |
1718 | { | |
1719 | struct _stmt_vec_info *stmt_info | |
1720 | = si->stmt ? vinfo_for_stmt (si->stmt) : NULL; | |
1721 | (void) add_stmt_cost (data, si->count, si->kind, stmt_info, | |
1722 | si->misalign, vect_prologue); | |
1723 | } | |
1724 | ||
1a4b99c1 RB |
1725 | /* Record the instance's instructions in the target cost model. */ |
1726 | FOR_EACH_VEC_ELT (body_cost_vec, i, si) | |
1727 | { | |
1728 | struct _stmt_vec_info *stmt_info | |
1729 | = si->stmt ? vinfo_for_stmt (si->stmt) : NULL; | |
1730 | (void) add_stmt_cost (data, si->count, si->kind, stmt_info, | |
1731 | si->misalign, vect_body); | |
1732 | } | |
1733 | ||
23847df4 | 1734 | prologue_cost_vec.release (); |
1a4b99c1 | 1735 | body_cost_vec.release (); |
23847df4 | 1736 | } |
e4a707c4 | 1737 | |
1ba91a49 AL |
1738 | /* Splits a group of stores, currently beginning at FIRST_STMT, into two groups: |
1739 | one (still beginning at FIRST_STMT) of size GROUP1_SIZE (also containing | |
1740 | the first GROUP1_SIZE stmts, since stores are consecutive), the second | |
1741 | containing the remainder. | |
1742 | Return the first stmt in the second group. */ | |
1743 | ||
1744 | static gimple * | |
1745 | vect_split_slp_store_group (gimple *first_stmt, unsigned group1_size) | |
1746 | { | |
1747 | stmt_vec_info first_vinfo = vinfo_for_stmt (first_stmt); | |
1748 | gcc_assert (GROUP_FIRST_ELEMENT (first_vinfo) == first_stmt); | |
1749 | gcc_assert (group1_size > 0); | |
1750 | int group2_size = GROUP_SIZE (first_vinfo) - group1_size; | |
1751 | gcc_assert (group2_size > 0); | |
1752 | GROUP_SIZE (first_vinfo) = group1_size; | |
1753 | ||
1754 | gimple *stmt = first_stmt; | |
1755 | for (unsigned i = group1_size; i > 1; i--) | |
1756 | { | |
1757 | stmt = GROUP_NEXT_ELEMENT (vinfo_for_stmt (stmt)); | |
1758 | gcc_assert (GROUP_GAP (vinfo_for_stmt (stmt)) == 1); | |
1759 | } | |
1760 | /* STMT is now the last element of the first group. */ | |
1761 | gimple *group2 = GROUP_NEXT_ELEMENT (vinfo_for_stmt (stmt)); | |
1762 | GROUP_NEXT_ELEMENT (vinfo_for_stmt (stmt)) = 0; | |
1763 | ||
1764 | GROUP_SIZE (vinfo_for_stmt (group2)) = group2_size; | |
1765 | for (stmt = group2; stmt; stmt = GROUP_NEXT_ELEMENT (vinfo_for_stmt (stmt))) | |
1766 | { | |
1767 | GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)) = group2; | |
1768 | gcc_assert (GROUP_GAP (vinfo_for_stmt (stmt)) == 1); | |
1769 | } | |
1770 | ||
1771 | /* For the second group, the GROUP_GAP is that before the original group, | |
1772 | plus skipping over the first vector. */ | |
1773 | GROUP_GAP (vinfo_for_stmt (group2)) = | |
1774 | GROUP_GAP (first_vinfo) + group1_size; | |
1775 | ||
1776 | /* GROUP_GAP of the first group now has to skip over the second group too. */ | |
1777 | GROUP_GAP (first_vinfo) += group2_size; | |
1778 | ||
1779 | if (dump_enabled_p ()) | |
1780 | dump_printf_loc (MSG_NOTE, vect_location, "Split group into %d and %d\n", | |
1781 | group1_size, group2_size); | |
1782 | ||
1783 | return group2; | |
1784 | } | |
1785 | ||
0d0293ac | 1786 | /* Analyze an SLP instance starting from a group of grouped stores. Call |
b8698a0f | 1787 | vect_build_slp_tree to build a tree of packed stmts if possible. |
ebfd146a IR |
1788 | Return FALSE if it's impossible to SLP any stmt in the loop. */ |
1789 | ||
1790 | static bool | |
310213d4 | 1791 | vect_analyze_slp_instance (vec_info *vinfo, |
355fe088 | 1792 | gimple *stmt, unsigned max_tree_size) |
ebfd146a IR |
1793 | { |
1794 | slp_instance new_instance; | |
d092494c | 1795 | slp_tree node; |
e14c1050 | 1796 | unsigned int group_size = GROUP_SIZE (vinfo_for_stmt (stmt)); |
ebfd146a | 1797 | unsigned int unrolling_factor = 1, nunits; |
b5aeb3bb | 1798 | tree vectype, scalar_type = NULL_TREE; |
355fe088 | 1799 | gimple *next; |
1ba91a49 | 1800 | unsigned int i; |
ebfd146a | 1801 | unsigned int max_nunits = 0; |
9771b263 | 1802 | vec<slp_tree> loads; |
b5aeb3bb | 1803 | struct data_reference *dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (stmt)); |
355fe088 | 1804 | vec<gimple *> scalar_stmts; |
b5aeb3bb | 1805 | |
b010117a | 1806 | if (GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt))) |
b5aeb3bb | 1807 | { |
b010117a IR |
1808 | if (dr) |
1809 | { | |
1810 | scalar_type = TREE_TYPE (DR_REF (dr)); | |
1811 | vectype = get_vectype_for_scalar_type (scalar_type); | |
1812 | } | |
1813 | else | |
1814 | { | |
310213d4 | 1815 | gcc_assert (is_a <loop_vec_info> (vinfo)); |
b010117a IR |
1816 | vectype = STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt)); |
1817 | } | |
1818 | ||
e14c1050 | 1819 | group_size = GROUP_SIZE (vinfo_for_stmt (stmt)); |
b5aeb3bb IR |
1820 | } |
1821 | else | |
1822 | { | |
310213d4 | 1823 | gcc_assert (is_a <loop_vec_info> (vinfo)); |
b5aeb3bb | 1824 | vectype = STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt)); |
310213d4 | 1825 | group_size = as_a <loop_vec_info> (vinfo)->reductions.length (); |
b5aeb3bb | 1826 | } |
b8698a0f | 1827 | |
ebfd146a IR |
1828 | if (!vectype) |
1829 | { | |
73fbfcad | 1830 | if (dump_enabled_p ()) |
ebfd146a | 1831 | { |
78c60e3d SS |
1832 | dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location, |
1833 | "Build SLP failed: unsupported data-type "); | |
1834 | dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, scalar_type); | |
e645e942 | 1835 | dump_printf (MSG_MISSED_OPTIMIZATION, "\n"); |
ebfd146a | 1836 | } |
b5aeb3bb | 1837 | |
ebfd146a IR |
1838 | return false; |
1839 | } | |
ebfd146a | 1840 | nunits = TYPE_VECTOR_SUBPARTS (vectype); |
a70d6342 | 1841 | |
0d0293ac | 1842 | /* Create a node (a root of the SLP tree) for the packed grouped stores. */ |
9771b263 | 1843 | scalar_stmts.create (group_size); |
ebfd146a | 1844 | next = stmt; |
b010117a | 1845 | if (GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt))) |
ebfd146a | 1846 | { |
b5aeb3bb IR |
1847 | /* Collect the stores and store them in SLP_TREE_SCALAR_STMTS. */ |
1848 | while (next) | |
1849 | { | |
f7e531cf IR |
1850 | if (STMT_VINFO_IN_PATTERN_P (vinfo_for_stmt (next)) |
1851 | && STMT_VINFO_RELATED_STMT (vinfo_for_stmt (next))) | |
9771b263 DN |
1852 | scalar_stmts.safe_push ( |
1853 | STMT_VINFO_RELATED_STMT (vinfo_for_stmt (next))); | |
f7e531cf | 1854 | else |
9771b263 | 1855 | scalar_stmts.safe_push (next); |
e14c1050 | 1856 | next = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next)); |
b5aeb3bb | 1857 | } |
14a61437 RB |
1858 | /* Mark the first element of the reduction chain as reduction to properly |
1859 | transform the node. In the reduction analysis phase only the last | |
1860 | element of the chain is marked as reduction. */ | |
1861 | if (!STMT_VINFO_GROUPED_ACCESS (vinfo_for_stmt (stmt))) | |
1862 | STMT_VINFO_DEF_TYPE (vinfo_for_stmt (stmt)) = vect_reduction_def; | |
b5aeb3bb IR |
1863 | } |
1864 | else | |
1865 | { | |
1866 | /* Collect reduction statements. */ | |
310213d4 | 1867 | vec<gimple *> reductions = as_a <loop_vec_info> (vinfo)->reductions; |
9771b263 DN |
1868 | for (i = 0; reductions.iterate (i, &next); i++) |
1869 | scalar_stmts.safe_push (next); | |
ebfd146a IR |
1870 | } |
1871 | ||
9771b263 | 1872 | loads.create (group_size); |
ebfd146a IR |
1873 | |
1874 | /* Build the tree for the SLP instance. */ | |
89d390e5 RB |
1875 | bool *matches = XALLOCAVEC (bool, group_size); |
1876 | unsigned npermutes = 0; | |
e569db5f | 1877 | node = vect_build_slp_tree (vinfo, scalar_stmts, group_size, |
e403d17e | 1878 | &max_nunits, &loads, matches, &npermutes, |
e569db5f VK |
1879 | NULL, max_tree_size); |
1880 | if (node != NULL) | |
ebfd146a | 1881 | { |
4ef69dfc | 1882 | /* Calculate the unrolling factor based on the smallest type. */ |
e569db5f VK |
1883 | unrolling_factor |
1884 | = least_common_multiple (max_nunits, group_size) / group_size; | |
b8698a0f | 1885 | |
e569db5f VK |
1886 | if (unrolling_factor != 1 |
1887 | && is_a <bb_vec_info> (vinfo)) | |
1888 | { | |
1889 | ||
1890 | if (max_nunits > group_size) | |
4ef69dfc | 1891 | { |
e645e942 | 1892 | dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location, |
e569db5f VK |
1893 | "Build SLP failed: store group " |
1894 | "size not a multiple of the vector size " | |
1895 | "in basic block SLP\n"); | |
c7e62a26 | 1896 | vect_free_slp_tree (node); |
9771b263 | 1897 | loads.release (); |
4ef69dfc IR |
1898 | return false; |
1899 | } | |
e569db5f VK |
1900 | /* Fatal mismatch. */ |
1901 | matches[group_size/max_nunits * max_nunits] = false; | |
1902 | vect_free_slp_tree (node); | |
1903 | loads.release (); | |
1904 | } | |
1905 | else | |
1906 | { | |
4ef69dfc IR |
1907 | /* Create a new SLP instance. */ |
1908 | new_instance = XNEW (struct _slp_instance); | |
1909 | SLP_INSTANCE_TREE (new_instance) = node; | |
1910 | SLP_INSTANCE_GROUP_SIZE (new_instance) = group_size; | |
ebfd146a | 1911 | SLP_INSTANCE_UNROLLING_FACTOR (new_instance) = unrolling_factor; |
ebfd146a | 1912 | SLP_INSTANCE_LOADS (new_instance) = loads; |
abf9bfbc RB |
1913 | |
1914 | /* Compute the load permutation. */ | |
1915 | slp_tree load_node; | |
1916 | bool loads_permuted = false; | |
abf9bfbc RB |
1917 | FOR_EACH_VEC_ELT (loads, i, load_node) |
1918 | { | |
01d8bf07 | 1919 | vec<unsigned> load_permutation; |
abf9bfbc | 1920 | int j; |
355fe088 | 1921 | gimple *load, *first_stmt; |
01d8bf07 RB |
1922 | bool this_load_permuted = false; |
1923 | load_permutation.create (group_size); | |
6983e6b5 RB |
1924 | first_stmt = GROUP_FIRST_ELEMENT |
1925 | (vinfo_for_stmt (SLP_TREE_SCALAR_STMTS (load_node)[0])); | |
abf9bfbc RB |
1926 | FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (load_node), j, load) |
1927 | { | |
e569db5f VK |
1928 | int load_place = vect_get_place_in_interleaving_chain |
1929 | (load, first_stmt); | |
6983e6b5 RB |
1930 | gcc_assert (load_place != -1); |
1931 | if (load_place != j) | |
01d8bf07 | 1932 | this_load_permuted = true; |
abf9bfbc RB |
1933 | load_permutation.safe_push (load_place); |
1934 | } | |
fe2bef71 RB |
1935 | if (!this_load_permuted |
1936 | /* The load requires permutation when unrolling exposes | |
1937 | a gap either because the group is larger than the SLP | |
1938 | group-size or because there is a gap between the groups. */ | |
1939 | && (unrolling_factor == 1 | |
1940 | || (group_size == GROUP_SIZE (vinfo_for_stmt (first_stmt)) | |
1941 | && GROUP_GAP (vinfo_for_stmt (first_stmt)) == 0))) | |
01d8bf07 RB |
1942 | { |
1943 | load_permutation.release (); | |
1944 | continue; | |
1945 | } | |
1946 | SLP_TREE_LOAD_PERMUTATION (load_node) = load_permutation; | |
1947 | loads_permuted = true; | |
abf9bfbc | 1948 | } |
6aa904c4 IR |
1949 | |
1950 | if (loads_permuted) | |
ebfd146a | 1951 | { |
01d8bf07 | 1952 | if (!vect_supported_load_permutation_p (new_instance)) |
ebfd146a | 1953 | { |
73fbfcad | 1954 | if (dump_enabled_p ()) |
ebfd146a | 1955 | { |
e645e942 | 1956 | dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location, |
78c60e3d SS |
1957 | "Build SLP failed: unsupported load " |
1958 | "permutation "); | |
e569db5f VK |
1959 | dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, |
1960 | TDF_SLIM, stmt, 0); | |
ebfd146a | 1961 | } |
ebfd146a IR |
1962 | vect_free_slp_instance (new_instance); |
1963 | return false; | |
1964 | } | |
ebfd146a | 1965 | } |
ebfd146a | 1966 | |
e569db5f | 1967 | /* If the loads and stores can be handled with load/store-lan |
bb0f5ca7 AL |
1968 | instructions do not generate this SLP instance. */ |
1969 | if (is_a <loop_vec_info> (vinfo) | |
1970 | && loads_permuted | |
1971 | && dr && vect_store_lanes_supported (vectype, group_size)) | |
1972 | { | |
1973 | slp_tree load_node; | |
1974 | FOR_EACH_VEC_ELT (loads, i, load_node) | |
1975 | { | |
1976 | gimple *first_stmt = GROUP_FIRST_ELEMENT | |
1977 | (vinfo_for_stmt (SLP_TREE_SCALAR_STMTS (load_node)[0])); | |
1978 | stmt_vec_info stmt_vinfo = vinfo_for_stmt (first_stmt); | |
e569db5f VK |
1979 | /* Use SLP for strided accesses (or if we |
1980 | can't load-lanes). */ | |
bb0f5ca7 AL |
1981 | if (STMT_VINFO_STRIDED_P (stmt_vinfo) |
1982 | || ! vect_load_lanes_supported | |
1983 | (STMT_VINFO_VECTYPE (stmt_vinfo), | |
1984 | GROUP_SIZE (stmt_vinfo))) | |
1985 | break; | |
1986 | } | |
1987 | if (i == loads.length ()) | |
1988 | { | |
1989 | if (dump_enabled_p ()) | |
1990 | dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location, | |
1991 | "Built SLP cancelled: can use " | |
1992 | "load/store-lanes\n"); | |
1993 | vect_free_slp_instance (new_instance); | |
1994 | return false; | |
1995 | } | |
1996 | } | |
1997 | ||
310213d4 | 1998 | vinfo->slp_instances.safe_push (new_instance); |
b8698a0f | 1999 | |
73fbfcad | 2000 | if (dump_enabled_p ()) |
c2a12ca0 RB |
2001 | { |
2002 | dump_printf_loc (MSG_NOTE, vect_location, | |
2003 | "Final SLP tree for instance:\n"); | |
2004 | vect_print_slp_tree (MSG_NOTE, vect_location, node); | |
2005 | } | |
ebfd146a IR |
2006 | |
2007 | return true; | |
2008 | } | |
e569db5f VK |
2009 | } |
2010 | else | |
2011 | { | |
ebfd146a IR |
2012 | /* Failed to SLP. */ |
2013 | /* Free the allocated memory. */ | |
e403d17e | 2014 | scalar_stmts.release (); |
9771b263 | 2015 | loads.release (); |
e569db5f | 2016 | } |
b8698a0f | 2017 | |
1ba91a49 | 2018 | /* For basic block SLP, try to break the group up into multiples of the |
97a1a642 | 2019 | vector size. */ |
1ba91a49 AL |
2020 | if (is_a <bb_vec_info> (vinfo) |
2021 | && GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)) | |
2022 | && STMT_VINFO_GROUPED_ACCESS (vinfo_for_stmt (stmt))) | |
2023 | { | |
2024 | /* We consider breaking the group only on VF boundaries from the existing | |
2025 | start. */ | |
2026 | for (i = 0; i < group_size; i++) | |
2027 | if (!matches[i]) break; | |
2028 | ||
97a1a642 | 2029 | if (i >= nunits && i < group_size) |
1ba91a49 AL |
2030 | { |
2031 | /* Split into two groups at the first vector boundary before i. */ | |
97a1a642 RB |
2032 | gcc_assert ((nunits & (nunits - 1)) == 0); |
2033 | unsigned group1_size = i & ~(nunits - 1); | |
1ba91a49 AL |
2034 | |
2035 | gimple *rest = vect_split_slp_store_group (stmt, group1_size); | |
2036 | bool res = vect_analyze_slp_instance (vinfo, stmt, max_tree_size); | |
2037 | /* If the first non-match was in the middle of a vector, | |
2038 | skip the rest of that vector. */ | |
2039 | if (group1_size < i) | |
2040 | { | |
97a1a642 | 2041 | i = group1_size + nunits; |
1ba91a49 | 2042 | if (i < group_size) |
97a1a642 | 2043 | rest = vect_split_slp_store_group (rest, nunits); |
1ba91a49 AL |
2044 | } |
2045 | if (i < group_size) | |
2046 | res |= vect_analyze_slp_instance (vinfo, rest, max_tree_size); | |
2047 | return res; | |
2048 | } | |
2049 | /* Even though the first vector did not all match, we might be able to SLP | |
2050 | (some) of the remainder. FORNOW ignore this possibility. */ | |
2051 | } | |
2052 | ||
a70d6342 | 2053 | return false; |
ebfd146a IR |
2054 | } |
2055 | ||
2056 | ||
ff802fa1 | 2057 | /* Check if there are stmts in the loop can be vectorized using SLP. Build SLP |
ebfd146a IR |
2058 | trees of packed scalar stmts if SLP is possible. */ |
2059 | ||
2060 | bool | |
310213d4 | 2061 | vect_analyze_slp (vec_info *vinfo, unsigned max_tree_size) |
ebfd146a IR |
2062 | { |
2063 | unsigned int i; | |
355fe088 | 2064 | gimple *first_element; |
a70d6342 | 2065 | bool ok = false; |
ebfd146a | 2066 | |
73fbfcad | 2067 | if (dump_enabled_p ()) |
e645e942 | 2068 | dump_printf_loc (MSG_NOTE, vect_location, "=== vect_analyze_slp ===\n"); |
ebfd146a | 2069 | |
0d0293ac | 2070 | /* Find SLP sequences starting from groups of grouped stores. */ |
310213d4 RB |
2071 | FOR_EACH_VEC_ELT (vinfo->grouped_stores, i, first_element) |
2072 | if (vect_analyze_slp_instance (vinfo, first_element, max_tree_size)) | |
a70d6342 | 2073 | ok = true; |
ebfd146a | 2074 | |
310213d4 | 2075 | if (loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo)) |
b010117a | 2076 | { |
310213d4 RB |
2077 | if (loop_vinfo->reduction_chains.length () > 0) |
2078 | { | |
2079 | /* Find SLP sequences starting from reduction chains. */ | |
2080 | FOR_EACH_VEC_ELT (loop_vinfo->reduction_chains, i, first_element) | |
2081 | if (vect_analyze_slp_instance (vinfo, first_element, | |
2082 | max_tree_size)) | |
2083 | ok = true; | |
2084 | else | |
2085 | return false; | |
b010117a | 2086 | |
310213d4 RB |
2087 | /* Don't try to vectorize SLP reductions if reduction chain was |
2088 | detected. */ | |
2089 | return ok; | |
2090 | } | |
b010117a | 2091 | |
310213d4 RB |
2092 | /* Find SLP sequences starting from groups of reductions. */ |
2093 | if (loop_vinfo->reductions.length () > 1 | |
2094 | && vect_analyze_slp_instance (vinfo, loop_vinfo->reductions[0], | |
2095 | max_tree_size)) | |
2096 | ok = true; | |
2097 | } | |
b5aeb3bb | 2098 | |
ebfd146a IR |
2099 | return true; |
2100 | } | |
2101 | ||
2102 | ||
2103 | /* For each possible SLP instance decide whether to SLP it and calculate overall | |
437f4a00 IR |
2104 | unrolling factor needed to SLP the loop. Return TRUE if decided to SLP at |
2105 | least one instance. */ | |
ebfd146a | 2106 | |
437f4a00 | 2107 | bool |
ebfd146a IR |
2108 | vect_make_slp_decision (loop_vec_info loop_vinfo) |
2109 | { | |
2110 | unsigned int i, unrolling_factor = 1; | |
9771b263 | 2111 | vec<slp_instance> slp_instances = LOOP_VINFO_SLP_INSTANCES (loop_vinfo); |
ebfd146a IR |
2112 | slp_instance instance; |
2113 | int decided_to_slp = 0; | |
2114 | ||
73fbfcad | 2115 | if (dump_enabled_p ()) |
e645e942 TJ |
2116 | dump_printf_loc (MSG_NOTE, vect_location, "=== vect_make_slp_decision ===" |
2117 | "\n"); | |
ebfd146a | 2118 | |
9771b263 | 2119 | FOR_EACH_VEC_ELT (slp_instances, i, instance) |
ebfd146a IR |
2120 | { |
2121 | /* FORNOW: SLP if you can. */ | |
2122 | if (unrolling_factor < SLP_INSTANCE_UNROLLING_FACTOR (instance)) | |
2123 | unrolling_factor = SLP_INSTANCE_UNROLLING_FACTOR (instance); | |
2124 | ||
ff802fa1 | 2125 | /* Mark all the stmts that belong to INSTANCE as PURE_SLP stmts. Later we |
b8698a0f | 2126 | call vect_detect_hybrid_slp () to find stmts that need hybrid SLP and |
ff802fa1 | 2127 | loop-based vectorization. Such stmts will be marked as HYBRID. */ |
ebfd146a IR |
2128 | vect_mark_slp_stmts (SLP_INSTANCE_TREE (instance), pure_slp, -1); |
2129 | decided_to_slp++; | |
2130 | } | |
2131 | ||
2132 | LOOP_VINFO_SLP_UNROLLING_FACTOR (loop_vinfo) = unrolling_factor; | |
2133 | ||
73fbfcad | 2134 | if (decided_to_slp && dump_enabled_p ()) |
ccb3ad87 | 2135 | dump_printf_loc (MSG_NOTE, vect_location, |
e645e942 | 2136 | "Decided to SLP %d instances. Unrolling factor %d\n", |
78c60e3d | 2137 | decided_to_slp, unrolling_factor); |
437f4a00 IR |
2138 | |
2139 | return (decided_to_slp > 0); | |
ebfd146a IR |
2140 | } |
2141 | ||
2142 | ||
2143 | /* Find stmts that must be both vectorized and SLPed (since they feed stmts that | |
ff802fa1 | 2144 | can't be SLPed) in the tree rooted at NODE. Mark such stmts as HYBRID. */ |
ebfd146a IR |
2145 | |
2146 | static void | |
642fce57 | 2147 | vect_detect_hybrid_slp_stmts (slp_tree node, unsigned i, slp_vect_type stype) |
ebfd146a | 2148 | { |
355fe088 | 2149 | gimple *stmt = SLP_TREE_SCALAR_STMTS (node)[i]; |
ebfd146a | 2150 | imm_use_iterator imm_iter; |
355fe088 | 2151 | gimple *use_stmt; |
642fce57 | 2152 | stmt_vec_info use_vinfo, stmt_vinfo = vinfo_for_stmt (stmt); |
d755c7ef | 2153 | slp_tree child; |
f2c74cc4 | 2154 | loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_vinfo); |
642fce57 RB |
2155 | struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo); |
2156 | int j; | |
2157 | ||
2158 | /* Propagate hybrid down the SLP tree. */ | |
2159 | if (stype == hybrid) | |
2160 | ; | |
2161 | else if (HYBRID_SLP_STMT (stmt_vinfo)) | |
2162 | stype = hybrid; | |
2163 | else | |
2164 | { | |
2165 | /* Check if a pure SLP stmt has uses in non-SLP stmts. */ | |
2166 | gcc_checking_assert (PURE_SLP_STMT (stmt_vinfo)); | |
2935d994 RB |
2167 | /* If we get a pattern stmt here we have to use the LHS of the |
2168 | original stmt for immediate uses. */ | |
2169 | if (! STMT_VINFO_IN_PATTERN_P (stmt_vinfo) | |
2170 | && STMT_VINFO_RELATED_STMT (stmt_vinfo)) | |
29764870 | 2171 | stmt = STMT_VINFO_RELATED_STMT (stmt_vinfo); |
642fce57 RB |
2172 | if (TREE_CODE (gimple_op (stmt, 0)) == SSA_NAME) |
2173 | FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, gimple_op (stmt, 0)) | |
29764870 RB |
2174 | { |
2175 | if (!flow_bb_inside_loop_p (loop, gimple_bb (use_stmt))) | |
2176 | continue; | |
2177 | use_vinfo = vinfo_for_stmt (use_stmt); | |
2178 | if (STMT_VINFO_IN_PATTERN_P (use_vinfo) | |
2179 | && STMT_VINFO_RELATED_STMT (use_vinfo)) | |
2180 | use_vinfo = vinfo_for_stmt (STMT_VINFO_RELATED_STMT (use_vinfo)); | |
2181 | if (!STMT_SLP_TYPE (use_vinfo) | |
2182 | && (STMT_VINFO_RELEVANT (use_vinfo) | |
2183 | || VECTORIZABLE_CYCLE_DEF (STMT_VINFO_DEF_TYPE (use_vinfo))) | |
2184 | && !(gimple_code (use_stmt) == GIMPLE_PHI | |
2185 | && STMT_VINFO_DEF_TYPE (use_vinfo) == vect_reduction_def)) | |
502f0263 RB |
2186 | { |
2187 | if (dump_enabled_p ()) | |
2188 | { | |
2189 | dump_printf_loc (MSG_NOTE, vect_location, "use of SLP " | |
2190 | "def in non-SLP stmt: "); | |
2191 | dump_gimple_stmt (MSG_NOTE, TDF_SLIM, use_stmt, 0); | |
2192 | } | |
2193 | stype = hybrid; | |
2194 | } | |
29764870 | 2195 | } |
642fce57 | 2196 | } |
ebfd146a | 2197 | |
502f0263 RB |
2198 | if (stype == hybrid |
2199 | && !HYBRID_SLP_STMT (stmt_vinfo)) | |
b1af7da6 RB |
2200 | { |
2201 | if (dump_enabled_p ()) | |
2202 | { | |
2203 | dump_printf_loc (MSG_NOTE, vect_location, "marking hybrid: "); | |
2204 | dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0); | |
2205 | } | |
2206 | STMT_SLP_TYPE (stmt_vinfo) = hybrid; | |
2207 | } | |
ebfd146a | 2208 | |
642fce57 | 2209 | FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), j, child) |
603cca93 | 2210 | if (SLP_TREE_DEF_TYPE (child) != vect_external_def) |
90dd6e3d | 2211 | vect_detect_hybrid_slp_stmts (child, i, stype); |
642fce57 | 2212 | } |
f2c74cc4 | 2213 | |
642fce57 | 2214 | /* Helpers for vect_detect_hybrid_slp walking pattern stmt uses. */ |
ebfd146a | 2215 | |
642fce57 RB |
2216 | static tree |
2217 | vect_detect_hybrid_slp_1 (tree *tp, int *, void *data) | |
2218 | { | |
2219 | walk_stmt_info *wi = (walk_stmt_info *)data; | |
2220 | struct loop *loopp = (struct loop *)wi->info; | |
2221 | ||
2222 | if (wi->is_lhs) | |
2223 | return NULL_TREE; | |
2224 | ||
2225 | if (TREE_CODE (*tp) == SSA_NAME | |
2226 | && !SSA_NAME_IS_DEFAULT_DEF (*tp)) | |
2227 | { | |
355fe088 | 2228 | gimple *def_stmt = SSA_NAME_DEF_STMT (*tp); |
642fce57 RB |
2229 | if (flow_bb_inside_loop_p (loopp, gimple_bb (def_stmt)) |
2230 | && PURE_SLP_STMT (vinfo_for_stmt (def_stmt))) | |
b1af7da6 RB |
2231 | { |
2232 | if (dump_enabled_p ()) | |
2233 | { | |
2234 | dump_printf_loc (MSG_NOTE, vect_location, "marking hybrid: "); | |
2235 | dump_gimple_stmt (MSG_NOTE, TDF_SLIM, def_stmt, 0); | |
2236 | } | |
2237 | STMT_SLP_TYPE (vinfo_for_stmt (def_stmt)) = hybrid; | |
2238 | } | |
642fce57 RB |
2239 | } |
2240 | ||
2241 | return NULL_TREE; | |
ebfd146a IR |
2242 | } |
2243 | ||
642fce57 RB |
2244 | static tree |
2245 | vect_detect_hybrid_slp_2 (gimple_stmt_iterator *gsi, bool *handled, | |
2246 | walk_stmt_info *) | |
2247 | { | |
2248 | /* If the stmt is in a SLP instance then this isn't a reason | |
2249 | to mark use definitions in other SLP instances as hybrid. */ | |
2250 | if (STMT_SLP_TYPE (vinfo_for_stmt (gsi_stmt (*gsi))) != loop_vect) | |
2251 | *handled = true; | |
2252 | return NULL_TREE; | |
2253 | } | |
ebfd146a IR |
2254 | |
2255 | /* Find stmts that must be both vectorized and SLPed. */ | |
2256 | ||
2257 | void | |
2258 | vect_detect_hybrid_slp (loop_vec_info loop_vinfo) | |
2259 | { | |
2260 | unsigned int i; | |
9771b263 | 2261 | vec<slp_instance> slp_instances = LOOP_VINFO_SLP_INSTANCES (loop_vinfo); |
ebfd146a IR |
2262 | slp_instance instance; |
2263 | ||
73fbfcad | 2264 | if (dump_enabled_p ()) |
e645e942 TJ |
2265 | dump_printf_loc (MSG_NOTE, vect_location, "=== vect_detect_hybrid_slp ===" |
2266 | "\n"); | |
ebfd146a | 2267 | |
642fce57 RB |
2268 | /* First walk all pattern stmt in the loop and mark defs of uses as |
2269 | hybrid because immediate uses in them are not recorded. */ | |
2270 | for (i = 0; i < LOOP_VINFO_LOOP (loop_vinfo)->num_nodes; ++i) | |
2271 | { | |
2272 | basic_block bb = LOOP_VINFO_BBS (loop_vinfo)[i]; | |
2273 | for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi); | |
2274 | gsi_next (&gsi)) | |
2275 | { | |
355fe088 | 2276 | gimple *stmt = gsi_stmt (gsi); |
642fce57 RB |
2277 | stmt_vec_info stmt_info = vinfo_for_stmt (stmt); |
2278 | if (STMT_VINFO_IN_PATTERN_P (stmt_info)) | |
2279 | { | |
2280 | walk_stmt_info wi; | |
2281 | memset (&wi, 0, sizeof (wi)); | |
2282 | wi.info = LOOP_VINFO_LOOP (loop_vinfo); | |
2283 | gimple_stmt_iterator gsi2 | |
2284 | = gsi_for_stmt (STMT_VINFO_RELATED_STMT (stmt_info)); | |
2285 | walk_gimple_stmt (&gsi2, vect_detect_hybrid_slp_2, | |
2286 | vect_detect_hybrid_slp_1, &wi); | |
2287 | walk_gimple_seq (STMT_VINFO_PATTERN_DEF_SEQ (stmt_info), | |
2288 | vect_detect_hybrid_slp_2, | |
2289 | vect_detect_hybrid_slp_1, &wi); | |
2290 | } | |
2291 | } | |
2292 | } | |
2293 | ||
2294 | /* Then walk the SLP instance trees marking stmts with uses in | |
2295 | non-SLP stmts as hybrid, also propagating hybrid down the | |
2296 | SLP tree, collecting the above info on-the-fly. */ | |
9771b263 | 2297 | FOR_EACH_VEC_ELT (slp_instances, i, instance) |
642fce57 RB |
2298 | { |
2299 | for (unsigned i = 0; i < SLP_INSTANCE_GROUP_SIZE (instance); ++i) | |
2300 | vect_detect_hybrid_slp_stmts (SLP_INSTANCE_TREE (instance), | |
2301 | i, pure_slp); | |
2302 | } | |
ebfd146a IR |
2303 | } |
2304 | ||
a70d6342 IR |
2305 | |
2306 | /* Create and initialize a new bb_vec_info struct for BB, as well as | |
2307 | stmt_vec_info structs for all the stmts in it. */ | |
b8698a0f | 2308 | |
a70d6342 | 2309 | static bb_vec_info |
61d371eb RB |
2310 | new_bb_vec_info (gimple_stmt_iterator region_begin, |
2311 | gimple_stmt_iterator region_end) | |
a70d6342 | 2312 | { |
61d371eb | 2313 | basic_block bb = gsi_bb (region_begin); |
a70d6342 IR |
2314 | bb_vec_info res = NULL; |
2315 | gimple_stmt_iterator gsi; | |
2316 | ||
2317 | res = (bb_vec_info) xcalloc (1, sizeof (struct _bb_vec_info)); | |
310213d4 | 2318 | res->kind = vec_info::bb; |
a70d6342 | 2319 | BB_VINFO_BB (res) = bb; |
61d371eb RB |
2320 | res->region_begin = region_begin; |
2321 | res->region_end = region_end; | |
a70d6342 | 2322 | |
61d371eb RB |
2323 | for (gsi = region_begin; gsi_stmt (gsi) != gsi_stmt (region_end); |
2324 | gsi_next (&gsi)) | |
a70d6342 | 2325 | { |
355fe088 | 2326 | gimple *stmt = gsi_stmt (gsi); |
a70d6342 | 2327 | gimple_set_uid (stmt, 0); |
310213d4 | 2328 | set_vinfo_for_stmt (stmt, new_stmt_vec_info (stmt, res)); |
a70d6342 IR |
2329 | } |
2330 | ||
9771b263 DN |
2331 | BB_VINFO_GROUPED_STORES (res).create (10); |
2332 | BB_VINFO_SLP_INSTANCES (res).create (2); | |
c3e7ee41 | 2333 | BB_VINFO_TARGET_COST_DATA (res) = init_cost (NULL); |
a70d6342 IR |
2334 | |
2335 | bb->aux = res; | |
2336 | return res; | |
2337 | } | |
2338 | ||
2339 | ||
2340 | /* Free BB_VINFO struct, as well as all the stmt_vec_info structs of all the | |
2341 | stmts in the basic block. */ | |
2342 | ||
2343 | static void | |
2344 | destroy_bb_vec_info (bb_vec_info bb_vinfo) | |
2345 | { | |
c7e62a26 | 2346 | slp_instance instance; |
c7e62a26 | 2347 | unsigned i; |
a70d6342 IR |
2348 | |
2349 | if (!bb_vinfo) | |
2350 | return; | |
2351 | ||
78810bd3 RB |
2352 | vect_destroy_datarefs (bb_vinfo); |
2353 | free_dependence_relations (BB_VINFO_DDRS (bb_vinfo)); | |
2354 | BB_VINFO_GROUPED_STORES (bb_vinfo).release (); | |
2355 | FOR_EACH_VEC_ELT (BB_VINFO_SLP_INSTANCES (bb_vinfo), i, instance) | |
2356 | vect_free_slp_instance (instance); | |
2357 | BB_VINFO_SLP_INSTANCES (bb_vinfo).release (); | |
2358 | destroy_cost_data (BB_VINFO_TARGET_COST_DATA (bb_vinfo)); | |
a70d6342 | 2359 | |
78810bd3 | 2360 | for (gimple_stmt_iterator si = bb_vinfo->region_begin; |
61d371eb | 2361 | gsi_stmt (si) != gsi_stmt (bb_vinfo->region_end); gsi_next (&si)) |
a70d6342 | 2362 | { |
355fe088 | 2363 | gimple *stmt = gsi_stmt (si); |
a70d6342 IR |
2364 | stmt_vec_info stmt_info = vinfo_for_stmt (stmt); |
2365 | ||
2366 | if (stmt_info) | |
2367 | /* Free stmt_vec_info. */ | |
2368 | free_stmt_vec_info (stmt); | |
61d371eb RB |
2369 | |
2370 | /* Reset region marker. */ | |
2371 | gimple_set_uid (stmt, -1); | |
a70d6342 IR |
2372 | } |
2373 | ||
78810bd3 | 2374 | BB_VINFO_BB (bb_vinfo)->aux = NULL; |
a70d6342 | 2375 | free (bb_vinfo); |
a70d6342 IR |
2376 | } |
2377 | ||
2378 | ||
2379 | /* Analyze statements contained in SLP tree node after recursively analyzing | |
2380 | the subtree. Return TRUE if the operations are supported. */ | |
2381 | ||
2382 | static bool | |
a12e42fc | 2383 | vect_slp_analyze_node_operations (slp_tree node) |
a70d6342 IR |
2384 | { |
2385 | bool dummy; | |
603cca93 | 2386 | int i, j; |
355fe088 | 2387 | gimple *stmt; |
d755c7ef | 2388 | slp_tree child; |
a70d6342 | 2389 | |
603cca93 | 2390 | if (SLP_TREE_DEF_TYPE (node) != vect_internal_def) |
a70d6342 IR |
2391 | return true; |
2392 | ||
9771b263 | 2393 | FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child) |
a12e42fc | 2394 | if (!vect_slp_analyze_node_operations (child)) |
d092494c | 2395 | return false; |
a70d6342 | 2396 | |
603cca93 | 2397 | bool res = true; |
9771b263 | 2398 | FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt) |
a70d6342 IR |
2399 | { |
2400 | stmt_vec_info stmt_info = vinfo_for_stmt (stmt); | |
2401 | gcc_assert (stmt_info); | |
a12e42fc | 2402 | gcc_assert (STMT_SLP_TYPE (stmt_info) != loop_vect); |
a70d6342 | 2403 | |
6379dfb5 RB |
2404 | /* Push SLP node def-type to stmt operands. */ |
2405 | FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), j, child) | |
2406 | if (SLP_TREE_DEF_TYPE (child) != vect_internal_def) | |
2407 | STMT_VINFO_DEF_TYPE (vinfo_for_stmt (SLP_TREE_SCALAR_STMTS (child)[i])) | |
2408 | = SLP_TREE_DEF_TYPE (child); | |
2409 | res = vect_analyze_stmt (stmt, &dummy, node); | |
2410 | /* Restore def-types. */ | |
2411 | FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), j, child) | |
2412 | if (SLP_TREE_DEF_TYPE (child) != vect_internal_def) | |
2413 | STMT_VINFO_DEF_TYPE (vinfo_for_stmt (SLP_TREE_SCALAR_STMTS (child)[i])) | |
2414 | = vect_internal_def; | |
2415 | if (! res) | |
2416 | break; | |
a70d6342 IR |
2417 | } |
2418 | ||
603cca93 | 2419 | return res; |
a70d6342 IR |
2420 | } |
2421 | ||
2422 | ||
ff802fa1 | 2423 | /* Analyze statements in SLP instances of the basic block. Return TRUE if the |
a70d6342 IR |
2424 | operations are supported. */ |
2425 | ||
a12e42fc | 2426 | bool |
1a4b99c1 | 2427 | vect_slp_analyze_operations (vec<slp_instance> slp_instances, void *data) |
a70d6342 | 2428 | { |
a70d6342 IR |
2429 | slp_instance instance; |
2430 | int i; | |
2431 | ||
a12e42fc RB |
2432 | if (dump_enabled_p ()) |
2433 | dump_printf_loc (MSG_NOTE, vect_location, | |
2434 | "=== vect_slp_analyze_operations ===\n"); | |
2435 | ||
9771b263 | 2436 | for (i = 0; slp_instances.iterate (i, &instance); ) |
a70d6342 | 2437 | { |
a12e42fc | 2438 | if (!vect_slp_analyze_node_operations (SLP_INSTANCE_TREE (instance))) |
a70d6342 | 2439 | { |
a12e42fc RB |
2440 | dump_printf_loc (MSG_NOTE, vect_location, |
2441 | "removing SLP instance operations starting from: "); | |
2442 | dump_gimple_stmt (MSG_NOTE, TDF_SLIM, | |
2443 | SLP_TREE_SCALAR_STMTS | |
2444 | (SLP_INSTANCE_TREE (instance))[0], 0); | |
2445 | vect_free_slp_instance (instance); | |
9771b263 | 2446 | slp_instances.ordered_remove (i); |
a70d6342 IR |
2447 | } |
2448 | else | |
1a4b99c1 RB |
2449 | { |
2450 | /* Compute the costs of the SLP instance. */ | |
2451 | vect_analyze_slp_cost (instance, data); | |
2452 | i++; | |
2453 | } | |
b8698a0f L |
2454 | } |
2455 | ||
9771b263 | 2456 | if (!slp_instances.length ()) |
a70d6342 IR |
2457 | return false; |
2458 | ||
2459 | return true; | |
2460 | } | |
2461 | ||
6eddf228 RB |
2462 | |
2463 | /* Compute the scalar cost of the SLP node NODE and its children | |
2464 | and return it. Do not account defs that are marked in LIFE and | |
2465 | update LIFE according to uses of NODE. */ | |
2466 | ||
2467 | static unsigned | |
292cba13 | 2468 | vect_bb_slp_scalar_cost (basic_block bb, |
ff4c81cc | 2469 | slp_tree node, vec<bool, va_heap> *life) |
6eddf228 RB |
2470 | { |
2471 | unsigned scalar_cost = 0; | |
2472 | unsigned i; | |
355fe088 | 2473 | gimple *stmt; |
6eddf228 RB |
2474 | slp_tree child; |
2475 | ||
2476 | FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt) | |
2477 | { | |
2478 | unsigned stmt_cost; | |
2479 | ssa_op_iter op_iter; | |
2480 | def_operand_p def_p; | |
2481 | stmt_vec_info stmt_info; | |
2482 | ||
ff4c81cc | 2483 | if ((*life)[i]) |
6eddf228 RB |
2484 | continue; |
2485 | ||
2486 | /* If there is a non-vectorized use of the defs then the scalar | |
2487 | stmt is kept live in which case we do not account it or any | |
2488 | required defs in the SLP children in the scalar cost. This | |
2489 | way we make the vectorization more costly when compared to | |
2490 | the scalar cost. */ | |
2491 | FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, op_iter, SSA_OP_DEF) | |
2492 | { | |
2493 | imm_use_iterator use_iter; | |
355fe088 | 2494 | gimple *use_stmt; |
6eddf228 | 2495 | FOR_EACH_IMM_USE_STMT (use_stmt, use_iter, DEF_FROM_PTR (def_p)) |
f30a0ba5 | 2496 | if (!is_gimple_debug (use_stmt) |
61d371eb RB |
2497 | && (! vect_stmt_in_region_p (vinfo_for_stmt (stmt)->vinfo, |
2498 | use_stmt) | |
603cca93 | 2499 | || ! PURE_SLP_STMT (vinfo_for_stmt (use_stmt)))) |
6eddf228 | 2500 | { |
ff4c81cc | 2501 | (*life)[i] = true; |
6eddf228 RB |
2502 | BREAK_FROM_IMM_USE_STMT (use_iter); |
2503 | } | |
2504 | } | |
ff4c81cc | 2505 | if ((*life)[i]) |
6eddf228 RB |
2506 | continue; |
2507 | ||
b555a2e4 RB |
2508 | /* Count scalar stmts only once. */ |
2509 | if (gimple_visited_p (stmt)) | |
2510 | continue; | |
2511 | gimple_set_visited (stmt, true); | |
2512 | ||
6eddf228 RB |
2513 | stmt_info = vinfo_for_stmt (stmt); |
2514 | if (STMT_VINFO_DATA_REF (stmt_info)) | |
2515 | { | |
2516 | if (DR_IS_READ (STMT_VINFO_DATA_REF (stmt_info))) | |
2517 | stmt_cost = vect_get_stmt_cost (scalar_load); | |
2518 | else | |
2519 | stmt_cost = vect_get_stmt_cost (scalar_store); | |
2520 | } | |
2521 | else | |
2522 | stmt_cost = vect_get_stmt_cost (scalar_stmt); | |
2523 | ||
2524 | scalar_cost += stmt_cost; | |
2525 | } | |
2526 | ||
2527 | FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child) | |
603cca93 | 2528 | if (SLP_TREE_DEF_TYPE (child) == vect_internal_def) |
90dd6e3d | 2529 | scalar_cost += vect_bb_slp_scalar_cost (bb, child, life); |
6eddf228 RB |
2530 | |
2531 | return scalar_cost; | |
2532 | } | |
2533 | ||
69f11a13 IR |
2534 | /* Check if vectorization of the basic block is profitable. */ |
2535 | ||
2536 | static bool | |
2537 | vect_bb_vectorization_profitable_p (bb_vec_info bb_vinfo) | |
2538 | { | |
9771b263 | 2539 | vec<slp_instance> slp_instances = BB_VINFO_SLP_INSTANCES (bb_vinfo); |
69f11a13 | 2540 | slp_instance instance; |
1a4b99c1 | 2541 | int i; |
c3e7ee41 | 2542 | unsigned int vec_inside_cost = 0, vec_outside_cost = 0, scalar_cost = 0; |
92345349 | 2543 | unsigned int vec_prologue_cost = 0, vec_epilogue_cost = 0; |
69f11a13 IR |
2544 | |
2545 | /* Calculate scalar cost. */ | |
6eddf228 | 2546 | FOR_EACH_VEC_ELT (slp_instances, i, instance) |
69f11a13 | 2547 | { |
00f96dc9 | 2548 | auto_vec<bool, 20> life; |
ff4c81cc | 2549 | life.safe_grow_cleared (SLP_INSTANCE_GROUP_SIZE (instance)); |
292cba13 RB |
2550 | scalar_cost += vect_bb_slp_scalar_cost (BB_VINFO_BB (bb_vinfo), |
2551 | SLP_INSTANCE_TREE (instance), | |
ff4c81cc | 2552 | &life); |
69f11a13 IR |
2553 | } |
2554 | ||
b555a2e4 RB |
2555 | /* Unset visited flag. */ |
2556 | for (gimple_stmt_iterator gsi = bb_vinfo->region_begin; | |
2557 | gsi_stmt (gsi) != gsi_stmt (bb_vinfo->region_end); gsi_next (&gsi)) | |
2558 | gimple_set_visited (gsi_stmt (gsi), false); | |
2559 | ||
c3e7ee41 | 2560 | /* Complete the target-specific cost calculation. */ |
92345349 BS |
2561 | finish_cost (BB_VINFO_TARGET_COST_DATA (bb_vinfo), &vec_prologue_cost, |
2562 | &vec_inside_cost, &vec_epilogue_cost); | |
2563 | ||
2564 | vec_outside_cost = vec_prologue_cost + vec_epilogue_cost; | |
c3e7ee41 | 2565 | |
73fbfcad | 2566 | if (dump_enabled_p ()) |
69f11a13 | 2567 | { |
78c60e3d SS |
2568 | dump_printf_loc (MSG_NOTE, vect_location, "Cost model analysis: \n"); |
2569 | dump_printf (MSG_NOTE, " Vector inside of basic block cost: %d\n", | |
2570 | vec_inside_cost); | |
2571 | dump_printf (MSG_NOTE, " Vector prologue cost: %d\n", vec_prologue_cost); | |
2572 | dump_printf (MSG_NOTE, " Vector epilogue cost: %d\n", vec_epilogue_cost); | |
e645e942 | 2573 | dump_printf (MSG_NOTE, " Scalar cost of basic block: %d\n", scalar_cost); |
69f11a13 IR |
2574 | } |
2575 | ||
a6524bba RB |
2576 | /* Vectorization is profitable if its cost is more than the cost of scalar |
2577 | version. Note that we err on the vector side for equal cost because | |
2578 | the cost estimate is otherwise quite pessimistic (constant uses are | |
2579 | free on the scalar side but cost a load on the vector side for | |
2580 | example). */ | |
2581 | if (vec_outside_cost + vec_inside_cost > scalar_cost) | |
69f11a13 IR |
2582 | return false; |
2583 | ||
2584 | return true; | |
2585 | } | |
2586 | ||
a5b50aa1 RB |
2587 | /* Check if the basic block can be vectorized. Returns a bb_vec_info |
2588 | if so and sets fatal to true if failure is independent of | |
2589 | current_vector_size. */ | |
a70d6342 | 2590 | |
8e19f5a1 | 2591 | static bb_vec_info |
61d371eb RB |
2592 | vect_slp_analyze_bb_1 (gimple_stmt_iterator region_begin, |
2593 | gimple_stmt_iterator region_end, | |
a5b50aa1 RB |
2594 | vec<data_reference_p> datarefs, int n_stmts, |
2595 | bool &fatal) | |
a70d6342 IR |
2596 | { |
2597 | bb_vec_info bb_vinfo; | |
a70d6342 | 2598 | slp_instance instance; |
8e19f5a1 | 2599 | int i; |
777e1f09 | 2600 | int min_vf = 2; |
e4a707c4 | 2601 | |
a5b50aa1 RB |
2602 | /* The first group of checks is independent of the vector size. */ |
2603 | fatal = true; | |
2604 | ||
61d371eb RB |
2605 | if (n_stmts > PARAM_VALUE (PARAM_SLP_MAX_INSNS_IN_BB)) |
2606 | { | |
2607 | if (dump_enabled_p ()) | |
2608 | dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location, | |
2609 | "not vectorized: too many instructions in " | |
2610 | "basic block.\n"); | |
2611 | free_data_refs (datarefs); | |
2612 | return NULL; | |
2613 | } | |
2614 | ||
2615 | bb_vinfo = new_bb_vec_info (region_begin, region_end); | |
a70d6342 IR |
2616 | if (!bb_vinfo) |
2617 | return NULL; | |
2618 | ||
61d371eb | 2619 | BB_VINFO_DATAREFS (bb_vinfo) = datarefs; |
428db0ba RB |
2620 | |
2621 | /* Analyze the data references. */ | |
2622 | ||
2623 | if (!vect_analyze_data_refs (bb_vinfo, &min_vf)) | |
a70d6342 | 2624 | { |
73fbfcad | 2625 | if (dump_enabled_p ()) |
78c60e3d SS |
2626 | dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location, |
2627 | "not vectorized: unhandled data-ref in basic " | |
2628 | "block.\n"); | |
b8698a0f | 2629 | |
a70d6342 IR |
2630 | destroy_bb_vec_info (bb_vinfo); |
2631 | return NULL; | |
2632 | } | |
2633 | ||
fcac74a1 | 2634 | if (BB_VINFO_DATAREFS (bb_vinfo).length () < 2) |
a70d6342 | 2635 | { |
73fbfcad | 2636 | if (dump_enabled_p ()) |
78c60e3d SS |
2637 | dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location, |
2638 | "not vectorized: not enough data-refs in " | |
2639 | "basic block.\n"); | |
a70d6342 IR |
2640 | |
2641 | destroy_bb_vec_info (bb_vinfo); | |
2642 | return NULL; | |
2643 | } | |
2644 | ||
310213d4 | 2645 | if (!vect_analyze_data_ref_accesses (bb_vinfo)) |
5abe1e05 RB |
2646 | { |
2647 | if (dump_enabled_p ()) | |
2648 | dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location, | |
2649 | "not vectorized: unhandled data access in " | |
2650 | "basic block.\n"); | |
2651 | ||
2652 | destroy_bb_vec_info (bb_vinfo); | |
2653 | return NULL; | |
2654 | } | |
2655 | ||
a5b50aa1 RB |
2656 | /* If there are no grouped stores in the region there is no need |
2657 | to continue with pattern recog as vect_analyze_slp will fail | |
2658 | anyway. */ | |
2659 | if (bb_vinfo->grouped_stores.is_empty ()) | |
a70d6342 | 2660 | { |
73fbfcad | 2661 | if (dump_enabled_p ()) |
a5b50aa1 RB |
2662 | dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location, |
2663 | "not vectorized: no grouped stores in " | |
2664 | "basic block.\n"); | |
b8698a0f | 2665 | |
a70d6342 IR |
2666 | destroy_bb_vec_info (bb_vinfo); |
2667 | return NULL; | |
2668 | } | |
b8698a0f | 2669 | |
a5b50aa1 RB |
2670 | /* While the rest of the analysis below depends on it in some way. */ |
2671 | fatal = false; | |
2672 | ||
2673 | vect_pattern_recog (bb_vinfo); | |
2674 | ||
a70d6342 IR |
2675 | /* Check the SLP opportunities in the basic block, analyze and build SLP |
2676 | trees. */ | |
310213d4 | 2677 | if (!vect_analyze_slp (bb_vinfo, n_stmts)) |
a70d6342 | 2678 | { |
73fbfcad | 2679 | if (dump_enabled_p ()) |
effb52da RB |
2680 | { |
2681 | dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location, | |
2682 | "Failed to SLP the basic block.\n"); | |
2683 | dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location, | |
2684 | "not vectorized: failed to find SLP opportunities " | |
2685 | "in basic block.\n"); | |
2686 | } | |
a70d6342 IR |
2687 | |
2688 | destroy_bb_vec_info (bb_vinfo); | |
2689 | return NULL; | |
2690 | } | |
b8698a0f | 2691 | |
c2a12ca0 RB |
2692 | /* Analyze and verify the alignment of data references and the |
2693 | dependence in the SLP instances. */ | |
a5b50aa1 RB |
2694 | for (i = 0; BB_VINFO_SLP_INSTANCES (bb_vinfo).iterate (i, &instance); ) |
2695 | { | |
c2a12ca0 RB |
2696 | if (! vect_slp_analyze_and_verify_instance_alignment (instance) |
2697 | || ! vect_slp_analyze_instance_dependence (instance)) | |
a5b50aa1 RB |
2698 | { |
2699 | dump_printf_loc (MSG_NOTE, vect_location, | |
2700 | "removing SLP instance operations starting from: "); | |
2701 | dump_gimple_stmt (MSG_NOTE, TDF_SLIM, | |
2702 | SLP_TREE_SCALAR_STMTS | |
2703 | (SLP_INSTANCE_TREE (instance))[0], 0); | |
2704 | vect_free_slp_instance (instance); | |
2705 | BB_VINFO_SLP_INSTANCES (bb_vinfo).ordered_remove (i); | |
2706 | continue; | |
2707 | } | |
c2a12ca0 RB |
2708 | |
2709 | /* Mark all the statements that we want to vectorize as pure SLP and | |
2710 | relevant. */ | |
2711 | vect_mark_slp_stmts (SLP_INSTANCE_TREE (instance), pure_slp, -1); | |
2712 | vect_mark_slp_stmts_relevant (SLP_INSTANCE_TREE (instance)); | |
2713 | ||
a5b50aa1 RB |
2714 | i++; |
2715 | } | |
a5b50aa1 RB |
2716 | if (! BB_VINFO_SLP_INSTANCES (bb_vinfo).length ()) |
2717 | { | |
2718 | destroy_bb_vec_info (bb_vinfo); | |
2719 | return NULL; | |
2720 | } | |
2721 | ||
1a4b99c1 RB |
2722 | if (!vect_slp_analyze_operations (BB_VINFO_SLP_INSTANCES (bb_vinfo), |
2723 | BB_VINFO_TARGET_COST_DATA (bb_vinfo))) | |
a70d6342 | 2724 | { |
73fbfcad | 2725 | if (dump_enabled_p ()) |
e645e942 | 2726 | dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location, |
78c60e3d | 2727 | "not vectorized: bad operation in basic block.\n"); |
a70d6342 IR |
2728 | |
2729 | destroy_bb_vec_info (bb_vinfo); | |
2730 | return NULL; | |
2731 | } | |
2732 | ||
69f11a13 | 2733 | /* Cost model: check if the vectorization is worthwhile. */ |
8b5e1202 | 2734 | if (!unlimited_cost_model (NULL) |
69f11a13 IR |
2735 | && !vect_bb_vectorization_profitable_p (bb_vinfo)) |
2736 | { | |
73fbfcad | 2737 | if (dump_enabled_p ()) |
78c60e3d SS |
2738 | dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location, |
2739 | "not vectorized: vectorization is not " | |
2740 | "profitable.\n"); | |
69f11a13 IR |
2741 | |
2742 | destroy_bb_vec_info (bb_vinfo); | |
2743 | return NULL; | |
2744 | } | |
2745 | ||
73fbfcad | 2746 | if (dump_enabled_p ()) |
78c60e3d SS |
2747 | dump_printf_loc (MSG_NOTE, vect_location, |
2748 | "Basic block will be vectorized using SLP\n"); | |
a70d6342 IR |
2749 | |
2750 | return bb_vinfo; | |
2751 | } | |
2752 | ||
2753 | ||
428db0ba RB |
2754 | /* Main entry for the BB vectorizer. Analyze and transform BB, returns |
2755 | true if anything in the basic-block was vectorized. */ | |
2756 | ||
2757 | bool | |
2758 | vect_slp_bb (basic_block bb) | |
8e19f5a1 IR |
2759 | { |
2760 | bb_vec_info bb_vinfo; | |
8e19f5a1 IR |
2761 | gimple_stmt_iterator gsi; |
2762 | unsigned int vector_sizes; | |
61d371eb | 2763 | bool any_vectorized = false; |
8e19f5a1 | 2764 | |
73fbfcad | 2765 | if (dump_enabled_p ()) |
78c60e3d | 2766 | dump_printf_loc (MSG_NOTE, vect_location, "===vect_slp_analyze_bb===\n"); |
8e19f5a1 | 2767 | |
8e19f5a1 IR |
2768 | /* Autodetect first vector size we try. */ |
2769 | current_vector_size = 0; | |
2770 | vector_sizes = targetm.vectorize.autovectorize_vector_sizes (); | |
2771 | ||
61d371eb RB |
2772 | gsi = gsi_start_bb (bb); |
2773 | ||
8e19f5a1 IR |
2774 | while (1) |
2775 | { | |
61d371eb RB |
2776 | if (gsi_end_p (gsi)) |
2777 | break; | |
2778 | ||
2779 | gimple_stmt_iterator region_begin = gsi; | |
2780 | vec<data_reference_p> datarefs = vNULL; | |
2781 | int insns = 0; | |
2782 | ||
2783 | for (; !gsi_end_p (gsi); gsi_next (&gsi)) | |
428db0ba | 2784 | { |
61d371eb RB |
2785 | gimple *stmt = gsi_stmt (gsi); |
2786 | if (is_gimple_debug (stmt)) | |
2787 | continue; | |
2788 | insns++; | |
2789 | ||
2790 | if (gimple_location (stmt) != UNKNOWN_LOCATION) | |
2791 | vect_location = gimple_location (stmt); | |
2792 | ||
2793 | if (!find_data_references_in_stmt (NULL, stmt, &datarefs)) | |
2794 | break; | |
2795 | } | |
2796 | ||
2797 | /* Skip leading unhandled stmts. */ | |
2798 | if (gsi_stmt (region_begin) == gsi_stmt (gsi)) | |
2799 | { | |
2800 | gsi_next (&gsi); | |
2801 | continue; | |
2802 | } | |
428db0ba | 2803 | |
61d371eb RB |
2804 | gimple_stmt_iterator region_end = gsi; |
2805 | ||
2806 | bool vectorized = false; | |
a5b50aa1 | 2807 | bool fatal = false; |
61d371eb | 2808 | bb_vinfo = vect_slp_analyze_bb_1 (region_begin, region_end, |
a5b50aa1 | 2809 | datarefs, insns, fatal); |
61d371eb RB |
2810 | if (bb_vinfo |
2811 | && dbg_cnt (vect_slp)) | |
2812 | { | |
428db0ba | 2813 | if (dump_enabled_p ()) |
61d371eb | 2814 | dump_printf_loc (MSG_NOTE, vect_location, "SLPing BB part\n"); |
428db0ba RB |
2815 | |
2816 | vect_schedule_slp (bb_vinfo); | |
2817 | ||
2818 | if (dump_enabled_p ()) | |
2819 | dump_printf_loc (MSG_NOTE, vect_location, | |
61d371eb | 2820 | "basic block part vectorized\n"); |
428db0ba RB |
2821 | |
2822 | destroy_bb_vec_info (bb_vinfo); | |
2823 | ||
61d371eb | 2824 | vectorized = true; |
428db0ba | 2825 | } |
61d371eb RB |
2826 | else |
2827 | destroy_bb_vec_info (bb_vinfo); | |
8e19f5a1 | 2828 | |
61d371eb | 2829 | any_vectorized |= vectorized; |
8e19f5a1 IR |
2830 | |
2831 | vector_sizes &= ~current_vector_size; | |
61d371eb RB |
2832 | if (vectorized |
2833 | || vector_sizes == 0 | |
a5b50aa1 RB |
2834 | || current_vector_size == 0 |
2835 | /* If vect_slp_analyze_bb_1 signaled that analysis for all | |
2836 | vector sizes will fail do not bother iterating. */ | |
2837 | || fatal) | |
61d371eb RB |
2838 | { |
2839 | if (gsi_end_p (region_end)) | |
2840 | break; | |
8e19f5a1 | 2841 | |
61d371eb RB |
2842 | /* Skip the unhandled stmt. */ |
2843 | gsi_next (&gsi); | |
2844 | ||
2845 | /* And reset vector sizes. */ | |
2846 | current_vector_size = 0; | |
2847 | vector_sizes = targetm.vectorize.autovectorize_vector_sizes (); | |
2848 | } | |
2849 | else | |
2850 | { | |
2851 | /* Try the next biggest vector size. */ | |
2852 | current_vector_size = 1 << floor_log2 (vector_sizes); | |
2853 | if (dump_enabled_p ()) | |
2854 | dump_printf_loc (MSG_NOTE, vect_location, | |
2855 | "***** Re-trying analysis with " | |
2856 | "vector size %d\n", current_vector_size); | |
2857 | ||
2858 | /* Start over. */ | |
2859 | gsi = region_begin; | |
2860 | } | |
8e19f5a1 | 2861 | } |
61d371eb RB |
2862 | |
2863 | return any_vectorized; | |
8e19f5a1 IR |
2864 | } |
2865 | ||
2866 | ||
e4af0bc4 IE |
2867 | /* Return 1 if vector type of boolean constant which is OPNUM |
2868 | operand in statement STMT is a boolean vector. */ | |
2869 | ||
2870 | static bool | |
2871 | vect_mask_constant_operand_p (gimple *stmt, int opnum) | |
2872 | { | |
2873 | stmt_vec_info stmt_vinfo = vinfo_for_stmt (stmt); | |
2874 | enum tree_code code = gimple_expr_code (stmt); | |
2875 | tree op, vectype; | |
2876 | gimple *def_stmt; | |
2877 | enum vect_def_type dt; | |
2878 | ||
2879 | /* For comparison and COND_EXPR type is chosen depending | |
2880 | on the other comparison operand. */ | |
2881 | if (TREE_CODE_CLASS (code) == tcc_comparison) | |
2882 | { | |
2883 | if (opnum) | |
2884 | op = gimple_assign_rhs1 (stmt); | |
2885 | else | |
2886 | op = gimple_assign_rhs2 (stmt); | |
2887 | ||
2888 | if (!vect_is_simple_use (op, stmt_vinfo->vinfo, &def_stmt, | |
2889 | &dt, &vectype)) | |
2890 | gcc_unreachable (); | |
2891 | ||
2892 | return !vectype || VECTOR_BOOLEAN_TYPE_P (vectype); | |
2893 | } | |
2894 | ||
2895 | if (code == COND_EXPR) | |
2896 | { | |
2897 | tree cond = gimple_assign_rhs1 (stmt); | |
2898 | ||
2899 | if (TREE_CODE (cond) == SSA_NAME) | |
7b1b0cc1 RB |
2900 | op = cond; |
2901 | else if (opnum) | |
e4af0bc4 IE |
2902 | op = TREE_OPERAND (cond, 1); |
2903 | else | |
2904 | op = TREE_OPERAND (cond, 0); | |
2905 | ||
2906 | if (!vect_is_simple_use (op, stmt_vinfo->vinfo, &def_stmt, | |
2907 | &dt, &vectype)) | |
2908 | gcc_unreachable (); | |
2909 | ||
2910 | return !vectype || VECTOR_BOOLEAN_TYPE_P (vectype); | |
2911 | } | |
2912 | ||
2913 | return VECTOR_BOOLEAN_TYPE_P (STMT_VINFO_VECTYPE (stmt_vinfo)); | |
2914 | } | |
2915 | ||
2916 | ||
b8698a0f L |
2917 | /* For constant and loop invariant defs of SLP_NODE this function returns |
2918 | (vector) defs (VEC_OPRNDS) that will be used in the vectorized stmts. | |
d59dc888 IR |
2919 | OP_NUM determines if we gather defs for operand 0 or operand 1 of the RHS of |
2920 | scalar stmts. NUMBER_OF_VECTORS is the number of vector defs to create. | |
b5aeb3bb IR |
2921 | REDUC_INDEX is the index of the reduction operand in the statements, unless |
2922 | it is -1. */ | |
ebfd146a IR |
2923 | |
2924 | static void | |
9dc3f7de | 2925 | vect_get_constant_vectors (tree op, slp_tree slp_node, |
9771b263 | 2926 | vec<tree> *vec_oprnds, |
b5aeb3bb IR |
2927 | unsigned int op_num, unsigned int number_of_vectors, |
2928 | int reduc_index) | |
ebfd146a | 2929 | { |
355fe088 TS |
2930 | vec<gimple *> stmts = SLP_TREE_SCALAR_STMTS (slp_node); |
2931 | gimple *stmt = stmts[0]; | |
ebfd146a | 2932 | stmt_vec_info stmt_vinfo = vinfo_for_stmt (stmt); |
d2a12ae7 | 2933 | unsigned nunits; |
ebfd146a | 2934 | tree vec_cst; |
d2a12ae7 RG |
2935 | tree *elts; |
2936 | unsigned j, number_of_places_left_in_vector; | |
ebfd146a | 2937 | tree vector_type; |
9dc3f7de | 2938 | tree vop; |
9771b263 | 2939 | int group_size = stmts.length (); |
ebfd146a | 2940 | unsigned int vec_num, i; |
d2a12ae7 | 2941 | unsigned number_of_copies = 1; |
9771b263 DN |
2942 | vec<tree> voprnds; |
2943 | voprnds.create (number_of_vectors); | |
ebfd146a | 2944 | bool constant_p, is_store; |
b5aeb3bb | 2945 | tree neutral_op = NULL; |
bac430c9 | 2946 | enum tree_code code = gimple_expr_code (stmt); |
355fe088 | 2947 | gimple *def_stmt; |
0e93a64e | 2948 | struct loop *loop; |
13396b6e | 2949 | gimple_seq ctor_seq = NULL; |
b5aeb3bb | 2950 | |
42fd8198 IE |
2951 | /* Check if vector type is a boolean vector. */ |
2952 | if (TREE_CODE (TREE_TYPE (op)) == BOOLEAN_TYPE | |
e4af0bc4 | 2953 | && vect_mask_constant_operand_p (stmt, op_num)) |
42fd8198 IE |
2954 | vector_type |
2955 | = build_same_sized_truth_vector_type (STMT_VINFO_VECTYPE (stmt_vinfo)); | |
2956 | else | |
2957 | vector_type = get_vectype_for_scalar_type (TREE_TYPE (op)); | |
afbe6325 RB |
2958 | nunits = TYPE_VECTOR_SUBPARTS (vector_type); |
2959 | ||
29ed4920 IR |
2960 | if (STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_reduction_def |
2961 | && reduc_index != -1) | |
b5aeb3bb | 2962 | { |
afbe6325 RB |
2963 | op_num = reduc_index; |
2964 | op = gimple_op (stmt, op_num + 1); | |
b5aeb3bb | 2965 | /* For additional copies (see the explanation of NUMBER_OF_COPIES below) |
ff802fa1 | 2966 | we need either neutral operands or the original operands. See |
b5aeb3bb IR |
2967 | get_initial_def_for_reduction() for details. */ |
2968 | switch (code) | |
2969 | { | |
2970 | case WIDEN_SUM_EXPR: | |
2971 | case DOT_PROD_EXPR: | |
afbe6325 | 2972 | case SAD_EXPR: |
b5aeb3bb IR |
2973 | case PLUS_EXPR: |
2974 | case MINUS_EXPR: | |
2975 | case BIT_IOR_EXPR: | |
2976 | case BIT_XOR_EXPR: | |
2977 | if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (op))) | |
2978 | neutral_op = build_real (TREE_TYPE (op), dconst0); | |
2979 | else | |
2980 | neutral_op = build_int_cst (TREE_TYPE (op), 0); | |
2981 | ||
2982 | break; | |
2983 | ||
2984 | case MULT_EXPR: | |
b5aeb3bb IR |
2985 | if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (op))) |
2986 | neutral_op = build_real (TREE_TYPE (op), dconst1); | |
2987 | else | |
2988 | neutral_op = build_int_cst (TREE_TYPE (op), 1); | |
2989 | ||
2990 | break; | |
2991 | ||
c1e822d5 IR |
2992 | case BIT_AND_EXPR: |
2993 | neutral_op = build_int_cst (TREE_TYPE (op), -1); | |
2994 | break; | |
2995 | ||
f1485e5b RB |
2996 | /* For MIN/MAX we don't have an easy neutral operand but |
2997 | the initial values can be used fine here. Only for | |
2998 | a reduction chain we have to force a neutral element. */ | |
2999 | case MAX_EXPR: | |
3000 | case MIN_EXPR: | |
3001 | if (!GROUP_FIRST_ELEMENT (stmt_vinfo)) | |
3002 | neutral_op = NULL; | |
3003 | else | |
3004 | { | |
3005 | def_stmt = SSA_NAME_DEF_STMT (op); | |
3006 | loop = (gimple_bb (stmt))->loop_father; | |
3007 | neutral_op = PHI_ARG_DEF_FROM_EDGE (def_stmt, | |
3008 | loop_preheader_edge (loop)); | |
3009 | } | |
3010 | break; | |
0e93a64e | 3011 | |
b5aeb3bb | 3012 | default: |
afbe6325 | 3013 | gcc_assert (!GROUP_FIRST_ELEMENT (stmt_vinfo)); |
0e93a64e | 3014 | neutral_op = NULL; |
b5aeb3bb IR |
3015 | } |
3016 | } | |
ebfd146a IR |
3017 | |
3018 | if (STMT_VINFO_DATA_REF (stmt_vinfo)) | |
3019 | { | |
3020 | is_store = true; | |
3021 | op = gimple_assign_rhs1 (stmt); | |
3022 | } | |
3023 | else | |
9dc3f7de IR |
3024 | is_store = false; |
3025 | ||
3026 | gcc_assert (op); | |
ebfd146a IR |
3027 | |
3028 | if (CONSTANT_CLASS_P (op)) | |
d59dc888 | 3029 | constant_p = true; |
ebfd146a | 3030 | else |
d59dc888 IR |
3031 | constant_p = false; |
3032 | ||
ebfd146a | 3033 | /* NUMBER_OF_COPIES is the number of times we need to use the same values in |
b8698a0f | 3034 | created vectors. It is greater than 1 if unrolling is performed. |
ebfd146a IR |
3035 | |
3036 | For example, we have two scalar operands, s1 and s2 (e.g., group of | |
3037 | strided accesses of size two), while NUNITS is four (i.e., four scalars | |
f7e531cf IR |
3038 | of this type can be packed in a vector). The output vector will contain |
3039 | two copies of each scalar operand: {s1, s2, s1, s2}. (NUMBER_OF_COPIES | |
ebfd146a IR |
3040 | will be 2). |
3041 | ||
b8698a0f | 3042 | If GROUP_SIZE > NUNITS, the scalars will be split into several vectors |
ebfd146a IR |
3043 | containing the operands. |
3044 | ||
3045 | For example, NUNITS is four as before, and the group size is 8 | |
f7e531cf | 3046 | (s1, s2, ..., s8). We will create two vectors {s1, s2, s3, s4} and |
ebfd146a | 3047 | {s5, s6, s7, s8}. */ |
b8698a0f | 3048 | |
14a61437 | 3049 | number_of_copies = nunits * number_of_vectors / group_size; |
ebfd146a IR |
3050 | |
3051 | number_of_places_left_in_vector = nunits; | |
d2a12ae7 | 3052 | elts = XALLOCAVEC (tree, nunits); |
90dd6e3d | 3053 | bool place_after_defs = false; |
ebfd146a IR |
3054 | for (j = 0; j < number_of_copies; j++) |
3055 | { | |
9771b263 | 3056 | for (i = group_size - 1; stmts.iterate (i, &stmt); i--) |
ebfd146a IR |
3057 | { |
3058 | if (is_store) | |
3059 | op = gimple_assign_rhs1 (stmt); | |
bac430c9 | 3060 | else |
f7e531cf | 3061 | { |
bac430c9 | 3062 | switch (code) |
f7e531cf | 3063 | { |
bac430c9 | 3064 | case COND_EXPR: |
a989bcc3 IE |
3065 | { |
3066 | tree cond = gimple_assign_rhs1 (stmt); | |
3067 | if (TREE_CODE (cond) == SSA_NAME) | |
3068 | op = gimple_op (stmt, op_num + 1); | |
3069 | else if (op_num == 0 || op_num == 1) | |
bac430c9 | 3070 | op = TREE_OPERAND (cond, op_num); |
a989bcc3 IE |
3071 | else |
3072 | { | |
3073 | if (op_num == 2) | |
3074 | op = gimple_assign_rhs2 (stmt); | |
3075 | else | |
3076 | op = gimple_assign_rhs3 (stmt); | |
3077 | } | |
3078 | } | |
bac430c9 IR |
3079 | break; |
3080 | ||
3081 | case CALL_EXPR: | |
3082 | op = gimple_call_arg (stmt, op_num); | |
3083 | break; | |
3084 | ||
b84b294a JJ |
3085 | case LSHIFT_EXPR: |
3086 | case RSHIFT_EXPR: | |
3087 | case LROTATE_EXPR: | |
3088 | case RROTATE_EXPR: | |
3089 | op = gimple_op (stmt, op_num + 1); | |
3090 | /* Unlike the other binary operators, shifts/rotates have | |
3091 | the shift count being int, instead of the same type as | |
3092 | the lhs, so make sure the scalar is the right type if | |
3093 | we are dealing with vectors of | |
3094 | long long/long/short/char. */ | |
793d9a16 | 3095 | if (op_num == 1 && TREE_CODE (op) == INTEGER_CST) |
b84b294a JJ |
3096 | op = fold_convert (TREE_TYPE (vector_type), op); |
3097 | break; | |
3098 | ||
bac430c9 IR |
3099 | default: |
3100 | op = gimple_op (stmt, op_num + 1); | |
b84b294a | 3101 | break; |
f7e531cf IR |
3102 | } |
3103 | } | |
b8698a0f | 3104 | |
b5aeb3bb IR |
3105 | if (reduc_index != -1) |
3106 | { | |
0e93a64e IR |
3107 | loop = (gimple_bb (stmt))->loop_father; |
3108 | def_stmt = SSA_NAME_DEF_STMT (op); | |
b5aeb3bb IR |
3109 | |
3110 | gcc_assert (loop); | |
b010117a IR |
3111 | |
3112 | /* Get the def before the loop. In reduction chain we have only | |
3113 | one initial value. */ | |
3114 | if ((j != (number_of_copies - 1) | |
3115 | || (GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)) | |
3116 | && i != 0)) | |
3117 | && neutral_op) | |
b5aeb3bb | 3118 | op = neutral_op; |
b010117a IR |
3119 | else |
3120 | op = PHI_ARG_DEF_FROM_EDGE (def_stmt, | |
3121 | loop_preheader_edge (loop)); | |
b5aeb3bb IR |
3122 | } |
3123 | ||
ebfd146a | 3124 | /* Create 'vect_ = {op0,op1,...,opn}'. */ |
ebfd146a | 3125 | number_of_places_left_in_vector--; |
90dd6e3d | 3126 | tree orig_op = op; |
13396b6e | 3127 | if (!types_compatible_p (TREE_TYPE (vector_type), TREE_TYPE (op))) |
50eeef09 | 3128 | { |
793d9a16 | 3129 | if (CONSTANT_CLASS_P (op)) |
13396b6e | 3130 | { |
42fd8198 IE |
3131 | if (VECTOR_BOOLEAN_TYPE_P (vector_type)) |
3132 | { | |
3133 | /* Can't use VIEW_CONVERT_EXPR for booleans because | |
3134 | of possibly different sizes of scalar value and | |
3135 | vector element. */ | |
3136 | if (integer_zerop (op)) | |
3137 | op = build_int_cst (TREE_TYPE (vector_type), 0); | |
3138 | else if (integer_onep (op)) | |
158beb4a | 3139 | op = build_all_ones_cst (TREE_TYPE (vector_type)); |
42fd8198 IE |
3140 | else |
3141 | gcc_unreachable (); | |
3142 | } | |
3143 | else | |
3144 | op = fold_unary (VIEW_CONVERT_EXPR, | |
3145 | TREE_TYPE (vector_type), op); | |
13396b6e JJ |
3146 | gcc_assert (op && CONSTANT_CLASS_P (op)); |
3147 | } | |
3148 | else | |
3149 | { | |
b731b390 | 3150 | tree new_temp = make_ssa_name (TREE_TYPE (vector_type)); |
355fe088 | 3151 | gimple *init_stmt; |
262a363f JJ |
3152 | if (VECTOR_BOOLEAN_TYPE_P (vector_type)) |
3153 | { | |
158beb4a JJ |
3154 | tree true_val |
3155 | = build_all_ones_cst (TREE_TYPE (vector_type)); | |
3156 | tree false_val | |
3157 | = build_zero_cst (TREE_TYPE (vector_type)); | |
7c285ab9 | 3158 | gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (op))); |
158beb4a JJ |
3159 | init_stmt = gimple_build_assign (new_temp, COND_EXPR, |
3160 | op, true_val, | |
3161 | false_val); | |
262a363f | 3162 | } |
262a363f JJ |
3163 | else |
3164 | { | |
3165 | op = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (vector_type), | |
3166 | op); | |
3167 | init_stmt | |
3168 | = gimple_build_assign (new_temp, VIEW_CONVERT_EXPR, | |
3169 | op); | |
3170 | } | |
13396b6e JJ |
3171 | gimple_seq_add_stmt (&ctor_seq, init_stmt); |
3172 | op = new_temp; | |
3173 | } | |
50eeef09 | 3174 | } |
d2a12ae7 | 3175 | elts[number_of_places_left_in_vector] = op; |
793d9a16 RB |
3176 | if (!CONSTANT_CLASS_P (op)) |
3177 | constant_p = false; | |
90dd6e3d RB |
3178 | if (TREE_CODE (orig_op) == SSA_NAME |
3179 | && !SSA_NAME_IS_DEFAULT_DEF (orig_op) | |
3180 | && STMT_VINFO_BB_VINFO (stmt_vinfo) | |
3181 | && (STMT_VINFO_BB_VINFO (stmt_vinfo)->bb | |
3182 | == gimple_bb (SSA_NAME_DEF_STMT (orig_op)))) | |
3183 | place_after_defs = true; | |
ebfd146a IR |
3184 | |
3185 | if (number_of_places_left_in_vector == 0) | |
3186 | { | |
3187 | number_of_places_left_in_vector = nunits; | |
3188 | ||
3189 | if (constant_p) | |
d2a12ae7 | 3190 | vec_cst = build_vector (vector_type, elts); |
ebfd146a | 3191 | else |
d2a12ae7 | 3192 | { |
9771b263 | 3193 | vec<constructor_elt, va_gc> *v; |
d2a12ae7 | 3194 | unsigned k; |
9771b263 | 3195 | vec_alloc (v, nunits); |
d2a12ae7 RG |
3196 | for (k = 0; k < nunits; ++k) |
3197 | CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, elts[k]); | |
3198 | vec_cst = build_constructor (vector_type, v); | |
3199 | } | |
90dd6e3d RB |
3200 | tree init; |
3201 | gimple_stmt_iterator gsi; | |
3202 | if (place_after_defs) | |
3203 | { | |
3204 | gsi = gsi_for_stmt | |
3205 | (vect_find_last_scalar_stmt_in_slp (slp_node)); | |
3206 | init = vect_init_vector (stmt, vec_cst, vector_type, &gsi); | |
3207 | } | |
3208 | else | |
3209 | init = vect_init_vector (stmt, vec_cst, vector_type, NULL); | |
13396b6e JJ |
3210 | if (ctor_seq != NULL) |
3211 | { | |
90dd6e3d | 3212 | gsi = gsi_for_stmt (SSA_NAME_DEF_STMT (init)); |
13396b6e JJ |
3213 | gsi_insert_seq_before_without_update (&gsi, ctor_seq, |
3214 | GSI_SAME_STMT); | |
3215 | ctor_seq = NULL; | |
3216 | } | |
90dd6e3d RB |
3217 | voprnds.quick_push (init); |
3218 | place_after_defs = false; | |
ebfd146a IR |
3219 | } |
3220 | } | |
3221 | } | |
3222 | ||
b8698a0f | 3223 | /* Since the vectors are created in the reverse order, we should invert |
ebfd146a | 3224 | them. */ |
9771b263 | 3225 | vec_num = voprnds.length (); |
d2a12ae7 | 3226 | for (j = vec_num; j != 0; j--) |
ebfd146a | 3227 | { |
9771b263 DN |
3228 | vop = voprnds[j - 1]; |
3229 | vec_oprnds->quick_push (vop); | |
ebfd146a IR |
3230 | } |
3231 | ||
9771b263 | 3232 | voprnds.release (); |
ebfd146a IR |
3233 | |
3234 | /* In case that VF is greater than the unrolling factor needed for the SLP | |
b8698a0f L |
3235 | group of stmts, NUMBER_OF_VECTORS to be created is greater than |
3236 | NUMBER_OF_SCALARS/NUNITS or NUNITS/NUMBER_OF_SCALARS, and hence we have | |
ebfd146a | 3237 | to replicate the vectors. */ |
9771b263 | 3238 | while (number_of_vectors > vec_oprnds->length ()) |
ebfd146a | 3239 | { |
b5aeb3bb IR |
3240 | tree neutral_vec = NULL; |
3241 | ||
3242 | if (neutral_op) | |
3243 | { | |
3244 | if (!neutral_vec) | |
b9acc9f1 | 3245 | neutral_vec = build_vector_from_val (vector_type, neutral_op); |
b5aeb3bb | 3246 | |
9771b263 | 3247 | vec_oprnds->quick_push (neutral_vec); |
b5aeb3bb IR |
3248 | } |
3249 | else | |
3250 | { | |
9771b263 DN |
3251 | for (i = 0; vec_oprnds->iterate (i, &vop) && i < vec_num; i++) |
3252 | vec_oprnds->quick_push (vop); | |
b5aeb3bb | 3253 | } |
ebfd146a IR |
3254 | } |
3255 | } | |
3256 | ||
3257 | ||
3258 | /* Get vectorized definitions from SLP_NODE that contains corresponding | |
3259 | vectorized def-stmts. */ | |
3260 | ||
3261 | static void | |
9771b263 | 3262 | vect_get_slp_vect_defs (slp_tree slp_node, vec<tree> *vec_oprnds) |
ebfd146a IR |
3263 | { |
3264 | tree vec_oprnd; | |
355fe088 | 3265 | gimple *vec_def_stmt; |
ebfd146a IR |
3266 | unsigned int i; |
3267 | ||
9771b263 | 3268 | gcc_assert (SLP_TREE_VEC_STMTS (slp_node).exists ()); |
ebfd146a | 3269 | |
9771b263 | 3270 | FOR_EACH_VEC_ELT (SLP_TREE_VEC_STMTS (slp_node), i, vec_def_stmt) |
ebfd146a IR |
3271 | { |
3272 | gcc_assert (vec_def_stmt); | |
3273 | vec_oprnd = gimple_get_lhs (vec_def_stmt); | |
9771b263 | 3274 | vec_oprnds->quick_push (vec_oprnd); |
ebfd146a IR |
3275 | } |
3276 | } | |
3277 | ||
3278 | ||
b8698a0f L |
3279 | /* Get vectorized definitions for SLP_NODE. |
3280 | If the scalar definitions are loop invariants or constants, collect them and | |
ebfd146a IR |
3281 | call vect_get_constant_vectors() to create vector stmts. |
3282 | Otherwise, the def-stmts must be already vectorized and the vectorized stmts | |
d092494c IR |
3283 | must be stored in the corresponding child of SLP_NODE, and we call |
3284 | vect_get_slp_vect_defs () to retrieve them. */ | |
b8698a0f | 3285 | |
ebfd146a | 3286 | void |
9771b263 | 3287 | vect_get_slp_defs (vec<tree> ops, slp_tree slp_node, |
37b5ec8f | 3288 | vec<vec<tree> > *vec_oprnds, int reduc_index) |
ebfd146a | 3289 | { |
355fe088 | 3290 | gimple *first_stmt; |
d092494c | 3291 | int number_of_vects = 0, i; |
b8698a0f | 3292 | HOST_WIDE_INT lhs_size_unit, rhs_size_unit; |
d092494c | 3293 | slp_tree child = NULL; |
37b5ec8f | 3294 | vec<tree> vec_defs; |
e44978dc | 3295 | tree oprnd; |
038000bd | 3296 | bool first_iteration = true; |
ebfd146a | 3297 | |
9771b263 DN |
3298 | first_stmt = SLP_TREE_SCALAR_STMTS (slp_node)[0]; |
3299 | FOR_EACH_VEC_ELT (ops, i, oprnd) | |
ebfd146a | 3300 | { |
038000bd AH |
3301 | bool vectorized_defs = false; |
3302 | ||
3303 | if (oprnd == NULL) | |
3304 | { | |
3305 | vec_defs = vNULL; | |
3306 | vec_defs.create (0); | |
3307 | vec_oprnds->quick_push (vec_defs); | |
3308 | continue; | |
3309 | } | |
3310 | ||
d092494c IR |
3311 | /* For each operand we check if it has vectorized definitions in a child |
3312 | node or we need to create them (for invariants and constants). We | |
3313 | check if the LHS of the first stmt of the next child matches OPRND. | |
3314 | If it does, we found the correct child. Otherwise, we call | |
038000bd AH |
3315 | vect_get_constant_vectors (). */ |
3316 | for (unsigned int child_index = 0; | |
3317 | child_index < SLP_TREE_CHILDREN (slp_node).length (); child_index++) | |
ebfd146a | 3318 | { |
01d8bf07 | 3319 | child = SLP_TREE_CHILDREN (slp_node)[child_index]; |
d092494c | 3320 | |
e44978dc | 3321 | /* We have to check both pattern and original def, if available. */ |
603cca93 | 3322 | if (SLP_TREE_DEF_TYPE (child) == vect_internal_def) |
e44978dc | 3323 | { |
355fe088 TS |
3324 | gimple *first_def = SLP_TREE_SCALAR_STMTS (child)[0]; |
3325 | gimple *related | |
90dd6e3d RB |
3326 | = STMT_VINFO_RELATED_STMT (vinfo_for_stmt (first_def)); |
3327 | ||
3328 | if (operand_equal_p (oprnd, gimple_get_lhs (first_def), 0) | |
3329 | || (related | |
3330 | && operand_equal_p (oprnd, gimple_get_lhs (related), 0))) | |
3331 | { | |
3332 | /* The number of vector defs is determined by the number of | |
3333 | vector statements in the node from which we get those | |
3334 | statements. */ | |
3335 | number_of_vects = SLP_TREE_NUMBER_OF_VEC_STMTS (child); | |
3336 | vectorized_defs = true; | |
038000bd | 3337 | break; |
90dd6e3d | 3338 | } |
e44978dc | 3339 | } |
d092494c | 3340 | } |
ebfd146a | 3341 | |
038000bd AH |
3342 | if (!vectorized_defs && first_iteration) |
3343 | { | |
3344 | number_of_vects = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node); | |
3345 | /* Number of vector stmts was calculated according to LHS in | |
3346 | vect_schedule_slp_instance (), fix it by replacing LHS with | |
3347 | RHS, if necessary. See vect_get_smallest_scalar_type () for | |
3348 | details. */ | |
3349 | vect_get_smallest_scalar_type (first_stmt, &lhs_size_unit, | |
3350 | &rhs_size_unit); | |
3351 | if (rhs_size_unit != lhs_size_unit) | |
3352 | { | |
3353 | number_of_vects *= rhs_size_unit; | |
3354 | number_of_vects /= lhs_size_unit; | |
3355 | } | |
d092494c | 3356 | } |
b5aeb3bb | 3357 | |
d092494c | 3358 | /* Allocate memory for vectorized defs. */ |
37b5ec8f JJ |
3359 | vec_defs = vNULL; |
3360 | vec_defs.create (number_of_vects); | |
ebfd146a | 3361 | |
d092494c IR |
3362 | /* For reduction defs we call vect_get_constant_vectors (), since we are |
3363 | looking for initial loop invariant values. */ | |
3364 | if (vectorized_defs && reduc_index == -1) | |
3365 | /* The defs are already vectorized. */ | |
37b5ec8f | 3366 | vect_get_slp_vect_defs (child, &vec_defs); |
d092494c IR |
3367 | else |
3368 | /* Build vectors from scalar defs. */ | |
37b5ec8f | 3369 | vect_get_constant_vectors (oprnd, slp_node, &vec_defs, i, |
d092494c | 3370 | number_of_vects, reduc_index); |
ebfd146a | 3371 | |
37b5ec8f | 3372 | vec_oprnds->quick_push (vec_defs); |
ebfd146a | 3373 | |
d092494c IR |
3374 | /* For reductions, we only need initial values. */ |
3375 | if (reduc_index != -1) | |
3376 | return; | |
038000bd AH |
3377 | |
3378 | first_iteration = false; | |
d092494c | 3379 | } |
ebfd146a IR |
3380 | } |
3381 | ||
a70d6342 | 3382 | |
b8698a0f | 3383 | /* Create NCOPIES permutation statements using the mask MASK_BYTES (by |
ebfd146a IR |
3384 | building a vector of type MASK_TYPE from it) and two input vectors placed in |
3385 | DR_CHAIN at FIRST_VEC_INDX and SECOND_VEC_INDX for the first copy and | |
3386 | shifting by STRIDE elements of DR_CHAIN for every copy. | |
3387 | (STRIDE is the number of vectorized stmts for NODE divided by the number of | |
b8698a0f | 3388 | copies). |
ebfd146a IR |
3389 | VECT_STMTS_COUNTER specifies the index in the vectorized stmts of NODE, where |
3390 | the created stmts must be inserted. */ | |
3391 | ||
3392 | static inline void | |
355fe088 | 3393 | vect_create_mask_and_perm (gimple *stmt, |
faf63e39 | 3394 | tree mask, int first_vec_indx, int second_vec_indx, |
b8698a0f | 3395 | gimple_stmt_iterator *gsi, slp_tree node, |
9771b263 | 3396 | tree vectype, vec<tree> dr_chain, |
ebfd146a IR |
3397 | int ncopies, int vect_stmts_counter) |
3398 | { | |
faf63e39 | 3399 | tree perm_dest; |
355fe088 | 3400 | gimple *perm_stmt = NULL; |
7706cb01 | 3401 | int i, stride_in, stride_out; |
ebfd146a | 3402 | tree first_vec, second_vec, data_ref; |
ebfd146a | 3403 | |
7706cb01 RB |
3404 | stride_out = SLP_TREE_NUMBER_OF_VEC_STMTS (node) / ncopies; |
3405 | stride_in = dr_chain.length () / ncopies; | |
ebfd146a | 3406 | |
b8698a0f | 3407 | /* Initialize the vect stmts of NODE to properly insert the generated |
ebfd146a | 3408 | stmts later. */ |
9771b263 | 3409 | for (i = SLP_TREE_VEC_STMTS (node).length (); |
ebfd146a | 3410 | i < (int) SLP_TREE_NUMBER_OF_VEC_STMTS (node); i++) |
9771b263 | 3411 | SLP_TREE_VEC_STMTS (node).quick_push (NULL); |
ebfd146a IR |
3412 | |
3413 | perm_dest = vect_create_destination_var (gimple_assign_lhs (stmt), vectype); | |
3414 | for (i = 0; i < ncopies; i++) | |
3415 | { | |
9771b263 DN |
3416 | first_vec = dr_chain[first_vec_indx]; |
3417 | second_vec = dr_chain[second_vec_indx]; | |
ebfd146a | 3418 | |
be377c80 RB |
3419 | /* Generate the permute statement if necessary. */ |
3420 | if (mask) | |
3421 | { | |
3422 | perm_stmt = gimple_build_assign (perm_dest, VEC_PERM_EXPR, | |
3423 | first_vec, second_vec, mask); | |
3424 | data_ref = make_ssa_name (perm_dest, perm_stmt); | |
3425 | gimple_set_lhs (perm_stmt, data_ref); | |
3426 | vect_finish_stmt_generation (stmt, perm_stmt, gsi); | |
3427 | } | |
3428 | else | |
3429 | /* If mask was NULL_TREE generate the requested identity transform. */ | |
3430 | perm_stmt = SSA_NAME_DEF_STMT (first_vec); | |
ebfd146a | 3431 | |
b8698a0f | 3432 | /* Store the vector statement in NODE. */ |
7706cb01 RB |
3433 | SLP_TREE_VEC_STMTS (node)[stride_out * i + vect_stmts_counter] |
3434 | = perm_stmt; | |
ebfd146a | 3435 | |
7706cb01 RB |
3436 | first_vec_indx += stride_in; |
3437 | second_vec_indx += stride_in; | |
ebfd146a | 3438 | } |
ebfd146a IR |
3439 | } |
3440 | ||
3441 | ||
ebfd146a IR |
3442 | /* Generate vector permute statements from a list of loads in DR_CHAIN. |
3443 | If ANALYZE_ONLY is TRUE, only check that it is possible to create valid | |
01d8bf07 RB |
3444 | permute statements for the SLP node NODE of the SLP instance |
3445 | SLP_NODE_INSTANCE. */ | |
3446 | ||
ebfd146a | 3447 | bool |
01d8bf07 | 3448 | vect_transform_slp_perm_load (slp_tree node, vec<tree> dr_chain, |
ebfd146a | 3449 | gimple_stmt_iterator *gsi, int vf, |
29afecdf RB |
3450 | slp_instance slp_node_instance, bool analyze_only, |
3451 | unsigned *n_perms) | |
ebfd146a | 3452 | { |
355fe088 | 3453 | gimple *stmt = SLP_TREE_SCALAR_STMTS (node)[0]; |
ebfd146a IR |
3454 | stmt_vec_info stmt_info = vinfo_for_stmt (stmt); |
3455 | tree mask_element_type = NULL_TREE, mask_type; | |
2ce27200 | 3456 | int nunits, vec_index = 0; |
2635892a | 3457 | tree vectype = STMT_VINFO_VECTYPE (stmt_info); |
ebfd146a | 3458 | int group_size = SLP_INSTANCE_GROUP_SIZE (slp_node_instance); |
2ce27200 | 3459 | int unroll_factor, mask_element, ncopies; |
22e4dee7 | 3460 | unsigned char *mask; |
ef4bddc2 | 3461 | machine_mode mode; |
ebfd146a | 3462 | |
91ff1504 RB |
3463 | if (!STMT_VINFO_GROUPED_ACCESS (stmt_info)) |
3464 | return false; | |
3465 | ||
3466 | stmt_info = vinfo_for_stmt (GROUP_FIRST_ELEMENT (stmt_info)); | |
3467 | ||
22e4dee7 RH |
3468 | mode = TYPE_MODE (vectype); |
3469 | ||
2635892a RH |
3470 | /* The generic VEC_PERM_EXPR code always uses an integral type of the |
3471 | same size as the vector element being permuted. */ | |
96f9265a RG |
3472 | mask_element_type = lang_hooks.types.type_for_mode |
3473 | (int_mode_for_mode (TYPE_MODE (TREE_TYPE (vectype))), 1); | |
ebfd146a | 3474 | mask_type = get_vectype_for_scalar_type (mask_element_type); |
ebfd146a | 3475 | nunits = TYPE_VECTOR_SUBPARTS (vectype); |
22e4dee7 | 3476 | mask = XALLOCAVEC (unsigned char, nunits); |
ebfd146a IR |
3477 | unroll_factor = SLP_INSTANCE_UNROLLING_FACTOR (slp_node_instance); |
3478 | ||
b8698a0f | 3479 | /* Number of copies is determined by the final vectorization factor |
ebfd146a | 3480 | relatively to SLP_NODE_INSTANCE unrolling factor. */ |
b8698a0f | 3481 | ncopies = vf / SLP_INSTANCE_UNROLLING_FACTOR (slp_node_instance); |
ebfd146a | 3482 | |
b8698a0f L |
3483 | /* Generate permutation masks for every NODE. Number of masks for each NODE |
3484 | is equal to GROUP_SIZE. | |
3485 | E.g., we have a group of three nodes with three loads from the same | |
3486 | location in each node, and the vector size is 4. I.e., we have a | |
3487 | a0b0c0a1b1c1... sequence and we need to create the following vectors: | |
ebfd146a IR |
3488 | for a's: a0a0a0a1 a1a1a2a2 a2a3a3a3 |
3489 | for b's: b0b0b0b1 b1b1b2b2 b2b3b3b3 | |
3490 | ... | |
3491 | ||
2635892a | 3492 | The masks for a's should be: {0,0,0,3} {3,3,6,6} {6,9,9,9}. |
b8698a0f | 3493 | The last mask is illegal since we assume two operands for permute |
ff802fa1 IR |
3494 | operation, and the mask element values can't be outside that range. |
3495 | Hence, the last mask must be converted into {2,5,5,5}. | |
b8698a0f | 3496 | For the first two permutations we need the first and the second input |
ebfd146a | 3497 | vectors: {a0,b0,c0,a1} and {b1,c1,a2,b2}, and for the last permutation |
b8698a0f | 3498 | we need the second and the third vectors: {b1,c1,a2,b2} and |
ebfd146a IR |
3499 | {c2,a3,b3,c3}. */ |
3500 | ||
2ce27200 RB |
3501 | int vect_stmts_counter = 0; |
3502 | int index = 0; | |
3503 | int first_vec_index = -1; | |
3504 | int second_vec_index = -1; | |
be377c80 | 3505 | bool noop_p = true; |
29afecdf | 3506 | *n_perms = 0; |
ebfd146a | 3507 | |
2ce27200 RB |
3508 | for (int j = 0; j < unroll_factor; j++) |
3509 | { | |
3510 | for (int k = 0; k < group_size; k++) | |
3511 | { | |
3512 | int i = (SLP_TREE_LOAD_PERMUTATION (node)[k] | |
3513 | + j * STMT_VINFO_GROUP_SIZE (stmt_info)); | |
3514 | vec_index = i / nunits; | |
3515 | mask_element = i % nunits; | |
3516 | if (vec_index == first_vec_index | |
3517 | || first_vec_index == -1) | |
3518 | { | |
3519 | first_vec_index = vec_index; | |
3520 | } | |
3521 | else if (vec_index == second_vec_index | |
3522 | || second_vec_index == -1) | |
3523 | { | |
3524 | second_vec_index = vec_index; | |
3525 | mask_element += nunits; | |
3526 | } | |
3527 | else | |
3528 | { | |
3529 | if (dump_enabled_p ()) | |
3530 | { | |
3531 | dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location, | |
3532 | "permutation requires at " | |
3533 | "least three vectors "); | |
3534 | dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, | |
3535 | stmt, 0); | |
2ce27200 RB |
3536 | } |
3537 | return false; | |
3538 | } | |
ebfd146a | 3539 | |
2ce27200 RB |
3540 | gcc_assert (mask_element >= 0 |
3541 | && mask_element < 2 * nunits); | |
be377c80 RB |
3542 | if (mask_element != index) |
3543 | noop_p = false; | |
2ce27200 RB |
3544 | mask[index++] = mask_element; |
3545 | ||
3546 | if (index == nunits) | |
3547 | { | |
be377c80 RB |
3548 | if (! noop_p |
3549 | && ! can_vec_perm_p (mode, false, mask)) | |
2ce27200 RB |
3550 | { |
3551 | if (dump_enabled_p ()) | |
22e4dee7 | 3552 | { |
2ce27200 RB |
3553 | dump_printf_loc (MSG_MISSED_OPTIMIZATION, |
3554 | vect_location, | |
3555 | "unsupported vect permute { "); | |
3556 | for (i = 0; i < nunits; ++i) | |
3557 | dump_printf (MSG_MISSED_OPTIMIZATION, "%d ", mask[i]); | |
3558 | dump_printf (MSG_MISSED_OPTIMIZATION, "}\n"); | |
22e4dee7 | 3559 | } |
2ce27200 RB |
3560 | return false; |
3561 | } | |
22e4dee7 | 3562 | |
29afecdf RB |
3563 | if (! noop_p) |
3564 | ++*n_perms; | |
3565 | ||
2ce27200 RB |
3566 | if (!analyze_only) |
3567 | { | |
be377c80 RB |
3568 | tree mask_vec = NULL_TREE; |
3569 | ||
3570 | if (! noop_p) | |
3571 | { | |
3572 | tree *mask_elts = XALLOCAVEC (tree, nunits); | |
3573 | for (int l = 0; l < nunits; ++l) | |
3574 | mask_elts[l] = build_int_cst (mask_element_type, | |
3575 | mask[l]); | |
3576 | mask_vec = build_vector (mask_type, mask_elts); | |
3577 | } | |
2ce27200 RB |
3578 | |
3579 | if (second_vec_index == -1) | |
3580 | second_vec_index = first_vec_index; | |
3581 | vect_create_mask_and_perm (stmt, mask_vec, first_vec_index, | |
3582 | second_vec_index, | |
3583 | gsi, node, vectype, dr_chain, | |
3584 | ncopies, vect_stmts_counter++); | |
3585 | } | |
ebfd146a | 3586 | |
2ce27200 RB |
3587 | index = 0; |
3588 | first_vec_index = -1; | |
3589 | second_vec_index = -1; | |
be377c80 | 3590 | noop_p = true; |
2ce27200 RB |
3591 | } |
3592 | } | |
b8698a0f | 3593 | } |
ebfd146a | 3594 | |
ebfd146a IR |
3595 | return true; |
3596 | } | |
3597 | ||
3598 | ||
3599 | ||
3600 | /* Vectorize SLP instance tree in postorder. */ | |
3601 | ||
3602 | static bool | |
3603 | vect_schedule_slp_instance (slp_tree node, slp_instance instance, | |
a70d6342 | 3604 | unsigned int vectorization_factor) |
ebfd146a | 3605 | { |
355fe088 | 3606 | gimple *stmt; |
0d0293ac | 3607 | bool grouped_store, is_store; |
ebfd146a IR |
3608 | gimple_stmt_iterator si; |
3609 | stmt_vec_info stmt_info; | |
3610 | unsigned int vec_stmts_size, nunits, group_size; | |
3611 | tree vectype; | |
603cca93 | 3612 | int i, j; |
d755c7ef | 3613 | slp_tree child; |
ebfd146a | 3614 | |
603cca93 | 3615 | if (SLP_TREE_DEF_TYPE (node) != vect_internal_def) |
ebfd146a IR |
3616 | return false; |
3617 | ||
9771b263 | 3618 | FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child) |
d755c7ef | 3619 | vect_schedule_slp_instance (child, instance, vectorization_factor); |
b8698a0f | 3620 | |
603cca93 RB |
3621 | /* Push SLP node def-type to stmts. */ |
3622 | FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child) | |
3623 | if (SLP_TREE_DEF_TYPE (child) != vect_internal_def) | |
3624 | FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (child), j, stmt) | |
3625 | STMT_VINFO_DEF_TYPE (vinfo_for_stmt (stmt)) = SLP_TREE_DEF_TYPE (child); | |
3626 | ||
9771b263 | 3627 | stmt = SLP_TREE_SCALAR_STMTS (node)[0]; |
ebfd146a IR |
3628 | stmt_info = vinfo_for_stmt (stmt); |
3629 | ||
3630 | /* VECTYPE is the type of the destination. */ | |
b690cc0f | 3631 | vectype = STMT_VINFO_VECTYPE (stmt_info); |
ebfd146a IR |
3632 | nunits = (unsigned int) TYPE_VECTOR_SUBPARTS (vectype); |
3633 | group_size = SLP_INSTANCE_GROUP_SIZE (instance); | |
3634 | ||
3635 | /* For each SLP instance calculate number of vector stmts to be created | |
ff802fa1 | 3636 | for the scalar stmts in each node of the SLP tree. Number of vector |
ebfd146a IR |
3637 | elements in one vector iteration is the number of scalar elements in |
3638 | one scalar iteration (GROUP_SIZE) multiplied by VF divided by vector | |
14a61437 RB |
3639 | size. |
3640 | Unless this is a SLP reduction in which case the number of vector | |
3641 | stmts is equal to the number of vector stmts of the children. */ | |
3642 | if (GROUP_FIRST_ELEMENT (stmt_info) | |
3643 | && !STMT_VINFO_GROUPED_ACCESS (stmt_info)) | |
3644 | vec_stmts_size = SLP_TREE_NUMBER_OF_VEC_STMTS (SLP_TREE_CHILDREN (node)[0]); | |
3645 | else | |
3646 | vec_stmts_size = (vectorization_factor * group_size) / nunits; | |
ebfd146a | 3647 | |
9771b263 | 3648 | if (!SLP_TREE_VEC_STMTS (node).exists ()) |
ebfd146a | 3649 | { |
9771b263 | 3650 | SLP_TREE_VEC_STMTS (node).create (vec_stmts_size); |
ebfd146a IR |
3651 | SLP_TREE_NUMBER_OF_VEC_STMTS (node) = vec_stmts_size; |
3652 | } | |
3653 | ||
73fbfcad | 3654 | if (dump_enabled_p ()) |
ebfd146a | 3655 | { |
78c60e3d SS |
3656 | dump_printf_loc (MSG_NOTE,vect_location, |
3657 | "------>vectorizing SLP node starting from: "); | |
3658 | dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0); | |
b8698a0f | 3659 | } |
ebfd146a | 3660 | |
2e8ab70c RB |
3661 | /* Vectorized stmts go before the last scalar stmt which is where |
3662 | all uses are ready. */ | |
3663 | si = gsi_for_stmt (vect_find_last_scalar_stmt_in_slp (node)); | |
e4a707c4 | 3664 | |
b010117a IR |
3665 | /* Mark the first element of the reduction chain as reduction to properly |
3666 | transform the node. In the analysis phase only the last element of the | |
3667 | chain is marked as reduction. */ | |
0d0293ac | 3668 | if (GROUP_FIRST_ELEMENT (stmt_info) && !STMT_VINFO_GROUPED_ACCESS (stmt_info) |
b010117a IR |
3669 | && GROUP_FIRST_ELEMENT (stmt_info) == stmt) |
3670 | { | |
3671 | STMT_VINFO_DEF_TYPE (stmt_info) = vect_reduction_def; | |
3672 | STMT_VINFO_TYPE (stmt_info) = reduc_vec_info_type; | |
3673 | } | |
3674 | ||
6876e5bc RB |
3675 | /* Handle two-operation SLP nodes by vectorizing the group with |
3676 | both operations and then performing a merge. */ | |
3677 | if (SLP_TREE_TWO_OPERATORS (node)) | |
3678 | { | |
3679 | enum tree_code code0 = gimple_assign_rhs_code (stmt); | |
567a3691 | 3680 | enum tree_code ocode = ERROR_MARK; |
355fe088 | 3681 | gimple *ostmt; |
6876e5bc | 3682 | unsigned char *mask = XALLOCAVEC (unsigned char, group_size); |
6876e5bc RB |
3683 | FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, ostmt) |
3684 | if (gimple_assign_rhs_code (ostmt) != code0) | |
3685 | { | |
3686 | mask[i] = 1; | |
6876e5bc RB |
3687 | ocode = gimple_assign_rhs_code (ostmt); |
3688 | } | |
3689 | else | |
3690 | mask[i] = 0; | |
567a3691 | 3691 | if (ocode != ERROR_MARK) |
6876e5bc | 3692 | { |
355fe088 TS |
3693 | vec<gimple *> v0; |
3694 | vec<gimple *> v1; | |
6876e5bc RB |
3695 | unsigned j; |
3696 | tree tmask = NULL_TREE; | |
3697 | vect_transform_stmt (stmt, &si, &grouped_store, node, instance); | |
3698 | v0 = SLP_TREE_VEC_STMTS (node).copy (); | |
3699 | SLP_TREE_VEC_STMTS (node).truncate (0); | |
3700 | gimple_assign_set_rhs_code (stmt, ocode); | |
3701 | vect_transform_stmt (stmt, &si, &grouped_store, node, instance); | |
3702 | gimple_assign_set_rhs_code (stmt, code0); | |
3703 | v1 = SLP_TREE_VEC_STMTS (node).copy (); | |
3704 | SLP_TREE_VEC_STMTS (node).truncate (0); | |
3705 | tree meltype = build_nonstandard_integer_type | |
3706 | (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (vectype))), 1); | |
3707 | tree mvectype = get_same_sized_vectype (meltype, vectype); | |
3708 | unsigned k = 0, l; | |
3709 | for (j = 0; j < v0.length (); ++j) | |
3710 | { | |
3711 | tree *melts = XALLOCAVEC (tree, TYPE_VECTOR_SUBPARTS (vectype)); | |
3712 | for (l = 0; l < TYPE_VECTOR_SUBPARTS (vectype); ++l) | |
3713 | { | |
1ece8d4c | 3714 | if (k >= group_size) |
6876e5bc RB |
3715 | k = 0; |
3716 | melts[l] = build_int_cst | |
3717 | (meltype, mask[k++] * TYPE_VECTOR_SUBPARTS (vectype) + l); | |
3718 | } | |
3719 | tmask = build_vector (mvectype, melts); | |
3720 | ||
3721 | /* ??? Not all targets support a VEC_PERM_EXPR with a | |
3722 | constant mask that would translate to a vec_merge RTX | |
3723 | (with their vec_perm_const_ok). We can either not | |
3724 | vectorize in that case or let veclower do its job. | |
3725 | Unfortunately that isn't too great and at least for | |
3726 | plus/minus we'd eventually like to match targets | |
3727 | vector addsub instructions. */ | |
355fe088 | 3728 | gimple *vstmt; |
6876e5bc RB |
3729 | vstmt = gimple_build_assign (make_ssa_name (vectype), |
3730 | VEC_PERM_EXPR, | |
3731 | gimple_assign_lhs (v0[j]), | |
3732 | gimple_assign_lhs (v1[j]), tmask); | |
3733 | vect_finish_stmt_generation (stmt, vstmt, &si); | |
3734 | SLP_TREE_VEC_STMTS (node).quick_push (vstmt); | |
3735 | } | |
3736 | v0.release (); | |
3737 | v1.release (); | |
3738 | return false; | |
3739 | } | |
3740 | } | |
0d0293ac | 3741 | is_store = vect_transform_stmt (stmt, &si, &grouped_store, node, instance); |
603cca93 RB |
3742 | |
3743 | /* Restore stmt def-types. */ | |
3744 | FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child) | |
3745 | if (SLP_TREE_DEF_TYPE (child) != vect_internal_def) | |
3746 | FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (child), j, stmt) | |
3747 | STMT_VINFO_DEF_TYPE (vinfo_for_stmt (stmt)) = vect_internal_def; | |
3748 | ||
b5aeb3bb | 3749 | return is_store; |
ebfd146a IR |
3750 | } |
3751 | ||
dd34c087 JJ |
3752 | /* Replace scalar calls from SLP node NODE with setting of their lhs to zero. |
3753 | For loop vectorization this is done in vectorizable_call, but for SLP | |
3754 | it needs to be deferred until end of vect_schedule_slp, because multiple | |
3755 | SLP instances may refer to the same scalar stmt. */ | |
3756 | ||
3757 | static void | |
3758 | vect_remove_slp_scalar_calls (slp_tree node) | |
3759 | { | |
355fe088 | 3760 | gimple *stmt, *new_stmt; |
dd34c087 JJ |
3761 | gimple_stmt_iterator gsi; |
3762 | int i; | |
d755c7ef | 3763 | slp_tree child; |
dd34c087 JJ |
3764 | tree lhs; |
3765 | stmt_vec_info stmt_info; | |
3766 | ||
603cca93 | 3767 | if (SLP_TREE_DEF_TYPE (node) != vect_internal_def) |
dd34c087 JJ |
3768 | return; |
3769 | ||
9771b263 | 3770 | FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child) |
d755c7ef | 3771 | vect_remove_slp_scalar_calls (child); |
dd34c087 | 3772 | |
9771b263 | 3773 | FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt) |
dd34c087 JJ |
3774 | { |
3775 | if (!is_gimple_call (stmt) || gimple_bb (stmt) == NULL) | |
3776 | continue; | |
3777 | stmt_info = vinfo_for_stmt (stmt); | |
3778 | if (stmt_info == NULL | |
3779 | || is_pattern_stmt_p (stmt_info) | |
3780 | || !PURE_SLP_STMT (stmt_info)) | |
3781 | continue; | |
3782 | lhs = gimple_call_lhs (stmt); | |
3783 | new_stmt = gimple_build_assign (lhs, build_zero_cst (TREE_TYPE (lhs))); | |
3784 | set_vinfo_for_stmt (new_stmt, stmt_info); | |
3785 | set_vinfo_for_stmt (stmt, NULL); | |
3786 | STMT_VINFO_STMT (stmt_info) = new_stmt; | |
3787 | gsi = gsi_for_stmt (stmt); | |
3788 | gsi_replace (&gsi, new_stmt, false); | |
3789 | SSA_NAME_DEF_STMT (gimple_assign_lhs (new_stmt)) = new_stmt; | |
3790 | } | |
3791 | } | |
ebfd146a | 3792 | |
ff802fa1 IR |
3793 | /* Generate vector code for all SLP instances in the loop/basic block. */ |
3794 | ||
ebfd146a | 3795 | bool |
310213d4 | 3796 | vect_schedule_slp (vec_info *vinfo) |
ebfd146a | 3797 | { |
9771b263 | 3798 | vec<slp_instance> slp_instances; |
ebfd146a | 3799 | slp_instance instance; |
01d8bf07 | 3800 | unsigned int i, vf; |
ebfd146a IR |
3801 | bool is_store = false; |
3802 | ||
310213d4 RB |
3803 | slp_instances = vinfo->slp_instances; |
3804 | if (is_a <loop_vec_info> (vinfo)) | |
3805 | vf = as_a <loop_vec_info> (vinfo)->vectorization_factor; | |
a70d6342 | 3806 | else |
310213d4 | 3807 | vf = 1; |
a70d6342 | 3808 | |
9771b263 | 3809 | FOR_EACH_VEC_ELT (slp_instances, i, instance) |
ebfd146a IR |
3810 | { |
3811 | /* Schedule the tree of INSTANCE. */ | |
3812 | is_store = vect_schedule_slp_instance (SLP_INSTANCE_TREE (instance), | |
a70d6342 | 3813 | instance, vf); |
73fbfcad | 3814 | if (dump_enabled_p ()) |
78c60e3d | 3815 | dump_printf_loc (MSG_NOTE, vect_location, |
e645e942 | 3816 | "vectorizing stmts using SLP.\n"); |
ebfd146a IR |
3817 | } |
3818 | ||
9771b263 | 3819 | FOR_EACH_VEC_ELT (slp_instances, i, instance) |
b5aeb3bb IR |
3820 | { |
3821 | slp_tree root = SLP_INSTANCE_TREE (instance); | |
355fe088 | 3822 | gimple *store; |
b5aeb3bb IR |
3823 | unsigned int j; |
3824 | gimple_stmt_iterator gsi; | |
3825 | ||
c40eced0 RB |
3826 | /* Remove scalar call stmts. Do not do this for basic-block |
3827 | vectorization as not all uses may be vectorized. | |
3828 | ??? Why should this be necessary? DCE should be able to | |
3829 | remove the stmts itself. | |
3830 | ??? For BB vectorization we can as well remove scalar | |
3831 | stmts starting from the SLP tree root if they have no | |
3832 | uses. */ | |
310213d4 | 3833 | if (is_a <loop_vec_info> (vinfo)) |
c40eced0 | 3834 | vect_remove_slp_scalar_calls (root); |
dd34c087 | 3835 | |
9771b263 | 3836 | for (j = 0; SLP_TREE_SCALAR_STMTS (root).iterate (j, &store) |
b5aeb3bb IR |
3837 | && j < SLP_INSTANCE_GROUP_SIZE (instance); j++) |
3838 | { | |
3839 | if (!STMT_VINFO_DATA_REF (vinfo_for_stmt (store))) | |
3840 | break; | |
3841 | ||
a024e70e IR |
3842 | if (is_pattern_stmt_p (vinfo_for_stmt (store))) |
3843 | store = STMT_VINFO_RELATED_STMT (vinfo_for_stmt (store)); | |
b5aeb3bb IR |
3844 | /* Free the attached stmt_vec_info and remove the stmt. */ |
3845 | gsi = gsi_for_stmt (store); | |
3d3f2249 | 3846 | unlink_stmt_vdef (store); |
b5aeb3bb | 3847 | gsi_remove (&gsi, true); |
3d3f2249 | 3848 | release_defs (store); |
b5aeb3bb IR |
3849 | free_stmt_vec_info (store); |
3850 | } | |
3851 | } | |
3852 | ||
ebfd146a IR |
3853 | return is_store; |
3854 | } |