2 Copyright (C) 2004-2022 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"
26 #include "tree-pass.h"
28 #include "fold-const.h"
32 #include "tree-ssa-loop-niter.h"
33 #include "tree-ssa-loop.h"
34 #include "tree-into-ssa.h"
36 #include "tree-inline.h"
37 #include "gimple-iterator.h"
39 #include "tree-ssa-loop-manip.h"
40 #include "tree-vectorizer.h"
41 #include "tree-pretty-print.h"
42 #include "gimple-range.h"
46 /* This file implements the loop unswitching, i.e. transformation of loops like
59 where inv is the loop invariant, into
78 Inv is considered invariant iff the values it compares are both invariant;
79 tree-ssa-loop-im.cc ensures that all the suitable conditions are in this
82 /* Loop unswitching algorithm for innermost loops works in the following steps:
84 1) Number of instructions is estimated for each BB that belongs to a loop.
85 2) Unswitching candidates are found for gcond and gswitch statements
86 (note that an unswitching predicate for a gswitch actually corresponds
87 to a non-default edge so it can contain multiple cases).
88 3) The so called unswitch predicates are stored in a cache where the
89 gimple_uid of the last stmt in a basic-block is an index to the cache.
90 4) We consider one by one the unswitching candidates and calculate BBs that
91 will be reachable in the unswitch version.
92 5) A selected predicate is chosen and we simplify the CFG (dead edges) in
93 both versions of the loop. We utilize both Ranger for condition
94 simplification and also symbol equivalence. The folded if conditions
95 are replaced with true/false values, while for gswitch we mark the
96 corresponding edges with a pass-defined unreachable flag.
97 6) Every time we unswitch a loop, we save unswitch_predicate to a vector
98 together with information if true or false edge was taken. Doing that
99 we have a so called PREDICATE_PATH that is utilized for simplification
101 7) The process is repeated until we reach a growth threshold or all
102 unswitching opportunities are taken. */
104 /* A tuple that holds a GENERIC condition and value range for an unswitching
107 struct unswitch_predicate
109 /* CTOR for a switch edge predicate. */
110 unswitch_predicate (tree cond
, tree lhs_
, int edge_index_
, edge e
,
111 const int_range_max
& edge_range
)
112 : condition (cond
), lhs (lhs_
),
113 true_range (edge_range
), edge_index (edge_index_
), switch_p (true)
115 gcc_assert (!(e
->flags
& (EDGE_TRUE_VALUE
|EDGE_FALSE_VALUE
))
116 && irange::supports_p (TREE_TYPE (lhs
)));
117 false_range
= true_range
;
118 if (!false_range
.varying_p ()
119 && !false_range
.undefined_p ())
120 false_range
.invert ();
121 num
= predicates
->length ();
122 predicates
->safe_push (this);
125 /* CTOR for a GIMPLE condition statement. */
126 unswitch_predicate (gcond
*stmt
)
129 if (EDGE_SUCC (gimple_bb (stmt
), 0)->flags
& EDGE_TRUE_VALUE
)
133 lhs
= gimple_cond_lhs (stmt
);
134 tree rhs
= gimple_cond_rhs (stmt
);
135 enum tree_code code
= gimple_cond_code (stmt
);
136 condition
= build2 (code
, boolean_type_node
, lhs
, rhs
);
137 if (irange::supports_p (TREE_TYPE (lhs
)))
139 auto range_op
= range_op_handler (code
, TREE_TYPE (lhs
));
140 int_range
<2> rhs_range (TREE_TYPE (rhs
));
141 if (CONSTANT_CLASS_P (rhs
))
142 rhs_range
.set (rhs
, rhs
);
143 if (!range_op
.op1_range (true_range
, TREE_TYPE (lhs
),
144 int_range
<2> (boolean_true_node
,
145 boolean_true_node
), rhs_range
)
146 || !range_op
.op1_range (false_range
, TREE_TYPE (lhs
),
147 int_range
<2> (boolean_false_node
,
151 true_range
.set_varying (TREE_TYPE (lhs
));
152 false_range
.set_varying (TREE_TYPE (lhs
));
155 num
= predicates
->length ();
156 predicates
->safe_push (this);
159 /* Copy ranges for purpose of usage in predicate path. */
162 copy_merged_ranges ()
164 merged_true_range
= true_range
;
165 merged_false_range
= false_range
;
168 /* GENERIC unswitching expression testing LHS against CONSTANT. */
171 /* LHS of the expression. */
174 /* Initial ranges (when the expression is true/false) for the expression. */
175 int_range_max true_range
= {}, false_range
= {};
177 /* Modified range that is part of a predicate path. */
178 int_range_max merged_true_range
= {}, merged_false_range
= {};
180 /* Index of the edge the predicate belongs to in the successor vector. */
183 /* Whether the predicate was created from a switch statement. */
186 /* The number of the predicate in the predicates vector below. */
189 /* Vector of all used predicates, used for assigning a unique id that
190 can be used for bitmap operations. */
191 static vec
<unswitch_predicate
*> *predicates
;
194 vec
<unswitch_predicate
*> *unswitch_predicate::predicates
;
196 /* Ranger instance used in the pass. */
197 static gimple_ranger
*ranger
= NULL
;
199 /* Cache storage for unswitch_predicate belonging to a basic block. */
200 static vec
<vec
<unswitch_predicate
*>> *bb_predicates
;
202 /* The type represents a predicate path leading to a basic block. */
203 typedef vec
<std::pair
<unswitch_predicate
*, bool>> predicate_vector
;
205 static class loop
*tree_unswitch_loop (class loop
*, edge
, tree
);
206 static bool tree_unswitch_single_loop (class loop
*, dump_user_location_t
,
207 predicate_vector
&predicate_path
,
208 unsigned loop_size
, unsigned &budget
,
209 int ignored_edge_flag
, bitmap
);
211 find_unswitching_predicates_for_bb (basic_block bb
, class loop
*loop
,
212 vec
<unswitch_predicate
*> &candidates
);
213 static bool tree_unswitch_outer_loop (class loop
*);
214 static edge
find_loop_guard (class loop
*, vec
<gimple
*>&);
215 static bool empty_bb_without_guard_p (class loop
*, basic_block
,
217 static bool used_outside_loop_p (class loop
*, tree
, vec
<gimple
*>&);
218 static void hoist_guard (class loop
*, edge
);
219 static bool check_exit_phi (class loop
*);
220 static tree
get_vop_from_header (class loop
*);
221 static void clean_up_after_unswitching (int);
223 /* Return vector of predicates that belong to a basic block. */
225 static vec
<unswitch_predicate
*> &
226 get_predicates_for_bb (basic_block bb
)
228 gimple
*last
= last_stmt (bb
);
229 return (*bb_predicates
)[last
== NULL
? 0 : gimple_uid (last
)];
232 /* Save predicates that belong to a basic block. */
235 set_predicates_for_bb (basic_block bb
, vec
<unswitch_predicate
*> predicates
)
237 gimple_set_uid (last_stmt (bb
), bb_predicates
->length ());
238 bb_predicates
->safe_push (predicates
);
241 /* Initialize LOOP information reused during the unswitching pass.
242 Return total number of instructions in the loop. */
245 init_loop_unswitch_info (class loop
*loop
)
247 unsigned total_insns
= 0;
249 /* Calculate instruction count. */
250 basic_block
*bbs
= get_loop_body (loop
);
251 for (unsigned i
= 0; i
< loop
->num_nodes
; i
++)
254 for (gimple_stmt_iterator gsi
= gsi_start_bb (bbs
[i
]); !gsi_end_p (gsi
);
256 insns
+= estimate_num_insns (gsi_stmt (gsi
), &eni_size_weights
);
258 bbs
[i
]->aux
= (void *)(uintptr_t)insns
;
259 total_insns
+= insns
;
262 /* Find all unswitching candidates. */
263 for (unsigned i
= 0; i
!= loop
->num_nodes
; i
++)
265 /* Find a bb to unswitch on. */
266 vec
<unswitch_predicate
*> candidates
;
267 candidates
.create (1);
268 find_unswitching_predicates_for_bb (bbs
[i
], loop
, candidates
);
269 if (!candidates
.is_empty ())
270 set_predicates_for_bb (bbs
[i
], candidates
);
273 candidates
.release ();
274 gimple
*last
= last_stmt (bbs
[i
]);
276 gimple_set_uid (last
, 0);
285 /* Main entry point. Perform loop unswitching on all suitable loops. */
288 tree_ssa_unswitch_loops (function
*fun
)
290 bool changed_unswitch
= false;
291 bool changed_hoist
= false;
292 auto_edge_flag
ignored_edge_flag (fun
);
294 ranger
= enable_ranger (fun
);
296 /* Go through all loops starting from innermost. */
297 for (auto loop
: loops_list (fun
, LI_FROM_INNERMOST
))
301 /* Perform initial tests if unswitch is eligible. */
302 dump_user_location_t loc
= find_loop_location (loop
);
304 /* Do not unswitch in cold regions. */
305 if (optimize_loop_for_size_p (loop
))
307 if (dump_enabled_p ())
308 dump_printf_loc (MSG_NOTE
, loc
,
309 "Not unswitching cold loops\n");
313 /* If the loop is not expected to iterate, there is no need
315 HOST_WIDE_INT iterations
= estimated_loop_iterations_int (loop
);
317 iterations
= likely_max_loop_iterations_int (loop
);
318 if (iterations
>= 0 && iterations
<= 1)
320 if (dump_enabled_p ())
321 dump_printf_loc (MSG_NOTE
, loc
,
322 "Not unswitching, loop is not expected"
327 bb_predicates
= new vec
<vec
<unswitch_predicate
*>> ();
328 bb_predicates
->safe_push (vec
<unswitch_predicate
*> ());
329 unswitch_predicate::predicates
= new vec
<unswitch_predicate
*> ();
331 /* Unswitch innermost loop. */
332 unsigned int loop_size
= init_loop_unswitch_info (loop
);
333 unsigned int budget
= loop_size
+ param_max_unswitch_insns
;
335 predicate_vector predicate_path
;
336 predicate_path
.create (8);
339 |= tree_unswitch_single_loop (loop
, loc
, predicate_path
,
341 ignored_edge_flag
, handled
);
342 predicate_path
.release ();
344 for (auto predlist
: bb_predicates
)
346 bb_predicates
->release ();
347 delete bb_predicates
;
348 bb_predicates
= NULL
;
350 for (auto pred
: unswitch_predicate::predicates
)
352 unswitch_predicate::predicates
->release ();
353 delete unswitch_predicate::predicates
;
354 unswitch_predicate::predicates
= NULL
;
357 changed_hoist
|= tree_unswitch_outer_loop (loop
);
360 disable_ranger (fun
);
361 clear_aux_for_blocks ();
363 if (changed_unswitch
)
364 clean_up_after_unswitching (ignored_edge_flag
);
366 if (changed_unswitch
|| changed_hoist
)
367 return TODO_cleanup_cfg
;
372 /* Return TRUE if an SSA_NAME maybe undefined and is therefore
373 unsuitable for unswitching. STMT is the statement we are
374 considering for unswitching and LOOP is the loop it appears in. */
377 is_maybe_undefined (const tree name
, gimple
*stmt
, class loop
*loop
)
379 /* The loop header is the only block we can trivially determine that
380 will always be executed. If the comparison is in the loop
381 header, we know it's OK to unswitch on it. */
382 if (gimple_bb (stmt
) == loop
->header
)
385 auto_bitmap visited_ssa
;
386 auto_vec
<tree
> worklist
;
387 worklist
.safe_push (name
);
388 bitmap_set_bit (visited_ssa
, SSA_NAME_VERSION (name
));
389 while (!worklist
.is_empty ())
391 tree t
= worklist
.pop ();
393 /* If it's obviously undefined, avoid further computations. */
394 if (ssa_undefined_value_p (t
, true))
397 if (ssa_defined_default_def_p (t
))
400 gimple
*def
= SSA_NAME_DEF_STMT (t
);
402 /* Check that all the PHI args are fully defined. */
403 if (gphi
*phi
= dyn_cast
<gphi
*> (def
))
405 for (unsigned i
= 0; i
< gimple_phi_num_args (phi
); ++i
)
407 tree t
= gimple_phi_arg_def (phi
, i
);
408 /* If an SSA has already been seen, it may be a loop,
409 but we can continue and ignore this use. Otherwise,
410 add the SSA_NAME to the queue and visit it later. */
411 if (TREE_CODE (t
) == SSA_NAME
412 && bitmap_set_bit (visited_ssa
, SSA_NAME_VERSION (t
)))
413 worklist
.safe_push (t
);
418 /* Uses in stmts always executed when the region header executes
420 if (dominated_by_p (CDI_DOMINATORS
, loop
->header
, gimple_bb (def
)))
423 /* Handle calls and memory loads conservatively. */
424 if (!is_gimple_assign (def
)
425 || (gimple_assign_single_p (def
)
426 && gimple_vuse (def
)))
429 /* Check that any SSA names used to define NAME are also fully
433 FOR_EACH_SSA_USE_OPERAND (use_p
, def
, iter
, SSA_OP_USE
)
435 tree t
= USE_FROM_PTR (use_p
);
436 /* If an SSA has already been seen, it may be a loop,
437 but we can continue and ignore this use. Otherwise,
438 add the SSA_NAME to the queue and visit it later. */
439 if (bitmap_set_bit (visited_ssa
, SSA_NAME_VERSION (t
)))
440 worklist
.safe_push (t
);
446 /* Checks whether we can unswitch LOOP on condition at end of BB -- one of its
447 basic blocks (for what it means see comments below).
448 All candidates all filled to the provided vector CANDIDATES. */
451 find_unswitching_predicates_for_bb (basic_block bb
, class loop
*loop
,
452 vec
<unswitch_predicate
*> &candidates
)
459 /* BB must end in a simple conditional jump. */
460 last
= last_stmt (bb
);
464 if (gcond
*stmt
= safe_dyn_cast
<gcond
*> (last
))
466 /* To keep the things simple, we do not directly remove the conditions,
467 but just replace tests with 0 != 0 resp. 1 != 0. Prevent the infinite
468 loop where we would unswitch again on such a condition. */
469 if (gimple_cond_true_p (stmt
) || gimple_cond_false_p (stmt
))
472 /* At least the LHS needs to be symbolic. */
473 if (TREE_CODE (gimple_cond_lhs (stmt
)) != SSA_NAME
)
476 /* Condition must be invariant. */
477 FOR_EACH_SSA_TREE_OPERAND (use
, stmt
, iter
, SSA_OP_USE
)
479 def
= SSA_NAME_DEF_STMT (use
);
480 def_bb
= gimple_bb (def
);
482 && flow_bb_inside_loop_p (loop
, def_bb
))
484 /* Unswitching on undefined values would introduce undefined
485 behavior that the original program might never exercise. */
486 if (is_maybe_undefined (use
, stmt
, loop
))
490 unswitch_predicate
*predicate
= new unswitch_predicate (stmt
);
491 candidates
.safe_push (predicate
);
493 else if (gswitch
*stmt
= safe_dyn_cast
<gswitch
*> (last
))
495 unsigned nlabels
= gimple_switch_num_labels (stmt
);
496 tree idx
= gimple_switch_index (stmt
);
497 tree idx_type
= TREE_TYPE (idx
);
498 if (!gimple_range_ssa_p (idx
) || nlabels
< 1)
500 /* Index must be invariant. */
501 def
= SSA_NAME_DEF_STMT (idx
);
502 def_bb
= gimple_bb (def
);
504 && flow_bb_inside_loop_p (loop
, def_bb
))
506 /* Unswitching on undefined values would introduce undefined
507 behavior that the original program might never exercise. */
508 if (is_maybe_undefined (idx
, stmt
, loop
))
511 /* Build compound expression for all outgoing edges of the switch. */
512 auto_vec
<tree
, 16> preds
;
513 auto_vec
<int_range_max
> edge_range
;
514 preds
.safe_grow_cleared (EDGE_COUNT (gimple_bb (stmt
)->succs
), true);
515 edge_range
.safe_grow_cleared (EDGE_COUNT (gimple_bb (stmt
)->succs
), true);
518 unsigned edge_index
= 0;
519 FOR_EACH_EDGE (e
, ei
, gimple_bb (stmt
)->succs
)
520 e
->aux
= (void *)(uintptr_t)edge_index
++;
521 for (unsigned i
= 1; i
< gimple_switch_num_labels (stmt
); ++i
)
523 tree lab
= gimple_switch_label (stmt
, i
);
525 int_range
<2> lab_range
;
526 tree low
= fold_convert (idx_type
, CASE_LOW (lab
));
527 if (CASE_HIGH (lab
) != NULL_TREE
)
529 tree high
= fold_convert (idx_type
, CASE_HIGH (lab
));
530 tree cmp1
= fold_build2 (GE_EXPR
, boolean_type_node
, idx
, low
);
531 tree cmp2
= fold_build2 (LE_EXPR
, boolean_type_node
, idx
, high
);
532 cmp
= fold_build2 (BIT_AND_EXPR
, boolean_type_node
, cmp1
, cmp2
);
533 lab_range
.set (low
, high
);
537 cmp
= fold_build2 (EQ_EXPR
, boolean_type_node
, idx
, low
);
538 lab_range
.set (low
, low
);
541 /* Combine the expression with the existing one. */
542 basic_block dest
= label_to_block (cfun
, CASE_LABEL (lab
));
543 e
= find_edge (gimple_bb (stmt
), dest
);
544 tree
&expr
= preds
[(uintptr_t)e
->aux
];
545 if (expr
== NULL_TREE
)
548 expr
= fold_build2 (BIT_IOR_EXPR
, boolean_type_node
, expr
, cmp
);
549 edge_range
[(uintptr_t)e
->aux
].union_ (lab_range
);
552 /* Now register the predicates. */
553 for (edge_index
= 0; edge_index
< preds
.length (); ++edge_index
)
555 edge e
= EDGE_SUCC (gimple_bb (stmt
), edge_index
);
557 if (preds
[edge_index
] != NULL_TREE
)
559 unswitch_predicate
*predicate
560 = new unswitch_predicate (preds
[edge_index
], idx
,
562 edge_range
[edge_index
]);
563 candidates
.safe_push (predicate
);
569 /* Merge ranges for the last item of PREDICATE_PATH with a predicate
570 that shared the same LHS. */
573 merge_last (predicate_vector
&predicate_path
)
575 unswitch_predicate
*last_predicate
= predicate_path
.last ().first
;
577 for (int i
= predicate_path
.length () - 2; i
>= 0; i
--)
579 unswitch_predicate
*predicate
= predicate_path
[i
].first
;
580 bool true_edge
= predicate_path
[i
].second
;
582 if (operand_equal_p (predicate
->lhs
, last_predicate
->lhs
, 0))
584 irange
&other
= (true_edge
? predicate
->merged_true_range
585 : predicate
->merged_false_range
);
586 last_predicate
->merged_true_range
.intersect (other
);
587 last_predicate
->merged_false_range
.intersect (other
);
593 /* Add PREDICATE to PREDICATE_PATH on TRUE_EDGE. */
596 add_predicate_to_path (predicate_vector
&predicate_path
,
597 unswitch_predicate
*predicate
, bool true_edge
)
599 predicate
->copy_merged_ranges ();
600 predicate_path
.safe_push (std::make_pair (predicate
, true_edge
));
601 merge_last (predicate_path
);
605 find_range_for_lhs (predicate_vector
&predicate_path
, tree lhs
,
606 int_range_max
&range
)
608 for (int i
= predicate_path
.length () - 1; i
>= 0; i
--)
610 unswitch_predicate
*predicate
= predicate_path
[i
].first
;
611 bool true_edge
= predicate_path
[i
].second
;
613 if (operand_equal_p (predicate
->lhs
, lhs
, 0))
615 range
= (true_edge
? predicate
->merged_true_range
616 : predicate
->merged_false_range
);
617 return !range
.undefined_p ();
624 /* Simplifies STMT using the predicate we unswitched on which is the last
625 in PREDICATE_PATH. For switch statements add newly unreachable edges
626 to IGNORED_EDGES (but do not set IGNORED_EDGE_FLAG on them). */
629 evaluate_control_stmt_using_entry_checks (gimple
*stmt
,
630 predicate_vector
&predicate_path
,
631 int ignored_edge_flag
,
632 hash_set
<edge
> *ignored_edges
)
634 unswitch_predicate
*last_predicate
= predicate_path
.last ().first
;
635 bool true_edge
= predicate_path
.last ().second
;
637 if (gcond
*cond
= dyn_cast
<gcond
*> (stmt
))
639 tree lhs
= gimple_cond_lhs (cond
);
640 if (!operand_equal_p (lhs
, last_predicate
->lhs
))
642 /* Try a symbolic match which works for floating point and fully
643 symbolic conditions. */
644 if (gimple_cond_code (cond
) == TREE_CODE (last_predicate
->condition
)
645 && operand_equal_p (gimple_cond_rhs (cond
),
646 TREE_OPERAND (last_predicate
->condition
, 1)))
647 return true_edge
? boolean_true_node
: boolean_false_node
;
648 /* Else try ranger if it supports LHS. */
649 else if (irange::supports_p (TREE_TYPE (lhs
)))
652 int_range_max path_range
;
654 if (find_range_for_lhs (predicate_path
, lhs
, path_range
)
655 && fold_range (r
, cond
, path_range
)
657 return r
.zero_p () ? boolean_false_node
: boolean_true_node
;
660 else if (gswitch
*swtch
= dyn_cast
<gswitch
*> (stmt
))
662 unsigned nlabels
= gimple_switch_num_labels (swtch
);
664 tree idx
= gimple_switch_index (swtch
);
666 /* Already folded switch. */
667 if (TREE_CONSTANT (idx
))
670 int_range_max path_range
;
671 if (!find_range_for_lhs (predicate_path
, idx
, path_range
))
674 tree result
= NULL_TREE
;
675 edge single_edge
= NULL
;
676 for (unsigned i
= 0; i
< nlabels
; ++i
)
678 tree lab
= gimple_switch_label (swtch
, i
);
679 basic_block dest
= label_to_block (cfun
, CASE_LABEL (lab
));
680 edge e
= find_edge (gimple_bb (stmt
), dest
);
681 if (e
->flags
& ignored_edge_flag
)
685 if (!ranger
->gori ().outgoing_edge_range_p (r
, e
, idx
,
686 *get_global_range_query ()))
688 r
.intersect (path_range
);
689 if (r
.undefined_p ())
690 ignored_edges
->add (e
);
696 result
= CASE_LOW (lab
);
698 else if (single_edge
!= e
)
703 /* Only one edge from the switch is alive. */
704 if (single_edge
&& result
)
711 /* Simplify LOOP based on PREDICATE_PATH where dead edges are properly
715 simplify_loop_version (class loop
*loop
, predicate_vector
&predicate_path
,
716 int ignored_edge_flag
, bitmap handled
)
718 bool changed
= false;
719 basic_block
*bbs
= get_loop_body (loop
);
721 hash_set
<edge
> ignored_edges
;
722 for (unsigned i
= 0; i
!= loop
->num_nodes
; i
++)
724 vec
<unswitch_predicate
*> &predicates
= get_predicates_for_bb (bbs
[i
]);
725 if (predicates
.is_empty ())
728 gimple
*stmt
= last_stmt (bbs
[i
]);
729 tree folded
= evaluate_control_stmt_using_entry_checks (stmt
,
734 if (gcond
*cond
= dyn_cast
<gcond
*> (stmt
))
739 if (integer_nonzerop (folded
))
740 gimple_cond_set_condition_from_tree (cond
, boolean_true_node
);
742 gimple_cond_set_condition_from_tree (cond
, boolean_false_node
);
744 gcc_assert (predicates
.length () == 1);
745 bitmap_set_bit (handled
, predicates
[0]->num
);
751 else if (gswitch
*swtch
= dyn_cast
<gswitch
*> (stmt
))
755 FOR_EACH_EDGE (e
, ei
, bbs
[i
]->succs
)
756 if (ignored_edges
.contains (e
))
757 e
->flags
|= ignored_edge_flag
;
759 for (unsigned j
= 0; j
< predicates
.length (); j
++)
761 edge e
= EDGE_SUCC (bbs
[i
], predicates
[j
]->edge_index
);
762 if (ignored_edges
.contains (e
))
763 bitmap_set_bit (handled
, predicates
[j
]->num
);
768 gimple_switch_set_index (swtch
, folded
);
779 /* Evaluate reachable blocks in LOOP and call VISIT on them, aborting the
780 DFS walk if VISIT returns true. When PREDICATE_PATH is specified then
781 take into account that when computing reachability, otherwise just
782 look at the simplified state and IGNORED_EDGE_FLAG. */
784 template <typename VisitOp
>
786 evaluate_bbs (class loop
*loop
, predicate_vector
*predicate_path
,
787 int ignored_edge_flag
, VisitOp visit
)
789 auto_bb_flag
reachable_flag (cfun
);
790 auto_vec
<basic_block
, 10> worklist (loop
->num_nodes
);
791 auto_vec
<basic_block
, 10> reachable (loop
->num_nodes
);
792 hash_set
<edge
> ignored_edges
;
794 loop
->header
->flags
|= reachable_flag
;
795 worklist
.quick_push (loop
->header
);
796 reachable
.safe_push (loop
->header
);
798 while (!worklist
.is_empty ())
802 int flags
= ignored_edge_flag
;
803 basic_block bb
= worklist
.pop ();
808 gimple
*last
= last_stmt (bb
);
809 if (gcond
*cond
= safe_dyn_cast
<gcond
*> (last
))
811 if (gimple_cond_true_p (cond
))
812 flags
= EDGE_FALSE_VALUE
;
813 else if (gimple_cond_false_p (cond
))
814 flags
= EDGE_TRUE_VALUE
;
815 else if (predicate_path
)
818 if (!get_predicates_for_bb (bb
).is_empty ()
819 && (res
= evaluate_control_stmt_using_entry_checks
820 (cond
, *predicate_path
, ignored_edge_flag
,
822 flags
= (integer_nonzerop (res
)
823 ? EDGE_FALSE_VALUE
: EDGE_TRUE_VALUE
);
826 else if (gswitch
*swtch
= safe_dyn_cast
<gswitch
*> (last
))
828 && !get_predicates_for_bb (bb
).is_empty ())
829 evaluate_control_stmt_using_entry_checks (swtch
, *predicate_path
,
833 /* Note that for the moment we do not account reachable conditions
834 which are simplified to take a known edge as zero size nor
835 are we accounting for the required addition of the versioning
836 condition. Those should cancel out conservatively. */
838 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
840 basic_block dest
= e
->dest
;
842 if (dest
->loop_father
== loop
843 && !(dest
->flags
& reachable_flag
)
844 && !(e
->flags
& flags
)
845 && !ignored_edges
.contains (e
))
847 dest
->flags
|= reachable_flag
;
848 worklist
.safe_push (dest
);
849 reachable
.safe_push (dest
);
854 /* Clear the flag from basic blocks. */
855 while (!reachable
.is_empty ())
856 reachable
.pop ()->flags
&= ~reachable_flag
;
859 /* Evaluate how many instruction will we have if we unswitch LOOP (with BBS)
860 based on PREDICATE predicate (using PREDICATE_PATH). Store the
861 result in TRUE_SIZE and FALSE_SIZE. */
864 evaluate_loop_insns_for_predicate (class loop
*loop
,
865 predicate_vector
&predicate_path
,
866 unswitch_predicate
*predicate
,
867 int ignored_edge_flag
,
868 unsigned *true_size
, unsigned *false_size
)
871 auto sum_size
= [&](basic_block bb
) -> bool
872 { size
+= (uintptr_t)bb
->aux
; return false; };
874 add_predicate_to_path (predicate_path
, predicate
, true);
875 evaluate_bbs (loop
, &predicate_path
, ignored_edge_flag
, sum_size
);
876 predicate_path
.pop ();
877 unsigned true_loop_cost
= size
;
880 add_predicate_to_path (predicate_path
, predicate
, false);
881 evaluate_bbs (loop
, &predicate_path
, ignored_edge_flag
, sum_size
);
882 predicate_path
.pop ();
883 unsigned false_loop_cost
= size
;
885 *true_size
= true_loop_cost
;
886 *false_size
= false_loop_cost
;
889 /* Unswitch single LOOP. PREDICATE_PATH contains so far used predicates
890 for unswitching. BUDGET is number of instruction for which we can increase
891 the loop and is updated when unswitching occurs. */
894 tree_unswitch_single_loop (class loop
*loop
, dump_user_location_t loc
,
895 predicate_vector
&predicate_path
,
896 unsigned loop_size
, unsigned &budget
,
897 int ignored_edge_flag
, bitmap handled
)
900 bool changed
= false;
901 unswitch_predicate
*predicate
= NULL
;
902 basic_block predicate_bb
= NULL
;
903 unsigned true_size
= 0, false_size
= 0;
905 auto check_predicates
= [&](basic_block bb
) -> bool
907 for (auto pred
: get_predicates_for_bb (bb
))
909 if (bitmap_bit_p (handled
, pred
->num
))
912 evaluate_loop_insns_for_predicate (loop
, predicate_path
,
913 pred
, ignored_edge_flag
,
914 &true_size
, &false_size
);
916 /* We'll get LOOP replaced with a simplified version according
917 to PRED estimated to TRUE_SIZE and a copy simplified
918 according to the inverted PRED estimated to FALSE_SIZE. */
919 if (true_size
+ false_size
< budget
+ loop_size
)
924 /* There are cases where true_size and false_size add up to
925 less than the original loop_size. We do not want to
926 grow the remaining budget because of that. */
927 if (true_size
+ false_size
> loop_size
)
928 budget
-= (true_size
+ false_size
- loop_size
);
930 /* FIXME: right now we select first candidate, but we can
931 choose the cheapest or hottest one. */
934 else if (dump_enabled_p ())
935 dump_printf_loc (MSG_NOTE
, loc
,
936 "not unswitching condition, cost too big "
937 "(%u insns copied to %u and %u)\n", loop_size
,
938 true_size
, false_size
);
942 /* Check predicates of reachable blocks. */
943 evaluate_bbs (loop
, NULL
, ignored_edge_flag
, check_predicates
);
945 if (predicate
!= NULL
)
947 if (!dbg_cnt (loop_unswitch
))
950 if (dump_enabled_p ())
952 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, loc
,
953 "unswitching loop %d on %qs with condition: %T\n",
954 loop
->num
, predicate
->switch_p
? "switch" : "if",
955 predicate
->condition
);
956 dump_printf_loc (MSG_NOTE
, loc
,
957 "optimized sizes estimated to %u (true) "
958 "and %u (false) from original size %u\n",
959 true_size
, false_size
, loop_size
);
962 bitmap_set_bit (handled
, predicate
->num
);
963 initialize_original_copy_tables ();
964 /* Unswitch the loop on this condition. */
965 nloop
= tree_unswitch_loop (loop
, EDGE_SUCC (predicate_bb
,
966 predicate
->edge_index
),
967 predicate
->condition
);
970 free_original_copy_tables ();
975 basic_block
*bbs2
= get_loop_body (nloop
);
976 for (unsigned i
= 0; i
< nloop
->num_nodes
; i
++)
977 bbs2
[i
]->aux
= get_bb_original (bbs2
[i
])->aux
;
980 free_original_copy_tables ();
982 /* Update the SSA form after unswitching. */
983 update_ssa (TODO_update_ssa_no_phi
);
985 /* Invoke itself on modified loops. */
986 bitmap handled_copy
= BITMAP_ALLOC (NULL
);
987 bitmap_copy (handled_copy
, handled
);
988 add_predicate_to_path (predicate_path
, predicate
, false);
989 changed
|= simplify_loop_version (nloop
, predicate_path
,
990 ignored_edge_flag
, handled_copy
);
991 tree_unswitch_single_loop (nloop
, loc
, predicate_path
,
993 ignored_edge_flag
, handled_copy
);
994 predicate_path
.pop ();
995 BITMAP_FREE (handled_copy
);
997 /* FIXME: After unwinding above we have to reset all ->handled
998 flags as otherwise we fail to realize unswitching opportunities
999 in the below recursion. See gcc.dg/loop-unswitch-16.c */
1000 add_predicate_to_path (predicate_path
, predicate
, true);
1001 changed
|= simplify_loop_version (loop
, predicate_path
,
1002 ignored_edge_flag
, handled
);
1003 tree_unswitch_single_loop (loop
, loc
, predicate_path
,
1005 ignored_edge_flag
, handled
);
1006 predicate_path
.pop ();
1014 /* Unswitch a LOOP w.r. to given EDGE_TRUE. We only support unswitching of
1015 innermost loops. COND is the condition determining which loop is entered;
1016 the new loop is entered if COND is true. Returns NULL if impossible, new
1020 tree_unswitch_loop (class loop
*loop
, edge edge_true
, tree cond
)
1022 /* Some sanity checking. */
1023 gcc_assert (flow_bb_inside_loop_p (loop
, edge_true
->src
));
1024 gcc_assert (EDGE_COUNT (edge_true
->src
->succs
) >= 2);
1025 gcc_assert (loop
->inner
== NULL
);
1027 profile_probability prob_true
= edge_true
->probability
;
1028 return loop_version (loop
, unshare_expr (cond
),
1030 prob_true
.invert (),
1031 prob_true
, prob_true
.invert (),
1035 /* Unswitch outer loops by hoisting invariant guard on
1036 inner loop without code duplication. */
1038 tree_unswitch_outer_loop (class loop
*loop
)
1041 HOST_WIDE_INT iterations
;
1043 gcc_assert (loop
->inner
);
1044 if (loop
->inner
->next
)
1046 /* Accept loops with single exit only which is not from inner loop. */
1047 exit
= single_exit (loop
);
1048 if (!exit
|| exit
->src
->loop_father
!= loop
)
1050 /* Check that phi argument of exit edge is not defined inside loop. */
1051 if (!check_exit_phi (loop
))
1053 /* If the loop is not expected to iterate, there is no need
1055 iterations
= estimated_loop_iterations_int (loop
);
1057 iterations
= likely_max_loop_iterations_int (loop
);
1058 if (iterations
>= 0 && iterations
<= 1)
1060 if (dump_enabled_p ())
1061 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, find_loop_location (loop
),
1062 "Not unswitching, loop is not expected"
1067 bool changed
= false;
1068 auto_vec
<gimple
*> dbg_to_reset
;
1069 while ((guard
= find_loop_guard (loop
, dbg_to_reset
)))
1071 hoist_guard (loop
, guard
);
1072 for (gimple
*debug_stmt
: dbg_to_reset
)
1074 gimple_debug_bind_reset_value (debug_stmt
);
1075 update_stmt (debug_stmt
);
1077 dbg_to_reset
.truncate (0);
1083 /* Checks if the body of the LOOP is within an invariant guard. If this
1084 is the case, returns the edge that jumps over the real body of the loop,
1085 otherwise returns NULL. */
1088 find_loop_guard (class loop
*loop
, vec
<gimple
*> &dbg_to_reset
)
1090 basic_block header
= loop
->header
;
1091 edge guard_edge
, te
, fe
;
1092 basic_block
*body
= NULL
;
1097 /* We check for the following situation:
1106 nvar = phi(orig, bvar) ... for all variables changed in body;
1116 1) cond1 is loop invariant
1117 2) If cond1 is false, then the loop is essentially empty; i.e.,
1118 a) nothing in something1, something2 and something3 has side
1120 b) anything defined in something1, something2 and something3
1121 is not used outside of the loop. */
1126 basic_block next
= NULL
;
1127 if (single_succ_p (header
))
1128 next
= single_succ (header
);
1131 cond
= safe_dyn_cast
<gcond
*> (last_stmt (header
));
1134 extract_true_false_edges_from_block (header
, &te
, &fe
);
1135 /* Make sure to skip earlier hoisted guards that are left
1136 in place as if (true). */
1137 if (gimple_cond_true_p (cond
))
1139 else if (gimple_cond_false_p (cond
))
1144 /* Never traverse a backedge. */
1145 if (header
->loop_father
->header
== next
)
1150 if (!flow_bb_inside_loop_p (loop
, te
->dest
)
1151 || !flow_bb_inside_loop_p (loop
, fe
->dest
))
1154 if (just_once_each_iteration_p (loop
, te
->dest
)
1155 || (single_succ_p (te
->dest
)
1156 && just_once_each_iteration_p (loop
, single_succ (te
->dest
))))
1158 if (just_once_each_iteration_p (loop
, fe
->dest
))
1162 else if (just_once_each_iteration_p (loop
, fe
->dest
)
1163 || (single_succ_p (fe
->dest
)
1164 && just_once_each_iteration_p (loop
, single_succ (fe
->dest
))))
1169 dump_user_location_t loc
= find_loop_location (loop
);
1171 /* Guard edge must skip inner loop. */
1172 if (!dominated_by_p (CDI_DOMINATORS
, loop
->inner
->header
,
1173 guard_edge
== fe
? te
->dest
: fe
->dest
))
1175 if (dump_enabled_p ())
1176 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, loc
,
1177 "Guard edge %d --> %d is not around the loop!\n",
1178 guard_edge
->src
->index
, guard_edge
->dest
->index
);
1181 if (guard_edge
->dest
== loop
->latch
)
1183 if (dump_enabled_p ())
1184 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, loc
,
1185 "Guard edge destination is loop latch.\n");
1189 if (dump_enabled_p ())
1190 dump_printf_loc (MSG_NOTE
, loc
,
1191 "Considering guard %d -> %d in loop %d\n",
1192 guard_edge
->src
->index
, guard_edge
->dest
->index
,
1194 /* Check if condition operands do not have definitions inside loop since
1195 any bb copying is not performed. */
1196 FOR_EACH_SSA_TREE_OPERAND (use
, cond
, iter
, SSA_OP_USE
)
1198 gimple
*def
= SSA_NAME_DEF_STMT (use
);
1199 basic_block def_bb
= gimple_bb (def
);
1201 && flow_bb_inside_loop_p (loop
, def_bb
))
1203 if (dump_enabled_p ())
1204 dump_printf_loc (MSG_NOTE
, loc
, "guard operands have definitions"
1210 body
= get_loop_body (loop
);
1211 for (i
= 0; i
< loop
->num_nodes
; i
++)
1213 basic_block bb
= body
[i
];
1214 if (bb
->loop_father
!= loop
)
1216 if (bb
->flags
& BB_IRREDUCIBLE_LOOP
)
1218 if (dump_enabled_p ())
1219 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, loc
,
1220 "Block %d is marked as irreducible in loop\n",
1225 if (!empty_bb_without_guard_p (loop
, bb
, dbg_to_reset
))
1227 if (dump_enabled_p ())
1228 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, loc
,
1229 "Block %d has side effects\n", bb
->index
);
1235 if (dump_enabled_p ())
1236 dump_printf_loc (MSG_NOTE
, loc
,
1237 "suitable to hoist\n");
1245 1) no statement in BB has side effects
1246 2) assuming that edge GUARD is always taken, all definitions in BB
1247 are noy used outside of the loop.
1248 KNOWN_INVARIANTS is a set of ssa names we know to be invariant, and
1249 PROCESSED is a set of ssa names for that we already tested whether they
1250 are invariant or not. Uses in debug stmts outside of the loop are
1251 pushed to DBG_TO_RESET. */
1254 empty_bb_without_guard_p (class loop
*loop
, basic_block bb
,
1255 vec
<gimple
*> &dbg_to_reset
)
1257 basic_block exit_bb
= single_exit (loop
)->src
;
1258 bool may_be_used_outside
= (bb
== exit_bb
1259 || !dominated_by_p (CDI_DOMINATORS
, bb
, exit_bb
));
1261 ssa_op_iter op_iter
;
1263 /* Phi nodes do not have side effects, but their results might be used
1264 outside of the loop. */
1265 if (may_be_used_outside
)
1267 for (gphi_iterator gsi
= gsi_start_phis (bb
);
1268 !gsi_end_p (gsi
); gsi_next (&gsi
))
1270 gphi
*phi
= gsi
.phi ();
1271 name
= PHI_RESULT (phi
);
1272 if (virtual_operand_p (name
))
1275 if (used_outside_loop_p (loop
, name
, dbg_to_reset
))
1280 for (gimple_stmt_iterator gsi
= gsi_start_bb (bb
);
1281 !gsi_end_p (gsi
); gsi_next (&gsi
))
1283 gimple
*stmt
= gsi_stmt (gsi
);
1284 if (is_gimple_debug (stmt
))
1287 if (gimple_has_side_effects (stmt
))
1290 if (gimple_vdef(stmt
))
1293 FOR_EACH_SSA_TREE_OPERAND (name
, stmt
, op_iter
, SSA_OP_DEF
)
1295 if (may_be_used_outside
1296 && used_outside_loop_p (loop
, name
, dbg_to_reset
))
1303 /* Return true if NAME is used outside of LOOP. Pushes debug stmts that
1304 have such uses to DBG_TO_RESET but do not consider such uses. */
1307 used_outside_loop_p (class loop
*loop
, tree name
, vec
<gimple
*> &dbg_to_reset
)
1309 imm_use_iterator it
;
1312 FOR_EACH_IMM_USE_FAST (use
, it
, name
)
1314 gimple
*stmt
= USE_STMT (use
);
1315 if (!flow_bb_inside_loop_p (loop
, gimple_bb (stmt
)))
1317 if (!is_gimple_debug (stmt
))
1319 dbg_to_reset
.safe_push (stmt
);
1326 /* Return argument for loop preheader edge in header virtual phi if any. */
1329 get_vop_from_header (class loop
*loop
)
1331 for (gphi_iterator gsi
= gsi_start_phis (loop
->header
);
1332 !gsi_end_p (gsi
); gsi_next (&gsi
))
1334 gphi
*phi
= gsi
.phi ();
1335 if (!virtual_operand_p (gimple_phi_result (phi
)))
1337 return PHI_ARG_DEF_FROM_EDGE (phi
, loop_preheader_edge (loop
));
1342 /* Move the check of GUARD outside of LOOP. */
1345 hoist_guard (class loop
*loop
, edge guard
)
1347 edge exit
= single_exit (loop
);
1348 edge preh
= loop_preheader_edge (loop
);
1349 basic_block pre_header
= preh
->src
;
1351 edge te
, fe
, e
, new_edge
;
1353 basic_block guard_bb
= guard
->src
;
1355 gimple_stmt_iterator gsi
;
1357 bool fix_dom_of_exit
;
1358 gcond
*cond_stmt
, *new_cond_stmt
;
1360 bb
= get_immediate_dominator (CDI_DOMINATORS
, exit
->dest
);
1361 fix_dom_of_exit
= flow_bb_inside_loop_p (loop
, bb
);
1362 gsi
= gsi_last_bb (guard_bb
);
1363 stmt
= gsi_stmt (gsi
);
1364 gcc_assert (gimple_code (stmt
) == GIMPLE_COND
);
1365 cond_stmt
= as_a
<gcond
*> (stmt
);
1366 extract_true_false_edges_from_block (guard_bb
, &te
, &fe
);
1367 /* Insert guard to PRE_HEADER. */
1368 if (!empty_block_p (pre_header
))
1369 gsi
= gsi_last_bb (pre_header
);
1371 gsi
= gsi_start_bb (pre_header
);
1372 /* Create copy of COND_STMT. */
1373 new_cond_stmt
= gimple_build_cond (gimple_cond_code (cond_stmt
),
1374 gimple_cond_lhs (cond_stmt
),
1375 gimple_cond_rhs (cond_stmt
),
1376 NULL_TREE
, NULL_TREE
);
1377 gsi_insert_after (&gsi
, new_cond_stmt
, GSI_NEW_STMT
);
1378 /* Convert COND_STMT to true/false conditional. */
1380 gimple_cond_make_false (cond_stmt
);
1382 gimple_cond_make_true (cond_stmt
);
1383 update_stmt (cond_stmt
);
1384 /* Create new loop pre-header. */
1385 e
= split_block (pre_header
, last_stmt (pre_header
));
1387 dump_user_location_t loc
= find_loop_location (loop
);
1389 if (dump_enabled_p ())
1392 guard
->probability
.dump (buffer
);
1394 dump_printf_loc (MSG_NOTE
, loc
,
1395 "Moving guard %i->%i (prob %s) to bb %i, "
1396 "new preheader is %i\n",
1397 guard
->src
->index
, guard
->dest
->index
,
1398 buffer
, e
->src
->index
, e
->dest
->index
);
1401 gcc_assert (loop_preheader_edge (loop
)->src
== e
->dest
);
1405 e
->flags
= EDGE_TRUE_VALUE
;
1406 flags
|= EDGE_FALSE_VALUE
;
1411 e
->flags
= EDGE_FALSE_VALUE
;
1412 flags
|= EDGE_TRUE_VALUE
;
1415 new_edge
= make_edge (pre_header
, exit
->dest
, flags
);
1417 /* Determine the probability that we skip the loop. Assume that loop has
1418 same average number of iterations regardless outcome of guard. */
1419 new_edge
->probability
= guard
->probability
;
1420 profile_count skip_count
= guard
->src
->count
.nonzero_p ()
1421 ? guard
->count ().apply_scale (pre_header
->count
,
1423 : guard
->count ().apply_probability (new_edge
->probability
);
1425 if (skip_count
> e
->count ())
1427 fprintf (dump_file
, " Capping count; expect profile inconsistency\n");
1428 skip_count
= e
->count ();
1430 if (dump_enabled_p ())
1433 new_edge
->probability
.dump (buffer
);
1435 dump_printf_loc (MSG_NOTE
, loc
,
1436 "Estimated probability of skipping loop is %s\n",
1440 /* Update profile after the transform:
1442 First decrease count of path from newly hoisted loop guard
1443 to loop header... */
1444 e
->probability
= new_edge
->probability
.invert ();
1445 e
->dest
->count
= e
->count ();
1447 /* ... now update profile to represent that original guard will be optimized
1449 guard
->probability
= profile_probability::never ();
1450 not_guard
->probability
= profile_probability::always ();
1452 /* ... finally scale everything in the loop except for guarded basic blocks
1453 where profile does not change. */
1454 basic_block
*body
= get_loop_body (loop
);
1456 for (unsigned int i
= 0; i
< loop
->num_nodes
; i
++)
1458 basic_block bb
= body
[i
];
1459 if (!dominated_by_p (CDI_DOMINATORS
, bb
, not_guard
->dest
))
1461 if (dump_enabled_p ())
1462 dump_printf_loc (MSG_NOTE
, loc
,
1463 "Scaling nonguarded BBs in loop: %i\n",
1465 if (e
->probability
.initialized_p ())
1466 scale_bbs_frequencies (&bb
, 1, e
->probability
);
1470 if (fix_dom_of_exit
)
1471 set_immediate_dominator (CDI_DOMINATORS
, exit
->dest
, pre_header
);
1472 /* Add NEW_ADGE argument for all phi in post-header block. */
1474 for (gphi_iterator gsi
= gsi_start_phis (bb
);
1475 !gsi_end_p (gsi
); gsi_next (&gsi
))
1477 gphi
*phi
= gsi
.phi ();
1479 if (virtual_operand_p (gimple_phi_result (phi
)))
1481 arg
= get_vop_from_header (loop
);
1482 if (arg
== NULL_TREE
)
1483 /* Use exit edge argument. */
1484 arg
= PHI_ARG_DEF_FROM_EDGE (phi
, exit
);
1485 add_phi_arg (phi
, arg
, new_edge
, UNKNOWN_LOCATION
);
1489 /* Use exit edge argument. */
1490 arg
= PHI_ARG_DEF_FROM_EDGE (phi
, exit
);
1491 add_phi_arg (phi
, arg
, new_edge
, UNKNOWN_LOCATION
);
1495 if (dump_enabled_p ())
1496 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, loc
,
1502 /* Return true if phi argument for exit edge can be used
1503 for edge around loop. */
1506 check_exit_phi (class loop
*loop
)
1508 edge exit
= single_exit (loop
);
1509 basic_block pre_header
= loop_preheader_edge (loop
)->src
;
1511 for (gphi_iterator gsi
= gsi_start_phis (exit
->dest
);
1512 !gsi_end_p (gsi
); gsi_next (&gsi
))
1514 gphi
*phi
= gsi
.phi ();
1518 if (virtual_operand_p (gimple_phi_result (phi
)))
1520 arg
= PHI_ARG_DEF_FROM_EDGE (phi
, exit
);
1521 if (TREE_CODE (arg
) != SSA_NAME
)
1523 def
= SSA_NAME_DEF_STMT (arg
);
1526 def_bb
= gimple_bb (def
);
1529 if (!dominated_by_p (CDI_DOMINATORS
, pre_header
, def_bb
))
1530 /* Definition inside loop! */
1532 /* Check loop closed phi invariant. */
1533 if (!flow_bb_inside_loop_p (def_bb
->loop_father
, pre_header
))
1539 /* Remove all dead cases from switches that are unswitched. */
1542 clean_up_after_unswitching (int ignored_edge_flag
)
1548 FOR_EACH_BB_FN (bb
, cfun
)
1550 gswitch
*stmt
= safe_dyn_cast
<gswitch
*> (last_stmt (bb
));
1551 if (stmt
&& !CONSTANT_CLASS_P (gimple_switch_index (stmt
)))
1553 unsigned nlabels
= gimple_switch_num_labels (stmt
);
1555 tree lab
= gimple_switch_default_label (stmt
);
1556 edge default_e
= find_edge (gimple_bb (stmt
),
1557 label_to_block (cfun
, CASE_LABEL (lab
)));
1558 for (unsigned i
= 1; i
< nlabels
; ++i
)
1560 tree lab
= gimple_switch_label (stmt
, i
);
1561 basic_block dest
= label_to_block (cfun
, CASE_LABEL (lab
));
1562 edge e
= find_edge (gimple_bb (stmt
), dest
);
1564 ; /* The edge is already removed. */
1565 else if (e
->flags
& ignored_edge_flag
)
1567 /* We may not remove the default label so we also have
1568 to preserve its edge. But we can remove the
1569 non-default CASE sharing the edge. */
1575 gimple_switch_set_label (stmt
, index
, lab
);
1580 if (index
!= nlabels
)
1581 gimple_switch_set_num_labels (stmt
, index
);
1584 /* Clean up the ignored_edge_flag from edges. */
1585 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1586 e
->flags
&= ~ignored_edge_flag
;
1590 /* Loop unswitching pass. */
1594 const pass_data pass_data_tree_unswitch
=
1596 GIMPLE_PASS
, /* type */
1597 "unswitch", /* name */
1598 OPTGROUP_LOOP
, /* optinfo_flags */
1599 TV_TREE_LOOP_UNSWITCH
, /* tv_id */
1600 PROP_cfg
, /* properties_required */
1601 0, /* properties_provided */
1602 0, /* properties_destroyed */
1603 0, /* todo_flags_start */
1604 0, /* todo_flags_finish */
1607 class pass_tree_unswitch
: public gimple_opt_pass
1610 pass_tree_unswitch (gcc::context
*ctxt
)
1611 : gimple_opt_pass (pass_data_tree_unswitch
, ctxt
)
1614 /* opt_pass methods: */
1615 bool gate (function
*) final override
{ return flag_unswitch_loops
!= 0; }
1616 unsigned int execute (function
*) final override
;
1618 }; // class pass_tree_unswitch
1621 pass_tree_unswitch::execute (function
*fun
)
1623 if (number_of_loops (fun
) <= 1)
1626 return tree_ssa_unswitch_loops (fun
);
1632 make_pass_tree_unswitch (gcc::context
*ctxt
)
1634 return new pass_tree_unswitch (ctxt
);