<commit_before>/*
 * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
 *
 * This file is part of libFirm.
 *
 * This file may be distributed and/or modified under the terms of the
 * GNU General Public License version 2 as published by the Free Software
 * Foundation and appearing in the file LICENSE.GPL included in the
 * packaging of this file.
 *
 * Licensees holding valid libFirm Professional Edition licenses may use
 * this file in accordance with the libFirm Commercial License.
 * Agreement provided with the Software.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE.
 */

/**
 * @file
 * @brief    Optimizations for a whole ir graph, i.e., a procedure.
 * @author   <NAME>, <NAME>, <NAME>,
 *           <NAME>
 * @version  $Id$
 */
#include "config.h"

#include <assert.h>

#include "irnode_t.h"
#include "irgraph_t.h"

#include "iroptimize.h"
#include "iropt_t.h"
#include "irgopt.h"
#include "irgmod.h"
#include "irgwalk.h"

#include "adt/pdeq.h"

#include "irpass_t.h"
#include "irflag_t.h"
#include "iredges_t.h"
#include "irtools.h"

/*------------------------------------------------------------------*/
/* apply optimizations of iropt to all nodes.                       */
/*------------------------------------------------------------------*/

/**
 * A wrapper around optimize_inplace_2() to be called from a walker.
 */
static void optimize_in_place_wrapper(ir_node *n, void *env)
{
	ir_node *optimized = optimize_in_place_2(n);
	(void) env;

	if (optimized != n) {
		exchange(n, optimized);
	}
}

/**
 * Do local optimizations for a node.
 *
 * @param n  the IR-node where to start. Typically the End node
 *           of a graph
 *
 * @note current_ir_graph must be set
 */
static inline void do_local_optimize(ir_node *n)
{
	ir_graph *irg = get_irn_irg(n);

	/* Handle graph state */
	assert(get_irg_phase_state(irg) != phase_building);

	if (get_opt_global_cse())
		set_irg_pinned(irg, op_pin_state_floats);
	clear_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_DOMINANCE);

	/* Clean the value_table in irg for the CSE. */
	new_identities(irg);

	/* walk over the graph */
	irg_walk(n, firm_clear_link, optimize_in_place_wrapper, NULL);
}

/* Applies local optimizations (see iropt.h) to all nodes reachable from node n */
void local_optimize_node(ir_node *n)
{
	ir_graph *rem = current_ir_graph;
	current_ir_graph = get_irn_irg(n);

	do_local_optimize(n);

	current_ir_graph = rem;
}

static void enqueue_node(ir_node *node, pdeq *waitq)
{
	if (get_irn_link(node) == waitq)
		return;
	pdeq_putr(waitq, node);
	set_irn_link(node, waitq);
}

/**
 * Enqueue all users of a node to a wait queue.
 * Handles mode_T nodes.
 */
static void enqueue_users(ir_node *n, pdeq *waitq)
{
	const ir_edge_t *edge;

	foreach_out_edge(n, edge) {
		ir_node *succ = get_edge_src_irn(edge);

		enqueue_node(succ, waitq);
		if (get_irn_mode(succ) == mode_T) {
		/* A mode_T node has Proj's. Because most optimizations
			run on the Proj's we have to enqueue them also. */
			enqueue_users(succ, waitq);
		}
	}
}

/**
 * Block-Walker: uses dominance depth to mark dead blocks.
 */
static void find_unreachable_blocks(ir_node *block, void *env)
{
	pdeq *waitq = (pdeq*) env;

	if (get_Block_dom_depth(block) < 0) {
		ir_graph *irg = get_irn_irg(block);
		ir_node  *end = get_irg_end(irg);

		const ir_edge_t *edge;
		foreach_block_succ(block, edge) {
			const ir_edge_t *edge2;
			ir_node *succ_block = get_edge_src_irn(edge);
			enqueue_node(succ_block, waitq);
			foreach_out_edge(succ_block, edge2) {
				ir_node *succ = get_edge_src_irn(edge2);
				if (is_Phi(succ))
					enqueue_node(succ, waitq);
			}
		}
		enqueue_node(end, waitq);
	}
}

/* Applies local optimizations (see iropt.h) to all nodes reachable from node n. */
void local_optimize_graph(ir_graph *irg)
{
	ir_graph *rem = current_ir_graph;
	current_ir_graph = irg;

	do_local_optimize(get_irg_end(irg));

	current_ir_graph = rem;
}

/**
 * Data flow optimization walker.
 * Optimizes all nodes and enqueue its users
 * if done.
 */
static void opt_walker(ir_node *n, void *env)
{
	pdeq *waitq = (pdeq*)env;
	ir_node *optimized;

	optimized = optimize_in_place_2(n);
	set_irn_link(optimized, NULL);

	if (optimized != n) {
		enqueue_users(n, waitq);
		exchange(n, optimized);
	}
}

/* Applies local optimizations to all nodes in the graph until fixpoint. */
int optimize_graph_df(ir_graph *irg)
{
	pdeq     *waitq = new_pdeq();
	ir_graph *rem = current_ir_graph;
	ir_node  *end;
	int      state, changed;

	current_ir_graph = irg;

	state = edges_assure(irg);

	/* Clean the value_table in irg for the CSE. */
	new_identities(irg);

	if (get_opt_global_cse()) {
		set_irg_pinned(irg, op_pin_state_floats);
	}

	/* The following enables unreachable code elimination (=Blocks may be
	 * Bad). */
	clear_irg_state(irg, IR_GRAPH_STATE_NO_UNREACHABLE_BLOCKS);

	/* invalidate info */
	clear_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_DOMINANCE);

	ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);

	/* Calculate dominance so we can kill unreachable code */
	assure_doms(irg);

	/* walk over the graph, but don't touch keep-alives */
	irg_walk_graph(irg, NULL, opt_walker, waitq);

	/* any optimized nodes are stored in the wait queue,
	 * so if it's not empty, the graph has been changed */
	changed = !pdeq_empty(waitq);

	while (!pdeq_empty(waitq)) {
		/* finish the wait queue */
		while (! pdeq_empty(waitq)) {
			ir_node *n = (ir_node*)pdeq_getl(waitq);
			opt_walker(n, waitq);
		}
		/* Calculate dominance so we can kill unreachable code */
		compute_doms(irg);
		irg_block_walk_graph(irg, NULL, find_unreachable_blocks, waitq);
	}
	clear_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_DOMINANCE);

	del_pdeq(waitq);

	ir_free_resources(irg, IR_RESOURCE_IRN_LINK);

	if (! state)
		edges_deactivate(irg);

	if (remove_bads(irg)) {
		edges_deactivate(irg);
	}

	/* Finally kill BAD and doublets from the keep alives.
	   Doing this AFTER edges where deactivated saves cycles */
	end = get_irg_end(irg);
	remove_End_Bads_and_doublets(end);

	set_irg_state(irg, IR_GRAPH_STATE_NO_UNREACHABLE_BLOCKS);

	current_ir_graph = rem;
	return changed;
}

/* Creates an ir_graph pass for optimize_graph_df. */
ir_graph_pass_t *optimize_graph_df_pass(const char *name)
{
	return def_graph_pass_ret(name ? name : "optimize_graph_df", optimize_graph_df);
}  /* optimize_graph_df_pass */
<commit_msg>Documentation: why kill unreachable code between localopts<commit_after>/*
 * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
 *
 * This file is part of libFirm.
 *
 * This file may be distributed and/or modified under the terms of the
 * GNU General Public License version 2 as published by the Free Software
 * Foundation and appearing in the file LICENSE.GPL included in the
 * packaging of this file.
 *
 * Licensees holding valid libFirm Professional Edition licenses may use
 * this file in accordance with the libFirm Commercial License.
 * Agreement provided with the Software.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE.
 */

/**
 * @file
 * @brief    Optimizations for a whole ir graph, i.e., a procedure.
 * @author   <NAME>, <NAME>, <NAME>,
 *           <NAME>
 * @version  $Id$
 */
#include "config.h"

#include <assert.h>

#include "irnode_t.h"
#include "irgraph_t.h"

#include "iroptimize.h"
#include "iropt_t.h"
#include "irgopt.h"
#include "irgmod.h"
#include "irgwalk.h"

#include "adt/pdeq.h"

#include "irpass_t.h"
#include "irflag_t.h"
#include "iredges_t.h"
#include "irtools.h"

/*------------------------------------------------------------------*/
/* apply optimizations of iropt to all nodes.                       */
/*------------------------------------------------------------------*/

/**
 * A wrapper around optimize_inplace_2() to be called from a walker.
 */
static void optimize_in_place_wrapper(ir_node *n, void *env)
{
	ir_node *optimized = optimize_in_place_2(n);
	(void) env;

	if (optimized != n) {
		exchange(n, optimized);
	}
}

/**
 * Do local optimizations for a node.
 *
 * @param n  the IR-node where to start. Typically the End node
 *           of a graph
 *
 * @note current_ir_graph must be set
 */
static inline void do_local_optimize(ir_node *n)
{
	ir_graph *irg = get_irn_irg(n);

	/* Handle graph state */
	assert(get_irg_phase_state(irg) != phase_building);

	if (get_opt_global_cse())
		set_irg_pinned(irg, op_pin_state_floats);
	clear_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_DOMINANCE);

	/* Clean the value_table in irg for the CSE. */
	new_identities(irg);

	/* walk over the graph */
	irg_walk(n, firm_clear_link, optimize_in_place_wrapper, NULL);
}

/* Applies local optimizations (see iropt.h) to all nodes reachable from node n */
void local_optimize_node(ir_node *n)
{
	ir_graph *rem = current_ir_graph;
	current_ir_graph = get_irn_irg(n);

	do_local_optimize(n);

	current_ir_graph = rem;
}

static void enqueue_node(ir_node *node, pdeq *waitq)
{
	if (get_irn_link(node) == waitq)
		return;
	pdeq_putr(waitq, node);
	set_irn_link(node, waitq);
}

/**
 * Enqueue all users of a node to a wait queue.
 * Handles mode_T nodes.
 */
static void enqueue_users(ir_node *n, pdeq *waitq)
{
	const ir_edge_t *edge;

	foreach_out_edge(n, edge) {
		ir_node *succ = get_edge_src_irn(edge);

		enqueue_node(succ, waitq);
		if (get_irn_mode(succ) == mode_T) {
		/* A mode_T node has Proj's. Because most optimizations
			run on the Proj's we have to enqueue them also. */
			enqueue_users(succ, waitq);
		}
	}
}

/**
 * Block-Walker: uses dominance depth to mark dead blocks.
 */
static void find_unreachable_blocks(ir_node *block, void *env)
{
	pdeq *waitq = (pdeq*) env;

	if (get_Block_dom_depth(block) < 0) {
		ir_graph *irg = get_irn_irg(block);
		ir_node  *end = get_irg_end(irg);

		const ir_edge_t *edge;
		foreach_block_succ(block, edge) {
			const ir_edge_t *edge2;
			ir_node *succ_block = get_edge_src_irn(edge);
			enqueue_node(succ_block, waitq);
			foreach_out_edge(succ_block, edge2) {
				ir_node *succ = get_edge_src_irn(edge2);
				if (is_Phi(succ))
					enqueue_node(succ, waitq);
			}
		}
		enqueue_node(end, waitq);
	}
}

/* Applies local optimizations (see iropt.h) to all nodes reachable from node n. */
void local_optimize_graph(ir_graph *irg)
{
	ir_graph *rem = current_ir_graph;
	current_ir_graph = irg;

	do_local_optimize(get_irg_end(irg));

	current_ir_graph = rem;
}

/**
 * Data flow optimization walker.
 * Optimizes all nodes and enqueue its users
 * if done.
 */
static void opt_walker(ir_node *n, void *env)
{
	pdeq *waitq = (pdeq*)env;
	ir_node *optimized;

	optimized = optimize_in_place_2(n);
	set_irn_link(optimized, NULL);

	if (optimized != n) {
		enqueue_users(n, waitq);
		exchange(n, optimized);
	}
}

/* Applies local optimizations to all nodes in the graph until fixpoint. */
int optimize_graph_df(ir_graph *irg)
{
	pdeq     *waitq = new_pdeq();
	ir_graph *rem = current_ir_graph;
	ir_node  *end;
	int      state, changed;

	current_ir_graph = irg;

	state = edges_assure(irg);

	/* Clean the value_table in irg for the CSE. */
	new_identities(irg);

	if (get_opt_global_cse()) {
		set_irg_pinned(irg, op_pin_state_floats);
	}

	/* The following enables unreachable code elimination (=Blocks may be
	 * Bad). */
	clear_irg_state(irg, IR_GRAPH_STATE_NO_UNREACHABLE_BLOCKS);

	/* invalidate info */
	clear_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_DOMINANCE);

	ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);

	/* Calculate dominance so we can kill unreachable code */
	assure_doms(irg);

	/* walk over the graph, but don't touch keep-alives */
	irg_walk_graph(irg, NULL, opt_walker, waitq);

	/* any optimized nodes are stored in the wait queue,
	 * so if it's not empty, the graph has been changed */
	changed = !pdeq_empty(waitq);

	while (!pdeq_empty(waitq)) {
		/* finish the wait queue */
		while (! pdeq_empty(waitq)) {
			ir_node *n = (ir_node*)pdeq_getl(waitq);
			opt_walker(n, waitq);
		}
		/* Calculate dominance so we can kill unreachable code
		 * We want this intertwined with localopts for better optimization (phase coupling) */
		compute_doms(irg);
		irg_block_walk_graph(irg, NULL, find_unreachable_blocks, waitq);
	}
	clear_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_DOMINANCE);

	del_pdeq(waitq);

	ir_free_resources(irg, IR_RESOURCE_IRN_LINK);

	if (! state)
		edges_deactivate(irg);

	if (remove_bads(irg)) {
		edges_deactivate(irg);
	}

	/* Finally kill BAD and doublets from the keep alives.
	   Doing this AFTER edges where deactivated saves cycles */
	end = get_irg_end(irg);
	remove_End_Bads_and_doublets(end);

	set_irg_state(irg, IR_GRAPH_STATE_NO_UNREACHABLE_BLOCKS);

	current_ir_graph = rem;
	return changed;
}

/* Creates an ir_graph pass for optimize_graph_df. */
ir_graph_pass_t *optimize_graph_df_pass(const char *name)
{
	return def_graph_pass_ret(name ? name : "optimize_graph_df", optimize_graph_df);
}  /* optimize_graph_df_pass */

{'fraction_non_alphanumeric': 0.12816395197675307, 'fraction_numerical': 0.002447044429150417, 'mean_word_length': 3.8151693667157587, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7007224', 'n_tokens_mistral': 5134, 'n_tokens_neox': 4826, 'n_words': 1257}