benode.c 9.52 KB
Newer Older
Sebastian Hack's avatar
Sebastian Hack committed
1
2
3
4
5
6
7
8
9
10
/**
 * @file   benode.c
 * @date   17.05.2005
 * @author Sebastian Hack
 *
 * Backend node support.
 *
 * Copyright (C) 2005 Universitaet Karlsruhe
 * Released under the GPL
 */
Michael Beck's avatar
Michael Beck committed
11
12
13
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
Sebastian Hack's avatar
Sebastian Hack committed
14
15
16
17
18
19

#include <stdlib.h>

#include "obst.h"
#include "set.h"
#include "pmap.h"
20
#include "util.h"
Sebastian Hack's avatar
Sebastian Hack committed
21
22
23
24

#include "irop_t.h"
#include "irmode_t.h"
#include "irnode_t.h"
25
#include "ircons_t.h"
Sebastian Hack's avatar
Sebastian Hack committed
26

27
28
29
#include "be_t.h"
#include "belive_t.h"
#include "besched_t.h"
Sebastian Hack's avatar
Sebastian Hack committed
30
31
#include "benode_t.h"

32
33
#include "beirgmod.h"

Sebastian Hack's avatar
Sebastian Hack committed
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
typedef enum _node_kind_t {
  node_kind_spill,
  node_kind_reload,
  node_kind_perm,
  node_kind_copy,
  node_kind_last
} node_kind_t;

typedef struct {
  node_kind_t kind;
  const arch_register_class_t *cls;
  ir_op *op;
  int n_pos;
  int *pos;
} be_op_t;

static int templ_pos_Spill[] = {
  arch_pos_make_in(0)
};

static int templ_pos_Reload[] = {
  arch_pos_make_out(0)
};

static int templ_pos_Copy[] = {
  arch_pos_make_in(0),
  arch_pos_make_out(0)
};

#define ARRSIZE(x) (sizeof(x) / sizeof(x[0]))

static int cmp_op_map(const void *a, const void *b, size_t size)
{
  const be_op_t *x = a;
  const be_op_t *y = b;

  return !(x->kind == y->kind && x->cls == y->cls);
}

static be_op_t *get_op(const be_node_factory_t *fact,
    const arch_register_class_t *cls, node_kind_t kind)
{
  be_op_t templ;

  templ.kind = kind;
  templ.cls = cls;

  return set_insert(fact->ops, &templ, sizeof(templ),
      HASH_PTR(cls) + 7 * kind);
}

ir_node *new_Spill(const be_node_factory_t *factory,
    const arch_register_class_t *cls,
    ir_graph *irg, ir_node *bl, ir_node *node_to_spill)
{
  ir_node *in[1];
  ir_op *op = get_op(factory, cls, node_kind_spill)->op;

  assert(op && "Spill opcode must be present for this register class");
  in[0] = node_to_spill;

  return new_ir_node(NULL, irg, bl, op, mode_M, 1, in);
}

ir_node *new_Reload(const be_node_factory_t *factory,
    const arch_register_class_t *cls,
    ir_graph *irg, ir_node *bl, ir_node *spill_node)
{
  ir_mode *mode;
  ir_node *in[1];
  ir_op *op = get_op(factory, cls, node_kind_reload)->op;

  assert(op && "Reload opcode must be present for this register class");
  assert(is_Spill(factory, spill_node) && "Operand of Reload must be a Spill");
  in[0] = spill_node;
  mode = get_irn_mode(get_irn_n(spill_node, 0));

  return new_ir_node(NULL, irg, bl, op, mode, 1, in);
}

ir_node *new_Perm(const be_node_factory_t *factory,
    const arch_register_class_t *cls,
    ir_graph *irg, ir_node *bl, int arity, ir_node **in)
{
  ir_op *op = get_op(factory, cls, node_kind_perm)->op;

  return new_ir_node(NULL, irg, bl, op, mode_T, arity, in);
}

ir_node *new_Copy(const be_node_factory_t *factory,
    const arch_register_class_t *cls,
    ir_graph *irg, ir_node *bl, ir_node *in)
{
  ir_node *ins[1];
  ir_op *op = get_op(factory, cls, node_kind_perm)->op;

  ins[0] = in;

  return new_ir_node(NULL, irg, bl, op, get_irn_mode(in), 1, ins);
}

/**
 * If the node is a proj, reset the node to the proj's target and return
 * the proj number.
 * @param node The address of a node pointer.
 * @param def  A default value.
 * @return     If *node is a Proj, *node is set to the Proj's target and
 *             the Proj number is returned. Else *node remains the same and @p def
 *             is returned.
 */
static int redir_proj(const ir_node **node, int def)
{
  const ir_node *n = *node;

  if(is_Proj(n)) {
    *node = get_Proj_pred(n);
    def = get_Proj_proj(n);
  }

  return def;
}

static const arch_register_req_t *
be_node_get_irn_reg_req(const arch_irn_ops_t *_self,
    arch_register_req_t *req, const ir_node *irn, int pos)
{
  be_op_t *bo;
  const be_node_factory_t *factory =
    container_of(_self, const be_node_factory_t, irn_ops);

  pos = arch_pos_make_out(redir_proj(&irn, arch_pos_get_index(pos)));

  bo = pmap_get(factory->irn_op_map, get_irn_op(irn));

  if(bo) {
    int i;

    req->type = arch_register_req_type_normal;
    req->cls = bo->cls;

    for(i = 0; i < bo->n_pos; ++pos)
      if(pos == bo->pos[i])
        return req;
  }

  return NULL;
}

static int
be_node_get_n_operands(const arch_irn_ops_t *_self, const ir_node *irn, int in_out)
{
  be_op_t *bo;
  int i, res = 0;
  const be_node_factory_t *factory =
    container_of(_self, const be_node_factory_t, irn_ops);

  redir_proj(&irn, 0);
  bo = pmap_get(factory->irn_op_map, get_irn_op(irn));

  for(i = 0; i < bo->n_pos; ++i)
    res += (bo->pos[i] ^ in_out) > 0;

  return res;
}

void
be_node_set_irn_reg(const arch_irn_ops_t *_self, ir_node *irn,
    int idx, const arch_register_t *reg)
{
  const arch_register_t **regs;

  idx = redir_proj((const ir_node **) &irn, idx);
  regs = (const arch_register_t **) &irn->attr;
  regs[idx] = reg;
}

const arch_register_t *
be_node_get_irn_reg(const arch_irn_ops_t *_self, const ir_node *irn, int idx)
{
  const arch_register_t **regs;

  idx = redir_proj(&irn, idx);
  regs = (const arch_register_t **) &irn->attr;
  return regs[idx];
}

arch_irn_class_t be_node_classify(const arch_irn_ops_t *_self, const ir_node *irn)
{
  const be_node_factory_t *factory =
    container_of(_self, const be_node_factory_t, irn_ops);

  be_op_t *bo;
  int idx;

  idx = redir_proj(&irn, 0);
  bo = pmap_get(factory->irn_op_map, get_irn_op(irn));

  return 0;
}

static const arch_irn_ops_t *
be_node_get_irn_ops(const arch_irn_handler_t *_self, const ir_node *irn)
{
  be_op_t *bo;
  const be_node_factory_t *factory =
    container_of(_self, const be_node_factory_t, handler);

  redir_proj(&irn, 0);
  bo = pmap_get(factory->irn_op_map, get_irn_op(irn));

  return bo ? &factory->irn_ops : NULL;
}

const arch_irn_handler_t *be_node_get_irn_handler(const be_node_factory_t *f)
{
  return &f->handler;
}

int is_Spill(const be_node_factory_t *f, const ir_node *irn)
{
  be_op_t *bo;
  bo = pmap_get(f->irn_op_map, get_irn_op(irn));
  return bo->kind == node_kind_spill;
}

259
260
be_node_factory_t *be_node_factory_init(be_node_factory_t *factory,
    const arch_isa_if_t *isa)
Sebastian Hack's avatar
Sebastian Hack committed
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
{
  char buf[256];
  int i, j, n;

  factory->ops = new_set(cmp_op_map, 64);
  factory->irn_op_map = pmap_create();
  obstack_init(&factory->obst);

  factory->handler.get_irn_ops = be_node_get_irn_ops;

  factory->irn_ops.get_irn_reg_req = be_node_get_irn_reg_req;
  factory->irn_ops.get_n_operands  = be_node_get_n_operands;
  factory->irn_ops.set_irn_reg     = be_node_set_irn_reg;
  factory->irn_ops.get_irn_reg     = be_node_get_irn_reg;
  factory->irn_ops.classify        = be_node_classify;

  for(i = 0, n = isa->get_n_reg_class(); i < n; ++i) {
    const arch_register_class_t *cls = isa->get_reg_class(i);
    be_op_t *ent;

    ent = get_op(factory, cls, node_kind_spill);
    snprintf(buf, sizeof(buf), "Spill_%s", cls->name);
    ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned,
        0, 0, oparity_unary, 0);
    ent->n_pos = ARRSIZE(templ_pos_Spill);
    ent->pos = templ_pos_Spill;
    pmap_insert(factory->irn_op_map, ent->op, ent);

    ent = get_op(factory, cls, node_kind_reload);
    snprintf(buf, sizeof(buf), "Reload_%s", cls->name);
    ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned, 0, 0,
        oparity_unary, sizeof(const arch_register_t *));
    ent->n_pos = ARRSIZE(templ_pos_Reload);
    ent->pos = templ_pos_Reload;
    pmap_insert(factory->irn_op_map, ent->op, ent);

    ent = get_op(factory, cls, node_kind_copy);
    snprintf(buf, sizeof(buf), "Copy_%s", cls->name);
    ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned, 0, 0,
        oparity_unary, sizeof(const arch_register_t *));
    ent->n_pos = ARRSIZE(templ_pos_Copy);
    ent->pos = templ_pos_Copy;
    pmap_insert(factory->irn_op_map, ent->op, ent);

    ent = get_op(factory, cls, node_kind_perm);
    snprintf(buf, sizeof(buf), "Perm_%s", cls->name);
    ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned, 0, 0,
        oparity_variable, sizeof(const arch_register_t) * cls->n_regs);
    ent->n_pos = 2 * cls->n_regs;
    ent->pos = obstack_alloc(&factory->obst, sizeof(ent->pos[0]) * ent->n_pos);
    for(j = 0; j < ent->n_pos; j += 2) {
      ent->pos[j] = arch_pos_make_in(j);
      ent->pos[j + 1] = arch_pos_make_out(j);
    }
    pmap_insert(factory->irn_op_map, ent->op, ent);

  }

  return factory;
}

322
ir_node *insert_Perm_after(const be_main_session_env_t *env,
323
    const arch_register_class_t *cls, ir_node *pos)
Sebastian Hack's avatar
Sebastian Hack committed
324
{
325
326
327
  const arch_env_t *arch_env = env->main_env->arch_env;
  ir_node *bl = is_Block(pos) ? pos : get_nodes_block(pos);
  ir_graph *irg = get_irn_irg(bl);
Sebastian Hack's avatar
Sebastian Hack committed
328
  pset *live = put_live_end(bl, pset_new_ptr_default());
329
330
331
  ir_node *curr, *irn, *perm, **nodes;
  int i, n;

Sebastian Hack's avatar
Sebastian Hack committed
332
  ir_printf("Insert Perm after: %+F\n", pos);
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347

  sched_foreach_reverse(bl, irn) {

    if(arch_irn_has_reg_class(arch_env, irn, arch_pos_make_out(0), cls))
      pset_remove_ptr(live, irn);

    for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
      ir_node *op = get_irn_n(irn, i);

      if(arch_irn_has_reg_class(arch_env, op, arch_pos_make_out(0), cls))
        pset_insert_ptr(live, op);
    }

    if(sched_prev(irn) == pos)
      break;
Sebastian Hack's avatar
Sebastian Hack committed
348
349
  }

350
351
  n = pset_count(live);
  nodes = malloc(n * sizeof(nodes[0]));
Sebastian Hack's avatar
Sebastian Hack committed
352

353
354
355
  for(irn = pset_first(live), i = 0; irn; irn = pset_next(live), i++)
    nodes[i] = irn;

Sebastian Hack's avatar
Sebastian Hack committed
356
  perm = new_Perm(env->main_env->node_factory, cls, irg, bl, n, nodes);
357
358
359
  sched_add_after(pos, perm);
  free(nodes);

Sebastian Hack's avatar
Sebastian Hack committed
360
  curr = perm;
361
362
363
364
365
366
367
368
369
370
371
372
  for(i = 0; i < n; ++i) {
    ir_node *copies[1];
    ir_node *perm_op = get_irn_n(perm, i);

    ir_mode *mode = get_irn_mode(perm_op);
    ir_node *proj = new_r_Proj(irg, bl, perm, mode, i);
    sched_add_after(curr, proj);
    curr = proj;

    copies[0] = proj;
    be_introduce_copies(env->dom_front, perm_op, array_size(copies), copies);
  }
373
  return perm;
Sebastian Hack's avatar
Sebastian Hack committed
374
}