ad7e614491d8ba27bc873433c4b40e6c35ce70ff
[libfirm] / testprograms / dead_block_example.c
1 /* Copyright (C) 1998 - 2000 by Universitaet Karlsruhe
2 ** All rights reserved.
3 **
4 ** Authors: Christian Schaefer, Goetz Lindenmaier
5 **
6 ** testprogram.
7 */
8
9 # include <stdio.h>
10
11 # include "irdump.h"
12 # include "firm.h"
13
14 /*
15  *   a dead block / unreachable code.
16  */
17
18 /**
19 ***  This file constructs a control flow of following shape:
20 ***
21 ***
22 ***         firstBlock
23 ***          /   \
24 ***         /     \
25 ***       |/_     _\|
26 ***     Block1    Block2   deadBlock
27 ***        \       |       /
28 ***         \      |      /
29 ***         _\|   \ /   |/_
30 ***            nextBlock
31 ***
32 ***
33 ***   This is a program as, e.g.,
34 ***
35 ***   if () then
36 ***     { Jmp label1; } // happens anyways
37 ***   else
38 ***     { Jmp label1; } // happens anyways
39 *** label1:
40 ***   return();
41 ***   Jmp label1;
42 ***
43 **/
44
45 int main(int argc, char **argv)
46 {
47   ir_graph *irg;          /* this variable contains the irgraph */
48   type_class *owner;      /* the class in which this method is defined */
49   type_method *proc_main; /* type information for the method main */
50   entity *ent;            /* represents this method as entity of owner */
51   ir_node *c1, *c2, *cond, *f, *t, *endBlock, *Block1, *jmp, *Block2,
52           *deadBlock, *x;
53
54   /* init library */
55   init_firm ();
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_BLOCK"
64 #define METHODNAME "main"
65 #define NRARGS 0
66 #define NRES 0
67   printf("creating an IR graph: %s...\n", CLASSNAME);
68
69   owner = new_type_class (id_from_str (CLASSNAME, strlen(CLASSNAME)));
70   proc_main = new_type_method(id_from_str(METHODNAME, strlen(METHODNAME)),
71                               NRARGS, NRES);
72   ent = new_entity ((type *)owner,
73                     id_from_str (METHODNAME, strlen(METHODNAME)),
74                     (type *)proc_main);
75
76 #define NUM_OF_LOCAL_VARS 1
77
78   irg = new_ir_graph (ent, NUM_OF_LOCAL_VARS);
79
80   /* two make a condition  */
81   c1 = new_Const (mode_i, tarval_from_long (mode_i, 1));
82   c2 = new_Const (mode_i, tarval_from_long (mode_i, 2));
83   set_value(0, c2);
84
85   cond = new_Cond(new_Proj(new_Cmp(c1, c2), mode_b, Eq));
86   f = new_Proj(cond, mode_X, 0);
87   t = new_Proj(cond, mode_X, 1);
88   mature_block(irg->current_block);
89
90   /* end block to add jmps */
91   endBlock = new_Block();
92
93   /* Block 1 */
94   Block1 = new_Block();
95   add_in_edge(Block1, t);
96   mature_block(Block1);
97   jmp = new_Jmp();
98   add_in_edge(endBlock, jmp);
99
100   /* Block 2 */
101   Block2 = new_Block();
102   add_in_edge(Block2, f);
103   mature_block(Block2);
104   jmp = new_Jmp();
105   add_in_edge(endBlock, jmp);
106
107   /* dead Block */
108   deadBlock = new_Block();
109   mature_block(deadBlock);
110   jmp = new_Jmp();
111   add_in_edge(endBlock, jmp);
112
113   /* finish end block */
114   switch_block(endBlock);
115   {
116     ir_node *in[1];
117     in[0] = get_value(0, mode_i);
118     get_store();
119     x = new_Return (get_store(), 1, in);
120   }
121   mature_block (irg->current_block);
122
123   add_in_edge (irg->end_block, x);
124   mature_block (irg->end_block);
125
126   /* verify the graph */
127   irg_vrfy(irg);
128
129   printf("\nDone building the graph.\n");
130   set_opt_constant_folding (0);
131   set_optimize(0);
132   set_opt_cse(0);
133   local_optimize_graph (irg);
134   dead_node_elimination (irg);
135   printf("\nDone local optimization.\n");
136   printf("Dumping the graph and a control flow graph.\n");
137
138   dump_ir_block_graph (irg);
139   dump_cfg (irg);
140
141   printf("use xvcg to view these graphs:\n");
142   printf("/ben/goetz/bin/xvcg GRAPHNAME\n");
143
144   return (0);
145 }