2 Copyright (C) 2004-2015 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
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
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/>. */
22 #include "coretypes.h"
27 #include "double-int.h"
34 #include "fold-const.h"
37 #include "hard-reg-set.h"
40 #include "dominance.h"
42 #include "basic-block.h"
43 #include "tree-ssa-alias.h"
44 #include "internal-fn.h"
45 #include "gimple-expr.h"
49 #include "gimple-ssa.h"
51 #include "tree-phinodes.h"
52 #include "ssa-iterators.h"
53 #include "tree-ssa-loop-niter.h"
54 #include "tree-ssa-loop.h"
55 #include "tree-into-ssa.h"
58 #include "tree-pass.h"
59 #include "tree-inline.h"
61 /* This file implements the loop unswitching, i.e. transformation of loops like
74 where inv is the loop invariant, into
93 Inv is considered invariant iff the values it compares are both invariant;
94 tree-ssa-loop-im.c ensures that all the suitable conditions are in this
97 static struct loop
*tree_unswitch_loop (struct loop
*, basic_block
, tree
);
98 static bool tree_unswitch_single_loop (struct loop
*, int);
99 static tree
tree_may_unswitch_on (basic_block
, struct loop
*);
101 /* Main entry point. Perform loop unswitching on all suitable loops. */
104 tree_ssa_unswitch_loops (void)
107 bool changed
= false;
108 HOST_WIDE_INT iterations
;
110 /* Go through inner loops (only original ones). */
111 FOR_EACH_LOOP (loop
, LI_ONLY_INNERMOST
)
113 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
114 fprintf (dump_file
, ";; Considering loop %d\n", loop
->num
);
116 /* Do not unswitch in cold regions. */
117 if (optimize_loop_for_size_p (loop
))
119 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
120 fprintf (dump_file
, ";; Not unswitching cold loops\n");
124 /* The loop should not be too large, to limit code growth. */
125 if (tree_num_loop_insns (loop
, &eni_size_weights
)
126 > (unsigned) PARAM_VALUE (PARAM_MAX_UNSWITCH_INSNS
))
128 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
129 fprintf (dump_file
, ";; Not unswitching, loop too big\n");
133 /* If the loop is not expected to iterate, there is no need
135 iterations
= estimated_loop_iterations_int (loop
);
136 if (iterations
>= 0 && iterations
<= 1)
138 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
139 fprintf (dump_file
, ";; Not unswitching, loop is not expected to iterate\n");
143 changed
|= tree_unswitch_single_loop (loop
, 0);
147 return TODO_cleanup_cfg
;
151 /* Checks whether we can unswitch LOOP on condition at end of BB -- one of its
152 basic blocks (for what it means see comments below). */
155 tree_may_unswitch_on (basic_block bb
, struct loop
*loop
)
163 /* BB must end in a simple conditional jump. */
164 last
= last_stmt (bb
);
165 if (!last
|| gimple_code (last
) != GIMPLE_COND
)
167 stmt
= as_a
<gcond
*> (last
);
169 /* To keep the things simple, we do not directly remove the conditions,
170 but just replace tests with 0 != 0 resp. 1 != 0. Prevent the infinite
171 loop where we would unswitch again on such a condition. */
172 if (gimple_cond_true_p (stmt
) || gimple_cond_false_p (stmt
))
175 /* Condition must be invariant. */
176 FOR_EACH_SSA_TREE_OPERAND (use
, stmt
, iter
, SSA_OP_USE
)
178 def
= SSA_NAME_DEF_STMT (use
);
179 def_bb
= gimple_bb (def
);
181 && flow_bb_inside_loop_p (loop
, def_bb
))
185 cond
= build2 (gimple_cond_code (stmt
), boolean_type_node
,
186 gimple_cond_lhs (stmt
), gimple_cond_rhs (stmt
));
191 /* Simplifies COND using checks in front of the entry of the LOOP. Just very
192 simplish (sufficient to prevent us from duplicating loop in unswitching
196 simplify_using_entry_checks (struct loop
*loop
, tree cond
)
198 edge e
= loop_preheader_edge (loop
);
203 stmt
= last_stmt (e
->src
);
205 && gimple_code (stmt
) == GIMPLE_COND
206 && gimple_cond_code (stmt
) == TREE_CODE (cond
)
207 && operand_equal_p (gimple_cond_lhs (stmt
),
208 TREE_OPERAND (cond
, 0), 0)
209 && operand_equal_p (gimple_cond_rhs (stmt
),
210 TREE_OPERAND (cond
, 1), 0))
211 return (e
->flags
& EDGE_TRUE_VALUE
213 : boolean_false_node
);
215 if (!single_pred_p (e
->src
))
218 e
= single_pred_edge (e
->src
);
219 if (e
->src
== ENTRY_BLOCK_PTR_FOR_FN (cfun
))
224 /* Unswitch single LOOP. NUM is number of unswitchings done; we do not allow
225 it to grow too much, it is too easy to create example on that the code would
226 grow exponentially. */
229 tree_unswitch_single_loop (struct loop
*loop
, int num
)
234 tree cond
= NULL_TREE
;
236 bool changed
= false;
239 bbs
= get_loop_body (loop
);
240 found
= loop
->num_nodes
;
244 /* Find a bb to unswitch on. */
245 for (; i
< loop
->num_nodes
; i
++)
246 if ((cond
= tree_may_unswitch_on (bbs
[i
], loop
)))
249 if (i
== loop
->num_nodes
)
252 && num
> PARAM_VALUE (PARAM_MAX_UNSWITCH_LEVEL
)
253 && (dump_flags
& TDF_DETAILS
))
254 fprintf (dump_file
, ";; Not unswitching anymore, hit max level\n");
256 if (found
== loop
->num_nodes
)
264 cond
= simplify_using_entry_checks (loop
, cond
);
265 stmt
= last_stmt (bbs
[i
]);
266 if (integer_nonzerop (cond
))
268 /* Remove false path. */
269 gimple_cond_set_condition_from_tree (as_a
<gcond
*> (stmt
),
273 else if (integer_zerop (cond
))
275 /* Remove true path. */
276 gimple_cond_set_condition_from_tree (as_a
<gcond
*> (stmt
),
280 /* Do not unswitch too much. */
281 else if (num
> PARAM_VALUE (PARAM_MAX_UNSWITCH_LEVEL
))
286 /* In nested tree_unswitch_single_loop first optimize all conditions
287 using entry checks, then discover still reachable blocks in the
288 loop and find the condition only among those still reachable bbs. */
291 if (found
== loop
->num_nodes
)
308 basic_block
*tos
, *worklist
;
310 /* When called recursively, first do a quick discovery
311 of reachable bbs after the above changes and only
312 consider conditions in still reachable bbs. */
313 tos
= worklist
= XNEWVEC (basic_block
, loop
->num_nodes
);
315 for (i
= 0; i
< loop
->num_nodes
; i
++)
316 bbs
[i
]->flags
&= ~BB_REACHABLE
;
318 /* Start with marking header. */
320 bbs
[0]->flags
|= BB_REACHABLE
;
322 /* Iterate: find everything reachable from what we've already seen
323 within the same innermost loop. Don't look through false edges
324 if condition is always true or true edges if condition is
326 while (tos
!= worklist
)
328 basic_block b
= *--tos
;
333 if (EDGE_COUNT (b
->succs
) == 2)
335 gimple stmt
= last_stmt (b
);
337 && gimple_code (stmt
) == GIMPLE_COND
)
339 gcond
*cond_stmt
= as_a
<gcond
*> (stmt
);
340 if (gimple_cond_true_p (cond_stmt
))
341 flags
= EDGE_FALSE_VALUE
;
342 else if (gimple_cond_false_p (cond_stmt
))
343 flags
= EDGE_TRUE_VALUE
;
347 FOR_EACH_EDGE (e
, ei
, b
->succs
)
349 basic_block dest
= e
->dest
;
351 if (dest
->loop_father
== loop
352 && !(dest
->flags
& BB_REACHABLE
)
353 && !(e
->flags
& flags
))
356 dest
->flags
|= BB_REACHABLE
;
363 /* Find a bb to unswitch on. */
364 for (; found
< loop
->num_nodes
; found
++)
365 if ((bbs
[found
]->flags
& BB_REACHABLE
)
366 && (cond
= tree_may_unswitch_on (bbs
[found
], loop
)))
369 if (found
== loop
->num_nodes
)
376 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
377 fprintf (dump_file
, ";; Unswitching loop\n");
379 initialize_original_copy_tables ();
380 /* Unswitch the loop on this condition. */
381 nloop
= tree_unswitch_loop (loop
, bbs
[found
], cond
);
384 free_original_copy_tables ();
389 /* Update the SSA form after unswitching. */
390 update_ssa (TODO_update_ssa
);
391 free_original_copy_tables ();
393 /* Invoke itself on modified loops. */
394 tree_unswitch_single_loop (nloop
, num
+ 1);
395 tree_unswitch_single_loop (loop
, num
+ 1);
400 /* Unswitch a LOOP w.r. to given basic block UNSWITCH_ON. We only support
401 unswitching of innermost loops. COND is the condition determining which
402 loop is entered -- the new loop is entered if COND is true. Returns NULL
403 if impossible, new loop otherwise. */
406 tree_unswitch_loop (struct loop
*loop
,
407 basic_block unswitch_on
, tree cond
)
410 edge edge_true
, edge_false
;
412 /* Some sanity checking. */
413 gcc_assert (flow_bb_inside_loop_p (loop
, unswitch_on
));
414 gcc_assert (EDGE_COUNT (unswitch_on
->succs
) == 2);
415 gcc_assert (loop
->inner
== NULL
);
417 extract_true_false_edges_from_block (unswitch_on
, &edge_true
, &edge_false
);
418 prob_true
= edge_true
->probability
;
419 return loop_version (loop
, unshare_expr (cond
),
420 NULL
, prob_true
, prob_true
,
421 REG_BR_PROB_BASE
- prob_true
, false);
424 /* Loop unswitching pass. */
428 const pass_data pass_data_tree_unswitch
=
430 GIMPLE_PASS
, /* type */
431 "unswitch", /* name */
432 OPTGROUP_LOOP
, /* optinfo_flags */
433 TV_TREE_LOOP_UNSWITCH
, /* tv_id */
434 PROP_cfg
, /* properties_required */
435 0, /* properties_provided */
436 0, /* properties_destroyed */
437 0, /* todo_flags_start */
438 0, /* todo_flags_finish */
441 class pass_tree_unswitch
: public gimple_opt_pass
444 pass_tree_unswitch (gcc::context
*ctxt
)
445 : gimple_opt_pass (pass_data_tree_unswitch
, ctxt
)
448 /* opt_pass methods: */
449 virtual bool gate (function
*) { return flag_unswitch_loops
!= 0; }
450 virtual unsigned int execute (function
*);
452 }; // class pass_tree_unswitch
455 pass_tree_unswitch::execute (function
*fun
)
457 if (number_of_loops (fun
) <= 1)
460 return tree_ssa_unswitch_loops ();
466 make_pass_tree_unswitch (gcc::context
*ctxt
)
468 return new pass_tree_unswitch (ctxt
);