- add support for statistics and merge debug info
[libfirm] / testprograms / dead_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 <libfirm/firm.h>
17
18 /**
19  *  This file constructs a control flow with an unreachable
20  *  loop _and_ an unreachable endless loop.  This looks like:
21  *
22  *    LoopBlock2                          LoopBlock2'
23  *     |    /|\                            |    /|\
24  *     |     |                             |     |
25  *    \|/    |                            \|/    |
26  *    LoopBlock1    StartBlock            LoopBlock1'
27  *        \              /
28  *         \            /
29  *         _\|        |/_
30  *          ReturnBlock
31  *              |
32  *              |
33  *             \|/
34  *           nextBlock
35  *
36  *
37  **/
38
39 int main(void)
40 {
41   ir_graph *irg;        /* this variable contains the irgraph */
42   ir_type     *prim_t_int;
43   ir_type     *owner;      /* the class in which this method is defined */
44   ir_type     *proc_main;  /* typeinformation for the method main */
45   ir_entity   *ent;        /* represents this method as ir_entity of owner */
46   ir_node  *returnBlock, *loopBlock1, *loopBlock2, *x, *c1, *c2, *t, *f;
47
48
49   /* init library */
50   init_firm(NULL);
51   /*set_opt_normalize(0); */
52   set_opt_constant_folding(0);  /* so that the stupid tests are not optimized. */
53   set_opt_cse(1);
54
55   /* FIRM was designed for oo languages where all methods belong to a class.
56    * For imperative languages like C we view a file as a large class containing
57    * all functions as methods in this file.
58    * Therefore we define a class "empty" according to the file name
59    * with a method main as an ir_entity.
60    */
61 #define CLASSNAME "DEAD_LOOP"
62 #define METHODNAME "main"
63 #define NRARGS 1
64 #define NRES 0
65   printf("\nCreating an IR graph: %s...\n", CLASSNAME);
66
67   prim_t_int = new_type_primitive(new_id_from_str("int"), mode_Is);
68
69   owner = new_type_class(new_id_from_str(CLASSNAME));
70   proc_main = new_type_method(new_id_from_str(METHODNAME), NRARGS, NRES);
71   set_method_param_type(proc_main, 0, prim_t_int);
72   ent = new_entity(owner, new_id_from_str(METHODNAME), proc_main);
73   get_entity_ld_name(ent); /* To enforce name mangling for vcg graph name */
74
75 #define NUM_OF_LOCAL_VARS 0
76
77   irg = new_ir_graph(ent, NUM_OF_LOCAL_VARS);
78
79   returnBlock = get_irg_current_block(irg);
80
81   /* Make some real stupid stuff: a data loop (without Phi). */
82   {
83    ir_node *a, *b, *c, *in[2];
84    add_immBlock_pred(get_cur_block(), new_Bad());
85    a = new_Const(mode_Is, new_tarval_from_long(1, mode_Is));
86    b = new_Const(mode_Is, new_tarval_from_long(2, mode_Is));
87    c = new_Add(a, b, mode_Is);
88    b = new_Sub(c, b, mode_Is);
89    in[0] = b;
90    in[1] = new_Bad();
91    a = new_Phi(2, in, mode_Is);
92    set_Add_left(c, a);
93    /* add_End_keepalive(get_irg_end(irg), a); */
94    set_nodes_block(c, new_Bad());
95    set_nodes_block(a, new_Bad());
96   }
97
98   /* Make the unreachable loop */
99   loopBlock1 = new_immBlock();
100   loopBlock2 = new_immBlock();
101   x = new_Jmp();
102   add_immBlock_pred(loopBlock1, x);
103   mature_immBlock(loopBlock1);
104
105   set_cur_block(loopBlock1);
106   c1 = new_Const(mode_Is, new_tarval_from_long(1, mode_Is));
107   c2 = new_Proj(get_irg_args(irg), mode_Is, 0);
108   x =  new_Cond(new_Proj(new_Cmp(c1, c2), mode_b, pn_Cmp_Eq));
109   f = new_Proj(x, mode_X, pn_Cond_false);
110   t = new_Proj(x, mode_X, pn_Cond_true);
111   add_immBlock_pred(loopBlock2, t);
112   add_immBlock_pred(returnBlock, f);
113   mature_immBlock(loopBlock2);
114
115   /* Make the unreachable, endless loop */
116   loopBlock1 = new_immBlock();
117   loopBlock2 = new_immBlock();
118   x = new_Jmp();
119   add_immBlock_pred(loopBlock1, x);
120   mature_immBlock(loopBlock1);
121
122   set_cur_block(loopBlock1);
123   x = new_Jmp();
124   add_immBlock_pred(loopBlock2, x);
125   add_End_keepalive(get_irg_end(irg), loopBlock1);
126   mature_immBlock(loopBlock2);
127
128   /* Make the return block */
129   set_cur_block(returnBlock);
130   x = new_Return(get_store(), 0, NULL);
131   mature_immBlock(get_irg_current_block(irg));
132
133   add_immBlock_pred(get_irg_end_block(irg), x);
134   mature_immBlock(get_irg_end_block(irg));
135
136   irg_finalize_cons(irg);
137
138 #if 0
139   printf("Optimizing ...\n");
140   dead_node_elimination(irg);
141 #endif
142
143   /* verify the graph */
144   irg_vrfy(irg);
145
146   printf("Dumping the graph and a control flow graph.\n");
147   turn_off_edge_labels();
148   dump_keepalive_edges(1);
149   dump_consts_local(0);
150   dump_ir_graph(irg, "-cfg");
151   dump_ir_block_graph(irg, "-cfg");
152   dump_cfg(irg, "-cfg");
153
154   printf("Running analyses.\n");
155   compute_irg_outs(irg);
156   compute_doms(irg);
157   construct_backedges(irg);
158
159   printf("Dumping the graph with analyses information.\n");
160
161   dump_out_edges(0);
162   dump_dominator_information(0);
163   dump_loop_information(0);
164   dump_backedge_information(1);
165
166   dump_ir_graph(irg, "-ana");
167   dump_ir_block_graph(irg, "-anablocks");
168   dump_cfg(irg, "-ana");
169   dump_loop_tree(irg, "-ana");
170
171   printf("Optimizing.\n");
172   optimize_cf(current_ir_graph);
173   local_optimize_graph(current_ir_graph);
174
175   printf("Dumping the optimized graph.\n");
176   dump_ir_graph(irg, "-opt");
177   dump_ir_block_graph(irg, "-opt");
178   dump_cfg(irg, "-opt");
179   dump_loop_tree(irg, "-opt");
180
181   printf("Use ycomp to view these graphs:\n");
182   printf("ycomp GRAPHNAME\n\n");
183
184   return 0;
185 }