*** empty log message ***
[libfirm] / testprograms / irr_loop_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  *   an irreducible loop.
16  */
17
18 /**
19 ***  This file constructs a control flow of following shape:
20 ***
21 ***
22 ***         firstBlock
23 ***          /      \
24 ***         /        \
25 ***       |/_        _\|
26 ***            ---->
27 *** LoopBlock1       LoopBlock2
28 ***            <----
29 ***        \              /
30 ***         \            /
31 ***         _\|        |/_
32 ***           nextBlock
33 ***
34 ***
35 **/
36
37 int main(int argc, char **argv)
38 {
39   ir_graph *irg;          /* this variable contains the irgraph */
40   type_class *owner;      /* the class in which this method is defined */
41   type_method *proc_main; /* typeinformation for the method main */
42   entity *ent;            /* represents this method as entity of owner */
43   ir_node *expr, *c1, *c2, *c3, *cond, *f, *t, *loopBlock1, *f_l1, *t_l1,
44           *loopBlock2, *f_l2, *t_l2, *x;
45
46
47   /* init library */
48   init_firm ();
49   set_opt_constant_folding (1);
50
51   /* FIRM was designed for oo languages where all methods belong to a class.
52    * For imperative languages like C we view a file as a large class containing
53    * all functions as methods in this file.
54    * Therefore we define a class "empty" according to the file name
55    * with a method main as an entity.
56    */
57 #define CLASSNAME "IRR_LOOP"
58 #define METHODNAME "main"
59 #define NRARGS 0
60 #define NRES 0
61   printf("creating an IR graph: %s...\n", CLASSNAME);
62
63   owner = new_type_class (id_from_str (CLASSNAME, strlen(CLASSNAME)));
64   proc_main = new_type_method(id_from_str(METHODNAME, strlen(METHODNAME)),
65                               NRARGS, NRES);
66   ent = new_entity ((type *)owner,
67                     id_from_str (METHODNAME, strlen(METHODNAME)),
68                     (type *)proc_main);
69
70 #define NUM_OF_LOCAL_VARS 0
71
72   irg = new_ir_graph (ent, NUM_OF_LOCAL_VARS);
73
74   /* two make three conditionals  */
75   expr = new_Const (mode_i, tarval_from_long (mode_i, 0));
76   c1 = new_Const (mode_i, tarval_from_long (mode_i, 1));
77   c2 = new_Const (mode_i, tarval_from_long (mode_i, 2));
78   c3 = new_Const (mode_i, tarval_from_long (mode_i, 2));
79
80   cond = new_Cond(new_Proj(new_Cmp(expr, c1), mode_b, Eq));
81   f = new_Proj(cond, mode_X, 0);
82   t = new_Proj(cond, mode_X, 1);
83   mature_block(irg->current_block);
84
85   loopBlock1 = new_Block();
86   add_in_edge(loopBlock1, t);
87   cond = new_Cond(new_Proj(new_Cmp(expr, c2), mode_b, Eq));
88   f_l1 = new_Proj(cond, mode_X, 0);
89   t_l1 = new_Proj(cond, mode_X, 1);
90
91   loopBlock2 = new_Block();
92   add_in_edge(loopBlock2, f);
93   cond = new_Cond(new_Proj(new_Cmp(expr, c3), mode_b, Eq));
94   f_l2 = new_Proj(cond, mode_X, 0);
95   t_l2 = new_Proj(cond, mode_X, 1);
96
97   add_in_edge(loopBlock1, t_l2);
98   add_in_edge(loopBlock2, t_l1);
99   mature_block(loopBlock1);
100   mature_block(loopBlock2);
101
102   new_Block();
103   add_in_edge(irg->current_block, f_l2);
104   add_in_edge(irg->current_block, f_l1);
105   {
106     ir_node *in[0];
107     x = new_Return (get_store(), 1, in);
108   }
109   mature_block (irg->current_block);
110
111   add_in_edge (irg->end_block, x);
112   mature_block (irg->end_block);
113
114   /* verify the graph */
115   irg_vrfy(irg);
116
117   dead_node_elimination(irg);
118
119   printf("\nDone building the graph..\n");
120   printf("Dumping the graph and a control flow graph.\n");
121   dump_ir_block_graph (irg);
122   dump_cfg (irg);
123
124   printf("use xvcg to view these graphs:\n");
125   printf("/ben/goetz/bin/xvcg GRAPHNAME\n");
126
127   return (0);
128 }