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