e95db3fdbea9120409635e3c0c1cb91ddb10fc5d
[libfirm] / ir / ir / irvrfy.c
1 /*
2  * Project:     libFIRM
3  * File name:   ir/ir/irvrfy.c
4  * Purpose:     Check irnodes for correctness.
5  * Author:      Christian Schaefer
6  * Modified by: Goetz Lindenmaier. Till Riedel
7  * Created:
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 1998-2003 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13 #ifdef HAVE_CONFIG_H
14 # include <config.h>
15 #endif
16
17 # include "irgraph_t.h"
18 # include "irvrfy.h"
19 # include "irgwalk.h"
20
21 #ifdef NDEBUG
22 /*
23  * in RELEASE mode, returns ret if the expression expr evaluates to zero
24  * in ASSERT mode, asserts the expression expr (and the string string).
25  */
26 #define ASSERT_AND_RET(expr, string, ret)               if (!(expr)) return (ret)
27
28 /*
29  * in RELEASE mode, returns ret if the expression expr evaluates to zero
30  * in ASSERT mode, executes blk if the expression expr evaluates to zero and asserts
31  */
32 #define ASSERT_AND_RET_DBG(expr, string, ret, blk)      if (!(expr)) return (ret)
33 #else
34 #define ASSERT_AND_RET(expr, string, ret)               do { assert((expr) && string); if (!(expr)) return (ret); } while(0)
35 #define ASSERT_AND_RET_DBG(expr, string, ret, blk)      do { if (!(expr)) { { blk } assert(0 && string); return (ret); } } while(0)
36 #endif
37
38 /* @@@ replace use of array "in" by access functions. */
39 ir_node **get_irn_in(ir_node *node);
40
41 bool opt_do_node_verification = 1;
42 void do_node_verification(bool b) {
43   opt_do_node_verification = b;
44 }
45
46 /**
47  * Prints a failure message for a binop
48  */
49 static void show_binop_failure(ir_node *n, const char *text)
50 {
51   ir_node *left  = get_binop_left(n);
52   ir_node *right = get_binop_right(n);
53
54   fprintf(stderr, "\nFIRM: irn_vrfy_irg() of node %ld %s%s(%s%s, %s%s) did not match (%s)\n",
55       get_irn_node_nr(n),
56       get_irn_opname(n), get_irn_modename(n),
57       get_irn_opname(left), get_irn_modename(left),
58       get_irn_opname(right), get_irn_modename(right),
59       text);
60 }
61
62 /**
63  * Prints a failure message for an unop
64  */
65 static void show_unop_failure(ir_node *n, const char *text)
66 {
67   ir_node *op  = get_unop_op(n);
68
69   fprintf(stderr, "\nFIRM: irn_vrfy_irg() of node %ld %s%s(%s%s) did not match (%s)\n",
70       get_irn_node_nr(n),
71       get_irn_opname(n), get_irn_modename(n),
72       get_irn_opname(op), get_irn_modename(op),
73       text);
74 }
75
76 /**
77  * Prints a failure message for a proj
78  */
79 static void show_proj_failure(ir_node *n)
80 {
81   ir_node *op  = get_Proj_pred(n);
82   int proj     = get_Proj_proj(n);
83
84   fprintf(stderr, "\nFIRM: irn_vrfy_irg() of node %ld %s%s %d(%s%s) failed\n" ,
85       get_irn_node_nr(n),
86       get_irn_opname(n), get_irn_modename(n), proj,
87       get_irn_opname(op), get_irn_modename(op));
88 }
89
90 /**
91  * Prints a failure message for a proj
92  */
93 static void show_proj_failure_ent(ir_node *n, entity *ent)
94 {
95   ir_node *op  = get_Proj_pred(n);
96   int proj     = get_Proj_proj(n);
97   ir_mode *m   = get_type_mode(get_entity_type(ent));
98
99   fprintf(stderr, "\nFIRM: irn_vrfy_irg() of node %ld %s%s %d(%s%s) entity %s(type %s mode %s)failed\n" ,
100       get_irn_node_nr(n),
101       get_irn_opname(n), get_irn_modename(n), proj,
102       get_irn_opname(op), get_irn_modename(op),
103       get_entity_name(ent), get_type_name(get_entity_type(ent)),
104       m ? get_mode_name(m) : "<no mode>");
105 }
106
107
108 /**
109  * Show a node and a graph
110  */
111 static void show_node_on_graph(ir_graph *irg, ir_node *n)
112 {
113   entity *ent = get_irg_ent(irg);
114
115   if (ent)
116     fprintf(stderr, "\nFIRM: irn_vrfy_irg() of entity %s, node %ld %s%s\n",
117       get_entity_name(ent),
118       get_irn_node_nr(n), get_irn_opname(n), get_irn_modename(n));
119   else
120     fprintf(stderr, "\nFIRM: irn_vrfy_irg() of graph %p, node %ld %s%s\n",
121       (void *)irg,
122       get_irn_node_nr(n), get_irn_opname(n), get_irn_modename(n));
123 }
124
125 /**
126  * Show call params
127  */
128 static void show_call_param(ir_node *n, type *mt)
129 {
130   int i;
131
132   fprintf(stderr, "\nFIRM: irn_vrfy_irg() Call type-check failed: %s(", get_type_name(mt));
133   for (i = 0; i < get_method_n_params(mt); ++i) {
134     fprintf(stderr, "%s ", get_mode_name(get_type_mode(get_method_param_type(mt, i))));
135   }
136   fprintf(stderr, ") != CALL(");
137
138   for (i = 0; i < get_Call_n_params(n); ++i) {
139     fprintf(stderr, "%s ", get_mode_name(get_irn_mode(get_Call_param(n, i))));
140   }
141   fprintf(stderr, ")\n");
142
143 }
144
145 /**
146  * Show return modes
147  */
148 static void show_return_modes(ir_graph *irg, ir_node *n, type *mt, int i)
149 {
150   entity *ent = get_irg_ent(irg);
151
152   fprintf(stderr, "\nFIRM: irn_vrfy_irg() Return node %ld in entity \"%s\" mode %s different from type mode %s\n",
153     get_irn_node_nr(n), get_entity_name(ent),
154     get_mode_name(get_irn_mode(get_Return_res(n, i))),
155     get_mode_name(get_type_mode(get_method_res_type(mt, i)))
156   );
157 }
158
159 /**
160  * Show return number of results
161  */
162 static void show_return_nres(ir_graph *irg, ir_node *n, type *mt)
163 {
164   entity *ent = get_irg_ent(irg);
165
166   fprintf(stderr, "\nFIRM: irn_vrfy_irg() Return node %ld in entity \"%s\" has %d results different from type %d\n",
167     get_irn_node_nr(n), get_entity_name(ent),
168     get_Return_n_ress(n), get_method_n_ress(mt));
169 }
170
171 INLINE static int
172 vrfy_Proj_proj(ir_node *p, ir_graph *irg) {
173   ir_node *pred;
174   ir_mode *mode;
175   int proj;
176
177   pred = skip_nop(get_Proj_pred(p));
178   assert(get_irn_mode(pred) == mode_T);
179   mode = get_irn_mode(p);
180   proj = get_Proj_proj(p);
181
182   switch (get_irn_opcode(pred)) {
183     case iro_Start:
184       ASSERT_AND_RET_DBG(
185           (
186            (proj == pns_initial_exec   && mode == mode_X) ||
187            (proj == pns_global_store   && mode == mode_M) ||
188            (proj == pns_frame_base     && mode_is_reference(mode)) ||
189            (proj == pns_globals        && mode_is_reference(mode)) ||
190            (proj == pns_args           && mode == mode_T) ||
191            (proj == pns_value_arg_base && mode_is_reference(mode))
192           ),
193           "wrong Proj from Start", 0,
194           show_proj_failure(p);
195       );
196       break;
197
198     case iro_Cond:
199       ASSERT_AND_RET_DBG(
200         (proj >= 0 && mode == mode_X),
201         "wrong Proj from Cond", 0,
202         show_proj_failure(p);
203       );
204       break;
205
206     case iro_Raise:
207       ASSERT_AND_RET_DBG(
208         ((proj == pn_Raise_X && mode == mode_X) || (proj == pn_Raise_M && mode == mode_M)),
209         "wrong Proj from Raise", 0,
210         show_proj_failure(p);
211       );
212       break;
213
214     case iro_InstOf:
215       ASSERT_AND_RET_DBG(
216         (proj >= 0 && mode == mode_X),
217         "wrong Proj from InstOf", 0,
218         show_proj_failure(p);
219       );
220       break;
221
222     case iro_Call:
223       ASSERT_AND_RET_DBG(
224         ((proj == pn_Call_M_regular        && mode == mode_M) ||
225          (proj == pn_Call_X_except         && mode == mode_X) ||
226          (proj == pn_Call_T_result         && mode == mode_T) ||
227          (proj == pn_Call_M_except         && mode == mode_M) ||
228          (proj == pn_Call_P_value_res_base && mode == mode_P)),
229         "wrong Proj from Call", 0,
230         show_proj_failure(p);
231       );
232       break;
233
234     case iro_Quot:
235       ASSERT_AND_RET_DBG(
236         ((proj == pn_Quot_M        && mode == mode_M) ||
237          (proj == pn_Quot_X_except && mode == mode_X) ||
238          (proj == pn_Quot_res      && mode_is_float(mode))),
239         "wrong Proj from Quot", 0,
240         show_proj_failure(p);
241       );
242       break;
243
244     case iro_DivMod:
245       ASSERT_AND_RET_DBG(
246         ((proj == pn_DivMod_M        && mode == mode_M) ||
247          (proj == pn_DivMod_X_except && mode == mode_X) ||
248          (proj == pn_DivMod_res_div  && mode_is_int(mode)) ||
249          (proj == pn_DivMod_res_mod  && mode_is_int(mode))),
250         "wrong Proj from DivMod", 0,
251         show_proj_failure(p);
252       );
253       break;
254
255     case iro_Div:
256       ASSERT_AND_RET_DBG(
257         ((proj == pn_Div_M        && mode == mode_M) ||
258          (proj == pn_Div_X_except && mode == mode_X) ||
259          (proj == pn_Div_res      && mode_is_int(mode))),
260         "wrong Proj from Div or Mod", 0,
261         show_proj_failure(p);
262       );
263       break;
264
265     case iro_Mod:
266       ASSERT_AND_RET_DBG(
267         ((proj == pn_Mod_M        && mode == mode_M) ||
268          (proj == pn_Mod_X_except && mode == mode_X) ||
269          (proj == pn_Mod_res      && mode_is_int(mode))),
270         "wrong Proj from Div or Mod", 0,
271         show_proj_failure(p);
272       );
273       break;
274
275     case iro_Cmp:
276       ASSERT_AND_RET_DBG(
277         (proj >= 0 && proj <= 15 && mode == mode_b),
278         "wrong Proj from Cmp", 0,
279         show_proj_failure(p);
280       );
281       break;
282
283     case iro_Load:
284       if (proj == pn_Load_res) {
285         ir_node *ptr = get_Load_ptr(pred);
286         entity *ent = NULL;
287         if (get_irn_op(ptr) == op_Sel) {
288           ent = get_Sel_entity(ptr);
289         } /*
290         We may not test this, after lowering and optimization the Const can
291         have an unexpected type.
292     else if ((get_irn_op(ptr) == op_Const) &&
293                    tarval_is_entity(get_Const_tarval(ptr))) {
294           ent = get_tarval_entity(get_Const_tarval(ptr));
295         } */
296         if (ent) {
297           ASSERT_AND_RET_DBG(
298             (mode == get_type_mode(get_entity_type(ent))),
299             "wrong data Proj from Load, entity type_mode failed", 0,
300             show_proj_failure_ent(p, ent);
301           );
302         }
303         else {
304           ASSERT_AND_RET_DBG(
305             mode_is_data(mode),
306            "wrong data Proj from Load", 0,
307            show_proj_failure(p);
308          );
309         }
310       } else {
311         ASSERT_AND_RET_DBG(
312           ((proj == pn_Load_M        && mode == mode_M) ||
313            (proj == pn_Load_X_except && mode == mode_X)),
314           "wrong Proj from Load", 0,
315           show_proj_failure(p);
316         );
317       }
318       break;
319
320     case iro_Store:
321       ASSERT_AND_RET_DBG(
322         ((proj == pn_Store_M        && mode == mode_M) ||
323          (proj == pn_Store_X_except && mode == mode_X)),
324         "wrong Proj from Store", 0,
325         show_proj_failure(p);
326       );
327       break;
328
329     case iro_Alloc:
330       ASSERT_AND_RET_DBG(
331         (
332          (proj == pn_Alloc_M        && mode == mode_M) ||
333          (proj == pn_Alloc_X_except /* && mode == mode_X*/) ||
334          (proj == pn_Alloc_res      && mode_is_reference(mode))
335         ),
336         "wrong Proj from Alloc", 0,
337         show_proj_failure(p);
338       );
339       break;
340
341     case iro_Proj:
342       {
343         type *mt; /* A method type */
344         pred = skip_nop(get_Proj_pred(pred));
345         ASSERT_AND_RET((get_irn_mode(pred) == mode_T), "Proj from something not a tuple", 0);
346         switch (get_irn_opcode(pred))
347         {
348           case iro_Start:
349             {
350               ASSERT_AND_RET(
351                   (proj >= 0 && mode_is_data(mode)),
352                   "wrong Proj from Proj from Start", 0);
353               mt = get_entity_type(get_irg_ent(irg));
354               ASSERT_AND_RET(
355                 (proj < get_method_n_params(mt)),
356                 "More Projs for args than args in type", 0
357               );
358               if ((mode_is_reference(mode)) && is_compound_type(get_method_param_type(mt, proj)))
359                 /* value argument */ break;
360
361               ASSERT_AND_RET(
362                   (mode == get_type_mode(get_method_param_type(mt, proj))),
363                   "Mode of Proj from Start doesn't match mode of param type.", 0);
364             }
365             break;
366
367           case iro_Call:
368             {
369               ASSERT_AND_RET(
370                   (proj >= 0 && mode_is_data(mode)),
371                   "wrong Proj from Proj from Call", 0);
372               mt = get_Call_type(pred);
373               ASSERT_AND_RET(
374                   (proj < get_method_n_ress(mt)),
375                   "More Projs for results than results in type.", 0);
376               if ((mode_is_reference(mode)) && is_compound_type(get_method_res_type(mt, proj)))
377                 /* value result */ break;
378
379               ASSERT_AND_RET(
380                   (mode == get_type_mode(get_method_res_type(mt, proj))),
381                   "Mode of Proj from Call doesn't match mode of result type.", 0);
382             }
383             break;
384
385           case iro_Tuple:
386             /* We don't test */
387             break;
388
389           default:
390             ASSERT_AND_RET(0, "Unknown opcode", 0);
391         }
392         break;
393
394       }
395     case iro_Tuple:
396       /* We don't test */
397       break;
398
399     case iro_CallBegin:
400       break;
401
402     case iro_EndReg:
403       break;
404
405     case iro_EndExcept:
406       break;
407
408     default:
409       ASSERT_AND_RET(0, "Unknown opcode", 0);
410   }
411
412   /* all went ok */
413   return 1;
414 }
415
416 int irn_vrfy_irg(ir_node *n, ir_graph *irg)
417 {
418   int i;
419   int opcode, opcode1;
420   ir_mode *mymode, *op1mode = NULL, *op2mode, *op3mode;
421   int op_is_symmetric = 1;  /*  0: asymmetric
422                                 1: operands have identical modes
423                                 2: modes of operands == mode of this node */
424   type *mt; /* A method type */
425
426   ir_node **in;
427
428   if (!opt_do_node_verification) return 1;
429
430   if (! interprocedural_view) {
431     /*
432      * do NOT check placement in interprocedural view, as we don't always know
433      * the "right" graph ...
434      */
435     ASSERT_AND_RET_DBG(
436       node_is_in_irgs_storage(irg, n),
437       "Node is not stored on proper IR graph!", 0,
438       show_node_on_graph(irg, n);
439     );
440   }
441
442   opcode = get_irn_opcode (n);
443
444   /* We don't want to test nodes whose predecessors are Bad or Unknown,
445      as we would have to special case that for each operation. */
446   if (opcode != iro_Phi && opcode != iro_Block)
447     for (i = 0; i < get_irn_arity(n); i++) {
448       opcode1 = get_irn_opcode(get_irn_n(n, i));
449       if (opcode1 == iro_Bad /*|| opcode1 == iro_Unknown*/)  /* GL: for analyses mode must be correct. */
450         return 1;
451     }
452
453   mymode = get_irn_mode (n);
454   in = get_irn_in (n);
455
456   switch (opcode)
457   {
458
459     case iro_Block:
460       for (i = 0; i < get_Block_n_cfgpreds(n); ++i) {
461         ir_node *pred =  get_Block_cfgpred(n, i);
462         ASSERT_AND_RET(
463           (is_Bad(pred)     ||
464            is_Unknown(pred) ||
465            (get_irn_mode(pred) == mode_X)
466           ), "Block node", 0);
467       }
468       // End block may only have Return, Raise or fragile ops as preds.
469       if (n == get_irg_end_block(irg))
470         for (i = 0; i < get_Block_n_cfgpreds(n); ++i) {
471           ir_node *pred =  skip_Proj(get_Block_cfgpred(n, i));
472           if (is_Proj(pred) || get_irn_op(pred) == op_Tuple)
473             break;   // We can not test properly.  How many tuples are there?
474           ASSERT_AND_RET(((get_irn_op(pred) == op_Return) ||
475                           is_Bad(pred)                    ||
476                           (get_irn_op(pred) == op_Raise)  ||
477                           is_fragile_op(pred)               ),
478                          "End Block node", 0);
479         }
480       // irg attr must == graph we are in.
481       if (! interprocedural_view) {
482         ASSERT_AND_RET(((get_irn_irg(n) && get_irn_irg(n) == irg)), "Block node has wrong irg attribute", 0);
483       }
484
485       break;
486
487     case iro_Start:
488       ASSERT_AND_RET(
489           /* Start: BB --> X x M x ref x data1 x ... x datan x ref */
490           mymode == mode_T, "Start node", 0
491           );
492       break;
493
494     case iro_Jmp:
495       ASSERT_AND_RET(
496           /* Jmp: BB --> X */
497           mymode == mode_X, "Jmp node", 0
498           );
499       break;
500
501     case iro_Break:
502       ASSERT_AND_RET(
503           /* Jmp: BB --> X */
504           mymode == mode_X, "Jmp node", 0
505           );
506       break;
507
508     case iro_Cond:
509       op1mode = get_irn_mode(in[1]);
510       ASSERT_AND_RET(
511           /* Cond: BB x b --> X x X */
512           (op1mode == mode_b ||
513            /* Cond: BB x int --> X^n */
514            mode_is_int(op1mode) ),  "Cond node", 0
515           );
516       ASSERT_AND_RET(mymode == mode_T, "Cond mode is not a tuple", 0);
517       break;
518
519     case iro_Return:
520       op1mode = get_irn_mode(in[1]);
521       /* Return: BB x M x data1 x ... x datan --> X */
522       /* printf("mode: %s, code %s\n", ID_TO_STR(n->mode->name), ID_TO_STR(n->op->name));*/
523       ASSERT_AND_RET( op1mode == mode_M, "Return node", 0 );  /* operand M */
524       for (i=2; i < get_irn_arity(n); i++) {
525         ASSERT_AND_RET( mode_is_data(get_irn_mode(in[i])), "Return node", 0 );  /* operand datai */
526       };
527       ASSERT_AND_RET( mymode == mode_X, "Result X", 0 );   /* result X */
528       /* Compare returned results with result types of method type */
529       mt = get_entity_type(get_irg_ent(irg));
530       ASSERT_AND_RET_DBG( get_Return_n_ress(n) == get_method_n_ress(mt),
531         "Number of results for Return doesn't match number of results in type.", 0,
532         show_return_nres(irg, n, mt););
533       for (i = 0; i < get_Return_n_ress(n); i++)
534         ASSERT_AND_RET_DBG(
535           get_irn_mode(get_Return_res(n, i)) == get_type_mode(get_method_res_type(mt, i)),
536           "Mode of result for Return doesn't match mode of result type.", 0,
537            show_return_modes(irg, n, mt, i););
538       break;
539
540     case iro_Raise:
541       op1mode = get_irn_mode(in[1]);
542       op2mode = get_irn_mode(in[2]);
543       ASSERT_AND_RET(
544           /* Sel: BB x M x ref --> X x M */
545           op1mode == mode_M && mode_is_reference(op2mode) &&
546           mymode == mode_T, "Raise node", 0
547           );
548       break;
549
550     case iro_Const:
551       ASSERT_AND_RET(
552           /* Const: BB --> data */
553           (mode_is_data (mymode) ||
554            mymode == mode_b)      /* we want boolean constants for static evaluation */
555           ,"Const node", 0        /* of Cmp. */
556           );
557       break;
558
559     case iro_SymConst:
560       ASSERT_AND_RET(
561           /* SymConst: BB --> int*/
562           (mode_is_int(mymode) ||
563            /* SymConst: BB --> ref */
564            mode_is_reference(mymode))
565           ,"SymConst node", 0);
566       break;
567
568     case iro_Sel:
569       op1mode = get_irn_mode(in[1]);
570       op2mode = get_irn_mode(in[2]);
571       ASSERT_AND_RET(
572           /* Sel: BB x M x ref x int^n --> ref */
573           (op1mode == mode_M && op2mode == mymode && mode_is_reference(mymode)),
574           "Sel node", 0
575           );
576       for (i=3; i < get_irn_arity(n); i++)
577       {
578         ASSERT_AND_RET(mode_is_int(get_irn_mode(in[i])), "Sel node", 0);
579       }
580       break;
581
582     case iro_InstOf:
583       ASSERT_AND_RET(mode_T == mymode, "mode of Instof is not a tuple", 0);
584       ASSERT_AND_RET(mode_is_data(op1mode), "Instof not on data", 0);
585       break;
586
587     case iro_Call:
588       op1mode = get_irn_mode(in[1]);
589       op2mode = get_irn_mode(in[2]);
590       /* Call: BB x M x ref x data1 x ... x datan
591          --> M x datan+1 x ... x data n+m */
592       ASSERT_AND_RET( op1mode == mode_M && mode_is_reference(op2mode), "Call node", 0 );  /* operand M x ref */
593       for (i=3; i < get_irn_arity(n); i++) {
594         ASSERT_AND_RET( mode_is_data(get_irn_mode(in[i])), "Call node", 0 );  /* operand datai */
595       };
596       ASSERT_AND_RET( mymode == mode_T, "Call result not a tuple", 0 );   /* result T */
597       /* Compare arguments of node with those of type */
598       mt = get_Call_type(n);
599
600       if (get_method_variadicity(mt) == variadicity_variadic) {
601         ASSERT_AND_RET_DBG(
602             get_Call_n_params(n) >= get_method_n_params(mt),
603             "Number of args for Call doesn't match number of args in variadic type.",
604             0,
605             fprintf(stderr, "Call has %d params, method %s type %d\n",
606               get_Call_n_params(n), get_type_name(mt), get_method_n_params(mt));
607             );
608       }
609       else {
610         ASSERT_AND_RET(
611             get_Call_n_params(n) == get_method_n_params(mt),
612             "Number of args for Call doesn't match number of args in non variadic type.",
613             0);
614       }
615
616       for (i = 0; i < get_method_n_params(mt); i++) {
617         ASSERT_AND_RET_DBG(
618             get_irn_mode(get_Call_param(n, i)) == get_type_mode(get_method_param_type(mt, i)),
619             "Mode of arg for Call doesn't match mode of arg type.", 0,
620             show_call_param(n, mt);
621             );
622       }
623       break;
624
625     case iro_Add:
626       op1mode = get_irn_mode(in[1]);
627       op2mode = get_irn_mode(in[2]);
628       ASSERT_AND_RET_DBG(
629           (
630            /* common Add: BB x numP x numP --> numP */
631            (op1mode == mymode && op2mode == op1mode && mode_is_numP(mymode)) ||
632            /* Pointer Add: BB x ref x int --> ref */
633            (mode_is_reference(op1mode) && mode_is_int(op2mode) && op1mode == mymode) ||
634            /* Pointer Add: BB x int x ref --> ref */
635            (mode_is_int(op1mode) && op2mode == mymode && mode_is_reference(mymode))
636           ),
637           "Add node", 0,
638           show_binop_failure(n, "/* common Add: BB x numP x numP --> numP */ |\n"
639                                 "/* Pointer Add: BB x ref x int --> ref */   |\n"
640                                 "/* Pointer Add: BB x int x ref --> ref */");
641           );
642       if (mode_is_reference(op1mode) != mode_is_reference(op2mode)) {
643         /* BB x ref x int --> ref or BB x int x ref --> ref */
644         op_is_symmetric = 0;
645       } else {
646         /* BB x num x num --> num or BB x ref x ref */
647         op_is_symmetric = 2;
648       }
649       break;
650
651     case iro_Sub:
652       op1mode = get_irn_mode(in[1]);
653       op2mode = get_irn_mode(in[2]);
654       ASSERT_AND_RET_DBG(
655           /* common Sub: BB x numP x numP --> numP */
656           ((mymode ==op1mode && mymode == op2mode && mode_is_numP(op1mode)) ||
657            /* Pointer Sub: BB x ref x int --> ref */
658            (op1mode == mymode && mode_is_int(op2mode) && mode_is_reference(mymode)) ||
659            /* Pointer Sub: BB x int x ref --> ref */
660            (mode_is_int(op1mode) && op2mode == mymode && mode_is_reference(mymode)) ||
661            /* Pointer Sub: BB x ref x ref --> int */
662            (op1mode == op2mode && mode_is_reference(op2mode) && mode_is_int(mymode))),
663           "Sub node", 0,
664           show_binop_failure(n, "/* common Sub: BB x numP x numP --> numP */ |\n"
665                                 "/* Pointer Sub: BB x ref x int --> ref */   |\n"
666                                 "/* Pointer Sub: BB x int x ref --> ref */   |\n"
667                                 "/* Pointer Sub: BB x ref x ref --> int */" );
668           );
669       if (mode_is_reference(op1mode) != mode_is_reference(op2mode)) {
670         op_is_symmetric = 0;
671       } else {
672         op_is_symmetric = 2;
673       }
674       break;
675
676     case iro_Minus:
677       op1mode = get_irn_mode(in[1]);
678       ASSERT_AND_RET_DBG(
679           /* Minus: BB x float --> float */
680           op1mode == mymode && get_mode_sort(op1mode) == irms_float_number, "Minus node", 0,
681           show_unop_failure(n , "/* Minus: BB x float --> float */");
682           );
683       op_is_symmetric = 2;
684       break;
685
686     case iro_Mul:
687       op1mode = get_irn_mode(in[1]);
688       op2mode = get_irn_mode(in[2]);
689       ASSERT_AND_RET_DBG(
690           /* Mul: BB x int1 x int1 --> int2 */
691           ((mode_is_int(op1mode)   && op2mode == op1mode && mode_is_int(mymode)) ||
692            (mode_is_float(op1mode) && op2mode == op1mode && mymode == op1mode)),
693           "Mul node",0,
694           show_binop_failure(n, "/* Mul: BB x int1 x int1 --> int2 */");
695           );
696       op_is_symmetric = 2;
697       break;
698
699     case iro_Quot:
700       op1mode = get_irn_mode(in[1]);
701       op2mode = get_irn_mode(in[2]);
702       op3mode = get_irn_mode(in[3]);
703       ASSERT_AND_RET_DBG(
704           /* Quot: BB x M x float x float --> M x X x float */
705           op1mode == mode_M && op2mode == op3mode &&
706           get_mode_sort(op2mode) == irms_float_number &&
707           mymode == mode_T,
708           "Quot node",0,
709           show_binop_failure(n, "/* Quot: BB x M x float x float --> M x X x float */");
710           );
711       op_is_symmetric = 2;
712       break;
713
714     case iro_DivMod:
715       op1mode = get_irn_mode(in[1]);
716       op2mode = get_irn_mode(in[2]);
717       op3mode = get_irn_mode(in[3]);
718       ASSERT_AND_RET(
719           /* DivMod: BB x M x int x int --> M x X x int x int */
720           op1mode == mode_M &&
721           mode_is_int(op2mode) &&
722           op3mode == op2mode &&
723           mymode == mode_T,
724           "DivMod node", 0
725           );
726       op_is_symmetric = 1;
727       break;
728
729     case iro_Div:
730     case iro_Mod:
731       op1mode = get_irn_mode(in[1]);
732       op2mode = get_irn_mode(in[2]);
733       op3mode = get_irn_mode(in[3]);
734       ASSERT_AND_RET(
735           /* Div or Mod: BB x M x int x int --> M x X x int */
736           op1mode == mode_M &&
737           op2mode == op3mode &&
738           mode_is_int(op2mode) &&
739           mymode == mode_T,
740           "Div or Mod node", 0
741           );
742       op_is_symmetric = 1;
743       break;
744
745     case iro_Abs:
746       op1mode = get_irn_mode(in[1]);
747       ASSERT_AND_RET_DBG(
748         /* Abs: BB x num --> num */
749         op1mode == mymode &&
750         mode_is_num (op1mode),
751         "Abs node", 0,
752         show_unop_failure(n, "/* Abs: BB x num --> num */");
753       );
754       op_is_symmetric = 2;
755       break;
756
757     case iro_And:
758     case iro_Or:
759     case iro_Eor:
760       op1mode = get_irn_mode(in[1]);
761       op2mode = get_irn_mode(in[2]);
762       ASSERT_AND_RET_DBG(
763         /* And or Or or Eor: BB x int x int --> int */
764         mode_is_int(mymode) &&
765         op2mode == op1mode &&
766         mymode == op2mode,
767         "And, Or or Eor node", 0,
768         show_binop_failure(n, "/* And or Or or Eor: BB x int x int --> int */");
769       );
770       op_is_symmetric = 2;
771       break;
772
773     case iro_Not:
774       op1mode = get_irn_mode(in[1]);
775       ASSERT_AND_RET_DBG(
776         /* Not: BB x int --> int */
777         mode_is_int(mymode) &&
778         mymode == op1mode,
779         "Not node", 0,
780         show_unop_failure(n, "/* Not: BB x int --> int */");
781       );
782       op_is_symmetric = 2;
783       break;
784
785
786     case iro_Cmp:
787       op1mode = get_irn_mode(in[1]);
788       op2mode = get_irn_mode(in[2]);
789       ASSERT_AND_RET_DBG(
790         /* Cmp: BB x datab x datab --> b16 */
791         mode_is_data (op1mode) &&
792         op2mode == op1mode &&
793         mymode == mode_T,
794         "Cmp node", 0,
795         show_binop_failure(n, "/* Cmp: BB x datab x datab --> b16 */");
796       );
797       break;
798
799     case iro_Shl:
800     case iro_Shr:
801     case iro_Shrs:
802       op1mode = get_irn_mode(in[1]);
803       op2mode = get_irn_mode(in[2]);
804       ASSERT_AND_RET_DBG(
805         /* Shl, Shr or Shrs: BB x int x int_u --> int */
806         mode_is_int(op1mode) &&
807         mode_is_int(op2mode) &&
808         !mode_is_signed(op2mode) &&
809         mymode == op1mode,
810         "Shl, Shr, Shr or Rot node", 0,
811         show_binop_failure(n, "/* Shl, Shr or Shrs: BB x int x int_u --> int */");
812       );
813       break;
814
815     case iro_Rot:
816       op1mode = get_irn_mode(in[1]);
817       op2mode = get_irn_mode(in[2]);
818       ASSERT_AND_RET_DBG(
819         /* Rot: BB x int x int --> int */
820         mode_is_int(op1mode) &&
821         mode_is_int(op2mode) &&
822         mymode == op1mode,
823         "Rot node", 0,
824         show_binop_failure(n, "/* Rot: BB x int x int --> int */");
825       );
826       break;
827
828     case iro_Conv:
829       op1mode = get_irn_mode(in[1]);
830       ASSERT_AND_RET_DBG(
831         /* Conv: BB x datab1 --> datab2 */
832         mode_is_datab(op1mode) && mode_is_data(mymode),
833         "Conv node", 0,
834         show_unop_failure(n, "/* Conv: BB x datab1 --> datab2 */");
835       );
836       break;
837
838     case iro_Cast:
839       op1mode = get_irn_mode(in[1]);
840       ASSERT_AND_RET_DBG(
841         /* Conv: BB x datab1 --> datab2 */
842         mode_is_data(op1mode) && op1mode == mymode,
843         "Cast node", 0,
844         show_unop_failure(n, "/* Conv: BB x datab1 --> datab2 */");
845       );
846       break;
847
848     case iro_Phi:
849       /* Phi: BB x dataM^n --> dataM */
850       /* for some reason "<=" aborts. int there a problem with get_store? */
851       for (i=1; i < get_irn_arity(n); i++) {
852         if (!is_Bad(in[i]) && (get_irn_op(in[i]) != op_Unknown))
853           ASSERT_AND_RET( get_irn_mode(in[i]) == mymode, "Phi node", 0);
854       };
855       ASSERT_AND_RET( mode_is_dataM(mymode), "Phi node", 0 );
856       break;
857
858     case iro_Load:
859       op1mode = get_irn_mode(in[1]);
860       op2mode = get_irn_mode(in[2]);
861       ASSERT_AND_RET(
862           /* Load: BB x M x ref --> M x X x data */
863           op1mode == mode_M && mode_is_reference(op2mode),
864           "Load node", 0
865           );
866       ASSERT_AND_RET( mymode == mode_T, "Load node", 0 );
867       break;
868
869     case iro_Store:
870       op1mode = get_irn_mode(in[1]);
871       op2mode = get_irn_mode(in[2]);
872       op3mode = get_irn_mode(in[3]);
873       ASSERT_AND_RET(
874           /* Load: BB x M x ref data --> M x X */
875           op1mode == mode_M && mode_is_reference(op2mode) && mode_is_data(op3mode),
876           "Store node", 0
877           );
878       ASSERT_AND_RET(mymode == mode_T, "Store node", 0);
879       break;
880
881     case iro_Alloc:
882       op1mode = get_irn_mode(in[1]);
883       op2mode = get_irn_mode(in[2]);
884       ASSERT_AND_RET_DBG(
885         /* Alloc: BB x M x int_u --> M x X x ref */
886         op1mode == mode_M &&
887         mode_is_int(op2mode) &&
888         !mode_is_signed(op2mode) &&
889         mymode == mode_T,
890         "Alloc node", 0,
891         show_binop_failure(n, "/* Alloc: BB x M x int_u --> M x X x ref */");
892       );
893       break;
894
895     case iro_Free:
896       op1mode = get_irn_mode(in[1]);
897       op2mode = get_irn_mode(in[2]);
898       ASSERT_AND_RET_DBG(
899         /* Free: BB x M x ref --> M */
900         op1mode == mode_M && mode_is_reference(op2mode) &&
901         mymode == mode_M,
902         "Free node", 0,
903         show_binop_failure(n, "/* Free: BB x M x ref --> M */");
904       );
905       break;
906
907     case iro_Sync:
908       /* Sync: BB x M^n --> M */
909       for (i=1; i < get_irn_arity(n); i++) {
910         ASSERT_AND_RET( get_irn_mode(in[i]) == mode_M, "Sync node", 0 );
911       };
912       ASSERT_AND_RET( mymode == mode_M, "Sync node", 0 );
913       break;
914
915     case iro_Proj:
916       return vrfy_Proj_proj(n, irg);
917       break;
918
919     case iro_Confirm:
920       op1mode = get_irn_mode(in[1]);
921       op2mode = get_irn_mode(in[2]);
922       ASSERT_AND_RET_DBG(
923         /* Confirm: BB x T x T --> T */
924         op1mode == mymode &&
925         op2mode == mymode,
926         "Confirm node", 0,
927         show_binop_failure(n, "/* Confirm: BB x T x T --> T */");
928       );
929       break;
930
931     default:
932       break;
933   }
934
935   /* All went ok */
936   return 1;
937 }
938
939 int irn_vrfy(ir_node *n)
940 {
941   int res = 1;
942 #ifdef DEBUG_libfirm
943   res = irn_vrfy_irg(n, current_ir_graph);
944 #endif
945   return res;
946 }
947
948 /*******************************************************************/
949 /* Verify the whole graph.                                         */
950 /*******************************************************************/
951
952 static void vrfy_wrap(ir_node *node, void *env)
953 {
954   int *res = env;
955
956   *res = irn_vrfy(node);
957 }
958
959 int irg_vrfy(ir_graph *irg)
960 {
961   int res = 1;
962 #ifdef DEBUG_libfirm
963   ir_graph *rem;
964
965   rem = current_ir_graph;
966   current_ir_graph = irg;
967
968   assert(get_irg_pinned(irg) == pinned);
969
970   irg_walk(irg->end, vrfy_wrap, NULL, &res);
971
972   current_ir_graph = rem;
973 #endif
974   return res;
975 }