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.
21 * This file constructs the ir for the following pseudo-program:
41 type *proc_main; /* type information for the method main */
43 ir_node *x, *r, *t, *f, *a, *cmp;
46 printf("\nCreating an IR graph: GLOBAL_CSE_EXAMPLE...\n");
51 set_opt_constant_folding(1);
53 set_opt_global_cse(1);
54 set_opt_dead_node_elimination (1);
56 #define CLASSNAME "GLOBAL_CSE_EXAMPLE"
57 #define METHODNAME "GLOBAL_CSE_EXAMPLE_main"
61 /** Type information for the procedure **/
63 owner = get_glob_type();
64 /* Type information for the procedure */
65 proc_main = new_type_method(new_id_from_chars(METHODNAME, strlen(METHODNAME)),
67 /* The entity for the procedure */
68 ent = new_entity (owner,
69 new_id_from_chars (METHODNAME, strlen(METHODNAME)),
71 /* The type int. This type is necessary to model the result and parameters
73 #define PRIM_NAME "int"
74 typ = new_type_primitive(new_id_from_chars(PRIM_NAME, strlen(PRIM_NAME)), mode_Is);
75 /* The parameter and result types of the procedure. */
76 set_method_param_type(proc_main, 0, typ);
77 set_method_res_type(proc_main, 0, typ);
79 /** The code of the procedure **/
81 /* Generates start and end blocks and nodes, and a first, initial block */
83 irg = new_ir_graph (ent, NRLOCS);
85 /* The value position used for: */
89 /* Get the procedure parameter and assign it to the parameter variable
91 set_value (a_pos, new_Proj (get_irg_args(irg), mode_Is, 0));
92 /* Generate the constant and assign it to b. The assignment is resovled to a
94 set_value (b_pos, new_Const (mode_Is, new_tarval_from_long (2, mode_Is)));
95 /* We know all predecessors of the block and all set_values and set_stores are
96 preformed. We can mature the block. */
97 mature_immBlock (get_irg_current_block(irg));
99 /* Generate a conditional branch */
100 cmp = new_Cmp(get_value(a_pos, mode_Is), get_value(b_pos, mode_Is)); /*
101 cmp = new_Cmp(new_Const (mode_Is, new_tarval_from_long (2, mode_Is)),
102 new_Const (mode_Is, new_tarval_from_long (2, mode_Is)));*/
103 x = new_Cond (new_Proj(cmp, mode_b, Eq));
104 f = new_Proj (x, mode_X, 0);
105 t = new_Proj (x, mode_X, 1);
107 /* generate and fill the then block */
109 add_immBlock_pred (r, t);
110 a = new_Sub(get_value(a_pos, mode_Is),
111 new_Const (mode_Is, new_tarval_from_long (3, mode_Is)),
113 set_value (a_pos, a);
118 /* generate the else block */
120 add_immBlock_pred (r, f);
121 a = new_Sub(get_value(a_pos, mode_Is),
122 new_Const (mode_Is, new_tarval_from_long (3, mode_Is)),
124 a = new_Add(a, new_Const (mode_Is, new_tarval_from_long (5, mode_Is)), mode_Is);
125 set_value (a_pos, a);
130 /* generate the fall through block and add all cfg edges */
132 add_immBlock_pred (r, f);
133 add_immBlock_pred (r, t);
135 /* The Return statement */
137 ir_node *in[1], *store ;
138 in[0] = get_value (a_pos, mode_Is);
141 x = new_Return (store, 1, in);
144 /* finalize the end block generated in new_ir_graph() */
145 add_immBlock_pred (get_irg_end_block(irg), x);
146 mature_immBlock (get_irg_end_block(irg));
148 /* verify the graph */
152 printf("Optimizing ...\n");
153 local_optimize_graph(irg);
154 dead_node_elimination(irg);
156 /* output the vcg file */
157 printf("Done building the graph. Dumping it.\n");
158 dump_ir_block_graph (irg, 0);
159 printf("use xvcg to view this graph:\n");
160 printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");