]>
Commit | Line | Data |
---|---|---|
862be747 | 1 | /* Natural loop functions |
3072d30e | 2 | Copyright (C) 1987, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, |
d91f7526 | 3 | 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc. |
862be747 | 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 | |
8c4c00c1 | 9 | Software Foundation; either version 3, or (at your option) any later |
862be747 | 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 | |
8c4c00c1 | 18 | along with GCC; see the file COPYING3. If not see |
19 | <http://www.gnu.org/licenses/>. */ | |
862be747 | 20 | |
bcf3c70d | 21 | #ifndef GCC_CFGLOOP_H |
22 | #define GCC_CFGLOOP_H | |
23 | ||
24 | #include "basic-block.h" | |
ad3827a6 | 25 | #include "double-int.h" |
bcf3c70d | 26 | |
0f71a633 | 27 | #include "bitmap.h" |
28 | #include "sbitmap.h" | |
29 | ||
862be747 | 30 | /* Structure to hold decision about unrolling/peeling. */ |
31 | enum lpt_dec | |
32 | { | |
33 | LPT_NONE, | |
34 | LPT_PEEL_COMPLETELY, | |
35 | LPT_PEEL_SIMPLE, | |
36 | LPT_UNROLL_CONSTANT, | |
37 | LPT_UNROLL_RUNTIME, | |
38 | LPT_UNROLL_STUPID | |
39 | }; | |
40 | ||
fb1e4f4a | 41 | struct GTY (()) lpt_decision { |
862be747 | 42 | enum lpt_dec decision; |
43 | unsigned times; | |
44 | }; | |
45 | ||
aedb7bf8 | 46 | /* The type of extend applied to an IV. */ |
47 | enum iv_extend_code | |
48 | { | |
49 | IV_SIGN_EXTEND, | |
50 | IV_ZERO_EXTEND, | |
51 | IV_UNKNOWN_EXTEND | |
52 | }; | |
53 | ||
bc3c8ad4 | 54 | /* The structure describing a bound on number of iterations of a loop. */ |
55 | ||
fb1e4f4a | 56 | struct GTY ((chain_next ("%h.next"))) nb_iter_bound { |
faa56cf9 | 57 | /* The statement STMT is executed at most ... */ |
75a70cf9 | 58 | gimple stmt; |
faa56cf9 | 59 | |
60 | /* ... BOUND + 1 times (BOUND must be an unsigned constant). | |
61 | The + 1 is added for the following reasons: | |
62 | ||
63 | a) 0 would otherwise be unused, while we would need to care more about | |
64 | overflows (as MAX + 1 is sometimes produced as the estimate on number | |
65 | of executions of STMT). | |
66 | b) it is consistent with the result of number_of_iterations_exit. */ | |
67 | double_int bound; | |
68 | ||
48e1416a | 69 | /* True if the statement will cause the loop to be leaved the (at most) |
faa56cf9 | 70 | BOUND + 1-st time it is executed, that is, all the statements after it |
71 | are executed at most BOUND times. */ | |
72 | bool is_exit; | |
73 | ||
faa56cf9 | 74 | /* The next bound in the list. */ |
bc3c8ad4 | 75 | struct nb_iter_bound *next; |
bc3c8ad4 | 76 | }; |
77 | ||
dce58e66 | 78 | /* Description of the loop exit. */ |
79 | ||
fb1e4f4a | 80 | struct GTY (()) loop_exit { |
dce58e66 | 81 | /* The exit edge. */ |
161dfa6e | 82 | edge e; |
dce58e66 | 83 | |
84 | /* Previous and next exit in the list of the exits of the loop. */ | |
85 | struct loop_exit *prev; | |
86 | struct loop_exit *next; | |
87 | ||
88 | /* Next element in the list of loops from that E exits. */ | |
89 | struct loop_exit *next_e; | |
90 | }; | |
91 | ||
9e3536f4 | 92 | typedef struct loop *loop_p; |
9e3536f4 | 93 | |
f780cc25 | 94 | /* An integer estimation of the number of iterations. Estimate_state |
95 | describes what is the state of the estimation. */ | |
96 | enum loop_estimation | |
97 | { | |
98 | /* Estimate was not computed yet. */ | |
99 | EST_NOT_COMPUTED, | |
100 | /* Estimate is ready. */ | |
101 | EST_AVAILABLE | |
102 | }; | |
103 | ||
862be747 | 104 | /* Structure to hold information for each natural loop. */ |
fb1e4f4a | 105 | struct GTY ((chain_next ("%h.next"))) loop { |
862be747 | 106 | /* Index into loops array. */ |
107 | int num; | |
108 | ||
0ac758f7 | 109 | /* Number of loop insns. */ |
110 | unsigned ninsns; | |
111 | ||
862be747 | 112 | /* Basic block of loop header. */ |
161dfa6e | 113 | basic_block header; |
862be747 | 114 | |
115 | /* Basic block of loop latch. */ | |
161dfa6e | 116 | basic_block latch; |
862be747 | 117 | |
862be747 | 118 | /* For loop unrolling/peeling decision. */ |
119 | struct lpt_decision lpt_decision; | |
120 | ||
862be747 | 121 | /* Average number of executed insns per iteration. */ |
122 | unsigned av_ninsns; | |
123 | ||
862be747 | 124 | /* Number of blocks contained within the loop. */ |
125 | unsigned num_nodes; | |
126 | ||
9e3536f4 | 127 | /* Superloops of the loop, starting with the outermost loop. */ |
f1f41a6c | 128 | vec<loop_p, va_gc> *superloops; |
862be747 | 129 | |
130 | /* The first inner (child) loop or NULL if innermost loop. */ | |
131 | struct loop *inner; | |
132 | ||
133 | /* Link to the next (sibling) loop. */ | |
134 | struct loop *next; | |
135 | ||
862be747 | 136 | /* Auxiliary info specific to a pass. */ |
ccae4f9f | 137 | PTR GTY ((skip (""))) aux; |
862be747 | 138 | |
134c053e | 139 | /* The number of times the latch of the loop is executed. This can be an |
140 | INTEGER_CST, or a symbolic expression representing the number of | |
141 | iterations like "N - 1", or a COND_EXPR containing the runtime | |
142 | conditions under which the number of iterations is non zero. | |
143 | ||
144 | Don't access this field directly: number_of_latch_executions | |
145 | computes and caches the computed information in this field. */ | |
c2c3fd24 | 146 | tree nb_iterations; |
147 | ||
8fe79ba5 | 148 | /* An integer guaranteed to be greater or equal to nb_iterations. Only |
149 | valid if any_upper_bound is true. */ | |
4b4ab846 | 150 | double_int nb_iterations_upper_bound; |
151 | ||
8fe79ba5 | 152 | /* An integer giving an estimate on nb_iterations. Unlike |
153 | nb_iterations_upper_bound, there is no guarantee that it is at least | |
154 | nb_iterations. */ | |
4b4ab846 | 155 | double_int nb_iterations_estimate; |
bc3c8ad4 | 156 | |
0ac758f7 | 157 | bool any_upper_bound; |
158 | bool any_estimate; | |
159 | ||
609c710b | 160 | /* True if the loop can be parallel. */ |
161 | bool can_be_parallel; | |
162 | ||
0ac758f7 | 163 | /* An integer estimation of the number of iterations. Estimate_state |
164 | describes what is the state of the estimation. */ | |
165 | enum loop_estimation estimate_state; | |
166 | ||
b9d73ea6 | 167 | /* Upper bound on number of iterations of a loop. */ |
168 | struct nb_iter_bound *bounds; | |
bb445479 | 169 | |
dce58e66 | 170 | /* Head of the cyclic list of the exits of the loop. */ |
ccae4f9f | 171 | struct loop_exit *exits; |
862be747 | 172 | }; |
173 | ||
174 | /* Flags for state of loop structure. */ | |
175 | enum | |
176 | { | |
177 | LOOPS_HAVE_PREHEADERS = 1, | |
178 | LOOPS_HAVE_SIMPLE_LATCHES = 2, | |
bb445479 | 179 | LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS = 4, |
4a6f9e19 | 180 | LOOPS_HAVE_RECORDED_EXITS = 8, |
d8a0d6b8 | 181 | LOOPS_MAY_HAVE_MULTIPLE_LATCHES = 16, |
eb2a640e | 182 | LOOP_CLOSED_SSA = 32, |
e1ab7874 | 183 | LOOPS_NEED_FIXUP = 64, |
184 | LOOPS_HAVE_FALLTHRU_PREHEADERS = 128 | |
862be747 | 185 | }; |
186 | ||
c8d055f6 | 187 | #define LOOPS_NORMAL (LOOPS_HAVE_PREHEADERS | LOOPS_HAVE_SIMPLE_LATCHES \ |
188 | | LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS) | |
4a6f9e19 | 189 | #define AVOID_CFG_MODIFICATIONS (LOOPS_MAY_HAVE_MULTIPLE_LATCHES) |
c8d055f6 | 190 | |
862be747 | 191 | /* Structure to hold CFG information about natural loops within a function. */ |
fb1e4f4a | 192 | struct GTY (()) loops { |
677144a0 | 193 | /* State of loops. */ |
194 | int state; | |
862be747 | 195 | |
17519ba0 | 196 | /* Array of the loops. */ |
f1f41a6c | 197 | vec<loop_p, va_gc> *larray; |
862be747 | 198 | |
dce58e66 | 199 | /* Maps edges to the list of their descriptions as loop exits. Edges |
200 | whose sources or destinations have loop_father == NULL (which may | |
201 | happen during the cfg manipulations) should not appear in EXITS. */ | |
ccae4f9f | 202 | htab_t GTY((param_is (struct loop_exit))) exits; |
dce58e66 | 203 | |
862be747 | 204 | /* Pointer to root of loop hierarchy tree. */ |
205 | struct loop *tree_root; | |
862be747 | 206 | }; |
207 | ||
862be747 | 208 | /* Loop recognition. */ |
ffc6b5d5 | 209 | extern int flow_loops_find (struct loops *); |
4a6f9e19 | 210 | extern void disambiguate_loops_with_multiple_latches (void); |
4c9e08a4 | 211 | extern void flow_loops_free (struct loops *); |
7194de72 | 212 | extern void flow_loops_dump (FILE *, |
4c9e08a4 | 213 | void (*)(const struct loop *, FILE *, int), int); |
214 | extern void flow_loop_dump (const struct loop *, FILE *, | |
215 | void (*)(const struct loop *, FILE *, int), int); | |
dce58e66 | 216 | struct loop *alloc_loop (void); |
4c9e08a4 | 217 | extern void flow_loop_free (struct loop *); |
053fdd99 | 218 | int flow_loop_nodes_find (basic_block, struct loop *); |
7194de72 | 219 | void fix_loop_structure (bitmap changed_bbs); |
c9263b6a | 220 | bool mark_irreducible_loops (void); |
dce58e66 | 221 | void release_recorded_exits (void); |
222 | void record_loop_exits (void); | |
223 | void rescan_loop_exit (edge, bool, bool); | |
862be747 | 224 | |
917bbcab | 225 | /* Loop data structure manipulation/querying. */ |
4c9e08a4 | 226 | extern void flow_loop_tree_node_add (struct loop *, struct loop *); |
227 | extern void flow_loop_tree_node_remove (struct loop *); | |
4a6f9e19 | 228 | extern void add_loop (struct loop *, struct loop *); |
4c9e08a4 | 229 | extern bool flow_loop_nested_p (const struct loop *, const struct loop *); |
7ecb5bb2 | 230 | extern bool flow_bb_inside_loop_p (const struct loop *, const_basic_block); |
4c9e08a4 | 231 | extern struct loop * find_common_loop (struct loop *, struct loop *); |
7d23383d | 232 | struct loop *superloop_at_depth (struct loop *, unsigned); |
bc8bb825 | 233 | struct eni_weights_d; |
234 | extern unsigned tree_num_loop_insns (struct loop *, struct eni_weights_d *); | |
7ecb5bb2 | 235 | extern int num_loop_insns (const struct loop *); |
236 | extern int average_num_loop_insns (const struct loop *); | |
2d49f824 | 237 | extern unsigned get_loop_level (const struct loop *); |
7ecb5bb2 | 238 | extern bool loop_exit_edge_p (const struct loop *, const_edge); |
259c0e44 | 239 | extern bool loop_exits_to_bb_p (struct loop *, basic_block); |
240 | extern bool loop_exits_from_bb_p (struct loop *, basic_block); | |
7194de72 | 241 | extern void mark_loop_exit_edges (void); |
862be747 | 242 | |
243 | /* Loops & cfg manipulation. */ | |
4c9e08a4 | 244 | extern basic_block *get_loop_body (const struct loop *); |
4a6f9e19 | 245 | extern unsigned get_loop_body_with_size (const struct loop *, basic_block *, |
246 | unsigned); | |
f9cce2dc | 247 | extern basic_block *get_loop_body_in_dom_order (const struct loop *); |
07c03fb0 | 248 | extern basic_block *get_loop_body_in_bfs_order (const struct loop *); |
48e1416a | 249 | extern basic_block *get_loop_body_in_custom_order (const struct loop *, |
e1ab7874 | 250 | int (*) (const void *, const void *)); |
251 | ||
f1f41a6c | 252 | extern vec<edge> get_loop_exit_edges (const struct loop *); |
97fc8ad6 | 253 | extern edge single_exit (const struct loop *); |
254 | extern edge single_likely_exit (struct loop *loop); | |
f9cce2dc | 255 | extern unsigned num_loop_branches (const struct loop *); |
862be747 | 256 | |
4c9e08a4 | 257 | extern edge loop_preheader_edge (const struct loop *); |
258 | extern edge loop_latch_edge (const struct loop *); | |
862be747 | 259 | |
4c9e08a4 | 260 | extern void add_bb_to_loop (basic_block, struct loop *); |
261 | extern void remove_bb_from_loops (basic_block); | |
862be747 | 262 | |
7194de72 | 263 | extern void cancel_loop_tree (struct loop *); |
17519ba0 | 264 | extern void delete_loop (struct loop *); |
862be747 | 265 | |
862be747 | 266 | enum |
267 | { | |
e1ab7874 | 268 | CP_SIMPLE_PREHEADERS = 1, |
269 | CP_FALLTHRU_PREHEADERS = 2 | |
862be747 | 270 | }; |
271 | ||
7e0311ae | 272 | basic_block create_preheader (struct loop *, int); |
7194de72 | 273 | extern void create_preheaders (int); |
274 | extern void force_single_succ_latches (void); | |
862be747 | 275 | |
7194de72 | 276 | extern void verify_loop_structure (void); |
862be747 | 277 | |
278 | /* Loop analysis. */ | |
7ecb5bb2 | 279 | extern bool just_once_each_iteration_p (const struct loop *, const_basic_block); |
d97e22fb | 280 | gcov_type expected_loop_iterations_unbounded (const struct loop *); |
4c9e08a4 | 281 | extern unsigned expected_loop_iterations (const struct loop *); |
fc2abfd3 | 282 | extern rtx doloop_condition_get (rtx); |
6a606e3c | 283 | |
7f521ab4 | 284 | void estimate_numbers_of_iterations_loop (struct loop *); |
b6556916 | 285 | void record_niter_bound (struct loop *, double_int, bool, bool); |
fee017b3 | 286 | bool estimated_loop_iterations (struct loop *, double_int *); |
287 | bool max_loop_iterations (struct loop *, double_int *); | |
288 | HOST_WIDE_INT estimated_loop_iterations_int (struct loop *); | |
289 | HOST_WIDE_INT max_loop_iterations_int (struct loop *); | |
290 | bool max_stmt_executions (struct loop *, double_int *); | |
291 | bool estimated_stmt_executions (struct loop *, double_int *); | |
292 | HOST_WIDE_INT max_stmt_executions_int (struct loop *); | |
293 | HOST_WIDE_INT estimated_stmt_executions_int (struct loop *); | |
d500fef3 | 294 | |
6a606e3c | 295 | /* Loop manipulation. */ |
7ecb5bb2 | 296 | extern bool can_duplicate_loop_p (const struct loop *loop); |
6a606e3c | 297 | |
298 | #define DLTHE_FLAG_UPDATE_FREQ 1 /* Update frequencies in | |
299 | duplicate_loop_to_header_edge. */ | |
75cc36a4 | 300 | #define DLTHE_RECORD_COPY_NUMBER 2 /* Record copy number in the aux |
301 | field of newly create BB. */ | |
fb54ef7c | 302 | #define DLTHE_FLAG_COMPLETTE_PEEL 4 /* Update frequencies expecting |
3ce7ff97 | 303 | a complete peeling. */ |
6a606e3c | 304 | |
255b6be7 | 305 | extern edge create_empty_if_region_on_edge (edge, tree); |
306 | extern struct loop *create_empty_loop_on_edge (edge, tree, tree, tree, tree, | |
b65ec27f | 307 | tree *, tree *, struct loop *); |
7194de72 | 308 | extern struct loop * duplicate_loop (struct loop *, struct loop *); |
efa7c5bd | 309 | extern void copy_loop_info (struct loop *loop, struct loop *target); |
b0fb253a | 310 | extern void duplicate_subloops (struct loop *, struct loop *); |
48e1416a | 311 | extern bool duplicate_loop_to_header_edge (struct loop *, edge, |
f3c40e6d | 312 | unsigned, sbitmap, edge, |
f1f41a6c | 313 | vec<edge> *, int); |
7194de72 | 314 | extern struct loop *loopify (edge, edge, |
7cef6c97 | 315 | basic_block, edge, edge, bool, |
316 | unsigned, unsigned); | |
7194de72 | 317 | struct loop * loop_version (struct loop *, void *, |
7cef6c97 | 318 | basic_block *, unsigned, unsigned, unsigned, bool); |
7194de72 | 319 | extern bool remove_path (edge); |
9f0ac045 | 320 | extern void unloop (struct loop *, bool *, bitmap); |
c790d986 | 321 | extern void scale_loop_frequencies (struct loop *, int, int); |
6a606e3c | 322 | |
f9cce2dc | 323 | /* Induction variable analysis. */ |
324 | ||
325 | /* The description of induction variable. The things are a bit complicated | |
326 | due to need to handle subregs and extends. The value of the object described | |
327 | by it can be obtained as follows (all computations are done in extend_mode): | |
328 | ||
329 | Value in i-th iteration is | |
330 | delta + mult * extend_{extend_mode} (subreg_{mode} (base + i * step)). | |
331 | ||
332 | If first_special is true, the value in the first iteration is | |
333 | delta + mult * base | |
a0c938f0 | 334 | |
21f1e711 | 335 | If extend = UNKNOWN, first_special must be false, delta 0, mult 1 and value is |
f9cce2dc | 336 | subreg_{mode} (base + i * step) |
337 | ||
338 | The get_iv_value function can be used to obtain these expressions. | |
339 | ||
340 | ??? Add a third mode field that would specify the mode in that inner | |
341 | computation is done, which would enable it to be different from the | |
342 | outer one? */ | |
343 | ||
344 | struct rtx_iv | |
345 | { | |
346 | /* Its base and step (mode of base and step is supposed to be extend_mode, | |
347 | see the description above). */ | |
348 | rtx base, step; | |
349 | ||
aedb7bf8 | 350 | /* The type of extend applied to it (IV_SIGN_EXTEND, IV_ZERO_EXTEND, |
351 | or IV_UNKNOWN_EXTEND). */ | |
352 | enum iv_extend_code extend; | |
f9cce2dc | 353 | |
354 | /* Operations applied in the extended mode. */ | |
355 | rtx delta, mult; | |
356 | ||
357 | /* The mode it is extended to. */ | |
358 | enum machine_mode extend_mode; | |
359 | ||
360 | /* The mode the variable iterates in. */ | |
361 | enum machine_mode mode; | |
362 | ||
f9cce2dc | 363 | /* Whether the first iteration needs to be handled specially. */ |
364 | unsigned first_special : 1; | |
365 | }; | |
366 | ||
9c1ccc0f | 367 | /* The description of an exit from the loop and of the number of iterations |
368 | till we take the exit. */ | |
f9cce2dc | 369 | |
370 | struct niter_desc | |
371 | { | |
372 | /* The edge out of the loop. */ | |
373 | edge out_edge; | |
374 | ||
375 | /* The other edge leading from the condition. */ | |
376 | edge in_edge; | |
377 | ||
378 | /* True if we are able to say anything about number of iterations of the | |
379 | loop. */ | |
380 | bool simple_p; | |
381 | ||
382 | /* True if the loop iterates the constant number of times. */ | |
383 | bool const_iter; | |
384 | ||
385 | /* Number of iterations if constant. */ | |
386 | unsigned HOST_WIDEST_INT niter; | |
387 | ||
f9cce2dc | 388 | /* Assumptions under that the rest of the information is valid. */ |
389 | rtx assumptions; | |
390 | ||
391 | /* Assumptions under that the loop ends before reaching the latch, | |
392 | even if value of niter_expr says otherwise. */ | |
393 | rtx noloop_assumptions; | |
394 | ||
395 | /* Condition under that the loop is infinite. */ | |
396 | rtx infinite; | |
397 | ||
398 | /* Whether the comparison is signed. */ | |
399 | bool signed_p; | |
400 | ||
401 | /* The mode in that niter_expr should be computed. */ | |
402 | enum machine_mode mode; | |
403 | ||
404 | /* The number of iterations of the loop. */ | |
405 | rtx niter_expr; | |
406 | }; | |
407 | ||
408 | extern void iv_analysis_loop_init (struct loop *); | |
f08e8669 | 409 | extern bool iv_analyze (rtx, rtx, struct rtx_iv *); |
3f37d465 | 410 | extern bool iv_analyze_result (rtx, rtx, struct rtx_iv *); |
411 | extern bool iv_analyze_expr (rtx, rtx, enum machine_mode, struct rtx_iv *); | |
f9cce2dc | 412 | extern rtx get_iv_value (struct rtx_iv *, rtx); |
a9989fb4 | 413 | extern bool biv_p (rtx, rtx); |
f9cce2dc | 414 | extern void find_simple_exit (struct loop *, struct niter_desc *); |
f9cce2dc | 415 | extern void iv_analysis_done (void); |
416 | ||
417 | extern struct niter_desc *get_simple_loop_desc (struct loop *loop); | |
418 | extern void free_simple_loop_desc (struct loop *loop); | |
419 | ||
420 | static inline struct niter_desc * | |
421 | simple_loop_desc (struct loop *loop) | |
422 | { | |
a9c6c0e3 | 423 | return (struct niter_desc *) loop->aux; |
f9cce2dc | 424 | } |
425 | ||
17519ba0 | 426 | /* Accessors for the loop structures. */ |
427 | ||
428 | /* Returns the loop with index NUM from current_loops. */ | |
429 | ||
430 | static inline struct loop * | |
431 | get_loop (unsigned num) | |
432 | { | |
f1f41a6c | 433 | return (*current_loops->larray)[num]; |
17519ba0 | 434 | } |
435 | ||
9e3536f4 | 436 | /* Returns the number of superloops of LOOP. */ |
437 | ||
438 | static inline unsigned | |
439 | loop_depth (const struct loop *loop) | |
440 | { | |
f1f41a6c | 441 | return vec_safe_length (loop->superloops); |
9e3536f4 | 442 | } |
443 | ||
6b42039a | 444 | /* Returns the loop depth of the loop BB belongs to. */ |
445 | ||
446 | static inline int | |
447 | bb_loop_depth (const_basic_block bb) | |
448 | { | |
449 | return bb->loop_father ? loop_depth (bb->loop_father) : 0; | |
450 | } | |
451 | ||
9e3536f4 | 452 | /* Returns the immediate superloop of LOOP, or NULL if LOOP is the outermost |
453 | loop. */ | |
454 | ||
455 | static inline struct loop * | |
456 | loop_outer (const struct loop *loop) | |
457 | { | |
f1f41a6c | 458 | unsigned n = vec_safe_length (loop->superloops); |
9e3536f4 | 459 | |
460 | if (n == 0) | |
461 | return NULL; | |
462 | ||
f1f41a6c | 463 | return (*loop->superloops)[n - 1]; |
9e3536f4 | 464 | } |
465 | ||
a8d6ade3 | 466 | /* Returns true if LOOP has at least one exit edge. */ |
467 | ||
468 | static inline bool | |
469 | loop_has_exit_edges (const struct loop *loop) | |
470 | { | |
471 | return loop->exits->next->e != NULL; | |
472 | } | |
473 | ||
17519ba0 | 474 | /* Returns the list of loops in current_loops. */ |
475 | ||
f1f41a6c | 476 | static inline vec<loop_p, va_gc> * |
17519ba0 | 477 | get_loops (void) |
478 | { | |
479 | if (!current_loops) | |
480 | return NULL; | |
481 | ||
482 | return current_loops->larray; | |
483 | } | |
484 | ||
485 | /* Returns the number of loops in current_loops (including the removed | |
486 | ones and the fake loop that forms the root of the loop tree). */ | |
487 | ||
488 | static inline unsigned | |
489 | number_of_loops (void) | |
490 | { | |
491 | if (!current_loops) | |
492 | return 0; | |
493 | ||
f1f41a6c | 494 | return vec_safe_length (current_loops->larray); |
17519ba0 | 495 | } |
496 | ||
f24ec26f | 497 | /* Returns true if state of the loops satisfies all properties |
498 | described by FLAGS. */ | |
499 | ||
500 | static inline bool | |
501 | loops_state_satisfies_p (unsigned flags) | |
502 | { | |
503 | return (current_loops->state & flags) == flags; | |
504 | } | |
505 | ||
506 | /* Sets FLAGS to the loops state. */ | |
507 | ||
508 | static inline void | |
509 | loops_state_set (unsigned flags) | |
510 | { | |
511 | current_loops->state |= flags; | |
512 | } | |
513 | ||
514 | /* Clears FLAGS from the loops state. */ | |
515 | ||
516 | static inline void | |
517 | loops_state_clear (unsigned flags) | |
518 | { | |
519 | if (!current_loops) | |
520 | return; | |
521 | current_loops->state &= ~flags; | |
522 | } | |
523 | ||
17519ba0 | 524 | /* Loop iterators. */ |
525 | ||
526 | /* Flags for loop iteration. */ | |
527 | ||
528 | enum li_flags | |
529 | { | |
3bbbcdff | 530 | LI_INCLUDE_ROOT = 1, /* Include the fake root of the loop tree. */ |
531 | LI_FROM_INNERMOST = 2, /* Iterate over the loops in the reverse order, | |
532 | starting from innermost ones. */ | |
533 | LI_ONLY_INNERMOST = 4 /* Iterate only over innermost loops. */ | |
17519ba0 | 534 | }; |
535 | ||
536 | /* The iterator for loops. */ | |
537 | ||
538 | typedef struct | |
539 | { | |
3bbbcdff | 540 | /* The list of loops to visit. */ |
f1f41a6c | 541 | vec<int> to_visit; |
3bbbcdff | 542 | |
543 | /* The index of the actual loop. */ | |
544 | unsigned idx; | |
17519ba0 | 545 | } loop_iterator; |
546 | ||
547 | static inline void | |
3bbbcdff | 548 | fel_next (loop_iterator *li, loop_p *loop) |
17519ba0 | 549 | { |
3bbbcdff | 550 | int anum; |
551 | ||
f1f41a6c | 552 | while (li->to_visit.iterate (li->idx, &anum)) |
17519ba0 | 553 | { |
17519ba0 | 554 | li->idx++; |
3bbbcdff | 555 | *loop = get_loop (anum); |
556 | if (*loop) | |
557 | return; | |
17519ba0 | 558 | } |
559 | ||
f1f41a6c | 560 | li->to_visit.release (); |
17519ba0 | 561 | *loop = NULL; |
562 | } | |
563 | ||
564 | static inline void | |
565 | fel_init (loop_iterator *li, loop_p *loop, unsigned flags) | |
566 | { | |
3bbbcdff | 567 | struct loop *aloop; |
568 | unsigned i; | |
569 | int mn; | |
570 | ||
571 | li->idx = 0; | |
17519ba0 | 572 | if (!current_loops) |
573 | { | |
f1f41a6c | 574 | li->to_visit.create (0); |
17519ba0 | 575 | *loop = NULL; |
576 | return; | |
577 | } | |
578 | ||
f1f41a6c | 579 | li->to_visit.create (number_of_loops ()); |
3bbbcdff | 580 | mn = (flags & LI_INCLUDE_ROOT) ? 0 : 1; |
581 | ||
582 | if (flags & LI_ONLY_INNERMOST) | |
583 | { | |
f1f41a6c | 584 | for (i = 0; vec_safe_iterate (current_loops->larray, i, &aloop); i++) |
3bbbcdff | 585 | if (aloop != NULL |
586 | && aloop->inner == NULL | |
587 | && aloop->num >= mn) | |
f1f41a6c | 588 | li->to_visit.quick_push (aloop->num); |
3bbbcdff | 589 | } |
590 | else if (flags & LI_FROM_INNERMOST) | |
17519ba0 | 591 | { |
3bbbcdff | 592 | /* Push the loops to LI->TO_VISIT in postorder. */ |
593 | for (aloop = current_loops->tree_root; | |
594 | aloop->inner != NULL; | |
595 | aloop = aloop->inner) | |
596 | continue; | |
597 | ||
598 | while (1) | |
599 | { | |
600 | if (aloop->num >= mn) | |
f1f41a6c | 601 | li->to_visit.quick_push (aloop->num); |
3bbbcdff | 602 | |
603 | if (aloop->next) | |
604 | { | |
605 | for (aloop = aloop->next; | |
606 | aloop->inner != NULL; | |
607 | aloop = aloop->inner) | |
608 | continue; | |
609 | } | |
9e3536f4 | 610 | else if (!loop_outer (aloop)) |
3bbbcdff | 611 | break; |
612 | else | |
9e3536f4 | 613 | aloop = loop_outer (aloop); |
3bbbcdff | 614 | } |
17519ba0 | 615 | } |
616 | else | |
617 | { | |
3bbbcdff | 618 | /* Push the loops to LI->TO_VISIT in preorder. */ |
619 | aloop = current_loops->tree_root; | |
620 | while (1) | |
621 | { | |
622 | if (aloop->num >= mn) | |
f1f41a6c | 623 | li->to_visit.quick_push (aloop->num); |
3bbbcdff | 624 | |
625 | if (aloop->inner != NULL) | |
626 | aloop = aloop->inner; | |
627 | else | |
628 | { | |
629 | while (aloop != NULL && aloop->next == NULL) | |
9e3536f4 | 630 | aloop = loop_outer (aloop); |
3bbbcdff | 631 | if (aloop == NULL) |
632 | break; | |
633 | aloop = aloop->next; | |
634 | } | |
635 | } | |
17519ba0 | 636 | } |
3bbbcdff | 637 | |
638 | fel_next (li, loop); | |
17519ba0 | 639 | } |
640 | ||
641 | #define FOR_EACH_LOOP(LI, LOOP, FLAGS) \ | |
642 | for (fel_init (&(LI), &(LOOP), FLAGS); \ | |
643 | (LOOP); \ | |
3bbbcdff | 644 | fel_next (&(LI), &(LOOP))) |
645 | ||
646 | #define FOR_EACH_LOOP_BREAK(LI) \ | |
647 | { \ | |
f1f41a6c | 648 | (LI).to_visit.release (); \ |
3bbbcdff | 649 | break; \ |
650 | } | |
17519ba0 | 651 | |
dec41e98 | 652 | /* The properties of the target. */ |
e8aa5a28 | 653 | struct target_cfgloop { |
654 | /* Number of available registers. */ | |
655 | unsigned x_target_avail_regs; | |
dec41e98 | 656 | |
e8aa5a28 | 657 | /* Number of available registers that are call-clobbered. */ |
658 | unsigned x_target_clobbered_regs; | |
659 | ||
660 | /* Number of registers reserved for temporary expressions. */ | |
661 | unsigned x_target_res_regs; | |
662 | ||
663 | /* The cost for register when there still is some reserve, but we are | |
664 | approaching the number of available registers. */ | |
665 | unsigned x_target_reg_cost[2]; | |
666 | ||
667 | /* The cost for register when we need to spill. */ | |
668 | unsigned x_target_spill_cost[2]; | |
669 | }; | |
670 | ||
671 | extern struct target_cfgloop default_target_cfgloop; | |
672 | #if SWITCHABLE_TARGET | |
673 | extern struct target_cfgloop *this_target_cfgloop; | |
674 | #else | |
675 | #define this_target_cfgloop (&default_target_cfgloop) | |
676 | #endif | |
677 | ||
678 | #define target_avail_regs \ | |
679 | (this_target_cfgloop->x_target_avail_regs) | |
680 | #define target_clobbered_regs \ | |
681 | (this_target_cfgloop->x_target_clobbered_regs) | |
682 | #define target_res_regs \ | |
683 | (this_target_cfgloop->x_target_res_regs) | |
684 | #define target_reg_cost \ | |
685 | (this_target_cfgloop->x_target_reg_cost) | |
686 | #define target_spill_cost \ | |
687 | (this_target_cfgloop->x_target_spill_cost) | |
dec41e98 | 688 | |
3a0ecac2 | 689 | /* Register pressure estimation for induction variable optimizations & loop |
690 | invariant motion. */ | |
a6b74a67 | 691 | extern unsigned estimate_reg_pressure_cost (unsigned, unsigned, bool, bool); |
3a0ecac2 | 692 | extern void init_set_costs (void); |
693 | ||
6a606e3c | 694 | /* Loop optimizer initialization. */ |
88e6f696 | 695 | extern void loop_optimizer_init (unsigned); |
696 | extern void loop_optimizer_finalize (void); | |
6a606e3c | 697 | |
698 | /* Optimization passes. */ | |
7194de72 | 699 | extern void unswitch_loops (void); |
6a606e3c | 700 | |
ce32fe65 | 701 | enum |
702 | { | |
703 | UAP_PEEL = 1, /* Enables loop peeling. */ | |
88e6f696 | 704 | UAP_UNROLL = 2, /* Enables unrolling of loops if it seems profitable. */ |
705 | UAP_UNROLL_ALL = 4 /* Enables unrolling of all loops. */ | |
ce32fe65 | 706 | }; |
707 | ||
7194de72 | 708 | extern void unroll_and_peel_loops (int); |
709 | extern void doloop_optimize_loops (void); | |
710 | extern void move_loop_invariants (void); | |
df9b545b | 711 | extern bool finite_loop_p (struct loop *); |
877584e4 | 712 | extern void scale_loop_profile (struct loop *loop, int scale, int iteration_bound); |
f1f41a6c | 713 | extern vec<basic_block> get_loop_hot_path (const struct loop *loop); |
bcf3c70d | 714 | |
7b64e7e0 | 715 | /* Returns the outermost loop of the loop nest that contains LOOP.*/ |
716 | static inline struct loop * | |
717 | loop_outermost (struct loop *loop) | |
718 | { | |
f1f41a6c | 719 | unsigned n = vec_safe_length (loop->superloops); |
7b64e7e0 | 720 | |
721 | if (n <= 1) | |
722 | return loop; | |
723 | ||
f1f41a6c | 724 | return (*loop->superloops)[1]; |
7b64e7e0 | 725 | } |
726 | ||
727 | ||
bcf3c70d | 728 | #endif /* GCC_CFGLOOP_H */ |