0e836e4c0af632520c208decff7944f0e89aca49
[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 "beverify.h"
13 #include "belive.h"
14 #include "besched_t.h"
15
16 #include "irnode.h"
17 #include "irgraph.h"
18 #include "irgwalk.h"
19 #include "irprintf.h"
20 #include "irdump_t.h"
21 #include "iredges.h"
22 #include "set.h"
23 #include "array.h"
24 #include "benode_t.h"
25
26 static int my_values_interfere(const ir_node *a, const ir_node *b);
27
28 typedef struct be_verify_register_pressure_env_t_ {
29         ir_graph                    *irg;                 /**< the irg to verify */
30          be_lv_t                    *lv;                  /**< Liveness information. */
31         const arch_env_t            *arch_env;            /**< an architecture environment */
32         const arch_register_class_t *cls;                 /**< the register class to check for */
33         int                         registers_available;  /**< number of available registers */
34         int                         problem_found;        /**< flag indicating if a problem was found */
35 } be_verify_register_pressure_env_t;
36
37 /**
38  * Print all nodes of a pset into a file.
39  */
40 static void print_living_values(FILE *F, pset *live_nodes)
41 {
42         ir_node *node;
43
44         ir_fprintf(F, "\t");
45         foreach_pset(live_nodes, node) {
46                 ir_fprintf(F, "%+F ", node);
47         }
48         ir_fprintf(F, "\n");
49 }
50
51 /**
52  * Check if number of live nodes never exceeds the number of available registers.
53  */
54 static void verify_liveness_walker(ir_node *block, void *data)
55 {
56         be_verify_register_pressure_env_t *env = (be_verify_register_pressure_env_t *)data;
57         pset    *live_nodes = pset_new_ptr_default();
58         ir_node *irn;
59         int pressure;
60
61         /* collect register pressure info, start with end of a block */
62         be_liveness_end_of_block(env->lv, env->arch_env, env->cls, block, live_nodes);
63
64         pressure = pset_count(live_nodes);
65         if(pressure > env->registers_available) {
66                 ir_fprintf(stderr, "Verify Warning: Register pressure too high at end of block %+F(%s) (%d/%d):\n",
67                         block, get_irg_dump_name(env->irg), pressure, env->registers_available);
68                 print_living_values(stderr, live_nodes);
69                 env->problem_found = 1;
70         }
71
72         sched_foreach_reverse(block, irn) {
73                 if (is_Phi(irn))
74                         break;
75
76                 be_liveness_transfer(env->arch_env, env->cls, irn, live_nodes);
77
78                 pressure = pset_count(live_nodes);
79
80                 if(pressure > env->registers_available) {
81                         ir_fprintf(stderr, "Verify Warning: Register pressure too high before node %+F in %+F(%s) (%d/%d):\n",
82                                 irn, block, get_irg_dump_name(env->irg), pressure, env->registers_available);
83                         print_living_values(stderr, live_nodes);
84                         env->problem_found = 1;
85                 }
86         }
87         del_pset(live_nodes);
88 }
89
90 /**
91  * Start a walk over the irg and check the register pressure.
92  */
93 int be_verify_register_pressure(const arch_env_t *arch_env, const arch_register_class_t *cls, ir_graph *irg)
94 {
95         be_verify_register_pressure_env_t env;
96
97         env.lv                  = be_liveness(irg);
98         env.irg                 = irg;
99         env.arch_env            = arch_env;
100         env.cls                 = cls;
101         env.registers_available = arch_count_non_ignore_regs(arch_env, cls);
102         env.problem_found       = 0;
103
104         irg_block_walk_graph(irg, verify_liveness_walker, NULL, &env);
105         be_liveness_free(env.lv);
106
107         return ! env.problem_found;
108 }
109
110 typedef struct be_verify_schedule_env_t_ {
111         int      problem_found;    /**< flags indicating if there was a problem */
112         ir_graph *irg;             /**< the irg to check */
113 } be_verify_schedule_env_t;
114
115 /**
116  * Simple schedule checker.
117  */
118 static void verify_schedule_walker(ir_node *block, void *data)
119 {
120         be_verify_schedule_env_t *env = (be_verify_schedule_env_t*) data;
121         ir_node *node;
122         int non_phi_found  = 0;
123         int cfchange_found = 0;
124         // TODO ask arch about delay branches
125         int delay_branches = 0;
126         pset *uses = pset_new_ptr_default();
127
128         /*
129          * Tests for the following things:
130          *   1. Make sure that all phi nodes are scheduled at the beginning of the block
131          *   2. There is 1 or no control flow changing node scheduled and exactly delay_branches operations after it.
132          *   3. No value is defined after it has been used
133          */
134         sched_foreach(block, node) {
135                 int i, arity;
136
137                 // 1. Check for phis
138                 if (is_Phi(node)) {
139                         if (non_phi_found) {
140                                 ir_fprintf(stderr, "Verify Warning: Phi node %+F scheduled after non-Phi nodes in block %+F (%s)\n",
141                                         node, block, get_irg_dump_name(env->irg));
142                                 env->problem_found = 1;
143                         }
144                 } else {
145                         non_phi_found = 1;
146                 }
147
148                 // 2. Check for control flow changing nodes
149                 if (is_cfop(node) && get_irn_opcode(node) != iro_Start) {
150                         /* check, that only one CF operation is scheduled */
151                         if (cfchange_found == 1) {
152                                 ir_fprintf(stderr, "Verify Warning: More than 1 control flow changing node (%+F) scheduled in block %+F (%s)\n",
153                                         node, block, get_irg_dump_name(env->irg));
154                                 env->problem_found = 1;
155                         }
156                         cfchange_found = 1;
157                 } else if (cfchange_found) {
158                         // proj and keepany aren't real instructions...
159                         if(!is_Proj(node) && !be_is_Keep(node)) {
160                                 /* check for delay branches */
161                                 if (delay_branches == 0) {
162                                         ir_fprintf(stderr, "Verify Warning: Node %+F scheduled after control flow changing node (+delay branches) in block %+F (%s)\n",
163                                                 node, block, get_irg_dump_name(env->irg));
164                                         env->problem_found = 1;
165                                 } else {
166                                         delay_branches--;
167                                 }
168                         }
169                 }
170
171                 // 3. Check for uses
172                 if(pset_find_ptr(uses, node)) {
173                         ir_fprintf(stderr, "Verify Warning: Value %+F used before it was defined in block %+F (%s)\n",
174                                 node, block, get_irg_dump_name(env->irg));
175                         env->problem_found = 1;
176                 }
177                 if(!is_Phi(node)) {
178                         for(i = 0, arity = get_irn_arity(node); i < arity; ++i) {
179                                 pset_insert_ptr(uses, get_irn_n(node, i));
180                         }
181                 }
182         }
183         del_pset(uses);
184
185         /* check that all delay branches are filled (at least with NOPs) */
186         if (cfchange_found && delay_branches != 0) {
187                 ir_fprintf(stderr, "Verify warning: Not all delay slots filled after jump (%d/%d) in block %+F (%s)\n",
188                         block, get_irg_dump_name(env->irg));
189                 env->problem_found = 1;
190         }
191 }
192
193 static int should_be_scheduled(ir_node *node) {
194         if(is_Block(node))
195                 return -1;
196
197         if(get_irn_mode(node) == mode_M) {
198                 if(is_Proj(node))
199                         return -1;
200                 if(is_Phi(node) || is_Sync(node) || get_irn_opcode(node) == iro_Pin)
201                         return 0;
202         }
203         if(is_Proj(node) && get_irn_mode(node) == mode_X)
204                 return 0;
205         if(be_is_Keep(node) && get_irn_opcode(get_nodes_block(node)) == iro_Bad)
206                 return 0;
207
208         switch(get_irn_opcode(node)) {
209         case iro_End:
210         case iro_NoMem:
211         case iro_Bad:
212                 return 0;
213         default:
214                 break;
215         }
216
217         return 1;
218 }
219
220 static void check_schedule(ir_node *node, void *data) {
221         be_verify_schedule_env_t *env = data;
222         int should_be;
223
224         should_be = should_be_scheduled(node);
225         if(should_be == -1)
226                 return;
227
228         if(should_be ? !sched_is_scheduled(node) : sched_is_scheduled(node)) {
229                 ir_fprintf(stderr, "Verify warning: Node %+F in block %+F(%s) should%s be scheduled\n",
230                         node, get_nodes_block(node), get_irg_dump_name(env->irg), should_be ? "" : " not");
231                 env->problem_found = 1;
232         }
233 }
234
235 /**
236  * Start a walk over the irg and check schedule.
237  */
238 int be_verify_schedule(ir_graph *irg)
239 {
240         be_verify_schedule_env_t env;
241
242         env.problem_found = 0;
243         env.irg           = irg;
244
245         irg_block_walk_graph(irg, verify_schedule_walker, NULL, &env);
246         // check if all nodes are scheduled
247         irg_walk_graph(irg, check_schedule, NULL, &env);
248
249         return ! env.problem_found;
250 }
251
252
253
254 //---------------------------------------------------------------------------
255
256
257
258 typedef struct _spill_t {
259         ir_node *spill;
260         entity *ent;
261 } spill_t;
262
263 typedef struct {
264         be_lv_t *lv;
265         ir_graph *irg;
266         set *spills;
267         ir_node **reloads;
268         int problem_found;
269 } be_verify_spillslots_env_t;
270
271 static int cmp_spill(const void* d1, const void* d2, size_t size) {
272         const spill_t* s1 = d1;
273         const spill_t* s2 = d2;
274         return s1->spill != s2->spill;
275 }
276
277 static spill_t *find_spill(be_verify_spillslots_env_t *env, ir_node *node) {
278         spill_t spill;
279
280         spill.spill = node;
281         return set_find(env->spills, &spill, sizeof(spill), HASH_PTR(node));
282 }
283
284 static spill_t *get_spill(be_verify_spillslots_env_t *env, ir_node *node, entity *ent) {
285         spill_t spill, *res;
286         int hash = HASH_PTR(node);
287
288         spill.spill = node;
289         res = set_find(env->spills, &spill, sizeof(spill), hash);
290
291         if(res == NULL) {
292                 spill.ent = ent;
293                 res = set_insert(env->spills, &spill, sizeof(spill), hash);
294         }
295
296         return res;
297 }
298
299 static void collect(be_verify_spillslots_env_t *env, ir_node *node, ir_node *reload, entity* ent);
300
301 static void check_entity(be_verify_spillslots_env_t *env, ir_node *node, entity *ent) {
302         if(ent == NULL) {
303                 ir_fprintf(stderr, "Verify warning: Node %+F in block %+F(%s) should have an entity assigned\n",
304                            node, get_nodes_block(node), get_irg_dump_name(env->irg));
305         }
306 }
307
308 static void collect_spill(be_verify_spillslots_env_t *env, ir_node *node, ir_node *reload, entity* ent) {
309         entity *spillent = be_get_frame_entity(node);
310         check_entity(env, node, spillent);
311         get_spill(env, node, ent);
312
313         if(spillent != ent) {
314                 ir_fprintf(stderr, "Verify warning: Spill %+F has different entity than reload %+F in block %+F(%s)\n",
315                         node, reload, get_nodes_block(node), get_irg_dump_name(env->irg));
316                 env->problem_found = 1;
317         }
318 }
319
320 static void collect_memperm(be_verify_spillslots_env_t *env, ir_node *node, ir_node *reload, entity* ent) {
321         int i, arity;
322         spill_t spill, *res;
323         int hash = HASH_PTR(node);
324         int out;
325         ir_node* memperm;
326         entity *spillent;
327
328         assert(is_Proj(node));
329
330         memperm = get_Proj_pred(node);
331         out = get_Proj_proj(node);
332
333         spillent = be_get_MemPerm_out_entity(memperm, out);
334         check_entity(env, memperm, spillent);
335         if(spillent != ent) {
336                 ir_fprintf(stderr, "Verify warning: MemPerm %+F has different entity than reload %+F in block %+F(%s)\n",
337                         node, reload, get_nodes_block(node), get_irg_dump_name(env->irg));
338                 env->problem_found = 1;
339         }
340
341         spill.spill = node;
342         res = set_find(env->spills, &spill, sizeof(spill), hash);
343         if(res != NULL) {
344                 return;
345         }
346
347         spill.ent = spillent;
348         res = set_insert(env->spills, &spill, sizeof(spill), hash);
349
350         for(i = 0, arity = get_irn_arity(memperm); i < arity; ++i) {
351                 ir_node* arg = get_irn_n(memperm, i);
352                 entity* argent = be_get_MemPerm_in_entity(memperm, i);
353
354                 collect(env, arg, memperm, argent);
355         }
356 }
357
358 static void collect_memphi(be_verify_spillslots_env_t *env, ir_node *node, ir_node *reload, entity *ent) {
359         int i, arity;
360         spill_t spill, *res;
361         int hash = HASH_PTR(node);
362
363         assert(is_Phi(node));
364
365         spill.spill = node;
366         res = set_find(env->spills, &spill, sizeof(spill), hash);
367         if(res != NULL) {
368                 return;
369         }
370
371         spill.ent = ent;
372         res = set_insert(env->spills, &spill, sizeof(spill), hash);
373
374         // is 1 of the arguments a spill?
375         for(i = 0, arity = get_irn_arity(node); i < arity; ++i) {
376                 ir_node* arg = get_irn_n(node, i);
377                 collect(env, arg, reload, ent);
378         }
379 }
380
381 static void collect(be_verify_spillslots_env_t *env, ir_node *node, ir_node *reload, entity* ent) {
382         if(be_is_Spill(node)) {
383                 collect_spill(env, node, reload, ent);
384         } else if(is_Proj(node)) {
385                 collect_memperm(env, node, reload, ent);
386         } else if(is_Phi(node) && get_irn_mode(node) == mode_M) {
387                 collect_memphi(env, node, reload, ent);
388         } else {
389                 ir_fprintf(stderr, "Verify warning: No spill, memperm or memphi attached to node %+F found from node %+F in block %+F(%s)\n",
390                         node, reload, get_nodes_block(node), get_irg_dump_name(env->irg));
391                 env->problem_found = 1;
392         }
393 }
394
395 /**
396  * This walker function searches for reloads and collects all the spills
397  * and memphis attached to them.
398  */
399 static void collect_spills_walker(ir_node *node, void *data) {
400         be_verify_spillslots_env_t *env = data;
401
402         if(be_is_Reload(node)) {
403                 ir_node *spill = get_irn_n(node, be_pos_Reload_mem);
404                 entity* ent = be_get_frame_entity(node);
405                 check_entity(env, node, ent);
406
407                 collect(env, spill, node, ent);
408                 ARR_APP1(ir_node*, env->reloads, node);
409         }
410 }
411
412 static void check_spillslot_interference(be_verify_spillslots_env_t *env) {
413         int spillcount = set_count(env->spills);
414         spill_t **spills = alloca(spillcount * sizeof(spills[0]));
415         spill_t *spill;
416         int i;
417
418         for(spill = set_first(env->spills), i = 0; spill != NULL; spill = set_next(env->spills), ++i) {
419                 spills[i] = spill;
420         }
421
422         for(i = 0; i < spillcount; ++i) {
423                 spill_t *sp1 = spills[i];
424                 int i2;
425
426                 for(i2 = i+1; i2 < spillcount; ++i2) {
427                         spill_t *sp2 = spills[i2];
428
429                         if(sp1->ent != sp2->ent)
430                                 continue;
431
432                         if(my_values_interfere(sp1->spill, sp2->spill)) {
433                                 ir_fprintf(stderr, "Verify warning: Spillslots for %+F in block %+F(%s) and %+F in block %+F(%s) interfere\n",
434                                         sp1->spill, get_nodes_block(sp1->spill), get_irg_dump_name(env->irg),
435                                         sp2->spill, get_nodes_block(sp2->spill), get_irg_dump_name(env->irg));
436                                 env->problem_found = 1;
437                                 my_values_interfere(sp1->spill, sp2->spill);
438                                 printf("Intf: %d\n", values_interfere(env->lv, sp1->spill, sp2->spill));
439                         }
440                 }
441         }
442 }
443
444 static void check_lonely_spills(ir_node *node, void *data) {
445         be_verify_spillslots_env_t *env = data;
446
447         if(be_is_Spill(node) || (is_Proj(node) && be_is_MemPerm(get_Proj_pred(node)))) {
448                 spill_t *spill = find_spill(env, node);
449                 if(be_is_Spill(node)) {
450                         entity *ent = be_get_frame_entity(node);
451                         check_entity(env, node, ent);
452                 }
453
454                 if(spill == NULL) {
455                         ir_fprintf(stderr, "Verify warning: Node %+F in block %+F(%s) not connected to a reaload\n",
456                                    node, get_nodes_block(node), get_irg_dump_name(env->irg));
457                 }
458         }
459 }
460
461 int be_verify_spillslots(ir_graph *irg)
462 {
463         be_verify_spillslots_env_t env;
464
465         env.irg = irg;
466         env.spills = new_set(cmp_spill, 10);
467         env.reloads = NEW_ARR_F(ir_node*, 0);
468         env.problem_found = 0;
469         env.lv = be_liveness(irg);
470
471         irg_walk_graph(irg, collect_spills_walker, NULL, &env);
472         irg_walk_graph(irg, check_lonely_spills, NULL, &env);
473
474         check_spillslot_interference(&env);
475
476         be_liveness_free(env.lv);
477         DEL_ARR_F(env.reloads);
478         del_set(env.spills);
479
480         return ! env.problem_found;
481 }
482
483
484
485 //---------------------------------------------------------------------------
486
487
488
489 /**
490  * Check, if two values interfere.
491  * @param a The first value.
492  * @param b The second value.
493  * @return 1, if a and b interfere, 0 if not.
494  */
495 static int my_values_interfere(const ir_node *a, const ir_node *b)
496 {
497         const ir_edge_t *edge;
498         ir_node *bb;
499         int a2b = value_dominates(a, b);
500         int b2a = value_dominates(b, a);
501
502         /* If there is no dominance relation, they do not interfere. */
503         if(!a2b && !b2a)
504                 return 0;
505
506         /*
507          * Adjust a and b so, that a dominates b if
508          * a dominates b or vice versa.
509          */
510         if(b2a) {
511                 const ir_node *t = a;
512                 a = b;
513                 b = t;
514         }
515
516         bb = get_nodes_block(b);
517
518         /*
519          * Look at all usages of a.
520          * If there's one usage of a in the block of b, then
521          * we check, if this use is dominated by b, if that's true
522          * a and b interfere. Note that b must strictly dominate the user,
523          * since if b is the last user of in the block, b and a do not
524          * interfere.
525          * Uses of a not in b's block can be disobeyed, because the
526          * check for a being live at the end of b's block is already
527          * performed.
528          */
529         foreach_out_edge(a, edge) {
530                 const ir_node *user = get_edge_src_irn(edge);
531                 if(b == user)
532                         continue;
533
534                 if(get_irn_opcode(user) == iro_End)
535                         continue;
536
537                 // in case of phi arguments we compare with the block the value comes from
538                 if(is_Phi(user)) {
539                         ir_node *phiblock = get_nodes_block(user);
540                         if(phiblock == bb)
541                                 continue;
542                         user = get_irn_n(phiblock, get_edge_src_pos(edge));
543                 }
544
545                 if(value_dominates(b, user))
546                         return 1;
547         }
548
549         return 0;
550 }