3 * File name: testprograms/irr_loop_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.
21 * This file constructs a control flow of following shape:
30 * LoopBlock1 LoopBlock2
39 * scndBlock <---------- firstBlock
44 * LoopBlock1 LoopBlock2 1 LoopBlock3
49 * nextBlock <---------- /
54 int main(int argc, char **argv)
56 ir_graph *irg; /* this variable contains the irgraph */
57 type *owner; /* the class in which this method is defined */
58 type *proc_tp; /* typeinformation for the method main */
59 entity *ent; /* represents this method as entity of owner */
60 ir_node *expr, *c1, *c2, *c3, *c4, *c5, *cond, *f, *t, *f2, *loopBlock1, *f_l1, *t_l1,
61 *loopBlock2, *f_l2, *t_l2, *loopBlock3, *f_l3, *t_l3, *x;
67 set_opt_constant_folding (0); /* so that the stupid tests are not optimized. */
69 set_opt_dead_node_elimination(1);
70 turn_off_edge_labels();
72 /* FIRM was designed for oo languages where all methods belong to a class.
73 * For imperative languages like C we view a file as a large class containing
74 * all functions as methods in this file.
75 * Therefore we define a class "empty" according to the file name
76 * with a method main as an entity.
78 #define CLASSNAME "IRR_LOOP"
79 #define METHOD_TP_NAME "METH_TP_NOARG_NORES"
82 #define NUM_OF_LOCAL_VARS 0
84 owner = new_type_class (new_id_from_str (CLASSNAME));
85 printf("\nCreating testprogram: %s...\n", CLASSNAME);
86 proc_tp = new_type_method(new_id_from_str(METHOD_TP_NAME), NRARGS, NRES);
88 /* --- method loop1 ------------------------------------------------------ */
90 #define METHODNAME "loop1"
92 ent = new_entity (owner, new_id_from_str (METHODNAME), proc_tp);
93 get_entity_ld_name(ent); /* To enforce name mangling for vcg graph name */
95 irg = new_ir_graph (ent, NUM_OF_LOCAL_VARS);
97 /* to make three conditionals */
98 expr = new_Const (mode_Is, new_tarval_from_long (0, mode_Is));
99 c1 = new_Const (mode_Is, new_tarval_from_long (1, mode_Is));
100 c2 = new_Const (mode_Is, new_tarval_from_long (2, mode_Is));
101 c3 = new_Const (mode_Is, new_tarval_from_long (2, mode_Is));
103 cond = new_Cond(new_Proj(new_Cmp(expr, c1), mode_b, pn_Cmp_Eq));
104 f = new_Proj(cond, mode_X, 0);
105 t = new_Proj(cond, mode_X, 1);
106 mature_immBlock(get_irg_current_block(irg));
108 loopBlock1 = new_immBlock();
109 add_immBlock_pred(loopBlock1, t);
110 cond = new_Cond(new_Proj(new_Cmp(expr, c2), mode_b, pn_Cmp_Eq));
111 f_l1 = new_Proj(cond, mode_X, 0);
112 t_l1 = new_Proj(cond, mode_X, 1);
114 loopBlock2 = new_immBlock();
115 add_immBlock_pred(loopBlock2, f);
116 cond = new_Cond(new_Proj(new_Cmp(expr, c3), mode_b, pn_Cmp_Eq));
117 f_l2 = new_Proj(cond, mode_X, 0);
118 t_l2 = new_Proj(cond, mode_X, 1);
120 add_immBlock_pred(loopBlock1, t_l2);
121 add_immBlock_pred(loopBlock2, t_l1);
122 mature_immBlock(loopBlock1);
123 mature_immBlock(loopBlock2);
126 add_immBlock_pred(get_irg_current_block(irg), f_l2);
127 add_immBlock_pred(get_irg_current_block(irg), f_l1);
128 x = new_Return (get_store(), 0, NULL);
129 mature_immBlock (get_irg_current_block(irg));
131 add_immBlock_pred (get_irg_end_block(irg), x);
132 mature_immBlock (get_irg_end_block(irg));
134 irg_finalize_cons (irg);
136 /* --- method loop2 ------------------------------------------------------ */
139 #define METHODNAME "loop2"
141 ent = new_entity (owner, new_id_from_str (METHODNAME), proc_tp);
142 get_entity_ld_name(ent); /* To enforce name mangling for vcg graph name */
144 irg = new_ir_graph (ent, NUM_OF_LOCAL_VARS);
146 /* to make several conditionals */
147 expr = new_Const (mode_Is, new_tarval_from_long (0, mode_Is));
148 c1 = new_Const (mode_Is, new_tarval_from_long (1, mode_Is));
149 c2 = new_Const (mode_Is, new_tarval_from_long (2, mode_Is));
150 c3 = new_Const (mode_Is, new_tarval_from_long (3, mode_Is));
151 c4 = new_Const (mode_Is, new_tarval_from_long (4, mode_Is));
152 c5 = new_Const (mode_Is, new_tarval_from_long (5, mode_Is));
154 cond = new_Cond(new_Proj(new_Cmp(expr, c1), mode_b, pn_Cmp_Eq));
155 f2 = new_Proj(cond, mode_X, 0);
156 t = new_Proj(cond, mode_X, 1);
157 mature_immBlock(get_irg_current_block(irg));
160 add_immBlock_pred(get_irg_current_block(irg), t);
161 cond = new_Cond(new_Proj(new_Cmp(expr, c5), mode_b, pn_Cmp_Eq));
162 f = new_Proj(cond, mode_X, 0);
163 t = new_Proj(cond, mode_X, 1);
164 mature_immBlock(get_irg_current_block(irg));
166 loopBlock1 = new_immBlock();
167 add_immBlock_pred(loopBlock1, t);
168 cond = new_Cond(new_Proj(new_Cmp(expr, c2), mode_b, pn_Cmp_Eq));
169 f_l1 = new_Proj(cond, mode_X, 0);
170 t_l1 = new_Proj(cond, mode_X, 1);
172 loopBlock2 = new_immBlock();
173 add_immBlock_pred(loopBlock2, f);
174 cond = new_Cond(new_Proj(new_Cmp(expr, c3), mode_b, pn_Cmp_Eq));
175 f_l2 = new_Proj(cond, mode_X, 0);
176 t_l2 = new_Proj(cond, mode_X, 1);
178 loopBlock3 = new_immBlock();
179 add_immBlock_pred(loopBlock3, f2);
180 cond = new_Cond(new_Proj(new_Cmp(expr, c4), mode_b, pn_Cmp_Eq));
181 f_l3 = new_Proj(cond, mode_X, 0);
182 t_l3 = new_Proj(cond, mode_X, 1);
184 add_immBlock_pred(loopBlock1, t_l2);
185 add_immBlock_pred(loopBlock2, t_l1);
186 add_immBlock_pred(loopBlock3, f_l2);
187 add_immBlock_pred(loopBlock2, t_l3);
188 mature_immBlock(loopBlock1);
189 mature_immBlock(loopBlock2);
190 mature_immBlock(loopBlock3);
193 add_immBlock_pred(get_irg_current_block(irg), f_l1);
194 add_immBlock_pred(get_irg_current_block(irg), f_l3);
195 x = new_Return (get_store(), 0, NULL);
196 mature_immBlock (get_irg_current_block(irg));
198 add_immBlock_pred (get_irg_end_block(irg), x);
199 mature_immBlock (get_irg_end_block(irg));
201 irg_finalize_cons (irg);
203 /* --- more ... ------------------------------------------------------ */
205 printf("Optimizing ...\n");
206 //dead_node_elimination(irg);
208 for (i = 0; i < get_irp_n_irgs(); ++i) {
209 current_ir_graph = get_irp_irg(i);
210 /* verify the graph */
211 irg_vrfy(current_ir_graph);
212 construct_cf_backedges(current_ir_graph);
213 dump_loop_tree(current_ir_graph, "");
215 printf("Dumping the graph and a control flow graph.\n");
216 dump_ir_block_graph(current_ir_graph, "");
217 dump_cfg(current_ir_graph, "");
218 printf("Use xvcg to view these graphs:\n");
219 printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");
222 /* Error for loop2 */
223 compute_execution_frequency(get_irp_irg(0), 10, 0.001);
224 dump_interval_graph(get_irp_irg(0), "");