Fix ChangeLog entry for r240730
[official-gcc.git] / gcc / lra.c
blobcb2bd35cffd6b2c74784eeb4a07c0d8b881fe18d
1 /* LRA (local register allocator) driver and LRA utilities.
2 Copyright (C) 2010-2016 Free Software Foundation, Inc.
3 Contributed by Vladimir Makarov <vmakarov@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* The Local Register Allocator (LRA) is a replacement of former
23 reload pass. It is focused to simplify code solving the reload
24 pass tasks, to make the code maintenance easier, and to implement new
25 perspective optimizations.
27 The major LRA design solutions are:
28 o division small manageable, separated sub-tasks
29 o reflection of all transformations and decisions in RTL as more
30 as possible
31 o insn constraints as a primary source of the info (minimizing
32 number of target-depended macros/hooks)
34 In brief LRA works by iterative insn process with the final goal is
35 to satisfy all insn and address constraints:
36 o New reload insns (in brief reloads) and reload pseudos might be
37 generated;
38 o Some pseudos might be spilled to assign hard registers to
39 new reload pseudos;
40 o Recalculating spilled pseudo values (rematerialization);
41 o Changing spilled pseudos to stack memory or their equivalences;
42 o Allocation stack memory changes the address displacement and
43 new iteration is needed.
45 Here is block diagram of LRA passes:
47 ------------------------
48 --------------- | Undo inheritance for | ---------------
49 | Memory-memory | | spilled pseudos, | | New (and old) |
50 | move coalesce |<---| splits for pseudos got |<-- | pseudos |
51 --------------- | the same hard regs, | | assignment |
52 Start | | and optional reloads | ---------------
53 | | ------------------------ ^
54 V | ---------------- |
55 ----------- V | Update virtual | |
56 | Remove |----> ------------>| register | |
57 | scratches | ^ | displacements | |
58 ----------- | ---------------- |
59 | | |
60 | V New |
61 | ------------ pseudos -------------------
62 | |Constraints:| or insns | Inheritance/split |
63 | | RTL |--------->| transformations |
64 | | transfor- | | in EBB scope |
65 | substi- | mations | -------------------
66 | tutions ------------
67 | | No change
68 ---------------- V
69 | Spilled pseudo | -------------------
70 | to memory |<----| Rematerialization |
71 | substitution | -------------------
72 ----------------
73 | No susbtitions
75 -------------------------
76 | Hard regs substitution, |
77 | devirtalization, and |------> Finish
78 | restoring scratches got |
79 | memory |
80 -------------------------
82 To speed up the process:
83 o We process only insns affected by changes on previous
84 iterations;
85 o We don't use DFA-infrastructure because it results in much slower
86 compiler speed than a special IR described below does;
87 o We use a special insn representation for quick access to insn
88 info which is always *synchronized* with the current RTL;
89 o Insn IR is minimized by memory. It is divided on three parts:
90 o one specific for each insn in RTL (only operand locations);
91 o one common for all insns in RTL with the same insn code
92 (different operand attributes from machine descriptions);
93 o one oriented for maintenance of live info (list of pseudos).
94 o Pseudo data:
95 o all insns where the pseudo is referenced;
96 o live info (conflicting hard regs, live ranges, # of
97 references etc);
98 o data used for assigning (preferred hard regs, costs etc).
100 This file contains LRA driver, LRA utility functions and data, and
101 code for dealing with scratches. */
103 #include "config.h"
104 #include "system.h"
105 #include "coretypes.h"
106 #include "backend.h"
107 #include "target.h"
108 #include "rtl.h"
109 #include "tree.h"
110 #include "predict.h"
111 #include "df.h"
112 #include "tm_p.h"
113 #include "optabs.h"
114 #include "regs.h"
115 #include "ira.h"
116 #include "recog.h"
117 #include "expr.h"
118 #include "cfgrtl.h"
119 #include "cfgbuild.h"
120 #include "lra.h"
121 #include "lra-int.h"
122 #include "print-rtl.h"
124 /* Dump bitmap SET with TITLE and BB INDEX. */
125 void
126 lra_dump_bitmap_with_title (const char *title, bitmap set, int index)
128 unsigned int i;
129 int count;
130 bitmap_iterator bi;
131 static const int max_nums_on_line = 10;
133 if (bitmap_empty_p (set))
134 return;
135 fprintf (lra_dump_file, " %s %d:", title, index);
136 fprintf (lra_dump_file, "\n");
137 count = max_nums_on_line + 1;
138 EXECUTE_IF_SET_IN_BITMAP (set, 0, i, bi)
140 if (count > max_nums_on_line)
142 fprintf (lra_dump_file, "\n ");
143 count = 0;
145 fprintf (lra_dump_file, " %4u", i);
146 count++;
148 fprintf (lra_dump_file, "\n");
151 /* Hard registers currently not available for allocation. It can
152 changed after some hard registers become not eliminable. */
153 HARD_REG_SET lra_no_alloc_regs;
155 static int get_new_reg_value (void);
156 static void expand_reg_info (void);
157 static void invalidate_insn_recog_data (int);
158 static int get_insn_freq (rtx_insn *);
159 static void invalidate_insn_data_regno_info (lra_insn_recog_data_t,
160 rtx_insn *, int);
162 /* Expand all regno related info needed for LRA. */
163 static void
164 expand_reg_data (int old)
166 resize_reg_info ();
167 expand_reg_info ();
168 ira_expand_reg_equiv ();
169 for (int i = (int) max_reg_num () - 1; i >= old; i--)
170 lra_change_class (i, ALL_REGS, " Set", true);
173 /* Create and return a new reg of ORIGINAL mode. If ORIGINAL is NULL
174 or of VOIDmode, use MD_MODE for the new reg. Initialize its
175 register class to RCLASS. Print message about assigning class
176 RCLASS containing new register name TITLE unless it is NULL. Use
177 attributes of ORIGINAL if it is a register. The created register
178 will have unique held value. */
180 lra_create_new_reg_with_unique_value (machine_mode md_mode, rtx original,
181 enum reg_class rclass, const char *title)
183 machine_mode mode;
184 rtx new_reg;
186 if (original == NULL_RTX || (mode = GET_MODE (original)) == VOIDmode)
187 mode = md_mode;
188 lra_assert (mode != VOIDmode);
189 new_reg = gen_reg_rtx (mode);
190 if (original == NULL_RTX || ! REG_P (original))
192 if (lra_dump_file != NULL)
193 fprintf (lra_dump_file, " Creating newreg=%i", REGNO (new_reg));
195 else
197 if (ORIGINAL_REGNO (original) >= FIRST_PSEUDO_REGISTER)
198 ORIGINAL_REGNO (new_reg) = ORIGINAL_REGNO (original);
199 REG_USERVAR_P (new_reg) = REG_USERVAR_P (original);
200 REG_POINTER (new_reg) = REG_POINTER (original);
201 REG_ATTRS (new_reg) = REG_ATTRS (original);
202 if (lra_dump_file != NULL)
203 fprintf (lra_dump_file, " Creating newreg=%i from oldreg=%i",
204 REGNO (new_reg), REGNO (original));
206 if (lra_dump_file != NULL)
208 if (title != NULL)
209 fprintf (lra_dump_file, ", assigning class %s to%s%s r%d",
210 reg_class_names[rclass], *title == '\0' ? "" : " ",
211 title, REGNO (new_reg));
212 fprintf (lra_dump_file, "\n");
214 expand_reg_data (max_reg_num ());
215 setup_reg_classes (REGNO (new_reg), rclass, NO_REGS, rclass);
216 return new_reg;
219 /* Analogous to the previous function but also inherits value of
220 ORIGINAL. */
222 lra_create_new_reg (machine_mode md_mode, rtx original,
223 enum reg_class rclass, const char *title)
225 rtx new_reg;
227 new_reg
228 = lra_create_new_reg_with_unique_value (md_mode, original, rclass, title);
229 if (original != NULL_RTX && REG_P (original))
230 lra_assign_reg_val (REGNO (original), REGNO (new_reg));
231 return new_reg;
234 /* Set up for REGNO unique hold value. */
235 void
236 lra_set_regno_unique_value (int regno)
238 lra_reg_info[regno].val = get_new_reg_value ();
241 /* Invalidate INSN related info used by LRA. The info should never be
242 used after that. */
243 void
244 lra_invalidate_insn_data (rtx_insn *insn)
246 lra_invalidate_insn_regno_info (insn);
247 invalidate_insn_recog_data (INSN_UID (insn));
250 /* Mark INSN deleted and invalidate the insn related info used by
251 LRA. */
252 void
253 lra_set_insn_deleted (rtx_insn *insn)
255 lra_invalidate_insn_data (insn);
256 SET_INSN_DELETED (insn);
259 /* Delete an unneeded INSN and any previous insns who sole purpose is
260 loading data that is dead in INSN. */
261 void
262 lra_delete_dead_insn (rtx_insn *insn)
264 rtx_insn *prev = prev_real_insn (insn);
265 rtx prev_dest;
267 /* If the previous insn sets a register that dies in our insn,
268 delete it too. */
269 if (prev && GET_CODE (PATTERN (prev)) == SET
270 && (prev_dest = SET_DEST (PATTERN (prev)), REG_P (prev_dest))
271 && reg_mentioned_p (prev_dest, PATTERN (insn))
272 && find_regno_note (insn, REG_DEAD, REGNO (prev_dest))
273 && ! side_effects_p (SET_SRC (PATTERN (prev))))
274 lra_delete_dead_insn (prev);
276 lra_set_insn_deleted (insn);
279 /* Emit insn x = y + z. Return NULL if we failed to do it.
280 Otherwise, return the insn. We don't use gen_add3_insn as it might
281 clobber CC. */
282 static rtx_insn *
283 emit_add3_insn (rtx x, rtx y, rtx z)
285 rtx_insn *last;
287 last = get_last_insn ();
289 if (have_addptr3_insn (x, y, z))
291 rtx_insn *insn = gen_addptr3_insn (x, y, z);
293 /* If the target provides an "addptr" pattern it hopefully does
294 for a reason. So falling back to the normal add would be
295 a bug. */
296 lra_assert (insn != NULL_RTX);
297 emit_insn (insn);
298 return insn;
301 rtx_insn *insn = emit_insn (gen_rtx_SET (x, gen_rtx_PLUS (GET_MODE (y),
302 y, z)));
303 if (recog_memoized (insn) < 0)
305 delete_insns_since (last);
306 insn = NULL;
308 return insn;
311 /* Emit insn x = x + y. Return the insn. We use gen_add2_insn as the
312 last resort. */
313 static rtx_insn *
314 emit_add2_insn (rtx x, rtx y)
316 rtx_insn *insn = emit_add3_insn (x, x, y);
317 if (insn == NULL_RTX)
319 insn = gen_add2_insn (x, y);
320 if (insn != NULL_RTX)
321 emit_insn (insn);
323 return insn;
326 /* Target checks operands through operand predicates to recognize an
327 insn. We should have a special precaution to generate add insns
328 which are frequent results of elimination.
330 Emit insns for x = y + z. X can be used to store intermediate
331 values and should be not in Y and Z when we use X to store an
332 intermediate value. Y + Z should form [base] [+ index[ * scale]] [
333 + disp] where base and index are registers, disp and scale are
334 constants. Y should contain base if it is present, Z should
335 contain disp if any. index[*scale] can be part of Y or Z. */
336 void
337 lra_emit_add (rtx x, rtx y, rtx z)
339 int old;
340 rtx_insn *last;
341 rtx a1, a2, base, index, disp, scale, index_scale;
342 bool ok_p;
344 rtx_insn *add3_insn = emit_add3_insn (x, y, z);
345 old = max_reg_num ();
346 if (add3_insn != NULL)
348 else
350 disp = a2 = NULL_RTX;
351 if (GET_CODE (y) == PLUS)
353 a1 = XEXP (y, 0);
354 a2 = XEXP (y, 1);
355 disp = z;
357 else
359 a1 = y;
360 if (CONSTANT_P (z))
361 disp = z;
362 else
363 a2 = z;
365 index_scale = scale = NULL_RTX;
366 if (GET_CODE (a1) == MULT)
368 index_scale = a1;
369 index = XEXP (a1, 0);
370 scale = XEXP (a1, 1);
371 base = a2;
373 else if (a2 != NULL_RTX && GET_CODE (a2) == MULT)
375 index_scale = a2;
376 index = XEXP (a2, 0);
377 scale = XEXP (a2, 1);
378 base = a1;
380 else
382 base = a1;
383 index = a2;
385 if ((base != NULL_RTX && ! (REG_P (base) || GET_CODE (base) == SUBREG))
386 || (index != NULL_RTX
387 && ! (REG_P (index) || GET_CODE (index) == SUBREG))
388 || (disp != NULL_RTX && ! CONSTANT_P (disp))
389 || (scale != NULL_RTX && ! CONSTANT_P (scale)))
391 /* Probably we have no 3 op add. Last chance is to use 2-op
392 add insn. To succeed, don't move Z to X as an address
393 segment always comes in Y. Otherwise, we might fail when
394 adding the address segment to register. */
395 lra_assert (x != y && x != z);
396 emit_move_insn (x, y);
397 rtx_insn *insn = emit_add2_insn (x, z);
398 lra_assert (insn != NULL_RTX);
400 else
402 if (index_scale == NULL_RTX)
403 index_scale = index;
404 if (disp == NULL_RTX)
406 /* Generate x = index_scale; x = x + base. */
407 lra_assert (index_scale != NULL_RTX && base != NULL_RTX);
408 emit_move_insn (x, index_scale);
409 rtx_insn *insn = emit_add2_insn (x, base);
410 lra_assert (insn != NULL_RTX);
412 else if (scale == NULL_RTX)
414 /* Try x = base + disp. */
415 lra_assert (base != NULL_RTX);
416 last = get_last_insn ();
417 rtx_insn *move_insn =
418 emit_move_insn (x, gen_rtx_PLUS (GET_MODE (base), base, disp));
419 if (recog_memoized (move_insn) < 0)
421 delete_insns_since (last);
422 /* Generate x = disp; x = x + base. */
423 emit_move_insn (x, disp);
424 rtx_insn *add2_insn = emit_add2_insn (x, base);
425 lra_assert (add2_insn != NULL_RTX);
427 /* Generate x = x + index. */
428 if (index != NULL_RTX)
430 rtx_insn *insn = emit_add2_insn (x, index);
431 lra_assert (insn != NULL_RTX);
434 else
436 /* Try x = index_scale; x = x + disp; x = x + base. */
437 last = get_last_insn ();
438 rtx_insn *move_insn = emit_move_insn (x, index_scale);
439 ok_p = false;
440 if (recog_memoized (move_insn) >= 0)
442 rtx_insn *insn = emit_add2_insn (x, disp);
443 if (insn != NULL_RTX)
445 if (base == NULL_RTX)
446 ok_p = true;
447 else
449 insn = emit_add2_insn (x, base);
450 if (insn != NULL_RTX)
451 ok_p = true;
455 if (! ok_p)
457 rtx_insn *insn;
459 delete_insns_since (last);
460 /* Generate x = disp; x = x + base; x = x + index_scale. */
461 emit_move_insn (x, disp);
462 if (base != NULL_RTX)
464 insn = emit_add2_insn (x, base);
465 lra_assert (insn != NULL_RTX);
467 insn = emit_add2_insn (x, index_scale);
468 lra_assert (insn != NULL_RTX);
473 /* Functions emit_... can create pseudos -- so expand the pseudo
474 data. */
475 if (old != max_reg_num ())
476 expand_reg_data (old);
479 /* The number of emitted reload insns so far. */
480 int lra_curr_reload_num;
482 /* Emit x := y, processing special case when y = u + v or y = u + v *
483 scale + w through emit_add (Y can be an address which is base +
484 index reg * scale + displacement in general case). X may be used
485 as intermediate result therefore it should be not in Y. */
486 void
487 lra_emit_move (rtx x, rtx y)
489 int old;
491 if (GET_CODE (y) != PLUS)
493 if (rtx_equal_p (x, y))
494 return;
495 old = max_reg_num ();
496 emit_move_insn (x, y);
497 if (REG_P (x))
498 lra_reg_info[ORIGINAL_REGNO (x)].last_reload = ++lra_curr_reload_num;
499 /* Function emit_move can create pseudos -- so expand the pseudo
500 data. */
501 if (old != max_reg_num ())
502 expand_reg_data (old);
503 return;
505 lra_emit_add (x, XEXP (y, 0), XEXP (y, 1));
508 /* Update insn operands which are duplication of operands whose
509 numbers are in array of NOPS (with end marker -1). The insn is
510 represented by its LRA internal representation ID. */
511 void
512 lra_update_dups (lra_insn_recog_data_t id, signed char *nops)
514 int i, j, nop;
515 struct lra_static_insn_data *static_id = id->insn_static_data;
517 for (i = 0; i < static_id->n_dups; i++)
518 for (j = 0; (nop = nops[j]) >= 0; j++)
519 if (static_id->dup_num[i] == nop)
520 *id->dup_loc[i] = *id->operand_loc[nop];
525 /* This page contains code dealing with info about registers in the
526 insns. */
528 /* Pools for insn reg info. */
529 object_allocator<lra_insn_reg> lra_insn_reg_pool ("insn regs");
531 /* Create LRA insn related info about a reference to REGNO in INSN with
532 TYPE (in/out/inout), biggest reference mode MODE, flag that it is
533 reference through subreg (SUBREG_P), flag that is early clobbered
534 in the insn (EARLY_CLOBBER), and reference to the next insn reg
535 info (NEXT). */
536 static struct lra_insn_reg *
537 new_insn_reg (rtx_insn *insn, int regno, enum op_type type,
538 machine_mode mode,
539 bool subreg_p, bool early_clobber, struct lra_insn_reg *next)
541 lra_insn_reg *ir = lra_insn_reg_pool.allocate ();
542 ir->type = type;
543 ir->biggest_mode = mode;
544 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (lra_reg_info[regno].biggest_mode)
545 && NONDEBUG_INSN_P (insn))
546 lra_reg_info[regno].biggest_mode = mode;
547 ir->subreg_p = subreg_p;
548 ir->early_clobber = early_clobber;
549 ir->regno = regno;
550 ir->next = next;
551 return ir;
554 /* Free insn reg info list IR. */
555 static void
556 free_insn_regs (struct lra_insn_reg *ir)
558 struct lra_insn_reg *next_ir;
560 for (; ir != NULL; ir = next_ir)
562 next_ir = ir->next;
563 lra_insn_reg_pool.remove (ir);
567 /* Finish pool for insn reg info. */
568 static void
569 finish_insn_regs (void)
571 lra_insn_reg_pool.release ();
576 /* This page contains code dealing LRA insn info (or in other words
577 LRA internal insn representation). */
579 /* Map INSN_CODE -> the static insn data. This info is valid during
580 all translation unit. */
581 struct lra_static_insn_data *insn_code_data[NUM_INSN_CODES];
583 /* Debug insns are represented as a special insn with one input
584 operand which is RTL expression in var_location. */
586 /* The following data are used as static insn operand data for all
587 debug insns. If structure lra_operand_data is changed, the
588 initializer should be changed too. */
589 static struct lra_operand_data debug_operand_data =
591 NULL, /* alternative */
592 VOIDmode, /* We are not interesting in the operand mode. */
593 OP_IN,
594 0, 0, 0, 0
597 /* The following data are used as static insn data for all debug
598 insns. If structure lra_static_insn_data is changed, the
599 initializer should be changed too. */
600 static struct lra_static_insn_data debug_insn_static_data =
602 &debug_operand_data,
603 0, /* Duplication operands #. */
604 -1, /* Commutative operand #. */
605 1, /* Operands #. There is only one operand which is debug RTL
606 expression. */
607 0, /* Duplications #. */
608 0, /* Alternatives #. We are not interesting in alternatives
609 because we does not proceed debug_insns for reloads. */
610 NULL, /* Hard registers referenced in machine description. */
611 NULL /* Descriptions of operands in alternatives. */
614 /* Called once per compiler work to initialize some LRA data related
615 to insns. */
616 static void
617 init_insn_code_data_once (void)
619 memset (insn_code_data, 0, sizeof (insn_code_data));
622 /* Called once per compiler work to finalize some LRA data related to
623 insns. */
624 static void
625 finish_insn_code_data_once (void)
627 for (unsigned int i = 0; i < NUM_INSN_CODES; i++)
629 if (insn_code_data[i] != NULL)
630 free (insn_code_data[i]);
634 /* Return static insn data, allocate and setup if necessary. Although
635 dup_num is static data (it depends only on icode), to set it up we
636 need to extract insn first. So recog_data should be valid for
637 normal insn (ICODE >= 0) before the call. */
638 static struct lra_static_insn_data *
639 get_static_insn_data (int icode, int nop, int ndup, int nalt)
641 struct lra_static_insn_data *data;
642 size_t n_bytes;
644 lra_assert (icode < (int) NUM_INSN_CODES);
645 if (icode >= 0 && (data = insn_code_data[icode]) != NULL)
646 return data;
647 lra_assert (nop >= 0 && ndup >= 0 && nalt >= 0);
648 n_bytes = sizeof (struct lra_static_insn_data)
649 + sizeof (struct lra_operand_data) * nop
650 + sizeof (int) * ndup;
651 data = XNEWVAR (struct lra_static_insn_data, n_bytes);
652 data->operand_alternative = NULL;
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;
699 /* Expand, if necessary, LRA data about insns. */
700 static void
701 check_and_expand_insn_recog_data (int index)
703 int i, old;
705 if (lra_insn_recog_data_len > index)
706 return;
707 old = lra_insn_recog_data_len;
708 lra_insn_recog_data_len = index * 3 / 2 + 1;
709 lra_insn_recog_data = XRESIZEVEC (lra_insn_recog_data_t,
710 lra_insn_recog_data,
711 lra_insn_recog_data_len);
712 for (i = old; i < lra_insn_recog_data_len; i++)
713 lra_insn_recog_data[i] = NULL;
716 /* Finish LRA DATA about insn. */
717 static void
718 free_insn_recog_data (lra_insn_recog_data_t data)
720 if (data->operand_loc != NULL)
721 free (data->operand_loc);
722 if (data->dup_loc != NULL)
723 free (data->dup_loc);
724 if (data->arg_hard_regs != NULL)
725 free (data->arg_hard_regs);
726 if (data->icode < 0 && NONDEBUG_INSN_P (data->insn))
728 if (data->insn_static_data->operand_alternative != NULL)
729 free (const_cast <operand_alternative *>
730 (data->insn_static_data->operand_alternative));
731 free_insn_regs (data->insn_static_data->hard_regs);
732 free (data->insn_static_data);
734 free_insn_regs (data->regs);
735 data->regs = NULL;
736 free (data);
739 /* Pools for copies. */
740 static object_allocator<lra_copy> lra_copy_pool ("lra copies");
742 /* Finish LRA data about all insns. */
743 static void
744 finish_insn_recog_data (void)
746 int i;
747 lra_insn_recog_data_t data;
749 for (i = 0; i < lra_insn_recog_data_len; i++)
750 if ((data = lra_insn_recog_data[i]) != NULL)
751 free_insn_recog_data (data);
752 finish_insn_regs ();
753 lra_copy_pool.release ();
754 lra_insn_reg_pool.release ();
755 free (lra_insn_recog_data);
758 /* Setup info about operands in alternatives of LRA DATA of insn. */
759 static void
760 setup_operand_alternative (lra_insn_recog_data_t data,
761 const operand_alternative *op_alt)
763 int i, j, nop, nalt;
764 int icode = data->icode;
765 struct lra_static_insn_data *static_data = data->insn_static_data;
767 static_data->commutative = -1;
768 nop = static_data->n_operands;
769 nalt = static_data->n_alternatives;
770 static_data->operand_alternative = op_alt;
771 for (i = 0; i < nop; i++)
773 static_data->operand[i].early_clobber = false;
774 static_data->operand[i].is_address = false;
775 if (static_data->operand[i].constraint[0] == '%')
777 /* We currently only support one commutative pair of operands. */
778 if (static_data->commutative < 0)
779 static_data->commutative = i;
780 else
781 lra_assert (icode < 0); /* Asm */
782 /* The last operand should not be marked commutative. */
783 lra_assert (i != nop - 1);
786 for (j = 0; j < nalt; j++)
787 for (i = 0; i < nop; i++, op_alt++)
789 static_data->operand[i].early_clobber |= op_alt->earlyclobber;
790 static_data->operand[i].is_address |= op_alt->is_address;
794 /* Recursively process X and collect info about registers, which are
795 not the insn operands, in X with TYPE (in/out/inout) and flag that
796 it is early clobbered in the insn (EARLY_CLOBBER) and add the info
797 to LIST. X is a part of insn given by DATA. Return the result
798 list. */
799 static struct lra_insn_reg *
800 collect_non_operand_hard_regs (rtx *x, lra_insn_recog_data_t data,
801 struct lra_insn_reg *list,
802 enum op_type type, bool early_clobber)
804 int i, j, regno, last;
805 bool subreg_p;
806 machine_mode mode;
807 struct lra_insn_reg *curr;
808 rtx op = *x;
809 enum rtx_code code = GET_CODE (op);
810 const char *fmt = GET_RTX_FORMAT (code);
812 for (i = 0; i < data->insn_static_data->n_operands; i++)
813 if (x == data->operand_loc[i])
814 /* It is an operand loc. Stop here. */
815 return list;
816 for (i = 0; i < data->insn_static_data->n_dups; i++)
817 if (x == data->dup_loc[i])
818 /* It is a dup loc. Stop here. */
819 return list;
820 mode = GET_MODE (op);
821 subreg_p = false;
822 if (code == SUBREG)
824 op = SUBREG_REG (op);
825 code = GET_CODE (op);
826 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (op)))
828 mode = GET_MODE (op);
829 if (GET_MODE_SIZE (mode) > REGMODE_NATURAL_SIZE (mode))
830 subreg_p = true;
833 if (REG_P (op))
835 if ((regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER)
836 return list;
837 /* Process all regs even unallocatable ones as we need info
838 about all regs for rematerialization pass. */
839 for (last = regno + hard_regno_nregs[regno][mode];
840 regno < last;
841 regno++)
843 for (curr = list; curr != NULL; curr = curr->next)
844 if (curr->regno == regno && curr->subreg_p == subreg_p
845 && curr->biggest_mode == mode)
847 if (curr->type != type)
848 curr->type = OP_INOUT;
849 if (curr->early_clobber != early_clobber)
850 curr->early_clobber = true;
851 break;
853 if (curr == NULL)
855 /* This is a new hard regno or the info can not be
856 integrated into the found structure. */
857 #ifdef STACK_REGS
858 early_clobber
859 = (early_clobber
860 /* This clobber is to inform popping floating
861 point stack only. */
862 && ! (FIRST_STACK_REG <= regno
863 && regno <= LAST_STACK_REG));
864 #endif
865 list = new_insn_reg (data->insn, regno, type, mode, subreg_p,
866 early_clobber, list);
869 return list;
871 switch (code)
873 case SET:
874 list = collect_non_operand_hard_regs (&SET_DEST (op), data,
875 list, OP_OUT, false);
876 list = collect_non_operand_hard_regs (&SET_SRC (op), data,
877 list, OP_IN, false);
878 break;
879 case CLOBBER:
880 /* We treat clobber of non-operand hard registers as early
881 clobber (the behavior is expected from asm). */
882 list = collect_non_operand_hard_regs (&XEXP (op, 0), data,
883 list, OP_OUT, true);
884 break;
885 case PRE_INC: case PRE_DEC: case POST_INC: case POST_DEC:
886 list = collect_non_operand_hard_regs (&XEXP (op, 0), data,
887 list, OP_INOUT, false);
888 break;
889 case PRE_MODIFY: case POST_MODIFY:
890 list = collect_non_operand_hard_regs (&XEXP (op, 0), data,
891 list, OP_INOUT, false);
892 list = collect_non_operand_hard_regs (&XEXP (op, 1), data,
893 list, OP_IN, false);
894 break;
895 default:
896 fmt = GET_RTX_FORMAT (code);
897 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
899 if (fmt[i] == 'e')
900 list = collect_non_operand_hard_regs (&XEXP (op, i), data,
901 list, OP_IN, false);
902 else if (fmt[i] == 'E')
903 for (j = XVECLEN (op, i) - 1; j >= 0; j--)
904 list = collect_non_operand_hard_regs (&XVECEXP (op, i, j), data,
905 list, OP_IN, false);
908 return list;
911 /* Set up and return info about INSN. Set up the info if it is not set up
912 yet. */
913 lra_insn_recog_data_t
914 lra_set_insn_recog_data (rtx_insn *insn)
916 lra_insn_recog_data_t data;
917 int i, n, icode;
918 rtx **locs;
919 unsigned int uid = INSN_UID (insn);
920 struct lra_static_insn_data *insn_static_data;
922 check_and_expand_insn_recog_data (uid);
923 if (DEBUG_INSN_P (insn))
924 icode = -1;
925 else
927 icode = INSN_CODE (insn);
928 if (icode < 0)
929 /* It might be a new simple insn which is not recognized yet. */
930 INSN_CODE (insn) = icode = recog_memoized (insn);
932 data = XNEW (struct lra_insn_recog_data);
933 lra_insn_recog_data[uid] = data;
934 data->insn = insn;
935 data->used_insn_alternative = -1;
936 data->icode = icode;
937 data->regs = NULL;
938 if (DEBUG_INSN_P (insn))
940 data->insn_static_data = &debug_insn_static_data;
941 data->dup_loc = NULL;
942 data->arg_hard_regs = NULL;
943 data->preferred_alternatives = ALL_ALTERNATIVES;
944 data->operand_loc = XNEWVEC (rtx *, 1);
945 data->operand_loc[0] = &INSN_VAR_LOCATION_LOC (insn);
946 return data;
948 if (icode < 0)
950 int nop, nalt;
951 machine_mode operand_mode[MAX_RECOG_OPERANDS];
952 const char *constraints[MAX_RECOG_OPERANDS];
954 nop = asm_noperands (PATTERN (insn));
955 data->operand_loc = data->dup_loc = NULL;
956 nalt = 1;
957 if (nop < 0)
959 /* It is a special insn like USE or CLOBBER. We should
960 recognize any regular insn otherwise LRA can do nothing
961 with this insn. */
962 gcc_assert (GET_CODE (PATTERN (insn)) == USE
963 || GET_CODE (PATTERN (insn)) == CLOBBER
964 || GET_CODE (PATTERN (insn)) == ASM_INPUT);
965 data->insn_static_data = insn_static_data
966 = get_static_insn_data (-1, 0, 0, nalt);
968 else
970 /* expand_asm_operands makes sure there aren't too many
971 operands. */
972 lra_assert (nop <= MAX_RECOG_OPERANDS);
973 if (nop != 0)
974 data->operand_loc = XNEWVEC (rtx *, nop);
975 /* Now get the operand values and constraints out of the
976 insn. */
977 decode_asm_operands (PATTERN (insn), NULL,
978 data->operand_loc,
979 constraints, operand_mode, NULL);
980 if (nop > 0)
982 const char *p = recog_data.constraints[0];
984 for (p = constraints[0]; *p; p++)
985 nalt += *p == ',';
987 data->insn_static_data = insn_static_data
988 = get_static_insn_data (-1, nop, 0, nalt);
989 for (i = 0; i < nop; i++)
991 insn_static_data->operand[i].mode = operand_mode[i];
992 insn_static_data->operand[i].constraint = constraints[i];
993 insn_static_data->operand[i].strict_low = false;
994 insn_static_data->operand[i].is_operator = false;
995 insn_static_data->operand[i].is_address = false;
998 for (i = 0; i < insn_static_data->n_operands; i++)
999 insn_static_data->operand[i].type
1000 = (insn_static_data->operand[i].constraint[0] == '=' ? OP_OUT
1001 : insn_static_data->operand[i].constraint[0] == '+' ? OP_INOUT
1002 : OP_IN);
1003 data->preferred_alternatives = ALL_ALTERNATIVES;
1004 if (nop > 0)
1006 operand_alternative *op_alt = XCNEWVEC (operand_alternative,
1007 nalt * nop);
1008 preprocess_constraints (nop, nalt, constraints, op_alt);
1009 setup_operand_alternative (data, op_alt);
1012 else
1014 insn_extract (insn);
1015 data->insn_static_data = insn_static_data
1016 = get_static_insn_data (icode, insn_data[icode].n_operands,
1017 insn_data[icode].n_dups,
1018 insn_data[icode].n_alternatives);
1019 n = insn_static_data->n_operands;
1020 if (n == 0)
1021 locs = NULL;
1022 else
1024 locs = XNEWVEC (rtx *, n);
1025 memcpy (locs, recog_data.operand_loc, n * sizeof (rtx *));
1027 data->operand_loc = locs;
1028 n = insn_static_data->n_dups;
1029 if (n == 0)
1030 locs = NULL;
1031 else
1033 locs = XNEWVEC (rtx *, n);
1034 memcpy (locs, recog_data.dup_loc, n * sizeof (rtx *));
1036 data->dup_loc = locs;
1037 data->preferred_alternatives = get_preferred_alternatives (insn);
1038 const operand_alternative *op_alt = preprocess_insn_constraints (icode);
1039 if (!insn_static_data->operand_alternative)
1040 setup_operand_alternative (data, op_alt);
1041 else if (op_alt != insn_static_data->operand_alternative)
1042 insn_static_data->operand_alternative = op_alt;
1044 if (GET_CODE (PATTERN (insn)) == CLOBBER || GET_CODE (PATTERN (insn)) == USE)
1045 insn_static_data->hard_regs = NULL;
1046 else
1047 insn_static_data->hard_regs
1048 = collect_non_operand_hard_regs (&PATTERN (insn), data,
1049 NULL, OP_IN, false);
1050 data->arg_hard_regs = NULL;
1051 if (CALL_P (insn))
1053 bool use_p;
1054 rtx link;
1055 int n_hard_regs, regno, arg_hard_regs[FIRST_PSEUDO_REGISTER];
1057 n_hard_regs = 0;
1058 /* Finding implicit hard register usage. We believe it will be
1059 not changed whatever transformations are used. Call insns
1060 are such example. */
1061 for (link = CALL_INSN_FUNCTION_USAGE (insn);
1062 link != NULL_RTX;
1063 link = XEXP (link, 1))
1064 if (((use_p = GET_CODE (XEXP (link, 0)) == USE)
1065 || GET_CODE (XEXP (link, 0)) == CLOBBER)
1066 && REG_P (XEXP (XEXP (link, 0), 0)))
1068 regno = REGNO (XEXP (XEXP (link, 0), 0));
1069 lra_assert (regno < FIRST_PSEUDO_REGISTER);
1070 /* It is an argument register. */
1071 for (i = REG_NREGS (XEXP (XEXP (link, 0), 0)) - 1; i >= 0; i--)
1072 arg_hard_regs[n_hard_regs++]
1073 = regno + i + (use_p ? 0 : FIRST_PSEUDO_REGISTER);
1075 if (n_hard_regs != 0)
1077 arg_hard_regs[n_hard_regs++] = -1;
1078 data->arg_hard_regs = XNEWVEC (int, n_hard_regs);
1079 memcpy (data->arg_hard_regs, arg_hard_regs,
1080 sizeof (int) * n_hard_regs);
1083 /* Some output operand can be recognized only from the context not
1084 from the constraints which are empty in this case. Call insn may
1085 contain a hard register in set destination with empty constraint
1086 and extract_insn treats them as an input. */
1087 for (i = 0; i < insn_static_data->n_operands; i++)
1089 int j;
1090 rtx pat, set;
1091 struct lra_operand_data *operand = &insn_static_data->operand[i];
1093 /* ??? Should we treat 'X' the same way. It looks to me that
1094 'X' means anything and empty constraint means we do not
1095 care. */
1096 if (operand->type != OP_IN || *operand->constraint != '\0'
1097 || operand->is_operator)
1098 continue;
1099 pat = PATTERN (insn);
1100 if (GET_CODE (pat) == SET)
1102 if (data->operand_loc[i] != &SET_DEST (pat))
1103 continue;
1105 else if (GET_CODE (pat) == PARALLEL)
1107 for (j = XVECLEN (pat, 0) - 1; j >= 0; j--)
1109 set = XVECEXP (PATTERN (insn), 0, j);
1110 if (GET_CODE (set) == SET
1111 && &SET_DEST (set) == data->operand_loc[i])
1112 break;
1114 if (j < 0)
1115 continue;
1117 else
1118 continue;
1119 operand->type = OP_OUT;
1121 return data;
1124 /* Return info about insn give by UID. The info should be already set
1125 up. */
1126 static lra_insn_recog_data_t
1127 get_insn_recog_data_by_uid (int uid)
1129 lra_insn_recog_data_t data;
1131 data = lra_insn_recog_data[uid];
1132 lra_assert (data != NULL);
1133 return data;
1136 /* Invalidate all info about insn given by its UID. */
1137 static void
1138 invalidate_insn_recog_data (int uid)
1140 lra_insn_recog_data_t data;
1142 data = lra_insn_recog_data[uid];
1143 lra_assert (data != NULL);
1144 free_insn_recog_data (data);
1145 lra_insn_recog_data[uid] = NULL;
1148 /* Update all the insn info about INSN. It is usually called when
1149 something in the insn was changed. Return the updated info. */
1150 lra_insn_recog_data_t
1151 lra_update_insn_recog_data (rtx_insn *insn)
1153 lra_insn_recog_data_t data;
1154 int n;
1155 unsigned int uid = INSN_UID (insn);
1156 struct lra_static_insn_data *insn_static_data;
1157 HOST_WIDE_INT sp_offset = 0;
1159 check_and_expand_insn_recog_data (uid);
1160 if ((data = lra_insn_recog_data[uid]) != NULL
1161 && data->icode != INSN_CODE (insn))
1163 sp_offset = data->sp_offset;
1164 invalidate_insn_data_regno_info (data, insn, get_insn_freq (insn));
1165 invalidate_insn_recog_data (uid);
1166 data = NULL;
1168 if (data == NULL)
1170 data = lra_get_insn_recog_data (insn);
1171 /* Initiate or restore SP offset. */
1172 data->sp_offset = sp_offset;
1173 return data;
1175 insn_static_data = data->insn_static_data;
1176 data->used_insn_alternative = -1;
1177 if (DEBUG_INSN_P (insn))
1178 return data;
1179 if (data->icode < 0)
1181 int nop;
1182 machine_mode operand_mode[MAX_RECOG_OPERANDS];
1183 const char *constraints[MAX_RECOG_OPERANDS];
1185 nop = asm_noperands (PATTERN (insn));
1186 if (nop >= 0)
1188 lra_assert (nop == data->insn_static_data->n_operands);
1189 /* Now get the operand values and constraints out of the
1190 insn. */
1191 decode_asm_operands (PATTERN (insn), NULL,
1192 data->operand_loc,
1193 constraints, operand_mode, NULL);
1195 if (flag_checking)
1196 for (int i = 0; i < nop; i++)
1197 lra_assert
1198 (insn_static_data->operand[i].mode == operand_mode[i]
1199 && insn_static_data->operand[i].constraint == constraints[i]
1200 && ! insn_static_data->operand[i].is_operator);
1203 if (flag_checking)
1204 for (int i = 0; i < insn_static_data->n_operands; i++)
1205 lra_assert
1206 (insn_static_data->operand[i].type
1207 == (insn_static_data->operand[i].constraint[0] == '=' ? OP_OUT
1208 : insn_static_data->operand[i].constraint[0] == '+' ? OP_INOUT
1209 : OP_IN));
1211 else
1213 insn_extract (insn);
1214 n = insn_static_data->n_operands;
1215 if (n != 0)
1216 memcpy (data->operand_loc, recog_data.operand_loc, n * sizeof (rtx *));
1217 n = insn_static_data->n_dups;
1218 if (n != 0)
1219 memcpy (data->dup_loc, recog_data.dup_loc, n * sizeof (rtx *));
1220 lra_assert (check_bool_attrs (insn));
1222 return data;
1225 /* Set up that INSN is using alternative ALT now. */
1226 void
1227 lra_set_used_insn_alternative (rtx_insn *insn, int alt)
1229 lra_insn_recog_data_t data;
1231 data = lra_get_insn_recog_data (insn);
1232 data->used_insn_alternative = alt;
1235 /* Set up that insn with UID is using alternative ALT now. The insn
1236 info should be already set up. */
1237 void
1238 lra_set_used_insn_alternative_by_uid (int uid, int alt)
1240 lra_insn_recog_data_t data;
1242 check_and_expand_insn_recog_data (uid);
1243 data = lra_insn_recog_data[uid];
1244 lra_assert (data != NULL);
1245 data->used_insn_alternative = alt;
1250 /* This page contains code dealing with common register info and
1251 pseudo copies. */
1253 /* The size of the following array. */
1254 static int reg_info_size;
1255 /* Common info about each register. */
1256 struct lra_reg *lra_reg_info;
1258 /* Last register value. */
1259 static int last_reg_value;
1261 /* Return new register value. */
1262 static int
1263 get_new_reg_value (void)
1265 return ++last_reg_value;
1268 /* Vec referring to pseudo copies. */
1269 static vec<lra_copy_t> copy_vec;
1271 /* Initialize I-th element of lra_reg_info. */
1272 static inline void
1273 initialize_lra_reg_info_element (int i)
1275 bitmap_initialize (&lra_reg_info[i].insn_bitmap, &reg_obstack);
1276 #ifdef STACK_REGS
1277 lra_reg_info[i].no_stack_p = false;
1278 #endif
1279 CLEAR_HARD_REG_SET (lra_reg_info[i].conflict_hard_regs);
1280 CLEAR_HARD_REG_SET (lra_reg_info[i].actual_call_used_reg_set);
1281 lra_reg_info[i].preferred_hard_regno1 = -1;
1282 lra_reg_info[i].preferred_hard_regno2 = -1;
1283 lra_reg_info[i].preferred_hard_regno_profit1 = 0;
1284 lra_reg_info[i].preferred_hard_regno_profit2 = 0;
1285 lra_reg_info[i].biggest_mode = VOIDmode;
1286 lra_reg_info[i].live_ranges = NULL;
1287 lra_reg_info[i].nrefs = lra_reg_info[i].freq = 0;
1288 lra_reg_info[i].last_reload = 0;
1289 lra_reg_info[i].restore_rtx = NULL_RTX;
1290 lra_reg_info[i].val = get_new_reg_value ();
1291 lra_reg_info[i].offset = 0;
1292 lra_reg_info[i].copies = NULL;
1295 /* Initialize common reg info and copies. */
1296 static void
1297 init_reg_info (void)
1299 int i;
1301 last_reg_value = 0;
1302 reg_info_size = max_reg_num () * 3 / 2 + 1;
1303 lra_reg_info = XNEWVEC (struct lra_reg, reg_info_size);
1304 for (i = 0; i < reg_info_size; i++)
1305 initialize_lra_reg_info_element (i);
1306 copy_vec.truncate (0);
1310 /* Finish common reg info and copies. */
1311 static void
1312 finish_reg_info (void)
1314 int i;
1316 for (i = 0; i < reg_info_size; i++)
1317 bitmap_clear (&lra_reg_info[i].insn_bitmap);
1318 free (lra_reg_info);
1319 reg_info_size = 0;
1322 /* Expand common reg info if it is necessary. */
1323 static void
1324 expand_reg_info (void)
1326 int i, old = reg_info_size;
1328 if (reg_info_size > max_reg_num ())
1329 return;
1330 reg_info_size = max_reg_num () * 3 / 2 + 1;
1331 lra_reg_info = XRESIZEVEC (struct lra_reg, lra_reg_info, reg_info_size);
1332 for (i = old; i < reg_info_size; i++)
1333 initialize_lra_reg_info_element (i);
1336 /* Free all copies. */
1337 void
1338 lra_free_copies (void)
1340 lra_copy_t cp;
1342 while (copy_vec.length () != 0)
1344 cp = copy_vec.pop ();
1345 lra_reg_info[cp->regno1].copies = lra_reg_info[cp->regno2].copies = NULL;
1346 lra_copy_pool.remove (cp);
1350 /* Create copy of two pseudos REGNO1 and REGNO2. The copy execution
1351 frequency is FREQ. */
1352 void
1353 lra_create_copy (int regno1, int regno2, int freq)
1355 bool regno1_dest_p;
1356 lra_copy_t cp;
1358 lra_assert (regno1 != regno2);
1359 regno1_dest_p = true;
1360 if (regno1 > regno2)
1362 std::swap (regno1, regno2);
1363 regno1_dest_p = false;
1365 cp = lra_copy_pool.allocate ();
1366 copy_vec.safe_push (cp);
1367 cp->regno1_dest_p = regno1_dest_p;
1368 cp->freq = freq;
1369 cp->regno1 = regno1;
1370 cp->regno2 = regno2;
1371 cp->regno1_next = lra_reg_info[regno1].copies;
1372 lra_reg_info[regno1].copies = cp;
1373 cp->regno2_next = lra_reg_info[regno2].copies;
1374 lra_reg_info[regno2].copies = cp;
1375 if (lra_dump_file != NULL)
1376 fprintf (lra_dump_file, " Creating copy r%d%sr%d@%d\n",
1377 regno1, regno1_dest_p ? "<-" : "->", regno2, freq);
1380 /* Return N-th (0, 1, ...) copy. If there is no copy, return
1381 NULL. */
1382 lra_copy_t
1383 lra_get_copy (int n)
1385 if (n >= (int) copy_vec.length ())
1386 return NULL;
1387 return copy_vec[n];
1392 /* This page contains code dealing with info about registers in
1393 insns. */
1395 /* Process X of insn UID recursively and add info (operand type is
1396 given by TYPE, flag of that it is early clobber is EARLY_CLOBBER)
1397 about registers in X to the insn DATA. */
1398 static void
1399 add_regs_to_insn_regno_info (lra_insn_recog_data_t data, rtx x, int uid,
1400 enum op_type type, bool early_clobber)
1402 int i, j, regno;
1403 bool subreg_p;
1404 machine_mode mode;
1405 const char *fmt;
1406 enum rtx_code code;
1407 struct lra_insn_reg *curr;
1409 code = GET_CODE (x);
1410 mode = GET_MODE (x);
1411 subreg_p = false;
1412 if (GET_CODE (x) == SUBREG)
1414 x = SUBREG_REG (x);
1415 code = GET_CODE (x);
1416 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (x)))
1418 mode = GET_MODE (x);
1419 if (GET_MODE_SIZE (mode) > REGMODE_NATURAL_SIZE (mode))
1420 subreg_p = true;
1423 if (REG_P (x))
1425 regno = REGNO (x);
1426 /* Process all regs even unallocatable ones as we need info about
1427 all regs for rematerialization pass. */
1428 expand_reg_info ();
1429 if (bitmap_set_bit (&lra_reg_info[regno].insn_bitmap, uid))
1431 data->regs = new_insn_reg (data->insn, regno, type, mode, subreg_p,
1432 early_clobber, data->regs);
1433 return;
1435 else
1437 for (curr = data->regs; curr != NULL; curr = curr->next)
1438 if (curr->regno == regno)
1440 if (curr->subreg_p != subreg_p || curr->biggest_mode != mode)
1441 /* The info can not be integrated into the found
1442 structure. */
1443 data->regs = new_insn_reg (data->insn, regno, type, mode,
1444 subreg_p, early_clobber,
1445 data->regs);
1446 else
1448 if (curr->type != type)
1449 curr->type = OP_INOUT;
1450 if (curr->early_clobber != early_clobber)
1451 curr->early_clobber = true;
1453 return;
1455 gcc_unreachable ();
1459 switch (code)
1461 case SET:
1462 add_regs_to_insn_regno_info (data, SET_DEST (x), uid, OP_OUT, false);
1463 add_regs_to_insn_regno_info (data, SET_SRC (x), uid, OP_IN, false);
1464 break;
1465 case CLOBBER:
1466 /* We treat clobber of non-operand hard registers as early
1467 clobber (the behavior is expected from asm). */
1468 add_regs_to_insn_regno_info (data, XEXP (x, 0), uid, OP_OUT, true);
1469 break;
1470 case PRE_INC: case PRE_DEC: case POST_INC: case POST_DEC:
1471 add_regs_to_insn_regno_info (data, XEXP (x, 0), uid, OP_INOUT, false);
1472 break;
1473 case PRE_MODIFY: case POST_MODIFY:
1474 add_regs_to_insn_regno_info (data, XEXP (x, 0), uid, OP_INOUT, false);
1475 add_regs_to_insn_regno_info (data, XEXP (x, 1), uid, OP_IN, false);
1476 break;
1477 default:
1478 if ((code != PARALLEL && code != EXPR_LIST) || type != OP_OUT)
1479 /* Some targets place small structures in registers for return
1480 values of functions, and those registers are wrapped in
1481 PARALLEL that we may see as the destination of a SET. Here
1482 is an example:
1484 (call_insn 13 12 14 2 (set (parallel:BLK [
1485 (expr_list:REG_DEP_TRUE (reg:DI 0 ax)
1486 (const_int 0 [0]))
1487 (expr_list:REG_DEP_TRUE (reg:DI 1 dx)
1488 (const_int 8 [0x8]))
1490 (call (mem:QI (symbol_ref:DI (... */
1491 type = OP_IN;
1492 fmt = GET_RTX_FORMAT (code);
1493 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1495 if (fmt[i] == 'e')
1496 add_regs_to_insn_regno_info (data, XEXP (x, i), uid, type, false);
1497 else if (fmt[i] == 'E')
1499 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1500 add_regs_to_insn_regno_info (data, XVECEXP (x, i, j), uid,
1501 type, false);
1507 /* Return execution frequency of INSN. */
1508 static int
1509 get_insn_freq (rtx_insn *insn)
1511 basic_block bb = BLOCK_FOR_INSN (insn);
1513 gcc_checking_assert (bb != NULL);
1514 return REG_FREQ_FROM_BB (bb);
1517 /* Invalidate all reg info of INSN with DATA and execution frequency
1518 FREQ. Update common info about the invalidated registers. */
1519 static void
1520 invalidate_insn_data_regno_info (lra_insn_recog_data_t data, rtx_insn *insn,
1521 int freq)
1523 int uid;
1524 bool debug_p;
1525 unsigned int i;
1526 struct lra_insn_reg *ir, *next_ir;
1528 uid = INSN_UID (insn);
1529 debug_p = DEBUG_INSN_P (insn);
1530 for (ir = data->regs; ir != NULL; ir = next_ir)
1532 i = ir->regno;
1533 next_ir = ir->next;
1534 lra_insn_reg_pool.remove (ir);
1535 bitmap_clear_bit (&lra_reg_info[i].insn_bitmap, uid);
1536 if (i >= FIRST_PSEUDO_REGISTER && ! debug_p)
1538 lra_reg_info[i].nrefs--;
1539 lra_reg_info[i].freq -= freq;
1540 lra_assert (lra_reg_info[i].nrefs >= 0 && lra_reg_info[i].freq >= 0);
1543 data->regs = NULL;
1546 /* Invalidate all reg info of INSN. Update common info about the
1547 invalidated registers. */
1548 void
1549 lra_invalidate_insn_regno_info (rtx_insn *insn)
1551 invalidate_insn_data_regno_info (lra_get_insn_recog_data (insn), insn,
1552 get_insn_freq (insn));
1555 /* Update common reg info from reg info of insn given by its DATA and
1556 execution frequency FREQ. */
1557 static void
1558 setup_insn_reg_info (lra_insn_recog_data_t data, int freq)
1560 unsigned int i;
1561 struct lra_insn_reg *ir;
1563 for (ir = data->regs; ir != NULL; ir = ir->next)
1564 if ((i = ir->regno) >= FIRST_PSEUDO_REGISTER)
1566 lra_reg_info[i].nrefs++;
1567 lra_reg_info[i].freq += freq;
1571 /* Set up insn reg info of INSN. Update common reg info from reg info
1572 of INSN. */
1573 void
1574 lra_update_insn_regno_info (rtx_insn *insn)
1576 int i, uid, freq;
1577 lra_insn_recog_data_t data;
1578 struct lra_static_insn_data *static_data;
1579 enum rtx_code code;
1580 rtx link;
1582 if (! INSN_P (insn))
1583 return;
1584 data = lra_get_insn_recog_data (insn);
1585 static_data = data->insn_static_data;
1586 freq = get_insn_freq (insn);
1587 invalidate_insn_data_regno_info (data, insn, freq);
1588 uid = INSN_UID (insn);
1589 for (i = static_data->n_operands - 1; i >= 0; i--)
1590 add_regs_to_insn_regno_info (data, *data->operand_loc[i], uid,
1591 static_data->operand[i].type,
1592 static_data->operand[i].early_clobber);
1593 if ((code = GET_CODE (PATTERN (insn))) == CLOBBER || code == USE)
1594 add_regs_to_insn_regno_info (data, XEXP (PATTERN (insn), 0), uid,
1595 code == USE ? OP_IN : OP_OUT, false);
1596 if (CALL_P (insn))
1597 /* On some targets call insns can refer to pseudos in memory in
1598 CALL_INSN_FUNCTION_USAGE list. Process them in order to
1599 consider their occurrences in calls for different
1600 transformations (e.g. inheritance) with given pseudos. */
1601 for (link = CALL_INSN_FUNCTION_USAGE (insn);
1602 link != NULL_RTX;
1603 link = XEXP (link, 1))
1604 if (((code = GET_CODE (XEXP (link, 0))) == USE || code == CLOBBER)
1605 && MEM_P (XEXP (XEXP (link, 0), 0)))
1606 add_regs_to_insn_regno_info (data, XEXP (XEXP (link, 0), 0), uid,
1607 code == USE ? OP_IN : OP_OUT, false);
1608 if (NONDEBUG_INSN_P (insn))
1609 setup_insn_reg_info (data, freq);
1612 /* Return reg info of insn given by it UID. */
1613 struct lra_insn_reg *
1614 lra_get_insn_regs (int uid)
1616 lra_insn_recog_data_t data;
1618 data = get_insn_recog_data_by_uid (uid);
1619 return data->regs;
1624 /* Recursive hash function for RTL X. */
1625 hashval_t
1626 lra_rtx_hash (rtx x)
1628 int i, j;
1629 enum rtx_code code;
1630 const char *fmt;
1631 hashval_t val = 0;
1633 if (x == 0)
1634 return val;
1636 code = GET_CODE (x);
1637 val += (int) code + 4095;
1639 /* Some RTL can be compared nonrecursively. */
1640 switch (code)
1642 case REG:
1643 return val + REGNO (x);
1645 case LABEL_REF:
1646 return iterative_hash_object (XEXP (x, 0), val);
1648 case SYMBOL_REF:
1649 return iterative_hash_object (XSTR (x, 0), val);
1651 case SCRATCH:
1652 case CONST_DOUBLE:
1653 case CONST_INT:
1654 case CONST_VECTOR:
1655 return val;
1657 default:
1658 break;
1661 /* Hash the elements. */
1662 fmt = GET_RTX_FORMAT (code);
1663 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1665 switch (fmt[i])
1667 case 'w':
1668 val += XWINT (x, i);
1669 break;
1671 case 'n':
1672 case 'i':
1673 val += XINT (x, i);
1674 break;
1676 case 'V':
1677 case 'E':
1678 val += XVECLEN (x, i);
1680 for (j = 0; j < XVECLEN (x, i); j++)
1681 val += lra_rtx_hash (XVECEXP (x, i, j));
1682 break;
1684 case 'e':
1685 val += lra_rtx_hash (XEXP (x, i));
1686 break;
1688 case 'S':
1689 case 's':
1690 val += htab_hash_string (XSTR (x, i));
1691 break;
1693 case 'u':
1694 case '0':
1695 case 't':
1696 break;
1698 /* It is believed that rtx's at this level will never
1699 contain anything but integers and other rtx's, except for
1700 within LABEL_REFs and SYMBOL_REFs. */
1701 default:
1702 abort ();
1705 return val;
1710 /* This page contains code dealing with stack of the insns which
1711 should be processed by the next constraint pass. */
1713 /* Bitmap used to put an insn on the stack only in one exemplar. */
1714 static sbitmap lra_constraint_insn_stack_bitmap;
1716 /* The stack itself. */
1717 vec<rtx_insn *> lra_constraint_insn_stack;
1719 /* Put INSN on the stack. If ALWAYS_UPDATE is true, always update the reg
1720 info for INSN, otherwise only update it if INSN is not already on the
1721 stack. */
1722 static inline void
1723 lra_push_insn_1 (rtx_insn *insn, bool always_update)
1725 unsigned int uid = INSN_UID (insn);
1726 if (always_update)
1727 lra_update_insn_regno_info (insn);
1728 if (uid >= SBITMAP_SIZE (lra_constraint_insn_stack_bitmap))
1729 lra_constraint_insn_stack_bitmap =
1730 sbitmap_resize (lra_constraint_insn_stack_bitmap, 3 * uid / 2, 0);
1731 if (bitmap_bit_p (lra_constraint_insn_stack_bitmap, uid))
1732 return;
1733 bitmap_set_bit (lra_constraint_insn_stack_bitmap, uid);
1734 if (! always_update)
1735 lra_update_insn_regno_info (insn);
1736 lra_constraint_insn_stack.safe_push (insn);
1739 /* Put INSN on the stack. */
1740 void
1741 lra_push_insn (rtx_insn *insn)
1743 lra_push_insn_1 (insn, false);
1746 /* Put INSN on the stack and update its reg info. */
1747 void
1748 lra_push_insn_and_update_insn_regno_info (rtx_insn *insn)
1750 lra_push_insn_1 (insn, true);
1753 /* Put insn with UID on the stack. */
1754 void
1755 lra_push_insn_by_uid (unsigned int uid)
1757 lra_push_insn (lra_insn_recog_data[uid]->insn);
1760 /* Take the last-inserted insns off the stack and return it. */
1761 rtx_insn *
1762 lra_pop_insn (void)
1764 rtx_insn *insn = lra_constraint_insn_stack.pop ();
1765 bitmap_clear_bit (lra_constraint_insn_stack_bitmap, INSN_UID (insn));
1766 return insn;
1769 /* Return the current size of the insn stack. */
1770 unsigned int
1771 lra_insn_stack_length (void)
1773 return lra_constraint_insn_stack.length ();
1776 /* Push insns FROM to TO (excluding it) going in reverse order. */
1777 static void
1778 push_insns (rtx_insn *from, rtx_insn *to)
1780 rtx_insn *insn;
1782 if (from == NULL_RTX)
1783 return;
1784 for (insn = from; insn != to; insn = PREV_INSN (insn))
1785 if (INSN_P (insn))
1786 lra_push_insn (insn);
1789 /* Set up sp offset for insn in range [FROM, LAST]. The offset is
1790 taken from the next BB insn after LAST or zero if there in such
1791 insn. */
1792 static void
1793 setup_sp_offset (rtx_insn *from, rtx_insn *last)
1795 rtx_insn *before = next_nonnote_insn_bb (last);
1796 HOST_WIDE_INT offset = (before == NULL_RTX || ! INSN_P (before)
1797 ? 0 : lra_get_insn_recog_data (before)->sp_offset);
1799 for (rtx_insn *insn = from; insn != NEXT_INSN (last); insn = NEXT_INSN (insn))
1800 lra_get_insn_recog_data (insn)->sp_offset = offset;
1803 /* Emit insns BEFORE before INSN and insns AFTER after INSN. Put the
1804 insns onto the stack. Print about emitting the insns with
1805 TITLE. */
1806 void
1807 lra_process_new_insns (rtx_insn *insn, rtx_insn *before, rtx_insn *after,
1808 const char *title)
1810 rtx_insn *last;
1812 if (before == NULL_RTX && after == NULL_RTX)
1813 return;
1814 if (lra_dump_file != NULL)
1816 dump_insn_slim (lra_dump_file, insn);
1817 if (before != NULL_RTX)
1819 fprintf (lra_dump_file," %s before:\n", title);
1820 dump_rtl_slim (lra_dump_file, before, NULL, -1, 0);
1822 if (after != NULL_RTX)
1824 fprintf (lra_dump_file, " %s after:\n", title);
1825 dump_rtl_slim (lra_dump_file, after, NULL, -1, 0);
1827 fprintf (lra_dump_file, "\n");
1829 if (before != NULL_RTX)
1831 if (cfun->can_throw_non_call_exceptions)
1832 copy_reg_eh_region_note_forward (insn, before, NULL);
1833 emit_insn_before (before, insn);
1834 push_insns (PREV_INSN (insn), PREV_INSN (before));
1835 setup_sp_offset (before, PREV_INSN (insn));
1837 if (after != NULL_RTX)
1839 if (cfun->can_throw_non_call_exceptions)
1840 copy_reg_eh_region_note_forward (insn, after, NULL);
1841 for (last = after; NEXT_INSN (last) != NULL_RTX; last = NEXT_INSN (last))
1843 emit_insn_after (after, insn);
1844 push_insns (last, insn);
1845 setup_sp_offset (after, last);
1847 if (cfun->can_throw_non_call_exceptions)
1849 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
1850 if (note && !insn_could_throw_p (insn))
1851 remove_note (insn, note);
1856 /* Replace all references to register OLD_REGNO in *LOC with pseudo
1857 register NEW_REG. Try to simplify subreg of constant if SUBREG_P.
1858 Return true if any change was made. */
1859 bool
1860 lra_substitute_pseudo (rtx *loc, int old_regno, rtx new_reg, bool subreg_p)
1862 rtx x = *loc;
1863 bool result = false;
1864 enum rtx_code code;
1865 const char *fmt;
1866 int i, j;
1868 if (x == NULL_RTX)
1869 return false;
1871 code = GET_CODE (x);
1872 if (code == SUBREG && subreg_p)
1874 rtx subst, inner = SUBREG_REG (x);
1875 /* Transform subreg of constant while we still have inner mode
1876 of the subreg. The subreg internal should not be an insn
1877 operand. */
1878 if (REG_P (inner) && (int) REGNO (inner) == old_regno
1879 && CONSTANT_P (new_reg)
1880 && (subst = simplify_subreg (GET_MODE (x), new_reg, GET_MODE (inner),
1881 SUBREG_BYTE (x))) != NULL_RTX)
1883 *loc = subst;
1884 return true;
1888 else if (code == REG && (int) REGNO (x) == old_regno)
1890 machine_mode mode = GET_MODE (x);
1891 machine_mode inner_mode = GET_MODE (new_reg);
1893 if (mode != inner_mode
1894 && ! (CONST_INT_P (new_reg) && SCALAR_INT_MODE_P (mode)))
1896 if (GET_MODE_SIZE (mode) >= GET_MODE_SIZE (inner_mode)
1897 || ! SCALAR_INT_MODE_P (inner_mode))
1898 new_reg = gen_rtx_SUBREG (mode, new_reg, 0);
1899 else
1900 new_reg = gen_lowpart_SUBREG (mode, new_reg);
1902 *loc = new_reg;
1903 return true;
1906 /* Scan all the operand sub-expressions. */
1907 fmt = GET_RTX_FORMAT (code);
1908 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1910 if (fmt[i] == 'e')
1912 if (lra_substitute_pseudo (&XEXP (x, i), old_regno,
1913 new_reg, subreg_p))
1914 result = true;
1916 else if (fmt[i] == 'E')
1918 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1919 if (lra_substitute_pseudo (&XVECEXP (x, i, j), old_regno,
1920 new_reg, subreg_p))
1921 result = true;
1924 return result;
1927 /* Call lra_substitute_pseudo within an insn. Try to simplify subreg
1928 of constant if SUBREG_P. This won't update the insn ptr, just the
1929 contents of the insn. */
1930 bool
1931 lra_substitute_pseudo_within_insn (rtx_insn *insn, int old_regno,
1932 rtx new_reg, bool subreg_p)
1934 rtx loc = insn;
1935 return lra_substitute_pseudo (&loc, old_regno, new_reg, subreg_p);
1940 /* This page contains code dealing with scratches (changing them onto
1941 pseudos and restoring them from the pseudos).
1943 We change scratches into pseudos at the beginning of LRA to
1944 simplify dealing with them (conflicts, hard register assignments).
1946 If the pseudo denoting scratch was spilled it means that we do need
1947 a hard register for it. Such pseudos are transformed back to
1948 scratches at the end of LRA. */
1950 /* Description of location of a former scratch operand. */
1951 struct sloc
1953 rtx_insn *insn; /* Insn where the scratch was. */
1954 int nop; /* Number of the operand which was a scratch. */
1957 typedef struct sloc *sloc_t;
1959 /* Locations of the former scratches. */
1960 static vec<sloc_t> scratches;
1962 /* Bitmap of scratch regnos. */
1963 static bitmap_head scratch_bitmap;
1965 /* Bitmap of scratch operands. */
1966 static bitmap_head scratch_operand_bitmap;
1968 /* Return true if pseudo REGNO is made of SCRATCH. */
1969 bool
1970 lra_former_scratch_p (int regno)
1972 return bitmap_bit_p (&scratch_bitmap, regno);
1975 /* Return true if the operand NOP of INSN is a former scratch. */
1976 bool
1977 lra_former_scratch_operand_p (rtx_insn *insn, int nop)
1979 return bitmap_bit_p (&scratch_operand_bitmap,
1980 INSN_UID (insn) * MAX_RECOG_OPERANDS + nop) != 0;
1983 /* Register operand NOP in INSN as a former scratch. It will be
1984 changed to scratch back, if it is necessary, at the LRA end. */
1985 void
1986 lra_register_new_scratch_op (rtx_insn *insn, int nop)
1988 lra_insn_recog_data_t id = lra_get_insn_recog_data (insn);
1989 rtx op = *id->operand_loc[nop];
1990 sloc_t loc = XNEW (struct sloc);
1991 lra_assert (REG_P (op));
1992 loc->insn = insn;
1993 loc->nop = nop;
1994 scratches.safe_push (loc);
1995 bitmap_set_bit (&scratch_bitmap, REGNO (op));
1996 bitmap_set_bit (&scratch_operand_bitmap,
1997 INSN_UID (insn) * MAX_RECOG_OPERANDS + nop);
1998 add_reg_note (insn, REG_UNUSED, op);
2001 /* Change scratches onto pseudos and save their location. */
2002 static void
2003 remove_scratches (void)
2005 int i;
2006 bool insn_changed_p;
2007 basic_block bb;
2008 rtx_insn *insn;
2009 rtx reg;
2010 lra_insn_recog_data_t id;
2011 struct lra_static_insn_data *static_id;
2013 scratches.create (get_max_uid ());
2014 bitmap_initialize (&scratch_bitmap, &reg_obstack);
2015 bitmap_initialize (&scratch_operand_bitmap, &reg_obstack);
2016 FOR_EACH_BB_FN (bb, cfun)
2017 FOR_BB_INSNS (bb, insn)
2018 if (INSN_P (insn))
2020 id = lra_get_insn_recog_data (insn);
2021 static_id = id->insn_static_data;
2022 insn_changed_p = false;
2023 for (i = 0; i < static_id->n_operands; i++)
2024 if (GET_CODE (*id->operand_loc[i]) == SCRATCH
2025 && GET_MODE (*id->operand_loc[i]) != VOIDmode)
2027 insn_changed_p = true;
2028 *id->operand_loc[i] = reg
2029 = lra_create_new_reg (static_id->operand[i].mode,
2030 *id->operand_loc[i], ALL_REGS, NULL);
2031 lra_register_new_scratch_op (insn, i);
2032 if (lra_dump_file != NULL)
2033 fprintf (lra_dump_file,
2034 "Removing SCRATCH in insn #%u (nop %d)\n",
2035 INSN_UID (insn), i);
2037 if (insn_changed_p)
2038 /* Because we might use DF right after caller-saves sub-pass
2039 we need to keep DF info up to date. */
2040 df_insn_rescan (insn);
2044 /* Changes pseudos created by function remove_scratches onto scratches. */
2045 static void
2046 restore_scratches (void)
2048 int regno;
2049 unsigned i;
2050 sloc_t loc;
2051 rtx_insn *last = NULL;
2052 lra_insn_recog_data_t id = NULL;
2054 for (i = 0; scratches.iterate (i, &loc); i++)
2056 /* Ignore already deleted insns. */
2057 if (NOTE_P (loc->insn)
2058 && NOTE_KIND (loc->insn) == NOTE_INSN_DELETED)
2059 continue;
2060 if (last != loc->insn)
2062 last = loc->insn;
2063 id = lra_get_insn_recog_data (last);
2065 if (REG_P (*id->operand_loc[loc->nop])
2066 && ((regno = REGNO (*id->operand_loc[loc->nop]))
2067 >= FIRST_PSEUDO_REGISTER)
2068 && lra_get_regno_hard_regno (regno) < 0)
2070 /* It should be only case when scratch register with chosen
2071 constraint 'X' did not get memory or hard register. */
2072 lra_assert (lra_former_scratch_p (regno));
2073 *id->operand_loc[loc->nop]
2074 = gen_rtx_SCRATCH (GET_MODE (*id->operand_loc[loc->nop]));
2075 lra_update_dup (id, loc->nop);
2076 if (lra_dump_file != NULL)
2077 fprintf (lra_dump_file, "Restoring SCRATCH in insn #%u(nop %d)\n",
2078 INSN_UID (loc->insn), loc->nop);
2081 for (i = 0; scratches.iterate (i, &loc); i++)
2082 free (loc);
2083 scratches.release ();
2084 bitmap_clear (&scratch_bitmap);
2085 bitmap_clear (&scratch_operand_bitmap);
2090 /* Function checks RTL for correctness. If FINAL_P is true, it is
2091 done at the end of LRA and the check is more rigorous. */
2092 static void
2093 check_rtl (bool final_p)
2095 basic_block bb;
2096 rtx_insn *insn;
2098 lra_assert (! final_p || reload_completed);
2099 FOR_EACH_BB_FN (bb, cfun)
2100 FOR_BB_INSNS (bb, insn)
2101 if (NONDEBUG_INSN_P (insn)
2102 && GET_CODE (PATTERN (insn)) != USE
2103 && GET_CODE (PATTERN (insn)) != CLOBBER
2104 && GET_CODE (PATTERN (insn)) != ASM_INPUT)
2106 if (final_p)
2108 extract_constrain_insn (insn);
2109 continue;
2111 /* LRA code is based on assumption that all addresses can be
2112 correctly decomposed. LRA can generate reloads for
2113 decomposable addresses. The decomposition code checks the
2114 correctness of the addresses. So we don't need to check
2115 the addresses here. Don't call insn_invalid_p here, it can
2116 change the code at this stage. */
2117 if (recog_memoized (insn) < 0 && asm_noperands (PATTERN (insn)) < 0)
2118 fatal_insn_not_found (insn);
2122 /* Determine if the current function has an exception receiver block
2123 that reaches the exit block via non-exceptional edges */
2124 static bool
2125 has_nonexceptional_receiver (void)
2127 edge e;
2128 edge_iterator ei;
2129 basic_block *tos, *worklist, bb;
2131 /* If we're not optimizing, then just err on the safe side. */
2132 if (!optimize)
2133 return true;
2135 /* First determine which blocks can reach exit via normal paths. */
2136 tos = worklist = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun) + 1);
2138 FOR_EACH_BB_FN (bb, cfun)
2139 bb->flags &= ~BB_REACHABLE;
2141 /* Place the exit block on our worklist. */
2142 EXIT_BLOCK_PTR_FOR_FN (cfun)->flags |= BB_REACHABLE;
2143 *tos++ = EXIT_BLOCK_PTR_FOR_FN (cfun);
2145 /* Iterate: find everything reachable from what we've already seen. */
2146 while (tos != worklist)
2148 bb = *--tos;
2150 FOR_EACH_EDGE (e, ei, bb->preds)
2151 if (e->flags & EDGE_ABNORMAL)
2153 free (worklist);
2154 return true;
2156 else
2158 basic_block src = e->src;
2160 if (!(src->flags & BB_REACHABLE))
2162 src->flags |= BB_REACHABLE;
2163 *tos++ = src;
2167 free (worklist);
2168 /* No exceptional block reached exit unexceptionally. */
2169 return false;
2173 /* Process recursively X of INSN and add REG_INC notes if necessary. */
2174 static void
2175 add_auto_inc_notes (rtx_insn *insn, rtx x)
2177 enum rtx_code code = GET_CODE (x);
2178 const char *fmt;
2179 int i, j;
2181 if (code == MEM && auto_inc_p (XEXP (x, 0)))
2183 add_reg_note (insn, REG_INC, XEXP (XEXP (x, 0), 0));
2184 return;
2187 /* Scan all X sub-expressions. */
2188 fmt = GET_RTX_FORMAT (code);
2189 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2191 if (fmt[i] == 'e')
2192 add_auto_inc_notes (insn, XEXP (x, i));
2193 else if (fmt[i] == 'E')
2194 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2195 add_auto_inc_notes (insn, XVECEXP (x, i, j));
2200 /* Remove all REG_DEAD and REG_UNUSED notes and regenerate REG_INC.
2201 We change pseudos by hard registers without notification of DF and
2202 that can make the notes obsolete. DF-infrastructure does not deal
2203 with REG_INC notes -- so we should regenerate them here. */
2204 static void
2205 update_inc_notes (void)
2207 rtx *pnote;
2208 basic_block bb;
2209 rtx_insn *insn;
2211 FOR_EACH_BB_FN (bb, cfun)
2212 FOR_BB_INSNS (bb, insn)
2213 if (NONDEBUG_INSN_P (insn))
2215 pnote = &REG_NOTES (insn);
2216 while (*pnote != 0)
2218 if (REG_NOTE_KIND (*pnote) == REG_DEAD
2219 || REG_NOTE_KIND (*pnote) == REG_UNUSED
2220 || REG_NOTE_KIND (*pnote) == REG_INC)
2221 *pnote = XEXP (*pnote, 1);
2222 else
2223 pnote = &XEXP (*pnote, 1);
2226 if (AUTO_INC_DEC)
2227 add_auto_inc_notes (insn, PATTERN (insn));
2231 /* Set to 1 while in lra. */
2232 int lra_in_progress;
2234 /* Start of pseudo regnos before the LRA. */
2235 int lra_new_regno_start;
2237 /* Start of reload pseudo regnos before the new spill pass. */
2238 int lra_constraint_new_regno_start;
2240 /* Avoid spilling pseudos with regno more than the following value if
2241 it is possible. */
2242 int lra_bad_spill_regno_start;
2244 /* Inheritance pseudo regnos before the new spill pass. */
2245 bitmap_head lra_inheritance_pseudos;
2247 /* Split regnos before the new spill pass. */
2248 bitmap_head lra_split_regs;
2250 /* Reload pseudo regnos before the new assignmnet pass which still can
2251 be spilled after the assinment pass as memory is also accepted in
2252 insns for the reload pseudos. */
2253 bitmap_head lra_optional_reload_pseudos;
2255 /* Pseudo regnos used for subreg reloads before the new assignment
2256 pass. Such pseudos still can be spilled after the assinment
2257 pass. */
2258 bitmap_head lra_subreg_reload_pseudos;
2260 /* File used for output of LRA debug information. */
2261 FILE *lra_dump_file;
2263 /* True if we should try spill into registers of different classes
2264 instead of memory. */
2265 bool lra_reg_spill_p;
2267 /* Set up value LRA_REG_SPILL_P. */
2268 static void
2269 setup_reg_spill_flag (void)
2271 int cl, mode;
2273 if (targetm.spill_class != NULL)
2274 for (cl = 0; cl < (int) LIM_REG_CLASSES; cl++)
2275 for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
2276 if (targetm.spill_class ((enum reg_class) cl,
2277 (machine_mode) mode) != NO_REGS)
2279 lra_reg_spill_p = true;
2280 return;
2282 lra_reg_spill_p = false;
2285 /* True if the current function is too big to use regular algorithms
2286 in LRA. In other words, we should use simpler and faster algorithms
2287 in LRA. It also means we should not worry about generation code
2288 for caller saves. The value is set up in IRA. */
2289 bool lra_simple_p;
2291 /* Major LRA entry function. F is a file should be used to dump LRA
2292 debug info. */
2293 void
2294 lra (FILE *f)
2296 int i;
2297 bool live_p, scratch_p, inserted_p;
2299 lra_dump_file = f;
2301 timevar_push (TV_LRA);
2303 /* Make sure that the last insn is a note. Some subsequent passes
2304 need it. */
2305 emit_note (NOTE_INSN_DELETED);
2307 COPY_HARD_REG_SET (lra_no_alloc_regs, ira_no_alloc_regs);
2309 init_reg_info ();
2310 expand_reg_info ();
2312 init_insn_recog_data ();
2314 /* Some quick check on RTL generated by previous passes. */
2315 if (flag_checking)
2316 check_rtl (false);
2318 lra_in_progress = 1;
2320 lra_live_range_iter = lra_coalesce_iter = lra_constraint_iter = 0;
2321 lra_assignment_iter = lra_assignment_iter_after_spill = 0;
2322 lra_inheritance_iter = lra_undo_inheritance_iter = 0;
2323 lra_rematerialization_iter = 0;
2325 setup_reg_spill_flag ();
2327 /* Function remove_scratches can creates new pseudos for clobbers --
2328 so set up lra_constraint_new_regno_start before its call to
2329 permit changing reg classes for pseudos created by this
2330 simplification. */
2331 lra_constraint_new_regno_start = lra_new_regno_start = max_reg_num ();
2332 lra_bad_spill_regno_start = INT_MAX;
2333 remove_scratches ();
2334 scratch_p = lra_constraint_new_regno_start != max_reg_num ();
2336 /* A function that has a non-local label that can reach the exit
2337 block via non-exceptional paths must save all call-saved
2338 registers. */
2339 if (cfun->has_nonlocal_label && has_nonexceptional_receiver ())
2340 crtl->saves_all_registers = 1;
2342 if (crtl->saves_all_registers)
2343 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2344 if (! call_used_regs[i] && ! fixed_regs[i] && ! LOCAL_REGNO (i))
2345 df_set_regs_ever_live (i, true);
2347 /* We don't DF from now and avoid its using because it is to
2348 expensive when a lot of RTL changes are made. */
2349 df_set_flags (DF_NO_INSN_RESCAN);
2350 lra_constraint_insn_stack.create (get_max_uid ());
2351 lra_constraint_insn_stack_bitmap = sbitmap_alloc (get_max_uid ());
2352 bitmap_clear (lra_constraint_insn_stack_bitmap);
2353 lra_live_ranges_init ();
2354 lra_constraints_init ();
2355 lra_curr_reload_num = 0;
2356 push_insns (get_last_insn (), NULL);
2357 /* It is needed for the 1st coalescing. */
2358 bitmap_initialize (&lra_inheritance_pseudos, &reg_obstack);
2359 bitmap_initialize (&lra_split_regs, &reg_obstack);
2360 bitmap_initialize (&lra_optional_reload_pseudos, &reg_obstack);
2361 bitmap_initialize (&lra_subreg_reload_pseudos, &reg_obstack);
2362 live_p = false;
2363 if (get_frame_size () != 0 && crtl->stack_alignment_needed)
2364 /* If we have a stack frame, we must align it now. The stack size
2365 may be a part of the offset computation for register
2366 elimination. */
2367 assign_stack_local (BLKmode, 0, crtl->stack_alignment_needed);
2368 lra_init_equiv ();
2369 for (;;)
2371 for (;;)
2373 /* We should try to assign hard registers to scratches even
2374 if there were no RTL transformations in
2375 lra_constraints. */
2376 if (! lra_constraints (lra_constraint_iter == 0)
2377 && (lra_constraint_iter > 1
2378 || (! scratch_p && ! caller_save_needed)))
2379 break;
2380 /* Constraint transformations may result in that eliminable
2381 hard regs become uneliminable and pseudos which use them
2382 should be spilled. It is better to do it before pseudo
2383 assignments.
2385 For example, rs6000 can make
2386 RS6000_PIC_OFFSET_TABLE_REGNUM uneliminable if we started
2387 to use a constant pool. */
2388 lra_eliminate (false, false);
2389 /* Do inheritance only for regular algorithms. */
2390 if (! lra_simple_p)
2392 if (flag_ipa_ra)
2394 if (live_p)
2395 lra_clear_live_ranges ();
2396 /* As a side-effect of lra_create_live_ranges, we calculate
2397 actual_call_used_reg_set, which is needed during
2398 lra_inheritance. */
2399 lra_create_live_ranges (true, true);
2400 live_p = true;
2402 lra_inheritance ();
2404 if (live_p)
2405 lra_clear_live_ranges ();
2406 /* We need live ranges for lra_assign -- so build them. But
2407 don't remove dead insns or change global live info as we
2408 can undo inheritance transformations after inheritance
2409 pseudo assigning. */
2410 lra_create_live_ranges (true, false);
2411 live_p = true;
2412 /* If we don't spill non-reload and non-inheritance pseudos,
2413 there is no sense to run memory-memory move coalescing.
2414 If inheritance pseudos were spilled, the memory-memory
2415 moves involving them will be removed by pass undoing
2416 inheritance. */
2417 if (lra_simple_p)
2418 lra_assign ();
2419 else
2421 bool spill_p = !lra_assign ();
2423 if (lra_undo_inheritance ())
2424 live_p = false;
2425 if (spill_p)
2427 if (! live_p)
2429 lra_create_live_ranges (true, true);
2430 live_p = true;
2432 if (lra_coalesce ())
2433 live_p = false;
2435 if (! live_p)
2436 lra_clear_live_ranges ();
2439 /* Don't clear optional reloads bitmap until all constraints are
2440 satisfied as we need to differ them from regular reloads. */
2441 bitmap_clear (&lra_optional_reload_pseudos);
2442 bitmap_clear (&lra_subreg_reload_pseudos);
2443 bitmap_clear (&lra_inheritance_pseudos);
2444 bitmap_clear (&lra_split_regs);
2445 if (! live_p)
2447 /* We need full live info for spilling pseudos into
2448 registers instead of memory. */
2449 lra_create_live_ranges (lra_reg_spill_p, true);
2450 live_p = true;
2452 /* We should check necessity for spilling here as the above live
2453 range pass can remove spilled pseudos. */
2454 if (! lra_need_for_spills_p ())
2455 break;
2456 /* Now we know what pseudos should be spilled. Try to
2457 rematerialize them first. */
2458 if (lra_remat ())
2460 /* We need full live info -- see the comment above. */
2461 lra_create_live_ranges (lra_reg_spill_p, true);
2462 live_p = true;
2463 if (! lra_need_for_spills_p ())
2464 break;
2466 lra_spill ();
2467 /* Assignment of stack slots changes elimination offsets for
2468 some eliminations. So update the offsets here. */
2469 lra_eliminate (false, false);
2470 lra_constraint_new_regno_start = max_reg_num ();
2471 if (lra_bad_spill_regno_start == INT_MAX
2472 && lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES
2473 && lra_rematerialization_iter > LRA_MAX_REMATERIALIZATION_PASSES)
2474 /* After switching off inheritance and rematerialization
2475 passes, avoid spilling reload pseudos will be created to
2476 prevent LRA cycling in some complicated cases. */
2477 lra_bad_spill_regno_start = lra_constraint_new_regno_start;
2478 lra_assignment_iter_after_spill = 0;
2480 restore_scratches ();
2481 lra_eliminate (true, false);
2482 lra_final_code_change ();
2483 lra_in_progress = 0;
2484 if (live_p)
2485 lra_clear_live_ranges ();
2486 lra_live_ranges_finish ();
2487 lra_constraints_finish ();
2488 finish_reg_info ();
2489 sbitmap_free (lra_constraint_insn_stack_bitmap);
2490 lra_constraint_insn_stack.release ();
2491 finish_insn_recog_data ();
2492 regstat_free_n_sets_and_refs ();
2493 regstat_free_ri ();
2494 reload_completed = 1;
2495 update_inc_notes ();
2497 inserted_p = fixup_abnormal_edges ();
2499 /* We've possibly turned single trapping insn into multiple ones. */
2500 if (cfun->can_throw_non_call_exceptions)
2502 auto_sbitmap blocks (last_basic_block_for_fn (cfun));
2503 bitmap_ones (blocks);
2504 find_many_sub_basic_blocks (blocks);
2507 if (inserted_p)
2508 commit_edge_insertions ();
2510 /* Replacing pseudos with their memory equivalents might have
2511 created shared rtx. Subsequent passes would get confused
2512 by this, so unshare everything here. */
2513 unshare_all_rtl_again (get_insns ());
2515 if (flag_checking)
2516 check_rtl (true);
2518 timevar_pop (TV_LRA);
2521 /* Called once per compiler to initialize LRA data once. */
2522 void
2523 lra_init_once (void)
2525 init_insn_code_data_once ();
2528 /* Called once per compiler to finish LRA data which are initialize
2529 once. */
2530 void
2531 lra_finish_once (void)
2533 finish_insn_code_data_once ();