tree-vector-builder.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* A class for building vector tree constants.
  2. Copyright (C) 2017-2018 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_TREE_VECTOR_BUILDER_H
  16. #define GCC_TREE_VECTOR_BUILDER_H
  17. #include "vector-builder.h"
  18. /* This class is used to build VECTOR_CSTs from a sequence of elements.
  19. See vector_builder for more details. */
  20. class tree_vector_builder : public vector_builder<tree, tree_vector_builder>
  21. {
  22. typedef vector_builder<tree, tree_vector_builder> parent;
  23. friend class vector_builder<tree, tree_vector_builder>;
  24. public:
  25. tree_vector_builder () : m_type (0) {}
  26. tree_vector_builder (tree, unsigned int, unsigned int);
  27. tree build ();
  28. tree type () const { return m_type; }
  29. void new_vector (tree, unsigned int, unsigned int);
  30. bool new_unary_operation (tree, tree, bool);
  31. bool new_binary_operation (tree, tree, tree, bool);
  32. static unsigned int binary_encoded_nelts (tree, tree);
  33. private:
  34. bool equal_p (const_tree, const_tree) const;
  35. bool allow_steps_p () const;
  36. bool integral_p (const_tree) const;
  37. wide_int step (const_tree, const_tree) const;
  38. tree apply_step (tree, unsigned int, const wide_int &) const;
  39. bool can_elide_p (const_tree) const;
  40. void note_representative (tree *, tree);
  41. tree m_type;
  42. };
  43. /* Create a new builder for a vector of type TYPE. Initially encode the
  44. value as NPATTERNS interleaved patterns with NELTS_PER_PATTERN elements
  45. each. */
  46. inline
  47. tree_vector_builder::tree_vector_builder (tree type, unsigned int npatterns,
  48. unsigned int nelts_per_pattern)
  49. {
  50. new_vector (type, npatterns, nelts_per_pattern);
  51. }
  52. /* Start building a new vector of type TYPE. Initially encode the value
  53. as NPATTERNS interleaved patterns with NELTS_PER_PATTERN elements each. */
  54. inline void
  55. tree_vector_builder::new_vector (tree type, unsigned int npatterns,
  56. unsigned int nelts_per_pattern)
  57. {
  58. m_type = type;
  59. parent::new_vector (TYPE_VECTOR_SUBPARTS (type), npatterns,
  60. nelts_per_pattern);
  61. }
  62. /* Return true if elements I1 and I2 are equal. */
  63. inline bool
  64. tree_vector_builder::equal_p (const_tree elt1, const_tree elt2) const
  65. {
  66. return operand_equal_p (elt1, elt2, 0);
  67. }
  68. /* Return true if a stepped representation is OK. We don't allow
  69. linear series for anything other than integers, to avoid problems
  70. with rounding. */
  71. inline bool
  72. tree_vector_builder::allow_steps_p () const
  73. {
  74. return INTEGRAL_TYPE_P (TREE_TYPE (m_type));
  75. }
  76. /* Return true if ELT can be interpreted as an integer. */
  77. inline bool
  78. tree_vector_builder::integral_p (const_tree elt) const
  79. {
  80. return TREE_CODE (elt) == INTEGER_CST;
  81. }
  82. /* Return the value of element ELT2 minus the value of element ELT1.
  83. Both elements are known to be INTEGER_CSTs. */
  84. inline wide_int
  85. tree_vector_builder::step (const_tree elt1, const_tree elt2) const
  86. {
  87. return wi::to_wide (elt2) - wi::to_wide (elt1);
  88. }
  89. /* Return true if we can drop element ELT, even if the retained elements
  90. are different. Return false if this would mean losing overflow
  91. information. */
  92. inline bool
  93. tree_vector_builder::can_elide_p (const_tree elt) const
  94. {
  95. return !CONSTANT_CLASS_P (elt) || !TREE_OVERFLOW (elt);
  96. }
  97. /* Record that ELT2 is being elided, given that ELT1_PTR points to the last
  98. encoded element for the containing pattern. */
  99. inline void
  100. tree_vector_builder::note_representative (tree *elt1_ptr, tree elt2)
  101. {
  102. if (CONSTANT_CLASS_P (elt2) && TREE_OVERFLOW (elt2))
  103. {
  104. gcc_assert (operand_equal_p (*elt1_ptr, elt2, 0));
  105. if (!TREE_OVERFLOW (elt2))
  106. *elt1_ptr = elt2;
  107. }
  108. }
  109. #endif