You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

613 lines
20KB

  1. /* A class for building vector constant patterns.
  2. Copyright (C) 2017-2020 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. Shape is the type that specifies the number of elements in the vector
  37. and (where relevant) the type of each element.
  38. The derived class Derived provides the functionality of this class
  39. for specific Ts. Derived needs to provide the following interface:
  40. bool equal_p (T elt1, T elt2) const;
  41. Return true if elements ELT1 and ELT2 are equal.
  42. bool allow_steps_p () const;
  43. Return true if a stepped representation is OK. We don't allow
  44. linear series for anything other than integers, to avoid problems
  45. with rounding.
  46. bool integral_p (T elt) const;
  47. Return true if element ELT can be interpreted as an integer.
  48. StepType step (T elt1, T elt2) const;
  49. Return the value of element ELT2 minus the value of element ELT1,
  50. given integral_p (ELT1) && integral_p (ELT2). There is no fixed
  51. choice of StepType.
  52. T apply_step (T base, unsigned int factor, StepType step) const;
  53. Return a vector element with the value BASE + FACTOR * STEP.
  54. bool can_elide_p (T elt) const;
  55. Return true if we can drop element ELT, even if the retained
  56. elements are different. This is provided for TREE_OVERFLOW
  57. handling.
  58. void note_representative (T *elt1_ptr, T elt2);
  59. Record that ELT2 is being elided, given that ELT1_PTR points to
  60. the last encoded element for the containing pattern. This is
  61. again provided for TREE_OVERFLOW handling.
  62. static poly_uint64 shape_nelts (Shape shape);
  63. Return the number of elements in SHAPE.
  64. The class provides additional functionality for the case in which
  65. T can describe a vector constant as well as an individual element.
  66. This functionality requires:
  67. static poly_uint64 nelts_of (T x);
  68. Return the number of elements in vector constant X.
  69. static unsigned int npatterns_of (T x);
  70. Return the number of patterns used to encode vector constant X.
  71. static unsigned int nelts_per_pattern_of (T x);
  72. Return the number of elements used to encode each pattern
  73. in vector constant X. */
  74. template<typename T, typename Shape, typename Derived>
  75. class vector_builder : public auto_vec<T, 32>
  76. {
  77. public:
  78. vector_builder ();
  79. poly_uint64 full_nelts () const { return m_full_nelts; }
  80. unsigned int npatterns () const { return m_npatterns; }
  81. unsigned int nelts_per_pattern () const { return m_nelts_per_pattern; }
  82. unsigned int encoded_nelts () const;
  83. bool encoded_full_vector_p () const;
  84. T elt (unsigned int) const;
  85. unsigned int count_dups (int, int, int) const;
  86. bool operator == (const Derived &) const;
  87. bool operator != (const Derived &x) const { return !operator == (x); }
  88. bool new_unary_operation (Shape, T, bool);
  89. bool new_binary_operation (Shape, T, T, bool);
  90. void finalize ();
  91. static unsigned int binary_encoded_nelts (T, T);
  92. protected:
  93. void new_vector (poly_uint64, unsigned int, unsigned int);
  94. void reshape (unsigned int, unsigned int);
  95. bool repeating_sequence_p (unsigned int, unsigned int, unsigned int);
  96. bool stepped_sequence_p (unsigned int, unsigned int, unsigned int);
  97. bool try_npatterns (unsigned int);
  98. private:
  99. vector_builder (const vector_builder &);
  100. vector_builder &operator= (const vector_builder &);
  101. Derived *derived () { return static_cast<Derived *> (this); }
  102. const Derived *derived () const;
  103. poly_uint64 m_full_nelts;
  104. unsigned int m_npatterns;
  105. unsigned int m_nelts_per_pattern;
  106. };
  107. template<typename T, typename Shape, typename Derived>
  108. inline const Derived *
  109. vector_builder<T, Shape, Derived>::derived () const
  110. {
  111. return static_cast<const Derived *> (this);
  112. }
  113. template<typename T, typename Shape, typename Derived>
  114. inline
  115. vector_builder<T, Shape, Derived>::vector_builder ()
  116. : m_full_nelts (0),
  117. m_npatterns (0),
  118. m_nelts_per_pattern (0)
  119. {}
  120. /* Return the number of elements that are explicitly encoded. The vec
  121. starts with these explicitly-encoded elements and may contain additional
  122. elided elements. */
  123. template<typename T, typename Shape, typename Derived>
  124. inline unsigned int
  125. vector_builder<T, Shape, Derived>::encoded_nelts () const
  126. {
  127. return m_npatterns * m_nelts_per_pattern;
  128. }
  129. /* Return true if every element of the vector is explicitly encoded. */
  130. template<typename T, typename Shape, typename Derived>
  131. inline bool
  132. vector_builder<T, Shape, Derived>::encoded_full_vector_p () const
  133. {
  134. return known_eq (m_npatterns * m_nelts_per_pattern, m_full_nelts);
  135. }
  136. /* Start building a vector that has FULL_NELTS elements. Initially
  137. encode it using NPATTERNS patterns with NELTS_PER_PATTERN each. */
  138. template<typename T, typename Shape, typename Derived>
  139. void
  140. vector_builder<T, Shape, Derived>::new_vector (poly_uint64 full_nelts,
  141. unsigned int npatterns,
  142. unsigned int nelts_per_pattern)
  143. {
  144. m_full_nelts = full_nelts;
  145. m_npatterns = npatterns;
  146. m_nelts_per_pattern = nelts_per_pattern;
  147. this->reserve (encoded_nelts ());
  148. this->truncate (0);
  149. }
  150. /* Return true if this vector and OTHER have the same elements and
  151. are encoded in the same way. */
  152. template<typename T, typename Shape, typename Derived>
  153. bool
  154. vector_builder<T, Shape, Derived>::operator == (const Derived &other) const
  155. {
  156. if (maybe_ne (m_full_nelts, other.m_full_nelts)
  157. || m_npatterns != other.m_npatterns
  158. || m_nelts_per_pattern != other.m_nelts_per_pattern)
  159. return false;
  160. unsigned int nelts = encoded_nelts ();
  161. for (unsigned int i = 0; i < nelts; ++i)
  162. if (!derived ()->equal_p ((*this)[i], other[i]))
  163. return false;
  164. return true;
  165. }
  166. /* Return the value of vector element I, which might or might not be
  167. encoded explicitly. */
  168. template<typename T, typename Shape, typename Derived>
  169. T
  170. vector_builder<T, Shape, Derived>::elt (unsigned int i) const
  171. {
  172. /* First handle elements that are already present in the underlying
  173. vector, regardless of whether they're part of the encoding or not. */
  174. if (i < this->length ())
  175. return (*this)[i];
  176. /* Extrapolation is only possible if the encoding has been fully
  177. populated. */
  178. gcc_checking_assert (encoded_nelts () <= this->length ());
  179. /* Identify the pattern that contains element I and work out the index of
  180. the last encoded element for that pattern. */
  181. unsigned int pattern = i % m_npatterns;
  182. unsigned int count = i / m_npatterns;
  183. unsigned int final_i = encoded_nelts () - m_npatterns + pattern;
  184. T final = (*this)[final_i];
  185. /* If there are no steps, the final encoded value is the right one. */
  186. if (m_nelts_per_pattern <= 2)
  187. return final;
  188. /* Otherwise work out the value from the last two encoded elements. */
  189. T prev = (*this)[final_i - m_npatterns];
  190. return derived ()->apply_step (final, count - 2,
  191. derived ()->step (prev, final));
  192. }
  193. /* Try to start building a new vector of shape SHAPE that holds the result of
  194. a unary operation on vector constant VEC. ALLOW_STEPPED_P is true if the
  195. operation can handle stepped encodings directly, without having to expand
  196. the full sequence.
  197. Return true if the operation is possible, which it always is when
  198. ALLOW_STEPPED_P is true. Leave the builder unchanged otherwise. */
  199. template<typename T, typename Shape, typename Derived>
  200. bool
  201. vector_builder<T, Shape, Derived>::new_unary_operation (Shape shape, T vec,
  202. bool allow_stepped_p)
  203. {
  204. poly_uint64 full_nelts = Derived::shape_nelts (shape);
  205. gcc_assert (known_eq (full_nelts, Derived::nelts_of (vec)));
  206. unsigned int npatterns = Derived::npatterns_of (vec);
  207. unsigned int nelts_per_pattern = Derived::nelts_per_pattern_of (vec);
  208. if (!allow_stepped_p && nelts_per_pattern > 2)
  209. {
  210. if (!full_nelts.is_constant ())
  211. return false;
  212. npatterns = full_nelts.to_constant ();
  213. nelts_per_pattern = 1;
  214. }
  215. derived ()->new_vector (shape, npatterns, nelts_per_pattern);
  216. return true;
  217. }
  218. /* Try to start building a new vector of shape SHAPE that holds the result of
  219. a binary operation on vector constants VEC1 and VEC2. ALLOW_STEPPED_P is
  220. true if the operation can handle stepped encodings directly, without
  221. having to expand the full sequence.
  222. Return true if the operation is possible. Leave the builder unchanged
  223. otherwise. */
  224. template<typename T, typename Shape, typename Derived>
  225. bool
  226. vector_builder<T, Shape, Derived>::new_binary_operation (Shape shape,
  227. T vec1, T vec2,
  228. bool allow_stepped_p)
  229. {
  230. poly_uint64 full_nelts = Derived::shape_nelts (shape);
  231. gcc_assert (known_eq (full_nelts, Derived::nelts_of (vec1))
  232. && known_eq (full_nelts, Derived::nelts_of (vec2)));
  233. /* Conceptually we split the patterns in VEC1 and VEC2 until we have
  234. an equal number for both. Each split pattern requires the same
  235. number of elements per pattern as the original. E.g. splitting:
  236. { 1, 2, 3, ... }
  237. into two gives:
  238. { 1, 3, 5, ... }
  239. { 2, 4, 6, ... }
  240. while splitting:
  241. { 1, 0, ... }
  242. into two gives:
  243. { 1, 0, ... }
  244. { 0, 0, ... }. */
  245. unsigned int npatterns
  246. = least_common_multiple (Derived::npatterns_of (vec1),
  247. Derived::npatterns_of (vec2));
  248. unsigned int nelts_per_pattern
  249. = MAX (Derived::nelts_per_pattern_of (vec1),
  250. Derived::nelts_per_pattern_of (vec2));
  251. if (!allow_stepped_p && nelts_per_pattern > 2)
  252. {
  253. if (!full_nelts.is_constant ())
  254. return false;
  255. npatterns = full_nelts.to_constant ();
  256. nelts_per_pattern = 1;
  257. }
  258. derived ()->new_vector (shape, npatterns, nelts_per_pattern);
  259. return true;
  260. }
  261. /* Return the number of elements that the caller needs to operate on in
  262. order to handle a binary operation on vector constants VEC1 and VEC2.
  263. This static function is used instead of new_binary_operation if the
  264. result of the operation is not a constant vector. */
  265. template<typename T, typename Shape, typename Derived>
  266. unsigned int
  267. vector_builder<T, Shape, Derived>::binary_encoded_nelts (T vec1, T vec2)
  268. {
  269. poly_uint64 nelts = Derived::nelts_of (vec1);
  270. gcc_assert (known_eq (nelts, Derived::nelts_of (vec2)));
  271. /* See new_binary_operation for details. */
  272. unsigned int npatterns
  273. = least_common_multiple (Derived::npatterns_of (vec1),
  274. Derived::npatterns_of (vec2));
  275. unsigned int nelts_per_pattern
  276. = MAX (Derived::nelts_per_pattern_of (vec1),
  277. Derived::nelts_per_pattern_of (vec2));
  278. unsigned HOST_WIDE_INT const_nelts;
  279. if (nelts.is_constant (&const_nelts))
  280. return MIN (npatterns * nelts_per_pattern, const_nelts);
  281. return npatterns * nelts_per_pattern;
  282. }
  283. /* Return the number of leading duplicate elements in the range
  284. [START:END:STEP]. The value is always at least 1. */
  285. template<typename T, typename Shape, typename Derived>
  286. unsigned int
  287. vector_builder<T, Shape, Derived>::count_dups (int start, int end,
  288. int step) const
  289. {
  290. gcc_assert ((end - start) % step == 0);
  291. unsigned int ndups = 1;
  292. for (int i = start + step;
  293. i != end && derived ()->equal_p (elt (i), elt (start));
  294. i += step)
  295. ndups++;
  296. return ndups;
  297. }
  298. /* Change the encoding to NPATTERNS patterns of NELTS_PER_PATTERN each,
  299. but without changing the underlying vector. */
  300. template<typename T, typename Shape, typename Derived>
  301. void
  302. vector_builder<T, Shape, Derived>::reshape (unsigned int npatterns,
  303. unsigned int nelts_per_pattern)
  304. {
  305. unsigned int old_encoded_nelts = encoded_nelts ();
  306. unsigned int new_encoded_nelts = npatterns * nelts_per_pattern;
  307. gcc_checking_assert (new_encoded_nelts <= old_encoded_nelts);
  308. unsigned int next = new_encoded_nelts - npatterns;
  309. for (unsigned int i = new_encoded_nelts; i < old_encoded_nelts; ++i)
  310. {
  311. derived ()->note_representative (&(*this)[next], (*this)[i]);
  312. next += 1;
  313. if (next == new_encoded_nelts)
  314. next -= npatterns;
  315. }
  316. m_npatterns = npatterns;
  317. m_nelts_per_pattern = nelts_per_pattern;
  318. }
  319. /* Return true if elements [START, END) contain a repeating sequence of
  320. STEP elements. */
  321. template<typename T, typename Shape, typename Derived>
  322. bool
  323. vector_builder<T, Shape, Derived>::repeating_sequence_p (unsigned int start,
  324. unsigned int end,
  325. unsigned int step)
  326. {
  327. for (unsigned int i = start; i < end - step; ++i)
  328. if (!derived ()->equal_p ((*this)[i], (*this)[i + step]))
  329. return false;
  330. return true;
  331. }
  332. /* Return true if elements [START, END) contain STEP interleaved linear
  333. series. */
  334. template<typename T, typename Shape, typename Derived>
  335. bool
  336. vector_builder<T, Shape, Derived>::stepped_sequence_p (unsigned int start,
  337. unsigned int end,
  338. unsigned int step)
  339. {
  340. if (!derived ()->allow_steps_p ())
  341. return false;
  342. for (unsigned int i = start + step * 2; i < end; ++i)
  343. {
  344. T elt1 = (*this)[i - step * 2];
  345. T elt2 = (*this)[i - step];
  346. T elt3 = (*this)[i];
  347. if (!derived ()->integral_p (elt1)
  348. || !derived ()->integral_p (elt2)
  349. || !derived ()->integral_p (elt3))
  350. return false;
  351. if (maybe_ne (derived ()->step (elt1, elt2),
  352. derived ()->step (elt2, elt3)))
  353. return false;
  354. if (!derived ()->can_elide_p (elt3))
  355. return false;
  356. }
  357. return true;
  358. }
  359. /* Try to change the number of encoded patterns to NPATTERNS, returning
  360. true on success. */
  361. template<typename T, typename Shape, typename Derived>
  362. bool
  363. vector_builder<T, Shape, Derived>::try_npatterns (unsigned int npatterns)
  364. {
  365. if (m_nelts_per_pattern == 1)
  366. {
  367. /* See whether NPATTERNS is valid with the current 1-element-per-pattern
  368. encoding. */
  369. if (repeating_sequence_p (0, encoded_nelts (), npatterns))
  370. {
  371. reshape (npatterns, 1);
  372. return true;
  373. }
  374. /* We can only increase the number of elements per pattern if all
  375. elements are still encoded explicitly. */
  376. if (!encoded_full_vector_p ())
  377. return false;
  378. }
  379. if (m_nelts_per_pattern <= 2)
  380. {
  381. /* See whether NPATTERNS is valid with a 2-element-per-pattern
  382. encoding. */
  383. if (repeating_sequence_p (npatterns, encoded_nelts (), npatterns))
  384. {
  385. reshape (npatterns, 2);
  386. return true;
  387. }
  388. /* We can only increase the number of elements per pattern if all
  389. elements are still encoded explicitly. */
  390. if (!encoded_full_vector_p ())
  391. return false;
  392. }
  393. if (m_nelts_per_pattern <= 3)
  394. {
  395. /* See whether we have NPATTERNS interleaved linear series,
  396. giving a 3-element-per-pattern encoding. */
  397. if (stepped_sequence_p (npatterns, encoded_nelts (), npatterns))
  398. {
  399. reshape (npatterns, 3);
  400. return true;
  401. }
  402. return false;
  403. }
  404. gcc_unreachable ();
  405. }
  406. /* Replace the current encoding with the canonical form. */
  407. template<typename T, typename Shape, typename Derived>
  408. void
  409. vector_builder<T, Shape, Derived>::finalize ()
  410. {
  411. /* The encoding requires the same number of elements to come from each
  412. pattern. */
  413. gcc_assert (multiple_p (m_full_nelts, m_npatterns));
  414. /* Allow the caller to build more elements than necessary. For example,
  415. it's often convenient to build a stepped vector from the natural
  416. encoding of three elements even if the vector itself only has two. */
  417. unsigned HOST_WIDE_INT const_full_nelts;
  418. if (m_full_nelts.is_constant (&const_full_nelts)
  419. && const_full_nelts <= encoded_nelts ())
  420. {
  421. m_npatterns = const_full_nelts;
  422. m_nelts_per_pattern = 1;
  423. }
  424. /* Try to whittle down the number of elements per pattern. That is:
  425. 1. If we have stepped patterns whose steps are all 0, reduce the
  426. number of elements per pattern from 3 to 2.
  427. 2. If we have background fill values that are the same as the
  428. foreground values, reduce the number of elements per pattern
  429. from 2 to 1. */
  430. while (m_nelts_per_pattern > 1
  431. && repeating_sequence_p (encoded_nelts () - m_npatterns * 2,
  432. encoded_nelts (), m_npatterns))
  433. /* The last two sequences of M_NPATTERNS elements are equal,
  434. so remove the last one. */
  435. reshape (m_npatterns, m_nelts_per_pattern - 1);
  436. if (pow2p_hwi (m_npatterns))
  437. {
  438. /* Try to halve the number of patterns while doing so gives a
  439. valid pattern. This approach is linear in the number of
  440. elements, whereas searcing from 1 up would be O(n*log(n)).
  441. Each halving step tries to keep the number of elements per pattern
  442. the same. If that isn't possible, and if all elements are still
  443. explicitly encoded, the halving step can instead increase the number
  444. of elements per pattern.
  445. E.g. for:
  446. { 0, 2, 3, 4, 5, 6, 7, 8 } npatterns == 8 full_nelts == 8
  447. we first realize that the second half of the sequence is not
  448. equal to the first, so we cannot maintain 1 element per pattern
  449. for npatterns == 4. Instead we halve the number of patterns
  450. and double the number of elements per pattern, treating this
  451. as a "foreground" { 0, 2, 3, 4 } against a "background" of
  452. { 5, 6, 7, 8 | 5, 6, 7, 8 ... }:
  453. { 0, 2, 3, 4 | 5, 6, 7, 8 } npatterns == 4
  454. Next we realize that this is *not* a foreround of { 0, 2 }
  455. against a background of { 3, 4 | 3, 4 ... }, so the only
  456. remaining option for reducing the number of patterns is
  457. to use a foreground of { 0, 2 } against a stepped background
  458. of { 1, 2 | 3, 4 | 5, 6 ... }. This is valid because we still
  459. haven't elided any elements:
  460. { 0, 2 | 3, 4 | 5, 6 } npatterns == 2
  461. This in turn can be reduced to a foreground of { 0 } against a
  462. stepped background of { 1 | 2 | 3 ... }:
  463. { 0 | 2 | 3 } npatterns == 1
  464. This last step would not have been possible for:
  465. { 0, 0 | 3, 4 | 5, 6 } npatterns == 2. */
  466. while ((m_npatterns & 1) == 0 && try_npatterns (m_npatterns / 2))
  467. continue;
  468. /* Builders of arbitrary fixed-length vectors can use:
  469. new_vector (x, x, 1)
  470. so that every element is specified explicitly. Handle cases
  471. that are actually wrapping series, like { 0, 1, 2, 3, 0, 1, 2, 3 }
  472. would be for 2-bit elements. We'll have treated them as
  473. duplicates in the loop above. */
  474. if (m_nelts_per_pattern == 1
  475. && m_full_nelts.is_constant (&const_full_nelts)
  476. && this->length () >= const_full_nelts
  477. && (m_npatterns & 3) == 0
  478. && stepped_sequence_p (m_npatterns / 4, const_full_nelts,
  479. m_npatterns / 4))
  480. {
  481. reshape (m_npatterns / 4, 3);
  482. while ((m_npatterns & 1) == 0 && try_npatterns (m_npatterns / 2))
  483. continue;
  484. }
  485. }
  486. else
  487. /* For the non-power-of-2 case, do a simple search up from 1. */
  488. for (unsigned int i = 1; i <= m_npatterns / 2; ++i)
  489. if (m_npatterns % i == 0 && try_npatterns (i))
  490. break;
  491. }
  492. #endif