OSDN Git Service

609a06d26b4723b2554f8a0830cbb8496b92d06c
[pf3gnuchains/gcc-fork.git] / gcc / regcprop.c
1 /* Copy propagation on hard registers for the GNU compiler.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
3    2010  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
8    under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3, or (at your option)
10    any later version.
11
12    GCC is distributed in the hope that it will be useful, but WITHOUT
13    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
15    License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with GCC; see the file COPYING3.  If not see
19    <http://www.gnu.org/licenses/>.  */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "rtl.h"
26 #include "tm_p.h"
27 #include "insn-config.h"
28 #include "regs.h"
29 #include "addresses.h"
30 #include "hard-reg-set.h"
31 #include "basic-block.h"
32 #include "reload.h"
33 #include "output.h"
34 #include "function.h"
35 #include "recog.h"
36 #include "flags.h"
37 #include "toplev.h"
38 #include "obstack.h"
39 #include "timevar.h"
40 #include "tree-pass.h"
41 #include "df.h"
42
43 /* The following code does forward propagation of hard register copies.
44    The object is to eliminate as many dependencies as possible, so that
45    we have the most scheduling freedom.  As a side effect, we also clean
46    up some silly register allocation decisions made by reload.  This
47    code may be obsoleted by a new register allocator.  */
48
49 /* For each register, we have a list of registers that contain the same
50    value.  The OLDEST_REGNO field points to the head of the list, and
51    the NEXT_REGNO field runs through the list.  The MODE field indicates
52    what mode the data is known to be in; this field is VOIDmode when the
53    register is not known to contain valid data.  */
54
55 struct value_data_entry
56 {
57   enum machine_mode mode;
58   unsigned int oldest_regno;
59   unsigned int next_regno;
60 };
61
62 struct value_data
63 {
64   struct value_data_entry e[FIRST_PSEUDO_REGISTER];
65   unsigned int max_value_regs;
66 };
67
68 static void kill_value_one_regno (unsigned, struct value_data *);
69 static void kill_value_regno (unsigned, unsigned, struct value_data *);
70 static void kill_value (rtx, struct value_data *);
71 static void set_value_regno (unsigned, enum machine_mode, struct value_data *);
72 static void init_value_data (struct value_data *);
73 static void kill_clobbered_value (rtx, const_rtx, void *);
74 static void kill_set_value (rtx, const_rtx, void *);
75 static int kill_autoinc_value (rtx *, void *);
76 static void copy_value (rtx, rtx, struct value_data *);
77 static bool mode_change_ok (enum machine_mode, enum machine_mode,
78                             unsigned int);
79 static rtx maybe_mode_change (enum machine_mode, enum machine_mode,
80                               enum machine_mode, unsigned int, unsigned int);
81 static rtx find_oldest_value_reg (enum reg_class, rtx, struct value_data *);
82 static bool replace_oldest_value_reg (rtx *, enum reg_class, rtx,
83                                       struct value_data *);
84 static bool replace_oldest_value_addr (rtx *, enum reg_class,
85                                        enum machine_mode, rtx,
86                                        struct value_data *);
87 static bool replace_oldest_value_mem (rtx, rtx, struct value_data *);
88 static bool copyprop_hardreg_forward_1 (basic_block, struct value_data *);
89 extern void debug_value_data (struct value_data *);
90 #ifdef ENABLE_CHECKING
91 static void validate_value_data (struct value_data *);
92 #endif
93
94 /* Kill register REGNO.  This involves removing it from any value
95    lists, and resetting the value mode to VOIDmode.  This is only a
96    helper function; it does not handle any hard registers overlapping
97    with REGNO.  */
98
99 static void
100 kill_value_one_regno (unsigned int regno, struct value_data *vd)
101 {
102   unsigned int i, next;
103
104   if (vd->e[regno].oldest_regno != regno)
105     {
106       for (i = vd->e[regno].oldest_regno;
107            vd->e[i].next_regno != regno;
108            i = vd->e[i].next_regno)
109         continue;
110       vd->e[i].next_regno = vd->e[regno].next_regno;
111     }
112   else if ((next = vd->e[regno].next_regno) != INVALID_REGNUM)
113     {
114       for (i = next; i != INVALID_REGNUM; i = vd->e[i].next_regno)
115         vd->e[i].oldest_regno = next;
116     }
117
118   vd->e[regno].mode = VOIDmode;
119   vd->e[regno].oldest_regno = regno;
120   vd->e[regno].next_regno = INVALID_REGNUM;
121
122 #ifdef ENABLE_CHECKING
123   validate_value_data (vd);
124 #endif
125 }
126
127 /* Kill the value in register REGNO for NREGS, and any other registers
128    whose values overlap.  */
129
130 static void
131 kill_value_regno (unsigned int regno, unsigned int nregs,
132                   struct value_data *vd)
133 {
134   unsigned int j;
135
136   /* Kill the value we're told to kill.  */
137   for (j = 0; j < nregs; ++j)
138     kill_value_one_regno (regno + j, vd);
139
140   /* Kill everything that overlapped what we're told to kill.  */
141   if (regno < vd->max_value_regs)
142     j = 0;
143   else
144     j = regno - vd->max_value_regs;
145   for (; j < regno; ++j)
146     {
147       unsigned int i, n;
148       if (vd->e[j].mode == VOIDmode)
149         continue;
150       n = hard_regno_nregs[j][vd->e[j].mode];
151       if (j + n > regno)
152         for (i = 0; i < n; ++i)
153           kill_value_one_regno (j + i, vd);
154     }
155 }
156
157 /* Kill X.  This is a convenience function wrapping kill_value_regno
158    so that we mind the mode the register is in.  */
159
160 static void
161 kill_value (rtx x, struct value_data *vd)
162 {
163   rtx orig_rtx = x;
164
165   if (GET_CODE (x) == SUBREG)
166     {
167       x = simplify_subreg (GET_MODE (x), SUBREG_REG (x),
168                            GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
169       if (x == NULL_RTX)
170         x = SUBREG_REG (orig_rtx);
171     }
172   if (REG_P (x))
173     {
174       unsigned int regno = REGNO (x);
175       unsigned int n = hard_regno_nregs[regno][GET_MODE (x)];
176
177       kill_value_regno (regno, n, vd);
178     }
179 }
180
181 /* Remember that REGNO is valid in MODE.  */
182
183 static void
184 set_value_regno (unsigned int regno, enum machine_mode mode,
185                  struct value_data *vd)
186 {
187   unsigned int nregs;
188
189   vd->e[regno].mode = mode;
190
191   nregs = hard_regno_nregs[regno][mode];
192   if (nregs > vd->max_value_regs)
193     vd->max_value_regs = nregs;
194 }
195
196 /* Initialize VD such that there are no known relationships between regs.  */
197
198 static void
199 init_value_data (struct value_data *vd)
200 {
201   int i;
202   for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
203     {
204       vd->e[i].mode = VOIDmode;
205       vd->e[i].oldest_regno = i;
206       vd->e[i].next_regno = INVALID_REGNUM;
207     }
208   vd->max_value_regs = 0;
209 }
210
211 /* Called through note_stores.  If X is clobbered, kill its value.  */
212
213 static void
214 kill_clobbered_value (rtx x, const_rtx set, void *data)
215 {
216   struct value_data *const vd = (struct value_data *) data;
217   if (GET_CODE (set) == CLOBBER)
218     kill_value (x, vd);
219 }
220
221 /* Called through note_stores.  If X is set, not clobbered, kill its
222    current value and install it as the root of its own value list.  */
223
224 static void
225 kill_set_value (rtx x, const_rtx set, void *data)
226 {
227   struct value_data *const vd = (struct value_data *) data;
228   if (GET_CODE (set) != CLOBBER)
229     {
230       kill_value (x, vd);
231       if (REG_P (x))
232         set_value_regno (REGNO (x), GET_MODE (x), vd);
233     }
234 }
235
236 /* Called through for_each_rtx.  Kill any register used as the base of an
237    auto-increment expression, and install that register as the root of its
238    own value list.  */
239
240 static int
241 kill_autoinc_value (rtx *px, void *data)
242 {
243   rtx x = *px;
244   struct value_data *const vd = (struct value_data *) data;
245
246   if (GET_RTX_CLASS (GET_CODE (x)) == RTX_AUTOINC)
247     {
248       x = XEXP (x, 0);
249       kill_value (x, vd);
250       set_value_regno (REGNO (x), GET_MODE (x), vd);
251       return -1;
252     }
253
254   return 0;
255 }
256
257 /* Assert that SRC has been copied to DEST.  Adjust the data structures
258    to reflect that SRC contains an older copy of the shared value.  */
259
260 static void
261 copy_value (rtx dest, rtx src, struct value_data *vd)
262 {
263   unsigned int dr = REGNO (dest);
264   unsigned int sr = REGNO (src);
265   unsigned int dn, sn;
266   unsigned int i;
267
268   /* ??? At present, it's possible to see noop sets.  It'd be nice if
269      this were cleaned up beforehand...  */
270   if (sr == dr)
271     return;
272
273   /* Do not propagate copies to the stack pointer, as that can leave
274      memory accesses with no scheduling dependency on the stack update.  */
275   if (dr == STACK_POINTER_REGNUM)
276     return;
277
278   /* Likewise with the frame pointer, if we're using one.  */
279   if (frame_pointer_needed && dr == HARD_FRAME_POINTER_REGNUM)
280     return;
281
282   /* Do not propagate copies to fixed or global registers, patterns
283      can be relying to see particular fixed register or users can
284      expect the chosen global register in asm.  */
285   if (fixed_regs[dr] || global_regs[dr])
286     return;
287
288   /* If SRC and DEST overlap, don't record anything.  */
289   dn = hard_regno_nregs[dr][GET_MODE (dest)];
290   sn = hard_regno_nregs[sr][GET_MODE (dest)];
291   if ((dr > sr && dr < sr + sn)
292       || (sr > dr && sr < dr + dn))
293     return;
294
295   /* If SRC had no assigned mode (i.e. we didn't know it was live)
296      assign it now and assume the value came from an input argument
297      or somesuch.  */
298   if (vd->e[sr].mode == VOIDmode)
299     set_value_regno (sr, vd->e[dr].mode, vd);
300
301   /* If we are narrowing the input to a smaller number of hard regs,
302      and it is in big endian, we are really extracting a high part.
303      Since we generally associate a low part of a value with the value itself,
304      we must not do the same for the high part.
305      Note we can still get low parts for the same mode combination through
306      a two-step copy involving differently sized hard regs.
307      Assume hard regs fr* are 32 bits bits each, while r* are 64 bits each:
308      (set (reg:DI r0) (reg:DI fr0))
309      (set (reg:SI fr2) (reg:SI r0))
310      loads the low part of (reg:DI fr0) - i.e. fr1 - into fr2, while:
311      (set (reg:SI fr2) (reg:SI fr0))
312      loads the high part of (reg:DI fr0) into fr2.
313
314      We can't properly represent the latter case in our tables, so don't
315      record anything then.  */
316   else if (sn < (unsigned int) hard_regno_nregs[sr][vd->e[sr].mode]
317            && (GET_MODE_SIZE (vd->e[sr].mode) > UNITS_PER_WORD
318                ? WORDS_BIG_ENDIAN : BYTES_BIG_ENDIAN))
319     return;
320
321   /* If SRC had been assigned a mode narrower than the copy, we can't
322      link DEST into the chain, because not all of the pieces of the
323      copy came from oldest_regno.  */
324   else if (sn > (unsigned int) hard_regno_nregs[sr][vd->e[sr].mode])
325     return;
326
327   /* Link DR at the end of the value chain used by SR.  */
328
329   vd->e[dr].oldest_regno = vd->e[sr].oldest_regno;
330
331   for (i = sr; vd->e[i].next_regno != INVALID_REGNUM; i = vd->e[i].next_regno)
332     continue;
333   vd->e[i].next_regno = dr;
334
335 #ifdef ENABLE_CHECKING
336   validate_value_data (vd);
337 #endif
338 }
339
340 /* Return true if a mode change from ORIG to NEW is allowed for REGNO.  */
341
342 static bool
343 mode_change_ok (enum machine_mode orig_mode, enum machine_mode new_mode,
344                 unsigned int regno ATTRIBUTE_UNUSED)
345 {
346   if (GET_MODE_SIZE (orig_mode) < GET_MODE_SIZE (new_mode))
347     return false;
348
349 #ifdef CANNOT_CHANGE_MODE_CLASS
350   return !REG_CANNOT_CHANGE_MODE_P (regno, orig_mode, new_mode);
351 #endif
352
353   return true;
354 }
355
356 /* Register REGNO was originally set in ORIG_MODE.  It - or a copy of it -
357    was copied in COPY_MODE to COPY_REGNO, and then COPY_REGNO was accessed
358    in NEW_MODE.
359    Return a NEW_MODE rtx for REGNO if that's OK, otherwise return NULL_RTX.  */
360
361 static rtx
362 maybe_mode_change (enum machine_mode orig_mode, enum machine_mode copy_mode,
363                    enum machine_mode new_mode, unsigned int regno,
364                    unsigned int copy_regno ATTRIBUTE_UNUSED)
365 {
366   if (GET_MODE_SIZE (copy_mode) < GET_MODE_SIZE (orig_mode)
367       && GET_MODE_SIZE (copy_mode) < GET_MODE_SIZE (new_mode))
368     return NULL_RTX;
369
370   if (orig_mode == new_mode)
371     return gen_rtx_raw_REG (new_mode, regno);
372   else if (mode_change_ok (orig_mode, new_mode, regno))
373     {
374       int copy_nregs = hard_regno_nregs[copy_regno][copy_mode];
375       int use_nregs = hard_regno_nregs[copy_regno][new_mode];
376       int copy_offset
377         = GET_MODE_SIZE (copy_mode) / copy_nregs * (copy_nregs - use_nregs);
378       int offset
379         = GET_MODE_SIZE (orig_mode) - GET_MODE_SIZE (new_mode) - copy_offset;
380       int byteoffset = offset % UNITS_PER_WORD;
381       int wordoffset = offset - byteoffset;
382
383       offset = ((WORDS_BIG_ENDIAN ? wordoffset : 0)
384                 + (BYTES_BIG_ENDIAN ? byteoffset : 0));
385       return gen_rtx_raw_REG (new_mode,
386                               regno + subreg_regno_offset (regno, orig_mode,
387                                                            offset,
388                                                            new_mode));
389     }
390   return NULL_RTX;
391 }
392
393 /* Find the oldest copy of the value contained in REGNO that is in
394    register class CL and has mode MODE.  If found, return an rtx
395    of that oldest register, otherwise return NULL.  */
396
397 static rtx
398 find_oldest_value_reg (enum reg_class cl, rtx reg, struct value_data *vd)
399 {
400   unsigned int regno = REGNO (reg);
401   enum machine_mode mode = GET_MODE (reg);
402   unsigned int i;
403
404   /* If we are accessing REG in some mode other that what we set it in,
405      make sure that the replacement is valid.  In particular, consider
406         (set (reg:DI r11) (...))
407         (set (reg:SI r9) (reg:SI r11))
408         (set (reg:SI r10) (...))
409         (set (...) (reg:DI r9))
410      Replacing r9 with r11 is invalid.  */
411   if (mode != vd->e[regno].mode)
412     {
413       if (hard_regno_nregs[regno][mode]
414           > hard_regno_nregs[regno][vd->e[regno].mode])
415         return NULL_RTX;
416     }
417
418   for (i = vd->e[regno].oldest_regno; i != regno; i = vd->e[i].next_regno)
419     {
420       enum machine_mode oldmode = vd->e[i].mode;
421       rtx new_rtx;
422
423       if (!in_hard_reg_set_p (reg_class_contents[cl], mode, i))
424         return NULL_RTX;
425
426       new_rtx = maybe_mode_change (oldmode, vd->e[regno].mode, mode, i, regno);
427       if (new_rtx)
428         {
429           ORIGINAL_REGNO (new_rtx) = ORIGINAL_REGNO (reg);
430           REG_ATTRS (new_rtx) = REG_ATTRS (reg);
431           REG_POINTER (new_rtx) = REG_POINTER (reg);
432           return new_rtx;
433         }
434     }
435
436   return NULL_RTX;
437 }
438
439 /* If possible, replace the register at *LOC with the oldest register
440    in register class CL.  Return true if successfully replaced.  */
441
442 static bool
443 replace_oldest_value_reg (rtx *loc, enum reg_class cl, rtx insn,
444                           struct value_data *vd)
445 {
446   rtx new_rtx = find_oldest_value_reg (cl, *loc, vd);
447   if (new_rtx)
448     {
449       if (dump_file)
450         fprintf (dump_file, "insn %u: replaced reg %u with %u\n",
451                  INSN_UID (insn), REGNO (*loc), REGNO (new_rtx));
452
453       validate_change (insn, loc, new_rtx, 1);
454       return true;
455     }
456   return false;
457 }
458
459 /* Similar to replace_oldest_value_reg, but *LOC contains an address.
460    Adapted from find_reloads_address_1.  CL is INDEX_REG_CLASS or
461    BASE_REG_CLASS depending on how the register is being considered.  */
462
463 static bool
464 replace_oldest_value_addr (rtx *loc, enum reg_class cl,
465                            enum machine_mode mode, rtx insn,
466                            struct value_data *vd)
467 {
468   rtx x = *loc;
469   RTX_CODE code = GET_CODE (x);
470   const char *fmt;
471   int i, j;
472   bool changed = false;
473
474   switch (code)
475     {
476     case PLUS:
477       if (DEBUG_INSN_P (insn))
478         break;
479
480       {
481         rtx orig_op0 = XEXP (x, 0);
482         rtx orig_op1 = XEXP (x, 1);
483         RTX_CODE code0 = GET_CODE (orig_op0);
484         RTX_CODE code1 = GET_CODE (orig_op1);
485         rtx op0 = orig_op0;
486         rtx op1 = orig_op1;
487         rtx *locI = NULL;
488         rtx *locB = NULL;
489         enum rtx_code index_code = SCRATCH;
490
491         if (GET_CODE (op0) == SUBREG)
492           {
493             op0 = SUBREG_REG (op0);
494             code0 = GET_CODE (op0);
495           }
496
497         if (GET_CODE (op1) == SUBREG)
498           {
499             op1 = SUBREG_REG (op1);
500             code1 = GET_CODE (op1);
501           }
502
503         if (code0 == MULT || code0 == SIGN_EXTEND || code0 == TRUNCATE
504             || code0 == ZERO_EXTEND || code1 == MEM)
505           {
506             locI = &XEXP (x, 0);
507             locB = &XEXP (x, 1);
508             index_code = GET_CODE (*locI);
509           }
510         else if (code1 == MULT || code1 == SIGN_EXTEND || code1 == TRUNCATE
511                  || code1 == ZERO_EXTEND || code0 == MEM)
512           {
513             locI = &XEXP (x, 1);
514             locB = &XEXP (x, 0);
515             index_code = GET_CODE (*locI);
516           }
517         else if (code0 == CONST_INT || code0 == CONST
518                  || code0 == SYMBOL_REF || code0 == LABEL_REF)
519           {
520             locB = &XEXP (x, 1);
521             index_code = GET_CODE (XEXP (x, 0));
522           }
523         else if (code1 == CONST_INT || code1 == CONST
524                  || code1 == SYMBOL_REF || code1 == LABEL_REF)
525           {
526             locB = &XEXP (x, 0);
527             index_code = GET_CODE (XEXP (x, 1));
528           }
529         else if (code0 == REG && code1 == REG)
530           {
531             int index_op;
532             unsigned regno0 = REGNO (op0), regno1 = REGNO (op1);
533
534             if (REGNO_OK_FOR_INDEX_P (regno1)
535                 && regno_ok_for_base_p (regno0, mode, PLUS, REG))
536               index_op = 1;
537             else if (REGNO_OK_FOR_INDEX_P (regno0)
538                      && regno_ok_for_base_p (regno1, mode, PLUS, REG))
539               index_op = 0;
540             else if (regno_ok_for_base_p (regno0, mode, PLUS, REG)
541                      || REGNO_OK_FOR_INDEX_P (regno1))
542               index_op = 1;
543             else if (regno_ok_for_base_p (regno1, mode, PLUS, REG))
544               index_op = 0;
545             else
546               index_op = 1;
547
548             locI = &XEXP (x, index_op);
549             locB = &XEXP (x, !index_op);
550             index_code = GET_CODE (*locI);
551           }
552         else if (code0 == REG)
553           {
554             locI = &XEXP (x, 0);
555             locB = &XEXP (x, 1);
556             index_code = GET_CODE (*locI);
557           }
558         else if (code1 == REG)
559           {
560             locI = &XEXP (x, 1);
561             locB = &XEXP (x, 0);
562             index_code = GET_CODE (*locI);
563           }
564
565         if (locI)
566           changed |= replace_oldest_value_addr (locI, INDEX_REG_CLASS, mode,
567                                                 insn, vd);
568         if (locB)
569           changed |= replace_oldest_value_addr (locB,
570                                                 base_reg_class (mode, PLUS,
571                                                                 index_code),
572                                                 mode, insn, vd);
573         return changed;
574       }
575
576     case POST_INC:
577     case POST_DEC:
578     case POST_MODIFY:
579     case PRE_INC:
580     case PRE_DEC:
581     case PRE_MODIFY:
582       return false;
583
584     case MEM:
585       return replace_oldest_value_mem (x, insn, vd);
586
587     case REG:
588       return replace_oldest_value_reg (loc, cl, insn, vd);
589
590     default:
591       break;
592     }
593
594   fmt = GET_RTX_FORMAT (code);
595   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
596     {
597       if (fmt[i] == 'e')
598         changed |= replace_oldest_value_addr (&XEXP (x, i), cl, mode,
599                                               insn, vd);
600       else if (fmt[i] == 'E')
601         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
602           changed |= replace_oldest_value_addr (&XVECEXP (x, i, j), cl,
603                                                 mode, insn, vd);
604     }
605
606   return changed;
607 }
608
609 /* Similar to replace_oldest_value_reg, but X contains a memory.  */
610
611 static bool
612 replace_oldest_value_mem (rtx x, rtx insn, struct value_data *vd)
613 {
614   enum reg_class cl;
615
616   if (DEBUG_INSN_P (insn))
617     cl = ALL_REGS;
618   else
619     cl = base_reg_class (GET_MODE (x), MEM, SCRATCH);
620
621   return replace_oldest_value_addr (&XEXP (x, 0), cl,
622                                     GET_MODE (x), insn, vd);
623 }
624
625 /* Perform the forward copy propagation on basic block BB.  */
626
627 static bool
628 copyprop_hardreg_forward_1 (basic_block bb, struct value_data *vd)
629 {
630   bool anything_changed = false;
631   rtx insn;
632
633   for (insn = BB_HEAD (bb); ; insn = NEXT_INSN (insn))
634     {
635       int n_ops, i, alt, predicated;
636       bool is_asm, any_replacements;
637       rtx set;
638       bool replaced[MAX_RECOG_OPERANDS];
639       bool changed = false;
640
641       if (!NONDEBUG_INSN_P (insn))
642         {
643           if (DEBUG_INSN_P (insn))
644             {
645               rtx loc = INSN_VAR_LOCATION_LOC (insn);
646               if (!VAR_LOC_UNKNOWN_P (loc)
647                   && replace_oldest_value_addr (&INSN_VAR_LOCATION_LOC (insn),
648                                                 ALL_REGS, GET_MODE (loc),
649                                                 insn, vd))
650                 {
651                   changed = apply_change_group ();
652                   gcc_assert (changed);
653                   anything_changed = true;
654                 }
655             }
656
657           if (insn == BB_END (bb))
658             break;
659           else
660             continue;
661         }
662
663       set = single_set (insn);
664       extract_insn (insn);
665       if (! constrain_operands (1))
666         fatal_insn_not_found (insn);
667       preprocess_constraints ();
668       alt = which_alternative;
669       n_ops = recog_data.n_operands;
670       is_asm = asm_noperands (PATTERN (insn)) >= 0;
671
672       /* Simplify the code below by rewriting things to reflect
673          matching constraints.  Also promote OP_OUT to OP_INOUT
674          in predicated instructions.  */
675
676       predicated = GET_CODE (PATTERN (insn)) == COND_EXEC;
677       for (i = 0; i < n_ops; ++i)
678         {
679           int matches = recog_op_alt[i][alt].matches;
680           if (matches >= 0)
681             recog_op_alt[i][alt].cl = recog_op_alt[matches][alt].cl;
682           if (matches >= 0 || recog_op_alt[i][alt].matched >= 0
683               || (predicated && recog_data.operand_type[i] == OP_OUT))
684             recog_data.operand_type[i] = OP_INOUT;
685         }
686
687       /* For each earlyclobber operand, zap the value data.  */
688       for (i = 0; i < n_ops; i++)
689         if (recog_op_alt[i][alt].earlyclobber)
690           kill_value (recog_data.operand[i], vd);
691
692       /* Within asms, a clobber cannot overlap inputs or outputs.
693          I wouldn't think this were true for regular insns, but
694          scan_rtx treats them like that...  */
695       note_stores (PATTERN (insn), kill_clobbered_value, vd);
696
697       /* Kill all auto-incremented values.  */
698       /* ??? REG_INC is useless, since stack pushes aren't done that way.  */
699       for_each_rtx (&PATTERN (insn), kill_autoinc_value, vd);
700
701       /* Kill all early-clobbered operands.  */
702       for (i = 0; i < n_ops; i++)
703         if (recog_op_alt[i][alt].earlyclobber)
704           kill_value (recog_data.operand[i], vd);
705
706       /* Special-case plain move instructions, since we may well
707          be able to do the move from a different register class.  */
708       if (set && REG_P (SET_SRC (set)))
709         {
710           rtx src = SET_SRC (set);
711           unsigned int regno = REGNO (src);
712           enum machine_mode mode = GET_MODE (src);
713           unsigned int i;
714           rtx new_rtx;
715
716           /* If we are accessing SRC in some mode other that what we
717              set it in, make sure that the replacement is valid.  */
718           if (mode != vd->e[regno].mode)
719             {
720               if (hard_regno_nregs[regno][mode]
721                   > hard_regno_nregs[regno][vd->e[regno].mode])
722                 goto no_move_special_case;
723             }
724
725           /* If the destination is also a register, try to find a source
726              register in the same class.  */
727           if (REG_P (SET_DEST (set)))
728             {
729               new_rtx = find_oldest_value_reg (REGNO_REG_CLASS (regno), src, vd);
730               if (new_rtx && validate_change (insn, &SET_SRC (set), new_rtx, 0))
731                 {
732                   if (dump_file)
733                     fprintf (dump_file,
734                              "insn %u: replaced reg %u with %u\n",
735                              INSN_UID (insn), regno, REGNO (new_rtx));
736                   changed = true;
737                   goto did_replacement;
738                 }
739             }
740
741           /* Otherwise, try all valid registers and see if its valid.  */
742           for (i = vd->e[regno].oldest_regno; i != regno;
743                i = vd->e[i].next_regno)
744             {
745               new_rtx = maybe_mode_change (vd->e[i].mode, vd->e[regno].mode,
746                                        mode, i, regno);
747               if (new_rtx != NULL_RTX)
748                 {
749                   if (validate_change (insn, &SET_SRC (set), new_rtx, 0))
750                     {
751                       ORIGINAL_REGNO (new_rtx) = ORIGINAL_REGNO (src);
752                       REG_ATTRS (new_rtx) = REG_ATTRS (src);
753                       REG_POINTER (new_rtx) = REG_POINTER (src);
754                       if (dump_file)
755                         fprintf (dump_file,
756                                  "insn %u: replaced reg %u with %u\n",
757                                  INSN_UID (insn), regno, REGNO (new_rtx));
758                       changed = true;
759                       goto did_replacement;
760                     }
761                 }
762             }
763         }
764       no_move_special_case:
765
766       any_replacements = false;
767
768       /* For each input operand, replace a hard register with the
769          eldest live copy that's in an appropriate register class.  */
770       for (i = 0; i < n_ops; i++)
771         {
772           replaced[i] = false;
773
774           /* Don't scan match_operand here, since we've no reg class
775              information to pass down.  Any operands that we could
776              substitute in will be represented elsewhere.  */
777           if (recog_data.constraints[i][0] == '\0')
778             continue;
779
780           /* Don't replace in asms intentionally referencing hard regs.  */
781           if (is_asm && REG_P (recog_data.operand[i])
782               && (REGNO (recog_data.operand[i])
783                   == ORIGINAL_REGNO (recog_data.operand[i])))
784             continue;
785
786           if (recog_data.operand_type[i] == OP_IN)
787             {
788               if (recog_op_alt[i][alt].is_address)
789                 replaced[i]
790                   = replace_oldest_value_addr (recog_data.operand_loc[i],
791                                                recog_op_alt[i][alt].cl,
792                                                VOIDmode, insn, vd);
793               else if (REG_P (recog_data.operand[i]))
794                 replaced[i]
795                   = replace_oldest_value_reg (recog_data.operand_loc[i],
796                                               recog_op_alt[i][alt].cl,
797                                               insn, vd);
798               else if (MEM_P (recog_data.operand[i]))
799                 replaced[i] = replace_oldest_value_mem (recog_data.operand[i],
800                                                         insn, vd);
801             }
802           else if (MEM_P (recog_data.operand[i]))
803             replaced[i] = replace_oldest_value_mem (recog_data.operand[i],
804                                                     insn, vd);
805
806           /* If we performed any replacement, update match_dups.  */
807           if (replaced[i])
808             {
809               int j;
810               rtx new_rtx;
811
812               new_rtx = *recog_data.operand_loc[i];
813               recog_data.operand[i] = new_rtx;
814               for (j = 0; j < recog_data.n_dups; j++)
815                 if (recog_data.dup_num[j] == i)
816                   validate_unshare_change (insn, recog_data.dup_loc[j], new_rtx, 1);
817
818               any_replacements = true;
819             }
820         }
821
822       if (any_replacements)
823         {
824           if (! apply_change_group ())
825             {
826               for (i = 0; i < n_ops; i++)
827                 if (replaced[i])
828                   {
829                     rtx old = *recog_data.operand_loc[i];
830                     recog_data.operand[i] = old;
831                   }
832
833               if (dump_file)
834                 fprintf (dump_file,
835                          "insn %u: reg replacements not verified\n",
836                          INSN_UID (insn));
837             }
838           else
839             changed = true;
840         }
841
842     did_replacement:
843       if (changed)
844         anything_changed = true;
845
846       /* Clobber call-clobbered registers.  */
847       if (CALL_P (insn))
848         for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
849           if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
850             kill_value_regno (i, 1, vd);
851
852       /* Notice stores.  */
853       note_stores (PATTERN (insn), kill_set_value, vd);
854
855       /* Notice copies.  */
856       if (set && REG_P (SET_DEST (set)) && REG_P (SET_SRC (set)))
857         copy_value (SET_DEST (set), SET_SRC (set), vd);
858
859       if (insn == BB_END (bb))
860         break;
861     }
862
863   return anything_changed;
864 }
865
866 /* Main entry point for the forward copy propagation optimization.  */
867
868 static unsigned int
869 copyprop_hardreg_forward (void)
870 {
871   struct value_data *all_vd;
872   basic_block bb;
873   sbitmap visited;
874
875   all_vd = XNEWVEC (struct value_data, last_basic_block);
876
877   visited = sbitmap_alloc (last_basic_block);
878   sbitmap_zero (visited);
879
880   FOR_EACH_BB (bb)
881     {
882       SET_BIT (visited, bb->index);
883
884       /* If a block has a single predecessor, that we've already
885          processed, begin with the value data that was live at
886          the end of the predecessor block.  */
887       /* ??? Ought to use more intelligent queuing of blocks.  */
888       if (single_pred_p (bb)
889           && TEST_BIT (visited, single_pred (bb)->index)
890           && ! (single_pred_edge (bb)->flags & (EDGE_ABNORMAL_CALL | EDGE_EH)))
891         all_vd[bb->index] = all_vd[single_pred (bb)->index];
892       else
893         init_value_data (all_vd + bb->index);
894
895       copyprop_hardreg_forward_1 (bb, all_vd + bb->index);
896     }
897
898   sbitmap_free (visited);
899   free (all_vd);
900   return 0;
901 }
902
903 /* Dump the value chain data to stderr.  */
904
905 void
906 debug_value_data (struct value_data *vd)
907 {
908   HARD_REG_SET set;
909   unsigned int i, j;
910
911   CLEAR_HARD_REG_SET (set);
912
913   for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
914     if (vd->e[i].oldest_regno == i)
915       {
916         if (vd->e[i].mode == VOIDmode)
917           {
918             if (vd->e[i].next_regno != INVALID_REGNUM)
919               fprintf (stderr, "[%u] Bad next_regno for empty chain (%u)\n",
920                        i, vd->e[i].next_regno);
921             continue;
922           }
923
924         SET_HARD_REG_BIT (set, i);
925         fprintf (stderr, "[%u %s] ", i, GET_MODE_NAME (vd->e[i].mode));
926
927         for (j = vd->e[i].next_regno;
928              j != INVALID_REGNUM;
929              j = vd->e[j].next_regno)
930           {
931             if (TEST_HARD_REG_BIT (set, j))
932               {
933                 fprintf (stderr, "[%u] Loop in regno chain\n", j);
934                 return;
935               }
936
937             if (vd->e[j].oldest_regno != i)
938               {
939                 fprintf (stderr, "[%u] Bad oldest_regno (%u)\n",
940                          j, vd->e[j].oldest_regno);
941                 return;
942               }
943             SET_HARD_REG_BIT (set, j);
944             fprintf (stderr, "[%u %s] ", j, GET_MODE_NAME (vd->e[j].mode));
945           }
946         fputc ('\n', stderr);
947       }
948
949   for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
950     if (! TEST_HARD_REG_BIT (set, i)
951         && (vd->e[i].mode != VOIDmode
952             || vd->e[i].oldest_regno != i
953             || vd->e[i].next_regno != INVALID_REGNUM))
954       fprintf (stderr, "[%u] Non-empty reg in chain (%s %u %i)\n",
955                i, GET_MODE_NAME (vd->e[i].mode), vd->e[i].oldest_regno,
956                vd->e[i].next_regno);
957 }
958
959 #ifdef ENABLE_CHECKING
960 static void
961 validate_value_data (struct value_data *vd)
962 {
963   HARD_REG_SET set;
964   unsigned int i, j;
965
966   CLEAR_HARD_REG_SET (set);
967
968   for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
969     if (vd->e[i].oldest_regno == i)
970       {
971         if (vd->e[i].mode == VOIDmode)
972           {
973             if (vd->e[i].next_regno != INVALID_REGNUM)
974               internal_error ("validate_value_data: [%u] Bad next_regno for empty chain (%u)",
975                               i, vd->e[i].next_regno);
976             continue;
977           }
978
979         SET_HARD_REG_BIT (set, i);
980
981         for (j = vd->e[i].next_regno;
982              j != INVALID_REGNUM;
983              j = vd->e[j].next_regno)
984           {
985             if (TEST_HARD_REG_BIT (set, j))
986               internal_error ("validate_value_data: Loop in regno chain (%u)",
987                               j);
988             if (vd->e[j].oldest_regno != i)
989               internal_error ("validate_value_data: [%u] Bad oldest_regno (%u)",
990                               j, vd->e[j].oldest_regno);
991
992             SET_HARD_REG_BIT (set, j);
993           }
994       }
995
996   for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
997     if (! TEST_HARD_REG_BIT (set, i)
998         && (vd->e[i].mode != VOIDmode
999             || vd->e[i].oldest_regno != i
1000             || vd->e[i].next_regno != INVALID_REGNUM))
1001       internal_error ("validate_value_data: [%u] Non-empty reg in chain (%s %u %i)",
1002                       i, GET_MODE_NAME (vd->e[i].mode), vd->e[i].oldest_regno,
1003                       vd->e[i].next_regno);
1004 }
1005 #endif
1006 \f
1007 static bool
1008 gate_handle_cprop (void)
1009 {
1010   return (optimize > 0 && (flag_cprop_registers));
1011 }
1012
1013
1014 struct rtl_opt_pass pass_cprop_hardreg =
1015 {
1016  {
1017   RTL_PASS,
1018   "cprop_hardreg",                      /* name */
1019   gate_handle_cprop,                    /* gate */
1020   copyprop_hardreg_forward,             /* execute */
1021   NULL,                                 /* sub */
1022   NULL,                                 /* next */
1023   0,                                    /* static_pass_number */
1024   TV_CPROP_REGISTERS,                   /* tv_id */
1025   0,                                    /* properties_required */
1026   0,                                    /* properties_provided */
1027   0,                                    /* properties_destroyed */
1028   0,                                    /* todo_flags_start */
1029   TODO_dump_func | TODO_verify_rtl_sharing /* todo_flags_finish */
1030  }
1031 };