1*38fd1498Szrj /* Define control flow data structures for the CFG.
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 #ifndef GCC_BASIC_BLOCK_H
21*38fd1498Szrj #define GCC_BASIC_BLOCK_H
22*38fd1498Szrj
23*38fd1498Szrj #include <profile-count.h>
24*38fd1498Szrj
25*38fd1498Szrj /* Control flow edge information. */
26*38fd1498Szrj struct GTY((user)) edge_def {
27*38fd1498Szrj /* The two blocks at the ends of the edge. */
28*38fd1498Szrj basic_block src;
29*38fd1498Szrj basic_block dest;
30*38fd1498Szrj
31*38fd1498Szrj /* Instructions queued on the edge. */
32*38fd1498Szrj union edge_def_insns {
33*38fd1498Szrj gimple_seq g;
34*38fd1498Szrj rtx_insn *r;
35*38fd1498Szrj } insns;
36*38fd1498Szrj
37*38fd1498Szrj /* Auxiliary info specific to a pass. */
38*38fd1498Szrj PTR aux;
39*38fd1498Szrj
40*38fd1498Szrj /* Location of any goto implicit in the edge. */
41*38fd1498Szrj location_t goto_locus;
42*38fd1498Szrj
43*38fd1498Szrj /* The index number corresponding to this edge in the edge vector
44*38fd1498Szrj dest->preds. */
45*38fd1498Szrj unsigned int dest_idx;
46*38fd1498Szrj
47*38fd1498Szrj int flags; /* see cfg-flags.def */
48*38fd1498Szrj profile_probability probability;
49*38fd1498Szrj
50*38fd1498Szrj /* Return count of edge E. */
51*38fd1498Szrj inline profile_count count () const;
52*38fd1498Szrj };
53*38fd1498Szrj
54*38fd1498Szrj /* Masks for edge.flags. */
55*38fd1498Szrj #define DEF_EDGE_FLAG(NAME,IDX) EDGE_##NAME = 1 << IDX ,
56*38fd1498Szrj enum cfg_edge_flags {
57*38fd1498Szrj #include "cfg-flags.def"
58*38fd1498Szrj LAST_CFG_EDGE_FLAG /* this is only used for EDGE_ALL_FLAGS */
59*38fd1498Szrj };
60*38fd1498Szrj #undef DEF_EDGE_FLAG
61*38fd1498Szrj
62*38fd1498Szrj /* Bit mask for all edge flags. */
63*38fd1498Szrj #define EDGE_ALL_FLAGS ((LAST_CFG_EDGE_FLAG - 1) * 2 - 1)
64*38fd1498Szrj
65*38fd1498Szrj /* The following four flags all indicate something special about an edge.
66*38fd1498Szrj Test the edge flags on EDGE_COMPLEX to detect all forms of "strange"
67*38fd1498Szrj control flow transfers. */
68*38fd1498Szrj #define EDGE_COMPLEX \
69*38fd1498Szrj (EDGE_ABNORMAL | EDGE_ABNORMAL_CALL | EDGE_EH | EDGE_PRESERVE)
70*38fd1498Szrj
71*38fd1498Szrj struct GTY(()) rtl_bb_info {
72*38fd1498Szrj /* The first insn of the block is embedded into bb->il.x. */
73*38fd1498Szrj /* The last insn of the block. */
74*38fd1498Szrj rtx_insn *end_;
75*38fd1498Szrj
76*38fd1498Szrj /* In CFGlayout mode points to insn notes/jumptables to be placed just before
77*38fd1498Szrj and after the block. */
78*38fd1498Szrj rtx_insn *header_;
79*38fd1498Szrj rtx_insn *footer_;
80*38fd1498Szrj };
81*38fd1498Szrj
82*38fd1498Szrj struct GTY(()) gimple_bb_info {
83*38fd1498Szrj /* Sequence of statements in this block. */
84*38fd1498Szrj gimple_seq seq;
85*38fd1498Szrj
86*38fd1498Szrj /* PHI nodes for this block. */
87*38fd1498Szrj gimple_seq phi_nodes;
88*38fd1498Szrj };
89*38fd1498Szrj
90*38fd1498Szrj /* A basic block is a sequence of instructions with only one entry and
91*38fd1498Szrj only one exit. If any one of the instructions are executed, they
92*38fd1498Szrj will all be executed, and in sequence from first to last.
93*38fd1498Szrj
94*38fd1498Szrj There may be COND_EXEC instructions in the basic block. The
95*38fd1498Szrj COND_EXEC *instructions* will be executed -- but if the condition
96*38fd1498Szrj is false the conditionally executed *expressions* will of course
97*38fd1498Szrj not be executed. We don't consider the conditionally executed
98*38fd1498Szrj expression (which might have side-effects) to be in a separate
99*38fd1498Szrj basic block because the program counter will always be at the same
100*38fd1498Szrj location after the COND_EXEC instruction, regardless of whether the
101*38fd1498Szrj condition is true or not.
102*38fd1498Szrj
103*38fd1498Szrj Basic blocks need not start with a label nor end with a jump insn.
104*38fd1498Szrj For example, a previous basic block may just "conditionally fall"
105*38fd1498Szrj into the succeeding basic block, and the last basic block need not
106*38fd1498Szrj end with a jump insn. Block 0 is a descendant of the entry block.
107*38fd1498Szrj
108*38fd1498Szrj A basic block beginning with two labels cannot have notes between
109*38fd1498Szrj the labels.
110*38fd1498Szrj
111*38fd1498Szrj Data for jump tables are stored in jump_insns that occur in no
112*38fd1498Szrj basic block even though these insns can follow or precede insns in
113*38fd1498Szrj basic blocks. */
114*38fd1498Szrj
115*38fd1498Szrj /* Basic block information indexed by block number. */
116*38fd1498Szrj struct GTY((chain_next ("%h.next_bb"), chain_prev ("%h.prev_bb"))) basic_block_def {
117*38fd1498Szrj /* The edges into and out of the block. */
118*38fd1498Szrj vec<edge, va_gc> *preds;
119*38fd1498Szrj vec<edge, va_gc> *succs;
120*38fd1498Szrj
121*38fd1498Szrj /* Auxiliary info specific to a pass. */
122*38fd1498Szrj PTR GTY ((skip (""))) aux;
123*38fd1498Szrj
124*38fd1498Szrj /* Innermost loop containing the block. */
125*38fd1498Szrj struct loop *loop_father;
126*38fd1498Szrj
127*38fd1498Szrj /* The dominance and postdominance information node. */
128*38fd1498Szrj struct et_node * GTY ((skip (""))) dom[2];
129*38fd1498Szrj
130*38fd1498Szrj /* Previous and next blocks in the chain. */
131*38fd1498Szrj basic_block prev_bb;
132*38fd1498Szrj basic_block next_bb;
133*38fd1498Szrj
134*38fd1498Szrj union basic_block_il_dependent {
135*38fd1498Szrj struct gimple_bb_info GTY ((tag ("0"))) gimple;
136*38fd1498Szrj struct {
137*38fd1498Szrj rtx_insn *head_;
138*38fd1498Szrj struct rtl_bb_info * rtl;
139*38fd1498Szrj } GTY ((tag ("1"))) x;
140*38fd1498Szrj } GTY ((desc ("((%1.flags & BB_RTL) != 0)"))) il;
141*38fd1498Szrj
142*38fd1498Szrj /* Various flags. See cfg-flags.def. */
143*38fd1498Szrj int flags;
144*38fd1498Szrj
145*38fd1498Szrj /* The index of this block. */
146*38fd1498Szrj int index;
147*38fd1498Szrj
148*38fd1498Szrj /* Expected number of executions: calculated in profile.c. */
149*38fd1498Szrj profile_count count;
150*38fd1498Szrj
151*38fd1498Szrj /* The discriminator for this block. The discriminator distinguishes
152*38fd1498Szrj among several basic blocks that share a common locus, allowing for
153*38fd1498Szrj more accurate sample-based profiling. */
154*38fd1498Szrj int discriminator;
155*38fd1498Szrj };
156*38fd1498Szrj
157*38fd1498Szrj /* This ensures that struct gimple_bb_info is smaller than
158*38fd1498Szrj struct rtl_bb_info, so that inlining the former into basic_block_def
159*38fd1498Szrj is the better choice. */
160*38fd1498Szrj typedef int __assert_gimple_bb_smaller_rtl_bb
161*38fd1498Szrj [(int) sizeof (struct rtl_bb_info)
162*38fd1498Szrj - (int) sizeof (struct gimple_bb_info)];
163*38fd1498Szrj
164*38fd1498Szrj
165*38fd1498Szrj #define BB_FREQ_MAX 10000
166*38fd1498Szrj
167*38fd1498Szrj /* Masks for basic_block.flags. */
168*38fd1498Szrj #define DEF_BASIC_BLOCK_FLAG(NAME,IDX) BB_##NAME = 1 << IDX ,
169*38fd1498Szrj enum cfg_bb_flags
170*38fd1498Szrj {
171*38fd1498Szrj #include "cfg-flags.def"
172*38fd1498Szrj LAST_CFG_BB_FLAG /* this is only used for BB_ALL_FLAGS */
173*38fd1498Szrj };
174*38fd1498Szrj #undef DEF_BASIC_BLOCK_FLAG
175*38fd1498Szrj
176*38fd1498Szrj /* Bit mask for all basic block flags. */
177*38fd1498Szrj #define BB_ALL_FLAGS ((LAST_CFG_BB_FLAG - 1) * 2 - 1)
178*38fd1498Szrj
179*38fd1498Szrj /* Bit mask for all basic block flags that must be preserved. These are
180*38fd1498Szrj the bit masks that are *not* cleared by clear_bb_flags. */
181*38fd1498Szrj #define BB_FLAGS_TO_PRESERVE \
182*38fd1498Szrj (BB_DISABLE_SCHEDULE | BB_RTL | BB_NON_LOCAL_GOTO_TARGET \
183*38fd1498Szrj | BB_HOT_PARTITION | BB_COLD_PARTITION)
184*38fd1498Szrj
185*38fd1498Szrj /* Dummy bitmask for convenience in the hot/cold partitioning code. */
186*38fd1498Szrj #define BB_UNPARTITIONED 0
187*38fd1498Szrj
188*38fd1498Szrj /* Partitions, to be used when partitioning hot and cold basic blocks into
189*38fd1498Szrj separate sections. */
190*38fd1498Szrj #define BB_PARTITION(bb) ((bb)->flags & (BB_HOT_PARTITION|BB_COLD_PARTITION))
191*38fd1498Szrj #define BB_SET_PARTITION(bb, part) do { \
192*38fd1498Szrj basic_block bb_ = (bb); \
193*38fd1498Szrj bb_->flags = ((bb_->flags & ~(BB_HOT_PARTITION|BB_COLD_PARTITION)) \
194*38fd1498Szrj | (part)); \
195*38fd1498Szrj } while (0)
196*38fd1498Szrj
197*38fd1498Szrj #define BB_COPY_PARTITION(dstbb, srcbb) \
198*38fd1498Szrj BB_SET_PARTITION (dstbb, BB_PARTITION (srcbb))
199*38fd1498Szrj
200*38fd1498Szrj /* Defines for accessing the fields of the CFG structure for function FN. */
201*38fd1498Szrj #define ENTRY_BLOCK_PTR_FOR_FN(FN) ((FN)->cfg->x_entry_block_ptr)
202*38fd1498Szrj #define EXIT_BLOCK_PTR_FOR_FN(FN) ((FN)->cfg->x_exit_block_ptr)
203*38fd1498Szrj #define basic_block_info_for_fn(FN) ((FN)->cfg->x_basic_block_info)
204*38fd1498Szrj #define n_basic_blocks_for_fn(FN) ((FN)->cfg->x_n_basic_blocks)
205*38fd1498Szrj #define n_edges_for_fn(FN) ((FN)->cfg->x_n_edges)
206*38fd1498Szrj #define last_basic_block_for_fn(FN) ((FN)->cfg->x_last_basic_block)
207*38fd1498Szrj #define label_to_block_map_for_fn(FN) ((FN)->cfg->x_label_to_block_map)
208*38fd1498Szrj #define profile_status_for_fn(FN) ((FN)->cfg->x_profile_status)
209*38fd1498Szrj
210*38fd1498Szrj #define BASIC_BLOCK_FOR_FN(FN,N) \
211*38fd1498Szrj ((*basic_block_info_for_fn (FN))[(N)])
212*38fd1498Szrj #define SET_BASIC_BLOCK_FOR_FN(FN,N,BB) \
213*38fd1498Szrj ((*basic_block_info_for_fn (FN))[(N)] = (BB))
214*38fd1498Szrj
215*38fd1498Szrj /* For iterating over basic blocks. */
216*38fd1498Szrj #define FOR_BB_BETWEEN(BB, FROM, TO, DIR) \
217*38fd1498Szrj for (BB = FROM; BB != TO; BB = BB->DIR)
218*38fd1498Szrj
219*38fd1498Szrj #define FOR_EACH_BB_FN(BB, FN) \
220*38fd1498Szrj FOR_BB_BETWEEN (BB, (FN)->cfg->x_entry_block_ptr->next_bb, (FN)->cfg->x_exit_block_ptr, next_bb)
221*38fd1498Szrj
222*38fd1498Szrj #define FOR_EACH_BB_REVERSE_FN(BB, FN) \
223*38fd1498Szrj FOR_BB_BETWEEN (BB, (FN)->cfg->x_exit_block_ptr->prev_bb, (FN)->cfg->x_entry_block_ptr, prev_bb)
224*38fd1498Szrj
225*38fd1498Szrj /* For iterating over insns in basic block. */
226*38fd1498Szrj #define FOR_BB_INSNS(BB, INSN) \
227*38fd1498Szrj for ((INSN) = BB_HEAD (BB); \
228*38fd1498Szrj (INSN) && (INSN) != NEXT_INSN (BB_END (BB)); \
229*38fd1498Szrj (INSN) = NEXT_INSN (INSN))
230*38fd1498Szrj
231*38fd1498Szrj /* For iterating over insns in basic block when we might remove the
232*38fd1498Szrj current insn. */
233*38fd1498Szrj #define FOR_BB_INSNS_SAFE(BB, INSN, CURR) \
234*38fd1498Szrj for ((INSN) = BB_HEAD (BB), (CURR) = (INSN) ? NEXT_INSN ((INSN)): NULL; \
235*38fd1498Szrj (INSN) && (INSN) != NEXT_INSN (BB_END (BB)); \
236*38fd1498Szrj (INSN) = (CURR), (CURR) = (INSN) ? NEXT_INSN ((INSN)) : NULL)
237*38fd1498Szrj
238*38fd1498Szrj #define FOR_BB_INSNS_REVERSE(BB, INSN) \
239*38fd1498Szrj for ((INSN) = BB_END (BB); \
240*38fd1498Szrj (INSN) && (INSN) != PREV_INSN (BB_HEAD (BB)); \
241*38fd1498Szrj (INSN) = PREV_INSN (INSN))
242*38fd1498Szrj
243*38fd1498Szrj #define FOR_BB_INSNS_REVERSE_SAFE(BB, INSN, CURR) \
244*38fd1498Szrj for ((INSN) = BB_END (BB),(CURR) = (INSN) ? PREV_INSN ((INSN)) : NULL; \
245*38fd1498Szrj (INSN) && (INSN) != PREV_INSN (BB_HEAD (BB)); \
246*38fd1498Szrj (INSN) = (CURR), (CURR) = (INSN) ? PREV_INSN ((INSN)) : NULL)
247*38fd1498Szrj
248*38fd1498Szrj /* Cycles through _all_ basic blocks, even the fake ones (entry and
249*38fd1498Szrj exit block). */
250*38fd1498Szrj
251*38fd1498Szrj #define FOR_ALL_BB_FN(BB, FN) \
252*38fd1498Szrj for (BB = ENTRY_BLOCK_PTR_FOR_FN (FN); BB; BB = BB->next_bb)
253*38fd1498Szrj
254*38fd1498Szrj
255*38fd1498Szrj /* Stuff for recording basic block info. */
256*38fd1498Szrj
257*38fd1498Szrj /* For now, these will be functions (so that they can include checked casts
258*38fd1498Szrj to rtx_insn. Once the underlying fields are converted from rtx
259*38fd1498Szrj to rtx_insn, these can be converted back to macros. */
260*38fd1498Szrj
261*38fd1498Szrj #define BB_HEAD(B) (B)->il.x.head_
262*38fd1498Szrj #define BB_END(B) (B)->il.x.rtl->end_
263*38fd1498Szrj #define BB_HEADER(B) (B)->il.x.rtl->header_
264*38fd1498Szrj #define BB_FOOTER(B) (B)->il.x.rtl->footer_
265*38fd1498Szrj
266*38fd1498Szrj /* Special block numbers [markers] for entry and exit.
267*38fd1498Szrj Neither of them is supposed to hold actual statements. */
268*38fd1498Szrj #define ENTRY_BLOCK (0)
269*38fd1498Szrj #define EXIT_BLOCK (1)
270*38fd1498Szrj
271*38fd1498Szrj /* The two blocks that are always in the cfg. */
272*38fd1498Szrj #define NUM_FIXED_BLOCKS (2)
273*38fd1498Szrj
274*38fd1498Szrj /* This is the value which indicates no edge is present. */
275*38fd1498Szrj #define EDGE_INDEX_NO_EDGE -1
276*38fd1498Szrj
277*38fd1498Szrj /* EDGE_INDEX returns an integer index for an edge, or EDGE_INDEX_NO_EDGE
278*38fd1498Szrj if there is no edge between the 2 basic blocks. */
279*38fd1498Szrj #define EDGE_INDEX(el, pred, succ) (find_edge_index ((el), (pred), (succ)))
280*38fd1498Szrj
281*38fd1498Szrj /* INDEX_EDGE_PRED_BB and INDEX_EDGE_SUCC_BB return a pointer to the basic
282*38fd1498Szrj block which is either the pred or succ end of the indexed edge. */
283*38fd1498Szrj #define INDEX_EDGE_PRED_BB(el, index) ((el)->index_to_edge[(index)]->src)
284*38fd1498Szrj #define INDEX_EDGE_SUCC_BB(el, index) ((el)->index_to_edge[(index)]->dest)
285*38fd1498Szrj
286*38fd1498Szrj /* INDEX_EDGE returns a pointer to the edge. */
287*38fd1498Szrj #define INDEX_EDGE(el, index) ((el)->index_to_edge[(index)])
288*38fd1498Szrj
289*38fd1498Szrj /* Number of edges in the compressed edge list. */
290*38fd1498Szrj #define NUM_EDGES(el) ((el)->num_edges)
291*38fd1498Szrj
292*38fd1498Szrj /* BB is assumed to contain conditional jump. Return the fallthru edge. */
293*38fd1498Szrj #define FALLTHRU_EDGE(bb) (EDGE_SUCC ((bb), 0)->flags & EDGE_FALLTHRU \
294*38fd1498Szrj ? EDGE_SUCC ((bb), 0) : EDGE_SUCC ((bb), 1))
295*38fd1498Szrj
296*38fd1498Szrj /* BB is assumed to contain conditional jump. Return the branch edge. */
297*38fd1498Szrj #define BRANCH_EDGE(bb) (EDGE_SUCC ((bb), 0)->flags & EDGE_FALLTHRU \
298*38fd1498Szrj ? EDGE_SUCC ((bb), 1) : EDGE_SUCC ((bb), 0))
299*38fd1498Szrj
300*38fd1498Szrj /* Return expected execution frequency of the edge E. */
301*38fd1498Szrj #define EDGE_FREQUENCY(e) e->count ().to_frequency (cfun)
302*38fd1498Szrj
303*38fd1498Szrj /* Compute a scale factor (or probability) suitable for scaling of
304*38fd1498Szrj gcov_type values via apply_probability() and apply_scale(). */
305*38fd1498Szrj #define GCOV_COMPUTE_SCALE(num,den) \
306*38fd1498Szrj ((den) ? RDIV ((num) * REG_BR_PROB_BASE, (den)) : REG_BR_PROB_BASE)
307*38fd1498Szrj
308*38fd1498Szrj /* Return nonzero if edge is critical. */
309*38fd1498Szrj #define EDGE_CRITICAL_P(e) (EDGE_COUNT ((e)->src->succs) >= 2 \
310*38fd1498Szrj && EDGE_COUNT ((e)->dest->preds) >= 2)
311*38fd1498Szrj
312*38fd1498Szrj #define EDGE_COUNT(ev) vec_safe_length (ev)
313*38fd1498Szrj #define EDGE_I(ev,i) (*ev)[(i)]
314*38fd1498Szrj #define EDGE_PRED(bb,i) (*(bb)->preds)[(i)]
315*38fd1498Szrj #define EDGE_SUCC(bb,i) (*(bb)->succs)[(i)]
316*38fd1498Szrj
317*38fd1498Szrj /* Returns true if BB has precisely one successor. */
318*38fd1498Szrj
319*38fd1498Szrj static inline bool
single_succ_p(const_basic_block bb)320*38fd1498Szrj single_succ_p (const_basic_block bb)
321*38fd1498Szrj {
322*38fd1498Szrj return EDGE_COUNT (bb->succs) == 1;
323*38fd1498Szrj }
324*38fd1498Szrj
325*38fd1498Szrj /* Returns true if BB has precisely one predecessor. */
326*38fd1498Szrj
327*38fd1498Szrj static inline bool
single_pred_p(const_basic_block bb)328*38fd1498Szrj single_pred_p (const_basic_block bb)
329*38fd1498Szrj {
330*38fd1498Szrj return EDGE_COUNT (bb->preds) == 1;
331*38fd1498Szrj }
332*38fd1498Szrj
333*38fd1498Szrj /* Returns the single successor edge of basic block BB. Aborts if
334*38fd1498Szrj BB does not have exactly one successor. */
335*38fd1498Szrj
336*38fd1498Szrj static inline edge
single_succ_edge(const_basic_block bb)337*38fd1498Szrj single_succ_edge (const_basic_block bb)
338*38fd1498Szrj {
339*38fd1498Szrj gcc_checking_assert (single_succ_p (bb));
340*38fd1498Szrj return EDGE_SUCC (bb, 0);
341*38fd1498Szrj }
342*38fd1498Szrj
343*38fd1498Szrj /* Returns the single predecessor edge of basic block BB. Aborts
344*38fd1498Szrj if BB does not have exactly one predecessor. */
345*38fd1498Szrj
346*38fd1498Szrj static inline edge
single_pred_edge(const_basic_block bb)347*38fd1498Szrj single_pred_edge (const_basic_block bb)
348*38fd1498Szrj {
349*38fd1498Szrj gcc_checking_assert (single_pred_p (bb));
350*38fd1498Szrj return EDGE_PRED (bb, 0);
351*38fd1498Szrj }
352*38fd1498Szrj
353*38fd1498Szrj /* Returns the single successor block of basic block BB. Aborts
354*38fd1498Szrj if BB does not have exactly one successor. */
355*38fd1498Szrj
356*38fd1498Szrj static inline basic_block
single_succ(const_basic_block bb)357*38fd1498Szrj single_succ (const_basic_block bb)
358*38fd1498Szrj {
359*38fd1498Szrj return single_succ_edge (bb)->dest;
360*38fd1498Szrj }
361*38fd1498Szrj
362*38fd1498Szrj /* Returns the single predecessor block of basic block BB. Aborts
363*38fd1498Szrj if BB does not have exactly one predecessor.*/
364*38fd1498Szrj
365*38fd1498Szrj static inline basic_block
single_pred(const_basic_block bb)366*38fd1498Szrj single_pred (const_basic_block bb)
367*38fd1498Szrj {
368*38fd1498Szrj return single_pred_edge (bb)->src;
369*38fd1498Szrj }
370*38fd1498Szrj
371*38fd1498Szrj /* Iterator object for edges. */
372*38fd1498Szrj
373*38fd1498Szrj struct edge_iterator {
374*38fd1498Szrj unsigned index;
375*38fd1498Szrj vec<edge, va_gc> **container;
376*38fd1498Szrj };
377*38fd1498Szrj
378*38fd1498Szrj static inline vec<edge, va_gc> *
ei_container(edge_iterator i)379*38fd1498Szrj ei_container (edge_iterator i)
380*38fd1498Szrj {
381*38fd1498Szrj gcc_checking_assert (i.container);
382*38fd1498Szrj return *i.container;
383*38fd1498Szrj }
384*38fd1498Szrj
385*38fd1498Szrj #define ei_start(iter) ei_start_1 (&(iter))
386*38fd1498Szrj #define ei_last(iter) ei_last_1 (&(iter))
387*38fd1498Szrj
388*38fd1498Szrj /* Return an iterator pointing to the start of an edge vector. */
389*38fd1498Szrj static inline edge_iterator
ei_start_1(vec<edge,va_gc> ** ev)390*38fd1498Szrj ei_start_1 (vec<edge, va_gc> **ev)
391*38fd1498Szrj {
392*38fd1498Szrj edge_iterator i;
393*38fd1498Szrj
394*38fd1498Szrj i.index = 0;
395*38fd1498Szrj i.container = ev;
396*38fd1498Szrj
397*38fd1498Szrj return i;
398*38fd1498Szrj }
399*38fd1498Szrj
400*38fd1498Szrj /* Return an iterator pointing to the last element of an edge
401*38fd1498Szrj vector. */
402*38fd1498Szrj static inline edge_iterator
ei_last_1(vec<edge,va_gc> ** ev)403*38fd1498Szrj ei_last_1 (vec<edge, va_gc> **ev)
404*38fd1498Szrj {
405*38fd1498Szrj edge_iterator i;
406*38fd1498Szrj
407*38fd1498Szrj i.index = EDGE_COUNT (*ev) - 1;
408*38fd1498Szrj i.container = ev;
409*38fd1498Szrj
410*38fd1498Szrj return i;
411*38fd1498Szrj }
412*38fd1498Szrj
413*38fd1498Szrj /* Is the iterator `i' at the end of the sequence? */
414*38fd1498Szrj static inline bool
ei_end_p(edge_iterator i)415*38fd1498Szrj ei_end_p (edge_iterator i)
416*38fd1498Szrj {
417*38fd1498Szrj return (i.index == EDGE_COUNT (ei_container (i)));
418*38fd1498Szrj }
419*38fd1498Szrj
420*38fd1498Szrj /* Is the iterator `i' at one position before the end of the
421*38fd1498Szrj sequence? */
422*38fd1498Szrj static inline bool
ei_one_before_end_p(edge_iterator i)423*38fd1498Szrj ei_one_before_end_p (edge_iterator i)
424*38fd1498Szrj {
425*38fd1498Szrj return (i.index + 1 == EDGE_COUNT (ei_container (i)));
426*38fd1498Szrj }
427*38fd1498Szrj
428*38fd1498Szrj /* Advance the iterator to the next element. */
429*38fd1498Szrj static inline void
ei_next(edge_iterator * i)430*38fd1498Szrj ei_next (edge_iterator *i)
431*38fd1498Szrj {
432*38fd1498Szrj gcc_checking_assert (i->index < EDGE_COUNT (ei_container (*i)));
433*38fd1498Szrj i->index++;
434*38fd1498Szrj }
435*38fd1498Szrj
436*38fd1498Szrj /* Move the iterator to the previous element. */
437*38fd1498Szrj static inline void
ei_prev(edge_iterator * i)438*38fd1498Szrj ei_prev (edge_iterator *i)
439*38fd1498Szrj {
440*38fd1498Szrj gcc_checking_assert (i->index > 0);
441*38fd1498Szrj i->index--;
442*38fd1498Szrj }
443*38fd1498Szrj
444*38fd1498Szrj /* Return the edge pointed to by the iterator `i'. */
445*38fd1498Szrj static inline edge
ei_edge(edge_iterator i)446*38fd1498Szrj ei_edge (edge_iterator i)
447*38fd1498Szrj {
448*38fd1498Szrj return EDGE_I (ei_container (i), i.index);
449*38fd1498Szrj }
450*38fd1498Szrj
451*38fd1498Szrj /* Return an edge pointed to by the iterator. Do it safely so that
452*38fd1498Szrj NULL is returned when the iterator is pointing at the end of the
453*38fd1498Szrj sequence. */
454*38fd1498Szrj static inline edge
ei_safe_edge(edge_iterator i)455*38fd1498Szrj ei_safe_edge (edge_iterator i)
456*38fd1498Szrj {
457*38fd1498Szrj return !ei_end_p (i) ? ei_edge (i) : NULL;
458*38fd1498Szrj }
459*38fd1498Szrj
460*38fd1498Szrj /* Return 1 if we should continue to iterate. Return 0 otherwise.
461*38fd1498Szrj *Edge P is set to the next edge if we are to continue to iterate
462*38fd1498Szrj and NULL otherwise. */
463*38fd1498Szrj
464*38fd1498Szrj static inline bool
ei_cond(edge_iterator ei,edge * p)465*38fd1498Szrj ei_cond (edge_iterator ei, edge *p)
466*38fd1498Szrj {
467*38fd1498Szrj if (!ei_end_p (ei))
468*38fd1498Szrj {
469*38fd1498Szrj *p = ei_edge (ei);
470*38fd1498Szrj return 1;
471*38fd1498Szrj }
472*38fd1498Szrj else
473*38fd1498Szrj {
474*38fd1498Szrj *p = NULL;
475*38fd1498Szrj return 0;
476*38fd1498Szrj }
477*38fd1498Szrj }
478*38fd1498Szrj
479*38fd1498Szrj /* This macro serves as a convenient way to iterate each edge in a
480*38fd1498Szrj vector of predecessor or successor edges. It must not be used when
481*38fd1498Szrj an element might be removed during the traversal, otherwise
482*38fd1498Szrj elements will be missed. Instead, use a for-loop like that shown
483*38fd1498Szrj in the following pseudo-code:
484*38fd1498Szrj
485*38fd1498Szrj FOR (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
486*38fd1498Szrj {
487*38fd1498Szrj IF (e != taken_edge)
488*38fd1498Szrj remove_edge (e);
489*38fd1498Szrj ELSE
490*38fd1498Szrj ei_next (&ei);
491*38fd1498Szrj }
492*38fd1498Szrj */
493*38fd1498Szrj
494*38fd1498Szrj #define FOR_EACH_EDGE(EDGE,ITER,EDGE_VEC) \
495*38fd1498Szrj for ((ITER) = ei_start ((EDGE_VEC)); \
496*38fd1498Szrj ei_cond ((ITER), &(EDGE)); \
497*38fd1498Szrj ei_next (&(ITER)))
498*38fd1498Szrj
499*38fd1498Szrj #define CLEANUP_EXPENSIVE 1 /* Do relatively expensive optimizations
500*38fd1498Szrj except for edge forwarding */
501*38fd1498Szrj #define CLEANUP_CROSSJUMP 2 /* Do crossjumping. */
502*38fd1498Szrj #define CLEANUP_POST_REGSTACK 4 /* We run after reg-stack and need
503*38fd1498Szrj to care REG_DEAD notes. */
504*38fd1498Szrj #define CLEANUP_THREADING 8 /* Do jump threading. */
505*38fd1498Szrj #define CLEANUP_NO_INSN_DEL 16 /* Do not try to delete trivially dead
506*38fd1498Szrj insns. */
507*38fd1498Szrj #define CLEANUP_CFGLAYOUT 32 /* Do cleanup in cfglayout mode. */
508*38fd1498Szrj #define CLEANUP_CFG_CHANGED 64 /* The caller changed the CFG. */
509*38fd1498Szrj #define CLEANUP_NO_PARTITIONING 128 /* Do not try to fix partitions. */
510*38fd1498Szrj
511*38fd1498Szrj /* Return true if BB is in a transaction. */
512*38fd1498Szrj
513*38fd1498Szrj static inline bool
bb_in_transaction(basic_block bb)514*38fd1498Szrj bb_in_transaction (basic_block bb)
515*38fd1498Szrj {
516*38fd1498Szrj return bb->flags & BB_IN_TRANSACTION;
517*38fd1498Szrj }
518*38fd1498Szrj
519*38fd1498Szrj /* Return true when one of the predecessor edges of BB is marked with EDGE_EH. */
520*38fd1498Szrj static inline bool
bb_has_eh_pred(basic_block bb)521*38fd1498Szrj bb_has_eh_pred (basic_block bb)
522*38fd1498Szrj {
523*38fd1498Szrj edge e;
524*38fd1498Szrj edge_iterator ei;
525*38fd1498Szrj
526*38fd1498Szrj FOR_EACH_EDGE (e, ei, bb->preds)
527*38fd1498Szrj {
528*38fd1498Szrj if (e->flags & EDGE_EH)
529*38fd1498Szrj return true;
530*38fd1498Szrj }
531*38fd1498Szrj return false;
532*38fd1498Szrj }
533*38fd1498Szrj
534*38fd1498Szrj /* Return true when one of the predecessor edges of BB is marked with EDGE_ABNORMAL. */
535*38fd1498Szrj static inline bool
bb_has_abnormal_pred(basic_block bb)536*38fd1498Szrj bb_has_abnormal_pred (basic_block bb)
537*38fd1498Szrj {
538*38fd1498Szrj edge e;
539*38fd1498Szrj edge_iterator ei;
540*38fd1498Szrj
541*38fd1498Szrj FOR_EACH_EDGE (e, ei, bb->preds)
542*38fd1498Szrj {
543*38fd1498Szrj if (e->flags & EDGE_ABNORMAL)
544*38fd1498Szrj return true;
545*38fd1498Szrj }
546*38fd1498Szrj return false;
547*38fd1498Szrj }
548*38fd1498Szrj
549*38fd1498Szrj /* Return the fallthru edge in EDGES if it exists, NULL otherwise. */
550*38fd1498Szrj static inline edge
find_fallthru_edge(vec<edge,va_gc> * edges)551*38fd1498Szrj find_fallthru_edge (vec<edge, va_gc> *edges)
552*38fd1498Szrj {
553*38fd1498Szrj edge e;
554*38fd1498Szrj edge_iterator ei;
555*38fd1498Szrj
556*38fd1498Szrj FOR_EACH_EDGE (e, ei, edges)
557*38fd1498Szrj if (e->flags & EDGE_FALLTHRU)
558*38fd1498Szrj break;
559*38fd1498Szrj
560*38fd1498Szrj return e;
561*38fd1498Szrj }
562*38fd1498Szrj
563*38fd1498Szrj /* Check tha probability is sane. */
564*38fd1498Szrj
565*38fd1498Szrj static inline void
check_probability(int prob)566*38fd1498Szrj check_probability (int prob)
567*38fd1498Szrj {
568*38fd1498Szrj gcc_checking_assert (prob >= 0 && prob <= REG_BR_PROB_BASE);
569*38fd1498Szrj }
570*38fd1498Szrj
571*38fd1498Szrj /* Given PROB1 and PROB2, return PROB1*PROB2/REG_BR_PROB_BASE.
572*38fd1498Szrj Used to combine BB probabilities. */
573*38fd1498Szrj
574*38fd1498Szrj static inline int
combine_probabilities(int prob1,int prob2)575*38fd1498Szrj combine_probabilities (int prob1, int prob2)
576*38fd1498Szrj {
577*38fd1498Szrj check_probability (prob1);
578*38fd1498Szrj check_probability (prob2);
579*38fd1498Szrj return RDIV (prob1 * prob2, REG_BR_PROB_BASE);
580*38fd1498Szrj }
581*38fd1498Szrj
582*38fd1498Szrj /* Apply scale factor SCALE on frequency or count FREQ. Use this
583*38fd1498Szrj interface when potentially scaling up, so that SCALE is not
584*38fd1498Szrj constrained to be < REG_BR_PROB_BASE. */
585*38fd1498Szrj
586*38fd1498Szrj static inline gcov_type
apply_scale(gcov_type freq,gcov_type scale)587*38fd1498Szrj apply_scale (gcov_type freq, gcov_type scale)
588*38fd1498Szrj {
589*38fd1498Szrj return RDIV (freq * scale, REG_BR_PROB_BASE);
590*38fd1498Szrj }
591*38fd1498Szrj
592*38fd1498Szrj /* Apply probability PROB on frequency or count FREQ. */
593*38fd1498Szrj
594*38fd1498Szrj static inline gcov_type
apply_probability(gcov_type freq,int prob)595*38fd1498Szrj apply_probability (gcov_type freq, int prob)
596*38fd1498Szrj {
597*38fd1498Szrj check_probability (prob);
598*38fd1498Szrj return apply_scale (freq, prob);
599*38fd1498Szrj }
600*38fd1498Szrj
601*38fd1498Szrj /* Return inverse probability for PROB. */
602*38fd1498Szrj
603*38fd1498Szrj static inline int
inverse_probability(int prob1)604*38fd1498Szrj inverse_probability (int prob1)
605*38fd1498Szrj {
606*38fd1498Szrj check_probability (prob1);
607*38fd1498Szrj return REG_BR_PROB_BASE - prob1;
608*38fd1498Szrj }
609*38fd1498Szrj
610*38fd1498Szrj /* Return true if BB has at least one abnormal outgoing edge. */
611*38fd1498Szrj
612*38fd1498Szrj static inline bool
has_abnormal_or_eh_outgoing_edge_p(basic_block bb)613*38fd1498Szrj has_abnormal_or_eh_outgoing_edge_p (basic_block bb)
614*38fd1498Szrj {
615*38fd1498Szrj edge e;
616*38fd1498Szrj edge_iterator ei;
617*38fd1498Szrj
618*38fd1498Szrj FOR_EACH_EDGE (e, ei, bb->succs)
619*38fd1498Szrj if (e->flags & (EDGE_ABNORMAL | EDGE_EH))
620*38fd1498Szrj return true;
621*38fd1498Szrj
622*38fd1498Szrj return false;
623*38fd1498Szrj }
624*38fd1498Szrj
625*38fd1498Szrj /* Return true when one of the predecessor edges of BB is marked with
626*38fd1498Szrj EDGE_ABNORMAL_CALL or EDGE_EH. */
627*38fd1498Szrj
628*38fd1498Szrj static inline bool
has_abnormal_call_or_eh_pred_edge_p(basic_block bb)629*38fd1498Szrj has_abnormal_call_or_eh_pred_edge_p (basic_block bb)
630*38fd1498Szrj {
631*38fd1498Szrj edge e;
632*38fd1498Szrj edge_iterator ei;
633*38fd1498Szrj
634*38fd1498Szrj FOR_EACH_EDGE (e, ei, bb->preds)
635*38fd1498Szrj if (e->flags & (EDGE_ABNORMAL_CALL | EDGE_EH))
636*38fd1498Szrj return true;
637*38fd1498Szrj
638*38fd1498Szrj return false;
639*38fd1498Szrj }
640*38fd1498Szrj
641*38fd1498Szrj /* Return count of edge E. */
count()642*38fd1498Szrj inline profile_count edge_def::count () const
643*38fd1498Szrj {
644*38fd1498Szrj return src->count.apply_probability (probability);
645*38fd1498Szrj }
646*38fd1498Szrj
647*38fd1498Szrj #endif /* GCC_BASIC_BLOCK_H */
648