2015-06-11 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / lra.c
blobe44cec0dd6272c44bfcd259dace931abbe6270cf
1 /* LRA (local register allocator) driver and LRA utilities.
2 Copyright (C) 2010-2015 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 Recalculating spilled pseudo values (rematerialization);
41 o Changing spilled pseudos to stack memory or their equivalences;
42 o Allocation stack memory changes the address displacement and
43 new iteration is needed.
45 Here is block diagram of LRA passes:
47 ------------------------
48 --------------- | Undo inheritance for | ---------------
49 | Memory-memory | | spilled pseudos, | | New (and old) |
50 | move coalesce |<---| splits for pseudos got |<-- | pseudos |
51 --------------- | the same hard regs, | | assignment |
52 Start | | and optional reloads | ---------------
53 | | ------------------------ ^
54 V | ---------------- |
55 ----------- V | Update virtual | |
56 | Remove |----> ------------>| register | |
57 | scratches | ^ | displacements | |
58 ----------- | ---------------- |
59 | | |
60 | V New |
61 | ------------ pseudos -------------------
62 | |Constraints:| or insns | Inheritance/split |
63 | | RTL |--------->| transformations |
64 | | transfor- | | in EBB scope |
65 | substi- | mations | -------------------
66 | tutions ------------
67 | | No change
68 ---------------- V
69 | Spilled pseudo | -------------------
70 | to memory |<----| Rematerialization |
71 | substitution | -------------------
72 ----------------
73 | No susbtitions
75 -------------------------
76 | Hard regs substitution, |
77 | devirtalization, and |------> Finish
78 | restoring scratches got |
79 | memory |
80 -------------------------
82 To speed up the process:
83 o We process only insns affected by changes on previous
84 iterations;
85 o We don't use DFA-infrastructure because it results in much slower
86 compiler speed than a special IR described below does;
87 o We use a special insn representation for quick access to insn
88 info which is always *synchronized* with the current RTL;
89 o Insn IR is minimized by memory. It is divided on three parts:
90 o one specific for each insn in RTL (only operand locations);
91 o one common for all insns in RTL with the same insn code
92 (different operand attributes from machine descriptions);
93 o one oriented for maintenance of live info (list of pseudos).
94 o Pseudo data:
95 o all insns where the pseudo is referenced;
96 o live info (conflicting hard regs, live ranges, # of
97 references etc);
98 o data used for assigning (preferred hard regs, costs etc).
100 This file contains LRA driver, LRA utility functions and data, and
101 code for dealing with scratches. */
103 #include "config.h"
104 #include "system.h"
105 #include "coretypes.h"
106 #include "tm.h"
107 #include "hard-reg-set.h"
108 #include "rtl.h"
109 #include "tm_p.h"
110 #include "regs.h"
111 #include "insn-config.h"
112 #include "insn-codes.h"
113 #include "recog.h"
114 #include "output.h"
115 #include "addresses.h"
116 #include "flags.h"
117 #include "input.h"
118 #include "function.h"
119 #include "symtab.h"
120 #include "tree.h"
121 #include "optabs.h"
122 #include "alias.h"
123 #include "expmed.h"
124 #include "dojump.h"
125 #include "explow.h"
126 #include "calls.h"
127 #include "emit-rtl.h"
128 #include "varasm.h"
129 #include "stmt.h"
130 #include "expr.h"
131 #include "predict.h"
132 #include "dominance.h"
133 #include "cfg.h"
134 #include "cfgrtl.h"
135 #include "cfgbuild.h"
136 #include "basic-block.h"
137 #include "except.h"
138 #include "tree-pass.h"
139 #include "timevar.h"
140 #include "target.h"
141 #include "ira.h"
142 #include "alloc-pool.h"
143 #include "lra-int.h"
144 #include "df.h"
146 /* Dump bitmap SET with TITLE and BB INDEX. */
147 void
148 lra_dump_bitmap_with_title (const char *title, bitmap set, int index)
150 unsigned int i;
151 int count;
152 bitmap_iterator bi;
153 static const int max_nums_on_line = 10;
155 if (bitmap_empty_p (set))
156 return;
157 fprintf (lra_dump_file, " %s %d:", title, index);
158 fprintf (lra_dump_file, "\n");
159 count = max_nums_on_line + 1;
160 EXECUTE_IF_SET_IN_BITMAP (set, 0, i, bi)
162 if (count > max_nums_on_line)
164 fprintf (lra_dump_file, "\n ");
165 count = 0;
167 fprintf (lra_dump_file, " %4u", i);
168 count++;
170 fprintf (lra_dump_file, "\n");
173 /* Hard registers currently not available for allocation. It can
174 changed after some hard registers become not eliminable. */
175 HARD_REG_SET lra_no_alloc_regs;
177 static int get_new_reg_value (void);
178 static void expand_reg_info (void);
179 static void invalidate_insn_recog_data (int);
180 static int get_insn_freq (rtx_insn *);
181 static void invalidate_insn_data_regno_info (lra_insn_recog_data_t,
182 rtx_insn *, int);
184 /* Expand all regno related info needed for LRA. */
185 static void
186 expand_reg_data (int old)
188 resize_reg_info ();
189 expand_reg_info ();
190 ira_expand_reg_equiv ();
191 for (int i = (int) max_reg_num () - 1; i >= old; i--)
192 lra_change_class (i, ALL_REGS, " Set", true);
195 /* Create and return a new reg of ORIGINAL mode. If ORIGINAL is NULL
196 or of VOIDmode, use MD_MODE for the new reg. Initialize its
197 register class to RCLASS. Print message about assigning class
198 RCLASS containing new register name TITLE unless it is NULL. Use
199 attributes of ORIGINAL if it is a register. The created register
200 will have unique held value. */
202 lra_create_new_reg_with_unique_value (machine_mode md_mode, rtx original,
203 enum reg_class rclass, const char *title)
205 machine_mode mode;
206 rtx new_reg;
208 if (original == NULL_RTX || (mode = GET_MODE (original)) == VOIDmode)
209 mode = md_mode;
210 lra_assert (mode != VOIDmode);
211 new_reg = gen_reg_rtx (mode);
212 if (original == NULL_RTX || ! REG_P (original))
214 if (lra_dump_file != NULL)
215 fprintf (lra_dump_file, " Creating newreg=%i", REGNO (new_reg));
217 else
219 if (ORIGINAL_REGNO (original) >= FIRST_PSEUDO_REGISTER)
220 ORIGINAL_REGNO (new_reg) = ORIGINAL_REGNO (original);
221 REG_USERVAR_P (new_reg) = REG_USERVAR_P (original);
222 REG_POINTER (new_reg) = REG_POINTER (original);
223 REG_ATTRS (new_reg) = REG_ATTRS (original);
224 if (lra_dump_file != NULL)
225 fprintf (lra_dump_file, " Creating newreg=%i from oldreg=%i",
226 REGNO (new_reg), REGNO (original));
228 if (lra_dump_file != NULL)
230 if (title != NULL)
231 fprintf (lra_dump_file, ", assigning class %s to%s%s r%d",
232 reg_class_names[rclass], *title == '\0' ? "" : " ",
233 title, REGNO (new_reg));
234 fprintf (lra_dump_file, "\n");
236 expand_reg_data (max_reg_num ());
237 setup_reg_classes (REGNO (new_reg), rclass, NO_REGS, rclass);
238 return new_reg;
241 /* Analogous to the previous function but also inherits value of
242 ORIGINAL. */
244 lra_create_new_reg (machine_mode md_mode, rtx original,
245 enum reg_class rclass, const char *title)
247 rtx new_reg;
249 new_reg
250 = lra_create_new_reg_with_unique_value (md_mode, original, rclass, title);
251 if (original != NULL_RTX && REG_P (original))
252 lra_assign_reg_val (REGNO (original), REGNO (new_reg));
253 return new_reg;
256 /* Set up for REGNO unique hold value. */
257 void
258 lra_set_regno_unique_value (int regno)
260 lra_reg_info[regno].val = get_new_reg_value ();
263 /* Invalidate INSN related info used by LRA. The info should never be
264 used after that. */
265 void
266 lra_invalidate_insn_data (rtx_insn *insn)
268 lra_invalidate_insn_regno_info (insn);
269 invalidate_insn_recog_data (INSN_UID (insn));
272 /* Mark INSN deleted and invalidate the insn related info used by
273 LRA. */
274 void
275 lra_set_insn_deleted (rtx_insn *insn)
277 lra_invalidate_insn_data (insn);
278 SET_INSN_DELETED (insn);
281 /* Delete an unneeded INSN and any previous insns who sole purpose is
282 loading data that is dead in INSN. */
283 void
284 lra_delete_dead_insn (rtx_insn *insn)
286 rtx_insn *prev = prev_real_insn (insn);
287 rtx prev_dest;
289 /* If the previous insn sets a register that dies in our insn,
290 delete it too. */
291 if (prev && GET_CODE (PATTERN (prev)) == SET
292 && (prev_dest = SET_DEST (PATTERN (prev)), REG_P (prev_dest))
293 && reg_mentioned_p (prev_dest, PATTERN (insn))
294 && find_regno_note (insn, REG_DEAD, REGNO (prev_dest))
295 && ! side_effects_p (SET_SRC (PATTERN (prev))))
296 lra_delete_dead_insn (prev);
298 lra_set_insn_deleted (insn);
301 /* Emit insn x = y + z. Return NULL if we failed to do it.
302 Otherwise, return the insn. We don't use gen_add3_insn as it might
303 clobber CC. */
304 static rtx_insn *
305 emit_add3_insn (rtx x, rtx y, rtx z)
307 rtx_insn *last;
309 last = get_last_insn ();
311 if (have_addptr3_insn (x, y, z))
313 rtx_insn *insn = gen_addptr3_insn (x, y, z);
315 /* If the target provides an "addptr" pattern it hopefully does
316 for a reason. So falling back to the normal add would be
317 a bug. */
318 lra_assert (insn != NULL_RTX);
319 emit_insn (insn);
320 return insn;
323 rtx_insn *insn = emit_insn (gen_rtx_SET (x, gen_rtx_PLUS (GET_MODE (y),
324 y, z)));
325 if (recog_memoized (insn) < 0)
327 delete_insns_since (last);
328 insn = NULL;
330 return insn;
333 /* Emit insn x = x + y. Return the insn. We use gen_add2_insn as the
334 last resort. */
335 static rtx_insn *
336 emit_add2_insn (rtx x, rtx y)
338 rtx_insn *insn = emit_add3_insn (x, x, y);
339 if (insn == NULL_RTX)
341 insn = gen_add2_insn (x, y);
342 if (insn != NULL_RTX)
343 emit_insn (insn);
345 return insn;
348 /* Target checks operands through operand predicates to recognize an
349 insn. We should have a special precaution to generate add insns
350 which are frequent results of elimination.
352 Emit insns for x = y + z. X can be used to store intermediate
353 values and should be not in Y and Z when we use X to store an
354 intermediate value. Y + Z should form [base] [+ index[ * scale]] [
355 + disp] where base and index are registers, disp and scale are
356 constants. Y should contain base if it is present, Z should
357 contain disp if any. index[*scale] can be part of Y or Z. */
358 void
359 lra_emit_add (rtx x, rtx y, rtx z)
361 int old;
362 rtx_insn *last;
363 rtx a1, a2, base, index, disp, scale, index_scale;
364 bool ok_p;
366 rtx_insn *add3_insn = emit_add3_insn (x, y, z);
367 old = max_reg_num ();
368 if (add3_insn != NULL)
370 else
372 disp = a2 = NULL_RTX;
373 if (GET_CODE (y) == PLUS)
375 a1 = XEXP (y, 0);
376 a2 = XEXP (y, 1);
377 disp = z;
379 else
381 a1 = y;
382 if (CONSTANT_P (z))
383 disp = z;
384 else
385 a2 = z;
387 index_scale = scale = NULL_RTX;
388 if (GET_CODE (a1) == MULT)
390 index_scale = a1;
391 index = XEXP (a1, 0);
392 scale = XEXP (a1, 1);
393 base = a2;
395 else if (a2 != NULL_RTX && GET_CODE (a2) == MULT)
397 index_scale = a2;
398 index = XEXP (a2, 0);
399 scale = XEXP (a2, 1);
400 base = a1;
402 else
404 base = a1;
405 index = a2;
407 if (! (REG_P (base) || GET_CODE (base) == SUBREG)
408 || (index != NULL_RTX
409 && ! (REG_P (index) || GET_CODE (index) == SUBREG))
410 || (disp != NULL_RTX && ! CONSTANT_P (disp))
411 || (scale != NULL_RTX && ! CONSTANT_P (scale)))
413 /* Probably we have no 3 op add. Last chance is to use 2-op
414 add insn. To succeed, don't move Z to X as an address
415 segment always comes in Y. Otherwise, we might fail when
416 adding the address segment to register. */
417 lra_assert (x != y && x != z);
418 emit_move_insn (x, y);
419 rtx_insn *insn = emit_add2_insn (x, z);
420 lra_assert (insn != NULL_RTX);
422 else
424 if (index_scale == NULL_RTX)
425 index_scale = index;
426 if (disp == NULL_RTX)
428 /* Generate x = index_scale; x = x + base. */
429 lra_assert (index_scale != NULL_RTX && base != NULL_RTX);
430 emit_move_insn (x, index_scale);
431 rtx_insn *insn = emit_add2_insn (x, base);
432 lra_assert (insn != NULL_RTX);
434 else if (scale == NULL_RTX)
436 /* Try x = base + disp. */
437 lra_assert (base != NULL_RTX);
438 last = get_last_insn ();
439 rtx_insn *move_insn =
440 emit_move_insn (x, gen_rtx_PLUS (GET_MODE (base), base, disp));
441 if (recog_memoized (move_insn) < 0)
443 delete_insns_since (last);
444 /* Generate x = disp; x = x + base. */
445 emit_move_insn (x, disp);
446 rtx_insn *add2_insn = emit_add2_insn (x, base);
447 lra_assert (add2_insn != NULL_RTX);
449 /* Generate x = x + index. */
450 if (index != NULL_RTX)
452 rtx_insn *insn = emit_add2_insn (x, index);
453 lra_assert (insn != NULL_RTX);
456 else
458 /* Try x = index_scale; x = x + disp; x = x + base. */
459 last = get_last_insn ();
460 rtx_insn *move_insn = emit_move_insn (x, index_scale);
461 ok_p = false;
462 if (recog_memoized (move_insn) >= 0)
464 rtx_insn *insn = emit_add2_insn (x, disp);
465 if (insn != NULL_RTX)
467 insn = emit_add2_insn (x, base);
468 if (insn != NULL_RTX)
469 ok_p = true;
472 if (! ok_p)
474 delete_insns_since (last);
475 /* Generate x = disp; x = x + base; x = x + index_scale. */
476 emit_move_insn (x, disp);
477 rtx_insn *insn = emit_add2_insn (x, base);
478 lra_assert (insn != NULL_RTX);
479 insn = emit_add2_insn (x, index_scale);
480 lra_assert (insn != NULL_RTX);
485 /* Functions emit_... can create pseudos -- so expand the pseudo
486 data. */
487 if (old != max_reg_num ())
488 expand_reg_data (old);
491 /* The number of emitted reload insns so far. */
492 int lra_curr_reload_num;
494 /* Emit x := y, processing special case when y = u + v or y = u + v *
495 scale + w through emit_add (Y can be an address which is base +
496 index reg * scale + displacement in general case). X may be used
497 as intermediate result therefore it should be not in Y. */
498 void
499 lra_emit_move (rtx x, rtx y)
501 int old;
503 if (GET_CODE (y) != PLUS)
505 if (rtx_equal_p (x, y))
506 return;
507 old = max_reg_num ();
508 emit_move_insn (x, y);
509 if (REG_P (x))
510 lra_reg_info[ORIGINAL_REGNO (x)].last_reload = ++lra_curr_reload_num;
511 /* Function emit_move can create pseudos -- so expand the pseudo
512 data. */
513 if (old != max_reg_num ())
514 expand_reg_data (old);
515 return;
517 lra_emit_add (x, XEXP (y, 0), XEXP (y, 1));
520 /* Update insn operands which are duplication of operands whose
521 numbers are in array of NOPS (with end marker -1). The insn is
522 represented by its LRA internal representation ID. */
523 void
524 lra_update_dups (lra_insn_recog_data_t id, signed char *nops)
526 int i, j, nop;
527 struct lra_static_insn_data *static_id = id->insn_static_data;
529 for (i = 0; i < static_id->n_dups; i++)
530 for (j = 0; (nop = nops[j]) >= 0; j++)
531 if (static_id->dup_num[i] == nop)
532 *id->dup_loc[i] = *id->operand_loc[nop];
537 /* This page contains code dealing with info about registers in the
538 insns. */
540 /* Pools for insn reg info. */
541 pool_allocator<lra_insn_reg> lra_insn_reg::pool ("insn regs", 100);
543 /* Create LRA insn related info about a reference to REGNO in INSN with
544 TYPE (in/out/inout), biggest reference mode MODE, flag that it is
545 reference through subreg (SUBREG_P), flag that is early clobbered
546 in the insn (EARLY_CLOBBER), and reference to the next insn reg
547 info (NEXT). */
548 static struct lra_insn_reg *
549 new_insn_reg (rtx_insn *insn, int regno, enum op_type type,
550 machine_mode mode,
551 bool subreg_p, bool early_clobber, struct lra_insn_reg *next)
553 lra_insn_reg *ir = new lra_insn_reg ();
554 ir->type = type;
555 ir->biggest_mode = mode;
556 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (lra_reg_info[regno].biggest_mode)
557 && NONDEBUG_INSN_P (insn))
558 lra_reg_info[regno].biggest_mode = mode;
559 ir->subreg_p = subreg_p;
560 ir->early_clobber = early_clobber;
561 ir->regno = regno;
562 ir->next = next;
563 return ir;
566 /* Free insn reg info list IR. */
567 static void
568 free_insn_regs (struct lra_insn_reg *ir)
570 struct lra_insn_reg *next_ir;
572 for (; ir != NULL; ir = next_ir)
574 next_ir = ir->next;
575 delete ir;
579 /* Finish pool for insn reg info. */
580 static void
581 finish_insn_regs (void)
583 lra_insn_reg::pool.release ();
588 /* This page contains code dealing LRA insn info (or in other words
589 LRA internal insn representation). */
591 /* Map INSN_CODE -> the static insn data. This info is valid during
592 all translation unit. */
593 struct lra_static_insn_data *insn_code_data[LAST_INSN_CODE];
595 /* Debug insns are represented as a special insn with one input
596 operand which is RTL expression in var_location. */
598 /* The following data are used as static insn operand data for all
599 debug insns. If structure lra_operand_data is changed, the
600 initializer should be changed too. */
601 static struct lra_operand_data debug_operand_data =
603 NULL, /* alternative */
604 VOIDmode, /* We are not interesting in the operand mode. */
605 OP_IN,
606 0, 0, 0, 0
609 /* The following data are used as static insn data for all debug
610 insns. If structure lra_static_insn_data is changed, the
611 initializer should be changed too. */
612 static struct lra_static_insn_data debug_insn_static_data =
614 &debug_operand_data,
615 0, /* Duplication operands #. */
616 -1, /* Commutative operand #. */
617 1, /* Operands #. There is only one operand which is debug RTL
618 expression. */
619 0, /* Duplications #. */
620 0, /* Alternatives #. We are not interesting in alternatives
621 because we does not proceed debug_insns for reloads. */
622 NULL, /* Hard registers referenced in machine description. */
623 NULL /* Descriptions of operands in alternatives. */
626 /* Called once per compiler work to initialize some LRA data related
627 to insns. */
628 static void
629 init_insn_code_data_once (void)
631 memset (insn_code_data, 0, sizeof (insn_code_data));
634 /* Called once per compiler work to finalize some LRA data related to
635 insns. */
636 static void
637 finish_insn_code_data_once (void)
639 int i;
641 for (i = 0; i < LAST_INSN_CODE; i++)
643 if (insn_code_data[i] != NULL)
644 free (insn_code_data[i]);
648 /* Return static insn data, allocate and setup if necessary. Although
649 dup_num is static data (it depends only on icode), to set it up we
650 need to extract insn first. So recog_data should be valid for
651 normal insn (ICODE >= 0) before the call. */
652 static struct lra_static_insn_data *
653 get_static_insn_data (int icode, int nop, int ndup, int nalt)
655 struct lra_static_insn_data *data;
656 size_t n_bytes;
658 lra_assert (icode < LAST_INSN_CODE);
659 if (icode >= 0 && (data = insn_code_data[icode]) != NULL)
660 return data;
661 lra_assert (nop >= 0 && ndup >= 0 && nalt >= 0);
662 n_bytes = sizeof (struct lra_static_insn_data)
663 + sizeof (struct lra_operand_data) * nop
664 + sizeof (int) * ndup;
665 data = XNEWVAR (struct lra_static_insn_data, n_bytes);
666 data->operand_alternative = NULL;
667 data->n_operands = nop;
668 data->n_dups = ndup;
669 data->n_alternatives = nalt;
670 data->operand = ((struct lra_operand_data *)
671 ((char *) data + sizeof (struct lra_static_insn_data)));
672 data->dup_num = ((int *) ((char *) data->operand
673 + sizeof (struct lra_operand_data) * nop));
674 if (icode >= 0)
676 int i;
678 insn_code_data[icode] = data;
679 for (i = 0; i < nop; i++)
681 data->operand[i].constraint
682 = insn_data[icode].operand[i].constraint;
683 data->operand[i].mode = insn_data[icode].operand[i].mode;
684 data->operand[i].strict_low = insn_data[icode].operand[i].strict_low;
685 data->operand[i].is_operator
686 = insn_data[icode].operand[i].is_operator;
687 data->operand[i].type
688 = (data->operand[i].constraint[0] == '=' ? OP_OUT
689 : data->operand[i].constraint[0] == '+' ? OP_INOUT
690 : OP_IN);
691 data->operand[i].is_address = false;
693 for (i = 0; i < ndup; i++)
694 data->dup_num[i] = recog_data.dup_num[i];
696 return data;
699 /* The current length of the following array. */
700 int lra_insn_recog_data_len;
702 /* Map INSN_UID -> the insn recog data (NULL if unknown). */
703 lra_insn_recog_data_t *lra_insn_recog_data;
705 /* Initialize LRA data about insns. */
706 static void
707 init_insn_recog_data (void)
709 lra_insn_recog_data_len = 0;
710 lra_insn_recog_data = NULL;
713 /* Expand, if necessary, LRA data about insns. */
714 static void
715 check_and_expand_insn_recog_data (int index)
717 int i, old;
719 if (lra_insn_recog_data_len > index)
720 return;
721 old = lra_insn_recog_data_len;
722 lra_insn_recog_data_len = index * 3 / 2 + 1;
723 lra_insn_recog_data = XRESIZEVEC (lra_insn_recog_data_t,
724 lra_insn_recog_data,
725 lra_insn_recog_data_len);
726 for (i = old; i < lra_insn_recog_data_len; i++)
727 lra_insn_recog_data[i] = NULL;
730 /* Finish LRA DATA about insn. */
731 static void
732 free_insn_recog_data (lra_insn_recog_data_t data)
734 if (data->operand_loc != NULL)
735 free (data->operand_loc);
736 if (data->dup_loc != NULL)
737 free (data->dup_loc);
738 if (data->arg_hard_regs != NULL)
739 free (data->arg_hard_regs);
740 if (data->icode < 0 && NONDEBUG_INSN_P (data->insn))
742 if (data->insn_static_data->operand_alternative != NULL)
743 free (const_cast <operand_alternative *>
744 (data->insn_static_data->operand_alternative));
745 free_insn_regs (data->insn_static_data->hard_regs);
746 free (data->insn_static_data);
748 free_insn_regs (data->regs);
749 data->regs = NULL;
750 free (data);
753 /* Finish LRA data about all insns. */
754 static void
755 finish_insn_recog_data (void)
757 int i;
758 lra_insn_recog_data_t data;
760 for (i = 0; i < lra_insn_recog_data_len; i++)
761 if ((data = lra_insn_recog_data[i]) != NULL)
762 free_insn_recog_data (data);
763 finish_insn_regs ();
764 lra_copy::pool.release ();
765 lra_insn_reg::pool.release ();
766 free (lra_insn_recog_data);
769 /* Setup info about operands in alternatives of LRA DATA of insn. */
770 static void
771 setup_operand_alternative (lra_insn_recog_data_t data,
772 const operand_alternative *op_alt)
774 int i, j, nop, nalt;
775 int icode = data->icode;
776 struct lra_static_insn_data *static_data = data->insn_static_data;
778 static_data->commutative = -1;
779 nop = static_data->n_operands;
780 nalt = static_data->n_alternatives;
781 static_data->operand_alternative = op_alt;
782 for (i = 0; i < nop; i++)
784 static_data->operand[i].early_clobber = false;
785 static_data->operand[i].is_address = false;
786 if (static_data->operand[i].constraint[0] == '%')
788 /* We currently only support one commutative pair of operands. */
789 if (static_data->commutative < 0)
790 static_data->commutative = i;
791 else
792 lra_assert (icode < 0); /* Asm */
793 /* The last operand should not be marked commutative. */
794 lra_assert (i != nop - 1);
797 for (j = 0; j < nalt; j++)
798 for (i = 0; i < nop; i++, op_alt++)
800 static_data->operand[i].early_clobber |= op_alt->earlyclobber;
801 static_data->operand[i].is_address |= op_alt->is_address;
805 /* Recursively process X and collect info about registers, which are
806 not the insn operands, in X with TYPE (in/out/inout) and flag that
807 it is early clobbered in the insn (EARLY_CLOBBER) and add the info
808 to LIST. X is a part of insn given by DATA. Return the result
809 list. */
810 static struct lra_insn_reg *
811 collect_non_operand_hard_regs (rtx *x, lra_insn_recog_data_t data,
812 struct lra_insn_reg *list,
813 enum op_type type, bool early_clobber)
815 int i, j, regno, last;
816 bool subreg_p;
817 machine_mode mode;
818 struct lra_insn_reg *curr;
819 rtx op = *x;
820 enum rtx_code code = GET_CODE (op);
821 const char *fmt = GET_RTX_FORMAT (code);
823 for (i = 0; i < data->insn_static_data->n_operands; i++)
824 if (x == data->operand_loc[i])
825 /* It is an operand loc. Stop here. */
826 return list;
827 for (i = 0; i < data->insn_static_data->n_dups; i++)
828 if (x == data->dup_loc[i])
829 /* It is a dup loc. Stop here. */
830 return list;
831 mode = GET_MODE (op);
832 subreg_p = false;
833 if (code == SUBREG)
835 op = SUBREG_REG (op);
836 code = GET_CODE (op);
837 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (op)))
839 mode = GET_MODE (op);
840 if (GET_MODE_SIZE (mode) > REGMODE_NATURAL_SIZE (mode))
841 subreg_p = true;
844 if (REG_P (op))
846 if ((regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER)
847 return list;
848 /* Process all regs even unallocatable ones as we need info
849 about all regs for rematerialization pass. */
850 for (last = regno + hard_regno_nregs[regno][mode];
851 regno < last;
852 regno++)
854 for (curr = list; curr != NULL; curr = curr->next)
855 if (curr->regno == regno && curr->subreg_p == subreg_p
856 && curr->biggest_mode == mode)
858 if (curr->type != type)
859 curr->type = OP_INOUT;
860 if (curr->early_clobber != early_clobber)
861 curr->early_clobber = true;
862 break;
864 if (curr == NULL)
866 /* This is a new hard regno or the info can not be
867 integrated into the found structure. */
868 #ifdef STACK_REGS
869 early_clobber
870 = (early_clobber
871 /* This clobber is to inform popping floating
872 point stack only. */
873 && ! (FIRST_STACK_REG <= regno
874 && regno <= LAST_STACK_REG));
875 #endif
876 list = new_insn_reg (data->insn, regno, type, mode, subreg_p,
877 early_clobber, list);
880 return list;
882 switch (code)
884 case SET:
885 list = collect_non_operand_hard_regs (&SET_DEST (op), data,
886 list, OP_OUT, false);
887 list = collect_non_operand_hard_regs (&SET_SRC (op), data,
888 list, OP_IN, false);
889 break;
890 case CLOBBER:
891 /* We treat clobber of non-operand hard registers as early
892 clobber (the behavior is expected from asm). */
893 list = collect_non_operand_hard_regs (&XEXP (op, 0), data,
894 list, OP_OUT, true);
895 break;
896 case PRE_INC: case PRE_DEC: case POST_INC: case POST_DEC:
897 list = collect_non_operand_hard_regs (&XEXP (op, 0), data,
898 list, OP_INOUT, false);
899 break;
900 case PRE_MODIFY: case POST_MODIFY:
901 list = collect_non_operand_hard_regs (&XEXP (op, 0), data,
902 list, OP_INOUT, false);
903 list = collect_non_operand_hard_regs (&XEXP (op, 1), data,
904 list, OP_IN, false);
905 break;
906 default:
907 fmt = GET_RTX_FORMAT (code);
908 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
910 if (fmt[i] == 'e')
911 list = collect_non_operand_hard_regs (&XEXP (op, i), data,
912 list, OP_IN, false);
913 else if (fmt[i] == 'E')
914 for (j = XVECLEN (op, i) - 1; j >= 0; j--)
915 list = collect_non_operand_hard_regs (&XVECEXP (op, i, j), data,
916 list, OP_IN, false);
919 return list;
922 /* Set up and return info about INSN. Set up the info if it is not set up
923 yet. */
924 lra_insn_recog_data_t
925 lra_set_insn_recog_data (rtx_insn *insn)
927 lra_insn_recog_data_t data;
928 int i, n, icode;
929 rtx **locs;
930 unsigned int uid = INSN_UID (insn);
931 struct lra_static_insn_data *insn_static_data;
933 check_and_expand_insn_recog_data (uid);
934 if (DEBUG_INSN_P (insn))
935 icode = -1;
936 else
938 icode = INSN_CODE (insn);
939 if (icode < 0)
940 /* It might be a new simple insn which is not recognized yet. */
941 INSN_CODE (insn) = icode = recog_memoized (insn);
943 data = XNEW (struct lra_insn_recog_data);
944 lra_insn_recog_data[uid] = data;
945 data->insn = insn;
946 data->used_insn_alternative = -1;
947 data->icode = icode;
948 data->regs = NULL;
949 if (DEBUG_INSN_P (insn))
951 data->insn_static_data = &debug_insn_static_data;
952 data->dup_loc = NULL;
953 data->arg_hard_regs = NULL;
954 data->preferred_alternatives = ALL_ALTERNATIVES;
955 data->operand_loc = XNEWVEC (rtx *, 1);
956 data->operand_loc[0] = &INSN_VAR_LOCATION_LOC (insn);
957 return data;
959 if (icode < 0)
961 int nop, nalt;
962 machine_mode operand_mode[MAX_RECOG_OPERANDS];
963 const char *constraints[MAX_RECOG_OPERANDS];
965 nop = asm_noperands (PATTERN (insn));
966 data->operand_loc = data->dup_loc = NULL;
967 nalt = 1;
968 if (nop < 0)
970 /* It is a special insn like USE or CLOBBER. We should
971 recognize any regular insn otherwise LRA can do nothing
972 with this insn. */
973 gcc_assert (GET_CODE (PATTERN (insn)) == USE
974 || GET_CODE (PATTERN (insn)) == CLOBBER
975 || GET_CODE (PATTERN (insn)) == ASM_INPUT);
976 data->insn_static_data = insn_static_data
977 = get_static_insn_data (-1, 0, 0, nalt);
979 else
981 /* expand_asm_operands makes sure there aren't too many
982 operands. */
983 lra_assert (nop <= MAX_RECOG_OPERANDS);
984 if (nop != 0)
985 data->operand_loc = XNEWVEC (rtx *, nop);
986 /* Now get the operand values and constraints out of the
987 insn. */
988 decode_asm_operands (PATTERN (insn), NULL,
989 data->operand_loc,
990 constraints, operand_mode, NULL);
991 if (nop > 0)
993 const char *p = recog_data.constraints[0];
995 for (p = constraints[0]; *p; p++)
996 nalt += *p == ',';
998 data->insn_static_data = insn_static_data
999 = get_static_insn_data (-1, nop, 0, nalt);
1000 for (i = 0; i < nop; i++)
1002 insn_static_data->operand[i].mode = operand_mode[i];
1003 insn_static_data->operand[i].constraint = constraints[i];
1004 insn_static_data->operand[i].strict_low = false;
1005 insn_static_data->operand[i].is_operator = false;
1006 insn_static_data->operand[i].is_address = false;
1009 for (i = 0; i < insn_static_data->n_operands; i++)
1010 insn_static_data->operand[i].type
1011 = (insn_static_data->operand[i].constraint[0] == '=' ? OP_OUT
1012 : insn_static_data->operand[i].constraint[0] == '+' ? OP_INOUT
1013 : OP_IN);
1014 data->preferred_alternatives = ALL_ALTERNATIVES;
1015 if (nop > 0)
1017 operand_alternative *op_alt = XCNEWVEC (operand_alternative,
1018 nalt * nop);
1019 preprocess_constraints (nop, nalt, constraints, op_alt);
1020 setup_operand_alternative (data, op_alt);
1023 else
1025 insn_extract (insn);
1026 data->insn_static_data = insn_static_data
1027 = get_static_insn_data (icode, insn_data[icode].n_operands,
1028 insn_data[icode].n_dups,
1029 insn_data[icode].n_alternatives);
1030 n = insn_static_data->n_operands;
1031 if (n == 0)
1032 locs = NULL;
1033 else
1035 locs = XNEWVEC (rtx *, n);
1036 memcpy (locs, recog_data.operand_loc, n * sizeof (rtx *));
1038 data->operand_loc = locs;
1039 n = insn_static_data->n_dups;
1040 if (n == 0)
1041 locs = NULL;
1042 else
1044 locs = XNEWVEC (rtx *, n);
1045 memcpy (locs, recog_data.dup_loc, n * sizeof (rtx *));
1047 data->dup_loc = locs;
1048 data->preferred_alternatives = get_preferred_alternatives (insn);
1049 const operand_alternative *op_alt = preprocess_insn_constraints (icode);
1050 if (!insn_static_data->operand_alternative)
1051 setup_operand_alternative (data, op_alt);
1052 else if (op_alt != insn_static_data->operand_alternative)
1053 insn_static_data->operand_alternative = op_alt;
1055 if (GET_CODE (PATTERN (insn)) == CLOBBER || GET_CODE (PATTERN (insn)) == USE)
1056 insn_static_data->hard_regs = NULL;
1057 else
1058 insn_static_data->hard_regs
1059 = collect_non_operand_hard_regs (&PATTERN (insn), data,
1060 NULL, OP_IN, false);
1061 data->arg_hard_regs = NULL;
1062 if (CALL_P (insn))
1064 rtx link;
1065 int n_hard_regs, regno, arg_hard_regs[FIRST_PSEUDO_REGISTER];
1067 n_hard_regs = 0;
1068 /* Finding implicit hard register usage. We believe it will be
1069 not changed whatever transformations are used. Call insns
1070 are such example. */
1071 for (link = CALL_INSN_FUNCTION_USAGE (insn);
1072 link != NULL_RTX;
1073 link = XEXP (link, 1))
1074 if (GET_CODE (XEXP (link, 0)) == USE
1075 && REG_P (XEXP (XEXP (link, 0), 0)))
1077 regno = REGNO (XEXP (XEXP (link, 0), 0));
1078 lra_assert (regno < FIRST_PSEUDO_REGISTER);
1079 /* It is an argument register. */
1080 for (i = REG_NREGS (XEXP (XEXP (link, 0), 0)) - 1; i >= 0; i--)
1081 arg_hard_regs[n_hard_regs++] = regno + i;
1083 if (n_hard_regs != 0)
1085 arg_hard_regs[n_hard_regs++] = -1;
1086 data->arg_hard_regs = XNEWVEC (int, n_hard_regs);
1087 memcpy (data->arg_hard_regs, arg_hard_regs,
1088 sizeof (int) * n_hard_regs);
1091 /* Some output operand can be recognized only from the context not
1092 from the constraints which are empty in this case. Call insn may
1093 contain a hard register in set destination with empty constraint
1094 and extract_insn treats them as an input. */
1095 for (i = 0; i < insn_static_data->n_operands; i++)
1097 int j;
1098 rtx pat, set;
1099 struct lra_operand_data *operand = &insn_static_data->operand[i];
1101 /* ??? Should we treat 'X' the same way. It looks to me that
1102 'X' means anything and empty constraint means we do not
1103 care. */
1104 if (operand->type != OP_IN || *operand->constraint != '\0'
1105 || operand->is_operator)
1106 continue;
1107 pat = PATTERN (insn);
1108 if (GET_CODE (pat) == SET)
1110 if (data->operand_loc[i] != &SET_DEST (pat))
1111 continue;
1113 else if (GET_CODE (pat) == PARALLEL)
1115 for (j = XVECLEN (pat, 0) - 1; j >= 0; j--)
1117 set = XVECEXP (PATTERN (insn), 0, j);
1118 if (GET_CODE (set) == SET
1119 && &SET_DEST (set) == data->operand_loc[i])
1120 break;
1122 if (j < 0)
1123 continue;
1125 else
1126 continue;
1127 operand->type = OP_OUT;
1129 return data;
1132 /* Return info about insn give by UID. The info should be already set
1133 up. */
1134 static lra_insn_recog_data_t
1135 get_insn_recog_data_by_uid (int uid)
1137 lra_insn_recog_data_t data;
1139 data = lra_insn_recog_data[uid];
1140 lra_assert (data != NULL);
1141 return data;
1144 /* Invalidate all info about insn given by its UID. */
1145 static void
1146 invalidate_insn_recog_data (int uid)
1148 lra_insn_recog_data_t data;
1150 data = lra_insn_recog_data[uid];
1151 lra_assert (data != NULL);
1152 free_insn_recog_data (data);
1153 lra_insn_recog_data[uid] = NULL;
1156 /* Update all the insn info about INSN. It is usually called when
1157 something in the insn was changed. Return the updated info. */
1158 lra_insn_recog_data_t
1159 lra_update_insn_recog_data (rtx_insn *insn)
1161 lra_insn_recog_data_t data;
1162 int n;
1163 unsigned int uid = INSN_UID (insn);
1164 struct lra_static_insn_data *insn_static_data;
1165 HOST_WIDE_INT sp_offset = 0;
1167 check_and_expand_insn_recog_data (uid);
1168 if ((data = lra_insn_recog_data[uid]) != NULL
1169 && data->icode != INSN_CODE (insn))
1171 sp_offset = data->sp_offset;
1172 invalidate_insn_data_regno_info (data, insn, get_insn_freq (insn));
1173 invalidate_insn_recog_data (uid);
1174 data = NULL;
1176 if (data == NULL)
1178 data = lra_get_insn_recog_data (insn);
1179 /* Initiate or restore SP offset. */
1180 data->sp_offset = sp_offset;
1181 return data;
1183 insn_static_data = data->insn_static_data;
1184 data->used_insn_alternative = -1;
1185 if (DEBUG_INSN_P (insn))
1186 return data;
1187 if (data->icode < 0)
1189 int nop;
1190 machine_mode operand_mode[MAX_RECOG_OPERANDS];
1191 const char *constraints[MAX_RECOG_OPERANDS];
1193 nop = asm_noperands (PATTERN (insn));
1194 if (nop >= 0)
1196 lra_assert (nop == data->insn_static_data->n_operands);
1197 /* Now get the operand values and constraints out of the
1198 insn. */
1199 decode_asm_operands (PATTERN (insn), NULL,
1200 data->operand_loc,
1201 constraints, operand_mode, NULL);
1202 #ifdef ENABLE_CHECKING
1204 int i;
1206 for (i = 0; i < nop; i++)
1207 lra_assert
1208 (insn_static_data->operand[i].mode == operand_mode[i]
1209 && insn_static_data->operand[i].constraint == constraints[i]
1210 && ! insn_static_data->operand[i].is_operator);
1212 #endif
1214 #ifdef ENABLE_CHECKING
1216 int i;
1218 for (i = 0; i < insn_static_data->n_operands; i++)
1219 lra_assert
1220 (insn_static_data->operand[i].type
1221 == (insn_static_data->operand[i].constraint[0] == '=' ? OP_OUT
1222 : insn_static_data->operand[i].constraint[0] == '+' ? OP_INOUT
1223 : OP_IN));
1225 #endif
1227 else
1229 insn_extract (insn);
1230 n = insn_static_data->n_operands;
1231 if (n != 0)
1232 memcpy (data->operand_loc, recog_data.operand_loc, n * sizeof (rtx *));
1233 n = insn_static_data->n_dups;
1234 if (n != 0)
1235 memcpy (data->dup_loc, recog_data.dup_loc, n * sizeof (rtx *));
1236 lra_assert (check_bool_attrs (insn));
1238 return data;
1241 /* Set up that INSN is using alternative ALT now. */
1242 void
1243 lra_set_used_insn_alternative (rtx_insn *insn, int alt)
1245 lra_insn_recog_data_t data;
1247 data = lra_get_insn_recog_data (insn);
1248 data->used_insn_alternative = alt;
1251 /* Set up that insn with UID is using alternative ALT now. The insn
1252 info should be already set up. */
1253 void
1254 lra_set_used_insn_alternative_by_uid (int uid, int alt)
1256 lra_insn_recog_data_t data;
1258 check_and_expand_insn_recog_data (uid);
1259 data = lra_insn_recog_data[uid];
1260 lra_assert (data != NULL);
1261 data->used_insn_alternative = alt;
1266 /* This page contains code dealing with common register info and
1267 pseudo copies. */
1269 /* The size of the following array. */
1270 static int reg_info_size;
1271 /* Common info about each register. */
1272 struct lra_reg *lra_reg_info;
1274 /* Last register value. */
1275 static int last_reg_value;
1277 /* Return new register value. */
1278 static int
1279 get_new_reg_value (void)
1281 return ++last_reg_value;
1284 /* Pools for copies. */
1285 pool_allocator<lra_copy> lra_copy::pool ("lra copies", 100);
1287 /* Vec referring to pseudo copies. */
1288 static vec<lra_copy_t> copy_vec;
1290 /* Initialize I-th element of lra_reg_info. */
1291 static inline void
1292 initialize_lra_reg_info_element (int i)
1294 bitmap_initialize (&lra_reg_info[i].insn_bitmap, &reg_obstack);
1295 #ifdef STACK_REGS
1296 lra_reg_info[i].no_stack_p = false;
1297 #endif
1298 CLEAR_HARD_REG_SET (lra_reg_info[i].conflict_hard_regs);
1299 CLEAR_HARD_REG_SET (lra_reg_info[i].actual_call_used_reg_set);
1300 lra_reg_info[i].preferred_hard_regno1 = -1;
1301 lra_reg_info[i].preferred_hard_regno2 = -1;
1302 lra_reg_info[i].preferred_hard_regno_profit1 = 0;
1303 lra_reg_info[i].preferred_hard_regno_profit2 = 0;
1304 lra_reg_info[i].biggest_mode = VOIDmode;
1305 lra_reg_info[i].live_ranges = NULL;
1306 lra_reg_info[i].nrefs = lra_reg_info[i].freq = 0;
1307 lra_reg_info[i].last_reload = 0;
1308 lra_reg_info[i].restore_regno = -1;
1309 lra_reg_info[i].val = get_new_reg_value ();
1310 lra_reg_info[i].offset = 0;
1311 lra_reg_info[i].copies = NULL;
1314 /* Initialize common reg info and copies. */
1315 static void
1316 init_reg_info (void)
1318 int i;
1320 last_reg_value = 0;
1321 reg_info_size = max_reg_num () * 3 / 2 + 1;
1322 lra_reg_info = XNEWVEC (struct lra_reg, reg_info_size);
1323 for (i = 0; i < reg_info_size; i++)
1324 initialize_lra_reg_info_element (i);
1325 copy_vec.create (100);
1329 /* Finish common reg info and copies. */
1330 static void
1331 finish_reg_info (void)
1333 int i;
1335 for (i = 0; i < reg_info_size; i++)
1336 bitmap_clear (&lra_reg_info[i].insn_bitmap);
1337 free (lra_reg_info);
1338 reg_info_size = 0;
1341 /* Expand common reg info if it is necessary. */
1342 static void
1343 expand_reg_info (void)
1345 int i, old = reg_info_size;
1347 if (reg_info_size > max_reg_num ())
1348 return;
1349 reg_info_size = max_reg_num () * 3 / 2 + 1;
1350 lra_reg_info = XRESIZEVEC (struct lra_reg, lra_reg_info, reg_info_size);
1351 for (i = old; i < reg_info_size; i++)
1352 initialize_lra_reg_info_element (i);
1355 /* Free all copies. */
1356 void
1357 lra_free_copies (void)
1359 lra_copy_t cp;
1361 while (copy_vec.length () != 0)
1363 cp = copy_vec.pop ();
1364 lra_reg_info[cp->regno1].copies = lra_reg_info[cp->regno2].copies = NULL;
1365 delete cp;
1369 /* Create copy of two pseudos REGNO1 and REGNO2. The copy execution
1370 frequency is FREQ. */
1371 void
1372 lra_create_copy (int regno1, int regno2, int freq)
1374 bool regno1_dest_p;
1375 lra_copy_t cp;
1377 lra_assert (regno1 != regno2);
1378 regno1_dest_p = true;
1379 if (regno1 > regno2)
1381 int temp = regno2;
1383 regno1_dest_p = false;
1384 regno2 = regno1;
1385 regno1 = temp;
1387 cp = new lra_copy ();
1388 copy_vec.safe_push (cp);
1389 cp->regno1_dest_p = regno1_dest_p;
1390 cp->freq = freq;
1391 cp->regno1 = regno1;
1392 cp->regno2 = regno2;
1393 cp->regno1_next = lra_reg_info[regno1].copies;
1394 lra_reg_info[regno1].copies = cp;
1395 cp->regno2_next = lra_reg_info[regno2].copies;
1396 lra_reg_info[regno2].copies = cp;
1397 if (lra_dump_file != NULL)
1398 fprintf (lra_dump_file, " Creating copy r%d%sr%d@%d\n",
1399 regno1, regno1_dest_p ? "<-" : "->", regno2, freq);
1402 /* Return N-th (0, 1, ...) copy. If there is no copy, return
1403 NULL. */
1404 lra_copy_t
1405 lra_get_copy (int n)
1407 if (n >= (int) copy_vec.length ())
1408 return NULL;
1409 return copy_vec[n];
1414 /* This page contains code dealing with info about registers in
1415 insns. */
1417 /* Process X of insn UID recursively and add info (operand type is
1418 given by TYPE, flag of that it is early clobber is EARLY_CLOBBER)
1419 about registers in X to the insn DATA. */
1420 static void
1421 add_regs_to_insn_regno_info (lra_insn_recog_data_t data, rtx x, int uid,
1422 enum op_type type, bool early_clobber)
1424 int i, j, regno;
1425 bool subreg_p;
1426 machine_mode mode;
1427 const char *fmt;
1428 enum rtx_code code;
1429 struct lra_insn_reg *curr;
1431 code = GET_CODE (x);
1432 mode = GET_MODE (x);
1433 subreg_p = false;
1434 if (GET_CODE (x) == SUBREG)
1436 x = SUBREG_REG (x);
1437 code = GET_CODE (x);
1438 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (x)))
1440 mode = GET_MODE (x);
1441 if (GET_MODE_SIZE (mode) > REGMODE_NATURAL_SIZE (mode))
1442 subreg_p = true;
1445 if (REG_P (x))
1447 regno = REGNO (x);
1448 /* Process all regs even unallocatable ones as we need info about
1449 all regs for rematerialization pass. */
1450 expand_reg_info ();
1451 if (bitmap_set_bit (&lra_reg_info[regno].insn_bitmap, uid))
1453 data->regs = new_insn_reg (data->insn, regno, type, mode, subreg_p,
1454 early_clobber, data->regs);
1455 return;
1457 else
1459 for (curr = data->regs; curr != NULL; curr = curr->next)
1460 if (curr->regno == regno)
1462 if (curr->subreg_p != subreg_p || curr->biggest_mode != mode)
1463 /* The info can not be integrated into the found
1464 structure. */
1465 data->regs = new_insn_reg (data->insn, regno, type, mode,
1466 subreg_p, early_clobber,
1467 data->regs);
1468 else
1470 if (curr->type != type)
1471 curr->type = OP_INOUT;
1472 if (curr->early_clobber != early_clobber)
1473 curr->early_clobber = true;
1475 return;
1477 gcc_unreachable ();
1481 switch (code)
1483 case SET:
1484 add_regs_to_insn_regno_info (data, SET_DEST (x), uid, OP_OUT, false);
1485 add_regs_to_insn_regno_info (data, SET_SRC (x), uid, OP_IN, false);
1486 break;
1487 case CLOBBER:
1488 /* We treat clobber of non-operand hard registers as early
1489 clobber (the behavior is expected from asm). */
1490 add_regs_to_insn_regno_info (data, XEXP (x, 0), uid, OP_OUT, true);
1491 break;
1492 case PRE_INC: case PRE_DEC: case POST_INC: case POST_DEC:
1493 add_regs_to_insn_regno_info (data, XEXP (x, 0), uid, OP_INOUT, false);
1494 break;
1495 case PRE_MODIFY: case POST_MODIFY:
1496 add_regs_to_insn_regno_info (data, XEXP (x, 0), uid, OP_INOUT, false);
1497 add_regs_to_insn_regno_info (data, XEXP (x, 1), uid, OP_IN, false);
1498 break;
1499 default:
1500 if ((code != PARALLEL && code != EXPR_LIST) || type != OP_OUT)
1501 /* Some targets place small structures in registers for return
1502 values of functions, and those registers are wrapped in
1503 PARALLEL that we may see as the destination of a SET. Here
1504 is an example:
1506 (call_insn 13 12 14 2 (set (parallel:BLK [
1507 (expr_list:REG_DEP_TRUE (reg:DI 0 ax)
1508 (const_int 0 [0]))
1509 (expr_list:REG_DEP_TRUE (reg:DI 1 dx)
1510 (const_int 8 [0x8]))
1512 (call (mem:QI (symbol_ref:DI (... */
1513 type = OP_IN;
1514 fmt = GET_RTX_FORMAT (code);
1515 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1517 if (fmt[i] == 'e')
1518 add_regs_to_insn_regno_info (data, XEXP (x, i), uid, type, false);
1519 else if (fmt[i] == 'E')
1521 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1522 add_regs_to_insn_regno_info (data, XVECEXP (x, i, j), uid,
1523 type, false);
1529 /* Return execution frequency of INSN. */
1530 static int
1531 get_insn_freq (rtx_insn *insn)
1533 basic_block bb = BLOCK_FOR_INSN (insn);
1535 gcc_checking_assert (bb != NULL);
1536 return REG_FREQ_FROM_BB (bb);
1539 /* Invalidate all reg info of INSN with DATA and execution frequency
1540 FREQ. Update common info about the invalidated registers. */
1541 static void
1542 invalidate_insn_data_regno_info (lra_insn_recog_data_t data, rtx_insn *insn,
1543 int freq)
1545 int uid;
1546 bool debug_p;
1547 unsigned int i;
1548 struct lra_insn_reg *ir, *next_ir;
1550 uid = INSN_UID (insn);
1551 debug_p = DEBUG_INSN_P (insn);
1552 for (ir = data->regs; ir != NULL; ir = next_ir)
1554 i = ir->regno;
1555 next_ir = ir->next;
1556 delete ir;
1557 bitmap_clear_bit (&lra_reg_info[i].insn_bitmap, uid);
1558 if (i >= FIRST_PSEUDO_REGISTER && ! debug_p)
1560 lra_reg_info[i].nrefs--;
1561 lra_reg_info[i].freq -= freq;
1562 lra_assert (lra_reg_info[i].nrefs >= 0 && lra_reg_info[i].freq >= 0);
1565 data->regs = NULL;
1568 /* Invalidate all reg info of INSN. Update common info about the
1569 invalidated registers. */
1570 void
1571 lra_invalidate_insn_regno_info (rtx_insn *insn)
1573 invalidate_insn_data_regno_info (lra_get_insn_recog_data (insn), insn,
1574 get_insn_freq (insn));
1577 /* Update common reg info from reg info of insn given by its DATA and
1578 execution frequency FREQ. */
1579 static void
1580 setup_insn_reg_info (lra_insn_recog_data_t data, int freq)
1582 unsigned int i;
1583 struct lra_insn_reg *ir;
1585 for (ir = data->regs; ir != NULL; ir = ir->next)
1586 if ((i = ir->regno) >= FIRST_PSEUDO_REGISTER)
1588 lra_reg_info[i].nrefs++;
1589 lra_reg_info[i].freq += freq;
1593 /* Set up insn reg info of INSN. Update common reg info from reg info
1594 of INSN. */
1595 void
1596 lra_update_insn_regno_info (rtx_insn *insn)
1598 int i, uid, freq;
1599 lra_insn_recog_data_t data;
1600 struct lra_static_insn_data *static_data;
1601 enum rtx_code code;
1602 rtx link;
1604 if (! INSN_P (insn))
1605 return;
1606 data = lra_get_insn_recog_data (insn);
1607 static_data = data->insn_static_data;
1608 freq = get_insn_freq (insn);
1609 invalidate_insn_data_regno_info (data, insn, freq);
1610 uid = INSN_UID (insn);
1611 for (i = static_data->n_operands - 1; i >= 0; i--)
1612 add_regs_to_insn_regno_info (data, *data->operand_loc[i], uid,
1613 static_data->operand[i].type,
1614 static_data->operand[i].early_clobber);
1615 if ((code = GET_CODE (PATTERN (insn))) == CLOBBER || code == USE)
1616 add_regs_to_insn_regno_info (data, XEXP (PATTERN (insn), 0), uid,
1617 code == USE ? OP_IN : OP_OUT, false);
1618 if (CALL_P (insn))
1619 /* On some targets call insns can refer to pseudos in memory in
1620 CALL_INSN_FUNCTION_USAGE list. Process them in order to
1621 consider their occurrences in calls for different
1622 transformations (e.g. inheritance) with given pseudos. */
1623 for (link = CALL_INSN_FUNCTION_USAGE (insn);
1624 link != NULL_RTX;
1625 link = XEXP (link, 1))
1626 if (((code = GET_CODE (XEXP (link, 0))) == USE || code == CLOBBER)
1627 && MEM_P (XEXP (XEXP (link, 0), 0)))
1628 add_regs_to_insn_regno_info (data, XEXP (XEXP (link, 0), 0), uid,
1629 code == USE ? OP_IN : OP_OUT, false);
1630 if (NONDEBUG_INSN_P (insn))
1631 setup_insn_reg_info (data, freq);
1634 /* Return reg info of insn given by it UID. */
1635 struct lra_insn_reg *
1636 lra_get_insn_regs (int uid)
1638 lra_insn_recog_data_t data;
1640 data = get_insn_recog_data_by_uid (uid);
1641 return data->regs;
1646 /* This page contains code dealing with stack of the insns which
1647 should be processed by the next constraint pass. */
1649 /* Bitmap used to put an insn on the stack only in one exemplar. */
1650 static sbitmap lra_constraint_insn_stack_bitmap;
1652 /* The stack itself. */
1653 vec<rtx_insn *> lra_constraint_insn_stack;
1655 /* Put INSN on the stack. If ALWAYS_UPDATE is true, always update the reg
1656 info for INSN, otherwise only update it if INSN is not already on the
1657 stack. */
1658 static inline void
1659 lra_push_insn_1 (rtx_insn *insn, bool always_update)
1661 unsigned int uid = INSN_UID (insn);
1662 if (always_update)
1663 lra_update_insn_regno_info (insn);
1664 if (uid >= SBITMAP_SIZE (lra_constraint_insn_stack_bitmap))
1665 lra_constraint_insn_stack_bitmap =
1666 sbitmap_resize (lra_constraint_insn_stack_bitmap, 3 * uid / 2, 0);
1667 if (bitmap_bit_p (lra_constraint_insn_stack_bitmap, uid))
1668 return;
1669 bitmap_set_bit (lra_constraint_insn_stack_bitmap, uid);
1670 if (! always_update)
1671 lra_update_insn_regno_info (insn);
1672 lra_constraint_insn_stack.safe_push (insn);
1675 /* Put INSN on the stack. */
1676 void
1677 lra_push_insn (rtx_insn *insn)
1679 lra_push_insn_1 (insn, false);
1682 /* Put INSN on the stack and update its reg info. */
1683 void
1684 lra_push_insn_and_update_insn_regno_info (rtx_insn *insn)
1686 lra_push_insn_1 (insn, true);
1689 /* Put insn with UID on the stack. */
1690 void
1691 lra_push_insn_by_uid (unsigned int uid)
1693 lra_push_insn (lra_insn_recog_data[uid]->insn);
1696 /* Take the last-inserted insns off the stack and return it. */
1697 rtx_insn *
1698 lra_pop_insn (void)
1700 rtx_insn *insn = lra_constraint_insn_stack.pop ();
1701 bitmap_clear_bit (lra_constraint_insn_stack_bitmap, INSN_UID (insn));
1702 return insn;
1705 /* Return the current size of the insn stack. */
1706 unsigned int
1707 lra_insn_stack_length (void)
1709 return lra_constraint_insn_stack.length ();
1712 /* Push insns FROM to TO (excluding it) going in reverse order. */
1713 static void
1714 push_insns (rtx_insn *from, rtx_insn *to)
1716 rtx_insn *insn;
1718 if (from == NULL_RTX)
1719 return;
1720 for (insn = from; insn != to; insn = PREV_INSN (insn))
1721 if (INSN_P (insn))
1722 lra_push_insn (insn);
1725 /* Set up sp offset for insn in range [FROM, LAST]. The offset is
1726 taken from the next BB insn after LAST or zero if there in such
1727 insn. */
1728 static void
1729 setup_sp_offset (rtx_insn *from, rtx_insn *last)
1731 rtx_insn *before = next_nonnote_insn_bb (last);
1732 HOST_WIDE_INT offset = (before == NULL_RTX || ! INSN_P (before)
1733 ? 0 : lra_get_insn_recog_data (before)->sp_offset);
1735 for (rtx_insn *insn = from; insn != NEXT_INSN (last); insn = NEXT_INSN (insn))
1736 lra_get_insn_recog_data (insn)->sp_offset = offset;
1739 /* Emit insns BEFORE before INSN and insns AFTER after INSN. Put the
1740 insns onto the stack. Print about emitting the insns with
1741 TITLE. */
1742 void
1743 lra_process_new_insns (rtx_insn *insn, rtx_insn *before, rtx_insn *after,
1744 const char *title)
1746 rtx_insn *last;
1748 if (before == NULL_RTX && after == NULL_RTX)
1749 return;
1750 if (lra_dump_file != NULL)
1752 dump_insn_slim (lra_dump_file, insn);
1753 if (before != NULL_RTX)
1755 fprintf (lra_dump_file," %s before:\n", title);
1756 dump_rtl_slim (lra_dump_file, before, NULL, -1, 0);
1758 if (after != NULL_RTX)
1760 fprintf (lra_dump_file, " %s after:\n", title);
1761 dump_rtl_slim (lra_dump_file, after, NULL, -1, 0);
1763 fprintf (lra_dump_file, "\n");
1765 if (before != NULL_RTX)
1767 emit_insn_before (before, insn);
1768 push_insns (PREV_INSN (insn), PREV_INSN (before));
1769 setup_sp_offset (before, PREV_INSN (insn));
1771 if (after != NULL_RTX)
1773 for (last = after; NEXT_INSN (last) != NULL_RTX; last = NEXT_INSN (last))
1775 emit_insn_after (after, insn);
1776 push_insns (last, insn);
1777 setup_sp_offset (after, last);
1783 /* Replace all references to register OLD_REGNO in *LOC with pseudo
1784 register NEW_REG. Return true if any change was made. */
1785 bool
1786 lra_substitute_pseudo (rtx *loc, int old_regno, rtx new_reg)
1788 rtx x = *loc;
1789 bool result = false;
1790 enum rtx_code code;
1791 const char *fmt;
1792 int i, j;
1794 if (x == NULL_RTX)
1795 return false;
1797 code = GET_CODE (x);
1798 if (code == REG && (int) REGNO (x) == old_regno)
1800 machine_mode mode = GET_MODE (*loc);
1801 machine_mode inner_mode = GET_MODE (new_reg);
1803 if (mode != inner_mode
1804 && ! (CONST_INT_P (new_reg) && SCALAR_INT_MODE_P (mode)))
1806 if (GET_MODE_SIZE (mode) >= GET_MODE_SIZE (inner_mode)
1807 || ! SCALAR_INT_MODE_P (inner_mode))
1808 new_reg = gen_rtx_SUBREG (mode, new_reg, 0);
1809 else
1810 new_reg = gen_lowpart_SUBREG (mode, new_reg);
1812 *loc = new_reg;
1813 return true;
1816 /* Scan all the operand sub-expressions. */
1817 fmt = GET_RTX_FORMAT (code);
1818 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1820 if (fmt[i] == 'e')
1822 if (lra_substitute_pseudo (&XEXP (x, i), old_regno, new_reg))
1823 result = true;
1825 else if (fmt[i] == 'E')
1827 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1828 if (lra_substitute_pseudo (&XVECEXP (x, i, j), old_regno, new_reg))
1829 result = true;
1832 return result;
1835 /* Call lra_substitute_pseudo within an insn. This won't update the insn ptr,
1836 just the contents of the insn. */
1837 bool
1838 lra_substitute_pseudo_within_insn (rtx_insn *insn, int old_regno, rtx new_reg)
1840 rtx loc = insn;
1841 return lra_substitute_pseudo (&loc, old_regno, new_reg);
1846 /* This page contains code dealing with scratches (changing them onto
1847 pseudos and restoring them from the pseudos).
1849 We change scratches into pseudos at the beginning of LRA to
1850 simplify dealing with them (conflicts, hard register assignments).
1852 If the pseudo denoting scratch was spilled it means that we do need
1853 a hard register for it. Such pseudos are transformed back to
1854 scratches at the end of LRA. */
1856 /* Description of location of a former scratch operand. */
1857 struct sloc
1859 rtx_insn *insn; /* Insn where the scratch was. */
1860 int nop; /* Number of the operand which was a scratch. */
1863 typedef struct sloc *sloc_t;
1865 /* Locations of the former scratches. */
1866 static vec<sloc_t> scratches;
1868 /* Bitmap of scratch regnos. */
1869 static bitmap_head scratch_bitmap;
1871 /* Bitmap of scratch operands. */
1872 static bitmap_head scratch_operand_bitmap;
1874 /* Return true if pseudo REGNO is made of SCRATCH. */
1875 bool
1876 lra_former_scratch_p (int regno)
1878 return bitmap_bit_p (&scratch_bitmap, regno);
1881 /* Return true if the operand NOP of INSN is a former scratch. */
1882 bool
1883 lra_former_scratch_operand_p (rtx_insn *insn, int nop)
1885 return bitmap_bit_p (&scratch_operand_bitmap,
1886 INSN_UID (insn) * MAX_RECOG_OPERANDS + nop) != 0;
1889 /* Register operand NOP in INSN as a former scratch. It will be
1890 changed to scratch back, if it is necessary, at the LRA end. */
1891 void
1892 lra_register_new_scratch_op (rtx_insn *insn, int nop)
1894 lra_insn_recog_data_t id = lra_get_insn_recog_data (insn);
1895 rtx op = *id->operand_loc[nop];
1896 sloc_t loc = XNEW (struct sloc);
1897 lra_assert (REG_P (op));
1898 loc->insn = insn;
1899 loc->nop = nop;
1900 scratches.safe_push (loc);
1901 bitmap_set_bit (&scratch_bitmap, REGNO (op));
1902 bitmap_set_bit (&scratch_operand_bitmap,
1903 INSN_UID (insn) * MAX_RECOG_OPERANDS + nop);
1904 add_reg_note (insn, REG_UNUSED, op);
1907 /* Change scratches onto pseudos and save their location. */
1908 static void
1909 remove_scratches (void)
1911 int i;
1912 bool insn_changed_p;
1913 basic_block bb;
1914 rtx_insn *insn;
1915 rtx reg;
1916 lra_insn_recog_data_t id;
1917 struct lra_static_insn_data *static_id;
1919 scratches.create (get_max_uid ());
1920 bitmap_initialize (&scratch_bitmap, &reg_obstack);
1921 bitmap_initialize (&scratch_operand_bitmap, &reg_obstack);
1922 FOR_EACH_BB_FN (bb, cfun)
1923 FOR_BB_INSNS (bb, insn)
1924 if (INSN_P (insn))
1926 id = lra_get_insn_recog_data (insn);
1927 static_id = id->insn_static_data;
1928 insn_changed_p = false;
1929 for (i = 0; i < static_id->n_operands; i++)
1930 if (GET_CODE (*id->operand_loc[i]) == SCRATCH
1931 && GET_MODE (*id->operand_loc[i]) != VOIDmode)
1933 insn_changed_p = true;
1934 *id->operand_loc[i] = reg
1935 = lra_create_new_reg (static_id->operand[i].mode,
1936 *id->operand_loc[i], ALL_REGS, NULL);
1937 lra_register_new_scratch_op (insn, i);
1938 if (lra_dump_file != NULL)
1939 fprintf (lra_dump_file,
1940 "Removing SCRATCH in insn #%u (nop %d)\n",
1941 INSN_UID (insn), i);
1943 if (insn_changed_p)
1944 /* Because we might use DF right after caller-saves sub-pass
1945 we need to keep DF info up to date. */
1946 df_insn_rescan (insn);
1950 /* Changes pseudos created by function remove_scratches onto scratches. */
1951 static void
1952 restore_scratches (void)
1954 int regno;
1955 unsigned i;
1956 sloc_t loc;
1957 rtx_insn *last = NULL;
1958 lra_insn_recog_data_t id = NULL;
1960 for (i = 0; scratches.iterate (i, &loc); i++)
1962 if (last != loc->insn)
1964 last = loc->insn;
1965 id = lra_get_insn_recog_data (last);
1967 if (REG_P (*id->operand_loc[loc->nop])
1968 && ((regno = REGNO (*id->operand_loc[loc->nop]))
1969 >= FIRST_PSEUDO_REGISTER)
1970 && lra_get_regno_hard_regno (regno) < 0)
1972 /* It should be only case when scratch register with chosen
1973 constraint 'X' did not get memory or hard register. */
1974 lra_assert (lra_former_scratch_p (regno));
1975 *id->operand_loc[loc->nop]
1976 = gen_rtx_SCRATCH (GET_MODE (*id->operand_loc[loc->nop]));
1977 lra_update_dup (id, loc->nop);
1978 if (lra_dump_file != NULL)
1979 fprintf (lra_dump_file, "Restoring SCRATCH in insn #%u(nop %d)\n",
1980 INSN_UID (loc->insn), loc->nop);
1983 for (i = 0; scratches.iterate (i, &loc); i++)
1984 free (loc);
1985 scratches.release ();
1986 bitmap_clear (&scratch_bitmap);
1987 bitmap_clear (&scratch_operand_bitmap);
1992 #ifdef ENABLE_CHECKING
1994 /* Function checks RTL for correctness. If FINAL_P is true, it is
1995 done at the end of LRA and the check is more rigorous. */
1996 static void
1997 check_rtl (bool final_p)
1999 basic_block bb;
2000 rtx_insn *insn;
2002 lra_assert (! final_p || reload_completed);
2003 FOR_EACH_BB_FN (bb, cfun)
2004 FOR_BB_INSNS (bb, insn)
2005 if (NONDEBUG_INSN_P (insn)
2006 && GET_CODE (PATTERN (insn)) != USE
2007 && GET_CODE (PATTERN (insn)) != CLOBBER
2008 && GET_CODE (PATTERN (insn)) != ASM_INPUT)
2010 if (final_p)
2012 #ifdef ENABLED_CHECKING
2013 extract_constrain_insn (insn);
2014 #endif
2015 continue;
2017 /* LRA code is based on assumption that all addresses can be
2018 correctly decomposed. LRA can generate reloads for
2019 decomposable addresses. The decomposition code checks the
2020 correctness of the addresses. So we don't need to check
2021 the addresses here. Don't call insn_invalid_p here, it can
2022 change the code at this stage. */
2023 if (recog_memoized (insn) < 0 && asm_noperands (PATTERN (insn)) < 0)
2024 fatal_insn_not_found (insn);
2027 #endif /* #ifdef ENABLE_CHECKING */
2029 /* Determine if the current function has an exception receiver block
2030 that reaches the exit block via non-exceptional edges */
2031 static bool
2032 has_nonexceptional_receiver (void)
2034 edge e;
2035 edge_iterator ei;
2036 basic_block *tos, *worklist, bb;
2038 /* If we're not optimizing, then just err on the safe side. */
2039 if (!optimize)
2040 return true;
2042 /* First determine which blocks can reach exit via normal paths. */
2043 tos = worklist = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun) + 1);
2045 FOR_EACH_BB_FN (bb, cfun)
2046 bb->flags &= ~BB_REACHABLE;
2048 /* Place the exit block on our worklist. */
2049 EXIT_BLOCK_PTR_FOR_FN (cfun)->flags |= BB_REACHABLE;
2050 *tos++ = EXIT_BLOCK_PTR_FOR_FN (cfun);
2052 /* Iterate: find everything reachable from what we've already seen. */
2053 while (tos != worklist)
2055 bb = *--tos;
2057 FOR_EACH_EDGE (e, ei, bb->preds)
2058 if (e->flags & EDGE_ABNORMAL)
2060 free (worklist);
2061 return true;
2063 else
2065 basic_block src = e->src;
2067 if (!(src->flags & BB_REACHABLE))
2069 src->flags |= BB_REACHABLE;
2070 *tos++ = src;
2074 free (worklist);
2075 /* No exceptional block reached exit unexceptionally. */
2076 return false;
2079 #ifdef AUTO_INC_DEC
2081 /* Process recursively X of INSN and add REG_INC notes if necessary. */
2082 static void
2083 add_auto_inc_notes (rtx_insn *insn, rtx x)
2085 enum rtx_code code = GET_CODE (x);
2086 const char *fmt;
2087 int i, j;
2089 if (code == MEM && auto_inc_p (XEXP (x, 0)))
2091 add_reg_note (insn, REG_INC, XEXP (XEXP (x, 0), 0));
2092 return;
2095 /* Scan all X sub-expressions. */
2096 fmt = GET_RTX_FORMAT (code);
2097 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2099 if (fmt[i] == 'e')
2100 add_auto_inc_notes (insn, XEXP (x, i));
2101 else if (fmt[i] == 'E')
2102 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2103 add_auto_inc_notes (insn, XVECEXP (x, i, j));
2107 #endif
2109 /* Remove all REG_DEAD and REG_UNUSED notes and regenerate REG_INC.
2110 We change pseudos by hard registers without notification of DF and
2111 that can make the notes obsolete. DF-infrastructure does not deal
2112 with REG_INC notes -- so we should regenerate them here. */
2113 static void
2114 update_inc_notes (void)
2116 rtx *pnote;
2117 basic_block bb;
2118 rtx_insn *insn;
2120 FOR_EACH_BB_FN (bb, cfun)
2121 FOR_BB_INSNS (bb, insn)
2122 if (NONDEBUG_INSN_P (insn))
2124 pnote = &REG_NOTES (insn);
2125 while (*pnote != 0)
2127 if (REG_NOTE_KIND (*pnote) == REG_DEAD
2128 || REG_NOTE_KIND (*pnote) == REG_UNUSED
2129 || REG_NOTE_KIND (*pnote) == REG_INC)
2130 *pnote = XEXP (*pnote, 1);
2131 else
2132 pnote = &XEXP (*pnote, 1);
2134 #ifdef AUTO_INC_DEC
2135 add_auto_inc_notes (insn, PATTERN (insn));
2136 #endif
2140 /* Set to 1 while in lra. */
2141 int lra_in_progress;
2143 /* Start of pseudo regnos before the LRA. */
2144 int lra_new_regno_start;
2146 /* Start of reload pseudo regnos before the new spill pass. */
2147 int lra_constraint_new_regno_start;
2149 /* Avoid spilling pseudos with regno more than the following value if
2150 it is possible. */
2151 int lra_bad_spill_regno_start;
2153 /* Inheritance pseudo regnos before the new spill pass. */
2154 bitmap_head lra_inheritance_pseudos;
2156 /* Split regnos before the new spill pass. */
2157 bitmap_head lra_split_regs;
2159 /* Reload pseudo regnos before the new assignmnet pass which still can
2160 be spilled after the assinment pass as memory is also accepted in
2161 insns for the reload pseudos. */
2162 bitmap_head lra_optional_reload_pseudos;
2164 /* Pseudo regnos used for subreg reloads before the new assignment
2165 pass. Such pseudos still can be spilled after the assinment
2166 pass. */
2167 bitmap_head lra_subreg_reload_pseudos;
2169 /* File used for output of LRA debug information. */
2170 FILE *lra_dump_file;
2172 /* True if we should try spill into registers of different classes
2173 instead of memory. */
2174 bool lra_reg_spill_p;
2176 /* Set up value LRA_REG_SPILL_P. */
2177 static void
2178 setup_reg_spill_flag (void)
2180 int cl, mode;
2182 if (targetm.spill_class != NULL)
2183 for (cl = 0; cl < (int) LIM_REG_CLASSES; cl++)
2184 for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
2185 if (targetm.spill_class ((enum reg_class) cl,
2186 (machine_mode) mode) != NO_REGS)
2188 lra_reg_spill_p = true;
2189 return;
2191 lra_reg_spill_p = false;
2194 /* True if the current function is too big to use regular algorithms
2195 in LRA. In other words, we should use simpler and faster algorithms
2196 in LRA. It also means we should not worry about generation code
2197 for caller saves. The value is set up in IRA. */
2198 bool lra_simple_p;
2200 /* Major LRA entry function. F is a file should be used to dump LRA
2201 debug info. */
2202 void
2203 lra (FILE *f)
2205 int i;
2206 bool live_p, scratch_p, inserted_p;
2208 lra_dump_file = f;
2210 timevar_push (TV_LRA);
2212 /* Make sure that the last insn is a note. Some subsequent passes
2213 need it. */
2214 emit_note (NOTE_INSN_DELETED);
2216 COPY_HARD_REG_SET (lra_no_alloc_regs, ira_no_alloc_regs);
2218 init_reg_info ();
2219 expand_reg_info ();
2221 init_insn_recog_data ();
2223 #ifdef ENABLE_CHECKING
2224 /* Some quick check on RTL generated by previous passes. */
2225 check_rtl (false);
2226 #endif
2228 lra_in_progress = 1;
2230 lra_live_range_iter = lra_coalesce_iter = lra_constraint_iter = 0;
2231 lra_assignment_iter = lra_assignment_iter_after_spill = 0;
2232 lra_inheritance_iter = lra_undo_inheritance_iter = 0;
2233 lra_rematerialization_iter = 0;
2235 setup_reg_spill_flag ();
2237 /* Function remove_scratches can creates new pseudos for clobbers --
2238 so set up lra_constraint_new_regno_start before its call to
2239 permit changing reg classes for pseudos created by this
2240 simplification. */
2241 lra_constraint_new_regno_start = lra_new_regno_start = max_reg_num ();
2242 lra_bad_spill_regno_start = INT_MAX;
2243 remove_scratches ();
2244 scratch_p = lra_constraint_new_regno_start != max_reg_num ();
2246 /* A function that has a non-local label that can reach the exit
2247 block via non-exceptional paths must save all call-saved
2248 registers. */
2249 if (cfun->has_nonlocal_label && has_nonexceptional_receiver ())
2250 crtl->saves_all_registers = 1;
2252 if (crtl->saves_all_registers)
2253 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2254 if (! call_used_regs[i] && ! fixed_regs[i] && ! LOCAL_REGNO (i))
2255 df_set_regs_ever_live (i, true);
2257 /* We don't DF from now and avoid its using because it is to
2258 expensive when a lot of RTL changes are made. */
2259 df_set_flags (DF_NO_INSN_RESCAN);
2260 lra_constraint_insn_stack.create (get_max_uid ());
2261 lra_constraint_insn_stack_bitmap = sbitmap_alloc (get_max_uid ());
2262 bitmap_clear (lra_constraint_insn_stack_bitmap);
2263 lra_live_ranges_init ();
2264 lra_constraints_init ();
2265 lra_curr_reload_num = 0;
2266 push_insns (get_last_insn (), NULL);
2267 /* It is needed for the 1st coalescing. */
2268 bitmap_initialize (&lra_inheritance_pseudos, &reg_obstack);
2269 bitmap_initialize (&lra_split_regs, &reg_obstack);
2270 bitmap_initialize (&lra_optional_reload_pseudos, &reg_obstack);
2271 bitmap_initialize (&lra_subreg_reload_pseudos, &reg_obstack);
2272 live_p = false;
2273 if (get_frame_size () != 0 && crtl->stack_alignment_needed)
2274 /* If we have a stack frame, we must align it now. The stack size
2275 may be a part of the offset computation for register
2276 elimination. */
2277 assign_stack_local (BLKmode, 0, crtl->stack_alignment_needed);
2278 lra_init_equiv ();
2279 for (;;)
2281 for (;;)
2283 /* We should try to assign hard registers to scratches even
2284 if there were no RTL transformations in
2285 lra_constraints. */
2286 if (! lra_constraints (lra_constraint_iter == 0)
2287 && (lra_constraint_iter > 1
2288 || (! scratch_p && ! caller_save_needed)))
2289 break;
2290 /* Constraint transformations may result in that eliminable
2291 hard regs become uneliminable and pseudos which use them
2292 should be spilled. It is better to do it before pseudo
2293 assignments.
2295 For example, rs6000 can make
2296 RS6000_PIC_OFFSET_TABLE_REGNUM uneliminable if we started
2297 to use a constant pool. */
2298 lra_eliminate (false, false);
2299 /* Do inheritance only for regular algorithms. */
2300 if (! lra_simple_p)
2302 if (flag_ipa_ra)
2304 if (live_p)
2305 lra_clear_live_ranges ();
2306 /* As a side-effect of lra_create_live_ranges, we calculate
2307 actual_call_used_reg_set, which is needed during
2308 lra_inheritance. */
2309 lra_create_live_ranges (true, true);
2310 live_p = true;
2312 lra_inheritance ();
2314 if (live_p)
2315 lra_clear_live_ranges ();
2316 /* We need live ranges for lra_assign -- so build them. But
2317 don't remove dead insns or change global live info as we
2318 can undo inheritance transformations after inheritance
2319 pseudo assigning. */
2320 lra_create_live_ranges (true, false);
2321 live_p = true;
2322 /* If we don't spill non-reload and non-inheritance pseudos,
2323 there is no sense to run memory-memory move coalescing.
2324 If inheritance pseudos were spilled, the memory-memory
2325 moves involving them will be removed by pass undoing
2326 inheritance. */
2327 if (lra_simple_p)
2328 lra_assign ();
2329 else
2331 bool spill_p = !lra_assign ();
2333 if (lra_undo_inheritance ())
2334 live_p = false;
2335 if (spill_p)
2337 if (! live_p)
2339 lra_create_live_ranges (true, true);
2340 live_p = true;
2342 if (lra_coalesce ())
2343 live_p = false;
2345 if (! live_p)
2346 lra_clear_live_ranges ();
2349 /* Don't clear optional reloads bitmap until all constraints are
2350 satisfied as we need to differ them from regular reloads. */
2351 bitmap_clear (&lra_optional_reload_pseudos);
2352 bitmap_clear (&lra_subreg_reload_pseudos);
2353 bitmap_clear (&lra_inheritance_pseudos);
2354 bitmap_clear (&lra_split_regs);
2355 if (! live_p)
2357 /* We need full live info for spilling pseudos into
2358 registers instead of memory. */
2359 lra_create_live_ranges (lra_reg_spill_p, true);
2360 live_p = true;
2362 /* We should check necessity for spilling here as the above live
2363 range pass can remove spilled pseudos. */
2364 if (! lra_need_for_spills_p ())
2365 break;
2366 /* Now we know what pseudos should be spilled. Try to
2367 rematerialize them first. */
2368 if (lra_remat ())
2370 /* We need full live info -- see the comment above. */
2371 lra_create_live_ranges (lra_reg_spill_p, true);
2372 live_p = true;
2373 if (! lra_need_for_spills_p ())
2374 break;
2376 lra_spill ();
2377 /* Assignment of stack slots changes elimination offsets for
2378 some eliminations. So update the offsets here. */
2379 lra_eliminate (false, false);
2380 lra_constraint_new_regno_start = max_reg_num ();
2381 if (lra_bad_spill_regno_start == INT_MAX
2382 && lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES
2383 && lra_rematerialization_iter > LRA_MAX_REMATERIALIZATION_PASSES)
2384 /* After switching off inheritance and rematerialization
2385 passes, avoid spilling reload pseudos will be created to
2386 prevent LRA cycling in some complicated cases. */
2387 lra_bad_spill_regno_start = lra_constraint_new_regno_start;
2388 lra_assignment_iter_after_spill = 0;
2390 restore_scratches ();
2391 lra_eliminate (true, false);
2392 lra_final_code_change ();
2393 lra_in_progress = 0;
2394 if (live_p)
2395 lra_clear_live_ranges ();
2396 lra_live_ranges_finish ();
2397 lra_constraints_finish ();
2398 finish_reg_info ();
2399 sbitmap_free (lra_constraint_insn_stack_bitmap);
2400 lra_constraint_insn_stack.release ();
2401 finish_insn_recog_data ();
2402 regstat_free_n_sets_and_refs ();
2403 regstat_free_ri ();
2404 reload_completed = 1;
2405 update_inc_notes ();
2407 inserted_p = fixup_abnormal_edges ();
2409 /* We've possibly turned single trapping insn into multiple ones. */
2410 if (cfun->can_throw_non_call_exceptions)
2412 sbitmap blocks;
2413 blocks = sbitmap_alloc (last_basic_block_for_fn (cfun));
2414 bitmap_ones (blocks);
2415 find_many_sub_basic_blocks (blocks);
2416 sbitmap_free (blocks);
2419 if (inserted_p)
2420 commit_edge_insertions ();
2422 /* Replacing pseudos with their memory equivalents might have
2423 created shared rtx. Subsequent passes would get confused
2424 by this, so unshare everything here. */
2425 unshare_all_rtl_again (get_insns ());
2427 #ifdef ENABLE_CHECKING
2428 check_rtl (true);
2429 #endif
2431 timevar_pop (TV_LRA);
2434 /* Called once per compiler to initialize LRA data once. */
2435 void
2436 lra_init_once (void)
2438 init_insn_code_data_once ();
2441 /* Called once per compiler to finish LRA data which are initialize
2442 once. */
2443 void
2444 lra_finish_once (void)
2446 finish_insn_code_data_once ();