besched.c 4.96 KB
Newer Older
Christian Würdig's avatar
Christian Würdig committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*
 * Copyright (C) 1995-2007 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.
 */

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$
 */
Michael Beck's avatar
Michael Beck committed
26
#ifdef HAVE_CONFIG_H
Michael Beck's avatar
Michael Beck committed
27
# include "config.h"
Michael Beck's avatar
Michael Beck committed
28
#endif
29

Michael Beck's avatar
Michael Beck committed
30
31
32
#ifdef HAVE_STDLIB_H
# include <stdlib.h>
#endif
Sebastian Hack's avatar
Sebastian Hack committed
33

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

44
#include "bemodule.h"
45
#include "bearch_t.h"
Sebastian Hack's avatar
Sebastian Hack committed
46
#include "besched_t.h"
Sebastian Hack's avatar
Sebastian Hack committed
47
#include "beutil.h"
48
49
#include "belistsched.h"

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

65
66
67
68
size_t sched_irn_data_offset = 0;

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

72
	ir_fprintf(f, "%+F:\n", block);
73

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

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

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

91
92
BE_REGISTER_MODULE_CONSTRUCTOR(be_init_sched);

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

	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
104
105
}

Daniel Grund's avatar
Daniel Grund committed
106
int sched_skip_cf_predicator(const ir_node *irn, void *data) {
Michael Beck's avatar
BugFix:    
Michael Beck committed
107
	arch_env_t *ae = data;
Christian Würdig's avatar
Christian Würdig committed
108
	return arch_irn_class_is(ae, irn, branch);
Daniel Grund's avatar
Daniel Grund committed
109
110
111
}

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

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

122
123
124
125
126
127
128
129
130
131
132
	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
133
134

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

137
//---------------------------------------------------------------------------
138
139
140

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

145
146
147
/**
 * Post-walker: remember all visited nodes in a bitset.
 */
148
149
150
151
152
153
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));
}

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

163
164
	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
165
166
		next = sched_next(node);

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

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

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

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

	// 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);
}