6 * File name: ir/ana/irsimpletype.c
7 * Purpose: Run most simple type analyses.
8 * Author: Goetz Lindenmaier
12 * Copyright: (c) 2003 Universität Karlsruhe
13 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
16 * We compute type information for each node. It is derived from the
17 * types of the origines of values, e.g. parameter types can be derived
18 * from the method type.
19 * The type information so far is saved in the link field.
24 # include "irtypeinfo.h"
25 # include "irsimpletype.h"
31 #define VERBOSE_UNKNOWN_TYPE(s) printf s
33 static type *phi_cycle_type = NULL;
36 /* ------------ Building and Removing the type information ----------- */
39 /* init type link field so that types point to their pointers. */
40 void precompute_pointer_types(void) {
42 set_type_link(unknown_type, unknown_type);
43 set_type_link(none_type, unknown_type);
44 for (i = 0; i < get_irp_n_types(); ++i)
45 set_type_link(get_irp_type(i), (void *)unknown_type);
46 for (i = get_irp_n_types()-1; i>=0; --i) {
47 type *tp = get_irp_type(i);
48 if (is_pointer_type(tp))
49 set_type_link(get_pointer_points_to_type(tp), (void *)tp);
53 /* Store pointer in link to speed up search of pointer type. */
54 static type *find_pointer_type_to (type *tp) {
55 return (type *)get_type_link(tp);
58 static type* compute_irn_type(ir_node *n);
60 static type *find_type_for_Proj(ir_node *n) {
62 ir_node *pred = skip_Tuple(get_Proj_pred(n));
63 ir_mode *m = get_irn_mode(n);
72 switch(get_irn_opcode(pred)) {
75 /* Deal with Start / Call here: we need to know the Proj Nr. */
76 assert(get_irn_mode(pred) == mode_T);
77 pred_pred = get_Proj_pred(pred);
78 if (get_irn_op(pred_pred) == op_Start) {
79 type *mtp = get_entity_type(get_irg_ent(get_Start_irg(pred_pred)));
80 tp = get_method_param_type(mtp, get_Proj_proj(n));
81 } else if (get_irn_op(pred_pred) == op_Call) {
82 type *mtp = get_Call_type(pred_pred);
83 tp = get_method_res_type(mtp, get_Proj_proj(n));
85 VERBOSE_UNKNOWN_TYPE(("Proj %ld from Proj from ??: unknown type\n", get_irn_node_nr(n)));
90 /* globals and frame pointer */
91 if (get_Proj_proj(n) == pns_frame_base)
92 tp = find_pointer_type_to(get_irg_frame_type(get_Start_irg(pred)));
93 else if (get_Proj_proj(n) == pns_globals)
94 tp = find_pointer_type_to(get_glob_type());
95 else if (get_Proj_proj(n) == pns_value_arg_base) {
96 VERBOSE_UNKNOWN_TYPE(("Value arg base proj %ld from Start: unknown type\n", get_irn_node_nr(n)));
97 tp = unknown_type; //find_pointer_type_to(get....(get_entity_type(get_irg_ent(get_Start_irg(pred)))));
99 VERBOSE_UNKNOWN_TYPE(("Proj %ld %ld from Start: unknown type\n", get_Proj_proj(n), get_irn_node_nr(n)));
104 /* value args pointer */
105 if (get_Proj_proj(n) == pncl_value_res_base) {
106 VERBOSE_UNKNOWN_TYPE(("Value res base Proj %ld from Call: unknown type\n", get_irn_node_nr(n)));
107 tp = unknown_type; //find_pointer_type_to(get....get_Call_type(pred));
109 VERBOSE_UNKNOWN_TYPE(("Proj %ld %ld from Call: unknown type\n", get_Proj_proj(n), get_irn_node_nr(n)));
114 tp = compute_irn_type(pred);
120 static type *find_type_for_node(ir_node *n) {
122 type *tp1 = NULL, *tp2 = NULL;
123 ir_node *a = NULL, *b = NULL;
129 tp1 = compute_irn_type(a);
132 a = get_binop_left(n);
133 b = get_binop_right(n);
134 tp1 = compute_irn_type(a);
135 tp2 = compute_irn_type(b);
138 switch(get_irn_opcode(n)) {
141 assert(0 && "op_InstOf not supported");
146 /* Check returned type. */
149 type *meth_type = get_entity_type(get_irg_ent(current_ir_graph));
150 for (i = 0; i < get_method_n_ress(meth_type); i++) {
151 type *res_type = get_method_res_type(meth_type, i);
152 type *ana_res_type = get_irn_type(get_Return_res(n, i));
153 if (ana_res_type == unknown_type) continue;
154 if (res_type != ana_res_type && "return value has wrong type") {
156 assert(res_type == ana_res_type && "return value has wrong type");
178 tp = none_type; break;
180 /* compute the type */
181 case iro_Const: tp = get_Const_type(n); break;
183 tp = unknown_type; break;
185 tp = find_pointer_type_to(get_entity_type(get_Sel_entity(n))); break;
186 /* assymetric binops */
193 tp = get_Cast_type(n); break;
196 int n_preds = get_Phi_n_preds(n);
198 if (n_preds == 0) {tp = none_type; break; }
200 /* initialize this Phi */
201 set_irn_type(n, phi_cycle_type);
203 /* find a first real type */
204 for (i = 0; i < n_preds; ++i) {
205 tp1 = compute_irn_type(get_Phi_pred(n, i));
206 assert(tp1 != initial_type);
207 if ((tp1 != phi_cycle_type) && (tp1 != none_type))
211 /* find a second real type */
213 for (; (i < n_preds); ++i) {
214 tp2 = compute_irn_type(get_Phi_pred(n, i));
215 if ((tp2 == phi_cycle_type) || (tp2 == none_type)) {
219 if (tp2 != tp1) break;
222 //printf("Types in Phi %s and %s \n", get_type_name(tp1), get_type_name(tp2));
224 if (tp1 == tp2) { tp = tp1; break; }
226 VERBOSE_UNKNOWN_TYPE(("Phi %ld with two different types: %s, %s: unknown type.\n", get_irn_node_nr(n),
227 get_type_name(tp1), get_type_name(tp2)));
231 ir_node *a = get_Load_ptr(n);
232 if (get_irn_op(a) == op_Sel)
233 tp = get_entity_type(get_Sel_entity(a));
234 else if ((get_irn_op(a) == op_Const) &&
235 (tarval_is_entity(get_Const_tarval(a))))
236 tp = get_entity_type(tarval_to_entity(get_Const_tarval(a)));
237 else if (is_pointer_type(compute_irn_type(a))) {
238 tp = get_pointer_points_to_type(get_irn_type(a));
239 if (is_array_type(tp)) tp = get_array_element_type(tp);
241 VERBOSE_UNKNOWN_TYPE(("Load %ld with typeless address. result: unknown type\n", get_irn_node_nr(n)));
246 tp = find_pointer_type_to(get_Alloc_type(n)); break;
248 tp = find_type_for_Proj(n); break;
250 tp = compute_irn_type(get_Id_pred(n)); break;
252 tp = unknown_type; break;
254 assert(0 && "Filter not implemented"); break;
256 /* catch special cases with fallthrough to binop/unop cases in default. */
258 if (mode_is_int(get_irn_mode(n)) &&
259 mode_is_reference(get_irn_mode(a)) &&
260 mode_is_reference(get_irn_mode(b)) ) {
261 VERBOSE_UNKNOWN_TYPE(("Sub %ld ptr - ptr = int: unknown type\n", get_irn_node_nr(n)));
262 tp = unknown_type; break;
264 } /* fall through to Add. */
266 if (mode_is_reference(get_irn_mode(n)) &&
267 mode_is_reference(get_irn_mode(a)) &&
268 mode_is_int(get_irn_mode(b)) ) {
271 if (mode_is_reference(get_irn_mode(n)) &&
272 mode_is_int(get_irn_mode(a)) &&
273 mode_is_reference(get_irn_mode(b)) ) {
279 if (get_irn_mode(n) != get_irn_mode(a)) {
280 VERBOSE_UNKNOWN_TYPE(("Mul %ld int1 * int1 = int2: unknown type\n", get_irn_node_nr(n)));
281 tp = unknown_type; break;
290 /* Is is proper to walk past a Conv??? */
300 if((tp1 == phi_cycle_type) || (tp2 == phi_cycle_type)) {
304 VERBOSE_UNKNOWN_TYPE(("Binop %ld with two different types: %s, %s: unknown type \n", get_irn_node_nr(n),
305 get_type_name(tp1), get_type_name(tp2)));
310 printf(" not implemented: "); DDMN(n);
311 } break; /* default */
314 //printf (" found %s ", get_type_name(tp)); DDM;
320 static type* compute_irn_type(ir_node *n) {
323 type *tp = get_irn_type(n);
325 if ((tp == initial_type)) {
326 tp = find_type_for_node(n);
330 //printf (" found %s ", get_type_name(tp)); DDM;
335 static void compute_type(ir_node *n, void *env) {
337 type *tp = get_irn_type(n);
338 if (tp == phi_cycle_type) {
339 //printf(" recomputing for phi_cycle_type "); DDMN(n);
340 set_irn_type(n, initial_type);
346 static void analyse_irg (ir_graph *irg) {
347 set_irg_typeinfo_state(irg, irg_typeinfo_consistent);
348 irg_walk_graph(irg, NULL, compute_type, NULL);
351 static void init_irsimpletype(void) {
354 phi_cycle_type = new_type_class(new_id_from_str("phi_cycle_type"));
355 precompute_pointer_types();
358 void simple_analyse_types(void) {
361 for (i = 0; i < get_irp_n_irgs(); i++) {
362 current_ir_graph = get_irp_irg(i);
363 analyse_irg(current_ir_graph);
367 void free_simple_type_information(void) {
370 if (phi_cycle_type) {
371 free_type(phi_cycle_type);
372 phi_cycle_type = NULL;