2004-02-24 Aldy Hernandez <aldyh@redhat.com>
[official-gcc.git] / gcc / cfgloop.h
blob4226592a750fed6e03b07c2dabd670f4072f5f73
1 /* Natural loop functions
2 Copyright (C) 1987, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
22 /* Structure to hold decision about unrolling/peeling. */
23 enum lpt_dec
25 LPT_NONE,
26 LPT_PEEL_COMPLETELY,
27 LPT_PEEL_SIMPLE,
28 LPT_UNROLL_CONSTANT,
29 LPT_UNROLL_RUNTIME,
30 LPT_UNROLL_STUPID
33 struct lpt_decision
35 enum lpt_dec decision;
36 unsigned times;
39 /* Description of loop for simple loop unrolling. */
40 struct loop_desc
42 int postincr; /* 1 if increment/decrement is done after loop exit condition. */
43 rtx stride; /* Value added to VAR in each iteration. */
44 rtx var; /* Loop control variable. */
45 enum machine_mode inner_mode;
46 /* The mode from that it is extended. */
47 enum rtx_code extend; /* With this extend. */
48 rtx var_alts; /* List of definitions of its initial value. */
49 rtx lim; /* Expression var is compared with. */
50 rtx lim_alts; /* List of definitions of its initial value. */
51 bool const_iter; /* True if it iterates constant number of times. */
52 unsigned HOST_WIDE_INT niter;
53 /* Number of iterations if it is constant. */
54 bool may_be_zero; /* If we cannot determine that the first iteration will pass. */
55 enum rtx_code cond; /* Exit condition. */
56 int neg; /* Set to 1 if loop ends when condition is satisfied. */
57 edge out_edge; /* The exit edge. */
58 edge in_edge; /* And the other one. */
59 int n_branches; /* Number of branches inside the loop. */
62 /* Structure to hold information for each natural loop. */
63 struct loop
65 /* Index into loops array. */
66 int num;
68 /* Basic block of loop header. */
69 basic_block header;
71 /* Basic block of loop latch. */
72 basic_block latch;
74 /* Basic block of loop preheader or NULL if it does not exist. */
75 basic_block pre_header;
77 /* For loop unrolling/peeling decision. */
78 struct lpt_decision lpt_decision;
80 /* Simple loop description. */
81 int simple;
82 struct loop_desc desc;
83 int has_desc;
85 /* Number of loop insns. */
86 unsigned ninsns;
88 /* Average number of executed insns per iteration. */
89 unsigned av_ninsns;
91 /* Array of edges along the preheader extended basic block trace.
92 The source of the first edge is the root node of preheader
93 extended basic block, if it exists. */
94 edge *pre_header_edges;
96 /* Number of edges along the pre_header extended basic block trace. */
97 int num_pre_header_edges;
99 /* The first block in the loop. This is not necessarily the same as
100 the loop header. */
101 basic_block first;
103 /* The last block in the loop. This is not necessarily the same as
104 the loop latch. */
105 basic_block last;
107 /* Bitmap of blocks contained within the loop. */
108 sbitmap nodes;
110 /* Number of blocks contained within the loop. */
111 unsigned num_nodes;
113 /* Array of edges that enter the loop. */
114 edge *entry_edges;
116 /* Number of edges that enter the loop. */
117 int num_entries;
119 /* Array of edges that exit the loop. */
120 edge *exit_edges;
122 /* Number of edges that exit the loop. */
123 int num_exits;
125 /* Bitmap of blocks that dominate all exits of the loop. */
126 sbitmap exits_doms;
128 /* The loop nesting depth. */
129 int depth;
131 /* Superloops of the loop. */
132 struct loop **pred;
134 /* The height of the loop (enclosed loop levels) within the loop
135 hierarchy tree. */
136 int level;
138 /* The outer (parent) loop or NULL if outermost loop. */
139 struct loop *outer;
141 /* The first inner (child) loop or NULL if innermost loop. */
142 struct loop *inner;
144 /* Link to the next (sibling) loop. */
145 struct loop *next;
147 /* Loop that is copy of this loop. */
148 struct loop *copy;
150 /* Nonzero if the loop is invalid (e.g., contains setjmp.). */
151 int invalid;
153 /* Auxiliary info specific to a pass. */
154 void *aux;
156 /* The following are currently used by loop.c but they are likely to
157 disappear as loop.c is converted to use the CFG. */
159 /* Nonzero if the loop has a NOTE_INSN_LOOP_VTOP. */
160 rtx vtop;
162 /* Nonzero if the loop has a NOTE_INSN_LOOP_CONT.
163 A continue statement will generate a branch to NEXT_INSN (cont). */
164 rtx cont;
166 /* The dominator of cont. */
167 rtx cont_dominator;
169 /* The NOTE_INSN_LOOP_BEG. */
170 rtx start;
172 /* The NOTE_INSN_LOOP_END. */
173 rtx end;
175 /* For a rotated loop that is entered near the bottom,
176 this is the label at the top. Otherwise it is zero. */
177 rtx top;
179 /* Place in the loop where control enters. */
180 rtx scan_start;
182 /* The position where to sink insns out of the loop. */
183 rtx sink;
185 /* List of all LABEL_REFs which refer to code labels outside the
186 loop. Used by routines that need to know all loop exits, such as
187 final_biv_value and final_giv_value.
189 This does not include loop exits due to return instructions.
190 This is because all bivs and givs are pseudos, and hence must be
191 dead after a return, so the presence of a return does not affect
192 any of the optimizations that use this info. It is simpler to
193 just not include return instructions on this list. */
194 rtx exit_labels;
196 /* The number of LABEL_REFs on exit_labels for this loop and all
197 loops nested inside it. */
198 int exit_count;
201 /* Flags for state of loop structure. */
202 enum
204 LOOPS_HAVE_PREHEADERS = 1,
205 LOOPS_HAVE_SIMPLE_LATCHES = 2,
206 LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS = 4
209 /* Structure to hold CFG information about natural loops within a function. */
210 struct loops
212 /* Number of natural loops in the function. */
213 unsigned num;
215 /* Maximum nested loop level in the function. */
216 unsigned levels;
218 /* Array of natural loop descriptors (scanning this array in reverse order
219 will find the inner loops before their enclosing outer loops). */
220 struct loop *array;
222 /* The above array is unused in new loop infrastructure and is kept only for
223 purposes of the old loop optimizer. Instead we store just pointers to
224 loops here. */
225 struct loop **parray;
227 /* Pointer to root of loop hierarchy tree. */
228 struct loop *tree_root;
230 /* Information derived from the CFG. */
231 struct cfg
233 /* The ordering of the basic blocks in a depth first search. */
234 int *dfs_order;
236 /* The reverse completion ordering of the basic blocks found in a
237 depth first search. */
238 int *rc_order;
239 } cfg;
241 /* Headers shared by multiple loops that should be merged. */
242 sbitmap shared_headers;
244 /* State of loops. */
245 int state;
248 /* Flags for loop discovery. */
250 #define LOOP_TREE 1 /* Build loop hierarchy tree. */
251 #define LOOP_PRE_HEADER 2 /* Analyze loop preheader. */
252 #define LOOP_ENTRY_EDGES 4 /* Find entry edges. */
253 #define LOOP_EXIT_EDGES 8 /* Find exit edges. */
254 #define LOOP_EDGES (LOOP_ENTRY_EDGES | LOOP_EXIT_EDGES)
255 #define LOOP_ALL 15 /* All of the above */
257 /* Loop recognition. */
258 extern int flow_loops_find (struct loops *, int flags);
259 extern int flow_loops_update (struct loops *, int flags);
260 extern void flow_loops_free (struct loops *);
261 extern void flow_loops_dump (const struct loops *, FILE *,
262 void (*)(const struct loop *, FILE *, int), int);
263 extern void flow_loop_dump (const struct loop *, FILE *,
264 void (*)(const struct loop *, FILE *, int), int);
265 extern int flow_loop_scan (struct loop *, int);
266 extern void flow_loop_free (struct loop *);
267 void mark_irreducible_loops (struct loops *);
269 /* Loop data structure manipulation/querying. */
270 extern void flow_loop_tree_node_add (struct loop *, struct loop *);
271 extern void flow_loop_tree_node_remove (struct loop *);
272 extern bool flow_loop_outside_edge_p (const struct loop *, edge);
273 extern bool flow_loop_nested_p (const struct loop *, const struct loop *);
274 extern bool flow_bb_inside_loop_p (const struct loop *, const basic_block);
275 extern struct loop * find_common_loop (struct loop *, struct loop *);
276 extern int num_loop_insns (struct loop *);
277 extern int average_num_loop_insns (struct loop *);
279 /* Loops & cfg manipulation. */
280 extern basic_block *get_loop_body (const struct loop *);
281 extern basic_block *get_loop_body_in_dom_order (const struct loop *);
282 extern edge *get_loop_exit_edges (const struct loop *, unsigned *);
283 extern unsigned num_loop_branches (const struct loop *);
285 extern edge loop_preheader_edge (const struct loop *);
286 extern edge loop_latch_edge (const struct loop *);
288 extern void add_bb_to_loop (basic_block, struct loop *);
289 extern void remove_bb_from_loops (basic_block);
291 extern void cancel_loop (struct loops *, struct loop *);
292 extern void cancel_loop_tree (struct loops *, struct loop *);
294 extern basic_block loop_split_edge_with (edge, rtx);
295 extern int fix_loop_placement (struct loop *);
297 enum
299 CP_SIMPLE_PREHEADERS = 1
302 extern void create_preheaders (struct loops *, int);
303 extern void force_single_succ_latches (struct loops *);
305 extern void verify_loop_structure (struct loops *);
307 /* Loop analysis. */
308 extern bool simple_loop_p (struct loop *, struct loop_desc *);
309 extern rtx count_loop_iterations (struct loop_desc *, rtx, rtx);
310 extern bool just_once_each_iteration_p (struct loop *, basic_block);
311 extern unsigned expected_loop_iterations (const struct loop *);
313 /* Loop manipulation. */
314 extern bool can_duplicate_loop_p (struct loop *loop);
316 #define DLTHE_FLAG_UPDATE_FREQ 1 /* Update frequencies in
317 duplicate_loop_to_header_edge. */
319 extern int duplicate_loop_to_header_edge (struct loop *, edge, struct loops *,
320 unsigned, sbitmap, edge, edge *,
321 unsigned *, int);
322 extern struct loop *loopify (struct loops *, edge, edge, basic_block);
323 extern void unloop (struct loops *, struct loop *);
324 extern bool remove_path (struct loops *, edge);
325 extern edge split_loop_bb (basic_block, rtx);
327 /* Induction variable analysis. */
329 /* The description of induction variable. The things are a bit complicated
330 due to need to handle subregs and extends. The value of the object described
331 by it can be obtained as follows (all computations are done in extend_mode):
333 Value in i-th iteration is
334 delta + mult * extend_{extend_mode} (subreg_{mode} (base + i * step)).
336 If first_special is true, the value in the first iteration is
337 delta + mult * base
339 If extend = NIL, first_special must be false, delta 0, mult 1 and value is
340 subreg_{mode} (base + i * step)
342 The get_iv_value function can be used to obtain these expressions.
344 ??? Add a third mode field that would specify the mode in that inner
345 computation is done, which would enable it to be different from the
346 outer one? */
348 struct rtx_iv
350 /* Its base and step (mode of base and step is supposed to be extend_mode,
351 see the description above). */
352 rtx base, step;
354 /* The type of extend applied to it (SIGN_EXTEND, ZERO_EXTEND or NIL). */
355 enum rtx_code extend;
357 /* Operations applied in the extended mode. */
358 rtx delta, mult;
360 /* The mode it is extended to. */
361 enum machine_mode extend_mode;
363 /* The mode the variable iterates in. */
364 enum machine_mode mode;
366 /* Whether we have already filled the remaining fields. */
367 unsigned analysed : 1;
369 /* Whether the first iteration needs to be handled specially. */
370 unsigned first_special : 1;
373 /* This should replace struct loop_desc. We keep this just so that we are
374 able to compare the results. */
376 struct niter_desc
378 /* The edge out of the loop. */
379 edge out_edge;
381 /* The other edge leading from the condition. */
382 edge in_edge;
384 /* True if we are able to say anything about number of iterations of the
385 loop. */
386 bool simple_p;
388 /* True if the loop iterates the constant number of times. */
389 bool const_iter;
391 /* Number of iterations if constant. */
392 unsigned HOST_WIDEST_INT niter;
394 /* Upper bound on the number of iterations. */
395 unsigned HOST_WIDEST_INT niter_max;
397 /* Assumptions under that the rest of the information is valid. */
398 rtx assumptions;
400 /* Assumptions under that the loop ends before reaching the latch,
401 even if value of niter_expr says otherwise. */
402 rtx noloop_assumptions;
404 /* Condition under that the loop is infinite. */
405 rtx infinite;
407 /* Whether the comparison is signed. */
408 bool signed_p;
410 /* The mode in that niter_expr should be computed. */
411 enum machine_mode mode;
413 /* The number of iterations of the loop. */
414 rtx niter_expr;
417 extern void iv_analysis_loop_init (struct loop *);
418 extern rtx iv_get_reaching_def (rtx, rtx);
419 extern bool iv_analyze (rtx, rtx, struct rtx_iv *);
420 extern rtx get_iv_value (struct rtx_iv *, rtx);
421 extern void find_simple_exit (struct loop *, struct niter_desc *);
422 extern void iv_number_of_iterations (struct loop *, rtx, rtx,
423 struct niter_desc *);
424 extern void iv_analysis_done (void);
426 extern struct niter_desc *get_simple_loop_desc (struct loop *loop);
427 extern void free_simple_loop_desc (struct loop *loop);
429 static inline struct niter_desc *
430 simple_loop_desc (struct loop *loop)
432 return loop->aux;
435 /* Loop optimizer initialization. */
436 extern struct loops *loop_optimizer_init (FILE *);
437 extern void loop_optimizer_finalize (struct loops *, FILE *);
439 /* Optimization passes. */
440 extern void unswitch_loops (struct loops *);
442 enum
444 UAP_PEEL = 1, /* Enables loop peeling. */
445 UAP_UNROLL = 2, /* Enables peeling of loops if it seems profitable. */
446 UAP_UNROLL_ALL = 4 /* Enables peeling of all loops. */
449 extern void unroll_and_peel_loops (struct loops *, int);