2 * Author: Matthias Braun
4 * Copyright: (c) Universitaet Karlsruhe
5 * License: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
27 #include "besched_t.h"
30 static int my_values_interfere(const ir_node *a, const ir_node *b);
32 typedef struct be_verify_register_pressure_env_t_ {
33 ir_graph *irg; /**< the irg to verify */
34 be_lv_t *lv; /**< Liveness information. */
35 const arch_env_t *arch_env; /**< an architecture environment */
36 const arch_register_class_t *cls; /**< the register class to check for */
37 int registers_available; /**< number of available registers */
38 int problem_found; /**< flag indicating if a problem was found */
39 } be_verify_register_pressure_env_t;
42 * Print all nodes of a pset into a file.
44 static void print_living_values(FILE *F, pset *live_nodes) {
48 foreach_pset(live_nodes, node) {
49 ir_fprintf(F, "%+F ", node);
55 * Check if number of live nodes never exceeds the number of available registers.
57 static void verify_liveness_walker(ir_node *block, void *data) {
58 be_verify_register_pressure_env_t *env = (be_verify_register_pressure_env_t *)data;
59 pset *live_nodes = pset_new_ptr_default();
63 /* collect register pressure info, start with end of a block */
64 be_liveness_end_of_block(env->lv, env->arch_env, env->cls, block, live_nodes);
66 pressure = pset_count(live_nodes);
67 if(pressure > env->registers_available) {
68 ir_fprintf(stderr, "Verify Warning: Register pressure too high at end of block %+F(%s) (%d/%d):\n",
69 block, get_irg_dump_name(env->irg), pressure, env->registers_available);
70 print_living_values(stderr, live_nodes);
71 env->problem_found = 1;
74 sched_foreach_reverse(block, irn) {
78 be_liveness_transfer(env->arch_env, env->cls, irn, live_nodes);
80 pressure = pset_count(live_nodes);
82 if(pressure > env->registers_available) {
83 ir_fprintf(stderr, "Verify Warning: Register pressure too high before node %+F in %+F(%s) (%d/%d):\n",
84 irn, block, get_irg_dump_name(env->irg), pressure, env->registers_available);
85 print_living_values(stderr, live_nodes);
86 env->problem_found = 1;
93 * Start a walk over the irg and check the register pressure.
95 int be_verify_register_pressure(const be_irg_t *birg, const arch_register_class_t *cls, ir_graph *irg) {
96 be_verify_register_pressure_env_t env;
98 env.lv = be_liveness(irg);
100 env.arch_env = birg->main_env->arch_env;
102 env.registers_available = env.cls->n_regs - be_put_ignore_regs(birg, env.cls, NULL);
103 env.problem_found = 0;
105 irg_block_walk_graph(irg, verify_liveness_walker, NULL, &env);
106 be_liveness_free(env.lv);
108 return ! env.problem_found;
111 typedef struct be_verify_schedule_env_t_ {
112 int problem_found; /**< flags indicating if there was a problem */
113 bitset_t *scheduled; /**< bitset of scheduled nodes */
114 ir_graph *irg; /**< the irg to check */
115 } be_verify_schedule_env_t;
118 * Simple schedule checker.
120 static void verify_schedule_walker(ir_node *block, void *data) {
121 be_verify_schedule_env_t *env = (be_verify_schedule_env_t*) data;
123 int non_phi_found = 0;
124 int cfchange_found = 0;
125 // TODO ask arch about delay branches
126 int delay_branches = 0;
127 int last_timestep = INT_MIN;
130 * Tests for the following things:
131 * 1. Make sure that all phi nodes are scheduled at the beginning of the block
132 * 2. There is 1 or no control flow changing node scheduled and exactly delay_branches operations after it.
133 * 3. No value is defined after it has been used
135 sched_foreach(block, node) {
139 // this node is scheduled
140 if(bitset_is_set(env->scheduled, get_irn_idx(node))) {
141 ir_fprintf(stderr, "Verify warning: %+F appears to be schedule twice\n");
142 env->problem_found = 1;
144 bitset_set(env->scheduled, get_irn_idx(node));
146 // Check that scheduled nodes are in the correct block
147 if(get_nodes_block(node) != block) {
148 ir_fprintf(stderr, "Verify warning: %+F is in block %+F but scheduled in %+F\n", node, get_nodes_block(node), block);
149 env->problem_found = 1;
152 // Check that timesteps are increasing
153 timestep = sched_get_time_step(node);
154 if(timestep <= last_timestep) {
155 ir_fprintf(stderr, "Verify warning: Schedule timestep did not increase at node %+F\n",
157 env->problem_found = 1;
159 last_timestep = timestep;
161 // Check that phis come before any other node
164 ir_fprintf(stderr, "Verify Warning: Phi node %+F scheduled after non-Phi nodes in block %+F (%s)\n",
165 node, block, get_irg_dump_name(env->irg));
166 env->problem_found = 1;
172 // Check for control flow changing nodes
173 if (is_cfop(node) && get_irn_opcode(node) != iro_Start) {
174 /* check, that only one CF operation is scheduled */
175 if (cfchange_found == 1) {
176 ir_fprintf(stderr, "Verify Warning: More than 1 control flow changing node (%+F) scheduled in block %+F (%s)\n",
177 node, block, get_irg_dump_name(env->irg));
178 env->problem_found = 1;
181 } else if (cfchange_found) {
182 // proj and keepany aren't real instructions...
183 if(!is_Proj(node) && !be_is_Keep(node)) {
184 /* check for delay branches */
185 if (delay_branches == 0) {
186 ir_fprintf(stderr, "Verify Warning: Node %+F scheduled after control flow changing node (+delay branches) in block %+F (%s)\n",
187 node, block, get_irg_dump_name(env->irg));
188 env->problem_found = 1;
195 // Check that all uses come before their definitions
197 int nodetime = sched_get_time_step(node);
198 for(i = 0, arity = get_irn_arity(node); i < arity; ++i) {
199 ir_node *arg = get_irn_n(node, i);
200 if(get_nodes_block(arg) != block
201 || !sched_is_scheduled(arg))
204 if(sched_get_time_step(arg) >= nodetime) {
205 ir_fprintf(stderr, "Verify Warning: Value %+F used by %+F before it was defined in block %+F (%s)\n",
206 arg, node, block, get_irg_dump_name(env->irg));
207 env->problem_found = 1;
212 // Check that no dead nodes are scheduled
213 if(get_irn_n_edges(node) == 0) {
214 ir_fprintf(stderr, "Verify warning: Node %+F is dead but scheduled in block %+F (%s)\n",
215 node, block, get_irg_dump_name(env->irg));
216 env->problem_found = 1;
220 /* check that all delay branches are filled (at least with NOPs) */
221 if (cfchange_found && delay_branches != 0) {
222 ir_fprintf(stderr, "Verify warning: Not all delay slots filled after jump (%d/%d) in block %+F (%s)\n",
223 block, get_irg_dump_name(env->irg));
224 env->problem_found = 1;
228 static int should_be_scheduled(ir_node *node) {
232 if(get_irn_mode(node) == mode_M) {
235 if(is_Phi(node) || is_Sync(node) || is_Pin(node))
238 if(is_Proj(node) && get_irn_mode(node) == mode_X)
240 if(be_is_Keep(node) && get_irn_opcode(get_nodes_block(node)) == iro_Bad)
243 switch(get_irn_opcode(node)) {
256 static void check_schedule(ir_node *node, void *data) {
257 be_verify_schedule_env_t *env = data;
261 should_be = should_be_scheduled(node);
265 scheduled = bitset_is_set(env->scheduled, get_irn_idx(node)) ? 1 : 0;
266 should_be = should_be ? 1 : 0;
267 if(should_be != scheduled) {
268 ir_fprintf(stderr, "Verify warning: Node %+F in block %+F(%s) should%s be scheduled\n",
269 node, get_nodes_block(node), get_irg_dump_name(env->irg), should_be ? "" : " not");
270 env->problem_found = 1;
275 * Start a walk over the irg and check schedule.
277 int be_verify_schedule(ir_graph *irg)
279 be_verify_schedule_env_t env;
281 env.problem_found = 0;
282 env.scheduled = bitset_alloca(get_irg_last_idx(irg));
285 irg_block_walk_graph(irg, verify_schedule_walker, NULL, &env);
286 // check if all nodes are scheduled
287 irg_walk_graph(irg, check_schedule, NULL, &env);
289 return ! env.problem_found;
294 //---------------------------------------------------------------------------
298 typedef struct _spill_t {
304 const arch_env_t *arch_env;
309 } be_verify_spillslots_env_t;
311 static int cmp_spill(const void* d1, const void* d2, size_t size) {
312 const spill_t* s1 = d1;
313 const spill_t* s2 = d2;
314 return s1->spill != s2->spill;
317 static spill_t *find_spill(be_verify_spillslots_env_t *env, ir_node *node) {
321 return set_find(env->spills, &spill, sizeof(spill), HASH_PTR(node));
324 static spill_t *get_spill(be_verify_spillslots_env_t *env, ir_node *node, ir_entity *ent) {
326 int hash = HASH_PTR(node);
329 res = set_find(env->spills, &spill, sizeof(spill), hash);
333 res = set_insert(env->spills, &spill, sizeof(spill), hash);
339 static ir_node *get_memory_edge(const ir_node *node) {
341 ir_node *result = NULL;
343 arity = get_irn_arity(node);
344 for(i = arity - 1; i >= 0; --i) {
345 ir_node *arg = get_irn_n(node, i);
346 if(get_irn_mode(arg) == mode_M) {
347 assert(result == NULL);
355 static void collect(be_verify_spillslots_env_t *env, ir_node *node, ir_node *reload, ir_entity* ent);
357 static void check_entity(be_verify_spillslots_env_t *env, ir_node *node, ir_entity *ent) {
359 ir_fprintf(stderr, "Verify warning: Node %+F in block %+F(%s) should have an entity assigned\n",
360 node, get_nodes_block(node), get_irg_dump_name(env->irg));
364 static void collect_spill(be_verify_spillslots_env_t *env, ir_node *node, ir_node *reload, ir_entity* ent) {
365 ir_entity *spillent = arch_get_frame_entity(env->arch_env, node);
366 check_entity(env, node, spillent);
367 get_spill(env, node, ent);
369 if(spillent != ent) {
370 ir_fprintf(stderr, "Verify warning: Spill %+F has different entity than reload %+F in block %+F(%s)\n",
371 node, reload, get_nodes_block(node), get_irg_dump_name(env->irg));
372 env->problem_found = 1;
376 static void collect_memperm(be_verify_spillslots_env_t *env, ir_node *node, ir_node *reload, ir_entity* ent) {
379 int hash = HASH_PTR(node);
384 assert(is_Proj(node));
386 memperm = get_Proj_pred(node);
387 out = get_Proj_proj(node);
389 spillent = be_get_MemPerm_out_entity(memperm, out);
390 check_entity(env, memperm, spillent);
391 if(spillent != ent) {
392 ir_fprintf(stderr, "Verify warning: MemPerm %+F has different entity than reload %+F in block %+F(%s)\n",
393 node, reload, get_nodes_block(node), get_irg_dump_name(env->irg));
394 env->problem_found = 1;
398 res = set_find(env->spills, &spill, sizeof(spill), hash);
403 spill.ent = spillent;
404 res = set_insert(env->spills, &spill, sizeof(spill), hash);
406 for(i = 0, arity = be_get_MemPerm_entity_arity(memperm); i < arity; ++i) {
407 ir_node* arg = get_irn_n(memperm, i + 1);
408 ir_entity* argent = be_get_MemPerm_in_entity(memperm, i);
410 collect(env, arg, memperm, argent);
414 static void collect_memphi(be_verify_spillslots_env_t *env, ir_node *node, ir_node *reload, ir_entity *ent) {
417 int hash = HASH_PTR(node);
419 assert(is_Phi(node));
422 res = set_find(env->spills, &spill, sizeof(spill), hash);
428 res = set_insert(env->spills, &spill, sizeof(spill), hash);
430 // is 1 of the arguments a spill?
431 for(i = 0, arity = get_irn_arity(node); i < arity; ++i) {
432 ir_node* arg = get_irn_n(node, i);
433 collect(env, arg, reload, ent);
437 static void collect(be_verify_spillslots_env_t *env, ir_node *node, ir_node *reload, ir_entity* ent) {
438 if(be_is_Spill(node)) {
439 collect_spill(env, node, reload, ent);
440 } else if(is_Proj(node)) {
441 collect_memperm(env, node, reload, ent);
442 } else if(is_Phi(node) && get_irn_mode(node) == mode_M) {
443 collect_memphi(env, node, reload, ent);
445 ir_fprintf(stderr, "Verify warning: No spill, memperm or memphi attached to node %+F found from node %+F in block %+F(%s)\n",
446 node, reload, get_nodes_block(node), get_irg_dump_name(env->irg));
447 env->problem_found = 1;
452 * This walker function searches for reloads and collects all the spills
453 * and memphis attached to them.
455 static void collect_spills_walker(ir_node *node, void *data) {
456 be_verify_spillslots_env_t *env = data;
457 const arch_env_t *arch_env = env->arch_env;
459 // @@@ ia32_classify returns classification of Proj_pred :-/
463 if(arch_irn_class_is(arch_env, node, reload)) {
464 ir_node *spill = get_memory_edge(node);
468 ir_fprintf(stderr, "Verify warning: No spill attached to reload %+F in block %+F(%s)\n",
469 node, get_nodes_block(node), get_irg_dump_name(env->irg));
470 env->problem_found = 1;
473 ent = arch_get_frame_entity(env->arch_env, node);
474 check_entity(env, node, ent);
476 collect(env, spill, node, ent);
477 ARR_APP1(ir_node*, env->reloads, node);
481 static void check_spillslot_interference(be_verify_spillslots_env_t *env) {
482 int spillcount = set_count(env->spills);
483 spill_t **spills = alloca(spillcount * sizeof(spills[0]));
487 for(spill = set_first(env->spills), i = 0; spill != NULL; spill = set_next(env->spills), ++i) {
491 for(i = 0; i < spillcount; ++i) {
492 spill_t *sp1 = spills[i];
495 for(i2 = i+1; i2 < spillcount; ++i2) {
496 spill_t *sp2 = spills[i2];
498 if(sp1->ent != sp2->ent)
501 if(my_values_interfere(sp1->spill, sp2->spill)) {
502 ir_fprintf(stderr, "Verify warning: Spillslots for %+F in block %+F(%s) and %+F in block %+F(%s) interfere\n",
503 sp1->spill, get_nodes_block(sp1->spill), get_irg_dump_name(env->irg),
504 sp2->spill, get_nodes_block(sp2->spill), get_irg_dump_name(env->irg));
505 env->problem_found = 1;
506 my_values_interfere(sp1->spill, sp2->spill);
512 static void check_lonely_spills(ir_node *node, void *data) {
513 be_verify_spillslots_env_t *env = data;
515 if(be_is_Spill(node) || (is_Proj(node) && be_is_MemPerm(get_Proj_pred(node)))) {
516 spill_t *spill = find_spill(env, node);
517 if(be_is_Spill(node)) {
518 ir_entity *ent = arch_get_frame_entity(env->arch_env, node);
519 check_entity(env, node, ent);
523 ir_fprintf(stderr, "Verify warning: Node %+F in block %+F(%s) not connected to a reaload\n",
524 node, get_nodes_block(node), get_irg_dump_name(env->irg));
529 int be_verify_spillslots(const arch_env_t *arch_env, ir_graph *irg)
531 be_verify_spillslots_env_t env;
533 env.arch_env = arch_env;
535 env.spills = new_set(cmp_spill, 10);
536 env.reloads = NEW_ARR_F(ir_node*, 0);
537 env.problem_found = 0;
539 irg_walk_graph(irg, collect_spills_walker, NULL, &env);
540 irg_walk_graph(irg, check_lonely_spills, NULL, &env);
542 check_spillslot_interference(&env);
544 DEL_ARR_F(env.reloads);
547 return ! env.problem_found;
552 //---------------------------------------------------------------------------
557 * Check, if two values interfere.
558 * @param a The first value.
559 * @param b The second value.
560 * @return 1, if a and b interfere, 0 if not.
562 static int my_values_interfere(const ir_node *a, const ir_node *b) {
563 const ir_edge_t *edge;
565 int a2b = value_dominates(a, b);
566 int b2a = value_dominates(b, a);
568 /* If there is no dominance relation, they do not interfere. */
573 * Adjust a and b so, that a dominates b if
574 * a dominates b or vice versa.
577 const ir_node *t = a;
582 bb = get_nodes_block(b);
585 * Look at all usages of a.
586 * If there's one usage of a in the block of b, then
587 * we check, if this use is dominated by b, if that's true
588 * a and b interfere. Note that b must strictly dominate the user,
589 * since if b is the last user of in the block, b and a do not
591 * Uses of a not in b's block can be disobeyed, because the
592 * check for a being live at the end of b's block is already
595 foreach_out_edge(a, edge) {
596 const ir_node *user = get_edge_src_irn(edge);
600 if(get_irn_opcode(user) == iro_End)
603 // in case of phi arguments we compare with the block the value comes from
605 ir_node *phiblock = get_nodes_block(user);
608 user = get_irn_n(phiblock, get_edge_src_pos(edge));
611 if(value_dominates(b, user))
620 //---------------------------------------------------------------------------
624 typedef struct _be_verify_register_allocation_env_t {
625 const arch_env_t *arch_env;
629 } be_verify_register_allocation_env_t;
631 static void check_register_constraints(ir_node *node, be_verify_register_allocation_env_t *env) {
632 const arch_env_t *arch_env = env->arch_env;
633 const arch_register_t *reg;
636 /* verify output register */
637 if (arch_get_irn_reg_class(arch_env, node, -1) != NULL) {
638 reg = arch_get_irn_register(arch_env, node);
640 ir_fprintf(stderr, "Verify warning: Node %+F in block %+F(%s) should have a register assigned\n",
641 node, get_nodes_block(node), get_irg_dump_name(env->irg));
642 env->problem_found = 1;
644 else if (! arch_register_type_is(reg, joker) && !arch_reg_is_allocatable(arch_env, node, -1, reg)) {
645 ir_fprintf(stderr, "Verify warning: Register %s assigned as output of %+F not allowed (register constraint) in block %+F(%s)\n",
646 reg->name, node, get_nodes_block(node), get_irg_dump_name(env->irg));
647 env->problem_found = 1;
651 /* verify input register */
652 arity = get_irn_arity(node);
653 for (i = 0; i < arity; ++i) {
654 ir_node *pred = get_irn_n(node, i);
656 if (is_Unknown(pred))
660 ir_fprintf(stderr, "Verify warning: %+F in block %+F(%s) has Bad as input %d\n",
661 node, get_nodes_block(node), get_irg_dump_name(env->irg), i);
662 env->problem_found = 1;
666 if (arch_get_irn_reg_class(arch_env, node, i) == NULL)
669 reg = arch_get_irn_register(arch_env, pred);
671 ir_fprintf(stderr, "Verify warning: Node %+F in block %+F(%s) should have a register assigned\n",
672 pred, get_nodes_block(pred), get_irg_dump_name(env->irg));
673 env->problem_found = 1;
676 else if (! arch_register_type_is(reg, joker) && ! arch_reg_is_allocatable(arch_env, node, i, reg)) {
677 ir_fprintf(stderr, "Verify warning: Register %s as input %d of %+F not allowed (register constraint) in block %+F(%s)\n",
678 reg->name, i, node, get_nodes_block(node), get_irg_dump_name(env->irg));
679 env->problem_found = 1;
684 static void check_register_allocation(be_verify_register_allocation_env_t *env,
685 const arch_register_class_t *regclass, pset *nodes) {
686 const arch_env_t *arch_env = env->arch_env;
687 const arch_register_t *reg = NULL;
689 bitset_t *registers = bitset_alloca(arch_register_class_n_regs(regclass));
692 foreach_pset(nodes, node) {
693 if (arch_get_irn_reg_class(arch_env, node, -1) != regclass)
696 reg = arch_get_irn_register(arch_env, node);
698 /* this problem is already reported in 'check_register_constraints' */
702 if (bitset_is_set(registers, reg->index)) {
707 bitset_set(registers, reg->index);
711 ir_fprintf(stderr, "Verify warning: Register %s assigned more than once in block %+F(%s)\n",
712 reg->name, get_nodes_block(node), get_irg_dump_name(env->irg));
713 env->problem_found = 1;
715 foreach_pset(nodes, node) {
716 if (arch_get_irn_register(arch_env, node) == reg) {
717 ir_fprintf(stderr, " at node %+F\n", node);
723 static void verify_block_register_allocation(ir_node *block, void *data) {
724 be_verify_register_allocation_env_t *env = data;
725 const arch_env_t *arch_env = env->arch_env;
726 const arch_isa_t *isa = arch_env->isa;
729 nregclasses = arch_isa_get_n_reg_class(isa);
730 for (i = 0; i < nregclasses; ++i) {
731 const arch_register_class_t *regclass = arch_isa_get_reg_class(isa, i);
733 pset *live_nodes = pset_new_ptr_default();
735 be_liveness_end_of_block(env->lv, env->arch_env, regclass, block, live_nodes);
736 check_register_allocation(env, regclass, live_nodes);
738 sched_foreach_reverse(block, node) {
742 be_liveness_transfer(env->arch_env, regclass, node, live_nodes);
743 check_register_allocation(env, regclass, live_nodes);
744 check_register_constraints(node, env);
747 del_pset(live_nodes);
751 int be_verify_register_allocation(const arch_env_t *arch_env, ir_graph *irg) {
752 be_verify_register_allocation_env_t env;
754 env.arch_env = arch_env;
756 env.lv = be_liveness(irg);
757 env.problem_found = 0;
759 irg_block_walk_graph(irg, verify_block_register_allocation, NULL, &env);
761 be_liveness_free(env.lv);
763 return !env.problem_found;
768 //---------------------------------------------------------------------------
772 typedef struct _verify_out_dead_nodes_env {
776 } verify_out_dead_nodes_env;
778 static void check_out_edges(ir_node *node, verify_out_dead_nodes_env *env) {
779 ir_graph *irg = env->irg;
780 const ir_edge_t* edge;
782 if(irn_visited(node))
784 mark_irn_visited(node);
786 foreach_out_edge(node, edge) {
787 ir_node* src = get_edge_src_irn(edge);
789 if(!bitset_is_set(env->reachable, get_irn_idx(src)) && !is_Block(node)) {
790 ir_fprintf(stderr, "Verify warning: Node %+F in block %+F(%s) only reachable through out edges from %+F\n",
791 src, get_nodes_block(src), get_irg_dump_name(irg), node);
792 env->problem_found = 1;
796 check_out_edges(src, env);
800 static void set_reachable(ir_node *node, void* data)
802 bitset_t* reachable = data;
803 bitset_set(reachable, get_irn_idx(node));
806 int be_verify_out_edges(ir_graph *irg) {
807 verify_out_dead_nodes_env env;
810 env.reachable = bitset_alloca(get_irg_last_idx(irg));
811 env.problem_found = edges_verify(irg);
813 irg_walk_in_or_dep_graph(irg, set_reachable, NULL, env.reachable);
814 irg_walk_anchors(irg, set_reachable, NULL, env.reachable);
815 inc_irg_visited(irg);
816 check_out_edges(get_irg_start(irg), &env);
818 return ! env.problem_found;