2 Copyright (C) 2004, 2005, 2007, 2008, 2010, 2012
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3, or (at your option) any
12 GCC is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
27 #include "basic-block.h"
28 #include "tree-flow.h"
31 #include "tree-pass.h"
32 #include "tree-inline.h"
34 /* This file implements the loop unswitching, i.e. transformation of loops like
47 where inv is the loop invariant, into
66 Inv is considered invariant iff the values it compares are both invariant;
67 tree-ssa-loop-im.c ensures that all the suitable conditions are in this
70 static struct loop
*tree_unswitch_loop (struct loop
*, basic_block
, tree
);
71 static bool tree_unswitch_single_loop (struct loop
*, int);
72 static tree
tree_may_unswitch_on (basic_block
, struct loop
*);
74 /* Main entry point. Perform loop unswitching on all suitable loops. */
77 tree_ssa_unswitch_loops (void)
82 HOST_WIDE_INT iterations
;
84 /* Go through inner loops (only original ones). */
85 FOR_EACH_LOOP (li
, loop
, LI_ONLY_INNERMOST
)
87 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
88 fprintf (dump_file
, ";; Considering loop %d\n", loop
->num
);
90 /* Do not unswitch in cold regions. */
91 if (optimize_loop_for_size_p (loop
))
93 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
94 fprintf (dump_file
, ";; Not unswitching cold loops\n");
98 /* The loop should not be too large, to limit code growth. */
99 if (tree_num_loop_insns (loop
, &eni_size_weights
)
100 > (unsigned) PARAM_VALUE (PARAM_MAX_UNSWITCH_INSNS
))
102 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
103 fprintf (dump_file
, ";; Not unswitching, loop too big\n");
107 /* If the loop is not expected to iterate, there is no need
109 iterations
= estimated_loop_iterations_int (loop
);
110 if (iterations
>= 0 && iterations
<= 1)
112 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
113 fprintf (dump_file
, ";; Not unswitching, loop is not expected to iterate\n");
117 changed
|= tree_unswitch_single_loop (loop
, 0);
121 return TODO_cleanup_cfg
;
125 /* Checks whether we can unswitch LOOP on condition at end of BB -- one of its
126 basic blocks (for what it means see comments below). */
129 tree_may_unswitch_on (basic_block bb
, struct loop
*loop
)
136 /* BB must end in a simple conditional jump. */
137 stmt
= last_stmt (bb
);
138 if (!stmt
|| gimple_code (stmt
) != GIMPLE_COND
)
141 /* To keep the things simple, we do not directly remove the conditions,
142 but just replace tests with 0 != 0 resp. 1 != 0. Prevent the infinite
143 loop where we would unswitch again on such a condition. */
144 if (gimple_cond_true_p (stmt
) || gimple_cond_false_p (stmt
))
147 /* Condition must be invariant. */
148 FOR_EACH_SSA_TREE_OPERAND (use
, stmt
, iter
, SSA_OP_USE
)
150 def
= SSA_NAME_DEF_STMT (use
);
151 def_bb
= gimple_bb (def
);
153 && flow_bb_inside_loop_p (loop
, def_bb
))
157 cond
= build2 (gimple_cond_code (stmt
), boolean_type_node
,
158 gimple_cond_lhs (stmt
), gimple_cond_rhs (stmt
));
163 /* Simplifies COND using checks in front of the entry of the LOOP. Just very
164 simplish (sufficient to prevent us from duplicating loop in unswitching
168 simplify_using_entry_checks (struct loop
*loop
, tree cond
)
170 edge e
= loop_preheader_edge (loop
);
175 stmt
= last_stmt (e
->src
);
177 && gimple_code (stmt
) == GIMPLE_COND
178 && gimple_cond_code (stmt
) == TREE_CODE (cond
)
179 && operand_equal_p (gimple_cond_lhs (stmt
),
180 TREE_OPERAND (cond
, 0), 0)
181 && operand_equal_p (gimple_cond_rhs (stmt
),
182 TREE_OPERAND (cond
, 1), 0))
183 return (e
->flags
& EDGE_TRUE_VALUE
185 : boolean_false_node
);
187 if (!single_pred_p (e
->src
))
190 e
= single_pred_edge (e
->src
);
191 if (e
->src
== ENTRY_BLOCK_PTR
)
196 /* Unswitch single LOOP. NUM is number of unswitchings done; we do not allow
197 it to grow too much, it is too easy to create example on that the code would
198 grow exponentially. */
201 tree_unswitch_single_loop (struct loop
*loop
, int num
)
206 tree cond
= NULL_TREE
;
208 bool changed
= false;
211 bbs
= get_loop_body (loop
);
212 found
= loop
->num_nodes
;
216 /* Find a bb to unswitch on. */
217 for (; i
< loop
->num_nodes
; i
++)
218 if ((cond
= tree_may_unswitch_on (bbs
[i
], loop
)))
221 if (i
== loop
->num_nodes
)
224 && num
> PARAM_VALUE (PARAM_MAX_UNSWITCH_LEVEL
)
225 && (dump_flags
& TDF_DETAILS
))
226 fprintf (dump_file
, ";; Not unswitching anymore, hit max level\n");
228 if (found
== loop
->num_nodes
)
236 cond
= simplify_using_entry_checks (loop
, cond
);
237 stmt
= last_stmt (bbs
[i
]);
238 if (integer_nonzerop (cond
))
240 /* Remove false path. */
241 gimple_cond_set_condition_from_tree (stmt
, boolean_true_node
);
244 else if (integer_zerop (cond
))
246 /* Remove true path. */
247 gimple_cond_set_condition_from_tree (stmt
, boolean_false_node
);
250 /* Do not unswitch too much. */
251 else if (num
> PARAM_VALUE (PARAM_MAX_UNSWITCH_LEVEL
))
256 /* In nested tree_unswitch_single_loop first optimize all conditions
257 using entry checks, then discover still reachable blocks in the
258 loop and find the condition only among those still reachable bbs. */
261 if (found
== loop
->num_nodes
)
278 basic_block
*tos
, *worklist
;
280 /* When called recursively, first do a quick discovery
281 of reachable bbs after the above changes and only
282 consider conditions in still reachable bbs. */
283 tos
= worklist
= XNEWVEC (basic_block
, loop
->num_nodes
);
285 for (i
= 0; i
< loop
->num_nodes
; i
++)
286 bbs
[i
]->flags
&= ~BB_REACHABLE
;
288 /* Start with marking header. */
290 bbs
[0]->flags
|= BB_REACHABLE
;
292 /* Iterate: find everything reachable from what we've already seen
293 within the same innermost loop. Don't look through false edges
294 if condition is always true or true edges if condition is
296 while (tos
!= worklist
)
298 basic_block b
= *--tos
;
303 if (EDGE_COUNT (b
->succs
) == 2)
305 gimple stmt
= last_stmt (b
);
307 && gimple_code (stmt
) == GIMPLE_COND
)
309 if (gimple_cond_true_p (stmt
))
310 flags
= EDGE_FALSE_VALUE
;
311 else if (gimple_cond_false_p (stmt
))
312 flags
= EDGE_TRUE_VALUE
;
316 FOR_EACH_EDGE (e
, ei
, b
->succs
)
318 basic_block dest
= e
->dest
;
320 if (dest
->loop_father
== loop
321 && !(dest
->flags
& BB_REACHABLE
)
322 && !(e
->flags
& flags
))
325 dest
->flags
|= BB_REACHABLE
;
332 /* Find a bb to unswitch on. */
333 for (; found
< loop
->num_nodes
; found
++)
334 if ((bbs
[found
]->flags
& BB_REACHABLE
)
335 && (cond
= tree_may_unswitch_on (bbs
[found
], loop
)))
338 if (found
== loop
->num_nodes
)
345 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
346 fprintf (dump_file
, ";; Unswitching loop\n");
348 initialize_original_copy_tables ();
349 /* Unswitch the loop on this condition. */
350 nloop
= tree_unswitch_loop (loop
, bbs
[found
], cond
);
353 free_original_copy_tables ();
358 /* Update the SSA form after unswitching. */
359 update_ssa (TODO_update_ssa
);
360 free_original_copy_tables ();
362 /* Invoke itself on modified loops. */
363 tree_unswitch_single_loop (nloop
, num
+ 1);
364 tree_unswitch_single_loop (loop
, num
+ 1);
369 /* Unswitch a LOOP w.r. to given basic block UNSWITCH_ON. We only support
370 unswitching of innermost loops. COND is the condition determining which
371 loop is entered -- the new loop is entered if COND is true. Returns NULL
372 if impossible, new loop otherwise. */
375 tree_unswitch_loop (struct loop
*loop
,
376 basic_block unswitch_on
, tree cond
)
379 edge edge_true
, edge_false
;
381 /* Some sanity checking. */
382 gcc_assert (flow_bb_inside_loop_p (loop
, unswitch_on
));
383 gcc_assert (EDGE_COUNT (unswitch_on
->succs
) == 2);
384 gcc_assert (loop
->inner
== NULL
);
386 extract_true_false_edges_from_block (unswitch_on
, &edge_true
, &edge_false
);
387 prob_true
= edge_true
->probability
;
388 return loop_version (loop
, unshare_expr (cond
),
389 NULL
, prob_true
, prob_true
,
390 REG_BR_PROB_BASE
- prob_true
, false);