fixed warnings
[libfirm] / testprograms / irr_loop_example.c
1 /*
2  * Project:     libFIRM
3  * File name:   testprograms/irr_loop_example.c
4  * Purpose:     Test Phi construction with irregular control flow.
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 a control flow of following shape:
22  *
23  * method loop1:
24  *
25  *         firstBlock
26  *          /      \
27  *         /        \
28  *       |/_        _\|
29  *            ---->
30  * LoopBlock1       LoopBlock2
31  *            <----
32  *        \              /
33  *         \            /
34  *         _\|        |/_
35  *           nextBlock
36  *
37  * method loop2:
38  *
39  *         scndBlock  <---------- firstBlock
40  *          /      \                  \
41  *         /        \                  \
42  *       |/_        _\|                _\|
43  *            ---->              ---->
44  * LoopBlock1       LoopBlock2 1       LoopBlock3
45  *            <----              <----
46  *        \                             /
47  *         \                           /
48  *         _\|                        /
49  *           nextBlock   <---------- /
50  *
51  *
52  **/
53
54 int main(int argc, char **argv)
55 {
56   ir_graph *irg;        /* this variable contains the irgraph */
57   type     *owner;      /* the class in which this method is defined */
58   type     *proc_tp;  /* typeinformation for the method main */
59   entity   *ent;        /* represents this method as entity of owner */
60   ir_node  *expr, *c1, *c2, *c3, *c4, *c5, *cond, *f, *t, *f2, *loopBlock1, *f_l1, *t_l1,
61            *loopBlock2, *f_l2, *t_l2, *loopBlock3, *f_l3, *t_l3, *x;
62   int       i;
63
64
65   /* init library */
66   init_firm (NULL);
67   set_opt_constant_folding (0);  /* so that the stupid tests are not optimized. */
68   set_opt_cse(1);
69   set_opt_dead_node_elimination(1);
70   turn_off_edge_labels();
71
72   /* FIRM was designed for oo languages where all methods belong to a class.
73    * For imperative languages like C we view a file as a large class containing
74    * all functions as methods in this file.
75    * Therefore we define a class "empty" according to the file name
76    * with a method main as an entity.
77    */
78 #define CLASSNAME "IRR_LOOP"
79 #define METHOD_TP_NAME "METH_TP_NOARG_NORES"
80 #define NRARGS 0
81 #define NRES 0
82 #define NUM_OF_LOCAL_VARS 0
83
84   owner = new_type_class (new_id_from_str (CLASSNAME));
85   printf("\nCreating testprogram: %s...\n", CLASSNAME);
86   proc_tp = new_type_method(new_id_from_str(METHOD_TP_NAME), NRARGS, NRES);
87
88   /* --- method loop1 ------------------------------------------------------ */
89
90 #define METHODNAME "loop1"
91
92   ent = new_entity (owner, new_id_from_str (METHODNAME), proc_tp);
93   get_entity_ld_name(ent); /* To enforce name mangling for vcg graph name */
94
95   irg = new_ir_graph (ent, NUM_OF_LOCAL_VARS);
96
97   /* to make three conditionals  */
98   expr = new_Const (mode_Is, new_tarval_from_long (0, mode_Is));
99   c1 = new_Const (mode_Is, new_tarval_from_long (1, mode_Is));
100   c2 = new_Const (mode_Is, new_tarval_from_long (2, mode_Is));
101   c3 = new_Const (mode_Is, new_tarval_from_long (2, mode_Is));
102
103   cond = new_Cond(new_Proj(new_Cmp(expr, c1), mode_b, pn_Cmp_Eq));
104   f = new_Proj(cond, mode_X, 0);
105   t = new_Proj(cond, mode_X, 1);
106   mature_immBlock(get_irg_current_block(irg));
107
108   loopBlock1 = new_immBlock();
109   add_immBlock_pred(loopBlock1, t);
110   cond = new_Cond(new_Proj(new_Cmp(expr, c2), mode_b, pn_Cmp_Eq));
111   f_l1 = new_Proj(cond, mode_X, 0);
112   t_l1 = new_Proj(cond, mode_X, 1);
113
114   loopBlock2 = new_immBlock();
115   add_immBlock_pred(loopBlock2, f);
116   cond = new_Cond(new_Proj(new_Cmp(expr, c3), mode_b, pn_Cmp_Eq));
117   f_l2 = new_Proj(cond, mode_X, 0);
118   t_l2 = new_Proj(cond, mode_X, 1);
119
120   add_immBlock_pred(loopBlock1, t_l2);
121   add_immBlock_pred(loopBlock2, t_l1);
122   mature_immBlock(loopBlock1);
123   mature_immBlock(loopBlock2);
124
125   new_immBlock();
126   add_immBlock_pred(get_irg_current_block(irg), f_l2);
127   add_immBlock_pred(get_irg_current_block(irg), f_l1);
128   x = new_Return (get_store(), 0, NULL);
129   mature_immBlock (get_irg_current_block(irg));
130
131   add_immBlock_pred (get_irg_end_block(irg), x);
132   mature_immBlock (get_irg_end_block(irg));
133
134   irg_finalize_cons (irg);
135
136   /* --- method loop2 ------------------------------------------------------ */
137
138 #undef METHODNAME
139 #define METHODNAME "loop2"
140
141   ent = new_entity (owner, new_id_from_str (METHODNAME), proc_tp);
142   get_entity_ld_name(ent); /* To enforce name mangling for vcg graph name */
143
144   irg = new_ir_graph (ent, NUM_OF_LOCAL_VARS);
145
146   /* to make several conditionals  */
147   expr = new_Const (mode_Is, new_tarval_from_long (0, mode_Is));
148   c1   = new_Const (mode_Is, new_tarval_from_long (1, mode_Is));
149   c2   = new_Const (mode_Is, new_tarval_from_long (2, mode_Is));
150   c3   = new_Const (mode_Is, new_tarval_from_long (3, mode_Is));
151   c4   = new_Const (mode_Is, new_tarval_from_long (4, mode_Is));
152   c5   = new_Const (mode_Is, new_tarval_from_long (5, mode_Is));
153
154   cond = new_Cond(new_Proj(new_Cmp(expr, c1), mode_b, pn_Cmp_Eq));
155   f2 = new_Proj(cond, mode_X, 0);
156   t = new_Proj(cond, mode_X, 1);
157   mature_immBlock(get_irg_current_block(irg));
158
159   new_immBlock();
160   add_immBlock_pred(get_irg_current_block(irg), t);
161   cond = new_Cond(new_Proj(new_Cmp(expr, c5), mode_b, pn_Cmp_Eq));
162   f = new_Proj(cond, mode_X, 0);
163   t = new_Proj(cond, mode_X, 1);
164   mature_immBlock(get_irg_current_block(irg));
165
166   loopBlock1 = new_immBlock();
167   add_immBlock_pred(loopBlock1, t);
168   cond = new_Cond(new_Proj(new_Cmp(expr, c2), mode_b, pn_Cmp_Eq));
169   f_l1 = new_Proj(cond, mode_X, 0);
170   t_l1 = new_Proj(cond, mode_X, 1);
171
172   loopBlock2 = new_immBlock();
173   add_immBlock_pred(loopBlock2, f);
174   cond = new_Cond(new_Proj(new_Cmp(expr, c3), mode_b, pn_Cmp_Eq));
175   f_l2 = new_Proj(cond, mode_X, 0);
176   t_l2 = new_Proj(cond, mode_X, 1);
177
178   loopBlock3 = new_immBlock();
179   add_immBlock_pred(loopBlock3, f2);
180   cond = new_Cond(new_Proj(new_Cmp(expr, c4), mode_b, pn_Cmp_Eq));
181   f_l3 = new_Proj(cond, mode_X, 0);
182   t_l3 = new_Proj(cond, mode_X, 1);
183
184   add_immBlock_pred(loopBlock1, t_l2);
185   add_immBlock_pred(loopBlock2, t_l1);
186   add_immBlock_pred(loopBlock3, f_l2);
187   add_immBlock_pred(loopBlock2, t_l3);
188   mature_immBlock(loopBlock1);
189   mature_immBlock(loopBlock2);
190   mature_immBlock(loopBlock3);
191
192   new_immBlock();
193   add_immBlock_pred(get_irg_current_block(irg), f_l1);
194   add_immBlock_pred(get_irg_current_block(irg), f_l3);
195   x = new_Return (get_store(), 0, NULL);
196   mature_immBlock (get_irg_current_block(irg));
197
198   add_immBlock_pred (get_irg_end_block(irg), x);
199   mature_immBlock (get_irg_end_block(irg));
200
201   irg_finalize_cons (irg);
202
203   /* --- more ...  ------------------------------------------------------ */
204
205   printf("Optimizing ...\n");
206   //dead_node_elimination(irg);
207
208   for (i = 0; i < get_irp_n_irgs(); ++i) {
209     current_ir_graph = get_irp_irg(i);
210     /* verify the graph */
211     irg_vrfy(current_ir_graph);
212     construct_cf_backedges(current_ir_graph);
213     dump_loop_tree(current_ir_graph, "");
214
215     printf("Dumping the graph and a control flow graph.\n");
216     dump_ir_block_graph(current_ir_graph, "");
217     dump_cfg(current_ir_graph, "");
218     printf("Use xvcg to view these graphs:\n");
219     printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");
220
221   }
222   /* Error for loop2 */
223   compute_execution_frequency(get_irp_irg(0), 10, 0.001);
224   dump_interval_graph(get_irp_irg(0), "");
225
226   return (0);
227 }