init_firm() takes now a parameter, the old behavior is enfrced with NULL.
[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 <stdio.h>
12 # include <string.h>
13
14 # include "irvrfy.h"
15 # include "irdump.h"
16 # include "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   type *owner;
38   entity *ent;
39   type *proc_main; /* type information for the method main */
40   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   set_opt_dead_node_elimination (1);
53
54 #define CLASSNAME "GLOBAL_CSE_EXAMPLE"
55 #define METHODNAME "GLOBAL_CSE_EXAMPLE_main"
56 #define NRARGS 1
57 #define NRES 1
58
59   /** Type information for the procedure **/
60
61   owner = get_glob_type();
62   /* Type information for the procedure */
63   proc_main = new_type_method(id_from_str(METHODNAME, strlen(METHODNAME)),
64                               NRARGS, NRES);
65   /* The entity for the procedure */
66   ent = new_entity (owner,
67                     id_from_str (METHODNAME, strlen(METHODNAME)),
68                     proc_main);
69   /* The type int.  This type is necessary to model the result and parameters
70      the procedure. */
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);
76
77   /** The code of the procedure **/
78
79   /* Generates start and end blocks and nodes, and a first, initial block */
80 #define NRLOCS 2
81   irg = new_ir_graph (ent, NRLOCS);
82
83   /* The value position used for: */
84   a_pos = 0;
85   b_pos = 1;
86
87   /* Get the procedure parameter and assign it to the parameter variable
88      a. */
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
91      dataflow edge. */
92   set_value (b_pos, new_Const (mode_Is, tarval_from_long (mode_Is, 2)));
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));
96
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, tarval_from_long (mode_Is, 2)),
100                 new_Const (mode_Is, tarval_from_long (mode_Is, 2)));*/
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);
104
105   /* generate and fill the then block */
106   r = new_immBlock ();
107   add_in_edge (r, t);
108   a = new_Sub(get_value(a_pos, mode_Is),
109               new_Const (mode_Is, tarval_from_long (mode_Is, 3)),
110               mode_Is);
111   set_value (a_pos, a);
112
113   mature_block (r);
114   t = new_Jmp ();
115
116   /* generate the else block */
117   r = new_immBlock ();
118   add_in_edge (r, f);
119   a = new_Sub(get_value(a_pos, mode_Is),
120               new_Const (mode_Is, tarval_from_long (mode_Is, 3)),
121               mode_Is);
122   a = new_Add(a, new_Const (mode_Is, tarval_from_long (mode_Is, 5)), mode_Is);
123   set_value (a_pos, a);
124
125   mature_block (r);
126   f = new_Jmp ();
127
128   /* generate the fall through block and add all cfg edges */
129   r = new_immBlock ();
130   add_in_edge (r, f);
131   add_in_edge (r, t);
132   mature_block (r);
133   /* The Return statement */
134   {
135      ir_node *in[1], *store ;
136      in[0] = get_value (a_pos, mode_Is);
137      store = get_store();
138
139      x = new_Return (store, 1, in);
140   }
141
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));
145
146   /* verify the graph */
147   irg_vrfy(irg);
148   finalize_cons (irg);
149
150   printf("Optimizing ...\n");
151   local_optimize_graph(irg);
152   dead_node_elimination(irg);
153
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");
159
160   return (0);
161 }