removed pointless bespillremat.h includes
[libfirm] / testprograms / nested_phi.c
1 /*
2  * Project:     libFIRM
3  * File name:   testprograms/while_example.c
4  * Purpose:     Construct a loop.
5  * Author:      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 <libfirm/firm.h>
17
18 /**
19  *  This file constructs the ir for the following pseudo-program:
20  *
21  *  main(int a, int b) { //  pos 0, pos 1
22  *    int c = 1;         //  pos 2
23  *    int d = 2;         //  pos 3
24  *
25  *    while (a < c) {
26  *      while (a < d) {
27  *      }
28  *    }
29  *
30  *    return a-b;
31  *  }
32  */
33
34 #define a_pos 0
35 #define b_pos 1
36 #define c_pos 2
37 #define d_pos 3
38
39 int
40 main(void)
41 {
42   ir_type *prim_t_int;
43   ir_graph *irg;
44   ir_type *owner;
45   ir_type *proc_main;
46   ir_entity *ent;
47   ir_node *h1, *b1, *h2, *b2, *x, *r, *t1, *f1, *t2, *f2;
48
49   printf("\nCreating an IR graph: NESTED_PHI...\n");
50
51   init_firm(NULL);
52   set_optimize(1);
53   set_opt_constant_folding(1);
54   set_opt_cse(1);
55
56   prim_t_int = new_type_primitive(new_id_from_str("int"), mode_Is);
57
58 #define METHODNAME "main_tp"
59 #define NRARGS 1
60 #define NRES 1
61
62   proc_main = new_type_method(new_id_from_str(METHODNAME), NRARGS, NRES);
63   set_method_param_type(proc_main, 0, prim_t_int);
64   set_method_res_type(proc_main, 0, prim_t_int);
65
66   owner = new_type_class(new_id_from_str("NESTED_PHI"));
67   ent = new_entity(owner, new_id_from_str("main"), proc_main);
68
69   /* Generates start and end blocks and nodes and a first, initial block */
70   irg = new_ir_graph(ent, 4);
71
72   /* Generate two values */
73   set_value(a_pos, new_Proj(get_irg_args(irg), mode_Is, 0));
74   set_value(b_pos, new_Proj(get_irg_args(irg), mode_Is, 0));
75   set_value(c_pos, new_Const(mode_Is, new_tarval_from_long(1, mode_Is)));
76   set_value(d_pos, new_Const(mode_Is, new_tarval_from_long(2, mode_Is)));
77
78   /* a block for the outer loop header and the conditional branch */
79   h1 = get_irg_current_block(irg);
80   x = new_Cond(new_Proj(new_Cmp(get_value(a_pos, mode_Is), get_value(c_pos, mode_Is)),
81                          mode_b, pn_Cmp_Le));
82   f1 = new_Proj(x, mode_X, pn_Cond_false);
83   t1 = new_Proj(x, mode_X, pn_Cond_true);
84
85   /* generate the block for the loop body */
86   b1 = new_immBlock();
87   add_immBlock_pred(b1, t1);
88
89   /* The loop body is the head of the inner loop */
90   h2 = b1;
91   x = new_Cond(new_Proj(new_Cmp(get_value(a_pos, mode_Is), get_value(d_pos, mode_Is)),
92                          mode_b, pn_Cmp_Le));
93   f2 = new_Proj(x, mode_X, pn_Cond_false);
94   t2 = new_Proj(x, mode_X, pn_Cond_true);
95   add_immBlock_pred(h1, f2);
96   mature_immBlock(h1);
97
98   /* The inner loop body */
99   b2 = new_immBlock();
100   add_immBlock_pred(b2, t2);
101   mature_immBlock(b2);
102   x = new_Jmp();
103   add_immBlock_pred(h2, x);
104   mature_immBlock(h2);
105
106   /* generate the return block */
107   r = new_immBlock();
108   add_immBlock_pred(r, f1);
109   mature_immBlock(r);
110
111   {
112      ir_node *in[1];
113      in[0] = new_Sub(get_value(a_pos, mode_Is), get_value(b_pos, mode_Is), mode_Is);
114
115      x = new_Return(get_store(), 1, in);
116   }
117
118   /* finalize the end block generated in new_ir_graph() */
119   add_immBlock_pred(get_irg_end_block(irg), x);
120   mature_immBlock(get_irg_end_block(irg));
121
122   irg_finalize_cons(irg);
123
124   printf("Optimizing ...\n");
125
126 #if 0
127   local_optimize_graph(irg),
128   dead_node_elimination(irg);
129 #endif
130
131   /* verify the graph */
132   irg_vrfy(irg);
133
134   /* output the vcg file */
135   printf("Done building the graph.  Dumping it.\n");
136   turn_off_edge_labels();
137   dump_all_types("");
138   dump_ir_block_graph(irg, "");
139   printf("Use ycomp to view this graphs:\n");
140   printf("ycomp main.vcg\n\n");
141   printf("ycomp All_types.vcg\n\n");
142
143   return 0;
144 }