3 * File name: testprograms/while_example.c
4 * Purpose: Construct a loop.
5 * Author: Goetz Lindenmaier
9 * Copyright: (c) 1999-2003 Universität Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
16 #include <libfirm/firm.h>
19 * This file constructs the ir for the following pseudo-program:
21 * main(int a, int b) { // pos 0, pos 1
47 ir_node *h1, *b1, *h2, *b2, *x, *r, *t1, *f1, *t2, *f2;
49 printf("\nCreating an IR graph: NESTED_PHI...\n");
53 set_opt_constant_folding(1);
56 prim_t_int = new_type_primitive(new_id_from_str("int"), mode_Is);
58 #define METHODNAME "main_tp"
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);
66 owner = new_type_class(new_id_from_str("NESTED_PHI"));
67 ent = new_entity(owner, new_id_from_str("main"), proc_main);
69 /* Generates start and end blocks and nodes and a first, initial block */
70 irg = new_ir_graph(ent, 4);
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)));
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)),
82 f1 = new_Proj(x, mode_X, pn_Cond_false);
83 t1 = new_Proj(x, mode_X, pn_Cond_true);
85 /* generate the block for the loop body */
87 add_immBlock_pred(b1, t1);
89 /* The loop body is the head of the inner loop */
91 x = new_Cond(new_Proj(new_Cmp(get_value(a_pos, mode_Is), get_value(d_pos, mode_Is)),
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);
98 /* The inner loop body */
100 add_immBlock_pred(b2, t2);
103 add_immBlock_pred(h2, x);
106 /* generate the return block */
108 add_immBlock_pred(r, f1);
113 in[0] = new_Sub(get_value(a_pos, mode_Is), get_value(b_pos, mode_Is), mode_Is);
115 x = new_Return(get_store(), 1, in);
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));
122 irg_finalize_cons(irg);
124 printf("Optimizing ...\n");
127 local_optimize_graph(irg),
128 dead_node_elimination(irg);
131 /* verify the graph */
134 /* output the vcg file */
135 printf("Done building the graph. Dumping it.\n");
136 turn_off_edge_labels();
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");