OSDN Git Service

2004-04-05 Caroline Tice <ctice@apple.com>
[pf3gnuchains/gcc-fork.git] / gcc / cfgloop.h
1 /* Natural loop functions
2    Copyright (C) 1987, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
3    Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
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.
11
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.
16
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.  */
21
22 /* Structure to hold decision about unrolling/peeling.  */
23 enum lpt_dec
24 {
25   LPT_NONE,
26   LPT_PEEL_COMPLETELY,
27   LPT_PEEL_SIMPLE,
28   LPT_UNROLL_CONSTANT,
29   LPT_UNROLL_RUNTIME,
30   LPT_UNROLL_STUPID
31 };
32
33 struct lpt_decision
34 {
35   enum lpt_dec decision;
36   unsigned times;
37 };
38
39 /* Structure to hold information for each natural loop.  */
40 struct loop
41 {
42   /* Index into loops array.  */
43   int num;
44
45   /* Basic block of loop header.  */
46   basic_block header;
47
48   /* Basic block of loop latch.  */
49   basic_block latch;
50
51   /* Basic block of loop preheader or NULL if it does not exist.  */
52   basic_block pre_header;
53
54   /* For loop unrolling/peeling decision.  */
55   struct lpt_decision lpt_decision;
56
57   /* Number of loop insns.  */
58   unsigned ninsns;
59
60   /* Average number of executed insns per iteration.  */
61   unsigned av_ninsns;
62
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;
67
68   /* Number of edges along the pre_header extended basic block trace.  */
69   int num_pre_header_edges;
70
71   /* The first block in the loop.  This is not necessarily the same as
72      the loop header.  */
73   basic_block first;
74
75   /* The last block in the loop.  This is not necessarily the same as
76      the loop latch.  */
77   basic_block last;
78
79   /* Bitmap of blocks contained within the loop.  */
80   sbitmap nodes;
81
82   /* Number of blocks contained within the loop.  */
83   unsigned num_nodes;
84
85   /* Array of edges that enter the loop.  */
86   edge *entry_edges;
87
88   /* Number of edges that enter the loop.  */
89   int num_entries;
90
91   /* Array of edges that exit the loop.  */
92   edge *exit_edges;
93
94   /* Number of edges that exit the loop.  */
95   int num_exits;
96
97   /* Bitmap of blocks that dominate all exits of the loop.  */
98   sbitmap exits_doms;
99
100   /* The loop nesting depth.  */
101   int depth;
102
103   /* Superloops of the loop.  */
104   struct loop **pred;
105
106   /* The height of the loop (enclosed loop levels) within the loop
107      hierarchy tree.  */
108   int level;
109
110   /* The outer (parent) loop or NULL if outermost loop.  */
111   struct loop *outer;
112
113   /* The first inner (child) loop or NULL if innermost loop.  */
114   struct loop *inner;
115
116   /* Link to the next (sibling) loop.  */
117   struct loop *next;
118
119   /* Loop that is copy of this loop.  */
120   struct loop *copy;
121
122   /* Nonzero if the loop is invalid (e.g., contains setjmp.).  */
123   int invalid;
124
125   /* Auxiliary info specific to a pass.  */
126   void *aux;
127
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.  */
130
131   /* Nonzero if the loop has a NOTE_INSN_LOOP_VTOP.  */
132   rtx vtop;
133
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;
137
138   /* The dominator of cont.  */
139   rtx cont_dominator;
140
141   /* The NOTE_INSN_LOOP_BEG.  */
142   rtx start;
143
144   /* The NOTE_INSN_LOOP_END.  */
145   rtx end;
146
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;
150
151   /* Place in the loop where control enters.  */
152   rtx scan_start;
153
154   /* The position where to sink insns out of the loop.  */
155   rtx sink;
156
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.
160
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;
167
168   /* The number of LABEL_REFs on exit_labels for this loop and all
169      loops nested inside it.  */
170   int exit_count;
171 };
172
173 /* Flags for state of loop structure.  */
174 enum
175 {
176   LOOPS_HAVE_PREHEADERS = 1,
177   LOOPS_HAVE_SIMPLE_LATCHES = 2,
178   LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS = 4
179 };
180
181 /* Structure to hold CFG information about natural loops within a function.  */
182 struct loops
183 {
184   /* Number of natural loops in the function.  */
185   unsigned num;
186
187   /* Maximum nested loop level in the function.  */
188   unsigned levels;
189
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;
193
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;
198
199   /* Pointer to root of loop hierarchy tree.  */
200   struct loop *tree_root;
201
202   /* Information derived from the CFG.  */
203   struct cfg
204   {
205     /* The ordering of the basic blocks in a depth first search.  */
206     int *dfs_order;
207
208     /* The reverse completion ordering of the basic blocks found in a
209        depth first search.  */
210     int *rc_order;
211   } cfg;
212
213   /* Headers shared by multiple loops that should be merged.  */
214   sbitmap shared_headers;
215
216   /* State of loops.  */
217   int state;
218 };
219
220 /* Flags for loop discovery.  */
221
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  */
228
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
241 /* Loop data structure manipulation/querying.  */
242 extern void flow_loop_tree_node_add (struct loop *, struct loop *);
243 extern void flow_loop_tree_node_remove (struct loop *);
244 extern bool flow_loop_outside_edge_p (const struct loop *, edge);
245 extern bool flow_loop_nested_p  (const struct loop *, const struct loop *);
246 extern bool flow_bb_inside_loop_p (const struct loop *, const basic_block);
247 extern struct loop * find_common_loop (struct loop *, struct loop *);
248 extern int num_loop_insns (struct loop *);
249 extern int average_num_loop_insns (struct loop *);
250 extern unsigned get_loop_level (const struct loop *);
251
252 /* Loops & cfg manipulation.  */
253 extern basic_block *get_loop_body (const struct loop *);
254 extern basic_block *get_loop_body_in_dom_order (const struct loop *);
255 extern edge *get_loop_exit_edges (const struct loop *, unsigned *);
256 extern unsigned num_loop_branches (const struct loop *);
257
258 extern edge loop_preheader_edge (const struct loop *);
259 extern edge loop_latch_edge (const struct loop *);
260
261 extern void add_bb_to_loop (basic_block, struct loop *);
262 extern void remove_bb_from_loops (basic_block);
263
264 extern void cancel_loop (struct loops *, struct loop *);
265 extern void cancel_loop_tree (struct loops *, struct loop *);
266
267 extern basic_block loop_split_edge_with (edge, rtx);
268 extern int fix_loop_placement (struct loop *);
269
270 enum
271 {
272   CP_SIMPLE_PREHEADERS = 1
273 };
274
275 extern void create_preheaders (struct loops *, int);
276 extern void force_single_succ_latches (struct loops *);
277
278 extern void verify_loop_structure (struct loops *);
279
280 /* Loop analysis.  */
281 extern bool just_once_each_iteration_p (struct loop *, basic_block);
282 extern unsigned expected_loop_iterations (const struct loop *);
283
284 /* Loop manipulation.  */
285 extern bool can_duplicate_loop_p (struct loop *loop);
286
287 #define DLTHE_FLAG_UPDATE_FREQ  1       /* Update frequencies in
288                                            duplicate_loop_to_header_edge.  */
289
290 extern int duplicate_loop_to_header_edge (struct loop *, edge, struct loops *,
291                                           unsigned, sbitmap, edge, edge *,
292                                           unsigned *, int);
293 extern struct loop *loopify (struct loops *, edge, edge, basic_block);
294 extern void unloop (struct loops *, struct loop *);
295 extern bool remove_path (struct loops *, edge);
296 extern edge split_loop_bb (basic_block, rtx);
297
298 /* Induction variable analysis.  */
299
300 /* The description of induction variable.  The things are a bit complicated
301    due to need to handle subregs and extends.  The value of the object described
302    by it can be obtained as follows (all computations are done in extend_mode):
303
304    Value in i-th iteration is
305      delta + mult * extend_{extend_mode} (subreg_{mode} (base + i * step)).
306
307    If first_special is true, the value in the first iteration is
308      delta + mult * base
309      
310    If extend = NIL, first_special must be false, delta 0, mult 1 and value is
311      subreg_{mode} (base + i * step)
312
313    The get_iv_value function can be used to obtain these expressions.
314
315    ??? Add a third mode field that would specify the mode in that inner
316    computation is done, which would enable it to be different from the
317    outer one?  */
318
319 struct rtx_iv
320 {
321   /* Its base and step (mode of base and step is supposed to be extend_mode,
322      see the description above).  */
323   rtx base, step;
324
325   /* The type of extend applied to it (SIGN_EXTEND, ZERO_EXTEND or NIL).  */
326   enum rtx_code extend;
327
328   /* Operations applied in the extended mode.  */
329   rtx delta, mult;
330
331   /* The mode it is extended to.  */
332   enum machine_mode extend_mode;
333
334   /* The mode the variable iterates in.  */
335   enum machine_mode mode;
336
337   /* Whether we have already filled the remaining fields.  */
338   unsigned analysed : 1;
339
340   /* Whether the first iteration needs to be handled specially.  */
341   unsigned first_special : 1;
342 };
343
344 /* The description of an exit from the loop and of the number of iterations
345    till we take the exit.  */
346
347 struct niter_desc
348 {
349   /* The edge out of the loop.  */
350   edge out_edge;
351
352   /* The other edge leading from the condition.  */
353   edge in_edge;
354
355   /* True if we are able to say anything about number of iterations of the
356      loop.  */
357   bool simple_p;
358
359   /* True if the loop iterates the constant number of times.  */
360   bool const_iter;
361
362   /* Number of iterations if constant.  */
363   unsigned HOST_WIDEST_INT niter;
364
365   /* Upper bound on the number of iterations.  */
366   unsigned HOST_WIDEST_INT niter_max;
367
368   /* Assumptions under that the rest of the information is valid.  */
369   rtx assumptions;
370
371   /* Assumptions under that the loop ends before reaching the latch,
372      even if value of niter_expr says otherwise.  */
373   rtx noloop_assumptions;
374
375   /* Condition under that the loop is infinite.  */
376   rtx infinite;
377
378   /* Whether the comparison is signed.  */
379   bool signed_p;
380
381   /* The mode in that niter_expr should be computed.  */
382   enum machine_mode mode;
383
384   /* The number of iterations of the loop.  */
385   rtx niter_expr;
386 };
387
388 extern void iv_analysis_loop_init (struct loop *);
389 extern rtx iv_get_reaching_def (rtx, rtx);
390 extern bool iv_analyze (rtx, rtx, struct rtx_iv *);
391 extern rtx get_iv_value (struct rtx_iv *, rtx);
392 extern void find_simple_exit (struct loop *, struct niter_desc *);
393 extern void iv_number_of_iterations (struct loop *, rtx, rtx,
394                                      struct niter_desc *);
395 extern void iv_analysis_done (void);
396
397 extern struct niter_desc *get_simple_loop_desc (struct loop *loop);
398 extern void free_simple_loop_desc (struct loop *loop);
399
400 static inline struct niter_desc *
401 simple_loop_desc (struct loop *loop)
402 {
403   return loop->aux;
404 }
405
406 /* Loop optimizer initialization.  */
407 extern struct loops *loop_optimizer_init (FILE *);
408 extern void loop_optimizer_finalize (struct loops *, FILE *);
409
410 /* Optimization passes.  */
411 extern void unswitch_loops (struct loops *);
412
413 enum
414 {
415   UAP_PEEL = 1,         /* Enables loop peeling.  */
416   UAP_UNROLL = 2,       /* Enables peeling of loops if it seems profitable.  */
417   UAP_UNROLL_ALL = 4    /* Enables peeling of all loops.  */
418 };
419
420 extern void unroll_and_peel_loops (struct loops *, int);
421 extern void doloop_optimize_loops (struct loops *);