cgana.c 19.4 KB
Newer Older
Götz Lindenmaier's avatar
Götz Lindenmaier committed
1
/*
Matthias Braun's avatar
Matthias Braun committed
2
 * This file is part of libFirm.
3
 * Copyright (C) 2012 University of Karlsruhe.
Götz Lindenmaier's avatar
Götz Lindenmaier committed
4
 */
Götz Lindenmaier's avatar
bugfix    
Götz Lindenmaier committed
5

Matthias Braun's avatar
Matthias Braun committed
6
7
8
9
10
/**
 * @file
 * @brief      Intraprozedural analyses to estimate the call graph.
 * @author     Hubert Schmid
 * @date       09.06.2002
yb9976's avatar
yb9976 committed
11
 * @brief
Matthias Braun's avatar
Matthias Braun committed
12
13
14
15
16
17
18
 *  Interprocedural analysis to estimate the calling relation.
 *
 *  This analysis computes all entities representing methods that
 *  can be called at a Call node.  Further it computes a set of
 *  methods that are 'free', i.e., their adress is handled by
 *  the program directly, or they are visible external.
 */
19
#include "cgana.h"
Michael Beck's avatar
Michael Beck committed
20
#include "xmalloc.h"
Götz Lindenmaier's avatar
Götz Lindenmaier committed
21
22
23
#include "irnode_t.h"
#include "irmode_t.h"
#include "irprog_t.h"
24
25
26
#include "irgwalk.h"
#include "ircons.h"
#include "irgmod.h"
Götz Lindenmaier's avatar
bugfix    
Götz Lindenmaier committed
27
#include "iropt.h"
28
#include "irtools.h"
29

Götz Lindenmaier's avatar
Götz Lindenmaier committed
30
#include "irflag_t.h"
Götz Lindenmaier's avatar
Götz Lindenmaier committed
31
#include "dbginfo_t.h"
Michael Beck's avatar
Michael Beck committed
32
#include "iropt_dbg.h"
Götz Lindenmaier's avatar
Götz Lindenmaier committed
33

Götz Lindenmaier's avatar
Götz Lindenmaier committed
34
35
#include "pmap.h"
#include "array.h"
36
#include "error.h"
Götz Lindenmaier's avatar
Götz Lindenmaier committed
37

38
39
#include "irdump.h"

Michael Beck's avatar
Michael Beck committed
40
/* unambiguous address used as a mark. */
Götz Lindenmaier's avatar
bugfix    
Götz Lindenmaier committed
41
static void *MARK = &MARK;
42

Matthias Braun's avatar
Matthias Braun committed
43
static pset *entities = NULL;
44

45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
int cg_call_has_callees(const ir_node *node)
{
	assert(is_Call(node));
	return ((get_irg_callee_info_state(get_irn_irg(node)) != irg_callee_info_none) &&
	        (node->attr.call.callee_arr != NULL));
}

size_t cg_get_call_n_callees(const ir_node *node)
{
  assert(is_Call(node) && node->attr.call.callee_arr);
  return ARR_LEN(node->attr.call.callee_arr);
}

ir_entity *cg_get_call_callee(const ir_node *node, size_t pos)
{
	assert(pos < cg_get_call_n_callees(node));
	return node->attr.call.callee_arr[pos];
}

void cg_set_call_callee_arr(ir_node *node, size_t n, ir_entity **arr)
{
	assert(is_Call(node));
	if (node->attr.call.callee_arr==NULL || cg_get_call_n_callees(node) != n) {
		ir_graph *const irg = get_irn_irg(node);
		node->attr.call.callee_arr = NEW_ARR_D(ir_entity*, get_irg_obstack(irg), n);
	}
	memcpy(node->attr.call.callee_arr, arr, n * sizeof(ir_entity *));
}

void cg_remove_call_callee_arr(ir_node *node)
{
	assert(is_Call(node));
	node->attr.call.callee_arr = NULL;
}

Götz Lindenmaier's avatar
bugfix    
Götz Lindenmaier committed
80
81
82
/*--------------------------------------------------------------------------*/
/* The analysis                                                             */
/*--------------------------------------------------------------------------*/
83
84


Götz Lindenmaier's avatar
bugfix    
Götz Lindenmaier committed
85
86
87
88
/*--------------------------------------------------------------------------*/
/* Initialize datastructures, remove unwanted constructs, optimize          */
/* call target computations.                                                */
/*--------------------------------------------------------------------------*/
89

90
/** Collect the entity representing the implementation of this
91
 *  method (not the same if inherited) and all entities for overwriting
92
 *  implementations in parameter set.
93
94
95
 *  A recursive descend in the overwritten relation.
 *  Cycle-free, therefore must terminate.
 *
96
 * @param method   the overwritten method
97
 * @param set      A set of entities.
98
99
 *
 * @return Number of entities in set.
100
 */
Matthias Braun's avatar
Matthias Braun committed
101
static size_t collect_impls(ir_entity *method, pset *set)
Matthias Braun's avatar
Matthias Braun committed
102
{
103
	size_t size = 0;
Matthias Braun's avatar
Matthias Braun committed
104
	if (get_entity_irg(method) != NULL) {
105
		/* has an implementation */
Matthias Braun's avatar
Matthias Braun committed
106
		pset_insert_ptr(set, method);
107
		++size;
Michael Beck's avatar
Michael Beck committed
108
109
110
	}

	/*- recursive descent -*/
Matthias Braun's avatar
Matthias Braun committed
111
112
113
	for (size_t i = get_entity_n_overwrittenby(method); i-- > 0;) {
		size += collect_impls(get_entity_overwrittenby(method, i), set);
	}
114
	return size;
115
116
}

117
118
119
120
/**
 * Determine all methods that overwrite the given method (and implement it).
 * The returned array must be freed by the caller (see DEL_ARR_F).
 * If the set of overwriting methods is empty, returns NULL.
121
 *
122
 * @param method  the method
123
 */
124
static ir_entity **get_impl_methods(ir_entity *method)
Matthias Braun's avatar
Matthias Braun committed
125
{
126
	assert(is_method_entity(method));
Michael Beck's avatar
Michael Beck committed
127
	/* Collect all method entities that can be called here */
Matthias Braun's avatar
Matthias Braun committed
128
129
130
131
	ir_entity  **arr = NULL;
	pset       *set  = pset_new_ptr_default();
	size_t      size = collect_impls(method, set);
	if (size > 0) {
Michael Beck's avatar
Michael Beck committed
132
		arr = NEW_ARR_F(ir_entity *, size);
133
		foreach_pset(set, ir_entity, ent) {
134
			arr[--size] = ent;
Matthias Braun's avatar
Matthias Braun committed
135
		}
Michael Beck's avatar
Michael Beck committed
136
	}
Matthias Braun's avatar
Matthias Braun committed
137
	del_pset(set);
Michael Beck's avatar
Michael Beck committed
138
	return arr;
139
140
}

Michael Beck's avatar
Michael Beck committed
141
/** Analyze address computations.
142
 *
Götz Lindenmaier's avatar
bugfix    
Götz Lindenmaier committed
143
 *  Compute for all Sel nodes the set of methods that can be selected.
Götz Lindenmaier's avatar
Götz Lindenmaier committed
144
 *  For each entity we store the set of subentities in the link field.
Götz Lindenmaier's avatar
bugfix    
Götz Lindenmaier committed
145
146
147
148
 *
 *  Further do some optimizations:
 *  - Call standard optimizations for Sel nodes: this removes polymorphic
 *    calls.
149
 *
Michael Beck's avatar
Michael Beck committed
150
 *  @param node  The node to analyze
151
 */
Matthias Braun's avatar
Matthias Braun committed
152
153
static void sel_methods_walker(ir_node *node, void *env)
{
154
	(void)env;
Matthias Braun's avatar
Matthias Braun committed
155
156
	if (!is_Sel(node))
		return;
Michael Beck's avatar
Michael Beck committed
157
158

	/* Call standard optimizations */
Matthias Braun's avatar
Matthias Braun committed
159
160
161
162
163
164
	ir_node *new_node = optimize_in_place(node);
	if (node != new_node) {
		exchange(node, new_node);
		node = new_node;
		if (!is_Sel(node))
			return;
Michael Beck's avatar
Michael Beck committed
165
166
	}

Matthias Braun's avatar
Matthias Braun committed
167
	ir_entity     *const entity      = get_Sel_entity(node);
168
	if (!is_method_entity(entity))
Matthias Braun's avatar
Matthias Braun committed
169
170
171
		return;
	/* we may have a vtable entry and need this redirection to get the actually
	 * called method */
172
	ir_entity *const called = get_Address_entity(get_atomic_ent_value(entity));
Matthias Braun's avatar
Matthias Braun committed
173
174
175
176
177
178
	if (!pset_find_ptr(entities, called)) {
		/* Entity not yet handled. Find all (internal or external)
		 * implemented methods that overwrites this entity.
		 * This set is stored in the entity link. */
		set_entity_link(called, get_impl_methods(called));
		pset_insert_ptr(entities, called);
Michael Beck's avatar
Michael Beck committed
179
	}
180
181
}

Michael Beck's avatar
Michael Beck committed
182
183
/**
 * Initialize auxiliary data structures.
Götz Lindenmaier's avatar
Götz Lindenmaier committed
184
 *
Michael Beck's avatar
Michael Beck committed
185
186
 * Computes a set of entities that overwrite an entity and contain
 * an implementation. The set is stored in the entity's link field.
Götz Lindenmaier's avatar
Götz Lindenmaier committed
187
 *
Michael Beck's avatar
Michael Beck committed
188
 * Further replaces Sel nodes where this set contains exactly one
189
 * method by Address nodes.
Michael Beck's avatar
Michael Beck committed
190
 */
Matthias Braun's avatar
Matthias Braun committed
191
192
static void sel_methods_init(void)
{
Michael Beck's avatar
Michael Beck committed
193
	assert(entities == NULL);
Matthias Braun's avatar
Matthias Braun committed
194
	entities = pset_new_ptr_default();
195
	all_irg_walk(sel_methods_walker, NULL, NULL);
196
197
}

Götz Lindenmaier's avatar
bugfix    
Götz Lindenmaier committed
198
199
200
201
202
203
/*--------------------------------------------------------------------------*/
/* Find free methods.
 *
 * We expect that each entity has an array with all implementations in its
 * link field.                                                              */
/*--------------------------------------------------------------------------*/
204

205
206
207
/**
 * Returns an array of all methods that could be called at a Sel node.
 * This array contains every entry only once.
208
209
 *
 * @param sel  the Sel node
210
 */
Matthias Braun's avatar
Matthias Braun committed
211
static ir_entity **get_Sel_arr(ir_node *sel)
Matthias Braun's avatar
Matthias Braun committed
212
{
Matthias Braun's avatar
Matthias Braun committed
213
214
215
	ir_entity *const entity = get_Sel_entity(sel);
	assert(is_Method_type(get_entity_type(entity))); /* what else? */
	return (ir_entity**)get_entity_link(entity);
216
217
}

218
219
/**
 * Returns the number of possible called methods at a Sel node.
220
221
 *
 * @param sel  the Sel node
222
 */
Matthias Braun's avatar
Matthias Braun committed
223
static size_t get_Sel_n_methods(ir_node *sel)
Matthias Braun's avatar
Matthias Braun committed
224
{
Matthias Braun's avatar
Matthias Braun committed
225
226
227
228
	ir_entity **const arr = get_Sel_arr(sel);
	if (arr == NULL)
		return 0;
	return ARR_LEN(arr);
229
230
}

231
232
233
/**
 * Returns the ith possible called method entity at a Sel node.
 */
Matthias Braun's avatar
Matthias Braun committed
234
static ir_entity *get_Sel_method(ir_node *sel, size_t pos)
Matthias Braun's avatar
Matthias Braun committed
235
{
Matthias Braun's avatar
Matthias Braun committed
236
	ir_entity **arr = get_Sel_arr(sel);
237
	assert(pos < ARR_LEN(arr));
Michael Beck's avatar
Michael Beck committed
238
	return arr[pos];
239
240
}

Michael Beck's avatar
Michael Beck committed
241
/* forward */
Matthias Braun's avatar
Matthias Braun committed
242
static void free_mark(ir_node *node, pset *set);
243

Matthias Braun's avatar
Matthias Braun committed
244
static void free_mark_proj(ir_node *node, long n, pset *set)
Matthias Braun's avatar
Matthias Braun committed
245
{
Michael Beck's avatar
Michael Beck committed
246
247
248
249
250
251
252
253
	assert(get_irn_mode(node) == mode_T);
	if (get_irn_link(node) == MARK) {
		/* already visited */
		return;
	}
	set_irn_link(node, MARK);
	switch (get_irn_opcode(node)) {
	case iro_Proj: {
Matthias Braun's avatar
Matthias Braun committed
254
255
256
		/* proj_proj: in a correct graph we now find an op_Tuple or something
		 * which is handled by free_ana_walker(). */
		ir_node *pred = get_Proj_pred(node);
257
		if (get_irn_link(pred) != MARK && is_Tuple(pred)) {
Michael Beck's avatar
Michael Beck committed
258
259
260
261
262
263
264
265
266
267
268
269
			free_mark_proj(get_Tuple_pred(pred, get_Proj_proj(node)), n, set);
		}
		break;
	}

	case iro_Tuple:
		free_mark(get_Tuple_pred(node, n), set);
		break;

	case iro_Start:
	case iro_Alloc:
	case iro_Load:
Matthias Braun's avatar
Matthias Braun committed
270
		/* nothing: operations are handled in free_ana_walker() */
Michael Beck's avatar
Michael Beck committed
271
272
273
		break;

	default:
Matthias Braun's avatar
Matthias Braun committed
274
		panic("unexpected opcode or opcode not implemented");
Michael Beck's avatar
Michael Beck committed
275
	}
Götz Lindenmaier's avatar
bugfix    
Götz Lindenmaier committed
276
277
}

278
/**
Michael Beck's avatar
Michael Beck committed
279
280
281
282
283
284
285
 * Called for predecessors nodes of "interesting" ones.
 * Interesting ones include all nodes that can somehow make
 * a method visible.
 *
 * If a method (or a set of methods in case of polymorph calls) gets visible,
 * add it to the set of 'free' methods
 *
286
287
288
 * @param node  the current visited node
 * @param set   the set of all free methods
 */
Matthias Braun's avatar
Matthias Braun committed
289
static void free_mark(ir_node *node, pset *set)
Matthias Braun's avatar
Matthias Braun committed
290
{
Michael Beck's avatar
Michael Beck committed
291
292
293
294
295
296
297
	if (get_irn_link(node) == MARK)
		return; /* already visited */

	set_irn_link(node, MARK);

	switch (get_irn_opcode(node)) {
	case iro_Sel: {
Matthias Braun's avatar
Matthias Braun committed
298
		const ir_entity *ent = get_Sel_entity(node);
Michael Beck's avatar
Michael Beck committed
299
		if (is_method_entity(ent)) {
Matthias Braun's avatar
Matthias Braun committed
300
			for (size_t i = 0, n = get_Sel_n_methods(node); i < n; ++i) {
Matthias Braun's avatar
Matthias Braun committed
301
				pset_insert_ptr(set, get_Sel_method(node, i));
Michael Beck's avatar
Michael Beck committed
302
303
304
			}
		}
		break;
Michael Beck's avatar
Michael Beck committed
305
	}
306
307
308
309
310

	case iro_Address: {
		const ir_entity *ent = get_Address_entity(node);
		if (is_method_entity(ent)) {
			pset_insert_ptr(set, ent);
Michael Beck's avatar
Michael Beck committed
311
312
		}
		break;
313
	}
Michael Beck's avatar
Michael Beck committed
314
315

	case iro_Phi:
Matthias Braun's avatar
Matthias Braun committed
316
		for (int i = 0, n = get_Phi_n_preds(node); i < n; ++i) {
Michael Beck's avatar
Michael Beck committed
317
318
319
			free_mark(get_Phi_pred(node, i), set);
		}
		break;
Matthias Braun's avatar
Matthias Braun committed
320

Michael Beck's avatar
Michael Beck committed
321
322
323
324
325
326
	case iro_Proj:
		free_mark_proj(get_Proj_pred(node), get_Proj_proj(node), set);
		break;
	default:
		break;
	}
Götz Lindenmaier's avatar
bugfix    
Götz Lindenmaier committed
327
}
328

329
/**
Michael Beck's avatar
Michael Beck committed
330
 * post-walker. Find method addresses.
331
 */
Matthias Braun's avatar
Matthias Braun committed
332
333
static void free_ana_walker(ir_node *node, void *env)
{
Michael Beck's avatar
Michael Beck committed
334
335
336
337
	if (get_irn_link(node) == MARK) {
		/* already visited */
		return;
	}
Matthias Braun's avatar
Matthias Braun committed
338
339

	pset *set = (pset*) env;
Michael Beck's avatar
Michael Beck committed
340
341
	switch (get_irn_opcode(node)) {
		/* special nodes */
342
	case iro_Address:
343
	case iro_Align:
Michael Beck's avatar
Michael Beck committed
344
345
	case iro_Sel:
	case iro_Const:
346
	case iro_Offset:
Michael Beck's avatar
Michael Beck committed
347
348
349
	case iro_Phi:
	case iro_Id:
	case iro_Proj:
350
	case iro_Size:
Michael Beck's avatar
Michael Beck committed
351
352
353
354
	case iro_Tuple:
		/* nothing */
		break;
	case iro_Call:
Michael Beck's avatar
Michael Beck committed
355
356
		/* we must handle Call nodes specially, because their call address input
		   do not expose a method address. */
Michael Beck's avatar
Michael Beck committed
357
		set_irn_link(node, MARK);
Matthias Braun's avatar
Matthias Braun committed
358
		for (size_t i = 0, n = get_Call_n_params(node); i < n; ++i) {
Michael Beck's avatar
Michael Beck committed
359
360
361
362
363
364
			ir_node *pred = get_Call_param(node, i);
			if (mode_is_reference(get_irn_mode(pred))) {
				free_mark(pred, set);
			}
		}
		break;
Matthias Braun's avatar
Matthias Braun committed
365
366

	default:
yb9976's avatar
yb9976 committed
367
		/* other nodes: Alle anderen Knoten nehmen wir als Verr�ter an, bis
Michael Beck's avatar
Michael Beck committed
368
369
		 * jemand das Gegenteil implementiert. */
		set_irn_link(node, MARK);
Matthias Braun's avatar
Matthias Braun committed
370
		for (int i = get_irn_arity(node) - 1; i >= 0; --i) {
Michael Beck's avatar
Michael Beck committed
371
			ir_node *pred = get_irn_n(node, i);
Michael Beck's avatar
Michael Beck committed
372
373
374
375
376
377
			if (mode_is_reference(get_irn_mode(pred))) {
				free_mark(pred, set);
			}
		}
		break;
	}
Götz Lindenmaier's avatar
bugfix    
Götz Lindenmaier committed
378
379
}

380
381
382
383
/**
 * Add all method addresses in global new style initializers to the set.
 *
 * @note
384
 * We do NOT check the type here, just if it's an entity address.
385
386
387
388
389
390
 * The reason for this is code like:
 *
 * void *p = function;
 *
 * which is sometimes used to anchor functions.
 */
391
static void add_method_address_inititializer(ir_initializer_t *initializer,
Matthias Braun's avatar
Matthias Braun committed
392
                                             pset *set)
393
{
394
	switch (initializer->kind) {
Matthias Braun's avatar
Matthias Braun committed
395
396
	case IR_INITIALIZER_CONST: {
		ir_node *n = initializer->consti.value;
397
398

		/* let's check if it's the address of a function */
399
400
		if (is_Address(n)) {
			ir_entity *ent = get_Address_entity(n);
401
402

			if (is_Method_type(get_entity_type(ent)))
Matthias Braun's avatar
Matthias Braun committed
403
				pset_insert_ptr(set, ent);
404
405
		}
		return;
Matthias Braun's avatar
Matthias Braun committed
406
	}
407
408
409
	case IR_INITIALIZER_TARVAL:
	case IR_INITIALIZER_NULL:
		return;
410
	case IR_INITIALIZER_COMPOUND:
Matthias Braun's avatar
Matthias Braun committed
411
		for (size_t i = 0; i < initializer->compound.n_initializers; ++i) {
412
413
			ir_initializer_t *sub_initializer
				= initializer->compound.initializers[i];
414
			add_method_address_inititializer(sub_initializer, set);
415
416
417
418
419
420
		}
		return;
	}
	panic("invalid initializer found");
}

421
422
/**
 * Add all method addresses in global initializers to the set.
423
424
 *
 * @note
425
 * We do NOT check the type here, just if it's an entity address.
426
427
428
429
430
 * The reason for this is code like:
 *
 * void *p = function;
 *
 * which is sometimes used to anchor functions.
431
 */
Matthias Braun's avatar
Matthias Braun committed
432
static void add_method_address(ir_entity *ent, pset *set)
433
{
434
	/* ignore methods: these of course reference their addresses
Michael Beck's avatar
Michael Beck committed
435
	 * TODO: remove this later once this incorrect self-initialisation is gone
Matthias Braun's avatar
Matthias Braun committed
436
	 */
Matthias Braun's avatar
Matthias Braun committed
437
	ir_type *tp = get_entity_type(ent);
Matthias Braun's avatar
Matthias Braun committed
438
	if (is_Method_type(tp))
Michael Beck's avatar
Michael Beck committed
439
440
		return;

Matthias Braun's avatar
Matthias Braun committed
441
	if (ent->initializer != NULL) {
442
		add_method_address_inititializer(get_entity_initializer(ent), set);
Michael Beck's avatar
Michael Beck committed
443
	}
444
445
}

Michael Beck's avatar
Michael Beck committed
446
447
448
449
/**
 * returns a list of 'free' methods, i.e., the methods that can be called
 * from external or via function pointers.
 *
Matthias Braun's avatar
Matthias Braun committed
450
451
 * the datastructures for sel_methods must be constructed before calling
 * get_free_methods().
Michael Beck's avatar
Michael Beck committed
452
 */
453
static size_t get_free_methods(ir_entity ***free_methods)
Götz Lindenmaier's avatar
bugfix    
Götz Lindenmaier committed
454
{
Matthias Braun's avatar
Matthias Braun committed
455
	pset *free_set = pset_new_ptr_default();
Matthias Braun's avatar
Matthias Braun committed
456
457
458
459
460

	for (size_t i = 0, n = get_irp_n_irgs(); i < n; ++i) {
		ir_graph  *const irg     = get_irp_irg(i);
		ir_entity *const ent     = get_irg_entity(irg);
		ir_linkage const linkage = get_entity_linkage(ent);
Matthias Braun's avatar
Matthias Braun committed
461

Michael Beck's avatar
Michael Beck committed
462
		if ((linkage & IR_LINKAGE_HIDDEN_USER) || entity_is_externally_visible(ent)) {
Matthias Braun's avatar
Matthias Braun committed
463
			pset_insert_ptr(free_set, ent);
Michael Beck's avatar
Michael Beck committed
464
		}
Michael Beck's avatar
Michael Beck committed
465

466
		ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
467
		/* Find all method entities that gets "visible" through this graphs,
Michael Beck's avatar
Michael Beck committed
468
		 * for instance because their address is stored. */
469
		irg_walk_graph(irg, firm_clear_link, free_ana_walker, free_set);
470
		ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
Michael Beck's avatar
Michael Beck committed
471
472
	}

Michael Beck's avatar
Michael Beck committed
473
	/* insert all methods that are used in global variables initializers */
Matthias Braun's avatar
Matthias Braun committed
474
475
476
	ir_type *global_tp = get_glob_type();
	for (size_t j = 0, m = get_class_n_members(global_tp); j < m; ++j) {
		ir_entity *const ent = get_class_member(global_tp, j);
Michael Beck's avatar
Michael Beck committed
477
478
		add_method_address(ent, free_set);
	}
Matthias Braun's avatar
Matthias Braun committed
479
480
481
	ir_type *tls_tp = get_tls_type();
	for (size_t j = 0, m = get_compound_n_members(tls_tp); j < m; ++j) {
		ir_entity *const ent = get_compound_member(tls_tp, j);
Michael Beck's avatar
Michael Beck committed
482
483
484
485
		add_method_address(ent, free_set);
	}

	/* the main program is even then "free", if it's not external visible. */
Matthias Braun's avatar
Matthias Braun committed
486
	ir_graph *irg = get_irp_main_irg();
Michael Beck's avatar
Michael Beck committed
487
	if (irg != NULL)
Matthias Braun's avatar
Matthias Braun committed
488
		pset_insert_ptr(free_set, get_irg_entity(irg));
Michael Beck's avatar
Michael Beck committed
489
490

	/* Finally, transform the set into an array. */
Matthias Braun's avatar
Matthias Braun committed
491
492
493
	size_t      length = pset_count(free_set);
	ir_entity **arr    = XMALLOCN(ir_entity*, length);
	size_t      i      = 0;
494
	foreach_pset(free_set, ir_entity, ent) {
Michael Beck's avatar
Michael Beck committed
495
		arr[i++] = ent;
Michael Beck's avatar
Michael Beck committed
496
	}
Matthias Braun's avatar
Matthias Braun committed
497
	del_pset(free_set);
Michael Beck's avatar
Michael Beck committed
498

499
500
	*free_methods = arr;
	return length;
Götz Lindenmaier's avatar
bugfix    
Götz Lindenmaier committed
501
502
503
504
505
506
}

/*--------------------------------------------------------------------------*/
/* Callee analysis.                                                         */
/*--------------------------------------------------------------------------*/

Matthias Braun's avatar
Matthias Braun committed
507
static void callee_ana_node(ir_node *node, pset *methods);
508

Matthias Braun's avatar
Matthias Braun committed
509
static void callee_ana_proj(ir_node *node, long n, pset *methods)
Matthias Braun's avatar
Matthias Braun committed
510
{
Michael Beck's avatar
Michael Beck committed
511
512
513
514
515
516
517
518
519
	assert(get_irn_mode(node) == mode_T);
	if (get_irn_link(node) == MARK) {
		/* already visited */
		return;
	}
	set_irn_link(node, MARK);

	switch (get_irn_opcode(node)) {
	case iro_Proj: {
Matthias Braun's avatar
Matthias Braun committed
520
521
		/* proj_proj: in a correct graph we now get an op_Tuple or a node
		 * returning a free method. */
Michael Beck's avatar
Michael Beck committed
522
		ir_node *pred = get_Proj_pred(node);
Michael Beck's avatar
Michael Beck committed
523
		if (get_irn_link(pred) != MARK) {
Michael Beck's avatar
Michael Beck committed
524
			if (is_Tuple(pred)) {
Michael Beck's avatar
Michael Beck committed
525
526
				callee_ana_proj(get_Tuple_pred(pred, get_Proj_proj(node)), n, methods);
			} else {
527
				pset_insert_ptr(methods, get_unknown_entity()); /* free method -> unknown */
Michael Beck's avatar
Michael Beck committed
528
529
530
			}
		}
		break;
Michael Beck's avatar
Michael Beck committed
531
	}
Michael Beck's avatar
Michael Beck committed
532
533
534
535
536
537

	case iro_Tuple:
		callee_ana_node(get_Tuple_pred(node, n), methods);
		break;

	default:
538
		pset_insert_ptr(methods, get_unknown_entity()); /* free method -> unknown */
Michael Beck's avatar
Michael Beck committed
539
540
		break;
	}
541
542
}

Michael Beck's avatar
Michael Beck committed
543
/**
Michael Beck's avatar
Michael Beck committed
544
 * Analyse a Call address.
Michael Beck's avatar
Michael Beck committed
545
 *
Michael Beck's avatar
Michael Beck committed
546
547
 * @param node     the node representing the call address
 * @param methods  after call contains the set of all possibly called entities
Michael Beck's avatar
Michael Beck committed
548
 */
Matthias Braun's avatar
Matthias Braun committed
549
static void callee_ana_node(ir_node *node, pset *methods)
Matthias Braun's avatar
Matthias Braun committed
550
{
Michael Beck's avatar
Michael Beck committed
551
552
553
554
555
556
557
558
559
560
	assert(mode_is_reference(get_irn_mode(node)) || is_Bad(node));
	/* Beware of recursion */
	if (get_irn_link(node) == MARK) {
		/* already visited */
		return;
	}
	set_irn_link(node, MARK);

	switch (get_irn_opcode(node)) {
	case iro_Const:
561
		/* A direct address call. We treat this as an external
Michael Beck's avatar
Michael Beck committed
562
		   call and ignore it completely. */
563
		pset_insert_ptr(methods, get_unknown_entity()); /* free method -> unknown */
Michael Beck's avatar
Michael Beck committed
564
		break;
565

566
567
	case iro_Address: {
		ir_entity *ent = get_Address_entity(node);
568
569
		if (is_method_entity(ent))
			pset_insert_ptr(methods, ent);
Michael Beck's avatar
Michael Beck committed
570
		break;
571
572
	}

573
574
575
576
	case iro_Sel: {
		ir_entity *entity = get_Sel_entity(node);
		if (!is_method_entity(entity))
			break;
Michael Beck's avatar
Michael Beck committed
577
		/* polymorphic method */
Matthias Braun's avatar
Matthias Braun committed
578
		for (size_t i = 0, n = get_Sel_n_methods(node); i < n; ++i) {
Michael Beck's avatar
Michael Beck committed
579
580
			ir_entity *ent = get_Sel_method(node, i);
			if (ent != NULL) {
Matthias Braun's avatar
Matthias Braun committed
581
				pset_insert_ptr(methods, ent);
Michael Beck's avatar
Michael Beck committed
582
			} else {
583
				pset_insert_ptr(methods, get_unknown_entity());
Michael Beck's avatar
Michael Beck committed
584
585
586
			}
		}
		break;
587
	}
Michael Beck's avatar
Michael Beck committed
588
589
590
591

	case iro_Bad:
		break;

Matthias Braun's avatar
Matthias Braun committed
592
593
	case iro_Phi:
		for (int i = get_Phi_n_preds(node) - 1; i >= 0; --i) {
Michael Beck's avatar
Michael Beck committed
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
			callee_ana_node(get_Phi_pred(node, i), methods);
		}
		break;

	case iro_Mux:
		callee_ana_node(get_Mux_false(node), methods);
		callee_ana_node(get_Mux_true(node), methods);
		break;

	case iro_Proj:
		callee_ana_proj(get_Proj_pred(node), get_Proj_proj(node), methods);
		break;

	case iro_Add:
	case iro_Sub:
	case iro_Conv:
		/* extern */
611
		pset_insert_ptr(methods, get_unknown_entity()); /* free method -> unknown */
Michael Beck's avatar
Michael Beck committed
612
613
614
		break;

	default:
Matthias Braun's avatar
Matthias Braun committed
615
		panic("invalid opcode or opcode not implemented");
Michael Beck's avatar
Michael Beck committed
616
	}
617
618
}

Michael Beck's avatar
Michael Beck committed
619
/**
Michael Beck's avatar
Michael Beck committed
620
 * Walker: Analyses every Call node and calculates an array of possible
Michael Beck's avatar
Michael Beck committed
621
622
 * callees for that call.
 */
Matthias Braun's avatar
Matthias Braun committed
623
624
static void callee_walker(ir_node *call, void *env)
{
Michael Beck's avatar
Michael Beck committed
625
	(void) env;
Matthias Braun's avatar
Matthias Braun committed
626
627
628
629
630
631
632
633
634
635
636
637
638
	if (!is_Call(call))
		return;

	pset *methods = pset_new_ptr_default();
	callee_ana_node(get_Call_ptr(call), methods);
	ir_entity **arr = NEW_ARR_F(ir_entity*, pset_count(methods));
	size_t      i   = 0;
	foreach_pset(methods, ir_entity, ent) {
		arr[i] = ent;
		/* we want the unknown_entity on the zero position for easy tests later */
		if (is_unknown_entity(ent)) {
			arr[i] = arr[0];
			arr[0] = get_unknown_entity();
Michael Beck's avatar
Michael Beck committed
639
		}
Matthias Braun's avatar
Matthias Braun committed
640
		++i;
Michael Beck's avatar
Michael Beck committed
641
	}
642
	cg_set_call_callee_arr(call, ARR_LEN(arr), arr);
Matthias Braun's avatar
Matthias Braun committed
643
644
	DEL_ARR_F(arr);
	del_pset(methods);
645
646
}

Michael Beck's avatar
Michael Beck committed
647
648
649
650
651
/**
 * Determine for every Call the set of possibly called methods and stores it
 * inside the Call (@see set_Call_callee()).
 * Uses the sel_methods set with much be already calculated.
 */
Matthias Braun's avatar
Matthias Braun committed
652
653
static void callee_ana(void)
{
Michael Beck's avatar
Michael Beck committed
654
	/* analyse all graphs */
Matthias Braun's avatar
Matthias Braun committed
655
	for (size_t i = 0, n = get_irp_n_irgs(); i < n; ++i) {
Michael Beck's avatar
Michael Beck committed
656
		ir_graph *irg = get_irp_irg(i);
Matthias Braun's avatar
Matthias Braun committed
657
658
		assure_irg_properties(irg, IR_GRAPH_PROPERTY_NO_TUPLES);
		irg_walk_graph(irg, callee_walker, NULL, NULL);
Michael Beck's avatar
Michael Beck committed
659
660
661
		set_irg_callee_info_state(irg, irg_callee_info_consistent);
	}
	set_irp_callee_info_state(irg_callee_info_consistent);
662
663
}

Götz Lindenmaier's avatar
bugfix    
Götz Lindenmaier committed
664
665
666
/*--------------------------------------------------------------------------*/
/* Cleanup after analyses.                                                  */
/*--------------------------------------------------------------------------*/
667

Götz Lindenmaier's avatar
bugfix    
Götz Lindenmaier committed
668
/** Frees intermediate data structures. */
Matthias Braun's avatar
Matthias Braun committed
669
670
static void sel_methods_dispose(void)
{
Michael Beck's avatar
Michael Beck committed
671
	assert(entities);
672
	foreach_pset(entities, ir_entity, ent) {
Matthias Braun's avatar
Matthias Braun committed
673
674
		ir_entity **arr = (ir_entity**) get_entity_link(ent);
		if (arr != NULL) {
Michael Beck's avatar
Michael Beck committed
675
676
677
678
			DEL_ARR_F(arr);
		}
		set_entity_link(ent, NULL);
	}
Matthias Braun's avatar
Matthias Braun committed
679
	del_pset(entities);
Michael Beck's avatar
Michael Beck committed
680
	entities = NULL;
681
682
}

Matthias Braun's avatar
Matthias Braun committed
683
static void destruct_walker(ir_node *node, void *env)
Matthias Braun's avatar
Matthias Braun committed
684
{
685
686
687
	(void)env;
	if (is_Call(node))
		cg_remove_call_callee_arr(node);
688
689
}

690
size_t cgana(ir_entity ***free_methods)
Matthias Braun's avatar
Matthias Braun committed
691
{
692
	/* Optimize Address/Sel nodes and compute all methods that implement an entity. */
Michael Beck's avatar
Michael Beck committed
693
	sel_methods_init();
Matthias Braun's avatar
Matthias Braun committed
694
	size_t length = get_free_methods(free_methods);
Michael Beck's avatar
Michael Beck committed
695
696
	callee_ana();
	sel_methods_dispose();
697
	return length;
698
}
699

Matthias Braun's avatar
Matthias Braun committed
700
701
void free_callee_info(ir_graph *irg)
{
Michael Beck's avatar
Michael Beck committed
702
703
	irg_walk_graph(irg, destruct_walker, NULL, NULL);
	set_irg_callee_info_state(irg, irg_callee_info_none);
Götz Lindenmaier's avatar
Götz Lindenmaier committed
704
705
}

Matthias Braun's avatar
Matthias Braun committed
706
707
void free_irp_callee_info(void)
{
Matthias Braun's avatar
Matthias Braun committed
708
	for (size_t i = 0, n = get_irp_n_irgs(); i < n; ++i) {
Michael Beck's avatar
Michael Beck committed
709
710
		free_callee_info(get_irp_irg(i));
	}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
711
712
}

Matthias Braun's avatar
Matthias Braun committed
713
714
void opt_call_addrs(void)
{
715
716
717
718
	/* Optimize the address expressions passed to call nodes.
	 *
	 * This optimization performs the following transformations for
	 * all ir graphs:
719
	 * - All Address operations that refer to intern methods are replaced
720
721
722
723
724
725
726
727
728
	 *   by Const operations referring to the corresponding entity.
	 * - Sel nodes, that select entities that are not overwritten are
	 *   replaced by Const nodes referring to the selected entity.
	 * - Sel nodes, for which no method exists at all are replaced by Bad
	 *   nodes.
	 * - Sel nodes with a pointer input that is an Alloc node are replaced
	 *   by Const nodes referring to the entity that implements the method in
	 *   the type given by the Alloc node.
	 */
Michael Beck's avatar
Michael Beck committed
729
730
	sel_methods_init();
	sel_methods_dispose();
731
}