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