2 * Author: Matthias Braun
4 * Copyright: (c) Universitaet Karlsruhe
5 * License: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
25 #include "besched_t.h"
28 static int my_values_interfere(const ir_node *a, const ir_node *b);
30 typedef struct be_verify_register_pressure_env_t_ {
31 ir_graph *irg; /**< the irg to verify */
32 be_lv_t *lv; /**< Liveness information. */
33 const arch_env_t *arch_env; /**< an architecture environment */
34 const arch_register_class_t *cls; /**< the register class to check for */
35 int registers_available; /**< number of available registers */
36 int problem_found; /**< flag indicating if a problem was found */
37 } be_verify_register_pressure_env_t;
40 * Print all nodes of a pset into a file.
42 static void print_living_values(FILE *F, pset *live_nodes) {
46 foreach_pset(live_nodes, node) {
47 ir_fprintf(F, "%+F ", node);
53 * Check if number of live nodes never exceeds the number of available registers.
55 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) {
94 be_verify_register_pressure_env_t env;
96 env.lv = be_liveness(irg);
98 env.arch_env = arch_env;
100 env.registers_available = arch_count_non_ignore_regs(arch_env, cls);
101 env.problem_found = 0;
103 irg_block_walk_graph(irg, verify_liveness_walker, NULL, &env);
104 be_liveness_free(env.lv);
106 return ! env.problem_found;
109 typedef struct be_verify_schedule_env_t_ {
110 int problem_found; /**< flags indicating if there was a problem */
111 ir_graph *irg; /**< the irg to check */
112 } be_verify_schedule_env_t;
115 * Simple schedule checker.
117 static void verify_schedule_walker(ir_node *block, void *data) {
118 be_verify_schedule_env_t *env = (be_verify_schedule_env_t*) data;
120 int non_phi_found = 0;
121 int cfchange_found = 0;
122 // TODO ask arch about delay branches
123 int delay_branches = 0;
124 pset *uses = pset_new_ptr_default();
127 * Tests for the following things:
128 * 1. Make sure that all phi nodes are scheduled at the beginning of the block
129 * 2. There is 1 or no control flow changing node scheduled and exactly delay_branches operations after it.
130 * 3. No value is defined after it has been used
132 sched_foreach(block, node) {
138 ir_fprintf(stderr, "Verify Warning: Phi node %+F scheduled after non-Phi nodes in block %+F (%s)\n",
139 node, block, get_irg_dump_name(env->irg));
140 env->problem_found = 1;
146 // 2. Check for control flow changing nodes
147 if (is_cfop(node) && get_irn_opcode(node) != iro_Start) {
148 /* check, that only one CF operation is scheduled */
149 if (cfchange_found == 1) {
150 ir_fprintf(stderr, "Verify Warning: More than 1 control flow changing node (%+F) scheduled in block %+F (%s)\n",
151 node, block, get_irg_dump_name(env->irg));
152 env->problem_found = 1;
155 } else if (cfchange_found) {
156 // proj and keepany aren't real instructions...
157 if(!is_Proj(node) && !be_is_Keep(node)) {
158 /* check for delay branches */
159 if (delay_branches == 0) {
160 ir_fprintf(stderr, "Verify Warning: Node %+F scheduled after control flow changing node (+delay branches) in block %+F (%s)\n",
161 node, block, get_irg_dump_name(env->irg));
162 env->problem_found = 1;
170 if(pset_find_ptr(uses, node)) {
171 ir_fprintf(stderr, "Verify Warning: Value %+F used before it was defined in block %+F (%s)\n",
172 node, block, get_irg_dump_name(env->irg));
173 env->problem_found = 1;
176 for(i = 0, arity = get_irn_arity(node); i < arity; ++i) {
177 pset_insert_ptr(uses, get_irn_n(node, i));
183 /* check that all delay branches are filled (at least with NOPs) */
184 if (cfchange_found && delay_branches != 0) {
185 ir_fprintf(stderr, "Verify warning: Not all delay slots filled after jump (%d/%d) in block %+F (%s)\n",
186 block, get_irg_dump_name(env->irg));
187 env->problem_found = 1;
191 static int should_be_scheduled(ir_node *node) {
195 if(get_irn_mode(node) == mode_M) {
198 if(is_Phi(node) || is_Sync(node) || get_irn_opcode(node) == iro_Pin)
201 if(is_Proj(node) && get_irn_mode(node) == mode_X)
203 if(be_is_Keep(node) && get_irn_opcode(get_nodes_block(node)) == iro_Bad)
206 switch(get_irn_opcode(node)) {
218 static void check_schedule(ir_node *node, void *data) {
219 be_verify_schedule_env_t *env = data;
222 should_be = should_be_scheduled(node);
226 if(should_be ? !sched_is_scheduled(node) : sched_is_scheduled(node)) {
227 ir_fprintf(stderr, "Verify warning: Node %+F in block %+F(%s) should%s be scheduled\n",
228 node, get_nodes_block(node), get_irg_dump_name(env->irg), should_be ? "" : " not");
229 env->problem_found = 1;
234 * Start a walk over the irg and check schedule.
236 int be_verify_schedule(ir_graph *irg)
238 be_verify_schedule_env_t env;
240 env.problem_found = 0;
243 irg_block_walk_graph(irg, verify_schedule_walker, NULL, &env);
244 // check if all nodes are scheduled
245 irg_walk_graph(irg, check_schedule, NULL, &env);
247 return ! env.problem_found;
252 //---------------------------------------------------------------------------
256 typedef struct _spill_t {
267 } be_verify_spillslots_env_t;
269 static int cmp_spill(const void* d1, const void* d2, size_t size) {
270 const spill_t* s1 = d1;
271 const spill_t* s2 = d2;
272 return s1->spill != s2->spill;
275 static spill_t *find_spill(be_verify_spillslots_env_t *env, ir_node *node) {
279 return set_find(env->spills, &spill, sizeof(spill), HASH_PTR(node));
282 static spill_t *get_spill(be_verify_spillslots_env_t *env, ir_node *node, entity *ent) {
284 int hash = HASH_PTR(node);
287 res = set_find(env->spills, &spill, sizeof(spill), hash);
291 res = set_insert(env->spills, &spill, sizeof(spill), hash);
297 static void collect(be_verify_spillslots_env_t *env, ir_node *node, ir_node *reload, entity* ent);
299 static void check_entity(be_verify_spillslots_env_t *env, ir_node *node, entity *ent) {
301 ir_fprintf(stderr, "Verify warning: Node %+F in block %+F(%s) should have an entity assigned\n",
302 node, get_nodes_block(node), get_irg_dump_name(env->irg));
306 static void collect_spill(be_verify_spillslots_env_t *env, ir_node *node, ir_node *reload, entity* ent) {
307 entity *spillent = be_get_frame_entity(node);
308 check_entity(env, node, spillent);
309 get_spill(env, node, ent);
311 if(spillent != ent) {
312 ir_fprintf(stderr, "Verify warning: Spill %+F has different entity than reload %+F in block %+F(%s)\n",
313 node, reload, get_nodes_block(node), get_irg_dump_name(env->irg));
314 env->problem_found = 1;
318 static void collect_memperm(be_verify_spillslots_env_t *env, ir_node *node, ir_node *reload, entity* ent) {
321 int hash = HASH_PTR(node);
326 assert(is_Proj(node));
328 memperm = get_Proj_pred(node);
329 out = get_Proj_proj(node);
331 spillent = be_get_MemPerm_out_entity(memperm, out);
332 check_entity(env, memperm, spillent);
333 if(spillent != ent) {
334 ir_fprintf(stderr, "Verify warning: MemPerm %+F has different entity than reload %+F in block %+F(%s)\n",
335 node, reload, get_nodes_block(node), get_irg_dump_name(env->irg));
336 env->problem_found = 1;
340 res = set_find(env->spills, &spill, sizeof(spill), hash);
345 spill.ent = spillent;
346 res = set_insert(env->spills, &spill, sizeof(spill), hash);
348 for(i = 0, arity = be_get_MemPerm_entity_arity(memperm); i < arity; ++i) {
349 ir_node* arg = get_irn_n(memperm, i + 1);
350 entity* argent = be_get_MemPerm_in_entity(memperm, i);
352 collect(env, arg, memperm, argent);
356 static void collect_memphi(be_verify_spillslots_env_t *env, ir_node *node, ir_node *reload, entity *ent) {
359 int hash = HASH_PTR(node);
361 assert(is_Phi(node));
364 res = set_find(env->spills, &spill, sizeof(spill), hash);
370 res = set_insert(env->spills, &spill, sizeof(spill), hash);
372 // is 1 of the arguments a spill?
373 for(i = 0, arity = get_irn_arity(node); i < arity; ++i) {
374 ir_node* arg = get_irn_n(node, i);
375 collect(env, arg, reload, ent);
379 static void collect(be_verify_spillslots_env_t *env, ir_node *node, ir_node *reload, entity* ent) {
380 if(be_is_Spill(node)) {
381 collect_spill(env, node, reload, ent);
382 } else if(is_Proj(node)) {
383 collect_memperm(env, node, reload, ent);
384 } else if(is_Phi(node) && get_irn_mode(node) == mode_M) {
385 collect_memphi(env, node, reload, ent);
387 ir_fprintf(stderr, "Verify warning: No spill, memperm or memphi attached to node %+F found from node %+F in block %+F(%s)\n",
388 node, reload, get_nodes_block(node), get_irg_dump_name(env->irg));
389 env->problem_found = 1;
394 * This walker function searches for reloads and collects all the spills
395 * and memphis attached to them.
397 static void collect_spills_walker(ir_node *node, void *data) {
398 be_verify_spillslots_env_t *env = data;
400 if(be_is_Reload(node)) {
401 ir_node *spill = get_irn_n(node, be_pos_Reload_mem);
402 entity* ent = be_get_frame_entity(node);
403 check_entity(env, node, ent);
405 collect(env, spill, node, ent);
406 ARR_APP1(ir_node*, env->reloads, node);
410 static void check_spillslot_interference(be_verify_spillslots_env_t *env) {
411 int spillcount = set_count(env->spills);
412 spill_t **spills = alloca(spillcount * sizeof(spills[0]));
416 for(spill = set_first(env->spills), i = 0; spill != NULL; spill = set_next(env->spills), ++i) {
420 for(i = 0; i < spillcount; ++i) {
421 spill_t *sp1 = spills[i];
424 for(i2 = i+1; i2 < spillcount; ++i2) {
425 spill_t *sp2 = spills[i2];
427 if(sp1->ent != sp2->ent)
430 if(my_values_interfere(sp1->spill, sp2->spill)) {
431 ir_fprintf(stderr, "Verify warning: Spillslots for %+F in block %+F(%s) and %+F in block %+F(%s) interfere\n",
432 sp1->spill, get_nodes_block(sp1->spill), get_irg_dump_name(env->irg),
433 sp2->spill, get_nodes_block(sp2->spill), get_irg_dump_name(env->irg));
434 env->problem_found = 1;
435 my_values_interfere(sp1->spill, sp2->spill);
436 printf("Intf: %d\n", values_interfere(env->lv, sp1->spill, sp2->spill));
442 static void check_lonely_spills(ir_node *node, void *data) {
443 be_verify_spillslots_env_t *env = data;
445 if(be_is_Spill(node) || (is_Proj(node) && be_is_MemPerm(get_Proj_pred(node)))) {
446 spill_t *spill = find_spill(env, node);
447 if(be_is_Spill(node)) {
448 entity *ent = be_get_frame_entity(node);
449 check_entity(env, node, ent);
453 ir_fprintf(stderr, "Verify warning: Node %+F in block %+F(%s) not connected to a reaload\n",
454 node, get_nodes_block(node), get_irg_dump_name(env->irg));
459 int be_verify_spillslots(ir_graph *irg)
461 be_verify_spillslots_env_t env;
464 env.spills = new_set(cmp_spill, 10);
465 env.reloads = NEW_ARR_F(ir_node*, 0);
466 env.problem_found = 0;
467 env.lv = be_liveness(irg);
469 irg_walk_graph(irg, collect_spills_walker, NULL, &env);
470 irg_walk_graph(irg, check_lonely_spills, NULL, &env);
472 check_spillslot_interference(&env);
474 be_liveness_free(env.lv);
475 DEL_ARR_F(env.reloads);
478 return ! env.problem_found;
483 //---------------------------------------------------------------------------
488 * Check, if two values interfere.
489 * @param a The first value.
490 * @param b The second value.
491 * @return 1, if a and b interfere, 0 if not.
493 static int my_values_interfere(const ir_node *a, const ir_node *b) {
494 const ir_edge_t *edge;
496 int a2b = value_dominates(a, b);
497 int b2a = value_dominates(b, a);
499 /* If there is no dominance relation, they do not interfere. */
504 * Adjust a and b so, that a dominates b if
505 * a dominates b or vice versa.
508 const ir_node *t = a;
513 bb = get_nodes_block(b);
516 * Look at all usages of a.
517 * If there's one usage of a in the block of b, then
518 * we check, if this use is dominated by b, if that's true
519 * a and b interfere. Note that b must strictly dominate the user,
520 * since if b is the last user of in the block, b and a do not
522 * Uses of a not in b's block can be disobeyed, because the
523 * check for a being live at the end of b's block is already
526 foreach_out_edge(a, edge) {
527 const ir_node *user = get_edge_src_irn(edge);
531 if(get_irn_opcode(user) == iro_End)
534 // in case of phi arguments we compare with the block the value comes from
536 ir_node *phiblock = get_nodes_block(user);
539 user = get_irn_n(phiblock, get_edge_src_pos(edge));
542 if(value_dominates(b, user))
551 //---------------------------------------------------------------------------
555 typedef struct _be_verify_register_allocation_env_t {
556 const arch_env_t *arch_env;
560 } be_verify_register_allocation_env_t;
562 static void check_register_allocation(be_verify_register_allocation_env_t *env,
563 const arch_register_class_t *regclass, pset *nodes) {
564 const arch_env_t *arch_env = env->arch_env;
567 bitset_t *registers = bitset_alloca(arch_register_class_n_regs(regclass));
569 foreach_pset(nodes, node) {
570 const arch_register_t *reg;
571 if(arch_get_irn_reg_class(arch_env, node, -1) != regclass)
574 reg = arch_get_irn_register(arch_env, node);
576 ir_fprintf(stderr, "Verify warning: Node %+F in block %+F(%s) should have a register assigned\n",
577 node, get_nodes_block(node), get_irg_dump_name(env->irg));
578 env->problem_found = 1;
581 if(bitset_is_set(registers, reg->index)) {
582 ir_fprintf(stderr, "Verify warning: Register %s assigned more than once at node %+F in block %+F(%s)\n",
583 reg->name, node, get_nodes_block(node), get_irg_dump_name(env->irg));
584 env->problem_found = 1;
587 bitset_set(registers, reg->index);
591 static void verify_block_register_allocation(ir_node *block, void *data) {
592 be_verify_register_allocation_env_t *env = data;
593 const arch_env_t *arch_env = env->arch_env;
594 const arch_isa_t *isa = arch_env->isa;
597 nregclasses = arch_isa_get_n_reg_class(isa);
598 for (i = 0; i < nregclasses; ++i) {
599 const arch_register_class_t *regclass = arch_isa_get_reg_class(isa, i);
601 pset *live_nodes = pset_new_ptr_default();
603 be_liveness_end_of_block(env->lv, env->arch_env, regclass, block, live_nodes);
604 check_register_allocation(env, regclass, live_nodes);
606 sched_foreach_reverse(block, node) {
610 be_liveness_transfer(env->arch_env, regclass, node, live_nodes);
611 check_register_allocation(env, regclass, live_nodes);
614 del_pset(live_nodes);
618 int be_verify_register_allocation(const arch_env_t *arch_env, ir_graph *irg) {
619 be_verify_register_allocation_env_t env;
621 env.arch_env = arch_env;
623 env.lv = be_liveness(irg);
624 env.problem_found = 0;
626 irg_block_walk_graph(irg, verify_block_register_allocation, NULL, &env);
628 be_liveness_free(env.lv);
630 return !env.problem_found;