dead_code_elimination.c 2.92 KB
Newer Older
1
2
/*
 * This file is part of libFirm.
3
 * Copyright (C) 2012 University of Karlsruhe.
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
 */

/**
 * @file
 * @brief    Dead node elimination
 * @author   Michael Beck, Goetz Lindenmaier
 *
 * Strictly speaking dead node elimination is unnecessary in firm - everthying
 * which is not used can't be found by any walker.
 * The only drawback is that the nodes still take up memory. This phase fixes
 * this by copying all (reachable) nodes to a new obstack and throwing away
 * the old one.
 */
#include "iroptimize.h"
#include "irnode_t.h"
#include "irgraph_t.h"
#include "iredges_t.h"
#include "irhooks.h"
#include "irtools.h"
#include "irgwalk.h"
#include "cgana.h"
#include "irouts.h"
#include "iropt_t.h"
#include "pmap.h"

/**
 * Reroute the inputs of a node from nodes in the old graph to copied nodes in
 * the new graph
 */
static void rewire_inputs(ir_node *node, void *env)
{
	(void) env;
	irn_rewire_inputs(node);
}

static void copy_node_dce(ir_node *node, void *env)
{
41
	ir_node  *new_node = exact_copy(node);
42
43
44
45
46
47
48
49
50
	(void) env;

	/* preserve the node numbers for easier debugging */
	new_node->node_nr = node->node_nr;

	set_irn_link(node, new_node);
}

/**
51
52
 * Copies the graph reachable from the End node to the obstack
 * in irg. Then fixes the fields containing nodes of the graph.
53
54
55
56
57
 *
 * @param copy_node_nr  If non-zero, the node number will be copied
 */
static void copy_graph_env(ir_graph *irg)
{
58
	ir_node *anchor = irg->anchor;
59
	ir_node *new_anchor;
60
61

	/* copy nodes */
62
	irg_walk_in_or_dep(anchor, copy_node_dce, rewire_inputs, NULL);
63
64

	/* fix the anchor */
65
	new_anchor = (ir_node*)get_irn_link(anchor);
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
	assert(new_anchor != NULL);
	irg->anchor = new_anchor;
}

/**
 * Copies all reachable nodes to a new obstack.  Removes bad inputs
 * from block nodes and the corresponding inputs from Phi nodes.
 * Merges single exit blocks with single entry blocks and removes
 * 1-input Phis.
 * Adds all new nodes to a new hash table for CSE.  Does not
 * perform CSE, so the hash table might contain common subexpressions.
 */
void dead_node_elimination(ir_graph *irg)
{
	edges_deactivate(irg);

	/* inform statistics that we started a dead-node elimination run */
	hook_dead_node_elim(irg, 1);

	/* Handle graph state */
	free_callee_info(irg);
	free_irg_outs(irg);
	free_loop_information(irg);
89
	free_vrp_data(irg);
90
	clear_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE);
91
92
93

	/* A quiet place, where the old obstack can rest in peace,
	   until it will be cremated. */
94
	struct obstack graveyard_obst = irg->obst;
95
96

	/* A new obstack, where the reachable nodes will be copied to. */
97
	obstack_init(&irg->obst);
98
99
100
	irg->last_node_idx = 0;

	/* We also need a new value table for CSE */
101
	new_identities(irg);
102
103
104
105
106

	/* Copy the graph from the old to the new obstack */
	copy_graph_env(irg);

	/* Free memory from old unoptimized obstack */
107
	obstack_free(&graveyard_obst, 0);  /* First empty the obstack ... */
108
109
110
111

	/* inform statistics that the run is over */
	hook_dead_node_elim(irg, 0);
}