fixup_args_size_notes takes a pair of rtx_insn
[official-gcc.git] / gcc / lra.c
blob911bcb046cea037a2a794bb83dc8fefdf68b04ec
1 /* LRA (local register allocator) driver and LRA utilities.
2 Copyright (C) 2010-2014 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_insn *);
129 static void invalidate_insn_data_regno_info (lra_insn_recog_data_t,
130 rtx_insn *, int);
132 /* Expand all regno related info needed for LRA. */
133 static void
134 expand_reg_data (int old)
136 resize_reg_info ();
137 expand_reg_info ();
138 ira_expand_reg_equiv ();
139 for (int i = (int) max_reg_num () - 1; i >= old; i--)
140 lra_change_class (i, ALL_REGS, " Set", true);
143 /* Create and return a new reg of ORIGINAL mode. If ORIGINAL is NULL
144 or of VOIDmode, use MD_MODE for the new reg. Initialize its
145 register class to RCLASS. Print message about assigning class
146 RCLASS containing new register name TITLE unless it is NULL. Use
147 attributes of ORIGINAL if it is a register. The created register
148 will have unique held value. */
150 lra_create_new_reg_with_unique_value (enum machine_mode md_mode, rtx original,
151 enum reg_class rclass, const char *title)
153 enum machine_mode mode;
154 rtx new_reg;
156 if (original == NULL_RTX || (mode = GET_MODE (original)) == VOIDmode)
157 mode = md_mode;
158 lra_assert (mode != VOIDmode);
159 new_reg = gen_reg_rtx (mode);
160 if (original == NULL_RTX || ! REG_P (original))
162 if (lra_dump_file != NULL)
163 fprintf (lra_dump_file, " Creating newreg=%i", REGNO (new_reg));
165 else
167 if (ORIGINAL_REGNO (original) >= FIRST_PSEUDO_REGISTER)
168 ORIGINAL_REGNO (new_reg) = ORIGINAL_REGNO (original);
169 REG_USERVAR_P (new_reg) = REG_USERVAR_P (original);
170 REG_POINTER (new_reg) = REG_POINTER (original);
171 REG_ATTRS (new_reg) = REG_ATTRS (original);
172 if (lra_dump_file != NULL)
173 fprintf (lra_dump_file, " Creating newreg=%i from oldreg=%i",
174 REGNO (new_reg), REGNO (original));
176 if (lra_dump_file != NULL)
178 if (title != NULL)
179 fprintf (lra_dump_file, ", assigning class %s to%s%s r%d",
180 reg_class_names[rclass], *title == '\0' ? "" : " ",
181 title, REGNO (new_reg));
182 fprintf (lra_dump_file, "\n");
184 expand_reg_data (max_reg_num ());
185 setup_reg_classes (REGNO (new_reg), rclass, NO_REGS, rclass);
186 return new_reg;
189 /* Analogous to the previous function but also inherits value of
190 ORIGINAL. */
192 lra_create_new_reg (enum machine_mode md_mode, rtx original,
193 enum reg_class rclass, const char *title)
195 rtx new_reg;
197 new_reg
198 = lra_create_new_reg_with_unique_value (md_mode, original, rclass, title);
199 if (original != NULL_RTX && REG_P (original))
200 lra_assign_reg_val (REGNO (original), REGNO (new_reg));
201 return new_reg;
204 /* Set up for REGNO unique hold value. */
205 void
206 lra_set_regno_unique_value (int regno)
208 lra_reg_info[regno].val = get_new_reg_value ();
211 /* Invalidate INSN related info used by LRA. The info should never be
212 used after that. */
213 void
214 lra_invalidate_insn_data (rtx_insn *insn)
216 lra_invalidate_insn_regno_info (insn);
217 invalidate_insn_recog_data (INSN_UID (insn));
220 /* Mark INSN deleted and invalidate the insn related info used by
221 LRA. */
222 void
223 lra_set_insn_deleted (rtx_insn *insn)
225 lra_invalidate_insn_data (insn);
226 SET_INSN_DELETED (insn);
229 /* Delete an unneeded INSN and any previous insns who sole purpose is
230 loading data that is dead in INSN. */
231 void
232 lra_delete_dead_insn (rtx_insn *insn)
234 rtx_insn *prev = prev_real_insn (insn);
235 rtx prev_dest;
237 /* If the previous insn sets a register that dies in our insn,
238 delete it too. */
239 if (prev && GET_CODE (PATTERN (prev)) == SET
240 && (prev_dest = SET_DEST (PATTERN (prev)), REG_P (prev_dest))
241 && reg_mentioned_p (prev_dest, PATTERN (insn))
242 && find_regno_note (insn, REG_DEAD, REGNO (prev_dest))
243 && ! side_effects_p (SET_SRC (PATTERN (prev))))
244 lra_delete_dead_insn (prev);
246 lra_set_insn_deleted (insn);
249 /* Emit insn x = y + z. Return NULL if we failed to do it.
250 Otherwise, return the insn. We don't use gen_add3_insn as it might
251 clobber CC. */
252 static rtx
253 emit_add3_insn (rtx x, rtx y, rtx z)
255 rtx_insn *last;
256 rtx insn;
258 last = get_last_insn ();
260 if (have_addptr3_insn (x, y, z))
262 insn = gen_addptr3_insn (x, y, z);
264 /* If the target provides an "addptr" pattern it hopefully does
265 for a reason. So falling back to the normal add would be
266 a bug. */
267 lra_assert (insn != NULL_RTX);
268 emit_insn (insn);
269 return insn;
272 insn = emit_insn (gen_rtx_SET (VOIDmode, x,
273 gen_rtx_PLUS (GET_MODE (y), y, z)));
274 if (recog_memoized (insn) < 0)
276 delete_insns_since (last);
277 insn = NULL_RTX;
279 return insn;
282 /* Emit insn x = x + y. Return the insn. We use gen_add2_insn as the
283 last resort. */
284 static rtx
285 emit_add2_insn (rtx x, rtx y)
287 rtx insn;
289 insn = emit_add3_insn (x, x, y);
290 if (insn == NULL_RTX)
292 insn = gen_add2_insn (x, y);
293 if (insn != NULL_RTX)
294 emit_insn (insn);
296 return insn;
299 /* Target checks operands through operand predicates to recognize an
300 insn. We should have a special precaution to generate add insns
301 which are frequent results of elimination.
303 Emit insns for x = y + z. X can be used to store intermediate
304 values and should be not in Y and Z when we use X to store an
305 intermediate value. Y + Z should form [base] [+ index[ * scale]] [
306 + disp] where base and index are registers, disp and scale are
307 constants. Y should contain base if it is present, Z should
308 contain disp if any. index[*scale] can be part of Y or Z. */
309 void
310 lra_emit_add (rtx x, rtx y, rtx z)
312 int old;
313 rtx insn;
314 rtx_insn *last;
315 rtx a1, a2, base, index, disp, scale, index_scale;
316 bool ok_p;
318 insn = emit_add3_insn (x, y, z);
319 old = max_reg_num ();
320 if (insn != NULL_RTX)
322 else
324 disp = a2 = NULL_RTX;
325 if (GET_CODE (y) == PLUS)
327 a1 = XEXP (y, 0);
328 a2 = XEXP (y, 1);
329 disp = z;
331 else
333 a1 = y;
334 if (CONSTANT_P (z))
335 disp = z;
336 else
337 a2 = z;
339 index_scale = scale = NULL_RTX;
340 if (GET_CODE (a1) == MULT)
342 index_scale = a1;
343 index = XEXP (a1, 0);
344 scale = XEXP (a1, 1);
345 base = a2;
347 else if (a2 != NULL_RTX && GET_CODE (a2) == MULT)
349 index_scale = a2;
350 index = XEXP (a2, 0);
351 scale = XEXP (a2, 1);
352 base = a1;
354 else
356 base = a1;
357 index = a2;
359 if (! (REG_P (base) || GET_CODE (base) == SUBREG)
360 || (index != NULL_RTX
361 && ! (REG_P (index) || GET_CODE (index) == SUBREG))
362 || (disp != NULL_RTX && ! CONSTANT_P (disp))
363 || (scale != NULL_RTX && ! CONSTANT_P (scale)))
365 /* Probably we have no 3 op add. Last chance is to use 2-op
366 add insn. To succeed, don't move Z to X as an address
367 segment always comes in Y. Otherwise, we might fail when
368 adding the address segment to register. */
369 lra_assert (x != y && x != z);
370 emit_move_insn (x, y);
371 insn = emit_add2_insn (x, z);
372 lra_assert (insn != NULL_RTX);
374 else
376 if (index_scale == NULL_RTX)
377 index_scale = index;
378 if (disp == NULL_RTX)
380 /* Generate x = index_scale; x = x + base. */
381 lra_assert (index_scale != NULL_RTX && base != NULL_RTX);
382 emit_move_insn (x, index_scale);
383 insn = emit_add2_insn (x, base);
384 lra_assert (insn != NULL_RTX);
386 else if (scale == NULL_RTX)
388 /* Try x = base + disp. */
389 lra_assert (base != NULL_RTX);
390 last = get_last_insn ();
391 insn = emit_move_insn (x, gen_rtx_PLUS (GET_MODE (base),
392 base, disp));
393 if (recog_memoized (insn) < 0)
395 delete_insns_since (last);
396 /* Generate x = disp; x = x + base. */
397 emit_move_insn (x, disp);
398 insn = emit_add2_insn (x, base);
399 lra_assert (insn != NULL_RTX);
401 /* Generate x = x + index. */
402 if (index != NULL_RTX)
404 insn = emit_add2_insn (x, index);
405 lra_assert (insn != NULL_RTX);
408 else
410 /* Try x = index_scale; x = x + disp; x = x + base. */
411 last = get_last_insn ();
412 insn = emit_move_insn (x, index_scale);
413 ok_p = false;
414 if (recog_memoized (insn) >= 0)
416 insn = emit_add2_insn (x, disp);
417 if (insn != NULL_RTX)
419 insn = emit_add2_insn (x, disp);
420 if (insn != NULL_RTX)
421 ok_p = true;
424 if (! ok_p)
426 delete_insns_since (last);
427 /* Generate x = disp; x = x + base; x = x + index_scale. */
428 emit_move_insn (x, disp);
429 insn = emit_add2_insn (x, base);
430 lra_assert (insn != NULL_RTX);
431 insn = emit_add2_insn (x, index_scale);
432 lra_assert (insn != NULL_RTX);
437 /* Functions emit_... can create pseudos -- so expand the pseudo
438 data. */
439 if (old != max_reg_num ())
440 expand_reg_data (old);
443 /* The number of emitted reload insns so far. */
444 int lra_curr_reload_num;
446 /* Emit x := y, processing special case when y = u + v or y = u + v *
447 scale + w through emit_add (Y can be an address which is base +
448 index reg * scale + displacement in general case). X may be used
449 as intermediate result therefore it should be not in Y. */
450 void
451 lra_emit_move (rtx x, rtx y)
453 int old;
455 if (GET_CODE (y) != PLUS)
457 if (rtx_equal_p (x, y))
458 return;
459 old = max_reg_num ();
460 emit_move_insn (x, y);
461 if (REG_P (x))
462 lra_reg_info[ORIGINAL_REGNO (x)].last_reload = ++lra_curr_reload_num;
463 /* Function emit_move can create pseudos -- so expand the pseudo
464 data. */
465 if (old != max_reg_num ())
466 expand_reg_data (old);
467 return;
469 lra_emit_add (x, XEXP (y, 0), XEXP (y, 1));
472 /* Update insn operands which are duplication of operands whose
473 numbers are in array of NOPS (with end marker -1). The insn is
474 represented by its LRA internal representation ID. */
475 void
476 lra_update_dups (lra_insn_recog_data_t id, signed char *nops)
478 int i, j, nop;
479 struct lra_static_insn_data *static_id = id->insn_static_data;
481 for (i = 0; i < static_id->n_dups; i++)
482 for (j = 0; (nop = nops[j]) >= 0; j++)
483 if (static_id->dup_num[i] == nop)
484 *id->dup_loc[i] = *id->operand_loc[nop];
489 /* This page contains code dealing with info about registers in the
490 insns. */
492 /* Pools for insn reg info. */
493 static alloc_pool insn_reg_pool;
495 /* Initiate pool for insn reg info. */
496 static void
497 init_insn_regs (void)
499 insn_reg_pool
500 = create_alloc_pool ("insn regs", sizeof (struct lra_insn_reg), 100);
503 /* Create LRA insn related info about a reference to REGNO in INSN with
504 TYPE (in/out/inout), biggest reference mode MODE, flag that it is
505 reference through subreg (SUBREG_P), flag that is early clobbered
506 in the insn (EARLY_CLOBBER), and reference to the next insn reg
507 info (NEXT). */
508 static struct lra_insn_reg *
509 new_insn_reg (rtx_insn *insn, int regno, enum op_type type,
510 enum machine_mode mode,
511 bool subreg_p, bool early_clobber, struct lra_insn_reg *next)
513 struct lra_insn_reg *ir;
515 ir = (struct lra_insn_reg *) pool_alloc (insn_reg_pool);
516 ir->type = type;
517 ir->biggest_mode = mode;
518 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (lra_reg_info[regno].biggest_mode)
519 && NONDEBUG_INSN_P (insn))
520 lra_reg_info[regno].biggest_mode = mode;
521 ir->subreg_p = subreg_p;
522 ir->early_clobber = early_clobber;
523 ir->regno = regno;
524 ir->next = next;
525 return ir;
528 /* Free insn reg info IR. */
529 static void
530 free_insn_reg (struct lra_insn_reg *ir)
532 pool_free (insn_reg_pool, ir);
535 /* Free insn reg info list IR. */
536 static void
537 free_insn_regs (struct lra_insn_reg *ir)
539 struct lra_insn_reg *next_ir;
541 for (; ir != NULL; ir = next_ir)
543 next_ir = ir->next;
544 free_insn_reg (ir);
548 /* Finish pool for insn reg info. */
549 static void
550 finish_insn_regs (void)
552 free_alloc_pool (insn_reg_pool);
557 /* This page contains code dealing LRA insn info (or in other words
558 LRA internal insn representation). */
560 /* Map INSN_CODE -> the static insn data. This info is valid during
561 all translation unit. */
562 struct lra_static_insn_data *insn_code_data[LAST_INSN_CODE];
564 /* Debug insns are represented as a special insn with one input
565 operand which is RTL expression in var_location. */
567 /* The following data are used as static insn operand data for all
568 debug insns. If structure lra_operand_data is changed, the
569 initializer should be changed too. */
570 static struct lra_operand_data debug_operand_data =
572 NULL, /* alternative */
573 VOIDmode, /* We are not interesting in the operand mode. */
574 OP_IN,
575 0, 0, 0, 0
578 /* The following data are used as static insn data for all debug
579 insns. If structure lra_static_insn_data is changed, the
580 initializer should be changed too. */
581 static struct lra_static_insn_data debug_insn_static_data =
583 &debug_operand_data,
584 0, /* Duplication operands #. */
585 -1, /* Commutative operand #. */
586 1, /* Operands #. There is only one operand which is debug RTL
587 expression. */
588 0, /* Duplications #. */
589 0, /* Alternatives #. We are not interesting in alternatives
590 because we does not proceed debug_insns for reloads. */
591 NULL, /* Hard registers referenced in machine description. */
592 NULL /* Descriptions of operands in alternatives. */
595 /* Called once per compiler work to initialize some LRA data related
596 to insns. */
597 static void
598 init_insn_code_data_once (void)
600 memset (insn_code_data, 0, sizeof (insn_code_data));
603 /* Called once per compiler work to finalize some LRA data related to
604 insns. */
605 static void
606 finish_insn_code_data_once (void)
608 int i;
610 for (i = 0; i < LAST_INSN_CODE; i++)
612 if (insn_code_data[i] != NULL)
613 free (insn_code_data[i]);
617 /* Return static insn data, allocate and setup if necessary. Although
618 dup_num is static data (it depends only on icode), to set it up we
619 need to extract insn first. So recog_data should be valid for
620 normal insn (ICODE >= 0) before the call. */
621 static struct lra_static_insn_data *
622 get_static_insn_data (int icode, int nop, int ndup, int nalt)
624 struct lra_static_insn_data *data;
625 size_t n_bytes;
627 lra_assert (icode < LAST_INSN_CODE);
628 if (icode >= 0 && (data = insn_code_data[icode]) != NULL)
629 return data;
630 lra_assert (nop >= 0 && ndup >= 0 && nalt >= 0);
631 n_bytes = sizeof (struct lra_static_insn_data)
632 + sizeof (struct lra_operand_data) * nop
633 + sizeof (int) * ndup;
634 data = XNEWVAR (struct lra_static_insn_data, n_bytes);
635 data->operand_alternative = NULL;
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 (data->icode < 0 && NONDEBUG_INSN_P (data->insn))
712 if (data->insn_static_data->operand_alternative != NULL)
713 free (const_cast <operand_alternative *>
714 (data->insn_static_data->operand_alternative));
715 free_insn_regs (data->insn_static_data->hard_regs);
716 free (data->insn_static_data);
718 free_insn_regs (data->regs);
719 data->regs = NULL;
720 free (data);
723 /* Finish LRA data about all insns. */
724 static void
725 finish_insn_recog_data (void)
727 int i;
728 lra_insn_recog_data_t data;
730 for (i = 0; i < lra_insn_recog_data_len; i++)
731 if ((data = lra_insn_recog_data[i]) != NULL)
732 free_insn_recog_data (data);
733 finish_insn_regs ();
734 free (lra_insn_recog_data);
737 /* Setup info about operands in alternatives of LRA DATA of insn. */
738 static void
739 setup_operand_alternative (lra_insn_recog_data_t data,
740 const operand_alternative *op_alt)
742 int i, j, nop, nalt;
743 int icode = data->icode;
744 struct lra_static_insn_data *static_data = data->insn_static_data;
746 static_data->commutative = -1;
747 nop = static_data->n_operands;
748 nalt = static_data->n_alternatives;
749 static_data->operand_alternative = op_alt;
750 for (i = 0; i < nop; i++)
752 static_data->operand[i].early_clobber = false;
753 static_data->operand[i].is_address = false;
754 if (static_data->operand[i].constraint[0] == '%')
756 /* We currently only support one commutative pair of operands. */
757 if (static_data->commutative < 0)
758 static_data->commutative = i;
759 else
760 lra_assert (icode < 0); /* Asm */
761 /* The last operand should not be marked commutative. */
762 lra_assert (i != nop - 1);
765 for (j = 0; j < nalt; j++)
766 for (i = 0; i < nop; i++, op_alt++)
768 static_data->operand[i].early_clobber |= op_alt->earlyclobber;
769 static_data->operand[i].is_address |= op_alt->is_address;
773 /* Recursively process X and collect info about registers, which are
774 not the insn operands, in X with TYPE (in/out/inout) and flag that
775 it is early clobbered in the insn (EARLY_CLOBBER) and add the info
776 to LIST. X is a part of insn given by DATA. Return the result
777 list. */
778 static struct lra_insn_reg *
779 collect_non_operand_hard_regs (rtx *x, lra_insn_recog_data_t data,
780 struct lra_insn_reg *list,
781 enum op_type type, bool early_clobber)
783 int i, j, regno, last;
784 bool subreg_p;
785 enum machine_mode mode;
786 struct lra_insn_reg *curr;
787 rtx op = *x;
788 enum rtx_code code = GET_CODE (op);
789 const char *fmt = GET_RTX_FORMAT (code);
791 for (i = 0; i < data->insn_static_data->n_operands; i++)
792 if (x == data->operand_loc[i])
793 /* It is an operand loc. Stop here. */
794 return list;
795 for (i = 0; i < data->insn_static_data->n_dups; i++)
796 if (x == data->dup_loc[i])
797 /* It is a dup loc. Stop here. */
798 return list;
799 mode = GET_MODE (op);
800 subreg_p = false;
801 if (code == SUBREG)
803 op = SUBREG_REG (op);
804 code = GET_CODE (op);
805 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (op)))
807 mode = GET_MODE (op);
808 if (GET_MODE_SIZE (mode) > REGMODE_NATURAL_SIZE (mode))
809 subreg_p = true;
812 if (REG_P (op))
814 if ((regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER)
815 return list;
816 for (last = regno + hard_regno_nregs[regno][mode];
817 regno < last;
818 regno++)
819 if (! TEST_HARD_REG_BIT (lra_no_alloc_regs, regno)
820 || TEST_HARD_REG_BIT (eliminable_regset, regno))
822 for (curr = list; curr != NULL; curr = curr->next)
823 if (curr->regno == regno && curr->subreg_p == subreg_p
824 && curr->biggest_mode == mode)
826 if (curr->type != type)
827 curr->type = OP_INOUT;
828 if (curr->early_clobber != early_clobber)
829 curr->early_clobber = true;
830 break;
832 if (curr == NULL)
834 /* This is a new hard regno or the info can not be
835 integrated into the found structure. */
836 #ifdef STACK_REGS
837 early_clobber
838 = (early_clobber
839 /* This clobber is to inform popping floating
840 point stack only. */
841 && ! (FIRST_STACK_REG <= regno
842 && regno <= LAST_STACK_REG));
843 #endif
844 list = new_insn_reg (data->insn, regno, type, mode, subreg_p,
845 early_clobber, list);
848 return list;
850 switch (code)
852 case SET:
853 list = collect_non_operand_hard_regs (&SET_DEST (op), data,
854 list, OP_OUT, false);
855 list = collect_non_operand_hard_regs (&SET_SRC (op), data,
856 list, OP_IN, false);
857 break;
858 case CLOBBER:
859 /* We treat clobber of non-operand hard registers as early
860 clobber (the behavior is expected from asm). */
861 list = collect_non_operand_hard_regs (&XEXP (op, 0), data,
862 list, OP_OUT, true);
863 break;
864 case PRE_INC: case PRE_DEC: case POST_INC: case POST_DEC:
865 list = collect_non_operand_hard_regs (&XEXP (op, 0), data,
866 list, OP_INOUT, false);
867 break;
868 case PRE_MODIFY: case POST_MODIFY:
869 list = collect_non_operand_hard_regs (&XEXP (op, 0), data,
870 list, OP_INOUT, false);
871 list = collect_non_operand_hard_regs (&XEXP (op, 1), data,
872 list, OP_IN, false);
873 break;
874 default:
875 fmt = GET_RTX_FORMAT (code);
876 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
878 if (fmt[i] == 'e')
879 list = collect_non_operand_hard_regs (&XEXP (op, i), data,
880 list, OP_IN, false);
881 else if (fmt[i] == 'E')
882 for (j = XVECLEN (op, i) - 1; j >= 0; j--)
883 list = collect_non_operand_hard_regs (&XVECEXP (op, i, j), data,
884 list, OP_IN, false);
887 return list;
890 /* Set up and return info about INSN. Set up the info if it is not set up
891 yet. */
892 lra_insn_recog_data_t
893 lra_set_insn_recog_data (rtx_insn *insn)
895 lra_insn_recog_data_t data;
896 int i, n, icode;
897 rtx **locs;
898 unsigned int uid = INSN_UID (insn);
899 struct lra_static_insn_data *insn_static_data;
901 check_and_expand_insn_recog_data (uid);
902 if (DEBUG_INSN_P (insn))
903 icode = -1;
904 else
906 icode = INSN_CODE (insn);
907 if (icode < 0)
908 /* It might be a new simple insn which is not recognized yet. */
909 INSN_CODE (insn) = icode = recog_memoized (insn);
911 data = XNEW (struct lra_insn_recog_data);
912 lra_insn_recog_data[uid] = data;
913 data->insn = insn;
914 data->used_insn_alternative = -1;
915 data->icode = icode;
916 data->regs = NULL;
917 if (DEBUG_INSN_P (insn))
919 data->insn_static_data = &debug_insn_static_data;
920 data->dup_loc = NULL;
921 data->arg_hard_regs = NULL;
922 data->enabled_alternatives = ALL_ALTERNATIVES;
923 data->operand_loc = XNEWVEC (rtx *, 1);
924 data->operand_loc[0] = &INSN_VAR_LOCATION_LOC (insn);
925 return data;
927 if (icode < 0)
929 int nop, nalt;
930 enum machine_mode operand_mode[MAX_RECOG_OPERANDS];
931 const char *constraints[MAX_RECOG_OPERANDS];
933 nop = asm_noperands (PATTERN (insn));
934 data->operand_loc = data->dup_loc = NULL;
935 nalt = 1;
936 if (nop < 0)
938 /* Its is a special insn like USE or CLOBBER. We should
939 recognize any regular insn otherwise LRA can do nothing
940 with this insn. */
941 gcc_assert (GET_CODE (PATTERN (insn)) == USE
942 || GET_CODE (PATTERN (insn)) == CLOBBER
943 || GET_CODE (PATTERN (insn)) == ASM_INPUT);
944 data->insn_static_data = insn_static_data
945 = get_static_insn_data (-1, 0, 0, nalt);
947 else
949 /* expand_asm_operands makes sure there aren't too many
950 operands. */
951 lra_assert (nop <= MAX_RECOG_OPERANDS);
952 if (nop != 0)
953 data->operand_loc = XNEWVEC (rtx *, nop);
954 /* Now get the operand values and constraints out of the
955 insn. */
956 decode_asm_operands (PATTERN (insn), NULL,
957 data->operand_loc,
958 constraints, operand_mode, NULL);
959 if (nop > 0)
961 const char *p = recog_data.constraints[0];
963 for (p = constraints[0]; *p; p++)
964 nalt += *p == ',';
966 data->insn_static_data = insn_static_data
967 = get_static_insn_data (-1, nop, 0, nalt);
968 for (i = 0; i < nop; i++)
970 insn_static_data->operand[i].mode = operand_mode[i];
971 insn_static_data->operand[i].constraint = constraints[i];
972 insn_static_data->operand[i].strict_low = false;
973 insn_static_data->operand[i].is_operator = false;
974 insn_static_data->operand[i].is_address = false;
977 for (i = 0; i < insn_static_data->n_operands; i++)
978 insn_static_data->operand[i].type
979 = (insn_static_data->operand[i].constraint[0] == '=' ? OP_OUT
980 : insn_static_data->operand[i].constraint[0] == '+' ? OP_INOUT
981 : OP_IN);
982 data->enabled_alternatives = ALL_ALTERNATIVES;
983 if (nop > 0)
985 operand_alternative *op_alt = XCNEWVEC (operand_alternative,
986 nalt * nop);
987 preprocess_constraints (nop, nalt, constraints, op_alt);
988 setup_operand_alternative (data, op_alt);
991 else
993 insn_extract (insn);
994 data->insn_static_data = insn_static_data
995 = get_static_insn_data (icode, insn_data[icode].n_operands,
996 insn_data[icode].n_dups,
997 insn_data[icode].n_alternatives);
998 n = insn_static_data->n_operands;
999 if (n == 0)
1000 locs = NULL;
1001 else
1003 locs = XNEWVEC (rtx *, n);
1004 memcpy (locs, recog_data.operand_loc, n * sizeof (rtx *));
1006 data->operand_loc = locs;
1007 n = insn_static_data->n_dups;
1008 if (n == 0)
1009 locs = NULL;
1010 else
1012 locs = XNEWVEC (rtx *, n);
1013 memcpy (locs, recog_data.dup_loc, n * sizeof (rtx *));
1015 data->dup_loc = locs;
1016 data->enabled_alternatives = get_enabled_alternatives (insn);
1017 const operand_alternative *op_alt = preprocess_insn_constraints (icode);
1018 if (!insn_static_data->operand_alternative)
1019 setup_operand_alternative (data, op_alt);
1020 else if (op_alt != insn_static_data->operand_alternative)
1021 insn_static_data->operand_alternative = op_alt;
1023 if (GET_CODE (PATTERN (insn)) == CLOBBER || GET_CODE (PATTERN (insn)) == USE)
1024 insn_static_data->hard_regs = NULL;
1025 else
1026 insn_static_data->hard_regs
1027 = collect_non_operand_hard_regs (&PATTERN (insn), data,
1028 NULL, OP_IN, false);
1029 data->arg_hard_regs = NULL;
1030 if (CALL_P (insn))
1032 rtx link;
1033 int n_hard_regs, regno, arg_hard_regs[FIRST_PSEUDO_REGISTER];
1035 n_hard_regs = 0;
1036 /* Finding implicit hard register usage. We believe it will be
1037 not changed whatever transformations are used. Call insns
1038 are such example. */
1039 for (link = CALL_INSN_FUNCTION_USAGE (insn);
1040 link != NULL_RTX;
1041 link = XEXP (link, 1))
1042 if (GET_CODE (XEXP (link, 0)) == USE
1043 && REG_P (XEXP (XEXP (link, 0), 0)))
1045 regno = REGNO (XEXP (XEXP (link, 0), 0));
1046 lra_assert (regno < FIRST_PSEUDO_REGISTER);
1047 /* It is an argument register. */
1048 for (i = (hard_regno_nregs
1049 [regno][GET_MODE (XEXP (XEXP (link, 0), 0))]) - 1;
1050 i >= 0;
1051 i--)
1052 arg_hard_regs[n_hard_regs++] = regno + i;
1054 if (n_hard_regs != 0)
1056 arg_hard_regs[n_hard_regs++] = -1;
1057 data->arg_hard_regs = XNEWVEC (int, n_hard_regs);
1058 memcpy (data->arg_hard_regs, arg_hard_regs,
1059 sizeof (int) * n_hard_regs);
1062 /* Some output operand can be recognized only from the context not
1063 from the constraints which are empty in this case. Call insn may
1064 contain a hard register in set destination with empty constraint
1065 and extract_insn treats them as an input. */
1066 for (i = 0; i < insn_static_data->n_operands; i++)
1068 int j;
1069 rtx pat, set;
1070 struct lra_operand_data *operand = &insn_static_data->operand[i];
1072 /* ??? Should we treat 'X' the same way. It looks to me that
1073 'X' means anything and empty constraint means we do not
1074 care. */
1075 if (operand->type != OP_IN || *operand->constraint != '\0'
1076 || operand->is_operator)
1077 continue;
1078 pat = PATTERN (insn);
1079 if (GET_CODE (pat) == SET)
1081 if (data->operand_loc[i] != &SET_DEST (pat))
1082 continue;
1084 else if (GET_CODE (pat) == PARALLEL)
1086 for (j = XVECLEN (pat, 0) - 1; j >= 0; j--)
1088 set = XVECEXP (PATTERN (insn), 0, j);
1089 if (GET_CODE (set) == SET
1090 && &SET_DEST (set) == data->operand_loc[i])
1091 break;
1093 if (j < 0)
1094 continue;
1096 else
1097 continue;
1098 operand->type = OP_OUT;
1100 return data;
1103 /* Return info about insn give by UID. The info should be already set
1104 up. */
1105 static lra_insn_recog_data_t
1106 get_insn_recog_data_by_uid (int uid)
1108 lra_insn_recog_data_t data;
1110 data = lra_insn_recog_data[uid];
1111 lra_assert (data != NULL);
1112 return data;
1115 /* Invalidate all info about insn given by its UID. */
1116 static void
1117 invalidate_insn_recog_data (int uid)
1119 lra_insn_recog_data_t data;
1121 data = lra_insn_recog_data[uid];
1122 lra_assert (data != NULL);
1123 free_insn_recog_data (data);
1124 lra_insn_recog_data[uid] = NULL;
1127 /* Update all the insn info about INSN. It is usually called when
1128 something in the insn was changed. Return the updated info. */
1129 lra_insn_recog_data_t
1130 lra_update_insn_recog_data (rtx_insn *insn)
1132 lra_insn_recog_data_t data;
1133 int n;
1134 unsigned int uid = INSN_UID (insn);
1135 struct lra_static_insn_data *insn_static_data;
1136 HOST_WIDE_INT sp_offset = 0;
1138 check_and_expand_insn_recog_data (uid);
1139 if ((data = lra_insn_recog_data[uid]) != NULL
1140 && data->icode != INSN_CODE (insn))
1142 sp_offset = data->sp_offset;
1143 invalidate_insn_data_regno_info (data, insn, get_insn_freq (insn));
1144 invalidate_insn_recog_data (uid);
1145 data = NULL;
1147 if (data == NULL)
1149 data = lra_get_insn_recog_data (insn);
1150 /* Initiate or restore SP offset. */
1151 data->sp_offset = sp_offset;
1152 return data;
1154 insn_static_data = data->insn_static_data;
1155 data->used_insn_alternative = -1;
1156 if (DEBUG_INSN_P (insn))
1157 return data;
1158 if (data->icode < 0)
1160 int nop;
1161 enum machine_mode operand_mode[MAX_RECOG_OPERANDS];
1162 const char *constraints[MAX_RECOG_OPERANDS];
1164 nop = asm_noperands (PATTERN (insn));
1165 if (nop >= 0)
1167 lra_assert (nop == data->insn_static_data->n_operands);
1168 /* Now get the operand values and constraints out of the
1169 insn. */
1170 decode_asm_operands (PATTERN (insn), NULL,
1171 data->operand_loc,
1172 constraints, operand_mode, NULL);
1173 #ifdef ENABLE_CHECKING
1175 int i;
1177 for (i = 0; i < nop; i++)
1178 lra_assert
1179 (insn_static_data->operand[i].mode == operand_mode[i]
1180 && insn_static_data->operand[i].constraint == constraints[i]
1181 && ! insn_static_data->operand[i].is_operator);
1183 #endif
1185 #ifdef ENABLE_CHECKING
1187 int i;
1189 for (i = 0; i < insn_static_data->n_operands; i++)
1190 lra_assert
1191 (insn_static_data->operand[i].type
1192 == (insn_static_data->operand[i].constraint[0] == '=' ? OP_OUT
1193 : insn_static_data->operand[i].constraint[0] == '+' ? OP_INOUT
1194 : OP_IN));
1196 #endif
1198 else
1200 insn_extract (insn);
1201 n = insn_static_data->n_operands;
1202 if (n != 0)
1203 memcpy (data->operand_loc, recog_data.operand_loc, n * sizeof (rtx *));
1204 n = insn_static_data->n_dups;
1205 if (n != 0)
1206 memcpy (data->dup_loc, recog_data.dup_loc, n * sizeof (rtx *));
1207 #if HAVE_ATTR_enabled
1208 #ifdef ENABLE_CHECKING
1210 int i;
1211 alternative_mask enabled;
1213 n = insn_static_data->n_alternatives;
1214 enabled = data->enabled_alternatives;
1215 lra_assert (n >= 0);
1216 /* Cache the insn to prevent extract_insn call from
1217 get_attr_enabled. */
1218 recog_data.insn = insn;
1219 for (i = 0; i < n; i++)
1221 which_alternative = i;
1222 lra_assert (TEST_BIT (enabled, i)
1223 == (bool) get_attr_enabled (insn));
1226 #endif
1227 #endif
1229 return data;
1232 /* Set up that INSN is using alternative ALT now. */
1233 void
1234 lra_set_used_insn_alternative (rtx_insn *insn, int alt)
1236 lra_insn_recog_data_t data;
1238 data = lra_get_insn_recog_data (insn);
1239 data->used_insn_alternative = alt;
1242 /* Set up that insn with UID is using alternative ALT now. The insn
1243 info should be already set up. */
1244 void
1245 lra_set_used_insn_alternative_by_uid (int uid, int alt)
1247 lra_insn_recog_data_t data;
1249 check_and_expand_insn_recog_data (uid);
1250 data = lra_insn_recog_data[uid];
1251 lra_assert (data != NULL);
1252 data->used_insn_alternative = alt;
1257 /* This page contains code dealing with common register info and
1258 pseudo copies. */
1260 /* The size of the following array. */
1261 static int reg_info_size;
1262 /* Common info about each register. */
1263 struct lra_reg *lra_reg_info;
1265 /* Last register value. */
1266 static int last_reg_value;
1268 /* Return new register value. */
1269 static int
1270 get_new_reg_value (void)
1272 return ++last_reg_value;
1275 /* Pools for copies. */
1276 static alloc_pool copy_pool;
1278 /* Vec referring to pseudo copies. */
1279 static vec<lra_copy_t> copy_vec;
1281 /* Initialize I-th element of lra_reg_info. */
1282 static inline void
1283 initialize_lra_reg_info_element (int i)
1285 bitmap_initialize (&lra_reg_info[i].insn_bitmap, &reg_obstack);
1286 #ifdef STACK_REGS
1287 lra_reg_info[i].no_stack_p = false;
1288 #endif
1289 CLEAR_HARD_REG_SET (lra_reg_info[i].conflict_hard_regs);
1290 CLEAR_HARD_REG_SET (lra_reg_info[i].actual_call_used_reg_set);
1291 lra_reg_info[i].preferred_hard_regno1 = -1;
1292 lra_reg_info[i].preferred_hard_regno2 = -1;
1293 lra_reg_info[i].preferred_hard_regno_profit1 = 0;
1294 lra_reg_info[i].preferred_hard_regno_profit2 = 0;
1295 lra_reg_info[i].biggest_mode = VOIDmode;
1296 lra_reg_info[i].live_ranges = NULL;
1297 lra_reg_info[i].nrefs = lra_reg_info[i].freq = 0;
1298 lra_reg_info[i].last_reload = 0;
1299 lra_reg_info[i].restore_regno = -1;
1300 lra_reg_info[i].val = get_new_reg_value ();
1301 lra_reg_info[i].offset = 0;
1302 lra_reg_info[i].copies = NULL;
1305 /* Initialize common reg info and copies. */
1306 static void
1307 init_reg_info (void)
1309 int i;
1311 last_reg_value = 0;
1312 reg_info_size = max_reg_num () * 3 / 2 + 1;
1313 lra_reg_info = XNEWVEC (struct lra_reg, reg_info_size);
1314 for (i = 0; i < reg_info_size; i++)
1315 initialize_lra_reg_info_element (i);
1316 copy_pool
1317 = create_alloc_pool ("lra copies", sizeof (struct lra_copy), 100);
1318 copy_vec.create (100);
1322 /* Finish common reg info and copies. */
1323 static void
1324 finish_reg_info (void)
1326 int i;
1328 for (i = 0; i < reg_info_size; i++)
1329 bitmap_clear (&lra_reg_info[i].insn_bitmap);
1330 free (lra_reg_info);
1331 reg_info_size = 0;
1332 free_alloc_pool (copy_pool);
1333 copy_vec.release ();
1336 /* Expand common reg info if it is necessary. */
1337 static void
1338 expand_reg_info (void)
1340 int i, old = reg_info_size;
1342 if (reg_info_size > max_reg_num ())
1343 return;
1344 reg_info_size = max_reg_num () * 3 / 2 + 1;
1345 lra_reg_info = XRESIZEVEC (struct lra_reg, lra_reg_info, reg_info_size);
1346 for (i = old; i < reg_info_size; i++)
1347 initialize_lra_reg_info_element (i);
1350 /* Free all copies. */
1351 void
1352 lra_free_copies (void)
1354 lra_copy_t cp;
1356 while (copy_vec.length () != 0)
1358 cp = copy_vec.pop ();
1359 lra_reg_info[cp->regno1].copies = lra_reg_info[cp->regno2].copies = NULL;
1360 pool_free (copy_pool, cp);
1364 /* Create copy of two pseudos REGNO1 and REGNO2. The copy execution
1365 frequency is FREQ. */
1366 void
1367 lra_create_copy (int regno1, int regno2, int freq)
1369 bool regno1_dest_p;
1370 lra_copy_t cp;
1372 lra_assert (regno1 != regno2);
1373 regno1_dest_p = true;
1374 if (regno1 > regno2)
1376 int temp = regno2;
1378 regno1_dest_p = false;
1379 regno2 = regno1;
1380 regno1 = temp;
1382 cp = (lra_copy_t) pool_alloc (copy_pool);
1383 copy_vec.safe_push (cp);
1384 cp->regno1_dest_p = regno1_dest_p;
1385 cp->freq = freq;
1386 cp->regno1 = regno1;
1387 cp->regno2 = regno2;
1388 cp->regno1_next = lra_reg_info[regno1].copies;
1389 lra_reg_info[regno1].copies = cp;
1390 cp->regno2_next = lra_reg_info[regno2].copies;
1391 lra_reg_info[regno2].copies = cp;
1392 if (lra_dump_file != NULL)
1393 fprintf (lra_dump_file, " Creating copy r%d%sr%d@%d\n",
1394 regno1, regno1_dest_p ? "<-" : "->", regno2, freq);
1397 /* Return N-th (0, 1, ...) copy. If there is no copy, return
1398 NULL. */
1399 lra_copy_t
1400 lra_get_copy (int n)
1402 if (n >= (int) copy_vec.length ())
1403 return NULL;
1404 return copy_vec[n];
1409 /* This page contains code dealing with info about registers in
1410 insns. */
1412 /* Process X of insn UID recursively and add info (operand type is
1413 given by TYPE, flag of that it is early clobber is EARLY_CLOBBER)
1414 about registers in X to the insn DATA. */
1415 static void
1416 add_regs_to_insn_regno_info (lra_insn_recog_data_t data, rtx x, int uid,
1417 enum op_type type, bool early_clobber)
1419 int i, j, regno;
1420 bool subreg_p;
1421 enum machine_mode mode;
1422 const char *fmt;
1423 enum rtx_code code;
1424 struct lra_insn_reg *curr;
1426 code = GET_CODE (x);
1427 mode = GET_MODE (x);
1428 subreg_p = false;
1429 if (GET_CODE (x) == SUBREG)
1431 x = SUBREG_REG (x);
1432 code = GET_CODE (x);
1433 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (x)))
1435 mode = GET_MODE (x);
1436 if (GET_MODE_SIZE (mode) > REGMODE_NATURAL_SIZE (mode))
1437 subreg_p = true;
1440 if (REG_P (x))
1442 regno = REGNO (x);
1443 if (regno < FIRST_PSEUDO_REGISTER
1444 && TEST_HARD_REG_BIT (lra_no_alloc_regs, regno)
1445 && ! TEST_HARD_REG_BIT (eliminable_regset, regno))
1446 return;
1447 expand_reg_info ();
1448 if (bitmap_set_bit (&lra_reg_info[regno].insn_bitmap, uid))
1450 data->regs = new_insn_reg (data->insn, regno, type, mode, subreg_p,
1451 early_clobber, data->regs);
1452 return;
1454 else
1456 for (curr = data->regs; curr != NULL; curr = curr->next)
1457 if (curr->regno == regno)
1459 if (curr->subreg_p != subreg_p || curr->biggest_mode != mode)
1460 /* The info can not be integrated into the found
1461 structure. */
1462 data->regs = new_insn_reg (data->insn, regno, type, mode,
1463 subreg_p, early_clobber,
1464 data->regs);
1465 else
1467 if (curr->type != type)
1468 curr->type = OP_INOUT;
1469 if (curr->early_clobber != early_clobber)
1470 curr->early_clobber = true;
1472 return;
1474 gcc_unreachable ();
1478 switch (code)
1480 case SET:
1481 add_regs_to_insn_regno_info (data, SET_DEST (x), uid, OP_OUT, false);
1482 add_regs_to_insn_regno_info (data, SET_SRC (x), uid, OP_IN, false);
1483 break;
1484 case CLOBBER:
1485 /* We treat clobber of non-operand hard registers as early
1486 clobber (the behavior is expected from asm). */
1487 add_regs_to_insn_regno_info (data, XEXP (x, 0), uid, OP_OUT, true);
1488 break;
1489 case PRE_INC: case PRE_DEC: case POST_INC: case POST_DEC:
1490 add_regs_to_insn_regno_info (data, XEXP (x, 0), uid, OP_INOUT, false);
1491 break;
1492 case PRE_MODIFY: case POST_MODIFY:
1493 add_regs_to_insn_regno_info (data, XEXP (x, 0), uid, OP_INOUT, false);
1494 add_regs_to_insn_regno_info (data, XEXP (x, 1), uid, OP_IN, false);
1495 break;
1496 default:
1497 if ((code != PARALLEL && code != EXPR_LIST) || type != OP_OUT)
1498 /* Some targets place small structures in registers for return
1499 values of functions, and those registers are wrapped in
1500 PARALLEL that we may see as the destination of a SET. Here
1501 is an example:
1503 (call_insn 13 12 14 2 (set (parallel:BLK [
1504 (expr_list:REG_DEP_TRUE (reg:DI 0 ax)
1505 (const_int 0 [0]))
1506 (expr_list:REG_DEP_TRUE (reg:DI 1 dx)
1507 (const_int 8 [0x8]))
1509 (call (mem:QI (symbol_ref:DI (... */
1510 type = OP_IN;
1511 fmt = GET_RTX_FORMAT (code);
1512 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1514 if (fmt[i] == 'e')
1515 add_regs_to_insn_regno_info (data, XEXP (x, i), uid, type, false);
1516 else if (fmt[i] == 'E')
1518 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1519 add_regs_to_insn_regno_info (data, XVECEXP (x, i, j), uid,
1520 type, false);
1526 /* Return execution frequency of INSN. */
1527 static int
1528 get_insn_freq (rtx_insn *insn)
1530 basic_block bb = BLOCK_FOR_INSN (insn);
1532 gcc_checking_assert (bb != NULL);
1533 return REG_FREQ_FROM_BB (bb);
1536 /* Invalidate all reg info of INSN with DATA and execution frequency
1537 FREQ. Update common info about the invalidated registers. */
1538 static void
1539 invalidate_insn_data_regno_info (lra_insn_recog_data_t data, rtx_insn *insn,
1540 int freq)
1542 int uid;
1543 bool debug_p;
1544 unsigned int i;
1545 struct lra_insn_reg *ir, *next_ir;
1547 uid = INSN_UID (insn);
1548 debug_p = DEBUG_INSN_P (insn);
1549 for (ir = data->regs; ir != NULL; ir = next_ir)
1551 i = ir->regno;
1552 next_ir = ir->next;
1553 free_insn_reg (ir);
1554 bitmap_clear_bit (&lra_reg_info[i].insn_bitmap, uid);
1555 if (i >= FIRST_PSEUDO_REGISTER && ! debug_p)
1557 lra_reg_info[i].nrefs--;
1558 lra_reg_info[i].freq -= freq;
1559 lra_assert (lra_reg_info[i].nrefs >= 0 && lra_reg_info[i].freq >= 0);
1562 data->regs = NULL;
1565 /* Invalidate all reg info of INSN. Update common info about the
1566 invalidated registers. */
1567 void
1568 lra_invalidate_insn_regno_info (rtx_insn *insn)
1570 invalidate_insn_data_regno_info (lra_get_insn_recog_data (insn), insn,
1571 get_insn_freq (insn));
1574 /* Update common reg info from reg info of insn given by its DATA and
1575 execution frequency FREQ. */
1576 static void
1577 setup_insn_reg_info (lra_insn_recog_data_t data, int freq)
1579 unsigned int i;
1580 struct lra_insn_reg *ir;
1582 for (ir = data->regs; ir != NULL; ir = ir->next)
1583 if ((i = ir->regno) >= FIRST_PSEUDO_REGISTER)
1585 lra_reg_info[i].nrefs++;
1586 lra_reg_info[i].freq += freq;
1590 /* Set up insn reg info of INSN. Update common reg info from reg info
1591 of INSN. */
1592 void
1593 lra_update_insn_regno_info (rtx_insn *insn)
1595 int i, uid, freq;
1596 lra_insn_recog_data_t data;
1597 struct lra_static_insn_data *static_data;
1598 enum rtx_code code;
1600 if (! INSN_P (insn))
1601 return;
1602 data = lra_get_insn_recog_data (insn);
1603 static_data = data->insn_static_data;
1604 freq = get_insn_freq (insn);
1605 invalidate_insn_data_regno_info (data, insn, freq);
1606 uid = INSN_UID (insn);
1607 for (i = static_data->n_operands - 1; i >= 0; i--)
1608 add_regs_to_insn_regno_info (data, *data->operand_loc[i], uid,
1609 static_data->operand[i].type,
1610 static_data->operand[i].early_clobber);
1611 if ((code = GET_CODE (PATTERN (insn))) == CLOBBER || code == USE)
1612 add_regs_to_insn_regno_info (data, XEXP (PATTERN (insn), 0), uid,
1613 code == USE ? OP_IN : OP_OUT, false);
1614 if (NONDEBUG_INSN_P (insn))
1615 setup_insn_reg_info (data, freq);
1618 /* Return reg info of insn given by it UID. */
1619 struct lra_insn_reg *
1620 lra_get_insn_regs (int uid)
1622 lra_insn_recog_data_t data;
1624 data = get_insn_recog_data_by_uid (uid);
1625 return data->regs;
1630 /* This page contains code dealing with stack of the insns which
1631 should be processed by the next constraint pass. */
1633 /* Bitmap used to put an insn on the stack only in one exemplar. */
1634 static sbitmap lra_constraint_insn_stack_bitmap;
1636 /* The stack itself. */
1637 vec<rtx_insn *> lra_constraint_insn_stack;
1639 /* Put INSN on the stack. If ALWAYS_UPDATE is true, always update the reg
1640 info for INSN, otherwise only update it if INSN is not already on the
1641 stack. */
1642 static inline void
1643 lra_push_insn_1 (rtx_insn *insn, bool always_update)
1645 unsigned int uid = INSN_UID (insn);
1646 if (always_update)
1647 lra_update_insn_regno_info (insn);
1648 if (uid >= SBITMAP_SIZE (lra_constraint_insn_stack_bitmap))
1649 lra_constraint_insn_stack_bitmap =
1650 sbitmap_resize (lra_constraint_insn_stack_bitmap, 3 * uid / 2, 0);
1651 if (bitmap_bit_p (lra_constraint_insn_stack_bitmap, uid))
1652 return;
1653 bitmap_set_bit (lra_constraint_insn_stack_bitmap, uid);
1654 if (! always_update)
1655 lra_update_insn_regno_info (insn);
1656 lra_constraint_insn_stack.safe_push (insn);
1659 /* Put INSN on the stack. */
1660 void
1661 lra_push_insn (rtx_insn *insn)
1663 lra_push_insn_1 (insn, false);
1666 /* Put INSN on the stack and update its reg info. */
1667 void
1668 lra_push_insn_and_update_insn_regno_info (rtx_insn *insn)
1670 lra_push_insn_1 (insn, true);
1673 /* Put insn with UID on the stack. */
1674 void
1675 lra_push_insn_by_uid (unsigned int uid)
1677 lra_push_insn (lra_insn_recog_data[uid]->insn);
1680 /* Take the last-inserted insns off the stack and return it. */
1681 rtx_insn *
1682 lra_pop_insn (void)
1684 rtx_insn *insn = lra_constraint_insn_stack.pop ();
1685 bitmap_clear_bit (lra_constraint_insn_stack_bitmap, INSN_UID (insn));
1686 return insn;
1689 /* Return the current size of the insn stack. */
1690 unsigned int
1691 lra_insn_stack_length (void)
1693 return lra_constraint_insn_stack.length ();
1696 /* Push insns FROM to TO (excluding it) going in reverse order. */
1697 static void
1698 push_insns (rtx_insn *from, rtx_insn *to)
1700 rtx_insn *insn;
1702 if (from == NULL_RTX)
1703 return;
1704 for (insn = from; insn != to; insn = PREV_INSN (insn))
1705 if (INSN_P (insn))
1706 lra_push_insn (insn);
1709 /* Set up sp offset for insn in range [FROM, LAST]. The offset is
1710 taken from the next BB insn after LAST or zero if there in such
1711 insn. */
1712 static void
1713 setup_sp_offset (rtx_insn *from, rtx_insn *last)
1715 rtx_insn *before = next_nonnote_insn_bb (last);
1716 HOST_WIDE_INT offset = (before == NULL_RTX || ! INSN_P (before)
1717 ? 0 : lra_get_insn_recog_data (before)->sp_offset);
1719 for (rtx_insn *insn = from; insn != NEXT_INSN (last); insn = NEXT_INSN (insn))
1720 lra_get_insn_recog_data (insn)->sp_offset = offset;
1723 /* Emit insns BEFORE before INSN and insns AFTER after INSN. Put the
1724 insns onto the stack. Print about emitting the insns with
1725 TITLE. */
1726 void
1727 lra_process_new_insns (rtx_insn *insn, rtx_insn *before, rtx_insn *after,
1728 const char *title)
1730 rtx_insn *last;
1732 if (before == NULL_RTX && after == NULL_RTX)
1733 return;
1734 if (lra_dump_file != NULL)
1736 dump_insn_slim (lra_dump_file, insn);
1737 if (before != NULL_RTX)
1739 fprintf (lra_dump_file," %s before:\n", title);
1740 dump_rtl_slim (lra_dump_file, before, NULL, -1, 0);
1742 if (after != NULL_RTX)
1744 fprintf (lra_dump_file, " %s after:\n", title);
1745 dump_rtl_slim (lra_dump_file, after, NULL, -1, 0);
1747 fprintf (lra_dump_file, "\n");
1749 if (before != NULL_RTX)
1751 emit_insn_before (before, insn);
1752 push_insns (PREV_INSN (insn), PREV_INSN (before));
1753 setup_sp_offset (before, PREV_INSN (insn));
1755 if (after != NULL_RTX)
1757 for (last = after; NEXT_INSN (last) != NULL_RTX; last = NEXT_INSN (last))
1759 emit_insn_after (after, insn);
1760 push_insns (last, insn);
1761 setup_sp_offset (after, last);
1767 /* This page contains code dealing with scratches (changing them onto
1768 pseudos and restoring them from the pseudos).
1770 We change scratches into pseudos at the beginning of LRA to
1771 simplify dealing with them (conflicts, hard register assignments).
1773 If the pseudo denoting scratch was spilled it means that we do need
1774 a hard register for it. Such pseudos are transformed back to
1775 scratches at the end of LRA. */
1777 /* Description of location of a former scratch operand. */
1778 struct sloc
1780 rtx_insn *insn; /* Insn where the scratch was. */
1781 int nop; /* Number of the operand which was a scratch. */
1784 typedef struct sloc *sloc_t;
1786 /* Locations of the former scratches. */
1787 static vec<sloc_t> scratches;
1789 /* Bitmap of scratch regnos. */
1790 static bitmap_head scratch_bitmap;
1792 /* Bitmap of scratch operands. */
1793 static bitmap_head scratch_operand_bitmap;
1795 /* Return true if pseudo REGNO is made of SCRATCH. */
1796 bool
1797 lra_former_scratch_p (int regno)
1799 return bitmap_bit_p (&scratch_bitmap, regno);
1802 /* Return true if the operand NOP of INSN is a former scratch. */
1803 bool
1804 lra_former_scratch_operand_p (rtx_insn *insn, int nop)
1806 return bitmap_bit_p (&scratch_operand_bitmap,
1807 INSN_UID (insn) * MAX_RECOG_OPERANDS + nop) != 0;
1810 /* Change scratches onto pseudos and save their location. */
1811 static void
1812 remove_scratches (void)
1814 int i;
1815 bool insn_changed_p;
1816 basic_block bb;
1817 rtx_insn *insn;
1818 rtx reg;
1819 sloc_t loc;
1820 lra_insn_recog_data_t id;
1821 struct lra_static_insn_data *static_id;
1823 scratches.create (get_max_uid ());
1824 bitmap_initialize (&scratch_bitmap, &reg_obstack);
1825 bitmap_initialize (&scratch_operand_bitmap, &reg_obstack);
1826 FOR_EACH_BB_FN (bb, cfun)
1827 FOR_BB_INSNS (bb, insn)
1828 if (INSN_P (insn))
1830 id = lra_get_insn_recog_data (insn);
1831 static_id = id->insn_static_data;
1832 insn_changed_p = false;
1833 for (i = 0; i < static_id->n_operands; i++)
1834 if (GET_CODE (*id->operand_loc[i]) == SCRATCH
1835 && GET_MODE (*id->operand_loc[i]) != VOIDmode)
1837 insn_changed_p = true;
1838 *id->operand_loc[i] = reg
1839 = lra_create_new_reg (static_id->operand[i].mode,
1840 *id->operand_loc[i], ALL_REGS, NULL);
1841 add_reg_note (insn, REG_UNUSED, reg);
1842 lra_update_dup (id, i);
1843 loc = XNEW (struct sloc);
1844 loc->insn = insn;
1845 loc->nop = i;
1846 scratches.safe_push (loc);
1847 bitmap_set_bit (&scratch_bitmap, REGNO (*id->operand_loc[i]));
1848 bitmap_set_bit (&scratch_operand_bitmap,
1849 INSN_UID (insn) * MAX_RECOG_OPERANDS + i);
1850 if (lra_dump_file != NULL)
1851 fprintf (lra_dump_file,
1852 "Removing SCRATCH in insn #%u (nop %d)\n",
1853 INSN_UID (insn), i);
1855 if (insn_changed_p)
1856 /* Because we might use DF right after caller-saves sub-pass
1857 we need to keep DF info up to date. */
1858 df_insn_rescan (insn);
1862 /* Changes pseudos created by function remove_scratches onto scratches. */
1863 static void
1864 restore_scratches (void)
1866 int regno;
1867 unsigned i;
1868 sloc_t loc;
1869 rtx_insn *last = NULL;
1870 lra_insn_recog_data_t id = NULL;
1872 for (i = 0; scratches.iterate (i, &loc); i++)
1874 if (last != loc->insn)
1876 last = loc->insn;
1877 id = lra_get_insn_recog_data (last);
1879 if (REG_P (*id->operand_loc[loc->nop])
1880 && ((regno = REGNO (*id->operand_loc[loc->nop]))
1881 >= FIRST_PSEUDO_REGISTER)
1882 && lra_get_regno_hard_regno (regno) < 0)
1884 /* It should be only case when scratch register with chosen
1885 constraint 'X' did not get memory or hard register. */
1886 lra_assert (lra_former_scratch_p (regno));
1887 *id->operand_loc[loc->nop]
1888 = gen_rtx_SCRATCH (GET_MODE (*id->operand_loc[loc->nop]));
1889 lra_update_dup (id, loc->nop);
1890 if (lra_dump_file != NULL)
1891 fprintf (lra_dump_file, "Restoring SCRATCH in insn #%u(nop %d)\n",
1892 INSN_UID (loc->insn), loc->nop);
1895 for (i = 0; scratches.iterate (i, &loc); i++)
1896 free (loc);
1897 scratches.release ();
1898 bitmap_clear (&scratch_bitmap);
1899 bitmap_clear (&scratch_operand_bitmap);
1904 #ifdef ENABLE_CHECKING
1906 /* Function checks RTL for correctness. If FINAL_P is true, it is
1907 done at the end of LRA and the check is more rigorous. */
1908 static void
1909 check_rtl (bool final_p)
1911 basic_block bb;
1912 rtx_insn *insn;
1914 lra_assert (! final_p || reload_completed);
1915 FOR_EACH_BB_FN (bb, cfun)
1916 FOR_BB_INSNS (bb, insn)
1917 if (NONDEBUG_INSN_P (insn)
1918 && GET_CODE (PATTERN (insn)) != USE
1919 && GET_CODE (PATTERN (insn)) != CLOBBER
1920 && GET_CODE (PATTERN (insn)) != ASM_INPUT)
1922 if (final_p)
1924 extract_insn (insn);
1925 lra_assert (constrain_operands (1));
1926 continue;
1928 /* LRA code is based on assumption that all addresses can be
1929 correctly decomposed. LRA can generate reloads for
1930 decomposable addresses. The decomposition code checks the
1931 correctness of the addresses. So we don't need to check
1932 the addresses here. Don't call insn_invalid_p here, it can
1933 change the code at this stage. */
1934 if (recog_memoized (insn) < 0 && asm_noperands (PATTERN (insn)) < 0)
1935 fatal_insn_not_found (insn);
1938 #endif /* #ifdef ENABLE_CHECKING */
1940 /* Determine if the current function has an exception receiver block
1941 that reaches the exit block via non-exceptional edges */
1942 static bool
1943 has_nonexceptional_receiver (void)
1945 edge e;
1946 edge_iterator ei;
1947 basic_block *tos, *worklist, bb;
1949 /* If we're not optimizing, then just err on the safe side. */
1950 if (!optimize)
1951 return true;
1953 /* First determine which blocks can reach exit via normal paths. */
1954 tos = worklist = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun) + 1);
1956 FOR_EACH_BB_FN (bb, cfun)
1957 bb->flags &= ~BB_REACHABLE;
1959 /* Place the exit block on our worklist. */
1960 EXIT_BLOCK_PTR_FOR_FN (cfun)->flags |= BB_REACHABLE;
1961 *tos++ = EXIT_BLOCK_PTR_FOR_FN (cfun);
1963 /* Iterate: find everything reachable from what we've already seen. */
1964 while (tos != worklist)
1966 bb = *--tos;
1968 FOR_EACH_EDGE (e, ei, bb->preds)
1969 if (e->flags & EDGE_ABNORMAL)
1971 free (worklist);
1972 return true;
1974 else
1976 basic_block src = e->src;
1978 if (!(src->flags & BB_REACHABLE))
1980 src->flags |= BB_REACHABLE;
1981 *tos++ = src;
1985 free (worklist);
1986 /* No exceptional block reached exit unexceptionally. */
1987 return false;
1990 #ifdef AUTO_INC_DEC
1992 /* Process recursively X of INSN and add REG_INC notes if necessary. */
1993 static void
1994 add_auto_inc_notes (rtx_insn *insn, rtx x)
1996 enum rtx_code code = GET_CODE (x);
1997 const char *fmt;
1998 int i, j;
2000 if (code == MEM && auto_inc_p (XEXP (x, 0)))
2002 add_reg_note (insn, REG_INC, XEXP (XEXP (x, 0), 0));
2003 return;
2006 /* Scan all X sub-expressions. */
2007 fmt = GET_RTX_FORMAT (code);
2008 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2010 if (fmt[i] == 'e')
2011 add_auto_inc_notes (insn, XEXP (x, i));
2012 else if (fmt[i] == 'E')
2013 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2014 add_auto_inc_notes (insn, XVECEXP (x, i, j));
2018 #endif
2020 /* Remove all REG_DEAD and REG_UNUSED notes and regenerate REG_INC.
2021 We change pseudos by hard registers without notification of DF and
2022 that can make the notes obsolete. DF-infrastructure does not deal
2023 with REG_INC notes -- so we should regenerate them here. */
2024 static void
2025 update_inc_notes (void)
2027 rtx *pnote;
2028 basic_block bb;
2029 rtx_insn *insn;
2031 FOR_EACH_BB_FN (bb, cfun)
2032 FOR_BB_INSNS (bb, insn)
2033 if (NONDEBUG_INSN_P (insn))
2035 pnote = &REG_NOTES (insn);
2036 while (*pnote != 0)
2038 if (REG_NOTE_KIND (*pnote) == REG_DEAD
2039 || REG_NOTE_KIND (*pnote) == REG_UNUSED
2040 || REG_NOTE_KIND (*pnote) == REG_INC)
2041 *pnote = XEXP (*pnote, 1);
2042 else
2043 pnote = &XEXP (*pnote, 1);
2045 #ifdef AUTO_INC_DEC
2046 add_auto_inc_notes (insn, PATTERN (insn));
2047 #endif
2051 /* Set to 1 while in lra. */
2052 int lra_in_progress;
2054 /* Start of pseudo regnos before the LRA. */
2055 int lra_new_regno_start;
2057 /* Start of reload pseudo regnos before the new spill pass. */
2058 int lra_constraint_new_regno_start;
2060 /* Inheritance pseudo regnos before the new spill pass. */
2061 bitmap_head lra_inheritance_pseudos;
2063 /* Split regnos before the new spill pass. */
2064 bitmap_head lra_split_regs;
2066 /* Reload pseudo regnos before the new assignmnet pass which still can
2067 be spilled after the assinment pass as memory is also accepted in
2068 insns for the reload pseudos. */
2069 bitmap_head lra_optional_reload_pseudos;
2071 /* Pseudo regnos used for subreg reloads before the new assignment
2072 pass. Such pseudos still can be spilled after the assinment
2073 pass. */
2074 bitmap_head lra_subreg_reload_pseudos;
2076 /* First UID of insns generated before a new spill pass. */
2077 int lra_constraint_new_insn_uid_start;
2079 /* File used for output of LRA debug information. */
2080 FILE *lra_dump_file;
2082 /* True if we should try spill into registers of different classes
2083 instead of memory. */
2084 bool lra_reg_spill_p;
2086 /* Set up value LRA_REG_SPILL_P. */
2087 static void
2088 setup_reg_spill_flag (void)
2090 int cl, mode;
2092 if (targetm.spill_class != NULL)
2093 for (cl = 0; cl < (int) LIM_REG_CLASSES; cl++)
2094 for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
2095 if (targetm.spill_class ((enum reg_class) cl,
2096 (enum machine_mode) mode) != NO_REGS)
2098 lra_reg_spill_p = true;
2099 return;
2101 lra_reg_spill_p = false;
2104 /* True if the current function is too big to use regular algorithms
2105 in LRA. In other words, we should use simpler and faster algorithms
2106 in LRA. It also means we should not worry about generation code
2107 for caller saves. The value is set up in IRA. */
2108 bool lra_simple_p;
2110 /* Major LRA entry function. F is a file should be used to dump LRA
2111 debug info. */
2112 void
2113 lra (FILE *f)
2115 int i;
2116 bool live_p, scratch_p, inserted_p;
2118 lra_dump_file = f;
2120 timevar_push (TV_LRA);
2122 /* Make sure that the last insn is a note. Some subsequent passes
2123 need it. */
2124 emit_note (NOTE_INSN_DELETED);
2126 COPY_HARD_REG_SET (lra_no_alloc_regs, ira_no_alloc_regs);
2128 init_reg_info ();
2129 expand_reg_info ();
2131 init_insn_recog_data ();
2133 #ifdef ENABLE_CHECKING
2134 /* Some quick check on RTL generated by previous passes. */
2135 check_rtl (false);
2136 #endif
2138 lra_in_progress = 1;
2140 lra_live_range_iter = lra_coalesce_iter = 0;
2141 lra_constraint_iter = lra_constraint_iter_after_spill = 0;
2142 lra_inheritance_iter = lra_undo_inheritance_iter = 0;
2144 setup_reg_spill_flag ();
2146 /* Function remove_scratches can creates new pseudos for clobbers --
2147 so set up lra_constraint_new_regno_start before its call to
2148 permit changing reg classes for pseudos created by this
2149 simplification. */
2150 lra_constraint_new_regno_start = lra_new_regno_start = max_reg_num ();
2151 remove_scratches ();
2152 scratch_p = lra_constraint_new_regno_start != max_reg_num ();
2154 /* A function that has a non-local label that can reach the exit
2155 block via non-exceptional paths must save all call-saved
2156 registers. */
2157 if (cfun->has_nonlocal_label && has_nonexceptional_receiver ())
2158 crtl->saves_all_registers = 1;
2160 if (crtl->saves_all_registers)
2161 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2162 if (! call_used_regs[i] && ! fixed_regs[i] && ! LOCAL_REGNO (i))
2163 df_set_regs_ever_live (i, true);
2165 /* We don't DF from now and avoid its using because it is to
2166 expensive when a lot of RTL changes are made. */
2167 df_set_flags (DF_NO_INSN_RESCAN);
2168 lra_constraint_insn_stack.create (get_max_uid ());
2169 lra_constraint_insn_stack_bitmap = sbitmap_alloc (get_max_uid ());
2170 bitmap_clear (lra_constraint_insn_stack_bitmap);
2171 lra_live_ranges_init ();
2172 lra_constraints_init ();
2173 lra_curr_reload_num = 0;
2174 push_insns (get_last_insn (), NULL);
2175 /* It is needed for the 1st coalescing. */
2176 lra_constraint_new_insn_uid_start = get_max_uid ();
2177 bitmap_initialize (&lra_inheritance_pseudos, &reg_obstack);
2178 bitmap_initialize (&lra_split_regs, &reg_obstack);
2179 bitmap_initialize (&lra_optional_reload_pseudos, &reg_obstack);
2180 bitmap_initialize (&lra_subreg_reload_pseudos, &reg_obstack);
2181 live_p = false;
2182 if (get_frame_size () != 0 && crtl->stack_alignment_needed)
2183 /* If we have a stack frame, we must align it now. The stack size
2184 may be a part of the offset computation for register
2185 elimination. */
2186 assign_stack_local (BLKmode, 0, crtl->stack_alignment_needed);
2187 lra_init_equiv ();
2188 for (;;)
2190 for (;;)
2192 /* We should try to assign hard registers to scratches even
2193 if there were no RTL transformations in
2194 lra_constraints. */
2195 if (! lra_constraints (lra_constraint_iter == 0)
2196 && (lra_constraint_iter > 1
2197 || (! scratch_p && ! caller_save_needed)))
2198 break;
2199 /* Constraint transformations may result in that eliminable
2200 hard regs become uneliminable and pseudos which use them
2201 should be spilled. It is better to do it before pseudo
2202 assignments.
2204 For example, rs6000 can make
2205 RS6000_PIC_OFFSET_TABLE_REGNUM uneliminable if we started
2206 to use a constant pool. */
2207 lra_eliminate (false, false);
2208 /* Do inheritance only for regular algorithms. */
2209 if (! lra_simple_p)
2211 if (flag_use_caller_save)
2213 if (live_p)
2214 lra_clear_live_ranges ();
2215 /* As a side-effect of lra_create_live_ranges, we calculate
2216 actual_call_used_reg_set, which is needed during
2217 lra_inheritance. */
2218 lra_create_live_ranges (true);
2220 lra_inheritance ();
2222 if (live_p)
2223 lra_clear_live_ranges ();
2224 /* We need live ranges for lra_assign -- so build them. */
2225 lra_create_live_ranges (true);
2226 live_p = true;
2227 /* If we don't spill non-reload and non-inheritance pseudos,
2228 there is no sense to run memory-memory move coalescing.
2229 If inheritance pseudos were spilled, the memory-memory
2230 moves involving them will be removed by pass undoing
2231 inheritance. */
2232 if (lra_simple_p)
2233 lra_assign ();
2234 else
2236 bool spill_p = !lra_assign ();
2238 if (lra_undo_inheritance ())
2239 live_p = false;
2240 if (spill_p)
2242 if (! live_p)
2244 lra_create_live_ranges (true);
2245 live_p = true;
2247 if (lra_coalesce ())
2248 live_p = false;
2250 if (! live_p)
2251 lra_clear_live_ranges ();
2254 /* Don't clear optional reloads bitmap until all constraints are
2255 satisfied as we need to differ them from regular reloads. */
2256 bitmap_clear (&lra_optional_reload_pseudos);
2257 bitmap_clear (&lra_subreg_reload_pseudos);
2258 bitmap_clear (&lra_inheritance_pseudos);
2259 bitmap_clear (&lra_split_regs);
2260 if (! lra_need_for_spills_p ())
2261 break;
2262 if (! live_p)
2264 /* We need full live info for spilling pseudos into
2265 registers instead of memory. */
2266 lra_create_live_ranges (lra_reg_spill_p);
2267 live_p = true;
2269 lra_spill ();
2270 /* Assignment of stack slots changes elimination offsets for
2271 some eliminations. So update the offsets here. */
2272 lra_eliminate (false, false);
2273 lra_constraint_new_regno_start = max_reg_num ();
2274 lra_constraint_new_insn_uid_start = get_max_uid ();
2275 lra_constraint_iter_after_spill = 0;
2277 restore_scratches ();
2278 lra_eliminate (true, false);
2279 lra_final_code_change ();
2280 lra_in_progress = 0;
2281 if (live_p)
2282 lra_clear_live_ranges ();
2283 lra_live_ranges_finish ();
2284 lra_constraints_finish ();
2285 finish_reg_info ();
2286 sbitmap_free (lra_constraint_insn_stack_bitmap);
2287 lra_constraint_insn_stack.release ();
2288 finish_insn_recog_data ();
2289 regstat_free_n_sets_and_refs ();
2290 regstat_free_ri ();
2291 reload_completed = 1;
2292 update_inc_notes ();
2294 inserted_p = fixup_abnormal_edges ();
2296 /* We've possibly turned single trapping insn into multiple ones. */
2297 if (cfun->can_throw_non_call_exceptions)
2299 sbitmap blocks;
2300 blocks = sbitmap_alloc (last_basic_block_for_fn (cfun));
2301 bitmap_ones (blocks);
2302 find_many_sub_basic_blocks (blocks);
2303 sbitmap_free (blocks);
2306 if (inserted_p)
2307 commit_edge_insertions ();
2309 /* Replacing pseudos with their memory equivalents might have
2310 created shared rtx. Subsequent passes would get confused
2311 by this, so unshare everything here. */
2312 unshare_all_rtl_again (get_insns ());
2314 #ifdef ENABLE_CHECKING
2315 check_rtl (true);
2316 #endif
2318 timevar_pop (TV_LRA);
2321 /* Called once per compiler to initialize LRA data once. */
2322 void
2323 lra_init_once (void)
2325 init_insn_code_data_once ();
2328 /* Called once per compiler to finish LRA data which are initialize
2329 once. */
2330 void
2331 lra_finish_once (void)
2333 finish_insn_code_data_once ();