Updated for libbid move.
[official-gcc.git] / gcc / regstat.c
blob0a028ae12b066220d982c809193a213ddec2cedf
1 /* Scanning of rtl for dataflow analysis.
2 Copyright (C) 2007
3 Free Software Foundation, Inc.
4 Contributed by Kenneth Zadeck (zadeck@naturalbridge.com).
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA.
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "tm.h"
29 #include "rtl.h"
30 #include "tm_p.h"
31 #include "flags.h"
32 #include "regs.h"
33 #include "output.h"
34 #include "except.h"
35 #include "hard-reg-set.h"
36 #include "basic-block.h"
37 #include "timevar.h"
38 #include "df.h"
41 struct regstat_n_sets_and_refs_t *regstat_n_sets_and_refs;
42 struct regstat_n_sets_and_refs_t *regstat_n_sets_and_refs;
44 /*----------------------------------------------------------------------------
45 REG_N_SETS and REG_N_REFS.
46 ----------------------------------------------------------------------------*/
48 /* If a pass need to change these values in some magical way or or the
49 pass needs to have accurate values for these and is not using
50 incremental df scanning, then it should use REG_N_SETS and
51 REG_N_USES. If the pass is doing incremental scanning then it
52 should be getting the info from DF_REG_DEF_COUNT and
53 DF_REG_USE_COUNT. */
55 void
56 regstat_init_n_sets_and_refs (void)
58 unsigned int i;
59 unsigned int max_regno = max_reg_num ();
61 timevar_push (TV_REG_STATS);
62 df_grow_reg_info ();
63 gcc_assert (!regstat_n_sets_and_refs);
65 regstat_n_sets_and_refs = xmalloc (max_regno * sizeof (struct regstat_n_sets_and_refs_t));
67 for (i = 0; i < max_regno; i++)
69 SET_REG_N_SETS (i, DF_REG_DEF_COUNT (i));
70 SET_REG_N_REFS (i, DF_REG_USE_COUNT (i) + REG_N_SETS (i));
72 timevar_pop (TV_REG_STATS);
77 /* Free the array that holds the REG_N_SETS and REG_N_REFS. */
79 void
80 regstat_free_n_sets_and_refs (void)
82 gcc_assert (regstat_n_sets_and_refs);
83 free (regstat_n_sets_and_refs);
84 regstat_n_sets_and_refs = NULL;
88 /*----------------------------------------------------------------------------
89 REGISTER INFORMATION
91 Process REG_N_DEATHS, REG_LIVE_LENGTH, REG_N_CALLS_CROSSED,
92 REG_N_THROWING_CALLS_CROSSED and REG_BASIC_BLOCK.
94 ----------------------------------------------------------------------------*/
96 static bitmap setjmp_crosses;
97 struct reg_info_t *reg_info_p;
99 /* The number allocated elements of reg_info_p. */
100 size_t reg_info_p_size;
102 /* Compute register info: lifetime, bb, and number of defs and uses
103 for basic block BB. The three bitvectors are scratch regs used
104 here. */
106 static void
107 regstat_bb_compute_ri (unsigned int bb_index,
108 bitmap live, bitmap do_not_gen, bitmap artificial_uses,
109 bitmap local_live, bitmap local_processed)
111 basic_block bb = BASIC_BLOCK (bb_index);
112 rtx insn;
113 struct df_ref **def_rec;
114 struct df_ref **use_rec;
115 int luid = 0;
116 bitmap_iterator bi;
117 unsigned int regno;
119 bitmap_copy (live, df_get_live_out (bb));
120 bitmap_clear (artificial_uses);
122 /* Process the regs live at the end of the block. Mark them as
123 not local to any one basic block. */
124 EXECUTE_IF_SET_IN_BITMAP (live, 0, regno, bi)
125 REG_BASIC_BLOCK (regno) = REG_BLOCK_GLOBAL;
127 /* Process the artificial defs and uses at the bottom of the block
128 to begin processing. */
129 for (def_rec = df_get_artificial_defs (bb_index); *def_rec; def_rec++)
131 struct df_ref *def = *def_rec;
132 if ((DF_REF_FLAGS (def) & DF_REF_AT_TOP) == 0)
133 bitmap_clear_bit (live, DF_REF_REGNO (def));
136 for (use_rec = df_get_artificial_uses (bb_index); *use_rec; use_rec++)
138 struct df_ref *use = *use_rec;
139 if ((DF_REF_FLAGS (use) & DF_REF_AT_TOP) == 0)
141 regno = DF_REF_REGNO (use);
142 bitmap_set_bit (live, regno);
143 bitmap_set_bit (artificial_uses, regno);
147 FOR_BB_INSNS_REVERSE (bb, insn)
149 unsigned int uid = INSN_UID (insn);
150 unsigned int regno;
151 bitmap_iterator bi;
152 struct df_mw_hardreg **mws_rec;
153 rtx link;
155 if (!INSN_P (insn))
156 continue;
158 /* Increment the live_length for all of the registers that
159 are are referenced in this block and live at this
160 particular point. */
161 EXECUTE_IF_SET_IN_BITMAP (local_live, 0, regno, bi)
163 REG_LIVE_LENGTH (regno)++;
165 luid++;
167 bitmap_clear (do_not_gen);
169 link = REG_NOTES (insn);
170 while (link)
172 if (REG_NOTE_KIND (link) == REG_DEAD)
173 REG_N_DEATHS(REGNO (XEXP (link, 0)))++;
174 link = XEXP (link, 1);
177 /* Process the defs. */
178 if (CALL_P (insn))
180 bool can_throw = can_throw_internal (insn);
181 bool set_jump = (find_reg_note (insn, REG_SETJMP, NULL) != NULL);
182 EXECUTE_IF_SET_IN_BITMAP (live, 0, regno, bi)
184 REG_N_CALLS_CROSSED (regno)++;
185 if (can_throw)
186 REG_N_THROWING_CALLS_CROSSED (regno)++;
188 /* We have a problem with any pseudoreg that lives
189 across the setjmp. ANSI says that if a user variable
190 does not change in value between the setjmp and the
191 longjmp, then the longjmp preserves it. This
192 includes longjmp from a place where the pseudo
193 appears dead. (In principle, the value still exists
194 if it is in scope.) If the pseudo goes in a hard
195 reg, some other value may occupy that hard reg where
196 this pseudo is dead, thus clobbering the pseudo.
197 Conclusion: such a pseudo must not go in a hard
198 reg. */
199 if (set_jump)
200 bitmap_set_bit (setjmp_crosses, regno);
204 /* We only care about real sets for calls. Clobbers only
205 may clobbers cannot be depended on. */
206 for (mws_rec = DF_INSN_UID_MWS (uid); *mws_rec; mws_rec++)
208 struct df_mw_hardreg *mws = *mws_rec;
209 if (mws->type == DF_REF_REG_DEF)
211 bool all_dead = true;
212 unsigned int r;
214 for (r=mws->start_regno; r <= mws->end_regno; r++)
215 if ((bitmap_bit_p (live, r))
216 || bitmap_bit_p (artificial_uses, r))
218 all_dead = false;
219 break;
222 if (all_dead)
224 unsigned int regno = mws->start_regno;
225 bitmap_set_bit (do_not_gen, regno);
226 /* Only do this if the value is totally dead. */
227 REG_LIVE_LENGTH (regno)++;
232 /* All of the defs except the return value are some sort of
233 clobber. This code is for the return. */
234 for (def_rec = DF_INSN_UID_DEFS (uid); *def_rec; def_rec++)
236 struct df_ref *def = *def_rec;
237 if ((!CALL_P (insn))
238 || (!(DF_REF_FLAGS (def) & (DF_REF_MUST_CLOBBER | DF_REF_MAY_CLOBBER))))
240 unsigned int dregno = DF_REF_REGNO (def);
242 if (bitmap_bit_p (live, dregno))
244 /* If we have seen this regno, then it has already been
245 processed correctly with the per insn increment. If we
246 have not seen it we need to add the length from here to
247 the end of the block to the live length. */
248 if (bitmap_bit_p (local_processed, dregno))
250 if (!(DF_REF_FLAGS (def) & (DF_REF_PARTIAL | DF_REF_CONDITIONAL)))
251 bitmap_clear_bit (local_live, dregno);
253 else
255 bitmap_set_bit (local_processed, dregno);
256 REG_LIVE_LENGTH (dregno) += luid;
259 else if ((!(DF_REF_FLAGS (def) & DF_REF_MW_HARDREG))
260 && (!bitmap_bit_p (artificial_uses, dregno)))
262 REG_LIVE_LENGTH (dregno)++;
265 if (dregno >= FIRST_PSEUDO_REGISTER)
267 REG_FREQ (dregno) += REG_FREQ_FROM_BB (bb);
268 if (REG_BASIC_BLOCK (dregno) == REG_BLOCK_UNKNOWN)
269 REG_BASIC_BLOCK (dregno) = bb->index;
270 else if (REG_BASIC_BLOCK (dregno) != bb->index)
271 REG_BASIC_BLOCK (dregno) = REG_BLOCK_GLOBAL;
274 if (!(DF_REF_FLAGS (def) & (DF_REF_MUST_CLOBBER + DF_REF_MAY_CLOBBER)))
275 bitmap_set_bit (do_not_gen, dregno);
277 /* Kill this register if it is not a subreg store or conditional store. */
278 if (!(DF_REF_FLAGS (def) & (DF_REF_PARTIAL | DF_REF_CONDITIONAL)))
279 bitmap_clear_bit (live, dregno);
283 for (use_rec = DF_INSN_UID_USES (uid); *use_rec; use_rec++)
285 struct df_ref *use = *use_rec;
286 unsigned int uregno = DF_REF_REGNO (use);
288 if (uregno >= FIRST_PSEUDO_REGISTER)
290 REG_FREQ (uregno) += REG_FREQ_FROM_BB (bb);
291 if (REG_BASIC_BLOCK (uregno) == REG_BLOCK_UNKNOWN)
292 REG_BASIC_BLOCK (uregno) = bb->index;
293 else if (REG_BASIC_BLOCK (uregno) != bb->index)
294 REG_BASIC_BLOCK (uregno) = REG_BLOCK_GLOBAL;
297 if (!bitmap_bit_p (live, uregno))
299 /* This register is now live. */
300 bitmap_set_bit (live, uregno);
302 /* If we have seen this regno, then it has already been
303 processed correctly with the per insn increment. If
304 we have not seen it we set the bit so that begins to
305 get processed locally. Note that we don't even get
306 here if the variable was live at the end of the block
307 since just a ref inside the block does not effect the
308 calculations. */
309 REG_LIVE_LENGTH (uregno) ++;
310 bitmap_set_bit (local_live, uregno);
311 bitmap_set_bit (local_processed, uregno);
316 /* Add the length of the block to all of the registers that were not
317 referenced, but still live in this block. */
318 bitmap_and_compl_into (live, local_processed);
319 EXECUTE_IF_SET_IN_BITMAP (live, 0, regno, bi)
320 REG_LIVE_LENGTH (regno) += luid;
322 bitmap_clear (local_processed);
323 bitmap_clear (local_live);
327 /* Compute register info: lifetime, bb, and number of defs and uses. */
328 void
329 regstat_compute_ri (void)
331 basic_block bb;
332 bitmap live = BITMAP_ALLOC (&df_bitmap_obstack);
333 bitmap do_not_gen = BITMAP_ALLOC (&df_bitmap_obstack);
334 bitmap artificial_uses = BITMAP_ALLOC (&df_bitmap_obstack);
335 bitmap local_live = BITMAP_ALLOC (&df_bitmap_obstack);
336 bitmap local_processed = BITMAP_ALLOC (&df_bitmap_obstack);
337 unsigned int regno;
338 bitmap_iterator bi;
340 /* Initialize everything. */
342 gcc_assert (!reg_info_p);
344 timevar_push (TV_REG_STATS);
345 setjmp_crosses = BITMAP_ALLOC (&df_bitmap_obstack);
346 max_regno = max_reg_num ();
347 reg_info_p_size = max_regno;
348 reg_info_p = xcalloc (max_regno, sizeof (struct reg_info_t));
350 FOR_EACH_BB (bb)
352 regstat_bb_compute_ri (bb->index, live, do_not_gen, artificial_uses,
353 local_live, local_processed);
356 BITMAP_FREE (live);
357 BITMAP_FREE (do_not_gen);
358 BITMAP_FREE (artificial_uses);
360 /* See the setjmp comment in regstat_ri_bb_compute. */
361 EXECUTE_IF_SET_IN_BITMAP (setjmp_crosses, FIRST_PSEUDO_REGISTER, regno, bi)
363 REG_BASIC_BLOCK (regno) = REG_BLOCK_UNKNOWN;
364 REG_LIVE_LENGTH (regno) = -1;
367 BITMAP_FREE (local_live);
368 BITMAP_FREE (local_processed);
369 timevar_pop (TV_REG_STATS);
373 /* Free all storage associated with the problem. */
375 void
376 regstat_free_ri (void)
378 gcc_assert (reg_info_p);
379 reg_info_p_size = 0;
380 free (reg_info_p);
381 reg_info_p = NULL;
383 BITMAP_FREE (setjmp_crosses);
387 /* Return a bitmap containing the set of registers that cross a setjmp.
388 The client should not change or delete this bitmap. */
390 bitmap
391 regstat_get_setjmp_crosses (void)
393 return setjmp_crosses;
396 /*----------------------------------------------------------------------------
397 Process REG_N_CALLS_CROSSED.
399 This is used by sched_deps. A good implementation of sched-deps
400 would really process the blocks directly rather than going thur
401 lists of insns. If it did this, it could use the exact regs that
402 cross an individual call rather than using this info that merges
403 the info for all calls.
405 ----------------------------------------------------------------------------*/
409 /* Compute calls crossed for BB. Live is a scratch bitvector. */
411 static void
412 regstat_bb_compute_calls_crossed (unsigned int bb_index, bitmap live)
414 basic_block bb = BASIC_BLOCK (bb_index);
415 rtx insn;
416 struct df_ref **def_rec;
417 struct df_ref **use_rec;
419 bitmap_copy (live, df_get_live_out (bb));
421 /* Process the artificial defs and uses at the bottom of the block
422 to begin processing. */
423 for (def_rec = df_get_artificial_defs (bb_index); *def_rec; def_rec++)
425 struct df_ref *def = *def_rec;
426 if ((DF_REF_FLAGS (def) & DF_REF_AT_TOP) == 0)
427 bitmap_clear_bit (live, DF_REF_REGNO (def));
430 for (use_rec = df_get_artificial_uses (bb_index); *use_rec; use_rec++)
432 struct df_ref *use = *use_rec;
433 if ((DF_REF_FLAGS (use) & DF_REF_AT_TOP) == 0)
434 bitmap_set_bit (live, DF_REF_REGNO (use));
437 FOR_BB_INSNS_REVERSE (bb, insn)
439 unsigned int uid = INSN_UID (insn);
440 unsigned int regno;
442 if (!INSN_P (insn))
443 continue;
445 /* Process the defs. */
446 if (CALL_P (insn))
448 bitmap_iterator bi;
449 EXECUTE_IF_SET_IN_BITMAP (live, 0, regno, bi)
450 REG_N_CALLS_CROSSED (regno)++;
453 /* All of the defs except the return value are some sort of
454 clobber. This code is for the return. */
455 for (def_rec = DF_INSN_UID_DEFS (uid); *def_rec; def_rec++)
457 struct df_ref *def = *def_rec;
458 if ((!CALL_P (insn))
459 || (!(DF_REF_FLAGS (def) & (DF_REF_MUST_CLOBBER | DF_REF_MAY_CLOBBER))))
461 /* Kill this register if it is not a subreg store or conditional store. */
462 if (!(DF_REF_FLAGS (def) & (DF_REF_PARTIAL | DF_REF_CONDITIONAL)))
463 bitmap_clear_bit (live, DF_REF_REGNO (def));
467 for (use_rec = DF_INSN_UID_USES (uid); *use_rec; use_rec++)
469 struct df_ref *use = *use_rec;
470 bitmap_set_bit (live, DF_REF_REGNO (use));
476 /* Compute register info: lifetime, bb, and number of defs and uses. */
477 void
478 regstat_compute_calls_crossed (void)
480 basic_block bb;
481 bitmap live = BITMAP_ALLOC (&df_bitmap_obstack);
483 /* Initialize everything. */
484 gcc_assert (!reg_info_p);
486 timevar_push (TV_REG_STATS);
487 max_regno = max_reg_num ();
488 reg_info_p_size = max_regno;
489 reg_info_p = xcalloc (max_regno, sizeof (struct reg_info_t));
491 FOR_EACH_BB (bb)
493 regstat_bb_compute_calls_crossed (bb->index, live);
496 BITMAP_FREE (live);
497 timevar_pop (TV_REG_STATS);
501 /* Free all storage associated with the problem. */
503 void
504 regstat_free_calls_crossed (void)
506 gcc_assert (reg_info_p);
507 reg_info_p_size = 0;
508 free (reg_info_p);
509 reg_info_p = NULL;