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.
21 * This file constructs a control flow with an unreachable
22 * loop _and_ an unreachable endless loop. This looks like:
24 * LoopBlock2 LoopBlock2'
28 * LoopBlock1 StartBlock LoopBlock1'
41 int main(int argc, char **argv)
43 ir_graph *irg; /* this variable contains the irgraph */
45 type *owner; /* the class in which this method is defined */
46 type *proc_main; /* typeinformation for the method main */
47 entity *ent; /* represents this method as entity of owner */
48 ir_node *returnBlock, *loopBlock1, *loopBlock2, *x, *c1, *c2, *t, *f;
53 //set_opt_normalize(0);
54 set_opt_constant_folding (0); /* so that the stupid tests are not optimized. */
56 set_opt_dead_node_elimination(1);
58 /* FIRM was designed for oo languages where all methods belong to a class.
59 * For imperative languages like C we view a file as a large class containing
60 * all functions as methods in this file.
61 * Therefore we define a class "empty" according to the file name
62 * with a method main as an entity.
64 #define CLASSNAME "DEAD_LOOP"
65 #define METHODNAME "main"
68 printf("\nCreating an IR graph: %s...\n", CLASSNAME);
70 prim_t_int = new_type_primitive(id_from_str ("int", 3), mode_Is);
72 owner = new_type_class (new_id_from_str (CLASSNAME));
73 proc_main = new_type_method(new_id_from_str(METHODNAME), NRARGS, NRES);
74 set_method_param_type(proc_main, 0, prim_t_int);
75 ent = new_entity (owner, new_id_from_str (METHODNAME), proc_main);
76 get_entity_ld_name(ent); /* To enforce name mangling for vcg graph name */
78 #define NUM_OF_LOCAL_VARS 0
80 irg = new_ir_graph (ent, NUM_OF_LOCAL_VARS);
82 returnBlock = get_irg_current_block(irg);
85 /* Make the unreachable loop */
86 loopBlock1 = new_immBlock();
87 loopBlock2 = new_immBlock();
89 add_in_edge(loopBlock1, x);
90 mature_block(loopBlock1);
92 switch_block(loopBlock1);
93 c1 = new_Const (mode_Is, new_tarval_from_long (1, mode_Is));
94 c2 = new_Proj(get_irg_args(irg), mode_Is, 0);
95 x = new_Cond(new_Proj(new_Cmp(c1, c2), mode_b, Eq));
96 f = new_Proj(x, mode_X, 0);
97 t = new_Proj(x, mode_X, 1);
98 add_in_edge(loopBlock2, t);
99 add_in_edge(returnBlock, f);
100 mature_block(loopBlock2);
104 /* Make the unreachable, endless loop */
105 loopBlock1 = new_immBlock();
106 loopBlock2 = new_immBlock();
108 add_in_edge(loopBlock1, x);
109 mature_block(loopBlock1);
111 switch_block(loopBlock1);
113 add_in_edge(loopBlock2, x);
114 add_End_keepalive(get_irg_end(irg), x);
115 mature_block(loopBlock2);
118 /* Make the return block */
119 switch_block(returnBlock);
120 x = new_Return (get_store(), 0, NULL);
121 mature_block (get_irg_current_block(irg));
123 add_in_edge (get_irg_end_block(irg), x);
124 mature_block (get_irg_end_block(irg));
128 printf("Optimizing ...\n");
129 dead_node_elimination(irg);
131 /* verify the graph */
134 printf("Dumping the graph and a control flow graph.\n");
135 turn_off_edge_labels();
136 dump_keepalive_edges(1);
137 dump_consts_local(1);
138 dump_ir_block_graph (irg);
141 printf("Running analyses.\n");
144 construct_backedges(irg);
146 printf("Dumping the graph with analyses information.\n");
147 dump_file_suffix = "-ana";
150 dump_dominator_information();
151 dump_loop_information();
152 dump_backedge_information(1);
154 dump_ir_block_graph (irg);
156 dump_loop_tree(irg, dump_file_suffix);
158 printf("Use xvcg to view these graphs:\n");
159 printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");