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