Typo in comment.
[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 (is_Start(pred_pred))  {
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 (is_Call(pred_pred)) {
124                         ir_type *mtp = get_Call_type(pred_pred);
125                         tp = get_method_res_type(mtp, get_Proj_proj(n));
126                 } else if (is_Tuple(pred_pred)) {
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:
237                 tp = get_Const_type(n);
238                 break;
239         case iro_SymConst:
240                 tp = get_SymConst_value_type(n);
241                 break;
242         case iro_Sel:
243                 tp = find_pointer_type_to(get_entity_type(get_Sel_entity(n)));
244                 break;
245
246                 /* asymmetric binops */
247         case iro_Shl:
248         case iro_Shr:
249         case iro_Shrs:
250         case iro_Rotl:
251                 tp = tp1;
252                 break;
253         case iro_Cast:
254                 tp = get_Cast_type(n);
255                 break;
256         case iro_Phi: {
257                 int i;
258                 int n_preds = get_Phi_n_preds(n);
259
260                 if (n_preds == 0)
261                         break;
262
263                 /* initialize this Phi */
264                 set_irn_typeinfo_type(n, phi_cycle_type);
265
266                 /* find a first real type */
267                 for (i = 0; i < n_preds; ++i) {
268                         tp1 = compute_irn_type(get_Phi_pred(n, i));
269                         assert(tp1 != initial_type);
270                         if ((tp1 != phi_cycle_type) && (tp1 != firm_none_type))
271                                 break;
272                 }
273
274                 /* find a second real type */
275                 tp2 = tp1;
276                 for (; (i < n_preds); ++i) {
277                         tp2 = compute_irn_type(get_Phi_pred(n, i));
278                         if ((tp2 == phi_cycle_type) || (tp2 == firm_none_type)) {
279                                 tp2 = tp1;
280                                 continue;
281                         }
282                         if (tp2 != tp1) break;
283                 }
284
285                 /* printf("Types in Phi %s and %s \n", get_type_name(tp1), get_type_name(tp2)); */
286
287                 if (tp1 == tp2) { tp = tp1; break; }
288
289                 DB((dbg, SET_LEVEL_2, "Phi %ld with two different types: %s, %s: unknown type.\n", get_irn_node_nr(n),
290                         get_type_name(tp1), get_type_name(tp2)));
291                 tp = firm_unknown_type;   /* Test for supertypes? */
292                 break;
293         }
294
295         case iro_Load: {
296                 ir_node *a = get_Load_ptr(n);
297                 if (is_Sel(a))
298                         tp = get_entity_type(get_Sel_entity(a));
299                 else if (is_Pointer_type(compute_irn_type(a))) {
300                         tp = get_pointer_points_to_type(get_irn_typeinfo_type(a));
301                         if (is_Array_type(tp))
302                                 tp = get_array_element_type(tp);
303                 } else {
304                         DB((dbg, SET_LEVEL_1, "Load %ld with typeless address. result: unknown type\n", get_irn_node_nr(n)));
305                 }
306                 break;
307         }
308         case iro_Alloc:
309                 tp = find_pointer_type_to(get_Alloc_type(n));
310                 break;
311         case iro_Proj:
312                 tp = find_type_for_Proj(n);
313                 break;
314         case iro_Id:
315                 tp = compute_irn_type(get_Id_pred(n));
316                 break;
317         case iro_Unknown:
318                 tp = firm_unknown_type;
319                 break;
320         case iro_Filter:
321                 assert(0 && "Filter not implemented");
322                 break;
323
324                 /* catch special cases with fallthrough to binop/unop cases in default. */
325         case iro_Sub:
326                 if (mode_is_int(get_irn_mode(n))       &&
327                     mode_is_reference(get_irn_mode(a)) &&
328                     mode_is_reference(get_irn_mode(b))   ) {
329                         DB((dbg, SET_LEVEL_1, "Sub %ld ptr - ptr = int: unknown type\n", get_irn_node_nr(n)));
330                         tp =  firm_unknown_type; break;
331                 }
332                 /* fall through to Add. */
333         case iro_Add:
334                 if (mode_is_reference(get_irn_mode(n)) &&
335                     mode_is_reference(get_irn_mode(a)) &&
336                     mode_is_int(get_irn_mode(b))         ) {
337                         tp = tp1; break;
338                 }
339                 if (mode_is_reference(get_irn_mode(n)) &&
340                     mode_is_int(get_irn_mode(a))       &&
341                     mode_is_reference(get_irn_mode(b))    ) {
342                         tp = tp2; break;
343                 }
344                 goto default_code;
345
346         case iro_Mul:
347                 if (get_irn_mode(n) != get_irn_mode(a)) {
348                         DB((dbg, SET_LEVEL_1, "Mul %ld int1 * int1 = int2: unknown type\n", get_irn_node_nr(n)));
349                         tp = firm_unknown_type; break;
350                 }
351                 goto default_code;
352
353         case iro_Mux:
354                 a = get_Mux_true(n);
355                 b = get_Mux_false(n);
356                 tp1 = compute_irn_type(a);
357                 tp2 = compute_irn_type(b);
358                 if (tp1 == tp2)
359                         tp = tp1;
360                 break;
361
362         case iro_Bound:
363                 tp = compute_irn_type(get_Bound_index(n));
364                 break;
365         case iro_Confirm:
366                 tp = compute_irn_type(get_Confirm_value(n));
367                 break;
368         case iro_Conv:
369                 /* Conv is a unop, but changing the mode implies
370                 changing the type. */
371                 break;
372
373         default:
374 default_code:
375                 if (is_unop(n)) {
376                         /* It's not proper to walk past a Conv, so this case is handled above. */
377                         tp = tp1;
378                         break;
379                 }
380
381                 if (is_binop(n)) {
382                         if (tp1 == tp2) {
383                                 tp = tp1;
384                                 break;
385                         }
386                         if ((tp1 == phi_cycle_type) || (tp2 == phi_cycle_type)) {
387                                 tp = phi_cycle_type;
388                                 break;
389                         }
390                         DB((dbg, SET_LEVEL_2, "Binop %ld with two different types: %s, %s: unknown type \n", get_irn_node_nr(n),
391                                 get_type_name(tp1), get_type_name(tp2)));
392                         tp = firm_unknown_type;
393                         break;
394                 }
395
396                 panic(" not implemented: %+F", n);
397                 break;
398         } /* end switch */
399
400         return tp;
401 }
402
403 /** Compute the type of an IR node. */
404 static ir_type *compute_irn_type(ir_node *n) {
405         ir_type *tp = get_irn_typeinfo_type(n);
406
407         if (tp == initial_type) {
408                 tp = find_type_for_node(n);
409                 set_irn_typeinfo_type(n, tp);
410         }
411         return tp;
412 }
413
414 /**
415  * Post-Walker: computes the type for every node
416  * and store it into a map.
417  * Post-walking ensures that the types for all predecessor
418  * nodes are already computed.
419  */
420 static void compute_type(ir_node *n, void *env) {
421         ir_type *tp = get_irn_typeinfo_type(n);
422         (void) env;
423         if (tp ==  phi_cycle_type) {
424                 /* printf(" recomputing for phi_cycle_type "); DDMN(n); */
425                 set_irn_typeinfo_type(n, initial_type);
426         }
427         compute_irn_type(n);
428 }
429
430 /**
431  * Compute the types for all nodes of a graph.
432  */
433 static void analyse_irg (ir_graph *irg) {
434         set_irg_typeinfo_state(irg, ir_typeinfo_consistent);
435         irg_walk_graph(irg, NULL, compute_type, NULL);
436 }
437
438 /**
439  * Initialize the analysis by creating a phi_cycle_type and
440  * computing pointer types for all class and struct types.
441  */
442 static void init_irsimpletype(void) {
443         init_irtypeinfo();
444         if (!phi_cycle_type)
445                 phi_cycle_type = new_type_class(new_id_from_str("phi_cycle_type"));
446         precompute_pointer_types();
447 }
448
449 /* Computes type information for each node in all ir graphs. */
450 void simple_analyse_types(void) {
451         int i;
452         FIRM_DBG_REGISTER(dbg, "firm.ana.simpletype");
453
454         init_irsimpletype();
455         for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
456                 ir_graph *irg = get_irp_irg(i);
457                 analyse_irg(irg);
458         }
459         set_irp_typeinfo_state(ir_typeinfo_consistent);
460 }
461
462 void free_simple_type_information(void) {
463         free_irtypeinfo();
464
465         if (phi_cycle_type) {
466                 free_type(phi_cycle_type);
467                 phi_cycle_type = NULL;
468         }
469         set_irp_typeinfo_state(ir_typeinfo_none);
470 }