latest version
[libfirm] / testprograms / dead_block_example.c
1 /*
2  * Project:     libFIRM
3  * File name:   testprograms/dead_block_example.c
4  * Purpose:     Test unreachable code elimination.
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  *   a dead block / unreachable code.
22  */
23
24 /**
25 *  This file constructs a control flow of following shape:
26 *
27 *
28 *         firstBlock
29 *          /   \
30 *         /     \
31 *       |/_     _\|
32 *     Block1    Block2   deadBlock
33 *        \       |       /
34 *       \      |      /
35 *       _\|   \ /   |/_
36 *            nextBlock
37 *
38 *
39 *   This is a program as, e.g.,
40 *
41 *   if () then
42 *     { Jmp label1; } //  happens anyways
43 *   else
44 *     { Jmp label1; } //  happens anyways
45 * label1:
46 *   return();
47 *   Jmp label1;
48 *
49 **/
50
51 int main(int argc, char **argv)
52 {
53   ir_graph *irg;          /* this variable contains the irgraph */
54   type *owner;      /* the class in which this method is defined */
55   type *proc_main; /* type information for the method main */
56   type     *prim_t_int;
57   entity *ent;            /* represents this method as entity of owner */
58   ir_node *c1, *c2, *cond, *f, *t, *endBlock, *Block1, *jmp, *Block2,
59           *deadBlock, *x;
60
61   /* init library */
62   init_firm (NULL);
63
64   /*** Make basic type information for primitive type int. ***/
65   prim_t_int = new_type_primitive(new_id_from_chars ("int", 3), mode_Is);
66
67   /* FIRM was designed for oo languages where all methods belong to a class.
68    * For imperative languages like C we view a file as a large class containing
69    * all functions as methods in this file.
70    * Therefore we define a class "empty" according to the file name
71    * with a method main as an entity.
72    */
73 #define CLASSNAME "DEAD_BLOCK"
74 #define METHODNAME "main"
75 #define NRARGS 0
76 #define NRES 1
77   printf("\nCreating an IR graph: %s...\n", CLASSNAME);
78
79   owner = new_type_class (new_id_from_chars (CLASSNAME, strlen(CLASSNAME)));
80   proc_main = new_type_method(new_id_from_chars(METHODNAME, strlen(METHODNAME)),
81                               NRARGS, NRES);
82   set_method_res_type(proc_main, 0, prim_t_int);
83   ent = new_entity (owner,
84                     new_id_from_chars (METHODNAME, strlen(METHODNAME)),
85                     proc_main);
86   get_entity_ld_name(ent); /* To enforce name mangling for vcg graph name */
87 #define NUM_OF_LOCAL_VARS 1
88
89   irg = new_ir_graph (ent, NUM_OF_LOCAL_VARS);
90
91   /* to make a condition  */
92   c1 = new_Const (mode_Is, new_tarval_from_long (1, mode_Is));
93   c2 = new_Const (mode_Is, new_tarval_from_long (2, mode_Is));
94   set_value(0, c2);
95
96   cond = new_Cond(new_Proj(new_Cmp(c1, c2), mode_b, pn_Cmp_Eq));
97   f = new_Proj(cond, mode_X, 0);
98   t = new_Proj(cond, mode_X, 1);
99   mature_immBlock(get_irg_current_block(irg));
100
101   /* end block to add jmps */
102   endBlock = new_immBlock();
103
104   /* Block 1 */
105   Block1 = new_immBlock();
106   add_immBlock_pred(Block1, t);
107   mature_immBlock(Block1);
108   jmp = new_Jmp();
109   add_immBlock_pred(endBlock, jmp);
110
111   /* Block 2 */
112   Block2 = new_immBlock();
113   add_immBlock_pred(Block2, f);
114   mature_immBlock(Block2);
115   jmp = new_Jmp();
116   add_immBlock_pred(endBlock, jmp);
117
118   /* dead Block */
119   deadBlock = new_immBlock();
120   mature_immBlock(deadBlock);
121   jmp = new_Jmp();
122   add_immBlock_pred(endBlock, jmp);
123
124   /* finish end block */
125   set_cur_block(endBlock);
126   {
127     ir_node *in[1];
128     in[0] = get_value(0, mode_Is);
129     get_store();
130     x = new_Return (get_store(), 1, in);
131   }
132   mature_immBlock (get_irg_current_block(irg));
133
134   add_immBlock_pred (get_irg_end_block(irg), x);
135   mature_immBlock (get_irg_end_block(irg));
136
137   irg_finalize_cons (irg);
138
139   printf("Optimizing ...\n");
140   local_optimize_graph (irg);
141   dead_node_elimination (irg);
142
143   /* verify the graph */
144   irg_vrfy(irg);
145
146   printf("Dumping the graph and a control flow graph.\n");
147   char *dump_file_suffix = "";
148   dump_ir_block_graph (irg, dump_file_suffix);
149   dump_cfg (irg, dump_file_suffix);
150   printf("Use xvcg to view these graphs:\n");
151   printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");
152
153   return (0);
154 }