1*38fd1498Szrj /* Control flow graph building code for GNU compiler.
2*38fd1498Szrj Copyright (C) 1987-2018 Free Software Foundation, Inc.
3*38fd1498Szrj
4*38fd1498Szrj This file is part of GCC.
5*38fd1498Szrj
6*38fd1498Szrj GCC is free software; you can redistribute it and/or modify it under
7*38fd1498Szrj the terms of the GNU General Public License as published by the Free
8*38fd1498Szrj Software Foundation; either version 3, or (at your option) any later
9*38fd1498Szrj version.
10*38fd1498Szrj
11*38fd1498Szrj GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12*38fd1498Szrj WARRANTY; without even the implied warranty of MERCHANTABILITY or
13*38fd1498Szrj FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14*38fd1498Szrj for more details.
15*38fd1498Szrj
16*38fd1498Szrj You should have received a copy of the GNU General Public License
17*38fd1498Szrj along with GCC; see the file COPYING3. If not see
18*38fd1498Szrj <http://www.gnu.org/licenses/>. */
19*38fd1498Szrj
20*38fd1498Szrj
21*38fd1498Szrj #include "config.h"
22*38fd1498Szrj #include "system.h"
23*38fd1498Szrj #include "coretypes.h"
24*38fd1498Szrj #include "backend.h"
25*38fd1498Szrj #include "rtl.h"
26*38fd1498Szrj #include "cfghooks.h"
27*38fd1498Szrj #include "memmodel.h"
28*38fd1498Szrj #include "emit-rtl.h"
29*38fd1498Szrj #include "cfgrtl.h"
30*38fd1498Szrj #include "cfganal.h"
31*38fd1498Szrj #include "cfgbuild.h"
32*38fd1498Szrj #include "except.h"
33*38fd1498Szrj #include "stmt.h"
34*38fd1498Szrj
35*38fd1498Szrj static void make_edges (basic_block, basic_block, int);
36*38fd1498Szrj static void make_label_edge (sbitmap, basic_block, rtx, int);
37*38fd1498Szrj static void find_bb_boundaries (basic_block);
38*38fd1498Szrj static void compute_outgoing_frequencies (basic_block);
39*38fd1498Szrj
40*38fd1498Szrj /* Return true if insn is something that should be contained inside basic
41*38fd1498Szrj block. */
42*38fd1498Szrj
43*38fd1498Szrj bool
inside_basic_block_p(const rtx_insn * insn)44*38fd1498Szrj inside_basic_block_p (const rtx_insn *insn)
45*38fd1498Szrj {
46*38fd1498Szrj switch (GET_CODE (insn))
47*38fd1498Szrj {
48*38fd1498Szrj case CODE_LABEL:
49*38fd1498Szrj /* Avoid creating of basic block for jumptables. */
50*38fd1498Szrj return (NEXT_INSN (insn) == 0
51*38fd1498Szrj || ! JUMP_TABLE_DATA_P (NEXT_INSN (insn)));
52*38fd1498Szrj
53*38fd1498Szrj case JUMP_INSN:
54*38fd1498Szrj case CALL_INSN:
55*38fd1498Szrj case INSN:
56*38fd1498Szrj case DEBUG_INSN:
57*38fd1498Szrj return true;
58*38fd1498Szrj
59*38fd1498Szrj case JUMP_TABLE_DATA:
60*38fd1498Szrj case BARRIER:
61*38fd1498Szrj case NOTE:
62*38fd1498Szrj return false;
63*38fd1498Szrj
64*38fd1498Szrj default:
65*38fd1498Szrj gcc_unreachable ();
66*38fd1498Szrj }
67*38fd1498Szrj }
68*38fd1498Szrj
69*38fd1498Szrj /* Return true if INSN may cause control flow transfer, so it should be last in
70*38fd1498Szrj the basic block. */
71*38fd1498Szrj
72*38fd1498Szrj bool
control_flow_insn_p(const rtx_insn * insn)73*38fd1498Szrj control_flow_insn_p (const rtx_insn *insn)
74*38fd1498Szrj {
75*38fd1498Szrj switch (GET_CODE (insn))
76*38fd1498Szrj {
77*38fd1498Szrj case NOTE:
78*38fd1498Szrj case CODE_LABEL:
79*38fd1498Szrj case DEBUG_INSN:
80*38fd1498Szrj return false;
81*38fd1498Szrj
82*38fd1498Szrj case JUMP_INSN:
83*38fd1498Szrj return true;
84*38fd1498Szrj
85*38fd1498Szrj case CALL_INSN:
86*38fd1498Szrj /* Noreturn and sibling call instructions terminate the basic blocks
87*38fd1498Szrj (but only if they happen unconditionally). */
88*38fd1498Szrj if ((SIBLING_CALL_P (insn)
89*38fd1498Szrj || find_reg_note (insn, REG_NORETURN, 0))
90*38fd1498Szrj && GET_CODE (PATTERN (insn)) != COND_EXEC)
91*38fd1498Szrj return true;
92*38fd1498Szrj
93*38fd1498Szrj /* Call insn may return to the nonlocal goto handler. */
94*38fd1498Szrj if (can_nonlocal_goto (insn))
95*38fd1498Szrj return true;
96*38fd1498Szrj break;
97*38fd1498Szrj
98*38fd1498Szrj case INSN:
99*38fd1498Szrj /* Treat trap instructions like noreturn calls (same provision). */
100*38fd1498Szrj if (GET_CODE (PATTERN (insn)) == TRAP_IF
101*38fd1498Szrj && XEXP (PATTERN (insn), 0) == const1_rtx)
102*38fd1498Szrj return true;
103*38fd1498Szrj if (!cfun->can_throw_non_call_exceptions)
104*38fd1498Szrj return false;
105*38fd1498Szrj break;
106*38fd1498Szrj
107*38fd1498Szrj case JUMP_TABLE_DATA:
108*38fd1498Szrj case BARRIER:
109*38fd1498Szrj /* It is nonsense to reach this when looking for the
110*38fd1498Szrj end of basic block, but before dead code is eliminated
111*38fd1498Szrj this may happen. */
112*38fd1498Szrj return false;
113*38fd1498Szrj
114*38fd1498Szrj default:
115*38fd1498Szrj gcc_unreachable ();
116*38fd1498Szrj }
117*38fd1498Szrj
118*38fd1498Szrj return can_throw_internal (insn);
119*38fd1498Szrj }
120*38fd1498Szrj
121*38fd1498Szrj
122*38fd1498Szrj /* Create an edge between two basic blocks. FLAGS are auxiliary information
123*38fd1498Szrj about the edge that is accumulated between calls. */
124*38fd1498Szrj
125*38fd1498Szrj /* Create an edge from a basic block to a label. */
126*38fd1498Szrj
127*38fd1498Szrj static void
make_label_edge(sbitmap edge_cache,basic_block src,rtx label,int flags)128*38fd1498Szrj make_label_edge (sbitmap edge_cache, basic_block src, rtx label, int flags)
129*38fd1498Szrj {
130*38fd1498Szrj gcc_assert (LABEL_P (label));
131*38fd1498Szrj
132*38fd1498Szrj /* If the label was never emitted, this insn is junk, but avoid a
133*38fd1498Szrj crash trying to refer to BLOCK_FOR_INSN (label). This can happen
134*38fd1498Szrj as a result of a syntax error and a diagnostic has already been
135*38fd1498Szrj printed. */
136*38fd1498Szrj
137*38fd1498Szrj if (INSN_UID (label) == 0)
138*38fd1498Szrj return;
139*38fd1498Szrj
140*38fd1498Szrj cached_make_edge (edge_cache, src, BLOCK_FOR_INSN (label), flags);
141*38fd1498Szrj }
142*38fd1498Szrj
143*38fd1498Szrj /* Create the edges generated by INSN in REGION. */
144*38fd1498Szrj
145*38fd1498Szrj void
rtl_make_eh_edge(sbitmap edge_cache,basic_block src,rtx insn)146*38fd1498Szrj rtl_make_eh_edge (sbitmap edge_cache, basic_block src, rtx insn)
147*38fd1498Szrj {
148*38fd1498Szrj eh_landing_pad lp = get_eh_landing_pad_from_rtx (insn);
149*38fd1498Szrj
150*38fd1498Szrj if (lp)
151*38fd1498Szrj {
152*38fd1498Szrj rtx_insn *label = lp->landing_pad;
153*38fd1498Szrj
154*38fd1498Szrj /* During initial rtl generation, use the post_landing_pad. */
155*38fd1498Szrj if (label == NULL)
156*38fd1498Szrj {
157*38fd1498Szrj gcc_assert (lp->post_landing_pad);
158*38fd1498Szrj label = label_rtx (lp->post_landing_pad);
159*38fd1498Szrj }
160*38fd1498Szrj
161*38fd1498Szrj make_label_edge (edge_cache, src, label,
162*38fd1498Szrj EDGE_ABNORMAL | EDGE_EH
163*38fd1498Szrj | (CALL_P (insn) ? EDGE_ABNORMAL_CALL : 0));
164*38fd1498Szrj }
165*38fd1498Szrj }
166*38fd1498Szrj
167*38fd1498Szrj /* States of basic block as seen by find_many_sub_basic_blocks. */
168*38fd1498Szrj enum state {
169*38fd1498Szrj /* Basic blocks created via split_block belong to this state.
170*38fd1498Szrj make_edges will examine these basic blocks to see if we need to
171*38fd1498Szrj create edges going out of them. */
172*38fd1498Szrj BLOCK_NEW = 0,
173*38fd1498Szrj
174*38fd1498Szrj /* Basic blocks that do not need examining belong to this state.
175*38fd1498Szrj These blocks will be left intact. In particular, make_edges will
176*38fd1498Szrj not create edges going out of these basic blocks. */
177*38fd1498Szrj BLOCK_ORIGINAL,
178*38fd1498Szrj
179*38fd1498Szrj /* Basic blocks that may need splitting (due to a label appearing in
180*38fd1498Szrj the middle, etc) belong to this state. After splitting them,
181*38fd1498Szrj make_edges will create edges going out of them as needed. */
182*38fd1498Szrj BLOCK_TO_SPLIT
183*38fd1498Szrj };
184*38fd1498Szrj
185*38fd1498Szrj #define STATE(BB) (enum state) ((size_t) (BB)->aux)
186*38fd1498Szrj #define SET_STATE(BB, STATE) ((BB)->aux = (void *) (size_t) (STATE))
187*38fd1498Szrj
188*38fd1498Szrj /* Used internally by purge_dead_tablejump_edges, ORed into state. */
189*38fd1498Szrj #define BLOCK_USED_BY_TABLEJUMP 32
190*38fd1498Szrj #define FULL_STATE(BB) ((size_t) (BB)->aux)
191*38fd1498Szrj
192*38fd1498Szrj /* Identify the edges going out of basic blocks between MIN and MAX,
193*38fd1498Szrj inclusive, that have their states set to BLOCK_NEW or
194*38fd1498Szrj BLOCK_TO_SPLIT.
195*38fd1498Szrj
196*38fd1498Szrj UPDATE_P should be nonzero if we are updating CFG and zero if we
197*38fd1498Szrj are building CFG from scratch. */
198*38fd1498Szrj
199*38fd1498Szrj static void
make_edges(basic_block min,basic_block max,int update_p)200*38fd1498Szrj make_edges (basic_block min, basic_block max, int update_p)
201*38fd1498Szrj {
202*38fd1498Szrj basic_block bb;
203*38fd1498Szrj sbitmap edge_cache = NULL;
204*38fd1498Szrj
205*38fd1498Szrj /* Heavy use of computed goto in machine-generated code can lead to
206*38fd1498Szrj nearly fully-connected CFGs. In that case we spend a significant
207*38fd1498Szrj amount of time searching the edge lists for duplicates. */
208*38fd1498Szrj if (!vec_safe_is_empty (forced_labels)
209*38fd1498Szrj || cfun->cfg->max_jumptable_ents > 100)
210*38fd1498Szrj edge_cache = sbitmap_alloc (last_basic_block_for_fn (cfun));
211*38fd1498Szrj
212*38fd1498Szrj /* By nature of the way these get numbered, ENTRY_BLOCK_PTR->next_bb block
213*38fd1498Szrj is always the entry. */
214*38fd1498Szrj if (min == ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb)
215*38fd1498Szrj make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun), min, EDGE_FALLTHRU);
216*38fd1498Szrj
217*38fd1498Szrj FOR_BB_BETWEEN (bb, min, max->next_bb, next_bb)
218*38fd1498Szrj {
219*38fd1498Szrj rtx_insn *insn;
220*38fd1498Szrj enum rtx_code code;
221*38fd1498Szrj edge e;
222*38fd1498Szrj edge_iterator ei;
223*38fd1498Szrj
224*38fd1498Szrj if (STATE (bb) == BLOCK_ORIGINAL)
225*38fd1498Szrj continue;
226*38fd1498Szrj
227*38fd1498Szrj /* If we have an edge cache, cache edges going out of BB. */
228*38fd1498Szrj if (edge_cache)
229*38fd1498Szrj {
230*38fd1498Szrj bitmap_clear (edge_cache);
231*38fd1498Szrj if (update_p)
232*38fd1498Szrj {
233*38fd1498Szrj FOR_EACH_EDGE (e, ei, bb->succs)
234*38fd1498Szrj if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
235*38fd1498Szrj bitmap_set_bit (edge_cache, e->dest->index);
236*38fd1498Szrj }
237*38fd1498Szrj }
238*38fd1498Szrj
239*38fd1498Szrj if (LABEL_P (BB_HEAD (bb))
240*38fd1498Szrj && LABEL_ALT_ENTRY_P (BB_HEAD (bb)))
241*38fd1498Szrj cached_make_edge (NULL, ENTRY_BLOCK_PTR_FOR_FN (cfun), bb, 0);
242*38fd1498Szrj
243*38fd1498Szrj /* Examine the last instruction of the block, and discover the
244*38fd1498Szrj ways we can leave the block. */
245*38fd1498Szrj
246*38fd1498Szrj insn = BB_END (bb);
247*38fd1498Szrj code = GET_CODE (insn);
248*38fd1498Szrj
249*38fd1498Szrj /* A branch. */
250*38fd1498Szrj if (code == JUMP_INSN)
251*38fd1498Szrj {
252*38fd1498Szrj rtx tmp;
253*38fd1498Szrj rtx_jump_table_data *table;
254*38fd1498Szrj
255*38fd1498Szrj /* Recognize a non-local goto as a branch outside the
256*38fd1498Szrj current function. */
257*38fd1498Szrj if (find_reg_note (insn, REG_NON_LOCAL_GOTO, NULL_RTX))
258*38fd1498Szrj ;
259*38fd1498Szrj
260*38fd1498Szrj /* Recognize a tablejump and do the right thing. */
261*38fd1498Szrj else if (tablejump_p (insn, NULL, &table))
262*38fd1498Szrj {
263*38fd1498Szrj rtvec vec = table->get_labels ();
264*38fd1498Szrj int j;
265*38fd1498Szrj
266*38fd1498Szrj for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j)
267*38fd1498Szrj make_label_edge (edge_cache, bb,
268*38fd1498Szrj XEXP (RTVEC_ELT (vec, j), 0), 0);
269*38fd1498Szrj
270*38fd1498Szrj /* Some targets (eg, ARM) emit a conditional jump that also
271*38fd1498Szrj contains the out-of-range target. Scan for these and
272*38fd1498Szrj add an edge if necessary. */
273*38fd1498Szrj if ((tmp = single_set (insn)) != NULL
274*38fd1498Szrj && SET_DEST (tmp) == pc_rtx
275*38fd1498Szrj && GET_CODE (SET_SRC (tmp)) == IF_THEN_ELSE
276*38fd1498Szrj && GET_CODE (XEXP (SET_SRC (tmp), 2)) == LABEL_REF)
277*38fd1498Szrj make_label_edge (edge_cache, bb,
278*38fd1498Szrj label_ref_label (XEXP (SET_SRC (tmp), 2)), 0);
279*38fd1498Szrj }
280*38fd1498Szrj
281*38fd1498Szrj /* If this is a computed jump, then mark it as reaching
282*38fd1498Szrj everything on the forced_labels list. */
283*38fd1498Szrj else if (computed_jump_p (insn))
284*38fd1498Szrj {
285*38fd1498Szrj rtx_insn *insn;
286*38fd1498Szrj unsigned int i;
287*38fd1498Szrj FOR_EACH_VEC_SAFE_ELT (forced_labels, i, insn)
288*38fd1498Szrj make_label_edge (edge_cache, bb, insn, EDGE_ABNORMAL);
289*38fd1498Szrj }
290*38fd1498Szrj
291*38fd1498Szrj /* Returns create an exit out. */
292*38fd1498Szrj else if (returnjump_p (insn))
293*38fd1498Szrj cached_make_edge (edge_cache, bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
294*38fd1498Szrj
295*38fd1498Szrj /* Recognize asm goto and do the right thing. */
296*38fd1498Szrj else if ((tmp = extract_asm_operands (PATTERN (insn))) != NULL)
297*38fd1498Szrj {
298*38fd1498Szrj int i, n = ASM_OPERANDS_LABEL_LENGTH (tmp);
299*38fd1498Szrj for (i = 0; i < n; ++i)
300*38fd1498Szrj make_label_edge (edge_cache, bb,
301*38fd1498Szrj XEXP (ASM_OPERANDS_LABEL (tmp, i), 0), 0);
302*38fd1498Szrj }
303*38fd1498Szrj
304*38fd1498Szrj /* Otherwise, we have a plain conditional or unconditional jump. */
305*38fd1498Szrj else
306*38fd1498Szrj {
307*38fd1498Szrj gcc_assert (JUMP_LABEL (insn));
308*38fd1498Szrj make_label_edge (edge_cache, bb, JUMP_LABEL (insn), 0);
309*38fd1498Szrj }
310*38fd1498Szrj }
311*38fd1498Szrj
312*38fd1498Szrj /* If this is a sibling call insn, then this is in effect a combined call
313*38fd1498Szrj and return, and so we need an edge to the exit block. No need to
314*38fd1498Szrj worry about EH edges, since we wouldn't have created the sibling call
315*38fd1498Szrj in the first place. */
316*38fd1498Szrj if (code == CALL_INSN && SIBLING_CALL_P (insn))
317*38fd1498Szrj cached_make_edge (edge_cache, bb, EXIT_BLOCK_PTR_FOR_FN (cfun),
318*38fd1498Szrj EDGE_SIBCALL | EDGE_ABNORMAL);
319*38fd1498Szrj
320*38fd1498Szrj /* If this is a CALL_INSN, then mark it as reaching the active EH
321*38fd1498Szrj handler for this CALL_INSN. If we're handling non-call
322*38fd1498Szrj exceptions then any insn can reach any of the active handlers.
323*38fd1498Szrj Also mark the CALL_INSN as reaching any nonlocal goto handler. */
324*38fd1498Szrj else if (code == CALL_INSN || cfun->can_throw_non_call_exceptions)
325*38fd1498Szrj {
326*38fd1498Szrj /* Add any appropriate EH edges. */
327*38fd1498Szrj rtl_make_eh_edge (edge_cache, bb, insn);
328*38fd1498Szrj
329*38fd1498Szrj if (code == CALL_INSN)
330*38fd1498Szrj {
331*38fd1498Szrj if (can_nonlocal_goto (insn))
332*38fd1498Szrj {
333*38fd1498Szrj /* ??? This could be made smarter: in some cases it's
334*38fd1498Szrj possible to tell that certain calls will not do a
335*38fd1498Szrj nonlocal goto. For example, if the nested functions
336*38fd1498Szrj that do the nonlocal gotos do not have their addresses
337*38fd1498Szrj taken, then only calls to those functions or to other
338*38fd1498Szrj nested functions that use them could possibly do
339*38fd1498Szrj nonlocal gotos. */
340*38fd1498Szrj for (rtx_insn_list *x = nonlocal_goto_handler_labels;
341*38fd1498Szrj x;
342*38fd1498Szrj x = x->next ())
343*38fd1498Szrj make_label_edge (edge_cache, bb, x->insn (),
344*38fd1498Szrj EDGE_ABNORMAL | EDGE_ABNORMAL_CALL);
345*38fd1498Szrj }
346*38fd1498Szrj
347*38fd1498Szrj if (flag_tm)
348*38fd1498Szrj {
349*38fd1498Szrj rtx note;
350*38fd1498Szrj for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
351*38fd1498Szrj if (REG_NOTE_KIND (note) == REG_TM)
352*38fd1498Szrj make_label_edge (edge_cache, bb, XEXP (note, 0),
353*38fd1498Szrj EDGE_ABNORMAL | EDGE_ABNORMAL_CALL);
354*38fd1498Szrj }
355*38fd1498Szrj }
356*38fd1498Szrj }
357*38fd1498Szrj
358*38fd1498Szrj /* Find out if we can drop through to the next block. */
359*38fd1498Szrj insn = NEXT_INSN (insn);
360*38fd1498Szrj e = find_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun));
361*38fd1498Szrj if (e && e->flags & EDGE_FALLTHRU)
362*38fd1498Szrj insn = NULL;
363*38fd1498Szrj
364*38fd1498Szrj while (insn
365*38fd1498Szrj && NOTE_P (insn)
366*38fd1498Szrj && NOTE_KIND (insn) != NOTE_INSN_BASIC_BLOCK)
367*38fd1498Szrj insn = NEXT_INSN (insn);
368*38fd1498Szrj
369*38fd1498Szrj if (!insn)
370*38fd1498Szrj cached_make_edge (edge_cache, bb, EXIT_BLOCK_PTR_FOR_FN (cfun),
371*38fd1498Szrj EDGE_FALLTHRU);
372*38fd1498Szrj else if (bb->next_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
373*38fd1498Szrj {
374*38fd1498Szrj if (insn == BB_HEAD (bb->next_bb))
375*38fd1498Szrj cached_make_edge (edge_cache, bb, bb->next_bb, EDGE_FALLTHRU);
376*38fd1498Szrj }
377*38fd1498Szrj }
378*38fd1498Szrj
379*38fd1498Szrj if (edge_cache)
380*38fd1498Szrj sbitmap_free (edge_cache);
381*38fd1498Szrj }
382*38fd1498Szrj
383*38fd1498Szrj static void
mark_tablejump_edge(rtx label)384*38fd1498Szrj mark_tablejump_edge (rtx label)
385*38fd1498Szrj {
386*38fd1498Szrj basic_block bb;
387*38fd1498Szrj
388*38fd1498Szrj gcc_assert (LABEL_P (label));
389*38fd1498Szrj /* See comment in make_label_edge. */
390*38fd1498Szrj if (INSN_UID (label) == 0)
391*38fd1498Szrj return;
392*38fd1498Szrj bb = BLOCK_FOR_INSN (label);
393*38fd1498Szrj SET_STATE (bb, FULL_STATE (bb) | BLOCK_USED_BY_TABLEJUMP);
394*38fd1498Szrj }
395*38fd1498Szrj
396*38fd1498Szrj static void
purge_dead_tablejump_edges(basic_block bb,rtx_jump_table_data * table)397*38fd1498Szrj purge_dead_tablejump_edges (basic_block bb, rtx_jump_table_data *table)
398*38fd1498Szrj {
399*38fd1498Szrj rtx_insn *insn = BB_END (bb);
400*38fd1498Szrj rtx tmp;
401*38fd1498Szrj rtvec vec;
402*38fd1498Szrj int j;
403*38fd1498Szrj edge_iterator ei;
404*38fd1498Szrj edge e;
405*38fd1498Szrj
406*38fd1498Szrj vec = table->get_labels ();
407*38fd1498Szrj
408*38fd1498Szrj for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j)
409*38fd1498Szrj mark_tablejump_edge (XEXP (RTVEC_ELT (vec, j), 0));
410*38fd1498Szrj
411*38fd1498Szrj /* Some targets (eg, ARM) emit a conditional jump that also
412*38fd1498Szrj contains the out-of-range target. Scan for these and
413*38fd1498Szrj add an edge if necessary. */
414*38fd1498Szrj if ((tmp = single_set (insn)) != NULL
415*38fd1498Szrj && SET_DEST (tmp) == pc_rtx
416*38fd1498Szrj && GET_CODE (SET_SRC (tmp)) == IF_THEN_ELSE
417*38fd1498Szrj && GET_CODE (XEXP (SET_SRC (tmp), 2)) == LABEL_REF)
418*38fd1498Szrj mark_tablejump_edge (label_ref_label (XEXP (SET_SRC (tmp), 2)));
419*38fd1498Szrj
420*38fd1498Szrj for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
421*38fd1498Szrj {
422*38fd1498Szrj if (FULL_STATE (e->dest) & BLOCK_USED_BY_TABLEJUMP)
423*38fd1498Szrj SET_STATE (e->dest, FULL_STATE (e->dest)
424*38fd1498Szrj & ~(size_t) BLOCK_USED_BY_TABLEJUMP);
425*38fd1498Szrj else if (!(e->flags & (EDGE_ABNORMAL | EDGE_EH)))
426*38fd1498Szrj {
427*38fd1498Szrj remove_edge (e);
428*38fd1498Szrj continue;
429*38fd1498Szrj }
430*38fd1498Szrj ei_next (&ei);
431*38fd1498Szrj }
432*38fd1498Szrj }
433*38fd1498Szrj
434*38fd1498Szrj /* Scan basic block BB for possible BB boundaries inside the block
435*38fd1498Szrj and create new basic blocks in the progress. */
436*38fd1498Szrj
437*38fd1498Szrj static void
find_bb_boundaries(basic_block bb)438*38fd1498Szrj find_bb_boundaries (basic_block bb)
439*38fd1498Szrj {
440*38fd1498Szrj basic_block orig_bb = bb;
441*38fd1498Szrj rtx_insn *insn = BB_HEAD (bb);
442*38fd1498Szrj rtx_insn *end = BB_END (bb), *x;
443*38fd1498Szrj rtx_jump_table_data *table;
444*38fd1498Szrj rtx_insn *flow_transfer_insn = NULL;
445*38fd1498Szrj rtx_insn *debug_insn = NULL;
446*38fd1498Szrj edge fallthru = NULL;
447*38fd1498Szrj bool skip_purge;
448*38fd1498Szrj
449*38fd1498Szrj if (insn == end)
450*38fd1498Szrj return;
451*38fd1498Szrj
452*38fd1498Szrj if (DEBUG_INSN_P (insn) || DEBUG_INSN_P (end))
453*38fd1498Szrj {
454*38fd1498Szrj /* Check whether, without debug insns, the insn==end test above
455*38fd1498Szrj would have caused us to return immediately, and behave the
456*38fd1498Szrj same way even with debug insns. If we don't do this, debug
457*38fd1498Szrj insns could cause us to purge dead edges at different times,
458*38fd1498Szrj which could in turn change the cfg and affect codegen
459*38fd1498Szrj decisions in subtle but undesirable ways. */
460*38fd1498Szrj while (insn != end && DEBUG_INSN_P (insn))
461*38fd1498Szrj insn = NEXT_INSN (insn);
462*38fd1498Szrj rtx_insn *e = end;
463*38fd1498Szrj while (insn != e && DEBUG_INSN_P (e))
464*38fd1498Szrj e = PREV_INSN (e);
465*38fd1498Szrj if (insn == e)
466*38fd1498Szrj {
467*38fd1498Szrj /* If there are debug insns after a single insn that is a
468*38fd1498Szrj control flow insn in the block, we'd have left right
469*38fd1498Szrj away, but we should clean up the debug insns after the
470*38fd1498Szrj control flow insn, because they can't remain in the same
471*38fd1498Szrj block. So, do the debug insn cleaning up, but then bail
472*38fd1498Szrj out without purging dead edges as we would if the debug
473*38fd1498Szrj insns hadn't been there. */
474*38fd1498Szrj if (e != end && !DEBUG_INSN_P (e) && control_flow_insn_p (e))
475*38fd1498Szrj {
476*38fd1498Szrj skip_purge = true;
477*38fd1498Szrj flow_transfer_insn = e;
478*38fd1498Szrj goto clean_up_debug_after_control_flow;
479*38fd1498Szrj }
480*38fd1498Szrj return;
481*38fd1498Szrj }
482*38fd1498Szrj }
483*38fd1498Szrj
484*38fd1498Szrj if (LABEL_P (insn))
485*38fd1498Szrj insn = NEXT_INSN (insn);
486*38fd1498Szrj
487*38fd1498Szrj /* Scan insn chain and try to find new basic block boundaries. */
488*38fd1498Szrj while (1)
489*38fd1498Szrj {
490*38fd1498Szrj enum rtx_code code = GET_CODE (insn);
491*38fd1498Szrj
492*38fd1498Szrj if (code == DEBUG_INSN)
493*38fd1498Szrj {
494*38fd1498Szrj if (flow_transfer_insn && !debug_insn)
495*38fd1498Szrj debug_insn = insn;
496*38fd1498Szrj }
497*38fd1498Szrj /* In case we've previously seen an insn that effects a control
498*38fd1498Szrj flow transfer, split the block. */
499*38fd1498Szrj else if ((flow_transfer_insn || code == CODE_LABEL)
500*38fd1498Szrj && inside_basic_block_p (insn))
501*38fd1498Szrj {
502*38fd1498Szrj rtx_insn *prev = PREV_INSN (insn);
503*38fd1498Szrj
504*38fd1498Szrj /* If the first non-debug inside_basic_block_p insn after a control
505*38fd1498Szrj flow transfer is not a label, split the block before the debug
506*38fd1498Szrj insn instead of before the non-debug insn, so that the debug
507*38fd1498Szrj insns are not lost. */
508*38fd1498Szrj if (debug_insn && code != CODE_LABEL && code != BARRIER)
509*38fd1498Szrj prev = PREV_INSN (debug_insn);
510*38fd1498Szrj fallthru = split_block (bb, prev);
511*38fd1498Szrj if (flow_transfer_insn)
512*38fd1498Szrj {
513*38fd1498Szrj BB_END (bb) = flow_transfer_insn;
514*38fd1498Szrj
515*38fd1498Szrj rtx_insn *next;
516*38fd1498Szrj /* Clean up the bb field for the insns between the blocks. */
517*38fd1498Szrj for (x = NEXT_INSN (flow_transfer_insn);
518*38fd1498Szrj x != BB_HEAD (fallthru->dest);
519*38fd1498Szrj x = next)
520*38fd1498Szrj {
521*38fd1498Szrj next = NEXT_INSN (x);
522*38fd1498Szrj /* Debug insns should not be in between basic blocks,
523*38fd1498Szrj drop them on the floor. */
524*38fd1498Szrj if (DEBUG_INSN_P (x))
525*38fd1498Szrj delete_insn (x);
526*38fd1498Szrj else if (!BARRIER_P (x))
527*38fd1498Szrj set_block_for_insn (x, NULL);
528*38fd1498Szrj }
529*38fd1498Szrj }
530*38fd1498Szrj
531*38fd1498Szrj bb = fallthru->dest;
532*38fd1498Szrj remove_edge (fallthru);
533*38fd1498Szrj /* BB is unreachable at this point - we need to determine its profile
534*38fd1498Szrj once edges are built. */
535*38fd1498Szrj bb->count = profile_count::uninitialized ();
536*38fd1498Szrj flow_transfer_insn = NULL;
537*38fd1498Szrj debug_insn = NULL;
538*38fd1498Szrj if (code == CODE_LABEL && LABEL_ALT_ENTRY_P (insn))
539*38fd1498Szrj make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun), bb, 0);
540*38fd1498Szrj }
541*38fd1498Szrj else if (code == BARRIER)
542*38fd1498Szrj {
543*38fd1498Szrj /* __builtin_unreachable () may cause a barrier to be emitted in
544*38fd1498Szrj the middle of a BB. We need to split it in the same manner as
545*38fd1498Szrj if the barrier were preceded by a control_flow_insn_p insn. */
546*38fd1498Szrj if (!flow_transfer_insn)
547*38fd1498Szrj flow_transfer_insn = prev_nonnote_nondebug_insn_bb (insn);
548*38fd1498Szrj }
549*38fd1498Szrj
550*38fd1498Szrj if (control_flow_insn_p (insn))
551*38fd1498Szrj flow_transfer_insn = insn;
552*38fd1498Szrj if (insn == end)
553*38fd1498Szrj break;
554*38fd1498Szrj insn = NEXT_INSN (insn);
555*38fd1498Szrj }
556*38fd1498Szrj
557*38fd1498Szrj /* In case expander replaced normal insn by sequence terminating by
558*38fd1498Szrj return and barrier, or possibly other sequence not behaving like
559*38fd1498Szrj ordinary jump, we need to take care and move basic block boundary. */
560*38fd1498Szrj if (flow_transfer_insn && flow_transfer_insn != end)
561*38fd1498Szrj {
562*38fd1498Szrj skip_purge = false;
563*38fd1498Szrj
564*38fd1498Szrj clean_up_debug_after_control_flow:
565*38fd1498Szrj BB_END (bb) = flow_transfer_insn;
566*38fd1498Szrj
567*38fd1498Szrj /* Clean up the bb field for the insns that do not belong to BB. */
568*38fd1498Szrj rtx_insn *next;
569*38fd1498Szrj for (x = NEXT_INSN (flow_transfer_insn); ; x = next)
570*38fd1498Szrj {
571*38fd1498Szrj next = NEXT_INSN (x);
572*38fd1498Szrj /* Debug insns should not be in between basic blocks,
573*38fd1498Szrj drop them on the floor. */
574*38fd1498Szrj if (DEBUG_INSN_P (x))
575*38fd1498Szrj delete_insn (x);
576*38fd1498Szrj else if (!BARRIER_P (x))
577*38fd1498Szrj set_block_for_insn (x, NULL);
578*38fd1498Szrj if (x == end)
579*38fd1498Szrj break;
580*38fd1498Szrj }
581*38fd1498Szrj
582*38fd1498Szrj if (skip_purge)
583*38fd1498Szrj return;
584*38fd1498Szrj }
585*38fd1498Szrj
586*38fd1498Szrj /* We've possibly replaced the conditional jump by conditional jump
587*38fd1498Szrj followed by cleanup at fallthru edge, so the outgoing edges may
588*38fd1498Szrj be dead. */
589*38fd1498Szrj purge_dead_edges (bb);
590*38fd1498Szrj
591*38fd1498Szrj /* purge_dead_edges doesn't handle tablejump's, but if we have split the
592*38fd1498Szrj basic block, we might need to kill some edges. */
593*38fd1498Szrj if (bb != orig_bb && tablejump_p (BB_END (bb), NULL, &table))
594*38fd1498Szrj purge_dead_tablejump_edges (bb, table);
595*38fd1498Szrj }
596*38fd1498Szrj
597*38fd1498Szrj /* Assume that frequency of basic block B is known. Compute frequencies
598*38fd1498Szrj and probabilities of outgoing edges. */
599*38fd1498Szrj
600*38fd1498Szrj static void
compute_outgoing_frequencies(basic_block b)601*38fd1498Szrj compute_outgoing_frequencies (basic_block b)
602*38fd1498Szrj {
603*38fd1498Szrj edge e, f;
604*38fd1498Szrj edge_iterator ei;
605*38fd1498Szrj
606*38fd1498Szrj if (EDGE_COUNT (b->succs) == 2)
607*38fd1498Szrj {
608*38fd1498Szrj rtx note = find_reg_note (BB_END (b), REG_BR_PROB, NULL);
609*38fd1498Szrj int probability;
610*38fd1498Szrj
611*38fd1498Szrj if (note)
612*38fd1498Szrj {
613*38fd1498Szrj probability = XINT (note, 0);
614*38fd1498Szrj e = BRANCH_EDGE (b);
615*38fd1498Szrj e->probability
616*38fd1498Szrj = profile_probability::from_reg_br_prob_note (probability);
617*38fd1498Szrj f = FALLTHRU_EDGE (b);
618*38fd1498Szrj f->probability = e->probability.invert ();
619*38fd1498Szrj return;
620*38fd1498Szrj }
621*38fd1498Szrj else
622*38fd1498Szrj {
623*38fd1498Szrj guess_outgoing_edge_probabilities (b);
624*38fd1498Szrj }
625*38fd1498Szrj }
626*38fd1498Szrj else if (single_succ_p (b))
627*38fd1498Szrj {
628*38fd1498Szrj e = single_succ_edge (b);
629*38fd1498Szrj e->probability = profile_probability::always ();
630*38fd1498Szrj return;
631*38fd1498Szrj }
632*38fd1498Szrj else
633*38fd1498Szrj {
634*38fd1498Szrj /* We rely on BBs with more than two successors to have sane probabilities
635*38fd1498Szrj and do not guess them here. For BBs terminated by switch statements
636*38fd1498Szrj expanded to jump-table jump, we have done the right thing during
637*38fd1498Szrj expansion. For EH edges, we still guess the probabilities here. */
638*38fd1498Szrj bool complex_edge = false;
639*38fd1498Szrj FOR_EACH_EDGE (e, ei, b->succs)
640*38fd1498Szrj if (e->flags & EDGE_COMPLEX)
641*38fd1498Szrj {
642*38fd1498Szrj complex_edge = true;
643*38fd1498Szrj break;
644*38fd1498Szrj }
645*38fd1498Szrj if (complex_edge)
646*38fd1498Szrj guess_outgoing_edge_probabilities (b);
647*38fd1498Szrj }
648*38fd1498Szrj }
649*38fd1498Szrj
650*38fd1498Szrj /* Assume that some pass has inserted labels or control flow
651*38fd1498Szrj instructions within a basic block. Split basic blocks as needed
652*38fd1498Szrj and create edges. */
653*38fd1498Szrj
654*38fd1498Szrj void
find_many_sub_basic_blocks(sbitmap blocks)655*38fd1498Szrj find_many_sub_basic_blocks (sbitmap blocks)
656*38fd1498Szrj {
657*38fd1498Szrj basic_block bb, min, max;
658*38fd1498Szrj bool found = false;
659*38fd1498Szrj auto_vec<unsigned int> n_succs;
660*38fd1498Szrj n_succs.safe_grow_cleared (last_basic_block_for_fn (cfun));
661*38fd1498Szrj
662*38fd1498Szrj FOR_EACH_BB_FN (bb, cfun)
663*38fd1498Szrj SET_STATE (bb,
664*38fd1498Szrj bitmap_bit_p (blocks, bb->index) ? BLOCK_TO_SPLIT : BLOCK_ORIGINAL);
665*38fd1498Szrj
666*38fd1498Szrj FOR_EACH_BB_FN (bb, cfun)
667*38fd1498Szrj if (STATE (bb) == BLOCK_TO_SPLIT)
668*38fd1498Szrj {
669*38fd1498Szrj int n = last_basic_block_for_fn (cfun);
670*38fd1498Szrj unsigned int ns = EDGE_COUNT (bb->succs);
671*38fd1498Szrj
672*38fd1498Szrj find_bb_boundaries (bb);
673*38fd1498Szrj if (n == last_basic_block_for_fn (cfun) && ns == EDGE_COUNT (bb->succs))
674*38fd1498Szrj n_succs[bb->index] = EDGE_COUNT (bb->succs);
675*38fd1498Szrj }
676*38fd1498Szrj
677*38fd1498Szrj FOR_EACH_BB_FN (bb, cfun)
678*38fd1498Szrj if (STATE (bb) != BLOCK_ORIGINAL)
679*38fd1498Szrj {
680*38fd1498Szrj found = true;
681*38fd1498Szrj break;
682*38fd1498Szrj }
683*38fd1498Szrj
684*38fd1498Szrj if (!found)
685*38fd1498Szrj return;
686*38fd1498Szrj
687*38fd1498Szrj min = max = bb;
688*38fd1498Szrj for (; bb != EXIT_BLOCK_PTR_FOR_FN (cfun); bb = bb->next_bb)
689*38fd1498Szrj if (STATE (bb) != BLOCK_ORIGINAL)
690*38fd1498Szrj max = bb;
691*38fd1498Szrj
692*38fd1498Szrj /* Now re-scan and wire in all edges. This expect simple (conditional)
693*38fd1498Szrj jumps at the end of each new basic blocks. */
694*38fd1498Szrj make_edges (min, max, 1);
695*38fd1498Szrj
696*38fd1498Szrj /* Update branch probabilities. Expect only (un)conditional jumps
697*38fd1498Szrj to be created with only the forward edges. */
698*38fd1498Szrj if (profile_status_for_fn (cfun) != PROFILE_ABSENT)
699*38fd1498Szrj FOR_BB_BETWEEN (bb, min, max->next_bb, next_bb)
700*38fd1498Szrj {
701*38fd1498Szrj edge e;
702*38fd1498Szrj edge_iterator ei;
703*38fd1498Szrj
704*38fd1498Szrj if (STATE (bb) == BLOCK_ORIGINAL)
705*38fd1498Szrj continue;
706*38fd1498Szrj if (STATE (bb) == BLOCK_NEW)
707*38fd1498Szrj {
708*38fd1498Szrj bool initialized_src = false, uninitialized_src = false;
709*38fd1498Szrj bb->count = profile_count::zero ();
710*38fd1498Szrj FOR_EACH_EDGE (e, ei, bb->preds)
711*38fd1498Szrj {
712*38fd1498Szrj if (e->count ().initialized_p ())
713*38fd1498Szrj {
714*38fd1498Szrj bb->count += e->count ();
715*38fd1498Szrj initialized_src = true;
716*38fd1498Szrj }
717*38fd1498Szrj else
718*38fd1498Szrj uninitialized_src = true;
719*38fd1498Szrj }
720*38fd1498Szrj /* When some edges are missing with read profile, this is
721*38fd1498Szrj most likely because RTL expansion introduced loop.
722*38fd1498Szrj When profile is guessed we may have BB that is reachable
723*38fd1498Szrj from unlikely path as well as from normal path.
724*38fd1498Szrj
725*38fd1498Szrj TODO: We should handle loops created during BB expansion
726*38fd1498Szrj correctly here. For now we assume all those loop to cycle
727*38fd1498Szrj precisely once. */
728*38fd1498Szrj if (!initialized_src
729*38fd1498Szrj || (uninitialized_src
730*38fd1498Szrj && profile_status_for_fn (cfun) < PROFILE_GUESSED))
731*38fd1498Szrj bb->count = profile_count::uninitialized ();
732*38fd1498Szrj }
733*38fd1498Szrj /* If nothing changed, there is no need to create new BBs. */
734*38fd1498Szrj else if (EDGE_COUNT (bb->succs) == n_succs[bb->index])
735*38fd1498Szrj {
736*38fd1498Szrj /* In rare occassions RTL expansion might have mistakely assigned
737*38fd1498Szrj a probabilities different from what is in CFG. This happens
738*38fd1498Szrj when we try to split branch to two but optimize out the
739*38fd1498Szrj second branch during the way. See PR81030. */
740*38fd1498Szrj if (JUMP_P (BB_END (bb)) && any_condjump_p (BB_END (bb))
741*38fd1498Szrj && EDGE_COUNT (bb->succs) >= 2)
742*38fd1498Szrj update_br_prob_note (bb);
743*38fd1498Szrj continue;
744*38fd1498Szrj }
745*38fd1498Szrj
746*38fd1498Szrj compute_outgoing_frequencies (bb);
747*38fd1498Szrj }
748*38fd1498Szrj
749*38fd1498Szrj FOR_EACH_BB_FN (bb, cfun)
750*38fd1498Szrj SET_STATE (bb, 0);
751*38fd1498Szrj }
752