becopyheur2: Use rbitset_copy_to_bitset().
[libfirm] / ir / be / bearch.c
index c00de53..0989ab8 100644 (file)
+/*
+ * This file is part of libFirm.
+ * Copyright (C) 2012 University of Karlsruhe.
+ */
+
 /**
- * Processor architecture specification.
- * @author Sebastian Hack
- * @date 11.2.2005
- *
- * $Id$
+ * @file
+ * @brief       Processor architecture specification.
+ * @author      Sebastian Hack
  */
+#include "config.h"
 
 #include <string.h>
 
-#include "bearch_t.h"
-
-#include "firm_config.h"
-#include "set.h"
-
-#include "entity.h"
+#include "bearch.h"
+#include "benode.h"
+#include "beinfo.h"
 #include "ircons_t.h"
+#include "irnode_t.h"
+#include "irop_t.h"
 
-#if 1 /* HAVE_ALLOCA_H */
-#include <alloca.h>
-#endif /* HAVE_ALLOCA_H */
-
-#define INIT_HEADER(tgt, kind_suffix, a_isa, str) \
-       do { \
-               arch_header_t *h = (arch_header_t *) (tgt); \
-               memset(tgt, 0, sizeof(*(tgt))); \
-               h->kind = arch_kind_ ## kind_suffix; \
-               h->name = new_id_from_str(str); \
-               h->isa = a_isa; \
-       } while(0)
-
-static INLINE int hash_header(const arch_header_t *header)
-{
-       int res = HASH_PTR(header->isa);
-       res = 37 * res + HASH_STR(header->name, strlen(header->name));
-       res = 37 * res + header->kind;
-       return res;
-}
-
-static int cmp_header(const void *a, const void *b, size_t size)
-{
-       const arch_header_t *h1 = a;
-       const arch_header_t *h2 = b;
-
-       return !(h1->kind == h2->kind && strcmp(h1->name, h2->name) == 0);
-}
+#include "bitset.h"
+#include "pset.h"
+#include "raw_bitset.h"
 
-static set *arch_data = NULL;
-
-static set *get_arch_data(void)
-{
-       if(!arch_data)
-               arch_data = new_set(cmp_header, 256);
+#include "irprintf.h"
 
-       return arch_data;
-}
-
-typedef struct _obj_info_t {
-       const char *name;
-       int listed_in_isa;
-       size_t size;
-} obj_info_t;
-
-static const obj_info_t obj_info[] = {
-#define ARCH_OBJ(name,listed_in_isa)           { #name, listed_in_isa, sizeof(arch_ ## name ## _t) },
-#include "bearch_obj.def"
-#undef ARCH_OBJ
-       { 0 }
+arch_register_req_t const arch_no_requirement = {
+       arch_register_req_type_none,
+       NULL,
+       NULL,
+       0,
+       0,
+       0
 };
 
 /**
- * Insert an arch object to the global arch obj storage.
- *
- * If the object has already been created there, nothing is done and
- * the old object is created.
- *
- * @param kind The kind of the arch object.
- * @param isa The isa the object belongs to or NULL if it is the isa
- * itself.
- * @param name The name of the object.
- * @param was_new A pointer to an int where 1/0 is stored if the
- * object was created or already present. If NULL, it is simply ignored.
- * @return A pointer to the object.
+ * Get the isa responsible for a node.
+ * @param irn The node to get the responsible isa for.
+ * @return The irn operations given by the responsible isa.
  */
-static INLINE void *_arch_data_insert(arch_kind_t kind, arch_isa_t *isa,
-               const char *name, size_t size, int *was_new)
+static const arch_irn_ops_t *get_irn_ops(const ir_node *irn)
 {
-       const obj_info_t *info = &obj_info[kind];
-       arch_header_t *data = alloca(size);
-       arch_header_t *res = NULL;
-
-       memset(data, 0, size);
-       data->kind = kind;
-       data->isa = isa;
-       data->name = get_id_str(new_id_from_str(name));
-       data->is_new = 1;
-
-       res = set_insert(get_arch_data(), data, size, hash_header(data));
-
-       /* If the object is newly created and thus not yet present
-        * in the set, add it to the isa */
-       if(res->is_new) {
-
-               /*
-                * The inserted object was no isa, list it in the isa if this is
-                * desired.
-                */
-               if(isa && info->listed_in_isa)
-                       list_add(&res->list, &isa->heads[kind]);
-
-               /* The inserted object is an isa, so initialize all its list heads. */
-               else {
-                       int i;
-                       arch_isa_t *isa = (arch_isa_t *) res;
-
-                       for(i = 0; i < arch_kind_last; ++i)
-                               INIT_LIST_HEAD(&isa->heads[i]);
-               }
+       if (is_Proj(irn)) {
+               irn = get_Proj_pred(irn);
+               assert(!is_Proj(irn));
        }
 
-       /*
-        * If the caller wants to know, of the object was newly created,
-        * give it to him.
-        */
-       if(was_new)
-               *was_new = res->is_new;
-
-       /* Mark the object as NOT new. */
-       res->is_new = 0;
+       ir_op                *ops    = get_irn_op(irn);
+       const arch_irn_ops_t *be_ops = get_op_ops(ops)->be_ops;
 
-       return res;
+       return be_ops;
 }
 
-#define arch_data_insert(type_suffix, isa, name, was_new) \
-       _arch_data_insert(arch_kind_ ## type_suffix, isa, name, sizeof(arch_ ## type_suffix ## _t), was_new)
-
-static INLINE void *_arch_data_find(arch_kind_t kind, const arch_isa_t *isa, const char *name)
+void arch_set_frame_offset(ir_node *irn, int offset)
 {
-       arch_header_t header;
-
-       memset(&header, 0, sizeof(header));
-       header.kind = kind;
-       header.isa = (arch_isa_t *) isa;
-       header.name = name;
-
-       return set_find(get_arch_data(), &header, sizeof(header), hash_header(&header));
+       const arch_irn_ops_t *ops = get_irn_ops(irn);
+       ops->set_frame_offset(irn, offset);
 }
 
-#define arch_data_find(type_suffix, isa, name) \
-       _arch_data_find(arch_kind_ ## type_suffix, isa, name)
-
-arch_isa_t *arch_add_isa(const char *name)
+ir_entity *arch_get_frame_entity(const ir_node *irn)
 {
-       return arch_data_insert(isa, NULL, name, NULL);
+       const arch_irn_ops_t *ops = get_irn_ops(irn);
+       return ops->get_frame_entity(irn);
 }
 
-arch_register_class_t *arch_add_register_class(arch_isa_t *isa, const char *name, int n_regs)
+int arch_get_sp_bias(ir_node *irn)
 {
-       arch_register_class_t *cls =
-               _arch_data_insert(arch_kind_register_class, isa, name,
-                               sizeof(arch_register_class_t) + n_regs * sizeof(arch_register_t *), NULL);
+       const arch_irn_ops_t *ops = get_irn_ops(irn);
+       return ops->get_sp_bias(irn);
+}
 
-       cls->n_regs = n_regs;
+int arch_possible_memory_operand(const ir_node *irn, unsigned int i)
+{
+       const arch_irn_ops_t *ops = get_irn_ops(irn);
 
-       return cls;
+       if (ops->possible_memory_operand) {
+               return ops->possible_memory_operand(irn, i);
+       } else {
+               return 0;
+       }
 }
 
-arch_register_t *arch_add_register(arch_register_class_t *cls, int index, const char *name)
+void arch_perform_memory_operand(ir_node *irn, ir_node *spill, unsigned int i)
 {
-       arch_register_t *reg = NULL;
+       const arch_irn_ops_t *ops = get_irn_ops(irn);
 
-       assert(index >= 0 && index < cls->n_regs);
-       reg = _arch_data_insert(arch_kind_register, arch_obj_get_isa(cls), name,
-                       sizeof(arch_register_t), NULL);
-       cls->regs[index] = reg;
+       if (ops->perform_memory_operand) {
+               ops->perform_memory_operand(irn, spill, i);
+       } else {
+               return;
+       }
+}
 
-       reg->index = index;
-       reg->reg_class = cls;
-       reg->flags = arch_register_flag_none;
+int arch_get_op_estimated_cost(const ir_node *irn)
+{
+       const arch_irn_ops_t *ops = get_irn_ops(irn);
 
-       return reg;
+       if (ops->get_op_estimated_cost) {
+               return ops->get_op_estimated_cost(irn);
+       } else {
+               return 1;
+       }
 }
 
-arch_immediate_t *arch_add_immediate(arch_isa_t *isa, const char *name, ir_mode *mode)
+static reg_out_info_t *get_out_info_n(const ir_node *node, unsigned pos)
 {
-       arch_immediate_t *imm = arch_data_insert(immediate, isa, name, NULL);
-       imm->mode = mode;
-       return imm;
+       const backend_info_t *info = be_get_info(node);
+       assert(pos < (unsigned)ARR_LEN(info->out_infos));
+       return &info->out_infos[pos];
 }
 
-static const size_t operand_sizes[] = {
-       0,
-       0,
-       sizeof(entity *),
-       sizeof(arch_register_t *),
-       sizeof(tarval *)
-};
 
-arch_insn_format_t *arch_add_insn_format(arch_isa_t *isa, const char *name, int n_in, int n_out)
+const arch_register_t *arch_get_irn_register(const ir_node *node)
 {
-       int i;
-
-       arch_insn_format_t *fmt =
-               _arch_data_insert(arch_kind_insn_format, isa, name,
-                               sizeof(arch_insn_format_t) + (n_in + n_out) * sizeof(arch_operand_type_t), NULL);
-
-       fmt->n_in = n_in;
-       fmt->n_out = n_out;
-       fmt->irn_data_size = 0;
-
-       /*
-        * Compute the number of bytes which must be extra allocated if this
-        * opcode is instantiated.
-        */
-       for(i = 0; i < fmt->n_in; ++i) {
-               arch_operand_t *op = arch_get_in_operand(fmt, i);
-               op->offset_in_irn_data = fmt->irn_data_size;
-               fmt->irn_data_size += operand_sizes[op->type];
-       }
-
-       if(fmt->n_out == 1) {
-               arch_operand_t *op = arch_get_in_operand(fmt, i);
-               op->offset_in_irn_data = fmt->irn_data_size;
-               fmt->irn_data_size += operand_sizes[op->type];
-       }
-
-       return fmt;
+       const reg_out_info_t *out = get_out_info(node);
+       return out->reg;
 }
 
-arch_insn_t *arch_add_insn(arch_insn_format_t *fmt, const char *name)
+const arch_register_t *arch_get_irn_register_out(const ir_node *node,
+                                                 unsigned pos)
 {
-       /* Insert the insn into the isa. */
-       arch_insn_t *insn = arch_data_insert(insn, arch_obj_get_isa(fmt), name, NULL);
-
-       insn->format = fmt;
-       insn->op = new_ir_op(get_next_ir_opcode(), name, op_pin_state_pinned, 0,
-                       oparity_dynamic, 0, sizeof(arch_irn_data_t) + fmt->irn_data_size);
+       const reg_out_info_t *out = get_out_info_n(node, pos);
+       return out->reg;
+}
 
-       return insn;
+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);
 }
 
-arch_insn_format_t *arch_find_insn_format(const arch_isa_t *isa, const char *name)
+void arch_set_irn_register_out(ir_node *node, unsigned pos,
+                               const arch_register_t *reg)
 {
-       return arch_data_find(insn_format, isa, name);
+       reg_out_info_t *out = get_out_info_n(node, pos);
+       out->reg            = reg;
 }
 
-arch_isa_t *arch_find_isa(const char *name)
+void arch_set_irn_register(ir_node *node, const arch_register_t *reg)
 {
-       return arch_data_find(isa, NULL, name);
+       reg_out_info_t *out = get_out_info(node);
+       out->reg = reg;
 }
 
-arch_insn_t *arch_find_insn(const arch_isa_t *isa, const char *name)
+void arch_set_irn_flags(ir_node *node, arch_irn_flags_t flags)
 {
-       return arch_data_find(insn, isa, name);
+       backend_info_t *const info = be_get_info(node);
+       info->flags = flags;
 }
 
-arch_register_class_t *arch_find_register_class_t(arch_isa_t *isa, const char *name)
+void arch_add_irn_flags(ir_node *node, arch_irn_flags_t flags)
 {
-       return arch_data_find(register_class, isa, name);
+       backend_info_t *const info = be_get_info(node);
+       info->flags |= flags;
 }
 
-arch_register_set_t *arch_get_register_set_for_class(arch_register_class_t *cls)
+bool arch_reg_is_allocatable(const arch_register_req_t *req,
+                             const arch_register_t *reg)
 {
-       return _arch_get_register_set_for_class(cls);
+       assert(req->type != arch_register_req_type_none);
+       if (req->cls != reg->reg_class)
+               return false;
+       if (reg->type & arch_register_type_virtual)
+               return true;
+       if (arch_register_req_is(req, limited))
+               return rbitset_is_set(req->limited, reg->index);
+       return true;
 }
 
-ir_node *arch_new_node(const arch_insn_t *insn, ir_graph *irg, ir_node *block,
-               ir_mode *mode, int arity, ir_node **in)
+/**
+ * Print information about a register requirement in human readable form
+ * @param F   output stream/file
+ * @param req The requirements structure to format.
+ */
+static void arch_dump_register_req(FILE *F, const arch_register_req_t *req,
+                            const ir_node *node)
 {
-       ir_node *irn = new_ir_node(NULL, irg, block, insn->op, mode, arity, in);
-       arch_irn_data_t *data = (void *) &irn->attr;
+       if (req == NULL || req->type == arch_register_req_type_none) {
+               fprintf(F, "n/a");
+               return;
+       }
 
-       data->magic = ARCH_IRN_FOURCC;
-       data->insn = insn;
+       fprintf(F, "%s", req->cls->name);
 
-       return irn;
-}
+       if (arch_register_req_is(req, limited)) {
+               unsigned n_regs = req->cls->n_regs;
+               unsigned i;
 
-ir_node *arch_new_node_bare(const arch_insn_t *insn, ir_graph *irg, int arity)
-{
-       int i;
-       ir_node **in = alloca(sizeof(in[0]) * arity);
+               fprintf(F, " limited to");
+               for (i = 0; i < n_regs; ++i) {
+                       if (rbitset_is_set(req->limited, i)) {
+                               const arch_register_t *reg = &req->cls->regs[i];
+                               fprintf(F, " %s", reg->name);
+                       }
+               }
+       }
 
-       for(i = 0; i < arity; ++i)
-               in[i] = new_Unknown(mode_Is);
+       if (arch_register_req_is(req, should_be_same)) {
+               const unsigned other = req->other_same;
+               int i;
 
-       return arch_new_node(insn, irg, new_Unknown(mode_BB), mode_Is, arity, in);
+               fprintf(F, " same as");
+               for (i = 0; 1U << i <= other; ++i) {
+                       if (other & (1U << i)) {
+                               ir_fprintf(F, " #%d (%+F)", i, get_irn_n(skip_Proj_const(node), i));
+                       }
+               }
+       }
+
+       if (arch_register_req_is(req, must_be_different)) {
+               const unsigned other = req->other_different;
+               int i;
+
+               fprintf(F, " different from");
+               for (i = 0; 1U << i <= other; ++i) {
+                       if (other & (1U << i)) {
+                               ir_fprintf(F, " #%d (%+F)", i, get_irn_n(skip_Proj_const(node), i));
+                       }
+               }
+       }
+
+       if (req->width != 1) {
+               fprintf(F, " width:%d", req->width);
+       }
+       if (arch_register_req_is(req, aligned)) {
+               fprintf(F, " aligned");
+       }
+       if (arch_register_req_is(req, ignore)) {
+               fprintf(F, " ignore");
+       }
+       if (arch_register_req_is(req, produces_sp)) {
+               fprintf(F, " produces_sp");
+       }
 }
 
-ir_mode *arch_get_unknown_mode(void)
+void arch_dump_reqs_and_registers(FILE *F, const ir_node *node)
 {
-       return mode_Is;
+       backend_info_t *const info  = be_get_info(node);
+       int             const n_ins = get_irn_arity(node);
+       /* don't fail on invalid graphs */
+       if (!info || (!info->in_reqs && n_ins != 0) || !info->out_infos) {
+               fprintf(F, "invalid register requirements!!!\n");
+               return;
+       }
+
+       for (int i = 0; i < n_ins; ++i) {
+               const arch_register_req_t *req = arch_get_irn_register_req_in(node, i);
+               fprintf(F, "inreq #%d = ", i);
+               arch_dump_register_req(F, req, node);
+               fputs("\n", F);
+       }
+       be_foreach_out(node, o) {
+               const arch_register_req_t *req = arch_get_irn_register_req_out(node, o);
+               fprintf(F, "outreq #%u = ", o);
+               arch_dump_register_req(F, req, node);
+               const arch_register_t *reg = arch_get_irn_register_out(node, o);
+               fprintf(F, " [%s]\n", reg != NULL ? reg->name : "n/a");
+       }
+
+       fprintf(F, "flags =");
+       arch_irn_flags_t flags = arch_get_irn_flags(node);
+       if (flags == arch_irn_flags_none) {
+               fprintf(F, " none");
+       } else {
+               if (flags & arch_irn_flags_dont_spill) {
+                       fprintf(F, " unspillable");
+               }
+               if (flags & arch_irn_flags_rematerializable) {
+                       fprintf(F, " remat");
+               }
+               if (flags & arch_irn_flags_modify_flags) {
+                       fprintf(F, " modify_flags");
+               }
+               if (flags & arch_irn_flags_simple_jump) {
+                       fprintf(F, " simple_jump");
+               }
+               if (flags & arch_irn_flags_not_scheduled) {
+                       fprintf(F, " not_scheduled");
+               }
+       }
+       fprintf(F, " (0x%x)\n", (unsigned)flags);
 }