3 * File name: testprograms/irr_cf_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.
22 * This file constructs a control flow of following shape:
38 * This is a program as, e.g.,
49 int main(int argc, char **argv)
51 ir_graph *irg; /* this variable contains the irgraph */
52 type *owner; /* the class in which this method is defined */
53 type *proc_main; /* typeinformation for the method main */
54 entity *ent; /* represents this method as entity of owner */
55 ir_node *expr, *c1, *c2, *cond, *f, *t, *jmp, *x;
57 printf("\nCreating an IR graph: IRR_CF...\n");
61 set_opt_constant_folding(0); /* so that stupid test are not evaluated. */
63 /* FIRM was designed for oo languages where all methods belong to a class.
64 * For imperative languages like C we view a file as a large class containing
65 * all functions as methods in this file.
66 * Therefore we define a class "empty" according to the file name
67 * with a method main as an entity.
69 #define CLASSNAME "IRREGULAR_CF"
70 #define METHODNAME "main"
74 owner = new_type_class (new_id_from_chars (CLASSNAME, strlen(CLASSNAME)));
75 proc_main = new_type_method(new_id_from_chars(METHODNAME, strlen(METHODNAME)),
77 ent = new_entity ((type *)owner,
78 new_id_from_chars (METHODNAME, strlen(METHODNAME)),
80 get_entity_ld_name(ent);
81 #define NUM_OF_LOCAL_VARS 0
83 irg = new_ir_graph (ent, NUM_OF_LOCAL_VARS);
85 /* two make two conditionals that represent a switch */
86 expr = new_Const (mode_Is, new_tarval_from_long (0, mode_Is));
87 c1 = new_Const (mode_Is, new_tarval_from_long (1, mode_Is));
88 c2 = new_Const (mode_Is, new_tarval_from_long (2, mode_Is));
90 cond = new_Cond(new_Proj(new_Cmp(expr, c1), mode_b, pn_Cmp_Eq));
91 f = new_Proj(cond, mode_X, 0);
92 t = new_Proj(cond, mode_X, 1);
93 mature_immBlock(get_irg_current_block(irg));
96 add_immBlock_pred(get_irg_current_block(irg), t);
98 mature_immBlock(get_irg_current_block(irg));
101 add_immBlock_pred(get_irg_current_block(irg), f);
102 cond = new_Cond(new_Proj(new_Cmp(expr, c2), mode_b, pn_Cmp_Eq));
103 f = new_Proj(cond, mode_X, 0);
104 t = new_Proj(cond, mode_X, 1);
105 mature_immBlock(get_irg_current_block(irg));
108 add_immBlock_pred(get_irg_current_block(irg), t);
109 add_immBlock_pred(get_irg_current_block(irg), jmp);
111 mature_immBlock(get_irg_current_block(irg));
114 add_immBlock_pred(get_irg_current_block(irg), f);
116 mature_immBlock(get_irg_current_block(irg));
119 add_immBlock_pred(get_irg_current_block(irg), t);
120 add_immBlock_pred(get_irg_current_block(irg), jmp);
122 x = new_Return (get_store(), 0, NULL);
124 mature_immBlock (get_irg_current_block(irg));
126 add_immBlock_pred (get_irg_end_block(irg), x);
127 mature_immBlock (get_irg_end_block(irg));
129 irg_finalize_cons (irg);
131 printf("Optimizing ...\n");
132 dead_node_elimination(irg);
134 /* verify the graph */
137 printf("Dumping the graph and a control flow graph.\n");
138 char *dump_file_suffix = "";
139 dump_ir_block_graph (irg, dump_file_suffix);
140 dump_cfg (irg, dump_file_suffix);
141 printf("Use xvcg to view these graphs:\n");
142 printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");