1 /* Copyright (C) 2002 by Universitaet Karlsruhe
2 ** All rights reserved.
4 ** Authors: Christian Schaefer, Goetz Lindenmaier
15 *** This file constructs the ir for the following pseudo-program:
35 type *proc_main; /* type information for the method main */
37 ir_node *x, *r, *t, *f, *a, *cmp;
40 printf("\nCreating an IR graph: GLOBAL_CSE_EXAMPLE...\n");
45 set_opt_constant_folding(1);
47 set_opt_global_cse(1);
48 set_opt_dead_node_elimination (1);
50 #define CLASSNAME "GLOBAL_CSE_EXAMPLE"
51 #define METHODNAME "main"
55 /** Type information for the procedure **/
57 owner = get_glob_type();
58 /* Type information for the procedure */
59 proc_main = new_type_method(id_from_str(METHODNAME, strlen(METHODNAME)),
61 /* The entity for the procedure */
62 ent = new_entity (owner,
63 id_from_str (METHODNAME, strlen(METHODNAME)),
65 /* The type int. This type is necessary to model the result and parameters
67 #define PRIM_NAME "int"
68 typ = new_type_primitive(id_from_str(PRIM_NAME, strlen(PRIM_NAME)), mode_i);
69 /* The parameter and result types of the procedure. */
70 set_method_param_type(proc_main, 0, typ);
71 set_method_res_type(proc_main, 0, typ);
73 /** The code of the procedure **/
75 /* Generates start and end blocks and nodes, and a first, initial block */
77 irg = new_ir_graph (ent, NRLOCS);
79 /* The value position used for: */
83 /* Get the procedure parameter and assign it to the parameter variable
85 set_value (a_pos, new_Proj (get_irg_args(irg), mode_i, 0));
86 /* Generate the constant and assign it to b. The assignment is resovled to a
88 set_value (b_pos, new_Const (mode_i, tarval_from_long (mode_i, 2)));
89 /* We know all predecessors of the block and all set_values and set_stores are
90 preformed. We can mature the block. */
91 mature_block (get_irg_current_block(irg));
93 /* Generate a conditional branch */
94 cmp = new_Cmp(get_value(a_pos, mode_i), get_value(b_pos, mode_i)); /*
95 cmp = new_Cmp(new_Const (mode_i, tarval_from_long (mode_i, 2)),
96 new_Const (mode_i, tarval_from_long (mode_i, 2)));*/
97 x = new_Cond (new_Proj(cmp, mode_b, Eq));
98 f = new_Proj (x, mode_X, 0);
99 t = new_Proj (x, mode_X, 1);
101 /* generate and fill the then block */
104 a = new_Sub(get_value(a_pos, mode_i),
105 new_Const (mode_i, tarval_from_long (mode_i, 3)),
107 set_value (a_pos, a);
112 /* generate the else block */
115 a = new_Sub(get_value(a_pos, mode_i),
116 new_Const (mode_i, tarval_from_long (mode_i, 3)),
118 a = new_Add(a, new_Const (mode_i, tarval_from_long (mode_i, 5)), mode_i);
119 set_value (a_pos, a);
124 /* generate the fall through block and add all cfg edges */
129 /* The Return statement */
131 ir_node *in[1], *store ;
132 in[0] = get_value (a_pos, mode_i);
135 x = new_Return (store, 1, in);
138 /* finalize the end block generated in new_ir_graph() */
139 add_in_edge (get_irg_end_block(irg), x);
140 mature_block (get_irg_end_block(irg));
142 /* verify the graph */
146 printf("Optimizing ...\n");
147 local_optimize_graph(irg);
148 dead_node_elimination(irg);
150 /* output the vcg file */
151 printf("Done building the graph. Dumping it.\n");
152 dump_ir_block_graph (irg);
153 printf("use xvcg to view this graph:\n");
154 printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");