Home | History | Annotate | Line # | Download | only in gcc
vec-perm-indices.h revision 1.1.1.4
      1 /* A representation of vector permutation indices.
      2    Copyright (C) 2017-2022 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 #ifndef GCC_VEC_PERN_INDICES_H
     21 #define GCC_VEC_PERN_INDICES_H 1
     22 
     23 #include "int-vector-builder.h"
     24 
     25 /* A vector_builder for building constant permutation vectors.
     26    The elements do not need to be clamped to a particular range
     27    of input elements.  */
     28 typedef int_vector_builder<poly_int64> vec_perm_builder;
     29 
     30 /* This class represents a constant permutation vector, such as that used
     31    as the final operand to a VEC_PERM_EXPR.
     32 
     33    Permutation vectors select indices modulo the number of input elements,
     34    and the class canonicalizes each permutation vector for a particular
     35    number of input vectors and for a particular number of elements per
     36    input.  For example, the gimple statements:
     37 
     38     _1 = VEC_PERM_EXPR <a, a, { 0, 2, 4, 6, 0, 2, 4, 6 }>;
     39     _2 = VEC_PERM_EXPR <a, a, { 0, 2, 4, 6, 8, 10, 12, 14 }>;
     40     _3 = VEC_PERM_EXPR <a, a, { 0, 2, 20, 22, 24, 2, 4, 14 }>;
     41 
     42    effectively have only a single vector input "a".  If "a" has 8
     43    elements, the indices select elements modulo 8, which makes all three
     44    VEC_PERM_EXPRs equivalent.  The canonical form is for the indices to be
     45    in the range [0, number of input elements - 1], so the class treats the
     46    second and third permutation vectors as though they had been the first.
     47 
     48    The class copes with cases in which the input and output vectors have
     49    different numbers of elements.  */
     50 class vec_perm_indices
     51 {
     52   typedef poly_int64 element_type;
     53 
     54 public:
     55   vec_perm_indices ();
     56   vec_perm_indices (const vec_perm_builder &, unsigned int, poly_uint64);
     57 
     58   void new_vector (const vec_perm_builder &, unsigned int, poly_uint64);
     59   void new_expanded_vector (const vec_perm_indices &, unsigned int);
     60   bool new_shrunk_vector (const vec_perm_indices &, unsigned int);
     61   void rotate_inputs (int delta);
     62 
     63   /* Return the underlying vector encoding.  */
     64   const vec_perm_builder &encoding () const { return m_encoding; }
     65 
     66   /* Return the number of output elements.  This is called length ()
     67      so that we present a more vec-like interface.  */
     68   poly_uint64 length () const { return m_encoding.full_nelts (); }
     69 
     70   /* Return the number of input vectors being permuted.  */
     71   unsigned int ninputs () const { return m_ninputs; }
     72 
     73   /* Return the number of elements in each input vector.  */
     74   poly_uint64 nelts_per_input () const { return m_nelts_per_input; }
     75 
     76   /* Return the total number of input elements.  */
     77   poly_uint64 input_nelts () const { return m_ninputs * m_nelts_per_input; }
     78 
     79   element_type clamp (element_type) const;
     80   element_type operator[] (unsigned int i) const;
     81   bool series_p (unsigned int, unsigned int, element_type, element_type) const;
     82   bool all_in_range_p (element_type, element_type) const;
     83   bool all_from_input_p (unsigned int) const;
     84 
     85 private:
     86   vec_perm_indices (const vec_perm_indices &);
     87 
     88   vec_perm_builder m_encoding;
     89   unsigned int m_ninputs;
     90   poly_uint64 m_nelts_per_input;
     91 };
     92 
     93 bool tree_to_vec_perm_builder (vec_perm_builder *, tree);
     94 tree vec_perm_indices_to_tree (tree, const vec_perm_indices &);
     95 rtx vec_perm_indices_to_rtx (machine_mode, const vec_perm_indices &);
     96 
     97 inline
     98 vec_perm_indices::vec_perm_indices ()
     99   : m_ninputs (0),
    100     m_nelts_per_input (0)
    101 {
    102 }
    103 
    104 /* Construct a permutation vector that selects between NINPUTS vector
    105    inputs that have NELTS_PER_INPUT elements each.  Take the elements of
    106    the new vector from ELEMENTS, clamping each one to be in range.  */
    107 
    108 inline
    109 vec_perm_indices::vec_perm_indices (const vec_perm_builder &elements,
    110 				    unsigned int ninputs,
    111 				    poly_uint64 nelts_per_input)
    112 {
    113   new_vector (elements, ninputs, nelts_per_input);
    114 }
    115 
    116 /* Return the canonical value for permutation vector element ELT,
    117    taking into account the current number of input elements.  */
    118 
    119 inline vec_perm_indices::element_type
    120 vec_perm_indices::clamp (element_type elt) const
    121 {
    122   element_type limit = input_nelts (), elem_within_input;
    123   HOST_WIDE_INT input;
    124   if (!can_div_trunc_p (elt, limit, &input, &elem_within_input))
    125     return elt;
    126 
    127   /* Treat negative elements as counting from the end.  This only matters
    128      if the vector size is not a power of 2.  */
    129   if (known_lt (elem_within_input, 0))
    130     return elem_within_input + limit;
    131 
    132   return elem_within_input;
    133 }
    134 
    135 /* Return the value of vector element I, which might or might not be
    136    explicitly encoded.  */
    137 
    138 inline vec_perm_indices::element_type
    139 vec_perm_indices::operator[] (unsigned int i) const
    140 {
    141   return clamp (m_encoding.elt (i));
    142 }
    143 
    144 /* Return true if the permutation vector only selects elements from
    145    input I.  */
    146 
    147 inline bool
    148 vec_perm_indices::all_from_input_p (unsigned int i) const
    149 {
    150   return all_in_range_p (i * m_nelts_per_input, m_nelts_per_input);
    151 }
    152 
    153 #endif
    154