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 {
225 } be_verify_spillslots_env_t;
227 static int cmp_spill(const void* d1, const void* d2, size_t size) {
228 const spill_t* s1 = d1;
229 const spill_t* s2 = d2;
230 return s1->spill != s2->spill;
233 static spill_t *get_spill(be_verify_spillslots_env_t *env, ir_node *node, entity *ent) {
235 int hash = HASH_PTR(node);
238 res = set_find(env->spills, &spill, sizeof(spill), hash);
242 res = set_insert(env->spills, &spill, sizeof(spill), hash);
248 static void collect(be_verify_spillslots_env_t *env, ir_node *node, ir_node *reload, entity* ent);
250 static void collect_spill(be_verify_spillslots_env_t *env, ir_node *node, ir_node *reload, entity* ent) {
251 entity *spillent = be_get_frame_entity(node);
252 get_spill(env, node, ent);
254 if(spillent != ent) {
255 ir_fprintf(stderr, "Verify warning: Spill %+F has different entity than reload %+F in block %+F(%s)\n",
256 node, reload, get_nodes_block(node), get_irg_dump_name(env->irg));
257 env->problem_found = 1;
261 static void collect_memperm(be_verify_spillslots_env_t *env, ir_node *node, ir_node *reload, entity* ent) {
264 int hash = HASH_PTR(node);
269 assert(is_Proj(node));
271 memperm = get_Proj_pred(node);
272 out = get_Proj_proj(node);
274 spillent = be_get_MemPerm_out_entity(memperm, out);
275 if(spillent != ent) {
276 ir_fprintf(stderr, "Verify warning: MemPerm %+F has different entity than reload %+F in block %+F(%s)\n",
277 node, reload, get_nodes_block(node), get_irg_dump_name(env->irg));
278 env->problem_found = 1;
282 res = set_find(env->spills, &spill, sizeof(spill), hash);
287 spill.ent = spillent;
288 res = set_insert(env->spills, &spill, sizeof(spill), hash);
290 for(i = 0, arity = get_irn_arity(memperm); i < arity; ++i) {
291 ir_node* arg = get_irn_n(memperm, i);
292 entity* argent = be_get_MemPerm_in_entity(memperm, i);
294 collect(env, arg, memperm, argent);
298 static void collect_memphi(be_verify_spillslots_env_t *env, ir_node *node, ir_node *reload, entity *ent) {
301 int hash = HASH_PTR(node);
303 assert(is_Phi(node));
306 res = set_find(env->spills, &spill, sizeof(spill), hash);
312 res = set_insert(env->spills, &spill, sizeof(spill), hash);
314 // is 1 of the arguments a spill?
315 for(i = 0, arity = get_irn_arity(node); i < arity; ++i) {
316 ir_node* arg = get_irn_n(node, i);
317 collect(env, arg, reload, ent);
321 static void collect(be_verify_spillslots_env_t *env, ir_node *node, ir_node *reload, entity* ent) {
322 if(be_is_Spill(node)) {
323 collect_spill(env, node, reload, ent);
324 } else if(is_Proj(node)) {
325 collect_memperm(env, node, reload, ent);
326 } else if(is_Phi(node) && get_irn_mode(node) == mode_M) {
327 collect_memphi(env, node, reload, ent);
329 ir_fprintf(stderr, "Verify warning: No spill, memperm or memphi attached to node %+F found from node %+F in block %+F(%s)\n",
330 node, reload, get_nodes_block(node), get_irg_dump_name(env->irg));
331 env->problem_found = 1;
336 * This walker function searches for reloads and collects all the spills
337 * and memphis attached to them.
339 static void collect_spills_walker(ir_node *node, void *data) {
340 be_verify_spillslots_env_t *env = data;
342 if(be_is_Reload(node)) {
343 ir_node *spill = get_irn_n(node, be_pos_Reload_mem);
344 entity* ent = be_get_frame_entity(node);
346 collect(env, spill, node, ent);
347 ARR_APP1(ir_node*, env->reloads, node);
351 static void check_spillslot_interference(be_verify_spillslots_env_t *env) {
352 int spillcount = set_count(env->spills);
353 spill_t **spills = alloca(spillcount * sizeof(spills[0]));
357 for(spill = set_first(env->spills), i = 0; spill != NULL; spill = set_next(env->spills), ++i) {
361 for(i = 0; i < spillcount; ++i) {
362 spill_t *sp1 = spills[i];
365 for(i2 = i+1; i2 < spillcount; ++i2) {
366 spill_t *sp2 = spills[i2];
368 if(sp1->ent != sp2->ent)
371 if(my_values_interfere(sp1->spill, sp2->spill)) {
372 ir_fprintf(stderr, "Verify warning: Spillslots for %+F in block %+F(%s) and %+F in block %+F(%s) interfere\n",
373 sp1->spill, get_nodes_block(sp1->spill), get_irg_dump_name(env->irg),
374 sp2->spill, get_nodes_block(sp2->spill), get_irg_dump_name(env->irg));
375 env->problem_found = 1;
381 int be_verify_spillslots(ir_graph *irg)
383 be_verify_spillslots_env_t env;
386 env.spills = new_set(cmp_spill, 10);
387 env.reloads = NEW_ARR_F(ir_node*, 0);
388 env.problem_found = 0;
390 irg_walk_graph(irg, collect_spills_walker, NULL, &env);
392 check_spillslot_interference(&env);
394 DEL_ARR_F(env.reloads);
397 return ! env.problem_found;
402 //---------------------------------------------------------------------------
406 static int my_value_dominates(const ir_node *a, const ir_node *b)
409 const ir_node *ba = get_block(a);
410 const ir_node *bb = get_block(b);
413 * a and b are not in the same block,
414 * so dominance is determined by the dominance of the blocks.
417 res = block_dominates(ba, bb);
420 * Dominance is determined by the time steps of the schedule.
423 sched_timestep_t as = sched_get_time_step(a);
424 sched_timestep_t bs = sched_get_time_step(b);
432 * Check, if two values interfere.
433 * @param a The first value.
434 * @param b The second value.
435 * @return 1, if a and b interfere, 0 if not.
437 static int my_values_interfere(const ir_node *a, const ir_node *b)
439 const ir_edge_t *edge;
441 int a2b = my_value_dominates(a, b);
442 int b2a = my_value_dominates(b, a);
444 /* If there is no dominance relation, they do not interfere. */
449 * Adjust a and b so, that a dominates b if
450 * a dominates b or vice versa.
453 const ir_node *t = a;
458 bb = get_nodes_block(b);
461 * Look at all usages of a.
462 * If there's one usage of a in the block of b, then
463 * we check, if this use is dominated by b, if that's true
464 * a and b interfere. Note that b must strictly dominate the user,
465 * since if b is the last user of in the block, b and a do not
467 * Uses of a not in b's block can be disobeyed, because the
468 * check for a being live at the end of b's block is already
471 foreach_out_edge(a, edge) {
472 const ir_node *user = get_edge_src_irn(edge);
476 // in case of phi arguments we compare with the block the value comes from
478 ir_node *phiblock = get_nodes_block(user);
479 user = get_irn_n(phiblock, get_edge_src_pos(edge));
482 if(my_value_dominates(b, user))