Clear hardware capabilities for gcc.dg/vect/vect-simd-clone-*.c
[official-gcc.git] / gcc / lra.c
blobd052b363ce9f08d3d2fd154bf458a2543c1a712e
1 /* LRA (local register allocator) driver and LRA utilities.
2 Copyright (C) 2010-2014 Free Software Foundation, Inc.
3 Contributed by Vladimir Makarov <vmakarov@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* The Local Register Allocator (LRA) is a replacement of former
23 reload pass. It is focused to simplify code solving the reload
24 pass tasks, to make the code maintenance easier, and to implement new
25 perspective optimizations.
27 The major LRA design solutions are:
28 o division small manageable, separated sub-tasks
29 o reflection of all transformations and decisions in RTL as more
30 as possible
31 o insn constraints as a primary source of the info (minimizing
32 number of target-depended macros/hooks)
34 In brief LRA works by iterative insn process with the final goal is
35 to satisfy all insn and address constraints:
36 o New reload insns (in brief reloads) and reload pseudos might be
37 generated;
38 o Some pseudos might be spilled to assign hard registers to
39 new reload pseudos;
40 o Changing spilled pseudos to stack memory or their equivalences;
41 o Allocation stack memory changes the address displacement and
42 new iteration is needed.
44 Here is block diagram of LRA passes:
46 ------------------------
47 --------------- | Undo inheritance for | ---------------
48 | Memory-memory | | spilled pseudos, | | New (and old) |
49 | move coalesce |<---| splits for pseudos got |<-- | pseudos |
50 --------------- | the same hard regs, | | assignment |
51 Start | | and optional reloads | ---------------
52 | | ------------------------ ^
53 V | ---------------- |
54 ----------- V | Update virtual | |
55 | Remove |----> ------------>| register | |
56 | scratches | ^ | displacements | |
57 ----------- | ---------------- |
58 | | |
59 | V New |
60 ---------------- No ------------ pseudos -------------------
61 | Spilled pseudo | change |Constraints:| or insns | Inheritance/split |
62 | to memory |<-------| RTL |--------->| transformations |
63 | substitution | | transfor- | | in EBB scope |
64 ---------------- | mations | -------------------
65 | ------------
67 -------------------------
68 | Hard regs substitution, |
69 | devirtalization, and |------> Finish
70 | restoring scratches got |
71 | memory |
72 -------------------------
74 To speed up the process:
75 o We process only insns affected by changes on previous
76 iterations;
77 o We don't use DFA-infrastructure because it results in much slower
78 compiler speed than a special IR described below does;
79 o We use a special insn representation for quick access to insn
80 info which is always *synchronized* with the current RTL;
81 o Insn IR is minimized by memory. It is divided on three parts:
82 o one specific for each insn in RTL (only operand locations);
83 o one common for all insns in RTL with the same insn code
84 (different operand attributes from machine descriptions);
85 o one oriented for maintenance of live info (list of pseudos).
86 o Pseudo data:
87 o all insns where the pseudo is referenced;
88 o live info (conflicting hard regs, live ranges, # of
89 references etc);
90 o data used for assigning (preferred hard regs, costs etc).
92 This file contains LRA driver, LRA utility functions and data, and
93 code for dealing with scratches. */
95 #include "config.h"
96 #include "system.h"
97 #include "coretypes.h"
98 #include "tm.h"
99 #include "hard-reg-set.h"
100 #include "rtl.h"
101 #include "tm_p.h"
102 #include "regs.h"
103 #include "insn-config.h"
104 #include "insn-codes.h"
105 #include "recog.h"
106 #include "output.h"
107 #include "addresses.h"
108 #include "flags.h"
109 #include "function.h"
110 #include "expr.h"
111 #include "basic-block.h"
112 #include "except.h"
113 #include "tree-pass.h"
114 #include "timevar.h"
115 #include "target.h"
116 #include "vec.h"
117 #include "ira.h"
118 #include "lra-int.h"
119 #include "df.h"
121 /* Hard registers currently not available for allocation. It can
122 changed after some hard registers become not eliminable. */
123 HARD_REG_SET lra_no_alloc_regs;
125 static int get_new_reg_value (void);
126 static void expand_reg_info (void);
127 static void invalidate_insn_recog_data (int);
128 static int get_insn_freq (rtx);
129 static void invalidate_insn_data_regno_info (lra_insn_recog_data_t, rtx, int);
131 /* Expand all regno related info needed for LRA. */
132 static void
133 expand_reg_data (int old)
135 resize_reg_info ();
136 expand_reg_info ();
137 ira_expand_reg_equiv ();
138 for (int i = (int) max_reg_num () - 1; i >= old; i--)
139 lra_change_class (i, ALL_REGS, " Set", true);
142 /* Create and return a new reg of ORIGINAL mode. If ORIGINAL is NULL
143 or of VOIDmode, use MD_MODE for the new reg. Initialize its
144 register class to RCLASS. Print message about assigning class
145 RCLASS containing new register name TITLE unless it is NULL. Use
146 attributes of ORIGINAL if it is a register. The created register
147 will have unique held value. */
149 lra_create_new_reg_with_unique_value (enum machine_mode md_mode, rtx original,
150 enum reg_class rclass, const char *title)
152 enum machine_mode mode;
153 rtx new_reg;
155 if (original == NULL_RTX || (mode = GET_MODE (original)) == VOIDmode)
156 mode = md_mode;
157 lra_assert (mode != VOIDmode);
158 new_reg = gen_reg_rtx (mode);
159 if (original == NULL_RTX || ! REG_P (original))
161 if (lra_dump_file != NULL)
162 fprintf (lra_dump_file, " Creating newreg=%i", REGNO (new_reg));
164 else
166 if (ORIGINAL_REGNO (original) >= FIRST_PSEUDO_REGISTER)
167 ORIGINAL_REGNO (new_reg) = ORIGINAL_REGNO (original);
168 REG_USERVAR_P (new_reg) = REG_USERVAR_P (original);
169 REG_POINTER (new_reg) = REG_POINTER (original);
170 REG_ATTRS (new_reg) = REG_ATTRS (original);
171 if (lra_dump_file != NULL)
172 fprintf (lra_dump_file, " Creating newreg=%i from oldreg=%i",
173 REGNO (new_reg), REGNO (original));
175 if (lra_dump_file != NULL)
177 if (title != NULL)
178 fprintf (lra_dump_file, ", assigning class %s to%s%s r%d",
179 reg_class_names[rclass], *title == '\0' ? "" : " ",
180 title, REGNO (new_reg));
181 fprintf (lra_dump_file, "\n");
183 expand_reg_data (max_reg_num ());
184 setup_reg_classes (REGNO (new_reg), rclass, NO_REGS, rclass);
185 return new_reg;
188 /* Analogous to the previous function but also inherits value of
189 ORIGINAL. */
191 lra_create_new_reg (enum machine_mode md_mode, rtx original,
192 enum reg_class rclass, const char *title)
194 rtx new_reg;
196 new_reg
197 = lra_create_new_reg_with_unique_value (md_mode, original, rclass, title);
198 if (original != NULL_RTX && REG_P (original))
199 lra_assign_reg_val (REGNO (original), REGNO (new_reg));
200 return new_reg;
203 /* Set up for REGNO unique hold value. */
204 void
205 lra_set_regno_unique_value (int regno)
207 lra_reg_info[regno].val = get_new_reg_value ();
210 /* Invalidate INSN related info used by LRA. The info should never be
211 used after that. */
212 void
213 lra_invalidate_insn_data (rtx insn)
215 lra_invalidate_insn_regno_info (insn);
216 invalidate_insn_recog_data (INSN_UID (insn));
219 /* Mark INSN deleted and invalidate the insn related info used by
220 LRA. */
221 void
222 lra_set_insn_deleted (rtx insn)
224 lra_invalidate_insn_data (insn);
225 SET_INSN_DELETED (insn);
228 /* Delete an unneeded INSN and any previous insns who sole purpose is
229 loading data that is dead in INSN. */
230 void
231 lra_delete_dead_insn (rtx insn)
233 rtx prev = prev_real_insn (insn);
234 rtx prev_dest;
236 /* If the previous insn sets a register that dies in our insn,
237 delete it too. */
238 if (prev && GET_CODE (PATTERN (prev)) == SET
239 && (prev_dest = SET_DEST (PATTERN (prev)), REG_P (prev_dest))
240 && reg_mentioned_p (prev_dest, PATTERN (insn))
241 && find_regno_note (insn, REG_DEAD, REGNO (prev_dest))
242 && ! side_effects_p (SET_SRC (PATTERN (prev))))
243 lra_delete_dead_insn (prev);
245 lra_set_insn_deleted (insn);
248 /* Emit insn x = y + z. Return NULL if we failed to do it.
249 Otherwise, return the insn. We don't use gen_add3_insn as it might
250 clobber CC. */
251 static rtx
252 emit_add3_insn (rtx x, rtx y, rtx z)
254 rtx insn, last;
256 last = get_last_insn ();
257 insn = emit_insn (gen_rtx_SET (VOIDmode, x,
258 gen_rtx_PLUS (GET_MODE (y), y, z)));
259 if (recog_memoized (insn) < 0)
261 delete_insns_since (last);
262 insn = NULL_RTX;
264 return insn;
267 /* Emit insn x = x + y. Return the insn. We use gen_add2_insn as the
268 last resort. */
269 static rtx
270 emit_add2_insn (rtx x, rtx y)
272 rtx insn;
274 insn = emit_add3_insn (x, x, y);
275 if (insn == NULL_RTX)
277 insn = gen_add2_insn (x, y);
278 if (insn != NULL_RTX)
279 emit_insn (insn);
281 return insn;
284 /* Target checks operands through operand predicates to recognize an
285 insn. We should have a special precaution to generate add insns
286 which are frequent results of elimination.
288 Emit insns for x = y + z. X can be used to store intermediate
289 values and should be not in Y and Z when we use X to store an
290 intermediate value. Y + Z should form [base] [+ index[ * scale]] [
291 + disp] where base and index are registers, disp and scale are
292 constants. Y should contain base if it is present, Z should
293 contain disp if any. index[*scale] can be part of Y or Z. */
294 void
295 lra_emit_add (rtx x, rtx y, rtx z)
297 int old;
298 rtx insn, last;
299 rtx a1, a2, base, index, disp, scale, index_scale;
300 bool ok_p;
302 insn = emit_add3_insn (x, y, z);
303 old = max_reg_num ();
304 if (insn != NULL_RTX)
306 else
308 disp = a2 = NULL_RTX;
309 if (GET_CODE (y) == PLUS)
311 a1 = XEXP (y, 0);
312 a2 = XEXP (y, 1);
313 disp = z;
315 else
317 a1 = y;
318 if (CONSTANT_P (z))
319 disp = z;
320 else
321 a2 = z;
323 index_scale = scale = NULL_RTX;
324 if (GET_CODE (a1) == MULT)
326 index_scale = a1;
327 index = XEXP (a1, 0);
328 scale = XEXP (a1, 1);
329 base = a2;
331 else if (a2 != NULL_RTX && GET_CODE (a2) == MULT)
333 index_scale = a2;
334 index = XEXP (a2, 0);
335 scale = XEXP (a2, 1);
336 base = a1;
338 else
340 base = a1;
341 index = a2;
343 if (! REG_P (base)
344 || (index != NULL_RTX && ! REG_P (index))
345 || (disp != NULL_RTX && ! CONSTANT_P (disp))
346 || (scale != NULL_RTX && ! CONSTANT_P (scale)))
348 /* Probably we have no 3 op add. Last chance is to use 2-op
349 add insn. To succeed, don't move Z to X as an address
350 segment always comes in Y. Otherwise, we might fail when
351 adding the address segment to register. */
352 lra_assert (x != y && x != z);
353 emit_move_insn (x, y);
354 insn = emit_add2_insn (x, z);
355 lra_assert (insn != NULL_RTX);
357 else
359 if (index_scale == NULL_RTX)
360 index_scale = index;
361 if (disp == NULL_RTX)
363 /* Generate x = index_scale; x = x + base. */
364 lra_assert (index_scale != NULL_RTX && base != NULL_RTX);
365 emit_move_insn (x, index_scale);
366 insn = emit_add2_insn (x, base);
367 lra_assert (insn != NULL_RTX);
369 else if (scale == NULL_RTX)
371 /* Try x = base + disp. */
372 lra_assert (base != NULL_RTX);
373 last = get_last_insn ();
374 insn = emit_move_insn (x, gen_rtx_PLUS (GET_MODE (base),
375 base, disp));
376 if (recog_memoized (insn) < 0)
378 delete_insns_since (last);
379 /* Generate x = disp; x = x + base. */
380 emit_move_insn (x, disp);
381 insn = emit_add2_insn (x, base);
382 lra_assert (insn != NULL_RTX);
384 /* Generate x = x + index. */
385 if (index != NULL_RTX)
387 insn = emit_add2_insn (x, index);
388 lra_assert (insn != NULL_RTX);
391 else
393 /* Try x = index_scale; x = x + disp; x = x + base. */
394 last = get_last_insn ();
395 insn = emit_move_insn (x, index_scale);
396 ok_p = false;
397 if (recog_memoized (insn) >= 0)
399 insn = emit_add2_insn (x, disp);
400 if (insn != NULL_RTX)
402 insn = emit_add2_insn (x, disp);
403 if (insn != NULL_RTX)
404 ok_p = true;
407 if (! ok_p)
409 delete_insns_since (last);
410 /* Generate x = disp; x = x + base; x = x + index_scale. */
411 emit_move_insn (x, disp);
412 insn = emit_add2_insn (x, base);
413 lra_assert (insn != NULL_RTX);
414 insn = emit_add2_insn (x, index_scale);
415 lra_assert (insn != NULL_RTX);
420 /* Functions emit_... can create pseudos -- so expand the pseudo
421 data. */
422 if (old != max_reg_num ())
423 expand_reg_data (old);
426 /* The number of emitted reload insns so far. */
427 int lra_curr_reload_num;
429 /* Emit x := y, processing special case when y = u + v or y = u + v *
430 scale + w through emit_add (Y can be an address which is base +
431 index reg * scale + displacement in general case). X may be used
432 as intermediate result therefore it should be not in Y. */
433 void
434 lra_emit_move (rtx x, rtx y)
436 int old;
438 if (GET_CODE (y) != PLUS)
440 if (rtx_equal_p (x, y))
441 return;
442 old = max_reg_num ();
443 emit_move_insn (x, y);
444 if (REG_P (x))
445 lra_reg_info[ORIGINAL_REGNO (x)].last_reload = ++lra_curr_reload_num;
446 /* Function emit_move can create pseudos -- so expand the pseudo
447 data. */
448 if (old != max_reg_num ())
449 expand_reg_data (old);
450 return;
452 lra_emit_add (x, XEXP (y, 0), XEXP (y, 1));
455 /* Update insn operands which are duplication of operands whose
456 numbers are in array of NOPS (with end marker -1). The insn is
457 represented by its LRA internal representation ID. */
458 void
459 lra_update_dups (lra_insn_recog_data_t id, signed char *nops)
461 int i, j, nop;
462 struct lra_static_insn_data *static_id = id->insn_static_data;
464 for (i = 0; i < static_id->n_dups; i++)
465 for (j = 0; (nop = nops[j]) >= 0; j++)
466 if (static_id->dup_num[i] == nop)
467 *id->dup_loc[i] = *id->operand_loc[nop];
472 /* This page contains code dealing with info about registers in the
473 insns. */
475 /* Pools for insn reg info. */
476 static alloc_pool insn_reg_pool;
478 /* Initiate pool for insn reg info. */
479 static void
480 init_insn_regs (void)
482 insn_reg_pool
483 = create_alloc_pool ("insn regs", sizeof (struct lra_insn_reg), 100);
486 /* Create LRA insn related info about a reference to REGNO in INSN with
487 TYPE (in/out/inout), biggest reference mode MODE, flag that it is
488 reference through subreg (SUBREG_P), flag that is early clobbered
489 in the insn (EARLY_CLOBBER), and reference to the next insn reg
490 info (NEXT). */
491 static struct lra_insn_reg *
492 new_insn_reg (rtx insn, int regno, enum op_type type, enum machine_mode mode,
493 bool subreg_p, bool early_clobber, struct lra_insn_reg *next)
495 struct lra_insn_reg *ir;
497 ir = (struct lra_insn_reg *) pool_alloc (insn_reg_pool);
498 ir->type = type;
499 ir->biggest_mode = mode;
500 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (lra_reg_info[regno].biggest_mode)
501 && NONDEBUG_INSN_P (insn))
502 lra_reg_info[regno].biggest_mode = mode;
503 ir->subreg_p = subreg_p;
504 ir->early_clobber = early_clobber;
505 ir->regno = regno;
506 ir->next = next;
507 return ir;
510 /* Free insn reg info IR. */
511 static void
512 free_insn_reg (struct lra_insn_reg *ir)
514 pool_free (insn_reg_pool, ir);
517 /* Free insn reg info list IR. */
518 static void
519 free_insn_regs (struct lra_insn_reg *ir)
521 struct lra_insn_reg *next_ir;
523 for (; ir != NULL; ir = next_ir)
525 next_ir = ir->next;
526 free_insn_reg (ir);
530 /* Finish pool for insn reg info. */
531 static void
532 finish_insn_regs (void)
534 free_alloc_pool (insn_reg_pool);
539 /* This page contains code dealing LRA insn info (or in other words
540 LRA internal insn representation). */
542 struct target_lra_int default_target_lra_int;
543 #if SWITCHABLE_TARGET
544 struct target_lra_int *this_target_lra_int = &default_target_lra_int;
545 #endif
547 /* Map INSN_CODE -> the static insn data. This info is valid during
548 all translation unit. */
549 struct lra_static_insn_data *insn_code_data[LAST_INSN_CODE];
551 /* Debug insns are represented as a special insn with one input
552 operand which is RTL expression in var_location. */
554 /* The following data are used as static insn operand data for all
555 debug insns. If structure lra_operand_data is changed, the
556 initializer should be changed too. */
557 static struct lra_operand_data debug_operand_data =
559 NULL, /* alternative */
560 VOIDmode, /* We are not interesting in the operand mode. */
561 OP_IN,
562 0, 0, 0, 0
565 /* The following data are used as static insn data for all debug
566 insns. If structure lra_static_insn_data is changed, the
567 initializer should be changed too. */
568 static struct lra_static_insn_data debug_insn_static_data =
570 &debug_operand_data,
571 0, /* Duplication operands #. */
572 -1, /* Commutative operand #. */
573 1, /* Operands #. There is only one operand which is debug RTL
574 expression. */
575 0, /* Duplications #. */
576 0, /* Alternatives #. We are not interesting in alternatives
577 because we does not proceed debug_insns for reloads. */
578 NULL, /* Hard registers referenced in machine description. */
579 NULL /* Descriptions of operands in alternatives. */
582 /* Called once per compiler work to initialize some LRA data related
583 to insns. */
584 static void
585 init_insn_code_data_once (void)
587 memset (insn_code_data, 0, sizeof (insn_code_data));
588 memset (op_alt_data, 0, sizeof (op_alt_data));
591 /* Called once per compiler work to finalize some LRA data related to
592 insns. */
593 static void
594 finish_insn_code_data_once (void)
596 int i;
598 for (i = 0; i < LAST_INSN_CODE; i++)
600 if (insn_code_data[i] != NULL)
601 free (insn_code_data[i]);
602 if (op_alt_data[i] != NULL)
603 free (op_alt_data[i]);
607 /* Initialize LRA info about operands in insn alternatives. */
608 static void
609 init_op_alt_data (void)
611 int i;
613 for (i = 0; i < LAST_INSN_CODE; i++)
614 if (op_alt_data[i] != NULL)
616 free (op_alt_data[i]);
617 op_alt_data[i] = NULL;
621 /* Return static insn data, allocate and setup if necessary. Although
622 dup_num is static data (it depends only on icode), to set it up we
623 need to extract insn first. So recog_data should be valid for
624 normal insn (ICODE >= 0) before the call. */
625 static struct lra_static_insn_data *
626 get_static_insn_data (int icode, int nop, int ndup, int nalt)
628 struct lra_static_insn_data *data;
629 size_t n_bytes;
631 lra_assert (icode < LAST_INSN_CODE);
632 if (icode >= 0 && (data = insn_code_data[icode]) != NULL)
633 return data;
634 lra_assert (nop >= 0 && ndup >= 0 && nalt >= 0);
635 n_bytes = sizeof (struct lra_static_insn_data)
636 + sizeof (struct lra_operand_data) * nop
637 + sizeof (int) * ndup;
638 data = XNEWVAR (struct lra_static_insn_data, n_bytes);
639 data->n_operands = nop;
640 data->n_dups = ndup;
641 data->n_alternatives = nalt;
642 data->operand = ((struct lra_operand_data *)
643 ((char *) data + sizeof (struct lra_static_insn_data)));
644 data->dup_num = ((int *) ((char *) data->operand
645 + sizeof (struct lra_operand_data) * nop));
646 if (icode >= 0)
648 int i;
650 insn_code_data[icode] = data;
651 for (i = 0; i < nop; i++)
653 data->operand[i].constraint
654 = insn_data[icode].operand[i].constraint;
655 data->operand[i].mode = insn_data[icode].operand[i].mode;
656 data->operand[i].strict_low = insn_data[icode].operand[i].strict_low;
657 data->operand[i].is_operator
658 = insn_data[icode].operand[i].is_operator;
659 data->operand[i].type
660 = (data->operand[i].constraint[0] == '=' ? OP_OUT
661 : data->operand[i].constraint[0] == '+' ? OP_INOUT
662 : OP_IN);
663 data->operand[i].is_address = false;
665 for (i = 0; i < ndup; i++)
666 data->dup_num[i] = recog_data.dup_num[i];
668 return data;
671 /* The current length of the following array. */
672 int lra_insn_recog_data_len;
674 /* Map INSN_UID -> the insn recog data (NULL if unknown). */
675 lra_insn_recog_data_t *lra_insn_recog_data;
677 /* Initialize LRA data about insns. */
678 static void
679 init_insn_recog_data (void)
681 lra_insn_recog_data_len = 0;
682 lra_insn_recog_data = NULL;
683 init_insn_regs ();
686 /* Expand, if necessary, LRA data about insns. */
687 static void
688 check_and_expand_insn_recog_data (int index)
690 int i, old;
692 if (lra_insn_recog_data_len > index)
693 return;
694 old = lra_insn_recog_data_len;
695 lra_insn_recog_data_len = index * 3 / 2 + 1;
696 lra_insn_recog_data = XRESIZEVEC (lra_insn_recog_data_t,
697 lra_insn_recog_data,
698 lra_insn_recog_data_len);
699 for (i = old; i < lra_insn_recog_data_len; i++)
700 lra_insn_recog_data[i] = NULL;
703 /* Finish LRA DATA about insn. */
704 static void
705 free_insn_recog_data (lra_insn_recog_data_t data)
707 if (data->operand_loc != NULL)
708 free (data->operand_loc);
709 if (data->dup_loc != NULL)
710 free (data->dup_loc);
711 if (data->arg_hard_regs != NULL)
712 free (data->arg_hard_regs);
713 if (HAVE_ATTR_enabled && data->alternative_enabled_p != NULL)
714 free (data->alternative_enabled_p);
715 if (data->icode < 0 && NONDEBUG_INSN_P (data->insn))
717 if (data->insn_static_data->operand_alternative != NULL)
718 free (data->insn_static_data->operand_alternative);
719 free_insn_regs (data->insn_static_data->hard_regs);
720 free (data->insn_static_data);
722 free_insn_regs (data->regs);
723 data->regs = NULL;
724 free (data);
727 /* Finish LRA data about all insns. */
728 static void
729 finish_insn_recog_data (void)
731 int i;
732 lra_insn_recog_data_t data;
734 for (i = 0; i < lra_insn_recog_data_len; i++)
735 if ((data = lra_insn_recog_data[i]) != NULL)
736 free_insn_recog_data (data);
737 finish_insn_regs ();
738 free (lra_insn_recog_data);
741 /* Setup info about operands in alternatives of LRA DATA of insn. */
742 static void
743 setup_operand_alternative (lra_insn_recog_data_t data)
745 int i, nop, nalt;
746 int icode = data->icode;
747 struct lra_static_insn_data *static_data = data->insn_static_data;
749 if (icode >= 0
750 && (static_data->operand_alternative = op_alt_data[icode]) != NULL)
751 return;
752 static_data->commutative = -1;
753 nop = static_data->n_operands;
754 if (nop == 0)
756 static_data->operand_alternative = NULL;
757 return;
759 nalt = static_data->n_alternatives;
760 static_data->operand_alternative = XNEWVEC (struct operand_alternative,
761 nalt * nop);
762 memset (static_data->operand_alternative, 0,
763 nalt * nop * sizeof (struct operand_alternative));
764 if (icode >= 0)
765 op_alt_data[icode] = static_data->operand_alternative;
766 for (i = 0; i < nop; i++)
768 int j;
769 struct operand_alternative *op_alt_start, *op_alt;
770 const char *p = static_data->operand[i].constraint;
772 static_data->operand[i].early_clobber = 0;
773 op_alt_start = &static_data->operand_alternative[i];
775 for (j = 0; j < nalt; j++)
777 op_alt = op_alt_start + j * nop;
778 op_alt->cl = NO_REGS;
779 op_alt->constraint = p;
780 op_alt->matches = -1;
781 op_alt->matched = -1;
783 if (*p == '\0' || *p == ',')
785 op_alt->anything_ok = 1;
786 continue;
789 for (;;)
791 char c = *p;
792 if (c == '#')
794 c = *++p;
795 while (c != ',' && c != '\0');
796 if (c == ',' || c == '\0')
798 p++;
799 break;
802 switch (c)
804 case '=': case '+': case '*':
805 case 'E': case 'F': case 'G': case 'H':
806 case 's': case 'i': case 'n':
807 case 'I': case 'J': case 'K': case 'L':
808 case 'M': case 'N': case 'O': case 'P':
809 /* These don't say anything we care about. */
810 break;
812 case '%':
813 /* We currently only support one commutative pair of
814 operands. */
815 if (static_data->commutative < 0)
816 static_data->commutative = i;
817 else
818 lra_assert (data->icode < 0); /* Asm */
820 /* The last operand should not be marked
821 commutative. */
822 lra_assert (i != nop - 1);
823 break;
825 case '?':
826 op_alt->reject += LRA_LOSER_COST_FACTOR;
827 break;
828 case '!':
829 op_alt->reject += LRA_MAX_REJECT;
830 break;
831 case '&':
832 op_alt->earlyclobber = 1;
833 static_data->operand[i].early_clobber = 1;
834 break;
836 case '0': case '1': case '2': case '3': case '4':
837 case '5': case '6': case '7': case '8': case '9':
839 char *end;
840 op_alt->matches = strtoul (p, &end, 10);
841 static_data->operand_alternative
842 [j * nop + op_alt->matches].matched = i;
843 p = end;
845 continue;
847 case TARGET_MEM_CONSTRAINT:
848 op_alt->memory_ok = 1;
849 break;
850 case '<':
851 op_alt->decmem_ok = 1;
852 break;
853 case '>':
854 op_alt->incmem_ok = 1;
855 break;
856 case 'V':
857 op_alt->nonoffmem_ok = 1;
858 break;
859 case 'o':
860 op_alt->offmem_ok = 1;
861 break;
862 case 'X':
863 op_alt->anything_ok = 1;
864 break;
866 case 'p':
867 static_data->operand[i].is_address = true;
868 op_alt->is_address = 1;
869 op_alt->cl = (reg_class_subunion[(int) op_alt->cl]
870 [(int) base_reg_class (VOIDmode,
871 ADDR_SPACE_GENERIC,
872 ADDRESS, SCRATCH)]);
873 break;
875 case 'g':
876 case 'r':
877 op_alt->cl =
878 reg_class_subunion[(int) op_alt->cl][(int) GENERAL_REGS];
879 break;
881 default:
882 if (EXTRA_MEMORY_CONSTRAINT (c, p))
884 op_alt->memory_ok = 1;
885 break;
887 if (EXTRA_ADDRESS_CONSTRAINT (c, p))
889 static_data->operand[i].is_address = true;
890 op_alt->is_address = 1;
891 op_alt->cl
892 = (reg_class_subunion
893 [(int) op_alt->cl]
894 [(int) base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
895 ADDRESS, SCRATCH)]);
896 break;
899 op_alt->cl
900 = (reg_class_subunion
901 [(int) op_alt->cl]
902 [(int)
903 REG_CLASS_FROM_CONSTRAINT ((unsigned char) c, p)]);
904 break;
906 p += CONSTRAINT_LEN (c, p);
912 /* Recursively process X and collect info about registers, which are
913 not the insn operands, in X with TYPE (in/out/inout) and flag that
914 it is early clobbered in the insn (EARLY_CLOBBER) and add the info
915 to LIST. X is a part of insn given by DATA. Return the result
916 list. */
917 static struct lra_insn_reg *
918 collect_non_operand_hard_regs (rtx *x, lra_insn_recog_data_t data,
919 struct lra_insn_reg *list,
920 enum op_type type, bool early_clobber)
922 int i, j, regno, last;
923 bool subreg_p;
924 enum machine_mode mode;
925 struct lra_insn_reg *curr;
926 rtx op = *x;
927 enum rtx_code code = GET_CODE (op);
928 const char *fmt = GET_RTX_FORMAT (code);
930 for (i = 0; i < data->insn_static_data->n_operands; i++)
931 if (x == data->operand_loc[i])
932 /* It is an operand loc. Stop here. */
933 return list;
934 for (i = 0; i < data->insn_static_data->n_dups; i++)
935 if (x == data->dup_loc[i])
936 /* It is a dup loc. Stop here. */
937 return list;
938 mode = GET_MODE (op);
939 subreg_p = false;
940 if (code == SUBREG)
942 op = SUBREG_REG (op);
943 code = GET_CODE (op);
944 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (op)))
946 mode = GET_MODE (op);
947 if (GET_MODE_SIZE (mode) > REGMODE_NATURAL_SIZE (mode))
948 subreg_p = true;
951 if (REG_P (op))
953 if ((regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER)
954 return list;
955 for (last = regno + hard_regno_nregs[regno][mode];
956 regno < last;
957 regno++)
958 if (! TEST_HARD_REG_BIT (lra_no_alloc_regs, regno)
959 || TEST_HARD_REG_BIT (eliminable_regset, regno))
961 for (curr = list; curr != NULL; curr = curr->next)
962 if (curr->regno == regno && curr->subreg_p == subreg_p
963 && curr->biggest_mode == mode)
965 if (curr->type != type)
966 curr->type = OP_INOUT;
967 if (curr->early_clobber != early_clobber)
968 curr->early_clobber = true;
969 break;
971 if (curr == NULL)
973 /* This is a new hard regno or the info can not be
974 integrated into the found structure. */
975 #ifdef STACK_REGS
976 early_clobber
977 = (early_clobber
978 /* This clobber is to inform popping floating
979 point stack only. */
980 && ! (FIRST_STACK_REG <= regno
981 && regno <= LAST_STACK_REG));
982 #endif
983 list = new_insn_reg (data->insn, regno, type, mode, subreg_p,
984 early_clobber, list);
987 return list;
989 switch (code)
991 case SET:
992 list = collect_non_operand_hard_regs (&SET_DEST (op), data,
993 list, OP_OUT, false);
994 list = collect_non_operand_hard_regs (&SET_SRC (op), data,
995 list, OP_IN, false);
996 break;
997 case CLOBBER:
998 /* We treat clobber of non-operand hard registers as early
999 clobber (the behavior is expected from asm). */
1000 list = collect_non_operand_hard_regs (&XEXP (op, 0), data,
1001 list, OP_OUT, true);
1002 break;
1003 case PRE_INC: case PRE_DEC: case POST_INC: case POST_DEC:
1004 list = collect_non_operand_hard_regs (&XEXP (op, 0), data,
1005 list, OP_INOUT, false);
1006 break;
1007 case PRE_MODIFY: case POST_MODIFY:
1008 list = collect_non_operand_hard_regs (&XEXP (op, 0), data,
1009 list, OP_INOUT, false);
1010 list = collect_non_operand_hard_regs (&XEXP (op, 1), data,
1011 list, OP_IN, false);
1012 break;
1013 default:
1014 fmt = GET_RTX_FORMAT (code);
1015 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1017 if (fmt[i] == 'e')
1018 list = collect_non_operand_hard_regs (&XEXP (op, i), data,
1019 list, OP_IN, false);
1020 else if (fmt[i] == 'E')
1021 for (j = XVECLEN (op, i) - 1; j >= 0; j--)
1022 list = collect_non_operand_hard_regs (&XVECEXP (op, i, j), data,
1023 list, OP_IN, false);
1026 return list;
1029 /* Set up and return info about INSN. Set up the info if it is not set up
1030 yet. */
1031 lra_insn_recog_data_t
1032 lra_set_insn_recog_data (rtx insn)
1034 lra_insn_recog_data_t data;
1035 int i, n, icode;
1036 rtx **locs;
1037 unsigned int uid = INSN_UID (insn);
1038 struct lra_static_insn_data *insn_static_data;
1040 check_and_expand_insn_recog_data (uid);
1041 if (DEBUG_INSN_P (insn))
1042 icode = -1;
1043 else
1045 icode = INSN_CODE (insn);
1046 if (icode < 0)
1047 /* It might be a new simple insn which is not recognized yet. */
1048 INSN_CODE (insn) = icode = recog_memoized (insn);
1050 data = XNEW (struct lra_insn_recog_data);
1051 lra_insn_recog_data[uid] = data;
1052 data->insn = insn;
1053 data->used_insn_alternative = -1;
1054 data->icode = icode;
1055 data->regs = NULL;
1056 if (DEBUG_INSN_P (insn))
1058 data->insn_static_data = &debug_insn_static_data;
1059 data->dup_loc = NULL;
1060 data->arg_hard_regs = NULL;
1061 data->alternative_enabled_p = NULL;
1062 data->operand_loc = XNEWVEC (rtx *, 1);
1063 data->operand_loc[0] = &INSN_VAR_LOCATION_LOC (insn);
1064 return data;
1066 if (icode < 0)
1068 int nop;
1069 enum machine_mode operand_mode[MAX_RECOG_OPERANDS];
1070 const char *constraints[MAX_RECOG_OPERANDS];
1072 nop = asm_noperands (PATTERN (insn));
1073 data->operand_loc = data->dup_loc = NULL;
1074 if (nop < 0)
1076 /* Its is a special insn like USE or CLOBBER. We should
1077 recognize any regular insn otherwise LRA can do nothing
1078 with this insn. */
1079 gcc_assert (GET_CODE (PATTERN (insn)) == USE
1080 || GET_CODE (PATTERN (insn)) == CLOBBER
1081 || GET_CODE (PATTERN (insn)) == ASM_INPUT);
1082 data->insn_static_data = insn_static_data
1083 = get_static_insn_data (-1, 0, 0, 1);
1085 else
1087 /* expand_asm_operands makes sure there aren't too many
1088 operands. */
1089 lra_assert (nop <= MAX_RECOG_OPERANDS);
1090 if (nop != 0)
1091 data->operand_loc = XNEWVEC (rtx *, nop);
1092 /* Now get the operand values and constraints out of the
1093 insn. */
1094 decode_asm_operands (PATTERN (insn), NULL,
1095 data->operand_loc,
1096 constraints, operand_mode, NULL);
1097 n = 1;
1098 if (nop > 0)
1100 const char *p = recog_data.constraints[0];
1102 for (p = constraints[0]; *p; p++)
1103 n += *p == ',';
1105 data->insn_static_data = insn_static_data
1106 = get_static_insn_data (-1, nop, 0, n);
1107 for (i = 0; i < nop; i++)
1109 insn_static_data->operand[i].mode = operand_mode[i];
1110 insn_static_data->operand[i].constraint = constraints[i];
1111 insn_static_data->operand[i].strict_low = false;
1112 insn_static_data->operand[i].is_operator = false;
1113 insn_static_data->operand[i].is_address = false;
1116 for (i = 0; i < insn_static_data->n_operands; i++)
1117 insn_static_data->operand[i].type
1118 = (insn_static_data->operand[i].constraint[0] == '=' ? OP_OUT
1119 : insn_static_data->operand[i].constraint[0] == '+' ? OP_INOUT
1120 : OP_IN);
1121 data->alternative_enabled_p = NULL;
1123 else
1125 insn_extract (insn);
1126 data->insn_static_data = insn_static_data
1127 = get_static_insn_data (icode, insn_data[icode].n_operands,
1128 insn_data[icode].n_dups,
1129 insn_data[icode].n_alternatives);
1130 n = insn_static_data->n_operands;
1131 if (n == 0)
1132 locs = NULL;
1133 else
1135 locs = XNEWVEC (rtx *, n);
1136 memcpy (locs, recog_data.operand_loc, n * sizeof (rtx *));
1138 data->operand_loc = locs;
1139 n = insn_static_data->n_dups;
1140 if (n == 0)
1141 locs = NULL;
1142 else
1144 locs = XNEWVEC (rtx *, n);
1145 memcpy (locs, recog_data.dup_loc, n * sizeof (rtx *));
1147 data->dup_loc = locs;
1148 if (HAVE_ATTR_enabled)
1150 bool *bp;
1152 n = insn_static_data->n_alternatives;
1153 lra_assert (n >= 0);
1154 data->alternative_enabled_p = bp = XNEWVEC (bool, n);
1155 /* Cache the insn because we don't want to call extract_insn
1156 from get_attr_enabled as extract_insn modifies
1157 which_alternative. The attribute enabled should not depend
1158 on insn operands, operand modes, operand types, and operand
1159 constraints. It should depend on the architecture. If it
1160 is not true, we should rewrite this file code to use
1161 extract_insn instead of less expensive insn_extract. */
1162 recog_data.insn = insn;
1163 for (i = 0; i < n; i++)
1165 which_alternative = i;
1166 bp[i] = get_attr_enabled (insn);
1170 if (GET_CODE (PATTERN (insn)) == CLOBBER || GET_CODE (PATTERN (insn)) == USE)
1171 insn_static_data->hard_regs = NULL;
1172 else
1173 insn_static_data->hard_regs
1174 = collect_non_operand_hard_regs (&PATTERN (insn), data,
1175 NULL, OP_IN, false);
1176 setup_operand_alternative (data);
1177 data->arg_hard_regs = NULL;
1178 if (CALL_P (insn))
1180 rtx link;
1181 int n_hard_regs, regno, arg_hard_regs[FIRST_PSEUDO_REGISTER];
1183 n_hard_regs = 0;
1184 /* Finding implicit hard register usage. We believe it will be
1185 not changed whatever transformations are used. Call insns
1186 are such example. */
1187 for (link = CALL_INSN_FUNCTION_USAGE (insn);
1188 link != NULL_RTX;
1189 link = XEXP (link, 1))
1190 if (GET_CODE (XEXP (link, 0)) == USE
1191 && REG_P (XEXP (XEXP (link, 0), 0)))
1193 regno = REGNO (XEXP (XEXP (link, 0), 0));
1194 lra_assert (regno < FIRST_PSEUDO_REGISTER);
1195 /* It is an argument register. */
1196 for (i = (hard_regno_nregs
1197 [regno][GET_MODE (XEXP (XEXP (link, 0), 0))]) - 1;
1198 i >= 0;
1199 i--)
1200 arg_hard_regs[n_hard_regs++] = regno + i;
1202 if (n_hard_regs != 0)
1204 arg_hard_regs[n_hard_regs++] = -1;
1205 data->arg_hard_regs = XNEWVEC (int, n_hard_regs);
1206 memcpy (data->arg_hard_regs, arg_hard_regs,
1207 sizeof (int) * n_hard_regs);
1210 /* Some output operand can be recognized only from the context not
1211 from the constraints which are empty in this case. Call insn may
1212 contain a hard register in set destination with empty constraint
1213 and extract_insn treats them as an input. */
1214 for (i = 0; i < insn_static_data->n_operands; i++)
1216 int j;
1217 rtx pat, set;
1218 struct lra_operand_data *operand = &insn_static_data->operand[i];
1220 /* ??? Should we treat 'X' the same way. It looks to me that
1221 'X' means anything and empty constraint means we do not
1222 care. */
1223 if (operand->type != OP_IN || *operand->constraint != '\0'
1224 || operand->is_operator)
1225 continue;
1226 pat = PATTERN (insn);
1227 if (GET_CODE (pat) == SET)
1229 if (data->operand_loc[i] != &SET_DEST (pat))
1230 continue;
1232 else if (GET_CODE (pat) == PARALLEL)
1234 for (j = XVECLEN (pat, 0) - 1; j >= 0; j--)
1236 set = XVECEXP (PATTERN (insn), 0, j);
1237 if (GET_CODE (set) == SET
1238 && &SET_DEST (set) == data->operand_loc[i])
1239 break;
1241 if (j < 0)
1242 continue;
1244 else
1245 continue;
1246 operand->type = OP_OUT;
1248 return data;
1251 /* Return info about insn give by UID. The info should be already set
1252 up. */
1253 static lra_insn_recog_data_t
1254 get_insn_recog_data_by_uid (int uid)
1256 lra_insn_recog_data_t data;
1258 data = lra_insn_recog_data[uid];
1259 lra_assert (data != NULL);
1260 return data;
1263 /* Invalidate all info about insn given by its UID. */
1264 static void
1265 invalidate_insn_recog_data (int uid)
1267 lra_insn_recog_data_t data;
1269 data = lra_insn_recog_data[uid];
1270 lra_assert (data != NULL);
1271 free_insn_recog_data (data);
1272 lra_insn_recog_data[uid] = NULL;
1275 /* Update all the insn info about INSN. It is usually called when
1276 something in the insn was changed. Return the updated info. */
1277 lra_insn_recog_data_t
1278 lra_update_insn_recog_data (rtx insn)
1280 lra_insn_recog_data_t data;
1281 int n;
1282 unsigned int uid = INSN_UID (insn);
1283 struct lra_static_insn_data *insn_static_data;
1284 HOST_WIDE_INT sp_offset = 0;
1286 check_and_expand_insn_recog_data (uid);
1287 if ((data = lra_insn_recog_data[uid]) != NULL
1288 && data->icode != INSN_CODE (insn))
1290 sp_offset = data->sp_offset;
1291 invalidate_insn_data_regno_info (data, insn, get_insn_freq (insn));
1292 invalidate_insn_recog_data (uid);
1293 data = NULL;
1295 if (data == NULL)
1297 data = lra_get_insn_recog_data (insn);
1298 /* Initiate or restore SP offset. */
1299 data->sp_offset = sp_offset;
1300 return data;
1302 insn_static_data = data->insn_static_data;
1303 data->used_insn_alternative = -1;
1304 if (DEBUG_INSN_P (insn))
1305 return data;
1306 if (data->icode < 0)
1308 int nop;
1309 enum machine_mode operand_mode[MAX_RECOG_OPERANDS];
1310 const char *constraints[MAX_RECOG_OPERANDS];
1312 nop = asm_noperands (PATTERN (insn));
1313 if (nop >= 0)
1315 lra_assert (nop == data->insn_static_data->n_operands);
1316 /* Now get the operand values and constraints out of the
1317 insn. */
1318 decode_asm_operands (PATTERN (insn), NULL,
1319 data->operand_loc,
1320 constraints, operand_mode, NULL);
1321 #ifdef ENABLE_CHECKING
1323 int i;
1325 for (i = 0; i < nop; i++)
1326 lra_assert
1327 (insn_static_data->operand[i].mode == operand_mode[i]
1328 && insn_static_data->operand[i].constraint == constraints[i]
1329 && ! insn_static_data->operand[i].is_operator);
1331 #endif
1333 #ifdef ENABLE_CHECKING
1335 int i;
1337 for (i = 0; i < insn_static_data->n_operands; i++)
1338 lra_assert
1339 (insn_static_data->operand[i].type
1340 == (insn_static_data->operand[i].constraint[0] == '=' ? OP_OUT
1341 : insn_static_data->operand[i].constraint[0] == '+' ? OP_INOUT
1342 : OP_IN));
1344 #endif
1346 else
1348 insn_extract (insn);
1349 n = insn_static_data->n_operands;
1350 if (n != 0)
1351 memcpy (data->operand_loc, recog_data.operand_loc, n * sizeof (rtx *));
1352 n = insn_static_data->n_dups;
1353 if (n != 0)
1354 memcpy (data->dup_loc, recog_data.dup_loc, n * sizeof (rtx *));
1355 #if HAVE_ATTR_enabled
1356 #ifdef ENABLE_CHECKING
1358 int i;
1359 bool *bp;
1361 n = insn_static_data->n_alternatives;
1362 bp = data->alternative_enabled_p;
1363 lra_assert (n >= 0 && bp != NULL);
1364 /* Cache the insn to prevent extract_insn call from
1365 get_attr_enabled. */
1366 recog_data.insn = insn;
1367 for (i = 0; i < n; i++)
1369 which_alternative = i;
1370 lra_assert (bp[i] == get_attr_enabled (insn));
1373 #endif
1374 #endif
1376 return data;
1379 /* Set up that INSN is using alternative ALT now. */
1380 void
1381 lra_set_used_insn_alternative (rtx insn, int alt)
1383 lra_insn_recog_data_t data;
1385 data = lra_get_insn_recog_data (insn);
1386 data->used_insn_alternative = alt;
1389 /* Set up that insn with UID is using alternative ALT now. The insn
1390 info should be already set up. */
1391 void
1392 lra_set_used_insn_alternative_by_uid (int uid, int alt)
1394 lra_insn_recog_data_t data;
1396 check_and_expand_insn_recog_data (uid);
1397 data = lra_insn_recog_data[uid];
1398 lra_assert (data != NULL);
1399 data->used_insn_alternative = alt;
1404 /* This page contains code dealing with common register info and
1405 pseudo copies. */
1407 /* The size of the following array. */
1408 static int reg_info_size;
1409 /* Common info about each register. */
1410 struct lra_reg *lra_reg_info;
1412 /* Last register value. */
1413 static int last_reg_value;
1415 /* Return new register value. */
1416 static int
1417 get_new_reg_value (void)
1419 return ++last_reg_value;
1422 /* Pools for copies. */
1423 static alloc_pool copy_pool;
1425 /* Vec referring to pseudo copies. */
1426 static vec<lra_copy_t> copy_vec;
1428 /* Initialize I-th element of lra_reg_info. */
1429 static inline void
1430 initialize_lra_reg_info_element (int i)
1432 bitmap_initialize (&lra_reg_info[i].insn_bitmap, &reg_obstack);
1433 #ifdef STACK_REGS
1434 lra_reg_info[i].no_stack_p = false;
1435 #endif
1436 CLEAR_HARD_REG_SET (lra_reg_info[i].conflict_hard_regs);
1437 lra_reg_info[i].preferred_hard_regno1 = -1;
1438 lra_reg_info[i].preferred_hard_regno2 = -1;
1439 lra_reg_info[i].preferred_hard_regno_profit1 = 0;
1440 lra_reg_info[i].preferred_hard_regno_profit2 = 0;
1441 lra_reg_info[i].biggest_mode = VOIDmode;
1442 lra_reg_info[i].live_ranges = NULL;
1443 lra_reg_info[i].nrefs = lra_reg_info[i].freq = 0;
1444 lra_reg_info[i].last_reload = 0;
1445 lra_reg_info[i].restore_regno = -1;
1446 lra_reg_info[i].val = get_new_reg_value ();
1447 lra_reg_info[i].offset = 0;
1448 lra_reg_info[i].copies = NULL;
1451 /* Initialize common reg info and copies. */
1452 static void
1453 init_reg_info (void)
1455 int i;
1457 last_reg_value = 0;
1458 reg_info_size = max_reg_num () * 3 / 2 + 1;
1459 lra_reg_info = XNEWVEC (struct lra_reg, reg_info_size);
1460 for (i = 0; i < reg_info_size; i++)
1461 initialize_lra_reg_info_element (i);
1462 copy_pool
1463 = create_alloc_pool ("lra copies", sizeof (struct lra_copy), 100);
1464 copy_vec.create (100);
1468 /* Finish common reg info and copies. */
1469 static void
1470 finish_reg_info (void)
1472 int i;
1474 for (i = 0; i < reg_info_size; i++)
1475 bitmap_clear (&lra_reg_info[i].insn_bitmap);
1476 free (lra_reg_info);
1477 reg_info_size = 0;
1478 free_alloc_pool (copy_pool);
1479 copy_vec.release ();
1482 /* Expand common reg info if it is necessary. */
1483 static void
1484 expand_reg_info (void)
1486 int i, old = reg_info_size;
1488 if (reg_info_size > max_reg_num ())
1489 return;
1490 reg_info_size = max_reg_num () * 3 / 2 + 1;
1491 lra_reg_info = XRESIZEVEC (struct lra_reg, lra_reg_info, reg_info_size);
1492 for (i = old; i < reg_info_size; i++)
1493 initialize_lra_reg_info_element (i);
1496 /* Free all copies. */
1497 void
1498 lra_free_copies (void)
1500 lra_copy_t cp;
1502 while (copy_vec.length () != 0)
1504 cp = copy_vec.pop ();
1505 lra_reg_info[cp->regno1].copies = lra_reg_info[cp->regno2].copies = NULL;
1506 pool_free (copy_pool, cp);
1510 /* Create copy of two pseudos REGNO1 and REGNO2. The copy execution
1511 frequency is FREQ. */
1512 void
1513 lra_create_copy (int regno1, int regno2, int freq)
1515 bool regno1_dest_p;
1516 lra_copy_t cp;
1518 lra_assert (regno1 != regno2);
1519 regno1_dest_p = true;
1520 if (regno1 > regno2)
1522 int temp = regno2;
1524 regno1_dest_p = false;
1525 regno2 = regno1;
1526 regno1 = temp;
1528 cp = (lra_copy_t) pool_alloc (copy_pool);
1529 copy_vec.safe_push (cp);
1530 cp->regno1_dest_p = regno1_dest_p;
1531 cp->freq = freq;
1532 cp->regno1 = regno1;
1533 cp->regno2 = regno2;
1534 cp->regno1_next = lra_reg_info[regno1].copies;
1535 lra_reg_info[regno1].copies = cp;
1536 cp->regno2_next = lra_reg_info[regno2].copies;
1537 lra_reg_info[regno2].copies = cp;
1538 if (lra_dump_file != NULL)
1539 fprintf (lra_dump_file, " Creating copy r%d%sr%d@%d\n",
1540 regno1, regno1_dest_p ? "<-" : "->", regno2, freq);
1543 /* Return N-th (0, 1, ...) copy. If there is no copy, return
1544 NULL. */
1545 lra_copy_t
1546 lra_get_copy (int n)
1548 if (n >= (int) copy_vec.length ())
1549 return NULL;
1550 return copy_vec[n];
1555 /* This page contains code dealing with info about registers in
1556 insns. */
1558 /* Process X of insn UID recursively and add info (operand type is
1559 given by TYPE, flag of that it is early clobber is EARLY_CLOBBER)
1560 about registers in X to the insn DATA. */
1561 static void
1562 add_regs_to_insn_regno_info (lra_insn_recog_data_t data, rtx x, int uid,
1563 enum op_type type, bool early_clobber)
1565 int i, j, regno;
1566 bool subreg_p;
1567 enum machine_mode mode;
1568 const char *fmt;
1569 enum rtx_code code;
1570 struct lra_insn_reg *curr;
1572 code = GET_CODE (x);
1573 mode = GET_MODE (x);
1574 subreg_p = false;
1575 if (GET_CODE (x) == SUBREG)
1577 x = SUBREG_REG (x);
1578 code = GET_CODE (x);
1579 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (x)))
1581 mode = GET_MODE (x);
1582 if (GET_MODE_SIZE (mode) > REGMODE_NATURAL_SIZE (mode))
1583 subreg_p = true;
1586 if (REG_P (x))
1588 regno = REGNO (x);
1589 if (regno < FIRST_PSEUDO_REGISTER
1590 && TEST_HARD_REG_BIT (lra_no_alloc_regs, regno)
1591 && ! TEST_HARD_REG_BIT (eliminable_regset, regno))
1592 return;
1593 expand_reg_info ();
1594 if (bitmap_set_bit (&lra_reg_info[regno].insn_bitmap, uid))
1596 data->regs = new_insn_reg (data->insn, regno, type, mode, subreg_p,
1597 early_clobber, data->regs);
1598 return;
1600 else
1602 for (curr = data->regs; curr != NULL; curr = curr->next)
1603 if (curr->regno == regno)
1605 if (curr->subreg_p != subreg_p || curr->biggest_mode != mode)
1606 /* The info can not be integrated into the found
1607 structure. */
1608 data->regs = new_insn_reg (data->insn, regno, type, mode,
1609 subreg_p, early_clobber,
1610 data->regs);
1611 else
1613 if (curr->type != type)
1614 curr->type = OP_INOUT;
1615 if (curr->early_clobber != early_clobber)
1616 curr->early_clobber = true;
1618 return;
1620 gcc_unreachable ();
1624 switch (code)
1626 case SET:
1627 add_regs_to_insn_regno_info (data, SET_DEST (x), uid, OP_OUT, false);
1628 add_regs_to_insn_regno_info (data, SET_SRC (x), uid, OP_IN, false);
1629 break;
1630 case CLOBBER:
1631 /* We treat clobber of non-operand hard registers as early
1632 clobber (the behavior is expected from asm). */
1633 add_regs_to_insn_regno_info (data, XEXP (x, 0), uid, OP_OUT, true);
1634 break;
1635 case PRE_INC: case PRE_DEC: case POST_INC: case POST_DEC:
1636 add_regs_to_insn_regno_info (data, XEXP (x, 0), uid, OP_INOUT, false);
1637 break;
1638 case PRE_MODIFY: case POST_MODIFY:
1639 add_regs_to_insn_regno_info (data, XEXP (x, 0), uid, OP_INOUT, false);
1640 add_regs_to_insn_regno_info (data, XEXP (x, 1), uid, OP_IN, false);
1641 break;
1642 default:
1643 if ((code != PARALLEL && code != EXPR_LIST) || type != OP_OUT)
1644 /* Some targets place small structures in registers for return
1645 values of functions, and those registers are wrapped in
1646 PARALLEL that we may see as the destination of a SET. Here
1647 is an example:
1649 (call_insn 13 12 14 2 (set (parallel:BLK [
1650 (expr_list:REG_DEP_TRUE (reg:DI 0 ax)
1651 (const_int 0 [0]))
1652 (expr_list:REG_DEP_TRUE (reg:DI 1 dx)
1653 (const_int 8 [0x8]))
1655 (call (mem:QI (symbol_ref:DI (... */
1656 type = OP_IN;
1657 fmt = GET_RTX_FORMAT (code);
1658 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1660 if (fmt[i] == 'e')
1661 add_regs_to_insn_regno_info (data, XEXP (x, i), uid, type, false);
1662 else if (fmt[i] == 'E')
1664 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1665 add_regs_to_insn_regno_info (data, XVECEXP (x, i, j), uid,
1666 type, false);
1672 /* Return execution frequency of INSN. */
1673 static int
1674 get_insn_freq (rtx insn)
1676 basic_block bb = BLOCK_FOR_INSN (insn);
1678 gcc_checking_assert (bb != NULL);
1679 return REG_FREQ_FROM_BB (bb);
1682 /* Invalidate all reg info of INSN with DATA and execution frequency
1683 FREQ. Update common info about the invalidated registers. */
1684 static void
1685 invalidate_insn_data_regno_info (lra_insn_recog_data_t data, rtx insn,
1686 int freq)
1688 int uid;
1689 bool debug_p;
1690 unsigned int i;
1691 struct lra_insn_reg *ir, *next_ir;
1693 uid = INSN_UID (insn);
1694 debug_p = DEBUG_INSN_P (insn);
1695 for (ir = data->regs; ir != NULL; ir = next_ir)
1697 i = ir->regno;
1698 next_ir = ir->next;
1699 free_insn_reg (ir);
1700 bitmap_clear_bit (&lra_reg_info[i].insn_bitmap, uid);
1701 if (i >= FIRST_PSEUDO_REGISTER && ! debug_p)
1703 lra_reg_info[i].nrefs--;
1704 lra_reg_info[i].freq -= freq;
1705 lra_assert (lra_reg_info[i].nrefs >= 0 && lra_reg_info[i].freq >= 0);
1708 data->regs = NULL;
1711 /* Invalidate all reg info of INSN. Update common info about the
1712 invalidated registers. */
1713 void
1714 lra_invalidate_insn_regno_info (rtx insn)
1716 invalidate_insn_data_regno_info (lra_get_insn_recog_data (insn), insn,
1717 get_insn_freq (insn));
1720 /* Update common reg info from reg info of insn given by its DATA and
1721 execution frequency FREQ. */
1722 static void
1723 setup_insn_reg_info (lra_insn_recog_data_t data, int freq)
1725 unsigned int i;
1726 struct lra_insn_reg *ir;
1728 for (ir = data->regs; ir != NULL; ir = ir->next)
1729 if ((i = ir->regno) >= FIRST_PSEUDO_REGISTER)
1731 lra_reg_info[i].nrefs++;
1732 lra_reg_info[i].freq += freq;
1736 /* Set up insn reg info of INSN. Update common reg info from reg info
1737 of INSN. */
1738 void
1739 lra_update_insn_regno_info (rtx insn)
1741 int i, uid, freq;
1742 lra_insn_recog_data_t data;
1743 struct lra_static_insn_data *static_data;
1744 enum rtx_code code;
1746 if (! INSN_P (insn))
1747 return;
1748 data = lra_get_insn_recog_data (insn);
1749 static_data = data->insn_static_data;
1750 freq = get_insn_freq (insn);
1751 invalidate_insn_data_regno_info (data, insn, freq);
1752 uid = INSN_UID (insn);
1753 for (i = static_data->n_operands - 1; i >= 0; i--)
1754 add_regs_to_insn_regno_info (data, *data->operand_loc[i], uid,
1755 static_data->operand[i].type,
1756 static_data->operand[i].early_clobber);
1757 if ((code = GET_CODE (PATTERN (insn))) == CLOBBER || code == USE)
1758 add_regs_to_insn_regno_info (data, XEXP (PATTERN (insn), 0), uid,
1759 code == USE ? OP_IN : OP_OUT, false);
1760 if (NONDEBUG_INSN_P (insn))
1761 setup_insn_reg_info (data, freq);
1764 /* Return reg info of insn given by it UID. */
1765 struct lra_insn_reg *
1766 lra_get_insn_regs (int uid)
1768 lra_insn_recog_data_t data;
1770 data = get_insn_recog_data_by_uid (uid);
1771 return data->regs;
1776 /* This page contains code dealing with stack of the insns which
1777 should be processed by the next constraint pass. */
1779 /* Bitmap used to put an insn on the stack only in one exemplar. */
1780 static sbitmap lra_constraint_insn_stack_bitmap;
1782 /* The stack itself. */
1783 vec<rtx> lra_constraint_insn_stack;
1785 /* Put INSN on the stack. If ALWAYS_UPDATE is true, always update the reg
1786 info for INSN, otherwise only update it if INSN is not already on the
1787 stack. */
1788 static inline void
1789 lra_push_insn_1 (rtx insn, bool always_update)
1791 unsigned int uid = INSN_UID (insn);
1792 if (always_update)
1793 lra_update_insn_regno_info (insn);
1794 if (uid >= SBITMAP_SIZE (lra_constraint_insn_stack_bitmap))
1795 lra_constraint_insn_stack_bitmap =
1796 sbitmap_resize (lra_constraint_insn_stack_bitmap, 3 * uid / 2, 0);
1797 if (bitmap_bit_p (lra_constraint_insn_stack_bitmap, uid))
1798 return;
1799 bitmap_set_bit (lra_constraint_insn_stack_bitmap, uid);
1800 if (! always_update)
1801 lra_update_insn_regno_info (insn);
1802 lra_constraint_insn_stack.safe_push (insn);
1805 /* Put INSN on the stack. */
1806 void
1807 lra_push_insn (rtx insn)
1809 lra_push_insn_1 (insn, false);
1812 /* Put INSN on the stack and update its reg info. */
1813 void
1814 lra_push_insn_and_update_insn_regno_info (rtx insn)
1816 lra_push_insn_1 (insn, true);
1819 /* Put insn with UID on the stack. */
1820 void
1821 lra_push_insn_by_uid (unsigned int uid)
1823 lra_push_insn (lra_insn_recog_data[uid]->insn);
1826 /* Take the last-inserted insns off the stack and return it. */
1828 lra_pop_insn (void)
1830 rtx insn = lra_constraint_insn_stack.pop ();
1831 bitmap_clear_bit (lra_constraint_insn_stack_bitmap, INSN_UID (insn));
1832 return insn;
1835 /* Return the current size of the insn stack. */
1836 unsigned int
1837 lra_insn_stack_length (void)
1839 return lra_constraint_insn_stack.length ();
1842 /* Push insns FROM to TO (excluding it) going in reverse order. */
1843 static void
1844 push_insns (rtx from, rtx to)
1846 rtx insn;
1848 if (from == NULL_RTX)
1849 return;
1850 for (insn = from; insn != to; insn = PREV_INSN (insn))
1851 if (INSN_P (insn))
1852 lra_push_insn (insn);
1855 /* Set up sp offset for insn in range [FROM, LAST]. The offset is
1856 taken from the next BB insn after LAST or zero if there in such
1857 insn. */
1858 static void
1859 setup_sp_offset (rtx from, rtx last)
1861 rtx before = next_nonnote_insn_bb (last);
1862 HOST_WIDE_INT offset = (before == NULL_RTX || ! INSN_P (before)
1863 ? 0 : lra_get_insn_recog_data (before)->sp_offset);
1865 for (rtx insn = from; insn != NEXT_INSN (last); insn = NEXT_INSN (insn))
1866 lra_get_insn_recog_data (insn)->sp_offset = offset;
1869 /* Emit insns BEFORE before INSN and insns AFTER after INSN. Put the
1870 insns onto the stack. Print about emitting the insns with
1871 TITLE. */
1872 void
1873 lra_process_new_insns (rtx insn, rtx before, rtx after, const char *title)
1875 rtx last;
1877 if (before == NULL_RTX && after == NULL_RTX)
1878 return;
1879 if (lra_dump_file != NULL)
1881 dump_insn_slim (lra_dump_file, insn);
1882 if (before != NULL_RTX)
1884 fprintf (lra_dump_file," %s before:\n", title);
1885 dump_rtl_slim (lra_dump_file, before, NULL_RTX, -1, 0);
1887 if (after != NULL_RTX)
1889 fprintf (lra_dump_file, " %s after:\n", title);
1890 dump_rtl_slim (lra_dump_file, after, NULL_RTX, -1, 0);
1892 fprintf (lra_dump_file, "\n");
1894 if (before != NULL_RTX)
1896 emit_insn_before (before, insn);
1897 push_insns (PREV_INSN (insn), PREV_INSN (before));
1898 setup_sp_offset (before, PREV_INSN (insn));
1900 if (after != NULL_RTX)
1902 for (last = after; NEXT_INSN (last) != NULL_RTX; last = NEXT_INSN (last))
1904 emit_insn_after (after, insn);
1905 push_insns (last, insn);
1906 setup_sp_offset (after, last);
1912 /* This page contains code dealing with scratches (changing them onto
1913 pseudos and restoring them from the pseudos).
1915 We change scratches into pseudos at the beginning of LRA to
1916 simplify dealing with them (conflicts, hard register assignments).
1918 If the pseudo denoting scratch was spilled it means that we do need
1919 a hard register for it. Such pseudos are transformed back to
1920 scratches at the end of LRA. */
1922 /* Description of location of a former scratch operand. */
1923 struct sloc
1925 rtx insn; /* Insn where the scratch was. */
1926 int nop; /* Number of the operand which was a scratch. */
1929 typedef struct sloc *sloc_t;
1931 /* Locations of the former scratches. */
1932 static vec<sloc_t> scratches;
1934 /* Bitmap of scratch regnos. */
1935 static bitmap_head scratch_bitmap;
1937 /* Bitmap of scratch operands. */
1938 static bitmap_head scratch_operand_bitmap;
1940 /* Return true if pseudo REGNO is made of SCRATCH. */
1941 bool
1942 lra_former_scratch_p (int regno)
1944 return bitmap_bit_p (&scratch_bitmap, regno);
1947 /* Return true if the operand NOP of INSN is a former scratch. */
1948 bool
1949 lra_former_scratch_operand_p (rtx insn, int nop)
1951 return bitmap_bit_p (&scratch_operand_bitmap,
1952 INSN_UID (insn) * MAX_RECOG_OPERANDS + nop) != 0;
1955 /* Change scratches onto pseudos and save their location. */
1956 static void
1957 remove_scratches (void)
1959 int i;
1960 bool insn_changed_p;
1961 basic_block bb;
1962 rtx insn, reg;
1963 sloc_t loc;
1964 lra_insn_recog_data_t id;
1965 struct lra_static_insn_data *static_id;
1967 scratches.create (get_max_uid ());
1968 bitmap_initialize (&scratch_bitmap, &reg_obstack);
1969 bitmap_initialize (&scratch_operand_bitmap, &reg_obstack);
1970 FOR_EACH_BB_FN (bb, cfun)
1971 FOR_BB_INSNS (bb, insn)
1972 if (INSN_P (insn))
1974 id = lra_get_insn_recog_data (insn);
1975 static_id = id->insn_static_data;
1976 insn_changed_p = false;
1977 for (i = 0; i < static_id->n_operands; i++)
1978 if (GET_CODE (*id->operand_loc[i]) == SCRATCH
1979 && GET_MODE (*id->operand_loc[i]) != VOIDmode)
1981 insn_changed_p = true;
1982 *id->operand_loc[i] = reg
1983 = lra_create_new_reg (static_id->operand[i].mode,
1984 *id->operand_loc[i], ALL_REGS, NULL);
1985 add_reg_note (insn, REG_UNUSED, reg);
1986 lra_update_dup (id, i);
1987 loc = XNEW (struct sloc);
1988 loc->insn = insn;
1989 loc->nop = i;
1990 scratches.safe_push (loc);
1991 bitmap_set_bit (&scratch_bitmap, REGNO (*id->operand_loc[i]));
1992 bitmap_set_bit (&scratch_operand_bitmap,
1993 INSN_UID (insn) * MAX_RECOG_OPERANDS + i);
1994 if (lra_dump_file != NULL)
1995 fprintf (lra_dump_file,
1996 "Removing SCRATCH in insn #%u (nop %d)\n",
1997 INSN_UID (insn), i);
1999 if (insn_changed_p)
2000 /* Because we might use DF right after caller-saves sub-pass
2001 we need to keep DF info up to date. */
2002 df_insn_rescan (insn);
2006 /* Changes pseudos created by function remove_scratches onto scratches. */
2007 static void
2008 restore_scratches (void)
2010 int regno;
2011 unsigned i;
2012 sloc_t loc;
2013 rtx last = NULL_RTX;
2014 lra_insn_recog_data_t id = NULL;
2016 for (i = 0; scratches.iterate (i, &loc); i++)
2018 if (last != loc->insn)
2020 last = loc->insn;
2021 id = lra_get_insn_recog_data (last);
2023 if (REG_P (*id->operand_loc[loc->nop])
2024 && ((regno = REGNO (*id->operand_loc[loc->nop]))
2025 >= FIRST_PSEUDO_REGISTER)
2026 && lra_get_regno_hard_regno (regno) < 0)
2028 /* It should be only case when scratch register with chosen
2029 constraint 'X' did not get memory or hard register. */
2030 lra_assert (lra_former_scratch_p (regno));
2031 *id->operand_loc[loc->nop]
2032 = gen_rtx_SCRATCH (GET_MODE (*id->operand_loc[loc->nop]));
2033 lra_update_dup (id, loc->nop);
2034 if (lra_dump_file != NULL)
2035 fprintf (lra_dump_file, "Restoring SCRATCH in insn #%u(nop %d)\n",
2036 INSN_UID (loc->insn), loc->nop);
2039 for (i = 0; scratches.iterate (i, &loc); i++)
2040 free (loc);
2041 scratches.release ();
2042 bitmap_clear (&scratch_bitmap);
2043 bitmap_clear (&scratch_operand_bitmap);
2048 #ifdef ENABLE_CHECKING
2050 /* Function checks RTL for correctness. If FINAL_P is true, it is
2051 done at the end of LRA and the check is more rigorous. */
2052 static void
2053 check_rtl (bool final_p)
2055 basic_block bb;
2056 rtx insn;
2058 lra_assert (! final_p || reload_completed);
2059 FOR_EACH_BB_FN (bb, cfun)
2060 FOR_BB_INSNS (bb, insn)
2061 if (NONDEBUG_INSN_P (insn)
2062 && GET_CODE (PATTERN (insn)) != USE
2063 && GET_CODE (PATTERN (insn)) != CLOBBER
2064 && GET_CODE (PATTERN (insn)) != ASM_INPUT)
2066 if (final_p)
2068 extract_insn (insn);
2069 lra_assert (constrain_operands (1));
2070 continue;
2072 /* LRA code is based on assumption that all addresses can be
2073 correctly decomposed. LRA can generate reloads for
2074 decomposable addresses. The decomposition code checks the
2075 correctness of the addresses. So we don't need to check
2076 the addresses here. Don't call insn_invalid_p here, it can
2077 change the code at this stage. */
2078 if (recog_memoized (insn) < 0 && asm_noperands (PATTERN (insn)) < 0)
2079 fatal_insn_not_found (insn);
2082 #endif /* #ifdef ENABLE_CHECKING */
2084 /* Determine if the current function has an exception receiver block
2085 that reaches the exit block via non-exceptional edges */
2086 static bool
2087 has_nonexceptional_receiver (void)
2089 edge e;
2090 edge_iterator ei;
2091 basic_block *tos, *worklist, bb;
2093 /* If we're not optimizing, then just err on the safe side. */
2094 if (!optimize)
2095 return true;
2097 /* First determine which blocks can reach exit via normal paths. */
2098 tos = worklist = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun) + 1);
2100 FOR_EACH_BB_FN (bb, cfun)
2101 bb->flags &= ~BB_REACHABLE;
2103 /* Place the exit block on our worklist. */
2104 EXIT_BLOCK_PTR_FOR_FN (cfun)->flags |= BB_REACHABLE;
2105 *tos++ = EXIT_BLOCK_PTR_FOR_FN (cfun);
2107 /* Iterate: find everything reachable from what we've already seen. */
2108 while (tos != worklist)
2110 bb = *--tos;
2112 FOR_EACH_EDGE (e, ei, bb->preds)
2113 if (e->flags & EDGE_ABNORMAL)
2115 free (worklist);
2116 return true;
2118 else
2120 basic_block src = e->src;
2122 if (!(src->flags & BB_REACHABLE))
2124 src->flags |= BB_REACHABLE;
2125 *tos++ = src;
2129 free (worklist);
2130 /* No exceptional block reached exit unexceptionally. */
2131 return false;
2134 #ifdef AUTO_INC_DEC
2136 /* Process recursively X of INSN and add REG_INC notes if necessary. */
2137 static void
2138 add_auto_inc_notes (rtx insn, rtx x)
2140 enum rtx_code code = GET_CODE (x);
2141 const char *fmt;
2142 int i, j;
2144 if (code == MEM && auto_inc_p (XEXP (x, 0)))
2146 add_reg_note (insn, REG_INC, XEXP (XEXP (x, 0), 0));
2147 return;
2150 /* Scan all X sub-expressions. */
2151 fmt = GET_RTX_FORMAT (code);
2152 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2154 if (fmt[i] == 'e')
2155 add_auto_inc_notes (insn, XEXP (x, i));
2156 else if (fmt[i] == 'E')
2157 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2158 add_auto_inc_notes (insn, XVECEXP (x, i, j));
2162 #endif
2164 /* Remove all REG_DEAD and REG_UNUSED notes and regenerate REG_INC.
2165 We change pseudos by hard registers without notification of DF and
2166 that can make the notes obsolete. DF-infrastructure does not deal
2167 with REG_INC notes -- so we should regenerate them here. */
2168 static void
2169 update_inc_notes (void)
2171 rtx *pnote;
2172 basic_block bb;
2173 rtx insn;
2175 FOR_EACH_BB_FN (bb, cfun)
2176 FOR_BB_INSNS (bb, insn)
2177 if (NONDEBUG_INSN_P (insn))
2179 pnote = &REG_NOTES (insn);
2180 while (*pnote != 0)
2182 if (REG_NOTE_KIND (*pnote) == REG_DEAD
2183 || REG_NOTE_KIND (*pnote) == REG_UNUSED
2184 || REG_NOTE_KIND (*pnote) == REG_INC)
2185 *pnote = XEXP (*pnote, 1);
2186 else
2187 pnote = &XEXP (*pnote, 1);
2189 #ifdef AUTO_INC_DEC
2190 add_auto_inc_notes (insn, PATTERN (insn));
2191 #endif
2195 /* Set to 1 while in lra. */
2196 int lra_in_progress;
2198 /* Start of pseudo regnos before the LRA. */
2199 int lra_new_regno_start;
2201 /* Start of reload pseudo regnos before the new spill pass. */
2202 int lra_constraint_new_regno_start;
2204 /* Inheritance pseudo regnos before the new spill pass. */
2205 bitmap_head lra_inheritance_pseudos;
2207 /* Split regnos before the new spill pass. */
2208 bitmap_head lra_split_regs;
2210 /* Reload pseudo regnos before the new assignmnet pass which still can
2211 be spilled after the assinment pass as memory is also accepted in
2212 insns for the reload pseudos. */
2213 bitmap_head lra_optional_reload_pseudos;
2215 /* Pseudo regnos used for subreg reloads before the new assignment
2216 pass. Such pseudos still can be spilled after the assinment
2217 pass. */
2218 bitmap_head lra_subreg_reload_pseudos;
2220 /* First UID of insns generated before a new spill pass. */
2221 int lra_constraint_new_insn_uid_start;
2223 /* File used for output of LRA debug information. */
2224 FILE *lra_dump_file;
2226 /* True if we should try spill into registers of different classes
2227 instead of memory. */
2228 bool lra_reg_spill_p;
2230 /* Set up value LRA_REG_SPILL_P. */
2231 static void
2232 setup_reg_spill_flag (void)
2234 int cl, mode;
2236 if (targetm.spill_class != NULL)
2237 for (cl = 0; cl < (int) LIM_REG_CLASSES; cl++)
2238 for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
2239 if (targetm.spill_class ((enum reg_class) cl,
2240 (enum machine_mode) mode) != NO_REGS)
2242 lra_reg_spill_p = true;
2243 return;
2245 lra_reg_spill_p = false;
2248 /* True if the current function is too big to use regular algorithms
2249 in LRA. In other words, we should use simpler and faster algorithms
2250 in LRA. It also means we should not worry about generation code
2251 for caller saves. The value is set up in IRA. */
2252 bool lra_simple_p;
2254 /* Major LRA entry function. F is a file should be used to dump LRA
2255 debug info. */
2256 void
2257 lra (FILE *f)
2259 int i;
2260 bool live_p, scratch_p, inserted_p;
2262 lra_dump_file = f;
2264 timevar_push (TV_LRA);
2266 /* Make sure that the last insn is a note. Some subsequent passes
2267 need it. */
2268 emit_note (NOTE_INSN_DELETED);
2270 COPY_HARD_REG_SET (lra_no_alloc_regs, ira_no_alloc_regs);
2272 init_reg_info ();
2273 expand_reg_info ();
2275 init_insn_recog_data ();
2277 #ifdef ENABLE_CHECKING
2278 /* Some quick check on RTL generated by previous passes. */
2279 check_rtl (false);
2280 #endif
2282 lra_in_progress = 1;
2284 lra_live_range_iter = lra_coalesce_iter = 0;
2285 lra_constraint_iter = lra_constraint_iter_after_spill = 0;
2286 lra_inheritance_iter = lra_undo_inheritance_iter = 0;
2288 setup_reg_spill_flag ();
2290 /* Function remove_scratches can creates new pseudos for clobbers --
2291 so set up lra_constraint_new_regno_start before its call to
2292 permit changing reg classes for pseudos created by this
2293 simplification. */
2294 lra_constraint_new_regno_start = lra_new_regno_start = max_reg_num ();
2295 remove_scratches ();
2296 scratch_p = lra_constraint_new_regno_start != max_reg_num ();
2298 /* A function that has a non-local label that can reach the exit
2299 block via non-exceptional paths must save all call-saved
2300 registers. */
2301 if (cfun->has_nonlocal_label && has_nonexceptional_receiver ())
2302 crtl->saves_all_registers = 1;
2304 if (crtl->saves_all_registers)
2305 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2306 if (! call_used_regs[i] && ! fixed_regs[i] && ! LOCAL_REGNO (i))
2307 df_set_regs_ever_live (i, true);
2309 /* We don't DF from now and avoid its using because it is to
2310 expensive when a lot of RTL changes are made. */
2311 df_set_flags (DF_NO_INSN_RESCAN);
2312 lra_constraint_insn_stack.create (get_max_uid ());
2313 lra_constraint_insn_stack_bitmap = sbitmap_alloc (get_max_uid ());
2314 bitmap_clear (lra_constraint_insn_stack_bitmap);
2315 lra_live_ranges_init ();
2316 lra_constraints_init ();
2317 lra_curr_reload_num = 0;
2318 push_insns (get_last_insn (), NULL_RTX);
2319 /* It is needed for the 1st coalescing. */
2320 lra_constraint_new_insn_uid_start = get_max_uid ();
2321 bitmap_initialize (&lra_inheritance_pseudos, &reg_obstack);
2322 bitmap_initialize (&lra_split_regs, &reg_obstack);
2323 bitmap_initialize (&lra_optional_reload_pseudos, &reg_obstack);
2324 bitmap_initialize (&lra_subreg_reload_pseudos, &reg_obstack);
2325 live_p = false;
2326 if (get_frame_size () != 0 && crtl->stack_alignment_needed)
2327 /* If we have a stack frame, we must align it now. The stack size
2328 may be a part of the offset computation for register
2329 elimination. */
2330 assign_stack_local (BLKmode, 0, crtl->stack_alignment_needed);
2331 lra_init_equiv ();
2332 for (;;)
2334 for (;;)
2336 /* We should try to assign hard registers to scratches even
2337 if there were no RTL transformations in
2338 lra_constraints. */
2339 if (! lra_constraints (lra_constraint_iter == 0)
2340 && (lra_constraint_iter > 1
2341 || (! scratch_p && ! caller_save_needed)))
2342 break;
2343 /* Constraint transformations may result in that eliminable
2344 hard regs become uneliminable and pseudos which use them
2345 should be spilled. It is better to do it before pseudo
2346 assignments.
2348 For example, rs6000 can make
2349 RS6000_PIC_OFFSET_TABLE_REGNUM uneliminable if we started
2350 to use a constant pool. */
2351 lra_eliminate (false, false);
2352 /* Do inheritance only for regular algorithms. */
2353 if (! lra_simple_p)
2354 lra_inheritance ();
2355 if (live_p)
2356 lra_clear_live_ranges ();
2357 /* We need live ranges for lra_assign -- so build them. */
2358 lra_create_live_ranges (true);
2359 live_p = true;
2360 /* If we don't spill non-reload and non-inheritance pseudos,
2361 there is no sense to run memory-memory move coalescing.
2362 If inheritance pseudos were spilled, the memory-memory
2363 moves involving them will be removed by pass undoing
2364 inheritance. */
2365 if (lra_simple_p)
2366 lra_assign ();
2367 else
2369 bool spill_p = !lra_assign ();
2371 if (lra_undo_inheritance ())
2372 live_p = false;
2373 if (spill_p)
2375 if (! live_p)
2377 lra_create_live_ranges (true);
2378 live_p = true;
2380 if (lra_coalesce ())
2381 live_p = false;
2383 if (! live_p)
2384 lra_clear_live_ranges ();
2387 /* Don't clear optional reloads bitmap until all constraints are
2388 satisfied as we need to differ them from regular reloads. */
2389 bitmap_clear (&lra_optional_reload_pseudos);
2390 bitmap_clear (&lra_subreg_reload_pseudos);
2391 bitmap_clear (&lra_inheritance_pseudos);
2392 bitmap_clear (&lra_split_regs);
2393 if (! lra_need_for_spills_p ())
2394 break;
2395 if (! live_p)
2397 /* We need full live info for spilling pseudos into
2398 registers instead of memory. */
2399 lra_create_live_ranges (lra_reg_spill_p);
2400 live_p = true;
2402 lra_spill ();
2403 /* Assignment of stack slots changes elimination offsets for
2404 some eliminations. So update the offsets here. */
2405 lra_eliminate (false, false);
2406 lra_constraint_new_regno_start = max_reg_num ();
2407 lra_constraint_new_insn_uid_start = get_max_uid ();
2408 lra_constraint_iter_after_spill = 0;
2410 restore_scratches ();
2411 lra_eliminate (true, false);
2412 lra_final_code_change ();
2413 lra_in_progress = 0;
2414 if (live_p)
2415 lra_clear_live_ranges ();
2416 lra_live_ranges_finish ();
2417 lra_constraints_finish ();
2418 finish_reg_info ();
2419 sbitmap_free (lra_constraint_insn_stack_bitmap);
2420 lra_constraint_insn_stack.release ();
2421 finish_insn_recog_data ();
2422 regstat_free_n_sets_and_refs ();
2423 regstat_free_ri ();
2424 reload_completed = 1;
2425 update_inc_notes ();
2427 inserted_p = fixup_abnormal_edges ();
2429 /* We've possibly turned single trapping insn into multiple ones. */
2430 if (cfun->can_throw_non_call_exceptions)
2432 sbitmap blocks;
2433 blocks = sbitmap_alloc (last_basic_block_for_fn (cfun));
2434 bitmap_ones (blocks);
2435 find_many_sub_basic_blocks (blocks);
2436 sbitmap_free (blocks);
2439 if (inserted_p)
2440 commit_edge_insertions ();
2442 /* Replacing pseudos with their memory equivalents might have
2443 created shared rtx. Subsequent passes would get confused
2444 by this, so unshare everything here. */
2445 unshare_all_rtl_again (get_insns ());
2447 #ifdef ENABLE_CHECKING
2448 check_rtl (true);
2449 #endif
2451 timevar_pop (TV_LRA);
2454 /* Called once per compiler to initialize LRA data once. */
2455 void
2456 lra_init_once (void)
2458 init_insn_code_data_once ();
2461 /* Initialize LRA whenever register-related information is changed. */
2462 void
2463 lra_init (void)
2465 init_op_alt_data ();
2468 /* Called once per compiler to finish LRA data which are initialize
2469 once. */
2470 void
2471 lra_finish_once (void)
2473 finish_insn_code_data_once ();