- get_irg_initial_exec()/set_irg_initial_exec() added
[libfirm] / ir / ana / irsimpletype.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief     Run most simple type analyses.
23  * @author    Goetz Lindenmaier
24  * @date      22.8.2003
25  * @version   $Id$
26  * @summary
27  *  Runs most simple type analyses.
28  *
29  *  We compute type information for each node.  It is derived from the
30  *  types of the origines of values, e.g. parameter types can be derived
31  *  from the method type.
32  *  The type information so far is saved in the link field.
33  */
34 #ifdef HAVE_CONFIG_H
35 # include "config.h"
36 #endif
37
38 #include "irtypeinfo.h"
39 #include "irsimpletype.h"
40
41 #include "irnode_t.h"
42 #include "irprog_t.h"
43 #include "irgwalk.h"
44 #include "ident.h"
45 #include "trouts.h"
46 #include "debug.h"
47 #include "error.h"
48
49 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL);
50
51 static ir_type *phi_cycle_type = NULL;
52
53
54 /* ------------ Building and Removing the type information  ----------- */
55
56
57 /**
58  * init type link field so that types point to their pointers.
59  */
60 static void precompute_pointer_types(void) {
61 #if 0
62         int i;
63         set_type_link(firm_unknown_type, firm_unknown_type);
64         set_type_link(firm_none_type,    firm_unknown_type);
65
66         for (i = get_irp_n_types() - 1; i >= 0; --i)
67                 set_type_link(get_irp_type(i), (void *)firm_unknown_type);
68
69         for (i = get_irp_n_types() - 1; i >= 0; --i) {
70                 ir_type *tp = get_irp_type(i);
71                 if (is_Pointer_type(tp))
72                         set_type_link(get_pointer_points_to_type(tp), (void *)tp);
73         }
74 #else
75         compute_trouts();
76 #endif
77 }
78
79 /**
80  * Returns a pointer to type which was stored in the link field
81  * to speed up search.
82  */
83 static ir_type *find_pointer_type_to (ir_type *tp) {
84 #if 0
85         return (ir_type *)get_type_link(tp);
86 #else
87         if (get_type_n_pointertypes_to(tp) > 0)
88                 return get_type_pointertype_to(tp, 0);
89         else
90                 return firm_unknown_type;
91 #endif
92 }
93
94 static ir_type *compute_irn_type(ir_node *n);
95
96 /**
97  * Try to determine a type for a Proj node.
98  * If a type cannot be determined, return @p firm_none_type.
99  */
100 static ir_type *find_type_for_Proj(ir_node *n) {
101         ir_type *tp;
102
103         /* Avoid nested Tuples. */
104         ir_node *pred = skip_Tuple(get_Proj_pred(n));
105         ir_mode *m = get_irn_mode(n);
106
107         if (m == mode_T  ||
108             m == mode_BB ||
109             m == mode_X  ||
110             m == mode_M  ||
111             m == mode_b    )
112                 return firm_none_type;
113
114         switch (get_irn_opcode(pred)) {
115         case iro_Proj: {
116                 ir_node *pred_pred;
117                 /* Deal with Start / Call here: we need to know the Proj Nr. */
118                 assert(get_irn_mode(pred) == mode_T);
119                 pred_pred = get_Proj_pred(pred);
120                 if (get_irn_op(pred_pred) == op_Start)  {
121                         ir_type *mtp = get_entity_type(get_irg_entity(get_irn_irg(pred_pred)));
122                         tp = get_method_param_type(mtp, get_Proj_proj(n));
123                 } else if (get_irn_op(pred_pred) == op_Call) {
124                         ir_type *mtp = get_Call_type(pred_pred);
125                         tp = get_method_res_type(mtp, get_Proj_proj(n));
126                 } else if (get_irn_op(pred_pred) == op_Tuple) {
127                         panic("Encountered nested Tuple");
128                 } else {
129                         DB((dbg, SET_LEVEL_1, "Proj %ld from Proj from ??: unknown type\n", get_irn_node_nr(n)));
130                         tp = firm_unknown_type;
131                 }
132                 break;
133         }
134         case iro_Start:
135                 /* frame pointer, globals and tls */
136                 switch (get_Proj_proj(n)) {
137                 case pn_Start_P_frame_base:
138                         tp = find_pointer_type_to(get_irg_frame_type(get_irn_irg(pred)));
139                         break;
140                 case pn_Start_P_tls:
141                         tp = find_pointer_type_to(get_tls_type());
142                         break;
143                 case pn_Start_P_value_arg_base:
144                         DB((dbg, SET_LEVEL_1, "Value arg base proj %ld from Start: unknown type\n", get_irn_node_nr(n)));
145                         tp =  firm_unknown_type; /* find_pointer_type_to(get....(get_entity_type(get_irg_entity(get_irn_irg(pred))))); */
146                         break;
147                 default:
148                         DB((dbg, SET_LEVEL_1, "Proj %ld %ld from Start: unknown type\n", get_Proj_proj(n), get_irn_node_nr(n)));
149                         tp = firm_unknown_type;
150                 }
151                 break;
152         case iro_Call:
153                 /* value args pointer */
154                 if (get_Proj_proj(n) == pn_Call_P_value_res_base) {
155                         DB((dbg, SET_LEVEL_1, "Value res base Proj %ld from Call: unknown type\n", get_irn_node_nr(n)));
156                         tp = firm_unknown_type; /* find_pointer_type_to(get....get_Call_type(pred)); */
157                 } else {
158                         DB((dbg, SET_LEVEL_1, "Proj %ld %ld from Call: unknown type\n", get_Proj_proj(n), get_irn_node_nr(n)));
159                         tp = firm_unknown_type;
160                 }
161                 break;
162         case iro_Tuple:
163                 tp = compute_irn_type(get_Tuple_pred(pred, get_Proj_proj(n)));
164                 break;
165         default:
166                 tp = compute_irn_type(pred);
167         }
168
169         return tp;
170 }
171
172 /**
173  * Try to determine the type of a node.
174  * If a type cannot be determined, return @p firm_none_type.
175  */
176 static ir_type *find_type_for_node(ir_node *n) {
177         ir_type *tp = firm_unknown_type;
178         ir_type *tp1 = NULL, *tp2 = NULL;
179         ir_node *a = NULL, *b = NULL;
180
181         if (is_unop(n)) {
182                 a = get_unop_op(n);
183                 tp1 = compute_irn_type(a);
184         }
185         if (is_binop(n)) {
186                 a = get_binop_left(n);
187                 b = get_binop_right(n);
188                 tp1 = compute_irn_type(a);
189                 tp2 = compute_irn_type(b);
190         }
191
192         switch (get_irn_opcode(n)) {
193
194         case iro_InstOf: {
195                 assert(0 && "op_InstOf not supported");
196         } break;
197
198                 /* has no type */
199         case iro_Return: {
200                 /* Check returned type. */
201                 /*
202                 int i;
203                 ir_type *meth_type = get_entity_type(get_irg_entity(current_ir_graph));
204                 for (i = 0; i < get_method_n_ress(meth_type); i++) {
205                 ir_type *res_type = get_method_res_type(meth_type, i);
206                 ir_type *ana_res_type = get_irn_type(get_Return_res(n, i));
207                 if (ana_res_type == firm_unknown_type) continue;
208                 if (res_type != ana_res_type && "return value has wrong type") {
209                 DDMN(n);
210                 assert(res_type == ana_res_type && "return value has wrong type");
211                 }
212                 }
213                 */
214         }
215         case iro_Block:
216         case iro_Start:
217         case iro_End:
218         case iro_Jmp:
219         case iro_Cond:
220         case iro_Raise:
221         case iro_Call:
222         case iro_Cmp:
223         case iro_Store:
224         case iro_Free:
225         case iro_Sync:
226         case iro_Tuple:
227         case iro_Bad:
228         case iro_NoMem:
229         case iro_Break:
230         case iro_CallBegin:
231         case iro_EndReg:
232         case iro_EndExcept:
233                 break;
234
235                 /* compute the type */
236         case iro_Const:  tp = get_Const_type(n); break;
237         case iro_SymConst:
238                 tp = get_SymConst_value_type(n); break;
239         case iro_Sel:
240                 tp = find_pointer_type_to(get_entity_type(get_Sel_entity(n))); break;
241                 /* asymmetric binops */
242         case iro_Shl:
243         case iro_Shr:
244         case iro_Shrs:
245         case iro_Rot:
246                 tp = tp1;  break;
247         case iro_Cast:
248                 tp = get_Cast_type(n);  break;
249         case iro_Phi: {
250                 int i;
251                 int n_preds = get_Phi_n_preds(n);
252
253                 if (n_preds == 0)
254                         break;
255
256                 /* initialize this Phi */
257                 set_irn_typeinfo_type(n, phi_cycle_type);
258
259                 /* find a first real type */
260                 for (i = 0; i < n_preds; ++i) {
261                         tp1 = compute_irn_type(get_Phi_pred(n, i));
262                         assert(tp1 != initial_type);
263                         if ((tp1 != phi_cycle_type) && (tp1 != firm_none_type))
264                                 break;
265                 }
266
267                 /* find a second real type */
268                 tp2 = tp1;
269                 for (; (i < n_preds); ++i) {
270                         tp2 = compute_irn_type(get_Phi_pred(n, i));
271                         if ((tp2 == phi_cycle_type) || (tp2 == firm_none_type)) {
272                                 tp2 = tp1;
273                                 continue;
274                         }
275                         if (tp2 != tp1) break;
276                 }
277
278                 /* printf("Types in Phi %s and %s \n", get_type_name(tp1), get_type_name(tp2)); */
279
280                 if (tp1 == tp2) { tp = tp1; break; }
281
282                 DB((dbg, SET_LEVEL_2, "Phi %ld with two different types: %s, %s: unknown type.\n", get_irn_node_nr(n),
283                         get_type_name(tp1), get_type_name(tp2)));
284                 tp = firm_unknown_type;   /* Test for supertypes? */
285         } break;
286
287         case iro_Load: {
288                 ir_node *a = get_Load_ptr(n);
289                 if (is_Sel(a))
290                         tp = get_entity_type(get_Sel_entity(a));
291                 else if (is_Pointer_type(compute_irn_type(a))) {
292                         tp = get_pointer_points_to_type(get_irn_typeinfo_type(a));
293                         if (is_Array_type(tp))
294                                 tp = get_array_element_type(tp);
295                 } else {
296                         DB((dbg, SET_LEVEL_1, "Load %ld with typeless address. result: unknown type\n", get_irn_node_nr(n)));
297                 }
298         } break;
299         case iro_Alloc:
300                 tp = find_pointer_type_to(get_Alloc_type(n));  break;
301         case iro_Proj:
302                 tp = find_type_for_Proj(n); break;
303         case iro_Id:
304                 tp = compute_irn_type(get_Id_pred(n)); break;
305         case iro_Unknown:
306                 tp = firm_unknown_type;  break;
307         case iro_Filter:
308                 assert(0 && "Filter not implemented"); break;
309
310                 /* catch special cases with fallthrough to binop/unop cases in default. */
311         case iro_Sub: {
312                 if (mode_is_int(get_irn_mode(n))       &&
313                     mode_is_reference(get_irn_mode(a)) &&
314                     mode_is_reference(get_irn_mode(b))   ) {
315                         DB((dbg, SET_LEVEL_1, "Sub %ld ptr - ptr = int: unknown type\n", get_irn_node_nr(n)));
316                         tp =  firm_unknown_type; break;
317                 }
318         } /* fall through to Add. */
319         case iro_Add: {
320                 if (mode_is_reference(get_irn_mode(n)) &&
321                     mode_is_reference(get_irn_mode(a)) &&
322                     mode_is_int(get_irn_mode(b))         ) {
323                         tp = tp1; break;
324                 }
325                 if (mode_is_reference(get_irn_mode(n)) &&
326                     mode_is_int(get_irn_mode(a))       &&
327                     mode_is_reference(get_irn_mode(b))    ) {
328                         tp = tp2; break;
329                 }
330                 goto default_code;
331         } break;
332
333         case iro_Mul: {
334                 if (get_irn_mode(n) != get_irn_mode(a)) {
335                         DB((dbg, SET_LEVEL_1, "Mul %ld int1 * int1 = int2: unknown type\n", get_irn_node_nr(n)));
336                         tp = firm_unknown_type; break;
337                 }
338                 goto default_code;
339         } break;
340
341         case iro_Mux: {
342                 a = get_Mux_true(n);
343                 b = get_Mux_false(n);
344                 tp1 = compute_irn_type(a);
345                 tp2 = compute_irn_type(b);
346                 if (tp1 == tp2)
347                         tp = tp1;
348         } break;
349
350         case iro_Psi: {
351                 int i, n_conds = get_Psi_n_conds(n);
352                 tp1 = compute_irn_type(get_Psi_default(n));
353
354                 for (i = 0; i < n_conds; ++i) {
355                         tp2 = compute_irn_type(get_Psi_val(n, i));
356                         if (tp2 != tp1)
357                                 break;
358                 }
359                 if (tp1 == tp2)
360                         tp = tp1;
361         } break;
362
363         case iro_Bound:
364                 tp = compute_irn_type(get_Bound_index(n));
365                 break;
366         case iro_Confirm:
367                 tp = compute_irn_type(get_Confirm_value(n));
368                 break;
369         case iro_Conv:
370                 /* Conv is a unop, but changing the mode implies
371                 changing the type. */
372                 break;
373
374         default:
375 default_code: {
376
377                 if (is_unop(n)) {
378                         /* It's not proper to walk past a Conv, so this case is handled above. */
379                         tp = tp1;
380                         break;
381                 }
382
383                 if (is_binop(n)) {
384                         if (tp1 == tp2) {
385                                 tp = tp1;
386                                 break;
387                         }
388                         if((tp1 == phi_cycle_type) || (tp2 == phi_cycle_type)) {
389                                 tp = phi_cycle_type;
390                                 break;
391                         }
392                         DB((dbg, SET_LEVEL_2, "Binop %ld with two different types: %s, %s: unknown type \n", get_irn_node_nr(n),
393                                 get_type_name(tp1), get_type_name(tp2)));
394                         tp = firm_unknown_type;
395                         break;
396                 }
397
398                 panic(" not implemented: %+F", n);
399         } break; /* default */
400         } /* end switch */
401
402         return tp;
403 }
404
405 /** Compute the type of an IR node. */
406 static ir_type *compute_irn_type(ir_node *n) {
407         ir_type *tp = get_irn_typeinfo_type(n);
408
409         if (tp == initial_type) {
410                 tp = find_type_for_node(n);
411                 set_irn_typeinfo_type(n, tp);
412         }
413         return tp;
414 }
415
416 /**
417  * Post-Walker: computes the type for every node
418  * and store it into a map.
419  * Post-walking ensures that the types for all predecessor
420  * nodes are already computed.
421  */
422 static void compute_type(ir_node *n, void *env) {
423         ir_type *tp = get_irn_typeinfo_type(n);
424         (void) env;
425         if (tp ==  phi_cycle_type) {
426                 /* printf(" recomputing for phi_cycle_type "); DDMN(n); */
427                 set_irn_typeinfo_type(n, initial_type);
428         }
429         compute_irn_type(n);
430 }
431
432 /**
433  * Compute the types for all nodes of a graph.
434  */
435 static void analyse_irg (ir_graph *irg) {
436         set_irg_typeinfo_state(irg, ir_typeinfo_consistent);
437         irg_walk_graph(irg, NULL, compute_type, NULL);
438 }
439
440 /**
441  * Initialize the analysis by creating a phi_cycle_type and
442  * computing pointer types for all class and struct types.
443  */
444 static void init_irsimpletype(void) {
445         init_irtypeinfo();
446         if (!phi_cycle_type)
447                 phi_cycle_type = new_type_class(new_id_from_str("phi_cycle_type"));
448         precompute_pointer_types();
449 }
450
451 /* Computes type information for each node in all ir graphs. */
452 void simple_analyse_types(void) {
453         int i;
454         FIRM_DBG_REGISTER(dbg, "firm.ana.simpletype");
455
456         init_irsimpletype();
457         for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
458                 ir_graph *irg = get_irp_irg(i);
459                 analyse_irg(irg);
460         }
461         set_irp_typeinfo_state(ir_typeinfo_consistent);
462 }
463
464 void free_simple_type_information(void) {
465         free_irtypeinfo();
466
467         if (phi_cycle_type) {
468                 free_type(phi_cycle_type);
469                 phi_cycle_type = NULL;
470         }
471         set_irp_typeinfo_state(ir_typeinfo_none);
472 }