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