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