vector-builder.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /* A class for building vector constant patterns.
  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_VECTOR_BUILDER_H
  16. #define GCC_VECTOR_BUILDER_H
  17. /* This class is a wrapper around auto_vec<T> for building vectors of T.
  18. It aims to encode each vector as npatterns interleaved patterns,
  19. where each pattern represents a sequence:
  20. { BASE0, BASE1, BASE1 + STEP, BASE1 + STEP*2, BASE1 + STEP*3, ... }
  21. The first three elements in each pattern provide enough information
  22. to derive the other elements. If all patterns have a STEP of zero,
  23. we only need to encode the first two elements in each pattern.
  24. If BASE1 is also equal to BASE0 for all patterns, we only need to
  25. encode the first element in each pattern. The number of encoded
  26. elements per pattern is given by nelts_per_pattern.
  27. The class can be used in two ways:
  28. 1. It can be used to build a full image of the vector, which is then
  29. canonicalized by finalize (). In this case npatterns is initially
  30. the number of elements in the vector and nelts_per_pattern is
  31. initially 1.
  32. 2. It can be used to build a vector that already has a known encoding.
  33. This is preferred since it is more efficient and copes with
  34. variable-length vectors. finalize () then canonicalizes the encoding
  35. to a simpler form if possible.
  36. The derived class Derived provides this functionality for specific Ts.
  37. Derived needs to provide the following interface:
  38. bool equal_p (T elt1, T elt2) const;
  39. Return true if elements ELT1 and ELT2 are equal.
  40. bool allow_steps_p () const;
  41. Return true if a stepped representation is OK. We don't allow
  42. linear series for anything other than integers, to avoid problems
  43. with rounding.
  44. bool integral_p (T elt) const;
  45. Return true if element ELT can be interpreted as an integer.
  46. StepType step (T elt1, T elt2) const;
  47. Return the value of element ELT2 minus the value of element ELT1,
  48. given integral_p (ELT1) && integral_p (ELT2). There is no fixed
  49. choice of StepType.
  50. T apply_step (T base, unsigned int factor, StepType step) const;
  51. Return a vector element with the value BASE + FACTOR * STEP.
  52. bool can_elide_p (T elt) const;
  53. Return true if we can drop element ELT, even if the retained
  54. elements are different. This is provided for TREE_OVERFLOW
  55. handling.
  56. void note_representative (T *elt1_ptr, T elt2);
  57. Record that ELT2 is being elided, given that ELT1_PTR points to
  58. the last encoded element for the containing pattern. This is
  59. again provided for TREE_OVERFLOW handling. */
  60. template<typename T, typename Derived>
  61. class vector_builder : public auto_vec<T, 32>
  62. {
  63. public:
  64. vector_builder ();
  65. poly_uint64 full_nelts () const { return m_full_nelts; }
  66. unsigned int npatterns () const { return m_npatterns; }
  67. unsigned int nelts_per_pattern () const { return m_nelts_per_pattern; }
  68. unsigned int encoded_nelts () const;
  69. bool encoded_full_vector_p () const;
  70. T elt (unsigned int) const;
  71. bool operator == (const Derived &) const;
  72. bool operator != (const Derived &x) const { return !operator == (x); }
  73. void finalize ();
  74. protected:
  75. void new_vector (poly_uint64, unsigned int, unsigned int);
  76. void reshape (unsigned int, unsigned int);
  77. bool repeating_sequence_p (unsigned int, unsigned int, unsigned int);
  78. bool stepped_sequence_p (unsigned int, unsigned int, unsigned int);
  79. bool try_npatterns (unsigned int);
  80. private:
  81. vector_builder (const vector_builder &);
  82. vector_builder &operator= (const vector_builder &);
  83. Derived *derived () { return static_cast<Derived *> (this); }
  84. const Derived *derived () const;
  85. poly_uint64 m_full_nelts;
  86. unsigned int m_npatterns;
  87. unsigned int m_nelts_per_pattern;
  88. };
  89. template<typename T, typename Derived>
  90. inline const Derived *
  91. vector_builder<T, Derived>::derived () const
  92. {
  93. return static_cast<const Derived *> (this);
  94. }
  95. template<typename T, typename Derived>
  96. inline
  97. vector_builder<T, Derived>::vector_builder ()
  98. : m_full_nelts (0),
  99. m_npatterns (0),
  100. m_nelts_per_pattern (0)
  101. {}
  102. /* Return the number of elements that are explicitly encoded. The vec
  103. starts with these explicitly-encoded elements and may contain additional
  104. elided elements. */
  105. template<typename T, typename Derived>
  106. inline unsigned int
  107. vector_builder<T, Derived>::encoded_nelts () const
  108. {
  109. return m_npatterns * m_nelts_per_pattern;
  110. }
  111. /* Return true if every element of the vector is explicitly encoded. */
  112. template<typename T, typename Derived>
  113. inline bool
  114. vector_builder<T, Derived>::encoded_full_vector_p () const
  115. {
  116. return known_eq (m_npatterns * m_nelts_per_pattern, m_full_nelts);
  117. }
  118. /* Start building a vector that has FULL_NELTS elements. Initially
  119. encode it using NPATTERNS patterns with NELTS_PER_PATTERN each. */
  120. template<typename T, typename Derived>
  121. void
  122. vector_builder<T, Derived>::new_vector (poly_uint64 full_nelts,
  123. unsigned int npatterns,
  124. unsigned int nelts_per_pattern)
  125. {
  126. m_full_nelts = full_nelts;
  127. m_npatterns = npatterns;
  128. m_nelts_per_pattern = nelts_per_pattern;
  129. this->reserve (encoded_nelts ());
  130. this->truncate (0);
  131. }
  132. /* Return true if this vector and OTHER have the same elements and
  133. are encoded in the same way. */
  134. template<typename T, typename Derived>
  135. bool
  136. vector_builder<T, Derived>::operator == (const Derived &other) const
  137. {
  138. if (maybe_ne (m_full_nelts, other.m_full_nelts)
  139. || m_npatterns != other.m_npatterns
  140. || m_nelts_per_pattern != other.m_nelts_per_pattern)
  141. return false;
  142. unsigned int nelts = encoded_nelts ();
  143. for (unsigned int i = 0; i < nelts; ++i)
  144. if (!derived ()->equal_p ((*this)[i], other[i]))
  145. return false;
  146. return true;
  147. }
  148. /* Return the value of vector element I, which might or might not be
  149. encoded explicitly. */
  150. template<typename T, typename Derived>
  151. T
  152. vector_builder<T, Derived>::elt (unsigned int i) const
  153. {
  154. /* This only makes sense if the encoding has been fully populated. */
  155. gcc_checking_assert (encoded_nelts () <= this->length ());
  156. /* First handle elements that are already present in the underlying
  157. vector, regardless of whether they're part of the encoding or not. */
  158. if (i < this->length ())
  159. return (*this)[i];
  160. /* Identify the pattern that contains element I and work out the index of
  161. the last encoded element for that pattern. */
  162. unsigned int pattern = i % m_npatterns;
  163. unsigned int count = i / m_npatterns;
  164. unsigned int final_i = encoded_nelts () - m_npatterns + pattern;
  165. T final = (*this)[final_i];
  166. /* If there are no steps, the final encoded value is the right one. */
  167. if (m_nelts_per_pattern <= 2)
  168. return final;
  169. /* Otherwise work out the value from the last two encoded elements. */
  170. T prev = (*this)[final_i - m_npatterns];
  171. return derived ()->apply_step (final, count - 2,
  172. derived ()->step (prev, final));
  173. }
  174. /* Change the encoding to NPATTERNS patterns of NELTS_PER_PATTERN each,
  175. but without changing the underlying vector. */
  176. template<typename T, typename Derived>
  177. void
  178. vector_builder<T, Derived>::reshape (unsigned int npatterns,
  179. unsigned int nelts_per_pattern)
  180. {
  181. unsigned int old_encoded_nelts = encoded_nelts ();
  182. unsigned int new_encoded_nelts = npatterns * nelts_per_pattern;
  183. gcc_checking_assert (new_encoded_nelts <= old_encoded_nelts);
  184. unsigned int next = new_encoded_nelts - npatterns;
  185. for (unsigned int i = new_encoded_nelts; i < old_encoded_nelts; ++i)
  186. {
  187. derived ()->note_representative (&(*this)[next], (*this)[i]);
  188. next += 1;
  189. if (next == new_encoded_nelts)
  190. next -= npatterns;
  191. }
  192. m_npatterns = npatterns;
  193. m_nelts_per_pattern = nelts_per_pattern;
  194. }
  195. /* Return true if elements [START, END) contain a repeating sequence of
  196. STEP elements. */
  197. template<typename T, typename Derived>
  198. bool
  199. vector_builder<T, Derived>::repeating_sequence_p (unsigned int start,
  200. unsigned int end,
  201. unsigned int step)
  202. {
  203. for (unsigned int i = start; i < end - step; ++i)
  204. if (!derived ()->equal_p ((*this)[i], (*this)[i + step]))
  205. return false;
  206. return true;
  207. }
  208. /* Return true if elements [START, END) contain STEP interleaved linear
  209. series. */
  210. template<typename T, typename Derived>
  211. bool
  212. vector_builder<T, Derived>::stepped_sequence_p (unsigned int start,
  213. unsigned int end,
  214. unsigned int step)
  215. {
  216. if (!derived ()->allow_steps_p ())
  217. return false;
  218. for (unsigned int i = start + step * 2; i < end; ++i)
  219. {
  220. T elt1 = (*this)[i - step * 2];
  221. T elt2 = (*this)[i - step];
  222. T elt3 = (*this)[i];
  223. if (!derived ()->integral_p (elt1)
  224. || !derived ()->integral_p (elt2)
  225. || !derived ()->integral_p (elt3))
  226. return false;
  227. if (maybe_ne (derived ()->step (elt1, elt2),
  228. derived ()->step (elt2, elt3)))
  229. return false;
  230. if (!derived ()->can_elide_p (elt3))
  231. return false;
  232. }
  233. return true;
  234. }
  235. /* Try to change the number of encoded patterns to NPATTERNS, returning
  236. true on success. */
  237. template<typename T, typename Derived>
  238. bool
  239. vector_builder<T, Derived>::try_npatterns (unsigned int npatterns)
  240. {
  241. if (m_nelts_per_pattern == 1)
  242. {
  243. /* See whether NPATTERNS is valid with the current 1-element-per-pattern
  244. encoding. */
  245. if (repeating_sequence_p (0, encoded_nelts (), npatterns))
  246. {
  247. reshape (npatterns, 1);
  248. return true;
  249. }
  250. /* We can only increase the number of elements per pattern if all
  251. elements are still encoded explicitly. */
  252. if (!encoded_full_vector_p ())
  253. return false;
  254. }
  255. if (m_nelts_per_pattern <= 2)
  256. {
  257. /* See whether NPATTERNS is valid with a 2-element-per-pattern
  258. encoding. */
  259. if (repeating_sequence_p (npatterns, encoded_nelts (), npatterns))
  260. {
  261. reshape (npatterns, 2);
  262. return true;
  263. }
  264. /* We can only increase the number of elements per pattern if all
  265. elements are still encoded explicitly. */
  266. if (!encoded_full_vector_p ())
  267. return false;
  268. }
  269. if (m_nelts_per_pattern <= 3)
  270. {
  271. /* See whether we have NPATTERNS interleaved linear series,
  272. giving a 3-element-per-pattern encoding. */
  273. if (stepped_sequence_p (npatterns, encoded_nelts (), npatterns))
  274. {
  275. reshape (npatterns, 3);
  276. return true;
  277. }
  278. return false;
  279. }
  280. gcc_unreachable ();
  281. }
  282. /* Replace the current encoding with the canonical form. */
  283. template<typename T, typename Derived>
  284. void
  285. vector_builder<T, Derived>::finalize ()
  286. {
  287. /* The encoding requires the same number of elements to come from each
  288. pattern. */
  289. gcc_assert (multiple_p (m_full_nelts, m_npatterns));
  290. /* Allow the caller to build more elements than necessary. For example,
  291. it's often convenient to build a stepped vector from the natural
  292. encoding of three elements even if the vector itself only has two. */
  293. unsigned HOST_WIDE_INT const_full_nelts;
  294. if (m_full_nelts.is_constant (&const_full_nelts)
  295. && const_full_nelts <= encoded_nelts ())
  296. {
  297. m_npatterns = const_full_nelts;
  298. m_nelts_per_pattern = 1;
  299. }
  300. /* Try to whittle down the number of elements per pattern. That is:
  301. 1. If we have stepped patterns whose steps are all 0, reduce the
  302. number of elements per pattern from 3 to 2.
  303. 2. If we have background fill values that are the same as the
  304. foreground values, reduce the number of elements per pattern
  305. from 2 to 1. */
  306. while (m_nelts_per_pattern > 1
  307. && repeating_sequence_p (encoded_nelts () - m_npatterns * 2,
  308. encoded_nelts (), m_npatterns))
  309. /* The last two sequences of M_NPATTERNS elements are equal,
  310. so remove the last one. */
  311. reshape (m_npatterns, m_nelts_per_pattern - 1);
  312. if (pow2p_hwi (m_npatterns))
  313. {
  314. /* Try to halve the number of patterns while doing so gives a
  315. valid pattern. This approach is linear in the number of
  316. elements, whereas searcing from 1 up would be O(n*log(n)).
  317. Each halving step tries to keep the number of elements per pattern
  318. the same. If that isn't possible, and if all elements are still
  319. explicitly encoded, the halving step can instead increase the number
  320. of elements per pattern.
  321. E.g. for:
  322. { 0, 2, 3, 4, 5, 6, 7, 8 } npatterns == 8 full_nelts == 8
  323. we first realize that the second half of the sequence is not
  324. equal to the first, so we cannot maintain 1 element per pattern
  325. for npatterns == 4. Instead we halve the number of patterns
  326. and double the number of elements per pattern, treating this
  327. as a "foreground" { 0, 2, 3, 4 } against a "background" of
  328. { 5, 6, 7, 8 | 5, 6, 7, 8 ... }:
  329. { 0, 2, 3, 4 | 5, 6, 7, 8 } npatterns == 4
  330. Next we realize that this is *not* a foreround of { 0, 2 }
  331. against a background of { 3, 4 | 3, 4 ... }, so the only
  332. remaining option for reducing the number of patterns is
  333. to use a foreground of { 0, 2 } against a stepped background
  334. of { 1, 2 | 3, 4 | 5, 6 ... }. This is valid because we still
  335. haven't elided any elements:
  336. { 0, 2 | 3, 4 | 5, 6 } npatterns == 2
  337. This in turn can be reduced to a foreground of { 0 } against a
  338. stepped background of { 1 | 2 | 3 ... }:
  339. { 0 | 2 | 3 } npatterns == 1
  340. This last step would not have been possible for:
  341. { 0, 0 | 3, 4 | 5, 6 } npatterns == 2. */
  342. while ((m_npatterns & 1) == 0 && try_npatterns (m_npatterns / 2))
  343. continue;
  344. /* Builders of arbitrary fixed-length vectors can use:
  345. new_vector (x, x, 1)
  346. so that every element is specified explicitly. Handle cases
  347. that are actually wrapping series, like { 0, 1, 2, 3, 0, 1, 2, 3 }
  348. would be for 2-bit elements. We'll have treated them as
  349. duplicates in the loop above. */
  350. if (m_nelts_per_pattern == 1
  351. && m_full_nelts.is_constant (&const_full_nelts)
  352. && this->length () >= const_full_nelts
  353. && (m_npatterns & 3) == 0
  354. && stepped_sequence_p (m_npatterns / 4, const_full_nelts,
  355. m_npatterns / 4))
  356. {
  357. reshape (m_npatterns / 4, 3);
  358. while ((m_npatterns & 1) == 0 && try_npatterns (m_npatterns / 2))
  359. continue;
  360. }
  361. }
  362. else
  363. /* For the non-power-of-2 case, do a simple search up from 1. */
  364. for (unsigned int i = 1; i <= m_npatterns / 2; ++i)
  365. if (m_npatterns % i == 0 && try_npatterns (i))
  366. break;
  367. }
  368. #endif