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