re PR target/66258 (compiling a stdarg function with arch +nofp generates an ICE)
[official-gcc.git] / gcc / lra.c
blob456f618203dcdc5184a28c76ede54bced2b4cc12
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 "hashtab.h"
118 #include "hash-set.h"
119 #include "vec.h"
120 #include "machmode.h"
121 #include "input.h"
122 #include "function.h"
123 #include "symtab.h"
124 #include "wide-int.h"
125 #include "inchash.h"
126 #include "tree.h"
127 #include "optabs.h"
128 #include "statistics.h"
129 #include "double-int.h"
130 #include "real.h"
131 #include "fixed-value.h"
132 #include "alias.h"
133 #include "expmed.h"
134 #include "dojump.h"
135 #include "explow.h"
136 #include "calls.h"
137 #include "emit-rtl.h"
138 #include "varasm.h"
139 #include "stmt.h"
140 #include "expr.h"
141 #include "predict.h"
142 #include "dominance.h"
143 #include "cfg.h"
144 #include "cfgrtl.h"
145 #include "cfgbuild.h"
146 #include "basic-block.h"
147 #include "except.h"
148 #include "tree-pass.h"
149 #include "timevar.h"
150 #include "target.h"
151 #include "ira.h"
152 #include "alloc-pool.h"
153 #include "lra-int.h"
154 #include "df.h"
156 /* Dump bitmap SET with TITLE and BB INDEX. */
157 void
158 lra_dump_bitmap_with_title (const char *title, bitmap set, int index)
160 unsigned int i;
161 int count;
162 bitmap_iterator bi;
163 static const int max_nums_on_line = 10;
165 if (bitmap_empty_p (set))
166 return;
167 fprintf (lra_dump_file, " %s %d:", title, index);
168 fprintf (lra_dump_file, "\n");
169 count = max_nums_on_line + 1;
170 EXECUTE_IF_SET_IN_BITMAP (set, 0, i, bi)
172 if (count > max_nums_on_line)
174 fprintf (lra_dump_file, "\n ");
175 count = 0;
177 fprintf (lra_dump_file, " %4u", i);
178 count++;
180 fprintf (lra_dump_file, "\n");
183 /* Hard registers currently not available for allocation. It can
184 changed after some hard registers become not eliminable. */
185 HARD_REG_SET lra_no_alloc_regs;
187 static int get_new_reg_value (void);
188 static void expand_reg_info (void);
189 static void invalidate_insn_recog_data (int);
190 static int get_insn_freq (rtx_insn *);
191 static void invalidate_insn_data_regno_info (lra_insn_recog_data_t,
192 rtx_insn *, int);
194 /* Expand all regno related info needed for LRA. */
195 static void
196 expand_reg_data (int old)
198 resize_reg_info ();
199 expand_reg_info ();
200 ira_expand_reg_equiv ();
201 for (int i = (int) max_reg_num () - 1; i >= old; i--)
202 lra_change_class (i, ALL_REGS, " Set", true);
205 /* Create and return a new reg of ORIGINAL mode. If ORIGINAL is NULL
206 or of VOIDmode, use MD_MODE for the new reg. Initialize its
207 register class to RCLASS. Print message about assigning class
208 RCLASS containing new register name TITLE unless it is NULL. Use
209 attributes of ORIGINAL if it is a register. The created register
210 will have unique held value. */
212 lra_create_new_reg_with_unique_value (machine_mode md_mode, rtx original,
213 enum reg_class rclass, const char *title)
215 machine_mode mode;
216 rtx new_reg;
218 if (original == NULL_RTX || (mode = GET_MODE (original)) == VOIDmode)
219 mode = md_mode;
220 lra_assert (mode != VOIDmode);
221 new_reg = gen_reg_rtx (mode);
222 if (original == NULL_RTX || ! REG_P (original))
224 if (lra_dump_file != NULL)
225 fprintf (lra_dump_file, " Creating newreg=%i", REGNO (new_reg));
227 else
229 if (ORIGINAL_REGNO (original) >= FIRST_PSEUDO_REGISTER)
230 ORIGINAL_REGNO (new_reg) = ORIGINAL_REGNO (original);
231 REG_USERVAR_P (new_reg) = REG_USERVAR_P (original);
232 REG_POINTER (new_reg) = REG_POINTER (original);
233 REG_ATTRS (new_reg) = REG_ATTRS (original);
234 if (lra_dump_file != NULL)
235 fprintf (lra_dump_file, " Creating newreg=%i from oldreg=%i",
236 REGNO (new_reg), REGNO (original));
238 if (lra_dump_file != NULL)
240 if (title != NULL)
241 fprintf (lra_dump_file, ", assigning class %s to%s%s r%d",
242 reg_class_names[rclass], *title == '\0' ? "" : " ",
243 title, REGNO (new_reg));
244 fprintf (lra_dump_file, "\n");
246 expand_reg_data (max_reg_num ());
247 setup_reg_classes (REGNO (new_reg), rclass, NO_REGS, rclass);
248 return new_reg;
251 /* Analogous to the previous function but also inherits value of
252 ORIGINAL. */
254 lra_create_new_reg (machine_mode md_mode, rtx original,
255 enum reg_class rclass, const char *title)
257 rtx new_reg;
259 new_reg
260 = lra_create_new_reg_with_unique_value (md_mode, original, rclass, title);
261 if (original != NULL_RTX && REG_P (original))
262 lra_assign_reg_val (REGNO (original), REGNO (new_reg));
263 return new_reg;
266 /* Set up for REGNO unique hold value. */
267 void
268 lra_set_regno_unique_value (int regno)
270 lra_reg_info[regno].val = get_new_reg_value ();
273 /* Invalidate INSN related info used by LRA. The info should never be
274 used after that. */
275 void
276 lra_invalidate_insn_data (rtx_insn *insn)
278 lra_invalidate_insn_regno_info (insn);
279 invalidate_insn_recog_data (INSN_UID (insn));
282 /* Mark INSN deleted and invalidate the insn related info used by
283 LRA. */
284 void
285 lra_set_insn_deleted (rtx_insn *insn)
287 lra_invalidate_insn_data (insn);
288 SET_INSN_DELETED (insn);
291 /* Delete an unneeded INSN and any previous insns who sole purpose is
292 loading data that is dead in INSN. */
293 void
294 lra_delete_dead_insn (rtx_insn *insn)
296 rtx_insn *prev = prev_real_insn (insn);
297 rtx prev_dest;
299 /* If the previous insn sets a register that dies in our insn,
300 delete it too. */
301 if (prev && GET_CODE (PATTERN (prev)) == SET
302 && (prev_dest = SET_DEST (PATTERN (prev)), REG_P (prev_dest))
303 && reg_mentioned_p (prev_dest, PATTERN (insn))
304 && find_regno_note (insn, REG_DEAD, REGNO (prev_dest))
305 && ! side_effects_p (SET_SRC (PATTERN (prev))))
306 lra_delete_dead_insn (prev);
308 lra_set_insn_deleted (insn);
311 /* Emit insn x = y + z. Return NULL if we failed to do it.
312 Otherwise, return the insn. We don't use gen_add3_insn as it might
313 clobber CC. */
314 static rtx
315 emit_add3_insn (rtx x, rtx y, rtx z)
317 rtx_insn *last;
319 last = get_last_insn ();
321 if (have_addptr3_insn (x, y, z))
323 rtx insn = gen_addptr3_insn (x, y, z);
325 /* If the target provides an "addptr" pattern it hopefully does
326 for a reason. So falling back to the normal add would be
327 a bug. */
328 lra_assert (insn != NULL_RTX);
329 emit_insn (insn);
330 return insn;
333 rtx_insn *insn = emit_insn (gen_rtx_SET (x, gen_rtx_PLUS (GET_MODE (y),
334 y, z)));
335 if (recog_memoized (insn) < 0)
337 delete_insns_since (last);
338 insn = NULL;
340 return insn;
343 /* Emit insn x = x + y. Return the insn. We use gen_add2_insn as the
344 last resort. */
345 static rtx
346 emit_add2_insn (rtx x, rtx y)
348 rtx insn;
350 insn = emit_add3_insn (x, x, y);
351 if (insn == NULL_RTX)
353 insn = gen_add2_insn (x, y);
354 if (insn != NULL_RTX)
355 emit_insn (insn);
357 return insn;
360 /* Target checks operands through operand predicates to recognize an
361 insn. We should have a special precaution to generate add insns
362 which are frequent results of elimination.
364 Emit insns for x = y + z. X can be used to store intermediate
365 values and should be not in Y and Z when we use X to store an
366 intermediate value. Y + Z should form [base] [+ index[ * scale]] [
367 + disp] where base and index are registers, disp and scale are
368 constants. Y should contain base if it is present, Z should
369 contain disp if any. index[*scale] can be part of Y or Z. */
370 void
371 lra_emit_add (rtx x, rtx y, rtx z)
373 int old;
374 rtx_insn *last;
375 rtx a1, a2, base, index, disp, scale, index_scale;
376 bool ok_p;
378 rtx add3_insn = emit_add3_insn (x, y, z);
379 old = max_reg_num ();
380 if (add3_insn != NULL)
382 else
384 disp = a2 = NULL_RTX;
385 if (GET_CODE (y) == PLUS)
387 a1 = XEXP (y, 0);
388 a2 = XEXP (y, 1);
389 disp = z;
391 else
393 a1 = y;
394 if (CONSTANT_P (z))
395 disp = z;
396 else
397 a2 = z;
399 index_scale = scale = NULL_RTX;
400 if (GET_CODE (a1) == MULT)
402 index_scale = a1;
403 index = XEXP (a1, 0);
404 scale = XEXP (a1, 1);
405 base = a2;
407 else if (a2 != NULL_RTX && GET_CODE (a2) == MULT)
409 index_scale = a2;
410 index = XEXP (a2, 0);
411 scale = XEXP (a2, 1);
412 base = a1;
414 else
416 base = a1;
417 index = a2;
419 if (! (REG_P (base) || GET_CODE (base) == SUBREG)
420 || (index != NULL_RTX
421 && ! (REG_P (index) || GET_CODE (index) == SUBREG))
422 || (disp != NULL_RTX && ! CONSTANT_P (disp))
423 || (scale != NULL_RTX && ! CONSTANT_P (scale)))
425 /* Probably we have no 3 op add. Last chance is to use 2-op
426 add insn. To succeed, don't move Z to X as an address
427 segment always comes in Y. Otherwise, we might fail when
428 adding the address segment to register. */
429 lra_assert (x != y && x != z);
430 emit_move_insn (x, y);
431 rtx insn = emit_add2_insn (x, z);
432 lra_assert (insn != NULL_RTX);
434 else
436 if (index_scale == NULL_RTX)
437 index_scale = index;
438 if (disp == NULL_RTX)
440 /* Generate x = index_scale; x = x + base. */
441 lra_assert (index_scale != NULL_RTX && base != NULL_RTX);
442 emit_move_insn (x, index_scale);
443 rtx insn = emit_add2_insn (x, base);
444 lra_assert (insn != NULL_RTX);
446 else if (scale == NULL_RTX)
448 /* Try x = base + disp. */
449 lra_assert (base != NULL_RTX);
450 last = get_last_insn ();
451 rtx_insn *move_insn =
452 emit_move_insn (x, gen_rtx_PLUS (GET_MODE (base), base, disp));
453 if (recog_memoized (move_insn) < 0)
455 delete_insns_since (last);
456 /* Generate x = disp; x = x + base. */
457 emit_move_insn (x, disp);
458 rtx add2_insn = emit_add2_insn (x, base);
459 lra_assert (add2_insn != NULL_RTX);
461 /* Generate x = x + index. */
462 if (index != NULL_RTX)
464 rtx insn = emit_add2_insn (x, index);
465 lra_assert (insn != NULL_RTX);
468 else
470 /* Try x = index_scale; x = x + disp; x = x + base. */
471 last = get_last_insn ();
472 rtx_insn *move_insn = emit_move_insn (x, index_scale);
473 ok_p = false;
474 if (recog_memoized (move_insn) >= 0)
476 rtx insn = emit_add2_insn (x, disp);
477 if (insn != NULL_RTX)
479 insn = emit_add2_insn (x, base);
480 if (insn != NULL_RTX)
481 ok_p = true;
484 if (! ok_p)
486 delete_insns_since (last);
487 /* Generate x = disp; x = x + base; x = x + index_scale. */
488 emit_move_insn (x, disp);
489 rtx insn = emit_add2_insn (x, base);
490 lra_assert (insn != NULL_RTX);
491 insn = emit_add2_insn (x, index_scale);
492 lra_assert (insn != NULL_RTX);
497 /* Functions emit_... can create pseudos -- so expand the pseudo
498 data. */
499 if (old != max_reg_num ())
500 expand_reg_data (old);
503 /* The number of emitted reload insns so far. */
504 int lra_curr_reload_num;
506 /* Emit x := y, processing special case when y = u + v or y = u + v *
507 scale + w through emit_add (Y can be an address which is base +
508 index reg * scale + displacement in general case). X may be used
509 as intermediate result therefore it should be not in Y. */
510 void
511 lra_emit_move (rtx x, rtx y)
513 int old;
515 if (GET_CODE (y) != PLUS)
517 if (rtx_equal_p (x, y))
518 return;
519 old = max_reg_num ();
520 emit_move_insn (x, y);
521 if (REG_P (x))
522 lra_reg_info[ORIGINAL_REGNO (x)].last_reload = ++lra_curr_reload_num;
523 /* Function emit_move can create pseudos -- so expand the pseudo
524 data. */
525 if (old != max_reg_num ())
526 expand_reg_data (old);
527 return;
529 lra_emit_add (x, XEXP (y, 0), XEXP (y, 1));
532 /* Update insn operands which are duplication of operands whose
533 numbers are in array of NOPS (with end marker -1). The insn is
534 represented by its LRA internal representation ID. */
535 void
536 lra_update_dups (lra_insn_recog_data_t id, signed char *nops)
538 int i, j, nop;
539 struct lra_static_insn_data *static_id = id->insn_static_data;
541 for (i = 0; i < static_id->n_dups; i++)
542 for (j = 0; (nop = nops[j]) >= 0; j++)
543 if (static_id->dup_num[i] == nop)
544 *id->dup_loc[i] = *id->operand_loc[nop];
549 /* This page contains code dealing with info about registers in the
550 insns. */
552 /* Pools for insn reg info. */
553 pool_allocator<lra_insn_reg> lra_insn_reg::pool ("insn regs", 100);
555 /* Create LRA insn related info about a reference to REGNO in INSN with
556 TYPE (in/out/inout), biggest reference mode MODE, flag that it is
557 reference through subreg (SUBREG_P), flag that is early clobbered
558 in the insn (EARLY_CLOBBER), and reference to the next insn reg
559 info (NEXT). */
560 static struct lra_insn_reg *
561 new_insn_reg (rtx_insn *insn, int regno, enum op_type type,
562 machine_mode mode,
563 bool subreg_p, bool early_clobber, struct lra_insn_reg *next)
565 lra_insn_reg *ir = new lra_insn_reg ();
566 ir->type = type;
567 ir->biggest_mode = mode;
568 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (lra_reg_info[regno].biggest_mode)
569 && NONDEBUG_INSN_P (insn))
570 lra_reg_info[regno].biggest_mode = mode;
571 ir->subreg_p = subreg_p;
572 ir->early_clobber = early_clobber;
573 ir->regno = regno;
574 ir->next = next;
575 return ir;
578 /* Free insn reg info list IR. */
579 static void
580 free_insn_regs (struct lra_insn_reg *ir)
582 struct lra_insn_reg *next_ir;
584 for (; ir != NULL; ir = next_ir)
586 next_ir = ir->next;
587 delete ir;
591 /* Finish pool for insn reg info. */
592 static void
593 finish_insn_regs (void)
595 lra_insn_reg::pool.release ();
600 /* This page contains code dealing LRA insn info (or in other words
601 LRA internal insn representation). */
603 /* Map INSN_CODE -> the static insn data. This info is valid during
604 all translation unit. */
605 struct lra_static_insn_data *insn_code_data[LAST_INSN_CODE];
607 /* Debug insns are represented as a special insn with one input
608 operand which is RTL expression in var_location. */
610 /* The following data are used as static insn operand data for all
611 debug insns. If structure lra_operand_data is changed, the
612 initializer should be changed too. */
613 static struct lra_operand_data debug_operand_data =
615 NULL, /* alternative */
616 VOIDmode, /* We are not interesting in the operand mode. */
617 OP_IN,
618 0, 0, 0, 0
621 /* The following data are used as static insn data for all debug
622 insns. If structure lra_static_insn_data is changed, the
623 initializer should be changed too. */
624 static struct lra_static_insn_data debug_insn_static_data =
626 &debug_operand_data,
627 0, /* Duplication operands #. */
628 -1, /* Commutative operand #. */
629 1, /* Operands #. There is only one operand which is debug RTL
630 expression. */
631 0, /* Duplications #. */
632 0, /* Alternatives #. We are not interesting in alternatives
633 because we does not proceed debug_insns for reloads. */
634 NULL, /* Hard registers referenced in machine description. */
635 NULL /* Descriptions of operands in alternatives. */
638 /* Called once per compiler work to initialize some LRA data related
639 to insns. */
640 static void
641 init_insn_code_data_once (void)
643 memset (insn_code_data, 0, sizeof (insn_code_data));
646 /* Called once per compiler work to finalize some LRA data related to
647 insns. */
648 static void
649 finish_insn_code_data_once (void)
651 int i;
653 for (i = 0; i < LAST_INSN_CODE; i++)
655 if (insn_code_data[i] != NULL)
656 free (insn_code_data[i]);
660 /* Return static insn data, allocate and setup if necessary. Although
661 dup_num is static data (it depends only on icode), to set it up we
662 need to extract insn first. So recog_data should be valid for
663 normal insn (ICODE >= 0) before the call. */
664 static struct lra_static_insn_data *
665 get_static_insn_data (int icode, int nop, int ndup, int nalt)
667 struct lra_static_insn_data *data;
668 size_t n_bytes;
670 lra_assert (icode < LAST_INSN_CODE);
671 if (icode >= 0 && (data = insn_code_data[icode]) != NULL)
672 return data;
673 lra_assert (nop >= 0 && ndup >= 0 && nalt >= 0);
674 n_bytes = sizeof (struct lra_static_insn_data)
675 + sizeof (struct lra_operand_data) * nop
676 + sizeof (int) * ndup;
677 data = XNEWVAR (struct lra_static_insn_data, n_bytes);
678 data->operand_alternative = NULL;
679 data->n_operands = nop;
680 data->n_dups = ndup;
681 data->n_alternatives = nalt;
682 data->operand = ((struct lra_operand_data *)
683 ((char *) data + sizeof (struct lra_static_insn_data)));
684 data->dup_num = ((int *) ((char *) data->operand
685 + sizeof (struct lra_operand_data) * nop));
686 if (icode >= 0)
688 int i;
690 insn_code_data[icode] = data;
691 for (i = 0; i < nop; i++)
693 data->operand[i].constraint
694 = insn_data[icode].operand[i].constraint;
695 data->operand[i].mode = insn_data[icode].operand[i].mode;
696 data->operand[i].strict_low = insn_data[icode].operand[i].strict_low;
697 data->operand[i].is_operator
698 = insn_data[icode].operand[i].is_operator;
699 data->operand[i].type
700 = (data->operand[i].constraint[0] == '=' ? OP_OUT
701 : data->operand[i].constraint[0] == '+' ? OP_INOUT
702 : OP_IN);
703 data->operand[i].is_address = false;
705 for (i = 0; i < ndup; i++)
706 data->dup_num[i] = recog_data.dup_num[i];
708 return data;
711 /* The current length of the following array. */
712 int lra_insn_recog_data_len;
714 /* Map INSN_UID -> the insn recog data (NULL if unknown). */
715 lra_insn_recog_data_t *lra_insn_recog_data;
717 /* Initialize LRA data about insns. */
718 static void
719 init_insn_recog_data (void)
721 lra_insn_recog_data_len = 0;
722 lra_insn_recog_data = NULL;
725 /* Expand, if necessary, LRA data about insns. */
726 static void
727 check_and_expand_insn_recog_data (int index)
729 int i, old;
731 if (lra_insn_recog_data_len > index)
732 return;
733 old = lra_insn_recog_data_len;
734 lra_insn_recog_data_len = index * 3 / 2 + 1;
735 lra_insn_recog_data = XRESIZEVEC (lra_insn_recog_data_t,
736 lra_insn_recog_data,
737 lra_insn_recog_data_len);
738 for (i = old; i < lra_insn_recog_data_len; i++)
739 lra_insn_recog_data[i] = NULL;
742 /* Finish LRA DATA about insn. */
743 static void
744 free_insn_recog_data (lra_insn_recog_data_t data)
746 if (data->operand_loc != NULL)
747 free (data->operand_loc);
748 if (data->dup_loc != NULL)
749 free (data->dup_loc);
750 if (data->arg_hard_regs != NULL)
751 free (data->arg_hard_regs);
752 if (data->icode < 0 && NONDEBUG_INSN_P (data->insn))
754 if (data->insn_static_data->operand_alternative != NULL)
755 free (const_cast <operand_alternative *>
756 (data->insn_static_data->operand_alternative));
757 free_insn_regs (data->insn_static_data->hard_regs);
758 free (data->insn_static_data);
760 free_insn_regs (data->regs);
761 data->regs = NULL;
762 free (data);
765 /* Finish LRA data about all insns. */
766 static void
767 finish_insn_recog_data (void)
769 int i;
770 lra_insn_recog_data_t data;
772 for (i = 0; i < lra_insn_recog_data_len; i++)
773 if ((data = lra_insn_recog_data[i]) != NULL)
774 free_insn_recog_data (data);
775 finish_insn_regs ();
776 lra_copy::pool.release ();
777 lra_insn_reg::pool.release ();
778 free (lra_insn_recog_data);
781 /* Setup info about operands in alternatives of LRA DATA of insn. */
782 static void
783 setup_operand_alternative (lra_insn_recog_data_t data,
784 const operand_alternative *op_alt)
786 int i, j, nop, nalt;
787 int icode = data->icode;
788 struct lra_static_insn_data *static_data = data->insn_static_data;
790 static_data->commutative = -1;
791 nop = static_data->n_operands;
792 nalt = static_data->n_alternatives;
793 static_data->operand_alternative = op_alt;
794 for (i = 0; i < nop; i++)
796 static_data->operand[i].early_clobber = false;
797 static_data->operand[i].is_address = false;
798 if (static_data->operand[i].constraint[0] == '%')
800 /* We currently only support one commutative pair of operands. */
801 if (static_data->commutative < 0)
802 static_data->commutative = i;
803 else
804 lra_assert (icode < 0); /* Asm */
805 /* The last operand should not be marked commutative. */
806 lra_assert (i != nop - 1);
809 for (j = 0; j < nalt; j++)
810 for (i = 0; i < nop; i++, op_alt++)
812 static_data->operand[i].early_clobber |= op_alt->earlyclobber;
813 static_data->operand[i].is_address |= op_alt->is_address;
817 /* Recursively process X and collect info about registers, which are
818 not the insn operands, in X with TYPE (in/out/inout) and flag that
819 it is early clobbered in the insn (EARLY_CLOBBER) and add the info
820 to LIST. X is a part of insn given by DATA. Return the result
821 list. */
822 static struct lra_insn_reg *
823 collect_non_operand_hard_regs (rtx *x, lra_insn_recog_data_t data,
824 struct lra_insn_reg *list,
825 enum op_type type, bool early_clobber)
827 int i, j, regno, last;
828 bool subreg_p;
829 machine_mode mode;
830 struct lra_insn_reg *curr;
831 rtx op = *x;
832 enum rtx_code code = GET_CODE (op);
833 const char *fmt = GET_RTX_FORMAT (code);
835 for (i = 0; i < data->insn_static_data->n_operands; i++)
836 if (x == data->operand_loc[i])
837 /* It is an operand loc. Stop here. */
838 return list;
839 for (i = 0; i < data->insn_static_data->n_dups; i++)
840 if (x == data->dup_loc[i])
841 /* It is a dup loc. Stop here. */
842 return list;
843 mode = GET_MODE (op);
844 subreg_p = false;
845 if (code == SUBREG)
847 op = SUBREG_REG (op);
848 code = GET_CODE (op);
849 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (op)))
851 mode = GET_MODE (op);
852 if (GET_MODE_SIZE (mode) > REGMODE_NATURAL_SIZE (mode))
853 subreg_p = true;
856 if (REG_P (op))
858 if ((regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER)
859 return list;
860 /* Process all regs even unallocatable ones as we need info
861 about all regs for rematerialization pass. */
862 for (last = regno + hard_regno_nregs[regno][mode];
863 regno < last;
864 regno++)
866 for (curr = list; curr != NULL; curr = curr->next)
867 if (curr->regno == regno && curr->subreg_p == subreg_p
868 && curr->biggest_mode == mode)
870 if (curr->type != type)
871 curr->type = OP_INOUT;
872 if (curr->early_clobber != early_clobber)
873 curr->early_clobber = true;
874 break;
876 if (curr == NULL)
878 /* This is a new hard regno or the info can not be
879 integrated into the found structure. */
880 #ifdef STACK_REGS
881 early_clobber
882 = (early_clobber
883 /* This clobber is to inform popping floating
884 point stack only. */
885 && ! (FIRST_STACK_REG <= regno
886 && regno <= LAST_STACK_REG));
887 #endif
888 list = new_insn_reg (data->insn, regno, type, mode, subreg_p,
889 early_clobber, list);
892 return list;
894 switch (code)
896 case SET:
897 list = collect_non_operand_hard_regs (&SET_DEST (op), data,
898 list, OP_OUT, false);
899 list = collect_non_operand_hard_regs (&SET_SRC (op), data,
900 list, OP_IN, false);
901 break;
902 case CLOBBER:
903 /* We treat clobber of non-operand hard registers as early
904 clobber (the behavior is expected from asm). */
905 list = collect_non_operand_hard_regs (&XEXP (op, 0), data,
906 list, OP_OUT, true);
907 break;
908 case PRE_INC: case PRE_DEC: case POST_INC: case POST_DEC:
909 list = collect_non_operand_hard_regs (&XEXP (op, 0), data,
910 list, OP_INOUT, false);
911 break;
912 case PRE_MODIFY: case POST_MODIFY:
913 list = collect_non_operand_hard_regs (&XEXP (op, 0), data,
914 list, OP_INOUT, false);
915 list = collect_non_operand_hard_regs (&XEXP (op, 1), data,
916 list, OP_IN, false);
917 break;
918 default:
919 fmt = GET_RTX_FORMAT (code);
920 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
922 if (fmt[i] == 'e')
923 list = collect_non_operand_hard_regs (&XEXP (op, i), data,
924 list, OP_IN, false);
925 else if (fmt[i] == 'E')
926 for (j = XVECLEN (op, i) - 1; j >= 0; j--)
927 list = collect_non_operand_hard_regs (&XVECEXP (op, i, j), data,
928 list, OP_IN, false);
931 return list;
934 /* Set up and return info about INSN. Set up the info if it is not set up
935 yet. */
936 lra_insn_recog_data_t
937 lra_set_insn_recog_data (rtx_insn *insn)
939 lra_insn_recog_data_t data;
940 int i, n, icode;
941 rtx **locs;
942 unsigned int uid = INSN_UID (insn);
943 struct lra_static_insn_data *insn_static_data;
945 check_and_expand_insn_recog_data (uid);
946 if (DEBUG_INSN_P (insn))
947 icode = -1;
948 else
950 icode = INSN_CODE (insn);
951 if (icode < 0)
952 /* It might be a new simple insn which is not recognized yet. */
953 INSN_CODE (insn) = icode = recog_memoized (insn);
955 data = XNEW (struct lra_insn_recog_data);
956 lra_insn_recog_data[uid] = data;
957 data->insn = insn;
958 data->used_insn_alternative = -1;
959 data->icode = icode;
960 data->regs = NULL;
961 if (DEBUG_INSN_P (insn))
963 data->insn_static_data = &debug_insn_static_data;
964 data->dup_loc = NULL;
965 data->arg_hard_regs = NULL;
966 data->preferred_alternatives = ALL_ALTERNATIVES;
967 data->operand_loc = XNEWVEC (rtx *, 1);
968 data->operand_loc[0] = &INSN_VAR_LOCATION_LOC (insn);
969 return data;
971 if (icode < 0)
973 int nop, nalt;
974 machine_mode operand_mode[MAX_RECOG_OPERANDS];
975 const char *constraints[MAX_RECOG_OPERANDS];
977 nop = asm_noperands (PATTERN (insn));
978 data->operand_loc = data->dup_loc = NULL;
979 nalt = 1;
980 if (nop < 0)
982 /* It is a special insn like USE or CLOBBER. We should
983 recognize any regular insn otherwise LRA can do nothing
984 with this insn. */
985 gcc_assert (GET_CODE (PATTERN (insn)) == USE
986 || GET_CODE (PATTERN (insn)) == CLOBBER
987 || GET_CODE (PATTERN (insn)) == ASM_INPUT);
988 data->insn_static_data = insn_static_data
989 = get_static_insn_data (-1, 0, 0, nalt);
991 else
993 /* expand_asm_operands makes sure there aren't too many
994 operands. */
995 lra_assert (nop <= MAX_RECOG_OPERANDS);
996 if (nop != 0)
997 data->operand_loc = XNEWVEC (rtx *, nop);
998 /* Now get the operand values and constraints out of the
999 insn. */
1000 decode_asm_operands (PATTERN (insn), NULL,
1001 data->operand_loc,
1002 constraints, operand_mode, NULL);
1003 if (nop > 0)
1005 const char *p = recog_data.constraints[0];
1007 for (p = constraints[0]; *p; p++)
1008 nalt += *p == ',';
1010 data->insn_static_data = insn_static_data
1011 = get_static_insn_data (-1, nop, 0, nalt);
1012 for (i = 0; i < nop; i++)
1014 insn_static_data->operand[i].mode = operand_mode[i];
1015 insn_static_data->operand[i].constraint = constraints[i];
1016 insn_static_data->operand[i].strict_low = false;
1017 insn_static_data->operand[i].is_operator = false;
1018 insn_static_data->operand[i].is_address = false;
1021 for (i = 0; i < insn_static_data->n_operands; i++)
1022 insn_static_data->operand[i].type
1023 = (insn_static_data->operand[i].constraint[0] == '=' ? OP_OUT
1024 : insn_static_data->operand[i].constraint[0] == '+' ? OP_INOUT
1025 : OP_IN);
1026 data->preferred_alternatives = ALL_ALTERNATIVES;
1027 if (nop > 0)
1029 operand_alternative *op_alt = XCNEWVEC (operand_alternative,
1030 nalt * nop);
1031 preprocess_constraints (nop, nalt, constraints, op_alt);
1032 setup_operand_alternative (data, op_alt);
1035 else
1037 insn_extract (insn);
1038 data->insn_static_data = insn_static_data
1039 = get_static_insn_data (icode, insn_data[icode].n_operands,
1040 insn_data[icode].n_dups,
1041 insn_data[icode].n_alternatives);
1042 n = insn_static_data->n_operands;
1043 if (n == 0)
1044 locs = NULL;
1045 else
1047 locs = XNEWVEC (rtx *, n);
1048 memcpy (locs, recog_data.operand_loc, n * sizeof (rtx *));
1050 data->operand_loc = locs;
1051 n = insn_static_data->n_dups;
1052 if (n == 0)
1053 locs = NULL;
1054 else
1056 locs = XNEWVEC (rtx *, n);
1057 memcpy (locs, recog_data.dup_loc, n * sizeof (rtx *));
1059 data->dup_loc = locs;
1060 data->preferred_alternatives = get_preferred_alternatives (insn);
1061 const operand_alternative *op_alt = preprocess_insn_constraints (icode);
1062 if (!insn_static_data->operand_alternative)
1063 setup_operand_alternative (data, op_alt);
1064 else if (op_alt != insn_static_data->operand_alternative)
1065 insn_static_data->operand_alternative = op_alt;
1067 if (GET_CODE (PATTERN (insn)) == CLOBBER || GET_CODE (PATTERN (insn)) == USE)
1068 insn_static_data->hard_regs = NULL;
1069 else
1070 insn_static_data->hard_regs
1071 = collect_non_operand_hard_regs (&PATTERN (insn), data,
1072 NULL, OP_IN, false);
1073 data->arg_hard_regs = NULL;
1074 if (CALL_P (insn))
1076 rtx link;
1077 int n_hard_regs, regno, arg_hard_regs[FIRST_PSEUDO_REGISTER];
1079 n_hard_regs = 0;
1080 /* Finding implicit hard register usage. We believe it will be
1081 not changed whatever transformations are used. Call insns
1082 are such example. */
1083 for (link = CALL_INSN_FUNCTION_USAGE (insn);
1084 link != NULL_RTX;
1085 link = XEXP (link, 1))
1086 if (GET_CODE (XEXP (link, 0)) == USE
1087 && REG_P (XEXP (XEXP (link, 0), 0)))
1089 regno = REGNO (XEXP (XEXP (link, 0), 0));
1090 lra_assert (regno < FIRST_PSEUDO_REGISTER);
1091 /* It is an argument register. */
1092 for (i = REG_NREGS (XEXP (XEXP (link, 0), 0)) - 1; i >= 0; i--)
1093 arg_hard_regs[n_hard_regs++] = regno + i;
1095 if (n_hard_regs != 0)
1097 arg_hard_regs[n_hard_regs++] = -1;
1098 data->arg_hard_regs = XNEWVEC (int, n_hard_regs);
1099 memcpy (data->arg_hard_regs, arg_hard_regs,
1100 sizeof (int) * n_hard_regs);
1103 /* Some output operand can be recognized only from the context not
1104 from the constraints which are empty in this case. Call insn may
1105 contain a hard register in set destination with empty constraint
1106 and extract_insn treats them as an input. */
1107 for (i = 0; i < insn_static_data->n_operands; i++)
1109 int j;
1110 rtx pat, set;
1111 struct lra_operand_data *operand = &insn_static_data->operand[i];
1113 /* ??? Should we treat 'X' the same way. It looks to me that
1114 'X' means anything and empty constraint means we do not
1115 care. */
1116 if (operand->type != OP_IN || *operand->constraint != '\0'
1117 || operand->is_operator)
1118 continue;
1119 pat = PATTERN (insn);
1120 if (GET_CODE (pat) == SET)
1122 if (data->operand_loc[i] != &SET_DEST (pat))
1123 continue;
1125 else if (GET_CODE (pat) == PARALLEL)
1127 for (j = XVECLEN (pat, 0) - 1; j >= 0; j--)
1129 set = XVECEXP (PATTERN (insn), 0, j);
1130 if (GET_CODE (set) == SET
1131 && &SET_DEST (set) == data->operand_loc[i])
1132 break;
1134 if (j < 0)
1135 continue;
1137 else
1138 continue;
1139 operand->type = OP_OUT;
1141 return data;
1144 /* Return info about insn give by UID. The info should be already set
1145 up. */
1146 static lra_insn_recog_data_t
1147 get_insn_recog_data_by_uid (int uid)
1149 lra_insn_recog_data_t data;
1151 data = lra_insn_recog_data[uid];
1152 lra_assert (data != NULL);
1153 return data;
1156 /* Invalidate all info about insn given by its UID. */
1157 static void
1158 invalidate_insn_recog_data (int uid)
1160 lra_insn_recog_data_t data;
1162 data = lra_insn_recog_data[uid];
1163 lra_assert (data != NULL);
1164 free_insn_recog_data (data);
1165 lra_insn_recog_data[uid] = NULL;
1168 /* Update all the insn info about INSN. It is usually called when
1169 something in the insn was changed. Return the updated info. */
1170 lra_insn_recog_data_t
1171 lra_update_insn_recog_data (rtx_insn *insn)
1173 lra_insn_recog_data_t data;
1174 int n;
1175 unsigned int uid = INSN_UID (insn);
1176 struct lra_static_insn_data *insn_static_data;
1177 HOST_WIDE_INT sp_offset = 0;
1179 check_and_expand_insn_recog_data (uid);
1180 if ((data = lra_insn_recog_data[uid]) != NULL
1181 && data->icode != INSN_CODE (insn))
1183 sp_offset = data->sp_offset;
1184 invalidate_insn_data_regno_info (data, insn, get_insn_freq (insn));
1185 invalidate_insn_recog_data (uid);
1186 data = NULL;
1188 if (data == NULL)
1190 data = lra_get_insn_recog_data (insn);
1191 /* Initiate or restore SP offset. */
1192 data->sp_offset = sp_offset;
1193 return data;
1195 insn_static_data = data->insn_static_data;
1196 data->used_insn_alternative = -1;
1197 if (DEBUG_INSN_P (insn))
1198 return data;
1199 if (data->icode < 0)
1201 int nop;
1202 machine_mode operand_mode[MAX_RECOG_OPERANDS];
1203 const char *constraints[MAX_RECOG_OPERANDS];
1205 nop = asm_noperands (PATTERN (insn));
1206 if (nop >= 0)
1208 lra_assert (nop == data->insn_static_data->n_operands);
1209 /* Now get the operand values and constraints out of the
1210 insn. */
1211 decode_asm_operands (PATTERN (insn), NULL,
1212 data->operand_loc,
1213 constraints, operand_mode, NULL);
1214 #ifdef ENABLE_CHECKING
1216 int i;
1218 for (i = 0; i < nop; i++)
1219 lra_assert
1220 (insn_static_data->operand[i].mode == operand_mode[i]
1221 && insn_static_data->operand[i].constraint == constraints[i]
1222 && ! insn_static_data->operand[i].is_operator);
1224 #endif
1226 #ifdef ENABLE_CHECKING
1228 int i;
1230 for (i = 0; i < insn_static_data->n_operands; i++)
1231 lra_assert
1232 (insn_static_data->operand[i].type
1233 == (insn_static_data->operand[i].constraint[0] == '=' ? OP_OUT
1234 : insn_static_data->operand[i].constraint[0] == '+' ? OP_INOUT
1235 : OP_IN));
1237 #endif
1239 else
1241 insn_extract (insn);
1242 n = insn_static_data->n_operands;
1243 if (n != 0)
1244 memcpy (data->operand_loc, recog_data.operand_loc, n * sizeof (rtx *));
1245 n = insn_static_data->n_dups;
1246 if (n != 0)
1247 memcpy (data->dup_loc, recog_data.dup_loc, n * sizeof (rtx *));
1248 lra_assert (check_bool_attrs (insn));
1250 return data;
1253 /* Set up that INSN is using alternative ALT now. */
1254 void
1255 lra_set_used_insn_alternative (rtx_insn *insn, int alt)
1257 lra_insn_recog_data_t data;
1259 data = lra_get_insn_recog_data (insn);
1260 data->used_insn_alternative = alt;
1263 /* Set up that insn with UID is using alternative ALT now. The insn
1264 info should be already set up. */
1265 void
1266 lra_set_used_insn_alternative_by_uid (int uid, int alt)
1268 lra_insn_recog_data_t data;
1270 check_and_expand_insn_recog_data (uid);
1271 data = lra_insn_recog_data[uid];
1272 lra_assert (data != NULL);
1273 data->used_insn_alternative = alt;
1278 /* This page contains code dealing with common register info and
1279 pseudo copies. */
1281 /* The size of the following array. */
1282 static int reg_info_size;
1283 /* Common info about each register. */
1284 struct lra_reg *lra_reg_info;
1286 /* Last register value. */
1287 static int last_reg_value;
1289 /* Return new register value. */
1290 static int
1291 get_new_reg_value (void)
1293 return ++last_reg_value;
1296 /* Pools for copies. */
1297 pool_allocator<lra_copy> lra_copy::pool ("lra copies", 100);
1299 /* Vec referring to pseudo copies. */
1300 static vec<lra_copy_t> copy_vec;
1302 /* Initialize I-th element of lra_reg_info. */
1303 static inline void
1304 initialize_lra_reg_info_element (int i)
1306 bitmap_initialize (&lra_reg_info[i].insn_bitmap, &reg_obstack);
1307 #ifdef STACK_REGS
1308 lra_reg_info[i].no_stack_p = false;
1309 #endif
1310 CLEAR_HARD_REG_SET (lra_reg_info[i].conflict_hard_regs);
1311 CLEAR_HARD_REG_SET (lra_reg_info[i].actual_call_used_reg_set);
1312 lra_reg_info[i].preferred_hard_regno1 = -1;
1313 lra_reg_info[i].preferred_hard_regno2 = -1;
1314 lra_reg_info[i].preferred_hard_regno_profit1 = 0;
1315 lra_reg_info[i].preferred_hard_regno_profit2 = 0;
1316 lra_reg_info[i].biggest_mode = VOIDmode;
1317 lra_reg_info[i].live_ranges = NULL;
1318 lra_reg_info[i].nrefs = lra_reg_info[i].freq = 0;
1319 lra_reg_info[i].last_reload = 0;
1320 lra_reg_info[i].restore_regno = -1;
1321 lra_reg_info[i].val = get_new_reg_value ();
1322 lra_reg_info[i].offset = 0;
1323 lra_reg_info[i].copies = NULL;
1326 /* Initialize common reg info and copies. */
1327 static void
1328 init_reg_info (void)
1330 int i;
1332 last_reg_value = 0;
1333 reg_info_size = max_reg_num () * 3 / 2 + 1;
1334 lra_reg_info = XNEWVEC (struct lra_reg, reg_info_size);
1335 for (i = 0; i < reg_info_size; i++)
1336 initialize_lra_reg_info_element (i);
1337 copy_vec.create (100);
1341 /* Finish common reg info and copies. */
1342 static void
1343 finish_reg_info (void)
1345 int i;
1347 for (i = 0; i < reg_info_size; i++)
1348 bitmap_clear (&lra_reg_info[i].insn_bitmap);
1349 free (lra_reg_info);
1350 reg_info_size = 0;
1353 /* Expand common reg info if it is necessary. */
1354 static void
1355 expand_reg_info (void)
1357 int i, old = reg_info_size;
1359 if (reg_info_size > max_reg_num ())
1360 return;
1361 reg_info_size = max_reg_num () * 3 / 2 + 1;
1362 lra_reg_info = XRESIZEVEC (struct lra_reg, lra_reg_info, reg_info_size);
1363 for (i = old; i < reg_info_size; i++)
1364 initialize_lra_reg_info_element (i);
1367 /* Free all copies. */
1368 void
1369 lra_free_copies (void)
1371 lra_copy_t cp;
1373 while (copy_vec.length () != 0)
1375 cp = copy_vec.pop ();
1376 lra_reg_info[cp->regno1].copies = lra_reg_info[cp->regno2].copies = NULL;
1377 delete cp;
1381 /* Create copy of two pseudos REGNO1 and REGNO2. The copy execution
1382 frequency is FREQ. */
1383 void
1384 lra_create_copy (int regno1, int regno2, int freq)
1386 bool regno1_dest_p;
1387 lra_copy_t cp;
1389 lra_assert (regno1 != regno2);
1390 regno1_dest_p = true;
1391 if (regno1 > regno2)
1393 int temp = regno2;
1395 regno1_dest_p = false;
1396 regno2 = regno1;
1397 regno1 = temp;
1399 cp = new lra_copy ();
1400 copy_vec.safe_push (cp);
1401 cp->regno1_dest_p = regno1_dest_p;
1402 cp->freq = freq;
1403 cp->regno1 = regno1;
1404 cp->regno2 = regno2;
1405 cp->regno1_next = lra_reg_info[regno1].copies;
1406 lra_reg_info[regno1].copies = cp;
1407 cp->regno2_next = lra_reg_info[regno2].copies;
1408 lra_reg_info[regno2].copies = cp;
1409 if (lra_dump_file != NULL)
1410 fprintf (lra_dump_file, " Creating copy r%d%sr%d@%d\n",
1411 regno1, regno1_dest_p ? "<-" : "->", regno2, freq);
1414 /* Return N-th (0, 1, ...) copy. If there is no copy, return
1415 NULL. */
1416 lra_copy_t
1417 lra_get_copy (int n)
1419 if (n >= (int) copy_vec.length ())
1420 return NULL;
1421 return copy_vec[n];
1426 /* This page contains code dealing with info about registers in
1427 insns. */
1429 /* Process X of insn UID recursively and add info (operand type is
1430 given by TYPE, flag of that it is early clobber is EARLY_CLOBBER)
1431 about registers in X to the insn DATA. */
1432 static void
1433 add_regs_to_insn_regno_info (lra_insn_recog_data_t data, rtx x, int uid,
1434 enum op_type type, bool early_clobber)
1436 int i, j, regno;
1437 bool subreg_p;
1438 machine_mode mode;
1439 const char *fmt;
1440 enum rtx_code code;
1441 struct lra_insn_reg *curr;
1443 code = GET_CODE (x);
1444 mode = GET_MODE (x);
1445 subreg_p = false;
1446 if (GET_CODE (x) == SUBREG)
1448 x = SUBREG_REG (x);
1449 code = GET_CODE (x);
1450 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (x)))
1452 mode = GET_MODE (x);
1453 if (GET_MODE_SIZE (mode) > REGMODE_NATURAL_SIZE (mode))
1454 subreg_p = true;
1457 if (REG_P (x))
1459 regno = REGNO (x);
1460 /* Process all regs even unallocatable ones as we need info about
1461 all regs for rematerialization pass. */
1462 expand_reg_info ();
1463 if (bitmap_set_bit (&lra_reg_info[regno].insn_bitmap, uid))
1465 data->regs = new_insn_reg (data->insn, regno, type, mode, subreg_p,
1466 early_clobber, data->regs);
1467 return;
1469 else
1471 for (curr = data->regs; curr != NULL; curr = curr->next)
1472 if (curr->regno == regno)
1474 if (curr->subreg_p != subreg_p || curr->biggest_mode != mode)
1475 /* The info can not be integrated into the found
1476 structure. */
1477 data->regs = new_insn_reg (data->insn, regno, type, mode,
1478 subreg_p, early_clobber,
1479 data->regs);
1480 else
1482 if (curr->type != type)
1483 curr->type = OP_INOUT;
1484 if (curr->early_clobber != early_clobber)
1485 curr->early_clobber = true;
1487 return;
1489 gcc_unreachable ();
1493 switch (code)
1495 case SET:
1496 add_regs_to_insn_regno_info (data, SET_DEST (x), uid, OP_OUT, false);
1497 add_regs_to_insn_regno_info (data, SET_SRC (x), uid, OP_IN, false);
1498 break;
1499 case CLOBBER:
1500 /* We treat clobber of non-operand hard registers as early
1501 clobber (the behavior is expected from asm). */
1502 add_regs_to_insn_regno_info (data, XEXP (x, 0), uid, OP_OUT, true);
1503 break;
1504 case PRE_INC: case PRE_DEC: case POST_INC: case POST_DEC:
1505 add_regs_to_insn_regno_info (data, XEXP (x, 0), uid, OP_INOUT, false);
1506 break;
1507 case PRE_MODIFY: case POST_MODIFY:
1508 add_regs_to_insn_regno_info (data, XEXP (x, 0), uid, OP_INOUT, false);
1509 add_regs_to_insn_regno_info (data, XEXP (x, 1), uid, OP_IN, false);
1510 break;
1511 default:
1512 if ((code != PARALLEL && code != EXPR_LIST) || type != OP_OUT)
1513 /* Some targets place small structures in registers for return
1514 values of functions, and those registers are wrapped in
1515 PARALLEL that we may see as the destination of a SET. Here
1516 is an example:
1518 (call_insn 13 12 14 2 (set (parallel:BLK [
1519 (expr_list:REG_DEP_TRUE (reg:DI 0 ax)
1520 (const_int 0 [0]))
1521 (expr_list:REG_DEP_TRUE (reg:DI 1 dx)
1522 (const_int 8 [0x8]))
1524 (call (mem:QI (symbol_ref:DI (... */
1525 type = OP_IN;
1526 fmt = GET_RTX_FORMAT (code);
1527 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1529 if (fmt[i] == 'e')
1530 add_regs_to_insn_regno_info (data, XEXP (x, i), uid, type, false);
1531 else if (fmt[i] == 'E')
1533 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1534 add_regs_to_insn_regno_info (data, XVECEXP (x, i, j), uid,
1535 type, false);
1541 /* Return execution frequency of INSN. */
1542 static int
1543 get_insn_freq (rtx_insn *insn)
1545 basic_block bb = BLOCK_FOR_INSN (insn);
1547 gcc_checking_assert (bb != NULL);
1548 return REG_FREQ_FROM_BB (bb);
1551 /* Invalidate all reg info of INSN with DATA and execution frequency
1552 FREQ. Update common info about the invalidated registers. */
1553 static void
1554 invalidate_insn_data_regno_info (lra_insn_recog_data_t data, rtx_insn *insn,
1555 int freq)
1557 int uid;
1558 bool debug_p;
1559 unsigned int i;
1560 struct lra_insn_reg *ir, *next_ir;
1562 uid = INSN_UID (insn);
1563 debug_p = DEBUG_INSN_P (insn);
1564 for (ir = data->regs; ir != NULL; ir = next_ir)
1566 i = ir->regno;
1567 next_ir = ir->next;
1568 delete ir;
1569 bitmap_clear_bit (&lra_reg_info[i].insn_bitmap, uid);
1570 if (i >= FIRST_PSEUDO_REGISTER && ! debug_p)
1572 lra_reg_info[i].nrefs--;
1573 lra_reg_info[i].freq -= freq;
1574 lra_assert (lra_reg_info[i].nrefs >= 0 && lra_reg_info[i].freq >= 0);
1577 data->regs = NULL;
1580 /* Invalidate all reg info of INSN. Update common info about the
1581 invalidated registers. */
1582 void
1583 lra_invalidate_insn_regno_info (rtx_insn *insn)
1585 invalidate_insn_data_regno_info (lra_get_insn_recog_data (insn), insn,
1586 get_insn_freq (insn));
1589 /* Update common reg info from reg info of insn given by its DATA and
1590 execution frequency FREQ. */
1591 static void
1592 setup_insn_reg_info (lra_insn_recog_data_t data, int freq)
1594 unsigned int i;
1595 struct lra_insn_reg *ir;
1597 for (ir = data->regs; ir != NULL; ir = ir->next)
1598 if ((i = ir->regno) >= FIRST_PSEUDO_REGISTER)
1600 lra_reg_info[i].nrefs++;
1601 lra_reg_info[i].freq += freq;
1605 /* Set up insn reg info of INSN. Update common reg info from reg info
1606 of INSN. */
1607 void
1608 lra_update_insn_regno_info (rtx_insn *insn)
1610 int i, uid, freq;
1611 lra_insn_recog_data_t data;
1612 struct lra_static_insn_data *static_data;
1613 enum rtx_code code;
1614 rtx link;
1616 if (! INSN_P (insn))
1617 return;
1618 data = lra_get_insn_recog_data (insn);
1619 static_data = data->insn_static_data;
1620 freq = get_insn_freq (insn);
1621 invalidate_insn_data_regno_info (data, insn, freq);
1622 uid = INSN_UID (insn);
1623 for (i = static_data->n_operands - 1; i >= 0; i--)
1624 add_regs_to_insn_regno_info (data, *data->operand_loc[i], uid,
1625 static_data->operand[i].type,
1626 static_data->operand[i].early_clobber);
1627 if ((code = GET_CODE (PATTERN (insn))) == CLOBBER || code == USE)
1628 add_regs_to_insn_regno_info (data, XEXP (PATTERN (insn), 0), uid,
1629 code == USE ? OP_IN : OP_OUT, false);
1630 if (CALL_P (insn))
1631 /* On some targets call insns can refer to pseudos in memory in
1632 CALL_INSN_FUNCTION_USAGE list. Process them in order to
1633 consider their occurrences in calls for different
1634 transformations (e.g. inheritance) with given pseudos. */
1635 for (link = CALL_INSN_FUNCTION_USAGE (insn);
1636 link != NULL_RTX;
1637 link = XEXP (link, 1))
1638 if (((code = GET_CODE (XEXP (link, 0))) == USE || code == CLOBBER)
1639 && MEM_P (XEXP (XEXP (link, 0), 0)))
1640 add_regs_to_insn_regno_info (data, XEXP (XEXP (link, 0), 0), uid,
1641 code == USE ? OP_IN : OP_OUT, false);
1642 if (NONDEBUG_INSN_P (insn))
1643 setup_insn_reg_info (data, freq);
1646 /* Return reg info of insn given by it UID. */
1647 struct lra_insn_reg *
1648 lra_get_insn_regs (int uid)
1650 lra_insn_recog_data_t data;
1652 data = get_insn_recog_data_by_uid (uid);
1653 return data->regs;
1658 /* This page contains code dealing with stack of the insns which
1659 should be processed by the next constraint pass. */
1661 /* Bitmap used to put an insn on the stack only in one exemplar. */
1662 static sbitmap lra_constraint_insn_stack_bitmap;
1664 /* The stack itself. */
1665 vec<rtx_insn *> lra_constraint_insn_stack;
1667 /* Put INSN on the stack. If ALWAYS_UPDATE is true, always update the reg
1668 info for INSN, otherwise only update it if INSN is not already on the
1669 stack. */
1670 static inline void
1671 lra_push_insn_1 (rtx_insn *insn, bool always_update)
1673 unsigned int uid = INSN_UID (insn);
1674 if (always_update)
1675 lra_update_insn_regno_info (insn);
1676 if (uid >= SBITMAP_SIZE (lra_constraint_insn_stack_bitmap))
1677 lra_constraint_insn_stack_bitmap =
1678 sbitmap_resize (lra_constraint_insn_stack_bitmap, 3 * uid / 2, 0);
1679 if (bitmap_bit_p (lra_constraint_insn_stack_bitmap, uid))
1680 return;
1681 bitmap_set_bit (lra_constraint_insn_stack_bitmap, uid);
1682 if (! always_update)
1683 lra_update_insn_regno_info (insn);
1684 lra_constraint_insn_stack.safe_push (insn);
1687 /* Put INSN on the stack. */
1688 void
1689 lra_push_insn (rtx_insn *insn)
1691 lra_push_insn_1 (insn, false);
1694 /* Put INSN on the stack and update its reg info. */
1695 void
1696 lra_push_insn_and_update_insn_regno_info (rtx_insn *insn)
1698 lra_push_insn_1 (insn, true);
1701 /* Put insn with UID on the stack. */
1702 void
1703 lra_push_insn_by_uid (unsigned int uid)
1705 lra_push_insn (lra_insn_recog_data[uid]->insn);
1708 /* Take the last-inserted insns off the stack and return it. */
1709 rtx_insn *
1710 lra_pop_insn (void)
1712 rtx_insn *insn = lra_constraint_insn_stack.pop ();
1713 bitmap_clear_bit (lra_constraint_insn_stack_bitmap, INSN_UID (insn));
1714 return insn;
1717 /* Return the current size of the insn stack. */
1718 unsigned int
1719 lra_insn_stack_length (void)
1721 return lra_constraint_insn_stack.length ();
1724 /* Push insns FROM to TO (excluding it) going in reverse order. */
1725 static void
1726 push_insns (rtx_insn *from, rtx_insn *to)
1728 rtx_insn *insn;
1730 if (from == NULL_RTX)
1731 return;
1732 for (insn = from; insn != to; insn = PREV_INSN (insn))
1733 if (INSN_P (insn))
1734 lra_push_insn (insn);
1737 /* Set up sp offset for insn in range [FROM, LAST]. The offset is
1738 taken from the next BB insn after LAST or zero if there in such
1739 insn. */
1740 static void
1741 setup_sp_offset (rtx_insn *from, rtx_insn *last)
1743 rtx_insn *before = next_nonnote_insn_bb (last);
1744 HOST_WIDE_INT offset = (before == NULL_RTX || ! INSN_P (before)
1745 ? 0 : lra_get_insn_recog_data (before)->sp_offset);
1747 for (rtx_insn *insn = from; insn != NEXT_INSN (last); insn = NEXT_INSN (insn))
1748 lra_get_insn_recog_data (insn)->sp_offset = offset;
1751 /* Emit insns BEFORE before INSN and insns AFTER after INSN. Put the
1752 insns onto the stack. Print about emitting the insns with
1753 TITLE. */
1754 void
1755 lra_process_new_insns (rtx_insn *insn, rtx_insn *before, rtx_insn *after,
1756 const char *title)
1758 rtx_insn *last;
1760 if (before == NULL_RTX && after == NULL_RTX)
1761 return;
1762 if (lra_dump_file != NULL)
1764 dump_insn_slim (lra_dump_file, insn);
1765 if (before != NULL_RTX)
1767 fprintf (lra_dump_file," %s before:\n", title);
1768 dump_rtl_slim (lra_dump_file, before, NULL, -1, 0);
1770 if (after != NULL_RTX)
1772 fprintf (lra_dump_file, " %s after:\n", title);
1773 dump_rtl_slim (lra_dump_file, after, NULL, -1, 0);
1775 fprintf (lra_dump_file, "\n");
1777 if (before != NULL_RTX)
1779 emit_insn_before (before, insn);
1780 push_insns (PREV_INSN (insn), PREV_INSN (before));
1781 setup_sp_offset (before, PREV_INSN (insn));
1783 if (after != NULL_RTX)
1785 for (last = after; NEXT_INSN (last) != NULL_RTX; last = NEXT_INSN (last))
1787 emit_insn_after (after, insn);
1788 push_insns (last, insn);
1789 setup_sp_offset (after, last);
1795 /* Replace all references to register OLD_REGNO in *LOC with pseudo
1796 register NEW_REG. Return true if any change was made. */
1797 bool
1798 lra_substitute_pseudo (rtx *loc, int old_regno, rtx new_reg)
1800 rtx x = *loc;
1801 bool result = false;
1802 enum rtx_code code;
1803 const char *fmt;
1804 int i, j;
1806 if (x == NULL_RTX)
1807 return false;
1809 code = GET_CODE (x);
1810 if (code == REG && (int) REGNO (x) == old_regno)
1812 machine_mode mode = GET_MODE (*loc);
1813 machine_mode inner_mode = GET_MODE (new_reg);
1815 if (mode != inner_mode
1816 && ! (CONST_INT_P (new_reg) && SCALAR_INT_MODE_P (mode)))
1818 if (GET_MODE_SIZE (mode) >= GET_MODE_SIZE (inner_mode)
1819 || ! SCALAR_INT_MODE_P (inner_mode))
1820 new_reg = gen_rtx_SUBREG (mode, new_reg, 0);
1821 else
1822 new_reg = gen_lowpart_SUBREG (mode, new_reg);
1824 *loc = new_reg;
1825 return true;
1828 /* Scan all the operand sub-expressions. */
1829 fmt = GET_RTX_FORMAT (code);
1830 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1832 if (fmt[i] == 'e')
1834 if (lra_substitute_pseudo (&XEXP (x, i), old_regno, new_reg))
1835 result = true;
1837 else if (fmt[i] == 'E')
1839 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1840 if (lra_substitute_pseudo (&XVECEXP (x, i, j), old_regno, new_reg))
1841 result = true;
1844 return result;
1847 /* Call lra_substitute_pseudo within an insn. This won't update the insn ptr,
1848 just the contents of the insn. */
1849 bool
1850 lra_substitute_pseudo_within_insn (rtx_insn *insn, int old_regno, rtx new_reg)
1852 rtx loc = insn;
1853 return lra_substitute_pseudo (&loc, old_regno, new_reg);
1858 /* This page contains code dealing with scratches (changing them onto
1859 pseudos and restoring them from the pseudos).
1861 We change scratches into pseudos at the beginning of LRA to
1862 simplify dealing with them (conflicts, hard register assignments).
1864 If the pseudo denoting scratch was spilled it means that we do need
1865 a hard register for it. Such pseudos are transformed back to
1866 scratches at the end of LRA. */
1868 /* Description of location of a former scratch operand. */
1869 struct sloc
1871 rtx_insn *insn; /* Insn where the scratch was. */
1872 int nop; /* Number of the operand which was a scratch. */
1875 typedef struct sloc *sloc_t;
1877 /* Locations of the former scratches. */
1878 static vec<sloc_t> scratches;
1880 /* Bitmap of scratch regnos. */
1881 static bitmap_head scratch_bitmap;
1883 /* Bitmap of scratch operands. */
1884 static bitmap_head scratch_operand_bitmap;
1886 /* Return true if pseudo REGNO is made of SCRATCH. */
1887 bool
1888 lra_former_scratch_p (int regno)
1890 return bitmap_bit_p (&scratch_bitmap, regno);
1893 /* Return true if the operand NOP of INSN is a former scratch. */
1894 bool
1895 lra_former_scratch_operand_p (rtx_insn *insn, int nop)
1897 return bitmap_bit_p (&scratch_operand_bitmap,
1898 INSN_UID (insn) * MAX_RECOG_OPERANDS + nop) != 0;
1901 /* Register operand NOP in INSN as a former scratch. It will be
1902 changed to scratch back, if it is necessary, at the LRA end. */
1903 void
1904 lra_register_new_scratch_op (rtx_insn *insn, int nop)
1906 lra_insn_recog_data_t id = lra_get_insn_recog_data (insn);
1907 rtx op = *id->operand_loc[nop];
1908 sloc_t loc = XNEW (struct sloc);
1909 lra_assert (REG_P (op));
1910 loc->insn = insn;
1911 loc->nop = nop;
1912 scratches.safe_push (loc);
1913 bitmap_set_bit (&scratch_bitmap, REGNO (op));
1914 bitmap_set_bit (&scratch_operand_bitmap,
1915 INSN_UID (insn) * MAX_RECOG_OPERANDS + nop);
1916 add_reg_note (insn, REG_UNUSED, op);
1919 /* Change scratches onto pseudos and save their location. */
1920 static void
1921 remove_scratches (void)
1923 int i;
1924 bool insn_changed_p;
1925 basic_block bb;
1926 rtx_insn *insn;
1927 rtx reg;
1928 lra_insn_recog_data_t id;
1929 struct lra_static_insn_data *static_id;
1931 scratches.create (get_max_uid ());
1932 bitmap_initialize (&scratch_bitmap, &reg_obstack);
1933 bitmap_initialize (&scratch_operand_bitmap, &reg_obstack);
1934 FOR_EACH_BB_FN (bb, cfun)
1935 FOR_BB_INSNS (bb, insn)
1936 if (INSN_P (insn))
1938 id = lra_get_insn_recog_data (insn);
1939 static_id = id->insn_static_data;
1940 insn_changed_p = false;
1941 for (i = 0; i < static_id->n_operands; i++)
1942 if (GET_CODE (*id->operand_loc[i]) == SCRATCH
1943 && GET_MODE (*id->operand_loc[i]) != VOIDmode)
1945 insn_changed_p = true;
1946 *id->operand_loc[i] = reg
1947 = lra_create_new_reg (static_id->operand[i].mode,
1948 *id->operand_loc[i], ALL_REGS, NULL);
1949 lra_register_new_scratch_op (insn, i);
1950 if (lra_dump_file != NULL)
1951 fprintf (lra_dump_file,
1952 "Removing SCRATCH in insn #%u (nop %d)\n",
1953 INSN_UID (insn), i);
1955 if (insn_changed_p)
1956 /* Because we might use DF right after caller-saves sub-pass
1957 we need to keep DF info up to date. */
1958 df_insn_rescan (insn);
1962 /* Changes pseudos created by function remove_scratches onto scratches. */
1963 static void
1964 restore_scratches (void)
1966 int regno;
1967 unsigned i;
1968 sloc_t loc;
1969 rtx_insn *last = NULL;
1970 lra_insn_recog_data_t id = NULL;
1972 for (i = 0; scratches.iterate (i, &loc); i++)
1974 if (last != loc->insn)
1976 last = loc->insn;
1977 id = lra_get_insn_recog_data (last);
1979 if (REG_P (*id->operand_loc[loc->nop])
1980 && ((regno = REGNO (*id->operand_loc[loc->nop]))
1981 >= FIRST_PSEUDO_REGISTER)
1982 && lra_get_regno_hard_regno (regno) < 0)
1984 /* It should be only case when scratch register with chosen
1985 constraint 'X' did not get memory or hard register. */
1986 lra_assert (lra_former_scratch_p (regno));
1987 *id->operand_loc[loc->nop]
1988 = gen_rtx_SCRATCH (GET_MODE (*id->operand_loc[loc->nop]));
1989 lra_update_dup (id, loc->nop);
1990 if (lra_dump_file != NULL)
1991 fprintf (lra_dump_file, "Restoring SCRATCH in insn #%u(nop %d)\n",
1992 INSN_UID (loc->insn), loc->nop);
1995 for (i = 0; scratches.iterate (i, &loc); i++)
1996 free (loc);
1997 scratches.release ();
1998 bitmap_clear (&scratch_bitmap);
1999 bitmap_clear (&scratch_operand_bitmap);
2004 #ifdef ENABLE_CHECKING
2006 /* Function checks RTL for correctness. If FINAL_P is true, it is
2007 done at the end of LRA and the check is more rigorous. */
2008 static void
2009 check_rtl (bool final_p)
2011 basic_block bb;
2012 rtx_insn *insn;
2014 lra_assert (! final_p || reload_completed);
2015 FOR_EACH_BB_FN (bb, cfun)
2016 FOR_BB_INSNS (bb, insn)
2017 if (NONDEBUG_INSN_P (insn)
2018 && GET_CODE (PATTERN (insn)) != USE
2019 && GET_CODE (PATTERN (insn)) != CLOBBER
2020 && GET_CODE (PATTERN (insn)) != ASM_INPUT)
2022 if (final_p)
2024 #ifdef ENABLED_CHECKING
2025 extract_constrain_insn (insn);
2026 #endif
2027 continue;
2029 /* LRA code is based on assumption that all addresses can be
2030 correctly decomposed. LRA can generate reloads for
2031 decomposable addresses. The decomposition code checks the
2032 correctness of the addresses. So we don't need to check
2033 the addresses here. Don't call insn_invalid_p here, it can
2034 change the code at this stage. */
2035 if (recog_memoized (insn) < 0 && asm_noperands (PATTERN (insn)) < 0)
2036 fatal_insn_not_found (insn);
2039 #endif /* #ifdef ENABLE_CHECKING */
2041 /* Determine if the current function has an exception receiver block
2042 that reaches the exit block via non-exceptional edges */
2043 static bool
2044 has_nonexceptional_receiver (void)
2046 edge e;
2047 edge_iterator ei;
2048 basic_block *tos, *worklist, bb;
2050 /* If we're not optimizing, then just err on the safe side. */
2051 if (!optimize)
2052 return true;
2054 /* First determine which blocks can reach exit via normal paths. */
2055 tos = worklist = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun) + 1);
2057 FOR_EACH_BB_FN (bb, cfun)
2058 bb->flags &= ~BB_REACHABLE;
2060 /* Place the exit block on our worklist. */
2061 EXIT_BLOCK_PTR_FOR_FN (cfun)->flags |= BB_REACHABLE;
2062 *tos++ = EXIT_BLOCK_PTR_FOR_FN (cfun);
2064 /* Iterate: find everything reachable from what we've already seen. */
2065 while (tos != worklist)
2067 bb = *--tos;
2069 FOR_EACH_EDGE (e, ei, bb->preds)
2070 if (e->flags & EDGE_ABNORMAL)
2072 free (worklist);
2073 return true;
2075 else
2077 basic_block src = e->src;
2079 if (!(src->flags & BB_REACHABLE))
2081 src->flags |= BB_REACHABLE;
2082 *tos++ = src;
2086 free (worklist);
2087 /* No exceptional block reached exit unexceptionally. */
2088 return false;
2091 #ifdef AUTO_INC_DEC
2093 /* Process recursively X of INSN and add REG_INC notes if necessary. */
2094 static void
2095 add_auto_inc_notes (rtx_insn *insn, rtx x)
2097 enum rtx_code code = GET_CODE (x);
2098 const char *fmt;
2099 int i, j;
2101 if (code == MEM && auto_inc_p (XEXP (x, 0)))
2103 add_reg_note (insn, REG_INC, XEXP (XEXP (x, 0), 0));
2104 return;
2107 /* Scan all X sub-expressions. */
2108 fmt = GET_RTX_FORMAT (code);
2109 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2111 if (fmt[i] == 'e')
2112 add_auto_inc_notes (insn, XEXP (x, i));
2113 else if (fmt[i] == 'E')
2114 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2115 add_auto_inc_notes (insn, XVECEXP (x, i, j));
2119 #endif
2121 /* Remove all REG_DEAD and REG_UNUSED notes and regenerate REG_INC.
2122 We change pseudos by hard registers without notification of DF and
2123 that can make the notes obsolete. DF-infrastructure does not deal
2124 with REG_INC notes -- so we should regenerate them here. */
2125 static void
2126 update_inc_notes (void)
2128 rtx *pnote;
2129 basic_block bb;
2130 rtx_insn *insn;
2132 FOR_EACH_BB_FN (bb, cfun)
2133 FOR_BB_INSNS (bb, insn)
2134 if (NONDEBUG_INSN_P (insn))
2136 pnote = &REG_NOTES (insn);
2137 while (*pnote != 0)
2139 if (REG_NOTE_KIND (*pnote) == REG_DEAD
2140 || REG_NOTE_KIND (*pnote) == REG_UNUSED
2141 || REG_NOTE_KIND (*pnote) == REG_INC)
2142 *pnote = XEXP (*pnote, 1);
2143 else
2144 pnote = &XEXP (*pnote, 1);
2146 #ifdef AUTO_INC_DEC
2147 add_auto_inc_notes (insn, PATTERN (insn));
2148 #endif
2152 /* Set to 1 while in lra. */
2153 int lra_in_progress;
2155 /* Start of pseudo regnos before the LRA. */
2156 int lra_new_regno_start;
2158 /* Start of reload pseudo regnos before the new spill pass. */
2159 int lra_constraint_new_regno_start;
2161 /* Avoid spilling pseudos with regno more than the following value if
2162 it is possible. */
2163 int lra_bad_spill_regno_start;
2165 /* Inheritance pseudo regnos before the new spill pass. */
2166 bitmap_head lra_inheritance_pseudos;
2168 /* Split regnos before the new spill pass. */
2169 bitmap_head lra_split_regs;
2171 /* Reload pseudo regnos before the new assignmnet pass which still can
2172 be spilled after the assinment pass as memory is also accepted in
2173 insns for the reload pseudos. */
2174 bitmap_head lra_optional_reload_pseudos;
2176 /* Pseudo regnos used for subreg reloads before the new assignment
2177 pass. Such pseudos still can be spilled after the assinment
2178 pass. */
2179 bitmap_head lra_subreg_reload_pseudos;
2181 /* File used for output of LRA debug information. */
2182 FILE *lra_dump_file;
2184 /* True if we should try spill into registers of different classes
2185 instead of memory. */
2186 bool lra_reg_spill_p;
2188 /* Set up value LRA_REG_SPILL_P. */
2189 static void
2190 setup_reg_spill_flag (void)
2192 int cl, mode;
2194 if (targetm.spill_class != NULL)
2195 for (cl = 0; cl < (int) LIM_REG_CLASSES; cl++)
2196 for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
2197 if (targetm.spill_class ((enum reg_class) cl,
2198 (machine_mode) mode) != NO_REGS)
2200 lra_reg_spill_p = true;
2201 return;
2203 lra_reg_spill_p = false;
2206 /* True if the current function is too big to use regular algorithms
2207 in LRA. In other words, we should use simpler and faster algorithms
2208 in LRA. It also means we should not worry about generation code
2209 for caller saves. The value is set up in IRA. */
2210 bool lra_simple_p;
2212 /* Major LRA entry function. F is a file should be used to dump LRA
2213 debug info. */
2214 void
2215 lra (FILE *f)
2217 int i;
2218 bool live_p, scratch_p, inserted_p;
2220 lra_dump_file = f;
2222 timevar_push (TV_LRA);
2224 /* Make sure that the last insn is a note. Some subsequent passes
2225 need it. */
2226 emit_note (NOTE_INSN_DELETED);
2228 COPY_HARD_REG_SET (lra_no_alloc_regs, ira_no_alloc_regs);
2230 init_reg_info ();
2231 expand_reg_info ();
2233 init_insn_recog_data ();
2235 #ifdef ENABLE_CHECKING
2236 /* Some quick check on RTL generated by previous passes. */
2237 check_rtl (false);
2238 #endif
2240 lra_in_progress = 1;
2242 lra_live_range_iter = lra_coalesce_iter = lra_constraint_iter = 0;
2243 lra_assignment_iter = lra_assignment_iter_after_spill = 0;
2244 lra_inheritance_iter = lra_undo_inheritance_iter = 0;
2245 lra_rematerialization_iter = 0;
2247 setup_reg_spill_flag ();
2249 /* Function remove_scratches can creates new pseudos for clobbers --
2250 so set up lra_constraint_new_regno_start before its call to
2251 permit changing reg classes for pseudos created by this
2252 simplification. */
2253 lra_constraint_new_regno_start = lra_new_regno_start = max_reg_num ();
2254 lra_bad_spill_regno_start = INT_MAX;
2255 remove_scratches ();
2256 scratch_p = lra_constraint_new_regno_start != max_reg_num ();
2258 /* A function that has a non-local label that can reach the exit
2259 block via non-exceptional paths must save all call-saved
2260 registers. */
2261 if (cfun->has_nonlocal_label && has_nonexceptional_receiver ())
2262 crtl->saves_all_registers = 1;
2264 if (crtl->saves_all_registers)
2265 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2266 if (! call_used_regs[i] && ! fixed_regs[i] && ! LOCAL_REGNO (i))
2267 df_set_regs_ever_live (i, true);
2269 /* We don't DF from now and avoid its using because it is to
2270 expensive when a lot of RTL changes are made. */
2271 df_set_flags (DF_NO_INSN_RESCAN);
2272 lra_constraint_insn_stack.create (get_max_uid ());
2273 lra_constraint_insn_stack_bitmap = sbitmap_alloc (get_max_uid ());
2274 bitmap_clear (lra_constraint_insn_stack_bitmap);
2275 lra_live_ranges_init ();
2276 lra_constraints_init ();
2277 lra_curr_reload_num = 0;
2278 push_insns (get_last_insn (), NULL);
2279 /* It is needed for the 1st coalescing. */
2280 bitmap_initialize (&lra_inheritance_pseudos, &reg_obstack);
2281 bitmap_initialize (&lra_split_regs, &reg_obstack);
2282 bitmap_initialize (&lra_optional_reload_pseudos, &reg_obstack);
2283 bitmap_initialize (&lra_subreg_reload_pseudos, &reg_obstack);
2284 live_p = false;
2285 if (get_frame_size () != 0 && crtl->stack_alignment_needed)
2286 /* If we have a stack frame, we must align it now. The stack size
2287 may be a part of the offset computation for register
2288 elimination. */
2289 assign_stack_local (BLKmode, 0, crtl->stack_alignment_needed);
2290 lra_init_equiv ();
2291 for (;;)
2293 for (;;)
2295 /* We should try to assign hard registers to scratches even
2296 if there were no RTL transformations in
2297 lra_constraints. */
2298 if (! lra_constraints (lra_constraint_iter == 0)
2299 && (lra_constraint_iter > 1
2300 || (! scratch_p && ! caller_save_needed)))
2301 break;
2302 /* Constraint transformations may result in that eliminable
2303 hard regs become uneliminable and pseudos which use them
2304 should be spilled. It is better to do it before pseudo
2305 assignments.
2307 For example, rs6000 can make
2308 RS6000_PIC_OFFSET_TABLE_REGNUM uneliminable if we started
2309 to use a constant pool. */
2310 lra_eliminate (false, false);
2311 /* Do inheritance only for regular algorithms. */
2312 if (! lra_simple_p)
2314 if (flag_ipa_ra)
2316 if (live_p)
2317 lra_clear_live_ranges ();
2318 /* As a side-effect of lra_create_live_ranges, we calculate
2319 actual_call_used_reg_set, which is needed during
2320 lra_inheritance. */
2321 lra_create_live_ranges (true, true);
2322 live_p = true;
2324 lra_inheritance ();
2326 if (live_p)
2327 lra_clear_live_ranges ();
2328 /* We need live ranges for lra_assign -- so build them. But
2329 don't remove dead insns or change global live info as we
2330 can undo inheritance transformations after inheritance
2331 pseudo assigning. */
2332 lra_create_live_ranges (true, false);
2333 live_p = true;
2334 /* If we don't spill non-reload and non-inheritance pseudos,
2335 there is no sense to run memory-memory move coalescing.
2336 If inheritance pseudos were spilled, the memory-memory
2337 moves involving them will be removed by pass undoing
2338 inheritance. */
2339 if (lra_simple_p)
2340 lra_assign ();
2341 else
2343 bool spill_p = !lra_assign ();
2345 if (lra_undo_inheritance ())
2346 live_p = false;
2347 if (spill_p)
2349 if (! live_p)
2351 lra_create_live_ranges (true, true);
2352 live_p = true;
2354 if (lra_coalesce ())
2355 live_p = false;
2357 if (! live_p)
2358 lra_clear_live_ranges ();
2361 /* Don't clear optional reloads bitmap until all constraints are
2362 satisfied as we need to differ them from regular reloads. */
2363 bitmap_clear (&lra_optional_reload_pseudos);
2364 bitmap_clear (&lra_subreg_reload_pseudos);
2365 bitmap_clear (&lra_inheritance_pseudos);
2366 bitmap_clear (&lra_split_regs);
2367 if (! live_p)
2369 /* We need full live info for spilling pseudos into
2370 registers instead of memory. */
2371 lra_create_live_ranges (lra_reg_spill_p, true);
2372 live_p = true;
2374 /* We should check necessity for spilling here as the above live
2375 range pass can remove spilled pseudos. */
2376 if (! lra_need_for_spills_p ())
2377 break;
2378 /* Now we know what pseudos should be spilled. Try to
2379 rematerialize them first. */
2380 if (lra_remat ())
2382 /* We need full live info -- see the comment above. */
2383 lra_create_live_ranges (lra_reg_spill_p, true);
2384 live_p = true;
2385 if (! lra_need_for_spills_p ())
2386 break;
2388 lra_spill ();
2389 /* Assignment of stack slots changes elimination offsets for
2390 some eliminations. So update the offsets here. */
2391 lra_eliminate (false, false);
2392 lra_constraint_new_regno_start = max_reg_num ();
2393 if (lra_bad_spill_regno_start == INT_MAX
2394 && lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES
2395 && lra_rematerialization_iter > LRA_MAX_REMATERIALIZATION_PASSES)
2396 /* After switching off inheritance and rematerialization
2397 passes, avoid spilling reload pseudos will be created to
2398 prevent LRA cycling in some complicated cases. */
2399 lra_bad_spill_regno_start = lra_constraint_new_regno_start;
2400 lra_assignment_iter_after_spill = 0;
2402 restore_scratches ();
2403 lra_eliminate (true, false);
2404 lra_final_code_change ();
2405 lra_in_progress = 0;
2406 if (live_p)
2407 lra_clear_live_ranges ();
2408 lra_live_ranges_finish ();
2409 lra_constraints_finish ();
2410 finish_reg_info ();
2411 sbitmap_free (lra_constraint_insn_stack_bitmap);
2412 lra_constraint_insn_stack.release ();
2413 finish_insn_recog_data ();
2414 regstat_free_n_sets_and_refs ();
2415 regstat_free_ri ();
2416 reload_completed = 1;
2417 update_inc_notes ();
2419 inserted_p = fixup_abnormal_edges ();
2421 /* We've possibly turned single trapping insn into multiple ones. */
2422 if (cfun->can_throw_non_call_exceptions)
2424 sbitmap blocks;
2425 blocks = sbitmap_alloc (last_basic_block_for_fn (cfun));
2426 bitmap_ones (blocks);
2427 find_many_sub_basic_blocks (blocks);
2428 sbitmap_free (blocks);
2431 if (inserted_p)
2432 commit_edge_insertions ();
2434 /* Replacing pseudos with their memory equivalents might have
2435 created shared rtx. Subsequent passes would get confused
2436 by this, so unshare everything here. */
2437 unshare_all_rtl_again (get_insns ());
2439 #ifdef ENABLE_CHECKING
2440 check_rtl (true);
2441 #endif
2443 timevar_pop (TV_LRA);
2446 /* Called once per compiler to initialize LRA data once. */
2447 void
2448 lra_init_once (void)
2450 init_insn_code_data_once ();
2453 /* Called once per compiler to finish LRA data which are initialize
2454 once. */
2455 void
2456 lra_finish_once (void)
2458 finish_insn_code_data_once ();