3 * File name: testprograms/global_cse.c
4 * Purpose: Test global cse.
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 the ir for the following pseudo-program:
39 ir_type *proc_main; /* ir_type information for the method main */
41 ir_node *x, *r, *t, *f, *a, *cmp;
44 printf("\nCreating an IR graph: GLOBAL_CSE_EXAMPLE...\n");
49 set_opt_constant_folding(1);
51 set_opt_global_cse(1);
53 #define CLASSNAME "GLOBAL_CSE_EXAMPLE"
54 #define METHODNAME "GLOBAL_CSE_EXAMPLE_main"
58 /** Type information for the procedure **/
60 owner = get_glob_type();
61 /* Type information for the procedure */
62 proc_main = new_type_method(new_id_from_chars(METHODNAME, strlen(METHODNAME)),
64 /* The ir_entity for the procedure */
65 ent = new_entity(owner,
66 new_id_from_chars(METHODNAME, strlen(METHODNAME)),
68 /* The ir_type int. This ir_type is necessary to model the result and parameters
70 #define PRIM_NAME "int"
71 typ = new_type_primitive(new_id_from_chars(PRIM_NAME, strlen(PRIM_NAME)), mode_Is);
72 /* The parameter and result types of the procedure. */
73 set_method_param_type(proc_main, 0, typ);
74 set_method_res_type(proc_main, 0, typ);
76 /** The code of the procedure **/
78 /* Generates start and end blocks and nodes, and a first, initial block */
80 irg = new_ir_graph(ent, NRLOCS);
82 /* The value position used for: */
86 /* Get the procedure parameter and assign it to the parameter variable
88 set_value(a_pos, new_Proj(get_irg_args(irg), mode_Is, 0));
89 /* Generate the constant and assign it to b. The assignment is resovled to a
91 set_value(b_pos, new_Const(mode_Is, new_tarval_from_long(2, mode_Is)));
92 /* We know all predecessors of the block and all set_values and set_stores are
93 preformed. We can mature the block. */
94 mature_immBlock(get_irg_current_block(irg));
96 /* Generate a conditional branch */
97 cmp = new_Cmp(get_value(a_pos, mode_Is), get_value(b_pos, mode_Is)); /*
98 cmp = new_Cmp(new_Const(mode_Is, new_tarval_from_long(2, mode_Is)),
99 new_Const(mode_Is, new_tarval_from_long(2, mode_Is)));*/
100 x = new_Cond(new_Proj(cmp, mode_b, pn_Cmp_Eq));
101 f = new_Proj(x, mode_X, pn_Cond_false);
102 t = new_Proj(x, mode_X, pn_Cond_true);
104 /* generate and fill the then block */
106 add_immBlock_pred(r, t);
107 a = new_Sub(get_value(a_pos, mode_Is),
108 new_Const(mode_Is, new_tarval_from_long(3, mode_Is)),
115 /* generate the else block */
117 add_immBlock_pred(r, f);
118 a = new_Sub(get_value(a_pos, mode_Is),
119 new_Const(mode_Is, new_tarval_from_long(3, mode_Is)),
121 a = new_Add(a, new_Const(mode_Is, new_tarval_from_long(5, mode_Is)), mode_Is);
127 /* generate the fall through block and add all cfg edges */
129 add_immBlock_pred(r, f);
130 add_immBlock_pred(r, t);
132 /* The Return statement */
134 ir_node *in[1], *store ;
135 in[0] = get_value(a_pos, mode_Is);
138 x = new_Return(store, 1, in);
141 /* finalize the end block generated in new_ir_graph() */
142 add_immBlock_pred(get_irg_end_block(irg), x);
143 mature_immBlock(get_irg_end_block(irg));
145 /* verify the graph */
147 irg_finalize_cons(irg);
149 printf("Optimizing ...\n");
150 local_optimize_graph(irg);
153 /* output the vcg file */
154 printf("Done building the graph. Dumping it.\n");
155 dump_ir_block_graph(irg, 0);
156 printf("Use ycomp to view this graph:\n");
157 printf("ycomp GRAPHNAME\n\n");