* go-gcc.cc: #include "langhooks.h".
[official-gcc.git] / gcc / lra.c
blobc1b92d8eee463e15542a5d889f4115fdb1bc8483
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 ();
258 if (have_addptr3_insn (x, y, z))
260 insn = gen_addptr3_insn (x, y, z);
262 /* If the target provides an "addptr" pattern it hopefully does
263 for a reason. So falling back to the normal add would be
264 a bug. */
265 lra_assert (insn != NULL_RTX);
266 emit_insn (insn);
267 return insn;
270 insn = emit_insn (gen_rtx_SET (VOIDmode, x,
271 gen_rtx_PLUS (GET_MODE (y), y, z)));
272 if (recog_memoized (insn) < 0)
274 delete_insns_since (last);
275 insn = NULL_RTX;
277 return insn;
280 /* Emit insn x = x + y. Return the insn. We use gen_add2_insn as the
281 last resort. */
282 static rtx
283 emit_add2_insn (rtx x, rtx y)
285 rtx insn;
287 insn = emit_add3_insn (x, x, y);
288 if (insn == NULL_RTX)
290 insn = gen_add2_insn (x, y);
291 if (insn != NULL_RTX)
292 emit_insn (insn);
294 return insn;
297 /* Target checks operands through operand predicates to recognize an
298 insn. We should have a special precaution to generate add insns
299 which are frequent results of elimination.
301 Emit insns for x = y + z. X can be used to store intermediate
302 values and should be not in Y and Z when we use X to store an
303 intermediate value. Y + Z should form [base] [+ index[ * scale]] [
304 + disp] where base and index are registers, disp and scale are
305 constants. Y should contain base if it is present, Z should
306 contain disp if any. index[*scale] can be part of Y or Z. */
307 void
308 lra_emit_add (rtx x, rtx y, rtx z)
310 int old;
311 rtx insn, last;
312 rtx a1, a2, base, index, disp, scale, index_scale;
313 bool ok_p;
315 insn = emit_add3_insn (x, y, z);
316 old = max_reg_num ();
317 if (insn != NULL_RTX)
319 else
321 disp = a2 = NULL_RTX;
322 if (GET_CODE (y) == PLUS)
324 a1 = XEXP (y, 0);
325 a2 = XEXP (y, 1);
326 disp = z;
328 else
330 a1 = y;
331 if (CONSTANT_P (z))
332 disp = z;
333 else
334 a2 = z;
336 index_scale = scale = NULL_RTX;
337 if (GET_CODE (a1) == MULT)
339 index_scale = a1;
340 index = XEXP (a1, 0);
341 scale = XEXP (a1, 1);
342 base = a2;
344 else if (a2 != NULL_RTX && GET_CODE (a2) == MULT)
346 index_scale = a2;
347 index = XEXP (a2, 0);
348 scale = XEXP (a2, 1);
349 base = a1;
351 else
353 base = a1;
354 index = a2;
356 if (! (REG_P (base) || GET_CODE (base) == SUBREG)
357 || (index != NULL_RTX
358 && ! (REG_P (index) || GET_CODE (index) == SUBREG))
359 || (disp != NULL_RTX && ! CONSTANT_P (disp))
360 || (scale != NULL_RTX && ! CONSTANT_P (scale)))
362 /* Probably we have no 3 op add. Last chance is to use 2-op
363 add insn. To succeed, don't move Z to X as an address
364 segment always comes in Y. Otherwise, we might fail when
365 adding the address segment to register. */
366 lra_assert (x != y && x != z);
367 emit_move_insn (x, y);
368 insn = emit_add2_insn (x, z);
369 lra_assert (insn != NULL_RTX);
371 else
373 if (index_scale == NULL_RTX)
374 index_scale = index;
375 if (disp == NULL_RTX)
377 /* Generate x = index_scale; x = x + base. */
378 lra_assert (index_scale != NULL_RTX && base != NULL_RTX);
379 emit_move_insn (x, index_scale);
380 insn = emit_add2_insn (x, base);
381 lra_assert (insn != NULL_RTX);
383 else if (scale == NULL_RTX)
385 /* Try x = base + disp. */
386 lra_assert (base != NULL_RTX);
387 last = get_last_insn ();
388 insn = emit_move_insn (x, gen_rtx_PLUS (GET_MODE (base),
389 base, disp));
390 if (recog_memoized (insn) < 0)
392 delete_insns_since (last);
393 /* Generate x = disp; x = x + base. */
394 emit_move_insn (x, disp);
395 insn = emit_add2_insn (x, base);
396 lra_assert (insn != NULL_RTX);
398 /* Generate x = x + index. */
399 if (index != NULL_RTX)
401 insn = emit_add2_insn (x, index);
402 lra_assert (insn != NULL_RTX);
405 else
407 /* Try x = index_scale; x = x + disp; x = x + base. */
408 last = get_last_insn ();
409 insn = emit_move_insn (x, index_scale);
410 ok_p = false;
411 if (recog_memoized (insn) >= 0)
413 insn = emit_add2_insn (x, disp);
414 if (insn != NULL_RTX)
416 insn = emit_add2_insn (x, disp);
417 if (insn != NULL_RTX)
418 ok_p = true;
421 if (! ok_p)
423 delete_insns_since (last);
424 /* Generate x = disp; x = x + base; x = x + index_scale. */
425 emit_move_insn (x, disp);
426 insn = emit_add2_insn (x, base);
427 lra_assert (insn != NULL_RTX);
428 insn = emit_add2_insn (x, index_scale);
429 lra_assert (insn != NULL_RTX);
434 /* Functions emit_... can create pseudos -- so expand the pseudo
435 data. */
436 if (old != max_reg_num ())
437 expand_reg_data (old);
440 /* The number of emitted reload insns so far. */
441 int lra_curr_reload_num;
443 /* Emit x := y, processing special case when y = u + v or y = u + v *
444 scale + w through emit_add (Y can be an address which is base +
445 index reg * scale + displacement in general case). X may be used
446 as intermediate result therefore it should be not in Y. */
447 void
448 lra_emit_move (rtx x, rtx y)
450 int old;
452 if (GET_CODE (y) != PLUS)
454 if (rtx_equal_p (x, y))
455 return;
456 old = max_reg_num ();
457 emit_move_insn (x, y);
458 if (REG_P (x))
459 lra_reg_info[ORIGINAL_REGNO (x)].last_reload = ++lra_curr_reload_num;
460 /* Function emit_move can create pseudos -- so expand the pseudo
461 data. */
462 if (old != max_reg_num ())
463 expand_reg_data (old);
464 return;
466 lra_emit_add (x, XEXP (y, 0), XEXP (y, 1));
469 /* Update insn operands which are duplication of operands whose
470 numbers are in array of NOPS (with end marker -1). The insn is
471 represented by its LRA internal representation ID. */
472 void
473 lra_update_dups (lra_insn_recog_data_t id, signed char *nops)
475 int i, j, nop;
476 struct lra_static_insn_data *static_id = id->insn_static_data;
478 for (i = 0; i < static_id->n_dups; i++)
479 for (j = 0; (nop = nops[j]) >= 0; j++)
480 if (static_id->dup_num[i] == nop)
481 *id->dup_loc[i] = *id->operand_loc[nop];
486 /* This page contains code dealing with info about registers in the
487 insns. */
489 /* Pools for insn reg info. */
490 static alloc_pool insn_reg_pool;
492 /* Initiate pool for insn reg info. */
493 static void
494 init_insn_regs (void)
496 insn_reg_pool
497 = create_alloc_pool ("insn regs", sizeof (struct lra_insn_reg), 100);
500 /* Create LRA insn related info about a reference to REGNO in INSN with
501 TYPE (in/out/inout), biggest reference mode MODE, flag that it is
502 reference through subreg (SUBREG_P), flag that is early clobbered
503 in the insn (EARLY_CLOBBER), and reference to the next insn reg
504 info (NEXT). */
505 static struct lra_insn_reg *
506 new_insn_reg (rtx insn, int regno, enum op_type type, enum machine_mode mode,
507 bool subreg_p, bool early_clobber, struct lra_insn_reg *next)
509 struct lra_insn_reg *ir;
511 ir = (struct lra_insn_reg *) pool_alloc (insn_reg_pool);
512 ir->type = type;
513 ir->biggest_mode = mode;
514 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (lra_reg_info[regno].biggest_mode)
515 && NONDEBUG_INSN_P (insn))
516 lra_reg_info[regno].biggest_mode = mode;
517 ir->subreg_p = subreg_p;
518 ir->early_clobber = early_clobber;
519 ir->regno = regno;
520 ir->next = next;
521 return ir;
524 /* Free insn reg info IR. */
525 static void
526 free_insn_reg (struct lra_insn_reg *ir)
528 pool_free (insn_reg_pool, ir);
531 /* Free insn reg info list IR. */
532 static void
533 free_insn_regs (struct lra_insn_reg *ir)
535 struct lra_insn_reg *next_ir;
537 for (; ir != NULL; ir = next_ir)
539 next_ir = ir->next;
540 free_insn_reg (ir);
544 /* Finish pool for insn reg info. */
545 static void
546 finish_insn_regs (void)
548 free_alloc_pool (insn_reg_pool);
553 /* This page contains code dealing LRA insn info (or in other words
554 LRA internal insn representation). */
556 struct target_lra_int default_target_lra_int;
557 #if SWITCHABLE_TARGET
558 struct target_lra_int *this_target_lra_int = &default_target_lra_int;
559 #endif
561 /* Map INSN_CODE -> the static insn data. This info is valid during
562 all translation unit. */
563 struct lra_static_insn_data *insn_code_data[LAST_INSN_CODE];
565 /* Debug insns are represented as a special insn with one input
566 operand which is RTL expression in var_location. */
568 /* The following data are used as static insn operand data for all
569 debug insns. If structure lra_operand_data is changed, the
570 initializer should be changed too. */
571 static struct lra_operand_data debug_operand_data =
573 NULL, /* alternative */
574 VOIDmode, /* We are not interesting in the operand mode. */
575 OP_IN,
576 0, 0, 0, 0
579 /* The following data are used as static insn data for all debug
580 insns. If structure lra_static_insn_data is changed, the
581 initializer should be changed too. */
582 static struct lra_static_insn_data debug_insn_static_data =
584 &debug_operand_data,
585 0, /* Duplication operands #. */
586 -1, /* Commutative operand #. */
587 1, /* Operands #. There is only one operand which is debug RTL
588 expression. */
589 0, /* Duplications #. */
590 0, /* Alternatives #. We are not interesting in alternatives
591 because we does not proceed debug_insns for reloads. */
592 NULL, /* Hard registers referenced in machine description. */
593 NULL /* Descriptions of operands in alternatives. */
596 /* Called once per compiler work to initialize some LRA data related
597 to insns. */
598 static void
599 init_insn_code_data_once (void)
601 memset (insn_code_data, 0, sizeof (insn_code_data));
602 memset (op_alt_data, 0, sizeof (op_alt_data));
605 /* Called once per compiler work to finalize some LRA data related to
606 insns. */
607 static void
608 finish_insn_code_data_once (void)
610 int i;
612 for (i = 0; i < LAST_INSN_CODE; i++)
614 if (insn_code_data[i] != NULL)
615 free (insn_code_data[i]);
616 if (op_alt_data[i] != NULL)
617 free (op_alt_data[i]);
621 /* Initialize LRA info about operands in insn alternatives. */
622 static void
623 init_op_alt_data (void)
625 int i;
627 for (i = 0; i < LAST_INSN_CODE; i++)
628 if (op_alt_data[i] != NULL)
630 free (op_alt_data[i]);
631 op_alt_data[i] = NULL;
635 /* Return static insn data, allocate and setup if necessary. Although
636 dup_num is static data (it depends only on icode), to set it up we
637 need to extract insn first. So recog_data should be valid for
638 normal insn (ICODE >= 0) before the call. */
639 static struct lra_static_insn_data *
640 get_static_insn_data (int icode, int nop, int ndup, int nalt)
642 struct lra_static_insn_data *data;
643 size_t n_bytes;
645 lra_assert (icode < LAST_INSN_CODE);
646 if (icode >= 0 && (data = insn_code_data[icode]) != NULL)
647 return data;
648 lra_assert (nop >= 0 && ndup >= 0 && nalt >= 0);
649 n_bytes = sizeof (struct lra_static_insn_data)
650 + sizeof (struct lra_operand_data) * nop
651 + sizeof (int) * ndup;
652 data = XNEWVAR (struct lra_static_insn_data, n_bytes);
653 data->n_operands = nop;
654 data->n_dups = ndup;
655 data->n_alternatives = nalt;
656 data->operand = ((struct lra_operand_data *)
657 ((char *) data + sizeof (struct lra_static_insn_data)));
658 data->dup_num = ((int *) ((char *) data->operand
659 + sizeof (struct lra_operand_data) * nop));
660 if (icode >= 0)
662 int i;
664 insn_code_data[icode] = data;
665 for (i = 0; i < nop; i++)
667 data->operand[i].constraint
668 = insn_data[icode].operand[i].constraint;
669 data->operand[i].mode = insn_data[icode].operand[i].mode;
670 data->operand[i].strict_low = insn_data[icode].operand[i].strict_low;
671 data->operand[i].is_operator
672 = insn_data[icode].operand[i].is_operator;
673 data->operand[i].type
674 = (data->operand[i].constraint[0] == '=' ? OP_OUT
675 : data->operand[i].constraint[0] == '+' ? OP_INOUT
676 : OP_IN);
677 data->operand[i].is_address = false;
679 for (i = 0; i < ndup; i++)
680 data->dup_num[i] = recog_data.dup_num[i];
682 return data;
685 /* The current length of the following array. */
686 int lra_insn_recog_data_len;
688 /* Map INSN_UID -> the insn recog data (NULL if unknown). */
689 lra_insn_recog_data_t *lra_insn_recog_data;
691 /* Initialize LRA data about insns. */
692 static void
693 init_insn_recog_data (void)
695 lra_insn_recog_data_len = 0;
696 lra_insn_recog_data = NULL;
697 init_insn_regs ();
700 /* Expand, if necessary, LRA data about insns. */
701 static void
702 check_and_expand_insn_recog_data (int index)
704 int i, old;
706 if (lra_insn_recog_data_len > index)
707 return;
708 old = lra_insn_recog_data_len;
709 lra_insn_recog_data_len = index * 3 / 2 + 1;
710 lra_insn_recog_data = XRESIZEVEC (lra_insn_recog_data_t,
711 lra_insn_recog_data,
712 lra_insn_recog_data_len);
713 for (i = old; i < lra_insn_recog_data_len; i++)
714 lra_insn_recog_data[i] = NULL;
717 /* Finish LRA DATA about insn. */
718 static void
719 free_insn_recog_data (lra_insn_recog_data_t data)
721 if (data->operand_loc != NULL)
722 free (data->operand_loc);
723 if (data->dup_loc != NULL)
724 free (data->dup_loc);
725 if (data->arg_hard_regs != NULL)
726 free (data->arg_hard_regs);
727 if (HAVE_ATTR_enabled && data->alternative_enabled_p != NULL)
728 free (data->alternative_enabled_p);
729 if (data->icode < 0 && NONDEBUG_INSN_P (data->insn))
731 if (data->insn_static_data->operand_alternative != NULL)
732 free (data->insn_static_data->operand_alternative);
733 free_insn_regs (data->insn_static_data->hard_regs);
734 free (data->insn_static_data);
736 free_insn_regs (data->regs);
737 data->regs = NULL;
738 free (data);
741 /* Finish LRA data about all insns. */
742 static void
743 finish_insn_recog_data (void)
745 int i;
746 lra_insn_recog_data_t data;
748 for (i = 0; i < lra_insn_recog_data_len; i++)
749 if ((data = lra_insn_recog_data[i]) != NULL)
750 free_insn_recog_data (data);
751 finish_insn_regs ();
752 free (lra_insn_recog_data);
755 /* Setup info about operands in alternatives of LRA DATA of insn. */
756 static void
757 setup_operand_alternative (lra_insn_recog_data_t data)
759 int i, nop, nalt;
760 int icode = data->icode;
761 struct lra_static_insn_data *static_data = data->insn_static_data;
763 if (icode >= 0
764 && (static_data->operand_alternative = op_alt_data[icode]) != NULL)
765 return;
766 static_data->commutative = -1;
767 nop = static_data->n_operands;
768 if (nop == 0)
770 static_data->operand_alternative = NULL;
771 return;
773 nalt = static_data->n_alternatives;
774 static_data->operand_alternative = XNEWVEC (struct operand_alternative,
775 nalt * nop);
776 memset (static_data->operand_alternative, 0,
777 nalt * nop * sizeof (struct operand_alternative));
778 if (icode >= 0)
779 op_alt_data[icode] = static_data->operand_alternative;
780 for (i = 0; i < nop; i++)
782 int j;
783 struct operand_alternative *op_alt_start, *op_alt;
784 const char *p = static_data->operand[i].constraint;
786 static_data->operand[i].early_clobber = 0;
787 op_alt_start = &static_data->operand_alternative[i];
789 for (j = 0; j < nalt; j++)
791 op_alt = op_alt_start + j * nop;
792 op_alt->cl = NO_REGS;
793 op_alt->constraint = p;
794 op_alt->matches = -1;
795 op_alt->matched = -1;
797 if (*p == '\0' || *p == ',')
799 op_alt->anything_ok = 1;
800 continue;
803 for (;;)
805 char c = *p;
806 if (c == '#')
808 c = *++p;
809 while (c != ',' && c != '\0');
810 if (c == ',' || c == '\0')
812 p++;
813 break;
816 switch (c)
818 case '=': case '+': case '*':
819 case 'E': case 'F': case 'G': case 'H':
820 case 's': case 'i': case 'n':
821 case 'I': case 'J': case 'K': case 'L':
822 case 'M': case 'N': case 'O': case 'P':
823 /* These don't say anything we care about. */
824 break;
826 case '%':
827 /* We currently only support one commutative pair of
828 operands. */
829 if (static_data->commutative < 0)
830 static_data->commutative = i;
831 else
832 lra_assert (data->icode < 0); /* Asm */
834 /* The last operand should not be marked
835 commutative. */
836 lra_assert (i != nop - 1);
837 break;
839 case '?':
840 op_alt->reject += LRA_LOSER_COST_FACTOR;
841 break;
842 case '!':
843 op_alt->reject += LRA_MAX_REJECT;
844 break;
845 case '&':
846 op_alt->earlyclobber = 1;
847 static_data->operand[i].early_clobber = 1;
848 break;
850 case '0': case '1': case '2': case '3': case '4':
851 case '5': case '6': case '7': case '8': case '9':
853 char *end;
854 op_alt->matches = strtoul (p, &end, 10);
855 static_data->operand_alternative
856 [j * nop + op_alt->matches].matched = i;
857 p = end;
859 continue;
861 case TARGET_MEM_CONSTRAINT:
862 op_alt->memory_ok = 1;
863 break;
864 case '<':
865 op_alt->decmem_ok = 1;
866 break;
867 case '>':
868 op_alt->incmem_ok = 1;
869 break;
870 case 'V':
871 op_alt->nonoffmem_ok = 1;
872 break;
873 case 'o':
874 op_alt->offmem_ok = 1;
875 break;
876 case 'X':
877 op_alt->anything_ok = 1;
878 break;
880 case 'p':
881 static_data->operand[i].is_address = true;
882 op_alt->is_address = 1;
883 op_alt->cl = (reg_class_subunion[(int) op_alt->cl]
884 [(int) base_reg_class (VOIDmode,
885 ADDR_SPACE_GENERIC,
886 ADDRESS, SCRATCH)]);
887 break;
889 case 'g':
890 case 'r':
891 op_alt->cl =
892 reg_class_subunion[(int) op_alt->cl][(int) GENERAL_REGS];
893 break;
895 default:
896 if (EXTRA_MEMORY_CONSTRAINT (c, p))
898 op_alt->memory_ok = 1;
899 break;
901 if (EXTRA_ADDRESS_CONSTRAINT (c, p))
903 static_data->operand[i].is_address = true;
904 op_alt->is_address = 1;
905 op_alt->cl
906 = (reg_class_subunion
907 [(int) op_alt->cl]
908 [(int) base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
909 ADDRESS, SCRATCH)]);
910 break;
913 op_alt->cl
914 = (reg_class_subunion
915 [(int) op_alt->cl]
916 [(int)
917 REG_CLASS_FROM_CONSTRAINT ((unsigned char) c, p)]);
918 break;
920 p += CONSTRAINT_LEN (c, p);
926 /* Recursively process X and collect info about registers, which are
927 not the insn operands, in X with TYPE (in/out/inout) and flag that
928 it is early clobbered in the insn (EARLY_CLOBBER) and add the info
929 to LIST. X is a part of insn given by DATA. Return the result
930 list. */
931 static struct lra_insn_reg *
932 collect_non_operand_hard_regs (rtx *x, lra_insn_recog_data_t data,
933 struct lra_insn_reg *list,
934 enum op_type type, bool early_clobber)
936 int i, j, regno, last;
937 bool subreg_p;
938 enum machine_mode mode;
939 struct lra_insn_reg *curr;
940 rtx op = *x;
941 enum rtx_code code = GET_CODE (op);
942 const char *fmt = GET_RTX_FORMAT (code);
944 for (i = 0; i < data->insn_static_data->n_operands; i++)
945 if (x == data->operand_loc[i])
946 /* It is an operand loc. Stop here. */
947 return list;
948 for (i = 0; i < data->insn_static_data->n_dups; i++)
949 if (x == data->dup_loc[i])
950 /* It is a dup loc. Stop here. */
951 return list;
952 mode = GET_MODE (op);
953 subreg_p = false;
954 if (code == SUBREG)
956 op = SUBREG_REG (op);
957 code = GET_CODE (op);
958 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (op)))
960 mode = GET_MODE (op);
961 if (GET_MODE_SIZE (mode) > REGMODE_NATURAL_SIZE (mode))
962 subreg_p = true;
965 if (REG_P (op))
967 if ((regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER)
968 return list;
969 for (last = regno + hard_regno_nregs[regno][mode];
970 regno < last;
971 regno++)
972 if (! TEST_HARD_REG_BIT (lra_no_alloc_regs, regno)
973 || TEST_HARD_REG_BIT (eliminable_regset, regno))
975 for (curr = list; curr != NULL; curr = curr->next)
976 if (curr->regno == regno && curr->subreg_p == subreg_p
977 && curr->biggest_mode == mode)
979 if (curr->type != type)
980 curr->type = OP_INOUT;
981 if (curr->early_clobber != early_clobber)
982 curr->early_clobber = true;
983 break;
985 if (curr == NULL)
987 /* This is a new hard regno or the info can not be
988 integrated into the found structure. */
989 #ifdef STACK_REGS
990 early_clobber
991 = (early_clobber
992 /* This clobber is to inform popping floating
993 point stack only. */
994 && ! (FIRST_STACK_REG <= regno
995 && regno <= LAST_STACK_REG));
996 #endif
997 list = new_insn_reg (data->insn, regno, type, mode, subreg_p,
998 early_clobber, list);
1001 return list;
1003 switch (code)
1005 case SET:
1006 list = collect_non_operand_hard_regs (&SET_DEST (op), data,
1007 list, OP_OUT, false);
1008 list = collect_non_operand_hard_regs (&SET_SRC (op), data,
1009 list, OP_IN, false);
1010 break;
1011 case CLOBBER:
1012 /* We treat clobber of non-operand hard registers as early
1013 clobber (the behavior is expected from asm). */
1014 list = collect_non_operand_hard_regs (&XEXP (op, 0), data,
1015 list, OP_OUT, true);
1016 break;
1017 case PRE_INC: case PRE_DEC: case POST_INC: case POST_DEC:
1018 list = collect_non_operand_hard_regs (&XEXP (op, 0), data,
1019 list, OP_INOUT, false);
1020 break;
1021 case PRE_MODIFY: case POST_MODIFY:
1022 list = collect_non_operand_hard_regs (&XEXP (op, 0), data,
1023 list, OP_INOUT, false);
1024 list = collect_non_operand_hard_regs (&XEXP (op, 1), data,
1025 list, OP_IN, false);
1026 break;
1027 default:
1028 fmt = GET_RTX_FORMAT (code);
1029 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1031 if (fmt[i] == 'e')
1032 list = collect_non_operand_hard_regs (&XEXP (op, i), data,
1033 list, OP_IN, false);
1034 else if (fmt[i] == 'E')
1035 for (j = XVECLEN (op, i) - 1; j >= 0; j--)
1036 list = collect_non_operand_hard_regs (&XVECEXP (op, i, j), data,
1037 list, OP_IN, false);
1040 return list;
1043 /* Set up and return info about INSN. Set up the info if it is not set up
1044 yet. */
1045 lra_insn_recog_data_t
1046 lra_set_insn_recog_data (rtx insn)
1048 lra_insn_recog_data_t data;
1049 int i, n, icode;
1050 rtx **locs;
1051 unsigned int uid = INSN_UID (insn);
1052 struct lra_static_insn_data *insn_static_data;
1054 check_and_expand_insn_recog_data (uid);
1055 if (DEBUG_INSN_P (insn))
1056 icode = -1;
1057 else
1059 icode = INSN_CODE (insn);
1060 if (icode < 0)
1061 /* It might be a new simple insn which is not recognized yet. */
1062 INSN_CODE (insn) = icode = recog_memoized (insn);
1064 data = XNEW (struct lra_insn_recog_data);
1065 lra_insn_recog_data[uid] = data;
1066 data->insn = insn;
1067 data->used_insn_alternative = -1;
1068 data->icode = icode;
1069 data->regs = NULL;
1070 if (DEBUG_INSN_P (insn))
1072 data->insn_static_data = &debug_insn_static_data;
1073 data->dup_loc = NULL;
1074 data->arg_hard_regs = NULL;
1075 data->alternative_enabled_p = NULL;
1076 data->operand_loc = XNEWVEC (rtx *, 1);
1077 data->operand_loc[0] = &INSN_VAR_LOCATION_LOC (insn);
1078 return data;
1080 if (icode < 0)
1082 int nop;
1083 enum machine_mode operand_mode[MAX_RECOG_OPERANDS];
1084 const char *constraints[MAX_RECOG_OPERANDS];
1086 nop = asm_noperands (PATTERN (insn));
1087 data->operand_loc = data->dup_loc = NULL;
1088 if (nop < 0)
1090 /* Its is a special insn like USE or CLOBBER. We should
1091 recognize any regular insn otherwise LRA can do nothing
1092 with this insn. */
1093 gcc_assert (GET_CODE (PATTERN (insn)) == USE
1094 || GET_CODE (PATTERN (insn)) == CLOBBER
1095 || GET_CODE (PATTERN (insn)) == ASM_INPUT);
1096 data->insn_static_data = insn_static_data
1097 = get_static_insn_data (-1, 0, 0, 1);
1099 else
1101 /* expand_asm_operands makes sure there aren't too many
1102 operands. */
1103 lra_assert (nop <= MAX_RECOG_OPERANDS);
1104 if (nop != 0)
1105 data->operand_loc = XNEWVEC (rtx *, nop);
1106 /* Now get the operand values and constraints out of the
1107 insn. */
1108 decode_asm_operands (PATTERN (insn), NULL,
1109 data->operand_loc,
1110 constraints, operand_mode, NULL);
1111 n = 1;
1112 if (nop > 0)
1114 const char *p = recog_data.constraints[0];
1116 for (p = constraints[0]; *p; p++)
1117 n += *p == ',';
1119 data->insn_static_data = insn_static_data
1120 = get_static_insn_data (-1, nop, 0, n);
1121 for (i = 0; i < nop; i++)
1123 insn_static_data->operand[i].mode = operand_mode[i];
1124 insn_static_data->operand[i].constraint = constraints[i];
1125 insn_static_data->operand[i].strict_low = false;
1126 insn_static_data->operand[i].is_operator = false;
1127 insn_static_data->operand[i].is_address = false;
1130 for (i = 0; i < insn_static_data->n_operands; i++)
1131 insn_static_data->operand[i].type
1132 = (insn_static_data->operand[i].constraint[0] == '=' ? OP_OUT
1133 : insn_static_data->operand[i].constraint[0] == '+' ? OP_INOUT
1134 : OP_IN);
1135 data->alternative_enabled_p = NULL;
1137 else
1139 insn_extract (insn);
1140 data->insn_static_data = insn_static_data
1141 = get_static_insn_data (icode, insn_data[icode].n_operands,
1142 insn_data[icode].n_dups,
1143 insn_data[icode].n_alternatives);
1144 n = insn_static_data->n_operands;
1145 if (n == 0)
1146 locs = NULL;
1147 else
1149 locs = XNEWVEC (rtx *, n);
1150 memcpy (locs, recog_data.operand_loc, n * sizeof (rtx *));
1152 data->operand_loc = locs;
1153 n = insn_static_data->n_dups;
1154 if (n == 0)
1155 locs = NULL;
1156 else
1158 locs = XNEWVEC (rtx *, n);
1159 memcpy (locs, recog_data.dup_loc, n * sizeof (rtx *));
1161 data->dup_loc = locs;
1162 if (HAVE_ATTR_enabled)
1164 bool *bp;
1166 n = insn_static_data->n_alternatives;
1167 lra_assert (n >= 0);
1168 data->alternative_enabled_p = bp = XNEWVEC (bool, n);
1169 /* Cache the insn because we don't want to call extract_insn
1170 from get_attr_enabled as extract_insn modifies
1171 which_alternative. The attribute enabled should not depend
1172 on insn operands, operand modes, operand types, and operand
1173 constraints. It should depend on the architecture. If it
1174 is not true, we should rewrite this file code to use
1175 extract_insn instead of less expensive insn_extract. */
1176 recog_data.insn = insn;
1177 for (i = 0; i < n; i++)
1179 which_alternative = i;
1180 bp[i] = get_attr_enabled (insn);
1184 if (GET_CODE (PATTERN (insn)) == CLOBBER || GET_CODE (PATTERN (insn)) == USE)
1185 insn_static_data->hard_regs = NULL;
1186 else
1187 insn_static_data->hard_regs
1188 = collect_non_operand_hard_regs (&PATTERN (insn), data,
1189 NULL, OP_IN, false);
1190 setup_operand_alternative (data);
1191 data->arg_hard_regs = NULL;
1192 if (CALL_P (insn))
1194 rtx link;
1195 int n_hard_regs, regno, arg_hard_regs[FIRST_PSEUDO_REGISTER];
1197 n_hard_regs = 0;
1198 /* Finding implicit hard register usage. We believe it will be
1199 not changed whatever transformations are used. Call insns
1200 are such example. */
1201 for (link = CALL_INSN_FUNCTION_USAGE (insn);
1202 link != NULL_RTX;
1203 link = XEXP (link, 1))
1204 if (GET_CODE (XEXP (link, 0)) == USE
1205 && REG_P (XEXP (XEXP (link, 0), 0)))
1207 regno = REGNO (XEXP (XEXP (link, 0), 0));
1208 lra_assert (regno < FIRST_PSEUDO_REGISTER);
1209 /* It is an argument register. */
1210 for (i = (hard_regno_nregs
1211 [regno][GET_MODE (XEXP (XEXP (link, 0), 0))]) - 1;
1212 i >= 0;
1213 i--)
1214 arg_hard_regs[n_hard_regs++] = regno + i;
1216 if (n_hard_regs != 0)
1218 arg_hard_regs[n_hard_regs++] = -1;
1219 data->arg_hard_regs = XNEWVEC (int, n_hard_regs);
1220 memcpy (data->arg_hard_regs, arg_hard_regs,
1221 sizeof (int) * n_hard_regs);
1224 /* Some output operand can be recognized only from the context not
1225 from the constraints which are empty in this case. Call insn may
1226 contain a hard register in set destination with empty constraint
1227 and extract_insn treats them as an input. */
1228 for (i = 0; i < insn_static_data->n_operands; i++)
1230 int j;
1231 rtx pat, set;
1232 struct lra_operand_data *operand = &insn_static_data->operand[i];
1234 /* ??? Should we treat 'X' the same way. It looks to me that
1235 'X' means anything and empty constraint means we do not
1236 care. */
1237 if (operand->type != OP_IN || *operand->constraint != '\0'
1238 || operand->is_operator)
1239 continue;
1240 pat = PATTERN (insn);
1241 if (GET_CODE (pat) == SET)
1243 if (data->operand_loc[i] != &SET_DEST (pat))
1244 continue;
1246 else if (GET_CODE (pat) == PARALLEL)
1248 for (j = XVECLEN (pat, 0) - 1; j >= 0; j--)
1250 set = XVECEXP (PATTERN (insn), 0, j);
1251 if (GET_CODE (set) == SET
1252 && &SET_DEST (set) == data->operand_loc[i])
1253 break;
1255 if (j < 0)
1256 continue;
1258 else
1259 continue;
1260 operand->type = OP_OUT;
1262 return data;
1265 /* Return info about insn give by UID. The info should be already set
1266 up. */
1267 static lra_insn_recog_data_t
1268 get_insn_recog_data_by_uid (int uid)
1270 lra_insn_recog_data_t data;
1272 data = lra_insn_recog_data[uid];
1273 lra_assert (data != NULL);
1274 return data;
1277 /* Invalidate all info about insn given by its UID. */
1278 static void
1279 invalidate_insn_recog_data (int uid)
1281 lra_insn_recog_data_t data;
1283 data = lra_insn_recog_data[uid];
1284 lra_assert (data != NULL);
1285 free_insn_recog_data (data);
1286 lra_insn_recog_data[uid] = NULL;
1289 /* Update all the insn info about INSN. It is usually called when
1290 something in the insn was changed. Return the updated info. */
1291 lra_insn_recog_data_t
1292 lra_update_insn_recog_data (rtx insn)
1294 lra_insn_recog_data_t data;
1295 int n;
1296 unsigned int uid = INSN_UID (insn);
1297 struct lra_static_insn_data *insn_static_data;
1298 HOST_WIDE_INT sp_offset = 0;
1300 check_and_expand_insn_recog_data (uid);
1301 if ((data = lra_insn_recog_data[uid]) != NULL
1302 && data->icode != INSN_CODE (insn))
1304 sp_offset = data->sp_offset;
1305 invalidate_insn_data_regno_info (data, insn, get_insn_freq (insn));
1306 invalidate_insn_recog_data (uid);
1307 data = NULL;
1309 if (data == NULL)
1311 data = lra_get_insn_recog_data (insn);
1312 /* Initiate or restore SP offset. */
1313 data->sp_offset = sp_offset;
1314 return data;
1316 insn_static_data = data->insn_static_data;
1317 data->used_insn_alternative = -1;
1318 if (DEBUG_INSN_P (insn))
1319 return data;
1320 if (data->icode < 0)
1322 int nop;
1323 enum machine_mode operand_mode[MAX_RECOG_OPERANDS];
1324 const char *constraints[MAX_RECOG_OPERANDS];
1326 nop = asm_noperands (PATTERN (insn));
1327 if (nop >= 0)
1329 lra_assert (nop == data->insn_static_data->n_operands);
1330 /* Now get the operand values and constraints out of the
1331 insn. */
1332 decode_asm_operands (PATTERN (insn), NULL,
1333 data->operand_loc,
1334 constraints, operand_mode, NULL);
1335 #ifdef ENABLE_CHECKING
1337 int i;
1339 for (i = 0; i < nop; i++)
1340 lra_assert
1341 (insn_static_data->operand[i].mode == operand_mode[i]
1342 && insn_static_data->operand[i].constraint == constraints[i]
1343 && ! insn_static_data->operand[i].is_operator);
1345 #endif
1347 #ifdef ENABLE_CHECKING
1349 int i;
1351 for (i = 0; i < insn_static_data->n_operands; i++)
1352 lra_assert
1353 (insn_static_data->operand[i].type
1354 == (insn_static_data->operand[i].constraint[0] == '=' ? OP_OUT
1355 : insn_static_data->operand[i].constraint[0] == '+' ? OP_INOUT
1356 : OP_IN));
1358 #endif
1360 else
1362 insn_extract (insn);
1363 n = insn_static_data->n_operands;
1364 if (n != 0)
1365 memcpy (data->operand_loc, recog_data.operand_loc, n * sizeof (rtx *));
1366 n = insn_static_data->n_dups;
1367 if (n != 0)
1368 memcpy (data->dup_loc, recog_data.dup_loc, n * sizeof (rtx *));
1369 #if HAVE_ATTR_enabled
1370 #ifdef ENABLE_CHECKING
1372 int i;
1373 bool *bp;
1375 n = insn_static_data->n_alternatives;
1376 bp = data->alternative_enabled_p;
1377 lra_assert (n >= 0 && bp != NULL);
1378 /* Cache the insn to prevent extract_insn call from
1379 get_attr_enabled. */
1380 recog_data.insn = insn;
1381 for (i = 0; i < n; i++)
1383 which_alternative = i;
1384 lra_assert (bp[i] == get_attr_enabled (insn));
1387 #endif
1388 #endif
1390 return data;
1393 /* Set up that INSN is using alternative ALT now. */
1394 void
1395 lra_set_used_insn_alternative (rtx insn, int alt)
1397 lra_insn_recog_data_t data;
1399 data = lra_get_insn_recog_data (insn);
1400 data->used_insn_alternative = alt;
1403 /* Set up that insn with UID is using alternative ALT now. The insn
1404 info should be already set up. */
1405 void
1406 lra_set_used_insn_alternative_by_uid (int uid, int alt)
1408 lra_insn_recog_data_t data;
1410 check_and_expand_insn_recog_data (uid);
1411 data = lra_insn_recog_data[uid];
1412 lra_assert (data != NULL);
1413 data->used_insn_alternative = alt;
1418 /* This page contains code dealing with common register info and
1419 pseudo copies. */
1421 /* The size of the following array. */
1422 static int reg_info_size;
1423 /* Common info about each register. */
1424 struct lra_reg *lra_reg_info;
1426 /* Last register value. */
1427 static int last_reg_value;
1429 /* Return new register value. */
1430 static int
1431 get_new_reg_value (void)
1433 return ++last_reg_value;
1436 /* Pools for copies. */
1437 static alloc_pool copy_pool;
1439 /* Vec referring to pseudo copies. */
1440 static vec<lra_copy_t> copy_vec;
1442 /* Initialize I-th element of lra_reg_info. */
1443 static inline void
1444 initialize_lra_reg_info_element (int i)
1446 bitmap_initialize (&lra_reg_info[i].insn_bitmap, &reg_obstack);
1447 #ifdef STACK_REGS
1448 lra_reg_info[i].no_stack_p = false;
1449 #endif
1450 CLEAR_HARD_REG_SET (lra_reg_info[i].conflict_hard_regs);
1451 lra_reg_info[i].preferred_hard_regno1 = -1;
1452 lra_reg_info[i].preferred_hard_regno2 = -1;
1453 lra_reg_info[i].preferred_hard_regno_profit1 = 0;
1454 lra_reg_info[i].preferred_hard_regno_profit2 = 0;
1455 lra_reg_info[i].biggest_mode = VOIDmode;
1456 lra_reg_info[i].live_ranges = NULL;
1457 lra_reg_info[i].nrefs = lra_reg_info[i].freq = 0;
1458 lra_reg_info[i].last_reload = 0;
1459 lra_reg_info[i].restore_regno = -1;
1460 lra_reg_info[i].val = get_new_reg_value ();
1461 lra_reg_info[i].offset = 0;
1462 lra_reg_info[i].copies = NULL;
1465 /* Initialize common reg info and copies. */
1466 static void
1467 init_reg_info (void)
1469 int i;
1471 last_reg_value = 0;
1472 reg_info_size = max_reg_num () * 3 / 2 + 1;
1473 lra_reg_info = XNEWVEC (struct lra_reg, reg_info_size);
1474 for (i = 0; i < reg_info_size; i++)
1475 initialize_lra_reg_info_element (i);
1476 copy_pool
1477 = create_alloc_pool ("lra copies", sizeof (struct lra_copy), 100);
1478 copy_vec.create (100);
1482 /* Finish common reg info and copies. */
1483 static void
1484 finish_reg_info (void)
1486 int i;
1488 for (i = 0; i < reg_info_size; i++)
1489 bitmap_clear (&lra_reg_info[i].insn_bitmap);
1490 free (lra_reg_info);
1491 reg_info_size = 0;
1492 free_alloc_pool (copy_pool);
1493 copy_vec.release ();
1496 /* Expand common reg info if it is necessary. */
1497 static void
1498 expand_reg_info (void)
1500 int i, old = reg_info_size;
1502 if (reg_info_size > max_reg_num ())
1503 return;
1504 reg_info_size = max_reg_num () * 3 / 2 + 1;
1505 lra_reg_info = XRESIZEVEC (struct lra_reg, lra_reg_info, reg_info_size);
1506 for (i = old; i < reg_info_size; i++)
1507 initialize_lra_reg_info_element (i);
1510 /* Free all copies. */
1511 void
1512 lra_free_copies (void)
1514 lra_copy_t cp;
1516 while (copy_vec.length () != 0)
1518 cp = copy_vec.pop ();
1519 lra_reg_info[cp->regno1].copies = lra_reg_info[cp->regno2].copies = NULL;
1520 pool_free (copy_pool, cp);
1524 /* Create copy of two pseudos REGNO1 and REGNO2. The copy execution
1525 frequency is FREQ. */
1526 void
1527 lra_create_copy (int regno1, int regno2, int freq)
1529 bool regno1_dest_p;
1530 lra_copy_t cp;
1532 lra_assert (regno1 != regno2);
1533 regno1_dest_p = true;
1534 if (regno1 > regno2)
1536 int temp = regno2;
1538 regno1_dest_p = false;
1539 regno2 = regno1;
1540 regno1 = temp;
1542 cp = (lra_copy_t) pool_alloc (copy_pool);
1543 copy_vec.safe_push (cp);
1544 cp->regno1_dest_p = regno1_dest_p;
1545 cp->freq = freq;
1546 cp->regno1 = regno1;
1547 cp->regno2 = regno2;
1548 cp->regno1_next = lra_reg_info[regno1].copies;
1549 lra_reg_info[regno1].copies = cp;
1550 cp->regno2_next = lra_reg_info[regno2].copies;
1551 lra_reg_info[regno2].copies = cp;
1552 if (lra_dump_file != NULL)
1553 fprintf (lra_dump_file, " Creating copy r%d%sr%d@%d\n",
1554 regno1, regno1_dest_p ? "<-" : "->", regno2, freq);
1557 /* Return N-th (0, 1, ...) copy. If there is no copy, return
1558 NULL. */
1559 lra_copy_t
1560 lra_get_copy (int n)
1562 if (n >= (int) copy_vec.length ())
1563 return NULL;
1564 return copy_vec[n];
1569 /* This page contains code dealing with info about registers in
1570 insns. */
1572 /* Process X of insn UID recursively and add info (operand type is
1573 given by TYPE, flag of that it is early clobber is EARLY_CLOBBER)
1574 about registers in X to the insn DATA. */
1575 static void
1576 add_regs_to_insn_regno_info (lra_insn_recog_data_t data, rtx x, int uid,
1577 enum op_type type, bool early_clobber)
1579 int i, j, regno;
1580 bool subreg_p;
1581 enum machine_mode mode;
1582 const char *fmt;
1583 enum rtx_code code;
1584 struct lra_insn_reg *curr;
1586 code = GET_CODE (x);
1587 mode = GET_MODE (x);
1588 subreg_p = false;
1589 if (GET_CODE (x) == SUBREG)
1591 x = SUBREG_REG (x);
1592 code = GET_CODE (x);
1593 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (x)))
1595 mode = GET_MODE (x);
1596 if (GET_MODE_SIZE (mode) > REGMODE_NATURAL_SIZE (mode))
1597 subreg_p = true;
1600 if (REG_P (x))
1602 regno = REGNO (x);
1603 if (regno < FIRST_PSEUDO_REGISTER
1604 && TEST_HARD_REG_BIT (lra_no_alloc_regs, regno)
1605 && ! TEST_HARD_REG_BIT (eliminable_regset, regno))
1606 return;
1607 expand_reg_info ();
1608 if (bitmap_set_bit (&lra_reg_info[regno].insn_bitmap, uid))
1610 data->regs = new_insn_reg (data->insn, regno, type, mode, subreg_p,
1611 early_clobber, data->regs);
1612 return;
1614 else
1616 for (curr = data->regs; curr != NULL; curr = curr->next)
1617 if (curr->regno == regno)
1619 if (curr->subreg_p != subreg_p || curr->biggest_mode != mode)
1620 /* The info can not be integrated into the found
1621 structure. */
1622 data->regs = new_insn_reg (data->insn, regno, type, mode,
1623 subreg_p, early_clobber,
1624 data->regs);
1625 else
1627 if (curr->type != type)
1628 curr->type = OP_INOUT;
1629 if (curr->early_clobber != early_clobber)
1630 curr->early_clobber = true;
1632 return;
1634 gcc_unreachable ();
1638 switch (code)
1640 case SET:
1641 add_regs_to_insn_regno_info (data, SET_DEST (x), uid, OP_OUT, false);
1642 add_regs_to_insn_regno_info (data, SET_SRC (x), uid, OP_IN, false);
1643 break;
1644 case CLOBBER:
1645 /* We treat clobber of non-operand hard registers as early
1646 clobber (the behavior is expected from asm). */
1647 add_regs_to_insn_regno_info (data, XEXP (x, 0), uid, OP_OUT, true);
1648 break;
1649 case PRE_INC: case PRE_DEC: case POST_INC: case POST_DEC:
1650 add_regs_to_insn_regno_info (data, XEXP (x, 0), uid, OP_INOUT, false);
1651 break;
1652 case PRE_MODIFY: case POST_MODIFY:
1653 add_regs_to_insn_regno_info (data, XEXP (x, 0), uid, OP_INOUT, false);
1654 add_regs_to_insn_regno_info (data, XEXP (x, 1), uid, OP_IN, false);
1655 break;
1656 default:
1657 if ((code != PARALLEL && code != EXPR_LIST) || type != OP_OUT)
1658 /* Some targets place small structures in registers for return
1659 values of functions, and those registers are wrapped in
1660 PARALLEL that we may see as the destination of a SET. Here
1661 is an example:
1663 (call_insn 13 12 14 2 (set (parallel:BLK [
1664 (expr_list:REG_DEP_TRUE (reg:DI 0 ax)
1665 (const_int 0 [0]))
1666 (expr_list:REG_DEP_TRUE (reg:DI 1 dx)
1667 (const_int 8 [0x8]))
1669 (call (mem:QI (symbol_ref:DI (... */
1670 type = OP_IN;
1671 fmt = GET_RTX_FORMAT (code);
1672 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1674 if (fmt[i] == 'e')
1675 add_regs_to_insn_regno_info (data, XEXP (x, i), uid, type, false);
1676 else if (fmt[i] == 'E')
1678 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1679 add_regs_to_insn_regno_info (data, XVECEXP (x, i, j), uid,
1680 type, false);
1686 /* Return execution frequency of INSN. */
1687 static int
1688 get_insn_freq (rtx insn)
1690 basic_block bb = BLOCK_FOR_INSN (insn);
1692 gcc_checking_assert (bb != NULL);
1693 return REG_FREQ_FROM_BB (bb);
1696 /* Invalidate all reg info of INSN with DATA and execution frequency
1697 FREQ. Update common info about the invalidated registers. */
1698 static void
1699 invalidate_insn_data_regno_info (lra_insn_recog_data_t data, rtx insn,
1700 int freq)
1702 int uid;
1703 bool debug_p;
1704 unsigned int i;
1705 struct lra_insn_reg *ir, *next_ir;
1707 uid = INSN_UID (insn);
1708 debug_p = DEBUG_INSN_P (insn);
1709 for (ir = data->regs; ir != NULL; ir = next_ir)
1711 i = ir->regno;
1712 next_ir = ir->next;
1713 free_insn_reg (ir);
1714 bitmap_clear_bit (&lra_reg_info[i].insn_bitmap, uid);
1715 if (i >= FIRST_PSEUDO_REGISTER && ! debug_p)
1717 lra_reg_info[i].nrefs--;
1718 lra_reg_info[i].freq -= freq;
1719 lra_assert (lra_reg_info[i].nrefs >= 0 && lra_reg_info[i].freq >= 0);
1722 data->regs = NULL;
1725 /* Invalidate all reg info of INSN. Update common info about the
1726 invalidated registers. */
1727 void
1728 lra_invalidate_insn_regno_info (rtx insn)
1730 invalidate_insn_data_regno_info (lra_get_insn_recog_data (insn), insn,
1731 get_insn_freq (insn));
1734 /* Update common reg info from reg info of insn given by its DATA and
1735 execution frequency FREQ. */
1736 static void
1737 setup_insn_reg_info (lra_insn_recog_data_t data, int freq)
1739 unsigned int i;
1740 struct lra_insn_reg *ir;
1742 for (ir = data->regs; ir != NULL; ir = ir->next)
1743 if ((i = ir->regno) >= FIRST_PSEUDO_REGISTER)
1745 lra_reg_info[i].nrefs++;
1746 lra_reg_info[i].freq += freq;
1750 /* Set up insn reg info of INSN. Update common reg info from reg info
1751 of INSN. */
1752 void
1753 lra_update_insn_regno_info (rtx insn)
1755 int i, uid, freq;
1756 lra_insn_recog_data_t data;
1757 struct lra_static_insn_data *static_data;
1758 enum rtx_code code;
1760 if (! INSN_P (insn))
1761 return;
1762 data = lra_get_insn_recog_data (insn);
1763 static_data = data->insn_static_data;
1764 freq = get_insn_freq (insn);
1765 invalidate_insn_data_regno_info (data, insn, freq);
1766 uid = INSN_UID (insn);
1767 for (i = static_data->n_operands - 1; i >= 0; i--)
1768 add_regs_to_insn_regno_info (data, *data->operand_loc[i], uid,
1769 static_data->operand[i].type,
1770 static_data->operand[i].early_clobber);
1771 if ((code = GET_CODE (PATTERN (insn))) == CLOBBER || code == USE)
1772 add_regs_to_insn_regno_info (data, XEXP (PATTERN (insn), 0), uid,
1773 code == USE ? OP_IN : OP_OUT, false);
1774 if (NONDEBUG_INSN_P (insn))
1775 setup_insn_reg_info (data, freq);
1778 /* Return reg info of insn given by it UID. */
1779 struct lra_insn_reg *
1780 lra_get_insn_regs (int uid)
1782 lra_insn_recog_data_t data;
1784 data = get_insn_recog_data_by_uid (uid);
1785 return data->regs;
1790 /* This page contains code dealing with stack of the insns which
1791 should be processed by the next constraint pass. */
1793 /* Bitmap used to put an insn on the stack only in one exemplar. */
1794 static sbitmap lra_constraint_insn_stack_bitmap;
1796 /* The stack itself. */
1797 vec<rtx> lra_constraint_insn_stack;
1799 /* Put INSN on the stack. If ALWAYS_UPDATE is true, always update the reg
1800 info for INSN, otherwise only update it if INSN is not already on the
1801 stack. */
1802 static inline void
1803 lra_push_insn_1 (rtx insn, bool always_update)
1805 unsigned int uid = INSN_UID (insn);
1806 if (always_update)
1807 lra_update_insn_regno_info (insn);
1808 if (uid >= SBITMAP_SIZE (lra_constraint_insn_stack_bitmap))
1809 lra_constraint_insn_stack_bitmap =
1810 sbitmap_resize (lra_constraint_insn_stack_bitmap, 3 * uid / 2, 0);
1811 if (bitmap_bit_p (lra_constraint_insn_stack_bitmap, uid))
1812 return;
1813 bitmap_set_bit (lra_constraint_insn_stack_bitmap, uid);
1814 if (! always_update)
1815 lra_update_insn_regno_info (insn);
1816 lra_constraint_insn_stack.safe_push (insn);
1819 /* Put INSN on the stack. */
1820 void
1821 lra_push_insn (rtx insn)
1823 lra_push_insn_1 (insn, false);
1826 /* Put INSN on the stack and update its reg info. */
1827 void
1828 lra_push_insn_and_update_insn_regno_info (rtx insn)
1830 lra_push_insn_1 (insn, true);
1833 /* Put insn with UID on the stack. */
1834 void
1835 lra_push_insn_by_uid (unsigned int uid)
1837 lra_push_insn (lra_insn_recog_data[uid]->insn);
1840 /* Take the last-inserted insns off the stack and return it. */
1842 lra_pop_insn (void)
1844 rtx insn = lra_constraint_insn_stack.pop ();
1845 bitmap_clear_bit (lra_constraint_insn_stack_bitmap, INSN_UID (insn));
1846 return insn;
1849 /* Return the current size of the insn stack. */
1850 unsigned int
1851 lra_insn_stack_length (void)
1853 return lra_constraint_insn_stack.length ();
1856 /* Push insns FROM to TO (excluding it) going in reverse order. */
1857 static void
1858 push_insns (rtx from, rtx to)
1860 rtx insn;
1862 if (from == NULL_RTX)
1863 return;
1864 for (insn = from; insn != to; insn = PREV_INSN (insn))
1865 if (INSN_P (insn))
1866 lra_push_insn (insn);
1869 /* Set up sp offset for insn in range [FROM, LAST]. The offset is
1870 taken from the next BB insn after LAST or zero if there in such
1871 insn. */
1872 static void
1873 setup_sp_offset (rtx from, rtx last)
1875 rtx before = next_nonnote_insn_bb (last);
1876 HOST_WIDE_INT offset = (before == NULL_RTX || ! INSN_P (before)
1877 ? 0 : lra_get_insn_recog_data (before)->sp_offset);
1879 for (rtx insn = from; insn != NEXT_INSN (last); insn = NEXT_INSN (insn))
1880 lra_get_insn_recog_data (insn)->sp_offset = offset;
1883 /* Emit insns BEFORE before INSN and insns AFTER after INSN. Put the
1884 insns onto the stack. Print about emitting the insns with
1885 TITLE. */
1886 void
1887 lra_process_new_insns (rtx insn, rtx before, rtx after, const char *title)
1889 rtx last;
1891 if (before == NULL_RTX && after == NULL_RTX)
1892 return;
1893 if (lra_dump_file != NULL)
1895 dump_insn_slim (lra_dump_file, insn);
1896 if (before != NULL_RTX)
1898 fprintf (lra_dump_file," %s before:\n", title);
1899 dump_rtl_slim (lra_dump_file, before, NULL_RTX, -1, 0);
1901 if (after != NULL_RTX)
1903 fprintf (lra_dump_file, " %s after:\n", title);
1904 dump_rtl_slim (lra_dump_file, after, NULL_RTX, -1, 0);
1906 fprintf (lra_dump_file, "\n");
1908 if (before != NULL_RTX)
1910 emit_insn_before (before, insn);
1911 push_insns (PREV_INSN (insn), PREV_INSN (before));
1912 setup_sp_offset (before, PREV_INSN (insn));
1914 if (after != NULL_RTX)
1916 for (last = after; NEXT_INSN (last) != NULL_RTX; last = NEXT_INSN (last))
1918 emit_insn_after (after, insn);
1919 push_insns (last, insn);
1920 setup_sp_offset (after, last);
1926 /* This page contains code dealing with scratches (changing them onto
1927 pseudos and restoring them from the pseudos).
1929 We change scratches into pseudos at the beginning of LRA to
1930 simplify dealing with them (conflicts, hard register assignments).
1932 If the pseudo denoting scratch was spilled it means that we do need
1933 a hard register for it. Such pseudos are transformed back to
1934 scratches at the end of LRA. */
1936 /* Description of location of a former scratch operand. */
1937 struct sloc
1939 rtx insn; /* Insn where the scratch was. */
1940 int nop; /* Number of the operand which was a scratch. */
1943 typedef struct sloc *sloc_t;
1945 /* Locations of the former scratches. */
1946 static vec<sloc_t> scratches;
1948 /* Bitmap of scratch regnos. */
1949 static bitmap_head scratch_bitmap;
1951 /* Bitmap of scratch operands. */
1952 static bitmap_head scratch_operand_bitmap;
1954 /* Return true if pseudo REGNO is made of SCRATCH. */
1955 bool
1956 lra_former_scratch_p (int regno)
1958 return bitmap_bit_p (&scratch_bitmap, regno);
1961 /* Return true if the operand NOP of INSN is a former scratch. */
1962 bool
1963 lra_former_scratch_operand_p (rtx insn, int nop)
1965 return bitmap_bit_p (&scratch_operand_bitmap,
1966 INSN_UID (insn) * MAX_RECOG_OPERANDS + nop) != 0;
1969 /* Change scratches onto pseudos and save their location. */
1970 static void
1971 remove_scratches (void)
1973 int i;
1974 bool insn_changed_p;
1975 basic_block bb;
1976 rtx insn, reg;
1977 sloc_t loc;
1978 lra_insn_recog_data_t id;
1979 struct lra_static_insn_data *static_id;
1981 scratches.create (get_max_uid ());
1982 bitmap_initialize (&scratch_bitmap, &reg_obstack);
1983 bitmap_initialize (&scratch_operand_bitmap, &reg_obstack);
1984 FOR_EACH_BB_FN (bb, cfun)
1985 FOR_BB_INSNS (bb, insn)
1986 if (INSN_P (insn))
1988 id = lra_get_insn_recog_data (insn);
1989 static_id = id->insn_static_data;
1990 insn_changed_p = false;
1991 for (i = 0; i < static_id->n_operands; i++)
1992 if (GET_CODE (*id->operand_loc[i]) == SCRATCH
1993 && GET_MODE (*id->operand_loc[i]) != VOIDmode)
1995 insn_changed_p = true;
1996 *id->operand_loc[i] = reg
1997 = lra_create_new_reg (static_id->operand[i].mode,
1998 *id->operand_loc[i], ALL_REGS, NULL);
1999 add_reg_note (insn, REG_UNUSED, reg);
2000 lra_update_dup (id, i);
2001 loc = XNEW (struct sloc);
2002 loc->insn = insn;
2003 loc->nop = i;
2004 scratches.safe_push (loc);
2005 bitmap_set_bit (&scratch_bitmap, REGNO (*id->operand_loc[i]));
2006 bitmap_set_bit (&scratch_operand_bitmap,
2007 INSN_UID (insn) * MAX_RECOG_OPERANDS + i);
2008 if (lra_dump_file != NULL)
2009 fprintf (lra_dump_file,
2010 "Removing SCRATCH in insn #%u (nop %d)\n",
2011 INSN_UID (insn), i);
2013 if (insn_changed_p)
2014 /* Because we might use DF right after caller-saves sub-pass
2015 we need to keep DF info up to date. */
2016 df_insn_rescan (insn);
2020 /* Changes pseudos created by function remove_scratches onto scratches. */
2021 static void
2022 restore_scratches (void)
2024 int regno;
2025 unsigned i;
2026 sloc_t loc;
2027 rtx last = NULL_RTX;
2028 lra_insn_recog_data_t id = NULL;
2030 for (i = 0; scratches.iterate (i, &loc); i++)
2032 if (last != loc->insn)
2034 last = loc->insn;
2035 id = lra_get_insn_recog_data (last);
2037 if (REG_P (*id->operand_loc[loc->nop])
2038 && ((regno = REGNO (*id->operand_loc[loc->nop]))
2039 >= FIRST_PSEUDO_REGISTER)
2040 && lra_get_regno_hard_regno (regno) < 0)
2042 /* It should be only case when scratch register with chosen
2043 constraint 'X' did not get memory or hard register. */
2044 lra_assert (lra_former_scratch_p (regno));
2045 *id->operand_loc[loc->nop]
2046 = gen_rtx_SCRATCH (GET_MODE (*id->operand_loc[loc->nop]));
2047 lra_update_dup (id, loc->nop);
2048 if (lra_dump_file != NULL)
2049 fprintf (lra_dump_file, "Restoring SCRATCH in insn #%u(nop %d)\n",
2050 INSN_UID (loc->insn), loc->nop);
2053 for (i = 0; scratches.iterate (i, &loc); i++)
2054 free (loc);
2055 scratches.release ();
2056 bitmap_clear (&scratch_bitmap);
2057 bitmap_clear (&scratch_operand_bitmap);
2062 #ifdef ENABLE_CHECKING
2064 /* Function checks RTL for correctness. If FINAL_P is true, it is
2065 done at the end of LRA and the check is more rigorous. */
2066 static void
2067 check_rtl (bool final_p)
2069 basic_block bb;
2070 rtx insn;
2072 lra_assert (! final_p || reload_completed);
2073 FOR_EACH_BB_FN (bb, cfun)
2074 FOR_BB_INSNS (bb, insn)
2075 if (NONDEBUG_INSN_P (insn)
2076 && GET_CODE (PATTERN (insn)) != USE
2077 && GET_CODE (PATTERN (insn)) != CLOBBER
2078 && GET_CODE (PATTERN (insn)) != ASM_INPUT)
2080 if (final_p)
2082 extract_insn (insn);
2083 lra_assert (constrain_operands (1));
2084 continue;
2086 /* LRA code is based on assumption that all addresses can be
2087 correctly decomposed. LRA can generate reloads for
2088 decomposable addresses. The decomposition code checks the
2089 correctness of the addresses. So we don't need to check
2090 the addresses here. Don't call insn_invalid_p here, it can
2091 change the code at this stage. */
2092 if (recog_memoized (insn) < 0 && asm_noperands (PATTERN (insn)) < 0)
2093 fatal_insn_not_found (insn);
2096 #endif /* #ifdef ENABLE_CHECKING */
2098 /* Determine if the current function has an exception receiver block
2099 that reaches the exit block via non-exceptional edges */
2100 static bool
2101 has_nonexceptional_receiver (void)
2103 edge e;
2104 edge_iterator ei;
2105 basic_block *tos, *worklist, bb;
2107 /* If we're not optimizing, then just err on the safe side. */
2108 if (!optimize)
2109 return true;
2111 /* First determine which blocks can reach exit via normal paths. */
2112 tos = worklist = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun) + 1);
2114 FOR_EACH_BB_FN (bb, cfun)
2115 bb->flags &= ~BB_REACHABLE;
2117 /* Place the exit block on our worklist. */
2118 EXIT_BLOCK_PTR_FOR_FN (cfun)->flags |= BB_REACHABLE;
2119 *tos++ = EXIT_BLOCK_PTR_FOR_FN (cfun);
2121 /* Iterate: find everything reachable from what we've already seen. */
2122 while (tos != worklist)
2124 bb = *--tos;
2126 FOR_EACH_EDGE (e, ei, bb->preds)
2127 if (e->flags & EDGE_ABNORMAL)
2129 free (worklist);
2130 return true;
2132 else
2134 basic_block src = e->src;
2136 if (!(src->flags & BB_REACHABLE))
2138 src->flags |= BB_REACHABLE;
2139 *tos++ = src;
2143 free (worklist);
2144 /* No exceptional block reached exit unexceptionally. */
2145 return false;
2148 #ifdef AUTO_INC_DEC
2150 /* Process recursively X of INSN and add REG_INC notes if necessary. */
2151 static void
2152 add_auto_inc_notes (rtx insn, rtx x)
2154 enum rtx_code code = GET_CODE (x);
2155 const char *fmt;
2156 int i, j;
2158 if (code == MEM && auto_inc_p (XEXP (x, 0)))
2160 add_reg_note (insn, REG_INC, XEXP (XEXP (x, 0), 0));
2161 return;
2164 /* Scan all X sub-expressions. */
2165 fmt = GET_RTX_FORMAT (code);
2166 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2168 if (fmt[i] == 'e')
2169 add_auto_inc_notes (insn, XEXP (x, i));
2170 else if (fmt[i] == 'E')
2171 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2172 add_auto_inc_notes (insn, XVECEXP (x, i, j));
2176 #endif
2178 /* Remove all REG_DEAD and REG_UNUSED notes and regenerate REG_INC.
2179 We change pseudos by hard registers without notification of DF and
2180 that can make the notes obsolete. DF-infrastructure does not deal
2181 with REG_INC notes -- so we should regenerate them here. */
2182 static void
2183 update_inc_notes (void)
2185 rtx *pnote;
2186 basic_block bb;
2187 rtx insn;
2189 FOR_EACH_BB_FN (bb, cfun)
2190 FOR_BB_INSNS (bb, insn)
2191 if (NONDEBUG_INSN_P (insn))
2193 pnote = &REG_NOTES (insn);
2194 while (*pnote != 0)
2196 if (REG_NOTE_KIND (*pnote) == REG_DEAD
2197 || REG_NOTE_KIND (*pnote) == REG_UNUSED
2198 || REG_NOTE_KIND (*pnote) == REG_INC)
2199 *pnote = XEXP (*pnote, 1);
2200 else
2201 pnote = &XEXP (*pnote, 1);
2203 #ifdef AUTO_INC_DEC
2204 add_auto_inc_notes (insn, PATTERN (insn));
2205 #endif
2209 /* Set to 1 while in lra. */
2210 int lra_in_progress;
2212 /* Start of pseudo regnos before the LRA. */
2213 int lra_new_regno_start;
2215 /* Start of reload pseudo regnos before the new spill pass. */
2216 int lra_constraint_new_regno_start;
2218 /* Inheritance pseudo regnos before the new spill pass. */
2219 bitmap_head lra_inheritance_pseudos;
2221 /* Split regnos before the new spill pass. */
2222 bitmap_head lra_split_regs;
2224 /* Reload pseudo regnos before the new assignmnet pass which still can
2225 be spilled after the assinment pass as memory is also accepted in
2226 insns for the reload pseudos. */
2227 bitmap_head lra_optional_reload_pseudos;
2229 /* Pseudo regnos used for subreg reloads before the new assignment
2230 pass. Such pseudos still can be spilled after the assinment
2231 pass. */
2232 bitmap_head lra_subreg_reload_pseudos;
2234 /* First UID of insns generated before a new spill pass. */
2235 int lra_constraint_new_insn_uid_start;
2237 /* File used for output of LRA debug information. */
2238 FILE *lra_dump_file;
2240 /* True if we should try spill into registers of different classes
2241 instead of memory. */
2242 bool lra_reg_spill_p;
2244 /* Set up value LRA_REG_SPILL_P. */
2245 static void
2246 setup_reg_spill_flag (void)
2248 int cl, mode;
2250 if (targetm.spill_class != NULL)
2251 for (cl = 0; cl < (int) LIM_REG_CLASSES; cl++)
2252 for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
2253 if (targetm.spill_class ((enum reg_class) cl,
2254 (enum machine_mode) mode) != NO_REGS)
2256 lra_reg_spill_p = true;
2257 return;
2259 lra_reg_spill_p = false;
2262 /* True if the current function is too big to use regular algorithms
2263 in LRA. In other words, we should use simpler and faster algorithms
2264 in LRA. It also means we should not worry about generation code
2265 for caller saves. The value is set up in IRA. */
2266 bool lra_simple_p;
2268 /* Major LRA entry function. F is a file should be used to dump LRA
2269 debug info. */
2270 void
2271 lra (FILE *f)
2273 int i;
2274 bool live_p, scratch_p, inserted_p;
2276 lra_dump_file = f;
2278 timevar_push (TV_LRA);
2280 /* Make sure that the last insn is a note. Some subsequent passes
2281 need it. */
2282 emit_note (NOTE_INSN_DELETED);
2284 COPY_HARD_REG_SET (lra_no_alloc_regs, ira_no_alloc_regs);
2286 init_reg_info ();
2287 expand_reg_info ();
2289 init_insn_recog_data ();
2291 #ifdef ENABLE_CHECKING
2292 /* Some quick check on RTL generated by previous passes. */
2293 check_rtl (false);
2294 #endif
2296 lra_in_progress = 1;
2298 lra_live_range_iter = lra_coalesce_iter = 0;
2299 lra_constraint_iter = lra_constraint_iter_after_spill = 0;
2300 lra_inheritance_iter = lra_undo_inheritance_iter = 0;
2302 setup_reg_spill_flag ();
2304 /* Function remove_scratches can creates new pseudos for clobbers --
2305 so set up lra_constraint_new_regno_start before its call to
2306 permit changing reg classes for pseudos created by this
2307 simplification. */
2308 lra_constraint_new_regno_start = lra_new_regno_start = max_reg_num ();
2309 remove_scratches ();
2310 scratch_p = lra_constraint_new_regno_start != max_reg_num ();
2312 /* A function that has a non-local label that can reach the exit
2313 block via non-exceptional paths must save all call-saved
2314 registers. */
2315 if (cfun->has_nonlocal_label && has_nonexceptional_receiver ())
2316 crtl->saves_all_registers = 1;
2318 if (crtl->saves_all_registers)
2319 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2320 if (! call_used_regs[i] && ! fixed_regs[i] && ! LOCAL_REGNO (i))
2321 df_set_regs_ever_live (i, true);
2323 /* We don't DF from now and avoid its using because it is to
2324 expensive when a lot of RTL changes are made. */
2325 df_set_flags (DF_NO_INSN_RESCAN);
2326 lra_constraint_insn_stack.create (get_max_uid ());
2327 lra_constraint_insn_stack_bitmap = sbitmap_alloc (get_max_uid ());
2328 bitmap_clear (lra_constraint_insn_stack_bitmap);
2329 lra_live_ranges_init ();
2330 lra_constraints_init ();
2331 lra_curr_reload_num = 0;
2332 push_insns (get_last_insn (), NULL_RTX);
2333 /* It is needed for the 1st coalescing. */
2334 lra_constraint_new_insn_uid_start = get_max_uid ();
2335 bitmap_initialize (&lra_inheritance_pseudos, &reg_obstack);
2336 bitmap_initialize (&lra_split_regs, &reg_obstack);
2337 bitmap_initialize (&lra_optional_reload_pseudos, &reg_obstack);
2338 bitmap_initialize (&lra_subreg_reload_pseudos, &reg_obstack);
2339 live_p = false;
2340 if (get_frame_size () != 0 && crtl->stack_alignment_needed)
2341 /* If we have a stack frame, we must align it now. The stack size
2342 may be a part of the offset computation for register
2343 elimination. */
2344 assign_stack_local (BLKmode, 0, crtl->stack_alignment_needed);
2345 lra_init_equiv ();
2346 for (;;)
2348 for (;;)
2350 /* We should try to assign hard registers to scratches even
2351 if there were no RTL transformations in
2352 lra_constraints. */
2353 if (! lra_constraints (lra_constraint_iter == 0)
2354 && (lra_constraint_iter > 1
2355 || (! scratch_p && ! caller_save_needed)))
2356 break;
2357 /* Constraint transformations may result in that eliminable
2358 hard regs become uneliminable and pseudos which use them
2359 should be spilled. It is better to do it before pseudo
2360 assignments.
2362 For example, rs6000 can make
2363 RS6000_PIC_OFFSET_TABLE_REGNUM uneliminable if we started
2364 to use a constant pool. */
2365 lra_eliminate (false, false);
2366 /* Do inheritance only for regular algorithms. */
2367 if (! lra_simple_p)
2368 lra_inheritance ();
2369 if (live_p)
2370 lra_clear_live_ranges ();
2371 /* We need live ranges for lra_assign -- so build them. */
2372 lra_create_live_ranges (true);
2373 live_p = true;
2374 /* If we don't spill non-reload and non-inheritance pseudos,
2375 there is no sense to run memory-memory move coalescing.
2376 If inheritance pseudos were spilled, the memory-memory
2377 moves involving them will be removed by pass undoing
2378 inheritance. */
2379 if (lra_simple_p)
2380 lra_assign ();
2381 else
2383 bool spill_p = !lra_assign ();
2385 if (lra_undo_inheritance ())
2386 live_p = false;
2387 if (spill_p)
2389 if (! live_p)
2391 lra_create_live_ranges (true);
2392 live_p = true;
2394 if (lra_coalesce ())
2395 live_p = false;
2397 if (! live_p)
2398 lra_clear_live_ranges ();
2401 /* Don't clear optional reloads bitmap until all constraints are
2402 satisfied as we need to differ them from regular reloads. */
2403 bitmap_clear (&lra_optional_reload_pseudos);
2404 bitmap_clear (&lra_subreg_reload_pseudos);
2405 bitmap_clear (&lra_inheritance_pseudos);
2406 bitmap_clear (&lra_split_regs);
2407 if (! lra_need_for_spills_p ())
2408 break;
2409 if (! live_p)
2411 /* We need full live info for spilling pseudos into
2412 registers instead of memory. */
2413 lra_create_live_ranges (lra_reg_spill_p);
2414 live_p = true;
2416 lra_spill ();
2417 /* Assignment of stack slots changes elimination offsets for
2418 some eliminations. So update the offsets here. */
2419 lra_eliminate (false, false);
2420 lra_constraint_new_regno_start = max_reg_num ();
2421 lra_constraint_new_insn_uid_start = get_max_uid ();
2422 lra_constraint_iter_after_spill = 0;
2424 restore_scratches ();
2425 lra_eliminate (true, false);
2426 lra_final_code_change ();
2427 lra_in_progress = 0;
2428 if (live_p)
2429 lra_clear_live_ranges ();
2430 lra_live_ranges_finish ();
2431 lra_constraints_finish ();
2432 finish_reg_info ();
2433 sbitmap_free (lra_constraint_insn_stack_bitmap);
2434 lra_constraint_insn_stack.release ();
2435 finish_insn_recog_data ();
2436 regstat_free_n_sets_and_refs ();
2437 regstat_free_ri ();
2438 reload_completed = 1;
2439 update_inc_notes ();
2441 inserted_p = fixup_abnormal_edges ();
2443 /* We've possibly turned single trapping insn into multiple ones. */
2444 if (cfun->can_throw_non_call_exceptions)
2446 sbitmap blocks;
2447 blocks = sbitmap_alloc (last_basic_block_for_fn (cfun));
2448 bitmap_ones (blocks);
2449 find_many_sub_basic_blocks (blocks);
2450 sbitmap_free (blocks);
2453 if (inserted_p)
2454 commit_edge_insertions ();
2456 /* Replacing pseudos with their memory equivalents might have
2457 created shared rtx. Subsequent passes would get confused
2458 by this, so unshare everything here. */
2459 unshare_all_rtl_again (get_insns ());
2461 #ifdef ENABLE_CHECKING
2462 check_rtl (true);
2463 #endif
2465 timevar_pop (TV_LRA);
2468 /* Called once per compiler to initialize LRA data once. */
2469 void
2470 lra_init_once (void)
2472 init_insn_code_data_once ();
2475 /* Initialize LRA whenever register-related information is changed. */
2476 void
2477 lra_init (void)
2479 init_op_alt_data ();
2482 /* Called once per compiler to finish LRA data which are initialize
2483 once. */
2484 void
2485 lra_finish_once (void)
2487 finish_insn_code_data_once ();