OSDN Git Service

* expr.c (highest_pow2_factor_for_type): Rename into
[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
251 /* Loops & cfg manipulation.  */
252 extern basic_block *get_loop_body (const struct loop *);
253 extern basic_block *get_loop_body_in_dom_order (const struct loop *);
254 extern edge *get_loop_exit_edges (const struct loop *, unsigned *);
255 extern unsigned num_loop_branches (const struct loop *);
256
257 extern edge loop_preheader_edge (const struct loop *);
258 extern edge loop_latch_edge (const struct loop *);
259
260 extern void add_bb_to_loop (basic_block, struct loop *);
261 extern void remove_bb_from_loops (basic_block);
262
263 extern void cancel_loop (struct loops *, struct loop *);
264 extern void cancel_loop_tree (struct loops *, struct loop *);
265
266 extern basic_block loop_split_edge_with (edge, rtx);
267 extern int fix_loop_placement (struct loop *);
268
269 enum
270 {
271   CP_SIMPLE_PREHEADERS = 1
272 };
273
274 extern void create_preheaders (struct loops *, int);
275 extern void force_single_succ_latches (struct loops *);
276
277 extern void verify_loop_structure (struct loops *);
278
279 /* Loop analysis.  */
280 extern bool just_once_each_iteration_p (struct loop *, basic_block);
281 extern unsigned expected_loop_iterations (const struct loop *);
282
283 /* Loop manipulation.  */
284 extern bool can_duplicate_loop_p (struct loop *loop);
285
286 #define DLTHE_FLAG_UPDATE_FREQ  1       /* Update frequencies in
287                                            duplicate_loop_to_header_edge.  */
288
289 extern int duplicate_loop_to_header_edge (struct loop *, edge, struct loops *,
290                                           unsigned, sbitmap, edge, edge *,
291                                           unsigned *, int);
292 extern struct loop *loopify (struct loops *, edge, edge, basic_block);
293 extern void unloop (struct loops *, struct loop *);
294 extern bool remove_path (struct loops *, edge);
295 extern edge split_loop_bb (basic_block, rtx);
296
297 /* Induction variable analysis.  */
298
299 /* The description of induction variable.  The things are a bit complicated
300    due to need to handle subregs and extends.  The value of the object described
301    by it can be obtained as follows (all computations are done in extend_mode):
302
303    Value in i-th iteration is
304      delta + mult * extend_{extend_mode} (subreg_{mode} (base + i * step)).
305
306    If first_special is true, the value in the first iteration is
307      delta + mult * base
308      
309    If extend = NIL, first_special must be false, delta 0, mult 1 and value is
310      subreg_{mode} (base + i * step)
311
312    The get_iv_value function can be used to obtain these expressions.
313
314    ??? Add a third mode field that would specify the mode in that inner
315    computation is done, which would enable it to be different from the
316    outer one?  */
317
318 struct rtx_iv
319 {
320   /* Its base and step (mode of base and step is supposed to be extend_mode,
321      see the description above).  */
322   rtx base, step;
323
324   /* The type of extend applied to it (SIGN_EXTEND, ZERO_EXTEND or NIL).  */
325   enum rtx_code extend;
326
327   /* Operations applied in the extended mode.  */
328   rtx delta, mult;
329
330   /* The mode it is extended to.  */
331   enum machine_mode extend_mode;
332
333   /* The mode the variable iterates in.  */
334   enum machine_mode mode;
335
336   /* Whether we have already filled the remaining fields.  */
337   unsigned analysed : 1;
338
339   /* Whether the first iteration needs to be handled specially.  */
340   unsigned first_special : 1;
341 };
342
343 /* The description of an exit from the loop and of the number of iterations
344    till we take the exit.  */
345
346 struct niter_desc
347 {
348   /* The edge out of the loop.  */
349   edge out_edge;
350
351   /* The other edge leading from the condition.  */
352   edge in_edge;
353
354   /* True if we are able to say anything about number of iterations of the
355      loop.  */
356   bool simple_p;
357
358   /* True if the loop iterates the constant number of times.  */
359   bool const_iter;
360
361   /* Number of iterations if constant.  */
362   unsigned HOST_WIDEST_INT niter;
363
364   /* Upper bound on the number of iterations.  */
365   unsigned HOST_WIDEST_INT niter_max;
366
367   /* Assumptions under that the rest of the information is valid.  */
368   rtx assumptions;
369
370   /* Assumptions under that the loop ends before reaching the latch,
371      even if value of niter_expr says otherwise.  */
372   rtx noloop_assumptions;
373
374   /* Condition under that the loop is infinite.  */
375   rtx infinite;
376
377   /* Whether the comparison is signed.  */
378   bool signed_p;
379
380   /* The mode in that niter_expr should be computed.  */
381   enum machine_mode mode;
382
383   /* The number of iterations of the loop.  */
384   rtx niter_expr;
385 };
386
387 extern void iv_analysis_loop_init (struct loop *);
388 extern rtx iv_get_reaching_def (rtx, rtx);
389 extern bool iv_analyze (rtx, rtx, struct rtx_iv *);
390 extern rtx get_iv_value (struct rtx_iv *, rtx);
391 extern void find_simple_exit (struct loop *, struct niter_desc *);
392 extern void iv_number_of_iterations (struct loop *, rtx, rtx,
393                                      struct niter_desc *);
394 extern void iv_analysis_done (void);
395
396 extern struct niter_desc *get_simple_loop_desc (struct loop *loop);
397 extern void free_simple_loop_desc (struct loop *loop);
398
399 static inline struct niter_desc *
400 simple_loop_desc (struct loop *loop)
401 {
402   return loop->aux;
403 }
404
405 /* Loop optimizer initialization.  */
406 extern struct loops *loop_optimizer_init (FILE *);
407 extern void loop_optimizer_finalize (struct loops *, FILE *);
408
409 /* Optimization passes.  */
410 extern void unswitch_loops (struct loops *);
411
412 enum
413 {
414   UAP_PEEL = 1,         /* Enables loop peeling.  */
415   UAP_UNROLL = 2,       /* Enables peeling of loops if it seems profitable.  */
416   UAP_UNROLL_ALL = 4    /* Enables peeling of all loops.  */
417 };
418
419 extern void unroll_and_peel_loops (struct loops *, int);