New directory: ana for analyses. Adapted configure/makefiles
[libfirm] / testprograms / global_cse.c
1 /* Copyright (C) 2002 by Universitaet Karlsruhe
2 ** All rights reserved.
3 **
4 ** Authors: Christian Schaefer, Goetz Lindenmaier
5 **
6 ** testprogram.
7 */
8
9 /* $Id$ */
10
11 # include "irdump.h"
12 # include "firm.h"
13
14 /**
15 ***  This file constructs the ir for the following pseudo-program:
16 ***
17 ***  int main(int a) {
18 ***    int b = 2;
19 ***    if ( a == b ) {
20 ***       a := a - 3;
21 ***    } else {
22 ***       a := a - 3;
23 ***       a := a + 5;
24 ***    }
25 ***    return a;
26 ***  }
27 **/
28
29 int
30 main(void)
31 {
32   ir_graph *irg;
33   type *owner;
34   entity *ent;
35   type *proc_main; /* type information for the method main */
36   type *typ;
37   ir_node *x, *r, *t, *f, *a, *cmp;
38   int a_pos, b_pos;
39
40   printf("\nCreating an IR graph: GLOBAL_CSE_EXAMPLE...\n");
41
42   init_firm ();
43
44   set_optimize(1);
45   set_opt_constant_folding(1);
46   set_opt_cse(1);
47   set_opt_global_cse(1);
48   set_opt_dead_node_elimination (1);
49
50 #define CLASSNAME "GLOBAL_CSE_EXAMPLE"
51 #define METHODNAME "main"
52 #define NRARGS 1
53 #define NRES 1
54
55   /** Type information for the procedure **/
56
57   owner = get_glob_type();
58   /* Type information for the procedure */
59   proc_main = new_type_method(id_from_str(METHODNAME, strlen(METHODNAME)),
60                               NRARGS, NRES);
61   /* The entity for the procedure */
62   ent = new_entity (owner,
63                     id_from_str (METHODNAME, strlen(METHODNAME)),
64                     proc_main);
65   /* The type int.  This type is necessary to model the result and parameters
66      the procedure. */
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);
72
73   /** The code of the procedure **/
74
75   /* Generates start and end blocks and nodes, and a first, initial block */
76 #define NRLOCS 2
77   irg = new_ir_graph (ent, NRLOCS);
78
79   /* The value position used for: */
80   a_pos = 0;
81   b_pos = 1;
82
83   /* Get the procedure parameter and assign it to the parameter variable
84      a. */
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
87      dataflow edge. */
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));
92
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);
100
101   /* generate and fill the then block */
102   r = new_immBlock ();
103   add_in_edge (r, t);
104   a = new_Sub(get_value(a_pos, mode_i),
105               new_Const (mode_i, tarval_from_long (mode_i, 3)),
106               mode_i);
107   set_value (a_pos, a);
108
109   mature_block (r);
110   t = new_Jmp ();
111
112   /* generate the else block */
113   r = new_immBlock ();
114   add_in_edge (r, f);
115   a = new_Sub(get_value(a_pos, mode_i),
116               new_Const (mode_i, tarval_from_long (mode_i, 3)),
117               mode_i);
118   a = new_Add(a, new_Const (mode_i, tarval_from_long (mode_i, 5)), mode_i);
119   set_value (a_pos, a);
120
121   mature_block (r);
122   f = new_Jmp ();
123
124   /* generate the fall through block and add all cfg edges */
125   r = new_immBlock ();
126   add_in_edge (r, f);
127   add_in_edge (r, t);
128   mature_block (r);
129   /* The Return statement */
130   {
131      ir_node *in[1], *store ;
132      in[0] = get_value (a_pos, mode_i);
133      store = get_store();
134
135      x = new_Return (store, 1, in);
136   }
137
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));
141
142   /* verify the graph */
143   irg_vrfy(irg);
144   finalize_cons (irg);
145
146   printf("Optimizing ...\n");
147   local_optimize_graph(irg);
148   dead_node_elimination(irg);
149
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");
155
156   return (0);
157 }