do not optimize dead blocks preventing endless loops ...
[libfirm] / ir / ir / irprog.c
1 /*
2  * Project:     libFIRM
3  * File name:   ir/ir/irprog.c
4  * Purpose:     Entry point to the representation of a whole program.
5  * Author:      Goetz Lindenmaier
6  * Modified by:
7  * Created:     2000
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 2000-2007 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13 #ifdef HAVE_CONFIG_H
14 # include "config.h"
15 #endif
16
17 #ifdef HAVE_STRING_H
18 # include <string.h>
19 #endif
20
21 #include "irprog_t.h"
22 #include "irgraph_t.h"
23 #include "pseudo_irg.h"
24 #include "array.h"
25 #include "obst.h"
26 #include "typegmod.h"
27 #include "irop_t.h"
28 #include "irmemory.h"
29
30 /** The name of the Global Type. */
31 #define GLOBAL_TYPE_NAME "GlobalType"
32 /** The name of the Thread Local STorage Type. */
33 #define TLS_TYPE_NAME "TLS"
34 /** The initial name of the irp program. */
35 #define INITAL_PROG_NAME "no_name_set"
36
37 /* A variable from where everything in the ir can be accessed. */
38 ir_prog *irp;
39 ir_prog *get_irp(void) { return irp; }
40
41 /**
42  *  Create a new incomplete ir_prog.
43  */
44 static ir_prog *new_incomplete_ir_prog (void) {
45   ir_prog *res;
46
47   res = xmalloc(sizeof(*res));
48   memset(res, 0, sizeof(*res));
49   irp = res;
50
51   res->kind          = k_ir_prog;
52   res->graphs        = NEW_ARR_F(ir_graph *, 0);
53   res->pseudo_graphs = NEW_ARR_F(ir_graph *, 0);
54   res->types         = NEW_ARR_F(ir_type *, 0);
55   res->modes         = NEW_ARR_F(ir_mode *, 0);
56   res->opcodes       = NEW_ARR_F(ir_op *, 0);
57
58 #ifdef DEBUG_libfirm
59   res->max_node_nr = 0;
60 #endif
61
62   return res;
63 }
64
65 /** Completes an incomplete irprog. */
66 static ir_prog *complete_ir_prog(ir_prog *irp) {
67 #define X(s) s, sizeof(s)-1
68   irp->name      = new_id_from_chars(X(INITAL_PROG_NAME));
69
70   irp->glob_type = new_type_class(new_id_from_chars(X(GLOBAL_TYPE_NAME)));
71   irp->tls_type  = new_type_struct(new_id_from_chars(X(TLS_TYPE_NAME)));
72   /* Remove these types from type list.  Must be treated differently than
73      other types. */
74   remove_irp_type(irp->glob_type);
75   remove_irp_type(irp->tls_type);
76
77   /* Set these flags for debugging. */
78   irp->glob_type->flags |= tf_global_type;
79   irp->tls_type->flags  |= tf_tls_type;
80
81   /* The global type is a class, but we cannot derive from it, so set
82      the final property to assist optimizations that checks for it. */
83   set_class_final(irp->glob_type, 1);
84
85   irp->const_code_irg   = new_const_code_irg();
86
87   irp->phase_state      = phase_building;
88   irp->outs_state       = outs_none;
89   irp->ip_outedges      = NULL;
90   irp->trouts_state     = outs_none;
91   irp->class_cast_state = ir_class_casts_transitive;
92   irp->globals_adr_taken_state = ir_address_taken_not_computed;
93
94   return irp;
95 }
96
97 /* initializes ir_prog. Constructs only the basic lists */
98 void init_irprog_1(void) {
99   new_incomplete_ir_prog();
100 }
101
102 /* Completes ir_prog. */
103 void init_irprog_2(void) {
104   complete_ir_prog(irp);
105 }
106
107 /* Create a new ir prog. Automatically called by init_firm through
108    init_irprog. */
109 ir_prog *new_ir_prog (void) {
110   return complete_ir_prog(new_incomplete_ir_prog());
111 }
112
113 /* frees all memory used by irp.  Types in type list, irgs in irg
114     list and entities in global type must be freed by hand before. */
115 void free_ir_prog(void) {
116   if (irp->glob_type)
117     free_type(irp->glob_type);
118   if (irp->tls_type)
119     free_type(irp->tls_type);
120
121   /* @@@ * free_ir_graph(irp->const_code_irg); * ?? End has no in?? */
122   DEL_ARR_F(irp->graphs);
123   DEL_ARR_F(irp->pseudo_graphs);
124   DEL_ARR_F(irp->types);
125   DEL_ARR_F(irp->modes);
126   DEL_ARR_F(irp->opcodes);
127
128   irp->name           = NULL;
129   irp->const_code_irg = NULL;
130   irp->kind           = k_BAD;
131 }
132
133 /*- Functions to access the fields of ir_prog -*/
134
135
136 /* Access the main routine of the compiled program. */
137 ir_graph *get_irp_main_irg(void) {
138   assert(irp);
139   return irp->main_irg;
140 }
141
142 void set_irp_main_irg(ir_graph *main_irg) {
143   assert (irp);
144   irp->main_irg = main_irg;
145 }
146
147 ir_type *(get_glob_type)(void) {
148   return _get_glob_type();
149 }
150
151 ir_type *(get_tls_type)(void) {
152   return _get_tls_type();
153 }
154
155 /* Adds irg to the list of ir graphs in irp. */
156 void add_irp_irg(ir_graph *irg) {
157   assert (irg != NULL);
158   assert(irp && irp->graphs);
159   ARR_APP1 (ir_graph *, irp->graphs, irg);
160 }
161
162 /* Removes irg from the list or irgs, shrinks the list by one. */
163 void remove_irp_irg_from_list(ir_graph *irg){
164   int i, found = 0;
165   assert(irg);
166   for (i = 0; i < (ARR_LEN (irp->graphs)); i++) {
167     if (irp->graphs[i] == irg) {
168       found = 1;
169       for(; i < (ARR_LEN (irp->graphs)) - 1; i++) {
170         irp->graphs[i] = irp->graphs[i+1];
171       }
172       ARR_SETLEN(ir_graph*, irp->graphs, (ARR_LEN(irp->graphs)) - 1);
173       break;
174     }
175   }
176   if (!found) {
177     for (i = 0; i < (ARR_LEN (irp->pseudo_graphs)); i++) {
178       if (irp->pseudo_graphs[i] == irg) {
179               for(; i < (ARR_LEN (irp->pseudo_graphs)) - 1; i++) {
180                 irp->pseudo_graphs[i] = irp->pseudo_graphs[i+1];
181               }
182               ARR_SETLEN(ir_graph*, irp->pseudo_graphs, (ARR_LEN(irp->pseudo_graphs)) - 1);
183               break;
184       }
185     }
186   }
187 }
188
189 /* Removes irg from the list or irgs, shrinks the list by one. */
190 void remove_irp_irg(ir_graph *irg){
191   assert(irg);
192   free_ir_graph(irg);
193   remove_irp_irg_from_list(irg);
194 }
195
196 int (get_irp_n_irgs)(void) {
197   return _get_irp_n_irgs();
198 }
199
200 ir_graph *(get_irp_irg)(int pos){
201   return _get_irp_irg(pos);
202 }
203
204 void set_irp_irg(int pos, ir_graph *irg) {
205   assert(irp && irg);
206   assert(pos < (ARR_LEN(irp->graphs)));
207   irp->graphs[pos] = irg;
208 }
209
210 /* Gets the number of graphs _and_ pseudo graphs. */
211 int get_irp_n_allirgs(void) {
212   /* We can not call get_irp_n_irgs, as we end up in a recursion ... */
213   return ARR_LEN(irp->graphs) + get_irp_n_pseudo_irgs();
214 }
215
216 /* Returns the ir graph at position pos of all graphs (including
217  pseudo graphs).  Visits first graphs, then pseudo graphs. */
218 ir_graph *get_irp_allirg(int pos) {
219   int n_irgs = ARR_LEN(irp->graphs);
220   assert(0 <= pos);
221   if (pos < n_irgs) {
222     return irp->graphs[pos];
223   } else {
224     return get_irp_pseudo_irg(pos-n_irgs);
225   }
226 }
227
228 /* Adds type to the list of types in irp. */
229 void add_irp_type(ir_type *typ) {
230   assert(typ != NULL);
231   assert(irp);
232   ARR_APP1 (ir_type *, irp->types, typ);
233 }
234
235 /* Remove type form the list of types in irp. */
236 void remove_irp_type(ir_type *typ) {
237   int i;
238   assert(typ);
239
240   for (i = ARR_LEN(irp->types) -1; i >= 0; i--) {
241     if (irp->types[i] == typ) {
242       for(; i < (ARR_LEN(irp->types)) - 1; i++) {
243         irp->types[i] = irp->types[i+1];
244       }
245       ARR_SETLEN(ir_type *, irp->types, (ARR_LEN(irp->types)) - 1);
246       break;
247     }
248   }
249 }
250
251 int (get_irp_n_types) (void) {
252   return _get_irp_n_types();
253 }
254
255 ir_type *(get_irp_type) (int pos) {
256   return _get_irp_type(pos);
257 }
258
259 void set_irp_type(int pos, ir_type *typ) {
260   assert(irp && typ);
261   assert(pos < (ARR_LEN((irp)->types)));
262   irp->types[pos] = typ;
263 }
264
265 /* Returns the number of all modes in the irp. */
266 int (get_irp_n_modes)(void) {
267   return _get_irp_n_modes();
268 }
269
270 /* Returns the mode at position pos in the irp. */
271 ir_mode *(get_irp_mode)(int pos) {
272   return _get_irp_mode(pos);
273 }
274
275 /* Adds mode to the list of modes in irp. */
276 void add_irp_mode(ir_mode *mode) {
277   assert(mode != NULL);
278   assert(irp);
279   ARR_APP1(ir_mode *, irp->modes, mode);
280 }
281
282 /* Adds opcode to the list of opcodes in irp. */
283 void add_irp_opcode(ir_op *opcode) {
284   assert(opcode != NULL);
285   assert(irp);
286   ARR_APP1(ir_op *, irp->opcodes, opcode);
287 }
288
289 /* Removes opcode from the list of opcodes and shrinks the list by one. */
290 void remove_irp_opcode(ir_op *opcode) {
291   int i;
292
293   assert(opcode);
294   for (i = ARR_LEN(irp->opcodes) -1; i >= 0; i--) {
295     if (irp->opcodes[i] == opcode) {
296       for (; i < (ARR_LEN(irp->opcodes)) - 1; i++) {
297         irp->opcodes[i] = irp->opcodes[i+1];
298       }
299       ARR_SETLEN(ir_op *, irp->opcodes, (ARR_LEN(irp->opcodes)) - 1);
300       break;
301     }
302   }
303 }
304
305 /* Returns the number of all opcodes in the irp. */
306 int (get_irp_n_opcodes)(void) {
307   return _get_irp_n_opcodes();
308 }
309
310 /* Returns the opcode at position pos in the irp. */
311 ir_op *(get_irp_opcode)(int pos) {
312   return _get_irp_opcode(pos);
313 }
314
315 /* Sets the generic function pointer of all opcodes to NULL */
316 void  clear_irp_opcodes_generic_func(void) {
317   int i;
318
319   for (i = get_irp_n_opcodes() - 1; i >= 0; --i) {
320     ir_op *op = get_irp_opcode(i);
321     op->ops.generic = (op_func)NULL;
322   }
323 }
324
325 /*- File name / executable name or the like -*/
326 void   set_irp_prog_name(ident *name) {
327   irp->name = name;
328 }
329 int irp_prog_name_is_set(void) {
330   return irp->name != new_id_from_str(INITAL_PROG_NAME);
331 }
332 ident *get_irp_prog_ident(void) {
333   return irp->name;
334 }
335 const char  *get_irp_prog_name(void) {
336   return get_id_str(irp->name);
337 }
338
339
340 ir_graph *(get_const_code_irg)(void) {
341   return _get_const_code_irg();
342 }
343
344 irg_phase_state get_irp_phase_state(void) {
345   return irp->phase_state;
346 }
347 void set_irp_phase_state(irg_phase_state s) {
348   irp->phase_state = s;
349 }
350
351 irg_outs_state get_irp_ip_outs_state(void) {
352   return irp->outs_state;
353 }
354
355 void set_irp_ip_outs_inconsistent(void) {
356   irp->outs_state = outs_inconsistent;
357 }
358
359 void set_irp_ip_outedges(ir_node ** ip_outedges) {
360   irp->ip_outedges = ip_outedges;
361 }
362
363 ir_node** get_irp_ip_outedges(void) {
364   return irp->ip_outedges;
365 }
366
367
368 irg_callee_info_state get_irp_callee_info_state(void) {
369   return irp->callee_info_state;
370 }
371
372 void set_irp_callee_info_state(irg_callee_info_state s) {
373   irp->callee_info_state = s;
374 }