]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/basic-block.h
This patch sanitizes the partitioning to address issues such as edge weight insanitie...
[thirdparty/gcc.git] / gcc / basic-block.h
CommitLineData
7a8cba34 1/* Define control flow data structures for the CFG.
d1e082c2 2 Copyright (C) 1987-2013 Free Software Foundation, Inc.
3245eea0 3
1322177d 4This file is part of GCC.
3245eea0 5
1322177d
LB
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
9dcd6f09 8Software Foundation; either version 3, or (at your option) any later
1322177d 9version.
3245eea0 10
1322177d
LB
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
3245eea0
CH
15
16You should have received a copy of the GNU General Public License
9dcd6f09
NC
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
3245eea0 19
88657302 20#ifndef GCC_BASIC_BLOCK_H
7f8a2125 21#define GCC_BASIC_BLOCK_H
3245eea0 22
6de9cd9a 23#include "predict.h"
628f6a4e 24#include "vec.h"
997de8ed 25#include "function.h"
19d18142 26
4977bab6
ZW
27/* Type we use to hold basic block counters. Should be at least
28 64bit. Although a counter cannot be negative, we use a signed
29 type, because erroneous negative counts can be generated when the
30 flow graph is manipulated by various optimizations. A signed type
32dd366d 31 makes those easy to detect. */
b2aec5c0 32typedef HOST_WIDEST_INT gcov_type;
9f71de84 33typedef unsigned HOST_WIDEST_INT gcov_type_unsigned;
b2aec5c0 34
e881bb1b 35/* Control flow edge information. */
0823efed 36struct GTY((user)) edge_def {
e881bb1b 37 /* The two blocks at the ends of the edge. */
b8244d74
SB
38 basic_block src;
39 basic_block dest;
e881bb1b
RH
40
41 /* Instructions queued on the edge. */
6de9cd9a 42 union edge_def_insns {
0823efed
DN
43 gimple_seq g;
44 rtx r;
45 } insns;
e881bb1b
RH
46
47 /* Auxiliary info specific to a pass. */
0823efed 48 PTR aux;
3245eea0 49
5368224f 50 /* Location of any goto implicit in the edge. */
2d593c86 51 location_t goto_locus;
62b857ea 52
2eac9a76
RG
53 /* The index number corresponding to this edge in the edge vector
54 dest->preds. */
55 unsigned int dest_idx;
56
a315c44c 57 int flags; /* see cfg-flags.def */
e881bb1b 58 int probability; /* biased by REG_BR_PROB_BASE */
b2aec5c0 59 gcov_type count; /* Expected number of executions calculated
51891abe 60 in profile.c */
6de9cd9a
DN
61};
62
3245eea0 63
0823efed
DN
64/* Garbage collection and PCH support for edge_def. */
65extern void gt_ggc_mx (edge_def *e);
66extern void gt_pch_nx (edge_def *e);
67extern void gt_pch_nx (edge_def *e, gt_pointer_operator, void *);
68
a315c44c
SB
69/* Masks for edge.flags. */
70#define DEF_EDGE_FLAG(NAME,IDX) EDGE_##NAME = 1 << IDX ,
71enum cfg_edge_flags {
72#include "cfg-flags.def"
73 LAST_CFG_EDGE_FLAG /* this is only used for EDGE_ALL_FLAGS */
74};
75#undef DEF_EDGE_FLAG
76
77/* Bit mask for all edge flags. */
78#define EDGE_ALL_FLAGS ((LAST_CFG_EDGE_FLAG - 1) * 2 - 1)
3245eea0 79
a315c44c
SB
80/* The following four flags all indicate something special about an edge.
81 Test the edge flags on EDGE_COMPLEX to detect all forms of "strange"
82 control flow transfers. */
0be7e7a6
RH
83#define EDGE_COMPLEX \
84 (EDGE_ABNORMAL | EDGE_ABNORMAL_CALL | EDGE_EH | EDGE_PRESERVE)
65b98a02 85
cdb23767 86/* Counter summary from the last set of coverage counts read by
71c0e7fc 87 profile.c. */
cdb23767
NS
88extern const struct gcov_ctr_summary *profile_info;
89
aa4723d7
SB
90/* Structure to gather statistic about profile consistency, per pass.
91 An array of this structure, indexed by pass static number, is allocated
92 in passes.c. The structure is defined here so that different CFG modes
93 can do their book-keeping via CFG hooks.
94
95 For every field[2], field[0] is the count before the pass runs, and
96 field[1] is the post-pass count. This allows us to monitor the effect
97 of each individual pass on the profile consistency.
98
99 This structure is not supposed to be used by anything other than passes.c
100 and one CFG hook per CFG mode. */
101struct profile_record
102{
103 /* The number of basic blocks where sum(freq) of the block's predecessors
104 doesn't match reasonably well with the incoming frequency. */
105 int num_mismatched_freq_in[2];
106 /* Likewise for a basic block's successors. */
107 int num_mismatched_freq_out[2];
108 /* The number of basic blocks where sum(count) of the block's predecessors
109 doesn't match reasonably well with the incoming frequency. */
110 int num_mismatched_count_in[2];
111 /* Likewise for a basic block's successors. */
112 int num_mismatched_count_out[2];
113 /* A weighted cost of the run-time of the function body. */
114 gcov_type time[2];
115 /* A weighted cost of the size of the function body. */
116 int size[2];
117 /* True iff this pass actually was run. */
118 bool run;
119};
120
3d436d2a
ZD
121/* Declared in cfgloop.h. */
122struct loop;
3245eea0 123
3e8b732e 124struct GTY(()) rtl_bb_info {
bcc708fc
MM
125 /* The first insn of the block is embedded into bb->il.x. */
126 /* The last insn of the block. */
3e8b732e
MM
127 rtx end_;
128
129 /* In CFGlayout mode points to insn notes/jumptables to be placed just before
130 and after the block. */
bcc708fc
MM
131 rtx header_;
132 rtx footer_;
3e8b732e
MM
133};
134
135struct GTY(()) gimple_bb_info {
136 /* Sequence of statements in this block. */
137 gimple_seq seq;
138
139 /* PHI nodes for this block. */
140 gimple_seq phi_nodes;
141};
4aab792d 142
229ecb89 143/* A basic block is a sequence of instructions with only one entry and
e68e3108
MM
144 only one exit. If any one of the instructions are executed, they
145 will all be executed, and in sequence from first to last.
146
147 There may be COND_EXEC instructions in the basic block. The
148 COND_EXEC *instructions* will be executed -- but if the condition
149 is false the conditionally executed *expressions* will of course
150 not be executed. We don't consider the conditionally executed
151 expression (which might have side-effects) to be in a separate
152 basic block because the program counter will always be at the same
153 location after the COND_EXEC instruction, regardless of whether the
154 condition is true or not.
155
156 Basic blocks need not start with a label nor end with a jump insn.
b53978a3
JO
157 For example, a previous basic block may just "conditionally fall"
158 into the succeeding basic block, and the last basic block need not
159 end with a jump insn. Block 0 is a descendant of the entry block.
160
161 A basic block beginning with two labels cannot have notes between
162 the labels.
163
164 Data for jump tables are stored in jump_insns that occur in no
165 basic block even though these insns can follow or precede insns in
166 basic blocks. */
167
e881bb1b 168/* Basic block information indexed by block number. */
d1b38208 169struct GTY((chain_next ("%h.next_bb"), chain_prev ("%h.prev_bb"))) basic_block_def {
e881bb1b 170 /* The edges into and out of the block. */
9771b263
DN
171 vec<edge, va_gc> *preds;
172 vec<edge, va_gc> *succs;
4d1d8045 173
e881bb1b 174 /* Auxiliary info specific to a pass. */
6de9cd9a 175 PTR GTY ((skip (""))) aux;
3245eea0 176
076c7ab8 177 /* Innermost loop containing the block. */
9e2f83a5 178 struct loop *loop_father;
076c7ab8
ZW
179
180 /* The dominance and postdominance information node. */
181 struct et_node * GTY ((skip (""))) dom[2];
336a6399 182
918ed612 183 /* Previous and next blocks in the chain. */
b8244d74
SB
184 basic_block prev_bb;
185 basic_block next_bb;
918ed612 186
5e2d947c 187 union basic_block_il_dependent {
3e8b732e 188 struct gimple_bb_info GTY ((tag ("0"))) gimple;
bcc708fc
MM
189 struct {
190 rtx head_;
191 struct rtl_bb_info * rtl;
192 } GTY ((tag ("1"))) x;
5e2d947c
JH
193 } GTY ((desc ("((%1.flags & BB_RTL) != 0)"))) il;
194
391886c8
RG
195 /* Various flags. See cfg-flags.def. */
196 int flags;
7f8a2125 197
076c7ab8
ZW
198 /* The index of this block. */
199 int index;
200
391886c8
RG
201 /* Expected number of executions: calculated in profile.c. */
202 gcov_type count;
076c7ab8 203
861f9cd0
JH
204 /* Expected frequency. Normalized to be in range 0 to BB_FREQ_MAX. */
205 int frequency;
006844a3 206
a315c44c
SB
207 /* The discriminator for this block. The discriminator distinguishes
208 among several basic blocks that share a common locus, allowing for
209 more accurate sample-based profiling. */
6c52e687 210 int discriminator;
6de9cd9a
DN
211};
212
3e8b732e
MM
213/* This ensures that struct gimple_bb_info is smaller than
214 struct rtl_bb_info, so that inlining the former into basic_block_def
215 is the better choice. */
216typedef int __assert_gimple_bb_smaller_rtl_bb
217 [(int)sizeof(struct rtl_bb_info)
218 - (int)sizeof (struct gimple_bb_info)];
7506e1cb 219
c71070ab 220
861f9cd0 221#define BB_FREQ_MAX 10000
e881bb1b 222
a315c44c
SB
223/* Masks for basic_block.flags. */
224#define DEF_BASIC_BLOCK_FLAG(NAME,IDX) BB_##NAME = 1 << IDX ,
225enum cfg_bb_flags
9e32d2be 226{
a315c44c
SB
227#include "cfg-flags.def"
228 LAST_CFG_BB_FLAG /* this is only used for BB_ALL_FLAGS */
9e32d2be 229};
a315c44c
SB
230#undef DEF_BASIC_BLOCK_FLAG
231
c4669594 232/* Bit mask for all basic block flags. */
a315c44c 233#define BB_ALL_FLAGS ((LAST_CFG_BB_FLAG - 1) * 2 - 1)
740ce53d 234
c4669594
SB
235/* Bit mask for all basic block flags that must be preserved. These are
236 the bit masks that are *not* cleared by clear_bb_flags. */
237#define BB_FLAGS_TO_PRESERVE \
238 (BB_DISABLE_SCHEDULE | BB_RTL | BB_NON_LOCAL_GOTO_TARGET \
239 | BB_HOT_PARTITION | BB_COLD_PARTITION)
240
a315c44c 241/* Dummy bitmask for convenience in the hot/cold partitioning code. */
076c7ab8 242#define BB_UNPARTITIONED 0
006844a3 243
750054a2
CT
244/* Partitions, to be used when partitioning hot and cold basic blocks into
245 separate sections. */
076c7ab8 246#define BB_PARTITION(bb) ((bb)->flags & (BB_HOT_PARTITION|BB_COLD_PARTITION))
51a904c9
ZW
247#define BB_SET_PARTITION(bb, part) do { \
248 basic_block bb_ = (bb); \
249 bb_->flags = ((bb_->flags & ~(BB_HOT_PARTITION|BB_COLD_PARTITION)) \
250 | (part)); \
251} while (0)
252
076c7ab8
ZW
253#define BB_COPY_PARTITION(dstbb, srcbb) \
254 BB_SET_PARTITION (dstbb, BB_PARTITION (srcbb))
750054a2 255
8fee41c2
ZD
256/* State of dominance information. */
257
258enum dom_state
259{
260 DOM_NONE, /* Not computed at all. */
261 DOM_NO_FAST_QUERY, /* The data is OK, but the fast query data are not usable. */
262 DOM_OK /* Everything is ok. */
263};
264
24b97832 265/* What sort of profiling information we have. */
c6a21142 266enum profile_status_d
24b97832
ILT
267{
268 PROFILE_ABSENT,
269 PROFILE_GUESSED,
fa766006
JH
270 PROFILE_READ,
271 PROFILE_LAST /* Last value, used by profile streaming. */
24b97832
ILT
272};
273
997de8ed
SB
274/* A structure to group all the per-function control flow graph data.
275 The x_* prefixing is necessary because otherwise references to the
276 fields of this struct are interpreted as the defines for backward
277 source compatibility following the definition of this struct. */
d1b38208 278struct GTY(()) control_flow_graph {
997de8ed
SB
279 /* Block pointers for the exit and entry of a function.
280 These are always the head and tail of the basic block list. */
281 basic_block x_entry_block_ptr;
282 basic_block x_exit_block_ptr;
283
284 /* Index by basic block number, get basic block struct info. */
9771b263 285 vec<basic_block, va_gc> *x_basic_block_info;
997de8ed
SB
286
287 /* Number of basic blocks in this flow graph. */
288 int x_n_basic_blocks;
e881bb1b 289
997de8ed
SB
290 /* Number of edges in this flow graph. */
291 int x_n_edges;
e881bb1b 292
997de8ed
SB
293 /* The first free basic block number. */
294 int x_last_basic_block;
d55bc081 295
25efe060
NF
296 /* UIDs for LABEL_DECLs. */
297 int last_label_uid;
298
997de8ed 299 /* Mapping of labels to their associated blocks. At present
726a989a 300 only used for the gimple CFG. */
9771b263 301 vec<basic_block, va_gc> *x_label_to_block_map;
d55bc081 302
c6a21142 303 enum profile_status_d x_profile_status;
8fee41c2
ZD
304
305 /* Whether the dominators and the postdominators are available. */
306 enum dom_state x_dom_computed[2];
307
308 /* Number of basic blocks in the dominance tree. */
309 unsigned x_n_bbs_in_dom_tree[2];
cb91fab0
JH
310
311 /* Maximal number of entities in the single jumptable. Used to estimate
312 final flowgraph size. */
313 int max_jumptable_ents;
997de8ed 314};
d3a923ee 315
997de8ed
SB
316/* Defines for accessing the fields of the CFG structure for function FN. */
317#define ENTRY_BLOCK_PTR_FOR_FUNCTION(FN) ((FN)->cfg->x_entry_block_ptr)
318#define EXIT_BLOCK_PTR_FOR_FUNCTION(FN) ((FN)->cfg->x_exit_block_ptr)
319#define basic_block_info_for_function(FN) ((FN)->cfg->x_basic_block_info)
320#define n_basic_blocks_for_function(FN) ((FN)->cfg->x_n_basic_blocks)
321#define n_edges_for_function(FN) ((FN)->cfg->x_n_edges)
322#define last_basic_block_for_function(FN) ((FN)->cfg->x_last_basic_block)
323#define label_to_block_map_for_function(FN) ((FN)->cfg->x_label_to_block_map)
9defb1fe 324#define profile_status_for_function(FN) ((FN)->cfg->x_profile_status)
997de8ed
SB
325
326#define BASIC_BLOCK_FOR_FUNCTION(FN,N) \
9771b263 327 ((*basic_block_info_for_function(FN))[(N)])
9defb1fe 328#define SET_BASIC_BLOCK_FOR_FUNCTION(FN,N,BB) \
9771b263 329 ((*basic_block_info_for_function(FN))[(N)] = (BB))
997de8ed 330
f0e4ea10 331/* Defines for textual backward source compatibility. */
997de8ed
SB
332#define ENTRY_BLOCK_PTR (cfun->cfg->x_entry_block_ptr)
333#define EXIT_BLOCK_PTR (cfun->cfg->x_exit_block_ptr)
334#define basic_block_info (cfun->cfg->x_basic_block_info)
335#define n_basic_blocks (cfun->cfg->x_n_basic_blocks)
336#define n_edges (cfun->cfg->x_n_edges)
337#define last_basic_block (cfun->cfg->x_last_basic_block)
338#define label_to_block_map (cfun->cfg->x_label_to_block_map)
339#define profile_status (cfun->cfg->x_profile_status)
340
9771b263
DN
341#define BASIC_BLOCK(N) ((*basic_block_info)[(N)])
342#define SET_BASIC_BLOCK(N,BB) ((*basic_block_info)[(N)] = (BB))
d3a923ee 343
918ed612
ZD
344/* For iterating over basic blocks. */
345#define FOR_BB_BETWEEN(BB, FROM, TO, DIR) \
346 for (BB = FROM; BB != TO; BB = BB->DIR)
347
997de8ed
SB
348#define FOR_EACH_BB_FN(BB, FN) \
349 FOR_BB_BETWEEN (BB, (FN)->cfg->x_entry_block_ptr->next_bb, (FN)->cfg->x_exit_block_ptr, next_bb)
350
351#define FOR_EACH_BB(BB) FOR_EACH_BB_FN (BB, cfun)
918ed612 352
997de8ed
SB
353#define FOR_EACH_BB_REVERSE_FN(BB, FN) \
354 FOR_BB_BETWEEN (BB, (FN)->cfg->x_exit_block_ptr->prev_bb, (FN)->cfg->x_entry_block_ptr, prev_bb)
355
356#define FOR_EACH_BB_REVERSE(BB) FOR_EACH_BB_REVERSE_FN(BB, cfun)
918ed612 357
50654f6c
ZD
358/* For iterating over insns in basic block. */
359#define FOR_BB_INSNS(BB, INSN) \
360 for ((INSN) = BB_HEAD (BB); \
24bd1a0b 361 (INSN) && (INSN) != NEXT_INSN (BB_END (BB)); \
50654f6c
ZD
362 (INSN) = NEXT_INSN (INSN))
363
6fb5fa3c
DB
364/* For iterating over insns in basic block when we might remove the
365 current insn. */
366#define FOR_BB_INSNS_SAFE(BB, INSN, CURR) \
367 for ((INSN) = BB_HEAD (BB), (CURR) = (INSN) ? NEXT_INSN ((INSN)): NULL; \
368 (INSN) && (INSN) != NEXT_INSN (BB_END (BB)); \
369 (INSN) = (CURR), (CURR) = (INSN) ? NEXT_INSN ((INSN)) : NULL)
b8698a0f 370
50654f6c
ZD
371#define FOR_BB_INSNS_REVERSE(BB, INSN) \
372 for ((INSN) = BB_END (BB); \
24bd1a0b 373 (INSN) && (INSN) != PREV_INSN (BB_HEAD (BB)); \
50654f6c
ZD
374 (INSN) = PREV_INSN (INSN))
375
6fb5fa3c
DB
376#define FOR_BB_INSNS_REVERSE_SAFE(BB, INSN, CURR) \
377 for ((INSN) = BB_END (BB),(CURR) = (INSN) ? PREV_INSN ((INSN)) : NULL; \
378 (INSN) && (INSN) != PREV_INSN (BB_HEAD (BB)); \
379 (INSN) = (CURR), (CURR) = (INSN) ? PREV_INSN ((INSN)) : NULL)
380
ed8d2920
MM
381/* Cycles through _all_ basic blocks, even the fake ones (entry and
382 exit block). */
383
384#define FOR_ALL_BB(BB) \
385 for (BB = ENTRY_BLOCK_PTR; BB; BB = BB->next_bb)
386
a930a4ef
JH
387#define FOR_ALL_BB_FN(BB, FN) \
388 for (BB = ENTRY_BLOCK_PTR_FOR_FUNCTION (FN); BB; BB = BB->next_bb)
389
5ece9746
JL
390\f
391/* Stuff for recording basic block info. */
392
bcc708fc
MM
393#define BB_HEAD(B) (B)->il.x.head_
394#define BB_END(B) (B)->il.x.rtl->end_
395#define BB_HEADER(B) (B)->il.x.rtl->header_
396#define BB_FOOTER(B) (B)->il.x.rtl->footer_
2b1d9dc0 397
95bca6b0
BRF
398/* Special block numbers [markers] for entry and exit.
399 Neither of them is supposed to hold actual statements. */
24bd1a0b
DB
400#define ENTRY_BLOCK (0)
401#define EXIT_BLOCK (1)
402
403/* The two blocks that are always in the cfg. */
404#define NUM_FIXED_BLOCKS (2)
5ece9746 405
ba4f7968 406#define set_block_for_insn(INSN, BB) (BLOCK_FOR_INSN (INSN) = BB)
e881bb1b 407
f55ade6e 408extern void compute_bb_for_insn (void);
c2924966 409extern unsigned int free_bb_for_insn (void);
f55ade6e 410extern void update_bb_for_insn (basic_block);
e881bb1b 411
f55ade6e 412extern void insert_insn_on_edge (rtx, edge);
598ec7bd 413basic_block split_edge_and_insert (edge, rtx);
3dec4024 414
4e3825db 415extern void commit_one_edge_insertion (edge e);
f55ade6e 416extern void commit_edge_insertions (void);
f55ade6e 417
f55ade6e 418extern edge unchecked_make_edge (basic_block, basic_block, int);
a6ee1a15 419extern edge cached_make_edge (sbitmap, basic_block, basic_block, int);
f55ade6e
AJ
420extern edge make_edge (basic_block, basic_block, int);
421extern edge make_single_succ_edge (basic_block, basic_block, int);
452ba14d 422extern void remove_edge_raw (edge);
f55ade6e
AJ
423extern void redirect_edge_succ (edge, basic_block);
424extern edge redirect_edge_succ_nodup (edge, basic_block);
425extern void redirect_edge_pred (edge, basic_block);
426extern basic_block create_basic_block_structure (rtx, rtx, rtx, basic_block);
427extern void clear_bb_flags (void);
a315c44c
SB
428extern void dump_bb_info (FILE *, basic_block, int, int, bool, bool);
429extern void dump_edge_info (FILE *, edge, int, int);
7b3b6ae4
LC
430extern void debug (edge_def &ref);
431extern void debug (edge_def *ptr);
c4669594 432extern void brief_dump_cfg (FILE *, int);
f55ade6e 433extern void clear_edges (void);
33156717 434extern void scale_bbs_frequencies_int (basic_block *, int, int, int);
c22cacf3 435extern void scale_bbs_frequencies_gcov_type (basic_block *, int, gcov_type,
33156717 436 gcov_type);
10c4b247 437
c05ffc49
BS
438/* Structure to group all of the information to process IF-THEN and
439 IF-THEN-ELSE blocks for the conditional execution support. This
440 needs to be in a public file in case the IFCVT macros call
441 functions passing the ce_if_block data structure. */
442
443typedef struct ce_if_block
444{
445 basic_block test_bb; /* First test block. */
446 basic_block then_bb; /* THEN block. */
447 basic_block else_bb; /* ELSE block or NULL. */
448 basic_block join_bb; /* Join THEN/ELSE blocks. */
449 basic_block last_test_bb; /* Last bb to hold && or || tests. */
450 int num_multiple_test_blocks; /* # of && and || basic blocks. */
451 int num_and_and_blocks; /* # of && blocks. */
452 int num_or_or_blocks; /* # of || blocks. */
453 int num_multiple_test_insns; /* # of insns in && and || blocks. */
454 int and_and_p; /* Complex test is &&. */
455 int num_then_insns; /* # of insns in THEN block. */
456 int num_else_insns; /* # of insns in ELSE block. */
457 int pass; /* Pass number. */
c05ffc49
BS
458} ce_if_block_t;
459
410538ea 460/* This structure maintains an edge list vector. */
9771b263 461/* FIXME: Make this a vec<edge>. */
7f8a2125 462struct edge_list
410538ea 463{
410538ea
AM
464 int num_edges;
465 edge *index_to_edge;
466};
467
e42922b1
JH
468/* The base value for branch probability notes and edge probabilities. */
469#define REG_BR_PROB_BASE 10000
470
410538ea
AM
471/* This is the value which indicates no edge is present. */
472#define EDGE_INDEX_NO_EDGE -1
473
474/* EDGE_INDEX returns an integer index for an edge, or EDGE_INDEX_NO_EDGE
475 if there is no edge between the 2 basic blocks. */
476#define EDGE_INDEX(el, pred, succ) (find_edge_index ((el), (pred), (succ)))
477
478/* INDEX_EDGE_PRED_BB and INDEX_EDGE_SUCC_BB return a pointer to the basic
479 block which is either the pred or succ end of the indexed edge. */
480#define INDEX_EDGE_PRED_BB(el, index) ((el)->index_to_edge[(index)]->src)
481#define INDEX_EDGE_SUCC_BB(el, index) ((el)->index_to_edge[(index)]->dest)
482
483/* INDEX_EDGE returns a pointer to the edge. */
484#define INDEX_EDGE(el, index) ((el)->index_to_edge[(index)])
485
486/* Number of edges in the compressed edge list. */
487#define NUM_EDGES(el) ((el)->num_edges)
488
7a442791 489/* BB is assumed to contain conditional jump. Return the fallthru edge. */
628f6a4e
BE
490#define FALLTHRU_EDGE(bb) (EDGE_SUCC ((bb), 0)->flags & EDGE_FALLTHRU \
491 ? EDGE_SUCC ((bb), 0) : EDGE_SUCC ((bb), 1))
7a442791
JH
492
493/* BB is assumed to contain conditional jump. Return the branch edge. */
628f6a4e
BE
494#define BRANCH_EDGE(bb) (EDGE_SUCC ((bb), 0)->flags & EDGE_FALLTHRU \
495 ? EDGE_SUCC ((bb), 1) : EDGE_SUCC ((bb), 0))
7a442791 496
e78410bf 497#define RDIV(X,Y) (((X) + (Y) / 2) / (Y))
134d3a2e 498/* Return expected execution frequency of the edge E. */
e78410bf
JH
499#define EDGE_FREQUENCY(e) RDIV ((e)->src->frequency * (e)->probability, \
500 REG_BR_PROB_BASE)
134d3a2e 501
8ddb5a29 502/* Compute a scale factor (or probability) suitable for scaling of
f41f80f9 503 gcov_type values via apply_probability() and apply_scale(). */
8ddb5a29
TJ
504#define GCOV_COMPUTE_SCALE(num,den) \
505 ((den) ? RDIV ((num) * REG_BR_PROB_BASE, (den)) : REG_BR_PROB_BASE)
506
4262e623 507/* Return nonzero if edge is critical. */
628f6a4e
BE
508#define EDGE_CRITICAL_P(e) (EDGE_COUNT ((e)->src->succs) >= 2 \
509 && EDGE_COUNT ((e)->dest->preds) >= 2)
510
9771b263
DN
511#define EDGE_COUNT(ev) vec_safe_length (ev)
512#define EDGE_I(ev,i) (*ev)[(i)]
513#define EDGE_PRED(bb,i) (*(bb)->preds)[(i)]
514#define EDGE_SUCC(bb,i) (*(bb)->succs)[(i)]
628f6a4e 515
c5cbcccf
ZD
516/* Returns true if BB has precisely one successor. */
517
518static inline bool
ed7a4b4b 519single_succ_p (const_basic_block bb)
c5cbcccf
ZD
520{
521 return EDGE_COUNT (bb->succs) == 1;
522}
523
524/* Returns true if BB has precisely one predecessor. */
525
526static inline bool
ed7a4b4b 527single_pred_p (const_basic_block bb)
c5cbcccf
ZD
528{
529 return EDGE_COUNT (bb->preds) == 1;
530}
531
81b29e2f
ZD
532/* Returns the single successor edge of basic block BB. Aborts if
533 BB does not have exactly one successor. */
c5cbcccf
ZD
534
535static inline edge
ed7a4b4b 536single_succ_edge (const_basic_block bb)
c5cbcccf 537{
77a74ed7 538 gcc_checking_assert (single_succ_p (bb));
c5cbcccf
ZD
539 return EDGE_SUCC (bb, 0);
540}
541
81b29e2f
ZD
542/* Returns the single predecessor edge of basic block BB. Aborts
543 if BB does not have exactly one predecessor. */
c5cbcccf
ZD
544
545static inline edge
ed7a4b4b 546single_pred_edge (const_basic_block bb)
c5cbcccf 547{
77a74ed7 548 gcc_checking_assert (single_pred_p (bb));
c5cbcccf
ZD
549 return EDGE_PRED (bb, 0);
550}
551
81b29e2f
ZD
552/* Returns the single successor block of basic block BB. Aborts
553 if BB does not have exactly one successor. */
c5cbcccf
ZD
554
555static inline basic_block
ed7a4b4b 556single_succ (const_basic_block bb)
c5cbcccf
ZD
557{
558 return single_succ_edge (bb)->dest;
559}
560
81b29e2f
ZD
561/* Returns the single predecessor block of basic block BB. Aborts
562 if BB does not have exactly one predecessor.*/
c5cbcccf
ZD
563
564static inline basic_block
ed7a4b4b 565single_pred (const_basic_block bb)
c5cbcccf
ZD
566{
567 return single_pred_edge (bb)->src;
568}
569
628f6a4e
BE
570/* Iterator object for edges. */
571
572typedef struct {
573 unsigned index;
9771b263 574 vec<edge, va_gc> **container;
628f6a4e
BE
575} edge_iterator;
576
9771b263 577static inline vec<edge, va_gc> *
f76ccf60
BE
578ei_container (edge_iterator i)
579{
77a74ed7 580 gcc_checking_assert (i.container);
f76ccf60
BE
581 return *i.container;
582}
583
584#define ei_start(iter) ei_start_1 (&(iter))
585#define ei_last(iter) ei_last_1 (&(iter))
586
628f6a4e
BE
587/* Return an iterator pointing to the start of an edge vector. */
588static inline edge_iterator
9771b263 589ei_start_1 (vec<edge, va_gc> **ev)
628f6a4e
BE
590{
591 edge_iterator i;
592
593 i.index = 0;
594 i.container = ev;
595
596 return i;
597}
598
599/* Return an iterator pointing to the last element of an edge
471854f8 600 vector. */
628f6a4e 601static inline edge_iterator
9771b263 602ei_last_1 (vec<edge, va_gc> **ev)
628f6a4e
BE
603{
604 edge_iterator i;
605
f76ccf60 606 i.index = EDGE_COUNT (*ev) - 1;
628f6a4e
BE
607 i.container = ev;
608
609 return i;
610}
611
612/* Is the iterator `i' at the end of the sequence? */
613static inline bool
614ei_end_p (edge_iterator i)
615{
f76ccf60 616 return (i.index == EDGE_COUNT (ei_container (i)));
628f6a4e
BE
617}
618
619/* Is the iterator `i' at one position before the end of the
620 sequence? */
621static inline bool
622ei_one_before_end_p (edge_iterator i)
623{
f76ccf60 624 return (i.index + 1 == EDGE_COUNT (ei_container (i)));
628f6a4e
BE
625}
626
627/* Advance the iterator to the next element. */
628static inline void
629ei_next (edge_iterator *i)
630{
77a74ed7 631 gcc_checking_assert (i->index < EDGE_COUNT (ei_container (*i)));
628f6a4e
BE
632 i->index++;
633}
634
635/* Move the iterator to the previous element. */
636static inline void
637ei_prev (edge_iterator *i)
638{
77a74ed7 639 gcc_checking_assert (i->index > 0);
628f6a4e
BE
640 i->index--;
641}
642
643/* Return the edge pointed to by the iterator `i'. */
644static inline edge
645ei_edge (edge_iterator i)
646{
f76ccf60 647 return EDGE_I (ei_container (i), i.index);
628f6a4e
BE
648}
649
650/* Return an edge pointed to by the iterator. Do it safely so that
651 NULL is returned when the iterator is pointing at the end of the
652 sequence. */
653static inline edge
654ei_safe_edge (edge_iterator i)
655{
656 return !ei_end_p (i) ? ei_edge (i) : NULL;
657}
658
f3522a84
KH
659/* Return 1 if we should continue to iterate. Return 0 otherwise.
660 *Edge P is set to the next edge if we are to continue to iterate
661 and NULL otherwise. */
662
663static inline bool
664ei_cond (edge_iterator ei, edge *p)
665{
666 if (!ei_end_p (ei))
667 {
668 *p = ei_edge (ei);
669 return 1;
670 }
671 else
672 {
673 *p = NULL;
674 return 0;
675 }
676}
677
628f6a4e 678/* This macro serves as a convenient way to iterate each edge in a
c2b7c2d8 679 vector of predecessor or successor edges. It must not be used when
628f6a4e
BE
680 an element might be removed during the traversal, otherwise
681 elements will be missed. Instead, use a for-loop like that shown
682 in the following pseudo-code:
c22cacf3 683
628f6a4e
BE
684 FOR (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
685 {
686 IF (e != taken_edge)
d0d2cc21 687 remove_edge (e);
628f6a4e
BE
688 ELSE
689 ei_next (&ei);
690 }
691*/
692
f3522a84
KH
693#define FOR_EACH_EDGE(EDGE,ITER,EDGE_VEC) \
694 for ((ITER) = ei_start ((EDGE_VEC)); \
695 ei_cond ((ITER), &(EDGE)); \
628f6a4e 696 ei_next (&(ITER)))
4262e623 697
e0bb17a8 698#define CLEANUP_EXPENSIVE 1 /* Do relatively expensive optimizations
46fac664
JH
699 except for edge forwarding */
700#define CLEANUP_CROSSJUMP 2 /* Do crossjumping. */
701#define CLEANUP_POST_REGSTACK 4 /* We run after reg-stack and need
702 to care REG_DEAD notes. */
6fb5fa3c
DB
703#define CLEANUP_THREADING 8 /* Do jump threading. */
704#define CLEANUP_NO_INSN_DEL 16 /* Do not try to delete trivially dead
95479831 705 insns. */
6fb5fa3c 706#define CLEANUP_CFGLAYOUT 32 /* Do cleanup in cfglayout mode. */
7d776ee2 707#define CLEANUP_CFG_CHANGED 64 /* The caller changed the CFG. */
6ce2bcb7 708
3c2c4f22 709/* In cfganal.c */
d7c028c0
LC
710extern void bitmap_intersection_of_succs (sbitmap, sbitmap *, basic_block);
711extern void bitmap_intersection_of_preds (sbitmap, sbitmap *, basic_block);
712extern void bitmap_union_of_succs (sbitmap, sbitmap *, basic_block);
713extern void bitmap_union_of_preds (sbitmap, sbitmap *, basic_block);
3c2c4f22 714
077692c6 715/* In lcm.c */
10d22567 716extern struct edge_list *pre_edge_lcm (int, sbitmap *, sbitmap *,
f55ade6e
AJ
717 sbitmap *, sbitmap *, sbitmap **,
718 sbitmap **);
10d22567 719extern struct edge_list *pre_edge_rev_lcm (int, sbitmap *,
f55ade6e
AJ
720 sbitmap *, sbitmap *,
721 sbitmap *, sbitmap **,
722 sbitmap **);
723extern void compute_available (sbitmap *, sbitmap *, sbitmap *, sbitmap *);
a05924f9 724
f1ebdfc5 725/* In predict.c */
2eb712b4 726extern bool maybe_hot_bb_p (struct function *, const_basic_block);
3250d724 727extern bool maybe_hot_edge_p (edge);
2eb712b4 728extern bool probably_never_executed_bb_p (struct function *, const_basic_block);
600b5b1d 729extern bool probably_never_executed_edge_p (struct function *, edge);
cc870036
JH
730extern bool optimize_bb_for_size_p (const_basic_block);
731extern bool optimize_bb_for_speed_p (const_basic_block);
bf08ebeb
JH
732extern bool optimize_edge_for_size_p (edge);
733extern bool optimize_edge_for_speed_p (edge);
cc870036
JH
734extern bool optimize_loop_for_size_p (struct loop *);
735extern bool optimize_loop_for_speed_p (struct loop *);
efd8f750
JH
736extern bool optimize_loop_nest_for_size_p (struct loop *);
737extern bool optimize_loop_nest_for_speed_p (struct loop *);
726a989a 738extern bool gimple_predicted_by_p (const_basic_block, enum br_predictor);
9678086d 739extern bool rtl_predicted_by_p (const_basic_block, enum br_predictor);
726a989a 740extern void gimple_predict_edge (edge, enum br_predictor, int);
6de9cd9a
DN
741extern void rtl_predict_edge (edge, enum br_predictor, int);
742extern void predict_edge_def (edge, enum br_predictor, enum prediction);
87022a6b 743extern void guess_outgoing_edge_probabilities (basic_block);
3809e990 744extern void remove_predictions_associated_with_edge (edge);
ed7a4b4b
KG
745extern bool edge_probability_reliable_p (const_edge);
746extern bool br_prob_note_reliable_p (const_rtx);
3a4fd356 747extern bool predictable_edge_p (edge);
f1ebdfc5 748
6fb5fa3c 749/* In cfg.c */
9defb1fe 750extern void init_flow (struct function *);
f55ade6e
AJ
751extern void debug_bb (basic_block);
752extern basic_block debug_bb_n (int);
532aafad 753extern void dump_flow_info (FILE *, int);
f55ade6e
AJ
754extern void expunge_block (basic_block);
755extern void link_block (basic_block, basic_block);
756extern void unlink_block (basic_block);
757extern void compact_blocks (void);
758extern basic_block alloc_block (void);
f55ade6e
AJ
759extern void alloc_aux_for_blocks (int);
760extern void clear_aux_for_blocks (void);
761extern void free_aux_for_blocks (void);
039496da 762extern void alloc_aux_for_edge (edge, int);
f55ade6e
AJ
763extern void alloc_aux_for_edges (int);
764extern void clear_aux_for_edges (void);
765extern void free_aux_for_edges (void);
6fb5fa3c
DB
766
767/* In cfganal.c */
768extern void find_unreachable_blocks (void);
532aafad
SB
769extern bool mark_dfs_back_edges (void);
770struct edge_list * create_edge_list (void);
771void free_edge_list (struct edge_list *);
772void print_edge_list (FILE *, struct edge_list *);
773void verify_edge_list (FILE *, struct edge_list *);
774int find_edge_index (struct edge_list *, basic_block, basic_block);
775edge find_edge (basic_block, basic_block);
776extern void remove_fake_edges (void);
777extern void remove_fake_exit_edges (void);
778extern void add_noreturn_fake_exit_edges (void);
779extern void connect_infinite_loops_to_exit (void);
780extern int post_order_compute (int *, bool, bool);
03b06a83 781extern basic_block dfs_find_deadend (basic_block);
532aafad
SB
782extern int inverted_post_order_compute (int *);
783extern int pre_and_rev_post_order_compute (int *, int *, bool);
784extern int dfs_enumerate_from (basic_block, int,
785 bool (*)(const_basic_block, const void *),
786 basic_block *, int, const void *);
787extern void compute_dominance_frontiers (struct bitmap_head_def *);
788extern bitmap compute_idf (bitmap, struct bitmap_head_def *);
6fb5fa3c
DB
789
790/* In cfgrtl.c */
6fb5fa3c 791extern rtx block_label (basic_block);
ef2be249 792extern rtx bb_note (basic_block);
6fb5fa3c
DB
793extern bool purge_all_dead_edges (void);
794extern bool purge_dead_edges (basic_block);
ba5e9aca 795extern bool fixup_abnormal_edges (void);
26898771 796extern basic_block force_nonfallthru_and_redirect (edge, basic_block, rtx);
2407343c 797extern bool contains_no_active_insn_p (const_basic_block);
532aafad
SB
798extern bool forwarder_block_p (const_basic_block);
799extern bool can_fallthru (basic_block, basic_block);
3371a64f 800extern void emit_barrier_after_bb (basic_block bb);
600b5b1d 801extern void fixup_partitions (void);
6fb5fa3c
DB
802
803/* In cfgbuild.c. */
804extern void find_many_sub_basic_blocks (sbitmap);
805extern void rtl_make_eh_edge (sbitmap, basic_block, rtx);
6fb5fa3c 806
472c95f5
TV
807enum replace_direction { dir_none, dir_forward, dir_backward, dir_both };
808
6fb5fa3c 809/* In cfgcleanup.c. */
6de9cd9a 810extern bool cleanup_cfg (int);
472c95f5
TV
811extern int flow_find_cross_jump (basic_block, basic_block, rtx *, rtx *,
812 enum replace_direction*);
31ce8a53
BS
813extern int flow_find_head_matching_sequence (basic_block, basic_block,
814 rtx *, rtx *, int);
815
6de9cd9a 816extern bool delete_unreachable_blocks (void);
11bdd2ae 817
f55ade6e 818extern void update_br_prob_note (basic_block);
ed7a4b4b
KG
819extern bool inside_basic_block_p (const_rtx);
820extern bool control_flow_insn_p (const_rtx);
96370780 821extern rtx get_last_bb_insn (basic_block);
11bdd2ae 822
f8032688
MM
823/* In dominance.c */
824
825enum cdi_direction
826{
2b28c07a
JC
827 CDI_DOMINATORS = 1,
828 CDI_POST_DOMINATORS = 2
f8032688
MM
829};
830
2b28c07a
JC
831extern enum dom_state dom_info_state (enum cdi_direction);
832extern void set_dom_info_availability (enum cdi_direction, enum dom_state);
fce22de5 833extern bool dom_info_available_p (enum cdi_direction);
d47cc544
SB
834extern void calculate_dominance_info (enum cdi_direction);
835extern void free_dominance_info (enum cdi_direction);
836extern basic_block nearest_common_dominator (enum cdi_direction,
f55ade6e 837 basic_block, basic_block);
c22cacf3 838extern basic_block nearest_common_dominator_for_set (enum cdi_direction,
0bca51f0 839 bitmap);
d47cc544 840extern void set_immediate_dominator (enum cdi_direction, basic_block,
f55ade6e 841 basic_block);
d47cc544 842extern basic_block get_immediate_dominator (enum cdi_direction, basic_block);
ed7a4b4b 843extern bool dominated_by_p (enum cdi_direction, const_basic_block, const_basic_block);
9771b263
DN
844extern vec<basic_block> get_dominated_by (enum cdi_direction, basic_block);
845extern vec<basic_block> get_dominated_by_region (enum cdi_direction,
66f97d31
ZD
846 basic_block *,
847 unsigned);
9771b263 848extern vec<basic_block> get_dominated_to_depth (enum cdi_direction,
cad9aa15 849 basic_block, int);
9771b263 850extern vec<basic_block> get_all_dominated_blocks (enum cdi_direction,
438c239d 851 basic_block);
d47cc544
SB
852extern void add_to_dominance_info (enum cdi_direction, basic_block);
853extern void delete_from_dominance_info (enum cdi_direction, basic_block);
66f97d31 854basic_block recompute_dominator (enum cdi_direction, basic_block);
d47cc544 855extern void redirect_immediate_dominators (enum cdi_direction, basic_block,
f55ade6e 856 basic_block);
66f97d31 857extern void iterate_fix_dominators (enum cdi_direction,
9771b263 858 vec<basic_block> , bool);
d47cc544
SB
859extern void verify_dominators (enum cdi_direction);
860extern basic_block first_dom_son (enum cdi_direction, basic_block);
861extern basic_block next_dom_son (enum cdi_direction, basic_block);
f074ff6c
ZD
862unsigned bb_dom_dfs_in (enum cdi_direction, basic_block);
863unsigned bb_dom_dfs_out (enum cdi_direction, basic_block);
864
6de9cd9a 865extern edge try_redirect_by_replacing_jump (edge, basic_block, bool);
12c3874e 866extern void break_superblocks (void);
ad21dab7 867extern void relink_block_chain (bool);
15db5571 868extern void update_bb_profile_for_threading (basic_block, int, gcov_type, edge);
5e2d947c 869extern void init_rtl_bb_info (basic_block);
9ee634e3 870
6580ee77
JH
871extern void initialize_original_copy_tables (void);
872extern void free_original_copy_tables (void);
873extern void set_bb_original (basic_block, basic_block);
874extern basic_block get_bb_original (basic_block);
875extern void set_bb_copy (basic_block, basic_block);
876extern basic_block get_bb_copy (basic_block);
561e8a90
ZD
877void set_loop_copy (struct loop *, struct loop *);
878struct loop *get_loop_copy (struct loop *);
879
9ee634e3
JH
880#include "cfghooks.h"
881
f66fd328 882/* Return true when one of the predecessor edges of BB is marked with EDGE_EH. */
bae8b6b2
SB
883static inline bool
884bb_has_eh_pred (basic_block bb)
fcc42bca
AK
885{
886 edge e;
887 edge_iterator ei;
888
889 FOR_EACH_EDGE (e, ei, bb->preds)
890 {
891 if (e->flags & EDGE_EH)
892 return true;
893 }
894 return false;
895}
896
ba49cb7b
KZ
897/* Return true when one of the predecessor edges of BB is marked with EDGE_ABNORMAL. */
898static inline bool
899bb_has_abnormal_pred (basic_block bb)
900{
901 edge e;
902 edge_iterator ei;
903
904 FOR_EACH_EDGE (e, ei, bb->preds)
905 {
906 if (e->flags & EDGE_ABNORMAL)
907 return true;
908 }
909 return false;
910}
911
0fd4b31d
NF
912/* Return the fallthru edge in EDGES if it exists, NULL otherwise. */
913static inline edge
9771b263 914find_fallthru_edge (vec<edge, va_gc> *edges)
0fd4b31d
NF
915{
916 edge e;
917 edge_iterator ei;
918
919 FOR_EACH_EDGE (e, ei, edges)
920 if (e->flags & EDGE_FALLTHRU)
921 break;
922
923 return e;
924}
925
b02b9b53
ZD
926/* In cfgloopmanip.c. */
927extern edge mfb_kj_edge;
bf08ebeb
JH
928extern bool mfb_keep_just (edge);
929
930/* In cfgexpand.c. */
931extern void rtl_profile_for_bb (basic_block);
932extern void rtl_profile_for_edge (edge);
933extern void default_rtl_profile (void);
b02b9b53 934
9f71de84 935/* In profile.c. */
f57ddb5b 936typedef struct gcov_working_set_info gcov_working_set_t;
9f71de84
TJ
937extern gcov_working_set_t *find_working_set(unsigned pct_times_10);
938
e78410bf
JH
939/* Check tha probability is sane. */
940
941static inline void
942check_probability (int prob)
943{
944 gcc_checking_assert (prob >= 0 && prob <= REG_BR_PROB_BASE);
945}
946
947/* Given PROB1 and PROB2, return PROB1*PROB2/REG_BR_PROB_BASE.
948 Used to combine BB probabilities. */
949
950static inline int
951combine_probabilities (int prob1, int prob2)
952{
953 check_probability (prob1);
954 check_probability (prob2);
955 return RDIV (prob1 * prob2, REG_BR_PROB_BASE);
956}
957
f41f80f9
TJ
958/* Apply scale factor SCALE on frequency or count FREQ. Use this
959 interface when potentially scaling up, so that SCALE is not
960 constrained to be < REG_BR_PROB_BASE. */
961
962static inline gcov_type
963apply_scale (gcov_type freq, int scale)
964{
965 return RDIV (freq * scale, REG_BR_PROB_BASE);
966}
967
e78410bf
JH
968/* Apply probability PROB on frequency or count FREQ. */
969
970static inline gcov_type
971apply_probability (gcov_type freq, int prob)
972{
973 check_probability (prob);
f41f80f9 974 return apply_scale (freq, prob);
e78410bf
JH
975}
976
977/* Return inverse probability for PROB. */
978
979static inline int
980inverse_probability (int prob1)
981{
982 check_probability (prob1);
983 return REG_BR_PROB_BASE - prob1;
984}
88657302 985#endif /* GCC_BASIC_BLOCK_H */