decl.c (gnat_to_gnu_entity): For a derived untagged type that renames discriminants...
[gcc.git] / gcc / lra.c
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>.
4
5 This file is part of GCC.
6
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.
11
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.
16
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/>. */
20
21
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.
26
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)
33
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.
43
44 Here is block diagram of LRA passes:
45
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 | ------------
66 V
67 -------------------------
68 | Hard regs substitution, |
69 | devirtalization, and |------> Finish
70 | restoring scratches got |
71 | memory |
72 -------------------------
73
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).
91
92 This file contains LRA driver, LRA utility functions and data, and
93 code for dealing with scratches. */
94
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 "hashtab.h"
110 #include "hash-set.h"
111 #include "vec.h"
112 #include "machmode.h"
113 #include "input.h"
114 #include "function.h"
115 #include "tree-core.h"
116 #include "optabs.h"
117 #include "expr.h"
118 #include "predict.h"
119 #include "dominance.h"
120 #include "cfg.h"
121 #include "cfgrtl.h"
122 #include "cfgbuild.h"
123 #include "basic-block.h"
124 #include "except.h"
125 #include "tree-pass.h"
126 #include "timevar.h"
127 #include "target.h"
128 #include "ira.h"
129 #include "lra-int.h"
130 #include "df.h"
131
132 /* Hard registers currently not available for allocation. It can
133 changed after some hard registers become not eliminable. */
134 HARD_REG_SET lra_no_alloc_regs;
135
136 static int get_new_reg_value (void);
137 static void expand_reg_info (void);
138 static void invalidate_insn_recog_data (int);
139 static int get_insn_freq (rtx_insn *);
140 static void invalidate_insn_data_regno_info (lra_insn_recog_data_t,
141 rtx_insn *, int);
142
143 /* Expand all regno related info needed for LRA. */
144 static void
145 expand_reg_data (int old)
146 {
147 resize_reg_info ();
148 expand_reg_info ();
149 ira_expand_reg_equiv ();
150 for (int i = (int) max_reg_num () - 1; i >= old; i--)
151 lra_change_class (i, ALL_REGS, " Set", true);
152 }
153
154 /* Create and return a new reg of ORIGINAL mode. If ORIGINAL is NULL
155 or of VOIDmode, use MD_MODE for the new reg. Initialize its
156 register class to RCLASS. Print message about assigning class
157 RCLASS containing new register name TITLE unless it is NULL. Use
158 attributes of ORIGINAL if it is a register. The created register
159 will have unique held value. */
160 rtx
161 lra_create_new_reg_with_unique_value (machine_mode md_mode, rtx original,
162 enum reg_class rclass, const char *title)
163 {
164 machine_mode mode;
165 rtx new_reg;
166
167 if (original == NULL_RTX || (mode = GET_MODE (original)) == VOIDmode)
168 mode = md_mode;
169 lra_assert (mode != VOIDmode);
170 new_reg = gen_reg_rtx (mode);
171 if (original == NULL_RTX || ! REG_P (original))
172 {
173 if (lra_dump_file != NULL)
174 fprintf (lra_dump_file, " Creating newreg=%i", REGNO (new_reg));
175 }
176 else
177 {
178 if (ORIGINAL_REGNO (original) >= FIRST_PSEUDO_REGISTER)
179 ORIGINAL_REGNO (new_reg) = ORIGINAL_REGNO (original);
180 REG_USERVAR_P (new_reg) = REG_USERVAR_P (original);
181 REG_POINTER (new_reg) = REG_POINTER (original);
182 REG_ATTRS (new_reg) = REG_ATTRS (original);
183 if (lra_dump_file != NULL)
184 fprintf (lra_dump_file, " Creating newreg=%i from oldreg=%i",
185 REGNO (new_reg), REGNO (original));
186 }
187 if (lra_dump_file != NULL)
188 {
189 if (title != NULL)
190 fprintf (lra_dump_file, ", assigning class %s to%s%s r%d",
191 reg_class_names[rclass], *title == '\0' ? "" : " ",
192 title, REGNO (new_reg));
193 fprintf (lra_dump_file, "\n");
194 }
195 expand_reg_data (max_reg_num ());
196 setup_reg_classes (REGNO (new_reg), rclass, NO_REGS, rclass);
197 return new_reg;
198 }
199
200 /* Analogous to the previous function but also inherits value of
201 ORIGINAL. */
202 rtx
203 lra_create_new_reg (machine_mode md_mode, rtx original,
204 enum reg_class rclass, const char *title)
205 {
206 rtx new_reg;
207
208 new_reg
209 = lra_create_new_reg_with_unique_value (md_mode, original, rclass, title);
210 if (original != NULL_RTX && REG_P (original))
211 lra_assign_reg_val (REGNO (original), REGNO (new_reg));
212 return new_reg;
213 }
214
215 /* Set up for REGNO unique hold value. */
216 void
217 lra_set_regno_unique_value (int regno)
218 {
219 lra_reg_info[regno].val = get_new_reg_value ();
220 }
221
222 /* Invalidate INSN related info used by LRA. The info should never be
223 used after that. */
224 void
225 lra_invalidate_insn_data (rtx_insn *insn)
226 {
227 lra_invalidate_insn_regno_info (insn);
228 invalidate_insn_recog_data (INSN_UID (insn));
229 }
230
231 /* Mark INSN deleted and invalidate the insn related info used by
232 LRA. */
233 void
234 lra_set_insn_deleted (rtx_insn *insn)
235 {
236 lra_invalidate_insn_data (insn);
237 SET_INSN_DELETED (insn);
238 }
239
240 /* Delete an unneeded INSN and any previous insns who sole purpose is
241 loading data that is dead in INSN. */
242 void
243 lra_delete_dead_insn (rtx_insn *insn)
244 {
245 rtx_insn *prev = prev_real_insn (insn);
246 rtx prev_dest;
247
248 /* If the previous insn sets a register that dies in our insn,
249 delete it too. */
250 if (prev && GET_CODE (PATTERN (prev)) == SET
251 && (prev_dest = SET_DEST (PATTERN (prev)), REG_P (prev_dest))
252 && reg_mentioned_p (prev_dest, PATTERN (insn))
253 && find_regno_note (insn, REG_DEAD, REGNO (prev_dest))
254 && ! side_effects_p (SET_SRC (PATTERN (prev))))
255 lra_delete_dead_insn (prev);
256
257 lra_set_insn_deleted (insn);
258 }
259
260 /* Emit insn x = y + z. Return NULL if we failed to do it.
261 Otherwise, return the insn. We don't use gen_add3_insn as it might
262 clobber CC. */
263 static rtx
264 emit_add3_insn (rtx x, rtx y, rtx z)
265 {
266 rtx_insn *last;
267
268 last = get_last_insn ();
269
270 if (have_addptr3_insn (x, y, z))
271 {
272 rtx insn = gen_addptr3_insn (x, y, z);
273
274 /* If the target provides an "addptr" pattern it hopefully does
275 for a reason. So falling back to the normal add would be
276 a bug. */
277 lra_assert (insn != NULL_RTX);
278 emit_insn (insn);
279 return insn;
280 }
281
282 rtx_insn *insn = emit_insn (gen_rtx_SET (VOIDmode, x,
283 gen_rtx_PLUS (GET_MODE (y), y, z)));
284 if (recog_memoized (insn) < 0)
285 {
286 delete_insns_since (last);
287 insn = NULL;
288 }
289 return insn;
290 }
291
292 /* Emit insn x = x + y. Return the insn. We use gen_add2_insn as the
293 last resort. */
294 static rtx
295 emit_add2_insn (rtx x, rtx y)
296 {
297 rtx insn;
298
299 insn = emit_add3_insn (x, x, y);
300 if (insn == NULL_RTX)
301 {
302 insn = gen_add2_insn (x, y);
303 if (insn != NULL_RTX)
304 emit_insn (insn);
305 }
306 return insn;
307 }
308
309 /* Target checks operands through operand predicates to recognize an
310 insn. We should have a special precaution to generate add insns
311 which are frequent results of elimination.
312
313 Emit insns for x = y + z. X can be used to store intermediate
314 values and should be not in Y and Z when we use X to store an
315 intermediate value. Y + Z should form [base] [+ index[ * scale]] [
316 + disp] where base and index are registers, disp and scale are
317 constants. Y should contain base if it is present, Z should
318 contain disp if any. index[*scale] can be part of Y or Z. */
319 void
320 lra_emit_add (rtx x, rtx y, rtx z)
321 {
322 int old;
323 rtx_insn *last;
324 rtx a1, a2, base, index, disp, scale, index_scale;
325 bool ok_p;
326
327 rtx add3_insn = emit_add3_insn (x, y, z);
328 old = max_reg_num ();
329 if (add3_insn != NULL)
330 ;
331 else
332 {
333 disp = a2 = NULL_RTX;
334 if (GET_CODE (y) == PLUS)
335 {
336 a1 = XEXP (y, 0);
337 a2 = XEXP (y, 1);
338 disp = z;
339 }
340 else
341 {
342 a1 = y;
343 if (CONSTANT_P (z))
344 disp = z;
345 else
346 a2 = z;
347 }
348 index_scale = scale = NULL_RTX;
349 if (GET_CODE (a1) == MULT)
350 {
351 index_scale = a1;
352 index = XEXP (a1, 0);
353 scale = XEXP (a1, 1);
354 base = a2;
355 }
356 else if (a2 != NULL_RTX && GET_CODE (a2) == MULT)
357 {
358 index_scale = a2;
359 index = XEXP (a2, 0);
360 scale = XEXP (a2, 1);
361 base = a1;
362 }
363 else
364 {
365 base = a1;
366 index = a2;
367 }
368 if (! (REG_P (base) || GET_CODE (base) == SUBREG)
369 || (index != NULL_RTX
370 && ! (REG_P (index) || GET_CODE (index) == SUBREG))
371 || (disp != NULL_RTX && ! CONSTANT_P (disp))
372 || (scale != NULL_RTX && ! CONSTANT_P (scale)))
373 {
374 /* Probably we have no 3 op add. Last chance is to use 2-op
375 add insn. To succeed, don't move Z to X as an address
376 segment always comes in Y. Otherwise, we might fail when
377 adding the address segment to register. */
378 lra_assert (x != y && x != z);
379 emit_move_insn (x, y);
380 rtx insn = emit_add2_insn (x, z);
381 lra_assert (insn != NULL_RTX);
382 }
383 else
384 {
385 if (index_scale == NULL_RTX)
386 index_scale = index;
387 if (disp == NULL_RTX)
388 {
389 /* Generate x = index_scale; x = x + base. */
390 lra_assert (index_scale != NULL_RTX && base != NULL_RTX);
391 emit_move_insn (x, index_scale);
392 rtx insn = emit_add2_insn (x, base);
393 lra_assert (insn != NULL_RTX);
394 }
395 else if (scale == NULL_RTX)
396 {
397 /* Try x = base + disp. */
398 lra_assert (base != NULL_RTX);
399 last = get_last_insn ();
400 rtx_insn *move_insn =
401 emit_move_insn (x, gen_rtx_PLUS (GET_MODE (base), base, disp));
402 if (recog_memoized (move_insn) < 0)
403 {
404 delete_insns_since (last);
405 /* Generate x = disp; x = x + base. */
406 emit_move_insn (x, disp);
407 rtx add2_insn = emit_add2_insn (x, base);
408 lra_assert (add2_insn != NULL_RTX);
409 }
410 /* Generate x = x + index. */
411 if (index != NULL_RTX)
412 {
413 rtx insn = emit_add2_insn (x, index);
414 lra_assert (insn != NULL_RTX);
415 }
416 }
417 else
418 {
419 /* Try x = index_scale; x = x + disp; x = x + base. */
420 last = get_last_insn ();
421 rtx_insn *move_insn = emit_move_insn (x, index_scale);
422 ok_p = false;
423 if (recog_memoized (move_insn) >= 0)
424 {
425 rtx insn = emit_add2_insn (x, disp);
426 if (insn != NULL_RTX)
427 {
428 insn = emit_add2_insn (x, disp);
429 if (insn != NULL_RTX)
430 ok_p = true;
431 }
432 }
433 if (! ok_p)
434 {
435 delete_insns_since (last);
436 /* Generate x = disp; x = x + base; x = x + index_scale. */
437 emit_move_insn (x, disp);
438 rtx insn = emit_add2_insn (x, base);
439 lra_assert (insn != NULL_RTX);
440 insn = emit_add2_insn (x, index_scale);
441 lra_assert (insn != NULL_RTX);
442 }
443 }
444 }
445 }
446 /* Functions emit_... can create pseudos -- so expand the pseudo
447 data. */
448 if (old != max_reg_num ())
449 expand_reg_data (old);
450 }
451
452 /* The number of emitted reload insns so far. */
453 int lra_curr_reload_num;
454
455 /* Emit x := y, processing special case when y = u + v or y = u + v *
456 scale + w through emit_add (Y can be an address which is base +
457 index reg * scale + displacement in general case). X may be used
458 as intermediate result therefore it should be not in Y. */
459 void
460 lra_emit_move (rtx x, rtx y)
461 {
462 int old;
463
464 if (GET_CODE (y) != PLUS)
465 {
466 if (rtx_equal_p (x, y))
467 return;
468 old = max_reg_num ();
469 emit_move_insn (x, y);
470 if (REG_P (x))
471 lra_reg_info[ORIGINAL_REGNO (x)].last_reload = ++lra_curr_reload_num;
472 /* Function emit_move can create pseudos -- so expand the pseudo
473 data. */
474 if (old != max_reg_num ())
475 expand_reg_data (old);
476 return;
477 }
478 lra_emit_add (x, XEXP (y, 0), XEXP (y, 1));
479 }
480
481 /* Update insn operands which are duplication of operands whose
482 numbers are in array of NOPS (with end marker -1). The insn is
483 represented by its LRA internal representation ID. */
484 void
485 lra_update_dups (lra_insn_recog_data_t id, signed char *nops)
486 {
487 int i, j, nop;
488 struct lra_static_insn_data *static_id = id->insn_static_data;
489
490 for (i = 0; i < static_id->n_dups; i++)
491 for (j = 0; (nop = nops[j]) >= 0; j++)
492 if (static_id->dup_num[i] == nop)
493 *id->dup_loc[i] = *id->operand_loc[nop];
494 }
495
496 \f
497
498 /* This page contains code dealing with info about registers in the
499 insns. */
500
501 /* Pools for insn reg info. */
502 static alloc_pool insn_reg_pool;
503
504 /* Initiate pool for insn reg info. */
505 static void
506 init_insn_regs (void)
507 {
508 insn_reg_pool
509 = create_alloc_pool ("insn regs", sizeof (struct lra_insn_reg), 100);
510 }
511
512 /* Create LRA insn related info about a reference to REGNO in INSN with
513 TYPE (in/out/inout), biggest reference mode MODE, flag that it is
514 reference through subreg (SUBREG_P), flag that is early clobbered
515 in the insn (EARLY_CLOBBER), and reference to the next insn reg
516 info (NEXT). */
517 static struct lra_insn_reg *
518 new_insn_reg (rtx_insn *insn, int regno, enum op_type type,
519 machine_mode mode,
520 bool subreg_p, bool early_clobber, struct lra_insn_reg *next)
521 {
522 struct lra_insn_reg *ir;
523
524 ir = (struct lra_insn_reg *) pool_alloc (insn_reg_pool);
525 ir->type = type;
526 ir->biggest_mode = mode;
527 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (lra_reg_info[regno].biggest_mode)
528 && NONDEBUG_INSN_P (insn))
529 lra_reg_info[regno].biggest_mode = mode;
530 ir->subreg_p = subreg_p;
531 ir->early_clobber = early_clobber;
532 ir->regno = regno;
533 ir->next = next;
534 return ir;
535 }
536
537 /* Free insn reg info IR. */
538 static void
539 free_insn_reg (struct lra_insn_reg *ir)
540 {
541 pool_free (insn_reg_pool, ir);
542 }
543
544 /* Free insn reg info list IR. */
545 static void
546 free_insn_regs (struct lra_insn_reg *ir)
547 {
548 struct lra_insn_reg *next_ir;
549
550 for (; ir != NULL; ir = next_ir)
551 {
552 next_ir = ir->next;
553 free_insn_reg (ir);
554 }
555 }
556
557 /* Finish pool for insn reg info. */
558 static void
559 finish_insn_regs (void)
560 {
561 free_alloc_pool (insn_reg_pool);
562 }
563
564 \f
565
566 /* This page contains code dealing LRA insn info (or in other words
567 LRA internal insn representation). */
568
569 /* Map INSN_CODE -> the static insn data. This info is valid during
570 all translation unit. */
571 struct lra_static_insn_data *insn_code_data[LAST_INSN_CODE];
572
573 /* Debug insns are represented as a special insn with one input
574 operand which is RTL expression in var_location. */
575
576 /* The following data are used as static insn operand data for all
577 debug insns. If structure lra_operand_data is changed, the
578 initializer should be changed too. */
579 static struct lra_operand_data debug_operand_data =
580 {
581 NULL, /* alternative */
582 VOIDmode, /* We are not interesting in the operand mode. */
583 OP_IN,
584 0, 0, 0, 0
585 };
586
587 /* The following data are used as static insn data for all debug
588 insns. If structure lra_static_insn_data is changed, the
589 initializer should be changed too. */
590 static struct lra_static_insn_data debug_insn_static_data =
591 {
592 &debug_operand_data,
593 0, /* Duplication operands #. */
594 -1, /* Commutative operand #. */
595 1, /* Operands #. There is only one operand which is debug RTL
596 expression. */
597 0, /* Duplications #. */
598 0, /* Alternatives #. We are not interesting in alternatives
599 because we does not proceed debug_insns for reloads. */
600 NULL, /* Hard registers referenced in machine description. */
601 NULL /* Descriptions of operands in alternatives. */
602 };
603
604 /* Called once per compiler work to initialize some LRA data related
605 to insns. */
606 static void
607 init_insn_code_data_once (void)
608 {
609 memset (insn_code_data, 0, sizeof (insn_code_data));
610 }
611
612 /* Called once per compiler work to finalize some LRA data related to
613 insns. */
614 static void
615 finish_insn_code_data_once (void)
616 {
617 int i;
618
619 for (i = 0; i < LAST_INSN_CODE; i++)
620 {
621 if (insn_code_data[i] != NULL)
622 free (insn_code_data[i]);
623 }
624 }
625
626 /* Return static insn data, allocate and setup if necessary. Although
627 dup_num is static data (it depends only on icode), to set it up we
628 need to extract insn first. So recog_data should be valid for
629 normal insn (ICODE >= 0) before the call. */
630 static struct lra_static_insn_data *
631 get_static_insn_data (int icode, int nop, int ndup, int nalt)
632 {
633 struct lra_static_insn_data *data;
634 size_t n_bytes;
635
636 lra_assert (icode < LAST_INSN_CODE);
637 if (icode >= 0 && (data = insn_code_data[icode]) != NULL)
638 return data;
639 lra_assert (nop >= 0 && ndup >= 0 && nalt >= 0);
640 n_bytes = sizeof (struct lra_static_insn_data)
641 + sizeof (struct lra_operand_data) * nop
642 + sizeof (int) * ndup;
643 data = XNEWVAR (struct lra_static_insn_data, n_bytes);
644 data->operand_alternative = NULL;
645 data->n_operands = nop;
646 data->n_dups = ndup;
647 data->n_alternatives = nalt;
648 data->operand = ((struct lra_operand_data *)
649 ((char *) data + sizeof (struct lra_static_insn_data)));
650 data->dup_num = ((int *) ((char *) data->operand
651 + sizeof (struct lra_operand_data) * nop));
652 if (icode >= 0)
653 {
654 int i;
655
656 insn_code_data[icode] = data;
657 for (i = 0; i < nop; i++)
658 {
659 data->operand[i].constraint
660 = insn_data[icode].operand[i].constraint;
661 data->operand[i].mode = insn_data[icode].operand[i].mode;
662 data->operand[i].strict_low = insn_data[icode].operand[i].strict_low;
663 data->operand[i].is_operator
664 = insn_data[icode].operand[i].is_operator;
665 data->operand[i].type
666 = (data->operand[i].constraint[0] == '=' ? OP_OUT
667 : data->operand[i].constraint[0] == '+' ? OP_INOUT
668 : OP_IN);
669 data->operand[i].is_address = false;
670 }
671 for (i = 0; i < ndup; i++)
672 data->dup_num[i] = recog_data.dup_num[i];
673 }
674 return data;
675 }
676
677 /* The current length of the following array. */
678 int lra_insn_recog_data_len;
679
680 /* Map INSN_UID -> the insn recog data (NULL if unknown). */
681 lra_insn_recog_data_t *lra_insn_recog_data;
682
683 /* Initialize LRA data about insns. */
684 static void
685 init_insn_recog_data (void)
686 {
687 lra_insn_recog_data_len = 0;
688 lra_insn_recog_data = NULL;
689 init_insn_regs ();
690 }
691
692 /* Expand, if necessary, LRA data about insns. */
693 static void
694 check_and_expand_insn_recog_data (int index)
695 {
696 int i, old;
697
698 if (lra_insn_recog_data_len > index)
699 return;
700 old = lra_insn_recog_data_len;
701 lra_insn_recog_data_len = index * 3 / 2 + 1;
702 lra_insn_recog_data = XRESIZEVEC (lra_insn_recog_data_t,
703 lra_insn_recog_data,
704 lra_insn_recog_data_len);
705 for (i = old; i < lra_insn_recog_data_len; i++)
706 lra_insn_recog_data[i] = NULL;
707 }
708
709 /* Finish LRA DATA about insn. */
710 static void
711 free_insn_recog_data (lra_insn_recog_data_t data)
712 {
713 if (data->operand_loc != NULL)
714 free (data->operand_loc);
715 if (data->dup_loc != NULL)
716 free (data->dup_loc);
717 if (data->arg_hard_regs != NULL)
718 free (data->arg_hard_regs);
719 if (data->icode < 0 && NONDEBUG_INSN_P (data->insn))
720 {
721 if (data->insn_static_data->operand_alternative != NULL)
722 free (const_cast <operand_alternative *>
723 (data->insn_static_data->operand_alternative));
724 free_insn_regs (data->insn_static_data->hard_regs);
725 free (data->insn_static_data);
726 }
727 free_insn_regs (data->regs);
728 data->regs = NULL;
729 free (data);
730 }
731
732 /* Finish LRA data about all insns. */
733 static void
734 finish_insn_recog_data (void)
735 {
736 int i;
737 lra_insn_recog_data_t data;
738
739 for (i = 0; i < lra_insn_recog_data_len; i++)
740 if ((data = lra_insn_recog_data[i]) != NULL)
741 free_insn_recog_data (data);
742 finish_insn_regs ();
743 free (lra_insn_recog_data);
744 }
745
746 /* Setup info about operands in alternatives of LRA DATA of insn. */
747 static void
748 setup_operand_alternative (lra_insn_recog_data_t data,
749 const operand_alternative *op_alt)
750 {
751 int i, j, nop, nalt;
752 int icode = data->icode;
753 struct lra_static_insn_data *static_data = data->insn_static_data;
754
755 static_data->commutative = -1;
756 nop = static_data->n_operands;
757 nalt = static_data->n_alternatives;
758 static_data->operand_alternative = op_alt;
759 for (i = 0; i < nop; i++)
760 {
761 static_data->operand[i].early_clobber = false;
762 static_data->operand[i].is_address = false;
763 if (static_data->operand[i].constraint[0] == '%')
764 {
765 /* We currently only support one commutative pair of operands. */
766 if (static_data->commutative < 0)
767 static_data->commutative = i;
768 else
769 lra_assert (icode < 0); /* Asm */
770 /* The last operand should not be marked commutative. */
771 lra_assert (i != nop - 1);
772 }
773 }
774 for (j = 0; j < nalt; j++)
775 for (i = 0; i < nop; i++, op_alt++)
776 {
777 static_data->operand[i].early_clobber |= op_alt->earlyclobber;
778 static_data->operand[i].is_address |= op_alt->is_address;
779 }
780 }
781
782 /* Recursively process X and collect info about registers, which are
783 not the insn operands, in X with TYPE (in/out/inout) and flag that
784 it is early clobbered in the insn (EARLY_CLOBBER) and add the info
785 to LIST. X is a part of insn given by DATA. Return the result
786 list. */
787 static struct lra_insn_reg *
788 collect_non_operand_hard_regs (rtx *x, lra_insn_recog_data_t data,
789 struct lra_insn_reg *list,
790 enum op_type type, bool early_clobber)
791 {
792 int i, j, regno, last;
793 bool subreg_p;
794 machine_mode mode;
795 struct lra_insn_reg *curr;
796 rtx op = *x;
797 enum rtx_code code = GET_CODE (op);
798 const char *fmt = GET_RTX_FORMAT (code);
799
800 for (i = 0; i < data->insn_static_data->n_operands; i++)
801 if (x == data->operand_loc[i])
802 /* It is an operand loc. Stop here. */
803 return list;
804 for (i = 0; i < data->insn_static_data->n_dups; i++)
805 if (x == data->dup_loc[i])
806 /* It is a dup loc. Stop here. */
807 return list;
808 mode = GET_MODE (op);
809 subreg_p = false;
810 if (code == SUBREG)
811 {
812 op = SUBREG_REG (op);
813 code = GET_CODE (op);
814 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (op)))
815 {
816 mode = GET_MODE (op);
817 if (GET_MODE_SIZE (mode) > REGMODE_NATURAL_SIZE (mode))
818 subreg_p = true;
819 }
820 }
821 if (REG_P (op))
822 {
823 if ((regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER)
824 return list;
825 for (last = regno + hard_regno_nregs[regno][mode];
826 regno < last;
827 regno++)
828 if (! TEST_HARD_REG_BIT (lra_no_alloc_regs, regno)
829 || TEST_HARD_REG_BIT (eliminable_regset, regno))
830 {
831 for (curr = list; curr != NULL; curr = curr->next)
832 if (curr->regno == regno && curr->subreg_p == subreg_p
833 && curr->biggest_mode == mode)
834 {
835 if (curr->type != type)
836 curr->type = OP_INOUT;
837 if (curr->early_clobber != early_clobber)
838 curr->early_clobber = true;
839 break;
840 }
841 if (curr == NULL)
842 {
843 /* This is a new hard regno or the info can not be
844 integrated into the found structure. */
845 #ifdef STACK_REGS
846 early_clobber
847 = (early_clobber
848 /* This clobber is to inform popping floating
849 point stack only. */
850 && ! (FIRST_STACK_REG <= regno
851 && regno <= LAST_STACK_REG));
852 #endif
853 list = new_insn_reg (data->insn, regno, type, mode, subreg_p,
854 early_clobber, list);
855 }
856 }
857 return list;
858 }
859 switch (code)
860 {
861 case SET:
862 list = collect_non_operand_hard_regs (&SET_DEST (op), data,
863 list, OP_OUT, false);
864 list = collect_non_operand_hard_regs (&SET_SRC (op), data,
865 list, OP_IN, false);
866 break;
867 case CLOBBER:
868 /* We treat clobber of non-operand hard registers as early
869 clobber (the behavior is expected from asm). */
870 list = collect_non_operand_hard_regs (&XEXP (op, 0), data,
871 list, OP_OUT, true);
872 break;
873 case PRE_INC: case PRE_DEC: case POST_INC: case POST_DEC:
874 list = collect_non_operand_hard_regs (&XEXP (op, 0), data,
875 list, OP_INOUT, false);
876 break;
877 case PRE_MODIFY: case POST_MODIFY:
878 list = collect_non_operand_hard_regs (&XEXP (op, 0), data,
879 list, OP_INOUT, false);
880 list = collect_non_operand_hard_regs (&XEXP (op, 1), data,
881 list, OP_IN, false);
882 break;
883 default:
884 fmt = GET_RTX_FORMAT (code);
885 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
886 {
887 if (fmt[i] == 'e')
888 list = collect_non_operand_hard_regs (&XEXP (op, i), data,
889 list, OP_IN, false);
890 else if (fmt[i] == 'E')
891 for (j = XVECLEN (op, i) - 1; j >= 0; j--)
892 list = collect_non_operand_hard_regs (&XVECEXP (op, i, j), data,
893 list, OP_IN, false);
894 }
895 }
896 return list;
897 }
898
899 /* Set up and return info about INSN. Set up the info if it is not set up
900 yet. */
901 lra_insn_recog_data_t
902 lra_set_insn_recog_data (rtx_insn *insn)
903 {
904 lra_insn_recog_data_t data;
905 int i, n, icode;
906 rtx **locs;
907 unsigned int uid = INSN_UID (insn);
908 struct lra_static_insn_data *insn_static_data;
909
910 check_and_expand_insn_recog_data (uid);
911 if (DEBUG_INSN_P (insn))
912 icode = -1;
913 else
914 {
915 icode = INSN_CODE (insn);
916 if (icode < 0)
917 /* It might be a new simple insn which is not recognized yet. */
918 INSN_CODE (insn) = icode = recog_memoized (insn);
919 }
920 data = XNEW (struct lra_insn_recog_data);
921 lra_insn_recog_data[uid] = data;
922 data->insn = insn;
923 data->used_insn_alternative = -1;
924 data->icode = icode;
925 data->regs = NULL;
926 if (DEBUG_INSN_P (insn))
927 {
928 data->insn_static_data = &debug_insn_static_data;
929 data->dup_loc = NULL;
930 data->arg_hard_regs = NULL;
931 data->preferred_alternatives = ALL_ALTERNATIVES;
932 data->operand_loc = XNEWVEC (rtx *, 1);
933 data->operand_loc[0] = &INSN_VAR_LOCATION_LOC (insn);
934 return data;
935 }
936 if (icode < 0)
937 {
938 int nop, nalt;
939 machine_mode operand_mode[MAX_RECOG_OPERANDS];
940 const char *constraints[MAX_RECOG_OPERANDS];
941
942 nop = asm_noperands (PATTERN (insn));
943 data->operand_loc = data->dup_loc = NULL;
944 nalt = 1;
945 if (nop < 0)
946 {
947 /* It is a special insn like USE or CLOBBER. We should
948 recognize any regular insn otherwise LRA can do nothing
949 with this insn. */
950 gcc_assert (GET_CODE (PATTERN (insn)) == USE
951 || GET_CODE (PATTERN (insn)) == CLOBBER
952 || GET_CODE (PATTERN (insn)) == ASM_INPUT);
953 data->insn_static_data = insn_static_data
954 = get_static_insn_data (-1, 0, 0, nalt);
955 }
956 else
957 {
958 /* expand_asm_operands makes sure there aren't too many
959 operands. */
960 lra_assert (nop <= MAX_RECOG_OPERANDS);
961 if (nop != 0)
962 data->operand_loc = XNEWVEC (rtx *, nop);
963 /* Now get the operand values and constraints out of the
964 insn. */
965 decode_asm_operands (PATTERN (insn), NULL,
966 data->operand_loc,
967 constraints, operand_mode, NULL);
968 if (nop > 0)
969 {
970 const char *p = recog_data.constraints[0];
971
972 for (p = constraints[0]; *p; p++)
973 nalt += *p == ',';
974 }
975 data->insn_static_data = insn_static_data
976 = get_static_insn_data (-1, nop, 0, nalt);
977 for (i = 0; i < nop; i++)
978 {
979 insn_static_data->operand[i].mode = operand_mode[i];
980 insn_static_data->operand[i].constraint = constraints[i];
981 insn_static_data->operand[i].strict_low = false;
982 insn_static_data->operand[i].is_operator = false;
983 insn_static_data->operand[i].is_address = false;
984 }
985 }
986 for (i = 0; i < insn_static_data->n_operands; i++)
987 insn_static_data->operand[i].type
988 = (insn_static_data->operand[i].constraint[0] == '=' ? OP_OUT
989 : insn_static_data->operand[i].constraint[0] == '+' ? OP_INOUT
990 : OP_IN);
991 data->preferred_alternatives = ALL_ALTERNATIVES;
992 if (nop > 0)
993 {
994 operand_alternative *op_alt = XCNEWVEC (operand_alternative,
995 nalt * nop);
996 preprocess_constraints (nop, nalt, constraints, op_alt);
997 setup_operand_alternative (data, op_alt);
998 }
999 }
1000 else
1001 {
1002 insn_extract (insn);
1003 data->insn_static_data = insn_static_data
1004 = get_static_insn_data (icode, insn_data[icode].n_operands,
1005 insn_data[icode].n_dups,
1006 insn_data[icode].n_alternatives);
1007 n = insn_static_data->n_operands;
1008 if (n == 0)
1009 locs = NULL;
1010 else
1011 {
1012 locs = XNEWVEC (rtx *, n);
1013 memcpy (locs, recog_data.operand_loc, n * sizeof (rtx *));
1014 }
1015 data->operand_loc = locs;
1016 n = insn_static_data->n_dups;
1017 if (n == 0)
1018 locs = NULL;
1019 else
1020 {
1021 locs = XNEWVEC (rtx *, n);
1022 memcpy (locs, recog_data.dup_loc, n * sizeof (rtx *));
1023 }
1024 data->dup_loc = locs;
1025 data->preferred_alternatives = get_preferred_alternatives (insn);
1026 const operand_alternative *op_alt = preprocess_insn_constraints (icode);
1027 if (!insn_static_data->operand_alternative)
1028 setup_operand_alternative (data, op_alt);
1029 else if (op_alt != insn_static_data->operand_alternative)
1030 insn_static_data->operand_alternative = op_alt;
1031 }
1032 if (GET_CODE (PATTERN (insn)) == CLOBBER || GET_CODE (PATTERN (insn)) == USE)
1033 insn_static_data->hard_regs = NULL;
1034 else
1035 insn_static_data->hard_regs
1036 = collect_non_operand_hard_regs (&PATTERN (insn), data,
1037 NULL, OP_IN, false);
1038 data->arg_hard_regs = NULL;
1039 if (CALL_P (insn))
1040 {
1041 rtx link;
1042 int n_hard_regs, regno, arg_hard_regs[FIRST_PSEUDO_REGISTER];
1043
1044 n_hard_regs = 0;
1045 /* Finding implicit hard register usage. We believe it will be
1046 not changed whatever transformations are used. Call insns
1047 are such example. */
1048 for (link = CALL_INSN_FUNCTION_USAGE (insn);
1049 link != NULL_RTX;
1050 link = XEXP (link, 1))
1051 if (GET_CODE (XEXP (link, 0)) == USE
1052 && REG_P (XEXP (XEXP (link, 0), 0)))
1053 {
1054 regno = REGNO (XEXP (XEXP (link, 0), 0));
1055 lra_assert (regno < FIRST_PSEUDO_REGISTER);
1056 /* It is an argument register. */
1057 for (i = (hard_regno_nregs
1058 [regno][GET_MODE (XEXP (XEXP (link, 0), 0))]) - 1;
1059 i >= 0;
1060 i--)
1061 arg_hard_regs[n_hard_regs++] = regno + i;
1062 }
1063 if (n_hard_regs != 0)
1064 {
1065 arg_hard_regs[n_hard_regs++] = -1;
1066 data->arg_hard_regs = XNEWVEC (int, n_hard_regs);
1067 memcpy (data->arg_hard_regs, arg_hard_regs,
1068 sizeof (int) * n_hard_regs);
1069 }
1070 }
1071 /* Some output operand can be recognized only from the context not
1072 from the constraints which are empty in this case. Call insn may
1073 contain a hard register in set destination with empty constraint
1074 and extract_insn treats them as an input. */
1075 for (i = 0; i < insn_static_data->n_operands; i++)
1076 {
1077 int j;
1078 rtx pat, set;
1079 struct lra_operand_data *operand = &insn_static_data->operand[i];
1080
1081 /* ??? Should we treat 'X' the same way. It looks to me that
1082 'X' means anything and empty constraint means we do not
1083 care. */
1084 if (operand->type != OP_IN || *operand->constraint != '\0'
1085 || operand->is_operator)
1086 continue;
1087 pat = PATTERN (insn);
1088 if (GET_CODE (pat) == SET)
1089 {
1090 if (data->operand_loc[i] != &SET_DEST (pat))
1091 continue;
1092 }
1093 else if (GET_CODE (pat) == PARALLEL)
1094 {
1095 for (j = XVECLEN (pat, 0) - 1; j >= 0; j--)
1096 {
1097 set = XVECEXP (PATTERN (insn), 0, j);
1098 if (GET_CODE (set) == SET
1099 && &SET_DEST (set) == data->operand_loc[i])
1100 break;
1101 }
1102 if (j < 0)
1103 continue;
1104 }
1105 else
1106 continue;
1107 operand->type = OP_OUT;
1108 }
1109 return data;
1110 }
1111
1112 /* Return info about insn give by UID. The info should be already set
1113 up. */
1114 static lra_insn_recog_data_t
1115 get_insn_recog_data_by_uid (int uid)
1116 {
1117 lra_insn_recog_data_t data;
1118
1119 data = lra_insn_recog_data[uid];
1120 lra_assert (data != NULL);
1121 return data;
1122 }
1123
1124 /* Invalidate all info about insn given by its UID. */
1125 static void
1126 invalidate_insn_recog_data (int uid)
1127 {
1128 lra_insn_recog_data_t data;
1129
1130 data = lra_insn_recog_data[uid];
1131 lra_assert (data != NULL);
1132 free_insn_recog_data (data);
1133 lra_insn_recog_data[uid] = NULL;
1134 }
1135
1136 /* Update all the insn info about INSN. It is usually called when
1137 something in the insn was changed. Return the updated info. */
1138 lra_insn_recog_data_t
1139 lra_update_insn_recog_data (rtx_insn *insn)
1140 {
1141 lra_insn_recog_data_t data;
1142 int n;
1143 unsigned int uid = INSN_UID (insn);
1144 struct lra_static_insn_data *insn_static_data;
1145 HOST_WIDE_INT sp_offset = 0;
1146
1147 check_and_expand_insn_recog_data (uid);
1148 if ((data = lra_insn_recog_data[uid]) != NULL
1149 && data->icode != INSN_CODE (insn))
1150 {
1151 sp_offset = data->sp_offset;
1152 invalidate_insn_data_regno_info (data, insn, get_insn_freq (insn));
1153 invalidate_insn_recog_data (uid);
1154 data = NULL;
1155 }
1156 if (data == NULL)
1157 {
1158 data = lra_get_insn_recog_data (insn);
1159 /* Initiate or restore SP offset. */
1160 data->sp_offset = sp_offset;
1161 return data;
1162 }
1163 insn_static_data = data->insn_static_data;
1164 data->used_insn_alternative = -1;
1165 if (DEBUG_INSN_P (insn))
1166 return data;
1167 if (data->icode < 0)
1168 {
1169 int nop;
1170 machine_mode operand_mode[MAX_RECOG_OPERANDS];
1171 const char *constraints[MAX_RECOG_OPERANDS];
1172
1173 nop = asm_noperands (PATTERN (insn));
1174 if (nop >= 0)
1175 {
1176 lra_assert (nop == data->insn_static_data->n_operands);
1177 /* Now get the operand values and constraints out of the
1178 insn. */
1179 decode_asm_operands (PATTERN (insn), NULL,
1180 data->operand_loc,
1181 constraints, operand_mode, NULL);
1182 #ifdef ENABLE_CHECKING
1183 {
1184 int i;
1185
1186 for (i = 0; i < nop; i++)
1187 lra_assert
1188 (insn_static_data->operand[i].mode == operand_mode[i]
1189 && insn_static_data->operand[i].constraint == constraints[i]
1190 && ! insn_static_data->operand[i].is_operator);
1191 }
1192 #endif
1193 }
1194 #ifdef ENABLE_CHECKING
1195 {
1196 int i;
1197
1198 for (i = 0; i < insn_static_data->n_operands; i++)
1199 lra_assert
1200 (insn_static_data->operand[i].type
1201 == (insn_static_data->operand[i].constraint[0] == '=' ? OP_OUT
1202 : insn_static_data->operand[i].constraint[0] == '+' ? OP_INOUT
1203 : OP_IN));
1204 }
1205 #endif
1206 }
1207 else
1208 {
1209 insn_extract (insn);
1210 n = insn_static_data->n_operands;
1211 if (n != 0)
1212 memcpy (data->operand_loc, recog_data.operand_loc, n * sizeof (rtx *));
1213 n = insn_static_data->n_dups;
1214 if (n != 0)
1215 memcpy (data->dup_loc, recog_data.dup_loc, n * sizeof (rtx *));
1216 lra_assert (check_bool_attrs (insn));
1217 }
1218 return data;
1219 }
1220
1221 /* Set up that INSN is using alternative ALT now. */
1222 void
1223 lra_set_used_insn_alternative (rtx_insn *insn, int alt)
1224 {
1225 lra_insn_recog_data_t data;
1226
1227 data = lra_get_insn_recog_data (insn);
1228 data->used_insn_alternative = alt;
1229 }
1230
1231 /* Set up that insn with UID is using alternative ALT now. The insn
1232 info should be already set up. */
1233 void
1234 lra_set_used_insn_alternative_by_uid (int uid, int alt)
1235 {
1236 lra_insn_recog_data_t data;
1237
1238 check_and_expand_insn_recog_data (uid);
1239 data = lra_insn_recog_data[uid];
1240 lra_assert (data != NULL);
1241 data->used_insn_alternative = alt;
1242 }
1243
1244 \f
1245
1246 /* This page contains code dealing with common register info and
1247 pseudo copies. */
1248
1249 /* The size of the following array. */
1250 static int reg_info_size;
1251 /* Common info about each register. */
1252 struct lra_reg *lra_reg_info;
1253
1254 /* Last register value. */
1255 static int last_reg_value;
1256
1257 /* Return new register value. */
1258 static int
1259 get_new_reg_value (void)
1260 {
1261 return ++last_reg_value;
1262 }
1263
1264 /* Pools for copies. */
1265 static alloc_pool copy_pool;
1266
1267 /* Vec referring to pseudo copies. */
1268 static vec<lra_copy_t> copy_vec;
1269
1270 /* Initialize I-th element of lra_reg_info. */
1271 static inline void
1272 initialize_lra_reg_info_element (int i)
1273 {
1274 bitmap_initialize (&lra_reg_info[i].insn_bitmap, &reg_obstack);
1275 #ifdef STACK_REGS
1276 lra_reg_info[i].no_stack_p = false;
1277 #endif
1278 CLEAR_HARD_REG_SET (lra_reg_info[i].conflict_hard_regs);
1279 CLEAR_HARD_REG_SET (lra_reg_info[i].actual_call_used_reg_set);
1280 lra_reg_info[i].preferred_hard_regno1 = -1;
1281 lra_reg_info[i].preferred_hard_regno2 = -1;
1282 lra_reg_info[i].preferred_hard_regno_profit1 = 0;
1283 lra_reg_info[i].preferred_hard_regno_profit2 = 0;
1284 lra_reg_info[i].biggest_mode = VOIDmode;
1285 lra_reg_info[i].live_ranges = NULL;
1286 lra_reg_info[i].nrefs = lra_reg_info[i].freq = 0;
1287 lra_reg_info[i].last_reload = 0;
1288 lra_reg_info[i].restore_regno = -1;
1289 lra_reg_info[i].val = get_new_reg_value ();
1290 lra_reg_info[i].offset = 0;
1291 lra_reg_info[i].copies = NULL;
1292 }
1293
1294 /* Initialize common reg info and copies. */
1295 static void
1296 init_reg_info (void)
1297 {
1298 int i;
1299
1300 last_reg_value = 0;
1301 reg_info_size = max_reg_num () * 3 / 2 + 1;
1302 lra_reg_info = XNEWVEC (struct lra_reg, reg_info_size);
1303 for (i = 0; i < reg_info_size; i++)
1304 initialize_lra_reg_info_element (i);
1305 copy_pool
1306 = create_alloc_pool ("lra copies", sizeof (struct lra_copy), 100);
1307 copy_vec.create (100);
1308 }
1309
1310
1311 /* Finish common reg info and copies. */
1312 static void
1313 finish_reg_info (void)
1314 {
1315 int i;
1316
1317 for (i = 0; i < reg_info_size; i++)
1318 bitmap_clear (&lra_reg_info[i].insn_bitmap);
1319 free (lra_reg_info);
1320 reg_info_size = 0;
1321 free_alloc_pool (copy_pool);
1322 copy_vec.release ();
1323 }
1324
1325 /* Expand common reg info if it is necessary. */
1326 static void
1327 expand_reg_info (void)
1328 {
1329 int i, old = reg_info_size;
1330
1331 if (reg_info_size > max_reg_num ())
1332 return;
1333 reg_info_size = max_reg_num () * 3 / 2 + 1;
1334 lra_reg_info = XRESIZEVEC (struct lra_reg, lra_reg_info, reg_info_size);
1335 for (i = old; i < reg_info_size; i++)
1336 initialize_lra_reg_info_element (i);
1337 }
1338
1339 /* Free all copies. */
1340 void
1341 lra_free_copies (void)
1342 {
1343 lra_copy_t cp;
1344
1345 while (copy_vec.length () != 0)
1346 {
1347 cp = copy_vec.pop ();
1348 lra_reg_info[cp->regno1].copies = lra_reg_info[cp->regno2].copies = NULL;
1349 pool_free (copy_pool, cp);
1350 }
1351 }
1352
1353 /* Create copy of two pseudos REGNO1 and REGNO2. The copy execution
1354 frequency is FREQ. */
1355 void
1356 lra_create_copy (int regno1, int regno2, int freq)
1357 {
1358 bool regno1_dest_p;
1359 lra_copy_t cp;
1360
1361 lra_assert (regno1 != regno2);
1362 regno1_dest_p = true;
1363 if (regno1 > regno2)
1364 {
1365 int temp = regno2;
1366
1367 regno1_dest_p = false;
1368 regno2 = regno1;
1369 regno1 = temp;
1370 }
1371 cp = (lra_copy_t) pool_alloc (copy_pool);
1372 copy_vec.safe_push (cp);
1373 cp->regno1_dest_p = regno1_dest_p;
1374 cp->freq = freq;
1375 cp->regno1 = regno1;
1376 cp->regno2 = regno2;
1377 cp->regno1_next = lra_reg_info[regno1].copies;
1378 lra_reg_info[regno1].copies = cp;
1379 cp->regno2_next = lra_reg_info[regno2].copies;
1380 lra_reg_info[regno2].copies = cp;
1381 if (lra_dump_file != NULL)
1382 fprintf (lra_dump_file, " Creating copy r%d%sr%d@%d\n",
1383 regno1, regno1_dest_p ? "<-" : "->", regno2, freq);
1384 }
1385
1386 /* Return N-th (0, 1, ...) copy. If there is no copy, return
1387 NULL. */
1388 lra_copy_t
1389 lra_get_copy (int n)
1390 {
1391 if (n >= (int) copy_vec.length ())
1392 return NULL;
1393 return copy_vec[n];
1394 }
1395
1396 \f
1397
1398 /* This page contains code dealing with info about registers in
1399 insns. */
1400
1401 /* Process X of insn UID recursively and add info (operand type is
1402 given by TYPE, flag of that it is early clobber is EARLY_CLOBBER)
1403 about registers in X to the insn DATA. */
1404 static void
1405 add_regs_to_insn_regno_info (lra_insn_recog_data_t data, rtx x, int uid,
1406 enum op_type type, bool early_clobber)
1407 {
1408 int i, j, regno;
1409 bool subreg_p;
1410 machine_mode mode;
1411 const char *fmt;
1412 enum rtx_code code;
1413 struct lra_insn_reg *curr;
1414
1415 code = GET_CODE (x);
1416 mode = GET_MODE (x);
1417 subreg_p = false;
1418 if (GET_CODE (x) == SUBREG)
1419 {
1420 x = SUBREG_REG (x);
1421 code = GET_CODE (x);
1422 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (x)))
1423 {
1424 mode = GET_MODE (x);
1425 if (GET_MODE_SIZE (mode) > REGMODE_NATURAL_SIZE (mode))
1426 subreg_p = true;
1427 }
1428 }
1429 if (REG_P (x))
1430 {
1431 regno = REGNO (x);
1432 if (regno < FIRST_PSEUDO_REGISTER
1433 && TEST_HARD_REG_BIT (lra_no_alloc_regs, regno)
1434 && ! TEST_HARD_REG_BIT (eliminable_regset, regno))
1435 return;
1436 expand_reg_info ();
1437 if (bitmap_set_bit (&lra_reg_info[regno].insn_bitmap, uid))
1438 {
1439 data->regs = new_insn_reg (data->insn, regno, type, mode, subreg_p,
1440 early_clobber, data->regs);
1441 return;
1442 }
1443 else
1444 {
1445 for (curr = data->regs; curr != NULL; curr = curr->next)
1446 if (curr->regno == regno)
1447 {
1448 if (curr->subreg_p != subreg_p || curr->biggest_mode != mode)
1449 /* The info can not be integrated into the found
1450 structure. */
1451 data->regs = new_insn_reg (data->insn, regno, type, mode,
1452 subreg_p, early_clobber,
1453 data->regs);
1454 else
1455 {
1456 if (curr->type != type)
1457 curr->type = OP_INOUT;
1458 if (curr->early_clobber != early_clobber)
1459 curr->early_clobber = true;
1460 }
1461 return;
1462 }
1463 gcc_unreachable ();
1464 }
1465 }
1466
1467 switch (code)
1468 {
1469 case SET:
1470 add_regs_to_insn_regno_info (data, SET_DEST (x), uid, OP_OUT, false);
1471 add_regs_to_insn_regno_info (data, SET_SRC (x), uid, OP_IN, false);
1472 break;
1473 case CLOBBER:
1474 /* We treat clobber of non-operand hard registers as early
1475 clobber (the behavior is expected from asm). */
1476 add_regs_to_insn_regno_info (data, XEXP (x, 0), uid, OP_OUT, true);
1477 break;
1478 case PRE_INC: case PRE_DEC: case POST_INC: case POST_DEC:
1479 add_regs_to_insn_regno_info (data, XEXP (x, 0), uid, OP_INOUT, false);
1480 break;
1481 case PRE_MODIFY: case POST_MODIFY:
1482 add_regs_to_insn_regno_info (data, XEXP (x, 0), uid, OP_INOUT, false);
1483 add_regs_to_insn_regno_info (data, XEXP (x, 1), uid, OP_IN, false);
1484 break;
1485 default:
1486 if ((code != PARALLEL && code != EXPR_LIST) || type != OP_OUT)
1487 /* Some targets place small structures in registers for return
1488 values of functions, and those registers are wrapped in
1489 PARALLEL that we may see as the destination of a SET. Here
1490 is an example:
1491
1492 (call_insn 13 12 14 2 (set (parallel:BLK [
1493 (expr_list:REG_DEP_TRUE (reg:DI 0 ax)
1494 (const_int 0 [0]))
1495 (expr_list:REG_DEP_TRUE (reg:DI 1 dx)
1496 (const_int 8 [0x8]))
1497 ])
1498 (call (mem:QI (symbol_ref:DI (... */
1499 type = OP_IN;
1500 fmt = GET_RTX_FORMAT (code);
1501 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1502 {
1503 if (fmt[i] == 'e')
1504 add_regs_to_insn_regno_info (data, XEXP (x, i), uid, type, false);
1505 else if (fmt[i] == 'E')
1506 {
1507 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1508 add_regs_to_insn_regno_info (data, XVECEXP (x, i, j), uid,
1509 type, false);
1510 }
1511 }
1512 }
1513 }
1514
1515 /* Return execution frequency of INSN. */
1516 static int
1517 get_insn_freq (rtx_insn *insn)
1518 {
1519 basic_block bb = BLOCK_FOR_INSN (insn);
1520
1521 gcc_checking_assert (bb != NULL);
1522 return REG_FREQ_FROM_BB (bb);
1523 }
1524
1525 /* Invalidate all reg info of INSN with DATA and execution frequency
1526 FREQ. Update common info about the invalidated registers. */
1527 static void
1528 invalidate_insn_data_regno_info (lra_insn_recog_data_t data, rtx_insn *insn,
1529 int freq)
1530 {
1531 int uid;
1532 bool debug_p;
1533 unsigned int i;
1534 struct lra_insn_reg *ir, *next_ir;
1535
1536 uid = INSN_UID (insn);
1537 debug_p = DEBUG_INSN_P (insn);
1538 for (ir = data->regs; ir != NULL; ir = next_ir)
1539 {
1540 i = ir->regno;
1541 next_ir = ir->next;
1542 free_insn_reg (ir);
1543 bitmap_clear_bit (&lra_reg_info[i].insn_bitmap, uid);
1544 if (i >= FIRST_PSEUDO_REGISTER && ! debug_p)
1545 {
1546 lra_reg_info[i].nrefs--;
1547 lra_reg_info[i].freq -= freq;
1548 lra_assert (lra_reg_info[i].nrefs >= 0 && lra_reg_info[i].freq >= 0);
1549 }
1550 }
1551 data->regs = NULL;
1552 }
1553
1554 /* Invalidate all reg info of INSN. Update common info about the
1555 invalidated registers. */
1556 void
1557 lra_invalidate_insn_regno_info (rtx_insn *insn)
1558 {
1559 invalidate_insn_data_regno_info (lra_get_insn_recog_data (insn), insn,
1560 get_insn_freq (insn));
1561 }
1562
1563 /* Update common reg info from reg info of insn given by its DATA and
1564 execution frequency FREQ. */
1565 static void
1566 setup_insn_reg_info (lra_insn_recog_data_t data, int freq)
1567 {
1568 unsigned int i;
1569 struct lra_insn_reg *ir;
1570
1571 for (ir = data->regs; ir != NULL; ir = ir->next)
1572 if ((i = ir->regno) >= FIRST_PSEUDO_REGISTER)
1573 {
1574 lra_reg_info[i].nrefs++;
1575 lra_reg_info[i].freq += freq;
1576 }
1577 }
1578
1579 /* Set up insn reg info of INSN. Update common reg info from reg info
1580 of INSN. */
1581 void
1582 lra_update_insn_regno_info (rtx_insn *insn)
1583 {
1584 int i, uid, freq;
1585 lra_insn_recog_data_t data;
1586 struct lra_static_insn_data *static_data;
1587 enum rtx_code code;
1588
1589 if (! INSN_P (insn))
1590 return;
1591 data = lra_get_insn_recog_data (insn);
1592 static_data = data->insn_static_data;
1593 freq = get_insn_freq (insn);
1594 invalidate_insn_data_regno_info (data, insn, freq);
1595 uid = INSN_UID (insn);
1596 for (i = static_data->n_operands - 1; i >= 0; i--)
1597 add_regs_to_insn_regno_info (data, *data->operand_loc[i], uid,
1598 static_data->operand[i].type,
1599 static_data->operand[i].early_clobber);
1600 if ((code = GET_CODE (PATTERN (insn))) == CLOBBER || code == USE)
1601 add_regs_to_insn_regno_info (data, XEXP (PATTERN (insn), 0), uid,
1602 code == USE ? OP_IN : OP_OUT, false);
1603 if (NONDEBUG_INSN_P (insn))
1604 setup_insn_reg_info (data, freq);
1605 }
1606
1607 /* Return reg info of insn given by it UID. */
1608 struct lra_insn_reg *
1609 lra_get_insn_regs (int uid)
1610 {
1611 lra_insn_recog_data_t data;
1612
1613 data = get_insn_recog_data_by_uid (uid);
1614 return data->regs;
1615 }
1616
1617 \f
1618
1619 /* This page contains code dealing with stack of the insns which
1620 should be processed by the next constraint pass. */
1621
1622 /* Bitmap used to put an insn on the stack only in one exemplar. */
1623 static sbitmap lra_constraint_insn_stack_bitmap;
1624
1625 /* The stack itself. */
1626 vec<rtx_insn *> lra_constraint_insn_stack;
1627
1628 /* Put INSN on the stack. If ALWAYS_UPDATE is true, always update the reg
1629 info for INSN, otherwise only update it if INSN is not already on the
1630 stack. */
1631 static inline void
1632 lra_push_insn_1 (rtx_insn *insn, bool always_update)
1633 {
1634 unsigned int uid = INSN_UID (insn);
1635 if (always_update)
1636 lra_update_insn_regno_info (insn);
1637 if (uid >= SBITMAP_SIZE (lra_constraint_insn_stack_bitmap))
1638 lra_constraint_insn_stack_bitmap =
1639 sbitmap_resize (lra_constraint_insn_stack_bitmap, 3 * uid / 2, 0);
1640 if (bitmap_bit_p (lra_constraint_insn_stack_bitmap, uid))
1641 return;
1642 bitmap_set_bit (lra_constraint_insn_stack_bitmap, uid);
1643 if (! always_update)
1644 lra_update_insn_regno_info (insn);
1645 lra_constraint_insn_stack.safe_push (insn);
1646 }
1647
1648 /* Put INSN on the stack. */
1649 void
1650 lra_push_insn (rtx_insn *insn)
1651 {
1652 lra_push_insn_1 (insn, false);
1653 }
1654
1655 /* Put INSN on the stack and update its reg info. */
1656 void
1657 lra_push_insn_and_update_insn_regno_info (rtx_insn *insn)
1658 {
1659 lra_push_insn_1 (insn, true);
1660 }
1661
1662 /* Put insn with UID on the stack. */
1663 void
1664 lra_push_insn_by_uid (unsigned int uid)
1665 {
1666 lra_push_insn (lra_insn_recog_data[uid]->insn);
1667 }
1668
1669 /* Take the last-inserted insns off the stack and return it. */
1670 rtx_insn *
1671 lra_pop_insn (void)
1672 {
1673 rtx_insn *insn = lra_constraint_insn_stack.pop ();
1674 bitmap_clear_bit (lra_constraint_insn_stack_bitmap, INSN_UID (insn));
1675 return insn;
1676 }
1677
1678 /* Return the current size of the insn stack. */
1679 unsigned int
1680 lra_insn_stack_length (void)
1681 {
1682 return lra_constraint_insn_stack.length ();
1683 }
1684
1685 /* Push insns FROM to TO (excluding it) going in reverse order. */
1686 static void
1687 push_insns (rtx_insn *from, rtx_insn *to)
1688 {
1689 rtx_insn *insn;
1690
1691 if (from == NULL_RTX)
1692 return;
1693 for (insn = from; insn != to; insn = PREV_INSN (insn))
1694 if (INSN_P (insn))
1695 lra_push_insn (insn);
1696 }
1697
1698 /* Set up sp offset for insn in range [FROM, LAST]. The offset is
1699 taken from the next BB insn after LAST or zero if there in such
1700 insn. */
1701 static void
1702 setup_sp_offset (rtx_insn *from, rtx_insn *last)
1703 {
1704 rtx_insn *before = next_nonnote_insn_bb (last);
1705 HOST_WIDE_INT offset = (before == NULL_RTX || ! INSN_P (before)
1706 ? 0 : lra_get_insn_recog_data (before)->sp_offset);
1707
1708 for (rtx_insn *insn = from; insn != NEXT_INSN (last); insn = NEXT_INSN (insn))
1709 lra_get_insn_recog_data (insn)->sp_offset = offset;
1710 }
1711
1712 /* Emit insns BEFORE before INSN and insns AFTER after INSN. Put the
1713 insns onto the stack. Print about emitting the insns with
1714 TITLE. */
1715 void
1716 lra_process_new_insns (rtx_insn *insn, rtx_insn *before, rtx_insn *after,
1717 const char *title)
1718 {
1719 rtx_insn *last;
1720
1721 if (before == NULL_RTX && after == NULL_RTX)
1722 return;
1723 if (lra_dump_file != NULL)
1724 {
1725 dump_insn_slim (lra_dump_file, insn);
1726 if (before != NULL_RTX)
1727 {
1728 fprintf (lra_dump_file," %s before:\n", title);
1729 dump_rtl_slim (lra_dump_file, before, NULL, -1, 0);
1730 }
1731 if (after != NULL_RTX)
1732 {
1733 fprintf (lra_dump_file, " %s after:\n", title);
1734 dump_rtl_slim (lra_dump_file, after, NULL, -1, 0);
1735 }
1736 fprintf (lra_dump_file, "\n");
1737 }
1738 if (before != NULL_RTX)
1739 {
1740 emit_insn_before (before, insn);
1741 push_insns (PREV_INSN (insn), PREV_INSN (before));
1742 setup_sp_offset (before, PREV_INSN (insn));
1743 }
1744 if (after != NULL_RTX)
1745 {
1746 for (last = after; NEXT_INSN (last) != NULL_RTX; last = NEXT_INSN (last))
1747 ;
1748 emit_insn_after (after, insn);
1749 push_insns (last, insn);
1750 setup_sp_offset (after, last);
1751 }
1752 }
1753
1754 \f
1755
1756 /* This page contains code dealing with scratches (changing them onto
1757 pseudos and restoring them from the pseudos).
1758
1759 We change scratches into pseudos at the beginning of LRA to
1760 simplify dealing with them (conflicts, hard register assignments).
1761
1762 If the pseudo denoting scratch was spilled it means that we do need
1763 a hard register for it. Such pseudos are transformed back to
1764 scratches at the end of LRA. */
1765
1766 /* Description of location of a former scratch operand. */
1767 struct sloc
1768 {
1769 rtx_insn *insn; /* Insn where the scratch was. */
1770 int nop; /* Number of the operand which was a scratch. */
1771 };
1772
1773 typedef struct sloc *sloc_t;
1774
1775 /* Locations of the former scratches. */
1776 static vec<sloc_t> scratches;
1777
1778 /* Bitmap of scratch regnos. */
1779 static bitmap_head scratch_bitmap;
1780
1781 /* Bitmap of scratch operands. */
1782 static bitmap_head scratch_operand_bitmap;
1783
1784 /* Return true if pseudo REGNO is made of SCRATCH. */
1785 bool
1786 lra_former_scratch_p (int regno)
1787 {
1788 return bitmap_bit_p (&scratch_bitmap, regno);
1789 }
1790
1791 /* Return true if the operand NOP of INSN is a former scratch. */
1792 bool
1793 lra_former_scratch_operand_p (rtx_insn *insn, int nop)
1794 {
1795 return bitmap_bit_p (&scratch_operand_bitmap,
1796 INSN_UID (insn) * MAX_RECOG_OPERANDS + nop) != 0;
1797 }
1798
1799 /* Change scratches onto pseudos and save their location. */
1800 static void
1801 remove_scratches (void)
1802 {
1803 int i;
1804 bool insn_changed_p;
1805 basic_block bb;
1806 rtx_insn *insn;
1807 rtx reg;
1808 sloc_t loc;
1809 lra_insn_recog_data_t id;
1810 struct lra_static_insn_data *static_id;
1811
1812 scratches.create (get_max_uid ());
1813 bitmap_initialize (&scratch_bitmap, &reg_obstack);
1814 bitmap_initialize (&scratch_operand_bitmap, &reg_obstack);
1815 FOR_EACH_BB_FN (bb, cfun)
1816 FOR_BB_INSNS (bb, insn)
1817 if (INSN_P (insn))
1818 {
1819 id = lra_get_insn_recog_data (insn);
1820 static_id = id->insn_static_data;
1821 insn_changed_p = false;
1822 for (i = 0; i < static_id->n_operands; i++)
1823 if (GET_CODE (*id->operand_loc[i]) == SCRATCH
1824 && GET_MODE (*id->operand_loc[i]) != VOIDmode)
1825 {
1826 insn_changed_p = true;
1827 *id->operand_loc[i] = reg
1828 = lra_create_new_reg (static_id->operand[i].mode,
1829 *id->operand_loc[i], ALL_REGS, NULL);
1830 add_reg_note (insn, REG_UNUSED, reg);
1831 lra_update_dup (id, i);
1832 loc = XNEW (struct sloc);
1833 loc->insn = insn;
1834 loc->nop = i;
1835 scratches.safe_push (loc);
1836 bitmap_set_bit (&scratch_bitmap, REGNO (*id->operand_loc[i]));
1837 bitmap_set_bit (&scratch_operand_bitmap,
1838 INSN_UID (insn) * MAX_RECOG_OPERANDS + i);
1839 if (lra_dump_file != NULL)
1840 fprintf (lra_dump_file,
1841 "Removing SCRATCH in insn #%u (nop %d)\n",
1842 INSN_UID (insn), i);
1843 }
1844 if (insn_changed_p)
1845 /* Because we might use DF right after caller-saves sub-pass
1846 we need to keep DF info up to date. */
1847 df_insn_rescan (insn);
1848 }
1849 }
1850
1851 /* Changes pseudos created by function remove_scratches onto scratches. */
1852 static void
1853 restore_scratches (void)
1854 {
1855 int regno;
1856 unsigned i;
1857 sloc_t loc;
1858 rtx_insn *last = NULL;
1859 lra_insn_recog_data_t id = NULL;
1860
1861 for (i = 0; scratches.iterate (i, &loc); i++)
1862 {
1863 if (last != loc->insn)
1864 {
1865 last = loc->insn;
1866 id = lra_get_insn_recog_data (last);
1867 }
1868 if (REG_P (*id->operand_loc[loc->nop])
1869 && ((regno = REGNO (*id->operand_loc[loc->nop]))
1870 >= FIRST_PSEUDO_REGISTER)
1871 && lra_get_regno_hard_regno (regno) < 0)
1872 {
1873 /* It should be only case when scratch register with chosen
1874 constraint 'X' did not get memory or hard register. */
1875 lra_assert (lra_former_scratch_p (regno));
1876 *id->operand_loc[loc->nop]
1877 = gen_rtx_SCRATCH (GET_MODE (*id->operand_loc[loc->nop]));
1878 lra_update_dup (id, loc->nop);
1879 if (lra_dump_file != NULL)
1880 fprintf (lra_dump_file, "Restoring SCRATCH in insn #%u(nop %d)\n",
1881 INSN_UID (loc->insn), loc->nop);
1882 }
1883 }
1884 for (i = 0; scratches.iterate (i, &loc); i++)
1885 free (loc);
1886 scratches.release ();
1887 bitmap_clear (&scratch_bitmap);
1888 bitmap_clear (&scratch_operand_bitmap);
1889 }
1890
1891 \f
1892
1893 #ifdef ENABLE_CHECKING
1894
1895 /* Function checks RTL for correctness. If FINAL_P is true, it is
1896 done at the end of LRA and the check is more rigorous. */
1897 static void
1898 check_rtl (bool final_p)
1899 {
1900 basic_block bb;
1901 rtx_insn *insn;
1902
1903 lra_assert (! final_p || reload_completed);
1904 FOR_EACH_BB_FN (bb, cfun)
1905 FOR_BB_INSNS (bb, insn)
1906 if (NONDEBUG_INSN_P (insn)
1907 && GET_CODE (PATTERN (insn)) != USE
1908 && GET_CODE (PATTERN (insn)) != CLOBBER
1909 && GET_CODE (PATTERN (insn)) != ASM_INPUT)
1910 {
1911 if (final_p)
1912 {
1913 #ifdef ENABLED_CHECKING
1914 extract_constrain_insn (insn);
1915 #endif
1916 continue;
1917 }
1918 /* LRA code is based on assumption that all addresses can be
1919 correctly decomposed. LRA can generate reloads for
1920 decomposable addresses. The decomposition code checks the
1921 correctness of the addresses. So we don't need to check
1922 the addresses here. Don't call insn_invalid_p here, it can
1923 change the code at this stage. */
1924 if (recog_memoized (insn) < 0 && asm_noperands (PATTERN (insn)) < 0)
1925 fatal_insn_not_found (insn);
1926 }
1927 }
1928 #endif /* #ifdef ENABLE_CHECKING */
1929
1930 /* Determine if the current function has an exception receiver block
1931 that reaches the exit block via non-exceptional edges */
1932 static bool
1933 has_nonexceptional_receiver (void)
1934 {
1935 edge e;
1936 edge_iterator ei;
1937 basic_block *tos, *worklist, bb;
1938
1939 /* If we're not optimizing, then just err on the safe side. */
1940 if (!optimize)
1941 return true;
1942
1943 /* First determine which blocks can reach exit via normal paths. */
1944 tos = worklist = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun) + 1);
1945
1946 FOR_EACH_BB_FN (bb, cfun)
1947 bb->flags &= ~BB_REACHABLE;
1948
1949 /* Place the exit block on our worklist. */
1950 EXIT_BLOCK_PTR_FOR_FN (cfun)->flags |= BB_REACHABLE;
1951 *tos++ = EXIT_BLOCK_PTR_FOR_FN (cfun);
1952
1953 /* Iterate: find everything reachable from what we've already seen. */
1954 while (tos != worklist)
1955 {
1956 bb = *--tos;
1957
1958 FOR_EACH_EDGE (e, ei, bb->preds)
1959 if (e->flags & EDGE_ABNORMAL)
1960 {
1961 free (worklist);
1962 return true;
1963 }
1964 else
1965 {
1966 basic_block src = e->src;
1967
1968 if (!(src->flags & BB_REACHABLE))
1969 {
1970 src->flags |= BB_REACHABLE;
1971 *tos++ = src;
1972 }
1973 }
1974 }
1975 free (worklist);
1976 /* No exceptional block reached exit unexceptionally. */
1977 return false;
1978 }
1979
1980 #ifdef AUTO_INC_DEC
1981
1982 /* Process recursively X of INSN and add REG_INC notes if necessary. */
1983 static void
1984 add_auto_inc_notes (rtx_insn *insn, rtx x)
1985 {
1986 enum rtx_code code = GET_CODE (x);
1987 const char *fmt;
1988 int i, j;
1989
1990 if (code == MEM && auto_inc_p (XEXP (x, 0)))
1991 {
1992 add_reg_note (insn, REG_INC, XEXP (XEXP (x, 0), 0));
1993 return;
1994 }
1995
1996 /* Scan all X sub-expressions. */
1997 fmt = GET_RTX_FORMAT (code);
1998 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1999 {
2000 if (fmt[i] == 'e')
2001 add_auto_inc_notes (insn, XEXP (x, i));
2002 else if (fmt[i] == 'E')
2003 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2004 add_auto_inc_notes (insn, XVECEXP (x, i, j));
2005 }
2006 }
2007
2008 #endif
2009
2010 /* Remove all REG_DEAD and REG_UNUSED notes and regenerate REG_INC.
2011 We change pseudos by hard registers without notification of DF and
2012 that can make the notes obsolete. DF-infrastructure does not deal
2013 with REG_INC notes -- so we should regenerate them here. */
2014 static void
2015 update_inc_notes (void)
2016 {
2017 rtx *pnote;
2018 basic_block bb;
2019 rtx_insn *insn;
2020
2021 FOR_EACH_BB_FN (bb, cfun)
2022 FOR_BB_INSNS (bb, insn)
2023 if (NONDEBUG_INSN_P (insn))
2024 {
2025 pnote = &REG_NOTES (insn);
2026 while (*pnote != 0)
2027 {
2028 if (REG_NOTE_KIND (*pnote) == REG_DEAD
2029 || REG_NOTE_KIND (*pnote) == REG_UNUSED
2030 || REG_NOTE_KIND (*pnote) == REG_INC)
2031 *pnote = XEXP (*pnote, 1);
2032 else
2033 pnote = &XEXP (*pnote, 1);
2034 }
2035 #ifdef AUTO_INC_DEC
2036 add_auto_inc_notes (insn, PATTERN (insn));
2037 #endif
2038 }
2039 }
2040
2041 /* Set to 1 while in lra. */
2042 int lra_in_progress;
2043
2044 /* Start of pseudo regnos before the LRA. */
2045 int lra_new_regno_start;
2046
2047 /* Start of reload pseudo regnos before the new spill pass. */
2048 int lra_constraint_new_regno_start;
2049
2050 /* Inheritance pseudo regnos before the new spill pass. */
2051 bitmap_head lra_inheritance_pseudos;
2052
2053 /* Split regnos before the new spill pass. */
2054 bitmap_head lra_split_regs;
2055
2056 /* Reload pseudo regnos before the new assignmnet pass which still can
2057 be spilled after the assinment pass as memory is also accepted in
2058 insns for the reload pseudos. */
2059 bitmap_head lra_optional_reload_pseudos;
2060
2061 /* Pseudo regnos used for subreg reloads before the new assignment
2062 pass. Such pseudos still can be spilled after the assinment
2063 pass. */
2064 bitmap_head lra_subreg_reload_pseudos;
2065
2066 /* First UID of insns generated before a new spill pass. */
2067 int lra_constraint_new_insn_uid_start;
2068
2069 /* File used for output of LRA debug information. */
2070 FILE *lra_dump_file;
2071
2072 /* True if we should try spill into registers of different classes
2073 instead of memory. */
2074 bool lra_reg_spill_p;
2075
2076 /* Set up value LRA_REG_SPILL_P. */
2077 static void
2078 setup_reg_spill_flag (void)
2079 {
2080 int cl, mode;
2081
2082 if (targetm.spill_class != NULL)
2083 for (cl = 0; cl < (int) LIM_REG_CLASSES; cl++)
2084 for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
2085 if (targetm.spill_class ((enum reg_class) cl,
2086 (machine_mode) mode) != NO_REGS)
2087 {
2088 lra_reg_spill_p = true;
2089 return;
2090 }
2091 lra_reg_spill_p = false;
2092 }
2093
2094 /* True if the current function is too big to use regular algorithms
2095 in LRA. In other words, we should use simpler and faster algorithms
2096 in LRA. It also means we should not worry about generation code
2097 for caller saves. The value is set up in IRA. */
2098 bool lra_simple_p;
2099
2100 /* Major LRA entry function. F is a file should be used to dump LRA
2101 debug info. */
2102 void
2103 lra (FILE *f)
2104 {
2105 int i;
2106 bool live_p, scratch_p, inserted_p;
2107
2108 lra_dump_file = f;
2109
2110 timevar_push (TV_LRA);
2111
2112 /* Make sure that the last insn is a note. Some subsequent passes
2113 need it. */
2114 emit_note (NOTE_INSN_DELETED);
2115
2116 COPY_HARD_REG_SET (lra_no_alloc_regs, ira_no_alloc_regs);
2117
2118 init_reg_info ();
2119 expand_reg_info ();
2120
2121 init_insn_recog_data ();
2122
2123 #ifdef ENABLE_CHECKING
2124 /* Some quick check on RTL generated by previous passes. */
2125 check_rtl (false);
2126 #endif
2127
2128 lra_in_progress = 1;
2129
2130 lra_live_range_iter = lra_coalesce_iter = lra_constraint_iter = 0;
2131 lra_assignment_iter = lra_assignment_iter_after_spill = 0;
2132 lra_inheritance_iter = lra_undo_inheritance_iter = 0;
2133
2134 setup_reg_spill_flag ();
2135
2136 /* Function remove_scratches can creates new pseudos for clobbers --
2137 so set up lra_constraint_new_regno_start before its call to
2138 permit changing reg classes for pseudos created by this
2139 simplification. */
2140 lra_constraint_new_regno_start = lra_new_regno_start = max_reg_num ();
2141 remove_scratches ();
2142 scratch_p = lra_constraint_new_regno_start != max_reg_num ();
2143
2144 /* A function that has a non-local label that can reach the exit
2145 block via non-exceptional paths must save all call-saved
2146 registers. */
2147 if (cfun->has_nonlocal_label && has_nonexceptional_receiver ())
2148 crtl->saves_all_registers = 1;
2149
2150 if (crtl->saves_all_registers)
2151 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2152 if (! call_used_regs[i] && ! fixed_regs[i] && ! LOCAL_REGNO (i))
2153 df_set_regs_ever_live (i, true);
2154
2155 /* We don't DF from now and avoid its using because it is to
2156 expensive when a lot of RTL changes are made. */
2157 df_set_flags (DF_NO_INSN_RESCAN);
2158 lra_constraint_insn_stack.create (get_max_uid ());
2159 lra_constraint_insn_stack_bitmap = sbitmap_alloc (get_max_uid ());
2160 bitmap_clear (lra_constraint_insn_stack_bitmap);
2161 lra_live_ranges_init ();
2162 lra_constraints_init ();
2163 lra_curr_reload_num = 0;
2164 push_insns (get_last_insn (), NULL);
2165 /* It is needed for the 1st coalescing. */
2166 lra_constraint_new_insn_uid_start = get_max_uid ();
2167 bitmap_initialize (&lra_inheritance_pseudos, &reg_obstack);
2168 bitmap_initialize (&lra_split_regs, &reg_obstack);
2169 bitmap_initialize (&lra_optional_reload_pseudos, &reg_obstack);
2170 bitmap_initialize (&lra_subreg_reload_pseudos, &reg_obstack);
2171 live_p = false;
2172 if (get_frame_size () != 0 && crtl->stack_alignment_needed)
2173 /* If we have a stack frame, we must align it now. The stack size
2174 may be a part of the offset computation for register
2175 elimination. */
2176 assign_stack_local (BLKmode, 0, crtl->stack_alignment_needed);
2177 lra_init_equiv ();
2178 for (;;)
2179 {
2180 for (;;)
2181 {
2182 /* We should try to assign hard registers to scratches even
2183 if there were no RTL transformations in
2184 lra_constraints. */
2185 if (! lra_constraints (lra_constraint_iter == 0)
2186 && (lra_constraint_iter > 1
2187 || (! scratch_p && ! caller_save_needed)))
2188 break;
2189 /* Constraint transformations may result in that eliminable
2190 hard regs become uneliminable and pseudos which use them
2191 should be spilled. It is better to do it before pseudo
2192 assignments.
2193
2194 For example, rs6000 can make
2195 RS6000_PIC_OFFSET_TABLE_REGNUM uneliminable if we started
2196 to use a constant pool. */
2197 lra_eliminate (false, false);
2198 /* Do inheritance only for regular algorithms. */
2199 if (! lra_simple_p)
2200 {
2201 if (flag_use_caller_save)
2202 {
2203 if (live_p)
2204 lra_clear_live_ranges ();
2205 /* As a side-effect of lra_create_live_ranges, we calculate
2206 actual_call_used_reg_set, which is needed during
2207 lra_inheritance. */
2208 lra_create_live_ranges (true);
2209 }
2210 lra_inheritance ();
2211 }
2212 if (live_p)
2213 lra_clear_live_ranges ();
2214 /* We need live ranges for lra_assign -- so build them. */
2215 lra_create_live_ranges (true);
2216 live_p = true;
2217 /* If we don't spill non-reload and non-inheritance pseudos,
2218 there is no sense to run memory-memory move coalescing.
2219 If inheritance pseudos were spilled, the memory-memory
2220 moves involving them will be removed by pass undoing
2221 inheritance. */
2222 if (lra_simple_p)
2223 lra_assign ();
2224 else
2225 {
2226 bool spill_p = !lra_assign ();
2227
2228 if (lra_undo_inheritance ())
2229 live_p = false;
2230 if (spill_p)
2231 {
2232 if (! live_p)
2233 {
2234 lra_create_live_ranges (true);
2235 live_p = true;
2236 }
2237 if (lra_coalesce ())
2238 live_p = false;
2239 }
2240 if (! live_p)
2241 lra_clear_live_ranges ();
2242 }
2243 }
2244 /* Don't clear optional reloads bitmap until all constraints are
2245 satisfied as we need to differ them from regular reloads. */
2246 bitmap_clear (&lra_optional_reload_pseudos);
2247 bitmap_clear (&lra_subreg_reload_pseudos);
2248 bitmap_clear (&lra_inheritance_pseudos);
2249 bitmap_clear (&lra_split_regs);
2250 if (! lra_need_for_spills_p ())
2251 break;
2252 if (! live_p)
2253 {
2254 /* We need full live info for spilling pseudos into
2255 registers instead of memory. */
2256 lra_create_live_ranges (lra_reg_spill_p);
2257 live_p = true;
2258 }
2259 lra_spill ();
2260 /* Assignment of stack slots changes elimination offsets for
2261 some eliminations. So update the offsets here. */
2262 lra_eliminate (false, false);
2263 lra_constraint_new_regno_start = max_reg_num ();
2264 lra_constraint_new_insn_uid_start = get_max_uid ();
2265 lra_assignment_iter_after_spill = 0;
2266 }
2267 restore_scratches ();
2268 lra_eliminate (true, false);
2269 lra_final_code_change ();
2270 lra_in_progress = 0;
2271 if (live_p)
2272 lra_clear_live_ranges ();
2273 lra_live_ranges_finish ();
2274 lra_constraints_finish ();
2275 finish_reg_info ();
2276 sbitmap_free (lra_constraint_insn_stack_bitmap);
2277 lra_constraint_insn_stack.release ();
2278 finish_insn_recog_data ();
2279 regstat_free_n_sets_and_refs ();
2280 regstat_free_ri ();
2281 reload_completed = 1;
2282 update_inc_notes ();
2283
2284 inserted_p = fixup_abnormal_edges ();
2285
2286 /* We've possibly turned single trapping insn into multiple ones. */
2287 if (cfun->can_throw_non_call_exceptions)
2288 {
2289 sbitmap blocks;
2290 blocks = sbitmap_alloc (last_basic_block_for_fn (cfun));
2291 bitmap_ones (blocks);
2292 find_many_sub_basic_blocks (blocks);
2293 sbitmap_free (blocks);
2294 }
2295
2296 if (inserted_p)
2297 commit_edge_insertions ();
2298
2299 /* Replacing pseudos with their memory equivalents might have
2300 created shared rtx. Subsequent passes would get confused
2301 by this, so unshare everything here. */
2302 unshare_all_rtl_again (get_insns ());
2303
2304 #ifdef ENABLE_CHECKING
2305 check_rtl (true);
2306 #endif
2307
2308 timevar_pop (TV_LRA);
2309 }
2310
2311 /* Called once per compiler to initialize LRA data once. */
2312 void
2313 lra_init_once (void)
2314 {
2315 init_insn_code_data_once ();
2316 }
2317
2318 /* Called once per compiler to finish LRA data which are initialize
2319 once. */
2320 void
2321 lra_finish_once (void)
2322 {
2323 finish_insn_code_data_once ();
2324 }