xref: /dflybsd-src/contrib/gcc-4.7/gcc/cfg.c (revision 04febcfb30580676d3e95f58a16c5137ee478b32)
1*e4b17023SJohn Marino /* Control flow graph manipulation code for GNU compiler.
2*e4b17023SJohn Marino    Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3*e4b17023SJohn Marino    1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4*e4b17023SJohn Marino    Free Software Foundation, Inc.
5*e4b17023SJohn Marino 
6*e4b17023SJohn Marino This file is part of GCC.
7*e4b17023SJohn Marino 
8*e4b17023SJohn Marino GCC is free software; you can redistribute it and/or modify it under
9*e4b17023SJohn Marino the terms of the GNU General Public License as published by the Free
10*e4b17023SJohn Marino Software Foundation; either version 3, or (at your option) any later
11*e4b17023SJohn Marino version.
12*e4b17023SJohn Marino 
13*e4b17023SJohn Marino GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14*e4b17023SJohn Marino WARRANTY; without even the implied warranty of MERCHANTABILITY or
15*e4b17023SJohn Marino FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16*e4b17023SJohn Marino for more details.
17*e4b17023SJohn Marino 
18*e4b17023SJohn Marino You should have received a copy of the GNU General Public License
19*e4b17023SJohn Marino along with GCC; see the file COPYING3.  If not see
20*e4b17023SJohn Marino <http://www.gnu.org/licenses/>.  */
21*e4b17023SJohn Marino 
22*e4b17023SJohn Marino /* This file contains low level functions to manipulate the CFG and
23*e4b17023SJohn Marino    analyze it.  All other modules should not transform the data structure
24*e4b17023SJohn Marino    directly and use abstraction instead.  The file is supposed to be
25*e4b17023SJohn Marino    ordered bottom-up and should not contain any code dependent on a
26*e4b17023SJohn Marino    particular intermediate language (RTL or trees).
27*e4b17023SJohn Marino 
28*e4b17023SJohn Marino    Available functionality:
29*e4b17023SJohn Marino      - Initialization/deallocation
30*e4b17023SJohn Marino 	 init_flow, clear_edges
31*e4b17023SJohn Marino      - Low level basic block manipulation
32*e4b17023SJohn Marino 	 alloc_block, expunge_block
33*e4b17023SJohn Marino      - Edge manipulation
34*e4b17023SJohn Marino 	 make_edge, make_single_succ_edge, cached_make_edge, remove_edge
35*e4b17023SJohn Marino 	 - Low level edge redirection (without updating instruction chain)
36*e4b17023SJohn Marino 	     redirect_edge_succ, redirect_edge_succ_nodup, redirect_edge_pred
37*e4b17023SJohn Marino      - Dumping and debugging
38*e4b17023SJohn Marino 	 dump_flow_info, debug_flow_info, dump_edge_info
39*e4b17023SJohn Marino      - Allocation of AUX fields for basic blocks
40*e4b17023SJohn Marino 	 alloc_aux_for_blocks, free_aux_for_blocks, alloc_aux_for_block
41*e4b17023SJohn Marino      - clear_bb_flags
42*e4b17023SJohn Marino      - Consistency checking
43*e4b17023SJohn Marino 	 verify_flow_info
44*e4b17023SJohn Marino      - Dumping and debugging
45*e4b17023SJohn Marino 	 print_rtl_with_bb, dump_bb, debug_bb, debug_bb_n
46*e4b17023SJohn Marino  */
47*e4b17023SJohn Marino 
48*e4b17023SJohn Marino #include "config.h"
49*e4b17023SJohn Marino #include "system.h"
50*e4b17023SJohn Marino #include "coretypes.h"
51*e4b17023SJohn Marino #include "tm.h"
52*e4b17023SJohn Marino #include "tree.h"
53*e4b17023SJohn Marino #include "rtl.h"
54*e4b17023SJohn Marino #include "hard-reg-set.h"
55*e4b17023SJohn Marino #include "regs.h"
56*e4b17023SJohn Marino #include "flags.h"
57*e4b17023SJohn Marino #include "output.h"
58*e4b17023SJohn Marino #include "function.h"
59*e4b17023SJohn Marino #include "except.h"
60*e4b17023SJohn Marino #include "diagnostic-core.h"
61*e4b17023SJohn Marino #include "tm_p.h"
62*e4b17023SJohn Marino #include "obstack.h"
63*e4b17023SJohn Marino #include "timevar.h"
64*e4b17023SJohn Marino #include "tree-pass.h"
65*e4b17023SJohn Marino #include "ggc.h"
66*e4b17023SJohn Marino #include "hashtab.h"
67*e4b17023SJohn Marino #include "alloc-pool.h"
68*e4b17023SJohn Marino #include "df.h"
69*e4b17023SJohn Marino #include "cfgloop.h"
70*e4b17023SJohn Marino #include "tree-flow.h"
71*e4b17023SJohn Marino 
72*e4b17023SJohn Marino /* The obstack on which the flow graph components are allocated.  */
73*e4b17023SJohn Marino 
74*e4b17023SJohn Marino struct bitmap_obstack reg_obstack;
75*e4b17023SJohn Marino 
76*e4b17023SJohn Marino void debug_flow_info (void);
77*e4b17023SJohn Marino static void free_edge (edge);
78*e4b17023SJohn Marino 
79*e4b17023SJohn Marino #define RDIV(X,Y) (((X) + (Y) / 2) / (Y))
80*e4b17023SJohn Marino 
81*e4b17023SJohn Marino /* Called once at initialization time.  */
82*e4b17023SJohn Marino 
83*e4b17023SJohn Marino void
init_flow(struct function * the_fun)84*e4b17023SJohn Marino init_flow (struct function *the_fun)
85*e4b17023SJohn Marino {
86*e4b17023SJohn Marino   if (!the_fun->cfg)
87*e4b17023SJohn Marino     the_fun->cfg = ggc_alloc_cleared_control_flow_graph ();
88*e4b17023SJohn Marino   n_edges_for_function (the_fun) = 0;
89*e4b17023SJohn Marino   ENTRY_BLOCK_PTR_FOR_FUNCTION (the_fun)
90*e4b17023SJohn Marino     = ggc_alloc_cleared_basic_block_def ();
91*e4b17023SJohn Marino   ENTRY_BLOCK_PTR_FOR_FUNCTION (the_fun)->index = ENTRY_BLOCK;
92*e4b17023SJohn Marino   EXIT_BLOCK_PTR_FOR_FUNCTION (the_fun)
93*e4b17023SJohn Marino     = ggc_alloc_cleared_basic_block_def ();
94*e4b17023SJohn Marino   EXIT_BLOCK_PTR_FOR_FUNCTION (the_fun)->index = EXIT_BLOCK;
95*e4b17023SJohn Marino   ENTRY_BLOCK_PTR_FOR_FUNCTION (the_fun)->next_bb
96*e4b17023SJohn Marino     = EXIT_BLOCK_PTR_FOR_FUNCTION (the_fun);
97*e4b17023SJohn Marino   EXIT_BLOCK_PTR_FOR_FUNCTION (the_fun)->prev_bb
98*e4b17023SJohn Marino     = ENTRY_BLOCK_PTR_FOR_FUNCTION (the_fun);
99*e4b17023SJohn Marino }
100*e4b17023SJohn Marino 
101*e4b17023SJohn Marino /* Helper function for remove_edge and clear_edges.  Frees edge structure
102*e4b17023SJohn Marino    without actually unlinking it from the pred/succ lists.  */
103*e4b17023SJohn Marino 
104*e4b17023SJohn Marino static void
free_edge(edge e ATTRIBUTE_UNUSED)105*e4b17023SJohn Marino free_edge (edge e ATTRIBUTE_UNUSED)
106*e4b17023SJohn Marino {
107*e4b17023SJohn Marino   n_edges--;
108*e4b17023SJohn Marino   ggc_free (e);
109*e4b17023SJohn Marino }
110*e4b17023SJohn Marino 
111*e4b17023SJohn Marino /* Free the memory associated with the edge structures.  */
112*e4b17023SJohn Marino 
113*e4b17023SJohn Marino void
clear_edges(void)114*e4b17023SJohn Marino clear_edges (void)
115*e4b17023SJohn Marino {
116*e4b17023SJohn Marino   basic_block bb;
117*e4b17023SJohn Marino   edge e;
118*e4b17023SJohn Marino   edge_iterator ei;
119*e4b17023SJohn Marino 
120*e4b17023SJohn Marino   FOR_EACH_BB (bb)
121*e4b17023SJohn Marino     {
122*e4b17023SJohn Marino       FOR_EACH_EDGE (e, ei, bb->succs)
123*e4b17023SJohn Marino 	free_edge (e);
124*e4b17023SJohn Marino       VEC_truncate (edge, bb->succs, 0);
125*e4b17023SJohn Marino       VEC_truncate (edge, bb->preds, 0);
126*e4b17023SJohn Marino     }
127*e4b17023SJohn Marino 
128*e4b17023SJohn Marino   FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
129*e4b17023SJohn Marino     free_edge (e);
130*e4b17023SJohn Marino   VEC_truncate (edge, EXIT_BLOCK_PTR->preds, 0);
131*e4b17023SJohn Marino   VEC_truncate (edge, ENTRY_BLOCK_PTR->succs, 0);
132*e4b17023SJohn Marino 
133*e4b17023SJohn Marino   gcc_assert (!n_edges);
134*e4b17023SJohn Marino }
135*e4b17023SJohn Marino 
136*e4b17023SJohn Marino /* Allocate memory for basic_block.  */
137*e4b17023SJohn Marino 
138*e4b17023SJohn Marino basic_block
alloc_block(void)139*e4b17023SJohn Marino alloc_block (void)
140*e4b17023SJohn Marino {
141*e4b17023SJohn Marino   basic_block bb;
142*e4b17023SJohn Marino   bb = ggc_alloc_cleared_basic_block_def ();
143*e4b17023SJohn Marino   return bb;
144*e4b17023SJohn Marino }
145*e4b17023SJohn Marino 
146*e4b17023SJohn Marino /* Link block B to chain after AFTER.  */
147*e4b17023SJohn Marino void
link_block(basic_block b,basic_block after)148*e4b17023SJohn Marino link_block (basic_block b, basic_block after)
149*e4b17023SJohn Marino {
150*e4b17023SJohn Marino   b->next_bb = after->next_bb;
151*e4b17023SJohn Marino   b->prev_bb = after;
152*e4b17023SJohn Marino   after->next_bb = b;
153*e4b17023SJohn Marino   b->next_bb->prev_bb = b;
154*e4b17023SJohn Marino }
155*e4b17023SJohn Marino 
156*e4b17023SJohn Marino /* Unlink block B from chain.  */
157*e4b17023SJohn Marino void
unlink_block(basic_block b)158*e4b17023SJohn Marino unlink_block (basic_block b)
159*e4b17023SJohn Marino {
160*e4b17023SJohn Marino   b->next_bb->prev_bb = b->prev_bb;
161*e4b17023SJohn Marino   b->prev_bb->next_bb = b->next_bb;
162*e4b17023SJohn Marino   b->prev_bb = NULL;
163*e4b17023SJohn Marino   b->next_bb = NULL;
164*e4b17023SJohn Marino }
165*e4b17023SJohn Marino 
166*e4b17023SJohn Marino /* Sequentially order blocks and compact the arrays.  */
167*e4b17023SJohn Marino void
compact_blocks(void)168*e4b17023SJohn Marino compact_blocks (void)
169*e4b17023SJohn Marino {
170*e4b17023SJohn Marino   int i;
171*e4b17023SJohn Marino 
172*e4b17023SJohn Marino   SET_BASIC_BLOCK (ENTRY_BLOCK, ENTRY_BLOCK_PTR);
173*e4b17023SJohn Marino   SET_BASIC_BLOCK (EXIT_BLOCK, EXIT_BLOCK_PTR);
174*e4b17023SJohn Marino 
175*e4b17023SJohn Marino   if (df)
176*e4b17023SJohn Marino     df_compact_blocks ();
177*e4b17023SJohn Marino   else
178*e4b17023SJohn Marino     {
179*e4b17023SJohn Marino       basic_block bb;
180*e4b17023SJohn Marino 
181*e4b17023SJohn Marino       i = NUM_FIXED_BLOCKS;
182*e4b17023SJohn Marino       FOR_EACH_BB (bb)
183*e4b17023SJohn Marino 	{
184*e4b17023SJohn Marino 	  SET_BASIC_BLOCK (i, bb);
185*e4b17023SJohn Marino 	  bb->index = i;
186*e4b17023SJohn Marino 	  i++;
187*e4b17023SJohn Marino 	}
188*e4b17023SJohn Marino       gcc_assert (i == n_basic_blocks);
189*e4b17023SJohn Marino 
190*e4b17023SJohn Marino       for (; i < last_basic_block; i++)
191*e4b17023SJohn Marino 	SET_BASIC_BLOCK (i, NULL);
192*e4b17023SJohn Marino     }
193*e4b17023SJohn Marino   last_basic_block = n_basic_blocks;
194*e4b17023SJohn Marino }
195*e4b17023SJohn Marino 
196*e4b17023SJohn Marino /* Remove block B from the basic block array.  */
197*e4b17023SJohn Marino 
198*e4b17023SJohn Marino void
expunge_block(basic_block b)199*e4b17023SJohn Marino expunge_block (basic_block b)
200*e4b17023SJohn Marino {
201*e4b17023SJohn Marino   unlink_block (b);
202*e4b17023SJohn Marino   SET_BASIC_BLOCK (b->index, NULL);
203*e4b17023SJohn Marino   n_basic_blocks--;
204*e4b17023SJohn Marino   /* We should be able to ggc_free here, but we are not.
205*e4b17023SJohn Marino      The dead SSA_NAMES are left pointing to dead statements that are pointing
206*e4b17023SJohn Marino      to dead basic blocks making garbage collector to die.
207*e4b17023SJohn Marino      We should be able to release all dead SSA_NAMES and at the same time we should
208*e4b17023SJohn Marino      clear out BB pointer of dead statements consistently.  */
209*e4b17023SJohn Marino }
210*e4b17023SJohn Marino 
211*e4b17023SJohn Marino /* Connect E to E->src.  */
212*e4b17023SJohn Marino 
213*e4b17023SJohn Marino static inline void
connect_src(edge e)214*e4b17023SJohn Marino connect_src (edge e)
215*e4b17023SJohn Marino {
216*e4b17023SJohn Marino   VEC_safe_push (edge, gc, e->src->succs, e);
217*e4b17023SJohn Marino   df_mark_solutions_dirty ();
218*e4b17023SJohn Marino }
219*e4b17023SJohn Marino 
220*e4b17023SJohn Marino /* Connect E to E->dest.  */
221*e4b17023SJohn Marino 
222*e4b17023SJohn Marino static inline void
connect_dest(edge e)223*e4b17023SJohn Marino connect_dest (edge e)
224*e4b17023SJohn Marino {
225*e4b17023SJohn Marino   basic_block dest = e->dest;
226*e4b17023SJohn Marino   VEC_safe_push (edge, gc, dest->preds, e);
227*e4b17023SJohn Marino   e->dest_idx = EDGE_COUNT (dest->preds) - 1;
228*e4b17023SJohn Marino   df_mark_solutions_dirty ();
229*e4b17023SJohn Marino }
230*e4b17023SJohn Marino 
231*e4b17023SJohn Marino /* Disconnect edge E from E->src.  */
232*e4b17023SJohn Marino 
233*e4b17023SJohn Marino static inline void
disconnect_src(edge e)234*e4b17023SJohn Marino disconnect_src (edge e)
235*e4b17023SJohn Marino {
236*e4b17023SJohn Marino   basic_block src = e->src;
237*e4b17023SJohn Marino   edge_iterator ei;
238*e4b17023SJohn Marino   edge tmp;
239*e4b17023SJohn Marino 
240*e4b17023SJohn Marino   for (ei = ei_start (src->succs); (tmp = ei_safe_edge (ei)); )
241*e4b17023SJohn Marino     {
242*e4b17023SJohn Marino       if (tmp == e)
243*e4b17023SJohn Marino 	{
244*e4b17023SJohn Marino 	  VEC_unordered_remove (edge, src->succs, ei.index);
245*e4b17023SJohn Marino 	  return;
246*e4b17023SJohn Marino 	}
247*e4b17023SJohn Marino       else
248*e4b17023SJohn Marino 	ei_next (&ei);
249*e4b17023SJohn Marino     }
250*e4b17023SJohn Marino 
251*e4b17023SJohn Marino   df_mark_solutions_dirty ();
252*e4b17023SJohn Marino   gcc_unreachable ();
253*e4b17023SJohn Marino }
254*e4b17023SJohn Marino 
255*e4b17023SJohn Marino /* Disconnect edge E from E->dest.  */
256*e4b17023SJohn Marino 
257*e4b17023SJohn Marino static inline void
disconnect_dest(edge e)258*e4b17023SJohn Marino disconnect_dest (edge e)
259*e4b17023SJohn Marino {
260*e4b17023SJohn Marino   basic_block dest = e->dest;
261*e4b17023SJohn Marino   unsigned int dest_idx = e->dest_idx;
262*e4b17023SJohn Marino 
263*e4b17023SJohn Marino   VEC_unordered_remove (edge, dest->preds, dest_idx);
264*e4b17023SJohn Marino 
265*e4b17023SJohn Marino   /* If we removed an edge in the middle of the edge vector, we need
266*e4b17023SJohn Marino      to update dest_idx of the edge that moved into the "hole".  */
267*e4b17023SJohn Marino   if (dest_idx < EDGE_COUNT (dest->preds))
268*e4b17023SJohn Marino     EDGE_PRED (dest, dest_idx)->dest_idx = dest_idx;
269*e4b17023SJohn Marino   df_mark_solutions_dirty ();
270*e4b17023SJohn Marino }
271*e4b17023SJohn Marino 
272*e4b17023SJohn Marino /* Create an edge connecting SRC and DEST with flags FLAGS.  Return newly
273*e4b17023SJohn Marino    created edge.  Use this only if you are sure that this edge can't
274*e4b17023SJohn Marino    possibly already exist.  */
275*e4b17023SJohn Marino 
276*e4b17023SJohn Marino edge
unchecked_make_edge(basic_block src,basic_block dst,int flags)277*e4b17023SJohn Marino unchecked_make_edge (basic_block src, basic_block dst, int flags)
278*e4b17023SJohn Marino {
279*e4b17023SJohn Marino   edge e;
280*e4b17023SJohn Marino   e = ggc_alloc_cleared_edge_def ();
281*e4b17023SJohn Marino   n_edges++;
282*e4b17023SJohn Marino 
283*e4b17023SJohn Marino   e->src = src;
284*e4b17023SJohn Marino   e->dest = dst;
285*e4b17023SJohn Marino   e->flags = flags;
286*e4b17023SJohn Marino 
287*e4b17023SJohn Marino   connect_src (e);
288*e4b17023SJohn Marino   connect_dest (e);
289*e4b17023SJohn Marino 
290*e4b17023SJohn Marino   execute_on_growing_pred (e);
291*e4b17023SJohn Marino   return e;
292*e4b17023SJohn Marino }
293*e4b17023SJohn Marino 
294*e4b17023SJohn Marino /* Create an edge connecting SRC and DST with FLAGS optionally using
295*e4b17023SJohn Marino    edge cache CACHE.  Return the new edge, NULL if already exist.  */
296*e4b17023SJohn Marino 
297*e4b17023SJohn Marino edge
cached_make_edge(sbitmap edge_cache,basic_block src,basic_block dst,int flags)298*e4b17023SJohn Marino cached_make_edge (sbitmap edge_cache, basic_block src, basic_block dst, int flags)
299*e4b17023SJohn Marino {
300*e4b17023SJohn Marino   if (edge_cache == NULL
301*e4b17023SJohn Marino       || src == ENTRY_BLOCK_PTR
302*e4b17023SJohn Marino       || dst == EXIT_BLOCK_PTR)
303*e4b17023SJohn Marino     return make_edge (src, dst, flags);
304*e4b17023SJohn Marino 
305*e4b17023SJohn Marino   /* Does the requested edge already exist?  */
306*e4b17023SJohn Marino   if (! TEST_BIT (edge_cache, dst->index))
307*e4b17023SJohn Marino     {
308*e4b17023SJohn Marino       /* The edge does not exist.  Create one and update the
309*e4b17023SJohn Marino 	 cache.  */
310*e4b17023SJohn Marino       SET_BIT (edge_cache, dst->index);
311*e4b17023SJohn Marino       return unchecked_make_edge (src, dst, flags);
312*e4b17023SJohn Marino     }
313*e4b17023SJohn Marino 
314*e4b17023SJohn Marino   /* At this point, we know that the requested edge exists.  Adjust
315*e4b17023SJohn Marino      flags if necessary.  */
316*e4b17023SJohn Marino   if (flags)
317*e4b17023SJohn Marino     {
318*e4b17023SJohn Marino       edge e = find_edge (src, dst);
319*e4b17023SJohn Marino       e->flags |= flags;
320*e4b17023SJohn Marino     }
321*e4b17023SJohn Marino 
322*e4b17023SJohn Marino   return NULL;
323*e4b17023SJohn Marino }
324*e4b17023SJohn Marino 
325*e4b17023SJohn Marino /* Create an edge connecting SRC and DEST with flags FLAGS.  Return newly
326*e4b17023SJohn Marino    created edge or NULL if already exist.  */
327*e4b17023SJohn Marino 
328*e4b17023SJohn Marino edge
make_edge(basic_block src,basic_block dest,int flags)329*e4b17023SJohn Marino make_edge (basic_block src, basic_block dest, int flags)
330*e4b17023SJohn Marino {
331*e4b17023SJohn Marino   edge e = find_edge (src, dest);
332*e4b17023SJohn Marino 
333*e4b17023SJohn Marino   /* Make sure we don't add duplicate edges.  */
334*e4b17023SJohn Marino   if (e)
335*e4b17023SJohn Marino     {
336*e4b17023SJohn Marino       e->flags |= flags;
337*e4b17023SJohn Marino       return NULL;
338*e4b17023SJohn Marino     }
339*e4b17023SJohn Marino 
340*e4b17023SJohn Marino   return unchecked_make_edge (src, dest, flags);
341*e4b17023SJohn Marino }
342*e4b17023SJohn Marino 
343*e4b17023SJohn Marino /* Create an edge connecting SRC to DEST and set probability by knowing
344*e4b17023SJohn Marino    that it is the single edge leaving SRC.  */
345*e4b17023SJohn Marino 
346*e4b17023SJohn Marino edge
make_single_succ_edge(basic_block src,basic_block dest,int flags)347*e4b17023SJohn Marino make_single_succ_edge (basic_block src, basic_block dest, int flags)
348*e4b17023SJohn Marino {
349*e4b17023SJohn Marino   edge e = make_edge (src, dest, flags);
350*e4b17023SJohn Marino 
351*e4b17023SJohn Marino   e->probability = REG_BR_PROB_BASE;
352*e4b17023SJohn Marino   e->count = src->count;
353*e4b17023SJohn Marino   return e;
354*e4b17023SJohn Marino }
355*e4b17023SJohn Marino 
356*e4b17023SJohn Marino /* This function will remove an edge from the flow graph.  */
357*e4b17023SJohn Marino 
358*e4b17023SJohn Marino void
remove_edge_raw(edge e)359*e4b17023SJohn Marino remove_edge_raw (edge e)
360*e4b17023SJohn Marino {
361*e4b17023SJohn Marino   remove_predictions_associated_with_edge (e);
362*e4b17023SJohn Marino   execute_on_shrinking_pred (e);
363*e4b17023SJohn Marino 
364*e4b17023SJohn Marino   disconnect_src (e);
365*e4b17023SJohn Marino   disconnect_dest (e);
366*e4b17023SJohn Marino 
367*e4b17023SJohn Marino   /* This is probably not needed, but it doesn't hurt.  */
368*e4b17023SJohn Marino   redirect_edge_var_map_clear (e);
369*e4b17023SJohn Marino 
370*e4b17023SJohn Marino   free_edge (e);
371*e4b17023SJohn Marino }
372*e4b17023SJohn Marino 
373*e4b17023SJohn Marino /* Redirect an edge's successor from one block to another.  */
374*e4b17023SJohn Marino 
375*e4b17023SJohn Marino void
redirect_edge_succ(edge e,basic_block new_succ)376*e4b17023SJohn Marino redirect_edge_succ (edge e, basic_block new_succ)
377*e4b17023SJohn Marino {
378*e4b17023SJohn Marino   execute_on_shrinking_pred (e);
379*e4b17023SJohn Marino 
380*e4b17023SJohn Marino   disconnect_dest (e);
381*e4b17023SJohn Marino 
382*e4b17023SJohn Marino   e->dest = new_succ;
383*e4b17023SJohn Marino 
384*e4b17023SJohn Marino   /* Reconnect the edge to the new successor block.  */
385*e4b17023SJohn Marino   connect_dest (e);
386*e4b17023SJohn Marino 
387*e4b17023SJohn Marino   execute_on_growing_pred (e);
388*e4b17023SJohn Marino }
389*e4b17023SJohn Marino 
390*e4b17023SJohn Marino /* Like previous but avoid possible duplicate edge.  */
391*e4b17023SJohn Marino 
392*e4b17023SJohn Marino edge
redirect_edge_succ_nodup(edge e,basic_block new_succ)393*e4b17023SJohn Marino redirect_edge_succ_nodup (edge e, basic_block new_succ)
394*e4b17023SJohn Marino {
395*e4b17023SJohn Marino   edge s;
396*e4b17023SJohn Marino 
397*e4b17023SJohn Marino   s = find_edge (e->src, new_succ);
398*e4b17023SJohn Marino   if (s && s != e)
399*e4b17023SJohn Marino     {
400*e4b17023SJohn Marino       s->flags |= e->flags;
401*e4b17023SJohn Marino       s->probability += e->probability;
402*e4b17023SJohn Marino       if (s->probability > REG_BR_PROB_BASE)
403*e4b17023SJohn Marino 	s->probability = REG_BR_PROB_BASE;
404*e4b17023SJohn Marino       s->count += e->count;
405*e4b17023SJohn Marino       redirect_edge_var_map_dup (s, e);
406*e4b17023SJohn Marino       remove_edge (e);
407*e4b17023SJohn Marino       e = s;
408*e4b17023SJohn Marino     }
409*e4b17023SJohn Marino   else
410*e4b17023SJohn Marino     redirect_edge_succ (e, new_succ);
411*e4b17023SJohn Marino 
412*e4b17023SJohn Marino   return e;
413*e4b17023SJohn Marino }
414*e4b17023SJohn Marino 
415*e4b17023SJohn Marino /* Redirect an edge's predecessor from one block to another.  */
416*e4b17023SJohn Marino 
417*e4b17023SJohn Marino void
redirect_edge_pred(edge e,basic_block new_pred)418*e4b17023SJohn Marino redirect_edge_pred (edge e, basic_block new_pred)
419*e4b17023SJohn Marino {
420*e4b17023SJohn Marino   disconnect_src (e);
421*e4b17023SJohn Marino 
422*e4b17023SJohn Marino   e->src = new_pred;
423*e4b17023SJohn Marino 
424*e4b17023SJohn Marino   /* Reconnect the edge to the new predecessor block.  */
425*e4b17023SJohn Marino   connect_src (e);
426*e4b17023SJohn Marino }
427*e4b17023SJohn Marino 
428*e4b17023SJohn Marino /* Clear all basic block flags, with the exception of partitioning and
429*e4b17023SJohn Marino    setjmp_target.  */
430*e4b17023SJohn Marino void
clear_bb_flags(void)431*e4b17023SJohn Marino clear_bb_flags (void)
432*e4b17023SJohn Marino {
433*e4b17023SJohn Marino   basic_block bb;
434*e4b17023SJohn Marino 
435*e4b17023SJohn Marino   FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
436*e4b17023SJohn Marino     bb->flags = (BB_PARTITION (bb)
437*e4b17023SJohn Marino 		 | (bb->flags & (BB_DISABLE_SCHEDULE + BB_RTL + BB_NON_LOCAL_GOTO_TARGET)));
438*e4b17023SJohn Marino }
439*e4b17023SJohn Marino 
440*e4b17023SJohn Marino /* Check the consistency of profile information.  We can't do that
441*e4b17023SJohn Marino    in verify_flow_info, as the counts may get invalid for incompletely
442*e4b17023SJohn Marino    solved graphs, later eliminating of conditionals or roundoff errors.
443*e4b17023SJohn Marino    It is still practical to have them reported for debugging of simple
444*e4b17023SJohn Marino    testcases.  */
445*e4b17023SJohn Marino void
check_bb_profile(basic_block bb,FILE * file)446*e4b17023SJohn Marino check_bb_profile (basic_block bb, FILE * file)
447*e4b17023SJohn Marino {
448*e4b17023SJohn Marino   edge e;
449*e4b17023SJohn Marino   int sum = 0;
450*e4b17023SJohn Marino   gcov_type lsum;
451*e4b17023SJohn Marino   edge_iterator ei;
452*e4b17023SJohn Marino 
453*e4b17023SJohn Marino   if (profile_status == PROFILE_ABSENT)
454*e4b17023SJohn Marino     return;
455*e4b17023SJohn Marino 
456*e4b17023SJohn Marino   if (bb != EXIT_BLOCK_PTR)
457*e4b17023SJohn Marino     {
458*e4b17023SJohn Marino       FOR_EACH_EDGE (e, ei, bb->succs)
459*e4b17023SJohn Marino 	sum += e->probability;
460*e4b17023SJohn Marino       if (EDGE_COUNT (bb->succs) && abs (sum - REG_BR_PROB_BASE) > 100)
461*e4b17023SJohn Marino 	fprintf (file, "Invalid sum of outgoing probabilities %.1f%%\n",
462*e4b17023SJohn Marino 		 sum * 100.0 / REG_BR_PROB_BASE);
463*e4b17023SJohn Marino       lsum = 0;
464*e4b17023SJohn Marino       FOR_EACH_EDGE (e, ei, bb->succs)
465*e4b17023SJohn Marino 	lsum += e->count;
466*e4b17023SJohn Marino       if (EDGE_COUNT (bb->succs)
467*e4b17023SJohn Marino 	  && (lsum - bb->count > 100 || lsum - bb->count < -100))
468*e4b17023SJohn Marino 	fprintf (file, "Invalid sum of outgoing counts %i, should be %i\n",
469*e4b17023SJohn Marino 		 (int) lsum, (int) bb->count);
470*e4b17023SJohn Marino     }
471*e4b17023SJohn Marino   if (bb != ENTRY_BLOCK_PTR)
472*e4b17023SJohn Marino     {
473*e4b17023SJohn Marino       sum = 0;
474*e4b17023SJohn Marino       FOR_EACH_EDGE (e, ei, bb->preds)
475*e4b17023SJohn Marino 	sum += EDGE_FREQUENCY (e);
476*e4b17023SJohn Marino       if (abs (sum - bb->frequency) > 100)
477*e4b17023SJohn Marino 	fprintf (file,
478*e4b17023SJohn Marino 		 "Invalid sum of incoming frequencies %i, should be %i\n",
479*e4b17023SJohn Marino 		 sum, bb->frequency);
480*e4b17023SJohn Marino       lsum = 0;
481*e4b17023SJohn Marino       FOR_EACH_EDGE (e, ei, bb->preds)
482*e4b17023SJohn Marino 	lsum += e->count;
483*e4b17023SJohn Marino       if (lsum - bb->count > 100 || lsum - bb->count < -100)
484*e4b17023SJohn Marino 	fprintf (file, "Invalid sum of incoming counts %i, should be %i\n",
485*e4b17023SJohn Marino 		 (int) lsum, (int) bb->count);
486*e4b17023SJohn Marino     }
487*e4b17023SJohn Marino }
488*e4b17023SJohn Marino 
489*e4b17023SJohn Marino /* Write information about registers and basic blocks into FILE.
490*e4b17023SJohn Marino    This is part of making a debugging dump.  */
491*e4b17023SJohn Marino 
492*e4b17023SJohn Marino void
dump_regset(regset r,FILE * outf)493*e4b17023SJohn Marino dump_regset (regset r, FILE *outf)
494*e4b17023SJohn Marino {
495*e4b17023SJohn Marino   unsigned i;
496*e4b17023SJohn Marino   reg_set_iterator rsi;
497*e4b17023SJohn Marino 
498*e4b17023SJohn Marino   if (r == NULL)
499*e4b17023SJohn Marino     {
500*e4b17023SJohn Marino       fputs (" (nil)", outf);
501*e4b17023SJohn Marino       return;
502*e4b17023SJohn Marino     }
503*e4b17023SJohn Marino 
504*e4b17023SJohn Marino   EXECUTE_IF_SET_IN_REG_SET (r, 0, i, rsi)
505*e4b17023SJohn Marino     {
506*e4b17023SJohn Marino       fprintf (outf, " %d", i);
507*e4b17023SJohn Marino       if (i < FIRST_PSEUDO_REGISTER)
508*e4b17023SJohn Marino 	fprintf (outf, " [%s]",
509*e4b17023SJohn Marino 		 reg_names[i]);
510*e4b17023SJohn Marino     }
511*e4b17023SJohn Marino }
512*e4b17023SJohn Marino 
513*e4b17023SJohn Marino /* Print a human-readable representation of R on the standard error
514*e4b17023SJohn Marino    stream.  This function is designed to be used from within the
515*e4b17023SJohn Marino    debugger.  */
516*e4b17023SJohn Marino 
517*e4b17023SJohn Marino DEBUG_FUNCTION void
debug_regset(regset r)518*e4b17023SJohn Marino debug_regset (regset r)
519*e4b17023SJohn Marino {
520*e4b17023SJohn Marino   dump_regset (r, stderr);
521*e4b17023SJohn Marino   putc ('\n', stderr);
522*e4b17023SJohn Marino }
523*e4b17023SJohn Marino 
524*e4b17023SJohn Marino /* Emit basic block information for BB.  HEADER is true if the user wants
525*e4b17023SJohn Marino    the generic information and the predecessors, FOOTER is true if they want
526*e4b17023SJohn Marino    the successors.  FLAGS is the dump flags of interest; TDF_DETAILS emit
527*e4b17023SJohn Marino    global register liveness information.  PREFIX is put in front of every
528*e4b17023SJohn Marino    line.  The output is emitted to FILE.  */
529*e4b17023SJohn Marino void
dump_bb_info(basic_block bb,bool header,bool footer,int flags,const char * prefix,FILE * file)530*e4b17023SJohn Marino dump_bb_info (basic_block bb, bool header, bool footer, int flags,
531*e4b17023SJohn Marino 	      const char *prefix, FILE *file)
532*e4b17023SJohn Marino {
533*e4b17023SJohn Marino   edge e;
534*e4b17023SJohn Marino   edge_iterator ei;
535*e4b17023SJohn Marino 
536*e4b17023SJohn Marino   if (header)
537*e4b17023SJohn Marino     {
538*e4b17023SJohn Marino       fprintf (file, "\n%sBasic block %d ", prefix, bb->index);
539*e4b17023SJohn Marino       if (bb->prev_bb)
540*e4b17023SJohn Marino         fprintf (file, ", prev %d", bb->prev_bb->index);
541*e4b17023SJohn Marino       if (bb->next_bb)
542*e4b17023SJohn Marino         fprintf (file, ", next %d", bb->next_bb->index);
543*e4b17023SJohn Marino       fprintf (file, ", loop_depth %d, count ", bb->loop_depth);
544*e4b17023SJohn Marino       fprintf (file, HOST_WIDEST_INT_PRINT_DEC, bb->count);
545*e4b17023SJohn Marino       fprintf (file, ", freq %i", bb->frequency);
546*e4b17023SJohn Marino       /* Both maybe_hot_bb_p & probably_never_executed_bb_p functions
547*e4b17023SJohn Marino 	 crash without cfun. */
548*e4b17023SJohn Marino       if (cfun && maybe_hot_bb_p (bb))
549*e4b17023SJohn Marino 	fputs (", maybe hot", file);
550*e4b17023SJohn Marino       if (cfun && probably_never_executed_bb_p (bb))
551*e4b17023SJohn Marino 	fputs (", probably never executed", file);
552*e4b17023SJohn Marino       if (bb->flags)
553*e4b17023SJohn Marino 	{
554*e4b17023SJohn Marino 	  static const char * const bits[] = {
555*e4b17023SJohn Marino 	    "new", "reachable", "irr_loop", "superblock", "disable_sched",
556*e4b17023SJohn Marino 	    "hot_partition", "cold_partition", "duplicated",
557*e4b17023SJohn Marino 	    "non_local_goto_target", "rtl", "forwarder", "nonthreadable",
558*e4b17023SJohn Marino 	    "modified"
559*e4b17023SJohn Marino 	  };
560*e4b17023SJohn Marino 	  unsigned int flags;
561*e4b17023SJohn Marino 
562*e4b17023SJohn Marino 	  fputs (", flags:", file);
563*e4b17023SJohn Marino 	  for (flags = bb->flags; flags ; flags &= flags - 1)
564*e4b17023SJohn Marino 	    {
565*e4b17023SJohn Marino 	      unsigned i = ctz_hwi (flags);
566*e4b17023SJohn Marino 	      if (i < ARRAY_SIZE (bits))
567*e4b17023SJohn Marino 		fprintf (file, " %s", bits[i]);
568*e4b17023SJohn Marino 	      else
569*e4b17023SJohn Marino 		fprintf (file, " <%d>", i);
570*e4b17023SJohn Marino 	    }
571*e4b17023SJohn Marino 	}
572*e4b17023SJohn Marino       fputs (".\n", file);
573*e4b17023SJohn Marino 
574*e4b17023SJohn Marino       fprintf (file, "%sPredecessors: ", prefix);
575*e4b17023SJohn Marino       FOR_EACH_EDGE (e, ei, bb->preds)
576*e4b17023SJohn Marino 	dump_edge_info (file, e, 0);
577*e4b17023SJohn Marino 
578*e4b17023SJohn Marino       if ((flags & TDF_DETAILS)
579*e4b17023SJohn Marino 	  && (bb->flags & BB_RTL)
580*e4b17023SJohn Marino 	  && df)
581*e4b17023SJohn Marino 	{
582*e4b17023SJohn Marino 	  putc ('\n', file);
583*e4b17023SJohn Marino 	  df_dump_top (bb, file);
584*e4b17023SJohn Marino 	}
585*e4b17023SJohn Marino    }
586*e4b17023SJohn Marino 
587*e4b17023SJohn Marino   if (footer)
588*e4b17023SJohn Marino     {
589*e4b17023SJohn Marino       fprintf (file, "\n%sSuccessors: ", prefix);
590*e4b17023SJohn Marino       FOR_EACH_EDGE (e, ei, bb->succs)
591*e4b17023SJohn Marino 	dump_edge_info (file, e, 1);
592*e4b17023SJohn Marino 
593*e4b17023SJohn Marino       if ((flags & TDF_DETAILS)
594*e4b17023SJohn Marino 	  && (bb->flags & BB_RTL)
595*e4b17023SJohn Marino 	  && df)
596*e4b17023SJohn Marino 	{
597*e4b17023SJohn Marino 	  putc ('\n', file);
598*e4b17023SJohn Marino 	  df_dump_bottom (bb, file);
599*e4b17023SJohn Marino 	}
600*e4b17023SJohn Marino    }
601*e4b17023SJohn Marino 
602*e4b17023SJohn Marino   putc ('\n', file);
603*e4b17023SJohn Marino }
604*e4b17023SJohn Marino 
605*e4b17023SJohn Marino /* Dump the register info to FILE.  */
606*e4b17023SJohn Marino 
607*e4b17023SJohn Marino void
dump_reg_info(FILE * file)608*e4b17023SJohn Marino dump_reg_info (FILE *file)
609*e4b17023SJohn Marino {
610*e4b17023SJohn Marino   unsigned int i, max = max_reg_num ();
611*e4b17023SJohn Marino   if (reload_completed)
612*e4b17023SJohn Marino     return;
613*e4b17023SJohn Marino 
614*e4b17023SJohn Marino   if (reg_info_p_size < max)
615*e4b17023SJohn Marino     max = reg_info_p_size;
616*e4b17023SJohn Marino 
617*e4b17023SJohn Marino   fprintf (file, "%d registers.\n", max);
618*e4b17023SJohn Marino   for (i = FIRST_PSEUDO_REGISTER; i < max; i++)
619*e4b17023SJohn Marino     {
620*e4b17023SJohn Marino       enum reg_class rclass, altclass;
621*e4b17023SJohn Marino 
622*e4b17023SJohn Marino       if (regstat_n_sets_and_refs)
623*e4b17023SJohn Marino 	fprintf (file, "\nRegister %d used %d times across %d insns",
624*e4b17023SJohn Marino 		 i, REG_N_REFS (i), REG_LIVE_LENGTH (i));
625*e4b17023SJohn Marino       else if (df)
626*e4b17023SJohn Marino 	fprintf (file, "\nRegister %d used %d times across %d insns",
627*e4b17023SJohn Marino 		 i, DF_REG_USE_COUNT (i) + DF_REG_DEF_COUNT (i), REG_LIVE_LENGTH (i));
628*e4b17023SJohn Marino 
629*e4b17023SJohn Marino       if (REG_BASIC_BLOCK (i) >= NUM_FIXED_BLOCKS)
630*e4b17023SJohn Marino 	fprintf (file, " in block %d", REG_BASIC_BLOCK (i));
631*e4b17023SJohn Marino       if (regstat_n_sets_and_refs)
632*e4b17023SJohn Marino 	fprintf (file, "; set %d time%s", REG_N_SETS (i),
633*e4b17023SJohn Marino 		 (REG_N_SETS (i) == 1) ? "" : "s");
634*e4b17023SJohn Marino       else if (df)
635*e4b17023SJohn Marino 	fprintf (file, "; set %d time%s", DF_REG_DEF_COUNT (i),
636*e4b17023SJohn Marino 		 (DF_REG_DEF_COUNT (i) == 1) ? "" : "s");
637*e4b17023SJohn Marino       if (regno_reg_rtx[i] != NULL && REG_USERVAR_P (regno_reg_rtx[i]))
638*e4b17023SJohn Marino 	fputs ("; user var", file);
639*e4b17023SJohn Marino       if (REG_N_DEATHS (i) != 1)
640*e4b17023SJohn Marino 	fprintf (file, "; dies in %d places", REG_N_DEATHS (i));
641*e4b17023SJohn Marino       if (REG_N_CALLS_CROSSED (i) == 1)
642*e4b17023SJohn Marino 	fputs ("; crosses 1 call", file);
643*e4b17023SJohn Marino       else if (REG_N_CALLS_CROSSED (i))
644*e4b17023SJohn Marino 	fprintf (file, "; crosses %d calls", REG_N_CALLS_CROSSED (i));
645*e4b17023SJohn Marino       if (REG_FREQ_CALLS_CROSSED (i))
646*e4b17023SJohn Marino 	fprintf (file, "; crosses call with %d frequency", REG_FREQ_CALLS_CROSSED (i));
647*e4b17023SJohn Marino       if (regno_reg_rtx[i] != NULL
648*e4b17023SJohn Marino 	  && PSEUDO_REGNO_BYTES (i) != UNITS_PER_WORD)
649*e4b17023SJohn Marino 	fprintf (file, "; %d bytes", PSEUDO_REGNO_BYTES (i));
650*e4b17023SJohn Marino 
651*e4b17023SJohn Marino       rclass = reg_preferred_class (i);
652*e4b17023SJohn Marino       altclass = reg_alternate_class (i);
653*e4b17023SJohn Marino       if (rclass != GENERAL_REGS || altclass != ALL_REGS)
654*e4b17023SJohn Marino 	{
655*e4b17023SJohn Marino 	  if (altclass == ALL_REGS || rclass == ALL_REGS)
656*e4b17023SJohn Marino 	    fprintf (file, "; pref %s", reg_class_names[(int) rclass]);
657*e4b17023SJohn Marino 	  else if (altclass == NO_REGS)
658*e4b17023SJohn Marino 	    fprintf (file, "; %s or none", reg_class_names[(int) rclass]);
659*e4b17023SJohn Marino 	  else
660*e4b17023SJohn Marino 	    fprintf (file, "; pref %s, else %s",
661*e4b17023SJohn Marino 		     reg_class_names[(int) rclass],
662*e4b17023SJohn Marino 		     reg_class_names[(int) altclass]);
663*e4b17023SJohn Marino 	}
664*e4b17023SJohn Marino 
665*e4b17023SJohn Marino       if (regno_reg_rtx[i] != NULL && REG_POINTER (regno_reg_rtx[i]))
666*e4b17023SJohn Marino 	fputs ("; pointer", file);
667*e4b17023SJohn Marino       fputs (".\n", file);
668*e4b17023SJohn Marino     }
669*e4b17023SJohn Marino }
670*e4b17023SJohn Marino 
671*e4b17023SJohn Marino 
672*e4b17023SJohn Marino void
dump_flow_info(FILE * file,int flags)673*e4b17023SJohn Marino dump_flow_info (FILE *file, int flags)
674*e4b17023SJohn Marino {
675*e4b17023SJohn Marino   basic_block bb;
676*e4b17023SJohn Marino 
677*e4b17023SJohn Marino   /* There are no pseudo registers after reload.  Don't dump them.  */
678*e4b17023SJohn Marino   if (reg_info_p_size && (flags & TDF_DETAILS) != 0)
679*e4b17023SJohn Marino     dump_reg_info (file);
680*e4b17023SJohn Marino 
681*e4b17023SJohn Marino   fprintf (file, "\n%d basic blocks, %d edges.\n", n_basic_blocks, n_edges);
682*e4b17023SJohn Marino   FOR_ALL_BB (bb)
683*e4b17023SJohn Marino     {
684*e4b17023SJohn Marino       dump_bb_info (bb, true, true, flags, "", file);
685*e4b17023SJohn Marino       check_bb_profile (bb, file);
686*e4b17023SJohn Marino     }
687*e4b17023SJohn Marino 
688*e4b17023SJohn Marino   putc ('\n', file);
689*e4b17023SJohn Marino }
690*e4b17023SJohn Marino 
691*e4b17023SJohn Marino DEBUG_FUNCTION void
debug_flow_info(void)692*e4b17023SJohn Marino debug_flow_info (void)
693*e4b17023SJohn Marino {
694*e4b17023SJohn Marino   dump_flow_info (stderr, TDF_DETAILS);
695*e4b17023SJohn Marino }
696*e4b17023SJohn Marino 
697*e4b17023SJohn Marino void
dump_edge_info(FILE * file,edge e,int do_succ)698*e4b17023SJohn Marino dump_edge_info (FILE *file, edge e, int do_succ)
699*e4b17023SJohn Marino {
700*e4b17023SJohn Marino   basic_block side = (do_succ ? e->dest : e->src);
701*e4b17023SJohn Marino   /* both ENTRY_BLOCK_PTR & EXIT_BLOCK_PTR depend upon cfun. */
702*e4b17023SJohn Marino   if (cfun && side == ENTRY_BLOCK_PTR)
703*e4b17023SJohn Marino     fputs (" ENTRY", file);
704*e4b17023SJohn Marino   else if (cfun && side == EXIT_BLOCK_PTR)
705*e4b17023SJohn Marino     fputs (" EXIT", file);
706*e4b17023SJohn Marino   else
707*e4b17023SJohn Marino     fprintf (file, " %d", side->index);
708*e4b17023SJohn Marino 
709*e4b17023SJohn Marino   if (e->probability)
710*e4b17023SJohn Marino     fprintf (file, " [%.1f%%] ", e->probability * 100.0 / REG_BR_PROB_BASE);
711*e4b17023SJohn Marino 
712*e4b17023SJohn Marino   if (e->count)
713*e4b17023SJohn Marino     {
714*e4b17023SJohn Marino       fputs (" count:", file);
715*e4b17023SJohn Marino       fprintf (file, HOST_WIDEST_INT_PRINT_DEC, e->count);
716*e4b17023SJohn Marino     }
717*e4b17023SJohn Marino 
718*e4b17023SJohn Marino   if (e->flags)
719*e4b17023SJohn Marino     {
720*e4b17023SJohn Marino       static const char * const bitnames[] = {
721*e4b17023SJohn Marino 	"fallthru", "ab", "abcall", "eh", "fake", "dfs_back",
722*e4b17023SJohn Marino 	"can_fallthru", "irreducible", "sibcall", "loop_exit",
723*e4b17023SJohn Marino 	"true", "false", "exec", "crossing", "preserve"
724*e4b17023SJohn Marino       };
725*e4b17023SJohn Marino       int comma = 0;
726*e4b17023SJohn Marino       int i, flags = e->flags;
727*e4b17023SJohn Marino 
728*e4b17023SJohn Marino       fputs (" (", file);
729*e4b17023SJohn Marino       for (i = 0; flags; i++)
730*e4b17023SJohn Marino 	if (flags & (1 << i))
731*e4b17023SJohn Marino 	  {
732*e4b17023SJohn Marino 	    flags &= ~(1 << i);
733*e4b17023SJohn Marino 
734*e4b17023SJohn Marino 	    if (comma)
735*e4b17023SJohn Marino 	      fputc (',', file);
736*e4b17023SJohn Marino 	    if (i < (int) ARRAY_SIZE (bitnames))
737*e4b17023SJohn Marino 	      fputs (bitnames[i], file);
738*e4b17023SJohn Marino 	    else
739*e4b17023SJohn Marino 	      fprintf (file, "%d", i);
740*e4b17023SJohn Marino 	    comma = 1;
741*e4b17023SJohn Marino 	  }
742*e4b17023SJohn Marino 
743*e4b17023SJohn Marino       fputc (')', file);
744*e4b17023SJohn Marino     }
745*e4b17023SJohn Marino }
746*e4b17023SJohn Marino 
747*e4b17023SJohn Marino /* Simple routines to easily allocate AUX fields of basic blocks.  */
748*e4b17023SJohn Marino 
749*e4b17023SJohn Marino static struct obstack block_aux_obstack;
750*e4b17023SJohn Marino static void *first_block_aux_obj = 0;
751*e4b17023SJohn Marino static struct obstack edge_aux_obstack;
752*e4b17023SJohn Marino static void *first_edge_aux_obj = 0;
753*e4b17023SJohn Marino 
754*e4b17023SJohn Marino /* Allocate a memory block of SIZE as BB->aux.  The obstack must
755*e4b17023SJohn Marino    be first initialized by alloc_aux_for_blocks.  */
756*e4b17023SJohn Marino 
757*e4b17023SJohn Marino static void
alloc_aux_for_block(basic_block bb,int size)758*e4b17023SJohn Marino alloc_aux_for_block (basic_block bb, int size)
759*e4b17023SJohn Marino {
760*e4b17023SJohn Marino   /* Verify that aux field is clear.  */
761*e4b17023SJohn Marino   gcc_assert (!bb->aux && first_block_aux_obj);
762*e4b17023SJohn Marino   bb->aux = obstack_alloc (&block_aux_obstack, size);
763*e4b17023SJohn Marino   memset (bb->aux, 0, size);
764*e4b17023SJohn Marino }
765*e4b17023SJohn Marino 
766*e4b17023SJohn Marino /* Initialize the block_aux_obstack and if SIZE is nonzero, call
767*e4b17023SJohn Marino    alloc_aux_for_block for each basic block.  */
768*e4b17023SJohn Marino 
769*e4b17023SJohn Marino void
alloc_aux_for_blocks(int size)770*e4b17023SJohn Marino alloc_aux_for_blocks (int size)
771*e4b17023SJohn Marino {
772*e4b17023SJohn Marino   static int initialized;
773*e4b17023SJohn Marino 
774*e4b17023SJohn Marino   if (!initialized)
775*e4b17023SJohn Marino     {
776*e4b17023SJohn Marino       gcc_obstack_init (&block_aux_obstack);
777*e4b17023SJohn Marino       initialized = 1;
778*e4b17023SJohn Marino     }
779*e4b17023SJohn Marino   else
780*e4b17023SJohn Marino     /* Check whether AUX data are still allocated.  */
781*e4b17023SJohn Marino     gcc_assert (!first_block_aux_obj);
782*e4b17023SJohn Marino 
783*e4b17023SJohn Marino   first_block_aux_obj = obstack_alloc (&block_aux_obstack, 0);
784*e4b17023SJohn Marino   if (size)
785*e4b17023SJohn Marino     {
786*e4b17023SJohn Marino       basic_block bb;
787*e4b17023SJohn Marino 
788*e4b17023SJohn Marino       FOR_ALL_BB (bb)
789*e4b17023SJohn Marino 	alloc_aux_for_block (bb, size);
790*e4b17023SJohn Marino     }
791*e4b17023SJohn Marino }
792*e4b17023SJohn Marino 
793*e4b17023SJohn Marino /* Clear AUX pointers of all blocks.  */
794*e4b17023SJohn Marino 
795*e4b17023SJohn Marino void
clear_aux_for_blocks(void)796*e4b17023SJohn Marino clear_aux_for_blocks (void)
797*e4b17023SJohn Marino {
798*e4b17023SJohn Marino   basic_block bb;
799*e4b17023SJohn Marino 
800*e4b17023SJohn Marino   FOR_ALL_BB (bb)
801*e4b17023SJohn Marino     bb->aux = NULL;
802*e4b17023SJohn Marino }
803*e4b17023SJohn Marino 
804*e4b17023SJohn Marino /* Free data allocated in block_aux_obstack and clear AUX pointers
805*e4b17023SJohn Marino    of all blocks.  */
806*e4b17023SJohn Marino 
807*e4b17023SJohn Marino void
free_aux_for_blocks(void)808*e4b17023SJohn Marino free_aux_for_blocks (void)
809*e4b17023SJohn Marino {
810*e4b17023SJohn Marino   gcc_assert (first_block_aux_obj);
811*e4b17023SJohn Marino   obstack_free (&block_aux_obstack, first_block_aux_obj);
812*e4b17023SJohn Marino   first_block_aux_obj = NULL;
813*e4b17023SJohn Marino 
814*e4b17023SJohn Marino   clear_aux_for_blocks ();
815*e4b17023SJohn Marino }
816*e4b17023SJohn Marino 
817*e4b17023SJohn Marino /* Allocate a memory edge of SIZE as E->aux.  The obstack must
818*e4b17023SJohn Marino    be first initialized by alloc_aux_for_edges.  */
819*e4b17023SJohn Marino 
820*e4b17023SJohn Marino void
alloc_aux_for_edge(edge e,int size)821*e4b17023SJohn Marino alloc_aux_for_edge (edge e, int size)
822*e4b17023SJohn Marino {
823*e4b17023SJohn Marino   /* Verify that aux field is clear.  */
824*e4b17023SJohn Marino   gcc_assert (!e->aux && first_edge_aux_obj);
825*e4b17023SJohn Marino   e->aux = obstack_alloc (&edge_aux_obstack, size);
826*e4b17023SJohn Marino   memset (e->aux, 0, size);
827*e4b17023SJohn Marino }
828*e4b17023SJohn Marino 
829*e4b17023SJohn Marino /* Initialize the edge_aux_obstack and if SIZE is nonzero, call
830*e4b17023SJohn Marino    alloc_aux_for_edge for each basic edge.  */
831*e4b17023SJohn Marino 
832*e4b17023SJohn Marino void
alloc_aux_for_edges(int size)833*e4b17023SJohn Marino alloc_aux_for_edges (int size)
834*e4b17023SJohn Marino {
835*e4b17023SJohn Marino   static int initialized;
836*e4b17023SJohn Marino 
837*e4b17023SJohn Marino   if (!initialized)
838*e4b17023SJohn Marino     {
839*e4b17023SJohn Marino       gcc_obstack_init (&edge_aux_obstack);
840*e4b17023SJohn Marino       initialized = 1;
841*e4b17023SJohn Marino     }
842*e4b17023SJohn Marino   else
843*e4b17023SJohn Marino     /* Check whether AUX data are still allocated.  */
844*e4b17023SJohn Marino     gcc_assert (!first_edge_aux_obj);
845*e4b17023SJohn Marino 
846*e4b17023SJohn Marino   first_edge_aux_obj = obstack_alloc (&edge_aux_obstack, 0);
847*e4b17023SJohn Marino   if (size)
848*e4b17023SJohn Marino     {
849*e4b17023SJohn Marino       basic_block bb;
850*e4b17023SJohn Marino 
851*e4b17023SJohn Marino       FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
852*e4b17023SJohn Marino 	{
853*e4b17023SJohn Marino 	  edge e;
854*e4b17023SJohn Marino 	  edge_iterator ei;
855*e4b17023SJohn Marino 
856*e4b17023SJohn Marino 	  FOR_EACH_EDGE (e, ei, bb->succs)
857*e4b17023SJohn Marino 	    alloc_aux_for_edge (e, size);
858*e4b17023SJohn Marino 	}
859*e4b17023SJohn Marino     }
860*e4b17023SJohn Marino }
861*e4b17023SJohn Marino 
862*e4b17023SJohn Marino /* Clear AUX pointers of all edges.  */
863*e4b17023SJohn Marino 
864*e4b17023SJohn Marino void
clear_aux_for_edges(void)865*e4b17023SJohn Marino clear_aux_for_edges (void)
866*e4b17023SJohn Marino {
867*e4b17023SJohn Marino   basic_block bb;
868*e4b17023SJohn Marino   edge e;
869*e4b17023SJohn Marino 
870*e4b17023SJohn Marino   FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
871*e4b17023SJohn Marino     {
872*e4b17023SJohn Marino       edge_iterator ei;
873*e4b17023SJohn Marino       FOR_EACH_EDGE (e, ei, bb->succs)
874*e4b17023SJohn Marino 	e->aux = NULL;
875*e4b17023SJohn Marino     }
876*e4b17023SJohn Marino }
877*e4b17023SJohn Marino 
878*e4b17023SJohn Marino /* Free data allocated in edge_aux_obstack and clear AUX pointers
879*e4b17023SJohn Marino    of all edges.  */
880*e4b17023SJohn Marino 
881*e4b17023SJohn Marino void
free_aux_for_edges(void)882*e4b17023SJohn Marino free_aux_for_edges (void)
883*e4b17023SJohn Marino {
884*e4b17023SJohn Marino   gcc_assert (first_edge_aux_obj);
885*e4b17023SJohn Marino   obstack_free (&edge_aux_obstack, first_edge_aux_obj);
886*e4b17023SJohn Marino   first_edge_aux_obj = NULL;
887*e4b17023SJohn Marino 
888*e4b17023SJohn Marino   clear_aux_for_edges ();
889*e4b17023SJohn Marino }
890*e4b17023SJohn Marino 
891*e4b17023SJohn Marino DEBUG_FUNCTION void
debug_bb(basic_block bb)892*e4b17023SJohn Marino debug_bb (basic_block bb)
893*e4b17023SJohn Marino {
894*e4b17023SJohn Marino   dump_bb (bb, stderr, 0);
895*e4b17023SJohn Marino }
896*e4b17023SJohn Marino 
897*e4b17023SJohn Marino DEBUG_FUNCTION basic_block
debug_bb_n(int n)898*e4b17023SJohn Marino debug_bb_n (int n)
899*e4b17023SJohn Marino {
900*e4b17023SJohn Marino   basic_block bb = BASIC_BLOCK (n);
901*e4b17023SJohn Marino   dump_bb (bb, stderr, 0);
902*e4b17023SJohn Marino   return bb;
903*e4b17023SJohn Marino }
904*e4b17023SJohn Marino 
905*e4b17023SJohn Marino /* Dumps cfg related information about basic block BB to FILE.  */
906*e4b17023SJohn Marino 
907*e4b17023SJohn Marino static void
dump_cfg_bb_info(FILE * file,basic_block bb)908*e4b17023SJohn Marino dump_cfg_bb_info (FILE *file, basic_block bb)
909*e4b17023SJohn Marino {
910*e4b17023SJohn Marino   unsigned i;
911*e4b17023SJohn Marino   edge_iterator ei;
912*e4b17023SJohn Marino   bool first = true;
913*e4b17023SJohn Marino   static const char * const bb_bitnames[] =
914*e4b17023SJohn Marino     {
915*e4b17023SJohn Marino       "new", "reachable", "irreducible_loop", "superblock",
916*e4b17023SJohn Marino       "nosched", "hot", "cold", "dup", "xlabel", "rtl",
917*e4b17023SJohn Marino       "fwdr", "nothrd"
918*e4b17023SJohn Marino     };
919*e4b17023SJohn Marino   const unsigned n_bitnames = sizeof (bb_bitnames) / sizeof (char *);
920*e4b17023SJohn Marino   edge e;
921*e4b17023SJohn Marino 
922*e4b17023SJohn Marino   fprintf (file, "Basic block %d", bb->index);
923*e4b17023SJohn Marino   for (i = 0; i < n_bitnames; i++)
924*e4b17023SJohn Marino     if (bb->flags & (1 << i))
925*e4b17023SJohn Marino       {
926*e4b17023SJohn Marino 	if (first)
927*e4b17023SJohn Marino 	  fputs (" (", file);
928*e4b17023SJohn Marino 	else
929*e4b17023SJohn Marino 	  fputs (", ", file);
930*e4b17023SJohn Marino 	first = false;
931*e4b17023SJohn Marino 	fputs (bb_bitnames[i], file);
932*e4b17023SJohn Marino       }
933*e4b17023SJohn Marino   if (!first)
934*e4b17023SJohn Marino     putc (')', file);
935*e4b17023SJohn Marino   putc ('\n', file);
936*e4b17023SJohn Marino 
937*e4b17023SJohn Marino   fputs ("Predecessors: ", file);
938*e4b17023SJohn Marino   FOR_EACH_EDGE (e, ei, bb->preds)
939*e4b17023SJohn Marino     dump_edge_info (file, e, 0);
940*e4b17023SJohn Marino 
941*e4b17023SJohn Marino   fprintf (file, "\nSuccessors: ");
942*e4b17023SJohn Marino   FOR_EACH_EDGE (e, ei, bb->succs)
943*e4b17023SJohn Marino     dump_edge_info (file, e, 1);
944*e4b17023SJohn Marino   fputs ("\n\n", file);
945*e4b17023SJohn Marino }
946*e4b17023SJohn Marino 
947*e4b17023SJohn Marino /* Dumps a brief description of cfg to FILE.  */
948*e4b17023SJohn Marino 
949*e4b17023SJohn Marino void
brief_dump_cfg(FILE * file)950*e4b17023SJohn Marino brief_dump_cfg (FILE *file)
951*e4b17023SJohn Marino {
952*e4b17023SJohn Marino   basic_block bb;
953*e4b17023SJohn Marino 
954*e4b17023SJohn Marino   FOR_EACH_BB (bb)
955*e4b17023SJohn Marino     {
956*e4b17023SJohn Marino       dump_cfg_bb_info (file, bb);
957*e4b17023SJohn Marino     }
958*e4b17023SJohn Marino }
959*e4b17023SJohn Marino 
960*e4b17023SJohn Marino /* An edge originally destinating BB of FREQUENCY and COUNT has been proved to
961*e4b17023SJohn Marino    leave the block by TAKEN_EDGE.  Update profile of BB such that edge E can be
962*e4b17023SJohn Marino    redirected to destination of TAKEN_EDGE.
963*e4b17023SJohn Marino 
964*e4b17023SJohn Marino    This function may leave the profile inconsistent in the case TAKEN_EDGE
965*e4b17023SJohn Marino    frequency or count is believed to be lower than FREQUENCY or COUNT
966*e4b17023SJohn Marino    respectively.  */
967*e4b17023SJohn Marino void
update_bb_profile_for_threading(basic_block bb,int edge_frequency,gcov_type count,edge taken_edge)968*e4b17023SJohn Marino update_bb_profile_for_threading (basic_block bb, int edge_frequency,
969*e4b17023SJohn Marino 				 gcov_type count, edge taken_edge)
970*e4b17023SJohn Marino {
971*e4b17023SJohn Marino   edge c;
972*e4b17023SJohn Marino   int prob;
973*e4b17023SJohn Marino   edge_iterator ei;
974*e4b17023SJohn Marino 
975*e4b17023SJohn Marino   bb->count -= count;
976*e4b17023SJohn Marino   if (bb->count < 0)
977*e4b17023SJohn Marino     {
978*e4b17023SJohn Marino       if (dump_file)
979*e4b17023SJohn Marino 	fprintf (dump_file, "bb %i count became negative after threading",
980*e4b17023SJohn Marino 		 bb->index);
981*e4b17023SJohn Marino       bb->count = 0;
982*e4b17023SJohn Marino     }
983*e4b17023SJohn Marino 
984*e4b17023SJohn Marino   /* Compute the probability of TAKEN_EDGE being reached via threaded edge.
985*e4b17023SJohn Marino      Watch for overflows.  */
986*e4b17023SJohn Marino   if (bb->frequency)
987*e4b17023SJohn Marino     prob = edge_frequency * REG_BR_PROB_BASE / bb->frequency;
988*e4b17023SJohn Marino   else
989*e4b17023SJohn Marino     prob = 0;
990*e4b17023SJohn Marino   if (prob > taken_edge->probability)
991*e4b17023SJohn Marino     {
992*e4b17023SJohn Marino       if (dump_file)
993*e4b17023SJohn Marino 	fprintf (dump_file, "Jump threading proved probability of edge "
994*e4b17023SJohn Marino 		 "%i->%i too small (it is %i, should be %i).\n",
995*e4b17023SJohn Marino 		 taken_edge->src->index, taken_edge->dest->index,
996*e4b17023SJohn Marino 		 taken_edge->probability, prob);
997*e4b17023SJohn Marino       prob = taken_edge->probability;
998*e4b17023SJohn Marino     }
999*e4b17023SJohn Marino 
1000*e4b17023SJohn Marino   /* Now rescale the probabilities.  */
1001*e4b17023SJohn Marino   taken_edge->probability -= prob;
1002*e4b17023SJohn Marino   prob = REG_BR_PROB_BASE - prob;
1003*e4b17023SJohn Marino   bb->frequency -= edge_frequency;
1004*e4b17023SJohn Marino   if (bb->frequency < 0)
1005*e4b17023SJohn Marino     bb->frequency = 0;
1006*e4b17023SJohn Marino   if (prob <= 0)
1007*e4b17023SJohn Marino     {
1008*e4b17023SJohn Marino       if (dump_file)
1009*e4b17023SJohn Marino 	fprintf (dump_file, "Edge frequencies of bb %i has been reset, "
1010*e4b17023SJohn Marino 		 "frequency of block should end up being 0, it is %i\n",
1011*e4b17023SJohn Marino 		 bb->index, bb->frequency);
1012*e4b17023SJohn Marino       EDGE_SUCC (bb, 0)->probability = REG_BR_PROB_BASE;
1013*e4b17023SJohn Marino       ei = ei_start (bb->succs);
1014*e4b17023SJohn Marino       ei_next (&ei);
1015*e4b17023SJohn Marino       for (; (c = ei_safe_edge (ei)); ei_next (&ei))
1016*e4b17023SJohn Marino 	c->probability = 0;
1017*e4b17023SJohn Marino     }
1018*e4b17023SJohn Marino   else if (prob != REG_BR_PROB_BASE)
1019*e4b17023SJohn Marino     {
1020*e4b17023SJohn Marino       int scale = RDIV (65536 * REG_BR_PROB_BASE, prob);
1021*e4b17023SJohn Marino 
1022*e4b17023SJohn Marino       FOR_EACH_EDGE (c, ei, bb->succs)
1023*e4b17023SJohn Marino 	{
1024*e4b17023SJohn Marino 	  /* Protect from overflow due to additional scaling.  */
1025*e4b17023SJohn Marino 	  if (c->probability > prob)
1026*e4b17023SJohn Marino 	    c->probability = REG_BR_PROB_BASE;
1027*e4b17023SJohn Marino 	  else
1028*e4b17023SJohn Marino 	    {
1029*e4b17023SJohn Marino 	      c->probability = RDIV (c->probability * scale, 65536);
1030*e4b17023SJohn Marino 	      if (c->probability > REG_BR_PROB_BASE)
1031*e4b17023SJohn Marino 		c->probability = REG_BR_PROB_BASE;
1032*e4b17023SJohn Marino 	    }
1033*e4b17023SJohn Marino 	}
1034*e4b17023SJohn Marino     }
1035*e4b17023SJohn Marino 
1036*e4b17023SJohn Marino   gcc_assert (bb == taken_edge->src);
1037*e4b17023SJohn Marino   taken_edge->count -= count;
1038*e4b17023SJohn Marino   if (taken_edge->count < 0)
1039*e4b17023SJohn Marino     {
1040*e4b17023SJohn Marino       if (dump_file)
1041*e4b17023SJohn Marino 	fprintf (dump_file, "edge %i->%i count became negative after threading",
1042*e4b17023SJohn Marino 		 taken_edge->src->index, taken_edge->dest->index);
1043*e4b17023SJohn Marino       taken_edge->count = 0;
1044*e4b17023SJohn Marino     }
1045*e4b17023SJohn Marino }
1046*e4b17023SJohn Marino 
1047*e4b17023SJohn Marino /* Multiply all frequencies of basic blocks in array BBS of length NBBS
1048*e4b17023SJohn Marino    by NUM/DEN, in int arithmetic.  May lose some accuracy.  */
1049*e4b17023SJohn Marino void
scale_bbs_frequencies_int(basic_block * bbs,int nbbs,int num,int den)1050*e4b17023SJohn Marino scale_bbs_frequencies_int (basic_block *bbs, int nbbs, int num, int den)
1051*e4b17023SJohn Marino {
1052*e4b17023SJohn Marino   int i;
1053*e4b17023SJohn Marino   edge e;
1054*e4b17023SJohn Marino   if (num < 0)
1055*e4b17023SJohn Marino     num = 0;
1056*e4b17023SJohn Marino 
1057*e4b17023SJohn Marino   /* Scale NUM and DEN to avoid overflows.  Frequencies are in order of
1058*e4b17023SJohn Marino      10^4, if we make DEN <= 10^3, we can afford to upscale by 100
1059*e4b17023SJohn Marino      and still safely fit in int during calculations.  */
1060*e4b17023SJohn Marino   if (den > 1000)
1061*e4b17023SJohn Marino     {
1062*e4b17023SJohn Marino       if (num > 1000000)
1063*e4b17023SJohn Marino 	return;
1064*e4b17023SJohn Marino 
1065*e4b17023SJohn Marino       num = RDIV (1000 * num, den);
1066*e4b17023SJohn Marino       den = 1000;
1067*e4b17023SJohn Marino     }
1068*e4b17023SJohn Marino   if (num > 100 * den)
1069*e4b17023SJohn Marino     return;
1070*e4b17023SJohn Marino 
1071*e4b17023SJohn Marino   for (i = 0; i < nbbs; i++)
1072*e4b17023SJohn Marino     {
1073*e4b17023SJohn Marino       edge_iterator ei;
1074*e4b17023SJohn Marino       bbs[i]->frequency = RDIV (bbs[i]->frequency * num, den);
1075*e4b17023SJohn Marino       /* Make sure the frequencies do not grow over BB_FREQ_MAX.  */
1076*e4b17023SJohn Marino       if (bbs[i]->frequency > BB_FREQ_MAX)
1077*e4b17023SJohn Marino 	bbs[i]->frequency = BB_FREQ_MAX;
1078*e4b17023SJohn Marino       bbs[i]->count = RDIV (bbs[i]->count * num, den);
1079*e4b17023SJohn Marino       FOR_EACH_EDGE (e, ei, bbs[i]->succs)
1080*e4b17023SJohn Marino 	e->count = RDIV (e->count * num, den);
1081*e4b17023SJohn Marino     }
1082*e4b17023SJohn Marino }
1083*e4b17023SJohn Marino 
1084*e4b17023SJohn Marino /* numbers smaller than this value are safe to multiply without getting
1085*e4b17023SJohn Marino    64bit overflow.  */
1086*e4b17023SJohn Marino #define MAX_SAFE_MULTIPLIER (1 << (sizeof (HOST_WIDEST_INT) * 4 - 1))
1087*e4b17023SJohn Marino 
1088*e4b17023SJohn Marino /* Multiply all frequencies of basic blocks in array BBS of length NBBS
1089*e4b17023SJohn Marino    by NUM/DEN, in gcov_type arithmetic.  More accurate than previous
1090*e4b17023SJohn Marino    function but considerably slower.  */
1091*e4b17023SJohn Marino void
scale_bbs_frequencies_gcov_type(basic_block * bbs,int nbbs,gcov_type num,gcov_type den)1092*e4b17023SJohn Marino scale_bbs_frequencies_gcov_type (basic_block *bbs, int nbbs, gcov_type num,
1093*e4b17023SJohn Marino 				 gcov_type den)
1094*e4b17023SJohn Marino {
1095*e4b17023SJohn Marino   int i;
1096*e4b17023SJohn Marino   edge e;
1097*e4b17023SJohn Marino   gcov_type fraction = RDIV (num * 65536, den);
1098*e4b17023SJohn Marino 
1099*e4b17023SJohn Marino   gcc_assert (fraction >= 0);
1100*e4b17023SJohn Marino 
1101*e4b17023SJohn Marino   if (num < MAX_SAFE_MULTIPLIER)
1102*e4b17023SJohn Marino     for (i = 0; i < nbbs; i++)
1103*e4b17023SJohn Marino       {
1104*e4b17023SJohn Marino 	edge_iterator ei;
1105*e4b17023SJohn Marino 	bbs[i]->frequency = RDIV (bbs[i]->frequency * num, den);
1106*e4b17023SJohn Marino 	if (bbs[i]->count <= MAX_SAFE_MULTIPLIER)
1107*e4b17023SJohn Marino 	  bbs[i]->count = RDIV (bbs[i]->count * num, den);
1108*e4b17023SJohn Marino 	else
1109*e4b17023SJohn Marino 	  bbs[i]->count = RDIV (bbs[i]->count * fraction, 65536);
1110*e4b17023SJohn Marino 	FOR_EACH_EDGE (e, ei, bbs[i]->succs)
1111*e4b17023SJohn Marino 	  if (bbs[i]->count <= MAX_SAFE_MULTIPLIER)
1112*e4b17023SJohn Marino 	    e->count = RDIV (e->count * num, den);
1113*e4b17023SJohn Marino 	  else
1114*e4b17023SJohn Marino 	    e->count = RDIV (e->count * fraction, 65536);
1115*e4b17023SJohn Marino       }
1116*e4b17023SJohn Marino    else
1117*e4b17023SJohn Marino     for (i = 0; i < nbbs; i++)
1118*e4b17023SJohn Marino       {
1119*e4b17023SJohn Marino 	edge_iterator ei;
1120*e4b17023SJohn Marino 	if (sizeof (gcov_type) > sizeof (int))
1121*e4b17023SJohn Marino 	  bbs[i]->frequency = RDIV (bbs[i]->frequency * num, den);
1122*e4b17023SJohn Marino 	else
1123*e4b17023SJohn Marino 	  bbs[i]->frequency = RDIV (bbs[i]->frequency * fraction, 65536);
1124*e4b17023SJohn Marino 	bbs[i]->count = RDIV (bbs[i]->count * fraction, 65536);
1125*e4b17023SJohn Marino 	FOR_EACH_EDGE (e, ei, bbs[i]->succs)
1126*e4b17023SJohn Marino 	  e->count = RDIV (e->count * fraction, 65536);
1127*e4b17023SJohn Marino       }
1128*e4b17023SJohn Marino }
1129*e4b17023SJohn Marino 
1130*e4b17023SJohn Marino /* Data structures used to maintain mapping between basic blocks and
1131*e4b17023SJohn Marino    copies.  */
1132*e4b17023SJohn Marino static htab_t bb_original;
1133*e4b17023SJohn Marino static htab_t bb_copy;
1134*e4b17023SJohn Marino 
1135*e4b17023SJohn Marino /* And between loops and copies.  */
1136*e4b17023SJohn Marino static htab_t loop_copy;
1137*e4b17023SJohn Marino static alloc_pool original_copy_bb_pool;
1138*e4b17023SJohn Marino 
1139*e4b17023SJohn Marino struct htab_bb_copy_original_entry
1140*e4b17023SJohn Marino {
1141*e4b17023SJohn Marino   /* Block we are attaching info to.  */
1142*e4b17023SJohn Marino   int index1;
1143*e4b17023SJohn Marino   /* Index of original or copy (depending on the hashtable) */
1144*e4b17023SJohn Marino   int index2;
1145*e4b17023SJohn Marino };
1146*e4b17023SJohn Marino 
1147*e4b17023SJohn Marino static hashval_t
bb_copy_original_hash(const void * p)1148*e4b17023SJohn Marino bb_copy_original_hash (const void *p)
1149*e4b17023SJohn Marino {
1150*e4b17023SJohn Marino   const struct htab_bb_copy_original_entry *data
1151*e4b17023SJohn Marino     = ((const struct htab_bb_copy_original_entry *)p);
1152*e4b17023SJohn Marino 
1153*e4b17023SJohn Marino   return data->index1;
1154*e4b17023SJohn Marino }
1155*e4b17023SJohn Marino static int
bb_copy_original_eq(const void * p,const void * q)1156*e4b17023SJohn Marino bb_copy_original_eq (const void *p, const void *q)
1157*e4b17023SJohn Marino {
1158*e4b17023SJohn Marino   const struct htab_bb_copy_original_entry *data
1159*e4b17023SJohn Marino     = ((const struct htab_bb_copy_original_entry *)p);
1160*e4b17023SJohn Marino   const struct htab_bb_copy_original_entry *data2
1161*e4b17023SJohn Marino     = ((const struct htab_bb_copy_original_entry *)q);
1162*e4b17023SJohn Marino 
1163*e4b17023SJohn Marino   return data->index1 == data2->index1;
1164*e4b17023SJohn Marino }
1165*e4b17023SJohn Marino 
1166*e4b17023SJohn Marino /* Initialize the data structures to maintain mapping between blocks
1167*e4b17023SJohn Marino    and its copies.  */
1168*e4b17023SJohn Marino void
initialize_original_copy_tables(void)1169*e4b17023SJohn Marino initialize_original_copy_tables (void)
1170*e4b17023SJohn Marino {
1171*e4b17023SJohn Marino   gcc_assert (!original_copy_bb_pool);
1172*e4b17023SJohn Marino   original_copy_bb_pool
1173*e4b17023SJohn Marino     = create_alloc_pool ("original_copy",
1174*e4b17023SJohn Marino 			 sizeof (struct htab_bb_copy_original_entry), 10);
1175*e4b17023SJohn Marino   bb_original = htab_create (10, bb_copy_original_hash,
1176*e4b17023SJohn Marino 			     bb_copy_original_eq, NULL);
1177*e4b17023SJohn Marino   bb_copy = htab_create (10, bb_copy_original_hash, bb_copy_original_eq, NULL);
1178*e4b17023SJohn Marino   loop_copy = htab_create (10, bb_copy_original_hash, bb_copy_original_eq, NULL);
1179*e4b17023SJohn Marino }
1180*e4b17023SJohn Marino 
1181*e4b17023SJohn Marino /* Free the data structures to maintain mapping between blocks and
1182*e4b17023SJohn Marino    its copies.  */
1183*e4b17023SJohn Marino void
free_original_copy_tables(void)1184*e4b17023SJohn Marino free_original_copy_tables (void)
1185*e4b17023SJohn Marino {
1186*e4b17023SJohn Marino   gcc_assert (original_copy_bb_pool);
1187*e4b17023SJohn Marino   htab_delete (bb_copy);
1188*e4b17023SJohn Marino   htab_delete (bb_original);
1189*e4b17023SJohn Marino   htab_delete (loop_copy);
1190*e4b17023SJohn Marino   free_alloc_pool (original_copy_bb_pool);
1191*e4b17023SJohn Marino   bb_copy = NULL;
1192*e4b17023SJohn Marino   bb_original = NULL;
1193*e4b17023SJohn Marino   loop_copy = NULL;
1194*e4b17023SJohn Marino   original_copy_bb_pool = NULL;
1195*e4b17023SJohn Marino }
1196*e4b17023SJohn Marino 
1197*e4b17023SJohn Marino /* Removes the value associated with OBJ from table TAB.  */
1198*e4b17023SJohn Marino 
1199*e4b17023SJohn Marino static void
copy_original_table_clear(htab_t tab,unsigned obj)1200*e4b17023SJohn Marino copy_original_table_clear (htab_t tab, unsigned obj)
1201*e4b17023SJohn Marino {
1202*e4b17023SJohn Marino   void **slot;
1203*e4b17023SJohn Marino   struct htab_bb_copy_original_entry key, *elt;
1204*e4b17023SJohn Marino 
1205*e4b17023SJohn Marino   if (!original_copy_bb_pool)
1206*e4b17023SJohn Marino     return;
1207*e4b17023SJohn Marino 
1208*e4b17023SJohn Marino   key.index1 = obj;
1209*e4b17023SJohn Marino   slot = htab_find_slot (tab, &key, NO_INSERT);
1210*e4b17023SJohn Marino   if (!slot)
1211*e4b17023SJohn Marino     return;
1212*e4b17023SJohn Marino 
1213*e4b17023SJohn Marino   elt = (struct htab_bb_copy_original_entry *) *slot;
1214*e4b17023SJohn Marino   htab_clear_slot (tab, slot);
1215*e4b17023SJohn Marino   pool_free (original_copy_bb_pool, elt);
1216*e4b17023SJohn Marino }
1217*e4b17023SJohn Marino 
1218*e4b17023SJohn Marino /* Sets the value associated with OBJ in table TAB to VAL.
1219*e4b17023SJohn Marino    Do nothing when data structures are not initialized.  */
1220*e4b17023SJohn Marino 
1221*e4b17023SJohn Marino static void
copy_original_table_set(htab_t tab,unsigned obj,unsigned val)1222*e4b17023SJohn Marino copy_original_table_set (htab_t tab, unsigned obj, unsigned val)
1223*e4b17023SJohn Marino {
1224*e4b17023SJohn Marino   struct htab_bb_copy_original_entry **slot;
1225*e4b17023SJohn Marino   struct htab_bb_copy_original_entry key;
1226*e4b17023SJohn Marino 
1227*e4b17023SJohn Marino   if (!original_copy_bb_pool)
1228*e4b17023SJohn Marino     return;
1229*e4b17023SJohn Marino 
1230*e4b17023SJohn Marino   key.index1 = obj;
1231*e4b17023SJohn Marino   slot = (struct htab_bb_copy_original_entry **)
1232*e4b17023SJohn Marino 		htab_find_slot (tab, &key, INSERT);
1233*e4b17023SJohn Marino   if (!*slot)
1234*e4b17023SJohn Marino     {
1235*e4b17023SJohn Marino       *slot = (struct htab_bb_copy_original_entry *)
1236*e4b17023SJohn Marino 		pool_alloc (original_copy_bb_pool);
1237*e4b17023SJohn Marino       (*slot)->index1 = obj;
1238*e4b17023SJohn Marino     }
1239*e4b17023SJohn Marino   (*slot)->index2 = val;
1240*e4b17023SJohn Marino }
1241*e4b17023SJohn Marino 
1242*e4b17023SJohn Marino /* Set original for basic block.  Do nothing when data structures are not
1243*e4b17023SJohn Marino    initialized so passes not needing this don't need to care.  */
1244*e4b17023SJohn Marino void
set_bb_original(basic_block bb,basic_block original)1245*e4b17023SJohn Marino set_bb_original (basic_block bb, basic_block original)
1246*e4b17023SJohn Marino {
1247*e4b17023SJohn Marino   copy_original_table_set (bb_original, bb->index, original->index);
1248*e4b17023SJohn Marino }
1249*e4b17023SJohn Marino 
1250*e4b17023SJohn Marino /* Get the original basic block.  */
1251*e4b17023SJohn Marino basic_block
get_bb_original(basic_block bb)1252*e4b17023SJohn Marino get_bb_original (basic_block bb)
1253*e4b17023SJohn Marino {
1254*e4b17023SJohn Marino   struct htab_bb_copy_original_entry *entry;
1255*e4b17023SJohn Marino   struct htab_bb_copy_original_entry key;
1256*e4b17023SJohn Marino 
1257*e4b17023SJohn Marino   gcc_assert (original_copy_bb_pool);
1258*e4b17023SJohn Marino 
1259*e4b17023SJohn Marino   key.index1 = bb->index;
1260*e4b17023SJohn Marino   entry = (struct htab_bb_copy_original_entry *) htab_find (bb_original, &key);
1261*e4b17023SJohn Marino   if (entry)
1262*e4b17023SJohn Marino     return BASIC_BLOCK (entry->index2);
1263*e4b17023SJohn Marino   else
1264*e4b17023SJohn Marino     return NULL;
1265*e4b17023SJohn Marino }
1266*e4b17023SJohn Marino 
1267*e4b17023SJohn Marino /* Set copy for basic block.  Do nothing when data structures are not
1268*e4b17023SJohn Marino    initialized so passes not needing this don't need to care.  */
1269*e4b17023SJohn Marino void
set_bb_copy(basic_block bb,basic_block copy)1270*e4b17023SJohn Marino set_bb_copy (basic_block bb, basic_block copy)
1271*e4b17023SJohn Marino {
1272*e4b17023SJohn Marino   copy_original_table_set (bb_copy, bb->index, copy->index);
1273*e4b17023SJohn Marino }
1274*e4b17023SJohn Marino 
1275*e4b17023SJohn Marino /* Get the copy of basic block.  */
1276*e4b17023SJohn Marino basic_block
get_bb_copy(basic_block bb)1277*e4b17023SJohn Marino get_bb_copy (basic_block bb)
1278*e4b17023SJohn Marino {
1279*e4b17023SJohn Marino   struct htab_bb_copy_original_entry *entry;
1280*e4b17023SJohn Marino   struct htab_bb_copy_original_entry key;
1281*e4b17023SJohn Marino 
1282*e4b17023SJohn Marino   gcc_assert (original_copy_bb_pool);
1283*e4b17023SJohn Marino 
1284*e4b17023SJohn Marino   key.index1 = bb->index;
1285*e4b17023SJohn Marino   entry = (struct htab_bb_copy_original_entry *) htab_find (bb_copy, &key);
1286*e4b17023SJohn Marino   if (entry)
1287*e4b17023SJohn Marino     return BASIC_BLOCK (entry->index2);
1288*e4b17023SJohn Marino   else
1289*e4b17023SJohn Marino     return NULL;
1290*e4b17023SJohn Marino }
1291*e4b17023SJohn Marino 
1292*e4b17023SJohn Marino /* Set copy for LOOP to COPY.  Do nothing when data structures are not
1293*e4b17023SJohn Marino    initialized so passes not needing this don't need to care.  */
1294*e4b17023SJohn Marino 
1295*e4b17023SJohn Marino void
set_loop_copy(struct loop * loop,struct loop * copy)1296*e4b17023SJohn Marino set_loop_copy (struct loop *loop, struct loop *copy)
1297*e4b17023SJohn Marino {
1298*e4b17023SJohn Marino   if (!copy)
1299*e4b17023SJohn Marino     copy_original_table_clear (loop_copy, loop->num);
1300*e4b17023SJohn Marino   else
1301*e4b17023SJohn Marino     copy_original_table_set (loop_copy, loop->num, copy->num);
1302*e4b17023SJohn Marino }
1303*e4b17023SJohn Marino 
1304*e4b17023SJohn Marino /* Get the copy of LOOP.  */
1305*e4b17023SJohn Marino 
1306*e4b17023SJohn Marino struct loop *
get_loop_copy(struct loop * loop)1307*e4b17023SJohn Marino get_loop_copy (struct loop *loop)
1308*e4b17023SJohn Marino {
1309*e4b17023SJohn Marino   struct htab_bb_copy_original_entry *entry;
1310*e4b17023SJohn Marino   struct htab_bb_copy_original_entry key;
1311*e4b17023SJohn Marino 
1312*e4b17023SJohn Marino   gcc_assert (original_copy_bb_pool);
1313*e4b17023SJohn Marino 
1314*e4b17023SJohn Marino   key.index1 = loop->num;
1315*e4b17023SJohn Marino   entry = (struct htab_bb_copy_original_entry *) htab_find (loop_copy, &key);
1316*e4b17023SJohn Marino   if (entry)
1317*e4b17023SJohn Marino     return get_loop (entry->index2);
1318*e4b17023SJohn Marino   else
1319*e4b17023SJohn Marino     return NULL;
1320*e4b17023SJohn Marino }
1321