]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/tree-ssa-loop-ch.c
[6/6] Preprocessor forced macro location
[thirdparty/gcc.git] / gcc / tree-ssa-loop-ch.c
CommitLineData
5f240ec4 1/* Loop header copying on trees.
85ec4feb 2 Copyright (C) 2004-2018 Free Software Foundation, Inc.
b8698a0f 3
5f240ec4 4This file is part of GCC.
b8698a0f 5
5f240ec4
ZD
6GCC is free software; you can redistribute it and/or modify it
7under the terms of the GNU General Public License as published by the
9dcd6f09 8Free Software Foundation; either version 3, or (at your option) any
5f240ec4 9later version.
b8698a0f 10
5f240ec4
ZD
11GCC is distributed in the hope that it will be useful, but WITHOUT
12ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
b8698a0f 15
5f240ec4 16You should have received a copy of the GNU General Public License
9dcd6f09
NC
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
5f240ec4
ZD
19
20#include "config.h"
21#include "system.h"
22#include "coretypes.h"
c7131fb2 23#include "backend.h"
5f240ec4 24#include "tree.h"
c7131fb2 25#include "gimple.h"
957060b5
AM
26#include "cfghooks.h"
27#include "tree-pass.h"
957060b5 28#include "gimple-ssa.h"
5be5c238 29#include "gimple-iterator.h"
442b4905
AM
30#include "tree-cfg.h"
31#include "tree-into-ssa.h"
5f240ec4
ZD
32#include "cfgloop.h"
33#include "tree-inline.h"
f6c72af4 34#include "tree-ssa-scopedtables.h"
4484a35a 35#include "tree-ssa-threadedge.h"
568876dc 36#include "params.h"
5f240ec4
ZD
37
38/* Duplicates headers of loops if they are small enough, so that the statements
39 in the loop body are always executed when the loop is entered. This
b01d837f 40 increases effectiveness of code motion optimizations, and reduces the need
5f240ec4
ZD
41 for loop preconditioning. */
42
43/* Check whether we should duplicate HEADER of LOOP. At most *LIMIT
44 instructions should be duplicated, limit is decreased by the actual
45 amount. */
46
47static bool
48should_duplicate_loop_header_p (basic_block header, struct loop *loop,
49 int *limit)
50{
726a989a 51 gimple_stmt_iterator bsi;
355fe088 52 gimple *last;
5f240ec4 53
174d7275 54 gcc_assert (!header->aux);
5f240ec4 55
cc870036
JH
56 /* Loop header copying usually increases size of the code. This used not to
57 be true, since quite often it is possible to verify that the condition is
58 satisfied in the first iteration and therefore to eliminate it. Jump
59 threading handles these cases now. */
b79fa2f6
RB
60 if (optimize_loop_for_size_p (loop)
61 && !loop->force_vectorize)
174d7275
JH
62 {
63 if (dump_file && (dump_flags & TDF_DETAILS))
64 fprintf (dump_file,
65 " Not duplicating bb %i: optimizing for size.\n",
66 header->index);
67 return false;
68 }
cc870036 69
628f6a4e 70 gcc_assert (EDGE_COUNT (header->succs) > 0);
c5cbcccf 71 if (single_succ_p (header))
174d7275
JH
72 {
73 if (dump_file && (dump_flags & TDF_DETAILS))
74 fprintf (dump_file,
75 " Not duplicating bb %i: it is single succ.\n",
76 header->index);
77 return false;
78 }
79
628f6a4e
BE
80 if (flow_bb_inside_loop_p (loop, EDGE_SUCC (header, 0)->dest)
81 && flow_bb_inside_loop_p (loop, EDGE_SUCC (header, 1)->dest))
174d7275
JH
82 {
83 if (dump_file && (dump_flags & TDF_DETAILS))
84 fprintf (dump_file,
85 " Not duplicating bb %i: both sucessors are in loop.\n",
86 loop->num);
87 return false;
88 }
5f240ec4
ZD
89
90 /* If this is not the original loop header, we want it to have just
91 one predecessor in order to match the && pattern. */
c5cbcccf 92 if (header != loop->header && !single_pred_p (header))
174d7275
JH
93 {
94 if (dump_file && (dump_flags & TDF_DETAILS))
95 fprintf (dump_file,
96 " Not duplicating bb %i: it has mutiple predecestors.\n",
97 header->index);
98 return false;
99 }
5f240ec4
ZD
100
101 last = last_stmt (header);
726a989a 102 if (gimple_code (last) != GIMPLE_COND)
174d7275
JH
103 {
104 if (dump_file && (dump_flags & TDF_DETAILS))
105 fprintf (dump_file,
106 " Not duplicating bb %i: it does not end by conditional.\n",
107 header->index);
108 return false;
109 }
5f240ec4 110
568876dc 111 /* Count number of instructions and punt on calls. */
726a989a 112 for (bsi = gsi_start_bb (header); !gsi_end_p (bsi); gsi_next (&bsi))
5f240ec4 113 {
726a989a 114 last = gsi_stmt (bsi);
5f240ec4 115
726a989a 116 if (gimple_code (last) == GIMPLE_LABEL)
5f240ec4
ZD
117 continue;
118
b5b8b0ac
AO
119 if (is_gimple_debug (last))
120 continue;
121
ce120587 122 if (gimple_code (last) == GIMPLE_CALL
90d43c80
BC
123 && (!gimple_inexpensive_call_p (as_a <gcall *> (last))
124 /* IFN_LOOP_DIST_ALIAS means that inner loop is distributed
125 at current loop's header. Don't copy in this case. */
126 || gimple_call_internal_p (last, IFN_LOOP_DIST_ALIAS)))
174d7275
JH
127 {
128 if (dump_file && (dump_flags & TDF_DETAILS))
129 fprintf (dump_file,
130 " Not duplicating bb %i: it contains call.\n",
131 header->index);
132 return false;
133 }
5f240ec4 134
7f9bc51b 135 *limit -= estimate_num_insns (last, &eni_size_weights);
5f240ec4 136 if (*limit < 0)
174d7275
JH
137 {
138 if (dump_file && (dump_flags & TDF_DETAILS))
139 fprintf (dump_file,
140 " Not duplicating bb %i contains too many insns.\n",
141 header->index);
142 return false;
143 }
5f240ec4 144 }
174d7275
JH
145 if (dump_file && (dump_flags & TDF_DETAILS))
146 fprintf (dump_file, " Will duplicate bb %i\n", header->index);
5f240ec4
ZD
147 return true;
148}
149
5f240ec4
ZD
150/* Checks whether LOOP is a do-while style loop. */
151
71343877 152static bool
5f240ec4
ZD
153do_while_loop_p (struct loop *loop)
154{
355fe088 155 gimple *stmt = last_stmt (loop->latch);
5f240ec4
ZD
156
157 /* If the latch of the loop is not empty, it is not a do-while loop. */
158 if (stmt
726a989a 159 && gimple_code (stmt) != GIMPLE_LABEL)
174d7275
JH
160 {
161 if (dump_file && (dump_flags & TDF_DETAILS))
162 fprintf (dump_file,
163 "Loop %i is not do-while loop: latch is not empty.\n",
164 loop->num);
165 return false;
166 }
5f240ec4 167
1c53fa8c
RB
168 /* If the latch does not have a single predecessor, it is not a
169 do-while loop. */
170 if (!single_pred_p (loop->latch))
171 {
172 if (dump_file && (dump_flags & TDF_DETAILS))
173 fprintf (dump_file,
174 "Loop %i is not do-while loop: latch has multiple "
175 "predecessors.\n", loop->num);
176 return false;
177 }
178
179 /* If the latch predecessor doesn't exit the loop, it is not a
180 do-while loop. */
181 if (!loop_exits_from_bb_p (loop, single_pred (loop->latch)))
174d7275
JH
182 {
183 if (dump_file && (dump_flags & TDF_DETAILS))
184 fprintf (dump_file,
1c53fa8c
RB
185 "Loop %i is not do-while loop: latch predecessor "
186 "does not exit loop.\n", loop->num);
174d7275
JH
187 return false;
188 }
1c53fa8c 189
174d7275
JH
190 if (dump_file && (dump_flags & TDF_DETAILS))
191 fprintf (dump_file, "Loop %i is do-while loop\n", loop->num);
5f240ec4
ZD
192
193 return true;
194}
195
be55bfe6
TS
196namespace {
197
4f9a2b4e
AL
198/* Common superclass for both header-copying phases. */
199class ch_base : public gimple_opt_pass
200{
201 protected:
202 ch_base (pass_data data, gcc::context *ctxt)
203 : gimple_opt_pass (data, ctxt)
204 {}
205
206 /* Copies headers of all loops in FUN for which process_loop_p is true. */
207 unsigned int copy_headers (function *fun);
208
209 /* Return true to copy headers of LOOP or false to skip. */
210 virtual bool process_loop_p (struct loop *loop) = 0;
211};
212
be55bfe6
TS
213const pass_data pass_data_ch =
214{
215 GIMPLE_PASS, /* type */
216 "ch", /* name */
217 OPTGROUP_LOOP, /* optinfo_flags */
be55bfe6
TS
218 TV_TREE_CH, /* tv_id */
219 ( PROP_cfg | PROP_ssa ), /* properties_required */
220 0, /* properties_provided */
221 0, /* properties_destroyed */
222 0, /* todo_flags_start */
9538c95b 223 0, /* todo_flags_finish */
be55bfe6
TS
224};
225
4f9a2b4e 226class pass_ch : public ch_base
be55bfe6
TS
227{
228public:
229 pass_ch (gcc::context *ctxt)
4f9a2b4e 230 : ch_base (pass_data_ch, ctxt)
be55bfe6
TS
231 {}
232
233 /* opt_pass methods: */
234 virtual bool gate (function *) { return flag_tree_ch != 0; }
4f9a2b4e
AL
235
236 /* Initialize and finalize loop structures, copying headers inbetween. */
be55bfe6
TS
237 virtual unsigned int execute (function *);
238
b5f34b42
TV
239 opt_pass * clone () { return new pass_ch (m_ctxt); }
240
4f9a2b4e
AL
241protected:
242 /* ch_base method: */
243 virtual bool process_loop_p (struct loop *loop);
be55bfe6
TS
244}; // class pass_ch
245
4f9a2b4e
AL
246const pass_data pass_data_ch_vect =
247{
248 GIMPLE_PASS, /* type */
249 "ch_vect", /* name */
250 OPTGROUP_LOOP, /* optinfo_flags */
251 TV_TREE_CH, /* tv_id */
252 ( PROP_cfg | PROP_ssa ), /* properties_required */
253 0, /* properties_provided */
254 0, /* properties_destroyed */
255 0, /* todo_flags_start */
256 0, /* todo_flags_finish */
257};
258
259/* This is a more aggressive version of the same pass, designed to run just
260 before if-conversion and vectorization, to put more loops into the form
261 required for those phases. */
262class pass_ch_vect : public ch_base
263{
264public:
265 pass_ch_vect (gcc::context *ctxt)
266 : ch_base (pass_data_ch_vect, ctxt)
267 {}
268
269 /* opt_pass methods: */
270 virtual bool gate (function *fun)
271 {
272 return flag_tree_ch != 0
273 && (flag_tree_loop_vectorize != 0 || fun->has_force_vectorize_loops);
274 }
275
276 /* Just copy headers, no initialization/finalization of loop structures. */
277 virtual unsigned int execute (function *);
278
279protected:
280 /* ch_base method: */
281 virtual bool process_loop_p (struct loop *loop);
282}; // class pass_ch_vect
283
284/* For all loops, copy the condition at the end of the loop body in front
285 of the loop. This is beneficial since it increases efficiency of
286 code motion optimizations. It also saves one jump on entry to the loop. */
287
be55bfe6 288unsigned int
4f9a2b4e 289ch_base::copy_headers (function *fun)
5f240ec4 290{
5f240ec4
ZD
291 struct loop *loop;
292 basic_block header;
33156717
JH
293 edge exit, entry;
294 basic_block *bbs, *copied_bbs;
42759f1e 295 unsigned n_bbs;
33156717 296 unsigned bbs_size;
9538c95b 297 bool changed = false;
5f240ec4 298
be55bfe6 299 if (number_of_loops (fun) <= 1)
d51157de 300 return 0;
5f240ec4 301
be55bfe6
TS
302 bbs = XNEWVEC (basic_block, n_basic_blocks_for_fn (fun));
303 copied_bbs = XNEWVEC (basic_block, n_basic_blocks_for_fn (fun));
304 bbs_size = n_basic_blocks_for_fn (fun);
42759f1e 305
f0bd40b1 306 FOR_EACH_LOOP (loop, 0)
5f240ec4 307 {
568876dc
JH
308 int initial_limit = PARAM_VALUE (PARAM_MAX_LOOP_HEADER_INSNS);
309 int remaining_limit = initial_limit;
174d7275
JH
310 if (dump_file && (dump_flags & TDF_DETAILS))
311 fprintf (dump_file,
312 "Analyzing loop %i\n", loop->num);
5f240ec4 313
42759f1e 314 header = loop->header;
5f240ec4
ZD
315
316 /* If the loop is already a do-while style one (either because it was
317 written as such, or because jump threading transformed it into one),
318 we might be in fact peeling the first iteration of the loop. This
1c53fa8c
RB
319 in general is not a good idea. Also avoid touching infinite loops. */
320 if (!loop_has_exit_edges (loop)
321 || !process_loop_p (loop))
5f240ec4
ZD
322 continue;
323
324 /* Iterate the header copying up to limit; this takes care of the cases
325 like while (a && b) {...}, where we want to have both of the conditions
326 copied. TODO -- handle while (a || b) - like cases, by not requiring
327 the header to have just a single successor and copying up to
42759f1e
ZD
328 postdominator. */
329
330 exit = NULL;
331 n_bbs = 0;
568876dc 332 while (should_duplicate_loop_header_p (header, loop, &remaining_limit))
5f240ec4 333 {
5f240ec4
ZD
334 /* Find a successor of header that is inside a loop; i.e. the new
335 header after the condition is copied. */
628f6a4e
BE
336 if (flow_bb_inside_loop_p (loop, EDGE_SUCC (header, 0)->dest))
337 exit = EDGE_SUCC (header, 0);
5f240ec4 338 else
628f6a4e 339 exit = EDGE_SUCC (header, 1);
42759f1e 340 bbs[n_bbs++] = header;
33156717 341 gcc_assert (bbs_size > n_bbs);
42759f1e 342 header = exit->dest;
2925cd9d
RB
343 /* Make sure to stop copying after we copied the first exit test.
344 Without further heuristics we do not want to rotate the loop
345 any further. */
346 if (loop_exits_from_bb_p (loop, exit->src))
347 break;
5f240ec4 348 }
5f240ec4 349
42759f1e
ZD
350 if (!exit)
351 continue;
5f240ec4 352
42759f1e
ZD
353 if (dump_file && (dump_flags & TDF_DETAILS))
354 fprintf (dump_file,
174d7275
JH
355 "Duplicating header of the loop %d up to edge %d->%d,"
356 " %i insns.\n",
568876dc
JH
357 loop->num, exit->src->index, exit->dest->index,
358 initial_limit - remaining_limit);
42759f1e
ZD
359
360 /* Ensure that the header will have just the latch as a predecessor
361 inside the loop. */
c5cbcccf 362 if (!single_pred_p (exit->dest))
598ec7bd 363 exit = single_pred_edge (split_edge (exit));
42759f1e 364
33156717 365 entry = loop_preheader_edge (loop);
33156717 366
6e02b5f5 367 propagate_threaded_block_debug_into (exit->dest, entry->dest);
f14540b6
SE
368 if (!gimple_duplicate_sese_region (entry, exit, bbs, n_bbs, copied_bbs,
369 true))
42759f1e
ZD
370 {
371 fprintf (dump_file, "Duplication failed.\n");
372 continue;
373 }
374
4df28528
ILT
375 /* If the loop has the form "for (i = j; i < j + 10; i++)" then
376 this copying can introduce a case where we rely on undefined
377 signed overflow to eliminate the preheader condition, because
378 we assume that "j < j + 10" is true. We don't want to warn
379 about that case for -Wstrict-overflow, because in general we
380 don't warn about overflow involving loops. Prevent the
726a989a 381 warning by setting the no_warning flag in the condition. */
4df28528
ILT
382 if (warn_strict_overflow > 0)
383 {
384 unsigned int i;
385
386 for (i = 0; i < n_bbs; ++i)
387 {
726a989a 388 gimple_stmt_iterator bsi;
e233ac97 389
726a989a
RB
390 for (bsi = gsi_start_bb (copied_bbs[i]);
391 !gsi_end_p (bsi);
392 gsi_next (&bsi))
e233ac97 393 {
355fe088 394 gimple *stmt = gsi_stmt (bsi);
726a989a
RB
395 if (gimple_code (stmt) == GIMPLE_COND)
396 gimple_set_no_warning (stmt, true);
397 else if (is_gimple_assign (stmt))
e233ac97 398 {
726a989a
RB
399 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
400 if (TREE_CODE_CLASS (rhs_code) == tcc_comparison)
401 gimple_set_no_warning (stmt, true);
e233ac97
ILT
402 }
403 }
4df28528
ILT
404 }
405 }
406
42759f1e
ZD
407 /* Ensure that the latch and the preheader is simple (we know that they
408 are not now, since there was the loop exit condition. */
598ec7bd
ZD
409 split_edge (loop_preheader_edge (loop));
410 split_edge (loop_latch_edge (loop));
9538c95b 411
1c53fa8c
RB
412 if (dump_file && (dump_flags & TDF_DETAILS))
413 {
414 if (do_while_loop_p (loop))
415 fprintf (dump_file, "Loop %d is now do-while loop.\n", loop->num);
416 else
417 fprintf (dump_file, "Loop %d is still not do-while loop.\n",
418 loop->num);
419 }
420
9538c95b 421 changed = true;
5f240ec4
ZD
422 }
423
4f9a2b4e
AL
424 if (changed)
425 update_ssa (TODO_update_ssa);
42759f1e 426 free (bbs);
33156717 427 free (copied_bbs);
42759f1e 428
9538c95b 429 return changed ? TODO_cleanup_cfg : 0;
5f240ec4
ZD
430}
431
4f9a2b4e
AL
432/* Initialize the loop structures we need, and finalize after. */
433
434unsigned int
435pass_ch::execute (function *fun)
436{
437 loop_optimizer_init (LOOPS_HAVE_PREHEADERS
1c53fa8c
RB
438 | LOOPS_HAVE_SIMPLE_LATCHES
439 | LOOPS_HAVE_RECORDED_EXITS);
4f9a2b4e
AL
440
441 unsigned int res = copy_headers (fun);
442
443 loop_optimizer_finalize ();
444 return res;
445}
446
447/* Assume an earlier phase has already initialized all the loop structures that
448 we need here (and perhaps others too), and that these will be finalized by
449 a later phase. */
450
451unsigned int
452pass_ch_vect::execute (function *fun)
453{
454 return copy_headers (fun);
455}
456
457/* Apply header copying according to a very simple test of do-while shape. */
458
459bool
460pass_ch::process_loop_p (struct loop *loop)
461{
462 return !do_while_loop_p (loop);
463}
464
465/* Apply header-copying to loops where we might enable vectorization. */
466
467bool
468pass_ch_vect::process_loop_p (struct loop *loop)
469{
0919ce3e 470 if (!flag_tree_loop_vectorize && !loop->force_vectorize)
4f9a2b4e
AL
471 return false;
472
473 if (loop->dont_vectorize)
474 return false;
475
476 if (!do_while_loop_p (loop))
477 return true;
478
479 /* The vectorizer won't handle anything with multiple exits, so skip. */
480 edge exit = single_exit (loop);
481 if (!exit)
482 return false;
483
484 /* Copy headers iff there looks to be code in the loop after the exit block,
485 i.e. the exit block has an edge to another block (besides the latch,
486 which should be empty). */
487 edge_iterator ei;
488 edge e;
489 FOR_EACH_EDGE (e, ei, exit->src->succs)
490 if (!loop_exit_edge_p (loop, e)
491 && e->dest != loop->header
492 && e->dest != loop->latch)
493 return true;
494
495 return false;
496}
497
27a4cd48
DM
498} // anon namespace
499
4f9a2b4e
AL
500gimple_opt_pass *
501make_pass_ch_vect (gcc::context *ctxt)
502{
503 return new pass_ch_vect (ctxt);
504}
505
27a4cd48
DM
506gimple_opt_pass *
507make_pass_ch (gcc::context *ctxt)
508{
509 return new pass_ch (ctxt);
510}