Nothing major
[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 (0);  /* so that the stupid tests are not optimized. */
50   set_opt_cse(1);
51   set_opt_dead_node_elimination(1);
52
53   /* FIRM was designed for oo languages where all methods belong to a class.
54    * For imperative languages like C we view a file as a large class containing
55    * all functions as methods in this file.
56    * Therefore we define a class "empty" according to the file name
57    * with a method main as an entity.
58    */
59 #define CLASSNAME "IRR_LOOP"
60 #define METHODNAME "main"
61 #define NRARGS 0
62 #define NRES 0
63   printf("\nCreating an IR graph: %s...\n", CLASSNAME);
64
65   owner = new_type_class (id_from_str (CLASSNAME, strlen(CLASSNAME)));
66   proc_main = new_type_method(id_from_str(METHODNAME, strlen(METHODNAME)),
67                               NRARGS, NRES);
68   ent = new_entity ((type *)owner,
69                     id_from_str (METHODNAME, strlen(METHODNAME)),
70                     (type *)proc_main);
71
72 #define NUM_OF_LOCAL_VARS 0
73
74   irg = new_ir_graph (ent, NUM_OF_LOCAL_VARS);
75
76   /* to make three conditionals  */
77   expr = new_Const (mode_i, tarval_from_long (mode_i, 0));
78   c1 = new_Const (mode_i, tarval_from_long (mode_i, 1));
79   c2 = new_Const (mode_i, tarval_from_long (mode_i, 2));
80   c3 = new_Const (mode_i, tarval_from_long (mode_i, 2));
81
82   cond = new_Cond(new_Proj(new_Cmp(expr, c1), mode_b, Eq));
83   f = new_Proj(cond, mode_X, 0);
84   t = new_Proj(cond, mode_X, 1);
85   mature_block(get_irg_current_block(irg));
86
87   loopBlock1 = new_immBlock();
88   add_in_edge(loopBlock1, t);
89   cond = new_Cond(new_Proj(new_Cmp(expr, c2), mode_b, Eq));
90   f_l1 = new_Proj(cond, mode_X, 0);
91   t_l1 = new_Proj(cond, mode_X, 1);
92
93   loopBlock2 = new_immBlock();
94   add_in_edge(loopBlock2, f);
95   cond = new_Cond(new_Proj(new_Cmp(expr, c3), mode_b, Eq));
96   f_l2 = new_Proj(cond, mode_X, 0);
97   t_l2 = new_Proj(cond, mode_X, 1);
98
99   add_in_edge(loopBlock1, t_l2);
100   add_in_edge(loopBlock2, t_l1);
101   mature_block(loopBlock1);
102   mature_block(loopBlock2);
103
104   new_immBlock();
105   add_in_edge(get_irg_current_block(irg), f_l2);
106   add_in_edge(get_irg_current_block(irg), f_l1);
107   {
108     ir_node *in[0];
109     x = new_Return (get_store(), 0, in);
110   }
111   mature_block (get_irg_current_block(irg));
112
113   add_in_edge (get_irg_end_block(irg), x);
114   mature_block (get_irg_end_block(irg));
115
116   printf("Optimizing ...\n");
117   dead_node_elimination(irg);
118
119   /* verify the graph */
120   irg_vrfy(irg);
121
122   printf("Dumping the graph and a control flow graph.\n");
123   dump_ir_block_graph (irg);
124   dump_cfg (irg);
125   printf("Use xvcg to view these graphs:\n");
126   printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");
127
128   return (0);
129 }