#include "irgwalk.h"
#include "irprintf.h"
#include "irdump_t.h"
+#include "iredges.h"
+#include "set.h"
+#include "array.h"
+#include "benode_t.h"
+
+static int my_value_dominates(const ir_node *a, const ir_node *b);
+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. */
const arch_env_t *arch_env; /**< an architecture environment */
const arch_register_class_t *cls; /**< the register class to check for */
int registers_available; /**< number of available registers */
int pressure;
/* collect register pressure info, start with end of a block */
- be_liveness_end_of_block(env->arch_env, env->cls, block, live_nodes);
+ be_liveness_end_of_block(env->lv, env->arch_env, env->cls, block, live_nodes);
pressure = pset_count(live_nodes);
if(pressure > env->registers_available) {
{
be_verify_register_pressure_env_t env;
- be_liveness(irg);
-
+ env.lv = be_liveness(irg);
env.irg = irg;
env.arch_env = arch_env;
env.cls = cls;
env.problem_found = 0;
irg_block_walk_graph(irg, verify_liveness_walker, NULL, &env);
+ be_liveness_free(env.lv);
return ! env.problem_found;
}
}
cfchange_found = 1;
} else if (cfchange_found) {
- /* check for delay branches */
- if (delay_branches == 0) {
- ir_fprintf(stderr, "Verify Warning: Node %+F scheduled after control flow changing node (+delay branches) in block %+F (%s)\n",
- node, block, get_irg_dump_name(env->irg));
- env->problem_found = 1;
- } else {
- delay_branches--;
+ // proj and keepany aren't real instructions...
+ if(!is_Proj(node) && !be_is_Keep(node)) {
+ /* check for delay branches */
+ if (delay_branches == 0) {
+ ir_fprintf(stderr, "Verify Warning: Node %+F scheduled after control flow changing node (+delay branches) in block %+F (%s)\n",
+ node, block, get_irg_dump_name(env->irg));
+ env->problem_found = 1;
+ } else {
+ delay_branches--;
+ }
}
}
return ! env.problem_found;
}
+
+
+
+//---------------------------------------------------------------------------
+
+
+
+typedef struct _spill_t {
+ ir_node *spill;
+ entity *ent;
+} spill_t;
+
+typedef struct {
+ 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 *get_spill(be_verify_spillslots_env_t *env, ir_node *node, 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 void collect(be_verify_spillslots_env_t *env, ir_node *node, ir_node *reload, entity* ent);
+
+static void collect_spill(be_verify_spillslots_env_t *env, ir_node *node, ir_node *reload, entity* ent) {
+ entity *spillent = be_get_frame_entity(node);
+ 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, entity* ent) {
+ int i, arity;
+ spill_t spill, *res;
+ int hash = HASH_PTR(node);
+ int out;
+ ir_node* memperm;
+ entity *spillent;
+
+ assert(is_Proj(node));
+
+ memperm = get_Proj_pred(node);
+ out = get_Proj_proj(node);
+
+ spillent = be_get_MemPerm_out_entity(memperm, out);
+ 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 = get_irn_arity(memperm); i < arity; ++i) {
+ ir_node* arg = get_irn_n(memperm, i);
+ 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, 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, 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;
+
+ if(be_is_Reload(node)) {
+ ir_node *spill = get_irn_n(node, be_pos_Reload_mem);
+ entity* ent = be_get_frame_entity(node);
+
+ 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;
+ }
+ }
+ }
+}
+
+int be_verify_spillslots(ir_graph *irg)
+{
+ be_verify_spillslots_env_t 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);
+
+ check_spillslot_interference(&env);
+
+ DEL_ARR_F(env.reloads);
+ del_set(env.spills);
+
+ return ! env.problem_found;
+}
+
+
+
+//---------------------------------------------------------------------------
+
+
+
+static sched_timestep_t get_time_step(const ir_node *irn)
+{
+ if(is_Phi(irn))
+ return 0;
+
+ return sched_get_time_step(irn);
+}
+
+static int my_value_dominates(const ir_node *a, const ir_node *b)
+{
+ int res = 0;
+ const ir_node *ba = get_block(a);
+ const ir_node *bb = get_block(b);
+
+ /*
+ * a and b are not in the same block,
+ * so dominance is determined by the dominance of the blocks.
+ */
+ if(ba != bb) {
+ res = block_dominates(ba, bb);
+
+ /*
+ * Dominance is determined by the time steps of the schedule.
+ */
+ } else {
+ sched_timestep_t as = get_time_step(a);
+ sched_timestep_t bs = get_time_step(b);
+ res = as <= bs;
+ }
+
+ return res;
+}
+
+/**
+ * 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 = my_value_dominates(a, b);
+ int b2a = my_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;
+
+ // 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);
+ user = get_irn_n(phiblock, get_edge_src_pos(edge));
+ }
+
+ if(my_value_dominates(b, user))
+ return 1;
+ }
+
+ return 0;
+}