1 /* Code for RTL transformations to satisfy insn constraints.
2    Copyright (C) 2010-2019 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 /* This file contains code for 3 passes: constraint pass,
23    inheritance/split pass, and pass for undoing failed inheritance and
24    split.
25 
26    The major goal of constraint pass is to transform RTL to satisfy
27    insn and address constraints by:
28      o choosing insn alternatives;
29      o generating *reload insns* (or reloads in brief) and *reload
30        pseudos* which will get necessary hard registers later;
31      o substituting pseudos with equivalent values and removing the
32        instructions that initialized those pseudos.
33 
34    The constraint pass has biggest and most complicated code in LRA.
35    There are a lot of important details like:
36      o reuse of input reload pseudos to simplify reload pseudo
37        allocations;
38      o some heuristics to choose insn alternative to improve the
39        inheritance;
40      o early clobbers etc.
41 
42    The pass is mimicking former reload pass in alternative choosing
43    because the reload pass is oriented to current machine description
44    model.  It might be changed if the machine description model is
45    changed.
46 
47    There is special code for preventing all LRA and this pass cycling
48    in case of bugs.
49 
50    On the first iteration of the pass we process every instruction and
51    choose an alternative for each one.  On subsequent iterations we try
52    to avoid reprocessing instructions if we can be sure that the old
53    choice is still valid.
54 
55    The inheritance/spilt pass is to transform code to achieve
56    ineheritance and live range splitting.  It is done on backward
57    traversal of EBBs.
58 
59    The inheritance optimization goal is to reuse values in hard
60    registers. There is analogous optimization in old reload pass.  The
61    inheritance is achieved by following transformation:
62 
63        reload_p1 <- p	     reload_p1 <- p
64        ...		     new_p <- reload_p1
65        ...		=>   ...
66        reload_p2 <- p	     reload_p2 <- new_p
67 
68    where p is spilled and not changed between the insns.  Reload_p1 is
69    also called *original pseudo* and new_p is called *inheritance
70    pseudo*.
71 
72    The subsequent assignment pass will try to assign the same (or
73    another if it is not possible) hard register to new_p as to
74    reload_p1 or reload_p2.
75 
76    If the assignment pass fails to assign a hard register to new_p,
77    this file will undo the inheritance and restore the original code.
78    This is because implementing the above sequence with a spilled
79    new_p would make the code much worse.  The inheritance is done in
80    EBB scope.  The above is just a simplified example to get an idea
81    of the inheritance as the inheritance is also done for non-reload
82    insns.
83 
84    Splitting (transformation) is also done in EBB scope on the same
85    pass as the inheritance:
86 
87        r <- ... or ... <- r		 r <- ... or ... <- r
88        ...				 s <- r (new insn -- save)
89        ...			  =>
90        ...				 r <- s (new insn -- restore)
91        ... <- r				 ... <- r
92 
93     The *split pseudo* s is assigned to the hard register of the
94     original pseudo or hard register r.
95 
96     Splitting is done:
97       o In EBBs with high register pressure for global pseudos (living
98 	in at least 2 BBs) and assigned to hard registers when there
99 	are more one reloads needing the hard registers;
100       o for pseudos needing save/restore code around calls.
101 
102     If the split pseudo still has the same hard register as the
103     original pseudo after the subsequent assignment pass or the
104     original pseudo was split, the opposite transformation is done on
105     the same pass for undoing inheritance.  */
106 
107 #undef REG_OK_STRICT
108 
109 #include "config.h"
110 #include "system.h"
111 #include "coretypes.h"
112 #include "backend.h"
113 #include "target.h"
114 #include "rtl.h"
115 #include "tree.h"
116 #include "predict.h"
117 #include "df.h"
118 #include "memmodel.h"
119 #include "tm_p.h"
120 #include "expmed.h"
121 #include "optabs.h"
122 #include "regs.h"
123 #include "ira.h"
124 #include "recog.h"
125 #include "output.h"
126 #include "addresses.h"
127 #include "expr.h"
128 #include "cfgrtl.h"
129 #include "rtl-error.h"
130 #include "params.h"
131 #include "lra.h"
132 #include "lra-int.h"
133 #include "print-rtl.h"
134 
135 /* Value of LRA_CURR_RELOAD_NUM at the beginning of BB of the current
136    insn.  Remember that LRA_CURR_RELOAD_NUM is the number of emitted
137    reload insns.  */
138 static int bb_reload_num;
139 
140 /* The current insn being processed and corresponding its single set
141    (NULL otherwise), its data (basic block, the insn data, the insn
142    static data, and the mode of each operand).  */
143 static rtx_insn *curr_insn;
144 static rtx curr_insn_set;
145 static basic_block curr_bb;
146 static lra_insn_recog_data_t curr_id;
147 static struct lra_static_insn_data *curr_static_id;
148 static machine_mode curr_operand_mode[MAX_RECOG_OPERANDS];
149 /* Mode of the register substituted by its equivalence with VOIDmode
150    (e.g. constant) and whose subreg is given operand of the current
151    insn.  VOIDmode in all other cases.  */
152 static machine_mode original_subreg_reg_mode[MAX_RECOG_OPERANDS];
153 
154 
155 
156 /* Start numbers for new registers and insns at the current constraints
157    pass start.	*/
158 static int new_regno_start;
159 static int new_insn_uid_start;
160 
161 /* If LOC is nonnull, strip any outer subreg from it.  */
162 static inline rtx *
strip_subreg(rtx * loc)163 strip_subreg (rtx *loc)
164 {
165   return loc && GET_CODE (*loc) == SUBREG ? &SUBREG_REG (*loc) : loc;
166 }
167 
168 /* Return hard regno of REGNO or if it is was not assigned to a hard
169    register, use a hard register from its allocno class.  */
170 static int
get_try_hard_regno(int regno)171 get_try_hard_regno (int regno)
172 {
173   int hard_regno;
174   enum reg_class rclass;
175 
176   if ((hard_regno = regno) >= FIRST_PSEUDO_REGISTER)
177     hard_regno = lra_get_regno_hard_regno (regno);
178   if (hard_regno >= 0)
179     return hard_regno;
180   rclass = lra_get_allocno_class (regno);
181   if (rclass == NO_REGS)
182     return -1;
183   return ira_class_hard_regs[rclass][0];
184 }
185 
186 /* Return the hard regno of X after removing its subreg.  If X is not
187    a register or a subreg of a register, return -1.  If X is a pseudo,
188    use its assignment.  If FINAL_P return the final hard regno which will
189    be after elimination.  */
190 static int
get_hard_regno(rtx x,bool final_p)191 get_hard_regno (rtx x, bool final_p)
192 {
193   rtx reg;
194   int hard_regno;
195 
196   reg = x;
197   if (SUBREG_P (x))
198     reg = SUBREG_REG (x);
199   if (! REG_P (reg))
200     return -1;
201   if (! HARD_REGISTER_NUM_P (hard_regno = REGNO (reg)))
202     hard_regno = lra_get_regno_hard_regno (hard_regno);
203   if (hard_regno < 0)
204     return -1;
205   if (final_p)
206     hard_regno = lra_get_elimination_hard_regno (hard_regno);
207   if (SUBREG_P (x))
208     hard_regno += subreg_regno_offset (hard_regno, GET_MODE (reg),
209 				       SUBREG_BYTE (x),  GET_MODE (x));
210   return hard_regno;
211 }
212 
213 /* If REGNO is a hard register or has been allocated a hard register,
214    return the class of that register.  If REGNO is a reload pseudo
215    created by the current constraints pass, return its allocno class.
216    Return NO_REGS otherwise.  */
217 static enum reg_class
get_reg_class(int regno)218 get_reg_class (int regno)
219 {
220   int hard_regno;
221 
222   if (! HARD_REGISTER_NUM_P (hard_regno = regno))
223     hard_regno = lra_get_regno_hard_regno (regno);
224   if (hard_regno >= 0)
225     {
226       hard_regno = lra_get_elimination_hard_regno (hard_regno);
227       return REGNO_REG_CLASS (hard_regno);
228     }
229   if (regno >= new_regno_start)
230     return lra_get_allocno_class (regno);
231   return NO_REGS;
232 }
233 
234 /* Return true if REG satisfies (or will satisfy) reg class constraint
235    CL.  Use elimination first if REG is a hard register.  If REG is a
236    reload pseudo created by this constraints pass, assume that it will
237    be allocated a hard register from its allocno class, but allow that
238    class to be narrowed to CL if it is currently a superset of CL and
239    if either:
240 
241    - ALLOW_ALL_RELOAD_CLASS_CHANGES_P is true or
242    - the instruction we're processing is not a reload move.
243 
244    If NEW_CLASS is nonnull, set *NEW_CLASS to the new allocno class of
245    REGNO (reg), or NO_REGS if no change in its class was needed.  */
246 static bool
247 in_class_p (rtx reg, enum reg_class cl, enum reg_class *new_class,
248 	    bool allow_all_reload_class_changes_p = false)
249 {
250   enum reg_class rclass, common_class;
251   machine_mode reg_mode;
252   int class_size, hard_regno, nregs, i, j;
253   int regno = REGNO (reg);
254 
255   if (new_class != NULL)
256     *new_class = NO_REGS;
257   if (regno < FIRST_PSEUDO_REGISTER)
258     {
259       rtx final_reg = reg;
260       rtx *final_loc = &final_reg;
261 
262       lra_eliminate_reg_if_possible (final_loc);
263       return TEST_HARD_REG_BIT (reg_class_contents[cl], REGNO (*final_loc));
264     }
265   reg_mode = GET_MODE (reg);
266   rclass = get_reg_class (regno);
267   if (regno < new_regno_start
268       /* Do not allow the constraints for reload instructions to
269 	 influence the classes of new pseudos.  These reloads are
270 	 typically moves that have many alternatives, and restricting
271 	 reload pseudos for one alternative may lead to situations
272 	 where other reload pseudos are no longer allocatable.  */
273       || (!allow_all_reload_class_changes_p
274 	  && INSN_UID (curr_insn) >= new_insn_uid_start
275 	  && curr_insn_set != NULL
276 	  && ((OBJECT_P (SET_SRC (curr_insn_set))
277 	       && ! CONSTANT_P (SET_SRC (curr_insn_set)))
278 	      || (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
279 		  && OBJECT_P (SUBREG_REG (SET_SRC (curr_insn_set)))
280 		  && ! CONSTANT_P (SUBREG_REG (SET_SRC (curr_insn_set)))))))
281     /* When we don't know what class will be used finally for reload
282        pseudos, we use ALL_REGS.  */
283     return ((regno >= new_regno_start && rclass == ALL_REGS)
284 	    || (rclass != NO_REGS && ira_class_subset_p[rclass][cl]
285 		&& ! hard_reg_set_subset_p (reg_class_contents[cl],
286 					    lra_no_alloc_regs)));
287   else
288     {
289       common_class = ira_reg_class_subset[rclass][cl];
290       if (new_class != NULL)
291 	*new_class = common_class;
292       if (hard_reg_set_subset_p (reg_class_contents[common_class],
293 				 lra_no_alloc_regs))
294 	return false;
295       /* Check that there are enough allocatable regs.  */
296       class_size = ira_class_hard_regs_num[common_class];
297       for (i = 0; i < class_size; i++)
298 	{
299 	  hard_regno = ira_class_hard_regs[common_class][i];
300 	  nregs = hard_regno_nregs (hard_regno, reg_mode);
301 	  if (nregs == 1)
302 	    return true;
303 	  for (j = 0; j < nregs; j++)
304 	    if (TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno + j)
305 		|| ! TEST_HARD_REG_BIT (reg_class_contents[common_class],
306 					hard_regno + j))
307 	      break;
308 	  if (j >= nregs)
309 	    return true;
310 	}
311       return false;
312     }
313 }
314 
315 /* Return true if REGNO satisfies a memory constraint.	*/
316 static bool
in_mem_p(int regno)317 in_mem_p (int regno)
318 {
319   return get_reg_class (regno) == NO_REGS;
320 }
321 
322 /* Return 1 if ADDR is a valid memory address for mode MODE in address
323    space AS, and check that each pseudo has the proper kind of hard
324    reg.	 */
325 static int
valid_address_p(machine_mode mode ATTRIBUTE_UNUSED,rtx addr,addr_space_t as)326 valid_address_p (machine_mode mode ATTRIBUTE_UNUSED,
327 		 rtx addr, addr_space_t as)
328 {
329 #ifdef GO_IF_LEGITIMATE_ADDRESS
330   lra_assert (ADDR_SPACE_GENERIC_P (as));
331   GO_IF_LEGITIMATE_ADDRESS (mode, addr, win);
332   return 0;
333 
334  win:
335   return 1;
336 #else
337   return targetm.addr_space.legitimate_address_p (mode, addr, 0, as);
338 #endif
339 }
340 
341 namespace {
342   /* Temporarily eliminates registers in an address (for the lifetime of
343      the object).  */
344   class address_eliminator {
345   public:
346     address_eliminator (struct address_info *ad);
347     ~address_eliminator ();
348 
349   private:
350     struct address_info *m_ad;
351     rtx *m_base_loc;
352     rtx m_base_reg;
353     rtx *m_index_loc;
354     rtx m_index_reg;
355   };
356 }
357 
address_eliminator(struct address_info * ad)358 address_eliminator::address_eliminator (struct address_info *ad)
359   : m_ad (ad),
360     m_base_loc (strip_subreg (ad->base_term)),
361     m_base_reg (NULL_RTX),
362     m_index_loc (strip_subreg (ad->index_term)),
363     m_index_reg (NULL_RTX)
364 {
365   if (m_base_loc != NULL)
366     {
367       m_base_reg = *m_base_loc;
368       /* If we have non-legitimate address which is decomposed not in
369 	 the way we expected, don't do elimination here.  In such case
370 	 the address will be reloaded and elimination will be done in
371 	 reload insn finally.  */
372       if (REG_P (m_base_reg))
373 	lra_eliminate_reg_if_possible (m_base_loc);
374       if (m_ad->base_term2 != NULL)
375 	*m_ad->base_term2 = *m_ad->base_term;
376     }
377   if (m_index_loc != NULL)
378     {
379       m_index_reg = *m_index_loc;
380       if (REG_P (m_index_reg))
381 	lra_eliminate_reg_if_possible (m_index_loc);
382     }
383 }
384 
~address_eliminator()385 address_eliminator::~address_eliminator ()
386 {
387   if (m_base_loc && *m_base_loc != m_base_reg)
388     {
389       *m_base_loc = m_base_reg;
390       if (m_ad->base_term2 != NULL)
391 	*m_ad->base_term2 = *m_ad->base_term;
392     }
393   if (m_index_loc && *m_index_loc != m_index_reg)
394     *m_index_loc = m_index_reg;
395 }
396 
397 /* Return true if the eliminated form of AD is a legitimate target address.  */
398 static bool
valid_address_p(struct address_info * ad)399 valid_address_p (struct address_info *ad)
400 {
401   address_eliminator eliminator (ad);
402   return valid_address_p (ad->mode, *ad->outer, ad->as);
403 }
404 
405 /* Return true if the eliminated form of memory reference OP satisfies
406    extra (special) memory constraint CONSTRAINT.  */
407 static bool
satisfies_memory_constraint_p(rtx op,enum constraint_num constraint)408 satisfies_memory_constraint_p (rtx op, enum constraint_num constraint)
409 {
410   struct address_info ad;
411 
412   decompose_mem_address (&ad, op);
413   address_eliminator eliminator (&ad);
414   return constraint_satisfied_p (op, constraint);
415 }
416 
417 /* Return true if the eliminated form of address AD satisfies extra
418    address constraint CONSTRAINT.  */
419 static bool
satisfies_address_constraint_p(struct address_info * ad,enum constraint_num constraint)420 satisfies_address_constraint_p (struct address_info *ad,
421 				enum constraint_num constraint)
422 {
423   address_eliminator eliminator (ad);
424   return constraint_satisfied_p (*ad->outer, constraint);
425 }
426 
427 /* Return true if the eliminated form of address OP satisfies extra
428    address constraint CONSTRAINT.  */
429 static bool
satisfies_address_constraint_p(rtx op,enum constraint_num constraint)430 satisfies_address_constraint_p (rtx op, enum constraint_num constraint)
431 {
432   struct address_info ad;
433 
434   decompose_lea_address (&ad, &op);
435   return satisfies_address_constraint_p (&ad, constraint);
436 }
437 
438 /* Initiate equivalences for LRA.  As we keep original equivalences
439    before any elimination, we need to make copies otherwise any change
440    in insns might change the equivalences.  */
441 void
lra_init_equiv(void)442 lra_init_equiv (void)
443 {
444   ira_expand_reg_equiv ();
445   for (int i = FIRST_PSEUDO_REGISTER; i < max_reg_num (); i++)
446     {
447       rtx res;
448 
449       if ((res = ira_reg_equiv[i].memory) != NULL_RTX)
450 	ira_reg_equiv[i].memory = copy_rtx (res);
451       if ((res = ira_reg_equiv[i].invariant) != NULL_RTX)
452 	ira_reg_equiv[i].invariant = copy_rtx (res);
453     }
454 }
455 
456 static rtx loc_equivalence_callback (rtx, const_rtx, void *);
457 
458 /* Update equivalence for REGNO.  We need to this as the equivalence
459    might contain other pseudos which are changed by their
460    equivalences.  */
461 static void
update_equiv(int regno)462 update_equiv (int regno)
463 {
464   rtx x;
465 
466   if ((x = ira_reg_equiv[regno].memory) != NULL_RTX)
467     ira_reg_equiv[regno].memory
468       = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
469 				 NULL_RTX);
470   if ((x = ira_reg_equiv[regno].invariant) != NULL_RTX)
471     ira_reg_equiv[regno].invariant
472       = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
473 				 NULL_RTX);
474 }
475 
476 /* If we have decided to substitute X with another value, return that
477    value, otherwise return X.  */
478 static rtx
get_equiv(rtx x)479 get_equiv (rtx x)
480 {
481   int regno;
482   rtx res;
483 
484   if (! REG_P (x) || (regno = REGNO (x)) < FIRST_PSEUDO_REGISTER
485       || ! ira_reg_equiv[regno].defined_p
486       || ! ira_reg_equiv[regno].profitable_p
487       || lra_get_regno_hard_regno (regno) >= 0)
488     return x;
489   if ((res = ira_reg_equiv[regno].memory) != NULL_RTX)
490     {
491       if (targetm.cannot_substitute_mem_equiv_p (res))
492 	return x;
493       return res;
494     }
495   if ((res = ira_reg_equiv[regno].constant) != NULL_RTX)
496     return res;
497   if ((res = ira_reg_equiv[regno].invariant) != NULL_RTX)
498     return res;
499   gcc_unreachable ();
500 }
501 
502 /* If we have decided to substitute X with the equivalent value,
503    return that value after elimination for INSN, otherwise return
504    X.  */
505 static rtx
get_equiv_with_elimination(rtx x,rtx_insn * insn)506 get_equiv_with_elimination (rtx x, rtx_insn *insn)
507 {
508   rtx res = get_equiv (x);
509 
510   if (x == res || CONSTANT_P (res))
511     return res;
512   return lra_eliminate_regs_1 (insn, res, GET_MODE (res),
513 			       false, false, 0, true);
514 }
515 
516 /* Set up curr_operand_mode.  */
517 static void
init_curr_operand_mode(void)518 init_curr_operand_mode (void)
519 {
520   int nop = curr_static_id->n_operands;
521   for (int i = 0; i < nop; i++)
522     {
523       machine_mode mode = GET_MODE (*curr_id->operand_loc[i]);
524       if (mode == VOIDmode)
525 	{
526 	  /* The .md mode for address operands is the mode of the
527 	     addressed value rather than the mode of the address itself.  */
528 	  if (curr_id->icode >= 0 && curr_static_id->operand[i].is_address)
529 	    mode = Pmode;
530 	  else
531 	    mode = curr_static_id->operand[i].mode;
532 	}
533       curr_operand_mode[i] = mode;
534     }
535 }
536 
537 
538 
539 /* The page contains code to reuse input reloads.  */
540 
541 /* Structure describes input reload of the current insns.  */
542 struct input_reload
543 {
544   /* True for input reload of matched operands.  */
545   bool match_p;
546   /* Reloaded value.  */
547   rtx input;
548   /* Reload pseudo used.  */
549   rtx reg;
550 };
551 
552 /* The number of elements in the following array.  */
553 static int curr_insn_input_reloads_num;
554 /* Array containing info about input reloads.  It is used to find the
555    same input reload and reuse the reload pseudo in this case.	*/
556 static struct input_reload curr_insn_input_reloads[LRA_MAX_INSN_RELOADS];
557 
558 /* Initiate data concerning reuse of input reloads for the current
559    insn.  */
560 static void
init_curr_insn_input_reloads(void)561 init_curr_insn_input_reloads (void)
562 {
563   curr_insn_input_reloads_num = 0;
564 }
565 
566 /* Create a new pseudo using MODE, RCLASS, ORIGINAL or reuse an existing
567    reload pseudo.  Don't reuse an existing reload pseudo if IN_SUBREG_P
568    is true and the reused pseudo should be wrapped up in a SUBREG.
569    The result pseudo is returned through RESULT_REG.  Return TRUE if we
570    created a new pseudo, FALSE if we reused an existing reload pseudo.
571    Use TITLE to describe new registers for debug purposes.  */
572 static bool
get_reload_reg(enum op_type type,machine_mode mode,rtx original,enum reg_class rclass,bool in_subreg_p,const char * title,rtx * result_reg)573 get_reload_reg (enum op_type type, machine_mode mode, rtx original,
574 		enum reg_class rclass, bool in_subreg_p,
575 		const char *title, rtx *result_reg)
576 {
577   int i, regno;
578   enum reg_class new_class;
579   bool unique_p = false;
580 
581   if (type == OP_OUT)
582     {
583       /* Output reload registers tend to start out with a conservative
584 	 choice of register class.  Usually this is ALL_REGS, although
585 	 a target might narrow it (for performance reasons) through
586 	 targetm.preferred_reload_class.  It's therefore quite common
587 	 for a reload instruction to require a more restrictive class
588 	 than the class that was originally assigned to the reload register.
589 
590 	 In these situations, it's more efficient to refine the choice
591 	 of register class rather than create a second reload register.
592 	 This also helps to avoid cycling for registers that are only
593 	 used by reload instructions.  */
594       rtx src = curr_insn_set != NULL ? SET_SRC (curr_insn_set) : NULL;
595       if (REG_P (original)
596 	  && (int) REGNO (original) >= new_regno_start
597 	  && INSN_UID (curr_insn) >= new_insn_uid_start
598 	  && in_class_p (original, rclass, &new_class, true)
599 	  && src != NULL
600 	  && ((REG_P (src) || MEM_P (src))
601 	      || (GET_CODE (src) == SUBREG
602 		  && (REG_P (SUBREG_REG (src)) || MEM_P (SUBREG_REG (src))))))
603 	{
604 	  unsigned int regno = REGNO (original);
605 	  if (lra_dump_file != NULL)
606 	    {
607 	      fprintf (lra_dump_file, "	 Reuse r%d for output ", regno);
608 	      dump_value_slim (lra_dump_file, original, 1);
609 	    }
610 	  if (new_class != lra_get_allocno_class (regno))
611 	    lra_change_class (regno, new_class, ", change to", false);
612 	  if (lra_dump_file != NULL)
613 	    fprintf (lra_dump_file, "\n");
614 	  *result_reg = original;
615 	  return false;
616 	}
617       *result_reg
618 	= lra_create_new_reg_with_unique_value (mode, original, rclass, title);
619       return true;
620     }
621   /* Prevent reuse value of expression with side effects,
622      e.g. volatile memory.  */
623   if (! side_effects_p (original))
624     for (i = 0; i < curr_insn_input_reloads_num; i++)
625       {
626 	if (! curr_insn_input_reloads[i].match_p
627 	    && rtx_equal_p (curr_insn_input_reloads[i].input, original)
628 	    && in_class_p (curr_insn_input_reloads[i].reg, rclass, &new_class))
629 	  {
630 	    rtx reg = curr_insn_input_reloads[i].reg;
631 	    regno = REGNO (reg);
632 	    /* If input is equal to original and both are VOIDmode,
633 	       GET_MODE (reg) might be still different from mode.
634 	       Ensure we don't return *result_reg with wrong mode.  */
635 	    if (GET_MODE (reg) != mode)
636 	      {
637 		if (in_subreg_p)
638 		  continue;
639 		if (maybe_lt (GET_MODE_SIZE (GET_MODE (reg)),
640 			      GET_MODE_SIZE (mode)))
641 		  continue;
642 		reg = lowpart_subreg (mode, reg, GET_MODE (reg));
643 		if (reg == NULL_RTX || GET_CODE (reg) != SUBREG)
644 		  continue;
645 	      }
646 	    *result_reg = reg;
647 	    if (lra_dump_file != NULL)
648 	      {
649 		fprintf (lra_dump_file, "	 Reuse r%d for reload ", regno);
650 		dump_value_slim (lra_dump_file, original, 1);
651 	      }
652 	    if (new_class != lra_get_allocno_class (regno))
653 	      lra_change_class (regno, new_class, ", change to", false);
654 	    if (lra_dump_file != NULL)
655 	      fprintf (lra_dump_file, "\n");
656 	    return false;
657 	  }
658 	/* If we have an input reload with a different mode, make sure it
659 	   will get a different hard reg.  */
660 	else if (REG_P (original)
661 		 && REG_P (curr_insn_input_reloads[i].input)
662 		 && REGNO (original) == REGNO (curr_insn_input_reloads[i].input)
663 		 && (GET_MODE (original)
664 		     != GET_MODE (curr_insn_input_reloads[i].input)))
665 	  unique_p = true;
666       }
667   *result_reg = (unique_p
668 		 ? lra_create_new_reg_with_unique_value
669 		 : lra_create_new_reg) (mode, original, rclass, title);
670   lra_assert (curr_insn_input_reloads_num < LRA_MAX_INSN_RELOADS);
671   curr_insn_input_reloads[curr_insn_input_reloads_num].input = original;
672   curr_insn_input_reloads[curr_insn_input_reloads_num].match_p = false;
673   curr_insn_input_reloads[curr_insn_input_reloads_num++].reg = *result_reg;
674   return true;
675 }
676 
677 
678 /* The page contains major code to choose the current insn alternative
679    and generate reloads for it.	 */
680 
681 /* Return the offset from REGNO of the least significant register
682    in (reg:MODE REGNO).
683 
684    This function is used to tell whether two registers satisfy
685    a matching constraint.  (reg:MODE1 REGNO1) matches (reg:MODE2 REGNO2) if:
686 
687          REGNO1 + lra_constraint_offset (REGNO1, MODE1)
688 	 == REGNO2 + lra_constraint_offset (REGNO2, MODE2)  */
689 int
lra_constraint_offset(int regno,machine_mode mode)690 lra_constraint_offset (int regno, machine_mode mode)
691 {
692   lra_assert (regno < FIRST_PSEUDO_REGISTER);
693 
694   scalar_int_mode int_mode;
695   if (WORDS_BIG_ENDIAN
696       && is_a <scalar_int_mode> (mode, &int_mode)
697       && GET_MODE_SIZE (int_mode) > UNITS_PER_WORD)
698     return hard_regno_nregs (regno, mode) - 1;
699   return 0;
700 }
701 
702 /* Like rtx_equal_p except that it allows a REG and a SUBREG to match
703    if they are the same hard reg, and has special hacks for
704    auto-increment and auto-decrement.  This is specifically intended for
705    process_alt_operands to use in determining whether two operands
706    match.  X is the operand whose number is the lower of the two.
707 
708    It is supposed that X is the output operand and Y is the input
709    operand.  Y_HARD_REGNO is the final hard regno of register Y or
710    register in subreg Y as we know it now.  Otherwise, it is a
711    negative value.  */
712 static bool
operands_match_p(rtx x,rtx y,int y_hard_regno)713 operands_match_p (rtx x, rtx y, int y_hard_regno)
714 {
715   int i;
716   RTX_CODE code = GET_CODE (x);
717   const char *fmt;
718 
719   if (x == y)
720     return true;
721   if ((code == REG || (code == SUBREG && REG_P (SUBREG_REG (x))))
722       && (REG_P (y) || (GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y)))))
723     {
724       int j;
725 
726       i = get_hard_regno (x, false);
727       if (i < 0)
728 	goto slow;
729 
730       if ((j = y_hard_regno) < 0)
731 	goto slow;
732 
733       i += lra_constraint_offset (i, GET_MODE (x));
734       j += lra_constraint_offset (j, GET_MODE (y));
735 
736       return i == j;
737     }
738 
739   /* If two operands must match, because they are really a single
740      operand of an assembler insn, then two post-increments are invalid
741      because the assembler insn would increment only once.  On the
742      other hand, a post-increment matches ordinary indexing if the
743      post-increment is the output operand.  */
744   if (code == POST_DEC || code == POST_INC || code == POST_MODIFY)
745     return operands_match_p (XEXP (x, 0), y, y_hard_regno);
746 
747   /* Two pre-increments are invalid because the assembler insn would
748      increment only once.  On the other hand, a pre-increment matches
749      ordinary indexing if the pre-increment is the input operand.  */
750   if (GET_CODE (y) == PRE_DEC || GET_CODE (y) == PRE_INC
751       || GET_CODE (y) == PRE_MODIFY)
752     return operands_match_p (x, XEXP (y, 0), -1);
753 
754  slow:
755 
756   if (code == REG && REG_P (y))
757     return REGNO (x) == REGNO (y);
758 
759   if (code == REG && GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y))
760       && x == SUBREG_REG (y))
761     return true;
762   if (GET_CODE (y) == REG && code == SUBREG && REG_P (SUBREG_REG (x))
763       && SUBREG_REG (x) == y)
764     return true;
765 
766   /* Now we have disposed of all the cases in which different rtx
767      codes can match.  */
768   if (code != GET_CODE (y))
769     return false;
770 
771   /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.  */
772   if (GET_MODE (x) != GET_MODE (y))
773     return false;
774 
775   switch (code)
776     {
777     CASE_CONST_UNIQUE:
778       return false;
779 
780     case CONST_VECTOR:
781       if (!same_vector_encodings_p (x, y))
782 	return false;
783       break;
784 
785     case LABEL_REF:
786       return label_ref_label (x) == label_ref_label (y);
787     case SYMBOL_REF:
788       return XSTR (x, 0) == XSTR (y, 0);
789 
790     default:
791       break;
792     }
793 
794   /* Compare the elements.  If any pair of corresponding elements fail
795      to match, return false for the whole things.  */
796 
797   fmt = GET_RTX_FORMAT (code);
798   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
799     {
800       int val, j;
801       switch (fmt[i])
802 	{
803 	case 'w':
804 	  if (XWINT (x, i) != XWINT (y, i))
805 	    return false;
806 	  break;
807 
808 	case 'i':
809 	  if (XINT (x, i) != XINT (y, i))
810 	    return false;
811 	  break;
812 
813 	case 'p':
814 	  if (maybe_ne (SUBREG_BYTE (x), SUBREG_BYTE (y)))
815 	    return false;
816 	  break;
817 
818 	case 'e':
819 	  val = operands_match_p (XEXP (x, i), XEXP (y, i), -1);
820 	  if (val == 0)
821 	    return false;
822 	  break;
823 
824 	case '0':
825 	  break;
826 
827 	case 'E':
828 	  if (XVECLEN (x, i) != XVECLEN (y, i))
829 	    return false;
830 	  for (j = XVECLEN (x, i) - 1; j >= 0; --j)
831 	    {
832 	      val = operands_match_p (XVECEXP (x, i, j), XVECEXP (y, i, j), -1);
833 	      if (val == 0)
834 		return false;
835 	    }
836 	  break;
837 
838 	  /* It is believed that rtx's at this level will never
839 	     contain anything but integers and other rtx's, except for
840 	     within LABEL_REFs and SYMBOL_REFs.	 */
841 	default:
842 	  gcc_unreachable ();
843 	}
844     }
845   return true;
846 }
847 
848 /* True if X is a constant that can be forced into the constant pool.
849    MODE is the mode of the operand, or VOIDmode if not known.  */
850 #define CONST_POOL_OK_P(MODE, X)		\
851   ((MODE) != VOIDmode				\
852    && CONSTANT_P (X)				\
853    && GET_CODE (X) != HIGH			\
854    && GET_MODE_SIZE (MODE).is_constant ()	\
855    && !targetm.cannot_force_const_mem (MODE, X))
856 
857 /* True if C is a non-empty register class that has too few registers
858    to be safely used as a reload target class.	*/
859 #define SMALL_REGISTER_CLASS_P(C)		\
860   (ira_class_hard_regs_num [(C)] == 1		\
861    || (ira_class_hard_regs_num [(C)] >= 1	\
862        && targetm.class_likely_spilled_p (C)))
863 
864 /* If REG is a reload pseudo, try to make its class satisfying CL.  */
865 static void
narrow_reload_pseudo_class(rtx reg,enum reg_class cl)866 narrow_reload_pseudo_class (rtx reg, enum reg_class cl)
867 {
868   enum reg_class rclass;
869 
870   /* Do not make more accurate class from reloads generated.  They are
871      mostly moves with a lot of constraints.  Making more accurate
872      class may results in very narrow class and impossibility of find
873      registers for several reloads of one insn.	 */
874   if (INSN_UID (curr_insn) >= new_insn_uid_start)
875     return;
876   if (GET_CODE (reg) == SUBREG)
877     reg = SUBREG_REG (reg);
878   if (! REG_P (reg) || (int) REGNO (reg) < new_regno_start)
879     return;
880   if (in_class_p (reg, cl, &rclass) && rclass != cl)
881     lra_change_class (REGNO (reg), rclass, "      Change to", true);
882 }
883 
884 /* Searches X for any reference to a reg with the same value as REGNO,
885    returning the rtx of the reference found if any.  Otherwise,
886    returns NULL_RTX.  */
887 static rtx
regno_val_use_in(unsigned int regno,rtx x)888 regno_val_use_in (unsigned int regno, rtx x)
889 {
890   const char *fmt;
891   int i, j;
892   rtx tem;
893 
894   if (REG_P (x) && lra_reg_info[REGNO (x)].val == lra_reg_info[regno].val)
895     return x;
896 
897   fmt = GET_RTX_FORMAT (GET_CODE (x));
898   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
899     {
900       if (fmt[i] == 'e')
901 	{
902 	  if ((tem = regno_val_use_in (regno, XEXP (x, i))))
903 	    return tem;
904 	}
905       else if (fmt[i] == 'E')
906 	for (j = XVECLEN (x, i) - 1; j >= 0; j--)
907 	  if ((tem = regno_val_use_in (regno , XVECEXP (x, i, j))))
908 	    return tem;
909     }
910 
911   return NULL_RTX;
912 }
913 
914 /* Return true if all current insn non-output operands except INS (it
915    has a negaitve end marker) do not use pseudos with the same value
916    as REGNO.  */
917 static bool
check_conflict_input_operands(int regno,signed char * ins)918 check_conflict_input_operands (int regno, signed char *ins)
919 {
920   int in;
921   int n_operands = curr_static_id->n_operands;
922 
923   for (int nop = 0; nop < n_operands; nop++)
924     if (! curr_static_id->operand[nop].is_operator
925 	&& curr_static_id->operand[nop].type != OP_OUT)
926       {
927 	for (int i = 0; (in = ins[i]) >= 0; i++)
928 	  if (in == nop)
929 	    break;
930 	if (in < 0
931 	    && regno_val_use_in (regno, *curr_id->operand_loc[nop]) != NULL_RTX)
932 	  return false;
933       }
934   return true;
935 }
936 
937 /* Generate reloads for matching OUT and INS (array of input operand
938    numbers with end marker -1) with reg class GOAL_CLASS, considering
939    output operands OUTS (similar array to INS) needing to be in different
940    registers.  Add input and output reloads correspondingly to the lists
941    *BEFORE and *AFTER.  OUT might be negative.  In this case we generate
942    input reloads for matched input operands INS.  EARLY_CLOBBER_P is a flag
943    that the output operand is early clobbered for chosen alternative.  */
944 static void
match_reload(signed char out,signed char * ins,signed char * outs,enum reg_class goal_class,rtx_insn ** before,rtx_insn ** after,bool early_clobber_p)945 match_reload (signed char out, signed char *ins, signed char *outs,
946 	      enum reg_class goal_class, rtx_insn **before,
947 	      rtx_insn **after, bool early_clobber_p)
948 {
949   bool out_conflict;
950   int i, in;
951   rtx new_in_reg, new_out_reg, reg;
952   machine_mode inmode, outmode;
953   rtx in_rtx = *curr_id->operand_loc[ins[0]];
954   rtx out_rtx = out < 0 ? in_rtx : *curr_id->operand_loc[out];
955 
956   inmode = curr_operand_mode[ins[0]];
957   outmode = out < 0 ? inmode : curr_operand_mode[out];
958   push_to_sequence (*before);
959   if (inmode != outmode)
960     {
961       /* process_alt_operands has already checked that the mode sizes
962 	 are ordered.  */
963       if (partial_subreg_p (outmode, inmode))
964 	{
965 	  reg = new_in_reg
966 	    = lra_create_new_reg_with_unique_value (inmode, in_rtx,
967 						    goal_class, "");
968 	  new_out_reg = gen_lowpart_SUBREG (outmode, reg);
969 	  LRA_SUBREG_P (new_out_reg) = 1;
970 	  /* If the input reg is dying here, we can use the same hard
971 	     register for REG and IN_RTX.  We do it only for original
972 	     pseudos as reload pseudos can die although original
973 	     pseudos still live where reload pseudos dies.  */
974 	  if (REG_P (in_rtx) && (int) REGNO (in_rtx) < lra_new_regno_start
975 	      && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx))
976 	      && (!early_clobber_p
977 		  || check_conflict_input_operands(REGNO (in_rtx), ins)))
978 	    lra_assign_reg_val (REGNO (in_rtx), REGNO (reg));
979 	}
980       else
981 	{
982 	  reg = new_out_reg
983 	    = lra_create_new_reg_with_unique_value (outmode, out_rtx,
984 						    goal_class, "");
985 	  new_in_reg = gen_lowpart_SUBREG (inmode, reg);
986 	  /* NEW_IN_REG is non-paradoxical subreg.  We don't want
987 	     NEW_OUT_REG living above.  We add clobber clause for
988 	     this.  This is just a temporary clobber.  We can remove
989 	     it at the end of LRA work.  */
990 	  rtx_insn *clobber = emit_clobber (new_out_reg);
991 	  LRA_TEMP_CLOBBER_P (PATTERN (clobber)) = 1;
992 	  LRA_SUBREG_P (new_in_reg) = 1;
993 	  if (GET_CODE (in_rtx) == SUBREG)
994 	    {
995 	      rtx subreg_reg = SUBREG_REG (in_rtx);
996 
997 	      /* If SUBREG_REG is dying here and sub-registers IN_RTX
998 		 and NEW_IN_REG are similar, we can use the same hard
999 		 register for REG and SUBREG_REG.  */
1000 	      if (REG_P (subreg_reg)
1001 		  && (int) REGNO (subreg_reg) < lra_new_regno_start
1002 		  && GET_MODE (subreg_reg) == outmode
1003 		  && known_eq (SUBREG_BYTE (in_rtx), SUBREG_BYTE (new_in_reg))
1004 		  && find_regno_note (curr_insn, REG_DEAD, REGNO (subreg_reg))
1005 		  && (! early_clobber_p
1006 		      || check_conflict_input_operands (REGNO (subreg_reg),
1007 							ins)))
1008 		lra_assign_reg_val (REGNO (subreg_reg), REGNO (reg));
1009 	    }
1010 	}
1011     }
1012   else
1013     {
1014       /* Pseudos have values -- see comments for lra_reg_info.
1015 	 Different pseudos with the same value do not conflict even if
1016 	 they live in the same place.  When we create a pseudo we
1017 	 assign value of original pseudo (if any) from which we
1018 	 created the new pseudo.  If we create the pseudo from the
1019 	 input pseudo, the new pseudo will have no conflict with the
1020 	 input pseudo which is wrong when the input pseudo lives after
1021 	 the insn and as the new pseudo value is changed by the insn
1022 	 output.  Therefore we create the new pseudo from the output
1023 	 except the case when we have single matched dying input
1024 	 pseudo.
1025 
1026 	 We cannot reuse the current output register because we might
1027 	 have a situation like "a <- a op b", where the constraints
1028 	 force the second input operand ("b") to match the output
1029 	 operand ("a").  "b" must then be copied into a new register
1030 	 so that it doesn't clobber the current value of "a".
1031 
1032 	 We cannot use the same value if the output pseudo is
1033 	 early clobbered or the input pseudo is mentioned in the
1034 	 output, e.g. as an address part in memory, because
1035 	 output reload will actually extend the pseudo liveness.
1036 	 We don't care about eliminable hard regs here as we are
1037 	 interesting only in pseudos.  */
1038 
1039       /* Matching input's register value is the same as one of the other
1040 	 output operand.  Output operands in a parallel insn must be in
1041 	 different registers.  */
1042       out_conflict = false;
1043       if (REG_P (in_rtx))
1044 	{
1045 	  for (i = 0; outs[i] >= 0; i++)
1046 	    {
1047 	      rtx other_out_rtx = *curr_id->operand_loc[outs[i]];
1048 	      if (REG_P (other_out_rtx)
1049 		  && (regno_val_use_in (REGNO (in_rtx), other_out_rtx)
1050 		      != NULL_RTX))
1051 		{
1052 		  out_conflict = true;
1053 		  break;
1054 		}
1055 	    }
1056 	}
1057 
1058       new_in_reg = new_out_reg
1059 	= (! early_clobber_p && ins[1] < 0 && REG_P (in_rtx)
1060 	   && (int) REGNO (in_rtx) < lra_new_regno_start
1061 	   && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx))
1062 	   && (! early_clobber_p
1063 	       || check_conflict_input_operands (REGNO (in_rtx), ins))
1064 	   && (out < 0
1065 	       || regno_val_use_in (REGNO (in_rtx), out_rtx) == NULL_RTX)
1066 	   && !out_conflict
1067 	   ? lra_create_new_reg (inmode, in_rtx, goal_class, "")
1068 	   : lra_create_new_reg_with_unique_value (outmode, out_rtx,
1069 						   goal_class, ""));
1070     }
1071   /* In operand can be got from transformations before processing insn
1072      constraints.  One example of such transformations is subreg
1073      reloading (see function simplify_operand_subreg).  The new
1074      pseudos created by the transformations might have inaccurate
1075      class (ALL_REGS) and we should make their classes more
1076      accurate.  */
1077   narrow_reload_pseudo_class (in_rtx, goal_class);
1078   lra_emit_move (copy_rtx (new_in_reg), in_rtx);
1079   *before = get_insns ();
1080   end_sequence ();
1081   /* Add the new pseudo to consider values of subsequent input reload
1082      pseudos.  */
1083   lra_assert (curr_insn_input_reloads_num < LRA_MAX_INSN_RELOADS);
1084   curr_insn_input_reloads[curr_insn_input_reloads_num].input = in_rtx;
1085   curr_insn_input_reloads[curr_insn_input_reloads_num].match_p = true;
1086   curr_insn_input_reloads[curr_insn_input_reloads_num++].reg = new_in_reg;
1087   for (i = 0; (in = ins[i]) >= 0; i++)
1088     {
1089       lra_assert
1090 	(GET_MODE (*curr_id->operand_loc[in]) == VOIDmode
1091 	 || GET_MODE (new_in_reg) == GET_MODE (*curr_id->operand_loc[in]));
1092       *curr_id->operand_loc[in] = new_in_reg;
1093     }
1094   lra_update_dups (curr_id, ins);
1095   if (out < 0)
1096     return;
1097   /* See a comment for the input operand above.  */
1098   narrow_reload_pseudo_class (out_rtx, goal_class);
1099   if (find_reg_note (curr_insn, REG_UNUSED, out_rtx) == NULL_RTX)
1100     {
1101       start_sequence ();
1102       lra_emit_move (out_rtx, copy_rtx (new_out_reg));
1103       emit_insn (*after);
1104       *after = get_insns ();
1105       end_sequence ();
1106     }
1107   *curr_id->operand_loc[out] = new_out_reg;
1108   lra_update_dup (curr_id, out);
1109 }
1110 
1111 /* Return register class which is union of all reg classes in insn
1112    constraint alternative string starting with P.  */
1113 static enum reg_class
reg_class_from_constraints(const char * p)1114 reg_class_from_constraints (const char *p)
1115 {
1116   int c, len;
1117   enum reg_class op_class = NO_REGS;
1118 
1119   do
1120     switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1121       {
1122       case '#':
1123       case ',':
1124 	return op_class;
1125 
1126       case 'g':
1127 	op_class = reg_class_subunion[op_class][GENERAL_REGS];
1128 	break;
1129 
1130       default:
1131 	enum constraint_num cn = lookup_constraint (p);
1132 	enum reg_class cl = reg_class_for_constraint (cn);
1133 	if (cl == NO_REGS)
1134 	  {
1135 	    if (insn_extra_address_constraint (cn))
1136 	      op_class
1137 		= (reg_class_subunion
1138 		   [op_class][base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1139 					      ADDRESS, SCRATCH)]);
1140 	    break;
1141 	  }
1142 
1143 	op_class = reg_class_subunion[op_class][cl];
1144  	break;
1145       }
1146   while ((p += len), c);
1147   return op_class;
1148 }
1149 
1150 /* If OP is a register, return the class of the register as per
1151    get_reg_class, otherwise return NO_REGS.  */
1152 static inline enum reg_class
get_op_class(rtx op)1153 get_op_class (rtx op)
1154 {
1155   return REG_P (op) ? get_reg_class (REGNO (op)) : NO_REGS;
1156 }
1157 
1158 /* Return generated insn mem_pseudo:=val if TO_P or val:=mem_pseudo
1159    otherwise.  If modes of MEM_PSEUDO and VAL are different, use
1160    SUBREG for VAL to make them equal.  */
1161 static rtx_insn *
emit_spill_move(bool to_p,rtx mem_pseudo,rtx val)1162 emit_spill_move (bool to_p, rtx mem_pseudo, rtx val)
1163 {
1164   if (GET_MODE (mem_pseudo) != GET_MODE (val))
1165     {
1166       /* Usually size of mem_pseudo is greater than val size but in
1167 	 rare cases it can be less as it can be defined by target
1168 	 dependent macro HARD_REGNO_CALLER_SAVE_MODE.  */
1169       if (! MEM_P (val))
1170 	{
1171 	  val = gen_lowpart_SUBREG (GET_MODE (mem_pseudo),
1172 				    GET_CODE (val) == SUBREG
1173 				    ? SUBREG_REG (val) : val);
1174 	  LRA_SUBREG_P (val) = 1;
1175 	}
1176       else
1177 	{
1178 	  mem_pseudo = gen_lowpart_SUBREG (GET_MODE (val), mem_pseudo);
1179 	  LRA_SUBREG_P (mem_pseudo) = 1;
1180 	}
1181     }
1182   return to_p ? gen_move_insn (mem_pseudo, val)
1183 	      : gen_move_insn (val, mem_pseudo);
1184 }
1185 
1186 /* Process a special case insn (register move), return true if we
1187    don't need to process it anymore.  INSN should be a single set
1188    insn.  Set up that RTL was changed through CHANGE_P and that hook
1189    TARGET_SECONDARY_MEMORY_NEEDED says to use secondary memory through
1190    SEC_MEM_P.  */
1191 static bool
check_and_process_move(bool * change_p,bool * sec_mem_p ATTRIBUTE_UNUSED)1192 check_and_process_move (bool *change_p, bool *sec_mem_p ATTRIBUTE_UNUSED)
1193 {
1194   int sregno, dregno;
1195   rtx dest, src, dreg, sreg, new_reg, scratch_reg;
1196   rtx_insn *before;
1197   enum reg_class dclass, sclass, secondary_class;
1198   secondary_reload_info sri;
1199 
1200   lra_assert (curr_insn_set != NULL_RTX);
1201   dreg = dest = SET_DEST (curr_insn_set);
1202   sreg = src = SET_SRC (curr_insn_set);
1203   if (GET_CODE (dest) == SUBREG)
1204     dreg = SUBREG_REG (dest);
1205   if (GET_CODE (src) == SUBREG)
1206     sreg = SUBREG_REG (src);
1207   if (! (REG_P (dreg) || MEM_P (dreg)) || ! (REG_P (sreg) || MEM_P (sreg)))
1208     return false;
1209   sclass = dclass = NO_REGS;
1210   if (REG_P (dreg))
1211     dclass = get_reg_class (REGNO (dreg));
1212   gcc_assert (dclass < LIM_REG_CLASSES);
1213   if (dclass == ALL_REGS)
1214     /* ALL_REGS is used for new pseudos created by transformations
1215        like reload of SUBREG_REG (see function
1216        simplify_operand_subreg).  We don't know their class yet.  We
1217        should figure out the class from processing the insn
1218        constraints not in this fast path function.  Even if ALL_REGS
1219        were a right class for the pseudo, secondary_... hooks usually
1220        are not define for ALL_REGS.  */
1221     return false;
1222   if (REG_P (sreg))
1223     sclass = get_reg_class (REGNO (sreg));
1224   gcc_assert (sclass < LIM_REG_CLASSES);
1225   if (sclass == ALL_REGS)
1226     /* See comments above.  */
1227     return false;
1228   if (sclass == NO_REGS && dclass == NO_REGS)
1229     return false;
1230   if (targetm.secondary_memory_needed (GET_MODE (src), sclass, dclass)
1231       && ((sclass != NO_REGS && dclass != NO_REGS)
1232 	  || (GET_MODE (src)
1233 	      != targetm.secondary_memory_needed_mode (GET_MODE (src)))))
1234     {
1235       *sec_mem_p = true;
1236       return false;
1237     }
1238   if (! REG_P (dreg) || ! REG_P (sreg))
1239     return false;
1240   sri.prev_sri = NULL;
1241   sri.icode = CODE_FOR_nothing;
1242   sri.extra_cost = 0;
1243   secondary_class = NO_REGS;
1244   /* Set up hard register for a reload pseudo for hook
1245      secondary_reload because some targets just ignore unassigned
1246      pseudos in the hook.  */
1247   if (dclass != NO_REGS && lra_get_regno_hard_regno (REGNO (dreg)) < 0)
1248     {
1249       dregno = REGNO (dreg);
1250       reg_renumber[dregno] = ira_class_hard_regs[dclass][0];
1251     }
1252   else
1253     dregno = -1;
1254   if (sclass != NO_REGS && lra_get_regno_hard_regno (REGNO (sreg)) < 0)
1255     {
1256       sregno = REGNO (sreg);
1257       reg_renumber[sregno] = ira_class_hard_regs[sclass][0];
1258     }
1259   else
1260     sregno = -1;
1261   if (sclass != NO_REGS)
1262     secondary_class
1263       = (enum reg_class) targetm.secondary_reload (false, dest,
1264 						   (reg_class_t) sclass,
1265 						   GET_MODE (src), &sri);
1266   if (sclass == NO_REGS
1267       || ((secondary_class != NO_REGS || sri.icode != CODE_FOR_nothing)
1268 	  && dclass != NO_REGS))
1269     {
1270       enum reg_class old_sclass = secondary_class;
1271       secondary_reload_info old_sri = sri;
1272 
1273       sri.prev_sri = NULL;
1274       sri.icode = CODE_FOR_nothing;
1275       sri.extra_cost = 0;
1276       secondary_class
1277 	= (enum reg_class) targetm.secondary_reload (true, src,
1278 						     (reg_class_t) dclass,
1279 						     GET_MODE (src), &sri);
1280       /* Check the target hook consistency.  */
1281       lra_assert
1282 	((secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1283 	 || (old_sclass == NO_REGS && old_sri.icode == CODE_FOR_nothing)
1284 	 || (secondary_class == old_sclass && sri.icode == old_sri.icode));
1285     }
1286   if (sregno >= 0)
1287     reg_renumber [sregno] = -1;
1288   if (dregno >= 0)
1289     reg_renumber [dregno] = -1;
1290   if (secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1291     return false;
1292   *change_p = true;
1293   new_reg = NULL_RTX;
1294   if (secondary_class != NO_REGS)
1295     new_reg = lra_create_new_reg_with_unique_value (GET_MODE (src), NULL_RTX,
1296 						    secondary_class,
1297 						    "secondary");
1298   start_sequence ();
1299   if (sri.icode == CODE_FOR_nothing)
1300     lra_emit_move (new_reg, src);
1301   else
1302     {
1303       enum reg_class scratch_class;
1304 
1305       scratch_class = (reg_class_from_constraints
1306 		       (insn_data[sri.icode].operand[2].constraint));
1307       scratch_reg = (lra_create_new_reg_with_unique_value
1308 		     (insn_data[sri.icode].operand[2].mode, NULL_RTX,
1309 		      scratch_class, "scratch"));
1310       emit_insn (GEN_FCN (sri.icode) (new_reg != NULL_RTX ? new_reg : dest,
1311 				      src, scratch_reg));
1312     }
1313   before = get_insns ();
1314   end_sequence ();
1315   lra_process_new_insns (curr_insn, before, NULL, "Inserting the move");
1316   if (new_reg != NULL_RTX)
1317     SET_SRC (curr_insn_set) = new_reg;
1318   else
1319     {
1320       if (lra_dump_file != NULL)
1321 	{
1322 	  fprintf (lra_dump_file, "Deleting move %u\n", INSN_UID (curr_insn));
1323 	  dump_insn_slim (lra_dump_file, curr_insn);
1324 	}
1325       lra_set_insn_deleted (curr_insn);
1326       return true;
1327     }
1328   return false;
1329 }
1330 
1331 /* The following data describe the result of process_alt_operands.
1332    The data are used in curr_insn_transform to generate reloads.  */
1333 
1334 /* The chosen reg classes which should be used for the corresponding
1335    operands.  */
1336 static enum reg_class goal_alt[MAX_RECOG_OPERANDS];
1337 /* True if the operand should be the same as another operand and that
1338    other operand does not need a reload.  */
1339 static bool goal_alt_match_win[MAX_RECOG_OPERANDS];
1340 /* True if the operand does not need a reload.	*/
1341 static bool goal_alt_win[MAX_RECOG_OPERANDS];
1342 /* True if the operand can be offsetable memory.  */
1343 static bool goal_alt_offmemok[MAX_RECOG_OPERANDS];
1344 /* The number of an operand to which given operand can be matched to.  */
1345 static int goal_alt_matches[MAX_RECOG_OPERANDS];
1346 /* The number of elements in the following array.  */
1347 static int goal_alt_dont_inherit_ops_num;
1348 /* Numbers of operands whose reload pseudos should not be inherited.  */
1349 static int goal_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1350 /* True if the insn commutative operands should be swapped.  */
1351 static bool goal_alt_swapped;
1352 /* The chosen insn alternative.	 */
1353 static int goal_alt_number;
1354 
1355 /* True if the corresponding operand is the result of an equivalence
1356    substitution.  */
1357 static bool equiv_substition_p[MAX_RECOG_OPERANDS];
1358 
1359 /* The following five variables are used to choose the best insn
1360    alternative.	 They reflect final characteristics of the best
1361    alternative.	 */
1362 
1363 /* Number of necessary reloads and overall cost reflecting the
1364    previous value and other unpleasantness of the best alternative.  */
1365 static int best_losers, best_overall;
1366 /* Overall number hard registers used for reloads.  For example, on
1367    some targets we need 2 general registers to reload DFmode and only
1368    one floating point register.	 */
1369 static int best_reload_nregs;
1370 /* Overall number reflecting distances of previous reloading the same
1371    value.  The distances are counted from the current BB start.  It is
1372    used to improve inheritance chances.  */
1373 static int best_reload_sum;
1374 
1375 /* True if the current insn should have no correspondingly input or
1376    output reloads.  */
1377 static bool no_input_reloads_p, no_output_reloads_p;
1378 
1379 /* True if we swapped the commutative operands in the current
1380    insn.  */
1381 static int curr_swapped;
1382 
1383 /* if CHECK_ONLY_P is false, arrange for address element *LOC to be a
1384    register of class CL.  Add any input reloads to list BEFORE.  AFTER
1385    is nonnull if *LOC is an automodified value; handle that case by
1386    adding the required output reloads to list AFTER.  Return true if
1387    the RTL was changed.
1388 
1389    if CHECK_ONLY_P is true, check that the *LOC is a correct address
1390    register.  Return false if the address register is correct.  */
1391 static bool
process_addr_reg(rtx * loc,bool check_only_p,rtx_insn ** before,rtx_insn ** after,enum reg_class cl)1392 process_addr_reg (rtx *loc, bool check_only_p, rtx_insn **before, rtx_insn **after,
1393 		  enum reg_class cl)
1394 {
1395   int regno;
1396   enum reg_class rclass, new_class;
1397   rtx reg;
1398   rtx new_reg;
1399   machine_mode mode;
1400   bool subreg_p, before_p = false;
1401 
1402   subreg_p = GET_CODE (*loc) == SUBREG;
1403   if (subreg_p)
1404     {
1405       reg = SUBREG_REG (*loc);
1406       mode = GET_MODE (reg);
1407 
1408       /* For mode with size bigger than ptr_mode, there unlikely to be "mov"
1409 	 between two registers with different classes, but there normally will
1410 	 be "mov" which transfers element of vector register into the general
1411 	 register, and this normally will be a subreg which should be reloaded
1412 	 as a whole.  This is particularly likely to be triggered when
1413 	 -fno-split-wide-types specified.  */
1414       if (!REG_P (reg)
1415 	  || in_class_p (reg, cl, &new_class)
1416 	  || known_le (GET_MODE_SIZE (mode), GET_MODE_SIZE (ptr_mode)))
1417        loc = &SUBREG_REG (*loc);
1418     }
1419 
1420   reg = *loc;
1421   mode = GET_MODE (reg);
1422   if (! REG_P (reg))
1423     {
1424       if (check_only_p)
1425 	return true;
1426       /* Always reload memory in an address even if the target supports
1427 	 such addresses.  */
1428       new_reg = lra_create_new_reg_with_unique_value (mode, reg, cl, "address");
1429       before_p = true;
1430     }
1431   else
1432     {
1433       regno = REGNO (reg);
1434       rclass = get_reg_class (regno);
1435       if (! check_only_p
1436 	  && (*loc = get_equiv_with_elimination (reg, curr_insn)) != reg)
1437 	{
1438 	  if (lra_dump_file != NULL)
1439 	    {
1440 	      fprintf (lra_dump_file,
1441 		       "Changing pseudo %d in address of insn %u on equiv ",
1442 		       REGNO (reg), INSN_UID (curr_insn));
1443 	      dump_value_slim (lra_dump_file, *loc, 1);
1444 	      fprintf (lra_dump_file, "\n");
1445 	    }
1446 	  *loc = copy_rtx (*loc);
1447 	}
1448       if (*loc != reg || ! in_class_p (reg, cl, &new_class))
1449 	{
1450 	  if (check_only_p)
1451 	    return true;
1452 	  reg = *loc;
1453 	  if (get_reload_reg (after == NULL ? OP_IN : OP_INOUT,
1454 			      mode, reg, cl, subreg_p, "address", &new_reg))
1455 	    before_p = true;
1456 	}
1457       else if (new_class != NO_REGS && rclass != new_class)
1458 	{
1459 	  if (check_only_p)
1460 	    return true;
1461 	  lra_change_class (regno, new_class, "	   Change to", true);
1462 	  return false;
1463 	}
1464       else
1465 	return false;
1466     }
1467   if (before_p)
1468     {
1469       push_to_sequence (*before);
1470       lra_emit_move (new_reg, reg);
1471       *before = get_insns ();
1472       end_sequence ();
1473     }
1474   *loc = new_reg;
1475   if (after != NULL)
1476     {
1477       start_sequence ();
1478       lra_emit_move (before_p ? copy_rtx (reg) : reg, new_reg);
1479       emit_insn (*after);
1480       *after = get_insns ();
1481       end_sequence ();
1482     }
1483   return true;
1484 }
1485 
1486 /* Insert move insn in simplify_operand_subreg. BEFORE returns
1487    the insn to be inserted before curr insn. AFTER returns the
1488    the insn to be inserted after curr insn.  ORIGREG and NEWREG
1489    are the original reg and new reg for reload.  */
1490 static void
insert_move_for_subreg(rtx_insn ** before,rtx_insn ** after,rtx origreg,rtx newreg)1491 insert_move_for_subreg (rtx_insn **before, rtx_insn **after, rtx origreg,
1492 			rtx newreg)
1493 {
1494   if (before)
1495     {
1496       push_to_sequence (*before);
1497       lra_emit_move (newreg, origreg);
1498       *before = get_insns ();
1499       end_sequence ();
1500     }
1501   if (after)
1502     {
1503       start_sequence ();
1504       lra_emit_move (origreg, newreg);
1505       emit_insn (*after);
1506       *after = get_insns ();
1507       end_sequence ();
1508     }
1509 }
1510 
1511 static int valid_address_p (machine_mode mode, rtx addr, addr_space_t as);
1512 static bool process_address (int, bool, rtx_insn **, rtx_insn **);
1513 
1514 /* Make reloads for subreg in operand NOP with internal subreg mode
1515    REG_MODE, add new reloads for further processing.  Return true if
1516    any change was done.  */
1517 static bool
simplify_operand_subreg(int nop,machine_mode reg_mode)1518 simplify_operand_subreg (int nop, machine_mode reg_mode)
1519 {
1520   int hard_regno;
1521   rtx_insn *before, *after;
1522   machine_mode mode, innermode;
1523   rtx reg, new_reg;
1524   rtx operand = *curr_id->operand_loc[nop];
1525   enum reg_class regclass;
1526   enum op_type type;
1527 
1528   before = after = NULL;
1529 
1530   if (GET_CODE (operand) != SUBREG)
1531     return false;
1532 
1533   mode = GET_MODE (operand);
1534   reg = SUBREG_REG (operand);
1535   innermode = GET_MODE (reg);
1536   type = curr_static_id->operand[nop].type;
1537   if (MEM_P (reg))
1538     {
1539       const bool addr_was_valid
1540 	= valid_address_p (innermode, XEXP (reg, 0), MEM_ADDR_SPACE (reg));
1541       alter_subreg (curr_id->operand_loc[nop], false);
1542       rtx subst = *curr_id->operand_loc[nop];
1543       lra_assert (MEM_P (subst));
1544       const bool addr_is_valid = valid_address_p (GET_MODE (subst),
1545 						  XEXP (subst, 0),
1546 						  MEM_ADDR_SPACE (subst));
1547       if (!addr_was_valid
1548 	  || addr_is_valid
1549 	  || ((get_constraint_type (lookup_constraint
1550 				    (curr_static_id->operand[nop].constraint))
1551 	       != CT_SPECIAL_MEMORY)
1552 	      /* We still can reload address and if the address is
1553 		 valid, we can remove subreg without reloading its
1554 		 inner memory.  */
1555 	      && valid_address_p (GET_MODE (subst),
1556 				  regno_reg_rtx
1557 				  [ira_class_hard_regs
1558 				   [base_reg_class (GET_MODE (subst),
1559 						    MEM_ADDR_SPACE (subst),
1560 						    ADDRESS, SCRATCH)][0]],
1561 				  MEM_ADDR_SPACE (subst))))
1562 	{
1563 	  /* If we change the address for a paradoxical subreg of memory, the
1564 	     new address might violate the necessary alignment or the access
1565 	     might be slow; take this into consideration.  We need not worry
1566 	     about accesses beyond allocated memory for paradoxical memory
1567 	     subregs as we don't substitute such equiv memory (see processing
1568 	     equivalences in function lra_constraints) and because for spilled
1569 	     pseudos we allocate stack memory enough for the biggest
1570 	     corresponding paradoxical subreg.
1571 
1572 	     However, do not blindly simplify a (subreg (mem ...)) for
1573 	     WORD_REGISTER_OPERATIONS targets as this may lead to loading junk
1574 	     data into a register when the inner is narrower than outer or
1575 	     missing important data from memory when the inner is wider than
1576 	     outer.  This rule only applies to modes that are no wider than
1577 	     a word.
1578 
1579 	     If valid memory becomes invalid after subreg elimination
1580 	     and address might be different we still have to reload
1581 	     memory.
1582 	  */
1583 	  if ((! addr_was_valid
1584 	       || addr_is_valid
1585 	       || known_eq (GET_MODE_SIZE (mode), GET_MODE_SIZE (innermode)))
1586 	      && !(maybe_ne (GET_MODE_PRECISION (mode),
1587 			     GET_MODE_PRECISION (innermode))
1588 		   && known_le (GET_MODE_SIZE (mode), UNITS_PER_WORD)
1589 		   && known_le (GET_MODE_SIZE (innermode), UNITS_PER_WORD)
1590 		   && WORD_REGISTER_OPERATIONS)
1591 	      && (!(MEM_ALIGN (subst) < GET_MODE_ALIGNMENT (mode)
1592 		    && targetm.slow_unaligned_access (mode, MEM_ALIGN (subst)))
1593 		  || (MEM_ALIGN (reg) < GET_MODE_ALIGNMENT (innermode)
1594 		      && targetm.slow_unaligned_access (innermode,
1595 							MEM_ALIGN (reg)))))
1596 	    return true;
1597 
1598 	  *curr_id->operand_loc[nop] = operand;
1599 
1600 	  /* But if the address was not valid, we cannot reload the MEM without
1601 	     reloading the address first.  */
1602 	  if (!addr_was_valid)
1603 	    process_address (nop, false, &before, &after);
1604 
1605 	  /* INNERMODE is fast, MODE slow.  Reload the mem in INNERMODE.  */
1606 	  enum reg_class rclass
1607 	    = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1608 	  if (get_reload_reg (curr_static_id->operand[nop].type, innermode,
1609 			      reg, rclass, TRUE, "slow/invalid mem", &new_reg))
1610 	    {
1611 	      bool insert_before, insert_after;
1612 	      bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1613 
1614 	      insert_before = (type != OP_OUT
1615 			       || partial_subreg_p (mode, innermode));
1616 	      insert_after = type != OP_IN;
1617 	      insert_move_for_subreg (insert_before ? &before : NULL,
1618 				      insert_after ? &after : NULL,
1619 				      reg, new_reg);
1620 	    }
1621 	  SUBREG_REG (operand) = new_reg;
1622 
1623 	  /* Convert to MODE.  */
1624 	  reg = operand;
1625 	  rclass
1626 	    = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1627 	  if (get_reload_reg (curr_static_id->operand[nop].type, mode, reg,
1628 			      rclass, TRUE, "slow/invalid mem", &new_reg))
1629 	    {
1630 	      bool insert_before, insert_after;
1631 	      bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1632 
1633 	      insert_before = type != OP_OUT;
1634 	      insert_after = type != OP_IN;
1635 	      insert_move_for_subreg (insert_before ? &before : NULL,
1636 				      insert_after ? &after : NULL,
1637 				      reg, new_reg);
1638 	    }
1639 	  *curr_id->operand_loc[nop] = new_reg;
1640 	  lra_process_new_insns (curr_insn, before, after,
1641 				 "Inserting slow/invalid mem reload");
1642 	  return true;
1643 	}
1644 
1645       /* If the address was valid and became invalid, prefer to reload
1646 	 the memory.  Typical case is when the index scale should
1647 	 correspond the memory.  */
1648       *curr_id->operand_loc[nop] = operand;
1649       /* Do not return false here as the MEM_P (reg) will be processed
1650 	 later in this function.  */
1651     }
1652   else if (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER)
1653     {
1654       alter_subreg (curr_id->operand_loc[nop], false);
1655       return true;
1656     }
1657   else if (CONSTANT_P (reg))
1658     {
1659       /* Try to simplify subreg of constant.  It is usually result of
1660 	 equivalence substitution.  */
1661       if (innermode == VOIDmode
1662 	  && (innermode = original_subreg_reg_mode[nop]) == VOIDmode)
1663 	innermode = curr_static_id->operand[nop].mode;
1664       if ((new_reg = simplify_subreg (mode, reg, innermode,
1665 				      SUBREG_BYTE (operand))) != NULL_RTX)
1666 	{
1667 	  *curr_id->operand_loc[nop] = new_reg;
1668 	  return true;
1669 	}
1670     }
1671   /* Put constant into memory when we have mixed modes.  It generates
1672      a better code in most cases as it does not need a secondary
1673      reload memory.  It also prevents LRA looping when LRA is using
1674      secondary reload memory again and again.  */
1675   if (CONSTANT_P (reg) && CONST_POOL_OK_P (reg_mode, reg)
1676       && SCALAR_INT_MODE_P (reg_mode) != SCALAR_INT_MODE_P (mode))
1677     {
1678       SUBREG_REG (operand) = force_const_mem (reg_mode, reg);
1679       alter_subreg (curr_id->operand_loc[nop], false);
1680       return true;
1681     }
1682   /* Force a reload of the SUBREG_REG if this is a constant or PLUS or
1683      if there may be a problem accessing OPERAND in the outer
1684      mode.  */
1685   if ((REG_P (reg)
1686        && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1687        && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1688        /* Don't reload paradoxical subregs because we could be looping
1689 	  having repeatedly final regno out of hard regs range.  */
1690        && (hard_regno_nregs (hard_regno, innermode)
1691 	   >= hard_regno_nregs (hard_regno, mode))
1692        && simplify_subreg_regno (hard_regno, innermode,
1693 				 SUBREG_BYTE (operand), mode) < 0
1694        /* Don't reload subreg for matching reload.  It is actually
1695 	  valid subreg in LRA.  */
1696        && ! LRA_SUBREG_P (operand))
1697       || CONSTANT_P (reg) || GET_CODE (reg) == PLUS || MEM_P (reg))
1698     {
1699       enum reg_class rclass;
1700 
1701       if (REG_P (reg))
1702 	/* There is a big probability that we will get the same class
1703 	   for the new pseudo and we will get the same insn which
1704 	   means infinite looping.  So spill the new pseudo.  */
1705 	rclass = NO_REGS;
1706       else
1707 	/* The class will be defined later in curr_insn_transform.  */
1708 	rclass
1709 	  = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1710 
1711       if (get_reload_reg (curr_static_id->operand[nop].type, reg_mode, reg,
1712 			  rclass, TRUE, "subreg reg", &new_reg))
1713 	{
1714 	  bool insert_before, insert_after;
1715 	  bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1716 
1717 	  insert_before = (type != OP_OUT
1718 			   || read_modify_subreg_p (operand));
1719 	  insert_after = (type != OP_IN);
1720 	  insert_move_for_subreg (insert_before ? &before : NULL,
1721 				  insert_after ? &after : NULL,
1722 				  reg, new_reg);
1723 	}
1724       SUBREG_REG (operand) = new_reg;
1725       lra_process_new_insns (curr_insn, before, after,
1726 			     "Inserting subreg reload");
1727       return true;
1728     }
1729   /* Force a reload for a paradoxical subreg. For paradoxical subreg,
1730      IRA allocates hardreg to the inner pseudo reg according to its mode
1731      instead of the outermode, so the size of the hardreg may not be enough
1732      to contain the outermode operand, in that case we may need to insert
1733      reload for the reg. For the following two types of paradoxical subreg,
1734      we need to insert reload:
1735      1. If the op_type is OP_IN, and the hardreg could not be paired with
1736         other hardreg to contain the outermode operand
1737         (checked by in_hard_reg_set_p), we need to insert the reload.
1738      2. If the op_type is OP_OUT or OP_INOUT.
1739 
1740      Here is a paradoxical subreg example showing how the reload is generated:
1741 
1742      (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1743         (subreg:TI (reg:DI 107 [ __comp ]) 0)) {*movti_internal_rex64}
1744 
1745      In IRA, reg107 is allocated to a DImode hardreg. We use x86-64 as example
1746      here, if reg107 is assigned to hardreg R15, because R15 is the last
1747      hardreg, compiler cannot find another hardreg to pair with R15 to
1748      contain TImode data. So we insert a TImode reload reg180 for it.
1749      After reload is inserted:
1750 
1751      (insn 283 0 0 (set (subreg:DI (reg:TI 180 [orig:107 __comp ] [107]) 0)
1752         (reg:DI 107 [ __comp ])) -1
1753      (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1754         (subreg:TI (reg:TI 180 [orig:107 __comp ] [107]) 0)) {*movti_internal_rex64}
1755 
1756      Two reload hard registers will be allocated to reg180 to save TImode data
1757      in LRA_assign.
1758 
1759      For LRA pseudos this should normally be handled by the biggest_mode
1760      mechanism.  However, it's possible for new uses of an LRA pseudo
1761      to be introduced after we've allocated it, such as when undoing
1762      inheritance, and the allocated register might not then be appropriate
1763      for the new uses.  */
1764   else if (REG_P (reg)
1765 	   && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1766 	   && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1767 	   && (hard_regno_nregs (hard_regno, innermode)
1768 	       < hard_regno_nregs (hard_regno, mode))
1769 	   && (regclass = lra_get_allocno_class (REGNO (reg)))
1770 	   && (type != OP_IN
1771 	       || !in_hard_reg_set_p (reg_class_contents[regclass],
1772 				      mode, hard_regno)
1773 	       || overlaps_hard_reg_set_p (lra_no_alloc_regs,
1774 					   mode, hard_regno)))
1775     {
1776       /* The class will be defined later in curr_insn_transform.  */
1777       enum reg_class rclass
1778 	= (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1779 
1780       if (get_reload_reg (curr_static_id->operand[nop].type, mode, reg,
1781                           rclass, TRUE, "paradoxical subreg", &new_reg))
1782         {
1783 	  rtx subreg;
1784 	  bool insert_before, insert_after;
1785 
1786 	  PUT_MODE (new_reg, mode);
1787           subreg = gen_lowpart_SUBREG (innermode, new_reg);
1788 	  bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1789 
1790 	  insert_before = (type != OP_OUT);
1791 	  insert_after = (type != OP_IN);
1792 	  insert_move_for_subreg (insert_before ? &before : NULL,
1793 				  insert_after ? &after : NULL,
1794 				  reg, subreg);
1795 	}
1796       SUBREG_REG (operand) = new_reg;
1797       lra_process_new_insns (curr_insn, before, after,
1798                              "Inserting paradoxical subreg reload");
1799       return true;
1800     }
1801   return false;
1802 }
1803 
1804 /* Return TRUE if X refers for a hard register from SET.  */
1805 static bool
uses_hard_regs_p(rtx x,HARD_REG_SET set)1806 uses_hard_regs_p (rtx x, HARD_REG_SET set)
1807 {
1808   int i, j, x_hard_regno;
1809   machine_mode mode;
1810   const char *fmt;
1811   enum rtx_code code;
1812 
1813   if (x == NULL_RTX)
1814     return false;
1815   code = GET_CODE (x);
1816   mode = GET_MODE (x);
1817 
1818   if (code == SUBREG)
1819     {
1820       /* For all SUBREGs we want to check whether the full multi-register
1821 	 overlaps the set.  For normal SUBREGs this means 'get_hard_regno' of
1822 	 the inner register, for paradoxical SUBREGs this means the
1823 	 'get_hard_regno' of the full SUBREG and for complete SUBREGs either is
1824 	 fine.  Use the wider mode for all cases.  */
1825       rtx subreg = SUBREG_REG (x);
1826       mode = wider_subreg_mode (x);
1827       if (mode == GET_MODE (subreg))
1828 	{
1829 	  x = subreg;
1830 	  code = GET_CODE (x);
1831 	}
1832     }
1833 
1834   if (REG_P (x) || SUBREG_P (x))
1835     {
1836       x_hard_regno = get_hard_regno (x, true);
1837       return (x_hard_regno >= 0
1838 	      && overlaps_hard_reg_set_p (set, mode, x_hard_regno));
1839     }
1840   if (MEM_P (x))
1841     {
1842       struct address_info ad;
1843 
1844       decompose_mem_address (&ad, x);
1845       if (ad.base_term != NULL && uses_hard_regs_p (*ad.base_term, set))
1846 	return true;
1847       if (ad.index_term != NULL && uses_hard_regs_p (*ad.index_term, set))
1848 	return true;
1849     }
1850   fmt = GET_RTX_FORMAT (code);
1851   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1852     {
1853       if (fmt[i] == 'e')
1854 	{
1855 	  if (uses_hard_regs_p (XEXP (x, i), set))
1856 	    return true;
1857 	}
1858       else if (fmt[i] == 'E')
1859 	{
1860 	  for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1861 	    if (uses_hard_regs_p (XVECEXP (x, i, j), set))
1862 	      return true;
1863 	}
1864     }
1865   return false;
1866 }
1867 
1868 /* Return true if OP is a spilled pseudo. */
1869 static inline bool
spilled_pseudo_p(rtx op)1870 spilled_pseudo_p (rtx op)
1871 {
1872   return (REG_P (op)
1873 	  && REGNO (op) >= FIRST_PSEUDO_REGISTER && in_mem_p (REGNO (op)));
1874 }
1875 
1876 /* Return true if X is a general constant.  */
1877 static inline bool
general_constant_p(rtx x)1878 general_constant_p (rtx x)
1879 {
1880   return CONSTANT_P (x) && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (x));
1881 }
1882 
1883 static bool
reg_in_class_p(rtx reg,enum reg_class cl)1884 reg_in_class_p (rtx reg, enum reg_class cl)
1885 {
1886   if (cl == NO_REGS)
1887     return get_reg_class (REGNO (reg)) == NO_REGS;
1888   return in_class_p (reg, cl, NULL);
1889 }
1890 
1891 /* Return true if SET of RCLASS contains no hard regs which can be
1892    used in MODE.  */
1893 static bool
prohibited_class_reg_set_mode_p(enum reg_class rclass,HARD_REG_SET & set,machine_mode mode)1894 prohibited_class_reg_set_mode_p (enum reg_class rclass,
1895 				 HARD_REG_SET &set,
1896 				 machine_mode mode)
1897 {
1898   HARD_REG_SET temp;
1899 
1900   lra_assert (hard_reg_set_subset_p (reg_class_contents[rclass], set));
1901   COPY_HARD_REG_SET (temp, set);
1902   AND_COMPL_HARD_REG_SET (temp, lra_no_alloc_regs);
1903   return (hard_reg_set_subset_p
1904 	  (temp, ira_prohibited_class_mode_regs[rclass][mode]));
1905 }
1906 
1907 
1908 /* Used to check validity info about small class input operands.  It
1909    should be incremented at start of processing an insn
1910    alternative.  */
1911 static unsigned int curr_small_class_check = 0;
1912 
1913 /* Update number of used inputs of class OP_CLASS for operand NOP.
1914    Return true if we have more such class operands than the number of
1915    available regs.  */
1916 static bool
update_and_check_small_class_inputs(int nop,enum reg_class op_class)1917 update_and_check_small_class_inputs (int nop, enum reg_class op_class)
1918 {
1919   static unsigned int small_class_check[LIM_REG_CLASSES];
1920   static int small_class_input_nums[LIM_REG_CLASSES];
1921 
1922   if (SMALL_REGISTER_CLASS_P (op_class)
1923       /* We are interesting in classes became small because of fixing
1924 	 some hard regs, e.g. by an user through GCC options.  */
1925       && hard_reg_set_intersect_p (reg_class_contents[op_class],
1926 				   ira_no_alloc_regs)
1927       && (curr_static_id->operand[nop].type != OP_OUT
1928 	  || curr_static_id->operand[nop].early_clobber))
1929     {
1930       if (small_class_check[op_class] == curr_small_class_check)
1931 	small_class_input_nums[op_class]++;
1932       else
1933 	{
1934 	  small_class_check[op_class] = curr_small_class_check;
1935 	  small_class_input_nums[op_class] = 1;
1936 	}
1937       if (small_class_input_nums[op_class] > ira_class_hard_regs_num[op_class])
1938 	return true;
1939     }
1940   return false;
1941 }
1942 
1943 /* Major function to choose the current insn alternative and what
1944    operands should be reloaded and how.	 If ONLY_ALTERNATIVE is not
1945    negative we should consider only this alternative.  Return false if
1946    we cannot choose the alternative or find how to reload the
1947    operands.  */
1948 static bool
process_alt_operands(int only_alternative)1949 process_alt_operands (int only_alternative)
1950 {
1951   bool ok_p = false;
1952   int nop, overall, nalt;
1953   int n_alternatives = curr_static_id->n_alternatives;
1954   int n_operands = curr_static_id->n_operands;
1955   /* LOSERS counts the operands that don't fit this alternative and
1956      would require loading.  */
1957   int losers;
1958   int addr_losers;
1959   /* REJECT is a count of how undesirable this alternative says it is
1960      if any reloading is required.  If the alternative matches exactly
1961      then REJECT is ignored, but otherwise it gets this much counted
1962      against it in addition to the reloading needed.  */
1963   int reject;
1964   /* This is defined by '!' or '?' alternative constraint and added to
1965      reject.  But in some cases it can be ignored.  */
1966   int static_reject;
1967   int op_reject;
1968   /* The number of elements in the following array.  */
1969   int early_clobbered_regs_num;
1970   /* Numbers of operands which are early clobber registers.  */
1971   int early_clobbered_nops[MAX_RECOG_OPERANDS];
1972   enum reg_class curr_alt[MAX_RECOG_OPERANDS];
1973   HARD_REG_SET curr_alt_set[MAX_RECOG_OPERANDS];
1974   bool curr_alt_match_win[MAX_RECOG_OPERANDS];
1975   bool curr_alt_win[MAX_RECOG_OPERANDS];
1976   bool curr_alt_offmemok[MAX_RECOG_OPERANDS];
1977   int curr_alt_matches[MAX_RECOG_OPERANDS];
1978   /* The number of elements in the following array.  */
1979   int curr_alt_dont_inherit_ops_num;
1980   /* Numbers of operands whose reload pseudos should not be inherited.	*/
1981   int curr_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1982   rtx op;
1983   /* The register when the operand is a subreg of register, otherwise the
1984      operand itself.  */
1985   rtx no_subreg_reg_operand[MAX_RECOG_OPERANDS];
1986   /* The register if the operand is a register or subreg of register,
1987      otherwise NULL.  */
1988   rtx operand_reg[MAX_RECOG_OPERANDS];
1989   int hard_regno[MAX_RECOG_OPERANDS];
1990   machine_mode biggest_mode[MAX_RECOG_OPERANDS];
1991   int reload_nregs, reload_sum;
1992   bool costly_p;
1993   enum reg_class cl;
1994 
1995   /* Calculate some data common for all alternatives to speed up the
1996      function.	*/
1997   for (nop = 0; nop < n_operands; nop++)
1998     {
1999       rtx reg;
2000 
2001       op = no_subreg_reg_operand[nop] = *curr_id->operand_loc[nop];
2002       /* The real hard regno of the operand after the allocation.  */
2003       hard_regno[nop] = get_hard_regno (op, true);
2004 
2005       operand_reg[nop] = reg = op;
2006       biggest_mode[nop] = GET_MODE (op);
2007       if (GET_CODE (op) == SUBREG)
2008 	{
2009 	  biggest_mode[nop] = wider_subreg_mode (op);
2010 	  operand_reg[nop] = reg = SUBREG_REG (op);
2011 	}
2012       if (! REG_P (reg))
2013 	operand_reg[nop] = NULL_RTX;
2014       else if (REGNO (reg) >= FIRST_PSEUDO_REGISTER
2015 	       || ((int) REGNO (reg)
2016 		   == lra_get_elimination_hard_regno (REGNO (reg))))
2017 	no_subreg_reg_operand[nop] = reg;
2018       else
2019 	operand_reg[nop] = no_subreg_reg_operand[nop]
2020 	  /* Just use natural mode for elimination result.  It should
2021 	     be enough for extra constraints hooks.  */
2022 	  = regno_reg_rtx[hard_regno[nop]];
2023     }
2024 
2025   /* The constraints are made of several alternatives.	Each operand's
2026      constraint looks like foo,bar,... with commas separating the
2027      alternatives.  The first alternatives for all operands go
2028      together, the second alternatives go together, etc.
2029 
2030      First loop over alternatives.  */
2031   alternative_mask preferred = curr_id->preferred_alternatives;
2032   if (only_alternative >= 0)
2033     preferred &= ALTERNATIVE_BIT (only_alternative);
2034 
2035   for (nalt = 0; nalt < n_alternatives; nalt++)
2036     {
2037       /* Loop over operands for one constraint alternative.  */
2038       if (!TEST_BIT (preferred, nalt))
2039 	continue;
2040 
2041       bool matching_early_clobber[MAX_RECOG_OPERANDS];
2042       curr_small_class_check++;
2043       overall = losers = addr_losers = 0;
2044       static_reject = reject = reload_nregs = reload_sum = 0;
2045       for (nop = 0; nop < n_operands; nop++)
2046 	{
2047 	  int inc = (curr_static_id
2048 		     ->operand_alternative[nalt * n_operands + nop].reject);
2049 	  if (lra_dump_file != NULL && inc != 0)
2050 	    fprintf (lra_dump_file,
2051 		     "            Staticly defined alt reject+=%d\n", inc);
2052 	  static_reject += inc;
2053 	  matching_early_clobber[nop] = 0;
2054 	}
2055       reject += static_reject;
2056       early_clobbered_regs_num = 0;
2057 
2058       for (nop = 0; nop < n_operands; nop++)
2059 	{
2060 	  const char *p;
2061 	  char *end;
2062 	  int len, c, m, i, opalt_num, this_alternative_matches;
2063 	  bool win, did_match, offmemok, early_clobber_p;
2064 	  /* false => this operand can be reloaded somehow for this
2065 	     alternative.  */
2066 	  bool badop;
2067 	  /* true => this operand can be reloaded if the alternative
2068 	     allows regs.  */
2069 	  bool winreg;
2070 	  /* True if a constant forced into memory would be OK for
2071 	     this operand.  */
2072 	  bool constmemok;
2073 	  enum reg_class this_alternative, this_costly_alternative;
2074 	  HARD_REG_SET this_alternative_set, this_costly_alternative_set;
2075 	  bool this_alternative_match_win, this_alternative_win;
2076 	  bool this_alternative_offmemok;
2077 	  bool scratch_p;
2078 	  machine_mode mode;
2079 	  enum constraint_num cn;
2080 
2081 	  opalt_num = nalt * n_operands + nop;
2082 	  if (curr_static_id->operand_alternative[opalt_num].anything_ok)
2083 	    {
2084 	      /* Fast track for no constraints at all.	*/
2085 	      curr_alt[nop] = NO_REGS;
2086 	      CLEAR_HARD_REG_SET (curr_alt_set[nop]);
2087 	      curr_alt_win[nop] = true;
2088 	      curr_alt_match_win[nop] = false;
2089 	      curr_alt_offmemok[nop] = false;
2090 	      curr_alt_matches[nop] = -1;
2091 	      continue;
2092 	    }
2093 
2094 	  op = no_subreg_reg_operand[nop];
2095 	  mode = curr_operand_mode[nop];
2096 
2097 	  win = did_match = winreg = offmemok = constmemok = false;
2098 	  badop = true;
2099 
2100 	  early_clobber_p = false;
2101 	  p = curr_static_id->operand_alternative[opalt_num].constraint;
2102 
2103 	  this_costly_alternative = this_alternative = NO_REGS;
2104 	  /* We update set of possible hard regs besides its class
2105 	     because reg class might be inaccurate.  For example,
2106 	     union of LO_REGS (l), HI_REGS(h), and STACK_REG(k) in ARM
2107 	     is translated in HI_REGS because classes are merged by
2108 	     pairs and there is no accurate intermediate class.	 */
2109 	  CLEAR_HARD_REG_SET (this_alternative_set);
2110 	  CLEAR_HARD_REG_SET (this_costly_alternative_set);
2111 	  this_alternative_win = false;
2112 	  this_alternative_match_win = false;
2113 	  this_alternative_offmemok = false;
2114 	  this_alternative_matches = -1;
2115 
2116 	  /* An empty constraint should be excluded by the fast
2117 	     track.  */
2118 	  lra_assert (*p != 0 && *p != ',');
2119 
2120 	  op_reject = 0;
2121 	  /* Scan this alternative's specs for this operand; set WIN
2122 	     if the operand fits any letter in this alternative.
2123 	     Otherwise, clear BADOP if this operand could fit some
2124 	     letter after reloads, or set WINREG if this operand could
2125 	     fit after reloads provided the constraint allows some
2126 	     registers.	 */
2127 	  costly_p = false;
2128 	  do
2129 	    {
2130 	      switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
2131 		{
2132 		case '\0':
2133 		  len = 0;
2134 		  break;
2135 		case ',':
2136 		  c = '\0';
2137 		  break;
2138 
2139 		case '&':
2140 		  early_clobber_p = true;
2141 		  break;
2142 
2143 		case '$':
2144 		  op_reject += LRA_MAX_REJECT;
2145 		  break;
2146 		case '^':
2147 		  op_reject += LRA_LOSER_COST_FACTOR;
2148 		  break;
2149 
2150 		case '#':
2151 		  /* Ignore rest of this alternative.  */
2152 		  c = '\0';
2153 		  break;
2154 
2155 		case '0':  case '1':  case '2':	 case '3':  case '4':
2156 		case '5':  case '6':  case '7':	 case '8':  case '9':
2157 		  {
2158 		    int m_hregno;
2159 		    bool match_p;
2160 
2161 		    m = strtoul (p, &end, 10);
2162 		    p = end;
2163 		    len = 0;
2164 		    lra_assert (nop > m);
2165 
2166 		    /* Reject matches if we don't know which operand is
2167 		       bigger.  This situation would arguably be a bug in
2168 		       an .md pattern, but could also occur in a user asm.  */
2169 		    if (!ordered_p (GET_MODE_SIZE (biggest_mode[m]),
2170 				    GET_MODE_SIZE (biggest_mode[nop])))
2171 		      break;
2172 
2173 		    /* Don't match wrong asm insn operands for proper
2174 		       diagnostic later.  */
2175 		    if (INSN_CODE (curr_insn) < 0
2176 			&& (curr_operand_mode[m] == BLKmode
2177 			    || curr_operand_mode[nop] == BLKmode)
2178 			&& curr_operand_mode[m] != curr_operand_mode[nop])
2179 		      break;
2180 
2181 		    m_hregno = get_hard_regno (*curr_id->operand_loc[m], false);
2182 		    /* We are supposed to match a previous operand.
2183 		       If we do, we win if that one did.  If we do
2184 		       not, count both of the operands as losers.
2185 		       (This is too conservative, since most of the
2186 		       time only a single reload insn will be needed
2187 		       to make the two operands win.  As a result,
2188 		       this alternative may be rejected when it is
2189 		       actually desirable.)  */
2190 		    match_p = false;
2191 		    if (operands_match_p (*curr_id->operand_loc[nop],
2192 					  *curr_id->operand_loc[m], m_hregno))
2193 		      {
2194 			/* We should reject matching of an early
2195 			   clobber operand if the matching operand is
2196 			   not dying in the insn.  */
2197 			if (! curr_static_id->operand[m].early_clobber
2198 			    || operand_reg[nop] == NULL_RTX
2199 			    || (find_regno_note (curr_insn, REG_DEAD,
2200 						 REGNO (op))
2201 				|| REGNO (op) == REGNO (operand_reg[m])))
2202 			  match_p = true;
2203 		      }
2204 		    if (match_p)
2205 		      {
2206 			/* If we are matching a non-offsettable
2207 			   address where an offsettable address was
2208 			   expected, then we must reject this
2209 			   combination, because we can't reload
2210 			   it.	*/
2211 			if (curr_alt_offmemok[m]
2212 			    && MEM_P (*curr_id->operand_loc[m])
2213 			    && curr_alt[m] == NO_REGS && ! curr_alt_win[m])
2214 			  continue;
2215 		      }
2216 		    else
2217 		      {
2218 			/* Operands don't match.  If the operands are
2219 			   different user defined explicit hard registers,
2220 			   then we cannot make them match.  */
2221 			if ((REG_P (*curr_id->operand_loc[nop])
2222 			     || SUBREG_P (*curr_id->operand_loc[nop]))
2223 			    && (REG_P (*curr_id->operand_loc[m])
2224 				|| SUBREG_P (*curr_id->operand_loc[m])))
2225 			  {
2226 			    rtx nop_reg = *curr_id->operand_loc[nop];
2227 			    if (SUBREG_P (nop_reg))
2228 			      nop_reg = SUBREG_REG (nop_reg);
2229 			    rtx m_reg = *curr_id->operand_loc[m];
2230 			    if (SUBREG_P (m_reg))
2231 			      m_reg = SUBREG_REG (m_reg);
2232 
2233 			    if (REG_P (nop_reg)
2234 				&& HARD_REGISTER_P (nop_reg)
2235 				&& REG_USERVAR_P (nop_reg)
2236 				&& REG_P (m_reg)
2237 				&& HARD_REGISTER_P (m_reg)
2238 				&& REG_USERVAR_P (m_reg))
2239 			      break;
2240 			  }
2241 
2242 			/* Both operands must allow a reload register,
2243 			   otherwise we cannot make them match.  */
2244 			if (curr_alt[m] == NO_REGS)
2245 			  break;
2246 			/* Retroactively mark the operand we had to
2247 			   match as a loser, if it wasn't already and
2248 			   it wasn't matched to a register constraint
2249 			   (e.g it might be matched by memory). */
2250 			if (curr_alt_win[m]
2251 			    && (operand_reg[m] == NULL_RTX
2252 				|| hard_regno[m] < 0))
2253 			  {
2254 			    losers++;
2255 			    reload_nregs
2256 			      += (ira_reg_class_max_nregs[curr_alt[m]]
2257 				  [GET_MODE (*curr_id->operand_loc[m])]);
2258 			  }
2259 
2260 			/* Prefer matching earlyclobber alternative as
2261 			   it results in less hard regs required for
2262 			   the insn than a non-matching earlyclobber
2263 			   alternative.  */
2264 			if (curr_static_id->operand[m].early_clobber)
2265 			  {
2266 			    if (lra_dump_file != NULL)
2267 			      fprintf
2268 				(lra_dump_file,
2269 				 "            %d Matching earlyclobber alt:"
2270 				 " reject--\n",
2271 				 nop);
2272 			    if (!matching_early_clobber[m])
2273 			      {
2274 				reject--;
2275 				matching_early_clobber[m] = 1;
2276 			      }
2277 			  }
2278 			/* Otherwise we prefer no matching
2279 			   alternatives because it gives more freedom
2280 			   in RA.  */
2281 			else if (operand_reg[nop] == NULL_RTX
2282 				 || (find_regno_note (curr_insn, REG_DEAD,
2283 						      REGNO (operand_reg[nop]))
2284 				     == NULL_RTX))
2285 			  {
2286 			    if (lra_dump_file != NULL)
2287 			      fprintf
2288 				(lra_dump_file,
2289 				 "            %d Matching alt: reject+=2\n",
2290 				 nop);
2291 			    reject += 2;
2292 			  }
2293 		      }
2294 		    /* If we have to reload this operand and some
2295 		       previous operand also had to match the same
2296 		       thing as this operand, we don't know how to do
2297 		       that.  */
2298 		    if (!match_p || !curr_alt_win[m])
2299 		      {
2300 			for (i = 0; i < nop; i++)
2301 			  if (curr_alt_matches[i] == m)
2302 			    break;
2303 			if (i < nop)
2304 			  break;
2305 		      }
2306 		    else
2307 		      did_match = true;
2308 
2309 		    this_alternative_matches = m;
2310 		    /* This can be fixed with reloads if the operand
2311 		       we are supposed to match can be fixed with
2312 		       reloads. */
2313 		    badop = false;
2314 		    this_alternative = curr_alt[m];
2315 		    COPY_HARD_REG_SET (this_alternative_set, curr_alt_set[m]);
2316 		    winreg = this_alternative != NO_REGS;
2317 		    break;
2318 		  }
2319 
2320 		case 'g':
2321 		  if (MEM_P (op)
2322 		      || general_constant_p (op)
2323 		      || spilled_pseudo_p (op))
2324 		    win = true;
2325 		  cl = GENERAL_REGS;
2326 		  goto reg;
2327 
2328 		default:
2329 		  cn = lookup_constraint (p);
2330 		  switch (get_constraint_type (cn))
2331 		    {
2332 		    case CT_REGISTER:
2333 		      cl = reg_class_for_constraint (cn);
2334 		      if (cl != NO_REGS)
2335 			goto reg;
2336 		      break;
2337 
2338 		    case CT_CONST_INT:
2339 		      if (CONST_INT_P (op)
2340 			  && insn_const_int_ok_for_constraint (INTVAL (op), cn))
2341 			win = true;
2342 		      break;
2343 
2344 		    case CT_MEMORY:
2345 		      if (MEM_P (op)
2346 			  && satisfies_memory_constraint_p (op, cn))
2347 			win = true;
2348 		      else if (spilled_pseudo_p (op))
2349 			win = true;
2350 
2351 		      /* If we didn't already win, we can reload constants
2352 			 via force_const_mem or put the pseudo value into
2353 			 memory, or make other memory by reloading the
2354 			 address like for 'o'.  */
2355 		      if (CONST_POOL_OK_P (mode, op)
2356 			  || MEM_P (op) || REG_P (op)
2357 			  /* We can restore the equiv insn by a
2358 			     reload.  */
2359 			  || equiv_substition_p[nop])
2360 			badop = false;
2361 		      constmemok = true;
2362 		      offmemok = true;
2363 		      break;
2364 
2365 		    case CT_ADDRESS:
2366 		      /* An asm operand with an address constraint
2367 			 that doesn't satisfy address_operand has
2368 			 is_address cleared, so that we don't try to
2369 			 make a non-address fit.  */
2370 		      if (!curr_static_id->operand[nop].is_address)
2371 			break;
2372 		      /* If we didn't already win, we can reload the address
2373 			 into a base register.  */
2374 		      if (satisfies_address_constraint_p (op, cn))
2375 			win = true;
2376 		      cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
2377 					   ADDRESS, SCRATCH);
2378 		      badop = false;
2379 		      goto reg;
2380 
2381 		    case CT_FIXED_FORM:
2382 		      if (constraint_satisfied_p (op, cn))
2383 			win = true;
2384 		      break;
2385 
2386 		    case CT_SPECIAL_MEMORY:
2387 		      if (MEM_P (op)
2388 			  && satisfies_memory_constraint_p (op, cn))
2389 			win = true;
2390 		      else if (spilled_pseudo_p (op))
2391 			win = true;
2392 		      break;
2393 		    }
2394 		  break;
2395 
2396 		reg:
2397 		  if (mode == BLKmode)
2398 		    break;
2399 		  this_alternative = reg_class_subunion[this_alternative][cl];
2400 		  IOR_HARD_REG_SET (this_alternative_set,
2401 				    reg_class_contents[cl]);
2402 		  if (costly_p)
2403 		    {
2404 		      this_costly_alternative
2405 			= reg_class_subunion[this_costly_alternative][cl];
2406 		      IOR_HARD_REG_SET (this_costly_alternative_set,
2407 					reg_class_contents[cl]);
2408 		    }
2409 		  winreg = true;
2410 		  if (REG_P (op))
2411 		    {
2412 		      if (hard_regno[nop] >= 0
2413 			  && in_hard_reg_set_p (this_alternative_set,
2414 						mode, hard_regno[nop]))
2415 			win = true;
2416 		      else if (hard_regno[nop] < 0
2417 			       && in_class_p (op, this_alternative, NULL))
2418 			win = true;
2419 		    }
2420 		  break;
2421 		}
2422 	      if (c != ' ' && c != '\t')
2423 		costly_p = c == '*';
2424 	    }
2425 	  while ((p += len), c);
2426 
2427 	  scratch_p = (operand_reg[nop] != NULL_RTX
2428 		       && lra_former_scratch_p (REGNO (operand_reg[nop])));
2429 	  /* Record which operands fit this alternative.  */
2430 	  if (win)
2431 	    {
2432 	      this_alternative_win = true;
2433 	      if (operand_reg[nop] != NULL_RTX)
2434 		{
2435 		  if (hard_regno[nop] >= 0)
2436 		    {
2437 		      if (in_hard_reg_set_p (this_costly_alternative_set,
2438 					     mode, hard_regno[nop]))
2439 			{
2440 			  if (lra_dump_file != NULL)
2441 			    fprintf (lra_dump_file,
2442 				     "            %d Costly set: reject++\n",
2443 				     nop);
2444 			  reject++;
2445 			}
2446 		    }
2447 		  else
2448 		    {
2449 		      /* Prefer won reg to spilled pseudo under other
2450 			 equal conditions for possibe inheritance.  */
2451 		      if (! scratch_p)
2452 			{
2453 			  if (lra_dump_file != NULL)
2454 			    fprintf
2455 			      (lra_dump_file,
2456 			       "            %d Non pseudo reload: reject++\n",
2457 			       nop);
2458 			  reject++;
2459 			}
2460 		      if (in_class_p (operand_reg[nop],
2461 				      this_costly_alternative, NULL))
2462 			{
2463 			  if (lra_dump_file != NULL)
2464 			    fprintf
2465 			      (lra_dump_file,
2466 			       "            %d Non pseudo costly reload:"
2467 			       " reject++\n",
2468 			       nop);
2469 			  reject++;
2470 			}
2471 		    }
2472 		  /* We simulate the behavior of old reload here.
2473 		     Although scratches need hard registers and it
2474 		     might result in spilling other pseudos, no reload
2475 		     insns are generated for the scratches.  So it
2476 		     might cost something but probably less than old
2477 		     reload pass believes.  */
2478 		  if (scratch_p)
2479 		    {
2480 		      if (lra_dump_file != NULL)
2481 			fprintf (lra_dump_file,
2482 				 "            %d Scratch win: reject+=2\n",
2483 				 nop);
2484 		      reject += 2;
2485 		    }
2486 		}
2487 	    }
2488 	  else if (did_match)
2489 	    this_alternative_match_win = true;
2490 	  else
2491 	    {
2492 	      int const_to_mem = 0;
2493 	      bool no_regs_p;
2494 
2495 	      reject += op_reject;
2496 	      /* Never do output reload of stack pointer.  It makes
2497 		 impossible to do elimination when SP is changed in
2498 		 RTL.  */
2499 	      if (op == stack_pointer_rtx && ! frame_pointer_needed
2500 		  && curr_static_id->operand[nop].type != OP_IN)
2501 		goto fail;
2502 
2503 	      /* If this alternative asks for a specific reg class, see if there
2504 		 is at least one allocatable register in that class.  */
2505 	      no_regs_p
2506 		= (this_alternative == NO_REGS
2507 		   || (hard_reg_set_subset_p
2508 		       (reg_class_contents[this_alternative],
2509 			lra_no_alloc_regs)));
2510 
2511 	      /* For asms, verify that the class for this alternative is possible
2512 		 for the mode that is specified.  */
2513 	      if (!no_regs_p && INSN_CODE (curr_insn) < 0)
2514 		{
2515 		  int i;
2516 		  for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2517 		    if (targetm.hard_regno_mode_ok (i, mode)
2518 			&& in_hard_reg_set_p (reg_class_contents[this_alternative],
2519 					      mode, i))
2520 		      break;
2521 		  if (i == FIRST_PSEUDO_REGISTER)
2522 		    winreg = false;
2523 		}
2524 
2525 	      /* If this operand accepts a register, and if the
2526 		 register class has at least one allocatable register,
2527 		 then this operand can be reloaded.  */
2528 	      if (winreg && !no_regs_p)
2529 		badop = false;
2530 
2531 	      if (badop)
2532 		{
2533 		  if (lra_dump_file != NULL)
2534 		    fprintf (lra_dump_file,
2535 			     "            alt=%d: Bad operand -- refuse\n",
2536 			     nalt);
2537 		  goto fail;
2538 		}
2539 
2540 	      if (this_alternative != NO_REGS)
2541 		{
2542 		  HARD_REG_SET available_regs;
2543 
2544 		  COPY_HARD_REG_SET (available_regs,
2545 				     reg_class_contents[this_alternative]);
2546 		  AND_COMPL_HARD_REG_SET
2547 		    (available_regs,
2548 		     ira_prohibited_class_mode_regs[this_alternative][mode]);
2549 		  AND_COMPL_HARD_REG_SET (available_regs, lra_no_alloc_regs);
2550 		  if (hard_reg_set_empty_p (available_regs))
2551 		    {
2552 		      /* There are no hard regs holding a value of given
2553 			 mode.  */
2554 		      if (offmemok)
2555 			{
2556 			  this_alternative = NO_REGS;
2557 			  if (lra_dump_file != NULL)
2558 			    fprintf (lra_dump_file,
2559 				     "            %d Using memory because of"
2560 				     " a bad mode: reject+=2\n",
2561 				     nop);
2562 			  reject += 2;
2563 			}
2564 		      else
2565 			{
2566 			  if (lra_dump_file != NULL)
2567 			    fprintf (lra_dump_file,
2568 				     "            alt=%d: Wrong mode -- refuse\n",
2569 				     nalt);
2570 			  goto fail;
2571 			}
2572 		    }
2573 		}
2574 
2575 	      /* If not assigned pseudo has a class which a subset of
2576 		 required reg class, it is a less costly alternative
2577 		 as the pseudo still can get a hard reg of necessary
2578 		 class.  */
2579 	      if (! no_regs_p && REG_P (op) && hard_regno[nop] < 0
2580 		  && (cl = get_reg_class (REGNO (op))) != NO_REGS
2581 		  && ira_class_subset_p[this_alternative][cl])
2582 		{
2583 		  if (lra_dump_file != NULL)
2584 		    fprintf
2585 		      (lra_dump_file,
2586 		       "            %d Super set class reg: reject-=3\n", nop);
2587 		  reject -= 3;
2588 		}
2589 
2590 	      this_alternative_offmemok = offmemok;
2591 	      if (this_costly_alternative != NO_REGS)
2592 		{
2593 		  if (lra_dump_file != NULL)
2594 		    fprintf (lra_dump_file,
2595 			     "            %d Costly loser: reject++\n", nop);
2596 		  reject++;
2597 		}
2598 	      /* If the operand is dying, has a matching constraint,
2599 		 and satisfies constraints of the matched operand
2600 		 which failed to satisfy the own constraints, most probably
2601 		 the reload for this operand will be gone.  */
2602 	      if (this_alternative_matches >= 0
2603 		  && !curr_alt_win[this_alternative_matches]
2604 		  && REG_P (op)
2605 		  && find_regno_note (curr_insn, REG_DEAD, REGNO (op))
2606 		  && (hard_regno[nop] >= 0
2607 		      ? in_hard_reg_set_p (this_alternative_set,
2608 					   mode, hard_regno[nop])
2609 		      : in_class_p (op, this_alternative, NULL)))
2610 		{
2611 		  if (lra_dump_file != NULL)
2612 		    fprintf
2613 		      (lra_dump_file,
2614 		       "            %d Dying matched operand reload: reject++\n",
2615 		       nop);
2616 		  reject++;
2617 		}
2618 	      else
2619 		{
2620 		  /* Strict_low_part requires to reload the register
2621 		     not the sub-register.  In this case we should
2622 		     check that a final reload hard reg can hold the
2623 		     value mode.  */
2624 		  if (curr_static_id->operand[nop].strict_low
2625 		      && REG_P (op)
2626 		      && hard_regno[nop] < 0
2627 		      && GET_CODE (*curr_id->operand_loc[nop]) == SUBREG
2628 		      && ira_class_hard_regs_num[this_alternative] > 0
2629 		      && (!targetm.hard_regno_mode_ok
2630 			  (ira_class_hard_regs[this_alternative][0],
2631 			   GET_MODE (*curr_id->operand_loc[nop]))))
2632 		    {
2633 		      if (lra_dump_file != NULL)
2634 			fprintf
2635 			  (lra_dump_file,
2636 			   "            alt=%d: Strict low subreg reload -- refuse\n",
2637 			   nalt);
2638 		      goto fail;
2639 		    }
2640 		  losers++;
2641 		}
2642 	      if (operand_reg[nop] != NULL_RTX
2643 		  /* Output operands and matched input operands are
2644 		     not inherited.  The following conditions do not
2645 		     exactly describe the previous statement but they
2646 		     are pretty close.  */
2647 		  && curr_static_id->operand[nop].type != OP_OUT
2648 		  && (this_alternative_matches < 0
2649 		      || curr_static_id->operand[nop].type != OP_IN))
2650 		{
2651 		  int last_reload = (lra_reg_info[ORIGINAL_REGNO
2652 						  (operand_reg[nop])]
2653 				     .last_reload);
2654 
2655 		  /* The value of reload_sum has sense only if we
2656 		     process insns in their order.  It happens only on
2657 		     the first constraints sub-pass when we do most of
2658 		     reload work.  */
2659 		  if (lra_constraint_iter == 1 && last_reload > bb_reload_num)
2660 		    reload_sum += last_reload - bb_reload_num;
2661 		}
2662 	      /* If this is a constant that is reloaded into the
2663 		 desired class by copying it to memory first, count
2664 		 that as another reload.  This is consistent with
2665 		 other code and is required to avoid choosing another
2666 		 alternative when the constant is moved into memory.
2667 		 Note that the test here is precisely the same as in
2668 		 the code below that calls force_const_mem.  */
2669 	      if (CONST_POOL_OK_P (mode, op)
2670 		  && ((targetm.preferred_reload_class
2671 		       (op, this_alternative) == NO_REGS)
2672 		      || no_input_reloads_p))
2673 		{
2674 		  const_to_mem = 1;
2675 		  if (! no_regs_p)
2676 		    losers++;
2677 		}
2678 
2679 	      /* Alternative loses if it requires a type of reload not
2680 		 permitted for this insn.  We can always reload
2681 		 objects with a REG_UNUSED note.  */
2682 	      if ((curr_static_id->operand[nop].type != OP_IN
2683 		   && no_output_reloads_p
2684 		   && ! find_reg_note (curr_insn, REG_UNUSED, op))
2685 		  || (curr_static_id->operand[nop].type != OP_OUT
2686 		      && no_input_reloads_p && ! const_to_mem)
2687 		  || (this_alternative_matches >= 0
2688 		      && (no_input_reloads_p
2689 			  || (no_output_reloads_p
2690 			      && (curr_static_id->operand
2691 				  [this_alternative_matches].type != OP_IN)
2692 			      && ! find_reg_note (curr_insn, REG_UNUSED,
2693 						  no_subreg_reg_operand
2694 						  [this_alternative_matches])))))
2695 		{
2696 		  if (lra_dump_file != NULL)
2697 		    fprintf
2698 		      (lra_dump_file,
2699 		       "            alt=%d: No input/otput reload -- refuse\n",
2700 		       nalt);
2701 		  goto fail;
2702 		}
2703 
2704 	      /* Alternative loses if it required class pseudo cannot
2705 		 hold value of required mode.  Such insns can be
2706 		 described by insn definitions with mode iterators.  */
2707 	      if (GET_MODE (*curr_id->operand_loc[nop]) != VOIDmode
2708 		  && ! hard_reg_set_empty_p (this_alternative_set)
2709 		  /* It is common practice for constraints to use a
2710 		     class which does not have actually enough regs to
2711 		     hold the value (e.g. x86 AREG for mode requiring
2712 		     more one general reg).  Therefore we have 2
2713 		     conditions to check that the reload pseudo cannot
2714 		     hold the mode value.  */
2715 		  && (!targetm.hard_regno_mode_ok
2716 		      (ira_class_hard_regs[this_alternative][0],
2717 		       GET_MODE (*curr_id->operand_loc[nop])))
2718 		  /* The above condition is not enough as the first
2719 		     reg in ira_class_hard_regs can be not aligned for
2720 		     multi-words mode values.  */
2721 		  && (prohibited_class_reg_set_mode_p
2722 		      (this_alternative, this_alternative_set,
2723 		       GET_MODE (*curr_id->operand_loc[nop]))))
2724 		{
2725 		  if (lra_dump_file != NULL)
2726 		    fprintf (lra_dump_file,
2727 			     "            alt=%d: reload pseudo for op %d "
2728 			     "cannot hold the mode value -- refuse\n",
2729 			     nalt, nop);
2730 		  goto fail;
2731 		}
2732 
2733 	      /* Check strong discouragement of reload of non-constant
2734 		 into class THIS_ALTERNATIVE.  */
2735 	      if (! CONSTANT_P (op) && ! no_regs_p
2736 		  && (targetm.preferred_reload_class
2737 		      (op, this_alternative) == NO_REGS
2738 		      || (curr_static_id->operand[nop].type == OP_OUT
2739 			  && (targetm.preferred_output_reload_class
2740 			      (op, this_alternative) == NO_REGS))))
2741 		{
2742 		  if (lra_dump_file != NULL)
2743 		    fprintf (lra_dump_file,
2744 			     "            %d Non-prefered reload: reject+=%d\n",
2745 			     nop, LRA_MAX_REJECT);
2746 		  reject += LRA_MAX_REJECT;
2747 		}
2748 
2749 	      if (! (MEM_P (op) && offmemok)
2750 		  && ! (const_to_mem && constmemok))
2751 		{
2752 		  /* We prefer to reload pseudos over reloading other
2753 		     things, since such reloads may be able to be
2754 		     eliminated later.  So bump REJECT in other cases.
2755 		     Don't do this in the case where we are forcing a
2756 		     constant into memory and it will then win since
2757 		     we don't want to have a different alternative
2758 		     match then.  */
2759 		  if (! (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER))
2760 		    {
2761 		      if (lra_dump_file != NULL)
2762 			fprintf
2763 			  (lra_dump_file,
2764 			   "            %d Non-pseudo reload: reject+=2\n",
2765 			   nop);
2766 		      reject += 2;
2767 		    }
2768 
2769 		  if (! no_regs_p)
2770 		    reload_nregs
2771 		      += ira_reg_class_max_nregs[this_alternative][mode];
2772 
2773 		  if (SMALL_REGISTER_CLASS_P (this_alternative))
2774 		    {
2775 		      if (lra_dump_file != NULL)
2776 			fprintf
2777 			  (lra_dump_file,
2778 			   "            %d Small class reload: reject+=%d\n",
2779 			   nop, LRA_LOSER_COST_FACTOR / 2);
2780 		      reject += LRA_LOSER_COST_FACTOR / 2;
2781 		    }
2782 		}
2783 
2784 	      /* We are trying to spill pseudo into memory.  It is
2785 		 usually more costly than moving to a hard register
2786 		 although it might takes the same number of
2787 		 reloads.
2788 
2789 		 Non-pseudo spill may happen also.  Suppose a target allows both
2790 		 register and memory in the operand constraint alternatives,
2791 		 then it's typical that an eliminable register has a substition
2792 		 of "base + offset" which can either be reloaded by a simple
2793 		 "new_reg <= base + offset" which will match the register
2794 		 constraint, or a similar reg addition followed by further spill
2795 		 to and reload from memory which will match the memory
2796 		 constraint, but this memory spill will be much more costly
2797 		 usually.
2798 
2799 		 Code below increases the reject for both pseudo and non-pseudo
2800 		 spill.  */
2801 	      if (no_regs_p
2802 		  && !(MEM_P (op) && offmemok)
2803 		  && !(REG_P (op) && hard_regno[nop] < 0))
2804 		{
2805 		  if (lra_dump_file != NULL)
2806 		    fprintf
2807 		      (lra_dump_file,
2808 		       "            %d Spill %spseudo into memory: reject+=3\n",
2809 		       nop, REG_P (op) ? "" : "Non-");
2810 		  reject += 3;
2811 		  if (VECTOR_MODE_P (mode))
2812 		    {
2813 		      /* Spilling vectors into memory is usually more
2814 			 costly as they contain big values.  */
2815 		      if (lra_dump_file != NULL)
2816 			fprintf
2817 			  (lra_dump_file,
2818 			   "            %d Spill vector pseudo: reject+=2\n",
2819 			   nop);
2820 		      reject += 2;
2821 		    }
2822 		}
2823 
2824 	      /* When we use an operand requiring memory in given
2825 		 alternative, the insn should write *and* read the
2826 		 value to/from memory it is costly in comparison with
2827 		 an insn alternative which does not use memory
2828 		 (e.g. register or immediate operand).  We exclude
2829 		 memory operand for such case as we can satisfy the
2830 		 memory constraints by reloading address.  */
2831 	      if (no_regs_p && offmemok && !MEM_P (op))
2832 		{
2833 		  if (lra_dump_file != NULL)
2834 		    fprintf
2835 		      (lra_dump_file,
2836 		       "            Using memory insn operand %d: reject+=3\n",
2837 		       nop);
2838 		  reject += 3;
2839 		}
2840 
2841 	      /* If reload requires moving value through secondary
2842 		 memory, it will need one more insn at least.  */
2843 	      if (this_alternative != NO_REGS
2844 		  && REG_P (op) && (cl = get_reg_class (REGNO (op))) != NO_REGS
2845 		  && ((curr_static_id->operand[nop].type != OP_OUT
2846 		       && targetm.secondary_memory_needed (GET_MODE (op), cl,
2847 							   this_alternative))
2848 		      || (curr_static_id->operand[nop].type != OP_IN
2849 			  && (targetm.secondary_memory_needed
2850 			      (GET_MODE (op), this_alternative, cl)))))
2851 		losers++;
2852 
2853 	      if (MEM_P (op) && offmemok)
2854 		addr_losers++;
2855 	      else
2856 		{
2857 		  /* Input reloads can be inherited more often than
2858 		     output reloads can be removed, so penalize output
2859 		     reloads.  */
2860 		  if (!REG_P (op) || curr_static_id->operand[nop].type != OP_IN)
2861 		    {
2862 		      if (lra_dump_file != NULL)
2863 			fprintf
2864 			  (lra_dump_file,
2865 			   "            %d Non input pseudo reload: reject++\n",
2866 			   nop);
2867 		      reject++;
2868 		    }
2869 
2870 		  if (curr_static_id->operand[nop].type == OP_INOUT)
2871 		    {
2872 		      if (lra_dump_file != NULL)
2873 			fprintf
2874 			  (lra_dump_file,
2875 			   "            %d Input/Output reload: reject+=%d\n",
2876 			   nop, LRA_LOSER_COST_FACTOR);
2877 		      reject += LRA_LOSER_COST_FACTOR;
2878 		    }
2879 		}
2880 	    }
2881 
2882 	  if (early_clobber_p && ! scratch_p)
2883 	    {
2884 	      if (lra_dump_file != NULL)
2885 		fprintf (lra_dump_file,
2886 			 "            %d Early clobber: reject++\n", nop);
2887 	      reject++;
2888 	    }
2889 	  /* ??? We check early clobbers after processing all operands
2890 	     (see loop below) and there we update the costs more.
2891 	     Should we update the cost (may be approximately) here
2892 	     because of early clobber register reloads or it is a rare
2893 	     or non-important thing to be worth to do it.  */
2894 	  overall = (losers * LRA_LOSER_COST_FACTOR + reject
2895 		     - (addr_losers == losers ? static_reject : 0));
2896 	  if ((best_losers == 0 || losers != 0) && best_overall < overall)
2897             {
2898               if (lra_dump_file != NULL)
2899 		fprintf (lra_dump_file,
2900 			 "            alt=%d,overall=%d,losers=%d -- refuse\n",
2901 			 nalt, overall, losers);
2902               goto fail;
2903             }
2904 
2905 	  if (update_and_check_small_class_inputs (nop, this_alternative))
2906 	    {
2907 	      if (lra_dump_file != NULL)
2908 		fprintf (lra_dump_file,
2909 			 "            alt=%d, not enough small class regs -- refuse\n",
2910 			 nalt);
2911 	      goto fail;
2912 	    }
2913 	  curr_alt[nop] = this_alternative;
2914 	  COPY_HARD_REG_SET (curr_alt_set[nop], this_alternative_set);
2915 	  curr_alt_win[nop] = this_alternative_win;
2916 	  curr_alt_match_win[nop] = this_alternative_match_win;
2917 	  curr_alt_offmemok[nop] = this_alternative_offmemok;
2918 	  curr_alt_matches[nop] = this_alternative_matches;
2919 
2920 	  if (this_alternative_matches >= 0
2921 	      && !did_match && !this_alternative_win)
2922 	    curr_alt_win[this_alternative_matches] = false;
2923 
2924 	  if (early_clobber_p && operand_reg[nop] != NULL_RTX)
2925 	    early_clobbered_nops[early_clobbered_regs_num++] = nop;
2926 	}
2927 
2928       if (curr_insn_set != NULL_RTX && n_operands == 2
2929 	  /* Prevent processing non-move insns.  */
2930 	  && (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
2931 	      || SET_SRC (curr_insn_set) == no_subreg_reg_operand[1])
2932 	  && ((! curr_alt_win[0] && ! curr_alt_win[1]
2933 	       && REG_P (no_subreg_reg_operand[0])
2934 	       && REG_P (no_subreg_reg_operand[1])
2935 	       && (reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2936 		   || reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0])))
2937 	      || (! curr_alt_win[0] && curr_alt_win[1]
2938 		  && REG_P (no_subreg_reg_operand[1])
2939 		  /* Check that we reload memory not the memory
2940 		     address.  */
2941 		  && ! (curr_alt_offmemok[0]
2942 			&& MEM_P (no_subreg_reg_operand[0]))
2943 		  && reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0]))
2944 	      || (curr_alt_win[0] && ! curr_alt_win[1]
2945 		  && REG_P (no_subreg_reg_operand[0])
2946 		  /* Check that we reload memory not the memory
2947 		     address.  */
2948 		  && ! (curr_alt_offmemok[1]
2949 			&& MEM_P (no_subreg_reg_operand[1]))
2950 		  && reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2951 		  && (! CONST_POOL_OK_P (curr_operand_mode[1],
2952 					 no_subreg_reg_operand[1])
2953 		      || (targetm.preferred_reload_class
2954 			  (no_subreg_reg_operand[1],
2955 			   (enum reg_class) curr_alt[1]) != NO_REGS))
2956 		  /* If it is a result of recent elimination in move
2957 		     insn we can transform it into an add still by
2958 		     using this alternative.  */
2959 		  && GET_CODE (no_subreg_reg_operand[1]) != PLUS
2960 		  /* Likewise if the source has been replaced with an
2961 		     equivalent value.  This only happens once -- the reload
2962 		     will use the equivalent value instead of the register it
2963 		     replaces -- so there should be no danger of cycling.  */
2964 		  && !equiv_substition_p[1])))
2965 	{
2966 	  /* We have a move insn and a new reload insn will be similar
2967 	     to the current insn.  We should avoid such situation as
2968 	     it results in LRA cycling.  */
2969 	  if (lra_dump_file != NULL)
2970 	    fprintf (lra_dump_file,
2971 		     "            Cycle danger: overall += LRA_MAX_REJECT\n");
2972 	  overall += LRA_MAX_REJECT;
2973 	}
2974       ok_p = true;
2975       curr_alt_dont_inherit_ops_num = 0;
2976       for (nop = 0; nop < early_clobbered_regs_num; nop++)
2977 	{
2978 	  int i, j, clobbered_hard_regno, first_conflict_j, last_conflict_j;
2979 	  HARD_REG_SET temp_set;
2980 
2981 	  i = early_clobbered_nops[nop];
2982 	  if ((! curr_alt_win[i] && ! curr_alt_match_win[i])
2983 	      || hard_regno[i] < 0)
2984 	    continue;
2985 	  lra_assert (operand_reg[i] != NULL_RTX);
2986 	  clobbered_hard_regno = hard_regno[i];
2987 	  CLEAR_HARD_REG_SET (temp_set);
2988 	  add_to_hard_reg_set (&temp_set, biggest_mode[i], clobbered_hard_regno);
2989 	  first_conflict_j = last_conflict_j = -1;
2990 	  for (j = 0; j < n_operands; j++)
2991 	    if (j == i
2992 		/* We don't want process insides of match_operator and
2993 		   match_parallel because otherwise we would process
2994 		   their operands once again generating a wrong
2995 		   code.  */
2996 		|| curr_static_id->operand[j].is_operator)
2997 	      continue;
2998 	    else if ((curr_alt_matches[j] == i && curr_alt_match_win[j])
2999 		     || (curr_alt_matches[i] == j && curr_alt_match_win[i]))
3000 	      continue;
3001 	    /* If we don't reload j-th operand, check conflicts.  */
3002 	    else if ((curr_alt_win[j] || curr_alt_match_win[j])
3003 		     && uses_hard_regs_p (*curr_id->operand_loc[j], temp_set))
3004 	      {
3005 		if (first_conflict_j < 0)
3006 		  first_conflict_j = j;
3007 		last_conflict_j = j;
3008 		/* Both the earlyclobber operand and conflicting operand
3009 		   cannot both be user defined hard registers.  */
3010 		if (HARD_REGISTER_P (operand_reg[i])
3011 		    && REG_USERVAR_P (operand_reg[i])
3012 		    && operand_reg[j] != NULL_RTX
3013 		    && HARD_REGISTER_P (operand_reg[j])
3014 		    && REG_USERVAR_P (operand_reg[j]))
3015 		  {
3016 		    /* For asm, let curr_insn_transform diagnose it.  */
3017 		    if (INSN_CODE (curr_insn) < 0)
3018 		      return false;
3019 		    fatal_insn ("unable to generate reloads for "
3020 				"impossible constraints:", curr_insn);
3021 		  }
3022 	      }
3023 	  if (last_conflict_j < 0)
3024 	    continue;
3025 
3026 	  /* If an earlyclobber operand conflicts with another non-matching
3027 	     operand (ie, they have been assigned the same hard register),
3028 	     then it is better to reload the other operand, as there may
3029 	     exist yet another operand with a matching constraint associated
3030 	     with the earlyclobber operand.  However, if one of the operands
3031 	     is an explicit use of a hard register, then we must reload the
3032 	     other non-hard register operand.  */
3033 	  if (HARD_REGISTER_P (operand_reg[i])
3034 	      || (first_conflict_j == last_conflict_j
3035 		  && operand_reg[last_conflict_j] != NULL_RTX
3036 		  && !curr_alt_match_win[last_conflict_j]
3037 		  && !HARD_REGISTER_P (operand_reg[last_conflict_j])))
3038 	    {
3039 	      curr_alt_win[last_conflict_j] = false;
3040 	      curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++]
3041 		= last_conflict_j;
3042 	      losers++;
3043 	      if (lra_dump_file != NULL)
3044 		fprintf
3045 		  (lra_dump_file,
3046 		   "            %d Conflict early clobber reload: reject--\n",
3047 		   i);
3048 	    }
3049 	  else
3050 	    {
3051 	      /* We need to reload early clobbered register and the
3052 		 matched registers.  */
3053 	      for (j = 0; j < n_operands; j++)
3054 		if (curr_alt_matches[j] == i)
3055 		  {
3056 		    curr_alt_match_win[j] = false;
3057 		    losers++;
3058 		    overall += LRA_LOSER_COST_FACTOR;
3059 		  }
3060 	      if (! curr_alt_match_win[i])
3061 		curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++] = i;
3062 	      else
3063 		{
3064 		  /* Remember pseudos used for match reloads are never
3065 		     inherited.  */
3066 		  lra_assert (curr_alt_matches[i] >= 0);
3067 		  curr_alt_win[curr_alt_matches[i]] = false;
3068 		}
3069 	      curr_alt_win[i] = curr_alt_match_win[i] = false;
3070 	      losers++;
3071 	      if (lra_dump_file != NULL)
3072 		fprintf
3073 		  (lra_dump_file,
3074 		   "            %d Matched conflict early clobber reloads: "
3075 		   "reject--\n",
3076 		   i);
3077 	    }
3078 	  /* Early clobber was already reflected in REJECT. */
3079 	  if (!matching_early_clobber[i])
3080 	    {
3081 	      lra_assert (reject > 0);
3082 	      reject--;
3083 	      matching_early_clobber[i] = 1;
3084 	    }
3085 	  overall += LRA_LOSER_COST_FACTOR - 1;
3086 	}
3087       if (lra_dump_file != NULL)
3088 	fprintf (lra_dump_file, "          alt=%d,overall=%d,losers=%d,rld_nregs=%d\n",
3089 		 nalt, overall, losers, reload_nregs);
3090 
3091       /* If this alternative can be made to work by reloading, and it
3092 	 needs less reloading than the others checked so far, record
3093 	 it as the chosen goal for reloading.  */
3094       if ((best_losers != 0 && losers == 0)
3095 	  || (((best_losers == 0 && losers == 0)
3096 	       || (best_losers != 0 && losers != 0))
3097 	      && (best_overall > overall
3098 		  || (best_overall == overall
3099 		      /* If the cost of the reloads is the same,
3100 			 prefer alternative which requires minimal
3101 			 number of reload regs.  */
3102 		      && (reload_nregs < best_reload_nregs
3103 			  || (reload_nregs == best_reload_nregs
3104 			      && (best_reload_sum < reload_sum
3105 				  || (best_reload_sum == reload_sum
3106 				      && nalt < goal_alt_number))))))))
3107 	{
3108 	  for (nop = 0; nop < n_operands; nop++)
3109 	    {
3110 	      goal_alt_win[nop] = curr_alt_win[nop];
3111 	      goal_alt_match_win[nop] = curr_alt_match_win[nop];
3112 	      goal_alt_matches[nop] = curr_alt_matches[nop];
3113 	      goal_alt[nop] = curr_alt[nop];
3114 	      goal_alt_offmemok[nop] = curr_alt_offmemok[nop];
3115 	    }
3116 	  goal_alt_dont_inherit_ops_num = curr_alt_dont_inherit_ops_num;
3117 	  for (nop = 0; nop < curr_alt_dont_inherit_ops_num; nop++)
3118 	    goal_alt_dont_inherit_ops[nop] = curr_alt_dont_inherit_ops[nop];
3119 	  goal_alt_swapped = curr_swapped;
3120 	  best_overall = overall;
3121 	  best_losers = losers;
3122 	  best_reload_nregs = reload_nregs;
3123 	  best_reload_sum = reload_sum;
3124 	  goal_alt_number = nalt;
3125 	}
3126       if (losers == 0)
3127 	/* Everything is satisfied.  Do not process alternatives
3128 	   anymore.  */
3129 	break;
3130     fail:
3131       ;
3132     }
3133   return ok_p;
3134 }
3135 
3136 /* Make reload base reg from address AD.  */
3137 static rtx
base_to_reg(struct address_info * ad)3138 base_to_reg (struct address_info *ad)
3139 {
3140   enum reg_class cl;
3141   int code = -1;
3142   rtx new_inner = NULL_RTX;
3143   rtx new_reg = NULL_RTX;
3144   rtx_insn *insn;
3145   rtx_insn *last_insn = get_last_insn();
3146 
3147   lra_assert (ad->disp == ad->disp_term);
3148   cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
3149                        get_index_code (ad));
3150   new_reg = lra_create_new_reg (GET_MODE (*ad->base), NULL_RTX,
3151                                 cl, "base");
3152   new_inner = simplify_gen_binary (PLUS, GET_MODE (new_reg), new_reg,
3153                                    ad->disp_term == NULL
3154                                    ? const0_rtx
3155                                    : *ad->disp_term);
3156   if (!valid_address_p (ad->mode, new_inner, ad->as))
3157     return NULL_RTX;
3158   insn = emit_insn (gen_rtx_SET (new_reg, *ad->base));
3159   code = recog_memoized (insn);
3160   if (code < 0)
3161     {
3162       delete_insns_since (last_insn);
3163       return NULL_RTX;
3164     }
3165 
3166   return new_inner;
3167 }
3168 
3169 /* Make reload base reg + DISP from address AD.  Return the new pseudo.  */
3170 static rtx
base_plus_disp_to_reg(struct address_info * ad,rtx disp)3171 base_plus_disp_to_reg (struct address_info *ad, rtx disp)
3172 {
3173   enum reg_class cl;
3174   rtx new_reg;
3175 
3176   lra_assert (ad->base == ad->base_term);
3177   cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
3178 		       get_index_code (ad));
3179   new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
3180 				cl, "base + disp");
3181   lra_emit_add (new_reg, *ad->base_term, disp);
3182   return new_reg;
3183 }
3184 
3185 /* Make reload of index part of address AD.  Return the new
3186    pseudo.  */
3187 static rtx
index_part_to_reg(struct address_info * ad)3188 index_part_to_reg (struct address_info *ad)
3189 {
3190   rtx new_reg;
3191 
3192   new_reg = lra_create_new_reg (GET_MODE (*ad->index), NULL_RTX,
3193 				INDEX_REG_CLASS, "index term");
3194   expand_mult (GET_MODE (*ad->index), *ad->index_term,
3195 	       GEN_INT (get_index_scale (ad)), new_reg, 1);
3196   return new_reg;
3197 }
3198 
3199 /* Return true if we can add a displacement to address AD, even if that
3200    makes the address invalid.  The fix-up code requires any new address
3201    to be the sum of the BASE_TERM, INDEX and DISP_TERM fields.  */
3202 static bool
can_add_disp_p(struct address_info * ad)3203 can_add_disp_p (struct address_info *ad)
3204 {
3205   return (!ad->autoinc_p
3206 	  && ad->segment == NULL
3207 	  && ad->base == ad->base_term
3208 	  && ad->disp == ad->disp_term);
3209 }
3210 
3211 /* Make equiv substitution in address AD.  Return true if a substitution
3212    was made.  */
3213 static bool
equiv_address_substitution(struct address_info * ad)3214 equiv_address_substitution (struct address_info *ad)
3215 {
3216   rtx base_reg, new_base_reg, index_reg, new_index_reg, *base_term, *index_term;
3217   poly_int64 disp;
3218   HOST_WIDE_INT scale;
3219   bool change_p;
3220 
3221   base_term = strip_subreg (ad->base_term);
3222   if (base_term == NULL)
3223     base_reg = new_base_reg = NULL_RTX;
3224   else
3225     {
3226       base_reg = *base_term;
3227       new_base_reg = get_equiv_with_elimination (base_reg, curr_insn);
3228     }
3229   index_term = strip_subreg (ad->index_term);
3230   if (index_term == NULL)
3231     index_reg = new_index_reg = NULL_RTX;
3232   else
3233     {
3234       index_reg = *index_term;
3235       new_index_reg = get_equiv_with_elimination (index_reg, curr_insn);
3236     }
3237   if (base_reg == new_base_reg && index_reg == new_index_reg)
3238     return false;
3239   disp = 0;
3240   change_p = false;
3241   if (lra_dump_file != NULL)
3242     {
3243       fprintf (lra_dump_file, "Changing address in insn %d ",
3244 	       INSN_UID (curr_insn));
3245       dump_value_slim (lra_dump_file, *ad->outer, 1);
3246     }
3247   if (base_reg != new_base_reg)
3248     {
3249       poly_int64 offset;
3250       if (REG_P (new_base_reg))
3251 	{
3252 	  *base_term = new_base_reg;
3253 	  change_p = true;
3254 	}
3255       else if (GET_CODE (new_base_reg) == PLUS
3256 	       && REG_P (XEXP (new_base_reg, 0))
3257 	       && poly_int_rtx_p (XEXP (new_base_reg, 1), &offset)
3258 	       && can_add_disp_p (ad))
3259 	{
3260 	  disp += offset;
3261 	  *base_term = XEXP (new_base_reg, 0);
3262 	  change_p = true;
3263 	}
3264       if (ad->base_term2 != NULL)
3265 	*ad->base_term2 = *ad->base_term;
3266     }
3267   if (index_reg != new_index_reg)
3268     {
3269       poly_int64 offset;
3270       if (REG_P (new_index_reg))
3271 	{
3272 	  *index_term = new_index_reg;
3273 	  change_p = true;
3274 	}
3275       else if (GET_CODE (new_index_reg) == PLUS
3276 	       && REG_P (XEXP (new_index_reg, 0))
3277 	       && poly_int_rtx_p (XEXP (new_index_reg, 1), &offset)
3278 	       && can_add_disp_p (ad)
3279 	       && (scale = get_index_scale (ad)))
3280 	{
3281 	  disp += offset * scale;
3282 	  *index_term = XEXP (new_index_reg, 0);
3283 	  change_p = true;
3284 	}
3285     }
3286   if (maybe_ne (disp, 0))
3287     {
3288       if (ad->disp != NULL)
3289 	*ad->disp = plus_constant (GET_MODE (*ad->inner), *ad->disp, disp);
3290       else
3291 	{
3292 	  *ad->inner = plus_constant (GET_MODE (*ad->inner), *ad->inner, disp);
3293 	  update_address (ad);
3294 	}
3295       change_p = true;
3296     }
3297   if (lra_dump_file != NULL)
3298     {
3299       if (! change_p)
3300 	fprintf (lra_dump_file, " -- no change\n");
3301       else
3302 	{
3303 	  fprintf (lra_dump_file, " on equiv ");
3304 	  dump_value_slim (lra_dump_file, *ad->outer, 1);
3305 	  fprintf (lra_dump_file, "\n");
3306 	}
3307     }
3308   return change_p;
3309 }
3310 
3311 /* Major function to make reloads for an address in operand NOP or
3312    check its correctness (If CHECK_ONLY_P is true). The supported
3313    cases are:
3314 
3315    1) an address that existed before LRA started, at which point it
3316    must have been valid.  These addresses are subject to elimination
3317    and may have become invalid due to the elimination offset being out
3318    of range.
3319 
3320    2) an address created by forcing a constant to memory
3321    (force_const_to_mem).  The initial form of these addresses might
3322    not be valid, and it is this function's job to make them valid.
3323 
3324    3) a frame address formed from a register and a (possibly zero)
3325    constant offset.  As above, these addresses might not be valid and
3326    this function must make them so.
3327 
3328    Add reloads to the lists *BEFORE and *AFTER.  We might need to add
3329    reloads to *AFTER because of inc/dec, {pre, post} modify in the
3330    address.  Return true for any RTL change.
3331 
3332    The function is a helper function which does not produce all
3333    transformations (when CHECK_ONLY_P is false) which can be
3334    necessary.  It does just basic steps.  To do all necessary
3335    transformations use function process_address.  */
3336 static bool
process_address_1(int nop,bool check_only_p,rtx_insn ** before,rtx_insn ** after)3337 process_address_1 (int nop, bool check_only_p,
3338 		   rtx_insn **before, rtx_insn **after)
3339 {
3340   struct address_info ad;
3341   rtx new_reg;
3342   HOST_WIDE_INT scale;
3343   rtx op = *curr_id->operand_loc[nop];
3344   const char *constraint = curr_static_id->operand[nop].constraint;
3345   enum constraint_num cn = lookup_constraint (constraint);
3346   bool change_p = false;
3347 
3348   if (MEM_P (op)
3349       && GET_MODE (op) == BLKmode
3350       && GET_CODE (XEXP (op, 0)) == SCRATCH)
3351     return false;
3352 
3353   if (insn_extra_address_constraint (cn)
3354       /* When we find an asm operand with an address constraint that
3355 	 doesn't satisfy address_operand to begin with, we clear
3356 	 is_address, so that we don't try to make a non-address fit.
3357 	 If the asm statement got this far, it's because other
3358 	 constraints are available, and we'll use them, disregarding
3359 	 the unsatisfiable address ones.  */
3360       && curr_static_id->operand[nop].is_address)
3361     decompose_lea_address (&ad, curr_id->operand_loc[nop]);
3362   /* Do not attempt to decompose arbitrary addresses generated by combine
3363      for asm operands with loose constraints, e.g 'X'.  */
3364   else if (MEM_P (op)
3365 	   && !(INSN_CODE (curr_insn) < 0
3366 		&& get_constraint_type (cn) == CT_FIXED_FORM
3367 	        && constraint_satisfied_p (op, cn)))
3368     decompose_mem_address (&ad, op);
3369   else if (GET_CODE (op) == SUBREG
3370 	   && MEM_P (SUBREG_REG (op)))
3371     decompose_mem_address (&ad, SUBREG_REG (op));
3372   else
3373     return false;
3374   /* If INDEX_REG_CLASS is assigned to base_term already and isn't to
3375      index_term, swap them so to avoid assigning INDEX_REG_CLASS to both
3376      when INDEX_REG_CLASS is a single register class.  */
3377   if (ad.base_term != NULL
3378       && ad.index_term != NULL
3379       && ira_class_hard_regs_num[INDEX_REG_CLASS] == 1
3380       && REG_P (*ad.base_term)
3381       && REG_P (*ad.index_term)
3382       && in_class_p (*ad.base_term, INDEX_REG_CLASS, NULL)
3383       && ! in_class_p (*ad.index_term, INDEX_REG_CLASS, NULL))
3384     {
3385       std::swap (ad.base, ad.index);
3386       std::swap (ad.base_term, ad.index_term);
3387     }
3388   if (! check_only_p)
3389     change_p = equiv_address_substitution (&ad);
3390   if (ad.base_term != NULL
3391       && (process_addr_reg
3392 	  (ad.base_term, check_only_p, before,
3393 	   (ad.autoinc_p
3394 	    && !(REG_P (*ad.base_term)
3395 		 && find_regno_note (curr_insn, REG_DEAD,
3396 				     REGNO (*ad.base_term)) != NULL_RTX)
3397 	    ? after : NULL),
3398 	   base_reg_class (ad.mode, ad.as, ad.base_outer_code,
3399 			   get_index_code (&ad)))))
3400     {
3401       change_p = true;
3402       if (ad.base_term2 != NULL)
3403 	*ad.base_term2 = *ad.base_term;
3404     }
3405   if (ad.index_term != NULL
3406       && process_addr_reg (ad.index_term, check_only_p,
3407 			   before, NULL, INDEX_REG_CLASS))
3408     change_p = true;
3409 
3410   /* Target hooks sometimes don't treat extra-constraint addresses as
3411      legitimate address_operands, so handle them specially.  */
3412   if (insn_extra_address_constraint (cn)
3413       && satisfies_address_constraint_p (&ad, cn))
3414     return change_p;
3415 
3416   if (check_only_p)
3417     return change_p;
3418 
3419   /* There are three cases where the shape of *AD.INNER may now be invalid:
3420 
3421      1) the original address was valid, but either elimination or
3422      equiv_address_substitution was applied and that made
3423      the address invalid.
3424 
3425      2) the address is an invalid symbolic address created by
3426      force_const_to_mem.
3427 
3428      3) the address is a frame address with an invalid offset.
3429 
3430      4) the address is a frame address with an invalid base.
3431 
3432      All these cases involve a non-autoinc address, so there is no
3433      point revalidating other types.  */
3434   if (ad.autoinc_p || valid_address_p (&ad))
3435     return change_p;
3436 
3437   /* Any index existed before LRA started, so we can assume that the
3438      presence and shape of the index is valid.  */
3439   push_to_sequence (*before);
3440   lra_assert (ad.disp == ad.disp_term);
3441   if (ad.base == NULL)
3442     {
3443       if (ad.index == NULL)
3444 	{
3445 	  rtx_insn *insn;
3446 	  rtx_insn *last = get_last_insn ();
3447 	  int code = -1;
3448 	  enum reg_class cl = base_reg_class (ad.mode, ad.as,
3449 					      SCRATCH, SCRATCH);
3450 	  rtx addr = *ad.inner;
3451 
3452 	  new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "addr");
3453 	  if (HAVE_lo_sum)
3454 	    {
3455 	      /* addr => lo_sum (new_base, addr), case (2) above.  */
3456 	      insn = emit_insn (gen_rtx_SET
3457 				(new_reg,
3458 				 gen_rtx_HIGH (Pmode, copy_rtx (addr))));
3459 	      code = recog_memoized (insn);
3460 	      if (code >= 0)
3461 		{
3462 		  *ad.inner = gen_rtx_LO_SUM (Pmode, new_reg, addr);
3463 		  if (! valid_address_p (ad.mode, *ad.outer, ad.as))
3464 		    {
3465 		      /* Try to put lo_sum into register.  */
3466 		      insn = emit_insn (gen_rtx_SET
3467 					(new_reg,
3468 					 gen_rtx_LO_SUM (Pmode, new_reg, addr)));
3469 		      code = recog_memoized (insn);
3470 		      if (code >= 0)
3471 			{
3472 			  *ad.inner = new_reg;
3473 			  if (! valid_address_p (ad.mode, *ad.outer, ad.as))
3474 			    {
3475 			      *ad.inner = addr;
3476 			      code = -1;
3477 			    }
3478 			}
3479 
3480 		    }
3481 		}
3482 	      if (code < 0)
3483 		delete_insns_since (last);
3484 	    }
3485 
3486 	  if (code < 0)
3487 	    {
3488 	      /* addr => new_base, case (2) above.  */
3489 	      lra_emit_move (new_reg, addr);
3490 
3491 	      for (insn = last == NULL_RTX ? get_insns () : NEXT_INSN (last);
3492 		   insn != NULL_RTX;
3493 		   insn = NEXT_INSN (insn))
3494 		if (recog_memoized (insn) < 0)
3495 		  break;
3496 	      if (insn != NULL_RTX)
3497 		{
3498 		  /* Do nothing if we cannot generate right insns.
3499 		     This is analogous to reload pass behavior.  */
3500 		  delete_insns_since (last);
3501 		  end_sequence ();
3502 		  return false;
3503 		}
3504 	      *ad.inner = new_reg;
3505 	    }
3506 	}
3507       else
3508 	{
3509 	  /* index * scale + disp => new base + index * scale,
3510 	     case (1) above.  */
3511 	  enum reg_class cl = base_reg_class (ad.mode, ad.as, PLUS,
3512 					      GET_CODE (*ad.index));
3513 
3514 	  lra_assert (INDEX_REG_CLASS != NO_REGS);
3515 	  new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "disp");
3516 	  lra_emit_move (new_reg, *ad.disp);
3517 	  *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3518 					   new_reg, *ad.index);
3519 	}
3520     }
3521   else if (ad.index == NULL)
3522     {
3523       int regno;
3524       enum reg_class cl;
3525       rtx set;
3526       rtx_insn *insns, *last_insn;
3527       /* Try to reload base into register only if the base is invalid
3528          for the address but with valid offset, case (4) above.  */
3529       start_sequence ();
3530       new_reg = base_to_reg (&ad);
3531 
3532       /* base + disp => new base, cases (1) and (3) above.  */
3533       /* Another option would be to reload the displacement into an
3534 	 index register.  However, postreload has code to optimize
3535 	 address reloads that have the same base and different
3536 	 displacements, so reloading into an index register would
3537 	 not necessarily be a win.  */
3538       if (new_reg == NULL_RTX)
3539 	{
3540 	  /* See if the target can split the displacement into a
3541 	     legitimate new displacement from a local anchor.  */
3542 	  gcc_assert (ad.disp == ad.disp_term);
3543 	  poly_int64 orig_offset;
3544 	  rtx offset1, offset2;
3545 	  if (poly_int_rtx_p (*ad.disp, &orig_offset)
3546 	      && targetm.legitimize_address_displacement (&offset1, &offset2,
3547 							  orig_offset,
3548 							  ad.mode))
3549 	    {
3550 	      new_reg = base_plus_disp_to_reg (&ad, offset1);
3551 	      new_reg = gen_rtx_PLUS (GET_MODE (new_reg), new_reg, offset2);
3552 	    }
3553 	  else
3554 	    new_reg = base_plus_disp_to_reg (&ad, *ad.disp);
3555 	}
3556       insns = get_insns ();
3557       last_insn = get_last_insn ();
3558       /* If we generated at least two insns, try last insn source as
3559 	 an address.  If we succeed, we generate one less insn.  */
3560       if (REG_P (new_reg)
3561 	  && last_insn != insns
3562 	  && (set = single_set (last_insn)) != NULL_RTX
3563 	  && GET_CODE (SET_SRC (set)) == PLUS
3564 	  && REG_P (XEXP (SET_SRC (set), 0))
3565 	  && CONSTANT_P (XEXP (SET_SRC (set), 1)))
3566 	{
3567 	  *ad.inner = SET_SRC (set);
3568 	  if (valid_address_p (ad.mode, *ad.outer, ad.as))
3569 	    {
3570 	      *ad.base_term = XEXP (SET_SRC (set), 0);
3571 	      *ad.disp_term = XEXP (SET_SRC (set), 1);
3572 	      cl = base_reg_class (ad.mode, ad.as, ad.base_outer_code,
3573 				   get_index_code (&ad));
3574 	      regno = REGNO (*ad.base_term);
3575 	      if (regno >= FIRST_PSEUDO_REGISTER
3576 		  && cl != lra_get_allocno_class (regno))
3577 		lra_change_class (regno, cl, "      Change to", true);
3578 	      new_reg = SET_SRC (set);
3579 	      delete_insns_since (PREV_INSN (last_insn));
3580 	    }
3581 	}
3582       end_sequence ();
3583       emit_insn (insns);
3584       *ad.inner = new_reg;
3585     }
3586   else if (ad.disp_term != NULL)
3587     {
3588       /* base + scale * index + disp => new base + scale * index,
3589 	 case (1) above.  */
3590       gcc_assert (ad.disp == ad.disp_term);
3591       new_reg = base_plus_disp_to_reg (&ad, *ad.disp);
3592       *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3593 				       new_reg, *ad.index);
3594     }
3595   else if ((scale = get_index_scale (&ad)) == 1)
3596     {
3597       /* The last transformation to one reg will be made in
3598 	 curr_insn_transform function.  */
3599       end_sequence ();
3600       return false;
3601     }
3602   else if (scale != 0)
3603     {
3604       /* base + scale * index => base + new_reg,
3605 	 case (1) above.
3606       Index part of address may become invalid.  For example, we
3607       changed pseudo on the equivalent memory and a subreg of the
3608       pseudo onto the memory of different mode for which the scale is
3609       prohibitted.  */
3610       new_reg = index_part_to_reg (&ad);
3611       *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3612 				       *ad.base_term, new_reg);
3613     }
3614   else
3615     {
3616       enum reg_class cl = base_reg_class (ad.mode, ad.as,
3617 					  SCRATCH, SCRATCH);
3618       rtx addr = *ad.inner;
3619 
3620       new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "addr");
3621       /* addr => new_base.  */
3622       lra_emit_move (new_reg, addr);
3623       *ad.inner = new_reg;
3624     }
3625   *before = get_insns ();
3626   end_sequence ();
3627   return true;
3628 }
3629 
3630 /* If CHECK_ONLY_P is false, do address reloads until it is necessary.
3631    Use process_address_1 as a helper function.  Return true for any
3632    RTL changes.
3633 
3634    If CHECK_ONLY_P is true, just check address correctness.  Return
3635    false if the address correct.  */
3636 static bool
process_address(int nop,bool check_only_p,rtx_insn ** before,rtx_insn ** after)3637 process_address (int nop, bool check_only_p,
3638 		 rtx_insn **before, rtx_insn **after)
3639 {
3640   bool res = false;
3641 
3642   while (process_address_1 (nop, check_only_p, before, after))
3643     {
3644       if (check_only_p)
3645 	return true;
3646       res = true;
3647     }
3648   return res;
3649 }
3650 
3651 /* Emit insns to reload VALUE into a new register.  VALUE is an
3652    auto-increment or auto-decrement RTX whose operand is a register or
3653    memory location; so reloading involves incrementing that location.
3654    IN is either identical to VALUE, or some cheaper place to reload
3655    value being incremented/decremented from.
3656 
3657    INC_AMOUNT is the number to increment or decrement by (always
3658    positive and ignored for POST_MODIFY/PRE_MODIFY).
3659 
3660    Return pseudo containing the result.	 */
3661 static rtx
emit_inc(enum reg_class new_rclass,rtx in,rtx value,poly_int64 inc_amount)3662 emit_inc (enum reg_class new_rclass, rtx in, rtx value, poly_int64 inc_amount)
3663 {
3664   /* REG or MEM to be copied and incremented.  */
3665   rtx incloc = XEXP (value, 0);
3666   /* Nonzero if increment after copying.  */
3667   int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC
3668 	      || GET_CODE (value) == POST_MODIFY);
3669   rtx_insn *last;
3670   rtx inc;
3671   rtx_insn *add_insn;
3672   int code;
3673   rtx real_in = in == value ? incloc : in;
3674   rtx result;
3675   bool plus_p = true;
3676 
3677   if (GET_CODE (value) == PRE_MODIFY || GET_CODE (value) == POST_MODIFY)
3678     {
3679       lra_assert (GET_CODE (XEXP (value, 1)) == PLUS
3680 		  || GET_CODE (XEXP (value, 1)) == MINUS);
3681       lra_assert (rtx_equal_p (XEXP (XEXP (value, 1), 0), XEXP (value, 0)));
3682       plus_p = GET_CODE (XEXP (value, 1)) == PLUS;
3683       inc = XEXP (XEXP (value, 1), 1);
3684     }
3685   else
3686     {
3687       if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
3688 	inc_amount = -inc_amount;
3689 
3690       inc = gen_int_mode (inc_amount, GET_MODE (value));
3691     }
3692 
3693   if (! post && REG_P (incloc))
3694     result = incloc;
3695   else
3696     result = lra_create_new_reg (GET_MODE (value), value, new_rclass,
3697 				 "INC/DEC result");
3698 
3699   if (real_in != result)
3700     {
3701       /* First copy the location to the result register.  */
3702       lra_assert (REG_P (result));
3703       emit_insn (gen_move_insn (result, real_in));
3704     }
3705 
3706   /* We suppose that there are insns to add/sub with the constant
3707      increment permitted in {PRE/POST)_{DEC/INC/MODIFY}.  At least the
3708      old reload worked with this assumption.  If the assumption
3709      becomes wrong, we should use approach in function
3710      base_plus_disp_to_reg.  */
3711   if (in == value)
3712     {
3713       /* See if we can directly increment INCLOC.  */
3714       last = get_last_insn ();
3715       add_insn = emit_insn (plus_p
3716 			    ? gen_add2_insn (incloc, inc)
3717 			    : gen_sub2_insn (incloc, inc));
3718 
3719       code = recog_memoized (add_insn);
3720       if (code >= 0)
3721 	{
3722 	  if (! post && result != incloc)
3723 	    emit_insn (gen_move_insn (result, incloc));
3724 	  return result;
3725 	}
3726       delete_insns_since (last);
3727     }
3728 
3729   /* If couldn't do the increment directly, must increment in RESULT.
3730      The way we do this depends on whether this is pre- or
3731      post-increment.  For pre-increment, copy INCLOC to the reload
3732      register, increment it there, then save back.  */
3733   if (! post)
3734     {
3735       if (real_in != result)
3736 	emit_insn (gen_move_insn (result, real_in));
3737       if (plus_p)
3738 	emit_insn (gen_add2_insn (result, inc));
3739       else
3740 	emit_insn (gen_sub2_insn (result, inc));
3741       if (result != incloc)
3742 	emit_insn (gen_move_insn (incloc, result));
3743     }
3744   else
3745     {
3746       /* Post-increment.
3747 
3748 	 Because this might be a jump insn or a compare, and because
3749 	 RESULT may not be available after the insn in an input
3750 	 reload, we must do the incrementing before the insn being
3751 	 reloaded for.
3752 
3753 	 We have already copied IN to RESULT.  Increment the copy in
3754 	 RESULT, save that back, then decrement RESULT so it has
3755 	 the original value.  */
3756       if (plus_p)
3757 	emit_insn (gen_add2_insn (result, inc));
3758       else
3759 	emit_insn (gen_sub2_insn (result, inc));
3760       emit_insn (gen_move_insn (incloc, result));
3761       /* Restore non-modified value for the result.  We prefer this
3762 	 way because it does not require an additional hard
3763 	 register.  */
3764       if (plus_p)
3765 	{
3766 	  poly_int64 offset;
3767 	  if (poly_int_rtx_p (inc, &offset))
3768 	    emit_insn (gen_add2_insn (result,
3769 				      gen_int_mode (-offset,
3770 						    GET_MODE (result))));
3771 	  else
3772 	    emit_insn (gen_sub2_insn (result, inc));
3773 	}
3774       else
3775 	emit_insn (gen_add2_insn (result, inc));
3776     }
3777   return result;
3778 }
3779 
3780 /* Return true if the current move insn does not need processing as we
3781    already know that it satisfies its constraints.  */
3782 static bool
simple_move_p(void)3783 simple_move_p (void)
3784 {
3785   rtx dest, src;
3786   enum reg_class dclass, sclass;
3787 
3788   lra_assert (curr_insn_set != NULL_RTX);
3789   dest = SET_DEST (curr_insn_set);
3790   src = SET_SRC (curr_insn_set);
3791 
3792   /* If the instruction has multiple sets we need to process it even if it
3793      is single_set.  This can happen if one or more of the SETs are dead.
3794      See PR73650.  */
3795   if (multiple_sets (curr_insn))
3796     return false;
3797 
3798   return ((dclass = get_op_class (dest)) != NO_REGS
3799 	  && (sclass = get_op_class (src)) != NO_REGS
3800 	  /* The backend guarantees that register moves of cost 2
3801 	     never need reloads.  */
3802 	  && targetm.register_move_cost (GET_MODE (src), sclass, dclass) == 2);
3803  }
3804 
3805 /* Swap operands NOP and NOP + 1. */
3806 static inline void
swap_operands(int nop)3807 swap_operands (int nop)
3808 {
3809   std::swap (curr_operand_mode[nop], curr_operand_mode[nop + 1]);
3810   std::swap (original_subreg_reg_mode[nop], original_subreg_reg_mode[nop + 1]);
3811   std::swap (*curr_id->operand_loc[nop], *curr_id->operand_loc[nop + 1]);
3812   std::swap (equiv_substition_p[nop], equiv_substition_p[nop + 1]);
3813   /* Swap the duplicates too.  */
3814   lra_update_dup (curr_id, nop);
3815   lra_update_dup (curr_id, nop + 1);
3816 }
3817 
3818 /* Main entry point of the constraint code: search the body of the
3819    current insn to choose the best alternative.  It is mimicking insn
3820    alternative cost calculation model of former reload pass.  That is
3821    because machine descriptions were written to use this model.  This
3822    model can be changed in future.  Make commutative operand exchange
3823    if it is chosen.
3824 
3825    if CHECK_ONLY_P is false, do RTL changes to satisfy the
3826    constraints.  Return true if any change happened during function
3827    call.
3828 
3829    If CHECK_ONLY_P is true then don't do any transformation.  Just
3830    check that the insn satisfies all constraints.  If the insn does
3831    not satisfy any constraint, return true.  */
3832 static bool
curr_insn_transform(bool check_only_p)3833 curr_insn_transform (bool check_only_p)
3834 {
3835   int i, j, k;
3836   int n_operands;
3837   int n_alternatives;
3838   int n_outputs;
3839   int commutative;
3840   signed char goal_alt_matched[MAX_RECOG_OPERANDS][MAX_RECOG_OPERANDS];
3841   signed char match_inputs[MAX_RECOG_OPERANDS + 1];
3842   signed char outputs[MAX_RECOG_OPERANDS + 1];
3843   rtx_insn *before, *after;
3844   bool alt_p = false;
3845   /* Flag that the insn has been changed through a transformation.  */
3846   bool change_p;
3847   bool sec_mem_p;
3848   bool use_sec_mem_p;
3849   int max_regno_before;
3850   int reused_alternative_num;
3851 
3852   curr_insn_set = single_set (curr_insn);
3853   if (curr_insn_set != NULL_RTX && simple_move_p ())
3854     {
3855       /* We assume that the corresponding insn alternative has no
3856 	 earlier clobbers.  If it is not the case, don't define move
3857 	 cost equal to 2 for the corresponding register classes.  */
3858       lra_set_used_insn_alternative (curr_insn, LRA_NON_CLOBBERED_ALT);
3859       return false;
3860     }
3861 
3862   no_input_reloads_p = no_output_reloads_p = false;
3863   goal_alt_number = -1;
3864   change_p = sec_mem_p = false;
3865   /* JUMP_INSNs and CALL_INSNs are not allowed to have any output
3866      reloads; neither are insns that SET cc0.  Insns that use CC0 are
3867      not allowed to have any input reloads.  */
3868   if (JUMP_P (curr_insn) || CALL_P (curr_insn))
3869     no_output_reloads_p = true;
3870 
3871   if (HAVE_cc0 && reg_referenced_p (cc0_rtx, PATTERN (curr_insn)))
3872     no_input_reloads_p = true;
3873   if (HAVE_cc0 && reg_set_p (cc0_rtx, PATTERN (curr_insn)))
3874     no_output_reloads_p = true;
3875 
3876   n_operands = curr_static_id->n_operands;
3877   n_alternatives = curr_static_id->n_alternatives;
3878 
3879   /* Just return "no reloads" if insn has no operands with
3880      constraints.  */
3881   if (n_operands == 0 || n_alternatives == 0)
3882     return false;
3883 
3884   max_regno_before = max_reg_num ();
3885 
3886   for (i = 0; i < n_operands; i++)
3887     {
3888       goal_alt_matched[i][0] = -1;
3889       goal_alt_matches[i] = -1;
3890     }
3891 
3892   commutative = curr_static_id->commutative;
3893 
3894   /* Now see what we need for pseudos that didn't get hard regs or got
3895      the wrong kind of hard reg.  For this, we must consider all the
3896      operands together against the register constraints.  */
3897 
3898   best_losers = best_overall = INT_MAX;
3899   best_reload_sum = 0;
3900 
3901   curr_swapped = false;
3902   goal_alt_swapped = false;
3903 
3904   if (! check_only_p)
3905     /* Make equivalence substitution and memory subreg elimination
3906        before address processing because an address legitimacy can
3907        depend on memory mode.  */
3908     for (i = 0; i < n_operands; i++)
3909       {
3910 	rtx op, subst, old;
3911 	bool op_change_p = false;
3912 
3913 	if (curr_static_id->operand[i].is_operator)
3914 	  continue;
3915 
3916 	old = op = *curr_id->operand_loc[i];
3917 	if (GET_CODE (old) == SUBREG)
3918 	  old = SUBREG_REG (old);
3919 	subst = get_equiv_with_elimination (old, curr_insn);
3920 	original_subreg_reg_mode[i] = VOIDmode;
3921 	equiv_substition_p[i] = false;
3922 	if (subst != old)
3923 	  {
3924 	    equiv_substition_p[i] = true;
3925 	    subst = copy_rtx (subst);
3926 	    lra_assert (REG_P (old));
3927 	    if (GET_CODE (op) != SUBREG)
3928 	      *curr_id->operand_loc[i] = subst;
3929 	    else
3930 	      {
3931 		SUBREG_REG (op) = subst;
3932 		if (GET_MODE (subst) == VOIDmode)
3933 		  original_subreg_reg_mode[i] = GET_MODE (old);
3934 	      }
3935 	    if (lra_dump_file != NULL)
3936 	      {
3937 		fprintf (lra_dump_file,
3938 			 "Changing pseudo %d in operand %i of insn %u on equiv ",
3939 			 REGNO (old), i, INSN_UID (curr_insn));
3940 		dump_value_slim (lra_dump_file, subst, 1);
3941 		fprintf (lra_dump_file, "\n");
3942 	      }
3943 	    op_change_p = change_p = true;
3944 	  }
3945 	if (simplify_operand_subreg (i, GET_MODE (old)) || op_change_p)
3946 	  {
3947 	    change_p = true;
3948 	    lra_update_dup (curr_id, i);
3949 	  }
3950       }
3951 
3952   /* Reload address registers and displacements.  We do it before
3953      finding an alternative because of memory constraints.  */
3954   before = after = NULL;
3955   for (i = 0; i < n_operands; i++)
3956     if (! curr_static_id->operand[i].is_operator
3957 	&& process_address (i, check_only_p, &before, &after))
3958       {
3959 	if (check_only_p)
3960 	  return true;
3961 	change_p = true;
3962 	lra_update_dup (curr_id, i);
3963       }
3964 
3965   if (change_p)
3966     /* If we've changed the instruction then any alternative that
3967        we chose previously may no longer be valid.  */
3968     lra_set_used_insn_alternative (curr_insn, LRA_UNKNOWN_ALT);
3969 
3970   if (! check_only_p && curr_insn_set != NULL_RTX
3971       && check_and_process_move (&change_p, &sec_mem_p))
3972     return change_p;
3973 
3974  try_swapped:
3975 
3976   reused_alternative_num = check_only_p ? LRA_UNKNOWN_ALT : curr_id->used_insn_alternative;
3977   if (lra_dump_file != NULL && reused_alternative_num >= 0)
3978     fprintf (lra_dump_file, "Reusing alternative %d for insn #%u\n",
3979 	     reused_alternative_num, INSN_UID (curr_insn));
3980 
3981   if (process_alt_operands (reused_alternative_num))
3982     alt_p = true;
3983 
3984   if (check_only_p)
3985     return ! alt_p || best_losers != 0;
3986 
3987   /* If insn is commutative (it's safe to exchange a certain pair of
3988      operands) then we need to try each alternative twice, the second
3989      time matching those two operands as if we had exchanged them.  To
3990      do this, really exchange them in operands.
3991 
3992      If we have just tried the alternatives the second time, return
3993      operands to normal and drop through.  */
3994 
3995   if (reused_alternative_num < 0 && commutative >= 0)
3996     {
3997       curr_swapped = !curr_swapped;
3998       if (curr_swapped)
3999 	{
4000 	  swap_operands (commutative);
4001 	  goto try_swapped;
4002 	}
4003       else
4004 	swap_operands (commutative);
4005     }
4006 
4007   if (! alt_p && ! sec_mem_p)
4008     {
4009       /* No alternative works with reloads??  */
4010       if (INSN_CODE (curr_insn) >= 0)
4011 	fatal_insn ("unable to generate reloads for:", curr_insn);
4012       error_for_asm (curr_insn,
4013 		     "inconsistent operand constraints in an %<asm%>");
4014       lra_asm_error_p = true;
4015       /* Avoid further trouble with this insn.  Don't generate use
4016 	 pattern here as we could use the insn SP offset.  */
4017       lra_set_insn_deleted (curr_insn);
4018       return true;
4019     }
4020 
4021   /* If the best alternative is with operands 1 and 2 swapped, swap
4022      them.  Update the operand numbers of any reloads already
4023      pushed.  */
4024 
4025   if (goal_alt_swapped)
4026     {
4027       if (lra_dump_file != NULL)
4028 	fprintf (lra_dump_file, "  Commutative operand exchange in insn %u\n",
4029 		 INSN_UID (curr_insn));
4030 
4031       /* Swap the duplicates too.  */
4032       swap_operands (commutative);
4033       change_p = true;
4034     }
4035 
4036   /* Some targets' TARGET_SECONDARY_MEMORY_NEEDED (e.g. x86) are defined
4037      too conservatively.  So we use the secondary memory only if there
4038      is no any alternative without reloads.  */
4039   use_sec_mem_p = false;
4040   if (! alt_p)
4041     use_sec_mem_p = true;
4042   else if (sec_mem_p)
4043     {
4044       for (i = 0; i < n_operands; i++)
4045 	if (! goal_alt_win[i] && ! goal_alt_match_win[i])
4046 	  break;
4047       use_sec_mem_p = i < n_operands;
4048     }
4049 
4050   if (use_sec_mem_p)
4051     {
4052       int in = -1, out = -1;
4053       rtx new_reg, src, dest, rld;
4054       machine_mode sec_mode, rld_mode;
4055 
4056       lra_assert (curr_insn_set != NULL_RTX && sec_mem_p);
4057       dest = SET_DEST (curr_insn_set);
4058       src = SET_SRC (curr_insn_set);
4059       for (i = 0; i < n_operands; i++)
4060 	if (*curr_id->operand_loc[i] == dest)
4061 	  out = i;
4062 	else if (*curr_id->operand_loc[i] == src)
4063 	  in = i;
4064       for (i = 0; i < curr_static_id->n_dups; i++)
4065 	if (out < 0 && *curr_id->dup_loc[i] == dest)
4066 	  out = curr_static_id->dup_num[i];
4067 	else if (in < 0 && *curr_id->dup_loc[i] == src)
4068 	  in = curr_static_id->dup_num[i];
4069       lra_assert (out >= 0 && in >= 0
4070 		  && curr_static_id->operand[out].type == OP_OUT
4071 		  && curr_static_id->operand[in].type == OP_IN);
4072       rld = partial_subreg_p (GET_MODE (src), GET_MODE (dest)) ? src : dest;
4073       rld_mode = GET_MODE (rld);
4074       sec_mode = targetm.secondary_memory_needed_mode (rld_mode);
4075       new_reg = lra_create_new_reg (sec_mode, NULL_RTX,
4076 				    NO_REGS, "secondary");
4077       /* If the mode is changed, it should be wider.  */
4078       lra_assert (!partial_subreg_p (sec_mode, rld_mode));
4079       if (sec_mode != rld_mode)
4080         {
4081 	  /* If the target says specifically to use another mode for
4082 	     secondary memory moves we cannot reuse the original
4083 	     insn.  */
4084 	  after = emit_spill_move (false, new_reg, dest);
4085 	  lra_process_new_insns (curr_insn, NULL, after,
4086 				 "Inserting the sec. move");
4087 	  /* We may have non null BEFORE here (e.g. after address
4088 	     processing.  */
4089 	  push_to_sequence (before);
4090 	  before = emit_spill_move (true, new_reg, src);
4091 	  emit_insn (before);
4092 	  before = get_insns ();
4093 	  end_sequence ();
4094 	  lra_process_new_insns (curr_insn, before, NULL, "Changing on");
4095 	  lra_set_insn_deleted (curr_insn);
4096 	}
4097       else if (dest == rld)
4098         {
4099 	  *curr_id->operand_loc[out] = new_reg;
4100 	  lra_update_dup (curr_id, out);
4101 	  after = emit_spill_move (false, new_reg, dest);
4102 	  lra_process_new_insns (curr_insn, NULL, after,
4103 				 "Inserting the sec. move");
4104 	}
4105       else
4106 	{
4107 	  *curr_id->operand_loc[in] = new_reg;
4108 	  lra_update_dup (curr_id, in);
4109 	  /* See comments above.  */
4110 	  push_to_sequence (before);
4111 	  before = emit_spill_move (true, new_reg, src);
4112 	  emit_insn (before);
4113 	  before = get_insns ();
4114 	  end_sequence ();
4115 	  lra_process_new_insns (curr_insn, before, NULL,
4116 				 "Inserting the sec. move");
4117 	}
4118       lra_update_insn_regno_info (curr_insn);
4119       return true;
4120     }
4121 
4122   lra_assert (goal_alt_number >= 0);
4123   lra_set_used_insn_alternative (curr_insn, goal_alt_number);
4124 
4125   if (lra_dump_file != NULL)
4126     {
4127       const char *p;
4128 
4129       fprintf (lra_dump_file, "	 Choosing alt %d in insn %u:",
4130 	       goal_alt_number, INSN_UID (curr_insn));
4131       for (i = 0; i < n_operands; i++)
4132 	{
4133 	  p = (curr_static_id->operand_alternative
4134 	       [goal_alt_number * n_operands + i].constraint);
4135 	  if (*p == '\0')
4136 	    continue;
4137 	  fprintf (lra_dump_file, "  (%d) ", i);
4138 	  for (; *p != '\0' && *p != ',' && *p != '#'; p++)
4139 	    fputc (*p, lra_dump_file);
4140 	}
4141       if (INSN_CODE (curr_insn) >= 0
4142           && (p = get_insn_name (INSN_CODE (curr_insn))) != NULL)
4143         fprintf (lra_dump_file, " {%s}", p);
4144       if (maybe_ne (curr_id->sp_offset, 0))
4145 	{
4146 	  fprintf (lra_dump_file, " (sp_off=");
4147 	  print_dec (curr_id->sp_offset, lra_dump_file);
4148 	  fprintf (lra_dump_file, ")");
4149 	}
4150       fprintf (lra_dump_file, "\n");
4151     }
4152 
4153   /* Right now, for any pair of operands I and J that are required to
4154      match, with J < I, goal_alt_matches[I] is J.  Add I to
4155      goal_alt_matched[J].  */
4156 
4157   for (i = 0; i < n_operands; i++)
4158     if ((j = goal_alt_matches[i]) >= 0)
4159       {
4160 	for (k = 0; goal_alt_matched[j][k] >= 0; k++)
4161 	  ;
4162 	/* We allow matching one output operand and several input
4163 	   operands.  */
4164 	lra_assert (k == 0
4165 		    || (curr_static_id->operand[j].type == OP_OUT
4166 			&& curr_static_id->operand[i].type == OP_IN
4167 			&& (curr_static_id->operand
4168 			    [goal_alt_matched[j][0]].type == OP_IN)));
4169 	goal_alt_matched[j][k] = i;
4170 	goal_alt_matched[j][k + 1] = -1;
4171       }
4172 
4173   for (i = 0; i < n_operands; i++)
4174     goal_alt_win[i] |= goal_alt_match_win[i];
4175 
4176   /* Any constants that aren't allowed and can't be reloaded into
4177      registers are here changed into memory references.	 */
4178   for (i = 0; i < n_operands; i++)
4179     if (goal_alt_win[i])
4180       {
4181 	int regno;
4182 	enum reg_class new_class;
4183 	rtx reg = *curr_id->operand_loc[i];
4184 
4185 	if (GET_CODE (reg) == SUBREG)
4186 	  reg = SUBREG_REG (reg);
4187 
4188 	if (REG_P (reg) && (regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
4189 	  {
4190 	    bool ok_p = in_class_p (reg, goal_alt[i], &new_class);
4191 
4192 	    if (new_class != NO_REGS && get_reg_class (regno) != new_class)
4193 	      {
4194 		lra_assert (ok_p);
4195 		lra_change_class (regno, new_class, "      Change to", true);
4196 	      }
4197 	  }
4198       }
4199     else
4200       {
4201 	const char *constraint;
4202 	char c;
4203 	rtx op = *curr_id->operand_loc[i];
4204 	rtx subreg = NULL_RTX;
4205 	machine_mode mode = curr_operand_mode[i];
4206 
4207 	if (GET_CODE (op) == SUBREG)
4208 	  {
4209 	    subreg = op;
4210 	    op = SUBREG_REG (op);
4211 	    mode = GET_MODE (op);
4212 	  }
4213 
4214 	if (CONST_POOL_OK_P (mode, op)
4215 	    && ((targetm.preferred_reload_class
4216 		 (op, (enum reg_class) goal_alt[i]) == NO_REGS)
4217 		|| no_input_reloads_p))
4218 	  {
4219 	    rtx tem = force_const_mem (mode, op);
4220 
4221 	    change_p = true;
4222 	    if (subreg != NULL_RTX)
4223 	      tem = gen_rtx_SUBREG (mode, tem, SUBREG_BYTE (subreg));
4224 
4225 	    *curr_id->operand_loc[i] = tem;
4226 	    lra_update_dup (curr_id, i);
4227 	    process_address (i, false, &before, &after);
4228 
4229 	    /* If the alternative accepts constant pool refs directly
4230 	       there will be no reload needed at all.  */
4231 	    if (subreg != NULL_RTX)
4232 	      continue;
4233 	    /* Skip alternatives before the one requested.  */
4234 	    constraint = (curr_static_id->operand_alternative
4235 			  [goal_alt_number * n_operands + i].constraint);
4236 	    for (;
4237 		 (c = *constraint) && c != ',' && c != '#';
4238 		 constraint += CONSTRAINT_LEN (c, constraint))
4239 	      {
4240 		enum constraint_num cn = lookup_constraint (constraint);
4241 		if ((insn_extra_memory_constraint (cn)
4242 		     || insn_extra_special_memory_constraint (cn))
4243 		    && satisfies_memory_constraint_p (tem, cn))
4244 		  break;
4245 	      }
4246 	    if (c == '\0' || c == ',' || c == '#')
4247 	      continue;
4248 
4249 	    goal_alt_win[i] = true;
4250 	  }
4251       }
4252 
4253   n_outputs = 0;
4254   outputs[0] = -1;
4255   for (i = 0; i < n_operands; i++)
4256     {
4257       int regno;
4258       bool optional_p = false;
4259       rtx old, new_reg;
4260       rtx op = *curr_id->operand_loc[i];
4261 
4262       if (goal_alt_win[i])
4263 	{
4264 	  if (goal_alt[i] == NO_REGS
4265 	      && REG_P (op)
4266 	      /* When we assign NO_REGS it means that we will not
4267 		 assign a hard register to the scratch pseudo by
4268 		 assigment pass and the scratch pseudo will be
4269 		 spilled.  Spilled scratch pseudos are transformed
4270 		 back to scratches at the LRA end.  */
4271 	      && lra_former_scratch_operand_p (curr_insn, i)
4272 	      && lra_former_scratch_p (REGNO (op)))
4273 	    {
4274 	      int regno = REGNO (op);
4275 	      lra_change_class (regno, NO_REGS, "      Change to", true);
4276 	      if (lra_get_regno_hard_regno (regno) >= 0)
4277 		/* We don't have to mark all insn affected by the
4278 		   spilled pseudo as there is only one such insn, the
4279 		   current one.  */
4280 		reg_renumber[regno] = -1;
4281 	      lra_assert (bitmap_single_bit_set_p
4282 			  (&lra_reg_info[REGNO (op)].insn_bitmap));
4283 	    }
4284 	  /* We can do an optional reload.  If the pseudo got a hard
4285 	     reg, we might improve the code through inheritance.  If
4286 	     it does not get a hard register we coalesce memory/memory
4287 	     moves later.  Ignore move insns to avoid cycling.  */
4288 	  if (! lra_simple_p
4289 	      && lra_undo_inheritance_iter < LRA_MAX_INHERITANCE_PASSES
4290 	      && goal_alt[i] != NO_REGS && REG_P (op)
4291 	      && (regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER
4292 	      && regno < new_regno_start
4293 	      && ! lra_former_scratch_p (regno)
4294 	      && reg_renumber[regno] < 0
4295 	      /* Check that the optional reload pseudo will be able to
4296 		 hold given mode value.  */
4297 	      && ! (prohibited_class_reg_set_mode_p
4298 		    (goal_alt[i], reg_class_contents[goal_alt[i]],
4299 		     PSEUDO_REGNO_MODE (regno)))
4300 	      && (curr_insn_set == NULL_RTX
4301 		  || !((REG_P (SET_SRC (curr_insn_set))
4302 			|| MEM_P (SET_SRC (curr_insn_set))
4303 			|| GET_CODE (SET_SRC (curr_insn_set)) == SUBREG)
4304 		       && (REG_P (SET_DEST (curr_insn_set))
4305 			   || MEM_P (SET_DEST (curr_insn_set))
4306 			   || GET_CODE (SET_DEST (curr_insn_set)) == SUBREG))))
4307 	    optional_p = true;
4308 	  else if (goal_alt_matched[i][0] != -1
4309 		   && curr_static_id->operand[i].type == OP_OUT
4310 		   && (curr_static_id->operand_alternative
4311 		       [goal_alt_number * n_operands + i].earlyclobber)
4312 		   && REG_P (op))
4313 	    {
4314 	      for (j = 0; goal_alt_matched[i][j] != -1; j++)
4315 		{
4316 		  rtx op2 = *curr_id->operand_loc[goal_alt_matched[i][j]];
4317 
4318 		  if (REG_P (op2) && REGNO (op) != REGNO (op2))
4319 		    break;
4320 		}
4321 	      if (goal_alt_matched[i][j] != -1)
4322 		{
4323 		  /* Generate reloads for different output and matched
4324 		     input registers.  This is the easiest way to avoid
4325 		     creation of non-existing register conflicts in
4326 		     lra-lives.c.  */
4327 		  match_reload (i, goal_alt_matched[i], outputs, goal_alt[i], &before,
4328 				&after, TRUE);
4329 		  outputs[n_outputs++] = i;
4330 		  outputs[n_outputs] = -1;
4331 		}
4332 	      continue;
4333 	    }
4334 	  else
4335 	    continue;
4336 	}
4337 
4338       /* Operands that match previous ones have already been handled.  */
4339       if (goal_alt_matches[i] >= 0)
4340 	continue;
4341 
4342       /* We should not have an operand with a non-offsettable address
4343 	 appearing where an offsettable address will do.  It also may
4344 	 be a case when the address should be special in other words
4345 	 not a general one (e.g. it needs no index reg).  */
4346       if (goal_alt_matched[i][0] == -1 && goal_alt_offmemok[i] && MEM_P (op))
4347 	{
4348 	  enum reg_class rclass;
4349 	  rtx *loc = &XEXP (op, 0);
4350 	  enum rtx_code code = GET_CODE (*loc);
4351 
4352 	  push_to_sequence (before);
4353 	  rclass = base_reg_class (GET_MODE (op), MEM_ADDR_SPACE (op),
4354 				   MEM, SCRATCH);
4355 	  if (GET_RTX_CLASS (code) == RTX_AUTOINC)
4356 	    new_reg = emit_inc (rclass, *loc, *loc,
4357 				/* This value does not matter for MODIFY.  */
4358 				GET_MODE_SIZE (GET_MODE (op)));
4359 	  else if (get_reload_reg (OP_IN, Pmode, *loc, rclass, FALSE,
4360 				   "offsetable address", &new_reg))
4361 	    {
4362 	      rtx addr = *loc;
4363 	      enum rtx_code code = GET_CODE (addr);
4364 
4365 	      if (code == AND && CONST_INT_P (XEXP (addr, 1)))
4366 		/* (and ... (const_int -X)) is used to align to X bytes.  */
4367 		addr = XEXP (*loc, 0);
4368 	      lra_emit_move (new_reg, addr);
4369 	      if (addr != *loc)
4370 		emit_move_insn (new_reg, gen_rtx_AND (GET_MODE (new_reg), new_reg, XEXP (*loc, 1)));
4371 	    }
4372 	  before = get_insns ();
4373 	  end_sequence ();
4374 	  *loc = new_reg;
4375 	  lra_update_dup (curr_id, i);
4376 	}
4377       else if (goal_alt_matched[i][0] == -1)
4378 	{
4379 	  machine_mode mode;
4380 	  rtx reg, *loc;
4381 	  int hard_regno;
4382 	  enum op_type type = curr_static_id->operand[i].type;
4383 
4384 	  loc = curr_id->operand_loc[i];
4385 	  mode = curr_operand_mode[i];
4386 	  if (GET_CODE (*loc) == SUBREG)
4387 	    {
4388 	      reg = SUBREG_REG (*loc);
4389 	      poly_int64 byte = SUBREG_BYTE (*loc);
4390 	      if (REG_P (reg)
4391 		  /* Strict_low_part requires reloading the register and not
4392 		     just the subreg.  Likewise for a strict subreg no wider
4393 		     than a word for WORD_REGISTER_OPERATIONS targets.  */
4394 		  && (curr_static_id->operand[i].strict_low
4395 		      || (!paradoxical_subreg_p (mode, GET_MODE (reg))
4396 			  && (hard_regno
4397 			      = get_try_hard_regno (REGNO (reg))) >= 0
4398 			  && (simplify_subreg_regno
4399 			      (hard_regno,
4400 			       GET_MODE (reg), byte, mode) < 0)
4401 			  && (goal_alt[i] == NO_REGS
4402 			      || (simplify_subreg_regno
4403 				  (ira_class_hard_regs[goal_alt[i]][0],
4404 				   GET_MODE (reg), byte, mode) >= 0)))
4405 		      || (partial_subreg_p (mode, GET_MODE (reg))
4406 			  && known_le (GET_MODE_SIZE (GET_MODE (reg)),
4407 				       UNITS_PER_WORD)
4408 			  && WORD_REGISTER_OPERATIONS)))
4409 		{
4410 		  /* An OP_INOUT is required when reloading a subreg of a
4411 		     mode wider than a word to ensure that data beyond the
4412 		     word being reloaded is preserved.  Also automatically
4413 		     ensure that strict_low_part reloads are made into
4414 		     OP_INOUT which should already be true from the backend
4415 		     constraints.  */
4416 		  if (type == OP_OUT
4417 		      && (curr_static_id->operand[i].strict_low
4418 			  || read_modify_subreg_p (*loc)))
4419 		    type = OP_INOUT;
4420 		  loc = &SUBREG_REG (*loc);
4421 		  mode = GET_MODE (*loc);
4422 		}
4423 	    }
4424 	  old = *loc;
4425 	  if (get_reload_reg (type, mode, old, goal_alt[i],
4426 			      loc != curr_id->operand_loc[i], "", &new_reg)
4427 	      && type != OP_OUT)
4428 	    {
4429 	      push_to_sequence (before);
4430 	      lra_emit_move (new_reg, old);
4431 	      before = get_insns ();
4432 	      end_sequence ();
4433 	    }
4434 	  *loc = new_reg;
4435 	  if (type != OP_IN
4436 	      && find_reg_note (curr_insn, REG_UNUSED, old) == NULL_RTX)
4437 	    {
4438 	      start_sequence ();
4439 	      lra_emit_move (type == OP_INOUT ? copy_rtx (old) : old, new_reg);
4440 	      emit_insn (after);
4441 	      after = get_insns ();
4442 	      end_sequence ();
4443 	      *loc = new_reg;
4444 	    }
4445 	  for (j = 0; j < goal_alt_dont_inherit_ops_num; j++)
4446 	    if (goal_alt_dont_inherit_ops[j] == i)
4447 	      {
4448 		lra_set_regno_unique_value (REGNO (new_reg));
4449 		break;
4450 	      }
4451 	  lra_update_dup (curr_id, i);
4452 	}
4453       else if (curr_static_id->operand[i].type == OP_IN
4454 	       && (curr_static_id->operand[goal_alt_matched[i][0]].type
4455 		   == OP_OUT
4456 		   || (curr_static_id->operand[goal_alt_matched[i][0]].type
4457 		       == OP_INOUT
4458 		       && (operands_match_p
4459 			   (*curr_id->operand_loc[i],
4460 			    *curr_id->operand_loc[goal_alt_matched[i][0]],
4461 			    -1)))))
4462 	{
4463 	  /* generate reloads for input and matched outputs.  */
4464 	  match_inputs[0] = i;
4465 	  match_inputs[1] = -1;
4466 	  match_reload (goal_alt_matched[i][0], match_inputs, outputs,
4467 			goal_alt[i], &before, &after,
4468 			curr_static_id->operand_alternative
4469 			[goal_alt_number * n_operands + goal_alt_matched[i][0]]
4470 			.earlyclobber);
4471 	}
4472       else if ((curr_static_id->operand[i].type == OP_OUT
4473 		|| (curr_static_id->operand[i].type == OP_INOUT
4474 		    && (operands_match_p
4475 			(*curr_id->operand_loc[i],
4476 			 *curr_id->operand_loc[goal_alt_matched[i][0]],
4477 			 -1))))
4478 	       && (curr_static_id->operand[goal_alt_matched[i][0]].type
4479 		    == OP_IN))
4480 	/* Generate reloads for output and matched inputs.  */
4481 	match_reload (i, goal_alt_matched[i], outputs, goal_alt[i], &before,
4482 		      &after, curr_static_id->operand_alternative
4483 			      [goal_alt_number * n_operands + i].earlyclobber);
4484       else if (curr_static_id->operand[i].type == OP_IN
4485 	       && (curr_static_id->operand[goal_alt_matched[i][0]].type
4486 		   == OP_IN))
4487 	{
4488 	  /* Generate reloads for matched inputs.  */
4489 	  match_inputs[0] = i;
4490 	  for (j = 0; (k = goal_alt_matched[i][j]) >= 0; j++)
4491 	    match_inputs[j + 1] = k;
4492 	  match_inputs[j + 1] = -1;
4493 	  match_reload (-1, match_inputs, outputs, goal_alt[i], &before,
4494 			&after, false);
4495 	}
4496       else
4497 	/* We must generate code in any case when function
4498 	   process_alt_operands decides that it is possible.  */
4499 	gcc_unreachable ();
4500 
4501       /* Memorise processed outputs so that output remaining to be processed
4502 	 can avoid using the same register value (see match_reload).  */
4503       if (curr_static_id->operand[i].type == OP_OUT)
4504 	{
4505 	  outputs[n_outputs++] = i;
4506 	  outputs[n_outputs] = -1;
4507 	}
4508 
4509       if (optional_p)
4510 	{
4511 	  rtx reg = op;
4512 
4513 	  lra_assert (REG_P (reg));
4514 	  regno = REGNO (reg);
4515 	  op = *curr_id->operand_loc[i]; /* Substitution.  */
4516 	  if (GET_CODE (op) == SUBREG)
4517 	    op = SUBREG_REG (op);
4518 	  gcc_assert (REG_P (op) && (int) REGNO (op) >= new_regno_start);
4519 	  bitmap_set_bit (&lra_optional_reload_pseudos, REGNO (op));
4520 	  lra_reg_info[REGNO (op)].restore_rtx = reg;
4521 	  if (lra_dump_file != NULL)
4522 	    fprintf (lra_dump_file,
4523 		     "      Making reload reg %d for reg %d optional\n",
4524 		     REGNO (op), regno);
4525 	}
4526     }
4527   if (before != NULL_RTX || after != NULL_RTX
4528       || max_regno_before != max_reg_num ())
4529     change_p = true;
4530   if (change_p)
4531     {
4532       lra_update_operator_dups (curr_id);
4533       /* Something changes -- process the insn.	 */
4534       lra_update_insn_regno_info (curr_insn);
4535     }
4536   lra_process_new_insns (curr_insn, before, after, "Inserting insn reload");
4537   return change_p;
4538 }
4539 
4540 /* Return true if INSN satisfies all constraints.  In other words, no
4541    reload insns are needed.  */
4542 bool
lra_constrain_insn(rtx_insn * insn)4543 lra_constrain_insn (rtx_insn *insn)
4544 {
4545   int saved_new_regno_start = new_regno_start;
4546   int saved_new_insn_uid_start = new_insn_uid_start;
4547   bool change_p;
4548 
4549   curr_insn = insn;
4550   curr_id = lra_get_insn_recog_data (curr_insn);
4551   curr_static_id = curr_id->insn_static_data;
4552   new_insn_uid_start = get_max_uid ();
4553   new_regno_start = max_reg_num ();
4554   change_p = curr_insn_transform (true);
4555   new_regno_start = saved_new_regno_start;
4556   new_insn_uid_start = saved_new_insn_uid_start;
4557   return ! change_p;
4558 }
4559 
4560 /* Return true if X is in LIST.	 */
4561 static bool
in_list_p(rtx x,rtx list)4562 in_list_p (rtx x, rtx list)
4563 {
4564   for (; list != NULL_RTX; list = XEXP (list, 1))
4565     if (XEXP (list, 0) == x)
4566       return true;
4567   return false;
4568 }
4569 
4570 /* Return true if X contains an allocatable hard register (if
4571    HARD_REG_P) or a (spilled if SPILLED_P) pseudo.  */
4572 static bool
contains_reg_p(rtx x,bool hard_reg_p,bool spilled_p)4573 contains_reg_p (rtx x, bool hard_reg_p, bool spilled_p)
4574 {
4575   int i, j;
4576   const char *fmt;
4577   enum rtx_code code;
4578 
4579   code = GET_CODE (x);
4580   if (REG_P (x))
4581     {
4582       int regno = REGNO (x);
4583       HARD_REG_SET alloc_regs;
4584 
4585       if (hard_reg_p)
4586 	{
4587 	  if (regno >= FIRST_PSEUDO_REGISTER)
4588 	    regno = lra_get_regno_hard_regno (regno);
4589 	  if (regno < 0)
4590 	    return false;
4591 	  COMPL_HARD_REG_SET (alloc_regs, lra_no_alloc_regs);
4592 	  return overlaps_hard_reg_set_p (alloc_regs, GET_MODE (x), regno);
4593 	}
4594       else
4595 	{
4596 	  if (regno < FIRST_PSEUDO_REGISTER)
4597 	    return false;
4598 	  if (! spilled_p)
4599 	    return true;
4600 	  return lra_get_regno_hard_regno (regno) < 0;
4601 	}
4602     }
4603   fmt = GET_RTX_FORMAT (code);
4604   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4605     {
4606       if (fmt[i] == 'e')
4607 	{
4608 	  if (contains_reg_p (XEXP (x, i), hard_reg_p, spilled_p))
4609 	    return true;
4610 	}
4611       else if (fmt[i] == 'E')
4612 	{
4613 	  for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4614 	    if (contains_reg_p (XVECEXP (x, i, j), hard_reg_p, spilled_p))
4615 	      return true;
4616 	}
4617     }
4618   return false;
4619 }
4620 
4621 /* Process all regs in location *LOC and change them on equivalent
4622    substitution.  Return true if any change was done.  */
4623 static bool
loc_equivalence_change_p(rtx * loc)4624 loc_equivalence_change_p (rtx *loc)
4625 {
4626   rtx subst, reg, x = *loc;
4627   bool result = false;
4628   enum rtx_code code = GET_CODE (x);
4629   const char *fmt;
4630   int i, j;
4631 
4632   if (code == SUBREG)
4633     {
4634       reg = SUBREG_REG (x);
4635       if ((subst = get_equiv_with_elimination (reg, curr_insn)) != reg
4636 	  && GET_MODE (subst) == VOIDmode)
4637 	{
4638 	  /* We cannot reload debug location.  Simplify subreg here
4639 	     while we know the inner mode.  */
4640 	  *loc = simplify_gen_subreg (GET_MODE (x), subst,
4641 				      GET_MODE (reg), SUBREG_BYTE (x));
4642 	  return true;
4643 	}
4644     }
4645   if (code == REG && (subst = get_equiv_with_elimination (x, curr_insn)) != x)
4646     {
4647       *loc = subst;
4648       return true;
4649     }
4650 
4651   /* Scan all the operand sub-expressions.  */
4652   fmt = GET_RTX_FORMAT (code);
4653   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4654     {
4655       if (fmt[i] == 'e')
4656 	result = loc_equivalence_change_p (&XEXP (x, i)) || result;
4657       else if (fmt[i] == 'E')
4658 	for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4659 	  result
4660 	    = loc_equivalence_change_p (&XVECEXP (x, i, j)) || result;
4661     }
4662   return result;
4663 }
4664 
4665 /* Similar to loc_equivalence_change_p, but for use as
4666    simplify_replace_fn_rtx callback.  DATA is insn for which the
4667    elimination is done.  If it null we don't do the elimination.  */
4668 static rtx
loc_equivalence_callback(rtx loc,const_rtx,void * data)4669 loc_equivalence_callback (rtx loc, const_rtx, void *data)
4670 {
4671   if (!REG_P (loc))
4672     return NULL_RTX;
4673 
4674   rtx subst = (data == NULL
4675 	       ? get_equiv (loc) : get_equiv_with_elimination (loc, (rtx_insn *) data));
4676   if (subst != loc)
4677     return subst;
4678 
4679   return NULL_RTX;
4680 }
4681 
4682 /* Maximum number of generated reload insns per an insn.  It is for
4683    preventing this pass cycling in a bug case.	*/
4684 #define MAX_RELOAD_INSNS_NUMBER LRA_MAX_INSN_RELOADS
4685 
4686 /* The current iteration number of this LRA pass.  */
4687 int lra_constraint_iter;
4688 
4689 /* True if we substituted equiv which needs checking register
4690    allocation correctness because the equivalent value contains
4691    allocatable hard registers or when we restore multi-register
4692    pseudo.  */
4693 bool lra_risky_transformations_p;
4694 
4695 /* Return true if REGNO is referenced in more than one block.  */
4696 static bool
multi_block_pseudo_p(int regno)4697 multi_block_pseudo_p (int regno)
4698 {
4699   basic_block bb = NULL;
4700   unsigned int uid;
4701   bitmap_iterator bi;
4702 
4703   if (regno < FIRST_PSEUDO_REGISTER)
4704     return false;
4705 
4706     EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
4707       if (bb == NULL)
4708 	bb = BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn);
4709       else if (BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn) != bb)
4710 	return true;
4711     return false;
4712 }
4713 
4714 /* Return true if LIST contains a deleted insn.  */
4715 static bool
contains_deleted_insn_p(rtx_insn_list * list)4716 contains_deleted_insn_p (rtx_insn_list *list)
4717 {
4718   for (; list != NULL_RTX; list = list->next ())
4719     if (NOTE_P (list->insn ())
4720 	&& NOTE_KIND (list->insn ()) == NOTE_INSN_DELETED)
4721       return true;
4722   return false;
4723 }
4724 
4725 /* Return true if X contains a pseudo dying in INSN.  */
4726 static bool
dead_pseudo_p(rtx x,rtx_insn * insn)4727 dead_pseudo_p (rtx x, rtx_insn *insn)
4728 {
4729   int i, j;
4730   const char *fmt;
4731   enum rtx_code code;
4732 
4733   if (REG_P (x))
4734     return (insn != NULL_RTX
4735 	    && find_regno_note (insn, REG_DEAD, REGNO (x)) != NULL_RTX);
4736   code = GET_CODE (x);
4737   fmt = GET_RTX_FORMAT (code);
4738   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4739     {
4740       if (fmt[i] == 'e')
4741 	{
4742 	  if (dead_pseudo_p (XEXP (x, i), insn))
4743 	    return true;
4744 	}
4745       else if (fmt[i] == 'E')
4746 	{
4747 	  for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4748 	    if (dead_pseudo_p (XVECEXP (x, i, j), insn))
4749 	      return true;
4750 	}
4751     }
4752   return false;
4753 }
4754 
4755 /* Return true if INSN contains a dying pseudo in INSN right hand
4756    side.  */
4757 static bool
insn_rhs_dead_pseudo_p(rtx_insn * insn)4758 insn_rhs_dead_pseudo_p (rtx_insn *insn)
4759 {
4760   rtx set = single_set (insn);
4761 
4762   gcc_assert (set != NULL);
4763   return dead_pseudo_p (SET_SRC (set), insn);
4764 }
4765 
4766 /* Return true if any init insn of REGNO contains a dying pseudo in
4767    insn right hand side.  */
4768 static bool
init_insn_rhs_dead_pseudo_p(int regno)4769 init_insn_rhs_dead_pseudo_p (int regno)
4770 {
4771   rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4772 
4773   if (insns == NULL)
4774     return false;
4775   for (; insns != NULL_RTX; insns = insns->next ())
4776     if (insn_rhs_dead_pseudo_p (insns->insn ()))
4777       return true;
4778   return false;
4779 }
4780 
4781 /* Return TRUE if REGNO has a reverse equivalence.  The equivalence is
4782    reverse only if we have one init insn with given REGNO as a
4783    source.  */
4784 static bool
reverse_equiv_p(int regno)4785 reverse_equiv_p (int regno)
4786 {
4787   rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4788   rtx set;
4789 
4790   if (insns == NULL)
4791     return false;
4792   if (! INSN_P (insns->insn ())
4793       || insns->next () != NULL)
4794     return false;
4795   if ((set = single_set (insns->insn ())) == NULL_RTX)
4796     return false;
4797   return REG_P (SET_SRC (set)) && (int) REGNO (SET_SRC (set)) == regno;
4798 }
4799 
4800 /* Return TRUE if REGNO was reloaded in an equivalence init insn.  We
4801    call this function only for non-reverse equivalence.  */
4802 static bool
contains_reloaded_insn_p(int regno)4803 contains_reloaded_insn_p (int regno)
4804 {
4805   rtx set;
4806   rtx_insn_list *list = ira_reg_equiv[regno].init_insns;
4807 
4808   for (; list != NULL; list = list->next ())
4809     if ((set = single_set (list->insn ())) == NULL_RTX
4810 	|| ! REG_P (SET_DEST (set))
4811 	|| (int) REGNO (SET_DEST (set)) != regno)
4812       return true;
4813   return false;
4814 }
4815 
4816 /* Entry function of LRA constraint pass.  Return true if the
4817    constraint pass did change the code.	 */
4818 bool
lra_constraints(bool first_p)4819 lra_constraints (bool first_p)
4820 {
4821   bool changed_p;
4822   int i, hard_regno, new_insns_num;
4823   unsigned int min_len, new_min_len, uid;
4824   rtx set, x, reg, dest_reg;
4825   basic_block last_bb;
4826   bitmap_iterator bi;
4827 
4828   lra_constraint_iter++;
4829   if (lra_dump_file != NULL)
4830     fprintf (lra_dump_file, "\n********** Local #%d: **********\n\n",
4831 	     lra_constraint_iter);
4832   changed_p = false;
4833   if (pic_offset_table_rtx
4834       && REGNO (pic_offset_table_rtx) >= FIRST_PSEUDO_REGISTER)
4835     lra_risky_transformations_p = true;
4836   else
4837     /* On the first iteration we should check IRA assignment
4838        correctness.  In rare cases, the assignments can be wrong as
4839        early clobbers operands are ignored in IRA or usages of
4840        paradoxical sub-registers are not taken into account by
4841        IRA.  */
4842     lra_risky_transformations_p = first_p;
4843   new_insn_uid_start = get_max_uid ();
4844   new_regno_start = first_p ? lra_constraint_new_regno_start : max_reg_num ();
4845   /* Mark used hard regs for target stack size calulations.  */
4846   for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4847     if (lra_reg_info[i].nrefs != 0
4848 	&& (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4849       {
4850 	int j, nregs;
4851 
4852 	nregs = hard_regno_nregs (hard_regno, lra_reg_info[i].biggest_mode);
4853 	for (j = 0; j < nregs; j++)
4854 	  df_set_regs_ever_live (hard_regno + j, true);
4855       }
4856   /* Do elimination before the equivalence processing as we can spill
4857      some pseudos during elimination.  */
4858   lra_eliminate (false, first_p);
4859   auto_bitmap equiv_insn_bitmap (&reg_obstack);
4860   for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4861     if (lra_reg_info[i].nrefs != 0)
4862       {
4863 	ira_reg_equiv[i].profitable_p = true;
4864 	reg = regno_reg_rtx[i];
4865 	if (lra_get_regno_hard_regno (i) < 0 && (x = get_equiv (reg)) != reg)
4866 	  {
4867 	    bool pseudo_p = contains_reg_p (x, false, false);
4868 
4869 	    /* After RTL transformation, we cannot guarantee that
4870 	       pseudo in the substitution was not reloaded which might
4871 	       make equivalence invalid.  For example, in reverse
4872 	       equiv of p0
4873 
4874 	       p0 <- ...
4875 	       ...
4876 	       equiv_mem <- p0
4877 
4878 	       the memory address register was reloaded before the 2nd
4879 	       insn.  */
4880 	    if ((! first_p && pseudo_p)
4881 		/* We don't use DF for compilation speed sake.  So it
4882 		   is problematic to update live info when we use an
4883 		   equivalence containing pseudos in more than one
4884 		   BB.  */
4885 		|| (pseudo_p && multi_block_pseudo_p (i))
4886 		/* If an init insn was deleted for some reason, cancel
4887 		   the equiv.  We could update the equiv insns after
4888 		   transformations including an equiv insn deletion
4889 		   but it is not worthy as such cases are extremely
4890 		   rare.  */
4891 		|| contains_deleted_insn_p (ira_reg_equiv[i].init_insns)
4892 		/* If it is not a reverse equivalence, we check that a
4893 		   pseudo in rhs of the init insn is not dying in the
4894 		   insn.  Otherwise, the live info at the beginning of
4895 		   the corresponding BB might be wrong after we
4896 		   removed the insn.  When the equiv can be a
4897 		   constant, the right hand side of the init insn can
4898 		   be a pseudo.  */
4899 		|| (! reverse_equiv_p (i)
4900 		    && (init_insn_rhs_dead_pseudo_p (i)
4901 			/* If we reloaded the pseudo in an equivalence
4902 			   init insn, we cannot remove the equiv init
4903 			   insns and the init insns might write into
4904 			   const memory in this case.  */
4905 			|| contains_reloaded_insn_p (i)))
4906 		/* Prevent access beyond equivalent memory for
4907 		   paradoxical subregs.  */
4908 		|| (MEM_P (x)
4909 		    && maybe_gt (GET_MODE_SIZE (lra_reg_info[i].biggest_mode),
4910 				 GET_MODE_SIZE (GET_MODE (x))))
4911 		|| (pic_offset_table_rtx
4912 		    && ((CONST_POOL_OK_P (PSEUDO_REGNO_MODE (i), x)
4913 			 && (targetm.preferred_reload_class
4914 			     (x, lra_get_allocno_class (i)) == NO_REGS))
4915 			|| contains_symbol_ref_p (x))))
4916 	      ira_reg_equiv[i].defined_p = false;
4917 	    if (contains_reg_p (x, false, true))
4918 	      ira_reg_equiv[i].profitable_p = false;
4919 	    if (get_equiv (reg) != reg)
4920 	      bitmap_ior_into (equiv_insn_bitmap, &lra_reg_info[i].insn_bitmap);
4921 	  }
4922       }
4923   for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4924     update_equiv (i);
4925   /* We should add all insns containing pseudos which should be
4926      substituted by their equivalences.  */
4927   EXECUTE_IF_SET_IN_BITMAP (equiv_insn_bitmap, 0, uid, bi)
4928     lra_push_insn_by_uid (uid);
4929   min_len = lra_insn_stack_length ();
4930   new_insns_num = 0;
4931   last_bb = NULL;
4932   changed_p = false;
4933   while ((new_min_len = lra_insn_stack_length ()) != 0)
4934     {
4935       curr_insn = lra_pop_insn ();
4936       --new_min_len;
4937       curr_bb = BLOCK_FOR_INSN (curr_insn);
4938       if (curr_bb != last_bb)
4939 	{
4940 	  last_bb = curr_bb;
4941 	  bb_reload_num = lra_curr_reload_num;
4942 	}
4943       if (min_len > new_min_len)
4944 	{
4945 	  min_len = new_min_len;
4946 	  new_insns_num = 0;
4947 	}
4948       if (new_insns_num > MAX_RELOAD_INSNS_NUMBER)
4949 	internal_error
4950 	  ("Max. number of generated reload insns per insn is achieved (%d)\n",
4951 	   MAX_RELOAD_INSNS_NUMBER);
4952       new_insns_num++;
4953       if (DEBUG_INSN_P (curr_insn))
4954 	{
4955 	  /* We need to check equivalence in debug insn and change
4956 	     pseudo to the equivalent value if necessary.  */
4957 	  curr_id = lra_get_insn_recog_data (curr_insn);
4958 	  if (bitmap_bit_p (equiv_insn_bitmap, INSN_UID (curr_insn)))
4959 	    {
4960 	      rtx old = *curr_id->operand_loc[0];
4961 	      *curr_id->operand_loc[0]
4962 		= simplify_replace_fn_rtx (old, NULL_RTX,
4963 					   loc_equivalence_callback, curr_insn);
4964 	      if (old != *curr_id->operand_loc[0])
4965 		{
4966 		  lra_update_insn_regno_info (curr_insn);
4967 		  changed_p = true;
4968 		}
4969 	    }
4970 	}
4971       else if (INSN_P (curr_insn))
4972 	{
4973 	  if ((set = single_set (curr_insn)) != NULL_RTX)
4974 	    {
4975 	      dest_reg = SET_DEST (set);
4976 	      /* The equivalence pseudo could be set up as SUBREG in a
4977 		 case when it is a call restore insn in a mode
4978 		 different from the pseudo mode.  */
4979 	      if (GET_CODE (dest_reg) == SUBREG)
4980 		dest_reg = SUBREG_REG (dest_reg);
4981 	      if ((REG_P (dest_reg)
4982 		   && (x = get_equiv (dest_reg)) != dest_reg
4983 		   /* Remove insns which set up a pseudo whose value
4984 		      cannot be changed.  Such insns might be not in
4985 		      init_insns because we don't update equiv data
4986 		      during insn transformations.
4987 
4988 		      As an example, let suppose that a pseudo got
4989 		      hard register and on the 1st pass was not
4990 		      changed to equivalent constant.  We generate an
4991 		      additional insn setting up the pseudo because of
4992 		      secondary memory movement.  Then the pseudo is
4993 		      spilled and we use the equiv constant.  In this
4994 		      case we should remove the additional insn and
4995 		      this insn is not init_insns list.  */
4996 		   && (! MEM_P (x) || MEM_READONLY_P (x)
4997 		       /* Check that this is actually an insn setting
4998 			  up the equivalence.  */
4999 		       || in_list_p (curr_insn,
5000 				     ira_reg_equiv
5001 				     [REGNO (dest_reg)].init_insns)))
5002 		  || (((x = get_equiv (SET_SRC (set))) != SET_SRC (set))
5003 		      && in_list_p (curr_insn,
5004 				    ira_reg_equiv
5005 				    [REGNO (SET_SRC (set))].init_insns)))
5006 		{
5007 		  /* This is equiv init insn of pseudo which did not get a
5008 		     hard register -- remove the insn.	*/
5009 		  if (lra_dump_file != NULL)
5010 		    {
5011 		      fprintf (lra_dump_file,
5012 			       "      Removing equiv init insn %i (freq=%d)\n",
5013 			       INSN_UID (curr_insn),
5014 			       REG_FREQ_FROM_BB (BLOCK_FOR_INSN (curr_insn)));
5015 		      dump_insn_slim (lra_dump_file, curr_insn);
5016 		    }
5017 		  if (contains_reg_p (x, true, false))
5018 		    lra_risky_transformations_p = true;
5019 		  lra_set_insn_deleted (curr_insn);
5020 		  continue;
5021 		}
5022 	    }
5023 	  curr_id = lra_get_insn_recog_data (curr_insn);
5024 	  curr_static_id = curr_id->insn_static_data;
5025 	  init_curr_insn_input_reloads ();
5026 	  init_curr_operand_mode ();
5027 	  if (curr_insn_transform (false))
5028 	    changed_p = true;
5029 	  /* Check non-transformed insns too for equiv change as USE
5030 	     or CLOBBER don't need reloads but can contain pseudos
5031 	     being changed on their equivalences.  */
5032 	  else if (bitmap_bit_p (equiv_insn_bitmap, INSN_UID (curr_insn))
5033 		   && loc_equivalence_change_p (&PATTERN (curr_insn)))
5034 	    {
5035 	      lra_update_insn_regno_info (curr_insn);
5036 	      changed_p = true;
5037 	    }
5038 	}
5039     }
5040 
5041   /* If we used a new hard regno, changed_p should be true because the
5042      hard reg is assigned to a new pseudo.  */
5043   if (flag_checking && !changed_p)
5044     {
5045       for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
5046 	if (lra_reg_info[i].nrefs != 0
5047 	    && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
5048 	  {
5049 	    int j, nregs = hard_regno_nregs (hard_regno,
5050 					     PSEUDO_REGNO_MODE (i));
5051 
5052 	    for (j = 0; j < nregs; j++)
5053 	      lra_assert (df_regs_ever_live_p (hard_regno + j));
5054 	  }
5055     }
5056   return changed_p;
5057 }
5058 
5059 static void initiate_invariants (void);
5060 static void finish_invariants (void);
5061 
5062 /* Initiate the LRA constraint pass.  It is done once per
5063    function.  */
5064 void
lra_constraints_init(void)5065 lra_constraints_init (void)
5066 {
5067   initiate_invariants ();
5068 }
5069 
5070 /* Finalize the LRA constraint pass.  It is done once per
5071    function.  */
5072 void
lra_constraints_finish(void)5073 lra_constraints_finish (void)
5074 {
5075   finish_invariants ();
5076 }
5077 
5078 
5079 
5080 /* Structure describes invariants for ineheritance.  */
5081 struct lra_invariant
5082 {
5083   /* The order number of the invariant.  */
5084   int num;
5085   /* The invariant RTX.  */
5086   rtx invariant_rtx;
5087   /* The origin insn of the invariant.  */
5088   rtx_insn *insn;
5089 };
5090 
5091 typedef lra_invariant invariant_t;
5092 typedef invariant_t *invariant_ptr_t;
5093 typedef const invariant_t *const_invariant_ptr_t;
5094 
5095 /* Pointer to the inheritance invariants.  */
5096 static vec<invariant_ptr_t> invariants;
5097 
5098 /* Allocation pool for the invariants.  */
5099 static object_allocator<lra_invariant> *invariants_pool;
5100 
5101 /* Hash table for the invariants.  */
5102 static htab_t invariant_table;
5103 
5104 /* Hash function for INVARIANT.  */
5105 static hashval_t
invariant_hash(const void * invariant)5106 invariant_hash (const void *invariant)
5107 {
5108   rtx inv = ((const_invariant_ptr_t) invariant)->invariant_rtx;
5109   return lra_rtx_hash (inv);
5110 }
5111 
5112 /* Equal function for invariants INVARIANT1 and INVARIANT2.  */
5113 static int
invariant_eq_p(const void * invariant1,const void * invariant2)5114 invariant_eq_p (const void *invariant1, const void *invariant2)
5115 {
5116   rtx inv1 = ((const_invariant_ptr_t) invariant1)->invariant_rtx;
5117   rtx inv2 = ((const_invariant_ptr_t) invariant2)->invariant_rtx;
5118 
5119   return rtx_equal_p (inv1, inv2);
5120 }
5121 
5122 /* Insert INVARIANT_RTX into the table if it is not there yet.  Return
5123    invariant which is in the table.  */
5124 static invariant_ptr_t
insert_invariant(rtx invariant_rtx)5125 insert_invariant (rtx invariant_rtx)
5126 {
5127   void **entry_ptr;
5128   invariant_t invariant;
5129   invariant_ptr_t invariant_ptr;
5130 
5131   invariant.invariant_rtx = invariant_rtx;
5132   entry_ptr = htab_find_slot (invariant_table, &invariant, INSERT);
5133   if (*entry_ptr == NULL)
5134     {
5135       invariant_ptr = invariants_pool->allocate ();
5136       invariant_ptr->invariant_rtx = invariant_rtx;
5137       invariant_ptr->insn = NULL;
5138       invariants.safe_push (invariant_ptr);
5139       *entry_ptr = (void *) invariant_ptr;
5140     }
5141   return (invariant_ptr_t) *entry_ptr;
5142 }
5143 
5144 /* Initiate the invariant table.  */
5145 static void
initiate_invariants(void)5146 initiate_invariants (void)
5147 {
5148   invariants.create (100);
5149   invariants_pool
5150     = new object_allocator<lra_invariant> ("Inheritance invariants");
5151   invariant_table = htab_create (100, invariant_hash, invariant_eq_p, NULL);
5152 }
5153 
5154 /* Finish the invariant table.  */
5155 static void
finish_invariants(void)5156 finish_invariants (void)
5157 {
5158   htab_delete (invariant_table);
5159   delete invariants_pool;
5160   invariants.release ();
5161 }
5162 
5163 /* Make the invariant table empty.  */
5164 static void
clear_invariants(void)5165 clear_invariants (void)
5166 {
5167   htab_empty (invariant_table);
5168   invariants_pool->release ();
5169   invariants.truncate (0);
5170 }
5171 
5172 
5173 
5174 /* This page contains code to do inheritance/split
5175    transformations.  */
5176 
5177 /* Number of reloads passed so far in current EBB.  */
5178 static int reloads_num;
5179 
5180 /* Number of calls passed so far in current EBB.  */
5181 static int calls_num;
5182 
5183 /* Current reload pseudo check for validity of elements in
5184    USAGE_INSNS.	 */
5185 static int curr_usage_insns_check;
5186 
5187 /* Info about last usage of registers in EBB to do inheritance/split
5188    transformation.  Inheritance transformation is done from a spilled
5189    pseudo and split transformations from a hard register or a pseudo
5190    assigned to a hard register.	 */
5191 struct usage_insns
5192 {
5193   /* If the value is equal to CURR_USAGE_INSNS_CHECK, then the member
5194      value INSNS is valid.  The insns is chain of optional debug insns
5195      and a finishing non-debug insn using the corresponding reg.  The
5196      value is also used to mark the registers which are set up in the
5197      current insn.  The negated insn uid is used for this.  */
5198   int check;
5199   /* Value of global reloads_num at the last insn in INSNS.  */
5200   int reloads_num;
5201   /* Value of global reloads_nums at the last insn in INSNS.  */
5202   int calls_num;
5203   /* It can be true only for splitting.	 And it means that the restore
5204      insn should be put after insn given by the following member.  */
5205   bool after_p;
5206   /* Next insns in the current EBB which use the original reg and the
5207      original reg value is not changed between the current insn and
5208      the next insns.  In order words, e.g. for inheritance, if we need
5209      to use the original reg value again in the next insns we can try
5210      to use the value in a hard register from a reload insn of the
5211      current insn.  */
5212   rtx insns;
5213 };
5214 
5215 /* Map: regno -> corresponding pseudo usage insns.  */
5216 static struct usage_insns *usage_insns;
5217 
5218 static void
setup_next_usage_insn(int regno,rtx insn,int reloads_num,bool after_p)5219 setup_next_usage_insn (int regno, rtx insn, int reloads_num, bool after_p)
5220 {
5221   usage_insns[regno].check = curr_usage_insns_check;
5222   usage_insns[regno].insns = insn;
5223   usage_insns[regno].reloads_num = reloads_num;
5224   usage_insns[regno].calls_num = calls_num;
5225   usage_insns[regno].after_p = after_p;
5226 }
5227 
5228 /* The function is used to form list REGNO usages which consists of
5229    optional debug insns finished by a non-debug insn using REGNO.
5230    RELOADS_NUM is current number of reload insns processed so far.  */
5231 static void
add_next_usage_insn(int regno,rtx_insn * insn,int reloads_num)5232 add_next_usage_insn (int regno, rtx_insn *insn, int reloads_num)
5233 {
5234   rtx next_usage_insns;
5235 
5236   if (usage_insns[regno].check == curr_usage_insns_check
5237       && (next_usage_insns = usage_insns[regno].insns) != NULL_RTX
5238       && DEBUG_INSN_P (insn))
5239     {
5240       /* Check that we did not add the debug insn yet.	*/
5241       if (next_usage_insns != insn
5242 	  && (GET_CODE (next_usage_insns) != INSN_LIST
5243 	      || XEXP (next_usage_insns, 0) != insn))
5244 	usage_insns[regno].insns = gen_rtx_INSN_LIST (VOIDmode, insn,
5245 						      next_usage_insns);
5246     }
5247   else if (NONDEBUG_INSN_P (insn))
5248     setup_next_usage_insn (regno, insn, reloads_num, false);
5249   else
5250     usage_insns[regno].check = 0;
5251 }
5252 
5253 /* Return first non-debug insn in list USAGE_INSNS.  */
5254 static rtx_insn *
skip_usage_debug_insns(rtx usage_insns)5255 skip_usage_debug_insns (rtx usage_insns)
5256 {
5257   rtx insn;
5258 
5259   /* Skip debug insns.  */
5260   for (insn = usage_insns;
5261        insn != NULL_RTX && GET_CODE (insn) == INSN_LIST;
5262        insn = XEXP (insn, 1))
5263     ;
5264   return safe_as_a <rtx_insn *> (insn);
5265 }
5266 
5267 /* Return true if we need secondary memory moves for insn in
5268    USAGE_INSNS after inserting inherited pseudo of class INHER_CL
5269    into the insn.  */
5270 static bool
check_secondary_memory_needed_p(enum reg_class inher_cl ATTRIBUTE_UNUSED,rtx usage_insns ATTRIBUTE_UNUSED)5271 check_secondary_memory_needed_p (enum reg_class inher_cl ATTRIBUTE_UNUSED,
5272 				 rtx usage_insns ATTRIBUTE_UNUSED)
5273 {
5274   rtx_insn *insn;
5275   rtx set, dest;
5276   enum reg_class cl;
5277 
5278   if (inher_cl == ALL_REGS
5279       || (insn = skip_usage_debug_insns (usage_insns)) == NULL_RTX)
5280     return false;
5281   lra_assert (INSN_P (insn));
5282   if ((set = single_set (insn)) == NULL_RTX || ! REG_P (SET_DEST (set)))
5283     return false;
5284   dest = SET_DEST (set);
5285   if (! REG_P (dest))
5286     return false;
5287   lra_assert (inher_cl != NO_REGS);
5288   cl = get_reg_class (REGNO (dest));
5289   return (cl != NO_REGS && cl != ALL_REGS
5290 	  && targetm.secondary_memory_needed (GET_MODE (dest), inher_cl, cl));
5291 }
5292 
5293 /* Registers involved in inheritance/split in the current EBB
5294    (inheritance/split pseudos and original registers).	*/
5295 static bitmap_head check_only_regs;
5296 
5297 /* Reload pseudos cannot be involded in invariant inheritance in the
5298    current EBB.  */
5299 static bitmap_head invalid_invariant_regs;
5300 
5301 /* Do inheritance transformations for insn INSN, which defines (if
5302    DEF_P) or uses ORIGINAL_REGNO.  NEXT_USAGE_INSNS specifies which
5303    instruction in the EBB next uses ORIGINAL_REGNO; it has the same
5304    form as the "insns" field of usage_insns.  Return true if we
5305    succeed in such transformation.
5306 
5307    The transformations look like:
5308 
5309      p <- ...		  i <- ...
5310      ...		  p <- i    (new insn)
5311      ...	     =>
5312      <- ... p ...	  <- ... i ...
5313    or
5314      ...		  i <- p    (new insn)
5315      <- ... p ...	  <- ... i ...
5316      ...	     =>
5317      <- ... p ...	  <- ... i ...
5318    where p is a spilled original pseudo and i is a new inheritance pseudo.
5319 
5320 
5321    The inheritance pseudo has the smallest class of two classes CL and
5322    class of ORIGINAL REGNO.  */
5323 static bool
inherit_reload_reg(bool def_p,int original_regno,enum reg_class cl,rtx_insn * insn,rtx next_usage_insns)5324 inherit_reload_reg (bool def_p, int original_regno,
5325 		    enum reg_class cl, rtx_insn *insn, rtx next_usage_insns)
5326 {
5327   if (optimize_function_for_size_p (cfun))
5328     return false;
5329 
5330   enum reg_class rclass = lra_get_allocno_class (original_regno);
5331   rtx original_reg = regno_reg_rtx[original_regno];
5332   rtx new_reg, usage_insn;
5333   rtx_insn *new_insns;
5334 
5335   lra_assert (! usage_insns[original_regno].after_p);
5336   if (lra_dump_file != NULL)
5337     fprintf (lra_dump_file,
5338 	     "    <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
5339   if (! ira_reg_classes_intersect_p[cl][rclass])
5340     {
5341       if (lra_dump_file != NULL)
5342 	{
5343 	  fprintf (lra_dump_file,
5344 		   "    Rejecting inheritance for %d "
5345 		   "because of disjoint classes %s and %s\n",
5346 		   original_regno, reg_class_names[cl],
5347 		   reg_class_names[rclass]);
5348 	  fprintf (lra_dump_file,
5349 		   "    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
5350 	}
5351       return false;
5352     }
5353   if ((ira_class_subset_p[cl][rclass] && cl != rclass)
5354       /* We don't use a subset of two classes because it can be
5355 	 NO_REGS.  This transformation is still profitable in most
5356 	 cases even if the classes are not intersected as register
5357 	 move is probably cheaper than a memory load.  */
5358       || ira_class_hard_regs_num[cl] < ira_class_hard_regs_num[rclass])
5359     {
5360       if (lra_dump_file != NULL)
5361 	fprintf (lra_dump_file, "    Use smallest class of %s and %s\n",
5362 		 reg_class_names[cl], reg_class_names[rclass]);
5363 
5364       rclass = cl;
5365     }
5366   if (check_secondary_memory_needed_p (rclass, next_usage_insns))
5367     {
5368       /* Reject inheritance resulting in secondary memory moves.
5369 	 Otherwise, there is a danger in LRA cycling.  Also such
5370 	 transformation will be unprofitable.  */
5371       if (lra_dump_file != NULL)
5372 	{
5373 	  rtx_insn *insn = skip_usage_debug_insns (next_usage_insns);
5374 	  rtx set = single_set (insn);
5375 
5376 	  lra_assert (set != NULL_RTX);
5377 
5378 	  rtx dest = SET_DEST (set);
5379 
5380 	  lra_assert (REG_P (dest));
5381 	  fprintf (lra_dump_file,
5382 		   "    Rejecting inheritance for insn %d(%s)<-%d(%s) "
5383 		   "as secondary mem is needed\n",
5384 		   REGNO (dest), reg_class_names[get_reg_class (REGNO (dest))],
5385 		   original_regno, reg_class_names[rclass]);
5386 	  fprintf (lra_dump_file,
5387 		   "    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
5388 	}
5389       return false;
5390     }
5391   new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
5392 				rclass, "inheritance");
5393   start_sequence ();
5394   if (def_p)
5395     lra_emit_move (original_reg, new_reg);
5396   else
5397     lra_emit_move (new_reg, original_reg);
5398   new_insns = get_insns ();
5399   end_sequence ();
5400   if (NEXT_INSN (new_insns) != NULL_RTX)
5401     {
5402       if (lra_dump_file != NULL)
5403 	{
5404 	  fprintf (lra_dump_file,
5405 		   "    Rejecting inheritance %d->%d "
5406 		   "as it results in 2 or more insns:\n",
5407 		   original_regno, REGNO (new_reg));
5408 	  dump_rtl_slim (lra_dump_file, new_insns, NULL, -1, 0);
5409 	  fprintf (lra_dump_file,
5410 		   "	>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
5411 	}
5412       return false;
5413     }
5414   lra_substitute_pseudo_within_insn (insn, original_regno, new_reg, false);
5415   lra_update_insn_regno_info (insn);
5416   if (! def_p)
5417     /* We now have a new usage insn for original regno.  */
5418     setup_next_usage_insn (original_regno, new_insns, reloads_num, false);
5419   if (lra_dump_file != NULL)
5420     fprintf (lra_dump_file, "    Original reg change %d->%d (bb%d):\n",
5421 	     original_regno, REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
5422   lra_reg_info[REGNO (new_reg)].restore_rtx = regno_reg_rtx[original_regno];
5423   bitmap_set_bit (&check_only_regs, REGNO (new_reg));
5424   bitmap_set_bit (&check_only_regs, original_regno);
5425   bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
5426   if (def_p)
5427     lra_process_new_insns (insn, NULL, new_insns,
5428 			   "Add original<-inheritance");
5429   else
5430     lra_process_new_insns (insn, new_insns, NULL,
5431 			   "Add inheritance<-original");
5432   while (next_usage_insns != NULL_RTX)
5433     {
5434       if (GET_CODE (next_usage_insns) != INSN_LIST)
5435 	{
5436 	  usage_insn = next_usage_insns;
5437 	  lra_assert (NONDEBUG_INSN_P (usage_insn));
5438 	  next_usage_insns = NULL;
5439 	}
5440       else
5441 	{
5442 	  usage_insn = XEXP (next_usage_insns, 0);
5443 	  lra_assert (DEBUG_INSN_P (usage_insn));
5444 	  next_usage_insns = XEXP (next_usage_insns, 1);
5445 	}
5446       lra_substitute_pseudo (&usage_insn, original_regno, new_reg, false,
5447 			     DEBUG_INSN_P (usage_insn));
5448       lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
5449       if (lra_dump_file != NULL)
5450 	{
5451 	  basic_block bb = BLOCK_FOR_INSN (usage_insn);
5452 	  fprintf (lra_dump_file,
5453 		   "    Inheritance reuse change %d->%d (bb%d):\n",
5454 		   original_regno, REGNO (new_reg),
5455 		   bb ? bb->index : -1);
5456 	  dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
5457 	}
5458     }
5459   if (lra_dump_file != NULL)
5460     fprintf (lra_dump_file,
5461 	     "	  >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
5462   return true;
5463 }
5464 
5465 /* Return true if we need a caller save/restore for pseudo REGNO which
5466    was assigned to a hard register.  */
5467 static inline bool
need_for_call_save_p(int regno)5468 need_for_call_save_p (int regno)
5469 {
5470   lra_assert (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0);
5471   return (usage_insns[regno].calls_num < calls_num
5472 	  && (overlaps_hard_reg_set_p
5473 	      ((flag_ipa_ra &&
5474 		! hard_reg_set_empty_p (lra_reg_info[regno].actual_call_used_reg_set))
5475 	       ? lra_reg_info[regno].actual_call_used_reg_set
5476 	       : call_used_reg_set,
5477 	       PSEUDO_REGNO_MODE (regno), reg_renumber[regno])
5478 	      || (targetm.hard_regno_call_part_clobbered
5479 		  (lra_reg_info[regno].call_insn,
5480 		   reg_renumber[regno], PSEUDO_REGNO_MODE (regno)))));
5481 }
5482 
5483 /* Global registers occurring in the current EBB.  */
5484 static bitmap_head ebb_global_regs;
5485 
5486 /* Return true if we need a split for hard register REGNO or pseudo
5487    REGNO which was assigned to a hard register.
5488    POTENTIAL_RELOAD_HARD_REGS contains hard registers which might be
5489    used for reloads since the EBB end.	It is an approximation of the
5490    used hard registers in the split range.  The exact value would
5491    require expensive calculations.  If we were aggressive with
5492    splitting because of the approximation, the split pseudo will save
5493    the same hard register assignment and will be removed in the undo
5494    pass.  We still need the approximation because too aggressive
5495    splitting would result in too inaccurate cost calculation in the
5496    assignment pass because of too many generated moves which will be
5497    probably removed in the undo pass.  */
5498 static inline bool
need_for_split_p(HARD_REG_SET potential_reload_hard_regs,int regno)5499 need_for_split_p (HARD_REG_SET potential_reload_hard_regs, int regno)
5500 {
5501   int hard_regno = regno < FIRST_PSEUDO_REGISTER ? regno : reg_renumber[regno];
5502 
5503   lra_assert (hard_regno >= 0);
5504   return ((TEST_HARD_REG_BIT (potential_reload_hard_regs, hard_regno)
5505 	   /* Don't split eliminable hard registers, otherwise we can
5506 	      split hard registers like hard frame pointer, which
5507 	      lives on BB start/end according to DF-infrastructure,
5508 	      when there is a pseudo assigned to the register and
5509 	      living in the same BB.  */
5510 	   && (regno >= FIRST_PSEUDO_REGISTER
5511 	       || ! TEST_HARD_REG_BIT (eliminable_regset, hard_regno))
5512 	   && ! TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno)
5513 	   /* Don't split call clobbered hard regs living through
5514 	      calls, otherwise we might have a check problem in the
5515 	      assign sub-pass as in the most cases (exception is a
5516 	      situation when lra_risky_transformations_p value is
5517 	      true) the assign pass assumes that all pseudos living
5518 	      through calls are assigned to call saved hard regs.  */
5519 	   && (regno >= FIRST_PSEUDO_REGISTER
5520 	       || ! TEST_HARD_REG_BIT (call_used_reg_set, regno)
5521 	       || usage_insns[regno].calls_num == calls_num)
5522 	   /* We need at least 2 reloads to make pseudo splitting
5523 	      profitable.  We should provide hard regno splitting in
5524 	      any case to solve 1st insn scheduling problem when
5525 	      moving hard register definition up might result in
5526 	      impossibility to find hard register for reload pseudo of
5527 	      small register class.  */
5528 	   && (usage_insns[regno].reloads_num
5529 	       + (regno < FIRST_PSEUDO_REGISTER ? 0 : 3) < reloads_num)
5530 	   && (regno < FIRST_PSEUDO_REGISTER
5531 	       /* For short living pseudos, spilling + inheritance can
5532 		  be considered a substitution for splitting.
5533 		  Therefore we do not splitting for local pseudos.  It
5534 		  decreases also aggressiveness of splitting.  The
5535 		  minimal number of references is chosen taking into
5536 		  account that for 2 references splitting has no sense
5537 		  as we can just spill the pseudo.  */
5538 	       || (regno >= FIRST_PSEUDO_REGISTER
5539 		   && lra_reg_info[regno].nrefs > 3
5540 		   && bitmap_bit_p (&ebb_global_regs, regno))))
5541 	  || (regno >= FIRST_PSEUDO_REGISTER && need_for_call_save_p (regno)));
5542 }
5543 
5544 /* Return class for the split pseudo created from original pseudo with
5545    ALLOCNO_CLASS and MODE which got a hard register HARD_REGNO.	 We
5546    choose subclass of ALLOCNO_CLASS which contains HARD_REGNO and
5547    results in no secondary memory movements.  */
5548 static enum reg_class
choose_split_class(enum reg_class allocno_class,int hard_regno ATTRIBUTE_UNUSED,machine_mode mode ATTRIBUTE_UNUSED)5549 choose_split_class (enum reg_class allocno_class,
5550 		    int hard_regno ATTRIBUTE_UNUSED,
5551 		    machine_mode mode ATTRIBUTE_UNUSED)
5552 {
5553   int i;
5554   enum reg_class cl, best_cl = NO_REGS;
5555   enum reg_class hard_reg_class ATTRIBUTE_UNUSED
5556     = REGNO_REG_CLASS (hard_regno);
5557 
5558   if (! targetm.secondary_memory_needed (mode, allocno_class, allocno_class)
5559       && TEST_HARD_REG_BIT (reg_class_contents[allocno_class], hard_regno))
5560     return allocno_class;
5561   for (i = 0;
5562        (cl = reg_class_subclasses[allocno_class][i]) != LIM_REG_CLASSES;
5563        i++)
5564     if (! targetm.secondary_memory_needed (mode, cl, hard_reg_class)
5565 	&& ! targetm.secondary_memory_needed (mode, hard_reg_class, cl)
5566 	&& TEST_HARD_REG_BIT (reg_class_contents[cl], hard_regno)
5567 	&& (best_cl == NO_REGS
5568 	    || ira_class_hard_regs_num[best_cl] < ira_class_hard_regs_num[cl]))
5569       best_cl = cl;
5570   return best_cl;
5571 }
5572 
5573 /* Copy any equivalence information from ORIGINAL_REGNO to NEW_REGNO.
5574    It only makes sense to call this function if NEW_REGNO is always
5575    equal to ORIGINAL_REGNO.  */
5576 
5577 static void
lra_copy_reg_equiv(unsigned int new_regno,unsigned int original_regno)5578 lra_copy_reg_equiv (unsigned int new_regno, unsigned int original_regno)
5579 {
5580   if (!ira_reg_equiv[original_regno].defined_p)
5581     return;
5582 
5583   ira_expand_reg_equiv ();
5584   ira_reg_equiv[new_regno].defined_p = true;
5585   if (ira_reg_equiv[original_regno].memory)
5586     ira_reg_equiv[new_regno].memory
5587       = copy_rtx (ira_reg_equiv[original_regno].memory);
5588   if (ira_reg_equiv[original_regno].constant)
5589     ira_reg_equiv[new_regno].constant
5590       = copy_rtx (ira_reg_equiv[original_regno].constant);
5591   if (ira_reg_equiv[original_regno].invariant)
5592     ira_reg_equiv[new_regno].invariant
5593       = copy_rtx (ira_reg_equiv[original_regno].invariant);
5594 }
5595 
5596 /* Do split transformations for insn INSN, which defines or uses
5597    ORIGINAL_REGNO.  NEXT_USAGE_INSNS specifies which instruction in
5598    the EBB next uses ORIGINAL_REGNO; it has the same form as the
5599    "insns" field of usage_insns.  If TO is not NULL, we don't use
5600    usage_insns, we put restore insns after TO insn.  It is a case when
5601    we call it from lra_split_hard_reg_for, outside the inheritance
5602    pass.
5603 
5604    The transformations look like:
5605 
5606      p <- ...		  p <- ...
5607      ...		  s <- p    (new insn -- save)
5608      ...	     =>
5609      ...		  p <- s    (new insn -- restore)
5610      <- ... p ...	  <- ... p ...
5611    or
5612      <- ... p ...	  <- ... p ...
5613      ...		  s <- p    (new insn -- save)
5614      ...	     =>
5615      ...		  p <- s    (new insn -- restore)
5616      <- ... p ...	  <- ... p ...
5617 
5618    where p is an original pseudo got a hard register or a hard
5619    register and s is a new split pseudo.  The save is put before INSN
5620    if BEFORE_P is true.	 Return true if we succeed in such
5621    transformation.  */
5622 static bool
split_reg(bool before_p,int original_regno,rtx_insn * insn,rtx next_usage_insns,rtx_insn * to)5623 split_reg (bool before_p, int original_regno, rtx_insn *insn,
5624 	   rtx next_usage_insns, rtx_insn *to)
5625 {
5626   enum reg_class rclass;
5627   rtx original_reg;
5628   int hard_regno, nregs;
5629   rtx new_reg, usage_insn;
5630   rtx_insn *restore, *save;
5631   bool after_p;
5632   bool call_save_p;
5633   machine_mode mode;
5634 
5635   if (original_regno < FIRST_PSEUDO_REGISTER)
5636     {
5637       rclass = ira_allocno_class_translate[REGNO_REG_CLASS (original_regno)];
5638       hard_regno = original_regno;
5639       call_save_p = false;
5640       nregs = 1;
5641       mode = lra_reg_info[hard_regno].biggest_mode;
5642       machine_mode reg_rtx_mode = GET_MODE (regno_reg_rtx[hard_regno]);
5643       /* A reg can have a biggest_mode of VOIDmode if it was only ever seen
5644 	 as part of a multi-word register.  In that case, or if the biggest
5645 	 mode was larger than a register, just use the reg_rtx.  Otherwise,
5646 	 limit the size to that of the biggest access in the function.  */
5647       if (mode == VOIDmode
5648 	  || paradoxical_subreg_p (mode, reg_rtx_mode))
5649 	{
5650 	  original_reg = regno_reg_rtx[hard_regno];
5651 	  mode = reg_rtx_mode;
5652 	}
5653       else
5654 	original_reg = gen_rtx_REG (mode, hard_regno);
5655     }
5656   else
5657     {
5658       mode = PSEUDO_REGNO_MODE (original_regno);
5659       hard_regno = reg_renumber[original_regno];
5660       nregs = hard_regno_nregs (hard_regno, mode);
5661       rclass = lra_get_allocno_class (original_regno);
5662       original_reg = regno_reg_rtx[original_regno];
5663       call_save_p = need_for_call_save_p (original_regno);
5664     }
5665   lra_assert (hard_regno >= 0);
5666   if (lra_dump_file != NULL)
5667     fprintf (lra_dump_file,
5668 	     "	  ((((((((((((((((((((((((((((((((((((((((((((((((\n");
5669 
5670   if (call_save_p)
5671     {
5672       mode = HARD_REGNO_CALLER_SAVE_MODE (hard_regno,
5673 					  hard_regno_nregs (hard_regno, mode),
5674 					  mode);
5675       new_reg = lra_create_new_reg (mode, NULL_RTX, NO_REGS, "save");
5676     }
5677   else
5678     {
5679       rclass = choose_split_class (rclass, hard_regno, mode);
5680       if (rclass == NO_REGS)
5681 	{
5682 	  if (lra_dump_file != NULL)
5683 	    {
5684 	      fprintf (lra_dump_file,
5685 		       "    Rejecting split of %d(%s): "
5686 		       "no good reg class for %d(%s)\n",
5687 		       original_regno,
5688 		       reg_class_names[lra_get_allocno_class (original_regno)],
5689 		       hard_regno,
5690 		       reg_class_names[REGNO_REG_CLASS (hard_regno)]);
5691 	      fprintf
5692 		(lra_dump_file,
5693 		 "    ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5694 	    }
5695 	  return false;
5696 	}
5697       /* Split_if_necessary can split hard registers used as part of a
5698 	 multi-register mode but splits each register individually.  The
5699 	 mode used for each independent register may not be supported
5700 	 so reject the split.  Splitting the wider mode should theoretically
5701 	 be possible but is not implemented.  */
5702       if (!targetm.hard_regno_mode_ok (hard_regno, mode))
5703 	{
5704 	  if (lra_dump_file != NULL)
5705 	    {
5706 	      fprintf (lra_dump_file,
5707 		       "    Rejecting split of %d(%s): unsuitable mode %s\n",
5708 		       original_regno,
5709 		       reg_class_names[lra_get_allocno_class (original_regno)],
5710 		       GET_MODE_NAME (mode));
5711 	      fprintf
5712 		(lra_dump_file,
5713 		 "    ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5714 	    }
5715 	  return false;
5716 	}
5717       new_reg = lra_create_new_reg (mode, original_reg, rclass, "split");
5718       reg_renumber[REGNO (new_reg)] = hard_regno;
5719     }
5720   int new_regno = REGNO (new_reg);
5721   save = emit_spill_move (true, new_reg, original_reg);
5722   if (NEXT_INSN (save) != NULL_RTX && !call_save_p)
5723     {
5724       if (lra_dump_file != NULL)
5725 	{
5726 	  fprintf
5727 	    (lra_dump_file,
5728 	     "	  Rejecting split %d->%d resulting in > 2 save insns:\n",
5729 	     original_regno, new_regno);
5730 	  dump_rtl_slim (lra_dump_file, save, NULL, -1, 0);
5731 	  fprintf (lra_dump_file,
5732 		   "	))))))))))))))))))))))))))))))))))))))))))))))))\n");
5733 	}
5734       return false;
5735     }
5736   restore = emit_spill_move (false, new_reg, original_reg);
5737   if (NEXT_INSN (restore) != NULL_RTX && !call_save_p)
5738     {
5739       if (lra_dump_file != NULL)
5740 	{
5741 	  fprintf (lra_dump_file,
5742 		   "	Rejecting split %d->%d "
5743 		   "resulting in > 2 restore insns:\n",
5744 		   original_regno, new_regno);
5745 	  dump_rtl_slim (lra_dump_file, restore, NULL, -1, 0);
5746 	  fprintf (lra_dump_file,
5747 		   "	))))))))))))))))))))))))))))))))))))))))))))))))\n");
5748 	}
5749       return false;
5750     }
5751   /* Transfer equivalence information to the spill register, so that
5752      if we fail to allocate the spill register, we have the option of
5753      rematerializing the original value instead of spilling to the stack.  */
5754   if (!HARD_REGISTER_NUM_P (original_regno)
5755       && mode == PSEUDO_REGNO_MODE (original_regno))
5756     lra_copy_reg_equiv (new_regno, original_regno);
5757   lra_reg_info[new_regno].restore_rtx = regno_reg_rtx[original_regno];
5758   bitmap_set_bit (&lra_split_regs, new_regno);
5759   if (to != NULL)
5760     {
5761       lra_assert (next_usage_insns == NULL);
5762       usage_insn = to;
5763       after_p = TRUE;
5764     }
5765   else
5766     {
5767       /* We need check_only_regs only inside the inheritance pass.  */
5768       bitmap_set_bit (&check_only_regs, new_regno);
5769       bitmap_set_bit (&check_only_regs, original_regno);
5770       after_p = usage_insns[original_regno].after_p;
5771       for (;;)
5772 	{
5773 	  if (GET_CODE (next_usage_insns) != INSN_LIST)
5774 	    {
5775 	      usage_insn = next_usage_insns;
5776 	      break;
5777 	    }
5778 	  usage_insn = XEXP (next_usage_insns, 0);
5779 	  lra_assert (DEBUG_INSN_P (usage_insn));
5780 	  next_usage_insns = XEXP (next_usage_insns, 1);
5781 	  lra_substitute_pseudo (&usage_insn, original_regno, new_reg, false,
5782 				 true);
5783 	  lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
5784 	  if (lra_dump_file != NULL)
5785 	    {
5786 	      fprintf (lra_dump_file, "    Split reuse change %d->%d:\n",
5787 		       original_regno, new_regno);
5788 	      dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
5789 	    }
5790 	}
5791     }
5792   lra_assert (NOTE_P (usage_insn) || NONDEBUG_INSN_P (usage_insn));
5793   lra_assert (usage_insn != insn || (after_p && before_p));
5794   lra_process_new_insns (as_a <rtx_insn *> (usage_insn),
5795 			 after_p ? NULL : restore,
5796 			 after_p ? restore : NULL,
5797 			 call_save_p
5798 			 ?  "Add reg<-save" : "Add reg<-split");
5799   lra_process_new_insns (insn, before_p ? save : NULL,
5800 			 before_p ? NULL : save,
5801 			 call_save_p
5802 			 ?  "Add save<-reg" : "Add split<-reg");
5803   if (nregs > 1)
5804     /* If we are trying to split multi-register.  We should check
5805        conflicts on the next assignment sub-pass.  IRA can allocate on
5806        sub-register levels, LRA do this on pseudos level right now and
5807        this discrepancy may create allocation conflicts after
5808        splitting.  */
5809     lra_risky_transformations_p = true;
5810   if (lra_dump_file != NULL)
5811     fprintf (lra_dump_file,
5812 	     "	  ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5813   return true;
5814 }
5815 
5816 /* Split a hard reg for reload pseudo REGNO having RCLASS and living
5817    in the range [FROM, TO].  Return true if did a split.  Otherwise,
5818    return false.  */
5819 bool
spill_hard_reg_in_range(int regno,enum reg_class rclass,rtx_insn * from,rtx_insn * to)5820 spill_hard_reg_in_range (int regno, enum reg_class rclass, rtx_insn *from, rtx_insn *to)
5821 {
5822   int i, hard_regno;
5823   int rclass_size;
5824   rtx_insn *insn;
5825   unsigned int uid;
5826   bitmap_iterator bi;
5827   HARD_REG_SET ignore;
5828 
5829   lra_assert (from != NULL && to != NULL);
5830   CLEAR_HARD_REG_SET (ignore);
5831   EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
5832     {
5833       lra_insn_recog_data_t id = lra_insn_recog_data[uid];
5834       struct lra_static_insn_data *static_id = id->insn_static_data;
5835       struct lra_insn_reg *reg;
5836 
5837       for (reg = id->regs; reg != NULL; reg = reg->next)
5838 	if (reg->regno < FIRST_PSEUDO_REGISTER)
5839 	  SET_HARD_REG_BIT (ignore, reg->regno);
5840       for (reg = static_id->hard_regs; reg != NULL; reg = reg->next)
5841 	SET_HARD_REG_BIT (ignore, reg->regno);
5842     }
5843   rclass_size = ira_class_hard_regs_num[rclass];
5844   for (i = 0; i < rclass_size; i++)
5845     {
5846       hard_regno = ira_class_hard_regs[rclass][i];
5847       if (! TEST_HARD_REG_BIT (lra_reg_info[regno].conflict_hard_regs, hard_regno)
5848 	  || TEST_HARD_REG_BIT (ignore, hard_regno))
5849 	continue;
5850       for (insn = from; insn != NEXT_INSN (to); insn = NEXT_INSN (insn))
5851 	{
5852 	  struct lra_static_insn_data *static_id;
5853 	  struct lra_insn_reg *reg;
5854 
5855 	  if (!INSN_P (insn))
5856 	      continue;
5857 	  if (bitmap_bit_p (&lra_reg_info[hard_regno].insn_bitmap,
5858 			    INSN_UID (insn)))
5859 	    break;
5860 	  static_id = lra_get_insn_recog_data (insn)->insn_static_data;
5861 	  for (reg = static_id->hard_regs; reg != NULL; reg = reg->next)
5862 	    if (reg->regno == hard_regno)
5863 	      break;
5864 	  if (reg != NULL)
5865 	    break;
5866 	}
5867       if (insn != NEXT_INSN (to))
5868 	continue;
5869       if (split_reg (TRUE, hard_regno, from, NULL, to))
5870 	return true;
5871     }
5872   return false;
5873 }
5874 
5875 /* Recognize that we need a split transformation for insn INSN, which
5876    defines or uses REGNO in its insn biggest MODE (we use it only if
5877    REGNO is a hard register).  POTENTIAL_RELOAD_HARD_REGS contains
5878    hard registers which might be used for reloads since the EBB end.
5879    Put the save before INSN if BEFORE_P is true.  MAX_UID is maximla
5880    uid before starting INSN processing.  Return true if we succeed in
5881    such transformation.  */
5882 static bool
split_if_necessary(int regno,machine_mode mode,HARD_REG_SET potential_reload_hard_regs,bool before_p,rtx_insn * insn,int max_uid)5883 split_if_necessary (int regno, machine_mode mode,
5884 		    HARD_REG_SET potential_reload_hard_regs,
5885 		    bool before_p, rtx_insn *insn, int max_uid)
5886 {
5887   bool res = false;
5888   int i, nregs = 1;
5889   rtx next_usage_insns;
5890 
5891   if (regno < FIRST_PSEUDO_REGISTER)
5892     nregs = hard_regno_nregs (regno, mode);
5893   for (i = 0; i < nregs; i++)
5894     if (usage_insns[regno + i].check == curr_usage_insns_check
5895 	&& (next_usage_insns = usage_insns[regno + i].insns) != NULL_RTX
5896 	/* To avoid processing the register twice or more.  */
5897 	&& ((GET_CODE (next_usage_insns) != INSN_LIST
5898 	     && INSN_UID (next_usage_insns) < max_uid)
5899 	    || (GET_CODE (next_usage_insns) == INSN_LIST
5900 		&& (INSN_UID (XEXP (next_usage_insns, 0)) < max_uid)))
5901 	&& need_for_split_p (potential_reload_hard_regs, regno + i)
5902 	&& split_reg (before_p, regno + i, insn, next_usage_insns, NULL))
5903     res = true;
5904   return res;
5905 }
5906 
5907 /* Return TRUE if rtx X is considered as an invariant for
5908    inheritance.  */
5909 static bool
invariant_p(const_rtx x)5910 invariant_p (const_rtx x)
5911 {
5912   machine_mode mode;
5913   const char *fmt;
5914   enum rtx_code code;
5915   int i, j;
5916 
5917   if (side_effects_p (x))
5918     return false;
5919 
5920   code = GET_CODE (x);
5921   mode = GET_MODE (x);
5922   if (code == SUBREG)
5923     {
5924       x = SUBREG_REG (x);
5925       code = GET_CODE (x);
5926       mode = wider_subreg_mode (mode, GET_MODE (x));
5927     }
5928 
5929   if (MEM_P (x))
5930     return false;
5931 
5932   if (REG_P (x))
5933     {
5934       int i, nregs, regno = REGNO (x);
5935 
5936       if (regno >= FIRST_PSEUDO_REGISTER || regno == STACK_POINTER_REGNUM
5937 	  || TEST_HARD_REG_BIT (eliminable_regset, regno)
5938 	  || GET_MODE_CLASS (GET_MODE (x)) == MODE_CC)
5939 	return false;
5940       nregs = hard_regno_nregs (regno, mode);
5941       for (i = 0; i < nregs; i++)
5942 	if (! fixed_regs[regno + i]
5943 	    /* A hard register may be clobbered in the current insn
5944 	       but we can ignore this case because if the hard
5945 	       register is used it should be set somewhere after the
5946 	       clobber.  */
5947 	    || bitmap_bit_p (&invalid_invariant_regs, regno + i))
5948 	  return false;
5949     }
5950   fmt = GET_RTX_FORMAT (code);
5951   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
5952     {
5953       if (fmt[i] == 'e')
5954 	{
5955 	  if (! invariant_p (XEXP (x, i)))
5956 	    return false;
5957 	}
5958       else if (fmt[i] == 'E')
5959 	{
5960 	  for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5961 	    if (! invariant_p (XVECEXP (x, i, j)))
5962 	      return false;
5963 	}
5964     }
5965   return true;
5966 }
5967 
5968 /* We have 'dest_reg <- invariant'.  Let us try to make an invariant
5969    inheritance transformation (using dest_reg instead invariant in a
5970    subsequent insn).  */
5971 static bool
process_invariant_for_inheritance(rtx dst_reg,rtx invariant_rtx)5972 process_invariant_for_inheritance (rtx dst_reg, rtx invariant_rtx)
5973 {
5974   invariant_ptr_t invariant_ptr;
5975   rtx_insn *insn, *new_insns;
5976   rtx insn_set, insn_reg, new_reg;
5977   int insn_regno;
5978   bool succ_p = false;
5979   int dst_regno = REGNO (dst_reg);
5980   machine_mode dst_mode = GET_MODE (dst_reg);
5981   enum reg_class cl = lra_get_allocno_class (dst_regno), insn_reg_cl;
5982 
5983   invariant_ptr = insert_invariant (invariant_rtx);
5984   if ((insn = invariant_ptr->insn) != NULL_RTX)
5985     {
5986       /* We have a subsequent insn using the invariant.  */
5987       insn_set = single_set (insn);
5988       lra_assert (insn_set != NULL);
5989       insn_reg = SET_DEST (insn_set);
5990       lra_assert (REG_P (insn_reg));
5991       insn_regno = REGNO (insn_reg);
5992       insn_reg_cl = lra_get_allocno_class (insn_regno);
5993 
5994       if (dst_mode == GET_MODE (insn_reg)
5995 	  /* We should consider only result move reg insns which are
5996 	     cheap.  */
5997 	  && targetm.register_move_cost (dst_mode, cl, insn_reg_cl) == 2
5998 	  && targetm.register_move_cost (dst_mode, cl, cl) == 2)
5999 	{
6000 	  if (lra_dump_file != NULL)
6001 	    fprintf (lra_dump_file,
6002 		     "    [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[\n");
6003 	  new_reg = lra_create_new_reg (dst_mode, dst_reg,
6004 					cl, "invariant inheritance");
6005 	  bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
6006 	  bitmap_set_bit (&check_only_regs, REGNO (new_reg));
6007 	  lra_reg_info[REGNO (new_reg)].restore_rtx = PATTERN (insn);
6008 	  start_sequence ();
6009 	  lra_emit_move (new_reg, dst_reg);
6010 	  new_insns = get_insns ();
6011 	  end_sequence ();
6012 	  lra_process_new_insns (curr_insn, NULL, new_insns,
6013 				 "Add invariant inheritance<-original");
6014 	  start_sequence ();
6015 	  lra_emit_move (SET_DEST (insn_set), new_reg);
6016 	  new_insns = get_insns ();
6017 	  end_sequence ();
6018 	  lra_process_new_insns (insn, NULL, new_insns,
6019 				 "Changing reload<-inheritance");
6020 	  lra_set_insn_deleted (insn);
6021 	  succ_p = true;
6022 	  if (lra_dump_file != NULL)
6023 	    {
6024 	      fprintf (lra_dump_file,
6025 		       "    Invariant inheritance reuse change %d (bb%d):\n",
6026 		       REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
6027 	      dump_insn_slim (lra_dump_file, insn);
6028 	      fprintf (lra_dump_file,
6029 		       "	  ]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]\n");
6030 	    }
6031 	}
6032     }
6033   invariant_ptr->insn = curr_insn;
6034   return succ_p;
6035 }
6036 
6037 /* Check only registers living at the current program point in the
6038    current EBB.	 */
6039 static bitmap_head live_regs;
6040 
6041 /* Update live info in EBB given by its HEAD and TAIL insns after
6042    inheritance/split transformation.  The function removes dead moves
6043    too.	 */
6044 static void
update_ebb_live_info(rtx_insn * head,rtx_insn * tail)6045 update_ebb_live_info (rtx_insn *head, rtx_insn *tail)
6046 {
6047   unsigned int j;
6048   int i, regno;
6049   bool live_p;
6050   rtx_insn *prev_insn;
6051   rtx set;
6052   bool remove_p;
6053   basic_block last_bb, prev_bb, curr_bb;
6054   bitmap_iterator bi;
6055   struct lra_insn_reg *reg;
6056   edge e;
6057   edge_iterator ei;
6058 
6059   last_bb = BLOCK_FOR_INSN (tail);
6060   prev_bb = NULL;
6061   for (curr_insn = tail;
6062        curr_insn != PREV_INSN (head);
6063        curr_insn = prev_insn)
6064     {
6065       prev_insn = PREV_INSN (curr_insn);
6066       /* We need to process empty blocks too.  They contain
6067 	 NOTE_INSN_BASIC_BLOCK referring for the basic block.  */
6068       if (NOTE_P (curr_insn) && NOTE_KIND (curr_insn) != NOTE_INSN_BASIC_BLOCK)
6069 	continue;
6070       curr_bb = BLOCK_FOR_INSN (curr_insn);
6071       if (curr_bb != prev_bb)
6072 	{
6073 	  if (prev_bb != NULL)
6074 	    {
6075 	      /* Update df_get_live_in (prev_bb):  */
6076 	      EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
6077 		if (bitmap_bit_p (&live_regs, j))
6078 		  bitmap_set_bit (df_get_live_in (prev_bb), j);
6079 		else
6080 		  bitmap_clear_bit (df_get_live_in (prev_bb), j);
6081 	    }
6082 	  if (curr_bb != last_bb)
6083 	    {
6084 	      /* Update df_get_live_out (curr_bb):  */
6085 	      EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
6086 		{
6087 		  live_p = bitmap_bit_p (&live_regs, j);
6088 		  if (! live_p)
6089 		    FOR_EACH_EDGE (e, ei, curr_bb->succs)
6090 		      if (bitmap_bit_p (df_get_live_in (e->dest), j))
6091 			{
6092 			  live_p = true;
6093 			  break;
6094 			}
6095 		  if (live_p)
6096 		    bitmap_set_bit (df_get_live_out (curr_bb), j);
6097 		  else
6098 		    bitmap_clear_bit (df_get_live_out (curr_bb), j);
6099 		}
6100 	    }
6101 	  prev_bb = curr_bb;
6102 	  bitmap_and (&live_regs, &check_only_regs, df_get_live_out (curr_bb));
6103 	}
6104       if (! NONDEBUG_INSN_P (curr_insn))
6105 	continue;
6106       curr_id = lra_get_insn_recog_data (curr_insn);
6107       curr_static_id = curr_id->insn_static_data;
6108       remove_p = false;
6109       if ((set = single_set (curr_insn)) != NULL_RTX
6110 	  && REG_P (SET_DEST (set))
6111 	  && (regno = REGNO (SET_DEST (set))) >= FIRST_PSEUDO_REGISTER
6112 	  && SET_DEST (set) != pic_offset_table_rtx
6113 	  && bitmap_bit_p (&check_only_regs, regno)
6114 	  && ! bitmap_bit_p (&live_regs, regno))
6115 	remove_p = true;
6116       /* See which defined values die here.  */
6117       for (reg = curr_id->regs; reg != NULL; reg = reg->next)
6118 	if (reg->type == OP_OUT && ! reg->subreg_p)
6119 	  bitmap_clear_bit (&live_regs, reg->regno);
6120       for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
6121 	if (reg->type == OP_OUT && ! reg->subreg_p)
6122 	  bitmap_clear_bit (&live_regs, reg->regno);
6123       if (curr_id->arg_hard_regs != NULL)
6124 	/* Make clobbered argument hard registers die.  */
6125 	for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
6126 	  if (regno >= FIRST_PSEUDO_REGISTER)
6127 	    bitmap_clear_bit (&live_regs, regno - FIRST_PSEUDO_REGISTER);
6128       /* Mark each used value as live.  */
6129       for (reg = curr_id->regs; reg != NULL; reg = reg->next)
6130 	if (reg->type != OP_OUT
6131 	    && bitmap_bit_p (&check_only_regs, reg->regno))
6132 	  bitmap_set_bit (&live_regs, reg->regno);
6133       for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
6134 	if (reg->type != OP_OUT
6135 	    && bitmap_bit_p (&check_only_regs, reg->regno))
6136 	  bitmap_set_bit (&live_regs, reg->regno);
6137       if (curr_id->arg_hard_regs != NULL)
6138 	/* Make used argument hard registers live.  */
6139 	for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
6140 	  if (regno < FIRST_PSEUDO_REGISTER
6141 	      && bitmap_bit_p (&check_only_regs, regno))
6142 	    bitmap_set_bit (&live_regs, regno);
6143       /* It is quite important to remove dead move insns because it
6144 	 means removing dead store.  We don't need to process them for
6145 	 constraints.  */
6146       if (remove_p)
6147 	{
6148 	  if (lra_dump_file != NULL)
6149 	    {
6150 	      fprintf (lra_dump_file, "	    Removing dead insn:\n ");
6151 	      dump_insn_slim (lra_dump_file, curr_insn);
6152 	    }
6153 	  lra_set_insn_deleted (curr_insn);
6154 	}
6155     }
6156 }
6157 
6158 /* The structure describes info to do an inheritance for the current
6159    insn.  We need to collect such info first before doing the
6160    transformations because the transformations change the insn
6161    internal representation.  */
6162 struct to_inherit
6163 {
6164   /* Original regno.  */
6165   int regno;
6166   /* Subsequent insns which can inherit original reg value.  */
6167   rtx insns;
6168 };
6169 
6170 /* Array containing all info for doing inheritance from the current
6171    insn.  */
6172 static struct to_inherit to_inherit[LRA_MAX_INSN_RELOADS];
6173 
6174 /* Number elements in the previous array.  */
6175 static int to_inherit_num;
6176 
6177 /* Add inheritance info REGNO and INSNS. Their meaning is described in
6178    structure to_inherit.  */
6179 static void
add_to_inherit(int regno,rtx insns)6180 add_to_inherit (int regno, rtx insns)
6181 {
6182   int i;
6183 
6184   for (i = 0; i < to_inherit_num; i++)
6185     if (to_inherit[i].regno == regno)
6186       return;
6187   lra_assert (to_inherit_num < LRA_MAX_INSN_RELOADS);
6188   to_inherit[to_inherit_num].regno = regno;
6189   to_inherit[to_inherit_num++].insns = insns;
6190 }
6191 
6192 /* Return the last non-debug insn in basic block BB, or the block begin
6193    note if none.  */
6194 static rtx_insn *
get_last_insertion_point(basic_block bb)6195 get_last_insertion_point (basic_block bb)
6196 {
6197   rtx_insn *insn;
6198 
6199   FOR_BB_INSNS_REVERSE (bb, insn)
6200     if (NONDEBUG_INSN_P (insn) || NOTE_INSN_BASIC_BLOCK_P (insn))
6201       return insn;
6202   gcc_unreachable ();
6203 }
6204 
6205 /* Set up RES by registers living on edges FROM except the edge (FROM,
6206    TO) or by registers set up in a jump insn in BB FROM.  */
6207 static void
get_live_on_other_edges(basic_block from,basic_block to,bitmap res)6208 get_live_on_other_edges (basic_block from, basic_block to, bitmap res)
6209 {
6210   rtx_insn *last;
6211   struct lra_insn_reg *reg;
6212   edge e;
6213   edge_iterator ei;
6214 
6215   lra_assert (to != NULL);
6216   bitmap_clear (res);
6217   FOR_EACH_EDGE (e, ei, from->succs)
6218     if (e->dest != to)
6219       bitmap_ior_into (res, df_get_live_in (e->dest));
6220   last = get_last_insertion_point (from);
6221   if (! JUMP_P (last))
6222     return;
6223   curr_id = lra_get_insn_recog_data (last);
6224   for (reg = curr_id->regs; reg != NULL; reg = reg->next)
6225     if (reg->type != OP_IN)
6226       bitmap_set_bit (res, reg->regno);
6227 }
6228 
6229 /* Used as a temporary results of some bitmap calculations.  */
6230 static bitmap_head temp_bitmap;
6231 
6232 /* We split for reloads of small class of hard regs.  The following
6233    defines how many hard regs the class should have to be qualified as
6234    small.  The code is mostly oriented to x86/x86-64 architecture
6235    where some insns need to use only specific register or pair of
6236    registers and these register can live in RTL explicitly, e.g. for
6237    parameter passing.  */
6238 static const int max_small_class_regs_num = 2;
6239 
6240 /* Do inheritance/split transformations in EBB starting with HEAD and
6241    finishing on TAIL.  We process EBB insns in the reverse order.
6242    Return true if we did any inheritance/split transformation in the
6243    EBB.
6244 
6245    We should avoid excessive splitting which results in worse code
6246    because of inaccurate cost calculations for spilling new split
6247    pseudos in such case.  To achieve this we do splitting only if
6248    register pressure is high in given basic block and there are reload
6249    pseudos requiring hard registers.  We could do more register
6250    pressure calculations at any given program point to avoid necessary
6251    splitting even more but it is to expensive and the current approach
6252    works well enough.  */
6253 static bool
inherit_in_ebb(rtx_insn * head,rtx_insn * tail)6254 inherit_in_ebb (rtx_insn *head, rtx_insn *tail)
6255 {
6256   int i, src_regno, dst_regno, nregs;
6257   bool change_p, succ_p, update_reloads_num_p;
6258   rtx_insn *prev_insn, *last_insn;
6259   rtx next_usage_insns, curr_set;
6260   enum reg_class cl;
6261   struct lra_insn_reg *reg;
6262   basic_block last_processed_bb, curr_bb = NULL;
6263   HARD_REG_SET potential_reload_hard_regs, live_hard_regs;
6264   bitmap to_process;
6265   unsigned int j;
6266   bitmap_iterator bi;
6267   bool head_p, after_p;
6268 
6269   change_p = false;
6270   curr_usage_insns_check++;
6271   clear_invariants ();
6272   reloads_num = calls_num = 0;
6273   bitmap_clear (&check_only_regs);
6274   bitmap_clear (&invalid_invariant_regs);
6275   last_processed_bb = NULL;
6276   CLEAR_HARD_REG_SET (potential_reload_hard_regs);
6277   COPY_HARD_REG_SET (live_hard_regs, eliminable_regset);
6278   IOR_HARD_REG_SET (live_hard_regs, lra_no_alloc_regs);
6279   /* We don't process new insns generated in the loop.	*/
6280   for (curr_insn = tail; curr_insn != PREV_INSN (head); curr_insn = prev_insn)
6281     {
6282       prev_insn = PREV_INSN (curr_insn);
6283       if (BLOCK_FOR_INSN (curr_insn) != NULL)
6284 	curr_bb = BLOCK_FOR_INSN (curr_insn);
6285       if (last_processed_bb != curr_bb)
6286 	{
6287 	  /* We are at the end of BB.  Add qualified living
6288 	     pseudos for potential splitting.  */
6289 	  to_process = df_get_live_out (curr_bb);
6290 	  if (last_processed_bb != NULL)
6291 	    {
6292 	      /* We are somewhere in the middle of EBB.	 */
6293 	      get_live_on_other_edges (curr_bb, last_processed_bb,
6294 				       &temp_bitmap);
6295 	      to_process = &temp_bitmap;
6296 	    }
6297 	  last_processed_bb = curr_bb;
6298 	  last_insn = get_last_insertion_point (curr_bb);
6299 	  after_p = (! JUMP_P (last_insn)
6300 		     && (! CALL_P (last_insn)
6301 			 || (find_reg_note (last_insn,
6302 					   REG_NORETURN, NULL_RTX) == NULL_RTX
6303 			     && ! SIBLING_CALL_P (last_insn))));
6304 	  CLEAR_HARD_REG_SET (potential_reload_hard_regs);
6305 	  EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
6306 	    {
6307 	      if ((int) j >= lra_constraint_new_regno_start)
6308 		break;
6309 	      if (j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
6310 		{
6311 		  if (j < FIRST_PSEUDO_REGISTER)
6312 		    SET_HARD_REG_BIT (live_hard_regs, j);
6313 		  else
6314 		    add_to_hard_reg_set (&live_hard_regs,
6315 					 PSEUDO_REGNO_MODE (j),
6316 					 reg_renumber[j]);
6317 		  setup_next_usage_insn (j, last_insn, reloads_num, after_p);
6318 		}
6319 	    }
6320 	}
6321       src_regno = dst_regno = -1;
6322       curr_set = single_set (curr_insn);
6323       if (curr_set != NULL_RTX && REG_P (SET_DEST (curr_set)))
6324 	dst_regno = REGNO (SET_DEST (curr_set));
6325       if (curr_set != NULL_RTX && REG_P (SET_SRC (curr_set)))
6326 	src_regno = REGNO (SET_SRC (curr_set));
6327       update_reloads_num_p = true;
6328       if (src_regno < lra_constraint_new_regno_start
6329 	  && src_regno >= FIRST_PSEUDO_REGISTER
6330 	  && reg_renumber[src_regno] < 0
6331 	  && dst_regno >= lra_constraint_new_regno_start
6332 	  && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS)
6333 	{
6334 	  /* 'reload_pseudo <- original_pseudo'.  */
6335 	  if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
6336 	    reloads_num++;
6337 	  update_reloads_num_p = false;
6338 	  succ_p = false;
6339 	  if (usage_insns[src_regno].check == curr_usage_insns_check
6340 	      && (next_usage_insns = usage_insns[src_regno].insns) != NULL_RTX)
6341 	    succ_p = inherit_reload_reg (false, src_regno, cl,
6342 					 curr_insn, next_usage_insns);
6343 	  if (succ_p)
6344 	    change_p = true;
6345 	  else
6346 	    setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
6347 	  if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
6348 	    IOR_HARD_REG_SET (potential_reload_hard_regs,
6349 			      reg_class_contents[cl]);
6350 	}
6351       else if (src_regno < 0
6352 	       && dst_regno >= lra_constraint_new_regno_start
6353 	       && invariant_p (SET_SRC (curr_set))
6354 	       && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS
6355 	       && ! bitmap_bit_p (&invalid_invariant_regs, dst_regno)
6356 	       && ! bitmap_bit_p (&invalid_invariant_regs,
6357 				  ORIGINAL_REGNO(regno_reg_rtx[dst_regno])))
6358 	{
6359 	  /* 'reload_pseudo <- invariant'.  */
6360 	  if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
6361 	    reloads_num++;
6362 	  update_reloads_num_p = false;
6363 	  if (process_invariant_for_inheritance (SET_DEST (curr_set), SET_SRC (curr_set)))
6364 	    change_p = true;
6365 	  if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
6366 	    IOR_HARD_REG_SET (potential_reload_hard_regs,
6367 			      reg_class_contents[cl]);
6368 	}
6369       else if (src_regno >= lra_constraint_new_regno_start
6370 	       && dst_regno < lra_constraint_new_regno_start
6371 	       && dst_regno >= FIRST_PSEUDO_REGISTER
6372 	       && reg_renumber[dst_regno] < 0
6373 	       && (cl = lra_get_allocno_class (src_regno)) != NO_REGS
6374 	       && usage_insns[dst_regno].check == curr_usage_insns_check
6375 	       && (next_usage_insns
6376 		   = usage_insns[dst_regno].insns) != NULL_RTX)
6377 	{
6378 	  if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
6379 	    reloads_num++;
6380 	  update_reloads_num_p = false;
6381 	  /* 'original_pseudo <- reload_pseudo'.  */
6382 	  if (! JUMP_P (curr_insn)
6383 	      && inherit_reload_reg (true, dst_regno, cl,
6384 				     curr_insn, next_usage_insns))
6385 	    change_p = true;
6386 	  /* Invalidate.  */
6387 	  usage_insns[dst_regno].check = 0;
6388 	  if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
6389 	    IOR_HARD_REG_SET (potential_reload_hard_regs,
6390 			      reg_class_contents[cl]);
6391 	}
6392       else if (INSN_P (curr_insn))
6393 	{
6394 	  int iter;
6395 	  int max_uid = get_max_uid ();
6396 
6397 	  curr_id = lra_get_insn_recog_data (curr_insn);
6398 	  curr_static_id = curr_id->insn_static_data;
6399 	  to_inherit_num = 0;
6400 	  /* Process insn definitions.	*/
6401 	  for (iter = 0; iter < 2; iter++)
6402 	    for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
6403 		 reg != NULL;
6404 		 reg = reg->next)
6405 	      if (reg->type != OP_IN
6406 		  && (dst_regno = reg->regno) < lra_constraint_new_regno_start)
6407 		{
6408 		  if (dst_regno >= FIRST_PSEUDO_REGISTER && reg->type == OP_OUT
6409 		      && reg_renumber[dst_regno] < 0 && ! reg->subreg_p
6410 		      && usage_insns[dst_regno].check == curr_usage_insns_check
6411 		      && (next_usage_insns
6412 			  = usage_insns[dst_regno].insns) != NULL_RTX)
6413 		    {
6414 		      struct lra_insn_reg *r;
6415 
6416 		      for (r = curr_id->regs; r != NULL; r = r->next)
6417 			if (r->type != OP_OUT && r->regno == dst_regno)
6418 			  break;
6419 		      /* Don't do inheritance if the pseudo is also
6420 			 used in the insn.  */
6421 		      if (r == NULL)
6422 			/* We cannot do inheritance right now
6423 			   because the current insn reg info (chain
6424 			   regs) can change after that.  */
6425 			add_to_inherit (dst_regno, next_usage_insns);
6426 		    }
6427 		  /* We cannot process one reg twice here because of
6428 		     usage_insns invalidation.  */
6429 		  if ((dst_regno < FIRST_PSEUDO_REGISTER
6430 		       || reg_renumber[dst_regno] >= 0)
6431 		      && ! reg->subreg_p && reg->type != OP_IN)
6432 		    {
6433 		      HARD_REG_SET s;
6434 
6435 		      if (split_if_necessary (dst_regno, reg->biggest_mode,
6436 					      potential_reload_hard_regs,
6437 					      false, curr_insn, max_uid))
6438 			change_p = true;
6439 		      CLEAR_HARD_REG_SET (s);
6440 		      if (dst_regno < FIRST_PSEUDO_REGISTER)
6441 			add_to_hard_reg_set (&s, reg->biggest_mode, dst_regno);
6442 		      else
6443 			add_to_hard_reg_set (&s, PSEUDO_REGNO_MODE (dst_regno),
6444 					     reg_renumber[dst_regno]);
6445 		      AND_COMPL_HARD_REG_SET (live_hard_regs, s);
6446 		      AND_COMPL_HARD_REG_SET (potential_reload_hard_regs, s);
6447 		    }
6448 		  /* We should invalidate potential inheritance or
6449 		     splitting for the current insn usages to the next
6450 		     usage insns (see code below) as the output pseudo
6451 		     prevents this.  */
6452 		  if ((dst_regno >= FIRST_PSEUDO_REGISTER
6453 		       && reg_renumber[dst_regno] < 0)
6454 		      || (reg->type == OP_OUT && ! reg->subreg_p
6455 			  && (dst_regno < FIRST_PSEUDO_REGISTER
6456 			      || reg_renumber[dst_regno] >= 0)))
6457 		    {
6458 		      /* Invalidate and mark definitions.  */
6459 		      if (dst_regno >= FIRST_PSEUDO_REGISTER)
6460 			usage_insns[dst_regno].check = -(int) INSN_UID (curr_insn);
6461 		      else
6462 			{
6463 			  nregs = hard_regno_nregs (dst_regno,
6464 						    reg->biggest_mode);
6465 			  for (i = 0; i < nregs; i++)
6466 			    usage_insns[dst_regno + i].check
6467 			      = -(int) INSN_UID (curr_insn);
6468 			}
6469 		    }
6470 		}
6471 	  /* Process clobbered call regs.  */
6472 	  if (curr_id->arg_hard_regs != NULL)
6473 	    for (i = 0; (dst_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
6474 	      if (dst_regno >= FIRST_PSEUDO_REGISTER)
6475 		usage_insns[dst_regno - FIRST_PSEUDO_REGISTER].check
6476 		  = -(int) INSN_UID (curr_insn);
6477 	  if (! JUMP_P (curr_insn))
6478 	    for (i = 0; i < to_inherit_num; i++)
6479 	      if (inherit_reload_reg (true, to_inherit[i].regno,
6480 				      ALL_REGS, curr_insn,
6481 				      to_inherit[i].insns))
6482 	      change_p = true;
6483 	  if (CALL_P (curr_insn))
6484 	    {
6485 	      rtx cheap, pat, dest;
6486 	      rtx_insn *restore;
6487 	      int regno, hard_regno;
6488 
6489 	      calls_num++;
6490 	      if ((cheap = find_reg_note (curr_insn,
6491 					  REG_RETURNED, NULL_RTX)) != NULL_RTX
6492 		  && ((cheap = XEXP (cheap, 0)), true)
6493 		  && (regno = REGNO (cheap)) >= FIRST_PSEUDO_REGISTER
6494 		  && (hard_regno = reg_renumber[regno]) >= 0
6495 		  && usage_insns[regno].check == curr_usage_insns_check
6496 		  /* If there are pending saves/restores, the
6497 		     optimization is not worth.	 */
6498 		  && usage_insns[regno].calls_num == calls_num - 1
6499 		  && TEST_HARD_REG_BIT (call_used_reg_set, hard_regno))
6500 		{
6501 		  /* Restore the pseudo from the call result as
6502 		     REG_RETURNED note says that the pseudo value is
6503 		     in the call result and the pseudo is an argument
6504 		     of the call.  */
6505 		  pat = PATTERN (curr_insn);
6506 		  if (GET_CODE (pat) == PARALLEL)
6507 		    pat = XVECEXP (pat, 0, 0);
6508 		  dest = SET_DEST (pat);
6509 		  /* For multiple return values dest is PARALLEL.
6510 		     Currently we handle only single return value case.  */
6511 		  if (REG_P (dest))
6512 		    {
6513 		      start_sequence ();
6514 		      emit_move_insn (cheap, copy_rtx (dest));
6515 		      restore = get_insns ();
6516 		      end_sequence ();
6517 		      lra_process_new_insns (curr_insn, NULL, restore,
6518 					     "Inserting call parameter restore");
6519 		      /* We don't need to save/restore of the pseudo from
6520 			 this call.	 */
6521 		      usage_insns[regno].calls_num = calls_num;
6522 		      bitmap_set_bit (&check_only_regs, regno);
6523 		    }
6524 		}
6525 	    }
6526 	  to_inherit_num = 0;
6527 	  /* Process insn usages.  */
6528 	  for (iter = 0; iter < 2; iter++)
6529 	    for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
6530 		 reg != NULL;
6531 		 reg = reg->next)
6532 	      if ((reg->type != OP_OUT
6533 		   || (reg->type == OP_OUT && reg->subreg_p))
6534 		  && (src_regno = reg->regno) < lra_constraint_new_regno_start)
6535 		{
6536 		  if (src_regno >= FIRST_PSEUDO_REGISTER
6537 		      && reg_renumber[src_regno] < 0 && reg->type == OP_IN)
6538 		    {
6539 		      if (usage_insns[src_regno].check == curr_usage_insns_check
6540 			  && (next_usage_insns
6541 			      = usage_insns[src_regno].insns) != NULL_RTX
6542 			  && NONDEBUG_INSN_P (curr_insn))
6543 			add_to_inherit (src_regno, next_usage_insns);
6544 		      else if (usage_insns[src_regno].check
6545 			       != -(int) INSN_UID (curr_insn))
6546 			/* Add usages but only if the reg is not set up
6547 			   in the same insn.  */
6548 			add_next_usage_insn (src_regno, curr_insn, reloads_num);
6549 		    }
6550 		  else if (src_regno < FIRST_PSEUDO_REGISTER
6551 			   || reg_renumber[src_regno] >= 0)
6552 		    {
6553 		      bool before_p;
6554 		      rtx_insn *use_insn = curr_insn;
6555 
6556 		      before_p = (JUMP_P (curr_insn)
6557 				  || (CALL_P (curr_insn) && reg->type == OP_IN));
6558 		      if (NONDEBUG_INSN_P (curr_insn)
6559 			  && (! JUMP_P (curr_insn) || reg->type == OP_IN)
6560 			  && split_if_necessary (src_regno, reg->biggest_mode,
6561 						 potential_reload_hard_regs,
6562 						 before_p, curr_insn, max_uid))
6563 			{
6564 			  if (reg->subreg_p)
6565 			    lra_risky_transformations_p = true;
6566 			  change_p = true;
6567 			  /* Invalidate. */
6568 			  usage_insns[src_regno].check = 0;
6569 			  if (before_p)
6570 			    use_insn = PREV_INSN (curr_insn);
6571 			}
6572 		      if (NONDEBUG_INSN_P (curr_insn))
6573 			{
6574 			  if (src_regno < FIRST_PSEUDO_REGISTER)
6575 			    add_to_hard_reg_set (&live_hard_regs,
6576 						 reg->biggest_mode, src_regno);
6577 			  else
6578 			    add_to_hard_reg_set (&live_hard_regs,
6579 						 PSEUDO_REGNO_MODE (src_regno),
6580 						 reg_renumber[src_regno]);
6581 			}
6582 		      if (src_regno >= FIRST_PSEUDO_REGISTER)
6583 			add_next_usage_insn (src_regno, use_insn, reloads_num);
6584 		      else
6585 			{
6586 			  for (i = 0; i < hard_regno_nregs (src_regno, reg->biggest_mode); i++)
6587 			    add_next_usage_insn (src_regno + i, use_insn, reloads_num);
6588 			}
6589 		    }
6590 		}
6591 	  /* Process used call regs.  */
6592 	  if (curr_id->arg_hard_regs != NULL)
6593 	    for (i = 0; (src_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
6594 	      if (src_regno < FIRST_PSEUDO_REGISTER)
6595 		{
6596 	           SET_HARD_REG_BIT (live_hard_regs, src_regno);
6597 	           add_next_usage_insn (src_regno, curr_insn, reloads_num);
6598 		}
6599 	  for (i = 0; i < to_inherit_num; i++)
6600 	    {
6601 	      src_regno = to_inherit[i].regno;
6602 	      if (inherit_reload_reg (false, src_regno, ALL_REGS,
6603 				      curr_insn, to_inherit[i].insns))
6604 		change_p = true;
6605 	      else
6606 		setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
6607 	    }
6608 	}
6609       if (update_reloads_num_p
6610 	  && NONDEBUG_INSN_P (curr_insn) && curr_set != NULL_RTX)
6611 	{
6612 	  int regno = -1;
6613 	  if ((REG_P (SET_DEST (curr_set))
6614 	       && (regno = REGNO (SET_DEST (curr_set))) >= lra_constraint_new_regno_start
6615 	       && reg_renumber[regno] < 0
6616 	       && (cl = lra_get_allocno_class (regno)) != NO_REGS)
6617 	      || (REG_P (SET_SRC (curr_set))
6618 	          && (regno = REGNO (SET_SRC (curr_set))) >= lra_constraint_new_regno_start
6619 	          && reg_renumber[regno] < 0
6620 	          && (cl = lra_get_allocno_class (regno)) != NO_REGS))
6621 	    {
6622 	      if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
6623 		reloads_num++;
6624 	      if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
6625 		IOR_HARD_REG_SET (potential_reload_hard_regs,
6626 	                          reg_class_contents[cl]);
6627 	    }
6628 	}
6629       if (NONDEBUG_INSN_P (curr_insn))
6630 	{
6631 	  int regno;
6632 
6633 	  /* Invalidate invariants with changed regs.  */
6634 	  curr_id = lra_get_insn_recog_data (curr_insn);
6635 	  for (reg = curr_id->regs; reg != NULL; reg = reg->next)
6636 	    if (reg->type != OP_IN)
6637 	      {
6638 		bitmap_set_bit (&invalid_invariant_regs, reg->regno);
6639 		bitmap_set_bit (&invalid_invariant_regs,
6640 				ORIGINAL_REGNO (regno_reg_rtx[reg->regno]));
6641 	      }
6642 	  curr_static_id = curr_id->insn_static_data;
6643 	  for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
6644 	    if (reg->type != OP_IN)
6645 	      bitmap_set_bit (&invalid_invariant_regs, reg->regno);
6646 	  if (curr_id->arg_hard_regs != NULL)
6647 	    for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
6648 	      if (regno >= FIRST_PSEUDO_REGISTER)
6649 		bitmap_set_bit (&invalid_invariant_regs,
6650 				regno - FIRST_PSEUDO_REGISTER);
6651 	}
6652       /* We reached the start of the current basic block.  */
6653       if (prev_insn == NULL_RTX || prev_insn == PREV_INSN (head)
6654 	  || BLOCK_FOR_INSN (prev_insn) != curr_bb)
6655 	{
6656 	  /* We reached the beginning of the current block -- do
6657 	     rest of spliting in the current BB.  */
6658 	  to_process = df_get_live_in (curr_bb);
6659 	  if (BLOCK_FOR_INSN (head) != curr_bb)
6660 	    {
6661 	      /* We are somewhere in the middle of EBB.	 */
6662 	      get_live_on_other_edges (EDGE_PRED (curr_bb, 0)->src,
6663 				       curr_bb, &temp_bitmap);
6664 	      to_process = &temp_bitmap;
6665 	    }
6666 	  head_p = true;
6667 	  EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
6668 	    {
6669 	      if ((int) j >= lra_constraint_new_regno_start)
6670 		break;
6671 	      if (((int) j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
6672 		  && usage_insns[j].check == curr_usage_insns_check
6673 		  && (next_usage_insns = usage_insns[j].insns) != NULL_RTX)
6674 		{
6675 		  if (need_for_split_p (potential_reload_hard_regs, j))
6676 		    {
6677 		      if (lra_dump_file != NULL && head_p)
6678 			{
6679 			  fprintf (lra_dump_file,
6680 				   "  ----------------------------------\n");
6681 			  head_p = false;
6682 			}
6683 		      if (split_reg (false, j, bb_note (curr_bb),
6684 				     next_usage_insns, NULL))
6685 			change_p = true;
6686 		    }
6687 		  usage_insns[j].check = 0;
6688 		}
6689 	    }
6690 	}
6691     }
6692   return change_p;
6693 }
6694 
6695 /* This value affects EBB forming.  If probability of edge from EBB to
6696    a BB is not greater than the following value, we don't add the BB
6697    to EBB.  */
6698 #define EBB_PROBABILITY_CUTOFF \
6699   ((REG_BR_PROB_BASE * LRA_INHERITANCE_EBB_PROBABILITY_CUTOFF) / 100)
6700 
6701 /* Current number of inheritance/split iteration.  */
6702 int lra_inheritance_iter;
6703 
6704 /* Entry function for inheritance/split pass.  */
6705 void
lra_inheritance(void)6706 lra_inheritance (void)
6707 {
6708   int i;
6709   basic_block bb, start_bb;
6710   edge e;
6711 
6712   lra_inheritance_iter++;
6713   if (lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
6714     return;
6715   timevar_push (TV_LRA_INHERITANCE);
6716   if (lra_dump_file != NULL)
6717     fprintf (lra_dump_file, "\n********** Inheritance #%d: **********\n\n",
6718 	     lra_inheritance_iter);
6719   curr_usage_insns_check = 0;
6720   usage_insns = XNEWVEC (struct usage_insns, lra_constraint_new_regno_start);
6721   for (i = 0; i < lra_constraint_new_regno_start; i++)
6722     usage_insns[i].check = 0;
6723   bitmap_initialize (&check_only_regs, &reg_obstack);
6724   bitmap_initialize (&invalid_invariant_regs, &reg_obstack);
6725   bitmap_initialize (&live_regs, &reg_obstack);
6726   bitmap_initialize (&temp_bitmap, &reg_obstack);
6727   bitmap_initialize (&ebb_global_regs, &reg_obstack);
6728   FOR_EACH_BB_FN (bb, cfun)
6729     {
6730       start_bb = bb;
6731       if (lra_dump_file != NULL)
6732 	fprintf (lra_dump_file, "EBB");
6733       /* Form a EBB starting with BB.  */
6734       bitmap_clear (&ebb_global_regs);
6735       bitmap_ior_into (&ebb_global_regs, df_get_live_in (bb));
6736       for (;;)
6737 	{
6738 	  if (lra_dump_file != NULL)
6739 	    fprintf (lra_dump_file, " %d", bb->index);
6740 	  if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
6741 	      || LABEL_P (BB_HEAD (bb->next_bb)))
6742 	    break;
6743 	  e = find_fallthru_edge (bb->succs);
6744 	  if (! e)
6745 	    break;
6746 	  if (e->probability.initialized_p ()
6747 	      && e->probability.to_reg_br_prob_base () < EBB_PROBABILITY_CUTOFF)
6748 	    break;
6749 	  bb = bb->next_bb;
6750 	}
6751       bitmap_ior_into (&ebb_global_regs, df_get_live_out (bb));
6752       if (lra_dump_file != NULL)
6753 	fprintf (lra_dump_file, "\n");
6754       if (inherit_in_ebb (BB_HEAD (start_bb), BB_END (bb)))
6755 	/* Remember that the EBB head and tail can change in
6756 	   inherit_in_ebb.  */
6757 	update_ebb_live_info (BB_HEAD (start_bb), BB_END (bb));
6758     }
6759   bitmap_release (&ebb_global_regs);
6760   bitmap_release (&temp_bitmap);
6761   bitmap_release (&live_regs);
6762   bitmap_release (&invalid_invariant_regs);
6763   bitmap_release (&check_only_regs);
6764   free (usage_insns);
6765 
6766   timevar_pop (TV_LRA_INHERITANCE);
6767 }
6768 
6769 
6770 
6771 /* This page contains code to undo failed inheritance/split
6772    transformations.  */
6773 
6774 /* Current number of iteration undoing inheritance/split.  */
6775 int lra_undo_inheritance_iter;
6776 
6777 /* Fix BB live info LIVE after removing pseudos created on pass doing
6778    inheritance/split which are REMOVED_PSEUDOS.	 */
6779 static void
fix_bb_live_info(bitmap live,bitmap removed_pseudos)6780 fix_bb_live_info (bitmap live, bitmap removed_pseudos)
6781 {
6782   unsigned int regno;
6783   bitmap_iterator bi;
6784 
6785   EXECUTE_IF_SET_IN_BITMAP (removed_pseudos, 0, regno, bi)
6786     if (bitmap_clear_bit (live, regno)
6787 	&& REG_P (lra_reg_info[regno].restore_rtx))
6788       bitmap_set_bit (live, REGNO (lra_reg_info[regno].restore_rtx));
6789 }
6790 
6791 /* Return regno of the (subreg of) REG. Otherwise, return a negative
6792    number.  */
6793 static int
get_regno(rtx reg)6794 get_regno (rtx reg)
6795 {
6796   if (GET_CODE (reg) == SUBREG)
6797     reg = SUBREG_REG (reg);
6798   if (REG_P (reg))
6799     return REGNO (reg);
6800   return -1;
6801 }
6802 
6803 /* Delete a move INSN with destination reg DREGNO and a previous
6804    clobber insn with the same regno.  The inheritance/split code can
6805    generate moves with preceding clobber and when we delete such moves
6806    we should delete the clobber insn too to keep the correct life
6807    info.  */
6808 static void
delete_move_and_clobber(rtx_insn * insn,int dregno)6809 delete_move_and_clobber (rtx_insn *insn, int dregno)
6810 {
6811   rtx_insn *prev_insn = PREV_INSN (insn);
6812 
6813   lra_set_insn_deleted (insn);
6814   lra_assert (dregno >= 0);
6815   if (prev_insn != NULL && NONDEBUG_INSN_P (prev_insn)
6816       && GET_CODE (PATTERN (prev_insn)) == CLOBBER
6817       && dregno == get_regno (XEXP (PATTERN (prev_insn), 0)))
6818     lra_set_insn_deleted (prev_insn);
6819 }
6820 
6821 /* Remove inheritance/split pseudos which are in REMOVE_PSEUDOS and
6822    return true if we did any change.  The undo transformations for
6823    inheritance looks like
6824       i <- i2
6825       p <- i	  =>   p <- i2
6826    or removing
6827       p <- i, i <- p, and i <- i3
6828    where p is original pseudo from which inheritance pseudo i was
6829    created, i and i3 are removed inheritance pseudos, i2 is another
6830    not removed inheritance pseudo.  All split pseudos or other
6831    occurrences of removed inheritance pseudos are changed on the
6832    corresponding original pseudos.
6833 
6834    The function also schedules insns changed and created during
6835    inheritance/split pass for processing by the subsequent constraint
6836    pass.  */
6837 static bool
remove_inheritance_pseudos(bitmap remove_pseudos)6838 remove_inheritance_pseudos (bitmap remove_pseudos)
6839 {
6840   basic_block bb;
6841   int regno, sregno, prev_sregno, dregno;
6842   rtx restore_rtx;
6843   rtx set, prev_set;
6844   rtx_insn *prev_insn;
6845   bool change_p, done_p;
6846 
6847   change_p = ! bitmap_empty_p (remove_pseudos);
6848   /* We cannot finish the function right away if CHANGE_P is true
6849      because we need to marks insns affected by previous
6850      inheritance/split pass for processing by the subsequent
6851      constraint pass.  */
6852   FOR_EACH_BB_FN (bb, cfun)
6853     {
6854       fix_bb_live_info (df_get_live_in (bb), remove_pseudos);
6855       fix_bb_live_info (df_get_live_out (bb), remove_pseudos);
6856       FOR_BB_INSNS_REVERSE (bb, curr_insn)
6857 	{
6858 	  if (! INSN_P (curr_insn))
6859 	    continue;
6860 	  done_p = false;
6861 	  sregno = dregno = -1;
6862 	  if (change_p && NONDEBUG_INSN_P (curr_insn)
6863 	      && (set = single_set (curr_insn)) != NULL_RTX)
6864 	    {
6865 	      dregno = get_regno (SET_DEST (set));
6866 	      sregno = get_regno (SET_SRC (set));
6867 	    }
6868 
6869 	  if (sregno >= 0 && dregno >= 0)
6870 	    {
6871 	      if (bitmap_bit_p (remove_pseudos, dregno)
6872 		  && ! REG_P (lra_reg_info[dregno].restore_rtx))
6873 		{
6874 		  /* invariant inheritance pseudo <- original pseudo */
6875 		  if (lra_dump_file != NULL)
6876 		    {
6877 		      fprintf (lra_dump_file, "	   Removing invariant inheritance:\n");
6878 		      dump_insn_slim (lra_dump_file, curr_insn);
6879 		      fprintf (lra_dump_file, "\n");
6880 		    }
6881 		  delete_move_and_clobber (curr_insn, dregno);
6882 		  done_p = true;
6883 		}
6884 	      else if (bitmap_bit_p (remove_pseudos, sregno)
6885 		       && ! REG_P (lra_reg_info[sregno].restore_rtx))
6886 		{
6887 		  /* reload pseudo <- invariant inheritance pseudo */
6888 		  start_sequence ();
6889 		  /* We cannot just change the source.  It might be
6890 		     an insn different from the move.  */
6891 		  emit_insn (lra_reg_info[sregno].restore_rtx);
6892 		  rtx_insn *new_insns = get_insns ();
6893 		  end_sequence ();
6894 		  lra_assert (single_set (new_insns) != NULL
6895 			      && SET_DEST (set) == SET_DEST (single_set (new_insns)));
6896 		  lra_process_new_insns (curr_insn, NULL, new_insns,
6897 					 "Changing reload<-invariant inheritance");
6898 		  delete_move_and_clobber (curr_insn, dregno);
6899 		  done_p = true;
6900 		}
6901 	      else if ((bitmap_bit_p (remove_pseudos, sregno)
6902 			&& (get_regno (lra_reg_info[sregno].restore_rtx) == dregno
6903 			    || (bitmap_bit_p (remove_pseudos, dregno)
6904 				&& get_regno (lra_reg_info[sregno].restore_rtx) >= 0
6905 				&& (get_regno (lra_reg_info[sregno].restore_rtx)
6906 				    == get_regno (lra_reg_info[dregno].restore_rtx)))))
6907 		       || (bitmap_bit_p (remove_pseudos, dregno)
6908 			   && get_regno (lra_reg_info[dregno].restore_rtx) == sregno))
6909 		/* One of the following cases:
6910 		     original <- removed inheritance pseudo
6911 		     removed inherit pseudo <- another removed inherit pseudo
6912 		     removed inherit pseudo <- original pseudo
6913 		   Or
6914 		     removed_split_pseudo <- original_reg
6915 		     original_reg <- removed_split_pseudo */
6916 		{
6917 		  if (lra_dump_file != NULL)
6918 		    {
6919 		      fprintf (lra_dump_file, "	   Removing %s:\n",
6920 			       bitmap_bit_p (&lra_split_regs, sregno)
6921 			       || bitmap_bit_p (&lra_split_regs, dregno)
6922 			       ? "split" : "inheritance");
6923 		      dump_insn_slim (lra_dump_file, curr_insn);
6924 		    }
6925 		  delete_move_and_clobber (curr_insn, dregno);
6926 		  done_p = true;
6927 		}
6928 	      else if (bitmap_bit_p (remove_pseudos, sregno)
6929 		       && bitmap_bit_p (&lra_inheritance_pseudos, sregno))
6930 		{
6931 		  /* Search the following pattern:
6932 		       inherit_or_split_pseudo1 <- inherit_or_split_pseudo2
6933 		       original_pseudo <- inherit_or_split_pseudo1
6934 		    where the 2nd insn is the current insn and
6935 		    inherit_or_split_pseudo2 is not removed.  If it is found,
6936 		    change the current insn onto:
6937 		       original_pseudo <- inherit_or_split_pseudo2.  */
6938 		  for (prev_insn = PREV_INSN (curr_insn);
6939 		       prev_insn != NULL_RTX && ! NONDEBUG_INSN_P (prev_insn);
6940 		       prev_insn = PREV_INSN (prev_insn))
6941 		    ;
6942 		  if (prev_insn != NULL_RTX && BLOCK_FOR_INSN (prev_insn) == bb
6943 		      && (prev_set = single_set (prev_insn)) != NULL_RTX
6944 		      /* There should be no subregs in insn we are
6945 			 searching because only the original reg might
6946 			 be in subreg when we changed the mode of
6947 			 load/store for splitting.  */
6948 		      && REG_P (SET_DEST (prev_set))
6949 		      && REG_P (SET_SRC (prev_set))
6950 		      && (int) REGNO (SET_DEST (prev_set)) == sregno
6951 		      && ((prev_sregno = REGNO (SET_SRC (prev_set)))
6952 			  >= FIRST_PSEUDO_REGISTER)
6953 		      && (lra_reg_info[prev_sregno].restore_rtx == NULL_RTX
6954 			  ||
6955 			  /* As we consider chain of inheritance or
6956 			     splitting described in above comment we should
6957 			     check that sregno and prev_sregno were
6958 			     inheritance/split pseudos created from the
6959 			     same original regno.  */
6960 			  (get_regno (lra_reg_info[sregno].restore_rtx) >= 0
6961 			   && (get_regno (lra_reg_info[sregno].restore_rtx)
6962 			       == get_regno (lra_reg_info[prev_sregno].restore_rtx))))
6963 		      && ! bitmap_bit_p (remove_pseudos, prev_sregno))
6964 		    {
6965 		      lra_assert (GET_MODE (SET_SRC (prev_set))
6966 				  == GET_MODE (regno_reg_rtx[sregno]));
6967 		      /* Although we have a single set, the insn can
6968 			 contain more one sregno register occurrence
6969 			 as a source.  Change all occurrences.  */
6970 		      lra_substitute_pseudo_within_insn (curr_insn, sregno,
6971 							 SET_SRC (prev_set),
6972 							 false);
6973 		      /* As we are finishing with processing the insn
6974 			 here, check the destination too as it might
6975 			 inheritance pseudo for another pseudo.  */
6976 		      if (bitmap_bit_p (remove_pseudos, dregno)
6977 			  && bitmap_bit_p (&lra_inheritance_pseudos, dregno)
6978 			  && (restore_rtx
6979 			      = lra_reg_info[dregno].restore_rtx) != NULL_RTX)
6980 			{
6981 			  if (GET_CODE (SET_DEST (set)) == SUBREG)
6982 			    SUBREG_REG (SET_DEST (set)) = restore_rtx;
6983 			  else
6984 			    SET_DEST (set) = restore_rtx;
6985 			}
6986 		      lra_push_insn_and_update_insn_regno_info (curr_insn);
6987 		      lra_set_used_insn_alternative_by_uid
6988 			(INSN_UID (curr_insn), LRA_UNKNOWN_ALT);
6989 		      done_p = true;
6990 		      if (lra_dump_file != NULL)
6991 			{
6992 			  fprintf (lra_dump_file, "    Change reload insn:\n");
6993 			  dump_insn_slim (lra_dump_file, curr_insn);
6994 			}
6995 		    }
6996 		}
6997 	    }
6998 	  if (! done_p)
6999 	    {
7000 	      struct lra_insn_reg *reg;
7001 	      bool restored_regs_p = false;
7002 	      bool kept_regs_p = false;
7003 
7004 	      curr_id = lra_get_insn_recog_data (curr_insn);
7005 	      for (reg = curr_id->regs; reg != NULL; reg = reg->next)
7006 		{
7007 		  regno = reg->regno;
7008 		  restore_rtx = lra_reg_info[regno].restore_rtx;
7009 		  if (restore_rtx != NULL_RTX)
7010 		    {
7011 		      if (change_p && bitmap_bit_p (remove_pseudos, regno))
7012 			{
7013 			  lra_substitute_pseudo_within_insn
7014 			    (curr_insn, regno, restore_rtx, false);
7015 			  restored_regs_p = true;
7016 			}
7017 		      else
7018 			kept_regs_p = true;
7019 		    }
7020 		}
7021 	      if (NONDEBUG_INSN_P (curr_insn) && kept_regs_p)
7022 		{
7023 		  /* The instruction has changed since the previous
7024 		     constraints pass.  */
7025 		  lra_push_insn_and_update_insn_regno_info (curr_insn);
7026 		  lra_set_used_insn_alternative_by_uid
7027 		    (INSN_UID (curr_insn), LRA_UNKNOWN_ALT);
7028 		}
7029 	      else if (restored_regs_p)
7030 		/* The instruction has been restored to the form that
7031 		   it had during the previous constraints pass.  */
7032 		lra_update_insn_regno_info (curr_insn);
7033 	      if (restored_regs_p && lra_dump_file != NULL)
7034 		{
7035 		  fprintf (lra_dump_file, "   Insn after restoring regs:\n");
7036 		  dump_insn_slim (lra_dump_file, curr_insn);
7037 		}
7038 	    }
7039 	}
7040     }
7041   return change_p;
7042 }
7043 
7044 /* If optional reload pseudos failed to get a hard register or was not
7045    inherited, it is better to remove optional reloads.  We do this
7046    transformation after undoing inheritance to figure out necessity to
7047    remove optional reloads easier.  Return true if we do any
7048    change.  */
7049 static bool
undo_optional_reloads(void)7050 undo_optional_reloads (void)
7051 {
7052   bool change_p, keep_p;
7053   unsigned int regno, uid;
7054   bitmap_iterator bi, bi2;
7055   rtx_insn *insn;
7056   rtx set, src, dest;
7057   auto_bitmap removed_optional_reload_pseudos (&reg_obstack);
7058 
7059   bitmap_copy (removed_optional_reload_pseudos, &lra_optional_reload_pseudos);
7060   EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
7061     {
7062       keep_p = false;
7063       /* Keep optional reloads from previous subpasses.  */
7064       if (lra_reg_info[regno].restore_rtx == NULL_RTX
7065 	  /* If the original pseudo changed its allocation, just
7066 	     removing the optional pseudo is dangerous as the original
7067 	     pseudo will have longer live range.  */
7068 	  || reg_renumber[REGNO (lra_reg_info[regno].restore_rtx)] >= 0)
7069 	keep_p = true;
7070       else if (reg_renumber[regno] >= 0)
7071 	EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi2)
7072 	  {
7073 	    insn = lra_insn_recog_data[uid]->insn;
7074 	    if ((set = single_set (insn)) == NULL_RTX)
7075 	      continue;
7076 	    src = SET_SRC (set);
7077 	    dest = SET_DEST (set);
7078 	    if (! REG_P (src) || ! REG_P (dest))
7079 	      continue;
7080 	    if (REGNO (dest) == regno
7081 		/* Ignore insn for optional reloads itself.  */
7082 		&& REGNO (lra_reg_info[regno].restore_rtx) != REGNO (src)
7083 		/* Check only inheritance on last inheritance pass.  */
7084 		&& (int) REGNO (src) >= new_regno_start
7085 		/* Check that the optional reload was inherited.  */
7086 		&& bitmap_bit_p (&lra_inheritance_pseudos, REGNO (src)))
7087 	      {
7088 		keep_p = true;
7089 		break;
7090 	      }
7091 	  }
7092       if (keep_p)
7093 	{
7094 	  bitmap_clear_bit (removed_optional_reload_pseudos, regno);
7095 	  if (lra_dump_file != NULL)
7096 	    fprintf (lra_dump_file, "Keep optional reload reg %d\n", regno);
7097 	}
7098     }
7099   change_p = ! bitmap_empty_p (removed_optional_reload_pseudos);
7100   auto_bitmap insn_bitmap (&reg_obstack);
7101   EXECUTE_IF_SET_IN_BITMAP (removed_optional_reload_pseudos, 0, regno, bi)
7102     {
7103       if (lra_dump_file != NULL)
7104 	fprintf (lra_dump_file, "Remove optional reload reg %d\n", regno);
7105       bitmap_copy (insn_bitmap, &lra_reg_info[regno].insn_bitmap);
7106       EXECUTE_IF_SET_IN_BITMAP (insn_bitmap, 0, uid, bi2)
7107 	{
7108 	  insn = lra_insn_recog_data[uid]->insn;
7109 	  if ((set = single_set (insn)) != NULL_RTX)
7110 	    {
7111 	      src = SET_SRC (set);
7112 	      dest = SET_DEST (set);
7113 	      if (REG_P (src) && REG_P (dest)
7114 		  && ((REGNO (src) == regno
7115 		       && (REGNO (lra_reg_info[regno].restore_rtx)
7116 			   == REGNO (dest)))
7117 		      || (REGNO (dest) == regno
7118 			  && (REGNO (lra_reg_info[regno].restore_rtx)
7119 			      == REGNO (src)))))
7120 		{
7121 		  if (lra_dump_file != NULL)
7122 		    {
7123 		      fprintf (lra_dump_file, "  Deleting move %u\n",
7124 			       INSN_UID (insn));
7125 		      dump_insn_slim (lra_dump_file, insn);
7126 		    }
7127 		  delete_move_and_clobber (insn, REGNO (dest));
7128 		  continue;
7129 		}
7130 	      /* We should not worry about generation memory-memory
7131 		 moves here as if the corresponding inheritance did
7132 		 not work (inheritance pseudo did not get a hard reg),
7133 		 we remove the inheritance pseudo and the optional
7134 		 reload.  */
7135 	    }
7136 	  lra_substitute_pseudo_within_insn
7137 	    (insn, regno, lra_reg_info[regno].restore_rtx, false);
7138 	  lra_update_insn_regno_info (insn);
7139 	  if (lra_dump_file != NULL)
7140 	    {
7141 	      fprintf (lra_dump_file,
7142 		       "  Restoring original insn:\n");
7143 	      dump_insn_slim (lra_dump_file, insn);
7144 	    }
7145 	}
7146     }
7147   /* Clear restore_regnos.  */
7148   EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
7149     lra_reg_info[regno].restore_rtx = NULL_RTX;
7150   return change_p;
7151 }
7152 
7153 /* Entry function for undoing inheritance/split transformation.	 Return true
7154    if we did any RTL change in this pass.  */
7155 bool
lra_undo_inheritance(void)7156 lra_undo_inheritance (void)
7157 {
7158   unsigned int regno;
7159   int hard_regno;
7160   int n_all_inherit, n_inherit, n_all_split, n_split;
7161   rtx restore_rtx;
7162   bitmap_iterator bi;
7163   bool change_p;
7164 
7165   lra_undo_inheritance_iter++;
7166   if (lra_undo_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
7167     return false;
7168   if (lra_dump_file != NULL)
7169     fprintf (lra_dump_file,
7170 	     "\n********** Undoing inheritance #%d: **********\n\n",
7171 	     lra_undo_inheritance_iter);
7172   auto_bitmap remove_pseudos (&reg_obstack);
7173   n_inherit = n_all_inherit = 0;
7174   EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
7175     if (lra_reg_info[regno].restore_rtx != NULL_RTX)
7176       {
7177 	n_all_inherit++;
7178 	if (reg_renumber[regno] < 0
7179 	    /* If the original pseudo changed its allocation, just
7180 	       removing inheritance is dangerous as for changing
7181 	       allocation we used shorter live-ranges.  */
7182 	    && (! REG_P (lra_reg_info[regno].restore_rtx)
7183 		|| reg_renumber[REGNO (lra_reg_info[regno].restore_rtx)] < 0))
7184 	  bitmap_set_bit (remove_pseudos, regno);
7185 	else
7186 	  n_inherit++;
7187       }
7188   if (lra_dump_file != NULL && n_all_inherit != 0)
7189     fprintf (lra_dump_file, "Inherit %d out of %d (%.2f%%)\n",
7190 	     n_inherit, n_all_inherit,
7191 	     (double) n_inherit / n_all_inherit * 100);
7192   n_split = n_all_split = 0;
7193   EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
7194     if ((restore_rtx = lra_reg_info[regno].restore_rtx) != NULL_RTX)
7195       {
7196 	int restore_regno = REGNO (restore_rtx);
7197 
7198 	n_all_split++;
7199 	hard_regno = (restore_regno >= FIRST_PSEUDO_REGISTER
7200 		      ? reg_renumber[restore_regno] : restore_regno);
7201 	if (hard_regno < 0 || reg_renumber[regno] == hard_regno)
7202 	  bitmap_set_bit (remove_pseudos, regno);
7203 	else
7204 	  {
7205 	    n_split++;
7206 	    if (lra_dump_file != NULL)
7207 	      fprintf (lra_dump_file, "	     Keep split r%d (orig=r%d)\n",
7208 		       regno, restore_regno);
7209 	  }
7210       }
7211   if (lra_dump_file != NULL && n_all_split != 0)
7212     fprintf (lra_dump_file, "Split %d out of %d (%.2f%%)\n",
7213 	     n_split, n_all_split,
7214 	     (double) n_split / n_all_split * 100);
7215   change_p = remove_inheritance_pseudos (remove_pseudos);
7216   /* Clear restore_regnos.  */
7217   EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
7218     lra_reg_info[regno].restore_rtx = NULL_RTX;
7219   EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
7220     lra_reg_info[regno].restore_rtx = NULL_RTX;
7221   change_p = undo_optional_reloads () || change_p;
7222   return change_p;
7223 }
7224