OSDN Git Service

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