LCOV - code coverage report
Current view: top level - gcc - tree-vect-slp-patterns.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 82.2 % 640 526
Test Date: 2026-05-11 19:44:49 Functions: 67.7 % 31 21
Legend: Lines:     hit not hit

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

Generated by: LCOV version 2.4-beta

LCOV profile is generated on x86_64 machine using following configure options: configure --disable-bootstrap --enable-coverage=opt --enable-languages=c,c++,fortran,go,jit,lto,rust,m2 --enable-host-shared. GCC test suite is run with the built compiler.