Makefile.in (OBJS): Add postreload.o.
[gcc.git] / gcc / postreload.c
1 /* Perform simple optimizations to clean up the result of reload.
2 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26
27 #include "machmode.h"
28 #include "hard-reg-set.h"
29 #include "rtl.h"
30 #include "tm_p.h"
31 #include "obstack.h"
32 #include "insn-config.h"
33 #include "flags.h"
34 #include "function.h"
35 #include "expr.h"
36 #include "optabs.h"
37 #include "regs.h"
38 #include "basic-block.h"
39 #include "reload.h"
40 #include "recog.h"
41 #include "output.h"
42 #include "cselib.h"
43 #include "real.h"
44 #include "toplev.h"
45 #include "except.h"
46 #include "tree.h"
47
48 static int reload_cse_noop_set_p PARAMS ((rtx));
49 static void reload_cse_simplify PARAMS ((rtx, rtx));
50 static void reload_cse_regs_1 PARAMS ((rtx));
51 static int reload_cse_simplify_set PARAMS ((rtx, rtx));
52 static int reload_cse_simplify_operands PARAMS ((rtx, rtx));
53
54 static void reload_combine PARAMS ((void));
55 static void reload_combine_note_use PARAMS ((rtx *, rtx));
56 static void reload_combine_note_store PARAMS ((rtx, rtx, void *));
57
58 static void reload_cse_move2add PARAMS ((rtx));
59 static void move2add_note_store PARAMS ((rtx, rtx, void *));
60
61 /* Call cse / combine like post-reload optimization phases.
62 FIRST is the first instruction. */
63 void
64 reload_cse_regs (first)
65 rtx first ATTRIBUTE_UNUSED;
66 {
67 reload_cse_regs_1 (first);
68 reload_combine ();
69 reload_cse_move2add (first);
70 if (flag_expensive_optimizations)
71 reload_cse_regs_1 (first);
72 }
73
74 /* See whether a single set SET is a noop. */
75 static int
76 reload_cse_noop_set_p (set)
77 rtx set;
78 {
79 if (cselib_reg_set_mode (SET_DEST (set)) != GET_MODE (SET_DEST (set)))
80 return 0;
81
82 return rtx_equal_for_cselib_p (SET_DEST (set), SET_SRC (set));
83 }
84
85 /* Try to simplify INSN. */
86 static void
87 reload_cse_simplify (insn, testreg)
88 rtx insn;
89 rtx testreg;
90 {
91 rtx body = PATTERN (insn);
92
93 if (GET_CODE (body) == SET)
94 {
95 int count = 0;
96
97 /* Simplify even if we may think it is a no-op.
98 We may think a memory load of a value smaller than WORD_SIZE
99 is redundant because we haven't taken into account possible
100 implicit extension. reload_cse_simplify_set() will bring
101 this out, so it's safer to simplify before we delete. */
102 count += reload_cse_simplify_set (body, insn);
103
104 if (!count && reload_cse_noop_set_p (body))
105 {
106 rtx value = SET_DEST (body);
107 if (REG_P (value)
108 && ! REG_FUNCTION_VALUE_P (value))
109 value = 0;
110 delete_insn_and_edges (insn);
111 return;
112 }
113
114 if (count > 0)
115 apply_change_group ();
116 else
117 reload_cse_simplify_operands (insn, testreg);
118 }
119 else if (GET_CODE (body) == PARALLEL)
120 {
121 int i;
122 int count = 0;
123 rtx value = NULL_RTX;
124
125 /* If every action in a PARALLEL is a noop, we can delete
126 the entire PARALLEL. */
127 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
128 {
129 rtx part = XVECEXP (body, 0, i);
130 if (GET_CODE (part) == SET)
131 {
132 if (! reload_cse_noop_set_p (part))
133 break;
134 if (REG_P (SET_DEST (part))
135 && REG_FUNCTION_VALUE_P (SET_DEST (part)))
136 {
137 if (value)
138 break;
139 value = SET_DEST (part);
140 }
141 }
142 else if (GET_CODE (part) != CLOBBER)
143 break;
144 }
145
146 if (i < 0)
147 {
148 delete_insn_and_edges (insn);
149 /* We're done with this insn. */
150 return;
151 }
152
153 /* It's not a no-op, but we can try to simplify it. */
154 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
155 if (GET_CODE (XVECEXP (body, 0, i)) == SET)
156 count += reload_cse_simplify_set (XVECEXP (body, 0, i), insn);
157
158 if (count > 0)
159 apply_change_group ();
160 else
161 reload_cse_simplify_operands (insn, testreg);
162 }
163 }
164
165 /* Do a very simple CSE pass over the hard registers.
166
167 This function detects no-op moves where we happened to assign two
168 different pseudo-registers to the same hard register, and then
169 copied one to the other. Reload will generate a useless
170 instruction copying a register to itself.
171
172 This function also detects cases where we load a value from memory
173 into two different registers, and (if memory is more expensive than
174 registers) changes it to simply copy the first register into the
175 second register.
176
177 Another optimization is performed that scans the operands of each
178 instruction to see whether the value is already available in a
179 hard register. It then replaces the operand with the hard register
180 if possible, much like an optional reload would. */
181
182 static void
183 reload_cse_regs_1 (first)
184 rtx first;
185 {
186 rtx insn;
187 rtx testreg = gen_rtx_REG (VOIDmode, -1);
188
189 cselib_init ();
190 init_alias_analysis ();
191
192 for (insn = first; insn; insn = NEXT_INSN (insn))
193 {
194 if (INSN_P (insn))
195 reload_cse_simplify (insn, testreg);
196
197 cselib_process_insn (insn);
198 }
199
200 /* Clean up. */
201 end_alias_analysis ();
202 cselib_finish ();
203 }
204
205 /* Try to simplify a single SET instruction. SET is the set pattern.
206 INSN is the instruction it came from.
207 This function only handles one case: if we set a register to a value
208 which is not a register, we try to find that value in some other register
209 and change the set into a register copy. */
210
211 static int
212 reload_cse_simplify_set (set, insn)
213 rtx set;
214 rtx insn;
215 {
216 int did_change = 0;
217 int dreg;
218 rtx src;
219 enum reg_class dclass;
220 int old_cost;
221 cselib_val *val;
222 struct elt_loc_list *l;
223 #ifdef LOAD_EXTEND_OP
224 enum rtx_code extend_op = NIL;
225 #endif
226
227 dreg = true_regnum (SET_DEST (set));
228 if (dreg < 0)
229 return 0;
230
231 src = SET_SRC (set);
232 if (side_effects_p (src) || true_regnum (src) >= 0)
233 return 0;
234
235 dclass = REGNO_REG_CLASS (dreg);
236
237 #ifdef LOAD_EXTEND_OP
238 /* When replacing a memory with a register, we need to honor assumptions
239 that combine made wrt the contents of sign bits. We'll do this by
240 generating an extend instruction instead of a reg->reg copy. Thus
241 the destination must be a register that we can widen. */
242 if (GET_CODE (src) == MEM
243 && GET_MODE_BITSIZE (GET_MODE (src)) < BITS_PER_WORD
244 && (extend_op = LOAD_EXTEND_OP (GET_MODE (src))) != NIL
245 && GET_CODE (SET_DEST (set)) != REG)
246 return 0;
247 #endif
248
249 /* If memory loads are cheaper than register copies, don't change them. */
250 if (GET_CODE (src) == MEM)
251 old_cost = MEMORY_MOVE_COST (GET_MODE (src), dclass, 1);
252 else if (CONSTANT_P (src))
253 old_cost = rtx_cost (src, SET);
254 else if (GET_CODE (src) == REG)
255 old_cost = REGISTER_MOVE_COST (GET_MODE (src),
256 REGNO_REG_CLASS (REGNO (src)), dclass);
257 else
258 /* ??? */
259 old_cost = rtx_cost (src, SET);
260
261 val = cselib_lookup (src, GET_MODE (SET_DEST (set)), 0);
262 if (! val)
263 return 0;
264 for (l = val->locs; l; l = l->next)
265 {
266 rtx this_rtx = l->loc;
267 int this_cost;
268
269 if (CONSTANT_P (this_rtx) && ! references_value_p (this_rtx, 0))
270 {
271 #ifdef LOAD_EXTEND_OP
272 if (extend_op != NIL)
273 {
274 HOST_WIDE_INT this_val;
275
276 /* ??? I'm lazy and don't wish to handle CONST_DOUBLE. Other
277 constants, such as SYMBOL_REF, cannot be extended. */
278 if (GET_CODE (this_rtx) != CONST_INT)
279 continue;
280
281 this_val = INTVAL (this_rtx);
282 switch (extend_op)
283 {
284 case ZERO_EXTEND:
285 this_val &= GET_MODE_MASK (GET_MODE (src));
286 break;
287 case SIGN_EXTEND:
288 /* ??? In theory we're already extended. */
289 if (this_val == trunc_int_for_mode (this_val, GET_MODE (src)))
290 break;
291 default:
292 abort ();
293 }
294 this_rtx = GEN_INT (this_val);
295 }
296 #endif
297 this_cost = rtx_cost (this_rtx, SET);
298 }
299 else if (GET_CODE (this_rtx) == REG)
300 {
301 #ifdef LOAD_EXTEND_OP
302 if (extend_op != NIL)
303 {
304 this_rtx = gen_rtx_fmt_e (extend_op, word_mode, this_rtx);
305 this_cost = rtx_cost (this_rtx, SET);
306 }
307 else
308 #endif
309 this_cost = REGISTER_MOVE_COST (GET_MODE (this_rtx),
310 REGNO_REG_CLASS (REGNO (this_rtx)),
311 dclass);
312 }
313 else
314 continue;
315
316 /* If equal costs, prefer registers over anything else. That
317 tends to lead to smaller instructions on some machines. */
318 if (this_cost < old_cost
319 || (this_cost == old_cost
320 && GET_CODE (this_rtx) == REG
321 && GET_CODE (SET_SRC (set)) != REG))
322 {
323 #ifdef LOAD_EXTEND_OP
324 if (GET_MODE_BITSIZE (GET_MODE (SET_DEST (set))) < BITS_PER_WORD
325 && extend_op != NIL
326 #ifdef CANNOT_CHANGE_MODE_CLASS
327 && !CANNOT_CHANGE_MODE_CLASS (GET_MODE (SET_DEST (set)),
328 word_mode,
329 REGNO_REG_CLASS (REGNO (SET_DEST (set))))
330 #endif
331 )
332 {
333 rtx wide_dest = gen_rtx_REG (word_mode, REGNO (SET_DEST (set)));
334 ORIGINAL_REGNO (wide_dest) = ORIGINAL_REGNO (SET_DEST (set));
335 validate_change (insn, &SET_DEST (set), wide_dest, 1);
336 }
337 #endif
338
339 validate_change (insn, &SET_SRC (set), copy_rtx (this_rtx), 1);
340 old_cost = this_cost, did_change = 1;
341 }
342 }
343
344 return did_change;
345 }
346
347 /* Try to replace operands in INSN with equivalent values that are already
348 in registers. This can be viewed as optional reloading.
349
350 For each non-register operand in the insn, see if any hard regs are
351 known to be equivalent to that operand. Record the alternatives which
352 can accept these hard registers. Among all alternatives, select the
353 ones which are better or equal to the one currently matching, where
354 "better" is in terms of '?' and '!' constraints. Among the remaining
355 alternatives, select the one which replaces most operands with
356 hard registers. */
357
358 static int
359 reload_cse_simplify_operands (insn, testreg)
360 rtx insn;
361 rtx testreg;
362 {
363 int i, j;
364
365 /* For each operand, all registers that are equivalent to it. */
366 HARD_REG_SET equiv_regs[MAX_RECOG_OPERANDS];
367
368 const char *constraints[MAX_RECOG_OPERANDS];
369
370 /* Vector recording how bad an alternative is. */
371 int *alternative_reject;
372 /* Vector recording how many registers can be introduced by choosing
373 this alternative. */
374 int *alternative_nregs;
375 /* Array of vectors recording, for each operand and each alternative,
376 which hard register to substitute, or -1 if the operand should be
377 left as it is. */
378 int *op_alt_regno[MAX_RECOG_OPERANDS];
379 /* Array of alternatives, sorted in order of decreasing desirability. */
380 int *alternative_order;
381
382 extract_insn (insn);
383
384 if (recog_data.n_alternatives == 0 || recog_data.n_operands == 0)
385 return 0;
386
387 /* Figure out which alternative currently matches. */
388 if (! constrain_operands (1))
389 fatal_insn_not_found (insn);
390
391 alternative_reject = (int *) alloca (recog_data.n_alternatives * sizeof (int));
392 alternative_nregs = (int *) alloca (recog_data.n_alternatives * sizeof (int));
393 alternative_order = (int *) alloca (recog_data.n_alternatives * sizeof (int));
394 memset ((char *) alternative_reject, 0, recog_data.n_alternatives * sizeof (int));
395 memset ((char *) alternative_nregs, 0, recog_data.n_alternatives * sizeof (int));
396
397 /* For each operand, find out which regs are equivalent. */
398 for (i = 0; i < recog_data.n_operands; i++)
399 {
400 cselib_val *v;
401 struct elt_loc_list *l;
402
403 CLEAR_HARD_REG_SET (equiv_regs[i]);
404
405 /* cselib blows up on CODE_LABELs. Trying to fix that doesn't seem
406 right, so avoid the problem here. Likewise if we have a constant
407 and the insn pattern doesn't tell us the mode we need. */
408 if (GET_CODE (recog_data.operand[i]) == CODE_LABEL
409 || (CONSTANT_P (recog_data.operand[i])
410 && recog_data.operand_mode[i] == VOIDmode))
411 continue;
412
413 v = cselib_lookup (recog_data.operand[i], recog_data.operand_mode[i], 0);
414 if (! v)
415 continue;
416
417 for (l = v->locs; l; l = l->next)
418 if (GET_CODE (l->loc) == REG)
419 SET_HARD_REG_BIT (equiv_regs[i], REGNO (l->loc));
420 }
421
422 for (i = 0; i < recog_data.n_operands; i++)
423 {
424 enum machine_mode mode;
425 int regno;
426 const char *p;
427
428 op_alt_regno[i] = (int *) alloca (recog_data.n_alternatives * sizeof (int));
429 for (j = 0; j < recog_data.n_alternatives; j++)
430 op_alt_regno[i][j] = -1;
431
432 p = constraints[i] = recog_data.constraints[i];
433 mode = recog_data.operand_mode[i];
434
435 /* Add the reject values for each alternative given by the constraints
436 for this operand. */
437 j = 0;
438 while (*p != '\0')
439 {
440 char c = *p++;
441 if (c == ',')
442 j++;
443 else if (c == '?')
444 alternative_reject[j] += 3;
445 else if (c == '!')
446 alternative_reject[j] += 300;
447 }
448
449 /* We won't change operands which are already registers. We
450 also don't want to modify output operands. */
451 regno = true_regnum (recog_data.operand[i]);
452 if (regno >= 0
453 || constraints[i][0] == '='
454 || constraints[i][0] == '+')
455 continue;
456
457 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
458 {
459 int class = (int) NO_REGS;
460
461 if (! TEST_HARD_REG_BIT (equiv_regs[i], regno))
462 continue;
463
464 REGNO (testreg) = regno;
465 PUT_MODE (testreg, mode);
466
467 /* We found a register equal to this operand. Now look for all
468 alternatives that can accept this register and have not been
469 assigned a register they can use yet. */
470 j = 0;
471 p = constraints[i];
472 for (;;)
473 {
474 char c = *p;
475
476 switch (c)
477 {
478 case '=': case '+': case '?':
479 case '#': case '&': case '!':
480 case '*': case '%':
481 case '0': case '1': case '2': case '3': case '4':
482 case '5': case '6': case '7': case '8': case '9':
483 case 'm': case '<': case '>': case 'V': case 'o':
484 case 'E': case 'F': case 'G': case 'H':
485 case 's': case 'i': case 'n':
486 case 'I': case 'J': case 'K': case 'L':
487 case 'M': case 'N': case 'O': case 'P':
488 case 'p': case 'X':
489 /* These don't say anything we care about. */
490 break;
491
492 case 'g': case 'r':
493 class = reg_class_subunion[(int) class][(int) GENERAL_REGS];
494 break;
495
496 default:
497 class
498 = (reg_class_subunion
499 [(int) class]
500 [(int) REG_CLASS_FROM_CONSTRAINT ((unsigned char) c, p)]);
501 break;
502
503 case ',': case '\0':
504 /* See if REGNO fits this alternative, and set it up as the
505 replacement register if we don't have one for this
506 alternative yet and the operand being replaced is not
507 a cheap CONST_INT. */
508 if (op_alt_regno[i][j] == -1
509 && reg_fits_class_p (testreg, class, 0, mode)
510 && (GET_CODE (recog_data.operand[i]) != CONST_INT
511 || (rtx_cost (recog_data.operand[i], SET)
512 > rtx_cost (testreg, SET))))
513 {
514 alternative_nregs[j]++;
515 op_alt_regno[i][j] = regno;
516 }
517 j++;
518 break;
519 }
520 p += CONSTRAINT_LEN (c, p);
521
522 if (c == '\0')
523 break;
524 }
525 }
526 }
527
528 /* Record all alternatives which are better or equal to the currently
529 matching one in the alternative_order array. */
530 for (i = j = 0; i < recog_data.n_alternatives; i++)
531 if (alternative_reject[i] <= alternative_reject[which_alternative])
532 alternative_order[j++] = i;
533 recog_data.n_alternatives = j;
534
535 /* Sort it. Given a small number of alternatives, a dumb algorithm
536 won't hurt too much. */
537 for (i = 0; i < recog_data.n_alternatives - 1; i++)
538 {
539 int best = i;
540 int best_reject = alternative_reject[alternative_order[i]];
541 int best_nregs = alternative_nregs[alternative_order[i]];
542 int tmp;
543
544 for (j = i + 1; j < recog_data.n_alternatives; j++)
545 {
546 int this_reject = alternative_reject[alternative_order[j]];
547 int this_nregs = alternative_nregs[alternative_order[j]];
548
549 if (this_reject < best_reject
550 || (this_reject == best_reject && this_nregs < best_nregs))
551 {
552 best = j;
553 best_reject = this_reject;
554 best_nregs = this_nregs;
555 }
556 }
557
558 tmp = alternative_order[best];
559 alternative_order[best] = alternative_order[i];
560 alternative_order[i] = tmp;
561 }
562
563 /* Substitute the operands as determined by op_alt_regno for the best
564 alternative. */
565 j = alternative_order[0];
566
567 for (i = 0; i < recog_data.n_operands; i++)
568 {
569 enum machine_mode mode = recog_data.operand_mode[i];
570 if (op_alt_regno[i][j] == -1)
571 continue;
572
573 validate_change (insn, recog_data.operand_loc[i],
574 gen_rtx_REG (mode, op_alt_regno[i][j]), 1);
575 }
576
577 for (i = recog_data.n_dups - 1; i >= 0; i--)
578 {
579 int op = recog_data.dup_num[i];
580 enum machine_mode mode = recog_data.operand_mode[op];
581
582 if (op_alt_regno[op][j] == -1)
583 continue;
584
585 validate_change (insn, recog_data.dup_loc[i],
586 gen_rtx_REG (mode, op_alt_regno[op][j]), 1);
587 }
588
589 return apply_change_group ();
590 }
591 \f
592 /* If reload couldn't use reg+reg+offset addressing, try to use reg+reg
593 addressing now.
594 This code might also be useful when reload gave up on reg+reg addressing
595 because of clashes between the return register and INDEX_REG_CLASS. */
596
597 /* The maximum number of uses of a register we can keep track of to
598 replace them with reg+reg addressing. */
599 #define RELOAD_COMBINE_MAX_USES 6
600
601 /* INSN is the insn where a register has ben used, and USEP points to the
602 location of the register within the rtl. */
603 struct reg_use { rtx insn, *usep; };
604
605 /* If the register is used in some unknown fashion, USE_INDEX is negative.
606 If it is dead, USE_INDEX is RELOAD_COMBINE_MAX_USES, and STORE_RUID
607 indicates where it becomes live again.
608 Otherwise, USE_INDEX is the index of the last encountered use of the
609 register (which is first among these we have seen since we scan backwards),
610 OFFSET contains the constant offset that is added to the register in
611 all encountered uses, and USE_RUID indicates the first encountered, i.e.
612 last, of these uses.
613 STORE_RUID is always meaningful if we only want to use a value in a
614 register in a different place: it denotes the next insn in the insn
615 stream (i.e. the last encountered) that sets or clobbers the register. */
616 static struct
617 {
618 struct reg_use reg_use[RELOAD_COMBINE_MAX_USES];
619 int use_index;
620 rtx offset;
621 int store_ruid;
622 int use_ruid;
623 } reg_state[FIRST_PSEUDO_REGISTER];
624
625 /* Reverse linear uid. This is increased in reload_combine while scanning
626 the instructions from last to first. It is used to set last_label_ruid
627 and the store_ruid / use_ruid fields in reg_state. */
628 static int reload_combine_ruid;
629
630 #define LABEL_LIVE(LABEL) \
631 (label_live[CODE_LABEL_NUMBER (LABEL) - min_labelno])
632
633 static void
634 reload_combine ()
635 {
636 rtx insn, set;
637 int first_index_reg = -1;
638 int last_index_reg = 0;
639 int i;
640 basic_block bb;
641 unsigned int r;
642 int last_label_ruid;
643 int min_labelno, n_labels;
644 HARD_REG_SET ever_live_at_start, *label_live;
645
646 /* If reg+reg can be used in offsetable memory addresses, the main chunk of
647 reload has already used it where appropriate, so there is no use in
648 trying to generate it now. */
649 if (double_reg_address_ok && INDEX_REG_CLASS != NO_REGS)
650 return;
651
652 /* To avoid wasting too much time later searching for an index register,
653 determine the minimum and maximum index register numbers. */
654 for (r = 0; r < FIRST_PSEUDO_REGISTER; r++)
655 if (TEST_HARD_REG_BIT (reg_class_contents[INDEX_REG_CLASS], r))
656 {
657 if (first_index_reg == -1)
658 first_index_reg = r;
659
660 last_index_reg = r;
661 }
662
663 /* If no index register is available, we can quit now. */
664 if (first_index_reg == -1)
665 return;
666
667 /* Set up LABEL_LIVE and EVER_LIVE_AT_START. The register lifetime
668 information is a bit fuzzy immediately after reload, but it's
669 still good enough to determine which registers are live at a jump
670 destination. */
671 min_labelno = get_first_label_num ();
672 n_labels = max_label_num () - min_labelno;
673 label_live = (HARD_REG_SET *) xmalloc (n_labels * sizeof (HARD_REG_SET));
674 CLEAR_HARD_REG_SET (ever_live_at_start);
675
676 FOR_EACH_BB_REVERSE (bb)
677 {
678 insn = bb->head;
679 if (GET_CODE (insn) == CODE_LABEL)
680 {
681 HARD_REG_SET live;
682
683 REG_SET_TO_HARD_REG_SET (live,
684 bb->global_live_at_start);
685 compute_use_by_pseudos (&live,
686 bb->global_live_at_start);
687 COPY_HARD_REG_SET (LABEL_LIVE (insn), live);
688 IOR_HARD_REG_SET (ever_live_at_start, live);
689 }
690 }
691
692 /* Initialize last_label_ruid, reload_combine_ruid and reg_state. */
693 last_label_ruid = reload_combine_ruid = 0;
694 for (r = 0; r < FIRST_PSEUDO_REGISTER; r++)
695 {
696 reg_state[r].store_ruid = reload_combine_ruid;
697 if (fixed_regs[r])
698 reg_state[r].use_index = -1;
699 else
700 reg_state[r].use_index = RELOAD_COMBINE_MAX_USES;
701 }
702
703 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
704 {
705 rtx note;
706
707 /* We cannot do our optimization across labels. Invalidating all the use
708 information we have would be costly, so we just note where the label
709 is and then later disable any optimization that would cross it. */
710 if (GET_CODE (insn) == CODE_LABEL)
711 last_label_ruid = reload_combine_ruid;
712 else if (GET_CODE (insn) == BARRIER)
713 for (r = 0; r < FIRST_PSEUDO_REGISTER; r++)
714 if (! fixed_regs[r])
715 reg_state[r].use_index = RELOAD_COMBINE_MAX_USES;
716
717 if (! INSN_P (insn))
718 continue;
719
720 reload_combine_ruid++;
721
722 /* Look for (set (REGX) (CONST_INT))
723 (set (REGX) (PLUS (REGX) (REGY)))
724 ...
725 ... (MEM (REGX)) ...
726 and convert it to
727 (set (REGZ) (CONST_INT))
728 ...
729 ... (MEM (PLUS (REGZ) (REGY)))... .
730
731 First, check that we have (set (REGX) (PLUS (REGX) (REGY)))
732 and that we know all uses of REGX before it dies. */
733 set = single_set (insn);
734 if (set != NULL_RTX
735 && GET_CODE (SET_DEST (set)) == REG
736 && (HARD_REGNO_NREGS (REGNO (SET_DEST (set)),
737 GET_MODE (SET_DEST (set)))
738 == 1)
739 && GET_CODE (SET_SRC (set)) == PLUS
740 && GET_CODE (XEXP (SET_SRC (set), 1)) == REG
741 && rtx_equal_p (XEXP (SET_SRC (set), 0), SET_DEST (set))
742 && last_label_ruid < reg_state[REGNO (SET_DEST (set))].use_ruid)
743 {
744 rtx reg = SET_DEST (set);
745 rtx plus = SET_SRC (set);
746 rtx base = XEXP (plus, 1);
747 rtx prev = prev_nonnote_insn (insn);
748 rtx prev_set = prev ? single_set (prev) : NULL_RTX;
749 unsigned int regno = REGNO (reg);
750 rtx const_reg = NULL_RTX;
751 rtx reg_sum = NULL_RTX;
752
753 /* Now, we need an index register.
754 We'll set index_reg to this index register, const_reg to the
755 register that is to be loaded with the constant
756 (denoted as REGZ in the substitution illustration above),
757 and reg_sum to the register-register that we want to use to
758 substitute uses of REG (typically in MEMs) with.
759 First check REG and BASE for being index registers;
760 we can use them even if they are not dead. */
761 if (TEST_HARD_REG_BIT (reg_class_contents[INDEX_REG_CLASS], regno)
762 || TEST_HARD_REG_BIT (reg_class_contents[INDEX_REG_CLASS],
763 REGNO (base)))
764 {
765 const_reg = reg;
766 reg_sum = plus;
767 }
768 else
769 {
770 /* Otherwise, look for a free index register. Since we have
771 checked above that neiter REG nor BASE are index registers,
772 if we find anything at all, it will be different from these
773 two registers. */
774 for (i = first_index_reg; i <= last_index_reg; i++)
775 {
776 if (TEST_HARD_REG_BIT (reg_class_contents[INDEX_REG_CLASS],
777 i)
778 && reg_state[i].use_index == RELOAD_COMBINE_MAX_USES
779 && reg_state[i].store_ruid <= reg_state[regno].use_ruid
780 && HARD_REGNO_NREGS (i, GET_MODE (reg)) == 1)
781 {
782 rtx index_reg = gen_rtx_REG (GET_MODE (reg), i);
783
784 const_reg = index_reg;
785 reg_sum = gen_rtx_PLUS (GET_MODE (reg), index_reg, base);
786 break;
787 }
788 }
789 }
790
791 /* Check that PREV_SET is indeed (set (REGX) (CONST_INT)) and that
792 (REGY), i.e. BASE, is not clobbered before the last use we'll
793 create. */
794 if (prev_set != 0
795 && GET_CODE (SET_SRC (prev_set)) == CONST_INT
796 && rtx_equal_p (SET_DEST (prev_set), reg)
797 && reg_state[regno].use_index >= 0
798 && (reg_state[REGNO (base)].store_ruid
799 <= reg_state[regno].use_ruid)
800 && reg_sum != 0)
801 {
802 int i;
803
804 /* Change destination register and, if necessary, the
805 constant value in PREV, the constant loading instruction. */
806 validate_change (prev, &SET_DEST (prev_set), const_reg, 1);
807 if (reg_state[regno].offset != const0_rtx)
808 validate_change (prev,
809 &SET_SRC (prev_set),
810 GEN_INT (INTVAL (SET_SRC (prev_set))
811 + INTVAL (reg_state[regno].offset)),
812 1);
813
814 /* Now for every use of REG that we have recorded, replace REG
815 with REG_SUM. */
816 for (i = reg_state[regno].use_index;
817 i < RELOAD_COMBINE_MAX_USES; i++)
818 validate_change (reg_state[regno].reg_use[i].insn,
819 reg_state[regno].reg_use[i].usep,
820 /* Each change must have its own
821 replacement. */
822 copy_rtx (reg_sum), 1);
823
824 if (apply_change_group ())
825 {
826 rtx *np;
827
828 /* Delete the reg-reg addition. */
829 delete_insn (insn);
830
831 if (reg_state[regno].offset != const0_rtx)
832 /* Previous REG_EQUIV / REG_EQUAL notes for PREV
833 are now invalid. */
834 for (np = &REG_NOTES (prev); *np;)
835 {
836 if (REG_NOTE_KIND (*np) == REG_EQUAL
837 || REG_NOTE_KIND (*np) == REG_EQUIV)
838 *np = XEXP (*np, 1);
839 else
840 np = &XEXP (*np, 1);
841 }
842
843 reg_state[regno].use_index = RELOAD_COMBINE_MAX_USES;
844 reg_state[REGNO (const_reg)].store_ruid
845 = reload_combine_ruid;
846 continue;
847 }
848 }
849 }
850
851 note_stores (PATTERN (insn), reload_combine_note_store, NULL);
852
853 if (GET_CODE (insn) == CALL_INSN)
854 {
855 rtx link;
856
857 for (r = 0; r < FIRST_PSEUDO_REGISTER; r++)
858 if (call_used_regs[r])
859 {
860 reg_state[r].use_index = RELOAD_COMBINE_MAX_USES;
861 reg_state[r].store_ruid = reload_combine_ruid;
862 }
863
864 for (link = CALL_INSN_FUNCTION_USAGE (insn); link;
865 link = XEXP (link, 1))
866 {
867 rtx usage_rtx = XEXP (XEXP (link, 0), 0);
868 if (GET_CODE (usage_rtx) == REG)
869 {
870 unsigned int i;
871 unsigned int start_reg = REGNO (usage_rtx);
872 unsigned int num_regs =
873 HARD_REGNO_NREGS (start_reg, GET_MODE (usage_rtx));
874 unsigned int end_reg = start_reg + num_regs - 1;
875 for (i = start_reg; i <= end_reg; i++)
876 if (GET_CODE (XEXP (link, 0)) == CLOBBER)
877 {
878 reg_state[i].use_index = RELOAD_COMBINE_MAX_USES;
879 reg_state[i].store_ruid = reload_combine_ruid;
880 }
881 else
882 reg_state[i].use_index = -1;
883 }
884 }
885
886 }
887 else if (GET_CODE (insn) == JUMP_INSN
888 && GET_CODE (PATTERN (insn)) != RETURN)
889 {
890 /* Non-spill registers might be used at the call destination in
891 some unknown fashion, so we have to mark the unknown use. */
892 HARD_REG_SET *live;
893
894 if ((condjump_p (insn) || condjump_in_parallel_p (insn))
895 && JUMP_LABEL (insn))
896 live = &LABEL_LIVE (JUMP_LABEL (insn));
897 else
898 live = &ever_live_at_start;
899
900 for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; --i)
901 if (TEST_HARD_REG_BIT (*live, i))
902 reg_state[i].use_index = -1;
903 }
904
905 reload_combine_note_use (&PATTERN (insn), insn);
906 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
907 {
908 if (REG_NOTE_KIND (note) == REG_INC
909 && GET_CODE (XEXP (note, 0)) == REG)
910 {
911 int regno = REGNO (XEXP (note, 0));
912
913 reg_state[regno].store_ruid = reload_combine_ruid;
914 reg_state[regno].use_index = -1;
915 }
916 }
917 }
918
919 free (label_live);
920 }
921
922 /* Check if DST is a register or a subreg of a register; if it is,
923 update reg_state[regno].store_ruid and reg_state[regno].use_index
924 accordingly. Called via note_stores from reload_combine. */
925
926 static void
927 reload_combine_note_store (dst, set, data)
928 rtx dst, set;
929 void *data ATTRIBUTE_UNUSED;
930 {
931 int regno = 0;
932 int i;
933 enum machine_mode mode = GET_MODE (dst);
934
935 if (GET_CODE (dst) == SUBREG)
936 {
937 regno = subreg_regno_offset (REGNO (SUBREG_REG (dst)),
938 GET_MODE (SUBREG_REG (dst)),
939 SUBREG_BYTE (dst),
940 GET_MODE (dst));
941 dst = SUBREG_REG (dst);
942 }
943 if (GET_CODE (dst) != REG)
944 return;
945 regno += REGNO (dst);
946
947 /* note_stores might have stripped a STRICT_LOW_PART, so we have to be
948 careful with registers / register parts that are not full words.
949
950 Similarly for ZERO_EXTRACT and SIGN_EXTRACT. */
951 if (GET_CODE (set) != SET
952 || GET_CODE (SET_DEST (set)) == ZERO_EXTRACT
953 || GET_CODE (SET_DEST (set)) == SIGN_EXTRACT
954 || GET_CODE (SET_DEST (set)) == STRICT_LOW_PART)
955 {
956 for (i = HARD_REGNO_NREGS (regno, mode) - 1 + regno; i >= regno; i--)
957 {
958 reg_state[i].use_index = -1;
959 reg_state[i].store_ruid = reload_combine_ruid;
960 }
961 }
962 else
963 {
964 for (i = HARD_REGNO_NREGS (regno, mode) - 1 + regno; i >= regno; i--)
965 {
966 reg_state[i].store_ruid = reload_combine_ruid;
967 reg_state[i].use_index = RELOAD_COMBINE_MAX_USES;
968 }
969 }
970 }
971
972 /* XP points to a piece of rtl that has to be checked for any uses of
973 registers.
974 *XP is the pattern of INSN, or a part of it.
975 Called from reload_combine, and recursively by itself. */
976 static void
977 reload_combine_note_use (xp, insn)
978 rtx *xp, insn;
979 {
980 rtx x = *xp;
981 enum rtx_code code = x->code;
982 const char *fmt;
983 int i, j;
984 rtx offset = const0_rtx; /* For the REG case below. */
985
986 switch (code)
987 {
988 case SET:
989 if (GET_CODE (SET_DEST (x)) == REG)
990 {
991 reload_combine_note_use (&SET_SRC (x), insn);
992 return;
993 }
994 break;
995
996 case USE:
997 /* If this is the USE of a return value, we can't change it. */
998 if (GET_CODE (XEXP (x, 0)) == REG && REG_FUNCTION_VALUE_P (XEXP (x, 0)))
999 {
1000 /* Mark the return register as used in an unknown fashion. */
1001 rtx reg = XEXP (x, 0);
1002 int regno = REGNO (reg);
1003 int nregs = HARD_REGNO_NREGS (regno, GET_MODE (reg));
1004
1005 while (--nregs >= 0)
1006 reg_state[regno + nregs].use_index = -1;
1007 return;
1008 }
1009 break;
1010
1011 case CLOBBER:
1012 if (GET_CODE (SET_DEST (x)) == REG)
1013 {
1014 /* No spurious CLOBBERs of pseudo registers may remain. */
1015 if (REGNO (SET_DEST (x)) >= FIRST_PSEUDO_REGISTER)
1016 abort ();
1017 return;
1018 }
1019 break;
1020
1021 case PLUS:
1022 /* We are interested in (plus (reg) (const_int)) . */
1023 if (GET_CODE (XEXP (x, 0)) != REG
1024 || GET_CODE (XEXP (x, 1)) != CONST_INT)
1025 break;
1026 offset = XEXP (x, 1);
1027 x = XEXP (x, 0);
1028 /* Fall through. */
1029 case REG:
1030 {
1031 int regno = REGNO (x);
1032 int use_index;
1033 int nregs;
1034
1035 /* No spurious USEs of pseudo registers may remain. */
1036 if (regno >= FIRST_PSEUDO_REGISTER)
1037 abort ();
1038
1039 nregs = HARD_REGNO_NREGS (regno, GET_MODE (x));
1040
1041 /* We can't substitute into multi-hard-reg uses. */
1042 if (nregs > 1)
1043 {
1044 while (--nregs >= 0)
1045 reg_state[regno + nregs].use_index = -1;
1046 return;
1047 }
1048
1049 /* If this register is already used in some unknown fashion, we
1050 can't do anything.
1051 If we decrement the index from zero to -1, we can't store more
1052 uses, so this register becomes used in an unknown fashion. */
1053 use_index = --reg_state[regno].use_index;
1054 if (use_index < 0)
1055 return;
1056
1057 if (use_index != RELOAD_COMBINE_MAX_USES - 1)
1058 {
1059 /* We have found another use for a register that is already
1060 used later. Check if the offsets match; if not, mark the
1061 register as used in an unknown fashion. */
1062 if (! rtx_equal_p (offset, reg_state[regno].offset))
1063 {
1064 reg_state[regno].use_index = -1;
1065 return;
1066 }
1067 }
1068 else
1069 {
1070 /* This is the first use of this register we have seen since we
1071 marked it as dead. */
1072 reg_state[regno].offset = offset;
1073 reg_state[regno].use_ruid = reload_combine_ruid;
1074 }
1075 reg_state[regno].reg_use[use_index].insn = insn;
1076 reg_state[regno].reg_use[use_index].usep = xp;
1077 return;
1078 }
1079
1080 default:
1081 break;
1082 }
1083
1084 /* Recursively process the components of X. */
1085 fmt = GET_RTX_FORMAT (code);
1086 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1087 {
1088 if (fmt[i] == 'e')
1089 reload_combine_note_use (&XEXP (x, i), insn);
1090 else if (fmt[i] == 'E')
1091 {
1092 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1093 reload_combine_note_use (&XVECEXP (x, i, j), insn);
1094 }
1095 }
1096 }
1097 \f
1098 /* See if we can reduce the cost of a constant by replacing a move
1099 with an add. We track situations in which a register is set to a
1100 constant or to a register plus a constant. */
1101 /* We cannot do our optimization across labels. Invalidating all the
1102 information about register contents we have would be costly, so we
1103 use move2add_last_label_luid to note where the label is and then
1104 later disable any optimization that would cross it.
1105 reg_offset[n] / reg_base_reg[n] / reg_mode[n] are only valid if
1106 reg_set_luid[n] is greater than move2add_last_label_luid. */
1107 static int reg_set_luid[FIRST_PSEUDO_REGISTER];
1108
1109 /* If reg_base_reg[n] is negative, register n has been set to
1110 reg_offset[n] in mode reg_mode[n] .
1111 If reg_base_reg[n] is non-negative, register n has been set to the
1112 sum of reg_offset[n] and the value of register reg_base_reg[n]
1113 before reg_set_luid[n], calculated in mode reg_mode[n] . */
1114 static HOST_WIDE_INT reg_offset[FIRST_PSEUDO_REGISTER];
1115 static int reg_base_reg[FIRST_PSEUDO_REGISTER];
1116 static enum machine_mode reg_mode[FIRST_PSEUDO_REGISTER];
1117
1118 /* move2add_luid is linearly increased while scanning the instructions
1119 from first to last. It is used to set reg_set_luid in
1120 reload_cse_move2add and move2add_note_store. */
1121 static int move2add_luid;
1122
1123 /* move2add_last_label_luid is set whenever a label is found. Labels
1124 invalidate all previously collected reg_offset data. */
1125 static int move2add_last_label_luid;
1126
1127 /* ??? We don't know how zero / sign extension is handled, hence we
1128 can't go from a narrower to a wider mode. */
1129 #define MODES_OK_FOR_MOVE2ADD(OUTMODE, INMODE) \
1130 (GET_MODE_SIZE (OUTMODE) == GET_MODE_SIZE (INMODE) \
1131 || (GET_MODE_SIZE (OUTMODE) <= GET_MODE_SIZE (INMODE) \
1132 && TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (OUTMODE), \
1133 GET_MODE_BITSIZE (INMODE))))
1134
1135 static void
1136 reload_cse_move2add (first)
1137 rtx first;
1138 {
1139 int i;
1140 rtx insn;
1141
1142 for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; i--)
1143 reg_set_luid[i] = 0;
1144
1145 move2add_last_label_luid = 0;
1146 move2add_luid = 2;
1147 for (insn = first; insn; insn = NEXT_INSN (insn), move2add_luid++)
1148 {
1149 rtx pat, note;
1150
1151 if (GET_CODE (insn) == CODE_LABEL)
1152 {
1153 move2add_last_label_luid = move2add_luid;
1154 /* We're going to increment move2add_luid twice after a
1155 label, so that we can use move2add_last_label_luid + 1 as
1156 the luid for constants. */
1157 move2add_luid++;
1158 continue;
1159 }
1160 if (! INSN_P (insn))
1161 continue;
1162 pat = PATTERN (insn);
1163 /* For simplicity, we only perform this optimization on
1164 straightforward SETs. */
1165 if (GET_CODE (pat) == SET
1166 && GET_CODE (SET_DEST (pat)) == REG)
1167 {
1168 rtx reg = SET_DEST (pat);
1169 int regno = REGNO (reg);
1170 rtx src = SET_SRC (pat);
1171
1172 /* Check if we have valid information on the contents of this
1173 register in the mode of REG. */
1174 if (reg_set_luid[regno] > move2add_last_label_luid
1175 && MODES_OK_FOR_MOVE2ADD (GET_MODE (reg), reg_mode[regno]))
1176 {
1177 /* Try to transform (set (REGX) (CONST_INT A))
1178 ...
1179 (set (REGX) (CONST_INT B))
1180 to
1181 (set (REGX) (CONST_INT A))
1182 ...
1183 (set (REGX) (plus (REGX) (CONST_INT B-A)))
1184 or
1185 (set (REGX) (CONST_INT A))
1186 ...
1187 (set (STRICT_LOW_PART (REGX)) (CONST_INT B))
1188 */
1189
1190 if (GET_CODE (src) == CONST_INT && reg_base_reg[regno] < 0)
1191 {
1192 rtx new_src =
1193 GEN_INT (trunc_int_for_mode (INTVAL (src)
1194 - reg_offset[regno],
1195 GET_MODE (reg)));
1196 /* (set (reg) (plus (reg) (const_int 0))) is not canonical;
1197 use (set (reg) (reg)) instead.
1198 We don't delete this insn, nor do we convert it into a
1199 note, to avoid losing register notes or the return
1200 value flag. jump2 already knows how to get rid of
1201 no-op moves. */
1202 if (new_src == const0_rtx)
1203 {
1204 /* If the constants are different, this is a
1205 truncation, that, if turned into (set (reg)
1206 (reg)), would be discarded. Maybe we should
1207 try a truncMN pattern? */
1208 if (INTVAL (src) == reg_offset [regno])
1209 validate_change (insn, &SET_SRC (pat), reg, 0);
1210 }
1211 else if (rtx_cost (new_src, PLUS) < rtx_cost (src, SET)
1212 && have_add2_insn (reg, new_src))
1213 {
1214 rtx newpat = gen_add2_insn (reg, new_src);
1215 if (INSN_P (newpat) && NEXT_INSN (newpat) == NULL_RTX)
1216 newpat = PATTERN (newpat);
1217 /* If it was the first insn of a sequence or
1218 some other emitted insn, validate_change will
1219 reject it. */
1220 validate_change (insn, &PATTERN (insn),
1221 newpat, 0);
1222 }
1223 else
1224 {
1225 enum machine_mode narrow_mode;
1226 for (narrow_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
1227 narrow_mode != GET_MODE (reg);
1228 narrow_mode = GET_MODE_WIDER_MODE (narrow_mode))
1229 {
1230 if (have_insn_for (STRICT_LOW_PART, narrow_mode)
1231 && ((reg_offset[regno]
1232 & ~GET_MODE_MASK (narrow_mode))
1233 == (INTVAL (src)
1234 & ~GET_MODE_MASK (narrow_mode))))
1235 {
1236 rtx narrow_reg = gen_rtx_REG (narrow_mode,
1237 REGNO (reg));
1238 rtx narrow_src =
1239 GEN_INT (trunc_int_for_mode (INTVAL (src),
1240 narrow_mode));
1241 rtx new_set =
1242 gen_rtx_SET (VOIDmode,
1243 gen_rtx_STRICT_LOW_PART (VOIDmode,
1244 narrow_reg),
1245 narrow_src);
1246 if (validate_change (insn, &PATTERN (insn),
1247 new_set, 0))
1248 break;
1249 }
1250 }
1251 }
1252 reg_set_luid[regno] = move2add_luid;
1253 reg_mode[regno] = GET_MODE (reg);
1254 reg_offset[regno] = INTVAL (src);
1255 continue;
1256 }
1257
1258 /* Try to transform (set (REGX) (REGY))
1259 (set (REGX) (PLUS (REGX) (CONST_INT A)))
1260 ...
1261 (set (REGX) (REGY))
1262 (set (REGX) (PLUS (REGX) (CONST_INT B)))
1263 to
1264 (set (REGX) (REGY))
1265 (set (REGX) (PLUS (REGX) (CONST_INT A)))
1266 ...
1267 (set (REGX) (plus (REGX) (CONST_INT B-A))) */
1268 else if (GET_CODE (src) == REG
1269 && reg_set_luid[regno] == reg_set_luid[REGNO (src)]
1270 && reg_base_reg[regno] == reg_base_reg[REGNO (src)]
1271 && MODES_OK_FOR_MOVE2ADD (GET_MODE (reg),
1272 reg_mode[REGNO (src)]))
1273 {
1274 rtx next = next_nonnote_insn (insn);
1275 rtx set = NULL_RTX;
1276 if (next)
1277 set = single_set (next);
1278 if (set
1279 && SET_DEST (set) == reg
1280 && GET_CODE (SET_SRC (set)) == PLUS
1281 && XEXP (SET_SRC (set), 0) == reg
1282 && GET_CODE (XEXP (SET_SRC (set), 1)) == CONST_INT)
1283 {
1284 rtx src3 = XEXP (SET_SRC (set), 1);
1285 HOST_WIDE_INT added_offset = INTVAL (src3);
1286 HOST_WIDE_INT base_offset = reg_offset[REGNO (src)];
1287 HOST_WIDE_INT regno_offset = reg_offset[regno];
1288 rtx new_src =
1289 GEN_INT (trunc_int_for_mode (added_offset
1290 + base_offset
1291 - regno_offset,
1292 GET_MODE (reg)));
1293 int success = 0;
1294
1295 if (new_src == const0_rtx)
1296 /* See above why we create (set (reg) (reg)) here. */
1297 success
1298 = validate_change (next, &SET_SRC (set), reg, 0);
1299 else if ((rtx_cost (new_src, PLUS)
1300 < COSTS_N_INSNS (1) + rtx_cost (src3, SET))
1301 && have_add2_insn (reg, new_src))
1302 {
1303 rtx newpat = gen_add2_insn (reg, new_src);
1304 if (INSN_P (newpat)
1305 && NEXT_INSN (newpat) == NULL_RTX)
1306 newpat = PATTERN (newpat);
1307 success
1308 = validate_change (next, &PATTERN (next),
1309 newpat, 0);
1310 }
1311 if (success)
1312 delete_insn (insn);
1313 insn = next;
1314 reg_mode[regno] = GET_MODE (reg);
1315 reg_offset[regno] =
1316 trunc_int_for_mode (added_offset + base_offset,
1317 GET_MODE (reg));
1318 continue;
1319 }
1320 }
1321 }
1322 }
1323
1324 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
1325 {
1326 if (REG_NOTE_KIND (note) == REG_INC
1327 && GET_CODE (XEXP (note, 0)) == REG)
1328 {
1329 /* Reset the information about this register. */
1330 int regno = REGNO (XEXP (note, 0));
1331 if (regno < FIRST_PSEUDO_REGISTER)
1332 reg_set_luid[regno] = 0;
1333 }
1334 }
1335 note_stores (PATTERN (insn), move2add_note_store, NULL);
1336
1337 /* If INSN is a conditional branch, we try to extract an
1338 implicit set out of it. */
1339 if (any_condjump_p (insn) && onlyjump_p (insn))
1340 {
1341 rtx cnd = fis_get_condition (insn);
1342
1343 if (cnd != NULL_RTX
1344 && GET_CODE (cnd) == NE
1345 && GET_CODE (XEXP (cnd, 0)) == REG
1346 /* The following two checks, which are also in
1347 move2add_note_store, are intended to reduce the
1348 number of calls to gen_rtx_SET to avoid memory
1349 allocation if possible. */
1350 && SCALAR_INT_MODE_P (GET_MODE (XEXP (cnd, 0)))
1351 && HARD_REGNO_NREGS (REGNO (XEXP (cnd, 0)), GET_MODE (XEXP (cnd, 0))) == 1
1352 && GET_CODE (XEXP (cnd, 1)) == CONST_INT)
1353 {
1354 rtx implicit_set =
1355 gen_rtx_SET (VOIDmode, XEXP (cnd, 0), XEXP (cnd, 1));
1356 move2add_note_store (SET_DEST (implicit_set), implicit_set, 0);
1357 }
1358 }
1359
1360 /* If this is a CALL_INSN, all call used registers are stored with
1361 unknown values. */
1362 if (GET_CODE (insn) == CALL_INSN)
1363 {
1364 for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; i--)
1365 {
1366 if (call_used_regs[i])
1367 /* Reset the information about this register. */
1368 reg_set_luid[i] = 0;
1369 }
1370 }
1371 }
1372 }
1373
1374 /* SET is a SET or CLOBBER that sets DST.
1375 Update reg_set_luid, reg_offset and reg_base_reg accordingly.
1376 Called from reload_cse_move2add via note_stores. */
1377
1378 static void
1379 move2add_note_store (dst, set, data)
1380 rtx dst, set;
1381 void *data ATTRIBUTE_UNUSED;
1382 {
1383 unsigned int regno = 0;
1384 unsigned int i;
1385 enum machine_mode mode = GET_MODE (dst);
1386
1387 if (GET_CODE (dst) == SUBREG)
1388 {
1389 regno = subreg_regno_offset (REGNO (SUBREG_REG (dst)),
1390 GET_MODE (SUBREG_REG (dst)),
1391 SUBREG_BYTE (dst),
1392 GET_MODE (dst));
1393 dst = SUBREG_REG (dst);
1394 }
1395
1396 /* Some targets do argument pushes without adding REG_INC notes. */
1397
1398 if (GET_CODE (dst) == MEM)
1399 {
1400 dst = XEXP (dst, 0);
1401 if (GET_CODE (dst) == PRE_INC || GET_CODE (dst) == POST_INC
1402 || GET_CODE (dst) == PRE_DEC || GET_CODE (dst) == POST_DEC)
1403 reg_set_luid[REGNO (XEXP (dst, 0))] = 0;
1404 return;
1405 }
1406 if (GET_CODE (dst) != REG)
1407 return;
1408
1409 regno += REGNO (dst);
1410
1411 if (SCALAR_INT_MODE_P (mode)
1412 && HARD_REGNO_NREGS (regno, mode) == 1 && GET_CODE (set) == SET
1413 && GET_CODE (SET_DEST (set)) != ZERO_EXTRACT
1414 && GET_CODE (SET_DEST (set)) != SIGN_EXTRACT
1415 && GET_CODE (SET_DEST (set)) != STRICT_LOW_PART)
1416 {
1417 rtx src = SET_SRC (set);
1418 rtx base_reg;
1419 HOST_WIDE_INT offset;
1420 int base_regno;
1421 /* This may be different from mode, if SET_DEST (set) is a
1422 SUBREG. */
1423 enum machine_mode dst_mode = GET_MODE (dst);
1424
1425 switch (GET_CODE (src))
1426 {
1427 case PLUS:
1428 if (GET_CODE (XEXP (src, 0)) == REG)
1429 {
1430 base_reg = XEXP (src, 0);
1431
1432 if (GET_CODE (XEXP (src, 1)) == CONST_INT)
1433 offset = INTVAL (XEXP (src, 1));
1434 else if (GET_CODE (XEXP (src, 1)) == REG
1435 && (reg_set_luid[REGNO (XEXP (src, 1))]
1436 > move2add_last_label_luid)
1437 && (MODES_OK_FOR_MOVE2ADD
1438 (dst_mode, reg_mode[REGNO (XEXP (src, 1))])))
1439 {
1440 if (reg_base_reg[REGNO (XEXP (src, 1))] < 0)
1441 offset = reg_offset[REGNO (XEXP (src, 1))];
1442 /* Maybe the first register is known to be a
1443 constant. */
1444 else if (reg_set_luid[REGNO (base_reg)]
1445 > move2add_last_label_luid
1446 && (MODES_OK_FOR_MOVE2ADD
1447 (dst_mode, reg_mode[REGNO (XEXP (src, 1))]))
1448 && reg_base_reg[REGNO (base_reg)] < 0)
1449 {
1450 offset = reg_offset[REGNO (base_reg)];
1451 base_reg = XEXP (src, 1);
1452 }
1453 else
1454 goto invalidate;
1455 }
1456 else
1457 goto invalidate;
1458
1459 break;
1460 }
1461
1462 goto invalidate;
1463
1464 case REG:
1465 base_reg = src;
1466 offset = 0;
1467 break;
1468
1469 case CONST_INT:
1470 /* Start tracking the register as a constant. */
1471 reg_base_reg[regno] = -1;
1472 reg_offset[regno] = INTVAL (SET_SRC (set));
1473 /* We assign the same luid to all registers set to constants. */
1474 reg_set_luid[regno] = move2add_last_label_luid + 1;
1475 reg_mode[regno] = mode;
1476 return;
1477
1478 default:
1479 invalidate:
1480 /* Invalidate the contents of the register. */
1481 reg_set_luid[regno] = 0;
1482 return;
1483 }
1484
1485 base_regno = REGNO (base_reg);
1486 /* If information about the base register is not valid, set it
1487 up as a new base register, pretending its value is known
1488 starting from the current insn. */
1489 if (reg_set_luid[base_regno] <= move2add_last_label_luid)
1490 {
1491 reg_base_reg[base_regno] = base_regno;
1492 reg_offset[base_regno] = 0;
1493 reg_set_luid[base_regno] = move2add_luid;
1494 reg_mode[base_regno] = mode;
1495 }
1496 else if (! MODES_OK_FOR_MOVE2ADD (dst_mode,
1497 reg_mode[base_regno]))
1498 goto invalidate;
1499
1500 reg_mode[regno] = mode;
1501
1502 /* Copy base information from our base register. */
1503 reg_set_luid[regno] = reg_set_luid[base_regno];
1504 reg_base_reg[regno] = reg_base_reg[base_regno];
1505
1506 /* Compute the sum of the offsets or constants. */
1507 reg_offset[regno] = trunc_int_for_mode (offset
1508 + reg_offset[base_regno],
1509 dst_mode);
1510 }
1511 else
1512 {
1513 unsigned int endregno = regno + HARD_REGNO_NREGS (regno, mode);
1514
1515 for (i = regno; i < endregno; i++)
1516 /* Reset the information about this register. */
1517 reg_set_luid[i] = 0;
1518 }
1519 }