Line data Source code
1 : /* SLP - Pattern matcher on SLP trees
2 : Copyright (C) 2020-2026 Free Software Foundation, Inc.
3 :
4 : This file is part of GCC.
5 :
6 : GCC is free software; you can redistribute it and/or modify it under
7 : the terms of the GNU General Public License as published by the Free
8 : Software Foundation; either version 3, or (at your option) any later
9 : version.
10 :
11 : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 : WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 : for more details.
15 :
16 : You should have received a copy of the GNU General Public License
17 : along with GCC; see the file COPYING3. If not see
18 : <http://www.gnu.org/licenses/>. */
19 :
20 : #include "config.h"
21 : #include "system.h"
22 : #include "coretypes.h"
23 : #include "backend.h"
24 : #include "target.h"
25 : #include "rtl.h"
26 : #include "tree.h"
27 : #include "gimple.h"
28 : #include "tree-pass.h"
29 : #include "ssa.h"
30 : #include "optabs-tree.h"
31 : #include "insn-config.h"
32 : #include "recog.h" /* FIXME: for insn_data */
33 : #include "fold-const.h"
34 : #include "stor-layout.h"
35 : #include "gimple-iterator.h"
36 : #include "cfgloop.h"
37 : #include "tree-vectorizer.h"
38 : #include "langhooks.h"
39 : #include "gimple-walk.h"
40 : #include "dbgcnt.h"
41 : #include "tree-vector-builder.h"
42 : #include "vec-perm-indices.h"
43 : #include "gimple-fold.h"
44 : #include "internal-fn.h"
45 :
46 : /* SLP Pattern matching mechanism.
47 :
48 : This extension to the SLP vectorizer allows one to transform the generated SLP
49 : tree based on any pattern. The difference between this and the normal vect
50 : pattern matcher is that unlike the former, this matcher allows you to match
51 : with instructions that do not belong to the same SSA dominator graph.
52 :
53 : The only requirement that this pattern matcher has is that you are only
54 : only allowed to either match an entire group or none.
55 :
56 : The pattern matcher currently only allows you to perform replacements to
57 : internal functions.
58 :
59 : Once the patterns are matched it is one way, these cannot be undone. It is
60 : currently not supported to match patterns recursively.
61 :
62 : To add a new pattern, implement the vect_pattern class and add the type to
63 : slp_patterns.
64 :
65 : */
66 :
67 : /*******************************************************************************
68 : * vect_pattern class
69 : ******************************************************************************/
70 :
71 : /* Default implementation of recognize that performs matching, validation and
72 : replacement of nodes but that can be overridden if required. */
73 :
74 : static bool
75 5981 : vect_pattern_validate_optab (internal_fn ifn, slp_tree node)
76 : {
77 5981 : tree vectype = SLP_TREE_VECTYPE (node);
78 5981 : if (ifn == IFN_LAST || !vectype)
79 : return false;
80 :
81 5981 : if (dump_enabled_p ())
82 716 : dump_printf_loc (MSG_NOTE, vect_location,
83 : "Found %s pattern in SLP tree\n",
84 : internal_fn_name (ifn));
85 :
86 5981 : if (direct_internal_fn_supported_p (ifn, vectype, OPTIMIZE_FOR_SPEED))
87 : {
88 1114 : if (dump_enabled_p ())
89 24 : dump_printf_loc (MSG_NOTE, vect_location,
90 : "Target supports %s vectorization with mode %T\n",
91 : internal_fn_name (ifn), vectype);
92 : }
93 : else
94 : {
95 4867 : if (dump_enabled_p ())
96 : {
97 692 : if (!vectype)
98 : dump_printf_loc (MSG_NOTE, vect_location,
99 : "Target does not support vector type for %G\n",
100 : STMT_VINFO_STMT (SLP_TREE_REPRESENTATIVE (node)));
101 : else
102 692 : dump_printf_loc (MSG_NOTE, vect_location,
103 : "Target does not support %s for vector type "
104 : "%T\n", internal_fn_name (ifn), vectype);
105 : }
106 : return false;
107 : }
108 : return true;
109 : }
110 :
111 : /*******************************************************************************
112 : * General helper types
113 : ******************************************************************************/
114 :
115 : /* The COMPLEX_OPERATION enum denotes the possible pair of operations that can
116 : be matched when looking for expressions that we are interested matching for
117 : complex numbers addition and mla. */
118 :
119 : typedef enum _complex_operation : unsigned {
120 : PLUS_PLUS,
121 : MINUS_PLUS,
122 : PLUS_MINUS,
123 : MULT_MULT,
124 : CMPLX_NONE
125 : } complex_operation_t;
126 :
127 : /*******************************************************************************
128 : * General helper functions
129 : ******************************************************************************/
130 :
131 : /* Helper function of linear_loads_p that checks to see if the load permutation
132 : is sequential and in monotonically increasing order of loads with no gaps.
133 : */
134 :
135 : static inline complex_perm_kinds_t
136 2406 : is_linear_load_p (load_permutation_t loads)
137 : {
138 2406 : if (loads.length() == 0)
139 : return PERM_UNKNOWN;
140 :
141 2406 : if (loads.length () == 1)
142 0 : return loads[0] == 0 ? PERM_EVENEVEN : PERM_ODDODD;
143 :
144 2406 : vec_perm_builder builder;
145 2406 : builder.new_vector (loads.length (), loads.length (), 1);
146 8653 : for (unsigned load : loads)
147 : {
148 6350 : if (load >= loads.length ())
149 2406 : return PERM_UNKNOWN;
150 6247 : builder.quick_push (load);
151 : }
152 :
153 2303 : vec_perm_indices indices (builder, 1, loads.length ());
154 :
155 2303 : if (indices.series_p (0, 2, 1, 2)
156 2303 : && indices.series_p (1, 2, 1, 2))
157 359 : return PERM_ODDODD;
158 :
159 1944 : if (indices.series_p (0, 2, 0, 2)
160 1944 : && indices.series_p (1, 2, 0, 2))
161 1028 : return PERM_EVENEVEN;
162 :
163 916 : if (indices.series_p (0, 1, 0, 1))
164 : return PERM_EVENODD;
165 :
166 875 : if (indices.series_p (0, 2, 1, 2)
167 875 : && indices.series_p (1, 2, 0, 2))
168 850 : return PERM_ODDEVEN;
169 :
170 : return PERM_UNKNOWN;
171 4709 : }
172 :
173 : /* Combine complex_perm_kinds A and B into a new permute kind that describes the
174 : resulting operation. */
175 :
176 : static inline complex_perm_kinds_t
177 19513 : vect_merge_perms (complex_perm_kinds_t a, complex_perm_kinds_t b)
178 : {
179 19513 : if (a == b)
180 : return a;
181 :
182 16981 : if (a == PERM_TOP)
183 : return b;
184 :
185 2062 : if (b == PERM_TOP)
186 : return a;
187 :
188 : return PERM_UNKNOWN;
189 : }
190 :
191 : /* Check to see if all loads rooted in ROOT are linear. Linearity is
192 : defined as having no gaps between values loaded. */
193 :
194 : static complex_perm_kinds_t
195 40173 : linear_loads_p (slp_tree_to_load_perm_map_t *perm_cache, slp_tree root)
196 : {
197 40173 : if (!root)
198 : return PERM_UNKNOWN;
199 :
200 40168 : unsigned i;
201 40168 : complex_perm_kinds_t *tmp;
202 :
203 40168 : if ((tmp = perm_cache->get (root)) != NULL)
204 15661 : return *tmp;
205 :
206 24507 : complex_perm_kinds_t retval = PERM_UNKNOWN;
207 24507 : perm_cache->put (root, retval);
208 :
209 : /* If it's a load node, then just read the load permute. */
210 24507 : if (SLP_TREE_DEF_TYPE (root) == vect_internal_def
211 21155 : && !SLP_TREE_PERMUTE_P (root)
212 18137 : && STMT_VINFO_DATA_REF (SLP_TREE_REPRESENTATIVE (root))
213 3774 : && DR_IS_READ (STMT_VINFO_DATA_REF (SLP_TREE_REPRESENTATIVE (root))))
214 : {
215 3774 : if (SLP_TREE_LOAD_PERMUTATION (root).exists ())
216 2406 : retval = is_linear_load_p (SLP_TREE_LOAD_PERMUTATION (root));
217 : else
218 : retval = PERM_EVENODD;
219 3774 : perm_cache->put (root, retval);
220 3774 : return retval;
221 : }
222 20733 : else if (SLP_TREE_DEF_TYPE (root) != vect_internal_def)
223 : {
224 3352 : retval = PERM_TOP;
225 3352 : perm_cache->put (root, retval);
226 3352 : return retval;
227 : }
228 :
229 : complex_perm_kinds_t kind = PERM_TOP;
230 :
231 : slp_tree child;
232 20010 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (root), i, child)
233 : {
234 19513 : complex_perm_kinds_t res = linear_loads_p (perm_cache, child);
235 19513 : kind = vect_merge_perms (kind, res);
236 : /* Unknown and Top are not valid on blends as they produce no permute. */
237 19513 : retval = kind;
238 19513 : if (kind == PERM_UNKNOWN || kind == PERM_TOP)
239 : return retval;
240 : }
241 :
242 497 : retval = kind;
243 :
244 497 : perm_cache->put (root, retval);
245 497 : return retval;
246 : }
247 :
248 :
249 : /* This function attempts to make a node rooted in NODE is linear. If the node
250 : if already linear than the node itself is returned in RESULT.
251 :
252 : If the node is not linear then a new VEC_PERM_EXPR node is created with a
253 : lane permute that when applied will make the node linear. If such a
254 : permute cannot be created then FALSE is returned from the function.
255 :
256 : Here linearity is defined as having a sequential, monotically increasing
257 : load position inside the load permute generated by the loads reachable from
258 : NODE. */
259 :
260 : static slp_tree
261 0 : vect_build_swap_evenodd_node (slp_tree node)
262 : {
263 : /* Attempt to linearise the permute. */
264 0 : lane_permutation_t zipped;
265 0 : zipped.create (SLP_TREE_LANES (node));
266 :
267 0 : for (unsigned x = 0; x < SLP_TREE_LANES (node); x+=2)
268 : {
269 0 : zipped.quick_push (std::make_pair (0, x+1));
270 0 : zipped.quick_push (std::make_pair (0, x));
271 : }
272 :
273 : /* Create the new permute node and store it instead. */
274 0 : slp_tree vnode = vect_create_new_slp_node (1, VEC_PERM_EXPR);
275 0 : SLP_TREE_LANE_PERMUTATION (vnode) = zipped;
276 0 : SLP_TREE_VECTYPE (vnode) = SLP_TREE_VECTYPE (node);
277 0 : SLP_TREE_CHILDREN (vnode).quick_push (node);
278 0 : SLP_TREE_REF_COUNT (vnode) = 1;
279 0 : SLP_TREE_LANES (vnode) = SLP_TREE_LANES (node);
280 0 : SLP_TREE_REF_COUNT (node)++;
281 0 : return vnode;
282 : }
283 :
284 : /* Checks to see of the expression represented by NODE is a gimple assign with
285 : code CODE. */
286 :
287 : static inline bool
288 11066544 : vect_match_expression_p (slp_tree node, code_helper code)
289 : {
290 11066544 : if (!node
291 10118218 : || SLP_TREE_PERMUTE_P (node)
292 10054975 : || !SLP_TREE_REPRESENTATIVE (node))
293 : return false;
294 :
295 9068164 : gimple* expr = STMT_VINFO_STMT (SLP_TREE_REPRESENTATIVE (node));
296 9068164 : if (is_gimple_assign (expr)
297 7745453 : && code.is_tree_code ()
298 16804195 : && gimple_assign_rhs_code (expr) == (tree_code) code)
299 : return true;
300 8516454 : if (is_a <gcall *> (expr)
301 95360 : && !code.is_tree_code ()
302 8516502 : && gimple_call_combined_fn (expr) == (combined_fn) code)
303 6 : return true;
304 :
305 : return false;
306 : }
307 :
308 : /* Check if the given lane permute in PERMUTES matches an alternating sequence
309 : of {even odd even odd ...}. This to account for unrolled loops. Further
310 : mode there resulting permute must be linear. */
311 :
312 : static inline bool
313 8157 : vect_check_evenodd_blend (lane_permutation_t &permutes,
314 : unsigned even, unsigned odd)
315 : {
316 8157 : if (permutes.length () == 0
317 7901 : || permutes.length () % 2 != 0)
318 : return false;
319 :
320 7879 : unsigned val[2] = {even, odd};
321 7879 : unsigned seed = 0;
322 26700 : for (unsigned i = 0; i < permutes.length (); i++)
323 19142 : if (permutes[i].first != val[i % 2]
324 19142 : || permutes[i].second != seed++)
325 : return false;
326 :
327 : return true;
328 : }
329 :
330 : /* This function will match the two gimple expressions representing NODE1 and
331 : NODE2 in parallel and returns the pair operation that represents the two
332 : expressions in the two statements.
333 :
334 : If match is successful then the corresponding complex_operation is
335 : returned and the arguments to the two matched operations are returned in OPS.
336 :
337 : If TWO_OPERANDS it is expected that the LANES of the parent VEC_PERM select
338 : from the two nodes alternatingly.
339 :
340 : If unsuccessful then CMPLX_NONE is returned and OPS is untouched.
341 :
342 : e.g. the following gimple statements
343 :
344 : stmt 0 _39 = _37 + _12;
345 : stmt 1 _6 = _38 - _36;
346 :
347 : will return PLUS_MINUS along with OPS containing {_37, _12, _38, _36}.
348 : */
349 :
350 : static complex_operation_t
351 1654069 : vect_detect_pair_op (slp_tree node1, slp_tree node2, lane_permutation_t &lanes,
352 : bool two_operands = true, vec<slp_tree> *ops = NULL)
353 : {
354 1654069 : complex_operation_t result = CMPLX_NONE;
355 :
356 1654069 : if (vect_match_expression_p (node1, MINUS_EXPR)
357 48624 : && vect_match_expression_p (node2, PLUS_EXPR)
358 1657907 : && (!two_operands || vect_check_evenodd_blend (lanes, 0, 1)))
359 : result = MINUS_PLUS;
360 1650573 : else if (vect_match_expression_p (node1, PLUS_EXPR)
361 164562 : && vect_match_expression_p (node2, MINUS_EXPR)
362 1654892 : && (!two_operands || vect_check_evenodd_blend (lanes, 0, 1)))
363 : result = PLUS_MINUS;
364 1646511 : else if (vect_match_expression_p (node1, PLUS_EXPR)
365 1646511 : && vect_match_expression_p (node2, PLUS_EXPR))
366 : result = PLUS_PLUS;
367 1643464 : else if (vect_match_expression_p (node1, MULT_EXPR)
368 1643464 : && vect_match_expression_p (node2, MULT_EXPR))
369 5322 : result = MULT_MULT;
370 :
371 1654069 : if (result != CMPLX_NONE && ops != NULL)
372 : {
373 15890 : if (two_operands)
374 : {
375 15890 : auto l0node = SLP_TREE_CHILDREN (node1);
376 15890 : auto l1node = SLP_TREE_CHILDREN (node2);
377 :
378 : /* Check if the tree is connected as we expect it. */
379 23914 : if (!((l0node[0] == l1node[0] && l0node[1] == l1node[1])
380 8090 : || (l0node[0] == l1node[1] && l0node[1] == l1node[0])))
381 1654069 : return CMPLX_NONE;
382 : }
383 7860 : ops->safe_push (node1);
384 7860 : ops->safe_push (node2);
385 : }
386 : return result;
387 : }
388 :
389 : /* Overload of vect_detect_pair_op that matches against the representative
390 : statements in the children of NODE. It is expected that NODE has exactly
391 : two children and when TWO_OPERANDS then NODE must be a VEC_PERM. */
392 :
393 : static complex_operation_t
394 5573044 : vect_detect_pair_op (slp_tree node, bool two_operands = true,
395 : vec<slp_tree> *ops = NULL)
396 : {
397 5573044 : if (!two_operands && SLP_TREE_PERMUTE_P (node))
398 : return CMPLX_NONE;
399 :
400 5573044 : if (SLP_TREE_CHILDREN (node).length () != 2)
401 : return CMPLX_NONE;
402 :
403 1654069 : vec<slp_tree> children = SLP_TREE_CHILDREN (node);
404 1654069 : lane_permutation_t &lanes = SLP_TREE_LANE_PERMUTATION (node);
405 :
406 1654069 : return vect_detect_pair_op (children[0], children[1], lanes, two_operands,
407 1654069 : ops);
408 : }
409 :
410 : /*******************************************************************************
411 : * complex_pattern class
412 : ******************************************************************************/
413 :
414 : /* SLP Complex Numbers pattern matching.
415 :
416 : As an example, the following simple loop:
417 :
418 : double a[restrict N]; double b[restrict N]; double c[restrict N];
419 :
420 : for (int i=0; i < N; i+=2)
421 : {
422 : c[i] = a[i] - b[i+1];
423 : c[i+1] = a[i+1] + b[i];
424 : }
425 :
426 : which represents a complex addition on with a rotation of 90* around the
427 : argand plane. i.e. if `a` and `b` were complex numbers then this would be the
428 : same as `a + (b * I)`.
429 :
430 : Here the expressions for `c[i]` and `c[i+1]` are independent but have to be
431 : both recognized in order for the pattern to work. As an SLP tree this is
432 : represented as
433 :
434 : +--------------------------------+
435 : | stmt 0 *_9 = _10; |
436 : | stmt 1 *_15 = _16; |
437 : +--------------------------------+
438 : |
439 : |
440 : v
441 : +--------------------------------+
442 : | stmt 0 _10 = _4 - _8; |
443 : | stmt 1 _16 = _12 + _14; |
444 : | lane permutation { 0[0] 1[1] } |
445 : +--------------------------------+
446 : | |
447 : | |
448 : | |
449 : +-----+ | | +-----+
450 : | | | | | |
451 : +-----| { } |<-----+ +----->| { } --------+
452 : | | | +------------------| | |
453 : | +-----+ | +-----+ |
454 : | | | |
455 : | | | |
456 : | +------|------------------+ |
457 : | | | |
458 : v v v v
459 : +--------------------------+ +--------------------------------+
460 : | stmt 0 _8 = *_7; | | stmt 0 _4 = *_3; |
461 : | stmt 1 _14 = *_13; | | stmt 1 _12 = *_11; |
462 : | load permutation { 1 0 } | | load permutation { 0 1 } |
463 : +--------------------------+ +--------------------------------+
464 :
465 : The pattern matcher allows you to replace both statements 0 and 1 or none at
466 : all. Because this operation is a two operands operation the actual nodes
467 : being replaced are those in the { } nodes. The actual scalar statements
468 : themselves are not replaced or used during the matching but instead the
469 : SLP_TREE_REPRESENTATIVE statements are inspected. You are also allowed to
470 : replace and match on any number of nodes.
471 :
472 : Because the pattern matcher matches on the representative statement for the
473 : SLP node the case of two_operators it allows you to match the children of the
474 : node. This is done using the method `recognize ()`.
475 :
476 : */
477 :
478 : /* The complex_pattern class contains common code for pattern matchers that work
479 : on complex numbers. These provide functionality to allow de-construction and
480 : validation of sequences depicting/transforming REAL and IMAG pairs. */
481 :
482 : class complex_pattern : public vect_pattern
483 : {
484 : protected:
485 : auto_vec<slp_tree> m_workset;
486 32 : complex_pattern (slp_tree *node, vec<slp_tree> *m_ops, internal_fn ifn)
487 64 : : vect_pattern (node, m_ops, ifn)
488 : {
489 32 : this->m_workset.safe_push (*node);
490 32 : }
491 :
492 : public:
493 : void build (vec_info *) override;
494 :
495 : static internal_fn
496 : matches (complex_operation_t op, slp_tree_to_load_perm_map_t *, slp_tree *,
497 : vec<slp_tree> *);
498 : };
499 :
500 : /* Create a replacement pattern statement for each node in m_node and inserts
501 : the new statement into m_node as the new representative statement. The old
502 : statement is marked as being in a pattern defined by the new statement. The
503 : statement is created as call to internal function IFN with m_num_args
504 : arguments.
505 :
506 : Furthermore the new pattern is also added to the vectorization information
507 : structure VINFO and the old statement STMT_INFO is marked as unused while
508 : the new statement is marked as used and the number of SLP uses of the new
509 : statement is incremented.
510 :
511 : The newly created SLP nodes are marked as SLP only and will be dissolved
512 : if SLP is aborted.
513 :
514 : The newly created gimple call is returned and the BB remains unchanged.
515 :
516 : This default method is designed to only match against simple operands where
517 : all the input and output types are the same.
518 : */
519 :
520 : void
521 32 : complex_pattern::build (vec_info *vinfo)
522 : {
523 32 : stmt_vec_info stmt_info;
524 :
525 32 : auto_vec<tree> args;
526 32 : args.create (this->m_num_args);
527 32 : args.quick_grow_cleared (this->m_num_args);
528 32 : slp_tree node;
529 32 : unsigned ix;
530 32 : stmt_vec_info call_stmt_info;
531 32 : gcall *call_stmt = NULL;
532 :
533 : /* Now modify the nodes themselves. */
534 128 : FOR_EACH_VEC_ELT (this->m_workset, ix, node)
535 : {
536 : /* Calculate the location of the statement in NODE to replace. */
537 32 : stmt_info = SLP_TREE_SCALAR_STMTS (node)[0];
538 32 : gimple* old_stmt = STMT_VINFO_STMT (stmt_info);
539 32 : tree lhs_old_stmt = gimple_get_lhs (old_stmt);
540 32 : tree type = TREE_TYPE (lhs_old_stmt);
541 :
542 : /* Create the argument set for use by gimple_build_call_internal_vec. */
543 112 : for (unsigned i = 0; i < this->m_num_args; i++)
544 80 : args[i] = lhs_old_stmt;
545 :
546 : /* Create the new pattern statements. */
547 32 : call_stmt = gimple_build_call_internal_vec (this->m_ifn, args);
548 32 : tree var = make_temp_ssa_name (type, call_stmt, "slp_patt");
549 32 : gimple_call_set_lhs (call_stmt, var);
550 32 : gimple_set_location (call_stmt, gimple_location (old_stmt));
551 32 : gimple_call_set_nothrow (call_stmt, true);
552 :
553 : /* Adjust the book-keeping for the new and old statements for use during
554 : SLP. This is required to get the right VF and statement during SLP
555 : analysis. These changes are created after relevancy has been set for
556 : the nodes as such we need to manually update them. Any changes will be
557 : undone if SLP is cancelled. */
558 32 : call_stmt_info
559 32 : = vinfo->add_pattern_stmt (call_stmt, vect_orig_stmt (stmt_info));
560 :
561 : /* Make sure to mark the representative statement pure_slp and
562 : relevant and transfer reduction info. */
563 32 : STMT_VINFO_RELEVANT (call_stmt_info) = vect_used_in_scope;
564 32 : STMT_SLP_TYPE (call_stmt_info) = pure_slp;
565 :
566 32 : gimple_set_bb (call_stmt, gimple_bb (stmt_info->stmt));
567 32 : STMT_VINFO_VECTYPE (call_stmt_info) = SLP_TREE_VECTYPE (node);
568 :
569 : /* Since we are replacing all the statements in the group with the same
570 : thing it doesn't really matter. So just set it every time a new stmt
571 : is created. */
572 32 : SLP_TREE_REPRESENTATIVE (node) = call_stmt_info;
573 32 : SLP_TREE_LANE_PERMUTATION (node).release ();
574 32 : SLP_TREE_CODE (node) = ERROR_MARK;
575 : }
576 32 : }
577 :
578 : /*******************************************************************************
579 : * complex_add_pattern class
580 : ******************************************************************************/
581 :
582 : class complex_add_pattern : public complex_pattern
583 : {
584 : protected:
585 0 : complex_add_pattern (slp_tree *node, vec<slp_tree> *m_ops, internal_fn ifn)
586 0 : : complex_pattern (node, m_ops, ifn)
587 : {
588 0 : this->m_num_args = 2;
589 : }
590 :
591 : public:
592 : void build (vec_info *) final override;
593 : static internal_fn
594 : matches (complex_operation_t op, slp_tree_to_load_perm_map_t *,
595 : slp_compat_nodes_map_t *, slp_tree *, vec<slp_tree> *);
596 :
597 : static vect_pattern*
598 : recognize (slp_tree_to_load_perm_map_t *, slp_compat_nodes_map_t *,
599 : slp_tree *);
600 :
601 : static vect_pattern*
602 0 : mkInstance (slp_tree *node, vec<slp_tree> *m_ops, internal_fn ifn)
603 : {
604 0 : return new complex_add_pattern (node, m_ops, ifn);
605 : }
606 : };
607 :
608 : /* Perform a replacement of the detected complex add pattern with the new
609 : instruction sequences. */
610 :
611 : void
612 0 : complex_add_pattern::build (vec_info *vinfo)
613 : {
614 0 : SLP_TREE_CHILDREN (*this->m_node).reserve_exact (2);
615 :
616 0 : slp_tree node = this->m_ops[0];
617 0 : vec<slp_tree> children = SLP_TREE_CHILDREN (node);
618 :
619 : /* First re-arrange the children. */
620 0 : SLP_TREE_CHILDREN (*this->m_node)[0] = children[0];
621 0 : SLP_TREE_CHILDREN (*this->m_node)[1] =
622 0 : vect_build_swap_evenodd_node (children[1]);
623 :
624 0 : SLP_TREE_REF_COUNT (SLP_TREE_CHILDREN (*this->m_node)[0])++;
625 0 : SLP_TREE_REF_COUNT (SLP_TREE_CHILDREN (*this->m_node)[1])++;
626 0 : vect_free_slp_tree (this->m_ops[0]);
627 0 : vect_free_slp_tree (this->m_ops[1]);
628 :
629 0 : complex_pattern::build (vinfo);
630 0 : }
631 :
632 : /* Pattern matcher for trying to match complex addition pattern in SLP tree.
633 :
634 : If no match is found then IFN is set to IFN_LAST.
635 : This function matches the patterns shaped as:
636 :
637 : c[i] = a[i] - b[i+1];
638 : c[i+1] = a[i+1] + b[i];
639 :
640 : If a match occurred then TRUE is returned, else FALSE. The initial match is
641 : expected to be in OP1 and the initial match operands in args0. */
642 :
643 : internal_fn
644 5557802 : complex_add_pattern::matches (complex_operation_t op,
645 : slp_tree_to_load_perm_map_t *perm_cache,
646 : slp_compat_nodes_map_t * /* compat_cache */,
647 : slp_tree *node, vec<slp_tree> *ops)
648 : {
649 5557802 : internal_fn ifn = IFN_LAST;
650 :
651 : /* Find the two components. Rotation in the complex plane will modify
652 : the operations:
653 :
654 : * Rotation 0: + +
655 : * Rotation 90: - +
656 : * Rotation 180: - -
657 : * Rotation 270: + -
658 :
659 : Rotation 0 and 180 can be handled by normal SIMD code, so we don't need
660 : to care about them here. */
661 5557802 : if (op == MINUS_PLUS)
662 : ifn = IFN_COMPLEX_ADD_ROT90;
663 5554341 : else if (op == PLUS_MINUS)
664 : ifn = IFN_COMPLEX_ADD_ROT270;
665 : else
666 : return ifn;
667 :
668 : /* verify that there is a permute, otherwise this isn't a pattern we
669 : we support. */
670 7499 : gcc_assert (ops->length () == 2);
671 :
672 7499 : vec<slp_tree> children = SLP_TREE_CHILDREN ((*ops)[0]);
673 :
674 : /* First node must be unpermuted. */
675 7499 : if (linear_loads_p (perm_cache, children[0]) != PERM_EVENODD)
676 : return IFN_LAST;
677 :
678 : /* Second node must be permuted. */
679 539 : if (linear_loads_p (perm_cache, children[1]) != PERM_ODDEVEN)
680 : return IFN_LAST;
681 :
682 352 : if (!vect_pattern_validate_optab (ifn, *node))
683 352 : return IFN_LAST;
684 :
685 : return ifn;
686 : }
687 :
688 : /* Attempt to recognize a complex add pattern. */
689 :
690 : vect_pattern*
691 0 : complex_add_pattern::recognize (slp_tree_to_load_perm_map_t *perm_cache,
692 : slp_compat_nodes_map_t *compat_cache,
693 : slp_tree *node)
694 : {
695 0 : auto_vec<slp_tree> ops;
696 0 : complex_operation_t op
697 0 : = vect_detect_pair_op (*node, true, &ops);
698 0 : internal_fn ifn
699 0 : = complex_add_pattern::matches (op, perm_cache, compat_cache, node, &ops);
700 0 : if (ifn == IFN_LAST)
701 : return NULL;
702 :
703 0 : return new complex_add_pattern (node, &ops, ifn);
704 0 : }
705 :
706 : /*******************************************************************************
707 : * complex_mul_pattern
708 : ******************************************************************************/
709 :
710 : /* Helper function to check if PERM is KIND or PERM_TOP. */
711 :
712 : static inline bool
713 677 : is_eq_or_top (slp_tree_to_load_perm_map_t *perm_cache,
714 : slp_tree op1, complex_perm_kinds_t kind1,
715 : slp_tree op2, complex_perm_kinds_t kind2)
716 : {
717 677 : complex_perm_kinds_t perm1 = linear_loads_p (perm_cache, op1);
718 677 : if (perm1 != kind1 && perm1 != PERM_TOP)
719 : return false;
720 :
721 258 : complex_perm_kinds_t perm2 = linear_loads_p (perm_cache, op2);
722 258 : if (perm2 != kind2 && perm2 != PERM_TOP)
723 0 : return false;
724 :
725 : return true;
726 : }
727 :
728 : enum _conj_status { CONJ_NONE, CONJ_FST, CONJ_SND };
729 :
730 : static inline bool
731 512 : compatible_complex_nodes_p (slp_compat_nodes_map_t *compat_cache,
732 : slp_tree a, int *pa, slp_tree b, int *pb)
733 : {
734 512 : bool *tmp;
735 512 : std::pair<slp_tree, slp_tree> key = std::make_pair(a, b);
736 512 : if ((tmp = compat_cache->get (key)) != NULL)
737 34 : return *tmp;
738 :
739 478 : compat_cache->put (key, false);
740 :
741 534 : if (SLP_TREE_CHILDREN (a).length () != SLP_TREE_CHILDREN (b).length ())
742 : return false;
743 :
744 474 : if (SLP_TREE_DEF_TYPE (a) != SLP_TREE_DEF_TYPE (b))
745 : return false;
746 :
747 : /* Only internal nodes can be loads, as such we can't check further if they
748 : are externals. */
749 474 : if (SLP_TREE_DEF_TYPE (a) != vect_internal_def)
750 : {
751 94 : unsigned group_size = SLP_TREE_LANES (a);
752 282 : gcc_assert (SLP_TREE_SCALAR_OPS (a).length () == group_size
753 : && SLP_TREE_SCALAR_OPS (b).length () == group_size);
754 286 : for (unsigned i = 0; i < group_size; i++)
755 : {
756 194 : tree op1 = SLP_TREE_SCALAR_OPS (a)[pa[i % 2]];
757 194 : tree op2 = SLP_TREE_SCALAR_OPS (b)[pb[i % 2]];
758 194 : if (!operand_equal_p (op1, op2, 0))
759 : return false;
760 : }
761 :
762 92 : compat_cache->put (key, true);
763 92 : return true;
764 : }
765 :
766 380 : if (SLP_TREE_PERMUTE_P (a) != SLP_TREE_PERMUTE_P (b))
767 : return false;
768 380 : else if (SLP_TREE_PERMUTE_P (a))
769 : ;
770 : else
771 : {
772 380 : auto a_stmt = STMT_VINFO_STMT (SLP_TREE_REPRESENTATIVE (a));
773 380 : auto b_stmt = STMT_VINFO_STMT (SLP_TREE_REPRESENTATIVE (b));
774 :
775 380 : if (gimple_code (a_stmt) != gimple_code (b_stmt))
776 : return false;
777 :
778 : /* code, children, type, externals, loads, constants */
779 380 : if (gimple_num_args (a_stmt) != gimple_num_args (b_stmt))
780 : return false;
781 :
782 : /* At this point, a and b are known to be the same gimple operations. */
783 380 : if (is_gimple_call (a_stmt))
784 : {
785 0 : if (!compatible_calls_p (dyn_cast <gcall *> (a_stmt),
786 : dyn_cast <gcall *> (b_stmt), false))
787 : return false;
788 : }
789 380 : else if (!is_gimple_assign (a_stmt))
790 : return false;
791 : else
792 : {
793 380 : tree_code acode = gimple_assign_rhs_code (a_stmt);
794 380 : tree_code bcode = gimple_assign_rhs_code (b_stmt);
795 380 : if ((acode == REALPART_EXPR || acode == IMAGPART_EXPR)
796 270 : && (bcode == REALPART_EXPR || bcode == IMAGPART_EXPR)
797 650 : && operand_equal_p (TREE_OPERAND (gimple_assign_rhs1 (a_stmt), 0),
798 270 : TREE_OPERAND (gimple_assign_rhs1 (b_stmt), 0)))
799 : return true;
800 :
801 110 : if (acode != bcode)
802 : return false;
803 : }
804 :
805 110 : if (!STMT_VINFO_DATA_REF (SLP_TREE_REPRESENTATIVE (a))
806 84 : || !STMT_VINFO_DATA_REF (SLP_TREE_REPRESENTATIVE (b)))
807 : {
808 78 : for (unsigned i = 0; i < gimple_num_args (a_stmt); i++)
809 : {
810 52 : tree t1 = gimple_arg (a_stmt, i);
811 52 : tree t2 = gimple_arg (b_stmt, i);
812 52 : if (TREE_CODE (t1) != TREE_CODE (t2))
813 : return false;
814 :
815 : /* If SSA name then we will need to inspect the children
816 : so we can punt here. */
817 52 : if (TREE_CODE (t1) == SSA_NAME)
818 38 : continue;
819 :
820 14 : if (!operand_equal_p (t1, t2, 0))
821 : return false;
822 : }
823 : }
824 : else
825 : {
826 84 : auto dr1 = STMT_VINFO_DATA_REF (SLP_TREE_REPRESENTATIVE (a));
827 84 : auto dr2 = STMT_VINFO_DATA_REF (SLP_TREE_REPRESENTATIVE (b));
828 : /* Don't check the last dimension as that's checked by the lineary
829 : checks. This check is also much stricter than what we need
830 : because it doesn't consider loading from adjacent elements
831 : in the same struct as loading from the same base object.
832 : But for now, I'll play it safe. */
833 84 : if (!same_data_refs (dr1, dr2, 1))
834 : return false;
835 : }
836 : }
837 :
838 134 : for (unsigned i = 0; i < SLP_TREE_CHILDREN (a).length (); i++)
839 : {
840 52 : if (!compatible_complex_nodes_p (compat_cache,
841 52 : SLP_TREE_CHILDREN (a)[i], pa,
842 52 : SLP_TREE_CHILDREN (b)[i], pb))
843 : return false;
844 : }
845 :
846 82 : compat_cache->put (key, true);
847 82 : return true;
848 : }
849 :
850 :
851 : /* Check to see if the operands to two multiplies, 2 each in ALL_OPS, match
852 : a complex multiplication or complex multiply-and-accumulate or complex
853 : multiply-and-subtract pattern. Do this using the permute cache PERM_CACHE
854 : and the combination compatibility list COMPAT_CACHE. If the operation is
855 : successful the matching operands are returned in OPS and _STATUS indicates
856 : if the operation matched includes a conjugate of one of the operands. If
857 : the operation succeeds True is returned, otherwise False and the values in
858 : ops are meaningless. */
859 : static inline bool
860 4527 : vect_validate_multiplication (slp_tree_to_load_perm_map_t *perm_cache,
861 : slp_compat_nodes_map_t *compat_cache,
862 : const slp_tree *all_ops,
863 : const unsigned *op_index, bool subtract,
864 : unsigned perm, vec<slp_tree> &ops,
865 : enum _conj_status *_status)
866 : {
867 4527 : enum _conj_status stats = CONJ_NONE;
868 4527 : gcc_assert (perm < 2);
869 :
870 : /* The complex operations can occur in two layouts and two permute sequences
871 : so declare them and re-use them. */
872 4527 : int styles[][4] = { { 0, 2, 1, 3} /* {L1, R1} + {L2, R2}. */
873 : , { 0, 3, 1, 2} /* {L1, R2} + {L2, R1}. */
874 : };
875 :
876 : /* Now for the corresponding permutes that go with these values. */
877 4527 : complex_perm_kinds_t perms[][4]
878 : = { { PERM_EVENEVEN, PERM_ODDODD, PERM_EVENODD, PERM_ODDEVEN }
879 : , { PERM_EVENODD, PERM_ODDEVEN, PERM_EVENEVEN, PERM_ODDODD }
880 : };
881 :
882 : /* These permutes are used during comparisons of externals on which
883 : we require strict equality. */
884 4527 : int cq[][4][2]
885 : = { { { 0, 0 }, { 1, 1 }, { 0, 1 }, { 1, 0 } }
886 : , { { 0, 1 }, { 1, 0 }, { 0, 0 }, { 1, 1 } }
887 : };
888 :
889 : /* Default to style 0, most operations use this one. */
890 4527 : int style = 0;
891 :
892 : /* Create the combined inputs after remapping. */
893 4527 : ops.create (4);
894 27162 : for (unsigned i = 0; i < 4; ++i)
895 18108 : ops.quick_push (all_ops[op_index[i]]);
896 :
897 : /* Check if we have a negate operation, if so absorb the node and continue
898 : looking. */
899 4527 : bool neg0 = vect_match_expression_p (ops[2], NEGATE_EXPR);
900 4527 : bool neg1 = vect_match_expression_p (ops[3], NEGATE_EXPR);
901 :
902 : /* Determine which style we're looking at. We only have different ones
903 : whenever a conjugate is involved. */
904 4527 : if (neg0 && neg1)
905 : ;
906 4527 : else if (neg0)
907 : {
908 1 : ops[2] = SLP_TREE_CHILDREN (ops[2])[0];
909 1 : stats = CONJ_FST;
910 1 : if (subtract)
911 0 : perm = 0;
912 : }
913 4526 : else if (neg1)
914 : {
915 21 : ops[3] = SLP_TREE_CHILDREN (ops[3])[0];
916 21 : stats = CONJ_SND;
917 21 : perm = 1;
918 : }
919 :
920 4527 : *_status = stats;
921 :
922 : /* Extract out the elements to check. */
923 4527 : slp_tree op0 = ops[styles[style][0]];
924 4527 : slp_tree op1 = ops[styles[style][1]];
925 4527 : slp_tree op2 = ops[styles[style][2]];
926 4527 : slp_tree op3 = ops[styles[style][3]];
927 :
928 : /* Do cheapest test first. If failed no need to analyze further. */
929 4527 : if (linear_loads_p (perm_cache, op0) != perms[perm][0]
930 1252 : || linear_loads_p (perm_cache, op1) != perms[perm][1]
931 5204 : || !is_eq_or_top (perm_cache, op2, perms[perm][2], op3, perms[perm][3]))
932 : return false;
933 :
934 258 : return compatible_complex_nodes_p (compat_cache, op0, cq[perm][0], op1,
935 258 : cq[perm][1])
936 460 : && compatible_complex_nodes_p (compat_cache, op2, cq[perm][2], op3,
937 202 : cq[perm][3]);
938 : }
939 :
940 : /* Try to validate LEFT_OP and RIGHT_OP as the operands of a complex
941 : multiplication. Since MULT_EXPR is commutative, try all combinations of
942 : swapping the operands of each multiplication. If a match is found, set OPS
943 : and STATUS for the matching order. */
944 :
945 : static inline bool
946 1213 : vect_validate_multiplication_commutative (slp_tree_to_load_perm_map_t *perm_cache,
947 : slp_compat_nodes_map_t *compat_cache,
948 : vec<slp_tree> &left_op,
949 : vec<slp_tree> &right_op,
950 : bool subtract, vec<slp_tree> &ops,
951 : enum _conj_status *status)
952 : {
953 1213 : unsigned perm = subtract ? 1 : 0;
954 1213 : static const unsigned op_indices[][4] = {
955 : { 0, 1, 2, 3 }, /* (L0 * L1), (R0 * R1). */
956 : { 0, 1, 3, 2 }, /* (L0 * L1), (R1 * R0). */
957 : { 1, 0, 2, 3 }, /* (L1 * L0), (R0 * R1). */
958 : { 1, 0, 3, 2 }, /* (L1 * L0), (R1 * R0). */
959 : };
960 :
961 : /* Only try permutations that swap operands within each MULT_EXPR. Swapping
962 : the two product terms is not valid because the real lane is ordered by a
963 : subtraction. */
964 1213 : slp_tree all_ops[4] = { left_op[0], left_op[1], right_op[0], right_op[1] };
965 5544 : for (unsigned i = 0; i < ARRAY_SIZE (op_indices); ++i)
966 : {
967 4527 : auto_vec<slp_tree> trial_ops;
968 4527 : if (vect_validate_multiplication (perm_cache, compat_cache, all_ops,
969 4527 : op_indices[i], subtract, perm,
970 : trial_ops, status))
971 : {
972 196 : ops.safe_splice (trial_ops);
973 196 : return true;
974 : }
975 4527 : }
976 :
977 : return false;
978 : }
979 :
980 : /* This function combines two nodes containing only even and only odd lanes
981 : together into a single node which contains the nodes in even/odd order
982 : by using a lane permute.
983 :
984 : The lanes in EVEN and ODD are duplicated 2 times inside the vectors.
985 : So for a lanes = 4 EVEN contains {EVEN1, EVEN1, EVEN2, EVEN2}.
986 :
987 : The tree REPRESENTATION is taken from the supplied REP along with the
988 : vectype which must be the same between all three nodes.
989 : */
990 :
991 : static slp_tree
992 32 : vect_build_combine_node (slp_tree even, slp_tree odd, slp_tree rep)
993 : {
994 32 : lane_permutation_t perm;
995 32 : perm.create (SLP_TREE_LANES (rep));
996 :
997 96 : for (unsigned x = 0; x < SLP_TREE_LANES (rep); x+=2)
998 : {
999 32 : perm.quick_push (std::make_pair (0, x));
1000 32 : perm.quick_push (std::make_pair (1, x+1));
1001 : }
1002 :
1003 32 : slp_tree vnode = vect_create_new_slp_node (2, VEC_PERM_EXPR);
1004 32 : SLP_TREE_LANE_PERMUTATION (vnode) = perm;
1005 :
1006 32 : SLP_TREE_CHILDREN (vnode).create (2);
1007 32 : SLP_TREE_CHILDREN (vnode).quick_push (even);
1008 32 : SLP_TREE_CHILDREN (vnode).quick_push (odd);
1009 32 : SLP_TREE_REF_COUNT (even)++;
1010 32 : SLP_TREE_REF_COUNT (odd)++;
1011 32 : SLP_TREE_REF_COUNT (vnode) = 1;
1012 :
1013 32 : SLP_TREE_LANES (vnode) = SLP_TREE_LANES (rep);
1014 64 : gcc_assert (perm.length () == SLP_TREE_LANES (vnode));
1015 32 : SLP_TREE_VECTYPE (vnode) = SLP_TREE_VECTYPE (rep);
1016 32 : return vnode;
1017 : }
1018 :
1019 : class complex_mul_pattern : public complex_pattern
1020 : {
1021 : protected:
1022 32 : complex_mul_pattern (slp_tree *node, vec<slp_tree> *m_ops, internal_fn ifn)
1023 64 : : complex_pattern (node, m_ops, ifn)
1024 : {
1025 32 : this->m_num_args = 2;
1026 : }
1027 :
1028 : public:
1029 : void build (vec_info *) final override;
1030 : static internal_fn
1031 : matches (complex_operation_t op, slp_tree_to_load_perm_map_t *,
1032 : slp_compat_nodes_map_t *, slp_tree *, vec<slp_tree> *);
1033 :
1034 : static vect_pattern*
1035 : recognize (slp_tree_to_load_perm_map_t *, slp_compat_nodes_map_t *,
1036 : slp_tree *);
1037 :
1038 : static vect_pattern*
1039 32 : mkInstance (slp_tree *node, vec<slp_tree> *m_ops, internal_fn ifn)
1040 : {
1041 32 : return new complex_mul_pattern (node, m_ops, ifn);
1042 : }
1043 :
1044 : };
1045 :
1046 : /* Pattern matcher for trying to match complex multiply and complex multiply
1047 : and accumulate pattern in SLP tree. If the operation matches then IFN
1048 : is set to the operation it matched and the arguments to the two
1049 : replacement statements are put in m_ops.
1050 :
1051 : If no match is found then IFN is set to IFN_LAST and m_ops is unchanged.
1052 :
1053 : This function matches the patterns shaped as:
1054 :
1055 : double ax = (b[i+1] * a[i]);
1056 : double bx = (a[i+1] * b[i]);
1057 :
1058 : c[i] = c[i] - ax;
1059 : c[i+1] = c[i+1] + bx;
1060 :
1061 : If a match occurred then TRUE is returned, else FALSE. The initial match is
1062 : expected to be in OP1 and the initial match operands in args0. */
1063 :
1064 : internal_fn
1065 5557834 : complex_mul_pattern::matches (complex_operation_t op,
1066 : slp_tree_to_load_perm_map_t *perm_cache,
1067 : slp_compat_nodes_map_t *compat_cache,
1068 : slp_tree *node, vec<slp_tree> *ops)
1069 : {
1070 5557834 : internal_fn ifn = IFN_LAST;
1071 :
1072 5557834 : if (op != MINUS_PLUS)
1073 : return IFN_LAST;
1074 :
1075 : /* It's only valid to form FMAs and MUL with -ffp-contract=fast. */
1076 3493 : if (flag_fp_contract_mode != FP_CONTRACT_FAST
1077 3493 : && FLOAT_TYPE_P (SLP_TREE_VECTYPE (*node)))
1078 : return IFN_LAST;
1079 :
1080 3458 : auto childs = *ops;
1081 3458 : auto l0node = SLP_TREE_CHILDREN (childs[0]);
1082 :
1083 3458 : bool mul0 = vect_match_expression_p (l0node[0], MULT_EXPR);
1084 3458 : bool mul1 = vect_match_expression_p (l0node[1], MULT_EXPR);
1085 3458 : if (!mul0 && !mul1)
1086 : return IFN_LAST;
1087 :
1088 : /* Now operand2+4 may lead to another expression. */
1089 2439 : auto_vec<slp_tree> left_op, right_op;
1090 2439 : slp_tree add0 = NULL;
1091 :
1092 : /* Check if we may be a multiply add. */
1093 2439 : if (!mul0
1094 2439 : && vect_match_expression_p (l0node[0], PLUS_EXPR))
1095 : {
1096 1082 : auto vals = SLP_TREE_CHILDREN (l0node[0]);
1097 : /* Check if it's a multiply, otherwise no idea what this is. */
1098 1082 : if (!(mul0 = vect_match_expression_p (vals[1], MULT_EXPR)))
1099 2439 : return IFN_LAST;
1100 :
1101 : /* Check if the ADD is linear, otherwise it's not valid complex FMA. */
1102 645 : if (linear_loads_p (perm_cache, vals[0]) != PERM_EVENODD)
1103 : return IFN_LAST;
1104 :
1105 28 : left_op.safe_splice (SLP_TREE_CHILDREN (vals[1]));
1106 28 : add0 = vals[0];
1107 : }
1108 : else
1109 1357 : left_op.safe_splice (SLP_TREE_CHILDREN (l0node[0]));
1110 :
1111 1385 : right_op.safe_splice (SLP_TREE_CHILDREN (l0node[1]));
1112 :
1113 1385 : if (left_op.length () != 2
1114 3706 : || right_op.length () != 2
1115 : || !mul0
1116 1267 : || !mul1
1117 2536 : || linear_loads_p (perm_cache, left_op[1]) == PERM_ODDEVEN)
1118 : return IFN_LAST;
1119 :
1120 1195 : enum _conj_status status;
1121 1195 : auto_vec<slp_tree> res_ops;
1122 1195 : if (!vect_validate_multiplication_commutative (perm_cache, compat_cache,
1123 : left_op, right_op, false,
1124 : res_ops, &status))
1125 : return IFN_LAST;
1126 :
1127 196 : if (status == CONJ_NONE)
1128 : {
1129 175 : if (add0)
1130 : ifn = IFN_COMPLEX_FMA;
1131 : else
1132 165 : ifn = IFN_COMPLEX_MUL;
1133 : }
1134 : else
1135 : {
1136 21 : if(add0)
1137 : ifn = IFN_COMPLEX_FMA_CONJ;
1138 : else
1139 11 : ifn = IFN_COMPLEX_MUL_CONJ;
1140 : }
1141 :
1142 196 : if (!vect_pattern_validate_optab (ifn, *node))
1143 : return IFN_LAST;
1144 :
1145 32 : ops->truncate (0);
1146 48 : ops->create (add0 ? 4 : 3);
1147 :
1148 32 : if (add0)
1149 16 : ops->quick_push (add0);
1150 :
1151 32 : complex_perm_kinds_t kind = linear_loads_p (perm_cache, res_ops[0]);
1152 32 : if (kind == PERM_EVENODD || kind == PERM_TOP)
1153 : {
1154 16 : ops->quick_push (res_ops[1]);
1155 16 : ops->quick_push (res_ops[3]);
1156 16 : ops->quick_push (res_ops[0]);
1157 : }
1158 16 : else if (kind == PERM_EVENEVEN && status != CONJ_SND)
1159 : {
1160 16 : ops->quick_push (res_ops[0]);
1161 16 : ops->quick_push (res_ops[2]);
1162 16 : ops->quick_push (res_ops[1]);
1163 : }
1164 : else
1165 : {
1166 0 : ops->quick_push (res_ops[0]);
1167 0 : ops->quick_push (res_ops[3]);
1168 0 : ops->quick_push (res_ops[1]);
1169 : }
1170 :
1171 : return ifn;
1172 3634 : }
1173 :
1174 : /* Attempt to recognize a complex mul pattern. */
1175 :
1176 : vect_pattern*
1177 0 : complex_mul_pattern::recognize (slp_tree_to_load_perm_map_t *perm_cache,
1178 : slp_compat_nodes_map_t *compat_cache,
1179 : slp_tree *node)
1180 : {
1181 0 : auto_vec<slp_tree> ops;
1182 0 : complex_operation_t op
1183 0 : = vect_detect_pair_op (*node, true, &ops);
1184 0 : internal_fn ifn
1185 0 : = complex_mul_pattern::matches (op, perm_cache, compat_cache, node, &ops);
1186 0 : if (ifn == IFN_LAST)
1187 : return NULL;
1188 :
1189 0 : return new complex_mul_pattern (node, &ops, ifn);
1190 0 : }
1191 :
1192 : /* Perform a replacement of the detected complex mul pattern with the new
1193 : instruction sequences. */
1194 :
1195 : void
1196 32 : complex_mul_pattern::build (vec_info *vinfo)
1197 : {
1198 32 : slp_tree node;
1199 32 : unsigned i;
1200 32 : switch (this->m_ifn)
1201 : {
1202 16 : case IFN_COMPLEX_MUL:
1203 16 : case IFN_COMPLEX_MUL_CONJ:
1204 16 : {
1205 16 : slp_tree newnode
1206 16 : = vect_build_combine_node (this->m_ops[0], this->m_ops[1],
1207 16 : *this->m_node);
1208 16 : SLP_TREE_REF_COUNT (this->m_ops[2])++;
1209 :
1210 48 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (*this->m_node), i, node)
1211 32 : vect_free_slp_tree (node);
1212 :
1213 : /* First re-arrange the children. */
1214 16 : SLP_TREE_CHILDREN (*this->m_node).reserve_exact (2);
1215 16 : SLP_TREE_CHILDREN (*this->m_node)[0] = this->m_ops[2];
1216 16 : SLP_TREE_CHILDREN (*this->m_node)[1] = newnode;
1217 16 : break;
1218 : }
1219 16 : case IFN_COMPLEX_FMA:
1220 16 : case IFN_COMPLEX_FMA_CONJ:
1221 16 : {
1222 16 : SLP_TREE_REF_COUNT (this->m_ops[0])++;
1223 16 : slp_tree newnode
1224 16 : = vect_build_combine_node (this->m_ops[1], this->m_ops[2],
1225 16 : *this->m_node);
1226 16 : SLP_TREE_REF_COUNT (this->m_ops[3])++;
1227 :
1228 48 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (*this->m_node), i, node)
1229 32 : vect_free_slp_tree (node);
1230 :
1231 : /* First re-arrange the children. */
1232 16 : SLP_TREE_CHILDREN (*this->m_node).safe_grow (3);
1233 16 : SLP_TREE_CHILDREN (*this->m_node)[0] = this->m_ops[3];
1234 16 : SLP_TREE_CHILDREN (*this->m_node)[1] = newnode;
1235 16 : SLP_TREE_CHILDREN (*this->m_node)[2] = this->m_ops[0];
1236 :
1237 : /* Tell the builder to expect an extra argument. */
1238 16 : this->m_num_args++;
1239 16 : break;
1240 : }
1241 0 : default:
1242 0 : gcc_unreachable ();
1243 : }
1244 :
1245 : /* And then rewrite the node itself. */
1246 32 : complex_pattern::build (vinfo);
1247 32 : }
1248 :
1249 : /*******************************************************************************
1250 : * complex_fms_pattern class
1251 : ******************************************************************************/
1252 :
1253 : class complex_fms_pattern : public complex_pattern
1254 : {
1255 : protected:
1256 0 : complex_fms_pattern (slp_tree *node, vec<slp_tree> *m_ops, internal_fn ifn)
1257 0 : : complex_pattern (node, m_ops, ifn)
1258 : {
1259 0 : this->m_num_args = 3;
1260 : }
1261 :
1262 : public:
1263 : void build (vec_info *) final override;
1264 : static internal_fn
1265 : matches (complex_operation_t op, slp_tree_to_load_perm_map_t *,
1266 : slp_compat_nodes_map_t *, slp_tree *, vec<slp_tree> *);
1267 :
1268 : static vect_pattern*
1269 : recognize (slp_tree_to_load_perm_map_t *, slp_compat_nodes_map_t *,
1270 : slp_tree *);
1271 :
1272 : static vect_pattern*
1273 0 : mkInstance (slp_tree *node, vec<slp_tree> *m_ops, internal_fn ifn)
1274 : {
1275 0 : return new complex_fms_pattern (node, m_ops, ifn);
1276 : }
1277 : };
1278 :
1279 :
1280 : /* Pattern matcher for trying to match complex multiply and subtract pattern
1281 : in SLP tree. If the operation matches then IFN is set to the operation
1282 : it matched and the arguments to the two replacement statements are put in
1283 : m_ops.
1284 :
1285 : If no match is found then IFN is set to IFN_LAST and m_ops is unchanged.
1286 :
1287 : This function matches the patterns shaped as:
1288 :
1289 : double ax = (b[i+1] * a[i]) + (b[i] * a[i]);
1290 : double bx = (a[i+1] * b[i]) - (a[i+1] * b[i+1]);
1291 :
1292 : c[i] = c[i] - ax;
1293 : c[i+1] = c[i+1] + bx;
1294 :
1295 : If a match occurred then TRUE is returned, else FALSE. The initial match is
1296 : expected to be in OP1 and the initial match operands in args0. */
1297 :
1298 : internal_fn
1299 5557834 : complex_fms_pattern::matches (complex_operation_t op,
1300 : slp_tree_to_load_perm_map_t *perm_cache,
1301 : slp_compat_nodes_map_t *compat_cache,
1302 : slp_tree * ref_node, vec<slp_tree> *ops)
1303 : {
1304 : /* It's only valid to form FMSs with -ffp-contract=fast. */
1305 5557834 : if (!SLP_TREE_VECTYPE (*ref_node)
1306 5557834 : || (flag_fp_contract_mode != FP_CONTRACT_FAST
1307 35601 : && FLOAT_TYPE_P (SLP_TREE_VECTYPE (*ref_node))))
1308 : return IFN_LAST;
1309 :
1310 : /* Match c - a * b when SLP has built the result as:
1311 :
1312 : c.real + (a.imag * b.imag - a.real * b.real)
1313 : c.imag - (a.real * b.imag + a.imag * b.real)
1314 :
1315 : This represents the same operation as the existing FMS matcher below,
1316 : but with the accumulator outside the complex product node. */
1317 3905481 : if (op == PLUS_MINUS)
1318 : {
1319 4036 : auto plus_ops = SLP_TREE_CHILDREN ((*ops)[0]);
1320 4036 : auto minus_ops = SLP_TREE_CHILDREN ((*ops)[1]);
1321 12108 : if (plus_ops.length () != 2 || minus_ops.length () != 2)
1322 : return IFN_LAST;
1323 :
1324 4036 : slp_tree acc = minus_ops[0];
1325 4036 : slp_tree prod = minus_ops[1];
1326 8072 : if (!((plus_ops[0] == acc && plus_ops[1] == prod)
1327 0 : || (plus_ops[1] == acc && plus_ops[0] == prod)))
1328 : return IFN_LAST;
1329 4036 : if (linear_loads_p (perm_cache, acc) != PERM_EVENODD)
1330 : return IFN_LAST;
1331 :
1332 172 : auto_vec<slp_tree> prod_ops;
1333 172 : if (vect_detect_pair_op (prod, true, &prod_ops) != MINUS_PLUS)
1334 : return IFN_LAST;
1335 172 : if (prod_ops.length () != 2)
1336 : return IFN_LAST;
1337 :
1338 0 : auto prod_left = SLP_TREE_CHILDREN (prod_ops[0]);
1339 0 : auto prod_right = SLP_TREE_CHILDREN (prod_ops[1]);
1340 0 : if (prod_left.length () != 2
1341 0 : || prod_right.length () != 2
1342 0 : || !vect_match_expression_p (prod_left[0], MULT_EXPR)
1343 0 : || !vect_match_expression_p (prod_left[1], MULT_EXPR)
1344 0 : || !vect_match_expression_p (prod_right[0], MULT_EXPR)
1345 0 : || !vect_match_expression_p (prod_right[1], MULT_EXPR))
1346 : return IFN_LAST;
1347 :
1348 0 : auto_vec<slp_tree> left_op, right_op;
1349 0 : left_op.safe_splice (SLP_TREE_CHILDREN (prod_left[0]));
1350 0 : right_op.safe_splice (SLP_TREE_CHILDREN (prod_left[1]));
1351 :
1352 0 : enum _conj_status status;
1353 0 : auto_vec<slp_tree> res_ops;
1354 0 : if (!vect_validate_multiplication_commutative (perm_cache, compat_cache,
1355 : right_op, left_op, true,
1356 : res_ops, &status))
1357 : return IFN_LAST;
1358 :
1359 0 : internal_fn ifn = status == CONJ_NONE ? IFN_COMPLEX_FMS
1360 : : IFN_COMPLEX_FMS_CONJ;
1361 0 : if (!vect_pattern_validate_optab (ifn, *ref_node))
1362 : return IFN_LAST;
1363 :
1364 0 : ops->truncate (0);
1365 0 : ops->create (4);
1366 :
1367 0 : complex_perm_kinds_t kind = linear_loads_p (perm_cache, res_ops[0]);
1368 0 : if (kind == PERM_EVENODD || kind == PERM_TOP)
1369 : {
1370 0 : ops->quick_push (acc);
1371 0 : ops->quick_push (res_ops[0]);
1372 0 : ops->quick_push (res_ops[1]);
1373 0 : ops->quick_push (res_ops[3]);
1374 : }
1375 0 : else if (kind == PERM_EVENEVEN && status != CONJ_SND)
1376 : {
1377 0 : ops->quick_push (acc);
1378 0 : ops->quick_push (res_ops[1]);
1379 0 : ops->quick_push (res_ops[0]);
1380 0 : ops->quick_push (res_ops[2]);
1381 : }
1382 : else
1383 : {
1384 0 : ops->quick_push (acc);
1385 0 : ops->quick_push (res_ops[1]);
1386 0 : ops->quick_push (res_ops[0]);
1387 0 : ops->quick_push (res_ops[3]);
1388 : }
1389 :
1390 : return ifn;
1391 172 : }
1392 :
1393 : /* We need to ignore the two_operands nodes that may also match,
1394 : for that we can check if they have any scalar statements and also
1395 : check that it's not a permute node as we're looking for a normal
1396 : MINUS_EXPR operation. */
1397 3901445 : if (op != CMPLX_NONE)
1398 : return IFN_LAST;
1399 :
1400 3897658 : slp_tree root = *ref_node;
1401 3897658 : if (!vect_match_expression_p (root, MINUS_EXPR))
1402 : return IFN_LAST;
1403 :
1404 : /* TODO: Support invariants here, with the new layout CADD now
1405 : can match before we get a chance to try CFMS. */
1406 71037 : auto nodes = SLP_TREE_CHILDREN (root);
1407 142050 : if (!vect_match_expression_p (nodes[1], MULT_EXPR)
1408 86075 : || vect_detect_pair_op (nodes[0]) != PLUS_MINUS)
1409 : return IFN_LAST;
1410 :
1411 24 : auto childs = SLP_TREE_CHILDREN (nodes[0]);
1412 24 : auto l0node = SLP_TREE_CHILDREN (childs[0]);
1413 :
1414 : /* Now operand2+4 may lead to another expression. */
1415 24 : auto_vec<slp_tree> left_op, right_op;
1416 24 : left_op.safe_splice (SLP_TREE_CHILDREN (l0node[1]));
1417 24 : right_op.safe_splice (SLP_TREE_CHILDREN (nodes[1]));
1418 :
1419 : /* If these nodes don't have any children then they're
1420 : not ones we're interested in. */
1421 24 : if (left_op.length () != 2
1422 18 : || right_op.length () != 2
1423 42 : || !vect_match_expression_p (l0node[1], MULT_EXPR))
1424 : return IFN_LAST;
1425 :
1426 18 : enum _conj_status status;
1427 18 : auto_vec<slp_tree> res_ops;
1428 18 : if (!vect_validate_multiplication_commutative (perm_cache, compat_cache,
1429 : right_op, left_op, true,
1430 : res_ops, &status))
1431 : return IFN_LAST;
1432 :
1433 0 : internal_fn ifn = status == CONJ_NONE ? IFN_COMPLEX_FMS
1434 : : IFN_COMPLEX_FMS_CONJ;
1435 0 : if (!vect_pattern_validate_optab (ifn, *ref_node))
1436 : return IFN_LAST;
1437 :
1438 0 : ops->truncate (0);
1439 0 : ops->create (4);
1440 :
1441 0 : complex_perm_kinds_t kind = linear_loads_p (perm_cache, res_ops[2]);
1442 0 : if (kind == PERM_EVENODD)
1443 : {
1444 0 : ops->quick_push (l0node[0]);
1445 0 : ops->quick_push (res_ops[2]);
1446 0 : ops->quick_push (res_ops[3]);
1447 0 : ops->quick_push (res_ops[1]);
1448 : }
1449 : else
1450 : {
1451 0 : ops->quick_push (l0node[0]);
1452 0 : ops->quick_push (res_ops[3]);
1453 0 : ops->quick_push (res_ops[2]);
1454 0 : ops->quick_push (res_ops[0]);
1455 : }
1456 :
1457 : return ifn;
1458 42 : }
1459 :
1460 : /* Attempt to recognize a complex mul pattern. */
1461 :
1462 : vect_pattern*
1463 0 : complex_fms_pattern::recognize (slp_tree_to_load_perm_map_t *perm_cache,
1464 : slp_compat_nodes_map_t *compat_cache,
1465 : slp_tree *node)
1466 : {
1467 0 : auto_vec<slp_tree> ops;
1468 0 : complex_operation_t op
1469 0 : = vect_detect_pair_op (*node, true, &ops);
1470 0 : internal_fn ifn
1471 0 : = complex_fms_pattern::matches (op, perm_cache, compat_cache, node, &ops);
1472 0 : if (ifn == IFN_LAST)
1473 : return NULL;
1474 :
1475 0 : return new complex_fms_pattern (node, &ops, ifn);
1476 0 : }
1477 :
1478 : /* Perform a replacement of the detected complex mul pattern with the new
1479 : instruction sequences. */
1480 :
1481 : void
1482 0 : complex_fms_pattern::build (vec_info *vinfo)
1483 : {
1484 0 : slp_tree node;
1485 0 : unsigned i;
1486 0 : slp_tree newnode =
1487 0 : vect_build_combine_node (this->m_ops[2], this->m_ops[3], *this->m_node);
1488 0 : SLP_TREE_REF_COUNT (this->m_ops[0])++;
1489 0 : SLP_TREE_REF_COUNT (this->m_ops[1])++;
1490 :
1491 0 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (*this->m_node), i, node)
1492 0 : vect_free_slp_tree (node);
1493 :
1494 0 : SLP_TREE_CHILDREN (*this->m_node).release ();
1495 0 : SLP_TREE_CHILDREN (*this->m_node).create (3);
1496 :
1497 : /* First re-arrange the children. */
1498 0 : SLP_TREE_CHILDREN (*this->m_node).quick_push (this->m_ops[1]);
1499 0 : SLP_TREE_CHILDREN (*this->m_node).quick_push (newnode);
1500 0 : SLP_TREE_CHILDREN (*this->m_node).quick_push (this->m_ops[0]);
1501 :
1502 : /* And then rewrite the node itself. */
1503 0 : complex_pattern::build (vinfo);
1504 0 : }
1505 :
1506 : /*******************************************************************************
1507 : * complex_operations_pattern class
1508 : ******************************************************************************/
1509 :
1510 : /* This function combines all the existing pattern matchers above into one class
1511 : that shares the functionality between them. The initial match is shared
1512 : between all complex operations. */
1513 :
1514 : class complex_operations_pattern : public complex_pattern
1515 : {
1516 : protected:
1517 : complex_operations_pattern (slp_tree *node, vec<slp_tree> *m_ops,
1518 : internal_fn ifn)
1519 : : complex_pattern (node, m_ops, ifn)
1520 : {
1521 : this->m_num_args = 0;
1522 : }
1523 :
1524 : public:
1525 : void build (vec_info *) final override;
1526 : static internal_fn
1527 : matches (complex_operation_t op, slp_tree_to_load_perm_map_t *,
1528 : slp_compat_nodes_map_t *, slp_tree *, vec<slp_tree> *);
1529 :
1530 : static vect_pattern*
1531 : recognize (slp_tree_to_load_perm_map_t *, slp_compat_nodes_map_t *,
1532 : slp_tree *);
1533 : };
1534 :
1535 : /* Dummy matches implementation for proxy object. */
1536 :
1537 : internal_fn
1538 0 : complex_operations_pattern::
1539 : matches (complex_operation_t /* op */,
1540 : slp_tree_to_load_perm_map_t * /* perm_cache */,
1541 : slp_compat_nodes_map_t * /* compat_cache */,
1542 : slp_tree * /* ref_node */, vec<slp_tree> * /* ops */)
1543 : {
1544 0 : return IFN_LAST;
1545 : }
1546 :
1547 : /* Attempt to recognize a complex mul pattern. */
1548 :
1549 : vect_pattern*
1550 5557834 : complex_operations_pattern::recognize (slp_tree_to_load_perm_map_t *perm_cache,
1551 : slp_compat_nodes_map_t *ccache,
1552 : slp_tree *node)
1553 : {
1554 5557834 : auto_vec<slp_tree> ops;
1555 5557834 : complex_operation_t op
1556 5557834 : = vect_detect_pair_op (*node, true, &ops);
1557 5557834 : internal_fn ifn = IFN_LAST;
1558 :
1559 5557834 : ifn = complex_fms_pattern::matches (op, perm_cache, ccache, node, &ops);
1560 5557834 : if (ifn != IFN_LAST)
1561 0 : return complex_fms_pattern::mkInstance (node, &ops, ifn);
1562 :
1563 5557834 : ifn = complex_mul_pattern::matches (op, perm_cache, ccache, node, &ops);
1564 5557834 : if (ifn != IFN_LAST)
1565 32 : return complex_mul_pattern::mkInstance (node, &ops, ifn);
1566 :
1567 5557802 : ifn = complex_add_pattern::matches (op, perm_cache, ccache, node, &ops);
1568 5557802 : if (ifn != IFN_LAST)
1569 0 : return complex_add_pattern::mkInstance (node, &ops, ifn);
1570 :
1571 : return NULL;
1572 5557834 : }
1573 :
1574 : /* Dummy implementation of build. */
1575 :
1576 : void
1577 0 : complex_operations_pattern::build (vec_info * /* vinfo */)
1578 : {
1579 0 : gcc_unreachable ();
1580 : }
1581 :
1582 :
1583 : /* The addsub_pattern. */
1584 :
1585 : class addsub_pattern : public vect_pattern
1586 : {
1587 : public:
1588 1082 : addsub_pattern (slp_tree *node, internal_fn ifn)
1589 1082 : : vect_pattern (node, NULL, ifn) {};
1590 :
1591 : void build (vec_info *) final override;
1592 :
1593 : static vect_pattern*
1594 : recognize (slp_tree_to_load_perm_map_t *, slp_compat_nodes_map_t *,
1595 : slp_tree *);
1596 : };
1597 :
1598 : vect_pattern *
1599 5557834 : addsub_pattern::recognize (slp_tree_to_load_perm_map_t *,
1600 : slp_compat_nodes_map_t *, slp_tree *node_)
1601 : {
1602 5557834 : slp_tree node = *node_;
1603 5557834 : if (!SLP_TREE_PERMUTE_P (node)
1604 26695 : || SLP_TREE_CHILDREN (node).length () != 2
1605 5581256 : || SLP_TREE_LANE_PERMUTATION (node).length () % 2)
1606 : return NULL;
1607 :
1608 : /* Match a blend of a plus and a minus op with the same number of plus and
1609 : minus lanes on the same operands. */
1610 18365 : unsigned l0 = SLP_TREE_LANE_PERMUTATION (node)[0].first;
1611 18365 : unsigned l1 = SLP_TREE_LANE_PERMUTATION (node)[1].first;
1612 18365 : if (l0 == l1)
1613 : return NULL;
1614 15399 : bool fma_p = false;
1615 15399 : bool l0add_p = vect_match_expression_p (SLP_TREE_CHILDREN (node)[l0],
1616 15399 : PLUS_EXPR);
1617 15399 : if (!l0add_p
1618 15399 : && !vect_match_expression_p (SLP_TREE_CHILDREN (node)[l0], MINUS_EXPR))
1619 : {
1620 6612 : l0add_p = vect_match_expression_p (SLP_TREE_CHILDREN (node)[l0], CFN_FMA);
1621 6612 : if (!l0add_p
1622 6612 : && !vect_match_expression_p (SLP_TREE_CHILDREN (node)[l0], CFN_FMS))
1623 6610 : return NULL;
1624 : fma_p = true;
1625 : }
1626 8789 : bool l1add_p = vect_match_expression_p (SLP_TREE_CHILDREN (node)[l1],
1627 8789 : PLUS_EXPR);
1628 8789 : if (l1add_p && fma_p)
1629 : return NULL;
1630 8789 : if (!l1add_p
1631 8789 : && !vect_match_expression_p (SLP_TREE_CHILDREN (node)[l1], MINUS_EXPR))
1632 : {
1633 720 : if (!fma_p)
1634 : return NULL;
1635 2 : l1add_p = vect_match_expression_p (SLP_TREE_CHILDREN (node)[l1], CFN_FMA);
1636 2 : if (!l1add_p
1637 2 : && !vect_match_expression_p (SLP_TREE_CHILDREN (node)[l1], CFN_FMS))
1638 0 : return NULL;
1639 : }
1640 8069 : else if (!l1add_p && fma_p)
1641 : return NULL;
1642 :
1643 8071 : slp_tree l0node = SLP_TREE_CHILDREN (node)[l0];
1644 8071 : slp_tree l1node = SLP_TREE_CHILDREN (node)[l1];
1645 8071 : if (!((SLP_TREE_CHILDREN (l0node)[0] == SLP_TREE_CHILDREN (l1node)[0]
1646 7665 : && SLP_TREE_CHILDREN (l0node)[1] == SLP_TREE_CHILDREN (l1node)[1])
1647 429 : || (SLP_TREE_CHILDREN (l0node)[0] == SLP_TREE_CHILDREN (l1node)[1]
1648 0 : && SLP_TREE_CHILDREN (l0node)[1] == SLP_TREE_CHILDREN (l1node)[0])))
1649 : return NULL;
1650 :
1651 26165 : for (unsigned i = 0; i < SLP_TREE_LANE_PERMUTATION (node).length (); ++i)
1652 : {
1653 18664 : std::pair<unsigned, unsigned> perm = SLP_TREE_LANE_PERMUTATION (node)[i];
1654 : /* It has to be alternating -, +, -,
1655 : While we could permute the .ADDSUB inputs and the .ADDSUB output
1656 : that's only profitable over the add + sub + blend if at least
1657 : one of the permute is optimized which we can't determine here. */
1658 28040 : if (perm.first != ((i & 1) ? l1 : l0)
1659 18572 : || perm.second != i)
1660 5557834 : return NULL;
1661 : }
1662 :
1663 : /* Now we have either { -, +, -, + ... } (!l0add_p) or { +, -, +, - ... }
1664 : (l0add_p), see whether we have FMA variants. We can only form FMAs
1665 : if allowed via -ffp-contract=fast or if they were FMA before. */
1666 7501 : if (!fma_p
1667 7499 : && flag_fp_contract_mode != FP_CONTRACT_FAST
1668 7538 : && FLOAT_TYPE_P (SLP_TREE_VECTYPE (l0node)))
1669 : ;
1670 7464 : else if (!l0add_p
1671 7464 : && (fma_p
1672 3426 : || vect_match_expression_p (SLP_TREE_CHILDREN (l0node)[0],
1673 3426 : MULT_EXPR)))
1674 : {
1675 : /* (c * d) -+ a */
1676 1278 : if (vect_pattern_validate_optab (IFN_VEC_FMADDSUB, node))
1677 37 : return new addsub_pattern (node_, IFN_VEC_FMADDSUB);
1678 : }
1679 6186 : else if (l0add_p
1680 6186 : && (fma_p
1681 6186 : || vect_match_expression_p (SLP_TREE_CHILDREN (l1node)[0],
1682 4036 : MULT_EXPR)))
1683 : {
1684 : /* (c * d) +- a */
1685 731 : if (vect_pattern_validate_optab (IFN_VEC_FMSUBADD, node))
1686 23 : return new addsub_pattern (node_, IFN_VEC_FMSUBADD);
1687 : }
1688 :
1689 7441 : if (!fma_p && !l0add_p && vect_pattern_validate_optab (IFN_VEC_ADDSUB, node))
1690 1022 : return new addsub_pattern (node_, IFN_VEC_ADDSUB);
1691 :
1692 : return NULL;
1693 : }
1694 :
1695 : void
1696 1082 : addsub_pattern::build (vec_info *vinfo)
1697 : {
1698 1082 : slp_tree node = *m_node;
1699 :
1700 1082 : unsigned l0 = SLP_TREE_LANE_PERMUTATION (node)[0].first;
1701 1082 : unsigned l1 = SLP_TREE_LANE_PERMUTATION (node)[1].first;
1702 :
1703 1082 : switch (m_ifn)
1704 : {
1705 1022 : case IFN_VEC_ADDSUB:
1706 1022 : {
1707 1022 : slp_tree sub = SLP_TREE_CHILDREN (node)[l0];
1708 1022 : slp_tree add = SLP_TREE_CHILDREN (node)[l1];
1709 :
1710 : /* Modify the blend node in-place. */
1711 1022 : SLP_TREE_CHILDREN (node)[0] = SLP_TREE_CHILDREN (sub)[0];
1712 1022 : SLP_TREE_CHILDREN (node)[1] = SLP_TREE_CHILDREN (sub)[1];
1713 1022 : SLP_TREE_REF_COUNT (SLP_TREE_CHILDREN (node)[0])++;
1714 1022 : SLP_TREE_REF_COUNT (SLP_TREE_CHILDREN (node)[1])++;
1715 :
1716 : /* Build IFN_VEC_ADDSUB from the sub representative operands. */
1717 1022 : stmt_vec_info rep = SLP_TREE_REPRESENTATIVE (sub);
1718 1022 : gcall *call = gimple_build_call_internal (IFN_VEC_ADDSUB, 2,
1719 : gimple_assign_rhs1 (rep->stmt),
1720 1022 : gimple_assign_rhs2 (rep->stmt));
1721 1022 : gimple_call_set_lhs (call, make_ssa_name
1722 1022 : (TREE_TYPE (gimple_assign_lhs (rep->stmt))));
1723 1022 : gimple_call_set_nothrow (call, true);
1724 1022 : gimple_set_bb (call, gimple_bb (rep->stmt));
1725 1022 : stmt_vec_info new_rep
1726 1022 : = vinfo->add_pattern_stmt (call, vect_orig_stmt (rep));
1727 1022 : SLP_TREE_REPRESENTATIVE (node) = new_rep;
1728 1022 : STMT_VINFO_RELEVANT (new_rep) = vect_used_in_scope;
1729 1022 : STMT_SLP_TYPE (new_rep) = pure_slp;
1730 1022 : STMT_VINFO_VECTYPE (new_rep) = SLP_TREE_VECTYPE (node);
1731 1022 : SLP_TREE_CODE (node) = ERROR_MARK;
1732 1022 : SLP_TREE_LANE_PERMUTATION (node).release ();
1733 :
1734 1022 : vect_free_slp_tree (sub);
1735 1022 : vect_free_slp_tree (add);
1736 1022 : break;
1737 : }
1738 60 : case IFN_VEC_FMADDSUB:
1739 60 : case IFN_VEC_FMSUBADD:
1740 60 : {
1741 60 : slp_tree sub, add;
1742 60 : if (m_ifn == IFN_VEC_FMADDSUB)
1743 : {
1744 37 : sub = SLP_TREE_CHILDREN (node)[l0];
1745 37 : add = SLP_TREE_CHILDREN (node)[l1];
1746 : }
1747 : else /* m_ifn == IFN_VEC_FMSUBADD */
1748 : {
1749 23 : sub = SLP_TREE_CHILDREN (node)[l1];
1750 23 : add = SLP_TREE_CHILDREN (node)[l0];
1751 : }
1752 : /* Modify the blend node in-place. */
1753 60 : SLP_TREE_CHILDREN (node).safe_grow (3, true);
1754 60 : gcall *call;
1755 60 : stmt_vec_info srep = SLP_TREE_REPRESENTATIVE (sub);
1756 60 : if (vect_match_expression_p (add, CFN_FMA))
1757 : {
1758 2 : SLP_TREE_CHILDREN (node)[0] = SLP_TREE_CHILDREN (add)[0];
1759 2 : SLP_TREE_CHILDREN (node)[1] = SLP_TREE_CHILDREN (add)[1];
1760 2 : SLP_TREE_CHILDREN (node)[2] = SLP_TREE_CHILDREN (add)[2];
1761 : /* Build IFN_VEC_FMADDSUB from the fms representative
1762 : operands. */
1763 2 : call = gimple_build_call_internal (m_ifn, 3,
1764 : gimple_call_arg (srep->stmt, 0),
1765 : gimple_call_arg (srep->stmt, 1),
1766 2 : gimple_call_arg (srep->stmt, 2));
1767 : }
1768 : else
1769 : {
1770 58 : slp_tree mul = SLP_TREE_CHILDREN (sub)[0];
1771 58 : SLP_TREE_CHILDREN (node)[0] = SLP_TREE_CHILDREN (mul)[0];
1772 58 : SLP_TREE_CHILDREN (node)[1] = SLP_TREE_CHILDREN (mul)[1];
1773 58 : SLP_TREE_CHILDREN (node)[2] = SLP_TREE_CHILDREN (sub)[1];
1774 : /* Build IFN_VEC_FMADDSUB from the mul/sub representative
1775 : operands. */
1776 58 : stmt_vec_info mrep = SLP_TREE_REPRESENTATIVE (mul);
1777 58 : call = gimple_build_call_internal (m_ifn, 3,
1778 : gimple_assign_rhs1 (mrep->stmt),
1779 58 : gimple_assign_rhs2 (mrep->stmt),
1780 58 : gimple_assign_rhs2 (srep->stmt));
1781 : }
1782 60 : SLP_TREE_REF_COUNT (SLP_TREE_CHILDREN (node)[0])++;
1783 60 : SLP_TREE_REF_COUNT (SLP_TREE_CHILDREN (node)[1])++;
1784 60 : SLP_TREE_REF_COUNT (SLP_TREE_CHILDREN (node)[2])++;
1785 :
1786 60 : gimple_call_set_lhs (call, make_ssa_name
1787 60 : (TREE_TYPE (gimple_get_lhs (srep->stmt))));
1788 60 : gimple_call_set_nothrow (call, true);
1789 60 : gimple_set_bb (call, gimple_bb (srep->stmt));
1790 60 : stmt_vec_info new_rep
1791 60 : = vinfo->add_pattern_stmt (call, vect_orig_stmt (srep));
1792 60 : SLP_TREE_REPRESENTATIVE (node) = new_rep;
1793 60 : STMT_VINFO_RELEVANT (new_rep) = vect_used_in_scope;
1794 60 : STMT_SLP_TYPE (new_rep) = pure_slp;
1795 60 : STMT_VINFO_VECTYPE (new_rep) = SLP_TREE_VECTYPE (node);
1796 60 : SLP_TREE_CODE (node) = ERROR_MARK;
1797 60 : SLP_TREE_LANE_PERMUTATION (node).release ();
1798 :
1799 60 : vect_free_slp_tree (sub);
1800 60 : vect_free_slp_tree (add);
1801 60 : break;
1802 : }
1803 1082 : default:;
1804 : }
1805 1082 : }
1806 :
1807 : /*******************************************************************************
1808 : * Pattern matching definitions
1809 : ******************************************************************************/
1810 :
1811 : #define SLP_PATTERN(x) &x::recognize
1812 : vect_pattern_decl_t slp_patterns[]
1813 : {
1814 : /* For least amount of back-tracking and more efficient matching
1815 : order patterns from the largest to the smallest. Especially if they
1816 : overlap in what they can detect. */
1817 :
1818 : SLP_PATTERN (complex_operations_pattern),
1819 : SLP_PATTERN (addsub_pattern)
1820 : };
1821 : #undef SLP_PATTERN
1822 :
1823 : /* Set the number of SLP pattern matchers available. */
1824 : size_t num__slp_patterns = ARRAY_SIZE (slp_patterns);
|