shows several errors in analyses
[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     *owner;      /* the class in which this method is defined */
45   type     *proc_main;  /* typeinformation for the method main */
46   entity   *ent;        /* represents this method as entity of owner */
47   ir_node  *returnBlock, *loopBlock1, *loopBlock2, *x, *c1, *c2, *t, *f;
48
49
50   /* init library */
51   init_firm (NULL);
52   set_opt_normalize(0);
53   set_opt_constant_folding (0);  /* so that the stupid tests are not optimized. */
54   set_opt_cse(1);
55   set_opt_dead_node_elimination(1);
56
57   /* FIRM was designed for oo languages where all methods belong to a class.
58    * For imperative languages like C we view a file as a large class containing
59    * all functions as methods in this file.
60    * Therefore we define a class "empty" according to the file name
61    * with a method main as an entity.
62    */
63 #define CLASSNAME "DEAD_LOOP"
64 #define METHODNAME "main"
65 #define NRARGS 0
66 #define NRES 0
67   printf("\nCreating an IR graph: %s...\n", CLASSNAME);
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   ent = new_entity (owner, new_id_from_str (METHODNAME), proc_main);
72   get_entity_ld_name(ent); /* To enforce name mangling for vcg graph name */
73
74 #define NUM_OF_LOCAL_VARS 0
75
76   irg = new_ir_graph (ent, NUM_OF_LOCAL_VARS);
77
78   returnBlock = get_irg_current_block(irg);
79
80 #if 1
81   /* Make the unreachable loop */
82   loopBlock1 = new_immBlock();
83   loopBlock2 = new_immBlock();
84   x = new_Jmp();
85   add_in_edge(loopBlock1, x);
86   mature_block(loopBlock1);
87
88   switch_block(loopBlock1);
89   c1 = new_Const (mode_Is, new_tarval_from_long (1, mode_Is));
90   c2 = new_Const (mode_Is, new_tarval_from_long (2, mode_Is));
91   x =  new_Cond(new_Proj(new_Cmp(c1, c2), mode_b, Eq));
92   f = new_Proj(x, mode_X, 0);
93   t = new_Proj(x, mode_X, 1);
94   add_in_edge(loopBlock2, t);
95   add_in_edge(returnBlock, f);
96   mature_block(loopBlock2);
97 #endif
98
99   /* Make the unreachable, endless loop */
100   loopBlock1 = new_immBlock();
101   loopBlock2 = new_immBlock();
102   x = new_Jmp();
103   add_in_edge(loopBlock1, x);
104   mature_block(loopBlock1);
105
106   switch_block(loopBlock1);
107   x = new_Jmp();
108   add_in_edge(loopBlock2, x);
109   add_End_keepalive(get_irg_end(irg), x);
110   mature_block(loopBlock2);
111
112   /* Make the return block */
113   switch_block(returnBlock);
114   x = new_Return (get_store(), 0, NULL);
115   mature_block (get_irg_current_block(irg));
116
117   add_in_edge (get_irg_end_block(irg), x);
118   mature_block (get_irg_end_block(irg));
119
120   finalize_cons (irg);
121
122   printf("Optimizing ...\n");
123   dead_node_elimination(irg);
124
125   /* verify the graph */
126   irg_vrfy(irg);
127
128   printf("Dumping the graph and a control flow graph.\n");
129   turn_off_edge_labels();
130   dump_keepalive_edges(1);
131   dump_consts_local(1);
132   dump_ir_block_graph (irg);
133   dump_cfg (irg);
134
135   printf("Running analyses.\n");
136   compute_outs(irg);
137   compute_doms(irg);
138   construct_backedges(irg);
139
140   printf("Dumping the graph with analyses information.\n");
141
142   dump_out_edges();
143   dump_dominator_information();
144   dump_loop_information();
145   dump_backedge_information(1);
146
147   dump_ir_block_graph (irg);
148
149   printf("Use xvcg to view these graphs:\n");
150   printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");
151
152   return (0);
153 }