OSDN Git Service

385b8fb4725703641e5fe3012b6e2d7084c0ecec
[pf3gnuchains/gcc-fork.git] / gcc / df-problems.c
1 /* Standard problems for dataflow support routines.
2    Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
3    Free Software Foundation, Inc.
4    Originally contributed by Michael P. Hayes 
5              (m.hayes@elec.canterbury.ac.nz, mhayes@redhat.com)
6    Major rewrite contributed by Danny Berlin (dberlin@dberlin.org)
7              and Kenneth Zadeck (zadeck@naturalbridge.com).
8
9 This file is part of GCC.
10
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 2, or (at your option) any later
14 version.
15
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19 for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING.  If not, write to the Free
23 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
24 02110-1301, USA.  */
25
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "rtl.h"
31 #include "tm_p.h"
32 #include "insn-config.h"
33 #include "recog.h"
34 #include "function.h"
35 #include "regs.h"
36 #include "output.h"
37 #include "alloc-pool.h"
38 #include "flags.h"
39 #include "hard-reg-set.h"
40 #include "basic-block.h"
41 #include "sbitmap.h"
42 #include "bitmap.h"
43 #include "timevar.h"
44 #include "df.h"
45 #include "vecprim.h"
46 #include "except.h"
47
48 #if 0
49 #define REG_DEAD_DEBUGGING
50 #endif
51
52 #define DF_SPARSE_THRESHOLD 32
53
54 static bitmap seen_in_block = NULL;
55 static bitmap seen_in_insn = NULL;
56 static void df_ri_dump (struct dataflow *, FILE *);
57
58 \f
59 /*----------------------------------------------------------------------------
60    Public functions access functions for the dataflow problems.
61 ----------------------------------------------------------------------------*/
62
63 /* Create a du or ud chain from SRC to DST and link it into SRC.   */
64
65 struct df_link *
66 df_chain_create (struct dataflow *dflow, struct df_ref *src, struct df_ref *dst)
67 {
68   struct df_link *head = DF_REF_CHAIN (src);
69   struct df_link *link = pool_alloc (dflow->block_pool);;
70   
71   DF_REF_CHAIN (src) = link;
72   link->next = head;
73   link->ref = dst;
74   return link;
75 }
76
77
78 /* Delete a du or ud chain for REF.  If LINK is NULL, delete all
79    chains for ref and check to see if the reverse chains can also be
80    deleted.  If LINK is not NULL it must be a link off of ref.  In
81    this case, the other end is not deleted.  */
82
83 void
84 df_chain_unlink (struct dataflow *dflow, struct df_ref *ref, struct df_link *link)
85 {
86   struct df_link *chain = DF_REF_CHAIN (ref);
87   if (link)
88     {
89       /* Link was the first element in the chain.  */
90       if (chain == link)
91         DF_REF_CHAIN (ref) = link->next;
92       else
93         {
94           /* Link is an internal element in the chain.  */
95           struct df_link *prev = chain;
96           while (chain)
97             {
98               if (chain == link)
99                 {
100                   prev->next = chain->next;
101                   break;
102                 }
103               prev = chain;
104               chain = chain->next;
105             }
106         }
107       pool_free (dflow->block_pool, link);
108     }
109   else
110     {
111       /* If chain is NULL here, it was because of a recursive call
112          when the other flavor of chains was not built.  Just run thru
113          the entire chain calling the other side and then deleting the
114          link.  */
115       while (chain)
116         {
117           struct df_link *next = chain->next;
118           /* Delete the other side if it exists.  */
119           df_chain_unlink (dflow, chain->ref, chain);
120           chain = next;
121         }
122     }
123 }
124
125
126 /* Copy the du or ud chain starting at FROM_REF and attach it to
127    TO_REF.  */ 
128
129 void 
130 df_chain_copy (struct dataflow *dflow, 
131                struct df_ref *to_ref, 
132                struct df_link *from_ref)
133 {
134   while (from_ref)
135     {
136       df_chain_create (dflow, to_ref, from_ref->ref);
137       from_ref = from_ref->next;
138     }
139 }
140
141
142 /* Get the live in set for BB no matter what problem happens to be
143    defined.  */
144
145 bitmap
146 df_get_live_in (struct df *df, basic_block bb)
147 {
148   gcc_assert (df->problems_by_index[DF_LR]);
149
150   if (df->problems_by_index[DF_UREC])
151     return DF_RA_LIVE_IN (df, bb);
152   else if (df->problems_by_index[DF_UR])
153     return DF_LIVE_IN (df, bb);
154   else 
155     return DF_UPWARD_LIVE_IN (df, bb);
156 }
157
158
159 /* Get the live out set for BB no matter what problem happens to be
160    defined.  */
161
162 bitmap
163 df_get_live_out (struct df *df, basic_block bb)
164 {
165   gcc_assert (df->problems_by_index[DF_LR]);
166
167   if (df->problems_by_index[DF_UREC])
168     return DF_RA_LIVE_OUT (df, bb);
169   else if (df->problems_by_index[DF_UR])
170     return DF_LIVE_OUT (df, bb);
171   else 
172     return DF_UPWARD_LIVE_OUT (df, bb);
173 }
174
175
176 /*----------------------------------------------------------------------------
177    Utility functions.
178 ----------------------------------------------------------------------------*/
179
180 /* Generic versions to get the void* version of the block info.  Only
181    used inside the problem instance vectors.  */
182
183 /* Grow the bb_info array.  */
184
185 void
186 df_grow_bb_info (struct dataflow *dflow)
187 {
188   unsigned int new_size = last_basic_block + 1;
189   if (dflow->block_info_size < new_size)
190     {
191       new_size += new_size / 4;
192       dflow->block_info = xrealloc (dflow->block_info, 
193                                     new_size *sizeof (void*));
194       memset (dflow->block_info + dflow->block_info_size, 0,
195               (new_size - dflow->block_info_size) *sizeof (void *));
196       dflow->block_info_size = new_size;
197     }
198 }
199
200 /* Dump a def-use or use-def chain for REF to FILE.  */
201
202 void
203 df_chain_dump (struct df_link *link, FILE *file)
204 {
205   fprintf (file, "{ ");
206   for (; link; link = link->next)
207     {
208       fprintf (file, "%c%d(bb %d insn %d) ",
209                DF_REF_REG_DEF_P (link->ref) ? 'd' : 'u',
210                DF_REF_ID (link->ref),
211                DF_REF_BBNO (link->ref),
212                DF_REF_INSN (link->ref) ? DF_REF_INSN_UID (link->ref) : -1);
213     }
214   fprintf (file, "}");
215 }
216
217
218 /* Print some basic block info as part of df_dump.  */
219
220 void 
221 df_print_bb_index (basic_block bb, FILE *file)
222 {
223   edge e;
224   edge_iterator ei;
225
226   fprintf (file, "( ");
227     FOR_EACH_EDGE (e, ei, bb->preds)
228     {
229       basic_block pred = e->src;
230       fprintf (file, "%d ", pred->index);
231     } 
232   fprintf (file, ")->[%d]->( ", bb->index);
233   FOR_EACH_EDGE (e, ei, bb->succs)
234     {
235       basic_block succ = e->dest;
236       fprintf (file, "%d ", succ->index);
237     } 
238   fprintf (file, ")\n");
239 }
240
241
242 /* Return a bitmap for REGNO from the cache MAPS.  The bitmap is to
243    contain COUNT bits starting at START.  These bitmaps are not to be
244    changed since there is a cache of them.  */
245
246 static inline bitmap
247 df_ref_bitmap (bitmap *maps, unsigned int regno, int start, int count)
248 {
249   bitmap ids = maps[regno];
250   if (!ids)
251     {
252       unsigned int i;
253       unsigned int end = start + count;;
254       ids = BITMAP_ALLOC (NULL);
255       maps[regno] = ids;
256       for (i = start; i < end; i++)
257         bitmap_set_bit (ids, i);
258     }
259   return ids;
260 }
261
262
263 /* Make sure that the seen_in_insn and seen_in_block sbitmaps are set
264    up correctly. */
265
266 static void
267 df_set_seen (void)
268 {
269   seen_in_block = BITMAP_ALLOC (NULL);
270   seen_in_insn = BITMAP_ALLOC (NULL);
271 }
272
273
274 static void
275 df_unset_seen (void)
276 {
277   BITMAP_FREE (seen_in_block);
278   BITMAP_FREE (seen_in_insn);
279 }
280
281
282 \f
283 /*----------------------------------------------------------------------------
284    REACHING USES
285
286    Find the locations in the function where each use site for a pseudo
287    can reach backwards.  In and out bitvectors are built for each basic
288    block.  The id field in the ref is used to index into these sets.
289    See df.h for details.
290
291 ----------------------------------------------------------------------------*/
292
293 /* This problem plays a large number of games for the sake of
294    efficiency.  
295    
296    1) The order of the bits in the bitvectors.  After the scanning
297    phase, all of the uses are sorted.  All of the uses for the reg 0
298    are first, followed by all uses for reg 1 and so on.
299    
300    2) There are two kill sets, one if the number of uses is less or
301    equal to DF_SPARSE_THRESHOLD and another if it is greater.
302
303    <= : There is a bitmap for each register, uses_sites[N], that is
304    built on demand.  This bitvector contains a 1 for each use or reg
305    N.
306
307    > : One level of indirection is used to keep from generating long
308    strings of 1 bits in the kill sets.  Bitvectors that are indexed
309    by the regnum are used to represent that there is a killing def
310    for the register.  The confluence and transfer functions use
311    these along with the bitmap_clear_range call to remove ranges of
312    bits without actually generating a knockout vector.
313
314    The kill and sparse_kill and the dense_invalidated_by_call and
315    sparse_invalidated_by call both play this game.  */
316
317 /* Private data used to compute the solution for this problem.  These
318    data structures are not accessible outside of this module.  */
319 struct df_ru_problem_data
320 {
321
322   bitmap *use_sites;            /* Bitmap of uses for each pseudo.  */
323   unsigned int use_sites_size;  /* Size of use_sites.  */
324   /* The set of defs to regs invalidated by call.  */
325   bitmap sparse_invalidated_by_call;  
326   /* The set of defs to regs invalidated by call for ru.  */  
327   bitmap dense_invalidated_by_call;   
328 };
329
330 /* Get basic block info.  */
331
332 struct df_ru_bb_info *
333 df_ru_get_bb_info (struct dataflow *dflow, unsigned int index)
334 {
335   return (struct df_ru_bb_info *) dflow->block_info[index];
336 }
337
338
339 /* Set basic block info.  */
340
341 static void
342 df_ru_set_bb_info (struct dataflow *dflow, unsigned int index, 
343                    struct df_ru_bb_info *bb_info)
344 {
345   dflow->block_info[index] = bb_info;
346 }
347
348
349 /* Free basic block info.  */
350
351 static void
352 df_ru_free_bb_info (struct dataflow *dflow, 
353                     basic_block bb ATTRIBUTE_UNUSED, 
354                     void *vbb_info)
355 {
356   struct df_ru_bb_info *bb_info = (struct df_ru_bb_info *) vbb_info;
357   if (bb_info)
358     {
359       BITMAP_FREE (bb_info->kill);
360       BITMAP_FREE (bb_info->sparse_kill);
361       BITMAP_FREE (bb_info->gen);
362       BITMAP_FREE (bb_info->in);
363       BITMAP_FREE (bb_info->out);
364       pool_free (dflow->block_pool, bb_info);
365     }
366 }
367
368
369 /* Allocate or reset bitmaps for DFLOW blocks. The solution bits are
370    not touched unless the block is new.  */
371
372 static void 
373 df_ru_alloc (struct dataflow *dflow, 
374              bitmap blocks_to_rescan ATTRIBUTE_UNUSED,
375              bitmap all_blocks)
376 {
377   unsigned int bb_index;
378   bitmap_iterator bi;
379   unsigned int reg_size = max_reg_num ();
380
381   if (!dflow->block_pool)
382     dflow->block_pool = create_alloc_pool ("df_ru_block pool", 
383                                            sizeof (struct df_ru_bb_info), 50);
384
385   if (dflow->problem_data)
386     {
387       unsigned int i;
388       struct df_ru_problem_data *problem_data
389         = (struct df_ru_problem_data *) dflow->problem_data;
390
391       for (i = 0; i < problem_data->use_sites_size; i++)
392         {
393           bitmap bm = problem_data->use_sites[i];
394           if (bm)
395             {
396               BITMAP_FREE (bm);
397               problem_data->use_sites[i] = NULL;
398             }
399         }
400       
401       if (problem_data->use_sites_size < reg_size)
402         {
403           problem_data->use_sites 
404             = xrealloc (problem_data->use_sites, reg_size * sizeof (bitmap));
405           memset (problem_data->use_sites + problem_data->use_sites_size, 0,
406                   (reg_size - problem_data->use_sites_size) * sizeof (bitmap));
407           problem_data->use_sites_size = reg_size;
408         }
409
410       bitmap_clear (problem_data->sparse_invalidated_by_call);
411       bitmap_clear (problem_data->dense_invalidated_by_call);
412     }
413   else 
414     {
415       struct df_ru_problem_data *problem_data = XNEW (struct df_ru_problem_data);
416       dflow->problem_data = problem_data;
417
418       problem_data->use_sites = XCNEWVEC (bitmap, reg_size);
419       problem_data->use_sites_size = reg_size;
420       problem_data->sparse_invalidated_by_call = BITMAP_ALLOC (NULL);
421       problem_data->dense_invalidated_by_call = BITMAP_ALLOC (NULL);
422     }
423
424   df_grow_bb_info (dflow);
425
426   /* Because of the clustering of all def sites for the same pseudo,
427      we have to process all of the blocks before doing the
428      analysis.  */
429
430   EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
431     {
432       struct df_ru_bb_info *bb_info = df_ru_get_bb_info (dflow, bb_index);
433       if (bb_info)
434         { 
435           bitmap_clear (bb_info->kill);
436           bitmap_clear (bb_info->sparse_kill);
437           bitmap_clear (bb_info->gen);
438         }
439       else
440         { 
441           bb_info = (struct df_ru_bb_info *) pool_alloc (dflow->block_pool);
442           df_ru_set_bb_info (dflow, bb_index, bb_info);
443           bb_info->kill = BITMAP_ALLOC (NULL);
444           bb_info->sparse_kill = BITMAP_ALLOC (NULL);
445           bb_info->gen = BITMAP_ALLOC (NULL);
446           bb_info->in = BITMAP_ALLOC (NULL);
447           bb_info->out = BITMAP_ALLOC (NULL);
448         }
449     }
450 }
451
452
453 /* Process a list of DEFs for df_ru_bb_local_compute.  */
454
455 static void
456 df_ru_bb_local_compute_process_def (struct dataflow *dflow,
457                                     struct df_ru_bb_info *bb_info, 
458                                     struct df_ref *def,
459                                     enum df_ref_flags top_flag)
460 {
461   struct df *df = dflow->df;
462   while (def)
463     {
464       if ((top_flag == (DF_REF_FLAGS (def) & DF_REF_AT_TOP))
465           /* If the def is to only part of the reg, it is as if it did
466              not happen, since some of the bits may get thru.  */
467           && (!(DF_REF_FLAGS (def) & DF_REF_PARTIAL)))
468         {
469           unsigned int regno = DF_REF_REGNO (def);
470           unsigned int begin = DF_REG_USE_GET (df, regno)->begin;
471           unsigned int n_uses = DF_REG_USE_GET (df, regno)->n_refs;
472           if (!bitmap_bit_p (seen_in_block, regno))
473             {
474               /* The first def for regno in the insn, causes the kill
475                  info to be generated.  Do not modify the gen set
476                  because the only values in it are the uses from here
477                  to the top of the block and this def does not effect
478                  them.  */
479               if (!bitmap_bit_p (seen_in_insn, regno))
480                 {
481                   if (n_uses > DF_SPARSE_THRESHOLD)
482                     bitmap_set_bit (bb_info->sparse_kill, regno);
483                   else
484                     {
485                       struct df_ru_problem_data * problem_data
486                         = (struct df_ru_problem_data *)dflow->problem_data;
487                       bitmap uses 
488                         = df_ref_bitmap (problem_data->use_sites, regno, 
489                                        begin, n_uses);
490                       bitmap_ior_into (bb_info->kill, uses);
491                     }
492                 }
493               bitmap_set_bit (seen_in_insn, regno);
494             }
495         }
496       def = def->next_ref;
497     }
498 }
499
500
501 /* Process a list of USEs for df_ru_bb_local_compute.  */
502
503 static void
504 df_ru_bb_local_compute_process_use (struct df_ru_bb_info *bb_info, 
505                                     struct df_ref *use,
506                                     enum df_ref_flags top_flag)
507 {
508   while (use)
509     {
510       if (top_flag == (DF_REF_FLAGS (use) & DF_REF_AT_TOP))
511         {
512           /* Add use to set of gens in this BB unless we have seen a
513              def in a previous instruction.  */
514           unsigned int regno = DF_REF_REGNO (use);
515           if (!bitmap_bit_p (seen_in_block, regno))
516             bitmap_set_bit (bb_info->gen, DF_REF_ID (use));
517         }
518       use = use->next_ref;
519     }
520 }
521
522 /* Compute local reaching use (upward exposed use) info for basic
523    block BB.  USE_INFO->REGS[R] caches the set of uses for register R.  */
524 static void
525 df_ru_bb_local_compute (struct dataflow *dflow, unsigned int bb_index)
526 {
527   struct df *df = dflow->df;
528   basic_block bb = BASIC_BLOCK (bb_index);
529   struct df_ru_bb_info *bb_info = df_ru_get_bb_info (dflow, bb_index);
530   rtx insn;
531
532   /* Set when a def for regno is seen.  */
533   bitmap_clear (seen_in_block);
534   bitmap_clear (seen_in_insn);
535
536 #ifdef EH_USES
537   /* Variables defined in the prolog that are used by the exception
538      handler.  */
539   df_ru_bb_local_compute_process_use (bb_info, 
540                                       df_get_artificial_uses (df, bb_index),
541                                       DF_REF_AT_TOP);
542 #endif
543   df_ru_bb_local_compute_process_def (dflow, bb_info, 
544                                       df_get_artificial_defs (df, bb_index),
545                                       DF_REF_AT_TOP);
546
547   FOR_BB_INSNS (bb, insn)
548     {
549       unsigned int uid = INSN_UID (insn);
550       if (!INSN_P (insn))
551         continue;
552
553       df_ru_bb_local_compute_process_use (bb_info, 
554                                           DF_INSN_UID_USES (df, uid), 0);
555
556       df_ru_bb_local_compute_process_def (dflow, bb_info, 
557                                           DF_INSN_UID_DEFS (df, uid), 0);
558
559       bitmap_ior_into (seen_in_block, seen_in_insn);
560       bitmap_clear (seen_in_insn);
561     }
562
563   /* Process the hardware registers that are always live.  */
564   df_ru_bb_local_compute_process_use (bb_info, 
565                                       df_get_artificial_uses (df, bb_index), 0);
566
567   df_ru_bb_local_compute_process_def (dflow, bb_info, 
568                                       df_get_artificial_defs (df, bb_index), 0);
569 }
570
571
572 /* Compute local reaching use (upward exposed use) info for each basic
573    block within BLOCKS.  */
574 static void
575 df_ru_local_compute (struct dataflow *dflow, 
576                      bitmap all_blocks,
577                      bitmap rescan_blocks  ATTRIBUTE_UNUSED)
578 {
579   struct df *df = dflow->df;
580   unsigned int bb_index;
581   bitmap_iterator bi;
582   unsigned int regno;
583   struct df_ru_problem_data *problem_data
584     = (struct df_ru_problem_data *) dflow->problem_data;
585   bitmap sparse_invalidated = problem_data->sparse_invalidated_by_call;
586   bitmap dense_invalidated = problem_data->dense_invalidated_by_call;
587
588   df_set_seen ();
589   df_reorganize_refs (&df->use_info);
590
591   EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
592     {
593       df_ru_bb_local_compute (dflow, bb_index);
594     }
595   
596   /* Set up the knockout bit vectors to be applied across EH_EDGES.  */
597   EXECUTE_IF_SET_IN_BITMAP (df_invalidated_by_call, 0, regno, bi)
598     {
599       struct df_reg_info *reg_info = DF_REG_USE_GET (df, regno);
600       if (reg_info->n_refs > DF_SPARSE_THRESHOLD)
601         bitmap_set_bit (sparse_invalidated, regno);
602       else
603         {
604           bitmap defs = df_ref_bitmap (problem_data->use_sites, regno, 
605                                        reg_info->begin, reg_info->n_refs);
606           bitmap_ior_into (dense_invalidated, defs);
607         }
608     }
609
610   df_unset_seen ();
611 }
612
613
614 /* Initialize the solution bit vectors for problem.  */
615
616 static void 
617 df_ru_init_solution (struct dataflow *dflow, bitmap all_blocks)
618 {
619   unsigned int bb_index;
620   bitmap_iterator bi;
621
622   EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
623     {
624       struct df_ru_bb_info *bb_info = df_ru_get_bb_info (dflow, bb_index);
625       bitmap_copy (bb_info->in, bb_info->gen);
626       bitmap_clear (bb_info->out);
627     }
628 }
629
630
631 /* Out of target gets or of in of source.  */
632
633 static void
634 df_ru_confluence_n (struct dataflow *dflow, edge e)
635 {
636   bitmap op1 = df_ru_get_bb_info (dflow, e->src->index)->out;
637   bitmap op2 = df_ru_get_bb_info (dflow, e->dest->index)->in;
638
639   if (e->flags & EDGE_EH)
640     {
641       struct df_ru_problem_data *problem_data
642         = (struct df_ru_problem_data *) dflow->problem_data;
643       bitmap sparse_invalidated = problem_data->sparse_invalidated_by_call;
644       bitmap dense_invalidated = problem_data->dense_invalidated_by_call;
645       struct df *df = dflow->df;
646       bitmap_iterator bi;
647       unsigned int regno;
648       bitmap tmp = BITMAP_ALLOC (NULL);
649
650       bitmap_copy (tmp, op2);
651       bitmap_and_compl_into (tmp, dense_invalidated);
652
653       EXECUTE_IF_SET_IN_BITMAP (sparse_invalidated, 0, regno, bi)
654         {
655           bitmap_clear_range (tmp, 
656                               DF_REG_USE_GET (df, regno)->begin, 
657                               DF_REG_USE_GET (df, regno)->n_refs);
658         }
659       bitmap_ior_into (op1, tmp);
660       BITMAP_FREE (tmp);
661     }
662   else
663     bitmap_ior_into (op1, op2);
664 }
665
666
667 /* Transfer function.  */
668
669 static bool
670 df_ru_transfer_function (struct dataflow *dflow, int bb_index)
671 {
672   struct df_ru_bb_info *bb_info = df_ru_get_bb_info (dflow, bb_index);
673   unsigned int regno;
674   bitmap_iterator bi;
675   bitmap in = bb_info->in;
676   bitmap out = bb_info->out;
677   bitmap gen = bb_info->gen;
678   bitmap kill = bb_info->kill;
679   bitmap sparse_kill = bb_info->sparse_kill;
680
681   if (bitmap_empty_p (sparse_kill))
682     return  bitmap_ior_and_compl (in, gen, out, kill);
683   else 
684     {
685       struct df *df = dflow->df;
686       bool changed = false;
687       bitmap tmp = BITMAP_ALLOC (NULL);
688       bitmap_copy (tmp, out);
689       EXECUTE_IF_SET_IN_BITMAP (sparse_kill, 0, regno, bi)
690         {
691           bitmap_clear_range (tmp, 
692                               DF_REG_USE_GET (df, regno)->begin, 
693                               DF_REG_USE_GET (df, regno)->n_refs);
694         }
695       bitmap_and_compl_into (tmp, kill);
696       bitmap_ior_into (tmp, gen);
697       changed = !bitmap_equal_p (tmp, in);
698       if (changed)
699         {
700           BITMAP_FREE (in);
701           bb_info->in = tmp;
702         }
703       else 
704         BITMAP_FREE (tmp);
705       return changed;
706     }
707 }
708
709
710 /* Free all storage associated with the problem.  */
711
712 static void
713 df_ru_free (struct dataflow *dflow)
714 {
715   unsigned int i;
716   struct df_ru_problem_data *problem_data
717     = (struct df_ru_problem_data *) dflow->problem_data;
718
719   if (problem_data)
720     {
721       for (i = 0; i < dflow->block_info_size; i++)
722         {
723           struct df_ru_bb_info *bb_info = df_ru_get_bb_info (dflow, i);
724           if (bb_info)
725             {
726               BITMAP_FREE (bb_info->kill);
727               BITMAP_FREE (bb_info->sparse_kill);
728               BITMAP_FREE (bb_info->gen);
729               BITMAP_FREE (bb_info->in);
730               BITMAP_FREE (bb_info->out);
731             }
732         }
733       
734       free_alloc_pool (dflow->block_pool);
735       
736       for (i = 0; i < problem_data->use_sites_size; i++)
737         {
738           bitmap bm = problem_data->use_sites[i];
739           if (bm)
740             BITMAP_FREE (bm);
741         }
742       
743       free (problem_data->use_sites);
744       BITMAP_FREE (problem_data->sparse_invalidated_by_call);
745       BITMAP_FREE (problem_data->dense_invalidated_by_call);
746       
747       dflow->block_info_size = 0;
748       free (dflow->block_info);
749       free (dflow->problem_data);
750     }
751   free (dflow);
752 }
753
754
755 /* Debugging info.  */
756
757 static void
758 df_ru_dump (struct dataflow *dflow, FILE *file)
759 {
760   basic_block bb;
761   struct df *df = dflow->df;
762   struct df_ru_problem_data *problem_data
763     = (struct df_ru_problem_data *) dflow->problem_data;
764   unsigned int m = max_reg_num ();
765   unsigned int regno;
766   
767   if (!dflow->block_info) 
768     return;
769
770   fprintf (file, "Reaching uses:\n");
771
772   fprintf (file, "  sparse invalidated \t");
773   dump_bitmap (file, problem_data->sparse_invalidated_by_call);
774   fprintf (file, "  dense invalidated \t");
775   dump_bitmap (file, problem_data->dense_invalidated_by_call);
776   
777   for (regno = 0; regno < m; regno++)
778     if (DF_REG_USE_GET (df, regno)->n_refs)
779       fprintf (file, "%d[%d,%d] ", regno, 
780                DF_REG_USE_GET (df, regno)->begin, 
781                DF_REG_USE_GET (df, regno)->n_refs);
782   fprintf (file, "\n");
783
784   FOR_ALL_BB (bb)
785     {
786       struct df_ru_bb_info *bb_info = df_ru_get_bb_info (dflow, bb->index);
787       df_print_bb_index (bb, file);
788       
789       if (!bb_info->in)
790         continue;
791       
792       fprintf (file, "  in  \t(%d)\n", (int) bitmap_count_bits (bb_info->in));
793       dump_bitmap (file, bb_info->in);
794       fprintf (file, "  gen \t(%d)\n", (int) bitmap_count_bits (bb_info->gen));
795       dump_bitmap (file, bb_info->gen);
796       fprintf (file, "  kill\t(%d)\n", (int) bitmap_count_bits (bb_info->kill));
797       dump_bitmap (file, bb_info->kill);
798       fprintf (file, "  out \t(%d)\n", (int) bitmap_count_bits (bb_info->out));
799       dump_bitmap (file, bb_info->out);
800     }
801 }
802
803 /* All of the information associated with every instance of the problem.  */
804
805 static struct df_problem problem_RU =
806 {
807   DF_RU,                      /* Problem id.  */
808   DF_BACKWARD,                /* Direction.  */
809   df_ru_alloc,                /* Allocate the problem specific data.  */
810   NULL,                       /* Reset global information.  */
811   df_ru_free_bb_info,         /* Free basic block info.  */
812   df_ru_local_compute,        /* Local compute function.  */
813   df_ru_init_solution,        /* Init the solution specific data.  */
814   df_iterative_dataflow,      /* Iterative solver.  */
815   NULL,                       /* Confluence operator 0.  */ 
816   df_ru_confluence_n,         /* Confluence operator n.  */ 
817   df_ru_transfer_function,    /* Transfer function.  */
818   NULL,                       /* Finalize function.  */
819   df_ru_free,                 /* Free all of the problem information.  */
820   df_ru_dump,                 /* Debugging.  */
821   NULL,                       /* Dependent problem.  */
822   0                           /* Changeable flags.  */
823 };
824
825
826
827 /* Create a new DATAFLOW instance and add it to an existing instance
828    of DF.  The returned structure is what is used to get at the
829    solution.  */
830
831 struct dataflow *
832 df_ru_add_problem (struct df *df, int flags)
833 {
834   return df_add_problem (df, &problem_RU, flags);
835 }
836
837 \f
838 /*----------------------------------------------------------------------------
839    REACHING DEFINITIONS
840
841    Find the locations in the function where each definition site for a
842    pseudo reaches.  In and out bitvectors are built for each basic
843    block.  The id field in the ref is used to index into these sets.
844    See df.h for details.
845    ----------------------------------------------------------------------------*/
846
847 /* See the comment at the top of the Reaching Uses problem for how the
848    uses are represented in the kill sets. The same games are played
849    here for the defs.  */
850
851 /* Private data used to compute the solution for this problem.  These
852    data structures are not accessible outside of this module.  */
853 struct df_rd_problem_data
854 {
855   /* If the number of defs for regnum N is less than
856      DF_SPARSE_THRESHOLD, uses_sites[N] contains a mask of the all of
857      the defs of reg N indexed by the id in the ref structure.  If
858      there are more than DF_SPARSE_THRESHOLD defs for regnum N a
859      different mechanism is used to mask the def.  */
860   bitmap *def_sites;            /* Bitmap of defs for each pseudo.  */
861   unsigned int def_sites_size;  /* Size of def_sites.  */
862   /* The set of defs to regs invalidated by call.  */
863   bitmap sparse_invalidated_by_call;  
864   /* The set of defs to regs invalidate by call for rd.  */  
865   bitmap dense_invalidated_by_call;   
866 };
867
868 /* Get basic block info.  */
869
870 struct df_rd_bb_info *
871 df_rd_get_bb_info (struct dataflow *dflow, unsigned int index)
872 {
873   return (struct df_rd_bb_info *) dflow->block_info[index];
874 }
875
876
877 /* Set basic block info.  */
878
879 static void
880 df_rd_set_bb_info (struct dataflow *dflow, unsigned int index, 
881                    struct df_rd_bb_info *bb_info)
882 {
883   dflow->block_info[index] = bb_info;
884 }
885
886
887 /* Free basic block info.  */
888
889 static void
890 df_rd_free_bb_info (struct dataflow *dflow, 
891                     basic_block bb ATTRIBUTE_UNUSED, 
892                     void *vbb_info)
893 {
894   struct df_rd_bb_info *bb_info = (struct df_rd_bb_info *) vbb_info;
895   if (bb_info)
896     {
897       BITMAP_FREE (bb_info->kill);
898       BITMAP_FREE (bb_info->sparse_kill);
899       BITMAP_FREE (bb_info->gen);
900       BITMAP_FREE (bb_info->in);
901       BITMAP_FREE (bb_info->out);
902       pool_free (dflow->block_pool, bb_info);
903     }
904 }
905
906
907 /* Allocate or reset bitmaps for DFLOW blocks. The solution bits are
908    not touched unless the block is new.  */
909
910 static void 
911 df_rd_alloc (struct dataflow *dflow, 
912              bitmap blocks_to_rescan ATTRIBUTE_UNUSED,
913              bitmap all_blocks)
914 {
915   unsigned int bb_index;
916   bitmap_iterator bi;
917   unsigned int reg_size = max_reg_num ();
918
919   if (!dflow->block_pool)
920     dflow->block_pool = create_alloc_pool ("df_rd_block pool", 
921                                            sizeof (struct df_rd_bb_info), 50);
922
923   if (dflow->problem_data)
924     {
925       unsigned int i;
926       struct df_rd_problem_data *problem_data
927         = (struct df_rd_problem_data *) dflow->problem_data;
928
929       for (i = 0; i < problem_data->def_sites_size; i++)
930         {
931           bitmap bm = problem_data->def_sites[i];
932           if (bm)
933             {
934               BITMAP_FREE (bm);
935               problem_data->def_sites[i] = NULL;
936             }
937         }
938       
939       if (problem_data->def_sites_size < reg_size)
940         {
941           problem_data->def_sites 
942             = xrealloc (problem_data->def_sites, reg_size *sizeof (bitmap));
943           memset (problem_data->def_sites + problem_data->def_sites_size, 0,
944                   (reg_size - problem_data->def_sites_size) *sizeof (bitmap));
945           problem_data->def_sites_size = reg_size;
946         }
947
948       bitmap_clear (problem_data->sparse_invalidated_by_call);
949       bitmap_clear (problem_data->dense_invalidated_by_call);
950     }
951   else 
952     {
953       struct df_rd_problem_data *problem_data = XNEW (struct df_rd_problem_data);
954       dflow->problem_data = problem_data;
955
956       problem_data->def_sites = XCNEWVEC (bitmap, reg_size);
957       problem_data->def_sites_size = reg_size;
958       problem_data->sparse_invalidated_by_call = BITMAP_ALLOC (NULL);
959       problem_data->dense_invalidated_by_call = BITMAP_ALLOC (NULL);
960     }
961
962   df_grow_bb_info (dflow);
963
964   /* Because of the clustering of all use sites for the same pseudo,
965      we have to process all of the blocks before doing the
966      analysis.  */
967
968   EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
969     {
970       struct df_rd_bb_info *bb_info = df_rd_get_bb_info (dflow, bb_index);
971       if (bb_info)
972         { 
973           bitmap_clear (bb_info->kill);
974           bitmap_clear (bb_info->sparse_kill);
975           bitmap_clear (bb_info->gen);
976         }
977       else
978         { 
979           bb_info = (struct df_rd_bb_info *) pool_alloc (dflow->block_pool);
980           df_rd_set_bb_info (dflow, bb_index, bb_info);
981           bb_info->kill = BITMAP_ALLOC (NULL);
982           bb_info->sparse_kill = BITMAP_ALLOC (NULL);
983           bb_info->gen = BITMAP_ALLOC (NULL);
984           bb_info->in = BITMAP_ALLOC (NULL);
985           bb_info->out = BITMAP_ALLOC (NULL);
986         }
987     }
988 }
989
990
991 /* Process a list of DEFs for df_rd_bb_local_compute.  */
992
993 static void
994 df_rd_bb_local_compute_process_def (struct dataflow *dflow,
995                                     struct df_rd_bb_info *bb_info, 
996                                     struct df_ref *def,
997                                     enum df_ref_flags top_flag)
998 {
999   struct df *df = dflow->df;
1000   while (def)
1001     {
1002       if (top_flag == (DF_REF_FLAGS (def) & DF_REF_AT_TOP))
1003         {
1004           unsigned int regno = DF_REF_REGNO (def);
1005           unsigned int begin = DF_REG_DEF_GET (df, regno)->begin;
1006           unsigned int n_defs = DF_REG_DEF_GET (df, regno)->n_refs;
1007           
1008           /* Only the last def(s) for a regno in the block has any
1009              effect.  */ 
1010           if (!bitmap_bit_p (seen_in_block, regno))
1011             {
1012               /* The first def for regno in insn gets to knock out the
1013                  defs from other instructions.  */
1014               if ((!bitmap_bit_p (seen_in_insn, regno))
1015                   /* If the def is to only part of the reg, it does
1016                      not kill the other defs that reach here.  */
1017                   && (!((DF_REF_FLAGS (def) & DF_REF_PARTIAL)
1018                          || (DF_REF_FLAGS (def) & DF_REF_MAY_CLOBBER))))
1019                 {
1020                   if (n_defs > DF_SPARSE_THRESHOLD)
1021                     {
1022                       bitmap_set_bit (bb_info->sparse_kill, regno);
1023                       bitmap_clear_range(bb_info->gen, begin, n_defs);
1024                     }
1025                   else
1026                     {
1027                       struct df_rd_problem_data * problem_data
1028                         = (struct df_rd_problem_data *)dflow->problem_data;
1029                       bitmap defs = df_ref_bitmap (problem_data->def_sites, 
1030                                                    regno, begin, n_defs);
1031                       bitmap_ior_into (bb_info->kill, defs);
1032                       bitmap_and_compl_into (bb_info->gen, defs);
1033                     }
1034                 }
1035               
1036               bitmap_set_bit (seen_in_insn, regno);
1037               /* All defs for regno in the instruction may be put into
1038                  the gen set.  */
1039               if (!(DF_REF_FLAGS (def) 
1040                      & (DF_REF_MUST_CLOBBER | DF_REF_MAY_CLOBBER)))
1041                 bitmap_set_bit (bb_info->gen, DF_REF_ID (def));
1042             }
1043         }
1044       def = def->next_ref;
1045     }
1046 }
1047
1048 /* Compute local reaching def info for basic block BB.  */
1049
1050 static void
1051 df_rd_bb_local_compute (struct dataflow *dflow, unsigned int bb_index)
1052 {
1053   struct df *df = dflow->df;
1054   basic_block bb = BASIC_BLOCK (bb_index);
1055   struct df_rd_bb_info *bb_info = df_rd_get_bb_info (dflow, bb_index);
1056   rtx insn;
1057
1058   bitmap_clear (seen_in_block);
1059   bitmap_clear (seen_in_insn);
1060
1061   df_rd_bb_local_compute_process_def (dflow, bb_info, 
1062                                       df_get_artificial_defs (df, bb_index), 0);
1063
1064   FOR_BB_INSNS_REVERSE (bb, insn)
1065     {
1066       unsigned int uid = INSN_UID (insn);
1067
1068       if (!INSN_P (insn))
1069         continue;
1070
1071       df_rd_bb_local_compute_process_def (dflow, bb_info, 
1072                                           DF_INSN_UID_DEFS (df, uid), 0);
1073
1074       /* This complex dance with the two bitmaps is required because
1075          instructions can assign twice to the same pseudo.  This
1076          generally happens with calls that will have one def for the
1077          result and another def for the clobber.  If only one vector
1078          is used and the clobber goes first, the result will be
1079          lost.  */
1080       bitmap_ior_into (seen_in_block, seen_in_insn);
1081       bitmap_clear (seen_in_insn);
1082     }
1083
1084   /* Process the artificial defs at the top of the block last since we
1085      are going backwards through the block and these are logically at
1086      the start.  */
1087   df_rd_bb_local_compute_process_def (dflow, bb_info, 
1088                                       df_get_artificial_defs (df, bb_index),
1089                                       DF_REF_AT_TOP);
1090 }
1091
1092
1093 /* Compute local reaching def info for each basic block within BLOCKS.  */
1094
1095 static void
1096 df_rd_local_compute (struct dataflow *dflow, 
1097                      bitmap all_blocks,
1098                      bitmap rescan_blocks  ATTRIBUTE_UNUSED)
1099 {
1100   struct df *df = dflow->df;
1101   unsigned int bb_index;
1102   bitmap_iterator bi;
1103   unsigned int regno;
1104   struct df_rd_problem_data *problem_data
1105     = (struct df_rd_problem_data *) dflow->problem_data;
1106   bitmap sparse_invalidated = problem_data->sparse_invalidated_by_call;
1107   bitmap dense_invalidated = problem_data->dense_invalidated_by_call;
1108
1109   df_set_seen ();
1110   df_reorganize_refs (&df->def_info);
1111
1112   EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
1113     {
1114       df_rd_bb_local_compute (dflow, bb_index);
1115     }
1116   
1117   /* Set up the knockout bit vectors to be applied across EH_EDGES.  */
1118   EXECUTE_IF_SET_IN_BITMAP (df_invalidated_by_call, 0, regno, bi)
1119     {
1120       struct df_reg_info *reg_info = DF_REG_DEF_GET (df, regno);
1121       if (reg_info->n_refs > DF_SPARSE_THRESHOLD)
1122         {
1123           bitmap_set_bit (sparse_invalidated, regno);
1124         }
1125       else
1126         {
1127           bitmap defs = df_ref_bitmap (problem_data->def_sites, regno, 
1128                                        reg_info->begin, reg_info->n_refs);
1129           bitmap_ior_into (dense_invalidated, defs);
1130         }
1131     }
1132   df_unset_seen ();
1133 }
1134
1135
1136 /* Initialize the solution bit vectors for problem.  */
1137
1138 static void 
1139 df_rd_init_solution (struct dataflow *dflow, bitmap all_blocks)
1140 {
1141   unsigned int bb_index;
1142   bitmap_iterator bi;
1143
1144   EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
1145     {
1146       struct df_rd_bb_info *bb_info = df_rd_get_bb_info (dflow, bb_index);
1147       
1148       bitmap_copy (bb_info->out, bb_info->gen);
1149       bitmap_clear (bb_info->in);
1150     }
1151 }
1152
1153 /* In of target gets or of out of source.  */
1154
1155 static void
1156 df_rd_confluence_n (struct dataflow *dflow, edge e)
1157 {
1158   bitmap op1 = df_rd_get_bb_info (dflow, e->dest->index)->in;
1159   bitmap op2 = df_rd_get_bb_info (dflow, e->src->index)->out;
1160
1161   if (e->flags & EDGE_EH)
1162     {
1163       struct df_rd_problem_data *problem_data
1164         = (struct df_rd_problem_data *) dflow->problem_data;
1165       bitmap sparse_invalidated = problem_data->sparse_invalidated_by_call;
1166       bitmap dense_invalidated = problem_data->dense_invalidated_by_call;
1167       struct df *df = dflow->df;
1168       bitmap_iterator bi;
1169       unsigned int regno;
1170       bitmap tmp = BITMAP_ALLOC (NULL);
1171
1172       bitmap_copy (tmp, op2);
1173       bitmap_and_compl_into (tmp, dense_invalidated);
1174
1175       EXECUTE_IF_SET_IN_BITMAP (sparse_invalidated, 0, regno, bi)
1176         {
1177           bitmap_clear_range (tmp, 
1178                               DF_REG_DEF_GET (df, regno)->begin, 
1179                               DF_REG_DEF_GET (df, regno)->n_refs);
1180         }
1181       bitmap_ior_into (op1, tmp);
1182       BITMAP_FREE (tmp);
1183     }
1184   else
1185     bitmap_ior_into (op1, op2);
1186 }
1187
1188
1189 /* Transfer function.  */
1190
1191 static bool
1192 df_rd_transfer_function (struct dataflow *dflow, int bb_index)
1193 {
1194   struct df_rd_bb_info *bb_info = df_rd_get_bb_info (dflow, bb_index);
1195   unsigned int regno;
1196   bitmap_iterator bi;
1197   bitmap in = bb_info->in;
1198   bitmap out = bb_info->out;
1199   bitmap gen = bb_info->gen;
1200   bitmap kill = bb_info->kill;
1201   bitmap sparse_kill = bb_info->sparse_kill;
1202
1203   if (bitmap_empty_p (sparse_kill))
1204     return  bitmap_ior_and_compl (out, gen, in, kill);
1205   else 
1206     {
1207       struct df *df = dflow->df;
1208       bool changed = false;
1209       bitmap tmp = BITMAP_ALLOC (NULL);
1210       bitmap_copy (tmp, in);
1211       EXECUTE_IF_SET_IN_BITMAP (sparse_kill, 0, regno, bi)
1212         {
1213           bitmap_clear_range (tmp, 
1214                               DF_REG_DEF_GET (df, regno)->begin, 
1215                               DF_REG_DEF_GET (df, regno)->n_refs);
1216         }
1217       bitmap_and_compl_into (tmp, kill);
1218       bitmap_ior_into (tmp, gen);
1219       changed = !bitmap_equal_p (tmp, out);
1220       if (changed)
1221         {
1222           BITMAP_FREE (out);
1223           bb_info->out = tmp;
1224         }
1225       else 
1226           BITMAP_FREE (tmp);
1227       return changed;
1228     }
1229 }
1230
1231
1232 /* Free all storage associated with the problem.  */
1233
1234 static void
1235 df_rd_free (struct dataflow *dflow)
1236 {
1237   unsigned int i;
1238   struct df_rd_problem_data *problem_data
1239     = (struct df_rd_problem_data *) dflow->problem_data;
1240
1241   if (problem_data)
1242     {
1243       for (i = 0; i < dflow->block_info_size; i++)
1244         {
1245           struct df_rd_bb_info *bb_info = df_rd_get_bb_info (dflow, i);
1246           if (bb_info)
1247             {
1248               BITMAP_FREE (bb_info->kill);
1249               BITMAP_FREE (bb_info->sparse_kill);
1250               BITMAP_FREE (bb_info->gen);
1251               BITMAP_FREE (bb_info->in);
1252               BITMAP_FREE (bb_info->out);
1253             }
1254         }
1255       
1256       free_alloc_pool (dflow->block_pool);
1257       
1258       for (i = 0; i < problem_data->def_sites_size; i++)
1259         {
1260           bitmap bm = problem_data->def_sites[i];
1261           if (bm)
1262             BITMAP_FREE (bm);
1263         }
1264       
1265       free (problem_data->def_sites);
1266       BITMAP_FREE (problem_data->sparse_invalidated_by_call);
1267       BITMAP_FREE (problem_data->dense_invalidated_by_call);
1268       
1269       dflow->block_info_size = 0;
1270       free (dflow->block_info);
1271       free (dflow->problem_data);
1272     }
1273   free (dflow);
1274 }
1275
1276
1277 /* Debugging info.  */
1278
1279 static void
1280 df_rd_dump (struct dataflow *dflow, FILE *file)
1281 {
1282   struct df *df = dflow->df;
1283   basic_block bb;
1284   struct df_rd_problem_data *problem_data
1285     = (struct df_rd_problem_data *) dflow->problem_data;
1286   unsigned int m = max_reg_num ();
1287   unsigned int regno;
1288   
1289   if (!dflow->block_info) 
1290     return;
1291
1292   fprintf (file, "Reaching defs:\n\n");
1293
1294   fprintf (file, "  sparse invalidated \t");
1295   dump_bitmap (file, problem_data->sparse_invalidated_by_call);
1296   fprintf (file, "  dense invalidated \t");
1297   dump_bitmap (file, problem_data->dense_invalidated_by_call);
1298
1299   for (regno = 0; regno < m; regno++)
1300     if (DF_REG_DEF_GET (df, regno)->n_refs)
1301       fprintf (file, "%d[%d,%d] ", regno, 
1302                DF_REG_DEF_GET (df, regno)->begin, 
1303                DF_REG_DEF_GET (df, regno)->n_refs);
1304   fprintf (file, "\n");
1305
1306   FOR_ALL_BB (bb)
1307     {
1308       struct df_rd_bb_info *bb_info = df_rd_get_bb_info (dflow, bb->index);
1309       df_print_bb_index (bb, file);
1310       
1311       if (!bb_info->in)
1312         continue;
1313       
1314       fprintf (file, "  in  \t(%d)\n", (int) bitmap_count_bits (bb_info->in));
1315       dump_bitmap (file, bb_info->in);
1316       fprintf (file, "  gen \t(%d)\n", (int) bitmap_count_bits (bb_info->gen));
1317       dump_bitmap (file, bb_info->gen);
1318       fprintf (file, "  kill\t(%d)\n", (int) bitmap_count_bits (bb_info->kill));
1319       dump_bitmap (file, bb_info->kill);
1320       fprintf (file, "  out \t(%d)\n", (int) bitmap_count_bits (bb_info->out));
1321       dump_bitmap (file, bb_info->out);
1322     }
1323 }
1324
1325 /* All of the information associated with every instance of the problem.  */
1326
1327 static struct df_problem problem_RD =
1328 {
1329   DF_RD,                      /* Problem id.  */
1330   DF_FORWARD,                 /* Direction.  */
1331   df_rd_alloc,                /* Allocate the problem specific data.  */
1332   NULL,                       /* Reset global information.  */
1333   df_rd_free_bb_info,         /* Free basic block info.  */
1334   df_rd_local_compute,        /* Local compute function.  */
1335   df_rd_init_solution,        /* Init the solution specific data.  */
1336   df_iterative_dataflow,      /* Iterative solver.  */
1337   NULL,                       /* Confluence operator 0.  */ 
1338   df_rd_confluence_n,         /* Confluence operator n.  */ 
1339   df_rd_transfer_function,    /* Transfer function.  */
1340   NULL,                       /* Finalize function.  */
1341   df_rd_free,                 /* Free all of the problem information.  */
1342   df_rd_dump,                 /* Debugging.  */
1343   NULL,                       /* Dependent problem.  */
1344   0                           /* Changeable flags.  */
1345 };
1346
1347
1348
1349 /* Create a new DATAFLOW instance and add it to an existing instance
1350    of DF.  The returned structure is what is used to get at the
1351    solution.  */
1352
1353 struct dataflow *
1354 df_rd_add_problem (struct df *df, int flags)
1355 {
1356   return df_add_problem (df, &problem_RD, flags);
1357 }
1358
1359
1360 \f
1361 /*----------------------------------------------------------------------------
1362    LIVE REGISTERS
1363
1364    Find the locations in the function where any use of a pseudo can
1365    reach in the backwards direction.  In and out bitvectors are built
1366    for each basic block.  The regnum is used to index into these sets.
1367    See df.h for details.
1368    ----------------------------------------------------------------------------*/
1369
1370 /* Get basic block info.  */
1371
1372 struct df_lr_bb_info *
1373 df_lr_get_bb_info (struct dataflow *dflow, unsigned int index)
1374 {
1375   return (struct df_lr_bb_info *) dflow->block_info[index];
1376 }
1377
1378
1379 /* Set basic block info.  */
1380
1381 static void
1382 df_lr_set_bb_info (struct dataflow *dflow, unsigned int index, 
1383                    struct df_lr_bb_info *bb_info)
1384 {
1385   dflow->block_info[index] = bb_info;
1386 }
1387
1388  
1389 /* Free basic block info.  */
1390
1391 static void
1392 df_lr_free_bb_info (struct dataflow *dflow, 
1393                     basic_block bb ATTRIBUTE_UNUSED, 
1394                     void *vbb_info)
1395 {
1396   struct df_lr_bb_info *bb_info = (struct df_lr_bb_info *) vbb_info;
1397   if (bb_info)
1398     {
1399       BITMAP_FREE (bb_info->use);
1400       BITMAP_FREE (bb_info->def);
1401       BITMAP_FREE (bb_info->in);
1402       BITMAP_FREE (bb_info->out);
1403       pool_free (dflow->block_pool, bb_info);
1404     }
1405 }
1406
1407
1408 /* Allocate or reset bitmaps for DFLOW blocks. The solution bits are
1409    not touched unless the block is new.  */
1410
1411 static void 
1412 df_lr_alloc (struct dataflow *dflow, bitmap blocks_to_rescan,
1413              bitmap all_blocks ATTRIBUTE_UNUSED)
1414 {
1415   unsigned int bb_index;
1416   bitmap_iterator bi;
1417
1418   if (!dflow->block_pool)
1419     dflow->block_pool = create_alloc_pool ("df_lr_block pool", 
1420                                            sizeof (struct df_lr_bb_info), 50);
1421
1422   df_grow_bb_info (dflow);
1423
1424   EXECUTE_IF_SET_IN_BITMAP (blocks_to_rescan, 0, bb_index, bi)
1425     {
1426       struct df_lr_bb_info *bb_info = df_lr_get_bb_info (dflow, bb_index);
1427       if (bb_info)
1428         { 
1429           bitmap_clear (bb_info->def);
1430           bitmap_clear (bb_info->use);
1431         }
1432       else
1433         { 
1434           bb_info = (struct df_lr_bb_info *) pool_alloc (dflow->block_pool);
1435           df_lr_set_bb_info (dflow, bb_index, bb_info);
1436           bb_info->use = BITMAP_ALLOC (NULL);
1437           bb_info->def = BITMAP_ALLOC (NULL);
1438           bb_info->in = BITMAP_ALLOC (NULL);
1439           bb_info->out = BITMAP_ALLOC (NULL);
1440         }
1441     }
1442 }
1443
1444
1445 /* Compute local live register info for basic block BB.  */
1446
1447 static void
1448 df_lr_bb_local_compute (struct dataflow *dflow, 
1449                         struct df *df, unsigned int bb_index)
1450 {
1451   basic_block bb = BASIC_BLOCK (bb_index);
1452   struct df_lr_bb_info *bb_info = df_lr_get_bb_info (dflow, bb_index);
1453   rtx insn;
1454   struct df_ref *def;
1455   struct df_ref *use;
1456
1457   /* Process the registers set in an exception handler.  */
1458   for (def = df_get_artificial_defs (df, bb_index); def; def = def->next_ref)
1459     if (((DF_REF_FLAGS (def) & DF_REF_AT_TOP) == 0)
1460         && (!(DF_REF_FLAGS (def) & DF_REF_PARTIAL)))
1461       {
1462         unsigned int dregno = DF_REF_REGNO (def);
1463         bitmap_set_bit (bb_info->def, dregno);
1464         bitmap_clear_bit (bb_info->use, dregno);
1465       }
1466
1467   /* Process the hardware registers that are always live.  */
1468   for (use = df_get_artificial_uses (df, bb_index); use; use = use->next_ref)
1469     /* Add use to set of uses in this BB.  */
1470     if ((DF_REF_FLAGS (use) & DF_REF_AT_TOP) == 0)
1471       bitmap_set_bit (bb_info->use, DF_REF_REGNO (use));
1472
1473   FOR_BB_INSNS_REVERSE (bb, insn)
1474     {
1475       unsigned int uid = INSN_UID (insn);
1476
1477       if (!INSN_P (insn))
1478         continue;       
1479
1480       if (CALL_P (insn))
1481         {
1482           for (def = DF_INSN_UID_DEFS (df, uid); def; def = def->next_ref)
1483             {
1484               unsigned int dregno = DF_REF_REGNO (def);
1485               
1486               if (dregno >= FIRST_PSEUDO_REGISTER
1487                   || !(SIBLING_CALL_P (insn)
1488                        && bitmap_bit_p (df->exit_block_uses, dregno)
1489                        && !refers_to_regno_p (dregno, dregno+1,
1490                                               current_function_return_rtx,
1491                                               (rtx *)0)))
1492                 {
1493                   /* If the def is to only part of the reg, it does
1494                      not kill the other defs that reach here.  */
1495                   if (!(DF_REF_FLAGS (def) & DF_REF_PARTIAL))
1496                     {
1497                       bitmap_set_bit (bb_info->def, dregno);
1498                       bitmap_clear_bit (bb_info->use, dregno);
1499                     }
1500                 }
1501             }
1502         }
1503       else
1504         {
1505           for (def = DF_INSN_UID_DEFS (df, uid); def; def = def->next_ref)
1506             {
1507               unsigned int dregno = DF_REF_REGNO (def);
1508               
1509               if (DF_INSN_CONTAINS_ASM (df, insn) 
1510                   && dregno < FIRST_PSEUDO_REGISTER)
1511                 {
1512                   unsigned int i;
1513                   unsigned int end = dregno 
1514                     + hard_regno_nregs[dregno][GET_MODE (DF_REF_REG (def))] - 1;
1515                   for (i = dregno; i <= end; ++i)
1516                     regs_asm_clobbered[i] = 1;
1517                 }
1518               /* If the def is to only part of the reg, it does
1519                      not kill the other defs that reach here.  */
1520               if (!(DF_REF_FLAGS (def) & DF_REF_PARTIAL))
1521                 {
1522                   bitmap_set_bit (bb_info->def, dregno);
1523                   bitmap_clear_bit (bb_info->use, dregno);
1524                 }
1525             }
1526         }
1527
1528       for (use = DF_INSN_UID_USES (df, uid); use; use = use->next_ref)
1529         /* Add use to set of uses in this BB.  */
1530         bitmap_set_bit (bb_info->use, DF_REF_REGNO (use));
1531     }
1532
1533   /* Process the registers set in an exception handler.  */
1534   for (def = df_get_artificial_defs (df, bb_index); def; def = def->next_ref)
1535     if ((DF_REF_FLAGS (def) & DF_REF_AT_TOP)
1536         && (!(DF_REF_FLAGS (def) & DF_REF_PARTIAL)))
1537       {
1538         unsigned int dregno = DF_REF_REGNO (def);
1539         bitmap_set_bit (bb_info->def, dregno);
1540         bitmap_clear_bit (bb_info->use, dregno);
1541       }
1542   
1543 #ifdef EH_USES
1544   /* Process the uses that are live into an exception handler.  */
1545   for (use = df_get_artificial_uses (df, bb_index); use; use = use->next_ref)
1546     /* Add use to set of uses in this BB.  */
1547     if (DF_REF_FLAGS (use) & DF_REF_AT_TOP)
1548       bitmap_set_bit (bb_info->use, DF_REF_REGNO (use));
1549 #endif
1550 }
1551
1552
1553 /* Compute local live register info for each basic block within BLOCKS.  */
1554
1555 static void
1556 df_lr_local_compute (struct dataflow *dflow, 
1557                      bitmap all_blocks,
1558                      bitmap rescan_blocks)
1559 {
1560   struct df *df = dflow->df;
1561   unsigned int bb_index;
1562   bitmap_iterator bi;
1563     
1564   /* Assume that the stack pointer is unchanging if alloca hasn't
1565      been used.  */
1566   if (bitmap_equal_p (all_blocks, rescan_blocks))
1567     memset (regs_asm_clobbered, 0, sizeof (regs_asm_clobbered));
1568   
1569   bitmap_clear (df->hardware_regs_used);
1570   
1571   /* The all-important stack pointer must always be live.  */
1572   bitmap_set_bit (df->hardware_regs_used, STACK_POINTER_REGNUM);
1573   
1574   /* Before reload, there are a few registers that must be forced
1575      live everywhere -- which might not already be the case for
1576      blocks within infinite loops.  */
1577   if (!reload_completed)
1578     {
1579       /* Any reference to any pseudo before reload is a potential
1580          reference of the frame pointer.  */
1581       bitmap_set_bit (df->hardware_regs_used, FRAME_POINTER_REGNUM);
1582       
1583 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
1584       /* Pseudos with argument area equivalences may require
1585          reloading via the argument pointer.  */
1586       if (fixed_regs[ARG_POINTER_REGNUM])
1587         bitmap_set_bit (df->hardware_regs_used, ARG_POINTER_REGNUM);
1588 #endif
1589       
1590       /* Any constant, or pseudo with constant equivalences, may
1591          require reloading from memory using the pic register.  */
1592       if ((unsigned) PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
1593           && fixed_regs[PIC_OFFSET_TABLE_REGNUM])
1594         bitmap_set_bit (df->hardware_regs_used, PIC_OFFSET_TABLE_REGNUM);
1595     }
1596   
1597   if (bitmap_bit_p (rescan_blocks, EXIT_BLOCK))
1598     {
1599       /* The exit block is special for this problem and its bits are
1600          computed from thin air.  */
1601       struct df_lr_bb_info *bb_info = df_lr_get_bb_info (dflow, EXIT_BLOCK);
1602       bitmap_copy (bb_info->use, df->exit_block_uses);
1603     }
1604   
1605   EXECUTE_IF_SET_IN_BITMAP (rescan_blocks, 0, bb_index, bi)
1606     {
1607       if (bb_index == EXIT_BLOCK)
1608         continue;
1609       df_lr_bb_local_compute (dflow, df, bb_index);
1610     }
1611 }
1612
1613
1614 /* Initialize the solution vectors.  */
1615
1616 static void 
1617 df_lr_init (struct dataflow *dflow, bitmap all_blocks)
1618 {
1619   unsigned int bb_index;
1620   bitmap_iterator bi;
1621
1622   EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
1623     {
1624       struct df_lr_bb_info *bb_info = df_lr_get_bb_info (dflow, bb_index);
1625       bitmap_copy (bb_info->in, bb_info->use);
1626       bitmap_clear (bb_info->out);
1627     }
1628 }
1629
1630
1631 /* Confluence function that processes infinite loops.  This might be a
1632    noreturn function that throws.  And even if it isn't, getting the
1633    unwind info right helps debugging.  */
1634 static void
1635 df_lr_confluence_0 (struct dataflow *dflow, basic_block bb)
1636 {
1637   struct df *df = dflow->df;
1638
1639   bitmap op1 = df_lr_get_bb_info (dflow, bb->index)->out;
1640   if (bb != EXIT_BLOCK_PTR)
1641     bitmap_copy (op1, df->hardware_regs_used);
1642
1643
1644
1645 /* Confluence function that ignores fake edges.  */
1646
1647 static void
1648 df_lr_confluence_n (struct dataflow *dflow, edge e)
1649 {
1650   bitmap op1 = df_lr_get_bb_info (dflow, e->src->index)->out;
1651   bitmap op2 = df_lr_get_bb_info (dflow, e->dest->index)->in;
1652  
1653   /* Call-clobbered registers die across exception and call edges.  */
1654   /* ??? Abnormal call edges ignored for the moment, as this gets
1655      confused by sibling call edges, which crashes reg-stack.  */
1656   if (e->flags & EDGE_EH)
1657     bitmap_ior_and_compl_into (op1, op2, df_invalidated_by_call);
1658   else
1659     bitmap_ior_into (op1, op2);
1660
1661   bitmap_ior_into (op1, dflow->df->hardware_regs_used);
1662
1663
1664
1665 /* Transfer function.  */
1666
1667 static bool
1668 df_lr_transfer_function (struct dataflow *dflow, int bb_index)
1669 {
1670   struct df_lr_bb_info *bb_info = df_lr_get_bb_info (dflow, bb_index);
1671   bitmap in = bb_info->in;
1672   bitmap out = bb_info->out;
1673   bitmap use = bb_info->use;
1674   bitmap def = bb_info->def;
1675
1676   return bitmap_ior_and_compl (in, use, out, def);
1677 }
1678
1679
1680 /* Free all storage associated with the problem.  */
1681
1682 static void
1683 df_lr_free (struct dataflow *dflow)
1684 {
1685   if (dflow->block_info)
1686     {
1687       unsigned int i;
1688       for (i = 0; i < dflow->block_info_size; i++)
1689         {
1690           struct df_lr_bb_info *bb_info = df_lr_get_bb_info (dflow, i);
1691           if (bb_info)
1692             {
1693               BITMAP_FREE (bb_info->use);
1694               BITMAP_FREE (bb_info->def);
1695               BITMAP_FREE (bb_info->in);
1696               BITMAP_FREE (bb_info->out);
1697             }
1698         }
1699       free_alloc_pool (dflow->block_pool);
1700       
1701       dflow->block_info_size = 0;
1702       free (dflow->block_info);
1703     }
1704
1705   free (dflow->problem_data);
1706   free (dflow);
1707 }
1708
1709
1710 /* Debugging info.  */
1711
1712 static void
1713 df_lr_dump (struct dataflow *dflow, FILE *file)
1714 {
1715   basic_block bb;
1716   
1717   if (!dflow->block_info) 
1718     return;
1719
1720   fprintf (file, "Live Registers:\n");
1721   FOR_ALL_BB (bb)
1722     {
1723       struct df_lr_bb_info *bb_info = df_lr_get_bb_info (dflow, bb->index);
1724       df_print_bb_index (bb, file);
1725       
1726       if (!bb_info->in)
1727         continue;
1728       
1729       fprintf (file, "  in  \t");
1730       dump_bitmap (file, bb_info->in);
1731       fprintf (file, "  use \t");
1732       dump_bitmap (file, bb_info->use);
1733       fprintf (file, "  def \t");
1734       dump_bitmap (file, bb_info->def);
1735       fprintf (file, "  out \t");
1736       dump_bitmap (file, bb_info->out);
1737     }
1738 }
1739
1740 /* All of the information associated with every instance of the problem.  */
1741
1742 static struct df_problem problem_LR =
1743 {
1744   DF_LR,                      /* Problem id.  */
1745   DF_BACKWARD,                /* Direction.  */
1746   df_lr_alloc,                /* Allocate the problem specific data.  */
1747   NULL,                       /* Reset global information.  */
1748   df_lr_free_bb_info,         /* Free basic block info.  */
1749   df_lr_local_compute,        /* Local compute function.  */
1750   df_lr_init,                 /* Init the solution specific data.  */
1751   df_iterative_dataflow,      /* Iterative solver.  */
1752   df_lr_confluence_0,         /* Confluence operator 0.  */ 
1753   df_lr_confluence_n,         /* Confluence operator n.  */ 
1754   df_lr_transfer_function,    /* Transfer function.  */
1755   NULL,                       /* Finalize function.  */
1756   df_lr_free,                 /* Free all of the problem information.  */
1757   df_lr_dump,                 /* Debugging.  */
1758   NULL,                       /* Dependent problem.  */
1759   0
1760 };
1761
1762
1763 /* Create a new DATAFLOW instance and add it to an existing instance
1764    of DF.  The returned structure is what is used to get at the
1765    solution.  */
1766
1767 struct dataflow *
1768 df_lr_add_problem (struct df *df, int flags)
1769 {
1770   return df_add_problem (df, &problem_LR, flags);
1771 }
1772
1773
1774 \f
1775 /*----------------------------------------------------------------------------
1776    UNINITIALIZED REGISTERS
1777
1778    Find the set of uses for registers that are reachable from the entry
1779    block without passing thru a definition.  In and out bitvectors are built
1780    for each basic block.  The regnum is used to index into these sets.
1781    See df.h for details.
1782 ----------------------------------------------------------------------------*/
1783
1784 /* Get basic block info.  */
1785
1786 struct df_ur_bb_info *
1787 df_ur_get_bb_info (struct dataflow *dflow, unsigned int index)
1788 {
1789   return (struct df_ur_bb_info *) dflow->block_info[index];
1790 }
1791
1792
1793 /* Set basic block info.  */
1794
1795 static void
1796 df_ur_set_bb_info (struct dataflow *dflow, unsigned int index, 
1797                    struct df_ur_bb_info *bb_info)
1798 {
1799   dflow->block_info[index] = bb_info;
1800 }
1801
1802
1803 /* Free basic block info.  */
1804
1805 static void
1806 df_ur_free_bb_info (struct dataflow *dflow, 
1807                     basic_block bb ATTRIBUTE_UNUSED, 
1808                     void *vbb_info)
1809 {
1810   struct df_ur_bb_info *bb_info = (struct df_ur_bb_info *) vbb_info;
1811   if (bb_info)
1812     {
1813       BITMAP_FREE (bb_info->gen);
1814       BITMAP_FREE (bb_info->kill);
1815       BITMAP_FREE (bb_info->in);
1816       BITMAP_FREE (bb_info->out);
1817       pool_free (dflow->block_pool, bb_info);
1818     }
1819 }
1820
1821
1822 /* Allocate or reset bitmaps for DFLOW blocks. The solution bits are
1823    not touched unless the block is new.  */
1824
1825 static void 
1826 df_ur_alloc (struct dataflow *dflow, bitmap blocks_to_rescan,
1827              bitmap all_blocks ATTRIBUTE_UNUSED)
1828 {
1829   unsigned int bb_index;
1830   bitmap_iterator bi;
1831
1832   if (!dflow->block_pool)
1833     dflow->block_pool = create_alloc_pool ("df_ur_block pool", 
1834                                            sizeof (struct df_ur_bb_info), 100);
1835
1836   df_grow_bb_info (dflow);
1837
1838   EXECUTE_IF_SET_IN_BITMAP (blocks_to_rescan, 0, bb_index, bi)
1839     {
1840       struct df_ur_bb_info *bb_info = df_ur_get_bb_info (dflow, bb_index);
1841       if (bb_info)
1842         { 
1843           bitmap_clear (bb_info->kill);
1844           bitmap_clear (bb_info->gen);
1845         }
1846       else
1847         { 
1848           bb_info = (struct df_ur_bb_info *) pool_alloc (dflow->block_pool);
1849           df_ur_set_bb_info (dflow, bb_index, bb_info);
1850           bb_info->kill = BITMAP_ALLOC (NULL);
1851           bb_info->gen = BITMAP_ALLOC (NULL);
1852           bb_info->in = BITMAP_ALLOC (NULL);
1853           bb_info->out = BITMAP_ALLOC (NULL);
1854         }
1855     }
1856 }
1857
1858
1859 /* Compute local uninitialized register info for basic block BB.  */
1860
1861 static void
1862 df_ur_bb_local_compute (struct dataflow *dflow, unsigned int bb_index)
1863 {
1864   struct df *df = dflow->df;
1865   basic_block bb = BASIC_BLOCK (bb_index);
1866   struct df_ur_bb_info *bb_info = df_ur_get_bb_info (dflow, bb_index);
1867   rtx insn;
1868   struct df_ref *def;
1869
1870   bitmap_clear (seen_in_block);
1871   bitmap_clear (seen_in_insn);
1872
1873   for (def = df_get_artificial_defs (df, bb_index); def; def = def->next_ref)
1874     if ((DF_REF_FLAGS (def) & DF_REF_AT_TOP) == 0)
1875       {
1876         unsigned int regno = DF_REF_REGNO (def);
1877         if (!bitmap_bit_p (seen_in_block, regno))
1878           {
1879             bitmap_set_bit (seen_in_block, regno);
1880             bitmap_set_bit (bb_info->gen, regno);
1881           }
1882       }
1883
1884   FOR_BB_INSNS_REVERSE (bb, insn)
1885     {
1886       unsigned int uid = INSN_UID (insn);
1887       if (!INSN_P (insn))
1888         continue;
1889
1890       for (def = DF_INSN_UID_DEFS (df, uid); def; def = def->next_ref)
1891         {
1892           unsigned int regno = DF_REF_REGNO (def);
1893           /* Only the last def counts.  */
1894           if (!bitmap_bit_p (seen_in_block, regno))
1895             {
1896               bitmap_set_bit (seen_in_insn, regno);
1897               
1898               if (DF_REF_FLAGS (def) 
1899                   & (DF_REF_MUST_CLOBBER | DF_REF_MAY_CLOBBER))
1900                 {
1901                   /* Only must clobbers for the entire reg destroy the
1902                      value.  */
1903                   if ((DF_REF_FLAGS (def) & DF_REF_MUST_CLOBBER)
1904                       && (!DF_REF_FLAGS (def) & DF_REF_PARTIAL))
1905                     bitmap_set_bit (bb_info->kill, regno);
1906                 }
1907               else
1908                 bitmap_set_bit (bb_info->gen, regno);
1909             }
1910         }
1911       bitmap_ior_into (seen_in_block, seen_in_insn);
1912       bitmap_clear (seen_in_insn);
1913     }
1914
1915   for (def = df_get_artificial_defs (df, bb_index); def; def = def->next_ref)
1916     if (DF_REF_FLAGS (def) & DF_REF_AT_TOP)
1917       {
1918         unsigned int regno = DF_REF_REGNO (def);
1919         if (!bitmap_bit_p (seen_in_block, regno))
1920           {
1921             bitmap_set_bit (seen_in_block, regno);
1922             bitmap_set_bit (bb_info->gen, regno);
1923           }
1924       }
1925 }
1926
1927
1928 /* Compute local uninitialized register info.  */
1929
1930 static void
1931 df_ur_local_compute (struct dataflow *dflow, 
1932                      bitmap all_blocks ATTRIBUTE_UNUSED,
1933                      bitmap rescan_blocks)
1934 {
1935   unsigned int bb_index;
1936   bitmap_iterator bi;
1937
1938   df_set_seen ();
1939
1940   EXECUTE_IF_SET_IN_BITMAP (rescan_blocks, 0, bb_index, bi)
1941     {
1942       df_ur_bb_local_compute (dflow, bb_index);
1943     }
1944
1945   df_unset_seen ();
1946 }
1947
1948
1949 /* Initialize the solution vectors.  */
1950
1951 static void 
1952 df_ur_init (struct dataflow *dflow, bitmap all_blocks)
1953 {
1954   unsigned int bb_index;
1955   bitmap_iterator bi;
1956
1957   EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
1958     {
1959       struct df_ur_bb_info *bb_info = df_ur_get_bb_info (dflow, bb_index);
1960
1961       bitmap_copy (bb_info->out, bb_info->gen);
1962       bitmap_clear (bb_info->in);
1963     }
1964 }
1965
1966
1967 /* Or in the stack regs, hard regs and early clobber regs into the
1968    ur_in sets of all of the blocks.  */
1969
1970 static void
1971 df_ur_local_finalize (struct dataflow *dflow, bitmap all_blocks)
1972 {
1973   struct df *df = dflow->df;
1974   struct dataflow *lr_dflow = df->problems_by_index[DF_LR];
1975   bitmap tmp = BITMAP_ALLOC (NULL);
1976   bitmap_iterator bi;
1977   unsigned int bb_index;
1978
1979   EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
1980     {
1981       struct df_ur_bb_info *bb_info = df_ur_get_bb_info (dflow, bb_index);
1982       struct df_lr_bb_info *bb_lr_info = df_lr_get_bb_info (lr_dflow, bb_index);
1983       
1984       /* No register may reach a location where it is not used.  Thus
1985          we trim the rr result to the places where it is used.  */
1986       bitmap_and_into (bb_info->in, bb_lr_info->in);
1987       bitmap_and_into (bb_info->out, bb_lr_info->out);
1988       
1989 #if 1
1990       /* Hard registers may still stick in the ur_out set, but not
1991          be in the ur_in set, if their only mention was in a call
1992          in this block.  This is because a call kills in the lr
1993          problem but does not kill in the ur problem.  To clean
1994          this up, we execute the transfer function on the lr_in
1995          set and then use that to knock bits out of ur_out.  */
1996       bitmap_ior_and_compl (tmp, bb_info->gen, bb_lr_info->in, 
1997                             bb_info->kill);
1998       bitmap_and_into (bb_info->out, tmp);
1999 #endif
2000     }
2001
2002   BITMAP_FREE (tmp);
2003 }
2004
2005
2006 /* Confluence function that ignores fake edges.  */
2007
2008 static void
2009 df_ur_confluence_n (struct dataflow *dflow, edge e)
2010 {
2011   bitmap op1 = df_ur_get_bb_info (dflow, e->dest->index)->in;
2012   bitmap op2 = df_ur_get_bb_info (dflow, e->src->index)->out;
2013  
2014   if (e->flags & EDGE_FAKE) 
2015     return;
2016
2017   bitmap_ior_into (op1, op2);
2018
2019
2020
2021 /* Transfer function.  */
2022
2023 static bool
2024 df_ur_transfer_function (struct dataflow *dflow, int bb_index)
2025 {
2026   struct df_ur_bb_info *bb_info = df_ur_get_bb_info (dflow, bb_index);
2027   bitmap in = bb_info->in;
2028   bitmap out = bb_info->out;
2029   bitmap gen = bb_info->gen;
2030   bitmap kill = bb_info->kill;
2031
2032   return bitmap_ior_and_compl (out, gen, in, kill);
2033 }
2034
2035
2036 /* Free all storage associated with the problem.  */
2037
2038 static void
2039 df_ur_free (struct dataflow *dflow)
2040 {
2041   if (dflow->block_info)
2042     {
2043       unsigned int i;
2044       
2045       for (i = 0; i < dflow->block_info_size; i++)
2046         {
2047           struct df_ur_bb_info *bb_info = df_ur_get_bb_info (dflow, i);
2048           if (bb_info)
2049             {
2050               BITMAP_FREE (bb_info->gen);
2051               BITMAP_FREE (bb_info->kill);
2052               BITMAP_FREE (bb_info->in);
2053               BITMAP_FREE (bb_info->out);
2054             }
2055         }
2056       
2057       free_alloc_pool (dflow->block_pool);
2058       dflow->block_info_size = 0;
2059       free (dflow->block_info);
2060     }
2061   free (dflow);
2062 }
2063
2064
2065 /* Debugging info.  */
2066
2067 static void
2068 df_ur_dump (struct dataflow *dflow, FILE *file)
2069 {
2070   basic_block bb;
2071   
2072   if (!dflow->block_info) 
2073     return;
2074
2075   fprintf (file, "Undefined regs:\n");
2076  
2077   FOR_ALL_BB (bb)
2078     {
2079       struct df_ur_bb_info *bb_info = df_ur_get_bb_info (dflow, bb->index);
2080       df_print_bb_index (bb, file);
2081       
2082       if (!bb_info->in)
2083         continue;
2084       
2085       fprintf (file, "  in  \t");
2086       dump_bitmap (file, bb_info->in);
2087       fprintf (file, "  gen \t");
2088       dump_bitmap (file, bb_info->gen);
2089       fprintf (file, "  kill\t");
2090       dump_bitmap (file, bb_info->kill);
2091       fprintf (file, "  out \t");
2092       dump_bitmap (file, bb_info->out);
2093     }
2094 }
2095
2096 /* All of the information associated with every instance of the problem.  */
2097
2098 static struct df_problem problem_UR =
2099 {
2100   DF_UR,                      /* Problem id.  */
2101   DF_FORWARD,                 /* Direction.  */
2102   df_ur_alloc,                /* Allocate the problem specific data.  */
2103   NULL,                       /* Reset global information.  */
2104   df_ur_free_bb_info,         /* Free basic block info.  */
2105   df_ur_local_compute,        /* Local compute function.  */
2106   df_ur_init,                 /* Init the solution specific data.  */
2107   df_iterative_dataflow,      /* Iterative solver.  */
2108   NULL,                       /* Confluence operator 0.  */ 
2109   df_ur_confluence_n,         /* Confluence operator n.  */ 
2110   df_ur_transfer_function,    /* Transfer function.  */
2111   df_ur_local_finalize,       /* Finalize function.  */
2112   df_ur_free,                 /* Free all of the problem information.  */
2113   df_ur_dump,                 /* Debugging.  */
2114   df_lr_add_problem,          /* Dependent problem.  */
2115   0                           /* Changeable flags.  */
2116 };
2117
2118
2119 /* Create a new DATAFLOW instance and add it to an existing instance
2120    of DF.  The returned structure is what is used to get at the
2121    solution.  */
2122
2123 struct dataflow *
2124 df_ur_add_problem (struct df *df, int flags)
2125 {
2126   return df_add_problem (df, &problem_UR, flags);
2127 }
2128
2129
2130 \f
2131 /*----------------------------------------------------------------------------
2132    UNINITIALIZED REGISTERS WITH EARLYCLOBBER
2133
2134    Find the set of uses for registers that are reachable from the entry
2135    block without passing thru a definition.  In and out bitvectors are built
2136    for each basic block.  The regnum is used to index into these sets.
2137    See df.h for details.
2138
2139    This is a variant of the UR problem above that has a lot of special
2140    features just for the register allocation phase.  This problem
2141    should go a away if someone would fix the interference graph.
2142
2143    ----------------------------------------------------------------------------*/
2144
2145 /* Private data used to compute the solution for this problem.  These
2146    data structures are not accessible outside of this module.  */
2147 struct df_urec_problem_data
2148 {
2149   bool earlyclobbers_found;     /* True if any instruction contains an
2150                                    earlyclobber.  */
2151 #ifdef STACK_REGS
2152   bitmap stack_regs;            /* Registers that may be allocated to a STACK_REGS.  */
2153 #endif
2154 };
2155
2156
2157 /* Get basic block info.  */
2158
2159 struct df_urec_bb_info *
2160 df_urec_get_bb_info (struct dataflow *dflow, unsigned int index)
2161 {
2162   return (struct df_urec_bb_info *) dflow->block_info[index];
2163 }
2164
2165
2166 /* Set basic block info.  */
2167
2168 static void
2169 df_urec_set_bb_info (struct dataflow *dflow, unsigned int index, 
2170                    struct df_urec_bb_info *bb_info)
2171 {
2172   dflow->block_info[index] = bb_info;
2173 }
2174
2175
2176 /* Free basic block info.  */
2177
2178 static void
2179 df_urec_free_bb_info (struct dataflow *dflow, 
2180                       basic_block bb ATTRIBUTE_UNUSED, 
2181                       void *vbb_info)
2182 {
2183   struct df_urec_bb_info *bb_info = (struct df_urec_bb_info *) vbb_info;
2184   if (bb_info)
2185     {
2186       BITMAP_FREE (bb_info->gen);
2187       BITMAP_FREE (bb_info->kill);
2188       BITMAP_FREE (bb_info->in);
2189       BITMAP_FREE (bb_info->out);
2190       BITMAP_FREE (bb_info->earlyclobber);
2191       pool_free (dflow->block_pool, bb_info);
2192     }
2193 }
2194
2195
2196 /* Allocate or reset bitmaps for DFLOW blocks. The solution bits are
2197    not touched unless the block is new.  */
2198
2199 static void 
2200 df_urec_alloc (struct dataflow *dflow, bitmap blocks_to_rescan,
2201                bitmap all_blocks ATTRIBUTE_UNUSED)
2202
2203 {
2204   unsigned int bb_index;
2205   bitmap_iterator bi;
2206   struct df_urec_problem_data *problem_data
2207     = (struct df_urec_problem_data *) dflow->problem_data;
2208
2209   if (!dflow->block_pool)
2210     dflow->block_pool = create_alloc_pool ("df_urec_block pool", 
2211                                            sizeof (struct df_urec_bb_info), 50);
2212
2213   if (!dflow->problem_data)
2214     {
2215       problem_data = XNEW (struct df_urec_problem_data);
2216       dflow->problem_data = problem_data;
2217     }
2218   problem_data->earlyclobbers_found = false;
2219
2220   df_grow_bb_info (dflow);
2221
2222   EXECUTE_IF_SET_IN_BITMAP (blocks_to_rescan, 0, bb_index, bi)
2223     {
2224       struct df_urec_bb_info *bb_info = df_urec_get_bb_info (dflow, bb_index);
2225       if (bb_info)
2226         { 
2227           bitmap_clear (bb_info->kill);
2228           bitmap_clear (bb_info->gen);
2229           bitmap_clear (bb_info->earlyclobber);
2230         }
2231       else
2232         { 
2233           bb_info = (struct df_urec_bb_info *) pool_alloc (dflow->block_pool);
2234           df_urec_set_bb_info (dflow, bb_index, bb_info);
2235           bb_info->kill = BITMAP_ALLOC (NULL);
2236           bb_info->gen = BITMAP_ALLOC (NULL);
2237           bb_info->in = BITMAP_ALLOC (NULL);
2238           bb_info->out = BITMAP_ALLOC (NULL);
2239           bb_info->earlyclobber = BITMAP_ALLOC (NULL);
2240         }
2241     }
2242 }
2243
2244
2245 /* The function modifies local info for register REG being changed in
2246    SETTER.  DATA is used to pass the current basic block info.  */
2247
2248 static void
2249 df_urec_mark_reg_change (rtx reg, rtx setter, void *data)
2250 {
2251   int regno;
2252   int endregno;
2253   int i;
2254   struct df_urec_bb_info *bb_info = (struct df_urec_bb_info*) data;
2255
2256   if (GET_CODE (reg) == SUBREG)
2257     reg = SUBREG_REG (reg);
2258
2259   if (!REG_P (reg))
2260     return;
2261   
2262   
2263   endregno = regno = REGNO (reg);
2264   if (regno < FIRST_PSEUDO_REGISTER)
2265     {
2266       endregno +=hard_regno_nregs[regno][GET_MODE (reg)];
2267       
2268       for (i = regno; i < endregno; i++)
2269         {
2270           bitmap_set_bit (bb_info->kill, i);
2271           
2272           if (GET_CODE (setter) != CLOBBER)
2273             bitmap_set_bit (bb_info->gen, i);
2274           else
2275             bitmap_clear_bit (bb_info->gen, i);
2276         }
2277     }
2278   else
2279     {
2280       bitmap_set_bit (bb_info->kill, regno);
2281       
2282       if (GET_CODE (setter) != CLOBBER)
2283         bitmap_set_bit (bb_info->gen, regno);
2284       else
2285         bitmap_clear_bit (bb_info->gen, regno);
2286     }
2287 }
2288 /* Classes of registers which could be early clobbered in the current
2289    insn.  */
2290
2291 static VEC(int,heap) *earlyclobber_regclass;
2292
2293 /* This function finds and stores register classes that could be early
2294    clobbered in INSN.  If any earlyclobber classes are found, the function
2295    returns TRUE, in all other cases it returns FALSE.  */
2296
2297 static bool
2298 df_urec_check_earlyclobber (rtx insn)
2299 {
2300   int opno;
2301   bool found = false;
2302
2303   extract_insn (insn);
2304
2305   VEC_truncate (int, earlyclobber_regclass, 0);
2306   for (opno = 0; opno < recog_data.n_operands; opno++)
2307     {
2308       char c;
2309       bool amp_p;
2310       int i;
2311       enum reg_class class;
2312       const char *p = recog_data.constraints[opno];
2313
2314       class = NO_REGS;
2315       amp_p = false;
2316       for (;;)
2317         {
2318           c = *p;
2319           switch (c)
2320             {
2321             case '=':  case '+':  case '?':
2322             case '#':  case '!':
2323             case '*':  case '%':
2324             case 'm':  case '<':  case '>':  case 'V':  case 'o':
2325             case 'E':  case 'F':  case 'G':  case 'H':
2326             case 's':  case 'i':  case 'n':
2327             case 'I':  case 'J':  case 'K':  case 'L':
2328             case 'M':  case 'N':  case 'O':  case 'P':
2329             case 'X':
2330             case '0': case '1':  case '2':  case '3':  case '4':
2331             case '5': case '6':  case '7':  case '8':  case '9':
2332               /* These don't say anything we care about.  */
2333               break;
2334
2335             case '&':
2336               amp_p = true;
2337               break;
2338             case '\0':
2339             case ',':
2340               if (amp_p && class != NO_REGS)
2341                 {
2342                   int rc;
2343
2344                   found = true;
2345                   for (i = 0;
2346                        VEC_iterate (int, earlyclobber_regclass, i, rc);
2347                        i++)
2348                     {
2349                       if (rc == (int) class)
2350                         goto found_rc;
2351                     }
2352
2353                   /* We use VEC_quick_push here because
2354                      earlyclobber_regclass holds no more than
2355                      N_REG_CLASSES elements. */
2356                   VEC_quick_push (int, earlyclobber_regclass, (int) class);
2357                 found_rc:
2358                   ;
2359                 }
2360               
2361               amp_p = false;
2362               class = NO_REGS;
2363               break;
2364
2365             case 'r':
2366               class = GENERAL_REGS;
2367               break;
2368
2369             default:
2370               class = REG_CLASS_FROM_CONSTRAINT (c, p);
2371               break;
2372             }
2373           if (c == '\0')
2374             break;
2375           p += CONSTRAINT_LEN (c, p);
2376         }
2377     }
2378
2379   return found;
2380 }
2381
2382 /* The function checks that pseudo-register *X has a class
2383    intersecting with the class of pseudo-register could be early
2384    clobbered in the same insn.
2385
2386    This function is a no-op if earlyclobber_regclass is empty. 
2387
2388    Reload can assign the same hard register to uninitialized
2389    pseudo-register and early clobbered pseudo-register in an insn if
2390    the pseudo-register is used first time in given BB and not lived at
2391    the BB start.  To prevent this we don't change life information for
2392    such pseudo-registers.  */
2393
2394 static int
2395 df_urec_mark_reg_use_for_earlyclobber (rtx *x, void *data)
2396 {
2397   enum reg_class pref_class, alt_class;
2398   int i, regno;
2399   struct df_urec_bb_info *bb_info = (struct df_urec_bb_info*) data;
2400
2401   if (REG_P (*x) && REGNO (*x) >= FIRST_PSEUDO_REGISTER)
2402     {
2403       int rc;
2404
2405       regno = REGNO (*x);
2406       if (bitmap_bit_p (bb_info->kill, regno)
2407           || bitmap_bit_p (bb_info->gen, regno))
2408         return 0;
2409       pref_class = reg_preferred_class (regno);
2410       alt_class = reg_alternate_class (regno);
2411       for (i = 0; VEC_iterate (int, earlyclobber_regclass, i, rc); i++)
2412         {
2413           if (reg_classes_intersect_p (rc, pref_class)
2414               || (rc != NO_REGS
2415                   && reg_classes_intersect_p (rc, alt_class)))
2416             {
2417               bitmap_set_bit (bb_info->earlyclobber, regno);
2418               break;
2419             }
2420         }
2421     }
2422   return 0;
2423 }
2424
2425 /* The function processes all pseudo-registers in *X with the aid of
2426    previous function.  */
2427
2428 static void
2429 df_urec_mark_reg_use_for_earlyclobber_1 (rtx *x, void *data)
2430 {
2431   for_each_rtx (x, df_urec_mark_reg_use_for_earlyclobber, data);
2432 }
2433
2434
2435 /* Compute local uninitialized register info for basic block BB.  */
2436
2437 static void
2438 df_urec_bb_local_compute (struct dataflow *dflow, unsigned int bb_index)
2439 {
2440   struct df *df = dflow->df;
2441   basic_block bb = BASIC_BLOCK (bb_index);
2442   struct df_urec_bb_info *bb_info = df_urec_get_bb_info (dflow, bb_index);
2443   rtx insn;
2444   struct df_ref *def;
2445
2446   for (def = df_get_artificial_defs (df, bb_index); def; def = def->next_ref)
2447     if (DF_REF_FLAGS (def) & DF_REF_AT_TOP)
2448       {
2449         unsigned int regno = DF_REF_REGNO (def);
2450         bitmap_set_bit (bb_info->gen, regno);
2451       }
2452   
2453   FOR_BB_INSNS (bb, insn)
2454     {
2455       if (INSN_P (insn))
2456         {
2457           note_stores (PATTERN (insn), df_urec_mark_reg_change, bb_info);
2458           if (df_urec_check_earlyclobber (insn))
2459             {
2460               struct df_urec_problem_data *problem_data
2461                 = (struct df_urec_problem_data *) dflow->problem_data;
2462               problem_data->earlyclobbers_found = true;
2463               note_uses (&PATTERN (insn), 
2464                          df_urec_mark_reg_use_for_earlyclobber_1, bb_info);
2465             }
2466         }
2467     }
2468
2469   for (def = df_get_artificial_defs (df, bb_index); def; def = def->next_ref)
2470     if ((DF_REF_FLAGS (def) & DF_REF_AT_TOP) == 0)
2471       {
2472         unsigned int regno = DF_REF_REGNO (def);
2473         bitmap_set_bit (bb_info->gen, regno);
2474       }
2475
2476 }
2477
2478
2479 /* Compute local uninitialized register info.  */
2480
2481 static void
2482 df_urec_local_compute (struct dataflow *dflow, 
2483                      bitmap all_blocks ATTRIBUTE_UNUSED,
2484                      bitmap rescan_blocks)
2485 {
2486   unsigned int bb_index;
2487   bitmap_iterator bi;
2488 #ifdef STACK_REGS
2489   int i;
2490   HARD_REG_SET stack_hard_regs, used;
2491   struct df_urec_problem_data *problem_data
2492     = (struct df_urec_problem_data *) dflow->problem_data;
2493   
2494   /* Any register that MAY be allocated to a register stack (like the
2495      387) is treated poorly.  Each such register is marked as being
2496      live everywhere.  This keeps the register allocator and the
2497      subsequent passes from doing anything useful with these values.
2498
2499      FIXME: This seems like an incredibly poor idea.  */
2500
2501   CLEAR_HARD_REG_SET (stack_hard_regs);
2502   for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
2503     SET_HARD_REG_BIT (stack_hard_regs, i);
2504   problem_data->stack_regs = BITMAP_ALLOC (NULL);
2505   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
2506     {
2507       COPY_HARD_REG_SET (used, reg_class_contents[reg_preferred_class (i)]);
2508       IOR_HARD_REG_SET (used, reg_class_contents[reg_alternate_class (i)]);
2509       AND_HARD_REG_SET (used, stack_hard_regs);
2510       if (!hard_reg_set_empty_p (used))
2511         bitmap_set_bit (problem_data->stack_regs, i);
2512     }
2513 #endif
2514
2515   /* We know that earlyclobber_regclass holds no more than
2516     N_REG_CLASSES elements.  See df_urec_check_earlyclobber.  */
2517   earlyclobber_regclass = VEC_alloc (int, heap, N_REG_CLASSES);
2518
2519   EXECUTE_IF_SET_IN_BITMAP (rescan_blocks, 0, bb_index, bi)
2520     {
2521       df_urec_bb_local_compute (dflow, bb_index);
2522     }
2523
2524   VEC_free (int, heap, earlyclobber_regclass);
2525 }
2526
2527
2528 /* Initialize the solution vectors.  */
2529
2530 static void 
2531 df_urec_init (struct dataflow *dflow, bitmap all_blocks)
2532 {
2533   unsigned int bb_index;
2534   bitmap_iterator bi;
2535
2536   EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
2537     {
2538       struct df_urec_bb_info *bb_info = df_urec_get_bb_info (dflow, bb_index);
2539
2540       bitmap_copy (bb_info->out, bb_info->gen);
2541       bitmap_clear (bb_info->in);
2542     }
2543 }
2544
2545
2546 /* Or in the stack regs, hard regs and early clobber regs into the
2547    ur_in sets of all of the blocks.  */
2548
2549 static void
2550 df_urec_local_finalize (struct dataflow *dflow, bitmap all_blocks)
2551 {
2552   struct df *df = dflow->df;
2553   struct dataflow *lr_dflow = df->problems_by_index[DF_LR];
2554   bitmap tmp = BITMAP_ALLOC (NULL);
2555   bitmap_iterator bi;
2556   unsigned int bb_index;
2557   struct df_urec_problem_data *problem_data
2558     = (struct df_urec_problem_data *) dflow->problem_data;
2559
2560   EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
2561     {
2562       struct df_urec_bb_info *bb_info = df_urec_get_bb_info (dflow, bb_index);
2563       struct df_lr_bb_info *bb_lr_info = df_lr_get_bb_info (lr_dflow, bb_index);
2564
2565       if (bb_index != ENTRY_BLOCK && bb_index != EXIT_BLOCK)
2566         {
2567           if (problem_data->earlyclobbers_found)
2568             bitmap_ior_into (bb_info->in, bb_info->earlyclobber);
2569         
2570 #ifdef STACK_REGS
2571           /* We can not use the same stack register for uninitialized
2572              pseudo-register and another living pseudo-register
2573              because if the uninitialized pseudo-register dies,
2574              subsequent pass reg-stack will be confused (it will
2575              believe that the other register dies).  */
2576           bitmap_ior_into (bb_info->in, problem_data->stack_regs);
2577           bitmap_ior_into (bb_info->out, problem_data->stack_regs);
2578 #endif
2579         }
2580
2581       /* No register may reach a location where it is not used.  Thus
2582          we trim the rr result to the places where it is used.  */
2583       bitmap_and_into (bb_info->in, bb_lr_info->in);
2584       bitmap_and_into (bb_info->out, bb_lr_info->out);
2585       
2586 #if 1
2587       /* Hard registers may still stick in the ur_out set, but not
2588          be in the ur_in set, if their only mention was in a call
2589          in this block.  This is because a call kills in the lr
2590          problem but does not kill in the rr problem.  To clean
2591          this up, we execute the transfer function on the lr_in
2592          set and then use that to knock bits out of ur_out.  */
2593       bitmap_ior_and_compl (tmp, bb_info->gen, bb_lr_info->in, 
2594                             bb_info->kill);
2595       bitmap_and_into (bb_info->out, tmp);
2596 #endif
2597     }
2598   
2599 #ifdef STACK_REGS
2600   BITMAP_FREE (problem_data->stack_regs);
2601 #endif
2602   BITMAP_FREE (tmp);
2603 }
2604
2605
2606 /* Confluence function that ignores fake edges.  */
2607
2608 static void
2609 df_urec_confluence_n (struct dataflow *dflow, edge e)
2610 {
2611   bitmap op1 = df_urec_get_bb_info (dflow, e->dest->index)->in;
2612   bitmap op2 = df_urec_get_bb_info (dflow, e->src->index)->out;
2613  
2614   if (e->flags & EDGE_FAKE) 
2615     return;
2616
2617   bitmap_ior_into (op1, op2);
2618
2619
2620
2621 /* Transfer function.  */
2622
2623 static bool
2624 df_urec_transfer_function (struct dataflow *dflow, int bb_index)
2625 {
2626   struct df_urec_bb_info *bb_info = df_urec_get_bb_info (dflow, bb_index);
2627   bitmap in = bb_info->in;
2628   bitmap out = bb_info->out;
2629   bitmap gen = bb_info->gen;
2630   bitmap kill = bb_info->kill;
2631
2632   return bitmap_ior_and_compl (out, gen, in, kill);
2633 }
2634
2635
2636 /* Free all storage associated with the problem.  */
2637
2638 static void
2639 df_urec_free (struct dataflow *dflow)
2640 {
2641   if (dflow->block_info)
2642     {
2643       unsigned int i;
2644       
2645       for (i = 0; i < dflow->block_info_size; i++)
2646         {
2647           struct df_urec_bb_info *bb_info = df_urec_get_bb_info (dflow, i);
2648           if (bb_info)
2649             {
2650               BITMAP_FREE (bb_info->gen);
2651               BITMAP_FREE (bb_info->kill);
2652               BITMAP_FREE (bb_info->in);
2653               BITMAP_FREE (bb_info->out);
2654               BITMAP_FREE (bb_info->earlyclobber);
2655             }
2656         }
2657       
2658       free_alloc_pool (dflow->block_pool);
2659       
2660       dflow->block_info_size = 0;
2661       free (dflow->block_info);
2662       free (dflow->problem_data);
2663     }
2664   free (dflow);
2665 }
2666
2667
2668 /* Debugging info.  */
2669
2670 static void
2671 df_urec_dump (struct dataflow *dflow, FILE *file)
2672 {
2673   basic_block bb;
2674   
2675   if (!dflow->block_info) 
2676     return;
2677
2678   fprintf (file, "Undefined regs:\n");
2679  
2680   FOR_ALL_BB (bb)
2681     {
2682       struct df_urec_bb_info *bb_info = df_urec_get_bb_info (dflow, bb->index);
2683       df_print_bb_index (bb, file);
2684       
2685       if (!bb_info->in)
2686         continue;
2687       
2688       fprintf (file, "  in  \t");
2689       dump_bitmap (file, bb_info->in);
2690       fprintf (file, "  gen \t");
2691       dump_bitmap (file, bb_info->gen);
2692       fprintf (file, "  kill\t");
2693       dump_bitmap (file, bb_info->kill);
2694       fprintf (file, "  ec\t");
2695       dump_bitmap (file, bb_info->earlyclobber);
2696       fprintf (file, "  out \t");
2697       dump_bitmap (file, bb_info->out);
2698     }
2699 }
2700
2701 /* All of the information associated with every instance of the problem.  */
2702
2703 static struct df_problem problem_UREC =
2704 {
2705   DF_UREC,                    /* Problem id.  */
2706   DF_FORWARD,                 /* Direction.  */
2707   df_urec_alloc,              /* Allocate the problem specific data.  */
2708   NULL,                       /* Reset global information.  */
2709   df_urec_free_bb_info,       /* Free basic block info.  */
2710   df_urec_local_compute,      /* Local compute function.  */
2711   df_urec_init,               /* Init the solution specific data.  */
2712   df_iterative_dataflow,      /* Iterative solver.  */
2713   NULL,                       /* Confluence operator 0.  */ 
2714   df_urec_confluence_n,       /* Confluence operator n.  */ 
2715   df_urec_transfer_function,  /* Transfer function.  */
2716   df_urec_local_finalize,     /* Finalize function.  */
2717   df_urec_free,               /* Free all of the problem information.  */
2718   df_urec_dump,               /* Debugging.  */
2719   df_lr_add_problem,          /* Dependent problem.  */
2720   0                           /* Changeable flags.  */
2721 };
2722
2723
2724 /* Create a new DATAFLOW instance and add it to an existing instance
2725    of DF.  The returned structure is what is used to get at the
2726    solution.  */
2727
2728 struct dataflow *
2729 df_urec_add_problem (struct df *df, int flags)
2730 {
2731   return df_add_problem (df, &problem_UREC, flags);
2732 }
2733
2734
2735 \f
2736 /*----------------------------------------------------------------------------
2737    CREATE DEF_USE (DU) and / or USE_DEF (UD) CHAINS
2738
2739    Link either the defs to the uses and / or the uses to the defs.
2740
2741    These problems are set up like the other dataflow problems so that
2742    they nicely fit into the framework.  They are much simpler and only
2743    involve a single traversal of instructions and an examination of
2744    the reaching defs information (the dependent problem).
2745 ----------------------------------------------------------------------------*/
2746
2747 /* Create def-use or use-def chains.  */
2748
2749 static void  
2750 df_chain_alloc (struct dataflow *dflow, 
2751                 bitmap blocks_to_rescan ATTRIBUTE_UNUSED,
2752                 bitmap all_blocks ATTRIBUTE_UNUSED)
2753
2754 {
2755   struct df *df = dflow->df;
2756   unsigned int i;
2757
2758   /* Wholesale destruction of the old chains.  */ 
2759   if (dflow->block_pool)
2760     free_alloc_pool (dflow->block_pool);
2761
2762   dflow->block_pool = create_alloc_pool ("df_chain_chain_block pool", 
2763                                          sizeof (struct df_link), 100);
2764
2765   if (dflow->flags & DF_DU_CHAIN)
2766     {
2767       df_reorganize_refs (&df->def_info);
2768       
2769       /* Clear out the pointers from the refs.  */
2770       for (i = 0; i < DF_DEFS_SIZE (df); i++)
2771         {
2772           struct df_ref *ref = df->def_info.refs[i];
2773           DF_REF_CHAIN (ref) = NULL;
2774         }
2775     }
2776   
2777   if (dflow->flags & DF_UD_CHAIN)
2778     {
2779       df_reorganize_refs (&df->use_info);
2780       for (i = 0; i < DF_USES_SIZE (df); i++)
2781         {
2782           struct df_ref *ref = df->use_info.refs[i];
2783           DF_REF_CHAIN (ref) = NULL;
2784         }
2785     }
2786 }
2787
2788
2789 /* Reset all def_use and use_def chains in INSN.  */
2790
2791 static void 
2792 df_chain_insn_reset (struct dataflow *dflow, rtx insn)
2793 {
2794   struct df *df = dflow->df;
2795   unsigned int uid = INSN_UID (insn);
2796   struct df_insn_info *insn_info = NULL;
2797   struct df_ref *ref;
2798
2799   if (uid < df->insns_size)
2800     insn_info = DF_INSN_UID_GET (df, uid);
2801
2802   if (insn_info)
2803     {
2804       if (dflow->flags & DF_DU_CHAIN)
2805         {
2806           ref = insn_info->defs;
2807           while (ref)
2808             {
2809               ref->chain = NULL;
2810               ref = ref->next_ref;
2811             }
2812         }
2813
2814       if (dflow->flags & DF_UD_CHAIN)
2815         {
2816           ref = insn_info->uses;
2817           while (ref) 
2818             {
2819               ref->chain = NULL;
2820               ref = ref->next_ref;
2821             }
2822         }
2823     }
2824 }
2825
2826
2827 /* Reset all def_use and use_def chains in basic block.  */
2828
2829 static void 
2830 df_chain_bb_reset (struct dataflow *dflow, unsigned int bb_index)
2831 {
2832   struct df *df = dflow->df; 
2833   rtx insn;
2834   basic_block bb = BASIC_BLOCK (bb_index);
2835
2836   /* Some one deleted the basic block out from under us.  */
2837   if (!bb)
2838     return;
2839
2840   FOR_BB_INSNS (bb, insn)
2841     {
2842       if (INSN_P (insn))
2843         {
2844           /* Record defs within INSN.  */
2845           df_chain_insn_reset (dflow, insn);
2846         }
2847     }
2848   
2849   /* Get rid of any chains in artificial uses or defs.  */
2850   if (dflow->flags & DF_DU_CHAIN)
2851     {
2852       struct df_ref *def;
2853       def = df_get_artificial_defs (df, bb_index);
2854       while (def)
2855         {
2856           def->chain = NULL;
2857           def = def->next_ref;
2858         }
2859     }
2860
2861   if (dflow->flags & DF_UD_CHAIN)
2862     {
2863       struct df_ref *use;
2864       use = df_get_artificial_uses (df, bb_index);
2865       while (use)
2866         {
2867           use->chain = NULL;
2868           use = use->next_ref;
2869         }
2870     }
2871 }
2872
2873
2874 /* Reset all of the chains when the set of basic blocks changes.  */
2875
2876
2877 static void
2878 df_chain_reset (struct dataflow *dflow, bitmap blocks_to_clear)
2879 {
2880   bitmap_iterator bi;
2881   unsigned int bb_index;
2882   
2883   EXECUTE_IF_SET_IN_BITMAP (blocks_to_clear, 0, bb_index, bi)
2884     {
2885       df_chain_bb_reset (dflow, bb_index);
2886     }
2887
2888   free_alloc_pool (dflow->block_pool);
2889   dflow->block_pool = NULL;
2890 }
2891
2892
2893 /* Create the chains for a list of USEs.  */
2894
2895 static void
2896 df_chain_create_bb_process_use (struct dataflow *dflow, 
2897                                 bitmap local_rd,
2898                                 struct df_ref *use,
2899                                 enum df_ref_flags top_flag)
2900 {
2901   struct df *df = dflow->df;
2902   bitmap_iterator bi;
2903   unsigned int def_index;
2904   
2905   while (use)
2906     {
2907       /* Do not want to go through this for an uninitialized var.  */
2908       unsigned int uregno = DF_REF_REGNO (use);
2909       int count = DF_REG_DEF_GET (df, uregno)->n_refs;
2910       if (count)
2911         {
2912           if (top_flag == (DF_REF_FLAGS (use) & DF_REF_AT_TOP))
2913             {
2914               unsigned int first_index = DF_REG_DEF_GET (df, uregno)->begin;
2915               unsigned int last_index = first_index + count - 1;
2916               
2917               EXECUTE_IF_SET_IN_BITMAP (local_rd, first_index, def_index, bi)
2918                 {
2919                   struct df_ref *def;
2920                   if (def_index > last_index) 
2921                     break;
2922                   
2923                   def = DF_DEFS_GET (df, def_index);
2924                   if (dflow->flags & DF_DU_CHAIN)
2925                     df_chain_create (dflow, def, use);
2926                   if (dflow->flags & DF_UD_CHAIN)
2927                     df_chain_create (dflow, use, def);
2928                 }
2929             }
2930         }
2931       use = use->next_ref;
2932     }
2933 }
2934
2935 /* Reset the storage pool that the def-use or use-def chains have been
2936    allocated in. We do not need to re adjust the pointers in the refs,
2937    these have already been clean out.*/
2938
2939 /* Create chains from reaching defs bitmaps for basic block BB.  */
2940 static void
2941 df_chain_create_bb (struct dataflow *dflow, 
2942                     struct dataflow *rd_dflow,
2943                     unsigned int bb_index)
2944 {
2945   basic_block bb = BASIC_BLOCK (bb_index);
2946   struct df_rd_bb_info *bb_info = df_rd_get_bb_info (rd_dflow, bb_index);
2947   rtx insn;
2948   bitmap cpy = BITMAP_ALLOC (NULL);
2949   struct df *df = dflow->df;
2950   struct df_ref *def;
2951
2952   bitmap_copy (cpy, bb_info->in);
2953
2954   /* Since we are going forwards, process the artificial uses first
2955      then the artificial defs second.  */
2956
2957 #ifdef EH_USES
2958   /* Create the chains for the artificial uses from the EH_USES at the
2959      beginning of the block.  */
2960   df_chain_create_bb_process_use (dflow, cpy,
2961                                   df_get_artificial_uses (df, bb->index), 
2962                                   DF_REF_AT_TOP);
2963 #endif
2964
2965   for (def = df_get_artificial_defs (df, bb_index); def; def = def->next_ref)
2966     if (DF_REF_FLAGS (def) & DF_REF_AT_TOP)
2967       {
2968         unsigned int dregno = DF_REF_REGNO (def);
2969         if (!(DF_REF_FLAGS (def) & DF_REF_PARTIAL))
2970           bitmap_clear_range (cpy, 
2971                               DF_REG_DEF_GET (df, dregno)->begin, 
2972                               DF_REG_DEF_GET (df, dregno)->n_refs);
2973         bitmap_set_bit (cpy, DF_REF_ID (def));
2974       }
2975   
2976   /* Process the regular instructions next.  */
2977   FOR_BB_INSNS (bb, insn)
2978     {
2979       struct df_ref *def;
2980       unsigned int uid = INSN_UID (insn);
2981
2982       if (!INSN_P (insn))
2983         continue;
2984
2985       /* Now scan the uses and link them up with the defs that remain
2986          in the cpy vector.  */
2987       
2988       df_chain_create_bb_process_use (dflow, cpy,
2989                                      DF_INSN_UID_USES (df, uid), 0);
2990
2991       /* Since we are going forwards, process the defs second.  This
2992          pass only changes the bits in cpy.  */
2993       for (def = DF_INSN_UID_DEFS (df, uid); def; def = def->next_ref)
2994         {
2995           unsigned int dregno = DF_REF_REGNO (def);
2996           if (!(DF_REF_FLAGS (def) & DF_REF_PARTIAL))
2997             bitmap_clear_range (cpy, 
2998                                 DF_REG_DEF_GET (df, dregno)->begin, 
2999                                 DF_REG_DEF_GET (df, dregno)->n_refs);
3000           if (!(DF_REF_FLAGS (def) 
3001                  & (DF_REF_MUST_CLOBBER | DF_REF_MAY_CLOBBER)))
3002             bitmap_set_bit (cpy, DF_REF_ID (def));
3003         }
3004     }
3005
3006   /* Create the chains for the artificial uses of the hard registers
3007      at the end of the block.  */
3008   df_chain_create_bb_process_use (dflow, cpy,
3009                                   df_get_artificial_uses (df, bb->index), 0);
3010 }
3011
3012 /* Create def-use chains from reaching use bitmaps for basic blocks
3013    in BLOCKS.  */
3014
3015 static void
3016 df_chain_finalize (struct dataflow *dflow, bitmap all_blocks)
3017 {
3018   unsigned int bb_index;
3019   bitmap_iterator bi;
3020   struct df *df = dflow->df;
3021   struct dataflow *rd_dflow = df->problems_by_index [DF_RD];
3022   
3023   EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
3024     {
3025       df_chain_create_bb (dflow, rd_dflow, bb_index);
3026     }
3027 }
3028
3029
3030 /* Free all storage associated with the problem.  */
3031
3032 static void
3033 df_chain_free (struct dataflow *dflow)
3034 {
3035   free_alloc_pool (dflow->block_pool);
3036   free (dflow);
3037 }
3038
3039
3040 /* Debugging info.  */
3041
3042 static void
3043 df_chains_dump (struct dataflow *dflow, FILE *file)
3044 {
3045   struct df *df = dflow->df;
3046   unsigned int j;
3047
3048   if (dflow->flags & DF_DU_CHAIN)
3049     {
3050       fprintf (file, "Def-use chains:\n");
3051       for (j = 0; j < df->def_info.bitmap_size; j++)
3052         {
3053           struct df_ref *def = DF_DEFS_GET (df, j);
3054           if (def)
3055             {
3056               fprintf (file, "d%d bb %d luid %d insn %d reg %d ",
3057                        j, DF_REF_BBNO (def),
3058                        DF_REF_INSN (def) ? 
3059                        DF_INSN_LUID (df, DF_REF_INSN (def)):
3060                        -1,
3061                        DF_REF_INSN (def) ? DF_REF_INSN_UID (def) : -1,
3062                        DF_REF_REGNO (def));
3063               if (def->flags & DF_REF_READ_WRITE)
3064                 fprintf (file, "read/write ");
3065               df_chain_dump (DF_REF_CHAIN (def), file);
3066               fprintf (file, "\n");
3067             }
3068         }
3069     }
3070
3071   if (dflow->flags & DF_UD_CHAIN)
3072     {
3073       fprintf (file, "Use-def chains:\n");
3074       for (j = 0; j < df->use_info.bitmap_size; j++)
3075         {
3076           struct df_ref *use = DF_USES_GET (df, j);
3077           if (use)
3078             {
3079               fprintf (file, "u%d bb %d luid %d insn %d reg %d ",
3080                        j, DF_REF_BBNO (use),
3081                        DF_REF_INSN (use) ? 
3082                        DF_INSN_LUID (df, DF_REF_INSN (use))
3083                        : -1,
3084                        DF_REF_INSN (DF_USES_GET (df, j)) ?
3085                        DF_REF_INSN_UID (DF_USES_GET (df,j))
3086                        : -1,
3087                        DF_REF_REGNO (use));
3088               if (use->flags & DF_REF_READ_WRITE)
3089                 fprintf (file, "read/write ");
3090               if (use->flags & DF_REF_STRIPPED)
3091                 fprintf (file, "stripped ");
3092               if (use->flags & DF_REF_IN_NOTE)
3093                 fprintf (file, "note ");
3094               df_chain_dump (DF_REF_CHAIN (use), file);
3095               fprintf (file, "\n");
3096             }
3097         }
3098     }
3099 }
3100
3101
3102 static struct df_problem problem_CHAIN =
3103 {
3104   DF_CHAIN,                   /* Problem id.  */
3105   DF_NONE,                    /* Direction.  */
3106   df_chain_alloc,             /* Allocate the problem specific data.  */
3107   df_chain_reset,             /* Reset global information.  */
3108   NULL,                       /* Free basic block info.  */
3109   NULL,                       /* Local compute function.  */
3110   NULL,                       /* Init the solution specific data.  */
3111   NULL,                       /* Iterative solver.  */
3112   NULL,                       /* Confluence operator 0.  */ 
3113   NULL,                       /* Confluence operator n.  */ 
3114   NULL,                       /* Transfer function.  */
3115   df_chain_finalize,          /* Finalize function.  */
3116   df_chain_free,              /* Free all of the problem information.  */
3117   df_chains_dump,             /* Debugging.  */
3118   df_rd_add_problem,          /* Dependent problem.  */
3119   0                           /* Changeable flags.  */
3120 };
3121
3122
3123 /* Create a new DATAFLOW instance and add it to an existing instance
3124    of DF.  The returned structure is what is used to get at the
3125    solution.  */
3126
3127 struct dataflow *
3128 df_chain_add_problem (struct df *df, int flags)
3129 {
3130   return df_add_problem (df, &problem_CHAIN, flags);
3131 }
3132
3133
3134 /*----------------------------------------------------------------------------
3135    REGISTER INFORMATION
3136
3137    This pass properly computes REG_DEAD and REG_UNUSED notes.
3138
3139    If the DF_RI_LIFE flag is set the following vectors containing
3140    information about register usage are properly set: REG_N_REFS,
3141    REG_N_DEATHS, REG_N_SETS, REG_LIVE_LENGTH, REG_N_CALLS_CROSSED,
3142    REG_N_THROWING_CALLS_CROSSED and REG_BASIC_BLOCK.
3143
3144    ----------------------------------------------------------------------------*/
3145
3146 #ifdef REG_DEAD_DEBUGGING
3147 static void 
3148 print_note (char *prefix, rtx insn, rtx note)
3149 {
3150   fprintf (stderr, "%s %d ", prefix, INSN_UID (insn));
3151   print_rtl (stderr, note);
3152   fprintf (stderr, "\n");
3153 }
3154 #endif
3155
3156 /* Allocate the lifetime information.  */
3157
3158 static void 
3159 df_ri_alloc (struct dataflow *dflow, 
3160              bitmap blocks_to_rescan ATTRIBUTE_UNUSED,
3161              bitmap all_blocks ATTRIBUTE_UNUSED)
3162 {
3163   int i;
3164   struct df *df = dflow->df;
3165
3166   if (dflow->flags & DF_RI_LIFE)
3167     {
3168       max_regno = max_reg_num ();
3169       allocate_reg_info (max_regno, FALSE, FALSE);
3170       
3171       /* Reset all the data we'll collect.  */
3172       for (i = 0; i < max_regno; i++)
3173         {
3174           REG_N_SETS (i) = DF_REG_DEF_COUNT (df, i);
3175           REG_N_REFS (i) = DF_REG_USE_COUNT (df, i) + REG_N_SETS (i);
3176           REG_N_DEATHS (i) = 0;
3177           REG_N_CALLS_CROSSED (i) = 0;
3178           REG_N_THROWING_CALLS_CROSSED (i) = 0;
3179           REG_LIVE_LENGTH (i) = 0;
3180           REG_FREQ (i) = 0;
3181           REG_BASIC_BLOCK (i) = REG_BLOCK_UNKNOWN;
3182         }
3183     }
3184 }
3185
3186
3187 /* After reg-stack, the x86 floating point stack regs are difficult to
3188    analyze because of all of the pushes, pops and rotations.  Thus, we
3189    just leave the notes alone. */
3190
3191 static inline bool 
3192 df_ignore_stack_reg (int regno ATTRIBUTE_UNUSED)
3193 {
3194 #ifdef STACK_REGS
3195   return (regstack_completed
3196           && IN_RANGE (regno, FIRST_STACK_REG, LAST_STACK_REG));
3197 #else
3198   return false;
3199 #endif
3200 }
3201
3202
3203 /* Remove all of the REG_DEAD or REG_UNUSED notes from INSN.  */
3204
3205 static void
3206 df_kill_notes (rtx insn, int flags)
3207 {
3208   rtx *pprev = &REG_NOTES (insn);
3209   rtx link = *pprev;
3210   
3211   while (link)
3212     {
3213       switch (REG_NOTE_KIND (link))
3214         {
3215         case REG_DEAD:
3216           if (flags & DF_RI_LIFE)
3217             if (df_ignore_stack_reg (REGNO (XEXP (link, 0))))
3218               REG_N_DEATHS (REGNO (XEXP (link, 0)))++;
3219
3220           /* Fallthru */
3221         case REG_UNUSED:
3222           if (!df_ignore_stack_reg (REGNO (XEXP (link, 0))))
3223             {
3224               rtx next = XEXP (link, 1);
3225 #ifdef REG_DEAD_DEBUGGING
3226               print_note ("deleting: ", insn, link);
3227 #endif
3228               free_EXPR_LIST_node (link);
3229               *pprev = link = next;
3230             }
3231           break;
3232           
3233         default:
3234           pprev = &XEXP (link, 1);
3235           link = *pprev;
3236           break;
3237         }
3238     }
3239 }
3240
3241
3242 /* Set the REG_UNUSED notes for the multiword hardreg defs in INSN
3243    based on the bits in LIVE.  Do not generate notes for registers in
3244    artificial uses.  DO_NOT_GEN is updated so that REG_DEAD notes are
3245    not generated if the reg is both read and written by the
3246    instruction.
3247 */
3248
3249 static void
3250 df_set_unused_notes_for_mw (rtx insn, struct df_mw_hardreg *mws,
3251                             bitmap live, bitmap do_not_gen, 
3252                             bitmap artificial_uses, int flags)
3253 {
3254   bool all_dead = true;
3255   struct df_link *regs = mws->regs;
3256   unsigned int regno = DF_REF_REGNO (regs->ref);
3257   
3258 #ifdef REG_DEAD_DEBUGGING
3259   fprintf (stderr, "mw unused looking at %d\n", DF_REF_REGNO (regs->ref));
3260   df_ref_debug (regs->ref, stderr);
3261 #endif
3262   while (regs)
3263     {
3264       unsigned int regno = DF_REF_REGNO (regs->ref);
3265       if ((bitmap_bit_p (live, regno))
3266           || bitmap_bit_p (artificial_uses, regno))
3267         {
3268           all_dead = false;
3269           break;
3270         }
3271       regs = regs->next;
3272     }
3273   
3274   if (all_dead)
3275     {
3276       struct df_link *regs = mws->regs;
3277       rtx note = alloc_EXPR_LIST (REG_UNUSED, *DF_REF_LOC (regs->ref), 
3278                                   REG_NOTES (insn));
3279       REG_NOTES (insn) = note;
3280 #ifdef REG_DEAD_DEBUGGING
3281       print_note ("adding 1: ", insn, note);
3282 #endif
3283       bitmap_set_bit (do_not_gen, regno);
3284       /* Only do this if the value is totally dead.  */
3285       if (flags & DF_RI_LIFE)
3286         {
3287           REG_N_DEATHS (regno) ++;
3288           REG_LIVE_LENGTH (regno)++;
3289         }
3290     }
3291   else
3292     {
3293       struct df_link *regs = mws->regs;
3294       while (regs)
3295         {
3296           struct df_ref *ref = regs->ref;
3297           
3298           regno = DF_REF_REGNO (ref);
3299           if ((!bitmap_bit_p (live, regno))
3300               && (!bitmap_bit_p (artificial_uses, regno)))
3301             {
3302               rtx note = alloc_EXPR_LIST (REG_UNUSED, regno_reg_rtx[regno], 
3303                                           REG_NOTES (insn));
3304               REG_NOTES (insn) = note;
3305 #ifdef REG_DEAD_DEBUGGING
3306               print_note ("adding 2: ", insn, note);
3307 #endif
3308             }
3309           bitmap_set_bit (do_not_gen, regno);
3310           regs = regs->next;
3311         }
3312     }
3313 }
3314
3315
3316 /* Set the REG_DEAD notes for the multiword hardreg use in INSN based
3317    on the bits in LIVE.  DO_NOT_GEN is used to keep REG_DEAD notes
3318    from being set if the instruction both reads and writes the
3319    register.  */
3320
3321 static void
3322 df_set_dead_notes_for_mw (rtx insn, struct df_mw_hardreg *mws,
3323                           bitmap live, bitmap do_not_gen,
3324                           bitmap artificial_uses, int flags)
3325 {
3326   bool all_dead = true;
3327   struct df_link *regs = mws->regs;
3328   unsigned int regno = DF_REF_REGNO (regs->ref);
3329   
3330 #ifdef REG_DEAD_DEBUGGING
3331   fprintf (stderr, "mw looking at %d\n", DF_REF_REGNO (regs->ref));
3332   df_ref_debug (regs->ref, stderr);
3333 #endif
3334   while (regs)
3335     {
3336       unsigned int regno = DF_REF_REGNO (regs->ref);
3337       if ((bitmap_bit_p (live, regno))
3338           || bitmap_bit_p (artificial_uses, regno))
3339         {
3340           all_dead = false;
3341           break;
3342         }
3343       regs = regs->next;
3344     }
3345   
3346   if (all_dead)
3347     {
3348       if (!bitmap_bit_p (do_not_gen, regno))
3349         {
3350           /* Add a dead note for the entire multi word register.  */
3351           struct df_link *regs = mws->regs;
3352           rtx note = alloc_EXPR_LIST (REG_DEAD, *DF_REF_LOC (regs->ref), 
3353                                       REG_NOTES (insn));
3354           REG_NOTES (insn) = note;
3355 #ifdef REG_DEAD_DEBUGGING
3356           print_note ("adding 1: ", insn, note);
3357 #endif
3358
3359           if (flags & DF_RI_LIFE)
3360             {
3361               struct df_link *regs = mws->regs;
3362               while (regs)
3363                 {
3364                   struct df_ref *ref = regs->ref;
3365                   regno = DF_REF_REGNO (ref);
3366                   REG_N_DEATHS (regno)++;
3367                   regs = regs->next;
3368                 }
3369             }
3370         }
3371     }
3372   else
3373     {
3374       struct df_link *regs = mws->regs;
3375       while (regs)
3376         {
3377           struct df_ref *ref = regs->ref;
3378
3379           regno = DF_REF_REGNO (ref);
3380           if ((!bitmap_bit_p (live, regno))
3381               && (!bitmap_bit_p (artificial_uses, regno))
3382               && (!bitmap_bit_p (do_not_gen, regno)))
3383             {
3384               rtx note = alloc_EXPR_LIST (REG_DEAD, regno_reg_rtx[regno], 
3385                                           REG_NOTES (insn));
3386               REG_NOTES (insn) = note;
3387               if (flags & DF_RI_LIFE)
3388                 REG_N_DEATHS (regno)++;
3389 #ifdef REG_DEAD_DEBUGGING
3390               print_note ("adding 2: ", insn, note);
3391 #endif
3392             }
3393
3394           regs = regs->next;
3395         }
3396     }
3397 }
3398
3399
3400 /* Create a REG_UNUSED note if necessary for DEF in INSN updating LIVE
3401    and DO_NOT_GEN.  Do not generate notes for registers in artificial
3402    uses.  */
3403
3404 static void
3405 df_create_unused_note (basic_block bb, rtx insn, struct df_ref *def, 
3406                        bitmap live, bitmap do_not_gen, bitmap artificial_uses, 
3407                        bitmap local_live, bitmap local_processed, 
3408                        int flags, int luid)
3409 {
3410   unsigned int dregno = DF_REF_REGNO (def);
3411   
3412 #ifdef REG_DEAD_DEBUGGING
3413   fprintf (stderr, "  regular looking at def ");
3414   df_ref_debug (def, stderr);
3415 #endif
3416
3417   if (bitmap_bit_p (live, dregno))
3418     {
3419       if (flags & DF_RI_LIFE)
3420         {
3421           /* If we have seen this regno, then it has already been
3422              processed correctly with the per insn increment.  If we
3423              have not seen it we need to add the length from here to
3424              the end of the block to the live length.  */
3425           if (bitmap_bit_p (local_processed, dregno))
3426             {
3427               if (!(DF_REF_FLAGS (def) & DF_REF_PARTIAL))
3428                 bitmap_clear_bit (local_live, dregno);
3429             }
3430           else
3431             {
3432               bitmap_set_bit (local_processed, dregno);
3433               REG_LIVE_LENGTH (dregno) += luid;
3434             }
3435         }
3436     }
3437   else if ((!(DF_REF_FLAGS (def) & DF_REF_MW_HARDREG))
3438             && (!bitmap_bit_p (artificial_uses, dregno)) 
3439             && (!df_ignore_stack_reg (dregno)))
3440     {
3441       rtx reg = GET_CODE (*DF_REF_LOC (def)) == SUBREG ?
3442         SUBREG_REG (*DF_REF_LOC (def)) : *DF_REF_LOC (def);
3443       rtx note = alloc_EXPR_LIST (REG_UNUSED, reg, REG_NOTES (insn));
3444       REG_NOTES (insn) = note;
3445 #ifdef REG_DEAD_DEBUGGING
3446       print_note ("adding 3: ", insn, note);
3447 #endif
3448       if (flags & DF_RI_LIFE)
3449         {
3450           REG_N_DEATHS (dregno) ++;
3451           REG_LIVE_LENGTH (dregno)++;
3452         }
3453     }
3454   
3455   if ((flags & DF_RI_LIFE) && (dregno >= FIRST_PSEUDO_REGISTER))
3456     {
3457       REG_FREQ (dregno) += REG_FREQ_FROM_BB (bb);
3458       if (REG_BASIC_BLOCK (dregno) == REG_BLOCK_UNKNOWN)
3459         REG_BASIC_BLOCK (dregno) = bb->index;
3460       else if (REG_BASIC_BLOCK (dregno) != bb->index)
3461         REG_BASIC_BLOCK (dregno) = REG_BLOCK_GLOBAL;
3462     }
3463
3464   if (!(DF_REF_FLAGS (def) & (DF_REF_MUST_CLOBBER + DF_REF_MAY_CLOBBER)))
3465     bitmap_set_bit (do_not_gen, dregno);
3466   
3467   /* Kill this register if it is not a subreg store.  */
3468   if (!(DF_REF_FLAGS (def) & DF_REF_PARTIAL))
3469     bitmap_clear_bit (live, dregno);
3470 }
3471
3472
3473 /* Recompute the REG_DEAD and REG_UNUSED notes and compute register
3474    info: lifetime, bb, and number of defs and uses for basic block
3475    BB.  The three bitvectors are scratch regs used here.  */
3476
3477 static void
3478 df_ri_bb_compute (struct dataflow *dflow, unsigned int bb_index, 
3479                   bitmap live, bitmap do_not_gen, bitmap artificial_uses,
3480                   bitmap local_live, bitmap local_processed, bitmap setjumps_crossed)
3481 {
3482   struct df *df = dflow->df;
3483   basic_block bb = BASIC_BLOCK (bb_index);
3484   rtx insn;
3485   struct df_ref *def;
3486   struct df_ref *use;
3487   int luid = 0;
3488
3489   bitmap_copy (live, df_get_live_out (df, bb));
3490   bitmap_clear (artificial_uses);
3491
3492   if (dflow->flags & DF_RI_LIFE)
3493     {
3494       /* Process the regs live at the end of the block.  Mark them as
3495          not local to any one basic block.  */
3496       bitmap_iterator bi;
3497       unsigned int regno;
3498       EXECUTE_IF_SET_IN_BITMAP (live, 0, regno, bi)
3499         REG_BASIC_BLOCK (regno) = REG_BLOCK_GLOBAL;
3500     }
3501
3502   /* Process the artificial defs and uses at the bottom of the block
3503      to begin processing.  */
3504   for (def = df_get_artificial_defs (df, bb_index); def; def = def->next_ref)
3505     if ((DF_REF_FLAGS (def) & DF_REF_AT_TOP) == 0)
3506       bitmap_clear_bit (live, DF_REF_REGNO (def));
3507
3508   for (use = df_get_artificial_uses (df, bb_index); use; use = use->next_ref)
3509     if ((DF_REF_FLAGS (use) & DF_REF_AT_TOP) == 0)
3510       {
3511         unsigned int regno = DF_REF_REGNO (use);
3512         bitmap_set_bit (live, regno);
3513
3514         /* Notes are not generated for any of the artificial registers
3515            at the bottom of the block.  */
3516         bitmap_set_bit (artificial_uses, regno);
3517       }
3518   
3519   FOR_BB_INSNS_REVERSE (bb, insn)
3520     {
3521       unsigned int uid = INSN_UID (insn);
3522       unsigned int regno;
3523       bitmap_iterator bi;
3524       struct df_mw_hardreg *mws;
3525       
3526       if (!INSN_P (insn))
3527         continue;
3528
3529       if (dflow->flags & DF_RI_LIFE)
3530         {
3531           /* Increment the live_length for all of the registers that
3532              are are referenced in this block and live at this
3533              particular point.  */
3534           bitmap_iterator bi;
3535           unsigned int regno;
3536           EXECUTE_IF_SET_IN_BITMAP (local_live, 0, regno, bi)
3537             {
3538               REG_LIVE_LENGTH (regno)++;
3539             }
3540           luid++;
3541         }
3542
3543       bitmap_clear (do_not_gen);
3544       df_kill_notes (insn, dflow->flags);
3545
3546       /* Process the defs.  */
3547       if (CALL_P (insn))
3548         {
3549           if (dflow->flags & DF_RI_LIFE)
3550             {
3551               bool can_throw = can_throw_internal (insn); 
3552               bool set_jump = (find_reg_note (insn, REG_SETJMP, NULL) != NULL);
3553               EXECUTE_IF_SET_IN_BITMAP (live, 0, regno, bi)
3554                 {
3555                   REG_N_CALLS_CROSSED (regno)++;
3556                   if (can_throw)
3557                     REG_N_THROWING_CALLS_CROSSED (regno)++;
3558
3559                   /* We have a problem with any pseudoreg that lives
3560                      across the setjmp.  ANSI says that if a user
3561                      variable does not change in value between the
3562                      setjmp and the longjmp, then the longjmp
3563                      preserves it.  This includes longjmp from a place
3564                      where the pseudo appears dead.  (In principle,
3565                      the value still exists if it is in scope.)  If
3566                      the pseudo goes in a hard reg, some other value
3567                      may occupy that hard reg where this pseudo is
3568                      dead, thus clobbering the pseudo.  Conclusion:
3569                      such a pseudo must not go in a hard reg.  */
3570                   if (set_jump && regno >= FIRST_PSEUDO_REGISTER)
3571                     bitmap_set_bit (setjumps_crossed, regno);
3572                 }
3573             }
3574           
3575           /* We only care about real sets for calls.  Clobbers only
3576              may clobber and cannot be depended on.  */
3577           for (mws = DF_INSN_UID_MWS (df, uid); mws; mws = mws->next)
3578             {
3579               if ((mws->type == DF_REF_REG_DEF) 
3580                   && !df_ignore_stack_reg (REGNO (mws->mw_reg)))
3581                 df_set_unused_notes_for_mw (insn, mws, live, do_not_gen, 
3582                                             artificial_uses, dflow->flags);
3583             }
3584
3585           /* All of the defs except the return value are some sort of
3586              clobber.  This code is for the return.  */
3587           for (def = DF_INSN_UID_DEFS (df, uid); def; def = def->next_ref)
3588             if (!(DF_REF_FLAGS (def) & (DF_REF_MUST_CLOBBER | DF_REF_MAY_CLOBBER)))
3589               df_create_unused_note (bb, insn, def, live, do_not_gen, 
3590                                      artificial_uses, local_live, 
3591                                      local_processed, dflow->flags, luid);
3592
3593         }
3594       else
3595         {
3596           /* Regular insn.  */
3597           for (mws = DF_INSN_UID_MWS (df, uid); mws; mws = mws->next)
3598             {
3599               if (mws->type == DF_REF_REG_DEF)
3600                 df_set_unused_notes_for_mw (insn, mws, live, do_not_gen, 
3601                                             artificial_uses, dflow->flags);
3602             }
3603
3604           for (def = DF_INSN_UID_DEFS (df, uid); def; def = def->next_ref)
3605             df_create_unused_note (bb, insn, def, live, do_not_gen, 
3606                                    artificial_uses, local_live, 
3607                                    local_processed, dflow->flags, luid);
3608         }
3609       
3610       /* Process the uses.  */
3611       for (mws = DF_INSN_UID_MWS (df, uid); mws; mws = mws->next)
3612         {
3613           if ((mws->type != DF_REF_REG_DEF)  
3614               && !df_ignore_stack_reg (REGNO (mws->mw_reg)))
3615             df_set_dead_notes_for_mw (insn, mws, live, do_not_gen,
3616                                       artificial_uses, dflow->flags);
3617         }
3618
3619       for (use = DF_INSN_UID_USES (df, uid); use; use = use->next_ref)
3620         {
3621           unsigned int uregno = DF_REF_REGNO (use);
3622
3623           if ((dflow->flags & DF_RI_LIFE) && (uregno >= FIRST_PSEUDO_REGISTER))
3624             {
3625               REG_FREQ (uregno) += REG_FREQ_FROM_BB (bb);
3626               if (REG_BASIC_BLOCK (uregno) == REG_BLOCK_UNKNOWN)
3627                 REG_BASIC_BLOCK (uregno) = bb->index;
3628               else if (REG_BASIC_BLOCK (uregno) != bb->index)
3629                 REG_BASIC_BLOCK (uregno) = REG_BLOCK_GLOBAL;
3630             }
3631           
3632 #ifdef REG_DEAD_DEBUGGING
3633           fprintf (stderr, "  regular looking at use ");
3634           df_ref_debug (use, stderr);
3635 #endif
3636           if (!bitmap_bit_p (live, uregno))
3637             {
3638               if ( (!(DF_REF_FLAGS (use) & DF_REF_MW_HARDREG))
3639                    && (!bitmap_bit_p (do_not_gen, uregno))
3640                    && (!bitmap_bit_p (artificial_uses, uregno))
3641                    && (!(DF_REF_FLAGS (use) & DF_REF_READ_WRITE))
3642                    && (!df_ignore_stack_reg (uregno)))
3643                 {
3644                   rtx reg = GET_CODE (*DF_REF_LOC (use)) == SUBREG ?
3645                     SUBREG_REG (*DF_REF_LOC (use)) : *DF_REF_LOC (use);
3646                   rtx note = alloc_EXPR_LIST (REG_DEAD, reg, REG_NOTES (insn));
3647                   REG_NOTES (insn) = note;
3648                   if (dflow->flags & DF_RI_LIFE)
3649                     REG_N_DEATHS (uregno)++;
3650
3651 #ifdef REG_DEAD_DEBUGGING
3652                   print_note ("adding 4: ", insn, note);
3653 #endif
3654                 }
3655               /* This register is now live.  */
3656               bitmap_set_bit (live, uregno);
3657
3658               if (dflow->flags & DF_RI_LIFE)
3659                 {
3660                   /* If we have seen this regno, then it has already
3661                      been processed correctly with the per insn
3662                      increment.  If we have not seen it we set the bit
3663                      so that begins to get processed locally.  Note
3664                      that we don't even get here if the variable was
3665                      live at the end of the block since just a ref
3666                      inside the block does not effect the
3667                      calculations.  */
3668                   REG_LIVE_LENGTH (uregno) ++;
3669                   bitmap_set_bit (local_live, uregno);
3670                   bitmap_set_bit (local_processed, uregno);
3671                 }
3672             }
3673         }
3674     }
3675   
3676   if (dflow->flags & DF_RI_LIFE)
3677     {
3678       /* Add the length of the block to all of the registers that were
3679          not referenced, but still live in this block.  */
3680       bitmap_iterator bi;
3681       unsigned int regno;
3682       bitmap_and_compl_into (live, local_processed);
3683       EXECUTE_IF_SET_IN_BITMAP (live, 0, regno, bi)
3684         {
3685           REG_LIVE_LENGTH (regno) += luid;
3686         }
3687       bitmap_clear (local_processed);
3688       bitmap_clear (local_live);
3689     }
3690 }
3691
3692
3693 /* Compute register info: lifetime, bb, and number of defs and uses.  */
3694 static void
3695 df_ri_compute (struct dataflow *dflow, bitmap all_blocks ATTRIBUTE_UNUSED, 
3696                bitmap blocks_to_scan)
3697 {
3698   unsigned int bb_index;
3699   bitmap_iterator bi;
3700   bitmap live = BITMAP_ALLOC (NULL);
3701   bitmap do_not_gen = BITMAP_ALLOC (NULL);
3702   bitmap artificial_uses = BITMAP_ALLOC (NULL);
3703   bitmap local_live = NULL;
3704   bitmap local_processed = NULL;
3705   bitmap setjumps_crossed = NULL;
3706
3707   if (dflow->flags & DF_RI_LIFE)
3708     {
3709       local_live = BITMAP_ALLOC (NULL);
3710       local_processed = BITMAP_ALLOC (NULL);
3711       setjumps_crossed = BITMAP_ALLOC (NULL);
3712     }
3713
3714
3715 #ifdef REG_DEAD_DEBUGGING
3716   df_lr_dump (dflow->df->problems_by_index [DF_LR], stderr);
3717   print_rtl_with_bb (stderr, get_insns());
3718 #endif
3719
3720   EXECUTE_IF_SET_IN_BITMAP (blocks_to_scan, 0, bb_index, bi)
3721   {
3722     df_ri_bb_compute (dflow, bb_index, live, do_not_gen, artificial_uses,
3723                       local_live, local_processed, setjumps_crossed);
3724   }
3725
3726   BITMAP_FREE (live);
3727   BITMAP_FREE (do_not_gen);
3728   BITMAP_FREE (artificial_uses);
3729   if (dflow->flags & DF_RI_LIFE)
3730     {
3731       bitmap_iterator bi;
3732       unsigned int regno;
3733       /* See the setjump comment in df_ri_bb_compute.  */
3734       EXECUTE_IF_SET_IN_BITMAP (setjumps_crossed, 0, regno, bi)
3735         {
3736           REG_BASIC_BLOCK (regno) = REG_BLOCK_UNKNOWN;
3737           REG_LIVE_LENGTH (regno) = -1;
3738         }         
3739
3740       BITMAP_FREE (local_live);
3741       BITMAP_FREE (local_processed);
3742       BITMAP_FREE (setjumps_crossed);
3743     }
3744 }
3745
3746
3747 /* Free all storage associated with the problem.  */
3748
3749 static void
3750 df_ri_free (struct dataflow *dflow)
3751 {
3752   free (dflow->problem_data);
3753   free (dflow);
3754 }
3755
3756
3757 /* Debugging info.  */
3758
3759 static void
3760 df_ri_dump (struct dataflow *dflow, FILE *file)
3761 {
3762   print_rtl_with_bb (file, get_insns ());
3763
3764   if (dflow->flags & DF_RI_LIFE)
3765     {
3766       fprintf (file, "Register info:\n");
3767       dump_flow_info (file, -1);
3768     }
3769 }
3770
3771 /* All of the information associated every instance of the problem.  */
3772
3773 static struct df_problem problem_RI =
3774 {
3775   DF_RI,                      /* Problem id.  */
3776   DF_NONE,                    /* Direction.  */
3777   df_ri_alloc,                /* Allocate the problem specific data.  */
3778   NULL,                       /* Reset global information.  */
3779   NULL,                       /* Free basic block info.  */
3780   df_ri_compute,              /* Local compute function.  */
3781   NULL,                       /* Init the solution specific data.  */
3782   NULL,                       /* Iterative solver.  */
3783   NULL,                       /* Confluence operator 0.  */ 
3784   NULL,                       /* Confluence operator n.  */ 
3785   NULL,                       /* Transfer function.  */
3786   NULL,                       /* Finalize function.  */
3787   df_ri_free,                 /* Free all of the problem information.  */
3788   df_ri_dump,                 /* Debugging.  */
3789
3790   /* Technically this is only dependent on the live registers problem
3791      but it will produce information if built one of uninitialized
3792      register problems (UR, UREC) is also run.  */
3793   df_lr_add_problem,          /* Dependent problem.  */
3794   0                           /* Changeable flags.  */
3795 };
3796
3797
3798 /* Create a new DATAFLOW instance and add it to an existing instance
3799    of DF.  The returned structure is what is used to get at the
3800    solution.  */
3801
3802 struct dataflow * 
3803 df_ri_add_problem (struct df *df, int flags)
3804 {
3805   return df_add_problem (df, &problem_RI, flags);
3806 }