3 * File name: testprograms/dead_block_example.c
4 * Purpose: Test unreachable code elimination.
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.
18 #include <libfirm/firm.h>
21 * a dead block / unreachable code.
25 * This file constructs a control flow of following shape:
32 * Block1 Block2 deadBlock
39 * This is a program as, e.g.,
42 * { Jmp label1; } // happens anyways
44 * { Jmp label1; } // happens anyways
53 char *dump_file_suffix = "";
54 ir_graph *irg; /* this variable contains the irgraph */
55 ir_type *owner; /* the class in which this method is defined */
56 ir_type *proc_main; /* ir_type information for the method main */
58 ir_entity *ent; /* represents this method as ir_entity of owner */
59 ir_node *c1, *c2, *cond, *f, *t, *endBlock, *Block1, *jmp, *Block2,
65 /*** Make basic ir_type information for primitive ir_type int. ***/
66 prim_t_int = new_type_primitive(new_id_from_chars ("int", 3), mode_Is);
68 /* FIRM was designed for oo languages where all methods belong to a class.
69 * For imperative languages like C we view a file as a large class containing
70 * all functions as methods in this file.
71 * Therefore we define a class "empty" according to the file name
72 * with a method main as an ir_entity.
74 #define CLASSNAME "DEAD_BLOCK"
75 #define METHODNAME "main"
78 printf("\nCreating an IR graph: %s...\n", CLASSNAME);
80 owner = new_type_class (new_id_from_chars (CLASSNAME, strlen(CLASSNAME)));
81 proc_main = new_type_method(new_id_from_chars(METHODNAME, strlen(METHODNAME)),
83 set_method_res_type(proc_main, 0, prim_t_int);
84 ent = new_entity (owner,
85 new_id_from_chars (METHODNAME, strlen(METHODNAME)),
87 get_entity_ld_name(ent); /* To enforce name mangling for vcg graph name */
88 #define NUM_OF_LOCAL_VARS 1
90 irg = new_ir_graph (ent, NUM_OF_LOCAL_VARS);
92 /* to make a condition */
93 c1 = new_Const (mode_Is, new_tarval_from_long (1, mode_Is));
94 c2 = new_Const (mode_Is, new_tarval_from_long (2, mode_Is));
97 cond = new_Cond(new_Proj(new_Cmp(c1, c2), mode_b, pn_Cmp_Eq));
98 f = new_Proj(cond, mode_X, 0);
99 t = new_Proj(cond, mode_X, 1);
100 mature_immBlock(get_irg_current_block(irg));
102 /* end block to add jmps */
103 endBlock = new_immBlock();
106 Block1 = new_immBlock();
107 add_immBlock_pred(Block1, t);
108 mature_immBlock(Block1);
110 add_immBlock_pred(endBlock, jmp);
113 Block2 = new_immBlock();
114 add_immBlock_pred(Block2, f);
115 mature_immBlock(Block2);
117 add_immBlock_pred(endBlock, jmp);
120 deadBlock = new_immBlock();
121 mature_immBlock(deadBlock);
123 add_immBlock_pred(endBlock, jmp);
125 /* finish end block */
126 set_cur_block(endBlock);
129 in[0] = get_value(0, mode_Is);
131 x = new_Return (get_store(), 1, in);
133 mature_immBlock (get_irg_current_block(irg));
135 add_immBlock_pred (get_irg_end_block(irg), x);
136 mature_immBlock (get_irg_end_block(irg));
138 irg_finalize_cons (irg);
140 printf("Optimizing ...\n");
141 local_optimize_graph (irg);
142 dead_node_elimination (irg);
144 /* verify the graph */
147 printf("Dumping the graph and a control flow graph.\n");
148 dump_ir_block_graph (irg, dump_file_suffix);
149 dump_cfg (irg, dump_file_suffix);
150 printf("Use ycomp to view these graphs:\n");
151 printf("ycomp GRAPHNAME\n\n");