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