1*38fd1498Szrj /* A representation of vector permutation indices.
2*38fd1498Szrj    Copyright (C) 2017-2018 Free Software Foundation, Inc.
3*38fd1498Szrj 
4*38fd1498Szrj This file is part of GCC.
5*38fd1498Szrj 
6*38fd1498Szrj GCC is free software; you can redistribute it and/or modify it under
7*38fd1498Szrj the terms of the GNU General Public License as published by the Free
8*38fd1498Szrj Software Foundation; either version 3, or (at your option) any later
9*38fd1498Szrj version.
10*38fd1498Szrj 
11*38fd1498Szrj GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12*38fd1498Szrj WARRANTY; without even the implied warranty of MERCHANTABILITY or
13*38fd1498Szrj FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14*38fd1498Szrj for more details.
15*38fd1498Szrj 
16*38fd1498Szrj You should have received a copy of the GNU General Public License
17*38fd1498Szrj along with GCC; see the file COPYING3.  If not see
18*38fd1498Szrj <http://www.gnu.org/licenses/>.  */
19*38fd1498Szrj 
20*38fd1498Szrj #ifndef GCC_VEC_PERN_INDICES_H
21*38fd1498Szrj #define GCC_VEC_PERN_INDICES_H 1
22*38fd1498Szrj 
23*38fd1498Szrj #include "int-vector-builder.h"
24*38fd1498Szrj 
25*38fd1498Szrj /* A vector_builder for building constant permutation vectors.
26*38fd1498Szrj    The elements do not need to be clamped to a particular range
27*38fd1498Szrj    of input elements.  */
28*38fd1498Szrj typedef int_vector_builder<poly_int64> vec_perm_builder;
29*38fd1498Szrj 
30*38fd1498Szrj /* This class represents a constant permutation vector, such as that used
31*38fd1498Szrj    as the final operand to a VEC_PERM_EXPR.
32*38fd1498Szrj 
33*38fd1498Szrj    Permutation vectors select indices modulo the number of input elements,
34*38fd1498Szrj    and the class canonicalizes each permutation vector for a particular
35*38fd1498Szrj    number of input vectors and for a particular number of elements per
36*38fd1498Szrj    input.  For example, the gimple statements:
37*38fd1498Szrj 
38*38fd1498Szrj     _1 = VEC_PERM_EXPR <a, a, { 0, 2, 4, 6, 0, 2, 4, 6 }>;
39*38fd1498Szrj     _2 = VEC_PERM_EXPR <a, a, { 0, 2, 4, 6, 8, 10, 12, 14 }>;
40*38fd1498Szrj     _3 = VEC_PERM_EXPR <a, a, { 0, 2, 20, 22, 24, 2, 4, 14 }>;
41*38fd1498Szrj 
42*38fd1498Szrj    effectively have only a single vector input "a".  If "a" has 8
43*38fd1498Szrj    elements, the indices select elements modulo 8, which makes all three
44*38fd1498Szrj    VEC_PERM_EXPRs equivalent.  The canonical form is for the indices to be
45*38fd1498Szrj    in the range [0, number of input elements - 1], so the class treats the
46*38fd1498Szrj    second and third permutation vectors as though they had been the first.
47*38fd1498Szrj 
48*38fd1498Szrj    The class copes with cases in which the input and output vectors have
49*38fd1498Szrj    different numbers of elements.  */
50*38fd1498Szrj class vec_perm_indices
51*38fd1498Szrj {
52*38fd1498Szrj   typedef poly_int64 element_type;
53*38fd1498Szrj 
54*38fd1498Szrj public:
55*38fd1498Szrj   vec_perm_indices ();
56*38fd1498Szrj   vec_perm_indices (const vec_perm_builder &, unsigned int, poly_uint64);
57*38fd1498Szrj 
58*38fd1498Szrj   void new_vector (const vec_perm_builder &, unsigned int, poly_uint64);
59*38fd1498Szrj   void new_expanded_vector (const vec_perm_indices &, unsigned int);
60*38fd1498Szrj   void rotate_inputs (int delta);
61*38fd1498Szrj 
62*38fd1498Szrj   /* Return the underlying vector encoding.  */
encoding()63*38fd1498Szrj   const vec_perm_builder &encoding () const { return m_encoding; }
64*38fd1498Szrj 
65*38fd1498Szrj   /* Return the number of output elements.  This is called length ()
66*38fd1498Szrj      so that we present a more vec-like interface.  */
length()67*38fd1498Szrj   poly_uint64 length () const { return m_encoding.full_nelts (); }
68*38fd1498Szrj 
69*38fd1498Szrj   /* Return the number of input vectors being permuted.  */
ninputs()70*38fd1498Szrj   unsigned int ninputs () const { return m_ninputs; }
71*38fd1498Szrj 
72*38fd1498Szrj   /* Return the number of elements in each input vector.  */
nelts_per_input()73*38fd1498Szrj   poly_uint64 nelts_per_input () const { return m_nelts_per_input; }
74*38fd1498Szrj 
75*38fd1498Szrj   /* Return the total number of input elements.  */
input_nelts()76*38fd1498Szrj   poly_uint64 input_nelts () const { return m_ninputs * m_nelts_per_input; }
77*38fd1498Szrj 
78*38fd1498Szrj   element_type clamp (element_type) const;
79*38fd1498Szrj   element_type operator[] (unsigned int i) const;
80*38fd1498Szrj   bool series_p (unsigned int, unsigned int, element_type, element_type) const;
81*38fd1498Szrj   bool all_in_range_p (element_type, element_type) const;
82*38fd1498Szrj   bool all_from_input_p (unsigned int) const;
83*38fd1498Szrj 
84*38fd1498Szrj private:
85*38fd1498Szrj   vec_perm_indices (const vec_perm_indices &);
86*38fd1498Szrj 
87*38fd1498Szrj   vec_perm_builder m_encoding;
88*38fd1498Szrj   unsigned int m_ninputs;
89*38fd1498Szrj   poly_uint64 m_nelts_per_input;
90*38fd1498Szrj };
91*38fd1498Szrj 
92*38fd1498Szrj bool tree_to_vec_perm_builder (vec_perm_builder *, tree);
93*38fd1498Szrj tree vec_perm_indices_to_tree (tree, const vec_perm_indices &);
94*38fd1498Szrj rtx vec_perm_indices_to_rtx (machine_mode, const vec_perm_indices &);
95*38fd1498Szrj 
96*38fd1498Szrj inline
vec_perm_indices()97*38fd1498Szrj vec_perm_indices::vec_perm_indices ()
98*38fd1498Szrj   : m_ninputs (0),
99*38fd1498Szrj     m_nelts_per_input (0)
100*38fd1498Szrj {
101*38fd1498Szrj }
102*38fd1498Szrj 
103*38fd1498Szrj /* Construct a permutation vector that selects between NINPUTS vector
104*38fd1498Szrj    inputs that have NELTS_PER_INPUT elements each.  Take the elements of
105*38fd1498Szrj    the new vector from ELEMENTS, clamping each one to be in range.  */
106*38fd1498Szrj 
107*38fd1498Szrj inline
vec_perm_indices(const vec_perm_builder & elements,unsigned int ninputs,poly_uint64 nelts_per_input)108*38fd1498Szrj vec_perm_indices::vec_perm_indices (const vec_perm_builder &elements,
109*38fd1498Szrj 				    unsigned int ninputs,
110*38fd1498Szrj 				    poly_uint64 nelts_per_input)
111*38fd1498Szrj {
112*38fd1498Szrj   new_vector (elements, ninputs, nelts_per_input);
113*38fd1498Szrj }
114*38fd1498Szrj 
115*38fd1498Szrj /* Return the canonical value for permutation vector element ELT,
116*38fd1498Szrj    taking into account the current number of input elements.  */
117*38fd1498Szrj 
118*38fd1498Szrj inline vec_perm_indices::element_type
clamp(element_type elt)119*38fd1498Szrj vec_perm_indices::clamp (element_type elt) const
120*38fd1498Szrj {
121*38fd1498Szrj   element_type limit = input_nelts (), elem_within_input;
122*38fd1498Szrj   HOST_WIDE_INT input;
123*38fd1498Szrj   if (!can_div_trunc_p (elt, limit, &input, &elem_within_input))
124*38fd1498Szrj     return elt;
125*38fd1498Szrj 
126*38fd1498Szrj   /* Treat negative elements as counting from the end.  This only matters
127*38fd1498Szrj      if the vector size is not a power of 2.  */
128*38fd1498Szrj   if (known_lt (elem_within_input, 0))
129*38fd1498Szrj     return elem_within_input + limit;
130*38fd1498Szrj 
131*38fd1498Szrj   return elem_within_input;
132*38fd1498Szrj }
133*38fd1498Szrj 
134*38fd1498Szrj /* Return the value of vector element I, which might or might not be
135*38fd1498Szrj    explicitly encoded.  */
136*38fd1498Szrj 
137*38fd1498Szrj inline vec_perm_indices::element_type
138*38fd1498Szrj vec_perm_indices::operator[] (unsigned int i) const
139*38fd1498Szrj {
140*38fd1498Szrj   return clamp (m_encoding.elt (i));
141*38fd1498Szrj }
142*38fd1498Szrj 
143*38fd1498Szrj /* Return true if the permutation vector only selects elements from
144*38fd1498Szrj    input I.  */
145*38fd1498Szrj 
146*38fd1498Szrj inline bool
all_from_input_p(unsigned int i)147*38fd1498Szrj vec_perm_indices::all_from_input_p (unsigned int i) const
148*38fd1498Szrj {
149*38fd1498Szrj   return all_in_range_p (i * m_nelts_per_input, m_nelts_per_input);
150*38fd1498Szrj }
151*38fd1498Szrj 
152*38fd1498Szrj #endif
153