<commit_before>/*
 * This file is part of libFirm.
 * Copyright (C) 2012 University of Karlsruhe.
 */

/**
 * @file
 * @brief    Optimizations for a whole ir graph, i.e., a procedure.
 * @author   <NAME>, <NAME>, <NAME>,
 *           <NAME>
 */
#include <assert.h>

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

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

#include "adt/pdeq.h"

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

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

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

void local_optimize_node(ir_node *n)
{
	ir_graph *irg = get_irn_irg(n);

	if (get_opt_global_cse())
		set_irg_pinned(irg, op_pin_state_floats);
	clear_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE);

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

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

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)
{
	foreach_out_edge(n, edge) {
		ir_node *succ  = get_edge_src_irn(edge);

		enqueue_node(succ, waitq);

		/* Also enqueue Phis to prevent inconsistencies. */
		if (is_Block(succ)) {
			foreach_out_edge(succ, edge2) {
				ir_node *succ2 = get_edge_src_irn(edge2);

				if (is_Phi(succ2)) {
					enqueue_node(succ2, waitq);
				}
			}
		} else 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);

		foreach_block_succ(block, edge) {
			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);
	}
}

void local_optimize_graph(ir_graph *irg)
{
	local_optimize_node(get_irg_end(irg));
}

/**
 * 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;
	set_irn_link(n, NULL);

	/* If CSE occurs during the optimization,
	 * our operands have fewer users than before.
	 * Thus, we may be able to apply a rule that
	 * requires an operand with only one user.
	 * Hence, we need a loop to reach the fixpoint. */
	ir_node *optimized = n;
	ir_node *last;
	do {
		last      = optimized;
		optimized = optimize_in_place_2(last);

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

void optimize_graph_df(ir_graph *irg)
{
	pdeq *waitq = new_pdeq();

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

	/* enable unreachable code elimination,
	 * not that currently disabling algebraic simplifications disables all
	 * transform_node_XXX() functions and therefore unreachable code
	 * elimination. */
	if (get_opt_algebraic_simplification()) {
		assert(!irg_is_constrained(irg, IR_GRAPH_CONSTRAINT_OPTIMIZE_UNREACHABLE_CODE));
		add_irg_constraints(irg, IR_GRAPH_CONSTRAINT_OPTIMIZE_UNREACHABLE_CODE);
	}

	new_identities(irg);
	assure_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_OUT_EDGES
	                         | IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE);

	ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
<commit_msg>Do not assure dominance at this place.<commit_after>/*
 * This file is part of libFirm.
 * Copyright (C) 2012 University of Karlsruhe.
 */

/**
 * @file
 * @brief    Optimizations for a whole ir graph, i.e., a procedure.
 * @author   <NAME>, <NAME>, <NAME>,
 *           <NAME>
 */
#include <assert.h>

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

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

#include "adt/pdeq.h"

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

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

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

void local_optimize_node(ir_node *n)
{
	ir_graph *irg = get_irn_irg(n);

	if (get_opt_global_cse())
		set_irg_pinned(irg, op_pin_state_floats);
	clear_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE);

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

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

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)
{
	foreach_out_edge(n, edge) {
		ir_node *succ  = get_edge_src_irn(edge);

		enqueue_node(succ, waitq);

		/* Also enqueue Phis to prevent inconsistencies. */
		if (is_Block(succ)) {
			foreach_out_edge(succ, edge2) {
				ir_node *succ2 = get_edge_src_irn(edge2);

				if (is_Phi(succ2)) {
					enqueue_node(succ2, waitq);
				}
			}
		} else 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);

		foreach_block_succ(block, edge) {
			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);
	}
}

void local_optimize_graph(ir_graph *irg)
{
	local_optimize_node(get_irg_end(irg));
}

/**
 * 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;
	set_irn_link(n, NULL);

	/* If CSE occurs during the optimization,
	 * our operands have fewer users than before.
	 * Thus, we may be able to apply a rule that
	 * requires an operand with only one user.
	 * Hence, we need a loop to reach the fixpoint. */
	ir_node *optimized = n;
	ir_node *last;
	do {
		last      = optimized;
		optimized = optimize_in_place_2(last);

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

void optimize_graph_df(ir_graph *irg)
{
	pdeq *waitq = new_pdeq();

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

	/* enable unreachable code elimination,
	 * not that currently disabling algebraic simplifications disables all
	 * transform_node_XXX() functions and therefore unreachable code
	 * elimination. */
	if (get_opt_algebraic_simplification()) {
		assert(!irg_is_constrained(irg, IR_GRAPH_CONSTRAINT_OPTIMIZE_UNREACHABLE_CODE));
		add_irg_constraints(irg, IR_GRAPH_CONSTRAINT_OPTIMIZE_UNREACHABLE_CODE);
	}

	new_identities(irg);
	assure_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_OUT_EDGES);

	ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);

{'fraction_non_alphanumeric': 0.11877394636015326, 'fraction_numerical': 0.0035919540229885057, 'mean_word_length': 4.431079323797139, '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': '3167005', 'n_tokens_mistral': 3589, 'n_tokens_neox': 3303, 'n_words': 745}