3 * File name: ir/ir/irvrfy.c
4 * Purpose: Check irnodes for correctness.
5 * Author: Christian Schaefer
6 * Modified by: Goetz Lindenmaier. Till Riedel
9 * Copyright: (c) 1998-2003 Universität Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
17 # include "irgraph_t.h"
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).
26 #define ASSERT_AND_RET(expr, string, ret) if (!(expr)) return (ret)
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
32 #define ASSERT_AND_RET_DBG(expr, string, ret, blk) if (!(expr)) return (ret)
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)
38 /* @@@ replace use of array "in" by access functions. */
39 ir_node **get_irn_in(ir_node *node);
41 bool opt_do_node_verification = 1;
42 void do_node_verification(bool b) {
43 opt_do_node_verification = b;
48 vrfy_Proj_proj(ir_node *p, ir_graph *irg) {
53 pred = skip_nop(get_Proj_pred(p));
54 assert(get_irn_mode(pred) == mode_T);
55 mode = get_irn_mode(p);
56 proj = get_Proj_proj(p);
58 switch (get_irn_opcode(pred)) {
62 (proj == pns_initial_exec && mode == mode_X) ||
63 (proj == pns_global_store && mode == mode_M) ||
64 (proj == pns_frame_base && mode_is_reference(mode)) ||
65 (proj == pns_globals && mode_is_reference(mode)) ||
66 (proj == pns_args && mode == mode_T) ||
67 (proj == pns_value_arg_base && mode_is_reference(mode))
69 "wrong Proj from Start", 0);
73 ASSERT_AND_RET( (proj >= 0 && mode == mode_X), "wrong Proj from Cond", 0);
78 ((proj == 0 && mode == mode_X) ||
79 (proj == 1 && mode == mode_M)),
80 "wrong Proj from Raise", 0);
84 ASSERT_AND_RET( (proj >= 0 && mode == mode_X), "wrong Proj from InstOf", 0);
89 ((proj == 0 && mode == mode_M) ||
90 (proj == 1 && mode == mode_X) ||
91 (proj == 2 && mode == mode_T) ||
92 (proj == 3 && mode == mode_M)),
93 "wrong Proj from Call", 0);
98 ((proj == 0 && mode == mode_M) ||
99 (proj == 1 && mode == mode_X) ||
100 (proj == 2 && mode_is_float(mode))),
101 "wrong Proj from Quot", 0);
106 ((proj == 0 && mode == mode_M) ||
107 (proj == 1 && mode == mode_X) ||
108 (proj == 2 && mode == mode_Is) ||
109 (proj == 3 && mode_is_int(mode))),
110 "wrong Proj from DivMod", 0);
116 ((proj == 0 && mode == mode_M) ||
117 (proj == 1 && mode == mode_X) ||
118 (proj == 2 && mode_is_int(mode))),
119 "wrong Proj from Div or Mod", 0);
124 (proj >= 0 && proj <= 15 && mode == mode_b),
125 "wrong Proj from Cmp", 0);
129 if (proj == pn_Load_res) {
130 ir_node *ptr = get_Load_ptr(pred);
132 if (get_irn_op(ptr) == op_Sel) {
133 ent = get_Sel_entity(ptr);
135 We may not test this, after lowering and optimization the Const can
136 have an unexpected type.
137 else if ((get_irn_op(ptr) == op_Const) &&
138 tarval_is_entity(get_Const_tarval(ptr))) {
139 ent = get_tarval_entity(get_Const_tarval(ptr));
142 ASSERT_AND_RET((mode == get_type_mode(get_entity_type(ent))),
143 "wrong data Proj from Load", 0);
145 ASSERT_AND_RET(mode_is_data(mode),
146 "wrong data Proj from Load", 0);
148 ASSERT_AND_RET(((proj == pn_Load_M && mode == mode_M) ||
149 (proj == pn_Load_X_except && mode == mode_X)),
150 "wrong Proj from Load", 0);
156 ((proj == 0 && mode == mode_M) ||
157 (proj == 1 && mode == mode_X)),
158 "wrong Proj from Store", 0);
164 (proj == 0 && mode == mode_M) ||
165 (proj == 1 /* && mode == mode_X*/) ||
166 (proj == 2 && mode_is_reference(mode))
168 "wrong Proj from Alloc", 0);
173 type *mt; /* A method type */
174 pred = skip_nop(get_Proj_pred(pred));
175 ASSERT_AND_RET((get_irn_mode(pred) == mode_T), "Proj from something not a tuple", 0);
176 switch (get_irn_opcode(pred))
181 (proj >= 0 && mode_is_data(mode)),
182 "wrong Proj from Proj from Start", 0);
183 mt = get_entity_type(get_irg_ent(irg));
185 (proj < get_method_n_params(mt)),
186 "More Projs for args than args in type", 0);
187 if ((mode_is_reference(mode)) && is_compound_type(get_method_param_type(mt, proj)))
188 /* value argument */ break;
191 (mode == get_type_mode(get_method_param_type(mt, proj))),
192 "Mode of Proj from Start doesn't match mode of param type.", 0);
199 (proj >= 0 && mode_is_data(mode)),
200 "wrong Proj from Proj from Call", 0);
201 mt = get_Call_type(pred);
203 (proj < get_method_n_ress(mt)),
204 "More Projs for results than results in type.", 0);
205 if ((mode_is_reference(mode)) && is_compound_type(get_method_res_type(mt, proj)))
206 /* value result */ break;
209 (mode == get_type_mode(get_method_res_type(mt, proj))),
210 "Mode of Proj from Call doesn't match mode of result type.", 0);
219 ASSERT_AND_RET(0, "Unknown opcode", 0);
238 ASSERT_AND_RET(0, "Unknown opcode", 0);
245 static void show_binop_failure(ir_node *n, const char *text)
247 ir_node *left = get_binop_left(n);
248 ir_node *right = get_binop_right(n);
250 fprintf(stderr, "Verification of node %s%s(%s, %s) did not match %s\n",
251 get_irn_opname(n), get_irn_modename(n),
252 get_irn_modename(left),
253 get_irn_modename(right),
257 int irn_vrfy_irg(ir_node *n, ir_graph *irg)
261 ir_mode *mymode, *op1mode = NULL, *op2mode, *op3mode;
262 int op_is_symmetric = 1; /* 0: asymmetric
263 1: operands have identical modes
264 2: modes of operands == mode of this node */
265 type *mt; /* A method type */
269 if (!opt_do_node_verification) return 1;
271 if (! interprocedural_view) {
273 * do NOT check placement in interprocedural view, as we don't always know
274 * the "right" graph ...
276 ASSERT_AND_RET(node_is_in_irgs_storage(irg, n), "Node is not stored on proper IR graph!", 0);
279 opcode = get_irn_opcode (n);
281 /* We don't want to test nodes whose predecessors are Bad or Unknown,
282 as we would have to special case that for each operation. */
283 if (opcode != iro_Phi && opcode != iro_Block)
284 for (i = 0; i < get_irn_arity(n); i++) {
285 opcode1 = get_irn_opcode(get_irn_n(n, i));
286 if (opcode1 == iro_Bad /*|| opcode1 == iro_Unknown*/) /* GL: for analyses mode must be correct. */
290 mymode = get_irn_mode (n);
297 for (i = 0; i < get_Block_n_cfgpreds(n); ++i) {
298 ir_node *pred = get_Block_cfgpred(n, i);
299 ASSERT_AND_RET((is_Bad(pred) ||
300 (get_irn_op(pred) == op_Unknown) ||
301 (get_irn_mode(pred) == mode_X) ),
304 // End block may only have Return, Raise or fragile ops as preds.
305 if (n == get_irg_end_block(irg))
306 for (i = 0; i < get_Block_n_cfgpreds(n); ++i) {
307 ir_node *pred = skip_Proj(get_Block_cfgpred(n, i));
308 if (is_Proj(pred) || get_irn_op(pred) == op_Tuple)
309 break; // We can not test properly. How many tuples are there?
310 ASSERT_AND_RET(((get_irn_op(pred) == op_Return) ||
312 (get_irn_op(pred) == op_Raise) ||
313 is_fragile_op(pred) ),
314 "End Block node", 0);
316 // irg attr must == graph we are in.
317 if (! interprocedural_view) {
318 ASSERT_AND_RET(((get_irn_irg(n) && get_irn_irg(n) == irg)), "", 0);
325 /* Start: BB --> X x M x ref x data1 x ... x datan x ref */
326 mymode == mode_T, "Start node", 0
333 mymode == mode_X, "Jmp node", 0
340 mymode == mode_X, "Jmp node", 0
345 op1mode = get_irn_mode(in[1]);
347 /* Cond: BB x b --> X x X */
348 (op1mode == mode_b ||
349 /* Cond: BB x int --> X^n */
350 mode_is_int(op1mode) ), "Cond node", 0
352 ASSERT_AND_RET(mymode == mode_T, "Cond mode is not a tuple", 0);
356 op1mode = get_irn_mode(in[1]);
357 /* Return: BB x M x data1 x ... x datan --> X */
358 /* printf("mode: %s, code %s\n", ID_TO_STR(n->mode->name), ID_TO_STR(n->op->name));*/
359 ASSERT_AND_RET( op1mode == mode_M, "Return node", 0 ); /* operand M */
360 for (i=2; i < get_irn_arity(n); i++) {
361 ASSERT_AND_RET( mode_is_data(get_irn_mode(in[i])), "Return node", 0 ); /* operand datai */
363 ASSERT_AND_RET( mymode == mode_X, "Result X", 0 ); /* result X */
364 /* Compare returned results with result types of method type */
365 mt = get_entity_type(get_irg_ent(irg));
366 ASSERT_AND_RET( get_Return_n_ress(n) == get_method_n_ress(mt),
367 "Number of results for Return doesn't match number of results in type.", 0 );
368 for (i = 0; i < get_Return_n_ress(n); i++)
370 get_irn_mode(get_Return_res(n, i)) == get_type_mode(get_method_res_type(mt, i)),
371 "Mode of result for Return doesn't match mode of result type.", 0);
375 op1mode = get_irn_mode(in[1]);
376 op2mode = get_irn_mode(in[2]);
378 /* Sel: BB x M x ref --> X x M */
379 op1mode == mode_M && mode_is_reference(op2mode) &&
380 mymode == mode_T, "Raise node", 0
386 /* Const: BB --> data */
387 (mode_is_data (mymode) ||
388 mymode == mode_b) /* we want boolean constants for static evaluation */
389 ,"Const node", 0 /* of Cmp. */
395 /* SymConst: BB --> int*/
396 (mode_is_int(mymode) ||
397 /* SymConst: BB --> ref */
398 mode_is_reference(mymode))
399 ,"SymConst node", 0);
403 op1mode = get_irn_mode(in[1]);
404 op2mode = get_irn_mode(in[2]);
406 /* Sel: BB x M x ref x int^n --> ref */
407 (op1mode == mode_M && op2mode == mymode && mode_is_reference(mymode)),
410 for (i=3; i < get_irn_arity(n); i++)
412 ASSERT_AND_RET(mode_is_int(get_irn_mode(in[i])), "Sel node", 0);
417 ASSERT_AND_RET(mode_T == mymode, "mode of Instof is not a tuple", 0);
418 ASSERT_AND_RET(mode_is_data(op1mode), "Instof not on data", 0);
422 op1mode = get_irn_mode(in[1]);
423 op2mode = get_irn_mode(in[2]);
424 /* Call: BB x M x ref x data1 x ... x datan
425 --> M x datan+1 x ... x data n+m */
426 ASSERT_AND_RET( op1mode == mode_M && mode_is_reference(op2mode), "Call node", 0 ); /* operand M x ref */
427 for (i=3; i < get_irn_arity(n); i++) {
428 ASSERT_AND_RET( mode_is_data(get_irn_mode(in[i])), "Call node", 0 ); /* operand datai */
430 ASSERT_AND_RET( mymode == mode_T, "Call result not a tuple", 0 ); /* result T */
431 /* Compare arguments of node with those of type */
432 mt = get_Call_type(n);
434 if (get_method_variadicity(mt) == variadicity_variadic) {
436 get_Call_n_params(n) >= get_method_n_params(mt),
437 "Number of args for Call doesn't match number of args in variadic type.",
442 get_Call_n_params(n) == get_method_n_params(mt),
443 "Number of args for Call doesn't match number of args in non variadic type.",
447 for (i = 0; i < get_method_n_params(mt); i++) {
449 get_irn_mode(get_Call_param(n, i)) == get_type_mode(get_method_param_type(mt, i)),
450 "Mode of arg for Call doesn't match mode of arg type.", 0,
454 fprintf(stderr, "Assertion for Call type-check failed: %s(", get_type_name(mt));
455 for (i = 0; i < get_method_n_params(mt); ++i) {
456 fprintf(stderr, "%s ", get_mode_name(get_type_mode(get_method_param_type(mt, i))));
458 fprintf(stderr, ") != CALL(");
460 for (i = 0; i < get_Call_n_params(n); ++i) {
461 fprintf(stderr, "%s ", get_mode_name(get_irn_mode(get_Call_param(n, i))));
463 fprintf(stderr, ")\n");
471 op1mode = get_irn_mode(in[1]);
472 op2mode = get_irn_mode(in[2]);
475 /* common Add: BB x numP x numP --> numP */
476 (op1mode == mymode && op2mode == op1mode && mode_is_numP(mymode)) ||
477 /* Pointer Add: BB x ref x int --> ref */
478 (mode_is_reference(op1mode) && mode_is_int(op2mode) && op1mode == mymode) ||
479 /* Pointer Add: BB x int x ref --> ref */
480 (mode_is_int(op1mode) && op2mode == mymode && mode_is_reference(mymode))
484 if (mode_is_reference(op1mode) != mode_is_reference(op2mode)) {
485 /* BB x ref x int --> ref or BB x int x ref --> ref */
488 /* BB x num x num --> num or BB x ref x ref */
494 op1mode = get_irn_mode(in[1]);
495 op2mode = get_irn_mode(in[2]);
497 /* common Sub: BB x numP x numP --> numP */
498 ((mymode ==op1mode && mymode == op2mode && mode_is_numP(op1mode)) ||
499 /* Pointer Sub: BB x ref x int --> ref */
500 (op1mode == mymode && mode_is_int(op2mode) && mode_is_reference(mymode)) ||
501 /* Pointer Sub: BB x int x ref --> ref */
502 (mode_is_int(op1mode) && op2mode == mymode && mode_is_reference(mymode)) ||
503 /* Pointer Sub: BB x ref x ref --> int */
504 (op1mode == op2mode && mode_is_reference(op2mode) && mode_is_int(mymode))),
507 if (mode_is_reference(op1mode) != mode_is_reference(op2mode)) {
515 op1mode = get_irn_mode(in[1]);
517 /* Minus: BB x float --> float */
518 op1mode == mymode && get_mode_sort(op1mode) == irms_float_number, "Minus node", 0
524 op1mode = get_irn_mode(in[1]);
525 op2mode = get_irn_mode(in[2]);
527 /* Mul: BB x int1 x int1 --> int2 */
528 ((mode_is_int(op1mode) && op2mode == op1mode && mode_is_int(mymode)) ||
529 (mode_is_float(op1mode) && op2mode == op1mode && mymode == op1mode)),
531 show_binop_failure(n, "/* Mul: BB x int1 x int1 --> int2 */");
537 op1mode = get_irn_mode(in[1]);
538 op2mode = get_irn_mode(in[2]);
539 op3mode = get_irn_mode(in[3]);
541 /* Quot: BB x M x float x float --> M x X x float */
542 op1mode == mode_M && op2mode == op3mode &&
543 get_mode_sort(op2mode) == irms_float_number &&
546 show_binop_failure(n, "/* Quot: BB x M x float x float --> M x X x float */");
552 op1mode = get_irn_mode(in[1]);
553 op2mode = get_irn_mode(in[2]);
554 op3mode = get_irn_mode(in[3]);
556 /* DivMod: BB x M x int x int --> M x X x int x int */
558 mode_is_int(op2mode) &&
559 op3mode == op2mode &&
568 op1mode = get_irn_mode(in[1]);
569 op2mode = get_irn_mode(in[2]);
570 op3mode = get_irn_mode(in[3]);
572 /* Div or Mod: BB x M x int x int --> M x X x int */
574 op2mode == op3mode &&
575 mode_is_int(op2mode) &&
583 op1mode = get_irn_mode(in[1]);
585 /* Abs: BB x num --> num */
587 mode_is_num (op1mode),
596 op1mode = get_irn_mode(in[1]);
597 op2mode = get_irn_mode(in[2]);
599 /* And or Or or Eor: BB x int x int --> int */
600 mode_is_int(mymode) &&
601 op2mode == op1mode &&
603 "And, Or or Eor node", 0
609 op1mode = get_irn_mode(in[1]);
611 /* Not: BB x int --> int */
612 mode_is_int(mymode) &&
621 op1mode = get_irn_mode(in[1]);
622 op2mode = get_irn_mode(in[2]);
624 /* Cmp: BB x datab x datab --> b16 */
625 mode_is_data (op1mode) &&
626 op2mode == op1mode &&
635 op1mode = get_irn_mode(in[1]);
636 op2mode = get_irn_mode(in[2]);
638 /* Shl, Shr or Shrs: BB x int x int_u --> int */
639 mode_is_int(op1mode) &&
640 mode_is_int(op2mode) &&
641 !mode_is_signed(op2mode) &&
643 "Shl, Shr, Shr or Rot node"
648 op1mode = get_irn_mode(in[1]);
649 op2mode = get_irn_mode(in[2]);
651 /* Rot: BB x int x int --> int */
652 mode_is_int(op1mode) &&
653 mode_is_int(op2mode) &&
660 op1mode = get_irn_mode(in[1]);
662 /* Conv: BB x datab1 --> datab2 */
663 mode_is_datab(op1mode) && mode_is_data(mymode),
669 op1mode = get_irn_mode(in[1]);
671 /* Conv: BB x datab1 --> datab2 */
672 mode_is_data(op1mode) && op1mode == mymode,
678 /* Phi: BB x dataM^n --> dataM */
679 /* for some reason "<=" aborts. int there a problem with get_store? */
680 for (i=1; i < get_irn_arity(n); i++) {
681 if (!is_Bad(in[i]) && (get_irn_op(in[i]) != op_Unknown))
682 ASSERT_AND_RET( get_irn_mode(in[i]) == mymode, "Phi node", 0);
684 ASSERT_AND_RET( mode_is_dataM(mymode), "Phi node", 0 );
688 op1mode = get_irn_mode(in[1]);
689 op2mode = get_irn_mode(in[2]);
691 /* Load: BB x M x ref --> M x X x data */
692 op1mode == mode_M && mode_is_reference(op2mode),
695 ASSERT_AND_RET( mymode == mode_T, "Load node", 0 );
699 op1mode = get_irn_mode(in[1]);
700 op2mode = get_irn_mode(in[2]);
701 op3mode = get_irn_mode(in[3]);
703 /* Load: BB x M x ref data --> M x X */
704 op1mode == mode_M && mode_is_reference(op2mode) && mode_is_data(op3mode),
707 ASSERT_AND_RET(mymode == mode_T, "Store node", 0);
711 op1mode = get_irn_mode(in[1]);
712 op2mode = get_irn_mode(in[2]);
714 /* Alloc: BB x M x int_u --> M x X x ref */
716 mode_is_int(op2mode) &&
717 !mode_is_signed(op2mode) &&
724 op1mode = get_irn_mode(in[1]);
725 op2mode = get_irn_mode(in[2]);
727 /* Free: BB x M x ref --> M */
728 op1mode == mode_M && mode_is_reference(op2mode) &&
735 /* Sync: BB x M^n --> M */
736 for (i=1; i < get_irn_arity(n); i++) {
737 ASSERT_AND_RET( get_irn_mode(in[i]) == mode_M, "Sync node", 0 );
739 ASSERT_AND_RET( mymode == mode_M, "Sync node", 0 );
743 return vrfy_Proj_proj(n, irg);
747 op1mode = get_irn_mode(in[1]);
748 op2mode = get_irn_mode(in[2]);
750 /* Confirm: BB x T x T --> T */
766 int irn_vrfy(ir_node *n)
768 return irn_vrfy_irg(n, current_ir_graph);
771 /*******************************************************************/
772 /* Verify the whole graph. */
773 /*******************************************************************/
775 static void vrfy_wrap(ir_node *node, void *env)
779 *res = irn_vrfy(node);
782 int irg_vrfy(ir_graph *irg)
787 rem = current_ir_graph;
788 current_ir_graph = irg;
790 assert(get_irg_pinned(irg) == pinned);
792 irg_walk(irg->end, vrfy_wrap, NULL, &res);
794 current_ir_graph = rem;