Remove assert in get_def_bb_for_const
[official-gcc.git] / gcc / lra.c
bloba724ef782c257a838b1db49e5dd83d0f76f8c1ca
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_regno = -1;
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 /* This page contains code dealing with stack of the insns which
1625 should be processed by the next constraint pass. */
1627 /* Bitmap used to put an insn on the stack only in one exemplar. */
1628 static sbitmap lra_constraint_insn_stack_bitmap;
1630 /* The stack itself. */
1631 vec<rtx_insn *> lra_constraint_insn_stack;
1633 /* Put INSN on the stack. If ALWAYS_UPDATE is true, always update the reg
1634 info for INSN, otherwise only update it if INSN is not already on the
1635 stack. */
1636 static inline void
1637 lra_push_insn_1 (rtx_insn *insn, bool always_update)
1639 unsigned int uid = INSN_UID (insn);
1640 if (always_update)
1641 lra_update_insn_regno_info (insn);
1642 if (uid >= SBITMAP_SIZE (lra_constraint_insn_stack_bitmap))
1643 lra_constraint_insn_stack_bitmap =
1644 sbitmap_resize (lra_constraint_insn_stack_bitmap, 3 * uid / 2, 0);
1645 if (bitmap_bit_p (lra_constraint_insn_stack_bitmap, uid))
1646 return;
1647 bitmap_set_bit (lra_constraint_insn_stack_bitmap, uid);
1648 if (! always_update)
1649 lra_update_insn_regno_info (insn);
1650 lra_constraint_insn_stack.safe_push (insn);
1653 /* Put INSN on the stack. */
1654 void
1655 lra_push_insn (rtx_insn *insn)
1657 lra_push_insn_1 (insn, false);
1660 /* Put INSN on the stack and update its reg info. */
1661 void
1662 lra_push_insn_and_update_insn_regno_info (rtx_insn *insn)
1664 lra_push_insn_1 (insn, true);
1667 /* Put insn with UID on the stack. */
1668 void
1669 lra_push_insn_by_uid (unsigned int uid)
1671 lra_push_insn (lra_insn_recog_data[uid]->insn);
1674 /* Take the last-inserted insns off the stack and return it. */
1675 rtx_insn *
1676 lra_pop_insn (void)
1678 rtx_insn *insn = lra_constraint_insn_stack.pop ();
1679 bitmap_clear_bit (lra_constraint_insn_stack_bitmap, INSN_UID (insn));
1680 return insn;
1683 /* Return the current size of the insn stack. */
1684 unsigned int
1685 lra_insn_stack_length (void)
1687 return lra_constraint_insn_stack.length ();
1690 /* Push insns FROM to TO (excluding it) going in reverse order. */
1691 static void
1692 push_insns (rtx_insn *from, rtx_insn *to)
1694 rtx_insn *insn;
1696 if (from == NULL_RTX)
1697 return;
1698 for (insn = from; insn != to; insn = PREV_INSN (insn))
1699 if (INSN_P (insn))
1700 lra_push_insn (insn);
1703 /* Set up sp offset for insn in range [FROM, LAST]. The offset is
1704 taken from the next BB insn after LAST or zero if there in such
1705 insn. */
1706 static void
1707 setup_sp_offset (rtx_insn *from, rtx_insn *last)
1709 rtx_insn *before = next_nonnote_insn_bb (last);
1710 HOST_WIDE_INT offset = (before == NULL_RTX || ! INSN_P (before)
1711 ? 0 : lra_get_insn_recog_data (before)->sp_offset);
1713 for (rtx_insn *insn = from; insn != NEXT_INSN (last); insn = NEXT_INSN (insn))
1714 lra_get_insn_recog_data (insn)->sp_offset = offset;
1717 /* Emit insns BEFORE before INSN and insns AFTER after INSN. Put the
1718 insns onto the stack. Print about emitting the insns with
1719 TITLE. */
1720 void
1721 lra_process_new_insns (rtx_insn *insn, rtx_insn *before, rtx_insn *after,
1722 const char *title)
1724 rtx_insn *last;
1726 if (before == NULL_RTX && after == NULL_RTX)
1727 return;
1728 if (lra_dump_file != NULL)
1730 dump_insn_slim (lra_dump_file, insn);
1731 if (before != NULL_RTX)
1733 fprintf (lra_dump_file," %s before:\n", title);
1734 dump_rtl_slim (lra_dump_file, before, NULL, -1, 0);
1736 if (after != NULL_RTX)
1738 fprintf (lra_dump_file, " %s after:\n", title);
1739 dump_rtl_slim (lra_dump_file, after, NULL, -1, 0);
1741 fprintf (lra_dump_file, "\n");
1743 if (before != NULL_RTX)
1745 if (cfun->can_throw_non_call_exceptions)
1746 copy_reg_eh_region_note_forward (insn, before, NULL);
1747 emit_insn_before (before, insn);
1748 push_insns (PREV_INSN (insn), PREV_INSN (before));
1749 setup_sp_offset (before, PREV_INSN (insn));
1751 if (after != NULL_RTX)
1753 if (cfun->can_throw_non_call_exceptions)
1754 copy_reg_eh_region_note_forward (insn, after, NULL);
1755 for (last = after; NEXT_INSN (last) != NULL_RTX; last = NEXT_INSN (last))
1757 emit_insn_after (after, insn);
1758 push_insns (last, insn);
1759 setup_sp_offset (after, last);
1761 if (cfun->can_throw_non_call_exceptions)
1763 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
1764 if (note && !insn_could_throw_p (insn))
1765 remove_note (insn, note);
1770 /* Replace all references to register OLD_REGNO in *LOC with pseudo
1771 register NEW_REG. Try to simplify subreg of constant if SUBREG_P.
1772 Return true if any change was made. */
1773 bool
1774 lra_substitute_pseudo (rtx *loc, int old_regno, rtx new_reg, bool subreg_p)
1776 rtx x = *loc;
1777 bool result = false;
1778 enum rtx_code code;
1779 const char *fmt;
1780 int i, j;
1782 if (x == NULL_RTX)
1783 return false;
1785 code = GET_CODE (x);
1786 if (code == SUBREG && subreg_p)
1788 rtx subst, inner = SUBREG_REG (x);
1789 /* Transform subreg of constant while we still have inner mode
1790 of the subreg. The subreg internal should not be an insn
1791 operand. */
1792 if (REG_P (inner) && (int) REGNO (inner) == old_regno
1793 && CONSTANT_P (new_reg)
1794 && (subst = simplify_subreg (GET_MODE (x), new_reg, GET_MODE (inner),
1795 SUBREG_BYTE (x))) != NULL_RTX)
1797 *loc = subst;
1798 return true;
1802 else if (code == REG && (int) REGNO (x) == old_regno)
1804 machine_mode mode = GET_MODE (x);
1805 machine_mode inner_mode = GET_MODE (new_reg);
1807 if (mode != inner_mode
1808 && ! (CONST_INT_P (new_reg) && SCALAR_INT_MODE_P (mode)))
1810 if (GET_MODE_SIZE (mode) >= GET_MODE_SIZE (inner_mode)
1811 || ! SCALAR_INT_MODE_P (inner_mode))
1812 new_reg = gen_rtx_SUBREG (mode, new_reg, 0);
1813 else
1814 new_reg = gen_lowpart_SUBREG (mode, new_reg);
1816 *loc = new_reg;
1817 return true;
1820 /* Scan all the operand sub-expressions. */
1821 fmt = GET_RTX_FORMAT (code);
1822 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1824 if (fmt[i] == 'e')
1826 if (lra_substitute_pseudo (&XEXP (x, i), old_regno,
1827 new_reg, subreg_p))
1828 result = true;
1830 else if (fmt[i] == 'E')
1832 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1833 if (lra_substitute_pseudo (&XVECEXP (x, i, j), old_regno,
1834 new_reg, subreg_p))
1835 result = true;
1838 return result;
1841 /* Call lra_substitute_pseudo within an insn. Try to simplify subreg
1842 of constant if SUBREG_P. This won't update the insn ptr, just the
1843 contents of the insn. */
1844 bool
1845 lra_substitute_pseudo_within_insn (rtx_insn *insn, int old_regno,
1846 rtx new_reg, bool subreg_p)
1848 rtx loc = insn;
1849 return lra_substitute_pseudo (&loc, old_regno, new_reg, subreg_p);
1854 /* This page contains code dealing with scratches (changing them onto
1855 pseudos and restoring them from the pseudos).
1857 We change scratches into pseudos at the beginning of LRA to
1858 simplify dealing with them (conflicts, hard register assignments).
1860 If the pseudo denoting scratch was spilled it means that we do need
1861 a hard register for it. Such pseudos are transformed back to
1862 scratches at the end of LRA. */
1864 /* Description of location of a former scratch operand. */
1865 struct sloc
1867 rtx_insn *insn; /* Insn where the scratch was. */
1868 int nop; /* Number of the operand which was a scratch. */
1871 typedef struct sloc *sloc_t;
1873 /* Locations of the former scratches. */
1874 static vec<sloc_t> scratches;
1876 /* Bitmap of scratch regnos. */
1877 static bitmap_head scratch_bitmap;
1879 /* Bitmap of scratch operands. */
1880 static bitmap_head scratch_operand_bitmap;
1882 /* Return true if pseudo REGNO is made of SCRATCH. */
1883 bool
1884 lra_former_scratch_p (int regno)
1886 return bitmap_bit_p (&scratch_bitmap, regno);
1889 /* Return true if the operand NOP of INSN is a former scratch. */
1890 bool
1891 lra_former_scratch_operand_p (rtx_insn *insn, int nop)
1893 return bitmap_bit_p (&scratch_operand_bitmap,
1894 INSN_UID (insn) * MAX_RECOG_OPERANDS + nop) != 0;
1897 /* Register operand NOP in INSN as a former scratch. It will be
1898 changed to scratch back, if it is necessary, at the LRA end. */
1899 void
1900 lra_register_new_scratch_op (rtx_insn *insn, int nop)
1902 lra_insn_recog_data_t id = lra_get_insn_recog_data (insn);
1903 rtx op = *id->operand_loc[nop];
1904 sloc_t loc = XNEW (struct sloc);
1905 lra_assert (REG_P (op));
1906 loc->insn = insn;
1907 loc->nop = nop;
1908 scratches.safe_push (loc);
1909 bitmap_set_bit (&scratch_bitmap, REGNO (op));
1910 bitmap_set_bit (&scratch_operand_bitmap,
1911 INSN_UID (insn) * MAX_RECOG_OPERANDS + nop);
1912 add_reg_note (insn, REG_UNUSED, op);
1915 /* Change scratches onto pseudos and save their location. */
1916 static void
1917 remove_scratches (void)
1919 int i;
1920 bool insn_changed_p;
1921 basic_block bb;
1922 rtx_insn *insn;
1923 rtx reg;
1924 lra_insn_recog_data_t id;
1925 struct lra_static_insn_data *static_id;
1927 scratches.create (get_max_uid ());
1928 bitmap_initialize (&scratch_bitmap, &reg_obstack);
1929 bitmap_initialize (&scratch_operand_bitmap, &reg_obstack);
1930 FOR_EACH_BB_FN (bb, cfun)
1931 FOR_BB_INSNS (bb, insn)
1932 if (INSN_P (insn))
1934 id = lra_get_insn_recog_data (insn);
1935 static_id = id->insn_static_data;
1936 insn_changed_p = false;
1937 for (i = 0; i < static_id->n_operands; i++)
1938 if (GET_CODE (*id->operand_loc[i]) == SCRATCH
1939 && GET_MODE (*id->operand_loc[i]) != VOIDmode)
1941 insn_changed_p = true;
1942 *id->operand_loc[i] = reg
1943 = lra_create_new_reg (static_id->operand[i].mode,
1944 *id->operand_loc[i], ALL_REGS, NULL);
1945 lra_register_new_scratch_op (insn, i);
1946 if (lra_dump_file != NULL)
1947 fprintf (lra_dump_file,
1948 "Removing SCRATCH in insn #%u (nop %d)\n",
1949 INSN_UID (insn), i);
1951 if (insn_changed_p)
1952 /* Because we might use DF right after caller-saves sub-pass
1953 we need to keep DF info up to date. */
1954 df_insn_rescan (insn);
1958 /* Changes pseudos created by function remove_scratches onto scratches. */
1959 static void
1960 restore_scratches (void)
1962 int regno;
1963 unsigned i;
1964 sloc_t loc;
1965 rtx_insn *last = NULL;
1966 lra_insn_recog_data_t id = NULL;
1968 for (i = 0; scratches.iterate (i, &loc); i++)
1970 /* Ignore already deleted insns. */
1971 if (NOTE_P (loc->insn)
1972 && NOTE_KIND (loc->insn) == NOTE_INSN_DELETED)
1973 continue;
1974 if (last != loc->insn)
1976 last = loc->insn;
1977 id = lra_get_insn_recog_data (last);
1979 if (REG_P (*id->operand_loc[loc->nop])
1980 && ((regno = REGNO (*id->operand_loc[loc->nop]))
1981 >= FIRST_PSEUDO_REGISTER)
1982 && lra_get_regno_hard_regno (regno) < 0)
1984 /* It should be only case when scratch register with chosen
1985 constraint 'X' did not get memory or hard register. */
1986 lra_assert (lra_former_scratch_p (regno));
1987 *id->operand_loc[loc->nop]
1988 = gen_rtx_SCRATCH (GET_MODE (*id->operand_loc[loc->nop]));
1989 lra_update_dup (id, loc->nop);
1990 if (lra_dump_file != NULL)
1991 fprintf (lra_dump_file, "Restoring SCRATCH in insn #%u(nop %d)\n",
1992 INSN_UID (loc->insn), loc->nop);
1995 for (i = 0; scratches.iterate (i, &loc); i++)
1996 free (loc);
1997 scratches.release ();
1998 bitmap_clear (&scratch_bitmap);
1999 bitmap_clear (&scratch_operand_bitmap);
2004 /* Function checks RTL for correctness. If FINAL_P is true, it is
2005 done at the end of LRA and the check is more rigorous. */
2006 static void
2007 check_rtl (bool final_p)
2009 basic_block bb;
2010 rtx_insn *insn;
2012 lra_assert (! final_p || reload_completed);
2013 FOR_EACH_BB_FN (bb, cfun)
2014 FOR_BB_INSNS (bb, insn)
2015 if (NONDEBUG_INSN_P (insn)
2016 && GET_CODE (PATTERN (insn)) != USE
2017 && GET_CODE (PATTERN (insn)) != CLOBBER
2018 && GET_CODE (PATTERN (insn)) != ASM_INPUT)
2020 if (final_p)
2022 extract_constrain_insn (insn);
2023 continue;
2025 /* LRA code is based on assumption that all addresses can be
2026 correctly decomposed. LRA can generate reloads for
2027 decomposable addresses. The decomposition code checks the
2028 correctness of the addresses. So we don't need to check
2029 the addresses here. Don't call insn_invalid_p here, it can
2030 change the code at this stage. */
2031 if (recog_memoized (insn) < 0 && asm_noperands (PATTERN (insn)) < 0)
2032 fatal_insn_not_found (insn);
2036 /* Determine if the current function has an exception receiver block
2037 that reaches the exit block via non-exceptional edges */
2038 static bool
2039 has_nonexceptional_receiver (void)
2041 edge e;
2042 edge_iterator ei;
2043 basic_block *tos, *worklist, bb;
2045 /* If we're not optimizing, then just err on the safe side. */
2046 if (!optimize)
2047 return true;
2049 /* First determine which blocks can reach exit via normal paths. */
2050 tos = worklist = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun) + 1);
2052 FOR_EACH_BB_FN (bb, cfun)
2053 bb->flags &= ~BB_REACHABLE;
2055 /* Place the exit block on our worklist. */
2056 EXIT_BLOCK_PTR_FOR_FN (cfun)->flags |= BB_REACHABLE;
2057 *tos++ = EXIT_BLOCK_PTR_FOR_FN (cfun);
2059 /* Iterate: find everything reachable from what we've already seen. */
2060 while (tos != worklist)
2062 bb = *--tos;
2064 FOR_EACH_EDGE (e, ei, bb->preds)
2065 if (e->flags & EDGE_ABNORMAL)
2067 free (worklist);
2068 return true;
2070 else
2072 basic_block src = e->src;
2074 if (!(src->flags & BB_REACHABLE))
2076 src->flags |= BB_REACHABLE;
2077 *tos++ = src;
2081 free (worklist);
2082 /* No exceptional block reached exit unexceptionally. */
2083 return false;
2087 /* Process recursively X of INSN and add REG_INC notes if necessary. */
2088 static void
2089 add_auto_inc_notes (rtx_insn *insn, rtx x)
2091 enum rtx_code code = GET_CODE (x);
2092 const char *fmt;
2093 int i, j;
2095 if (code == MEM && auto_inc_p (XEXP (x, 0)))
2097 add_reg_note (insn, REG_INC, XEXP (XEXP (x, 0), 0));
2098 return;
2101 /* Scan all X sub-expressions. */
2102 fmt = GET_RTX_FORMAT (code);
2103 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2105 if (fmt[i] == 'e')
2106 add_auto_inc_notes (insn, XEXP (x, i));
2107 else if (fmt[i] == 'E')
2108 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2109 add_auto_inc_notes (insn, XVECEXP (x, i, j));
2114 /* Remove all REG_DEAD and REG_UNUSED notes and regenerate REG_INC.
2115 We change pseudos by hard registers without notification of DF and
2116 that can make the notes obsolete. DF-infrastructure does not deal
2117 with REG_INC notes -- so we should regenerate them here. */
2118 static void
2119 update_inc_notes (void)
2121 rtx *pnote;
2122 basic_block bb;
2123 rtx_insn *insn;
2125 FOR_EACH_BB_FN (bb, cfun)
2126 FOR_BB_INSNS (bb, insn)
2127 if (NONDEBUG_INSN_P (insn))
2129 pnote = &REG_NOTES (insn);
2130 while (*pnote != 0)
2132 if (REG_NOTE_KIND (*pnote) == REG_DEAD
2133 || REG_NOTE_KIND (*pnote) == REG_UNUSED
2134 || REG_NOTE_KIND (*pnote) == REG_INC)
2135 *pnote = XEXP (*pnote, 1);
2136 else
2137 pnote = &XEXP (*pnote, 1);
2140 if (AUTO_INC_DEC)
2141 add_auto_inc_notes (insn, PATTERN (insn));
2145 /* Set to 1 while in lra. */
2146 int lra_in_progress;
2148 /* Start of pseudo regnos before the LRA. */
2149 int lra_new_regno_start;
2151 /* Start of reload pseudo regnos before the new spill pass. */
2152 int lra_constraint_new_regno_start;
2154 /* Avoid spilling pseudos with regno more than the following value if
2155 it is possible. */
2156 int lra_bad_spill_regno_start;
2158 /* Inheritance pseudo regnos before the new spill pass. */
2159 bitmap_head lra_inheritance_pseudos;
2161 /* Split regnos before the new spill pass. */
2162 bitmap_head lra_split_regs;
2164 /* Reload pseudo regnos before the new assignmnet pass which still can
2165 be spilled after the assinment pass as memory is also accepted in
2166 insns for the reload pseudos. */
2167 bitmap_head lra_optional_reload_pseudos;
2169 /* Pseudo regnos used for subreg reloads before the new assignment
2170 pass. Such pseudos still can be spilled after the assinment
2171 pass. */
2172 bitmap_head lra_subreg_reload_pseudos;
2174 /* File used for output of LRA debug information. */
2175 FILE *lra_dump_file;
2177 /* True if we should try spill into registers of different classes
2178 instead of memory. */
2179 bool lra_reg_spill_p;
2181 /* Set up value LRA_REG_SPILL_P. */
2182 static void
2183 setup_reg_spill_flag (void)
2185 int cl, mode;
2187 if (targetm.spill_class != NULL)
2188 for (cl = 0; cl < (int) LIM_REG_CLASSES; cl++)
2189 for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
2190 if (targetm.spill_class ((enum reg_class) cl,
2191 (machine_mode) mode) != NO_REGS)
2193 lra_reg_spill_p = true;
2194 return;
2196 lra_reg_spill_p = false;
2199 /* True if the current function is too big to use regular algorithms
2200 in LRA. In other words, we should use simpler and faster algorithms
2201 in LRA. It also means we should not worry about generation code
2202 for caller saves. The value is set up in IRA. */
2203 bool lra_simple_p;
2205 /* Major LRA entry function. F is a file should be used to dump LRA
2206 debug info. */
2207 void
2208 lra (FILE *f)
2210 int i;
2211 bool live_p, scratch_p, inserted_p;
2213 lra_dump_file = f;
2215 timevar_push (TV_LRA);
2217 /* Make sure that the last insn is a note. Some subsequent passes
2218 need it. */
2219 emit_note (NOTE_INSN_DELETED);
2221 COPY_HARD_REG_SET (lra_no_alloc_regs, ira_no_alloc_regs);
2223 init_reg_info ();
2224 expand_reg_info ();
2226 init_insn_recog_data ();
2228 /* Some quick check on RTL generated by previous passes. */
2229 if (flag_checking)
2230 check_rtl (false);
2232 lra_in_progress = 1;
2234 lra_live_range_iter = lra_coalesce_iter = lra_constraint_iter = 0;
2235 lra_assignment_iter = lra_assignment_iter_after_spill = 0;
2236 lra_inheritance_iter = lra_undo_inheritance_iter = 0;
2237 lra_rematerialization_iter = 0;
2239 setup_reg_spill_flag ();
2241 /* Function remove_scratches can creates new pseudos for clobbers --
2242 so set up lra_constraint_new_regno_start before its call to
2243 permit changing reg classes for pseudos created by this
2244 simplification. */
2245 lra_constraint_new_regno_start = lra_new_regno_start = max_reg_num ();
2246 lra_bad_spill_regno_start = INT_MAX;
2247 remove_scratches ();
2248 scratch_p = lra_constraint_new_regno_start != max_reg_num ();
2250 /* A function that has a non-local label that can reach the exit
2251 block via non-exceptional paths must save all call-saved
2252 registers. */
2253 if (cfun->has_nonlocal_label && has_nonexceptional_receiver ())
2254 crtl->saves_all_registers = 1;
2256 if (crtl->saves_all_registers)
2257 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2258 if (! call_used_regs[i] && ! fixed_regs[i] && ! LOCAL_REGNO (i))
2259 df_set_regs_ever_live (i, true);
2261 /* We don't DF from now and avoid its using because it is to
2262 expensive when a lot of RTL changes are made. */
2263 df_set_flags (DF_NO_INSN_RESCAN);
2264 lra_constraint_insn_stack.create (get_max_uid ());
2265 lra_constraint_insn_stack_bitmap = sbitmap_alloc (get_max_uid ());
2266 bitmap_clear (lra_constraint_insn_stack_bitmap);
2267 lra_live_ranges_init ();
2268 lra_constraints_init ();
2269 lra_curr_reload_num = 0;
2270 push_insns (get_last_insn (), NULL);
2271 /* It is needed for the 1st coalescing. */
2272 bitmap_initialize (&lra_inheritance_pseudos, &reg_obstack);
2273 bitmap_initialize (&lra_split_regs, &reg_obstack);
2274 bitmap_initialize (&lra_optional_reload_pseudos, &reg_obstack);
2275 bitmap_initialize (&lra_subreg_reload_pseudos, &reg_obstack);
2276 live_p = false;
2277 if (get_frame_size () != 0 && crtl->stack_alignment_needed)
2278 /* If we have a stack frame, we must align it now. The stack size
2279 may be a part of the offset computation for register
2280 elimination. */
2281 assign_stack_local (BLKmode, 0, crtl->stack_alignment_needed);
2282 lra_init_equiv ();
2283 for (;;)
2285 for (;;)
2287 /* We should try to assign hard registers to scratches even
2288 if there were no RTL transformations in
2289 lra_constraints. */
2290 if (! lra_constraints (lra_constraint_iter == 0)
2291 && (lra_constraint_iter > 1
2292 || (! scratch_p && ! caller_save_needed)))
2293 break;
2294 /* Constraint transformations may result in that eliminable
2295 hard regs become uneliminable and pseudos which use them
2296 should be spilled. It is better to do it before pseudo
2297 assignments.
2299 For example, rs6000 can make
2300 RS6000_PIC_OFFSET_TABLE_REGNUM uneliminable if we started
2301 to use a constant pool. */
2302 lra_eliminate (false, false);
2303 /* Do inheritance only for regular algorithms. */
2304 if (! lra_simple_p)
2306 if (flag_ipa_ra)
2308 if (live_p)
2309 lra_clear_live_ranges ();
2310 /* As a side-effect of lra_create_live_ranges, we calculate
2311 actual_call_used_reg_set, which is needed during
2312 lra_inheritance. */
2313 lra_create_live_ranges (true, true);
2314 live_p = true;
2316 lra_inheritance ();
2318 if (live_p)
2319 lra_clear_live_ranges ();
2320 /* We need live ranges for lra_assign -- so build them. But
2321 don't remove dead insns or change global live info as we
2322 can undo inheritance transformations after inheritance
2323 pseudo assigning. */
2324 lra_create_live_ranges (true, false);
2325 live_p = true;
2326 /* If we don't spill non-reload and non-inheritance pseudos,
2327 there is no sense to run memory-memory move coalescing.
2328 If inheritance pseudos were spilled, the memory-memory
2329 moves involving them will be removed by pass undoing
2330 inheritance. */
2331 if (lra_simple_p)
2332 lra_assign ();
2333 else
2335 bool spill_p = !lra_assign ();
2337 if (lra_undo_inheritance ())
2338 live_p = false;
2339 if (spill_p)
2341 if (! live_p)
2343 lra_create_live_ranges (true, true);
2344 live_p = true;
2346 if (lra_coalesce ())
2347 live_p = false;
2349 if (! live_p)
2350 lra_clear_live_ranges ();
2353 /* Don't clear optional reloads bitmap until all constraints are
2354 satisfied as we need to differ them from regular reloads. */
2355 bitmap_clear (&lra_optional_reload_pseudos);
2356 bitmap_clear (&lra_subreg_reload_pseudos);
2357 bitmap_clear (&lra_inheritance_pseudos);
2358 bitmap_clear (&lra_split_regs);
2359 if (! live_p)
2361 /* We need full live info for spilling pseudos into
2362 registers instead of memory. */
2363 lra_create_live_ranges (lra_reg_spill_p, true);
2364 live_p = true;
2366 /* We should check necessity for spilling here as the above live
2367 range pass can remove spilled pseudos. */
2368 if (! lra_need_for_spills_p ())
2369 break;
2370 /* Now we know what pseudos should be spilled. Try to
2371 rematerialize them first. */
2372 if (lra_remat ())
2374 /* We need full live info -- see the comment above. */
2375 lra_create_live_ranges (lra_reg_spill_p, true);
2376 live_p = true;
2377 if (! lra_need_for_spills_p ())
2378 break;
2380 lra_spill ();
2381 /* Assignment of stack slots changes elimination offsets for
2382 some eliminations. So update the offsets here. */
2383 lra_eliminate (false, false);
2384 lra_constraint_new_regno_start = max_reg_num ();
2385 if (lra_bad_spill_regno_start == INT_MAX
2386 && lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES
2387 && lra_rematerialization_iter > LRA_MAX_REMATERIALIZATION_PASSES)
2388 /* After switching off inheritance and rematerialization
2389 passes, avoid spilling reload pseudos will be created to
2390 prevent LRA cycling in some complicated cases. */
2391 lra_bad_spill_regno_start = lra_constraint_new_regno_start;
2392 lra_assignment_iter_after_spill = 0;
2394 restore_scratches ();
2395 lra_eliminate (true, false);
2396 lra_final_code_change ();
2397 lra_in_progress = 0;
2398 if (live_p)
2399 lra_clear_live_ranges ();
2400 lra_live_ranges_finish ();
2401 lra_constraints_finish ();
2402 finish_reg_info ();
2403 sbitmap_free (lra_constraint_insn_stack_bitmap);
2404 lra_constraint_insn_stack.release ();
2405 finish_insn_recog_data ();
2406 regstat_free_n_sets_and_refs ();
2407 regstat_free_ri ();
2408 reload_completed = 1;
2409 update_inc_notes ();
2411 inserted_p = fixup_abnormal_edges ();
2413 /* We've possibly turned single trapping insn into multiple ones. */
2414 if (cfun->can_throw_non_call_exceptions)
2416 sbitmap blocks;
2417 blocks = sbitmap_alloc (last_basic_block_for_fn (cfun));
2418 bitmap_ones (blocks);
2419 find_many_sub_basic_blocks (blocks);
2420 sbitmap_free (blocks);
2423 if (inserted_p)
2424 commit_edge_insertions ();
2426 /* Replacing pseudos with their memory equivalents might have
2427 created shared rtx. Subsequent passes would get confused
2428 by this, so unshare everything here. */
2429 unshare_all_rtl_again (get_insns ());
2431 if (flag_checking)
2432 check_rtl (true);
2434 timevar_pop (TV_LRA);
2437 /* Called once per compiler to initialize LRA data once. */
2438 void
2439 lra_init_once (void)
2441 init_insn_code_data_once ();
2444 /* Called once per compiler to finish LRA data which are initialize
2445 once. */
2446 void
2447 lra_finish_once (void)
2449 finish_insn_code_data_once ();