3 * File name: testprograms/irr_cf_example.c
4 * Purpose: Test Phi construction with irregular control flow.
5 * Author: Christian Schaefer, Goetz Lindenmaier
9 * Copyright: (c) 1999-2003 Universität Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
16 #include <libfirm/firm.h>
19 * This file constructs a control flow of following shape:
35 * This is a program as, e.g.,
48 char *dump_file_suffix = "";
49 ir_graph *irg; /* this variable contains the irgraph */
50 ir_type *owner; /* the class in which this method is defined */
51 ir_type *proc_main; /* typeinformation for the method main */
52 ir_entity *ent; /* represents this method as ir_entity of owner */
53 ir_node *expr, *c1, *c2, *cond, *f, *t, *jmp, *x;
55 printf("\nCreating an IR graph: IRR_CF...\n");
59 set_opt_constant_folding(0); /* so that stupid test are not evaluated. */
61 /* FIRM was designed for oo languages where all methods belong to a class.
62 * For imperative languages like C we view a file as a large class containing
63 * all functions as methods in this file.
64 * Therefore we define a class "empty" according to the file name
65 * with a method main as an ir_entity.
67 #define CLASSNAME "IRREGULAR_CF"
68 #define METHODNAME "main"
72 owner = new_type_class(new_id_from_chars(CLASSNAME, strlen(CLASSNAME)));
73 proc_main = new_type_method(new_id_from_chars(METHODNAME, strlen(METHODNAME)),
75 ent = new_entity(owner,
76 new_id_from_chars(METHODNAME, strlen(METHODNAME)),
78 get_entity_ld_name(ent);
79 #define NUM_OF_LOCAL_VARS 0
81 irg = new_ir_graph(ent, NUM_OF_LOCAL_VARS);
83 /* two make two conditionals that represent a switch */
84 expr = new_Const(mode_Is, new_tarval_from_long(0, mode_Is));
85 c1 = new_Const(mode_Is, new_tarval_from_long(1, mode_Is));
86 c2 = new_Const(mode_Is, new_tarval_from_long(2, mode_Is));
88 cond = new_Cond(new_Proj(new_Cmp(expr, c1), mode_b, pn_Cmp_Eq));
89 f = new_Proj(cond, mode_X, pn_Cond_false);
90 t = new_Proj(cond, mode_X, pn_Cond_true);
91 mature_immBlock(get_irg_current_block(irg));
94 add_immBlock_pred(get_irg_current_block(irg), t);
96 mature_immBlock(get_irg_current_block(irg));
99 add_immBlock_pred(get_irg_current_block(irg), f);
100 cond = new_Cond(new_Proj(new_Cmp(expr, c2), mode_b, pn_Cmp_Eq));
101 f = new_Proj(cond, mode_X, pn_Cond_false);
102 t = new_Proj(cond, mode_X, pn_Cond_true);
103 mature_immBlock(get_irg_current_block(irg));
106 add_immBlock_pred(get_irg_current_block(irg), t);
107 add_immBlock_pred(get_irg_current_block(irg), jmp);
109 mature_immBlock(get_irg_current_block(irg));
112 add_immBlock_pred(get_irg_current_block(irg), f);
114 mature_immBlock(get_irg_current_block(irg));
117 add_immBlock_pred(get_irg_current_block(irg), t);
118 add_immBlock_pred(get_irg_current_block(irg), jmp);
120 x = new_Return(get_store(), 0, NULL);
122 mature_immBlock(get_irg_current_block(irg));
124 add_immBlock_pred(get_irg_end_block(irg), x);
125 mature_immBlock(get_irg_end_block(irg));
127 irg_finalize_cons(irg);
129 printf("Optimizing ...\n");
130 dead_node_elimination(irg);
132 /* verify the graph */
135 printf("Dumping the graph and a control flow graph.\n");
136 dump_ir_block_graph(irg, dump_file_suffix);
137 dump_cfg(irg, dump_file_suffix);
138 printf("Use ycomp to view these graphs:\n");
139 printf("ycomp IRREGULAR_CF_main.vcg\n\n");
140 printf("ycomp IRREGULAR_CF_main-cfg.vcg\n\n");