]>
Commit | Line | Data |
---|---|---|
3d436d2a | 1 | /* Natural loop functions |
a945c346 | 2 | Copyright (C) 1987-2024 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 | ||
893479de | 23 | #include "cfgloopmanip.h" |
7a8cba34 | 24 | |
3d436d2a ZD |
25 | /* Structure to hold decision about unrolling/peeling. */ |
26 | enum lpt_dec | |
27 | { | |
28 | LPT_NONE, | |
3d436d2a ZD |
29 | LPT_UNROLL_CONSTANT, |
30 | LPT_UNROLL_RUNTIME, | |
31 | LPT_UNROLL_STUPID | |
32 | }; | |
33 | ||
d1b38208 | 34 | struct GTY (()) lpt_decision { |
3d436d2a ZD |
35 | enum lpt_dec decision; |
36 | unsigned times; | |
37 | }; | |
38 | ||
1c1ad7bb SB |
39 | /* The type of extend applied to an IV. */ |
40 | enum iv_extend_code | |
41 | { | |
42 | IV_SIGN_EXTEND, | |
43 | IV_ZERO_EXTEND, | |
44 | IV_UNKNOWN_EXTEND | |
45 | }; | |
46 | ||
0d00385e JJ |
47 | typedef generic_wide_int <fixed_wide_int_storage <WIDE_INT_MAX_INL_PRECISION> > |
48 | bound_wide_int; | |
49 | ||
86df10e3 SP |
50 | /* The structure describing a bound on number of iterations of a loop. */ |
51 | ||
6c1dae73 MS |
52 | class GTY ((chain_next ("%h.next"))) nb_iter_bound { |
53 | public: | |
946e1bc7 | 54 | /* The statement STMT is executed at most ... */ |
355fe088 | 55 | gimple *stmt; |
946e1bc7 ZD |
56 | |
57 | /* ... BOUND + 1 times (BOUND must be an unsigned constant). | |
58 | The + 1 is added for the following reasons: | |
59 | ||
60 | a) 0 would otherwise be unused, while we would need to care more about | |
61 | overflows (as MAX + 1 is sometimes produced as the estimate on number | |
62 | of executions of STMT). | |
63 | b) it is consistent with the result of number_of_iterations_exit. */ | |
0d00385e | 64 | bound_wide_int bound; |
946e1bc7 | 65 | |
2bc8c6ed AC |
66 | /* True if, after executing the statement BOUND + 1 times, we will |
67 | leave the loop; that is, all the statements after it are executed at most | |
68 | BOUND times. */ | |
946e1bc7 ZD |
69 | bool is_exit; |
70 | ||
946e1bc7 | 71 | /* The next bound in the list. */ |
99b1c316 | 72 | class nb_iter_bound *next; |
86df10e3 SP |
73 | }; |
74 | ||
6270df4c ZD |
75 | /* Description of the loop exit. */ |
76 | ||
2a22f99c | 77 | struct GTY ((for_user)) loop_exit { |
6270df4c | 78 | /* The exit edge. */ |
b8244d74 | 79 | edge e; |
6270df4c ZD |
80 | |
81 | /* Previous and next exit in the list of the exits of the loop. */ | |
82 | struct loop_exit *prev; | |
83 | struct loop_exit *next; | |
84 | ||
85 | /* Next element in the list of loops from that E exits. */ | |
86 | struct loop_exit *next_e; | |
87 | }; | |
88 | ||
ca752f39 | 89 | struct loop_exit_hasher : ggc_ptr_hash<loop_exit> |
2a22f99c TS |
90 | { |
91 | typedef edge compare_type; | |
92 | ||
93 | static hashval_t hash (loop_exit *); | |
94 | static bool equal (loop_exit *, edge); | |
95 | static void remove (loop_exit *); | |
96 | }; | |
97 | ||
99b1c316 | 98 | typedef class loop *loop_p; |
9ba025a2 | 99 | |
ae50c0cb TN |
100 | /* An integer estimation of the number of iterations. Estimate_state |
101 | describes what is the state of the estimation. */ | |
102 | enum loop_estimation | |
103 | { | |
104 | /* Estimate was not computed yet. */ | |
105 | EST_NOT_COMPUTED, | |
106 | /* Estimate is ready. */ | |
dd366ec3 RB |
107 | EST_AVAILABLE, |
108 | EST_LAST | |
ae50c0cb TN |
109 | }; |
110 | ||
2f07b722 BC |
111 | /* The structure describing non-overflow control induction variable for |
112 | loop's exit edge. */ | |
113 | struct GTY ((chain_next ("%h.next"))) control_iv { | |
114 | tree base; | |
115 | tree step; | |
116 | struct control_iv *next; | |
117 | }; | |
118 | ||
3d436d2a | 119 | /* Structure to hold information for each natural loop. */ |
6c1dae73 MS |
120 | class GTY ((chain_next ("%h.next"))) loop { |
121 | public: | |
542e7230 BC |
122 | /* Index into loops array. Note indices will never be reused after loop |
123 | is destroyed. */ | |
3d436d2a ZD |
124 | int num; |
125 | ||
8f5929e1 JJ |
126 | /* Number of loop insns. */ |
127 | unsigned ninsns; | |
128 | ||
3d436d2a | 129 | /* Basic block of loop header. */ |
b8244d74 | 130 | basic_block header; |
3d436d2a ZD |
131 | |
132 | /* Basic block of loop latch. */ | |
b8244d74 | 133 | basic_block latch; |
3d436d2a | 134 | |
3d436d2a ZD |
135 | /* For loop unrolling/peeling decision. */ |
136 | struct lpt_decision lpt_decision; | |
137 | ||
3d436d2a ZD |
138 | /* Average number of executed insns per iteration. */ |
139 | unsigned av_ninsns; | |
140 | ||
3d436d2a ZD |
141 | /* Number of blocks contained within the loop. */ |
142 | unsigned num_nodes; | |
143 | ||
9ba025a2 | 144 | /* Superloops of the loop, starting with the outermost loop. */ |
9771b263 | 145 | vec<loop_p, va_gc> *superloops; |
3d436d2a ZD |
146 | |
147 | /* The first inner (child) loop or NULL if innermost loop. */ | |
99b1c316 | 148 | class loop *inner; |
3d436d2a ZD |
149 | |
150 | /* Link to the next (sibling) loop. */ | |
99b1c316 | 151 | class loop *next; |
3d436d2a | 152 | |
3d436d2a | 153 | /* Auxiliary info specific to a pass. */ |
4b865081 | 154 | void *GTY ((skip (""))) aux; |
3d436d2a | 155 | |
0a74c758 SP |
156 | /* The number of times the latch of the loop is executed. This can be an |
157 | INTEGER_CST, or a symbolic expression representing the number of | |
158 | iterations like "N - 1", or a COND_EXPR containing the runtime | |
159 | conditions under which the number of iterations is non zero. | |
160 | ||
161 | Don't access this field directly: number_of_latch_executions | |
162 | computes and caches the computed information in this field. */ | |
9baba81b SP |
163 | tree nb_iterations; |
164 | ||
b4a9343c ZD |
165 | /* An integer guaranteed to be greater or equal to nb_iterations. Only |
166 | valid if any_upper_bound is true. */ | |
0d00385e | 167 | bound_wide_int nb_iterations_upper_bound; |
9bdb685e | 168 | |
0d00385e | 169 | bound_wide_int nb_iterations_likely_upper_bound; |
105e29c5 | 170 | |
b4a9343c ZD |
171 | /* An integer giving an estimate on nb_iterations. Unlike |
172 | nb_iterations_upper_bound, there is no guarantee that it is at least | |
173 | nb_iterations. */ | |
0d00385e | 174 | bound_wide_int nb_iterations_estimate; |
86df10e3 | 175 | |
74bf76ed JJ |
176 | /* If > 0, an integer, where the user asserted that for any |
177 | I in [ 0, nb_iterations ) and for any J in | |
178 | [ I, min ( I + safelen, nb_iterations ) ), the Ith and Jth iterations | |
179 | of the loop can be safely evaluated concurrently. */ | |
180 | int safelen; | |
181 | ||
f63445e5 JJ |
182 | /* Preferred vectorization factor for the loop if non-zero. */ |
183 | int simdlen; | |
184 | ||
18767ebc BC |
185 | /* Constraints are generally set by consumers and affect certain |
186 | semantics of niter analyzer APIs. Currently the APIs affected are | |
187 | number_of_iterations_exit* functions and their callers. One typical | |
188 | use case of constraints is to vectorize possibly infinite loop: | |
189 | ||
190 | 1) Compute niter->assumptions by calling niter analyzer API and | |
191 | record it as possible condition for loop versioning. | |
192 | 2) Clear buffered result of niter/scev analyzer. | |
193 | 3) Set constraint LOOP_C_FINITE assuming the loop is finite. | |
194 | 4) Analyze data references. Since data reference analysis depends | |
195 | on niter/scev analyzer, the point is that niter/scev analysis | |
196 | is done under circumstance of LOOP_C_FINITE constraint. | |
197 | 5) Version the loop with niter->assumptions computed in step 1). | |
198 | 6) Vectorize the versioned loop in which niter->assumptions is | |
199 | checked to be true. | |
200 | 7) Update constraints in versioned loops so that niter analyzer | |
201 | in following passes can use it. | |
202 | ||
203 | Note consumers are usually the loop optimizers and it is consumers' | |
204 | responsibility to set/clear constraints correctly. Failing to do | |
205 | that might result in hard to track down bugs in niter/scev consumers. */ | |
206 | unsigned constraints; | |
207 | ||
34e82342 RB |
208 | /* An integer estimation of the number of iterations. Estimate_state |
209 | describes what is the state of the estimation. */ | |
210 | ENUM_BITFIELD(loop_estimation) estimate_state : 8; | |
211 | ||
212 | unsigned any_upper_bound : 1; | |
213 | unsigned any_estimate : 1; | |
214 | unsigned any_likely_upper_bound : 1; | |
215 | ||
216 | /* True if the loop can be parallel. */ | |
217 | unsigned can_be_parallel : 1; | |
218 | ||
219 | /* True if -Waggressive-loop-optimizations warned about this loop | |
220 | already. */ | |
221 | unsigned warned_aggressive_loop_optimizations : 1; | |
222 | ||
5ce9450f | 223 | /* True if this loop should never be vectorized. */ |
34e82342 | 224 | unsigned dont_vectorize : 1; |
5ce9450f | 225 | |
718c4601 | 226 | /* True if we should try harder to vectorize this loop. */ |
34e82342 | 227 | unsigned force_vectorize : 1; |
718c4601 | 228 | |
886c388d | 229 | /* True if the loop is part of an oacc kernels region. */ |
34e82342 | 230 | unsigned in_oacc_kernels_region : 1; |
886c388d | 231 | |
75efe9cb RB |
232 | /* True if the loop is known to be finite. This is a localized |
233 | flag_finite_loops or similar pragmas state. */ | |
234 | unsigned finite_p : 1; | |
235 | ||
ff27462e EB |
236 | /* The number of times to unroll the loop. 0 means no information given, |
237 | just do what we always do. A value of 1 means do not unroll the loop. | |
238 | A value of USHRT_MAX means unroll with no specific unrolling factor. | |
239 | Other values means unroll with the given unrolling factor. */ | |
ac9effed EB |
240 | unsigned short unroll; |
241 | ||
94ec37a9 RB |
242 | /* If this loop was inlined the main clique of the callee which does |
243 | not need remapping when copying the loop body. */ | |
244 | unsigned short owned_clique; | |
245 | ||
74bf76ed JJ |
246 | /* For SIMD loops, this is a unique identifier of the loop, referenced |
247 | by IFN_GOMP_SIMD_VF, IFN_GOMP_SIMD_LANE and IFN_GOMP_SIMD_LAST_LANE | |
248 | builtins. */ | |
249 | tree simduid; | |
250 | ||
542e7230 BC |
251 | /* In loop optimization, it's common to generate loops from the original |
252 | loop. This field records the index of the original loop which can be | |
253 | used to track the original loop from newly generated loops. This can | |
254 | be done by calling function get_loop (cfun, orig_loop_num). Note the | |
255 | original loop could be destroyed for various reasons thus no longer | |
256 | exists, as a result, function call to get_loop returns NULL pointer. | |
257 | In this case, this field should not be used and needs to be cleared | |
258 | whenever possible. */ | |
259 | int orig_loop_num; | |
260 | ||
e9eb809d | 261 | /* Upper bound on number of iterations of a loop. */ |
99b1c316 | 262 | class nb_iter_bound *bounds; |
82b85a85 | 263 | |
2f07b722 BC |
264 | /* Non-overflow control ivs of a loop. */ |
265 | struct control_iv *control_ivs; | |
266 | ||
6270df4c | 267 | /* Head of the cyclic list of the exits of the loop. */ |
9e2f83a5 | 268 | struct loop_exit *exits; |
ef23e6a2 RB |
269 | |
270 | /* Number of iteration analysis data for RTL. */ | |
99b1c316 | 271 | class niter_desc *simple_loop_desc; |
e4ca2139 | 272 | |
e4ca2139 RB |
273 | /* For sanity checking during loop fixup we record here the former |
274 | loop header for loops marked for removal. Note that this prevents | |
275 | the basic-block from being collected but its index can still be | |
276 | reused. */ | |
277 | basic_block former_header; | |
3d436d2a ZD |
278 | }; |
279 | ||
18767ebc BC |
280 | /* Set if the loop is known to be infinite. */ |
281 | #define LOOP_C_INFINITE (1 << 0) | |
282 | /* Set if the loop is known to be finite without any assumptions. */ | |
283 | #define LOOP_C_FINITE (1 << 1) | |
284 | ||
285 | /* Set C to the LOOP constraint. */ | |
cb3e0eac | 286 | inline void |
99b1c316 | 287 | loop_constraint_set (class loop *loop, unsigned c) |
18767ebc BC |
288 | { |
289 | loop->constraints |= c; | |
290 | } | |
291 | ||
292 | /* Clear C from the LOOP constraint. */ | |
cb3e0eac | 293 | inline void |
99b1c316 | 294 | loop_constraint_clear (class loop *loop, unsigned c) |
18767ebc BC |
295 | { |
296 | loop->constraints &= ~c; | |
297 | } | |
298 | ||
299 | /* Check if C is set in the LOOP constraint. */ | |
cb3e0eac | 300 | inline bool |
99b1c316 | 301 | loop_constraint_set_p (class loop *loop, unsigned c) |
18767ebc BC |
302 | { |
303 | return (loop->constraints & c) == c; | |
304 | } | |
305 | ||
3d436d2a ZD |
306 | /* Flags for state of loop structure. */ |
307 | enum | |
308 | { | |
309 | LOOPS_HAVE_PREHEADERS = 1, | |
310 | LOOPS_HAVE_SIMPLE_LATCHES = 2, | |
82b85a85 | 311 | LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS = 4, |
89f8f30f | 312 | LOOPS_HAVE_RECORDED_EXITS = 8, |
c7b852c8 | 313 | LOOPS_MAY_HAVE_MULTIPLE_LATCHES = 16, |
592c303d | 314 | LOOP_CLOSED_SSA = 32, |
e855c69d AB |
315 | LOOPS_NEED_FIXUP = 64, |
316 | LOOPS_HAVE_FALLTHRU_PREHEADERS = 128 | |
3d436d2a ZD |
317 | }; |
318 | ||
d78f3f78 ZD |
319 | #define LOOPS_NORMAL (LOOPS_HAVE_PREHEADERS | LOOPS_HAVE_SIMPLE_LATCHES \ |
320 | | LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS) | |
89f8f30f | 321 | #define AVOID_CFG_MODIFICATIONS (LOOPS_MAY_HAVE_MULTIPLE_LATCHES) |
d78f3f78 | 322 | |
3d436d2a | 323 | /* Structure to hold CFG information about natural loops within a function. */ |
d1b38208 | 324 | struct GTY (()) loops { |
0162f155 RG |
325 | /* State of loops. */ |
326 | int state; | |
3d436d2a | 327 | |
42fd6772 | 328 | /* Array of the loops. */ |
9771b263 | 329 | vec<loop_p, va_gc> *larray; |
3d436d2a | 330 | |
6270df4c ZD |
331 | /* Maps edges to the list of their descriptions as loop exits. Edges |
332 | whose sources or destinations have loop_father == NULL (which may | |
333 | happen during the cfg manipulations) should not appear in EXITS. */ | |
2a22f99c | 334 | hash_table<loop_exit_hasher> *GTY(()) exits; |
6270df4c | 335 | |
3d436d2a | 336 | /* Pointer to root of loop hierarchy tree. */ |
99b1c316 | 337 | class loop *tree_root; |
3d436d2a ZD |
338 | }; |
339 | ||
3d436d2a | 340 | /* Loop recognition. */ |
0375167b | 341 | bool bb_loop_header_p (basic_block); |
dd366ec3 | 342 | void init_loops_structure (struct function *, struct loops *, unsigned); |
0375167b | 343 | extern struct loops *flow_loops_find (struct loops *); |
89f8f30f | 344 | extern void disambiguate_loops_with_multiple_latches (void); |
d329e058 | 345 | extern void flow_loops_free (struct loops *); |
d73be268 | 346 | extern void flow_loops_dump (FILE *, |
99b1c316 MS |
347 | void (*)(const class loop *, FILE *, int), int); |
348 | extern void flow_loop_dump (const class loop *, FILE *, | |
349 | void (*)(const class loop *, FILE *, int), int); | |
350 | class loop *alloc_loop (void); | |
351 | extern void flow_loop_free (class loop *); | |
352 | int flow_loop_nodes_find (basic_block, class loop *); | |
8e89b5b5 | 353 | unsigned fix_loop_structure (bitmap changed_bbs); |
2de58650 | 354 | bool mark_irreducible_loops (void); |
61183076 | 355 | void release_recorded_exits (function *); |
6270df4c ZD |
356 | void record_loop_exits (void); |
357 | void rescan_loop_exit (edge, bool, bool); | |
26993e95 | 358 | void sort_sibling_loops (function *); |
3d436d2a | 359 | |
4d6922ee | 360 | /* Loop data structure manipulation/querying. */ |
99b1c316 MS |
361 | extern void flow_loop_tree_node_add (class loop *, class loop *, |
362 | class loop * = NULL); | |
363 | extern void flow_loop_tree_node_remove (class loop *); | |
364 | extern bool flow_loop_nested_p (const class loop *, const class loop *); | |
365 | extern bool flow_bb_inside_loop_p (const class loop *, const_basic_block); | |
366 | extern class loop * find_common_loop (class loop *, class loop *); | |
367 | class loop *superloop_at_depth (class loop *, unsigned); | |
a79683d5 | 368 | struct eni_weights; |
99b1c316 MS |
369 | extern int num_loop_insns (const class loop *); |
370 | extern int average_num_loop_insns (const class loop *); | |
371 | extern unsigned get_loop_level (const class loop *); | |
372 | extern bool loop_exit_edge_p (const class loop *, const_edge); | |
373 | extern bool loop_exits_to_bb_p (class loop *, basic_block); | |
374 | extern bool loop_exits_from_bb_p (class loop *, basic_block); | |
d73be268 | 375 | extern void mark_loop_exit_edges (void); |
99b1c316 | 376 | extern dump_user_location_t get_loop_location (class loop *loop); |
3d436d2a ZD |
377 | |
378 | /* Loops & cfg manipulation. */ | |
99b1c316 MS |
379 | extern basic_block *get_loop_body (const class loop *); |
380 | extern unsigned get_loop_body_with_size (const class loop *, basic_block *, | |
89f8f30f | 381 | unsigned); |
99b1c316 MS |
382 | extern basic_block *get_loop_body_in_dom_order (const class loop *); |
383 | extern basic_block *get_loop_body_in_bfs_order (const class loop *); | |
384 | extern basic_block *get_loop_body_in_custom_order (const class loop *, | |
e855c69d | 385 | int (*) (const void *, const void *)); |
eef99cd9 GB |
386 | extern basic_block *get_loop_body_in_custom_order (const class loop *, void *, |
387 | int (*) (const void *, const void *, void *)); | |
e855c69d | 388 | |
4b9d61f7 | 389 | extern auto_vec<edge> get_loop_exit_edges (const class loop *, basic_block * = NULL); |
99b1c316 | 390 | extern edge single_exit (const class loop *); |
00dcc88a | 391 | extern edge single_likely_exit (class loop *loop, const vec<edge> &); |
99b1c316 | 392 | extern unsigned num_loop_branches (const class loop *); |
3d436d2a | 393 | |
99b1c316 MS |
394 | extern edge loop_preheader_edge (const class loop *); |
395 | extern edge loop_latch_edge (const class loop *); | |
3d436d2a | 396 | |
99b1c316 | 397 | extern void add_bb_to_loop (basic_block, class loop *); |
d329e058 | 398 | extern void remove_bb_from_loops (basic_block); |
3d436d2a | 399 | |
99b1c316 MS |
400 | extern void cancel_loop_tree (class loop *); |
401 | extern void delete_loop (class loop *); | |
3d436d2a | 402 | |
3d436d2a | 403 | |
d73be268 | 404 | extern void verify_loop_structure (void); |
3d436d2a ZD |
405 | |
406 | /* Loop analysis. */ | |
99b1c316 MS |
407 | extern bool just_once_each_iteration_p (const class loop *, const_basic_block); |
408 | gcov_type expected_loop_iterations_unbounded (const class loop *, | |
89619f87 JH |
409 | bool *read_profile_p = NULL); |
410 | extern bool expected_loop_iterations_by_profile (const class loop *loop, | |
411 | sreal *ret, | |
412 | bool *reliable = NULL); | |
ea272814 | 413 | extern bool maybe_flat_loop_profile (const class loop *); |
99b1c316 | 414 | extern unsigned expected_loop_iterations (class loop *); |
30d2ef86 | 415 | extern rtx doloop_condition_get (rtx_insn *); |
617b465c | 416 | |
08c13199 | 417 | void mark_loop_for_removal (loop_p); |
15ec8d5a | 418 | void print_loop_info (FILE *file, const class loop *loop, const char *); |
08c13199 | 419 | |
50654f6c ZD |
420 | /* Induction variable analysis. */ |
421 | ||
422 | /* The description of induction variable. The things are a bit complicated | |
423 | due to need to handle subregs and extends. The value of the object described | |
424 | by it can be obtained as follows (all computations are done in extend_mode): | |
425 | ||
426 | Value in i-th iteration is | |
427 | delta + mult * extend_{extend_mode} (subreg_{mode} (base + i * step)). | |
428 | ||
429 | If first_special is true, the value in the first iteration is | |
430 | delta + mult * base | |
c22cacf3 | 431 | |
f822d252 | 432 | If extend = UNKNOWN, first_special must be false, delta 0, mult 1 and value is |
50654f6c ZD |
433 | subreg_{mode} (base + i * step) |
434 | ||
435 | The get_iv_value function can be used to obtain these expressions. | |
436 | ||
437 | ??? Add a third mode field that would specify the mode in that inner | |
438 | computation is done, which would enable it to be different from the | |
439 | outer one? */ | |
440 | ||
6c1dae73 | 441 | class rtx_iv |
50654f6c | 442 | { |
6c1dae73 | 443 | public: |
50654f6c ZD |
444 | /* Its base and step (mode of base and step is supposed to be extend_mode, |
445 | see the description above). */ | |
446 | rtx base, step; | |
447 | ||
1c1ad7bb SB |
448 | /* The type of extend applied to it (IV_SIGN_EXTEND, IV_ZERO_EXTEND, |
449 | or IV_UNKNOWN_EXTEND). */ | |
450 | enum iv_extend_code extend; | |
50654f6c ZD |
451 | |
452 | /* Operations applied in the extended mode. */ | |
453 | rtx delta, mult; | |
454 | ||
455 | /* The mode it is extended to. */ | |
3d88d1cd | 456 | scalar_int_mode extend_mode; |
50654f6c ZD |
457 | |
458 | /* The mode the variable iterates in. */ | |
3d88d1cd | 459 | scalar_int_mode mode; |
50654f6c | 460 | |
50654f6c ZD |
461 | /* Whether the first iteration needs to be handled specially. */ |
462 | unsigned first_special : 1; | |
463 | }; | |
464 | ||
f2dca510 ZD |
465 | /* The description of an exit from the loop and of the number of iterations |
466 | till we take the exit. */ | |
50654f6c | 467 | |
6c1dae73 | 468 | class GTY(()) niter_desc |
50654f6c | 469 | { |
6c1dae73 | 470 | public: |
50654f6c ZD |
471 | /* The edge out of the loop. */ |
472 | edge out_edge; | |
473 | ||
474 | /* The other edge leading from the condition. */ | |
475 | edge in_edge; | |
476 | ||
477 | /* True if we are able to say anything about number of iterations of the | |
478 | loop. */ | |
479 | bool simple_p; | |
480 | ||
481 | /* True if the loop iterates the constant number of times. */ | |
482 | bool const_iter; | |
483 | ||
484 | /* Number of iterations if constant. */ | |
a9243bfc | 485 | uint64_t niter; |
50654f6c | 486 | |
50654f6c ZD |
487 | /* Assumptions under that the rest of the information is valid. */ |
488 | rtx assumptions; | |
489 | ||
490 | /* Assumptions under that the loop ends before reaching the latch, | |
491 | even if value of niter_expr says otherwise. */ | |
492 | rtx noloop_assumptions; | |
493 | ||
494 | /* Condition under that the loop is infinite. */ | |
495 | rtx infinite; | |
496 | ||
497 | /* Whether the comparison is signed. */ | |
498 | bool signed_p; | |
499 | ||
500 | /* The mode in that niter_expr should be computed. */ | |
3d88d1cd | 501 | scalar_int_mode mode; |
50654f6c ZD |
502 | |
503 | /* The number of iterations of the loop. */ | |
504 | rtx niter_expr; | |
505 | }; | |
506 | ||
99b1c316 MS |
507 | extern void iv_analysis_loop_init (class loop *); |
508 | extern bool iv_analyze (rtx_insn *, scalar_int_mode, rtx, class rtx_iv *); | |
509 | extern bool iv_analyze_result (rtx_insn *, rtx, class rtx_iv *); | |
3d88d1cd | 510 | extern bool iv_analyze_expr (rtx_insn *, scalar_int_mode, rtx, |
99b1c316 MS |
511 | class rtx_iv *); |
512 | extern rtx get_iv_value (class rtx_iv *, rtx); | |
3d88d1cd | 513 | extern bool biv_p (rtx_insn *, scalar_int_mode, rtx); |
50654f6c ZD |
514 | extern void iv_analysis_done (void); |
515 | ||
99b1c316 MS |
516 | extern class niter_desc *get_simple_loop_desc (class loop *loop); |
517 | extern void free_simple_loop_desc (class loop *loop); | |
50654f6c | 518 | |
cb3e0eac | 519 | inline class niter_desc * |
99b1c316 | 520 | simple_loop_desc (class loop *loop) |
50654f6c | 521 | { |
ef23e6a2 | 522 | return loop->simple_loop_desc; |
50654f6c ZD |
523 | } |
524 | ||
42fd6772 ZD |
525 | /* Accessors for the loop structures. */ |
526 | ||
0fc822d0 | 527 | /* Returns the loop with index NUM from FNs loop tree. */ |
42fd6772 | 528 | |
cb3e0eac | 529 | inline class loop * |
0fc822d0 | 530 | get_loop (struct function *fn, unsigned num) |
42fd6772 | 531 | { |
0fc822d0 | 532 | return (*loops_for_fn (fn)->larray)[num]; |
42fd6772 ZD |
533 | } |
534 | ||
9ba025a2 ZD |
535 | /* Returns the number of superloops of LOOP. */ |
536 | ||
cb3e0eac | 537 | inline unsigned |
99b1c316 | 538 | loop_depth (const class loop *loop) |
9ba025a2 | 539 | { |
9771b263 | 540 | return vec_safe_length (loop->superloops); |
9ba025a2 ZD |
541 | } |
542 | ||
543 | /* Returns the immediate superloop of LOOP, or NULL if LOOP is the outermost | |
544 | loop. */ | |
545 | ||
cb3e0eac | 546 | inline class loop * |
99b1c316 | 547 | loop_outer (const class loop *loop) |
9ba025a2 | 548 | { |
9771b263 | 549 | unsigned n = vec_safe_length (loop->superloops); |
9ba025a2 ZD |
550 | |
551 | if (n == 0) | |
552 | return NULL; | |
553 | ||
9771b263 | 554 | return (*loop->superloops)[n - 1]; |
9ba025a2 ZD |
555 | } |
556 | ||
07643d76 AM |
557 | /* Returns true if LOOP has at least one exit edge. */ |
558 | ||
cb3e0eac | 559 | inline bool |
99b1c316 | 560 | loop_has_exit_edges (const class loop *loop) |
07643d76 AM |
561 | { |
562 | return loop->exits->next->e != NULL; | |
563 | } | |
564 | ||
0fc822d0 | 565 | /* Returns the list of loops in FN. */ |
42fd6772 | 566 | |
0fc822d0 RB |
567 | inline vec<loop_p, va_gc> * |
568 | get_loops (struct function *fn) | |
42fd6772 | 569 | { |
0fc822d0 RB |
570 | struct loops *loops = loops_for_fn (fn); |
571 | if (!loops) | |
42fd6772 ZD |
572 | return NULL; |
573 | ||
0fc822d0 | 574 | return loops->larray; |
42fd6772 ZD |
575 | } |
576 | ||
0fc822d0 | 577 | /* Returns the number of loops in FN (including the removed |
42fd6772 ZD |
578 | ones and the fake loop that forms the root of the loop tree). */ |
579 | ||
cb3e0eac | 580 | inline unsigned |
0fc822d0 | 581 | number_of_loops (struct function *fn) |
42fd6772 | 582 | { |
0fc822d0 | 583 | struct loops *loops = loops_for_fn (fn); |
0d0e2af6 | 584 | if (!loops) |
42fd6772 ZD |
585 | return 0; |
586 | ||
0fc822d0 | 587 | return vec_safe_length (loops->larray); |
42fd6772 ZD |
588 | } |
589 | ||
f87000d0 ZD |
590 | /* Returns true if state of the loops satisfies all properties |
591 | described by FLAGS. */ | |
592 | ||
cb3e0eac | 593 | inline bool |
61183076 RB |
594 | loops_state_satisfies_p (function *fn, unsigned flags) |
595 | { | |
596 | return (loops_for_fn (fn)->state & flags) == flags; | |
597 | } | |
598 | ||
cb3e0eac | 599 | inline bool |
f87000d0 ZD |
600 | loops_state_satisfies_p (unsigned flags) |
601 | { | |
61183076 | 602 | return loops_state_satisfies_p (cfun, flags); |
f87000d0 ZD |
603 | } |
604 | ||
605 | /* Sets FLAGS to the loops state. */ | |
606 | ||
cb3e0eac | 607 | inline void |
61183076 RB |
608 | loops_state_set (function *fn, unsigned flags) |
609 | { | |
610 | loops_for_fn (fn)->state |= flags; | |
611 | } | |
612 | ||
cb3e0eac | 613 | inline void |
f87000d0 ZD |
614 | loops_state_set (unsigned flags) |
615 | { | |
61183076 | 616 | loops_state_set (cfun, flags); |
f87000d0 ZD |
617 | } |
618 | ||
619 | /* Clears FLAGS from the loops state. */ | |
620 | ||
cb3e0eac | 621 | inline void |
61183076 RB |
622 | loops_state_clear (function *fn, unsigned flags) |
623 | { | |
624 | loops_for_fn (fn)->state &= ~flags; | |
625 | } | |
626 | ||
cb3e0eac | 627 | inline void |
f87000d0 ZD |
628 | loops_state_clear (unsigned flags) |
629 | { | |
630 | if (!current_loops) | |
631 | return; | |
61183076 | 632 | loops_state_clear (cfun, flags); |
f87000d0 ZD |
633 | } |
634 | ||
a84a49b7 RB |
635 | /* Check loop structure invariants, if internal consistency checks are |
636 | enabled. */ | |
637 | ||
cb3e0eac | 638 | inline void |
a84a49b7 RB |
639 | checking_verify_loop_structure (void) |
640 | { | |
641 | /* VERIFY_LOOP_STRUCTURE essentially asserts that no loops need fixups. | |
642 | ||
643 | The loop optimizers should never make changes to the CFG which | |
644 | require loop fixups. But the low level CFG manipulation code may | |
645 | set the flag conservatively. | |
646 | ||
647 | Go ahead and clear the flag here. That avoids the assert inside | |
648 | VERIFY_LOOP_STRUCTURE, and if there is an inconsistency in the loop | |
649 | structures VERIFY_LOOP_STRUCTURE will detect it. | |
650 | ||
651 | This also avoid the compile time cost of excessive fixups. */ | |
652 | loops_state_clear (LOOPS_NEED_FIXUP); | |
653 | if (flag_checking) | |
654 | verify_loop_structure (); | |
655 | } | |
656 | ||
42fd6772 ZD |
657 | /* Loop iterators. */ |
658 | ||
659 | /* Flags for loop iteration. */ | |
660 | ||
661 | enum li_flags | |
662 | { | |
677cc14d ZD |
663 | LI_INCLUDE_ROOT = 1, /* Include the fake root of the loop tree. */ |
664 | LI_FROM_INNERMOST = 2, /* Iterate over the loops in the reverse order, | |
665 | starting from innermost ones. */ | |
666 | LI_ONLY_INNERMOST = 4 /* Iterate only over innermost loops. */ | |
42fd6772 ZD |
667 | }; |
668 | ||
e41ba804 KL |
669 | /* Provide the functionality of std::as_const to support range-based for |
670 | to use const iterator. (We can't use std::as_const itself because it's | |
671 | a C++17 feature.) */ | |
672 | template <typename T> | |
673 | constexpr const T & | |
674 | as_const (T &t) | |
675 | { | |
676 | return t; | |
677 | } | |
678 | ||
679 | /* A list for visiting loops, which contains the loop numbers instead of | |
d0a5624b KL |
680 | the loop pointers. If the loop ROOT is offered (non-null), the visiting |
681 | will start from it, otherwise it would start from the tree_root of | |
682 | loops_for_fn (FN) instead. The scope is restricted in function FN and | |
683 | the visiting order is specified by FLAGS. */ | |
42fd6772 | 684 | |
e41ba804 | 685 | class loops_list |
42fd6772 | 686 | { |
6c1dae73 | 687 | public: |
d0a5624b | 688 | loops_list (function *fn, unsigned flags, class loop *root = nullptr); |
e41ba804 KL |
689 | |
690 | template <typename T> class Iter | |
691 | { | |
692 | public: | |
693 | Iter (const loops_list &l, unsigned idx) : list (l), curr_idx (idx) | |
694 | { | |
695 | fill_curr_loop (); | |
696 | } | |
697 | ||
698 | T operator* () const { return curr_loop; } | |
699 | ||
700 | Iter & | |
701 | operator++ () | |
702 | { | |
703 | if (curr_idx < list.to_visit.length ()) | |
704 | { | |
705 | /* Bump the index and fill a new one. */ | |
706 | curr_idx++; | |
707 | fill_curr_loop (); | |
708 | } | |
709 | else | |
710 | gcc_assert (!curr_loop); | |
711 | ||
712 | return *this; | |
713 | } | |
714 | ||
715 | bool | |
716 | operator!= (const Iter &rhs) const | |
717 | { | |
718 | return this->curr_idx != rhs.curr_idx; | |
719 | } | |
720 | ||
721 | private: | |
722 | /* Fill the current loop starting from the current index. */ | |
723 | void fill_curr_loop (); | |
724 | ||
725 | /* Reference to the loop list to visit. */ | |
726 | const loops_list &list; | |
f0bd40b1 | 727 | |
e41ba804 KL |
728 | /* The current index in the list to visit. */ |
729 | unsigned curr_idx; | |
f0bd40b1 | 730 | |
e41ba804 KL |
731 | /* The loop implied by the current index. */ |
732 | class loop *curr_loop; | |
733 | }; | |
734 | ||
735 | using iterator = Iter<class loop *>; | |
736 | using const_iterator = Iter<const class loop *>; | |
737 | ||
738 | iterator | |
739 | begin () | |
740 | { | |
741 | return iterator (*this, 0); | |
742 | } | |
743 | ||
744 | iterator | |
745 | end () | |
746 | { | |
747 | return iterator (*this, to_visit.length ()); | |
748 | } | |
749 | ||
750 | const_iterator | |
751 | begin () const | |
752 | { | |
753 | return const_iterator (*this, 0); | |
754 | } | |
755 | ||
756 | const_iterator | |
757 | end () const | |
758 | { | |
759 | return const_iterator (*this, to_visit.length ()); | |
760 | } | |
761 | ||
762 | private: | |
d0a5624b KL |
763 | /* Walk loop tree starting from ROOT as the visiting order specified |
764 | by FLAGS. */ | |
765 | void walk_loop_tree (class loop *root, unsigned flags); | |
766 | ||
61183076 RB |
767 | /* The function we are visiting. */ |
768 | function *fn; | |
769 | ||
677cc14d | 770 | /* The list of loops to visit. */ |
d78b7095 | 771 | auto_vec<int, 16> to_visit; |
f0bd40b1 | 772 | }; |
42fd6772 | 773 | |
e41ba804 KL |
774 | /* Starting from current index CURR_IDX (inclusive), find one index |
775 | which stands for one valid loop and fill the found loop as CURR_LOOP, | |
776 | if we can't find one, set CURR_LOOP as null. */ | |
777 | ||
778 | template <typename T> | |
779 | inline void | |
780 | loops_list::Iter<T>::fill_curr_loop () | |
42fd6772 | 781 | { |
677cc14d ZD |
782 | int anum; |
783 | ||
e41ba804 | 784 | while (this->list.to_visit.iterate (this->curr_idx, &anum)) |
42fd6772 | 785 | { |
e41ba804 | 786 | class loop *loop = get_loop (this->list.fn, anum); |
f0bd40b1 | 787 | if (loop) |
e41ba804 KL |
788 | { |
789 | curr_loop = loop; | |
790 | return; | |
791 | } | |
792 | this->curr_idx++; | |
42fd6772 ZD |
793 | } |
794 | ||
e41ba804 | 795 | curr_loop = nullptr; |
42fd6772 ZD |
796 | } |
797 | ||
e41ba804 | 798 | /* Set up the loops list to visit according to the specified |
d0a5624b KL |
799 | function scope FN and iterating order FLAGS. If ROOT is |
800 | not null, the visiting would start from it, otherwise it | |
801 | will start from tree_root of loops_for_fn (FN). */ | |
e41ba804 | 802 | |
d0a5624b | 803 | inline loops_list::loops_list (function *fn, unsigned flags, class loop *root) |
42fd6772 | 804 | { |
d0a5624b KL |
805 | struct loops *loops = loops_for_fn (fn); |
806 | gcc_assert (!root || loops); | |
807 | ||
808 | /* Check mutually exclusive flags should not co-exist. */ | |
809 | unsigned checked_flags = LI_ONLY_INNERMOST | LI_FROM_INNERMOST; | |
810 | gcc_assert ((flags & checked_flags) != checked_flags); | |
677cc14d | 811 | |
61183076 | 812 | this->fn = fn; |
d0a5624b | 813 | if (!loops) |
e41ba804 | 814 | return; |
42fd6772 | 815 | |
d0a5624b KL |
816 | class loop *tree_root = root ? root : loops->tree_root; |
817 | ||
d78b7095 | 818 | this->to_visit.reserve_exact (number_of_loops (fn)); |
677cc14d | 819 | |
d0a5624b KL |
820 | /* When root is tree_root of loops_for_fn (fn) and the visiting |
821 | order is LI_ONLY_INNERMOST, we would like to use linear | |
822 | search here since it has a more stable bound than the | |
823 | walk_loop_tree. */ | |
824 | if (flags & LI_ONLY_INNERMOST && tree_root == loops->tree_root) | |
677cc14d | 825 | { |
d0a5624b KL |
826 | gcc_assert (tree_root->num == 0); |
827 | if (tree_root->inner == NULL) | |
677cc14d | 828 | { |
d0a5624b KL |
829 | if (flags & LI_INCLUDE_ROOT) |
830 | this->to_visit.quick_push (0); | |
831 | ||
832 | return; | |
677cc14d | 833 | } |
d0a5624b KL |
834 | |
835 | class loop *aloop; | |
836 | unsigned int i; | |
837 | for (i = 1; vec_safe_iterate (loops->larray, i, &aloop); i++) | |
838 | if (aloop != NULL && aloop->inner == NULL) | |
839 | this->to_visit.quick_push (aloop->num); | |
42fd6772 ZD |
840 | } |
841 | else | |
d0a5624b | 842 | walk_loop_tree (tree_root, flags); |
42fd6772 ZD |
843 | } |
844 | ||
8b11a64c | 845 | /* The properties of the target. */ |
4391924a RS |
846 | struct target_cfgloop { |
847 | /* Number of available registers. */ | |
848 | unsigned x_target_avail_regs; | |
8b11a64c | 849 | |
4391924a RS |
850 | /* Number of available registers that are call-clobbered. */ |
851 | unsigned x_target_clobbered_regs; | |
852 | ||
853 | /* Number of registers reserved for temporary expressions. */ | |
854 | unsigned x_target_res_regs; | |
855 | ||
856 | /* The cost for register when there still is some reserve, but we are | |
857 | approaching the number of available registers. */ | |
858 | unsigned x_target_reg_cost[2]; | |
859 | ||
860 | /* The cost for register when we need to spill. */ | |
861 | unsigned x_target_spill_cost[2]; | |
862 | }; | |
863 | ||
864 | extern struct target_cfgloop default_target_cfgloop; | |
865 | #if SWITCHABLE_TARGET | |
866 | extern struct target_cfgloop *this_target_cfgloop; | |
867 | #else | |
868 | #define this_target_cfgloop (&default_target_cfgloop) | |
869 | #endif | |
870 | ||
871 | #define target_avail_regs \ | |
872 | (this_target_cfgloop->x_target_avail_regs) | |
873 | #define target_clobbered_regs \ | |
874 | (this_target_cfgloop->x_target_clobbered_regs) | |
875 | #define target_res_regs \ | |
876 | (this_target_cfgloop->x_target_res_regs) | |
877 | #define target_reg_cost \ | |
878 | (this_target_cfgloop->x_target_reg_cost) | |
879 | #define target_spill_cost \ | |
880 | (this_target_cfgloop->x_target_spill_cost) | |
8b11a64c | 881 | |
5e962776 ZD |
882 | /* Register pressure estimation for induction variable optimizations & loop |
883 | invariant motion. */ | |
bec922f0 | 884 | extern unsigned estimate_reg_pressure_cost (unsigned, unsigned, bool, bool); |
5e962776 ZD |
885 | extern void init_set_costs (void); |
886 | ||
617b465c | 887 | /* Loop optimizer initialization. */ |
598ec7bd | 888 | extern void loop_optimizer_init (unsigned); |
d87ee7f1 | 889 | extern void loop_optimizer_finalize (function *, bool = false); |
61183076 RB |
890 | inline void |
891 | loop_optimizer_finalize () | |
892 | { | |
893 | loop_optimizer_finalize (cfun); | |
894 | } | |
617b465c ZD |
895 | |
896 | /* Optimization passes. */ | |
b17d5d7c ZD |
897 | enum |
898 | { | |
f8934be7 JH |
899 | UAP_UNROLL = 1, /* Enables unrolling of loops if it seems profitable. */ |
900 | UAP_UNROLL_ALL = 2 /* Enables unrolling of all loops. */ | |
b17d5d7c ZD |
901 | }; |
902 | ||
d73be268 ZD |
903 | extern void doloop_optimize_loops (void); |
904 | extern void move_loop_invariants (void); | |
a165040e | 905 | extern auto_vec<basic_block> get_loop_hot_path (const class loop *loop); |
59587b18 | 906 | |
a5497b12 | 907 | /* Returns the outermost loop of the loop nest that contains LOOP.*/ |
cb3e0eac | 908 | inline class loop * |
99b1c316 | 909 | loop_outermost (class loop *loop) |
a5497b12 | 910 | { |
9771b263 | 911 | unsigned n = vec_safe_length (loop->superloops); |
a5497b12 VK |
912 | |
913 | if (n <= 1) | |
914 | return loop; | |
915 | ||
9771b263 | 916 | return (*loop->superloops)[1]; |
a5497b12 VK |
917 | } |
918 | ||
99b1c316 MS |
919 | extern void record_niter_bound (class loop *, const widest_int &, bool, bool); |
920 | extern HOST_WIDE_INT get_estimated_loop_iterations_int (class loop *); | |
921 | extern HOST_WIDE_INT get_max_loop_iterations_int (const class loop *); | |
922 | extern HOST_WIDE_INT get_likely_max_loop_iterations_int (class loop *); | |
923 | extern bool get_estimated_loop_iterations (class loop *loop, widest_int *nit); | |
924 | extern bool get_max_loop_iterations (const class loop *loop, widest_int *nit); | |
925 | extern bool get_likely_max_loop_iterations (class loop *loop, widest_int *nit); | |
4484a35a | 926 | extern int bb_loop_depth (const_basic_block); |
a7d4310a | 927 | extern edge single_dom_exit (class loop *); |
b278d308 | 928 | extern profile_count loop_count_in (const class loop *loop); |
a5497b12 | 929 | |
807e902e | 930 | /* Converts VAL to widest_int. */ |
71343877 | 931 | |
cb3e0eac | 932 | inline widest_int |
807e902e | 933 | gcov_type_to_wide_int (gcov_type val) |
71343877 | 934 | { |
807e902e | 935 | HOST_WIDE_INT a[2]; |
71343877 | 936 | |
807e902e | 937 | a[0] = (unsigned HOST_WIDE_INT) val; |
71343877 AM |
938 | /* If HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_WIDEST_INT, avoid shifting by |
939 | the size of type. */ | |
940 | val >>= HOST_BITS_PER_WIDE_INT - 1; | |
941 | val >>= 1; | |
807e902e | 942 | a[1] = (unsigned HOST_WIDE_INT) val; |
71343877 | 943 | |
807e902e | 944 | return widest_int::from_array (a, 2); |
71343877 | 945 | } |
59587b18 | 946 | #endif /* GCC_CFGLOOP_H */ |