cgana.c 18.5 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

Götz Lindenmaier's avatar
bugfix    
Götz Lindenmaier committed
45
46
47
/*--------------------------------------------------------------------------*/
/* The analysis                                                             */
/*--------------------------------------------------------------------------*/
48
49


Götz Lindenmaier's avatar
bugfix    
Götz Lindenmaier committed
50
51
52
53
/*--------------------------------------------------------------------------*/
/* Initialize datastructures, remove unwanted constructs, optimize          */
/* call target computations.                                                */
/*--------------------------------------------------------------------------*/
54

55
/** Collect the entity representing the implementation of this
56
 *  method (not the same if inherited) and all entities for overwriting
57
 *  implementations in parameter set.
58
59
60
 *  A recursive descend in the overwritten relation.
 *  Cycle-free, therefore must terminate.
 *
61
 * @param method   the overwritten method
62
 * @param set      A set of entities.
63
64
 *
 * @return Number of entities in set.
65
 */
Matthias Braun's avatar
Matthias Braun committed
66
static size_t collect_impls(ir_entity *method, pset *set)
Matthias Braun's avatar
Matthias Braun committed
67
{
68
	size_t size = 0;
Matthias Braun's avatar
Matthias Braun committed
69
	if (get_entity_irg(method) != NULL) {
70
		/* has an implementation */
Matthias Braun's avatar
Matthias Braun committed
71
		pset_insert_ptr(set, method);
72
		++size;
Michael Beck's avatar
Michael Beck committed
73
74
75
	}

	/*- recursive descent -*/
Matthias Braun's avatar
Matthias Braun committed
76
77
78
	for (size_t i = get_entity_n_overwrittenby(method); i-- > 0;) {
		size += collect_impls(get_entity_overwrittenby(method, i), set);
	}
79
	return size;
80
81
}

82
83
84
85
/**
 * 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.
86
 *
87
 * @param method  the method
88
 */
89
static ir_entity **get_impl_methods(ir_entity *method)
Matthias Braun's avatar
Matthias Braun committed
90
{
Michael Beck's avatar
Michael Beck committed
91
	/* Collect all method entities that can be called here */
Matthias Braun's avatar
Matthias Braun committed
92
93
94
95
	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
96
		arr = NEW_ARR_F(ir_entity *, size);
97
		foreach_pset(set, ir_entity, ent) {
98
			arr[--size] = ent;
Matthias Braun's avatar
Matthias Braun committed
99
		}
Michael Beck's avatar
Michael Beck committed
100
	}
Matthias Braun's avatar
Matthias Braun committed
101
	del_pset(set);
Michael Beck's avatar
Michael Beck committed
102
	return arr;
103
104
}

Michael Beck's avatar
Michael Beck committed
105
/** Analyze address computations.
106
 *
Götz Lindenmaier's avatar
bugfix    
Götz Lindenmaier committed
107
 *  Compute for all Sel nodes the set of methods that can be selected.
Götz Lindenmaier's avatar
Götz Lindenmaier committed
108
 *  For each entity we store the set of subentities in the link field.
Götz Lindenmaier's avatar
bugfix    
Götz Lindenmaier committed
109
110
111
112
 *
 *  Further do some optimizations:
 *  - Call standard optimizations for Sel nodes: this removes polymorphic
 *    calls.
113
 *
Michael Beck's avatar
Michael Beck committed
114
115
 *  @param node  The node to analyze
 *  @param env   A map that maps names of entities to the entities.
116
 */
Matthias Braun's avatar
Matthias Braun committed
117
118
static void sel_methods_walker(ir_node *node, void *env)
{
119
	(void)env;
Matthias Braun's avatar
Matthias Braun committed
120
121
	if (!is_Sel(node))
		return;
Michael Beck's avatar
Michael Beck committed
122
123

	/* Call standard optimizations */
Matthias Braun's avatar
Matthias Braun committed
124
125
126
127
128
129
	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
130
131
	}

Matthias Braun's avatar
Matthias Braun committed
132
133
134
135
136
137
138
139
140
141
142
143
144
	ir_entity     *const entity      = get_Sel_entity(node);
	const ir_type *const entity_type = get_entity_type(entity);
	if (!is_Method_type(entity_type))
		return;
	/* we may have a vtable entry and need this redirection to get the actually
	 * called method */
	ir_entity *const called = get_SymConst_entity(get_atomic_ent_value(entity));
	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
145
	}
146
147
}

Michael Beck's avatar
Michael Beck committed
148
149
/**
 * Initialize auxiliary data structures.
Götz Lindenmaier's avatar
Götz Lindenmaier committed
150
 *
Michael Beck's avatar
Michael Beck committed
151
152
 * 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
153
 *
Michael Beck's avatar
Michael Beck committed
154
155
156
157
158
 * Further replaces Sel nodes where this set contains exactly one
 * method by SymConst nodes.
 * Finally asserts if there is a SymConst(name) if there could be a
 * SymConst(ent).
 */
Matthias Braun's avatar
Matthias Braun committed
159
160
static void sel_methods_init(void)
{
Michael Beck's avatar
Michael Beck committed
161
	assert(entities == NULL);
Matthias Braun's avatar
Matthias Braun committed
162
	entities = pset_new_ptr_default();
163
	all_irg_walk(sel_methods_walker, NULL, NULL);
164
165
}

Götz Lindenmaier's avatar
bugfix    
Götz Lindenmaier committed
166
167
168
169
170
171
/*--------------------------------------------------------------------------*/
/* Find free methods.
 *
 * We expect that each entity has an array with all implementations in its
 * link field.                                                              */
/*--------------------------------------------------------------------------*/
172

173
174
175
/**
 * Returns an array of all methods that could be called at a Sel node.
 * This array contains every entry only once.
176
177
 *
 * @param sel  the Sel node
178
 */
Matthias Braun's avatar
Matthias Braun committed
179
static ir_entity **get_Sel_arr(ir_node *sel)
Matthias Braun's avatar
Matthias Braun committed
180
{
Matthias Braun's avatar
Matthias Braun committed
181
182
183
	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);
184
185
}

186
187
/**
 * Returns the number of possible called methods at a Sel node.
188
189
 *
 * @param sel  the Sel node
190
 */
Matthias Braun's avatar
Matthias Braun committed
191
static size_t get_Sel_n_methods(ir_node *sel)
Matthias Braun's avatar
Matthias Braun committed
192
{
Matthias Braun's avatar
Matthias Braun committed
193
194
195
196
	ir_entity **const arr = get_Sel_arr(sel);
	if (arr == NULL)
		return 0;
	return ARR_LEN(arr);
197
198
}

199
200
201
/**
 * Returns the ith possible called method entity at a Sel node.
 */
Matthias Braun's avatar
Matthias Braun committed
202
static ir_entity *get_Sel_method(ir_node *sel, size_t pos)
Matthias Braun's avatar
Matthias Braun committed
203
{
Matthias Braun's avatar
Matthias Braun committed
204
	ir_entity **arr = get_Sel_arr(sel);
205
	assert(pos < ARR_LEN(arr));
Michael Beck's avatar
Michael Beck committed
206
	return arr[pos];
207
208
}

Michael Beck's avatar
Michael Beck committed
209
/* forward */
Matthias Braun's avatar
Matthias Braun committed
210
static void free_mark(ir_node *node, pset *set);
211

Matthias Braun's avatar
Matthias Braun committed
212
static void free_mark_proj(ir_node *node, long n, pset *set)
Matthias Braun's avatar
Matthias Braun committed
213
{
Michael Beck's avatar
Michael Beck committed
214
215
216
217
218
219
220
221
	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
222
223
224
		/* 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);
225
		if (get_irn_link(pred) != MARK && is_Tuple(pred)) {
Michael Beck's avatar
Michael Beck committed
226
227
228
229
230
231
232
233
234
235
236
237
			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
238
		/* nothing: operations are handled in free_ana_walker() */
Michael Beck's avatar
Michael Beck committed
239
240
241
		break;

	default:
Matthias Braun's avatar
Matthias Braun committed
242
		panic("unexpected opcode or opcode not implemented");
Michael Beck's avatar
Michael Beck committed
243
	}
Götz Lindenmaier's avatar
bugfix    
Götz Lindenmaier committed
244
245
}

246
/**
Michael Beck's avatar
Michael Beck committed
247
248
249
250
251
252
253
 * 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
 *
254
255
256
 * @param node  the current visited node
 * @param set   the set of all free methods
 */
Matthias Braun's avatar
Matthias Braun committed
257
static void free_mark(ir_node *node, pset *set)
Matthias Braun's avatar
Matthias Braun committed
258
{
Michael Beck's avatar
Michael Beck committed
259
260
261
262
263
264
265
	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
266
		const ir_entity *ent = get_Sel_entity(node);
Michael Beck's avatar
Michael Beck committed
267
		if (is_method_entity(ent)) {
Matthias Braun's avatar
Matthias Braun committed
268
			for (size_t i = 0, n = get_Sel_n_methods(node); i < n; ++i) {
Matthias Braun's avatar
Matthias Braun committed
269
				pset_insert_ptr(set, get_Sel_method(node, i));
Michael Beck's avatar
Michael Beck committed
270
271
272
			}
		}
		break;
Michael Beck's avatar
Michael Beck committed
273
	}
Michael Beck's avatar
Michael Beck committed
274
275
	case iro_SymConst:
		if (get_SymConst_kind(node) == symconst_addr_ent) {
Matthias Braun's avatar
Matthias Braun committed
276
			const ir_entity *ent = get_SymConst_entity(node);
Michael Beck's avatar
Michael Beck committed
277
			if (is_method_entity(ent)) {
Matthias Braun's avatar
Matthias Braun committed
278
				pset_insert_ptr(set, ent);
Michael Beck's avatar
Michael Beck committed
279
280
281
282
283
			}
		}
		break;

	case iro_Phi:
Matthias Braun's avatar
Matthias Braun committed
284
		for (int i = 0, n = get_Phi_n_preds(node); i < n; ++i) {
Michael Beck's avatar
Michael Beck committed
285
286
287
			free_mark(get_Phi_pred(node, i), set);
		}
		break;
Matthias Braun's avatar
Matthias Braun committed
288

Michael Beck's avatar
Michael Beck committed
289
290
291
292
293
294
	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
295
}
296

297
/**
Michael Beck's avatar
Michael Beck committed
298
 * post-walker. Find method addresses.
299
 */
Matthias Braun's avatar
Matthias Braun committed
300
301
static void free_ana_walker(ir_node *node, void *env)
{
Michael Beck's avatar
Michael Beck committed
302
303
304
305
	if (get_irn_link(node) == MARK) {
		/* already visited */
		return;
	}
Matthias Braun's avatar
Matthias Braun committed
306
307

	pset *set = (pset*) env;
Michael Beck's avatar
Michael Beck committed
308
309
310
311
312
313
314
315
316
317
318
319
	switch (get_irn_opcode(node)) {
		/* special nodes */
	case iro_Sel:
	case iro_SymConst:
	case iro_Const:
	case iro_Phi:
	case iro_Id:
	case iro_Proj:
	case iro_Tuple:
		/* nothing */
		break;
	case iro_Call:
Michael Beck's avatar
Michael Beck committed
320
321
		/* we must handle Call nodes specially, because their call address input
		   do not expose a method address. */
Michael Beck's avatar
Michael Beck committed
322
		set_irn_link(node, MARK);
Matthias Braun's avatar
Matthias Braun committed
323
		for (size_t i = 0, n = get_Call_n_params(node); i < n; ++i) {
Michael Beck's avatar
Michael Beck committed
324
325
326
327
328
329
			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
330
331

	default:
yb9976's avatar
yb9976 committed
332
		/* other nodes: Alle anderen Knoten nehmen wir als Verr�ter an, bis
Michael Beck's avatar
Michael Beck committed
333
334
		 * jemand das Gegenteil implementiert. */
		set_irn_link(node, MARK);
Matthias Braun's avatar
Matthias Braun committed
335
		for (int i = get_irn_arity(node) - 1; i >= 0; --i) {
Michael Beck's avatar
Michael Beck committed
336
			ir_node *pred = get_irn_n(node, i);
Michael Beck's avatar
Michael Beck committed
337
338
339
340
341
342
			if (mode_is_reference(get_irn_mode(pred))) {
				free_mark(pred, set);
			}
		}
		break;
	}
Götz Lindenmaier's avatar
bugfix    
Götz Lindenmaier committed
343
344
}

345
346
347
348
/**
 * Add all method addresses in global new style initializers to the set.
 *
 * @note
349
 * We do NOT check the type here, just if it's an entity address.
350
351
352
353
354
355
 * The reason for this is code like:
 *
 * void *p = function;
 *
 * which is sometimes used to anchor functions.
 */
356
static void add_method_address_inititializer(ir_initializer_t *initializer,
Matthias Braun's avatar
Matthias Braun committed
357
                                             pset *set)
358
{
359
	switch (initializer->kind) {
Matthias Braun's avatar
Matthias Braun committed
360
361
	case IR_INITIALIZER_CONST: {
		ir_node *n = initializer->consti.value;
362
363

		/* let's check if it's the address of a function */
364
365
		if (is_SymConst_addr_ent(n)) {
			ir_entity *ent = get_SymConst_entity(n);
366
367

			if (is_Method_type(get_entity_type(ent)))
Matthias Braun's avatar
Matthias Braun committed
368
				pset_insert_ptr(set, ent);
369
370
		}
		return;
Matthias Braun's avatar
Matthias Braun committed
371
	}
372
373
374
	case IR_INITIALIZER_TARVAL:
	case IR_INITIALIZER_NULL:
		return;
375
	case IR_INITIALIZER_COMPOUND:
Matthias Braun's avatar
Matthias Braun committed
376
		for (size_t i = 0; i < initializer->compound.n_initializers; ++i) {
377
378
			ir_initializer_t *sub_initializer
				= initializer->compound.initializers[i];
379
			add_method_address_inititializer(sub_initializer, set);
380
381
382
383
384
385
		}
		return;
	}
	panic("invalid initializer found");
}

386
387
/**
 * Add all method addresses in global initializers to the set.
388
389
 *
 * @note
390
 * We do NOT check the type here, just if it's an entity address.
391
392
393
394
395
 * The reason for this is code like:
 *
 * void *p = function;
 *
 * which is sometimes used to anchor functions.
396
 */
Matthias Braun's avatar
Matthias Braun committed
397
static void add_method_address(ir_entity *ent, pset *set)
398
{
399
	/* ignore methods: these of course reference their addresses
Michael Beck's avatar
Michael Beck committed
400
	 * TODO: remove this later once this incorrect self-initialisation is gone
Matthias Braun's avatar
Matthias Braun committed
401
	 */
Matthias Braun's avatar
Matthias Braun committed
402
	ir_type *tp = get_entity_type(ent);
Matthias Braun's avatar
Matthias Braun committed
403
	if (is_Method_type(tp))
Michael Beck's avatar
Michael Beck committed
404
405
		return;

Matthias Braun's avatar
Matthias Braun committed
406
	if (ent->initializer != NULL) {
407
		add_method_address_inititializer(get_entity_initializer(ent), set);
Michael Beck's avatar
Michael Beck committed
408
	}
409
410
}

Michael Beck's avatar
Michael Beck committed
411
412
413
414
/**
 * 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
415
416
 * the datastructures for sel_methods must be constructed before calling
 * get_free_methods().
Michael Beck's avatar
Michael Beck committed
417
 */
418
static size_t get_free_methods(ir_entity ***free_methods)
Götz Lindenmaier's avatar
bugfix    
Götz Lindenmaier committed
419
{
Matthias Braun's avatar
Matthias Braun committed
420
	pset *free_set = pset_new_ptr_default();
Matthias Braun's avatar
Matthias Braun committed
421
422
423
424
425

	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
426

Michael Beck's avatar
Michael Beck committed
427
		if ((linkage & IR_LINKAGE_HIDDEN_USER) || entity_is_externally_visible(ent)) {
Matthias Braun's avatar
Matthias Braun committed
428
			pset_insert_ptr(free_set, ent);
Michael Beck's avatar
Michael Beck committed
429
		}
Michael Beck's avatar
Michael Beck committed
430

431
		ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
432
		/* Find all method entities that gets "visible" through this graphs,
Michael Beck's avatar
Michael Beck committed
433
		 * for instance because their address is stored. */
434
		irg_walk_graph(irg, firm_clear_link, free_ana_walker, free_set);
435
		ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
Michael Beck's avatar
Michael Beck committed
436
437
	}

Michael Beck's avatar
Michael Beck committed
438
	/* insert all methods that are used in global variables initializers */
Matthias Braun's avatar
Matthias Braun committed
439
440
441
	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
442
443
		add_method_address(ent, free_set);
	}
Matthias Braun's avatar
Matthias Braun committed
444
445
446
	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
447
448
449
450
		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
451
	ir_graph *irg = get_irp_main_irg();
Michael Beck's avatar
Michael Beck committed
452
	if (irg != NULL)
Matthias Braun's avatar
Matthias Braun committed
453
		pset_insert_ptr(free_set, get_irg_entity(irg));
Michael Beck's avatar
Michael Beck committed
454
455

	/* Finally, transform the set into an array. */
Matthias Braun's avatar
Matthias Braun committed
456
457
458
	size_t      length = pset_count(free_set);
	ir_entity **arr    = XMALLOCN(ir_entity*, length);
	size_t      i      = 0;
459
	foreach_pset(free_set, ir_entity, ent) {
Michael Beck's avatar
Michael Beck committed
460
		arr[i++] = ent;
Michael Beck's avatar
Michael Beck committed
461
	}
Matthias Braun's avatar
Matthias Braun committed
462
	del_pset(free_set);
Michael Beck's avatar
Michael Beck committed
463

464
465
	*free_methods = arr;
	return length;
Götz Lindenmaier's avatar
bugfix    
Götz Lindenmaier committed
466
467
468
469
470
471
}

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

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

Matthias Braun's avatar
Matthias Braun committed
474
static void callee_ana_proj(ir_node *node, long n, pset *methods)
Matthias Braun's avatar
Matthias Braun committed
475
{
Michael Beck's avatar
Michael Beck committed
476
477
478
479
480
481
482
483
484
	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
485
486
		/* 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
487
		ir_node *pred = get_Proj_pred(node);
Michael Beck's avatar
Michael Beck committed
488
		if (get_irn_link(pred) != MARK) {
Michael Beck's avatar
Michael Beck committed
489
			if (is_Tuple(pred)) {
Michael Beck's avatar
Michael Beck committed
490
491
				callee_ana_proj(get_Tuple_pred(pred, get_Proj_proj(node)), n, methods);
			} else {
492
				pset_insert_ptr(methods, get_unknown_entity()); /* free method -> unknown */
Michael Beck's avatar
Michael Beck committed
493
494
495
			}
		}
		break;
Michael Beck's avatar
Michael Beck committed
496
	}
Michael Beck's avatar
Michael Beck committed
497
498
499
500
501
502

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

	default:
503
		pset_insert_ptr(methods, get_unknown_entity()); /* free method -> unknown */
Michael Beck's avatar
Michael Beck committed
504
505
		break;
	}
506
507
}

Michael Beck's avatar
Michael Beck committed
508
/**
Michael Beck's avatar
Michael Beck committed
509
 * Analyse a Call address.
Michael Beck's avatar
Michael Beck committed
510
 *
Michael Beck's avatar
Michael Beck committed
511
512
 * @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
513
 */
Matthias Braun's avatar
Matthias Braun committed
514
static void callee_ana_node(ir_node *node, pset *methods)
Matthias Braun's avatar
Matthias Braun committed
515
{
Michael Beck's avatar
Michael Beck committed
516
517
518
519
520
521
522
523
524
525
	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:
526
		/* A direct address call. We treat this as an external
Michael Beck's avatar
Michael Beck committed
527
		   call and ignore it completely. */
528
		pset_insert_ptr(methods, get_unknown_entity()); /* free method -> unknown */
Michael Beck's avatar
Michael Beck committed
529
		break;
530
531
532
533

	case iro_SymConst: {
		ir_entity *ent = get_SymConst_entity(node);
		assert(ent && is_method_entity(ent));
Matthias Braun's avatar
Matthias Braun committed
534
		pset_insert_ptr(methods, ent);
Michael Beck's avatar
Michael Beck committed
535
		break;
536
537
	}

Matthias Braun's avatar
Matthias Braun committed
538
	case iro_Sel:
Michael Beck's avatar
Michael Beck committed
539
		/* polymorphic method */
Matthias Braun's avatar
Matthias Braun committed
540
		for (size_t i = 0, n = get_Sel_n_methods(node); i < n; ++i) {
Michael Beck's avatar
Michael Beck committed
541
542
			ir_entity *ent = get_Sel_method(node, i);
			if (ent != NULL) {
Matthias Braun's avatar
Matthias Braun committed
543
				pset_insert_ptr(methods, ent);
Michael Beck's avatar
Michael Beck committed
544
			} else {
545
				pset_insert_ptr(methods, get_unknown_entity());
Michael Beck's avatar
Michael Beck committed
546
547
548
549
550
551
552
			}
		}
		break;

	case iro_Bad:
		break;

Matthias Braun's avatar
Matthias Braun committed
553
554
	case iro_Phi:
		for (int i = get_Phi_n_preds(node) - 1; i >= 0; --i) {
Michael Beck's avatar
Michael Beck committed
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
			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 */
572
		pset_insert_ptr(methods, get_unknown_entity()); /* free method -> unknown */
Michael Beck's avatar
Michael Beck committed
573
574
575
		break;

	default:
Matthias Braun's avatar
Matthias Braun committed
576
		panic("invalid opcode or opcode not implemented");
Michael Beck's avatar
Michael Beck committed
577
	}
578
579
}

Michael Beck's avatar
Michael Beck committed
580
/**
Michael Beck's avatar
Michael Beck committed
581
 * Walker: Analyses every Call node and calculates an array of possible
Michael Beck's avatar
Michael Beck committed
582
583
 * callees for that call.
 */
Matthias Braun's avatar
Matthias Braun committed
584
585
static void callee_walker(ir_node *call, void *env)
{
Michael Beck's avatar
Michael Beck committed
586
	(void) env;
Matthias Braun's avatar
Matthias Braun committed
587
588
589
590
591
592
593
594
595
596
597
598
599
	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
600
		}
Matthias Braun's avatar
Matthias Braun committed
601
		++i;
Michael Beck's avatar
Michael Beck committed
602
	}
Matthias Braun's avatar
Matthias Braun committed
603
604
605
	set_Call_callee_arr(call, ARR_LEN(arr), arr);
	DEL_ARR_F(arr);
	del_pset(methods);
606
607
}

Michael Beck's avatar
Michael Beck committed
608
609
610
611
612
/**
 * 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
613
614
static void callee_ana(void)
{
Michael Beck's avatar
Michael Beck committed
615
	/* analyse all graphs */
Matthias Braun's avatar
Matthias Braun committed
616
	for (size_t i = 0, n = get_irp_n_irgs(); i < n; ++i) {
Michael Beck's avatar
Michael Beck committed
617
		ir_graph *irg = get_irp_irg(i);
Matthias Braun's avatar
Matthias Braun committed
618
619
		assure_irg_properties(irg, IR_GRAPH_PROPERTY_NO_TUPLES);
		irg_walk_graph(irg, callee_walker, NULL, NULL);
Michael Beck's avatar
Michael Beck committed
620
621
622
		set_irg_callee_info_state(irg, irg_callee_info_consistent);
	}
	set_irp_callee_info_state(irg_callee_info_consistent);
623
624
}

Götz Lindenmaier's avatar
bugfix    
Götz Lindenmaier committed
625
626
627
/*--------------------------------------------------------------------------*/
/* Cleanup after analyses.                                                  */
/*--------------------------------------------------------------------------*/
628

Götz Lindenmaier's avatar
bugfix    
Götz Lindenmaier committed
629
/** Frees intermediate data structures. */
Matthias Braun's avatar
Matthias Braun committed
630
631
static void sel_methods_dispose(void)
{
Michael Beck's avatar
Michael Beck committed
632
	assert(entities);
633
	foreach_pset(entities, ir_entity, ent) {
Matthias Braun's avatar
Matthias Braun committed
634
635
		ir_entity **arr = (ir_entity**) get_entity_link(ent);
		if (arr != NULL) {
Michael Beck's avatar
Michael Beck committed
636
637
638
639
			DEL_ARR_F(arr);
		}
		set_entity_link(ent, NULL);
	}
Matthias Braun's avatar
Matthias Braun committed
640
	del_pset(entities);
Michael Beck's avatar
Michael Beck committed
641
	entities = NULL;
642
643
}

Matthias Braun's avatar
Matthias Braun committed
644
static void destruct_walker(ir_node *node, void *env)
Matthias Braun's avatar
Matthias Braun committed
645
{
Michael Beck's avatar
Michael Beck committed
646
647
648
649
	(void) env;
	if (is_Call(node)) {
		remove_Call_callee_arr(node);
	}
650
651
}

652
size_t cgana(ir_entity ***free_methods)
Matthias Braun's avatar
Matthias Braun committed
653
{
Michael Beck's avatar
Michael Beck committed
654
655
	/* Optimize Sel/SymConst nodes and compute all methods that implement an entity. */
	sel_methods_init();
Matthias Braun's avatar
Matthias Braun committed
656
	size_t length = get_free_methods(free_methods);
Michael Beck's avatar
Michael Beck committed
657
658
	callee_ana();
	sel_methods_dispose();
659
	return length;
660
}
661

Matthias Braun's avatar
Matthias Braun committed
662
663
void free_callee_info(ir_graph *irg)
{
Michael Beck's avatar
Michael Beck committed
664
665
	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
666
667
}

Matthias Braun's avatar
Matthias Braun committed
668
669
void free_irp_callee_info(void)
{
Matthias Braun's avatar
Matthias Braun committed
670
	for (size_t i = 0, n = get_irp_n_irgs(); i < n; ++i) {
Michael Beck's avatar
Michael Beck committed
671
672
		free_callee_info(get_irp_irg(i));
	}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
673
674
}

Matthias Braun's avatar
Matthias Braun committed
675
676
void opt_call_addrs(void)
{
677
678
679
680
681
682
683
684
685
686
687
688
689
690
	/* Optimize the address expressions passed to call nodes.
	 *
	 * This optimization performs the following transformations for
	 * all ir graphs:
	 * - All SymConst operations that refer to intern methods are replaced
	 *   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
691
692
	sel_methods_init();
	sel_methods_dispose();
693
}