Refactor graphite-sese-to-poly, sese.h, graphite-poly.h
[official-gcc.git] / gcc / lra.c
blob55b856fbb0f142a9ab1dde364a17ca6462bf04b8
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 "backend.h"
107 #include "predict.h"
108 #include "tree.h"
109 #include "rtl.h"
110 #include "df.h"
111 #include "tm_p.h"
112 #include "regs.h"
113 #include "insn-config.h"
114 #include "insn-codes.h"
115 #include "recog.h"
116 #include "output.h"
117 #include "addresses.h"
118 #include "flags.h"
119 #include "optabs.h"
120 #include "alias.h"
121 #include "expmed.h"
122 #include "dojump.h"
123 #include "explow.h"
124 #include "calls.h"
125 #include "emit-rtl.h"
126 #include "varasm.h"
127 #include "stmt.h"
128 #include "expr.h"
129 #include "cfgrtl.h"
130 #include "cfgbuild.h"
131 #include "except.h"
132 #include "tree-pass.h"
133 #include "timevar.h"
134 #include "target.h"
135 #include "ira.h"
136 #include "alloc-pool.h"
137 #include "lra.h"
138 #include "insn-attr.h"
139 #include "lra-int.h"
140 #include "print-rtl.h"
142 /* Dump bitmap SET with TITLE and BB INDEX. */
143 void
144 lra_dump_bitmap_with_title (const char *title, bitmap set, int index)
146 unsigned int i;
147 int count;
148 bitmap_iterator bi;
149 static const int max_nums_on_line = 10;
151 if (bitmap_empty_p (set))
152 return;
153 fprintf (lra_dump_file, " %s %d:", title, index);
154 fprintf (lra_dump_file, "\n");
155 count = max_nums_on_line + 1;
156 EXECUTE_IF_SET_IN_BITMAP (set, 0, i, bi)
158 if (count > max_nums_on_line)
160 fprintf (lra_dump_file, "\n ");
161 count = 0;
163 fprintf (lra_dump_file, " %4u", i);
164 count++;
166 fprintf (lra_dump_file, "\n");
169 /* Hard registers currently not available for allocation. It can
170 changed after some hard registers become not eliminable. */
171 HARD_REG_SET lra_no_alloc_regs;
173 static int get_new_reg_value (void);
174 static void expand_reg_info (void);
175 static void invalidate_insn_recog_data (int);
176 static int get_insn_freq (rtx_insn *);
177 static void invalidate_insn_data_regno_info (lra_insn_recog_data_t,
178 rtx_insn *, int);
180 /* Expand all regno related info needed for LRA. */
181 static void
182 expand_reg_data (int old)
184 resize_reg_info ();
185 expand_reg_info ();
186 ira_expand_reg_equiv ();
187 for (int i = (int) max_reg_num () - 1; i >= old; i--)
188 lra_change_class (i, ALL_REGS, " Set", true);
191 /* Create and return a new reg of ORIGINAL mode. If ORIGINAL is NULL
192 or of VOIDmode, use MD_MODE for the new reg. Initialize its
193 register class to RCLASS. Print message about assigning class
194 RCLASS containing new register name TITLE unless it is NULL. Use
195 attributes of ORIGINAL if it is a register. The created register
196 will have unique held value. */
198 lra_create_new_reg_with_unique_value (machine_mode md_mode, rtx original,
199 enum reg_class rclass, const char *title)
201 machine_mode mode;
202 rtx new_reg;
204 if (original == NULL_RTX || (mode = GET_MODE (original)) == VOIDmode)
205 mode = md_mode;
206 lra_assert (mode != VOIDmode);
207 new_reg = gen_reg_rtx (mode);
208 if (original == NULL_RTX || ! REG_P (original))
210 if (lra_dump_file != NULL)
211 fprintf (lra_dump_file, " Creating newreg=%i", REGNO (new_reg));
213 else
215 if (ORIGINAL_REGNO (original) >= FIRST_PSEUDO_REGISTER)
216 ORIGINAL_REGNO (new_reg) = ORIGINAL_REGNO (original);
217 REG_USERVAR_P (new_reg) = REG_USERVAR_P (original);
218 REG_POINTER (new_reg) = REG_POINTER (original);
219 REG_ATTRS (new_reg) = REG_ATTRS (original);
220 if (lra_dump_file != NULL)
221 fprintf (lra_dump_file, " Creating newreg=%i from oldreg=%i",
222 REGNO (new_reg), REGNO (original));
224 if (lra_dump_file != NULL)
226 if (title != NULL)
227 fprintf (lra_dump_file, ", assigning class %s to%s%s r%d",
228 reg_class_names[rclass], *title == '\0' ? "" : " ",
229 title, REGNO (new_reg));
230 fprintf (lra_dump_file, "\n");
232 expand_reg_data (max_reg_num ());
233 setup_reg_classes (REGNO (new_reg), rclass, NO_REGS, rclass);
234 return new_reg;
237 /* Analogous to the previous function but also inherits value of
238 ORIGINAL. */
240 lra_create_new_reg (machine_mode md_mode, rtx original,
241 enum reg_class rclass, const char *title)
243 rtx new_reg;
245 new_reg
246 = lra_create_new_reg_with_unique_value (md_mode, original, rclass, title);
247 if (original != NULL_RTX && REG_P (original))
248 lra_assign_reg_val (REGNO (original), REGNO (new_reg));
249 return new_reg;
252 /* Set up for REGNO unique hold value. */
253 void
254 lra_set_regno_unique_value (int regno)
256 lra_reg_info[regno].val = get_new_reg_value ();
259 /* Invalidate INSN related info used by LRA. The info should never be
260 used after that. */
261 void
262 lra_invalidate_insn_data (rtx_insn *insn)
264 lra_invalidate_insn_regno_info (insn);
265 invalidate_insn_recog_data (INSN_UID (insn));
268 /* Mark INSN deleted and invalidate the insn related info used by
269 LRA. */
270 void
271 lra_set_insn_deleted (rtx_insn *insn)
273 lra_invalidate_insn_data (insn);
274 SET_INSN_DELETED (insn);
277 /* Delete an unneeded INSN and any previous insns who sole purpose is
278 loading data that is dead in INSN. */
279 void
280 lra_delete_dead_insn (rtx_insn *insn)
282 rtx_insn *prev = prev_real_insn (insn);
283 rtx prev_dest;
285 /* If the previous insn sets a register that dies in our insn,
286 delete it too. */
287 if (prev && GET_CODE (PATTERN (prev)) == SET
288 && (prev_dest = SET_DEST (PATTERN (prev)), REG_P (prev_dest))
289 && reg_mentioned_p (prev_dest, PATTERN (insn))
290 && find_regno_note (insn, REG_DEAD, REGNO (prev_dest))
291 && ! side_effects_p (SET_SRC (PATTERN (prev))))
292 lra_delete_dead_insn (prev);
294 lra_set_insn_deleted (insn);
297 /* Emit insn x = y + z. Return NULL if we failed to do it.
298 Otherwise, return the insn. We don't use gen_add3_insn as it might
299 clobber CC. */
300 static rtx_insn *
301 emit_add3_insn (rtx x, rtx y, rtx z)
303 rtx_insn *last;
305 last = get_last_insn ();
307 if (have_addptr3_insn (x, y, z))
309 rtx_insn *insn = gen_addptr3_insn (x, y, z);
311 /* If the target provides an "addptr" pattern it hopefully does
312 for a reason. So falling back to the normal add would be
313 a bug. */
314 lra_assert (insn != NULL_RTX);
315 emit_insn (insn);
316 return insn;
319 rtx_insn *insn = emit_insn (gen_rtx_SET (x, gen_rtx_PLUS (GET_MODE (y),
320 y, z)));
321 if (recog_memoized (insn) < 0)
323 delete_insns_since (last);
324 insn = NULL;
326 return insn;
329 /* Emit insn x = x + y. Return the insn. We use gen_add2_insn as the
330 last resort. */
331 static rtx_insn *
332 emit_add2_insn (rtx x, rtx y)
334 rtx_insn *insn = emit_add3_insn (x, x, y);
335 if (insn == NULL_RTX)
337 insn = gen_add2_insn (x, y);
338 if (insn != NULL_RTX)
339 emit_insn (insn);
341 return insn;
344 /* Target checks operands through operand predicates to recognize an
345 insn. We should have a special precaution to generate add insns
346 which are frequent results of elimination.
348 Emit insns for x = y + z. X can be used to store intermediate
349 values and should be not in Y and Z when we use X to store an
350 intermediate value. Y + Z should form [base] [+ index[ * scale]] [
351 + disp] where base and index are registers, disp and scale are
352 constants. Y should contain base if it is present, Z should
353 contain disp if any. index[*scale] can be part of Y or Z. */
354 void
355 lra_emit_add (rtx x, rtx y, rtx z)
357 int old;
358 rtx_insn *last;
359 rtx a1, a2, base, index, disp, scale, index_scale;
360 bool ok_p;
362 rtx_insn *add3_insn = emit_add3_insn (x, y, z);
363 old = max_reg_num ();
364 if (add3_insn != NULL)
366 else
368 disp = a2 = NULL_RTX;
369 if (GET_CODE (y) == PLUS)
371 a1 = XEXP (y, 0);
372 a2 = XEXP (y, 1);
373 disp = z;
375 else
377 a1 = y;
378 if (CONSTANT_P (z))
379 disp = z;
380 else
381 a2 = z;
383 index_scale = scale = NULL_RTX;
384 if (GET_CODE (a1) == MULT)
386 index_scale = a1;
387 index = XEXP (a1, 0);
388 scale = XEXP (a1, 1);
389 base = a2;
391 else if (a2 != NULL_RTX && GET_CODE (a2) == MULT)
393 index_scale = a2;
394 index = XEXP (a2, 0);
395 scale = XEXP (a2, 1);
396 base = a1;
398 else
400 base = a1;
401 index = a2;
403 if (! (REG_P (base) || GET_CODE (base) == SUBREG)
404 || (index != NULL_RTX
405 && ! (REG_P (index) || GET_CODE (index) == SUBREG))
406 || (disp != NULL_RTX && ! CONSTANT_P (disp))
407 || (scale != NULL_RTX && ! CONSTANT_P (scale)))
409 /* Probably we have no 3 op add. Last chance is to use 2-op
410 add insn. To succeed, don't move Z to X as an address
411 segment always comes in Y. Otherwise, we might fail when
412 adding the address segment to register. */
413 lra_assert (x != y && x != z);
414 emit_move_insn (x, y);
415 rtx_insn *insn = emit_add2_insn (x, z);
416 lra_assert (insn != NULL_RTX);
418 else
420 if (index_scale == NULL_RTX)
421 index_scale = index;
422 if (disp == NULL_RTX)
424 /* Generate x = index_scale; x = x + base. */
425 lra_assert (index_scale != NULL_RTX && base != NULL_RTX);
426 emit_move_insn (x, index_scale);
427 rtx_insn *insn = emit_add2_insn (x, base);
428 lra_assert (insn != NULL_RTX);
430 else if (scale == NULL_RTX)
432 /* Try x = base + disp. */
433 lra_assert (base != NULL_RTX);
434 last = get_last_insn ();
435 rtx_insn *move_insn =
436 emit_move_insn (x, gen_rtx_PLUS (GET_MODE (base), base, disp));
437 if (recog_memoized (move_insn) < 0)
439 delete_insns_since (last);
440 /* Generate x = disp; x = x + base. */
441 emit_move_insn (x, disp);
442 rtx_insn *add2_insn = emit_add2_insn (x, base);
443 lra_assert (add2_insn != NULL_RTX);
445 /* Generate x = x + index. */
446 if (index != NULL_RTX)
448 rtx_insn *insn = emit_add2_insn (x, index);
449 lra_assert (insn != NULL_RTX);
452 else
454 /* Try x = index_scale; x = x + disp; x = x + base. */
455 last = get_last_insn ();
456 rtx_insn *move_insn = emit_move_insn (x, index_scale);
457 ok_p = false;
458 if (recog_memoized (move_insn) >= 0)
460 rtx_insn *insn = emit_add2_insn (x, disp);
461 if (insn != NULL_RTX)
463 insn = emit_add2_insn (x, base);
464 if (insn != NULL_RTX)
465 ok_p = true;
468 if (! ok_p)
470 delete_insns_since (last);
471 /* Generate x = disp; x = x + base; x = x + index_scale. */
472 emit_move_insn (x, disp);
473 rtx_insn *insn = emit_add2_insn (x, base);
474 lra_assert (insn != NULL_RTX);
475 insn = emit_add2_insn (x, index_scale);
476 lra_assert (insn != NULL_RTX);
481 /* Functions emit_... can create pseudos -- so expand the pseudo
482 data. */
483 if (old != max_reg_num ())
484 expand_reg_data (old);
487 /* The number of emitted reload insns so far. */
488 int lra_curr_reload_num;
490 /* Emit x := y, processing special case when y = u + v or y = u + v *
491 scale + w through emit_add (Y can be an address which is base +
492 index reg * scale + displacement in general case). X may be used
493 as intermediate result therefore it should be not in Y. */
494 void
495 lra_emit_move (rtx x, rtx y)
497 int old;
499 if (GET_CODE (y) != PLUS)
501 if (rtx_equal_p (x, y))
502 return;
503 old = max_reg_num ();
504 emit_move_insn (x, y);
505 if (REG_P (x))
506 lra_reg_info[ORIGINAL_REGNO (x)].last_reload = ++lra_curr_reload_num;
507 /* Function emit_move can create pseudos -- so expand the pseudo
508 data. */
509 if (old != max_reg_num ())
510 expand_reg_data (old);
511 return;
513 lra_emit_add (x, XEXP (y, 0), XEXP (y, 1));
516 /* Update insn operands which are duplication of operands whose
517 numbers are in array of NOPS (with end marker -1). The insn is
518 represented by its LRA internal representation ID. */
519 void
520 lra_update_dups (lra_insn_recog_data_t id, signed char *nops)
522 int i, j, nop;
523 struct lra_static_insn_data *static_id = id->insn_static_data;
525 for (i = 0; i < static_id->n_dups; i++)
526 for (j = 0; (nop = nops[j]) >= 0; j++)
527 if (static_id->dup_num[i] == nop)
528 *id->dup_loc[i] = *id->operand_loc[nop];
533 /* This page contains code dealing with info about registers in the
534 insns. */
536 /* Pools for insn reg info. */
537 object_allocator<lra_insn_reg> lra_insn_reg_pool ("insn regs");
539 /* Create LRA insn related info about a reference to REGNO in INSN with
540 TYPE (in/out/inout), biggest reference mode MODE, flag that it is
541 reference through subreg (SUBREG_P), flag that is early clobbered
542 in the insn (EARLY_CLOBBER), and reference to the next insn reg
543 info (NEXT). */
544 static struct lra_insn_reg *
545 new_insn_reg (rtx_insn *insn, int regno, enum op_type type,
546 machine_mode mode,
547 bool subreg_p, bool early_clobber, struct lra_insn_reg *next)
549 lra_insn_reg *ir = lra_insn_reg_pool.allocate ();
550 ir->type = type;
551 ir->biggest_mode = mode;
552 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (lra_reg_info[regno].biggest_mode)
553 && NONDEBUG_INSN_P (insn))
554 lra_reg_info[regno].biggest_mode = mode;
555 ir->subreg_p = subreg_p;
556 ir->early_clobber = early_clobber;
557 ir->regno = regno;
558 ir->next = next;
559 return ir;
562 /* Free insn reg info list IR. */
563 static void
564 free_insn_regs (struct lra_insn_reg *ir)
566 struct lra_insn_reg *next_ir;
568 for (; ir != NULL; ir = next_ir)
570 next_ir = ir->next;
571 lra_insn_reg_pool.remove (ir);
575 /* Finish pool for insn reg info. */
576 static void
577 finish_insn_regs (void)
579 lra_insn_reg_pool.release ();
584 /* This page contains code dealing LRA insn info (or in other words
585 LRA internal insn representation). */
587 /* Map INSN_CODE -> the static insn data. This info is valid during
588 all translation unit. */
589 struct lra_static_insn_data *insn_code_data[NUM_INSN_CODES];
591 /* Debug insns are represented as a special insn with one input
592 operand which is RTL expression in var_location. */
594 /* The following data are used as static insn operand data for all
595 debug insns. If structure lra_operand_data is changed, the
596 initializer should be changed too. */
597 static struct lra_operand_data debug_operand_data =
599 NULL, /* alternative */
600 VOIDmode, /* We are not interesting in the operand mode. */
601 OP_IN,
602 0, 0, 0, 0
605 /* The following data are used as static insn data for all debug
606 insns. If structure lra_static_insn_data is changed, the
607 initializer should be changed too. */
608 static struct lra_static_insn_data debug_insn_static_data =
610 &debug_operand_data,
611 0, /* Duplication operands #. */
612 -1, /* Commutative operand #. */
613 1, /* Operands #. There is only one operand which is debug RTL
614 expression. */
615 0, /* Duplications #. */
616 0, /* Alternatives #. We are not interesting in alternatives
617 because we does not proceed debug_insns for reloads. */
618 NULL, /* Hard registers referenced in machine description. */
619 NULL /* Descriptions of operands in alternatives. */
622 /* Called once per compiler work to initialize some LRA data related
623 to insns. */
624 static void
625 init_insn_code_data_once (void)
627 memset (insn_code_data, 0, sizeof (insn_code_data));
630 /* Called once per compiler work to finalize some LRA data related to
631 insns. */
632 static void
633 finish_insn_code_data_once (void)
635 for (unsigned int i = 0; i < NUM_INSN_CODES; i++)
637 if (insn_code_data[i] != NULL)
638 free (insn_code_data[i]);
642 /* Return static insn data, allocate and setup if necessary. Although
643 dup_num is static data (it depends only on icode), to set it up we
644 need to extract insn first. So recog_data should be valid for
645 normal insn (ICODE >= 0) before the call. */
646 static struct lra_static_insn_data *
647 get_static_insn_data (int icode, int nop, int ndup, int nalt)
649 struct lra_static_insn_data *data;
650 size_t n_bytes;
652 lra_assert (icode < (int) NUM_INSN_CODES);
653 if (icode >= 0 && (data = insn_code_data[icode]) != NULL)
654 return data;
655 lra_assert (nop >= 0 && ndup >= 0 && nalt >= 0);
656 n_bytes = sizeof (struct lra_static_insn_data)
657 + sizeof (struct lra_operand_data) * nop
658 + sizeof (int) * ndup;
659 data = XNEWVAR (struct lra_static_insn_data, n_bytes);
660 data->operand_alternative = NULL;
661 data->n_operands = nop;
662 data->n_dups = ndup;
663 data->n_alternatives = nalt;
664 data->operand = ((struct lra_operand_data *)
665 ((char *) data + sizeof (struct lra_static_insn_data)));
666 data->dup_num = ((int *) ((char *) data->operand
667 + sizeof (struct lra_operand_data) * nop));
668 if (icode >= 0)
670 int i;
672 insn_code_data[icode] = data;
673 for (i = 0; i < nop; i++)
675 data->operand[i].constraint
676 = insn_data[icode].operand[i].constraint;
677 data->operand[i].mode = insn_data[icode].operand[i].mode;
678 data->operand[i].strict_low = insn_data[icode].operand[i].strict_low;
679 data->operand[i].is_operator
680 = insn_data[icode].operand[i].is_operator;
681 data->operand[i].type
682 = (data->operand[i].constraint[0] == '=' ? OP_OUT
683 : data->operand[i].constraint[0] == '+' ? OP_INOUT
684 : OP_IN);
685 data->operand[i].is_address = false;
687 for (i = 0; i < ndup; i++)
688 data->dup_num[i] = recog_data.dup_num[i];
690 return data;
693 /* The current length of the following array. */
694 int lra_insn_recog_data_len;
696 /* Map INSN_UID -> the insn recog data (NULL if unknown). */
697 lra_insn_recog_data_t *lra_insn_recog_data;
699 /* Initialize LRA data about insns. */
700 static void
701 init_insn_recog_data (void)
703 lra_insn_recog_data_len = 0;
704 lra_insn_recog_data = NULL;
707 /* Expand, if necessary, LRA data about insns. */
708 static void
709 check_and_expand_insn_recog_data (int index)
711 int i, old;
713 if (lra_insn_recog_data_len > index)
714 return;
715 old = lra_insn_recog_data_len;
716 lra_insn_recog_data_len = index * 3 / 2 + 1;
717 lra_insn_recog_data = XRESIZEVEC (lra_insn_recog_data_t,
718 lra_insn_recog_data,
719 lra_insn_recog_data_len);
720 for (i = old; i < lra_insn_recog_data_len; i++)
721 lra_insn_recog_data[i] = NULL;
724 /* Finish LRA DATA about insn. */
725 static void
726 free_insn_recog_data (lra_insn_recog_data_t data)
728 if (data->operand_loc != NULL)
729 free (data->operand_loc);
730 if (data->dup_loc != NULL)
731 free (data->dup_loc);
732 if (data->arg_hard_regs != NULL)
733 free (data->arg_hard_regs);
734 if (data->icode < 0 && NONDEBUG_INSN_P (data->insn))
736 if (data->insn_static_data->operand_alternative != NULL)
737 free (const_cast <operand_alternative *>
738 (data->insn_static_data->operand_alternative));
739 free_insn_regs (data->insn_static_data->hard_regs);
740 free (data->insn_static_data);
742 free_insn_regs (data->regs);
743 data->regs = NULL;
744 free (data);
747 /* Pools for copies. */
748 static object_allocator<lra_copy> lra_copy_pool ("lra copies");
750 /* Finish LRA data about all insns. */
751 static void
752 finish_insn_recog_data (void)
754 int i;
755 lra_insn_recog_data_t data;
757 for (i = 0; i < lra_insn_recog_data_len; i++)
758 if ((data = lra_insn_recog_data[i]) != NULL)
759 free_insn_recog_data (data);
760 finish_insn_regs ();
761 lra_copy_pool.release ();
762 lra_insn_reg_pool.release ();
763 free (lra_insn_recog_data);
766 /* Setup info about operands in alternatives of LRA DATA of insn. */
767 static void
768 setup_operand_alternative (lra_insn_recog_data_t data,
769 const operand_alternative *op_alt)
771 int i, j, nop, nalt;
772 int icode = data->icode;
773 struct lra_static_insn_data *static_data = data->insn_static_data;
775 static_data->commutative = -1;
776 nop = static_data->n_operands;
777 nalt = static_data->n_alternatives;
778 static_data->operand_alternative = op_alt;
779 for (i = 0; i < nop; i++)
781 static_data->operand[i].early_clobber = false;
782 static_data->operand[i].is_address = false;
783 if (static_data->operand[i].constraint[0] == '%')
785 /* We currently only support one commutative pair of operands. */
786 if (static_data->commutative < 0)
787 static_data->commutative = i;
788 else
789 lra_assert (icode < 0); /* Asm */
790 /* The last operand should not be marked commutative. */
791 lra_assert (i != nop - 1);
794 for (j = 0; j < nalt; j++)
795 for (i = 0; i < nop; i++, op_alt++)
797 static_data->operand[i].early_clobber |= op_alt->earlyclobber;
798 static_data->operand[i].is_address |= op_alt->is_address;
802 /* Recursively process X and collect info about registers, which are
803 not the insn operands, in X with TYPE (in/out/inout) and flag that
804 it is early clobbered in the insn (EARLY_CLOBBER) and add the info
805 to LIST. X is a part of insn given by DATA. Return the result
806 list. */
807 static struct lra_insn_reg *
808 collect_non_operand_hard_regs (rtx *x, lra_insn_recog_data_t data,
809 struct lra_insn_reg *list,
810 enum op_type type, bool early_clobber)
812 int i, j, regno, last;
813 bool subreg_p;
814 machine_mode mode;
815 struct lra_insn_reg *curr;
816 rtx op = *x;
817 enum rtx_code code = GET_CODE (op);
818 const char *fmt = GET_RTX_FORMAT (code);
820 for (i = 0; i < data->insn_static_data->n_operands; i++)
821 if (x == data->operand_loc[i])
822 /* It is an operand loc. Stop here. */
823 return list;
824 for (i = 0; i < data->insn_static_data->n_dups; i++)
825 if (x == data->dup_loc[i])
826 /* It is a dup loc. Stop here. */
827 return list;
828 mode = GET_MODE (op);
829 subreg_p = false;
830 if (code == SUBREG)
832 op = SUBREG_REG (op);
833 code = GET_CODE (op);
834 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (op)))
836 mode = GET_MODE (op);
837 if (GET_MODE_SIZE (mode) > REGMODE_NATURAL_SIZE (mode))
838 subreg_p = true;
841 if (REG_P (op))
843 if ((regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER)
844 return list;
845 /* Process all regs even unallocatable ones as we need info
846 about all regs for rematerialization pass. */
847 for (last = regno + hard_regno_nregs[regno][mode];
848 regno < last;
849 regno++)
851 for (curr = list; curr != NULL; curr = curr->next)
852 if (curr->regno == regno && curr->subreg_p == subreg_p
853 && curr->biggest_mode == mode)
855 if (curr->type != type)
856 curr->type = OP_INOUT;
857 if (curr->early_clobber != early_clobber)
858 curr->early_clobber = true;
859 break;
861 if (curr == NULL)
863 /* This is a new hard regno or the info can not be
864 integrated into the found structure. */
865 #ifdef STACK_REGS
866 early_clobber
867 = (early_clobber
868 /* This clobber is to inform popping floating
869 point stack only. */
870 && ! (FIRST_STACK_REG <= regno
871 && regno <= LAST_STACK_REG));
872 #endif
873 list = new_insn_reg (data->insn, regno, type, mode, subreg_p,
874 early_clobber, list);
877 return list;
879 switch (code)
881 case SET:
882 list = collect_non_operand_hard_regs (&SET_DEST (op), data,
883 list, OP_OUT, false);
884 list = collect_non_operand_hard_regs (&SET_SRC (op), data,
885 list, OP_IN, false);
886 break;
887 case CLOBBER:
888 /* We treat clobber of non-operand hard registers as early
889 clobber (the behavior is expected from asm). */
890 list = collect_non_operand_hard_regs (&XEXP (op, 0), data,
891 list, OP_OUT, true);
892 break;
893 case PRE_INC: case PRE_DEC: case POST_INC: case POST_DEC:
894 list = collect_non_operand_hard_regs (&XEXP (op, 0), data,
895 list, OP_INOUT, false);
896 break;
897 case PRE_MODIFY: case POST_MODIFY:
898 list = collect_non_operand_hard_regs (&XEXP (op, 0), data,
899 list, OP_INOUT, false);
900 list = collect_non_operand_hard_regs (&XEXP (op, 1), data,
901 list, OP_IN, false);
902 break;
903 default:
904 fmt = GET_RTX_FORMAT (code);
905 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
907 if (fmt[i] == 'e')
908 list = collect_non_operand_hard_regs (&XEXP (op, i), data,
909 list, OP_IN, false);
910 else if (fmt[i] == 'E')
911 for (j = XVECLEN (op, i) - 1; j >= 0; j--)
912 list = collect_non_operand_hard_regs (&XVECEXP (op, i, j), data,
913 list, OP_IN, false);
916 return list;
919 /* Set up and return info about INSN. Set up the info if it is not set up
920 yet. */
921 lra_insn_recog_data_t
922 lra_set_insn_recog_data (rtx_insn *insn)
924 lra_insn_recog_data_t data;
925 int i, n, icode;
926 rtx **locs;
927 unsigned int uid = INSN_UID (insn);
928 struct lra_static_insn_data *insn_static_data;
930 check_and_expand_insn_recog_data (uid);
931 if (DEBUG_INSN_P (insn))
932 icode = -1;
933 else
935 icode = INSN_CODE (insn);
936 if (icode < 0)
937 /* It might be a new simple insn which is not recognized yet. */
938 INSN_CODE (insn) = icode = recog_memoized (insn);
940 data = XNEW (struct lra_insn_recog_data);
941 lra_insn_recog_data[uid] = data;
942 data->insn = insn;
943 data->used_insn_alternative = -1;
944 data->icode = icode;
945 data->regs = NULL;
946 if (DEBUG_INSN_P (insn))
948 data->insn_static_data = &debug_insn_static_data;
949 data->dup_loc = NULL;
950 data->arg_hard_regs = NULL;
951 data->preferred_alternatives = ALL_ALTERNATIVES;
952 data->operand_loc = XNEWVEC (rtx *, 1);
953 data->operand_loc[0] = &INSN_VAR_LOCATION_LOC (insn);
954 return data;
956 if (icode < 0)
958 int nop, nalt;
959 machine_mode operand_mode[MAX_RECOG_OPERANDS];
960 const char *constraints[MAX_RECOG_OPERANDS];
962 nop = asm_noperands (PATTERN (insn));
963 data->operand_loc = data->dup_loc = NULL;
964 nalt = 1;
965 if (nop < 0)
967 /* It is a special insn like USE or CLOBBER. We should
968 recognize any regular insn otherwise LRA can do nothing
969 with this insn. */
970 gcc_assert (GET_CODE (PATTERN (insn)) == USE
971 || GET_CODE (PATTERN (insn)) == CLOBBER
972 || GET_CODE (PATTERN (insn)) == ASM_INPUT);
973 data->insn_static_data = insn_static_data
974 = get_static_insn_data (-1, 0, 0, nalt);
976 else
978 /* expand_asm_operands makes sure there aren't too many
979 operands. */
980 lra_assert (nop <= MAX_RECOG_OPERANDS);
981 if (nop != 0)
982 data->operand_loc = XNEWVEC (rtx *, nop);
983 /* Now get the operand values and constraints out of the
984 insn. */
985 decode_asm_operands (PATTERN (insn), NULL,
986 data->operand_loc,
987 constraints, operand_mode, NULL);
988 if (nop > 0)
990 const char *p = recog_data.constraints[0];
992 for (p = constraints[0]; *p; p++)
993 nalt += *p == ',';
995 data->insn_static_data = insn_static_data
996 = get_static_insn_data (-1, nop, 0, nalt);
997 for (i = 0; i < nop; i++)
999 insn_static_data->operand[i].mode = operand_mode[i];
1000 insn_static_data->operand[i].constraint = constraints[i];
1001 insn_static_data->operand[i].strict_low = false;
1002 insn_static_data->operand[i].is_operator = false;
1003 insn_static_data->operand[i].is_address = false;
1006 for (i = 0; i < insn_static_data->n_operands; i++)
1007 insn_static_data->operand[i].type
1008 = (insn_static_data->operand[i].constraint[0] == '=' ? OP_OUT
1009 : insn_static_data->operand[i].constraint[0] == '+' ? OP_INOUT
1010 : OP_IN);
1011 data->preferred_alternatives = ALL_ALTERNATIVES;
1012 if (nop > 0)
1014 operand_alternative *op_alt = XCNEWVEC (operand_alternative,
1015 nalt * nop);
1016 preprocess_constraints (nop, nalt, constraints, op_alt);
1017 setup_operand_alternative (data, op_alt);
1020 else
1022 insn_extract (insn);
1023 data->insn_static_data = insn_static_data
1024 = get_static_insn_data (icode, insn_data[icode].n_operands,
1025 insn_data[icode].n_dups,
1026 insn_data[icode].n_alternatives);
1027 n = insn_static_data->n_operands;
1028 if (n == 0)
1029 locs = NULL;
1030 else
1032 locs = XNEWVEC (rtx *, n);
1033 memcpy (locs, recog_data.operand_loc, n * sizeof (rtx *));
1035 data->operand_loc = locs;
1036 n = insn_static_data->n_dups;
1037 if (n == 0)
1038 locs = NULL;
1039 else
1041 locs = XNEWVEC (rtx *, n);
1042 memcpy (locs, recog_data.dup_loc, n * sizeof (rtx *));
1044 data->dup_loc = locs;
1045 data->preferred_alternatives = get_preferred_alternatives (insn);
1046 const operand_alternative *op_alt = preprocess_insn_constraints (icode);
1047 if (!insn_static_data->operand_alternative)
1048 setup_operand_alternative (data, op_alt);
1049 else if (op_alt != insn_static_data->operand_alternative)
1050 insn_static_data->operand_alternative = op_alt;
1052 if (GET_CODE (PATTERN (insn)) == CLOBBER || GET_CODE (PATTERN (insn)) == USE)
1053 insn_static_data->hard_regs = NULL;
1054 else
1055 insn_static_data->hard_regs
1056 = collect_non_operand_hard_regs (&PATTERN (insn), data,
1057 NULL, OP_IN, false);
1058 data->arg_hard_regs = NULL;
1059 if (CALL_P (insn))
1061 bool use_p;
1062 rtx link;
1063 int n_hard_regs, regno, arg_hard_regs[FIRST_PSEUDO_REGISTER];
1065 n_hard_regs = 0;
1066 /* Finding implicit hard register usage. We believe it will be
1067 not changed whatever transformations are used. Call insns
1068 are such example. */
1069 for (link = CALL_INSN_FUNCTION_USAGE (insn);
1070 link != NULL_RTX;
1071 link = XEXP (link, 1))
1072 if (((use_p = GET_CODE (XEXP (link, 0)) == USE)
1073 || GET_CODE (XEXP (link, 0)) == CLOBBER)
1074 && REG_P (XEXP (XEXP (link, 0), 0)))
1076 regno = REGNO (XEXP (XEXP (link, 0), 0));
1077 lra_assert (regno < FIRST_PSEUDO_REGISTER);
1078 /* It is an argument register. */
1079 for (i = REG_NREGS (XEXP (XEXP (link, 0), 0)) - 1; i >= 0; i--)
1080 arg_hard_regs[n_hard_regs++]
1081 = regno + i + (use_p ? 0 : FIRST_PSEUDO_REGISTER);
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 /* Vec referring to pseudo copies. */
1285 static vec<lra_copy_t> copy_vec;
1287 /* Initialize I-th element of lra_reg_info. */
1288 static inline void
1289 initialize_lra_reg_info_element (int i)
1291 bitmap_initialize (&lra_reg_info[i].insn_bitmap, &reg_obstack);
1292 #ifdef STACK_REGS
1293 lra_reg_info[i].no_stack_p = false;
1294 #endif
1295 CLEAR_HARD_REG_SET (lra_reg_info[i].conflict_hard_regs);
1296 CLEAR_HARD_REG_SET (lra_reg_info[i].actual_call_used_reg_set);
1297 lra_reg_info[i].preferred_hard_regno1 = -1;
1298 lra_reg_info[i].preferred_hard_regno2 = -1;
1299 lra_reg_info[i].preferred_hard_regno_profit1 = 0;
1300 lra_reg_info[i].preferred_hard_regno_profit2 = 0;
1301 lra_reg_info[i].biggest_mode = VOIDmode;
1302 lra_reg_info[i].live_ranges = NULL;
1303 lra_reg_info[i].nrefs = lra_reg_info[i].freq = 0;
1304 lra_reg_info[i].last_reload = 0;
1305 lra_reg_info[i].restore_regno = -1;
1306 lra_reg_info[i].val = get_new_reg_value ();
1307 lra_reg_info[i].offset = 0;
1308 lra_reg_info[i].copies = NULL;
1311 /* Initialize common reg info and copies. */
1312 static void
1313 init_reg_info (void)
1315 int i;
1317 last_reg_value = 0;
1318 reg_info_size = max_reg_num () * 3 / 2 + 1;
1319 lra_reg_info = XNEWVEC (struct lra_reg, reg_info_size);
1320 for (i = 0; i < reg_info_size; i++)
1321 initialize_lra_reg_info_element (i);
1322 copy_vec.create (100);
1326 /* Finish common reg info and copies. */
1327 static void
1328 finish_reg_info (void)
1330 int i;
1332 for (i = 0; i < reg_info_size; i++)
1333 bitmap_clear (&lra_reg_info[i].insn_bitmap);
1334 free (lra_reg_info);
1335 reg_info_size = 0;
1338 /* Expand common reg info if it is necessary. */
1339 static void
1340 expand_reg_info (void)
1342 int i, old = reg_info_size;
1344 if (reg_info_size > max_reg_num ())
1345 return;
1346 reg_info_size = max_reg_num () * 3 / 2 + 1;
1347 lra_reg_info = XRESIZEVEC (struct lra_reg, lra_reg_info, reg_info_size);
1348 for (i = old; i < reg_info_size; i++)
1349 initialize_lra_reg_info_element (i);
1352 /* Free all copies. */
1353 void
1354 lra_free_copies (void)
1356 lra_copy_t cp;
1358 while (copy_vec.length () != 0)
1360 cp = copy_vec.pop ();
1361 lra_reg_info[cp->regno1].copies = lra_reg_info[cp->regno2].copies = NULL;
1362 lra_copy_pool.remove (cp);
1366 /* Create copy of two pseudos REGNO1 and REGNO2. The copy execution
1367 frequency is FREQ. */
1368 void
1369 lra_create_copy (int regno1, int regno2, int freq)
1371 bool regno1_dest_p;
1372 lra_copy_t cp;
1374 lra_assert (regno1 != regno2);
1375 regno1_dest_p = true;
1376 if (regno1 > regno2)
1378 std::swap (regno1, regno2);
1379 regno1_dest_p = false;
1381 cp = lra_copy_pool.allocate ();
1382 copy_vec.safe_push (cp);
1383 cp->regno1_dest_p = regno1_dest_p;
1384 cp->freq = freq;
1385 cp->regno1 = regno1;
1386 cp->regno2 = regno2;
1387 cp->regno1_next = lra_reg_info[regno1].copies;
1388 lra_reg_info[regno1].copies = cp;
1389 cp->regno2_next = lra_reg_info[regno2].copies;
1390 lra_reg_info[regno2].copies = cp;
1391 if (lra_dump_file != NULL)
1392 fprintf (lra_dump_file, " Creating copy r%d%sr%d@%d\n",
1393 regno1, regno1_dest_p ? "<-" : "->", regno2, freq);
1396 /* Return N-th (0, 1, ...) copy. If there is no copy, return
1397 NULL. */
1398 lra_copy_t
1399 lra_get_copy (int n)
1401 if (n >= (int) copy_vec.length ())
1402 return NULL;
1403 return copy_vec[n];
1408 /* This page contains code dealing with info about registers in
1409 insns. */
1411 /* Process X of insn UID recursively and add info (operand type is
1412 given by TYPE, flag of that it is early clobber is EARLY_CLOBBER)
1413 about registers in X to the insn DATA. */
1414 static void
1415 add_regs_to_insn_regno_info (lra_insn_recog_data_t data, rtx x, int uid,
1416 enum op_type type, bool early_clobber)
1418 int i, j, regno;
1419 bool subreg_p;
1420 machine_mode mode;
1421 const char *fmt;
1422 enum rtx_code code;
1423 struct lra_insn_reg *curr;
1425 code = GET_CODE (x);
1426 mode = GET_MODE (x);
1427 subreg_p = false;
1428 if (GET_CODE (x) == SUBREG)
1430 x = SUBREG_REG (x);
1431 code = GET_CODE (x);
1432 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (x)))
1434 mode = GET_MODE (x);
1435 if (GET_MODE_SIZE (mode) > REGMODE_NATURAL_SIZE (mode))
1436 subreg_p = true;
1439 if (REG_P (x))
1441 regno = REGNO (x);
1442 /* Process all regs even unallocatable ones as we need info about
1443 all regs for rematerialization pass. */
1444 expand_reg_info ();
1445 if (bitmap_set_bit (&lra_reg_info[regno].insn_bitmap, uid))
1447 data->regs = new_insn_reg (data->insn, regno, type, mode, subreg_p,
1448 early_clobber, data->regs);
1449 return;
1451 else
1453 for (curr = data->regs; curr != NULL; curr = curr->next)
1454 if (curr->regno == regno)
1456 if (curr->subreg_p != subreg_p || curr->biggest_mode != mode)
1457 /* The info can not be integrated into the found
1458 structure. */
1459 data->regs = new_insn_reg (data->insn, regno, type, mode,
1460 subreg_p, early_clobber,
1461 data->regs);
1462 else
1464 if (curr->type != type)
1465 curr->type = OP_INOUT;
1466 if (curr->early_clobber != early_clobber)
1467 curr->early_clobber = true;
1469 return;
1471 gcc_unreachable ();
1475 switch (code)
1477 case SET:
1478 add_regs_to_insn_regno_info (data, SET_DEST (x), uid, OP_OUT, false);
1479 add_regs_to_insn_regno_info (data, SET_SRC (x), uid, OP_IN, false);
1480 break;
1481 case CLOBBER:
1482 /* We treat clobber of non-operand hard registers as early
1483 clobber (the behavior is expected from asm). */
1484 add_regs_to_insn_regno_info (data, XEXP (x, 0), uid, OP_OUT, true);
1485 break;
1486 case PRE_INC: case PRE_DEC: case POST_INC: case POST_DEC:
1487 add_regs_to_insn_regno_info (data, XEXP (x, 0), uid, OP_INOUT, false);
1488 break;
1489 case PRE_MODIFY: case POST_MODIFY:
1490 add_regs_to_insn_regno_info (data, XEXP (x, 0), uid, OP_INOUT, false);
1491 add_regs_to_insn_regno_info (data, XEXP (x, 1), uid, OP_IN, false);
1492 break;
1493 default:
1494 if ((code != PARALLEL && code != EXPR_LIST) || type != OP_OUT)
1495 /* Some targets place small structures in registers for return
1496 values of functions, and those registers are wrapped in
1497 PARALLEL that we may see as the destination of a SET. Here
1498 is an example:
1500 (call_insn 13 12 14 2 (set (parallel:BLK [
1501 (expr_list:REG_DEP_TRUE (reg:DI 0 ax)
1502 (const_int 0 [0]))
1503 (expr_list:REG_DEP_TRUE (reg:DI 1 dx)
1504 (const_int 8 [0x8]))
1506 (call (mem:QI (symbol_ref:DI (... */
1507 type = OP_IN;
1508 fmt = GET_RTX_FORMAT (code);
1509 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1511 if (fmt[i] == 'e')
1512 add_regs_to_insn_regno_info (data, XEXP (x, i), uid, type, false);
1513 else if (fmt[i] == 'E')
1515 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1516 add_regs_to_insn_regno_info (data, XVECEXP (x, i, j), uid,
1517 type, false);
1523 /* Return execution frequency of INSN. */
1524 static int
1525 get_insn_freq (rtx_insn *insn)
1527 basic_block bb = BLOCK_FOR_INSN (insn);
1529 gcc_checking_assert (bb != NULL);
1530 return REG_FREQ_FROM_BB (bb);
1533 /* Invalidate all reg info of INSN with DATA and execution frequency
1534 FREQ. Update common info about the invalidated registers. */
1535 static void
1536 invalidate_insn_data_regno_info (lra_insn_recog_data_t data, rtx_insn *insn,
1537 int freq)
1539 int uid;
1540 bool debug_p;
1541 unsigned int i;
1542 struct lra_insn_reg *ir, *next_ir;
1544 uid = INSN_UID (insn);
1545 debug_p = DEBUG_INSN_P (insn);
1546 for (ir = data->regs; ir != NULL; ir = next_ir)
1548 i = ir->regno;
1549 next_ir = ir->next;
1550 lra_insn_reg_pool.remove (ir);
1551 bitmap_clear_bit (&lra_reg_info[i].insn_bitmap, uid);
1552 if (i >= FIRST_PSEUDO_REGISTER && ! debug_p)
1554 lra_reg_info[i].nrefs--;
1555 lra_reg_info[i].freq -= freq;
1556 lra_assert (lra_reg_info[i].nrefs >= 0 && lra_reg_info[i].freq >= 0);
1559 data->regs = NULL;
1562 /* Invalidate all reg info of INSN. Update common info about the
1563 invalidated registers. */
1564 void
1565 lra_invalidate_insn_regno_info (rtx_insn *insn)
1567 invalidate_insn_data_regno_info (lra_get_insn_recog_data (insn), insn,
1568 get_insn_freq (insn));
1571 /* Update common reg info from reg info of insn given by its DATA and
1572 execution frequency FREQ. */
1573 static void
1574 setup_insn_reg_info (lra_insn_recog_data_t data, int freq)
1576 unsigned int i;
1577 struct lra_insn_reg *ir;
1579 for (ir = data->regs; ir != NULL; ir = ir->next)
1580 if ((i = ir->regno) >= FIRST_PSEUDO_REGISTER)
1582 lra_reg_info[i].nrefs++;
1583 lra_reg_info[i].freq += freq;
1587 /* Set up insn reg info of INSN. Update common reg info from reg info
1588 of INSN. */
1589 void
1590 lra_update_insn_regno_info (rtx_insn *insn)
1592 int i, uid, freq;
1593 lra_insn_recog_data_t data;
1594 struct lra_static_insn_data *static_data;
1595 enum rtx_code code;
1596 rtx link;
1598 if (! INSN_P (insn))
1599 return;
1600 data = lra_get_insn_recog_data (insn);
1601 static_data = data->insn_static_data;
1602 freq = get_insn_freq (insn);
1603 invalidate_insn_data_regno_info (data, insn, freq);
1604 uid = INSN_UID (insn);
1605 for (i = static_data->n_operands - 1; i >= 0; i--)
1606 add_regs_to_insn_regno_info (data, *data->operand_loc[i], uid,
1607 static_data->operand[i].type,
1608 static_data->operand[i].early_clobber);
1609 if ((code = GET_CODE (PATTERN (insn))) == CLOBBER || code == USE)
1610 add_regs_to_insn_regno_info (data, XEXP (PATTERN (insn), 0), uid,
1611 code == USE ? OP_IN : OP_OUT, false);
1612 if (CALL_P (insn))
1613 /* On some targets call insns can refer to pseudos in memory in
1614 CALL_INSN_FUNCTION_USAGE list. Process them in order to
1615 consider their occurrences in calls for different
1616 transformations (e.g. inheritance) with given pseudos. */
1617 for (link = CALL_INSN_FUNCTION_USAGE (insn);
1618 link != NULL_RTX;
1619 link = XEXP (link, 1))
1620 if (((code = GET_CODE (XEXP (link, 0))) == USE || code == CLOBBER)
1621 && MEM_P (XEXP (XEXP (link, 0), 0)))
1622 add_regs_to_insn_regno_info (data, XEXP (XEXP (link, 0), 0), uid,
1623 code == USE ? OP_IN : OP_OUT, false);
1624 if (NONDEBUG_INSN_P (insn))
1625 setup_insn_reg_info (data, freq);
1628 /* Return reg info of insn given by it UID. */
1629 struct lra_insn_reg *
1630 lra_get_insn_regs (int uid)
1632 lra_insn_recog_data_t data;
1634 data = get_insn_recog_data_by_uid (uid);
1635 return data->regs;
1640 /* This page contains code dealing with stack of the insns which
1641 should be processed by the next constraint pass. */
1643 /* Bitmap used to put an insn on the stack only in one exemplar. */
1644 static sbitmap lra_constraint_insn_stack_bitmap;
1646 /* The stack itself. */
1647 vec<rtx_insn *> lra_constraint_insn_stack;
1649 /* Put INSN on the stack. If ALWAYS_UPDATE is true, always update the reg
1650 info for INSN, otherwise only update it if INSN is not already on the
1651 stack. */
1652 static inline void
1653 lra_push_insn_1 (rtx_insn *insn, bool always_update)
1655 unsigned int uid = INSN_UID (insn);
1656 if (always_update)
1657 lra_update_insn_regno_info (insn);
1658 if (uid >= SBITMAP_SIZE (lra_constraint_insn_stack_bitmap))
1659 lra_constraint_insn_stack_bitmap =
1660 sbitmap_resize (lra_constraint_insn_stack_bitmap, 3 * uid / 2, 0);
1661 if (bitmap_bit_p (lra_constraint_insn_stack_bitmap, uid))
1662 return;
1663 bitmap_set_bit (lra_constraint_insn_stack_bitmap, uid);
1664 if (! always_update)
1665 lra_update_insn_regno_info (insn);
1666 lra_constraint_insn_stack.safe_push (insn);
1669 /* Put INSN on the stack. */
1670 void
1671 lra_push_insn (rtx_insn *insn)
1673 lra_push_insn_1 (insn, false);
1676 /* Put INSN on the stack and update its reg info. */
1677 void
1678 lra_push_insn_and_update_insn_regno_info (rtx_insn *insn)
1680 lra_push_insn_1 (insn, true);
1683 /* Put insn with UID on the stack. */
1684 void
1685 lra_push_insn_by_uid (unsigned int uid)
1687 lra_push_insn (lra_insn_recog_data[uid]->insn);
1690 /* Take the last-inserted insns off the stack and return it. */
1691 rtx_insn *
1692 lra_pop_insn (void)
1694 rtx_insn *insn = lra_constraint_insn_stack.pop ();
1695 bitmap_clear_bit (lra_constraint_insn_stack_bitmap, INSN_UID (insn));
1696 return insn;
1699 /* Return the current size of the insn stack. */
1700 unsigned int
1701 lra_insn_stack_length (void)
1703 return lra_constraint_insn_stack.length ();
1706 /* Push insns FROM to TO (excluding it) going in reverse order. */
1707 static void
1708 push_insns (rtx_insn *from, rtx_insn *to)
1710 rtx_insn *insn;
1712 if (from == NULL_RTX)
1713 return;
1714 for (insn = from; insn != to; insn = PREV_INSN (insn))
1715 if (INSN_P (insn))
1716 lra_push_insn (insn);
1719 /* Set up sp offset for insn in range [FROM, LAST]. The offset is
1720 taken from the next BB insn after LAST or zero if there in such
1721 insn. */
1722 static void
1723 setup_sp_offset (rtx_insn *from, rtx_insn *last)
1725 rtx_insn *before = next_nonnote_insn_bb (last);
1726 HOST_WIDE_INT offset = (before == NULL_RTX || ! INSN_P (before)
1727 ? 0 : lra_get_insn_recog_data (before)->sp_offset);
1729 for (rtx_insn *insn = from; insn != NEXT_INSN (last); insn = NEXT_INSN (insn))
1730 lra_get_insn_recog_data (insn)->sp_offset = offset;
1733 /* Emit insns BEFORE before INSN and insns AFTER after INSN. Put the
1734 insns onto the stack. Print about emitting the insns with
1735 TITLE. */
1736 void
1737 lra_process_new_insns (rtx_insn *insn, rtx_insn *before, rtx_insn *after,
1738 const char *title)
1740 rtx_insn *last;
1742 if (before == NULL_RTX && after == NULL_RTX)
1743 return;
1744 if (lra_dump_file != NULL)
1746 dump_insn_slim (lra_dump_file, insn);
1747 if (before != NULL_RTX)
1749 fprintf (lra_dump_file," %s before:\n", title);
1750 dump_rtl_slim (lra_dump_file, before, NULL, -1, 0);
1752 if (after != NULL_RTX)
1754 fprintf (lra_dump_file, " %s after:\n", title);
1755 dump_rtl_slim (lra_dump_file, after, NULL, -1, 0);
1757 fprintf (lra_dump_file, "\n");
1759 if (before != NULL_RTX)
1761 emit_insn_before (before, insn);
1762 push_insns (PREV_INSN (insn), PREV_INSN (before));
1763 setup_sp_offset (before, PREV_INSN (insn));
1765 if (after != NULL_RTX)
1767 for (last = after; NEXT_INSN (last) != NULL_RTX; last = NEXT_INSN (last))
1769 emit_insn_after (after, insn);
1770 push_insns (last, insn);
1771 setup_sp_offset (after, last);
1777 /* Replace all references to register OLD_REGNO in *LOC with pseudo
1778 register NEW_REG. Try to simplify subreg of constant if SUBREG_P.
1779 Return true if any change was made. */
1780 bool
1781 lra_substitute_pseudo (rtx *loc, int old_regno, rtx new_reg, bool subreg_p)
1783 rtx x = *loc;
1784 bool result = false;
1785 enum rtx_code code;
1786 const char *fmt;
1787 int i, j;
1789 if (x == NULL_RTX)
1790 return false;
1792 code = GET_CODE (x);
1793 if (code == SUBREG && subreg_p)
1795 rtx subst, inner = SUBREG_REG (x);
1796 /* Transform subreg of constant while we still have inner mode
1797 of the subreg. The subreg internal should not be an insn
1798 operand. */
1799 if (REG_P (inner) && (int) REGNO (inner) == old_regno
1800 && CONSTANT_P (new_reg)
1801 && (subst = simplify_subreg (GET_MODE (x), new_reg, GET_MODE (inner),
1802 SUBREG_BYTE (x))) != NULL_RTX)
1804 *loc = subst;
1805 return true;
1809 else if (code == REG && (int) REGNO (x) == old_regno)
1811 machine_mode mode = GET_MODE (x);
1812 machine_mode inner_mode = GET_MODE (new_reg);
1814 if (mode != inner_mode
1815 && ! (CONST_INT_P (new_reg) && SCALAR_INT_MODE_P (mode)))
1817 if (GET_MODE_SIZE (mode) >= GET_MODE_SIZE (inner_mode)
1818 || ! SCALAR_INT_MODE_P (inner_mode))
1819 new_reg = gen_rtx_SUBREG (mode, new_reg, 0);
1820 else
1821 new_reg = gen_lowpart_SUBREG (mode, new_reg);
1823 *loc = new_reg;
1824 return true;
1827 /* Scan all the operand sub-expressions. */
1828 fmt = GET_RTX_FORMAT (code);
1829 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1831 if (fmt[i] == 'e')
1833 if (lra_substitute_pseudo (&XEXP (x, i), old_regno,
1834 new_reg, subreg_p))
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,
1841 new_reg, subreg_p))
1842 result = true;
1845 return result;
1848 /* Call lra_substitute_pseudo within an insn. Try to simplify subreg
1849 of constant if SUBREG_P. This won't update the insn ptr, just the
1850 contents of the insn. */
1851 bool
1852 lra_substitute_pseudo_within_insn (rtx_insn *insn, int old_regno,
1853 rtx new_reg, bool subreg_p)
1855 rtx loc = insn;
1856 return lra_substitute_pseudo (&loc, old_regno, new_reg, subreg_p);
1861 /* This page contains code dealing with scratches (changing them onto
1862 pseudos and restoring them from the pseudos).
1864 We change scratches into pseudos at the beginning of LRA to
1865 simplify dealing with them (conflicts, hard register assignments).
1867 If the pseudo denoting scratch was spilled it means that we do need
1868 a hard register for it. Such pseudos are transformed back to
1869 scratches at the end of LRA. */
1871 /* Description of location of a former scratch operand. */
1872 struct sloc
1874 rtx_insn *insn; /* Insn where the scratch was. */
1875 int nop; /* Number of the operand which was a scratch. */
1878 typedef struct sloc *sloc_t;
1880 /* Locations of the former scratches. */
1881 static vec<sloc_t> scratches;
1883 /* Bitmap of scratch regnos. */
1884 static bitmap_head scratch_bitmap;
1886 /* Bitmap of scratch operands. */
1887 static bitmap_head scratch_operand_bitmap;
1889 /* Return true if pseudo REGNO is made of SCRATCH. */
1890 bool
1891 lra_former_scratch_p (int regno)
1893 return bitmap_bit_p (&scratch_bitmap, regno);
1896 /* Return true if the operand NOP of INSN is a former scratch. */
1897 bool
1898 lra_former_scratch_operand_p (rtx_insn *insn, int nop)
1900 return bitmap_bit_p (&scratch_operand_bitmap,
1901 INSN_UID (insn) * MAX_RECOG_OPERANDS + nop) != 0;
1904 /* Register operand NOP in INSN as a former scratch. It will be
1905 changed to scratch back, if it is necessary, at the LRA end. */
1906 void
1907 lra_register_new_scratch_op (rtx_insn *insn, int nop)
1909 lra_insn_recog_data_t id = lra_get_insn_recog_data (insn);
1910 rtx op = *id->operand_loc[nop];
1911 sloc_t loc = XNEW (struct sloc);
1912 lra_assert (REG_P (op));
1913 loc->insn = insn;
1914 loc->nop = nop;
1915 scratches.safe_push (loc);
1916 bitmap_set_bit (&scratch_bitmap, REGNO (op));
1917 bitmap_set_bit (&scratch_operand_bitmap,
1918 INSN_UID (insn) * MAX_RECOG_OPERANDS + nop);
1919 add_reg_note (insn, REG_UNUSED, op);
1922 /* Change scratches onto pseudos and save their location. */
1923 static void
1924 remove_scratches (void)
1926 int i;
1927 bool insn_changed_p;
1928 basic_block bb;
1929 rtx_insn *insn;
1930 rtx reg;
1931 lra_insn_recog_data_t id;
1932 struct lra_static_insn_data *static_id;
1934 scratches.create (get_max_uid ());
1935 bitmap_initialize (&scratch_bitmap, &reg_obstack);
1936 bitmap_initialize (&scratch_operand_bitmap, &reg_obstack);
1937 FOR_EACH_BB_FN (bb, cfun)
1938 FOR_BB_INSNS (bb, insn)
1939 if (INSN_P (insn))
1941 id = lra_get_insn_recog_data (insn);
1942 static_id = id->insn_static_data;
1943 insn_changed_p = false;
1944 for (i = 0; i < static_id->n_operands; i++)
1945 if (GET_CODE (*id->operand_loc[i]) == SCRATCH
1946 && GET_MODE (*id->operand_loc[i]) != VOIDmode)
1948 insn_changed_p = true;
1949 *id->operand_loc[i] = reg
1950 = lra_create_new_reg (static_id->operand[i].mode,
1951 *id->operand_loc[i], ALL_REGS, NULL);
1952 lra_register_new_scratch_op (insn, i);
1953 if (lra_dump_file != NULL)
1954 fprintf (lra_dump_file,
1955 "Removing SCRATCH in insn #%u (nop %d)\n",
1956 INSN_UID (insn), i);
1958 if (insn_changed_p)
1959 /* Because we might use DF right after caller-saves sub-pass
1960 we need to keep DF info up to date. */
1961 df_insn_rescan (insn);
1965 /* Changes pseudos created by function remove_scratches onto scratches. */
1966 static void
1967 restore_scratches (void)
1969 int regno;
1970 unsigned i;
1971 sloc_t loc;
1972 rtx_insn *last = NULL;
1973 lra_insn_recog_data_t id = NULL;
1975 for (i = 0; scratches.iterate (i, &loc); i++)
1977 if (last != loc->insn)
1979 last = loc->insn;
1980 id = lra_get_insn_recog_data (last);
1982 if (REG_P (*id->operand_loc[loc->nop])
1983 && ((regno = REGNO (*id->operand_loc[loc->nop]))
1984 >= FIRST_PSEUDO_REGISTER)
1985 && lra_get_regno_hard_regno (regno) < 0)
1987 /* It should be only case when scratch register with chosen
1988 constraint 'X' did not get memory or hard register. */
1989 lra_assert (lra_former_scratch_p (regno));
1990 *id->operand_loc[loc->nop]
1991 = gen_rtx_SCRATCH (GET_MODE (*id->operand_loc[loc->nop]));
1992 lra_update_dup (id, loc->nop);
1993 if (lra_dump_file != NULL)
1994 fprintf (lra_dump_file, "Restoring SCRATCH in insn #%u(nop %d)\n",
1995 INSN_UID (loc->insn), loc->nop);
1998 for (i = 0; scratches.iterate (i, &loc); i++)
1999 free (loc);
2000 scratches.release ();
2001 bitmap_clear (&scratch_bitmap);
2002 bitmap_clear (&scratch_operand_bitmap);
2007 #ifdef ENABLE_CHECKING
2009 /* Function checks RTL for correctness. If FINAL_P is true, it is
2010 done at the end of LRA and the check is more rigorous. */
2011 static void
2012 check_rtl (bool final_p)
2014 basic_block bb;
2015 rtx_insn *insn;
2017 lra_assert (! final_p || reload_completed);
2018 FOR_EACH_BB_FN (bb, cfun)
2019 FOR_BB_INSNS (bb, insn)
2020 if (NONDEBUG_INSN_P (insn)
2021 && GET_CODE (PATTERN (insn)) != USE
2022 && GET_CODE (PATTERN (insn)) != CLOBBER
2023 && GET_CODE (PATTERN (insn)) != ASM_INPUT)
2025 if (final_p)
2027 #ifdef ENABLED_CHECKING
2028 extract_constrain_insn (insn);
2029 #endif
2030 continue;
2032 /* LRA code is based on assumption that all addresses can be
2033 correctly decomposed. LRA can generate reloads for
2034 decomposable addresses. The decomposition code checks the
2035 correctness of the addresses. So we don't need to check
2036 the addresses here. Don't call insn_invalid_p here, it can
2037 change the code at this stage. */
2038 if (recog_memoized (insn) < 0 && asm_noperands (PATTERN (insn)) < 0)
2039 fatal_insn_not_found (insn);
2042 #endif /* #ifdef ENABLE_CHECKING */
2044 /* Determine if the current function has an exception receiver block
2045 that reaches the exit block via non-exceptional edges */
2046 static bool
2047 has_nonexceptional_receiver (void)
2049 edge e;
2050 edge_iterator ei;
2051 basic_block *tos, *worklist, bb;
2053 /* If we're not optimizing, then just err on the safe side. */
2054 if (!optimize)
2055 return true;
2057 /* First determine which blocks can reach exit via normal paths. */
2058 tos = worklist = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun) + 1);
2060 FOR_EACH_BB_FN (bb, cfun)
2061 bb->flags &= ~BB_REACHABLE;
2063 /* Place the exit block on our worklist. */
2064 EXIT_BLOCK_PTR_FOR_FN (cfun)->flags |= BB_REACHABLE;
2065 *tos++ = EXIT_BLOCK_PTR_FOR_FN (cfun);
2067 /* Iterate: find everything reachable from what we've already seen. */
2068 while (tos != worklist)
2070 bb = *--tos;
2072 FOR_EACH_EDGE (e, ei, bb->preds)
2073 if (e->flags & EDGE_ABNORMAL)
2075 free (worklist);
2076 return true;
2078 else
2080 basic_block src = e->src;
2082 if (!(src->flags & BB_REACHABLE))
2084 src->flags |= BB_REACHABLE;
2085 *tos++ = src;
2089 free (worklist);
2090 /* No exceptional block reached exit unexceptionally. */
2091 return false;
2095 /* Process recursively X of INSN and add REG_INC notes if necessary. */
2096 static void
2097 add_auto_inc_notes (rtx_insn *insn, rtx x)
2099 enum rtx_code code = GET_CODE (x);
2100 const char *fmt;
2101 int i, j;
2103 if (code == MEM && auto_inc_p (XEXP (x, 0)))
2105 add_reg_note (insn, REG_INC, XEXP (XEXP (x, 0), 0));
2106 return;
2109 /* Scan all X sub-expressions. */
2110 fmt = GET_RTX_FORMAT (code);
2111 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2113 if (fmt[i] == 'e')
2114 add_auto_inc_notes (insn, XEXP (x, i));
2115 else if (fmt[i] == 'E')
2116 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2117 add_auto_inc_notes (insn, XVECEXP (x, i, j));
2122 /* Remove all REG_DEAD and REG_UNUSED notes and regenerate REG_INC.
2123 We change pseudos by hard registers without notification of DF and
2124 that can make the notes obsolete. DF-infrastructure does not deal
2125 with REG_INC notes -- so we should regenerate them here. */
2126 static void
2127 update_inc_notes (void)
2129 rtx *pnote;
2130 basic_block bb;
2131 rtx_insn *insn;
2133 FOR_EACH_BB_FN (bb, cfun)
2134 FOR_BB_INSNS (bb, insn)
2135 if (NONDEBUG_INSN_P (insn))
2137 pnote = &REG_NOTES (insn);
2138 while (*pnote != 0)
2140 if (REG_NOTE_KIND (*pnote) == REG_DEAD
2141 || REG_NOTE_KIND (*pnote) == REG_UNUSED
2142 || REG_NOTE_KIND (*pnote) == REG_INC)
2143 *pnote = XEXP (*pnote, 1);
2144 else
2145 pnote = &XEXP (*pnote, 1);
2148 if (AUTO_INC_DEC)
2149 add_auto_inc_notes (insn, PATTERN (insn));
2153 /* Set to 1 while in lra. */
2154 int lra_in_progress;
2156 /* Start of pseudo regnos before the LRA. */
2157 int lra_new_regno_start;
2159 /* Start of reload pseudo regnos before the new spill pass. */
2160 int lra_constraint_new_regno_start;
2162 /* Avoid spilling pseudos with regno more than the following value if
2163 it is possible. */
2164 int lra_bad_spill_regno_start;
2166 /* Inheritance pseudo regnos before the new spill pass. */
2167 bitmap_head lra_inheritance_pseudos;
2169 /* Split regnos before the new spill pass. */
2170 bitmap_head lra_split_regs;
2172 /* Reload pseudo regnos before the new assignmnet pass which still can
2173 be spilled after the assinment pass as memory is also accepted in
2174 insns for the reload pseudos. */
2175 bitmap_head lra_optional_reload_pseudos;
2177 /* Pseudo regnos used for subreg reloads before the new assignment
2178 pass. Such pseudos still can be spilled after the assinment
2179 pass. */
2180 bitmap_head lra_subreg_reload_pseudos;
2182 /* File used for output of LRA debug information. */
2183 FILE *lra_dump_file;
2185 /* True if we should try spill into registers of different classes
2186 instead of memory. */
2187 bool lra_reg_spill_p;
2189 /* Set up value LRA_REG_SPILL_P. */
2190 static void
2191 setup_reg_spill_flag (void)
2193 int cl, mode;
2195 if (targetm.spill_class != NULL)
2196 for (cl = 0; cl < (int) LIM_REG_CLASSES; cl++)
2197 for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
2198 if (targetm.spill_class ((enum reg_class) cl,
2199 (machine_mode) mode) != NO_REGS)
2201 lra_reg_spill_p = true;
2202 return;
2204 lra_reg_spill_p = false;
2207 /* True if the current function is too big to use regular algorithms
2208 in LRA. In other words, we should use simpler and faster algorithms
2209 in LRA. It also means we should not worry about generation code
2210 for caller saves. The value is set up in IRA. */
2211 bool lra_simple_p;
2213 /* Major LRA entry function. F is a file should be used to dump LRA
2214 debug info. */
2215 void
2216 lra (FILE *f)
2218 int i;
2219 bool live_p, scratch_p, inserted_p;
2221 lra_dump_file = f;
2223 timevar_push (TV_LRA);
2225 /* Make sure that the last insn is a note. Some subsequent passes
2226 need it. */
2227 emit_note (NOTE_INSN_DELETED);
2229 COPY_HARD_REG_SET (lra_no_alloc_regs, ira_no_alloc_regs);
2231 init_reg_info ();
2232 expand_reg_info ();
2234 init_insn_recog_data ();
2236 #ifdef ENABLE_CHECKING
2237 /* Some quick check on RTL generated by previous passes. */
2238 check_rtl (false);
2239 #endif
2241 lra_in_progress = 1;
2243 lra_live_range_iter = lra_coalesce_iter = lra_constraint_iter = 0;
2244 lra_assignment_iter = lra_assignment_iter_after_spill = 0;
2245 lra_inheritance_iter = lra_undo_inheritance_iter = 0;
2246 lra_rematerialization_iter = 0;
2248 setup_reg_spill_flag ();
2250 /* Function remove_scratches can creates new pseudos for clobbers --
2251 so set up lra_constraint_new_regno_start before its call to
2252 permit changing reg classes for pseudos created by this
2253 simplification. */
2254 lra_constraint_new_regno_start = lra_new_regno_start = max_reg_num ();
2255 lra_bad_spill_regno_start = INT_MAX;
2256 remove_scratches ();
2257 scratch_p = lra_constraint_new_regno_start != max_reg_num ();
2259 /* A function that has a non-local label that can reach the exit
2260 block via non-exceptional paths must save all call-saved
2261 registers. */
2262 if (cfun->has_nonlocal_label && has_nonexceptional_receiver ())
2263 crtl->saves_all_registers = 1;
2265 if (crtl->saves_all_registers)
2266 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2267 if (! call_used_regs[i] && ! fixed_regs[i] && ! LOCAL_REGNO (i))
2268 df_set_regs_ever_live (i, true);
2270 /* We don't DF from now and avoid its using because it is to
2271 expensive when a lot of RTL changes are made. */
2272 df_set_flags (DF_NO_INSN_RESCAN);
2273 lra_constraint_insn_stack.create (get_max_uid ());
2274 lra_constraint_insn_stack_bitmap = sbitmap_alloc (get_max_uid ());
2275 bitmap_clear (lra_constraint_insn_stack_bitmap);
2276 lra_live_ranges_init ();
2277 lra_constraints_init ();
2278 lra_curr_reload_num = 0;
2279 push_insns (get_last_insn (), NULL);
2280 /* It is needed for the 1st coalescing. */
2281 bitmap_initialize (&lra_inheritance_pseudos, &reg_obstack);
2282 bitmap_initialize (&lra_split_regs, &reg_obstack);
2283 bitmap_initialize (&lra_optional_reload_pseudos, &reg_obstack);
2284 bitmap_initialize (&lra_subreg_reload_pseudos, &reg_obstack);
2285 live_p = false;
2286 if (get_frame_size () != 0 && crtl->stack_alignment_needed)
2287 /* If we have a stack frame, we must align it now. The stack size
2288 may be a part of the offset computation for register
2289 elimination. */
2290 assign_stack_local (BLKmode, 0, crtl->stack_alignment_needed);
2291 lra_init_equiv ();
2292 for (;;)
2294 for (;;)
2296 /* We should try to assign hard registers to scratches even
2297 if there were no RTL transformations in
2298 lra_constraints. */
2299 if (! lra_constraints (lra_constraint_iter == 0)
2300 && (lra_constraint_iter > 1
2301 || (! scratch_p && ! caller_save_needed)))
2302 break;
2303 /* Constraint transformations may result in that eliminable
2304 hard regs become uneliminable and pseudos which use them
2305 should be spilled. It is better to do it before pseudo
2306 assignments.
2308 For example, rs6000 can make
2309 RS6000_PIC_OFFSET_TABLE_REGNUM uneliminable if we started
2310 to use a constant pool. */
2311 lra_eliminate (false, false);
2312 /* Do inheritance only for regular algorithms. */
2313 if (! lra_simple_p)
2315 if (flag_ipa_ra)
2317 if (live_p)
2318 lra_clear_live_ranges ();
2319 /* As a side-effect of lra_create_live_ranges, we calculate
2320 actual_call_used_reg_set, which is needed during
2321 lra_inheritance. */
2322 lra_create_live_ranges (true, true);
2323 live_p = true;
2325 lra_inheritance ();
2327 if (live_p)
2328 lra_clear_live_ranges ();
2329 /* We need live ranges for lra_assign -- so build them. But
2330 don't remove dead insns or change global live info as we
2331 can undo inheritance transformations after inheritance
2332 pseudo assigning. */
2333 lra_create_live_ranges (true, false);
2334 live_p = true;
2335 /* If we don't spill non-reload and non-inheritance pseudos,
2336 there is no sense to run memory-memory move coalescing.
2337 If inheritance pseudos were spilled, the memory-memory
2338 moves involving them will be removed by pass undoing
2339 inheritance. */
2340 if (lra_simple_p)
2341 lra_assign ();
2342 else
2344 bool spill_p = !lra_assign ();
2346 if (lra_undo_inheritance ())
2347 live_p = false;
2348 if (spill_p)
2350 if (! live_p)
2352 lra_create_live_ranges (true, true);
2353 live_p = true;
2355 if (lra_coalesce ())
2356 live_p = false;
2358 if (! live_p)
2359 lra_clear_live_ranges ();
2362 /* Don't clear optional reloads bitmap until all constraints are
2363 satisfied as we need to differ them from regular reloads. */
2364 bitmap_clear (&lra_optional_reload_pseudos);
2365 bitmap_clear (&lra_subreg_reload_pseudos);
2366 bitmap_clear (&lra_inheritance_pseudos);
2367 bitmap_clear (&lra_split_regs);
2368 if (! live_p)
2370 /* We need full live info for spilling pseudos into
2371 registers instead of memory. */
2372 lra_create_live_ranges (lra_reg_spill_p, true);
2373 live_p = true;
2375 /* We should check necessity for spilling here as the above live
2376 range pass can remove spilled pseudos. */
2377 if (! lra_need_for_spills_p ())
2378 break;
2379 /* Now we know what pseudos should be spilled. Try to
2380 rematerialize them first. */
2381 if (lra_remat ())
2383 /* We need full live info -- see the comment above. */
2384 lra_create_live_ranges (lra_reg_spill_p, true);
2385 live_p = true;
2386 if (! lra_need_for_spills_p ())
2387 break;
2389 lra_spill ();
2390 /* Assignment of stack slots changes elimination offsets for
2391 some eliminations. So update the offsets here. */
2392 lra_eliminate (false, false);
2393 lra_constraint_new_regno_start = max_reg_num ();
2394 if (lra_bad_spill_regno_start == INT_MAX
2395 && lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES
2396 && lra_rematerialization_iter > LRA_MAX_REMATERIALIZATION_PASSES)
2397 /* After switching off inheritance and rematerialization
2398 passes, avoid spilling reload pseudos will be created to
2399 prevent LRA cycling in some complicated cases. */
2400 lra_bad_spill_regno_start = lra_constraint_new_regno_start;
2401 lra_assignment_iter_after_spill = 0;
2403 restore_scratches ();
2404 lra_eliminate (true, false);
2405 lra_final_code_change ();
2406 lra_in_progress = 0;
2407 if (live_p)
2408 lra_clear_live_ranges ();
2409 lra_live_ranges_finish ();
2410 lra_constraints_finish ();
2411 finish_reg_info ();
2412 sbitmap_free (lra_constraint_insn_stack_bitmap);
2413 lra_constraint_insn_stack.release ();
2414 finish_insn_recog_data ();
2415 regstat_free_n_sets_and_refs ();
2416 regstat_free_ri ();
2417 reload_completed = 1;
2418 update_inc_notes ();
2420 inserted_p = fixup_abnormal_edges ();
2422 /* We've possibly turned single trapping insn into multiple ones. */
2423 if (cfun->can_throw_non_call_exceptions)
2425 sbitmap blocks;
2426 blocks = sbitmap_alloc (last_basic_block_for_fn (cfun));
2427 bitmap_ones (blocks);
2428 find_many_sub_basic_blocks (blocks);
2429 sbitmap_free (blocks);
2432 if (inserted_p)
2433 commit_edge_insertions ();
2435 /* Replacing pseudos with their memory equivalents might have
2436 created shared rtx. Subsequent passes would get confused
2437 by this, so unshare everything here. */
2438 unshare_all_rtl_again (get_insns ());
2440 #ifdef ENABLE_CHECKING
2441 check_rtl (true);
2442 #endif
2444 timevar_pop (TV_LRA);
2447 /* Called once per compiler to initialize LRA data once. */
2448 void
2449 lra_init_once (void)
2451 init_insn_code_data_once ();
2454 /* Called once per compiler to finish LRA data which are initialize
2455 once. */
2456 void
2457 lra_finish_once (void)
2459 finish_insn_code_data_once ();