combos constant dataflow analysis has to be consistent with the localopt; this should...
[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  * @brief
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 {
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 {
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 {
102         ir_type *tp;
103
104         /* Avoid nested Tuples. */
105         ir_node *pred = skip_Tuple(get_Proj_pred(n));
106         ir_mode *m = get_irn_mode(n);
107
108         if (m == mode_T  ||
109             m == mode_BB ||
110             m == mode_X  ||
111             m == mode_M  ||
112             m == mode_b    )
113                 return firm_none_type;
114
115         switch (get_irn_opcode(pred)) {
116         case iro_Proj: {
117                 ir_node *pred_pred;
118                 /* Deal with Start / Call here: we need to know the Proj Nr. */
119                 assert(get_irn_mode(pred) == mode_T);
120                 pred_pred = get_Proj_pred(pred);
121                 if (is_Start(pred_pred))  {
122                         ir_type *mtp = get_entity_type(get_irg_entity(get_irn_irg(pred_pred)));
123                         tp = get_method_param_type(mtp, get_Proj_proj(n));
124                 } else if (is_Call(pred_pred)) {
125                         ir_type *mtp = get_Call_type(pred_pred);
126                         tp = get_method_res_type(mtp, get_Proj_proj(n));
127                 } else if (is_Tuple(pred_pred)) {
128                         panic("Encountered nested Tuple");
129                 } else {
130                         DB((dbg, SET_LEVEL_1, "Proj %ld from Proj from ??: unknown type\n", get_irn_node_nr(n)));
131                         tp = firm_unknown_type;
132                 }
133                 break;
134         }
135         case iro_Start:
136                 /* frame pointer, globals and tls */
137                 switch (get_Proj_proj(n)) {
138                 case pn_Start_P_frame_base:
139                         tp = find_pointer_type_to(get_irg_frame_type(get_irn_irg(pred)));
140                         break;
141                 case pn_Start_P_tls:
142                         tp = find_pointer_type_to(get_tls_type());
143                         break;
144                 default:
145                         DB((dbg, SET_LEVEL_1, "Proj %ld %ld from Start: unknown type\n", get_Proj_proj(n), get_irn_node_nr(n)));
146                         tp = firm_unknown_type;
147                 }
148                 break;
149         case iro_Call:
150                 /* value args pointer */
151                 if (get_Proj_proj(n) == pn_Call_P_value_res_base) {
152                         DB((dbg, SET_LEVEL_1, "Value res base Proj %ld from Call: unknown type\n", get_irn_node_nr(n)));
153                         tp = firm_unknown_type; /* find_pointer_type_to(get....get_Call_type(pred)); */
154                 } else {
155                         DB((dbg, SET_LEVEL_1, "Proj %ld %ld from Call: unknown type\n", get_Proj_proj(n), get_irn_node_nr(n)));
156                         tp = firm_unknown_type;
157                 }
158                 break;
159         case iro_Tuple:
160                 tp = compute_irn_type(get_Tuple_pred(pred, get_Proj_proj(n)));
161                 break;
162         default:
163                 tp = compute_irn_type(pred);
164         }
165
166         return tp;
167 }
168
169 /**
170  * Try to determine the type of a node.
171  * If a type cannot be determined, return @p firm_none_type.
172  */
173 static ir_type *find_type_for_node(ir_node *n)
174 {
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: %+F, %+F: unknown type.\n", get_irn_node_nr(n), tp1, tp2));
288                 tp = firm_unknown_type;   /* Test for supertypes? */
289                 break;
290         }
291
292         case iro_Load: {
293                 ir_node *a = get_Load_ptr(n);
294                 if (is_Sel(a))
295                         tp = get_entity_type(get_Sel_entity(a));
296                 else if (is_Pointer_type(compute_irn_type(a))) {
297                         tp = get_pointer_points_to_type(get_irn_typeinfo_type(a));
298                         if (is_Array_type(tp))
299                                 tp = get_array_element_type(tp);
300                 } else {
301                         DB((dbg, SET_LEVEL_1, "Load %ld with typeless address. result: unknown type\n", get_irn_node_nr(n)));
302                 }
303                 break;
304         }
305         case iro_Alloc:
306                 tp = find_pointer_type_to(get_Alloc_type(n));
307                 break;
308         case iro_Proj:
309                 tp = find_type_for_Proj(n);
310                 break;
311         case iro_Id:
312                 tp = compute_irn_type(get_Id_pred(n));
313                 break;
314         case iro_Unknown:
315                 tp = firm_unknown_type;
316                 break;
317         case iro_Filter:
318                 assert(0 && "Filter not implemented");
319                 break;
320
321                 /* catch special cases with fallthrough to binop/unop cases in default. */
322         case iro_Sub:
323                 if (mode_is_int(get_irn_mode(n))       &&
324                     mode_is_reference(get_irn_mode(a)) &&
325                     mode_is_reference(get_irn_mode(b))   ) {
326                         DB((dbg, SET_LEVEL_1, "Sub %ld ptr - ptr = int: unknown type\n", get_irn_node_nr(n)));
327                         tp =  firm_unknown_type; break;
328                 }
329                 /* fall through to Add. */
330         case iro_Add:
331                 if (mode_is_reference(get_irn_mode(n)) &&
332                     mode_is_reference(get_irn_mode(a)) &&
333                     mode_is_int(get_irn_mode(b))         ) {
334                         tp = tp1; break;
335                 }
336                 if (mode_is_reference(get_irn_mode(n)) &&
337                     mode_is_int(get_irn_mode(a))       &&
338                     mode_is_reference(get_irn_mode(b))    ) {
339                         tp = tp2; break;
340                 }
341                 goto default_code;
342
343         case iro_Mul:
344                 if (get_irn_mode(n) != get_irn_mode(a)) {
345                         DB((dbg, SET_LEVEL_1, "Mul %ld int1 * int1 = int2: unknown type\n", get_irn_node_nr(n)));
346                         tp = firm_unknown_type; break;
347                 }
348                 goto default_code;
349
350         case iro_Mux:
351                 a = get_Mux_true(n);
352                 b = get_Mux_false(n);
353                 tp1 = compute_irn_type(a);
354                 tp2 = compute_irn_type(b);
355                 if (tp1 == tp2)
356                         tp = tp1;
357                 break;
358
359         case iro_Bound:
360                 tp = compute_irn_type(get_Bound_index(n));
361                 break;
362         case iro_Confirm:
363                 tp = compute_irn_type(get_Confirm_value(n));
364                 break;
365         case iro_Conv:
366                 /* Conv is a unop, but changing the mode implies
367                 changing the type. */
368                 break;
369
370         default:
371 default_code:
372                 if (is_unop(n)) {
373                         /* It's not proper to walk past a Conv, so this case is handled above. */
374                         tp = tp1;
375                         break;
376                 }
377
378                 if (is_binop(n)) {
379                         if (tp1 == tp2) {
380                                 tp = tp1;
381                                 break;
382                         }
383                         if ((tp1 == phi_cycle_type) || (tp2 == phi_cycle_type)) {
384                                 tp = phi_cycle_type;
385                                 break;
386                         }
387                         DB((dbg, SET_LEVEL_2, "Binop %ld with two different types: %+F, %+F: unknown type\n", get_irn_node_nr(n), tp1, tp2));
388                         tp = firm_unknown_type;
389                         break;
390                 }
391
392                 panic(" not implemented: %+F", n);
393         } /* end switch */
394
395         return tp;
396 }
397
398 /** Compute the type of an IR node. */
399 static ir_type *compute_irn_type(ir_node *n)
400 {
401         ir_type *tp = get_irn_typeinfo_type(n);
402
403         if (tp == initial_type) {
404                 tp = find_type_for_node(n);
405                 set_irn_typeinfo_type(n, tp);
406         }
407         return tp;
408 }
409
410 /**
411  * Post-Walker: computes the type for every node
412  * and store it into a map.
413  * Post-walking ensures that the types for all predecessor
414  * nodes are already computed.
415  */
416 static void compute_type(ir_node *n, void *env)
417 {
418         ir_type *tp = get_irn_typeinfo_type(n);
419         (void) env;
420         if (tp ==  phi_cycle_type) {
421                 /* printf(" recomputing for phi_cycle_type "); DDMN(n); */
422                 set_irn_typeinfo_type(n, initial_type);
423         }
424         compute_irn_type(n);
425 }
426
427 /**
428  * Compute the types for all nodes of a graph.
429  */
430 static void analyse_irg (ir_graph *irg)
431 {
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 {
442         init_irtypeinfo();
443         if (!phi_cycle_type)
444                 phi_cycle_type = new_type_class(new_id_from_str("phi_cycle_type"));
445         precompute_pointer_types();
446 }
447
448 /* Computes type information for each node in all ir graphs. */
449 void simple_analyse_types(void)
450 {
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 {
464         free_irtypeinfo();
465
466         if (phi_cycle_type) {
467                 free_type(phi_cycle_type);
468                 phi_cycle_type = NULL;
469         }
470         set_irp_typeinfo_state(ir_typeinfo_none);
471 }