1 /* Copyright (C) 2002 by Universitaet Karlsruhe
4 * Authors: Christian Schaefer, Goetz Lindenmaier
19 * This file constructs the ir for the following pseudo-program:
39 type *proc_main; /* 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);
52 set_opt_dead_node_elimination (1);
54 #define CLASSNAME "GLOBAL_CSE_EXAMPLE"
55 #define METHODNAME "GLOBAL_CSE_EXAMPLE_main"
59 /** Type information for the procedure **/
61 owner = get_glob_type();
62 /* Type information for the procedure */
63 proc_main = new_type_method(id_from_str(METHODNAME, strlen(METHODNAME)),
65 /* The entity for the procedure */
66 ent = new_entity (owner,
67 id_from_str (METHODNAME, strlen(METHODNAME)),
69 /* The type int. This type is necessary to model the result and parameters
71 #define PRIM_NAME "int"
72 typ = new_type_primitive(id_from_str(PRIM_NAME, strlen(PRIM_NAME)), mode_Is);
73 /* The parameter and result types of the procedure. */
74 set_method_param_type(proc_main, 0, typ);
75 set_method_res_type(proc_main, 0, typ);
77 /** The code of the procedure **/
79 /* Generates start and end blocks and nodes, and a first, initial block */
81 irg = new_ir_graph (ent, NRLOCS);
83 /* The value position used for: */
87 /* Get the procedure parameter and assign it to the parameter variable
89 set_value (a_pos, new_Proj (get_irg_args(irg), mode_Is, 0));
90 /* Generate the constant and assign it to b. The assignment is resovled to a
92 set_value (b_pos, new_Const (mode_Is, new_tarval_from_long (2, mode_Is)));
93 /* We know all predecessors of the block and all set_values and set_stores are
94 preformed. We can mature the block. */
95 mature_block (get_irg_current_block(irg));
97 /* Generate a conditional branch */
98 cmp = new_Cmp(get_value(a_pos, mode_Is), get_value(b_pos, mode_Is)); /*
99 cmp = new_Cmp(new_Const (mode_Is, new_tarval_from_long (2, mode_Is)),
100 new_Const (mode_Is, new_tarval_from_long (2, mode_Is)));*/
101 x = new_Cond (new_Proj(cmp, mode_b, Eq));
102 f = new_Proj (x, mode_X, 0);
103 t = new_Proj (x, mode_X, 1);
105 /* generate and fill the then block */
108 a = new_Sub(get_value(a_pos, mode_Is),
109 new_Const (mode_Is, new_tarval_from_long (3, mode_Is)),
111 set_value (a_pos, a);
116 /* generate the else block */
119 a = new_Sub(get_value(a_pos, mode_Is),
120 new_Const (mode_Is, new_tarval_from_long (3, mode_Is)),
122 a = new_Add(a, new_Const (mode_Is, new_tarval_from_long (5, mode_Is)), mode_Is);
123 set_value (a_pos, a);
128 /* generate the fall through block and add all cfg edges */
133 /* The Return statement */
135 ir_node *in[1], *store ;
136 in[0] = get_value (a_pos, mode_Is);
139 x = new_Return (store, 1, in);
142 /* finalize the end block generated in new_ir_graph() */
143 add_in_edge (get_irg_end_block(irg), x);
144 mature_block (get_irg_end_block(irg));
146 /* verify the graph */
150 printf("Optimizing ...\n");
151 local_optimize_graph(irg);
152 dead_node_elimination(irg);
154 /* output the vcg file */
155 printf("Done building the graph. Dumping it.\n");
156 dump_ir_block_graph (irg);
157 printf("use xvcg to view this graph:\n");
158 printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");