backend_marked was a buggy/wrong concept, removed it
[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 #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                 default:
142                         DB((dbg, SET_LEVEL_1, "Proj %ld %ld from Start: unknown type\n", get_Proj_proj(n), get_irn_node_nr(n)));
143                         tp = firm_unknown_type;
144                 }
145                 break;
146         case iro_Call:
147                 /* value args pointer */
148                 if (get_Proj_proj(n) == pn_Call_P_value_res_base) {
149                         DB((dbg, SET_LEVEL_1, "Value res base Proj %ld from Call: unknown type\n", get_irn_node_nr(n)));
150                         tp = firm_unknown_type; /* find_pointer_type_to(get....get_Call_type(pred)); */
151                 } else {
152                         DB((dbg, SET_LEVEL_1, "Proj %ld %ld from Call: unknown type\n", get_Proj_proj(n), get_irn_node_nr(n)));
153                         tp = firm_unknown_type;
154                 }
155                 break;
156         case iro_Tuple:
157                 tp = compute_irn_type(get_Tuple_pred(pred, get_Proj_proj(n)));
158                 break;
159         default:
160                 tp = compute_irn_type(pred);
161         }
162
163         return tp;
164 }
165
166 /**
167  * Try to determine the type of a node.
168  * If a type cannot be determined, return @p firm_none_type.
169  */
170 static ir_type *find_type_for_node(ir_node *n) {
171         ir_type *tp = firm_unknown_type;
172         ir_type *tp1 = NULL, *tp2 = NULL;
173         ir_node *a = NULL, *b = NULL;
174
175         if (is_unop(n)) {
176                 a = get_unop_op(n);
177                 tp1 = compute_irn_type(a);
178         }
179         if (is_binop(n)) {
180                 a = get_binop_left(n);
181                 b = get_binop_right(n);
182                 tp1 = compute_irn_type(a);
183                 tp2 = compute_irn_type(b);
184         }
185
186         switch (get_irn_opcode(n)) {
187
188         case iro_InstOf:
189                 assert(0 && "op_InstOf not supported");
190                 break;
191
192                 /* has no type */
193         case iro_Return: {
194                 /* Check returned type. */
195                 /*
196                 int i;
197                 ir_type *meth_type = get_entity_type(get_irg_entity(current_ir_graph));
198                 for (i = 0; i < get_method_n_ress(meth_type); i++) {
199                 ir_type *res_type = get_method_res_type(meth_type, i);
200                 ir_type *ana_res_type = get_irn_type(get_Return_res(n, i));
201                 if (ana_res_type == firm_unknown_type) continue;
202                 if (res_type != ana_res_type && "return value has wrong type") {
203                 DDMN(n);
204                 assert(res_type == ana_res_type && "return value has wrong type");
205                 }
206                 }
207                 */
208         }
209         case iro_Block:
210         case iro_Start:
211         case iro_End:
212         case iro_Jmp:
213         case iro_Cond:
214         case iro_Raise:
215         case iro_Call:
216         case iro_Cmp:
217         case iro_Store:
218         case iro_Free:
219         case iro_Sync:
220         case iro_Tuple:
221         case iro_Bad:
222         case iro_NoMem:
223         case iro_Break:
224         case iro_CallBegin:
225         case iro_EndReg:
226         case iro_EndExcept:
227                 break;
228
229                 /* compute the type */
230         case iro_Const:
231                 tp = get_Const_type(n);
232                 break;
233         case iro_SymConst:
234                 tp = get_SymConst_value_type(n);
235                 break;
236         case iro_Sel:
237                 tp = find_pointer_type_to(get_entity_type(get_Sel_entity(n)));
238                 break;
239
240                 /* asymmetric binops */
241         case iro_Shl:
242         case iro_Shr:
243         case iro_Shrs:
244         case iro_Rotl:
245                 tp = tp1;
246                 break;
247         case iro_Cast:
248                 tp = get_Cast_type(n);
249                 break;
250         case iro_Phi: {
251                 int i;
252                 int n_preds = get_Phi_n_preds(n);
253
254                 if (n_preds == 0)
255                         break;
256
257                 /* initialize this Phi */
258                 set_irn_typeinfo_type(n, phi_cycle_type);
259
260                 /* find a first real type */
261                 for (i = 0; i < n_preds; ++i) {
262                         tp1 = compute_irn_type(get_Phi_pred(n, i));
263                         assert(tp1 != initial_type);
264                         if ((tp1 != phi_cycle_type) && (tp1 != firm_none_type))
265                                 break;
266                 }
267
268                 /* find a second real type */
269                 tp2 = tp1;
270                 for (; (i < n_preds); ++i) {
271                         tp2 = compute_irn_type(get_Phi_pred(n, i));
272                         if ((tp2 == phi_cycle_type) || (tp2 == firm_none_type)) {
273                                 tp2 = tp1;
274                                 continue;
275                         }
276                         if (tp2 != tp1) break;
277                 }
278
279                 /* printf("Types in Phi %s and %s \n", get_type_name(tp1), get_type_name(tp2)); */
280
281                 if (tp1 == tp2) { tp = tp1; break; }
282
283                 DB((dbg, SET_LEVEL_2, "Phi %ld with two different types: %+F, %+F: unknown type.\n", get_irn_node_nr(n), tp1, tp2));
284                 tp = firm_unknown_type;   /* Test for supertypes? */
285                 break;
286         }
287
288         case iro_Load: {
289                 ir_node *a = get_Load_ptr(n);
290                 if (is_Sel(a))
291                         tp = get_entity_type(get_Sel_entity(a));
292                 else if (is_Pointer_type(compute_irn_type(a))) {
293                         tp = get_pointer_points_to_type(get_irn_typeinfo_type(a));
294                         if (is_Array_type(tp))
295                                 tp = get_array_element_type(tp);
296                 } else {
297                         DB((dbg, SET_LEVEL_1, "Load %ld with typeless address. result: unknown type\n", get_irn_node_nr(n)));
298                 }
299                 break;
300         }
301         case iro_Alloc:
302                 tp = find_pointer_type_to(get_Alloc_type(n));
303                 break;
304         case iro_Proj:
305                 tp = find_type_for_Proj(n);
306                 break;
307         case iro_Id:
308                 tp = compute_irn_type(get_Id_pred(n));
309                 break;
310         case iro_Unknown:
311                 tp = firm_unknown_type;
312                 break;
313         case iro_Filter:
314                 assert(0 && "Filter not implemented");
315                 break;
316
317                 /* catch special cases with fallthrough to binop/unop cases in default. */
318         case iro_Sub:
319                 if (mode_is_int(get_irn_mode(n))       &&
320                     mode_is_reference(get_irn_mode(a)) &&
321                     mode_is_reference(get_irn_mode(b))   ) {
322                         DB((dbg, SET_LEVEL_1, "Sub %ld ptr - ptr = int: unknown type\n", get_irn_node_nr(n)));
323                         tp =  firm_unknown_type; break;
324                 }
325                 /* fall through to Add. */
326         case iro_Add:
327                 if (mode_is_reference(get_irn_mode(n)) &&
328                     mode_is_reference(get_irn_mode(a)) &&
329                     mode_is_int(get_irn_mode(b))         ) {
330                         tp = tp1; break;
331                 }
332                 if (mode_is_reference(get_irn_mode(n)) &&
333                     mode_is_int(get_irn_mode(a))       &&
334                     mode_is_reference(get_irn_mode(b))    ) {
335                         tp = tp2; break;
336                 }
337                 goto default_code;
338
339         case iro_Mul:
340                 if (get_irn_mode(n) != get_irn_mode(a)) {
341                         DB((dbg, SET_LEVEL_1, "Mul %ld int1 * int1 = int2: unknown type\n", get_irn_node_nr(n)));
342                         tp = firm_unknown_type; break;
343                 }
344                 goto default_code;
345
346         case iro_Mux:
347                 a = get_Mux_true(n);
348                 b = get_Mux_false(n);
349                 tp1 = compute_irn_type(a);
350                 tp2 = compute_irn_type(b);
351                 if (tp1 == tp2)
352                         tp = tp1;
353                 break;
354
355         case iro_Bound:
356                 tp = compute_irn_type(get_Bound_index(n));
357                 break;
358         case iro_Confirm:
359                 tp = compute_irn_type(get_Confirm_value(n));
360                 break;
361         case iro_Conv:
362                 /* Conv is a unop, but changing the mode implies
363                 changing the type. */
364                 break;
365
366         default:
367 default_code:
368                 if (is_unop(n)) {
369                         /* It's not proper to walk past a Conv, so this case is handled above. */
370                         tp = tp1;
371                         break;
372                 }
373
374                 if (is_binop(n)) {
375                         if (tp1 == tp2) {
376                                 tp = tp1;
377                                 break;
378                         }
379                         if ((tp1 == phi_cycle_type) || (tp2 == phi_cycle_type)) {
380                                 tp = phi_cycle_type;
381                                 break;
382                         }
383                         DB((dbg, SET_LEVEL_2, "Binop %ld with two different types: %+F, %+F: unknown type\n", get_irn_node_nr(n), tp1, tp2));
384                         tp = firm_unknown_type;
385                         break;
386                 }
387
388                 panic(" not implemented: %+F", n);
389                 break;
390         } /* end switch */
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         return tp;
404 }
405
406 /**
407  * Post-Walker: computes the type for every node
408  * and store it into a map.
409  * Post-walking ensures that the types for all predecessor
410  * nodes are already computed.
411  */
412 static void compute_type(ir_node *n, void *env) {
413         ir_type *tp = get_irn_typeinfo_type(n);
414         (void) env;
415         if (tp ==  phi_cycle_type) {
416                 /* printf(" recomputing for phi_cycle_type "); DDMN(n); */
417                 set_irn_typeinfo_type(n, initial_type);
418         }
419         compute_irn_type(n);
420 }
421
422 /**
423  * Compute the types for all nodes of a graph.
424  */
425 static void analyse_irg (ir_graph *irg) {
426         set_irg_typeinfo_state(irg, ir_typeinfo_consistent);
427         irg_walk_graph(irg, NULL, compute_type, NULL);
428 }
429
430 /**
431  * Initialize the analysis by creating a phi_cycle_type and
432  * computing pointer types for all class and struct types.
433  */
434 static void init_irsimpletype(void) {
435         init_irtypeinfo();
436         if (!phi_cycle_type)
437                 phi_cycle_type = new_type_class(new_id_from_str("phi_cycle_type"));
438         precompute_pointer_types();
439 }
440
441 /* Computes type information for each node in all ir graphs. */
442 void simple_analyse_types(void) {
443         int i;
444         FIRM_DBG_REGISTER(dbg, "firm.ana.simpletype");
445
446         init_irsimpletype();
447         for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
448                 ir_graph *irg = get_irp_irg(i);
449                 analyse_irg(irg);
450         }
451         set_irp_typeinfo_state(ir_typeinfo_consistent);
452 }
453
454 void free_simple_type_information(void) {
455         free_irtypeinfo();
456
457         if (phi_cycle_type) {
458                 free_type(phi_cycle_type);
459                 phi_cycle_type = NULL;
460         }
461         set_irp_typeinfo_state(ir_typeinfo_none);
462 }