* system.h: Poison NO_RECURSIVE_FUNCTION_CSE.
[official-gcc.git] / gcc / cfgloop.h
blobd9c758408f22fe80779a61b5c4555ef60e9e04e1
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 /* Structure to hold information for each natural loop. */
40 struct loop
42 /* Index into loops array. */
43 int num;
45 /* Basic block of loop header. */
46 basic_block header;
48 /* Basic block of loop latch. */
49 basic_block latch;
51 /* Basic block of loop preheader or NULL if it does not exist. */
52 basic_block pre_header;
54 /* For loop unrolling/peeling decision. */
55 struct lpt_decision lpt_decision;
57 /* Number of loop insns. */
58 unsigned ninsns;
60 /* Average number of executed insns per iteration. */
61 unsigned av_ninsns;
63 /* Array of edges along the preheader extended basic block trace.
64 The source of the first edge is the root node of preheader
65 extended basic block, if it exists. */
66 edge *pre_header_edges;
68 /* Number of edges along the pre_header extended basic block trace. */
69 int num_pre_header_edges;
71 /* The first block in the loop. This is not necessarily the same as
72 the loop header. */
73 basic_block first;
75 /* The last block in the loop. This is not necessarily the same as
76 the loop latch. */
77 basic_block last;
79 /* Bitmap of blocks contained within the loop. */
80 sbitmap nodes;
82 /* Number of blocks contained within the loop. */
83 unsigned num_nodes;
85 /* Array of edges that enter the loop. */
86 edge *entry_edges;
88 /* Number of edges that enter the loop. */
89 int num_entries;
91 /* Array of edges that exit the loop. */
92 edge *exit_edges;
94 /* Number of edges that exit the loop. */
95 int num_exits;
97 /* Bitmap of blocks that dominate all exits of the loop. */
98 sbitmap exits_doms;
100 /* The loop nesting depth. */
101 int depth;
103 /* Superloops of the loop. */
104 struct loop **pred;
106 /* The height of the loop (enclosed loop levels) within the loop
107 hierarchy tree. */
108 int level;
110 /* The outer (parent) loop or NULL if outermost loop. */
111 struct loop *outer;
113 /* The first inner (child) loop or NULL if innermost loop. */
114 struct loop *inner;
116 /* Link to the next (sibling) loop. */
117 struct loop *next;
119 /* Loop that is copy of this loop. */
120 struct loop *copy;
122 /* Nonzero if the loop is invalid (e.g., contains setjmp.). */
123 int invalid;
125 /* Auxiliary info specific to a pass. */
126 void *aux;
128 /* The following are currently used by loop.c but they are likely to
129 disappear as loop.c is converted to use the CFG. */
131 /* Nonzero if the loop has a NOTE_INSN_LOOP_VTOP. */
132 rtx vtop;
134 /* Nonzero if the loop has a NOTE_INSN_LOOP_CONT.
135 A continue statement will generate a branch to NEXT_INSN (cont). */
136 rtx cont;
138 /* The dominator of cont. */
139 rtx cont_dominator;
141 /* The NOTE_INSN_LOOP_BEG. */
142 rtx start;
144 /* The NOTE_INSN_LOOP_END. */
145 rtx end;
147 /* For a rotated loop that is entered near the bottom,
148 this is the label at the top. Otherwise it is zero. */
149 rtx top;
151 /* Place in the loop where control enters. */
152 rtx scan_start;
154 /* The position where to sink insns out of the loop. */
155 rtx sink;
157 /* List of all LABEL_REFs which refer to code labels outside the
158 loop. Used by routines that need to know all loop exits, such as
159 final_biv_value and final_giv_value.
161 This does not include loop exits due to return instructions.
162 This is because all bivs and givs are pseudos, and hence must be
163 dead after a return, so the presence of a return does not affect
164 any of the optimizations that use this info. It is simpler to
165 just not include return instructions on this list. */
166 rtx exit_labels;
168 /* The number of LABEL_REFs on exit_labels for this loop and all
169 loops nested inside it. */
170 int exit_count;
173 /* Flags for state of loop structure. */
174 enum
176 LOOPS_HAVE_PREHEADERS = 1,
177 LOOPS_HAVE_SIMPLE_LATCHES = 2,
178 LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS = 4
181 /* Structure to hold CFG information about natural loops within a function. */
182 struct loops
184 /* Number of natural loops in the function. */
185 unsigned num;
187 /* Maximum nested loop level in the function. */
188 unsigned levels;
190 /* Array of natural loop descriptors (scanning this array in reverse order
191 will find the inner loops before their enclosing outer loops). */
192 struct loop *array;
194 /* The above array is unused in new loop infrastructure and is kept only for
195 purposes of the old loop optimizer. Instead we store just pointers to
196 loops here. */
197 struct loop **parray;
199 /* Pointer to root of loop hierarchy tree. */
200 struct loop *tree_root;
202 /* Information derived from the CFG. */
203 struct cfg
205 /* The ordering of the basic blocks in a depth first search. */
206 int *dfs_order;
208 /* The reverse completion ordering of the basic blocks found in a
209 depth first search. */
210 int *rc_order;
211 } cfg;
213 /* Headers shared by multiple loops that should be merged. */
214 sbitmap shared_headers;
216 /* State of loops. */
217 int state;
220 /* Flags for loop discovery. */
222 #define LOOP_TREE 1 /* Build loop hierarchy tree. */
223 #define LOOP_PRE_HEADER 2 /* Analyze loop preheader. */
224 #define LOOP_ENTRY_EDGES 4 /* Find entry edges. */
225 #define LOOP_EXIT_EDGES 8 /* Find exit edges. */
226 #define LOOP_EDGES (LOOP_ENTRY_EDGES | LOOP_EXIT_EDGES)
227 #define LOOP_ALL 15 /* All of the above */
229 /* Loop recognition. */
230 extern int flow_loops_find (struct loops *, int flags);
231 extern int flow_loops_update (struct loops *, int flags);
232 extern void flow_loops_free (struct loops *);
233 extern void flow_loops_dump (const struct loops *, FILE *,
234 void (*)(const struct loop *, FILE *, int), int);
235 extern void flow_loop_dump (const struct loop *, FILE *,
236 void (*)(const struct loop *, FILE *, int), int);
237 extern int flow_loop_scan (struct loop *, int);
238 extern void flow_loop_free (struct loop *);
239 void mark_irreducible_loops (struct loops *);
240 extern void create_loop_notes (void);
242 /* Loop data structure manipulation/querying. */
243 extern void flow_loop_tree_node_add (struct loop *, struct loop *);
244 extern void flow_loop_tree_node_remove (struct loop *);
245 extern bool flow_loop_outside_edge_p (const struct loop *, edge);
246 extern bool flow_loop_nested_p (const struct loop *, const struct loop *);
247 extern bool flow_bb_inside_loop_p (const struct loop *, const basic_block);
248 extern struct loop * find_common_loop (struct loop *, struct loop *);
249 extern int num_loop_insns (struct loop *);
250 extern int average_num_loop_insns (struct loop *);
251 extern unsigned get_loop_level (const struct loop *);
253 /* Loops & cfg manipulation. */
254 extern basic_block *get_loop_body (const struct loop *);
255 extern basic_block *get_loop_body_in_dom_order (const struct loop *);
256 extern edge *get_loop_exit_edges (const struct loop *, unsigned *);
257 extern unsigned num_loop_branches (const struct loop *);
259 extern edge loop_preheader_edge (const struct loop *);
260 extern edge loop_latch_edge (const struct loop *);
262 extern void add_bb_to_loop (basic_block, struct loop *);
263 extern void remove_bb_from_loops (basic_block);
265 extern void cancel_loop (struct loops *, struct loop *);
266 extern void cancel_loop_tree (struct loops *, struct loop *);
268 extern basic_block loop_split_edge_with (edge, rtx);
269 extern int fix_loop_placement (struct loop *);
271 enum
273 CP_SIMPLE_PREHEADERS = 1
276 extern void create_preheaders (struct loops *, int);
277 extern void force_single_succ_latches (struct loops *);
279 extern void verify_loop_structure (struct loops *);
281 /* Loop analysis. */
282 extern bool just_once_each_iteration_p (struct loop *, basic_block);
283 extern unsigned expected_loop_iterations (const struct loop *);
285 /* Loop manipulation. */
286 extern bool can_duplicate_loop_p (struct loop *loop);
288 #define DLTHE_FLAG_UPDATE_FREQ 1 /* Update frequencies in
289 duplicate_loop_to_header_edge. */
291 extern int duplicate_loop_to_header_edge (struct loop *, edge, struct loops *,
292 unsigned, sbitmap, edge, edge *,
293 unsigned *, int);
294 extern struct loop *loopify (struct loops *, edge, edge, basic_block);
295 extern void unloop (struct loops *, struct loop *);
296 extern bool remove_path (struct loops *, edge);
297 extern edge split_loop_bb (basic_block, rtx);
299 /* Induction variable analysis. */
301 /* The description of induction variable. The things are a bit complicated
302 due to need to handle subregs and extends. The value of the object described
303 by it can be obtained as follows (all computations are done in extend_mode):
305 Value in i-th iteration is
306 delta + mult * extend_{extend_mode} (subreg_{mode} (base + i * step)).
308 If first_special is true, the value in the first iteration is
309 delta + mult * base
311 If extend = NIL, first_special must be false, delta 0, mult 1 and value is
312 subreg_{mode} (base + i * step)
314 The get_iv_value function can be used to obtain these expressions.
316 ??? Add a third mode field that would specify the mode in that inner
317 computation is done, which would enable it to be different from the
318 outer one? */
320 struct rtx_iv
322 /* Its base and step (mode of base and step is supposed to be extend_mode,
323 see the description above). */
324 rtx base, step;
326 /* The type of extend applied to it (SIGN_EXTEND, ZERO_EXTEND or NIL). */
327 enum rtx_code extend;
329 /* Operations applied in the extended mode. */
330 rtx delta, mult;
332 /* The mode it is extended to. */
333 enum machine_mode extend_mode;
335 /* The mode the variable iterates in. */
336 enum machine_mode mode;
338 /* Whether we have already filled the remaining fields. */
339 unsigned analysed : 1;
341 /* Whether the first iteration needs to be handled specially. */
342 unsigned first_special : 1;
345 /* The description of an exit from the loop and of the number of iterations
346 till we take the exit. */
348 struct niter_desc
350 /* The edge out of the loop. */
351 edge out_edge;
353 /* The other edge leading from the condition. */
354 edge in_edge;
356 /* True if we are able to say anything about number of iterations of the
357 loop. */
358 bool simple_p;
360 /* True if the loop iterates the constant number of times. */
361 bool const_iter;
363 /* Number of iterations if constant. */
364 unsigned HOST_WIDEST_INT niter;
366 /* Upper bound on the number of iterations. */
367 unsigned HOST_WIDEST_INT niter_max;
369 /* Assumptions under that the rest of the information is valid. */
370 rtx assumptions;
372 /* Assumptions under that the loop ends before reaching the latch,
373 even if value of niter_expr says otherwise. */
374 rtx noloop_assumptions;
376 /* Condition under that the loop is infinite. */
377 rtx infinite;
379 /* Whether the comparison is signed. */
380 bool signed_p;
382 /* The mode in that niter_expr should be computed. */
383 enum machine_mode mode;
385 /* The number of iterations of the loop. */
386 rtx niter_expr;
389 extern void iv_analysis_loop_init (struct loop *);
390 extern rtx iv_get_reaching_def (rtx, rtx);
391 extern bool iv_analyze (rtx, rtx, struct rtx_iv *);
392 extern rtx get_iv_value (struct rtx_iv *, rtx);
393 extern void find_simple_exit (struct loop *, struct niter_desc *);
394 extern void iv_number_of_iterations (struct loop *, rtx, rtx,
395 struct niter_desc *);
396 extern void iv_analysis_done (void);
398 extern struct niter_desc *get_simple_loop_desc (struct loop *loop);
399 extern void free_simple_loop_desc (struct loop *loop);
401 static inline struct niter_desc *
402 simple_loop_desc (struct loop *loop)
404 return loop->aux;
407 /* Loop optimizer initialization. */
408 extern struct loops *loop_optimizer_init (FILE *);
409 extern void loop_optimizer_finalize (struct loops *, FILE *);
411 /* Optimization passes. */
412 extern void unswitch_loops (struct loops *);
414 enum
416 UAP_PEEL = 1, /* Enables loop peeling. */
417 UAP_UNROLL = 2, /* Enables peeling of loops if it seems profitable. */
418 UAP_UNROLL_ALL = 4 /* Enables peeling of all loops. */
421 extern void unroll_and_peel_loops (struct loops *, int);
422 extern void doloop_optimize_loops (struct loops *);