- some cosmetic changes
[libfirm] / testprograms / global_cse.c
1 /*
2  * Project:     libFIRM
3  * File name:   testprograms/global_cse.c
4  * Purpose:     Test global cse.
5  * Author:      Christian Schaefer, Goetz Lindenmaier
6  * Modified by:
7  * Created:
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 1999-2003 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13 #include <stdio.h>
14 #include <string.h>
15
16 #include <libfirm/firm.h>
17
18 /**
19 *  This file constructs the ir for the following pseudo-program:
20 *
21 *  int main(int a) {
22 *    int b = 2;
23 *    if ( a == b ) {
24 *       a := a - 3;
25 *    } else {
26 *       a := a - 3;
27 *       a := a + 5;
28 *    }
29 *    return a;
30 *  }
31 **/
32
33 int
34 main(void)
35 {
36   ir_graph *irg;
37   ir_type *owner;
38   ir_entity *ent;
39   ir_type *proc_main; /* ir_type information for the method main */
40   ir_type *typ;
41   ir_node *x, *r, *t, *f, *a, *cmp;
42   int a_pos, b_pos;
43
44   printf("\nCreating an IR graph: GLOBAL_CSE_EXAMPLE...\n");
45
46   init_firm(NULL);
47
48   set_optimize(1);
49   set_opt_constant_folding(1);
50   set_opt_cse(1);
51   set_opt_global_cse(1);
52
53 #define CLASSNAME "GLOBAL_CSE_EXAMPLE"
54 #define METHODNAME "GLOBAL_CSE_EXAMPLE_main"
55 #define NRARGS 1
56 #define NRES 1
57
58   /** Type information for the procedure **/
59
60   owner = get_glob_type();
61   /* Type information for the procedure */
62   proc_main = new_type_method(new_id_from_chars(METHODNAME, strlen(METHODNAME)),
63                               NRARGS, NRES);
64   /* The ir_entity for the procedure */
65   ent = new_entity(owner,
66                    new_id_from_chars(METHODNAME, strlen(METHODNAME)),
67                    proc_main);
68   /* The ir_type int.  This ir_type is necessary to model the result and parameters
69      the procedure. */
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);
75
76   /** The code of the procedure **/
77
78   /* Generates start and end blocks and nodes, and a first, initial block */
79 #define NRLOCS 2
80   irg = new_ir_graph(ent, NRLOCS);
81
82   /* The value position used for: */
83   a_pos = 0;
84   b_pos = 1;
85
86   /* Get the procedure parameter and assign it to the parameter variable
87      a. */
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
90      dataflow edge. */
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));
95
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);
103
104   /* generate and fill the then block */
105   r = new_immBlock();
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)),
109               mode_Is);
110   set_value(a_pos, a);
111
112   mature_immBlock(r);
113   t = new_Jmp();
114
115   /* generate the else block */
116   r = new_immBlock();
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)),
120               mode_Is);
121   a = new_Add(a, new_Const(mode_Is, new_tarval_from_long(5, mode_Is)), mode_Is);
122   set_value(a_pos, a);
123
124   mature_immBlock(r);
125   f = new_Jmp();
126
127   /* generate the fall through block and add all cfg edges */
128   r = new_immBlock();
129   add_immBlock_pred(r, f);
130   add_immBlock_pred(r, t);
131   mature_immBlock(r);
132   /* The Return statement */
133   {
134      ir_node *in[1], *store ;
135      in[0] = get_value(a_pos, mode_Is);
136      store = get_store();
137
138      x = new_Return(store, 1, in);
139   }
140
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));
144
145   /* verify the graph */
146   irg_vrfy(irg);
147   irg_finalize_cons(irg);
148
149   printf("Optimizing ...\n");
150   local_optimize_graph(irg);
151
152
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");
158
159   return 0;
160 }