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