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