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);
123 if (proj == pn_Load_res) {
124 ir_node *ptr = get_Load_ptr(pred);
126 if (get_irn_op(ptr) == op_Sel) {
127 ent = get_Sel_entity(ptr);
128 } else if ((get_irn_op(ptr) == op_Const) &&
129 tarval_is_entity(get_Const_tarval(ptr))) {
130 ent = get_tarval_entity(get_Const_tarval(ptr));
133 ASSERT_AND_RET((mode == get_type_mode(get_entity_type(ent))),
134 "wrong data Proj from Load", 0);
136 ASSERT_AND_RET(mode_is_data(mode),
137 "wrong data Proj from Load", 0);
139 ASSERT_AND_RET(((proj == pn_Load_M && mode == mode_M) ||
140 (proj == pn_Load_X_except && mode == mode_X)),
141 "wrong Proj from Load", 0);
147 ((proj == 0 && mode == mode_M) ||
148 (proj == 1 && mode == mode_X)),
149 "wrong Proj from Store", 0);
155 (proj == 0 && mode == mode_M) ||
156 (proj == 1 /* && mode == mode_X*/) ||
157 (proj == 2 && mode_is_reference(mode))
159 "wrong Proj from Alloc", 0);
164 type *mt; /* A method type */
165 pred = skip_nop(get_Proj_pred(pred));
166 ASSERT_AND_RET((get_irn_mode(pred) == mode_T), "Proj from something not a tuple", 0);
167 switch (get_irn_opcode(pred))
172 (proj >= 0 && mode_is_data(mode)),
173 "wrong Proj from Proj from Start", 0);
174 mt = get_entity_type(get_irg_ent(irg));
176 (proj < get_method_n_params(mt)),
177 "More Projs for args than args in type", 0);
178 if ((mode_is_reference(mode)) && is_compound_type(get_method_param_type(mt, proj)))
179 /* value argument */ break;
182 (mode == get_type_mode(get_method_param_type(mt, proj))),
183 "Mode of Proj from Start doesn't match mode of param type.", 0);
190 (proj >= 0 && mode_is_data(mode)),
191 "wrong Proj from Proj from Call", 0);
192 mt = get_Call_type(pred);
194 (proj < get_method_n_ress(mt)),
195 "More Projs for results than results in type.", 0);
196 if ((mode_is_reference(mode)) && is_compound_type(get_method_res_type(mt, proj)))
197 /* value result */ break;
200 (mode == get_type_mode(get_method_res_type(mt, proj))),
201 "Mode of Proj from Call doesn't match mode of result type.", 0);
210 ASSERT_AND_RET(0, "Unknown opcode", 0);
229 ASSERT_AND_RET(0, "Unknown opcode", 0);
236 int irn_vrfy_irg(ir_node *n, ir_graph *irg)
240 ir_mode *mymode, *op1mode = NULL, *op2mode, *op3mode;
241 int op_is_symmetric = 1; /* 0: asymmetric
242 1: operands have identical modes
243 2: modes of operands == mode of this node */
244 type *mt; /* A method type */
248 if (! interprocedural_view) {
250 * do NOT check placement in interprocedural view, as we don't always know
251 * the "right" graph ...
253 ASSERT_AND_RET(node_is_in_irgs_storage(irg, n), "Node is not stored on proper IR graph!", 0);
256 opcode = get_irn_opcode (n);
258 /* We don't want to test nodes whose predecessors are Bad or Unknown,
259 as we would have to special case that for each operation. */
260 if (opcode != iro_Phi && opcode != iro_Block)
261 for (i = 0; i < get_irn_arity(n); i++) {
262 opcode1 = get_irn_opcode(get_irn_n(n, i));
263 if (opcode1 == iro_Bad || opcode1 == iro_Unknown)
267 mymode = get_irn_mode (n);
274 for (i = 0; i < get_Block_n_cfgpreds(n); ++i) {
275 ir_node *pred = get_Block_cfgpred(n, i);
276 ASSERT_AND_RET((is_Bad(pred) || get_irn_op(pred) == op_Unknown || (get_irn_mode(pred) == mode_X)), "Block node", 0);
278 // End block may only have Return, Raise or fragile ops as preds.
279 if (n == get_irg_end_block(irg))
280 for (i = 0; i < get_Block_n_cfgpreds(n); ++i) {
281 ir_node *pred = skip_Proj(get_Block_cfgpred(n, i));
282 if (is_Proj(pred) || get_irn_op(pred) == op_Tuple)
283 break; // We can not test properly. How many tuples are there?
284 ASSERT_AND_RET(((get_irn_op(pred) == op_Return) ||
286 (get_irn_op(pred) == op_Raise) ||
287 is_fragile_op(pred) ),
288 "End Block node", 0);
294 /* Start: BB --> X x M x ref x data1 x ... x datan x ref */
295 mymode == mode_T, "Start node", 0
302 mymode == mode_X, "Jmp node", 0
309 mymode == mode_X, "Jmp node", 0
314 op1mode = get_irn_mode(in[1]);
316 /* Cond: BB x b --> X x X */
317 (op1mode == mode_b ||
318 /* Cond: BB x int --> X^n */
319 mode_is_int(op1mode) ), "Cond node", 0
321 ASSERT_AND_RET(mymode == mode_T, "Cond mode is not a tuple", 0);
325 op1mode = get_irn_mode(in[1]);
326 /* Return: BB x M x data1 x ... x datan --> X */
327 /* printf("mode: %s, code %s\n", ID_TO_STR(n->mode->name), ID_TO_STR(n->op->name));*/
328 ASSERT_AND_RET( op1mode == mode_M, "Return node", 0 ); /* operand M */
329 for (i=2; i < get_irn_arity(n); i++) {
330 ASSERT_AND_RET( mode_is_data(get_irn_mode(in[i])), "Return node", 0 ); /* operand datai */
332 ASSERT_AND_RET( mymode == mode_X, "Result X", 0 ); /* result X */
333 /* Compare returned results with result types of method type */
334 mt = get_entity_type(get_irg_ent(irg));
335 ASSERT_AND_RET( get_Return_n_ress(n) == get_method_n_ress(mt),
336 "Number of results for Return doesn't match number of results in type.", 0 );
337 for (i = 0; i < get_Return_n_ress(n); i++)
339 get_irn_mode(get_Return_res(n, i)) == get_type_mode(get_method_res_type(mt, i)),
340 "Mode of result for Return doesn't match mode of result type.", 0);
344 op1mode = get_irn_mode(in[1]);
345 op2mode = get_irn_mode(in[2]);
347 /* Sel: BB x M x ref --> X x M */
348 op1mode == mode_M && mode_is_reference(op2mode) &&
349 mymode == mode_T, "Raise node", 0
355 /* Const: BB --> data */
356 (mode_is_data (mymode) ||
357 mymode == mode_b) /* we want boolean constants for static evaluation */
358 ,"Const node", 0 /* of Cmp. */
364 /* SymConst: BB --> int*/
365 (mode_is_int(mymode) ||
366 /* SymConst: BB --> ref */
367 mode_is_reference(mymode))
368 ,"SymConst node", 0);
372 op1mode = get_irn_mode(in[1]);
373 op2mode = get_irn_mode(in[2]);
375 /* Sel: BB x M x ref x int^n --> ref */
376 (op1mode == mode_M && op2mode == mymode && mode_is_reference(mymode)),
379 for (i=3; i < get_irn_arity(n); i++)
381 ASSERT_AND_RET(mode_is_int(get_irn_mode(in[i])), "Sel node", 0);
386 ASSERT_AND_RET(mode_T == mymode, "mode of Instof is not a tuple", 0);
387 ASSERT_AND_RET(mode_is_data(op1mode), "Instof not on data", 0);
391 op1mode = get_irn_mode(in[1]);
392 op2mode = get_irn_mode(in[2]);
393 /* Call: BB x M x ref x data1 x ... x datan
394 --> M x datan+1 x ... x data n+m */
395 ASSERT_AND_RET( op1mode == mode_M && mode_is_reference(op2mode), "Call node", 0 ); /* operand M x ref */
396 for (i=3; i < get_irn_arity(n); i++) {
397 ASSERT_AND_RET( mode_is_data(get_irn_mode(in[i])), "Call node", 0 ); /* operand datai */
399 ASSERT_AND_RET( mymode == mode_T, "Call result not a tuple", 0 ); /* result T */
400 /* Compare arguments of node with those of type */
401 mt = get_Call_type(n);
403 if (get_method_variadicity(mt) == variadicity_variadic) {
405 get_Call_n_params(n) >= get_method_n_params(mt),
406 "Number of args for Call doesn't match number of args in variadic type.",
411 get_Call_n_params(n) == get_method_n_params(mt),
412 "Number of args for Call doesn't match number of args in non variadic type.",
416 for (i = 0; i < get_method_n_params(mt); i++) {
418 get_irn_mode(get_Call_param(n, i)) == get_type_mode(get_method_param_type(mt, i)),
419 "Mode of arg for Call doesn't match mode of arg type.", 0,
423 fprintf(stderr, "Assertion for Call type-check failed: %s(", get_type_name(mt));
424 for (i = 0; i < get_method_n_params(mt); ++i) {
425 fprintf(stderr, "%s ", get_mode_name(get_type_mode(get_method_param_type(mt, i))));
427 fprintf(stderr, ") != CALL(");
429 for (i = 0; i < get_Call_n_params(n); ++i) {
430 fprintf(stderr, "%s ", get_mode_name(get_irn_mode(get_Call_param(n, i))));
432 fprintf(stderr, ")\n");
440 op1mode = get_irn_mode(in[1]);
441 op2mode = get_irn_mode(in[2]);
444 /* common Add: BB x numP x numP --> numP */
445 (op1mode == mymode && op2mode == op1mode && mode_is_numP(mymode)) ||
446 /* Pointer Add: BB x ref x int --> ref */
447 (mode_is_reference(op1mode) && mode_is_int(op2mode) && op1mode == mymode) ||
448 /* Pointer Add: BB x int x ref --> ref */
449 (mode_is_int(op1mode) && op2mode == mymode && mode_is_reference(mymode))
453 if (mode_is_reference(op1mode) != mode_is_reference(op2mode)) {
454 /* BB x ref x int --> ref or BB x int x ref --> ref */
457 /* BB x num x num --> num or BB x ref x ref */
463 op1mode = get_irn_mode(in[1]);
464 op2mode = get_irn_mode(in[2]);
466 /* common Sub: BB x numP x numP --> numP */
467 ((mymode ==op1mode && mymode == op2mode && mode_is_numP(op1mode)) ||
468 /* Pointer Sub: BB x ref x int --> ref */
469 (op1mode == mymode && mode_is_int(op2mode) && mode_is_reference(mymode)) ||
470 /* Pointer Sub: BB x int x ref --> ref */
471 (mode_is_int(op1mode) && op2mode == mymode && mode_is_reference(mymode)) ||
472 /* Pointer Sub: BB x ref x ref --> int */
473 (op1mode == op2mode && mode_is_reference(op2mode) && mode_is_int(mymode))),
476 if (mode_is_reference(op1mode) != mode_is_reference(op2mode)) {
484 op1mode = get_irn_mode(in[1]);
486 /* Minus: BB x float --> float */
487 op1mode == mymode && get_mode_sort(op1mode) == irms_float_number, "Minus node", 0
493 op1mode = get_irn_mode(in[1]);
494 op2mode = get_irn_mode(in[2]);
496 /* Mul: BB x int1 x int1 --> int2 */
497 ((mode_is_int(op1mode) && op2mode == op1mode && mode_is_int(mymode)) ||
498 (mode_is_float(op1mode) && op2mode == op1mode && mymode == op1mode)),
505 op1mode = get_irn_mode(in[1]);
506 op2mode = get_irn_mode(in[2]);
507 op3mode = get_irn_mode(in[3]);
509 /* Quot: BB x M x float x float --> M x X x float */
510 op1mode == mode_M && op2mode == op3mode &&
511 get_mode_sort(op2mode) == irms_float_number &&
519 op1mode = get_irn_mode(in[1]);
520 op2mode = get_irn_mode(in[2]);
521 op3mode = get_irn_mode(in[3]);
523 /* DivMod: BB x M x int x int --> M x X x int x int */
525 mode_is_int(op2mode) &&
526 op3mode == op2mode &&
535 op1mode = get_irn_mode(in[1]);
536 op2mode = get_irn_mode(in[2]);
537 op3mode = get_irn_mode(in[3]);
539 /* Div or Mod: BB x M x int x int --> M x X x int */
541 op2mode == op3mode &&
542 mode_is_int(op2mode) &&
550 op1mode = get_irn_mode(in[1]);
552 /* Abs: BB x num --> num */
554 mode_is_num (op1mode),
563 op1mode = get_irn_mode(in[1]);
564 op2mode = get_irn_mode(in[2]);
566 /* And or Or or Eor: BB x int x int --> int */
567 mode_is_int(mymode) &&
568 op2mode == op1mode &&
570 "And, Or or Eor node", 0
576 op1mode = get_irn_mode(in[1]);
578 /* Not: BB x int --> int */
579 mode_is_int(mymode) &&
588 op1mode = get_irn_mode(in[1]);
589 op2mode = get_irn_mode(in[2]);
591 /* Cmp: BB x datab x datab --> b16 */
592 mode_is_data (op1mode) &&
593 op2mode == op1mode &&
602 op1mode = get_irn_mode(in[1]);
603 op2mode = get_irn_mode(in[2]);
605 /* Shl, Shr or Shrs: BB x int x int_u --> int */
606 mode_is_int(op1mode) &&
607 mode_is_int(op2mode) &&
608 !mode_is_signed(op2mode) &&
610 "Shl, Shr, Shr or Rot node"
615 op1mode = get_irn_mode(in[1]);
616 op2mode = get_irn_mode(in[2]);
618 /* Rot: BB x int x int --> int */
619 mode_is_int(op1mode) &&
620 mode_is_int(op2mode) &&
627 op1mode = get_irn_mode(in[1]);
629 /* Conv: BB x datab1 --> datab2 */
630 mode_is_datab(op1mode) && mode_is_data(mymode),
636 op1mode = get_irn_mode(in[1]);
638 /* Conv: BB x datab1 --> datab2 */
639 mode_is_data(op1mode) && op1mode == mymode,
645 /* Phi: BB x dataM^n --> dataM */
646 /* for some reason "<=" aborts. int there a problem with get_store? */
647 for (i=1; i < get_irn_arity(n); i++) {
648 if (!is_Bad(in[i]) && (get_irn_op(in[i]) != op_Unknown))
649 ASSERT_AND_RET( get_irn_mode(in[i]) == mymode, "Phi node", 0);
651 ASSERT_AND_RET( mode_is_dataM(mymode), "Phi node", 0 );
655 op1mode = get_irn_mode(in[1]);
656 op2mode = get_irn_mode(in[2]);
658 /* Load: BB x M x ref --> M x X x data */
659 op1mode == mode_M && mode_is_reference(op2mode),
662 ASSERT_AND_RET( mymode == mode_T, "Load node", 0 );
666 op1mode = get_irn_mode(in[1]);
667 op2mode = get_irn_mode(in[2]);
668 op3mode = get_irn_mode(in[3]);
670 /* Load: BB x M x ref data --> M x X */
671 op1mode == mode_M && mode_is_reference(op2mode) && mode_is_data(op3mode),
674 ASSERT_AND_RET(mymode == mode_T, "Store node", 0);
678 op1mode = get_irn_mode(in[1]);
679 op2mode = get_irn_mode(in[2]);
681 /* Alloc: BB x M x int_u --> M x X x ref */
683 mode_is_int(op2mode) &&
684 !mode_is_signed(op2mode) &&
691 op1mode = get_irn_mode(in[1]);
692 op2mode = get_irn_mode(in[2]);
694 /* Free: BB x M x ref --> M */
695 op1mode == mode_M && mode_is_reference(op2mode) &&
702 /* Sync: BB x M^n --> M */
703 for (i=1; i < get_irn_arity(n); i++) {
704 ASSERT_AND_RET( get_irn_mode(in[i]) == mode_M, "Sync node", 0 );
706 ASSERT_AND_RET( mymode == mode_M, "Sync node", 0 );
710 return vrfy_Proj_proj(n, irg);
714 op1mode = get_irn_mode(in[1]);
715 op2mode = get_irn_mode(in[2]);
717 /* Confirm: BB x T x T --> T */
733 int irn_vrfy(ir_node *n)
735 return irn_vrfy_irg(n, current_ir_graph);
738 /*******************************************************************/
739 /* Verify the whole graph. */
740 /*******************************************************************/
742 static void vrfy_wrap(ir_node *node, void *env)
746 *res = irn_vrfy(node);
749 int irg_vrfy(ir_graph *irg)
754 rem = current_ir_graph;
755 current_ir_graph = irg;
757 assert(get_irg_pinned(irg) == pinned);
759 irg_walk(irg->end, vrfy_wrap, NULL, &res);
761 current_ir_graph = rem;