summaryrefslogtreecommitdiffhomepage
path: root/ir/be/bearch.c
blob: 1fa3c1eded36725dd15d8a9919d1292d07f6f7be (plain)
1
2
3
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
 * This file is part of libFirm.
 * Copyright (C) 2012 University of Karlsruhe.
 */

/**
 * @file
 * @brief       Processor architecture specification.
 * @author      Sebastian Hack
 */
#include "bearch.h"

#include "be_t.h"
#include "beinfo.h"
#include "beirg.h"
#include "bemodule.h"
#include "benode.h"
#include "ircons_t.h"
#include "irnode_t.h"
#include "irop_t.h"
#include "raw_bitset.h"
#include "target_t.h"
#include "util.h"

arch_register_class_t arch_exec_cls = {
	.name      = "exec",
	.mode      = NULL, /* Filled in at initialization. */
	.regs      = NULL,
	.class_req = arch_exec_req,
	.index     = (unsigned)-1,
	.n_regs    = 0,
	.manual_ra = true,
};

arch_register_req_t const arch_exec_requirement = {
	.cls = &arch_exec_cls,
};

static arch_register_class_t arch_memory_cls = {
	.name      = "memory",
	.mode      = NULL, /* Filled in at initialization. */
	.regs      = NULL,
	.class_req = arch_memory_req,
	.index     = (unsigned)-1,
	.n_regs    = 0,
	.manual_ra = true,
};

arch_register_req_t const arch_memory_requirement = {
	.cls = &arch_memory_cls,
};

static arch_register_class_t arch_none_cls = {
	.name      = "none",
	.mode      = NULL,
	.regs      = NULL,
	.class_req = arch_no_register_req,
	.index     = (unsigned)-1,
	.n_regs    = 0,
	.manual_ra = true,
};

arch_register_req_t const arch_no_requirement = {
	.cls = &arch_none_cls,
};

const arch_register_t *arch_get_irn_register(const ir_node *node)
{
	const reg_out_info_t *out = get_out_info(node);
	return out->reg;
}

const arch_register_t *arch_get_irn_register_out(const ir_node *node,
                                                 unsigned pos)
{
	const reg_out_info_t *out = get_out_info_n(node, pos);
	return out->reg;
}

const arch_register_t *arch_get_irn_register_in(const ir_node *node, int pos)
{
	ir_node *op = get_irn_n(node, pos);
	return arch_get_irn_register(op);
}

static inline void set_out_info_reg(reg_out_info_t *const out, arch_register_t const *const reg)
{
	assert(arch_reg_is_allocatable(out->req, reg));
	out->reg = reg;
}

void arch_set_irn_register_out(ir_node *node, unsigned pos,
                               const arch_register_t *reg)
{
	reg_out_info_t *out = get_out_info_n(node, pos);
	set_out_info_reg(out, reg);
}

void arch_set_irn_register(ir_node *node, const arch_register_t *reg)
{
	reg_out_info_t *out = get_out_info(node);
	set_out_info_reg(out, reg);
}

bool arch_reg_is_allocatable(const arch_register_req_t *req,
                             const arch_register_t *reg)
{
	if (req->cls != reg->cls)
		return false;
	if (reg->is_virtual)
		return true;
	if (req->limited != NULL)
		return rbitset_is_set(req->limited, reg->index);
	return true;
}

arch_register_t const *arch_find_register(char const *const name)
{
	arch_register_t const *const regs = ir_target.isa->registers;
	for (size_t i = 0, n = ir_target.isa->n_registers; i < n; ++i) {
		arch_register_t const *const reg = &regs[i];
		if (streq(reg->name, name))
			return reg;
	}
	return NULL;
}

void arch_set_additional_pressure(ir_node *const node,
                                  arch_register_class_t const *const cls,
                                  be_add_pressure_t const pressure)
{
	backend_info_t *const info = be_get_info(node);
	/* restricted to the first few classes for now */
	unsigned index = cls->index;
	assert(index < ARRAY_SIZE(info->add_pressure));
	info->add_pressure[index] = pressure;
}

be_add_pressure_t arch_get_additional_pressure(ir_node const *const node, arch_register_class_t const *const cls)
{
	backend_info_t *const info = be_get_info(node);
	/* restricted to the first few classes for now */
	unsigned index = cls->index;
	if (index < ARRAY_SIZE(info->add_pressure))
		return info->add_pressure[index];
	return 0;
}

void arch_copy_irn_out_info(ir_node *const dst, unsigned const dst_pos, ir_node const *const src)
{
	reg_out_info_t *const src_info = get_out_info(src);
	reg_out_info_t *const dst_info = get_out_info_n(dst, dst_pos);
	*dst_info = *src_info;
}

arch_register_req_t *be_create_cls_req(ir_graph *const irg, arch_register_class_t const *const cls, unsigned char const width)
{
	struct obstack      *const obst = be_get_be_obst(irg);
	arch_register_req_t *const req  = OALLOCZ(obst, arch_register_req_t);
	req->cls   = cls;
	req->width = width;
	return req;
}

arch_register_req_t const *be_create_reg_req(ir_graph *const irg, arch_register_t const *const reg, bool const ignore)
{
	if (!ignore)
		return reg->single_req;

	struct obstack              *const obst    = be_get_be_obst(irg);
	arch_register_class_t const *const cls     = reg->cls;
	unsigned                    *const limited = rbitset_obstack_alloc(obst, cls->n_regs);
	rbitset_set(limited, reg->index);
	arch_register_req_t *const req = OALLOCZ(obst, arch_register_req_t);
	req->cls     = cls;
	req->limited = limited;
	req->width   = 1;
	req->ignore  = ignore;
	return req;
}

int be_get_input_pos_for_req(ir_node const *const irn, arch_register_req_t const *const req)
{
	for (int i = 0, n = get_irn_arity(irn); i != n; ++i) {
		if (arch_get_irn_register_req_in(irn, i) == req)
			return i;
	}
	return -1;
}

BE_REGISTER_MODULE_CONSTRUCTOR(be_init_arch)
void be_init_arch(void)
{
	arch_exec_cls.mode   = mode_X;
	arch_memory_cls.mode = mode_M;
}