X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=ir%2Fbe%2Fbeverify.c;h=7b3f5d4f093b80ec23cfad58f6402f1eec94a847;hb=f73b8014037f87fca038a96e04a9a8cc5354079c;hp=97a3322a3d67c8794df101df3af35df5e10b0204;hpb=48f893878b07f6e334389ff52abda5cc2adbf179;p=libfirm diff --git a/ir/be/beverify.c b/ir/be/beverify.c index 97a3322a3..7b3f5d4f0 100644 --- a/ir/be/beverify.c +++ b/ir/be/beverify.c @@ -9,17 +9,24 @@ #include "config.h" #endif -#include "beverify.h" -#include "belive.h" -#include "besched_t.h" +#include "bitset.h" +#include "set.h" +#include "array.h" #include "irnode.h" #include "irgraph.h" #include "irgwalk.h" #include "irprintf.h" #include "irdump_t.h" +#include "iredges.h" + +#include "beverify.h" +#include "belive.h" +#include "besched_t.h" #include "benode_t.h" +static int my_values_interfere(const ir_node *a, const ir_node *b); + typedef struct be_verify_register_pressure_env_t_ { ir_graph *irg; /**< the irg to verify */ be_lv_t *lv; /**< Liveness information. */ @@ -32,8 +39,7 @@ typedef struct be_verify_register_pressure_env_t_ { /** * Print all nodes of a pset into a file. */ -static void print_living_values(FILE *F, pset *live_nodes) -{ +static void print_living_values(FILE *F, pset *live_nodes) { ir_node *node; ir_fprintf(F, "\t"); @@ -46,8 +52,7 @@ static void print_living_values(FILE *F, pset *live_nodes) /** * Check if number of live nodes never exceeds the number of available registers. */ -static void verify_liveness_walker(ir_node *block, void *data) -{ +static void verify_liveness_walker(ir_node *block, void *data) { be_verify_register_pressure_env_t *env = (be_verify_register_pressure_env_t *)data; pset *live_nodes = pset_new_ptr_default(); ir_node *irn; @@ -85,15 +90,14 @@ static void verify_liveness_walker(ir_node *block, void *data) /** * Start a walk over the irg and check the register pressure. */ -int be_verify_register_pressure(const arch_env_t *arch_env, const arch_register_class_t *cls, ir_graph *irg) -{ +int be_verify_register_pressure(const be_irg_t *birg, const arch_register_class_t *cls, ir_graph *irg) { be_verify_register_pressure_env_t env; env.lv = be_liveness(irg); env.irg = irg; - env.arch_env = arch_env; + env.arch_env = birg->main_env->arch_env; env.cls = cls; - env.registers_available = arch_count_non_ignore_regs(arch_env, cls); + env.registers_available = env.cls->n_regs - be_put_ignore_regs(birg, env.cls, NULL); env.problem_found = 0; irg_block_walk_graph(irg, verify_liveness_walker, NULL, &env); @@ -110,15 +114,13 @@ typedef struct be_verify_schedule_env_t_ { /** * Simple schedule checker. */ -static void verify_schedule_walker(ir_node *block, void *data) -{ +static void verify_schedule_walker(ir_node *block, void *data) { be_verify_schedule_env_t *env = (be_verify_schedule_env_t*) data; ir_node *node; int non_phi_found = 0; int cfchange_found = 0; // TODO ask arch about delay branches int delay_branches = 0; - pset *uses = pset_new_ptr_default(); /* * Tests for the following things: @@ -164,27 +166,81 @@ static void verify_schedule_walker(ir_node *block, void *data) } // 3. Check for uses - if(pset_find_ptr(uses, node)) { - ir_fprintf(stderr, "Verify Warning: Value %+F used before it was defined in block %+F (%s)\n", - node, block, get_irg_dump_name(env->irg)); - env->problem_found = 1; - } if(!is_Phi(node)) { + int nodetime = sched_get_time_step(node); for(i = 0, arity = get_irn_arity(node); i < arity; ++i) { - pset_insert_ptr(uses, get_irn_n(node, i)); + ir_node *arg = get_irn_n(node, i); + if(get_nodes_block(arg) != block + || !sched_is_scheduled(arg)) + continue; + + if(sched_get_time_step(arg) >= nodetime) { + ir_fprintf(stderr, "Verify Warning: Value %+F used by %+F before it was defined in block %+F (%s)\n", + arg, node, block, get_irg_dump_name(env->irg)); + env->problem_found = 1; + } } } + + // 4. check for dead nodes + if(get_irn_n_edges(node) == 0) { + ir_fprintf(stderr, "Verify warning: Node %+F is dead but scheduled in block %+F (%s)\n", + node, block, get_irg_dump_name(env->irg)); + env->problem_found = 1; + } } - del_pset(uses); /* check that all delay branches are filled (at least with NOPs) */ if (cfchange_found && delay_branches != 0) { - ir_fprintf(stderr, "Not all delay slots filled after jump (%d/%d) in block %+F (%s)\n", + ir_fprintf(stderr, "Verify warning: Not all delay slots filled after jump (%d/%d) in block %+F (%s)\n", block, get_irg_dump_name(env->irg)); env->problem_found = 1; } } +static int should_be_scheduled(ir_node *node) { + if(is_Block(node)) + return -1; + + if(get_irn_mode(node) == mode_M) { + if(is_Proj(node)) + return -1; + if(is_Phi(node) || is_Sync(node) || is_Pin(node)) + return 0; + } + if(is_Proj(node) && get_irn_mode(node) == mode_X) + return 0; + if(be_is_Keep(node) && get_irn_opcode(get_nodes_block(node)) == iro_Bad) + return 0; + + switch(get_irn_opcode(node)) { + case iro_End: + case iro_NoMem: + case iro_Bad: + case iro_Unknown: + return 0; + default: + break; + } + + return 1; +} + +static void check_schedule(ir_node *node, void *data) { + be_verify_schedule_env_t *env = data; + int should_be; + + should_be = should_be_scheduled(node); + if(should_be == -1) + return; + + if(should_be ? !sched_is_scheduled(node) : sched_is_scheduled(node)) { + ir_fprintf(stderr, "Verify warning: Node %+F in block %+F(%s) should%s be scheduled\n", + node, get_nodes_block(node), get_irg_dump_name(env->irg), should_be ? "" : " not"); + env->problem_found = 1; + } +} + /** * Start a walk over the irg and check schedule. */ @@ -196,6 +252,538 @@ int be_verify_schedule(ir_graph *irg) env.irg = irg; irg_block_walk_graph(irg, verify_schedule_walker, NULL, &env); + // check if all nodes are scheduled + irg_walk_graph(irg, check_schedule, NULL, &env); return ! env.problem_found; } + + + +//--------------------------------------------------------------------------- + + + +typedef struct _spill_t { + ir_node *spill; + ir_entity *ent; +} spill_t; + +typedef struct { + const arch_env_t *arch_env; + ir_graph *irg; + set *spills; + ir_node **reloads; + int problem_found; +} be_verify_spillslots_env_t; + +static int cmp_spill(const void* d1, const void* d2, size_t size) { + const spill_t* s1 = d1; + const spill_t* s2 = d2; + return s1->spill != s2->spill; +} + +static spill_t *find_spill(be_verify_spillslots_env_t *env, ir_node *node) { + spill_t spill; + + spill.spill = node; + return set_find(env->spills, &spill, sizeof(spill), HASH_PTR(node)); +} + +static spill_t *get_spill(be_verify_spillslots_env_t *env, ir_node *node, ir_entity *ent) { + spill_t spill, *res; + int hash = HASH_PTR(node); + + spill.spill = node; + res = set_find(env->spills, &spill, sizeof(spill), hash); + + if(res == NULL) { + spill.ent = ent; + res = set_insert(env->spills, &spill, sizeof(spill), hash); + } + + return res; +} + +static ir_node *get_memory_edge(const ir_node *node) { + int i, arity; + ir_node *result = NULL; + + arity = get_irn_arity(node); + for(i = arity - 1; i >= 0; --i) { + ir_node *arg = get_irn_n(node, i); + if(get_irn_mode(arg) == mode_M) { + assert(result == NULL); + result = arg; + } + } + + return result; +} + +static void collect(be_verify_spillslots_env_t *env, ir_node *node, ir_node *reload, ir_entity* ent); + +static void check_entity(be_verify_spillslots_env_t *env, ir_node *node, ir_entity *ent) { + if(ent == NULL) { + ir_fprintf(stderr, "Verify warning: Node %+F in block %+F(%s) should have an entity assigned\n", + node, get_nodes_block(node), get_irg_dump_name(env->irg)); + } +} + +static void collect_spill(be_verify_spillslots_env_t *env, ir_node *node, ir_node *reload, ir_entity* ent) { + ir_entity *spillent = arch_get_frame_entity(env->arch_env, node); + check_entity(env, node, spillent); + get_spill(env, node, ent); + + if(spillent != ent) { + ir_fprintf(stderr, "Verify warning: Spill %+F has different entity than reload %+F in block %+F(%s)\n", + node, reload, get_nodes_block(node), get_irg_dump_name(env->irg)); + env->problem_found = 1; + } +} + +static void collect_memperm(be_verify_spillslots_env_t *env, ir_node *node, ir_node *reload, ir_entity* ent) { + int i, arity; + spill_t spill, *res; + int hash = HASH_PTR(node); + int out; + ir_node* memperm; + ir_entity *spillent; + + assert(is_Proj(node)); + + memperm = get_Proj_pred(node); + out = get_Proj_proj(node); + + spillent = be_get_MemPerm_out_entity(memperm, out); + check_entity(env, memperm, spillent); + if(spillent != ent) { + ir_fprintf(stderr, "Verify warning: MemPerm %+F has different entity than reload %+F in block %+F(%s)\n", + node, reload, get_nodes_block(node), get_irg_dump_name(env->irg)); + env->problem_found = 1; + } + + spill.spill = node; + res = set_find(env->spills, &spill, sizeof(spill), hash); + if(res != NULL) { + return; + } + + spill.ent = spillent; + res = set_insert(env->spills, &spill, sizeof(spill), hash); + + for(i = 0, arity = be_get_MemPerm_entity_arity(memperm); i < arity; ++i) { + ir_node* arg = get_irn_n(memperm, i + 1); + ir_entity* argent = be_get_MemPerm_in_entity(memperm, i); + + collect(env, arg, memperm, argent); + } +} + +static void collect_memphi(be_verify_spillslots_env_t *env, ir_node *node, ir_node *reload, ir_entity *ent) { + int i, arity; + spill_t spill, *res; + int hash = HASH_PTR(node); + + assert(is_Phi(node)); + + spill.spill = node; + res = set_find(env->spills, &spill, sizeof(spill), hash); + if(res != NULL) { + return; + } + + spill.ent = ent; + res = set_insert(env->spills, &spill, sizeof(spill), hash); + + // is 1 of the arguments a spill? + for(i = 0, arity = get_irn_arity(node); i < arity; ++i) { + ir_node* arg = get_irn_n(node, i); + collect(env, arg, reload, ent); + } +} + +static void collect(be_verify_spillslots_env_t *env, ir_node *node, ir_node *reload, ir_entity* ent) { + if(be_is_Spill(node)) { + collect_spill(env, node, reload, ent); + } else if(is_Proj(node)) { + collect_memperm(env, node, reload, ent); + } else if(is_Phi(node) && get_irn_mode(node) == mode_M) { + collect_memphi(env, node, reload, ent); + } else { + ir_fprintf(stderr, "Verify warning: No spill, memperm or memphi attached to node %+F found from node %+F in block %+F(%s)\n", + node, reload, get_nodes_block(node), get_irg_dump_name(env->irg)); + env->problem_found = 1; + } +} + +/** + * This walker function searches for reloads and collects all the spills + * and memphis attached to them. + */ +static void collect_spills_walker(ir_node *node, void *data) { + be_verify_spillslots_env_t *env = data; + const arch_env_t *arch_env = env->arch_env; + + // @@@ ia32_classify returns classification of Proj_pred :-/ + if(is_Proj(node)) + return; + + if(arch_irn_class_is(arch_env, node, reload)) { + ir_node *spill = get_memory_edge(node); + ir_entity *ent; + + if(spill == NULL) { + ir_fprintf(stderr, "Verify warning: No spill attached to reload %+F in block %+F(%s)\n", + node, get_nodes_block(node), get_irg_dump_name(env->irg)); + env->problem_found = 1; + return; + } + ent = arch_get_frame_entity(env->arch_env, node); + check_entity(env, node, ent); + + collect(env, spill, node, ent); + ARR_APP1(ir_node*, env->reloads, node); + } +} + +static void check_spillslot_interference(be_verify_spillslots_env_t *env) { + int spillcount = set_count(env->spills); + spill_t **spills = alloca(spillcount * sizeof(spills[0])); + spill_t *spill; + int i; + + for(spill = set_first(env->spills), i = 0; spill != NULL; spill = set_next(env->spills), ++i) { + spills[i] = spill; + } + + for(i = 0; i < spillcount; ++i) { + spill_t *sp1 = spills[i]; + int i2; + + for(i2 = i+1; i2 < spillcount; ++i2) { + spill_t *sp2 = spills[i2]; + + if(sp1->ent != sp2->ent) + continue; + + if(my_values_interfere(sp1->spill, sp2->spill)) { + ir_fprintf(stderr, "Verify warning: Spillslots for %+F in block %+F(%s) and %+F in block %+F(%s) interfere\n", + sp1->spill, get_nodes_block(sp1->spill), get_irg_dump_name(env->irg), + sp2->spill, get_nodes_block(sp2->spill), get_irg_dump_name(env->irg)); + env->problem_found = 1; + my_values_interfere(sp1->spill, sp2->spill); + } + } + } +} + +static void check_lonely_spills(ir_node *node, void *data) { + be_verify_spillslots_env_t *env = data; + + if(be_is_Spill(node) || (is_Proj(node) && be_is_MemPerm(get_Proj_pred(node)))) { + spill_t *spill = find_spill(env, node); + if(be_is_Spill(node)) { + ir_entity *ent = arch_get_frame_entity(env->arch_env, node); + check_entity(env, node, ent); + } + + if(spill == NULL) { + ir_fprintf(stderr, "Verify warning: Node %+F in block %+F(%s) not connected to a reaload\n", + node, get_nodes_block(node), get_irg_dump_name(env->irg)); + } + } +} + +int be_verify_spillslots(const arch_env_t *arch_env, ir_graph *irg) +{ + be_verify_spillslots_env_t env; + + env.arch_env = arch_env; + env.irg = irg; + 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; +} + + + +//--------------------------------------------------------------------------- + + + +typedef struct _be_verify_register_allocation_env_t { + const arch_env_t *arch_env; + ir_graph *irg; + be_lv_t *lv; + int problem_found; +} be_verify_register_allocation_env_t; + +static void check_register_constraints(ir_node *node, be_verify_register_allocation_env_t *env) { + const arch_env_t *arch_env = env->arch_env; + const arch_register_t *reg; + int i, arity; + + /* verify output register */ + if (arch_get_irn_reg_class(arch_env, node, -1) != NULL) { + reg = arch_get_irn_register(arch_env, 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(env->irg)); + env->problem_found = 1; + } + else if (! arch_register_type_is(reg, joker) && !arch_reg_is_allocatable(arch_env, node, -1, 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(env->irg)); + env->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_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(env->irg), i); + env->problem_found = 1; + continue; + } + + if (arch_get_irn_reg_class(arch_env, node, i) == NULL) + continue; + + reg = arch_get_irn_register(arch_env, pred); + if (reg == NULL) { + ir_fprintf(stderr, "Verify warning: Node %+F in block %+F(%s) should have a register assigned\n", + pred, get_nodes_block(pred), get_irg_dump_name(env->irg)); + env->problem_found = 1; + continue; + } + else if (! arch_register_type_is(reg, joker) && ! arch_reg_is_allocatable(arch_env, 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(env->irg)); + env->problem_found = 1; + } + } +} + +static void check_register_allocation(be_verify_register_allocation_env_t *env, + const arch_register_class_t *regclass, pset *nodes) { + const arch_env_t *arch_env = env->arch_env; + const arch_register_t *reg = NULL; + int fail = 0; + bitset_t *registers = bitset_alloca(arch_register_class_n_regs(regclass)); + ir_node *node; + + foreach_pset(nodes, node) { + if (arch_get_irn_reg_class(arch_env, node, -1) != regclass) + continue; + + reg = arch_get_irn_register(arch_env, node); + + /* this problem is already reported in 'check_register_constraints' */ + if (! reg) + continue; + + if (bitset_is_set(registers, reg->index)) { + pset_break(nodes); + fail = 1; + break; + } + bitset_set(registers, reg->index); + } + + if (fail) { + ir_fprintf(stderr, "Verify warning: Register %s assigned more than once in block %+F(%s)\n", + reg->name, get_nodes_block(node), get_irg_dump_name(env->irg)); + env->problem_found = 1; + + foreach_pset(nodes, node) { + if (arch_get_irn_register(arch_env, node) == reg) { + ir_fprintf(stderr, " at node %+F\n", node); + } + } + } +} + +static void verify_block_register_allocation(ir_node *block, void *data) { + be_verify_register_allocation_env_t *env = data; + const arch_env_t *arch_env = env->arch_env; + const arch_isa_t *isa = arch_env->isa; + int i, nregclasses; + + nregclasses = arch_isa_get_n_reg_class(isa); + for (i = 0; i < nregclasses; ++i) { + const arch_register_class_t *regclass = arch_isa_get_reg_class(isa, i); + ir_node *node; + pset *live_nodes = pset_new_ptr_default(); + + be_liveness_end_of_block(env->lv, env->arch_env, regclass, block, live_nodes); + check_register_allocation(env, regclass, live_nodes); + + sched_foreach_reverse(block, node) { + if (is_Phi(node)) + break; + + be_liveness_transfer(env->arch_env, regclass, node, live_nodes); + check_register_allocation(env, regclass, live_nodes); + check_register_constraints(node, env); + } + + del_pset(live_nodes); + } +} + +int be_verify_register_allocation(const arch_env_t *arch_env, ir_graph *irg) { + be_verify_register_allocation_env_t env; + + env.arch_env = arch_env; + env.irg = irg; + env.lv = be_liveness(irg); + env.problem_found = 0; + + irg_block_walk_graph(irg, verify_block_register_allocation, NULL, &env); + + be_liveness_free(env.lv); + + return !env.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(node)) + return; + mark_irn_visited(node); + + foreach_out_edge(node, edge) { + ir_node* src = get_edge_src_irn(edge); + + if(!bitset_is_set(env->reachable, get_irn_idx(src))) { + if(src != get_irg_globals(irg) + && src != get_irg_tls(irg)) { + 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; + } + + if(!is_Block(src)) { + 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 = 0; + + irg_walk_graph(irg, set_reachable, NULL, env.reachable); + inc_irg_visited(irg); + check_out_edges(get_irg_start(irg), &env); + + return !env.problem_found; +}