gcc/gcc/tree-ssa-loop-unswitch.c
Steven Bosscher 7ee2468b92 system.h (dump_file): Do not define.
gcc/
	* system.h (dump_file): Do not define.
	* tree-pass.h: Include dumpfile.h, which is a new file containing...
	(enum tree_dump_index, TDF_*, get_dump_file_name, dump_enabled_p,
	dump_initialized_p, dump_begin, dump_end, dump_node, dump_switch_p,
	dump_flag_name, dump_file, dump_flags, dump_file_name,
	get_dump_file_info, struct dump_file_info): all of this, moved to...
	* dumpfile.h: Here, new file.
	* tree-dump.h: Include dumpfile.h, but not tree-pass.h.
	(dump_stmt): Remove prototype for C++ specific function.
	(dump_enumerated_decls): Move prototype from here...
	* tree-flow.h (dump_enumerated_decls): ... to here.
	(get_ref_base_and_extent) Move prototype from here ...
	* tree.h (get_ref_base_and_extent) ... to here.
	* tree-ssa-live.c: Do not inclde tree-pretty-print.h, because
	gimple-pretty-print.h is enough.  Do not include tree-dump.h,
	include timevar.h and dumpfile.h instead.
	(struct numbered_tree_d, compare_decls_by_uid,
	dump_enumerated_decls_push, dump_enumerated_decls): Move from here ...
	* tree-dfa.c:(struct numbered_tree_d, compare_decls_by_uid,
	dump_enumerated_decls_push, dump_enumerated_decls):... to here.
	Do not include timevar.h.
	* tree.c: Do not include timevar.h.
	* tree-cfg.c: Do not include langhooks.h, tree-pretty-print.h, and
	timevar.h.
	(dump_cfg_stats): Use current_function_name.
	(gimple_cfg2vcg): Likewise.
	(dump_function_to_file): Likewise.
	* df-scan.c: Do not include tree-pass.h and timevar.h.
	Include dumpfile.h.
	(df_entry_block_bitmap_verify, df_exit_block_bitmap_verify): Do not
	use print_current_pass.
	* df-problems.c: Include dumpfile.h.
	Always define REG_DEAD_DEBUGGING, avoid #ifdef code, because it
	leads to errors in the code not selected.
	(df_note_compute): Do not print_rtl_with_bb here.  Fix compilation
	bug if REG_DEAD_DEBUGGING is not 0, get_insns is not available here.
	* lcm.c: Include dumpfile.h.
	Remove obsolete include of insn-attr.h.
	* dojump.c (do_compare_rtx_and_jump): Remove failure printing for
	missing probability notes.
	* stmt.c: Include dumpfile.h.
	(emit_case_decision_tree): Re-enable printing expand details only
	if TDF_DETAILS.
	* alias.c, auto-inc-dec.c, bb-reorder.c, caller-save.c, cfg.c, 
	cfgcleanup.c, cfgexpand.c, cfgloop.c, cfgloopmanip.c, cgraph.c, 
	cgraphclones.c, cgraphunit.c, combine.c, combine-stack-adj.c, 
	coverage.c, cprop.c, cse.c, cselib.c, dbgcnt.c, dce.c, df-core.c, 
	dse.c, dwarf2out.c, emit-rtl.c, except.c, expr.c, final.c, 
	function.c, fwprop.c, gcse.c, gimple-fold.c, 
	gimple-pretty-print.c, gimple-ssa-strength-reduction.c, 
	gimplify.c, graphite-blocking.c, graphite-clast-to-gimple.c, 
	graphite-dependences.c, graphite-interchange.c, 
	graphite-optimize-isl.c, graphite-poly.c, 
	graphite-sese-to-poly.c, haifa-sched.c, hw-doloop.c, ifcvt.c, 
	ipa.c, ipa-cp.c, ipa-inline-analysis.c, ipa-inline.c, 
	ipa-inline-transform.c, ipa-prop.c, ipa-pure-const.c, 
	ipa-reference.c, ipa-split.c, ipa-utils.c, ira.c, ira-emit.c, 
	jump.c, loop-doloop.c, loop-init.c, loop-invariant.c, loop-iv.c, 
	loop-unroll.c, loop-unswitch.c, lower-subreg.c, 
	lto-section-out.c, lto-streamer-in.c, matrix-reorg.c, mcf.c, 
	mode-switching.c, modulo-sched.c, omega.c, omp-low.c, passes.c, 
	plugin.c, postreload.c, postreload-gcse.c, predict.c, print-rtl.c, 
	print-tree.c, profile.c, recog.c, ree.c, regcprop.c, reginfo.c, 
	regmove.c, regrename.c, reg-stack.c, reload1.c, reorg.c, 
	sched-rgn.c, sched-vis.c, sel-sched.c, sel-sched-ir.c, 
	store-motion.c, tracer.c, trans-mem.c, tree-affine.c, 
	tree-call-cdce.c, tree-cfgcleanup.c, tree-chrec.c, 
	tree-data-ref.c, tree-diagnostic.c, tree-dump.c,
	tree-eh.c, tree-flow-inline.h, tree-if-conv.c, tree-into-ssa.c, 
	tree-mudflap.c, tree-nrv.c, tree-object-size.c, 
	tree-optimize.c, tree-outof-ssa.c, tree-predcom.c, 
	tree-pretty-print.c, tree-profile.c, tree-scalar-evolution.c, 
	tree-sra.c, tree-ssa-address.c, tree-ssa-alias.c, tree-ssa.c, 
	tree-ssa-ccp.c, tree-ssa-coalesce.c, tree-ssa-copy.c, 
	tree-ssa-copyrename.c,, tree-ssa-dce.c, tree-ssa-dom.c, 
	tree-ssa-dse.c, tree-ssa-forwprop.c, tree-ssa-ifcombine.c, 
	tree-ssa-loop.c, tree-ssa-loop-ch.c, tree-ssa-loop-im.c, 
	tree-ssa-loop-ivcanon.c, tree-ssa-loop-ivopts.c, 
	tree-ssa-loop-manip.c, tree-ssa-loop-niter.c, 
	tree-ssa-loop-prefetch.c, tree-ssa-loop-unswitch.c, 
	tree-ssa-math-opts.c, tree-ssa-operands.c, tree-ssa-phiopt.c, 
	tree-ssa-phiprop.c, tree-ssa-pre.c, tree-ssa-propagate.c, 
	tree-ssa-reassoc.c, tree-ssa-sccvn.c, tree-ssa-sink.c, 
	tree-ssa-structalias.c, tree-ssa-tail-merge.c, tree-ssa-ter.c, 
	tree-ssa-threadedge.c, tree-ssa-threadupdate.c, 
	tree-ssa-uncprop.c, tree-ssa-uninit.c, 
	tree-switch-conversion.c, tree-tailcall.c, 
	tree-vect-data-refs.c, tree-vect-loop.c, 
	tree-vect-loop-manip.c, tree-vectorizer.c, 
	tree-vect-patterns.c, tree-vect-slp.c, tree-vect-stmts.c, 
	tree-vrp.c, value-prof.c, var-tracking.c, web.c: Include tree-pass.h
	only if needed.  If tree-pass.h is included, do not include timevar.h
	and dumpfile.h.  If tree-pass.h is not included but dump_file, or
	dump_flags, or the TDF_* flags are used, include dumpfile.h.
	If gimple-pretty-print.h is included, don't include tree-pretty-print.h.
	Remove assorted unnecessary includes.

	* config/mn10300/mn10300.c, config/c6x/c6x.c, config/ia64/ia64.c,
	config/arm/arm.c, config/bfin/bfin.c, config/frv/frv.c,
	config/spu/spu.c, config/mep/mep.c, config/i386/i386.c:
	Include dumpfile.h.
	* config/rl78/rl78.c: Include dumpfile.h instead of tree-pass.h.
	* arm/t-arm, avr/t-avr, i386/t-i386, ia64/t-ia64, mep/t-mep,
	spu/t-spu-elf: Fix dependencies.

c-family/
	* c-gimplify.c: Include dumpfile.h instead of tree-dump.h.
	* c-ada-spec.c: Likewise.
	* c-dump.c (dump_stmt): Move to cp/dump.c, the only user.

c/
	* c-decl.c: Include dumpfile.h instead of tree-dump.h.
	* Make-lang.in: Fix dependencies.

cp/
	* dump.c (dump_stmt): Moved here from c-dump.c.
	* optimize.c: Include dumpfile.h instead of tree-dump.h.
	* class.c: Likewise.
	* decl2.c: Likewise.
	* Make-lang.in: Fix dependencies.

fortran/
	* f95-lang.c: Include dumpfile.h instead of tree-dump.h.
	* Make-lang.in: Fix dependencies.

java/
	* java-gimplify.c Include dumpfile.h instead of tree-dump.h
	* Make-lang.in: Fix dependencies.

lto/
	* lto.c: Do not include timevar.h.
	* Make-lang.in: Fix dependencies.

ada/
	* gcc-interface/utils.c: Include timevar.h.
	* Make-lang.in: Fix dependencies.

From-SVN: r189519
2012-07-16 11:32:42 +00:00

380 lines
9.6 KiB
C

/* Loop unswitching.
Copyright (C) 2004, 2005, 2007, 2008, 2010 Free Software Foundation, Inc.
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 3, or (at your option) any
later version.
GCC is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "tree.h"
#include "tm_p.h"
#include "basic-block.h"
#include "tree-flow.h"
#include "cfgloop.h"
#include "params.h"
#include "tree-pass.h"
#include "tree-inline.h"
/* This file implements the loop unswitching, i.e. transformation of loops like
while (A)
{
if (inv)
B;
X;
if (!inv)
C;
}
where inv is the loop invariant, into
if (inv)
{
while (A)
{
B;
X;
}
}
else
{
while (A)
{
X;
C;
}
}
Inv is considered invariant iff the values it compares are both invariant;
tree-ssa-loop-im.c ensures that all the suitable conditions are in this
shape. */
static struct loop *tree_unswitch_loop (struct loop *, basic_block, tree);
static bool tree_unswitch_single_loop (struct loop *, int);
static tree tree_may_unswitch_on (basic_block, struct loop *);
/* Main entry point. Perform loop unswitching on all suitable loops. */
unsigned int
tree_ssa_unswitch_loops (void)
{
loop_iterator li;
struct loop *loop;
bool changed = false;
/* Go through inner loops (only original ones). */
FOR_EACH_LOOP (li, loop, LI_ONLY_INNERMOST)
{
if (dump_file && (dump_flags & TDF_DETAILS))
fprintf (dump_file, ";; Considering loop %d\n", loop->num);
/* Do not unswitch in cold regions. */
if (optimize_loop_for_size_p (loop))
{
if (dump_file && (dump_flags & TDF_DETAILS))
fprintf (dump_file, ";; Not unswitching cold loops\n");
continue;
}
/* The loop should not be too large, to limit code growth. */
if (tree_num_loop_insns (loop, &eni_size_weights)
> (unsigned) PARAM_VALUE (PARAM_MAX_UNSWITCH_INSNS))
{
if (dump_file && (dump_flags & TDF_DETAILS))
fprintf (dump_file, ";; Not unswitching, loop too big\n");
continue;
}
changed |= tree_unswitch_single_loop (loop, 0);
}
if (changed)
return TODO_cleanup_cfg;
return 0;
}
/* Checks whether we can unswitch LOOP on condition at end of BB -- one of its
basic blocks (for what it means see comments below). */
static tree
tree_may_unswitch_on (basic_block bb, struct loop *loop)
{
gimple stmt, def;
tree cond, use;
basic_block def_bb;
ssa_op_iter iter;
/* BB must end in a simple conditional jump. */
stmt = last_stmt (bb);
if (!stmt || gimple_code (stmt) != GIMPLE_COND)
return NULL_TREE;
/* To keep the things simple, we do not directly remove the conditions,
but just replace tests with 0 != 0 resp. 1 != 0. Prevent the infinite
loop where we would unswitch again on such a condition. */
if (gimple_cond_true_p (stmt) || gimple_cond_false_p (stmt))
return NULL_TREE;
/* Condition must be invariant. */
FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
{
def = SSA_NAME_DEF_STMT (use);
def_bb = gimple_bb (def);
if (def_bb
&& flow_bb_inside_loop_p (loop, def_bb))
return NULL_TREE;
}
cond = build2 (gimple_cond_code (stmt), boolean_type_node,
gimple_cond_lhs (stmt), gimple_cond_rhs (stmt));
return cond;
}
/* Simplifies COND using checks in front of the entry of the LOOP. Just very
simplish (sufficient to prevent us from duplicating loop in unswitching
unnecessarily). */
static tree
simplify_using_entry_checks (struct loop *loop, tree cond)
{
edge e = loop_preheader_edge (loop);
gimple stmt;
while (1)
{
stmt = last_stmt (e->src);
if (stmt
&& gimple_code (stmt) == GIMPLE_COND
&& gimple_cond_code (stmt) == TREE_CODE (cond)
&& operand_equal_p (gimple_cond_lhs (stmt),
TREE_OPERAND (cond, 0), 0)
&& operand_equal_p (gimple_cond_rhs (stmt),
TREE_OPERAND (cond, 1), 0))
return (e->flags & EDGE_TRUE_VALUE
? boolean_true_node
: boolean_false_node);
if (!single_pred_p (e->src))
return cond;
e = single_pred_edge (e->src);
if (e->src == ENTRY_BLOCK_PTR)
return cond;
}
}
/* Unswitch single LOOP. NUM is number of unswitchings done; we do not allow
it to grow too much, it is too easy to create example on that the code would
grow exponentially. */
static bool
tree_unswitch_single_loop (struct loop *loop, int num)
{
basic_block *bbs;
struct loop *nloop;
unsigned i, found;
tree cond = NULL_TREE;
gimple stmt;
bool changed = false;
i = 0;
bbs = get_loop_body (loop);
found = loop->num_nodes;
while (1)
{
/* Find a bb to unswitch on. */
for (; i < loop->num_nodes; i++)
if ((cond = tree_may_unswitch_on (bbs[i], loop)))
break;
if (i == loop->num_nodes)
{
if (dump_file
&& num > PARAM_VALUE (PARAM_MAX_UNSWITCH_LEVEL)
&& (dump_flags & TDF_DETAILS))
fprintf (dump_file, ";; Not unswitching anymore, hit max level\n");
if (found == loop->num_nodes)
{
free (bbs);
return changed;
}
break;
}
cond = simplify_using_entry_checks (loop, cond);
stmt = last_stmt (bbs[i]);
if (integer_nonzerop (cond))
{
/* Remove false path. */
gimple_cond_set_condition_from_tree (stmt, boolean_true_node);
changed = true;
}
else if (integer_zerop (cond))
{
/* Remove true path. */
gimple_cond_set_condition_from_tree (stmt, boolean_false_node);
changed = true;
}
/* Do not unswitch too much. */
else if (num > PARAM_VALUE (PARAM_MAX_UNSWITCH_LEVEL))
{
i++;
continue;
}
/* In nested tree_unswitch_single_loop first optimize all conditions
using entry checks, then discover still reachable blocks in the
loop and find the condition only among those still reachable bbs. */
else if (num != 0)
{
if (found == loop->num_nodes)
found = i;
i++;
continue;
}
else
{
found = i;
break;
}
update_stmt (stmt);
i++;
}
if (num != 0)
{
basic_block *tos, *worklist;
/* When called recursively, first do a quick discovery
of reachable bbs after the above changes and only
consider conditions in still reachable bbs. */
tos = worklist = XNEWVEC (basic_block, loop->num_nodes);
for (i = 0; i < loop->num_nodes; i++)
bbs[i]->flags &= ~BB_REACHABLE;
/* Start with marking header. */
*tos++ = bbs[0];
bbs[0]->flags |= BB_REACHABLE;
/* Iterate: find everything reachable from what we've already seen
within the same innermost loop. Don't look through false edges
if condition is always true or true edges if condition is
always false. */
while (tos != worklist)
{
basic_block b = *--tos;
edge e;
edge_iterator ei;
int flags = 0;
if (EDGE_COUNT (b->succs) == 2)
{
gimple stmt = last_stmt (b);
if (stmt
&& gimple_code (stmt) == GIMPLE_COND)
{
if (gimple_cond_true_p (stmt))
flags = EDGE_FALSE_VALUE;
else if (gimple_cond_false_p (stmt))
flags = EDGE_TRUE_VALUE;
}
}
FOR_EACH_EDGE (e, ei, b->succs)
{
basic_block dest = e->dest;
if (dest->loop_father == loop
&& !(dest->flags & BB_REACHABLE)
&& !(e->flags & flags))
{
*tos++ = dest;
dest->flags |= BB_REACHABLE;
}
}
}
free (worklist);
/* Find a bb to unswitch on. */
for (; found < loop->num_nodes; found++)
if ((bbs[found]->flags & BB_REACHABLE)
&& (cond = tree_may_unswitch_on (bbs[found], loop)))
break;
if (found == loop->num_nodes)
{
free (bbs);
return changed;
}
}
if (dump_file && (dump_flags & TDF_DETAILS))
fprintf (dump_file, ";; Unswitching loop\n");
initialize_original_copy_tables ();
/* Unswitch the loop on this condition. */
nloop = tree_unswitch_loop (loop, bbs[found], cond);
if (!nloop)
{
free_original_copy_tables ();
free (bbs);
return changed;
}
/* Update the SSA form after unswitching. */
update_ssa (TODO_update_ssa);
free_original_copy_tables ();
/* Invoke itself on modified loops. */
tree_unswitch_single_loop (nloop, num + 1);
tree_unswitch_single_loop (loop, num + 1);
free (bbs);
return true;
}
/* Unswitch a LOOP w.r. to given basic block UNSWITCH_ON. We only support
unswitching of innermost loops. COND is the condition determining which
loop is entered -- the new loop is entered if COND is true. Returns NULL
if impossible, new loop otherwise. */
static struct loop *
tree_unswitch_loop (struct loop *loop,
basic_block unswitch_on, tree cond)
{
unsigned prob_true;
edge edge_true, edge_false;
/* Some sanity checking. */
gcc_assert (flow_bb_inside_loop_p (loop, unswitch_on));
gcc_assert (EDGE_COUNT (unswitch_on->succs) == 2);
gcc_assert (loop->inner == NULL);
extract_true_false_edges_from_block (unswitch_on, &edge_true, &edge_false);
prob_true = edge_true->probability;
return loop_version (loop, unshare_expr (cond),
NULL, prob_true, prob_true,
REG_BR_PROB_BASE - prob_true, false);
}