OSDN Git Service

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