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);
42 vrfy_Proj_proj(ir_node *p, ir_graph *irg) {
47 pred = skip_nop(get_Proj_pred(p));
48 assert(get_irn_mode(pred) == mode_T);
49 mode = get_irn_mode(p);
50 proj = get_Proj_proj(p);
52 switch (get_irn_opcode(pred)) {
56 (proj == pns_initial_exec && mode == mode_X) ||
57 (proj == pns_global_store && mode == mode_M) ||
58 (proj == pns_frame_base && mode_is_reference(mode)) ||
59 (proj == pns_globals && mode_is_reference(mode)) ||
60 (proj == pns_args && mode == mode_T) ||
61 (proj == pns_value_arg_base && mode_is_reference(mode))
63 "wrong Proj from Start", 0);
67 ASSERT_AND_RET( (proj >= 0 && mode == mode_X), "wrong Proj from Cond", 0);
72 ((proj == 0 && mode == mode_X) ||
73 (proj == 1 && mode == mode_M)),
74 "wrong Proj from Raise", 0);
78 ASSERT_AND_RET( (proj >= 0 && mode == mode_X), "wrong Proj from InstOf", 0);
83 ((proj == 0 && mode == mode_M) ||
84 (proj == 1 && mode == mode_X) ||
85 (proj == 2 && mode == mode_T) ||
86 (proj == 3 && mode == mode_M)),
87 "wrong Proj from Call", 0);
92 ((proj == 0 && mode == mode_M) ||
93 (proj == 1 && mode == mode_X) ||
94 (proj == 2 && mode_is_float(mode))),
95 "wrong Proj from Quot", 0);
100 ((proj == 0 && mode == mode_M) ||
101 (proj == 1 && mode == mode_X) ||
102 (proj == 2 && mode == mode_Is) ||
103 (proj == 3 && mode_is_int(mode))),
104 "wrong Proj from DivMod", 0);
110 ((proj == 0 && mode == mode_M) ||
111 (proj == 1 && mode == mode_X) ||
112 (proj == 2 && mode_is_int(mode))),
113 "wrong Proj from Div or Mod", 0);
118 (proj >= 0 && proj <= 15 && mode == mode_b),
119 "wrong Proj from Cmp", 0);
124 ((proj == 0 && mode == mode_M) ||
125 (proj == 1 && mode == mode_X) ||
126 (proj == 2 && mode_is_data(mode))),
127 "wrong Proj from Load", 0);
132 ((proj == 0 && mode == mode_M) ||
133 (proj == 1 && mode == mode_X)),
134 "wrong Proj from Store", 0);
140 (proj == 0 && mode == mode_M) ||
141 (proj == 1 /* && mode == mode_X*/) ||
142 (proj == 2 && mode_is_reference(mode))
144 "wrong Proj from Alloc", 0);
149 type *mt; /* A method type */
150 pred = skip_nop(get_Proj_pred(pred));
151 ASSERT_AND_RET((get_irn_mode(pred) == mode_T), "Proj from something not a tuple", 0);
152 switch (get_irn_opcode(pred))
157 (proj >= 0 && mode_is_data(mode)),
158 "wrong Proj from Proj from Start", 0);
159 mt = get_entity_type(get_irg_ent(irg));
161 (proj < get_method_n_params(mt)),
162 "More Projs for args than args in type", 0);
163 if ((mode_is_reference(mode)) && is_compound_type(get_method_param_type(mt, proj)))
164 /* value argument */ break;
167 (mode == get_type_mode(get_method_param_type(mt, proj))),
168 "Mode of Proj from Start doesn't match mode of param type.", 0);
175 (proj >= 0 && mode_is_data(mode)),
176 "wrong Proj from Proj from Call", 0);
177 mt = get_Call_type(pred);
179 (proj < get_method_n_ress(mt)),
180 "More Projs for results than results in type.", 0);
181 if ((mode_is_reference(mode)) && is_compound_type(get_method_res_type(mt, proj)))
182 /* value result */ break;
185 (mode == get_type_mode(get_method_res_type(mt, proj))),
186 "Mode of Proj from Call doesn't match mode of result type.", 0);
195 ASSERT_AND_RET(0, "Unknown opcode", 0);
214 ASSERT_AND_RET(0, "Unknown opcode", 0);
221 int irn_vrfy_irg(ir_node *n, ir_graph *irg)
225 ir_mode *mymode, *op1mode = NULL, *op2mode, *op3mode;
226 int op_is_symmetric = 1; /* 0: asymmetric
227 1: operands have identical modes
228 2: modes of operands == mode of this node */
229 type *mt; /* A method type */
233 if (! interprocedural_view) {
235 * do NOT check placement in interprocedural view, as we don't always know
236 * the "right" graph ...
238 ASSERT_AND_RET(node_is_in_irgs_storage(irg, n), "Node is not stored on proper IR graph!", 0);
241 opcode = get_irn_opcode (n);
243 /* We don't want to test nodes whose predecessors are Bad or Unknown,
244 as we would have to special case that for each operation. */
245 if (opcode != iro_Phi && opcode != iro_Block)
246 for (i = 0; i < get_irn_arity(n); i++) {
247 opcode1 = get_irn_opcode(get_irn_n(n, i));
248 if (opcode1 == iro_Bad || opcode1 == iro_Unknown)
252 mymode = get_irn_mode (n);
259 /* Start: BB --> X x M x ref x data1 x ... x datan x ref */
260 mymode == mode_T, "Start node", 0
267 mymode == mode_X, "Jmp node", 0
274 mymode == mode_X, "Jmp node", 0
279 op1mode = get_irn_mode(in[1]);
281 /* Cond: BB x b --> X x X */
282 (op1mode == mode_b ||
283 /* Cond: BB x int --> X^n */
284 mode_is_int(op1mode) ), "Cond node", 0
286 ASSERT_AND_RET(mymode == mode_T, "Cond mode is not a tuple", 0);
290 op1mode = get_irn_mode(in[1]);
291 /* Return: BB x M x data1 x ... x datan --> X */
292 /* printf("mode: %s, code %s\n", ID_TO_STR(n->mode->name), ID_TO_STR(n->op->name));*/
293 ASSERT_AND_RET( op1mode == mode_M, "Return node", 0 ); /* operand M */
294 for (i=2; i < get_irn_arity(n); i++) {
295 ASSERT_AND_RET( mode_is_data(get_irn_mode(in[i])), "Return node", 0 ); /* operand datai */
297 ASSERT_AND_RET( mymode == mode_X, "Result X", 0 ); /* result X */
298 /* Compare returned results with result types of method type */
299 mt = get_entity_type(get_irg_ent(irg));
300 ASSERT_AND_RET( get_Return_n_ress(n) == get_method_n_ress(mt),
301 "Number of results for Return doesn't match number of results in type.", 0 );
302 for (i = 0; i < get_Return_n_ress(n); i++)
304 get_irn_mode(get_Return_res(n, i)) == get_type_mode(get_method_res_type(mt, i)),
305 "Mode of result for Return doesn't match mode of result type.", 0);
309 op1mode = get_irn_mode(in[1]);
310 op2mode = get_irn_mode(in[2]);
312 /* Sel: BB x M x ref --> X x M */
313 op1mode == mode_M && mode_is_reference(op2mode) &&
314 mymode == mode_T, "Raise node", 0
320 /* Const: BB --> data */
321 (mode_is_data (mymode) ||
322 mymode == mode_b) /* we want boolean constants for static evaluation */
323 ,"Const node", 0 /* of Cmp. */
329 /* SymConst: BB --> int*/
330 (mode_is_int(mymode) ||
331 /* SymConst: BB --> ref */
332 mode_is_reference(mymode))
333 ,"SymConst node", 0);
337 op1mode = get_irn_mode(in[1]);
338 op2mode = get_irn_mode(in[2]);
340 /* Sel: BB x M x ref x int^n --> ref */
341 (op1mode == mode_M && op2mode == mymode && mode_is_reference(mymode)),
344 for (i=3; i < get_irn_arity(n); i++)
346 ASSERT_AND_RET(mode_is_int(get_irn_mode(in[i])), "Sel node", 0);
351 ASSERT_AND_RET(mode_T == mymode, "mode of Instof is not a tuple", 0);
352 ASSERT_AND_RET(mode_is_data(op1mode), "Instof not on data", 0);
356 op1mode = get_irn_mode(in[1]);
357 op2mode = get_irn_mode(in[2]);
358 /* Call: BB x M x ref x data1 x ... x datan
359 --> M x datan+1 x ... x data n+m */
360 ASSERT_AND_RET( op1mode == mode_M && mode_is_reference(op2mode), "Call node", 0 ); /* operand M x ref */
361 for (i=3; i < get_irn_arity(n); i++) {
362 ASSERT_AND_RET( mode_is_data(get_irn_mode(in[i])), "Call node", 0 ); /* operand datai */
364 ASSERT_AND_RET( mymode == mode_T, "Call result not a tuple", 0 ); /* result T */
365 /* Compare arguments of node with those of type */
366 mt = get_Call_type(n);
368 if (get_method_variadicity(mt) == variadic) {
370 get_Call_n_params(n) >= get_method_n_params(mt),
371 "Number of args for Call doesn't match number of args in variadic type.",
376 get_Call_n_params(n) == get_method_n_params(mt),
377 "Number of args for Call doesn't match number of args in non variadic type.",
381 for (i = 0; i < get_method_n_params(mt); i++) {
383 get_irn_mode(get_Call_param(n, i)) == get_type_mode(get_method_param_type(mt, i)),
384 "Mode of arg for Call doesn't match mode of arg type.", 0,
388 fprintf(stderr, "Assertion for Call type-check failed: %s(", get_type_name(mt));
389 for (i = 0; i < get_method_n_params(mt); ++i) {
390 fprintf(stderr, "%s ", get_mode_name(get_type_mode(get_method_param_type(mt, i))));
392 fprintf(stderr, ") != CALL(");
394 for (i = 0; i < get_Call_n_params(n); ++i) {
395 fprintf(stderr, "%s ", get_mode_name(get_irn_mode(get_Call_param(n, i))));
397 fprintf(stderr, ")\n");
405 op1mode = get_irn_mode(in[1]);
406 op2mode = get_irn_mode(in[2]);
409 /* common Add: BB x numP x numP --> numP */
410 (op1mode == mymode && op2mode == op1mode && mode_is_numP(mymode)) ||
411 /* Pointer Add: BB x ref x int --> ref */
412 (mode_is_reference(op1mode) && mode_is_int(op2mode) && op1mode == mymode) ||
413 /* Pointer Add: BB x int x ref --> ref */
414 (mode_is_int(op1mode) && op2mode == mymode && mode_is_reference(mymode))
418 if (mode_is_reference(op1mode) != mode_is_reference(op2mode)) {
419 /* BB x ref x int --> ref or BB x int x ref --> ref */
422 /* BB x num x num --> num or BB x ref x ref */
428 op1mode = get_irn_mode(in[1]);
429 op2mode = get_irn_mode(in[2]);
431 /* common Sub: BB x numP x numP --> numP */
432 ((mymode ==op1mode && mymode == op2mode && mode_is_numP(op1mode)) ||
433 /* Pointer Sub: BB x ref x int --> ref */
434 (op1mode == mymode && mode_is_int(op2mode) && mode_is_reference(mymode)) ||
435 /* Pointer Sub: BB x int x ref --> ref */
436 (mode_is_int(op1mode) && op2mode == mymode && mode_is_reference(mymode)) ||
437 /* Pointer Sub: BB x ref x ref --> int */
438 (op1mode == op2mode && mode_is_reference(op2mode) && mode_is_int(mymode))),
441 if (mode_is_reference(op1mode) != mode_is_reference(op2mode)) {
449 op1mode = get_irn_mode(in[1]);
451 /* Minus: BB x float --> float */
452 op1mode == mymode && get_mode_sort(op1mode) == irms_float_number, "Minus node", 0
458 op1mode = get_irn_mode(in[1]);
459 op2mode = get_irn_mode(in[2]);
461 /* Mul: BB x int1 x int1 --> int2 */
462 ((mode_is_int(op1mode) && op2mode == op1mode && mode_is_int(mymode)) ||
463 (mode_is_float(op1mode) && op2mode == op1mode && mymode == op1mode)),
470 op1mode = get_irn_mode(in[1]);
471 op2mode = get_irn_mode(in[2]);
472 op3mode = get_irn_mode(in[3]);
474 /* Quot: BB x M x float x float --> M x X x float */
475 op1mode == mode_M && op2mode == op3mode &&
476 get_mode_sort(op2mode) == irms_float_number &&
484 op1mode = get_irn_mode(in[1]);
485 op2mode = get_irn_mode(in[2]);
486 op3mode = get_irn_mode(in[3]);
488 /* DivMod: BB x M x int x int --> M x X x int x int */
490 mode_is_int(op2mode) &&
491 op3mode == op2mode &&
500 op1mode = get_irn_mode(in[1]);
501 op2mode = get_irn_mode(in[2]);
502 op3mode = get_irn_mode(in[3]);
504 /* Div or Mod: BB x M x int x int --> M x X x int */
506 op2mode == op3mode &&
507 mode_is_int(op2mode) &&
515 op1mode = get_irn_mode(in[1]);
517 /* Abs: BB x num --> num */
519 mode_is_num (op1mode),
528 op1mode = get_irn_mode(in[1]);
529 op2mode = get_irn_mode(in[2]);
531 /* And or Or or Eor: BB x int x int --> int */
532 mode_is_int(mymode) &&
533 op2mode == op1mode &&
535 "And, Or or Eor node", 0
541 op1mode = get_irn_mode(in[1]);
543 /* Not: BB x int --> int */
544 mode_is_int(mymode) &&
553 op1mode = get_irn_mode(in[1]);
554 op2mode = get_irn_mode(in[2]);
556 /* Cmp: BB x datab x datab --> b16 */
557 mode_is_data (op1mode) &&
558 op2mode == op1mode &&
567 op1mode = get_irn_mode(in[1]);
568 op2mode = get_irn_mode(in[2]);
570 /* Shl, Shr or Shrs: BB x int x int_u --> int */
571 mode_is_int(op1mode) &&
572 mode_is_int(op2mode) &&
573 !mode_is_signed(op2mode) &&
575 "Shl, Shr, Shr or Rot node"
580 op1mode = get_irn_mode(in[1]);
581 op2mode = get_irn_mode(in[2]);
583 /* Rot: BB x int x int --> int */
584 mode_is_int(op1mode) &&
585 mode_is_int(op2mode) &&
592 op1mode = get_irn_mode(in[1]);
594 /* Conv: BB x datab1 --> datab2 */
595 mode_is_datab(op1mode) && mode_is_data(mymode),
601 op1mode = get_irn_mode(in[1]);
603 /* Conv: BB x datab1 --> datab2 */
604 mode_is_data(op1mode) && op1mode == mymode,
610 /* Phi: BB x dataM^n --> dataM */
611 /* for some reason "<=" aborts. int there a problem with get_store? */
612 for (i=1; i < get_irn_arity(n); i++) {
613 if (!is_Bad(in[i]) && (get_irn_op(in[i]) != op_Unknown))
614 ASSERT_AND_RET( get_irn_mode(in[i]) == mymode, "Phi node", 0);
616 ASSERT_AND_RET( mode_is_dataM(mymode), "Phi node", 0 );
620 op1mode = get_irn_mode(in[1]);
621 op2mode = get_irn_mode(in[2]);
623 /* Load: BB x M x ref --> M x X x data */
624 op1mode == mode_M && mode_is_reference(op2mode),
627 ASSERT_AND_RET( mymode == mode_T, "Load node", 0 );
631 op1mode = get_irn_mode(in[1]);
632 op2mode = get_irn_mode(in[2]);
633 op3mode = get_irn_mode(in[3]);
635 /* Load: BB x M x ref data --> M x X */
636 op1mode == mode_M && mode_is_reference(op2mode) && mode_is_data(op3mode),
639 ASSERT_AND_RET(mymode == mode_T, "Store node", 0);
643 op1mode = get_irn_mode(in[1]);
644 op2mode = get_irn_mode(in[2]);
646 /* Alloc: BB x M x int_u --> M x X x ref */
648 mode_is_int(op2mode) &&
649 !mode_is_signed(op2mode) &&
656 op1mode = get_irn_mode(in[1]);
657 op2mode = get_irn_mode(in[2]);
659 /* Free: BB x M x ref --> M */
660 op1mode == mode_M && mode_is_reference(op2mode) &&
667 /* Sync: BB x M^n --> M */
668 for (i=1; i < get_irn_arity(n); i++) {
669 ASSERT_AND_RET( get_irn_mode(in[i]) == mode_M, "Sync node", 0 );
671 ASSERT_AND_RET( mymode == mode_M, "Sync node", 0 );
675 return vrfy_Proj_proj(n, irg);
686 int irn_vrfy(ir_node *n)
688 return irn_vrfy_irg(n, current_ir_graph);
691 /*******************************************************************/
692 /* Verify the whole graph. */
693 /*******************************************************************/
695 static void vrfy_wrap(ir_node *node, void *env)
699 *res = irn_vrfy(node);
702 int irg_vrfy(ir_graph *irg)
707 rem = current_ir_graph;
708 current_ir_graph = irg;
710 assert(get_irg_pinned(irg) == pinned);
712 irg_walk(irg->end, vrfy_wrap, NULL, &res);
714 current_ir_graph = rem;