* g++.dg/debug/dwarf2/pr44641.C: Revert line number change. Remove
[official-gcc.git] / gcc / lra.c
blobbdbfe51355349d3b0e566a5b63e6e10ad5ac9d83
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"
141 /* Dump bitmap SET with TITLE and BB INDEX. */
142 void
143 lra_dump_bitmap_with_title (const char *title, bitmap set, int index)
145 unsigned int i;
146 int count;
147 bitmap_iterator bi;
148 static const int max_nums_on_line = 10;
150 if (bitmap_empty_p (set))
151 return;
152 fprintf (lra_dump_file, " %s %d:", title, index);
153 fprintf (lra_dump_file, "\n");
154 count = max_nums_on_line + 1;
155 EXECUTE_IF_SET_IN_BITMAP (set, 0, i, bi)
157 if (count > max_nums_on_line)
159 fprintf (lra_dump_file, "\n ");
160 count = 0;
162 fprintf (lra_dump_file, " %4u", i);
163 count++;
165 fprintf (lra_dump_file, "\n");
168 /* Hard registers currently not available for allocation. It can
169 changed after some hard registers become not eliminable. */
170 HARD_REG_SET lra_no_alloc_regs;
172 static int get_new_reg_value (void);
173 static void expand_reg_info (void);
174 static void invalidate_insn_recog_data (int);
175 static int get_insn_freq (rtx_insn *);
176 static void invalidate_insn_data_regno_info (lra_insn_recog_data_t,
177 rtx_insn *, int);
179 /* Expand all regno related info needed for LRA. */
180 static void
181 expand_reg_data (int old)
183 resize_reg_info ();
184 expand_reg_info ();
185 ira_expand_reg_equiv ();
186 for (int i = (int) max_reg_num () - 1; i >= old; i--)
187 lra_change_class (i, ALL_REGS, " Set", true);
190 /* Create and return a new reg of ORIGINAL mode. If ORIGINAL is NULL
191 or of VOIDmode, use MD_MODE for the new reg. Initialize its
192 register class to RCLASS. Print message about assigning class
193 RCLASS containing new register name TITLE unless it is NULL. Use
194 attributes of ORIGINAL if it is a register. The created register
195 will have unique held value. */
197 lra_create_new_reg_with_unique_value (machine_mode md_mode, rtx original,
198 enum reg_class rclass, const char *title)
200 machine_mode mode;
201 rtx new_reg;
203 if (original == NULL_RTX || (mode = GET_MODE (original)) == VOIDmode)
204 mode = md_mode;
205 lra_assert (mode != VOIDmode);
206 new_reg = gen_reg_rtx (mode);
207 if (original == NULL_RTX || ! REG_P (original))
209 if (lra_dump_file != NULL)
210 fprintf (lra_dump_file, " Creating newreg=%i", REGNO (new_reg));
212 else
214 if (ORIGINAL_REGNO (original) >= FIRST_PSEUDO_REGISTER)
215 ORIGINAL_REGNO (new_reg) = ORIGINAL_REGNO (original);
216 REG_USERVAR_P (new_reg) = REG_USERVAR_P (original);
217 REG_POINTER (new_reg) = REG_POINTER (original);
218 REG_ATTRS (new_reg) = REG_ATTRS (original);
219 if (lra_dump_file != NULL)
220 fprintf (lra_dump_file, " Creating newreg=%i from oldreg=%i",
221 REGNO (new_reg), REGNO (original));
223 if (lra_dump_file != NULL)
225 if (title != NULL)
226 fprintf (lra_dump_file, ", assigning class %s to%s%s r%d",
227 reg_class_names[rclass], *title == '\0' ? "" : " ",
228 title, REGNO (new_reg));
229 fprintf (lra_dump_file, "\n");
231 expand_reg_data (max_reg_num ());
232 setup_reg_classes (REGNO (new_reg), rclass, NO_REGS, rclass);
233 return new_reg;
236 /* Analogous to the previous function but also inherits value of
237 ORIGINAL. */
239 lra_create_new_reg (machine_mode md_mode, rtx original,
240 enum reg_class rclass, const char *title)
242 rtx new_reg;
244 new_reg
245 = lra_create_new_reg_with_unique_value (md_mode, original, rclass, title);
246 if (original != NULL_RTX && REG_P (original))
247 lra_assign_reg_val (REGNO (original), REGNO (new_reg));
248 return new_reg;
251 /* Set up for REGNO unique hold value. */
252 void
253 lra_set_regno_unique_value (int regno)
255 lra_reg_info[regno].val = get_new_reg_value ();
258 /* Invalidate INSN related info used by LRA. The info should never be
259 used after that. */
260 void
261 lra_invalidate_insn_data (rtx_insn *insn)
263 lra_invalidate_insn_regno_info (insn);
264 invalidate_insn_recog_data (INSN_UID (insn));
267 /* Mark INSN deleted and invalidate the insn related info used by
268 LRA. */
269 void
270 lra_set_insn_deleted (rtx_insn *insn)
272 lra_invalidate_insn_data (insn);
273 SET_INSN_DELETED (insn);
276 /* Delete an unneeded INSN and any previous insns who sole purpose is
277 loading data that is dead in INSN. */
278 void
279 lra_delete_dead_insn (rtx_insn *insn)
281 rtx_insn *prev = prev_real_insn (insn);
282 rtx prev_dest;
284 /* If the previous insn sets a register that dies in our insn,
285 delete it too. */
286 if (prev && GET_CODE (PATTERN (prev)) == SET
287 && (prev_dest = SET_DEST (PATTERN (prev)), REG_P (prev_dest))
288 && reg_mentioned_p (prev_dest, PATTERN (insn))
289 && find_regno_note (insn, REG_DEAD, REGNO (prev_dest))
290 && ! side_effects_p (SET_SRC (PATTERN (prev))))
291 lra_delete_dead_insn (prev);
293 lra_set_insn_deleted (insn);
296 /* Emit insn x = y + z. Return NULL if we failed to do it.
297 Otherwise, return the insn. We don't use gen_add3_insn as it might
298 clobber CC. */
299 static rtx_insn *
300 emit_add3_insn (rtx x, rtx y, rtx z)
302 rtx_insn *last;
304 last = get_last_insn ();
306 if (have_addptr3_insn (x, y, z))
308 rtx_insn *insn = gen_addptr3_insn (x, y, z);
310 /* If the target provides an "addptr" pattern it hopefully does
311 for a reason. So falling back to the normal add would be
312 a bug. */
313 lra_assert (insn != NULL_RTX);
314 emit_insn (insn);
315 return insn;
318 rtx_insn *insn = emit_insn (gen_rtx_SET (x, gen_rtx_PLUS (GET_MODE (y),
319 y, z)));
320 if (recog_memoized (insn) < 0)
322 delete_insns_since (last);
323 insn = NULL;
325 return insn;
328 /* Emit insn x = x + y. Return the insn. We use gen_add2_insn as the
329 last resort. */
330 static rtx_insn *
331 emit_add2_insn (rtx x, rtx y)
333 rtx_insn *insn = emit_add3_insn (x, x, y);
334 if (insn == NULL_RTX)
336 insn = gen_add2_insn (x, y);
337 if (insn != NULL_RTX)
338 emit_insn (insn);
340 return insn;
343 /* Target checks operands through operand predicates to recognize an
344 insn. We should have a special precaution to generate add insns
345 which are frequent results of elimination.
347 Emit insns for x = y + z. X can be used to store intermediate
348 values and should be not in Y and Z when we use X to store an
349 intermediate value. Y + Z should form [base] [+ index[ * scale]] [
350 + disp] where base and index are registers, disp and scale are
351 constants. Y should contain base if it is present, Z should
352 contain disp if any. index[*scale] can be part of Y or Z. */
353 void
354 lra_emit_add (rtx x, rtx y, rtx z)
356 int old;
357 rtx_insn *last;
358 rtx a1, a2, base, index, disp, scale, index_scale;
359 bool ok_p;
361 rtx_insn *add3_insn = emit_add3_insn (x, y, z);
362 old = max_reg_num ();
363 if (add3_insn != NULL)
365 else
367 disp = a2 = NULL_RTX;
368 if (GET_CODE (y) == PLUS)
370 a1 = XEXP (y, 0);
371 a2 = XEXP (y, 1);
372 disp = z;
374 else
376 a1 = y;
377 if (CONSTANT_P (z))
378 disp = z;
379 else
380 a2 = z;
382 index_scale = scale = NULL_RTX;
383 if (GET_CODE (a1) == MULT)
385 index_scale = a1;
386 index = XEXP (a1, 0);
387 scale = XEXP (a1, 1);
388 base = a2;
390 else if (a2 != NULL_RTX && GET_CODE (a2) == MULT)
392 index_scale = a2;
393 index = XEXP (a2, 0);
394 scale = XEXP (a2, 1);
395 base = a1;
397 else
399 base = a1;
400 index = a2;
402 if (! (REG_P (base) || GET_CODE (base) == SUBREG)
403 || (index != NULL_RTX
404 && ! (REG_P (index) || GET_CODE (index) == SUBREG))
405 || (disp != NULL_RTX && ! CONSTANT_P (disp))
406 || (scale != NULL_RTX && ! CONSTANT_P (scale)))
408 /* Probably we have no 3 op add. Last chance is to use 2-op
409 add insn. To succeed, don't move Z to X as an address
410 segment always comes in Y. Otherwise, we might fail when
411 adding the address segment to register. */
412 lra_assert (x != y && x != z);
413 emit_move_insn (x, y);
414 rtx_insn *insn = emit_add2_insn (x, z);
415 lra_assert (insn != NULL_RTX);
417 else
419 if (index_scale == NULL_RTX)
420 index_scale = index;
421 if (disp == NULL_RTX)
423 /* Generate x = index_scale; x = x + base. */
424 lra_assert (index_scale != NULL_RTX && base != NULL_RTX);
425 emit_move_insn (x, index_scale);
426 rtx_insn *insn = emit_add2_insn (x, base);
427 lra_assert (insn != NULL_RTX);
429 else if (scale == NULL_RTX)
431 /* Try x = base + disp. */
432 lra_assert (base != NULL_RTX);
433 last = get_last_insn ();
434 rtx_insn *move_insn =
435 emit_move_insn (x, gen_rtx_PLUS (GET_MODE (base), base, disp));
436 if (recog_memoized (move_insn) < 0)
438 delete_insns_since (last);
439 /* Generate x = disp; x = x + base. */
440 emit_move_insn (x, disp);
441 rtx_insn *add2_insn = emit_add2_insn (x, base);
442 lra_assert (add2_insn != NULL_RTX);
444 /* Generate x = x + index. */
445 if (index != NULL_RTX)
447 rtx_insn *insn = emit_add2_insn (x, index);
448 lra_assert (insn != NULL_RTX);
451 else
453 /* Try x = index_scale; x = x + disp; x = x + base. */
454 last = get_last_insn ();
455 rtx_insn *move_insn = emit_move_insn (x, index_scale);
456 ok_p = false;
457 if (recog_memoized (move_insn) >= 0)
459 rtx_insn *insn = emit_add2_insn (x, disp);
460 if (insn != NULL_RTX)
462 insn = emit_add2_insn (x, base);
463 if (insn != NULL_RTX)
464 ok_p = true;
467 if (! ok_p)
469 delete_insns_since (last);
470 /* Generate x = disp; x = x + base; x = x + index_scale. */
471 emit_move_insn (x, disp);
472 rtx_insn *insn = emit_add2_insn (x, base);
473 lra_assert (insn != NULL_RTX);
474 insn = emit_add2_insn (x, index_scale);
475 lra_assert (insn != NULL_RTX);
480 /* Functions emit_... can create pseudos -- so expand the pseudo
481 data. */
482 if (old != max_reg_num ())
483 expand_reg_data (old);
486 /* The number of emitted reload insns so far. */
487 int lra_curr_reload_num;
489 /* Emit x := y, processing special case when y = u + v or y = u + v *
490 scale + w through emit_add (Y can be an address which is base +
491 index reg * scale + displacement in general case). X may be used
492 as intermediate result therefore it should be not in Y. */
493 void
494 lra_emit_move (rtx x, rtx y)
496 int old;
498 if (GET_CODE (y) != PLUS)
500 if (rtx_equal_p (x, y))
501 return;
502 old = max_reg_num ();
503 emit_move_insn (x, y);
504 if (REG_P (x))
505 lra_reg_info[ORIGINAL_REGNO (x)].last_reload = ++lra_curr_reload_num;
506 /* Function emit_move can create pseudos -- so expand the pseudo
507 data. */
508 if (old != max_reg_num ())
509 expand_reg_data (old);
510 return;
512 lra_emit_add (x, XEXP (y, 0), XEXP (y, 1));
515 /* Update insn operands which are duplication of operands whose
516 numbers are in array of NOPS (with end marker -1). The insn is
517 represented by its LRA internal representation ID. */
518 void
519 lra_update_dups (lra_insn_recog_data_t id, signed char *nops)
521 int i, j, nop;
522 struct lra_static_insn_data *static_id = id->insn_static_data;
524 for (i = 0; i < static_id->n_dups; i++)
525 for (j = 0; (nop = nops[j]) >= 0; j++)
526 if (static_id->dup_num[i] == nop)
527 *id->dup_loc[i] = *id->operand_loc[nop];
532 /* This page contains code dealing with info about registers in the
533 insns. */
535 /* Pools for insn reg info. */
536 object_allocator<lra_insn_reg> lra_insn_reg_pool ("insn regs");
538 /* Create LRA insn related info about a reference to REGNO in INSN with
539 TYPE (in/out/inout), biggest reference mode MODE, flag that it is
540 reference through subreg (SUBREG_P), flag that is early clobbered
541 in the insn (EARLY_CLOBBER), and reference to the next insn reg
542 info (NEXT). */
543 static struct lra_insn_reg *
544 new_insn_reg (rtx_insn *insn, int regno, enum op_type type,
545 machine_mode mode,
546 bool subreg_p, bool early_clobber, struct lra_insn_reg *next)
548 lra_insn_reg *ir = lra_insn_reg_pool.allocate ();
549 ir->type = type;
550 ir->biggest_mode = mode;
551 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (lra_reg_info[regno].biggest_mode)
552 && NONDEBUG_INSN_P (insn))
553 lra_reg_info[regno].biggest_mode = mode;
554 ir->subreg_p = subreg_p;
555 ir->early_clobber = early_clobber;
556 ir->regno = regno;
557 ir->next = next;
558 return ir;
561 /* Free insn reg info list IR. */
562 static void
563 free_insn_regs (struct lra_insn_reg *ir)
565 struct lra_insn_reg *next_ir;
567 for (; ir != NULL; ir = next_ir)
569 next_ir = ir->next;
570 lra_insn_reg_pool.remove (ir);
574 /* Finish pool for insn reg info. */
575 static void
576 finish_insn_regs (void)
578 lra_insn_reg_pool.release ();
583 /* This page contains code dealing LRA insn info (or in other words
584 LRA internal insn representation). */
586 /* Map INSN_CODE -> the static insn data. This info is valid during
587 all translation unit. */
588 struct lra_static_insn_data *insn_code_data[NUM_INSN_CODES];
590 /* Debug insns are represented as a special insn with one input
591 operand which is RTL expression in var_location. */
593 /* The following data are used as static insn operand data for all
594 debug insns. If structure lra_operand_data is changed, the
595 initializer should be changed too. */
596 static struct lra_operand_data debug_operand_data =
598 NULL, /* alternative */
599 VOIDmode, /* We are not interesting in the operand mode. */
600 OP_IN,
601 0, 0, 0, 0
604 /* The following data are used as static insn data for all debug
605 insns. If structure lra_static_insn_data is changed, the
606 initializer should be changed too. */
607 static struct lra_static_insn_data debug_insn_static_data =
609 &debug_operand_data,
610 0, /* Duplication operands #. */
611 -1, /* Commutative operand #. */
612 1, /* Operands #. There is only one operand which is debug RTL
613 expression. */
614 0, /* Duplications #. */
615 0, /* Alternatives #. We are not interesting in alternatives
616 because we does not proceed debug_insns for reloads. */
617 NULL, /* Hard registers referenced in machine description. */
618 NULL /* Descriptions of operands in alternatives. */
621 /* Called once per compiler work to initialize some LRA data related
622 to insns. */
623 static void
624 init_insn_code_data_once (void)
626 memset (insn_code_data, 0, sizeof (insn_code_data));
629 /* Called once per compiler work to finalize some LRA data related to
630 insns. */
631 static void
632 finish_insn_code_data_once (void)
634 for (unsigned int i = 0; i < NUM_INSN_CODES; i++)
636 if (insn_code_data[i] != NULL)
637 free (insn_code_data[i]);
641 /* Return static insn data, allocate and setup if necessary. Although
642 dup_num is static data (it depends only on icode), to set it up we
643 need to extract insn first. So recog_data should be valid for
644 normal insn (ICODE >= 0) before the call. */
645 static struct lra_static_insn_data *
646 get_static_insn_data (int icode, int nop, int ndup, int nalt)
648 struct lra_static_insn_data *data;
649 size_t n_bytes;
651 lra_assert (icode < (int) NUM_INSN_CODES);
652 if (icode >= 0 && (data = insn_code_data[icode]) != NULL)
653 return data;
654 lra_assert (nop >= 0 && ndup >= 0 && nalt >= 0);
655 n_bytes = sizeof (struct lra_static_insn_data)
656 + sizeof (struct lra_operand_data) * nop
657 + sizeof (int) * ndup;
658 data = XNEWVAR (struct lra_static_insn_data, n_bytes);
659 data->operand_alternative = NULL;
660 data->n_operands = nop;
661 data->n_dups = ndup;
662 data->n_alternatives = nalt;
663 data->operand = ((struct lra_operand_data *)
664 ((char *) data + sizeof (struct lra_static_insn_data)));
665 data->dup_num = ((int *) ((char *) data->operand
666 + sizeof (struct lra_operand_data) * nop));
667 if (icode >= 0)
669 int i;
671 insn_code_data[icode] = data;
672 for (i = 0; i < nop; i++)
674 data->operand[i].constraint
675 = insn_data[icode].operand[i].constraint;
676 data->operand[i].mode = insn_data[icode].operand[i].mode;
677 data->operand[i].strict_low = insn_data[icode].operand[i].strict_low;
678 data->operand[i].is_operator
679 = insn_data[icode].operand[i].is_operator;
680 data->operand[i].type
681 = (data->operand[i].constraint[0] == '=' ? OP_OUT
682 : data->operand[i].constraint[0] == '+' ? OP_INOUT
683 : OP_IN);
684 data->operand[i].is_address = false;
686 for (i = 0; i < ndup; i++)
687 data->dup_num[i] = recog_data.dup_num[i];
689 return data;
692 /* The current length of the following array. */
693 int lra_insn_recog_data_len;
695 /* Map INSN_UID -> the insn recog data (NULL if unknown). */
696 lra_insn_recog_data_t *lra_insn_recog_data;
698 /* Initialize LRA data about insns. */
699 static void
700 init_insn_recog_data (void)
702 lra_insn_recog_data_len = 0;
703 lra_insn_recog_data = NULL;
706 /* Expand, if necessary, LRA data about insns. */
707 static void
708 check_and_expand_insn_recog_data (int index)
710 int i, old;
712 if (lra_insn_recog_data_len > index)
713 return;
714 old = lra_insn_recog_data_len;
715 lra_insn_recog_data_len = index * 3 / 2 + 1;
716 lra_insn_recog_data = XRESIZEVEC (lra_insn_recog_data_t,
717 lra_insn_recog_data,
718 lra_insn_recog_data_len);
719 for (i = old; i < lra_insn_recog_data_len; i++)
720 lra_insn_recog_data[i] = NULL;
723 /* Finish LRA DATA about insn. */
724 static void
725 free_insn_recog_data (lra_insn_recog_data_t data)
727 if (data->operand_loc != NULL)
728 free (data->operand_loc);
729 if (data->dup_loc != NULL)
730 free (data->dup_loc);
731 if (data->arg_hard_regs != NULL)
732 free (data->arg_hard_regs);
733 if (data->icode < 0 && NONDEBUG_INSN_P (data->insn))
735 if (data->insn_static_data->operand_alternative != NULL)
736 free (const_cast <operand_alternative *>
737 (data->insn_static_data->operand_alternative));
738 free_insn_regs (data->insn_static_data->hard_regs);
739 free (data->insn_static_data);
741 free_insn_regs (data->regs);
742 data->regs = NULL;
743 free (data);
746 /* Pools for copies. */
747 static object_allocator<lra_copy> lra_copy_pool ("lra copies");
749 /* Finish LRA data about all insns. */
750 static void
751 finish_insn_recog_data (void)
753 int i;
754 lra_insn_recog_data_t data;
756 for (i = 0; i < lra_insn_recog_data_len; i++)
757 if ((data = lra_insn_recog_data[i]) != NULL)
758 free_insn_recog_data (data);
759 finish_insn_regs ();
760 lra_copy_pool.release ();
761 lra_insn_reg_pool.release ();
762 free (lra_insn_recog_data);
765 /* Setup info about operands in alternatives of LRA DATA of insn. */
766 static void
767 setup_operand_alternative (lra_insn_recog_data_t data,
768 const operand_alternative *op_alt)
770 int i, j, nop, nalt;
771 int icode = data->icode;
772 struct lra_static_insn_data *static_data = data->insn_static_data;
774 static_data->commutative = -1;
775 nop = static_data->n_operands;
776 nalt = static_data->n_alternatives;
777 static_data->operand_alternative = op_alt;
778 for (i = 0; i < nop; i++)
780 static_data->operand[i].early_clobber = false;
781 static_data->operand[i].is_address = false;
782 if (static_data->operand[i].constraint[0] == '%')
784 /* We currently only support one commutative pair of operands. */
785 if (static_data->commutative < 0)
786 static_data->commutative = i;
787 else
788 lra_assert (icode < 0); /* Asm */
789 /* The last operand should not be marked commutative. */
790 lra_assert (i != nop - 1);
793 for (j = 0; j < nalt; j++)
794 for (i = 0; i < nop; i++, op_alt++)
796 static_data->operand[i].early_clobber |= op_alt->earlyclobber;
797 static_data->operand[i].is_address |= op_alt->is_address;
801 /* Recursively process X and collect info about registers, which are
802 not the insn operands, in X with TYPE (in/out/inout) and flag that
803 it is early clobbered in the insn (EARLY_CLOBBER) and add the info
804 to LIST. X is a part of insn given by DATA. Return the result
805 list. */
806 static struct lra_insn_reg *
807 collect_non_operand_hard_regs (rtx *x, lra_insn_recog_data_t data,
808 struct lra_insn_reg *list,
809 enum op_type type, bool early_clobber)
811 int i, j, regno, last;
812 bool subreg_p;
813 machine_mode mode;
814 struct lra_insn_reg *curr;
815 rtx op = *x;
816 enum rtx_code code = GET_CODE (op);
817 const char *fmt = GET_RTX_FORMAT (code);
819 for (i = 0; i < data->insn_static_data->n_operands; i++)
820 if (x == data->operand_loc[i])
821 /* It is an operand loc. Stop here. */
822 return list;
823 for (i = 0; i < data->insn_static_data->n_dups; i++)
824 if (x == data->dup_loc[i])
825 /* It is a dup loc. Stop here. */
826 return list;
827 mode = GET_MODE (op);
828 subreg_p = false;
829 if (code == SUBREG)
831 op = SUBREG_REG (op);
832 code = GET_CODE (op);
833 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (op)))
835 mode = GET_MODE (op);
836 if (GET_MODE_SIZE (mode) > REGMODE_NATURAL_SIZE (mode))
837 subreg_p = true;
840 if (REG_P (op))
842 if ((regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER)
843 return list;
844 /* Process all regs even unallocatable ones as we need info
845 about all regs for rematerialization pass. */
846 for (last = regno + hard_regno_nregs[regno][mode];
847 regno < last;
848 regno++)
850 for (curr = list; curr != NULL; curr = curr->next)
851 if (curr->regno == regno && curr->subreg_p == subreg_p
852 && curr->biggest_mode == mode)
854 if (curr->type != type)
855 curr->type = OP_INOUT;
856 if (curr->early_clobber != early_clobber)
857 curr->early_clobber = true;
858 break;
860 if (curr == NULL)
862 /* This is a new hard regno or the info can not be
863 integrated into the found structure. */
864 #ifdef STACK_REGS
865 early_clobber
866 = (early_clobber
867 /* This clobber is to inform popping floating
868 point stack only. */
869 && ! (FIRST_STACK_REG <= regno
870 && regno <= LAST_STACK_REG));
871 #endif
872 list = new_insn_reg (data->insn, regno, type, mode, subreg_p,
873 early_clobber, list);
876 return list;
878 switch (code)
880 case SET:
881 list = collect_non_operand_hard_regs (&SET_DEST (op), data,
882 list, OP_OUT, false);
883 list = collect_non_operand_hard_regs (&SET_SRC (op), data,
884 list, OP_IN, false);
885 break;
886 case CLOBBER:
887 /* We treat clobber of non-operand hard registers as early
888 clobber (the behavior is expected from asm). */
889 list = collect_non_operand_hard_regs (&XEXP (op, 0), data,
890 list, OP_OUT, true);
891 break;
892 case PRE_INC: case PRE_DEC: case POST_INC: case POST_DEC:
893 list = collect_non_operand_hard_regs (&XEXP (op, 0), data,
894 list, OP_INOUT, false);
895 break;
896 case PRE_MODIFY: case POST_MODIFY:
897 list = collect_non_operand_hard_regs (&XEXP (op, 0), data,
898 list, OP_INOUT, false);
899 list = collect_non_operand_hard_regs (&XEXP (op, 1), data,
900 list, OP_IN, false);
901 break;
902 default:
903 fmt = GET_RTX_FORMAT (code);
904 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
906 if (fmt[i] == 'e')
907 list = collect_non_operand_hard_regs (&XEXP (op, i), data,
908 list, OP_IN, false);
909 else if (fmt[i] == 'E')
910 for (j = XVECLEN (op, i) - 1; j >= 0; j--)
911 list = collect_non_operand_hard_regs (&XVECEXP (op, i, j), data,
912 list, OP_IN, false);
915 return list;
918 /* Set up and return info about INSN. Set up the info if it is not set up
919 yet. */
920 lra_insn_recog_data_t
921 lra_set_insn_recog_data (rtx_insn *insn)
923 lra_insn_recog_data_t data;
924 int i, n, icode;
925 rtx **locs;
926 unsigned int uid = INSN_UID (insn);
927 struct lra_static_insn_data *insn_static_data;
929 check_and_expand_insn_recog_data (uid);
930 if (DEBUG_INSN_P (insn))
931 icode = -1;
932 else
934 icode = INSN_CODE (insn);
935 if (icode < 0)
936 /* It might be a new simple insn which is not recognized yet. */
937 INSN_CODE (insn) = icode = recog_memoized (insn);
939 data = XNEW (struct lra_insn_recog_data);
940 lra_insn_recog_data[uid] = data;
941 data->insn = insn;
942 data->used_insn_alternative = -1;
943 data->icode = icode;
944 data->regs = NULL;
945 if (DEBUG_INSN_P (insn))
947 data->insn_static_data = &debug_insn_static_data;
948 data->dup_loc = NULL;
949 data->arg_hard_regs = NULL;
950 data->preferred_alternatives = ALL_ALTERNATIVES;
951 data->operand_loc = XNEWVEC (rtx *, 1);
952 data->operand_loc[0] = &INSN_VAR_LOCATION_LOC (insn);
953 return data;
955 if (icode < 0)
957 int nop, nalt;
958 machine_mode operand_mode[MAX_RECOG_OPERANDS];
959 const char *constraints[MAX_RECOG_OPERANDS];
961 nop = asm_noperands (PATTERN (insn));
962 data->operand_loc = data->dup_loc = NULL;
963 nalt = 1;
964 if (nop < 0)
966 /* It is a special insn like USE or CLOBBER. We should
967 recognize any regular insn otherwise LRA can do nothing
968 with this insn. */
969 gcc_assert (GET_CODE (PATTERN (insn)) == USE
970 || GET_CODE (PATTERN (insn)) == CLOBBER
971 || GET_CODE (PATTERN (insn)) == ASM_INPUT);
972 data->insn_static_data = insn_static_data
973 = get_static_insn_data (-1, 0, 0, nalt);
975 else
977 /* expand_asm_operands makes sure there aren't too many
978 operands. */
979 lra_assert (nop <= MAX_RECOG_OPERANDS);
980 if (nop != 0)
981 data->operand_loc = XNEWVEC (rtx *, nop);
982 /* Now get the operand values and constraints out of the
983 insn. */
984 decode_asm_operands (PATTERN (insn), NULL,
985 data->operand_loc,
986 constraints, operand_mode, NULL);
987 if (nop > 0)
989 const char *p = recog_data.constraints[0];
991 for (p = constraints[0]; *p; p++)
992 nalt += *p == ',';
994 data->insn_static_data = insn_static_data
995 = get_static_insn_data (-1, nop, 0, nalt);
996 for (i = 0; i < nop; i++)
998 insn_static_data->operand[i].mode = operand_mode[i];
999 insn_static_data->operand[i].constraint = constraints[i];
1000 insn_static_data->operand[i].strict_low = false;
1001 insn_static_data->operand[i].is_operator = false;
1002 insn_static_data->operand[i].is_address = false;
1005 for (i = 0; i < insn_static_data->n_operands; i++)
1006 insn_static_data->operand[i].type
1007 = (insn_static_data->operand[i].constraint[0] == '=' ? OP_OUT
1008 : insn_static_data->operand[i].constraint[0] == '+' ? OP_INOUT
1009 : OP_IN);
1010 data->preferred_alternatives = ALL_ALTERNATIVES;
1011 if (nop > 0)
1013 operand_alternative *op_alt = XCNEWVEC (operand_alternative,
1014 nalt * nop);
1015 preprocess_constraints (nop, nalt, constraints, op_alt);
1016 setup_operand_alternative (data, op_alt);
1019 else
1021 insn_extract (insn);
1022 data->insn_static_data = insn_static_data
1023 = get_static_insn_data (icode, insn_data[icode].n_operands,
1024 insn_data[icode].n_dups,
1025 insn_data[icode].n_alternatives);
1026 n = insn_static_data->n_operands;
1027 if (n == 0)
1028 locs = NULL;
1029 else
1031 locs = XNEWVEC (rtx *, n);
1032 memcpy (locs, recog_data.operand_loc, n * sizeof (rtx *));
1034 data->operand_loc = locs;
1035 n = insn_static_data->n_dups;
1036 if (n == 0)
1037 locs = NULL;
1038 else
1040 locs = XNEWVEC (rtx *, n);
1041 memcpy (locs, recog_data.dup_loc, n * sizeof (rtx *));
1043 data->dup_loc = locs;
1044 data->preferred_alternatives = get_preferred_alternatives (insn);
1045 const operand_alternative *op_alt = preprocess_insn_constraints (icode);
1046 if (!insn_static_data->operand_alternative)
1047 setup_operand_alternative (data, op_alt);
1048 else if (op_alt != insn_static_data->operand_alternative)
1049 insn_static_data->operand_alternative = op_alt;
1051 if (GET_CODE (PATTERN (insn)) == CLOBBER || GET_CODE (PATTERN (insn)) == USE)
1052 insn_static_data->hard_regs = NULL;
1053 else
1054 insn_static_data->hard_regs
1055 = collect_non_operand_hard_regs (&PATTERN (insn), data,
1056 NULL, OP_IN, false);
1057 data->arg_hard_regs = NULL;
1058 if (CALL_P (insn))
1060 bool use_p;
1061 rtx link;
1062 int n_hard_regs, regno, arg_hard_regs[FIRST_PSEUDO_REGISTER];
1064 n_hard_regs = 0;
1065 /* Finding implicit hard register usage. We believe it will be
1066 not changed whatever transformations are used. Call insns
1067 are such example. */
1068 for (link = CALL_INSN_FUNCTION_USAGE (insn);
1069 link != NULL_RTX;
1070 link = XEXP (link, 1))
1071 if (((use_p = GET_CODE (XEXP (link, 0)) == USE)
1072 || GET_CODE (XEXP (link, 0)) == CLOBBER)
1073 && REG_P (XEXP (XEXP (link, 0), 0)))
1075 regno = REGNO (XEXP (XEXP (link, 0), 0));
1076 lra_assert (regno < FIRST_PSEUDO_REGISTER);
1077 /* It is an argument register. */
1078 for (i = REG_NREGS (XEXP (XEXP (link, 0), 0)) - 1; i >= 0; i--)
1079 arg_hard_regs[n_hard_regs++]
1080 = regno + i + (use_p ? 0 : FIRST_PSEUDO_REGISTER);
1082 if (n_hard_regs != 0)
1084 arg_hard_regs[n_hard_regs++] = -1;
1085 data->arg_hard_regs = XNEWVEC (int, n_hard_regs);
1086 memcpy (data->arg_hard_regs, arg_hard_regs,
1087 sizeof (int) * n_hard_regs);
1090 /* Some output operand can be recognized only from the context not
1091 from the constraints which are empty in this case. Call insn may
1092 contain a hard register in set destination with empty constraint
1093 and extract_insn treats them as an input. */
1094 for (i = 0; i < insn_static_data->n_operands; i++)
1096 int j;
1097 rtx pat, set;
1098 struct lra_operand_data *operand = &insn_static_data->operand[i];
1100 /* ??? Should we treat 'X' the same way. It looks to me that
1101 'X' means anything and empty constraint means we do not
1102 care. */
1103 if (operand->type != OP_IN || *operand->constraint != '\0'
1104 || operand->is_operator)
1105 continue;
1106 pat = PATTERN (insn);
1107 if (GET_CODE (pat) == SET)
1109 if (data->operand_loc[i] != &SET_DEST (pat))
1110 continue;
1112 else if (GET_CODE (pat) == PARALLEL)
1114 for (j = XVECLEN (pat, 0) - 1; j >= 0; j--)
1116 set = XVECEXP (PATTERN (insn), 0, j);
1117 if (GET_CODE (set) == SET
1118 && &SET_DEST (set) == data->operand_loc[i])
1119 break;
1121 if (j < 0)
1122 continue;
1124 else
1125 continue;
1126 operand->type = OP_OUT;
1128 return data;
1131 /* Return info about insn give by UID. The info should be already set
1132 up. */
1133 static lra_insn_recog_data_t
1134 get_insn_recog_data_by_uid (int uid)
1136 lra_insn_recog_data_t data;
1138 data = lra_insn_recog_data[uid];
1139 lra_assert (data != NULL);
1140 return data;
1143 /* Invalidate all info about insn given by its UID. */
1144 static void
1145 invalidate_insn_recog_data (int uid)
1147 lra_insn_recog_data_t data;
1149 data = lra_insn_recog_data[uid];
1150 lra_assert (data != NULL);
1151 free_insn_recog_data (data);
1152 lra_insn_recog_data[uid] = NULL;
1155 /* Update all the insn info about INSN. It is usually called when
1156 something in the insn was changed. Return the updated info. */
1157 lra_insn_recog_data_t
1158 lra_update_insn_recog_data (rtx_insn *insn)
1160 lra_insn_recog_data_t data;
1161 int n;
1162 unsigned int uid = INSN_UID (insn);
1163 struct lra_static_insn_data *insn_static_data;
1164 HOST_WIDE_INT sp_offset = 0;
1166 check_and_expand_insn_recog_data (uid);
1167 if ((data = lra_insn_recog_data[uid]) != NULL
1168 && data->icode != INSN_CODE (insn))
1170 sp_offset = data->sp_offset;
1171 invalidate_insn_data_regno_info (data, insn, get_insn_freq (insn));
1172 invalidate_insn_recog_data (uid);
1173 data = NULL;
1175 if (data == NULL)
1177 data = lra_get_insn_recog_data (insn);
1178 /* Initiate or restore SP offset. */
1179 data->sp_offset = sp_offset;
1180 return data;
1182 insn_static_data = data->insn_static_data;
1183 data->used_insn_alternative = -1;
1184 if (DEBUG_INSN_P (insn))
1185 return data;
1186 if (data->icode < 0)
1188 int nop;
1189 machine_mode operand_mode[MAX_RECOG_OPERANDS];
1190 const char *constraints[MAX_RECOG_OPERANDS];
1192 nop = asm_noperands (PATTERN (insn));
1193 if (nop >= 0)
1195 lra_assert (nop == data->insn_static_data->n_operands);
1196 /* Now get the operand values and constraints out of the
1197 insn. */
1198 decode_asm_operands (PATTERN (insn), NULL,
1199 data->operand_loc,
1200 constraints, operand_mode, NULL);
1201 #ifdef ENABLE_CHECKING
1203 int i;
1205 for (i = 0; i < nop; i++)
1206 lra_assert
1207 (insn_static_data->operand[i].mode == operand_mode[i]
1208 && insn_static_data->operand[i].constraint == constraints[i]
1209 && ! insn_static_data->operand[i].is_operator);
1211 #endif
1213 #ifdef ENABLE_CHECKING
1215 int i;
1217 for (i = 0; i < insn_static_data->n_operands; i++)
1218 lra_assert
1219 (insn_static_data->operand[i].type
1220 == (insn_static_data->operand[i].constraint[0] == '=' ? OP_OUT
1221 : insn_static_data->operand[i].constraint[0] == '+' ? OP_INOUT
1222 : OP_IN));
1224 #endif
1226 else
1228 insn_extract (insn);
1229 n = insn_static_data->n_operands;
1230 if (n != 0)
1231 memcpy (data->operand_loc, recog_data.operand_loc, n * sizeof (rtx *));
1232 n = insn_static_data->n_dups;
1233 if (n != 0)
1234 memcpy (data->dup_loc, recog_data.dup_loc, n * sizeof (rtx *));
1235 lra_assert (check_bool_attrs (insn));
1237 return data;
1240 /* Set up that INSN is using alternative ALT now. */
1241 void
1242 lra_set_used_insn_alternative (rtx_insn *insn, int alt)
1244 lra_insn_recog_data_t data;
1246 data = lra_get_insn_recog_data (insn);
1247 data->used_insn_alternative = alt;
1250 /* Set up that insn with UID is using alternative ALT now. The insn
1251 info should be already set up. */
1252 void
1253 lra_set_used_insn_alternative_by_uid (int uid, int alt)
1255 lra_insn_recog_data_t data;
1257 check_and_expand_insn_recog_data (uid);
1258 data = lra_insn_recog_data[uid];
1259 lra_assert (data != NULL);
1260 data->used_insn_alternative = alt;
1265 /* This page contains code dealing with common register info and
1266 pseudo copies. */
1268 /* The size of the following array. */
1269 static int reg_info_size;
1270 /* Common info about each register. */
1271 struct lra_reg *lra_reg_info;
1273 /* Last register value. */
1274 static int last_reg_value;
1276 /* Return new register value. */
1277 static int
1278 get_new_reg_value (void)
1280 return ++last_reg_value;
1283 /* Vec referring to pseudo copies. */
1284 static vec<lra_copy_t> copy_vec;
1286 /* Initialize I-th element of lra_reg_info. */
1287 static inline void
1288 initialize_lra_reg_info_element (int i)
1290 bitmap_initialize (&lra_reg_info[i].insn_bitmap, &reg_obstack);
1291 #ifdef STACK_REGS
1292 lra_reg_info[i].no_stack_p = false;
1293 #endif
1294 CLEAR_HARD_REG_SET (lra_reg_info[i].conflict_hard_regs);
1295 CLEAR_HARD_REG_SET (lra_reg_info[i].actual_call_used_reg_set);
1296 lra_reg_info[i].preferred_hard_regno1 = -1;
1297 lra_reg_info[i].preferred_hard_regno2 = -1;
1298 lra_reg_info[i].preferred_hard_regno_profit1 = 0;
1299 lra_reg_info[i].preferred_hard_regno_profit2 = 0;
1300 lra_reg_info[i].biggest_mode = VOIDmode;
1301 lra_reg_info[i].live_ranges = NULL;
1302 lra_reg_info[i].nrefs = lra_reg_info[i].freq = 0;
1303 lra_reg_info[i].last_reload = 0;
1304 lra_reg_info[i].restore_regno = -1;
1305 lra_reg_info[i].val = get_new_reg_value ();
1306 lra_reg_info[i].offset = 0;
1307 lra_reg_info[i].copies = NULL;
1310 /* Initialize common reg info and copies. */
1311 static void
1312 init_reg_info (void)
1314 int i;
1316 last_reg_value = 0;
1317 reg_info_size = max_reg_num () * 3 / 2 + 1;
1318 lra_reg_info = XNEWVEC (struct lra_reg, reg_info_size);
1319 for (i = 0; i < reg_info_size; i++)
1320 initialize_lra_reg_info_element (i);
1321 copy_vec.create (100);
1325 /* Finish common reg info and copies. */
1326 static void
1327 finish_reg_info (void)
1329 int i;
1331 for (i = 0; i < reg_info_size; i++)
1332 bitmap_clear (&lra_reg_info[i].insn_bitmap);
1333 free (lra_reg_info);
1334 reg_info_size = 0;
1337 /* Expand common reg info if it is necessary. */
1338 static void
1339 expand_reg_info (void)
1341 int i, old = reg_info_size;
1343 if (reg_info_size > max_reg_num ())
1344 return;
1345 reg_info_size = max_reg_num () * 3 / 2 + 1;
1346 lra_reg_info = XRESIZEVEC (struct lra_reg, lra_reg_info, reg_info_size);
1347 for (i = old; i < reg_info_size; i++)
1348 initialize_lra_reg_info_element (i);
1351 /* Free all copies. */
1352 void
1353 lra_free_copies (void)
1355 lra_copy_t cp;
1357 while (copy_vec.length () != 0)
1359 cp = copy_vec.pop ();
1360 lra_reg_info[cp->regno1].copies = lra_reg_info[cp->regno2].copies = NULL;
1361 lra_copy_pool.remove (cp);
1365 /* Create copy of two pseudos REGNO1 and REGNO2. The copy execution
1366 frequency is FREQ. */
1367 void
1368 lra_create_copy (int regno1, int regno2, int freq)
1370 bool regno1_dest_p;
1371 lra_copy_t cp;
1373 lra_assert (regno1 != regno2);
1374 regno1_dest_p = true;
1375 if (regno1 > regno2)
1377 std::swap (regno1, regno2);
1378 regno1_dest_p = false;
1380 cp = lra_copy_pool.allocate ();
1381 copy_vec.safe_push (cp);
1382 cp->regno1_dest_p = regno1_dest_p;
1383 cp->freq = freq;
1384 cp->regno1 = regno1;
1385 cp->regno2 = regno2;
1386 cp->regno1_next = lra_reg_info[regno1].copies;
1387 lra_reg_info[regno1].copies = cp;
1388 cp->regno2_next = lra_reg_info[regno2].copies;
1389 lra_reg_info[regno2].copies = cp;
1390 if (lra_dump_file != NULL)
1391 fprintf (lra_dump_file, " Creating copy r%d%sr%d@%d\n",
1392 regno1, regno1_dest_p ? "<-" : "->", regno2, freq);
1395 /* Return N-th (0, 1, ...) copy. If there is no copy, return
1396 NULL. */
1397 lra_copy_t
1398 lra_get_copy (int n)
1400 if (n >= (int) copy_vec.length ())
1401 return NULL;
1402 return copy_vec[n];
1407 /* This page contains code dealing with info about registers in
1408 insns. */
1410 /* Process X of insn UID recursively and add info (operand type is
1411 given by TYPE, flag of that it is early clobber is EARLY_CLOBBER)
1412 about registers in X to the insn DATA. */
1413 static void
1414 add_regs_to_insn_regno_info (lra_insn_recog_data_t data, rtx x, int uid,
1415 enum op_type type, bool early_clobber)
1417 int i, j, regno;
1418 bool subreg_p;
1419 machine_mode mode;
1420 const char *fmt;
1421 enum rtx_code code;
1422 struct lra_insn_reg *curr;
1424 code = GET_CODE (x);
1425 mode = GET_MODE (x);
1426 subreg_p = false;
1427 if (GET_CODE (x) == SUBREG)
1429 x = SUBREG_REG (x);
1430 code = GET_CODE (x);
1431 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (x)))
1433 mode = GET_MODE (x);
1434 if (GET_MODE_SIZE (mode) > REGMODE_NATURAL_SIZE (mode))
1435 subreg_p = true;
1438 if (REG_P (x))
1440 regno = REGNO (x);
1441 /* Process all regs even unallocatable ones as we need info about
1442 all regs for rematerialization pass. */
1443 expand_reg_info ();
1444 if (bitmap_set_bit (&lra_reg_info[regno].insn_bitmap, uid))
1446 data->regs = new_insn_reg (data->insn, regno, type, mode, subreg_p,
1447 early_clobber, data->regs);
1448 return;
1450 else
1452 for (curr = data->regs; curr != NULL; curr = curr->next)
1453 if (curr->regno == regno)
1455 if (curr->subreg_p != subreg_p || curr->biggest_mode != mode)
1456 /* The info can not be integrated into the found
1457 structure. */
1458 data->regs = new_insn_reg (data->insn, regno, type, mode,
1459 subreg_p, early_clobber,
1460 data->regs);
1461 else
1463 if (curr->type != type)
1464 curr->type = OP_INOUT;
1465 if (curr->early_clobber != early_clobber)
1466 curr->early_clobber = true;
1468 return;
1470 gcc_unreachable ();
1474 switch (code)
1476 case SET:
1477 add_regs_to_insn_regno_info (data, SET_DEST (x), uid, OP_OUT, false);
1478 add_regs_to_insn_regno_info (data, SET_SRC (x), uid, OP_IN, false);
1479 break;
1480 case CLOBBER:
1481 /* We treat clobber of non-operand hard registers as early
1482 clobber (the behavior is expected from asm). */
1483 add_regs_to_insn_regno_info (data, XEXP (x, 0), uid, OP_OUT, true);
1484 break;
1485 case PRE_INC: case PRE_DEC: case POST_INC: case POST_DEC:
1486 add_regs_to_insn_regno_info (data, XEXP (x, 0), uid, OP_INOUT, false);
1487 break;
1488 case PRE_MODIFY: case POST_MODIFY:
1489 add_regs_to_insn_regno_info (data, XEXP (x, 0), uid, OP_INOUT, false);
1490 add_regs_to_insn_regno_info (data, XEXP (x, 1), uid, OP_IN, false);
1491 break;
1492 default:
1493 if ((code != PARALLEL && code != EXPR_LIST) || type != OP_OUT)
1494 /* Some targets place small structures in registers for return
1495 values of functions, and those registers are wrapped in
1496 PARALLEL that we may see as the destination of a SET. Here
1497 is an example:
1499 (call_insn 13 12 14 2 (set (parallel:BLK [
1500 (expr_list:REG_DEP_TRUE (reg:DI 0 ax)
1501 (const_int 0 [0]))
1502 (expr_list:REG_DEP_TRUE (reg:DI 1 dx)
1503 (const_int 8 [0x8]))
1505 (call (mem:QI (symbol_ref:DI (... */
1506 type = OP_IN;
1507 fmt = GET_RTX_FORMAT (code);
1508 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1510 if (fmt[i] == 'e')
1511 add_regs_to_insn_regno_info (data, XEXP (x, i), uid, type, false);
1512 else if (fmt[i] == 'E')
1514 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1515 add_regs_to_insn_regno_info (data, XVECEXP (x, i, j), uid,
1516 type, false);
1522 /* Return execution frequency of INSN. */
1523 static int
1524 get_insn_freq (rtx_insn *insn)
1526 basic_block bb = BLOCK_FOR_INSN (insn);
1528 gcc_checking_assert (bb != NULL);
1529 return REG_FREQ_FROM_BB (bb);
1532 /* Invalidate all reg info of INSN with DATA and execution frequency
1533 FREQ. Update common info about the invalidated registers. */
1534 static void
1535 invalidate_insn_data_regno_info (lra_insn_recog_data_t data, rtx_insn *insn,
1536 int freq)
1538 int uid;
1539 bool debug_p;
1540 unsigned int i;
1541 struct lra_insn_reg *ir, *next_ir;
1543 uid = INSN_UID (insn);
1544 debug_p = DEBUG_INSN_P (insn);
1545 for (ir = data->regs; ir != NULL; ir = next_ir)
1547 i = ir->regno;
1548 next_ir = ir->next;
1549 lra_insn_reg_pool.remove (ir);
1550 bitmap_clear_bit (&lra_reg_info[i].insn_bitmap, uid);
1551 if (i >= FIRST_PSEUDO_REGISTER && ! debug_p)
1553 lra_reg_info[i].nrefs--;
1554 lra_reg_info[i].freq -= freq;
1555 lra_assert (lra_reg_info[i].nrefs >= 0 && lra_reg_info[i].freq >= 0);
1558 data->regs = NULL;
1561 /* Invalidate all reg info of INSN. Update common info about the
1562 invalidated registers. */
1563 void
1564 lra_invalidate_insn_regno_info (rtx_insn *insn)
1566 invalidate_insn_data_regno_info (lra_get_insn_recog_data (insn), insn,
1567 get_insn_freq (insn));
1570 /* Update common reg info from reg info of insn given by its DATA and
1571 execution frequency FREQ. */
1572 static void
1573 setup_insn_reg_info (lra_insn_recog_data_t data, int freq)
1575 unsigned int i;
1576 struct lra_insn_reg *ir;
1578 for (ir = data->regs; ir != NULL; ir = ir->next)
1579 if ((i = ir->regno) >= FIRST_PSEUDO_REGISTER)
1581 lra_reg_info[i].nrefs++;
1582 lra_reg_info[i].freq += freq;
1586 /* Set up insn reg info of INSN. Update common reg info from reg info
1587 of INSN. */
1588 void
1589 lra_update_insn_regno_info (rtx_insn *insn)
1591 int i, uid, freq;
1592 lra_insn_recog_data_t data;
1593 struct lra_static_insn_data *static_data;
1594 enum rtx_code code;
1595 rtx link;
1597 if (! INSN_P (insn))
1598 return;
1599 data = lra_get_insn_recog_data (insn);
1600 static_data = data->insn_static_data;
1601 freq = get_insn_freq (insn);
1602 invalidate_insn_data_regno_info (data, insn, freq);
1603 uid = INSN_UID (insn);
1604 for (i = static_data->n_operands - 1; i >= 0; i--)
1605 add_regs_to_insn_regno_info (data, *data->operand_loc[i], uid,
1606 static_data->operand[i].type,
1607 static_data->operand[i].early_clobber);
1608 if ((code = GET_CODE (PATTERN (insn))) == CLOBBER || code == USE)
1609 add_regs_to_insn_regno_info (data, XEXP (PATTERN (insn), 0), uid,
1610 code == USE ? OP_IN : OP_OUT, false);
1611 if (CALL_P (insn))
1612 /* On some targets call insns can refer to pseudos in memory in
1613 CALL_INSN_FUNCTION_USAGE list. Process them in order to
1614 consider their occurrences in calls for different
1615 transformations (e.g. inheritance) with given pseudos. */
1616 for (link = CALL_INSN_FUNCTION_USAGE (insn);
1617 link != NULL_RTX;
1618 link = XEXP (link, 1))
1619 if (((code = GET_CODE (XEXP (link, 0))) == USE || code == CLOBBER)
1620 && MEM_P (XEXP (XEXP (link, 0), 0)))
1621 add_regs_to_insn_regno_info (data, XEXP (XEXP (link, 0), 0), uid,
1622 code == USE ? OP_IN : OP_OUT, false);
1623 if (NONDEBUG_INSN_P (insn))
1624 setup_insn_reg_info (data, freq);
1627 /* Return reg info of insn given by it UID. */
1628 struct lra_insn_reg *
1629 lra_get_insn_regs (int uid)
1631 lra_insn_recog_data_t data;
1633 data = get_insn_recog_data_by_uid (uid);
1634 return data->regs;
1639 /* This page contains code dealing with stack of the insns which
1640 should be processed by the next constraint pass. */
1642 /* Bitmap used to put an insn on the stack only in one exemplar. */
1643 static sbitmap lra_constraint_insn_stack_bitmap;
1645 /* The stack itself. */
1646 vec<rtx_insn *> lra_constraint_insn_stack;
1648 /* Put INSN on the stack. If ALWAYS_UPDATE is true, always update the reg
1649 info for INSN, otherwise only update it if INSN is not already on the
1650 stack. */
1651 static inline void
1652 lra_push_insn_1 (rtx_insn *insn, bool always_update)
1654 unsigned int uid = INSN_UID (insn);
1655 if (always_update)
1656 lra_update_insn_regno_info (insn);
1657 if (uid >= SBITMAP_SIZE (lra_constraint_insn_stack_bitmap))
1658 lra_constraint_insn_stack_bitmap =
1659 sbitmap_resize (lra_constraint_insn_stack_bitmap, 3 * uid / 2, 0);
1660 if (bitmap_bit_p (lra_constraint_insn_stack_bitmap, uid))
1661 return;
1662 bitmap_set_bit (lra_constraint_insn_stack_bitmap, uid);
1663 if (! always_update)
1664 lra_update_insn_regno_info (insn);
1665 lra_constraint_insn_stack.safe_push (insn);
1668 /* Put INSN on the stack. */
1669 void
1670 lra_push_insn (rtx_insn *insn)
1672 lra_push_insn_1 (insn, false);
1675 /* Put INSN on the stack and update its reg info. */
1676 void
1677 lra_push_insn_and_update_insn_regno_info (rtx_insn *insn)
1679 lra_push_insn_1 (insn, true);
1682 /* Put insn with UID on the stack. */
1683 void
1684 lra_push_insn_by_uid (unsigned int uid)
1686 lra_push_insn (lra_insn_recog_data[uid]->insn);
1689 /* Take the last-inserted insns off the stack and return it. */
1690 rtx_insn *
1691 lra_pop_insn (void)
1693 rtx_insn *insn = lra_constraint_insn_stack.pop ();
1694 bitmap_clear_bit (lra_constraint_insn_stack_bitmap, INSN_UID (insn));
1695 return insn;
1698 /* Return the current size of the insn stack. */
1699 unsigned int
1700 lra_insn_stack_length (void)
1702 return lra_constraint_insn_stack.length ();
1705 /* Push insns FROM to TO (excluding it) going in reverse order. */
1706 static void
1707 push_insns (rtx_insn *from, rtx_insn *to)
1709 rtx_insn *insn;
1711 if (from == NULL_RTX)
1712 return;
1713 for (insn = from; insn != to; insn = PREV_INSN (insn))
1714 if (INSN_P (insn))
1715 lra_push_insn (insn);
1718 /* Set up sp offset for insn in range [FROM, LAST]. The offset is
1719 taken from the next BB insn after LAST or zero if there in such
1720 insn. */
1721 static void
1722 setup_sp_offset (rtx_insn *from, rtx_insn *last)
1724 rtx_insn *before = next_nonnote_insn_bb (last);
1725 HOST_WIDE_INT offset = (before == NULL_RTX || ! INSN_P (before)
1726 ? 0 : lra_get_insn_recog_data (before)->sp_offset);
1728 for (rtx_insn *insn = from; insn != NEXT_INSN (last); insn = NEXT_INSN (insn))
1729 lra_get_insn_recog_data (insn)->sp_offset = offset;
1732 /* Emit insns BEFORE before INSN and insns AFTER after INSN. Put the
1733 insns onto the stack. Print about emitting the insns with
1734 TITLE. */
1735 void
1736 lra_process_new_insns (rtx_insn *insn, rtx_insn *before, rtx_insn *after,
1737 const char *title)
1739 rtx_insn *last;
1741 if (before == NULL_RTX && after == NULL_RTX)
1742 return;
1743 if (lra_dump_file != NULL)
1745 dump_insn_slim (lra_dump_file, insn);
1746 if (before != NULL_RTX)
1748 fprintf (lra_dump_file," %s before:\n", title);
1749 dump_rtl_slim (lra_dump_file, before, NULL, -1, 0);
1751 if (after != NULL_RTX)
1753 fprintf (lra_dump_file, " %s after:\n", title);
1754 dump_rtl_slim (lra_dump_file, after, NULL, -1, 0);
1756 fprintf (lra_dump_file, "\n");
1758 if (before != NULL_RTX)
1760 emit_insn_before (before, insn);
1761 push_insns (PREV_INSN (insn), PREV_INSN (before));
1762 setup_sp_offset (before, PREV_INSN (insn));
1764 if (after != NULL_RTX)
1766 for (last = after; NEXT_INSN (last) != NULL_RTX; last = NEXT_INSN (last))
1768 emit_insn_after (after, insn);
1769 push_insns (last, insn);
1770 setup_sp_offset (after, last);
1776 /* Replace all references to register OLD_REGNO in *LOC with pseudo
1777 register NEW_REG. Try to simplify subreg of constant if SUBREG_P.
1778 Return true if any change was made. */
1779 bool
1780 lra_substitute_pseudo (rtx *loc, int old_regno, rtx new_reg, bool subreg_p)
1782 rtx x = *loc;
1783 bool result = false;
1784 enum rtx_code code;
1785 const char *fmt;
1786 int i, j;
1788 if (x == NULL_RTX)
1789 return false;
1791 code = GET_CODE (x);
1792 if (code == SUBREG && subreg_p)
1794 rtx subst, inner = SUBREG_REG (x);
1795 /* Transform subreg of constant while we still have inner mode
1796 of the subreg. The subreg internal should not be an insn
1797 operand. */
1798 if (REG_P (inner) && (int) REGNO (inner) == old_regno
1799 && CONSTANT_P (new_reg)
1800 && (subst = simplify_subreg (GET_MODE (x), new_reg, GET_MODE (inner),
1801 SUBREG_BYTE (x))) != NULL_RTX)
1803 *loc = subst;
1804 return true;
1808 else if (code == REG && (int) REGNO (x) == old_regno)
1810 machine_mode mode = GET_MODE (x);
1811 machine_mode inner_mode = GET_MODE (new_reg);
1813 if (mode != inner_mode
1814 && ! (CONST_INT_P (new_reg) && SCALAR_INT_MODE_P (mode)))
1816 if (GET_MODE_SIZE (mode) >= GET_MODE_SIZE (inner_mode)
1817 || ! SCALAR_INT_MODE_P (inner_mode))
1818 new_reg = gen_rtx_SUBREG (mode, new_reg, 0);
1819 else
1820 new_reg = gen_lowpart_SUBREG (mode, new_reg);
1822 *loc = new_reg;
1823 return true;
1826 /* Scan all the operand sub-expressions. */
1827 fmt = GET_RTX_FORMAT (code);
1828 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1830 if (fmt[i] == 'e')
1832 if (lra_substitute_pseudo (&XEXP (x, i), old_regno,
1833 new_reg, subreg_p))
1834 result = true;
1836 else if (fmt[i] == 'E')
1838 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1839 if (lra_substitute_pseudo (&XVECEXP (x, i, j), old_regno,
1840 new_reg, subreg_p))
1841 result = true;
1844 return result;
1847 /* Call lra_substitute_pseudo within an insn. Try to simplify subreg
1848 of constant if SUBREG_P. This won't update the insn ptr, just the
1849 contents of the insn. */
1850 bool
1851 lra_substitute_pseudo_within_insn (rtx_insn *insn, int old_regno,
1852 rtx new_reg, bool subreg_p)
1854 rtx loc = insn;
1855 return lra_substitute_pseudo (&loc, old_regno, new_reg, subreg_p);
1860 /* This page contains code dealing with scratches (changing them onto
1861 pseudos and restoring them from the pseudos).
1863 We change scratches into pseudos at the beginning of LRA to
1864 simplify dealing with them (conflicts, hard register assignments).
1866 If the pseudo denoting scratch was spilled it means that we do need
1867 a hard register for it. Such pseudos are transformed back to
1868 scratches at the end of LRA. */
1870 /* Description of location of a former scratch operand. */
1871 struct sloc
1873 rtx_insn *insn; /* Insn where the scratch was. */
1874 int nop; /* Number of the operand which was a scratch. */
1877 typedef struct sloc *sloc_t;
1879 /* Locations of the former scratches. */
1880 static vec<sloc_t> scratches;
1882 /* Bitmap of scratch regnos. */
1883 static bitmap_head scratch_bitmap;
1885 /* Bitmap of scratch operands. */
1886 static bitmap_head scratch_operand_bitmap;
1888 /* Return true if pseudo REGNO is made of SCRATCH. */
1889 bool
1890 lra_former_scratch_p (int regno)
1892 return bitmap_bit_p (&scratch_bitmap, regno);
1895 /* Return true if the operand NOP of INSN is a former scratch. */
1896 bool
1897 lra_former_scratch_operand_p (rtx_insn *insn, int nop)
1899 return bitmap_bit_p (&scratch_operand_bitmap,
1900 INSN_UID (insn) * MAX_RECOG_OPERANDS + nop) != 0;
1903 /* Register operand NOP in INSN as a former scratch. It will be
1904 changed to scratch back, if it is necessary, at the LRA end. */
1905 void
1906 lra_register_new_scratch_op (rtx_insn *insn, int nop)
1908 lra_insn_recog_data_t id = lra_get_insn_recog_data (insn);
1909 rtx op = *id->operand_loc[nop];
1910 sloc_t loc = XNEW (struct sloc);
1911 lra_assert (REG_P (op));
1912 loc->insn = insn;
1913 loc->nop = nop;
1914 scratches.safe_push (loc);
1915 bitmap_set_bit (&scratch_bitmap, REGNO (op));
1916 bitmap_set_bit (&scratch_operand_bitmap,
1917 INSN_UID (insn) * MAX_RECOG_OPERANDS + nop);
1918 add_reg_note (insn, REG_UNUSED, op);
1921 /* Change scratches onto pseudos and save their location. */
1922 static void
1923 remove_scratches (void)
1925 int i;
1926 bool insn_changed_p;
1927 basic_block bb;
1928 rtx_insn *insn;
1929 rtx reg;
1930 lra_insn_recog_data_t id;
1931 struct lra_static_insn_data *static_id;
1933 scratches.create (get_max_uid ());
1934 bitmap_initialize (&scratch_bitmap, &reg_obstack);
1935 bitmap_initialize (&scratch_operand_bitmap, &reg_obstack);
1936 FOR_EACH_BB_FN (bb, cfun)
1937 FOR_BB_INSNS (bb, insn)
1938 if (INSN_P (insn))
1940 id = lra_get_insn_recog_data (insn);
1941 static_id = id->insn_static_data;
1942 insn_changed_p = false;
1943 for (i = 0; i < static_id->n_operands; i++)
1944 if (GET_CODE (*id->operand_loc[i]) == SCRATCH
1945 && GET_MODE (*id->operand_loc[i]) != VOIDmode)
1947 insn_changed_p = true;
1948 *id->operand_loc[i] = reg
1949 = lra_create_new_reg (static_id->operand[i].mode,
1950 *id->operand_loc[i], ALL_REGS, NULL);
1951 lra_register_new_scratch_op (insn, i);
1952 if (lra_dump_file != NULL)
1953 fprintf (lra_dump_file,
1954 "Removing SCRATCH in insn #%u (nop %d)\n",
1955 INSN_UID (insn), i);
1957 if (insn_changed_p)
1958 /* Because we might use DF right after caller-saves sub-pass
1959 we need to keep DF info up to date. */
1960 df_insn_rescan (insn);
1964 /* Changes pseudos created by function remove_scratches onto scratches. */
1965 static void
1966 restore_scratches (void)
1968 int regno;
1969 unsigned i;
1970 sloc_t loc;
1971 rtx_insn *last = NULL;
1972 lra_insn_recog_data_t id = NULL;
1974 for (i = 0; scratches.iterate (i, &loc); i++)
1976 if (last != loc->insn)
1978 last = loc->insn;
1979 id = lra_get_insn_recog_data (last);
1981 if (REG_P (*id->operand_loc[loc->nop])
1982 && ((regno = REGNO (*id->operand_loc[loc->nop]))
1983 >= FIRST_PSEUDO_REGISTER)
1984 && lra_get_regno_hard_regno (regno) < 0)
1986 /* It should be only case when scratch register with chosen
1987 constraint 'X' did not get memory or hard register. */
1988 lra_assert (lra_former_scratch_p (regno));
1989 *id->operand_loc[loc->nop]
1990 = gen_rtx_SCRATCH (GET_MODE (*id->operand_loc[loc->nop]));
1991 lra_update_dup (id, loc->nop);
1992 if (lra_dump_file != NULL)
1993 fprintf (lra_dump_file, "Restoring SCRATCH in insn #%u(nop %d)\n",
1994 INSN_UID (loc->insn), loc->nop);
1997 for (i = 0; scratches.iterate (i, &loc); i++)
1998 free (loc);
1999 scratches.release ();
2000 bitmap_clear (&scratch_bitmap);
2001 bitmap_clear (&scratch_operand_bitmap);
2006 #ifdef ENABLE_CHECKING
2008 /* Function checks RTL for correctness. If FINAL_P is true, it is
2009 done at the end of LRA and the check is more rigorous. */
2010 static void
2011 check_rtl (bool final_p)
2013 basic_block bb;
2014 rtx_insn *insn;
2016 lra_assert (! final_p || reload_completed);
2017 FOR_EACH_BB_FN (bb, cfun)
2018 FOR_BB_INSNS (bb, insn)
2019 if (NONDEBUG_INSN_P (insn)
2020 && GET_CODE (PATTERN (insn)) != USE
2021 && GET_CODE (PATTERN (insn)) != CLOBBER
2022 && GET_CODE (PATTERN (insn)) != ASM_INPUT)
2024 if (final_p)
2026 #ifdef ENABLED_CHECKING
2027 extract_constrain_insn (insn);
2028 #endif
2029 continue;
2031 /* LRA code is based on assumption that all addresses can be
2032 correctly decomposed. LRA can generate reloads for
2033 decomposable addresses. The decomposition code checks the
2034 correctness of the addresses. So we don't need to check
2035 the addresses here. Don't call insn_invalid_p here, it can
2036 change the code at this stage. */
2037 if (recog_memoized (insn) < 0 && asm_noperands (PATTERN (insn)) < 0)
2038 fatal_insn_not_found (insn);
2041 #endif /* #ifdef ENABLE_CHECKING */
2043 /* Determine if the current function has an exception receiver block
2044 that reaches the exit block via non-exceptional edges */
2045 static bool
2046 has_nonexceptional_receiver (void)
2048 edge e;
2049 edge_iterator ei;
2050 basic_block *tos, *worklist, bb;
2052 /* If we're not optimizing, then just err on the safe side. */
2053 if (!optimize)
2054 return true;
2056 /* First determine which blocks can reach exit via normal paths. */
2057 tos = worklist = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun) + 1);
2059 FOR_EACH_BB_FN (bb, cfun)
2060 bb->flags &= ~BB_REACHABLE;
2062 /* Place the exit block on our worklist. */
2063 EXIT_BLOCK_PTR_FOR_FN (cfun)->flags |= BB_REACHABLE;
2064 *tos++ = EXIT_BLOCK_PTR_FOR_FN (cfun);
2066 /* Iterate: find everything reachable from what we've already seen. */
2067 while (tos != worklist)
2069 bb = *--tos;
2071 FOR_EACH_EDGE (e, ei, bb->preds)
2072 if (e->flags & EDGE_ABNORMAL)
2074 free (worklist);
2075 return true;
2077 else
2079 basic_block src = e->src;
2081 if (!(src->flags & BB_REACHABLE))
2083 src->flags |= BB_REACHABLE;
2084 *tos++ = src;
2088 free (worklist);
2089 /* No exceptional block reached exit unexceptionally. */
2090 return false;
2094 /* Process recursively X of INSN and add REG_INC notes if necessary. */
2095 static void
2096 add_auto_inc_notes (rtx_insn *insn, rtx x)
2098 enum rtx_code code = GET_CODE (x);
2099 const char *fmt;
2100 int i, j;
2102 if (code == MEM && auto_inc_p (XEXP (x, 0)))
2104 add_reg_note (insn, REG_INC, XEXP (XEXP (x, 0), 0));
2105 return;
2108 /* Scan all X sub-expressions. */
2109 fmt = GET_RTX_FORMAT (code);
2110 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2112 if (fmt[i] == 'e')
2113 add_auto_inc_notes (insn, XEXP (x, i));
2114 else if (fmt[i] == 'E')
2115 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2116 add_auto_inc_notes (insn, XVECEXP (x, i, j));
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);
2147 if (AUTO_INC_DEC)
2148 add_auto_inc_notes (insn, PATTERN (insn));
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 ();