OSDN Git Service

2000-10-17 Benjamin Kosnik <bkoz@purist.soma.redhat.com>
[pf3gnuchains/gcc-fork.git] / gcc / basic-block.h
1 /* Define control and data flow tables, and regsets.
2    Copyright (C) 1987, 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
3
4 This file is part of GNU CC.
5
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING.  If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.  */
20
21 #ifndef _BASIC_BLOCK_H
22 #define _BASIC_BLOCK_H 1
23
24 #include "bitmap.h"
25 #include "sbitmap.h"
26 #include "varray.h"
27 #include "partition.h"
28
29 /* Head of register set linked list.  */
30 typedef bitmap_head regset_head;
31 /* A pointer to a regset_head.  */
32 typedef bitmap regset;
33
34 /* Initialize a new regset.  */
35 #define INIT_REG_SET(HEAD) bitmap_initialize (HEAD)
36
37 /* Clear a register set by freeing up the linked list.  */
38 #define CLEAR_REG_SET(HEAD) bitmap_clear (HEAD)
39
40 /* Copy a register set to another register set.  */
41 #define COPY_REG_SET(TO, FROM) bitmap_copy (TO, FROM)
42
43 /* Compare two register sets.  */
44 #define REG_SET_EQUAL_P(A, B) bitmap_equal_p (A, B)
45
46 /* `and' a register set with a second register set.  */
47 #define AND_REG_SET(TO, FROM) bitmap_operation (TO, TO, FROM, BITMAP_AND)
48
49 /* `and' the complement of a register set with a register set.  */
50 #define AND_COMPL_REG_SET(TO, FROM) \
51   bitmap_operation (TO, TO, FROM, BITMAP_AND_COMPL)
52
53 /* Inclusive or a register set with a second register set.  */
54 #define IOR_REG_SET(TO, FROM) bitmap_operation (TO, TO, FROM, BITMAP_IOR)
55
56 /* Exclusive or a register set with a second register set.  */
57 #define XOR_REG_SET(TO, FROM) bitmap_operation (TO, TO, FROM, BITMAP_XOR)
58
59 /* Or into TO the register set FROM1 `and'ed with the complement of FROM2.  */
60 #define IOR_AND_COMPL_REG_SET(TO, FROM1, FROM2) \
61   bitmap_ior_and_compl (TO, FROM1, FROM2)
62
63 /* Clear a single register in a register set.  */
64 #define CLEAR_REGNO_REG_SET(HEAD, REG) bitmap_clear_bit (HEAD, REG)
65
66 /* Set a single register in a register set.  */
67 #define SET_REGNO_REG_SET(HEAD, REG) bitmap_set_bit (HEAD, REG)
68
69 /* Return true if a register is set in a register set.  */
70 #define REGNO_REG_SET_P(TO, REG) bitmap_bit_p (TO, REG)
71
72 /* Copy the hard registers in a register set to the hard register set.  */
73 extern void reg_set_to_hard_reg_set PARAMS ((HARD_REG_SET *, bitmap));
74 #define REG_SET_TO_HARD_REG_SET(TO, FROM)                               \
75 do {                                                                    \
76   CLEAR_HARD_REG_SET (TO);                                              \
77   reg_set_to_hard_reg_set (&TO, FROM);                                  \
78 } while (0)
79
80 /* Loop over all registers in REGSET, starting with MIN, setting REGNUM to the
81    register number and executing CODE for all registers that are set. */
82 #define EXECUTE_IF_SET_IN_REG_SET(REGSET, MIN, REGNUM, CODE)            \
83   EXECUTE_IF_SET_IN_BITMAP (REGSET, MIN, REGNUM, CODE)
84
85 /* Loop over all registers in REGSET1 and REGSET2, starting with MIN, setting
86    REGNUM to the register number and executing CODE for all registers that are
87    set in the first regset and not set in the second. */
88 #define EXECUTE_IF_AND_COMPL_IN_REG_SET(REGSET1, REGSET2, MIN, REGNUM, CODE) \
89   EXECUTE_IF_AND_COMPL_IN_BITMAP (REGSET1, REGSET2, MIN, REGNUM, CODE)
90
91 /* Loop over all registers in REGSET1 and REGSET2, starting with MIN, setting
92    REGNUM to the register number and executing CODE for all registers that are
93    set in both regsets. */
94 #define EXECUTE_IF_AND_IN_REG_SET(REGSET1, REGSET2, MIN, REGNUM, CODE) \
95   EXECUTE_IF_AND_IN_BITMAP (REGSET1, REGSET2, MIN, REGNUM, CODE)
96
97 /* Allocate a register set with oballoc.  */
98 #define OBSTACK_ALLOC_REG_SET(OBSTACK) BITMAP_OBSTACK_ALLOC (OBSTACK)
99
100 /* Initialize a register set.  Returns the new register set.  */
101 #define INITIALIZE_REG_SET(HEAD) bitmap_initialize (&HEAD)
102
103 /* Do any cleanup needed on a regset when it is no longer used.  */
104 #define FREE_REG_SET(REGSET) BITMAP_FREE(REGSET)
105
106 /* Do any one-time initializations needed for regsets.  */
107 #define INIT_ONCE_REG_SET() BITMAP_INIT_ONCE ()
108
109 /* Grow any tables needed when the number of registers is calculated
110    or extended.  For the linked list allocation, nothing needs to
111    be done, other than zero the statistics on the first allocation.  */
112 #define MAX_REGNO_REG_SET(NUM_REGS, NEW_P, RENUMBER_P) 
113
114 /* Control flow edge information.  */
115 typedef struct edge_def {
116   /* Links through the predecessor and successor lists.  */
117   struct edge_def *pred_next, *succ_next;
118
119   /* The two blocks at the ends of the edge.  */
120   struct basic_block_def *src, *dest;
121
122   /* Instructions queued on the edge.  */
123   rtx insns;
124
125   /* Auxiliary info specific to a pass.  */
126   void *aux;
127
128   int flags;                    /* see EDGE_* below  */
129   int probability;              /* biased by REG_BR_PROB_BASE */
130   int count;                    /* Expected number of executions calculated
131                                    in profile.c  */
132 } *edge;
133
134 #define EDGE_FALLTHRU           1
135 #define EDGE_CRITICAL           2
136 #define EDGE_ABNORMAL           4
137 #define EDGE_ABNORMAL_CALL      8
138 #define EDGE_EH                 16
139 #define EDGE_FAKE               32
140
141 #define EDGE_COMPLEX    (EDGE_ABNORMAL | EDGE_ABNORMAL_CALL | EDGE_EH)
142
143
144 /* Basic blocks need not start with a label nor end with a jump insn.
145    For example, a previous basic block may just "conditionally fall"
146    into the succeeding basic block, and the last basic block need not
147    end with a jump insn.  Block 0 is a descendant of the entry block.
148
149    A basic block beginning with two labels cannot have notes between
150    the labels.
151
152    Data for jump tables are stored in jump_insns that occur in no
153    basic block even though these insns can follow or precede insns in
154    basic blocks.  */
155
156 /* Basic block information indexed by block number.  */
157 typedef struct basic_block_def {
158   /* The first and last insns of the block.  */
159   rtx head, end;
160
161   /* The edges into and out of the block.  */
162   edge pred, succ;
163
164   /* Liveness info.  Note that in SSA form, global_live_at_start does
165      not reflect the use of regs in phi functions, since the liveness
166      of these regs may depend on which edge was taken into the block.  */
167   regset local_set;
168   regset global_live_at_start;
169   regset global_live_at_end;
170
171   /* Auxiliary info specific to a pass.  */
172   void *aux;
173
174   /* The index of this block.  */
175   int index;
176   /* The loop depth of this block plus one.  */
177   int loop_depth;
178
179   /* The active eh region before head and after end.  */
180   int eh_beg, eh_end;
181
182   int count;            /* Expected number of executions: calculated in
183                            profile.c */
184 } *basic_block;
185
186 /* Number of basic blocks in the current function.  */
187
188 extern int n_basic_blocks;
189
190 /* Number of edges in the current function.  */
191
192 extern int n_edges;
193
194 /* Index by basic block number, get basic block struct info.  */
195
196 extern varray_type basic_block_info;
197
198 #define BASIC_BLOCK(N)  (VARRAY_BB (basic_block_info, (N)))
199
200 /* What registers are live at the setjmp call.  */
201
202 extern regset regs_live_at_setjmp;
203
204 /* Indexed by n, gives number of basic block that  (REG n) is used in.
205    If the value is REG_BLOCK_GLOBAL (-2),
206    it means (REG n) is used in more than one basic block.
207    REG_BLOCK_UNKNOWN (-1) means it hasn't been seen yet so we don't know.
208    This information remains valid for the rest of the compilation
209    of the current function; it is used to control register allocation.  */
210
211 #define REG_BLOCK_UNKNOWN -1
212 #define REG_BLOCK_GLOBAL -2
213
214 #define REG_BASIC_BLOCK(N) (VARRAY_REG (reg_n_info, N)->basic_block)
215 \f
216 /* Stuff for recording basic block info.  */
217
218 #define BLOCK_HEAD(B)      (BASIC_BLOCK (B)->head)
219 #define BLOCK_END(B)       (BASIC_BLOCK (B)->end)
220
221 /* Special block numbers [markers] for entry and exit.  */
222 #define ENTRY_BLOCK (-1)
223 #define EXIT_BLOCK (-2)
224
225 /* Special block number not valid for any block. */
226 #define INVALID_BLOCK (-3)
227
228 /* Similarly, block pointers for the edge list.  */
229 extern struct basic_block_def entry_exit_blocks[2];
230 #define ENTRY_BLOCK_PTR (&entry_exit_blocks[0])
231 #define EXIT_BLOCK_PTR  (&entry_exit_blocks[1])
232
233 extern varray_type basic_block_for_insn;
234 #define BLOCK_FOR_INSN(INSN)  VARRAY_BB (basic_block_for_insn, INSN_UID (INSN))
235 #define BLOCK_NUM(INSN)       (BLOCK_FOR_INSN (INSN)->index + 0)
236
237 extern void compute_bb_for_insn         PARAMS ((int));
238 extern void update_bb_for_insn          PARAMS ((basic_block));
239 extern void set_block_for_insn          PARAMS ((rtx, basic_block));
240 extern void set_block_num               PARAMS ((rtx, int));
241
242 extern void free_basic_block_vars       PARAMS ((int));
243
244 extern edge split_block                 PARAMS ((basic_block, rtx));
245 extern basic_block split_edge           PARAMS ((edge));
246 extern void insert_insn_on_edge         PARAMS ((rtx, edge));
247 extern void commit_edge_insertions      PARAMS ((void));
248 extern void remove_fake_edges           PARAMS ((void));
249 extern void add_noreturn_fake_exit_edges        PARAMS ((void));
250 extern void connect_infinite_loops_to_exit      PARAMS ((void));
251 extern rtx flow_delete_insn             PARAMS ((rtx));
252 extern void flow_delete_insn_chain      PARAMS ((rtx, rtx));
253 extern void make_edge                   PARAMS ((sbitmap *, basic_block,
254                                                  basic_block, int));
255 extern void remove_edge                 PARAMS ((edge));
256 extern void redirect_edge_succ          PARAMS ((edge, basic_block));
257 extern void redirect_edge_pred          PARAMS ((edge, basic_block));
258 extern void create_basic_block          PARAMS ((int, rtx, rtx, rtx));
259 extern int flow_delete_block            PARAMS ((basic_block));
260 extern void merge_blocks_nomove         PARAMS ((basic_block, basic_block));
261 extern void tidy_fallthru_edge          PARAMS ((edge, basic_block,
262                                                  basic_block));
263
264 /* Structure to hold information for each natural loop.  */
265 struct loop
266 {
267   /* Index into loops array.  */
268   int num;
269
270   /* Basic block of loop header.  */
271   basic_block header;
272
273   /* Basic block of loop latch.  */
274   basic_block latch;
275
276   /* Basic block of loop pre-header or NULL if it does not exist.  */
277   basic_block pre_header;
278
279   /* Root node of pre_header extended basic block.  */
280   basic_block pre_header_root;
281
282   /* Bitmap of blocks of trace from pre_header root to pre_header.  */
283   sbitmap pre_header_trace;
284
285   /* The first block in the loop.  This is not necessarily the same as
286      the loop header.  */
287   basic_block first;
288
289   /* The last block in the loop.  This is not necessarily the same as
290      the loop latch.  */
291   basic_block last;
292
293   /* Bitmap of blocks contained within the loop.  */
294   sbitmap nodes;
295
296   /* Number of blocks contained within the loop.  */
297   int num_nodes;
298
299   /* Array of edges that enter the loop.  */
300   edge *entry_edges;
301
302   /* Number of edges that enter the loop.  */
303   int num_entries;
304
305   /* Array of edges that exit the loop.  */
306   edge *exit_edges;
307
308   /* Number of edges that exit the loop.  */
309   int num_exits;
310
311   /* Bitmap of blocks that dominate all exits of the loop.  */
312   sbitmap exits_doms;
313
314   /* The loop nesting depth.  */
315   int depth;
316
317   /* The height of the loop (enclosed loop levels) within the loop
318      hierarchy tree.  */
319   int level;
320
321   /* The outer (parent) loop or NULL if outermost loop.  */
322   struct loop *outer;
323
324   /* The first inner (child) loop or NULL if innermost loop.  */
325   struct loop *inner;
326
327   /* Link to the next (sibling) loop.  */
328   struct loop *next;
329
330   /* Non-zero if the loop shares a header with another loop.  */
331   int shared;
332
333   /* Non-zero if the loop is invalid (e.g., contains setjmp.).  */
334   int invalid;
335
336   /* Auxiliary info specific to a pass.  */
337   void *aux;
338
339   /* The following are currently used by loop.c but they are likely to
340      disappear as loop.c is converted to use the CFG.  */
341
342   /* Non-zero if the loop has a NOTE_INSN_LOOP_VTOP.  */
343   rtx vtop;
344
345   /* Non-zero if the loop has a NOTE_INSN_LOOP_CONT.
346      A continue statement will generate a branch to NEXT_INSN (cont).  */
347   rtx cont;
348
349   /* The dominator of cont.  */
350   rtx cont_dominator;
351
352   /* The NOTE_INSN_LOOP_BEG.  */
353   rtx start;
354
355   /* The NOTE_INSN_LOOP_END.  */
356   rtx end;
357
358   /* For a rotated loop that is entered near the bottom,
359      this is the label at the top.  Otherwise it is zero.  */
360   rtx top;
361
362   /* Place in the loop where control enters.  */
363   rtx scan_start;
364
365   /* List of all LABEL_REFs which refer to code labels outside the
366      loop.  Used by routines that need to know all loop exits, such as
367      final_biv_value and final_giv_value.
368      
369      This does not include loop exits due to return instructions.
370      This is because all bivs and givs are pseudos, and hence must be
371      dead after a return, so the presense of a return does not affect
372      any of the optimizations that use this info.  It is simpler to
373      just not include return instructions on this list.  */
374   rtx exit_labels;
375
376   /* The number of LABEL_REFs on exit_labels for this loop and all
377      loops nested inside it.  */
378   int exit_count;
379 };
380
381
382 /* Structure to hold CFG information about natural loops within a function.  */
383 struct loops
384 {
385   /* Number of natural loops in the function.  */
386   int num;
387
388   /* Maxium nested loop level in the function.  */
389   int levels;
390
391   /* Array of natural loop descriptors (scanning this array in reverse order
392      will find the inner loops before their enclosing outer loops).  */
393   struct loop *array;
394
395   /* Pointer to root of loop heirachy tree.  */
396   struct loop *tree;
397
398   /* Information derived from the CFG.  */
399   struct cfg
400   {
401     /* The bitmap vector of dominators or NULL if not computed.  */
402     sbitmap *dom;
403
404     /* The ordering of the basic blocks in a depth first search.  */
405     int *dfs_order;
406
407     /* The reverse completion ordering of the basic blocks found in a
408        depth first search.  */
409     int *rc_order;
410   } cfg;
411
412   /* Headers shared by multiple loops that should be merged.  */
413   sbitmap shared_headers;
414 };
415
416 extern int flow_loops_find PARAMS ((struct loops *, int flags));
417 extern int flow_loops_update PARAMS ((struct loops *, int flags));
418 extern void flow_loops_free PARAMS ((struct loops *));
419 extern void flow_loops_dump PARAMS ((const struct loops *, FILE *,
420                                      void (*)(const struct loop *,
421                                               FILE *, int), int));
422 extern void flow_loop_dump PARAMS ((const struct loop *, FILE *,
423                                     void (*)(const struct loop *,
424                                              FILE *, int), int));
425
426 /* This structure maintains an edge list vector.  */
427 struct edge_list 
428 {
429   int num_blocks;
430   int num_edges;
431   edge *index_to_edge;
432 };
433
434 /* This is the value which indicates no edge is present.  */
435 #define EDGE_INDEX_NO_EDGE      -1
436
437 /* EDGE_INDEX returns an integer index for an edge, or EDGE_INDEX_NO_EDGE
438    if there is no edge between the 2 basic blocks.  */
439 #define EDGE_INDEX(el, pred, succ) (find_edge_index ((el), (pred), (succ)))
440
441 /* INDEX_EDGE_PRED_BB and INDEX_EDGE_SUCC_BB return a pointer to the basic
442    block which is either the pred or succ end of the indexed edge.  */
443 #define INDEX_EDGE_PRED_BB(el, index)   ((el)->index_to_edge[(index)]->src)
444 #define INDEX_EDGE_SUCC_BB(el, index)   ((el)->index_to_edge[(index)]->dest)
445
446 /* INDEX_EDGE returns a pointer to the edge.  */
447 #define INDEX_EDGE(el, index)           ((el)->index_to_edge[(index)])
448
449 /* Number of edges in the compressed edge list.  */
450 #define NUM_EDGES(el)                   ((el)->num_edges)
451
452 struct edge_list * create_edge_list     PARAMS ((void));
453 void free_edge_list                     PARAMS ((struct edge_list *));
454 void print_edge_list                    PARAMS ((FILE *, struct edge_list *));
455 void verify_edge_list                   PARAMS ((FILE *, struct edge_list *));
456 int find_edge_index                     PARAMS ((struct edge_list *, 
457                                                  basic_block, basic_block));
458
459 extern void compute_flow_dominators     PARAMS ((sbitmap *, sbitmap *));
460 extern void compute_immediate_dominators        PARAMS ((int *, sbitmap *));
461 extern void compute_immediate_postdominators    PARAMS ((int *, sbitmap *));
462
463
464 enum update_life_extent
465 {
466   UPDATE_LIFE_LOCAL = 0,
467   UPDATE_LIFE_GLOBAL = 1,
468   UPDATE_LIFE_GLOBAL_RM_NOTES = 2
469 };
470
471 /* Flags for life_analysis and update_life_info.  */
472
473 #define PROP_DEATH_NOTES        1       /* Create DEAD and UNUSED notes.  */
474 #define PROP_LOG_LINKS          2       /* Create LOG_LINKS.  */
475 #define PROP_REG_INFO           4       /* Update regs_ever_live et al.  */
476 #define PROP_KILL_DEAD_CODE     8       /* Remove dead code.  */
477 #define PROP_SCAN_DEAD_CODE     16      /* Scan for dead code.  */
478 #define PROP_AUTOINC            32      /* Create autoinc mem references.  */
479 #define PROP_FINAL              63      /* All of the above.  */
480
481
482 /* Flags for loop discovery.  */
483
484 #define LOOP_TREE               1       /* Build loop hierarchy tree.  */
485 #define LOOP_PRE_HEADER         2       /* Analyse loop pre-header.  */
486 #define LOOP_EDGES              4       /* Find entry and exit edges.  */
487 #define LOOP_EXITS_DOMS         8       /* Find nodes that dom. all exits.  */
488 #define LOOP_ALL               15       /* All of the above  */
489
490 extern void life_analysis       PARAMS ((rtx, FILE *, int));
491 extern void update_life_info    PARAMS ((sbitmap, enum update_life_extent,
492                                          int));
493 extern int count_or_remove_death_notes  PARAMS ((sbitmap, int));
494 extern void propagate_block     PARAMS ((basic_block, regset, regset, int));
495
496 struct propagate_block_info;
497 extern rtx propagate_one_insn   PARAMS ((struct propagate_block_info *, rtx));
498 extern struct propagate_block_info *init_propagate_block_info
499   PARAMS ((basic_block, regset, regset, int));
500 extern void free_propagate_block_info PARAMS ((struct propagate_block_info *));
501
502 /* In lcm.c */
503 extern struct edge_list *pre_edge_lcm   PARAMS ((FILE *, int, sbitmap *,
504                                                  sbitmap *, sbitmap *, 
505                                                  sbitmap *, sbitmap **,
506                                                  sbitmap **));
507 extern struct edge_list *pre_edge_rev_lcm PARAMS ((FILE *, int, sbitmap *,
508                                                    sbitmap *, sbitmap *, 
509                                                    sbitmap *, sbitmap **, 
510                                                    sbitmap **));
511 extern void compute_available           PARAMS ((sbitmap *, sbitmap *,
512                                                  sbitmap *, sbitmap *));
513 extern int optimize_mode_switching      PARAMS ((FILE *));
514
515 /* In emit-rtl.c.  */
516 extern rtx emit_block_insn_after        PARAMS ((rtx, rtx, basic_block));
517 extern rtx emit_block_insn_before       PARAMS ((rtx, rtx, basic_block));
518
519 /* In predict.c */
520 extern void estimate_probability        PARAMS ((struct loops *));
521 extern void expected_value_to_br_prob   PARAMS ((void));
522
523 /* In flow.c */
524 extern void init_flow                   PARAMS ((void));
525 extern void reorder_basic_blocks        PARAMS ((void));
526 extern void dump_bb                     PARAMS ((basic_block, FILE *));
527 extern void debug_bb                    PARAMS ((basic_block));
528 extern void debug_bb_n                  PARAMS ((int));
529 extern void dump_regset                 PARAMS ((regset, FILE *));
530 extern void debug_regset                PARAMS ((regset));
531 extern void allocate_reg_life_data      PARAMS ((void));
532
533 /* This function is always defined so it can be called from the
534    debugger, and it is declared extern so we don't get warnings about
535    it being unused. */
536 extern void verify_flow_info            PARAMS ((void));
537 extern int flow_loop_outside_edge_p     PARAMS ((const struct loop *, edge));
538
539 typedef struct conflict_graph_def *conflict_graph;
540
541 /* Callback function when enumerating conflicts.  The arguments are
542    the smaller and larger regno in the conflict.  Returns zero if
543    enumeration is to continue, non-zero to halt enumeration.  */
544 typedef int (*conflict_graph_enum_fn) PARAMS ((int, int, void *));
545
546
547 /* Prototypes of operations on conflict graphs.  */
548
549 extern conflict_graph conflict_graph_new 
550                                         PARAMS ((int));
551 extern void conflict_graph_delete       PARAMS ((conflict_graph));
552 extern int conflict_graph_add           PARAMS ((conflict_graph, 
553                                                  int, int));
554 extern int conflict_graph_conflict_p    PARAMS ((conflict_graph, 
555                                                  int, int));
556 extern void conflict_graph_enum         PARAMS ((conflict_graph, int, 
557                                                  conflict_graph_enum_fn, 
558                                                  void *));
559 extern void conflict_graph_merge_regs   PARAMS ((conflict_graph, int,
560                                                  int));
561 extern void conflict_graph_print        PARAMS ((conflict_graph, FILE*));
562 extern conflict_graph conflict_graph_compute 
563                                         PARAMS ((regset,
564                                                  partition));
565
566 #endif /* _BASIC_BLOCK_H */