Debian Codegraph @

gcc/tree-vect-slp-patterns.cc

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