2 * Author: Matthias Braun
4 * Copyright: (c) Universitaet Karlsruhe
5 * License: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
14 #include "besched_t.h"
26 static int my_values_interfere(const ir_node *a, const ir_node *b);
28 typedef struct be_verify_register_pressure_env_t_ {
29 ir_graph *irg; /**< the irg to verify */
30 be_lv_t *lv; /**< Liveness information. */
31 const arch_env_t *arch_env; /**< an architecture environment */
32 const arch_register_class_t *cls; /**< the register class to check for */
33 int registers_available; /**< number of available registers */
34 int problem_found; /**< flag indicating if a problem was found */
35 } be_verify_register_pressure_env_t;
38 * Print all nodes of a pset into a file.
40 static void print_living_values(FILE *F, pset *live_nodes)
45 foreach_pset(live_nodes, node) {
46 ir_fprintf(F, "%+F ", node);
52 * Check if number of live nodes never exceeds the number of available registers.
54 static void verify_liveness_walker(ir_node *block, void *data)
56 be_verify_register_pressure_env_t *env = (be_verify_register_pressure_env_t *)data;
57 pset *live_nodes = pset_new_ptr_default();
61 /* collect register pressure info, start with end of a block */
62 be_liveness_end_of_block(env->lv, env->arch_env, env->cls, block, live_nodes);
64 pressure = pset_count(live_nodes);
65 if(pressure > env->registers_available) {
66 ir_fprintf(stderr, "Verify Warning: Register pressure too high at end of block %+F(%s) (%d/%d):\n",
67 block, get_irg_dump_name(env->irg), pressure, env->registers_available);
68 print_living_values(stderr, live_nodes);
69 env->problem_found = 1;
72 sched_foreach_reverse(block, irn) {
76 be_liveness_transfer(env->arch_env, env->cls, irn, live_nodes);
78 pressure = pset_count(live_nodes);
80 if(pressure > env->registers_available) {
81 ir_fprintf(stderr, "Verify Warning: Register pressure too high before node %+F in %+F(%s) (%d/%d):\n",
82 irn, block, get_irg_dump_name(env->irg), pressure, env->registers_available);
83 print_living_values(stderr, live_nodes);
84 env->problem_found = 1;
91 * Start a walk over the irg and check the register pressure.
93 int be_verify_register_pressure(const arch_env_t *arch_env, const arch_register_class_t *cls, ir_graph *irg)
95 be_verify_register_pressure_env_t env;
97 env.lv = be_liveness(irg);
99 env.arch_env = arch_env;
101 env.registers_available = arch_count_non_ignore_regs(arch_env, cls);
102 env.problem_found = 0;
104 irg_block_walk_graph(irg, verify_liveness_walker, NULL, &env);
105 be_liveness_free(env.lv);
107 return ! env.problem_found;
110 typedef struct be_verify_schedule_env_t_ {
111 int problem_found; /**< flags indicating if there was a problem */
112 ir_graph *irg; /**< the irg to check */
113 } be_verify_schedule_env_t;
116 * Simple schedule checker.
118 static void verify_schedule_walker(ir_node *block, void *data)
120 be_verify_schedule_env_t *env = (be_verify_schedule_env_t*) data;
122 int non_phi_found = 0;
123 int cfchange_found = 0;
124 // TODO ask arch about delay branches
125 int delay_branches = 0;
126 pset *uses = pset_new_ptr_default();
129 * Tests for the following things:
130 * 1. Make sure that all phi nodes are scheduled at the beginning of the block
131 * 2. There is 1 or no control flow changing node scheduled and exactly delay_branches operations after it.
132 * 3. No value is defined after it has been used
134 sched_foreach(block, node) {
140 ir_fprintf(stderr, "Verify Warning: Phi node %+F scheduled after non-Phi nodes in block %+F (%s)\n",
141 node, block, get_irg_dump_name(env->irg));
142 env->problem_found = 1;
148 // 2. Check for control flow changing nodes
149 if (is_cfop(node) && get_irn_opcode(node) != iro_Start) {
150 /* check, that only one CF operation is scheduled */
151 if (cfchange_found == 1) {
152 ir_fprintf(stderr, "Verify Warning: More than 1 control flow changing node (%+F) scheduled in block %+F (%s)\n",
153 node, block, get_irg_dump_name(env->irg));
154 env->problem_found = 1;
157 } else if (cfchange_found) {
158 // proj and keepany aren't real instructions...
159 if(!is_Proj(node) && !be_is_Keep(node)) {
160 /* check for delay branches */
161 if (delay_branches == 0) {
162 ir_fprintf(stderr, "Verify Warning: Node %+F scheduled after control flow changing node (+delay branches) in block %+F (%s)\n",
163 node, block, get_irg_dump_name(env->irg));
164 env->problem_found = 1;
172 if(pset_find_ptr(uses, node)) {
173 ir_fprintf(stderr, "Verify Warning: Value %+F used before it was defined in block %+F (%s)\n",
174 node, block, get_irg_dump_name(env->irg));
175 env->problem_found = 1;
178 for(i = 0, arity = get_irn_arity(node); i < arity; ++i) {
179 pset_insert_ptr(uses, get_irn_n(node, i));
185 /* check that all delay branches are filled (at least with NOPs) */
186 if (cfchange_found && delay_branches != 0) {
187 ir_fprintf(stderr, "Verify warning: Not all delay slots filled after jump (%d/%d) in block %+F (%s)\n",
188 block, get_irg_dump_name(env->irg));
189 env->problem_found = 1;
193 static int should_be_scheduled(ir_node *node) {
197 if(get_irn_mode(node) == mode_M) {
200 if(is_Phi(node) || is_Sync(node) || get_irn_opcode(node) == iro_Pin)
203 if(is_Proj(node) && get_irn_mode(node) == mode_X)
205 if(be_is_Keep(node) && get_irn_opcode(get_nodes_block(node)) == iro_Bad)
208 switch(get_irn_opcode(node)) {
220 static void check_schedule(ir_node *node, void *data) {
221 be_verify_schedule_env_t *env = data;
224 should_be = should_be_scheduled(node);
228 if(should_be ? !sched_is_scheduled(node) : sched_is_scheduled(node)) {
229 ir_fprintf(stderr, "Verify warning: Node %+F in block %+F(%s) should%s be scheduled\n",
230 node, get_nodes_block(node), get_irg_dump_name(env->irg), should_be ? "" : " not");
231 env->problem_found = 1;
236 * Start a walk over the irg and check schedule.
238 int be_verify_schedule(ir_graph *irg)
240 be_verify_schedule_env_t env;
242 env.problem_found = 0;
245 irg_block_walk_graph(irg, verify_schedule_walker, NULL, &env);
246 // check if all nodes are scheduled
247 irg_walk_graph(irg, check_schedule, NULL, &env);
249 return ! env.problem_found;
254 //---------------------------------------------------------------------------
258 typedef struct _spill_t {
269 } be_verify_spillslots_env_t;
271 static int cmp_spill(const void* d1, const void* d2, size_t size) {
272 const spill_t* s1 = d1;
273 const spill_t* s2 = d2;
274 return s1->spill != s2->spill;
277 static spill_t *get_spill(be_verify_spillslots_env_t *env, ir_node *node, entity *ent) {
279 int hash = HASH_PTR(node);
282 res = set_find(env->spills, &spill, sizeof(spill), hash);
286 res = set_insert(env->spills, &spill, sizeof(spill), hash);
292 static void collect(be_verify_spillslots_env_t *env, ir_node *node, ir_node *reload, entity* ent);
294 static void collect_spill(be_verify_spillslots_env_t *env, ir_node *node, ir_node *reload, entity* ent) {
295 entity *spillent = be_get_frame_entity(node);
296 get_spill(env, node, ent);
298 if(spillent != ent) {
299 ir_fprintf(stderr, "Verify warning: Spill %+F has different entity than reload %+F in block %+F(%s)\n",
300 node, reload, get_nodes_block(node), get_irg_dump_name(env->irg));
301 env->problem_found = 1;
305 static void collect_memperm(be_verify_spillslots_env_t *env, ir_node *node, ir_node *reload, entity* ent) {
308 int hash = HASH_PTR(node);
313 assert(is_Proj(node));
315 memperm = get_Proj_pred(node);
316 out = get_Proj_proj(node);
318 spillent = be_get_MemPerm_out_entity(memperm, out);
319 if(spillent != ent) {
320 ir_fprintf(stderr, "Verify warning: MemPerm %+F has different entity than reload %+F in block %+F(%s)\n",
321 node, reload, get_nodes_block(node), get_irg_dump_name(env->irg));
322 env->problem_found = 1;
326 res = set_find(env->spills, &spill, sizeof(spill), hash);
331 spill.ent = spillent;
332 res = set_insert(env->spills, &spill, sizeof(spill), hash);
334 for(i = 0, arity = get_irn_arity(memperm); i < arity; ++i) {
335 ir_node* arg = get_irn_n(memperm, i);
336 entity* argent = be_get_MemPerm_in_entity(memperm, i);
338 collect(env, arg, memperm, argent);
342 static void collect_memphi(be_verify_spillslots_env_t *env, ir_node *node, ir_node *reload, entity *ent) {
345 int hash = HASH_PTR(node);
347 assert(is_Phi(node));
350 res = set_find(env->spills, &spill, sizeof(spill), hash);
356 res = set_insert(env->spills, &spill, sizeof(spill), hash);
358 // is 1 of the arguments a spill?
359 for(i = 0, arity = get_irn_arity(node); i < arity; ++i) {
360 ir_node* arg = get_irn_n(node, i);
361 collect(env, arg, reload, ent);
365 static void collect(be_verify_spillslots_env_t *env, ir_node *node, ir_node *reload, entity* ent) {
366 if(be_is_Spill(node)) {
367 collect_spill(env, node, reload, ent);
368 } else if(is_Proj(node)) {
369 collect_memperm(env, node, reload, ent);
370 } else if(is_Phi(node) && get_irn_mode(node) == mode_M) {
371 collect_memphi(env, node, reload, ent);
373 ir_fprintf(stderr, "Verify warning: No spill, memperm or memphi attached to node %+F found from node %+F in block %+F(%s)\n",
374 node, reload, get_nodes_block(node), get_irg_dump_name(env->irg));
375 env->problem_found = 1;
380 * This walker function searches for reloads and collects all the spills
381 * and memphis attached to them.
383 static void collect_spills_walker(ir_node *node, void *data) {
384 be_verify_spillslots_env_t *env = data;
386 if(be_is_Reload(node)) {
387 ir_node *spill = get_irn_n(node, be_pos_Reload_mem);
388 entity* ent = be_get_frame_entity(node);
390 collect(env, spill, node, ent);
391 ARR_APP1(ir_node*, env->reloads, node);
395 static void check_spillslot_interference(be_verify_spillslots_env_t *env) {
396 int spillcount = set_count(env->spills);
397 spill_t **spills = alloca(spillcount * sizeof(spills[0]));
401 for(spill = set_first(env->spills), i = 0; spill != NULL; spill = set_next(env->spills), ++i) {
405 for(i = 0; i < spillcount; ++i) {
406 spill_t *sp1 = spills[i];
409 for(i2 = i+1; i2 < spillcount; ++i2) {
410 spill_t *sp2 = spills[i2];
412 if(sp1->ent != sp2->ent)
415 if(my_values_interfere(sp1->spill, sp2->spill)) {
416 ir_fprintf(stderr, "Verify warning: Spillslots for %+F in block %+F(%s) and %+F in block %+F(%s) interfere\n",
417 sp1->spill, get_nodes_block(sp1->spill), get_irg_dump_name(env->irg),
418 sp2->spill, get_nodes_block(sp2->spill), get_irg_dump_name(env->irg));
419 env->problem_found = 1;
420 my_values_interfere(sp1->spill, sp2->spill);
421 printf("Intf: %d\n", values_interfere(env->lv, sp1->spill, sp2->spill));
427 int be_verify_spillslots(ir_graph *irg)
429 be_verify_spillslots_env_t env;
432 env.spills = new_set(cmp_spill, 10);
433 env.reloads = NEW_ARR_F(ir_node*, 0);
434 env.problem_found = 0;
435 env.lv = be_liveness(irg);
437 irg_walk_graph(irg, collect_spills_walker, NULL, &env);
439 check_spillslot_interference(&env);
441 be_liveness_free(env.lv);
442 DEL_ARR_F(env.reloads);
445 return ! env.problem_found;
450 //---------------------------------------------------------------------------
455 * Check, if two values interfere.
456 * @param a The first value.
457 * @param b The second value.
458 * @return 1, if a and b interfere, 0 if not.
460 static int my_values_interfere(const ir_node *a, const ir_node *b)
462 const ir_edge_t *edge;
464 int a2b = value_dominates(a, b);
465 int b2a = value_dominates(b, a);
467 /* If there is no dominance relation, they do not interfere. */
472 * Adjust a and b so, that a dominates b if
473 * a dominates b or vice versa.
476 const ir_node *t = a;
481 bb = get_nodes_block(b);
484 * Look at all usages of a.
485 * If there's one usage of a in the block of b, then
486 * we check, if this use is dominated by b, if that's true
487 * a and b interfere. Note that b must strictly dominate the user,
488 * since if b is the last user of in the block, b and a do not
490 * Uses of a not in b's block can be disobeyed, because the
491 * check for a being live at the end of b's block is already
494 foreach_out_edge(a, edge) {
495 const ir_node *user = get_edge_src_irn(edge);
499 if(get_irn_opcode(user) == iro_End)
502 // in case of phi arguments we compare with the block the value comes from
504 ir_node *phiblock = get_nodes_block(user);
507 user = get_irn_n(phiblock, get_edge_src_pos(edge));
510 if(value_dominates(b, user))