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