runtime: GOARCH values for ppc64 BE & LE
[official-gcc.git] / gcc / postreload.c
blob43d4681886c45c236af32b2a4ae22cc85aa77bda
1 /* Perform simple optimizations to clean up the result of reload.
2 Copyright (C) 1987-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
25 #include "machmode.h"
26 #include "hard-reg-set.h"
27 #include "rtl.h"
28 #include "tm_p.h"
29 #include "obstack.h"
30 #include "insn-config.h"
31 #include "flags.h"
32 #include "hashtab.h"
33 #include "hash-set.h"
34 #include "vec.h"
35 #include "input.h"
36 #include "function.h"
37 #include "expr.h"
38 #include "insn-codes.h"
39 #include "optabs.h"
40 #include "regs.h"
41 #include "predict.h"
42 #include "dominance.h"
43 #include "cfg.h"
44 #include "cfgrtl.h"
45 #include "cfgbuild.h"
46 #include "cfgcleanup.h"
47 #include "basic-block.h"
48 #include "reload.h"
49 #include "recog.h"
50 #include "cselib.h"
51 #include "diagnostic-core.h"
52 #include "except.h"
53 #include "tree.h"
54 #include "target.h"
55 #include "tree-pass.h"
56 #include "df.h"
57 #include "dbgcnt.h"
59 static int reload_cse_noop_set_p (rtx);
60 static bool reload_cse_simplify (rtx_insn *, rtx);
61 static void reload_cse_regs_1 (void);
62 static int reload_cse_simplify_set (rtx, rtx_insn *);
63 static int reload_cse_simplify_operands (rtx_insn *, rtx);
65 static void reload_combine (void);
66 static void reload_combine_note_use (rtx *, rtx_insn *, int, rtx);
67 static void reload_combine_note_store (rtx, const_rtx, void *);
69 static bool reload_cse_move2add (rtx_insn *);
70 static void move2add_note_store (rtx, const_rtx, void *);
72 /* Call cse / combine like post-reload optimization phases.
73 FIRST is the first instruction. */
75 static void
76 reload_cse_regs (rtx_insn *first ATTRIBUTE_UNUSED)
78 bool moves_converted;
79 reload_cse_regs_1 ();
80 reload_combine ();
81 moves_converted = reload_cse_move2add (first);
82 if (flag_expensive_optimizations)
84 if (moves_converted)
85 reload_combine ();
86 reload_cse_regs_1 ();
90 /* See whether a single set SET is a noop. */
91 static int
92 reload_cse_noop_set_p (rtx set)
94 if (cselib_reg_set_mode (SET_DEST (set)) != GET_MODE (SET_DEST (set)))
95 return 0;
97 return rtx_equal_for_cselib_p (SET_DEST (set), SET_SRC (set));
100 /* Try to simplify INSN. Return true if the CFG may have changed. */
101 static bool
102 reload_cse_simplify (rtx_insn *insn, rtx testreg)
104 rtx body = PATTERN (insn);
105 basic_block insn_bb = BLOCK_FOR_INSN (insn);
106 unsigned insn_bb_succs = EDGE_COUNT (insn_bb->succs);
108 if (GET_CODE (body) == SET)
110 int count = 0;
112 /* Simplify even if we may think it is a no-op.
113 We may think a memory load of a value smaller than WORD_SIZE
114 is redundant because we haven't taken into account possible
115 implicit extension. reload_cse_simplify_set() will bring
116 this out, so it's safer to simplify before we delete. */
117 count += reload_cse_simplify_set (body, insn);
119 if (!count && reload_cse_noop_set_p (body))
121 rtx value = SET_DEST (body);
122 if (REG_P (value)
123 && ! REG_FUNCTION_VALUE_P (value))
124 value = 0;
125 if (check_for_inc_dec (insn))
126 delete_insn_and_edges (insn);
127 /* We're done with this insn. */
128 goto done;
131 if (count > 0)
132 apply_change_group ();
133 else
134 reload_cse_simplify_operands (insn, testreg);
136 else if (GET_CODE (body) == PARALLEL)
138 int i;
139 int count = 0;
140 rtx value = NULL_RTX;
142 /* Registers mentioned in the clobber list for an asm cannot be reused
143 within the body of the asm. Invalidate those registers now so that
144 we don't try to substitute values for them. */
145 if (asm_noperands (body) >= 0)
147 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
149 rtx part = XVECEXP (body, 0, i);
150 if (GET_CODE (part) == CLOBBER && REG_P (XEXP (part, 0)))
151 cselib_invalidate_rtx (XEXP (part, 0));
155 /* If every action in a PARALLEL is a noop, we can delete
156 the entire PARALLEL. */
157 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
159 rtx part = XVECEXP (body, 0, i);
160 if (GET_CODE (part) == SET)
162 if (! reload_cse_noop_set_p (part))
163 break;
164 if (REG_P (SET_DEST (part))
165 && REG_FUNCTION_VALUE_P (SET_DEST (part)))
167 if (value)
168 break;
169 value = SET_DEST (part);
172 else if (GET_CODE (part) != CLOBBER)
173 break;
176 if (i < 0)
178 if (check_for_inc_dec (insn))
179 delete_insn_and_edges (insn);
180 /* We're done with this insn. */
181 goto done;
184 /* It's not a no-op, but we can try to simplify it. */
185 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
186 if (GET_CODE (XVECEXP (body, 0, i)) == SET)
187 count += reload_cse_simplify_set (XVECEXP (body, 0, i), insn);
189 if (count > 0)
190 apply_change_group ();
191 else
192 reload_cse_simplify_operands (insn, testreg);
195 done:
196 return (EDGE_COUNT (insn_bb->succs) != insn_bb_succs);
199 /* Do a very simple CSE pass over the hard registers.
201 This function detects no-op moves where we happened to assign two
202 different pseudo-registers to the same hard register, and then
203 copied one to the other. Reload will generate a useless
204 instruction copying a register to itself.
206 This function also detects cases where we load a value from memory
207 into two different registers, and (if memory is more expensive than
208 registers) changes it to simply copy the first register into the
209 second register.
211 Another optimization is performed that scans the operands of each
212 instruction to see whether the value is already available in a
213 hard register. It then replaces the operand with the hard register
214 if possible, much like an optional reload would. */
216 static void
217 reload_cse_regs_1 (void)
219 bool cfg_changed = false;
220 basic_block bb;
221 rtx_insn *insn;
222 rtx testreg = gen_rtx_REG (VOIDmode, -1);
224 cselib_init (CSELIB_RECORD_MEMORY);
225 init_alias_analysis ();
227 FOR_EACH_BB_FN (bb, cfun)
228 FOR_BB_INSNS (bb, insn)
230 if (INSN_P (insn))
231 cfg_changed |= reload_cse_simplify (insn, testreg);
233 cselib_process_insn (insn);
236 /* Clean up. */
237 end_alias_analysis ();
238 cselib_finish ();
239 if (cfg_changed)
240 cleanup_cfg (0);
243 /* Try to simplify a single SET instruction. SET is the set pattern.
244 INSN is the instruction it came from.
245 This function only handles one case: if we set a register to a value
246 which is not a register, we try to find that value in some other register
247 and change the set into a register copy. */
249 static int
250 reload_cse_simplify_set (rtx set, rtx_insn *insn)
252 int did_change = 0;
253 int dreg;
254 rtx src;
255 reg_class_t dclass;
256 int old_cost;
257 cselib_val *val;
258 struct elt_loc_list *l;
259 #ifdef LOAD_EXTEND_OP
260 enum rtx_code extend_op = UNKNOWN;
261 #endif
262 bool speed = optimize_bb_for_speed_p (BLOCK_FOR_INSN (insn));
264 dreg = true_regnum (SET_DEST (set));
265 if (dreg < 0)
266 return 0;
268 src = SET_SRC (set);
269 if (side_effects_p (src) || true_regnum (src) >= 0)
270 return 0;
272 dclass = REGNO_REG_CLASS (dreg);
274 #ifdef LOAD_EXTEND_OP
275 /* When replacing a memory with a register, we need to honor assumptions
276 that combine made wrt the contents of sign bits. We'll do this by
277 generating an extend instruction instead of a reg->reg copy. Thus
278 the destination must be a register that we can widen. */
279 if (MEM_P (src)
280 && GET_MODE_BITSIZE (GET_MODE (src)) < BITS_PER_WORD
281 && (extend_op = LOAD_EXTEND_OP (GET_MODE (src))) != UNKNOWN
282 && !REG_P (SET_DEST (set)))
283 return 0;
284 #endif
286 val = cselib_lookup (src, GET_MODE (SET_DEST (set)), 0, VOIDmode);
287 if (! val)
288 return 0;
290 /* If memory loads are cheaper than register copies, don't change them. */
291 if (MEM_P (src))
292 old_cost = memory_move_cost (GET_MODE (src), dclass, true);
293 else if (REG_P (src))
294 old_cost = register_move_cost (GET_MODE (src),
295 REGNO_REG_CLASS (REGNO (src)), dclass);
296 else
297 old_cost = set_src_cost (src, speed);
299 for (l = val->locs; l; l = l->next)
301 rtx this_rtx = l->loc;
302 int this_cost;
304 if (CONSTANT_P (this_rtx) && ! references_value_p (this_rtx, 0))
306 #ifdef LOAD_EXTEND_OP
307 if (extend_op != UNKNOWN)
309 wide_int result;
311 if (!CONST_SCALAR_INT_P (this_rtx))
312 continue;
314 switch (extend_op)
316 case ZERO_EXTEND:
317 result = wide_int::from (std::make_pair (this_rtx,
318 GET_MODE (src)),
319 BITS_PER_WORD, UNSIGNED);
320 break;
321 case SIGN_EXTEND:
322 result = wide_int::from (std::make_pair (this_rtx,
323 GET_MODE (src)),
324 BITS_PER_WORD, SIGNED);
325 break;
326 default:
327 gcc_unreachable ();
329 this_rtx = immed_wide_int_const (result, word_mode);
331 #endif
332 this_cost = set_src_cost (this_rtx, speed);
334 else if (REG_P (this_rtx))
336 #ifdef LOAD_EXTEND_OP
337 if (extend_op != UNKNOWN)
339 this_rtx = gen_rtx_fmt_e (extend_op, word_mode, this_rtx);
340 this_cost = set_src_cost (this_rtx, speed);
342 else
343 #endif
344 this_cost = register_move_cost (GET_MODE (this_rtx),
345 REGNO_REG_CLASS (REGNO (this_rtx)),
346 dclass);
348 else
349 continue;
351 /* If equal costs, prefer registers over anything else. That
352 tends to lead to smaller instructions on some machines. */
353 if (this_cost < old_cost
354 || (this_cost == old_cost
355 && REG_P (this_rtx)
356 && !REG_P (SET_SRC (set))))
358 #ifdef LOAD_EXTEND_OP
359 if (GET_MODE_BITSIZE (GET_MODE (SET_DEST (set))) < BITS_PER_WORD
360 && extend_op != UNKNOWN
361 #ifdef CANNOT_CHANGE_MODE_CLASS
362 && !CANNOT_CHANGE_MODE_CLASS (GET_MODE (SET_DEST (set)),
363 word_mode,
364 REGNO_REG_CLASS (REGNO (SET_DEST (set))))
365 #endif
368 rtx wide_dest = gen_rtx_REG (word_mode, REGNO (SET_DEST (set)));
369 ORIGINAL_REGNO (wide_dest) = ORIGINAL_REGNO (SET_DEST (set));
370 validate_change (insn, &SET_DEST (set), wide_dest, 1);
372 #endif
374 validate_unshare_change (insn, &SET_SRC (set), this_rtx, 1);
375 old_cost = this_cost, did_change = 1;
379 return did_change;
382 /* Try to replace operands in INSN with equivalent values that are already
383 in registers. This can be viewed as optional reloading.
385 For each non-register operand in the insn, see if any hard regs are
386 known to be equivalent to that operand. Record the alternatives which
387 can accept these hard registers. Among all alternatives, select the
388 ones which are better or equal to the one currently matching, where
389 "better" is in terms of '?' and '!' constraints. Among the remaining
390 alternatives, select the one which replaces most operands with
391 hard registers. */
393 static int
394 reload_cse_simplify_operands (rtx_insn *insn, rtx testreg)
396 int i, j;
398 /* For each operand, all registers that are equivalent to it. */
399 HARD_REG_SET equiv_regs[MAX_RECOG_OPERANDS];
401 const char *constraints[MAX_RECOG_OPERANDS];
403 /* Vector recording how bad an alternative is. */
404 int *alternative_reject;
405 /* Vector recording how many registers can be introduced by choosing
406 this alternative. */
407 int *alternative_nregs;
408 /* Array of vectors recording, for each operand and each alternative,
409 which hard register to substitute, or -1 if the operand should be
410 left as it is. */
411 int *op_alt_regno[MAX_RECOG_OPERANDS];
412 /* Array of alternatives, sorted in order of decreasing desirability. */
413 int *alternative_order;
415 extract_constrain_insn (insn);
417 if (recog_data.n_alternatives == 0 || recog_data.n_operands == 0)
418 return 0;
420 alternative_reject = XALLOCAVEC (int, recog_data.n_alternatives);
421 alternative_nregs = XALLOCAVEC (int, recog_data.n_alternatives);
422 alternative_order = XALLOCAVEC (int, recog_data.n_alternatives);
423 memset (alternative_reject, 0, recog_data.n_alternatives * sizeof (int));
424 memset (alternative_nregs, 0, recog_data.n_alternatives * sizeof (int));
426 /* For each operand, find out which regs are equivalent. */
427 for (i = 0; i < recog_data.n_operands; i++)
429 cselib_val *v;
430 struct elt_loc_list *l;
431 rtx op;
433 CLEAR_HARD_REG_SET (equiv_regs[i]);
435 /* cselib blows up on CODE_LABELs. Trying to fix that doesn't seem
436 right, so avoid the problem here. Likewise if we have a constant
437 and the insn pattern doesn't tell us the mode we need. */
438 if (LABEL_P (recog_data.operand[i])
439 || (CONSTANT_P (recog_data.operand[i])
440 && recog_data.operand_mode[i] == VOIDmode))
441 continue;
443 op = recog_data.operand[i];
444 #ifdef LOAD_EXTEND_OP
445 if (MEM_P (op)
446 && GET_MODE_BITSIZE (GET_MODE (op)) < BITS_PER_WORD
447 && LOAD_EXTEND_OP (GET_MODE (op)) != UNKNOWN)
449 rtx set = single_set (insn);
451 /* We might have multiple sets, some of which do implicit
452 extension. Punt on this for now. */
453 if (! set)
454 continue;
455 /* If the destination is also a MEM or a STRICT_LOW_PART, no
456 extension applies.
457 Also, if there is an explicit extension, we don't have to
458 worry about an implicit one. */
459 else if (MEM_P (SET_DEST (set))
460 || GET_CODE (SET_DEST (set)) == STRICT_LOW_PART
461 || GET_CODE (SET_SRC (set)) == ZERO_EXTEND
462 || GET_CODE (SET_SRC (set)) == SIGN_EXTEND)
463 ; /* Continue ordinary processing. */
464 #ifdef CANNOT_CHANGE_MODE_CLASS
465 /* If the register cannot change mode to word_mode, it follows that
466 it cannot have been used in word_mode. */
467 else if (REG_P (SET_DEST (set))
468 && CANNOT_CHANGE_MODE_CLASS (GET_MODE (SET_DEST (set)),
469 word_mode,
470 REGNO_REG_CLASS (REGNO (SET_DEST (set)))))
471 ; /* Continue ordinary processing. */
472 #endif
473 /* If this is a straight load, make the extension explicit. */
474 else if (REG_P (SET_DEST (set))
475 && recog_data.n_operands == 2
476 && SET_SRC (set) == op
477 && SET_DEST (set) == recog_data.operand[1-i])
479 validate_change (insn, recog_data.operand_loc[i],
480 gen_rtx_fmt_e (LOAD_EXTEND_OP (GET_MODE (op)),
481 word_mode, op),
483 validate_change (insn, recog_data.operand_loc[1-i],
484 gen_rtx_REG (word_mode, REGNO (SET_DEST (set))),
486 if (! apply_change_group ())
487 return 0;
488 return reload_cse_simplify_operands (insn, testreg);
490 else
491 /* ??? There might be arithmetic operations with memory that are
492 safe to optimize, but is it worth the trouble? */
493 continue;
495 #endif /* LOAD_EXTEND_OP */
496 if (side_effects_p (op))
497 continue;
498 v = cselib_lookup (op, recog_data.operand_mode[i], 0, VOIDmode);
499 if (! v)
500 continue;
502 for (l = v->locs; l; l = l->next)
503 if (REG_P (l->loc))
504 SET_HARD_REG_BIT (equiv_regs[i], REGNO (l->loc));
507 alternative_mask preferred = get_preferred_alternatives (insn);
508 for (i = 0; i < recog_data.n_operands; i++)
510 machine_mode mode;
511 int regno;
512 const char *p;
514 op_alt_regno[i] = XALLOCAVEC (int, recog_data.n_alternatives);
515 for (j = 0; j < recog_data.n_alternatives; j++)
516 op_alt_regno[i][j] = -1;
518 p = constraints[i] = recog_data.constraints[i];
519 mode = recog_data.operand_mode[i];
521 /* Add the reject values for each alternative given by the constraints
522 for this operand. */
523 j = 0;
524 while (*p != '\0')
526 char c = *p++;
527 if (c == ',')
528 j++;
529 else if (c == '?')
530 alternative_reject[j] += 3;
531 else if (c == '!')
532 alternative_reject[j] += 300;
535 /* We won't change operands which are already registers. We
536 also don't want to modify output operands. */
537 regno = true_regnum (recog_data.operand[i]);
538 if (regno >= 0
539 || constraints[i][0] == '='
540 || constraints[i][0] == '+')
541 continue;
543 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
545 enum reg_class rclass = NO_REGS;
547 if (! TEST_HARD_REG_BIT (equiv_regs[i], regno))
548 continue;
550 SET_REGNO_RAW (testreg, regno);
551 PUT_MODE (testreg, mode);
553 /* We found a register equal to this operand. Now look for all
554 alternatives that can accept this register and have not been
555 assigned a register they can use yet. */
556 j = 0;
557 p = constraints[i];
558 for (;;)
560 char c = *p;
562 switch (c)
564 case 'g':
565 rclass = reg_class_subunion[rclass][GENERAL_REGS];
566 break;
568 default:
569 rclass
570 = (reg_class_subunion
571 [rclass]
572 [reg_class_for_constraint (lookup_constraint (p))]);
573 break;
575 case ',': case '\0':
576 /* See if REGNO fits this alternative, and set it up as the
577 replacement register if we don't have one for this
578 alternative yet and the operand being replaced is not
579 a cheap CONST_INT. */
580 if (op_alt_regno[i][j] == -1
581 && TEST_BIT (preferred, j)
582 && reg_fits_class_p (testreg, rclass, 0, mode)
583 && (!CONST_INT_P (recog_data.operand[i])
584 || (set_src_cost (recog_data.operand[i],
585 optimize_bb_for_speed_p
586 (BLOCK_FOR_INSN (insn)))
587 > set_src_cost (testreg,
588 optimize_bb_for_speed_p
589 (BLOCK_FOR_INSN (insn))))))
591 alternative_nregs[j]++;
592 op_alt_regno[i][j] = regno;
594 j++;
595 rclass = NO_REGS;
596 break;
598 p += CONSTRAINT_LEN (c, p);
600 if (c == '\0')
601 break;
606 /* Record all alternatives which are better or equal to the currently
607 matching one in the alternative_order array. */
608 for (i = j = 0; i < recog_data.n_alternatives; i++)
609 if (alternative_reject[i] <= alternative_reject[which_alternative])
610 alternative_order[j++] = i;
611 recog_data.n_alternatives = j;
613 /* Sort it. Given a small number of alternatives, a dumb algorithm
614 won't hurt too much. */
615 for (i = 0; i < recog_data.n_alternatives - 1; i++)
617 int best = i;
618 int best_reject = alternative_reject[alternative_order[i]];
619 int best_nregs = alternative_nregs[alternative_order[i]];
620 int tmp;
622 for (j = i + 1; j < recog_data.n_alternatives; j++)
624 int this_reject = alternative_reject[alternative_order[j]];
625 int this_nregs = alternative_nregs[alternative_order[j]];
627 if (this_reject < best_reject
628 || (this_reject == best_reject && this_nregs > best_nregs))
630 best = j;
631 best_reject = this_reject;
632 best_nregs = this_nregs;
636 tmp = alternative_order[best];
637 alternative_order[best] = alternative_order[i];
638 alternative_order[i] = tmp;
641 /* Substitute the operands as determined by op_alt_regno for the best
642 alternative. */
643 j = alternative_order[0];
645 for (i = 0; i < recog_data.n_operands; i++)
647 machine_mode mode = recog_data.operand_mode[i];
648 if (op_alt_regno[i][j] == -1)
649 continue;
651 validate_change (insn, recog_data.operand_loc[i],
652 gen_rtx_REG (mode, op_alt_regno[i][j]), 1);
655 for (i = recog_data.n_dups - 1; i >= 0; i--)
657 int op = recog_data.dup_num[i];
658 machine_mode mode = recog_data.operand_mode[op];
660 if (op_alt_regno[op][j] == -1)
661 continue;
663 validate_change (insn, recog_data.dup_loc[i],
664 gen_rtx_REG (mode, op_alt_regno[op][j]), 1);
667 return apply_change_group ();
670 /* If reload couldn't use reg+reg+offset addressing, try to use reg+reg
671 addressing now.
672 This code might also be useful when reload gave up on reg+reg addressing
673 because of clashes between the return register and INDEX_REG_CLASS. */
675 /* The maximum number of uses of a register we can keep track of to
676 replace them with reg+reg addressing. */
677 #define RELOAD_COMBINE_MAX_USES 16
679 /* Describes a recorded use of a register. */
680 struct reg_use
682 /* The insn where a register has been used. */
683 rtx_insn *insn;
684 /* Points to the memory reference enclosing the use, if any, NULL_RTX
685 otherwise. */
686 rtx containing_mem;
687 /* Location of the register within INSN. */
688 rtx *usep;
689 /* The reverse uid of the insn. */
690 int ruid;
693 /* If the register is used in some unknown fashion, USE_INDEX is negative.
694 If it is dead, USE_INDEX is RELOAD_COMBINE_MAX_USES, and STORE_RUID
695 indicates where it is first set or clobbered.
696 Otherwise, USE_INDEX is the index of the last encountered use of the
697 register (which is first among these we have seen since we scan backwards).
698 USE_RUID indicates the first encountered, i.e. last, of these uses.
699 If ALL_OFFSETS_MATCH is true, all encountered uses were inside a PLUS
700 with a constant offset; OFFSET contains this constant in that case.
701 STORE_RUID is always meaningful if we only want to use a value in a
702 register in a different place: it denotes the next insn in the insn
703 stream (i.e. the last encountered) that sets or clobbers the register.
704 REAL_STORE_RUID is similar, but clobbers are ignored when updating it. */
705 static struct
707 struct reg_use reg_use[RELOAD_COMBINE_MAX_USES];
708 rtx offset;
709 int use_index;
710 int store_ruid;
711 int real_store_ruid;
712 int use_ruid;
713 bool all_offsets_match;
714 } reg_state[FIRST_PSEUDO_REGISTER];
716 /* Reverse linear uid. This is increased in reload_combine while scanning
717 the instructions from last to first. It is used to set last_label_ruid
718 and the store_ruid / use_ruid fields in reg_state. */
719 static int reload_combine_ruid;
721 /* The RUID of the last label we encountered in reload_combine. */
722 static int last_label_ruid;
724 /* The RUID of the last jump we encountered in reload_combine. */
725 static int last_jump_ruid;
727 /* The register numbers of the first and last index register. A value of
728 -1 in LAST_INDEX_REG indicates that we've previously computed these
729 values and found no suitable index registers. */
730 static int first_index_reg = -1;
731 static int last_index_reg;
733 #define LABEL_LIVE(LABEL) \
734 (label_live[CODE_LABEL_NUMBER (LABEL) - min_labelno])
736 /* Subroutine of reload_combine_split_ruids, called to fix up a single
737 ruid pointed to by *PRUID if it is higher than SPLIT_RUID. */
739 static inline void
740 reload_combine_split_one_ruid (int *pruid, int split_ruid)
742 if (*pruid > split_ruid)
743 (*pruid)++;
746 /* Called when we insert a new insn in a position we've already passed in
747 the scan. Examine all our state, increasing all ruids that are higher
748 than SPLIT_RUID by one in order to make room for a new insn. */
750 static void
751 reload_combine_split_ruids (int split_ruid)
753 unsigned i;
755 reload_combine_split_one_ruid (&reload_combine_ruid, split_ruid);
756 reload_combine_split_one_ruid (&last_label_ruid, split_ruid);
757 reload_combine_split_one_ruid (&last_jump_ruid, split_ruid);
759 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
761 int j, idx = reg_state[i].use_index;
762 reload_combine_split_one_ruid (&reg_state[i].use_ruid, split_ruid);
763 reload_combine_split_one_ruid (&reg_state[i].store_ruid, split_ruid);
764 reload_combine_split_one_ruid (&reg_state[i].real_store_ruid,
765 split_ruid);
766 if (idx < 0)
767 continue;
768 for (j = idx; j < RELOAD_COMBINE_MAX_USES; j++)
770 reload_combine_split_one_ruid (&reg_state[i].reg_use[j].ruid,
771 split_ruid);
776 /* Called when we are about to rescan a previously encountered insn with
777 reload_combine_note_use after modifying some part of it. This clears all
778 information about uses in that particular insn. */
780 static void
781 reload_combine_purge_insn_uses (rtx_insn *insn)
783 unsigned i;
785 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
787 int j, k, idx = reg_state[i].use_index;
788 if (idx < 0)
789 continue;
790 j = k = RELOAD_COMBINE_MAX_USES;
791 while (j-- > idx)
793 if (reg_state[i].reg_use[j].insn != insn)
795 k--;
796 if (k != j)
797 reg_state[i].reg_use[k] = reg_state[i].reg_use[j];
800 reg_state[i].use_index = k;
804 /* Called when we need to forget about all uses of REGNO after an insn
805 which is identified by RUID. */
807 static void
808 reload_combine_purge_reg_uses_after_ruid (unsigned regno, int ruid)
810 int j, k, idx = reg_state[regno].use_index;
811 if (idx < 0)
812 return;
813 j = k = RELOAD_COMBINE_MAX_USES;
814 while (j-- > idx)
816 if (reg_state[regno].reg_use[j].ruid >= ruid)
818 k--;
819 if (k != j)
820 reg_state[regno].reg_use[k] = reg_state[regno].reg_use[j];
823 reg_state[regno].use_index = k;
826 /* Find the use of REGNO with the ruid that is highest among those
827 lower than RUID_LIMIT, and return it if it is the only use of this
828 reg in the insn. Return NULL otherwise. */
830 static struct reg_use *
831 reload_combine_closest_single_use (unsigned regno, int ruid_limit)
833 int i, best_ruid = 0;
834 int use_idx = reg_state[regno].use_index;
835 struct reg_use *retval;
837 if (use_idx < 0)
838 return NULL;
839 retval = NULL;
840 for (i = use_idx; i < RELOAD_COMBINE_MAX_USES; i++)
842 struct reg_use *use = reg_state[regno].reg_use + i;
843 int this_ruid = use->ruid;
844 if (this_ruid >= ruid_limit)
845 continue;
846 if (this_ruid > best_ruid)
848 best_ruid = this_ruid;
849 retval = use;
851 else if (this_ruid == best_ruid)
852 retval = NULL;
854 if (last_label_ruid >= best_ruid)
855 return NULL;
856 return retval;
859 /* After we've moved an add insn, fix up any debug insns that occur
860 between the old location of the add and the new location. REG is
861 the destination register of the add insn; REPLACEMENT is the
862 SET_SRC of the add. FROM and TO specify the range in which we
863 should make this change on debug insns. */
865 static void
866 fixup_debug_insns (rtx reg, rtx replacement, rtx_insn *from, rtx_insn *to)
868 rtx_insn *insn;
869 for (insn = from; insn != to; insn = NEXT_INSN (insn))
871 rtx t;
873 if (!DEBUG_INSN_P (insn))
874 continue;
876 t = INSN_VAR_LOCATION_LOC (insn);
877 t = simplify_replace_rtx (t, reg, replacement);
878 validate_change (insn, &INSN_VAR_LOCATION_LOC (insn), t, 0);
882 /* Subroutine of reload_combine_recognize_const_pattern. Try to replace REG
883 with SRC in the insn described by USE, taking costs into account. Return
884 true if we made the replacement. */
886 static bool
887 try_replace_in_use (struct reg_use *use, rtx reg, rtx src)
889 rtx_insn *use_insn = use->insn;
890 rtx mem = use->containing_mem;
891 bool speed = optimize_bb_for_speed_p (BLOCK_FOR_INSN (use_insn));
893 if (mem != NULL_RTX)
895 addr_space_t as = MEM_ADDR_SPACE (mem);
896 rtx oldaddr = XEXP (mem, 0);
897 rtx newaddr = NULL_RTX;
898 int old_cost = address_cost (oldaddr, GET_MODE (mem), as, speed);
899 int new_cost;
901 newaddr = simplify_replace_rtx (oldaddr, reg, src);
902 if (memory_address_addr_space_p (GET_MODE (mem), newaddr, as))
904 XEXP (mem, 0) = newaddr;
905 new_cost = address_cost (newaddr, GET_MODE (mem), as, speed);
906 XEXP (mem, 0) = oldaddr;
907 if (new_cost <= old_cost
908 && validate_change (use_insn,
909 &XEXP (mem, 0), newaddr, 0))
910 return true;
913 else
915 rtx new_set = single_set (use_insn);
916 if (new_set
917 && REG_P (SET_DEST (new_set))
918 && GET_CODE (SET_SRC (new_set)) == PLUS
919 && REG_P (XEXP (SET_SRC (new_set), 0))
920 && CONSTANT_P (XEXP (SET_SRC (new_set), 1)))
922 rtx new_src;
923 int old_cost = set_src_cost (SET_SRC (new_set), speed);
925 gcc_assert (rtx_equal_p (XEXP (SET_SRC (new_set), 0), reg));
926 new_src = simplify_replace_rtx (SET_SRC (new_set), reg, src);
928 if (set_src_cost (new_src, speed) <= old_cost
929 && validate_change (use_insn, &SET_SRC (new_set),
930 new_src, 0))
931 return true;
934 return false;
937 /* Called by reload_combine when scanning INSN. This function tries to detect
938 patterns where a constant is added to a register, and the result is used
939 in an address.
940 Return true if no further processing is needed on INSN; false if it wasn't
941 recognized and should be handled normally. */
943 static bool
944 reload_combine_recognize_const_pattern (rtx_insn *insn)
946 int from_ruid = reload_combine_ruid;
947 rtx set, pat, reg, src, addreg;
948 unsigned int regno;
949 struct reg_use *use;
950 bool must_move_add;
951 rtx_insn *add_moved_after_insn = NULL;
952 int add_moved_after_ruid = 0;
953 int clobbered_regno = -1;
955 set = single_set (insn);
956 if (set == NULL_RTX)
957 return false;
959 reg = SET_DEST (set);
960 src = SET_SRC (set);
961 if (!REG_P (reg)
962 || hard_regno_nregs[REGNO (reg)][GET_MODE (reg)] != 1
963 || GET_MODE (reg) != Pmode
964 || reg == stack_pointer_rtx)
965 return false;
967 regno = REGNO (reg);
969 /* We look for a REG1 = REG2 + CONSTANT insn, followed by either
970 uses of REG1 inside an address, or inside another add insn. If
971 possible and profitable, merge the addition into subsequent
972 uses. */
973 if (GET_CODE (src) != PLUS
974 || !REG_P (XEXP (src, 0))
975 || !CONSTANT_P (XEXP (src, 1)))
976 return false;
978 addreg = XEXP (src, 0);
979 must_move_add = rtx_equal_p (reg, addreg);
981 pat = PATTERN (insn);
982 if (must_move_add && set != pat)
984 /* We have to be careful when moving the add; apart from the
985 single_set there may also be clobbers. Recognize one special
986 case, that of one clobber alongside the set (likely a clobber
987 of the CC register). */
988 gcc_assert (GET_CODE (PATTERN (insn)) == PARALLEL);
989 if (XVECLEN (pat, 0) != 2 || XVECEXP (pat, 0, 0) != set
990 || GET_CODE (XVECEXP (pat, 0, 1)) != CLOBBER
991 || !REG_P (XEXP (XVECEXP (pat, 0, 1), 0)))
992 return false;
993 clobbered_regno = REGNO (XEXP (XVECEXP (pat, 0, 1), 0));
998 use = reload_combine_closest_single_use (regno, from_ruid);
1000 if (use)
1001 /* Start the search for the next use from here. */
1002 from_ruid = use->ruid;
1004 if (use && GET_MODE (*use->usep) == Pmode)
1006 bool delete_add = false;
1007 rtx_insn *use_insn = use->insn;
1008 int use_ruid = use->ruid;
1010 /* Avoid moving the add insn past a jump. */
1011 if (must_move_add && use_ruid <= last_jump_ruid)
1012 break;
1014 /* If the add clobbers another hard reg in parallel, don't move
1015 it past a real set of this hard reg. */
1016 if (must_move_add && clobbered_regno >= 0
1017 && reg_state[clobbered_regno].real_store_ruid >= use_ruid)
1018 break;
1020 #ifdef HAVE_cc0
1021 /* Do not separate cc0 setter and cc0 user on HAVE_cc0 targets. */
1022 if (must_move_add && sets_cc0_p (PATTERN (use_insn)))
1023 break;
1024 #endif
1026 gcc_assert (reg_state[regno].store_ruid <= use_ruid);
1027 /* Avoid moving a use of ADDREG past a point where it is stored. */
1028 if (reg_state[REGNO (addreg)].store_ruid > use_ruid)
1029 break;
1031 /* We also must not move the addition past an insn that sets
1032 the same register, unless we can combine two add insns. */
1033 if (must_move_add && reg_state[regno].store_ruid == use_ruid)
1035 if (use->containing_mem == NULL_RTX)
1036 delete_add = true;
1037 else
1038 break;
1041 if (try_replace_in_use (use, reg, src))
1043 reload_combine_purge_insn_uses (use_insn);
1044 reload_combine_note_use (&PATTERN (use_insn), use_insn,
1045 use_ruid, NULL_RTX);
1047 if (delete_add)
1049 fixup_debug_insns (reg, src, insn, use_insn);
1050 delete_insn (insn);
1051 return true;
1053 if (must_move_add)
1055 add_moved_after_insn = use_insn;
1056 add_moved_after_ruid = use_ruid;
1058 continue;
1061 /* If we get here, we couldn't handle this use. */
1062 if (must_move_add)
1063 break;
1065 while (use);
1067 if (!must_move_add || add_moved_after_insn == NULL_RTX)
1068 /* Process the add normally. */
1069 return false;
1071 fixup_debug_insns (reg, src, insn, add_moved_after_insn);
1073 reorder_insns (insn, insn, add_moved_after_insn);
1074 reload_combine_purge_reg_uses_after_ruid (regno, add_moved_after_ruid);
1075 reload_combine_split_ruids (add_moved_after_ruid - 1);
1076 reload_combine_note_use (&PATTERN (insn), insn,
1077 add_moved_after_ruid, NULL_RTX);
1078 reg_state[regno].store_ruid = add_moved_after_ruid;
1080 return true;
1083 /* Called by reload_combine when scanning INSN. Try to detect a pattern we
1084 can handle and improve. Return true if no further processing is needed on
1085 INSN; false if it wasn't recognized and should be handled normally. */
1087 static bool
1088 reload_combine_recognize_pattern (rtx_insn *insn)
1090 rtx set, reg, src;
1091 unsigned int regno;
1093 set = single_set (insn);
1094 if (set == NULL_RTX)
1095 return false;
1097 reg = SET_DEST (set);
1098 src = SET_SRC (set);
1099 if (!REG_P (reg)
1100 || hard_regno_nregs[REGNO (reg)][GET_MODE (reg)] != 1)
1101 return false;
1103 regno = REGNO (reg);
1105 /* Look for (set (REGX) (CONST_INT))
1106 (set (REGX) (PLUS (REGX) (REGY)))
1108 ... (MEM (REGX)) ...
1109 and convert it to
1110 (set (REGZ) (CONST_INT))
1112 ... (MEM (PLUS (REGZ) (REGY)))... .
1114 First, check that we have (set (REGX) (PLUS (REGX) (REGY)))
1115 and that we know all uses of REGX before it dies.
1116 Also, explicitly check that REGX != REGY; our life information
1117 does not yet show whether REGY changes in this insn. */
1119 if (GET_CODE (src) == PLUS
1120 && reg_state[regno].all_offsets_match
1121 && last_index_reg != -1
1122 && REG_P (XEXP (src, 1))
1123 && rtx_equal_p (XEXP (src, 0), reg)
1124 && !rtx_equal_p (XEXP (src, 1), reg)
1125 && reg_state[regno].use_index >= 0
1126 && reg_state[regno].use_index < RELOAD_COMBINE_MAX_USES
1127 && last_label_ruid < reg_state[regno].use_ruid)
1129 rtx base = XEXP (src, 1);
1130 rtx_insn *prev = prev_nonnote_nondebug_insn (insn);
1131 rtx prev_set = prev ? single_set (prev) : NULL_RTX;
1132 rtx index_reg = NULL_RTX;
1133 rtx reg_sum = NULL_RTX;
1134 int i;
1136 /* Now we need to set INDEX_REG to an index register (denoted as
1137 REGZ in the illustration above) and REG_SUM to the expression
1138 register+register that we want to use to substitute uses of REG
1139 (typically in MEMs) with. First check REG and BASE for being
1140 index registers; we can use them even if they are not dead. */
1141 if (TEST_HARD_REG_BIT (reg_class_contents[INDEX_REG_CLASS], regno)
1142 || TEST_HARD_REG_BIT (reg_class_contents[INDEX_REG_CLASS],
1143 REGNO (base)))
1145 index_reg = reg;
1146 reg_sum = src;
1148 else
1150 /* Otherwise, look for a free index register. Since we have
1151 checked above that neither REG nor BASE are index registers,
1152 if we find anything at all, it will be different from these
1153 two registers. */
1154 for (i = first_index_reg; i <= last_index_reg; i++)
1156 if (TEST_HARD_REG_BIT (reg_class_contents[INDEX_REG_CLASS], i)
1157 && reg_state[i].use_index == RELOAD_COMBINE_MAX_USES
1158 && reg_state[i].store_ruid <= reg_state[regno].use_ruid
1159 && (call_used_regs[i] || df_regs_ever_live_p (i))
1160 && (!frame_pointer_needed || i != HARD_FRAME_POINTER_REGNUM)
1161 && !fixed_regs[i] && !global_regs[i]
1162 && hard_regno_nregs[i][GET_MODE (reg)] == 1
1163 && targetm.hard_regno_scratch_ok (i))
1165 index_reg = gen_rtx_REG (GET_MODE (reg), i);
1166 reg_sum = gen_rtx_PLUS (GET_MODE (reg), index_reg, base);
1167 break;
1172 /* Check that PREV_SET is indeed (set (REGX) (CONST_INT)) and that
1173 (REGY), i.e. BASE, is not clobbered before the last use we'll
1174 create. */
1175 if (reg_sum
1176 && prev_set
1177 && CONST_INT_P (SET_SRC (prev_set))
1178 && rtx_equal_p (SET_DEST (prev_set), reg)
1179 && (reg_state[REGNO (base)].store_ruid
1180 <= reg_state[regno].use_ruid))
1182 /* Change destination register and, if necessary, the constant
1183 value in PREV, the constant loading instruction. */
1184 validate_change (prev, &SET_DEST (prev_set), index_reg, 1);
1185 if (reg_state[regno].offset != const0_rtx)
1186 validate_change (prev,
1187 &SET_SRC (prev_set),
1188 GEN_INT (INTVAL (SET_SRC (prev_set))
1189 + INTVAL (reg_state[regno].offset)),
1192 /* Now for every use of REG that we have recorded, replace REG
1193 with REG_SUM. */
1194 for (i = reg_state[regno].use_index;
1195 i < RELOAD_COMBINE_MAX_USES; i++)
1196 validate_unshare_change (reg_state[regno].reg_use[i].insn,
1197 reg_state[regno].reg_use[i].usep,
1198 /* Each change must have its own
1199 replacement. */
1200 reg_sum, 1);
1202 if (apply_change_group ())
1204 struct reg_use *lowest_ruid = NULL;
1206 /* For every new use of REG_SUM, we have to record the use
1207 of BASE therein, i.e. operand 1. */
1208 for (i = reg_state[regno].use_index;
1209 i < RELOAD_COMBINE_MAX_USES; i++)
1211 struct reg_use *use = reg_state[regno].reg_use + i;
1212 reload_combine_note_use (&XEXP (*use->usep, 1), use->insn,
1213 use->ruid, use->containing_mem);
1214 if (lowest_ruid == NULL || use->ruid < lowest_ruid->ruid)
1215 lowest_ruid = use;
1218 fixup_debug_insns (reg, reg_sum, insn, lowest_ruid->insn);
1220 /* Delete the reg-reg addition. */
1221 delete_insn (insn);
1223 if (reg_state[regno].offset != const0_rtx)
1224 /* Previous REG_EQUIV / REG_EQUAL notes for PREV
1225 are now invalid. */
1226 remove_reg_equal_equiv_notes (prev);
1228 reg_state[regno].use_index = RELOAD_COMBINE_MAX_USES;
1229 return true;
1233 return false;
1236 static void
1237 reload_combine (void)
1239 rtx_insn *insn, *prev;
1240 basic_block bb;
1241 unsigned int r;
1242 int min_labelno, n_labels;
1243 HARD_REG_SET ever_live_at_start, *label_live;
1245 /* To avoid wasting too much time later searching for an index register,
1246 determine the minimum and maximum index register numbers. */
1247 if (INDEX_REG_CLASS == NO_REGS)
1248 last_index_reg = -1;
1249 else if (first_index_reg == -1 && last_index_reg == 0)
1251 for (r = 0; r < FIRST_PSEUDO_REGISTER; r++)
1252 if (TEST_HARD_REG_BIT (reg_class_contents[INDEX_REG_CLASS], r))
1254 if (first_index_reg == -1)
1255 first_index_reg = r;
1257 last_index_reg = r;
1260 /* If no index register is available, we can quit now. Set LAST_INDEX_REG
1261 to -1 so we'll know to quit early the next time we get here. */
1262 if (first_index_reg == -1)
1264 last_index_reg = -1;
1265 return;
1269 /* Set up LABEL_LIVE and EVER_LIVE_AT_START. The register lifetime
1270 information is a bit fuzzy immediately after reload, but it's
1271 still good enough to determine which registers are live at a jump
1272 destination. */
1273 min_labelno = get_first_label_num ();
1274 n_labels = max_label_num () - min_labelno;
1275 label_live = XNEWVEC (HARD_REG_SET, n_labels);
1276 CLEAR_HARD_REG_SET (ever_live_at_start);
1278 FOR_EACH_BB_REVERSE_FN (bb, cfun)
1280 insn = BB_HEAD (bb);
1281 if (LABEL_P (insn))
1283 HARD_REG_SET live;
1284 bitmap live_in = df_get_live_in (bb);
1286 REG_SET_TO_HARD_REG_SET (live, live_in);
1287 compute_use_by_pseudos (&live, live_in);
1288 COPY_HARD_REG_SET (LABEL_LIVE (insn), live);
1289 IOR_HARD_REG_SET (ever_live_at_start, live);
1293 /* Initialize last_label_ruid, reload_combine_ruid and reg_state. */
1294 last_label_ruid = last_jump_ruid = reload_combine_ruid = 0;
1295 for (r = 0; r < FIRST_PSEUDO_REGISTER; r++)
1297 reg_state[r].store_ruid = 0;
1298 reg_state[r].real_store_ruid = 0;
1299 if (fixed_regs[r])
1300 reg_state[r].use_index = -1;
1301 else
1302 reg_state[r].use_index = RELOAD_COMBINE_MAX_USES;
1305 for (insn = get_last_insn (); insn; insn = prev)
1307 bool control_flow_insn;
1308 rtx note;
1310 prev = PREV_INSN (insn);
1312 /* We cannot do our optimization across labels. Invalidating all the use
1313 information we have would be costly, so we just note where the label
1314 is and then later disable any optimization that would cross it. */
1315 if (LABEL_P (insn))
1316 last_label_ruid = reload_combine_ruid;
1317 else if (BARRIER_P (insn))
1319 /* Crossing a barrier resets all the use information. */
1320 for (r = 0; r < FIRST_PSEUDO_REGISTER; r++)
1321 if (! fixed_regs[r])
1322 reg_state[r].use_index = RELOAD_COMBINE_MAX_USES;
1324 else if (INSN_P (insn) && volatile_insn_p (PATTERN (insn)))
1325 /* Optimizations across insns being marked as volatile must be
1326 prevented. All the usage information is invalidated
1327 here. */
1328 for (r = 0; r < FIRST_PSEUDO_REGISTER; r++)
1329 if (! fixed_regs[r]
1330 && reg_state[r].use_index != RELOAD_COMBINE_MAX_USES)
1331 reg_state[r].use_index = -1;
1333 if (! NONDEBUG_INSN_P (insn))
1334 continue;
1336 reload_combine_ruid++;
1338 control_flow_insn = control_flow_insn_p (insn);
1339 if (control_flow_insn)
1340 last_jump_ruid = reload_combine_ruid;
1342 if (reload_combine_recognize_const_pattern (insn)
1343 || reload_combine_recognize_pattern (insn))
1344 continue;
1346 note_stores (PATTERN (insn), reload_combine_note_store, NULL);
1348 if (CALL_P (insn))
1350 rtx link;
1352 for (r = 0; r < FIRST_PSEUDO_REGISTER; r++)
1353 if (call_used_regs[r])
1355 reg_state[r].use_index = RELOAD_COMBINE_MAX_USES;
1356 reg_state[r].store_ruid = reload_combine_ruid;
1359 for (link = CALL_INSN_FUNCTION_USAGE (insn); link;
1360 link = XEXP (link, 1))
1362 rtx setuse = XEXP (link, 0);
1363 rtx usage_rtx = XEXP (setuse, 0);
1364 if ((GET_CODE (setuse) == USE || GET_CODE (setuse) == CLOBBER)
1365 && REG_P (usage_rtx))
1367 unsigned int i;
1368 unsigned int start_reg = REGNO (usage_rtx);
1369 unsigned int num_regs
1370 = hard_regno_nregs[start_reg][GET_MODE (usage_rtx)];
1371 unsigned int end_reg = start_reg + num_regs - 1;
1372 for (i = start_reg; i <= end_reg; i++)
1373 if (GET_CODE (XEXP (link, 0)) == CLOBBER)
1375 reg_state[i].use_index = RELOAD_COMBINE_MAX_USES;
1376 reg_state[i].store_ruid = reload_combine_ruid;
1378 else
1379 reg_state[i].use_index = -1;
1384 if (control_flow_insn && !ANY_RETURN_P (PATTERN (insn)))
1386 /* Non-spill registers might be used at the call destination in
1387 some unknown fashion, so we have to mark the unknown use. */
1388 HARD_REG_SET *live;
1390 if ((condjump_p (insn) || condjump_in_parallel_p (insn))
1391 && JUMP_LABEL (insn))
1393 if (ANY_RETURN_P (JUMP_LABEL (insn)))
1394 live = NULL;
1395 else
1396 live = &LABEL_LIVE (JUMP_LABEL (insn));
1398 else
1399 live = &ever_live_at_start;
1401 if (live)
1402 for (r = 0; r < FIRST_PSEUDO_REGISTER; r++)
1403 if (TEST_HARD_REG_BIT (*live, r))
1404 reg_state[r].use_index = -1;
1407 reload_combine_note_use (&PATTERN (insn), insn, reload_combine_ruid,
1408 NULL_RTX);
1410 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
1412 if (REG_NOTE_KIND (note) == REG_INC && REG_P (XEXP (note, 0)))
1414 int regno = REGNO (XEXP (note, 0));
1415 reg_state[regno].store_ruid = reload_combine_ruid;
1416 reg_state[regno].real_store_ruid = reload_combine_ruid;
1417 reg_state[regno].use_index = -1;
1422 free (label_live);
1425 /* Check if DST is a register or a subreg of a register; if it is,
1426 update store_ruid, real_store_ruid and use_index in the reg_state
1427 structure accordingly. Called via note_stores from reload_combine. */
1429 static void
1430 reload_combine_note_store (rtx dst, const_rtx set, void *data ATTRIBUTE_UNUSED)
1432 int regno = 0;
1433 int i;
1434 machine_mode mode = GET_MODE (dst);
1436 if (GET_CODE (dst) == SUBREG)
1438 regno = subreg_regno_offset (REGNO (SUBREG_REG (dst)),
1439 GET_MODE (SUBREG_REG (dst)),
1440 SUBREG_BYTE (dst),
1441 GET_MODE (dst));
1442 dst = SUBREG_REG (dst);
1445 /* Some targets do argument pushes without adding REG_INC notes. */
1447 if (MEM_P (dst))
1449 dst = XEXP (dst, 0);
1450 if (GET_CODE (dst) == PRE_INC || GET_CODE (dst) == POST_INC
1451 || GET_CODE (dst) == PRE_DEC || GET_CODE (dst) == POST_DEC
1452 || GET_CODE (dst) == PRE_MODIFY || GET_CODE (dst) == POST_MODIFY)
1454 regno = REGNO (XEXP (dst, 0));
1455 mode = GET_MODE (XEXP (dst, 0));
1456 for (i = hard_regno_nregs[regno][mode] - 1 + regno; i >= regno; i--)
1458 /* We could probably do better, but for now mark the register
1459 as used in an unknown fashion and set/clobbered at this
1460 insn. */
1461 reg_state[i].use_index = -1;
1462 reg_state[i].store_ruid = reload_combine_ruid;
1463 reg_state[i].real_store_ruid = reload_combine_ruid;
1466 else
1467 return;
1470 if (!REG_P (dst))
1471 return;
1472 regno += REGNO (dst);
1474 /* note_stores might have stripped a STRICT_LOW_PART, so we have to be
1475 careful with registers / register parts that are not full words.
1476 Similarly for ZERO_EXTRACT. */
1477 if (GET_CODE (SET_DEST (set)) == ZERO_EXTRACT
1478 || GET_CODE (SET_DEST (set)) == STRICT_LOW_PART)
1480 for (i = hard_regno_nregs[regno][mode] - 1 + regno; i >= regno; i--)
1482 reg_state[i].use_index = -1;
1483 reg_state[i].store_ruid = reload_combine_ruid;
1484 reg_state[i].real_store_ruid = reload_combine_ruid;
1487 else
1489 for (i = hard_regno_nregs[regno][mode] - 1 + regno; i >= regno; i--)
1491 reg_state[i].store_ruid = reload_combine_ruid;
1492 if (GET_CODE (set) == SET)
1493 reg_state[i].real_store_ruid = reload_combine_ruid;
1494 reg_state[i].use_index = RELOAD_COMBINE_MAX_USES;
1499 /* XP points to a piece of rtl that has to be checked for any uses of
1500 registers.
1501 *XP is the pattern of INSN, or a part of it.
1502 Called from reload_combine, and recursively by itself. */
1503 static void
1504 reload_combine_note_use (rtx *xp, rtx_insn *insn, int ruid, rtx containing_mem)
1506 rtx x = *xp;
1507 enum rtx_code code = x->code;
1508 const char *fmt;
1509 int i, j;
1510 rtx offset = const0_rtx; /* For the REG case below. */
1512 switch (code)
1514 case SET:
1515 if (REG_P (SET_DEST (x)))
1517 reload_combine_note_use (&SET_SRC (x), insn, ruid, NULL_RTX);
1518 return;
1520 break;
1522 case USE:
1523 /* If this is the USE of a return value, we can't change it. */
1524 if (REG_P (XEXP (x, 0)) && REG_FUNCTION_VALUE_P (XEXP (x, 0)))
1526 /* Mark the return register as used in an unknown fashion. */
1527 rtx reg = XEXP (x, 0);
1528 int regno = REGNO (reg);
1529 int nregs = hard_regno_nregs[regno][GET_MODE (reg)];
1531 while (--nregs >= 0)
1532 reg_state[regno + nregs].use_index = -1;
1533 return;
1535 break;
1537 case CLOBBER:
1538 if (REG_P (SET_DEST (x)))
1540 /* No spurious CLOBBERs of pseudo registers may remain. */
1541 gcc_assert (REGNO (SET_DEST (x)) < FIRST_PSEUDO_REGISTER);
1542 return;
1544 break;
1546 case PLUS:
1547 /* We are interested in (plus (reg) (const_int)) . */
1548 if (!REG_P (XEXP (x, 0))
1549 || !CONST_INT_P (XEXP (x, 1)))
1550 break;
1551 offset = XEXP (x, 1);
1552 x = XEXP (x, 0);
1553 /* Fall through. */
1554 case REG:
1556 int regno = REGNO (x);
1557 int use_index;
1558 int nregs;
1560 /* No spurious USEs of pseudo registers may remain. */
1561 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
1563 nregs = hard_regno_nregs[regno][GET_MODE (x)];
1565 /* We can't substitute into multi-hard-reg uses. */
1566 if (nregs > 1)
1568 while (--nregs >= 0)
1569 reg_state[regno + nregs].use_index = -1;
1570 return;
1573 /* We may be called to update uses in previously seen insns.
1574 Don't add uses beyond the last store we saw. */
1575 if (ruid < reg_state[regno].store_ruid)
1576 return;
1578 /* If this register is already used in some unknown fashion, we
1579 can't do anything.
1580 If we decrement the index from zero to -1, we can't store more
1581 uses, so this register becomes used in an unknown fashion. */
1582 use_index = --reg_state[regno].use_index;
1583 if (use_index < 0)
1584 return;
1586 if (use_index == RELOAD_COMBINE_MAX_USES - 1)
1588 /* This is the first use of this register we have seen since we
1589 marked it as dead. */
1590 reg_state[regno].offset = offset;
1591 reg_state[regno].all_offsets_match = true;
1592 reg_state[regno].use_ruid = ruid;
1594 else
1596 if (reg_state[regno].use_ruid > ruid)
1597 reg_state[regno].use_ruid = ruid;
1599 if (! rtx_equal_p (offset, reg_state[regno].offset))
1600 reg_state[regno].all_offsets_match = false;
1603 reg_state[regno].reg_use[use_index].insn = insn;
1604 reg_state[regno].reg_use[use_index].ruid = ruid;
1605 reg_state[regno].reg_use[use_index].containing_mem = containing_mem;
1606 reg_state[regno].reg_use[use_index].usep = xp;
1607 return;
1610 case MEM:
1611 containing_mem = x;
1612 break;
1614 default:
1615 break;
1618 /* Recursively process the components of X. */
1619 fmt = GET_RTX_FORMAT (code);
1620 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1622 if (fmt[i] == 'e')
1623 reload_combine_note_use (&XEXP (x, i), insn, ruid, containing_mem);
1624 else if (fmt[i] == 'E')
1626 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1627 reload_combine_note_use (&XVECEXP (x, i, j), insn, ruid,
1628 containing_mem);
1633 /* See if we can reduce the cost of a constant by replacing a move
1634 with an add. We track situations in which a register is set to a
1635 constant or to a register plus a constant. */
1636 /* We cannot do our optimization across labels. Invalidating all the
1637 information about register contents we have would be costly, so we
1638 use move2add_last_label_luid to note where the label is and then
1639 later disable any optimization that would cross it.
1640 reg_offset[n] / reg_base_reg[n] / reg_symbol_ref[n] / reg_mode[n]
1641 are only valid if reg_set_luid[n] is greater than
1642 move2add_last_label_luid.
1643 For a set that established a new (potential) base register with
1644 non-constant value, we use move2add_luid from the place where the
1645 setting insn is encountered; registers based off that base then
1646 get the same reg_set_luid. Constants all get
1647 move2add_last_label_luid + 1 as their reg_set_luid. */
1648 static int reg_set_luid[FIRST_PSEUDO_REGISTER];
1650 /* If reg_base_reg[n] is negative, register n has been set to
1651 reg_offset[n] or reg_symbol_ref[n] + reg_offset[n] in mode reg_mode[n].
1652 If reg_base_reg[n] is non-negative, register n has been set to the
1653 sum of reg_offset[n] and the value of register reg_base_reg[n]
1654 before reg_set_luid[n], calculated in mode reg_mode[n] .
1655 For multi-hard-register registers, all but the first one are
1656 recorded as BLKmode in reg_mode. Setting reg_mode to VOIDmode
1657 marks it as invalid. */
1658 static HOST_WIDE_INT reg_offset[FIRST_PSEUDO_REGISTER];
1659 static int reg_base_reg[FIRST_PSEUDO_REGISTER];
1660 static rtx reg_symbol_ref[FIRST_PSEUDO_REGISTER];
1661 static machine_mode reg_mode[FIRST_PSEUDO_REGISTER];
1663 /* move2add_luid is linearly increased while scanning the instructions
1664 from first to last. It is used to set reg_set_luid in
1665 reload_cse_move2add and move2add_note_store. */
1666 static int move2add_luid;
1668 /* move2add_last_label_luid is set whenever a label is found. Labels
1669 invalidate all previously collected reg_offset data. */
1670 static int move2add_last_label_luid;
1672 /* ??? We don't know how zero / sign extension is handled, hence we
1673 can't go from a narrower to a wider mode. */
1674 #define MODES_OK_FOR_MOVE2ADD(OUTMODE, INMODE) \
1675 (GET_MODE_SIZE (OUTMODE) == GET_MODE_SIZE (INMODE) \
1676 || (GET_MODE_SIZE (OUTMODE) <= GET_MODE_SIZE (INMODE) \
1677 && TRULY_NOOP_TRUNCATION_MODES_P (OUTMODE, INMODE)))
1679 /* Record that REG is being set to a value with the mode of REG. */
1681 static void
1682 move2add_record_mode (rtx reg)
1684 int regno, nregs;
1685 machine_mode mode = GET_MODE (reg);
1687 if (GET_CODE (reg) == SUBREG)
1689 regno = subreg_regno (reg);
1690 nregs = subreg_nregs (reg);
1692 else if (REG_P (reg))
1694 regno = REGNO (reg);
1695 nregs = hard_regno_nregs[regno][mode];
1697 else
1698 gcc_unreachable ();
1699 for (int i = nregs - 1; i > 0; i--)
1700 reg_mode[regno + i] = BLKmode;
1701 reg_mode[regno] = mode;
1704 /* Record that REG is being set to the sum of SYM and OFF. */
1706 static void
1707 move2add_record_sym_value (rtx reg, rtx sym, rtx off)
1709 int regno = REGNO (reg);
1711 move2add_record_mode (reg);
1712 reg_set_luid[regno] = move2add_luid;
1713 reg_base_reg[regno] = -1;
1714 reg_symbol_ref[regno] = sym;
1715 reg_offset[regno] = INTVAL (off);
1718 /* Check if REGNO contains a valid value in MODE. */
1720 static bool
1721 move2add_valid_value_p (int regno, machine_mode mode)
1723 if (reg_set_luid[regno] <= move2add_last_label_luid)
1724 return false;
1726 if (mode != reg_mode[regno])
1728 if (!MODES_OK_FOR_MOVE2ADD (mode, reg_mode[regno]))
1729 return false;
1730 /* The value loaded into regno in reg_mode[regno] is also valid in
1731 mode after truncation only if (REG:mode regno) is the lowpart of
1732 (REG:reg_mode[regno] regno). Now, for big endian, the starting
1733 regno of the lowpart might be different. */
1734 int s_off = subreg_lowpart_offset (mode, reg_mode[regno]);
1735 s_off = subreg_regno_offset (regno, reg_mode[regno], s_off, mode);
1736 if (s_off != 0)
1737 /* We could in principle adjust regno, check reg_mode[regno] to be
1738 BLKmode, and return s_off to the caller (vs. -1 for failure),
1739 but we currently have no callers that could make use of this
1740 information. */
1741 return false;
1744 for (int i = hard_regno_nregs[regno][mode] - 1; i > 0; i--)
1745 if (reg_mode[regno + i] != BLKmode)
1746 return false;
1747 return true;
1750 /* This function is called with INSN that sets REG to (SYM + OFF),
1751 while REG is known to already have value (SYM + offset).
1752 This function tries to change INSN into an add instruction
1753 (set (REG) (plus (REG) (OFF - offset))) using the known value.
1754 It also updates the information about REG's known value.
1755 Return true if we made a change. */
1757 static bool
1758 move2add_use_add2_insn (rtx reg, rtx sym, rtx off, rtx_insn *insn)
1760 rtx pat = PATTERN (insn);
1761 rtx src = SET_SRC (pat);
1762 int regno = REGNO (reg);
1763 rtx new_src = gen_int_mode (UINTVAL (off) - reg_offset[regno],
1764 GET_MODE (reg));
1765 bool speed = optimize_bb_for_speed_p (BLOCK_FOR_INSN (insn));
1766 bool changed = false;
1768 /* (set (reg) (plus (reg) (const_int 0))) is not canonical;
1769 use (set (reg) (reg)) instead.
1770 We don't delete this insn, nor do we convert it into a
1771 note, to avoid losing register notes or the return
1772 value flag. jump2 already knows how to get rid of
1773 no-op moves. */
1774 if (new_src == const0_rtx)
1776 /* If the constants are different, this is a
1777 truncation, that, if turned into (set (reg)
1778 (reg)), would be discarded. Maybe we should
1779 try a truncMN pattern? */
1780 if (INTVAL (off) == reg_offset [regno])
1781 changed = validate_change (insn, &SET_SRC (pat), reg, 0);
1783 else
1785 struct full_rtx_costs oldcst, newcst;
1786 rtx tem = gen_rtx_PLUS (GET_MODE (reg), reg, new_src);
1788 get_full_set_rtx_cost (pat, &oldcst);
1789 SET_SRC (pat) = tem;
1790 get_full_set_rtx_cost (pat, &newcst);
1791 SET_SRC (pat) = src;
1793 if (costs_lt_p (&newcst, &oldcst, speed)
1794 && have_add2_insn (reg, new_src))
1795 changed = validate_change (insn, &SET_SRC (pat), tem, 0);
1796 else if (sym == NULL_RTX && GET_MODE (reg) != BImode)
1798 machine_mode narrow_mode;
1799 for (narrow_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
1800 narrow_mode != VOIDmode
1801 && narrow_mode != GET_MODE (reg);
1802 narrow_mode = GET_MODE_WIDER_MODE (narrow_mode))
1804 if (have_insn_for (STRICT_LOW_PART, narrow_mode)
1805 && ((reg_offset[regno] & ~GET_MODE_MASK (narrow_mode))
1806 == (INTVAL (off) & ~GET_MODE_MASK (narrow_mode))))
1808 rtx narrow_reg = gen_lowpart_common (narrow_mode, reg);
1809 rtx narrow_src = gen_int_mode (INTVAL (off),
1810 narrow_mode);
1811 rtx new_set
1812 = gen_rtx_SET (VOIDmode,
1813 gen_rtx_STRICT_LOW_PART (VOIDmode,
1814 narrow_reg),
1815 narrow_src);
1816 get_full_set_rtx_cost (new_set, &newcst);
1817 if (costs_lt_p (&newcst, &oldcst, speed))
1819 changed = validate_change (insn, &PATTERN (insn),
1820 new_set, 0);
1821 if (changed)
1822 break;
1828 move2add_record_sym_value (reg, sym, off);
1829 return changed;
1833 /* This function is called with INSN that sets REG to (SYM + OFF),
1834 but REG doesn't have known value (SYM + offset). This function
1835 tries to find another register which is known to already have
1836 value (SYM + offset) and change INSN into an add instruction
1837 (set (REG) (plus (the found register) (OFF - offset))) if such
1838 a register is found. It also updates the information about
1839 REG's known value.
1840 Return true iff we made a change. */
1842 static bool
1843 move2add_use_add3_insn (rtx reg, rtx sym, rtx off, rtx_insn *insn)
1845 rtx pat = PATTERN (insn);
1846 rtx src = SET_SRC (pat);
1847 int regno = REGNO (reg);
1848 int min_regno = 0;
1849 bool speed = optimize_bb_for_speed_p (BLOCK_FOR_INSN (insn));
1850 int i;
1851 bool changed = false;
1852 struct full_rtx_costs oldcst, newcst, mincst;
1853 rtx plus_expr;
1855 init_costs_to_max (&mincst);
1856 get_full_set_rtx_cost (pat, &oldcst);
1858 plus_expr = gen_rtx_PLUS (GET_MODE (reg), reg, const0_rtx);
1859 SET_SRC (pat) = plus_expr;
1861 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1862 if (move2add_valid_value_p (i, GET_MODE (reg))
1863 && reg_base_reg[i] < 0
1864 && reg_symbol_ref[i] != NULL_RTX
1865 && rtx_equal_p (sym, reg_symbol_ref[i]))
1867 rtx new_src = gen_int_mode (UINTVAL (off) - reg_offset[i],
1868 GET_MODE (reg));
1869 /* (set (reg) (plus (reg) (const_int 0))) is not canonical;
1870 use (set (reg) (reg)) instead.
1871 We don't delete this insn, nor do we convert it into a
1872 note, to avoid losing register notes or the return
1873 value flag. jump2 already knows how to get rid of
1874 no-op moves. */
1875 if (new_src == const0_rtx)
1877 init_costs_to_zero (&mincst);
1878 min_regno = i;
1879 break;
1881 else
1883 XEXP (plus_expr, 1) = new_src;
1884 get_full_set_rtx_cost (pat, &newcst);
1886 if (costs_lt_p (&newcst, &mincst, speed))
1888 mincst = newcst;
1889 min_regno = i;
1893 SET_SRC (pat) = src;
1895 if (costs_lt_p (&mincst, &oldcst, speed))
1897 rtx tem;
1899 tem = gen_rtx_REG (GET_MODE (reg), min_regno);
1900 if (i != min_regno)
1902 rtx new_src = gen_int_mode (UINTVAL (off) - reg_offset[min_regno],
1903 GET_MODE (reg));
1904 tem = gen_rtx_PLUS (GET_MODE (reg), tem, new_src);
1906 if (validate_change (insn, &SET_SRC (pat), tem, 0))
1907 changed = true;
1909 reg_set_luid[regno] = move2add_luid;
1910 move2add_record_sym_value (reg, sym, off);
1911 return changed;
1914 /* Convert move insns with constant inputs to additions if they are cheaper.
1915 Return true if any changes were made. */
1916 static bool
1917 reload_cse_move2add (rtx_insn *first)
1919 int i;
1920 rtx_insn *insn;
1921 bool changed = false;
1923 for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; i--)
1925 reg_set_luid[i] = 0;
1926 reg_offset[i] = 0;
1927 reg_base_reg[i] = 0;
1928 reg_symbol_ref[i] = NULL_RTX;
1929 reg_mode[i] = VOIDmode;
1932 move2add_last_label_luid = 0;
1933 move2add_luid = 2;
1934 for (insn = first; insn; insn = NEXT_INSN (insn), move2add_luid++)
1936 rtx pat, note;
1938 if (LABEL_P (insn))
1940 move2add_last_label_luid = move2add_luid;
1941 /* We're going to increment move2add_luid twice after a
1942 label, so that we can use move2add_last_label_luid + 1 as
1943 the luid for constants. */
1944 move2add_luid++;
1945 continue;
1947 if (! INSN_P (insn))
1948 continue;
1949 pat = PATTERN (insn);
1950 /* For simplicity, we only perform this optimization on
1951 straightforward SETs. */
1952 if (GET_CODE (pat) == SET
1953 && REG_P (SET_DEST (pat)))
1955 rtx reg = SET_DEST (pat);
1956 int regno = REGNO (reg);
1957 rtx src = SET_SRC (pat);
1959 /* Check if we have valid information on the contents of this
1960 register in the mode of REG. */
1961 if (move2add_valid_value_p (regno, GET_MODE (reg))
1962 && dbg_cnt (cse2_move2add))
1964 /* Try to transform (set (REGX) (CONST_INT A))
1966 (set (REGX) (CONST_INT B))
1968 (set (REGX) (CONST_INT A))
1970 (set (REGX) (plus (REGX) (CONST_INT B-A)))
1972 (set (REGX) (CONST_INT A))
1974 (set (STRICT_LOW_PART (REGX)) (CONST_INT B))
1977 if (CONST_INT_P (src)
1978 && reg_base_reg[regno] < 0
1979 && reg_symbol_ref[regno] == NULL_RTX)
1981 changed |= move2add_use_add2_insn (reg, NULL_RTX, src, insn);
1982 continue;
1985 /* Try to transform (set (REGX) (REGY))
1986 (set (REGX) (PLUS (REGX) (CONST_INT A)))
1988 (set (REGX) (REGY))
1989 (set (REGX) (PLUS (REGX) (CONST_INT B)))
1991 (set (REGX) (REGY))
1992 (set (REGX) (PLUS (REGX) (CONST_INT A)))
1994 (set (REGX) (plus (REGX) (CONST_INT B-A))) */
1995 else if (REG_P (src)
1996 && reg_set_luid[regno] == reg_set_luid[REGNO (src)]
1997 && reg_base_reg[regno] == reg_base_reg[REGNO (src)]
1998 && move2add_valid_value_p (REGNO (src), GET_MODE (reg)))
2000 rtx_insn *next = next_nonnote_nondebug_insn (insn);
2001 rtx set = NULL_RTX;
2002 if (next)
2003 set = single_set (next);
2004 if (set
2005 && SET_DEST (set) == reg
2006 && GET_CODE (SET_SRC (set)) == PLUS
2007 && XEXP (SET_SRC (set), 0) == reg
2008 && CONST_INT_P (XEXP (SET_SRC (set), 1)))
2010 rtx src3 = XEXP (SET_SRC (set), 1);
2011 unsigned HOST_WIDE_INT added_offset = UINTVAL (src3);
2012 HOST_WIDE_INT base_offset = reg_offset[REGNO (src)];
2013 HOST_WIDE_INT regno_offset = reg_offset[regno];
2014 rtx new_src =
2015 gen_int_mode (added_offset
2016 + base_offset
2017 - regno_offset,
2018 GET_MODE (reg));
2019 bool success = false;
2020 bool speed = optimize_bb_for_speed_p (BLOCK_FOR_INSN (insn));
2022 if (new_src == const0_rtx)
2023 /* See above why we create (set (reg) (reg)) here. */
2024 success
2025 = validate_change (next, &SET_SRC (set), reg, 0);
2026 else
2028 rtx old_src = SET_SRC (set);
2029 struct full_rtx_costs oldcst, newcst;
2030 rtx tem = gen_rtx_PLUS (GET_MODE (reg), reg, new_src);
2032 get_full_set_rtx_cost (set, &oldcst);
2033 SET_SRC (set) = tem;
2034 get_full_set_src_cost (tem, &newcst);
2035 SET_SRC (set) = old_src;
2036 costs_add_n_insns (&oldcst, 1);
2038 if (costs_lt_p (&newcst, &oldcst, speed)
2039 && have_add2_insn (reg, new_src))
2041 rtx newpat = gen_rtx_SET (VOIDmode, reg, tem);
2042 success
2043 = validate_change (next, &PATTERN (next),
2044 newpat, 0);
2047 if (success)
2048 delete_insn (insn);
2049 changed |= success;
2050 insn = next;
2051 move2add_record_mode (reg);
2052 reg_offset[regno]
2053 = trunc_int_for_mode (added_offset + base_offset,
2054 GET_MODE (reg));
2055 continue;
2060 /* Try to transform
2061 (set (REGX) (CONST (PLUS (SYMBOL_REF) (CONST_INT A))))
2063 (set (REGY) (CONST (PLUS (SYMBOL_REF) (CONST_INT B))))
2065 (set (REGX) (CONST (PLUS (SYMBOL_REF) (CONST_INT A))))
2067 (set (REGY) (CONST (PLUS (REGX) (CONST_INT B-A)))) */
2068 if ((GET_CODE (src) == SYMBOL_REF
2069 || (GET_CODE (src) == CONST
2070 && GET_CODE (XEXP (src, 0)) == PLUS
2071 && GET_CODE (XEXP (XEXP (src, 0), 0)) == SYMBOL_REF
2072 && CONST_INT_P (XEXP (XEXP (src, 0), 1))))
2073 && dbg_cnt (cse2_move2add))
2075 rtx sym, off;
2077 if (GET_CODE (src) == SYMBOL_REF)
2079 sym = src;
2080 off = const0_rtx;
2082 else
2084 sym = XEXP (XEXP (src, 0), 0);
2085 off = XEXP (XEXP (src, 0), 1);
2088 /* If the reg already contains the value which is sum of
2089 sym and some constant value, we can use an add2 insn. */
2090 if (move2add_valid_value_p (regno, GET_MODE (reg))
2091 && reg_base_reg[regno] < 0
2092 && reg_symbol_ref[regno] != NULL_RTX
2093 && rtx_equal_p (sym, reg_symbol_ref[regno]))
2094 changed |= move2add_use_add2_insn (reg, sym, off, insn);
2096 /* Otherwise, we have to find a register whose value is sum
2097 of sym and some constant value. */
2098 else
2099 changed |= move2add_use_add3_insn (reg, sym, off, insn);
2101 continue;
2105 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2107 if (REG_NOTE_KIND (note) == REG_INC
2108 && REG_P (XEXP (note, 0)))
2110 /* Reset the information about this register. */
2111 int regno = REGNO (XEXP (note, 0));
2112 if (regno < FIRST_PSEUDO_REGISTER)
2114 move2add_record_mode (XEXP (note, 0));
2115 reg_mode[regno] = VOIDmode;
2119 note_stores (PATTERN (insn), move2add_note_store, insn);
2121 /* If INSN is a conditional branch, we try to extract an
2122 implicit set out of it. */
2123 if (any_condjump_p (insn))
2125 rtx cnd = fis_get_condition (insn);
2127 if (cnd != NULL_RTX
2128 && GET_CODE (cnd) == NE
2129 && REG_P (XEXP (cnd, 0))
2130 && !reg_set_p (XEXP (cnd, 0), insn)
2131 /* The following two checks, which are also in
2132 move2add_note_store, are intended to reduce the
2133 number of calls to gen_rtx_SET to avoid memory
2134 allocation if possible. */
2135 && SCALAR_INT_MODE_P (GET_MODE (XEXP (cnd, 0)))
2136 && hard_regno_nregs[REGNO (XEXP (cnd, 0))][GET_MODE (XEXP (cnd, 0))] == 1
2137 && CONST_INT_P (XEXP (cnd, 1)))
2139 rtx implicit_set =
2140 gen_rtx_SET (VOIDmode, XEXP (cnd, 0), XEXP (cnd, 1));
2141 move2add_note_store (SET_DEST (implicit_set), implicit_set, insn);
2145 /* If this is a CALL_INSN, all call used registers are stored with
2146 unknown values. */
2147 if (CALL_P (insn))
2149 for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; i--)
2151 if (call_used_regs[i])
2152 /* Reset the information about this register. */
2153 reg_mode[i] = VOIDmode;
2157 return changed;
2160 /* SET is a SET or CLOBBER that sets DST. DATA is the insn which
2161 contains SET.
2162 Update reg_set_luid, reg_offset and reg_base_reg accordingly.
2163 Called from reload_cse_move2add via note_stores. */
2165 static void
2166 move2add_note_store (rtx dst, const_rtx set, void *data)
2168 rtx_insn *insn = (rtx_insn *) data;
2169 unsigned int regno = 0;
2170 machine_mode mode = GET_MODE (dst);
2172 /* Some targets do argument pushes without adding REG_INC notes. */
2174 if (MEM_P (dst))
2176 dst = XEXP (dst, 0);
2177 if (GET_CODE (dst) == PRE_INC || GET_CODE (dst) == POST_INC
2178 || GET_CODE (dst) == PRE_DEC || GET_CODE (dst) == POST_DEC)
2179 reg_mode[REGNO (XEXP (dst, 0))] = VOIDmode;
2180 return;
2183 if (GET_CODE (dst) == SUBREG)
2184 regno = subreg_regno (dst);
2185 else if (REG_P (dst))
2186 regno = REGNO (dst);
2187 else
2188 return;
2190 if (SCALAR_INT_MODE_P (mode)
2191 && GET_CODE (set) == SET)
2193 rtx note, sym = NULL_RTX;
2194 rtx off;
2196 note = find_reg_equal_equiv_note (insn);
2197 if (note && GET_CODE (XEXP (note, 0)) == SYMBOL_REF)
2199 sym = XEXP (note, 0);
2200 off = const0_rtx;
2202 else if (note && GET_CODE (XEXP (note, 0)) == CONST
2203 && GET_CODE (XEXP (XEXP (note, 0), 0)) == PLUS
2204 && GET_CODE (XEXP (XEXP (XEXP (note, 0), 0), 0)) == SYMBOL_REF
2205 && CONST_INT_P (XEXP (XEXP (XEXP (note, 0), 0), 1)))
2207 sym = XEXP (XEXP (XEXP (note, 0), 0), 0);
2208 off = XEXP (XEXP (XEXP (note, 0), 0), 1);
2211 if (sym != NULL_RTX)
2213 move2add_record_sym_value (dst, sym, off);
2214 return;
2218 if (SCALAR_INT_MODE_P (mode)
2219 && GET_CODE (set) == SET
2220 && GET_CODE (SET_DEST (set)) != ZERO_EXTRACT
2221 && GET_CODE (SET_DEST (set)) != STRICT_LOW_PART)
2223 rtx src = SET_SRC (set);
2224 rtx base_reg;
2225 unsigned HOST_WIDE_INT offset;
2226 int base_regno;
2228 switch (GET_CODE (src))
2230 case PLUS:
2231 if (REG_P (XEXP (src, 0)))
2233 base_reg = XEXP (src, 0);
2235 if (CONST_INT_P (XEXP (src, 1)))
2236 offset = UINTVAL (XEXP (src, 1));
2237 else if (REG_P (XEXP (src, 1))
2238 && move2add_valid_value_p (REGNO (XEXP (src, 1)), mode))
2240 if (reg_base_reg[REGNO (XEXP (src, 1))] < 0
2241 && reg_symbol_ref[REGNO (XEXP (src, 1))] == NULL_RTX)
2242 offset = reg_offset[REGNO (XEXP (src, 1))];
2243 /* Maybe the first register is known to be a
2244 constant. */
2245 else if (move2add_valid_value_p (REGNO (base_reg), mode)
2246 && reg_base_reg[REGNO (base_reg)] < 0
2247 && reg_symbol_ref[REGNO (base_reg)] == NULL_RTX)
2249 offset = reg_offset[REGNO (base_reg)];
2250 base_reg = XEXP (src, 1);
2252 else
2253 goto invalidate;
2255 else
2256 goto invalidate;
2258 break;
2261 goto invalidate;
2263 case REG:
2264 base_reg = src;
2265 offset = 0;
2266 break;
2268 case CONST_INT:
2269 /* Start tracking the register as a constant. */
2270 reg_base_reg[regno] = -1;
2271 reg_symbol_ref[regno] = NULL_RTX;
2272 reg_offset[regno] = INTVAL (SET_SRC (set));
2273 /* We assign the same luid to all registers set to constants. */
2274 reg_set_luid[regno] = move2add_last_label_luid + 1;
2275 move2add_record_mode (dst);
2276 return;
2278 default:
2279 goto invalidate;
2282 base_regno = REGNO (base_reg);
2283 /* If information about the base register is not valid, set it
2284 up as a new base register, pretending its value is known
2285 starting from the current insn. */
2286 if (!move2add_valid_value_p (base_regno, mode))
2288 reg_base_reg[base_regno] = base_regno;
2289 reg_symbol_ref[base_regno] = NULL_RTX;
2290 reg_offset[base_regno] = 0;
2291 reg_set_luid[base_regno] = move2add_luid;
2292 gcc_assert (GET_MODE (base_reg) == mode);
2293 move2add_record_mode (base_reg);
2296 /* Copy base information from our base register. */
2297 reg_set_luid[regno] = reg_set_luid[base_regno];
2298 reg_base_reg[regno] = reg_base_reg[base_regno];
2299 reg_symbol_ref[regno] = reg_symbol_ref[base_regno];
2301 /* Compute the sum of the offsets or constants. */
2302 reg_offset[regno]
2303 = trunc_int_for_mode (offset + reg_offset[base_regno], mode);
2305 move2add_record_mode (dst);
2307 else
2309 invalidate:
2310 /* Invalidate the contents of the register. */
2311 move2add_record_mode (dst);
2312 reg_mode[regno] = VOIDmode;
2316 namespace {
2318 const pass_data pass_data_postreload_cse =
2320 RTL_PASS, /* type */
2321 "postreload", /* name */
2322 OPTGROUP_NONE, /* optinfo_flags */
2323 TV_RELOAD_CSE_REGS, /* tv_id */
2324 0, /* properties_required */
2325 0, /* properties_provided */
2326 0, /* properties_destroyed */
2327 0, /* todo_flags_start */
2328 TODO_df_finish, /* todo_flags_finish */
2331 class pass_postreload_cse : public rtl_opt_pass
2333 public:
2334 pass_postreload_cse (gcc::context *ctxt)
2335 : rtl_opt_pass (pass_data_postreload_cse, ctxt)
2338 /* opt_pass methods: */
2339 virtual bool gate (function *) { return (optimize > 0 && reload_completed); }
2341 virtual unsigned int execute (function *);
2343 }; // class pass_postreload_cse
2345 unsigned int
2346 pass_postreload_cse::execute (function *fun)
2348 if (!dbg_cnt (postreload_cse))
2349 return 0;
2351 /* Do a very simple CSE pass over just the hard registers. */
2352 reload_cse_regs (get_insns ());
2353 /* Reload_cse_regs can eliminate potentially-trapping MEMs.
2354 Remove any EH edges associated with them. */
2355 if (fun->can_throw_non_call_exceptions
2356 && purge_all_dead_edges ())
2357 cleanup_cfg (0);
2359 return 0;
2362 } // anon namespace
2364 rtl_opt_pass *
2365 make_pass_postreload_cse (gcc::context *ctxt)
2367 return new pass_postreload_cse (ctxt);