]>
Commit | Line | Data |
---|---|---|
c836de3f | 1 | /* Loop unrolling. |
f1717362 | 2 | Copyright (C) 2002-2016 Free Software Foundation, Inc. |
ad5201d0 | 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 | |
8c4c00c1 | 8 | Software Foundation; either version 3, or (at your option) any later |
ad5201d0 | 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 | |
8c4c00c1 | 17 | along with GCC; see the file COPYING3. If not see |
18 | <http://www.gnu.org/licenses/>. */ | |
ad5201d0 | 19 | |
20 | #include "config.h" | |
21 | #include "system.h" | |
22 | #include "coretypes.h" | |
9ef16211 | 23 | #include "backend.h" |
7c29e30e | 24 | #include "target.h" |
ad5201d0 | 25 | #include "rtl.h" |
41a8aa41 | 26 | #include "tree.h" |
7c29e30e | 27 | #include "cfghooks.h" |
7c29e30e | 28 | #include "optabs.h" |
29 | #include "emit-rtl.h" | |
30 | #include "recog.h" | |
886c1262 | 31 | #include "profile.h" |
94ea8568 | 32 | #include "cfgrtl.h" |
ad5201d0 | 33 | #include "cfgloop.h" |
ad5201d0 | 34 | #include "params.h" |
d53441c8 | 35 | #include "dojump.h" |
ad5201d0 | 36 | #include "expr.h" |
b9ed1410 | 37 | #include "dumpfile.h" |
ad5201d0 | 38 | |
c836de3f | 39 | /* This pass performs loop unrolling. We only perform this |
40 | optimization on innermost loops (with single exception) because | |
ad5201d0 | 41 | the impact on performance is greatest here, and we want to avoid |
42 | unnecessary code size growth. The gain is caused by greater sequentiality | |
917bbcab | 43 | of code, better code to optimize for further passes and in some cases |
ad5201d0 | 44 | by fewer testings of exit conditions. The main problem is code growth, |
45 | that impacts performance negatively due to effect of caches. | |
46 | ||
47 | What we do: | |
48 | ||
ad5201d0 | 49 | -- unrolling of loops that roll constant times; this is almost always |
50 | win, as we get rid of exit condition tests. | |
51 | -- unrolling of loops that roll number of times that we can compute | |
52 | in runtime; we also get rid of exit condition tests here, but there | |
53 | is the extra expense for calculating the number of iterations | |
54 | -- simple unrolling of remaining loops; this is performed only if we | |
55 | are asked to, as the gain is questionable in this case and often | |
56 | it may even slow down the code | |
57 | For more detailed descriptions of each of those, see comments at | |
58 | appropriate function below. | |
59 | ||
60 | There is a lot of parameters (defined and described in params.def) that | |
c836de3f | 61 | control how much we unroll. |
ad5201d0 | 62 | |
63 | ??? A great problem is that we don't have a good way how to determine | |
64 | how many times we should unroll the loop; the experiments I have made | |
65 | showed that this choice may affect performance in order of several %. | |
66 | */ | |
67 | ||
a9989fb4 | 68 | /* Information about induction variables to split. */ |
69 | ||
70 | struct iv_to_split | |
71 | { | |
222dc1d3 | 72 | rtx_insn *insn; /* The insn in that the induction variable occurs. */ |
ef770610 | 73 | rtx orig_var; /* The variable (register) for the IV before split. */ |
a9989fb4 | 74 | rtx base_var; /* The variable on that the values in the further |
75 | iterations are based. */ | |
76 | rtx step; /* Step of the induction variable. */ | |
b635a1a3 | 77 | struct iv_to_split *next; /* Next entry in walking order. */ |
a9989fb4 | 78 | }; |
79 | ||
375bb675 | 80 | /* Information about accumulators to expand. */ |
81 | ||
82 | struct var_to_expand | |
83 | { | |
222dc1d3 | 84 | rtx_insn *insn; /* The insn in that the variable expansion occurs. */ |
dac49aa5 | 85 | rtx reg; /* The accumulator which is expanded. */ |
f1f41a6c | 86 | vec<rtx> var_expansions; /* The copies of the accumulator which is expanded. */ |
b635a1a3 | 87 | struct var_to_expand *next; /* Next entry in walking order. */ |
48e1416a | 88 | enum rtx_code op; /* The type of the accumulation - addition, subtraction |
375bb675 | 89 | or multiplication. */ |
90 | int expansion_count; /* Count the number of expansions generated so far. */ | |
91 | int reuse_expansion; /* The expansion we intend to reuse to expand | |
48e1416a | 92 | the accumulator. If REUSE_EXPANSION is 0 reuse |
93 | the original accumulator. Else use | |
375bb675 | 94 | var_expansions[REUSE_EXPANSION - 1]. */ |
95 | }; | |
96 | ||
d9dd21a8 | 97 | /* Hashtable helper for iv_to_split. */ |
98 | ||
298e7f9a | 99 | struct iv_split_hasher : free_ptr_hash <iv_to_split> |
d9dd21a8 | 100 | { |
9969c043 | 101 | static inline hashval_t hash (const iv_to_split *); |
102 | static inline bool equal (const iv_to_split *, const iv_to_split *); | |
d9dd21a8 | 103 | }; |
104 | ||
105 | ||
106 | /* A hash function for information about insns to split. */ | |
107 | ||
108 | inline hashval_t | |
9969c043 | 109 | iv_split_hasher::hash (const iv_to_split *ivts) |
d9dd21a8 | 110 | { |
111 | return (hashval_t) INSN_UID (ivts->insn); | |
112 | } | |
113 | ||
114 | /* An equality functions for information about insns to split. */ | |
115 | ||
116 | inline bool | |
9969c043 | 117 | iv_split_hasher::equal (const iv_to_split *i1, const iv_to_split *i2) |
d9dd21a8 | 118 | { |
119 | return i1->insn == i2->insn; | |
120 | } | |
121 | ||
122 | /* Hashtable helper for iv_to_split. */ | |
123 | ||
298e7f9a | 124 | struct var_expand_hasher : free_ptr_hash <var_to_expand> |
d9dd21a8 | 125 | { |
9969c043 | 126 | static inline hashval_t hash (const var_to_expand *); |
127 | static inline bool equal (const var_to_expand *, const var_to_expand *); | |
d9dd21a8 | 128 | }; |
129 | ||
130 | /* Return a hash for VES. */ | |
131 | ||
132 | inline hashval_t | |
9969c043 | 133 | var_expand_hasher::hash (const var_to_expand *ves) |
d9dd21a8 | 134 | { |
135 | return (hashval_t) INSN_UID (ves->insn); | |
136 | } | |
137 | ||
138 | /* Return true if I1 and I2 refer to the same instruction. */ | |
139 | ||
140 | inline bool | |
9969c043 | 141 | var_expand_hasher::equal (const var_to_expand *i1, const var_to_expand *i2) |
d9dd21a8 | 142 | { |
143 | return i1->insn == i2->insn; | |
144 | } | |
145 | ||
375bb675 | 146 | /* Information about optimization applied in |
147 | the unrolled loop. */ | |
148 | ||
149 | struct opt_info | |
a9989fb4 | 150 | { |
c1f445d2 | 151 | hash_table<iv_split_hasher> *insns_to_split; /* A hashtable of insns to |
d9dd21a8 | 152 | split. */ |
b635a1a3 | 153 | struct iv_to_split *iv_to_split_head; /* The first iv to split. */ |
154 | struct iv_to_split **iv_to_split_tail; /* Pointer to the tail of the list. */ | |
c1f445d2 | 155 | hash_table<var_expand_hasher> *insns_with_var_to_expand; /* A hashtable of |
d9dd21a8 | 156 | insns with accumulators to expand. */ |
b635a1a3 | 157 | struct var_to_expand *var_to_expand_head; /* The first var to expand. */ |
158 | struct var_to_expand **var_to_expand_tail; /* Pointer to the tail of the list. */ | |
375bb675 | 159 | unsigned first_new_block; /* The first basic block that was |
160 | duplicated. */ | |
161 | basic_block loop_exit; /* The loop exit basic block. */ | |
162 | basic_block loop_preheader; /* The loop preheader basic block. */ | |
a9989fb4 | 163 | }; |
164 | ||
0051c76a | 165 | static void decide_unroll_stupid (struct loop *, int); |
166 | static void decide_unroll_constant_iterations (struct loop *, int); | |
167 | static void decide_unroll_runtime_iterations (struct loop *, int); | |
7194de72 | 168 | static void unroll_loop_stupid (struct loop *); |
c836de3f | 169 | static void decide_unrolling (int); |
7194de72 | 170 | static void unroll_loop_constant_iterations (struct loop *); |
171 | static void unroll_loop_runtime_iterations (struct loop *); | |
375bb675 | 172 | static struct opt_info *analyze_insns_in_loop (struct loop *); |
173 | static void opt_info_start_duplication (struct opt_info *); | |
174 | static void apply_opt_in_copies (struct opt_info *, unsigned, bool, bool); | |
175 | static void free_opt_info (struct opt_info *); | |
222dc1d3 | 176 | static struct var_to_expand *analyze_insn_to_expand_var (struct loop*, rtx_insn *); |
3b3940d7 | 177 | static bool referenced_in_one_insn_in_loop_p (struct loop *, rtx, int *); |
3eeb4f9a | 178 | static struct iv_to_split *analyze_iv_to_split_insn (rtx_insn *); |
222dc1d3 | 179 | static void expand_var_during_unrolling (struct var_to_expand *, rtx_insn *); |
b635a1a3 | 180 | static void insert_var_expansion_initialization (struct var_to_expand *, |
181 | basic_block); | |
182 | static void combine_var_copies_in_loop_exit (struct var_to_expand *, | |
183 | basic_block); | |
375bb675 | 184 | static rtx get_expansion (struct var_to_expand *); |
ad5201d0 | 185 | |
c836de3f | 186 | /* Emit a message summarizing the unroll that will be |
f55775aa | 187 | performed for LOOP, along with the loop's location LOCUS, if |
188 | appropriate given the dump or -fopt-info settings. */ | |
189 | ||
190 | static void | |
c836de3f | 191 | report_unroll (struct loop *loop, location_t locus) |
f55775aa | 192 | { |
f55775aa | 193 | int report_flags = MSG_OPTIMIZED_LOCATIONS | TDF_RTL | TDF_DETAILS; |
194 | ||
830b796a | 195 | if (loop->lpt_decision.decision == LPT_NONE) |
196 | return; | |
197 | ||
f55775aa | 198 | if (!dump_enabled_p ()) |
199 | return; | |
200 | ||
c836de3f | 201 | dump_printf_loc (report_flags, locus, |
202 | "loop unrolled %d times", | |
203 | loop->lpt_decision.times); | |
f55775aa | 204 | if (profile_info) |
205 | dump_printf (report_flags, | |
c836de3f | 206 | " (header execution count %d)", |
f55775aa | 207 | (int)loop->header->count); |
f55775aa | 208 | |
209 | dump_printf (report_flags, "\n"); | |
210 | } | |
211 | ||
c836de3f | 212 | /* Decide whether unroll loops and how much. */ |
ad5201d0 | 213 | static void |
c836de3f | 214 | decide_unrolling (int flags) |
ad5201d0 | 215 | { |
17519ba0 | 216 | struct loop *loop; |
ad5201d0 | 217 | |
218 | /* Scan the loops, inner ones first. */ | |
f21d4d00 | 219 | FOR_EACH_LOOP (loop, LI_FROM_INNERMOST) |
ad5201d0 | 220 | { |
ad5201d0 | 221 | loop->lpt_decision.decision = LPT_NONE; |
f55775aa | 222 | location_t locus = get_loop_location (loop); |
ad5201d0 | 223 | |
f55775aa | 224 | if (dump_enabled_p ()) |
225 | dump_printf_loc (TDF_RTL, locus, | |
226 | ";; *** Considering loop %d at BB %d for " | |
c836de3f | 227 | "unrolling ***\n", |
f55775aa | 228 | loop->num, loop->header->index); |
ad5201d0 | 229 | |
230 | /* Do not peel cold areas. */ | |
0bfd8d5c | 231 | if (optimize_loop_for_size_p (loop)) |
ad5201d0 | 232 | { |
450d042a | 233 | if (dump_file) |
234 | fprintf (dump_file, ";; Not considering loop, cold area\n"); | |
ad5201d0 | 235 | continue; |
236 | } | |
237 | ||
238 | /* Can the loop be manipulated? */ | |
239 | if (!can_duplicate_loop_p (loop)) | |
240 | { | |
450d042a | 241 | if (dump_file) |
242 | fprintf (dump_file, | |
ad5201d0 | 243 | ";; Not considering loop, cannot duplicate\n"); |
ad5201d0 | 244 | continue; |
245 | } | |
246 | ||
247 | /* Skip non-innermost loops. */ | |
248 | if (loop->inner) | |
249 | { | |
450d042a | 250 | if (dump_file) |
251 | fprintf (dump_file, ";; Not considering loop, is not innermost\n"); | |
ad5201d0 | 252 | continue; |
253 | } | |
254 | ||
255 | loop->ninsns = num_loop_insns (loop); | |
256 | loop->av_ninsns = average_num_loop_insns (loop); | |
257 | ||
258 | /* Try transformations one by one in decreasing order of | |
259 | priority. */ | |
260 | ||
0051c76a | 261 | decide_unroll_constant_iterations (loop, flags); |
ad5201d0 | 262 | if (loop->lpt_decision.decision == LPT_NONE) |
0051c76a | 263 | decide_unroll_runtime_iterations (loop, flags); |
ad5201d0 | 264 | if (loop->lpt_decision.decision == LPT_NONE) |
0051c76a | 265 | decide_unroll_stupid (loop, flags); |
ad5201d0 | 266 | |
c836de3f | 267 | report_unroll (loop, locus); |
ad5201d0 | 268 | } |
ad5201d0 | 269 | } |
270 | ||
c836de3f | 271 | /* Unroll LOOPS. */ |
272 | void | |
273 | unroll_loops (int flags) | |
ad5201d0 | 274 | { |
c836de3f | 275 | struct loop *loop; |
276 | bool changed = false; | |
ad5201d0 | 277 | |
c836de3f | 278 | /* Now decide rest of unrolling. */ |
279 | decide_unrolling (flags); | |
ad5201d0 | 280 | |
c836de3f | 281 | /* Scan the loops, inner ones first. */ |
282 | FOR_EACH_LOOP (loop, LI_FROM_INNERMOST) | |
ad5201d0 | 283 | { |
c836de3f | 284 | /* And perform the appropriate transformations. */ |
285 | switch (loop->lpt_decision.decision) | |
3ad4992f | 286 | { |
c836de3f | 287 | case LPT_UNROLL_CONSTANT: |
288 | unroll_loop_constant_iterations (loop); | |
289 | changed = true; | |
290 | break; | |
291 | case LPT_UNROLL_RUNTIME: | |
292 | unroll_loop_runtime_iterations (loop); | |
293 | changed = true; | |
294 | break; | |
295 | case LPT_UNROLL_STUPID: | |
296 | unroll_loop_stupid (loop); | |
297 | changed = true; | |
298 | break; | |
299 | case LPT_NONE: | |
300 | break; | |
301 | default: | |
302 | gcc_unreachable (); | |
ad5201d0 | 303 | } |
ad5201d0 | 304 | } |
305 | ||
c836de3f | 306 | if (changed) |
307 | { | |
308 | calculate_dominance_info (CDI_DOMINATORS); | |
309 | fix_loop_structure (NULL); | |
310 | } | |
3ad4992f | 311 | |
c836de3f | 312 | iv_analysis_done (); |
313 | } | |
ad5201d0 | 314 | |
c836de3f | 315 | /* Check whether exit of the LOOP is at the end of loop body. */ |
3ad4992f | 316 | |
c836de3f | 317 | static bool |
318 | loop_exit_at_end_p (struct loop *loop) | |
ad5201d0 | 319 | { |
f9cce2dc | 320 | struct niter_desc *desc = get_simple_loop_desc (loop); |
c836de3f | 321 | rtx_insn *insn; |
a5414ff5 | 322 | |
c836de3f | 323 | /* We should never have conditional in latch block. */ |
324 | gcc_assert (desc->in_edge->dest != loop->header); | |
48e1416a | 325 | |
c836de3f | 326 | if (desc->in_edge->dest != loop->latch) |
327 | return false; | |
a9989fb4 | 328 | |
c836de3f | 329 | /* Check that the latch is empty. */ |
330 | FOR_BB_INSNS (loop->latch, insn) | |
331 | { | |
332 | if (INSN_P (insn) && active_insn_p (insn)) | |
333 | return false; | |
a5414ff5 | 334 | } |
ad5201d0 | 335 | |
c836de3f | 336 | return true; |
ad5201d0 | 337 | } |
338 | ||
450d042a | 339 | /* Decide whether to unroll LOOP iterating constant number of times |
340 | and how much. */ | |
f9cce2dc | 341 | |
ad5201d0 | 342 | static void |
0051c76a | 343 | decide_unroll_constant_iterations (struct loop *loop, int flags) |
ad5201d0 | 344 | { |
f9cce2dc | 345 | unsigned nunroll, nunroll_by_av, best_copies, best_unroll = 0, n_copies, i; |
346 | struct niter_desc *desc; | |
5de9d3ed | 347 | widest_int iterations; |
ad5201d0 | 348 | |
349 | if (!(flags & UAP_UNROLL)) | |
350 | { | |
351 | /* We were not asked to, just return back silently. */ | |
352 | return; | |
353 | } | |
354 | ||
450d042a | 355 | if (dump_file) |
356 | fprintf (dump_file, | |
357 | "\n;; Considering unrolling loop with constant " | |
358 | "number of iterations\n"); | |
ad5201d0 | 359 | |
360 | /* nunroll = total number of copies of the original loop body in | |
361 | unrolled loop (i.e. if it is 2, we have to duplicate loop body once. */ | |
362 | nunroll = PARAM_VALUE (PARAM_MAX_UNROLLED_INSNS) / loop->ninsns; | |
450d042a | 363 | nunroll_by_av |
364 | = PARAM_VALUE (PARAM_MAX_AVERAGE_UNROLLED_INSNS) / loop->av_ninsns; | |
ad5201d0 | 365 | if (nunroll > nunroll_by_av) |
366 | nunroll = nunroll_by_av; | |
367 | if (nunroll > (unsigned) PARAM_VALUE (PARAM_MAX_UNROLL_TIMES)) | |
368 | nunroll = PARAM_VALUE (PARAM_MAX_UNROLL_TIMES); | |
369 | ||
e7f33cf8 | 370 | if (targetm.loop_unroll_adjust) |
371 | nunroll = targetm.loop_unroll_adjust (nunroll, loop); | |
372 | ||
ad5201d0 | 373 | /* Skip big loops. */ |
374 | if (nunroll <= 1) | |
375 | { | |
450d042a | 376 | if (dump_file) |
377 | fprintf (dump_file, ";; Not considering loop, is too big\n"); | |
ad5201d0 | 378 | return; |
379 | } | |
380 | ||
381 | /* Check for simple loops. */ | |
f9cce2dc | 382 | desc = get_simple_loop_desc (loop); |
ad5201d0 | 383 | |
384 | /* Check number of iterations. */ | |
f9cce2dc | 385 | if (!desc->simple_p || !desc->const_iter || desc->assumptions) |
ad5201d0 | 386 | { |
450d042a | 387 | if (dump_file) |
388 | fprintf (dump_file, | |
389 | ";; Unable to prove that the loop iterates constant times\n"); | |
ad5201d0 | 390 | return; |
391 | } | |
392 | ||
c8fbcf58 | 393 | /* Check whether the loop rolls enough to consider. |
394 | Consult also loop bounds and profile; in the case the loop has more | |
395 | than one exit it may well loop less than determined maximal number | |
396 | of iterations. */ | |
397 | if (desc->niter < 2 * nunroll | |
f86b328b | 398 | || ((get_estimated_loop_iterations (loop, &iterations) |
399 | || get_max_loop_iterations (loop, &iterations)) | |
796b6678 | 400 | && wi::ltu_p (iterations, 2 * nunroll))) |
ad5201d0 | 401 | { |
450d042a | 402 | if (dump_file) |
403 | fprintf (dump_file, ";; Not unrolling loop, doesn't roll\n"); | |
ad5201d0 | 404 | return; |
405 | } | |
406 | ||
407 | /* Success; now compute number of iterations to unroll. We alter | |
408 | nunroll so that as few as possible copies of loop body are | |
d01481af | 409 | necessary, while still not decreasing the number of unrollings |
ad5201d0 | 410 | too much (at most by 1). */ |
411 | best_copies = 2 * nunroll + 10; | |
412 | ||
413 | i = 2 * nunroll + 2; | |
f9cce2dc | 414 | if (i - 1 >= desc->niter) |
415 | i = desc->niter - 2; | |
ad5201d0 | 416 | |
417 | for (; i >= nunroll - 1; i--) | |
418 | { | |
f9cce2dc | 419 | unsigned exit_mod = desc->niter % (i + 1); |
ad5201d0 | 420 | |
f9cce2dc | 421 | if (!loop_exit_at_end_p (loop)) |
ad5201d0 | 422 | n_copies = exit_mod + i + 1; |
f9cce2dc | 423 | else if (exit_mod != (unsigned) i |
424 | || desc->noloop_assumptions != NULL_RTX) | |
ad5201d0 | 425 | n_copies = exit_mod + i + 2; |
426 | else | |
427 | n_copies = i + 1; | |
428 | ||
429 | if (n_copies < best_copies) | |
430 | { | |
431 | best_copies = n_copies; | |
432 | best_unroll = i; | |
433 | } | |
434 | } | |
435 | ||
ad5201d0 | 436 | loop->lpt_decision.decision = LPT_UNROLL_CONSTANT; |
437 | loop->lpt_decision.times = best_unroll; | |
438 | } | |
439 | ||
1c18ed19 | 440 | /* Unroll LOOP with constant number of iterations LOOP->LPT_DECISION.TIMES times. |
441 | The transformation does this: | |
3ad4992f | 442 | |
ad5201d0 | 443 | for (i = 0; i < 102; i++) |
444 | body; | |
3ad4992f | 445 | |
1c18ed19 | 446 | ==> (LOOP->LPT_DECISION.TIMES == 3) |
3ad4992f | 447 | |
ad5201d0 | 448 | i = 0; |
449 | body; i++; | |
450 | body; i++; | |
451 | while (i < 102) | |
452 | { | |
453 | body; i++; | |
454 | body; i++; | |
455 | body; i++; | |
456 | body; i++; | |
457 | } | |
458 | */ | |
459 | static void | |
7194de72 | 460 | unroll_loop_constant_iterations (struct loop *loop) |
ad5201d0 | 461 | { |
462 | unsigned HOST_WIDE_INT niter; | |
463 | unsigned exit_mod; | |
464 | sbitmap wont_exit; | |
f3c40e6d | 465 | unsigned i; |
f3c40e6d | 466 | edge e; |
ad5201d0 | 467 | unsigned max_unroll = loop->lpt_decision.times; |
f9cce2dc | 468 | struct niter_desc *desc = get_simple_loop_desc (loop); |
469 | bool exit_at_end = loop_exit_at_end_p (loop); | |
375bb675 | 470 | struct opt_info *opt_info = NULL; |
a53ff4c1 | 471 | bool ok; |
48e1416a | 472 | |
ad5201d0 | 473 | niter = desc->niter; |
474 | ||
a9989fb4 | 475 | /* Should not get here (such loop should be peeled instead). */ |
476 | gcc_assert (niter > max_unroll + 1); | |
ad5201d0 | 477 | |
478 | exit_mod = niter % (max_unroll + 1); | |
479 | ||
480 | wont_exit = sbitmap_alloc (max_unroll + 1); | |
53c5d9d4 | 481 | bitmap_ones (wont_exit); |
ad5201d0 | 482 | |
c2078b80 | 483 | auto_vec<edge> remove_edges; |
48e1416a | 484 | if (flag_split_ivs_in_unroller |
375bb675 | 485 | || flag_variable_expansion_in_unroller) |
486 | opt_info = analyze_insns_in_loop (loop); | |
48e1416a | 487 | |
f9cce2dc | 488 | if (!exit_at_end) |
ad5201d0 | 489 | { |
f9cce2dc | 490 | /* The exit is not at the end of the loop; leave exit test |
ad5201d0 | 491 | in the first copy, so that the loops that start with test |
492 | of exit condition have continuous body after unrolling. */ | |
493 | ||
450d042a | 494 | if (dump_file) |
1c18ed19 | 495 | fprintf (dump_file, ";; Condition at beginning of loop.\n"); |
ad5201d0 | 496 | |
497 | /* Peel exit_mod iterations. */ | |
08b7917c | 498 | bitmap_clear_bit (wont_exit, 0); |
f9cce2dc | 499 | if (desc->noloop_assumptions) |
08b7917c | 500 | bitmap_clear_bit (wont_exit, 1); |
ad5201d0 | 501 | |
f9cce2dc | 502 | if (exit_mod) |
503 | { | |
375bb675 | 504 | opt_info_start_duplication (opt_info); |
a53ff4c1 | 505 | ok = duplicate_loop_to_header_edge (loop, loop_preheader_edge (loop), |
7194de72 | 506 | exit_mod, |
f9cce2dc | 507 | wont_exit, desc->out_edge, |
f3c40e6d | 508 | &remove_edges, |
75cc36a4 | 509 | DLTHE_FLAG_UPDATE_FREQ |
510 | | (opt_info && exit_mod > 1 | |
511 | ? DLTHE_RECORD_COPY_NUMBER | |
512 | : 0)); | |
a53ff4c1 | 513 | gcc_assert (ok); |
f9cce2dc | 514 | |
375bb675 | 515 | if (opt_info && exit_mod > 1) |
48e1416a | 516 | apply_opt_in_copies (opt_info, exit_mod, false, false); |
517 | ||
f9cce2dc | 518 | desc->noloop_assumptions = NULL_RTX; |
519 | desc->niter -= exit_mod; | |
e913b5cd | 520 | loop->nb_iterations_upper_bound -= exit_mod; |
a9ef9877 | 521 | if (loop->any_estimate |
796b6678 | 522 | && wi::leu_p (exit_mod, loop->nb_iterations_estimate)) |
e913b5cd | 523 | loop->nb_iterations_estimate -= exit_mod; |
a9ef9877 | 524 | else |
525 | loop->any_estimate = false; | |
f9cce2dc | 526 | } |
ad5201d0 | 527 | |
08b7917c | 528 | bitmap_set_bit (wont_exit, 1); |
ad5201d0 | 529 | } |
530 | else | |
531 | { | |
532 | /* Leave exit test in last copy, for the same reason as above if | |
533 | the loop tests the condition at the end of loop body. */ | |
534 | ||
450d042a | 535 | if (dump_file) |
1c18ed19 | 536 | fprintf (dump_file, ";; Condition at end of loop.\n"); |
ad5201d0 | 537 | |
538 | /* We know that niter >= max_unroll + 2; so we do not need to care of | |
539 | case when we would exit before reaching the loop. So just peel | |
f9cce2dc | 540 | exit_mod + 1 iterations. */ |
541 | if (exit_mod != max_unroll | |
542 | || desc->noloop_assumptions) | |
ad5201d0 | 543 | { |
08b7917c | 544 | bitmap_clear_bit (wont_exit, 0); |
f9cce2dc | 545 | if (desc->noloop_assumptions) |
08b7917c | 546 | bitmap_clear_bit (wont_exit, 1); |
48e1416a | 547 | |
375bb675 | 548 | opt_info_start_duplication (opt_info); |
a53ff4c1 | 549 | ok = duplicate_loop_to_header_edge (loop, loop_preheader_edge (loop), |
7194de72 | 550 | exit_mod + 1, |
a53ff4c1 | 551 | wont_exit, desc->out_edge, |
f3c40e6d | 552 | &remove_edges, |
75cc36a4 | 553 | DLTHE_FLAG_UPDATE_FREQ |
554 | | (opt_info && exit_mod > 0 | |
555 | ? DLTHE_RECORD_COPY_NUMBER | |
556 | : 0)); | |
a53ff4c1 | 557 | gcc_assert (ok); |
48e1416a | 558 | |
375bb675 | 559 | if (opt_info && exit_mod > 0) |
560 | apply_opt_in_copies (opt_info, exit_mod + 1, false, false); | |
a9989fb4 | 561 | |
f9cce2dc | 562 | desc->niter -= exit_mod + 1; |
e913b5cd | 563 | loop->nb_iterations_upper_bound -= exit_mod + 1; |
a9ef9877 | 564 | if (loop->any_estimate |
796b6678 | 565 | && wi::leu_p (exit_mod + 1, loop->nb_iterations_estimate)) |
e913b5cd | 566 | loop->nb_iterations_estimate -= exit_mod + 1; |
a9ef9877 | 567 | else |
568 | loop->any_estimate = false; | |
f9cce2dc | 569 | desc->noloop_assumptions = NULL_RTX; |
570 | ||
08b7917c | 571 | bitmap_set_bit (wont_exit, 0); |
572 | bitmap_set_bit (wont_exit, 1); | |
ad5201d0 | 573 | } |
574 | ||
08b7917c | 575 | bitmap_clear_bit (wont_exit, max_unroll); |
ad5201d0 | 576 | } |
577 | ||
578 | /* Now unroll the loop. */ | |
48e1416a | 579 | |
375bb675 | 580 | opt_info_start_duplication (opt_info); |
a53ff4c1 | 581 | ok = duplicate_loop_to_header_edge (loop, loop_latch_edge (loop), |
7194de72 | 582 | max_unroll, |
a53ff4c1 | 583 | wont_exit, desc->out_edge, |
f3c40e6d | 584 | &remove_edges, |
75cc36a4 | 585 | DLTHE_FLAG_UPDATE_FREQ |
586 | | (opt_info | |
587 | ? DLTHE_RECORD_COPY_NUMBER | |
588 | : 0)); | |
a53ff4c1 | 589 | gcc_assert (ok); |
ad5201d0 | 590 | |
375bb675 | 591 | if (opt_info) |
a9989fb4 | 592 | { |
375bb675 | 593 | apply_opt_in_copies (opt_info, max_unroll, true, true); |
594 | free_opt_info (opt_info); | |
a9989fb4 | 595 | } |
596 | ||
ad5201d0 | 597 | free (wont_exit); |
598 | ||
f9cce2dc | 599 | if (exit_at_end) |
600 | { | |
01020a5f | 601 | basic_block exit_block = get_bb_copy (desc->in_edge->src); |
f9cce2dc | 602 | /* Find a new in and out edge; they are in the last copy we have made. */ |
48e1416a | 603 | |
cd665a06 | 604 | if (EDGE_SUCC (exit_block, 0)->dest == desc->out_edge->dest) |
f9cce2dc | 605 | { |
cd665a06 | 606 | desc->out_edge = EDGE_SUCC (exit_block, 0); |
607 | desc->in_edge = EDGE_SUCC (exit_block, 1); | |
f9cce2dc | 608 | } |
609 | else | |
610 | { | |
cd665a06 | 611 | desc->out_edge = EDGE_SUCC (exit_block, 1); |
612 | desc->in_edge = EDGE_SUCC (exit_block, 0); | |
f9cce2dc | 613 | } |
614 | } | |
615 | ||
616 | desc->niter /= max_unroll + 1; | |
a9ef9877 | 617 | loop->nb_iterations_upper_bound |
796b6678 | 618 | = wi::udiv_trunc (loop->nb_iterations_upper_bound, max_unroll + 1); |
a9ef9877 | 619 | if (loop->any_estimate) |
620 | loop->nb_iterations_estimate | |
796b6678 | 621 | = wi::udiv_trunc (loop->nb_iterations_estimate, max_unroll + 1); |
f9cce2dc | 622 | desc->niter_expr = GEN_INT (desc->niter); |
623 | ||
ad5201d0 | 624 | /* Remove the edges. */ |
f1f41a6c | 625 | FOR_EACH_VEC_ELT (remove_edges, i, e) |
f3c40e6d | 626 | remove_path (e); |
ad5201d0 | 627 | |
450d042a | 628 | if (dump_file) |
629 | fprintf (dump_file, | |
630 | ";; Unrolled loop %d times, constant # of iterations %i insns\n", | |
631 | max_unroll, num_loop_insns (loop)); | |
ad5201d0 | 632 | } |
633 | ||
634 | /* Decide whether to unroll LOOP iterating runtime computable number of times | |
635 | and how much. */ | |
636 | static void | |
0051c76a | 637 | decide_unroll_runtime_iterations (struct loop *loop, int flags) |
ad5201d0 | 638 | { |
639 | unsigned nunroll, nunroll_by_av, i; | |
f9cce2dc | 640 | struct niter_desc *desc; |
5de9d3ed | 641 | widest_int iterations; |
ad5201d0 | 642 | |
643 | if (!(flags & UAP_UNROLL)) | |
644 | { | |
645 | /* We were not asked to, just return back silently. */ | |
646 | return; | |
647 | } | |
648 | ||
450d042a | 649 | if (dump_file) |
650 | fprintf (dump_file, | |
651 | "\n;; Considering unrolling loop with runtime " | |
652 | "computable number of iterations\n"); | |
ad5201d0 | 653 | |
654 | /* nunroll = total number of copies of the original loop body in | |
655 | unrolled loop (i.e. if it is 2, we have to duplicate loop body once. */ | |
656 | nunroll = PARAM_VALUE (PARAM_MAX_UNROLLED_INSNS) / loop->ninsns; | |
657 | nunroll_by_av = PARAM_VALUE (PARAM_MAX_AVERAGE_UNROLLED_INSNS) / loop->av_ninsns; | |
658 | if (nunroll > nunroll_by_av) | |
659 | nunroll = nunroll_by_av; | |
660 | if (nunroll > (unsigned) PARAM_VALUE (PARAM_MAX_UNROLL_TIMES)) | |
661 | nunroll = PARAM_VALUE (PARAM_MAX_UNROLL_TIMES); | |
662 | ||
9ccaa774 | 663 | if (targetm.loop_unroll_adjust) |
664 | nunroll = targetm.loop_unroll_adjust (nunroll, loop); | |
665 | ||
ad5201d0 | 666 | /* Skip big loops. */ |
667 | if (nunroll <= 1) | |
668 | { | |
450d042a | 669 | if (dump_file) |
670 | fprintf (dump_file, ";; Not considering loop, is too big\n"); | |
ad5201d0 | 671 | return; |
672 | } | |
673 | ||
674 | /* Check for simple loops. */ | |
f9cce2dc | 675 | desc = get_simple_loop_desc (loop); |
ad5201d0 | 676 | |
677 | /* Check simpleness. */ | |
f9cce2dc | 678 | if (!desc->simple_p || desc->assumptions) |
ad5201d0 | 679 | { |
450d042a | 680 | if (dump_file) |
681 | fprintf (dump_file, | |
682 | ";; Unable to prove that the number of iterations " | |
683 | "can be counted in runtime\n"); | |
ad5201d0 | 684 | return; |
685 | } | |
686 | ||
f9cce2dc | 687 | if (desc->const_iter) |
ad5201d0 | 688 | { |
450d042a | 689 | if (dump_file) |
690 | fprintf (dump_file, ";; Loop iterates constant times\n"); | |
ad5201d0 | 691 | return; |
692 | } | |
693 | ||
204a2e45 | 694 | /* Check whether the loop rolls. */ |
f86b328b | 695 | if ((get_estimated_loop_iterations (loop, &iterations) |
696 | || get_max_loop_iterations (loop, &iterations)) | |
796b6678 | 697 | && wi::ltu_p (iterations, 2 * nunroll)) |
ad5201d0 | 698 | { |
450d042a | 699 | if (dump_file) |
700 | fprintf (dump_file, ";; Not unrolling loop, doesn't roll\n"); | |
ad5201d0 | 701 | return; |
702 | } | |
703 | ||
704 | /* Success; now force nunroll to be power of 2, as we are unable to | |
705 | cope with overflows in computation of number of iterations. */ | |
f9cce2dc | 706 | for (i = 1; 2 * i <= nunroll; i *= 2) |
707 | continue; | |
ad5201d0 | 708 | |
709 | loop->lpt_decision.decision = LPT_UNROLL_RUNTIME; | |
710 | loop->lpt_decision.times = i - 1; | |
711 | } | |
712 | ||
d6a1bbdb | 713 | /* Splits edge E and inserts the sequence of instructions INSNS on it, and |
714 | returns the newly created block. If INSNS is NULL_RTX, nothing is changed | |
715 | and NULL is returned instead. */ | |
88e6f696 | 716 | |
717 | basic_block | |
222dc1d3 | 718 | split_edge_and_insert (edge e, rtx_insn *insns) |
88e6f696 | 719 | { |
d6a1bbdb | 720 | basic_block bb; |
721 | ||
722 | if (!insns) | |
723 | return NULL; | |
48e1416a | 724 | bb = split_edge (e); |
88e6f696 | 725 | emit_insn_after (insns, BB_END (bb)); |
db1c50be | 726 | |
727 | /* ??? We used to assume that INSNS can contain control flow insns, and | |
728 | that we had to try to find sub basic blocks in BB to maintain a valid | |
729 | CFG. For this purpose we used to set the BB_SUPERBLOCK flag on BB | |
730 | and call break_superblocks when going out of cfglayout mode. But it | |
731 | turns out that this never happens; and that if it does ever happen, | |
8b88439e | 732 | the verify_flow_info at the end of the RTL loop passes would fail. |
db1c50be | 733 | |
734 | There are two reasons why we expected we could have control flow insns | |
735 | in INSNS. The first is when a comparison has to be done in parts, and | |
736 | the second is when the number of iterations is computed for loops with | |
737 | the number of iterations known at runtime. In both cases, test cases | |
738 | to get control flow in INSNS appear to be impossible to construct: | |
739 | ||
740 | * If do_compare_rtx_and_jump needs several branches to do comparison | |
741 | in a mode that needs comparison by parts, we cannot analyze the | |
742 | number of iterations of the loop, and we never get to unrolling it. | |
743 | ||
744 | * The code in expand_divmod that was suspected to cause creation of | |
745 | branching code seems to be only accessed for signed division. The | |
746 | divisions used by # of iterations analysis are always unsigned. | |
747 | Problems might arise on architectures that emits branching code | |
748 | for some operations that may appear in the unroller (especially | |
749 | for division), but we have no such architectures. | |
750 | ||
751 | Considering all this, it was decided that we should for now assume | |
752 | that INSNS can in theory contain control flow insns, but in practice | |
753 | it never does. So we don't handle the theoretical case, and should | |
754 | a real failure ever show up, we have a pretty good clue for how to | |
755 | fix it. */ | |
756 | ||
88e6f696 | 757 | return bb; |
758 | } | |
759 | ||
b99ab275 | 760 | /* Prepare a sequence comparing OP0 with OP1 using COMP and jumping to LABEL if |
761 | true, with probability PROB. If CINSN is not NULL, it is the insn to copy | |
762 | in order to create a jump. */ | |
763 | ||
222dc1d3 | 764 | static rtx_insn * |
f9a00e9e | 765 | compare_and_jump_seq (rtx op0, rtx op1, enum rtx_code comp, |
766 | rtx_code_label *label, int prob, rtx_insn *cinsn) | |
b99ab275 | 767 | { |
f9a00e9e | 768 | rtx_insn *seq; |
769 | rtx_jump_insn *jump; | |
222dc1d3 | 770 | rtx cond; |
3754d046 | 771 | machine_mode mode; |
b99ab275 | 772 | |
773 | mode = GET_MODE (op0); | |
774 | if (mode == VOIDmode) | |
775 | mode = GET_MODE (op1); | |
776 | ||
777 | start_sequence (); | |
778 | if (GET_MODE_CLASS (mode) == MODE_CC) | |
779 | { | |
780 | /* A hack -- there seems to be no easy generic way how to make a | |
781 | conditional jump from a ccmode comparison. */ | |
782 | gcc_assert (cinsn); | |
783 | cond = XEXP (SET_SRC (pc_set (cinsn)), 0); | |
784 | gcc_assert (GET_CODE (cond) == comp); | |
785 | gcc_assert (rtx_equal_p (op0, XEXP (cond, 0))); | |
786 | gcc_assert (rtx_equal_p (op1, XEXP (cond, 1))); | |
787 | emit_jump_insn (copy_insn (PATTERN (cinsn))); | |
f9a00e9e | 788 | jump = as_a <rtx_jump_insn *> (get_last_insn ()); |
b99ab275 | 789 | JUMP_LABEL (jump) = JUMP_LABEL (cinsn); |
790 | LABEL_NUSES (JUMP_LABEL (jump))++; | |
791 | redirect_jump (jump, label, 0); | |
792 | } | |
793 | else | |
794 | { | |
795 | gcc_assert (!cinsn); | |
796 | ||
797 | op0 = force_operand (op0, NULL_RTX); | |
798 | op1 = force_operand (op1, NULL_RTX); | |
799 | do_compare_rtx_and_jump (op0, op1, comp, 0, | |
f9a00e9e | 800 | mode, NULL_RTX, NULL, label, -1); |
801 | jump = as_a <rtx_jump_insn *> (get_last_insn ()); | |
802 | jump->set_jump_target (label); | |
b99ab275 | 803 | LABEL_NUSES (label)++; |
804 | } | |
805 | add_int_reg_note (jump, REG_BR_PROB, prob); | |
806 | ||
807 | seq = get_insns (); | |
808 | end_sequence (); | |
809 | ||
810 | return seq; | |
811 | } | |
812 | ||
1c18ed19 | 813 | /* Unroll LOOP for which we are able to count number of iterations in runtime |
814 | LOOP->LPT_DECISION.TIMES times. The transformation does this (with some | |
ad5201d0 | 815 | extra care for case n < 0): |
3ad4992f | 816 | |
ad5201d0 | 817 | for (i = 0; i < n; i++) |
818 | body; | |
3ad4992f | 819 | |
1c18ed19 | 820 | ==> (LOOP->LPT_DECISION.TIMES == 3) |
3ad4992f | 821 | |
ad5201d0 | 822 | i = 0; |
823 | mod = n % 4; | |
3ad4992f | 824 | |
ad5201d0 | 825 | switch (mod) |
826 | { | |
827 | case 3: | |
828 | body; i++; | |
829 | case 2: | |
830 | body; i++; | |
831 | case 1: | |
832 | body; i++; | |
833 | case 0: ; | |
834 | } | |
3ad4992f | 835 | |
ad5201d0 | 836 | while (i < n) |
837 | { | |
838 | body; i++; | |
839 | body; i++; | |
840 | body; i++; | |
841 | body; i++; | |
842 | } | |
843 | */ | |
844 | static void | |
7194de72 | 845 | unroll_loop_runtime_iterations (struct loop *loop) |
ad5201d0 | 846 | { |
222dc1d3 | 847 | rtx old_niter, niter, tmp; |
848 | rtx_insn *init_code, *branch_code; | |
ad5201d0 | 849 | unsigned i, j, p; |
3f9439d7 | 850 | basic_block preheader, *body, swtch, ezc_swtch; |
ad5201d0 | 851 | sbitmap wont_exit; |
852 | int may_exit_copy; | |
f3c40e6d | 853 | unsigned n_peel; |
f3c40e6d | 854 | edge e; |
ad5201d0 | 855 | bool extra_zero_check, last_may_exit; |
856 | unsigned max_unroll = loop->lpt_decision.times; | |
f9cce2dc | 857 | struct niter_desc *desc = get_simple_loop_desc (loop); |
858 | bool exit_at_end = loop_exit_at_end_p (loop); | |
375bb675 | 859 | struct opt_info *opt_info = NULL; |
a53ff4c1 | 860 | bool ok; |
48e1416a | 861 | |
375bb675 | 862 | if (flag_split_ivs_in_unroller |
863 | || flag_variable_expansion_in_unroller) | |
864 | opt_info = analyze_insns_in_loop (loop); | |
48e1416a | 865 | |
ad5201d0 | 866 | /* Remember blocks whose dominators will have to be updated. */ |
c2078b80 | 867 | auto_vec<basic_block> dom_bbs; |
ad5201d0 | 868 | |
869 | body = get_loop_body (loop); | |
870 | for (i = 0; i < loop->num_nodes; i++) | |
871 | { | |
f1f41a6c | 872 | vec<basic_block> ldom; |
3f9439d7 | 873 | basic_block bb; |
ad5201d0 | 874 | |
3f9439d7 | 875 | ldom = get_dominated_by (CDI_DOMINATORS, body[i]); |
f1f41a6c | 876 | FOR_EACH_VEC_ELT (ldom, j, bb) |
3f9439d7 | 877 | if (!flow_bb_inside_loop_p (loop, bb)) |
f1f41a6c | 878 | dom_bbs.safe_push (bb); |
ad5201d0 | 879 | |
f1f41a6c | 880 | ldom.release (); |
ad5201d0 | 881 | } |
882 | free (body); | |
883 | ||
f9cce2dc | 884 | if (!exit_at_end) |
ad5201d0 | 885 | { |
886 | /* Leave exit in first copy (for explanation why see comment in | |
887 | unroll_loop_constant_iterations). */ | |
888 | may_exit_copy = 0; | |
889 | n_peel = max_unroll - 1; | |
890 | extra_zero_check = true; | |
891 | last_may_exit = false; | |
892 | } | |
893 | else | |
894 | { | |
895 | /* Leave exit in last copy (for explanation why see comment in | |
896 | unroll_loop_constant_iterations). */ | |
897 | may_exit_copy = max_unroll; | |
898 | n_peel = max_unroll; | |
899 | extra_zero_check = false; | |
900 | last_may_exit = true; | |
901 | } | |
902 | ||
903 | /* Get expression for number of iterations. */ | |
904 | start_sequence (); | |
f9cce2dc | 905 | old_niter = niter = gen_reg_rtx (desc->mode); |
906 | tmp = force_operand (copy_rtx (desc->niter_expr), niter); | |
907 | if (tmp != niter) | |
908 | emit_move_insn (niter, tmp); | |
ad5201d0 | 909 | |
910 | /* Count modulo by ANDing it with max_unroll; we use the fact that | |
911 | the number of unrollings is a power of two, and thus this is correct | |
912 | even if there is overflow in the computation. */ | |
f9cce2dc | 913 | niter = expand_simple_binop (desc->mode, AND, |
0359f9f5 | 914 | niter, gen_int_mode (max_unroll, desc->mode), |
ad5201d0 | 915 | NULL_RTX, 0, OPTAB_LIB_WIDEN); |
916 | ||
917 | init_code = get_insns (); | |
918 | end_sequence (); | |
ffbd194b | 919 | unshare_all_rtl_in_chain (init_code); |
ad5201d0 | 920 | |
921 | /* Precondition the loop. */ | |
88e6f696 | 922 | split_edge_and_insert (loop_preheader_edge (loop), init_code); |
ad5201d0 | 923 | |
c2078b80 | 924 | auto_vec<edge> remove_edges; |
ad5201d0 | 925 | |
926 | wont_exit = sbitmap_alloc (max_unroll + 2); | |
927 | ||
928 | /* Peel the first copy of loop body (almost always we must leave exit test | |
929 | here; the only exception is when we have extra zero check and the number | |
f9cce2dc | 930 | of iterations is reliable. Also record the place of (possible) extra |
931 | zero check. */ | |
53c5d9d4 | 932 | bitmap_clear (wont_exit); |
f9cce2dc | 933 | if (extra_zero_check |
934 | && !desc->noloop_assumptions) | |
08b7917c | 935 | bitmap_set_bit (wont_exit, 1); |
ad5201d0 | 936 | ezc_swtch = loop_preheader_edge (loop)->src; |
a53ff4c1 | 937 | ok = duplicate_loop_to_header_edge (loop, loop_preheader_edge (loop), |
7194de72 | 938 | 1, wont_exit, desc->out_edge, |
f3c40e6d | 939 | &remove_edges, |
a53ff4c1 | 940 | DLTHE_FLAG_UPDATE_FREQ); |
941 | gcc_assert (ok); | |
ad5201d0 | 942 | |
943 | /* Record the place where switch will be built for preconditioning. */ | |
88e6f696 | 944 | swtch = split_edge (loop_preheader_edge (loop)); |
ad5201d0 | 945 | |
946 | for (i = 0; i < n_peel; i++) | |
947 | { | |
948 | /* Peel the copy. */ | |
53c5d9d4 | 949 | bitmap_clear (wont_exit); |
ad5201d0 | 950 | if (i != n_peel - 1 || !last_may_exit) |
08b7917c | 951 | bitmap_set_bit (wont_exit, 1); |
a53ff4c1 | 952 | ok = duplicate_loop_to_header_edge (loop, loop_preheader_edge (loop), |
7194de72 | 953 | 1, wont_exit, desc->out_edge, |
f3c40e6d | 954 | &remove_edges, |
a53ff4c1 | 955 | DLTHE_FLAG_UPDATE_FREQ); |
956 | gcc_assert (ok); | |
ad5201d0 | 957 | |
c87a3eff | 958 | /* Create item for switch. */ |
959 | j = n_peel - i - (extra_zero_check ? 0 : 1); | |
960 | p = REG_BR_PROB_BASE / (i + 2); | |
961 | ||
88e6f696 | 962 | preheader = split_edge (loop_preheader_edge (loop)); |
f9cce2dc | 963 | branch_code = compare_and_jump_seq (copy_rtx (niter), GEN_INT (j), EQ, |
a53ff4c1 | 964 | block_label (preheader), p, |
222dc1d3 | 965 | NULL); |
c87a3eff | 966 | |
d6a1bbdb | 967 | /* We rely on the fact that the compare and jump cannot be optimized out, |
968 | and hence the cfg we create is correct. */ | |
969 | gcc_assert (branch_code != NULL_RTX); | |
970 | ||
88e6f696 | 971 | swtch = split_edge_and_insert (single_pred_edge (swtch), branch_code); |
0051c76a | 972 | set_immediate_dominator (CDI_DOMINATORS, preheader, swtch); |
ea091dfd | 973 | single_pred_edge (swtch)->probability = REG_BR_PROB_BASE - p; |
c87a3eff | 974 | e = make_edge (swtch, preheader, |
ea091dfd | 975 | single_succ_edge (swtch)->flags & EDGE_IRREDUCIBLE_LOOP); |
a9ef9877 | 976 | e->count = RDIV (preheader->count * REG_BR_PROB_BASE, p); |
c87a3eff | 977 | e->probability = p; |
ad5201d0 | 978 | } |
979 | ||
980 | if (extra_zero_check) | |
981 | { | |
982 | /* Add branch for zero iterations. */ | |
983 | p = REG_BR_PROB_BASE / (max_unroll + 1); | |
984 | swtch = ezc_swtch; | |
88e6f696 | 985 | preheader = split_edge (loop_preheader_edge (loop)); |
f9cce2dc | 986 | branch_code = compare_and_jump_seq (copy_rtx (niter), const0_rtx, EQ, |
a53ff4c1 | 987 | block_label (preheader), p, |
222dc1d3 | 988 | NULL); |
d6a1bbdb | 989 | gcc_assert (branch_code != NULL_RTX); |
ad5201d0 | 990 | |
88e6f696 | 991 | swtch = split_edge_and_insert (single_succ_edge (swtch), branch_code); |
0051c76a | 992 | set_immediate_dominator (CDI_DOMINATORS, preheader, swtch); |
ea091dfd | 993 | single_succ_edge (swtch)->probability = REG_BR_PROB_BASE - p; |
a964683a | 994 | e = make_edge (swtch, preheader, |
ea091dfd | 995 | single_succ_edge (swtch)->flags & EDGE_IRREDUCIBLE_LOOP); |
a9ef9877 | 996 | e->count = RDIV (preheader->count * REG_BR_PROB_BASE, p); |
ad5201d0 | 997 | e->probability = p; |
998 | } | |
999 | ||
1000 | /* Recount dominators for outer blocks. */ | |
3f9439d7 | 1001 | iterate_fix_dominators (CDI_DOMINATORS, dom_bbs, false); |
ad5201d0 | 1002 | |
1003 | /* And unroll loop. */ | |
1004 | ||
53c5d9d4 | 1005 | bitmap_ones (wont_exit); |
08b7917c | 1006 | bitmap_clear_bit (wont_exit, may_exit_copy); |
375bb675 | 1007 | opt_info_start_duplication (opt_info); |
48e1416a | 1008 | |
a53ff4c1 | 1009 | ok = duplicate_loop_to_header_edge (loop, loop_latch_edge (loop), |
7194de72 | 1010 | max_unroll, |
a53ff4c1 | 1011 | wont_exit, desc->out_edge, |
f3c40e6d | 1012 | &remove_edges, |
75cc36a4 | 1013 | DLTHE_FLAG_UPDATE_FREQ |
1014 | | (opt_info | |
1015 | ? DLTHE_RECORD_COPY_NUMBER | |
1016 | : 0)); | |
a53ff4c1 | 1017 | gcc_assert (ok); |
48e1416a | 1018 | |
375bb675 | 1019 | if (opt_info) |
a9989fb4 | 1020 | { |
375bb675 | 1021 | apply_opt_in_copies (opt_info, max_unroll, true, true); |
1022 | free_opt_info (opt_info); | |
a9989fb4 | 1023 | } |
1024 | ||
ad5201d0 | 1025 | free (wont_exit); |
1026 | ||
f9cce2dc | 1027 | if (exit_at_end) |
1028 | { | |
01020a5f | 1029 | basic_block exit_block = get_bb_copy (desc->in_edge->src); |
a53ff4c1 | 1030 | /* Find a new in and out edge; they are in the last copy we have |
1031 | made. */ | |
48e1416a | 1032 | |
cd665a06 | 1033 | if (EDGE_SUCC (exit_block, 0)->dest == desc->out_edge->dest) |
f9cce2dc | 1034 | { |
cd665a06 | 1035 | desc->out_edge = EDGE_SUCC (exit_block, 0); |
1036 | desc->in_edge = EDGE_SUCC (exit_block, 1); | |
f9cce2dc | 1037 | } |
1038 | else | |
1039 | { | |
cd665a06 | 1040 | desc->out_edge = EDGE_SUCC (exit_block, 1); |
1041 | desc->in_edge = EDGE_SUCC (exit_block, 0); | |
f9cce2dc | 1042 | } |
1043 | } | |
1044 | ||
ad5201d0 | 1045 | /* Remove the edges. */ |
f1f41a6c | 1046 | FOR_EACH_VEC_ELT (remove_edges, i, e) |
f3c40e6d | 1047 | remove_path (e); |
ad5201d0 | 1048 | |
f9cce2dc | 1049 | /* We must be careful when updating the number of iterations due to |
1050 | preconditioning and the fact that the value must be valid at entry | |
1051 | of the loop. After passing through the above code, we see that | |
1052 | the correct new number of iterations is this: */ | |
a9989fb4 | 1053 | gcc_assert (!desc->const_iter); |
f9cce2dc | 1054 | desc->niter_expr = |
a53ff4c1 | 1055 | simplify_gen_binary (UDIV, desc->mode, old_niter, |
5d5ee71f | 1056 | gen_int_mode (max_unroll + 1, desc->mode)); |
a9ef9877 | 1057 | loop->nb_iterations_upper_bound |
796b6678 | 1058 | = wi::udiv_trunc (loop->nb_iterations_upper_bound, max_unroll + 1); |
a9ef9877 | 1059 | if (loop->any_estimate) |
1060 | loop->nb_iterations_estimate | |
796b6678 | 1061 | = wi::udiv_trunc (loop->nb_iterations_estimate, max_unroll + 1); |
f9cce2dc | 1062 | if (exit_at_end) |
1063 | { | |
1064 | desc->niter_expr = | |
1065 | simplify_gen_binary (MINUS, desc->mode, desc->niter_expr, const1_rtx); | |
1066 | desc->noloop_assumptions = NULL_RTX; | |
a9ef9877 | 1067 | --loop->nb_iterations_upper_bound; |
1068 | if (loop->any_estimate | |
e913b5cd | 1069 | && loop->nb_iterations_estimate != 0) |
a9ef9877 | 1070 | --loop->nb_iterations_estimate; |
1071 | else | |
1072 | loop->any_estimate = false; | |
f9cce2dc | 1073 | } |
1074 | ||
450d042a | 1075 | if (dump_file) |
1076 | fprintf (dump_file, | |
1077 | ";; Unrolled loop %d times, counting # of iterations " | |
1078 | "in runtime, %i insns\n", | |
ad5201d0 | 1079 | max_unroll, num_loop_insns (loop)); |
1080 | } | |
3ad4992f | 1081 | |
ad5201d0 | 1082 | /* Decide whether to unroll LOOP stupidly and how much. */ |
1083 | static void | |
0051c76a | 1084 | decide_unroll_stupid (struct loop *loop, int flags) |
ad5201d0 | 1085 | { |
1086 | unsigned nunroll, nunroll_by_av, i; | |
f9cce2dc | 1087 | struct niter_desc *desc; |
5de9d3ed | 1088 | widest_int iterations; |
ad5201d0 | 1089 | |
1090 | if (!(flags & UAP_UNROLL_ALL)) | |
1091 | { | |
1092 | /* We were not asked to, just return back silently. */ | |
1093 | return; | |
1094 | } | |
1095 | ||
450d042a | 1096 | if (dump_file) |
1097 | fprintf (dump_file, "\n;; Considering unrolling loop stupidly\n"); | |
ad5201d0 | 1098 | |
1099 | /* nunroll = total number of copies of the original loop body in | |
1100 | unrolled loop (i.e. if it is 2, we have to duplicate loop body once. */ | |
1101 | nunroll = PARAM_VALUE (PARAM_MAX_UNROLLED_INSNS) / loop->ninsns; | |
450d042a | 1102 | nunroll_by_av |
1103 | = PARAM_VALUE (PARAM_MAX_AVERAGE_UNROLLED_INSNS) / loop->av_ninsns; | |
ad5201d0 | 1104 | if (nunroll > nunroll_by_av) |
1105 | nunroll = nunroll_by_av; | |
1106 | if (nunroll > (unsigned) PARAM_VALUE (PARAM_MAX_UNROLL_TIMES)) | |
1107 | nunroll = PARAM_VALUE (PARAM_MAX_UNROLL_TIMES); | |
1108 | ||
9ccaa774 | 1109 | if (targetm.loop_unroll_adjust) |
1110 | nunroll = targetm.loop_unroll_adjust (nunroll, loop); | |
1111 | ||
ad5201d0 | 1112 | /* Skip big loops. */ |
1113 | if (nunroll <= 1) | |
1114 | { | |
450d042a | 1115 | if (dump_file) |
1116 | fprintf (dump_file, ";; Not considering loop, is too big\n"); | |
ad5201d0 | 1117 | return; |
1118 | } | |
1119 | ||
1120 | /* Check for simple loops. */ | |
f9cce2dc | 1121 | desc = get_simple_loop_desc (loop); |
ad5201d0 | 1122 | |
1123 | /* Check simpleness. */ | |
f9cce2dc | 1124 | if (desc->simple_p && !desc->assumptions) |
ad5201d0 | 1125 | { |
450d042a | 1126 | if (dump_file) |
1127 | fprintf (dump_file, ";; The loop is simple\n"); | |
ad5201d0 | 1128 | return; |
1129 | } | |
1130 | ||
1131 | /* Do not unroll loops with branches inside -- it increases number | |
d9459f6b | 1132 | of mispredicts. |
1133 | TODO: this heuristic needs tunning; call inside the loop body | |
1134 | is also relatively good reason to not unroll. */ | |
f9cce2dc | 1135 | if (num_loop_branches (loop) > 1) |
ad5201d0 | 1136 | { |
450d042a | 1137 | if (dump_file) |
1138 | fprintf (dump_file, ";; Not unrolling, contains branches\n"); | |
ad5201d0 | 1139 | return; |
1140 | } | |
1141 | ||
204a2e45 | 1142 | /* Check whether the loop rolls. */ |
f86b328b | 1143 | if ((get_estimated_loop_iterations (loop, &iterations) |
1144 | || get_max_loop_iterations (loop, &iterations)) | |
796b6678 | 1145 | && wi::ltu_p (iterations, 2 * nunroll)) |
ad5201d0 | 1146 | { |
450d042a | 1147 | if (dump_file) |
1148 | fprintf (dump_file, ";; Not unrolling loop, doesn't roll\n"); | |
ad5201d0 | 1149 | return; |
1150 | } | |
1151 | ||
1152 | /* Success. Now force nunroll to be power of 2, as it seems that this | |
d01481af | 1153 | improves results (partially because of better alignments, partially |
ad5201d0 | 1154 | because of some dark magic). */ |
f9cce2dc | 1155 | for (i = 1; 2 * i <= nunroll; i *= 2) |
1156 | continue; | |
ad5201d0 | 1157 | |
1158 | loop->lpt_decision.decision = LPT_UNROLL_STUPID; | |
1159 | loop->lpt_decision.times = i - 1; | |
1160 | } | |
1161 | ||
1c18ed19 | 1162 | /* Unroll a LOOP LOOP->LPT_DECISION.TIMES times. The transformation does this: |
1163 | ||
ad5201d0 | 1164 | while (cond) |
1165 | body; | |
1166 | ||
1c18ed19 | 1167 | ==> (LOOP->LPT_DECISION.TIMES == 3) |
ad5201d0 | 1168 | |
1169 | while (cond) | |
1170 | { | |
1171 | body; | |
1172 | if (!cond) break; | |
1173 | body; | |
1174 | if (!cond) break; | |
1175 | body; | |
1176 | if (!cond) break; | |
1177 | body; | |
1178 | } | |
1179 | */ | |
1180 | static void | |
7194de72 | 1181 | unroll_loop_stupid (struct loop *loop) |
ad5201d0 | 1182 | { |
1183 | sbitmap wont_exit; | |
1184 | unsigned nunroll = loop->lpt_decision.times; | |
f9cce2dc | 1185 | struct niter_desc *desc = get_simple_loop_desc (loop); |
375bb675 | 1186 | struct opt_info *opt_info = NULL; |
a53ff4c1 | 1187 | bool ok; |
48e1416a | 1188 | |
375bb675 | 1189 | if (flag_split_ivs_in_unroller |
1190 | || flag_variable_expansion_in_unroller) | |
1191 | opt_info = analyze_insns_in_loop (loop); | |
48e1416a | 1192 | |
1193 | ||
ad5201d0 | 1194 | wont_exit = sbitmap_alloc (nunroll + 1); |
53c5d9d4 | 1195 | bitmap_clear (wont_exit); |
375bb675 | 1196 | opt_info_start_duplication (opt_info); |
48e1416a | 1197 | |
a53ff4c1 | 1198 | ok = duplicate_loop_to_header_edge (loop, loop_latch_edge (loop), |
7194de72 | 1199 | nunroll, wont_exit, |
f3c40e6d | 1200 | NULL, NULL, |
75cc36a4 | 1201 | DLTHE_FLAG_UPDATE_FREQ |
1202 | | (opt_info | |
1203 | ? DLTHE_RECORD_COPY_NUMBER | |
1204 | : 0)); | |
a53ff4c1 | 1205 | gcc_assert (ok); |
48e1416a | 1206 | |
375bb675 | 1207 | if (opt_info) |
a9989fb4 | 1208 | { |
375bb675 | 1209 | apply_opt_in_copies (opt_info, nunroll, true, true); |
1210 | free_opt_info (opt_info); | |
a9989fb4 | 1211 | } |
1212 | ||
ad5201d0 | 1213 | free (wont_exit); |
3ad4992f | 1214 | |
f9cce2dc | 1215 | if (desc->simple_p) |
1216 | { | |
1217 | /* We indeed may get here provided that there are nontrivial assumptions | |
1218 | for a loop to be really simple. We could update the counts, but the | |
1219 | problem is that we are unable to decide which exit will be taken | |
1220 | (not really true in case the number of iterations is constant, | |
75de4aa2 | 1221 | but no one will do anything with this information, so we do not |
f9cce2dc | 1222 | worry about it). */ |
1223 | desc->simple_p = false; | |
1224 | } | |
1225 | ||
450d042a | 1226 | if (dump_file) |
1227 | fprintf (dump_file, ";; Unrolled loop %d times, %i insns\n", | |
ad5201d0 | 1228 | nunroll, num_loop_insns (loop)); |
1229 | } | |
a9989fb4 | 1230 | |
3b3940d7 | 1231 | /* Returns true if REG is referenced in one nondebug insn in LOOP. |
1232 | Set *DEBUG_USES to the number of debug insns that reference the | |
1233 | variable. */ | |
375bb675 | 1234 | |
f7b51129 | 1235 | static bool |
3b3940d7 | 1236 | referenced_in_one_insn_in_loop_p (struct loop *loop, rtx reg, |
1237 | int *debug_uses) | |
375bb675 | 1238 | { |
1239 | basic_block *body, bb; | |
1240 | unsigned i; | |
1241 | int count_ref = 0; | |
222dc1d3 | 1242 | rtx_insn *insn; |
48e1416a | 1243 | |
1244 | body = get_loop_body (loop); | |
375bb675 | 1245 | for (i = 0; i < loop->num_nodes; i++) |
1246 | { | |
1247 | bb = body[i]; | |
48e1416a | 1248 | |
375bb675 | 1249 | FOR_BB_INSNS (bb, insn) |
3b3940d7 | 1250 | if (!rtx_referenced_p (reg, insn)) |
1251 | continue; | |
1252 | else if (DEBUG_INSN_P (insn)) | |
1253 | ++*debug_uses; | |
1254 | else if (++count_ref > 1) | |
1255 | break; | |
375bb675 | 1256 | } |
3b3940d7 | 1257 | free (body); |
375bb675 | 1258 | return (count_ref == 1); |
1259 | } | |
1260 | ||
3b3940d7 | 1261 | /* Reset the DEBUG_USES debug insns in LOOP that reference REG. */ |
1262 | ||
1263 | static void | |
1264 | reset_debug_uses_in_loop (struct loop *loop, rtx reg, int debug_uses) | |
1265 | { | |
1266 | basic_block *body, bb; | |
1267 | unsigned i; | |
222dc1d3 | 1268 | rtx_insn *insn; |
3b3940d7 | 1269 | |
1270 | body = get_loop_body (loop); | |
1271 | for (i = 0; debug_uses && i < loop->num_nodes; i++) | |
1272 | { | |
1273 | bb = body[i]; | |
1274 | ||
1275 | FOR_BB_INSNS (bb, insn) | |
1276 | if (!DEBUG_INSN_P (insn) || !rtx_referenced_p (reg, insn)) | |
1277 | continue; | |
1278 | else | |
1279 | { | |
1280 | validate_change (insn, &INSN_VAR_LOCATION_LOC (insn), | |
1281 | gen_rtx_UNKNOWN_VAR_LOC (), 0); | |
1282 | if (!--debug_uses) | |
1283 | break; | |
1284 | } | |
1285 | } | |
1286 | free (body); | |
1287 | } | |
1288 | ||
375bb675 | 1289 | /* Determine whether INSN contains an accumulator |
48e1416a | 1290 | which can be expanded into separate copies, |
375bb675 | 1291 | one for each copy of the LOOP body. |
48e1416a | 1292 | |
375bb675 | 1293 | for (i = 0 ; i < n; i++) |
1294 | sum += a[i]; | |
48e1416a | 1295 | |
375bb675 | 1296 | ==> |
48e1416a | 1297 | |
375bb675 | 1298 | sum += a[i] |
1299 | .... | |
1300 | i = i+1; | |
1301 | sum1 += a[i] | |
1302 | .... | |
1303 | i = i+1 | |
1304 | sum2 += a[i]; | |
1305 | .... | |
1306 | ||
48e1416a | 1307 | Return NULL if INSN contains no opportunity for expansion of accumulator. |
1308 | Otherwise, allocate a VAR_TO_EXPAND structure, fill it with the relevant | |
375bb675 | 1309 | information and return a pointer to it. |
1310 | */ | |
1311 | ||
1312 | static struct var_to_expand * | |
222dc1d3 | 1313 | analyze_insn_to_expand_var (struct loop *loop, rtx_insn *insn) |
375bb675 | 1314 | { |
7497b98c | 1315 | rtx set, dest, src; |
375bb675 | 1316 | struct var_to_expand *ves; |
0def560a | 1317 | unsigned accum_pos; |
7497b98c | 1318 | enum rtx_code code; |
3b3940d7 | 1319 | int debug_uses = 0; |
0def560a | 1320 | |
375bb675 | 1321 | set = single_set (insn); |
1322 | if (!set) | |
1323 | return NULL; | |
48e1416a | 1324 | |
375bb675 | 1325 | dest = SET_DEST (set); |
1326 | src = SET_SRC (set); | |
7497b98c | 1327 | code = GET_CODE (src); |
48e1416a | 1328 | |
7497b98c | 1329 | if (code != PLUS && code != MINUS && code != MULT && code != FMA) |
375bb675 | 1330 | return NULL; |
1c6304f6 | 1331 | |
7497b98c | 1332 | if (FLOAT_MODE_P (GET_MODE (dest))) |
1333 | { | |
1334 | if (!flag_associative_math) | |
1335 | return NULL; | |
1336 | /* In the case of FMA, we're also changing the rounding. */ | |
1337 | if (code == FMA && !flag_unsafe_math_optimizations) | |
1338 | return NULL; | |
1339 | } | |
1340 | ||
1c6304f6 | 1341 | /* Hmm, this is a bit paradoxical. We know that INSN is a valid insn |
1342 | in MD. But if there is no optab to generate the insn, we can not | |
1343 | perform the variable expansion. This can happen if an MD provides | |
1344 | an insn but not a named pattern to generate it, for example to avoid | |
1345 | producing code that needs additional mode switches like for x87/mmx. | |
1346 | ||
1347 | So we check have_insn_for which looks for an optab for the operation | |
1348 | in SRC. If it doesn't exist, we can't perform the expansion even | |
1349 | though INSN is valid. */ | |
7497b98c | 1350 | if (!have_insn_for (code, GET_MODE (src))) |
1c6304f6 | 1351 | return NULL; |
1352 | ||
375bb675 | 1353 | if (!REG_P (dest) |
1354 | && !(GET_CODE (dest) == SUBREG | |
1355 | && REG_P (SUBREG_REG (dest)))) | |
1356 | return NULL; | |
48e1416a | 1357 | |
7497b98c | 1358 | /* Find the accumulator use within the operation. */ |
1359 | if (code == FMA) | |
1360 | { | |
1361 | /* We only support accumulation via FMA in the ADD position. */ | |
1362 | if (!rtx_equal_p (dest, XEXP (src, 2))) | |
1363 | return NULL; | |
1364 | accum_pos = 2; | |
1365 | } | |
1366 | else if (rtx_equal_p (dest, XEXP (src, 0))) | |
0def560a | 1367 | accum_pos = 0; |
7497b98c | 1368 | else if (rtx_equal_p (dest, XEXP (src, 1))) |
1369 | { | |
1370 | /* The method of expansion that we are using; which includes the | |
1371 | initialization of the expansions with zero and the summation of | |
1372 | the expansions at the end of the computation will yield wrong | |
1373 | results for (x = something - x) thus avoid using it in that case. */ | |
1374 | if (code == MINUS) | |
1375 | return NULL; | |
1376 | accum_pos = 1; | |
1377 | } | |
0def560a | 1378 | else |
1379 | return NULL; | |
1380 | ||
7497b98c | 1381 | /* It must not otherwise be used. */ |
1382 | if (code == FMA) | |
1383 | { | |
1384 | if (rtx_referenced_p (dest, XEXP (src, 0)) | |
1385 | || rtx_referenced_p (dest, XEXP (src, 1))) | |
1386 | return NULL; | |
1387 | } | |
1388 | else if (rtx_referenced_p (dest, XEXP (src, 1 - accum_pos))) | |
375bb675 | 1389 | return NULL; |
48e1416a | 1390 | |
7497b98c | 1391 | /* It must be used in exactly one insn. */ |
3b3940d7 | 1392 | if (!referenced_in_one_insn_in_loop_p (loop, dest, &debug_uses)) |
375bb675 | 1393 | return NULL; |
48e1416a | 1394 | |
e24a805c | 1395 | if (dump_file) |
7497b98c | 1396 | { |
1397 | fprintf (dump_file, "\n;; Expanding Accumulator "); | |
1398 | print_rtl (dump_file, dest); | |
1399 | fprintf (dump_file, "\n"); | |
1400 | } | |
e24a805c | 1401 | |
3b3940d7 | 1402 | if (debug_uses) |
1403 | /* Instead of resetting the debug insns, we could replace each | |
1404 | debug use in the loop with the sum or product of all expanded | |
1405 | accummulators. Since we'll only know of all expansions at the | |
1406 | end, we'd have to keep track of which vars_to_expand a debug | |
1407 | insn in the loop references, take note of each copy of the | |
1408 | debug insn during unrolling, and when it's all done, compute | |
1409 | the sum or product of each variable and adjust the original | |
1410 | debug insn and each copy thereof. What a pain! */ | |
1411 | reset_debug_uses_in_loop (loop, dest, debug_uses); | |
1412 | ||
375bb675 | 1413 | /* Record the accumulator to expand. */ |
4c36ffe6 | 1414 | ves = XNEW (struct var_to_expand); |
375bb675 | 1415 | ves->insn = insn; |
375bb675 | 1416 | ves->reg = copy_rtx (dest); |
f1f41a6c | 1417 | ves->var_expansions.create (1); |
b635a1a3 | 1418 | ves->next = NULL; |
375bb675 | 1419 | ves->op = GET_CODE (src); |
1420 | ves->expansion_count = 0; | |
1421 | ves->reuse_expansion = 0; | |
48e1416a | 1422 | return ves; |
375bb675 | 1423 | } |
1424 | ||
a9989fb4 | 1425 | /* Determine whether there is an induction variable in INSN that |
48e1416a | 1426 | we would like to split during unrolling. |
375bb675 | 1427 | |
1428 | I.e. replace | |
1429 | ||
1430 | i = i + 1; | |
1431 | ... | |
1432 | i = i + 1; | |
1433 | ... | |
1434 | i = i + 1; | |
1435 | ... | |
1436 | ||
1437 | type chains by | |
1438 | ||
1439 | i0 = i + 1 | |
1440 | ... | |
1441 | i = i0 + 1 | |
1442 | ... | |
1443 | i = i0 + 2 | |
1444 | ... | |
1445 | ||
48e1416a | 1446 | Return NULL if INSN contains no interesting IVs. Otherwise, allocate |
375bb675 | 1447 | an IV_TO_SPLIT structure, fill it with the relevant information and return a |
a9989fb4 | 1448 | pointer to it. */ |
1449 | ||
1450 | static struct iv_to_split * | |
3eeb4f9a | 1451 | analyze_iv_to_split_insn (rtx_insn *insn) |
a9989fb4 | 1452 | { |
1453 | rtx set, dest; | |
1454 | struct rtx_iv iv; | |
1455 | struct iv_to_split *ivts; | |
a53ff4c1 | 1456 | bool ok; |
a9989fb4 | 1457 | |
1458 | /* For now we just split the basic induction variables. Later this may be | |
1459 | extended for example by selecting also addresses of memory references. */ | |
1460 | set = single_set (insn); | |
1461 | if (!set) | |
1462 | return NULL; | |
1463 | ||
1464 | dest = SET_DEST (set); | |
1465 | if (!REG_P (dest)) | |
1466 | return NULL; | |
1467 | ||
1468 | if (!biv_p (insn, dest)) | |
1469 | return NULL; | |
1470 | ||
3f37d465 | 1471 | ok = iv_analyze_result (insn, dest, &iv); |
e8820970 | 1472 | |
1473 | /* This used to be an assert under the assumption that if biv_p returns | |
1474 | true that iv_analyze_result must also return true. However, that | |
1475 | assumption is not strictly correct as evidenced by pr25569. | |
1476 | ||
1477 | Returning NULL when iv_analyze_result returns false is safe and | |
1478 | avoids the problems in pr25569 until the iv_analyze_* routines | |
1479 | can be fixed, which is apparently hard and time consuming | |
1480 | according to their author. */ | |
1481 | if (! ok) | |
1482 | return NULL; | |
a9989fb4 | 1483 | |
1484 | if (iv.step == const0_rtx | |
1485 | || iv.mode != iv.extend_mode) | |
1486 | return NULL; | |
1487 | ||
1488 | /* Record the insn to split. */ | |
4c36ffe6 | 1489 | ivts = XNEW (struct iv_to_split); |
a9989fb4 | 1490 | ivts->insn = insn; |
ef770610 | 1491 | ivts->orig_var = dest; |
a9989fb4 | 1492 | ivts->base_var = NULL_RTX; |
1493 | ivts->step = iv.step; | |
b635a1a3 | 1494 | ivts->next = NULL; |
48e1416a | 1495 | |
a9989fb4 | 1496 | return ivts; |
1497 | } | |
1498 | ||
375bb675 | 1499 | /* Determines which of insns in LOOP can be optimized. |
1500 | Return a OPT_INFO struct with the relevant hash tables filled | |
1501 | with all insns to be optimized. The FIRST_NEW_BLOCK field | |
a9989fb4 | 1502 | is undefined for the return value. */ |
1503 | ||
375bb675 | 1504 | static struct opt_info * |
1505 | analyze_insns_in_loop (struct loop *loop) | |
a9989fb4 | 1506 | { |
1507 | basic_block *body, bb; | |
749ea85f | 1508 | unsigned i; |
4c36ffe6 | 1509 | struct opt_info *opt_info = XCNEW (struct opt_info); |
3eeb4f9a | 1510 | rtx_insn *insn; |
375bb675 | 1511 | struct iv_to_split *ivts = NULL; |
1512 | struct var_to_expand *ves = NULL; | |
d9dd21a8 | 1513 | iv_to_split **slot1; |
1514 | var_to_expand **slot2; | |
f1f41a6c | 1515 | vec<edge> edges = get_loop_exit_edges (loop); |
749ea85f | 1516 | edge exit; |
375bb675 | 1517 | bool can_apply = false; |
48e1416a | 1518 | |
a9989fb4 | 1519 | iv_analysis_loop_init (loop); |
1520 | ||
1521 | body = get_loop_body (loop); | |
375bb675 | 1522 | |
1523 | if (flag_split_ivs_in_unroller) | |
b635a1a3 | 1524 | { |
c1f445d2 | 1525 | opt_info->insns_to_split |
1526 | = new hash_table<iv_split_hasher> (5 * loop->num_nodes); | |
b635a1a3 | 1527 | opt_info->iv_to_split_head = NULL; |
1528 | opt_info->iv_to_split_tail = &opt_info->iv_to_split_head; | |
1529 | } | |
48e1416a | 1530 | |
375bb675 | 1531 | /* Record the loop exit bb and loop preheader before the unrolling. */ |
88e6f696 | 1532 | opt_info->loop_preheader = loop_preheader_edge (loop)->src; |
48e1416a | 1533 | |
f1f41a6c | 1534 | if (edges.length () == 1) |
375bb675 | 1535 | { |
f1f41a6c | 1536 | exit = edges[0]; |
749ea85f | 1537 | if (!(exit->flags & EDGE_COMPLEX)) |
1538 | { | |
1539 | opt_info->loop_exit = split_edge (exit); | |
1540 | can_apply = true; | |
1541 | } | |
375bb675 | 1542 | } |
48e1416a | 1543 | |
375bb675 | 1544 | if (flag_variable_expansion_in_unroller |
1545 | && can_apply) | |
b635a1a3 | 1546 | { |
c1f445d2 | 1547 | opt_info->insns_with_var_to_expand |
1548 | = new hash_table<var_expand_hasher> (5 * loop->num_nodes); | |
b635a1a3 | 1549 | opt_info->var_to_expand_head = NULL; |
1550 | opt_info->var_to_expand_tail = &opt_info->var_to_expand_head; | |
1551 | } | |
48e1416a | 1552 | |
a9989fb4 | 1553 | for (i = 0; i < loop->num_nodes; i++) |
1554 | { | |
1555 | bb = body[i]; | |
1556 | if (!dominated_by_p (CDI_DOMINATORS, loop->latch, bb)) | |
1557 | continue; | |
1558 | ||
1559 | FOR_BB_INSNS (bb, insn) | |
375bb675 | 1560 | { |
1561 | if (!INSN_P (insn)) | |
1562 | continue; | |
48e1416a | 1563 | |
c1f445d2 | 1564 | if (opt_info->insns_to_split) |
375bb675 | 1565 | ivts = analyze_iv_to_split_insn (insn); |
48e1416a | 1566 | |
375bb675 | 1567 | if (ivts) |
1568 | { | |
c1f445d2 | 1569 | slot1 = opt_info->insns_to_split->find_slot (ivts, INSERT); |
b635a1a3 | 1570 | gcc_assert (*slot1 == NULL); |
375bb675 | 1571 | *slot1 = ivts; |
b635a1a3 | 1572 | *opt_info->iv_to_split_tail = ivts; |
1573 | opt_info->iv_to_split_tail = &ivts->next; | |
375bb675 | 1574 | continue; |
1575 | } | |
48e1416a | 1576 | |
c1f445d2 | 1577 | if (opt_info->insns_with_var_to_expand) |
375bb675 | 1578 | ves = analyze_insn_to_expand_var (loop, insn); |
48e1416a | 1579 | |
375bb675 | 1580 | if (ves) |
1581 | { | |
c1f445d2 | 1582 | slot2 = opt_info->insns_with_var_to_expand->find_slot (ves, INSERT); |
b635a1a3 | 1583 | gcc_assert (*slot2 == NULL); |
375bb675 | 1584 | *slot2 = ves; |
b635a1a3 | 1585 | *opt_info->var_to_expand_tail = ves; |
1586 | opt_info->var_to_expand_tail = &ves->next; | |
375bb675 | 1587 | } |
1588 | } | |
a9989fb4 | 1589 | } |
48e1416a | 1590 | |
f1f41a6c | 1591 | edges.release (); |
a9989fb4 | 1592 | free (body); |
375bb675 | 1593 | return opt_info; |
a9989fb4 | 1594 | } |
1595 | ||
1596 | /* Called just before loop duplication. Records start of duplicated area | |
375bb675 | 1597 | to OPT_INFO. */ |
a9989fb4 | 1598 | |
48e1416a | 1599 | static void |
375bb675 | 1600 | opt_info_start_duplication (struct opt_info *opt_info) |
a9989fb4 | 1601 | { |
375bb675 | 1602 | if (opt_info) |
fe672ac0 | 1603 | opt_info->first_new_block = last_basic_block_for_fn (cfun); |
a9989fb4 | 1604 | } |
1605 | ||
1606 | /* Determine the number of iterations between initialization of the base | |
1607 | variable and the current copy (N_COPY). N_COPIES is the total number | |
1608 | of newly created copies. UNROLLING is true if we are unrolling | |
1609 | (not peeling) the loop. */ | |
1610 | ||
1611 | static unsigned | |
1612 | determine_split_iv_delta (unsigned n_copy, unsigned n_copies, bool unrolling) | |
1613 | { | |
1614 | if (unrolling) | |
1615 | { | |
1616 | /* If we are unrolling, initialization is done in the original loop | |
1617 | body (number 0). */ | |
1618 | return n_copy; | |
1619 | } | |
1620 | else | |
1621 | { | |
1622 | /* If we are peeling, the copy in that the initialization occurs has | |
1623 | number 1. The original loop (number 0) is the last. */ | |
1624 | if (n_copy) | |
1625 | return n_copy - 1; | |
1626 | else | |
1627 | return n_copies; | |
1628 | } | |
1629 | } | |
1630 | ||
b635a1a3 | 1631 | /* Allocate basic variable for the induction variable chain. */ |
a9989fb4 | 1632 | |
b635a1a3 | 1633 | static void |
1634 | allocate_basic_variable (struct iv_to_split *ivts) | |
a9989fb4 | 1635 | { |
686fd44c | 1636 | rtx expr = SET_SRC (single_set (ivts->insn)); |
a9989fb4 | 1637 | |
1638 | ivts->base_var = gen_reg_rtx (GET_MODE (expr)); | |
a9989fb4 | 1639 | } |
1640 | ||
1641 | /* Insert initialization of basic variable of IVTS before INSN, taking | |
1642 | the initial value from INSN. */ | |
1643 | ||
1644 | static void | |
222dc1d3 | 1645 | insert_base_initialization (struct iv_to_split *ivts, rtx_insn *insn) |
a9989fb4 | 1646 | { |
686fd44c | 1647 | rtx expr = copy_rtx (SET_SRC (single_set (insn))); |
222dc1d3 | 1648 | rtx_insn *seq; |
a9989fb4 | 1649 | |
1650 | start_sequence (); | |
1651 | expr = force_operand (expr, ivts->base_var); | |
1652 | if (expr != ivts->base_var) | |
1653 | emit_move_insn (ivts->base_var, expr); | |
1654 | seq = get_insns (); | |
1655 | end_sequence (); | |
1656 | ||
1657 | emit_insn_before (seq, insn); | |
1658 | } | |
1659 | ||
1660 | /* Replace the use of induction variable described in IVTS in INSN | |
1661 | by base variable + DELTA * step. */ | |
1662 | ||
1663 | static void | |
222dc1d3 | 1664 | split_iv (struct iv_to_split *ivts, rtx_insn *insn, unsigned delta) |
a9989fb4 | 1665 | { |
222dc1d3 | 1666 | rtx expr, *loc, incr, var; |
1667 | rtx_insn *seq; | |
3754d046 | 1668 | machine_mode mode = GET_MODE (ivts->base_var); |
a9989fb4 | 1669 | rtx src, dest, set; |
1670 | ||
1671 | /* Construct base + DELTA * step. */ | |
1672 | if (!delta) | |
1673 | expr = ivts->base_var; | |
1674 | else | |
1675 | { | |
1676 | incr = simplify_gen_binary (MULT, mode, | |
1677 | ivts->step, gen_int_mode (delta, mode)); | |
1678 | expr = simplify_gen_binary (PLUS, GET_MODE (ivts->base_var), | |
1679 | ivts->base_var, incr); | |
1680 | } | |
1681 | ||
1682 | /* Figure out where to do the replacement. */ | |
686fd44c | 1683 | loc = &SET_SRC (single_set (insn)); |
a9989fb4 | 1684 | |
1685 | /* If we can make the replacement right away, we're done. */ | |
1686 | if (validate_change (insn, loc, expr, 0)) | |
1687 | return; | |
1688 | ||
1689 | /* Otherwise, force EXPR into a register and try again. */ | |
1690 | start_sequence (); | |
1691 | var = gen_reg_rtx (mode); | |
1692 | expr = force_operand (expr, var); | |
1693 | if (expr != var) | |
1694 | emit_move_insn (var, expr); | |
1695 | seq = get_insns (); | |
1696 | end_sequence (); | |
1697 | emit_insn_before (seq, insn); | |
48e1416a | 1698 | |
a9989fb4 | 1699 | if (validate_change (insn, loc, var, 0)) |
1700 | return; | |
1701 | ||
1702 | /* The last chance. Try recreating the assignment in insn | |
1703 | completely from scratch. */ | |
1704 | set = single_set (insn); | |
1705 | gcc_assert (set); | |
1706 | ||
1707 | start_sequence (); | |
1708 | *loc = var; | |
1709 | src = copy_rtx (SET_SRC (set)); | |
1710 | dest = copy_rtx (SET_DEST (set)); | |
1711 | src = force_operand (src, dest); | |
1712 | if (src != dest) | |
1713 | emit_move_insn (dest, src); | |
1714 | seq = get_insns (); | |
1715 | end_sequence (); | |
48e1416a | 1716 | |
a9989fb4 | 1717 | emit_insn_before (seq, insn); |
1718 | delete_insn (insn); | |
1719 | } | |
1720 | ||
a9989fb4 | 1721 | |
6dce5fff | 1722 | /* Return one expansion of the accumulator recorded in struct VE. */ |
a9989fb4 | 1723 | |
375bb675 | 1724 | static rtx |
1725 | get_expansion (struct var_to_expand *ve) | |
1726 | { | |
1727 | rtx reg; | |
48e1416a | 1728 | |
375bb675 | 1729 | if (ve->reuse_expansion == 0) |
1730 | reg = ve->reg; | |
1731 | else | |
f1f41a6c | 1732 | reg = ve->var_expansions[ve->reuse_expansion - 1]; |
48e1416a | 1733 | |
f1f41a6c | 1734 | if (ve->var_expansions.length () == (unsigned) ve->reuse_expansion) |
375bb675 | 1735 | ve->reuse_expansion = 0; |
48e1416a | 1736 | else |
375bb675 | 1737 | ve->reuse_expansion++; |
48e1416a | 1738 | |
375bb675 | 1739 | return reg; |
1740 | } | |
a9989fb4 | 1741 | |
a9989fb4 | 1742 | |
48e1416a | 1743 | /* Given INSN replace the uses of the accumulator recorded in VE |
375bb675 | 1744 | with a new register. */ |
1745 | ||
1746 | static void | |
222dc1d3 | 1747 | expand_var_during_unrolling (struct var_to_expand *ve, rtx_insn *insn) |
375bb675 | 1748 | { |
1749 | rtx new_reg, set; | |
1750 | bool really_new_expansion = false; | |
48e1416a | 1751 | |
375bb675 | 1752 | set = single_set (insn); |
972e95af | 1753 | gcc_assert (set); |
48e1416a | 1754 | |
375bb675 | 1755 | /* Generate a new register only if the expansion limit has not been |
1756 | reached. Else reuse an already existing expansion. */ | |
1757 | if (PARAM_VALUE (PARAM_MAX_VARIABLE_EXPANSIONS) > ve->expansion_count) | |
1758 | { | |
1759 | really_new_expansion = true; | |
1760 | new_reg = gen_reg_rtx (GET_MODE (ve->reg)); | |
1761 | } | |
1762 | else | |
1763 | new_reg = get_expansion (ve); | |
1764 | ||
d022e236 | 1765 | validate_replace_rtx_group (SET_DEST (set), new_reg, insn); |
375bb675 | 1766 | if (apply_change_group ()) |
1767 | if (really_new_expansion) | |
1768 | { | |
f1f41a6c | 1769 | ve->var_expansions.safe_push (new_reg); |
375bb675 | 1770 | ve->expansion_count++; |
1771 | } | |
1772 | } | |
1773 | ||
b635a1a3 | 1774 | /* Initialize the variable expansions in loop preheader. PLACE is the |
1775 | loop-preheader basic block where the initialization of the | |
1776 | expansions should take place. The expansions are initialized with | |
1777 | (-0) when the operation is plus or minus to honor sign zero. This | |
1778 | way we can prevent cases where the sign of the final result is | |
1779 | effected by the sign of the expansion. Here is an example to | |
1780 | demonstrate this: | |
48e1416a | 1781 | |
428c97ad | 1782 | for (i = 0 ; i < n; i++) |
1783 | sum += something; | |
1784 | ||
1785 | ==> | |
1786 | ||
1787 | sum += something | |
1788 | .... | |
1789 | i = i+1; | |
1790 | sum1 += something | |
1791 | .... | |
1792 | i = i+1 | |
1793 | sum2 += something; | |
1794 | .... | |
48e1416a | 1795 | |
428c97ad | 1796 | When SUM is initialized with -zero and SOMETHING is also -zero; the |
1797 | final result of sum should be -zero thus the expansions sum1 and sum2 | |
1798 | should be initialized with -zero as well (otherwise we will get +zero | |
1799 | as the final result). */ | |
375bb675 | 1800 | |
b635a1a3 | 1801 | static void |
1802 | insert_var_expansion_initialization (struct var_to_expand *ve, | |
1803 | basic_block place) | |
375bb675 | 1804 | { |
222dc1d3 | 1805 | rtx_insn *seq; |
1806 | rtx var, zero_init; | |
375bb675 | 1807 | unsigned i; |
3754d046 | 1808 | machine_mode mode = GET_MODE (ve->reg); |
428c97ad | 1809 | bool honor_signed_zero_p = HONOR_SIGNED_ZEROS (mode); |
1810 | ||
f1f41a6c | 1811 | if (ve->var_expansions.length () == 0) |
b635a1a3 | 1812 | return; |
48e1416a | 1813 | |
375bb675 | 1814 | start_sequence (); |
7497b98c | 1815 | switch (ve->op) |
1816 | { | |
1817 | case FMA: | |
1818 | /* Note that we only accumulate FMA via the ADD operand. */ | |
1819 | case PLUS: | |
1820 | case MINUS: | |
f1f41a6c | 1821 | FOR_EACH_VEC_ELT (ve->var_expansions, i, var) |
7497b98c | 1822 | { |
1823 | if (honor_signed_zero_p) | |
1824 | zero_init = simplify_gen_unary (NEG, mode, CONST0_RTX (mode), mode); | |
1825 | else | |
1826 | zero_init = CONST0_RTX (mode); | |
1827 | emit_move_insn (var, zero_init); | |
1828 | } | |
1829 | break; | |
1830 | ||
1831 | case MULT: | |
f1f41a6c | 1832 | FOR_EACH_VEC_ELT (ve->var_expansions, i, var) |
7497b98c | 1833 | { |
1834 | zero_init = CONST1_RTX (GET_MODE (var)); | |
1835 | emit_move_insn (var, zero_init); | |
1836 | } | |
1837 | break; | |
1838 | ||
1839 | default: | |
1840 | gcc_unreachable (); | |
1841 | } | |
48e1416a | 1842 | |
375bb675 | 1843 | seq = get_insns (); |
1844 | end_sequence (); | |
48e1416a | 1845 | |
d022e236 | 1846 | emit_insn_after (seq, BB_END (place)); |
375bb675 | 1847 | } |
1848 | ||
b635a1a3 | 1849 | /* Combine the variable expansions at the loop exit. PLACE is the |
1850 | loop exit basic block where the summation of the expansions should | |
1851 | take place. */ | |
375bb675 | 1852 | |
b635a1a3 | 1853 | static void |
1854 | combine_var_copies_in_loop_exit (struct var_to_expand *ve, basic_block place) | |
375bb675 | 1855 | { |
375bb675 | 1856 | rtx sum = ve->reg; |
222dc1d3 | 1857 | rtx expr, var; |
1858 | rtx_insn *seq, *insn; | |
375bb675 | 1859 | unsigned i; |
1860 | ||
f1f41a6c | 1861 | if (ve->var_expansions.length () == 0) |
b635a1a3 | 1862 | return; |
48e1416a | 1863 | |
375bb675 | 1864 | start_sequence (); |
7497b98c | 1865 | switch (ve->op) |
1866 | { | |
1867 | case FMA: | |
1868 | /* Note that we only accumulate FMA via the ADD operand. */ | |
1869 | case PLUS: | |
1870 | case MINUS: | |
f1f41a6c | 1871 | FOR_EACH_VEC_ELT (ve->var_expansions, i, var) |
7497b98c | 1872 | sum = simplify_gen_binary (PLUS, GET_MODE (ve->reg), var, sum); |
1873 | break; | |
1874 | ||
1875 | case MULT: | |
f1f41a6c | 1876 | FOR_EACH_VEC_ELT (ve->var_expansions, i, var) |
7497b98c | 1877 | sum = simplify_gen_binary (MULT, GET_MODE (ve->reg), var, sum); |
1878 | break; | |
1879 | ||
1880 | default: | |
1881 | gcc_unreachable (); | |
1882 | } | |
48e1416a | 1883 | |
375bb675 | 1884 | expr = force_operand (sum, ve->reg); |
1885 | if (expr != ve->reg) | |
1886 | emit_move_insn (ve->reg, expr); | |
1887 | seq = get_insns (); | |
1888 | end_sequence (); | |
48e1416a | 1889 | |
375bb675 | 1890 | insn = BB_HEAD (place); |
1891 | while (!NOTE_INSN_BASIC_BLOCK_P (insn)) | |
1892 | insn = NEXT_INSN (insn); | |
1893 | ||
1894 | emit_insn_after (seq, insn); | |
375bb675 | 1895 | } |
1896 | ||
ef770610 | 1897 | /* Strip away REG_EQUAL notes for IVs we're splitting. |
1898 | ||
1899 | Updating REG_EQUAL notes for IVs we split is tricky: We | |
1900 | cannot tell until after unrolling, DF-rescanning, and liveness | |
1901 | updating, whether an EQ_USE is reached by the split IV while | |
1902 | the IV reg is still live. See PR55006. | |
1903 | ||
1904 | ??? We cannot use remove_reg_equal_equiv_notes_for_regno, | |
1905 | because RTL loop-iv requires us to defer rescanning insns and | |
1906 | any notes attached to them. So resort to old techniques... */ | |
1907 | ||
1908 | static void | |
222dc1d3 | 1909 | maybe_strip_eq_note_for_split_iv (struct opt_info *opt_info, rtx_insn *insn) |
ef770610 | 1910 | { |
1911 | struct iv_to_split *ivts; | |
1912 | rtx note = find_reg_equal_equiv_note (insn); | |
1913 | if (! note) | |
1914 | return; | |
1915 | for (ivts = opt_info->iv_to_split_head; ivts; ivts = ivts->next) | |
1916 | if (reg_mentioned_p (ivts->orig_var, note)) | |
1917 | { | |
1918 | remove_note (insn, note); | |
1919 | return; | |
1920 | } | |
1921 | } | |
1922 | ||
48e1416a | 1923 | /* Apply loop optimizations in loop copies using the |
1924 | data which gathered during the unrolling. Structure | |
375bb675 | 1925 | OPT_INFO record that data. |
48e1416a | 1926 | |
a9989fb4 | 1927 | UNROLLING is true if we unrolled (not peeled) the loop. |
1928 | REWRITE_ORIGINAL_BODY is true if we should also rewrite the original body of | |
1929 | the loop (as it should happen in complete unrolling, but not in ordinary | |
1930 | peeling of the loop). */ | |
1931 | ||
1932 | static void | |
48e1416a | 1933 | apply_opt_in_copies (struct opt_info *opt_info, |
1934 | unsigned n_copies, bool unrolling, | |
375bb675 | 1935 | bool rewrite_original_loop) |
a9989fb4 | 1936 | { |
1937 | unsigned i, delta; | |
1938 | basic_block bb, orig_bb; | |
222dc1d3 | 1939 | rtx_insn *insn, *orig_insn, *next; |
a9989fb4 | 1940 | struct iv_to_split ivts_templ, *ivts; |
375bb675 | 1941 | struct var_to_expand ve_templ, *ves; |
48e1416a | 1942 | |
a9989fb4 | 1943 | /* Sanity check -- we need to put initialization in the original loop |
1944 | body. */ | |
1945 | gcc_assert (!unrolling || rewrite_original_loop); | |
48e1416a | 1946 | |
a9989fb4 | 1947 | /* Allocate the basic variables (i0). */ |
c1f445d2 | 1948 | if (opt_info->insns_to_split) |
b635a1a3 | 1949 | for (ivts = opt_info->iv_to_split_head; ivts; ivts = ivts->next) |
1950 | allocate_basic_variable (ivts); | |
48e1416a | 1951 | |
fe672ac0 | 1952 | for (i = opt_info->first_new_block; |
1953 | i < (unsigned) last_basic_block_for_fn (cfun); | |
1954 | i++) | |
a9989fb4 | 1955 | { |
f5a6b05f | 1956 | bb = BASIC_BLOCK_FOR_FN (cfun, i); |
01020a5f | 1957 | orig_bb = get_bb_original (bb); |
48e1416a | 1958 | |
01020a5f | 1959 | /* bb->aux holds position in copy sequence initialized by |
1960 | duplicate_loop_to_header_edge. */ | |
1961 | delta = determine_split_iv_delta ((size_t)bb->aux, n_copies, | |
a9989fb4 | 1962 | unrolling); |
75cc36a4 | 1963 | bb->aux = 0; |
a9989fb4 | 1964 | orig_insn = BB_HEAD (orig_bb); |
ef770610 | 1965 | FOR_BB_INSNS_SAFE (bb, insn, next) |
375bb675 | 1966 | { |
0e6893c5 | 1967 | if (!INSN_P (insn) |
1968 | || (DEBUG_INSN_P (insn) | |
1969 | && TREE_CODE (INSN_VAR_LOCATION_DECL (insn)) == LABEL_DECL)) | |
375bb675 | 1970 | continue; |
48e1416a | 1971 | |
0e6893c5 | 1972 | while (!INSN_P (orig_insn) |
1973 | || (DEBUG_INSN_P (orig_insn) | |
1974 | && (TREE_CODE (INSN_VAR_LOCATION_DECL (orig_insn)) | |
1975 | == LABEL_DECL))) | |
375bb675 | 1976 | orig_insn = NEXT_INSN (orig_insn); |
48e1416a | 1977 | |
375bb675 | 1978 | ivts_templ.insn = orig_insn; |
1979 | ve_templ.insn = orig_insn; | |
48e1416a | 1980 | |
375bb675 | 1981 | /* Apply splitting iv optimization. */ |
c1f445d2 | 1982 | if (opt_info->insns_to_split) |
375bb675 | 1983 | { |
ef770610 | 1984 | maybe_strip_eq_note_for_split_iv (opt_info, insn); |
1985 | ||
c1f445d2 | 1986 | ivts = opt_info->insns_to_split->find (&ivts_templ); |
48e1416a | 1987 | |
375bb675 | 1988 | if (ivts) |
1989 | { | |
19ef3705 | 1990 | gcc_assert (GET_CODE (PATTERN (insn)) |
1991 | == GET_CODE (PATTERN (orig_insn))); | |
48e1416a | 1992 | |
375bb675 | 1993 | if (!delta) |
1994 | insert_base_initialization (ivts, insn); | |
1995 | split_iv (ivts, insn, delta); | |
1996 | } | |
1997 | } | |
1998 | /* Apply variable expansion optimization. */ | |
c1f445d2 | 1999 | if (unrolling && opt_info->insns_with_var_to_expand) |
375bb675 | 2000 | { |
4077bf7a | 2001 | ves = (struct var_to_expand *) |
c1f445d2 | 2002 | opt_info->insns_with_var_to_expand->find (&ve_templ); |
375bb675 | 2003 | if (ves) |
48e1416a | 2004 | { |
19ef3705 | 2005 | gcc_assert (GET_CODE (PATTERN (insn)) |
2006 | == GET_CODE (PATTERN (orig_insn))); | |
375bb675 | 2007 | expand_var_during_unrolling (ves, insn); |
2008 | } | |
2009 | } | |
2010 | orig_insn = NEXT_INSN (orig_insn); | |
2011 | } | |
a9989fb4 | 2012 | } |
2013 | ||
2014 | if (!rewrite_original_loop) | |
2015 | return; | |
48e1416a | 2016 | |
375bb675 | 2017 | /* Initialize the variable expansions in the loop preheader |
48e1416a | 2018 | and take care of combining them at the loop exit. */ |
c1f445d2 | 2019 | if (opt_info->insns_with_var_to_expand) |
375bb675 | 2020 | { |
b635a1a3 | 2021 | for (ves = opt_info->var_to_expand_head; ves; ves = ves->next) |
2022 | insert_var_expansion_initialization (ves, opt_info->loop_preheader); | |
2023 | for (ves = opt_info->var_to_expand_head; ves; ves = ves->next) | |
2024 | combine_var_copies_in_loop_exit (ves, opt_info->loop_exit); | |
375bb675 | 2025 | } |
48e1416a | 2026 | |
a9989fb4 | 2027 | /* Rewrite also the original loop body. Find them as originals of the blocks |
2028 | in the last copied iteration, i.e. those that have | |
01020a5f | 2029 | get_bb_copy (get_bb_original (bb)) == bb. */ |
fe672ac0 | 2030 | for (i = opt_info->first_new_block; |
2031 | i < (unsigned) last_basic_block_for_fn (cfun); | |
2032 | i++) | |
a9989fb4 | 2033 | { |
f5a6b05f | 2034 | bb = BASIC_BLOCK_FOR_FN (cfun, i); |
01020a5f | 2035 | orig_bb = get_bb_original (bb); |
2036 | if (get_bb_copy (orig_bb) != bb) | |
a9989fb4 | 2037 | continue; |
48e1416a | 2038 | |
a9989fb4 | 2039 | delta = determine_split_iv_delta (0, n_copies, unrolling); |
2040 | for (orig_insn = BB_HEAD (orig_bb); | |
375bb675 | 2041 | orig_insn != NEXT_INSN (BB_END (bb)); |
2042 | orig_insn = next) | |
2043 | { | |
2044 | next = NEXT_INSN (orig_insn); | |
48e1416a | 2045 | |
375bb675 | 2046 | if (!INSN_P (orig_insn)) |
2047 | continue; | |
48e1416a | 2048 | |
375bb675 | 2049 | ivts_templ.insn = orig_insn; |
c1f445d2 | 2050 | if (opt_info->insns_to_split) |
375bb675 | 2051 | { |
ef770610 | 2052 | maybe_strip_eq_note_for_split_iv (opt_info, orig_insn); |
2053 | ||
4077bf7a | 2054 | ivts = (struct iv_to_split *) |
c1f445d2 | 2055 | opt_info->insns_to_split->find (&ivts_templ); |
375bb675 | 2056 | if (ivts) |
2057 | { | |
2058 | if (!delta) | |
2059 | insert_base_initialization (ivts, orig_insn); | |
2060 | split_iv (ivts, orig_insn, delta); | |
2061 | continue; | |
2062 | } | |
2063 | } | |
48e1416a | 2064 | |
375bb675 | 2065 | } |
2066 | } | |
2067 | } | |
a9989fb4 | 2068 | |
375bb675 | 2069 | /* Release OPT_INFO. */ |
a9989fb4 | 2070 | |
2071 | static void | |
375bb675 | 2072 | free_opt_info (struct opt_info *opt_info) |
a9989fb4 | 2073 | { |
c1f445d2 | 2074 | delete opt_info->insns_to_split; |
2075 | opt_info->insns_to_split = NULL; | |
2076 | if (opt_info->insns_with_var_to_expand) | |
375bb675 | 2077 | { |
b635a1a3 | 2078 | struct var_to_expand *ves; |
2079 | ||
2080 | for (ves = opt_info->var_to_expand_head; ves; ves = ves->next) | |
f1f41a6c | 2081 | ves->var_expansions.release (); |
c1f445d2 | 2082 | delete opt_info->insns_with_var_to_expand; |
2083 | opt_info->insns_with_var_to_expand = NULL; | |
375bb675 | 2084 | } |
2085 | free (opt_info); | |
a9989fb4 | 2086 | } |