]>
Commit | Line | Data |
---|---|---|
3d436d2a | 1 | /* Natural loop functions |
23a5b65a | 2 | Copyright (C) 1987-2014 Free Software Foundation, Inc. |
3d436d2a ZD |
3 | |
4 | This file is part of GCC. | |
5 | ||
6 | GCC is free software; you can redistribute it and/or modify it under | |
7 | the terms of the GNU General Public License as published by the Free | |
9dcd6f09 | 8 | Software Foundation; either version 3, or (at your option) any later |
3d436d2a ZD |
9 | version. |
10 | ||
11 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY | |
12 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
13 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
14 | for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
9dcd6f09 NC |
17 | along with GCC; see the file COPYING3. If not see |
18 | <http://www.gnu.org/licenses/>. */ | |
3d436d2a | 19 | |
59587b18 JQ |
20 | #ifndef GCC_CFGLOOP_H |
21 | #define GCC_CFGLOOP_H | |
22 | ||
df582833 | 23 | #include "double-int.h" |
807e902e | 24 | #include "wide-int.h" |
7a8cba34 SB |
25 | #include "bitmap.h" |
26 | #include "sbitmap.h" | |
83685514 AM |
27 | #include "hashtab.h" |
28 | #include "hash-set.h" | |
29 | #include "vec.h" | |
30 | #include "machmode.h" | |
31 | #include "tm.h" | |
32 | #include "hard-reg-set.h" | |
33 | #include "input.h" | |
4484a35a | 34 | #include "function.h" |
7a8cba34 | 35 | |
3d436d2a ZD |
36 | /* Structure to hold decision about unrolling/peeling. */ |
37 | enum lpt_dec | |
38 | { | |
39 | LPT_NONE, | |
3d436d2a ZD |
40 | LPT_UNROLL_CONSTANT, |
41 | LPT_UNROLL_RUNTIME, | |
42 | LPT_UNROLL_STUPID | |
43 | }; | |
44 | ||
d1b38208 | 45 | struct GTY (()) lpt_decision { |
3d436d2a ZD |
46 | enum lpt_dec decision; |
47 | unsigned times; | |
48 | }; | |
49 | ||
1c1ad7bb SB |
50 | /* The type of extend applied to an IV. */ |
51 | enum iv_extend_code | |
52 | { | |
53 | IV_SIGN_EXTEND, | |
54 | IV_ZERO_EXTEND, | |
55 | IV_UNKNOWN_EXTEND | |
56 | }; | |
57 | ||
86df10e3 SP |
58 | /* The structure describing a bound on number of iterations of a loop. */ |
59 | ||
d1b38208 | 60 | struct GTY ((chain_next ("%h.next"))) nb_iter_bound { |
946e1bc7 | 61 | /* The statement STMT is executed at most ... */ |
726a989a | 62 | gimple stmt; |
946e1bc7 ZD |
63 | |
64 | /* ... BOUND + 1 times (BOUND must be an unsigned constant). | |
65 | The + 1 is added for the following reasons: | |
66 | ||
67 | a) 0 would otherwise be unused, while we would need to care more about | |
68 | overflows (as MAX + 1 is sometimes produced as the estimate on number | |
69 | of executions of STMT). | |
70 | b) it is consistent with the result of number_of_iterations_exit. */ | |
807e902e | 71 | widest_int bound; |
946e1bc7 | 72 | |
b8698a0f | 73 | /* True if the statement will cause the loop to be leaved the (at most) |
946e1bc7 ZD |
74 | BOUND + 1-st time it is executed, that is, all the statements after it |
75 | are executed at most BOUND times. */ | |
76 | bool is_exit; | |
77 | ||
946e1bc7 | 78 | /* The next bound in the list. */ |
86df10e3 | 79 | struct nb_iter_bound *next; |
86df10e3 SP |
80 | }; |
81 | ||
6270df4c ZD |
82 | /* Description of the loop exit. */ |
83 | ||
2a22f99c | 84 | struct GTY ((for_user)) loop_exit { |
6270df4c | 85 | /* The exit edge. */ |
b8244d74 | 86 | edge e; |
6270df4c ZD |
87 | |
88 | /* Previous and next exit in the list of the exits of the loop. */ | |
89 | struct loop_exit *prev; | |
90 | struct loop_exit *next; | |
91 | ||
92 | /* Next element in the list of loops from that E exits. */ | |
93 | struct loop_exit *next_e; | |
94 | }; | |
95 | ||
2a22f99c TS |
96 | struct loop_exit_hasher : ggc_hasher<loop_exit *> |
97 | { | |
98 | typedef edge compare_type; | |
99 | ||
100 | static hashval_t hash (loop_exit *); | |
101 | static bool equal (loop_exit *, edge); | |
102 | static void remove (loop_exit *); | |
103 | }; | |
104 | ||
9ba025a2 | 105 | typedef struct loop *loop_p; |
9ba025a2 | 106 | |
ae50c0cb TN |
107 | /* An integer estimation of the number of iterations. Estimate_state |
108 | describes what is the state of the estimation. */ | |
109 | enum loop_estimation | |
110 | { | |
111 | /* Estimate was not computed yet. */ | |
112 | EST_NOT_COMPUTED, | |
113 | /* Estimate is ready. */ | |
dd366ec3 RB |
114 | EST_AVAILABLE, |
115 | EST_LAST | |
ae50c0cb TN |
116 | }; |
117 | ||
3d436d2a | 118 | /* Structure to hold information for each natural loop. */ |
d1b38208 | 119 | struct GTY ((chain_next ("%h.next"))) loop { |
3d436d2a ZD |
120 | /* Index into loops array. */ |
121 | int num; | |
122 | ||
8f5929e1 JJ |
123 | /* Number of loop insns. */ |
124 | unsigned ninsns; | |
125 | ||
3d436d2a | 126 | /* Basic block of loop header. */ |
b8244d74 | 127 | basic_block header; |
3d436d2a ZD |
128 | |
129 | /* Basic block of loop latch. */ | |
b8244d74 | 130 | basic_block latch; |
3d436d2a | 131 | |
3d436d2a ZD |
132 | /* For loop unrolling/peeling decision. */ |
133 | struct lpt_decision lpt_decision; | |
134 | ||
3d436d2a ZD |
135 | /* Average number of executed insns per iteration. */ |
136 | unsigned av_ninsns; | |
137 | ||
3d436d2a ZD |
138 | /* Number of blocks contained within the loop. */ |
139 | unsigned num_nodes; | |
140 | ||
9ba025a2 | 141 | /* Superloops of the loop, starting with the outermost loop. */ |
9771b263 | 142 | vec<loop_p, va_gc> *superloops; |
3d436d2a ZD |
143 | |
144 | /* The first inner (child) loop or NULL if innermost loop. */ | |
145 | struct loop *inner; | |
146 | ||
147 | /* Link to the next (sibling) loop. */ | |
148 | struct loop *next; | |
149 | ||
3d436d2a | 150 | /* Auxiliary info specific to a pass. */ |
9e2f83a5 | 151 | PTR GTY ((skip (""))) aux; |
3d436d2a | 152 | |
0a74c758 SP |
153 | /* The number of times the latch of the loop is executed. This can be an |
154 | INTEGER_CST, or a symbolic expression representing the number of | |
155 | iterations like "N - 1", or a COND_EXPR containing the runtime | |
156 | conditions under which the number of iterations is non zero. | |
157 | ||
158 | Don't access this field directly: number_of_latch_executions | |
159 | computes and caches the computed information in this field. */ | |
9baba81b SP |
160 | tree nb_iterations; |
161 | ||
b4a9343c ZD |
162 | /* An integer guaranteed to be greater or equal to nb_iterations. Only |
163 | valid if any_upper_bound is true. */ | |
807e902e | 164 | widest_int nb_iterations_upper_bound; |
9bdb685e | 165 | |
b4a9343c ZD |
166 | /* An integer giving an estimate on nb_iterations. Unlike |
167 | nb_iterations_upper_bound, there is no guarantee that it is at least | |
168 | nb_iterations. */ | |
807e902e | 169 | widest_int nb_iterations_estimate; |
86df10e3 | 170 | |
8f5929e1 JJ |
171 | bool any_upper_bound; |
172 | bool any_estimate; | |
173 | ||
448f65db NF |
174 | /* True if the loop can be parallel. */ |
175 | bool can_be_parallel; | |
176 | ||
fbd28bc3 JJ |
177 | /* True if -Waggressive-loop-optimizations warned about this loop |
178 | already. */ | |
179 | bool warned_aggressive_loop_optimizations; | |
180 | ||
8f5929e1 JJ |
181 | /* An integer estimation of the number of iterations. Estimate_state |
182 | describes what is the state of the estimation. */ | |
183 | enum loop_estimation estimate_state; | |
184 | ||
74bf76ed JJ |
185 | /* If > 0, an integer, where the user asserted that for any |
186 | I in [ 0, nb_iterations ) and for any J in | |
187 | [ I, min ( I + safelen, nb_iterations ) ), the Ith and Jth iterations | |
188 | of the loop can be safely evaluated concurrently. */ | |
189 | int safelen; | |
190 | ||
5ce9450f JJ |
191 | /* True if this loop should never be vectorized. */ |
192 | bool dont_vectorize; | |
193 | ||
718c4601 EB |
194 | /* True if we should try harder to vectorize this loop. */ |
195 | bool force_vectorize; | |
196 | ||
74bf76ed JJ |
197 | /* For SIMD loops, this is a unique identifier of the loop, referenced |
198 | by IFN_GOMP_SIMD_VF, IFN_GOMP_SIMD_LANE and IFN_GOMP_SIMD_LAST_LANE | |
199 | builtins. */ | |
200 | tree simduid; | |
201 | ||
e9eb809d ZD |
202 | /* Upper bound on number of iterations of a loop. */ |
203 | struct nb_iter_bound *bounds; | |
82b85a85 | 204 | |
6270df4c | 205 | /* Head of the cyclic list of the exits of the loop. */ |
9e2f83a5 | 206 | struct loop_exit *exits; |
ef23e6a2 RB |
207 | |
208 | /* Number of iteration analysis data for RTL. */ | |
209 | struct niter_desc *simple_loop_desc; | |
e4ca2139 | 210 | |
e4ca2139 RB |
211 | /* For sanity checking during loop fixup we record here the former |
212 | loop header for loops marked for removal. Note that this prevents | |
213 | the basic-block from being collected but its index can still be | |
214 | reused. */ | |
215 | basic_block former_header; | |
3d436d2a ZD |
216 | }; |
217 | ||
218 | /* Flags for state of loop structure. */ | |
219 | enum | |
220 | { | |
221 | LOOPS_HAVE_PREHEADERS = 1, | |
222 | LOOPS_HAVE_SIMPLE_LATCHES = 2, | |
82b85a85 | 223 | LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS = 4, |
89f8f30f | 224 | LOOPS_HAVE_RECORDED_EXITS = 8, |
c7b852c8 | 225 | LOOPS_MAY_HAVE_MULTIPLE_LATCHES = 16, |
592c303d | 226 | LOOP_CLOSED_SSA = 32, |
e855c69d AB |
227 | LOOPS_NEED_FIXUP = 64, |
228 | LOOPS_HAVE_FALLTHRU_PREHEADERS = 128 | |
3d436d2a ZD |
229 | }; |
230 | ||
d78f3f78 ZD |
231 | #define LOOPS_NORMAL (LOOPS_HAVE_PREHEADERS | LOOPS_HAVE_SIMPLE_LATCHES \ |
232 | | LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS) | |
89f8f30f | 233 | #define AVOID_CFG_MODIFICATIONS (LOOPS_MAY_HAVE_MULTIPLE_LATCHES) |
d78f3f78 | 234 | |
3d436d2a | 235 | /* Structure to hold CFG information about natural loops within a function. */ |
d1b38208 | 236 | struct GTY (()) loops { |
0162f155 RG |
237 | /* State of loops. */ |
238 | int state; | |
3d436d2a | 239 | |
42fd6772 | 240 | /* Array of the loops. */ |
9771b263 | 241 | vec<loop_p, va_gc> *larray; |
3d436d2a | 242 | |
6270df4c ZD |
243 | /* Maps edges to the list of their descriptions as loop exits. Edges |
244 | whose sources or destinations have loop_father == NULL (which may | |
245 | happen during the cfg manipulations) should not appear in EXITS. */ | |
2a22f99c | 246 | hash_table<loop_exit_hasher> *GTY(()) exits; |
6270df4c | 247 | |
3d436d2a ZD |
248 | /* Pointer to root of loop hierarchy tree. */ |
249 | struct loop *tree_root; | |
3d436d2a ZD |
250 | }; |
251 | ||
3d436d2a | 252 | /* Loop recognition. */ |
0375167b | 253 | bool bb_loop_header_p (basic_block); |
dd366ec3 | 254 | void init_loops_structure (struct function *, struct loops *, unsigned); |
0375167b | 255 | extern struct loops *flow_loops_find (struct loops *); |
89f8f30f | 256 | extern void disambiguate_loops_with_multiple_latches (void); |
d329e058 | 257 | extern void flow_loops_free (struct loops *); |
d73be268 | 258 | extern void flow_loops_dump (FILE *, |
d329e058 AJ |
259 | void (*)(const struct loop *, FILE *, int), int); |
260 | extern void flow_loop_dump (const struct loop *, FILE *, | |
261 | void (*)(const struct loop *, FILE *, int), int); | |
6270df4c | 262 | struct loop *alloc_loop (void); |
d329e058 | 263 | extern void flow_loop_free (struct loop *); |
2b271002 | 264 | int flow_loop_nodes_find (basic_block, struct loop *); |
8e89b5b5 | 265 | unsigned fix_loop_structure (bitmap changed_bbs); |
2de58650 | 266 | bool mark_irreducible_loops (void); |
6270df4c ZD |
267 | void release_recorded_exits (void); |
268 | void record_loop_exits (void); | |
269 | void rescan_loop_exit (edge, bool, bool); | |
3d436d2a | 270 | |
4d6922ee | 271 | /* Loop data structure manipulation/querying. */ |
d329e058 AJ |
272 | extern void flow_loop_tree_node_add (struct loop *, struct loop *); |
273 | extern void flow_loop_tree_node_remove (struct loop *); | |
0fc822d0 | 274 | extern void place_new_loop (struct function *, struct loop *); |
89f8f30f | 275 | extern void add_loop (struct loop *, struct loop *); |
d329e058 | 276 | extern bool flow_loop_nested_p (const struct loop *, const struct loop *); |
ed7a4b4b | 277 | extern bool flow_bb_inside_loop_p (const struct loop *, const_basic_block); |
d329e058 | 278 | extern struct loop * find_common_loop (struct loop *, struct loop *); |
a7e5372d | 279 | struct loop *superloop_at_depth (struct loop *, unsigned); |
7f9bc51b | 280 | struct eni_weights_d; |
ed7a4b4b KG |
281 | extern int num_loop_insns (const struct loop *); |
282 | extern int average_num_loop_insns (const struct loop *); | |
689ba89d | 283 | extern unsigned get_loop_level (const struct loop *); |
ed7a4b4b | 284 | extern bool loop_exit_edge_p (const struct loop *, const_edge); |
f4ce375d VK |
285 | extern bool loop_exits_to_bb_p (struct loop *, basic_block); |
286 | extern bool loop_exits_from_bb_p (struct loop *, basic_block); | |
d73be268 | 287 | extern void mark_loop_exit_edges (void); |
e25a6711 | 288 | extern location_t get_loop_location (struct loop *loop); |
3d436d2a ZD |
289 | |
290 | /* Loops & cfg manipulation. */ | |
d329e058 | 291 | extern basic_block *get_loop_body (const struct loop *); |
89f8f30f ZD |
292 | extern unsigned get_loop_body_with_size (const struct loop *, basic_block *, |
293 | unsigned); | |
50654f6c | 294 | extern basic_block *get_loop_body_in_dom_order (const struct loop *); |
40923b20 | 295 | extern basic_block *get_loop_body_in_bfs_order (const struct loop *); |
b8698a0f | 296 | extern basic_block *get_loop_body_in_custom_order (const struct loop *, |
e855c69d AB |
297 | int (*) (const void *, const void *)); |
298 | ||
9771b263 | 299 | extern vec<edge> get_loop_exit_edges (const struct loop *); |
6d96bad2 JH |
300 | extern edge single_exit (const struct loop *); |
301 | extern edge single_likely_exit (struct loop *loop); | |
50654f6c | 302 | extern unsigned num_loop_branches (const struct loop *); |
3d436d2a | 303 | |
d329e058 AJ |
304 | extern edge loop_preheader_edge (const struct loop *); |
305 | extern edge loop_latch_edge (const struct loop *); | |
3d436d2a | 306 | |
d329e058 AJ |
307 | extern void add_bb_to_loop (basic_block, struct loop *); |
308 | extern void remove_bb_from_loops (basic_block); | |
3d436d2a | 309 | |
d73be268 | 310 | extern void cancel_loop_tree (struct loop *); |
42fd6772 | 311 | extern void delete_loop (struct loop *); |
3d436d2a | 312 | |
3d436d2a ZD |
313 | enum |
314 | { | |
e855c69d AB |
315 | CP_SIMPLE_PREHEADERS = 1, |
316 | CP_FALLTHRU_PREHEADERS = 2 | |
3d436d2a ZD |
317 | }; |
318 | ||
b02b9b53 | 319 | basic_block create_preheader (struct loop *, int); |
d73be268 ZD |
320 | extern void create_preheaders (int); |
321 | extern void force_single_succ_latches (void); | |
3d436d2a | 322 | |
d73be268 | 323 | extern void verify_loop_structure (void); |
3d436d2a ZD |
324 | |
325 | /* Loop analysis. */ | |
ed7a4b4b | 326 | extern bool just_once_each_iteration_p (const struct loop *, const_basic_block); |
ac84e05e | 327 | gcov_type expected_loop_iterations_unbounded (const struct loop *); |
d329e058 | 328 | extern unsigned expected_loop_iterations (const struct loop *); |
75c70254 | 329 | extern rtx doloop_condition_get (rtx); |
617b465c | 330 | |
4839cb59 | 331 | |
617b465c | 332 | /* Loop manipulation. */ |
ed7a4b4b | 333 | extern bool can_duplicate_loop_p (const struct loop *loop); |
617b465c ZD |
334 | |
335 | #define DLTHE_FLAG_UPDATE_FREQ 1 /* Update frequencies in | |
336 | duplicate_loop_to_header_edge. */ | |
7f7b1718 JH |
337 | #define DLTHE_RECORD_COPY_NUMBER 2 /* Record copy number in the aux |
338 | field of newly create BB. */ | |
178df94f | 339 | #define DLTHE_FLAG_COMPLETTE_PEEL 4 /* Update frequencies expecting |
a4d05547 | 340 | a complete peeling. */ |
617b465c | 341 | |
f8bf9252 SP |
342 | extern edge create_empty_if_region_on_edge (edge, tree); |
343 | extern struct loop *create_empty_loop_on_edge (edge, tree, tree, tree, tree, | |
8e74b397 | 344 | tree *, tree *, struct loop *); |
d73be268 | 345 | extern struct loop * duplicate_loop (struct loop *, struct loop *); |
bf45c4c0 | 346 | extern void copy_loop_info (struct loop *loop, struct loop *target); |
48710229 | 347 | extern void duplicate_subloops (struct loop *, struct loop *); |
b8698a0f | 348 | extern bool duplicate_loop_to_header_edge (struct loop *, edge, |
ee8c1b05 | 349 | unsigned, sbitmap, edge, |
9771b263 | 350 | vec<edge> *, int); |
d73be268 | 351 | extern struct loop *loopify (edge, edge, |
03cb2019 ZD |
352 | basic_block, edge, edge, bool, |
353 | unsigned, unsigned); | |
d73be268 | 354 | struct loop * loop_version (struct loop *, void *, |
03cb2019 | 355 | basic_block *, unsigned, unsigned, unsigned, bool); |
d73be268 | 356 | extern bool remove_path (edge); |
1a7de201 | 357 | extern void unloop (struct loop *, bool *, bitmap); |
b7442c2f | 358 | extern void scale_loop_frequencies (struct loop *, int, int); |
08c13199 RB |
359 | void mark_loop_for_removal (loop_p); |
360 | ||
617b465c | 361 | |
50654f6c ZD |
362 | /* Induction variable analysis. */ |
363 | ||
364 | /* The description of induction variable. The things are a bit complicated | |
365 | due to need to handle subregs and extends. The value of the object described | |
366 | by it can be obtained as follows (all computations are done in extend_mode): | |
367 | ||
368 | Value in i-th iteration is | |
369 | delta + mult * extend_{extend_mode} (subreg_{mode} (base + i * step)). | |
370 | ||
371 | If first_special is true, the value in the first iteration is | |
372 | delta + mult * base | |
c22cacf3 | 373 | |
f822d252 | 374 | If extend = UNKNOWN, first_special must be false, delta 0, mult 1 and value is |
50654f6c ZD |
375 | subreg_{mode} (base + i * step) |
376 | ||
377 | The get_iv_value function can be used to obtain these expressions. | |
378 | ||
379 | ??? Add a third mode field that would specify the mode in that inner | |
380 | computation is done, which would enable it to be different from the | |
381 | outer one? */ | |
382 | ||
383 | struct rtx_iv | |
384 | { | |
385 | /* Its base and step (mode of base and step is supposed to be extend_mode, | |
386 | see the description above). */ | |
387 | rtx base, step; | |
388 | ||
1c1ad7bb SB |
389 | /* The type of extend applied to it (IV_SIGN_EXTEND, IV_ZERO_EXTEND, |
390 | or IV_UNKNOWN_EXTEND). */ | |
391 | enum iv_extend_code extend; | |
50654f6c ZD |
392 | |
393 | /* Operations applied in the extended mode. */ | |
394 | rtx delta, mult; | |
395 | ||
396 | /* The mode it is extended to. */ | |
397 | enum machine_mode extend_mode; | |
398 | ||
399 | /* The mode the variable iterates in. */ | |
400 | enum machine_mode mode; | |
401 | ||
50654f6c ZD |
402 | /* Whether the first iteration needs to be handled specially. */ |
403 | unsigned first_special : 1; | |
404 | }; | |
405 | ||
f2dca510 ZD |
406 | /* The description of an exit from the loop and of the number of iterations |
407 | till we take the exit. */ | |
50654f6c | 408 | |
ef23e6a2 | 409 | struct GTY(()) niter_desc |
50654f6c ZD |
410 | { |
411 | /* The edge out of the loop. */ | |
412 | edge out_edge; | |
413 | ||
414 | /* The other edge leading from the condition. */ | |
415 | edge in_edge; | |
416 | ||
417 | /* True if we are able to say anything about number of iterations of the | |
418 | loop. */ | |
419 | bool simple_p; | |
420 | ||
421 | /* True if the loop iterates the constant number of times. */ | |
422 | bool const_iter; | |
423 | ||
424 | /* Number of iterations if constant. */ | |
a9243bfc | 425 | uint64_t niter; |
50654f6c | 426 | |
50654f6c ZD |
427 | /* Assumptions under that the rest of the information is valid. */ |
428 | rtx assumptions; | |
429 | ||
430 | /* Assumptions under that the loop ends before reaching the latch, | |
431 | even if value of niter_expr says otherwise. */ | |
432 | rtx noloop_assumptions; | |
433 | ||
434 | /* Condition under that the loop is infinite. */ | |
435 | rtx infinite; | |
436 | ||
437 | /* Whether the comparison is signed. */ | |
438 | bool signed_p; | |
439 | ||
440 | /* The mode in that niter_expr should be computed. */ | |
441 | enum machine_mode mode; | |
442 | ||
443 | /* The number of iterations of the loop. */ | |
444 | rtx niter_expr; | |
445 | }; | |
446 | ||
447 | extern void iv_analysis_loop_init (struct loop *); | |
1b20d55a DM |
448 | extern bool iv_analyze (rtx_insn *, rtx, struct rtx_iv *); |
449 | extern bool iv_analyze_result (rtx_insn *, rtx, struct rtx_iv *); | |
450 | extern bool iv_analyze_expr (rtx_insn *, rtx, enum machine_mode, | |
451 | struct rtx_iv *); | |
50654f6c | 452 | extern rtx get_iv_value (struct rtx_iv *, rtx); |
1b20d55a | 453 | extern bool biv_p (rtx_insn *, rtx); |
50654f6c | 454 | extern void find_simple_exit (struct loop *, struct niter_desc *); |
50654f6c ZD |
455 | extern void iv_analysis_done (void); |
456 | ||
457 | extern struct niter_desc *get_simple_loop_desc (struct loop *loop); | |
458 | extern void free_simple_loop_desc (struct loop *loop); | |
459 | ||
460 | static inline struct niter_desc * | |
461 | simple_loop_desc (struct loop *loop) | |
462 | { | |
ef23e6a2 | 463 | return loop->simple_loop_desc; |
50654f6c ZD |
464 | } |
465 | ||
42fd6772 ZD |
466 | /* Accessors for the loop structures. */ |
467 | ||
0fc822d0 | 468 | /* Returns the loop with index NUM from FNs loop tree. */ |
42fd6772 ZD |
469 | |
470 | static inline struct loop * | |
0fc822d0 | 471 | get_loop (struct function *fn, unsigned num) |
42fd6772 | 472 | { |
0fc822d0 | 473 | return (*loops_for_fn (fn)->larray)[num]; |
42fd6772 ZD |
474 | } |
475 | ||
9ba025a2 ZD |
476 | /* Returns the number of superloops of LOOP. */ |
477 | ||
478 | static inline unsigned | |
479 | loop_depth (const struct loop *loop) | |
480 | { | |
9771b263 | 481 | return vec_safe_length (loop->superloops); |
9ba025a2 ZD |
482 | } |
483 | ||
484 | /* Returns the immediate superloop of LOOP, or NULL if LOOP is the outermost | |
485 | loop. */ | |
486 | ||
487 | static inline struct loop * | |
488 | loop_outer (const struct loop *loop) | |
489 | { | |
9771b263 | 490 | unsigned n = vec_safe_length (loop->superloops); |
9ba025a2 ZD |
491 | |
492 | if (n == 0) | |
493 | return NULL; | |
494 | ||
9771b263 | 495 | return (*loop->superloops)[n - 1]; |
9ba025a2 ZD |
496 | } |
497 | ||
07643d76 AM |
498 | /* Returns true if LOOP has at least one exit edge. */ |
499 | ||
500 | static inline bool | |
501 | loop_has_exit_edges (const struct loop *loop) | |
502 | { | |
503 | return loop->exits->next->e != NULL; | |
504 | } | |
505 | ||
0fc822d0 | 506 | /* Returns the list of loops in FN. */ |
42fd6772 | 507 | |
0fc822d0 RB |
508 | inline vec<loop_p, va_gc> * |
509 | get_loops (struct function *fn) | |
42fd6772 | 510 | { |
0fc822d0 RB |
511 | struct loops *loops = loops_for_fn (fn); |
512 | if (!loops) | |
42fd6772 ZD |
513 | return NULL; |
514 | ||
0fc822d0 | 515 | return loops->larray; |
42fd6772 ZD |
516 | } |
517 | ||
0fc822d0 | 518 | /* Returns the number of loops in FN (including the removed |
42fd6772 ZD |
519 | ones and the fake loop that forms the root of the loop tree). */ |
520 | ||
521 | static inline unsigned | |
0fc822d0 | 522 | number_of_loops (struct function *fn) |
42fd6772 | 523 | { |
0fc822d0 | 524 | struct loops *loops = loops_for_fn (fn); |
0d0e2af6 | 525 | if (!loops) |
42fd6772 ZD |
526 | return 0; |
527 | ||
0fc822d0 | 528 | return vec_safe_length (loops->larray); |
42fd6772 ZD |
529 | } |
530 | ||
f87000d0 ZD |
531 | /* Returns true if state of the loops satisfies all properties |
532 | described by FLAGS. */ | |
533 | ||
534 | static inline bool | |
535 | loops_state_satisfies_p (unsigned flags) | |
536 | { | |
537 | return (current_loops->state & flags) == flags; | |
538 | } | |
539 | ||
540 | /* Sets FLAGS to the loops state. */ | |
541 | ||
542 | static inline void | |
543 | loops_state_set (unsigned flags) | |
544 | { | |
545 | current_loops->state |= flags; | |
546 | } | |
547 | ||
548 | /* Clears FLAGS from the loops state. */ | |
549 | ||
550 | static inline void | |
551 | loops_state_clear (unsigned flags) | |
552 | { | |
553 | if (!current_loops) | |
554 | return; | |
555 | current_loops->state &= ~flags; | |
556 | } | |
557 | ||
42fd6772 ZD |
558 | /* Loop iterators. */ |
559 | ||
560 | /* Flags for loop iteration. */ | |
561 | ||
562 | enum li_flags | |
563 | { | |
677cc14d ZD |
564 | LI_INCLUDE_ROOT = 1, /* Include the fake root of the loop tree. */ |
565 | LI_FROM_INNERMOST = 2, /* Iterate over the loops in the reverse order, | |
566 | starting from innermost ones. */ | |
567 | LI_ONLY_INNERMOST = 4 /* Iterate only over innermost loops. */ | |
42fd6772 ZD |
568 | }; |
569 | ||
570 | /* The iterator for loops. */ | |
571 | ||
f0bd40b1 | 572 | struct loop_iterator |
42fd6772 | 573 | { |
f0bd40b1 RB |
574 | loop_iterator (loop_p *loop, unsigned flags); |
575 | ~loop_iterator (); | |
576 | ||
577 | inline loop_p next (); | |
578 | ||
677cc14d | 579 | /* The list of loops to visit. */ |
9771b263 | 580 | vec<int> to_visit; |
677cc14d ZD |
581 | |
582 | /* The index of the actual loop. */ | |
583 | unsigned idx; | |
f0bd40b1 | 584 | }; |
42fd6772 | 585 | |
f0bd40b1 RB |
586 | inline loop_p |
587 | loop_iterator::next () | |
42fd6772 | 588 | { |
677cc14d ZD |
589 | int anum; |
590 | ||
f0bd40b1 | 591 | while (this->to_visit.iterate (this->idx, &anum)) |
42fd6772 | 592 | { |
f0bd40b1 RB |
593 | this->idx++; |
594 | loop_p loop = get_loop (cfun, anum); | |
595 | if (loop) | |
596 | return loop; | |
42fd6772 ZD |
597 | } |
598 | ||
f0bd40b1 | 599 | return NULL; |
42fd6772 ZD |
600 | } |
601 | ||
f0bd40b1 RB |
602 | inline |
603 | loop_iterator::loop_iterator (loop_p *loop, unsigned flags) | |
42fd6772 | 604 | { |
677cc14d ZD |
605 | struct loop *aloop; |
606 | unsigned i; | |
607 | int mn; | |
608 | ||
f0bd40b1 | 609 | this->idx = 0; |
42fd6772 ZD |
610 | if (!current_loops) |
611 | { | |
f0bd40b1 | 612 | this->to_visit.create (0); |
42fd6772 ZD |
613 | *loop = NULL; |
614 | return; | |
615 | } | |
616 | ||
f0bd40b1 | 617 | this->to_visit.create (number_of_loops (cfun)); |
677cc14d ZD |
618 | mn = (flags & LI_INCLUDE_ROOT) ? 0 : 1; |
619 | ||
620 | if (flags & LI_ONLY_INNERMOST) | |
621 | { | |
9771b263 | 622 | for (i = 0; vec_safe_iterate (current_loops->larray, i, &aloop); i++) |
677cc14d ZD |
623 | if (aloop != NULL |
624 | && aloop->inner == NULL | |
625 | && aloop->num >= mn) | |
f0bd40b1 | 626 | this->to_visit.quick_push (aloop->num); |
677cc14d ZD |
627 | } |
628 | else if (flags & LI_FROM_INNERMOST) | |
42fd6772 | 629 | { |
677cc14d ZD |
630 | /* Push the loops to LI->TO_VISIT in postorder. */ |
631 | for (aloop = current_loops->tree_root; | |
632 | aloop->inner != NULL; | |
633 | aloop = aloop->inner) | |
634 | continue; | |
635 | ||
636 | while (1) | |
637 | { | |
638 | if (aloop->num >= mn) | |
f0bd40b1 | 639 | this->to_visit.quick_push (aloop->num); |
677cc14d ZD |
640 | |
641 | if (aloop->next) | |
642 | { | |
643 | for (aloop = aloop->next; | |
644 | aloop->inner != NULL; | |
645 | aloop = aloop->inner) | |
646 | continue; | |
647 | } | |
9ba025a2 | 648 | else if (!loop_outer (aloop)) |
677cc14d ZD |
649 | break; |
650 | else | |
9ba025a2 | 651 | aloop = loop_outer (aloop); |
677cc14d | 652 | } |
42fd6772 ZD |
653 | } |
654 | else | |
655 | { | |
677cc14d ZD |
656 | /* Push the loops to LI->TO_VISIT in preorder. */ |
657 | aloop = current_loops->tree_root; | |
658 | while (1) | |
659 | { | |
660 | if (aloop->num >= mn) | |
f0bd40b1 | 661 | this->to_visit.quick_push (aloop->num); |
677cc14d ZD |
662 | |
663 | if (aloop->inner != NULL) | |
664 | aloop = aloop->inner; | |
665 | else | |
666 | { | |
667 | while (aloop != NULL && aloop->next == NULL) | |
9ba025a2 | 668 | aloop = loop_outer (aloop); |
677cc14d ZD |
669 | if (aloop == NULL) |
670 | break; | |
671 | aloop = aloop->next; | |
672 | } | |
673 | } | |
42fd6772 | 674 | } |
677cc14d | 675 | |
f0bd40b1 | 676 | *loop = this->next (); |
42fd6772 ZD |
677 | } |
678 | ||
f0bd40b1 RB |
679 | inline |
680 | loop_iterator::~loop_iterator () | |
681 | { | |
682 | this->to_visit.release (); | |
683 | } | |
677cc14d | 684 | |
f0bd40b1 RB |
685 | #define FOR_EACH_LOOP(LOOP, FLAGS) \ |
686 | for (loop_iterator li(&(LOOP), FLAGS); \ | |
687 | (LOOP); \ | |
688 | (LOOP) = li.next ()) | |
42fd6772 | 689 | |
8b11a64c | 690 | /* The properties of the target. */ |
4391924a RS |
691 | struct target_cfgloop { |
692 | /* Number of available registers. */ | |
693 | unsigned x_target_avail_regs; | |
8b11a64c | 694 | |
4391924a RS |
695 | /* Number of available registers that are call-clobbered. */ |
696 | unsigned x_target_clobbered_regs; | |
697 | ||
698 | /* Number of registers reserved for temporary expressions. */ | |
699 | unsigned x_target_res_regs; | |
700 | ||
701 | /* The cost for register when there still is some reserve, but we are | |
702 | approaching the number of available registers. */ | |
703 | unsigned x_target_reg_cost[2]; | |
704 | ||
705 | /* The cost for register when we need to spill. */ | |
706 | unsigned x_target_spill_cost[2]; | |
707 | }; | |
708 | ||
709 | extern struct target_cfgloop default_target_cfgloop; | |
710 | #if SWITCHABLE_TARGET | |
711 | extern struct target_cfgloop *this_target_cfgloop; | |
712 | #else | |
713 | #define this_target_cfgloop (&default_target_cfgloop) | |
714 | #endif | |
715 | ||
716 | #define target_avail_regs \ | |
717 | (this_target_cfgloop->x_target_avail_regs) | |
718 | #define target_clobbered_regs \ | |
719 | (this_target_cfgloop->x_target_clobbered_regs) | |
720 | #define target_res_regs \ | |
721 | (this_target_cfgloop->x_target_res_regs) | |
722 | #define target_reg_cost \ | |
723 | (this_target_cfgloop->x_target_reg_cost) | |
724 | #define target_spill_cost \ | |
725 | (this_target_cfgloop->x_target_spill_cost) | |
8b11a64c | 726 | |
5e962776 ZD |
727 | /* Register pressure estimation for induction variable optimizations & loop |
728 | invariant motion. */ | |
bec922f0 | 729 | extern unsigned estimate_reg_pressure_cost (unsigned, unsigned, bool, bool); |
5e962776 ZD |
730 | extern void init_set_costs (void); |
731 | ||
617b465c | 732 | /* Loop optimizer initialization. */ |
598ec7bd ZD |
733 | extern void loop_optimizer_init (unsigned); |
734 | extern void loop_optimizer_finalize (void); | |
617b465c ZD |
735 | |
736 | /* Optimization passes. */ | |
b17d5d7c ZD |
737 | enum |
738 | { | |
f8934be7 JH |
739 | UAP_UNROLL = 1, /* Enables unrolling of loops if it seems profitable. */ |
740 | UAP_UNROLL_ALL = 2 /* Enables unrolling of all loops. */ | |
b17d5d7c ZD |
741 | }; |
742 | ||
d73be268 ZD |
743 | extern void doloop_optimize_loops (void); |
744 | extern void move_loop_invariants (void); | |
7770c9e9 | 745 | extern void scale_loop_profile (struct loop *loop, int scale, gcov_type iteration_bound); |
9771b263 | 746 | extern vec<basic_block> get_loop_hot_path (const struct loop *loop); |
59587b18 | 747 | |
a5497b12 VK |
748 | /* Returns the outermost loop of the loop nest that contains LOOP.*/ |
749 | static inline struct loop * | |
750 | loop_outermost (struct loop *loop) | |
751 | { | |
9771b263 | 752 | unsigned n = vec_safe_length (loop->superloops); |
a5497b12 VK |
753 | |
754 | if (n <= 1) | |
755 | return loop; | |
756 | ||
9771b263 | 757 | return (*loop->superloops)[1]; |
a5497b12 VK |
758 | } |
759 | ||
807e902e | 760 | extern void record_niter_bound (struct loop *, const widest_int &, bool, bool); |
1ef88893 AM |
761 | extern HOST_WIDE_INT get_estimated_loop_iterations_int (struct loop *); |
762 | extern HOST_WIDE_INT get_max_loop_iterations_int (struct loop *); | |
807e902e KZ |
763 | extern bool get_estimated_loop_iterations (struct loop *loop, widest_int *nit); |
764 | extern bool get_max_loop_iterations (struct loop *loop, widest_int *nit); | |
4484a35a | 765 | extern int bb_loop_depth (const_basic_block); |
a5497b12 | 766 | |
807e902e | 767 | /* Converts VAL to widest_int. */ |
71343877 | 768 | |
807e902e KZ |
769 | static inline widest_int |
770 | gcov_type_to_wide_int (gcov_type val) | |
71343877 | 771 | { |
807e902e | 772 | HOST_WIDE_INT a[2]; |
71343877 | 773 | |
807e902e | 774 | a[0] = (unsigned HOST_WIDE_INT) val; |
71343877 AM |
775 | /* If HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_WIDEST_INT, avoid shifting by |
776 | the size of type. */ | |
777 | val >>= HOST_BITS_PER_WIDE_INT - 1; | |
778 | val >>= 1; | |
807e902e | 779 | a[1] = (unsigned HOST_WIDE_INT) val; |
71343877 | 780 | |
807e902e | 781 | return widest_int::from_array (a, 2); |
71343877 | 782 | } |
59587b18 | 783 | #endif /* GCC_CFGLOOP_H */ |