besched.c 5.01 KB
Newer Older
Christian Würdig's avatar
Christian Würdig committed
1
/*
Michael Beck's avatar
Michael Beck committed
2
 * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
Christian Würdig's avatar
Christian Würdig committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 *
 * 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.
 */

Christian Würdig's avatar
Christian Würdig committed
20
21
22
23
24
25
/**
 * @file
 * @brief       Scheduling utilities for nodes in Blocks and Blocks.
 * @author      Sebastian Hack
 * @version     $Id$
 */
Matthias Braun's avatar
Matthias Braun committed
26
#include "config.h"
27

28
#include <stdlib.h>
Sebastian Hack's avatar
Sebastian Hack committed
29

Sebastian Hack's avatar
Sebastian Hack committed
30
#include "impl.h"
31
32
#include "irprintf.h"
#include "irgwalk.h"
33
#include "firm_types.h"
Michael Beck's avatar
Michael Beck committed
34
#include "irgraph_t.h"
35
#include "iredges_t.h"
36
#include "ircons.h"
Christian Würdig's avatar
Christian Würdig committed
37
#include "irextbb.h"
38
#include "irgmod.h"
Sebastian Hack's avatar
Sebastian Hack committed
39
#include "debug.h"
40

41
#include "bemodule.h"
42
#include "bearch_t.h"
Sebastian Hack's avatar
Sebastian Hack committed
43
#include "besched_t.h"
Sebastian Hack's avatar
Sebastian Hack committed
44
#include "beutil.h"
45
46
#include "belistsched.h"

47
FIRM_IMPL1(have_sched_info, int, const ir_graph *)
Sebastian Hack's avatar
Sebastian Hack committed
48
FIRM_IMPL1(sched_get_time_step, int, const ir_node *)
49
FIRM_IMPL1(sched_has_next, int, const ir_node *)
Sebastian Hack's avatar
Sebastian Hack committed
50
FIRM_IMPL1(sched_has_prev, int, const ir_node *)
51
FIRM_IMPL1(sched_next, ir_node *, const ir_node *)
Sebastian Hack's avatar
Sebastian Hack committed
52
FIRM_IMPL1(sched_prev, ir_node *, const ir_node *)
53
FIRM_IMPL1(sched_is_scheduled, int, const ir_node *)
Sebastian Hack's avatar
Sebastian Hack committed
54
55
FIRM_IMPL1(sched_first, ir_node *, const ir_node *)
FIRM_IMPL1(sched_last, ir_node *, const ir_node *)
56
57
58
59
FIRM_IMPL2_VOID(sched_add_after, const ir_node *, const ir_node *)
FIRM_IMPL2_VOID(sched_add_before, const ir_node *, const ir_node *)
FIRM_IMPL1_VOID(sched_init_block, const ir_node *)
FIRM_IMPL1_VOID(sched_reset, const ir_node *)
Michael Beck's avatar
Michael Beck committed
60
FIRM_IMPL2(sched_comes_after, int, const ir_node *, const ir_node *)
61
FIRM_IMPL1_VOID(sched_remove, const ir_node *)
Sebastian Hack's avatar
Sebastian Hack committed
62

63
64
65
66
size_t sched_irn_data_offset = 0;

static void block_sched_dumper(ir_node *block, void *env)
{
67
	FILE  *f = env;
68
69
	const ir_node *curr;

70
	ir_fprintf(f, "%+F:\n", block);
71

72
	sched_foreach(block, curr) {
73
		sched_info_t *info = get_irn_sched_info(curr);
74
		ir_fprintf(f, "\t%6d: %+F\n", info->time_step, curr);
75
76
77
	}
}

Michael Beck's avatar
Michael Beck committed
78
void be_sched_dump(FILE *f, ir_graph *irg)
79
{
Michael Beck's avatar
Michael Beck committed
80
	irg_block_walk_graph(irg, block_sched_dumper, NULL, f);
81
82
}

Michael Beck's avatar
Michael Beck committed
83
/* Init the scheduling stuff. */
84
void be_init_sched(void)
85
{
86
	sched_irn_data_offset = firm_register_additional_node_data(sizeof(sched_info_t));
87
88
}

89
90
BE_REGISTER_MODULE_CONSTRUCTOR(be_init_sched);

Sebastian Hack's avatar
Sebastian Hack committed
91
92
void sched_renumber(const ir_node *block)
{
Michael Beck's avatar
BugFix:    
Michael Beck committed
93
94
	ir_node *irn;
	sched_info_t *inf;
95
	sched_timestep_t step = SCHED_INITIAL_GRANULARITY;
Michael Beck's avatar
BugFix:    
Michael Beck committed
96
97
98
99
100
101

	sched_foreach(block, irn) {
		inf = get_irn_sched_info(irn);
		inf->time_step = step;
		step += SCHED_INITIAL_GRANULARITY;
	}
Sebastian Hack's avatar
Sebastian Hack committed
102
103
}

104
105
106
107
int sched_skip_cf_predicator(const ir_node *irn, void *data)
{
	(void)data;
	return arch_irn_class_is(irn, branch);
Daniel Grund's avatar
Daniel Grund committed
108
109
110
}

int sched_skip_phi_predicator(const ir_node *irn, void *data) {
Matthias Braun's avatar
Matthias Braun committed
111
	(void) data;
Daniel Grund's avatar
Daniel Grund committed
112
113
114
	return is_Phi(irn);
}

115
/* Skip nodes in a schedule. */
Michael Beck's avatar
Michael Beck committed
116
ir_node *sched_skip(ir_node *from, int forward, sched_predicator_t *predicator, void *data)
Sebastian Hack's avatar
Sebastian Hack committed
117
{
118
	const ir_node *bl = get_block_const(from);
Daniel Grund's avatar
Daniel Grund committed
119
	ir_node *curr;
Sebastian Hack's avatar
Sebastian Hack committed
120

121
122
123
124
125
126
127
128
129
130
131
	if (forward) {
		if (is_Block(from))
			from = sched_next(from);
		for (curr = from; curr != bl && predicator(curr, data); curr = sched_next(curr)) {
		}
	} else {
		if (is_Block(from))
			from = sched_prev(from);
		for (curr = from; curr != bl && predicator(curr, data); curr = sched_prev(curr)) {
		}
	}
Daniel Grund's avatar
Daniel Grund committed
132
133

	return curr;
Sebastian Hack's avatar
Sebastian Hack committed
134
}
135

136
//---------------------------------------------------------------------------
137
138
139

typedef struct remove_dead_nodes_env_t_ {
	bitset_t *reachable;
140
141
	ir_graph *irg;
	be_lv_t  *lv;
142
143
} remove_dead_nodes_env_t;

144
145
146
/**
 * Post-walker: remember all visited nodes in a bitset.
 */
147
148
149
150
151
152
static void mark_dead_nodes_walker(ir_node *node, void *data)
{
	remove_dead_nodes_env_t *env = (remove_dead_nodes_env_t*) data;
	bitset_set(env->reachable, get_irn_idx(node));
}

153
154
155
156
/**
 * Post-block-walker:
 * Walk through the schedule of every block and remove all dead nodes from it.
 */
157
158
159
static void remove_dead_nodes_walker(ir_node *block, void *data)
{
	remove_dead_nodes_env_t *env = (remove_dead_nodes_env_t*) data;
160
	ir_node                 *node, *next;
161

162
163
	for (node = sched_first(block); ! sched_is_end(node); node = next) {
		/* get next node now, as after calling sched_remove it will be invalid */
Matthias Braun's avatar
Matthias Braun committed
164
165
		next = sched_next(node);

166
		if (bitset_is_set(env->reachable, get_irn_idx(node)))
167
168
			continue;

169
170
		if(env->lv)
			be_liveness_remove(env->lv, node);
Matthias Braun's avatar
Matthias Braun committed
171
		sched_remove(node);
172
		kill_node(node);
173
174
175
	}
}

176
void be_remove_dead_nodes_from_schedule(be_irg_t *birg)
177
{
178
179
	ir_graph *irg = be_get_birg_irg(birg);

180
181
	remove_dead_nodes_env_t env;
	env.reachable = bitset_alloca(get_irg_last_idx(irg));
182
183
	env.lv  = be_get_birg_liveness(birg);
	env.irg = irg;
184
185
186
187
188
189
190

	// mark all reachable nodes
	irg_walk_graph(irg, mark_dead_nodes_walker, NULL, &env);

	// walk schedule and remove non-marked nodes
	irg_block_walk_graph(irg, remove_dead_nodes_walker, NULL, &env);
}