xref: /openbsd-src/gnu/usr.bin/gcc/gcc/cfg.c (revision c87b03e512fc05ed6e0222f6fb0ae86264b1d05b)
1*c87b03e5Sespie /* Control flow graph manipulation code for GNU compiler.
2*c87b03e5Sespie    Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3*c87b03e5Sespie    1999, 2000, 2001, 2002 Free Software Foundation, Inc.
4*c87b03e5Sespie 
5*c87b03e5Sespie This file is part of GCC.
6*c87b03e5Sespie 
7*c87b03e5Sespie GCC is free software; you can redistribute it and/or modify it under
8*c87b03e5Sespie the terms of the GNU General Public License as published by the Free
9*c87b03e5Sespie Software Foundation; either version 2, or (at your option) any later
10*c87b03e5Sespie version.
11*c87b03e5Sespie 
12*c87b03e5Sespie GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13*c87b03e5Sespie WARRANTY; without even the implied warranty of MERCHANTABILITY or
14*c87b03e5Sespie FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15*c87b03e5Sespie for more details.
16*c87b03e5Sespie 
17*c87b03e5Sespie You should have received a copy of the GNU General Public License
18*c87b03e5Sespie along with GCC; see the file COPYING.  If not, write to the Free
19*c87b03e5Sespie Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20*c87b03e5Sespie 02111-1307, USA.  */
21*c87b03e5Sespie 
22*c87b03e5Sespie /* This file contains low level functions to manipulate the CFG and
23*c87b03e5Sespie    analyze it.  All other modules should not transform the datastructure
24*c87b03e5Sespie    directly and use abstraction instead.  The file is supposed to be
25*c87b03e5Sespie    ordered bottom-up and should not contain any code dependent on a
26*c87b03e5Sespie    particular intermediate language (RTL or trees).
27*c87b03e5Sespie 
28*c87b03e5Sespie    Available functionality:
29*c87b03e5Sespie      - Initialization/deallocation
30*c87b03e5Sespie 	 init_flow, clear_edges
31*c87b03e5Sespie      - Low level basic block manipulation
32*c87b03e5Sespie 	 alloc_block, expunge_block
33*c87b03e5Sespie      - Edge manipulation
34*c87b03e5Sespie 	 make_edge, make_single_succ_edge, cached_make_edge, remove_edge
35*c87b03e5Sespie 	 - Low level edge redirection (without updating instruction chain)
36*c87b03e5Sespie 	     redirect_edge_succ, redirect_edge_succ_nodup, redirect_edge_pred
37*c87b03e5Sespie      - Dumping and debugging
38*c87b03e5Sespie 	 dump_flow_info, debug_flow_info, dump_edge_info
39*c87b03e5Sespie      - Allocation of AUX fields for basic blocks
40*c87b03e5Sespie 	 alloc_aux_for_blocks, free_aux_for_blocks, alloc_aux_for_block
41*c87b03e5Sespie      - clear_bb_flags
42*c87b03e5Sespie  */
43*c87b03e5Sespie 
44*c87b03e5Sespie #include "config.h"
45*c87b03e5Sespie #include "system.h"
46*c87b03e5Sespie #include "tree.h"
47*c87b03e5Sespie #include "rtl.h"
48*c87b03e5Sespie #include "hard-reg-set.h"
49*c87b03e5Sespie #include "basic-block.h"
50*c87b03e5Sespie #include "regs.h"
51*c87b03e5Sespie #include "flags.h"
52*c87b03e5Sespie #include "output.h"
53*c87b03e5Sespie #include "function.h"
54*c87b03e5Sespie #include "except.h"
55*c87b03e5Sespie #include "toplev.h"
56*c87b03e5Sespie #include "tm_p.h"
57*c87b03e5Sespie #include "obstack.h"
58*c87b03e5Sespie 
59*c87b03e5Sespie /* The obstack on which the flow graph components are allocated.  */
60*c87b03e5Sespie 
61*c87b03e5Sespie struct obstack flow_obstack;
62*c87b03e5Sespie static char *flow_firstobj;
63*c87b03e5Sespie 
64*c87b03e5Sespie /* Number of basic blocks in the current function.  */
65*c87b03e5Sespie 
66*c87b03e5Sespie int n_basic_blocks;
67*c87b03e5Sespie 
68*c87b03e5Sespie /* First free basic block number.  */
69*c87b03e5Sespie 
70*c87b03e5Sespie int last_basic_block;
71*c87b03e5Sespie 
72*c87b03e5Sespie /* Number of edges in the current function.  */
73*c87b03e5Sespie 
74*c87b03e5Sespie int n_edges;
75*c87b03e5Sespie 
76*c87b03e5Sespie /* First edge in the deleted edges chain.  */
77*c87b03e5Sespie 
78*c87b03e5Sespie edge first_deleted_edge;
79*c87b03e5Sespie static basic_block first_deleted_block;
80*c87b03e5Sespie 
81*c87b03e5Sespie /* The basic block array.  */
82*c87b03e5Sespie 
83*c87b03e5Sespie varray_type basic_block_info;
84*c87b03e5Sespie 
85*c87b03e5Sespie /* The special entry and exit blocks.  */
86*c87b03e5Sespie 
87*c87b03e5Sespie struct basic_block_def entry_exit_blocks[2]
88*c87b03e5Sespie = {{NULL,			/* head */
89*c87b03e5Sespie     NULL,			/* end */
90*c87b03e5Sespie     NULL,			/* head_tree */
91*c87b03e5Sespie     NULL,			/* end_tree */
92*c87b03e5Sespie     NULL,			/* pred */
93*c87b03e5Sespie     NULL,			/* succ */
94*c87b03e5Sespie     NULL,			/* local_set */
95*c87b03e5Sespie     NULL,			/* cond_local_set */
96*c87b03e5Sespie     NULL,			/* global_live_at_start */
97*c87b03e5Sespie     NULL,			/* global_live_at_end */
98*c87b03e5Sespie     NULL,			/* aux */
99*c87b03e5Sespie     ENTRY_BLOCK,		/* index */
100*c87b03e5Sespie     NULL,			/* prev_bb */
101*c87b03e5Sespie     EXIT_BLOCK_PTR,		/* next_bb */
102*c87b03e5Sespie     0,				/* loop_depth */
103*c87b03e5Sespie     NULL,                       /* loop_father */
104*c87b03e5Sespie     0,				/* count */
105*c87b03e5Sespie     0,				/* frequency */
106*c87b03e5Sespie     0				/* flags */
107*c87b03e5Sespie   },
108*c87b03e5Sespie   {
109*c87b03e5Sespie     NULL,			/* head */
110*c87b03e5Sespie     NULL,			/* end */
111*c87b03e5Sespie     NULL,			/* head_tree */
112*c87b03e5Sespie     NULL,			/* end_tree */
113*c87b03e5Sespie     NULL,			/* pred */
114*c87b03e5Sespie     NULL,			/* succ */
115*c87b03e5Sespie     NULL,			/* local_set */
116*c87b03e5Sespie     NULL,			/* cond_local_set */
117*c87b03e5Sespie     NULL,			/* global_live_at_start */
118*c87b03e5Sespie     NULL,			/* global_live_at_end */
119*c87b03e5Sespie     NULL,			/* aux */
120*c87b03e5Sespie     EXIT_BLOCK,			/* index */
121*c87b03e5Sespie     ENTRY_BLOCK_PTR,		/* prev_bb */
122*c87b03e5Sespie     NULL,			/* next_bb */
123*c87b03e5Sespie     0,				/* loop_depth */
124*c87b03e5Sespie     NULL,                       /* loop_father */
125*c87b03e5Sespie     0,				/* count */
126*c87b03e5Sespie     0,				/* frequency */
127*c87b03e5Sespie     0				/* flags */
128*c87b03e5Sespie   }
129*c87b03e5Sespie };
130*c87b03e5Sespie 
131*c87b03e5Sespie void debug_flow_info			PARAMS ((void));
132*c87b03e5Sespie static void free_edge			PARAMS ((edge));
133*c87b03e5Sespie 
134*c87b03e5Sespie /* Called once at initialization time.  */
135*c87b03e5Sespie 
136*c87b03e5Sespie void
init_flow()137*c87b03e5Sespie init_flow ()
138*c87b03e5Sespie {
139*c87b03e5Sespie   static int initialized;
140*c87b03e5Sespie 
141*c87b03e5Sespie   first_deleted_edge = 0;
142*c87b03e5Sespie   first_deleted_block = 0;
143*c87b03e5Sespie   n_edges = 0;
144*c87b03e5Sespie 
145*c87b03e5Sespie   if (!initialized)
146*c87b03e5Sespie     {
147*c87b03e5Sespie       gcc_obstack_init (&flow_obstack);
148*c87b03e5Sespie       flow_firstobj = (char *) obstack_alloc (&flow_obstack, 0);
149*c87b03e5Sespie       initialized = 1;
150*c87b03e5Sespie     }
151*c87b03e5Sespie   else
152*c87b03e5Sespie     {
153*c87b03e5Sespie       obstack_free (&flow_obstack, flow_firstobj);
154*c87b03e5Sespie       flow_firstobj = (char *) obstack_alloc (&flow_obstack, 0);
155*c87b03e5Sespie     }
156*c87b03e5Sespie }
157*c87b03e5Sespie 
158*c87b03e5Sespie /* Helper function for remove_edge and clear_edges.  Frees edge structure
159*c87b03e5Sespie    without actually unlinking it from the pred/succ lists.  */
160*c87b03e5Sespie 
161*c87b03e5Sespie static void
free_edge(e)162*c87b03e5Sespie free_edge (e)
163*c87b03e5Sespie      edge e;
164*c87b03e5Sespie {
165*c87b03e5Sespie   n_edges--;
166*c87b03e5Sespie   memset (e, 0, sizeof *e);
167*c87b03e5Sespie   e->succ_next = first_deleted_edge;
168*c87b03e5Sespie   first_deleted_edge = e;
169*c87b03e5Sespie }
170*c87b03e5Sespie 
171*c87b03e5Sespie /* Free the memory associated with the edge structures.  */
172*c87b03e5Sespie 
173*c87b03e5Sespie void
clear_edges()174*c87b03e5Sespie clear_edges ()
175*c87b03e5Sespie {
176*c87b03e5Sespie   basic_block bb;
177*c87b03e5Sespie   edge e;
178*c87b03e5Sespie 
179*c87b03e5Sespie   FOR_EACH_BB (bb)
180*c87b03e5Sespie     {
181*c87b03e5Sespie       edge e = bb->succ;
182*c87b03e5Sespie 
183*c87b03e5Sespie       while (e)
184*c87b03e5Sespie 	{
185*c87b03e5Sespie 	  edge next = e->succ_next;
186*c87b03e5Sespie 
187*c87b03e5Sespie 	  free_edge (e);
188*c87b03e5Sespie 	  e = next;
189*c87b03e5Sespie 	}
190*c87b03e5Sespie 
191*c87b03e5Sespie       bb->succ = NULL;
192*c87b03e5Sespie       bb->pred = NULL;
193*c87b03e5Sespie     }
194*c87b03e5Sespie 
195*c87b03e5Sespie   e = ENTRY_BLOCK_PTR->succ;
196*c87b03e5Sespie   while (e)
197*c87b03e5Sespie     {
198*c87b03e5Sespie       edge next = e->succ_next;
199*c87b03e5Sespie 
200*c87b03e5Sespie       free_edge (e);
201*c87b03e5Sespie       e = next;
202*c87b03e5Sespie     }
203*c87b03e5Sespie 
204*c87b03e5Sespie   EXIT_BLOCK_PTR->pred = NULL;
205*c87b03e5Sespie   ENTRY_BLOCK_PTR->succ = NULL;
206*c87b03e5Sespie 
207*c87b03e5Sespie   if (n_edges)
208*c87b03e5Sespie     abort ();
209*c87b03e5Sespie }
210*c87b03e5Sespie 
211*c87b03e5Sespie /* Allocate memory for basic_block.  */
212*c87b03e5Sespie 
213*c87b03e5Sespie basic_block
alloc_block()214*c87b03e5Sespie alloc_block ()
215*c87b03e5Sespie {
216*c87b03e5Sespie   basic_block bb;
217*c87b03e5Sespie 
218*c87b03e5Sespie   if (first_deleted_block)
219*c87b03e5Sespie     {
220*c87b03e5Sespie       bb = first_deleted_block;
221*c87b03e5Sespie       first_deleted_block = (basic_block) bb->succ;
222*c87b03e5Sespie       bb->succ = NULL;
223*c87b03e5Sespie     }
224*c87b03e5Sespie   else
225*c87b03e5Sespie     {
226*c87b03e5Sespie       bb = (basic_block) obstack_alloc (&flow_obstack, sizeof *bb);
227*c87b03e5Sespie       memset (bb, 0, sizeof *bb);
228*c87b03e5Sespie     }
229*c87b03e5Sespie   return bb;
230*c87b03e5Sespie }
231*c87b03e5Sespie 
232*c87b03e5Sespie /* Link block B to chain after AFTER.  */
233*c87b03e5Sespie void
link_block(b,after)234*c87b03e5Sespie link_block (b, after)
235*c87b03e5Sespie      basic_block b, after;
236*c87b03e5Sespie {
237*c87b03e5Sespie   b->next_bb = after->next_bb;
238*c87b03e5Sespie   b->prev_bb = after;
239*c87b03e5Sespie   after->next_bb = b;
240*c87b03e5Sespie   b->next_bb->prev_bb = b;
241*c87b03e5Sespie }
242*c87b03e5Sespie 
243*c87b03e5Sespie /* Unlink block B from chain.  */
244*c87b03e5Sespie void
unlink_block(b)245*c87b03e5Sespie unlink_block (b)
246*c87b03e5Sespie      basic_block b;
247*c87b03e5Sespie {
248*c87b03e5Sespie   b->next_bb->prev_bb = b->prev_bb;
249*c87b03e5Sespie   b->prev_bb->next_bb = b->next_bb;
250*c87b03e5Sespie }
251*c87b03e5Sespie 
252*c87b03e5Sespie /* Sequentially order blocks and compact the arrays.  */
253*c87b03e5Sespie void
compact_blocks()254*c87b03e5Sespie compact_blocks ()
255*c87b03e5Sespie {
256*c87b03e5Sespie   int i;
257*c87b03e5Sespie   basic_block bb;
258*c87b03e5Sespie 
259*c87b03e5Sespie   i = 0;
260*c87b03e5Sespie   FOR_EACH_BB (bb)
261*c87b03e5Sespie     {
262*c87b03e5Sespie       BASIC_BLOCK (i) = bb;
263*c87b03e5Sespie       bb->index = i;
264*c87b03e5Sespie       i++;
265*c87b03e5Sespie     }
266*c87b03e5Sespie 
267*c87b03e5Sespie   if (i != n_basic_blocks)
268*c87b03e5Sespie     abort ();
269*c87b03e5Sespie 
270*c87b03e5Sespie   last_basic_block = n_basic_blocks;
271*c87b03e5Sespie }
272*c87b03e5Sespie 
273*c87b03e5Sespie 
274*c87b03e5Sespie /* Remove block B from the basic block array.  */
275*c87b03e5Sespie 
276*c87b03e5Sespie void
expunge_block(b)277*c87b03e5Sespie expunge_block (b)
278*c87b03e5Sespie      basic_block b;
279*c87b03e5Sespie {
280*c87b03e5Sespie   unlink_block (b);
281*c87b03e5Sespie   BASIC_BLOCK (b->index) = NULL;
282*c87b03e5Sespie   n_basic_blocks--;
283*c87b03e5Sespie 
284*c87b03e5Sespie   /* Invalidate data to make bughunting easier.  */
285*c87b03e5Sespie   memset (b, 0, sizeof *b);
286*c87b03e5Sespie   b->index = -3;
287*c87b03e5Sespie   b->succ = (edge) first_deleted_block;
288*c87b03e5Sespie   first_deleted_block = (basic_block) b;
289*c87b03e5Sespie }
290*c87b03e5Sespie 
291*c87b03e5Sespie /* Create an edge connecting SRC and DEST with flags FLAGS.  Return newly
292*c87b03e5Sespie    created edge.  Use this only if you are sure that this edge can't
293*c87b03e5Sespie    possibly already exist.  */
294*c87b03e5Sespie 
295*c87b03e5Sespie edge
unchecked_make_edge(src,dst,flags)296*c87b03e5Sespie unchecked_make_edge (src, dst, flags)
297*c87b03e5Sespie      basic_block src, dst;
298*c87b03e5Sespie      int flags;
299*c87b03e5Sespie {
300*c87b03e5Sespie   edge e;
301*c87b03e5Sespie 
302*c87b03e5Sespie   if (first_deleted_edge)
303*c87b03e5Sespie     {
304*c87b03e5Sespie       e = first_deleted_edge;
305*c87b03e5Sespie       first_deleted_edge = e->succ_next;
306*c87b03e5Sespie     }
307*c87b03e5Sespie   else
308*c87b03e5Sespie     {
309*c87b03e5Sespie       e = (edge) obstack_alloc (&flow_obstack, sizeof *e);
310*c87b03e5Sespie       memset (e, 0, sizeof *e);
311*c87b03e5Sespie     }
312*c87b03e5Sespie   n_edges++;
313*c87b03e5Sespie 
314*c87b03e5Sespie   e->succ_next = src->succ;
315*c87b03e5Sespie   e->pred_next = dst->pred;
316*c87b03e5Sespie   e->src = src;
317*c87b03e5Sespie   e->dest = dst;
318*c87b03e5Sespie   e->flags = flags;
319*c87b03e5Sespie 
320*c87b03e5Sespie   src->succ = e;
321*c87b03e5Sespie   dst->pred = e;
322*c87b03e5Sespie 
323*c87b03e5Sespie   return e;
324*c87b03e5Sespie }
325*c87b03e5Sespie /* Create an edge connecting SRC and DST with FLAGS optionally using
326*c87b03e5Sespie    edge cache CACHE.  Return the new edge, NULL if already exist.  */
327*c87b03e5Sespie 
328*c87b03e5Sespie edge
cached_make_edge(edge_cache,src,dst,flags)329*c87b03e5Sespie cached_make_edge (edge_cache, src, dst, flags)
330*c87b03e5Sespie      sbitmap *edge_cache;
331*c87b03e5Sespie      basic_block src, dst;
332*c87b03e5Sespie      int flags;
333*c87b03e5Sespie {
334*c87b03e5Sespie   int use_edge_cache;
335*c87b03e5Sespie   edge e;
336*c87b03e5Sespie 
337*c87b03e5Sespie   /* Don't bother with edge cache for ENTRY or EXIT, if there aren't that
338*c87b03e5Sespie      many edges to them, or we didn't allocate memory for it.  */
339*c87b03e5Sespie   use_edge_cache = (edge_cache
340*c87b03e5Sespie 		    && src != ENTRY_BLOCK_PTR && dst != EXIT_BLOCK_PTR);
341*c87b03e5Sespie 
342*c87b03e5Sespie   /* Make sure we don't add duplicate edges.  */
343*c87b03e5Sespie   switch (use_edge_cache)
344*c87b03e5Sespie     {
345*c87b03e5Sespie     default:
346*c87b03e5Sespie       /* Quick test for non-existence of the edge.  */
347*c87b03e5Sespie       if (! TEST_BIT (edge_cache[src->index], dst->index))
348*c87b03e5Sespie 	break;
349*c87b03e5Sespie 
350*c87b03e5Sespie       /* The edge exists; early exit if no work to do.  */
351*c87b03e5Sespie       if (flags == 0)
352*c87b03e5Sespie 	return NULL;
353*c87b03e5Sespie 
354*c87b03e5Sespie       /* FALLTHRU */
355*c87b03e5Sespie     case 0:
356*c87b03e5Sespie       for (e = src->succ; e; e = e->succ_next)
357*c87b03e5Sespie 	if (e->dest == dst)
358*c87b03e5Sespie 	  {
359*c87b03e5Sespie 	    e->flags |= flags;
360*c87b03e5Sespie 	    return NULL;
361*c87b03e5Sespie 	  }
362*c87b03e5Sespie       break;
363*c87b03e5Sespie     }
364*c87b03e5Sespie 
365*c87b03e5Sespie   e = unchecked_make_edge (src, dst, flags);
366*c87b03e5Sespie 
367*c87b03e5Sespie   if (use_edge_cache)
368*c87b03e5Sespie     SET_BIT (edge_cache[src->index], dst->index);
369*c87b03e5Sespie 
370*c87b03e5Sespie   return e;
371*c87b03e5Sespie }
372*c87b03e5Sespie 
373*c87b03e5Sespie /* Create an edge connecting SRC and DEST with flags FLAGS.  Return newly
374*c87b03e5Sespie    created edge or NULL if already exist.  */
375*c87b03e5Sespie 
376*c87b03e5Sespie edge
make_edge(src,dest,flags)377*c87b03e5Sespie make_edge (src, dest, flags)
378*c87b03e5Sespie      basic_block src, dest;
379*c87b03e5Sespie      int flags;
380*c87b03e5Sespie {
381*c87b03e5Sespie   return cached_make_edge (NULL, src, dest, flags);
382*c87b03e5Sespie }
383*c87b03e5Sespie 
384*c87b03e5Sespie /* Create an edge connecting SRC to DEST and set probability by knowing
385*c87b03e5Sespie    that it is the single edge leaving SRC.  */
386*c87b03e5Sespie 
387*c87b03e5Sespie edge
make_single_succ_edge(src,dest,flags)388*c87b03e5Sespie make_single_succ_edge (src, dest, flags)
389*c87b03e5Sespie      basic_block src, dest;
390*c87b03e5Sespie      int flags;
391*c87b03e5Sespie {
392*c87b03e5Sespie   edge e = make_edge (src, dest, flags);
393*c87b03e5Sespie 
394*c87b03e5Sespie   e->probability = REG_BR_PROB_BASE;
395*c87b03e5Sespie   e->count = src->count;
396*c87b03e5Sespie   return e;
397*c87b03e5Sespie }
398*c87b03e5Sespie 
399*c87b03e5Sespie /* This function will remove an edge from the flow graph.  */
400*c87b03e5Sespie 
401*c87b03e5Sespie void
remove_edge(e)402*c87b03e5Sespie remove_edge (e)
403*c87b03e5Sespie      edge e;
404*c87b03e5Sespie {
405*c87b03e5Sespie   edge last_pred = NULL;
406*c87b03e5Sespie   edge last_succ = NULL;
407*c87b03e5Sespie   edge tmp;
408*c87b03e5Sespie   basic_block src, dest;
409*c87b03e5Sespie 
410*c87b03e5Sespie   src = e->src;
411*c87b03e5Sespie   dest = e->dest;
412*c87b03e5Sespie   for (tmp = src->succ; tmp && tmp != e; tmp = tmp->succ_next)
413*c87b03e5Sespie     last_succ = tmp;
414*c87b03e5Sespie 
415*c87b03e5Sespie   if (!tmp)
416*c87b03e5Sespie     abort ();
417*c87b03e5Sespie   if (last_succ)
418*c87b03e5Sespie     last_succ->succ_next = e->succ_next;
419*c87b03e5Sespie   else
420*c87b03e5Sespie     src->succ = e->succ_next;
421*c87b03e5Sespie 
422*c87b03e5Sespie   for (tmp = dest->pred; tmp && tmp != e; tmp = tmp->pred_next)
423*c87b03e5Sespie     last_pred = tmp;
424*c87b03e5Sespie 
425*c87b03e5Sespie   if (!tmp)
426*c87b03e5Sespie     abort ();
427*c87b03e5Sespie   if (last_pred)
428*c87b03e5Sespie     last_pred->pred_next = e->pred_next;
429*c87b03e5Sespie   else
430*c87b03e5Sespie     dest->pred = e->pred_next;
431*c87b03e5Sespie 
432*c87b03e5Sespie   free_edge (e);
433*c87b03e5Sespie }
434*c87b03e5Sespie 
435*c87b03e5Sespie /* Redirect an edge's successor from one block to another.  */
436*c87b03e5Sespie 
437*c87b03e5Sespie void
redirect_edge_succ(e,new_succ)438*c87b03e5Sespie redirect_edge_succ (e, new_succ)
439*c87b03e5Sespie      edge e;
440*c87b03e5Sespie      basic_block new_succ;
441*c87b03e5Sespie {
442*c87b03e5Sespie   edge *pe;
443*c87b03e5Sespie 
444*c87b03e5Sespie   /* Disconnect the edge from the old successor block.  */
445*c87b03e5Sespie   for (pe = &e->dest->pred; *pe != e; pe = &(*pe)->pred_next)
446*c87b03e5Sespie     continue;
447*c87b03e5Sespie   *pe = (*pe)->pred_next;
448*c87b03e5Sespie 
449*c87b03e5Sespie   /* Reconnect the edge to the new successor block.  */
450*c87b03e5Sespie   e->pred_next = new_succ->pred;
451*c87b03e5Sespie   new_succ->pred = e;
452*c87b03e5Sespie   e->dest = new_succ;
453*c87b03e5Sespie }
454*c87b03e5Sespie 
455*c87b03e5Sespie /* Like previous but avoid possible duplicate edge.  */
456*c87b03e5Sespie 
457*c87b03e5Sespie edge
redirect_edge_succ_nodup(e,new_succ)458*c87b03e5Sespie redirect_edge_succ_nodup (e, new_succ)
459*c87b03e5Sespie      edge e;
460*c87b03e5Sespie      basic_block new_succ;
461*c87b03e5Sespie {
462*c87b03e5Sespie   edge s;
463*c87b03e5Sespie 
464*c87b03e5Sespie   /* Check whether the edge is already present.  */
465*c87b03e5Sespie   for (s = e->src->succ; s; s = s->succ_next)
466*c87b03e5Sespie     if (s->dest == new_succ && s != e)
467*c87b03e5Sespie       break;
468*c87b03e5Sespie 
469*c87b03e5Sespie   if (s)
470*c87b03e5Sespie     {
471*c87b03e5Sespie       s->flags |= e->flags;
472*c87b03e5Sespie       s->probability += e->probability;
473*c87b03e5Sespie       if (s->probability > REG_BR_PROB_BASE)
474*c87b03e5Sespie 	s->probability = REG_BR_PROB_BASE;
475*c87b03e5Sespie       s->count += e->count;
476*c87b03e5Sespie       remove_edge (e);
477*c87b03e5Sespie       e = s;
478*c87b03e5Sespie     }
479*c87b03e5Sespie   else
480*c87b03e5Sespie     redirect_edge_succ (e, new_succ);
481*c87b03e5Sespie 
482*c87b03e5Sespie   return e;
483*c87b03e5Sespie }
484*c87b03e5Sespie 
485*c87b03e5Sespie /* Redirect an edge's predecessor from one block to another.  */
486*c87b03e5Sespie 
487*c87b03e5Sespie void
redirect_edge_pred(e,new_pred)488*c87b03e5Sespie redirect_edge_pred (e, new_pred)
489*c87b03e5Sespie      edge e;
490*c87b03e5Sespie      basic_block new_pred;
491*c87b03e5Sespie {
492*c87b03e5Sespie   edge *pe;
493*c87b03e5Sespie 
494*c87b03e5Sespie   /* Disconnect the edge from the old predecessor block.  */
495*c87b03e5Sespie   for (pe = &e->src->succ; *pe != e; pe = &(*pe)->succ_next)
496*c87b03e5Sespie     continue;
497*c87b03e5Sespie 
498*c87b03e5Sespie   *pe = (*pe)->succ_next;
499*c87b03e5Sespie 
500*c87b03e5Sespie   /* Reconnect the edge to the new predecessor block.  */
501*c87b03e5Sespie   e->succ_next = new_pred->succ;
502*c87b03e5Sespie   new_pred->succ = e;
503*c87b03e5Sespie   e->src = new_pred;
504*c87b03e5Sespie }
505*c87b03e5Sespie 
506*c87b03e5Sespie void
clear_bb_flags()507*c87b03e5Sespie clear_bb_flags ()
508*c87b03e5Sespie {
509*c87b03e5Sespie   basic_block bb;
510*c87b03e5Sespie 
511*c87b03e5Sespie   FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
512*c87b03e5Sespie     bb->flags = 0;
513*c87b03e5Sespie }
514*c87b03e5Sespie 
515*c87b03e5Sespie void
dump_flow_info(file)516*c87b03e5Sespie dump_flow_info (file)
517*c87b03e5Sespie      FILE *file;
518*c87b03e5Sespie {
519*c87b03e5Sespie   int i;
520*c87b03e5Sespie   int max_regno = max_reg_num ();
521*c87b03e5Sespie   basic_block bb;
522*c87b03e5Sespie   static const char * const reg_class_names[] = REG_CLASS_NAMES;
523*c87b03e5Sespie 
524*c87b03e5Sespie   fprintf (file, "%d registers.\n", max_regno);
525*c87b03e5Sespie   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
526*c87b03e5Sespie     if (REG_N_REFS (i))
527*c87b03e5Sespie       {
528*c87b03e5Sespie 	enum reg_class class, altclass;
529*c87b03e5Sespie 
530*c87b03e5Sespie 	fprintf (file, "\nRegister %d used %d times across %d insns",
531*c87b03e5Sespie 		 i, REG_N_REFS (i), REG_LIVE_LENGTH (i));
532*c87b03e5Sespie 	if (REG_BASIC_BLOCK (i) >= 0)
533*c87b03e5Sespie 	  fprintf (file, " in block %d", REG_BASIC_BLOCK (i));
534*c87b03e5Sespie 	if (REG_N_SETS (i))
535*c87b03e5Sespie 	  fprintf (file, "; set %d time%s", REG_N_SETS (i),
536*c87b03e5Sespie 		   (REG_N_SETS (i) == 1) ? "" : "s");
537*c87b03e5Sespie 	if (regno_reg_rtx[i] != NULL && REG_USERVAR_P (regno_reg_rtx[i]))
538*c87b03e5Sespie 	  fprintf (file, "; user var");
539*c87b03e5Sespie 	if (REG_N_DEATHS (i) != 1)
540*c87b03e5Sespie 	  fprintf (file, "; dies in %d places", REG_N_DEATHS (i));
541*c87b03e5Sespie 	if (REG_N_CALLS_CROSSED (i) == 1)
542*c87b03e5Sespie 	  fprintf (file, "; crosses 1 call");
543*c87b03e5Sespie 	else if (REG_N_CALLS_CROSSED (i))
544*c87b03e5Sespie 	  fprintf (file, "; crosses %d calls", REG_N_CALLS_CROSSED (i));
545*c87b03e5Sespie 	if (regno_reg_rtx[i] != NULL
546*c87b03e5Sespie 	    && PSEUDO_REGNO_BYTES (i) != UNITS_PER_WORD)
547*c87b03e5Sespie 	  fprintf (file, "; %d bytes", PSEUDO_REGNO_BYTES (i));
548*c87b03e5Sespie 
549*c87b03e5Sespie 	class = reg_preferred_class (i);
550*c87b03e5Sespie 	altclass = reg_alternate_class (i);
551*c87b03e5Sespie 	if (class != GENERAL_REGS || altclass != ALL_REGS)
552*c87b03e5Sespie 	  {
553*c87b03e5Sespie 	    if (altclass == ALL_REGS || class == ALL_REGS)
554*c87b03e5Sespie 	      fprintf (file, "; pref %s", reg_class_names[(int) class]);
555*c87b03e5Sespie 	    else if (altclass == NO_REGS)
556*c87b03e5Sespie 	      fprintf (file, "; %s or none", reg_class_names[(int) class]);
557*c87b03e5Sespie 	    else
558*c87b03e5Sespie 	      fprintf (file, "; pref %s, else %s",
559*c87b03e5Sespie 		       reg_class_names[(int) class],
560*c87b03e5Sespie 		       reg_class_names[(int) altclass]);
561*c87b03e5Sespie 	  }
562*c87b03e5Sespie 
563*c87b03e5Sespie 	if (regno_reg_rtx[i] != NULL && REG_POINTER (regno_reg_rtx[i]))
564*c87b03e5Sespie 	  fprintf (file, "; pointer");
565*c87b03e5Sespie 	fprintf (file, ".\n");
566*c87b03e5Sespie       }
567*c87b03e5Sespie 
568*c87b03e5Sespie   fprintf (file, "\n%d basic blocks, %d edges.\n", n_basic_blocks, n_edges);
569*c87b03e5Sespie   FOR_EACH_BB (bb)
570*c87b03e5Sespie     {
571*c87b03e5Sespie       edge e;
572*c87b03e5Sespie       int sum;
573*c87b03e5Sespie       gcov_type lsum;
574*c87b03e5Sespie 
575*c87b03e5Sespie       fprintf (file, "\nBasic block %d: first insn %d, last %d, ",
576*c87b03e5Sespie 	       bb->index, INSN_UID (bb->head), INSN_UID (bb->end));
577*c87b03e5Sespie       fprintf (file, "prev %d, next %d, ",
578*c87b03e5Sespie 	       bb->prev_bb->index, bb->next_bb->index);
579*c87b03e5Sespie       fprintf (file, "loop_depth %d, count ", bb->loop_depth);
580*c87b03e5Sespie       fprintf (file, HOST_WIDEST_INT_PRINT_DEC, bb->count);
581*c87b03e5Sespie       fprintf (file, ", freq %i", bb->frequency);
582*c87b03e5Sespie       if (maybe_hot_bb_p (bb))
583*c87b03e5Sespie 	fprintf (file, ", maybe hot");
584*c87b03e5Sespie       if (probably_never_executed_bb_p (bb))
585*c87b03e5Sespie 	fprintf (file, ", probably never executed");
586*c87b03e5Sespie       fprintf (file, ".\n");
587*c87b03e5Sespie 
588*c87b03e5Sespie       fprintf (file, "Predecessors: ");
589*c87b03e5Sespie       for (e = bb->pred; e; e = e->pred_next)
590*c87b03e5Sespie 	dump_edge_info (file, e, 0);
591*c87b03e5Sespie 
592*c87b03e5Sespie       fprintf (file, "\nSuccessors: ");
593*c87b03e5Sespie       for (e = bb->succ; e; e = e->succ_next)
594*c87b03e5Sespie 	dump_edge_info (file, e, 1);
595*c87b03e5Sespie 
596*c87b03e5Sespie       fprintf (file, "\nRegisters live at start:");
597*c87b03e5Sespie       dump_regset (bb->global_live_at_start, file);
598*c87b03e5Sespie 
599*c87b03e5Sespie       fprintf (file, "\nRegisters live at end:");
600*c87b03e5Sespie       dump_regset (bb->global_live_at_end, file);
601*c87b03e5Sespie 
602*c87b03e5Sespie       putc ('\n', file);
603*c87b03e5Sespie 
604*c87b03e5Sespie       /* Check the consistency of profile information.  We can't do that
605*c87b03e5Sespie 	 in verify_flow_info, as the counts may get invalid for incompletely
606*c87b03e5Sespie 	 solved graphs, later elliminating of conditionals or roundoff errors.
607*c87b03e5Sespie 	 It is still practical to have them reported for debugging of simple
608*c87b03e5Sespie 	 testcases.  */
609*c87b03e5Sespie       sum = 0;
610*c87b03e5Sespie       for (e = bb->succ; e; e = e->succ_next)
611*c87b03e5Sespie 	sum += e->probability;
612*c87b03e5Sespie       if (bb->succ && abs (sum - REG_BR_PROB_BASE) > 100)
613*c87b03e5Sespie 	fprintf (file, "Invalid sum of outgoing probabilities %.1f%%\n",
614*c87b03e5Sespie 		 sum * 100.0 / REG_BR_PROB_BASE);
615*c87b03e5Sespie       sum = 0;
616*c87b03e5Sespie       for (e = bb->pred; e; e = e->pred_next)
617*c87b03e5Sespie 	sum += EDGE_FREQUENCY (e);
618*c87b03e5Sespie       if (abs (sum - bb->frequency) > 100)
619*c87b03e5Sespie 	fprintf (file,
620*c87b03e5Sespie 		 "Invalid sum of incomming frequencies %i, should be %i\n",
621*c87b03e5Sespie 		 sum, bb->frequency);
622*c87b03e5Sespie       lsum = 0;
623*c87b03e5Sespie       for (e = bb->pred; e; e = e->pred_next)
624*c87b03e5Sespie 	lsum += e->count;
625*c87b03e5Sespie       if (lsum - bb->count > 100 || lsum - bb->count < -100)
626*c87b03e5Sespie 	fprintf (file, "Invalid sum of incomming counts %i, should be %i\n",
627*c87b03e5Sespie 		 (int)lsum, (int)bb->count);
628*c87b03e5Sespie       lsum = 0;
629*c87b03e5Sespie       for (e = bb->succ; e; e = e->succ_next)
630*c87b03e5Sespie 	lsum += e->count;
631*c87b03e5Sespie       if (bb->succ && (lsum - bb->count > 100 || lsum - bb->count < -100))
632*c87b03e5Sespie 	fprintf (file, "Invalid sum of incomming counts %i, should be %i\n",
633*c87b03e5Sespie 		 (int)lsum, (int)bb->count);
634*c87b03e5Sespie     }
635*c87b03e5Sespie 
636*c87b03e5Sespie   putc ('\n', file);
637*c87b03e5Sespie }
638*c87b03e5Sespie 
639*c87b03e5Sespie void
debug_flow_info()640*c87b03e5Sespie debug_flow_info ()
641*c87b03e5Sespie {
642*c87b03e5Sespie   dump_flow_info (stderr);
643*c87b03e5Sespie }
644*c87b03e5Sespie 
645*c87b03e5Sespie void
dump_edge_info(file,e,do_succ)646*c87b03e5Sespie dump_edge_info (file, e, do_succ)
647*c87b03e5Sespie      FILE *file;
648*c87b03e5Sespie      edge e;
649*c87b03e5Sespie      int do_succ;
650*c87b03e5Sespie {
651*c87b03e5Sespie   basic_block side = (do_succ ? e->dest : e->src);
652*c87b03e5Sespie 
653*c87b03e5Sespie   if (side == ENTRY_BLOCK_PTR)
654*c87b03e5Sespie     fputs (" ENTRY", file);
655*c87b03e5Sespie   else if (side == EXIT_BLOCK_PTR)
656*c87b03e5Sespie     fputs (" EXIT", file);
657*c87b03e5Sespie   else
658*c87b03e5Sespie     fprintf (file, " %d", side->index);
659*c87b03e5Sespie 
660*c87b03e5Sespie   if (e->probability)
661*c87b03e5Sespie     fprintf (file, " [%.1f%%] ", e->probability * 100.0 / REG_BR_PROB_BASE);
662*c87b03e5Sespie 
663*c87b03e5Sespie   if (e->count)
664*c87b03e5Sespie     {
665*c87b03e5Sespie       fprintf (file, " count:");
666*c87b03e5Sespie       fprintf (file, HOST_WIDEST_INT_PRINT_DEC, e->count);
667*c87b03e5Sespie     }
668*c87b03e5Sespie 
669*c87b03e5Sespie   if (e->flags)
670*c87b03e5Sespie     {
671*c87b03e5Sespie       static const char * const bitnames[]
672*c87b03e5Sespie 	= {"fallthru", "ab", "abcall", "eh", "fake", "dfs_back", "can_fallthru"};
673*c87b03e5Sespie       int comma = 0;
674*c87b03e5Sespie       int i, flags = e->flags;
675*c87b03e5Sespie 
676*c87b03e5Sespie       fputs (" (", file);
677*c87b03e5Sespie       for (i = 0; flags; i++)
678*c87b03e5Sespie 	if (flags & (1 << i))
679*c87b03e5Sespie 	  {
680*c87b03e5Sespie 	    flags &= ~(1 << i);
681*c87b03e5Sespie 
682*c87b03e5Sespie 	    if (comma)
683*c87b03e5Sespie 	      fputc (',', file);
684*c87b03e5Sespie 	    if (i < (int) ARRAY_SIZE (bitnames))
685*c87b03e5Sespie 	      fputs (bitnames[i], file);
686*c87b03e5Sespie 	    else
687*c87b03e5Sespie 	      fprintf (file, "%d", i);
688*c87b03e5Sespie 	    comma = 1;
689*c87b03e5Sespie 	  }
690*c87b03e5Sespie 
691*c87b03e5Sespie       fputc (')', file);
692*c87b03e5Sespie     }
693*c87b03e5Sespie }
694*c87b03e5Sespie 
695*c87b03e5Sespie /* Simple routines to easily allocate AUX fields of basic blocks.  */
696*c87b03e5Sespie 
697*c87b03e5Sespie static struct obstack block_aux_obstack;
698*c87b03e5Sespie static void *first_block_aux_obj = 0;
699*c87b03e5Sespie static struct obstack edge_aux_obstack;
700*c87b03e5Sespie static void *first_edge_aux_obj = 0;
701*c87b03e5Sespie 
702*c87b03e5Sespie /* Allocate a memory block of SIZE as BB->aux.  The obstack must
703*c87b03e5Sespie    be first initialized by alloc_aux_for_blocks.  */
704*c87b03e5Sespie 
705*c87b03e5Sespie inline void
alloc_aux_for_block(bb,size)706*c87b03e5Sespie alloc_aux_for_block (bb, size)
707*c87b03e5Sespie      basic_block bb;
708*c87b03e5Sespie      int size;
709*c87b03e5Sespie {
710*c87b03e5Sespie   /* Verify that aux field is clear.  */
711*c87b03e5Sespie   if (bb->aux || !first_block_aux_obj)
712*c87b03e5Sespie     abort ();
713*c87b03e5Sespie   bb->aux = obstack_alloc (&block_aux_obstack, size);
714*c87b03e5Sespie   memset (bb->aux, 0, size);
715*c87b03e5Sespie }
716*c87b03e5Sespie 
717*c87b03e5Sespie /* Initialize the block_aux_obstack and if SIZE is nonzero, call
718*c87b03e5Sespie    alloc_aux_for_block for each basic block.  */
719*c87b03e5Sespie 
720*c87b03e5Sespie void
alloc_aux_for_blocks(size)721*c87b03e5Sespie alloc_aux_for_blocks (size)
722*c87b03e5Sespie      int size;
723*c87b03e5Sespie {
724*c87b03e5Sespie   static int initialized;
725*c87b03e5Sespie 
726*c87b03e5Sespie   if (!initialized)
727*c87b03e5Sespie     {
728*c87b03e5Sespie       gcc_obstack_init (&block_aux_obstack);
729*c87b03e5Sespie       initialized = 1;
730*c87b03e5Sespie     }
731*c87b03e5Sespie 
732*c87b03e5Sespie   /* Check whether AUX data are still allocated.  */
733*c87b03e5Sespie   else if (first_block_aux_obj)
734*c87b03e5Sespie     abort ();
735*c87b03e5Sespie   first_block_aux_obj = (char *) obstack_alloc (&block_aux_obstack, 0);
736*c87b03e5Sespie   if (size)
737*c87b03e5Sespie     {
738*c87b03e5Sespie       basic_block bb;
739*c87b03e5Sespie 
740*c87b03e5Sespie       FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
741*c87b03e5Sespie 	alloc_aux_for_block (bb, size);
742*c87b03e5Sespie     }
743*c87b03e5Sespie }
744*c87b03e5Sespie 
745*c87b03e5Sespie /* Clear AUX pointers of all blocks.  */
746*c87b03e5Sespie 
747*c87b03e5Sespie void
clear_aux_for_blocks()748*c87b03e5Sespie clear_aux_for_blocks ()
749*c87b03e5Sespie {
750*c87b03e5Sespie   basic_block bb;
751*c87b03e5Sespie 
752*c87b03e5Sespie   FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
753*c87b03e5Sespie     bb->aux = NULL;
754*c87b03e5Sespie }
755*c87b03e5Sespie 
756*c87b03e5Sespie /* Free data allocated in block_aux_obstack and clear AUX pointers
757*c87b03e5Sespie    of all blocks.  */
758*c87b03e5Sespie 
759*c87b03e5Sespie void
free_aux_for_blocks()760*c87b03e5Sespie free_aux_for_blocks ()
761*c87b03e5Sespie {
762*c87b03e5Sespie   if (!first_block_aux_obj)
763*c87b03e5Sespie     abort ();
764*c87b03e5Sespie   obstack_free (&block_aux_obstack, first_block_aux_obj);
765*c87b03e5Sespie   first_block_aux_obj = NULL;
766*c87b03e5Sespie 
767*c87b03e5Sespie   clear_aux_for_blocks ();
768*c87b03e5Sespie }
769*c87b03e5Sespie 
770*c87b03e5Sespie /* Allocate a memory edge of SIZE as BB->aux.  The obstack must
771*c87b03e5Sespie    be first initialized by alloc_aux_for_edges.  */
772*c87b03e5Sespie 
773*c87b03e5Sespie inline void
alloc_aux_for_edge(e,size)774*c87b03e5Sespie alloc_aux_for_edge (e, size)
775*c87b03e5Sespie      edge e;
776*c87b03e5Sespie      int size;
777*c87b03e5Sespie {
778*c87b03e5Sespie   /* Verify that aux field is clear.  */
779*c87b03e5Sespie   if (e->aux || !first_edge_aux_obj)
780*c87b03e5Sespie     abort ();
781*c87b03e5Sespie   e->aux = obstack_alloc (&edge_aux_obstack, size);
782*c87b03e5Sespie   memset (e->aux, 0, size);
783*c87b03e5Sespie }
784*c87b03e5Sespie 
785*c87b03e5Sespie /* Initialize the edge_aux_obstack and if SIZE is nonzero, call
786*c87b03e5Sespie    alloc_aux_for_edge for each basic edge.  */
787*c87b03e5Sespie 
788*c87b03e5Sespie void
alloc_aux_for_edges(size)789*c87b03e5Sespie alloc_aux_for_edges (size)
790*c87b03e5Sespie      int size;
791*c87b03e5Sespie {
792*c87b03e5Sespie   static int initialized;
793*c87b03e5Sespie 
794*c87b03e5Sespie   if (!initialized)
795*c87b03e5Sespie     {
796*c87b03e5Sespie       gcc_obstack_init (&edge_aux_obstack);
797*c87b03e5Sespie       initialized = 1;
798*c87b03e5Sespie     }
799*c87b03e5Sespie 
800*c87b03e5Sespie   /* Check whether AUX data are still allocated.  */
801*c87b03e5Sespie   else if (first_edge_aux_obj)
802*c87b03e5Sespie     abort ();
803*c87b03e5Sespie 
804*c87b03e5Sespie   first_edge_aux_obj = (char *) obstack_alloc (&edge_aux_obstack, 0);
805*c87b03e5Sespie   if (size)
806*c87b03e5Sespie     {
807*c87b03e5Sespie       basic_block bb;
808*c87b03e5Sespie 
809*c87b03e5Sespie       FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
810*c87b03e5Sespie 	{
811*c87b03e5Sespie 	  edge e;
812*c87b03e5Sespie 
813*c87b03e5Sespie 	  for (e = bb->succ; e; e = e->succ_next)
814*c87b03e5Sespie 	    alloc_aux_for_edge (e, size);
815*c87b03e5Sespie 	}
816*c87b03e5Sespie     }
817*c87b03e5Sespie }
818*c87b03e5Sespie 
819*c87b03e5Sespie /* Clear AUX pointers of all edges.  */
820*c87b03e5Sespie 
821*c87b03e5Sespie void
clear_aux_for_edges()822*c87b03e5Sespie clear_aux_for_edges ()
823*c87b03e5Sespie {
824*c87b03e5Sespie   basic_block bb;
825*c87b03e5Sespie   edge e;
826*c87b03e5Sespie 
827*c87b03e5Sespie   FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
828*c87b03e5Sespie     {
829*c87b03e5Sespie       for (e = bb->succ; e; e = e->succ_next)
830*c87b03e5Sespie 	e->aux = NULL;
831*c87b03e5Sespie     }
832*c87b03e5Sespie }
833*c87b03e5Sespie 
834*c87b03e5Sespie /* Free data allocated in edge_aux_obstack and clear AUX pointers
835*c87b03e5Sespie    of all edges.  */
836*c87b03e5Sespie 
837*c87b03e5Sespie void
free_aux_for_edges()838*c87b03e5Sespie free_aux_for_edges ()
839*c87b03e5Sespie {
840*c87b03e5Sespie   if (!first_edge_aux_obj)
841*c87b03e5Sespie     abort ();
842*c87b03e5Sespie   obstack_free (&edge_aux_obstack, first_edge_aux_obj);
843*c87b03e5Sespie   first_edge_aux_obj = NULL;
844*c87b03e5Sespie 
845*c87b03e5Sespie   clear_aux_for_edges ();
846*c87b03e5Sespie }
847