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.

526 lines
16KB

  1. /* Sets (bit vectors) of hard registers, and operations on them.
  2. Copyright (C) 1987-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_HARD_REG_SET_H
  16. #define GCC_HARD_REG_SET_H
  17. #include "array-traits.h"
  18. /* Define the type of a set of hard registers. */
  19. /* HARD_REG_ELT_TYPE is a typedef of the unsigned integral type which
  20. will be used for hard reg sets, either alone or in an array.
  21. If HARD_REG_SET is a macro, its definition is HARD_REG_ELT_TYPE,
  22. and it has enough bits to represent all the target machine's hard
  23. registers. Otherwise, it is a typedef for a suitably sized array
  24. of HARD_REG_ELT_TYPEs. HARD_REG_SET_LONGS is defined as how many.
  25. Note that lots of code assumes that the first part of a regset is
  26. the same format as a HARD_REG_SET. To help make sure this is true,
  27. we only try the widest fast integer mode (HOST_WIDEST_FAST_INT)
  28. instead of all the smaller types. This approach loses only if
  29. there are very few registers and then only in the few cases where
  30. we have an array of HARD_REG_SETs, so it needn't be as complex as
  31. it used to be. */
  32. typedef unsigned HOST_WIDEST_FAST_INT HARD_REG_ELT_TYPE;
  33. #if FIRST_PSEUDO_REGISTER <= HOST_BITS_PER_WIDEST_FAST_INT
  34. typedef HARD_REG_ELT_TYPE HARD_REG_SET;
  35. typedef const HARD_REG_SET const_hard_reg_set;
  36. #else
  37. #define HARD_REG_SET_LONGS \
  38. ((FIRST_PSEUDO_REGISTER + HOST_BITS_PER_WIDEST_FAST_INT - 1) \
  39. / HOST_BITS_PER_WIDEST_FAST_INT)
  40. struct HARD_REG_SET
  41. {
  42. HARD_REG_SET
  43. operator~ () const
  44. {
  45. HARD_REG_SET res;
  46. for (unsigned int i = 0; i < ARRAY_SIZE (elts); ++i)
  47. res.elts[i] = ~elts[i];
  48. return res;
  49. }
  50. HARD_REG_SET
  51. operator& (const HARD_REG_SET &other) const
  52. {
  53. HARD_REG_SET res;
  54. for (unsigned int i = 0; i < ARRAY_SIZE (elts); ++i)
  55. res.elts[i] = elts[i] & other.elts[i];
  56. return res;
  57. }
  58. HARD_REG_SET &
  59. operator&= (const HARD_REG_SET &other)
  60. {
  61. for (unsigned int i = 0; i < ARRAY_SIZE (elts); ++i)
  62. elts[i] &= other.elts[i];
  63. return *this;
  64. }
  65. HARD_REG_SET
  66. operator| (const HARD_REG_SET &other) const
  67. {
  68. HARD_REG_SET res;
  69. for (unsigned int i = 0; i < ARRAY_SIZE (elts); ++i)
  70. res.elts[i] = elts[i] | other.elts[i];
  71. return res;
  72. }
  73. HARD_REG_SET &
  74. operator|= (const HARD_REG_SET &other)
  75. {
  76. for (unsigned int i = 0; i < ARRAY_SIZE (elts); ++i)
  77. elts[i] |= other.elts[i];
  78. return *this;
  79. }
  80. bool
  81. operator== (const HARD_REG_SET &other) const
  82. {
  83. HARD_REG_ELT_TYPE bad = 0;
  84. for (unsigned int i = 0; i < ARRAY_SIZE (elts); ++i)
  85. bad |= (elts[i] ^ other.elts[i]);
  86. return bad == 0;
  87. }
  88. bool
  89. operator!= (const HARD_REG_SET &other) const
  90. {
  91. return !operator== (other);
  92. }
  93. HARD_REG_ELT_TYPE elts[HARD_REG_SET_LONGS];
  94. };
  95. typedef const HARD_REG_SET &const_hard_reg_set;
  96. template<>
  97. struct array_traits<HARD_REG_SET>
  98. {
  99. typedef HARD_REG_ELT_TYPE element_type;
  100. static const bool has_constant_size = true;
  101. static const size_t constant_size = HARD_REG_SET_LONGS;
  102. static const element_type *base (const HARD_REG_SET &x) { return x.elts; }
  103. static size_t size (const HARD_REG_SET &) { return HARD_REG_SET_LONGS; }
  104. };
  105. #endif
  106. /* HARD_REG_SET wrapped into a structure, to make it possible to
  107. use HARD_REG_SET even in APIs that should not include
  108. hard-reg-set.h. */
  109. struct hard_reg_set_container
  110. {
  111. HARD_REG_SET set;
  112. };
  113. /* HARD_CONST is used to cast a constant to the appropriate type
  114. for use with a HARD_REG_SET. */
  115. #define HARD_CONST(X) ((HARD_REG_ELT_TYPE) (X))
  116. /* Define macros SET_HARD_REG_BIT, CLEAR_HARD_REG_BIT and TEST_HARD_REG_BIT
  117. to set, clear or test one bit in a hard reg set of type HARD_REG_SET.
  118. All three take two arguments: the set and the register number.
  119. In the case where sets are arrays of longs, the first argument
  120. is actually a pointer to a long.
  121. Define two macros for initializing a set:
  122. CLEAR_HARD_REG_SET and SET_HARD_REG_SET.
  123. These take just one argument.
  124. Also define:
  125. hard_reg_set_subset_p (X, Y), which returns true if X is a subset of Y.
  126. hard_reg_set_intersect_p (X, Y), which returns true if X and Y intersect.
  127. hard_reg_set_empty_p (X), which returns true if X is empty. */
  128. #define UHOST_BITS_PER_WIDE_INT ((unsigned) HOST_BITS_PER_WIDEST_FAST_INT)
  129. #if FIRST_PSEUDO_REGISTER <= HOST_BITS_PER_WIDEST_FAST_INT
  130. #define SET_HARD_REG_BIT(SET, BIT) \
  131. ((SET) |= HARD_CONST (1) << (BIT))
  132. #define CLEAR_HARD_REG_BIT(SET, BIT) \
  133. ((SET) &= ~(HARD_CONST (1) << (BIT)))
  134. #define TEST_HARD_REG_BIT(SET, BIT) \
  135. (!!((SET) & (HARD_CONST (1) << (BIT))))
  136. #define CLEAR_HARD_REG_SET(TO) ((TO) = HARD_CONST (0))
  137. #define SET_HARD_REG_SET(TO) ((TO) = ~ HARD_CONST (0))
  138. static inline bool
  139. hard_reg_set_subset_p (const_hard_reg_set x, const_hard_reg_set y)
  140. {
  141. return (x & ~y) == HARD_CONST (0);
  142. }
  143. static inline bool
  144. hard_reg_set_intersect_p (const_hard_reg_set x, const_hard_reg_set y)
  145. {
  146. return (x & y) != HARD_CONST (0);
  147. }
  148. static inline bool
  149. hard_reg_set_empty_p (const_hard_reg_set x)
  150. {
  151. return x == HARD_CONST (0);
  152. }
  153. #else
  154. inline void
  155. SET_HARD_REG_BIT (HARD_REG_SET &set, unsigned int bit)
  156. {
  157. set.elts[bit / UHOST_BITS_PER_WIDE_INT]
  158. |= HARD_CONST (1) << (bit % UHOST_BITS_PER_WIDE_INT);
  159. }
  160. inline void
  161. CLEAR_HARD_REG_BIT (HARD_REG_SET &set, unsigned int bit)
  162. {
  163. set.elts[bit / UHOST_BITS_PER_WIDE_INT]
  164. &= ~(HARD_CONST (1) << (bit % UHOST_BITS_PER_WIDE_INT));
  165. }
  166. inline bool
  167. TEST_HARD_REG_BIT (const_hard_reg_set set, unsigned int bit)
  168. {
  169. return (set.elts[bit / UHOST_BITS_PER_WIDE_INT]
  170. & (HARD_CONST (1) << (bit % UHOST_BITS_PER_WIDE_INT)));
  171. }
  172. inline void
  173. CLEAR_HARD_REG_SET (HARD_REG_SET &set)
  174. {
  175. for (unsigned int i = 0; i < ARRAY_SIZE (set.elts); ++i)
  176. set.elts[i] = 0;
  177. }
  178. inline void
  179. SET_HARD_REG_SET (HARD_REG_SET &set)
  180. {
  181. for (unsigned int i = 0; i < ARRAY_SIZE (set.elts); ++i)
  182. set.elts[i] = -1;
  183. }
  184. static inline bool
  185. hard_reg_set_subset_p (const_hard_reg_set x, const_hard_reg_set y)
  186. {
  187. HARD_REG_ELT_TYPE bad = 0;
  188. for (unsigned int i = 0; i < ARRAY_SIZE (x.elts); ++i)
  189. bad |= (x.elts[i] & ~y.elts[i]);
  190. return bad == 0;
  191. }
  192. static inline bool
  193. hard_reg_set_intersect_p (const_hard_reg_set x, const_hard_reg_set y)
  194. {
  195. HARD_REG_ELT_TYPE good = 0;
  196. for (unsigned int i = 0; i < ARRAY_SIZE (x.elts); ++i)
  197. good |= (x.elts[i] & y.elts[i]);
  198. return good != 0;
  199. }
  200. static inline bool
  201. hard_reg_set_empty_p (const_hard_reg_set x)
  202. {
  203. HARD_REG_ELT_TYPE bad = 0;
  204. for (unsigned int i = 0; i < ARRAY_SIZE (x.elts); ++i)
  205. bad |= x.elts[i];
  206. return bad == 0;
  207. }
  208. #endif
  209. /* Iterator for hard register sets. */
  210. struct hard_reg_set_iterator
  211. {
  212. /* Pointer to the current element. */
  213. const HARD_REG_ELT_TYPE *pelt;
  214. /* The length of the set. */
  215. unsigned short length;
  216. /* Word within the current element. */
  217. unsigned short word_no;
  218. /* Contents of the actually processed word. When finding next bit
  219. it is shifted right, so that the actual bit is always the least
  220. significant bit of ACTUAL. */
  221. HARD_REG_ELT_TYPE bits;
  222. };
  223. #define HARD_REG_ELT_BITS UHOST_BITS_PER_WIDE_INT
  224. /* The implementation of the iterator functions is fully analogous to
  225. the bitmap iterators. */
  226. static inline void
  227. hard_reg_set_iter_init (hard_reg_set_iterator *iter, const_hard_reg_set set,
  228. unsigned min, unsigned *regno)
  229. {
  230. #ifdef HARD_REG_SET_LONGS
  231. iter->pelt = set.elts;
  232. iter->length = HARD_REG_SET_LONGS;
  233. #else
  234. iter->pelt = &set;
  235. iter->length = 1;
  236. #endif
  237. iter->word_no = min / HARD_REG_ELT_BITS;
  238. if (iter->word_no < iter->length)
  239. {
  240. iter->bits = iter->pelt[iter->word_no];
  241. iter->bits >>= min % HARD_REG_ELT_BITS;
  242. /* This is required for correct search of the next bit. */
  243. min += !iter->bits;
  244. }
  245. *regno = min;
  246. }
  247. static inline bool
  248. hard_reg_set_iter_set (hard_reg_set_iterator *iter, unsigned *regno)
  249. {
  250. while (1)
  251. {
  252. /* Return false when we're advanced past the end of the set. */
  253. if (iter->word_no >= iter->length)
  254. return false;
  255. if (iter->bits)
  256. {
  257. /* Find the correct bit and return it. */
  258. while (!(iter->bits & 1))
  259. {
  260. iter->bits >>= 1;
  261. *regno += 1;
  262. }
  263. return (*regno < FIRST_PSEUDO_REGISTER);
  264. }
  265. /* Round to the beginning of the next word. */
  266. *regno = (*regno + HARD_REG_ELT_BITS - 1);
  267. *regno -= *regno % HARD_REG_ELT_BITS;
  268. /* Find the next non-zero word. */
  269. while (++iter->word_no < iter->length)
  270. {
  271. iter->bits = iter->pelt[iter->word_no];
  272. if (iter->bits)
  273. break;
  274. *regno += HARD_REG_ELT_BITS;
  275. }
  276. }
  277. }
  278. static inline void
  279. hard_reg_set_iter_next (hard_reg_set_iterator *iter, unsigned *regno)
  280. {
  281. iter->bits >>= 1;
  282. *regno += 1;
  283. }
  284. #define EXECUTE_IF_SET_IN_HARD_REG_SET(SET, MIN, REGNUM, ITER) \
  285. for (hard_reg_set_iter_init (&(ITER), (SET), (MIN), &(REGNUM)); \
  286. hard_reg_set_iter_set (&(ITER), &(REGNUM)); \
  287. hard_reg_set_iter_next (&(ITER), &(REGNUM)))
  288. /* Define some standard sets of registers. */
  289. /* Indexed by hard register number, contains 1 for registers
  290. that are being used for global register decls.
  291. These must be exempt from ordinary flow analysis
  292. and are also considered fixed. */
  293. extern char global_regs[FIRST_PSEUDO_REGISTER];
  294. class simplifiable_subreg;
  295. class subreg_shape;
  296. struct simplifiable_subregs_hasher : nofree_ptr_hash <simplifiable_subreg>
  297. {
  298. typedef const subreg_shape *compare_type;
  299. static inline hashval_t hash (const simplifiable_subreg *);
  300. static inline bool equal (const simplifiable_subreg *, const subreg_shape *);
  301. };
  302. struct target_hard_regs {
  303. void finalize ();
  304. /* The set of registers that actually exist on the current target. */
  305. HARD_REG_SET x_accessible_reg_set;
  306. /* The set of registers that should be considered to be register
  307. operands. It is a subset of x_accessible_reg_set. */
  308. HARD_REG_SET x_operand_reg_set;
  309. /* Indexed by hard register number, contains 1 for registers
  310. that are fixed use (stack pointer, pc, frame pointer, etc.;.
  311. These are the registers that cannot be used to allocate
  312. a pseudo reg whose life does not cross calls. */
  313. char x_fixed_regs[FIRST_PSEUDO_REGISTER];
  314. /* The same info as a HARD_REG_SET. */
  315. HARD_REG_SET x_fixed_reg_set;
  316. /* Indexed by hard register number, contains 1 for registers
  317. that are fixed use or are clobbered by function calls.
  318. These are the registers that cannot be used to allocate
  319. a pseudo reg whose life crosses calls. */
  320. char x_call_used_regs[FIRST_PSEUDO_REGISTER];
  321. /* For targets that use reload rather than LRA, this is the set
  322. of registers that we are able to save and restore around calls
  323. (i.e. those for which we know a suitable mode and set of
  324. load/store instructions exist). For LRA targets it contains
  325. all registers.
  326. This is legacy information and should be removed if all targets
  327. switch to LRA. */
  328. HARD_REG_SET x_savable_regs;
  329. /* Contains registers that are fixed use -- i.e. in fixed_reg_set -- but
  330. only if they are not merely part of that set because they are global
  331. regs. Global regs that are not otherwise fixed can still take part
  332. in register allocation. */
  333. HARD_REG_SET x_fixed_nonglobal_reg_set;
  334. /* Contains 1 for registers that are set or clobbered by calls. */
  335. /* ??? Ideally, this would be just call_used_regs plus global_regs, but
  336. for someone's bright idea to have call_used_regs strictly include
  337. fixed_regs. Which leaves us guessing as to the set of fixed_regs
  338. that are actually preserved. We know for sure that those associated
  339. with the local stack frame are safe, but scant others. */
  340. HARD_REG_SET x_regs_invalidated_by_call;
  341. /* Table of register numbers in the order in which to try to use them. */
  342. int x_reg_alloc_order[FIRST_PSEUDO_REGISTER];
  343. /* The inverse of reg_alloc_order. */
  344. int x_inv_reg_alloc_order[FIRST_PSEUDO_REGISTER];
  345. /* For each reg class, a HARD_REG_SET saying which registers are in it. */
  346. HARD_REG_SET x_reg_class_contents[N_REG_CLASSES];
  347. /* For each reg class, a boolean saying whether the class contains only
  348. fixed registers. */
  349. bool x_class_only_fixed_regs[N_REG_CLASSES];
  350. /* For each reg class, number of regs it contains. */
  351. unsigned int x_reg_class_size[N_REG_CLASSES];
  352. /* For each reg class, table listing all the classes contained in it. */
  353. enum reg_class x_reg_class_subclasses[N_REG_CLASSES][N_REG_CLASSES];
  354. /* For each pair of reg classes,
  355. a largest reg class contained in their union. */
  356. enum reg_class x_reg_class_subunion[N_REG_CLASSES][N_REG_CLASSES];
  357. /* For each pair of reg classes,
  358. the smallest reg class that contains their union. */
  359. enum reg_class x_reg_class_superunion[N_REG_CLASSES][N_REG_CLASSES];
  360. /* Vector indexed by hardware reg giving its name. */
  361. const char *x_reg_names[FIRST_PSEUDO_REGISTER];
  362. /* Records which registers can form a particular subreg, with the subreg
  363. being identified by its outer mode, inner mode and offset. */
  364. hash_table <simplifiable_subregs_hasher> *x_simplifiable_subregs;
  365. };
  366. extern struct target_hard_regs default_target_hard_regs;
  367. #if SWITCHABLE_TARGET
  368. extern struct target_hard_regs *this_target_hard_regs;
  369. #else
  370. #define this_target_hard_regs (&default_target_hard_regs)
  371. #endif
  372. #define accessible_reg_set \
  373. (this_target_hard_regs->x_accessible_reg_set)
  374. #define operand_reg_set \
  375. (this_target_hard_regs->x_operand_reg_set)
  376. #define fixed_regs \
  377. (this_target_hard_regs->x_fixed_regs)
  378. #define fixed_reg_set \
  379. (this_target_hard_regs->x_fixed_reg_set)
  380. #define fixed_nonglobal_reg_set \
  381. (this_target_hard_regs->x_fixed_nonglobal_reg_set)
  382. #ifdef IN_TARGET_CODE
  383. #define call_used_regs \
  384. (this_target_hard_regs->x_call_used_regs)
  385. #endif
  386. #define savable_regs \
  387. (this_target_hard_regs->x_savable_regs)
  388. #ifdef IN_TARGET_CODE
  389. #define regs_invalidated_by_call \
  390. (this_target_hard_regs->x_regs_invalidated_by_call)
  391. #define call_used_or_fixed_regs \
  392. (regs_invalidated_by_call | fixed_reg_set)
  393. #endif
  394. #define reg_alloc_order \
  395. (this_target_hard_regs->x_reg_alloc_order)
  396. #define inv_reg_alloc_order \
  397. (this_target_hard_regs->x_inv_reg_alloc_order)
  398. #define reg_class_contents \
  399. (this_target_hard_regs->x_reg_class_contents)
  400. #define class_only_fixed_regs \
  401. (this_target_hard_regs->x_class_only_fixed_regs)
  402. #define reg_class_size \
  403. (this_target_hard_regs->x_reg_class_size)
  404. #define reg_class_subclasses \
  405. (this_target_hard_regs->x_reg_class_subclasses)
  406. #define reg_class_subunion \
  407. (this_target_hard_regs->x_reg_class_subunion)
  408. #define reg_class_superunion \
  409. (this_target_hard_regs->x_reg_class_superunion)
  410. #define reg_names \
  411. (this_target_hard_regs->x_reg_names)
  412. /* Vector indexed by reg class giving its name. */
  413. extern const char * reg_class_names[];
  414. /* Given a hard REGN a FROM mode and a TO mode, return true if
  415. REGN can change from mode FROM to mode TO. */
  416. #define REG_CAN_CHANGE_MODE_P(REGN, FROM, TO) \
  417. (targetm.can_change_mode_class (FROM, TO, REGNO_REG_CLASS (REGN)))
  418. #ifdef IN_TARGET_CODE
  419. /* Return true if register REGNO is either fixed or call-used
  420. (aka call-clobbered). */
  421. inline bool
  422. call_used_or_fixed_reg_p (unsigned int regno)
  423. {
  424. return fixed_regs[regno] || this_target_hard_regs->x_call_used_regs[regno];
  425. }
  426. #endif
  427. #endif /* ! GCC_HARD_REG_SET_H */