besched.c 4.69 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
/* $Id$ */

Michael Beck's avatar
Michael Beck committed
22
#ifdef HAVE_CONFIG_H
Michael Beck's avatar
Michael Beck committed
23
# include "config.h"
Michael Beck's avatar
Michael Beck committed
24
#endif
25

Michael Beck's avatar
Michael Beck committed
26
27
28
#ifdef HAVE_STDLIB_H
# include <stdlib.h>
#endif
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"
Sebastian Hack's avatar
Sebastian Hack committed
38
#include "debug.h"
39

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

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

61
62
63
64
size_t sched_irn_data_offset = 0;

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

68
	ir_fprintf(f, "%+F:\n", block);
69

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

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

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

87
88
BE_REGISTER_MODULE_CONSTRUCTOR(be_init_sched);

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

	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
100
101
}

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

int sched_skip_phi_predicator(const ir_node *irn, void *data) {
	return is_Phi(irn);
}

111
/* Skip nodes in a schedule. */
Michael Beck's avatar
Michael Beck committed
112
ir_node *sched_skip(ir_node *from, int forward, sched_predicator_t *predicator, void *data)
Sebastian Hack's avatar
Sebastian Hack committed
113
{
Daniel Grund's avatar
Daniel Grund committed
114
115
	const ir_node *bl = get_block(from);
	ir_node *curr;
Sebastian Hack's avatar
Sebastian Hack committed
116

117
118
119
120
121
122
123
124
125
126
127
	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
128
129

	return curr;
Sebastian Hack's avatar
Sebastian Hack committed
130
}
131

132
//---------------------------------------------------------------------------
133
134
135
136
137
138

typedef struct remove_dead_nodes_env_t_ {
	ir_graph *irg;
	bitset_t *reachable;
} remove_dead_nodes_env_t;

139
140
141
/**
 * Post-walker: remember all visited nodes in a bitset.
 */
142
143
144
145
146
147
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));
}

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

157
158
	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
159
160
		next = sched_next(node);

161
		if (bitset_is_set(env->reachable, get_irn_idx(node)))
162
163
			continue;

Matthias Braun's avatar
Matthias Braun committed
164
		sched_remove(node);
165
		be_kill_node(node);
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
	}
}

void be_remove_dead_nodes_from_schedule(ir_graph *irg)
{
	remove_dead_nodes_env_t env;
	env.irg = irg;
	env.reachable = bitset_alloca(get_irg_last_idx(irg));

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