aix: Use lcomm for TLS static data.
[official-gcc.git] / gcc / tree-vect-slp-patterns.c
blob1e2769662a54229ab8e24390f97dfe206f17ab57
1 /* SLP - Pattern matcher on SLP trees
2 Copyright (C) 2020-2021 Free Software Foundation, Inc.
4 This file is part of GCC.
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.
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.
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/>. */
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"
46 /* SLP Pattern matching mechanism.
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.
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.
56 The pattern matcher currently only allows you to perform replacements to
57 internal functions.
59 Once the patterns are matched it is one way, these cannot be undone. It is
60 currently not supported to match patterns recursively.
62 To add a new pattern, implement the vect_pattern class and add the type to
63 slp_patterns.
67 /*******************************************************************************
68 * vect_pattern class
69 ******************************************************************************/
71 /* Default implementation of recognize that performs matching, validation and
72 replacement of nodes but that can be overriden if required. */
74 static bool
75 vect_pattern_validate_optab (internal_fn ifn, slp_tree node)
77 tree vectype = SLP_TREE_VECTYPE (node);
78 if (ifn == IFN_LAST || !vectype)
79 return false;
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));
86 if (direct_internal_fn_supported_p (ifn, vectype, OPTIMIZE_FOR_SPEED))
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);
93 else
95 if (dump_enabled_p ())
97 if (!vectype)
98 dump_printf_loc (MSG_NOTE, vect_location,
99 "Target does not support vector type for %T\n",
100 SLP_TREE_DEF_TYPE (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);
106 return false;
108 return true;
111 /*******************************************************************************
112 * General helper types
113 ******************************************************************************/
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. */
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;
127 /*******************************************************************************
128 * General helper functions
129 ******************************************************************************/
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.
135 static inline complex_perm_kinds_t
136 is_linear_load_p (load_permutation_t loads)
138 if (loads.length() == 0)
139 return PERM_UNKNOWN;
141 unsigned load, i;
142 complex_perm_kinds_t candidates[4]
143 = { PERM_ODDODD
144 , PERM_EVENEVEN
145 , PERM_EVENODD
146 , PERM_ODDEVEN
149 int valid_patterns = 4;
150 FOR_EACH_VEC_ELT (loads, i, load)
152 if (candidates[0] != PERM_UNKNOWN && load != 1)
154 candidates[0] = PERM_UNKNOWN;
155 valid_patterns--;
157 if (candidates[1] != PERM_UNKNOWN && load != 0)
159 candidates[1] = PERM_UNKNOWN;
160 valid_patterns--;
162 if (candidates[2] != PERM_UNKNOWN && load != i)
164 candidates[2] = PERM_UNKNOWN;
165 valid_patterns--;
167 if (candidates[3] != PERM_UNKNOWN
168 && load != (i % 2 == 0 ? i + 1 : i - 1))
170 candidates[3] = PERM_UNKNOWN;
171 valid_patterns--;
174 if (valid_patterns == 0)
175 return PERM_UNKNOWN;
178 for (i = 0; i < sizeof(candidates); i++)
179 if (candidates[i] != PERM_UNKNOWN)
180 return candidates[i];
182 return PERM_UNKNOWN;
185 /* Combine complex_perm_kinds A and B into a new permute kind that describes the
186 resulting operation. */
188 static inline complex_perm_kinds_t
189 vect_merge_perms (complex_perm_kinds_t a, complex_perm_kinds_t b)
191 if (a == b)
192 return a;
194 if (a == PERM_TOP)
195 return b;
197 if (b == PERM_TOP)
198 return a;
200 return PERM_UNKNOWN;
203 /* Check to see if all loads rooted in ROOT are linear. Linearity is
204 defined as having no gaps between values loaded. */
206 static complex_load_perm_t
207 linear_loads_p (slp_tree_to_load_perm_map_t *perm_cache, slp_tree root)
209 if (!root)
210 return std::make_pair (PERM_UNKNOWN, vNULL);
212 unsigned i;
213 complex_load_perm_t *tmp;
215 if ((tmp = perm_cache->get (root)) != NULL)
216 return *tmp;
218 complex_load_perm_t retval = std::make_pair (PERM_UNKNOWN, vNULL);
219 perm_cache->put (root, retval);
221 /* If it's a load node, then just read the load permute. */
222 if (SLP_TREE_LOAD_PERMUTATION (root).exists ())
224 retval.first = is_linear_load_p (SLP_TREE_LOAD_PERMUTATION (root));
225 retval.second = SLP_TREE_LOAD_PERMUTATION (root);
226 perm_cache->put (root, retval);
227 return retval;
229 else if (SLP_TREE_DEF_TYPE (root) != vect_internal_def)
231 retval.first = PERM_TOP;
232 perm_cache->put (root, retval);
233 return retval;
236 auto_vec<load_permutation_t> all_loads;
237 complex_perm_kinds_t kind = PERM_TOP;
239 slp_tree child;
240 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (root), i, child)
242 complex_load_perm_t res = linear_loads_p (perm_cache, child);
243 kind = vect_merge_perms (kind, res.first);
244 /* Unknown and Top are not valid on blends as they produce no permute. */
245 retval.first = kind;
246 if (kind == PERM_UNKNOWN || kind == PERM_TOP)
247 return retval;
248 all_loads.safe_push (res.second);
251 if (SLP_TREE_LANE_PERMUTATION (root).exists ())
253 lane_permutation_t perm = SLP_TREE_LANE_PERMUTATION (root);
254 load_permutation_t nloads;
255 nloads.create (SLP_TREE_LANES (root));
256 nloads.quick_grow (SLP_TREE_LANES (root));
257 for (i = 0; i < SLP_TREE_LANES (root); i++)
258 nloads[i] = all_loads[perm[i].first][perm[i].second];
260 retval.first = kind;
261 retval.second = nloads;
263 else
265 retval.first = kind;
266 retval.second = all_loads[0];
269 perm_cache->put (root, retval);
270 return retval;
274 /* This function attempts to make a node rooted in NODE is linear. If the node
275 if already linear than the node itself is returned in RESULT.
277 If the node is not linear then a new VEC_PERM_EXPR node is created with a
278 lane permute that when applied will make the node linear. If such a
279 permute cannot be created then FALSE is returned from the function.
281 Here linearity is defined as having a sequential, monotically increasing
282 load position inside the load permute generated by the loads reachable from
283 NODE. */
285 static slp_tree
286 vect_build_swap_evenodd_node (slp_tree node)
288 /* Attempt to linearise the permute. */
289 vec<std::pair<unsigned, unsigned> > zipped;
290 zipped.create (SLP_TREE_LANES (node));
292 for (unsigned x = 0; x < SLP_TREE_LANES (node); x+=2)
294 zipped.quick_push (std::make_pair (0, x+1));
295 zipped.quick_push (std::make_pair (0, x));
298 /* Create the new permute node and store it instead. */
299 slp_tree vnode = vect_create_new_slp_node (1, VEC_PERM_EXPR);
300 SLP_TREE_LANE_PERMUTATION (vnode) = zipped;
301 SLP_TREE_VECTYPE (vnode) = SLP_TREE_VECTYPE (node);
302 SLP_TREE_CHILDREN (vnode).quick_push (node);
303 SLP_TREE_REF_COUNT (vnode) = 1;
304 SLP_TREE_LANES (vnode) = SLP_TREE_LANES (node);
305 SLP_TREE_REPRESENTATIVE (vnode) = SLP_TREE_REPRESENTATIVE (node);
306 SLP_TREE_REF_COUNT (node)++;
307 return vnode;
310 /* Checks to see of the expression represented by NODE is a gimple assign with
311 code CODE. */
313 static inline bool
314 vect_match_expression_p (slp_tree node, tree_code code)
316 if (!node
317 || !SLP_TREE_REPRESENTATIVE (node))
318 return false;
320 gimple* expr = STMT_VINFO_STMT (SLP_TREE_REPRESENTATIVE (node));
321 if (!is_gimple_assign (expr)
322 || gimple_assign_rhs_code (expr) != code)
323 return false;
325 return true;
328 /* Checks to see if the expression represented by NODE is a call to the internal
329 function FN. */
331 static inline bool
332 vect_match_call_p (slp_tree node, internal_fn fn)
334 if (!node
335 || !SLP_TREE_REPRESENTATIVE (node))
336 return false;
338 gimple* expr = STMT_VINFO_STMT (SLP_TREE_REPRESENTATIVE (node));
339 if (!expr
340 || !gimple_call_internal_p (expr, fn))
341 return false;
343 return true;
346 /* Check if the given lane permute in PERMUTES matches an alternating sequence
347 of {even odd even odd ...}. This to account for unrolled loops. Further
348 mode there resulting permute must be linear. */
350 static inline bool
351 vect_check_evenodd_blend (lane_permutation_t &permutes,
352 unsigned even, unsigned odd)
354 if (permutes.length () == 0)
355 return false;
357 unsigned val[2] = {even, odd};
358 unsigned seed = 0;
359 for (unsigned i = 0; i < permutes.length (); i++)
360 if (permutes[i].first != val[i % 2]
361 || permutes[i].second != seed++)
362 return false;
364 return true;
367 /* This function will match the two gimple expressions representing NODE1 and
368 NODE2 in parallel and returns the pair operation that represents the two
369 expressions in the two statements.
371 If match is successful then the corresponding complex_operation is
372 returned and the arguments to the two matched operations are returned in OPS.
374 If TWO_OPERANDS it is expected that the LANES of the parent VEC_PERM select
375 from the two nodes alternatingly.
377 If unsuccessful then CMPLX_NONE is returned and OPS is untouched.
379 e.g. the following gimple statements
381 stmt 0 _39 = _37 + _12;
382 stmt 1 _6 = _38 - _36;
384 will return PLUS_MINUS along with OPS containing {_37, _12, _38, _36}.
387 static complex_operation_t
388 vect_detect_pair_op (slp_tree node1, slp_tree node2, lane_permutation_t &lanes,
389 bool two_operands = true, vec<slp_tree> *ops = NULL)
391 complex_operation_t result = CMPLX_NONE;
393 if (vect_match_expression_p (node1, MINUS_EXPR)
394 && vect_match_expression_p (node2, PLUS_EXPR)
395 && (!two_operands || vect_check_evenodd_blend (lanes, 0, 1)))
396 result = MINUS_PLUS;
397 else if (vect_match_expression_p (node1, PLUS_EXPR)
398 && vect_match_expression_p (node2, MINUS_EXPR)
399 && (!two_operands || vect_check_evenodd_blend (lanes, 0, 1)))
400 result = PLUS_MINUS;
401 else if (vect_match_expression_p (node1, PLUS_EXPR)
402 && vect_match_expression_p (node2, PLUS_EXPR))
403 result = PLUS_PLUS;
404 else if (vect_match_expression_p (node1, MULT_EXPR)
405 && vect_match_expression_p (node2, MULT_EXPR))
406 result = MULT_MULT;
408 if (result != CMPLX_NONE && ops != NULL)
410 ops->safe_push (node1);
411 ops->safe_push (node2);
413 return result;
416 /* Overload of vect_detect_pair_op that matches against the representative
417 statements in the children of NODE. It is expected that NODE has exactly
418 two children and when TWO_OPERANDS then NODE must be a VEC_PERM. */
420 static complex_operation_t
421 vect_detect_pair_op (slp_tree node, bool two_operands = true,
422 vec<slp_tree> *ops = NULL)
424 if (!two_operands && SLP_TREE_CODE (node) == VEC_PERM_EXPR)
425 return CMPLX_NONE;
427 if (SLP_TREE_CHILDREN (node).length () != 2)
428 return CMPLX_NONE;
430 vec<slp_tree> children = SLP_TREE_CHILDREN (node);
431 lane_permutation_t &lanes = SLP_TREE_LANE_PERMUTATION (node);
433 return vect_detect_pair_op (children[0], children[1], lanes, two_operands,
434 ops);
437 /*******************************************************************************
438 * complex_pattern class
439 ******************************************************************************/
441 /* SLP Complex Numbers pattern matching.
443 As an example, the following simple loop:
445 double a[restrict N]; double b[restrict N]; double c[restrict N];
447 for (int i=0; i < N; i+=2)
449 c[i] = a[i] - b[i+1];
450 c[i+1] = a[i+1] + b[i];
453 which represents a complex addition on with a rotation of 90* around the
454 argand plane. i.e. if `a` and `b` were complex numbers then this would be the
455 same as `a + (b * I)`.
457 Here the expressions for `c[i]` and `c[i+1]` are independent but have to be
458 both recognized in order for the pattern to work. As an SLP tree this is
459 represented as
461 +--------------------------------+
462 | stmt 0 *_9 = _10; |
463 | stmt 1 *_15 = _16; |
464 +--------------------------------+
468 +--------------------------------+
469 | stmt 0 _10 = _4 - _8; |
470 | stmt 1 _16 = _12 + _14; |
471 | lane permutation { 0[0] 1[1] } |
472 +--------------------------------+
476 +-----+ | | +-----+
477 | | | | | |
478 +-----| { } |<-----+ +----->| { } --------+
479 | | | +------------------| | |
480 | +-----+ | +-----+ |
481 | | | |
482 | | | |
483 | +------|------------------+ |
484 | | | |
485 v v v v
486 +--------------------------+ +--------------------------------+
487 | stmt 0 _8 = *_7; | | stmt 0 _4 = *_3; |
488 | stmt 1 _14 = *_13; | | stmt 1 _12 = *_11; |
489 | load permutation { 1 0 } | | load permutation { 0 1 } |
490 +--------------------------+ +--------------------------------+
492 The pattern matcher allows you to replace both statements 0 and 1 or none at
493 all. Because this operation is a two operands operation the actual nodes
494 being replaced are those in the { } nodes. The actual scalar statements
495 themselves are not replaced or used during the matching but instead the
496 SLP_TREE_REPRESENTATIVE statements are inspected. You are also allowed to
497 replace and match on any number of nodes.
499 Because the pattern matcher matches on the representative statement for the
500 SLP node the case of two_operators it allows you to match the children of the
501 node. This is done using the method `recognize ()`.
505 /* The complex_pattern class contains common code for pattern matchers that work
506 on complex numbers. These provide functionality to allow de-construction and
507 validation of sequences depicting/transforming REAL and IMAG pairs. */
509 class complex_pattern : public vect_pattern
511 protected:
512 auto_vec<slp_tree> m_workset;
513 complex_pattern (slp_tree *node, vec<slp_tree> *m_ops, internal_fn ifn)
514 : vect_pattern (node, m_ops, ifn)
516 this->m_workset.safe_push (*node);
519 public:
520 void build (vec_info *);
522 static internal_fn
523 matches (complex_operation_t op, slp_tree_to_load_perm_map_t *, slp_tree *,
524 vec<slp_tree> *);
527 /* Create a replacement pattern statement for each node in m_node and inserts
528 the new statement into m_node as the new representative statement. The old
529 statement is marked as being in a pattern defined by the new statement. The
530 statement is created as call to internal function IFN with m_num_args
531 arguments.
533 Futhermore the new pattern is also added to the vectorization information
534 structure VINFO and the old statement STMT_INFO is marked as unused while
535 the new statement is marked as used and the number of SLP uses of the new
536 statement is incremented.
538 The newly created SLP nodes are marked as SLP only and will be dissolved
539 if SLP is aborted.
541 The newly created gimple call is returned and the BB remains unchanged.
543 This default method is designed to only match against simple operands where
544 all the input and output types are the same.
547 void
548 complex_pattern::build (vec_info *vinfo)
550 stmt_vec_info stmt_info;
552 auto_vec<tree> args;
553 args.create (this->m_num_args);
554 args.quick_grow_cleared (this->m_num_args);
555 slp_tree node;
556 unsigned ix;
557 stmt_vec_info call_stmt_info;
558 gcall *call_stmt = NULL;
560 /* Now modify the nodes themselves. */
561 FOR_EACH_VEC_ELT (this->m_workset, ix, node)
563 /* Calculate the location of the statement in NODE to replace. */
564 stmt_info = SLP_TREE_REPRESENTATIVE (node);
565 gimple* old_stmt = STMT_VINFO_STMT (stmt_info);
566 tree lhs_old_stmt = gimple_get_lhs (old_stmt);
567 tree type = TREE_TYPE (lhs_old_stmt);
569 /* Create the argument set for use by gimple_build_call_internal_vec. */
570 for (unsigned i = 0; i < this->m_num_args; i++)
571 args[i] = lhs_old_stmt;
573 /* Create the new pattern statements. */
574 call_stmt = gimple_build_call_internal_vec (this->m_ifn, args);
575 tree var = make_temp_ssa_name (type, call_stmt, "slp_patt");
576 gimple_call_set_lhs (call_stmt, var);
577 gimple_set_location (call_stmt, gimple_location (old_stmt));
578 gimple_call_set_nothrow (call_stmt, true);
580 /* Adjust the book-keeping for the new and old statements for use during
581 SLP. This is required to get the right VF and statement during SLP
582 analysis. These changes are created after relevancy has been set for
583 the nodes as such we need to manually update them. Any changes will be
584 undone if SLP is cancelled. */
585 call_stmt_info
586 = vinfo->add_pattern_stmt (call_stmt, stmt_info);
588 /* Make sure to mark the representative statement pure_slp and
589 relevant. */
590 STMT_VINFO_RELEVANT (call_stmt_info) = vect_used_in_scope;
591 STMT_SLP_TYPE (call_stmt_info) = pure_slp;
593 /* add_pattern_stmt can't be done in vect_mark_pattern_stmts because
594 the non-SLP pattern matchers already have added the statement to VINFO
595 by the time it is called. Some of them need to modify the returned
596 stmt_info. vect_mark_pattern_stmts is called by recog_pattern and it
597 would increase the size of each pattern with boilerplate code to make
598 the call there. */
599 vect_mark_pattern_stmts (vinfo, stmt_info, call_stmt,
600 SLP_TREE_VECTYPE (node));
601 STMT_VINFO_SLP_VECT_ONLY_PATTERN (call_stmt_info) = true;
603 /* Since we are replacing all the statements in the group with the same
604 thing it doesn't really matter. So just set it every time a new stmt
605 is created. */
606 SLP_TREE_REPRESENTATIVE (node) = call_stmt_info;
607 SLP_TREE_LANE_PERMUTATION (node).release ();
608 SLP_TREE_CODE (node) = CALL_EXPR;
612 /*******************************************************************************
613 * complex_add_pattern class
614 ******************************************************************************/
616 class complex_add_pattern : public complex_pattern
618 protected:
619 complex_add_pattern (slp_tree *node, vec<slp_tree> *m_ops, internal_fn ifn)
620 : complex_pattern (node, m_ops, ifn)
622 this->m_num_args = 2;
625 public:
626 void build (vec_info *);
627 static internal_fn
628 matches (complex_operation_t op, slp_tree_to_load_perm_map_t *, slp_tree *,
629 vec<slp_tree> *);
631 static vect_pattern*
632 recognize (slp_tree_to_load_perm_map_t *, slp_tree *);
634 static vect_pattern*
635 mkInstance (slp_tree *node, vec<slp_tree> *m_ops, internal_fn ifn)
637 return new complex_add_pattern (node, m_ops, ifn);
641 /* Perform a replacement of the detected complex add pattern with the new
642 instruction sequences. */
644 void
645 complex_add_pattern::build (vec_info *vinfo)
647 SLP_TREE_CHILDREN (*this->m_node).reserve_exact (2);
649 slp_tree node = this->m_ops[0];
650 vec<slp_tree> children = SLP_TREE_CHILDREN (node);
652 /* First re-arrange the children. */
653 SLP_TREE_CHILDREN (*this->m_node)[0] = children[0];
654 SLP_TREE_CHILDREN (*this->m_node)[1] =
655 vect_build_swap_evenodd_node (children[1]);
657 SLP_TREE_REF_COUNT (SLP_TREE_CHILDREN (*this->m_node)[0])++;
658 SLP_TREE_REF_COUNT (SLP_TREE_CHILDREN (*this->m_node)[1])++;
659 vect_free_slp_tree (this->m_ops[0]);
660 vect_free_slp_tree (this->m_ops[1]);
662 complex_pattern::build (vinfo);
665 /* Pattern matcher for trying to match complex addition pattern in SLP tree.
667 If no match is found then IFN is set to IFN_LAST.
668 This function matches the patterns shaped as:
670 c[i] = a[i] - b[i+1];
671 c[i+1] = a[i+1] + b[i];
673 If a match occurred then TRUE is returned, else FALSE. The initial match is
674 expected to be in OP1 and the initial match operands in args0. */
676 internal_fn
677 complex_add_pattern::matches (complex_operation_t op,
678 slp_tree_to_load_perm_map_t *perm_cache,
679 slp_tree *node, vec<slp_tree> *ops)
681 internal_fn ifn = IFN_LAST;
683 /* Find the two components. Rotation in the complex plane will modify
684 the operations:
686 * Rotation 0: + +
687 * Rotation 90: - +
688 * Rotation 180: - -
689 * Rotation 270: + -
691 Rotation 0 and 180 can be handled by normal SIMD code, so we don't need
692 to care about them here. */
693 if (op == MINUS_PLUS)
694 ifn = IFN_COMPLEX_ADD_ROT90;
695 else if (op == PLUS_MINUS)
696 ifn = IFN_COMPLEX_ADD_ROT270;
697 else
698 return ifn;
700 /* verify that there is a permute, otherwise this isn't a pattern we
701 we support. */
702 gcc_assert (ops->length () == 2);
704 vec<slp_tree> children = SLP_TREE_CHILDREN ((*ops)[0]);
706 /* First node must be unpermuted. */
707 if (linear_loads_p (perm_cache, children[0]).first != PERM_EVENODD)
708 return IFN_LAST;
710 /* Second node must be permuted. */
711 if (linear_loads_p (perm_cache, children[1]).first != PERM_ODDEVEN)
712 return IFN_LAST;
714 if (!vect_pattern_validate_optab (ifn, *node))
715 return IFN_LAST;
717 return ifn;
720 /* Attempt to recognize a complex add pattern. */
722 vect_pattern*
723 complex_add_pattern::recognize (slp_tree_to_load_perm_map_t *perm_cache,
724 slp_tree *node)
726 auto_vec<slp_tree> ops;
727 complex_operation_t op
728 = vect_detect_pair_op (*node, true, &ops);
729 internal_fn ifn
730 = complex_add_pattern::matches (op, perm_cache, node, &ops);
731 if (ifn == IFN_LAST)
732 return NULL;
734 return new complex_add_pattern (node, &ops, ifn);
737 /*******************************************************************************
738 * complex_mul_pattern
739 ******************************************************************************/
741 /* Helper function of that looks for a match in the CHILDth child of NODE. The
742 child used is stored in RES.
744 If the match is successful then ARGS will contain the operands matched
745 and the complex_operation_t type is returned. If match is not successful
746 then CMPLX_NONE is returned and ARGS is left unmodified. */
748 static inline complex_operation_t
749 vect_match_call_complex_mla (slp_tree node, unsigned child,
750 vec<slp_tree> *args = NULL, slp_tree *res = NULL)
752 gcc_assert (child < SLP_TREE_CHILDREN (node).length ());
754 slp_tree data = SLP_TREE_CHILDREN (node)[child];
756 if (res)
757 *res = data;
759 return vect_detect_pair_op (data, false, args);
762 /* Check to see if either of the trees in ARGS are a NEGATE_EXPR. If the first
763 child (args[0]) is a NEGATE_EXPR then NEG_FIRST_P is set to TRUE.
765 If a negate is found then the values in ARGS are reordered such that the
766 negate node is always the second one and the entry is replaced by the child
767 of the negate node. */
769 static inline bool
770 vect_normalize_conj_loc (vec<slp_tree> args, bool *neg_first_p = NULL)
772 gcc_assert (args.length () == 2);
773 bool neg_found = false;
775 if (vect_match_expression_p (args[0], NEGATE_EXPR))
777 std::swap (args[0], args[1]);
778 neg_found = true;
779 if (neg_first_p)
780 *neg_first_p = true;
782 else if (vect_match_expression_p (args[1], NEGATE_EXPR))
784 neg_found = true;
785 if (neg_first_p)
786 *neg_first_p = false;
789 if (neg_found)
790 args[1] = SLP_TREE_CHILDREN (args[1])[0];
792 return neg_found;
795 /* Helper function to check if PERM is KIND or PERM_TOP. */
797 static inline bool
798 is_eq_or_top (complex_load_perm_t perm, complex_perm_kinds_t kind)
800 return perm.first == kind || perm.first == PERM_TOP;
803 /* Helper function that checks to see if LEFT_OP and RIGHT_OP are both MULT_EXPR
804 nodes but also that they represent an operation that is either a complex
805 multiplication or a complex multiplication by conjugated value.
807 Of the negation is expected to be in the first half of the tree (As required
808 by an FMS pattern) then NEG_FIRST is true. If the operation is a conjugate
809 operation then CONJ_FIRST_OPERAND is set to indicate whether the first or
810 second operand contains the conjugate operation. */
812 static inline bool
813 vect_validate_multiplication (slp_tree_to_load_perm_map_t *perm_cache,
814 vec<slp_tree> left_op, vec<slp_tree> right_op,
815 bool neg_first, bool *conj_first_operand,
816 bool fms)
818 /* The presence of a negation indicates that we have either a conjugate or a
819 rotation. We need to distinguish which one. */
820 *conj_first_operand = false;
821 complex_perm_kinds_t kind;
823 /* Complex conjugates have the negation on the imaginary part of the
824 number where rotations affect the real component. So check if the
825 negation is on a dup of lane 1. */
826 if (fms)
828 /* Canonicalization for fms is not consistent. So have to test both
829 variants to be sure. This needs to be fixed in the mid-end so
830 this part can be simpler. */
831 kind = linear_loads_p (perm_cache, right_op[0]).first;
832 if (!((is_eq_or_top (linear_loads_p (perm_cache, right_op[0]), PERM_ODDODD)
833 && is_eq_or_top (linear_loads_p (perm_cache, right_op[1]),
834 PERM_ODDEVEN))
835 || (kind == PERM_ODDEVEN
836 && is_eq_or_top (linear_loads_p (perm_cache, right_op[1]),
837 PERM_ODDODD))))
838 return false;
840 else
842 if (linear_loads_p (perm_cache, right_op[1]).first != PERM_ODDODD
843 && !is_eq_or_top (linear_loads_p (perm_cache, right_op[0]),
844 PERM_ODDEVEN))
845 return false;
848 /* Deal with differences in indexes. */
849 int index1 = fms ? 1 : 0;
850 int index2 = fms ? 0 : 1;
852 /* Check if the conjugate is on the second first or second operand. The
853 order of the node with the conjugate value determines this, and the dup
854 node must be one of lane 0 of the same DR as the neg node. */
855 kind = linear_loads_p (perm_cache, left_op[index1]).first;
856 if (kind == PERM_TOP)
858 if (linear_loads_p (perm_cache, left_op[index2]).first == PERM_EVENODD)
859 return true;
861 else if (kind == PERM_EVENODD)
863 if ((kind = linear_loads_p (perm_cache, left_op[index2]).first) == PERM_EVENODD)
864 return false;
865 return true;
867 else if (!neg_first)
868 *conj_first_operand = true;
869 else
870 return false;
872 if (kind != PERM_EVENEVEN)
873 return false;
875 return true;
878 /* Helper function to help distinguish between a conjugate and a rotation in a
879 complex multiplication. The operations have similar shapes but the order of
880 the load permutes are different. This function returns TRUE when the order
881 is consistent with a multiplication or multiplication by conjugated
882 operand but returns FALSE if it's a multiplication by rotated operand. */
884 static inline bool
885 vect_validate_multiplication (slp_tree_to_load_perm_map_t *perm_cache,
886 vec<slp_tree> op, complex_perm_kinds_t permKind)
888 /* The left node is the more common case, test it first. */
889 if (!is_eq_or_top (linear_loads_p (perm_cache, op[0]), permKind))
891 if (!is_eq_or_top (linear_loads_p (perm_cache, op[1]), permKind))
892 return false;
894 return true;
897 /* This function combines two nodes containing only even and only odd lanes
898 together into a single node which contains the nodes in even/odd order
899 by using a lane permute.
901 The lanes in EVEN and ODD are duplicated 2 times inside the vectors.
902 So for a lanes = 4 EVEN contains {EVEN1, EVEN1, EVEN2, EVEN2}.
904 The tree REPRESENTATION is taken from the supplied REP along with the
905 vectype which must be the same between all three nodes.
908 static slp_tree
909 vect_build_combine_node (slp_tree even, slp_tree odd, slp_tree rep)
911 vec<std::pair<unsigned, unsigned> > perm;
912 perm.create (SLP_TREE_LANES (rep));
914 for (unsigned x = 0; x < SLP_TREE_LANES (rep); x+=2)
916 perm.quick_push (std::make_pair (0, x));
917 perm.quick_push (std::make_pair (1, x+1));
920 slp_tree vnode = vect_create_new_slp_node (2, SLP_TREE_CODE (even));
921 SLP_TREE_CODE (vnode) = VEC_PERM_EXPR;
922 SLP_TREE_LANE_PERMUTATION (vnode) = perm;
924 SLP_TREE_CHILDREN (vnode).create (2);
925 SLP_TREE_CHILDREN (vnode).quick_push (even);
926 SLP_TREE_CHILDREN (vnode).quick_push (odd);
927 SLP_TREE_REF_COUNT (even)++;
928 SLP_TREE_REF_COUNT (odd)++;
929 SLP_TREE_REF_COUNT (vnode) = 1;
931 SLP_TREE_LANES (vnode) = SLP_TREE_LANES (rep);
932 gcc_assert (perm.length () == SLP_TREE_LANES (vnode));
933 /* Representation is set to that of the current node as the vectorizer
934 can't deal with VEC_PERMs with no representation, as would be the
935 case with invariants. */
936 SLP_TREE_REPRESENTATIVE (vnode) = SLP_TREE_REPRESENTATIVE (rep);
937 SLP_TREE_VECTYPE (vnode) = SLP_TREE_VECTYPE (rep);
938 return vnode;
941 class complex_mul_pattern : public complex_pattern
943 protected:
944 complex_mul_pattern (slp_tree *node, vec<slp_tree> *m_ops, internal_fn ifn)
945 : complex_pattern (node, m_ops, ifn)
947 this->m_num_args = 2;
950 public:
951 void build (vec_info *);
952 static internal_fn
953 matches (complex_operation_t op, slp_tree_to_load_perm_map_t *, slp_tree *,
954 vec<slp_tree> *);
956 static vect_pattern*
957 recognize (slp_tree_to_load_perm_map_t *, slp_tree *);
959 static vect_pattern*
960 mkInstance (slp_tree *node, vec<slp_tree> *m_ops, internal_fn ifn)
962 return new complex_mul_pattern (node, m_ops, ifn);
967 /* Pattern matcher for trying to match complex multiply pattern in SLP tree
968 If the operation matches then IFN is set to the operation it matched
969 and the arguments to the two replacement statements are put in m_ops.
971 If no match is found then IFN is set to IFN_LAST and m_ops is unchanged.
973 This function matches the patterns shaped as:
975 double ax = (b[i+1] * a[i]);
976 double bx = (a[i+1] * b[i]);
978 c[i] = c[i] - ax;
979 c[i+1] = c[i+1] + bx;
981 If a match occurred then TRUE is returned, else FALSE. The initial match is
982 expected to be in OP1 and the initial match operands in args0. */
984 internal_fn
985 complex_mul_pattern::matches (complex_operation_t op,
986 slp_tree_to_load_perm_map_t *perm_cache,
987 slp_tree *node, vec<slp_tree> *ops)
989 internal_fn ifn = IFN_LAST;
991 if (op != MINUS_PLUS)
992 return IFN_LAST;
994 slp_tree root = *node;
995 /* First two nodes must be a multiply. */
996 auto_vec<slp_tree> muls;
997 if (vect_match_call_complex_mla (root, 0) != MULT_MULT
998 || vect_match_call_complex_mla (root, 1, &muls) != MULT_MULT)
999 return IFN_LAST;
1001 /* Now operand2+4 may lead to another expression. */
1002 auto_vec<slp_tree> left_op, right_op;
1003 left_op.safe_splice (SLP_TREE_CHILDREN (muls[0]));
1004 right_op.safe_splice (SLP_TREE_CHILDREN (muls[1]));
1006 if (linear_loads_p (perm_cache, left_op[1]).first == PERM_ODDEVEN)
1007 return IFN_LAST;
1009 bool neg_first = false;
1010 bool conj_first_operand = false;
1011 bool is_neg = vect_normalize_conj_loc (right_op, &neg_first);
1013 if (!is_neg)
1015 /* A multiplication needs to multiply agains the real pair, otherwise
1016 the pattern matches that of FMS. */
1017 if (!vect_validate_multiplication (perm_cache, left_op, PERM_EVENEVEN)
1018 || vect_normalize_conj_loc (left_op))
1019 return IFN_LAST;
1020 ifn = IFN_COMPLEX_MUL;
1022 else if (is_neg)
1024 if (!vect_validate_multiplication (perm_cache, left_op, right_op,
1025 neg_first, &conj_first_operand,
1026 false))
1027 return IFN_LAST;
1029 ifn = IFN_COMPLEX_MUL_CONJ;
1032 if (!vect_pattern_validate_optab (ifn, *node))
1033 return IFN_LAST;
1035 ops->truncate (0);
1036 ops->create (3);
1038 complex_perm_kinds_t kind = linear_loads_p (perm_cache, left_op[0]).first;
1039 if (kind == PERM_EVENODD)
1041 ops->quick_push (left_op[1]);
1042 ops->quick_push (right_op[1]);
1043 ops->quick_push (left_op[0]);
1045 else if (kind == PERM_TOP)
1047 ops->quick_push (left_op[1]);
1048 ops->quick_push (right_op[1]);
1049 ops->quick_push (left_op[0]);
1051 else if (kind == PERM_EVENEVEN && !conj_first_operand)
1053 ops->quick_push (left_op[0]);
1054 ops->quick_push (right_op[0]);
1055 ops->quick_push (left_op[1]);
1057 else
1059 ops->quick_push (left_op[0]);
1060 ops->quick_push (right_op[1]);
1061 ops->quick_push (left_op[1]);
1064 return ifn;
1067 /* Attempt to recognize a complex mul pattern. */
1069 vect_pattern*
1070 complex_mul_pattern::recognize (slp_tree_to_load_perm_map_t *perm_cache,
1071 slp_tree *node)
1073 auto_vec<slp_tree> ops;
1074 complex_operation_t op
1075 = vect_detect_pair_op (*node, true, &ops);
1076 internal_fn ifn
1077 = complex_mul_pattern::matches (op, perm_cache, node, &ops);
1078 if (ifn == IFN_LAST)
1079 return NULL;
1081 return new complex_mul_pattern (node, &ops, ifn);
1084 /* Perform a replacement of the detected complex mul pattern with the new
1085 instruction sequences. */
1087 void
1088 complex_mul_pattern::build (vec_info *vinfo)
1090 slp_tree node;
1091 unsigned i;
1092 slp_tree newnode
1093 = vect_build_combine_node (this->m_ops[0], this->m_ops[1], *this->m_node);
1094 SLP_TREE_REF_COUNT (this->m_ops[2])++;
1096 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (*this->m_node), i, node)
1097 vect_free_slp_tree (node);
1099 /* First re-arrange the children. */
1100 SLP_TREE_CHILDREN (*this->m_node).reserve_exact (2);
1101 SLP_TREE_CHILDREN (*this->m_node)[0] = this->m_ops[2];
1102 SLP_TREE_CHILDREN (*this->m_node)[1] = newnode;
1104 /* And then rewrite the node itself. */
1105 complex_pattern::build (vinfo);
1108 /*******************************************************************************
1109 * complex_fma_pattern class
1110 ******************************************************************************/
1112 class complex_fma_pattern : public complex_pattern
1114 protected:
1115 complex_fma_pattern (slp_tree *node, vec<slp_tree> *m_ops, internal_fn ifn)
1116 : complex_pattern (node, m_ops, ifn)
1118 this->m_num_args = 3;
1121 public:
1122 void build (vec_info *);
1123 static internal_fn
1124 matches (complex_operation_t op, slp_tree_to_load_perm_map_t *, slp_tree *,
1125 vec<slp_tree> *);
1127 static vect_pattern*
1128 recognize (slp_tree_to_load_perm_map_t *, slp_tree *);
1130 static vect_pattern*
1131 mkInstance (slp_tree *node, vec<slp_tree> *m_ops, internal_fn ifn)
1133 return new complex_fma_pattern (node, m_ops, ifn);
1137 /* Pattern matcher for trying to match complex multiply and accumulate
1138 and multiply and subtract patterns in SLP tree.
1139 If the operation matches then IFN is set to the operation it matched and
1140 the arguments to the two replacement statements are put in m_ops.
1142 If no match is found then IFN is set to IFN_LAST and m_ops is unchanged.
1144 This function matches the patterns shaped as:
1146 double ax = (b[i+1] * a[i]) + (b[i] * a[i]);
1147 double bx = (a[i+1] * b[i]) - (a[i+1] * b[i+1]);
1149 c[i] = c[i] - ax;
1150 c[i+1] = c[i+1] + bx;
1152 If a match occurred then TRUE is returned, else FALSE. The match is
1153 performed after COMPLEX_MUL which would have done the majority of the work.
1154 This function merely matches an ADD with a COMPLEX_MUL IFN. The initial
1155 match is expected to be in OP1 and the initial match operands in args0. */
1157 internal_fn
1158 complex_fma_pattern::matches (complex_operation_t op,
1159 slp_tree_to_load_perm_map_t * /* perm_cache */,
1160 slp_tree *ref_node, vec<slp_tree> *ops)
1162 internal_fn ifn = IFN_LAST;
1164 /* Find the two components. We match Complex MUL first which reduces the
1165 amount of work this pattern has to do. After that we just match the
1166 head node and we're done.:
1168 * FMA: + +.
1170 We need to ignore the two_operands nodes that may also match.
1171 For that we can check if they have any scalar statements and also
1172 check that it's not a permute node as we're looking for a normal
1173 PLUS_EXPR operation. */
1174 if (op != CMPLX_NONE)
1175 return IFN_LAST;
1177 /* Find the two components. We match Complex MUL first which reduces the
1178 amount of work this pattern has to do. After that we just match the
1179 head node and we're done.:
1181 * FMA: + + on a non-two_operands node. */
1182 slp_tree vnode = *ref_node;
1183 if (SLP_TREE_LANE_PERMUTATION (vnode).exists ()
1184 || !SLP_TREE_CHILDREN (vnode).exists ()
1185 || !vect_match_expression_p (vnode, PLUS_EXPR))
1186 return IFN_LAST;
1188 slp_tree node = SLP_TREE_CHILDREN (vnode)[1];
1190 if (vect_match_call_p (node, IFN_COMPLEX_MUL))
1191 ifn = IFN_COMPLEX_FMA;
1192 else if (vect_match_call_p (node, IFN_COMPLEX_MUL_CONJ))
1193 ifn = IFN_COMPLEX_FMA_CONJ;
1194 else
1195 return IFN_LAST;
1197 if (!vect_pattern_validate_optab (ifn, vnode))
1198 return IFN_LAST;
1200 ops->truncate (0);
1201 ops->create (3);
1203 if (ifn == IFN_COMPLEX_FMA)
1205 ops->quick_push (SLP_TREE_CHILDREN (vnode)[0]);
1206 ops->quick_push (SLP_TREE_CHILDREN (node)[1]);
1207 ops->quick_push (SLP_TREE_CHILDREN (node)[0]);
1209 else
1211 ops->quick_push (SLP_TREE_CHILDREN (vnode)[0]);
1212 ops->quick_push (SLP_TREE_CHILDREN (node)[0]);
1213 ops->quick_push (SLP_TREE_CHILDREN (node)[1]);
1216 return ifn;
1219 /* Attempt to recognize a complex mul pattern. */
1221 vect_pattern*
1222 complex_fma_pattern::recognize (slp_tree_to_load_perm_map_t *perm_cache,
1223 slp_tree *node)
1225 auto_vec<slp_tree> ops;
1226 complex_operation_t op
1227 = vect_detect_pair_op (*node, true, &ops);
1228 internal_fn ifn
1229 = complex_fma_pattern::matches (op, perm_cache, node, &ops);
1230 if (ifn == IFN_LAST)
1231 return NULL;
1233 return new complex_fma_pattern (node, &ops, ifn);
1236 /* Perform a replacement of the detected complex mul pattern with the new
1237 instruction sequences. */
1239 void
1240 complex_fma_pattern::build (vec_info *vinfo)
1242 slp_tree node = SLP_TREE_CHILDREN (*this->m_node)[1];
1244 SLP_TREE_CHILDREN (*this->m_node).release ();
1245 SLP_TREE_CHILDREN (*this->m_node).create (3);
1246 SLP_TREE_CHILDREN (*this->m_node).safe_splice (this->m_ops);
1248 SLP_TREE_REF_COUNT (this->m_ops[1])++;
1249 SLP_TREE_REF_COUNT (this->m_ops[2])++;
1251 vect_free_slp_tree (node);
1253 complex_pattern::build (vinfo);
1256 /*******************************************************************************
1257 * complex_fms_pattern class
1258 ******************************************************************************/
1260 class complex_fms_pattern : public complex_pattern
1262 protected:
1263 complex_fms_pattern (slp_tree *node, vec<slp_tree> *m_ops, internal_fn ifn)
1264 : complex_pattern (node, m_ops, ifn)
1266 this->m_num_args = 3;
1269 public:
1270 void build (vec_info *);
1271 static internal_fn
1272 matches (complex_operation_t op, slp_tree_to_load_perm_map_t *, slp_tree *,
1273 vec<slp_tree> *);
1275 static vect_pattern*
1276 recognize (slp_tree_to_load_perm_map_t *, slp_tree *);
1278 static vect_pattern*
1279 mkInstance (slp_tree *node, vec<slp_tree> *m_ops, internal_fn ifn)
1281 return new complex_fms_pattern (node, m_ops, ifn);
1286 /* Pattern matcher for trying to match complex multiply and accumulate
1287 and multiply and subtract patterns in SLP tree.
1288 If the operation matches then IFN is set to the operation it matched and
1289 the arguments to the two replacement statements are put in m_ops.
1291 If no match is found then IFN is set to IFN_LAST and m_ops is unchanged.
1293 This function matches the patterns shaped as:
1295 double ax = (b[i+1] * a[i]) + (b[i] * a[i]);
1296 double bx = (a[i+1] * b[i]) - (a[i+1] * b[i+1]);
1298 c[i] = c[i] - ax;
1299 c[i+1] = c[i+1] + bx;
1301 If a match occurred then TRUE is returned, else FALSE. The initial match is
1302 expected to be in OP1 and the initial match operands in args0. */
1304 internal_fn
1305 complex_fms_pattern::matches (complex_operation_t op,
1306 slp_tree_to_load_perm_map_t *perm_cache,
1307 slp_tree * ref_node, vec<slp_tree> *ops)
1309 internal_fn ifn = IFN_LAST;
1311 /* Find the two components. We match Complex MUL first which reduces the
1312 amount of work this pattern has to do. After that we just match the
1313 head node and we're done.:
1315 * FMS: - +. */
1316 slp_tree child = NULL;
1318 /* We need to ignore the two_operands nodes that may also match,
1319 for that we can check if they have any scalar statements and also
1320 check that it's not a permute node as we're looking for a normal
1321 PLUS_EXPR operation. */
1322 if (op != PLUS_MINUS)
1323 return IFN_LAST;
1325 child = SLP_TREE_CHILDREN ((*ops)[1])[1];
1326 if (vect_detect_pair_op (child) != MINUS_PLUS)
1327 return IFN_LAST;
1329 /* First two nodes must be a multiply. */
1330 auto_vec<slp_tree> muls;
1331 if (vect_match_call_complex_mla (child, 0) != MULT_MULT
1332 || vect_match_call_complex_mla (child, 1, &muls) != MULT_MULT)
1333 return IFN_LAST;
1335 /* Now operand2+4 may lead to another expression. */
1336 auto_vec<slp_tree> left_op, right_op;
1337 left_op.safe_splice (SLP_TREE_CHILDREN (muls[0]));
1338 right_op.safe_splice (SLP_TREE_CHILDREN (muls[1]));
1340 bool is_neg = vect_normalize_conj_loc (left_op);
1342 child = SLP_TREE_CHILDREN ((*ops)[1])[0];
1343 bool conj_first_operand = false;
1344 if (!vect_validate_multiplication (perm_cache, right_op, left_op, false,
1345 &conj_first_operand, true))
1346 return IFN_LAST;
1348 if (!is_neg)
1349 ifn = IFN_COMPLEX_FMS;
1350 else if (is_neg)
1351 ifn = IFN_COMPLEX_FMS_CONJ;
1353 if (!vect_pattern_validate_optab (ifn, *ref_node))
1354 return IFN_LAST;
1356 ops->truncate (0);
1357 ops->create (4);
1359 complex_perm_kinds_t kind = linear_loads_p (perm_cache, right_op[0]).first;
1360 if (kind == PERM_EVENODD)
1362 ops->quick_push (child);
1363 ops->quick_push (right_op[0]);
1364 ops->quick_push (right_op[1]);
1365 ops->quick_push (left_op[1]);
1367 else if (kind == PERM_TOP)
1369 ops->quick_push (child);
1370 ops->quick_push (right_op[1]);
1371 ops->quick_push (right_op[0]);
1372 ops->quick_push (left_op[0]);
1374 else if (kind == PERM_EVENEVEN && !is_neg)
1376 ops->quick_push (child);
1377 ops->quick_push (right_op[1]);
1378 ops->quick_push (right_op[0]);
1379 ops->quick_push (left_op[0]);
1381 else
1383 ops->quick_push (child);
1384 ops->quick_push (right_op[1]);
1385 ops->quick_push (right_op[0]);
1386 ops->quick_push (left_op[1]);
1389 return ifn;
1392 /* Attempt to recognize a complex mul pattern. */
1394 vect_pattern*
1395 complex_fms_pattern::recognize (slp_tree_to_load_perm_map_t *perm_cache,
1396 slp_tree *node)
1398 auto_vec<slp_tree> ops;
1399 complex_operation_t op
1400 = vect_detect_pair_op (*node, true, &ops);
1401 internal_fn ifn
1402 = complex_fms_pattern::matches (op, perm_cache, node, &ops);
1403 if (ifn == IFN_LAST)
1404 return NULL;
1406 return new complex_fms_pattern (node, &ops, ifn);
1409 /* Perform a replacement of the detected complex mul pattern with the new
1410 instruction sequences. */
1412 void
1413 complex_fms_pattern::build (vec_info *vinfo)
1415 slp_tree node;
1416 unsigned i;
1417 slp_tree newnode =
1418 vect_build_combine_node (this->m_ops[2], this->m_ops[3], *this->m_node);
1419 SLP_TREE_REF_COUNT (this->m_ops[0])++;
1420 SLP_TREE_REF_COUNT (this->m_ops[1])++;
1422 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (*this->m_node), i, node)
1423 vect_free_slp_tree (node);
1425 SLP_TREE_CHILDREN (*this->m_node).release ();
1426 SLP_TREE_CHILDREN (*this->m_node).create (3);
1428 /* First re-arrange the children. */
1429 SLP_TREE_CHILDREN (*this->m_node).quick_push (this->m_ops[0]);
1430 SLP_TREE_CHILDREN (*this->m_node).quick_push (this->m_ops[1]);
1431 SLP_TREE_CHILDREN (*this->m_node).quick_push (newnode);
1433 /* And then rewrite the node itself. */
1434 complex_pattern::build (vinfo);
1437 /*******************************************************************************
1438 * complex_operations_pattern class
1439 ******************************************************************************/
1441 /* This function combines all the existing pattern matchers above into one class
1442 that shares the functionality between them. The initial match is shared
1443 between all complex operations. */
1445 class complex_operations_pattern : public complex_pattern
1447 protected:
1448 complex_operations_pattern (slp_tree *node, vec<slp_tree> *m_ops,
1449 internal_fn ifn)
1450 : complex_pattern (node, m_ops, ifn)
1452 this->m_num_args = 0;
1455 public:
1456 void build (vec_info *);
1457 static internal_fn
1458 matches (complex_operation_t op, slp_tree_to_load_perm_map_t *, slp_tree *,
1459 vec<slp_tree> *);
1461 static vect_pattern*
1462 recognize (slp_tree_to_load_perm_map_t *, slp_tree *);
1465 /* Dummy matches implementation for proxy object. */
1467 internal_fn
1468 complex_operations_pattern::
1469 matches (complex_operation_t /* op */,
1470 slp_tree_to_load_perm_map_t * /* perm_cache */,
1471 slp_tree * /* ref_node */, vec<slp_tree> * /* ops */)
1473 return IFN_LAST;
1476 /* Attempt to recognize a complex mul pattern. */
1478 vect_pattern*
1479 complex_operations_pattern::recognize (slp_tree_to_load_perm_map_t *perm_cache,
1480 slp_tree *node)
1482 auto_vec<slp_tree> ops;
1483 complex_operation_t op
1484 = vect_detect_pair_op (*node, true, &ops);
1485 internal_fn ifn = IFN_LAST;
1487 ifn = complex_fms_pattern::matches (op, perm_cache, node, &ops);
1488 if (ifn != IFN_LAST)
1489 return complex_fms_pattern::mkInstance (node, &ops, ifn);
1491 ifn = complex_mul_pattern::matches (op, perm_cache, node, &ops);
1492 if (ifn != IFN_LAST)
1493 return complex_mul_pattern::mkInstance (node, &ops, ifn);
1495 ifn = complex_fma_pattern::matches (op, perm_cache, node, &ops);
1496 if (ifn != IFN_LAST)
1497 return complex_fma_pattern::mkInstance (node, &ops, ifn);
1499 ifn = complex_add_pattern::matches (op, perm_cache, node, &ops);
1500 if (ifn != IFN_LAST)
1501 return complex_add_pattern::mkInstance (node, &ops, ifn);
1503 return NULL;
1506 /* Dummy implementation of build. */
1508 void
1509 complex_operations_pattern::build (vec_info * /* vinfo */)
1511 gcc_unreachable ();
1514 /*******************************************************************************
1515 * Pattern matching definitions
1516 ******************************************************************************/
1518 #define SLP_PATTERN(x) &x::recognize
1519 vect_pattern_decl_t slp_patterns[]
1521 /* For least amount of back-tracking and more efficient matching
1522 order patterns from the largest to the smallest. Especially if they
1523 overlap in what they can detect. */
1525 SLP_PATTERN (complex_operations_pattern),
1527 #undef SLP_PATTERN
1529 /* Set the number of SLP pattern matchers available. */
1530 size_t num__slp_patterns = sizeof(slp_patterns)/sizeof(vect_pattern_decl_t);