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_value_dominates(const ir_node *a, const ir_node *b);
27 static int my_values_interfere(const ir_node *a, const ir_node *b);
29 typedef struct be_verify_register_pressure_env_t_ {
30 ir_graph *irg; /**< the irg to verify */
31 be_lv_t *lv; /**< Liveness information. */
32 const arch_env_t *arch_env; /**< an architecture environment */
33 const arch_register_class_t *cls; /**< the register class to check for */
34 int registers_available; /**< number of available registers */
35 int problem_found; /**< flag indicating if a problem was found */
36 } be_verify_register_pressure_env_t;
39 * Print all nodes of a pset into a file.
41 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)
57 be_verify_register_pressure_env_t *env = (be_verify_register_pressure_env_t *)data;
58 pset *live_nodes = pset_new_ptr_default();
62 /* collect register pressure info, start with end of a block */
63 be_liveness_end_of_block(env->lv, env->arch_env, env->cls, block, live_nodes);
65 pressure = pset_count(live_nodes);
66 if(pressure > env->registers_available) {
67 ir_fprintf(stderr, "Verify Warning: Register pressure too high at end of block %+F(%s) (%d/%d):\n",
68 block, get_irg_dump_name(env->irg), pressure, env->registers_available);
69 print_living_values(stderr, live_nodes);
70 env->problem_found = 1;
73 sched_foreach_reverse(block, irn) {
77 be_liveness_transfer(env->arch_env, env->cls, irn, live_nodes);
79 pressure = pset_count(live_nodes);
81 if(pressure > env->registers_available) {
82 ir_fprintf(stderr, "Verify Warning: Register pressure too high before node %+F in %+F(%s) (%d/%d):\n",
83 irn, block, get_irg_dump_name(env->irg), pressure, env->registers_available);
84 print_living_values(stderr, live_nodes);
85 env->problem_found = 1;
92 * Start a walk over the irg and check the register pressure.
94 int be_verify_register_pressure(const arch_env_t *arch_env, 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 = arch_env;
102 env.registers_available = arch_count_non_ignore_regs(arch_env, cls);
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 ir_graph *irg; /**< the irg to check */
114 } be_verify_schedule_env_t;
117 * Simple schedule checker.
119 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 pset *uses = pset_new_ptr_default();
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) {
141 ir_fprintf(stderr, "Verify Warning: Phi node %+F scheduled after non-Phi nodes in block %+F (%s)\n",
142 node, block, get_irg_dump_name(env->irg));
143 env->problem_found = 1;
149 // 2. Check for control flow changing nodes
150 if (is_cfop(node) && get_irn_opcode(node) != iro_Start) {
151 /* check, that only one CF operation is scheduled */
152 if (cfchange_found == 1) {
153 ir_fprintf(stderr, "Verify Warning: More than 1 control flow changing node (%+F) scheduled in block %+F (%s)\n",
154 node, block, get_irg_dump_name(env->irg));
155 env->problem_found = 1;
158 } else if (cfchange_found) {
159 // proj and keepany aren't real instructions...
160 if(!is_Proj(node) && !be_is_Keep(node)) {
161 /* check for delay branches */
162 if (delay_branches == 0) {
163 ir_fprintf(stderr, "Verify Warning: Node %+F scheduled after control flow changing node (+delay branches) in block %+F (%s)\n",
164 node, block, get_irg_dump_name(env->irg));
165 env->problem_found = 1;
173 if(pset_find_ptr(uses, node)) {
174 ir_fprintf(stderr, "Verify Warning: Value %+F used before it was defined in block %+F (%s)\n",
175 node, block, get_irg_dump_name(env->irg));
176 env->problem_found = 1;
179 for(i = 0, arity = get_irn_arity(node); i < arity; ++i) {
180 pset_insert_ptr(uses, get_irn_n(node, i));
186 /* check that all delay branches are filled (at least with NOPs) */
187 if (cfchange_found && delay_branches != 0) {
188 ir_fprintf(stderr, "Not all delay slots filled after jump (%d/%d) in block %+F (%s)\n",
189 block, get_irg_dump_name(env->irg));
190 env->problem_found = 1;
195 * Start a walk over the irg and check schedule.
197 int be_verify_schedule(ir_graph *irg)
199 be_verify_schedule_env_t env;
201 env.problem_found = 0;
204 irg_block_walk_graph(irg, verify_schedule_walker, NULL, &env);
206 return ! env.problem_found;
211 //---------------------------------------------------------------------------
215 typedef struct _spill_t {
226 } be_verify_spillslots_env_t;
228 static int cmp_spill(const void* d1, const void* d2, size_t size) {
229 const spill_t* s1 = d1;
230 const spill_t* s2 = d2;
231 return s1->spill != s2->spill;
234 static spill_t *get_spill(be_verify_spillslots_env_t *env, ir_node *node, entity *ent) {
236 int hash = HASH_PTR(node);
239 res = set_find(env->spills, &spill, sizeof(spill), hash);
243 res = set_insert(env->spills, &spill, sizeof(spill), hash);
249 static void collect(be_verify_spillslots_env_t *env, ir_node *node, ir_node *reload, entity* ent);
251 static void collect_spill(be_verify_spillslots_env_t *env, ir_node *node, ir_node *reload, entity* ent) {
252 entity *spillent = be_get_frame_entity(node);
253 get_spill(env, node, ent);
255 if(spillent != ent) {
256 ir_fprintf(stderr, "Verify warning: Spill %+F has different entity than reload %+F in block %+F(%s)\n",
257 node, reload, get_nodes_block(node), get_irg_dump_name(env->irg));
258 env->problem_found = 1;
262 static void collect_memperm(be_verify_spillslots_env_t *env, ir_node *node, ir_node *reload, entity* ent) {
265 int hash = HASH_PTR(node);
270 assert(is_Proj(node));
272 memperm = get_Proj_pred(node);
273 out = get_Proj_proj(node);
275 spillent = be_get_MemPerm_out_entity(memperm, out);
276 if(spillent != ent) {
277 ir_fprintf(stderr, "Verify warning: MemPerm %+F has different entity than reload %+F in block %+F(%s)\n",
278 node, reload, get_nodes_block(node), get_irg_dump_name(env->irg));
279 env->problem_found = 1;
283 res = set_find(env->spills, &spill, sizeof(spill), hash);
288 spill.ent = spillent;
289 res = set_insert(env->spills, &spill, sizeof(spill), hash);
291 for(i = 0, arity = get_irn_arity(memperm); i < arity; ++i) {
292 ir_node* arg = get_irn_n(memperm, i);
293 entity* argent = be_get_MemPerm_in_entity(memperm, i);
295 collect(env, arg, memperm, argent);
299 static void collect_memphi(be_verify_spillslots_env_t *env, ir_node *node, ir_node *reload, entity *ent) {
302 int hash = HASH_PTR(node);
304 assert(is_Phi(node));
307 res = set_find(env->spills, &spill, sizeof(spill), hash);
313 res = set_insert(env->spills, &spill, sizeof(spill), hash);
315 // is 1 of the arguments a spill?
316 for(i = 0, arity = get_irn_arity(node); i < arity; ++i) {
317 ir_node* arg = get_irn_n(node, i);
318 collect(env, arg, reload, ent);
322 static void collect(be_verify_spillslots_env_t *env, ir_node *node, ir_node *reload, entity* ent) {
323 if(be_is_Spill(node)) {
324 collect_spill(env, node, reload, ent);
325 } else if(is_Proj(node)) {
326 collect_memperm(env, node, reload, ent);
327 } else if(is_Phi(node) && get_irn_mode(node) == mode_M) {
328 collect_memphi(env, node, reload, ent);
330 ir_fprintf(stderr, "Verify warning: No spill, memperm or memphi attached to node %+F found from node %+F in block %+F(%s)\n",
331 node, reload, get_nodes_block(node), get_irg_dump_name(env->irg));
332 env->problem_found = 1;
337 * This walker function searches for reloads and collects all the spills
338 * and memphis attached to them.
340 static void collect_spills_walker(ir_node *node, void *data) {
341 be_verify_spillslots_env_t *env = data;
343 if(be_is_Reload(node)) {
344 ir_node *spill = get_irn_n(node, be_pos_Reload_mem);
345 entity* ent = be_get_frame_entity(node);
347 collect(env, spill, node, ent);
348 ARR_APP1(ir_node*, env->reloads, node);
352 static void check_spillslot_interference(be_verify_spillslots_env_t *env) {
353 int spillcount = set_count(env->spills);
354 spill_t **spills = alloca(spillcount * sizeof(spills[0]));
358 for(spill = set_first(env->spills), i = 0; spill != NULL; spill = set_next(env->spills), ++i) {
362 for(i = 0; i < spillcount; ++i) {
363 spill_t *sp1 = spills[i];
366 for(i2 = i+1; i2 < spillcount; ++i2) {
367 spill_t *sp2 = spills[i2];
369 if(sp1->ent != sp2->ent)
372 if(my_values_interfere(sp1->spill, sp2->spill)) {
373 ir_fprintf(stderr, "Verify warning: Spillslots for %+F in block %+F(%s) and %+F in block %+F(%s) interfere\n",
374 sp1->spill, get_nodes_block(sp1->spill), get_irg_dump_name(env->irg),
375 sp2->spill, get_nodes_block(sp2->spill), get_irg_dump_name(env->irg));
376 env->problem_found = 1;
377 my_values_interfere(sp1->spill, sp2->spill);
378 printf("Intf: %d\n", values_interfere(env->lv, sp1->spill, sp2->spill));
384 int be_verify_spillslots(ir_graph *irg)
386 be_verify_spillslots_env_t env;
389 env.spills = new_set(cmp_spill, 10);
390 env.reloads = NEW_ARR_F(ir_node*, 0);
391 env.problem_found = 0;
392 env.lv = be_liveness(irg);
394 irg_walk_graph(irg, collect_spills_walker, NULL, &env);
396 check_spillslot_interference(&env);
398 be_liveness_free(env.lv);
399 DEL_ARR_F(env.reloads);
402 return ! env.problem_found;
407 //---------------------------------------------------------------------------
412 * Check, if two values interfere.
413 * @param a The first value.
414 * @param b The second value.
415 * @return 1, if a and b interfere, 0 if not.
417 static int my_values_interfere(const ir_node *a, const ir_node *b)
419 const ir_edge_t *edge;
421 int a2b = value_dominates(a, b);
422 int b2a = value_dominates(b, a);
424 /* If there is no dominance relation, they do not interfere. */
429 * Adjust a and b so, that a dominates b if
430 * a dominates b or vice versa.
433 const ir_node *t = a;
438 bb = get_nodes_block(b);
441 * Look at all usages of a.
442 * If there's one usage of a in the block of b, then
443 * we check, if this use is dominated by b, if that's true
444 * a and b interfere. Note that b must strictly dominate the user,
445 * since if b is the last user of in the block, b and a do not
447 * Uses of a not in b's block can be disobeyed, because the
448 * check for a being live at the end of b's block is already
451 foreach_out_edge(a, edge) {
452 const ir_node *user = get_edge_src_irn(edge);
456 if(get_irn_opcode(user) == iro_End)
459 // in case of phi arguments we compare with the block the value comes from
461 ir_node *phiblock = get_nodes_block(user);
464 user = get_irn_n(phiblock, get_edge_src_pos(edge));
467 if(value_dominates(b, user))