* config/i386/predicates.md (general_reg_operand): Use GENERAL_REGNO_P.
[official-gcc.git] / gcc / postreload.c
blob3db2c07224ad9f158b721e7b86084da46a661559
1 /* Perform simple optimizations to clean up the result of reload.
2 Copyright (C) 1987-2015 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 "backend.h"
24 #include "tree.h"
25 #include "rtl.h"
26 #include "df.h"
28 #include "tm_p.h"
29 #include "obstack.h"
30 #include "insn-config.h"
31 #include "flags.h"
32 #include "alias.h"
33 #include "expmed.h"
34 #include "dojump.h"
35 #include "explow.h"
36 #include "calls.h"
37 #include "emit-rtl.h"
38 #include "varasm.h"
39 #include "stmt.h"
40 #include "expr.h"
41 #include "insn-codes.h"
42 #include "optabs.h"
43 #include "regs.h"
44 #include "cfgrtl.h"
45 #include "cfgbuild.h"
46 #include "cfgcleanup.h"
47 #include "reload.h"
48 #include "recog.h"
49 #include "alloc-pool.h"
50 #include "cselib.h"
51 #include "diagnostic-core.h"
52 #include "except.h"
53 #include "target.h"
54 #include "tree-pass.h"
55 #include "dbgcnt.h"
57 static int reload_cse_noop_set_p (rtx);
58 static bool reload_cse_simplify (rtx_insn *, rtx);
59 static void reload_cse_regs_1 (void);
60 static int reload_cse_simplify_set (rtx, rtx_insn *);
61 static int reload_cse_simplify_operands (rtx_insn *, rtx);
63 static void reload_combine (void);
64 static void reload_combine_note_use (rtx *, rtx_insn *, int, rtx);
65 static void reload_combine_note_store (rtx, const_rtx, void *);
67 static bool reload_cse_move2add (rtx_insn *);
68 static void move2add_note_store (rtx, const_rtx, void *);
70 /* Call cse / combine like post-reload optimization phases.
71 FIRST is the first instruction. */
73 static void
74 reload_cse_regs (rtx_insn *first ATTRIBUTE_UNUSED)
76 bool moves_converted;
77 reload_cse_regs_1 ();
78 reload_combine ();
79 moves_converted = reload_cse_move2add (first);
80 if (flag_expensive_optimizations)
82 if (moves_converted)
83 reload_combine ();
84 reload_cse_regs_1 ();
88 /* See whether a single set SET is a noop. */
89 static int
90 reload_cse_noop_set_p (rtx set)
92 if (cselib_reg_set_mode (SET_DEST (set)) != GET_MODE (SET_DEST (set)))
93 return 0;
95 return rtx_equal_for_cselib_p (SET_DEST (set), SET_SRC (set));
98 /* Try to simplify INSN. Return true if the CFG may have changed. */
99 static bool
100 reload_cse_simplify (rtx_insn *insn, rtx testreg)
102 rtx body = PATTERN (insn);
103 basic_block insn_bb = BLOCK_FOR_INSN (insn);
104 unsigned insn_bb_succs = EDGE_COUNT (insn_bb->succs);
106 if (GET_CODE (body) == SET)
108 int count = 0;
110 /* Simplify even if we may think it is a no-op.
111 We may think a memory load of a value smaller than WORD_SIZE
112 is redundant because we haven't taken into account possible
113 implicit extension. reload_cse_simplify_set() will bring
114 this out, so it's safer to simplify before we delete. */
115 count += reload_cse_simplify_set (body, insn);
117 if (!count && reload_cse_noop_set_p (body))
119 rtx value = SET_DEST (body);
120 if (REG_P (value)
121 && ! REG_FUNCTION_VALUE_P (value))
122 value = 0;
123 if (check_for_inc_dec (insn))
124 delete_insn_and_edges (insn);
125 /* We're done with this insn. */
126 goto done;
129 if (count > 0)
130 apply_change_group ();
131 else
132 reload_cse_simplify_operands (insn, testreg);
134 else if (GET_CODE (body) == PARALLEL)
136 int i;
137 int count = 0;
138 rtx value = NULL_RTX;
140 /* Registers mentioned in the clobber list for an asm cannot be reused
141 within the body of the asm. Invalidate those registers now so that
142 we don't try to substitute values for them. */
143 if (asm_noperands (body) >= 0)
145 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
147 rtx part = XVECEXP (body, 0, i);
148 if (GET_CODE (part) == CLOBBER && REG_P (XEXP (part, 0)))
149 cselib_invalidate_rtx (XEXP (part, 0));
153 /* If every action in a PARALLEL is a noop, we can delete
154 the entire PARALLEL. */
155 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
157 rtx part = XVECEXP (body, 0, i);
158 if (GET_CODE (part) == SET)
160 if (! reload_cse_noop_set_p (part))
161 break;
162 if (REG_P (SET_DEST (part))
163 && REG_FUNCTION_VALUE_P (SET_DEST (part)))
165 if (value)
166 break;
167 value = SET_DEST (part);
170 else if (GET_CODE (part) != CLOBBER)
171 break;
174 if (i < 0)
176 if (check_for_inc_dec (insn))
177 delete_insn_and_edges (insn);
178 /* We're done with this insn. */
179 goto done;
182 /* It's not a no-op, but we can try to simplify it. */
183 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
184 if (GET_CODE (XVECEXP (body, 0, i)) == SET)
185 count += reload_cse_simplify_set (XVECEXP (body, 0, i), insn);
187 if (count > 0)
188 apply_change_group ();
189 else
190 reload_cse_simplify_operands (insn, testreg);
193 done:
194 return (EDGE_COUNT (insn_bb->succs) != insn_bb_succs);
197 /* Do a very simple CSE pass over the hard registers.
199 This function detects no-op moves where we happened to assign two
200 different pseudo-registers to the same hard register, and then
201 copied one to the other. Reload will generate a useless
202 instruction copying a register to itself.
204 This function also detects cases where we load a value from memory
205 into two different registers, and (if memory is more expensive than
206 registers) changes it to simply copy the first register into the
207 second register.
209 Another optimization is performed that scans the operands of each
210 instruction to see whether the value is already available in a
211 hard register. It then replaces the operand with the hard register
212 if possible, much like an optional reload would. */
214 static void
215 reload_cse_regs_1 (void)
217 bool cfg_changed = false;
218 basic_block bb;
219 rtx_insn *insn;
220 rtx testreg = gen_rtx_REG (word_mode, LAST_VIRTUAL_REGISTER + 1);
222 cselib_init (CSELIB_RECORD_MEMORY);
223 init_alias_analysis ();
225 FOR_EACH_BB_FN (bb, cfun)
226 FOR_BB_INSNS (bb, insn)
228 if (INSN_P (insn))
229 cfg_changed |= reload_cse_simplify (insn, testreg);
231 cselib_process_insn (insn);
234 /* Clean up. */
235 end_alias_analysis ();
236 cselib_finish ();
237 if (cfg_changed)
238 cleanup_cfg (0);
241 /* Try to simplify a single SET instruction. SET is the set pattern.
242 INSN is the instruction it came from.
243 This function only handles one case: if we set a register to a value
244 which is not a register, we try to find that value in some other register
245 and change the set into a register copy. */
247 static int
248 reload_cse_simplify_set (rtx set, rtx_insn *insn)
250 int did_change = 0;
251 int dreg;
252 rtx src;
253 reg_class_t dclass;
254 int old_cost;
255 cselib_val *val;
256 struct elt_loc_list *l;
257 #ifdef LOAD_EXTEND_OP
258 enum rtx_code extend_op = UNKNOWN;
259 #endif
260 bool speed = optimize_bb_for_speed_p (BLOCK_FOR_INSN (insn));
262 dreg = true_regnum (SET_DEST (set));
263 if (dreg < 0)
264 return 0;
266 src = SET_SRC (set);
267 if (side_effects_p (src) || true_regnum (src) >= 0)
268 return 0;
270 dclass = REGNO_REG_CLASS (dreg);
272 #ifdef LOAD_EXTEND_OP
273 /* When replacing a memory with a register, we need to honor assumptions
274 that combine made wrt the contents of sign bits. We'll do this by
275 generating an extend instruction instead of a reg->reg copy. Thus
276 the destination must be a register that we can widen. */
277 if (MEM_P (src)
278 && GET_MODE_BITSIZE (GET_MODE (src)) < BITS_PER_WORD
279 && (extend_op = LOAD_EXTEND_OP (GET_MODE (src))) != UNKNOWN
280 && !REG_P (SET_DEST (set)))
281 return 0;
282 #endif
284 val = cselib_lookup (src, GET_MODE (SET_DEST (set)), 0, VOIDmode);
285 if (! val)
286 return 0;
288 /* If memory loads are cheaper than register copies, don't change them. */
289 if (MEM_P (src))
290 old_cost = memory_move_cost (GET_MODE (src), dclass, true);
291 else if (REG_P (src))
292 old_cost = register_move_cost (GET_MODE (src),
293 REGNO_REG_CLASS (REGNO (src)), dclass);
294 else
295 old_cost = set_src_cost (src, GET_MODE (SET_DEST (set)), speed);
297 for (l = val->locs; l; l = l->next)
299 rtx this_rtx = l->loc;
300 int this_cost;
302 if (CONSTANT_P (this_rtx) && ! references_value_p (this_rtx, 0))
304 #ifdef LOAD_EXTEND_OP
305 if (extend_op != UNKNOWN)
307 wide_int result;
309 if (!CONST_SCALAR_INT_P (this_rtx))
310 continue;
312 switch (extend_op)
314 case ZERO_EXTEND:
315 result = wide_int::from (std::make_pair (this_rtx,
316 GET_MODE (src)),
317 BITS_PER_WORD, UNSIGNED);
318 break;
319 case SIGN_EXTEND:
320 result = wide_int::from (std::make_pair (this_rtx,
321 GET_MODE (src)),
322 BITS_PER_WORD, SIGNED);
323 break;
324 default:
325 gcc_unreachable ();
327 this_rtx = immed_wide_int_const (result, word_mode);
329 #endif
330 this_cost = set_src_cost (this_rtx, GET_MODE (SET_DEST (set)), speed);
332 else if (REG_P (this_rtx))
334 #ifdef LOAD_EXTEND_OP
335 if (extend_op != UNKNOWN)
337 this_rtx = gen_rtx_fmt_e (extend_op, word_mode, this_rtx);
338 this_cost = set_src_cost (this_rtx, word_mode, speed);
340 else
341 #endif
342 this_cost = register_move_cost (GET_MODE (this_rtx),
343 REGNO_REG_CLASS (REGNO (this_rtx)),
344 dclass);
346 else
347 continue;
349 /* If equal costs, prefer registers over anything else. That
350 tends to lead to smaller instructions on some machines. */
351 if (this_cost < old_cost
352 || (this_cost == old_cost
353 && REG_P (this_rtx)
354 && !REG_P (SET_SRC (set))))
356 #ifdef LOAD_EXTEND_OP
357 if (GET_MODE_BITSIZE (GET_MODE (SET_DEST (set))) < BITS_PER_WORD
358 && extend_op != UNKNOWN
359 #ifdef CANNOT_CHANGE_MODE_CLASS
360 && !CANNOT_CHANGE_MODE_CLASS (GET_MODE (SET_DEST (set)),
361 word_mode,
362 REGNO_REG_CLASS (REGNO (SET_DEST (set))))
363 #endif
366 rtx wide_dest = gen_rtx_REG (word_mode, REGNO (SET_DEST (set)));
367 ORIGINAL_REGNO (wide_dest) = ORIGINAL_REGNO (SET_DEST (set));
368 validate_change (insn, &SET_DEST (set), wide_dest, 1);
370 #endif
372 validate_unshare_change (insn, &SET_SRC (set), this_rtx, 1);
373 old_cost = this_cost, did_change = 1;
377 return did_change;
380 /* Try to replace operands in INSN with equivalent values that are already
381 in registers. This can be viewed as optional reloading.
383 For each non-register operand in the insn, see if any hard regs are
384 known to be equivalent to that operand. Record the alternatives which
385 can accept these hard registers. Among all alternatives, select the
386 ones which are better or equal to the one currently matching, where
387 "better" is in terms of '?' and '!' constraints. Among the remaining
388 alternatives, select the one which replaces most operands with
389 hard registers. */
391 static int
392 reload_cse_simplify_operands (rtx_insn *insn, rtx testreg)
394 int i, j;
396 /* For each operand, all registers that are equivalent to it. */
397 HARD_REG_SET equiv_regs[MAX_RECOG_OPERANDS];
399 const char *constraints[MAX_RECOG_OPERANDS];
401 /* Vector recording how bad an alternative is. */
402 int *alternative_reject;
403 /* Vector recording how many registers can be introduced by choosing
404 this alternative. */
405 int *alternative_nregs;
406 /* Array of vectors recording, for each operand and each alternative,
407 which hard register to substitute, or -1 if the operand should be
408 left as it is. */
409 int *op_alt_regno[MAX_RECOG_OPERANDS];
410 /* Array of alternatives, sorted in order of decreasing desirability. */
411 int *alternative_order;
413 extract_constrain_insn (insn);
415 if (recog_data.n_alternatives == 0 || recog_data.n_operands == 0)
416 return 0;
418 alternative_reject = XALLOCAVEC (int, recog_data.n_alternatives);
419 alternative_nregs = XALLOCAVEC (int, recog_data.n_alternatives);
420 alternative_order = XALLOCAVEC (int, recog_data.n_alternatives);
421 memset (alternative_reject, 0, recog_data.n_alternatives * sizeof (int));
422 memset (alternative_nregs, 0, recog_data.n_alternatives * sizeof (int));
424 /* For each operand, find out which regs are equivalent. */
425 for (i = 0; i < recog_data.n_operands; i++)
427 cselib_val *v;
428 struct elt_loc_list *l;
429 rtx op;
431 CLEAR_HARD_REG_SET (equiv_regs[i]);
433 /* cselib blows up on CODE_LABELs. Trying to fix that doesn't seem
434 right, so avoid the problem here. Likewise if we have a constant
435 and the insn pattern doesn't tell us the mode we need. */
436 if (LABEL_P (recog_data.operand[i])
437 || (CONSTANT_P (recog_data.operand[i])
438 && recog_data.operand_mode[i] == VOIDmode))
439 continue;
441 op = recog_data.operand[i];
442 #ifdef LOAD_EXTEND_OP
443 if (MEM_P (op)
444 && GET_MODE_BITSIZE (GET_MODE (op)) < BITS_PER_WORD
445 && LOAD_EXTEND_OP (GET_MODE (op)) != UNKNOWN)
447 rtx set = single_set (insn);
449 /* We might have multiple sets, some of which do implicit
450 extension. Punt on this for now. */
451 if (! set)
452 continue;
453 /* If the destination is also a MEM or a STRICT_LOW_PART, no
454 extension applies.
455 Also, if there is an explicit extension, we don't have to
456 worry about an implicit one. */
457 else if (MEM_P (SET_DEST (set))
458 || GET_CODE (SET_DEST (set)) == STRICT_LOW_PART
459 || GET_CODE (SET_SRC (set)) == ZERO_EXTEND
460 || GET_CODE (SET_SRC (set)) == SIGN_EXTEND)
461 ; /* Continue ordinary processing. */
462 #ifdef CANNOT_CHANGE_MODE_CLASS
463 /* If the register cannot change mode to word_mode, it follows that
464 it cannot have been used in word_mode. */
465 else if (REG_P (SET_DEST (set))
466 && CANNOT_CHANGE_MODE_CLASS (GET_MODE (SET_DEST (set)),
467 word_mode,
468 REGNO_REG_CLASS (REGNO (SET_DEST (set)))))
469 ; /* Continue ordinary processing. */
470 #endif
471 /* If this is a straight load, make the extension explicit. */
472 else if (REG_P (SET_DEST (set))
473 && recog_data.n_operands == 2
474 && SET_SRC (set) == op
475 && SET_DEST (set) == recog_data.operand[1-i])
477 validate_change (insn, recog_data.operand_loc[i],
478 gen_rtx_fmt_e (LOAD_EXTEND_OP (GET_MODE (op)),
479 word_mode, op),
481 validate_change (insn, recog_data.operand_loc[1-i],
482 gen_rtx_REG (word_mode, REGNO (SET_DEST (set))),
484 if (! apply_change_group ())
485 return 0;
486 return reload_cse_simplify_operands (insn, testreg);
488 else
489 /* ??? There might be arithmetic operations with memory that are
490 safe to optimize, but is it worth the trouble? */
491 continue;
493 #endif /* LOAD_EXTEND_OP */
494 if (side_effects_p (op))
495 continue;
496 v = cselib_lookup (op, recog_data.operand_mode[i], 0, VOIDmode);
497 if (! v)
498 continue;
500 for (l = v->locs; l; l = l->next)
501 if (REG_P (l->loc))
502 SET_HARD_REG_BIT (equiv_regs[i], REGNO (l->loc));
505 alternative_mask preferred = get_preferred_alternatives (insn);
506 for (i = 0; i < recog_data.n_operands; i++)
508 machine_mode mode;
509 int regno;
510 const char *p;
512 op_alt_regno[i] = XALLOCAVEC (int, recog_data.n_alternatives);
513 for (j = 0; j < recog_data.n_alternatives; j++)
514 op_alt_regno[i][j] = -1;
516 p = constraints[i] = recog_data.constraints[i];
517 mode = recog_data.operand_mode[i];
519 /* Add the reject values for each alternative given by the constraints
520 for this operand. */
521 j = 0;
522 while (*p != '\0')
524 char c = *p++;
525 if (c == ',')
526 j++;
527 else if (c == '?')
528 alternative_reject[j] += 3;
529 else if (c == '!')
530 alternative_reject[j] += 300;
533 /* We won't change operands which are already registers. We
534 also don't want to modify output operands. */
535 regno = true_regnum (recog_data.operand[i]);
536 if (regno >= 0
537 || constraints[i][0] == '='
538 || constraints[i][0] == '+')
539 continue;
541 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
543 enum reg_class rclass = NO_REGS;
545 if (! TEST_HARD_REG_BIT (equiv_regs[i], regno))
546 continue;
548 set_mode_and_regno (testreg, mode, regno);
550 /* We found a register equal to this operand. Now look for all
551 alternatives that can accept this register and have not been
552 assigned a register they can use yet. */
553 j = 0;
554 p = constraints[i];
555 for (;;)
557 char c = *p;
559 switch (c)
561 case 'g':
562 rclass = reg_class_subunion[rclass][GENERAL_REGS];
563 break;
565 default:
566 rclass
567 = (reg_class_subunion
568 [rclass]
569 [reg_class_for_constraint (lookup_constraint (p))]);
570 break;
572 case ',': case '\0':
573 /* See if REGNO fits this alternative, and set it up as the
574 replacement register if we don't have one for this
575 alternative yet and the operand being replaced is not
576 a cheap CONST_INT. */
577 if (op_alt_regno[i][j] == -1
578 && TEST_BIT (preferred, j)
579 && reg_fits_class_p (testreg, rclass, 0, mode)
580 && (!CONST_INT_P (recog_data.operand[i])
581 || (set_src_cost (recog_data.operand[i], mode,
582 optimize_bb_for_speed_p
583 (BLOCK_FOR_INSN (insn)))
584 > set_src_cost (testreg, mode,
585 optimize_bb_for_speed_p
586 (BLOCK_FOR_INSN (insn))))))
588 alternative_nregs[j]++;
589 op_alt_regno[i][j] = regno;
591 j++;
592 rclass = NO_REGS;
593 break;
595 p += CONSTRAINT_LEN (c, p);
597 if (c == '\0')
598 break;
603 /* Record all alternatives which are better or equal to the currently
604 matching one in the alternative_order array. */
605 for (i = j = 0; i < recog_data.n_alternatives; i++)
606 if (alternative_reject[i] <= alternative_reject[which_alternative])
607 alternative_order[j++] = i;
608 recog_data.n_alternatives = j;
610 /* Sort it. Given a small number of alternatives, a dumb algorithm
611 won't hurt too much. */
612 for (i = 0; i < recog_data.n_alternatives - 1; i++)
614 int best = i;
615 int best_reject = alternative_reject[alternative_order[i]];
616 int best_nregs = alternative_nregs[alternative_order[i]];
618 for (j = i + 1; j < recog_data.n_alternatives; j++)
620 int this_reject = alternative_reject[alternative_order[j]];
621 int this_nregs = alternative_nregs[alternative_order[j]];
623 if (this_reject < best_reject
624 || (this_reject == best_reject && this_nregs > best_nregs))
626 best = j;
627 best_reject = this_reject;
628 best_nregs = this_nregs;
632 std::swap (alternative_order[best], alternative_order[i]);
635 /* Substitute the operands as determined by op_alt_regno for the best
636 alternative. */
637 j = alternative_order[0];
639 for (i = 0; i < recog_data.n_operands; i++)
641 machine_mode mode = recog_data.operand_mode[i];
642 if (op_alt_regno[i][j] == -1)
643 continue;
645 validate_change (insn, recog_data.operand_loc[i],
646 gen_rtx_REG (mode, op_alt_regno[i][j]), 1);
649 for (i = recog_data.n_dups - 1; i >= 0; i--)
651 int op = recog_data.dup_num[i];
652 machine_mode mode = recog_data.operand_mode[op];
654 if (op_alt_regno[op][j] == -1)
655 continue;
657 validate_change (insn, recog_data.dup_loc[i],
658 gen_rtx_REG (mode, op_alt_regno[op][j]), 1);
661 return apply_change_group ();
664 /* If reload couldn't use reg+reg+offset addressing, try to use reg+reg
665 addressing now.
666 This code might also be useful when reload gave up on reg+reg addressing
667 because of clashes between the return register and INDEX_REG_CLASS. */
669 /* The maximum number of uses of a register we can keep track of to
670 replace them with reg+reg addressing. */
671 #define RELOAD_COMBINE_MAX_USES 16
673 /* Describes a recorded use of a register. */
674 struct reg_use
676 /* The insn where a register has been used. */
677 rtx_insn *insn;
678 /* Points to the memory reference enclosing the use, if any, NULL_RTX
679 otherwise. */
680 rtx containing_mem;
681 /* Location of the register within INSN. */
682 rtx *usep;
683 /* The reverse uid of the insn. */
684 int ruid;
687 /* If the register is used in some unknown fashion, USE_INDEX is negative.
688 If it is dead, USE_INDEX is RELOAD_COMBINE_MAX_USES, and STORE_RUID
689 indicates where it is first set or clobbered.
690 Otherwise, USE_INDEX is the index of the last encountered use of the
691 register (which is first among these we have seen since we scan backwards).
692 USE_RUID indicates the first encountered, i.e. last, of these uses.
693 If ALL_OFFSETS_MATCH is true, all encountered uses were inside a PLUS
694 with a constant offset; OFFSET contains this constant in that case.
695 STORE_RUID is always meaningful if we only want to use a value in a
696 register in a different place: it denotes the next insn in the insn
697 stream (i.e. the last encountered) that sets or clobbers the register.
698 REAL_STORE_RUID is similar, but clobbers are ignored when updating it. */
699 static struct
701 struct reg_use reg_use[RELOAD_COMBINE_MAX_USES];
702 rtx offset;
703 int use_index;
704 int store_ruid;
705 int real_store_ruid;
706 int use_ruid;
707 bool all_offsets_match;
708 } reg_state[FIRST_PSEUDO_REGISTER];
710 /* Reverse linear uid. This is increased in reload_combine while scanning
711 the instructions from last to first. It is used to set last_label_ruid
712 and the store_ruid / use_ruid fields in reg_state. */
713 static int reload_combine_ruid;
715 /* The RUID of the last label we encountered in reload_combine. */
716 static int last_label_ruid;
718 /* The RUID of the last jump we encountered in reload_combine. */
719 static int last_jump_ruid;
721 /* The register numbers of the first and last index register. A value of
722 -1 in LAST_INDEX_REG indicates that we've previously computed these
723 values and found no suitable index registers. */
724 static int first_index_reg = -1;
725 static int last_index_reg;
727 #define LABEL_LIVE(LABEL) \
728 (label_live[CODE_LABEL_NUMBER (LABEL) - min_labelno])
730 /* Subroutine of reload_combine_split_ruids, called to fix up a single
731 ruid pointed to by *PRUID if it is higher than SPLIT_RUID. */
733 static inline void
734 reload_combine_split_one_ruid (int *pruid, int split_ruid)
736 if (*pruid > split_ruid)
737 (*pruid)++;
740 /* Called when we insert a new insn in a position we've already passed in
741 the scan. Examine all our state, increasing all ruids that are higher
742 than SPLIT_RUID by one in order to make room for a new insn. */
744 static void
745 reload_combine_split_ruids (int split_ruid)
747 unsigned i;
749 reload_combine_split_one_ruid (&reload_combine_ruid, split_ruid);
750 reload_combine_split_one_ruid (&last_label_ruid, split_ruid);
751 reload_combine_split_one_ruid (&last_jump_ruid, split_ruid);
753 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
755 int j, idx = reg_state[i].use_index;
756 reload_combine_split_one_ruid (&reg_state[i].use_ruid, split_ruid);
757 reload_combine_split_one_ruid (&reg_state[i].store_ruid, split_ruid);
758 reload_combine_split_one_ruid (&reg_state[i].real_store_ruid,
759 split_ruid);
760 if (idx < 0)
761 continue;
762 for (j = idx; j < RELOAD_COMBINE_MAX_USES; j++)
764 reload_combine_split_one_ruid (&reg_state[i].reg_use[j].ruid,
765 split_ruid);
770 /* Called when we are about to rescan a previously encountered insn with
771 reload_combine_note_use after modifying some part of it. This clears all
772 information about uses in that particular insn. */
774 static void
775 reload_combine_purge_insn_uses (rtx_insn *insn)
777 unsigned i;
779 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
781 int j, k, idx = reg_state[i].use_index;
782 if (idx < 0)
783 continue;
784 j = k = RELOAD_COMBINE_MAX_USES;
785 while (j-- > idx)
787 if (reg_state[i].reg_use[j].insn != insn)
789 k--;
790 if (k != j)
791 reg_state[i].reg_use[k] = reg_state[i].reg_use[j];
794 reg_state[i].use_index = k;
798 /* Called when we need to forget about all uses of REGNO after an insn
799 which is identified by RUID. */
801 static void
802 reload_combine_purge_reg_uses_after_ruid (unsigned regno, int ruid)
804 int j, k, idx = reg_state[regno].use_index;
805 if (idx < 0)
806 return;
807 j = k = RELOAD_COMBINE_MAX_USES;
808 while (j-- > idx)
810 if (reg_state[regno].reg_use[j].ruid >= ruid)
812 k--;
813 if (k != j)
814 reg_state[regno].reg_use[k] = reg_state[regno].reg_use[j];
817 reg_state[regno].use_index = k;
820 /* Find the use of REGNO with the ruid that is highest among those
821 lower than RUID_LIMIT, and return it if it is the only use of this
822 reg in the insn. Return NULL otherwise. */
824 static struct reg_use *
825 reload_combine_closest_single_use (unsigned regno, int ruid_limit)
827 int i, best_ruid = 0;
828 int use_idx = reg_state[regno].use_index;
829 struct reg_use *retval;
831 if (use_idx < 0)
832 return NULL;
833 retval = NULL;
834 for (i = use_idx; i < RELOAD_COMBINE_MAX_USES; i++)
836 struct reg_use *use = reg_state[regno].reg_use + i;
837 int this_ruid = use->ruid;
838 if (this_ruid >= ruid_limit)
839 continue;
840 if (this_ruid > best_ruid)
842 best_ruid = this_ruid;
843 retval = use;
845 else if (this_ruid == best_ruid)
846 retval = NULL;
848 if (last_label_ruid >= best_ruid)
849 return NULL;
850 return retval;
853 /* After we've moved an add insn, fix up any debug insns that occur
854 between the old location of the add and the new location. REG is
855 the destination register of the add insn; REPLACEMENT is the
856 SET_SRC of the add. FROM and TO specify the range in which we
857 should make this change on debug insns. */
859 static void
860 fixup_debug_insns (rtx reg, rtx replacement, rtx_insn *from, rtx_insn *to)
862 rtx_insn *insn;
863 for (insn = from; insn != to; insn = NEXT_INSN (insn))
865 rtx t;
867 if (!DEBUG_INSN_P (insn))
868 continue;
870 t = INSN_VAR_LOCATION_LOC (insn);
871 t = simplify_replace_rtx (t, reg, replacement);
872 validate_change (insn, &INSN_VAR_LOCATION_LOC (insn), t, 0);
876 /* Subroutine of reload_combine_recognize_const_pattern. Try to replace REG
877 with SRC in the insn described by USE, taking costs into account. Return
878 true if we made the replacement. */
880 static bool
881 try_replace_in_use (struct reg_use *use, rtx reg, rtx src)
883 rtx_insn *use_insn = use->insn;
884 rtx mem = use->containing_mem;
885 bool speed = optimize_bb_for_speed_p (BLOCK_FOR_INSN (use_insn));
887 if (mem != NULL_RTX)
889 addr_space_t as = MEM_ADDR_SPACE (mem);
890 rtx oldaddr = XEXP (mem, 0);
891 rtx newaddr = NULL_RTX;
892 int old_cost = address_cost (oldaddr, GET_MODE (mem), as, speed);
893 int new_cost;
895 newaddr = simplify_replace_rtx (oldaddr, reg, src);
896 if (memory_address_addr_space_p (GET_MODE (mem), newaddr, as))
898 XEXP (mem, 0) = newaddr;
899 new_cost = address_cost (newaddr, GET_MODE (mem), as, speed);
900 XEXP (mem, 0) = oldaddr;
901 if (new_cost <= old_cost
902 && validate_change (use_insn,
903 &XEXP (mem, 0), newaddr, 0))
904 return true;
907 else
909 rtx new_set = single_set (use_insn);
910 if (new_set
911 && REG_P (SET_DEST (new_set))
912 && GET_CODE (SET_SRC (new_set)) == PLUS
913 && REG_P (XEXP (SET_SRC (new_set), 0))
914 && CONSTANT_P (XEXP (SET_SRC (new_set), 1)))
916 rtx new_src;
917 machine_mode mode = GET_MODE (SET_DEST (new_set));
918 int old_cost = set_src_cost (SET_SRC (new_set), mode, speed);
920 gcc_assert (rtx_equal_p (XEXP (SET_SRC (new_set), 0), reg));
921 new_src = simplify_replace_rtx (SET_SRC (new_set), reg, src);
923 if (set_src_cost (new_src, mode, speed) <= old_cost
924 && validate_change (use_insn, &SET_SRC (new_set),
925 new_src, 0))
926 return true;
929 return false;
932 /* Called by reload_combine when scanning INSN. This function tries to detect
933 patterns where a constant is added to a register, and the result is used
934 in an address.
935 Return true if no further processing is needed on INSN; false if it wasn't
936 recognized and should be handled normally. */
938 static bool
939 reload_combine_recognize_const_pattern (rtx_insn *insn)
941 int from_ruid = reload_combine_ruid;
942 rtx set, pat, reg, src, addreg;
943 unsigned int regno;
944 struct reg_use *use;
945 bool must_move_add;
946 rtx_insn *add_moved_after_insn = NULL;
947 int add_moved_after_ruid = 0;
948 int clobbered_regno = -1;
950 set = single_set (insn);
951 if (set == NULL_RTX)
952 return false;
954 reg = SET_DEST (set);
955 src = SET_SRC (set);
956 if (!REG_P (reg)
957 || REG_NREGS (reg) != 1
958 || GET_MODE (reg) != Pmode
959 || reg == stack_pointer_rtx)
960 return false;
962 regno = REGNO (reg);
964 /* We look for a REG1 = REG2 + CONSTANT insn, followed by either
965 uses of REG1 inside an address, or inside another add insn. If
966 possible and profitable, merge the addition into subsequent
967 uses. */
968 if (GET_CODE (src) != PLUS
969 || !REG_P (XEXP (src, 0))
970 || !CONSTANT_P (XEXP (src, 1)))
971 return false;
973 addreg = XEXP (src, 0);
974 must_move_add = rtx_equal_p (reg, addreg);
976 pat = PATTERN (insn);
977 if (must_move_add && set != pat)
979 /* We have to be careful when moving the add; apart from the
980 single_set there may also be clobbers. Recognize one special
981 case, that of one clobber alongside the set (likely a clobber
982 of the CC register). */
983 gcc_assert (GET_CODE (PATTERN (insn)) == PARALLEL);
984 if (XVECLEN (pat, 0) != 2 || XVECEXP (pat, 0, 0) != set
985 || GET_CODE (XVECEXP (pat, 0, 1)) != CLOBBER
986 || !REG_P (XEXP (XVECEXP (pat, 0, 1), 0)))
987 return false;
988 clobbered_regno = REGNO (XEXP (XVECEXP (pat, 0, 1), 0));
993 use = reload_combine_closest_single_use (regno, from_ruid);
995 if (use)
996 /* Start the search for the next use from here. */
997 from_ruid = use->ruid;
999 if (use && GET_MODE (*use->usep) == Pmode)
1001 bool delete_add = false;
1002 rtx_insn *use_insn = use->insn;
1003 int use_ruid = use->ruid;
1005 /* Avoid moving the add insn past a jump. */
1006 if (must_move_add && use_ruid <= last_jump_ruid)
1007 break;
1009 /* If the add clobbers another hard reg in parallel, don't move
1010 it past a real set of this hard reg. */
1011 if (must_move_add && clobbered_regno >= 0
1012 && reg_state[clobbered_regno].real_store_ruid >= use_ruid)
1013 break;
1015 /* Do not separate cc0 setter and cc0 user on HAVE_cc0 targets. */
1016 if (HAVE_cc0 && must_move_add && sets_cc0_p (PATTERN (use_insn)))
1017 break;
1019 gcc_assert (reg_state[regno].store_ruid <= use_ruid);
1020 /* Avoid moving a use of ADDREG past a point where it is stored. */
1021 if (reg_state[REGNO (addreg)].store_ruid > use_ruid)
1022 break;
1024 /* We also must not move the addition past an insn that sets
1025 the same register, unless we can combine two add insns. */
1026 if (must_move_add && reg_state[regno].store_ruid == use_ruid)
1028 if (use->containing_mem == NULL_RTX)
1029 delete_add = true;
1030 else
1031 break;
1034 if (try_replace_in_use (use, reg, src))
1036 reload_combine_purge_insn_uses (use_insn);
1037 reload_combine_note_use (&PATTERN (use_insn), use_insn,
1038 use_ruid, NULL_RTX);
1040 if (delete_add)
1042 fixup_debug_insns (reg, src, insn, use_insn);
1043 delete_insn (insn);
1044 return true;
1046 if (must_move_add)
1048 add_moved_after_insn = use_insn;
1049 add_moved_after_ruid = use_ruid;
1051 continue;
1054 /* If we get here, we couldn't handle this use. */
1055 if (must_move_add)
1056 break;
1058 while (use);
1060 if (!must_move_add || add_moved_after_insn == NULL_RTX)
1061 /* Process the add normally. */
1062 return false;
1064 fixup_debug_insns (reg, src, insn, add_moved_after_insn);
1066 reorder_insns (insn, insn, add_moved_after_insn);
1067 reload_combine_purge_reg_uses_after_ruid (regno, add_moved_after_ruid);
1068 reload_combine_split_ruids (add_moved_after_ruid - 1);
1069 reload_combine_note_use (&PATTERN (insn), insn,
1070 add_moved_after_ruid, NULL_RTX);
1071 reg_state[regno].store_ruid = add_moved_after_ruid;
1073 return true;
1076 /* Called by reload_combine when scanning INSN. Try to detect a pattern we
1077 can handle and improve. Return true if no further processing is needed on
1078 INSN; false if it wasn't recognized and should be handled normally. */
1080 static bool
1081 reload_combine_recognize_pattern (rtx_insn *insn)
1083 rtx set, reg, src;
1084 unsigned int regno;
1086 set = single_set (insn);
1087 if (set == NULL_RTX)
1088 return false;
1090 reg = SET_DEST (set);
1091 src = SET_SRC (set);
1092 if (!REG_P (reg) || REG_NREGS (reg) != 1)
1093 return false;
1095 regno = REGNO (reg);
1097 /* Look for (set (REGX) (CONST_INT))
1098 (set (REGX) (PLUS (REGX) (REGY)))
1100 ... (MEM (REGX)) ...
1101 and convert it to
1102 (set (REGZ) (CONST_INT))
1104 ... (MEM (PLUS (REGZ) (REGY)))... .
1106 First, check that we have (set (REGX) (PLUS (REGX) (REGY)))
1107 and that we know all uses of REGX before it dies.
1108 Also, explicitly check that REGX != REGY; our life information
1109 does not yet show whether REGY changes in this insn. */
1111 if (GET_CODE (src) == PLUS
1112 && reg_state[regno].all_offsets_match
1113 && last_index_reg != -1
1114 && REG_P (XEXP (src, 1))
1115 && rtx_equal_p (XEXP (src, 0), reg)
1116 && !rtx_equal_p (XEXP (src, 1), reg)
1117 && reg_state[regno].use_index >= 0
1118 && reg_state[regno].use_index < RELOAD_COMBINE_MAX_USES
1119 && last_label_ruid < reg_state[regno].use_ruid)
1121 rtx base = XEXP (src, 1);
1122 rtx_insn *prev = prev_nonnote_nondebug_insn (insn);
1123 rtx prev_set = prev ? single_set (prev) : NULL_RTX;
1124 rtx index_reg = NULL_RTX;
1125 rtx reg_sum = NULL_RTX;
1126 int i;
1128 /* Now we need to set INDEX_REG to an index register (denoted as
1129 REGZ in the illustration above) and REG_SUM to the expression
1130 register+register that we want to use to substitute uses of REG
1131 (typically in MEMs) with. First check REG and BASE for being
1132 index registers; we can use them even if they are not dead. */
1133 if (TEST_HARD_REG_BIT (reg_class_contents[INDEX_REG_CLASS], regno)
1134 || TEST_HARD_REG_BIT (reg_class_contents[INDEX_REG_CLASS],
1135 REGNO (base)))
1137 index_reg = reg;
1138 reg_sum = src;
1140 else
1142 /* Otherwise, look for a free index register. Since we have
1143 checked above that neither REG nor BASE are index registers,
1144 if we find anything at all, it will be different from these
1145 two registers. */
1146 for (i = first_index_reg; i <= last_index_reg; i++)
1148 if (TEST_HARD_REG_BIT (reg_class_contents[INDEX_REG_CLASS], i)
1149 && reg_state[i].use_index == RELOAD_COMBINE_MAX_USES
1150 && reg_state[i].store_ruid <= reg_state[regno].use_ruid
1151 && (call_used_regs[i] || df_regs_ever_live_p (i))
1152 && (!frame_pointer_needed || i != HARD_FRAME_POINTER_REGNUM)
1153 && !fixed_regs[i] && !global_regs[i]
1154 && hard_regno_nregs[i][GET_MODE (reg)] == 1
1155 && targetm.hard_regno_scratch_ok (i))
1157 index_reg = gen_rtx_REG (GET_MODE (reg), i);
1158 reg_sum = gen_rtx_PLUS (GET_MODE (reg), index_reg, base);
1159 break;
1164 /* Check that PREV_SET is indeed (set (REGX) (CONST_INT)) and that
1165 (REGY), i.e. BASE, is not clobbered before the last use we'll
1166 create. */
1167 if (reg_sum
1168 && prev_set
1169 && CONST_INT_P (SET_SRC (prev_set))
1170 && rtx_equal_p (SET_DEST (prev_set), reg)
1171 && (reg_state[REGNO (base)].store_ruid
1172 <= reg_state[regno].use_ruid))
1174 /* Change destination register and, if necessary, the constant
1175 value in PREV, the constant loading instruction. */
1176 validate_change (prev, &SET_DEST (prev_set), index_reg, 1);
1177 if (reg_state[regno].offset != const0_rtx)
1178 validate_change (prev,
1179 &SET_SRC (prev_set),
1180 GEN_INT (INTVAL (SET_SRC (prev_set))
1181 + INTVAL (reg_state[regno].offset)),
1184 /* Now for every use of REG that we have recorded, replace REG
1185 with REG_SUM. */
1186 for (i = reg_state[regno].use_index;
1187 i < RELOAD_COMBINE_MAX_USES; i++)
1188 validate_unshare_change (reg_state[regno].reg_use[i].insn,
1189 reg_state[regno].reg_use[i].usep,
1190 /* Each change must have its own
1191 replacement. */
1192 reg_sum, 1);
1194 if (apply_change_group ())
1196 struct reg_use *lowest_ruid = NULL;
1198 /* For every new use of REG_SUM, we have to record the use
1199 of BASE therein, i.e. operand 1. */
1200 for (i = reg_state[regno].use_index;
1201 i < RELOAD_COMBINE_MAX_USES; i++)
1203 struct reg_use *use = reg_state[regno].reg_use + i;
1204 reload_combine_note_use (&XEXP (*use->usep, 1), use->insn,
1205 use->ruid, use->containing_mem);
1206 if (lowest_ruid == NULL || use->ruid < lowest_ruid->ruid)
1207 lowest_ruid = use;
1210 fixup_debug_insns (reg, reg_sum, insn, lowest_ruid->insn);
1212 /* Delete the reg-reg addition. */
1213 delete_insn (insn);
1215 if (reg_state[regno].offset != const0_rtx)
1216 /* Previous REG_EQUIV / REG_EQUAL notes for PREV
1217 are now invalid. */
1218 remove_reg_equal_equiv_notes (prev);
1220 reg_state[regno].use_index = RELOAD_COMBINE_MAX_USES;
1221 return true;
1225 return false;
1228 static void
1229 reload_combine (void)
1231 rtx_insn *insn, *prev;
1232 basic_block bb;
1233 unsigned int r;
1234 int min_labelno, n_labels;
1235 HARD_REG_SET ever_live_at_start, *label_live;
1237 /* To avoid wasting too much time later searching for an index register,
1238 determine the minimum and maximum index register numbers. */
1239 if (INDEX_REG_CLASS == NO_REGS)
1240 last_index_reg = -1;
1241 else if (first_index_reg == -1 && last_index_reg == 0)
1243 for (r = 0; r < FIRST_PSEUDO_REGISTER; r++)
1244 if (TEST_HARD_REG_BIT (reg_class_contents[INDEX_REG_CLASS], r))
1246 if (first_index_reg == -1)
1247 first_index_reg = r;
1249 last_index_reg = r;
1252 /* If no index register is available, we can quit now. Set LAST_INDEX_REG
1253 to -1 so we'll know to quit early the next time we get here. */
1254 if (first_index_reg == -1)
1256 last_index_reg = -1;
1257 return;
1261 /* Set up LABEL_LIVE and EVER_LIVE_AT_START. The register lifetime
1262 information is a bit fuzzy immediately after reload, but it's
1263 still good enough to determine which registers are live at a jump
1264 destination. */
1265 min_labelno = get_first_label_num ();
1266 n_labels = max_label_num () - min_labelno;
1267 label_live = XNEWVEC (HARD_REG_SET, n_labels);
1268 CLEAR_HARD_REG_SET (ever_live_at_start);
1270 FOR_EACH_BB_REVERSE_FN (bb, cfun)
1272 insn = BB_HEAD (bb);
1273 if (LABEL_P (insn))
1275 HARD_REG_SET live;
1276 bitmap live_in = df_get_live_in (bb);
1278 REG_SET_TO_HARD_REG_SET (live, live_in);
1279 compute_use_by_pseudos (&live, live_in);
1280 COPY_HARD_REG_SET (LABEL_LIVE (insn), live);
1281 IOR_HARD_REG_SET (ever_live_at_start, live);
1285 /* Initialize last_label_ruid, reload_combine_ruid and reg_state. */
1286 last_label_ruid = last_jump_ruid = reload_combine_ruid = 0;
1287 for (r = 0; r < FIRST_PSEUDO_REGISTER; r++)
1289 reg_state[r].store_ruid = 0;
1290 reg_state[r].real_store_ruid = 0;
1291 if (fixed_regs[r])
1292 reg_state[r].use_index = -1;
1293 else
1294 reg_state[r].use_index = RELOAD_COMBINE_MAX_USES;
1297 for (insn = get_last_insn (); insn; insn = prev)
1299 bool control_flow_insn;
1300 rtx note;
1302 prev = PREV_INSN (insn);
1304 /* We cannot do our optimization across labels. Invalidating all the use
1305 information we have would be costly, so we just note where the label
1306 is and then later disable any optimization that would cross it. */
1307 if (LABEL_P (insn))
1308 last_label_ruid = reload_combine_ruid;
1309 else if (BARRIER_P (insn))
1311 /* Crossing a barrier resets all the use information. */
1312 for (r = 0; r < FIRST_PSEUDO_REGISTER; r++)
1313 if (! fixed_regs[r])
1314 reg_state[r].use_index = RELOAD_COMBINE_MAX_USES;
1316 else if (INSN_P (insn) && volatile_insn_p (PATTERN (insn)))
1317 /* Optimizations across insns being marked as volatile must be
1318 prevented. All the usage information is invalidated
1319 here. */
1320 for (r = 0; r < FIRST_PSEUDO_REGISTER; r++)
1321 if (! fixed_regs[r]
1322 && reg_state[r].use_index != RELOAD_COMBINE_MAX_USES)
1323 reg_state[r].use_index = -1;
1325 if (! NONDEBUG_INSN_P (insn))
1326 continue;
1328 reload_combine_ruid++;
1330 control_flow_insn = control_flow_insn_p (insn);
1331 if (control_flow_insn)
1332 last_jump_ruid = reload_combine_ruid;
1334 if (reload_combine_recognize_const_pattern (insn)
1335 || reload_combine_recognize_pattern (insn))
1336 continue;
1338 note_stores (PATTERN (insn), reload_combine_note_store, NULL);
1340 if (CALL_P (insn))
1342 rtx link;
1343 HARD_REG_SET used_regs;
1345 get_call_reg_set_usage (insn, &used_regs, call_used_reg_set);
1347 for (r = 0; r < FIRST_PSEUDO_REGISTER; r++)
1348 if (TEST_HARD_REG_BIT (used_regs, r))
1350 reg_state[r].use_index = RELOAD_COMBINE_MAX_USES;
1351 reg_state[r].store_ruid = reload_combine_ruid;
1354 for (link = CALL_INSN_FUNCTION_USAGE (insn); link;
1355 link = XEXP (link, 1))
1357 rtx setuse = XEXP (link, 0);
1358 rtx usage_rtx = XEXP (setuse, 0);
1359 if ((GET_CODE (setuse) == USE || GET_CODE (setuse) == CLOBBER)
1360 && REG_P (usage_rtx))
1362 unsigned int end_regno = END_REGNO (usage_rtx);
1363 for (unsigned int i = REGNO (usage_rtx); i < end_regno; ++i)
1364 if (GET_CODE (XEXP (link, 0)) == CLOBBER)
1366 reg_state[i].use_index = RELOAD_COMBINE_MAX_USES;
1367 reg_state[i].store_ruid = reload_combine_ruid;
1369 else
1370 reg_state[i].use_index = -1;
1375 if (control_flow_insn && !ANY_RETURN_P (PATTERN (insn)))
1377 /* Non-spill registers might be used at the call destination in
1378 some unknown fashion, so we have to mark the unknown use. */
1379 HARD_REG_SET *live;
1381 if ((condjump_p (insn) || condjump_in_parallel_p (insn))
1382 && JUMP_LABEL (insn))
1384 if (ANY_RETURN_P (JUMP_LABEL (insn)))
1385 live = NULL;
1386 else
1387 live = &LABEL_LIVE (JUMP_LABEL (insn));
1389 else
1390 live = &ever_live_at_start;
1392 if (live)
1393 for (r = 0; r < FIRST_PSEUDO_REGISTER; r++)
1394 if (TEST_HARD_REG_BIT (*live, r))
1395 reg_state[r].use_index = -1;
1398 reload_combine_note_use (&PATTERN (insn), insn, reload_combine_ruid,
1399 NULL_RTX);
1401 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
1403 if (REG_NOTE_KIND (note) == REG_INC && REG_P (XEXP (note, 0)))
1405 int regno = REGNO (XEXP (note, 0));
1406 reg_state[regno].store_ruid = reload_combine_ruid;
1407 reg_state[regno].real_store_ruid = reload_combine_ruid;
1408 reg_state[regno].use_index = -1;
1413 free (label_live);
1416 /* Check if DST is a register or a subreg of a register; if it is,
1417 update store_ruid, real_store_ruid and use_index in the reg_state
1418 structure accordingly. Called via note_stores from reload_combine. */
1420 static void
1421 reload_combine_note_store (rtx dst, const_rtx set, void *data ATTRIBUTE_UNUSED)
1423 int regno = 0;
1424 int i;
1425 machine_mode mode = GET_MODE (dst);
1427 if (GET_CODE (dst) == SUBREG)
1429 regno = subreg_regno_offset (REGNO (SUBREG_REG (dst)),
1430 GET_MODE (SUBREG_REG (dst)),
1431 SUBREG_BYTE (dst),
1432 GET_MODE (dst));
1433 dst = SUBREG_REG (dst);
1436 /* Some targets do argument pushes without adding REG_INC notes. */
1438 if (MEM_P (dst))
1440 dst = XEXP (dst, 0);
1441 if (GET_CODE (dst) == PRE_INC || GET_CODE (dst) == POST_INC
1442 || GET_CODE (dst) == PRE_DEC || GET_CODE (dst) == POST_DEC
1443 || GET_CODE (dst) == PRE_MODIFY || GET_CODE (dst) == POST_MODIFY)
1445 unsigned int end_regno = END_REGNO (XEXP (dst, 0));
1446 for (unsigned int i = REGNO (XEXP (dst, 0)); i < end_regno; ++i)
1448 /* We could probably do better, but for now mark the register
1449 as used in an unknown fashion and set/clobbered at this
1450 insn. */
1451 reg_state[i].use_index = -1;
1452 reg_state[i].store_ruid = reload_combine_ruid;
1453 reg_state[i].real_store_ruid = reload_combine_ruid;
1456 else
1457 return;
1460 if (!REG_P (dst))
1461 return;
1462 regno += REGNO (dst);
1464 /* note_stores might have stripped a STRICT_LOW_PART, so we have to be
1465 careful with registers / register parts that are not full words.
1466 Similarly for ZERO_EXTRACT. */
1467 if (GET_CODE (SET_DEST (set)) == ZERO_EXTRACT
1468 || GET_CODE (SET_DEST (set)) == STRICT_LOW_PART)
1470 for (i = hard_regno_nregs[regno][mode] - 1 + regno; i >= regno; i--)
1472 reg_state[i].use_index = -1;
1473 reg_state[i].store_ruid = reload_combine_ruid;
1474 reg_state[i].real_store_ruid = reload_combine_ruid;
1477 else
1479 for (i = hard_regno_nregs[regno][mode] - 1 + regno; i >= regno; i--)
1481 reg_state[i].store_ruid = reload_combine_ruid;
1482 if (GET_CODE (set) == SET)
1483 reg_state[i].real_store_ruid = reload_combine_ruid;
1484 reg_state[i].use_index = RELOAD_COMBINE_MAX_USES;
1489 /* XP points to a piece of rtl that has to be checked for any uses of
1490 registers.
1491 *XP is the pattern of INSN, or a part of it.
1492 Called from reload_combine, and recursively by itself. */
1493 static void
1494 reload_combine_note_use (rtx *xp, rtx_insn *insn, int ruid, rtx containing_mem)
1496 rtx x = *xp;
1497 enum rtx_code code = x->code;
1498 const char *fmt;
1499 int i, j;
1500 rtx offset = const0_rtx; /* For the REG case below. */
1502 switch (code)
1504 case SET:
1505 if (REG_P (SET_DEST (x)))
1507 reload_combine_note_use (&SET_SRC (x), insn, ruid, NULL_RTX);
1508 return;
1510 break;
1512 case USE:
1513 /* If this is the USE of a return value, we can't change it. */
1514 if (REG_P (XEXP (x, 0)) && REG_FUNCTION_VALUE_P (XEXP (x, 0)))
1516 /* Mark the return register as used in an unknown fashion. */
1517 rtx reg = XEXP (x, 0);
1518 unsigned int end_regno = END_REGNO (reg);
1519 for (unsigned int regno = REGNO (reg); regno < end_regno; ++regno)
1520 reg_state[regno].use_index = -1;
1521 return;
1523 break;
1525 case CLOBBER:
1526 if (REG_P (SET_DEST (x)))
1528 /* No spurious CLOBBERs of pseudo registers may remain. */
1529 gcc_assert (REGNO (SET_DEST (x)) < FIRST_PSEUDO_REGISTER);
1530 return;
1532 break;
1534 case PLUS:
1535 /* We are interested in (plus (reg) (const_int)) . */
1536 if (!REG_P (XEXP (x, 0))
1537 || !CONST_INT_P (XEXP (x, 1)))
1538 break;
1539 offset = XEXP (x, 1);
1540 x = XEXP (x, 0);
1541 /* Fall through. */
1542 case REG:
1544 int regno = REGNO (x);
1545 int use_index;
1546 int nregs;
1548 /* No spurious USEs of pseudo registers may remain. */
1549 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
1551 nregs = REG_NREGS (x);
1553 /* We can't substitute into multi-hard-reg uses. */
1554 if (nregs > 1)
1556 while (--nregs >= 0)
1557 reg_state[regno + nregs].use_index = -1;
1558 return;
1561 /* We may be called to update uses in previously seen insns.
1562 Don't add uses beyond the last store we saw. */
1563 if (ruid < reg_state[regno].store_ruid)
1564 return;
1566 /* If this register is already used in some unknown fashion, we
1567 can't do anything.
1568 If we decrement the index from zero to -1, we can't store more
1569 uses, so this register becomes used in an unknown fashion. */
1570 use_index = --reg_state[regno].use_index;
1571 if (use_index < 0)
1572 return;
1574 if (use_index == RELOAD_COMBINE_MAX_USES - 1)
1576 /* This is the first use of this register we have seen since we
1577 marked it as dead. */
1578 reg_state[regno].offset = offset;
1579 reg_state[regno].all_offsets_match = true;
1580 reg_state[regno].use_ruid = ruid;
1582 else
1584 if (reg_state[regno].use_ruid > ruid)
1585 reg_state[regno].use_ruid = ruid;
1587 if (! rtx_equal_p (offset, reg_state[regno].offset))
1588 reg_state[regno].all_offsets_match = false;
1591 reg_state[regno].reg_use[use_index].insn = insn;
1592 reg_state[regno].reg_use[use_index].ruid = ruid;
1593 reg_state[regno].reg_use[use_index].containing_mem = containing_mem;
1594 reg_state[regno].reg_use[use_index].usep = xp;
1595 return;
1598 case MEM:
1599 containing_mem = x;
1600 break;
1602 default:
1603 break;
1606 /* Recursively process the components of X. */
1607 fmt = GET_RTX_FORMAT (code);
1608 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1610 if (fmt[i] == 'e')
1611 reload_combine_note_use (&XEXP (x, i), insn, ruid, containing_mem);
1612 else if (fmt[i] == 'E')
1614 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1615 reload_combine_note_use (&XVECEXP (x, i, j), insn, ruid,
1616 containing_mem);
1621 /* See if we can reduce the cost of a constant by replacing a move
1622 with an add. We track situations in which a register is set to a
1623 constant or to a register plus a constant. */
1624 /* We cannot do our optimization across labels. Invalidating all the
1625 information about register contents we have would be costly, so we
1626 use move2add_last_label_luid to note where the label is and then
1627 later disable any optimization that would cross it.
1628 reg_offset[n] / reg_base_reg[n] / reg_symbol_ref[n] / reg_mode[n]
1629 are only valid if reg_set_luid[n] is greater than
1630 move2add_last_label_luid.
1631 For a set that established a new (potential) base register with
1632 non-constant value, we use move2add_luid from the place where the
1633 setting insn is encountered; registers based off that base then
1634 get the same reg_set_luid. Constants all get
1635 move2add_last_label_luid + 1 as their reg_set_luid. */
1636 static int reg_set_luid[FIRST_PSEUDO_REGISTER];
1638 /* If reg_base_reg[n] is negative, register n has been set to
1639 reg_offset[n] or reg_symbol_ref[n] + reg_offset[n] in mode reg_mode[n].
1640 If reg_base_reg[n] is non-negative, register n has been set to the
1641 sum of reg_offset[n] and the value of register reg_base_reg[n]
1642 before reg_set_luid[n], calculated in mode reg_mode[n] .
1643 For multi-hard-register registers, all but the first one are
1644 recorded as BLKmode in reg_mode. Setting reg_mode to VOIDmode
1645 marks it as invalid. */
1646 static HOST_WIDE_INT reg_offset[FIRST_PSEUDO_REGISTER];
1647 static int reg_base_reg[FIRST_PSEUDO_REGISTER];
1648 static rtx reg_symbol_ref[FIRST_PSEUDO_REGISTER];
1649 static machine_mode reg_mode[FIRST_PSEUDO_REGISTER];
1651 /* move2add_luid is linearly increased while scanning the instructions
1652 from first to last. It is used to set reg_set_luid in
1653 reload_cse_move2add and move2add_note_store. */
1654 static int move2add_luid;
1656 /* move2add_last_label_luid is set whenever a label is found. Labels
1657 invalidate all previously collected reg_offset data. */
1658 static int move2add_last_label_luid;
1660 /* ??? We don't know how zero / sign extension is handled, hence we
1661 can't go from a narrower to a wider mode. */
1662 #define MODES_OK_FOR_MOVE2ADD(OUTMODE, INMODE) \
1663 (GET_MODE_SIZE (OUTMODE) == GET_MODE_SIZE (INMODE) \
1664 || (GET_MODE_SIZE (OUTMODE) <= GET_MODE_SIZE (INMODE) \
1665 && TRULY_NOOP_TRUNCATION_MODES_P (OUTMODE, INMODE)))
1667 /* Record that REG is being set to a value with the mode of REG. */
1669 static void
1670 move2add_record_mode (rtx reg)
1672 int regno, nregs;
1673 machine_mode mode = GET_MODE (reg);
1675 if (GET_CODE (reg) == SUBREG)
1677 regno = subreg_regno (reg);
1678 nregs = subreg_nregs (reg);
1680 else if (REG_P (reg))
1682 regno = REGNO (reg);
1683 nregs = REG_NREGS (reg);
1685 else
1686 gcc_unreachable ();
1687 for (int i = nregs - 1; i > 0; i--)
1688 reg_mode[regno + i] = BLKmode;
1689 reg_mode[regno] = mode;
1692 /* Record that REG is being set to the sum of SYM and OFF. */
1694 static void
1695 move2add_record_sym_value (rtx reg, rtx sym, rtx off)
1697 int regno = REGNO (reg);
1699 move2add_record_mode (reg);
1700 reg_set_luid[regno] = move2add_luid;
1701 reg_base_reg[regno] = -1;
1702 reg_symbol_ref[regno] = sym;
1703 reg_offset[regno] = INTVAL (off);
1706 /* Check if REGNO contains a valid value in MODE. */
1708 static bool
1709 move2add_valid_value_p (int regno, machine_mode mode)
1711 if (reg_set_luid[regno] <= move2add_last_label_luid)
1712 return false;
1714 if (mode != reg_mode[regno])
1716 if (!MODES_OK_FOR_MOVE2ADD (mode, reg_mode[regno]))
1717 return false;
1718 /* The value loaded into regno in reg_mode[regno] is also valid in
1719 mode after truncation only if (REG:mode regno) is the lowpart of
1720 (REG:reg_mode[regno] regno). Now, for big endian, the starting
1721 regno of the lowpart might be different. */
1722 int s_off = subreg_lowpart_offset (mode, reg_mode[regno]);
1723 s_off = subreg_regno_offset (regno, reg_mode[regno], s_off, mode);
1724 if (s_off != 0)
1725 /* We could in principle adjust regno, check reg_mode[regno] to be
1726 BLKmode, and return s_off to the caller (vs. -1 for failure),
1727 but we currently have no callers that could make use of this
1728 information. */
1729 return false;
1732 for (int i = hard_regno_nregs[regno][mode] - 1; i > 0; i--)
1733 if (reg_mode[regno + i] != BLKmode)
1734 return false;
1735 return true;
1738 /* This function is called with INSN that sets REG to (SYM + OFF),
1739 while REG is known to already have value (SYM + offset).
1740 This function tries to change INSN into an add instruction
1741 (set (REG) (plus (REG) (OFF - offset))) using the known value.
1742 It also updates the information about REG's known value.
1743 Return true if we made a change. */
1745 static bool
1746 move2add_use_add2_insn (rtx reg, rtx sym, rtx off, rtx_insn *insn)
1748 rtx pat = PATTERN (insn);
1749 rtx src = SET_SRC (pat);
1750 int regno = REGNO (reg);
1751 rtx new_src = gen_int_mode (UINTVAL (off) - reg_offset[regno],
1752 GET_MODE (reg));
1753 bool speed = optimize_bb_for_speed_p (BLOCK_FOR_INSN (insn));
1754 bool changed = false;
1756 /* (set (reg) (plus (reg) (const_int 0))) is not canonical;
1757 use (set (reg) (reg)) instead.
1758 We don't delete this insn, nor do we convert it into a
1759 note, to avoid losing register notes or the return
1760 value flag. jump2 already knows how to get rid of
1761 no-op moves. */
1762 if (new_src == const0_rtx)
1764 /* If the constants are different, this is a
1765 truncation, that, if turned into (set (reg)
1766 (reg)), would be discarded. Maybe we should
1767 try a truncMN pattern? */
1768 if (INTVAL (off) == reg_offset [regno])
1769 changed = validate_change (insn, &SET_SRC (pat), reg, 0);
1771 else
1773 struct full_rtx_costs oldcst, newcst;
1774 rtx tem = gen_rtx_PLUS (GET_MODE (reg), reg, new_src);
1776 get_full_set_rtx_cost (pat, &oldcst);
1777 SET_SRC (pat) = tem;
1778 get_full_set_rtx_cost (pat, &newcst);
1779 SET_SRC (pat) = src;
1781 if (costs_lt_p (&newcst, &oldcst, speed)
1782 && have_add2_insn (reg, new_src))
1783 changed = validate_change (insn, &SET_SRC (pat), tem, 0);
1784 else if (sym == NULL_RTX && GET_MODE (reg) != BImode)
1786 machine_mode narrow_mode;
1787 for (narrow_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
1788 narrow_mode != VOIDmode
1789 && narrow_mode != GET_MODE (reg);
1790 narrow_mode = GET_MODE_WIDER_MODE (narrow_mode))
1792 if (have_insn_for (STRICT_LOW_PART, narrow_mode)
1793 && ((reg_offset[regno] & ~GET_MODE_MASK (narrow_mode))
1794 == (INTVAL (off) & ~GET_MODE_MASK (narrow_mode))))
1796 rtx narrow_reg = gen_lowpart_common (narrow_mode, reg);
1797 rtx narrow_src = gen_int_mode (INTVAL (off),
1798 narrow_mode);
1799 rtx new_set
1800 = gen_rtx_SET (gen_rtx_STRICT_LOW_PART (VOIDmode,
1801 narrow_reg),
1802 narrow_src);
1803 get_full_set_rtx_cost (new_set, &newcst);
1804 if (costs_lt_p (&newcst, &oldcst, speed))
1806 changed = validate_change (insn, &PATTERN (insn),
1807 new_set, 0);
1808 if (changed)
1809 break;
1815 move2add_record_sym_value (reg, sym, off);
1816 return changed;
1820 /* This function is called with INSN that sets REG to (SYM + OFF),
1821 but REG doesn't have known value (SYM + offset). This function
1822 tries to find another register which is known to already have
1823 value (SYM + offset) and change INSN into an add instruction
1824 (set (REG) (plus (the found register) (OFF - offset))) if such
1825 a register is found. It also updates the information about
1826 REG's known value.
1827 Return true iff we made a change. */
1829 static bool
1830 move2add_use_add3_insn (rtx reg, rtx sym, rtx off, rtx_insn *insn)
1832 rtx pat = PATTERN (insn);
1833 rtx src = SET_SRC (pat);
1834 int regno = REGNO (reg);
1835 int min_regno = 0;
1836 bool speed = optimize_bb_for_speed_p (BLOCK_FOR_INSN (insn));
1837 int i;
1838 bool changed = false;
1839 struct full_rtx_costs oldcst, newcst, mincst;
1840 rtx plus_expr;
1842 init_costs_to_max (&mincst);
1843 get_full_set_rtx_cost (pat, &oldcst);
1845 plus_expr = gen_rtx_PLUS (GET_MODE (reg), reg, const0_rtx);
1846 SET_SRC (pat) = plus_expr;
1848 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1849 if (move2add_valid_value_p (i, GET_MODE (reg))
1850 && reg_base_reg[i] < 0
1851 && reg_symbol_ref[i] != NULL_RTX
1852 && rtx_equal_p (sym, reg_symbol_ref[i]))
1854 rtx new_src = gen_int_mode (UINTVAL (off) - reg_offset[i],
1855 GET_MODE (reg));
1856 /* (set (reg) (plus (reg) (const_int 0))) is not canonical;
1857 use (set (reg) (reg)) instead.
1858 We don't delete this insn, nor do we convert it into a
1859 note, to avoid losing register notes or the return
1860 value flag. jump2 already knows how to get rid of
1861 no-op moves. */
1862 if (new_src == const0_rtx)
1864 init_costs_to_zero (&mincst);
1865 min_regno = i;
1866 break;
1868 else
1870 XEXP (plus_expr, 1) = new_src;
1871 get_full_set_rtx_cost (pat, &newcst);
1873 if (costs_lt_p (&newcst, &mincst, speed))
1875 mincst = newcst;
1876 min_regno = i;
1880 SET_SRC (pat) = src;
1882 if (costs_lt_p (&mincst, &oldcst, speed))
1884 rtx tem;
1886 tem = gen_rtx_REG (GET_MODE (reg), min_regno);
1887 if (i != min_regno)
1889 rtx new_src = gen_int_mode (UINTVAL (off) - reg_offset[min_regno],
1890 GET_MODE (reg));
1891 tem = gen_rtx_PLUS (GET_MODE (reg), tem, new_src);
1893 if (validate_change (insn, &SET_SRC (pat), tem, 0))
1894 changed = true;
1896 reg_set_luid[regno] = move2add_luid;
1897 move2add_record_sym_value (reg, sym, off);
1898 return changed;
1901 /* Convert move insns with constant inputs to additions if they are cheaper.
1902 Return true if any changes were made. */
1903 static bool
1904 reload_cse_move2add (rtx_insn *first)
1906 int i;
1907 rtx_insn *insn;
1908 bool changed = false;
1910 for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; i--)
1912 reg_set_luid[i] = 0;
1913 reg_offset[i] = 0;
1914 reg_base_reg[i] = 0;
1915 reg_symbol_ref[i] = NULL_RTX;
1916 reg_mode[i] = VOIDmode;
1919 move2add_last_label_luid = 0;
1920 move2add_luid = 2;
1921 for (insn = first; insn; insn = NEXT_INSN (insn), move2add_luid++)
1923 rtx pat, note;
1925 if (LABEL_P (insn))
1927 move2add_last_label_luid = move2add_luid;
1928 /* We're going to increment move2add_luid twice after a
1929 label, so that we can use move2add_last_label_luid + 1 as
1930 the luid for constants. */
1931 move2add_luid++;
1932 continue;
1934 if (! INSN_P (insn))
1935 continue;
1936 pat = PATTERN (insn);
1937 /* For simplicity, we only perform this optimization on
1938 straightforward SETs. */
1939 if (GET_CODE (pat) == SET
1940 && REG_P (SET_DEST (pat)))
1942 rtx reg = SET_DEST (pat);
1943 int regno = REGNO (reg);
1944 rtx src = SET_SRC (pat);
1946 /* Check if we have valid information on the contents of this
1947 register in the mode of REG. */
1948 if (move2add_valid_value_p (regno, GET_MODE (reg))
1949 && dbg_cnt (cse2_move2add))
1951 /* Try to transform (set (REGX) (CONST_INT A))
1953 (set (REGX) (CONST_INT B))
1955 (set (REGX) (CONST_INT A))
1957 (set (REGX) (plus (REGX) (CONST_INT B-A)))
1959 (set (REGX) (CONST_INT A))
1961 (set (STRICT_LOW_PART (REGX)) (CONST_INT B))
1964 if (CONST_INT_P (src)
1965 && reg_base_reg[regno] < 0
1966 && reg_symbol_ref[regno] == NULL_RTX)
1968 changed |= move2add_use_add2_insn (reg, NULL_RTX, src, insn);
1969 continue;
1972 /* Try to transform (set (REGX) (REGY))
1973 (set (REGX) (PLUS (REGX) (CONST_INT A)))
1975 (set (REGX) (REGY))
1976 (set (REGX) (PLUS (REGX) (CONST_INT B)))
1978 (set (REGX) (REGY))
1979 (set (REGX) (PLUS (REGX) (CONST_INT A)))
1981 (set (REGX) (plus (REGX) (CONST_INT B-A))) */
1982 else if (REG_P (src)
1983 && reg_set_luid[regno] == reg_set_luid[REGNO (src)]
1984 && reg_base_reg[regno] == reg_base_reg[REGNO (src)]
1985 && move2add_valid_value_p (REGNO (src), GET_MODE (reg)))
1987 rtx_insn *next = next_nonnote_nondebug_insn (insn);
1988 rtx set = NULL_RTX;
1989 if (next)
1990 set = single_set (next);
1991 if (set
1992 && SET_DEST (set) == reg
1993 && GET_CODE (SET_SRC (set)) == PLUS
1994 && XEXP (SET_SRC (set), 0) == reg
1995 && CONST_INT_P (XEXP (SET_SRC (set), 1)))
1997 rtx src3 = XEXP (SET_SRC (set), 1);
1998 unsigned HOST_WIDE_INT added_offset = UINTVAL (src3);
1999 HOST_WIDE_INT base_offset = reg_offset[REGNO (src)];
2000 HOST_WIDE_INT regno_offset = reg_offset[regno];
2001 rtx new_src =
2002 gen_int_mode (added_offset
2003 + base_offset
2004 - regno_offset,
2005 GET_MODE (reg));
2006 bool success = false;
2007 bool speed = optimize_bb_for_speed_p (BLOCK_FOR_INSN (insn));
2009 if (new_src == const0_rtx)
2010 /* See above why we create (set (reg) (reg)) here. */
2011 success
2012 = validate_change (next, &SET_SRC (set), reg, 0);
2013 else
2015 rtx old_src = SET_SRC (set);
2016 struct full_rtx_costs oldcst, newcst;
2017 rtx tem = gen_rtx_PLUS (GET_MODE (reg), reg, new_src);
2019 get_full_set_rtx_cost (set, &oldcst);
2020 SET_SRC (set) = tem;
2021 get_full_set_src_cost (tem, GET_MODE (reg), &newcst);
2022 SET_SRC (set) = old_src;
2023 costs_add_n_insns (&oldcst, 1);
2025 if (costs_lt_p (&newcst, &oldcst, speed)
2026 && have_add2_insn (reg, new_src))
2028 rtx newpat = gen_rtx_SET (reg, tem);
2029 success
2030 = validate_change (next, &PATTERN (next),
2031 newpat, 0);
2034 if (success)
2035 delete_insn (insn);
2036 changed |= success;
2037 insn = next;
2038 move2add_record_mode (reg);
2039 reg_offset[regno]
2040 = trunc_int_for_mode (added_offset + base_offset,
2041 GET_MODE (reg));
2042 continue;
2047 /* Try to transform
2048 (set (REGX) (CONST (PLUS (SYMBOL_REF) (CONST_INT A))))
2050 (set (REGY) (CONST (PLUS (SYMBOL_REF) (CONST_INT B))))
2052 (set (REGX) (CONST (PLUS (SYMBOL_REF) (CONST_INT A))))
2054 (set (REGY) (CONST (PLUS (REGX) (CONST_INT B-A)))) */
2055 if ((GET_CODE (src) == SYMBOL_REF
2056 || (GET_CODE (src) == CONST
2057 && GET_CODE (XEXP (src, 0)) == PLUS
2058 && GET_CODE (XEXP (XEXP (src, 0), 0)) == SYMBOL_REF
2059 && CONST_INT_P (XEXP (XEXP (src, 0), 1))))
2060 && dbg_cnt (cse2_move2add))
2062 rtx sym, off;
2064 if (GET_CODE (src) == SYMBOL_REF)
2066 sym = src;
2067 off = const0_rtx;
2069 else
2071 sym = XEXP (XEXP (src, 0), 0);
2072 off = XEXP (XEXP (src, 0), 1);
2075 /* If the reg already contains the value which is sum of
2076 sym and some constant value, we can use an add2 insn. */
2077 if (move2add_valid_value_p (regno, GET_MODE (reg))
2078 && reg_base_reg[regno] < 0
2079 && reg_symbol_ref[regno] != NULL_RTX
2080 && rtx_equal_p (sym, reg_symbol_ref[regno]))
2081 changed |= move2add_use_add2_insn (reg, sym, off, insn);
2083 /* Otherwise, we have to find a register whose value is sum
2084 of sym and some constant value. */
2085 else
2086 changed |= move2add_use_add3_insn (reg, sym, off, insn);
2088 continue;
2092 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2094 if (REG_NOTE_KIND (note) == REG_INC
2095 && REG_P (XEXP (note, 0)))
2097 /* Reset the information about this register. */
2098 int regno = REGNO (XEXP (note, 0));
2099 if (regno < FIRST_PSEUDO_REGISTER)
2101 move2add_record_mode (XEXP (note, 0));
2102 reg_mode[regno] = VOIDmode;
2106 note_stores (PATTERN (insn), move2add_note_store, insn);
2108 /* If INSN is a conditional branch, we try to extract an
2109 implicit set out of it. */
2110 if (any_condjump_p (insn))
2112 rtx cnd = fis_get_condition (insn);
2114 if (cnd != NULL_RTX
2115 && GET_CODE (cnd) == NE
2116 && REG_P (XEXP (cnd, 0))
2117 && !reg_set_p (XEXP (cnd, 0), insn)
2118 /* The following two checks, which are also in
2119 move2add_note_store, are intended to reduce the
2120 number of calls to gen_rtx_SET to avoid memory
2121 allocation if possible. */
2122 && SCALAR_INT_MODE_P (GET_MODE (XEXP (cnd, 0)))
2123 && REG_NREGS (XEXP (cnd, 0)) == 1
2124 && CONST_INT_P (XEXP (cnd, 1)))
2126 rtx implicit_set =
2127 gen_rtx_SET (XEXP (cnd, 0), XEXP (cnd, 1));
2128 move2add_note_store (SET_DEST (implicit_set), implicit_set, insn);
2132 /* If this is a CALL_INSN, all call used registers are stored with
2133 unknown values. */
2134 if (CALL_P (insn))
2136 for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; i--)
2138 if (call_used_regs[i])
2139 /* Reset the information about this register. */
2140 reg_mode[i] = VOIDmode;
2144 return changed;
2147 /* SET is a SET or CLOBBER that sets DST. DATA is the insn which
2148 contains SET.
2149 Update reg_set_luid, reg_offset and reg_base_reg accordingly.
2150 Called from reload_cse_move2add via note_stores. */
2152 static void
2153 move2add_note_store (rtx dst, const_rtx set, void *data)
2155 rtx_insn *insn = (rtx_insn *) data;
2156 unsigned int regno = 0;
2157 machine_mode mode = GET_MODE (dst);
2159 /* Some targets do argument pushes without adding REG_INC notes. */
2161 if (MEM_P (dst))
2163 dst = XEXP (dst, 0);
2164 if (GET_CODE (dst) == PRE_INC || GET_CODE (dst) == POST_INC
2165 || GET_CODE (dst) == PRE_DEC || GET_CODE (dst) == POST_DEC)
2166 reg_mode[REGNO (XEXP (dst, 0))] = VOIDmode;
2167 return;
2170 if (GET_CODE (dst) == SUBREG)
2171 regno = subreg_regno (dst);
2172 else if (REG_P (dst))
2173 regno = REGNO (dst);
2174 else
2175 return;
2177 if (SCALAR_INT_MODE_P (mode)
2178 && GET_CODE (set) == SET)
2180 rtx note, sym = NULL_RTX;
2181 rtx off;
2183 note = find_reg_equal_equiv_note (insn);
2184 if (note && GET_CODE (XEXP (note, 0)) == SYMBOL_REF)
2186 sym = XEXP (note, 0);
2187 off = const0_rtx;
2189 else if (note && GET_CODE (XEXP (note, 0)) == CONST
2190 && GET_CODE (XEXP (XEXP (note, 0), 0)) == PLUS
2191 && GET_CODE (XEXP (XEXP (XEXP (note, 0), 0), 0)) == SYMBOL_REF
2192 && CONST_INT_P (XEXP (XEXP (XEXP (note, 0), 0), 1)))
2194 sym = XEXP (XEXP (XEXP (note, 0), 0), 0);
2195 off = XEXP (XEXP (XEXP (note, 0), 0), 1);
2198 if (sym != NULL_RTX)
2200 move2add_record_sym_value (dst, sym, off);
2201 return;
2205 if (SCALAR_INT_MODE_P (mode)
2206 && GET_CODE (set) == SET
2207 && GET_CODE (SET_DEST (set)) != ZERO_EXTRACT
2208 && GET_CODE (SET_DEST (set)) != STRICT_LOW_PART)
2210 rtx src = SET_SRC (set);
2211 rtx base_reg;
2212 unsigned HOST_WIDE_INT offset;
2213 int base_regno;
2215 switch (GET_CODE (src))
2217 case PLUS:
2218 if (REG_P (XEXP (src, 0)))
2220 base_reg = XEXP (src, 0);
2222 if (CONST_INT_P (XEXP (src, 1)))
2223 offset = UINTVAL (XEXP (src, 1));
2224 else if (REG_P (XEXP (src, 1))
2225 && move2add_valid_value_p (REGNO (XEXP (src, 1)), mode))
2227 if (reg_base_reg[REGNO (XEXP (src, 1))] < 0
2228 && reg_symbol_ref[REGNO (XEXP (src, 1))] == NULL_RTX)
2229 offset = reg_offset[REGNO (XEXP (src, 1))];
2230 /* Maybe the first register is known to be a
2231 constant. */
2232 else if (move2add_valid_value_p (REGNO (base_reg), mode)
2233 && reg_base_reg[REGNO (base_reg)] < 0
2234 && reg_symbol_ref[REGNO (base_reg)] == NULL_RTX)
2236 offset = reg_offset[REGNO (base_reg)];
2237 base_reg = XEXP (src, 1);
2239 else
2240 goto invalidate;
2242 else
2243 goto invalidate;
2245 break;
2248 goto invalidate;
2250 case REG:
2251 base_reg = src;
2252 offset = 0;
2253 break;
2255 case CONST_INT:
2256 /* Start tracking the register as a constant. */
2257 reg_base_reg[regno] = -1;
2258 reg_symbol_ref[regno] = NULL_RTX;
2259 reg_offset[regno] = INTVAL (SET_SRC (set));
2260 /* We assign the same luid to all registers set to constants. */
2261 reg_set_luid[regno] = move2add_last_label_luid + 1;
2262 move2add_record_mode (dst);
2263 return;
2265 default:
2266 goto invalidate;
2269 base_regno = REGNO (base_reg);
2270 /* If information about the base register is not valid, set it
2271 up as a new base register, pretending its value is known
2272 starting from the current insn. */
2273 if (!move2add_valid_value_p (base_regno, mode))
2275 reg_base_reg[base_regno] = base_regno;
2276 reg_symbol_ref[base_regno] = NULL_RTX;
2277 reg_offset[base_regno] = 0;
2278 reg_set_luid[base_regno] = move2add_luid;
2279 gcc_assert (GET_MODE (base_reg) == mode);
2280 move2add_record_mode (base_reg);
2283 /* Copy base information from our base register. */
2284 reg_set_luid[regno] = reg_set_luid[base_regno];
2285 reg_base_reg[regno] = reg_base_reg[base_regno];
2286 reg_symbol_ref[regno] = reg_symbol_ref[base_regno];
2288 /* Compute the sum of the offsets or constants. */
2289 reg_offset[regno]
2290 = trunc_int_for_mode (offset + reg_offset[base_regno], mode);
2292 move2add_record_mode (dst);
2294 else
2296 invalidate:
2297 /* Invalidate the contents of the register. */
2298 move2add_record_mode (dst);
2299 reg_mode[regno] = VOIDmode;
2303 namespace {
2305 const pass_data pass_data_postreload_cse =
2307 RTL_PASS, /* type */
2308 "postreload", /* name */
2309 OPTGROUP_NONE, /* optinfo_flags */
2310 TV_RELOAD_CSE_REGS, /* tv_id */
2311 0, /* properties_required */
2312 0, /* properties_provided */
2313 0, /* properties_destroyed */
2314 0, /* todo_flags_start */
2315 TODO_df_finish, /* todo_flags_finish */
2318 class pass_postreload_cse : public rtl_opt_pass
2320 public:
2321 pass_postreload_cse (gcc::context *ctxt)
2322 : rtl_opt_pass (pass_data_postreload_cse, ctxt)
2325 /* opt_pass methods: */
2326 virtual bool gate (function *) { return (optimize > 0 && reload_completed); }
2328 virtual unsigned int execute (function *);
2330 }; // class pass_postreload_cse
2332 unsigned int
2333 pass_postreload_cse::execute (function *fun)
2335 if (!dbg_cnt (postreload_cse))
2336 return 0;
2338 /* Do a very simple CSE pass over just the hard registers. */
2339 reload_cse_regs (get_insns ());
2340 /* Reload_cse_regs can eliminate potentially-trapping MEMs.
2341 Remove any EH edges associated with them. */
2342 if (fun->can_throw_non_call_exceptions
2343 && purge_all_dead_edges ())
2344 cleanup_cfg (0);
2346 return 0;
2349 } // anon namespace
2351 rtl_opt_pass *
2352 make_pass_postreload_cse (gcc::context *ctxt)
2354 return new pass_postreload_cse (ctxt);