3 * File name: testprograms/irr_loop_example.c
4 * Purpose: Test Phi construction with irregular control flow.
5 * Author: Christian Schaefer, 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 a control flow with an unreachable
20 * loop _and_ an unreachable endless loop. This looks like:
22 * LoopBlock2 LoopBlock2'
26 * LoopBlock1 StartBlock LoopBlock1'
41 ir_graph *irg; /* this variable contains the irgraph */
43 ir_type *owner; /* the class in which this method is defined */
44 ir_type *proc_main; /* typeinformation for the method main */
45 ir_entity *ent; /* represents this method as ir_entity of owner */
46 ir_node *returnBlock, *loopBlock1, *loopBlock2, *x, *c1, *c2, *t, *f;
51 /*set_opt_normalize(0); */
52 set_opt_constant_folding(0); /* so that the stupid tests are not optimized. */
55 /* FIRM was designed for oo languages where all methods belong to a class.
56 * For imperative languages like C we view a file as a large class containing
57 * all functions as methods in this file.
58 * Therefore we define a class "empty" according to the file name
59 * with a method main as an ir_entity.
61 #define CLASSNAME "DEAD_LOOP"
62 #define METHODNAME "main"
65 printf("\nCreating an IR graph: %s...\n", CLASSNAME);
67 prim_t_int = new_type_primitive(new_id_from_str("int"), mode_Is);
69 owner = new_type_class(new_id_from_str(CLASSNAME));
70 proc_main = new_type_method(new_id_from_str(METHODNAME), NRARGS, NRES);
71 set_method_param_type(proc_main, 0, prim_t_int);
72 ent = new_entity(owner, new_id_from_str(METHODNAME), proc_main);
73 get_entity_ld_name(ent); /* To enforce name mangling for vcg graph name */
75 #define NUM_OF_LOCAL_VARS 0
77 irg = new_ir_graph(ent, NUM_OF_LOCAL_VARS);
79 returnBlock = get_irg_current_block(irg);
81 /* Make some real stupid stuff: a data loop (without Phi). */
83 ir_node *a, *b, *c, *in[2];
84 add_immBlock_pred(get_cur_block(), new_Bad());
85 a = new_Const(mode_Is, new_tarval_from_long(1, mode_Is));
86 b = new_Const(mode_Is, new_tarval_from_long(2, mode_Is));
87 c = new_Add(a, b, mode_Is);
88 b = new_Sub(c, b, mode_Is);
91 a = new_Phi(2, in, mode_Is);
93 /* add_End_keepalive(get_irg_end(irg), a); */
94 set_nodes_block(c, new_Bad());
95 set_nodes_block(a, new_Bad());
98 /* Make the unreachable loop */
99 loopBlock1 = new_immBlock();
100 loopBlock2 = new_immBlock();
102 add_immBlock_pred(loopBlock1, x);
103 mature_immBlock(loopBlock1);
105 set_cur_block(loopBlock1);
106 c1 = new_Const(mode_Is, new_tarval_from_long(1, mode_Is));
107 c2 = new_Proj(get_irg_args(irg), mode_Is, 0);
108 x = new_Cond(new_Proj(new_Cmp(c1, c2), mode_b, pn_Cmp_Eq));
109 f = new_Proj(x, mode_X, pn_Cond_false);
110 t = new_Proj(x, mode_X, pn_Cond_true);
111 add_immBlock_pred(loopBlock2, t);
112 add_immBlock_pred(returnBlock, f);
113 mature_immBlock(loopBlock2);
115 /* Make the unreachable, endless loop */
116 loopBlock1 = new_immBlock();
117 loopBlock2 = new_immBlock();
119 add_immBlock_pred(loopBlock1, x);
120 mature_immBlock(loopBlock1);
122 set_cur_block(loopBlock1);
124 add_immBlock_pred(loopBlock2, x);
125 add_End_keepalive(get_irg_end(irg), loopBlock1);
126 mature_immBlock(loopBlock2);
128 /* Make the return block */
129 set_cur_block(returnBlock);
130 x = new_Return(get_store(), 0, NULL);
131 mature_immBlock(get_irg_current_block(irg));
133 add_immBlock_pred(get_irg_end_block(irg), x);
134 mature_immBlock(get_irg_end_block(irg));
136 irg_finalize_cons(irg);
139 printf("Optimizing ...\n");
140 dead_node_elimination(irg);
143 /* verify the graph */
146 printf("Dumping the graph and a control flow graph.\n");
147 turn_off_edge_labels();
148 dump_keepalive_edges(1);
149 dump_consts_local(0);
150 dump_ir_graph(irg, "-cfg");
151 dump_ir_block_graph(irg, "-cfg");
152 dump_cfg(irg, "-cfg");
154 printf("Running analyses.\n");
155 compute_irg_outs(irg);
157 construct_backedges(irg);
159 printf("Dumping the graph with analyses information.\n");
162 dump_dominator_information(0);
163 dump_loop_information(0);
164 dump_backedge_information(1);
166 dump_ir_graph(irg, "-ana");
167 dump_ir_block_graph(irg, "-anablocks");
168 dump_cfg(irg, "-ana");
169 dump_loop_tree(irg, "-ana");
171 printf("Optimizing.\n");
172 optimize_cf(current_ir_graph);
173 local_optimize_graph(current_ir_graph);
175 printf("Dumping the optimized graph.\n");
176 dump_ir_graph(irg, "-opt");
177 dump_ir_block_graph(irg, "-opt");
178 dump_cfg(irg, "-opt");
179 dump_loop_tree(irg, "-opt");
181 printf("Use ycomp to view these graphs:\n");
182 printf("ycomp GRAPHNAME\n\n");