fix firm backend
[libfirm] / ir / be / beverify.c
1 /*
2  * Author:    Matthias Braun
3  * Date:      05.05.2006
4  * Copyright: (c) Universitaet Karlsruhe
5  * License:   This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
6  * CVS-Id:    $Id$
7  */
8 #ifdef HAVE_CONFIG_H
9 #include <config.h>
10 #endif
11
12 #include <limits.h>
13
14 #include "bitset.h"
15 #include "set.h"
16 #include "array.h"
17
18 #include "irnode.h"
19 #include "irgraph.h"
20 #include "irgwalk.h"
21 #include "irprintf.h"
22 #include "irdump_t.h"
23 #include "iredges.h"
24
25 #include "beverify.h"
26 #include "belive.h"
27 #include "besched_t.h"
28 #include "benode_t.h"
29
30 static int my_values_interfere(const ir_node *a, const ir_node *b);
31
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;
40
41 /**
42  * Print all nodes of a pset into a file.
43  */
44 static void print_living_values(FILE *F, pset *live_nodes) {
45         ir_node *node;
46
47         ir_fprintf(F, "\t");
48         foreach_pset(live_nodes, node) {
49                 ir_fprintf(F, "%+F ", node);
50         }
51         ir_fprintf(F, "\n");
52 }
53
54 /**
55  * Check if number of live nodes never exceeds the number of available registers.
56  */
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();
60         ir_node *irn;
61         int pressure;
62
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);
65
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;
72         }
73
74         sched_foreach_reverse(block, irn) {
75                 if (is_Phi(irn))
76                         break;
77
78                 be_liveness_transfer(env->arch_env, env->cls, irn, live_nodes);
79
80                 pressure = pset_count(live_nodes);
81
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;
87                 }
88         }
89         del_pset(live_nodes);
90 }
91
92 /**
93  * Start a walk over the irg and check the register pressure.
94  */
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;
97
98         env.lv                  = be_liveness(irg);
99         env.irg                 = irg;
100         env.arch_env            = birg->main_env->arch_env;
101         env.cls                 = cls;
102         env.registers_available = env.cls->n_regs - be_put_ignore_regs(birg, env.cls, NULL);
103         env.problem_found       = 0;
104
105         irg_block_walk_graph(irg, verify_liveness_walker, NULL, &env);
106         be_liveness_free(env.lv);
107
108         return ! env.problem_found;
109 }
110
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;
116
117 /**
118  * Simple schedule checker.
119  */
120 static void verify_schedule_walker(ir_node *block, void *data) {
121         be_verify_schedule_env_t *env = (be_verify_schedule_env_t*) data;
122         ir_node *node;
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;
128
129         /*
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
134          */
135         sched_foreach(block, node) {
136                 int i, arity;
137                 int timestep;
138
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;
143                 }
144                 bitset_set(env->scheduled, get_irn_idx(node));
145
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;
150                 }
151
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",
156                                    node);
157                         env->problem_found = 1;
158                 }
159                 last_timestep = timestep;
160
161                 // Check that phis come before any other node
162                 if (is_Phi(node)) {
163                         if (non_phi_found) {
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;
167                         }
168                 } else {
169                         non_phi_found = 1;
170                 }
171
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;
179                         }
180                         cfchange_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;
189                                 } else {
190                                         delay_branches--;
191                                 }
192                         }
193                 }
194
195                 // Check that all uses come before their definitions
196                 if(!is_Phi(node)) {
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))
202                                         continue;
203
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;
208                                 }
209                         }
210                 }
211
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;
217                 }
218         }
219
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;
225         }
226 }
227
228 static int should_be_scheduled(ir_node *node) {
229         if(is_Block(node))
230                 return -1;
231
232         if(get_irn_mode(node) == mode_M) {
233                 if(is_Proj(node))
234                         return -1;
235                 if(is_Phi(node) || is_Sync(node) || is_Pin(node))
236                         return 0;
237         }
238         if(is_Proj(node) && get_irn_mode(node) == mode_X)
239                 return 0;
240         if(be_is_Keep(node) && get_irn_opcode(get_nodes_block(node)) == iro_Bad)
241                 return 0;
242
243         switch(get_irn_opcode(node)) {
244         case iro_End:
245         case iro_NoMem:
246         case iro_Bad:
247         case iro_Unknown:
248                 return 0;
249         default:
250                 break;
251         }
252
253         return 1;
254 }
255
256 static void check_schedule(ir_node *node, void *data) {
257         be_verify_schedule_env_t *env = data;
258         int should_be;
259         int scheduled;
260
261         should_be = should_be_scheduled(node);
262         if(should_be == -1)
263                 return;
264
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;
271         }
272 }
273
274 /**
275  * Start a walk over the irg and check schedule.
276  */
277 int be_verify_schedule(ir_graph *irg)
278 {
279         be_verify_schedule_env_t env;
280
281         env.problem_found = 0;
282         env.scheduled     = bitset_alloca(get_irg_last_idx(irg));
283         env.irg           = irg;
284
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);
288
289         return ! env.problem_found;
290 }
291
292
293
294 //---------------------------------------------------------------------------
295
296
297
298 typedef struct _spill_t {
299         ir_node *spill;
300         ir_entity *ent;
301 } spill_t;
302
303 typedef struct {
304         const arch_env_t *arch_env;
305         ir_graph *irg;
306         set *spills;
307         ir_node **reloads;
308         int problem_found;
309 } be_verify_spillslots_env_t;
310
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;
315 }
316
317 static spill_t *find_spill(be_verify_spillslots_env_t *env, ir_node *node) {
318         spill_t spill;
319
320         spill.spill = node;
321         return set_find(env->spills, &spill, sizeof(spill), HASH_PTR(node));
322 }
323
324 static spill_t *get_spill(be_verify_spillslots_env_t *env, ir_node *node, ir_entity *ent) {
325         spill_t spill, *res;
326         int hash = HASH_PTR(node);
327
328         spill.spill = node;
329         res = set_find(env->spills, &spill, sizeof(spill), hash);
330
331         if(res == NULL) {
332                 spill.ent = ent;
333                 res = set_insert(env->spills, &spill, sizeof(spill), hash);
334         }
335
336         return res;
337 }
338
339 static ir_node *get_memory_edge(const ir_node *node) {
340         int i, arity;
341         ir_node *result = NULL;
342
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);
348                         result = arg;
349                 }
350         }
351
352         return result;
353 }
354
355 static void collect(be_verify_spillslots_env_t *env, ir_node *node, ir_node *reload, ir_entity* ent);
356
357 static void check_entity(be_verify_spillslots_env_t *env, ir_node *node, ir_entity *ent) {
358         if(ent == NULL) {
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));
361         }
362 }
363
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);
368
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;
373         }
374 }
375
376 static void collect_memperm(be_verify_spillslots_env_t *env, ir_node *node, ir_node *reload, ir_entity* ent) {
377         int i, arity;
378         spill_t spill, *res;
379         int hash = HASH_PTR(node);
380         int out;
381         ir_node* memperm;
382         ir_entity *spillent;
383
384         assert(is_Proj(node));
385
386         memperm = get_Proj_pred(node);
387         out = get_Proj_proj(node);
388
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;
395         }
396
397         spill.spill = node;
398         res = set_find(env->spills, &spill, sizeof(spill), hash);
399         if(res != NULL) {
400                 return;
401         }
402
403         spill.ent = spillent;
404         res = set_insert(env->spills, &spill, sizeof(spill), hash);
405
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);
409
410                 collect(env, arg, memperm, argent);
411         }
412 }
413
414 static void collect_memphi(be_verify_spillslots_env_t *env, ir_node *node, ir_node *reload, ir_entity *ent) {
415         int i, arity;
416         spill_t spill, *res;
417         int hash = HASH_PTR(node);
418
419         assert(is_Phi(node));
420
421         spill.spill = node;
422         res = set_find(env->spills, &spill, sizeof(spill), hash);
423         if(res != NULL) {
424                 return;
425         }
426
427         spill.ent = ent;
428         res = set_insert(env->spills, &spill, sizeof(spill), hash);
429
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);
434         }
435 }
436
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);
444         } else {
445                 // Disabled for now, spills might get transformed by the backend
446 #if 0
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;
450 #endif
451         }
452 }
453
454 /**
455  * This walker function searches for reloads and collects all the spills
456  * and memphis attached to them.
457  */
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;
461
462         // @@@ ia32_classify returns classification of Proj_pred :-/
463         if(is_Proj(node))
464                 return;
465
466         if(arch_irn_class_is(arch_env, node, reload)) {
467                 ir_node *spill = get_memory_edge(node);
468                 ir_entity *ent;
469
470                 if(spill == NULL) {
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;
474                         return;
475                 }
476                 ent = arch_get_frame_entity(env->arch_env, node);
477                 check_entity(env, node, ent);
478
479                 collect(env, spill, node, ent);
480                 ARR_APP1(ir_node*, env->reloads, node);
481         }
482 }
483
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]));
487         spill_t *spill;
488         int i;
489
490         for(spill = set_first(env->spills), i = 0; spill != NULL; spill = set_next(env->spills), ++i) {
491                 spills[i] = spill;
492         }
493
494         for(i = 0; i < spillcount; ++i) {
495                 spill_t *sp1 = spills[i];
496                 int i2;
497
498                 for(i2 = i+1; i2 < spillcount; ++i2) {
499                         spill_t *sp2 = spills[i2];
500
501                         if(sp1->ent != sp2->ent)
502                                 continue;
503
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);
510                         }
511                 }
512         }
513 }
514
515 static void check_lonely_spills(ir_node *node, void *data) {
516         be_verify_spillslots_env_t *env = data;
517
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);
523                 }
524
525                 if(spill == NULL) {
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));
528                 }
529         }
530 }
531
532 int be_verify_spillslots(const arch_env_t *arch_env, ir_graph *irg)
533 {
534         be_verify_spillslots_env_t env;
535
536         env.arch_env = arch_env;
537         env.irg = irg;
538         env.spills = new_set(cmp_spill, 10);
539         env.reloads = NEW_ARR_F(ir_node*, 0);
540         env.problem_found = 0;
541
542         irg_walk_graph(irg, collect_spills_walker, NULL, &env);
543         irg_walk_graph(irg, check_lonely_spills, NULL, &env);
544
545         check_spillslot_interference(&env);
546
547         DEL_ARR_F(env.reloads);
548         del_set(env.spills);
549
550         return ! env.problem_found;
551 }
552
553
554
555 //---------------------------------------------------------------------------
556
557
558
559 /**
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.
564  */
565 static int my_values_interfere(const ir_node *a, const ir_node *b) {
566         const ir_edge_t *edge;
567         ir_node *bb;
568         int a2b = value_dominates(a, b);
569         int b2a = value_dominates(b, a);
570
571         /* If there is no dominance relation, they do not interfere. */
572         if(!a2b && !b2a)
573                 return 0;
574
575         /*
576          * Adjust a and b so, that a dominates b if
577          * a dominates b or vice versa.
578          */
579         if(b2a) {
580                 const ir_node *t = a;
581                 a = b;
582                 b = t;
583         }
584
585         bb = get_nodes_block(b);
586
587         /*
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
593          * interfere.
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
596          * performed.
597          */
598         foreach_out_edge(a, edge) {
599                 const ir_node *user = get_edge_src_irn(edge);
600                 if(b == user)
601                         continue;
602
603                 if(get_irn_opcode(user) == iro_End)
604                         continue;
605
606                 // in case of phi arguments we compare with the block the value comes from
607                 if(is_Phi(user)) {
608                         ir_node *phiblock = get_nodes_block(user);
609                         if(phiblock == bb)
610                                 continue;
611                         user = get_irn_n(phiblock, get_edge_src_pos(edge));
612                 }
613
614                 if(value_dominates(b, user))
615                         return 1;
616         }
617
618         return 0;
619 }
620
621
622
623 //---------------------------------------------------------------------------
624
625
626
627 typedef struct _be_verify_register_allocation_env_t {
628         const arch_env_t *arch_env;
629         ir_graph *irg;
630         be_lv_t *lv;
631         int problem_found;
632 } be_verify_register_allocation_env_t;
633
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;
637         int                   i, arity;
638
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);
642                 if (reg == NULL) {
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;
646                 }
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;
651                 }
652         }
653
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);
658
659                 if (is_Unknown(pred))
660                         continue;
661
662                 if (is_Bad(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;
666                         continue;
667                 }
668
669                 if (arch_get_irn_reg_class(arch_env, node, i) == NULL)
670                         continue;
671
672                 reg = arch_get_irn_register(arch_env, pred);
673                 if (reg == NULL) {
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;
677                         continue;
678                 }
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;
683                 }
684         }
685 }
686
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;
691         int                   fail       = 0;
692         bitset_t              *registers = bitset_alloca(arch_register_class_n_regs(regclass));
693         ir_node               *node;
694
695         foreach_pset(nodes, node) {
696                 if (arch_get_irn_reg_class(arch_env, node, -1) != regclass)
697                         continue;
698
699                 reg = arch_get_irn_register(arch_env, node);
700
701                 /* this problem is already reported in 'check_register_constraints' */
702                 if (! reg)
703                         continue;
704
705                 if (bitset_is_set(registers, reg->index)) {
706                         pset_break(nodes);
707                         fail = 1;
708                         break;
709                 }
710                 bitset_set(registers, reg->index);
711         }
712
713         if (fail) {
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;
717
718                 foreach_pset(nodes, node) {
719                         if (arch_get_irn_register(arch_env, node) == reg) {
720                                 ir_fprintf(stderr, "  at node %+F\n", node);
721                         }
722                 }
723         }
724 }
725
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;
730         int i, nregclasses;
731
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);
735                 ir_node *node;
736                 pset *live_nodes = pset_new_ptr_default();
737
738                 be_liveness_end_of_block(env->lv, env->arch_env, regclass, block, live_nodes);
739                 check_register_allocation(env, regclass, live_nodes);
740
741                 sched_foreach_reverse(block, node) {
742                         if (is_Phi(node))
743                                 break;
744
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);
748                 }
749
750                 del_pset(live_nodes);
751         }
752 }
753
754 int be_verify_register_allocation(const arch_env_t *arch_env, ir_graph *irg) {
755         be_verify_register_allocation_env_t env;
756
757         env.arch_env = arch_env;
758         env.irg = irg;
759         env.lv = be_liveness(irg);
760         env.problem_found = 0;
761
762         irg_block_walk_graph(irg, verify_block_register_allocation, NULL, &env);
763
764         be_liveness_free(env.lv);
765
766         return !env.problem_found;
767 }
768
769
770
771 //---------------------------------------------------------------------------
772
773
774
775 typedef struct _verify_out_dead_nodes_env {
776         ir_graph *irg;
777         bitset_t *reachable;
778         int problem_found;
779 } verify_out_dead_nodes_env;
780
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;
784
785         if(irn_visited(node))
786                 return;
787         mark_irn_visited(node);
788
789         foreach_out_edge(node, edge) {
790                 ir_node* src = get_edge_src_irn(edge);
791
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;
796                         continue;
797                 }
798
799                 check_out_edges(src, env);
800         }
801 }
802
803 static void set_reachable(ir_node *node, void* data)
804 {
805         bitset_t* reachable = data;
806         bitset_set(reachable, get_irn_idx(node));
807 }
808
809 int be_verify_out_edges(ir_graph *irg) {
810         verify_out_dead_nodes_env env;
811
812         env.irg           = irg;
813         env.reachable     = bitset_alloca(get_irg_last_idx(irg));
814         env.problem_found = edges_verify(irg);
815
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);
820
821         return ! env.problem_found;
822 }