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