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 // Disabled for now, spills might get transformed by the backend
447 ir_fprintf(stderr, "Verify warning: No spill, memperm or memphi attached to node %+F found from node %+F in block %+F(%s)\n",
448 node, reload, get_nodes_block(node), get_irg_dump_name(env->irg));
449 env->problem_found = 1;
455 * This walker function searches for reloads and collects all the spills
456 * and memphis attached to them.
458 static void collect_spills_walker(ir_node *node, void *data) {
459 be_verify_spillslots_env_t *env = data;
460 const arch_env_t *arch_env = env->arch_env;
462 // @@@ ia32_classify returns classification of Proj_pred :-/
466 if(arch_irn_class_is(arch_env, node, reload)) {
467 ir_node *spill = get_memory_edge(node);
471 ir_fprintf(stderr, "Verify warning: No spill attached to reload %+F in block %+F(%s)\n",
472 node, get_nodes_block(node), get_irg_dump_name(env->irg));
473 env->problem_found = 1;
476 ent = arch_get_frame_entity(env->arch_env, node);
477 check_entity(env, node, ent);
479 collect(env, spill, node, ent);
480 ARR_APP1(ir_node*, env->reloads, node);
484 static void check_spillslot_interference(be_verify_spillslots_env_t *env) {
485 int spillcount = set_count(env->spills);
486 spill_t **spills = alloca(spillcount * sizeof(spills[0]));
490 for(spill = set_first(env->spills), i = 0; spill != NULL; spill = set_next(env->spills), ++i) {
494 for(i = 0; i < spillcount; ++i) {
495 spill_t *sp1 = spills[i];
498 for(i2 = i+1; i2 < spillcount; ++i2) {
499 spill_t *sp2 = spills[i2];
501 if(sp1->ent != sp2->ent)
504 if(my_values_interfere(sp1->spill, sp2->spill)) {
505 ir_fprintf(stderr, "Verify warning: Spillslots for %+F in block %+F(%s) and %+F in block %+F(%s) interfere\n",
506 sp1->spill, get_nodes_block(sp1->spill), get_irg_dump_name(env->irg),
507 sp2->spill, get_nodes_block(sp2->spill), get_irg_dump_name(env->irg));
508 env->problem_found = 1;
509 my_values_interfere(sp1->spill, sp2->spill);
515 static void check_lonely_spills(ir_node *node, void *data) {
516 be_verify_spillslots_env_t *env = data;
518 if(be_is_Spill(node) || (is_Proj(node) && be_is_MemPerm(get_Proj_pred(node)))) {
519 spill_t *spill = find_spill(env, node);
520 if(be_is_Spill(node)) {
521 ir_entity *ent = arch_get_frame_entity(env->arch_env, node);
522 check_entity(env, node, ent);
526 ir_fprintf(stderr, "Verify warning: Node %+F in block %+F(%s) not connected to a reaload\n",
527 node, get_nodes_block(node), get_irg_dump_name(env->irg));
532 int be_verify_spillslots(const arch_env_t *arch_env, ir_graph *irg)
534 be_verify_spillslots_env_t env;
536 env.arch_env = arch_env;
538 env.spills = new_set(cmp_spill, 10);
539 env.reloads = NEW_ARR_F(ir_node*, 0);
540 env.problem_found = 0;
542 irg_walk_graph(irg, collect_spills_walker, NULL, &env);
543 irg_walk_graph(irg, check_lonely_spills, NULL, &env);
545 check_spillslot_interference(&env);
547 DEL_ARR_F(env.reloads);
550 return ! env.problem_found;
555 //---------------------------------------------------------------------------
560 * Check, if two values interfere.
561 * @param a The first value.
562 * @param b The second value.
563 * @return 1, if a and b interfere, 0 if not.
565 static int my_values_interfere(const ir_node *a, const ir_node *b) {
566 const ir_edge_t *edge;
568 int a2b = value_dominates(a, b);
569 int b2a = value_dominates(b, a);
571 /* If there is no dominance relation, they do not interfere. */
576 * Adjust a and b so, that a dominates b if
577 * a dominates b or vice versa.
580 const ir_node *t = a;
585 bb = get_nodes_block(b);
588 * Look at all usages of a.
589 * If there's one usage of a in the block of b, then
590 * we check, if this use is dominated by b, if that's true
591 * a and b interfere. Note that b must strictly dominate the user,
592 * since if b is the last user of in the block, b and a do not
594 * Uses of a not in b's block can be disobeyed, because the
595 * check for a being live at the end of b's block is already
598 foreach_out_edge(a, edge) {
599 const ir_node *user = get_edge_src_irn(edge);
603 if(get_irn_opcode(user) == iro_End)
606 // in case of phi arguments we compare with the block the value comes from
608 ir_node *phiblock = get_nodes_block(user);
611 user = get_irn_n(phiblock, get_edge_src_pos(edge));
614 if(value_dominates(b, user))
623 //---------------------------------------------------------------------------
627 typedef struct _be_verify_register_allocation_env_t {
628 const arch_env_t *arch_env;
632 } be_verify_register_allocation_env_t;
634 static void check_register_constraints(ir_node *node, be_verify_register_allocation_env_t *env) {
635 const arch_env_t *arch_env = env->arch_env;
636 const arch_register_t *reg;
639 /* verify output register */
640 if (arch_get_irn_reg_class(arch_env, node, -1) != NULL) {
641 reg = arch_get_irn_register(arch_env, node);
643 ir_fprintf(stderr, "Verify warning: Node %+F in block %+F(%s) should have a register assigned\n",
644 node, get_nodes_block(node), get_irg_dump_name(env->irg));
645 env->problem_found = 1;
647 else if (! arch_register_type_is(reg, joker) && !arch_reg_is_allocatable(arch_env, node, -1, reg)) {
648 ir_fprintf(stderr, "Verify warning: Register %s assigned as output of %+F not allowed (register constraint) in block %+F(%s)\n",
649 reg->name, node, get_nodes_block(node), get_irg_dump_name(env->irg));
650 env->problem_found = 1;
654 /* verify input register */
655 arity = get_irn_arity(node);
656 for (i = 0; i < arity; ++i) {
657 ir_node *pred = get_irn_n(node, i);
659 if (is_Unknown(pred))
663 ir_fprintf(stderr, "Verify warning: %+F in block %+F(%s) has Bad as input %d\n",
664 node, get_nodes_block(node), get_irg_dump_name(env->irg), i);
665 env->problem_found = 1;
669 if (arch_get_irn_reg_class(arch_env, node, i) == NULL)
672 reg = arch_get_irn_register(arch_env, pred);
674 ir_fprintf(stderr, "Verify warning: Node %+F in block %+F(%s) should have a register assigned\n",
675 pred, get_nodes_block(pred), get_irg_dump_name(env->irg));
676 env->problem_found = 1;
679 else if (! arch_register_type_is(reg, joker) && ! arch_reg_is_allocatable(arch_env, node, i, reg)) {
680 ir_fprintf(stderr, "Verify warning: Register %s as input %d of %+F not allowed (register constraint) in block %+F(%s)\n",
681 reg->name, i, node, get_nodes_block(node), get_irg_dump_name(env->irg));
682 env->problem_found = 1;
687 static void check_register_allocation(be_verify_register_allocation_env_t *env,
688 const arch_register_class_t *regclass, pset *nodes) {
689 const arch_env_t *arch_env = env->arch_env;
690 const arch_register_t *reg = NULL;
692 bitset_t *registers = bitset_alloca(arch_register_class_n_regs(regclass));
695 foreach_pset(nodes, node) {
696 if (arch_get_irn_reg_class(arch_env, node, -1) != regclass)
699 reg = arch_get_irn_register(arch_env, node);
701 /* this problem is already reported in 'check_register_constraints' */
705 if (bitset_is_set(registers, reg->index)) {
710 bitset_set(registers, reg->index);
714 ir_fprintf(stderr, "Verify warning: Register %s assigned more than once in block %+F(%s)\n",
715 reg->name, get_nodes_block(node), get_irg_dump_name(env->irg));
716 env->problem_found = 1;
718 foreach_pset(nodes, node) {
719 if (arch_get_irn_register(arch_env, node) == reg) {
720 ir_fprintf(stderr, " at node %+F\n", node);
726 static void verify_block_register_allocation(ir_node *block, void *data) {
727 be_verify_register_allocation_env_t *env = data;
728 const arch_env_t *arch_env = env->arch_env;
729 const arch_isa_t *isa = arch_env->isa;
732 nregclasses = arch_isa_get_n_reg_class(isa);
733 for (i = 0; i < nregclasses; ++i) {
734 const arch_register_class_t *regclass = arch_isa_get_reg_class(isa, i);
736 pset *live_nodes = pset_new_ptr_default();
738 be_liveness_end_of_block(env->lv, env->arch_env, regclass, block, live_nodes);
739 check_register_allocation(env, regclass, live_nodes);
741 sched_foreach_reverse(block, node) {
745 be_liveness_transfer(env->arch_env, regclass, node, live_nodes);
746 check_register_allocation(env, regclass, live_nodes);
747 check_register_constraints(node, env);
750 del_pset(live_nodes);
754 int be_verify_register_allocation(const arch_env_t *arch_env, ir_graph *irg) {
755 be_verify_register_allocation_env_t env;
757 env.arch_env = arch_env;
759 env.lv = be_liveness(irg);
760 env.problem_found = 0;
762 irg_block_walk_graph(irg, verify_block_register_allocation, NULL, &env);
764 be_liveness_free(env.lv);
766 return !env.problem_found;
771 //---------------------------------------------------------------------------
775 typedef struct _verify_out_dead_nodes_env {
779 } verify_out_dead_nodes_env;
781 static void check_out_edges(ir_node *node, verify_out_dead_nodes_env *env) {
782 ir_graph *irg = env->irg;
783 const ir_edge_t* edge;
785 if(irn_visited(node))
787 mark_irn_visited(node);
789 foreach_out_edge(node, edge) {
790 ir_node* src = get_edge_src_irn(edge);
792 if(!bitset_is_set(env->reachable, get_irn_idx(src)) && !is_Block(node)) {
793 ir_fprintf(stderr, "Verify warning: Node %+F in block %+F(%s) only reachable through out edges from %+F\n",
794 src, get_nodes_block(src), get_irg_dump_name(irg), node);
795 env->problem_found = 1;
799 check_out_edges(src, env);
803 static void set_reachable(ir_node *node, void* data)
805 bitset_t* reachable = data;
806 bitset_set(reachable, get_irn_idx(node));
809 int be_verify_out_edges(ir_graph *irg) {
810 verify_out_dead_nodes_env env;
813 env.reachable = bitset_alloca(get_irg_last_idx(irg));
814 env.problem_found = edges_verify(irg);
816 irg_walk_in_or_dep_graph(irg, set_reachable, NULL, env.reachable);
817 irg_walk_anchors(irg, set_reachable, NULL, env.reachable);
818 inc_irg_visited(irg);
819 check_out_edges(get_irg_start(irg), &env);
821 return ! env.problem_found;