moved access functions to more locigal places
[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 "irvrfy.h"
17 # include "irdump.h"
18 # include "firm.h"
19
20 /**
21 *  This file constructs the ir for the following pseudo-program:
22 *
23 *  int main(int a) {
24 *    int b = 2;
25 *    if ( a == b ) {
26 *       a := a - 3;
27 *    } else {
28 *       a := a - 3;
29 *       a := a + 5;
30 *    }
31 *    return a;
32 *  }
33 **/
34
35 int
36 main(void)
37 {
38   ir_graph *irg;
39   type *owner;
40   entity *ent;
41   type *proc_main; /* type information for the method main */
42   type *typ;
43   ir_node *x, *r, *t, *f, *a, *cmp;
44   int a_pos, b_pos;
45
46   printf("\nCreating an IR graph: GLOBAL_CSE_EXAMPLE...\n");
47
48   init_firm (NULL);
49
50   set_optimize(1);
51   set_opt_constant_folding(1);
52   set_opt_cse(1);
53   set_opt_global_cse(1);
54   set_opt_dead_node_elimination (1);
55
56 #define CLASSNAME "GLOBAL_CSE_EXAMPLE"
57 #define METHODNAME "GLOBAL_CSE_EXAMPLE_main"
58 #define NRARGS 1
59 #define NRES 1
60
61   /** Type information for the procedure **/
62
63   owner = get_glob_type();
64   /* Type information for the procedure */
65   proc_main = new_type_method(new_id_from_chars(METHODNAME, strlen(METHODNAME)),
66                               NRARGS, NRES);
67   /* The entity for the procedure */
68   ent = new_entity (owner,
69                     new_id_from_chars (METHODNAME, strlen(METHODNAME)),
70                     proc_main);
71   /* The type int.  This type is necessary to model the result and parameters
72      the procedure. */
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);
78
79   /** The code of the procedure **/
80
81   /* Generates start and end blocks and nodes, and a first, initial block */
82 #define NRLOCS 2
83   irg = new_ir_graph (ent, NRLOCS);
84
85   /* The value position used for: */
86   a_pos = 0;
87   b_pos = 1;
88
89   /* Get the procedure parameter and assign it to the parameter variable
90      a. */
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
93      dataflow edge. */
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));
98
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, pn_Cmp_Eq));
104   f = new_Proj (x, mode_X, 0);
105   t = new_Proj (x, mode_X, 1);
106
107   /* generate and fill the then block */
108   r = new_immBlock ();
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)),
112               mode_Is);
113   set_value (a_pos, a);
114
115   mature_immBlock (r);
116   t = new_Jmp ();
117
118   /* generate the else block */
119   r = new_immBlock ();
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)),
123               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);
126
127   mature_immBlock (r);
128   f = new_Jmp ();
129
130   /* generate the fall through block and add all cfg edges */
131   r = new_immBlock ();
132   add_immBlock_pred (r, f);
133   add_immBlock_pred (r, t);
134   mature_immBlock (r);
135   /* The Return statement */
136   {
137      ir_node *in[1], *store ;
138      in[0] = get_value (a_pos, mode_Is);
139      store = get_store();
140
141      x = new_Return (store, 1, in);
142   }
143
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));
147
148   /* verify the graph */
149   irg_vrfy(irg);
150   irg_finalize_cons (irg);
151
152   printf("Optimizing ...\n");
153   local_optimize_graph(irg);
154
155
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");
161
162   return (0);
163 }