Fixed and enhanced movement of Store operation through a PhiM node
[libfirm] / testprograms / while_example.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
14 # include <stdio.h>
15 # include <string.h>
16
17 # include "irvrfy.h"
18 # include "irdump.h"
19 # include "firm.h"
20
21 /**
22 *  This file constructs the ir for the following pseudo-program:
23 *
24 *  main(int a) {        //  pos 0
25 *    int b = 1;         //  pos 1
26 *    int h;             //  pos 2
27 *
28 *    while (0 == 2) loop {
29 *      h = a;
30 *      a = b;
31 *      b = h;
32 *    }
33 *
34 *    return a-b;
35 *  }
36 **/
37
38 int
39 main(void)
40 {
41   type *prim_t_int;
42   ir_graph *irg;
43   type *owner;
44   type *proc_main;
45   entity *ent;
46   ir_node *b, *x, *r, *t, *f;
47
48   printf("\nCreating an IR graph: WHILE_EXAMPLE...\n");
49
50   init_firm (NULL);
51
52   set_optimize(1);
53   set_opt_constant_folding(1);
54   set_opt_cse(1);
55   set_opt_dead_node_elimination (1);
56
57   prim_t_int = new_type_primitive(new_id_from_chars ("int", 3), mode_Is);
58
59 #define METHODNAME "main_tp"
60 #define NRARGS 1
61 #define NRES 1
62
63   proc_main = new_type_method(new_id_from_chars(METHODNAME, strlen(METHODNAME)),
64                               NRARGS, NRES);
65   set_method_param_type(proc_main, 0, prim_t_int);
66   set_method_res_type(proc_main, 0, prim_t_int);
67
68
69   owner = new_type_class (new_id_from_chars ("WHILE_EXAMPLE", 13));
70   ent = new_entity (owner, new_id_from_chars ("main", strlen("main")), proc_main);
71   get_entity_ld_name(ent); /* force name mangling */
72
73   /* Generates start and end blocks and nodes and a first, initial block */
74   irg = new_ir_graph (ent, 4);
75
76   /* Generate two values */
77   set_value (0, new_Proj(get_irg_args(irg), mode_Is, 0));
78   set_value (1, new_Const (mode_Is, new_tarval_from_long (1, mode_Is)));
79   x = new_Jmp();
80   mature_immBlock (get_irg_current_block(irg));
81
82
83   /* generate a block for the loop header and the conditional branch */
84   r = new_immBlock ();
85   add_immBlock_pred (r, x);
86   x = new_Cond (new_Proj(new_Cmp(new_Const (mode_Is, new_tarval_from_long (0, mode_Is)),
87                  get_value(1, mode_Is)),
88                          mode_b, pn_Cmp_Eq));
89   f = new_Proj (x, mode_X, 0);
90   t = new_Proj (x, mode_X, 1);
91
92   /* generate the block for the loop body */
93   b = new_immBlock ();
94   add_immBlock_pred (b, t);
95   x = new_Jmp ();
96   add_immBlock_pred (r, x);
97
98   /* The code in the loop body,
99      as we are dealing with local variables only the dataflow edges
100      are manipulated. */
101   set_value (2, get_value (0, mode_Is));
102   set_value (0, get_value (1, mode_Is));
103   set_value (1, get_value (2, mode_Is));
104   mature_immBlock (b);
105   mature_immBlock (r);
106
107   /* generate the return block */
108   r = new_immBlock ();
109   add_immBlock_pred (r, f);
110   mature_immBlock (r);
111
112   {
113      ir_node *in[1];
114      in[0] = new_Sub (get_value (0, mode_Is), get_value (1, mode_Is), mode_Is);
115
116      x = new_Return (get_store (), 1, in);
117   }
118
119   /* finalize the end block generated in new_ir_graph() */
120   add_immBlock_pred (get_irg_end_block(irg), x);
121   mature_immBlock (get_irg_end_block(irg));
122
123   irg_finalize_cons (irg);
124
125   printf("Optimizing ...\n");
126
127   local_optimize_graph(irg),
128   dead_node_elimination(irg);
129
130   /* verify the graph */
131   irg_vrfy(irg);
132
133   /* output the vcg file */
134   printf("Done building the graph.  Dumping it.\n");
135   turn_off_edge_labels();
136   char *suffix = "";
137   dump_all_types(suffix);
138   dump_ir_block_graph (irg, suffix);
139   printf("Use xvcg to view this graph:\n");
140   printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");
141
142   return (0);
143 }