[AArch64][14/14] Reuse target_option_current_node when passing pragma string to targe...
[official-gcc.git] / gcc / lra.c
blob8ced164f6469eb2c20670000eec69ae8b9f6e2b1
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", 100);
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[LAST_INSN_CODE];
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 int i;
636 for (i = 0; i < LAST_INSN_CODE; i++)
638 if (insn_code_data[i] != NULL)
639 free (insn_code_data[i]);
643 /* Return static insn data, allocate and setup if necessary. Although
644 dup_num is static data (it depends only on icode), to set it up we
645 need to extract insn first. So recog_data should be valid for
646 normal insn (ICODE >= 0) before the call. */
647 static struct lra_static_insn_data *
648 get_static_insn_data (int icode, int nop, int ndup, int nalt)
650 struct lra_static_insn_data *data;
651 size_t n_bytes;
653 lra_assert (icode < LAST_INSN_CODE);
654 if (icode >= 0 && (data = insn_code_data[icode]) != NULL)
655 return data;
656 lra_assert (nop >= 0 && ndup >= 0 && nalt >= 0);
657 n_bytes = sizeof (struct lra_static_insn_data)
658 + sizeof (struct lra_operand_data) * nop
659 + sizeof (int) * ndup;
660 data = XNEWVAR (struct lra_static_insn_data, n_bytes);
661 data->operand_alternative = NULL;
662 data->n_operands = nop;
663 data->n_dups = ndup;
664 data->n_alternatives = nalt;
665 data->operand = ((struct lra_operand_data *)
666 ((char *) data + sizeof (struct lra_static_insn_data)));
667 data->dup_num = ((int *) ((char *) data->operand
668 + sizeof (struct lra_operand_data) * nop));
669 if (icode >= 0)
671 int i;
673 insn_code_data[icode] = data;
674 for (i = 0; i < nop; i++)
676 data->operand[i].constraint
677 = insn_data[icode].operand[i].constraint;
678 data->operand[i].mode = insn_data[icode].operand[i].mode;
679 data->operand[i].strict_low = insn_data[icode].operand[i].strict_low;
680 data->operand[i].is_operator
681 = insn_data[icode].operand[i].is_operator;
682 data->operand[i].type
683 = (data->operand[i].constraint[0] == '=' ? OP_OUT
684 : data->operand[i].constraint[0] == '+' ? OP_INOUT
685 : OP_IN);
686 data->operand[i].is_address = false;
688 for (i = 0; i < ndup; i++)
689 data->dup_num[i] = recog_data.dup_num[i];
691 return data;
694 /* The current length of the following array. */
695 int lra_insn_recog_data_len;
697 /* Map INSN_UID -> the insn recog data (NULL if unknown). */
698 lra_insn_recog_data_t *lra_insn_recog_data;
700 /* Initialize LRA data about insns. */
701 static void
702 init_insn_recog_data (void)
704 lra_insn_recog_data_len = 0;
705 lra_insn_recog_data = NULL;
708 /* Expand, if necessary, LRA data about insns. */
709 static void
710 check_and_expand_insn_recog_data (int index)
712 int i, old;
714 if (lra_insn_recog_data_len > index)
715 return;
716 old = lra_insn_recog_data_len;
717 lra_insn_recog_data_len = index * 3 / 2 + 1;
718 lra_insn_recog_data = XRESIZEVEC (lra_insn_recog_data_t,
719 lra_insn_recog_data,
720 lra_insn_recog_data_len);
721 for (i = old; i < lra_insn_recog_data_len; i++)
722 lra_insn_recog_data[i] = NULL;
725 /* Finish LRA DATA about insn. */
726 static void
727 free_insn_recog_data (lra_insn_recog_data_t data)
729 if (data->operand_loc != NULL)
730 free (data->operand_loc);
731 if (data->dup_loc != NULL)
732 free (data->dup_loc);
733 if (data->arg_hard_regs != NULL)
734 free (data->arg_hard_regs);
735 if (data->icode < 0 && NONDEBUG_INSN_P (data->insn))
737 if (data->insn_static_data->operand_alternative != NULL)
738 free (const_cast <operand_alternative *>
739 (data->insn_static_data->operand_alternative));
740 free_insn_regs (data->insn_static_data->hard_regs);
741 free (data->insn_static_data);
743 free_insn_regs (data->regs);
744 data->regs = NULL;
745 free (data);
748 /* Pools for copies. */
749 static object_allocator<lra_copy> lra_copy_pool ("lra copies", 100);
751 /* Finish LRA data about all insns. */
752 static void
753 finish_insn_recog_data (void)
755 int i;
756 lra_insn_recog_data_t data;
758 for (i = 0; i < lra_insn_recog_data_len; i++)
759 if ((data = lra_insn_recog_data[i]) != NULL)
760 free_insn_recog_data (data);
761 finish_insn_regs ();
762 lra_copy_pool.release ();
763 lra_insn_reg_pool.release ();
764 free (lra_insn_recog_data);
767 /* Setup info about operands in alternatives of LRA DATA of insn. */
768 static void
769 setup_operand_alternative (lra_insn_recog_data_t data,
770 const operand_alternative *op_alt)
772 int i, j, nop, nalt;
773 int icode = data->icode;
774 struct lra_static_insn_data *static_data = data->insn_static_data;
776 static_data->commutative = -1;
777 nop = static_data->n_operands;
778 nalt = static_data->n_alternatives;
779 static_data->operand_alternative = op_alt;
780 for (i = 0; i < nop; i++)
782 static_data->operand[i].early_clobber = false;
783 static_data->operand[i].is_address = false;
784 if (static_data->operand[i].constraint[0] == '%')
786 /* We currently only support one commutative pair of operands. */
787 if (static_data->commutative < 0)
788 static_data->commutative = i;
789 else
790 lra_assert (icode < 0); /* Asm */
791 /* The last operand should not be marked commutative. */
792 lra_assert (i != nop - 1);
795 for (j = 0; j < nalt; j++)
796 for (i = 0; i < nop; i++, op_alt++)
798 static_data->operand[i].early_clobber |= op_alt->earlyclobber;
799 static_data->operand[i].is_address |= op_alt->is_address;
803 /* Recursively process X and collect info about registers, which are
804 not the insn operands, in X with TYPE (in/out/inout) and flag that
805 it is early clobbered in the insn (EARLY_CLOBBER) and add the info
806 to LIST. X is a part of insn given by DATA. Return the result
807 list. */
808 static struct lra_insn_reg *
809 collect_non_operand_hard_regs (rtx *x, lra_insn_recog_data_t data,
810 struct lra_insn_reg *list,
811 enum op_type type, bool early_clobber)
813 int i, j, regno, last;
814 bool subreg_p;
815 machine_mode mode;
816 struct lra_insn_reg *curr;
817 rtx op = *x;
818 enum rtx_code code = GET_CODE (op);
819 const char *fmt = GET_RTX_FORMAT (code);
821 for (i = 0; i < data->insn_static_data->n_operands; i++)
822 if (x == data->operand_loc[i])
823 /* It is an operand loc. Stop here. */
824 return list;
825 for (i = 0; i < data->insn_static_data->n_dups; i++)
826 if (x == data->dup_loc[i])
827 /* It is a dup loc. Stop here. */
828 return list;
829 mode = GET_MODE (op);
830 subreg_p = false;
831 if (code == SUBREG)
833 op = SUBREG_REG (op);
834 code = GET_CODE (op);
835 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (op)))
837 mode = GET_MODE (op);
838 if (GET_MODE_SIZE (mode) > REGMODE_NATURAL_SIZE (mode))
839 subreg_p = true;
842 if (REG_P (op))
844 if ((regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER)
845 return list;
846 /* Process all regs even unallocatable ones as we need info
847 about all regs for rematerialization pass. */
848 for (last = regno + hard_regno_nregs[regno][mode];
849 regno < last;
850 regno++)
852 for (curr = list; curr != NULL; curr = curr->next)
853 if (curr->regno == regno && curr->subreg_p == subreg_p
854 && curr->biggest_mode == mode)
856 if (curr->type != type)
857 curr->type = OP_INOUT;
858 if (curr->early_clobber != early_clobber)
859 curr->early_clobber = true;
860 break;
862 if (curr == NULL)
864 /* This is a new hard regno or the info can not be
865 integrated into the found structure. */
866 #ifdef STACK_REGS
867 early_clobber
868 = (early_clobber
869 /* This clobber is to inform popping floating
870 point stack only. */
871 && ! (FIRST_STACK_REG <= regno
872 && regno <= LAST_STACK_REG));
873 #endif
874 list = new_insn_reg (data->insn, regno, type, mode, subreg_p,
875 early_clobber, list);
878 return list;
880 switch (code)
882 case SET:
883 list = collect_non_operand_hard_regs (&SET_DEST (op), data,
884 list, OP_OUT, false);
885 list = collect_non_operand_hard_regs (&SET_SRC (op), data,
886 list, OP_IN, false);
887 break;
888 case CLOBBER:
889 /* We treat clobber of non-operand hard registers as early
890 clobber (the behavior is expected from asm). */
891 list = collect_non_operand_hard_regs (&XEXP (op, 0), data,
892 list, OP_OUT, true);
893 break;
894 case PRE_INC: case PRE_DEC: case POST_INC: case POST_DEC:
895 list = collect_non_operand_hard_regs (&XEXP (op, 0), data,
896 list, OP_INOUT, false);
897 break;
898 case PRE_MODIFY: case POST_MODIFY:
899 list = collect_non_operand_hard_regs (&XEXP (op, 0), data,
900 list, OP_INOUT, false);
901 list = collect_non_operand_hard_regs (&XEXP (op, 1), data,
902 list, OP_IN, false);
903 break;
904 default:
905 fmt = GET_RTX_FORMAT (code);
906 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
908 if (fmt[i] == 'e')
909 list = collect_non_operand_hard_regs (&XEXP (op, i), data,
910 list, OP_IN, false);
911 else if (fmt[i] == 'E')
912 for (j = XVECLEN (op, i) - 1; j >= 0; j--)
913 list = collect_non_operand_hard_regs (&XVECEXP (op, i, j), data,
914 list, OP_IN, false);
917 return list;
920 /* Set up and return info about INSN. Set up the info if it is not set up
921 yet. */
922 lra_insn_recog_data_t
923 lra_set_insn_recog_data (rtx_insn *insn)
925 lra_insn_recog_data_t data;
926 int i, n, icode;
927 rtx **locs;
928 unsigned int uid = INSN_UID (insn);
929 struct lra_static_insn_data *insn_static_data;
931 check_and_expand_insn_recog_data (uid);
932 if (DEBUG_INSN_P (insn))
933 icode = -1;
934 else
936 icode = INSN_CODE (insn);
937 if (icode < 0)
938 /* It might be a new simple insn which is not recognized yet. */
939 INSN_CODE (insn) = icode = recog_memoized (insn);
941 data = XNEW (struct lra_insn_recog_data);
942 lra_insn_recog_data[uid] = data;
943 data->insn = insn;
944 data->used_insn_alternative = -1;
945 data->icode = icode;
946 data->regs = NULL;
947 if (DEBUG_INSN_P (insn))
949 data->insn_static_data = &debug_insn_static_data;
950 data->dup_loc = NULL;
951 data->arg_hard_regs = NULL;
952 data->preferred_alternatives = ALL_ALTERNATIVES;
953 data->operand_loc = XNEWVEC (rtx *, 1);
954 data->operand_loc[0] = &INSN_VAR_LOCATION_LOC (insn);
955 return data;
957 if (icode < 0)
959 int nop, nalt;
960 machine_mode operand_mode[MAX_RECOG_OPERANDS];
961 const char *constraints[MAX_RECOG_OPERANDS];
963 nop = asm_noperands (PATTERN (insn));
964 data->operand_loc = data->dup_loc = NULL;
965 nalt = 1;
966 if (nop < 0)
968 /* It is a special insn like USE or CLOBBER. We should
969 recognize any regular insn otherwise LRA can do nothing
970 with this insn. */
971 gcc_assert (GET_CODE (PATTERN (insn)) == USE
972 || GET_CODE (PATTERN (insn)) == CLOBBER
973 || GET_CODE (PATTERN (insn)) == ASM_INPUT);
974 data->insn_static_data = insn_static_data
975 = get_static_insn_data (-1, 0, 0, nalt);
977 else
979 /* expand_asm_operands makes sure there aren't too many
980 operands. */
981 lra_assert (nop <= MAX_RECOG_OPERANDS);
982 if (nop != 0)
983 data->operand_loc = XNEWVEC (rtx *, nop);
984 /* Now get the operand values and constraints out of the
985 insn. */
986 decode_asm_operands (PATTERN (insn), NULL,
987 data->operand_loc,
988 constraints, operand_mode, NULL);
989 if (nop > 0)
991 const char *p = recog_data.constraints[0];
993 for (p = constraints[0]; *p; p++)
994 nalt += *p == ',';
996 data->insn_static_data = insn_static_data
997 = get_static_insn_data (-1, nop, 0, nalt);
998 for (i = 0; i < nop; i++)
1000 insn_static_data->operand[i].mode = operand_mode[i];
1001 insn_static_data->operand[i].constraint = constraints[i];
1002 insn_static_data->operand[i].strict_low = false;
1003 insn_static_data->operand[i].is_operator = false;
1004 insn_static_data->operand[i].is_address = false;
1007 for (i = 0; i < insn_static_data->n_operands; i++)
1008 insn_static_data->operand[i].type
1009 = (insn_static_data->operand[i].constraint[0] == '=' ? OP_OUT
1010 : insn_static_data->operand[i].constraint[0] == '+' ? OP_INOUT
1011 : OP_IN);
1012 data->preferred_alternatives = ALL_ALTERNATIVES;
1013 if (nop > 0)
1015 operand_alternative *op_alt = XCNEWVEC (operand_alternative,
1016 nalt * nop);
1017 preprocess_constraints (nop, nalt, constraints, op_alt);
1018 setup_operand_alternative (data, op_alt);
1021 else
1023 insn_extract (insn);
1024 data->insn_static_data = insn_static_data
1025 = get_static_insn_data (icode, insn_data[icode].n_operands,
1026 insn_data[icode].n_dups,
1027 insn_data[icode].n_alternatives);
1028 n = insn_static_data->n_operands;
1029 if (n == 0)
1030 locs = NULL;
1031 else
1033 locs = XNEWVEC (rtx *, n);
1034 memcpy (locs, recog_data.operand_loc, n * sizeof (rtx *));
1036 data->operand_loc = locs;
1037 n = insn_static_data->n_dups;
1038 if (n == 0)
1039 locs = NULL;
1040 else
1042 locs = XNEWVEC (rtx *, n);
1043 memcpy (locs, recog_data.dup_loc, n * sizeof (rtx *));
1045 data->dup_loc = locs;
1046 data->preferred_alternatives = get_preferred_alternatives (insn);
1047 const operand_alternative *op_alt = preprocess_insn_constraints (icode);
1048 if (!insn_static_data->operand_alternative)
1049 setup_operand_alternative (data, op_alt);
1050 else if (op_alt != insn_static_data->operand_alternative)
1051 insn_static_data->operand_alternative = op_alt;
1053 if (GET_CODE (PATTERN (insn)) == CLOBBER || GET_CODE (PATTERN (insn)) == USE)
1054 insn_static_data->hard_regs = NULL;
1055 else
1056 insn_static_data->hard_regs
1057 = collect_non_operand_hard_regs (&PATTERN (insn), data,
1058 NULL, OP_IN, false);
1059 data->arg_hard_regs = NULL;
1060 if (CALL_P (insn))
1062 bool use_p;
1063 rtx link;
1064 int n_hard_regs, regno, arg_hard_regs[FIRST_PSEUDO_REGISTER];
1066 n_hard_regs = 0;
1067 /* Finding implicit hard register usage. We believe it will be
1068 not changed whatever transformations are used. Call insns
1069 are such example. */
1070 for (link = CALL_INSN_FUNCTION_USAGE (insn);
1071 link != NULL_RTX;
1072 link = XEXP (link, 1))
1073 if (((use_p = GET_CODE (XEXP (link, 0)) == USE)
1074 || GET_CODE (XEXP (link, 0)) == CLOBBER)
1075 && REG_P (XEXP (XEXP (link, 0), 0)))
1077 regno = REGNO (XEXP (XEXP (link, 0), 0));
1078 lra_assert (regno < FIRST_PSEUDO_REGISTER);
1079 /* It is an argument register. */
1080 for (i = REG_NREGS (XEXP (XEXP (link, 0), 0)) - 1; i >= 0; i--)
1081 arg_hard_regs[n_hard_regs++]
1082 = regno + i + (use_p ? 0 : FIRST_PSEUDO_REGISTER);
1084 if (n_hard_regs != 0)
1086 arg_hard_regs[n_hard_regs++] = -1;
1087 data->arg_hard_regs = XNEWVEC (int, n_hard_regs);
1088 memcpy (data->arg_hard_regs, arg_hard_regs,
1089 sizeof (int) * n_hard_regs);
1092 /* Some output operand can be recognized only from the context not
1093 from the constraints which are empty in this case. Call insn may
1094 contain a hard register in set destination with empty constraint
1095 and extract_insn treats them as an input. */
1096 for (i = 0; i < insn_static_data->n_operands; i++)
1098 int j;
1099 rtx pat, set;
1100 struct lra_operand_data *operand = &insn_static_data->operand[i];
1102 /* ??? Should we treat 'X' the same way. It looks to me that
1103 'X' means anything and empty constraint means we do not
1104 care. */
1105 if (operand->type != OP_IN || *operand->constraint != '\0'
1106 || operand->is_operator)
1107 continue;
1108 pat = PATTERN (insn);
1109 if (GET_CODE (pat) == SET)
1111 if (data->operand_loc[i] != &SET_DEST (pat))
1112 continue;
1114 else if (GET_CODE (pat) == PARALLEL)
1116 for (j = XVECLEN (pat, 0) - 1; j >= 0; j--)
1118 set = XVECEXP (PATTERN (insn), 0, j);
1119 if (GET_CODE (set) == SET
1120 && &SET_DEST (set) == data->operand_loc[i])
1121 break;
1123 if (j < 0)
1124 continue;
1126 else
1127 continue;
1128 operand->type = OP_OUT;
1130 return data;
1133 /* Return info about insn give by UID. The info should be already set
1134 up. */
1135 static lra_insn_recog_data_t
1136 get_insn_recog_data_by_uid (int uid)
1138 lra_insn_recog_data_t data;
1140 data = lra_insn_recog_data[uid];
1141 lra_assert (data != NULL);
1142 return data;
1145 /* Invalidate all info about insn given by its UID. */
1146 static void
1147 invalidate_insn_recog_data (int uid)
1149 lra_insn_recog_data_t data;
1151 data = lra_insn_recog_data[uid];
1152 lra_assert (data != NULL);
1153 free_insn_recog_data (data);
1154 lra_insn_recog_data[uid] = NULL;
1157 /* Update all the insn info about INSN. It is usually called when
1158 something in the insn was changed. Return the updated info. */
1159 lra_insn_recog_data_t
1160 lra_update_insn_recog_data (rtx_insn *insn)
1162 lra_insn_recog_data_t data;
1163 int n;
1164 unsigned int uid = INSN_UID (insn);
1165 struct lra_static_insn_data *insn_static_data;
1166 HOST_WIDE_INT sp_offset = 0;
1168 check_and_expand_insn_recog_data (uid);
1169 if ((data = lra_insn_recog_data[uid]) != NULL
1170 && data->icode != INSN_CODE (insn))
1172 sp_offset = data->sp_offset;
1173 invalidate_insn_data_regno_info (data, insn, get_insn_freq (insn));
1174 invalidate_insn_recog_data (uid);
1175 data = NULL;
1177 if (data == NULL)
1179 data = lra_get_insn_recog_data (insn);
1180 /* Initiate or restore SP offset. */
1181 data->sp_offset = sp_offset;
1182 return data;
1184 insn_static_data = data->insn_static_data;
1185 data->used_insn_alternative = -1;
1186 if (DEBUG_INSN_P (insn))
1187 return data;
1188 if (data->icode < 0)
1190 int nop;
1191 machine_mode operand_mode[MAX_RECOG_OPERANDS];
1192 const char *constraints[MAX_RECOG_OPERANDS];
1194 nop = asm_noperands (PATTERN (insn));
1195 if (nop >= 0)
1197 lra_assert (nop == data->insn_static_data->n_operands);
1198 /* Now get the operand values and constraints out of the
1199 insn. */
1200 decode_asm_operands (PATTERN (insn), NULL,
1201 data->operand_loc,
1202 constraints, operand_mode, NULL);
1203 #ifdef ENABLE_CHECKING
1205 int i;
1207 for (i = 0; i < nop; i++)
1208 lra_assert
1209 (insn_static_data->operand[i].mode == operand_mode[i]
1210 && insn_static_data->operand[i].constraint == constraints[i]
1211 && ! insn_static_data->operand[i].is_operator);
1213 #endif
1215 #ifdef ENABLE_CHECKING
1217 int i;
1219 for (i = 0; i < insn_static_data->n_operands; i++)
1220 lra_assert
1221 (insn_static_data->operand[i].type
1222 == (insn_static_data->operand[i].constraint[0] == '=' ? OP_OUT
1223 : insn_static_data->operand[i].constraint[0] == '+' ? OP_INOUT
1224 : OP_IN));
1226 #endif
1228 else
1230 insn_extract (insn);
1231 n = insn_static_data->n_operands;
1232 if (n != 0)
1233 memcpy (data->operand_loc, recog_data.operand_loc, n * sizeof (rtx *));
1234 n = insn_static_data->n_dups;
1235 if (n != 0)
1236 memcpy (data->dup_loc, recog_data.dup_loc, n * sizeof (rtx *));
1237 lra_assert (check_bool_attrs (insn));
1239 return data;
1242 /* Set up that INSN is using alternative ALT now. */
1243 void
1244 lra_set_used_insn_alternative (rtx_insn *insn, int alt)
1246 lra_insn_recog_data_t data;
1248 data = lra_get_insn_recog_data (insn);
1249 data->used_insn_alternative = alt;
1252 /* Set up that insn with UID is using alternative ALT now. The insn
1253 info should be already set up. */
1254 void
1255 lra_set_used_insn_alternative_by_uid (int uid, int alt)
1257 lra_insn_recog_data_t data;
1259 check_and_expand_insn_recog_data (uid);
1260 data = lra_insn_recog_data[uid];
1261 lra_assert (data != NULL);
1262 data->used_insn_alternative = alt;
1267 /* This page contains code dealing with common register info and
1268 pseudo copies. */
1270 /* The size of the following array. */
1271 static int reg_info_size;
1272 /* Common info about each register. */
1273 struct lra_reg *lra_reg_info;
1275 /* Last register value. */
1276 static int last_reg_value;
1278 /* Return new register value. */
1279 static int
1280 get_new_reg_value (void)
1282 return ++last_reg_value;
1285 /* Vec referring to pseudo copies. */
1286 static vec<lra_copy_t> copy_vec;
1288 /* Initialize I-th element of lra_reg_info. */
1289 static inline void
1290 initialize_lra_reg_info_element (int i)
1292 bitmap_initialize (&lra_reg_info[i].insn_bitmap, &reg_obstack);
1293 #ifdef STACK_REGS
1294 lra_reg_info[i].no_stack_p = false;
1295 #endif
1296 CLEAR_HARD_REG_SET (lra_reg_info[i].conflict_hard_regs);
1297 CLEAR_HARD_REG_SET (lra_reg_info[i].actual_call_used_reg_set);
1298 lra_reg_info[i].preferred_hard_regno1 = -1;
1299 lra_reg_info[i].preferred_hard_regno2 = -1;
1300 lra_reg_info[i].preferred_hard_regno_profit1 = 0;
1301 lra_reg_info[i].preferred_hard_regno_profit2 = 0;
1302 lra_reg_info[i].biggest_mode = VOIDmode;
1303 lra_reg_info[i].live_ranges = NULL;
1304 lra_reg_info[i].nrefs = lra_reg_info[i].freq = 0;
1305 lra_reg_info[i].last_reload = 0;
1306 lra_reg_info[i].restore_regno = -1;
1307 lra_reg_info[i].val = get_new_reg_value ();
1308 lra_reg_info[i].offset = 0;
1309 lra_reg_info[i].copies = NULL;
1312 /* Initialize common reg info and copies. */
1313 static void
1314 init_reg_info (void)
1316 int i;
1318 last_reg_value = 0;
1319 reg_info_size = max_reg_num () * 3 / 2 + 1;
1320 lra_reg_info = XNEWVEC (struct lra_reg, reg_info_size);
1321 for (i = 0; i < reg_info_size; i++)
1322 initialize_lra_reg_info_element (i);
1323 copy_vec.create (100);
1327 /* Finish common reg info and copies. */
1328 static void
1329 finish_reg_info (void)
1331 int i;
1333 for (i = 0; i < reg_info_size; i++)
1334 bitmap_clear (&lra_reg_info[i].insn_bitmap);
1335 free (lra_reg_info);
1336 reg_info_size = 0;
1339 /* Expand common reg info if it is necessary. */
1340 static void
1341 expand_reg_info (void)
1343 int i, old = reg_info_size;
1345 if (reg_info_size > max_reg_num ())
1346 return;
1347 reg_info_size = max_reg_num () * 3 / 2 + 1;
1348 lra_reg_info = XRESIZEVEC (struct lra_reg, lra_reg_info, reg_info_size);
1349 for (i = old; i < reg_info_size; i++)
1350 initialize_lra_reg_info_element (i);
1353 /* Free all copies. */
1354 void
1355 lra_free_copies (void)
1357 lra_copy_t cp;
1359 while (copy_vec.length () != 0)
1361 cp = copy_vec.pop ();
1362 lra_reg_info[cp->regno1].copies = lra_reg_info[cp->regno2].copies = NULL;
1363 lra_copy_pool.remove (cp);
1367 /* Create copy of two pseudos REGNO1 and REGNO2. The copy execution
1368 frequency is FREQ. */
1369 void
1370 lra_create_copy (int regno1, int regno2, int freq)
1372 bool regno1_dest_p;
1373 lra_copy_t cp;
1375 lra_assert (regno1 != regno2);
1376 regno1_dest_p = true;
1377 if (regno1 > regno2)
1379 std::swap (regno1, regno2);
1380 regno1_dest_p = false;
1382 cp = lra_copy_pool.allocate ();
1383 copy_vec.safe_push (cp);
1384 cp->regno1_dest_p = regno1_dest_p;
1385 cp->freq = freq;
1386 cp->regno1 = regno1;
1387 cp->regno2 = regno2;
1388 cp->regno1_next = lra_reg_info[regno1].copies;
1389 lra_reg_info[regno1].copies = cp;
1390 cp->regno2_next = lra_reg_info[regno2].copies;
1391 lra_reg_info[regno2].copies = cp;
1392 if (lra_dump_file != NULL)
1393 fprintf (lra_dump_file, " Creating copy r%d%sr%d@%d\n",
1394 regno1, regno1_dest_p ? "<-" : "->", regno2, freq);
1397 /* Return N-th (0, 1, ...) copy. If there is no copy, return
1398 NULL. */
1399 lra_copy_t
1400 lra_get_copy (int n)
1402 if (n >= (int) copy_vec.length ())
1403 return NULL;
1404 return copy_vec[n];
1409 /* This page contains code dealing with info about registers in
1410 insns. */
1412 /* Process X of insn UID recursively and add info (operand type is
1413 given by TYPE, flag of that it is early clobber is EARLY_CLOBBER)
1414 about registers in X to the insn DATA. */
1415 static void
1416 add_regs_to_insn_regno_info (lra_insn_recog_data_t data, rtx x, int uid,
1417 enum op_type type, bool early_clobber)
1419 int i, j, regno;
1420 bool subreg_p;
1421 machine_mode mode;
1422 const char *fmt;
1423 enum rtx_code code;
1424 struct lra_insn_reg *curr;
1426 code = GET_CODE (x);
1427 mode = GET_MODE (x);
1428 subreg_p = false;
1429 if (GET_CODE (x) == SUBREG)
1431 x = SUBREG_REG (x);
1432 code = GET_CODE (x);
1433 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (x)))
1435 mode = GET_MODE (x);
1436 if (GET_MODE_SIZE (mode) > REGMODE_NATURAL_SIZE (mode))
1437 subreg_p = true;
1440 if (REG_P (x))
1442 regno = REGNO (x);
1443 /* Process all regs even unallocatable ones as we need info about
1444 all regs for rematerialization pass. */
1445 expand_reg_info ();
1446 if (bitmap_set_bit (&lra_reg_info[regno].insn_bitmap, uid))
1448 data->regs = new_insn_reg (data->insn, regno, type, mode, subreg_p,
1449 early_clobber, data->regs);
1450 return;
1452 else
1454 for (curr = data->regs; curr != NULL; curr = curr->next)
1455 if (curr->regno == regno)
1457 if (curr->subreg_p != subreg_p || curr->biggest_mode != mode)
1458 /* The info can not be integrated into the found
1459 structure. */
1460 data->regs = new_insn_reg (data->insn, regno, type, mode,
1461 subreg_p, early_clobber,
1462 data->regs);
1463 else
1465 if (curr->type != type)
1466 curr->type = OP_INOUT;
1467 if (curr->early_clobber != early_clobber)
1468 curr->early_clobber = true;
1470 return;
1472 gcc_unreachable ();
1476 switch (code)
1478 case SET:
1479 add_regs_to_insn_regno_info (data, SET_DEST (x), uid, OP_OUT, false);
1480 add_regs_to_insn_regno_info (data, SET_SRC (x), uid, OP_IN, false);
1481 break;
1482 case CLOBBER:
1483 /* We treat clobber of non-operand hard registers as early
1484 clobber (the behavior is expected from asm). */
1485 add_regs_to_insn_regno_info (data, XEXP (x, 0), uid, OP_OUT, true);
1486 break;
1487 case PRE_INC: case PRE_DEC: case POST_INC: case POST_DEC:
1488 add_regs_to_insn_regno_info (data, XEXP (x, 0), uid, OP_INOUT, false);
1489 break;
1490 case PRE_MODIFY: case POST_MODIFY:
1491 add_regs_to_insn_regno_info (data, XEXP (x, 0), uid, OP_INOUT, false);
1492 add_regs_to_insn_regno_info (data, XEXP (x, 1), uid, OP_IN, false);
1493 break;
1494 default:
1495 if ((code != PARALLEL && code != EXPR_LIST) || type != OP_OUT)
1496 /* Some targets place small structures in registers for return
1497 values of functions, and those registers are wrapped in
1498 PARALLEL that we may see as the destination of a SET. Here
1499 is an example:
1501 (call_insn 13 12 14 2 (set (parallel:BLK [
1502 (expr_list:REG_DEP_TRUE (reg:DI 0 ax)
1503 (const_int 0 [0]))
1504 (expr_list:REG_DEP_TRUE (reg:DI 1 dx)
1505 (const_int 8 [0x8]))
1507 (call (mem:QI (symbol_ref:DI (... */
1508 type = OP_IN;
1509 fmt = GET_RTX_FORMAT (code);
1510 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1512 if (fmt[i] == 'e')
1513 add_regs_to_insn_regno_info (data, XEXP (x, i), uid, type, false);
1514 else if (fmt[i] == 'E')
1516 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1517 add_regs_to_insn_regno_info (data, XVECEXP (x, i, j), uid,
1518 type, false);
1524 /* Return execution frequency of INSN. */
1525 static int
1526 get_insn_freq (rtx_insn *insn)
1528 basic_block bb = BLOCK_FOR_INSN (insn);
1530 gcc_checking_assert (bb != NULL);
1531 return REG_FREQ_FROM_BB (bb);
1534 /* Invalidate all reg info of INSN with DATA and execution frequency
1535 FREQ. Update common info about the invalidated registers. */
1536 static void
1537 invalidate_insn_data_regno_info (lra_insn_recog_data_t data, rtx_insn *insn,
1538 int freq)
1540 int uid;
1541 bool debug_p;
1542 unsigned int i;
1543 struct lra_insn_reg *ir, *next_ir;
1545 uid = INSN_UID (insn);
1546 debug_p = DEBUG_INSN_P (insn);
1547 for (ir = data->regs; ir != NULL; ir = next_ir)
1549 i = ir->regno;
1550 next_ir = ir->next;
1551 lra_insn_reg_pool.remove (ir);
1552 bitmap_clear_bit (&lra_reg_info[i].insn_bitmap, uid);
1553 if (i >= FIRST_PSEUDO_REGISTER && ! debug_p)
1555 lra_reg_info[i].nrefs--;
1556 lra_reg_info[i].freq -= freq;
1557 lra_assert (lra_reg_info[i].nrefs >= 0 && lra_reg_info[i].freq >= 0);
1560 data->regs = NULL;
1563 /* Invalidate all reg info of INSN. Update common info about the
1564 invalidated registers. */
1565 void
1566 lra_invalidate_insn_regno_info (rtx_insn *insn)
1568 invalidate_insn_data_regno_info (lra_get_insn_recog_data (insn), insn,
1569 get_insn_freq (insn));
1572 /* Update common reg info from reg info of insn given by its DATA and
1573 execution frequency FREQ. */
1574 static void
1575 setup_insn_reg_info (lra_insn_recog_data_t data, int freq)
1577 unsigned int i;
1578 struct lra_insn_reg *ir;
1580 for (ir = data->regs; ir != NULL; ir = ir->next)
1581 if ((i = ir->regno) >= FIRST_PSEUDO_REGISTER)
1583 lra_reg_info[i].nrefs++;
1584 lra_reg_info[i].freq += freq;
1588 /* Set up insn reg info of INSN. Update common reg info from reg info
1589 of INSN. */
1590 void
1591 lra_update_insn_regno_info (rtx_insn *insn)
1593 int i, uid, freq;
1594 lra_insn_recog_data_t data;
1595 struct lra_static_insn_data *static_data;
1596 enum rtx_code code;
1597 rtx link;
1599 if (! INSN_P (insn))
1600 return;
1601 data = lra_get_insn_recog_data (insn);
1602 static_data = data->insn_static_data;
1603 freq = get_insn_freq (insn);
1604 invalidate_insn_data_regno_info (data, insn, freq);
1605 uid = INSN_UID (insn);
1606 for (i = static_data->n_operands - 1; i >= 0; i--)
1607 add_regs_to_insn_regno_info (data, *data->operand_loc[i], uid,
1608 static_data->operand[i].type,
1609 static_data->operand[i].early_clobber);
1610 if ((code = GET_CODE (PATTERN (insn))) == CLOBBER || code == USE)
1611 add_regs_to_insn_regno_info (data, XEXP (PATTERN (insn), 0), uid,
1612 code == USE ? OP_IN : OP_OUT, false);
1613 if (CALL_P (insn))
1614 /* On some targets call insns can refer to pseudos in memory in
1615 CALL_INSN_FUNCTION_USAGE list. Process them in order to
1616 consider their occurrences in calls for different
1617 transformations (e.g. inheritance) with given pseudos. */
1618 for (link = CALL_INSN_FUNCTION_USAGE (insn);
1619 link != NULL_RTX;
1620 link = XEXP (link, 1))
1621 if (((code = GET_CODE (XEXP (link, 0))) == USE || code == CLOBBER)
1622 && MEM_P (XEXP (XEXP (link, 0), 0)))
1623 add_regs_to_insn_regno_info (data, XEXP (XEXP (link, 0), 0), uid,
1624 code == USE ? OP_IN : OP_OUT, false);
1625 if (NONDEBUG_INSN_P (insn))
1626 setup_insn_reg_info (data, freq);
1629 /* Return reg info of insn given by it UID. */
1630 struct lra_insn_reg *
1631 lra_get_insn_regs (int uid)
1633 lra_insn_recog_data_t data;
1635 data = get_insn_recog_data_by_uid (uid);
1636 return data->regs;
1641 /* This page contains code dealing with stack of the insns which
1642 should be processed by the next constraint pass. */
1644 /* Bitmap used to put an insn on the stack only in one exemplar. */
1645 static sbitmap lra_constraint_insn_stack_bitmap;
1647 /* The stack itself. */
1648 vec<rtx_insn *> lra_constraint_insn_stack;
1650 /* Put INSN on the stack. If ALWAYS_UPDATE is true, always update the reg
1651 info for INSN, otherwise only update it if INSN is not already on the
1652 stack. */
1653 static inline void
1654 lra_push_insn_1 (rtx_insn *insn, bool always_update)
1656 unsigned int uid = INSN_UID (insn);
1657 if (always_update)
1658 lra_update_insn_regno_info (insn);
1659 if (uid >= SBITMAP_SIZE (lra_constraint_insn_stack_bitmap))
1660 lra_constraint_insn_stack_bitmap =
1661 sbitmap_resize (lra_constraint_insn_stack_bitmap, 3 * uid / 2, 0);
1662 if (bitmap_bit_p (lra_constraint_insn_stack_bitmap, uid))
1663 return;
1664 bitmap_set_bit (lra_constraint_insn_stack_bitmap, uid);
1665 if (! always_update)
1666 lra_update_insn_regno_info (insn);
1667 lra_constraint_insn_stack.safe_push (insn);
1670 /* Put INSN on the stack. */
1671 void
1672 lra_push_insn (rtx_insn *insn)
1674 lra_push_insn_1 (insn, false);
1677 /* Put INSN on the stack and update its reg info. */
1678 void
1679 lra_push_insn_and_update_insn_regno_info (rtx_insn *insn)
1681 lra_push_insn_1 (insn, true);
1684 /* Put insn with UID on the stack. */
1685 void
1686 lra_push_insn_by_uid (unsigned int uid)
1688 lra_push_insn (lra_insn_recog_data[uid]->insn);
1691 /* Take the last-inserted insns off the stack and return it. */
1692 rtx_insn *
1693 lra_pop_insn (void)
1695 rtx_insn *insn = lra_constraint_insn_stack.pop ();
1696 bitmap_clear_bit (lra_constraint_insn_stack_bitmap, INSN_UID (insn));
1697 return insn;
1700 /* Return the current size of the insn stack. */
1701 unsigned int
1702 lra_insn_stack_length (void)
1704 return lra_constraint_insn_stack.length ();
1707 /* Push insns FROM to TO (excluding it) going in reverse order. */
1708 static void
1709 push_insns (rtx_insn *from, rtx_insn *to)
1711 rtx_insn *insn;
1713 if (from == NULL_RTX)
1714 return;
1715 for (insn = from; insn != to; insn = PREV_INSN (insn))
1716 if (INSN_P (insn))
1717 lra_push_insn (insn);
1720 /* Set up sp offset for insn in range [FROM, LAST]. The offset is
1721 taken from the next BB insn after LAST or zero if there in such
1722 insn. */
1723 static void
1724 setup_sp_offset (rtx_insn *from, rtx_insn *last)
1726 rtx_insn *before = next_nonnote_insn_bb (last);
1727 HOST_WIDE_INT offset = (before == NULL_RTX || ! INSN_P (before)
1728 ? 0 : lra_get_insn_recog_data (before)->sp_offset);
1730 for (rtx_insn *insn = from; insn != NEXT_INSN (last); insn = NEXT_INSN (insn))
1731 lra_get_insn_recog_data (insn)->sp_offset = offset;
1734 /* Emit insns BEFORE before INSN and insns AFTER after INSN. Put the
1735 insns onto the stack. Print about emitting the insns with
1736 TITLE. */
1737 void
1738 lra_process_new_insns (rtx_insn *insn, rtx_insn *before, rtx_insn *after,
1739 const char *title)
1741 rtx_insn *last;
1743 if (before == NULL_RTX && after == NULL_RTX)
1744 return;
1745 if (lra_dump_file != NULL)
1747 dump_insn_slim (lra_dump_file, insn);
1748 if (before != NULL_RTX)
1750 fprintf (lra_dump_file," %s before:\n", title);
1751 dump_rtl_slim (lra_dump_file, before, NULL, -1, 0);
1753 if (after != NULL_RTX)
1755 fprintf (lra_dump_file, " %s after:\n", title);
1756 dump_rtl_slim (lra_dump_file, after, NULL, -1, 0);
1758 fprintf (lra_dump_file, "\n");
1760 if (before != NULL_RTX)
1762 emit_insn_before (before, insn);
1763 push_insns (PREV_INSN (insn), PREV_INSN (before));
1764 setup_sp_offset (before, PREV_INSN (insn));
1766 if (after != NULL_RTX)
1768 for (last = after; NEXT_INSN (last) != NULL_RTX; last = NEXT_INSN (last))
1770 emit_insn_after (after, insn);
1771 push_insns (last, insn);
1772 setup_sp_offset (after, last);
1778 /* Replace all references to register OLD_REGNO in *LOC with pseudo
1779 register NEW_REG. Try to simplify subreg of constant if SUBREG_P.
1780 Return true if any change was made. */
1781 bool
1782 lra_substitute_pseudo (rtx *loc, int old_regno, rtx new_reg, bool subreg_p)
1784 rtx x = *loc;
1785 bool result = false;
1786 enum rtx_code code;
1787 const char *fmt;
1788 int i, j;
1790 if (x == NULL_RTX)
1791 return false;
1793 code = GET_CODE (x);
1794 if (code == SUBREG && subreg_p)
1796 rtx subst, inner = SUBREG_REG (x);
1797 /* Transform subreg of constant while we still have inner mode
1798 of the subreg. The subreg internal should not be an insn
1799 operand. */
1800 if (REG_P (inner) && (int) REGNO (inner) == old_regno
1801 && CONSTANT_P (new_reg)
1802 && (subst = simplify_subreg (GET_MODE (x), new_reg, GET_MODE (inner),
1803 SUBREG_BYTE (x))) != NULL_RTX)
1805 *loc = subst;
1806 return true;
1810 else if (code == REG && (int) REGNO (x) == old_regno)
1812 machine_mode mode = GET_MODE (x);
1813 machine_mode inner_mode = GET_MODE (new_reg);
1815 if (mode != inner_mode
1816 && ! (CONST_INT_P (new_reg) && SCALAR_INT_MODE_P (mode)))
1818 if (GET_MODE_SIZE (mode) >= GET_MODE_SIZE (inner_mode)
1819 || ! SCALAR_INT_MODE_P (inner_mode))
1820 new_reg = gen_rtx_SUBREG (mode, new_reg, 0);
1821 else
1822 new_reg = gen_lowpart_SUBREG (mode, new_reg);
1824 *loc = new_reg;
1825 return true;
1828 /* Scan all the operand sub-expressions. */
1829 fmt = GET_RTX_FORMAT (code);
1830 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1832 if (fmt[i] == 'e')
1834 if (lra_substitute_pseudo (&XEXP (x, i), old_regno,
1835 new_reg, subreg_p))
1836 result = true;
1838 else if (fmt[i] == 'E')
1840 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1841 if (lra_substitute_pseudo (&XVECEXP (x, i, j), old_regno,
1842 new_reg, subreg_p))
1843 result = true;
1846 return result;
1849 /* Call lra_substitute_pseudo within an insn. Try to simplify subreg
1850 of constant if SUBREG_P. This won't update the insn ptr, just the
1851 contents of the insn. */
1852 bool
1853 lra_substitute_pseudo_within_insn (rtx_insn *insn, int old_regno,
1854 rtx new_reg, bool subreg_p)
1856 rtx loc = insn;
1857 return lra_substitute_pseudo (&loc, old_regno, new_reg, subreg_p);
1862 /* This page contains code dealing with scratches (changing them onto
1863 pseudos and restoring them from the pseudos).
1865 We change scratches into pseudos at the beginning of LRA to
1866 simplify dealing with them (conflicts, hard register assignments).
1868 If the pseudo denoting scratch was spilled it means that we do need
1869 a hard register for it. Such pseudos are transformed back to
1870 scratches at the end of LRA. */
1872 /* Description of location of a former scratch operand. */
1873 struct sloc
1875 rtx_insn *insn; /* Insn where the scratch was. */
1876 int nop; /* Number of the operand which was a scratch. */
1879 typedef struct sloc *sloc_t;
1881 /* Locations of the former scratches. */
1882 static vec<sloc_t> scratches;
1884 /* Bitmap of scratch regnos. */
1885 static bitmap_head scratch_bitmap;
1887 /* Bitmap of scratch operands. */
1888 static bitmap_head scratch_operand_bitmap;
1890 /* Return true if pseudo REGNO is made of SCRATCH. */
1891 bool
1892 lra_former_scratch_p (int regno)
1894 return bitmap_bit_p (&scratch_bitmap, regno);
1897 /* Return true if the operand NOP of INSN is a former scratch. */
1898 bool
1899 lra_former_scratch_operand_p (rtx_insn *insn, int nop)
1901 return bitmap_bit_p (&scratch_operand_bitmap,
1902 INSN_UID (insn) * MAX_RECOG_OPERANDS + nop) != 0;
1905 /* Register operand NOP in INSN as a former scratch. It will be
1906 changed to scratch back, if it is necessary, at the LRA end. */
1907 void
1908 lra_register_new_scratch_op (rtx_insn *insn, int nop)
1910 lra_insn_recog_data_t id = lra_get_insn_recog_data (insn);
1911 rtx op = *id->operand_loc[nop];
1912 sloc_t loc = XNEW (struct sloc);
1913 lra_assert (REG_P (op));
1914 loc->insn = insn;
1915 loc->nop = nop;
1916 scratches.safe_push (loc);
1917 bitmap_set_bit (&scratch_bitmap, REGNO (op));
1918 bitmap_set_bit (&scratch_operand_bitmap,
1919 INSN_UID (insn) * MAX_RECOG_OPERANDS + nop);
1920 add_reg_note (insn, REG_UNUSED, op);
1923 /* Change scratches onto pseudos and save their location. */
1924 static void
1925 remove_scratches (void)
1927 int i;
1928 bool insn_changed_p;
1929 basic_block bb;
1930 rtx_insn *insn;
1931 rtx reg;
1932 lra_insn_recog_data_t id;
1933 struct lra_static_insn_data *static_id;
1935 scratches.create (get_max_uid ());
1936 bitmap_initialize (&scratch_bitmap, &reg_obstack);
1937 bitmap_initialize (&scratch_operand_bitmap, &reg_obstack);
1938 FOR_EACH_BB_FN (bb, cfun)
1939 FOR_BB_INSNS (bb, insn)
1940 if (INSN_P (insn))
1942 id = lra_get_insn_recog_data (insn);
1943 static_id = id->insn_static_data;
1944 insn_changed_p = false;
1945 for (i = 0; i < static_id->n_operands; i++)
1946 if (GET_CODE (*id->operand_loc[i]) == SCRATCH
1947 && GET_MODE (*id->operand_loc[i]) != VOIDmode)
1949 insn_changed_p = true;
1950 *id->operand_loc[i] = reg
1951 = lra_create_new_reg (static_id->operand[i].mode,
1952 *id->operand_loc[i], ALL_REGS, NULL);
1953 lra_register_new_scratch_op (insn, i);
1954 if (lra_dump_file != NULL)
1955 fprintf (lra_dump_file,
1956 "Removing SCRATCH in insn #%u (nop %d)\n",
1957 INSN_UID (insn), i);
1959 if (insn_changed_p)
1960 /* Because we might use DF right after caller-saves sub-pass
1961 we need to keep DF info up to date. */
1962 df_insn_rescan (insn);
1966 /* Changes pseudos created by function remove_scratches onto scratches. */
1967 static void
1968 restore_scratches (void)
1970 int regno;
1971 unsigned i;
1972 sloc_t loc;
1973 rtx_insn *last = NULL;
1974 lra_insn_recog_data_t id = NULL;
1976 for (i = 0; scratches.iterate (i, &loc); i++)
1978 if (last != loc->insn)
1980 last = loc->insn;
1981 id = lra_get_insn_recog_data (last);
1983 if (REG_P (*id->operand_loc[loc->nop])
1984 && ((regno = REGNO (*id->operand_loc[loc->nop]))
1985 >= FIRST_PSEUDO_REGISTER)
1986 && lra_get_regno_hard_regno (regno) < 0)
1988 /* It should be only case when scratch register with chosen
1989 constraint 'X' did not get memory or hard register. */
1990 lra_assert (lra_former_scratch_p (regno));
1991 *id->operand_loc[loc->nop]
1992 = gen_rtx_SCRATCH (GET_MODE (*id->operand_loc[loc->nop]));
1993 lra_update_dup (id, loc->nop);
1994 if (lra_dump_file != NULL)
1995 fprintf (lra_dump_file, "Restoring SCRATCH in insn #%u(nop %d)\n",
1996 INSN_UID (loc->insn), loc->nop);
1999 for (i = 0; scratches.iterate (i, &loc); i++)
2000 free (loc);
2001 scratches.release ();
2002 bitmap_clear (&scratch_bitmap);
2003 bitmap_clear (&scratch_operand_bitmap);
2008 #ifdef ENABLE_CHECKING
2010 /* Function checks RTL for correctness. If FINAL_P is true, it is
2011 done at the end of LRA and the check is more rigorous. */
2012 static void
2013 check_rtl (bool final_p)
2015 basic_block bb;
2016 rtx_insn *insn;
2018 lra_assert (! final_p || reload_completed);
2019 FOR_EACH_BB_FN (bb, cfun)
2020 FOR_BB_INSNS (bb, insn)
2021 if (NONDEBUG_INSN_P (insn)
2022 && GET_CODE (PATTERN (insn)) != USE
2023 && GET_CODE (PATTERN (insn)) != CLOBBER
2024 && GET_CODE (PATTERN (insn)) != ASM_INPUT)
2026 if (final_p)
2028 #ifdef ENABLED_CHECKING
2029 extract_constrain_insn (insn);
2030 #endif
2031 continue;
2033 /* LRA code is based on assumption that all addresses can be
2034 correctly decomposed. LRA can generate reloads for
2035 decomposable addresses. The decomposition code checks the
2036 correctness of the addresses. So we don't need to check
2037 the addresses here. Don't call insn_invalid_p here, it can
2038 change the code at this stage. */
2039 if (recog_memoized (insn) < 0 && asm_noperands (PATTERN (insn)) < 0)
2040 fatal_insn_not_found (insn);
2043 #endif /* #ifdef ENABLE_CHECKING */
2045 /* Determine if the current function has an exception receiver block
2046 that reaches the exit block via non-exceptional edges */
2047 static bool
2048 has_nonexceptional_receiver (void)
2050 edge e;
2051 edge_iterator ei;
2052 basic_block *tos, *worklist, bb;
2054 /* If we're not optimizing, then just err on the safe side. */
2055 if (!optimize)
2056 return true;
2058 /* First determine which blocks can reach exit via normal paths. */
2059 tos = worklist = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun) + 1);
2061 FOR_EACH_BB_FN (bb, cfun)
2062 bb->flags &= ~BB_REACHABLE;
2064 /* Place the exit block on our worklist. */
2065 EXIT_BLOCK_PTR_FOR_FN (cfun)->flags |= BB_REACHABLE;
2066 *tos++ = EXIT_BLOCK_PTR_FOR_FN (cfun);
2068 /* Iterate: find everything reachable from what we've already seen. */
2069 while (tos != worklist)
2071 bb = *--tos;
2073 FOR_EACH_EDGE (e, ei, bb->preds)
2074 if (e->flags & EDGE_ABNORMAL)
2076 free (worklist);
2077 return true;
2079 else
2081 basic_block src = e->src;
2083 if (!(src->flags & BB_REACHABLE))
2085 src->flags |= BB_REACHABLE;
2086 *tos++ = src;
2090 free (worklist);
2091 /* No exceptional block reached exit unexceptionally. */
2092 return false;
2096 /* Process recursively X of INSN and add REG_INC notes if necessary. */
2097 static void
2098 add_auto_inc_notes (rtx_insn *insn, rtx x)
2100 enum rtx_code code = GET_CODE (x);
2101 const char *fmt;
2102 int i, j;
2104 if (code == MEM && auto_inc_p (XEXP (x, 0)))
2106 add_reg_note (insn, REG_INC, XEXP (XEXP (x, 0), 0));
2107 return;
2110 /* Scan all X sub-expressions. */
2111 fmt = GET_RTX_FORMAT (code);
2112 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2114 if (fmt[i] == 'e')
2115 add_auto_inc_notes (insn, XEXP (x, i));
2116 else if (fmt[i] == 'E')
2117 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2118 add_auto_inc_notes (insn, XVECEXP (x, i, j));
2123 /* Remove all REG_DEAD and REG_UNUSED notes and regenerate REG_INC.
2124 We change pseudos by hard registers without notification of DF and
2125 that can make the notes obsolete. DF-infrastructure does not deal
2126 with REG_INC notes -- so we should regenerate them here. */
2127 static void
2128 update_inc_notes (void)
2130 rtx *pnote;
2131 basic_block bb;
2132 rtx_insn *insn;
2134 FOR_EACH_BB_FN (bb, cfun)
2135 FOR_BB_INSNS (bb, insn)
2136 if (NONDEBUG_INSN_P (insn))
2138 pnote = &REG_NOTES (insn);
2139 while (*pnote != 0)
2141 if (REG_NOTE_KIND (*pnote) == REG_DEAD
2142 || REG_NOTE_KIND (*pnote) == REG_UNUSED
2143 || REG_NOTE_KIND (*pnote) == REG_INC)
2144 *pnote = XEXP (*pnote, 1);
2145 else
2146 pnote = &XEXP (*pnote, 1);
2149 if (AUTO_INC_DEC)
2150 add_auto_inc_notes (insn, PATTERN (insn));
2154 /* Set to 1 while in lra. */
2155 int lra_in_progress;
2157 /* Start of pseudo regnos before the LRA. */
2158 int lra_new_regno_start;
2160 /* Start of reload pseudo regnos before the new spill pass. */
2161 int lra_constraint_new_regno_start;
2163 /* Avoid spilling pseudos with regno more than the following value if
2164 it is possible. */
2165 int lra_bad_spill_regno_start;
2167 /* Inheritance pseudo regnos before the new spill pass. */
2168 bitmap_head lra_inheritance_pseudos;
2170 /* Split regnos before the new spill pass. */
2171 bitmap_head lra_split_regs;
2173 /* Reload pseudo regnos before the new assignmnet pass which still can
2174 be spilled after the assinment pass as memory is also accepted in
2175 insns for the reload pseudos. */
2176 bitmap_head lra_optional_reload_pseudos;
2178 /* Pseudo regnos used for subreg reloads before the new assignment
2179 pass. Such pseudos still can be spilled after the assinment
2180 pass. */
2181 bitmap_head lra_subreg_reload_pseudos;
2183 /* File used for output of LRA debug information. */
2184 FILE *lra_dump_file;
2186 /* True if we should try spill into registers of different classes
2187 instead of memory. */
2188 bool lra_reg_spill_p;
2190 /* Set up value LRA_REG_SPILL_P. */
2191 static void
2192 setup_reg_spill_flag (void)
2194 int cl, mode;
2196 if (targetm.spill_class != NULL)
2197 for (cl = 0; cl < (int) LIM_REG_CLASSES; cl++)
2198 for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
2199 if (targetm.spill_class ((enum reg_class) cl,
2200 (machine_mode) mode) != NO_REGS)
2202 lra_reg_spill_p = true;
2203 return;
2205 lra_reg_spill_p = false;
2208 /* True if the current function is too big to use regular algorithms
2209 in LRA. In other words, we should use simpler and faster algorithms
2210 in LRA. It also means we should not worry about generation code
2211 for caller saves. The value is set up in IRA. */
2212 bool lra_simple_p;
2214 /* Major LRA entry function. F is a file should be used to dump LRA
2215 debug info. */
2216 void
2217 lra (FILE *f)
2219 int i;
2220 bool live_p, scratch_p, inserted_p;
2222 lra_dump_file = f;
2224 timevar_push (TV_LRA);
2226 /* Make sure that the last insn is a note. Some subsequent passes
2227 need it. */
2228 emit_note (NOTE_INSN_DELETED);
2230 COPY_HARD_REG_SET (lra_no_alloc_regs, ira_no_alloc_regs);
2232 init_reg_info ();
2233 expand_reg_info ();
2235 init_insn_recog_data ();
2237 #ifdef ENABLE_CHECKING
2238 /* Some quick check on RTL generated by previous passes. */
2239 check_rtl (false);
2240 #endif
2242 lra_in_progress = 1;
2244 lra_live_range_iter = lra_coalesce_iter = lra_constraint_iter = 0;
2245 lra_assignment_iter = lra_assignment_iter_after_spill = 0;
2246 lra_inheritance_iter = lra_undo_inheritance_iter = 0;
2247 lra_rematerialization_iter = 0;
2249 setup_reg_spill_flag ();
2251 /* Function remove_scratches can creates new pseudos for clobbers --
2252 so set up lra_constraint_new_regno_start before its call to
2253 permit changing reg classes for pseudos created by this
2254 simplification. */
2255 lra_constraint_new_regno_start = lra_new_regno_start = max_reg_num ();
2256 lra_bad_spill_regno_start = INT_MAX;
2257 remove_scratches ();
2258 scratch_p = lra_constraint_new_regno_start != max_reg_num ();
2260 /* A function that has a non-local label that can reach the exit
2261 block via non-exceptional paths must save all call-saved
2262 registers. */
2263 if (cfun->has_nonlocal_label && has_nonexceptional_receiver ())
2264 crtl->saves_all_registers = 1;
2266 if (crtl->saves_all_registers)
2267 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2268 if (! call_used_regs[i] && ! fixed_regs[i] && ! LOCAL_REGNO (i))
2269 df_set_regs_ever_live (i, true);
2271 /* We don't DF from now and avoid its using because it is to
2272 expensive when a lot of RTL changes are made. */
2273 df_set_flags (DF_NO_INSN_RESCAN);
2274 lra_constraint_insn_stack.create (get_max_uid ());
2275 lra_constraint_insn_stack_bitmap = sbitmap_alloc (get_max_uid ());
2276 bitmap_clear (lra_constraint_insn_stack_bitmap);
2277 lra_live_ranges_init ();
2278 lra_constraints_init ();
2279 lra_curr_reload_num = 0;
2280 push_insns (get_last_insn (), NULL);
2281 /* It is needed for the 1st coalescing. */
2282 bitmap_initialize (&lra_inheritance_pseudos, &reg_obstack);
2283 bitmap_initialize (&lra_split_regs, &reg_obstack);
2284 bitmap_initialize (&lra_optional_reload_pseudos, &reg_obstack);
2285 bitmap_initialize (&lra_subreg_reload_pseudos, &reg_obstack);
2286 live_p = false;
2287 if (get_frame_size () != 0 && crtl->stack_alignment_needed)
2288 /* If we have a stack frame, we must align it now. The stack size
2289 may be a part of the offset computation for register
2290 elimination. */
2291 assign_stack_local (BLKmode, 0, crtl->stack_alignment_needed);
2292 lra_init_equiv ();
2293 for (;;)
2295 for (;;)
2297 /* We should try to assign hard registers to scratches even
2298 if there were no RTL transformations in
2299 lra_constraints. */
2300 if (! lra_constraints (lra_constraint_iter == 0)
2301 && (lra_constraint_iter > 1
2302 || (! scratch_p && ! caller_save_needed)))
2303 break;
2304 /* Constraint transformations may result in that eliminable
2305 hard regs become uneliminable and pseudos which use them
2306 should be spilled. It is better to do it before pseudo
2307 assignments.
2309 For example, rs6000 can make
2310 RS6000_PIC_OFFSET_TABLE_REGNUM uneliminable if we started
2311 to use a constant pool. */
2312 lra_eliminate (false, false);
2313 /* Do inheritance only for regular algorithms. */
2314 if (! lra_simple_p)
2316 if (flag_ipa_ra)
2318 if (live_p)
2319 lra_clear_live_ranges ();
2320 /* As a side-effect of lra_create_live_ranges, we calculate
2321 actual_call_used_reg_set, which is needed during
2322 lra_inheritance. */
2323 lra_create_live_ranges (true, true);
2324 live_p = true;
2326 lra_inheritance ();
2328 if (live_p)
2329 lra_clear_live_ranges ();
2330 /* We need live ranges for lra_assign -- so build them. But
2331 don't remove dead insns or change global live info as we
2332 can undo inheritance transformations after inheritance
2333 pseudo assigning. */
2334 lra_create_live_ranges (true, false);
2335 live_p = true;
2336 /* If we don't spill non-reload and non-inheritance pseudos,
2337 there is no sense to run memory-memory move coalescing.
2338 If inheritance pseudos were spilled, the memory-memory
2339 moves involving them will be removed by pass undoing
2340 inheritance. */
2341 if (lra_simple_p)
2342 lra_assign ();
2343 else
2345 bool spill_p = !lra_assign ();
2347 if (lra_undo_inheritance ())
2348 live_p = false;
2349 if (spill_p)
2351 if (! live_p)
2353 lra_create_live_ranges (true, true);
2354 live_p = true;
2356 if (lra_coalesce ())
2357 live_p = false;
2359 if (! live_p)
2360 lra_clear_live_ranges ();
2363 /* Don't clear optional reloads bitmap until all constraints are
2364 satisfied as we need to differ them from regular reloads. */
2365 bitmap_clear (&lra_optional_reload_pseudos);
2366 bitmap_clear (&lra_subreg_reload_pseudos);
2367 bitmap_clear (&lra_inheritance_pseudos);
2368 bitmap_clear (&lra_split_regs);
2369 if (! live_p)
2371 /* We need full live info for spilling pseudos into
2372 registers instead of memory. */
2373 lra_create_live_ranges (lra_reg_spill_p, true);
2374 live_p = true;
2376 /* We should check necessity for spilling here as the above live
2377 range pass can remove spilled pseudos. */
2378 if (! lra_need_for_spills_p ())
2379 break;
2380 /* Now we know what pseudos should be spilled. Try to
2381 rematerialize them first. */
2382 if (lra_remat ())
2384 /* We need full live info -- see the comment above. */
2385 lra_create_live_ranges (lra_reg_spill_p, true);
2386 live_p = true;
2387 if (! lra_need_for_spills_p ())
2388 break;
2390 lra_spill ();
2391 /* Assignment of stack slots changes elimination offsets for
2392 some eliminations. So update the offsets here. */
2393 lra_eliminate (false, false);
2394 lra_constraint_new_regno_start = max_reg_num ();
2395 if (lra_bad_spill_regno_start == INT_MAX
2396 && lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES
2397 && lra_rematerialization_iter > LRA_MAX_REMATERIALIZATION_PASSES)
2398 /* After switching off inheritance and rematerialization
2399 passes, avoid spilling reload pseudos will be created to
2400 prevent LRA cycling in some complicated cases. */
2401 lra_bad_spill_regno_start = lra_constraint_new_regno_start;
2402 lra_assignment_iter_after_spill = 0;
2404 restore_scratches ();
2405 lra_eliminate (true, false);
2406 lra_final_code_change ();
2407 lra_in_progress = 0;
2408 if (live_p)
2409 lra_clear_live_ranges ();
2410 lra_live_ranges_finish ();
2411 lra_constraints_finish ();
2412 finish_reg_info ();
2413 sbitmap_free (lra_constraint_insn_stack_bitmap);
2414 lra_constraint_insn_stack.release ();
2415 finish_insn_recog_data ();
2416 regstat_free_n_sets_and_refs ();
2417 regstat_free_ri ();
2418 reload_completed = 1;
2419 update_inc_notes ();
2421 inserted_p = fixup_abnormal_edges ();
2423 /* We've possibly turned single trapping insn into multiple ones. */
2424 if (cfun->can_throw_non_call_exceptions)
2426 sbitmap blocks;
2427 blocks = sbitmap_alloc (last_basic_block_for_fn (cfun));
2428 bitmap_ones (blocks);
2429 find_many_sub_basic_blocks (blocks);
2430 sbitmap_free (blocks);
2433 if (inserted_p)
2434 commit_edge_insertions ();
2436 /* Replacing pseudos with their memory equivalents might have
2437 created shared rtx. Subsequent passes would get confused
2438 by this, so unshare everything here. */
2439 unshare_all_rtl_again (get_insns ());
2441 #ifdef ENABLE_CHECKING
2442 check_rtl (true);
2443 #endif
2445 timevar_pop (TV_LRA);
2448 /* Called once per compiler to initialize LRA data once. */
2449 void
2450 lra_init_once (void)
2452 init_insn_code_data_once ();
2455 /* Called once per compiler to finish LRA data which are initialize
2456 once. */
2457 void
2458 lra_finish_once (void)
2460 finish_insn_code_data_once ();