4 * @author Sebastian Hack
6 * Backend node support.
8 * This file provdies Perm, Copy, Spill and Reload nodes.
10 * Copyright (C) 2005 Universitaet Karlsruhe
11 * Released under the GPL
35 #include "besched_t.h"
40 /* Sometimes we want to put const nodes into get_irn_generic_attr ... */
41 #define get_irn_attr(irn) get_irn_generic_attr((ir_node *) (irn))
43 static unsigned be_node_tag = FOURCC('B', 'E', 'N', 'O');
45 typedef enum _node_kind_t {
56 const arch_register_class_t *cls;
63 arch_register_req_t req;
64 unsigned negate_limited : 1;
65 void (*old_limited)(void *ptr, bitset_t *bs);
66 void *old_limited_env;
70 const arch_register_t *reg;
76 const arch_register_class_t *cls;
77 be_reg_data_t *reg_data;
81 be_node_attr_t node_attr;
82 ir_node *spill_ctx; /**< The node in whose context this spill was introduced. */
83 entity *ent; /**< The entity in the stack frame the spill writes to. */
86 static ir_op *op_Spill;
87 static ir_op *op_Reload;
88 static ir_op *op_Perm;
89 static ir_op *op_Copy;
90 static ir_op *op_Keep;
92 static int beo_base = -1;
94 static const ir_op_ops be_node_op_ops;
96 #define N irop_flag_none
97 #define L irop_flag_labeled
98 #define C irop_flag_commutative
99 #define X irop_flag_cfopcode
100 #define I irop_flag_ip_cfopcode
101 #define F irop_flag_fragile
102 #define Y irop_flag_forking
103 #define H irop_flag_highlevel
104 #define c irop_flag_constlike
105 #define K irop_flag_keep
107 void be_node_init(void) {
108 static int inited = 0;
115 /* Acquire all needed opcodes. */
116 beo_base = get_next_ir_opcodes(beo_Last - 1);
118 op_Spill = new_ir_op(beo_base + beo_Spill, "Spill", op_pin_state_mem_pinned, N, oparity_unary, 0, sizeof(be_spill_attr_t), &be_node_op_ops);
119 op_Reload = new_ir_op(beo_base + beo_Reload, "Reload", op_pin_state_mem_pinned, N, oparity_zero, 0, sizeof(be_node_attr_t), &be_node_op_ops);
120 op_Perm = new_ir_op(beo_base + beo_Perm, "Perm", op_pin_state_pinned, N, oparity_variable, 0, sizeof(be_node_attr_t), &be_node_op_ops);
121 op_Copy = new_ir_op(beo_base + beo_Copy, "Copy", op_pin_state_pinned, N, oparity_unary, 0, sizeof(be_node_attr_t), &be_node_op_ops);
122 op_Keep = new_ir_op(beo_base + beo_Keep, "Keep", op_pin_state_pinned, K, oparity_variable, 0, sizeof(be_node_attr_t), &be_node_op_ops);
124 set_op_tag(op_Spill, &be_node_tag);
125 set_op_tag(op_Reload, &be_node_tag);
126 set_op_tag(op_Perm, &be_node_tag);
127 set_op_tag(op_Copy, &be_node_tag);
128 set_op_tag(op_Keep, &be_node_tag);
131 static void *init_node_attr(ir_node* irn, const arch_register_class_t *cls, ir_graph *irg, int n_outs)
133 be_node_attr_t *a = get_irn_attr(irn);
142 a->reg_data = NEW_ARR_D(be_reg_data_t, get_irg_obstack(irg), n_outs);
143 memset(a->reg_data, 0, n_outs * sizeof(a->reg_data[0]));
144 for(i = 0; i < n_outs; ++i) {
145 a->reg_data[i].req.req.cls = cls;
146 a->reg_data[i].req.req.type = arch_register_req_type_normal;
153 static INLINE int is_be_node(const ir_node *irn)
155 return get_op_tag(get_irn_op(irn)) == &be_node_tag;
158 be_opcode_t get_irn_be_opcode(const ir_node *irn)
160 return is_be_node(irn) ? get_irn_opcode(irn) - beo_base : beo_NoBeOp;
163 ir_node *be_new_Spill(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, ir_node *to_spill, ir_node *ctx)
170 res = new_ir_node(NULL, irg, bl, op_Spill, mode_M, 1, in);
171 a = init_node_attr(res, cls, irg, 0);
177 ir_node *be_new_Reload(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, ir_mode *mode, ir_node *mem)
183 res = new_ir_node(NULL, irg, bl, op_Reload, mode, 1, in);
184 init_node_attr(res, cls, irg, 1);
188 ir_node *be_new_Perm(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, int n, ir_node *in[])
190 ir_node *irn = new_ir_node(NULL, irg, bl, op_Perm, mode_T, n, in);
191 init_node_attr(irn, cls, irg, n);
195 ir_node *be_new_Copy(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, ir_node *op)
201 res = new_ir_node(NULL, irg, bl, op_Copy, get_irn_mode(op), 1, in);
202 init_node_attr(res, cls, irg, 1);
206 ir_node *be_new_Keep(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, int n, ir_node *in[])
210 irn = new_ir_node(NULL, irg, bl, op_Keep, mode_ANY, n, in);
211 init_node_attr(irn, cls, irg, 0);
216 int be_is_Spill(const ir_node *irn)
218 return get_irn_be_opcode(irn) == beo_Spill;
221 int be_is_Reload(const ir_node *irn)
223 return get_irn_be_opcode(irn) == beo_Reload;
226 int be_is_Copy(const ir_node *irn)
228 return get_irn_be_opcode(irn) == beo_Copy;
231 int be_is_Perm(const ir_node *irn)
233 return get_irn_be_opcode(irn) == beo_Perm;
236 int be_is_Keep(const ir_node *irn)
238 return get_irn_be_opcode(irn) == beo_Keep;
241 static void be_limited(void *data, bitset_t *bs)
243 be_req_t *req = data;
245 req->old_limited(req->old_limited_env, bs);
246 if(req->negate_limited)
250 void be_set_Perm_out_req(ir_node *irn, int pos, const arch_register_req_t *req, unsigned negate_limited)
252 be_node_attr_t *a = get_irn_attr(irn);
253 be_req_t *r = &a->reg_data[pos].req;
255 assert(be_is_Perm(irn));
256 assert(pos >= 0 && pos < get_irn_arity(irn));
257 memcpy(&r->req, req, sizeof(req[0]));
259 if(arch_register_req_is(req, limited)) {
260 r->old_limited = r->req.limited;
261 r->old_limited_env = r->req.limited_env;
262 r->req.limited = be_limited;
263 r->req.limited_env = r;
264 r->negate_limited = negate_limited;
268 void be_set_Spill_entity(ir_node *irn, entity *ent)
270 be_spill_attr_t *a = get_irn_attr(irn);
271 assert(be_is_Spill(irn));
275 static ir_node *find_a_spill_walker(ir_node *irn, unsigned visited_nr)
277 if(get_irn_visited(irn) < visited_nr) {
278 set_irn_visited(irn, visited_nr);
282 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
283 ir_node *n = find_a_spill_walker(get_irn_n(irn, i), visited_nr);
289 else if(get_irn_be_opcode(irn) == beo_Spill)
296 ir_node *be_get_Spill_context(const ir_node *irn) {
297 const be_spill_attr_t *a = get_irn_attr(irn);
298 assert(be_is_Spill(irn));
303 * Finds a spill for a reload.
304 * If the reload is directly using the spill, this is simple,
305 * else we perform DFS from the reload (over all PhiMs) and return
306 * the first spill node we find.
308 static INLINE ir_node *find_a_spill(ir_node *irn)
310 ir_graph *irg = get_irn_irg(irn);
311 unsigned visited_nr = get_irg_visited(irg) + 1;
313 assert(be_is_Reload(irn));
314 set_irg_visited(irg, visited_nr);
315 return find_a_spill_walker(irn, visited_nr);
318 entity *be_get_spill_entity(ir_node *irn)
320 int opc = get_irn_opcode(irn);
322 switch(get_irn_be_opcode(irn)) {
324 return be_get_spill_entity(find_a_spill(irn));
327 be_spill_attr_t *a = get_irn_attr(irn);
331 assert(0 && "Must give spill/reload node");
337 ir_node *be_spill(const arch_env_t *arch_env, ir_node *irn, ir_node *ctx)
339 const arch_register_class_t *cls = arch_get_irn_reg_class(arch_env, irn, -1);
341 ir_node *bl = get_nodes_block(irn);
342 ir_graph *irg = get_irn_irg(bl);
343 ir_node *spill = be_new_Spill(cls, irg, bl, irn, ctx);
347 * search the right insertion point. a spill of a phi cannot be put
348 * directly after the phi, if there are some phis behind the one which
351 insert = sched_next(irn);
352 while(is_Phi(insert) && !sched_is_end(insert))
353 insert = sched_next(insert);
355 sched_add_before(insert, spill);
359 ir_node *be_reload(const arch_env_t *arch_env,
360 const arch_register_class_t *cls,
361 ir_node *irn, int pos, ir_mode *mode, ir_node *spill)
365 ir_node *bl = get_nodes_block(irn);
366 ir_graph *irg = get_irn_irg(bl);
368 assert(be_is_Spill(spill) || (is_Phi(spill) && get_irn_mode(spill) == mode_M));
370 reload = be_new_Reload(cls, irg, bl, mode, spill);
372 set_irn_n(irn, pos, reload);
373 sched_add_before(irn, reload);
377 static int redir_proj(const ir_node **node, int pos)
379 const ir_node *n = *node;
382 assert(pos == -1 && "Illegal pos for a Proj");
383 *node = get_Proj_pred(n);
384 return get_Proj_proj(n);
390 static void *put_out_reg_req(arch_register_req_t *req, const ir_node *irn, int out_pos)
392 const be_node_attr_t *a = get_irn_attr(irn);
395 if(out_pos < a->n_outs)
396 memcpy(req, &a->reg_data[out_pos].req, sizeof(req[0]));
398 req->type = arch_register_req_type_none;
405 static void *put_in_reg_req(arch_register_req_t *req, const ir_node *irn, int pos)
407 const be_node_attr_t *a = get_irn_attr(irn);
408 int n = get_irn_arity(irn);
410 req->type = arch_register_req_type_none;
413 switch(get_irn_be_opcode(irn)) {
419 req->type = arch_register_req_type_normal;
431 static const arch_register_req_t *
432 be_node_get_irn_reg_req(const void *self, arch_register_req_t *req, const ir_node *irn, int pos)
437 if(get_irn_mode(irn) == mode_T)
440 out_pos = redir_proj((const ir_node **) &irn, pos);
441 assert(is_be_node(irn));
442 return put_out_reg_req(req, irn, out_pos);
446 return is_be_node(irn) ? put_in_reg_req(req, irn, pos) : NULL;
453 be_node_set_irn_reg(const void *_self, ir_node *irn, const arch_register_t *reg)
458 out_pos = redir_proj((const ir_node **) &irn, -1);
459 a = get_irn_attr(irn);
461 assert(is_be_node(irn));
462 assert(out_pos < a->n_outs && "position too high");
463 a->reg_data[out_pos].reg = reg;
466 const arch_register_t *
467 be_node_get_irn_reg(const void *_self, const ir_node *irn)
472 out_pos = redir_proj((const ir_node **) &irn, -1);
473 a = get_irn_attr(irn);
475 assert(is_be_node(irn));
476 assert(out_pos < a->n_outs && "position too high");
478 return a->reg_data[out_pos].reg;
481 arch_irn_class_t be_node_classify(const void *_self, const ir_node *irn)
483 redir_proj((const ir_node **) &irn, -1);
485 switch(get_irn_be_opcode(irn)) {
486 #define XXX(a,b) case beo_ ## a: return arch_irn_class_ ## b;
499 arch_irn_class_t be_node_get_flags(const void *_self, const ir_node *irn)
504 static const arch_irn_ops_if_t be_node_irn_ops_if = {
505 be_node_get_irn_reg_req,
512 static const arch_irn_ops_t be_node_irn_ops = {
516 const void *be_node_get_arch_ops(const arch_irn_handler_t *self, const ir_node *irn)
518 redir_proj((const ir_node **) &irn, -1);
519 return is_be_node(irn) ? &be_node_irn_ops : NULL;
522 const arch_irn_handler_t be_node_irn_handler = {
526 static int dump_node(ir_node *irn, FILE *f, dump_reason_t reason)
528 be_node_attr_t *at = get_irn_attr(irn);
531 assert(is_be_node(irn));
534 case dump_node_opcode_txt:
535 fprintf(f, get_op_name(get_irn_op(irn)));
537 case dump_node_mode_txt:
538 fprintf(f, get_mode_name(get_irn_mode(irn)));
540 case dump_node_nodeattr_txt:
542 case dump_node_info_txt:
543 fprintf(f, "reg class: %s\n", at->cls->name);
544 for(i = 0; i < at->n_outs; ++i) {
545 const arch_register_t *reg = at->reg_data[i].reg;
546 fprintf(f, "reg #%d: %s\n", i, reg ? reg->name : "n/a");
549 if(get_irn_be_opcode(irn) == beo_Spill) {
550 be_spill_attr_t *a = (be_spill_attr_t *) at;
551 unsigned ofs = get_entity_offset_bytes(a->ent);
552 ir_fprintf(f, "spill context: %+F\n", a->spill_ctx);
553 ir_fprintf(f, "spill entity: %+F offset %x (%d)\n", a->ent, ofs, ofs);
561 static const ir_op_ops be_node_op_ops = {
577 pset *nodes_live_at(const arch_env_t *arch_env, const arch_register_class_t *cls, const ir_node *pos, pset *live)
579 firm_dbg_module_t *dbg = firm_dbg_register("firm.be.node");
580 ir_node *bl = get_nodes_block(pos);
584 live_foreach(bl, li) {
585 ir_node *irn = (ir_node *) li->irn;
586 if(live_is_end(li) && arch_irn_consider_in_reg_alloc(arch_env, cls, irn))
587 pset_insert_ptr(live, irn);
590 sched_foreach_reverse(bl, irn) {
595 * If we encounter the node we want to insert the Perm after,
596 * exit immediately, so that this node is still live
601 DBG((dbg, LEVEL_1, "%+F\n", irn));
602 for(x = pset_first(live); x; x = pset_next(live))
603 DBG((dbg, LEVEL_1, "\tlive: %+F\n", x));
605 if(arch_irn_has_reg_class(arch_env, irn, -1, cls))
606 pset_remove_ptr(live, irn);
608 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
609 ir_node *op = get_irn_n(irn, i);
611 if(arch_irn_consider_in_reg_alloc(arch_env, cls, op))
612 pset_insert_ptr(live, op);
619 ir_node *insert_Perm_after(const arch_env_t *arch_env,
620 const arch_register_class_t *cls,
621 dom_front_info_t *dom_front,
624 ir_node *bl = is_Block(pos) ? pos : get_nodes_block(pos);
625 ir_graph *irg = get_irn_irg(bl);
626 pset *live = pset_new_ptr_default();
627 firm_dbg_module_t *dbg = firm_dbg_register("be.node");
629 ir_node *curr, *irn, *perm, **nodes;
632 DBG((dbg, LEVEL_1, "Insert Perm after: %+F\n", pos));
634 if(!nodes_live_at(arch_env, cls, pos, live))
635 assert(0 && "position not found");
637 n = pset_count(live);
642 nodes = malloc(n * sizeof(nodes[0]));
644 DBG((dbg, LEVEL_1, "live:\n"));
645 for(irn = pset_first(live), i = 0; irn; irn = pset_next(live), i++) {
646 DBG((dbg, LEVEL_1, "\t%+F\n", irn));
650 perm = be_new_Perm(cls, irg, bl, n, nodes);
651 sched_add_after(pos, perm);
655 for(i = 0; i < n; ++i) {
657 ir_node *perm_op = get_irn_n(perm, i);
658 const arch_register_t *reg = arch_get_irn_register(arch_env, perm_op);
660 ir_mode *mode = get_irn_mode(perm_op);
661 ir_node *proj = new_r_Proj(irg, bl, perm, mode, i);
662 arch_set_irn_register(arch_env, proj, reg);
664 sched_add_after(curr, proj);
668 be_introduce_copies(dom_front, perm_op, 1, copies);