]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/reginfo.c
4fa0d04ce9c80707d3fb51471cc3600f4fcb335f
[thirdparty/gcc.git] / gcc / reginfo.c
1 /* Compute different info about registers.
2 Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1996
3 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
4 2009, 2010, 2011 Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
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 3, or (at your option) any later
11 version.
12
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.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22
23 /* This file contains regscan pass of the compiler and passes for
24 dealing with info about modes of pseudo-registers inside
25 subregisters. It also defines some tables of information about the
26 hardware registers, function init_reg_sets to initialize the
27 tables, and other auxiliary functions to deal with info about
28 registers and their classes. */
29
30 #include "config.h"
31 #include "system.h"
32 #include "coretypes.h"
33 #include "tm.h"
34 #include "hard-reg-set.h"
35 #include "rtl.h"
36 #include "expr.h"
37 #include "tm_p.h"
38 #include "flags.h"
39 #include "basic-block.h"
40 #include "regs.h"
41 #include "addresses.h"
42 #include "function.h"
43 #include "insn-config.h"
44 #include "recog.h"
45 #include "reload.h"
46 #include "diagnostic-core.h"
47 #include "output.h"
48 #include "timevar.h"
49 #include "hashtab.h"
50 #include "target.h"
51 #include "tree-pass.h"
52 #include "df.h"
53 #include "ira.h"
54
55 /* Maximum register number used in this function, plus one. */
56
57 int max_regno;
58
59 \f
60 struct target_hard_regs default_target_hard_regs;
61 struct target_regs default_target_regs;
62 #if SWITCHABLE_TARGET
63 struct target_hard_regs *this_target_hard_regs = &default_target_hard_regs;
64 struct target_regs *this_target_regs = &default_target_regs;
65 #endif
66
67 /* Data for initializing fixed_regs. */
68 static const char initial_fixed_regs[] = FIXED_REGISTERS;
69
70 /* Data for initializing call_used_regs. */
71 static const char initial_call_used_regs[] = CALL_USED_REGISTERS;
72
73 #ifdef CALL_REALLY_USED_REGISTERS
74 /* Data for initializing call_really_used_regs. */
75 static const char initial_call_really_used_regs[] = CALL_REALLY_USED_REGISTERS;
76 #endif
77
78 #ifdef CALL_REALLY_USED_REGISTERS
79 #define CALL_REALLY_USED_REGNO_P(X) call_really_used_regs[X]
80 #else
81 #define CALL_REALLY_USED_REGNO_P(X) call_used_regs[X]
82 #endif
83
84 /* Indexed by hard register number, contains 1 for registers
85 that are being used for global register decls.
86 These must be exempt from ordinary flow analysis
87 and are also considered fixed. */
88 char global_regs[FIRST_PSEUDO_REGISTER];
89
90 /* Declaration for the global register. */
91 static tree GTY(()) global_regs_decl[FIRST_PSEUDO_REGISTER];
92
93 /* Same information as REGS_INVALIDATED_BY_CALL but in regset form to be used
94 in dataflow more conveniently. */
95 regset regs_invalidated_by_call_regset;
96
97 /* Same information as FIXED_REG_SET but in regset form. */
98 regset fixed_reg_set_regset;
99
100 /* The bitmap_obstack is used to hold some static variables that
101 should not be reset after each function is compiled. */
102 static bitmap_obstack persistent_obstack;
103
104 /* Used to initialize reg_alloc_order. */
105 #ifdef REG_ALLOC_ORDER
106 static int initial_reg_alloc_order[FIRST_PSEUDO_REGISTER] = REG_ALLOC_ORDER;
107 #endif
108
109 /* The same information, but as an array of unsigned ints. We copy from
110 these unsigned ints to the table above. We do this so the tm.h files
111 do not have to be aware of the wordsize for machines with <= 64 regs.
112 Note that we hard-code 32 here, not HOST_BITS_PER_INT. */
113 #define N_REG_INTS \
114 ((FIRST_PSEUDO_REGISTER + (32 - 1)) / 32)
115
116 static const unsigned int_reg_class_contents[N_REG_CLASSES][N_REG_INTS]
117 = REG_CLASS_CONTENTS;
118
119 /* Array containing all of the register names. */
120 static const char *const initial_reg_names[] = REGISTER_NAMES;
121
122 /* Array containing all of the register class names. */
123 const char * reg_class_names[] = REG_CLASS_NAMES;
124
125 /* No more global register variables may be declared; true once
126 reginfo has been initialized. */
127 static int no_global_reg_vars = 0;
128
129 /* Given a register bitmap, turn on the bits in a HARD_REG_SET that
130 correspond to the hard registers, if any, set in that map. This
131 could be done far more efficiently by having all sorts of special-cases
132 with moving single words, but probably isn't worth the trouble. */
133 void
134 reg_set_to_hard_reg_set (HARD_REG_SET *to, const_bitmap from)
135 {
136 unsigned i;
137 bitmap_iterator bi;
138
139 EXECUTE_IF_SET_IN_BITMAP (from, 0, i, bi)
140 {
141 if (i >= FIRST_PSEUDO_REGISTER)
142 return;
143 SET_HARD_REG_BIT (*to, i);
144 }
145 }
146
147 /* Function called only once per target_globals to initialize the
148 target_hard_regs structure. Once this is done, various switches
149 may override. */
150 void
151 init_reg_sets (void)
152 {
153 int i, j;
154
155 /* First copy the register information from the initial int form into
156 the regsets. */
157
158 for (i = 0; i < N_REG_CLASSES; i++)
159 {
160 CLEAR_HARD_REG_SET (reg_class_contents[i]);
161
162 /* Note that we hard-code 32 here, not HOST_BITS_PER_INT. */
163 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
164 if (int_reg_class_contents[i][j / 32]
165 & ((unsigned) 1 << (j % 32)))
166 SET_HARD_REG_BIT (reg_class_contents[i], j);
167 }
168
169 /* Sanity check: make sure the target macros FIXED_REGISTERS and
170 CALL_USED_REGISTERS had the right number of initializers. */
171 gcc_assert (sizeof fixed_regs == sizeof initial_fixed_regs);
172 gcc_assert (sizeof call_used_regs == sizeof initial_call_used_regs);
173 #ifdef CALL_REALLY_USED_REGISTERS
174 gcc_assert (sizeof call_really_used_regs
175 == sizeof initial_call_really_used_regs);
176 #endif
177 #ifdef REG_ALLOC_ORDER
178 gcc_assert (sizeof reg_alloc_order == sizeof initial_reg_alloc_order);
179 #endif
180 gcc_assert (sizeof reg_names == sizeof initial_reg_names);
181
182 memcpy (fixed_regs, initial_fixed_regs, sizeof fixed_regs);
183 memcpy (call_used_regs, initial_call_used_regs, sizeof call_used_regs);
184 #ifdef CALL_REALLY_USED_REGISTERS
185 memcpy (call_really_used_regs, initial_call_really_used_regs,
186 sizeof call_really_used_regs);
187 #endif
188 #ifdef REG_ALLOC_ORDER
189 memcpy (reg_alloc_order, initial_reg_alloc_order, sizeof reg_alloc_order);
190 #endif
191 memcpy (reg_names, initial_reg_names, sizeof reg_names);
192
193 SET_HARD_REG_SET (accessible_reg_set);
194 SET_HARD_REG_SET (operand_reg_set);
195 }
196
197 /* We need to save copies of some of the register information which
198 can be munged by command-line switches so we can restore it during
199 subsequent back-end reinitialization. */
200 static char saved_fixed_regs[FIRST_PSEUDO_REGISTER];
201 static char saved_call_used_regs[FIRST_PSEUDO_REGISTER];
202 #ifdef CALL_REALLY_USED_REGISTERS
203 static char saved_call_really_used_regs[FIRST_PSEUDO_REGISTER];
204 #endif
205 static const char *saved_reg_names[FIRST_PSEUDO_REGISTER];
206 static HARD_REG_SET saved_accessible_reg_set;
207 static HARD_REG_SET saved_operand_reg_set;
208
209 /* Save the register information. */
210 void
211 save_register_info (void)
212 {
213 /* Sanity check: make sure the target macros FIXED_REGISTERS and
214 CALL_USED_REGISTERS had the right number of initializers. */
215 gcc_assert (sizeof fixed_regs == sizeof saved_fixed_regs);
216 gcc_assert (sizeof call_used_regs == sizeof saved_call_used_regs);
217 memcpy (saved_fixed_regs, fixed_regs, sizeof fixed_regs);
218 memcpy (saved_call_used_regs, call_used_regs, sizeof call_used_regs);
219
220 /* Likewise for call_really_used_regs. */
221 #ifdef CALL_REALLY_USED_REGISTERS
222 gcc_assert (sizeof call_really_used_regs
223 == sizeof saved_call_really_used_regs);
224 memcpy (saved_call_really_used_regs, call_really_used_regs,
225 sizeof call_really_used_regs);
226 #endif
227
228 /* And similarly for reg_names. */
229 gcc_assert (sizeof reg_names == sizeof saved_reg_names);
230 memcpy (saved_reg_names, reg_names, sizeof reg_names);
231 COPY_HARD_REG_SET (saved_accessible_reg_set, accessible_reg_set);
232 COPY_HARD_REG_SET (saved_operand_reg_set, operand_reg_set);
233 }
234
235 /* Restore the register information. */
236 static void
237 restore_register_info (void)
238 {
239 memcpy (fixed_regs, saved_fixed_regs, sizeof fixed_regs);
240 memcpy (call_used_regs, saved_call_used_regs, sizeof call_used_regs);
241
242 #ifdef CALL_REALLY_USED_REGISTERS
243 memcpy (call_really_used_regs, saved_call_really_used_regs,
244 sizeof call_really_used_regs);
245 #endif
246
247 memcpy (reg_names, saved_reg_names, sizeof reg_names);
248 COPY_HARD_REG_SET (accessible_reg_set, saved_accessible_reg_set);
249 COPY_HARD_REG_SET (operand_reg_set, saved_operand_reg_set);
250 }
251
252 /* After switches have been processed, which perhaps alter
253 `fixed_regs' and `call_used_regs', convert them to HARD_REG_SETs. */
254 static void
255 init_reg_sets_1 (void)
256 {
257 unsigned int i, j;
258 unsigned int /* enum machine_mode */ m;
259
260 restore_register_info ();
261
262 #ifdef REG_ALLOC_ORDER
263 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
264 inv_reg_alloc_order[reg_alloc_order[i]] = i;
265 #endif
266
267 /* Let the target tweak things if necessary. */
268
269 targetm.conditional_register_usage ();
270
271 /* Compute number of hard regs in each class. */
272
273 memset (reg_class_size, 0, sizeof reg_class_size);
274 for (i = 0; i < N_REG_CLASSES; i++)
275 {
276 bool any_nonfixed = false;
277 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
278 if (TEST_HARD_REG_BIT (reg_class_contents[i], j))
279 {
280 reg_class_size[i]++;
281 if (!fixed_regs[j])
282 any_nonfixed = true;
283 }
284 class_only_fixed_regs[i] = !any_nonfixed;
285 }
286
287 /* Initialize the table of subunions.
288 reg_class_subunion[I][J] gets the largest-numbered reg-class
289 that is contained in the union of classes I and J. */
290
291 memset (reg_class_subunion, 0, sizeof reg_class_subunion);
292 for (i = 0; i < N_REG_CLASSES; i++)
293 {
294 for (j = 0; j < N_REG_CLASSES; j++)
295 {
296 HARD_REG_SET c;
297 int k;
298
299 COPY_HARD_REG_SET (c, reg_class_contents[i]);
300 IOR_HARD_REG_SET (c, reg_class_contents[j]);
301 for (k = 0; k < N_REG_CLASSES; k++)
302 if (hard_reg_set_subset_p (reg_class_contents[k], c)
303 && !hard_reg_set_subset_p (reg_class_contents[k],
304 reg_class_contents
305 [(int) reg_class_subunion[i][j]]))
306 reg_class_subunion[i][j] = (enum reg_class) k;
307 }
308 }
309
310 /* Initialize the table of superunions.
311 reg_class_superunion[I][J] gets the smallest-numbered reg-class
312 containing the union of classes I and J. */
313
314 memset (reg_class_superunion, 0, sizeof reg_class_superunion);
315 for (i = 0; i < N_REG_CLASSES; i++)
316 {
317 for (j = 0; j < N_REG_CLASSES; j++)
318 {
319 HARD_REG_SET c;
320 int k;
321
322 COPY_HARD_REG_SET (c, reg_class_contents[i]);
323 IOR_HARD_REG_SET (c, reg_class_contents[j]);
324 for (k = 0; k < N_REG_CLASSES; k++)
325 if (hard_reg_set_subset_p (c, reg_class_contents[k]))
326 break;
327
328 reg_class_superunion[i][j] = (enum reg_class) k;
329 }
330 }
331
332 /* Initialize the tables of subclasses and superclasses of each reg class.
333 First clear the whole table, then add the elements as they are found. */
334
335 for (i = 0; i < N_REG_CLASSES; i++)
336 {
337 for (j = 0; j < N_REG_CLASSES; j++)
338 reg_class_subclasses[i][j] = LIM_REG_CLASSES;
339 }
340
341 for (i = 0; i < N_REG_CLASSES; i++)
342 {
343 if (i == (int) NO_REGS)
344 continue;
345
346 for (j = i + 1; j < N_REG_CLASSES; j++)
347 if (hard_reg_set_subset_p (reg_class_contents[i],
348 reg_class_contents[j]))
349 {
350 /* Reg class I is a subclass of J.
351 Add J to the table of superclasses of I. */
352 enum reg_class *p;
353
354 /* Add I to the table of superclasses of J. */
355 p = &reg_class_subclasses[j][0];
356 while (*p != LIM_REG_CLASSES) p++;
357 *p = (enum reg_class) i;
358 }
359 }
360
361 /* Initialize "constant" tables. */
362
363 CLEAR_HARD_REG_SET (fixed_reg_set);
364 CLEAR_HARD_REG_SET (call_used_reg_set);
365 CLEAR_HARD_REG_SET (call_fixed_reg_set);
366 CLEAR_HARD_REG_SET (regs_invalidated_by_call);
367 if (!regs_invalidated_by_call_regset)
368 {
369 bitmap_obstack_initialize (&persistent_obstack);
370 regs_invalidated_by_call_regset = ALLOC_REG_SET (&persistent_obstack);
371 }
372 else
373 CLEAR_REG_SET (regs_invalidated_by_call_regset);
374 if (!fixed_reg_set_regset)
375 fixed_reg_set_regset = ALLOC_REG_SET (&persistent_obstack);
376 else
377 CLEAR_REG_SET (fixed_reg_set_regset);
378
379 AND_HARD_REG_SET (operand_reg_set, accessible_reg_set);
380 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
381 {
382 /* As a special exception, registers whose class is NO_REGS are
383 not accepted by `register_operand'. The reason for this change
384 is to allow the representation of special architecture artifacts
385 (such as a condition code register) without extending the rtl
386 definitions. Since registers of class NO_REGS cannot be used
387 as registers in any case where register classes are examined,
388 it is better to apply this exception in a target-independent way. */
389 if (REGNO_REG_CLASS (i) == NO_REGS)
390 CLEAR_HARD_REG_BIT (operand_reg_set, i);
391
392 /* If a register is too limited to be treated as a register operand,
393 then it should never be allocated to a pseudo. */
394 if (!TEST_HARD_REG_BIT (operand_reg_set, i))
395 {
396 fixed_regs[i] = 1;
397 call_used_regs[i] = 1;
398 }
399
400 /* call_used_regs must include fixed_regs. */
401 gcc_assert (!fixed_regs[i] || call_used_regs[i]);
402 #ifdef CALL_REALLY_USED_REGISTERS
403 /* call_used_regs must include call_really_used_regs. */
404 gcc_assert (!call_really_used_regs[i] || call_used_regs[i]);
405 #endif
406
407 if (fixed_regs[i])
408 {
409 SET_HARD_REG_BIT (fixed_reg_set, i);
410 SET_REGNO_REG_SET (fixed_reg_set_regset, i);
411 }
412
413 if (call_used_regs[i])
414 SET_HARD_REG_BIT (call_used_reg_set, i);
415
416 /* There are a couple of fixed registers that we know are safe to
417 exclude from being clobbered by calls:
418
419 The frame pointer is always preserved across calls. The arg
420 pointer is if it is fixed. The stack pointer usually is,
421 unless TARGET_RETURN_POPS_ARGS, in which case an explicit
422 CLOBBER will be present. If we are generating PIC code, the
423 PIC offset table register is preserved across calls, though the
424 target can override that. */
425
426 if (i == STACK_POINTER_REGNUM)
427 ;
428 else if (global_regs[i])
429 {
430 SET_HARD_REG_BIT (regs_invalidated_by_call, i);
431 SET_REGNO_REG_SET (regs_invalidated_by_call_regset, i);
432 }
433 else if (i == FRAME_POINTER_REGNUM)
434 ;
435 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
436 else if (i == HARD_FRAME_POINTER_REGNUM)
437 ;
438 #endif
439 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
440 else if (i == ARG_POINTER_REGNUM && fixed_regs[i])
441 ;
442 #endif
443 else if (!PIC_OFFSET_TABLE_REG_CALL_CLOBBERED
444 && i == (unsigned) PIC_OFFSET_TABLE_REGNUM && fixed_regs[i])
445 ;
446 else if (CALL_REALLY_USED_REGNO_P (i))
447 {
448 SET_HARD_REG_BIT (regs_invalidated_by_call, i);
449 SET_REGNO_REG_SET (regs_invalidated_by_call_regset, i);
450 }
451 }
452
453 COPY_HARD_REG_SET(call_fixed_reg_set, fixed_reg_set);
454
455 /* Preserve global registers if called more than once. */
456 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
457 {
458 if (global_regs[i])
459 {
460 fixed_regs[i] = call_used_regs[i] = 1;
461 SET_HARD_REG_BIT (fixed_reg_set, i);
462 SET_HARD_REG_BIT (call_used_reg_set, i);
463 SET_HARD_REG_BIT (call_fixed_reg_set, i);
464 }
465 }
466
467 memset (have_regs_of_mode, 0, sizeof (have_regs_of_mode));
468 memset (contains_reg_of_mode, 0, sizeof (contains_reg_of_mode));
469 for (m = 0; m < (unsigned int) MAX_MACHINE_MODE; m++)
470 {
471 HARD_REG_SET ok_regs;
472 CLEAR_HARD_REG_SET (ok_regs);
473 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
474 if (!fixed_regs [j] && HARD_REGNO_MODE_OK (j, (enum machine_mode) m))
475 SET_HARD_REG_BIT (ok_regs, j);
476
477 for (i = 0; i < N_REG_CLASSES; i++)
478 if ((targetm.class_max_nregs ((reg_class_t) i, (enum machine_mode) m)
479 <= reg_class_size[i])
480 && hard_reg_set_intersect_p (ok_regs, reg_class_contents[i]))
481 {
482 contains_reg_of_mode [i][m] = 1;
483 have_regs_of_mode [m] = 1;
484 }
485 }
486 }
487
488 /* Compute the table of register modes.
489 These values are used to record death information for individual registers
490 (as opposed to a multi-register mode).
491 This function might be invoked more than once, if the target has support
492 for changing register usage conventions on a per-function basis.
493 */
494 void
495 init_reg_modes_target (void)
496 {
497 int i, j;
498
499 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
500 for (j = 0; j < MAX_MACHINE_MODE; j++)
501 hard_regno_nregs[i][j] = HARD_REGNO_NREGS(i, (enum machine_mode)j);
502
503 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
504 {
505 reg_raw_mode[i] = choose_hard_reg_mode (i, 1, false);
506
507 /* If we couldn't find a valid mode, just use the previous mode
508 if it is suitable, otherwise fall back on word_mode. */
509 if (reg_raw_mode[i] == VOIDmode)
510 {
511 if (i > 0 && hard_regno_nregs[i][reg_raw_mode[i - 1]] == 1)
512 reg_raw_mode[i] = reg_raw_mode[i - 1];
513 else
514 reg_raw_mode[i] = word_mode;
515 }
516 }
517 }
518
519 /* Finish initializing the register sets and initialize the register modes.
520 This function might be invoked more than once, if the target has support
521 for changing register usage conventions on a per-function basis.
522 */
523 void
524 init_regs (void)
525 {
526 /* This finishes what was started by init_reg_sets, but couldn't be done
527 until after register usage was specified. */
528 init_reg_sets_1 ();
529 }
530
531 /* The same as previous function plus initializing IRA. */
532 void
533 reinit_regs (void)
534 {
535 init_regs ();
536 /* caller_save needs to be re-initialized. */
537 caller_save_initialized_p = false;
538 ira_init ();
539 }
540
541 /* Initialize some fake stack-frame MEM references for use in
542 memory_move_secondary_cost. */
543 void
544 init_fake_stack_mems (void)
545 {
546 int i;
547
548 for (i = 0; i < MAX_MACHINE_MODE; i++)
549 top_of_stack[i] = gen_rtx_MEM ((enum machine_mode) i, stack_pointer_rtx);
550 }
551
552
553 /* Compute cost of moving data from a register of class FROM to one of
554 TO, using MODE. */
555
556 int
557 register_move_cost (enum machine_mode mode, reg_class_t from, reg_class_t to)
558 {
559 return targetm.register_move_cost (mode, from, to);
560 }
561
562 /* Compute cost of moving registers to/from memory. */
563
564 int
565 memory_move_cost (enum machine_mode mode, reg_class_t rclass, bool in)
566 {
567 return targetm.memory_move_cost (mode, rclass, in);
568 }
569
570 /* Compute extra cost of moving registers to/from memory due to reloads.
571 Only needed if secondary reloads are required for memory moves. */
572 int
573 memory_move_secondary_cost (enum machine_mode mode, reg_class_t rclass,
574 bool in)
575 {
576 reg_class_t altclass;
577 int partial_cost = 0;
578 /* We need a memory reference to feed to SECONDARY... macros. */
579 /* mem may be unused even if the SECONDARY_ macros are defined. */
580 rtx mem ATTRIBUTE_UNUSED = top_of_stack[(int) mode];
581
582 altclass = secondary_reload_class (in ? 1 : 0, rclass, mode, mem);
583
584 if (altclass == NO_REGS)
585 return 0;
586
587 if (in)
588 partial_cost = register_move_cost (mode, altclass, rclass);
589 else
590 partial_cost = register_move_cost (mode, rclass, altclass);
591
592 if (rclass == altclass)
593 /* This isn't simply a copy-to-temporary situation. Can't guess
594 what it is, so TARGET_MEMORY_MOVE_COST really ought not to be
595 calling here in that case.
596
597 I'm tempted to put in an assert here, but returning this will
598 probably only give poor estimates, which is what we would've
599 had before this code anyways. */
600 return partial_cost;
601
602 /* Check if the secondary reload register will also need a
603 secondary reload. */
604 return memory_move_secondary_cost (mode, altclass, in) + partial_cost;
605 }
606
607 /* Return a machine mode that is legitimate for hard reg REGNO and large
608 enough to save nregs. If we can't find one, return VOIDmode.
609 If CALL_SAVED is true, only consider modes that are call saved. */
610 enum machine_mode
611 choose_hard_reg_mode (unsigned int regno ATTRIBUTE_UNUSED,
612 unsigned int nregs, bool call_saved)
613 {
614 unsigned int /* enum machine_mode */ m;
615 enum machine_mode found_mode = VOIDmode, mode;
616
617 /* We first look for the largest integer mode that can be validly
618 held in REGNO. If none, we look for the largest floating-point mode.
619 If we still didn't find a valid mode, try CCmode. */
620
621 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
622 mode != VOIDmode;
623 mode = GET_MODE_WIDER_MODE (mode))
624 if ((unsigned) hard_regno_nregs[regno][mode] == nregs
625 && HARD_REGNO_MODE_OK (regno, mode)
626 && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode)))
627 found_mode = mode;
628
629 if (found_mode != VOIDmode)
630 return found_mode;
631
632 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT);
633 mode != VOIDmode;
634 mode = GET_MODE_WIDER_MODE (mode))
635 if ((unsigned) hard_regno_nregs[regno][mode] == nregs
636 && HARD_REGNO_MODE_OK (regno, mode)
637 && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode)))
638 found_mode = mode;
639
640 if (found_mode != VOIDmode)
641 return found_mode;
642
643 for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_FLOAT);
644 mode != VOIDmode;
645 mode = GET_MODE_WIDER_MODE (mode))
646 if ((unsigned) hard_regno_nregs[regno][mode] == nregs
647 && HARD_REGNO_MODE_OK (regno, mode)
648 && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode)))
649 found_mode = mode;
650
651 if (found_mode != VOIDmode)
652 return found_mode;
653
654 for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_INT);
655 mode != VOIDmode;
656 mode = GET_MODE_WIDER_MODE (mode))
657 if ((unsigned) hard_regno_nregs[regno][mode] == nregs
658 && HARD_REGNO_MODE_OK (regno, mode)
659 && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode)))
660 found_mode = mode;
661
662 if (found_mode != VOIDmode)
663 return found_mode;
664
665 /* Iterate over all of the CCmodes. */
666 for (m = (unsigned int) CCmode; m < (unsigned int) NUM_MACHINE_MODES; ++m)
667 {
668 mode = (enum machine_mode) m;
669 if ((unsigned) hard_regno_nregs[regno][mode] == nregs
670 && HARD_REGNO_MODE_OK (regno, mode)
671 && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode)))
672 return mode;
673 }
674
675 /* We can't find a mode valid for this register. */
676 return VOIDmode;
677 }
678
679 /* Specify the usage characteristics of the register named NAME.
680 It should be a fixed register if FIXED and a
681 call-used register if CALL_USED. */
682 void
683 fix_register (const char *name, int fixed, int call_used)
684 {
685 int i;
686 int reg, nregs;
687
688 /* Decode the name and update the primary form of
689 the register info. */
690
691 if ((reg = decode_reg_name_and_count (name, &nregs)) >= 0)
692 {
693 gcc_assert (nregs >= 1);
694 for (i = reg; i < reg + nregs; i++)
695 {
696 if ((i == STACK_POINTER_REGNUM
697 #ifdef HARD_FRAME_POINTER_REGNUM
698 || i == HARD_FRAME_POINTER_REGNUM
699 #else
700 || i == FRAME_POINTER_REGNUM
701 #endif
702 )
703 && (fixed == 0 || call_used == 0))
704 {
705 switch (fixed)
706 {
707 case 0:
708 switch (call_used)
709 {
710 case 0:
711 error ("can%'t use %qs as a call-saved register", name);
712 break;
713
714 case 1:
715 error ("can%'t use %qs as a call-used register", name);
716 break;
717
718 default:
719 gcc_unreachable ();
720 }
721 break;
722
723 case 1:
724 switch (call_used)
725 {
726 case 1:
727 error ("can%'t use %qs as a fixed register", name);
728 break;
729
730 case 0:
731 default:
732 gcc_unreachable ();
733 }
734 break;
735
736 default:
737 gcc_unreachable ();
738 }
739 }
740 else
741 {
742 fixed_regs[i] = fixed;
743 call_used_regs[i] = call_used;
744 #ifdef CALL_REALLY_USED_REGISTERS
745 if (fixed == 0)
746 call_really_used_regs[i] = call_used;
747 #endif
748 }
749 }
750 }
751 else
752 {
753 warning (0, "unknown register name: %s", name);
754 }
755 }
756
757 /* Mark register number I as global. */
758 void
759 globalize_reg (tree decl, int i)
760 {
761 location_t loc = DECL_SOURCE_LOCATION (decl);
762
763 #ifdef STACK_REGS
764 if (IN_RANGE (i, FIRST_STACK_REG, LAST_STACK_REG))
765 {
766 error ("stack register used for global register variable");
767 return;
768 }
769 #endif
770
771 if (fixed_regs[i] == 0 && no_global_reg_vars)
772 error_at (loc, "global register variable follows a function definition");
773
774 if (global_regs[i])
775 {
776 warning_at (loc, 0,
777 "register of %qD used for multiple global register variables",
778 decl);
779 inform (DECL_SOURCE_LOCATION (global_regs_decl[i]),
780 "conflicts with %qD", global_regs_decl[i]);
781 return;
782 }
783
784 if (call_used_regs[i] && ! fixed_regs[i])
785 warning_at (loc, 0, "call-clobbered register used for global register variable");
786
787 global_regs[i] = 1;
788 global_regs_decl[i] = decl;
789
790 /* If we're globalizing the frame pointer, we need to set the
791 appropriate regs_invalidated_by_call bit, even if it's already
792 set in fixed_regs. */
793 if (i != STACK_POINTER_REGNUM)
794 {
795 SET_HARD_REG_BIT (regs_invalidated_by_call, i);
796 SET_REGNO_REG_SET (regs_invalidated_by_call_regset, i);
797 }
798
799 /* If already fixed, nothing else to do. */
800 if (fixed_regs[i])
801 return;
802
803 fixed_regs[i] = call_used_regs[i] = 1;
804 #ifdef CALL_REALLY_USED_REGISTERS
805 call_really_used_regs[i] = 1;
806 #endif
807
808 SET_HARD_REG_BIT (fixed_reg_set, i);
809 SET_HARD_REG_BIT (call_used_reg_set, i);
810 SET_HARD_REG_BIT (call_fixed_reg_set, i);
811
812 reinit_regs ();
813 }
814 \f
815
816 /* Structure used to record preferences of given pseudo. */
817 struct reg_pref
818 {
819 /* (enum reg_class) prefclass is the preferred class. May be
820 NO_REGS if no class is better than memory. */
821 char prefclass;
822
823 /* altclass is a register class that we should use for allocating
824 pseudo if no register in the preferred class is available.
825 If no register in this class is available, memory is preferred.
826
827 It might appear to be more general to have a bitmask of classes here,
828 but since it is recommended that there be a class corresponding to the
829 union of most major pair of classes, that generality is not required. */
830 char altclass;
831
832 /* allocnoclass is a register class that IRA uses for allocating
833 the pseudo. */
834 char allocnoclass;
835 };
836
837 /* Record preferences of each pseudo. This is available after RA is
838 run. */
839 static struct reg_pref *reg_pref;
840
841 /* Current size of reg_info. */
842 static int reg_info_size;
843
844 /* Return the reg_class in which pseudo reg number REGNO is best allocated.
845 This function is sometimes called before the info has been computed.
846 When that happens, just return GENERAL_REGS, which is innocuous. */
847 enum reg_class
848 reg_preferred_class (int regno)
849 {
850 if (reg_pref == 0)
851 return GENERAL_REGS;
852
853 return (enum reg_class) reg_pref[regno].prefclass;
854 }
855
856 enum reg_class
857 reg_alternate_class (int regno)
858 {
859 if (reg_pref == 0)
860 return ALL_REGS;
861
862 return (enum reg_class) reg_pref[regno].altclass;
863 }
864
865 /* Return the reg_class which is used by IRA for its allocation. */
866 enum reg_class
867 reg_allocno_class (int regno)
868 {
869 if (reg_pref == 0)
870 return NO_REGS;
871
872 return (enum reg_class) reg_pref[regno].allocnoclass;
873 }
874
875 \f
876
877 /* Allocate space for reg info. */
878 static void
879 allocate_reg_info (void)
880 {
881 reg_info_size = max_reg_num ();
882 gcc_assert (! reg_pref && ! reg_renumber);
883 reg_renumber = XNEWVEC (short, reg_info_size);
884 reg_pref = XCNEWVEC (struct reg_pref, reg_info_size);
885 memset (reg_renumber, -1, reg_info_size * sizeof (short));
886 }
887
888
889 /* Resize reg info. The new elements will be uninitialized. Return
890 TRUE if new elements (for new pseudos) were added. */
891 bool
892 resize_reg_info (void)
893 {
894 int old;
895
896 if (reg_pref == NULL)
897 {
898 allocate_reg_info ();
899 return true;
900 }
901 if (reg_info_size == max_reg_num ())
902 return false;
903 old = reg_info_size;
904 reg_info_size = max_reg_num ();
905 gcc_assert (reg_pref && reg_renumber);
906 reg_renumber = XRESIZEVEC (short, reg_renumber, reg_info_size);
907 reg_pref = XRESIZEVEC (struct reg_pref, reg_pref, reg_info_size);
908 memset (reg_pref + old, -1,
909 (reg_info_size - old) * sizeof (struct reg_pref));
910 memset (reg_renumber + old, -1, (reg_info_size - old) * sizeof (short));
911 return true;
912 }
913
914
915 /* Free up the space allocated by allocate_reg_info. */
916 void
917 free_reg_info (void)
918 {
919 if (reg_pref)
920 {
921 free (reg_pref);
922 reg_pref = NULL;
923 }
924
925 if (reg_renumber)
926 {
927 free (reg_renumber);
928 reg_renumber = NULL;
929 }
930 }
931
932 /* Initialize some global data for this pass. */
933 static unsigned int
934 reginfo_init (void)
935 {
936 if (df)
937 df_compute_regs_ever_live (true);
938
939 /* This prevents dump_flow_info from losing if called
940 before reginfo is run. */
941 reg_pref = NULL;
942 /* No more global register variables may be declared. */
943 no_global_reg_vars = 1;
944 return 1;
945 }
946
947 struct rtl_opt_pass pass_reginfo_init =
948 {
949 {
950 RTL_PASS,
951 "reginfo", /* name */
952 NULL, /* gate */
953 reginfo_init, /* execute */
954 NULL, /* sub */
955 NULL, /* next */
956 0, /* static_pass_number */
957 TV_NONE, /* tv_id */
958 0, /* properties_required */
959 0, /* properties_provided */
960 0, /* properties_destroyed */
961 0, /* todo_flags_start */
962 0 /* todo_flags_finish */
963 }
964 };
965
966 \f
967
968 /* Set up preferred, alternate, and cover classes for REGNO as
969 PREFCLASS, ALTCLASS, and ALLOCNOCLASS. */
970 void
971 setup_reg_classes (int regno,
972 enum reg_class prefclass, enum reg_class altclass,
973 enum reg_class allocnoclass)
974 {
975 if (reg_pref == NULL)
976 return;
977 gcc_assert (reg_info_size == max_reg_num ());
978 reg_pref[regno].prefclass = prefclass;
979 reg_pref[regno].altclass = altclass;
980 reg_pref[regno].allocnoclass = allocnoclass;
981 }
982
983 \f
984 /* This is the `regscan' pass of the compiler, run just before cse and
985 again just before loop. It finds the first and last use of each
986 pseudo-register. */
987
988 static void reg_scan_mark_refs (rtx, rtx);
989
990 void
991 reg_scan (rtx f, unsigned int nregs ATTRIBUTE_UNUSED)
992 {
993 rtx insn;
994
995 timevar_push (TV_REG_SCAN);
996
997 for (insn = f; insn; insn = NEXT_INSN (insn))
998 if (INSN_P (insn))
999 {
1000 reg_scan_mark_refs (PATTERN (insn), insn);
1001 if (REG_NOTES (insn))
1002 reg_scan_mark_refs (REG_NOTES (insn), insn);
1003 }
1004
1005 timevar_pop (TV_REG_SCAN);
1006 }
1007
1008
1009 /* X is the expression to scan. INSN is the insn it appears in.
1010 NOTE_FLAG is nonzero if X is from INSN's notes rather than its body.
1011 We should only record information for REGs with numbers
1012 greater than or equal to MIN_REGNO. */
1013 static void
1014 reg_scan_mark_refs (rtx x, rtx insn)
1015 {
1016 enum rtx_code code;
1017 rtx dest;
1018 rtx note;
1019
1020 if (!x)
1021 return;
1022 code = GET_CODE (x);
1023 switch (code)
1024 {
1025 case CONST:
1026 case CONST_INT:
1027 case CONST_DOUBLE:
1028 case CONST_FIXED:
1029 case CONST_VECTOR:
1030 case CC0:
1031 case PC:
1032 case SYMBOL_REF:
1033 case LABEL_REF:
1034 case ADDR_VEC:
1035 case ADDR_DIFF_VEC:
1036 case REG:
1037 return;
1038
1039 case EXPR_LIST:
1040 if (XEXP (x, 0))
1041 reg_scan_mark_refs (XEXP (x, 0), insn);
1042 if (XEXP (x, 1))
1043 reg_scan_mark_refs (XEXP (x, 1), insn);
1044 break;
1045
1046 case INSN_LIST:
1047 if (XEXP (x, 1))
1048 reg_scan_mark_refs (XEXP (x, 1), insn);
1049 break;
1050
1051 case CLOBBER:
1052 if (MEM_P (XEXP (x, 0)))
1053 reg_scan_mark_refs (XEXP (XEXP (x, 0), 0), insn);
1054 break;
1055
1056 case SET:
1057 /* Count a set of the destination if it is a register. */
1058 for (dest = SET_DEST (x);
1059 GET_CODE (dest) == SUBREG || GET_CODE (dest) == STRICT_LOW_PART
1060 || GET_CODE (dest) == ZERO_EXTEND;
1061 dest = XEXP (dest, 0))
1062 ;
1063
1064 /* If this is setting a pseudo from another pseudo or the sum of a
1065 pseudo and a constant integer and the other pseudo is known to be
1066 a pointer, set the destination to be a pointer as well.
1067
1068 Likewise if it is setting the destination from an address or from a
1069 value equivalent to an address or to the sum of an address and
1070 something else.
1071
1072 But don't do any of this if the pseudo corresponds to a user
1073 variable since it should have already been set as a pointer based
1074 on the type. */
1075
1076 if (REG_P (SET_DEST (x))
1077 && REGNO (SET_DEST (x)) >= FIRST_PSEUDO_REGISTER
1078 /* If the destination pseudo is set more than once, then other
1079 sets might not be to a pointer value (consider access to a
1080 union in two threads of control in the presence of global
1081 optimizations). So only set REG_POINTER on the destination
1082 pseudo if this is the only set of that pseudo. */
1083 && DF_REG_DEF_COUNT (REGNO (SET_DEST (x))) == 1
1084 && ! REG_USERVAR_P (SET_DEST (x))
1085 && ! REG_POINTER (SET_DEST (x))
1086 && ((REG_P (SET_SRC (x))
1087 && REG_POINTER (SET_SRC (x)))
1088 || ((GET_CODE (SET_SRC (x)) == PLUS
1089 || GET_CODE (SET_SRC (x)) == LO_SUM)
1090 && CONST_INT_P (XEXP (SET_SRC (x), 1))
1091 && REG_P (XEXP (SET_SRC (x), 0))
1092 && REG_POINTER (XEXP (SET_SRC (x), 0)))
1093 || GET_CODE (SET_SRC (x)) == CONST
1094 || GET_CODE (SET_SRC (x)) == SYMBOL_REF
1095 || GET_CODE (SET_SRC (x)) == LABEL_REF
1096 || (GET_CODE (SET_SRC (x)) == HIGH
1097 && (GET_CODE (XEXP (SET_SRC (x), 0)) == CONST
1098 || GET_CODE (XEXP (SET_SRC (x), 0)) == SYMBOL_REF
1099 || GET_CODE (XEXP (SET_SRC (x), 0)) == LABEL_REF))
1100 || ((GET_CODE (SET_SRC (x)) == PLUS
1101 || GET_CODE (SET_SRC (x)) == LO_SUM)
1102 && (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST
1103 || GET_CODE (XEXP (SET_SRC (x), 1)) == SYMBOL_REF
1104 || GET_CODE (XEXP (SET_SRC (x), 1)) == LABEL_REF))
1105 || ((note = find_reg_note (insn, REG_EQUAL, 0)) != 0
1106 && (GET_CODE (XEXP (note, 0)) == CONST
1107 || GET_CODE (XEXP (note, 0)) == SYMBOL_REF
1108 || GET_CODE (XEXP (note, 0)) == LABEL_REF))))
1109 REG_POINTER (SET_DEST (x)) = 1;
1110
1111 /* If this is setting a register from a register or from a simple
1112 conversion of a register, propagate REG_EXPR. */
1113 if (REG_P (dest) && !REG_ATTRS (dest))
1114 set_reg_attrs_from_value (dest, SET_SRC (x));
1115
1116 /* ... fall through ... */
1117
1118 default:
1119 {
1120 const char *fmt = GET_RTX_FORMAT (code);
1121 int i;
1122 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1123 {
1124 if (fmt[i] == 'e')
1125 reg_scan_mark_refs (XEXP (x, i), insn);
1126 else if (fmt[i] == 'E' && XVEC (x, i) != 0)
1127 {
1128 int j;
1129 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1130 reg_scan_mark_refs (XVECEXP (x, i, j), insn);
1131 }
1132 }
1133 }
1134 }
1135 }
1136 \f
1137
1138 /* Return nonzero if C1 is a subset of C2, i.e., if every register in C1
1139 is also in C2. */
1140 int
1141 reg_class_subset_p (reg_class_t c1, reg_class_t c2)
1142 {
1143 return (c1 == c2
1144 || c2 == ALL_REGS
1145 || hard_reg_set_subset_p (reg_class_contents[(int) c1],
1146 reg_class_contents[(int) c2]));
1147 }
1148
1149 /* Return nonzero if there is a register that is in both C1 and C2. */
1150 int
1151 reg_classes_intersect_p (reg_class_t c1, reg_class_t c2)
1152 {
1153 return (c1 == c2
1154 || c1 == ALL_REGS
1155 || c2 == ALL_REGS
1156 || hard_reg_set_intersect_p (reg_class_contents[(int) c1],
1157 reg_class_contents[(int) c2]));
1158 }
1159
1160 \f
1161
1162 /* Passes for keeping and updating info about modes of registers
1163 inside subregisters. */
1164
1165 #ifdef CANNOT_CHANGE_MODE_CLASS
1166
1167 static bitmap invalid_mode_changes;
1168
1169 static void
1170 record_subregs_of_mode (rtx subreg, bitmap subregs_of_mode)
1171 {
1172 enum machine_mode mode;
1173 unsigned int regno;
1174
1175 if (!REG_P (SUBREG_REG (subreg)))
1176 return;
1177
1178 regno = REGNO (SUBREG_REG (subreg));
1179 mode = GET_MODE (subreg);
1180
1181 if (regno < FIRST_PSEUDO_REGISTER)
1182 return;
1183
1184 if (bitmap_set_bit (subregs_of_mode,
1185 regno * NUM_MACHINE_MODES + (unsigned int) mode))
1186 {
1187 unsigned int rclass;
1188 for (rclass = 0; rclass < N_REG_CLASSES; rclass++)
1189 if (!bitmap_bit_p (invalid_mode_changes,
1190 regno * N_REG_CLASSES + rclass)
1191 && CANNOT_CHANGE_MODE_CLASS (PSEUDO_REGNO_MODE (regno),
1192 mode, (enum reg_class) rclass))
1193 bitmap_set_bit (invalid_mode_changes,
1194 regno * N_REG_CLASSES + rclass);
1195 }
1196 }
1197
1198 /* Call record_subregs_of_mode for all the subregs in X. */
1199 static void
1200 find_subregs_of_mode (rtx x, bitmap subregs_of_mode)
1201 {
1202 enum rtx_code code = GET_CODE (x);
1203 const char * const fmt = GET_RTX_FORMAT (code);
1204 int i;
1205
1206 if (code == SUBREG)
1207 record_subregs_of_mode (x, subregs_of_mode);
1208
1209 /* Time for some deep diving. */
1210 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1211 {
1212 if (fmt[i] == 'e')
1213 find_subregs_of_mode (XEXP (x, i), subregs_of_mode);
1214 else if (fmt[i] == 'E')
1215 {
1216 int j;
1217 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1218 find_subregs_of_mode (XVECEXP (x, i, j), subregs_of_mode);
1219 }
1220 }
1221 }
1222
1223 void
1224 init_subregs_of_mode (void)
1225 {
1226 basic_block bb;
1227 rtx insn;
1228 bitmap_obstack srom_obstack;
1229 bitmap subregs_of_mode;
1230
1231 gcc_assert (invalid_mode_changes == NULL);
1232 invalid_mode_changes = BITMAP_ALLOC (NULL);
1233 bitmap_obstack_initialize (&srom_obstack);
1234 subregs_of_mode = BITMAP_ALLOC (&srom_obstack);
1235
1236 FOR_EACH_BB (bb)
1237 FOR_BB_INSNS (bb, insn)
1238 if (NONDEBUG_INSN_P (insn))
1239 find_subregs_of_mode (PATTERN (insn), subregs_of_mode);
1240
1241 BITMAP_FREE (subregs_of_mode);
1242 bitmap_obstack_release (&srom_obstack);
1243 }
1244
1245 /* Return 1 if REGNO has had an invalid mode change in CLASS from FROM
1246 mode. */
1247 bool
1248 invalid_mode_change_p (unsigned int regno,
1249 enum reg_class rclass)
1250 {
1251 return bitmap_bit_p (invalid_mode_changes,
1252 regno * N_REG_CLASSES + (unsigned) rclass);
1253 }
1254
1255 void
1256 finish_subregs_of_mode (void)
1257 {
1258 BITMAP_FREE (invalid_mode_changes);
1259 }
1260 #else
1261 void
1262 init_subregs_of_mode (void)
1263 {
1264 }
1265 void
1266 finish_subregs_of_mode (void)
1267 {
1268 }
1269
1270 #endif /* CANNOT_CHANGE_MODE_CLASS */