[Ada] Revert "Enforce matching of extra formals"
[official-gcc.git] / gcc / basic-block.h
blobc9d1fc91bbbc84c1e937567cdef802cc99ae8a05
1 /* Define control flow data structures for the CFG.
2 Copyright (C) 1987-2022 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #ifndef GCC_BASIC_BLOCK_H
21 #define GCC_BASIC_BLOCK_H
23 #include <profile-count.h>
25 /* Control flow edge information. */
26 class GTY((user)) edge_def {
27 public:
28 /* The two blocks at the ends of the edge. */
29 basic_block src;
30 basic_block dest;
32 /* Instructions queued on the edge. */
33 union edge_def_insns {
34 gimple_seq g;
35 rtx_insn *r;
36 } insns;
38 /* Auxiliary info specific to a pass. */
39 void *aux;
41 /* Location of any goto implicit in the edge. */
42 location_t goto_locus;
44 /* The index number corresponding to this edge in the edge vector
45 dest->preds. */
46 unsigned int dest_idx;
48 int flags; /* see cfg-flags.def */
49 profile_probability probability;
51 /* Return count of edge E. */
52 inline profile_count count () const;
55 /* Masks for edge.flags. */
56 #define DEF_EDGE_FLAG(NAME,IDX) EDGE_##NAME = 1 << IDX ,
57 enum cfg_edge_flags {
58 #include "cfg-flags.def"
59 LAST_CFG_EDGE_FLAG /* this is only used for EDGE_ALL_FLAGS */
61 #undef DEF_EDGE_FLAG
63 /* Bit mask for all edge flags. */
64 #define EDGE_ALL_FLAGS ((LAST_CFG_EDGE_FLAG - 1) * 2 - 1)
66 /* The following four flags all indicate something special about an edge.
67 Test the edge flags on EDGE_COMPLEX to detect all forms of "strange"
68 control flow transfers. */
69 #define EDGE_COMPLEX \
70 (EDGE_ABNORMAL | EDGE_ABNORMAL_CALL | EDGE_EH | EDGE_PRESERVE)
72 struct GTY(()) rtl_bb_info {
73 /* The first insn of the block is embedded into bb->il.x. */
74 /* The last insn of the block. */
75 rtx_insn *end_;
77 /* In CFGlayout mode points to insn notes/jumptables to be placed just before
78 and after the block. */
79 rtx_insn *header_;
80 rtx_insn *footer_;
83 struct GTY(()) gimple_bb_info {
84 /* Sequence of statements in this block. */
85 gimple_seq seq;
87 /* PHI nodes for this block. */
88 gimple_seq phi_nodes;
91 /* A basic block is a sequence of instructions with only one entry and
92 only one exit. If any one of the instructions are executed, they
93 will all be executed, and in sequence from first to last.
95 There may be COND_EXEC instructions in the basic block. The
96 COND_EXEC *instructions* will be executed -- but if the condition
97 is false the conditionally executed *expressions* will of course
98 not be executed. We don't consider the conditionally executed
99 expression (which might have side-effects) to be in a separate
100 basic block because the program counter will always be at the same
101 location after the COND_EXEC instruction, regardless of whether the
102 condition is true or not.
104 Basic blocks need not start with a label nor end with a jump insn.
105 For example, a previous basic block may just "conditionally fall"
106 into the succeeding basic block, and the last basic block need not
107 end with a jump insn. Block 0 is a descendant of the entry block.
109 A basic block beginning with two labels cannot have notes between
110 the labels.
112 Data for jump tables are stored in jump_insns that occur in no
113 basic block even though these insns can follow or precede insns in
114 basic blocks. */
116 /* Basic block information indexed by block number. */
117 struct GTY((chain_next ("%h.next_bb"), chain_prev ("%h.prev_bb"))) basic_block_def {
118 /* The edges into and out of the block. */
119 vec<edge, va_gc> *preds;
120 vec<edge, va_gc> *succs;
122 /* Auxiliary info specific to a pass. */
123 void *GTY ((skip (""))) aux;
125 /* Innermost loop containing the block. */
126 class loop *loop_father;
128 /* The dominance and postdominance information node. */
129 struct et_node * GTY ((skip (""))) dom[2];
131 /* Previous and next blocks in the chain. */
132 basic_block prev_bb;
133 basic_block next_bb;
135 union basic_block_il_dependent {
136 struct gimple_bb_info GTY ((tag ("0"))) gimple;
137 struct {
138 rtx_insn *head_;
139 struct rtl_bb_info * rtl;
140 } GTY ((tag ("1"))) x;
141 } GTY ((desc ("((%1.flags & BB_RTL) != 0)"))) il;
143 /* Various flags. See cfg-flags.def. */
144 int flags;
146 /* The index of this block. */
147 int index;
149 /* Expected number of executions: calculated in profile.cc. */
150 profile_count count;
152 /* The discriminator for this block. The discriminator distinguishes
153 among several basic blocks that share a common locus, allowing for
154 more accurate sample-based profiling. */
155 int discriminator;
158 /* This ensures that struct gimple_bb_info is smaller than
159 struct rtl_bb_info, so that inlining the former into basic_block_def
160 is the better choice. */
161 STATIC_ASSERT (sizeof (rtl_bb_info) >= sizeof (gimple_bb_info));
163 #define BB_FREQ_MAX 10000
165 /* Masks for basic_block.flags. */
166 #define DEF_BASIC_BLOCK_FLAG(NAME,IDX) BB_##NAME = 1 << IDX ,
167 enum cfg_bb_flags
169 #include "cfg-flags.def"
170 LAST_CFG_BB_FLAG /* this is only used for BB_ALL_FLAGS */
172 #undef DEF_BASIC_BLOCK_FLAG
174 /* Bit mask for all basic block flags. */
175 #define BB_ALL_FLAGS ((LAST_CFG_BB_FLAG - 1) * 2 - 1)
177 /* Bit mask for all basic block flags that must be preserved. These are
178 the bit masks that are *not* cleared by clear_bb_flags. */
179 #define BB_FLAGS_TO_PRESERVE \
180 (BB_DISABLE_SCHEDULE | BB_RTL | BB_NON_LOCAL_GOTO_TARGET \
181 | BB_HOT_PARTITION | BB_COLD_PARTITION)
183 /* Dummy bitmask for convenience in the hot/cold partitioning code. */
184 #define BB_UNPARTITIONED 0
186 /* Partitions, to be used when partitioning hot and cold basic blocks into
187 separate sections. */
188 #define BB_PARTITION(bb) ((bb)->flags & (BB_HOT_PARTITION|BB_COLD_PARTITION))
189 #define BB_SET_PARTITION(bb, part) do { \
190 basic_block bb_ = (bb); \
191 bb_->flags = ((bb_->flags & ~(BB_HOT_PARTITION|BB_COLD_PARTITION)) \
192 | (part)); \
193 } while (0)
195 #define BB_COPY_PARTITION(dstbb, srcbb) \
196 BB_SET_PARTITION (dstbb, BB_PARTITION (srcbb))
198 /* Defines for accessing the fields of the CFG structure for function FN. */
199 #define ENTRY_BLOCK_PTR_FOR_FN(FN) ((FN)->cfg->x_entry_block_ptr)
200 #define EXIT_BLOCK_PTR_FOR_FN(FN) ((FN)->cfg->x_exit_block_ptr)
201 #define basic_block_info_for_fn(FN) ((FN)->cfg->x_basic_block_info)
202 #define n_basic_blocks_for_fn(FN) ((FN)->cfg->x_n_basic_blocks)
203 #define n_edges_for_fn(FN) ((FN)->cfg->x_n_edges)
204 #define last_basic_block_for_fn(FN) ((FN)->cfg->x_last_basic_block)
205 #define label_to_block_map_for_fn(FN) ((FN)->cfg->x_label_to_block_map)
206 #define profile_status_for_fn(FN) ((FN)->cfg->x_profile_status)
208 #define BASIC_BLOCK_FOR_FN(FN,N) \
209 ((*basic_block_info_for_fn (FN))[(N)])
210 #define SET_BASIC_BLOCK_FOR_FN(FN,N,BB) \
211 ((*basic_block_info_for_fn (FN))[(N)] = (BB))
213 /* For iterating over basic blocks. */
214 #define FOR_BB_BETWEEN(BB, FROM, TO, DIR) \
215 for (BB = FROM; BB != TO; BB = BB->DIR)
217 #define FOR_EACH_BB_FN(BB, FN) \
218 FOR_BB_BETWEEN (BB, (FN)->cfg->x_entry_block_ptr->next_bb, (FN)->cfg->x_exit_block_ptr, next_bb)
220 #define FOR_EACH_BB_REVERSE_FN(BB, FN) \
221 FOR_BB_BETWEEN (BB, (FN)->cfg->x_exit_block_ptr->prev_bb, (FN)->cfg->x_entry_block_ptr, prev_bb)
223 /* For iterating over insns in basic block. */
224 #define FOR_BB_INSNS(BB, INSN) \
225 for ((INSN) = BB_HEAD (BB); \
226 (INSN) && (INSN) != NEXT_INSN (BB_END (BB)); \
227 (INSN) = NEXT_INSN (INSN))
229 /* For iterating over insns in basic block when we might remove the
230 current insn. */
231 #define FOR_BB_INSNS_SAFE(BB, INSN, CURR) \
232 for ((INSN) = BB_HEAD (BB), (CURR) = (INSN) ? NEXT_INSN ((INSN)): NULL; \
233 (INSN) && (INSN) != NEXT_INSN (BB_END (BB)); \
234 (INSN) = (CURR), (CURR) = (INSN) ? NEXT_INSN ((INSN)) : NULL)
236 #define FOR_BB_INSNS_REVERSE(BB, INSN) \
237 for ((INSN) = BB_END (BB); \
238 (INSN) && (INSN) != PREV_INSN (BB_HEAD (BB)); \
239 (INSN) = PREV_INSN (INSN))
241 #define FOR_BB_INSNS_REVERSE_SAFE(BB, INSN, CURR) \
242 for ((INSN) = BB_END (BB),(CURR) = (INSN) ? PREV_INSN ((INSN)) : NULL; \
243 (INSN) && (INSN) != PREV_INSN (BB_HEAD (BB)); \
244 (INSN) = (CURR), (CURR) = (INSN) ? PREV_INSN ((INSN)) : NULL)
246 /* Cycles through _all_ basic blocks, even the fake ones (entry and
247 exit block). */
249 #define FOR_ALL_BB_FN(BB, FN) \
250 for (BB = ENTRY_BLOCK_PTR_FOR_FN (FN); BB; BB = BB->next_bb)
253 /* Stuff for recording basic block info. */
255 /* For now, these will be functions (so that they can include checked casts
256 to rtx_insn. Once the underlying fields are converted from rtx
257 to rtx_insn, these can be converted back to macros. */
259 #define BB_HEAD(B) (B)->il.x.head_
260 #define BB_END(B) (B)->il.x.rtl->end_
261 #define BB_HEADER(B) (B)->il.x.rtl->header_
262 #define BB_FOOTER(B) (B)->il.x.rtl->footer_
264 /* Special block numbers [markers] for entry and exit.
265 Neither of them is supposed to hold actual statements. */
266 #define ENTRY_BLOCK (0)
267 #define EXIT_BLOCK (1)
269 /* The two blocks that are always in the cfg. */
270 #define NUM_FIXED_BLOCKS (2)
272 /* This is the value which indicates no edge is present. */
273 #define EDGE_INDEX_NO_EDGE -1
275 /* EDGE_INDEX returns an integer index for an edge, or EDGE_INDEX_NO_EDGE
276 if there is no edge between the 2 basic blocks. */
277 #define EDGE_INDEX(el, pred, succ) (find_edge_index ((el), (pred), (succ)))
279 /* INDEX_EDGE_PRED_BB and INDEX_EDGE_SUCC_BB return a pointer to the basic
280 block which is either the pred or succ end of the indexed edge. */
281 #define INDEX_EDGE_PRED_BB(el, index) ((el)->index_to_edge[(index)]->src)
282 #define INDEX_EDGE_SUCC_BB(el, index) ((el)->index_to_edge[(index)]->dest)
284 /* INDEX_EDGE returns a pointer to the edge. */
285 #define INDEX_EDGE(el, index) ((el)->index_to_edge[(index)])
287 /* Number of edges in the compressed edge list. */
288 #define NUM_EDGES(el) ((el)->num_edges)
290 /* BB is assumed to contain conditional jump. Return the fallthru edge. */
291 #define FALLTHRU_EDGE(bb) (EDGE_SUCC ((bb), 0)->flags & EDGE_FALLTHRU \
292 ? EDGE_SUCC ((bb), 0) : EDGE_SUCC ((bb), 1))
294 /* BB is assumed to contain conditional jump. Return the branch edge. */
295 #define BRANCH_EDGE(bb) (EDGE_SUCC ((bb), 0)->flags & EDGE_FALLTHRU \
296 ? EDGE_SUCC ((bb), 1) : EDGE_SUCC ((bb), 0))
298 /* Return expected execution frequency of the edge E. */
299 #define EDGE_FREQUENCY(e) e->count ().to_frequency (cfun)
301 /* Compute a scale factor (or probability) suitable for scaling of
302 gcov_type values via apply_probability() and apply_scale(). */
303 #define GCOV_COMPUTE_SCALE(num,den) \
304 ((den) ? RDIV ((num) * REG_BR_PROB_BASE, (den)) : REG_BR_PROB_BASE)
306 /* Return nonzero if edge is critical. */
307 #define EDGE_CRITICAL_P(e) (EDGE_COUNT ((e)->src->succs) >= 2 \
308 && EDGE_COUNT ((e)->dest->preds) >= 2)
310 #define EDGE_COUNT(ev) vec_safe_length (ev)
311 #define EDGE_I(ev,i) (*ev)[(i)]
312 #define EDGE_PRED(bb,i) (*(bb)->preds)[(i)]
313 #define EDGE_SUCC(bb,i) (*(bb)->succs)[(i)]
315 /* Returns true if BB has precisely one successor. */
317 static inline bool
318 single_succ_p (const_basic_block bb)
320 return EDGE_COUNT (bb->succs) == 1;
323 /* Returns true if BB has precisely one predecessor. */
325 static inline bool
326 single_pred_p (const_basic_block bb)
328 return EDGE_COUNT (bb->preds) == 1;
331 /* Returns the single successor edge of basic block BB. Aborts if
332 BB does not have exactly one successor. */
334 static inline edge
335 single_succ_edge (const_basic_block bb)
337 gcc_checking_assert (single_succ_p (bb));
338 return EDGE_SUCC (bb, 0);
341 /* Returns the single predecessor edge of basic block BB. Aborts
342 if BB does not have exactly one predecessor. */
344 static inline edge
345 single_pred_edge (const_basic_block bb)
347 gcc_checking_assert (single_pred_p (bb));
348 return EDGE_PRED (bb, 0);
351 /* Returns the single successor block of basic block BB. Aborts
352 if BB does not have exactly one successor. */
354 static inline basic_block
355 single_succ (const_basic_block bb)
357 return single_succ_edge (bb)->dest;
360 /* Returns the single predecessor block of basic block BB. Aborts
361 if BB does not have exactly one predecessor.*/
363 static inline basic_block
364 single_pred (const_basic_block bb)
366 return single_pred_edge (bb)->src;
369 /* Iterator object for edges. */
371 struct edge_iterator {
372 unsigned index;
373 vec<edge, va_gc> **container;
376 static inline vec<edge, va_gc> *
377 ei_container (edge_iterator i)
379 gcc_checking_assert (i.container);
380 return *i.container;
383 #define ei_start(iter) ei_start_1 (&(iter))
384 #define ei_last(iter) ei_last_1 (&(iter))
386 /* Return an iterator pointing to the start of an edge vector. */
387 static inline edge_iterator
388 ei_start_1 (vec<edge, va_gc> **ev)
390 edge_iterator i;
392 i.index = 0;
393 i.container = ev;
395 return i;
398 /* Return an iterator pointing to the last element of an edge
399 vector. */
400 static inline edge_iterator
401 ei_last_1 (vec<edge, va_gc> **ev)
403 edge_iterator i;
405 i.index = EDGE_COUNT (*ev) - 1;
406 i.container = ev;
408 return i;
411 /* Is the iterator `i' at the end of the sequence? */
412 static inline bool
413 ei_end_p (edge_iterator i)
415 return (i.index == EDGE_COUNT (ei_container (i)));
418 /* Is the iterator `i' at one position before the end of the
419 sequence? */
420 static inline bool
421 ei_one_before_end_p (edge_iterator i)
423 return (i.index + 1 == EDGE_COUNT (ei_container (i)));
426 /* Advance the iterator to the next element. */
427 static inline void
428 ei_next (edge_iterator *i)
430 gcc_checking_assert (i->index < EDGE_COUNT (ei_container (*i)));
431 i->index++;
434 /* Move the iterator to the previous element. */
435 static inline void
436 ei_prev (edge_iterator *i)
438 gcc_checking_assert (i->index > 0);
439 i->index--;
442 /* Return the edge pointed to by the iterator `i'. */
443 static inline edge
444 ei_edge (edge_iterator i)
446 return EDGE_I (ei_container (i), i.index);
449 /* Return an edge pointed to by the iterator. Do it safely so that
450 NULL is returned when the iterator is pointing at the end of the
451 sequence. */
452 static inline edge
453 ei_safe_edge (edge_iterator i)
455 return !ei_end_p (i) ? ei_edge (i) : NULL;
458 /* Return 1 if we should continue to iterate. Return 0 otherwise.
459 *Edge P is set to the next edge if we are to continue to iterate
460 and NULL otherwise. */
462 static inline bool
463 ei_cond (edge_iterator ei, edge *p)
465 if (!ei_end_p (ei))
467 *p = ei_edge (ei);
468 return 1;
470 else
472 *p = NULL;
473 return 0;
477 /* This macro serves as a convenient way to iterate each edge in a
478 vector of predecessor or successor edges. It must not be used when
479 an element might be removed during the traversal, otherwise
480 elements will be missed. Instead, use a for-loop like that shown
481 in the following pseudo-code:
483 FOR (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
485 IF (e != taken_edge)
486 remove_edge (e);
487 ELSE
488 ei_next (&ei);
492 #define FOR_EACH_EDGE(EDGE,ITER,EDGE_VEC) \
493 for ((ITER) = ei_start ((EDGE_VEC)); \
494 ei_cond ((ITER), &(EDGE)); \
495 ei_next (&(ITER)))
497 #define CLEANUP_EXPENSIVE 1 /* Do relatively expensive optimizations
498 except for edge forwarding */
499 #define CLEANUP_CROSSJUMP 2 /* Do crossjumping. */
500 #define CLEANUP_POST_REGSTACK 4 /* We run after reg-stack and need
501 to care REG_DEAD notes. */
502 #define CLEANUP_THREADING 8 /* Do jump threading. */
503 #define CLEANUP_NO_INSN_DEL 16 /* Do not try to delete trivially dead
504 insns. */
505 #define CLEANUP_CFGLAYOUT 32 /* Do cleanup in cfglayout mode. */
506 #define CLEANUP_CFG_CHANGED 64 /* The caller changed the CFG. */
507 #define CLEANUP_NO_PARTITIONING 128 /* Do not try to fix partitions. */
508 #define CLEANUP_FORCE_FAST_DCE 0x100 /* Force run_fast_dce to be called
509 at least once. */
511 /* Return true if BB is in a transaction. */
513 static inline bool
514 bb_in_transaction (basic_block bb)
516 return bb->flags & BB_IN_TRANSACTION;
519 /* Return true when one of the predecessor edges of BB is marked with EDGE_EH. */
520 static inline bool
521 bb_has_eh_pred (basic_block bb)
523 edge e;
524 edge_iterator ei;
526 FOR_EACH_EDGE (e, ei, bb->preds)
528 if (e->flags & EDGE_EH)
529 return true;
531 return false;
534 /* Return true when one of the predecessor edges of BB is marked with EDGE_ABNORMAL. */
535 static inline bool
536 bb_has_abnormal_pred (basic_block bb)
538 edge e;
539 edge_iterator ei;
541 FOR_EACH_EDGE (e, ei, bb->preds)
543 if (e->flags & EDGE_ABNORMAL)
544 return true;
546 return false;
549 /* Return the fallthru edge in EDGES if it exists, NULL otherwise. */
550 static inline edge
551 find_fallthru_edge (vec<edge, va_gc> *edges)
553 edge e;
554 edge_iterator ei;
556 FOR_EACH_EDGE (e, ei, edges)
557 if (e->flags & EDGE_FALLTHRU)
558 break;
560 return e;
563 /* Check tha probability is sane. */
565 static inline void
566 check_probability (int prob)
568 gcc_checking_assert (prob >= 0 && prob <= REG_BR_PROB_BASE);
571 /* Given PROB1 and PROB2, return PROB1*PROB2/REG_BR_PROB_BASE.
572 Used to combine BB probabilities. */
574 static inline int
575 combine_probabilities (int prob1, int prob2)
577 check_probability (prob1);
578 check_probability (prob2);
579 return RDIV (prob1 * prob2, REG_BR_PROB_BASE);
582 /* Apply scale factor SCALE on frequency or count FREQ. Use this
583 interface when potentially scaling up, so that SCALE is not
584 constrained to be < REG_BR_PROB_BASE. */
586 static inline gcov_type
587 apply_scale (gcov_type freq, gcov_type scale)
589 return RDIV (freq * scale, REG_BR_PROB_BASE);
592 /* Apply probability PROB on frequency or count FREQ. */
594 static inline gcov_type
595 apply_probability (gcov_type freq, int prob)
597 check_probability (prob);
598 return apply_scale (freq, prob);
601 /* Return inverse probability for PROB. */
603 static inline int
604 inverse_probability (int prob1)
606 check_probability (prob1);
607 return REG_BR_PROB_BASE - prob1;
610 /* Return true if BB has at least one abnormal outgoing edge. */
612 static inline bool
613 has_abnormal_or_eh_outgoing_edge_p (basic_block bb)
615 edge e;
616 edge_iterator ei;
618 FOR_EACH_EDGE (e, ei, bb->succs)
619 if (e->flags & (EDGE_ABNORMAL | EDGE_EH))
620 return true;
622 return false;
625 /* Return true when one of the predecessor edges of BB is marked with
626 EDGE_ABNORMAL_CALL or EDGE_EH. */
628 static inline bool
629 has_abnormal_call_or_eh_pred_edge_p (basic_block bb)
631 edge e;
632 edge_iterator ei;
634 FOR_EACH_EDGE (e, ei, bb->preds)
635 if (e->flags & (EDGE_ABNORMAL_CALL | EDGE_EH))
636 return true;
638 return false;
641 /* Return count of edge E. */
642 inline profile_count edge_def::count () const
644 return src->count.apply_probability (probability);
647 #endif /* GCC_BASIC_BLOCK_H */