PR rtl-optimization/60601
[official-gcc.git] / gcc / lra.c
blob77074e296540da75550ff08b70a74db055bafc07
1 /* LRA (local register allocator) driver and LRA utilities.
2 Copyright (C) 2010-2014 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 Changing spilled pseudos to stack memory or their equivalences;
41 o Allocation stack memory changes the address displacement and
42 new iteration is needed.
44 Here is block diagram of LRA passes:
46 ------------------------
47 --------------- | Undo inheritance for | ---------------
48 | Memory-memory | | spilled pseudos, | | New (and old) |
49 | move coalesce |<---| splits for pseudos got |<-- | pseudos |
50 --------------- | the same hard regs, | | assignment |
51 Start | | and optional reloads | ---------------
52 | | ------------------------ ^
53 V | ---------------- |
54 ----------- V | Update virtual | |
55 | Remove |----> ------------>| register | |
56 | scratches | ^ | displacements | |
57 ----------- | ---------------- |
58 | | |
59 | V New |
60 ---------------- No ------------ pseudos -------------------
61 | Spilled pseudo | change |Constraints:| or insns | Inheritance/split |
62 | to memory |<-------| RTL |--------->| transformations |
63 | substitution | | transfor- | | in EBB scope |
64 ---------------- | mations | -------------------
65 | ------------
67 -------------------------
68 | Hard regs substitution, |
69 | devirtalization, and |------> Finish
70 | restoring scratches got |
71 | memory |
72 -------------------------
74 To speed up the process:
75 o We process only insns affected by changes on previous
76 iterations;
77 o We don't use DFA-infrastructure because it results in much slower
78 compiler speed than a special IR described below does;
79 o We use a special insn representation for quick access to insn
80 info which is always *synchronized* with the current RTL;
81 o Insn IR is minimized by memory. It is divided on three parts:
82 o one specific for each insn in RTL (only operand locations);
83 o one common for all insns in RTL with the same insn code
84 (different operand attributes from machine descriptions);
85 o one oriented for maintenance of live info (list of pseudos).
86 o Pseudo data:
87 o all insns where the pseudo is referenced;
88 o live info (conflicting hard regs, live ranges, # of
89 references etc);
90 o data used for assigning (preferred hard regs, costs etc).
92 This file contains LRA driver, LRA utility functions and data, and
93 code for dealing with scratches. */
95 #include "config.h"
96 #include "system.h"
97 #include "coretypes.h"
98 #include "tm.h"
99 #include "hard-reg-set.h"
100 #include "rtl.h"
101 #include "tm_p.h"
102 #include "regs.h"
103 #include "insn-config.h"
104 #include "insn-codes.h"
105 #include "recog.h"
106 #include "output.h"
107 #include "addresses.h"
108 #include "flags.h"
109 #include "function.h"
110 #include "expr.h"
111 #include "basic-block.h"
112 #include "except.h"
113 #include "tree-pass.h"
114 #include "timevar.h"
115 #include "target.h"
116 #include "vec.h"
117 #include "ira.h"
118 #include "lra-int.h"
119 #include "df.h"
121 /* Hard registers currently not available for allocation. It can
122 changed after some hard registers become not eliminable. */
123 HARD_REG_SET lra_no_alloc_regs;
125 static int get_new_reg_value (void);
126 static void expand_reg_info (void);
127 static void invalidate_insn_recog_data (int);
128 static int get_insn_freq (rtx);
129 static void invalidate_insn_data_regno_info (lra_insn_recog_data_t, rtx, int);
131 /* Expand all regno related info needed for LRA. */
132 static void
133 expand_reg_data (int old)
135 resize_reg_info ();
136 expand_reg_info ();
137 ira_expand_reg_equiv ();
138 for (int i = (int) max_reg_num () - 1; i >= old; i--)
139 lra_change_class (i, ALL_REGS, " Set", true);
142 /* Create and return a new reg of ORIGINAL mode. If ORIGINAL is NULL
143 or of VOIDmode, use MD_MODE for the new reg. Initialize its
144 register class to RCLASS. Print message about assigning class
145 RCLASS containing new register name TITLE unless it is NULL. Use
146 attributes of ORIGINAL if it is a register. The created register
147 will have unique held value. */
149 lra_create_new_reg_with_unique_value (enum machine_mode md_mode, rtx original,
150 enum reg_class rclass, const char *title)
152 enum machine_mode mode;
153 rtx new_reg;
155 if (original == NULL_RTX || (mode = GET_MODE (original)) == VOIDmode)
156 mode = md_mode;
157 lra_assert (mode != VOIDmode);
158 new_reg = gen_reg_rtx (mode);
159 if (original == NULL_RTX || ! REG_P (original))
161 if (lra_dump_file != NULL)
162 fprintf (lra_dump_file, " Creating newreg=%i", REGNO (new_reg));
164 else
166 if (ORIGINAL_REGNO (original) >= FIRST_PSEUDO_REGISTER)
167 ORIGINAL_REGNO (new_reg) = ORIGINAL_REGNO (original);
168 REG_USERVAR_P (new_reg) = REG_USERVAR_P (original);
169 REG_POINTER (new_reg) = REG_POINTER (original);
170 REG_ATTRS (new_reg) = REG_ATTRS (original);
171 if (lra_dump_file != NULL)
172 fprintf (lra_dump_file, " Creating newreg=%i from oldreg=%i",
173 REGNO (new_reg), REGNO (original));
175 if (lra_dump_file != NULL)
177 if (title != NULL)
178 fprintf (lra_dump_file, ", assigning class %s to%s%s r%d",
179 reg_class_names[rclass], *title == '\0' ? "" : " ",
180 title, REGNO (new_reg));
181 fprintf (lra_dump_file, "\n");
183 expand_reg_data (max_reg_num ());
184 setup_reg_classes (REGNO (new_reg), rclass, NO_REGS, rclass);
185 return new_reg;
188 /* Analogous to the previous function but also inherits value of
189 ORIGINAL. */
191 lra_create_new_reg (enum machine_mode md_mode, rtx original,
192 enum reg_class rclass, const char *title)
194 rtx new_reg;
196 new_reg
197 = lra_create_new_reg_with_unique_value (md_mode, original, rclass, title);
198 if (original != NULL_RTX && REG_P (original))
199 lra_assign_reg_val (REGNO (original), REGNO (new_reg));
200 return new_reg;
203 /* Set up for REGNO unique hold value. */
204 void
205 lra_set_regno_unique_value (int regno)
207 lra_reg_info[regno].val = get_new_reg_value ();
210 /* Invalidate INSN related info used by LRA. The info should never be
211 used after that. */
212 void
213 lra_invalidate_insn_data (rtx insn)
215 lra_invalidate_insn_regno_info (insn);
216 invalidate_insn_recog_data (INSN_UID (insn));
219 /* Mark INSN deleted and invalidate the insn related info used by
220 LRA. */
221 void
222 lra_set_insn_deleted (rtx insn)
224 lra_invalidate_insn_data (insn);
225 SET_INSN_DELETED (insn);
228 /* Delete an unneeded INSN and any previous insns who sole purpose is
229 loading data that is dead in INSN. */
230 void
231 lra_delete_dead_insn (rtx insn)
233 rtx prev = prev_real_insn (insn);
234 rtx prev_dest;
236 /* If the previous insn sets a register that dies in our insn,
237 delete it too. */
238 if (prev && GET_CODE (PATTERN (prev)) == SET
239 && (prev_dest = SET_DEST (PATTERN (prev)), REG_P (prev_dest))
240 && reg_mentioned_p (prev_dest, PATTERN (insn))
241 && find_regno_note (insn, REG_DEAD, REGNO (prev_dest))
242 && ! side_effects_p (SET_SRC (PATTERN (prev))))
243 lra_delete_dead_insn (prev);
245 lra_set_insn_deleted (insn);
248 /* Emit insn x = y + z. Return NULL if we failed to do it.
249 Otherwise, return the insn. We don't use gen_add3_insn as it might
250 clobber CC. */
251 static rtx
252 emit_add3_insn (rtx x, rtx y, rtx z)
254 rtx insn, last;
256 last = get_last_insn ();
257 insn = emit_insn (gen_rtx_SET (VOIDmode, x,
258 gen_rtx_PLUS (GET_MODE (y), y, z)));
259 if (recog_memoized (insn) < 0)
261 delete_insns_since (last);
262 insn = NULL_RTX;
264 return insn;
267 /* Emit insn x = x + y. Return the insn. We use gen_add2_insn as the
268 last resort. */
269 static rtx
270 emit_add2_insn (rtx x, rtx y)
272 rtx insn;
274 insn = emit_add3_insn (x, x, y);
275 if (insn == NULL_RTX)
277 insn = gen_add2_insn (x, y);
278 if (insn != NULL_RTX)
279 emit_insn (insn);
281 return insn;
284 /* Target checks operands through operand predicates to recognize an
285 insn. We should have a special precaution to generate add insns
286 which are frequent results of elimination.
288 Emit insns for x = y + z. X can be used to store intermediate
289 values and should be not in Y and Z when we use X to store an
290 intermediate value. Y + Z should form [base] [+ index[ * scale]] [
291 + disp] where base and index are registers, disp and scale are
292 constants. Y should contain base if it is present, Z should
293 contain disp if any. index[*scale] can be part of Y or Z. */
294 void
295 lra_emit_add (rtx x, rtx y, rtx z)
297 int old;
298 rtx insn, last;
299 rtx a1, a2, base, index, disp, scale, index_scale;
300 bool ok_p;
302 insn = emit_add3_insn (x, y, z);
303 old = max_reg_num ();
304 if (insn != NULL_RTX)
306 else
308 disp = a2 = NULL_RTX;
309 if (GET_CODE (y) == PLUS)
311 a1 = XEXP (y, 0);
312 a2 = XEXP (y, 1);
313 disp = z;
315 else
317 a1 = y;
318 if (CONSTANT_P (z))
319 disp = z;
320 else
321 a2 = z;
323 index_scale = scale = NULL_RTX;
324 if (GET_CODE (a1) == MULT)
326 index_scale = a1;
327 index = XEXP (a1, 0);
328 scale = XEXP (a1, 1);
329 base = a2;
331 else if (a2 != NULL_RTX && GET_CODE (a2) == MULT)
333 index_scale = a2;
334 index = XEXP (a2, 0);
335 scale = XEXP (a2, 1);
336 base = a1;
338 else
340 base = a1;
341 index = a2;
343 if (! (REG_P (base) || GET_CODE (base) == SUBREG)
344 || (index != NULL_RTX
345 && ! (REG_P (index) || GET_CODE (index) == SUBREG))
346 || (disp != NULL_RTX && ! CONSTANT_P (disp))
347 || (scale != NULL_RTX && ! CONSTANT_P (scale)))
349 /* Probably we have no 3 op add. Last chance is to use 2-op
350 add insn. To succeed, don't move Z to X as an address
351 segment always comes in Y. Otherwise, we might fail when
352 adding the address segment to register. */
353 lra_assert (x != y && x != z);
354 emit_move_insn (x, y);
355 insn = emit_add2_insn (x, z);
356 lra_assert (insn != NULL_RTX);
358 else
360 if (index_scale == NULL_RTX)
361 index_scale = index;
362 if (disp == NULL_RTX)
364 /* Generate x = index_scale; x = x + base. */
365 lra_assert (index_scale != NULL_RTX && base != NULL_RTX);
366 emit_move_insn (x, index_scale);
367 insn = emit_add2_insn (x, base);
368 lra_assert (insn != NULL_RTX);
370 else if (scale == NULL_RTX)
372 /* Try x = base + disp. */
373 lra_assert (base != NULL_RTX);
374 last = get_last_insn ();
375 insn = emit_move_insn (x, gen_rtx_PLUS (GET_MODE (base),
376 base, disp));
377 if (recog_memoized (insn) < 0)
379 delete_insns_since (last);
380 /* Generate x = disp; x = x + base. */
381 emit_move_insn (x, disp);
382 insn = emit_add2_insn (x, base);
383 lra_assert (insn != NULL_RTX);
385 /* Generate x = x + index. */
386 if (index != NULL_RTX)
388 insn = emit_add2_insn (x, index);
389 lra_assert (insn != NULL_RTX);
392 else
394 /* Try x = index_scale; x = x + disp; x = x + base. */
395 last = get_last_insn ();
396 insn = emit_move_insn (x, index_scale);
397 ok_p = false;
398 if (recog_memoized (insn) >= 0)
400 insn = emit_add2_insn (x, disp);
401 if (insn != NULL_RTX)
403 insn = emit_add2_insn (x, disp);
404 if (insn != NULL_RTX)
405 ok_p = true;
408 if (! ok_p)
410 delete_insns_since (last);
411 /* Generate x = disp; x = x + base; x = x + index_scale. */
412 emit_move_insn (x, disp);
413 insn = emit_add2_insn (x, base);
414 lra_assert (insn != NULL_RTX);
415 insn = emit_add2_insn (x, index_scale);
416 lra_assert (insn != NULL_RTX);
421 /* Functions emit_... can create pseudos -- so expand the pseudo
422 data. */
423 if (old != max_reg_num ())
424 expand_reg_data (old);
427 /* The number of emitted reload insns so far. */
428 int lra_curr_reload_num;
430 /* Emit x := y, processing special case when y = u + v or y = u + v *
431 scale + w through emit_add (Y can be an address which is base +
432 index reg * scale + displacement in general case). X may be used
433 as intermediate result therefore it should be not in Y. */
434 void
435 lra_emit_move (rtx x, rtx y)
437 int old;
439 if (GET_CODE (y) != PLUS)
441 if (rtx_equal_p (x, y))
442 return;
443 old = max_reg_num ();
444 emit_move_insn (x, y);
445 if (REG_P (x))
446 lra_reg_info[ORIGINAL_REGNO (x)].last_reload = ++lra_curr_reload_num;
447 /* Function emit_move can create pseudos -- so expand the pseudo
448 data. */
449 if (old != max_reg_num ())
450 expand_reg_data (old);
451 return;
453 lra_emit_add (x, XEXP (y, 0), XEXP (y, 1));
456 /* Update insn operands which are duplication of operands whose
457 numbers are in array of NOPS (with end marker -1). The insn is
458 represented by its LRA internal representation ID. */
459 void
460 lra_update_dups (lra_insn_recog_data_t id, signed char *nops)
462 int i, j, nop;
463 struct lra_static_insn_data *static_id = id->insn_static_data;
465 for (i = 0; i < static_id->n_dups; i++)
466 for (j = 0; (nop = nops[j]) >= 0; j++)
467 if (static_id->dup_num[i] == nop)
468 *id->dup_loc[i] = *id->operand_loc[nop];
473 /* This page contains code dealing with info about registers in the
474 insns. */
476 /* Pools for insn reg info. */
477 static alloc_pool insn_reg_pool;
479 /* Initiate pool for insn reg info. */
480 static void
481 init_insn_regs (void)
483 insn_reg_pool
484 = create_alloc_pool ("insn regs", sizeof (struct lra_insn_reg), 100);
487 /* Create LRA insn related info about a reference to REGNO in INSN with
488 TYPE (in/out/inout), biggest reference mode MODE, flag that it is
489 reference through subreg (SUBREG_P), flag that is early clobbered
490 in the insn (EARLY_CLOBBER), and reference to the next insn reg
491 info (NEXT). */
492 static struct lra_insn_reg *
493 new_insn_reg (rtx insn, int regno, enum op_type type, enum machine_mode mode,
494 bool subreg_p, bool early_clobber, struct lra_insn_reg *next)
496 struct lra_insn_reg *ir;
498 ir = (struct lra_insn_reg *) pool_alloc (insn_reg_pool);
499 ir->type = type;
500 ir->biggest_mode = mode;
501 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (lra_reg_info[regno].biggest_mode)
502 && NONDEBUG_INSN_P (insn))
503 lra_reg_info[regno].biggest_mode = mode;
504 ir->subreg_p = subreg_p;
505 ir->early_clobber = early_clobber;
506 ir->regno = regno;
507 ir->next = next;
508 return ir;
511 /* Free insn reg info IR. */
512 static void
513 free_insn_reg (struct lra_insn_reg *ir)
515 pool_free (insn_reg_pool, ir);
518 /* Free insn reg info list IR. */
519 static void
520 free_insn_regs (struct lra_insn_reg *ir)
522 struct lra_insn_reg *next_ir;
524 for (; ir != NULL; ir = next_ir)
526 next_ir = ir->next;
527 free_insn_reg (ir);
531 /* Finish pool for insn reg info. */
532 static void
533 finish_insn_regs (void)
535 free_alloc_pool (insn_reg_pool);
540 /* This page contains code dealing LRA insn info (or in other words
541 LRA internal insn representation). */
543 struct target_lra_int default_target_lra_int;
544 #if SWITCHABLE_TARGET
545 struct target_lra_int *this_target_lra_int = &default_target_lra_int;
546 #endif
548 /* Map INSN_CODE -> the static insn data. This info is valid during
549 all translation unit. */
550 struct lra_static_insn_data *insn_code_data[LAST_INSN_CODE];
552 /* Debug insns are represented as a special insn with one input
553 operand which is RTL expression in var_location. */
555 /* The following data are used as static insn operand data for all
556 debug insns. If structure lra_operand_data is changed, the
557 initializer should be changed too. */
558 static struct lra_operand_data debug_operand_data =
560 NULL, /* alternative */
561 VOIDmode, /* We are not interesting in the operand mode. */
562 OP_IN,
563 0, 0, 0, 0
566 /* The following data are used as static insn data for all debug
567 insns. If structure lra_static_insn_data is changed, the
568 initializer should be changed too. */
569 static struct lra_static_insn_data debug_insn_static_data =
571 &debug_operand_data,
572 0, /* Duplication operands #. */
573 -1, /* Commutative operand #. */
574 1, /* Operands #. There is only one operand which is debug RTL
575 expression. */
576 0, /* Duplications #. */
577 0, /* Alternatives #. We are not interesting in alternatives
578 because we does not proceed debug_insns for reloads. */
579 NULL, /* Hard registers referenced in machine description. */
580 NULL /* Descriptions of operands in alternatives. */
583 /* Called once per compiler work to initialize some LRA data related
584 to insns. */
585 static void
586 init_insn_code_data_once (void)
588 memset (insn_code_data, 0, sizeof (insn_code_data));
589 memset (op_alt_data, 0, sizeof (op_alt_data));
592 /* Called once per compiler work to finalize some LRA data related to
593 insns. */
594 static void
595 finish_insn_code_data_once (void)
597 int i;
599 for (i = 0; i < LAST_INSN_CODE; i++)
601 if (insn_code_data[i] != NULL)
602 free (insn_code_data[i]);
603 if (op_alt_data[i] != NULL)
604 free (op_alt_data[i]);
608 /* Initialize LRA info about operands in insn alternatives. */
609 static void
610 init_op_alt_data (void)
612 int i;
614 for (i = 0; i < LAST_INSN_CODE; i++)
615 if (op_alt_data[i] != NULL)
617 free (op_alt_data[i]);
618 op_alt_data[i] = NULL;
622 /* Return static insn data, allocate and setup if necessary. Although
623 dup_num is static data (it depends only on icode), to set it up we
624 need to extract insn first. So recog_data should be valid for
625 normal insn (ICODE >= 0) before the call. */
626 static struct lra_static_insn_data *
627 get_static_insn_data (int icode, int nop, int ndup, int nalt)
629 struct lra_static_insn_data *data;
630 size_t n_bytes;
632 lra_assert (icode < LAST_INSN_CODE);
633 if (icode >= 0 && (data = insn_code_data[icode]) != NULL)
634 return data;
635 lra_assert (nop >= 0 && ndup >= 0 && nalt >= 0);
636 n_bytes = sizeof (struct lra_static_insn_data)
637 + sizeof (struct lra_operand_data) * nop
638 + sizeof (int) * ndup;
639 data = XNEWVAR (struct lra_static_insn_data, n_bytes);
640 data->n_operands = nop;
641 data->n_dups = ndup;
642 data->n_alternatives = nalt;
643 data->operand = ((struct lra_operand_data *)
644 ((char *) data + sizeof (struct lra_static_insn_data)));
645 data->dup_num = ((int *) ((char *) data->operand
646 + sizeof (struct lra_operand_data) * nop));
647 if (icode >= 0)
649 int i;
651 insn_code_data[icode] = data;
652 for (i = 0; i < nop; i++)
654 data->operand[i].constraint
655 = insn_data[icode].operand[i].constraint;
656 data->operand[i].mode = insn_data[icode].operand[i].mode;
657 data->operand[i].strict_low = insn_data[icode].operand[i].strict_low;
658 data->operand[i].is_operator
659 = insn_data[icode].operand[i].is_operator;
660 data->operand[i].type
661 = (data->operand[i].constraint[0] == '=' ? OP_OUT
662 : data->operand[i].constraint[0] == '+' ? OP_INOUT
663 : OP_IN);
664 data->operand[i].is_address = false;
666 for (i = 0; i < ndup; i++)
667 data->dup_num[i] = recog_data.dup_num[i];
669 return data;
672 /* The current length of the following array. */
673 int lra_insn_recog_data_len;
675 /* Map INSN_UID -> the insn recog data (NULL if unknown). */
676 lra_insn_recog_data_t *lra_insn_recog_data;
678 /* Initialize LRA data about insns. */
679 static void
680 init_insn_recog_data (void)
682 lra_insn_recog_data_len = 0;
683 lra_insn_recog_data = NULL;
684 init_insn_regs ();
687 /* Expand, if necessary, LRA data about insns. */
688 static void
689 check_and_expand_insn_recog_data (int index)
691 int i, old;
693 if (lra_insn_recog_data_len > index)
694 return;
695 old = lra_insn_recog_data_len;
696 lra_insn_recog_data_len = index * 3 / 2 + 1;
697 lra_insn_recog_data = XRESIZEVEC (lra_insn_recog_data_t,
698 lra_insn_recog_data,
699 lra_insn_recog_data_len);
700 for (i = old; i < lra_insn_recog_data_len; i++)
701 lra_insn_recog_data[i] = NULL;
704 /* Finish LRA DATA about insn. */
705 static void
706 free_insn_recog_data (lra_insn_recog_data_t data)
708 if (data->operand_loc != NULL)
709 free (data->operand_loc);
710 if (data->dup_loc != NULL)
711 free (data->dup_loc);
712 if (data->arg_hard_regs != NULL)
713 free (data->arg_hard_regs);
714 if (HAVE_ATTR_enabled && data->alternative_enabled_p != NULL)
715 free (data->alternative_enabled_p);
716 if (data->icode < 0 && NONDEBUG_INSN_P (data->insn))
718 if (data->insn_static_data->operand_alternative != NULL)
719 free (data->insn_static_data->operand_alternative);
720 free_insn_regs (data->insn_static_data->hard_regs);
721 free (data->insn_static_data);
723 free_insn_regs (data->regs);
724 data->regs = NULL;
725 free (data);
728 /* Finish LRA data about all insns. */
729 static void
730 finish_insn_recog_data (void)
732 int i;
733 lra_insn_recog_data_t data;
735 for (i = 0; i < lra_insn_recog_data_len; i++)
736 if ((data = lra_insn_recog_data[i]) != NULL)
737 free_insn_recog_data (data);
738 finish_insn_regs ();
739 free (lra_insn_recog_data);
742 /* Setup info about operands in alternatives of LRA DATA of insn. */
743 static void
744 setup_operand_alternative (lra_insn_recog_data_t data)
746 int i, nop, nalt;
747 int icode = data->icode;
748 struct lra_static_insn_data *static_data = data->insn_static_data;
750 if (icode >= 0
751 && (static_data->operand_alternative = op_alt_data[icode]) != NULL)
752 return;
753 static_data->commutative = -1;
754 nop = static_data->n_operands;
755 if (nop == 0)
757 static_data->operand_alternative = NULL;
758 return;
760 nalt = static_data->n_alternatives;
761 static_data->operand_alternative = XNEWVEC (struct operand_alternative,
762 nalt * nop);
763 memset (static_data->operand_alternative, 0,
764 nalt * nop * sizeof (struct operand_alternative));
765 if (icode >= 0)
766 op_alt_data[icode] = static_data->operand_alternative;
767 for (i = 0; i < nop; i++)
769 int j;
770 struct operand_alternative *op_alt_start, *op_alt;
771 const char *p = static_data->operand[i].constraint;
773 static_data->operand[i].early_clobber = 0;
774 op_alt_start = &static_data->operand_alternative[i];
776 for (j = 0; j < nalt; j++)
778 op_alt = op_alt_start + j * nop;
779 op_alt->cl = NO_REGS;
780 op_alt->constraint = p;
781 op_alt->matches = -1;
782 op_alt->matched = -1;
784 if (*p == '\0' || *p == ',')
786 op_alt->anything_ok = 1;
787 continue;
790 for (;;)
792 char c = *p;
793 if (c == '#')
795 c = *++p;
796 while (c != ',' && c != '\0');
797 if (c == ',' || c == '\0')
799 p++;
800 break;
803 switch (c)
805 case '=': case '+': case '*':
806 case 'E': case 'F': case 'G': case 'H':
807 case 's': case 'i': case 'n':
808 case 'I': case 'J': case 'K': case 'L':
809 case 'M': case 'N': case 'O': case 'P':
810 /* These don't say anything we care about. */
811 break;
813 case '%':
814 /* We currently only support one commutative pair of
815 operands. */
816 if (static_data->commutative < 0)
817 static_data->commutative = i;
818 else
819 lra_assert (data->icode < 0); /* Asm */
821 /* The last operand should not be marked
822 commutative. */
823 lra_assert (i != nop - 1);
824 break;
826 case '?':
827 op_alt->reject += LRA_LOSER_COST_FACTOR;
828 break;
829 case '!':
830 op_alt->reject += LRA_MAX_REJECT;
831 break;
832 case '&':
833 op_alt->earlyclobber = 1;
834 static_data->operand[i].early_clobber = 1;
835 break;
837 case '0': case '1': case '2': case '3': case '4':
838 case '5': case '6': case '7': case '8': case '9':
840 char *end;
841 op_alt->matches = strtoul (p, &end, 10);
842 static_data->operand_alternative
843 [j * nop + op_alt->matches].matched = i;
844 p = end;
846 continue;
848 case TARGET_MEM_CONSTRAINT:
849 op_alt->memory_ok = 1;
850 break;
851 case '<':
852 op_alt->decmem_ok = 1;
853 break;
854 case '>':
855 op_alt->incmem_ok = 1;
856 break;
857 case 'V':
858 op_alt->nonoffmem_ok = 1;
859 break;
860 case 'o':
861 op_alt->offmem_ok = 1;
862 break;
863 case 'X':
864 op_alt->anything_ok = 1;
865 break;
867 case 'p':
868 static_data->operand[i].is_address = true;
869 op_alt->is_address = 1;
870 op_alt->cl = (reg_class_subunion[(int) op_alt->cl]
871 [(int) base_reg_class (VOIDmode,
872 ADDR_SPACE_GENERIC,
873 ADDRESS, SCRATCH)]);
874 break;
876 case 'g':
877 case 'r':
878 op_alt->cl =
879 reg_class_subunion[(int) op_alt->cl][(int) GENERAL_REGS];
880 break;
882 default:
883 if (EXTRA_MEMORY_CONSTRAINT (c, p))
885 op_alt->memory_ok = 1;
886 break;
888 if (EXTRA_ADDRESS_CONSTRAINT (c, p))
890 static_data->operand[i].is_address = true;
891 op_alt->is_address = 1;
892 op_alt->cl
893 = (reg_class_subunion
894 [(int) op_alt->cl]
895 [(int) base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
896 ADDRESS, SCRATCH)]);
897 break;
900 op_alt->cl
901 = (reg_class_subunion
902 [(int) op_alt->cl]
903 [(int)
904 REG_CLASS_FROM_CONSTRAINT ((unsigned char) c, p)]);
905 break;
907 p += CONSTRAINT_LEN (c, p);
913 /* Recursively process X and collect info about registers, which are
914 not the insn operands, in X with TYPE (in/out/inout) and flag that
915 it is early clobbered in the insn (EARLY_CLOBBER) and add the info
916 to LIST. X is a part of insn given by DATA. Return the result
917 list. */
918 static struct lra_insn_reg *
919 collect_non_operand_hard_regs (rtx *x, lra_insn_recog_data_t data,
920 struct lra_insn_reg *list,
921 enum op_type type, bool early_clobber)
923 int i, j, regno, last;
924 bool subreg_p;
925 enum machine_mode mode;
926 struct lra_insn_reg *curr;
927 rtx op = *x;
928 enum rtx_code code = GET_CODE (op);
929 const char *fmt = GET_RTX_FORMAT (code);
931 for (i = 0; i < data->insn_static_data->n_operands; i++)
932 if (x == data->operand_loc[i])
933 /* It is an operand loc. Stop here. */
934 return list;
935 for (i = 0; i < data->insn_static_data->n_dups; i++)
936 if (x == data->dup_loc[i])
937 /* It is a dup loc. Stop here. */
938 return list;
939 mode = GET_MODE (op);
940 subreg_p = false;
941 if (code == SUBREG)
943 op = SUBREG_REG (op);
944 code = GET_CODE (op);
945 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (op)))
947 mode = GET_MODE (op);
948 if (GET_MODE_SIZE (mode) > REGMODE_NATURAL_SIZE (mode))
949 subreg_p = true;
952 if (REG_P (op))
954 if ((regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER)
955 return list;
956 for (last = regno + hard_regno_nregs[regno][mode];
957 regno < last;
958 regno++)
959 if (! TEST_HARD_REG_BIT (lra_no_alloc_regs, regno)
960 || TEST_HARD_REG_BIT (eliminable_regset, regno))
962 for (curr = list; curr != NULL; curr = curr->next)
963 if (curr->regno == regno && curr->subreg_p == subreg_p
964 && curr->biggest_mode == mode)
966 if (curr->type != type)
967 curr->type = OP_INOUT;
968 if (curr->early_clobber != early_clobber)
969 curr->early_clobber = true;
970 break;
972 if (curr == NULL)
974 /* This is a new hard regno or the info can not be
975 integrated into the found structure. */
976 #ifdef STACK_REGS
977 early_clobber
978 = (early_clobber
979 /* This clobber is to inform popping floating
980 point stack only. */
981 && ! (FIRST_STACK_REG <= regno
982 && regno <= LAST_STACK_REG));
983 #endif
984 list = new_insn_reg (data->insn, regno, type, mode, subreg_p,
985 early_clobber, list);
988 return list;
990 switch (code)
992 case SET:
993 list = collect_non_operand_hard_regs (&SET_DEST (op), data,
994 list, OP_OUT, false);
995 list = collect_non_operand_hard_regs (&SET_SRC (op), data,
996 list, OP_IN, false);
997 break;
998 case CLOBBER:
999 /* We treat clobber of non-operand hard registers as early
1000 clobber (the behavior is expected from asm). */
1001 list = collect_non_operand_hard_regs (&XEXP (op, 0), data,
1002 list, OP_OUT, true);
1003 break;
1004 case PRE_INC: case PRE_DEC: case POST_INC: case POST_DEC:
1005 list = collect_non_operand_hard_regs (&XEXP (op, 0), data,
1006 list, OP_INOUT, false);
1007 break;
1008 case PRE_MODIFY: case POST_MODIFY:
1009 list = collect_non_operand_hard_regs (&XEXP (op, 0), data,
1010 list, OP_INOUT, false);
1011 list = collect_non_operand_hard_regs (&XEXP (op, 1), data,
1012 list, OP_IN, false);
1013 break;
1014 default:
1015 fmt = GET_RTX_FORMAT (code);
1016 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1018 if (fmt[i] == 'e')
1019 list = collect_non_operand_hard_regs (&XEXP (op, i), data,
1020 list, OP_IN, false);
1021 else if (fmt[i] == 'E')
1022 for (j = XVECLEN (op, i) - 1; j >= 0; j--)
1023 list = collect_non_operand_hard_regs (&XVECEXP (op, i, j), data,
1024 list, OP_IN, false);
1027 return list;
1030 /* Set up and return info about INSN. Set up the info if it is not set up
1031 yet. */
1032 lra_insn_recog_data_t
1033 lra_set_insn_recog_data (rtx insn)
1035 lra_insn_recog_data_t data;
1036 int i, n, icode;
1037 rtx **locs;
1038 unsigned int uid = INSN_UID (insn);
1039 struct lra_static_insn_data *insn_static_data;
1041 check_and_expand_insn_recog_data (uid);
1042 if (DEBUG_INSN_P (insn))
1043 icode = -1;
1044 else
1046 icode = INSN_CODE (insn);
1047 if (icode < 0)
1048 /* It might be a new simple insn which is not recognized yet. */
1049 INSN_CODE (insn) = icode = recog_memoized (insn);
1051 data = XNEW (struct lra_insn_recog_data);
1052 lra_insn_recog_data[uid] = data;
1053 data->insn = insn;
1054 data->used_insn_alternative = -1;
1055 data->icode = icode;
1056 data->regs = NULL;
1057 if (DEBUG_INSN_P (insn))
1059 data->insn_static_data = &debug_insn_static_data;
1060 data->dup_loc = NULL;
1061 data->arg_hard_regs = NULL;
1062 data->alternative_enabled_p = NULL;
1063 data->operand_loc = XNEWVEC (rtx *, 1);
1064 data->operand_loc[0] = &INSN_VAR_LOCATION_LOC (insn);
1065 return data;
1067 if (icode < 0)
1069 int nop;
1070 enum machine_mode operand_mode[MAX_RECOG_OPERANDS];
1071 const char *constraints[MAX_RECOG_OPERANDS];
1073 nop = asm_noperands (PATTERN (insn));
1074 data->operand_loc = data->dup_loc = NULL;
1075 if (nop < 0)
1077 /* Its is a special insn like USE or CLOBBER. We should
1078 recognize any regular insn otherwise LRA can do nothing
1079 with this insn. */
1080 gcc_assert (GET_CODE (PATTERN (insn)) == USE
1081 || GET_CODE (PATTERN (insn)) == CLOBBER
1082 || GET_CODE (PATTERN (insn)) == ASM_INPUT);
1083 data->insn_static_data = insn_static_data
1084 = get_static_insn_data (-1, 0, 0, 1);
1086 else
1088 /* expand_asm_operands makes sure there aren't too many
1089 operands. */
1090 lra_assert (nop <= MAX_RECOG_OPERANDS);
1091 if (nop != 0)
1092 data->operand_loc = XNEWVEC (rtx *, nop);
1093 /* Now get the operand values and constraints out of the
1094 insn. */
1095 decode_asm_operands (PATTERN (insn), NULL,
1096 data->operand_loc,
1097 constraints, operand_mode, NULL);
1098 n = 1;
1099 if (nop > 0)
1101 const char *p = recog_data.constraints[0];
1103 for (p = constraints[0]; *p; p++)
1104 n += *p == ',';
1106 data->insn_static_data = insn_static_data
1107 = get_static_insn_data (-1, nop, 0, n);
1108 for (i = 0; i < nop; i++)
1110 insn_static_data->operand[i].mode = operand_mode[i];
1111 insn_static_data->operand[i].constraint = constraints[i];
1112 insn_static_data->operand[i].strict_low = false;
1113 insn_static_data->operand[i].is_operator = false;
1114 insn_static_data->operand[i].is_address = false;
1117 for (i = 0; i < insn_static_data->n_operands; i++)
1118 insn_static_data->operand[i].type
1119 = (insn_static_data->operand[i].constraint[0] == '=' ? OP_OUT
1120 : insn_static_data->operand[i].constraint[0] == '+' ? OP_INOUT
1121 : OP_IN);
1122 data->alternative_enabled_p = NULL;
1124 else
1126 insn_extract (insn);
1127 data->insn_static_data = insn_static_data
1128 = get_static_insn_data (icode, insn_data[icode].n_operands,
1129 insn_data[icode].n_dups,
1130 insn_data[icode].n_alternatives);
1131 n = insn_static_data->n_operands;
1132 if (n == 0)
1133 locs = NULL;
1134 else
1136 locs = XNEWVEC (rtx *, n);
1137 memcpy (locs, recog_data.operand_loc, n * sizeof (rtx *));
1139 data->operand_loc = locs;
1140 n = insn_static_data->n_dups;
1141 if (n == 0)
1142 locs = NULL;
1143 else
1145 locs = XNEWVEC (rtx *, n);
1146 memcpy (locs, recog_data.dup_loc, n * sizeof (rtx *));
1148 data->dup_loc = locs;
1149 if (HAVE_ATTR_enabled)
1151 bool *bp;
1153 n = insn_static_data->n_alternatives;
1154 lra_assert (n >= 0);
1155 data->alternative_enabled_p = bp = XNEWVEC (bool, n);
1156 /* Cache the insn because we don't want to call extract_insn
1157 from get_attr_enabled as extract_insn modifies
1158 which_alternative. The attribute enabled should not depend
1159 on insn operands, operand modes, operand types, and operand
1160 constraints. It should depend on the architecture. If it
1161 is not true, we should rewrite this file code to use
1162 extract_insn instead of less expensive insn_extract. */
1163 recog_data.insn = insn;
1164 for (i = 0; i < n; i++)
1166 which_alternative = i;
1167 bp[i] = get_attr_enabled (insn);
1171 if (GET_CODE (PATTERN (insn)) == CLOBBER || GET_CODE (PATTERN (insn)) == USE)
1172 insn_static_data->hard_regs = NULL;
1173 else
1174 insn_static_data->hard_regs
1175 = collect_non_operand_hard_regs (&PATTERN (insn), data,
1176 NULL, OP_IN, false);
1177 setup_operand_alternative (data);
1178 data->arg_hard_regs = NULL;
1179 if (CALL_P (insn))
1181 rtx link;
1182 int n_hard_regs, regno, arg_hard_regs[FIRST_PSEUDO_REGISTER];
1184 n_hard_regs = 0;
1185 /* Finding implicit hard register usage. We believe it will be
1186 not changed whatever transformations are used. Call insns
1187 are such example. */
1188 for (link = CALL_INSN_FUNCTION_USAGE (insn);
1189 link != NULL_RTX;
1190 link = XEXP (link, 1))
1191 if (GET_CODE (XEXP (link, 0)) == USE
1192 && REG_P (XEXP (XEXP (link, 0), 0)))
1194 regno = REGNO (XEXP (XEXP (link, 0), 0));
1195 lra_assert (regno < FIRST_PSEUDO_REGISTER);
1196 /* It is an argument register. */
1197 for (i = (hard_regno_nregs
1198 [regno][GET_MODE (XEXP (XEXP (link, 0), 0))]) - 1;
1199 i >= 0;
1200 i--)
1201 arg_hard_regs[n_hard_regs++] = regno + i;
1203 if (n_hard_regs != 0)
1205 arg_hard_regs[n_hard_regs++] = -1;
1206 data->arg_hard_regs = XNEWVEC (int, n_hard_regs);
1207 memcpy (data->arg_hard_regs, arg_hard_regs,
1208 sizeof (int) * n_hard_regs);
1211 /* Some output operand can be recognized only from the context not
1212 from the constraints which are empty in this case. Call insn may
1213 contain a hard register in set destination with empty constraint
1214 and extract_insn treats them as an input. */
1215 for (i = 0; i < insn_static_data->n_operands; i++)
1217 int j;
1218 rtx pat, set;
1219 struct lra_operand_data *operand = &insn_static_data->operand[i];
1221 /* ??? Should we treat 'X' the same way. It looks to me that
1222 'X' means anything and empty constraint means we do not
1223 care. */
1224 if (operand->type != OP_IN || *operand->constraint != '\0'
1225 || operand->is_operator)
1226 continue;
1227 pat = PATTERN (insn);
1228 if (GET_CODE (pat) == SET)
1230 if (data->operand_loc[i] != &SET_DEST (pat))
1231 continue;
1233 else if (GET_CODE (pat) == PARALLEL)
1235 for (j = XVECLEN (pat, 0) - 1; j >= 0; j--)
1237 set = XVECEXP (PATTERN (insn), 0, j);
1238 if (GET_CODE (set) == SET
1239 && &SET_DEST (set) == data->operand_loc[i])
1240 break;
1242 if (j < 0)
1243 continue;
1245 else
1246 continue;
1247 operand->type = OP_OUT;
1249 return data;
1252 /* Return info about insn give by UID. The info should be already set
1253 up. */
1254 static lra_insn_recog_data_t
1255 get_insn_recog_data_by_uid (int uid)
1257 lra_insn_recog_data_t data;
1259 data = lra_insn_recog_data[uid];
1260 lra_assert (data != NULL);
1261 return data;
1264 /* Invalidate all info about insn given by its UID. */
1265 static void
1266 invalidate_insn_recog_data (int uid)
1268 lra_insn_recog_data_t data;
1270 data = lra_insn_recog_data[uid];
1271 lra_assert (data != NULL);
1272 free_insn_recog_data (data);
1273 lra_insn_recog_data[uid] = NULL;
1276 /* Update all the insn info about INSN. It is usually called when
1277 something in the insn was changed. Return the updated info. */
1278 lra_insn_recog_data_t
1279 lra_update_insn_recog_data (rtx insn)
1281 lra_insn_recog_data_t data;
1282 int n;
1283 unsigned int uid = INSN_UID (insn);
1284 struct lra_static_insn_data *insn_static_data;
1285 HOST_WIDE_INT sp_offset = 0;
1287 check_and_expand_insn_recog_data (uid);
1288 if ((data = lra_insn_recog_data[uid]) != NULL
1289 && data->icode != INSN_CODE (insn))
1291 sp_offset = data->sp_offset;
1292 invalidate_insn_data_regno_info (data, insn, get_insn_freq (insn));
1293 invalidate_insn_recog_data (uid);
1294 data = NULL;
1296 if (data == NULL)
1298 data = lra_get_insn_recog_data (insn);
1299 /* Initiate or restore SP offset. */
1300 data->sp_offset = sp_offset;
1301 return data;
1303 insn_static_data = data->insn_static_data;
1304 data->used_insn_alternative = -1;
1305 if (DEBUG_INSN_P (insn))
1306 return data;
1307 if (data->icode < 0)
1309 int nop;
1310 enum machine_mode operand_mode[MAX_RECOG_OPERANDS];
1311 const char *constraints[MAX_RECOG_OPERANDS];
1313 nop = asm_noperands (PATTERN (insn));
1314 if (nop >= 0)
1316 lra_assert (nop == data->insn_static_data->n_operands);
1317 /* Now get the operand values and constraints out of the
1318 insn. */
1319 decode_asm_operands (PATTERN (insn), NULL,
1320 data->operand_loc,
1321 constraints, operand_mode, NULL);
1322 #ifdef ENABLE_CHECKING
1324 int i;
1326 for (i = 0; i < nop; i++)
1327 lra_assert
1328 (insn_static_data->operand[i].mode == operand_mode[i]
1329 && insn_static_data->operand[i].constraint == constraints[i]
1330 && ! insn_static_data->operand[i].is_operator);
1332 #endif
1334 #ifdef ENABLE_CHECKING
1336 int i;
1338 for (i = 0; i < insn_static_data->n_operands; i++)
1339 lra_assert
1340 (insn_static_data->operand[i].type
1341 == (insn_static_data->operand[i].constraint[0] == '=' ? OP_OUT
1342 : insn_static_data->operand[i].constraint[0] == '+' ? OP_INOUT
1343 : OP_IN));
1345 #endif
1347 else
1349 insn_extract (insn);
1350 n = insn_static_data->n_operands;
1351 if (n != 0)
1352 memcpy (data->operand_loc, recog_data.operand_loc, n * sizeof (rtx *));
1353 n = insn_static_data->n_dups;
1354 if (n != 0)
1355 memcpy (data->dup_loc, recog_data.dup_loc, n * sizeof (rtx *));
1356 #if HAVE_ATTR_enabled
1357 #ifdef ENABLE_CHECKING
1359 int i;
1360 bool *bp;
1362 n = insn_static_data->n_alternatives;
1363 bp = data->alternative_enabled_p;
1364 lra_assert (n >= 0 && bp != NULL);
1365 /* Cache the insn to prevent extract_insn call from
1366 get_attr_enabled. */
1367 recog_data.insn = insn;
1368 for (i = 0; i < n; i++)
1370 which_alternative = i;
1371 lra_assert (bp[i] == get_attr_enabled (insn));
1374 #endif
1375 #endif
1377 return data;
1380 /* Set up that INSN is using alternative ALT now. */
1381 void
1382 lra_set_used_insn_alternative (rtx insn, int alt)
1384 lra_insn_recog_data_t data;
1386 data = lra_get_insn_recog_data (insn);
1387 data->used_insn_alternative = alt;
1390 /* Set up that insn with UID is using alternative ALT now. The insn
1391 info should be already set up. */
1392 void
1393 lra_set_used_insn_alternative_by_uid (int uid, int alt)
1395 lra_insn_recog_data_t data;
1397 check_and_expand_insn_recog_data (uid);
1398 data = lra_insn_recog_data[uid];
1399 lra_assert (data != NULL);
1400 data->used_insn_alternative = alt;
1405 /* This page contains code dealing with common register info and
1406 pseudo copies. */
1408 /* The size of the following array. */
1409 static int reg_info_size;
1410 /* Common info about each register. */
1411 struct lra_reg *lra_reg_info;
1413 /* Last register value. */
1414 static int last_reg_value;
1416 /* Return new register value. */
1417 static int
1418 get_new_reg_value (void)
1420 return ++last_reg_value;
1423 /* Pools for copies. */
1424 static alloc_pool copy_pool;
1426 /* Vec referring to pseudo copies. */
1427 static vec<lra_copy_t> copy_vec;
1429 /* Initialize I-th element of lra_reg_info. */
1430 static inline void
1431 initialize_lra_reg_info_element (int i)
1433 bitmap_initialize (&lra_reg_info[i].insn_bitmap, &reg_obstack);
1434 #ifdef STACK_REGS
1435 lra_reg_info[i].no_stack_p = false;
1436 #endif
1437 CLEAR_HARD_REG_SET (lra_reg_info[i].conflict_hard_regs);
1438 lra_reg_info[i].preferred_hard_regno1 = -1;
1439 lra_reg_info[i].preferred_hard_regno2 = -1;
1440 lra_reg_info[i].preferred_hard_regno_profit1 = 0;
1441 lra_reg_info[i].preferred_hard_regno_profit2 = 0;
1442 lra_reg_info[i].biggest_mode = VOIDmode;
1443 lra_reg_info[i].live_ranges = NULL;
1444 lra_reg_info[i].nrefs = lra_reg_info[i].freq = 0;
1445 lra_reg_info[i].last_reload = 0;
1446 lra_reg_info[i].restore_regno = -1;
1447 lra_reg_info[i].val = get_new_reg_value ();
1448 lra_reg_info[i].offset = 0;
1449 lra_reg_info[i].copies = NULL;
1452 /* Initialize common reg info and copies. */
1453 static void
1454 init_reg_info (void)
1456 int i;
1458 last_reg_value = 0;
1459 reg_info_size = max_reg_num () * 3 / 2 + 1;
1460 lra_reg_info = XNEWVEC (struct lra_reg, reg_info_size);
1461 for (i = 0; i < reg_info_size; i++)
1462 initialize_lra_reg_info_element (i);
1463 copy_pool
1464 = create_alloc_pool ("lra copies", sizeof (struct lra_copy), 100);
1465 copy_vec.create (100);
1469 /* Finish common reg info and copies. */
1470 static void
1471 finish_reg_info (void)
1473 int i;
1475 for (i = 0; i < reg_info_size; i++)
1476 bitmap_clear (&lra_reg_info[i].insn_bitmap);
1477 free (lra_reg_info);
1478 reg_info_size = 0;
1479 free_alloc_pool (copy_pool);
1480 copy_vec.release ();
1483 /* Expand common reg info if it is necessary. */
1484 static void
1485 expand_reg_info (void)
1487 int i, old = reg_info_size;
1489 if (reg_info_size > max_reg_num ())
1490 return;
1491 reg_info_size = max_reg_num () * 3 / 2 + 1;
1492 lra_reg_info = XRESIZEVEC (struct lra_reg, lra_reg_info, reg_info_size);
1493 for (i = old; i < reg_info_size; i++)
1494 initialize_lra_reg_info_element (i);
1497 /* Free all copies. */
1498 void
1499 lra_free_copies (void)
1501 lra_copy_t cp;
1503 while (copy_vec.length () != 0)
1505 cp = copy_vec.pop ();
1506 lra_reg_info[cp->regno1].copies = lra_reg_info[cp->regno2].copies = NULL;
1507 pool_free (copy_pool, cp);
1511 /* Create copy of two pseudos REGNO1 and REGNO2. The copy execution
1512 frequency is FREQ. */
1513 void
1514 lra_create_copy (int regno1, int regno2, int freq)
1516 bool regno1_dest_p;
1517 lra_copy_t cp;
1519 lra_assert (regno1 != regno2);
1520 regno1_dest_p = true;
1521 if (regno1 > regno2)
1523 int temp = regno2;
1525 regno1_dest_p = false;
1526 regno2 = regno1;
1527 regno1 = temp;
1529 cp = (lra_copy_t) pool_alloc (copy_pool);
1530 copy_vec.safe_push (cp);
1531 cp->regno1_dest_p = regno1_dest_p;
1532 cp->freq = freq;
1533 cp->regno1 = regno1;
1534 cp->regno2 = regno2;
1535 cp->regno1_next = lra_reg_info[regno1].copies;
1536 lra_reg_info[regno1].copies = cp;
1537 cp->regno2_next = lra_reg_info[regno2].copies;
1538 lra_reg_info[regno2].copies = cp;
1539 if (lra_dump_file != NULL)
1540 fprintf (lra_dump_file, " Creating copy r%d%sr%d@%d\n",
1541 regno1, regno1_dest_p ? "<-" : "->", regno2, freq);
1544 /* Return N-th (0, 1, ...) copy. If there is no copy, return
1545 NULL. */
1546 lra_copy_t
1547 lra_get_copy (int n)
1549 if (n >= (int) copy_vec.length ())
1550 return NULL;
1551 return copy_vec[n];
1556 /* This page contains code dealing with info about registers in
1557 insns. */
1559 /* Process X of insn UID recursively and add info (operand type is
1560 given by TYPE, flag of that it is early clobber is EARLY_CLOBBER)
1561 about registers in X to the insn DATA. */
1562 static void
1563 add_regs_to_insn_regno_info (lra_insn_recog_data_t data, rtx x, int uid,
1564 enum op_type type, bool early_clobber)
1566 int i, j, regno;
1567 bool subreg_p;
1568 enum machine_mode mode;
1569 const char *fmt;
1570 enum rtx_code code;
1571 struct lra_insn_reg *curr;
1573 code = GET_CODE (x);
1574 mode = GET_MODE (x);
1575 subreg_p = false;
1576 if (GET_CODE (x) == SUBREG)
1578 x = SUBREG_REG (x);
1579 code = GET_CODE (x);
1580 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (x)))
1582 mode = GET_MODE (x);
1583 if (GET_MODE_SIZE (mode) > REGMODE_NATURAL_SIZE (mode))
1584 subreg_p = true;
1587 if (REG_P (x))
1589 regno = REGNO (x);
1590 if (regno < FIRST_PSEUDO_REGISTER
1591 && TEST_HARD_REG_BIT (lra_no_alloc_regs, regno)
1592 && ! TEST_HARD_REG_BIT (eliminable_regset, regno))
1593 return;
1594 expand_reg_info ();
1595 if (bitmap_set_bit (&lra_reg_info[regno].insn_bitmap, uid))
1597 data->regs = new_insn_reg (data->insn, regno, type, mode, subreg_p,
1598 early_clobber, data->regs);
1599 return;
1601 else
1603 for (curr = data->regs; curr != NULL; curr = curr->next)
1604 if (curr->regno == regno)
1606 if (curr->subreg_p != subreg_p || curr->biggest_mode != mode)
1607 /* The info can not be integrated into the found
1608 structure. */
1609 data->regs = new_insn_reg (data->insn, regno, type, mode,
1610 subreg_p, early_clobber,
1611 data->regs);
1612 else
1614 if (curr->type != type)
1615 curr->type = OP_INOUT;
1616 if (curr->early_clobber != early_clobber)
1617 curr->early_clobber = true;
1619 return;
1621 gcc_unreachable ();
1625 switch (code)
1627 case SET:
1628 add_regs_to_insn_regno_info (data, SET_DEST (x), uid, OP_OUT, false);
1629 add_regs_to_insn_regno_info (data, SET_SRC (x), uid, OP_IN, false);
1630 break;
1631 case CLOBBER:
1632 /* We treat clobber of non-operand hard registers as early
1633 clobber (the behavior is expected from asm). */
1634 add_regs_to_insn_regno_info (data, XEXP (x, 0), uid, OP_OUT, true);
1635 break;
1636 case PRE_INC: case PRE_DEC: case POST_INC: case POST_DEC:
1637 add_regs_to_insn_regno_info (data, XEXP (x, 0), uid, OP_INOUT, false);
1638 break;
1639 case PRE_MODIFY: case POST_MODIFY:
1640 add_regs_to_insn_regno_info (data, XEXP (x, 0), uid, OP_INOUT, false);
1641 add_regs_to_insn_regno_info (data, XEXP (x, 1), uid, OP_IN, false);
1642 break;
1643 default:
1644 if ((code != PARALLEL && code != EXPR_LIST) || type != OP_OUT)
1645 /* Some targets place small structures in registers for return
1646 values of functions, and those registers are wrapped in
1647 PARALLEL that we may see as the destination of a SET. Here
1648 is an example:
1650 (call_insn 13 12 14 2 (set (parallel:BLK [
1651 (expr_list:REG_DEP_TRUE (reg:DI 0 ax)
1652 (const_int 0 [0]))
1653 (expr_list:REG_DEP_TRUE (reg:DI 1 dx)
1654 (const_int 8 [0x8]))
1656 (call (mem:QI (symbol_ref:DI (... */
1657 type = OP_IN;
1658 fmt = GET_RTX_FORMAT (code);
1659 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1661 if (fmt[i] == 'e')
1662 add_regs_to_insn_regno_info (data, XEXP (x, i), uid, type, false);
1663 else if (fmt[i] == 'E')
1665 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1666 add_regs_to_insn_regno_info (data, XVECEXP (x, i, j), uid,
1667 type, false);
1673 /* Return execution frequency of INSN. */
1674 static int
1675 get_insn_freq (rtx insn)
1677 basic_block bb = BLOCK_FOR_INSN (insn);
1679 gcc_checking_assert (bb != NULL);
1680 return REG_FREQ_FROM_BB (bb);
1683 /* Invalidate all reg info of INSN with DATA and execution frequency
1684 FREQ. Update common info about the invalidated registers. */
1685 static void
1686 invalidate_insn_data_regno_info (lra_insn_recog_data_t data, rtx insn,
1687 int freq)
1689 int uid;
1690 bool debug_p;
1691 unsigned int i;
1692 struct lra_insn_reg *ir, *next_ir;
1694 uid = INSN_UID (insn);
1695 debug_p = DEBUG_INSN_P (insn);
1696 for (ir = data->regs; ir != NULL; ir = next_ir)
1698 i = ir->regno;
1699 next_ir = ir->next;
1700 free_insn_reg (ir);
1701 bitmap_clear_bit (&lra_reg_info[i].insn_bitmap, uid);
1702 if (i >= FIRST_PSEUDO_REGISTER && ! debug_p)
1704 lra_reg_info[i].nrefs--;
1705 lra_reg_info[i].freq -= freq;
1706 lra_assert (lra_reg_info[i].nrefs >= 0 && lra_reg_info[i].freq >= 0);
1709 data->regs = NULL;
1712 /* Invalidate all reg info of INSN. Update common info about the
1713 invalidated registers. */
1714 void
1715 lra_invalidate_insn_regno_info (rtx insn)
1717 invalidate_insn_data_regno_info (lra_get_insn_recog_data (insn), insn,
1718 get_insn_freq (insn));
1721 /* Update common reg info from reg info of insn given by its DATA and
1722 execution frequency FREQ. */
1723 static void
1724 setup_insn_reg_info (lra_insn_recog_data_t data, int freq)
1726 unsigned int i;
1727 struct lra_insn_reg *ir;
1729 for (ir = data->regs; ir != NULL; ir = ir->next)
1730 if ((i = ir->regno) >= FIRST_PSEUDO_REGISTER)
1732 lra_reg_info[i].nrefs++;
1733 lra_reg_info[i].freq += freq;
1737 /* Set up insn reg info of INSN. Update common reg info from reg info
1738 of INSN. */
1739 void
1740 lra_update_insn_regno_info (rtx insn)
1742 int i, uid, freq;
1743 lra_insn_recog_data_t data;
1744 struct lra_static_insn_data *static_data;
1745 enum rtx_code code;
1747 if (! INSN_P (insn))
1748 return;
1749 data = lra_get_insn_recog_data (insn);
1750 static_data = data->insn_static_data;
1751 freq = get_insn_freq (insn);
1752 invalidate_insn_data_regno_info (data, insn, freq);
1753 uid = INSN_UID (insn);
1754 for (i = static_data->n_operands - 1; i >= 0; i--)
1755 add_regs_to_insn_regno_info (data, *data->operand_loc[i], uid,
1756 static_data->operand[i].type,
1757 static_data->operand[i].early_clobber);
1758 if ((code = GET_CODE (PATTERN (insn))) == CLOBBER || code == USE)
1759 add_regs_to_insn_regno_info (data, XEXP (PATTERN (insn), 0), uid,
1760 code == USE ? OP_IN : OP_OUT, false);
1761 if (NONDEBUG_INSN_P (insn))
1762 setup_insn_reg_info (data, freq);
1765 /* Return reg info of insn given by it UID. */
1766 struct lra_insn_reg *
1767 lra_get_insn_regs (int uid)
1769 lra_insn_recog_data_t data;
1771 data = get_insn_recog_data_by_uid (uid);
1772 return data->regs;
1777 /* This page contains code dealing with stack of the insns which
1778 should be processed by the next constraint pass. */
1780 /* Bitmap used to put an insn on the stack only in one exemplar. */
1781 static sbitmap lra_constraint_insn_stack_bitmap;
1783 /* The stack itself. */
1784 vec<rtx> lra_constraint_insn_stack;
1786 /* Put INSN on the stack. If ALWAYS_UPDATE is true, always update the reg
1787 info for INSN, otherwise only update it if INSN is not already on the
1788 stack. */
1789 static inline void
1790 lra_push_insn_1 (rtx insn, bool always_update)
1792 unsigned int uid = INSN_UID (insn);
1793 if (always_update)
1794 lra_update_insn_regno_info (insn);
1795 if (uid >= SBITMAP_SIZE (lra_constraint_insn_stack_bitmap))
1796 lra_constraint_insn_stack_bitmap =
1797 sbitmap_resize (lra_constraint_insn_stack_bitmap, 3 * uid / 2, 0);
1798 if (bitmap_bit_p (lra_constraint_insn_stack_bitmap, uid))
1799 return;
1800 bitmap_set_bit (lra_constraint_insn_stack_bitmap, uid);
1801 if (! always_update)
1802 lra_update_insn_regno_info (insn);
1803 lra_constraint_insn_stack.safe_push (insn);
1806 /* Put INSN on the stack. */
1807 void
1808 lra_push_insn (rtx insn)
1810 lra_push_insn_1 (insn, false);
1813 /* Put INSN on the stack and update its reg info. */
1814 void
1815 lra_push_insn_and_update_insn_regno_info (rtx insn)
1817 lra_push_insn_1 (insn, true);
1820 /* Put insn with UID on the stack. */
1821 void
1822 lra_push_insn_by_uid (unsigned int uid)
1824 lra_push_insn (lra_insn_recog_data[uid]->insn);
1827 /* Take the last-inserted insns off the stack and return it. */
1829 lra_pop_insn (void)
1831 rtx insn = lra_constraint_insn_stack.pop ();
1832 bitmap_clear_bit (lra_constraint_insn_stack_bitmap, INSN_UID (insn));
1833 return insn;
1836 /* Return the current size of the insn stack. */
1837 unsigned int
1838 lra_insn_stack_length (void)
1840 return lra_constraint_insn_stack.length ();
1843 /* Push insns FROM to TO (excluding it) going in reverse order. */
1844 static void
1845 push_insns (rtx from, rtx to)
1847 rtx insn;
1849 if (from == NULL_RTX)
1850 return;
1851 for (insn = from; insn != to; insn = PREV_INSN (insn))
1852 if (INSN_P (insn))
1853 lra_push_insn (insn);
1856 /* Set up sp offset for insn in range [FROM, LAST]. The offset is
1857 taken from the next BB insn after LAST or zero if there in such
1858 insn. */
1859 static void
1860 setup_sp_offset (rtx from, rtx last)
1862 rtx before = next_nonnote_insn_bb (last);
1863 HOST_WIDE_INT offset = (before == NULL_RTX || ! INSN_P (before)
1864 ? 0 : lra_get_insn_recog_data (before)->sp_offset);
1866 for (rtx insn = from; insn != NEXT_INSN (last); insn = NEXT_INSN (insn))
1867 lra_get_insn_recog_data (insn)->sp_offset = offset;
1870 /* Emit insns BEFORE before INSN and insns AFTER after INSN. Put the
1871 insns onto the stack. Print about emitting the insns with
1872 TITLE. */
1873 void
1874 lra_process_new_insns (rtx insn, rtx before, rtx after, const char *title)
1876 rtx last;
1878 if (before == NULL_RTX && after == NULL_RTX)
1879 return;
1880 if (lra_dump_file != NULL)
1882 dump_insn_slim (lra_dump_file, insn);
1883 if (before != NULL_RTX)
1885 fprintf (lra_dump_file," %s before:\n", title);
1886 dump_rtl_slim (lra_dump_file, before, NULL_RTX, -1, 0);
1888 if (after != NULL_RTX)
1890 fprintf (lra_dump_file, " %s after:\n", title);
1891 dump_rtl_slim (lra_dump_file, after, NULL_RTX, -1, 0);
1893 fprintf (lra_dump_file, "\n");
1895 if (before != NULL_RTX)
1897 emit_insn_before (before, insn);
1898 push_insns (PREV_INSN (insn), PREV_INSN (before));
1899 setup_sp_offset (before, PREV_INSN (insn));
1901 if (after != NULL_RTX)
1903 for (last = after; NEXT_INSN (last) != NULL_RTX; last = NEXT_INSN (last))
1905 emit_insn_after (after, insn);
1906 push_insns (last, insn);
1907 setup_sp_offset (after, last);
1913 /* This page contains code dealing with scratches (changing them onto
1914 pseudos and restoring them from the pseudos).
1916 We change scratches into pseudos at the beginning of LRA to
1917 simplify dealing with them (conflicts, hard register assignments).
1919 If the pseudo denoting scratch was spilled it means that we do need
1920 a hard register for it. Such pseudos are transformed back to
1921 scratches at the end of LRA. */
1923 /* Description of location of a former scratch operand. */
1924 struct sloc
1926 rtx insn; /* Insn where the scratch was. */
1927 int nop; /* Number of the operand which was a scratch. */
1930 typedef struct sloc *sloc_t;
1932 /* Locations of the former scratches. */
1933 static vec<sloc_t> scratches;
1935 /* Bitmap of scratch regnos. */
1936 static bitmap_head scratch_bitmap;
1938 /* Bitmap of scratch operands. */
1939 static bitmap_head scratch_operand_bitmap;
1941 /* Return true if pseudo REGNO is made of SCRATCH. */
1942 bool
1943 lra_former_scratch_p (int regno)
1945 return bitmap_bit_p (&scratch_bitmap, regno);
1948 /* Return true if the operand NOP of INSN is a former scratch. */
1949 bool
1950 lra_former_scratch_operand_p (rtx insn, int nop)
1952 return bitmap_bit_p (&scratch_operand_bitmap,
1953 INSN_UID (insn) * MAX_RECOG_OPERANDS + nop) != 0;
1956 /* Change scratches onto pseudos and save their location. */
1957 static void
1958 remove_scratches (void)
1960 int i;
1961 bool insn_changed_p;
1962 basic_block bb;
1963 rtx insn, reg;
1964 sloc_t loc;
1965 lra_insn_recog_data_t id;
1966 struct lra_static_insn_data *static_id;
1968 scratches.create (get_max_uid ());
1969 bitmap_initialize (&scratch_bitmap, &reg_obstack);
1970 bitmap_initialize (&scratch_operand_bitmap, &reg_obstack);
1971 FOR_EACH_BB_FN (bb, cfun)
1972 FOR_BB_INSNS (bb, insn)
1973 if (INSN_P (insn))
1975 id = lra_get_insn_recog_data (insn);
1976 static_id = id->insn_static_data;
1977 insn_changed_p = false;
1978 for (i = 0; i < static_id->n_operands; i++)
1979 if (GET_CODE (*id->operand_loc[i]) == SCRATCH
1980 && GET_MODE (*id->operand_loc[i]) != VOIDmode)
1982 insn_changed_p = true;
1983 *id->operand_loc[i] = reg
1984 = lra_create_new_reg (static_id->operand[i].mode,
1985 *id->operand_loc[i], ALL_REGS, NULL);
1986 add_reg_note (insn, REG_UNUSED, reg);
1987 lra_update_dup (id, i);
1988 loc = XNEW (struct sloc);
1989 loc->insn = insn;
1990 loc->nop = i;
1991 scratches.safe_push (loc);
1992 bitmap_set_bit (&scratch_bitmap, REGNO (*id->operand_loc[i]));
1993 bitmap_set_bit (&scratch_operand_bitmap,
1994 INSN_UID (insn) * MAX_RECOG_OPERANDS + i);
1995 if (lra_dump_file != NULL)
1996 fprintf (lra_dump_file,
1997 "Removing SCRATCH in insn #%u (nop %d)\n",
1998 INSN_UID (insn), i);
2000 if (insn_changed_p)
2001 /* Because we might use DF right after caller-saves sub-pass
2002 we need to keep DF info up to date. */
2003 df_insn_rescan (insn);
2007 /* Changes pseudos created by function remove_scratches onto scratches. */
2008 static void
2009 restore_scratches (void)
2011 int regno;
2012 unsigned i;
2013 sloc_t loc;
2014 rtx last = NULL_RTX;
2015 lra_insn_recog_data_t id = NULL;
2017 for (i = 0; scratches.iterate (i, &loc); i++)
2019 if (last != loc->insn)
2021 last = loc->insn;
2022 id = lra_get_insn_recog_data (last);
2024 if (REG_P (*id->operand_loc[loc->nop])
2025 && ((regno = REGNO (*id->operand_loc[loc->nop]))
2026 >= FIRST_PSEUDO_REGISTER)
2027 && lra_get_regno_hard_regno (regno) < 0)
2029 /* It should be only case when scratch register with chosen
2030 constraint 'X' did not get memory or hard register. */
2031 lra_assert (lra_former_scratch_p (regno));
2032 *id->operand_loc[loc->nop]
2033 = gen_rtx_SCRATCH (GET_MODE (*id->operand_loc[loc->nop]));
2034 lra_update_dup (id, loc->nop);
2035 if (lra_dump_file != NULL)
2036 fprintf (lra_dump_file, "Restoring SCRATCH in insn #%u(nop %d)\n",
2037 INSN_UID (loc->insn), loc->nop);
2040 for (i = 0; scratches.iterate (i, &loc); i++)
2041 free (loc);
2042 scratches.release ();
2043 bitmap_clear (&scratch_bitmap);
2044 bitmap_clear (&scratch_operand_bitmap);
2049 #ifdef ENABLE_CHECKING
2051 /* Function checks RTL for correctness. If FINAL_P is true, it is
2052 done at the end of LRA and the check is more rigorous. */
2053 static void
2054 check_rtl (bool final_p)
2056 basic_block bb;
2057 rtx insn;
2059 lra_assert (! final_p || reload_completed);
2060 FOR_EACH_BB_FN (bb, cfun)
2061 FOR_BB_INSNS (bb, insn)
2062 if (NONDEBUG_INSN_P (insn)
2063 && GET_CODE (PATTERN (insn)) != USE
2064 && GET_CODE (PATTERN (insn)) != CLOBBER
2065 && GET_CODE (PATTERN (insn)) != ASM_INPUT)
2067 if (final_p)
2069 extract_insn (insn);
2070 lra_assert (constrain_operands (1));
2071 continue;
2073 /* LRA code is based on assumption that all addresses can be
2074 correctly decomposed. LRA can generate reloads for
2075 decomposable addresses. The decomposition code checks the
2076 correctness of the addresses. So we don't need to check
2077 the addresses here. Don't call insn_invalid_p here, it can
2078 change the code at this stage. */
2079 if (recog_memoized (insn) < 0 && asm_noperands (PATTERN (insn)) < 0)
2080 fatal_insn_not_found (insn);
2083 #endif /* #ifdef ENABLE_CHECKING */
2085 /* Determine if the current function has an exception receiver block
2086 that reaches the exit block via non-exceptional edges */
2087 static bool
2088 has_nonexceptional_receiver (void)
2090 edge e;
2091 edge_iterator ei;
2092 basic_block *tos, *worklist, bb;
2094 /* If we're not optimizing, then just err on the safe side. */
2095 if (!optimize)
2096 return true;
2098 /* First determine which blocks can reach exit via normal paths. */
2099 tos = worklist = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun) + 1);
2101 FOR_EACH_BB_FN (bb, cfun)
2102 bb->flags &= ~BB_REACHABLE;
2104 /* Place the exit block on our worklist. */
2105 EXIT_BLOCK_PTR_FOR_FN (cfun)->flags |= BB_REACHABLE;
2106 *tos++ = EXIT_BLOCK_PTR_FOR_FN (cfun);
2108 /* Iterate: find everything reachable from what we've already seen. */
2109 while (tos != worklist)
2111 bb = *--tos;
2113 FOR_EACH_EDGE (e, ei, bb->preds)
2114 if (e->flags & EDGE_ABNORMAL)
2116 free (worklist);
2117 return true;
2119 else
2121 basic_block src = e->src;
2123 if (!(src->flags & BB_REACHABLE))
2125 src->flags |= BB_REACHABLE;
2126 *tos++ = src;
2130 free (worklist);
2131 /* No exceptional block reached exit unexceptionally. */
2132 return false;
2135 #ifdef AUTO_INC_DEC
2137 /* Process recursively X of INSN and add REG_INC notes if necessary. */
2138 static void
2139 add_auto_inc_notes (rtx insn, rtx x)
2141 enum rtx_code code = GET_CODE (x);
2142 const char *fmt;
2143 int i, j;
2145 if (code == MEM && auto_inc_p (XEXP (x, 0)))
2147 add_reg_note (insn, REG_INC, XEXP (XEXP (x, 0), 0));
2148 return;
2151 /* Scan all X sub-expressions. */
2152 fmt = GET_RTX_FORMAT (code);
2153 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2155 if (fmt[i] == 'e')
2156 add_auto_inc_notes (insn, XEXP (x, i));
2157 else if (fmt[i] == 'E')
2158 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2159 add_auto_inc_notes (insn, XVECEXP (x, i, j));
2163 #endif
2165 /* Remove all REG_DEAD and REG_UNUSED notes and regenerate REG_INC.
2166 We change pseudos by hard registers without notification of DF and
2167 that can make the notes obsolete. DF-infrastructure does not deal
2168 with REG_INC notes -- so we should regenerate them here. */
2169 static void
2170 update_inc_notes (void)
2172 rtx *pnote;
2173 basic_block bb;
2174 rtx insn;
2176 FOR_EACH_BB_FN (bb, cfun)
2177 FOR_BB_INSNS (bb, insn)
2178 if (NONDEBUG_INSN_P (insn))
2180 pnote = &REG_NOTES (insn);
2181 while (*pnote != 0)
2183 if (REG_NOTE_KIND (*pnote) == REG_DEAD
2184 || REG_NOTE_KIND (*pnote) == REG_UNUSED
2185 || REG_NOTE_KIND (*pnote) == REG_INC)
2186 *pnote = XEXP (*pnote, 1);
2187 else
2188 pnote = &XEXP (*pnote, 1);
2190 #ifdef AUTO_INC_DEC
2191 add_auto_inc_notes (insn, PATTERN (insn));
2192 #endif
2196 /* Set to 1 while in lra. */
2197 int lra_in_progress;
2199 /* Start of pseudo regnos before the LRA. */
2200 int lra_new_regno_start;
2202 /* Start of reload pseudo regnos before the new spill pass. */
2203 int lra_constraint_new_regno_start;
2205 /* Inheritance pseudo regnos before the new spill pass. */
2206 bitmap_head lra_inheritance_pseudos;
2208 /* Split regnos before the new spill pass. */
2209 bitmap_head lra_split_regs;
2211 /* Reload pseudo regnos before the new assignmnet pass which still can
2212 be spilled after the assinment pass as memory is also accepted in
2213 insns for the reload pseudos. */
2214 bitmap_head lra_optional_reload_pseudos;
2216 /* Pseudo regnos used for subreg reloads before the new assignment
2217 pass. Such pseudos still can be spilled after the assinment
2218 pass. */
2219 bitmap_head lra_subreg_reload_pseudos;
2221 /* First UID of insns generated before a new spill pass. */
2222 int lra_constraint_new_insn_uid_start;
2224 /* File used for output of LRA debug information. */
2225 FILE *lra_dump_file;
2227 /* True if we should try spill into registers of different classes
2228 instead of memory. */
2229 bool lra_reg_spill_p;
2231 /* Set up value LRA_REG_SPILL_P. */
2232 static void
2233 setup_reg_spill_flag (void)
2235 int cl, mode;
2237 if (targetm.spill_class != NULL)
2238 for (cl = 0; cl < (int) LIM_REG_CLASSES; cl++)
2239 for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
2240 if (targetm.spill_class ((enum reg_class) cl,
2241 (enum machine_mode) mode) != NO_REGS)
2243 lra_reg_spill_p = true;
2244 return;
2246 lra_reg_spill_p = false;
2249 /* True if the current function is too big to use regular algorithms
2250 in LRA. In other words, we should use simpler and faster algorithms
2251 in LRA. It also means we should not worry about generation code
2252 for caller saves. The value is set up in IRA. */
2253 bool lra_simple_p;
2255 /* Major LRA entry function. F is a file should be used to dump LRA
2256 debug info. */
2257 void
2258 lra (FILE *f)
2260 int i;
2261 bool live_p, scratch_p, inserted_p;
2263 lra_dump_file = f;
2265 timevar_push (TV_LRA);
2267 /* Make sure that the last insn is a note. Some subsequent passes
2268 need it. */
2269 emit_note (NOTE_INSN_DELETED);
2271 COPY_HARD_REG_SET (lra_no_alloc_regs, ira_no_alloc_regs);
2273 init_reg_info ();
2274 expand_reg_info ();
2276 init_insn_recog_data ();
2278 #ifdef ENABLE_CHECKING
2279 /* Some quick check on RTL generated by previous passes. */
2280 check_rtl (false);
2281 #endif
2283 lra_in_progress = 1;
2285 lra_live_range_iter = lra_coalesce_iter = 0;
2286 lra_constraint_iter = lra_constraint_iter_after_spill = 0;
2287 lra_inheritance_iter = lra_undo_inheritance_iter = 0;
2289 setup_reg_spill_flag ();
2291 /* Function remove_scratches can creates new pseudos for clobbers --
2292 so set up lra_constraint_new_regno_start before its call to
2293 permit changing reg classes for pseudos created by this
2294 simplification. */
2295 lra_constraint_new_regno_start = lra_new_regno_start = max_reg_num ();
2296 remove_scratches ();
2297 scratch_p = lra_constraint_new_regno_start != max_reg_num ();
2299 /* A function that has a non-local label that can reach the exit
2300 block via non-exceptional paths must save all call-saved
2301 registers. */
2302 if (cfun->has_nonlocal_label && has_nonexceptional_receiver ())
2303 crtl->saves_all_registers = 1;
2305 if (crtl->saves_all_registers)
2306 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2307 if (! call_used_regs[i] && ! fixed_regs[i] && ! LOCAL_REGNO (i))
2308 df_set_regs_ever_live (i, true);
2310 /* We don't DF from now and avoid its using because it is to
2311 expensive when a lot of RTL changes are made. */
2312 df_set_flags (DF_NO_INSN_RESCAN);
2313 lra_constraint_insn_stack.create (get_max_uid ());
2314 lra_constraint_insn_stack_bitmap = sbitmap_alloc (get_max_uid ());
2315 bitmap_clear (lra_constraint_insn_stack_bitmap);
2316 lra_live_ranges_init ();
2317 lra_constraints_init ();
2318 lra_curr_reload_num = 0;
2319 push_insns (get_last_insn (), NULL_RTX);
2320 /* It is needed for the 1st coalescing. */
2321 lra_constraint_new_insn_uid_start = get_max_uid ();
2322 bitmap_initialize (&lra_inheritance_pseudos, &reg_obstack);
2323 bitmap_initialize (&lra_split_regs, &reg_obstack);
2324 bitmap_initialize (&lra_optional_reload_pseudos, &reg_obstack);
2325 bitmap_initialize (&lra_subreg_reload_pseudos, &reg_obstack);
2326 live_p = false;
2327 if (get_frame_size () != 0 && crtl->stack_alignment_needed)
2328 /* If we have a stack frame, we must align it now. The stack size
2329 may be a part of the offset computation for register
2330 elimination. */
2331 assign_stack_local (BLKmode, 0, crtl->stack_alignment_needed);
2332 lra_init_equiv ();
2333 for (;;)
2335 for (;;)
2337 /* We should try to assign hard registers to scratches even
2338 if there were no RTL transformations in
2339 lra_constraints. */
2340 if (! lra_constraints (lra_constraint_iter == 0)
2341 && (lra_constraint_iter > 1
2342 || (! scratch_p && ! caller_save_needed)))
2343 break;
2344 /* Constraint transformations may result in that eliminable
2345 hard regs become uneliminable and pseudos which use them
2346 should be spilled. It is better to do it before pseudo
2347 assignments.
2349 For example, rs6000 can make
2350 RS6000_PIC_OFFSET_TABLE_REGNUM uneliminable if we started
2351 to use a constant pool. */
2352 lra_eliminate (false, false);
2353 /* Do inheritance only for regular algorithms. */
2354 if (! lra_simple_p)
2355 lra_inheritance ();
2356 if (live_p)
2357 lra_clear_live_ranges ();
2358 /* We need live ranges for lra_assign -- so build them. */
2359 lra_create_live_ranges (true);
2360 live_p = true;
2361 /* If we don't spill non-reload and non-inheritance pseudos,
2362 there is no sense to run memory-memory move coalescing.
2363 If inheritance pseudos were spilled, the memory-memory
2364 moves involving them will be removed by pass undoing
2365 inheritance. */
2366 if (lra_simple_p)
2367 lra_assign ();
2368 else
2370 bool spill_p = !lra_assign ();
2372 if (lra_undo_inheritance ())
2373 live_p = false;
2374 if (spill_p)
2376 if (! live_p)
2378 lra_create_live_ranges (true);
2379 live_p = true;
2381 if (lra_coalesce ())
2382 live_p = false;
2384 if (! live_p)
2385 lra_clear_live_ranges ();
2388 /* Don't clear optional reloads bitmap until all constraints are
2389 satisfied as we need to differ them from regular reloads. */
2390 bitmap_clear (&lra_optional_reload_pseudos);
2391 bitmap_clear (&lra_subreg_reload_pseudos);
2392 bitmap_clear (&lra_inheritance_pseudos);
2393 bitmap_clear (&lra_split_regs);
2394 if (! lra_need_for_spills_p ())
2395 break;
2396 if (! live_p)
2398 /* We need full live info for spilling pseudos into
2399 registers instead of memory. */
2400 lra_create_live_ranges (lra_reg_spill_p);
2401 live_p = true;
2403 lra_spill ();
2404 /* Assignment of stack slots changes elimination offsets for
2405 some eliminations. So update the offsets here. */
2406 lra_eliminate (false, false);
2407 lra_constraint_new_regno_start = max_reg_num ();
2408 lra_constraint_new_insn_uid_start = get_max_uid ();
2409 lra_constraint_iter_after_spill = 0;
2411 restore_scratches ();
2412 lra_eliminate (true, false);
2413 lra_final_code_change ();
2414 lra_in_progress = 0;
2415 if (live_p)
2416 lra_clear_live_ranges ();
2417 lra_live_ranges_finish ();
2418 lra_constraints_finish ();
2419 finish_reg_info ();
2420 sbitmap_free (lra_constraint_insn_stack_bitmap);
2421 lra_constraint_insn_stack.release ();
2422 finish_insn_recog_data ();
2423 regstat_free_n_sets_and_refs ();
2424 regstat_free_ri ();
2425 reload_completed = 1;
2426 update_inc_notes ();
2428 inserted_p = fixup_abnormal_edges ();
2430 /* We've possibly turned single trapping insn into multiple ones. */
2431 if (cfun->can_throw_non_call_exceptions)
2433 sbitmap blocks;
2434 blocks = sbitmap_alloc (last_basic_block_for_fn (cfun));
2435 bitmap_ones (blocks);
2436 find_many_sub_basic_blocks (blocks);
2437 sbitmap_free (blocks);
2440 if (inserted_p)
2441 commit_edge_insertions ();
2443 /* Replacing pseudos with their memory equivalents might have
2444 created shared rtx. Subsequent passes would get confused
2445 by this, so unshare everything here. */
2446 unshare_all_rtl_again (get_insns ());
2448 #ifdef ENABLE_CHECKING
2449 check_rtl (true);
2450 #endif
2452 timevar_pop (TV_LRA);
2455 /* Called once per compiler to initialize LRA data once. */
2456 void
2457 lra_init_once (void)
2459 init_insn_code_data_once ();
2462 /* Initialize LRA whenever register-related information is changed. */
2463 void
2464 lra_init (void)
2466 init_op_alt_data ();
2469 /* Called once per compiler to finish LRA data which are initialize
2470 once. */
2471 void
2472 lra_finish_once (void)
2474 finish_insn_code_data_once ();