2015-06-11 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / postreload.c
blobf83da4a5f0f2b81e4637fad549f8e6537275b171
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 "tm.h"
25 #include "hard-reg-set.h"
26 #include "rtl.h"
27 #include "tm_p.h"
28 #include "obstack.h"
29 #include "insn-config.h"
30 #include "flags.h"
31 #include "input.h"
32 #include "function.h"
33 #include "symtab.h"
34 #include "alias.h"
35 #include "tree.h"
36 #include "expmed.h"
37 #include "dojump.h"
38 #include "explow.h"
39 #include "calls.h"
40 #include "emit-rtl.h"
41 #include "varasm.h"
42 #include "stmt.h"
43 #include "expr.h"
44 #include "insn-codes.h"
45 #include "optabs.h"
46 #include "regs.h"
47 #include "predict.h"
48 #include "dominance.h"
49 #include "cfg.h"
50 #include "cfgrtl.h"
51 #include "cfgbuild.h"
52 #include "cfgcleanup.h"
53 #include "basic-block.h"
54 #include "reload.h"
55 #include "recog.h"
56 #include "alloc-pool.h"
57 #include "cselib.h"
58 #include "diagnostic-core.h"
59 #include "except.h"
60 #include "target.h"
61 #include "tree-pass.h"
62 #include "df.h"
63 #include "dbgcnt.h"
65 static int reload_cse_noop_set_p (rtx);
66 static bool reload_cse_simplify (rtx_insn *, rtx);
67 static void reload_cse_regs_1 (void);
68 static int reload_cse_simplify_set (rtx, rtx_insn *);
69 static int reload_cse_simplify_operands (rtx_insn *, rtx);
71 static void reload_combine (void);
72 static void reload_combine_note_use (rtx *, rtx_insn *, int, rtx);
73 static void reload_combine_note_store (rtx, const_rtx, void *);
75 static bool reload_cse_move2add (rtx_insn *);
76 static void move2add_note_store (rtx, const_rtx, void *);
78 /* Call cse / combine like post-reload optimization phases.
79 FIRST is the first instruction. */
81 static void
82 reload_cse_regs (rtx_insn *first ATTRIBUTE_UNUSED)
84 bool moves_converted;
85 reload_cse_regs_1 ();
86 reload_combine ();
87 moves_converted = reload_cse_move2add (first);
88 if (flag_expensive_optimizations)
90 if (moves_converted)
91 reload_combine ();
92 reload_cse_regs_1 ();
96 /* See whether a single set SET is a noop. */
97 static int
98 reload_cse_noop_set_p (rtx set)
100 if (cselib_reg_set_mode (SET_DEST (set)) != GET_MODE (SET_DEST (set)))
101 return 0;
103 return rtx_equal_for_cselib_p (SET_DEST (set), SET_SRC (set));
106 /* Try to simplify INSN. Return true if the CFG may have changed. */
107 static bool
108 reload_cse_simplify (rtx_insn *insn, rtx testreg)
110 rtx body = PATTERN (insn);
111 basic_block insn_bb = BLOCK_FOR_INSN (insn);
112 unsigned insn_bb_succs = EDGE_COUNT (insn_bb->succs);
114 if (GET_CODE (body) == SET)
116 int count = 0;
118 /* Simplify even if we may think it is a no-op.
119 We may think a memory load of a value smaller than WORD_SIZE
120 is redundant because we haven't taken into account possible
121 implicit extension. reload_cse_simplify_set() will bring
122 this out, so it's safer to simplify before we delete. */
123 count += reload_cse_simplify_set (body, insn);
125 if (!count && reload_cse_noop_set_p (body))
127 rtx value = SET_DEST (body);
128 if (REG_P (value)
129 && ! REG_FUNCTION_VALUE_P (value))
130 value = 0;
131 if (check_for_inc_dec (insn))
132 delete_insn_and_edges (insn);
133 /* We're done with this insn. */
134 goto done;
137 if (count > 0)
138 apply_change_group ();
139 else
140 reload_cse_simplify_operands (insn, testreg);
142 else if (GET_CODE (body) == PARALLEL)
144 int i;
145 int count = 0;
146 rtx value = NULL_RTX;
148 /* Registers mentioned in the clobber list for an asm cannot be reused
149 within the body of the asm. Invalidate those registers now so that
150 we don't try to substitute values for them. */
151 if (asm_noperands (body) >= 0)
153 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
155 rtx part = XVECEXP (body, 0, i);
156 if (GET_CODE (part) == CLOBBER && REG_P (XEXP (part, 0)))
157 cselib_invalidate_rtx (XEXP (part, 0));
161 /* If every action in a PARALLEL is a noop, we can delete
162 the entire PARALLEL. */
163 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
165 rtx part = XVECEXP (body, 0, i);
166 if (GET_CODE (part) == SET)
168 if (! reload_cse_noop_set_p (part))
169 break;
170 if (REG_P (SET_DEST (part))
171 && REG_FUNCTION_VALUE_P (SET_DEST (part)))
173 if (value)
174 break;
175 value = SET_DEST (part);
178 else if (GET_CODE (part) != CLOBBER)
179 break;
182 if (i < 0)
184 if (check_for_inc_dec (insn))
185 delete_insn_and_edges (insn);
186 /* We're done with this insn. */
187 goto done;
190 /* It's not a no-op, but we can try to simplify it. */
191 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
192 if (GET_CODE (XVECEXP (body, 0, i)) == SET)
193 count += reload_cse_simplify_set (XVECEXP (body, 0, i), insn);
195 if (count > 0)
196 apply_change_group ();
197 else
198 reload_cse_simplify_operands (insn, testreg);
201 done:
202 return (EDGE_COUNT (insn_bb->succs) != insn_bb_succs);
205 /* Do a very simple CSE pass over the hard registers.
207 This function detects no-op moves where we happened to assign two
208 different pseudo-registers to the same hard register, and then
209 copied one to the other. Reload will generate a useless
210 instruction copying a register to itself.
212 This function also detects cases where we load a value from memory
213 into two different registers, and (if memory is more expensive than
214 registers) changes it to simply copy the first register into the
215 second register.
217 Another optimization is performed that scans the operands of each
218 instruction to see whether the value is already available in a
219 hard register. It then replaces the operand with the hard register
220 if possible, much like an optional reload would. */
222 static void
223 reload_cse_regs_1 (void)
225 bool cfg_changed = false;
226 basic_block bb;
227 rtx_insn *insn;
228 rtx testreg = gen_rtx_REG (word_mode, LAST_VIRTUAL_REGISTER + 1);
230 cselib_init (CSELIB_RECORD_MEMORY);
231 init_alias_analysis ();
233 FOR_EACH_BB_FN (bb, cfun)
234 FOR_BB_INSNS (bb, insn)
236 if (INSN_P (insn))
237 cfg_changed |= reload_cse_simplify (insn, testreg);
239 cselib_process_insn (insn);
242 /* Clean up. */
243 end_alias_analysis ();
244 cselib_finish ();
245 if (cfg_changed)
246 cleanup_cfg (0);
249 /* Try to simplify a single SET instruction. SET is the set pattern.
250 INSN is the instruction it came from.
251 This function only handles one case: if we set a register to a value
252 which is not a register, we try to find that value in some other register
253 and change the set into a register copy. */
255 static int
256 reload_cse_simplify_set (rtx set, rtx_insn *insn)
258 int did_change = 0;
259 int dreg;
260 rtx src;
261 reg_class_t dclass;
262 int old_cost;
263 cselib_val *val;
264 struct elt_loc_list *l;
265 #ifdef LOAD_EXTEND_OP
266 enum rtx_code extend_op = UNKNOWN;
267 #endif
268 bool speed = optimize_bb_for_speed_p (BLOCK_FOR_INSN (insn));
270 dreg = true_regnum (SET_DEST (set));
271 if (dreg < 0)
272 return 0;
274 src = SET_SRC (set);
275 if (side_effects_p (src) || true_regnum (src) >= 0)
276 return 0;
278 dclass = REGNO_REG_CLASS (dreg);
280 #ifdef LOAD_EXTEND_OP
281 /* When replacing a memory with a register, we need to honor assumptions
282 that combine made wrt the contents of sign bits. We'll do this by
283 generating an extend instruction instead of a reg->reg copy. Thus
284 the destination must be a register that we can widen. */
285 if (MEM_P (src)
286 && GET_MODE_BITSIZE (GET_MODE (src)) < BITS_PER_WORD
287 && (extend_op = LOAD_EXTEND_OP (GET_MODE (src))) != UNKNOWN
288 && !REG_P (SET_DEST (set)))
289 return 0;
290 #endif
292 val = cselib_lookup (src, GET_MODE (SET_DEST (set)), 0, VOIDmode);
293 if (! val)
294 return 0;
296 /* If memory loads are cheaper than register copies, don't change them. */
297 if (MEM_P (src))
298 old_cost = memory_move_cost (GET_MODE (src), dclass, true);
299 else if (REG_P (src))
300 old_cost = register_move_cost (GET_MODE (src),
301 REGNO_REG_CLASS (REGNO (src)), dclass);
302 else
303 old_cost = set_src_cost (src, speed);
305 for (l = val->locs; l; l = l->next)
307 rtx this_rtx = l->loc;
308 int this_cost;
310 if (CONSTANT_P (this_rtx) && ! references_value_p (this_rtx, 0))
312 #ifdef LOAD_EXTEND_OP
313 if (extend_op != UNKNOWN)
315 wide_int result;
317 if (!CONST_SCALAR_INT_P (this_rtx))
318 continue;
320 switch (extend_op)
322 case ZERO_EXTEND:
323 result = wide_int::from (std::make_pair (this_rtx,
324 GET_MODE (src)),
325 BITS_PER_WORD, UNSIGNED);
326 break;
327 case SIGN_EXTEND:
328 result = wide_int::from (std::make_pair (this_rtx,
329 GET_MODE (src)),
330 BITS_PER_WORD, SIGNED);
331 break;
332 default:
333 gcc_unreachable ();
335 this_rtx = immed_wide_int_const (result, word_mode);
337 #endif
338 this_cost = set_src_cost (this_rtx, speed);
340 else if (REG_P (this_rtx))
342 #ifdef LOAD_EXTEND_OP
343 if (extend_op != UNKNOWN)
345 this_rtx = gen_rtx_fmt_e (extend_op, word_mode, this_rtx);
346 this_cost = set_src_cost (this_rtx, speed);
348 else
349 #endif
350 this_cost = register_move_cost (GET_MODE (this_rtx),
351 REGNO_REG_CLASS (REGNO (this_rtx)),
352 dclass);
354 else
355 continue;
357 /* If equal costs, prefer registers over anything else. That
358 tends to lead to smaller instructions on some machines. */
359 if (this_cost < old_cost
360 || (this_cost == old_cost
361 && REG_P (this_rtx)
362 && !REG_P (SET_SRC (set))))
364 #ifdef LOAD_EXTEND_OP
365 if (GET_MODE_BITSIZE (GET_MODE (SET_DEST (set))) < BITS_PER_WORD
366 && extend_op != UNKNOWN
367 #ifdef CANNOT_CHANGE_MODE_CLASS
368 && !CANNOT_CHANGE_MODE_CLASS (GET_MODE (SET_DEST (set)),
369 word_mode,
370 REGNO_REG_CLASS (REGNO (SET_DEST (set))))
371 #endif
374 rtx wide_dest = gen_rtx_REG (word_mode, REGNO (SET_DEST (set)));
375 ORIGINAL_REGNO (wide_dest) = ORIGINAL_REGNO (SET_DEST (set));
376 validate_change (insn, &SET_DEST (set), wide_dest, 1);
378 #endif
380 validate_unshare_change (insn, &SET_SRC (set), this_rtx, 1);
381 old_cost = this_cost, did_change = 1;
385 return did_change;
388 /* Try to replace operands in INSN with equivalent values that are already
389 in registers. This can be viewed as optional reloading.
391 For each non-register operand in the insn, see if any hard regs are
392 known to be equivalent to that operand. Record the alternatives which
393 can accept these hard registers. Among all alternatives, select the
394 ones which are better or equal to the one currently matching, where
395 "better" is in terms of '?' and '!' constraints. Among the remaining
396 alternatives, select the one which replaces most operands with
397 hard registers. */
399 static int
400 reload_cse_simplify_operands (rtx_insn *insn, rtx testreg)
402 int i, j;
404 /* For each operand, all registers that are equivalent to it. */
405 HARD_REG_SET equiv_regs[MAX_RECOG_OPERANDS];
407 const char *constraints[MAX_RECOG_OPERANDS];
409 /* Vector recording how bad an alternative is. */
410 int *alternative_reject;
411 /* Vector recording how many registers can be introduced by choosing
412 this alternative. */
413 int *alternative_nregs;
414 /* Array of vectors recording, for each operand and each alternative,
415 which hard register to substitute, or -1 if the operand should be
416 left as it is. */
417 int *op_alt_regno[MAX_RECOG_OPERANDS];
418 /* Array of alternatives, sorted in order of decreasing desirability. */
419 int *alternative_order;
421 extract_constrain_insn (insn);
423 if (recog_data.n_alternatives == 0 || recog_data.n_operands == 0)
424 return 0;
426 alternative_reject = XALLOCAVEC (int, recog_data.n_alternatives);
427 alternative_nregs = XALLOCAVEC (int, recog_data.n_alternatives);
428 alternative_order = XALLOCAVEC (int, recog_data.n_alternatives);
429 memset (alternative_reject, 0, recog_data.n_alternatives * sizeof (int));
430 memset (alternative_nregs, 0, recog_data.n_alternatives * sizeof (int));
432 /* For each operand, find out which regs are equivalent. */
433 for (i = 0; i < recog_data.n_operands; i++)
435 cselib_val *v;
436 struct elt_loc_list *l;
437 rtx op;
439 CLEAR_HARD_REG_SET (equiv_regs[i]);
441 /* cselib blows up on CODE_LABELs. Trying to fix that doesn't seem
442 right, so avoid the problem here. Likewise if we have a constant
443 and the insn pattern doesn't tell us the mode we need. */
444 if (LABEL_P (recog_data.operand[i])
445 || (CONSTANT_P (recog_data.operand[i])
446 && recog_data.operand_mode[i] == VOIDmode))
447 continue;
449 op = recog_data.operand[i];
450 #ifdef LOAD_EXTEND_OP
451 if (MEM_P (op)
452 && GET_MODE_BITSIZE (GET_MODE (op)) < BITS_PER_WORD
453 && LOAD_EXTEND_OP (GET_MODE (op)) != UNKNOWN)
455 rtx set = single_set (insn);
457 /* We might have multiple sets, some of which do implicit
458 extension. Punt on this for now. */
459 if (! set)
460 continue;
461 /* If the destination is also a MEM or a STRICT_LOW_PART, no
462 extension applies.
463 Also, if there is an explicit extension, we don't have to
464 worry about an implicit one. */
465 else if (MEM_P (SET_DEST (set))
466 || GET_CODE (SET_DEST (set)) == STRICT_LOW_PART
467 || GET_CODE (SET_SRC (set)) == ZERO_EXTEND
468 || GET_CODE (SET_SRC (set)) == SIGN_EXTEND)
469 ; /* Continue ordinary processing. */
470 #ifdef CANNOT_CHANGE_MODE_CLASS
471 /* If the register cannot change mode to word_mode, it follows that
472 it cannot have been used in word_mode. */
473 else if (REG_P (SET_DEST (set))
474 && CANNOT_CHANGE_MODE_CLASS (GET_MODE (SET_DEST (set)),
475 word_mode,
476 REGNO_REG_CLASS (REGNO (SET_DEST (set)))))
477 ; /* Continue ordinary processing. */
478 #endif
479 /* If this is a straight load, make the extension explicit. */
480 else if (REG_P (SET_DEST (set))
481 && recog_data.n_operands == 2
482 && SET_SRC (set) == op
483 && SET_DEST (set) == recog_data.operand[1-i])
485 validate_change (insn, recog_data.operand_loc[i],
486 gen_rtx_fmt_e (LOAD_EXTEND_OP (GET_MODE (op)),
487 word_mode, op),
489 validate_change (insn, recog_data.operand_loc[1-i],
490 gen_rtx_REG (word_mode, REGNO (SET_DEST (set))),
492 if (! apply_change_group ())
493 return 0;
494 return reload_cse_simplify_operands (insn, testreg);
496 else
497 /* ??? There might be arithmetic operations with memory that are
498 safe to optimize, but is it worth the trouble? */
499 continue;
501 #endif /* LOAD_EXTEND_OP */
502 if (side_effects_p (op))
503 continue;
504 v = cselib_lookup (op, recog_data.operand_mode[i], 0, VOIDmode);
505 if (! v)
506 continue;
508 for (l = v->locs; l; l = l->next)
509 if (REG_P (l->loc))
510 SET_HARD_REG_BIT (equiv_regs[i], REGNO (l->loc));
513 alternative_mask preferred = get_preferred_alternatives (insn);
514 for (i = 0; i < recog_data.n_operands; i++)
516 machine_mode mode;
517 int regno;
518 const char *p;
520 op_alt_regno[i] = XALLOCAVEC (int, recog_data.n_alternatives);
521 for (j = 0; j < recog_data.n_alternatives; j++)
522 op_alt_regno[i][j] = -1;
524 p = constraints[i] = recog_data.constraints[i];
525 mode = recog_data.operand_mode[i];
527 /* Add the reject values for each alternative given by the constraints
528 for this operand. */
529 j = 0;
530 while (*p != '\0')
532 char c = *p++;
533 if (c == ',')
534 j++;
535 else if (c == '?')
536 alternative_reject[j] += 3;
537 else if (c == '!')
538 alternative_reject[j] += 300;
541 /* We won't change operands which are already registers. We
542 also don't want to modify output operands. */
543 regno = true_regnum (recog_data.operand[i]);
544 if (regno >= 0
545 || constraints[i][0] == '='
546 || constraints[i][0] == '+')
547 continue;
549 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
551 enum reg_class rclass = NO_REGS;
553 if (! TEST_HARD_REG_BIT (equiv_regs[i], regno))
554 continue;
556 set_mode_and_regno (testreg, mode, regno);
558 /* We found a register equal to this operand. Now look for all
559 alternatives that can accept this register and have not been
560 assigned a register they can use yet. */
561 j = 0;
562 p = constraints[i];
563 for (;;)
565 char c = *p;
567 switch (c)
569 case 'g':
570 rclass = reg_class_subunion[rclass][GENERAL_REGS];
571 break;
573 default:
574 rclass
575 = (reg_class_subunion
576 [rclass]
577 [reg_class_for_constraint (lookup_constraint (p))]);
578 break;
580 case ',': case '\0':
581 /* See if REGNO fits this alternative, and set it up as the
582 replacement register if we don't have one for this
583 alternative yet and the operand being replaced is not
584 a cheap CONST_INT. */
585 if (op_alt_regno[i][j] == -1
586 && TEST_BIT (preferred, j)
587 && reg_fits_class_p (testreg, rclass, 0, mode)
588 && (!CONST_INT_P (recog_data.operand[i])
589 || (set_src_cost (recog_data.operand[i],
590 optimize_bb_for_speed_p
591 (BLOCK_FOR_INSN (insn)))
592 > set_src_cost (testreg,
593 optimize_bb_for_speed_p
594 (BLOCK_FOR_INSN (insn))))))
596 alternative_nregs[j]++;
597 op_alt_regno[i][j] = regno;
599 j++;
600 rclass = NO_REGS;
601 break;
603 p += CONSTRAINT_LEN (c, p);
605 if (c == '\0')
606 break;
611 /* Record all alternatives which are better or equal to the currently
612 matching one in the alternative_order array. */
613 for (i = j = 0; i < recog_data.n_alternatives; i++)
614 if (alternative_reject[i] <= alternative_reject[which_alternative])
615 alternative_order[j++] = i;
616 recog_data.n_alternatives = j;
618 /* Sort it. Given a small number of alternatives, a dumb algorithm
619 won't hurt too much. */
620 for (i = 0; i < recog_data.n_alternatives - 1; i++)
622 int best = i;
623 int best_reject = alternative_reject[alternative_order[i]];
624 int best_nregs = alternative_nregs[alternative_order[i]];
626 for (j = i + 1; j < recog_data.n_alternatives; j++)
628 int this_reject = alternative_reject[alternative_order[j]];
629 int this_nregs = alternative_nregs[alternative_order[j]];
631 if (this_reject < best_reject
632 || (this_reject == best_reject && this_nregs > best_nregs))
634 best = j;
635 best_reject = this_reject;
636 best_nregs = this_nregs;
640 std::swap (alternative_order[best], alternative_order[i]);
643 /* Substitute the operands as determined by op_alt_regno for the best
644 alternative. */
645 j = alternative_order[0];
647 for (i = 0; i < recog_data.n_operands; i++)
649 machine_mode mode = recog_data.operand_mode[i];
650 if (op_alt_regno[i][j] == -1)
651 continue;
653 validate_change (insn, recog_data.operand_loc[i],
654 gen_rtx_REG (mode, op_alt_regno[i][j]), 1);
657 for (i = recog_data.n_dups - 1; i >= 0; i--)
659 int op = recog_data.dup_num[i];
660 machine_mode mode = recog_data.operand_mode[op];
662 if (op_alt_regno[op][j] == -1)
663 continue;
665 validate_change (insn, recog_data.dup_loc[i],
666 gen_rtx_REG (mode, op_alt_regno[op][j]), 1);
669 return apply_change_group ();
672 /* If reload couldn't use reg+reg+offset addressing, try to use reg+reg
673 addressing now.
674 This code might also be useful when reload gave up on reg+reg addressing
675 because of clashes between the return register and INDEX_REG_CLASS. */
677 /* The maximum number of uses of a register we can keep track of to
678 replace them with reg+reg addressing. */
679 #define RELOAD_COMBINE_MAX_USES 16
681 /* Describes a recorded use of a register. */
682 struct reg_use
684 /* The insn where a register has been used. */
685 rtx_insn *insn;
686 /* Points to the memory reference enclosing the use, if any, NULL_RTX
687 otherwise. */
688 rtx containing_mem;
689 /* Location of the register within INSN. */
690 rtx *usep;
691 /* The reverse uid of the insn. */
692 int ruid;
695 /* If the register is used in some unknown fashion, USE_INDEX is negative.
696 If it is dead, USE_INDEX is RELOAD_COMBINE_MAX_USES, and STORE_RUID
697 indicates where it is first set or clobbered.
698 Otherwise, USE_INDEX is the index of the last encountered use of the
699 register (which is first among these we have seen since we scan backwards).
700 USE_RUID indicates the first encountered, i.e. last, of these uses.
701 If ALL_OFFSETS_MATCH is true, all encountered uses were inside a PLUS
702 with a constant offset; OFFSET contains this constant in that case.
703 STORE_RUID is always meaningful if we only want to use a value in a
704 register in a different place: it denotes the next insn in the insn
705 stream (i.e. the last encountered) that sets or clobbers the register.
706 REAL_STORE_RUID is similar, but clobbers are ignored when updating it. */
707 static struct
709 struct reg_use reg_use[RELOAD_COMBINE_MAX_USES];
710 rtx offset;
711 int use_index;
712 int store_ruid;
713 int real_store_ruid;
714 int use_ruid;
715 bool all_offsets_match;
716 } reg_state[FIRST_PSEUDO_REGISTER];
718 /* Reverse linear uid. This is increased in reload_combine while scanning
719 the instructions from last to first. It is used to set last_label_ruid
720 and the store_ruid / use_ruid fields in reg_state. */
721 static int reload_combine_ruid;
723 /* The RUID of the last label we encountered in reload_combine. */
724 static int last_label_ruid;
726 /* The RUID of the last jump we encountered in reload_combine. */
727 static int last_jump_ruid;
729 /* The register numbers of the first and last index register. A value of
730 -1 in LAST_INDEX_REG indicates that we've previously computed these
731 values and found no suitable index registers. */
732 static int first_index_reg = -1;
733 static int last_index_reg;
735 #define LABEL_LIVE(LABEL) \
736 (label_live[CODE_LABEL_NUMBER (LABEL) - min_labelno])
738 /* Subroutine of reload_combine_split_ruids, called to fix up a single
739 ruid pointed to by *PRUID if it is higher than SPLIT_RUID. */
741 static inline void
742 reload_combine_split_one_ruid (int *pruid, int split_ruid)
744 if (*pruid > split_ruid)
745 (*pruid)++;
748 /* Called when we insert a new insn in a position we've already passed in
749 the scan. Examine all our state, increasing all ruids that are higher
750 than SPLIT_RUID by one in order to make room for a new insn. */
752 static void
753 reload_combine_split_ruids (int split_ruid)
755 unsigned i;
757 reload_combine_split_one_ruid (&reload_combine_ruid, split_ruid);
758 reload_combine_split_one_ruid (&last_label_ruid, split_ruid);
759 reload_combine_split_one_ruid (&last_jump_ruid, split_ruid);
761 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
763 int j, idx = reg_state[i].use_index;
764 reload_combine_split_one_ruid (&reg_state[i].use_ruid, split_ruid);
765 reload_combine_split_one_ruid (&reg_state[i].store_ruid, split_ruid);
766 reload_combine_split_one_ruid (&reg_state[i].real_store_ruid,
767 split_ruid);
768 if (idx < 0)
769 continue;
770 for (j = idx; j < RELOAD_COMBINE_MAX_USES; j++)
772 reload_combine_split_one_ruid (&reg_state[i].reg_use[j].ruid,
773 split_ruid);
778 /* Called when we are about to rescan a previously encountered insn with
779 reload_combine_note_use after modifying some part of it. This clears all
780 information about uses in that particular insn. */
782 static void
783 reload_combine_purge_insn_uses (rtx_insn *insn)
785 unsigned i;
787 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
789 int j, k, idx = reg_state[i].use_index;
790 if (idx < 0)
791 continue;
792 j = k = RELOAD_COMBINE_MAX_USES;
793 while (j-- > idx)
795 if (reg_state[i].reg_use[j].insn != insn)
797 k--;
798 if (k != j)
799 reg_state[i].reg_use[k] = reg_state[i].reg_use[j];
802 reg_state[i].use_index = k;
806 /* Called when we need to forget about all uses of REGNO after an insn
807 which is identified by RUID. */
809 static void
810 reload_combine_purge_reg_uses_after_ruid (unsigned regno, int ruid)
812 int j, k, idx = reg_state[regno].use_index;
813 if (idx < 0)
814 return;
815 j = k = RELOAD_COMBINE_MAX_USES;
816 while (j-- > idx)
818 if (reg_state[regno].reg_use[j].ruid >= ruid)
820 k--;
821 if (k != j)
822 reg_state[regno].reg_use[k] = reg_state[regno].reg_use[j];
825 reg_state[regno].use_index = k;
828 /* Find the use of REGNO with the ruid that is highest among those
829 lower than RUID_LIMIT, and return it if it is the only use of this
830 reg in the insn. Return NULL otherwise. */
832 static struct reg_use *
833 reload_combine_closest_single_use (unsigned regno, int ruid_limit)
835 int i, best_ruid = 0;
836 int use_idx = reg_state[regno].use_index;
837 struct reg_use *retval;
839 if (use_idx < 0)
840 return NULL;
841 retval = NULL;
842 for (i = use_idx; i < RELOAD_COMBINE_MAX_USES; i++)
844 struct reg_use *use = reg_state[regno].reg_use + i;
845 int this_ruid = use->ruid;
846 if (this_ruid >= ruid_limit)
847 continue;
848 if (this_ruid > best_ruid)
850 best_ruid = this_ruid;
851 retval = use;
853 else if (this_ruid == best_ruid)
854 retval = NULL;
856 if (last_label_ruid >= best_ruid)
857 return NULL;
858 return retval;
861 /* After we've moved an add insn, fix up any debug insns that occur
862 between the old location of the add and the new location. REG is
863 the destination register of the add insn; REPLACEMENT is the
864 SET_SRC of the add. FROM and TO specify the range in which we
865 should make this change on debug insns. */
867 static void
868 fixup_debug_insns (rtx reg, rtx replacement, rtx_insn *from, rtx_insn *to)
870 rtx_insn *insn;
871 for (insn = from; insn != to; insn = NEXT_INSN (insn))
873 rtx t;
875 if (!DEBUG_INSN_P (insn))
876 continue;
878 t = INSN_VAR_LOCATION_LOC (insn);
879 t = simplify_replace_rtx (t, reg, replacement);
880 validate_change (insn, &INSN_VAR_LOCATION_LOC (insn), t, 0);
884 /* Subroutine of reload_combine_recognize_const_pattern. Try to replace REG
885 with SRC in the insn described by USE, taking costs into account. Return
886 true if we made the replacement. */
888 static bool
889 try_replace_in_use (struct reg_use *use, rtx reg, rtx src)
891 rtx_insn *use_insn = use->insn;
892 rtx mem = use->containing_mem;
893 bool speed = optimize_bb_for_speed_p (BLOCK_FOR_INSN (use_insn));
895 if (mem != NULL_RTX)
897 addr_space_t as = MEM_ADDR_SPACE (mem);
898 rtx oldaddr = XEXP (mem, 0);
899 rtx newaddr = NULL_RTX;
900 int old_cost = address_cost (oldaddr, GET_MODE (mem), as, speed);
901 int new_cost;
903 newaddr = simplify_replace_rtx (oldaddr, reg, src);
904 if (memory_address_addr_space_p (GET_MODE (mem), newaddr, as))
906 XEXP (mem, 0) = newaddr;
907 new_cost = address_cost (newaddr, GET_MODE (mem), as, speed);
908 XEXP (mem, 0) = oldaddr;
909 if (new_cost <= old_cost
910 && validate_change (use_insn,
911 &XEXP (mem, 0), newaddr, 0))
912 return true;
915 else
917 rtx new_set = single_set (use_insn);
918 if (new_set
919 && REG_P (SET_DEST (new_set))
920 && GET_CODE (SET_SRC (new_set)) == PLUS
921 && REG_P (XEXP (SET_SRC (new_set), 0))
922 && CONSTANT_P (XEXP (SET_SRC (new_set), 1)))
924 rtx new_src;
925 int old_cost = set_src_cost (SET_SRC (new_set), speed);
927 gcc_assert (rtx_equal_p (XEXP (SET_SRC (new_set), 0), reg));
928 new_src = simplify_replace_rtx (SET_SRC (new_set), reg, src);
930 if (set_src_cost (new_src, speed) <= old_cost
931 && validate_change (use_insn, &SET_SRC (new_set),
932 new_src, 0))
933 return true;
936 return false;
939 /* Called by reload_combine when scanning INSN. This function tries to detect
940 patterns where a constant is added to a register, and the result is used
941 in an address.
942 Return true if no further processing is needed on INSN; false if it wasn't
943 recognized and should be handled normally. */
945 static bool
946 reload_combine_recognize_const_pattern (rtx_insn *insn)
948 int from_ruid = reload_combine_ruid;
949 rtx set, pat, reg, src, addreg;
950 unsigned int regno;
951 struct reg_use *use;
952 bool must_move_add;
953 rtx_insn *add_moved_after_insn = NULL;
954 int add_moved_after_ruid = 0;
955 int clobbered_regno = -1;
957 set = single_set (insn);
958 if (set == NULL_RTX)
959 return false;
961 reg = SET_DEST (set);
962 src = SET_SRC (set);
963 if (!REG_P (reg)
964 || REG_NREGS (reg) != 1
965 || GET_MODE (reg) != Pmode
966 || reg == stack_pointer_rtx)
967 return false;
969 regno = REGNO (reg);
971 /* We look for a REG1 = REG2 + CONSTANT insn, followed by either
972 uses of REG1 inside an address, or inside another add insn. If
973 possible and profitable, merge the addition into subsequent
974 uses. */
975 if (GET_CODE (src) != PLUS
976 || !REG_P (XEXP (src, 0))
977 || !CONSTANT_P (XEXP (src, 1)))
978 return false;
980 addreg = XEXP (src, 0);
981 must_move_add = rtx_equal_p (reg, addreg);
983 pat = PATTERN (insn);
984 if (must_move_add && set != pat)
986 /* We have to be careful when moving the add; apart from the
987 single_set there may also be clobbers. Recognize one special
988 case, that of one clobber alongside the set (likely a clobber
989 of the CC register). */
990 gcc_assert (GET_CODE (PATTERN (insn)) == PARALLEL);
991 if (XVECLEN (pat, 0) != 2 || XVECEXP (pat, 0, 0) != set
992 || GET_CODE (XVECEXP (pat, 0, 1)) != CLOBBER
993 || !REG_P (XEXP (XVECEXP (pat, 0, 1), 0)))
994 return false;
995 clobbered_regno = REGNO (XEXP (XVECEXP (pat, 0, 1), 0));
1000 use = reload_combine_closest_single_use (regno, from_ruid);
1002 if (use)
1003 /* Start the search for the next use from here. */
1004 from_ruid = use->ruid;
1006 if (use && GET_MODE (*use->usep) == Pmode)
1008 bool delete_add = false;
1009 rtx_insn *use_insn = use->insn;
1010 int use_ruid = use->ruid;
1012 /* Avoid moving the add insn past a jump. */
1013 if (must_move_add && use_ruid <= last_jump_ruid)
1014 break;
1016 /* If the add clobbers another hard reg in parallel, don't move
1017 it past a real set of this hard reg. */
1018 if (must_move_add && clobbered_regno >= 0
1019 && reg_state[clobbered_regno].real_store_ruid >= use_ruid)
1020 break;
1022 /* Do not separate cc0 setter and cc0 user on HAVE_cc0 targets. */
1023 if (HAVE_cc0 && must_move_add && sets_cc0_p (PATTERN (use_insn)))
1024 break;
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) || REG_NREGS (reg) != 1)
1100 return false;
1102 regno = REGNO (reg);
1104 /* Look for (set (REGX) (CONST_INT))
1105 (set (REGX) (PLUS (REGX) (REGY)))
1107 ... (MEM (REGX)) ...
1108 and convert it to
1109 (set (REGZ) (CONST_INT))
1111 ... (MEM (PLUS (REGZ) (REGY)))... .
1113 First, check that we have (set (REGX) (PLUS (REGX) (REGY)))
1114 and that we know all uses of REGX before it dies.
1115 Also, explicitly check that REGX != REGY; our life information
1116 does not yet show whether REGY changes in this insn. */
1118 if (GET_CODE (src) == PLUS
1119 && reg_state[regno].all_offsets_match
1120 && last_index_reg != -1
1121 && REG_P (XEXP (src, 1))
1122 && rtx_equal_p (XEXP (src, 0), reg)
1123 && !rtx_equal_p (XEXP (src, 1), reg)
1124 && reg_state[regno].use_index >= 0
1125 && reg_state[regno].use_index < RELOAD_COMBINE_MAX_USES
1126 && last_label_ruid < reg_state[regno].use_ruid)
1128 rtx base = XEXP (src, 1);
1129 rtx_insn *prev = prev_nonnote_nondebug_insn (insn);
1130 rtx prev_set = prev ? single_set (prev) : NULL_RTX;
1131 rtx index_reg = NULL_RTX;
1132 rtx reg_sum = NULL_RTX;
1133 int i;
1135 /* Now we need to set INDEX_REG to an index register (denoted as
1136 REGZ in the illustration above) and REG_SUM to the expression
1137 register+register that we want to use to substitute uses of REG
1138 (typically in MEMs) with. First check REG and BASE for being
1139 index registers; we can use them even if they are not dead. */
1140 if (TEST_HARD_REG_BIT (reg_class_contents[INDEX_REG_CLASS], regno)
1141 || TEST_HARD_REG_BIT (reg_class_contents[INDEX_REG_CLASS],
1142 REGNO (base)))
1144 index_reg = reg;
1145 reg_sum = src;
1147 else
1149 /* Otherwise, look for a free index register. Since we have
1150 checked above that neither REG nor BASE are index registers,
1151 if we find anything at all, it will be different from these
1152 two registers. */
1153 for (i = first_index_reg; i <= last_index_reg; i++)
1155 if (TEST_HARD_REG_BIT (reg_class_contents[INDEX_REG_CLASS], i)
1156 && reg_state[i].use_index == RELOAD_COMBINE_MAX_USES
1157 && reg_state[i].store_ruid <= reg_state[regno].use_ruid
1158 && (call_used_regs[i] || df_regs_ever_live_p (i))
1159 && (!frame_pointer_needed || i != HARD_FRAME_POINTER_REGNUM)
1160 && !fixed_regs[i] && !global_regs[i]
1161 && hard_regno_nregs[i][GET_MODE (reg)] == 1
1162 && targetm.hard_regno_scratch_ok (i))
1164 index_reg = gen_rtx_REG (GET_MODE (reg), i);
1165 reg_sum = gen_rtx_PLUS (GET_MODE (reg), index_reg, base);
1166 break;
1171 /* Check that PREV_SET is indeed (set (REGX) (CONST_INT)) and that
1172 (REGY), i.e. BASE, is not clobbered before the last use we'll
1173 create. */
1174 if (reg_sum
1175 && prev_set
1176 && CONST_INT_P (SET_SRC (prev_set))
1177 && rtx_equal_p (SET_DEST (prev_set), reg)
1178 && (reg_state[REGNO (base)].store_ruid
1179 <= reg_state[regno].use_ruid))
1181 /* Change destination register and, if necessary, the constant
1182 value in PREV, the constant loading instruction. */
1183 validate_change (prev, &SET_DEST (prev_set), index_reg, 1);
1184 if (reg_state[regno].offset != const0_rtx)
1185 validate_change (prev,
1186 &SET_SRC (prev_set),
1187 GEN_INT (INTVAL (SET_SRC (prev_set))
1188 + INTVAL (reg_state[regno].offset)),
1191 /* Now for every use of REG that we have recorded, replace REG
1192 with REG_SUM. */
1193 for (i = reg_state[regno].use_index;
1194 i < RELOAD_COMBINE_MAX_USES; i++)
1195 validate_unshare_change (reg_state[regno].reg_use[i].insn,
1196 reg_state[regno].reg_use[i].usep,
1197 /* Each change must have its own
1198 replacement. */
1199 reg_sum, 1);
1201 if (apply_change_group ())
1203 struct reg_use *lowest_ruid = NULL;
1205 /* For every new use of REG_SUM, we have to record the use
1206 of BASE therein, i.e. operand 1. */
1207 for (i = reg_state[regno].use_index;
1208 i < RELOAD_COMBINE_MAX_USES; i++)
1210 struct reg_use *use = reg_state[regno].reg_use + i;
1211 reload_combine_note_use (&XEXP (*use->usep, 1), use->insn,
1212 use->ruid, use->containing_mem);
1213 if (lowest_ruid == NULL || use->ruid < lowest_ruid->ruid)
1214 lowest_ruid = use;
1217 fixup_debug_insns (reg, reg_sum, insn, lowest_ruid->insn);
1219 /* Delete the reg-reg addition. */
1220 delete_insn (insn);
1222 if (reg_state[regno].offset != const0_rtx)
1223 /* Previous REG_EQUIV / REG_EQUAL notes for PREV
1224 are now invalid. */
1225 remove_reg_equal_equiv_notes (prev);
1227 reg_state[regno].use_index = RELOAD_COMBINE_MAX_USES;
1228 return true;
1232 return false;
1235 static void
1236 reload_combine (void)
1238 rtx_insn *insn, *prev;
1239 basic_block bb;
1240 unsigned int r;
1241 int min_labelno, n_labels;
1242 HARD_REG_SET ever_live_at_start, *label_live;
1244 /* To avoid wasting too much time later searching for an index register,
1245 determine the minimum and maximum index register numbers. */
1246 if (INDEX_REG_CLASS == NO_REGS)
1247 last_index_reg = -1;
1248 else if (first_index_reg == -1 && last_index_reg == 0)
1250 for (r = 0; r < FIRST_PSEUDO_REGISTER; r++)
1251 if (TEST_HARD_REG_BIT (reg_class_contents[INDEX_REG_CLASS], r))
1253 if (first_index_reg == -1)
1254 first_index_reg = r;
1256 last_index_reg = r;
1259 /* If no index register is available, we can quit now. Set LAST_INDEX_REG
1260 to -1 so we'll know to quit early the next time we get here. */
1261 if (first_index_reg == -1)
1263 last_index_reg = -1;
1264 return;
1268 /* Set up LABEL_LIVE and EVER_LIVE_AT_START. The register lifetime
1269 information is a bit fuzzy immediately after reload, but it's
1270 still good enough to determine which registers are live at a jump
1271 destination. */
1272 min_labelno = get_first_label_num ();
1273 n_labels = max_label_num () - min_labelno;
1274 label_live = XNEWVEC (HARD_REG_SET, n_labels);
1275 CLEAR_HARD_REG_SET (ever_live_at_start);
1277 FOR_EACH_BB_REVERSE_FN (bb, cfun)
1279 insn = BB_HEAD (bb);
1280 if (LABEL_P (insn))
1282 HARD_REG_SET live;
1283 bitmap live_in = df_get_live_in (bb);
1285 REG_SET_TO_HARD_REG_SET (live, live_in);
1286 compute_use_by_pseudos (&live, live_in);
1287 COPY_HARD_REG_SET (LABEL_LIVE (insn), live);
1288 IOR_HARD_REG_SET (ever_live_at_start, live);
1292 /* Initialize last_label_ruid, reload_combine_ruid and reg_state. */
1293 last_label_ruid = last_jump_ruid = reload_combine_ruid = 0;
1294 for (r = 0; r < FIRST_PSEUDO_REGISTER; r++)
1296 reg_state[r].store_ruid = 0;
1297 reg_state[r].real_store_ruid = 0;
1298 if (fixed_regs[r])
1299 reg_state[r].use_index = -1;
1300 else
1301 reg_state[r].use_index = RELOAD_COMBINE_MAX_USES;
1304 for (insn = get_last_insn (); insn; insn = prev)
1306 bool control_flow_insn;
1307 rtx note;
1309 prev = PREV_INSN (insn);
1311 /* We cannot do our optimization across labels. Invalidating all the use
1312 information we have would be costly, so we just note where the label
1313 is and then later disable any optimization that would cross it. */
1314 if (LABEL_P (insn))
1315 last_label_ruid = reload_combine_ruid;
1316 else if (BARRIER_P (insn))
1318 /* Crossing a barrier resets all the use information. */
1319 for (r = 0; r < FIRST_PSEUDO_REGISTER; r++)
1320 if (! fixed_regs[r])
1321 reg_state[r].use_index = RELOAD_COMBINE_MAX_USES;
1323 else if (INSN_P (insn) && volatile_insn_p (PATTERN (insn)))
1324 /* Optimizations across insns being marked as volatile must be
1325 prevented. All the usage information is invalidated
1326 here. */
1327 for (r = 0; r < FIRST_PSEUDO_REGISTER; r++)
1328 if (! fixed_regs[r]
1329 && reg_state[r].use_index != RELOAD_COMBINE_MAX_USES)
1330 reg_state[r].use_index = -1;
1332 if (! NONDEBUG_INSN_P (insn))
1333 continue;
1335 reload_combine_ruid++;
1337 control_flow_insn = control_flow_insn_p (insn);
1338 if (control_flow_insn)
1339 last_jump_ruid = reload_combine_ruid;
1341 if (reload_combine_recognize_const_pattern (insn)
1342 || reload_combine_recognize_pattern (insn))
1343 continue;
1345 note_stores (PATTERN (insn), reload_combine_note_store, NULL);
1347 if (CALL_P (insn))
1349 rtx link;
1350 HARD_REG_SET used_regs;
1352 get_call_reg_set_usage (insn, &used_regs, call_used_reg_set);
1354 for (r = 0; r < FIRST_PSEUDO_REGISTER; r++)
1355 if (TEST_HARD_REG_BIT (used_regs, r))
1357 reg_state[r].use_index = RELOAD_COMBINE_MAX_USES;
1358 reg_state[r].store_ruid = reload_combine_ruid;
1361 for (link = CALL_INSN_FUNCTION_USAGE (insn); link;
1362 link = XEXP (link, 1))
1364 rtx setuse = XEXP (link, 0);
1365 rtx usage_rtx = XEXP (setuse, 0);
1366 if ((GET_CODE (setuse) == USE || GET_CODE (setuse) == CLOBBER)
1367 && REG_P (usage_rtx))
1369 unsigned int end_regno = END_REGNO (usage_rtx);
1370 for (unsigned int i = REGNO (usage_rtx); i < end_regno; ++i)
1371 if (GET_CODE (XEXP (link, 0)) == CLOBBER)
1373 reg_state[i].use_index = RELOAD_COMBINE_MAX_USES;
1374 reg_state[i].store_ruid = reload_combine_ruid;
1376 else
1377 reg_state[i].use_index = -1;
1382 if (control_flow_insn && !ANY_RETURN_P (PATTERN (insn)))
1384 /* Non-spill registers might be used at the call destination in
1385 some unknown fashion, so we have to mark the unknown use. */
1386 HARD_REG_SET *live;
1388 if ((condjump_p (insn) || condjump_in_parallel_p (insn))
1389 && JUMP_LABEL (insn))
1391 if (ANY_RETURN_P (JUMP_LABEL (insn)))
1392 live = NULL;
1393 else
1394 live = &LABEL_LIVE (JUMP_LABEL (insn));
1396 else
1397 live = &ever_live_at_start;
1399 if (live)
1400 for (r = 0; r < FIRST_PSEUDO_REGISTER; r++)
1401 if (TEST_HARD_REG_BIT (*live, r))
1402 reg_state[r].use_index = -1;
1405 reload_combine_note_use (&PATTERN (insn), insn, reload_combine_ruid,
1406 NULL_RTX);
1408 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
1410 if (REG_NOTE_KIND (note) == REG_INC && REG_P (XEXP (note, 0)))
1412 int regno = REGNO (XEXP (note, 0));
1413 reg_state[regno].store_ruid = reload_combine_ruid;
1414 reg_state[regno].real_store_ruid = reload_combine_ruid;
1415 reg_state[regno].use_index = -1;
1420 free (label_live);
1423 /* Check if DST is a register or a subreg of a register; if it is,
1424 update store_ruid, real_store_ruid and use_index in the reg_state
1425 structure accordingly. Called via note_stores from reload_combine. */
1427 static void
1428 reload_combine_note_store (rtx dst, const_rtx set, void *data ATTRIBUTE_UNUSED)
1430 int regno = 0;
1431 int i;
1432 machine_mode mode = GET_MODE (dst);
1434 if (GET_CODE (dst) == SUBREG)
1436 regno = subreg_regno_offset (REGNO (SUBREG_REG (dst)),
1437 GET_MODE (SUBREG_REG (dst)),
1438 SUBREG_BYTE (dst),
1439 GET_MODE (dst));
1440 dst = SUBREG_REG (dst);
1443 /* Some targets do argument pushes without adding REG_INC notes. */
1445 if (MEM_P (dst))
1447 dst = XEXP (dst, 0);
1448 if (GET_CODE (dst) == PRE_INC || GET_CODE (dst) == POST_INC
1449 || GET_CODE (dst) == PRE_DEC || GET_CODE (dst) == POST_DEC
1450 || GET_CODE (dst) == PRE_MODIFY || GET_CODE (dst) == POST_MODIFY)
1452 unsigned int end_regno = END_REGNO (XEXP (dst, 0));
1453 for (unsigned int i = REGNO (XEXP (dst, 0)); i < end_regno; ++i)
1455 /* We could probably do better, but for now mark the register
1456 as used in an unknown fashion and set/clobbered at this
1457 insn. */
1458 reg_state[i].use_index = -1;
1459 reg_state[i].store_ruid = reload_combine_ruid;
1460 reg_state[i].real_store_ruid = reload_combine_ruid;
1463 else
1464 return;
1467 if (!REG_P (dst))
1468 return;
1469 regno += REGNO (dst);
1471 /* note_stores might have stripped a STRICT_LOW_PART, so we have to be
1472 careful with registers / register parts that are not full words.
1473 Similarly for ZERO_EXTRACT. */
1474 if (GET_CODE (SET_DEST (set)) == ZERO_EXTRACT
1475 || GET_CODE (SET_DEST (set)) == STRICT_LOW_PART)
1477 for (i = hard_regno_nregs[regno][mode] - 1 + regno; i >= regno; i--)
1479 reg_state[i].use_index = -1;
1480 reg_state[i].store_ruid = reload_combine_ruid;
1481 reg_state[i].real_store_ruid = reload_combine_ruid;
1484 else
1486 for (i = hard_regno_nregs[regno][mode] - 1 + regno; i >= regno; i--)
1488 reg_state[i].store_ruid = reload_combine_ruid;
1489 if (GET_CODE (set) == SET)
1490 reg_state[i].real_store_ruid = reload_combine_ruid;
1491 reg_state[i].use_index = RELOAD_COMBINE_MAX_USES;
1496 /* XP points to a piece of rtl that has to be checked for any uses of
1497 registers.
1498 *XP is the pattern of INSN, or a part of it.
1499 Called from reload_combine, and recursively by itself. */
1500 static void
1501 reload_combine_note_use (rtx *xp, rtx_insn *insn, int ruid, rtx containing_mem)
1503 rtx x = *xp;
1504 enum rtx_code code = x->code;
1505 const char *fmt;
1506 int i, j;
1507 rtx offset = const0_rtx; /* For the REG case below. */
1509 switch (code)
1511 case SET:
1512 if (REG_P (SET_DEST (x)))
1514 reload_combine_note_use (&SET_SRC (x), insn, ruid, NULL_RTX);
1515 return;
1517 break;
1519 case USE:
1520 /* If this is the USE of a return value, we can't change it. */
1521 if (REG_P (XEXP (x, 0)) && REG_FUNCTION_VALUE_P (XEXP (x, 0)))
1523 /* Mark the return register as used in an unknown fashion. */
1524 rtx reg = XEXP (x, 0);
1525 unsigned int end_regno = END_REGNO (reg);
1526 for (unsigned int regno = REGNO (reg); regno < end_regno; ++regno)
1527 reg_state[regno].use_index = -1;
1528 return;
1530 break;
1532 case CLOBBER:
1533 if (REG_P (SET_DEST (x)))
1535 /* No spurious CLOBBERs of pseudo registers may remain. */
1536 gcc_assert (REGNO (SET_DEST (x)) < FIRST_PSEUDO_REGISTER);
1537 return;
1539 break;
1541 case PLUS:
1542 /* We are interested in (plus (reg) (const_int)) . */
1543 if (!REG_P (XEXP (x, 0))
1544 || !CONST_INT_P (XEXP (x, 1)))
1545 break;
1546 offset = XEXP (x, 1);
1547 x = XEXP (x, 0);
1548 /* Fall through. */
1549 case REG:
1551 int regno = REGNO (x);
1552 int use_index;
1553 int nregs;
1555 /* No spurious USEs of pseudo registers may remain. */
1556 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
1558 nregs = REG_NREGS (x);
1560 /* We can't substitute into multi-hard-reg uses. */
1561 if (nregs > 1)
1563 while (--nregs >= 0)
1564 reg_state[regno + nregs].use_index = -1;
1565 return;
1568 /* We may be called to update uses in previously seen insns.
1569 Don't add uses beyond the last store we saw. */
1570 if (ruid < reg_state[regno].store_ruid)
1571 return;
1573 /* If this register is already used in some unknown fashion, we
1574 can't do anything.
1575 If we decrement the index from zero to -1, we can't store more
1576 uses, so this register becomes used in an unknown fashion. */
1577 use_index = --reg_state[regno].use_index;
1578 if (use_index < 0)
1579 return;
1581 if (use_index == RELOAD_COMBINE_MAX_USES - 1)
1583 /* This is the first use of this register we have seen since we
1584 marked it as dead. */
1585 reg_state[regno].offset = offset;
1586 reg_state[regno].all_offsets_match = true;
1587 reg_state[regno].use_ruid = ruid;
1589 else
1591 if (reg_state[regno].use_ruid > ruid)
1592 reg_state[regno].use_ruid = ruid;
1594 if (! rtx_equal_p (offset, reg_state[regno].offset))
1595 reg_state[regno].all_offsets_match = false;
1598 reg_state[regno].reg_use[use_index].insn = insn;
1599 reg_state[regno].reg_use[use_index].ruid = ruid;
1600 reg_state[regno].reg_use[use_index].containing_mem = containing_mem;
1601 reg_state[regno].reg_use[use_index].usep = xp;
1602 return;
1605 case MEM:
1606 containing_mem = x;
1607 break;
1609 default:
1610 break;
1613 /* Recursively process the components of X. */
1614 fmt = GET_RTX_FORMAT (code);
1615 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1617 if (fmt[i] == 'e')
1618 reload_combine_note_use (&XEXP (x, i), insn, ruid, containing_mem);
1619 else if (fmt[i] == 'E')
1621 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1622 reload_combine_note_use (&XVECEXP (x, i, j), insn, ruid,
1623 containing_mem);
1628 /* See if we can reduce the cost of a constant by replacing a move
1629 with an add. We track situations in which a register is set to a
1630 constant or to a register plus a constant. */
1631 /* We cannot do our optimization across labels. Invalidating all the
1632 information about register contents we have would be costly, so we
1633 use move2add_last_label_luid to note where the label is and then
1634 later disable any optimization that would cross it.
1635 reg_offset[n] / reg_base_reg[n] / reg_symbol_ref[n] / reg_mode[n]
1636 are only valid if reg_set_luid[n] is greater than
1637 move2add_last_label_luid.
1638 For a set that established a new (potential) base register with
1639 non-constant value, we use move2add_luid from the place where the
1640 setting insn is encountered; registers based off that base then
1641 get the same reg_set_luid. Constants all get
1642 move2add_last_label_luid + 1 as their reg_set_luid. */
1643 static int reg_set_luid[FIRST_PSEUDO_REGISTER];
1645 /* If reg_base_reg[n] is negative, register n has been set to
1646 reg_offset[n] or reg_symbol_ref[n] + reg_offset[n] in mode reg_mode[n].
1647 If reg_base_reg[n] is non-negative, register n has been set to the
1648 sum of reg_offset[n] and the value of register reg_base_reg[n]
1649 before reg_set_luid[n], calculated in mode reg_mode[n] .
1650 For multi-hard-register registers, all but the first one are
1651 recorded as BLKmode in reg_mode. Setting reg_mode to VOIDmode
1652 marks it as invalid. */
1653 static HOST_WIDE_INT reg_offset[FIRST_PSEUDO_REGISTER];
1654 static int reg_base_reg[FIRST_PSEUDO_REGISTER];
1655 static rtx reg_symbol_ref[FIRST_PSEUDO_REGISTER];
1656 static machine_mode reg_mode[FIRST_PSEUDO_REGISTER];
1658 /* move2add_luid is linearly increased while scanning the instructions
1659 from first to last. It is used to set reg_set_luid in
1660 reload_cse_move2add and move2add_note_store. */
1661 static int move2add_luid;
1663 /* move2add_last_label_luid is set whenever a label is found. Labels
1664 invalidate all previously collected reg_offset data. */
1665 static int move2add_last_label_luid;
1667 /* ??? We don't know how zero / sign extension is handled, hence we
1668 can't go from a narrower to a wider mode. */
1669 #define MODES_OK_FOR_MOVE2ADD(OUTMODE, INMODE) \
1670 (GET_MODE_SIZE (OUTMODE) == GET_MODE_SIZE (INMODE) \
1671 || (GET_MODE_SIZE (OUTMODE) <= GET_MODE_SIZE (INMODE) \
1672 && TRULY_NOOP_TRUNCATION_MODES_P (OUTMODE, INMODE)))
1674 /* Record that REG is being set to a value with the mode of REG. */
1676 static void
1677 move2add_record_mode (rtx reg)
1679 int regno, nregs;
1680 machine_mode mode = GET_MODE (reg);
1682 if (GET_CODE (reg) == SUBREG)
1684 regno = subreg_regno (reg);
1685 nregs = subreg_nregs (reg);
1687 else if (REG_P (reg))
1689 regno = REGNO (reg);
1690 nregs = REG_NREGS (reg);
1692 else
1693 gcc_unreachable ();
1694 for (int i = nregs - 1; i > 0; i--)
1695 reg_mode[regno + i] = BLKmode;
1696 reg_mode[regno] = mode;
1699 /* Record that REG is being set to the sum of SYM and OFF. */
1701 static void
1702 move2add_record_sym_value (rtx reg, rtx sym, rtx off)
1704 int regno = REGNO (reg);
1706 move2add_record_mode (reg);
1707 reg_set_luid[regno] = move2add_luid;
1708 reg_base_reg[regno] = -1;
1709 reg_symbol_ref[regno] = sym;
1710 reg_offset[regno] = INTVAL (off);
1713 /* Check if REGNO contains a valid value in MODE. */
1715 static bool
1716 move2add_valid_value_p (int regno, machine_mode mode)
1718 if (reg_set_luid[regno] <= move2add_last_label_luid)
1719 return false;
1721 if (mode != reg_mode[regno])
1723 if (!MODES_OK_FOR_MOVE2ADD (mode, reg_mode[regno]))
1724 return false;
1725 /* The value loaded into regno in reg_mode[regno] is also valid in
1726 mode after truncation only if (REG:mode regno) is the lowpart of
1727 (REG:reg_mode[regno] regno). Now, for big endian, the starting
1728 regno of the lowpart might be different. */
1729 int s_off = subreg_lowpart_offset (mode, reg_mode[regno]);
1730 s_off = subreg_regno_offset (regno, reg_mode[regno], s_off, mode);
1731 if (s_off != 0)
1732 /* We could in principle adjust regno, check reg_mode[regno] to be
1733 BLKmode, and return s_off to the caller (vs. -1 for failure),
1734 but we currently have no callers that could make use of this
1735 information. */
1736 return false;
1739 for (int i = hard_regno_nregs[regno][mode] - 1; i > 0; i--)
1740 if (reg_mode[regno + i] != BLKmode)
1741 return false;
1742 return true;
1745 /* This function is called with INSN that sets REG to (SYM + OFF),
1746 while REG is known to already have value (SYM + offset).
1747 This function tries to change INSN into an add instruction
1748 (set (REG) (plus (REG) (OFF - offset))) using the known value.
1749 It also updates the information about REG's known value.
1750 Return true if we made a change. */
1752 static bool
1753 move2add_use_add2_insn (rtx reg, rtx sym, rtx off, rtx_insn *insn)
1755 rtx pat = PATTERN (insn);
1756 rtx src = SET_SRC (pat);
1757 int regno = REGNO (reg);
1758 rtx new_src = gen_int_mode (UINTVAL (off) - reg_offset[regno],
1759 GET_MODE (reg));
1760 bool speed = optimize_bb_for_speed_p (BLOCK_FOR_INSN (insn));
1761 bool changed = false;
1763 /* (set (reg) (plus (reg) (const_int 0))) is not canonical;
1764 use (set (reg) (reg)) instead.
1765 We don't delete this insn, nor do we convert it into a
1766 note, to avoid losing register notes or the return
1767 value flag. jump2 already knows how to get rid of
1768 no-op moves. */
1769 if (new_src == const0_rtx)
1771 /* If the constants are different, this is a
1772 truncation, that, if turned into (set (reg)
1773 (reg)), would be discarded. Maybe we should
1774 try a truncMN pattern? */
1775 if (INTVAL (off) == reg_offset [regno])
1776 changed = validate_change (insn, &SET_SRC (pat), reg, 0);
1778 else
1780 struct full_rtx_costs oldcst, newcst;
1781 rtx tem = gen_rtx_PLUS (GET_MODE (reg), reg, new_src);
1783 get_full_set_rtx_cost (pat, &oldcst);
1784 SET_SRC (pat) = tem;
1785 get_full_set_rtx_cost (pat, &newcst);
1786 SET_SRC (pat) = src;
1788 if (costs_lt_p (&newcst, &oldcst, speed)
1789 && have_add2_insn (reg, new_src))
1790 changed = validate_change (insn, &SET_SRC (pat), tem, 0);
1791 else if (sym == NULL_RTX && GET_MODE (reg) != BImode)
1793 machine_mode narrow_mode;
1794 for (narrow_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
1795 narrow_mode != VOIDmode
1796 && narrow_mode != GET_MODE (reg);
1797 narrow_mode = GET_MODE_WIDER_MODE (narrow_mode))
1799 if (have_insn_for (STRICT_LOW_PART, narrow_mode)
1800 && ((reg_offset[regno] & ~GET_MODE_MASK (narrow_mode))
1801 == (INTVAL (off) & ~GET_MODE_MASK (narrow_mode))))
1803 rtx narrow_reg = gen_lowpart_common (narrow_mode, reg);
1804 rtx narrow_src = gen_int_mode (INTVAL (off),
1805 narrow_mode);
1806 rtx new_set
1807 = gen_rtx_SET (gen_rtx_STRICT_LOW_PART (VOIDmode,
1808 narrow_reg),
1809 narrow_src);
1810 get_full_set_rtx_cost (new_set, &newcst);
1811 if (costs_lt_p (&newcst, &oldcst, speed))
1813 changed = validate_change (insn, &PATTERN (insn),
1814 new_set, 0);
1815 if (changed)
1816 break;
1822 move2add_record_sym_value (reg, sym, off);
1823 return changed;
1827 /* This function is called with INSN that sets REG to (SYM + OFF),
1828 but REG doesn't have known value (SYM + offset). This function
1829 tries to find another register which is known to already have
1830 value (SYM + offset) and change INSN into an add instruction
1831 (set (REG) (plus (the found register) (OFF - offset))) if such
1832 a register is found. It also updates the information about
1833 REG's known value.
1834 Return true iff we made a change. */
1836 static bool
1837 move2add_use_add3_insn (rtx reg, rtx sym, rtx off, rtx_insn *insn)
1839 rtx pat = PATTERN (insn);
1840 rtx src = SET_SRC (pat);
1841 int regno = REGNO (reg);
1842 int min_regno = 0;
1843 bool speed = optimize_bb_for_speed_p (BLOCK_FOR_INSN (insn));
1844 int i;
1845 bool changed = false;
1846 struct full_rtx_costs oldcst, newcst, mincst;
1847 rtx plus_expr;
1849 init_costs_to_max (&mincst);
1850 get_full_set_rtx_cost (pat, &oldcst);
1852 plus_expr = gen_rtx_PLUS (GET_MODE (reg), reg, const0_rtx);
1853 SET_SRC (pat) = plus_expr;
1855 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1856 if (move2add_valid_value_p (i, GET_MODE (reg))
1857 && reg_base_reg[i] < 0
1858 && reg_symbol_ref[i] != NULL_RTX
1859 && rtx_equal_p (sym, reg_symbol_ref[i]))
1861 rtx new_src = gen_int_mode (UINTVAL (off) - reg_offset[i],
1862 GET_MODE (reg));
1863 /* (set (reg) (plus (reg) (const_int 0))) is not canonical;
1864 use (set (reg) (reg)) instead.
1865 We don't delete this insn, nor do we convert it into a
1866 note, to avoid losing register notes or the return
1867 value flag. jump2 already knows how to get rid of
1868 no-op moves. */
1869 if (new_src == const0_rtx)
1871 init_costs_to_zero (&mincst);
1872 min_regno = i;
1873 break;
1875 else
1877 XEXP (plus_expr, 1) = new_src;
1878 get_full_set_rtx_cost (pat, &newcst);
1880 if (costs_lt_p (&newcst, &mincst, speed))
1882 mincst = newcst;
1883 min_regno = i;
1887 SET_SRC (pat) = src;
1889 if (costs_lt_p (&mincst, &oldcst, speed))
1891 rtx tem;
1893 tem = gen_rtx_REG (GET_MODE (reg), min_regno);
1894 if (i != min_regno)
1896 rtx new_src = gen_int_mode (UINTVAL (off) - reg_offset[min_regno],
1897 GET_MODE (reg));
1898 tem = gen_rtx_PLUS (GET_MODE (reg), tem, new_src);
1900 if (validate_change (insn, &SET_SRC (pat), tem, 0))
1901 changed = true;
1903 reg_set_luid[regno] = move2add_luid;
1904 move2add_record_sym_value (reg, sym, off);
1905 return changed;
1908 /* Convert move insns with constant inputs to additions if they are cheaper.
1909 Return true if any changes were made. */
1910 static bool
1911 reload_cse_move2add (rtx_insn *first)
1913 int i;
1914 rtx_insn *insn;
1915 bool changed = false;
1917 for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; i--)
1919 reg_set_luid[i] = 0;
1920 reg_offset[i] = 0;
1921 reg_base_reg[i] = 0;
1922 reg_symbol_ref[i] = NULL_RTX;
1923 reg_mode[i] = VOIDmode;
1926 move2add_last_label_luid = 0;
1927 move2add_luid = 2;
1928 for (insn = first; insn; insn = NEXT_INSN (insn), move2add_luid++)
1930 rtx pat, note;
1932 if (LABEL_P (insn))
1934 move2add_last_label_luid = move2add_luid;
1935 /* We're going to increment move2add_luid twice after a
1936 label, so that we can use move2add_last_label_luid + 1 as
1937 the luid for constants. */
1938 move2add_luid++;
1939 continue;
1941 if (! INSN_P (insn))
1942 continue;
1943 pat = PATTERN (insn);
1944 /* For simplicity, we only perform this optimization on
1945 straightforward SETs. */
1946 if (GET_CODE (pat) == SET
1947 && REG_P (SET_DEST (pat)))
1949 rtx reg = SET_DEST (pat);
1950 int regno = REGNO (reg);
1951 rtx src = SET_SRC (pat);
1953 /* Check if we have valid information on the contents of this
1954 register in the mode of REG. */
1955 if (move2add_valid_value_p (regno, GET_MODE (reg))
1956 && dbg_cnt (cse2_move2add))
1958 /* Try to transform (set (REGX) (CONST_INT A))
1960 (set (REGX) (CONST_INT B))
1962 (set (REGX) (CONST_INT A))
1964 (set (REGX) (plus (REGX) (CONST_INT B-A)))
1966 (set (REGX) (CONST_INT A))
1968 (set (STRICT_LOW_PART (REGX)) (CONST_INT B))
1971 if (CONST_INT_P (src)
1972 && reg_base_reg[regno] < 0
1973 && reg_symbol_ref[regno] == NULL_RTX)
1975 changed |= move2add_use_add2_insn (reg, NULL_RTX, src, insn);
1976 continue;
1979 /* Try to transform (set (REGX) (REGY))
1980 (set (REGX) (PLUS (REGX) (CONST_INT A)))
1982 (set (REGX) (REGY))
1983 (set (REGX) (PLUS (REGX) (CONST_INT B)))
1985 (set (REGX) (REGY))
1986 (set (REGX) (PLUS (REGX) (CONST_INT A)))
1988 (set (REGX) (plus (REGX) (CONST_INT B-A))) */
1989 else if (REG_P (src)
1990 && reg_set_luid[regno] == reg_set_luid[REGNO (src)]
1991 && reg_base_reg[regno] == reg_base_reg[REGNO (src)]
1992 && move2add_valid_value_p (REGNO (src), GET_MODE (reg)))
1994 rtx_insn *next = next_nonnote_nondebug_insn (insn);
1995 rtx set = NULL_RTX;
1996 if (next)
1997 set = single_set (next);
1998 if (set
1999 && SET_DEST (set) == reg
2000 && GET_CODE (SET_SRC (set)) == PLUS
2001 && XEXP (SET_SRC (set), 0) == reg
2002 && CONST_INT_P (XEXP (SET_SRC (set), 1)))
2004 rtx src3 = XEXP (SET_SRC (set), 1);
2005 unsigned HOST_WIDE_INT added_offset = UINTVAL (src3);
2006 HOST_WIDE_INT base_offset = reg_offset[REGNO (src)];
2007 HOST_WIDE_INT regno_offset = reg_offset[regno];
2008 rtx new_src =
2009 gen_int_mode (added_offset
2010 + base_offset
2011 - regno_offset,
2012 GET_MODE (reg));
2013 bool success = false;
2014 bool speed = optimize_bb_for_speed_p (BLOCK_FOR_INSN (insn));
2016 if (new_src == const0_rtx)
2017 /* See above why we create (set (reg) (reg)) here. */
2018 success
2019 = validate_change (next, &SET_SRC (set), reg, 0);
2020 else
2022 rtx old_src = SET_SRC (set);
2023 struct full_rtx_costs oldcst, newcst;
2024 rtx tem = gen_rtx_PLUS (GET_MODE (reg), reg, new_src);
2026 get_full_set_rtx_cost (set, &oldcst);
2027 SET_SRC (set) = tem;
2028 get_full_set_src_cost (tem, &newcst);
2029 SET_SRC (set) = old_src;
2030 costs_add_n_insns (&oldcst, 1);
2032 if (costs_lt_p (&newcst, &oldcst, speed)
2033 && have_add2_insn (reg, new_src))
2035 rtx newpat = gen_rtx_SET (reg, tem);
2036 success
2037 = validate_change (next, &PATTERN (next),
2038 newpat, 0);
2041 if (success)
2042 delete_insn (insn);
2043 changed |= success;
2044 insn = next;
2045 move2add_record_mode (reg);
2046 reg_offset[regno]
2047 = trunc_int_for_mode (added_offset + base_offset,
2048 GET_MODE (reg));
2049 continue;
2054 /* Try to transform
2055 (set (REGX) (CONST (PLUS (SYMBOL_REF) (CONST_INT A))))
2057 (set (REGY) (CONST (PLUS (SYMBOL_REF) (CONST_INT B))))
2059 (set (REGX) (CONST (PLUS (SYMBOL_REF) (CONST_INT A))))
2061 (set (REGY) (CONST (PLUS (REGX) (CONST_INT B-A)))) */
2062 if ((GET_CODE (src) == SYMBOL_REF
2063 || (GET_CODE (src) == CONST
2064 && GET_CODE (XEXP (src, 0)) == PLUS
2065 && GET_CODE (XEXP (XEXP (src, 0), 0)) == SYMBOL_REF
2066 && CONST_INT_P (XEXP (XEXP (src, 0), 1))))
2067 && dbg_cnt (cse2_move2add))
2069 rtx sym, off;
2071 if (GET_CODE (src) == SYMBOL_REF)
2073 sym = src;
2074 off = const0_rtx;
2076 else
2078 sym = XEXP (XEXP (src, 0), 0);
2079 off = XEXP (XEXP (src, 0), 1);
2082 /* If the reg already contains the value which is sum of
2083 sym and some constant value, we can use an add2 insn. */
2084 if (move2add_valid_value_p (regno, GET_MODE (reg))
2085 && reg_base_reg[regno] < 0
2086 && reg_symbol_ref[regno] != NULL_RTX
2087 && rtx_equal_p (sym, reg_symbol_ref[regno]))
2088 changed |= move2add_use_add2_insn (reg, sym, off, insn);
2090 /* Otherwise, we have to find a register whose value is sum
2091 of sym and some constant value. */
2092 else
2093 changed |= move2add_use_add3_insn (reg, sym, off, insn);
2095 continue;
2099 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2101 if (REG_NOTE_KIND (note) == REG_INC
2102 && REG_P (XEXP (note, 0)))
2104 /* Reset the information about this register. */
2105 int regno = REGNO (XEXP (note, 0));
2106 if (regno < FIRST_PSEUDO_REGISTER)
2108 move2add_record_mode (XEXP (note, 0));
2109 reg_mode[regno] = VOIDmode;
2113 note_stores (PATTERN (insn), move2add_note_store, insn);
2115 /* If INSN is a conditional branch, we try to extract an
2116 implicit set out of it. */
2117 if (any_condjump_p (insn))
2119 rtx cnd = fis_get_condition (insn);
2121 if (cnd != NULL_RTX
2122 && GET_CODE (cnd) == NE
2123 && REG_P (XEXP (cnd, 0))
2124 && !reg_set_p (XEXP (cnd, 0), insn)
2125 /* The following two checks, which are also in
2126 move2add_note_store, are intended to reduce the
2127 number of calls to gen_rtx_SET to avoid memory
2128 allocation if possible. */
2129 && SCALAR_INT_MODE_P (GET_MODE (XEXP (cnd, 0)))
2130 && REG_NREGS (XEXP (cnd, 0)) == 1
2131 && CONST_INT_P (XEXP (cnd, 1)))
2133 rtx implicit_set =
2134 gen_rtx_SET (XEXP (cnd, 0), XEXP (cnd, 1));
2135 move2add_note_store (SET_DEST (implicit_set), implicit_set, insn);
2139 /* If this is a CALL_INSN, all call used registers are stored with
2140 unknown values. */
2141 if (CALL_P (insn))
2143 for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; i--)
2145 if (call_used_regs[i])
2146 /* Reset the information about this register. */
2147 reg_mode[i] = VOIDmode;
2151 return changed;
2154 /* SET is a SET or CLOBBER that sets DST. DATA is the insn which
2155 contains SET.
2156 Update reg_set_luid, reg_offset and reg_base_reg accordingly.
2157 Called from reload_cse_move2add via note_stores. */
2159 static void
2160 move2add_note_store (rtx dst, const_rtx set, void *data)
2162 rtx_insn *insn = (rtx_insn *) data;
2163 unsigned int regno = 0;
2164 machine_mode mode = GET_MODE (dst);
2166 /* Some targets do argument pushes without adding REG_INC notes. */
2168 if (MEM_P (dst))
2170 dst = XEXP (dst, 0);
2171 if (GET_CODE (dst) == PRE_INC || GET_CODE (dst) == POST_INC
2172 || GET_CODE (dst) == PRE_DEC || GET_CODE (dst) == POST_DEC)
2173 reg_mode[REGNO (XEXP (dst, 0))] = VOIDmode;
2174 return;
2177 if (GET_CODE (dst) == SUBREG)
2178 regno = subreg_regno (dst);
2179 else if (REG_P (dst))
2180 regno = REGNO (dst);
2181 else
2182 return;
2184 if (SCALAR_INT_MODE_P (mode)
2185 && GET_CODE (set) == SET)
2187 rtx note, sym = NULL_RTX;
2188 rtx off;
2190 note = find_reg_equal_equiv_note (insn);
2191 if (note && GET_CODE (XEXP (note, 0)) == SYMBOL_REF)
2193 sym = XEXP (note, 0);
2194 off = const0_rtx;
2196 else if (note && GET_CODE (XEXP (note, 0)) == CONST
2197 && GET_CODE (XEXP (XEXP (note, 0), 0)) == PLUS
2198 && GET_CODE (XEXP (XEXP (XEXP (note, 0), 0), 0)) == SYMBOL_REF
2199 && CONST_INT_P (XEXP (XEXP (XEXP (note, 0), 0), 1)))
2201 sym = XEXP (XEXP (XEXP (note, 0), 0), 0);
2202 off = XEXP (XEXP (XEXP (note, 0), 0), 1);
2205 if (sym != NULL_RTX)
2207 move2add_record_sym_value (dst, sym, off);
2208 return;
2212 if (SCALAR_INT_MODE_P (mode)
2213 && GET_CODE (set) == SET
2214 && GET_CODE (SET_DEST (set)) != ZERO_EXTRACT
2215 && GET_CODE (SET_DEST (set)) != STRICT_LOW_PART)
2217 rtx src = SET_SRC (set);
2218 rtx base_reg;
2219 unsigned HOST_WIDE_INT offset;
2220 int base_regno;
2222 switch (GET_CODE (src))
2224 case PLUS:
2225 if (REG_P (XEXP (src, 0)))
2227 base_reg = XEXP (src, 0);
2229 if (CONST_INT_P (XEXP (src, 1)))
2230 offset = UINTVAL (XEXP (src, 1));
2231 else if (REG_P (XEXP (src, 1))
2232 && move2add_valid_value_p (REGNO (XEXP (src, 1)), mode))
2234 if (reg_base_reg[REGNO (XEXP (src, 1))] < 0
2235 && reg_symbol_ref[REGNO (XEXP (src, 1))] == NULL_RTX)
2236 offset = reg_offset[REGNO (XEXP (src, 1))];
2237 /* Maybe the first register is known to be a
2238 constant. */
2239 else if (move2add_valid_value_p (REGNO (base_reg), mode)
2240 && reg_base_reg[REGNO (base_reg)] < 0
2241 && reg_symbol_ref[REGNO (base_reg)] == NULL_RTX)
2243 offset = reg_offset[REGNO (base_reg)];
2244 base_reg = XEXP (src, 1);
2246 else
2247 goto invalidate;
2249 else
2250 goto invalidate;
2252 break;
2255 goto invalidate;
2257 case REG:
2258 base_reg = src;
2259 offset = 0;
2260 break;
2262 case CONST_INT:
2263 /* Start tracking the register as a constant. */
2264 reg_base_reg[regno] = -1;
2265 reg_symbol_ref[regno] = NULL_RTX;
2266 reg_offset[regno] = INTVAL (SET_SRC (set));
2267 /* We assign the same luid to all registers set to constants. */
2268 reg_set_luid[regno] = move2add_last_label_luid + 1;
2269 move2add_record_mode (dst);
2270 return;
2272 default:
2273 goto invalidate;
2276 base_regno = REGNO (base_reg);
2277 /* If information about the base register is not valid, set it
2278 up as a new base register, pretending its value is known
2279 starting from the current insn. */
2280 if (!move2add_valid_value_p (base_regno, mode))
2282 reg_base_reg[base_regno] = base_regno;
2283 reg_symbol_ref[base_regno] = NULL_RTX;
2284 reg_offset[base_regno] = 0;
2285 reg_set_luid[base_regno] = move2add_luid;
2286 gcc_assert (GET_MODE (base_reg) == mode);
2287 move2add_record_mode (base_reg);
2290 /* Copy base information from our base register. */
2291 reg_set_luid[regno] = reg_set_luid[base_regno];
2292 reg_base_reg[regno] = reg_base_reg[base_regno];
2293 reg_symbol_ref[regno] = reg_symbol_ref[base_regno];
2295 /* Compute the sum of the offsets or constants. */
2296 reg_offset[regno]
2297 = trunc_int_for_mode (offset + reg_offset[base_regno], mode);
2299 move2add_record_mode (dst);
2301 else
2303 invalidate:
2304 /* Invalidate the contents of the register. */
2305 move2add_record_mode (dst);
2306 reg_mode[regno] = VOIDmode;
2310 namespace {
2312 const pass_data pass_data_postreload_cse =
2314 RTL_PASS, /* type */
2315 "postreload", /* name */
2316 OPTGROUP_NONE, /* optinfo_flags */
2317 TV_RELOAD_CSE_REGS, /* tv_id */
2318 0, /* properties_required */
2319 0, /* properties_provided */
2320 0, /* properties_destroyed */
2321 0, /* todo_flags_start */
2322 TODO_df_finish, /* todo_flags_finish */
2325 class pass_postreload_cse : public rtl_opt_pass
2327 public:
2328 pass_postreload_cse (gcc::context *ctxt)
2329 : rtl_opt_pass (pass_data_postreload_cse, ctxt)
2332 /* opt_pass methods: */
2333 virtual bool gate (function *) { return (optimize > 0 && reload_completed); }
2335 virtual unsigned int execute (function *);
2337 }; // class pass_postreload_cse
2339 unsigned int
2340 pass_postreload_cse::execute (function *fun)
2342 if (!dbg_cnt (postreload_cse))
2343 return 0;
2345 /* Do a very simple CSE pass over just the hard registers. */
2346 reload_cse_regs (get_insns ());
2347 /* Reload_cse_regs can eliminate potentially-trapping MEMs.
2348 Remove any EH edges associated with them. */
2349 if (fun->can_throw_non_call_exceptions
2350 && purge_all_dead_edges ())
2351 cleanup_cfg (0);
2353 return 0;
2356 } // anon namespace
2358 rtl_opt_pass *
2359 make_pass_postreload_cse (gcc::context *ctxt)
2361 return new pass_postreload_cse (ctxt);