+ env.spills = new_set(cmp_spill, 10);
+ env.reloads = NEW_ARR_F(ir_node*, 0);
+ env.problem_found = 0;
+
+ irg_walk_graph(irg, collect_spills_walker, NULL, &env);
+ irg_walk_graph(irg, check_lonely_spills, NULL, &env);
+
+ check_spillslot_interference(&env);
+
+ DEL_ARR_F(env.reloads);
+ del_set(env.spills);
+
+ return ! env.problem_found;
+}
+
+
+
+/*--------------------------------------------------------------------------- */
+
+
+
+/**
+ * Check, if two values interfere.
+ * @param a The first value.
+ * @param b The second value.
+ * @return 1, if a and b interfere, 0 if not.
+ */
+static int my_values_interfere(const ir_node *a, const ir_node *b) {
+ const ir_edge_t *edge;
+ ir_node *bb;
+ int a2b = value_dominates(a, b);
+ int b2a = value_dominates(b, a);
+
+ /* If there is no dominance relation, they do not interfere. */
+ if(!a2b && !b2a)
+ return 0;
+
+ /*
+ * Adjust a and b so, that a dominates b if
+ * a dominates b or vice versa.
+ */
+ if(b2a) {
+ const ir_node *t = a;
+ a = b;
+ b = t;
+ }
+
+ bb = get_nodes_block(b);
+
+ /*
+ * Look at all usages of a.
+ * If there's one usage of a in the block of b, then
+ * we check, if this use is dominated by b, if that's true
+ * a and b interfere. Note that b must strictly dominate the user,
+ * since if b is the last user of in the block, b and a do not
+ * interfere.
+ * Uses of a not in b's block can be disobeyed, because the
+ * check for a being live at the end of b's block is already
+ * performed.
+ */
+ foreach_out_edge(a, edge) {
+ const ir_node *user = get_edge_src_irn(edge);
+ if(b == user)
+ continue;
+
+ if(get_irn_opcode(user) == iro_End)
+ continue;
+
+ /* in case of phi arguments we compare with the block the value comes from */
+ if(is_Phi(user)) {
+ ir_node *phiblock = get_nodes_block(user);
+ if(phiblock == bb)
+ continue;
+ user = get_irn_n(phiblock, get_edge_src_pos(edge));
+ }
+
+ if(value_dominates(b, user))
+ return 1;
+ }
+
+ return 0;
+}
+
+
+
+/*--------------------------------------------------------------------------- */
+
+static const arch_env_t *arch_env;
+static ir_graph *irg;
+static be_lv_t *lv;
+static int problem_found;
+static const arch_register_class_t *regclass;
+static ir_node **registers;
+
+static void check_register_constraints(ir_node *node)
+{
+ const arch_register_t *reg;
+ int i, arity;
+
+ /* verify output register */
+ if (arch_get_irn_reg_class_out(node) != NULL) {
+ reg = arch_get_irn_register(node);
+ if (reg == NULL) {
+ ir_fprintf(stderr, "Verify warning: Node %+F in block %+F(%s) should have a register assigned\n",
+ node, get_nodes_block(node), get_irg_dump_name(irg));
+ problem_found = 1;
+ } else if (!arch_register_type_is(reg, joker) && !arch_reg_out_is_allocatable(node, reg)) {
+ ir_fprintf(stderr, "Verify warning: Register %s assigned as output of %+F not allowed (register constraint) in block %+F(%s)\n",
+ reg->name, node, get_nodes_block(node), get_irg_dump_name(irg));
+ problem_found = 1;
+ }
+ }
+
+ /* verify input register */
+ arity = get_irn_arity(node);
+ for (i = 0; i < arity; ++i) {
+ ir_node *pred = get_irn_n(node, i);
+
+ if (is_Unknown(pred))
+ continue;
+
+ if (is_Bad(pred)) {
+ ir_fprintf(stderr, "Verify warning: %+F in block %+F(%s) has Bad as input %d\n",
+ node, get_nodes_block(node), get_irg_dump_name(irg), i);
+ problem_found = 1;
+ continue;
+ }
+
+ if (arch_get_irn_reg_class(node, i) == NULL)
+ continue;
+
+ reg = arch_get_irn_register(pred);
+ if (reg == NULL) {
+ ir_fprintf(stderr, "Verify warning: Node %+F in block %+F(%s) should have a register assigned (%+F input constraint)\n",
+ pred, get_nodes_block(pred), get_irg_dump_name(irg), node);
+ problem_found = 1;
+ continue;
+ } else if (!arch_register_type_is(reg, joker) && ! arch_reg_is_allocatable(node, i, reg)) {
+ ir_fprintf(stderr, "Verify warning: Register %s as input %d of %+F not allowed (register constraint) in block %+F(%s)\n",
+ reg->name, i, node, get_nodes_block(node), get_irg_dump_name(irg));
+ problem_found = 1;
+ }
+ }
+
+ /* phis should be NOPs at this point, which means all input regs
+ * must be the same as the output reg */
+ if (is_Phi(node)) {
+ int i, arity;
+
+ reg = arch_get_irn_register(node);
+
+ arity = get_irn_arity(node);
+ for (i = 0; i < arity; ++i) {
+ ir_node *pred = get_Phi_pred(node, i);
+ const arch_register_t *pred_reg = arch_get_irn_register(pred);
+
+ if (reg != pred_reg && !arch_register_type_is(pred_reg, joker)) {
+ ir_fprintf(stderr, "Verify warning: Input %d of %+F in block %+F(%s) uses register %s instead of %s\n",
+ i, node, get_nodes_block(node), get_irg_dump_name(irg), pred_reg->name, reg->name);
+ problem_found = 1;
+ }
+ }
+ }
+}
+
+static void value_used(ir_node *node) {
+ const arch_register_t *reg;
+ ir_node *reg_node;
+
+ if (arch_get_irn_reg_class_out(node) != regclass)
+ return;
+
+ reg = arch_get_irn_register(node);
+ if (reg->type & arch_register_type_virtual)
+ return;
+
+ reg_node = registers[reg->index];
+ if (reg_node != NULL && reg_node != node) {
+ ir_fprintf(stderr, "Verify warning: Register %s assigned more than once in block %+F(%s) (nodes %+F %+F)\n",
+ reg->name, get_nodes_block(node), get_irg_dump_name(irg),
+ node, reg_node);
+ problem_found = 1;
+ }
+
+ registers[reg->index] = node;
+}
+
+static void value_def(ir_node *node)
+{
+ const arch_register_t *reg;
+ ir_node *reg_node;
+
+ if (arch_get_irn_reg_class_out(node) != regclass)
+ return;
+
+ reg = arch_get_irn_register(node);
+ if (reg->type & arch_register_type_virtual)
+ return;
+
+ reg_node = registers[reg->index];
+
+ if (reg_node != node) {
+ ir_fprintf(stderr, "Verify warning: Node %+F not registered as value for Register %s (but %+F) in block %+F(%s)\n",
+ node, reg->name, reg_node, get_nodes_block(node), get_irg_dump_name(irg));
+ problem_found = 1;
+ }
+ registers[reg->index] = NULL;
+}
+
+static void verify_block_register_allocation(ir_node *block, void *data) {
+ int i, nregclasses;
+ (void) data;
+
+ nregclasses = arch_env_get_n_reg_class(arch_env);
+ for (i = 0; i < nregclasses; ++i) {
+ ir_node *node;
+ int idx, i2, n_regs;
+
+ regclass = arch_env_get_reg_class(arch_env, i);
+
+ assert(lv->nodes && "live sets must be computed");
+
+ n_regs = arch_register_class_n_regs(regclass);
+ registers = alloca(n_regs * sizeof(registers[0]));
+ memset(registers, 0, n_regs * sizeof(registers[0]));
+
+ be_lv_foreach(lv, block, be_lv_state_end, idx) {
+ ir_node *node = be_lv_get_irn(lv, block, idx);
+ value_used(node);
+ }
+
+ sched_foreach_reverse(block, node) {
+ int arity;
+
+ if (get_irn_mode(node) == mode_T) {
+ const ir_edge_t *edge;
+ foreach_out_edge(node, edge) {
+ ir_node *def = get_edge_src_irn(edge);
+ value_def(def);
+ }
+ } else {
+ value_def(node);
+ }
+
+ check_register_constraints(node);
+ if (is_Phi(node))
+ continue;
+
+ arity = get_irn_arity(node);
+ for (i2 = 0; i2 < arity; ++i2) {
+ ir_node *use = get_irn_n(node, i2);
+ value_used(use);
+ }
+ }
+
+ be_lv_foreach(lv, block, be_lv_state_in, idx) {
+ ir_node *node = be_lv_get_irn(lv, block, idx);
+ value_def(node);
+ }
+
+ /* set must be empty now */
+ for (i2 = 0; i2 < n_regs; ++i2) {
+ if (registers[i2] == NULL)
+ continue;
+
+ ir_fprintf(stderr, "Verify warning: Node %+F not live-in and no def found in block %+F(%s)\n",
+ registers[i2], block, get_irg_dump_name(irg));
+ problem_found = 1;
+ }
+ }
+}
+
+int be_verify_register_allocation(const be_irg_t *birg) {
+ arch_env = be_get_birg_arch_env(birg);
+ irg = be_get_birg_irg(birg);
+ lv = be_liveness(birg);
+ problem_found = 0;
+
+ be_liveness_assure_sets(lv);
+ irg_block_walk_graph(irg, verify_block_register_allocation, NULL, NULL);
+
+ be_liveness_free(lv);
+
+ return !problem_found;
+}
+
+
+
+/*--------------------------------------------------------------------------- */
+
+
+
+typedef struct _verify_out_dead_nodes_env {
+ ir_graph *irg;
+ bitset_t *reachable;
+ int problem_found;
+} verify_out_dead_nodes_env;
+
+static void check_out_edges(ir_node *node, verify_out_dead_nodes_env *env) {
+ ir_graph *irg = env->irg;
+ const ir_edge_t* edge;
+
+ if (irn_visited_else_mark(node))
+ return;
+
+ /* we find too many (uncritical) dead nodes in block out edges */
+ if(is_Block(node))
+ return;
+
+ foreach_out_edge(node, edge) {
+ ir_node* src = get_edge_src_irn(edge);
+
+ if(!bitset_is_set(env->reachable, get_irn_idx(src)) && !is_Block(src)) {
+ ir_fprintf(stderr, "Verify warning: Node %+F in block %+F(%s) only reachable through out edges from %+F\n",
+ src, get_nodes_block(src), get_irg_dump_name(irg), node);
+ env->problem_found = 1;
+ continue;
+ }
+
+ check_out_edges(src, env);
+ }
+}
+
+static void set_reachable(ir_node *node, void* data)
+{
+ bitset_t* reachable = data;
+ bitset_set(reachable, get_irn_idx(node));
+}
+
+int be_verify_out_edges(ir_graph *irg) {
+ verify_out_dead_nodes_env env;
+
+ env.irg = irg;
+ env.reachable = bitset_alloca(get_irg_last_idx(irg));
+ env.problem_found = edges_verify(irg);