re PR bootstrap/31776 (Bootstrap fails with "error: conflicting types for strsignal")
[platform/upstream/gcc.git] / gcc / cfgloop.h
1 /* Natural loop functions
2    Copyright (C) 1987, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
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, 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, USA.  */
21
22 #ifndef GCC_CFGLOOP_H
23 #define GCC_CFGLOOP_H
24
25 #include "basic-block.h"
26 /* For rtx_code.  */
27 #include "rtl.h"
28 #include "vecprim.h"
29 #include "double-int.h"
30
31 /* Structure to hold decision about unrolling/peeling.  */
32 enum lpt_dec
33 {
34   LPT_NONE,
35   LPT_PEEL_COMPLETELY,
36   LPT_PEEL_SIMPLE,
37   LPT_UNROLL_CONSTANT,
38   LPT_UNROLL_RUNTIME,
39   LPT_UNROLL_STUPID
40 };
41
42 struct lpt_decision
43 {
44   enum lpt_dec decision;
45   unsigned times;
46 };
47
48 /* The structure describing a bound on number of iterations of a loop.  */
49
50 struct nb_iter_bound
51 {
52   /* The statement STMT is executed at most ...  */
53   tree stmt;
54
55   /* ... BOUND + 1 times (BOUND must be an unsigned constant).
56      The + 1 is added for the following reasons:
57
58      a) 0 would otherwise be unused, while we would need to care more about
59         overflows (as MAX + 1 is sometimes produced as the estimate on number
60         of executions of STMT).
61      b) it is consistent with the result of number_of_iterations_exit.  */
62   double_int bound;
63
64   /* True if the statement will cause the loop to be leaved the (at most) 
65      BOUND + 1-st time it is executed, that is, all the statements after it
66      are executed at most BOUND times.  */
67   bool is_exit;
68
69   /* The next bound in the list.  */
70   struct nb_iter_bound *next;
71 };
72
73 /* Description of the loop exit.  */
74
75 struct loop_exit
76 {
77   /* The exit edge.  */
78   edge e;
79
80   /* Previous and next exit in the list of the exits of the loop.  */
81   struct loop_exit *prev;
82   struct loop_exit *next;
83
84   /* Next element in the list of loops from that E exits.  */
85   struct loop_exit *next_e;
86 };
87
88 /* Structure to hold information for each natural loop.  */
89 struct loop
90 {
91   /* Index into loops array.  */
92   int num;
93
94   /* Basic block of loop header.  */
95   basic_block header;
96
97   /* Basic block of loop latch.  */
98   basic_block latch;
99
100   /* For loop unrolling/peeling decision.  */
101   struct lpt_decision lpt_decision;
102
103   /* Number of loop insns.  */
104   unsigned ninsns;
105
106   /* Average number of executed insns per iteration.  */
107   unsigned av_ninsns;
108
109   /* Number of blocks contained within the loop.  */
110   unsigned num_nodes;
111
112   /* The loop nesting depth.  */
113   int depth;
114
115   /* Superloops of the loop.  */
116   struct loop **pred;
117
118   /* The outer (parent) loop or NULL if outermost loop.  */
119   struct loop *outer;
120
121   /* The first inner (child) loop or NULL if innermost loop.  */
122   struct loop *inner;
123
124   /* Link to the next (sibling) loop.  */
125   struct loop *next;
126
127   /* Loop that is copy of this loop.  */
128   struct loop *copy;
129
130   /* Auxiliary info specific to a pass.  */
131   void *aux;
132
133   /* The number of times the latch of the loop is executed.
134      This is an INTEGER_CST or an expression containing symbolic
135      names.  Don't access this field directly:
136      number_of_latch_executions computes and caches the computed
137      information in this field.  */
138   tree nb_iterations;
139
140   /* An integer estimation of the number of iterations.  Estimate_state
141      describes what is the state of the estimation.  */
142   enum
143     {
144       /* Estimate was not computed yet.  */
145       EST_NOT_COMPUTED,
146       /* Estimate is ready.  */
147       EST_AVAILABLE
148     } estimate_state;
149
150   /* An integer guaranteed to bound the number of iterations of the loop
151      from above.  */
152   bool any_upper_bound;
153   double_int nb_iterations_upper_bound;
154
155   /* An integer giving the expected number of iterations of the loop.  */
156   bool any_estimate;
157   double_int nb_iterations_estimate;
158
159   /* Upper bound on number of iterations of a loop.  */
160   struct nb_iter_bound *bounds;
161
162   /* Head of the cyclic list of the exits of the loop.  */
163   struct loop_exit exits;
164 };
165
166 /* Flags for state of loop structure.  */
167 enum
168 {
169   LOOPS_HAVE_PREHEADERS = 1,
170   LOOPS_HAVE_SIMPLE_LATCHES = 2,
171   LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS = 4,
172   LOOPS_HAVE_RECORDED_EXITS = 8,
173   LOOPS_MAY_HAVE_MULTIPLE_LATCHES = 16,
174   LOOP_CLOSED_SSA = 32
175 };
176
177 #define LOOPS_NORMAL (LOOPS_HAVE_PREHEADERS | LOOPS_HAVE_SIMPLE_LATCHES \
178                       | LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS)
179 #define AVOID_CFG_MODIFICATIONS (LOOPS_MAY_HAVE_MULTIPLE_LATCHES)
180
181 typedef struct loop *loop_p;
182 DEF_VEC_P (loop_p);
183 DEF_VEC_ALLOC_P (loop_p, heap);
184
185 /* Structure to hold CFG information about natural loops within a function.  */
186 struct loops
187 {
188   /* State of loops.  */
189   int state;
190
191   /* Array of the loops.  */
192   VEC (loop_p, heap) *larray;
193
194   /* Maps edges to the list of their descriptions as loop exits.  Edges
195      whose sources or destinations have loop_father == NULL (which may
196      happen during the cfg manipulations) should not appear in EXITS.  */
197   htab_t exits;
198
199   /* Pointer to root of loop hierarchy tree.  */
200   struct loop *tree_root;
201 };
202
203 /* Loop recognition.  */
204 extern int flow_loops_find (struct loops *);
205 extern void disambiguate_loops_with_multiple_latches (void);
206 extern void flow_loops_free (struct loops *);
207 extern void flow_loops_dump (FILE *,
208                              void (*)(const struct loop *, FILE *, int), int);
209 extern void flow_loop_dump (const struct loop *, FILE *,
210                             void (*)(const struct loop *, FILE *, int), int);
211 struct loop *alloc_loop (void);
212 extern void flow_loop_free (struct loop *);
213 int flow_loop_nodes_find (basic_block, struct loop *);
214 void fix_loop_structure (bitmap changed_bbs);
215 void mark_irreducible_loops (void);
216 void release_recorded_exits (void);
217 void record_loop_exits (void);
218 void rescan_loop_exit (edge, bool, bool);
219
220 /* Loop data structure manipulation/querying.  */
221 extern void flow_loop_tree_node_add (struct loop *, struct loop *);
222 extern void flow_loop_tree_node_remove (struct loop *);
223 extern void add_loop (struct loop *, struct loop *);
224 extern bool flow_loop_nested_p  (const struct loop *, const struct loop *);
225 extern bool flow_bb_inside_loop_p (const struct loop *, const basic_block);
226 extern struct loop * find_common_loop (struct loop *, struct loop *);
227 struct loop *superloop_at_depth (struct loop *, unsigned);
228 struct eni_weights_d;
229 extern unsigned tree_num_loop_insns (struct loop *, struct eni_weights_d *);
230 extern int num_loop_insns (struct loop *);
231 extern int average_num_loop_insns (struct loop *);
232 extern unsigned get_loop_level (const struct loop *);
233 extern bool loop_exit_edge_p (const struct loop *, edge);
234 extern void mark_loop_exit_edges (void);
235
236 /* Loops & cfg manipulation.  */
237 extern basic_block *get_loop_body (const struct loop *);
238 extern unsigned get_loop_body_with_size (const struct loop *, basic_block *,
239                                          unsigned);
240 extern basic_block *get_loop_body_in_dom_order (const struct loop *);
241 extern basic_block *get_loop_body_in_bfs_order (const struct loop *);
242 extern VEC (edge, heap) *get_loop_exit_edges (const struct loop *);
243 edge single_exit (const struct loop *);
244 extern unsigned num_loop_branches (const struct loop *);
245
246 extern edge loop_preheader_edge (const struct loop *);
247 extern edge loop_latch_edge (const struct loop *);
248
249 extern void add_bb_to_loop (basic_block, struct loop *);
250 extern void remove_bb_from_loops (basic_block);
251
252 extern void cancel_loop_tree (struct loop *);
253 extern void delete_loop (struct loop *);
254
255 enum
256 {
257   CP_SIMPLE_PREHEADERS = 1
258 };
259
260 extern void create_preheaders (int);
261 extern void force_single_succ_latches (void);
262
263 extern void verify_loop_structure (void);
264
265 /* Loop analysis.  */
266 extern bool just_once_each_iteration_p (const struct loop *, basic_block);
267 gcov_type expected_loop_iterations_unbounded (const struct loop *);
268 extern unsigned expected_loop_iterations (const struct loop *);
269 extern rtx doloop_condition_get (rtx);
270
271 void estimate_numbers_of_iterations_loop (struct loop *);
272 HOST_WIDE_INT estimated_loop_iterations_int (struct loop *, bool);
273 bool estimated_loop_iterations (struct loop *, bool, double_int *);
274
275 /* Loop manipulation.  */
276 extern bool can_duplicate_loop_p (struct loop *loop);
277
278 #define DLTHE_FLAG_UPDATE_FREQ  1       /* Update frequencies in
279                                            duplicate_loop_to_header_edge.  */
280 #define DLTHE_RECORD_COPY_NUMBER 2      /* Record copy number in the aux
281                                            field of newly create BB.  */
282 #define DLTHE_FLAG_COMPLETTE_PEEL 4     /* Update frequencies expecting
283                                            a complete peeling.  */
284
285 extern struct loop * duplicate_loop (struct loop *, struct loop *);
286 extern bool duplicate_loop_to_header_edge (struct loop *, edge, 
287                                            unsigned, sbitmap, edge,
288                                            VEC (edge, heap) **, int);
289 extern struct loop *loopify (edge, edge,
290                              basic_block, edge, edge, bool,
291                              unsigned, unsigned);
292 struct loop * loop_version (struct loop *, void *,
293                             basic_block *, unsigned, unsigned, unsigned, bool);
294 extern bool remove_path (edge);
295 void scale_loop_frequencies (struct loop *, int, int);
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 = UNKNOWN, 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 UNKNOWN).  */
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 the first iteration needs to be handled specially.  */
337   unsigned first_special : 1;
338 };
339
340 /* The description of an exit from the loop and of the number of iterations
341    till we take the exit.  */
342
343 struct niter_desc
344 {
345   /* The edge out of the loop.  */
346   edge out_edge;
347
348   /* The other edge leading from the condition.  */
349   edge in_edge;
350
351   /* True if we are able to say anything about number of iterations of the
352      loop.  */
353   bool simple_p;
354
355   /* True if the loop iterates the constant number of times.  */
356   bool const_iter;
357
358   /* Number of iterations if constant.  */
359   unsigned HOST_WIDEST_INT niter;
360
361   /* Upper bound on the number of iterations.  */
362   unsigned HOST_WIDEST_INT niter_max;
363
364   /* Assumptions under that the rest of the information is valid.  */
365   rtx assumptions;
366
367   /* Assumptions under that the loop ends before reaching the latch,
368      even if value of niter_expr says otherwise.  */
369   rtx noloop_assumptions;
370
371   /* Condition under that the loop is infinite.  */
372   rtx infinite;
373
374   /* Whether the comparison is signed.  */
375   bool signed_p;
376
377   /* The mode in that niter_expr should be computed.  */
378   enum machine_mode mode;
379
380   /* The number of iterations of the loop.  */
381   rtx niter_expr;
382 };
383
384 extern void iv_analysis_loop_init (struct loop *);
385 extern bool iv_analyze (rtx, rtx, struct rtx_iv *);
386 extern bool iv_analyze_result (rtx, rtx, struct rtx_iv *);
387 extern bool iv_analyze_expr (rtx, rtx, enum machine_mode, struct rtx_iv *);
388 extern rtx get_iv_value (struct rtx_iv *, rtx);
389 extern bool biv_p (rtx, rtx);
390 extern void find_simple_exit (struct loop *, struct niter_desc *);
391 extern void iv_analysis_done (void);
392 extern struct df *iv_current_loop_df (void);
393
394 extern struct niter_desc *get_simple_loop_desc (struct loop *loop);
395 extern void free_simple_loop_desc (struct loop *loop);
396
397 static inline struct niter_desc *
398 simple_loop_desc (struct loop *loop)
399 {
400   return (struct niter_desc *) loop->aux;
401 }
402
403 /* Accessors for the loop structures.  */
404
405 /* Returns the loop with index NUM from current_loops.  */
406
407 static inline struct loop *
408 get_loop (unsigned num)
409 {
410   return VEC_index (loop_p, current_loops->larray, num);
411 }
412
413 /* Returns the list of loops in current_loops.  */
414
415 static inline VEC (loop_p, heap) *
416 get_loops (void)
417 {
418   if (!current_loops)
419     return NULL;
420
421   return current_loops->larray;
422 }
423
424 /* Returns the number of loops in current_loops (including the removed
425    ones and the fake loop that forms the root of the loop tree).  */
426
427 static inline unsigned
428 number_of_loops (void)
429 {
430   if (!current_loops)
431     return 0;
432
433   return VEC_length (loop_p, current_loops->larray);
434 }
435
436 /* Loop iterators.  */
437
438 /* Flags for loop iteration.  */
439
440 enum li_flags
441 {
442   LI_INCLUDE_ROOT = 1,          /* Include the fake root of the loop tree.  */
443   LI_FROM_INNERMOST = 2,        /* Iterate over the loops in the reverse order,
444                                    starting from innermost ones.  */
445   LI_ONLY_INNERMOST = 4         /* Iterate only over innermost loops.  */
446 };
447
448 /* The iterator for loops.  */
449
450 typedef struct
451 {
452   /* The list of loops to visit.  */
453   VEC(int,heap) *to_visit;
454
455   /* The index of the actual loop.  */
456   unsigned idx;
457 } loop_iterator;
458
459 static inline void
460 fel_next (loop_iterator *li, loop_p *loop)
461 {
462   int anum;
463
464   while (VEC_iterate (int, li->to_visit, li->idx, anum))
465     {
466       li->idx++;
467       *loop = get_loop (anum);
468       if (*loop)
469         return;
470     }
471
472   VEC_free (int, heap, li->to_visit);
473   *loop = NULL;
474 }
475
476 static inline void
477 fel_init (loop_iterator *li, loop_p *loop, unsigned flags)
478 {
479   struct loop *aloop;
480   unsigned i;
481   int mn;
482
483   li->idx = 0;
484   if (!current_loops)
485     {
486       li->to_visit = NULL;
487       *loop = NULL;
488       return;
489     }
490
491   li->to_visit = VEC_alloc (int, heap, number_of_loops ());
492   mn = (flags & LI_INCLUDE_ROOT) ? 0 : 1;
493
494   if (flags & LI_ONLY_INNERMOST)
495     {
496       for (i = 0; VEC_iterate (loop_p, current_loops->larray, i, aloop); i++)
497         if (aloop != NULL
498             && aloop->inner == NULL
499             && aloop->num >= mn)
500           VEC_quick_push (int, li->to_visit, aloop->num);
501     }
502   else if (flags & LI_FROM_INNERMOST)
503     {
504       /* Push the loops to LI->TO_VISIT in postorder.  */
505       for (aloop = current_loops->tree_root;
506            aloop->inner != NULL;
507            aloop = aloop->inner)
508         continue;
509
510       while (1)
511         {
512           if (aloop->num >= mn)
513             VEC_quick_push (int, li->to_visit, aloop->num);
514
515           if (aloop->next)
516             {
517               for (aloop = aloop->next;
518                    aloop->inner != NULL;
519                    aloop = aloop->inner)
520                 continue;
521             }
522           else if (!aloop->outer)
523             break;
524           else
525             aloop = aloop->outer;
526         }
527     }
528   else
529     {
530       /* Push the loops to LI->TO_VISIT in preorder.  */
531       aloop = current_loops->tree_root;
532       while (1)
533         {
534           if (aloop->num >= mn)
535             VEC_quick_push (int, li->to_visit, aloop->num);
536
537           if (aloop->inner != NULL)
538             aloop = aloop->inner;
539           else
540             {
541               while (aloop != NULL && aloop->next == NULL)
542                 aloop = aloop->outer;
543               if (aloop == NULL)
544                 break;
545               aloop = aloop->next;
546             }
547         }
548     }
549
550   fel_next (li, loop);
551 }
552
553 #define FOR_EACH_LOOP(LI, LOOP, FLAGS) \
554   for (fel_init (&(LI), &(LOOP), FLAGS); \
555        (LOOP); \
556        fel_next (&(LI), &(LOOP)))
557
558 #define FOR_EACH_LOOP_BREAK(LI) \
559   { \
560     VEC_free (int, heap, (LI)->to_visit); \
561     break; \
562   }
563
564 /* The properties of the target.  */
565
566 extern unsigned target_avail_regs;
567 extern unsigned target_res_regs;
568 extern unsigned target_reg_cost;
569 extern unsigned target_spill_cost;
570
571 /* Register pressure estimation for induction variable optimizations & loop
572    invariant motion.  */
573 extern unsigned estimate_reg_pressure_cost (unsigned, unsigned);
574 extern void init_set_costs (void);
575
576 /* Loop optimizer initialization.  */
577 extern void loop_optimizer_init (unsigned);
578 extern void loop_optimizer_finalize (void);
579
580 /* Optimization passes.  */
581 extern void unswitch_loops (void);
582
583 enum
584 {
585   UAP_PEEL = 1,         /* Enables loop peeling.  */
586   UAP_UNROLL = 2,       /* Enables unrolling of loops if it seems profitable.  */
587   UAP_UNROLL_ALL = 4    /* Enables unrolling of all loops.  */
588 };
589
590 extern void unroll_and_peel_loops (int);
591 extern void doloop_optimize_loops (void);
592 extern void move_loop_invariants (void);
593
594 #endif /* GCC_CFGLOOP_H */