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