1 /* Loop unswitching for GNU compiler.
2 Copyright (C) 2002, 2003 Free Software Foundation, Inc.
4 This file is part of GCC.
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
8 Software Foundation; either version 2, or (at your option) any later
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
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING. If not, write to the Free
18 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 #include "coretypes.h"
26 #include "hard-reg-set.h"
27 #include "basic-block.h"
29 #include "cfglayout.h"
34 /* This pass moves constant conditions out of loops, duplicating the loop
35 in progress, i.e. this code:
49 where nothing inside the loop alters cond is transformed
74 Duplicating the loop might lead to code growth exponential in number of
75 branches inside loop, so we limit the number of unswitchings performed
76 in a single loop to PARAM_MAX_UNSWITCH_LEVEL. We only perform the
77 transformation on innermost loops, as the benefit of doing it on loops
78 containing subloops would not be very large compared to complications
79 with handling this case. */
81 static struct loop
*unswitch_loop (struct loops
*, struct loop
*,
83 static void unswitch_single_loop (struct loops
*, struct loop
*, rtx
, int);
84 static bool may_unswitch_on_p (basic_block
, struct loop
*,
86 static rtx
reversed_condition (rtx
);
88 /* Main entry point. Perform loop unswitching on all suitable LOOPS. */
90 unswitch_loops (struct loops
*loops
)
95 /* Go through inner loops (only original ones). */
98 for (i
= 1; i
< num
; i
++)
101 loop
= loops
->parray
[i
];
108 unswitch_single_loop (loops
, loop
, NULL_RTX
, 0);
109 #ifdef ENABLE_CHECKING
110 verify_dominators (CDI_DOMINATORS
);
111 verify_loop_structure (loops
);
116 /* Checks whether we can unswitch LOOP on condition at end of BB -- one of its
117 basic blocks (for what it means see comments below). List of basic blocks
118 inside LOOP is provided in BODY to save time. */
120 may_unswitch_on_p (basic_block bb
, struct loop
*loop
, basic_block
*body
)
125 /* BB must end in a simple conditional jump. */
126 if (!bb
->succ
|| !bb
->succ
->succ_next
|| bb
->succ
->succ_next
->succ_next
)
128 if (!any_condjump_p (BB_END (bb
)))
131 /* With branches inside loop. */
132 if (!flow_bb_inside_loop_p (loop
, bb
->succ
->dest
)
133 || !flow_bb_inside_loop_p (loop
, bb
->succ
->succ_next
->dest
))
136 /* It must be executed just once each iteration (because otherwise we
137 are unable to update dominator/irreducible loop information correctly). */
138 if (!just_once_each_iteration_p (loop
, bb
))
141 /* Condition must be invariant. We use just a stupid test of invariantness
142 of the condition: all used regs must not be modified inside loop body. */
143 test
= get_condition (BB_END (bb
), NULL
, true);
147 for (i
= 0; i
< loop
->num_nodes
; i
++)
148 if (modified_between_p (test
, BB_HEAD (body
[i
]), NEXT_INSN (BB_END (body
[i
]))))
154 /* Reverses CONDition; returns NULL if we cannot. */
156 reversed_condition (rtx cond
)
158 enum rtx_code reversed
;
159 reversed
= reversed_comparison_code (cond
, NULL
);
160 if (reversed
== UNKNOWN
)
163 return gen_rtx_fmt_ee (reversed
,
164 GET_MODE (cond
), XEXP (cond
, 0),
168 /* Unswitch single LOOP. COND_CHECKED holds list of conditions we already
169 unswitched on and are therefore known to be true in this LOOP. NUM is
170 number of unswitchings done; do not allow it to grow too much, it is too
171 easy to create example on that the code would grow exponentially. */
173 unswitch_single_loop (struct loops
*loops
, struct loop
*loop
,
174 rtx cond_checked
, int num
)
176 basic_block
*bbs
, bb
;
180 rtx cond
, rcond
, conds
, rconds
, acond
, split_before
;
186 /* Do not unswitch too much. */
187 if (num
> PARAM_VALUE (PARAM_MAX_UNSWITCH_LEVEL
))
190 fprintf (rtl_dump_file
, ";; Not unswitching anymore, hit max level\n");
194 /* Only unswitch innermost loops. */
198 fprintf (rtl_dump_file
, ";; Not unswitching, not innermost loop\n");
202 /* We must be able to duplicate loop body. */
203 if (!can_duplicate_loop_p (loop
))
206 fprintf (rtl_dump_file
, ";; Not unswitching, can't duplicate loop\n");
210 /* The loop should not be too large, to limit code growth. */
211 if (num_loop_insns (loop
) > PARAM_VALUE (PARAM_MAX_UNSWITCH_INSNS
))
214 fprintf (rtl_dump_file
, ";; Not unswitching, loop too big\n");
218 /* Do not unswitch in cold areas. */
219 if (!maybe_hot_bb_p (loop
->header
))
222 fprintf (rtl_dump_file
, ";; Not unswitching, not hot area\n");
226 /* Nor if the loop usually does not roll. */
227 if (expected_loop_iterations (loop
) < 1)
230 fprintf (rtl_dump_file
, ";; Not unswitching, loop iterations < 1\n");
238 /* Find a bb to unswitch on. */
239 bbs
= get_loop_body (loop
);
240 for (i
= 0; i
< loop
->num_nodes
; i
++)
241 if (may_unswitch_on_p (bbs
[i
], loop
, bbs
))
244 if (i
== loop
->num_nodes
)
250 if (!(cond
= get_condition (BB_END (bbs
[i
]), &split_before
, true)))
252 rcond
= reversed_condition (cond
);
254 /* Check whether the result can be predicted. */
257 for (acond
= cond_checked
; acond
; acond
= XEXP (acond
, 1))
259 if (rtx_equal_p (cond
, XEXP (acond
, 0)))
264 if (rtx_equal_p (rcond
, XEXP (acond
, 0)))
273 /* Remove false path. */
274 for (e
= bbs
[i
]->succ
; !(e
->flags
& EDGE_FALLTHRU
); e
= e
->succ_next
);
275 remove_path (loops
, e
);
279 else if (always_false
)
281 /* Remove true path. */
282 for (e
= bbs
[i
]->succ
; e
->flags
& EDGE_FALLTHRU
; e
= e
->succ_next
);
283 remove_path (loops
, e
);
289 /* We found the condition we can unswitch on. */
290 conds
= alloc_EXPR_LIST (0, cond
, cond_checked
);
292 rconds
= alloc_EXPR_LIST (0, rcond
, cond_checked
);
294 rconds
= cond_checked
;
296 /* Separate condition in a single basic block. */
297 bb
= split_loop_bb (bbs
[i
], PREV_INSN (split_before
))->dest
;
299 true_first
= !(bb
->succ
->flags
& EDGE_FALLTHRU
);
301 fprintf (rtl_dump_file
, ";; Unswitching loop\n");
303 /* Unswitch the loop on this condition. */
304 nloop
= unswitch_loop (loops
, loop
, bb
);
308 /* Invoke itself on modified loops. */
309 unswitch_single_loop (loops
, nloop
, true_first
? conds
: rconds
, num
+ 1);
310 unswitch_single_loop (loops
, loop
, true_first
? rconds
: conds
, num
+ 1);
312 free_EXPR_LIST_node (conds
);
314 free_EXPR_LIST_node (rconds
);
317 /* Unswitch a LOOP w.r. to given basic block UNSWITCH_ON. We only support
318 unswitching of innermost loops. UNSWITCH_ON must be executed in every
319 iteration, i.e. it must dominate LOOP latch, and should only contain code
320 for the condition we unswitch on. Returns NULL if impossible, new
323 unswitch_loop (struct loops
*loops
, struct loop
*loop
, basic_block unswitch_on
)
325 edge entry
, latch_edge
;
326 basic_block switch_bb
, unswitch_on_alt
, src
;
331 /* Some sanity checking. */
332 if (!flow_bb_inside_loop_p (loop
, unswitch_on
))
334 if (!unswitch_on
->succ
|| !unswitch_on
->succ
->succ_next
||
335 unswitch_on
->succ
->succ_next
->succ_next
)
337 if (!just_once_each_iteration_p (loop
, unswitch_on
))
341 if (!flow_bb_inside_loop_p (loop
, unswitch_on
->succ
->dest
))
343 if (!flow_bb_inside_loop_p (loop
, unswitch_on
->succ
->succ_next
->dest
))
346 /* Will we be able to perform redirection? */
347 if (!any_condjump_p (BB_END (unswitch_on
)))
349 if (!cfg_layout_can_duplicate_bb_p (unswitch_on
))
352 entry
= loop_preheader_edge (loop
);
356 irred_flag
= entry
->flags
& EDGE_IRREDUCIBLE_LOOP
;
357 entry
->flags
&= ~EDGE_IRREDUCIBLE_LOOP
;
358 zero_bitmap
= sbitmap_alloc (2);
359 sbitmap_zero (zero_bitmap
);
360 if (!duplicate_loop_to_header_edge (loop
, entry
, loops
, 1,
361 zero_bitmap
, NULL
, NULL
, NULL
, 0))
364 entry
->flags
|= irred_flag
;
366 /* Record the block with condition we unswitch on. */
367 unswitch_on_alt
= unswitch_on
->rbi
->copy
;
369 /* Make a copy of the block containing the condition; we will use
370 it as switch to decide which loop we want to use. */
371 switch_bb
= cfg_layout_duplicate_bb (unswitch_on
, NULL
);
374 switch_bb
->flags
|= BB_IRREDUCIBLE_LOOP
;
375 switch_bb
->succ
->flags
|= EDGE_IRREDUCIBLE_LOOP
;
376 switch_bb
->succ
->succ_next
->flags
|= EDGE_IRREDUCIBLE_LOOP
;
380 switch_bb
->flags
&= ~BB_IRREDUCIBLE_LOOP
;
381 switch_bb
->succ
->flags
&= ~EDGE_IRREDUCIBLE_LOOP
;
382 switch_bb
->succ
->succ_next
->flags
&= ~EDGE_IRREDUCIBLE_LOOP
;
384 add_to_dominance_info (CDI_DOMINATORS
, switch_bb
);
385 unswitch_on
->rbi
->copy
= unswitch_on_alt
;
387 /* Loopify from the copy of LOOP body, constructing the new loop. */
388 for (latch_edge
= loop
->latch
->rbi
->copy
->succ
;
389 latch_edge
->dest
!= loop
->header
;
390 latch_edge
= latch_edge
->succ_next
);
391 nloop
= loopify (loops
, latch_edge
,
392 loop
->header
->rbi
->copy
->pred
, switch_bb
);
394 /* Remove branches that are now unreachable in new loops. We rely on the
395 fact that cfg_layout_duplicate_bb reverses list of edges. */
396 remove_path (loops
, unswitch_on
->succ
);
397 remove_path (loops
, unswitch_on_alt
->succ
);
399 /* One of created loops do not have to be subloop of the outer loop now,
400 so fix its placement in loop data structure. */
401 fix_loop_placement (loop
);
402 fix_loop_placement (nloop
);
404 /* Preserve the simple loop preheaders. */
405 loop_split_edge_with (loop_preheader_edge (loop
), NULL_RTX
);
406 loop_split_edge_with (loop_preheader_edge (nloop
), NULL_RTX
);