vec-perm-indices.h 5.2 KB

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