3 * File name: testprograms/strength_red_example.c
4 * Purpose: Shows how strength red works
5 * Author: Beyhan Veliev
9 * Copyright: (c) 2004 Universität Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
23 * This file constructs the ir for the following pseudo-program:
37 #define new_Const_int(n) new_Const(mode_Is, new_tarval_from_long(n, mode_Is))
40 #define CLASSNAME "STRENGTH_RED_EXAMPLE"
41 #define METHODNAME1 "STRENGTH_RED_EXAMPLE_m1"
42 #define METHODNAME2 "STRENGTH_RED_EXAMPLE_m2"
43 #define METHODNAME3 "STRENGTH_RED_EXAMPLE_m3"
44 #define METHODNAME4 "STRENGTH_RED_EXAMPLE_m4"
45 #define METHODNAME5 "STRENGTH_RED_EXAMPLE_m5"
46 #define METHODNAME6 "STRENGTH_RED_EXAMPLE_m6"
47 #define METHODNAME7 "STRENGTH_RED_EXAMPLE_m7"
48 #define METHODNAME8 "STRENGTH_RED_EXAMPLE_m8"
49 #define METHODNAME9 "STRENGTH_RED_EXAMPLE_m9"
50 #define METHODTPNAME "STRENGTH_RED_EXAMPLE_meth_tp"
57 #define PRIM_NAME "int"
62 static int arr_pos = 1;
63 static type *typ, *typ2;
65 static ir_node *r1, *f, *r, *c2;
73 * Constructs a new (method-)function of the form
75 * typ fct_name(typ arg) {
76 * for (i = start_value; i < end_value;) { ... }
79 * After return, the loop body is the current block.
81 * @param owner owner-type of this (method-)function
82 * @param mtp the method type of this function
83 * @param fct_name the name of the function
84 * @param loop_dir the loop direction
86 static void function_begin(type *owner, type *mtp, char *fct_name, loop_dir_t loop_dir) {
90 int start_value, end_value;
93 if (loop_dir == loop_forward) {
104 /* The entity for the procedure */
105 entity *ent = new_entity (owner, new_id_from_str(fct_name), mtp);
107 /* make type infromation for the array */
108 type *array_type = new_type_array(new_id_from_str("array"), N_DIMS, typ);
110 /* set the bounds for the array */
111 set_array_bounds_int(array_type, 0, L_BOUND, U_BOUND);
113 /* The array is an entity of the owner type */
114 entity *array_ent = new_entity(owner, new_id_from_str("a"), array_type);
116 /** The code of the procedure **/
118 /* Generates start and end blocks and nodes, and a first, initial block */
120 new_ir_graph(ent, NRLOCS);
122 /* The value position used for: */
124 if (fct_name == METHODNAME7)
125 c2 = new_Const_int(5);
127 /* Generate the constant and assign it to b. The assignment is resolved to a
129 set_value(i_pos, new_Const_int(start_value));
131 sym.entity_p = array_ent;
132 set_value(arr_pos, new_SymConst(sym, symconst_addr_ent));
135 /* We know all predecessors of the block and all set_values and set_stores are
136 preformed. We can mature the block. */
137 mature_immBlock(get_irg_current_block(current_ir_graph));
139 /* Generate a conditional branch */
141 add_immBlock_pred(get_irg_current_block(current_ir_graph), x);
143 cmp = new_Cmp(new_Const_int(end_value), get_value(i_pos, mode_Is));
144 x = new_Cond(new_Proj(cmp, mode_b, cmp_dir));
146 f = new_Proj(x, mode_X, 0);
147 t = new_Proj(x, mode_X, 1);
149 /* generate and fill the loop body block */
151 add_immBlock_pred(r, t);
158 * finishes a builded function.
160 * @param b the return value
162 static void function_end(ir_node *b) {
163 ir_node *x = new_Jmp();
165 add_immBlock_pred(r1, x);
169 add_immBlock_pred(get_cur_block(), f);
170 mature_immBlock (get_cur_block());
171 /* The Return statement */
173 ir_node *in[1], *store ;
177 x = new_Return(store, 1, in);
179 add_immBlock_pred(get_irg_end_block(current_ir_graph), x);
182 /* finalize the end block generated in new_ir_graph() */
183 mature_immBlock(get_irg_end_block(current_ir_graph));
191 entity *ent, *array_ent, *array_ent2;
192 type *proc_tp, *array_type, *array_type2; /* type information for the method main */
193 ir_node *x,*x1 , *r, *t, *f, *f1, *t1, *cmp, *r1, *r2;
196 printf("\nCreating an IR graph: IF_EXAMPLE...\n");
200 arch_dep_set_opts(arch_dep_none);
202 do_node_verification(NODE_VERIFICATION_REPORT);
204 typ = new_type_primitive(new_id_from_chars(PRIM_NAME, strlen(PRIM_NAME)), mode_Is);
206 typ2 = new_type_primitive(new_id_from_chars(PRIM_NAME, strlen(PRIM_NAME)), mode_Is);
208 /** The global array variable a **/
211 /** Type information for the procedure **/
212 owner = get_glob_type();
213 /* Type information for the procedure */
214 proc_tp = new_type_method(new_id_from_chars(METHODTPNAME, strlen(METHODTPNAME)), NRARGS, NRES);
215 set_method_param_type(proc_tp, 0, typ);
216 set_method_res_type(proc_tp, 0, typ);
219 /* --------------------------------------------------------------------- */
221 /* The entity for the procedure */
222 ent = new_entity (owner,
223 new_id_from_str (METHODNAME1),
225 /* The parameter and result types of the procedure. */
227 /* make type infromation for the array */
228 array_type = new_type_array(new_id_from_chars("array", 5),N_DIMS, typ);
229 array_type2 = new_type_array(new_id_from_chars("array2", 6),N_DIMS, typ2);
230 /* set the bounds for the array */
231 set_array_bounds_int(array_type, 0, L_BOUND, U_BOUND);
232 set_array_bounds_int(array_type2, 0, L_BOUND, U_BOUND);
234 /* The array is an entity of the global typ */
235 array_ent = new_entity( owner, new_id_from_str("a"), array_type);
236 array_ent2 = new_entity( owner, new_id_from_str("a2"), array_type2);
238 /** The code of the procedure **/
241 /* Generates start and end blocks and nodes, and a first, initial block */
244 irg = new_ir_graph(ent, NRLOCS);
246 /* The value position used for: */
249 /* Generate the constant and assign it to b. The assignment is resovled to a
251 set_value (i_pos, new_Const_int(0));
254 /* We know all predecessors of the block and all set_values and set_stores are
255 preformed. We can mature the block. */
256 mature_immBlock (get_irg_current_block(irg));
258 /* Generate a conditional branch */
260 add_immBlock_pred(get_irg_current_block(irg), x);
261 cmp = new_Cmp(new_Const_int(10), get_value(i_pos, mode_Is));
262 x = new_Cond(new_Proj(cmp, mode_b, Gt));
263 f = new_Proj(x, mode_X, 0);
264 t = new_Proj(x, mode_X, 1);
266 /* generate and fill the then block */
268 add_immBlock_pred (r, t);
270 ir_node *b, *c, *d, *res;
271 symconst_symbol sym, sym2;
272 c = new_Const_int(1);
273 b = new_Const_int(4);
274 ir_node *b2 = new_Const_int(12);
275 sym.entity_p = array_ent ;
276 sym2.entity_p = array_ent2 ;
277 d = new_SymConst(sym, symconst_addr_ent);
278 ir_node *d2 = new_SymConst(sym2, symconst_addr_ent);
279 res = new_Add(d, new_Mul(get_value(i_pos, mode_Is), b, mode_Is), mode_P);
280 //ir_node *res2 = new_Add(d2, get_value(i_pos, mode_Is), mode_P);
281 ir_node *res2 = new_Add(d2, new_Mul(get_value(i_pos, mode_Is), b2, mode_Is), mode_P);
282 //res2 = new_Add(res2, new_Const_int(12), mode_P);
283 set_store (new_Proj(new_Store(get_store(), res, new_Const_int(19)), mode_M, 0));
284 set_store (new_Proj(new_Store(get_store(), res2, new_Const_int(16)), mode_M, 0));
285 d = new_SymConst(sym, symconst_addr_ent);
286 res = new_Add(d, new_Mul(get_value(i_pos, mode_Is), b, mode_Is), mode_P);
287 set_store(new_Proj(new_Store(get_store(), res, new_Const_int(15)), mode_M, 0));
289 set_value(i_pos, new_Add(get_value(i_pos, mode_Is), c, mode_Is));
294 add_immBlock_pred(r1, x);
298 ir_node *b1 = new_Const_int(45);
299 add_immBlock_pred(get_irg_current_block(irg), f);
300 cmp = new_Cmp(new_Const_int(0), b1);
301 x = new_Cond (new_Proj(cmp, mode_b, Lt));
302 f1 = new_Proj (x, mode_X, 0);
303 t1 = new_Proj (x, mode_X, 1);
305 ir_node *block = new_immBlock();
306 add_immBlock_pred(block, t1);
307 b1 = new_Sub (b1, get_value(i_pos, mode_Is), mode_Is);
308 res = new_Add(d, new_Mul(get_value(i_pos, mode_Is), b, mode_Is), mode_P);
309 set_store(new_Proj(new_Store(get_store(), res, new_Const_int(19)), mode_M, 0));
310 set_value(i_pos, new_Sub(get_value(i_pos, mode_Is), c, mode_Is));
314 mature_immBlock(block);
316 add_immBlock_pred(r2, x1);
319 block = new_immBlock();
320 add_immBlock_pred(block, f1);
321 mature_immBlock(block);
322 /* The Return statement */
324 ir_node *in[1], *store ;
325 in[0] = get_value(i_pos, mode_Is);
328 x = new_Return(store, 1, in);
330 add_immBlock_pred(get_irg_end_block(irg), x);
332 /* finalize the end block generated in new_ir_graph() */
333 mature_immBlock(get_irg_end_block(irg));
336 /* -------------------------------------------------------------------------------- */
338 function_begin(owner, proc_tp, METHODNAME2, loop_forward);
340 q = new_Const_int(15);
341 ir_node *q1 = new_Const_int(13);
342 c = new_Const_int(1);
343 b = new_Const_int(4);
344 mul = new_Mul(q, get_value(i_pos, mode_Is), mode_Is);
346 res = new_Add(get_value(arr_pos, mode_P), new_Mul(get_value(i_pos, mode_Is), b, mode_Is), mode_P);
347 res = new_Add(q1, res, mode_P);
348 set_store(new_Proj(new_Store (get_store(), res, mul), mode_M, 0));
350 set_value(i_pos, new_Add(get_value(i_pos, mode_Is), c, mode_Is));
354 /* -------------------------------------------------------------------------- */
356 function_begin(owner, proc_tp, METHODNAME3, loop_backward);
358 c = new_Const_int(1);
359 b = new_Const_int(4);
360 ir_node *b3 = new_Const_int(8);
362 res = new_Add(get_value(arr_pos, mode_P), new_Mul(get_value(i_pos, mode_Is), b, mode_Is), mode_P);
363 res = new_Add(b, res, mode_P);
364 res = new_Add(b3, res, mode_P);
365 ir_node *res3 = new_Add(b3, res, mode_P);
366 res = new_Add(res3, res, mode_P);
367 set_store(new_Proj(new_Store(get_store(), res, get_value(i_pos, mode_Is)), mode_M, 0));
370 set_value(i_pos, new_Sub(get_value(i_pos, mode_Is), c, mode_Is));
374 /* -------------------------------------------------------------------------- */
376 function_begin(owner, proc_tp, METHODNAME4, loop_forward);
378 c = new_Const_int(1);
379 b = new_Const_int(4);
380 ir_node *b4 = new_Const_int(8);
382 set_value(i_pos, new_Add(get_value(i_pos, mode_Is), c, mode_Is));
383 ir_node *mul4 = new_Mul(get_value(i_pos, mode_Is), b4, mode_Is);
384 res = new_Add(mul4, get_value(arr_pos, mode_P), mode_P);
385 set_store (new_Proj (new_Store (get_store (), res,get_value(i_pos, mode_Is)),
387 res = new_Add(get_value(arr_pos, mode_P), new_Mul(get_value(i_pos, mode_Is), b, mode_Is), mode_P);
388 set_store(new_Proj(new_Store(get_store(), res, get_value(i_pos, mode_Is)), mode_M, 0));
392 /* -------------------------------------------------------------------------- */
394 function_begin(owner, proc_tp, METHODNAME5, loop_backward);
396 c = new_Const_int(1);
397 b = new_Const_int(4);
399 set_value(i_pos, new_Sub(get_value(i_pos, mode_Is), c, mode_Is));
401 ir_node * res5 = new_Add (c, b, mode_Is);
402 res = new_Add(get_value(arr_pos, mode_P), new_Mul(get_value(i_pos, mode_Is), b, mode_Is), mode_P);
403 res = new_Add(res, b, mode_P);
404 res = new_Add(res, res5, mode_P);
405 set_store(new_Proj(new_Store(get_store(), res, new_Const_int(19)), mode_M, 0));
409 /* -------------------------------------------------------------------------- */
411 function_begin(owner, proc_tp, METHODNAME6, loop_forward);
413 c = new_Const_int(1);
414 ir_node *c1 = new_Const_int(5);
415 b = new_Const_int(4);
417 set_value(i_pos, new_Sub(get_value(i_pos, mode_Is), c, mode_Is));
419 res = new_Add( get_value(arr_pos, mode_P), new_Mul(get_value(i_pos, mode_Is),
420 b, mode_Is), mode_P);
421 res = new_Sub(c1, res, mode_P);
422 res = new_Add( b, res, mode_P);
423 res = new_Add(b, res, mode_P);
424 set_store(new_Proj(new_Store(get_store(), res, new_Const_int(19)), mode_M, 0));
428 /* -------------------------------------------------------------------------- */
430 function_begin(owner, proc_tp, METHODNAME7, loop_backward);
432 c = new_Const_int(1);
433 b = new_Const_int(4);
434 ir_node *b7 = new_Const_int(19);
437 res = get_value(i_pos, mode_Is);
438 res = new_Add(res, b, mode_Is);
439 res = new_Add(res, b7, mode_Is);
440 res = new_Mul(res, b, mode_Is);
441 res = new_Add(get_value(arr_pos, mode_P), res, mode_P);
442 ir_node *res7 = new_Add( get_value(i_pos, mode_Is), b7, mode_Is);
443 set_store(new_Proj(new_Store(get_store(), res, res7), mode_M, 0));
444 set_value(i_pos, new_Sub(get_value(i_pos, mode_Is), c, mode_Is));
447 /* -------------------------------------------------------------------------- */
449 int i, n_irgs = get_irp_n_irgs();
451 printf("Done building the graph. Dumping and optimizing it.\n");
452 dump_consts_local(1);
453 turn_off_edge_labels();
454 for (i = 0; i < n_irgs; ++i) {
455 current_ir_graph = get_irp_irg(i);
456 irg_vrfy(current_ir_graph);
457 finalize_cons (current_ir_graph);
459 /* output the vcg file */
460 //dump_ir_block_graph (current_ir_graph, "-early");
461 construct_backedges(current_ir_graph);
462 dump_ir_block_graph (current_ir_graph, 0);
464 set_opt_strength_red_verbose(0);
465 set_firm_verbosity(0);
466 optimize_reassociation(current_ir_graph);
467 reduce_strength(current_ir_graph);
469 // remove_critical_cf_edges(current_ir_graph);
470 //set_opt_global_cse(1);
471 //place_code(current_ir_graph);
472 //set_opt_global_cse(0);
473 // optimize_reassociation(current_ir_graph);
475 dump_loop_tree(current_ir_graph, "");
476 dump_ir_block_graph (current_ir_graph, "-strength_reduced");
478 //printf("use xvcg to view this graph:\n");
479 //printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");