regs.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /* Define per-register tables for data flow info and register allocation.
  2. Copyright (C) 1987-2015 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_REGS_H
  16. #define GCC_REGS_H
  17. #include "machmode.h"
  18. #include "hard-reg-set.h"
  19. #include "rtl.h"
  20. #define REG_BYTES(R) mode_size[(int) GET_MODE (R)]
  21. /* When you only have the mode of a pseudo register before it has a hard
  22. register chosen for it, this reports the size of each hard register
  23. a pseudo in such a mode would get allocated to. A target may
  24. override this. */
  25. #ifndef REGMODE_NATURAL_SIZE
  26. #define REGMODE_NATURAL_SIZE(MODE) UNITS_PER_WORD
  27. #endif
  28. /* Maximum register number used in this function, plus one. */
  29. extern int max_regno;
  30. /* REG_N_REFS and REG_N_SETS are initialized by a call to
  31. regstat_init_n_sets_and_refs from the current values of
  32. DF_REG_DEF_COUNT and DF_REG_USE_COUNT. REG_N_REFS and REG_N_SETS
  33. should only be used if a pass need to change these values in some
  34. magical way or the pass needs to have accurate values for these
  35. and is not using incremental df scanning.
  36. At the end of a pass that uses REG_N_REFS and REG_N_SETS, a call
  37. should be made to regstat_free_n_sets_and_refs.
  38. Local alloc seems to play pretty loose with these values.
  39. REG_N_REFS is set to 0 if the register is used in an asm.
  40. Furthermore, local_alloc calls regclass to hack both REG_N_REFS and
  41. REG_N_SETS for three address insns. Other passes seem to have
  42. other special values. */
  43. /* Structure to hold values for REG_N_SETS (i) and REG_N_REFS (i). */
  44. struct regstat_n_sets_and_refs_t
  45. {
  46. int sets; /* # of times (REG n) is set */
  47. int refs; /* # of times (REG n) is used or set */
  48. };
  49. extern struct regstat_n_sets_and_refs_t *regstat_n_sets_and_refs;
  50. /* Indexed by n, gives number of times (REG n) is used or set. */
  51. static inline int
  52. REG_N_REFS (int regno)
  53. {
  54. return regstat_n_sets_and_refs[regno].refs;
  55. }
  56. /* Indexed by n, gives number of times (REG n) is used or set. */
  57. #define SET_REG_N_REFS(N,V) (regstat_n_sets_and_refs[N].refs = V)
  58. #define INC_REG_N_REFS(N,V) (regstat_n_sets_and_refs[N].refs += V)
  59. /* Indexed by n, gives number of times (REG n) is set. */
  60. static inline int
  61. REG_N_SETS (int regno)
  62. {
  63. return regstat_n_sets_and_refs[regno].sets;
  64. }
  65. /* Indexed by n, gives number of times (REG n) is set. */
  66. #define SET_REG_N_SETS(N,V) (regstat_n_sets_and_refs[N].sets = V)
  67. #define INC_REG_N_SETS(N,V) (regstat_n_sets_and_refs[N].sets += V)
  68. /* Given a REG, return TRUE if the reg is a PARM_DECL, FALSE otherwise. */
  69. extern bool reg_is_parm_p (rtx);
  70. /* Functions defined in regstat.c. */
  71. extern void regstat_init_n_sets_and_refs (void);
  72. extern void regstat_free_n_sets_and_refs (void);
  73. extern void regstat_compute_ri (void);
  74. extern void regstat_free_ri (void);
  75. extern bitmap regstat_get_setjmp_crosses (void);
  76. extern void regstat_compute_calls_crossed (void);
  77. extern void regstat_free_calls_crossed (void);
  78. extern void dump_reg_info (FILE *);
  79. /* Register information indexed by register number. This structure is
  80. initialized by calling regstat_compute_ri and is destroyed by
  81. calling regstat_free_ri. */
  82. struct reg_info_t
  83. {
  84. int freq; /* # estimated frequency (REG n) is used or set */
  85. int deaths; /* # of times (REG n) dies */
  86. int live_length; /* # of instructions (REG n) is live */
  87. int calls_crossed; /* # of calls (REG n) is live across */
  88. int freq_calls_crossed; /* # estimated frequency (REG n) crosses call */
  89. int throw_calls_crossed; /* # of calls that may throw (REG n) is live across */
  90. int basic_block; /* # of basic blocks (REG n) is used in */
  91. };
  92. extern struct reg_info_t *reg_info_p;
  93. /* The number allocated elements of reg_info_p. */
  94. extern size_t reg_info_p_size;
  95. /* Estimate frequency of references to register N. */
  96. #define REG_FREQ(N) (reg_info_p[N].freq)
  97. /* The weights for each insn varies from 0 to REG_FREQ_BASE.
  98. This constant does not need to be high, as in infrequently executed
  99. regions we want to count instructions equivalently to optimize for
  100. size instead of speed. */
  101. #define REG_FREQ_MAX 1000
  102. /* Compute register frequency from the BB frequency. When optimizing for size,
  103. or profile driven feedback is available and the function is never executed,
  104. frequency is always equivalent. Otherwise rescale the basic block
  105. frequency. */
  106. #define REG_FREQ_FROM_BB(bb) (optimize_function_for_size_p (cfun) \
  107. ? REG_FREQ_MAX \
  108. : ((bb)->frequency * REG_FREQ_MAX / BB_FREQ_MAX)\
  109. ? ((bb)->frequency * REG_FREQ_MAX / BB_FREQ_MAX)\
  110. : 1)
  111. /* Indexed by N, gives number of insns in which register N dies.
  112. Note that if register N is live around loops, it can die
  113. in transitions between basic blocks, and that is not counted here.
  114. So this is only a reliable indicator of how many regions of life there are
  115. for registers that are contained in one basic block. */
  116. #define REG_N_DEATHS(N) (reg_info_p[N].deaths)
  117. /* Get the number of consecutive words required to hold pseudo-reg N. */
  118. #define PSEUDO_REGNO_SIZE(N) \
  119. ((GET_MODE_SIZE (PSEUDO_REGNO_MODE (N)) + UNITS_PER_WORD - 1) \
  120. / UNITS_PER_WORD)
  121. /* Get the number of bytes required to hold pseudo-reg N. */
  122. #define PSEUDO_REGNO_BYTES(N) \
  123. GET_MODE_SIZE (PSEUDO_REGNO_MODE (N))
  124. /* Get the machine mode of pseudo-reg N. */
  125. #define PSEUDO_REGNO_MODE(N) GET_MODE (regno_reg_rtx[N])
  126. /* Indexed by N, gives number of CALL_INSNS across which (REG n) is live. */
  127. #define REG_N_CALLS_CROSSED(N) (reg_info_p[N].calls_crossed)
  128. #define REG_FREQ_CALLS_CROSSED(N) (reg_info_p[N].freq_calls_crossed)
  129. /* Indexed by N, gives number of CALL_INSNS that may throw, across which
  130. (REG n) is live. */
  131. #define REG_N_THROWING_CALLS_CROSSED(N) (reg_info_p[N].throw_calls_crossed)
  132. /* Total number of instructions at which (REG n) is live.
  133. This is set in regstat.c whenever register info is requested and
  134. remains valid for the rest of the compilation of the function; it is
  135. used to control register allocation. The larger this is, the less
  136. priority (REG n) gets for allocation in a hard register (in IRA in
  137. priority-coloring mode).
  138. Negative values are special: -1 is used to mark a pseudo reg that
  139. should not be allocated to a hard register, because it crosses a
  140. setjmp call. */
  141. #define REG_LIVE_LENGTH(N) (reg_info_p[N].live_length)
  142. /* Indexed by n, gives number of basic block that (REG n) is used in.
  143. If the value is REG_BLOCK_GLOBAL (-1),
  144. it means (REG n) is used in more than one basic block.
  145. REG_BLOCK_UNKNOWN (0) means it hasn't been seen yet so we don't know.
  146. This information remains valid for the rest of the compilation
  147. of the current function; it is used to control register allocation. */
  148. #define REG_BLOCK_UNKNOWN 0
  149. #define REG_BLOCK_GLOBAL -1
  150. #define REG_BASIC_BLOCK(N) (reg_info_p[N].basic_block)
  151. /* Vector of substitutions of register numbers,
  152. used to map pseudo regs into hardware regs.
  153. This can't be folded into reg_n_info without changing all of the
  154. machine dependent directories, since the reload functions
  155. in the machine dependent files access it. */
  156. extern short *reg_renumber;
  157. /* Flag set by local-alloc or global-alloc if they decide to allocate
  158. something in a call-clobbered register. */
  159. extern int caller_save_needed;
  160. /* Select a register mode required for caller save of hard regno REGNO. */
  161. #ifndef HARD_REGNO_CALLER_SAVE_MODE
  162. #define HARD_REGNO_CALLER_SAVE_MODE(REGNO, NREGS, MODE) \
  163. choose_hard_reg_mode (REGNO, NREGS, false)
  164. #endif
  165. /* Registers that get partially clobbered by a call in a given mode.
  166. These must not be call used registers. */
  167. #ifndef HARD_REGNO_CALL_PART_CLOBBERED
  168. #define HARD_REGNO_CALL_PART_CLOBBERED(REGNO, MODE) 0
  169. #endif
  170. /* Target-dependent globals. */
  171. struct target_regs {
  172. /* For each starting hard register, the number of consecutive hard
  173. registers that a given machine mode occupies. */
  174. unsigned char x_hard_regno_nregs[FIRST_PSEUDO_REGISTER][MAX_MACHINE_MODE];
  175. /* For each hard register, the widest mode object that it can contain.
  176. This will be a MODE_INT mode if the register can hold integers. Otherwise
  177. it will be a MODE_FLOAT or a MODE_CC mode, whichever is valid for the
  178. register. */
  179. machine_mode x_reg_raw_mode[FIRST_PSEUDO_REGISTER];
  180. /* Vector indexed by machine mode saying whether there are regs of
  181. that mode. */
  182. bool x_have_regs_of_mode[MAX_MACHINE_MODE];
  183. /* 1 if the corresponding class contains a register of the given mode. */
  184. char x_contains_reg_of_mode[N_REG_CLASSES][MAX_MACHINE_MODE];
  185. /* Record for each mode whether we can move a register directly to or
  186. from an object of that mode in memory. If we can't, we won't try
  187. to use that mode directly when accessing a field of that mode. */
  188. char x_direct_load[NUM_MACHINE_MODES];
  189. char x_direct_store[NUM_MACHINE_MODES];
  190. /* Record for each mode whether we can float-extend from memory. */
  191. bool x_float_extend_from_mem[NUM_MACHINE_MODES][NUM_MACHINE_MODES];
  192. };
  193. extern struct target_regs default_target_regs;
  194. #if SWITCHABLE_TARGET
  195. extern struct target_regs *this_target_regs;
  196. #else
  197. #define this_target_regs (&default_target_regs)
  198. #endif
  199. #define hard_regno_nregs \
  200. (this_target_regs->x_hard_regno_nregs)
  201. #define reg_raw_mode \
  202. (this_target_regs->x_reg_raw_mode)
  203. #define have_regs_of_mode \
  204. (this_target_regs->x_have_regs_of_mode)
  205. #define contains_reg_of_mode \
  206. (this_target_regs->x_contains_reg_of_mode)
  207. #define direct_load \
  208. (this_target_regs->x_direct_load)
  209. #define direct_store \
  210. (this_target_regs->x_direct_store)
  211. #define float_extend_from_mem \
  212. (this_target_regs->x_float_extend_from_mem)
  213. /* Return an exclusive upper bound on the registers occupied by hard
  214. register (reg:MODE REGNO). */
  215. static inline unsigned int
  216. end_hard_regno (machine_mode mode, unsigned int regno)
  217. {
  218. return regno + hard_regno_nregs[regno][(int) mode];
  219. }
  220. /* Likewise for hard register X. */
  221. #define END_HARD_REGNO(X) end_hard_regno (GET_MODE (X), REGNO (X))
  222. /* Likewise for hard or pseudo register X. */
  223. #define END_REGNO(X) (HARD_REGISTER_P (X) ? END_HARD_REGNO (X) : REGNO (X) + 1)
  224. /* Add to REGS all the registers required to store a value of mode MODE
  225. in register REGNO. */
  226. static inline void
  227. add_to_hard_reg_set (HARD_REG_SET *regs, machine_mode mode,
  228. unsigned int regno)
  229. {
  230. unsigned int end_regno;
  231. end_regno = end_hard_regno (mode, regno);
  232. do
  233. SET_HARD_REG_BIT (*regs, regno);
  234. while (++regno < end_regno);
  235. }
  236. /* Likewise, but remove the registers. */
  237. static inline void
  238. remove_from_hard_reg_set (HARD_REG_SET *regs, machine_mode mode,
  239. unsigned int regno)
  240. {
  241. unsigned int end_regno;
  242. end_regno = end_hard_regno (mode, regno);
  243. do
  244. CLEAR_HARD_REG_BIT (*regs, regno);
  245. while (++regno < end_regno);
  246. }
  247. /* Return true if REGS contains the whole of (reg:MODE REGNO). */
  248. static inline bool
  249. in_hard_reg_set_p (const HARD_REG_SET regs, machine_mode mode,
  250. unsigned int regno)
  251. {
  252. unsigned int end_regno;
  253. gcc_assert (HARD_REGISTER_NUM_P (regno));
  254. if (!TEST_HARD_REG_BIT (regs, regno))
  255. return false;
  256. end_regno = end_hard_regno (mode, regno);
  257. if (!HARD_REGISTER_NUM_P (end_regno - 1))
  258. return false;
  259. while (++regno < end_regno)
  260. if (!TEST_HARD_REG_BIT (regs, regno))
  261. return false;
  262. return true;
  263. }
  264. /* Return true if (reg:MODE REGNO) includes an element of REGS. */
  265. static inline bool
  266. overlaps_hard_reg_set_p (const HARD_REG_SET regs, machine_mode mode,
  267. unsigned int regno)
  268. {
  269. unsigned int end_regno;
  270. if (TEST_HARD_REG_BIT (regs, regno))
  271. return true;
  272. end_regno = end_hard_regno (mode, regno);
  273. while (++regno < end_regno)
  274. if (TEST_HARD_REG_BIT (regs, regno))
  275. return true;
  276. return false;
  277. }
  278. /* Like add_to_hard_reg_set, but use a REGNO/NREGS range instead of
  279. REGNO and MODE. */
  280. static inline void
  281. add_range_to_hard_reg_set (HARD_REG_SET *regs, unsigned int regno,
  282. int nregs)
  283. {
  284. while (nregs-- > 0)
  285. SET_HARD_REG_BIT (*regs, regno + nregs);
  286. }
  287. /* Likewise, but remove the registers. */
  288. static inline void
  289. remove_range_from_hard_reg_set (HARD_REG_SET *regs, unsigned int regno,
  290. int nregs)
  291. {
  292. while (nregs-- > 0)
  293. CLEAR_HARD_REG_BIT (*regs, regno + nregs);
  294. }
  295. /* Like overlaps_hard_reg_set_p, but use a REGNO/NREGS range instead of
  296. REGNO and MODE. */
  297. static inline bool
  298. range_overlaps_hard_reg_set_p (const HARD_REG_SET set, unsigned regno,
  299. int nregs)
  300. {
  301. while (nregs-- > 0)
  302. if (TEST_HARD_REG_BIT (set, regno + nregs))
  303. return true;
  304. return false;
  305. }
  306. /* Like in_hard_reg_set_p, but use a REGNO/NREGS range instead of
  307. REGNO and MODE. */
  308. static inline bool
  309. range_in_hard_reg_set_p (const HARD_REG_SET set, unsigned regno, int nregs)
  310. {
  311. while (nregs-- > 0)
  312. if (!TEST_HARD_REG_BIT (set, regno + nregs))
  313. return false;
  314. return true;
  315. }
  316. /* Get registers used by given function call instruction. */
  317. extern bool get_call_reg_set_usage (rtx_insn *insn, HARD_REG_SET *reg_set,
  318. HARD_REG_SET default_set);
  319. #endif /* GCC_REGS_H */