Update changelog entry.
[official-gcc.git] / gcc / tree-ssa-loop-ch.c
blob4d4813df3c8277f19a0f1d957673001d7a0366e1
1 /* Loop header copying on trees.
2 Copyright (C) 2004-2018 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any
9 later version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT
12 ANY 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.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "backend.h"
24 #include "tree.h"
25 #include "gimple.h"
26 #include "cfghooks.h"
27 #include "tree-pass.h"
28 #include "gimple-ssa.h"
29 #include "gimple-iterator.h"
30 #include "tree-cfg.h"
31 #include "tree-into-ssa.h"
32 #include "cfgloop.h"
33 #include "tree-inline.h"
34 #include "tree-ssa-scopedtables.h"
35 #include "tree-ssa-threadedge.h"
36 #include "tree-ssa-sccvn.h"
37 #include "params.h"
39 /* Duplicates headers of loops if they are small enough, so that the statements
40 in the loop body are always executed when the loop is entered. This
41 increases effectiveness of code motion optimizations, and reduces the need
42 for loop preconditioning. */
44 /* Check whether we should duplicate HEADER of LOOP. At most *LIMIT
45 instructions should be duplicated, limit is decreased by the actual
46 amount. */
48 static bool
49 should_duplicate_loop_header_p (basic_block header, struct loop *loop,
50 int *limit)
52 gimple_stmt_iterator bsi;
53 gimple *last;
55 gcc_assert (!header->aux);
57 /* Loop header copying usually increases size of the code. This used not to
58 be true, since quite often it is possible to verify that the condition is
59 satisfied in the first iteration and therefore to eliminate it. Jump
60 threading handles these cases now. */
61 if (optimize_loop_for_size_p (loop)
62 && !loop->force_vectorize)
64 if (dump_file && (dump_flags & TDF_DETAILS))
65 fprintf (dump_file,
66 " Not duplicating bb %i: optimizing for size.\n",
67 header->index);
68 return false;
71 gcc_assert (EDGE_COUNT (header->succs) > 0);
72 if (single_succ_p (header))
74 if (dump_file && (dump_flags & TDF_DETAILS))
75 fprintf (dump_file,
76 " Not duplicating bb %i: it is single succ.\n",
77 header->index);
78 return false;
81 if (flow_bb_inside_loop_p (loop, EDGE_SUCC (header, 0)->dest)
82 && flow_bb_inside_loop_p (loop, EDGE_SUCC (header, 1)->dest))
84 if (dump_file && (dump_flags & TDF_DETAILS))
85 fprintf (dump_file,
86 " Not duplicating bb %i: both sucessors are in loop.\n",
87 loop->num);
88 return false;
91 /* If this is not the original loop header, we want it to have just
92 one predecessor in order to match the && pattern. */
93 if (header != loop->header && !single_pred_p (header))
95 if (dump_file && (dump_flags & TDF_DETAILS))
96 fprintf (dump_file,
97 " Not duplicating bb %i: it has mutiple predecestors.\n",
98 header->index);
99 return false;
102 last = last_stmt (header);
103 if (gimple_code (last) != GIMPLE_COND)
105 if (dump_file && (dump_flags & TDF_DETAILS))
106 fprintf (dump_file,
107 " Not duplicating bb %i: it does not end by conditional.\n",
108 header->index);
109 return false;
112 /* Count number of instructions and punt on calls. */
113 for (bsi = gsi_start_bb (header); !gsi_end_p (bsi); gsi_next (&bsi))
115 last = gsi_stmt (bsi);
117 if (gimple_code (last) == GIMPLE_LABEL)
118 continue;
120 if (is_gimple_debug (last))
121 continue;
123 if (gimple_code (last) == GIMPLE_CALL
124 && (!gimple_inexpensive_call_p (as_a <gcall *> (last))
125 /* IFN_LOOP_DIST_ALIAS means that inner loop is distributed
126 at current loop's header. Don't copy in this case. */
127 || gimple_call_internal_p (last, IFN_LOOP_DIST_ALIAS)))
129 if (dump_file && (dump_flags & TDF_DETAILS))
130 fprintf (dump_file,
131 " Not duplicating bb %i: it contains call.\n",
132 header->index);
133 return false;
136 *limit -= estimate_num_insns (last, &eni_size_weights);
137 if (*limit < 0)
139 if (dump_file && (dump_flags & TDF_DETAILS))
140 fprintf (dump_file,
141 " Not duplicating bb %i contains too many insns.\n",
142 header->index);
143 return false;
146 if (dump_file && (dump_flags & TDF_DETAILS))
147 fprintf (dump_file, " Will duplicate bb %i\n", header->index);
148 return true;
151 /* Checks whether LOOP is a do-while style loop. */
153 static bool
154 do_while_loop_p (struct loop *loop)
156 gimple *stmt = last_stmt (loop->latch);
158 /* If the latch of the loop is not empty, it is not a do-while loop. */
159 if (stmt
160 && gimple_code (stmt) != GIMPLE_LABEL)
162 if (dump_file && (dump_flags & TDF_DETAILS))
163 fprintf (dump_file,
164 "Loop %i is not do-while loop: latch is not empty.\n",
165 loop->num);
166 return false;
169 /* If the latch does not have a single predecessor, it is not a
170 do-while loop. */
171 if (!single_pred_p (loop->latch))
173 if (dump_file && (dump_flags & TDF_DETAILS))
174 fprintf (dump_file,
175 "Loop %i is not do-while loop: latch has multiple "
176 "predecessors.\n", loop->num);
177 return false;
180 /* If the latch predecessor doesn't exit the loop, it is not a
181 do-while loop. */
182 if (!loop_exits_from_bb_p (loop, single_pred (loop->latch)))
184 if (dump_file && (dump_flags & TDF_DETAILS))
185 fprintf (dump_file,
186 "Loop %i is not do-while loop: latch predecessor "
187 "does not exit loop.\n", loop->num);
188 return false;
191 if (dump_file && (dump_flags & TDF_DETAILS))
192 fprintf (dump_file, "Loop %i is do-while loop\n", loop->num);
194 return true;
197 namespace {
199 /* Common superclass for both header-copying phases. */
200 class ch_base : public gimple_opt_pass
202 protected:
203 ch_base (pass_data data, gcc::context *ctxt)
204 : gimple_opt_pass (data, ctxt)
207 /* Copies headers of all loops in FUN for which process_loop_p is true. */
208 unsigned int copy_headers (function *fun);
210 /* Return true to copy headers of LOOP or false to skip. */
211 virtual bool process_loop_p (struct loop *loop) = 0;
214 const pass_data pass_data_ch =
216 GIMPLE_PASS, /* type */
217 "ch", /* name */
218 OPTGROUP_LOOP, /* optinfo_flags */
219 TV_TREE_CH, /* tv_id */
220 ( PROP_cfg | PROP_ssa ), /* properties_required */
221 0, /* properties_provided */
222 0, /* properties_destroyed */
223 0, /* todo_flags_start */
224 0, /* todo_flags_finish */
227 class pass_ch : public ch_base
229 public:
230 pass_ch (gcc::context *ctxt)
231 : ch_base (pass_data_ch, ctxt)
234 /* opt_pass methods: */
235 virtual bool gate (function *) { return flag_tree_ch != 0; }
237 /* Initialize and finalize loop structures, copying headers inbetween. */
238 virtual unsigned int execute (function *);
240 opt_pass * clone () { return new pass_ch (m_ctxt); }
242 protected:
243 /* ch_base method: */
244 virtual bool process_loop_p (struct loop *loop);
245 }; // class pass_ch
247 const pass_data pass_data_ch_vect =
249 GIMPLE_PASS, /* type */
250 "ch_vect", /* name */
251 OPTGROUP_LOOP, /* optinfo_flags */
252 TV_TREE_CH, /* tv_id */
253 ( PROP_cfg | PROP_ssa ), /* properties_required */
254 0, /* properties_provided */
255 0, /* properties_destroyed */
256 0, /* todo_flags_start */
257 0, /* todo_flags_finish */
260 /* This is a more aggressive version of the same pass, designed to run just
261 before if-conversion and vectorization, to put more loops into the form
262 required for those phases. */
263 class pass_ch_vect : public ch_base
265 public:
266 pass_ch_vect (gcc::context *ctxt)
267 : ch_base (pass_data_ch_vect, ctxt)
270 /* opt_pass methods: */
271 virtual bool gate (function *fun)
273 return flag_tree_ch != 0
274 && (flag_tree_loop_vectorize != 0 || fun->has_force_vectorize_loops);
277 /* Just copy headers, no initialization/finalization of loop structures. */
278 virtual unsigned int execute (function *);
280 protected:
281 /* ch_base method: */
282 virtual bool process_loop_p (struct loop *loop);
283 }; // class pass_ch_vect
285 /* For all loops, copy the condition at the end of the loop body in front
286 of the loop. This is beneficial since it increases efficiency of
287 code motion optimizations. It also saves one jump on entry to the loop. */
289 unsigned int
290 ch_base::copy_headers (function *fun)
292 struct loop *loop;
293 basic_block header;
294 edge exit, entry;
295 basic_block *bbs, *copied_bbs;
296 unsigned n_bbs;
297 unsigned bbs_size;
298 bool changed = false;
300 if (number_of_loops (fun) <= 1)
301 return 0;
303 bbs = XNEWVEC (basic_block, n_basic_blocks_for_fn (fun));
304 copied_bbs = XNEWVEC (basic_block, n_basic_blocks_for_fn (fun));
305 bbs_size = n_basic_blocks_for_fn (fun);
307 auto_vec<std::pair<edge, loop_p> > copied;
309 FOR_EACH_LOOP (loop, 0)
311 int initial_limit = PARAM_VALUE (PARAM_MAX_LOOP_HEADER_INSNS);
312 int remaining_limit = initial_limit;
313 if (dump_file && (dump_flags & TDF_DETAILS))
314 fprintf (dump_file,
315 "Analyzing loop %i\n", loop->num);
317 header = loop->header;
319 /* If the loop is already a do-while style one (either because it was
320 written as such, or because jump threading transformed it into one),
321 we might be in fact peeling the first iteration of the loop. This
322 in general is not a good idea. Also avoid touching infinite loops. */
323 if (!loop_has_exit_edges (loop)
324 || !process_loop_p (loop))
325 continue;
327 /* Iterate the header copying up to limit; this takes care of the cases
328 like while (a && b) {...}, where we want to have both of the conditions
329 copied. TODO -- handle while (a || b) - like cases, by not requiring
330 the header to have just a single successor and copying up to
331 postdominator. */
333 exit = NULL;
334 n_bbs = 0;
335 while (should_duplicate_loop_header_p (header, loop, &remaining_limit))
337 /* Find a successor of header that is inside a loop; i.e. the new
338 header after the condition is copied. */
339 if (flow_bb_inside_loop_p (loop, EDGE_SUCC (header, 0)->dest))
340 exit = EDGE_SUCC (header, 0);
341 else
342 exit = EDGE_SUCC (header, 1);
343 bbs[n_bbs++] = header;
344 gcc_assert (bbs_size > n_bbs);
345 header = exit->dest;
346 /* Make sure to stop copying after we copied the first exit test.
347 Without further heuristics we do not want to rotate the loop
348 any further. */
349 if (loop_exits_from_bb_p (loop, exit->src))
350 break;
353 if (!exit)
354 continue;
356 if (dump_file && (dump_flags & TDF_DETAILS))
357 fprintf (dump_file,
358 "Duplicating header of the loop %d up to edge %d->%d,"
359 " %i insns.\n",
360 loop->num, exit->src->index, exit->dest->index,
361 initial_limit - remaining_limit);
363 /* Ensure that the header will have just the latch as a predecessor
364 inside the loop. */
365 if (!single_pred_p (exit->dest))
366 exit = single_pred_edge (split_edge (exit));
368 entry = loop_preheader_edge (loop);
370 propagate_threaded_block_debug_into (exit->dest, entry->dest);
371 if (!gimple_duplicate_sese_region (entry, exit, bbs, n_bbs, copied_bbs,
372 true))
374 fprintf (dump_file, "Duplication failed.\n");
375 continue;
377 copied.safe_push (std::make_pair (entry, loop));
379 /* If the loop has the form "for (i = j; i < j + 10; i++)" then
380 this copying can introduce a case where we rely on undefined
381 signed overflow to eliminate the preheader condition, because
382 we assume that "j < j + 10" is true. We don't want to warn
383 about that case for -Wstrict-overflow, because in general we
384 don't warn about overflow involving loops. Prevent the
385 warning by setting the no_warning flag in the condition. */
386 if (warn_strict_overflow > 0)
388 unsigned int i;
390 for (i = 0; i < n_bbs; ++i)
392 gimple_stmt_iterator bsi;
394 for (bsi = gsi_start_bb (copied_bbs[i]);
395 !gsi_end_p (bsi);
396 gsi_next (&bsi))
398 gimple *stmt = gsi_stmt (bsi);
399 if (gimple_code (stmt) == GIMPLE_COND)
400 gimple_set_no_warning (stmt, true);
401 else if (is_gimple_assign (stmt))
403 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
404 if (TREE_CODE_CLASS (rhs_code) == tcc_comparison)
405 gimple_set_no_warning (stmt, true);
411 /* Ensure that the latch and the preheader is simple (we know that they
412 are not now, since there was the loop exit condition. */
413 split_edge (loop_preheader_edge (loop));
414 split_edge (loop_latch_edge (loop));
416 if (dump_file && (dump_flags & TDF_DETAILS))
418 if (do_while_loop_p (loop))
419 fprintf (dump_file, "Loop %d is now do-while loop.\n", loop->num);
420 else
421 fprintf (dump_file, "Loop %d is still not do-while loop.\n",
422 loop->num);
425 changed = true;
428 if (changed)
430 update_ssa (TODO_update_ssa);
431 /* After updating SSA form perform CSE on the loop header
432 copies. This is esp. required for the pass before
433 vectorization since nothing cleans up copied exit tests
434 that can now be simplified. CSE from the entry of the
435 region we copied till all loop exit blocks but not
436 entering the loop itself. */
437 for (unsigned i = 0; i < copied.length (); ++i)
439 edge entry = copied[i].first;
440 loop_p loop = copied[i].second;
441 vec<edge> exit_edges = get_loop_exit_edges (loop);
442 bitmap exit_bbs = BITMAP_ALLOC (NULL);
443 for (unsigned j = 0; j < exit_edges.length (); ++j)
444 bitmap_set_bit (exit_bbs, exit_edges[j]->dest->index);
445 bitmap_set_bit (exit_bbs, loop->header->index);
446 do_rpo_vn (cfun, entry, exit_bbs);
447 BITMAP_FREE (exit_bbs);
448 exit_edges.release ();
451 free (bbs);
452 free (copied_bbs);
454 return changed ? TODO_cleanup_cfg : 0;
457 /* Initialize the loop structures we need, and finalize after. */
459 unsigned int
460 pass_ch::execute (function *fun)
462 loop_optimizer_init (LOOPS_HAVE_PREHEADERS
463 | LOOPS_HAVE_SIMPLE_LATCHES
464 | LOOPS_HAVE_RECORDED_EXITS);
466 unsigned int res = copy_headers (fun);
468 loop_optimizer_finalize ();
469 return res;
472 /* Assume an earlier phase has already initialized all the loop structures that
473 we need here (and perhaps others too), and that these will be finalized by
474 a later phase. */
476 unsigned int
477 pass_ch_vect::execute (function *fun)
479 return copy_headers (fun);
482 /* Apply header copying according to a very simple test of do-while shape. */
484 bool
485 pass_ch::process_loop_p (struct loop *loop)
487 return !do_while_loop_p (loop);
490 /* Apply header-copying to loops where we might enable vectorization. */
492 bool
493 pass_ch_vect::process_loop_p (struct loop *loop)
495 if (!flag_tree_loop_vectorize && !loop->force_vectorize)
496 return false;
498 if (loop->dont_vectorize)
499 return false;
501 /* The vectorizer won't handle anything with multiple exits, so skip. */
502 edge exit = single_exit (loop);
503 if (!exit)
504 return false;
506 if (!do_while_loop_p (loop))
507 return true;
509 return false;
512 } // anon namespace
514 gimple_opt_pass *
515 make_pass_ch_vect (gcc::context *ctxt)
517 return new pass_ch_vect (ctxt);
520 gimple_opt_pass *
521 make_pass_ch (gcc::context *ctxt)
523 return new pass_ch (ctxt);