2 * Copyright (C) 1995-2011 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Check irnodes for correctness.
23 * @author Christian Schaefer, Goetz Lindenmaier, Till Riedel, Michael Beck
29 #include "irgraph_t.h"
30 #include "irverify_t.h"
38 #include "irnodeset.h"
40 /** if this flag is set, verify entity types in Load & Store nodes */
41 static int verify_entities = 0;
43 const char *firm_verify_failure_msg;
45 /* enable verification of Load/Store entities */
46 void verify_enable_entity_tests(int enable)
48 verify_entities = enable;
54 * little helper for NULL modes
56 static const char *get_mode_name_ex(ir_mode *mode)
60 return get_mode_name(mode);
63 /** the last IRG, on which a verification error was found */
64 static ir_graph *last_irg_error = NULL;
67 * print the name of the entity of an verification failure
69 * @param node the node caused the failure
71 static void show_entity_failure(const ir_node *node)
73 ir_graph *irg = get_irn_irg(node);
75 if (last_irg_error == irg)
80 if (irg == get_const_code_irg()) {
81 fprintf(stderr, "\nFIRM: irn_verify_irg() <of CONST_CODE_IRG> failed\n");
83 ir_entity *ent = get_irg_entity(irg);
86 ir_type *ent_type = get_entity_owner(ent);
89 ir_fprintf(stderr, "\nFIRM: irn_verify_irg() %+F::%s failed\n",
90 ent_type, get_entity_name(ent));
92 fprintf(stderr, "\nFIRM: irn_verify_irg() <NULL>::%s failed\n", get_entity_name(ent));
95 fprintf(stderr, "\nFIRM: irn_verify_irg() <IRG %p> failed\n", (void *)irg);
100 static const char *get_irn_modename(const ir_node *node)
102 ir_mode *mode = get_irn_mode(node);
103 return get_mode_name(mode);
107 * Prints a failure for a Node
109 static void show_node_failure(const ir_node *n)
111 show_entity_failure(n);
112 fprintf(stderr, " node %ld %s%s\n" ,
114 get_irn_opname(n), get_irn_modename(n)
119 * Prints a failure message for a binop
121 static void show_binop_failure(const ir_node *n, const char *text)
123 ir_node *left = get_binop_left(n);
124 ir_node *right = get_binop_right(n);
126 show_entity_failure(n);
127 fprintf(stderr, " node %ld %s%s(%s%s, %s%s) did not match (%s)\n",
129 get_irn_opname(n), get_irn_modename(n),
130 get_irn_opname(left), get_irn_modename(left),
131 get_irn_opname(right), get_irn_modename(right),
136 * Prints a failure message for an unop
138 static void show_unop_failure(const ir_node *n, const char *text)
140 ir_node *op = get_unop_op(n);
142 show_entity_failure(n);
143 fprintf(stderr, " node %ld %s%s(%s%s) did not match (%s)\n",
145 get_irn_opname(n), get_irn_modename(n),
146 get_irn_opname(op), get_irn_modename(op),
151 * Prints a failure message for an op with 3 operands
153 static void show_triop_failure(const ir_node *n, const char *text)
155 ir_node *op0 = get_irn_n(n, 0);
156 ir_node *op1 = get_irn_n(n, 1);
157 ir_node *op2 = get_irn_n(n, 2);
159 show_entity_failure(n);
160 fprintf(stderr, " of node %ld %s%s(%s%s, %s%s, %s%s) did not match (%s)\n",
162 get_irn_opname(n), get_irn_modename(n),
163 get_irn_opname(op0), get_irn_modename(op0),
164 get_irn_opname(op1), get_irn_modename(op1),
165 get_irn_opname(op2), get_irn_modename(op2),
170 * Prints a failure message for a proj
172 static void show_proj_failure(const ir_node *n)
174 ir_node *op = get_Proj_pred(n);
175 int proj = get_Proj_proj(n);
177 show_entity_failure(n);
178 fprintf(stderr, " node %ld %s%s %d(%s%s) failed\n" ,
180 get_irn_opname(n), get_irn_modename(n), proj,
181 get_irn_opname(op), get_irn_modename(op));
185 * Prints a failure message for a proj from Start
187 static void show_proj_mode_failure(const ir_node *n, ir_type *ty)
189 long proj = get_Proj_proj(n);
190 ir_mode *m = get_type_mode(ty);
192 ir_print_type(type_name, sizeof(type_name), ty);
194 show_entity_failure(n);
195 fprintf(stderr, " Proj %ld mode %s proj %ld (type %s mode %s) failed\n" ,
200 get_mode_name_ex(m));
204 * Prints a failure message for a proj
206 static void show_proj_failure_ent(const ir_node *n, ir_entity *ent)
208 ir_node *op = get_Proj_pred(n);
209 int proj = get_Proj_proj(n);
210 ir_mode *m = get_type_mode(get_entity_type(ent));
212 ir_print_type(type_name, sizeof(type_name), get_entity_type(ent));
214 show_entity_failure(n);
215 fprintf(stderr, " node %ld %s%s %d(%s%s) entity %s(type %s mode %s)failed\n" ,
217 get_irn_opname(n), get_irn_modename(n), proj,
218 get_irn_opname(op), get_irn_modename(op),
219 get_entity_name(ent), type_name,
220 get_mode_name_ex(m));
224 * Show a node and a graph
226 static void show_node_on_graph(const ir_graph *irg, const ir_node *n)
228 ir_fprintf(stderr, "\nFIRM: irn_verify_irg() of %+F, node %+F\n", irg, n);
232 * Show call parameters
234 static void show_call_param(const ir_node *n, ir_type *mt)
238 ir_print_type(type_name, sizeof(type_name), mt);
240 show_entity_failure(n);
241 fprintf(stderr, " Call type-check failed: %s(", type_name);
242 for (i = 0; i < get_method_n_params(mt); ++i) {
243 fprintf(stderr, "%s ", get_mode_name_ex(get_type_mode(get_method_param_type(mt, i))));
245 fprintf(stderr, ") != CALL(");
247 for (i = 0; i < get_Call_n_params(n); ++i) {
248 fprintf(stderr, "%s ", get_mode_name_ex(get_irn_mode(get_Call_param(n, i))));
250 fprintf(stderr, ")\n");
256 static void show_return_modes(const ir_graph *irg, const ir_node *n,
259 ir_entity *ent = get_irg_entity(irg);
261 show_entity_failure(n);
262 fprintf(stderr, " Return node %ld in entity \"%s\" mode %s different from type mode %s\n",
263 get_irn_node_nr(n), get_entity_name(ent),
264 get_mode_name_ex(get_irn_mode(get_Return_res(n, i))),
265 get_mode_name_ex(get_type_mode(get_method_res_type(mt, i)))
270 * Show return number of results
272 static void show_return_nres(const ir_graph *irg, const ir_node *n, ir_type *mt)
274 ir_entity *ent = get_irg_entity(irg);
276 show_entity_failure(n);
277 fprintf(stderr, " Return node %ld in entity \"%s\" has %lu results different from type %lu\n",
278 get_irn_node_nr(n), get_entity_name(ent),
279 (unsigned long) get_Return_n_ress(n),
280 (unsigned long) get_method_n_ress(mt));
286 static void show_phi_failure(const ir_node *phi, const ir_node *pred, int pos)
289 show_entity_failure(phi);
290 fprintf(stderr, " Phi node %ld has mode %s different from predeccessor node %ld mode %s\n",
291 get_irn_node_nr(phi), get_mode_name_ex(get_irn_mode(phi)),
292 get_irn_node_nr(pred), get_mode_name_ex(get_irn_mode(pred)));
298 static void show_phi_inputs(const ir_node *phi, const ir_node *block)
300 show_entity_failure(phi);
301 fprintf(stderr, " Phi node %ld has %d inputs, its Block %ld has %d\n",
302 get_irn_node_nr(phi), get_irn_arity(phi),
303 get_irn_node_nr(block), get_irn_arity(block));
306 #endif /* #ifndef NDEBUG */
309 * If the address is Sel or SymConst, return the entity.
311 * @param ptr the node representing the address
313 static ir_entity *get_ptr_entity(const ir_node *ptr)
316 return get_Sel_entity(ptr);
317 } else if (is_SymConst_addr_ent(ptr)) {
318 return get_SymConst_entity(ptr);
324 * verify a Proj(Start) node
326 static int verify_node_Proj_Start(const ir_node *p)
328 ir_mode *mode = get_irn_mode(p);
329 long proj = get_Proj_proj(p);
333 (proj == pn_Start_X_initial_exec && mode == mode_X) ||
334 (proj == pn_Start_M && mode == mode_M) ||
335 (proj == pn_Start_P_frame_base && mode_is_reference(mode)) ||
336 (proj == pn_Start_T_args && mode == mode_T)
338 "wrong Proj from Start", 0,
339 show_proj_failure(p);
345 * verify a Proj(Cond) node
347 static int verify_node_Proj_Cond(const ir_node *p)
349 ir_mode *mode = get_irn_mode(p);
350 long proj = get_Proj_proj(p);
353 mode == mode_X && (proj == pn_Cond_false || proj == pn_Cond_true),
354 "wrong Proj from Cond", 0,
355 show_proj_failure(p);
360 static int verify_node_Proj_Switch(const ir_node *p)
362 ir_mode *mode = get_irn_mode(p);
363 long pn = get_Proj_proj(p);
364 ir_node *pred = get_Proj_pred(p);
366 mode == mode_X && (pn >= 0 && pn < (long)get_Switch_n_outs(pred)),
367 "wrong Proj from Switch", 0,
368 show_proj_failure(p);
374 * verify a Proj(Raise) node
376 static int verify_node_Proj_Raise(const ir_node *p)
378 ir_mode *mode = get_irn_mode(p);
379 long proj = get_Proj_proj(p);
382 ((proj == pn_Raise_X && mode == mode_X) || (proj == pn_Raise_M && mode == mode_M)),
383 "wrong Proj from Raise", 0,
384 show_proj_failure(p);
390 * verify a Proj(InstOf) node
392 static int verify_node_Proj_InstOf(const ir_node *p)
394 ir_mode *mode = get_irn_mode(p);
395 long proj = get_Proj_proj(p);
399 (proj == pn_InstOf_M && mode == mode_M) ||
400 (proj == pn_InstOf_X_regular && mode == mode_X) ||
401 (proj == pn_InstOf_X_except && mode == mode_X) ||
402 (proj == pn_InstOf_res && mode_is_reference(mode))
404 "wrong Proj from InstOf", 0,
405 show_proj_failure(p);
411 * verify a Proj(Call) node
413 static int verify_node_Proj_Call(const ir_node *p)
415 ir_mode *mode = get_irn_mode(p);
416 ir_node *n = get_Proj_pred(p);
417 long proj = get_Proj_proj(p);
421 (proj == pn_Call_M && mode == mode_M) ||
422 (proj == pn_Call_X_regular && mode == mode_X) ||
423 (proj == pn_Call_X_except && mode == mode_X) ||
424 (proj == pn_Call_T_result && mode == mode_T)
426 "wrong Proj from Call", 0,
427 show_proj_failure(p);
429 /* if we have exception flow, we must have a real Memory input */
430 if (proj == pn_Call_X_regular)
432 !is_NoMem(get_Call_mem(n)),
433 "Regular Proj from FunctionCall", 0);
434 else if (proj == pn_Call_X_except)
436 !is_NoMem(get_Call_mem(n)),
437 "Exception Proj from FunctionCall", 0);
442 * verify a Proj(Div) node
444 static int verify_node_Proj_Div(const ir_node *p)
446 ir_mode *mode = get_irn_mode(p);
447 ir_node *n = get_Proj_pred(p);
448 long proj = get_Proj_proj(p);
452 (proj == pn_Div_M && mode == mode_M) ||
453 (proj == pn_Div_X_regular && mode == mode_X) ||
454 (proj == pn_Div_X_except && mode == mode_X) ||
455 (proj == pn_Div_res && mode_is_data(mode) && mode == get_Div_resmode(n))
457 "wrong Proj from Div", 0,
458 show_proj_failure(p);
460 if (proj == pn_Div_X_regular)
462 get_irn_pinned(n) == op_pin_state_pinned,
463 "Regular Proj from unpinned Div", 0);
464 else if (proj == pn_Div_X_except)
466 get_irn_pinned(n) == op_pin_state_pinned,
467 "Exception Proj from unpinned Div", 0);
468 else if (proj == pn_Div_M)
470 get_irn_pinned(n) == op_pin_state_pinned,
471 "Memory Proj from unpinned Div", 0);
476 * verify a Proj(Mod) node
478 static int verify_node_Proj_Mod(const ir_node *p)
480 ir_mode *mode = get_irn_mode(p);
481 ir_node *n = get_Proj_pred(p);
482 long proj = get_Proj_proj(p);
486 (proj == pn_Mod_M && mode == mode_M) ||
487 (proj == pn_Mod_X_regular && mode == mode_X) ||
488 (proj == pn_Mod_X_except && mode == mode_X) ||
489 (proj == pn_Mod_res && mode == get_Mod_resmode(n))
491 "wrong Proj from Mod", 0,
492 show_proj_failure(p);
494 if (proj == pn_Mod_X_regular)
496 get_irn_pinned(n) == op_pin_state_pinned,
497 "Regular Proj from unpinned Mod", 0);
498 else if (proj == pn_Mod_X_except)
500 get_irn_pinned(n) == op_pin_state_pinned,
501 "Exception Proj from unpinned Mod", 0);
502 else if (proj == pn_Mod_M)
504 get_irn_pinned(n) == op_pin_state_pinned,
505 "Memory Proj from unpinned Div", 0);
510 * verify a Proj(Load) node
512 static int verify_node_Proj_Load(const ir_node *p)
514 ir_mode *mode = get_irn_mode(p);
515 ir_node *n = get_Proj_pred(p);
516 long proj = get_Proj_proj(p);
518 if (proj == pn_Load_res) {
519 ir_node *ptr = get_Load_ptr(n);
520 ir_entity *ent = get_ptr_entity(ptr);
521 ir_graph *irg = get_irn_irg(n);
523 if (verify_entities && ent && get_irg_phase_state(irg) == phase_high) {
524 /* do NOT check this for lowered phases, see comment on Store */
526 (mode == get_type_mode(get_entity_type(ent))),
527 "wrong data Proj from Load, entity type_mode failed", 0,
528 show_proj_failure_ent(p, ent);
533 mode_is_data(mode) && mode == get_Load_mode(n),
534 "wrong data Proj from Load", 0,
535 show_proj_failure(p);
542 (proj == pn_Load_M && mode == mode_M) ||
543 (proj == pn_Load_X_regular && mode == mode_X) ||
544 (proj == pn_Load_X_except && mode == mode_X)
546 "wrong Proj from Load", 0,
547 show_proj_failure(p);
550 if (proj == pn_Load_X_regular) {
552 get_irn_pinned(n) == op_pin_state_pinned,
553 "Regular Proj from unpinned Load", 0);
554 } else if (proj == pn_Load_X_except) {
556 get_irn_pinned(n) == op_pin_state_pinned,
557 "Exception Proj from unpinned Load", 0);
563 * verify a Proj(Store) node
565 static int verify_node_Proj_Store(const ir_node *p)
567 ir_mode *mode = get_irn_mode(p);
568 ir_node *n = get_Proj_pred(p);
569 long proj = get_Proj_proj(p);
573 (proj == pn_Store_M && mode == mode_M) ||
574 (proj == pn_Store_X_regular && mode == mode_X) ||
575 (proj == pn_Store_X_except && mode == mode_X)
577 "wrong Proj from Store", 0,
578 show_proj_failure(p);
580 if (proj == pn_Store_X_regular) {
582 get_irn_pinned(n) == op_pin_state_pinned,
583 "Regular Proj from unpinned Store", 0);
584 } else if (proj == pn_Store_X_except) {
586 get_irn_pinned(n) == op_pin_state_pinned,
587 "Exception Proj from unpinned Store", 0);
593 * verify a Proj(Alloc) node
595 static int verify_node_Proj_Alloc(const ir_node *p)
597 ir_mode *mode = get_irn_mode(p);
598 long proj = get_Proj_proj(p);
602 (proj == pn_Alloc_M && mode == mode_M) ||
603 (proj == pn_Alloc_X_regular && mode == mode_X) ||
604 (proj == pn_Alloc_X_except && mode == mode_X) ||
605 (proj == pn_Alloc_res && mode_is_reference(mode))
607 "wrong Proj from Alloc", 0,
608 show_proj_failure(p);
614 * verify a Proj(Proj) node
616 static int verify_node_Proj_Proj(const ir_node *p)
618 ir_mode *mode = get_irn_mode(p);
619 ir_node *pred = get_Proj_pred(p);
620 long proj = get_Proj_proj(p);
621 long nr = get_Proj_proj(pred);
622 ir_type *mt; /* A method type */
624 pred = skip_Id(get_Proj_pred(pred));
625 ASSERT_AND_RET((get_irn_mode(pred) == mode_T), "Proj from something not a tuple", 0);
627 switch (get_irn_opcode(pred)) {
629 mt = get_entity_type(get_irg_entity(get_irn_irg(pred)));
631 if (nr == pn_Start_T_args) {
633 (proj >= 0 && mode_is_datab(mode)),
634 "wrong Proj from Proj from Start", 0);
636 (proj < (int)get_method_n_params(mt)),
637 "More Projs for args than args in type", 0
639 if ((mode_is_reference(mode)) && is_compound_type(get_method_param_type(mt, proj)))
640 /* value argument */ break;
642 if (get_irg_phase_state(get_irn_irg(pred)) != phase_backend) {
644 (mode == get_type_mode(get_method_param_type(mt, proj))),
645 "Mode of Proj from Start doesn't match mode of param type.", 0,
646 show_proj_mode_failure(p, get_method_param_type(mt, proj));
655 (proj >= 0 && mode_is_datab(mode)),
656 "wrong Proj from Proj from Call", 0);
657 mt = get_Call_type(pred);
658 ASSERT_AND_RET(mt == get_unknown_type() || is_Method_type(mt),
659 "wrong call type on call", 0);
661 (proj < (int)get_method_n_ress(mt)),
662 "More Projs for results than results in type.", 0);
663 if ((mode_is_reference(mode)) && is_compound_type(get_method_res_type(mt, proj)))
664 /* value result */ break;
667 (mode == get_type_mode(get_method_res_type(mt, proj))),
668 "Mode of Proj from Call doesn't match mode of result type.", 0);
677 /* ASSERT_AND_RET(0, "Unknown opcode", 0); */
684 * verify a Proj(Tuple) node
686 static int verify_node_Proj_Tuple(const ir_node *p)
694 * verify a Proj(CopyB) node
696 static int verify_node_Proj_CopyB(const ir_node *p)
698 ir_mode *mode = get_irn_mode(p);
699 ir_node *n = get_Proj_pred(p);
700 long proj = get_Proj_proj(p);
704 (proj == pn_CopyB_M && mode == mode_M) ||
705 (proj == pn_CopyB_X_regular && mode == mode_X) ||
706 (proj == pn_CopyB_X_except && mode == mode_X)
708 "wrong Proj from CopyB", 0,
709 show_proj_failure(p);
711 if (proj == pn_CopyB_X_regular)
713 get_irn_pinned(n) == op_pin_state_pinned,
714 "Regular Proj from unpinned CopyB", 0);
715 else if (proj == pn_CopyB_X_except)
717 get_irn_pinned(n) == op_pin_state_pinned,
718 "Exception Proj from unpinned CopyB", 0);
723 * verify a Proj(Bound) node
725 static int verify_node_Proj_Bound(const ir_node *p)
727 ir_mode *mode = get_irn_mode(p);
728 ir_node *n = get_Proj_pred(p);
729 long proj = get_Proj_proj(p);
733 (proj == pn_Bound_M && mode == mode_M) ||
734 (proj == pn_Bound_X_regular && mode == mode_X) ||
735 (proj == pn_Bound_X_except && mode == mode_X) ||
736 (proj == pn_Bound_res && mode == get_irn_mode(get_Bound_index(n)))
738 "wrong Proj from Bound", 0,
739 show_proj_failure(p);
744 static int verify_node_Proj_fragile(const ir_node *node)
746 ir_node *pred = get_Proj_pred(node);
747 int throws_exception = ir_throws_exception(pred);
748 ASSERT_AND_RET((!is_x_except_Proj(node) || throws_exception)
749 && (!is_x_regular_Proj(node) || throws_exception),
750 "X_except und X_regular Proj only allowed when throws_exception is set",
758 static int verify_node_Proj(const ir_node *p)
760 ir_graph *irg = get_irn_irg(p);
764 pred = skip_Id(get_Proj_pred(p));
765 ASSERT_AND_RET(get_irn_mode(pred) == mode_T, "mode of a 'projed' node is not Tuple", 0);
766 ASSERT_AND_RET(get_irg_pinned(irg) == op_pin_state_floats || get_nodes_block(pred) == get_nodes_block(p), "Proj must be in same block as its predecessor", 0);
768 if (is_fragile_op(pred)) {
769 int res = verify_node_Proj_fragile(p);
774 op = get_irn_op(pred);
775 if (op->ops.verify_proj_node)
776 return op->ops.verify_proj_node(p);
783 * verify a Block node
785 static int verify_node_Block(const ir_node *n)
787 ir_graph *irg = get_irn_irg(n);
790 for (i = get_Block_n_cfgpreds(n) - 1; i >= 0; --i) {
791 ir_node *pred = get_Block_cfgpred(n, i);
792 ir_node *skipped_pred = skip_Proj(skip_Tuple(pred));
793 ASSERT_AND_RET(get_irn_mode(pred) == mode_X,
794 "Block node must have a mode_X predecessor", 0);
795 ASSERT_AND_RET(is_cfop(skipped_pred) || is_Bad(skipped_pred), "Block predecessor must be a cfop (or Bad)", 0);
798 if (n == get_irg_start_block(irg)) {
799 ASSERT_AND_RET(get_Block_n_cfgpreds(n) == 0, "Start Block node", 0);
802 if (n == get_irg_end_block(irg) && get_irg_phase_state(irg) != phase_backend) {
803 /* End block may only have Return, Raise or fragile ops as preds. */
804 for (i = get_Block_n_cfgpreds(n) - 1; i >= 0; --i) {
805 ir_node *pred = skip_Proj(get_Block_cfgpred(n, i));
806 if (is_Proj(pred) || is_Tuple(pred))
807 break; /* We can not test properly. How many tuples are there? */
815 "End Block node", 0);
818 /* irg attr must == graph we are in. */
819 ASSERT_AND_RET(((get_irn_irg(n) && get_irn_irg(n) == irg)), "Block node has wrong irg attribute", 0);
824 * verify a Start node
826 static int verify_node_Start(const ir_node *n)
828 ir_mode *mymode = get_irn_mode(n);
831 /* Start: BB --> X x M x ref x data1 x ... x datan x ref */
832 mymode == mode_T, "Start node", 0
840 static int verify_node_Jmp(const ir_node *n)
842 ir_mode *mymode = get_irn_mode(n);
846 mymode == mode_X, "Jmp node", 0
852 * verify an IJmp node
854 static int verify_node_IJmp(const ir_node *n)
856 ir_mode *mymode = get_irn_mode(n);
857 ir_mode *op1mode = get_irn_mode(get_IJmp_target(n));
860 /* IJmp: BB x ref --> X */
861 mymode == mode_X && mode_is_reference(op1mode), "IJmp node", 0
869 static int verify_node_Cond(const ir_node *n)
871 ir_mode *mymode = get_irn_mode(n);
872 ir_mode *op1mode = get_irn_mode(get_Cond_selector(n));
874 ASSERT_AND_RET(op1mode == mode_b, "Cond operand not mode_b", 0);
875 ASSERT_AND_RET(mymode == mode_T, "Cond mode is not a tuple", 0);
879 static int verify_switch_table(const ir_node *n)
881 const ir_switch_table *table = get_Switch_table(n);
882 size_t n_entries = ir_switch_table_get_n_entries(table);
883 unsigned n_outs = get_Switch_n_outs(n);
884 ir_node *selector = get_Switch_selector(n);
885 ir_mode *mode = get_irn_mode(selector);
888 for (e = 0; e < n_entries; ++e) {
889 const ir_switch_table_entry *entry
890 = ir_switch_table_get_entry_const(table, e);
893 ASSERT_AND_RET(entry->min != NULL && entry->max != NULL,
894 "switch table entry without min+max value", 0);
895 ASSERT_AND_RET(get_tarval_mode(entry->min) == mode &&
896 get_tarval_mode(entry->max) == mode,
897 "switch table entry with wrong modes", 0);
898 ASSERT_AND_RET(tarval_cmp(entry->min, entry->max) != ir_relation_greater,
899 "switch table entry without min+max value", 0);
900 ASSERT_AND_RET(entry->pn >= 0 && entry->pn < (long)n_outs,
901 "switch table entry with invalid proj number", 0);
906 static int verify_node_Switch(const ir_node *n)
908 ir_mode *mymode = get_irn_mode(n);
909 ir_mode *op1mode = get_irn_mode(get_Switch_selector(n));
910 if (!verify_switch_table(n))
913 ASSERT_AND_RET(mode_is_int(op1mode), "Switch operand not integer", 0);
914 ASSERT_AND_RET(mymode == mode_T, "Switch mode is not a tuple", 0);
919 * verify a Return node
921 static int verify_node_Return(const ir_node *n)
923 ir_graph *irg = get_irn_irg(n);
924 ir_mode *mymode = get_irn_mode(n);
925 ir_mode *mem_mode = get_irn_mode(get_Return_mem(n));
929 /* Return: BB x M x data1 x ... x datan --> X */
931 ASSERT_AND_RET( mem_mode == mode_M, "Return node", 0 ); /* operand M */
933 for (i = get_Return_n_ress(n) - 1; i >= 0; --i) {
934 ASSERT_AND_RET( mode_is_datab(get_irn_mode(get_Return_res(n, i))), "Return node", 0 ); /* operand datai */
936 ASSERT_AND_RET( mymode == mode_X, "Result X", 0 ); /* result X */
937 /* Compare returned results with result types of method type */
938 mt = get_entity_type(get_irg_entity(irg));
939 ASSERT_AND_RET_DBG(get_Return_n_ress(n) == get_method_n_ress(mt),
940 "Number of results for Return doesn't match number of results in type.", 0,
941 show_return_nres(irg, n, mt););
942 for (i = get_Return_n_ress(n) - 1; i >= 0; --i) {
943 ir_type *res_type = get_method_res_type(mt, i);
945 if (get_irg_phase_state(irg) != phase_backend) {
946 if (is_atomic_type(res_type)) {
948 get_irn_mode(get_Return_res(n, i)) == get_type_mode(res_type),
949 "Mode of result for Return doesn't match mode of result type.", 0,
950 show_return_modes(irg, n, mt, i);
954 mode_is_reference(get_irn_mode(get_Return_res(n, i))),
955 "Mode of result for Return doesn't match mode of result type.", 0,
956 show_return_modes(irg, n, mt, i);
965 * verify a Raise node
967 static int verify_node_Raise(const ir_node *n)
969 ir_mode *mymode = get_irn_mode(n);
970 ir_mode *op1mode = get_irn_mode(get_Raise_mem(n));
971 ir_mode *op2mode = get_irn_mode(get_Raise_exo_ptr(n));
974 /* Sel: BB x M x ref --> X x M */
975 op1mode == mode_M && mode_is_reference(op2mode) &&
976 mymode == mode_T, "Raise node", 0
982 * verify a Const node
984 static int verify_node_Const(const ir_node *n)
986 ir_mode *mymode = get_irn_mode(n);
989 /* Const: BB --> data */
990 (mode_is_data(mymode) ||
991 mymode == mode_b) /* we want boolean constants for static evaluation */
992 ,"Const node", 0 /* of Cmp. */
995 /* the modes of the constant and teh tarval must match */
996 mymode == get_tarval_mode(get_Const_tarval(n)),
997 "Const node, tarval and node mode mismatch", 0
1003 * verify a SymConst node
1005 static int verify_node_SymConst(const ir_node *n)
1007 ir_mode *mymode = get_irn_mode(n);
1010 /* SymConst: BB --> int*/
1011 (mode_is_int(mymode) ||
1012 /* SymConst: BB --> ref */
1013 mode_is_reference(mymode))
1014 ,"SymConst node", 0);
1021 static int verify_node_Sel(const ir_node *n)
1024 ir_mode *mymode = get_irn_mode(n);
1025 ir_mode *op1mode = get_irn_mode(get_Sel_mem(n));
1026 ir_mode *op2mode = get_irn_mode(get_Sel_ptr(n));
1030 /* Sel: BB x M x ref x int^n --> ref */
1031 (op1mode == mode_M && op2mode == mymode && mode_is_reference(mymode)),
1032 "Sel node", 0, show_node_failure(n);
1035 for (i = get_Sel_n_indexs(n) - 1; i >= 0; --i) {
1036 ASSERT_AND_RET_DBG(mode_is_int(get_irn_mode(get_Sel_index(n, i))), "Sel node", 0, show_node_failure(n););
1038 ent = get_Sel_entity(n);
1039 ASSERT_AND_RET_DBG(ent, "Sel node with empty entity", 0, show_node_failure(n););
1044 * verify an InstOf node
1046 static int verify_node_InstOf(const ir_node *n)
1048 ir_mode *mymode = get_irn_mode(n);
1049 ir_mode *op1mode = get_irn_mode(get_InstOf_obj(n));
1051 ASSERT_AND_RET(mode_T == mymode, "mode of Instof is not a tuple", 0);
1052 ASSERT_AND_RET(mode_is_data(op1mode), "Instof not on data", 0);
1057 * Check if the pinned state is right.
1059 static int verify_right_pinned(const ir_node *n)
1063 if (get_irn_pinned(n) == op_pin_state_pinned)
1065 mem = get_Call_mem(n);
1067 /* if it's not pinned, its memory predecessor must be NoMem or Pin */
1068 if (is_NoMem(mem) || is_Pin(mem))
1074 * verify a Call node
1076 static int verify_node_Call(const ir_node *n)
1078 ir_graph *irg = get_irn_irg(n);
1079 ir_mode *mymode = get_irn_mode(n);
1080 ir_mode *op1mode = get_irn_mode(get_Call_mem(n));
1081 ir_mode *op2mode = get_irn_mode(get_Call_ptr(n));
1086 /* Call: BB x M x ref x data1 x ... x datan
1087 --> M x datan+1 x ... x data n+m */
1088 ASSERT_AND_RET( op1mode == mode_M && mode_is_reference(op2mode), "Call node", 0 ); /* operand M x ref */
1090 /* NoMem nodes are only allowed as memory input if the Call is NOT pinned */
1091 ASSERT_AND_RET(verify_right_pinned(n),"Call node with wrong memory input", 0 );
1093 mt = get_Call_type(n);
1094 if (get_unknown_type() == mt) {
1098 n_params = get_Call_n_params(n);
1099 for (i = 0; i < n_params; ++i) {
1100 ASSERT_AND_RET( mode_is_datab(get_irn_mode(get_Call_param(n, i))), "Call node", 0 ); /* operand datai */
1103 ASSERT_AND_RET( mymode == mode_T, "Call result not a tuple", 0 ); /* result T */
1104 /* Compare arguments of node with those of type */
1106 if (get_method_variadicity(mt) == variadicity_variadic) {
1108 get_Call_n_params(n) >= get_method_n_params(mt),
1109 "Number of args for Call doesn't match number of args in variadic type.",
1111 ir_fprintf(stderr, "Call %+F has %d params, type %d\n",
1112 n, get_Call_n_params(n), get_method_n_params(mt));
1116 get_Call_n_params(n) == get_method_n_params(mt),
1117 "Number of args for Call doesn't match number of args in non variadic type.",
1119 ir_fprintf(stderr, "Call %+F has %d params, type %d\n",
1120 n, get_Call_n_params(n), get_method_n_params(mt));
1124 for (i = 0; i < get_method_n_params(mt); i++) {
1125 ir_type *t = get_method_param_type(mt, i);
1127 if (get_irg_phase_state(irg) != phase_backend) {
1128 if (is_atomic_type(t)) {
1130 get_irn_mode(get_Call_param(n, i)) == get_type_mode(t),
1131 "Mode of arg for Call doesn't match mode of arg type.", 0,
1132 show_call_param(n, mt);
1135 /* call with a compound type, mode must be reference */
1137 mode_is_reference(get_irn_mode(get_Call_param(n, i))),
1138 "Mode of arg for Call doesn't match mode of arg type.", 0,
1139 show_call_param(n, mt);
1149 * verify an Add node
1151 static int verify_node_Add(const ir_node *n)
1153 ir_mode *mymode = get_irn_mode(n);
1154 ir_mode *op1mode = get_irn_mode(get_Add_left(n));
1155 ir_mode *op2mode = get_irn_mode(get_Add_right(n));
1159 /* common Add: BB x numP x numP --> numP */
1160 (op1mode == mymode && op2mode == op1mode && mode_is_data(mymode)) ||
1161 /* Pointer Add: BB x ref x int --> ref */
1162 (mode_is_reference(op1mode) && mode_is_int(op2mode) && op1mode == mymode) ||
1163 /* Pointer Add: BB x int x ref --> ref */
1164 (mode_is_int(op1mode) && op2mode == mymode && mode_is_reference(mymode))
1167 show_binop_failure(n, "/* common Add: BB x numP x numP --> numP */ |\n"
1168 "/* Pointer Add: BB x ref x int --> ref */ |\n"
1169 "/* Pointer Add: BB x int x ref --> ref */");
1177 static int verify_node_Sub(const ir_node *n)
1179 ir_mode *mymode = get_irn_mode(n);
1180 ir_mode *op1mode = get_irn_mode(get_Sub_left(n));
1181 ir_mode *op2mode = get_irn_mode(get_Sub_right(n));
1185 /* common Sub: BB x numP x numP --> numP */
1186 (mymode ==op1mode && mymode == op2mode && mode_is_data(op1mode)) ||
1187 /* Pointer Sub: BB x ref x int --> ref */
1188 (op1mode == mymode && mode_is_int(op2mode) && mode_is_reference(mymode)) ||
1189 /* Pointer Sub: BB x ref x ref --> int */
1190 (op1mode == op2mode && mode_is_reference(op2mode) && mode_is_int(mymode))
1193 show_binop_failure(n, "/* common Sub: BB x numP x numP --> numP */ |\n"
1194 "/* Pointer Sub: BB x ref x int --> ref */ |\n"
1195 "/* Pointer Sub: BB x ref x ref --> int */" );
1201 * verify a Minus node
1203 static int verify_node_Minus(const ir_node *n)
1205 ir_mode *mymode = get_irn_mode(n);
1206 ir_mode *op1mode = get_irn_mode(get_Minus_op(n));
1209 /* Minus: BB x num --> num */
1210 op1mode == mymode && mode_is_num(op1mode), "Minus node", 0,
1211 show_unop_failure(n , "/* Minus: BB x num --> num */");
1219 static int verify_node_Mul(const ir_node *n)
1221 ir_mode *mymode = get_irn_mode(n);
1222 ir_mode *op1mode = get_irn_mode(get_Mul_left(n));
1223 ir_mode *op2mode = get_irn_mode(get_Mul_right(n));
1227 /* Mul: BB x int_n x int_n --> int_n|int_2n */
1228 (mode_is_int(op1mode) && op2mode == op1mode && mode_is_int(mymode) &&
1229 (op1mode == mymode || get_mode_size_bits(op1mode) * 2 == get_mode_size_bits(mymode))) ||
1230 /* Mul: BB x float x float --> float */
1231 (mode_is_float(op1mode) && op2mode == op1mode && mymode == op1mode)
1234 show_binop_failure(n, "/* Mul: BB x int_n x int_n --> int_n|int_2n */ |\n"
1235 "/* Mul: BB x float x float --> float */");
1241 * verify a Mulh node
1243 static int verify_node_Mulh(const ir_node *n)
1245 ir_mode *mymode = get_irn_mode(n);
1246 ir_mode *op1mode = get_irn_mode(get_Mulh_left(n));
1247 ir_mode *op2mode = get_irn_mode(get_Mulh_right(n));
1251 /* Mulh: BB x int x int --> int */
1252 (mode_is_int(op1mode) && op2mode == op1mode && op1mode == mymode)
1255 show_binop_failure(n, "/* Mulh: BB x int x int --> int */");
1263 static int verify_node_Div(const ir_node *n)
1265 ir_mode *mymode = get_irn_mode(n);
1266 ir_mode *op1mode = get_irn_mode(get_Div_mem(n));
1267 ir_mode *op2mode = get_irn_mode(get_Div_left(n));
1268 ir_mode *op3mode = get_irn_mode(get_Div_right(n));
1271 /* Div: BB x M x data x data --> M x X x data */
1272 op1mode == mode_M &&
1273 op2mode == op3mode &&
1274 mode_is_data(op2mode) &&
1284 static int verify_node_Mod(const ir_node *n)
1286 ir_mode *mymode = get_irn_mode(n);
1287 ir_mode *op1mode = get_irn_mode(get_Mod_mem(n));
1288 ir_mode *op2mode = get_irn_mode(get_Mod_left(n));
1289 ir_mode *op3mode = get_irn_mode(get_Mod_right(n));
1292 /* Mod: BB x M x int x int --> M x X x int */
1293 op1mode == mode_M &&
1294 op2mode == op3mode &&
1295 mode_is_int(op2mode) &&
1303 * verify a logical And, Or, Eor node
1305 static int verify_node_Logic(const ir_node *n)
1307 ir_mode *mymode = get_irn_mode(n);
1308 ir_mode *op1mode = get_irn_mode(get_binop_left(n));
1309 ir_mode *op2mode = get_irn_mode(get_binop_right(n));
1312 /* And or Or or Eor: BB x int x int --> int */
1313 (mode_is_int(mymode) || mode_is_reference(mymode) || mymode == mode_b) &&
1314 op2mode == op1mode &&
1316 "And, Or or Eor node", 0,
1317 show_binop_failure(n, "/* And or Or or Eor: BB x int x int --> int */");
1322 static int verify_node_And(const ir_node *n)
1324 return verify_node_Logic(n);
1327 static int verify_node_Or(const ir_node *n)
1329 return verify_node_Logic(n);
1332 static int verify_node_Eor(const ir_node *n)
1334 return verify_node_Logic(n);
1340 static int verify_node_Not(const ir_node *n)
1342 ir_mode *mymode = get_irn_mode(n);
1343 ir_mode *op1mode = get_irn_mode(get_Not_op(n));
1346 /* Not: BB x int --> int */
1347 (mode_is_int(mymode) || mymode == mode_b) &&
1350 show_unop_failure(n, "/* Not: BB x int --> int */");
1358 static int verify_node_Cmp(const ir_node *n)
1360 ir_mode *mymode = get_irn_mode(n);
1361 ir_mode *op1mode = get_irn_mode(get_Cmp_left(n));
1362 ir_mode *op2mode = get_irn_mode(get_Cmp_right(n));
1365 /* Cmp: BB x datab x datab --> b16 */
1366 mode_is_datab(op1mode) &&
1367 op2mode == op1mode &&
1370 show_binop_failure(n, "/* Cmp: BB x datab x datab --> b16 */");
1376 * verify a Shift node
1378 static int verify_node_Shift(const ir_node *n)
1380 ir_mode *mymode = get_irn_mode(n);
1381 ir_mode *op1mode = get_irn_mode(get_binop_left(n));
1382 ir_mode *op2mode = get_irn_mode(get_binop_right(n));
1385 /* Shl, Shr or Shrs: BB x int x int_u --> int */
1386 mode_is_int(op1mode) &&
1387 mode_is_int(op2mode) &&
1388 !mode_is_signed(op2mode) &&
1390 "Shl, Shr or Shrs node", 0,
1391 show_binop_failure(n, "/* Shl, Shr or Shrs: BB x int x int_u --> int */");
1396 static int verify_node_Shl(const ir_node *n)
1398 return verify_node_Shift(n);
1401 static int verify_node_Shr(const ir_node *n)
1403 return verify_node_Shift(n);
1406 static int verify_node_Shrs(const ir_node *n)
1408 return verify_node_Shift(n);
1412 * verify a Rotl node
1414 static int verify_node_Rotl(const ir_node *n)
1416 ir_mode *mymode = get_irn_mode(n);
1417 ir_mode *op1mode = get_irn_mode(get_Rotl_left(n));
1418 ir_mode *op2mode = get_irn_mode(get_Rotl_right(n));
1421 /* Rotl: BB x int x int --> int */
1422 mode_is_int(op1mode) &&
1423 mode_is_int(op2mode) &&
1426 show_binop_failure(n, "/* Rotl: BB x int x int --> int */");
1432 * verify a Conv node
1434 static int verify_node_Conv(const ir_node *n)
1436 ir_mode *mymode = get_irn_mode(n);
1437 ir_mode *op1mode = get_irn_mode(get_Conv_op(n));
1439 ASSERT_AND_RET_DBG(mode_is_data(op1mode) && mode_is_data(mymode),
1441 show_unop_failure(n, "/* Conv: BB x datab --> data */");
1447 * verify a Cast node
1449 static int verify_node_Cast(const ir_node *n)
1451 ir_mode *mymode = get_irn_mode(n);
1452 ir_mode *op1mode = get_irn_mode(get_Cast_op(n));
1455 /* Conv: BB x datab1 --> datab2 */
1456 mode_is_data(op1mode) && op1mode == mymode,
1458 show_unop_failure(n, "/* Conv: BB x datab1 --> datab2 */");
1466 static int verify_node_Phi(const ir_node *n)
1468 ir_mode *mymode = get_irn_mode(n);
1469 ir_node *block = get_nodes_block(n);
1472 /* a Phi node MUST have the same number of inputs as its block
1473 * Exception is a phi with 0 inputs which is used when (re)constructing the
1475 if (! is_Bad(block) && get_irg_phase_state(get_irn_irg(n)) != phase_building && get_irn_arity(n) > 0) {
1477 get_irn_arity(n) == get_irn_arity(block),
1478 "wrong number of inputs in Phi node", 0,
1479 show_phi_inputs(n, block);
1483 /* Phi: BB x dataM^n --> dataM */
1484 for (i = get_Phi_n_preds(n) - 1; i >= 0; --i) {
1485 ir_node *pred = get_Phi_pred(n, i);
1486 ASSERT_AND_RET_DBG(get_irn_mode(pred) == mymode,
1487 "Phi node", 0, show_phi_failure(n, pred, i);
1490 ASSERT_AND_RET(mode_is_dataM(mymode) || mymode == mode_b, "Phi node", 0 );
1496 * verify a Load node
1498 static int verify_node_Load(const ir_node *n)
1500 ir_graph *irg = get_irn_irg(n);
1501 ir_mode *mymode = get_irn_mode(n);
1502 ir_mode *op1mode = get_irn_mode(get_Load_mem(n));
1503 ir_mode *op2mode = get_irn_mode(get_Load_ptr(n));
1505 ASSERT_AND_RET(op1mode == mode_M, "Load node", 0);
1506 if (get_irg_phase_state(irg) != phase_backend) {
1507 ASSERT_AND_RET(mode_is_reference(op2mode), "Load node", 0 );
1509 ASSERT_AND_RET( mymode == mode_T, "Load node", 0 );
1512 * jack's gen_add_firm_code:simpleSel seems to build Load (Load
1513 * (Proj (Proj))) sometimes ...
1515 * interprete.c:ai_eval seems to assume that this happens, too
1517 * obset.c:get_abstval_any can't deal with this if the load has
1521 ir_entity *ent = hunt_for_entity (get_Load_ptr (n), n);
1522 assert ((NULL != ent) || (mymode != mode_T));
1530 * verify a Store node
1532 static int verify_node_Store(const ir_node *n)
1534 ir_graph *irg = get_irn_irg(n);
1537 ir_mode *mymode = get_irn_mode(n);
1538 ir_mode *op1mode = get_irn_mode(get_Store_mem(n));
1539 ir_mode *op2mode = get_irn_mode(get_Store_ptr(n));
1540 ir_mode *op3mode = get_irn_mode(get_Store_value(n));
1542 ASSERT_AND_RET(op1mode == mode_M && mode_is_datab(op3mode), "Store node", 0 );
1543 if (get_irg_phase_state(irg) != phase_backend) {
1544 ASSERT_AND_RET(mode_is_reference(op2mode), "Store node", 0 );
1546 ASSERT_AND_RET(mymode == mode_T, "Store node", 0);
1548 target = get_ptr_entity(get_Store_ptr(n));
1549 if (verify_entities && target && get_irg_phase_state(irg) == phase_high) {
1551 * If lowered code, any Sels that add 0 may be removed, causing
1552 * an direct access to entities of array or compound type.
1553 * Prevent this by checking the phase.
1555 ASSERT_AND_RET( op3mode == get_type_mode(get_entity_type(target)),
1563 * verify an Alloc node
1565 static int verify_node_Alloc(const ir_node *n)
1567 ir_mode *mymode = get_irn_mode(n);
1568 ir_mode *op1mode = get_irn_mode(get_Alloc_mem(n));
1569 ir_mode *op2mode = get_irn_mode(get_Alloc_count(n));
1572 /* Alloc: BB x M x int_u --> M x X x ref */
1573 op1mode == mode_M &&
1574 mode_is_int(op2mode) &&
1575 !mode_is_signed(op2mode) &&
1578 show_node_failure(n);
1584 * verify a Free node
1586 static int verify_node_Free(const ir_node *n)
1588 ir_mode *mymode = get_irn_mode(n);
1589 ir_mode *op1mode = get_irn_mode(get_Free_mem(n));
1590 ir_mode *op2mode = get_irn_mode(get_Free_ptr(n));
1591 ir_mode *op3mode = get_irn_mode(get_Free_count(n));
1594 /* Free: BB x M x ref x int_u --> M */
1595 op1mode == mode_M && mode_is_reference(op2mode) &&
1596 mode_is_int(op3mode) &&
1597 !mode_is_signed(op3mode) &&
1600 show_triop_failure(n, "/* Free: BB x M x ref x int_u --> M */");
1606 * verify a Sync node
1608 static int verify_node_Sync(const ir_node *n)
1611 ir_mode *mymode = get_irn_mode(n);
1613 /* Sync: BB x M^n --> M */
1614 for (i = get_Sync_n_preds(n) - 1; i >= 0; --i) {
1615 ASSERT_AND_RET( get_irn_mode(get_Sync_pred(n, i)) == mode_M, "Sync node", 0 );
1617 ASSERT_AND_RET( mymode == mode_M, "Sync node", 0 );
1622 * verify a Confirm node
1624 static int verify_node_Confirm(const ir_node *n)
1626 ir_mode *mymode = get_irn_mode(n);
1627 ir_mode *op1mode = get_irn_mode(get_Confirm_value(n));
1628 ir_mode *op2mode = get_irn_mode(get_Confirm_bound(n));
1631 /* Confirm: BB x T x T --> T */
1632 op1mode == mymode &&
1635 show_binop_failure(n, "/* Confirm: BB x T x T --> T */");
1643 static int verify_node_Mux(const ir_node *n)
1645 ir_mode *mymode = get_irn_mode(n);
1646 ir_mode *op1mode = get_irn_mode(get_Mux_sel(n));
1647 ir_mode *op2mode = get_irn_mode(get_Mux_true(n));
1648 ir_mode *op3mode = get_irn_mode(get_Mux_false(n));
1651 /* Mux: BB x b x datab x datab --> datab */
1652 op1mode == mode_b &&
1653 op2mode == mymode &&
1654 op3mode == mymode &&
1655 mode_is_datab(mymode),
1662 * verify a CopyB node
1664 static int verify_node_CopyB(const ir_node *n)
1666 ir_graph *irg = get_irn_irg(n);
1667 ir_mode *mymode = get_irn_mode(n);
1668 ir_mode *op1mode = get_irn_mode(get_CopyB_mem(n));
1669 ir_mode *op2mode = get_irn_mode(get_CopyB_dst(n));
1670 ir_mode *op3mode = get_irn_mode(get_CopyB_src(n));
1671 ir_type *t = get_CopyB_type(n);
1673 /* CopyB: BB x M x ref x ref --> M x X */
1674 ASSERT_AND_RET(mymode == mode_T && op1mode == mode_M, "CopyB node", 0);
1675 if (get_irg_phase_state(irg) != phase_backend) {
1676 ASSERT_AND_RET(mode_is_reference(op2mode) && mode_is_reference(op3mode),
1681 is_compound_type(t) || is_Array_type(t),
1682 "CopyB node should copy compound types only", 0 );
1684 /* NoMem nodes are only allowed as memory input if the CopyB is NOT pinned.
1685 This should happen RARELY, as CopyB COPIES MEMORY */
1686 ASSERT_AND_RET(verify_right_pinned(n), "CopyB node with wrong memory input", 0 );
1691 * verify a Bound node
1693 static int verify_node_Bound(const ir_node *n)
1695 ir_mode *mymode = get_irn_mode(n);
1696 ir_mode *op1mode = get_irn_mode(get_Bound_mem(n));
1697 ir_mode *op2mode = get_irn_mode(get_Bound_index(n));
1698 ir_mode *op3mode = get_irn_mode(get_Bound_lower(n));
1699 ir_mode *op4mode = get_irn_mode(get_Bound_upper(n));
1701 /* Bound: BB x M x int x int x int --> M x X */
1704 op1mode == mode_M &&
1705 op2mode == op3mode &&
1706 op3mode == op4mode &&
1707 mode_is_int(op3mode),
1714 * For each usage of a node, it is checked, if the block of the
1715 * node dominates the block of the usage (for phis: the predecessor
1716 * block of the phi for the corresponding edge).
1718 * @return non-zero on success, 0 on dominance error
1720 static int check_dominance_for_node(const ir_node *use)
1722 /* This won't work for blocks and the end node */
1723 if (!is_Block(use) && !is_End(use) && !is_Anchor(use)) {
1725 ir_node *bl = get_nodes_block(use);
1727 for (i = get_irn_arity(use) - 1; i >= 0; --i) {
1728 ir_node *def = get_irn_n(use, i);
1729 ir_node *def_bl = get_nodes_block(def);
1730 ir_node *use_bl = bl;
1733 /* we have no dominance relation for unreachable blocks, so we can't
1734 * check the dominance property there */
1735 if (!is_Block(def_bl) || get_Block_dom_depth(def_bl) == -1)
1741 use_bl = get_Block_cfgpred_block(bl, i);
1744 if (!is_Block(use_bl) || get_Block_dom_depth(use_bl) == -1)
1747 irg = get_irn_irg(use);
1749 block_dominates(def_bl, use_bl),
1750 "the definition of a value used violates the dominance property", 0,
1752 "graph %+F: %+F of %+F must dominate %+F of user %+F input %d\n",
1753 irg, def_bl, def, use_bl, use, i
1761 /* Tests the modes of n and its predecessors. */
1762 int irn_verify_irg(const ir_node *n, ir_graph *irg)
1766 if (!get_node_verification_mode())
1770 * do NOT check placement in interprocedural view, as we don't always
1771 * know the "right" graph ...
1775 /* this is an expensive check for large graphs (it has a quadratic
1776 * runtime but with a small constant); so do NOT run it in release mode
1779 node_is_in_irgs_storage(irg, n),
1780 "Node is not stored on proper IR graph!", 0,
1781 show_node_on_graph(irg, n);
1784 assert(get_irn_irg(n) == irg);
1786 unsigned idx = get_irn_idx(n);
1787 ir_node *node_from_map = get_idx_irn(irg, idx);
1788 ASSERT_AND_RET_DBG(node_from_map == n, "Node index and index map entry differ", 0,
1789 ir_printf("node %+F node in map %+F(%p)\n", n, node_from_map, node_from_map);
1795 if (get_op_pinned(op) >= op_pin_state_exc_pinned) {
1796 op_pin_state state = get_irn_pinned(n);
1798 state == op_pin_state_floats ||
1799 state == op_pin_state_pinned,
1800 "invalid pin state", 0,
1801 ir_printf("node %+F", n);
1803 } else if (!is_Block(n) && is_irn_pinned_in_irg(n)
1804 && is_irg_state(irg, IR_GRAPH_STATE_NO_BADS)) {
1805 ASSERT_AND_RET_DBG(is_Block(get_nodes_block(n)) || is_Anchor(n),
1806 "block input is not a block", 0,
1807 ir_printf("node %+F", n);
1811 if (op->ops.verify_node)
1812 return op->ops.verify_node(n);
1818 int irn_verify(const ir_node *n)
1820 #ifdef DEBUG_libfirm
1821 return irn_verify_irg(n, get_irn_irg(n));
1828 /*-----------------------------------------------------------------*/
1829 /* Verify the whole graph. */
1830 /*-----------------------------------------------------------------*/
1832 #ifdef DEBUG_libfirm
1834 * Walker to check every node
1836 static void verify_wrap(ir_node *node, void *env)
1838 int *res = (int*)env;
1839 *res = irn_verify_irg(node, get_irn_irg(node));
1843 * Walker to check every node including SSA property.
1844 * Only called if dominance info is available.
1846 static void verify_wrap_ssa(ir_node *node, void *env)
1848 int *res = (int*)env;
1850 *res = irn_verify_irg(node, get_irn_irg(node));
1852 *res = check_dominance_for_node(node);
1856 #endif /* DEBUG_libfirm */
1858 typedef struct check_cfg_env_t {
1859 pmap *branch_nodes; /**< map blocks to their branching nodes,
1860 map mode_X nodes to the blocks they branch to */
1862 ir_nodeset_t reachable_blocks;
1863 ir_nodeset_t kept_nodes;
1864 ir_nodeset_t true_projs;
1865 ir_nodeset_t false_projs;
1868 static int check_block_cfg(const ir_node *block, check_cfg_env_t *env)
1874 ASSERT_AND_RET_DBG(ir_nodeset_contains(&env->reachable_blocks, block),
1875 "Block is not reachable by blockwalker (endless loop with no kept block?)", 0,
1876 ir_printf("block %+F\n", block);
1879 n_cfgpreds = get_Block_n_cfgpreds(block);
1880 branch_nodes = env->branch_nodes;
1881 for (i = 0; i < n_cfgpreds; ++i) {
1882 /* check that each mode_X node is only connected
1884 ir_node *branch = get_Block_cfgpred(block, i);
1885 ir_node *former_dest;
1886 ir_node *former_branch;
1887 ir_node *branch_proj;
1888 ir_node *branch_block;
1889 branch = skip_Tuple(branch);
1892 former_dest = pmap_get(branch_nodes, branch);
1893 ASSERT_AND_RET_DBG(former_dest==NULL || is_unknown_jump(skip_Proj(branch)),
1894 "Multiple users on mode_X node", 0,
1895 ir_printf("node %+F\n", branch);
1897 pmap_insert(branch_nodes, branch, (void*)block);
1899 /* check that there's only 1 branching instruction in each block */
1900 branch_block = get_nodes_block(branch);
1901 branch_proj = branch;
1902 if (is_Proj(branch)) {
1903 branch = skip_Proj(branch);
1905 former_branch = pmap_get(branch_nodes, branch_block);
1907 ASSERT_AND_RET_DBG(former_branch == NULL || former_branch == branch,
1908 "Multiple branching nodes in a block", 0,
1909 ir_printf("nodes %+F,%+F in block %+F\n",
1910 branch, former_branch, branch_block);
1912 pmap_insert(branch_nodes, branch_block, branch);
1914 if (is_Cond(branch)) {
1915 long pn = get_Proj_proj(branch_proj);
1916 if (pn == pn_Cond_true)
1917 ir_nodeset_insert(&env->true_projs, branch);
1918 if (pn == pn_Cond_false)
1919 ir_nodeset_insert(&env->false_projs, branch);
1920 } else if (is_Switch(branch)) {
1921 long pn = get_Proj_proj(branch_proj);
1922 if (pn == pn_Switch_default)
1923 ir_nodeset_insert(&env->true_projs, branch);
1930 static void check_cfg_walk_func(ir_node *node, void *data)
1932 check_cfg_env_t *env = (check_cfg_env_t*)data;
1933 if (!is_Block(node))
1935 env->res &= check_block_cfg(node, env);
1938 static int verify_block_branch(const ir_node *block, check_cfg_env_t *env)
1940 ir_node *branch = pmap_get(env->branch_nodes, block);
1941 ASSERT_AND_RET_DBG(branch != NULL
1942 || ir_nodeset_contains(&env->kept_nodes, block)
1943 || block == get_irg_end_block(get_irn_irg(block)),
1944 "block contains no cfop", 0,
1945 ir_printf("block %+F\n", block);
1950 static int verify_cond_projs(const ir_node *cond, check_cfg_env_t *env)
1952 ASSERT_AND_RET_DBG(ir_nodeset_contains(&env->true_projs, cond),
1953 "Cond node lacks true proj", 0,
1954 ir_printf("Cond %+F\n", cond);
1956 ASSERT_AND_RET_DBG(ir_nodeset_contains(&env->false_projs, cond),
1957 "Cond node lacks false proj", 0,
1958 ir_printf("Cond %+F\n", cond);
1963 static int verify_switch_projs(const ir_node *sw, check_cfg_env_t *env)
1965 ASSERT_AND_RET_DBG(ir_nodeset_contains(&env->true_projs, sw),
1966 "Switch node lacks default Proj", 0,
1967 ir_printf("Switch %+F\n", sw);
1972 static void assert_branch(ir_node *node, void *data)
1974 check_cfg_env_t *env = (check_cfg_env_t*)data;
1975 if (is_Block(node)) {
1976 env->res &= verify_block_branch(node, env);
1977 } else if (is_Cond(node)) {
1978 env->res &= verify_cond_projs(node, env);
1979 } else if (is_Switch(node)) {
1980 env->res &= verify_switch_projs(node, env);
1984 static void collect_reachable_blocks(ir_node *block, void *data)
1986 ir_nodeset_t *reachable_blocks = (ir_nodeset_t*) data;
1987 ir_nodeset_insert(reachable_blocks, block);
1991 * Checks CFG well-formedness
1993 static int check_cfg(ir_graph *irg)
1995 check_cfg_env_t env;
1996 env.branch_nodes = pmap_create(); /**< map blocks to branch nodes */
1998 ir_nodeset_init(&env.reachable_blocks);
1999 ir_nodeset_init(&env.true_projs);
2000 ir_nodeset_init(&env.false_projs);
2002 irg_block_walk_graph(irg, collect_reachable_blocks, NULL,
2003 &env.reachable_blocks);
2005 /* note that we do not use irg_walk_block because it will miss these
2006 * invalid blocks without a jump instruction which we want to detect
2008 irg_walk_graph(irg, check_cfg_walk_func, NULL, &env);
2010 ir_nodeset_init(&env.kept_nodes);
2012 ir_node *end = get_irg_end(irg);
2013 int arity = get_irn_arity(end);
2015 for (i = 0; i < arity; ++i) {
2016 ir_node *n = get_irn_n(end, i);
2017 ir_nodeset_insert(&env.kept_nodes, n);
2020 irg_walk_graph(irg, assert_branch, NULL, &env);
2022 ir_nodeset_destroy(&env.false_projs);
2023 ir_nodeset_destroy(&env.true_projs);
2024 ir_nodeset_destroy(&env.kept_nodes);
2025 ir_nodeset_destroy(&env.reachable_blocks);
2026 pmap_destroy(env.branch_nodes);
2031 * Calls irn_verify for each node in irg.
2032 * Graph must be in state "op_pin_state_pinned".
2033 * If dominance info is available, check the SSA property.
2035 int irg_verify(ir_graph *irg, unsigned flags)
2038 #ifdef DEBUG_libfirm
2039 int pinned = get_irg_pinned(irg) == op_pin_state_pinned;
2042 last_irg_error = NULL;
2045 if (pinned && !check_cfg(irg))
2048 if (res == 1 && (flags & VERIFY_ENFORCE_SSA) && pinned)
2053 pinned && is_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_DOMINANCE)
2054 ? verify_wrap_ssa : verify_wrap,
2059 if (get_node_verification_mode() == FIRM_VERIFICATION_REPORT && ! res) {
2060 ir_entity *ent = get_irg_entity(irg);
2063 fprintf(stderr, "irg_verify: Verifying graph %s failed\n", get_entity_name(ent));
2065 fprintf(stderr, "irg_verify: Verifying graph %p failed\n", (void *)irg);
2071 #endif /* DEBUG_libfirm */
2076 typedef struct pass_t {
2077 ir_graph_pass_t pass;
2082 * Wrapper to irg_verify to be run as an ir_graph pass.
2084 static int irg_verify_wrapper(ir_graph *irg, void *context)
2086 pass_t *pass = (pass_t*)context;
2087 irg_verify(irg, pass->flags);
2088 /* do NOT rerun the pass if verify is ok :-) */
2092 /* Creates an ir_graph pass for irg_verify(). */
2093 ir_graph_pass_t *irg_verify_pass(const char *name, unsigned flags)
2095 pass_t *pass = XMALLOCZ(pass_t);
2097 def_graph_pass_constructor(
2098 &pass->pass, name ? name : "irg_verify", irg_verify_wrapper);
2100 /* neither dump for verify */
2101 pass->pass.dump_irg = (DUMP_ON_IRG_FUNC)ir_prog_no_dump;
2102 pass->pass.verify_irg = (RUN_ON_IRG_FUNC)ir_prog_no_verify;
2104 pass->flags = flags;
2108 /* create a verify pass */
2109 int irn_verify_irg_dump(const ir_node *n, ir_graph *irg,
2110 const char **bad_string)
2113 firm_verification_t old = get_node_verification_mode();
2115 firm_verify_failure_msg = NULL;
2116 do_node_verification(FIRM_VERIFICATION_ERROR_ONLY);
2117 res = irn_verify_irg(n, irg);
2118 if (res && is_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_DOMINANCE) &&
2119 get_irg_pinned(irg) == op_pin_state_pinned)
2120 res = check_dominance_for_node(n);
2121 do_node_verification(old);
2122 *bad_string = firm_verify_failure_msg;
2127 typedef struct verify_bad_env_t {
2133 * Pre-Walker: check Bad predecessors of node.
2135 static void check_bads(ir_node *node, void *env)
2137 verify_bad_env_t *venv = (verify_bad_env_t*)env;
2138 int i, arity = get_irn_arity(node);
2139 ir_graph *irg = get_irn_irg(node);
2141 if (is_Block(node)) {
2142 if ((venv->flags & BAD_CF) == 0) {
2144 /* check for Bad Block predecessor */
2145 for (i = 0; i < arity; ++i) {
2146 ir_node *pred = get_irn_n(node, i);
2149 venv->res |= BAD_CF;
2151 if (get_node_verification_mode() == FIRM_VERIFICATION_REPORT) {
2152 fprintf(stderr, "irg_verify_bads: Block %ld has Bad predecessor\n", get_irn_node_nr(node));
2154 if (get_node_verification_mode() == FIRM_VERIFICATION_ON) {
2155 dump_ir_graph(irg, "assert");
2156 assert(0 && "Bad CF detected");
2162 if ((venv->flags & BAD_BLOCK) == 0) {
2164 /* check for Bad Block */
2165 if (is_Bad(get_nodes_block(node))) {
2166 venv->res |= BAD_BLOCK;
2168 if (get_node_verification_mode() == FIRM_VERIFICATION_REPORT) {
2169 fprintf(stderr, "irg_verify_bads: node %ld has Bad Block\n", get_irn_node_nr(node));
2171 if (get_node_verification_mode() == FIRM_VERIFICATION_ON) {
2172 dump_ir_graph(irg, "assert");
2173 assert(0 && "Bad CF detected");
2178 if ((venv->flags & TUPLE) == 0) {
2179 if (is_Tuple(node)) {
2182 if (get_node_verification_mode() == FIRM_VERIFICATION_REPORT) {
2183 fprintf(stderr, "irg_verify_bads: node %ld is a Tuple\n", get_irn_node_nr(node));
2185 if (get_node_verification_mode() == FIRM_VERIFICATION_ON) {
2186 dump_ir_graph(irg, "assert");
2187 assert(0 && "Tuple detected");
2192 for (i = 0; i < arity; ++i) {
2193 ir_node *pred = get_irn_n(node, i);
2196 /* check for Phi with Bad inputs */
2197 if (is_Phi(node) && !is_Bad(get_nodes_block(node)) && is_Bad(get_irn_n(get_nodes_block(node), i))) {
2198 if (venv->flags & BAD_CF)
2201 venv->res |= BAD_CF;
2203 if (get_node_verification_mode() == FIRM_VERIFICATION_REPORT) {
2204 fprintf(stderr, "irg_verify_bads: Phi %ld has Bad Input\n", get_irn_node_nr(node));
2206 if (get_node_verification_mode() == FIRM_VERIFICATION_ON) {
2207 dump_ir_graph(irg, "assert");
2208 assert(0 && "Bad CF detected");
2213 /* Bad node input */
2214 if ((venv->flags & BAD_DF) == 0) {
2215 venv->res |= BAD_DF;
2217 if (get_node_verification_mode() == FIRM_VERIFICATION_REPORT) {
2218 fprintf(stderr, "irg_verify_bads: node %ld has Bad Input\n", get_irn_node_nr(node));
2220 if (get_node_verification_mode() == FIRM_VERIFICATION_ON) {
2221 dump_ir_graph(irg, "assert");
2222 assert(0 && "Bad NON-CF detected");
2231 * verify occurrence of bad nodes
2233 int irg_verify_bads(ir_graph *irg, int flags)
2235 verify_bad_env_t env;
2240 irg_walk_graph(irg, check_bads, NULL, &env);
2246 * set the default verify operation
2248 void firm_set_default_verifier(unsigned code, ir_op_ops *ops)
2252 ops->verify_node = verify_node_##a; \
2305 ops->verify_proj_node = verify_node_Proj_##a; \