2013-09-18 Marc Glisse <marc.glisse@inria.fr>
[official-gcc.git] / gcc / lra.c
blobf5aab1739272ba72922dbf7a35ecbfc50c2c4c0c
1 /* LRA (local register allocator) driver and LRA utilities.
2 Copyright (C) 2010-2013 Free Software Foundation, Inc.
3 Contributed by Vladimir Makarov <vmakarov@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* The Local Register Allocator (LRA) is a replacement of former
23 reload pass. It is focused to simplify code solving the reload
24 pass tasks, to make the code maintenance easier, and to implement new
25 perspective optimizations.
27 The major LRA design solutions are:
28 o division small manageable, separated sub-tasks
29 o reflection of all transformations and decisions in RTL as more
30 as possible
31 o insn constraints as a primary source of the info (minimizing
32 number of target-depended macros/hooks)
34 In brief LRA works by iterative insn process with the final goal is
35 to satisfy all insn and address constraints:
36 o New reload insns (in brief reloads) and reload pseudos might be
37 generated;
38 o Some pseudos might be spilled to assign hard registers to
39 new reload pseudos;
40 o Changing spilled pseudos to stack memory or their equivalences;
41 o Allocation stack memory changes the address displacement and
42 new iteration is needed.
44 Here is block diagram of LRA passes:
46 ------------------------
47 --------------- | Undo inheritance for | ---------------
48 | Memory-memory | | spilled pseudos, | | New (and old) |
49 | move coalesce |<---| splits for pseudos got |<-- | pseudos |
50 --------------- | the same hard regs, | | assignment |
51 Start | | and optional reloads | ---------------
52 | | ------------------------ ^
53 V | ---------------- |
54 ----------- V | Update virtual | |
55 | Remove |----> ------------>| register | |
56 | scratches | ^ | displacements | |
57 ----------- | ---------------- |
58 | | |
59 | V New |
60 ---------------- No ------------ pseudos -------------------
61 | Spilled pseudo | change |Constraints:| or insns | Inheritance/split |
62 | to memory |<-------| RTL |--------->| transformations |
63 | substitution | | transfor- | | in EBB scope |
64 ---------------- | mations | -------------------
65 | ------------
67 -------------------------
68 | Hard regs substitution, |
69 | devirtalization, and |------> Finish
70 | restoring scratches got |
71 | memory |
72 -------------------------
74 To speed up the process:
75 o We process only insns affected by changes on previous
76 iterations;
77 o We don't use DFA-infrastructure because it results in much slower
78 compiler speed than a special IR described below does;
79 o We use a special insn representation for quick access to insn
80 info which is always *synchronized* with the current RTL;
81 o Insn IR is minimized by memory. It is divided on three parts:
82 o one specific for each insn in RTL (only operand locations);
83 o one common for all insns in RTL with the same insn code
84 (different operand attributes from machine descriptions);
85 o one oriented for maintenance of live info (list of pseudos).
86 o Pseudo data:
87 o all insns where the pseudo is referenced;
88 o live info (conflicting hard regs, live ranges, # of
89 references etc);
90 o data used for assigning (preferred hard regs, costs etc).
92 This file contains LRA driver, LRA utility functions and data, and
93 code for dealing with scratches. */
95 #include "config.h"
96 #include "system.h"
97 #include "coretypes.h"
98 #include "tm.h"
99 #include "hard-reg-set.h"
100 #include "rtl.h"
101 #include "tm_p.h"
102 #include "regs.h"
103 #include "insn-config.h"
104 #include "insn-codes.h"
105 #include "recog.h"
106 #include "output.h"
107 #include "addresses.h"
108 #include "flags.h"
109 #include "function.h"
110 #include "expr.h"
111 #include "basic-block.h"
112 #include "except.h"
113 #include "tree-pass.h"
114 #include "timevar.h"
115 #include "target.h"
116 #include "vec.h"
117 #include "ira.h"
118 #include "lra-int.h"
119 #include "df.h"
121 /* Hard registers currently not available for allocation. It can
122 changed after some hard registers become not eliminable. */
123 HARD_REG_SET lra_no_alloc_regs;
125 static int get_new_reg_value (void);
126 static void expand_reg_info (void);
127 static void invalidate_insn_recog_data (int);
128 static int get_insn_freq (rtx);
129 static void invalidate_insn_data_regno_info (lra_insn_recog_data_t, rtx, int);
131 /* Expand all regno related info needed for LRA. */
132 static void
133 expand_reg_data (void)
135 resize_reg_info ();
136 expand_reg_info ();
137 ira_expand_reg_equiv ();
140 /* Create and return a new reg of ORIGINAL mode. If ORIGINAL is NULL
141 or of VOIDmode, use MD_MODE for the new reg. Initialize its
142 register class to RCLASS. Print message about assigning class
143 RCLASS containing new register name TITLE unless it is NULL. Use
144 attributes of ORIGINAL if it is a register. The created register
145 will have unique held value. */
147 lra_create_new_reg_with_unique_value (enum machine_mode md_mode, rtx original,
148 enum reg_class rclass, const char *title)
150 enum machine_mode mode;
151 rtx new_reg;
153 if (original == NULL_RTX || (mode = GET_MODE (original)) == VOIDmode)
154 mode = md_mode;
155 lra_assert (mode != VOIDmode);
156 new_reg = gen_reg_rtx (mode);
157 if (original == NULL_RTX || ! REG_P (original))
159 if (lra_dump_file != NULL)
160 fprintf (lra_dump_file, " Creating newreg=%i", REGNO (new_reg));
162 else
164 if (ORIGINAL_REGNO (original) >= FIRST_PSEUDO_REGISTER)
165 ORIGINAL_REGNO (new_reg) = ORIGINAL_REGNO (original);
166 REG_USERVAR_P (new_reg) = REG_USERVAR_P (original);
167 REG_POINTER (new_reg) = REG_POINTER (original);
168 REG_ATTRS (new_reg) = REG_ATTRS (original);
169 if (lra_dump_file != NULL)
170 fprintf (lra_dump_file, " Creating newreg=%i from oldreg=%i",
171 REGNO (new_reg), REGNO (original));
173 if (lra_dump_file != NULL)
175 if (title != NULL)
176 fprintf (lra_dump_file, ", assigning class %s to%s%s r%d",
177 reg_class_names[rclass], *title == '\0' ? "" : " ",
178 title, REGNO (new_reg));
179 fprintf (lra_dump_file, "\n");
181 expand_reg_data ();
182 setup_reg_classes (REGNO (new_reg), rclass, NO_REGS, rclass);
183 return new_reg;
186 /* Analogous to the previous function but also inherits value of
187 ORIGINAL. */
189 lra_create_new_reg (enum machine_mode md_mode, rtx original,
190 enum reg_class rclass, const char *title)
192 rtx new_reg;
194 new_reg
195 = lra_create_new_reg_with_unique_value (md_mode, original, rclass, title);
196 if (original != NULL_RTX && REG_P (original))
197 lra_assign_reg_val (REGNO (original), REGNO (new_reg));
198 return new_reg;
201 /* Set up for REGNO unique hold value. */
202 void
203 lra_set_regno_unique_value (int regno)
205 lra_reg_info[regno].val = get_new_reg_value ();
208 /* Invalidate INSN related info used by LRA. */
209 void
210 lra_invalidate_insn_data (rtx insn)
212 lra_invalidate_insn_regno_info (insn);
213 invalidate_insn_recog_data (INSN_UID (insn));
216 /* Mark INSN deleted and invalidate the insn related info used by
217 LRA. */
218 void
219 lra_set_insn_deleted (rtx insn)
221 lra_invalidate_insn_data (insn);
222 SET_INSN_DELETED (insn);
225 /* Delete an unneeded INSN and any previous insns who sole purpose is
226 loading data that is dead in INSN. */
227 void
228 lra_delete_dead_insn (rtx insn)
230 rtx prev = prev_real_insn (insn);
231 rtx prev_dest;
233 /* If the previous insn sets a register that dies in our insn,
234 delete it too. */
235 if (prev && GET_CODE (PATTERN (prev)) == SET
236 && (prev_dest = SET_DEST (PATTERN (prev)), REG_P (prev_dest))
237 && reg_mentioned_p (prev_dest, PATTERN (insn))
238 && find_regno_note (insn, REG_DEAD, REGNO (prev_dest))
239 && ! side_effects_p (SET_SRC (PATTERN (prev))))
240 lra_delete_dead_insn (prev);
242 lra_set_insn_deleted (insn);
245 /* Emit insn x = y + z. Return NULL if we failed to do it.
246 Otherwise, return the insn. We don't use gen_add3_insn as it might
247 clobber CC. */
248 static rtx
249 emit_add3_insn (rtx x, rtx y, rtx z)
251 rtx insn, last;
253 last = get_last_insn ();
254 insn = emit_insn (gen_rtx_SET (VOIDmode, x,
255 gen_rtx_PLUS (GET_MODE (y), y, z)));
256 if (recog_memoized (insn) < 0)
258 delete_insns_since (last);
259 insn = NULL_RTX;
261 return insn;
264 /* Emit insn x = x + y. Return the insn. We use gen_add2_insn as the
265 last resort. */
266 static rtx
267 emit_add2_insn (rtx x, rtx y)
269 rtx insn;
271 insn = emit_add3_insn (x, x, y);
272 if (insn == NULL_RTX)
274 insn = gen_add2_insn (x, y);
275 if (insn != NULL_RTX)
276 emit_insn (insn);
278 return insn;
281 /* Target checks operands through operand predicates to recognize an
282 insn. We should have a special precaution to generate add insns
283 which are frequent results of elimination.
285 Emit insns for x = y + z. X can be used to store intermediate
286 values and should be not in Y and Z when we use X to store an
287 intermediate value. Y + Z should form [base] [+ index[ * scale]] [
288 + disp] where base and index are registers, disp and scale are
289 constants. Y should contain base if it is present, Z should
290 contain disp if any. index[*scale] can be part of Y or Z. */
291 void
292 lra_emit_add (rtx x, rtx y, rtx z)
294 int old;
295 rtx insn, last;
296 rtx a1, a2, base, index, disp, scale, index_scale;
297 bool ok_p;
299 insn = emit_add3_insn (x, y, z);
300 old = max_reg_num ();
301 if (insn != NULL_RTX)
303 else
305 disp = a2 = NULL_RTX;
306 if (GET_CODE (y) == PLUS)
308 a1 = XEXP (y, 0);
309 a2 = XEXP (y, 1);
310 disp = z;
312 else
314 a1 = y;
315 if (CONSTANT_P (z))
316 disp = z;
317 else
318 a2 = z;
320 index_scale = scale = NULL_RTX;
321 if (GET_CODE (a1) == MULT)
323 index_scale = a1;
324 index = XEXP (a1, 0);
325 scale = XEXP (a1, 1);
326 base = a2;
328 else if (a2 != NULL_RTX && GET_CODE (a2) == MULT)
330 index_scale = a2;
331 index = XEXP (a2, 0);
332 scale = XEXP (a2, 1);
333 base = a1;
335 else
337 base = a1;
338 index = a2;
340 if (! REG_P (base)
341 || (index != NULL_RTX && ! REG_P (index))
342 || (disp != NULL_RTX && ! CONSTANT_P (disp))
343 || (scale != NULL_RTX && ! CONSTANT_P (scale)))
345 /* Probably we have no 3 op add. Last chance is to use 2-op
346 add insn. To succeed, don't move Z to X as an address
347 segment always comes in Y. Otherwise, we might fail when
348 adding the address segment to register. */
349 lra_assert (x != y && x != z);
350 emit_move_insn (x, y);
351 insn = emit_add2_insn (x, z);
352 lra_assert (insn != NULL_RTX);
354 else
356 if (index_scale == NULL_RTX)
357 index_scale = index;
358 if (disp == NULL_RTX)
360 /* Generate x = index_scale; x = x + base. */
361 lra_assert (index_scale != NULL_RTX && base != NULL_RTX);
362 emit_move_insn (x, index_scale);
363 insn = emit_add2_insn (x, base);
364 lra_assert (insn != NULL_RTX);
366 else if (scale == NULL_RTX)
368 /* Try x = base + disp. */
369 lra_assert (base != NULL_RTX);
370 last = get_last_insn ();
371 insn = emit_move_insn (x, gen_rtx_PLUS (GET_MODE (base),
372 base, disp));
373 if (recog_memoized (insn) < 0)
375 delete_insns_since (last);
376 /* Generate x = disp; x = x + base. */
377 emit_move_insn (x, disp);
378 insn = emit_add2_insn (x, base);
379 lra_assert (insn != NULL_RTX);
381 /* Generate x = x + index. */
382 if (index != NULL_RTX)
384 insn = emit_add2_insn (x, index);
385 lra_assert (insn != NULL_RTX);
388 else
390 /* Try x = index_scale; x = x + disp; x = x + base. */
391 last = get_last_insn ();
392 insn = emit_move_insn (x, index_scale);
393 ok_p = false;
394 if (recog_memoized (insn) >= 0)
396 insn = emit_add2_insn (x, disp);
397 if (insn != NULL_RTX)
399 insn = emit_add2_insn (x, disp);
400 if (insn != NULL_RTX)
401 ok_p = true;
404 if (! ok_p)
406 delete_insns_since (last);
407 /* Generate x = disp; x = x + base; x = x + index_scale. */
408 emit_move_insn (x, disp);
409 insn = emit_add2_insn (x, base);
410 lra_assert (insn != NULL_RTX);
411 insn = emit_add2_insn (x, index_scale);
412 lra_assert (insn != NULL_RTX);
417 /* Functions emit_... can create pseudos -- so expand the pseudo
418 data. */
419 if (old != max_reg_num ())
420 expand_reg_data ();
423 /* The number of emitted reload insns so far. */
424 int lra_curr_reload_num;
426 /* Emit x := y, processing special case when y = u + v or y = u + v *
427 scale + w through emit_add (Y can be an address which is base +
428 index reg * scale + displacement in general case). X may be used
429 as intermediate result therefore it should be not in Y. */
430 void
431 lra_emit_move (rtx x, rtx y)
433 int old;
435 if (GET_CODE (y) != PLUS)
437 if (rtx_equal_p (x, y))
438 return;
439 old = max_reg_num ();
440 emit_move_insn (x, y);
441 if (REG_P (x))
442 lra_reg_info[ORIGINAL_REGNO (x)].last_reload = ++lra_curr_reload_num;
443 /* Function emit_move can create pseudos -- so expand the pseudo
444 data. */
445 if (old != max_reg_num ())
446 expand_reg_data ();
447 return;
449 lra_emit_add (x, XEXP (y, 0), XEXP (y, 1));
452 /* Update insn operands which are duplication of operands whose
453 numbers are in array of NOPS (with end marker -1). The insn is
454 represented by its LRA internal representation ID. */
455 void
456 lra_update_dups (lra_insn_recog_data_t id, signed char *nops)
458 int i, j, nop;
459 struct lra_static_insn_data *static_id = id->insn_static_data;
461 for (i = 0; i < static_id->n_dups; i++)
462 for (j = 0; (nop = nops[j]) >= 0; j++)
463 if (static_id->dup_num[i] == nop)
464 *id->dup_loc[i] = *id->operand_loc[nop];
469 /* This page contains code dealing with info about registers in the
470 insns. */
472 /* Pools for insn reg info. */
473 static alloc_pool insn_reg_pool;
475 /* Initiate pool for insn reg info. */
476 static void
477 init_insn_regs (void)
479 insn_reg_pool
480 = create_alloc_pool ("insn regs", sizeof (struct lra_insn_reg), 100);
483 /* Create LRA insn related info about a reference to REGNO in INSN with
484 TYPE (in/out/inout), biggest reference mode MODE, flag that it is
485 reference through subreg (SUBREG_P), flag that is early clobbered
486 in the insn (EARLY_CLOBBER), and reference to the next insn reg
487 info (NEXT). */
488 static struct lra_insn_reg *
489 new_insn_reg (rtx insn, int regno, enum op_type type, enum machine_mode mode,
490 bool subreg_p, bool early_clobber, struct lra_insn_reg *next)
492 struct lra_insn_reg *ir;
494 ir = (struct lra_insn_reg *) pool_alloc (insn_reg_pool);
495 ir->type = type;
496 ir->biggest_mode = mode;
497 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (lra_reg_info[regno].biggest_mode)
498 && NONDEBUG_INSN_P (insn))
499 lra_reg_info[regno].biggest_mode = mode;
500 ir->subreg_p = subreg_p;
501 ir->early_clobber = early_clobber;
502 ir->regno = regno;
503 ir->next = next;
504 return ir;
507 /* Free insn reg info IR. */
508 static void
509 free_insn_reg (struct lra_insn_reg *ir)
511 pool_free (insn_reg_pool, ir);
514 /* Free insn reg info list IR. */
515 static void
516 free_insn_regs (struct lra_insn_reg *ir)
518 struct lra_insn_reg *next_ir;
520 for (; ir != NULL; ir = next_ir)
522 next_ir = ir->next;
523 free_insn_reg (ir);
527 /* Finish pool for insn reg info. */
528 static void
529 finish_insn_regs (void)
531 free_alloc_pool (insn_reg_pool);
536 /* This page contains code dealing LRA insn info (or in other words
537 LRA internal insn representation). */
539 struct target_lra_int default_target_lra_int;
540 #if SWITCHABLE_TARGET
541 struct target_lra_int *this_target_lra_int = &default_target_lra_int;
542 #endif
544 /* Map INSN_CODE -> the static insn data. This info is valid during
545 all translation unit. */
546 struct lra_static_insn_data *insn_code_data[LAST_INSN_CODE];
548 /* Debug insns are represented as a special insn with one input
549 operand which is RTL expression in var_location. */
551 /* The following data are used as static insn operand data for all
552 debug insns. If structure lra_operand_data is changed, the
553 initializer should be changed too. */
554 static struct lra_operand_data debug_operand_data =
556 NULL, /* alternative */
557 VOIDmode, /* We are not interesting in the operand mode. */
558 OP_IN,
559 0, 0, 0, 0
562 /* The following data are used as static insn data for all debug
563 insns. If structure lra_static_insn_data is changed, the
564 initializer should be changed too. */
565 static struct lra_static_insn_data debug_insn_static_data =
567 &debug_operand_data,
568 0, /* Duplication operands #. */
569 -1, /* Commutative operand #. */
570 1, /* Operands #. There is only one operand which is debug RTL
571 expression. */
572 0, /* Duplications #. */
573 0, /* Alternatives #. We are not interesting in alternatives
574 because we does not proceed debug_insns for reloads. */
575 NULL, /* Hard registers referenced in machine description. */
576 NULL /* Descriptions of operands in alternatives. */
579 /* Called once per compiler work to initialize some LRA data related
580 to insns. */
581 static void
582 init_insn_code_data_once (void)
584 memset (insn_code_data, 0, sizeof (insn_code_data));
585 memset (op_alt_data, 0, sizeof (op_alt_data));
588 /* Called once per compiler work to finalize some LRA data related to
589 insns. */
590 static void
591 finish_insn_code_data_once (void)
593 int i;
595 for (i = 0; i < LAST_INSN_CODE; i++)
597 if (insn_code_data[i] != NULL)
598 free (insn_code_data[i]);
599 if (op_alt_data[i] != NULL)
600 free (op_alt_data[i]);
604 /* Initialize LRA info about operands in insn alternatives. */
605 static void
606 init_op_alt_data (void)
608 int i;
610 for (i = 0; i < LAST_INSN_CODE; i++)
611 if (op_alt_data[i] != NULL)
613 free (op_alt_data[i]);
614 op_alt_data[i] = NULL;
618 /* Return static insn data, allocate and setup if necessary. Although
619 dup_num is static data (it depends only on icode), to set it up we
620 need to extract insn first. So recog_data should be valid for
621 normal insn (ICODE >= 0) before the call. */
622 static struct lra_static_insn_data *
623 get_static_insn_data (int icode, int nop, int ndup, int nalt)
625 struct lra_static_insn_data *data;
626 size_t n_bytes;
628 lra_assert (icode < LAST_INSN_CODE);
629 if (icode >= 0 && (data = insn_code_data[icode]) != NULL)
630 return data;
631 lra_assert (nop >= 0 && ndup >= 0 && nalt >= 0);
632 n_bytes = sizeof (struct lra_static_insn_data)
633 + sizeof (struct lra_operand_data) * nop
634 + sizeof (int) * ndup;
635 data = XNEWVAR (struct lra_static_insn_data, n_bytes);
636 data->n_operands = nop;
637 data->n_dups = ndup;
638 data->n_alternatives = nalt;
639 data->operand = ((struct lra_operand_data *)
640 ((char *) data + sizeof (struct lra_static_insn_data)));
641 data->dup_num = ((int *) ((char *) data->operand
642 + sizeof (struct lra_operand_data) * nop));
643 if (icode >= 0)
645 int i;
647 insn_code_data[icode] = data;
648 for (i = 0; i < nop; i++)
650 data->operand[i].constraint
651 = insn_data[icode].operand[i].constraint;
652 data->operand[i].mode = insn_data[icode].operand[i].mode;
653 data->operand[i].strict_low = insn_data[icode].operand[i].strict_low;
654 data->operand[i].is_operator
655 = insn_data[icode].operand[i].is_operator;
656 data->operand[i].type
657 = (data->operand[i].constraint[0] == '=' ? OP_OUT
658 : data->operand[i].constraint[0] == '+' ? OP_INOUT
659 : OP_IN);
660 data->operand[i].is_address = false;
662 for (i = 0; i < ndup; i++)
663 data->dup_num[i] = recog_data.dup_num[i];
665 return data;
668 /* The current length of the following array. */
669 int lra_insn_recog_data_len;
671 /* Map INSN_UID -> the insn recog data (NULL if unknown). */
672 lra_insn_recog_data_t *lra_insn_recog_data;
674 /* Initialize LRA data about insns. */
675 static void
676 init_insn_recog_data (void)
678 lra_insn_recog_data_len = 0;
679 lra_insn_recog_data = NULL;
680 init_insn_regs ();
683 /* Expand, if necessary, LRA data about insns. */
684 static void
685 check_and_expand_insn_recog_data (int index)
687 int i, old;
689 if (lra_insn_recog_data_len > index)
690 return;
691 old = lra_insn_recog_data_len;
692 lra_insn_recog_data_len = index * 3 / 2 + 1;
693 lra_insn_recog_data = XRESIZEVEC (lra_insn_recog_data_t,
694 lra_insn_recog_data,
695 lra_insn_recog_data_len);
696 for (i = old; i < lra_insn_recog_data_len; i++)
697 lra_insn_recog_data[i] = NULL;
700 /* Finish LRA DATA about insn. */
701 static void
702 free_insn_recog_data (lra_insn_recog_data_t data)
704 if (data->operand_loc != NULL)
705 free (data->operand_loc);
706 if (data->dup_loc != NULL)
707 free (data->dup_loc);
708 if (data->arg_hard_regs != NULL)
709 free (data->arg_hard_regs);
710 if (HAVE_ATTR_enabled && data->alternative_enabled_p != NULL)
711 free (data->alternative_enabled_p);
712 if (data->icode < 0 && NONDEBUG_INSN_P (data->insn))
714 if (data->insn_static_data->operand_alternative != NULL)
715 free (data->insn_static_data->operand_alternative);
716 free_insn_regs (data->insn_static_data->hard_regs);
717 free (data->insn_static_data);
719 free_insn_regs (data->regs);
720 data->regs = NULL;
721 free (data);
724 /* Finish LRA data about all insns. */
725 static void
726 finish_insn_recog_data (void)
728 int i;
729 lra_insn_recog_data_t data;
731 for (i = 0; i < lra_insn_recog_data_len; i++)
732 if ((data = lra_insn_recog_data[i]) != NULL)
733 free_insn_recog_data (data);
734 finish_insn_regs ();
735 free (lra_insn_recog_data);
738 /* Setup info about operands in alternatives of LRA DATA of insn. */
739 static void
740 setup_operand_alternative (lra_insn_recog_data_t data)
742 int i, nop, nalt;
743 int icode = data->icode;
744 struct lra_static_insn_data *static_data = data->insn_static_data;
746 if (icode >= 0
747 && (static_data->operand_alternative = op_alt_data[icode]) != NULL)
748 return;
749 static_data->commutative = -1;
750 nop = static_data->n_operands;
751 if (nop == 0)
753 static_data->operand_alternative = NULL;
754 return;
756 nalt = static_data->n_alternatives;
757 static_data->operand_alternative = XNEWVEC (struct operand_alternative,
758 nalt * nop);
759 memset (static_data->operand_alternative, 0,
760 nalt * nop * sizeof (struct operand_alternative));
761 if (icode >= 0)
762 op_alt_data[icode] = static_data->operand_alternative;
763 for (i = 0; i < nop; i++)
765 int j;
766 struct operand_alternative *op_alt_start, *op_alt;
767 const char *p = static_data->operand[i].constraint;
769 static_data->operand[i].early_clobber = 0;
770 op_alt_start = &static_data->operand_alternative[i];
772 for (j = 0; j < nalt; j++)
774 op_alt = op_alt_start + j * nop;
775 op_alt->cl = NO_REGS;
776 op_alt->constraint = p;
777 op_alt->matches = -1;
778 op_alt->matched = -1;
780 if (*p == '\0' || *p == ',')
782 op_alt->anything_ok = 1;
783 continue;
786 for (;;)
788 char c = *p;
789 if (c == '#')
791 c = *++p;
792 while (c != ',' && c != '\0');
793 if (c == ',' || c == '\0')
795 p++;
796 break;
799 switch (c)
801 case '=': case '+': case '*':
802 case 'E': case 'F': case 'G': case 'H':
803 case 's': case 'i': case 'n':
804 case 'I': case 'J': case 'K': case 'L':
805 case 'M': case 'N': case 'O': case 'P':
806 /* These don't say anything we care about. */
807 break;
809 case '%':
810 /* We currently only support one commutative pair of
811 operands. */
812 if (static_data->commutative < 0)
813 static_data->commutative = i;
814 else
815 lra_assert (data->icode < 0); /* Asm */
817 /* The last operand should not be marked
818 commutative. */
819 lra_assert (i != nop - 1);
820 break;
822 case '?':
823 op_alt->reject += LRA_LOSER_COST_FACTOR;
824 break;
825 case '!':
826 op_alt->reject += LRA_MAX_REJECT;
827 break;
828 case '&':
829 op_alt->earlyclobber = 1;
830 static_data->operand[i].early_clobber = 1;
831 break;
833 case '0': case '1': case '2': case '3': case '4':
834 case '5': case '6': case '7': case '8': case '9':
836 char *end;
837 op_alt->matches = strtoul (p, &end, 10);
838 static_data->operand_alternative
839 [j * nop + op_alt->matches].matched = i;
840 p = end;
842 continue;
844 case TARGET_MEM_CONSTRAINT:
845 op_alt->memory_ok = 1;
846 break;
847 case '<':
848 op_alt->decmem_ok = 1;
849 break;
850 case '>':
851 op_alt->incmem_ok = 1;
852 break;
853 case 'V':
854 op_alt->nonoffmem_ok = 1;
855 break;
856 case 'o':
857 op_alt->offmem_ok = 1;
858 break;
859 case 'X':
860 op_alt->anything_ok = 1;
861 break;
863 case 'p':
864 static_data->operand[i].is_address = true;
865 op_alt->is_address = 1;
866 op_alt->cl = (reg_class_subunion[(int) op_alt->cl]
867 [(int) base_reg_class (VOIDmode,
868 ADDR_SPACE_GENERIC,
869 ADDRESS, SCRATCH)]);
870 break;
872 case 'g':
873 case 'r':
874 op_alt->cl =
875 reg_class_subunion[(int) op_alt->cl][(int) GENERAL_REGS];
876 break;
878 default:
879 if (EXTRA_MEMORY_CONSTRAINT (c, p))
881 op_alt->memory_ok = 1;
882 break;
884 if (EXTRA_ADDRESS_CONSTRAINT (c, p))
886 static_data->operand[i].is_address = true;
887 op_alt->is_address = 1;
888 op_alt->cl
889 = (reg_class_subunion
890 [(int) op_alt->cl]
891 [(int) base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
892 ADDRESS, SCRATCH)]);
893 break;
896 op_alt->cl
897 = (reg_class_subunion
898 [(int) op_alt->cl]
899 [(int)
900 REG_CLASS_FROM_CONSTRAINT ((unsigned char) c, p)]);
901 break;
903 p += CONSTRAINT_LEN (c, p);
909 /* Recursively process X and collect info about registers, which are
910 not the insn operands, in X with TYPE (in/out/inout) and flag that
911 it is early clobbered in the insn (EARLY_CLOBBER) and add the info
912 to LIST. X is a part of insn given by DATA. Return the result
913 list. */
914 static struct lra_insn_reg *
915 collect_non_operand_hard_regs (rtx *x, lra_insn_recog_data_t data,
916 struct lra_insn_reg *list,
917 enum op_type type, bool early_clobber)
919 int i, j, regno, last;
920 bool subreg_p;
921 enum machine_mode mode;
922 struct lra_insn_reg *curr;
923 rtx op = *x;
924 enum rtx_code code = GET_CODE (op);
925 const char *fmt = GET_RTX_FORMAT (code);
927 for (i = 0; i < data->insn_static_data->n_operands; i++)
928 if (x == data->operand_loc[i])
929 /* It is an operand loc. Stop here. */
930 return list;
931 for (i = 0; i < data->insn_static_data->n_dups; i++)
932 if (x == data->dup_loc[i])
933 /* It is a dup loc. Stop here. */
934 return list;
935 mode = GET_MODE (op);
936 subreg_p = false;
937 if (code == SUBREG)
939 op = SUBREG_REG (op);
940 code = GET_CODE (op);
941 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (op)))
943 mode = GET_MODE (op);
944 if (GET_MODE_SIZE (mode) > REGMODE_NATURAL_SIZE (mode))
945 subreg_p = true;
948 if (REG_P (op))
950 if ((regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER)
951 return list;
952 for (last = regno + hard_regno_nregs[regno][mode];
953 regno < last;
954 regno++)
955 if (! TEST_HARD_REG_BIT (lra_no_alloc_regs, regno)
956 || TEST_HARD_REG_BIT (eliminable_regset, regno))
958 for (curr = list; curr != NULL; curr = curr->next)
959 if (curr->regno == regno && curr->subreg_p == subreg_p
960 && curr->biggest_mode == mode)
962 if (curr->type != type)
963 curr->type = OP_INOUT;
964 if (curr->early_clobber != early_clobber)
965 curr->early_clobber = true;
966 break;
968 if (curr == NULL)
970 /* This is a new hard regno or the info can not be
971 integrated into the found structure. */
972 #ifdef STACK_REGS
973 early_clobber
974 = (early_clobber
975 /* This clobber is to inform popping floating
976 point stack only. */
977 && ! (FIRST_STACK_REG <= regno
978 && regno <= LAST_STACK_REG));
979 #endif
980 list = new_insn_reg (data->insn, regno, type, mode, subreg_p,
981 early_clobber, list);
984 return list;
986 switch (code)
988 case SET:
989 list = collect_non_operand_hard_regs (&SET_DEST (op), data,
990 list, OP_OUT, false);
991 list = collect_non_operand_hard_regs (&SET_SRC (op), data,
992 list, OP_IN, false);
993 break;
994 case CLOBBER:
995 /* We treat clobber of non-operand hard registers as early
996 clobber (the behavior is expected from asm). */
997 list = collect_non_operand_hard_regs (&XEXP (op, 0), data,
998 list, OP_OUT, true);
999 break;
1000 case PRE_INC: case PRE_DEC: case POST_INC: case POST_DEC:
1001 list = collect_non_operand_hard_regs (&XEXP (op, 0), data,
1002 list, OP_INOUT, false);
1003 break;
1004 case PRE_MODIFY: case POST_MODIFY:
1005 list = collect_non_operand_hard_regs (&XEXP (op, 0), data,
1006 list, OP_INOUT, false);
1007 list = collect_non_operand_hard_regs (&XEXP (op, 1), data,
1008 list, OP_IN, false);
1009 break;
1010 default:
1011 fmt = GET_RTX_FORMAT (code);
1012 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1014 if (fmt[i] == 'e')
1015 list = collect_non_operand_hard_regs (&XEXP (op, i), data,
1016 list, OP_IN, false);
1017 else if (fmt[i] == 'E')
1018 for (j = XVECLEN (op, i) - 1; j >= 0; j--)
1019 list = collect_non_operand_hard_regs (&XVECEXP (op, i, j), data,
1020 list, OP_IN, false);
1023 return list;
1026 /* Set up and return info about INSN. Set up the info if it is not set up
1027 yet. */
1028 lra_insn_recog_data_t
1029 lra_set_insn_recog_data (rtx insn)
1031 lra_insn_recog_data_t data;
1032 int i, n, icode;
1033 rtx **locs;
1034 unsigned int uid = INSN_UID (insn);
1035 struct lra_static_insn_data *insn_static_data;
1037 check_and_expand_insn_recog_data (uid);
1038 if (DEBUG_INSN_P (insn))
1039 icode = -1;
1040 else
1042 icode = INSN_CODE (insn);
1043 if (icode < 0)
1044 /* It might be a new simple insn which is not recognized yet. */
1045 INSN_CODE (insn) = icode = recog_memoized (insn);
1047 data = XNEW (struct lra_insn_recog_data);
1048 lra_insn_recog_data[uid] = data;
1049 data->insn = insn;
1050 data->used_insn_alternative = -1;
1051 data->icode = icode;
1052 data->regs = NULL;
1053 if (DEBUG_INSN_P (insn))
1055 data->insn_static_data = &debug_insn_static_data;
1056 data->dup_loc = NULL;
1057 data->arg_hard_regs = NULL;
1058 data->alternative_enabled_p = NULL;
1059 data->operand_loc = XNEWVEC (rtx *, 1);
1060 data->operand_loc[0] = &INSN_VAR_LOCATION_LOC (insn);
1061 return data;
1063 if (icode < 0)
1065 int nop;
1066 enum machine_mode operand_mode[MAX_RECOG_OPERANDS];
1067 const char *constraints[MAX_RECOG_OPERANDS];
1069 nop = asm_noperands (PATTERN (insn));
1070 data->operand_loc = data->dup_loc = NULL;
1071 if (nop < 0)
1072 /* Its is a special insn like USE or CLOBBER. */
1073 data->insn_static_data = insn_static_data
1074 = get_static_insn_data (-1, 0, 0, 1);
1075 else
1077 /* expand_asm_operands makes sure there aren't too many
1078 operands. */
1079 lra_assert (nop <= MAX_RECOG_OPERANDS);
1080 if (nop != 0)
1081 data->operand_loc = XNEWVEC (rtx *, nop);
1082 /* Now get the operand values and constraints out of the
1083 insn. */
1084 decode_asm_operands (PATTERN (insn), NULL,
1085 data->operand_loc,
1086 constraints, operand_mode, NULL);
1087 n = 1;
1088 if (nop > 0)
1090 const char *p = recog_data.constraints[0];
1092 for (p = constraints[0]; *p; p++)
1093 n += *p == ',';
1095 data->insn_static_data = insn_static_data
1096 = get_static_insn_data (-1, nop, 0, n);
1097 for (i = 0; i < nop; i++)
1099 insn_static_data->operand[i].mode = operand_mode[i];
1100 insn_static_data->operand[i].constraint = constraints[i];
1101 insn_static_data->operand[i].strict_low = false;
1102 insn_static_data->operand[i].is_operator = false;
1103 insn_static_data->operand[i].is_address = false;
1106 for (i = 0; i < insn_static_data->n_operands; i++)
1107 insn_static_data->operand[i].type
1108 = (insn_static_data->operand[i].constraint[0] == '=' ? OP_OUT
1109 : insn_static_data->operand[i].constraint[0] == '+' ? OP_INOUT
1110 : OP_IN);
1111 data->alternative_enabled_p = NULL;
1113 else
1115 insn_extract (insn);
1116 data->insn_static_data = insn_static_data
1117 = get_static_insn_data (icode, insn_data[icode].n_operands,
1118 insn_data[icode].n_dups,
1119 insn_data[icode].n_alternatives);
1120 n = insn_static_data->n_operands;
1121 if (n == 0)
1122 locs = NULL;
1123 else
1125 locs = XNEWVEC (rtx *, n);
1126 memcpy (locs, recog_data.operand_loc, n * sizeof (rtx *));
1128 data->operand_loc = locs;
1129 n = insn_static_data->n_dups;
1130 if (n == 0)
1131 locs = NULL;
1132 else
1134 locs = XNEWVEC (rtx *, n);
1135 memcpy (locs, recog_data.dup_loc, n * sizeof (rtx *));
1137 data->dup_loc = locs;
1138 if (HAVE_ATTR_enabled)
1140 bool *bp;
1142 n = insn_static_data->n_alternatives;
1143 lra_assert (n >= 0);
1144 data->alternative_enabled_p = bp = XNEWVEC (bool, n);
1145 /* Cache the insn because we don't want to call extract_insn
1146 from get_attr_enabled as extract_insn modifies
1147 which_alternative. The attribute enabled should not depend
1148 on insn operands, operand modes, operand types, and operand
1149 constraints. It should depend on the architecture. If it
1150 is not true, we should rewrite this file code to use
1151 extract_insn instead of less expensive insn_extract. */
1152 recog_data.insn = insn;
1153 for (i = 0; i < n; i++)
1155 which_alternative = i;
1156 bp[i] = get_attr_enabled (insn);
1160 if (GET_CODE (PATTERN (insn)) == CLOBBER || GET_CODE (PATTERN (insn)) == USE)
1161 insn_static_data->hard_regs = NULL;
1162 else
1163 insn_static_data->hard_regs
1164 = collect_non_operand_hard_regs (&PATTERN (insn), data,
1165 NULL, OP_IN, false);
1166 setup_operand_alternative (data);
1167 data->arg_hard_regs = NULL;
1168 if (CALL_P (insn))
1170 rtx link;
1171 int n_hard_regs, regno, arg_hard_regs[FIRST_PSEUDO_REGISTER];
1173 n_hard_regs = 0;
1174 /* Finding implicit hard register usage. We believe it will be
1175 not changed whatever transformations are used. Call insns
1176 are such example. */
1177 for (link = CALL_INSN_FUNCTION_USAGE (insn);
1178 link != NULL_RTX;
1179 link = XEXP (link, 1))
1180 if (GET_CODE (XEXP (link, 0)) == USE
1181 && REG_P (XEXP (XEXP (link, 0), 0)))
1183 regno = REGNO (XEXP (XEXP (link, 0), 0));
1184 lra_assert (regno < FIRST_PSEUDO_REGISTER);
1185 /* It is an argument register. */
1186 for (i = (hard_regno_nregs
1187 [regno][GET_MODE (XEXP (XEXP (link, 0), 0))]) - 1;
1188 i >= 0;
1189 i--)
1190 arg_hard_regs[n_hard_regs++] = regno + i;
1192 if (n_hard_regs != 0)
1194 arg_hard_regs[n_hard_regs++] = -1;
1195 data->arg_hard_regs = XNEWVEC (int, n_hard_regs);
1196 memcpy (data->arg_hard_regs, arg_hard_regs,
1197 sizeof (int) * n_hard_regs);
1200 /* Some output operand can be recognized only from the context not
1201 from the constraints which are empty in this case. Call insn may
1202 contain a hard register in set destination with empty constraint
1203 and extract_insn treats them as an input. */
1204 for (i = 0; i < insn_static_data->n_operands; i++)
1206 int j;
1207 rtx pat, set;
1208 struct lra_operand_data *operand = &insn_static_data->operand[i];
1210 /* ??? Should we treat 'X' the same way. It looks to me that
1211 'X' means anything and empty constraint means we do not
1212 care. */
1213 if (operand->type != OP_IN || *operand->constraint != '\0'
1214 || operand->is_operator)
1215 continue;
1216 pat = PATTERN (insn);
1217 if (GET_CODE (pat) == SET)
1219 if (data->operand_loc[i] != &SET_DEST (pat))
1220 continue;
1222 else if (GET_CODE (pat) == PARALLEL)
1224 for (j = XVECLEN (pat, 0) - 1; j >= 0; j--)
1226 set = XVECEXP (PATTERN (insn), 0, j);
1227 if (GET_CODE (set) == SET
1228 && &SET_DEST (set) == data->operand_loc[i])
1229 break;
1231 if (j < 0)
1232 continue;
1234 else
1235 continue;
1236 operand->type = OP_OUT;
1238 return data;
1241 /* Return info about insn give by UID. The info should be already set
1242 up. */
1243 static lra_insn_recog_data_t
1244 get_insn_recog_data_by_uid (int uid)
1246 lra_insn_recog_data_t data;
1248 data = lra_insn_recog_data[uid];
1249 lra_assert (data != NULL);
1250 return data;
1253 /* Invalidate all info about insn given by its UID. */
1254 static void
1255 invalidate_insn_recog_data (int uid)
1257 lra_insn_recog_data_t data;
1259 data = lra_insn_recog_data[uid];
1260 lra_assert (data != NULL);
1261 free_insn_recog_data (data);
1262 lra_insn_recog_data[uid] = NULL;
1265 /* Update all the insn info about INSN. It is usually called when
1266 something in the insn was changed. Return the updated info. */
1267 lra_insn_recog_data_t
1268 lra_update_insn_recog_data (rtx insn)
1270 lra_insn_recog_data_t data;
1271 int n;
1272 unsigned int uid = INSN_UID (insn);
1273 struct lra_static_insn_data *insn_static_data;
1275 check_and_expand_insn_recog_data (uid);
1276 if ((data = lra_insn_recog_data[uid]) != NULL
1277 && data->icode != INSN_CODE (insn))
1279 invalidate_insn_data_regno_info (data, insn, get_insn_freq (insn));
1280 invalidate_insn_recog_data (uid);
1281 data = NULL;
1283 if (data == NULL)
1284 return lra_get_insn_recog_data (insn);
1285 insn_static_data = data->insn_static_data;
1286 data->used_insn_alternative = -1;
1287 if (DEBUG_INSN_P (insn))
1288 return data;
1289 if (data->icode < 0)
1291 int nop;
1292 enum machine_mode operand_mode[MAX_RECOG_OPERANDS];
1293 const char *constraints[MAX_RECOG_OPERANDS];
1295 nop = asm_noperands (PATTERN (insn));
1296 if (nop >= 0)
1298 lra_assert (nop == data->insn_static_data->n_operands);
1299 /* Now get the operand values and constraints out of the
1300 insn. */
1301 decode_asm_operands (PATTERN (insn), NULL,
1302 data->operand_loc,
1303 constraints, operand_mode, NULL);
1304 #ifdef ENABLE_CHECKING
1306 int i;
1308 for (i = 0; i < nop; i++)
1309 lra_assert
1310 (insn_static_data->operand[i].mode == operand_mode[i]
1311 && insn_static_data->operand[i].constraint == constraints[i]
1312 && ! insn_static_data->operand[i].is_operator);
1314 #endif
1316 #ifdef ENABLE_CHECKING
1318 int i;
1320 for (i = 0; i < insn_static_data->n_operands; i++)
1321 lra_assert
1322 (insn_static_data->operand[i].type
1323 == (insn_static_data->operand[i].constraint[0] == '=' ? OP_OUT
1324 : insn_static_data->operand[i].constraint[0] == '+' ? OP_INOUT
1325 : OP_IN));
1327 #endif
1329 else
1331 insn_extract (insn);
1332 n = insn_static_data->n_operands;
1333 if (n != 0)
1334 memcpy (data->operand_loc, recog_data.operand_loc, n * sizeof (rtx *));
1335 n = insn_static_data->n_dups;
1336 if (n != 0)
1337 memcpy (data->dup_loc, recog_data.dup_loc, n * sizeof (rtx *));
1338 #if HAVE_ATTR_enabled
1339 #ifdef ENABLE_CHECKING
1341 int i;
1342 bool *bp;
1344 n = insn_static_data->n_alternatives;
1345 bp = data->alternative_enabled_p;
1346 lra_assert (n >= 0 && bp != NULL);
1347 /* Cache the insn to prevent extract_insn call from
1348 get_attr_enabled. */
1349 recog_data.insn = insn;
1350 for (i = 0; i < n; i++)
1352 which_alternative = i;
1353 lra_assert (bp[i] == get_attr_enabled (insn));
1356 #endif
1357 #endif
1359 return data;
1362 /* Set up that INSN is using alternative ALT now. */
1363 void
1364 lra_set_used_insn_alternative (rtx insn, int alt)
1366 lra_insn_recog_data_t data;
1368 data = lra_get_insn_recog_data (insn);
1369 data->used_insn_alternative = alt;
1372 /* Set up that insn with UID is using alternative ALT now. The insn
1373 info should be already set up. */
1374 void
1375 lra_set_used_insn_alternative_by_uid (int uid, int alt)
1377 lra_insn_recog_data_t data;
1379 check_and_expand_insn_recog_data (uid);
1380 data = lra_insn_recog_data[uid];
1381 lra_assert (data != NULL);
1382 data->used_insn_alternative = alt;
1387 /* This page contains code dealing with common register info and
1388 pseudo copies. */
1390 /* The size of the following array. */
1391 static int reg_info_size;
1392 /* Common info about each register. */
1393 struct lra_reg *lra_reg_info;
1395 /* Last register value. */
1396 static int last_reg_value;
1398 /* Return new register value. */
1399 static int
1400 get_new_reg_value (void)
1402 return ++last_reg_value;
1405 /* Pools for copies. */
1406 static alloc_pool copy_pool;
1408 /* Vec referring to pseudo copies. */
1409 static vec<lra_copy_t> copy_vec;
1411 /* Initialize I-th element of lra_reg_info. */
1412 static inline void
1413 initialize_lra_reg_info_element (int i)
1415 bitmap_initialize (&lra_reg_info[i].insn_bitmap, &reg_obstack);
1416 #ifdef STACK_REGS
1417 lra_reg_info[i].no_stack_p = false;
1418 #endif
1419 CLEAR_HARD_REG_SET (lra_reg_info[i].conflict_hard_regs);
1420 lra_reg_info[i].preferred_hard_regno1 = -1;
1421 lra_reg_info[i].preferred_hard_regno2 = -1;
1422 lra_reg_info[i].preferred_hard_regno_profit1 = 0;
1423 lra_reg_info[i].preferred_hard_regno_profit2 = 0;
1424 lra_reg_info[i].biggest_mode = VOIDmode;
1425 lra_reg_info[i].live_ranges = NULL;
1426 lra_reg_info[i].nrefs = lra_reg_info[i].freq = 0;
1427 lra_reg_info[i].last_reload = 0;
1428 lra_reg_info[i].restore_regno = -1;
1429 lra_reg_info[i].val = get_new_reg_value ();
1430 lra_reg_info[i].offset = 0;
1431 lra_reg_info[i].copies = NULL;
1434 /* Initialize common reg info and copies. */
1435 static void
1436 init_reg_info (void)
1438 int i;
1440 last_reg_value = 0;
1441 reg_info_size = max_reg_num () * 3 / 2 + 1;
1442 lra_reg_info = XNEWVEC (struct lra_reg, reg_info_size);
1443 for (i = 0; i < reg_info_size; i++)
1444 initialize_lra_reg_info_element (i);
1445 copy_pool
1446 = create_alloc_pool ("lra copies", sizeof (struct lra_copy), 100);
1447 copy_vec.create (100);
1451 /* Finish common reg info and copies. */
1452 static void
1453 finish_reg_info (void)
1455 int i;
1457 for (i = 0; i < reg_info_size; i++)
1458 bitmap_clear (&lra_reg_info[i].insn_bitmap);
1459 free (lra_reg_info);
1460 reg_info_size = 0;
1461 free_alloc_pool (copy_pool);
1462 copy_vec.release ();
1465 /* Expand common reg info if it is necessary. */
1466 static void
1467 expand_reg_info (void)
1469 int i, old = reg_info_size;
1471 if (reg_info_size > max_reg_num ())
1472 return;
1473 reg_info_size = max_reg_num () * 3 / 2 + 1;
1474 lra_reg_info = XRESIZEVEC (struct lra_reg, lra_reg_info, reg_info_size);
1475 for (i = old; i < reg_info_size; i++)
1476 initialize_lra_reg_info_element (i);
1479 /* Free all copies. */
1480 void
1481 lra_free_copies (void)
1483 lra_copy_t cp;
1485 while (copy_vec.length () != 0)
1487 cp = copy_vec.pop ();
1488 lra_reg_info[cp->regno1].copies = lra_reg_info[cp->regno2].copies = NULL;
1489 pool_free (copy_pool, cp);
1493 /* Create copy of two pseudos REGNO1 and REGNO2. The copy execution
1494 frequency is FREQ. */
1495 void
1496 lra_create_copy (int regno1, int regno2, int freq)
1498 bool regno1_dest_p;
1499 lra_copy_t cp;
1501 lra_assert (regno1 != regno2);
1502 regno1_dest_p = true;
1503 if (regno1 > regno2)
1505 int temp = regno2;
1507 regno1_dest_p = false;
1508 regno2 = regno1;
1509 regno1 = temp;
1511 cp = (lra_copy_t) pool_alloc (copy_pool);
1512 copy_vec.safe_push (cp);
1513 cp->regno1_dest_p = regno1_dest_p;
1514 cp->freq = freq;
1515 cp->regno1 = regno1;
1516 cp->regno2 = regno2;
1517 cp->regno1_next = lra_reg_info[regno1].copies;
1518 lra_reg_info[regno1].copies = cp;
1519 cp->regno2_next = lra_reg_info[regno2].copies;
1520 lra_reg_info[regno2].copies = cp;
1521 if (lra_dump_file != NULL)
1522 fprintf (lra_dump_file, " Creating copy r%d%sr%d@%d\n",
1523 regno1, regno1_dest_p ? "<-" : "->", regno2, freq);
1526 /* Return N-th (0, 1, ...) copy. If there is no copy, return
1527 NULL. */
1528 lra_copy_t
1529 lra_get_copy (int n)
1531 if (n >= (int) copy_vec.length ())
1532 return NULL;
1533 return copy_vec[n];
1538 /* This page contains code dealing with info about registers in
1539 insns. */
1541 /* Process X of insn UID recursively and add info (operand type is
1542 given by TYPE, flag of that it is early clobber is EARLY_CLOBBER)
1543 about registers in X to the insn DATA. */
1544 static void
1545 add_regs_to_insn_regno_info (lra_insn_recog_data_t data, rtx x, int uid,
1546 enum op_type type, bool early_clobber)
1548 int i, j, regno;
1549 bool subreg_p;
1550 enum machine_mode mode;
1551 const char *fmt;
1552 enum rtx_code code;
1553 struct lra_insn_reg *curr;
1555 code = GET_CODE (x);
1556 mode = GET_MODE (x);
1557 subreg_p = false;
1558 if (GET_CODE (x) == SUBREG)
1560 x = SUBREG_REG (x);
1561 code = GET_CODE (x);
1562 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (x)))
1564 mode = GET_MODE (x);
1565 if (GET_MODE_SIZE (mode) > REGMODE_NATURAL_SIZE (mode))
1566 subreg_p = true;
1569 if (REG_P (x))
1571 regno = REGNO (x);
1572 if (regno < FIRST_PSEUDO_REGISTER
1573 && TEST_HARD_REG_BIT (lra_no_alloc_regs, regno)
1574 && ! TEST_HARD_REG_BIT (eliminable_regset, regno))
1575 return;
1576 expand_reg_info ();
1577 if (bitmap_set_bit (&lra_reg_info[regno].insn_bitmap, uid))
1579 data->regs = new_insn_reg (data->insn, regno, type, mode, subreg_p,
1580 early_clobber, data->regs);
1581 return;
1583 else
1585 for (curr = data->regs; curr != NULL; curr = curr->next)
1586 if (curr->regno == regno)
1588 if (curr->subreg_p != subreg_p || curr->biggest_mode != mode)
1589 /* The info can not be integrated into the found
1590 structure. */
1591 data->regs = new_insn_reg (data->insn, regno, type, mode,
1592 subreg_p, early_clobber,
1593 data->regs);
1594 else
1596 if (curr->type != type)
1597 curr->type = OP_INOUT;
1598 if (curr->early_clobber != early_clobber)
1599 curr->early_clobber = true;
1601 return;
1603 gcc_unreachable ();
1607 switch (code)
1609 case SET:
1610 add_regs_to_insn_regno_info (data, SET_DEST (x), uid, OP_OUT, false);
1611 add_regs_to_insn_regno_info (data, SET_SRC (x), uid, OP_IN, false);
1612 break;
1613 case CLOBBER:
1614 /* We treat clobber of non-operand hard registers as early
1615 clobber (the behavior is expected from asm). */
1616 add_regs_to_insn_regno_info (data, XEXP (x, 0), uid, OP_OUT, true);
1617 break;
1618 case PRE_INC: case PRE_DEC: case POST_INC: case POST_DEC:
1619 add_regs_to_insn_regno_info (data, XEXP (x, 0), uid, OP_INOUT, false);
1620 break;
1621 case PRE_MODIFY: case POST_MODIFY:
1622 add_regs_to_insn_regno_info (data, XEXP (x, 0), uid, OP_INOUT, false);
1623 add_regs_to_insn_regno_info (data, XEXP (x, 1), uid, OP_IN, false);
1624 break;
1625 default:
1626 if ((code != PARALLEL && code != EXPR_LIST) || type != OP_OUT)
1627 /* Some targets place small structures in registers for return
1628 values of functions, and those registers are wrapped in
1629 PARALLEL that we may see as the destination of a SET. Here
1630 is an example:
1632 (call_insn 13 12 14 2 (set (parallel:BLK [
1633 (expr_list:REG_DEP_TRUE (reg:DI 0 ax)
1634 (const_int 0 [0]))
1635 (expr_list:REG_DEP_TRUE (reg:DI 1 dx)
1636 (const_int 8 [0x8]))
1638 (call (mem:QI (symbol_ref:DI (... */
1639 type = OP_IN;
1640 fmt = GET_RTX_FORMAT (code);
1641 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1643 if (fmt[i] == 'e')
1644 add_regs_to_insn_regno_info (data, XEXP (x, i), uid, type, false);
1645 else if (fmt[i] == 'E')
1647 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1648 add_regs_to_insn_regno_info (data, XVECEXP (x, i, j), uid,
1649 type, false);
1655 /* Return execution frequency of INSN. */
1656 static int
1657 get_insn_freq (rtx insn)
1659 basic_block bb = BLOCK_FOR_INSN (insn);
1661 gcc_checking_assert (bb != NULL);
1662 return REG_FREQ_FROM_BB (bb);
1665 /* Invalidate all reg info of INSN with DATA and execution frequency
1666 FREQ. Update common info about the invalidated registers. */
1667 static void
1668 invalidate_insn_data_regno_info (lra_insn_recog_data_t data, rtx insn,
1669 int freq)
1671 int uid;
1672 bool debug_p;
1673 unsigned int i;
1674 struct lra_insn_reg *ir, *next_ir;
1676 uid = INSN_UID (insn);
1677 debug_p = DEBUG_INSN_P (insn);
1678 for (ir = data->regs; ir != NULL; ir = next_ir)
1680 i = ir->regno;
1681 next_ir = ir->next;
1682 free_insn_reg (ir);
1683 bitmap_clear_bit (&lra_reg_info[i].insn_bitmap, uid);
1684 if (i >= FIRST_PSEUDO_REGISTER && ! debug_p)
1686 lra_reg_info[i].nrefs--;
1687 lra_reg_info[i].freq -= freq;
1688 lra_assert (lra_reg_info[i].nrefs >= 0 && lra_reg_info[i].freq >= 0);
1691 data->regs = NULL;
1694 /* Invalidate all reg info of INSN. Update common info about the
1695 invalidated registers. */
1696 void
1697 lra_invalidate_insn_regno_info (rtx insn)
1699 invalidate_insn_data_regno_info (lra_get_insn_recog_data (insn), insn,
1700 get_insn_freq (insn));
1703 /* Update common reg info from reg info of insn given by its DATA and
1704 execution frequency FREQ. */
1705 static void
1706 setup_insn_reg_info (lra_insn_recog_data_t data, int freq)
1708 unsigned int i;
1709 struct lra_insn_reg *ir;
1711 for (ir = data->regs; ir != NULL; ir = ir->next)
1712 if ((i = ir->regno) >= FIRST_PSEUDO_REGISTER)
1714 lra_reg_info[i].nrefs++;
1715 lra_reg_info[i].freq += freq;
1719 /* Set up insn reg info of INSN. Update common reg info from reg info
1720 of INSN. */
1721 void
1722 lra_update_insn_regno_info (rtx insn)
1724 int i, uid, freq;
1725 lra_insn_recog_data_t data;
1726 struct lra_static_insn_data *static_data;
1727 enum rtx_code code;
1729 if (! INSN_P (insn))
1730 return;
1731 data = lra_get_insn_recog_data (insn);
1732 static_data = data->insn_static_data;
1733 freq = get_insn_freq (insn);
1734 invalidate_insn_data_regno_info (data, insn, freq);
1735 uid = INSN_UID (insn);
1736 for (i = static_data->n_operands - 1; i >= 0; i--)
1737 add_regs_to_insn_regno_info (data, *data->operand_loc[i], uid,
1738 static_data->operand[i].type,
1739 static_data->operand[i].early_clobber);
1740 if ((code = GET_CODE (PATTERN (insn))) == CLOBBER || code == USE)
1741 add_regs_to_insn_regno_info (data, XEXP (PATTERN (insn), 0), uid,
1742 code == USE ? OP_IN : OP_OUT, false);
1743 if (NONDEBUG_INSN_P (insn))
1744 setup_insn_reg_info (data, freq);
1747 /* Return reg info of insn given by it UID. */
1748 struct lra_insn_reg *
1749 lra_get_insn_regs (int uid)
1751 lra_insn_recog_data_t data;
1753 data = get_insn_recog_data_by_uid (uid);
1754 return data->regs;
1759 /* This page contains code dealing with stack of the insns which
1760 should be processed by the next constraint pass. */
1762 /* Bitmap used to put an insn on the stack only in one exemplar. */
1763 static sbitmap lra_constraint_insn_stack_bitmap;
1765 /* The stack itself. */
1766 vec<rtx> lra_constraint_insn_stack;
1768 /* Put INSN on the stack. If ALWAYS_UPDATE is true, always update the reg
1769 info for INSN, otherwise only update it if INSN is not already on the
1770 stack. */
1771 static inline void
1772 lra_push_insn_1 (rtx insn, bool always_update)
1774 unsigned int uid = INSN_UID (insn);
1775 if (always_update)
1776 lra_update_insn_regno_info (insn);
1777 if (uid >= SBITMAP_SIZE (lra_constraint_insn_stack_bitmap))
1778 lra_constraint_insn_stack_bitmap =
1779 sbitmap_resize (lra_constraint_insn_stack_bitmap, 3 * uid / 2, 0);
1780 if (bitmap_bit_p (lra_constraint_insn_stack_bitmap, uid))
1781 return;
1782 bitmap_set_bit (lra_constraint_insn_stack_bitmap, uid);
1783 if (! always_update)
1784 lra_update_insn_regno_info (insn);
1785 lra_constraint_insn_stack.safe_push (insn);
1788 /* Put INSN on the stack. */
1789 void
1790 lra_push_insn (rtx insn)
1792 lra_push_insn_1 (insn, false);
1795 /* Put INSN on the stack and update its reg info. */
1796 void
1797 lra_push_insn_and_update_insn_regno_info (rtx insn)
1799 lra_push_insn_1 (insn, true);
1802 /* Put insn with UID on the stack. */
1803 void
1804 lra_push_insn_by_uid (unsigned int uid)
1806 lra_push_insn (lra_insn_recog_data[uid]->insn);
1809 /* Take the last-inserted insns off the stack and return it. */
1811 lra_pop_insn (void)
1813 rtx insn = lra_constraint_insn_stack.pop ();
1814 bitmap_clear_bit (lra_constraint_insn_stack_bitmap, INSN_UID (insn));
1815 return insn;
1818 /* Return the current size of the insn stack. */
1819 unsigned int
1820 lra_insn_stack_length (void)
1822 return lra_constraint_insn_stack.length ();
1825 /* Push insns FROM to TO (excluding it) going in reverse order. */
1826 static void
1827 push_insns (rtx from, rtx to)
1829 rtx insn;
1831 if (from == NULL_RTX)
1832 return;
1833 for (insn = from; insn != to; insn = PREV_INSN (insn))
1834 if (INSN_P (insn))
1835 lra_push_insn (insn);
1838 /* Emit insns BEFORE before INSN and insns AFTER after INSN. Put the
1839 insns onto the stack. Print about emitting the insns with
1840 TITLE. */
1841 void
1842 lra_process_new_insns (rtx insn, rtx before, rtx after, const char *title)
1844 rtx last;
1846 if (lra_dump_file != NULL && (before != NULL_RTX || after != NULL_RTX))
1848 dump_insn_slim (lra_dump_file, insn);
1849 if (before != NULL_RTX)
1851 fprintf (lra_dump_file," %s before:\n", title);
1852 dump_rtl_slim (lra_dump_file, before, NULL_RTX, -1, 0);
1854 if (after != NULL_RTX)
1856 fprintf (lra_dump_file, " %s after:\n", title);
1857 dump_rtl_slim (lra_dump_file, after, NULL_RTX, -1, 0);
1859 fprintf (lra_dump_file, "\n");
1861 if (before != NULL_RTX)
1863 emit_insn_before (before, insn);
1864 push_insns (PREV_INSN (insn), PREV_INSN (before));
1866 if (after != NULL_RTX)
1868 for (last = after; NEXT_INSN (last) != NULL_RTX; last = NEXT_INSN (last))
1870 emit_insn_after (after, insn);
1871 push_insns (last, insn);
1877 /* This page contains code dealing with scratches (changing them onto
1878 pseudos and restoring them from the pseudos).
1880 We change scratches into pseudos at the beginning of LRA to
1881 simplify dealing with them (conflicts, hard register assignments).
1883 If the pseudo denoting scratch was spilled it means that we do need
1884 a hard register for it. Such pseudos are transformed back to
1885 scratches at the end of LRA. */
1887 /* Description of location of a former scratch operand. */
1888 struct sloc
1890 rtx insn; /* Insn where the scratch was. */
1891 int nop; /* Number of the operand which was a scratch. */
1894 typedef struct sloc *sloc_t;
1896 /* Locations of the former scratches. */
1897 static vec<sloc_t> scratches;
1899 /* Bitmap of scratch regnos. */
1900 static bitmap_head scratch_bitmap;
1902 /* Bitmap of scratch operands. */
1903 static bitmap_head scratch_operand_bitmap;
1905 /* Return true if pseudo REGNO is made of SCRATCH. */
1906 bool
1907 lra_former_scratch_p (int regno)
1909 return bitmap_bit_p (&scratch_bitmap, regno);
1912 /* Return true if the operand NOP of INSN is a former scratch. */
1913 bool
1914 lra_former_scratch_operand_p (rtx insn, int nop)
1916 return bitmap_bit_p (&scratch_operand_bitmap,
1917 INSN_UID (insn) * MAX_RECOG_OPERANDS + nop) != 0;
1920 /* Change scratches onto pseudos and save their location. */
1921 static void
1922 remove_scratches (void)
1924 int i;
1925 bool insn_changed_p;
1926 basic_block bb;
1927 rtx insn, reg;
1928 sloc_t loc;
1929 lra_insn_recog_data_t id;
1930 struct lra_static_insn_data *static_id;
1932 scratches.create (get_max_uid ());
1933 bitmap_initialize (&scratch_bitmap, &reg_obstack);
1934 bitmap_initialize (&scratch_operand_bitmap, &reg_obstack);
1935 FOR_EACH_BB (bb)
1936 FOR_BB_INSNS (bb, insn)
1937 if (INSN_P (insn))
1939 id = lra_get_insn_recog_data (insn);
1940 static_id = id->insn_static_data;
1941 insn_changed_p = false;
1942 for (i = 0; i < static_id->n_operands; i++)
1943 if (GET_CODE (*id->operand_loc[i]) == SCRATCH
1944 && GET_MODE (*id->operand_loc[i]) != VOIDmode)
1946 insn_changed_p = true;
1947 *id->operand_loc[i] = reg
1948 = lra_create_new_reg (static_id->operand[i].mode,
1949 *id->operand_loc[i], ALL_REGS, NULL);
1950 add_reg_note (insn, REG_UNUSED, reg);
1951 lra_update_dup (id, i);
1952 loc = XNEW (struct sloc);
1953 loc->insn = insn;
1954 loc->nop = i;
1955 scratches.safe_push (loc);
1956 bitmap_set_bit (&scratch_bitmap, REGNO (*id->operand_loc[i]));
1957 bitmap_set_bit (&scratch_operand_bitmap,
1958 INSN_UID (insn) * MAX_RECOG_OPERANDS + i);
1959 if (lra_dump_file != NULL)
1960 fprintf (lra_dump_file,
1961 "Removing SCRATCH in insn #%u (nop %d)\n",
1962 INSN_UID (insn), i);
1964 if (insn_changed_p)
1965 /* Because we might use DF right after caller-saves sub-pass
1966 we need to keep DF info up to date. */
1967 df_insn_rescan (insn);
1971 /* Changes pseudos created by function remove_scratches onto scratches. */
1972 static void
1973 restore_scratches (void)
1975 int regno;
1976 unsigned i;
1977 sloc_t loc;
1978 rtx last = NULL_RTX;
1979 lra_insn_recog_data_t id = NULL;
1981 for (i = 0; scratches.iterate (i, &loc); i++)
1983 if (last != loc->insn)
1985 last = loc->insn;
1986 id = lra_get_insn_recog_data (last);
1988 if (REG_P (*id->operand_loc[loc->nop])
1989 && ((regno = REGNO (*id->operand_loc[loc->nop]))
1990 >= FIRST_PSEUDO_REGISTER)
1991 && lra_get_regno_hard_regno (regno) < 0)
1993 /* It should be only case when scratch register with chosen
1994 constraint 'X' did not get memory or hard register. */
1995 lra_assert (lra_former_scratch_p (regno));
1996 *id->operand_loc[loc->nop]
1997 = gen_rtx_SCRATCH (GET_MODE (*id->operand_loc[loc->nop]));
1998 lra_update_dup (id, loc->nop);
1999 if (lra_dump_file != NULL)
2000 fprintf (lra_dump_file, "Restoring SCRATCH in insn #%u(nop %d)\n",
2001 INSN_UID (loc->insn), loc->nop);
2004 for (i = 0; scratches.iterate (i, &loc); i++)
2005 free (loc);
2006 scratches.release ();
2007 bitmap_clear (&scratch_bitmap);
2008 bitmap_clear (&scratch_operand_bitmap);
2013 #ifdef ENABLE_CHECKING
2015 /* Function checks RTL for correctness. If FINAL_P is true, it is
2016 done at the end of LRA and the check is more rigorous. */
2017 static void
2018 check_rtl (bool final_p)
2020 int i;
2021 basic_block bb;
2022 rtx insn;
2023 lra_insn_recog_data_t id;
2025 lra_assert (! final_p || reload_completed);
2026 FOR_EACH_BB (bb)
2027 FOR_BB_INSNS (bb, insn)
2028 if (NONDEBUG_INSN_P (insn)
2029 && GET_CODE (PATTERN (insn)) != USE
2030 && GET_CODE (PATTERN (insn)) != CLOBBER
2031 && GET_CODE (PATTERN (insn)) != ASM_INPUT)
2033 if (final_p)
2035 extract_insn (insn);
2036 lra_assert (constrain_operands (1));
2037 continue;
2039 if (insn_invalid_p (insn, false))
2040 fatal_insn_not_found (insn);
2041 if (asm_noperands (PATTERN (insn)) >= 0)
2042 continue;
2043 id = lra_get_insn_recog_data (insn);
2044 /* The code is based on assumption that all addresses in
2045 regular instruction are legitimate before LRA. The code in
2046 lra-constraints.c is based on assumption that there is no
2047 subreg of memory as an insn operand. */
2048 for (i = 0; i < id->insn_static_data->n_operands; i++)
2050 rtx op = *id->operand_loc[i];
2052 if (MEM_P (op)
2053 && (GET_MODE (op) != BLKmode
2054 || GET_CODE (XEXP (op, 0)) != SCRATCH)
2055 && ! memory_address_p (GET_MODE (op), XEXP (op, 0))
2056 /* Some ports don't recognize the following addresses
2057 as legitimate. Although they are legitimate if
2058 they satisfies the constraints and will be checked
2059 by insn constraints which we ignore here. */
2060 && GET_CODE (XEXP (op, 0)) != UNSPEC
2061 && GET_RTX_CLASS (GET_CODE (XEXP (op, 0))) != RTX_AUTOINC)
2062 fatal_insn_not_found (insn);
2066 #endif /* #ifdef ENABLE_CHECKING */
2068 /* Determine if the current function has an exception receiver block
2069 that reaches the exit block via non-exceptional edges */
2070 static bool
2071 has_nonexceptional_receiver (void)
2073 edge e;
2074 edge_iterator ei;
2075 basic_block *tos, *worklist, bb;
2077 /* If we're not optimizing, then just err on the safe side. */
2078 if (!optimize)
2079 return true;
2081 /* First determine which blocks can reach exit via normal paths. */
2082 tos = worklist = XNEWVEC (basic_block, n_basic_blocks + 1);
2084 FOR_EACH_BB (bb)
2085 bb->flags &= ~BB_REACHABLE;
2087 /* Place the exit block on our worklist. */
2088 EXIT_BLOCK_PTR->flags |= BB_REACHABLE;
2089 *tos++ = EXIT_BLOCK_PTR;
2091 /* Iterate: find everything reachable from what we've already seen. */
2092 while (tos != worklist)
2094 bb = *--tos;
2096 FOR_EACH_EDGE (e, ei, bb->preds)
2097 if (e->flags & EDGE_ABNORMAL)
2099 free (worklist);
2100 return true;
2102 else
2104 basic_block src = e->src;
2106 if (!(src->flags & BB_REACHABLE))
2108 src->flags |= BB_REACHABLE;
2109 *tos++ = src;
2113 free (worklist);
2114 /* No exceptional block reached exit unexceptionally. */
2115 return false;
2118 #ifdef AUTO_INC_DEC
2120 /* Process recursively X of INSN and add REG_INC notes if necessary. */
2121 static void
2122 add_auto_inc_notes (rtx insn, rtx x)
2124 enum rtx_code code = GET_CODE (x);
2125 const char *fmt;
2126 int i, j;
2128 if (code == MEM && auto_inc_p (XEXP (x, 0)))
2130 add_reg_note (insn, REG_INC, XEXP (XEXP (x, 0), 0));
2131 return;
2134 /* Scan all X sub-expressions. */
2135 fmt = GET_RTX_FORMAT (code);
2136 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2138 if (fmt[i] == 'e')
2139 add_auto_inc_notes (insn, XEXP (x, i));
2140 else if (fmt[i] == 'E')
2141 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2142 add_auto_inc_notes (insn, XVECEXP (x, i, j));
2146 #endif
2148 /* Remove all REG_DEAD and REG_UNUSED notes and regenerate REG_INC.
2149 We change pseudos by hard registers without notification of DF and
2150 that can make the notes obsolete. DF-infrastructure does not deal
2151 with REG_INC notes -- so we should regenerate them here. */
2152 static void
2153 update_inc_notes (void)
2155 rtx *pnote;
2156 basic_block bb;
2157 rtx insn;
2159 FOR_EACH_BB (bb)
2160 FOR_BB_INSNS (bb, insn)
2161 if (NONDEBUG_INSN_P (insn))
2163 pnote = &REG_NOTES (insn);
2164 while (*pnote != 0)
2166 if (REG_NOTE_KIND (*pnote) == REG_INC)
2167 *pnote = XEXP (*pnote, 1);
2168 else
2169 pnote = &XEXP (*pnote, 1);
2171 #ifdef AUTO_INC_DEC
2172 add_auto_inc_notes (insn, PATTERN (insn));
2173 #endif
2177 /* Set to 1 while in lra. */
2178 int lra_in_progress;
2180 /* Start of pseudo regnos before the LRA. */
2181 int lra_new_regno_start;
2183 /* Start of reload pseudo regnos before the new spill pass. */
2184 int lra_constraint_new_regno_start;
2186 /* Inheritance pseudo regnos before the new spill pass. */
2187 bitmap_head lra_inheritance_pseudos;
2189 /* Split regnos before the new spill pass. */
2190 bitmap_head lra_split_regs;
2192 /* Reload pseudo regnos before the new assignmnet pass which still can
2193 be spilled after the assinment pass as memory is also accepted in
2194 insns for the reload pseudos. */
2195 bitmap_head lra_optional_reload_pseudos;
2197 /* Pseudo regnos used for subreg reloads before the new assignment
2198 pass. Such pseudos still can be spilled after the assinment
2199 pass. */
2200 bitmap_head lra_subreg_reload_pseudos;
2202 /* First UID of insns generated before a new spill pass. */
2203 int lra_constraint_new_insn_uid_start;
2205 /* File used for output of LRA debug information. */
2206 FILE *lra_dump_file;
2208 /* True if we should try spill into registers of different classes
2209 instead of memory. */
2210 bool lra_reg_spill_p;
2212 /* Set up value LRA_REG_SPILL_P. */
2213 static void
2214 setup_reg_spill_flag (void)
2216 int cl, mode;
2218 if (targetm.spill_class != NULL)
2219 for (cl = 0; cl < (int) LIM_REG_CLASSES; cl++)
2220 for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
2221 if (targetm.spill_class ((enum reg_class) cl,
2222 (enum machine_mode) mode) != NO_REGS)
2224 lra_reg_spill_p = true;
2225 return;
2227 lra_reg_spill_p = false;
2230 /* True if the current function is too big to use regular algorithms
2231 in LRA. In other words, we should use simpler and faster algorithms
2232 in LRA. It also means we should not worry about generation code
2233 for caller saves. The value is set up in IRA. */
2234 bool lra_simple_p;
2236 /* Major LRA entry function. F is a file should be used to dump LRA
2237 debug info. */
2238 void
2239 lra (FILE *f)
2241 int i;
2242 bool live_p, scratch_p, inserted_p;
2244 lra_dump_file = f;
2246 timevar_push (TV_LRA);
2248 /* Make sure that the last insn is a note. Some subsequent passes
2249 need it. */
2250 emit_note (NOTE_INSN_DELETED);
2252 COPY_HARD_REG_SET (lra_no_alloc_regs, ira_no_alloc_regs);
2254 init_reg_info ();
2255 expand_reg_info ();
2257 init_insn_recog_data ();
2259 #ifdef ENABLE_CHECKING
2260 check_rtl (false);
2261 #endif
2263 lra_live_range_iter = lra_coalesce_iter = 0;
2264 lra_constraint_iter = lra_constraint_iter_after_spill = 0;
2265 lra_inheritance_iter = lra_undo_inheritance_iter = 0;
2267 setup_reg_spill_flag ();
2269 /* We can not set up reload_in_progress because it prevents new
2270 pseudo creation. */
2271 lra_in_progress = 1;
2273 /* Function remove_scratches can creates new pseudos for clobbers --
2274 so set up lra_constraint_new_regno_start before its call to
2275 permit changing reg classes for pseudos created by this
2276 simplification. */
2277 lra_constraint_new_regno_start = lra_new_regno_start = max_reg_num ();
2278 remove_scratches ();
2279 scratch_p = lra_constraint_new_regno_start != max_reg_num ();
2281 /* A function that has a non-local label that can reach the exit
2282 block via non-exceptional paths must save all call-saved
2283 registers. */
2284 if (cfun->has_nonlocal_label && has_nonexceptional_receiver ())
2285 crtl->saves_all_registers = 1;
2287 if (crtl->saves_all_registers)
2288 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2289 if (! call_used_regs[i] && ! fixed_regs[i] && ! LOCAL_REGNO (i))
2290 df_set_regs_ever_live (i, true);
2292 /* We don't DF from now and avoid its using because it is to
2293 expensive when a lot of RTL changes are made. */
2294 df_set_flags (DF_NO_INSN_RESCAN);
2295 lra_constraint_insn_stack.create (get_max_uid ());
2296 lra_constraint_insn_stack_bitmap = sbitmap_alloc (get_max_uid ());
2297 bitmap_clear (lra_constraint_insn_stack_bitmap);
2298 lra_live_ranges_init ();
2299 lra_constraints_init ();
2300 lra_curr_reload_num = 0;
2301 push_insns (get_last_insn (), NULL_RTX);
2302 /* It is needed for the 1st coalescing. */
2303 lra_constraint_new_insn_uid_start = get_max_uid ();
2304 bitmap_initialize (&lra_inheritance_pseudos, &reg_obstack);
2305 bitmap_initialize (&lra_split_regs, &reg_obstack);
2306 bitmap_initialize (&lra_optional_reload_pseudos, &reg_obstack);
2307 bitmap_initialize (&lra_subreg_reload_pseudos, &reg_obstack);
2308 live_p = false;
2309 if (get_frame_size () != 0 && crtl->stack_alignment_needed)
2310 /* If we have a stack frame, we must align it now. The stack size
2311 may be a part of the offset computation for register
2312 elimination. */
2313 assign_stack_local (BLKmode, 0, crtl->stack_alignment_needed);
2314 for (;;)
2316 for (;;)
2318 /* We should try to assign hard registers to scratches even
2319 if there were no RTL transformations in
2320 lra_constraints. */
2321 if (! lra_constraints (lra_constraint_iter == 0)
2322 && (lra_constraint_iter > 1
2323 || (! scratch_p && ! caller_save_needed)))
2324 break;
2325 /* Constraint transformations may result in that eliminable
2326 hard regs become uneliminable and pseudos which use them
2327 should be spilled. It is better to do it before pseudo
2328 assignments.
2330 For example, rs6000 can make
2331 RS6000_PIC_OFFSET_TABLE_REGNUM uneliminable if we started
2332 to use a constant pool. */
2333 lra_eliminate (false);
2334 /* Do inheritance only for regular algorithms. */
2335 if (! lra_simple_p)
2336 lra_inheritance ();
2337 if (live_p)
2338 lra_clear_live_ranges ();
2339 /* We need live ranges for lra_assign -- so build them. */
2340 lra_create_live_ranges (true);
2341 live_p = true;
2342 /* If we don't spill non-reload and non-inheritance pseudos,
2343 there is no sense to run memory-memory move coalescing.
2344 If inheritance pseudos were spilled, the memory-memory
2345 moves involving them will be removed by pass undoing
2346 inheritance. */
2347 if (lra_simple_p)
2348 lra_assign ();
2349 else
2351 bool spill_p = !lra_assign ();
2353 if (lra_undo_inheritance ())
2354 live_p = false;
2355 if (spill_p)
2357 if (! live_p)
2359 lra_create_live_ranges (true);
2360 live_p = true;
2362 if (lra_coalesce ())
2363 live_p = false;
2365 if (! live_p)
2366 lra_clear_live_ranges ();
2369 /* Don't clear optional reloads bitmap until all constraints are
2370 satisfied as we need to differ them from regular reloads. */
2371 bitmap_clear (&lra_optional_reload_pseudos);
2372 bitmap_clear (&lra_subreg_reload_pseudos);
2373 bitmap_clear (&lra_inheritance_pseudos);
2374 bitmap_clear (&lra_split_regs);
2375 if (! lra_need_for_spills_p ())
2376 break;
2377 if (! live_p)
2379 /* We need full live info for spilling pseudos into
2380 registers instead of memory. */
2381 lra_create_live_ranges (lra_reg_spill_p);
2382 live_p = true;
2384 lra_spill ();
2385 /* Assignment of stack slots changes elimination offsets for
2386 some eliminations. So update the offsets here. */
2387 lra_eliminate (false);
2388 lra_constraint_new_regno_start = max_reg_num ();
2389 lra_constraint_new_insn_uid_start = get_max_uid ();
2390 lra_constraint_iter_after_spill = 0;
2392 restore_scratches ();
2393 lra_eliminate (true);
2394 lra_final_code_change ();
2395 lra_in_progress = 0;
2396 if (live_p)
2397 lra_clear_live_ranges ();
2398 lra_live_ranges_finish ();
2399 lra_constraints_finish ();
2400 finish_reg_info ();
2401 sbitmap_free (lra_constraint_insn_stack_bitmap);
2402 lra_constraint_insn_stack.release ();
2403 finish_insn_recog_data ();
2404 regstat_free_n_sets_and_refs ();
2405 regstat_free_ri ();
2406 reload_completed = 1;
2407 update_inc_notes ();
2409 inserted_p = fixup_abnormal_edges ();
2411 /* We've possibly turned single trapping insn into multiple ones. */
2412 if (cfun->can_throw_non_call_exceptions)
2414 sbitmap blocks;
2415 blocks = sbitmap_alloc (last_basic_block);
2416 bitmap_ones (blocks);
2417 find_many_sub_basic_blocks (blocks);
2418 sbitmap_free (blocks);
2421 if (inserted_p)
2422 commit_edge_insertions ();
2424 /* Replacing pseudos with their memory equivalents might have
2425 created shared rtx. Subsequent passes would get confused
2426 by this, so unshare everything here. */
2427 unshare_all_rtl_again (get_insns ());
2429 #ifdef ENABLE_CHECKING
2430 check_rtl (true);
2431 #endif
2433 timevar_pop (TV_LRA);
2436 /* Called once per compiler to initialize LRA data once. */
2437 void
2438 lra_init_once (void)
2440 init_insn_code_data_once ();
2443 /* Initialize LRA whenever register-related information is changed. */
2444 void
2445 lra_init (void)
2447 init_op_alt_data ();
2450 /* Called once per compiler to finish LRA data which are initialize
2451 once. */
2452 void
2453 lra_finish_once (void)
2455 finish_insn_code_data_once ();