xref: /dflybsd-src/contrib/gcc-4.7/gcc/cfgbuild.c (revision 04febcfb30580676d3e95f58a16c5137ee478b32)
1*e4b17023SJohn Marino /* Control flow graph building 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, 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 
23*e4b17023SJohn Marino #include "config.h"
24*e4b17023SJohn Marino #include "system.h"
25*e4b17023SJohn Marino #include "coretypes.h"
26*e4b17023SJohn Marino #include "tm.h"
27*e4b17023SJohn Marino #include "tree.h"
28*e4b17023SJohn Marino #include "rtl.h"
29*e4b17023SJohn Marino #include "hard-reg-set.h"
30*e4b17023SJohn Marino #include "basic-block.h"
31*e4b17023SJohn Marino #include "regs.h"
32*e4b17023SJohn Marino #include "flags.h"
33*e4b17023SJohn Marino #include "output.h"
34*e4b17023SJohn Marino #include "function.h"
35*e4b17023SJohn Marino #include "except.h"
36*e4b17023SJohn Marino #include "expr.h"
37*e4b17023SJohn Marino #include "diagnostic-core.h"
38*e4b17023SJohn Marino #include "timevar.h"
39*e4b17023SJohn Marino #include "sbitmap.h"
40*e4b17023SJohn Marino 
41*e4b17023SJohn Marino static void make_edges (basic_block, basic_block, int);
42*e4b17023SJohn Marino static void make_label_edge (sbitmap, basic_block, rtx, int);
43*e4b17023SJohn Marino static void find_bb_boundaries (basic_block);
44*e4b17023SJohn Marino static void compute_outgoing_frequencies (basic_block);
45*e4b17023SJohn Marino 
46*e4b17023SJohn Marino /* Return true if insn is something that should be contained inside basic
47*e4b17023SJohn Marino    block.  */
48*e4b17023SJohn Marino 
49*e4b17023SJohn Marino bool
inside_basic_block_p(const_rtx insn)50*e4b17023SJohn Marino inside_basic_block_p (const_rtx insn)
51*e4b17023SJohn Marino {
52*e4b17023SJohn Marino   switch (GET_CODE (insn))
53*e4b17023SJohn Marino     {
54*e4b17023SJohn Marino     case CODE_LABEL:
55*e4b17023SJohn Marino       /* Avoid creating of basic block for jumptables.  */
56*e4b17023SJohn Marino       return (NEXT_INSN (insn) == 0
57*e4b17023SJohn Marino 	      || !JUMP_P (NEXT_INSN (insn))
58*e4b17023SJohn Marino 	      || (GET_CODE (PATTERN (NEXT_INSN (insn))) != ADDR_VEC
59*e4b17023SJohn Marino 		  && GET_CODE (PATTERN (NEXT_INSN (insn))) != ADDR_DIFF_VEC));
60*e4b17023SJohn Marino 
61*e4b17023SJohn Marino     case JUMP_INSN:
62*e4b17023SJohn Marino       return (GET_CODE (PATTERN (insn)) != ADDR_VEC
63*e4b17023SJohn Marino 	      && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC);
64*e4b17023SJohn Marino 
65*e4b17023SJohn Marino     case CALL_INSN:
66*e4b17023SJohn Marino     case INSN:
67*e4b17023SJohn Marino     case DEBUG_INSN:
68*e4b17023SJohn Marino       return true;
69*e4b17023SJohn Marino 
70*e4b17023SJohn Marino     case BARRIER:
71*e4b17023SJohn Marino     case NOTE:
72*e4b17023SJohn Marino       return false;
73*e4b17023SJohn Marino 
74*e4b17023SJohn Marino     default:
75*e4b17023SJohn Marino       gcc_unreachable ();
76*e4b17023SJohn Marino     }
77*e4b17023SJohn Marino }
78*e4b17023SJohn Marino 
79*e4b17023SJohn Marino /* Return true if INSN may cause control flow transfer, so it should be last in
80*e4b17023SJohn Marino    the basic block.  */
81*e4b17023SJohn Marino 
82*e4b17023SJohn Marino bool
control_flow_insn_p(const_rtx insn)83*e4b17023SJohn Marino control_flow_insn_p (const_rtx insn)
84*e4b17023SJohn Marino {
85*e4b17023SJohn Marino   switch (GET_CODE (insn))
86*e4b17023SJohn Marino     {
87*e4b17023SJohn Marino     case NOTE:
88*e4b17023SJohn Marino     case CODE_LABEL:
89*e4b17023SJohn Marino     case DEBUG_INSN:
90*e4b17023SJohn Marino       return false;
91*e4b17023SJohn Marino 
92*e4b17023SJohn Marino     case JUMP_INSN:
93*e4b17023SJohn Marino       /* Jump insn always causes control transfer except for tablejumps.  */
94*e4b17023SJohn Marino       return (GET_CODE (PATTERN (insn)) != ADDR_VEC
95*e4b17023SJohn Marino 	      && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC);
96*e4b17023SJohn Marino 
97*e4b17023SJohn Marino     case CALL_INSN:
98*e4b17023SJohn Marino       /* Noreturn and sibling call instructions terminate the basic blocks
99*e4b17023SJohn Marino 	 (but only if they happen unconditionally).  */
100*e4b17023SJohn Marino       if ((SIBLING_CALL_P (insn)
101*e4b17023SJohn Marino 	   || find_reg_note (insn, REG_NORETURN, 0))
102*e4b17023SJohn Marino 	  && GET_CODE (PATTERN (insn)) != COND_EXEC)
103*e4b17023SJohn Marino 	return true;
104*e4b17023SJohn Marino 
105*e4b17023SJohn Marino       /* Call insn may return to the nonlocal goto handler.  */
106*e4b17023SJohn Marino       if (can_nonlocal_goto (insn))
107*e4b17023SJohn Marino 	return true;
108*e4b17023SJohn Marino       break;
109*e4b17023SJohn Marino 
110*e4b17023SJohn Marino     case INSN:
111*e4b17023SJohn Marino       /* Treat trap instructions like noreturn calls (same provision).  */
112*e4b17023SJohn Marino       if (GET_CODE (PATTERN (insn)) == TRAP_IF
113*e4b17023SJohn Marino 	  && XEXP (PATTERN (insn), 0) == const1_rtx)
114*e4b17023SJohn Marino 	return true;
115*e4b17023SJohn Marino       if (!cfun->can_throw_non_call_exceptions)
116*e4b17023SJohn Marino 	return false;
117*e4b17023SJohn Marino       break;
118*e4b17023SJohn Marino 
119*e4b17023SJohn Marino     case BARRIER:
120*e4b17023SJohn Marino       /* It is nonsense to reach barrier when looking for the
121*e4b17023SJohn Marino 	 end of basic block, but before dead code is eliminated
122*e4b17023SJohn Marino 	 this may happen.  */
123*e4b17023SJohn Marino       return false;
124*e4b17023SJohn Marino 
125*e4b17023SJohn Marino     default:
126*e4b17023SJohn Marino       gcc_unreachable ();
127*e4b17023SJohn Marino     }
128*e4b17023SJohn Marino 
129*e4b17023SJohn Marino   return can_throw_internal (insn);
130*e4b17023SJohn Marino }
131*e4b17023SJohn Marino 
132*e4b17023SJohn Marino 
133*e4b17023SJohn Marino /* Create an edge between two basic blocks.  FLAGS are auxiliary information
134*e4b17023SJohn Marino    about the edge that is accumulated between calls.  */
135*e4b17023SJohn Marino 
136*e4b17023SJohn Marino /* Create an edge from a basic block to a label.  */
137*e4b17023SJohn Marino 
138*e4b17023SJohn Marino static void
make_label_edge(sbitmap edge_cache,basic_block src,rtx label,int flags)139*e4b17023SJohn Marino make_label_edge (sbitmap edge_cache, basic_block src, rtx label, int flags)
140*e4b17023SJohn Marino {
141*e4b17023SJohn Marino   gcc_assert (LABEL_P (label));
142*e4b17023SJohn Marino 
143*e4b17023SJohn Marino   /* If the label was never emitted, this insn is junk, but avoid a
144*e4b17023SJohn Marino      crash trying to refer to BLOCK_FOR_INSN (label).  This can happen
145*e4b17023SJohn Marino      as a result of a syntax error and a diagnostic has already been
146*e4b17023SJohn Marino      printed.  */
147*e4b17023SJohn Marino 
148*e4b17023SJohn Marino   if (INSN_UID (label) == 0)
149*e4b17023SJohn Marino     return;
150*e4b17023SJohn Marino 
151*e4b17023SJohn Marino   cached_make_edge (edge_cache, src, BLOCK_FOR_INSN (label), flags);
152*e4b17023SJohn Marino }
153*e4b17023SJohn Marino 
154*e4b17023SJohn Marino /* Create the edges generated by INSN in REGION.  */
155*e4b17023SJohn Marino 
156*e4b17023SJohn Marino void
rtl_make_eh_edge(sbitmap edge_cache,basic_block src,rtx insn)157*e4b17023SJohn Marino rtl_make_eh_edge (sbitmap edge_cache, basic_block src, rtx insn)
158*e4b17023SJohn Marino {
159*e4b17023SJohn Marino   eh_landing_pad lp = get_eh_landing_pad_from_rtx (insn);
160*e4b17023SJohn Marino 
161*e4b17023SJohn Marino   if (lp)
162*e4b17023SJohn Marino     {
163*e4b17023SJohn Marino       rtx label = lp->landing_pad;
164*e4b17023SJohn Marino 
165*e4b17023SJohn Marino       /* During initial rtl generation, use the post_landing_pad.  */
166*e4b17023SJohn Marino       if (label == NULL)
167*e4b17023SJohn Marino 	{
168*e4b17023SJohn Marino 	  gcc_assert (lp->post_landing_pad);
169*e4b17023SJohn Marino 	  label = label_rtx (lp->post_landing_pad);
170*e4b17023SJohn Marino 	}
171*e4b17023SJohn Marino 
172*e4b17023SJohn Marino       make_label_edge (edge_cache, src, label,
173*e4b17023SJohn Marino 		       EDGE_ABNORMAL | EDGE_EH
174*e4b17023SJohn Marino 		       | (CALL_P (insn) ? EDGE_ABNORMAL_CALL : 0));
175*e4b17023SJohn Marino     }
176*e4b17023SJohn Marino }
177*e4b17023SJohn Marino 
178*e4b17023SJohn Marino /* States of basic block as seen by find_many_sub_basic_blocks.  */
179*e4b17023SJohn Marino enum state {
180*e4b17023SJohn Marino   /* Basic blocks created via split_block belong to this state.
181*e4b17023SJohn Marino      make_edges will examine these basic blocks to see if we need to
182*e4b17023SJohn Marino      create edges going out of them.  */
183*e4b17023SJohn Marino   BLOCK_NEW = 0,
184*e4b17023SJohn Marino 
185*e4b17023SJohn Marino   /* Basic blocks that do not need examining belong to this state.
186*e4b17023SJohn Marino      These blocks will be left intact.  In particular, make_edges will
187*e4b17023SJohn Marino      not create edges going out of these basic blocks.  */
188*e4b17023SJohn Marino   BLOCK_ORIGINAL,
189*e4b17023SJohn Marino 
190*e4b17023SJohn Marino   /* Basic blocks that may need splitting (due to a label appearing in
191*e4b17023SJohn Marino      the middle, etc) belong to this state.  After splitting them,
192*e4b17023SJohn Marino      make_edges will create edges going out of them as needed.  */
193*e4b17023SJohn Marino   BLOCK_TO_SPLIT
194*e4b17023SJohn Marino };
195*e4b17023SJohn Marino 
196*e4b17023SJohn Marino #define STATE(BB) (enum state) ((size_t) (BB)->aux)
197*e4b17023SJohn Marino #define SET_STATE(BB, STATE) ((BB)->aux = (void *) (size_t) (STATE))
198*e4b17023SJohn Marino 
199*e4b17023SJohn Marino /* Used internally by purge_dead_tablejump_edges, ORed into state.  */
200*e4b17023SJohn Marino #define BLOCK_USED_BY_TABLEJUMP		32
201*e4b17023SJohn Marino #define FULL_STATE(BB) ((size_t) (BB)->aux)
202*e4b17023SJohn Marino 
203*e4b17023SJohn Marino /* Identify the edges going out of basic blocks between MIN and MAX,
204*e4b17023SJohn Marino    inclusive, that have their states set to BLOCK_NEW or
205*e4b17023SJohn Marino    BLOCK_TO_SPLIT.
206*e4b17023SJohn Marino 
207*e4b17023SJohn Marino    UPDATE_P should be nonzero if we are updating CFG and zero if we
208*e4b17023SJohn Marino    are building CFG from scratch.  */
209*e4b17023SJohn Marino 
210*e4b17023SJohn Marino static void
make_edges(basic_block min,basic_block max,int update_p)211*e4b17023SJohn Marino make_edges (basic_block min, basic_block max, int update_p)
212*e4b17023SJohn Marino {
213*e4b17023SJohn Marino   basic_block bb;
214*e4b17023SJohn Marino   sbitmap edge_cache = NULL;
215*e4b17023SJohn Marino 
216*e4b17023SJohn Marino   /* Heavy use of computed goto in machine-generated code can lead to
217*e4b17023SJohn Marino      nearly fully-connected CFGs.  In that case we spend a significant
218*e4b17023SJohn Marino      amount of time searching the edge lists for duplicates.  */
219*e4b17023SJohn Marino   if (forced_labels || cfun->cfg->max_jumptable_ents > 100)
220*e4b17023SJohn Marino     edge_cache = sbitmap_alloc (last_basic_block);
221*e4b17023SJohn Marino 
222*e4b17023SJohn Marino   /* By nature of the way these get numbered, ENTRY_BLOCK_PTR->next_bb block
223*e4b17023SJohn Marino      is always the entry.  */
224*e4b17023SJohn Marino   if (min == ENTRY_BLOCK_PTR->next_bb)
225*e4b17023SJohn Marino     make_edge (ENTRY_BLOCK_PTR, min, EDGE_FALLTHRU);
226*e4b17023SJohn Marino 
227*e4b17023SJohn Marino   FOR_BB_BETWEEN (bb, min, max->next_bb, next_bb)
228*e4b17023SJohn Marino     {
229*e4b17023SJohn Marino       rtx insn, x;
230*e4b17023SJohn Marino       enum rtx_code code;
231*e4b17023SJohn Marino       edge e;
232*e4b17023SJohn Marino       edge_iterator ei;
233*e4b17023SJohn Marino 
234*e4b17023SJohn Marino       if (STATE (bb) == BLOCK_ORIGINAL)
235*e4b17023SJohn Marino 	continue;
236*e4b17023SJohn Marino 
237*e4b17023SJohn Marino       /* If we have an edge cache, cache edges going out of BB.  */
238*e4b17023SJohn Marino       if (edge_cache)
239*e4b17023SJohn Marino 	{
240*e4b17023SJohn Marino 	  sbitmap_zero (edge_cache);
241*e4b17023SJohn Marino 	  if (update_p)
242*e4b17023SJohn Marino 	    {
243*e4b17023SJohn Marino 	      FOR_EACH_EDGE (e, ei, bb->succs)
244*e4b17023SJohn Marino 		if (e->dest != EXIT_BLOCK_PTR)
245*e4b17023SJohn Marino 		  SET_BIT (edge_cache, e->dest->index);
246*e4b17023SJohn Marino 	    }
247*e4b17023SJohn Marino 	}
248*e4b17023SJohn Marino 
249*e4b17023SJohn Marino       if (LABEL_P (BB_HEAD (bb))
250*e4b17023SJohn Marino 	  && LABEL_ALT_ENTRY_P (BB_HEAD (bb)))
251*e4b17023SJohn Marino 	cached_make_edge (NULL, ENTRY_BLOCK_PTR, bb, 0);
252*e4b17023SJohn Marino 
253*e4b17023SJohn Marino       /* Examine the last instruction of the block, and discover the
254*e4b17023SJohn Marino 	 ways we can leave the block.  */
255*e4b17023SJohn Marino 
256*e4b17023SJohn Marino       insn = BB_END (bb);
257*e4b17023SJohn Marino       code = GET_CODE (insn);
258*e4b17023SJohn Marino 
259*e4b17023SJohn Marino       /* A branch.  */
260*e4b17023SJohn Marino       if (code == JUMP_INSN)
261*e4b17023SJohn Marino 	{
262*e4b17023SJohn Marino 	  rtx tmp;
263*e4b17023SJohn Marino 
264*e4b17023SJohn Marino 	  /* Recognize a non-local goto as a branch outside the
265*e4b17023SJohn Marino 	     current function.  */
266*e4b17023SJohn Marino 	  if (find_reg_note (insn, REG_NON_LOCAL_GOTO, NULL_RTX))
267*e4b17023SJohn Marino 	    ;
268*e4b17023SJohn Marino 
269*e4b17023SJohn Marino 	  /* Recognize a tablejump and do the right thing.  */
270*e4b17023SJohn Marino 	  else if (tablejump_p (insn, NULL, &tmp))
271*e4b17023SJohn Marino 	    {
272*e4b17023SJohn Marino 	      rtvec vec;
273*e4b17023SJohn Marino 	      int j;
274*e4b17023SJohn Marino 
275*e4b17023SJohn Marino 	      if (GET_CODE (PATTERN (tmp)) == ADDR_VEC)
276*e4b17023SJohn Marino 		vec = XVEC (PATTERN (tmp), 0);
277*e4b17023SJohn Marino 	      else
278*e4b17023SJohn Marino 		vec = XVEC (PATTERN (tmp), 1);
279*e4b17023SJohn Marino 
280*e4b17023SJohn Marino 	      for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j)
281*e4b17023SJohn Marino 		make_label_edge (edge_cache, bb,
282*e4b17023SJohn Marino 				 XEXP (RTVEC_ELT (vec, j), 0), 0);
283*e4b17023SJohn Marino 
284*e4b17023SJohn Marino 	      /* Some targets (eg, ARM) emit a conditional jump that also
285*e4b17023SJohn Marino 		 contains the out-of-range target.  Scan for these and
286*e4b17023SJohn Marino 		 add an edge if necessary.  */
287*e4b17023SJohn Marino 	      if ((tmp = single_set (insn)) != NULL
288*e4b17023SJohn Marino 		  && SET_DEST (tmp) == pc_rtx
289*e4b17023SJohn Marino 		  && GET_CODE (SET_SRC (tmp)) == IF_THEN_ELSE
290*e4b17023SJohn Marino 		  && GET_CODE (XEXP (SET_SRC (tmp), 2)) == LABEL_REF)
291*e4b17023SJohn Marino 		make_label_edge (edge_cache, bb,
292*e4b17023SJohn Marino 				 XEXP (XEXP (SET_SRC (tmp), 2), 0), 0);
293*e4b17023SJohn Marino 	    }
294*e4b17023SJohn Marino 
295*e4b17023SJohn Marino 	  /* If this is a computed jump, then mark it as reaching
296*e4b17023SJohn Marino 	     everything on the forced_labels list.  */
297*e4b17023SJohn Marino 	  else if (computed_jump_p (insn))
298*e4b17023SJohn Marino 	    {
299*e4b17023SJohn Marino 	      for (x = forced_labels; x; x = XEXP (x, 1))
300*e4b17023SJohn Marino 		make_label_edge (edge_cache, bb, XEXP (x, 0), EDGE_ABNORMAL);
301*e4b17023SJohn Marino 	    }
302*e4b17023SJohn Marino 
303*e4b17023SJohn Marino 	  /* Returns create an exit out.  */
304*e4b17023SJohn Marino 	  else if (returnjump_p (insn))
305*e4b17023SJohn Marino 	    cached_make_edge (edge_cache, bb, EXIT_BLOCK_PTR, 0);
306*e4b17023SJohn Marino 
307*e4b17023SJohn Marino 	  /* Recognize asm goto and do the right thing.  */
308*e4b17023SJohn Marino 	  else if ((tmp = extract_asm_operands (PATTERN (insn))) != NULL)
309*e4b17023SJohn Marino 	    {
310*e4b17023SJohn Marino 	      int i, n = ASM_OPERANDS_LABEL_LENGTH (tmp);
311*e4b17023SJohn Marino 	      for (i = 0; i < n; ++i)
312*e4b17023SJohn Marino 		make_label_edge (edge_cache, bb,
313*e4b17023SJohn Marino 				 XEXP (ASM_OPERANDS_LABEL (tmp, i), 0), 0);
314*e4b17023SJohn Marino 	    }
315*e4b17023SJohn Marino 
316*e4b17023SJohn Marino 	  /* Otherwise, we have a plain conditional or unconditional jump.  */
317*e4b17023SJohn Marino 	  else
318*e4b17023SJohn Marino 	    {
319*e4b17023SJohn Marino 	      gcc_assert (JUMP_LABEL (insn));
320*e4b17023SJohn Marino 	      make_label_edge (edge_cache, bb, JUMP_LABEL (insn), 0);
321*e4b17023SJohn Marino 	    }
322*e4b17023SJohn Marino 	}
323*e4b17023SJohn Marino 
324*e4b17023SJohn Marino       /* If this is a sibling call insn, then this is in effect a combined call
325*e4b17023SJohn Marino 	 and return, and so we need an edge to the exit block.  No need to
326*e4b17023SJohn Marino 	 worry about EH edges, since we wouldn't have created the sibling call
327*e4b17023SJohn Marino 	 in the first place.  */
328*e4b17023SJohn Marino       if (code == CALL_INSN && SIBLING_CALL_P (insn))
329*e4b17023SJohn Marino 	cached_make_edge (edge_cache, bb, EXIT_BLOCK_PTR,
330*e4b17023SJohn Marino 			  EDGE_SIBCALL | EDGE_ABNORMAL);
331*e4b17023SJohn Marino 
332*e4b17023SJohn Marino       /* If this is a CALL_INSN, then mark it as reaching the active EH
333*e4b17023SJohn Marino 	 handler for this CALL_INSN.  If we're handling non-call
334*e4b17023SJohn Marino 	 exceptions then any insn can reach any of the active handlers.
335*e4b17023SJohn Marino 	 Also mark the CALL_INSN as reaching any nonlocal goto handler.  */
336*e4b17023SJohn Marino       else if (code == CALL_INSN || cfun->can_throw_non_call_exceptions)
337*e4b17023SJohn Marino 	{
338*e4b17023SJohn Marino 	  /* Add any appropriate EH edges.  */
339*e4b17023SJohn Marino 	  rtl_make_eh_edge (edge_cache, bb, insn);
340*e4b17023SJohn Marino 
341*e4b17023SJohn Marino 	  if (code == CALL_INSN)
342*e4b17023SJohn Marino 	    {
343*e4b17023SJohn Marino 	      if (can_nonlocal_goto (insn))
344*e4b17023SJohn Marino 		{
345*e4b17023SJohn Marino 		  /* ??? This could be made smarter: in some cases it's
346*e4b17023SJohn Marino 		     possible to tell that certain calls will not do a
347*e4b17023SJohn Marino 		     nonlocal goto.  For example, if the nested functions
348*e4b17023SJohn Marino 		     that do the nonlocal gotos do not have their addresses
349*e4b17023SJohn Marino 		     taken, then only calls to those functions or to other
350*e4b17023SJohn Marino 		     nested functions that use them could possibly do
351*e4b17023SJohn Marino 		     nonlocal gotos.  */
352*e4b17023SJohn Marino 		  for (x = nonlocal_goto_handler_labels; x; x = XEXP (x, 1))
353*e4b17023SJohn Marino 		    make_label_edge (edge_cache, bb, XEXP (x, 0),
354*e4b17023SJohn Marino 				     EDGE_ABNORMAL | EDGE_ABNORMAL_CALL);
355*e4b17023SJohn Marino 		}
356*e4b17023SJohn Marino 
357*e4b17023SJohn Marino 	      if (flag_tm)
358*e4b17023SJohn Marino 		{
359*e4b17023SJohn Marino 		  rtx note;
360*e4b17023SJohn Marino 		  for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
361*e4b17023SJohn Marino 		    if (REG_NOTE_KIND (note) == REG_TM)
362*e4b17023SJohn Marino 		      make_label_edge (edge_cache, bb, XEXP (note, 0),
363*e4b17023SJohn Marino 				       EDGE_ABNORMAL | EDGE_ABNORMAL_CALL);
364*e4b17023SJohn Marino 		}
365*e4b17023SJohn Marino 	    }
366*e4b17023SJohn Marino 	}
367*e4b17023SJohn Marino 
368*e4b17023SJohn Marino       /* Find out if we can drop through to the next block.  */
369*e4b17023SJohn Marino       insn = NEXT_INSN (insn);
370*e4b17023SJohn Marino       e = find_edge (bb, EXIT_BLOCK_PTR);
371*e4b17023SJohn Marino       if (e && e->flags & EDGE_FALLTHRU)
372*e4b17023SJohn Marino 	insn = NULL;
373*e4b17023SJohn Marino 
374*e4b17023SJohn Marino       while (insn
375*e4b17023SJohn Marino 	     && NOTE_P (insn)
376*e4b17023SJohn Marino 	     && NOTE_KIND (insn) != NOTE_INSN_BASIC_BLOCK)
377*e4b17023SJohn Marino 	insn = NEXT_INSN (insn);
378*e4b17023SJohn Marino 
379*e4b17023SJohn Marino       if (!insn)
380*e4b17023SJohn Marino 	cached_make_edge (edge_cache, bb, EXIT_BLOCK_PTR, EDGE_FALLTHRU);
381*e4b17023SJohn Marino       else if (bb->next_bb != EXIT_BLOCK_PTR)
382*e4b17023SJohn Marino 	{
383*e4b17023SJohn Marino 	  if (insn == BB_HEAD (bb->next_bb))
384*e4b17023SJohn Marino 	    cached_make_edge (edge_cache, bb, bb->next_bb, EDGE_FALLTHRU);
385*e4b17023SJohn Marino 	}
386*e4b17023SJohn Marino     }
387*e4b17023SJohn Marino 
388*e4b17023SJohn Marino   if (edge_cache)
389*e4b17023SJohn Marino     sbitmap_vector_free (edge_cache);
390*e4b17023SJohn Marino }
391*e4b17023SJohn Marino 
392*e4b17023SJohn Marino static void
mark_tablejump_edge(rtx label)393*e4b17023SJohn Marino mark_tablejump_edge (rtx label)
394*e4b17023SJohn Marino {
395*e4b17023SJohn Marino   basic_block bb;
396*e4b17023SJohn Marino 
397*e4b17023SJohn Marino   gcc_assert (LABEL_P (label));
398*e4b17023SJohn Marino   /* See comment in make_label_edge.  */
399*e4b17023SJohn Marino   if (INSN_UID (label) == 0)
400*e4b17023SJohn Marino     return;
401*e4b17023SJohn Marino   bb = BLOCK_FOR_INSN (label);
402*e4b17023SJohn Marino   SET_STATE (bb, FULL_STATE (bb) | BLOCK_USED_BY_TABLEJUMP);
403*e4b17023SJohn Marino }
404*e4b17023SJohn Marino 
405*e4b17023SJohn Marino static void
purge_dead_tablejump_edges(basic_block bb,rtx table)406*e4b17023SJohn Marino purge_dead_tablejump_edges (basic_block bb, rtx table)
407*e4b17023SJohn Marino {
408*e4b17023SJohn Marino   rtx insn = BB_END (bb), tmp;
409*e4b17023SJohn Marino   rtvec vec;
410*e4b17023SJohn Marino   int j;
411*e4b17023SJohn Marino   edge_iterator ei;
412*e4b17023SJohn Marino   edge e;
413*e4b17023SJohn Marino 
414*e4b17023SJohn Marino   if (GET_CODE (PATTERN (table)) == ADDR_VEC)
415*e4b17023SJohn Marino     vec = XVEC (PATTERN (table), 0);
416*e4b17023SJohn Marino   else
417*e4b17023SJohn Marino     vec = XVEC (PATTERN (table), 1);
418*e4b17023SJohn Marino 
419*e4b17023SJohn Marino   for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j)
420*e4b17023SJohn Marino     mark_tablejump_edge (XEXP (RTVEC_ELT (vec, j), 0));
421*e4b17023SJohn Marino 
422*e4b17023SJohn Marino   /* Some targets (eg, ARM) emit a conditional jump that also
423*e4b17023SJohn Marino      contains the out-of-range target.  Scan for these and
424*e4b17023SJohn Marino      add an edge if necessary.  */
425*e4b17023SJohn Marino   if ((tmp = single_set (insn)) != NULL
426*e4b17023SJohn Marino        && SET_DEST (tmp) == pc_rtx
427*e4b17023SJohn Marino        && GET_CODE (SET_SRC (tmp)) == IF_THEN_ELSE
428*e4b17023SJohn Marino        && GET_CODE (XEXP (SET_SRC (tmp), 2)) == LABEL_REF)
429*e4b17023SJohn Marino     mark_tablejump_edge (XEXP (XEXP (SET_SRC (tmp), 2), 0));
430*e4b17023SJohn Marino 
431*e4b17023SJohn Marino   for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
432*e4b17023SJohn Marino     {
433*e4b17023SJohn Marino       if (FULL_STATE (e->dest) & BLOCK_USED_BY_TABLEJUMP)
434*e4b17023SJohn Marino 	SET_STATE (e->dest, FULL_STATE (e->dest)
435*e4b17023SJohn Marino 			    & ~(size_t) BLOCK_USED_BY_TABLEJUMP);
436*e4b17023SJohn Marino       else if (!(e->flags & (EDGE_ABNORMAL | EDGE_EH)))
437*e4b17023SJohn Marino 	{
438*e4b17023SJohn Marino 	  remove_edge (e);
439*e4b17023SJohn Marino 	  continue;
440*e4b17023SJohn Marino 	}
441*e4b17023SJohn Marino       ei_next (&ei);
442*e4b17023SJohn Marino     }
443*e4b17023SJohn Marino }
444*e4b17023SJohn Marino 
445*e4b17023SJohn Marino /* Scan basic block BB for possible BB boundaries inside the block
446*e4b17023SJohn Marino    and create new basic blocks in the progress.  */
447*e4b17023SJohn Marino 
448*e4b17023SJohn Marino static void
find_bb_boundaries(basic_block bb)449*e4b17023SJohn Marino find_bb_boundaries (basic_block bb)
450*e4b17023SJohn Marino {
451*e4b17023SJohn Marino   basic_block orig_bb = bb;
452*e4b17023SJohn Marino   rtx insn = BB_HEAD (bb);
453*e4b17023SJohn Marino   rtx end = BB_END (bb), x;
454*e4b17023SJohn Marino   rtx table;
455*e4b17023SJohn Marino   rtx flow_transfer_insn = NULL_RTX;
456*e4b17023SJohn Marino   edge fallthru = NULL;
457*e4b17023SJohn Marino 
458*e4b17023SJohn Marino   if (insn == BB_END (bb))
459*e4b17023SJohn Marino     return;
460*e4b17023SJohn Marino 
461*e4b17023SJohn Marino   if (LABEL_P (insn))
462*e4b17023SJohn Marino     insn = NEXT_INSN (insn);
463*e4b17023SJohn Marino 
464*e4b17023SJohn Marino   /* Scan insn chain and try to find new basic block boundaries.  */
465*e4b17023SJohn Marino   while (1)
466*e4b17023SJohn Marino     {
467*e4b17023SJohn Marino       enum rtx_code code = GET_CODE (insn);
468*e4b17023SJohn Marino 
469*e4b17023SJohn Marino       /* In case we've previously seen an insn that effects a control
470*e4b17023SJohn Marino 	 flow transfer, split the block.  */
471*e4b17023SJohn Marino       if ((flow_transfer_insn || code == CODE_LABEL)
472*e4b17023SJohn Marino 	  && inside_basic_block_p (insn))
473*e4b17023SJohn Marino 	{
474*e4b17023SJohn Marino 	  fallthru = split_block (bb, PREV_INSN (insn));
475*e4b17023SJohn Marino 	  if (flow_transfer_insn)
476*e4b17023SJohn Marino 	    {
477*e4b17023SJohn Marino 	      BB_END (bb) = flow_transfer_insn;
478*e4b17023SJohn Marino 
479*e4b17023SJohn Marino 	      /* Clean up the bb field for the insns between the blocks.  */
480*e4b17023SJohn Marino 	      for (x = NEXT_INSN (flow_transfer_insn);
481*e4b17023SJohn Marino 		   x != BB_HEAD (fallthru->dest);
482*e4b17023SJohn Marino 		   x = NEXT_INSN (x))
483*e4b17023SJohn Marino 		if (!BARRIER_P (x))
484*e4b17023SJohn Marino 		  set_block_for_insn (x, NULL);
485*e4b17023SJohn Marino 	    }
486*e4b17023SJohn Marino 
487*e4b17023SJohn Marino 	  bb = fallthru->dest;
488*e4b17023SJohn Marino 	  remove_edge (fallthru);
489*e4b17023SJohn Marino 	  flow_transfer_insn = NULL_RTX;
490*e4b17023SJohn Marino 	  if (code == CODE_LABEL && LABEL_ALT_ENTRY_P (insn))
491*e4b17023SJohn Marino 	    make_edge (ENTRY_BLOCK_PTR, bb, 0);
492*e4b17023SJohn Marino 	}
493*e4b17023SJohn Marino       else if (code == BARRIER)
494*e4b17023SJohn Marino 	{
495*e4b17023SJohn Marino 	  /* __builtin_unreachable () may cause a barrier to be emitted in
496*e4b17023SJohn Marino 	     the middle of a BB.  We need to split it in the same manner as
497*e4b17023SJohn Marino 	     if the barrier were preceded by a control_flow_insn_p insn.  */
498*e4b17023SJohn Marino 	  if (!flow_transfer_insn)
499*e4b17023SJohn Marino 	    flow_transfer_insn = prev_nonnote_insn_bb (insn);
500*e4b17023SJohn Marino 	}
501*e4b17023SJohn Marino 
502*e4b17023SJohn Marino       if (control_flow_insn_p (insn))
503*e4b17023SJohn Marino 	flow_transfer_insn = insn;
504*e4b17023SJohn Marino       if (insn == end)
505*e4b17023SJohn Marino 	break;
506*e4b17023SJohn Marino       insn = NEXT_INSN (insn);
507*e4b17023SJohn Marino     }
508*e4b17023SJohn Marino 
509*e4b17023SJohn Marino   /* In case expander replaced normal insn by sequence terminating by
510*e4b17023SJohn Marino      return and barrier, or possibly other sequence not behaving like
511*e4b17023SJohn Marino      ordinary jump, we need to take care and move basic block boundary.  */
512*e4b17023SJohn Marino   if (flow_transfer_insn)
513*e4b17023SJohn Marino     {
514*e4b17023SJohn Marino       BB_END (bb) = flow_transfer_insn;
515*e4b17023SJohn Marino 
516*e4b17023SJohn Marino       /* Clean up the bb field for the insns that do not belong to BB.  */
517*e4b17023SJohn Marino       x = flow_transfer_insn;
518*e4b17023SJohn Marino       while (x != end)
519*e4b17023SJohn Marino 	{
520*e4b17023SJohn Marino 	  x = NEXT_INSN (x);
521*e4b17023SJohn Marino 	  if (!BARRIER_P (x))
522*e4b17023SJohn Marino 	    set_block_for_insn (x, NULL);
523*e4b17023SJohn Marino 	}
524*e4b17023SJohn Marino     }
525*e4b17023SJohn Marino 
526*e4b17023SJohn Marino   /* We've possibly replaced the conditional jump by conditional jump
527*e4b17023SJohn Marino      followed by cleanup at fallthru edge, so the outgoing edges may
528*e4b17023SJohn Marino      be dead.  */
529*e4b17023SJohn Marino   purge_dead_edges (bb);
530*e4b17023SJohn Marino 
531*e4b17023SJohn Marino   /* purge_dead_edges doesn't handle tablejump's, but if we have split the
532*e4b17023SJohn Marino      basic block, we might need to kill some edges.  */
533*e4b17023SJohn Marino   if (bb != orig_bb && tablejump_p (BB_END (bb), NULL, &table))
534*e4b17023SJohn Marino     purge_dead_tablejump_edges (bb, table);
535*e4b17023SJohn Marino }
536*e4b17023SJohn Marino 
537*e4b17023SJohn Marino /*  Assume that frequency of basic block B is known.  Compute frequencies
538*e4b17023SJohn Marino     and probabilities of outgoing edges.  */
539*e4b17023SJohn Marino 
540*e4b17023SJohn Marino static void
compute_outgoing_frequencies(basic_block b)541*e4b17023SJohn Marino compute_outgoing_frequencies (basic_block b)
542*e4b17023SJohn Marino {
543*e4b17023SJohn Marino   edge e, f;
544*e4b17023SJohn Marino   edge_iterator ei;
545*e4b17023SJohn Marino 
546*e4b17023SJohn Marino   if (EDGE_COUNT (b->succs) == 2)
547*e4b17023SJohn Marino     {
548*e4b17023SJohn Marino       rtx note = find_reg_note (BB_END (b), REG_BR_PROB, NULL);
549*e4b17023SJohn Marino       int probability;
550*e4b17023SJohn Marino 
551*e4b17023SJohn Marino       if (note)
552*e4b17023SJohn Marino 	{
553*e4b17023SJohn Marino 	  probability = INTVAL (XEXP (note, 0));
554*e4b17023SJohn Marino 	  e = BRANCH_EDGE (b);
555*e4b17023SJohn Marino 	  e->probability = probability;
556*e4b17023SJohn Marino 	  e->count = ((b->count * probability + REG_BR_PROB_BASE / 2)
557*e4b17023SJohn Marino 		      / REG_BR_PROB_BASE);
558*e4b17023SJohn Marino 	  f = FALLTHRU_EDGE (b);
559*e4b17023SJohn Marino 	  f->probability = REG_BR_PROB_BASE - probability;
560*e4b17023SJohn Marino 	  f->count = b->count - e->count;
561*e4b17023SJohn Marino 	  return;
562*e4b17023SJohn Marino 	}
563*e4b17023SJohn Marino     }
564*e4b17023SJohn Marino 
565*e4b17023SJohn Marino   if (single_succ_p (b))
566*e4b17023SJohn Marino     {
567*e4b17023SJohn Marino       e = single_succ_edge (b);
568*e4b17023SJohn Marino       e->probability = REG_BR_PROB_BASE;
569*e4b17023SJohn Marino       e->count = b->count;
570*e4b17023SJohn Marino       return;
571*e4b17023SJohn Marino     }
572*e4b17023SJohn Marino   guess_outgoing_edge_probabilities (b);
573*e4b17023SJohn Marino   if (b->count)
574*e4b17023SJohn Marino     FOR_EACH_EDGE (e, ei, b->succs)
575*e4b17023SJohn Marino       e->count = ((b->count * e->probability + REG_BR_PROB_BASE / 2)
576*e4b17023SJohn Marino 		  / REG_BR_PROB_BASE);
577*e4b17023SJohn Marino }
578*e4b17023SJohn Marino 
579*e4b17023SJohn Marino /* Assume that some pass has inserted labels or control flow
580*e4b17023SJohn Marino    instructions within a basic block.  Split basic blocks as needed
581*e4b17023SJohn Marino    and create edges.  */
582*e4b17023SJohn Marino 
583*e4b17023SJohn Marino void
find_many_sub_basic_blocks(sbitmap blocks)584*e4b17023SJohn Marino find_many_sub_basic_blocks (sbitmap blocks)
585*e4b17023SJohn Marino {
586*e4b17023SJohn Marino   basic_block bb, min, max;
587*e4b17023SJohn Marino 
588*e4b17023SJohn Marino   FOR_EACH_BB (bb)
589*e4b17023SJohn Marino     SET_STATE (bb,
590*e4b17023SJohn Marino 	       TEST_BIT (blocks, bb->index) ? BLOCK_TO_SPLIT : BLOCK_ORIGINAL);
591*e4b17023SJohn Marino 
592*e4b17023SJohn Marino   FOR_EACH_BB (bb)
593*e4b17023SJohn Marino     if (STATE (bb) == BLOCK_TO_SPLIT)
594*e4b17023SJohn Marino       find_bb_boundaries (bb);
595*e4b17023SJohn Marino 
596*e4b17023SJohn Marino   FOR_EACH_BB (bb)
597*e4b17023SJohn Marino     if (STATE (bb) != BLOCK_ORIGINAL)
598*e4b17023SJohn Marino       break;
599*e4b17023SJohn Marino 
600*e4b17023SJohn Marino   min = max = bb;
601*e4b17023SJohn Marino   for (; bb != EXIT_BLOCK_PTR; bb = bb->next_bb)
602*e4b17023SJohn Marino     if (STATE (bb) != BLOCK_ORIGINAL)
603*e4b17023SJohn Marino       max = bb;
604*e4b17023SJohn Marino 
605*e4b17023SJohn Marino   /* Now re-scan and wire in all edges.  This expect simple (conditional)
606*e4b17023SJohn Marino      jumps at the end of each new basic blocks.  */
607*e4b17023SJohn Marino   make_edges (min, max, 1);
608*e4b17023SJohn Marino 
609*e4b17023SJohn Marino   /* Update branch probabilities.  Expect only (un)conditional jumps
610*e4b17023SJohn Marino      to be created with only the forward edges.  */
611*e4b17023SJohn Marino   if (profile_status != PROFILE_ABSENT)
612*e4b17023SJohn Marino     FOR_BB_BETWEEN (bb, min, max->next_bb, next_bb)
613*e4b17023SJohn Marino       {
614*e4b17023SJohn Marino 	edge e;
615*e4b17023SJohn Marino 	edge_iterator ei;
616*e4b17023SJohn Marino 
617*e4b17023SJohn Marino 	if (STATE (bb) == BLOCK_ORIGINAL)
618*e4b17023SJohn Marino 	  continue;
619*e4b17023SJohn Marino 	if (STATE (bb) == BLOCK_NEW)
620*e4b17023SJohn Marino 	  {
621*e4b17023SJohn Marino 	    bb->count = 0;
622*e4b17023SJohn Marino 	    bb->frequency = 0;
623*e4b17023SJohn Marino 	    FOR_EACH_EDGE (e, ei, bb->preds)
624*e4b17023SJohn Marino 	      {
625*e4b17023SJohn Marino 		bb->count += e->count;
626*e4b17023SJohn Marino 		bb->frequency += EDGE_FREQUENCY (e);
627*e4b17023SJohn Marino 	      }
628*e4b17023SJohn Marino 	  }
629*e4b17023SJohn Marino 
630*e4b17023SJohn Marino 	compute_outgoing_frequencies (bb);
631*e4b17023SJohn Marino       }
632*e4b17023SJohn Marino 
633*e4b17023SJohn Marino   FOR_EACH_BB (bb)
634*e4b17023SJohn Marino     SET_STATE (bb, 0);
635*e4b17023SJohn Marino }
636