- don't place copies between proj cascades
[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 "benode_t.h"
22
23 typedef struct be_verify_register_pressure_env_t_ {
24         ir_graph                    *irg;                 /**< the irg to verify */
25         const arch_env_t            *arch_env;            /**< an architecture environment */
26         const arch_register_class_t *cls;                 /**< the register class to check for */
27         int                         registers_available;  /**< number of available registers */
28         int                         problem_found;        /**< flag indicating if a problem was found */
29 } be_verify_register_pressure_env_t;
30
31 /**
32  * Print all nodes of a pset into a file.
33  */
34 static void print_living_values(FILE *F, pset *live_nodes)
35 {
36         ir_node *node;
37
38         ir_fprintf(F, "\t");
39         foreach_pset(live_nodes, node) {
40                 ir_fprintf(F, "%+F ", node);
41         }
42         ir_fprintf(F, "\n");
43 }
44
45 /**
46  * Check if number of live nodes never exceeds the number of available registers.
47  */
48 static void verify_liveness_walker(ir_node *block, void *data)
49 {
50         be_verify_register_pressure_env_t *env = (be_verify_register_pressure_env_t *)data;
51         pset    *live_nodes = pset_new_ptr_default();
52         ir_node *irn;
53         int pressure;
54
55         /* collect register pressure info, start with end of a block */
56         be_liveness_end_of_block(env->arch_env, env->cls, block, live_nodes);
57
58         pressure = pset_count(live_nodes);
59         if(pressure > env->registers_available) {
60                 ir_fprintf(stderr, "Verify Warning: Register pressure too high at end of block %+F(%s) (%d/%d):\n",
61                         block, get_irg_dump_name(env->irg), pressure, env->registers_available);
62                 print_living_values(stderr, live_nodes);
63                 env->problem_found = 1;
64         }
65
66         sched_foreach_reverse(block, irn) {
67                 if (is_Phi(irn))
68                         break;
69
70                 be_liveness_transfer(env->arch_env, env->cls, irn, live_nodes);
71
72                 pressure = pset_count(live_nodes);
73
74                 if(pressure > env->registers_available) {
75                         ir_fprintf(stderr, "Verify Warning: Register pressure too high before node %+F in %+F(%s) (%d/%d):\n",
76                                 irn, block, get_irg_dump_name(env->irg), pressure, env->registers_available);
77                         print_living_values(stderr, live_nodes);
78                         env->problem_found = 1;
79                 }
80         }
81         del_pset(live_nodes);
82 }
83
84 /**
85  * Start a walk over the irg and check the register pressure.
86  */
87 int be_verify_register_pressure(const arch_env_t *arch_env, const arch_register_class_t *cls, ir_graph *irg)
88 {
89         be_verify_register_pressure_env_t env;
90
91         be_liveness(irg);
92
93         env.irg                 = irg;
94         env.arch_env            = arch_env;
95         env.cls                 = cls;
96         env.registers_available = arch_count_non_ignore_regs(arch_env, cls);
97         env.problem_found       = 0;
98
99         irg_block_walk_graph(irg, verify_liveness_walker, NULL, &env);
100
101         return ! env.problem_found;
102 }
103
104 typedef struct be_verify_schedule_env_t_ {
105         int      problem_found;    /**< flags indicating if there was a problem */
106         ir_graph *irg;             /**< the irg to check */
107 } be_verify_schedule_env_t;
108
109 /**
110  * Simple schedule checker.
111  */
112 static void verify_schedule_walker(ir_node *block, void *data)
113 {
114         be_verify_schedule_env_t *env = (be_verify_schedule_env_t*) data;
115         ir_node *node;
116         int non_phi_found  = 0;
117         int cfchange_found = 0;
118         // TODO ask arch about delay branches
119         int delay_branches = 0;
120         pset *uses = pset_new_ptr_default();
121
122         /*
123          * Tests for the following things:
124          *   1. Make sure that all phi nodes are scheduled at the beginning of the block
125          *   2. There is 1 or no control flow changing node scheduled and exactly delay_branches operations after it.
126          *   3. No value is defined after it has been used
127          */
128         sched_foreach(block, node) {
129                 int i, arity;
130
131                 // 1. Check for phis
132                 if (is_Phi(node)) {
133                         if (non_phi_found) {
134                                 ir_fprintf(stderr, "Verify Warning: Phi node %+F scheduled after non-Phi nodes in block %+F (%s)\n",
135                                         node, block, get_irg_dump_name(env->irg));
136                                 env->problem_found = 1;
137                         }
138                 } else {
139                         non_phi_found = 1;
140                 }
141
142                 // 2. Check for control flow changing nodes
143                 if (is_cfop(node) && get_irn_opcode(node) != iro_Start) {
144                         /* check, that only one CF operation is scheduled */
145                         if (cfchange_found == 1) {
146                                 ir_fprintf(stderr, "Verify Warning: More than 1 control flow changing node (%+F) scheduled in block %+F (%s)\n",
147                                         node, block, get_irg_dump_name(env->irg));
148                                 env->problem_found = 1;
149                         }
150                         cfchange_found = 1;
151                 } else if (cfchange_found) {
152                         // proj and keepany aren't real instructions...
153                         if(!is_Proj(node) && !be_is_Keep(node)) {
154                                 /* check for delay branches */
155                                 if (delay_branches == 0) {
156                                         ir_fprintf(stderr, "Verify Warning: Node %+F scheduled after control flow changing node (+delay branches) in block %+F (%s)\n",
157                                                 node, block, get_irg_dump_name(env->irg));
158                                         env->problem_found = 1;
159                                 } else {
160                                         delay_branches--;
161                                 }
162                         }
163                 }
164
165                 // 3. Check for uses
166                 if(pset_find_ptr(uses, node)) {
167                         ir_fprintf(stderr, "Verify Warning: Value %+F used before it was defined in block %+F (%s)\n",
168                                 node, block, get_irg_dump_name(env->irg));
169                         env->problem_found = 1;
170                 }
171                 if(!is_Phi(node)) {
172                         for(i = 0, arity = get_irn_arity(node); i < arity; ++i) {
173                                 pset_insert_ptr(uses, get_irn_n(node, i));
174                         }
175                 }
176         }
177         del_pset(uses);
178
179         /* check that all delay branches are filled (at least with NOPs) */
180         if (cfchange_found && delay_branches != 0) {
181                 ir_fprintf(stderr, "Not all delay slots filled after jump (%d/%d) in block %+F (%s)\n",
182                         block, get_irg_dump_name(env->irg));
183                 env->problem_found = 1;
184         }
185 }
186
187 /**
188  * Start a walk over the irg and check schedule.
189  */
190 int be_verify_schedule(ir_graph *irg)
191 {
192         be_verify_schedule_env_t env;
193
194         env.problem_found = 0;
195         env.irg           = irg;
196
197         irg_block_walk_graph(irg, verify_schedule_walker, NULL, &env);
198
199         return ! env.problem_found;
200 }