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