cgana.c 19.1 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"
Matthias Braun's avatar
Matthias Braun committed
36
#include "panic.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
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);
	}
Christoph Mallon's avatar
Christoph Mallon committed
71
	MEMCPY(node->attr.call.callee_arr, arr, n);
72
73
74
75
76
77
78
79
}

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
 *
143
 *  Compute for all Member 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
 *
 *  Further do some optimizations:
147
 *  - Call standard optimizations for Member nodes: this removes polymorphic
Götz Lindenmaier's avatar
bugfix    
Götz Lindenmaier committed
148
 *    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;
155
	if (!is_Member(node))
Matthias Braun's avatar
Matthias Braun committed
156
		return;
Michael Beck's avatar
Michael Beck committed
157
158

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

167
	ir_entity *const entity = get_Member_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
 *
188
 * Further replaces Member 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
 * Returns an array of all methods that could be called at a Member node.
207
 * This array contains every entry only once.
208
 *
209
 * @param member  the Member node
210
 */
211
static ir_entity **get_member_arr(ir_node *member)
Matthias Braun's avatar
Matthias Braun committed
212
{
213
	ir_entity *const entity = get_Member_entity(member);
Matthias Braun's avatar
Matthias Braun committed
214
215
	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 Member node.
220
 *
221
 * @param member  the Member node
222
 */
223
static size_t get_member_n_methods(ir_node *member)
Matthias Braun's avatar
Matthias Braun committed
224
{
225
	ir_entity **const arr = get_member_arr(member);
Matthias Braun's avatar
Matthias Braun committed
226
227
228
	if (arr == NULL)
		return 0;
	return ARR_LEN(arr);
229
230
}

231
/**
232
 * Returns the ith possible called method entity at a Member node.
233
 */
234
static ir_entity *get_member_method(ir_node *member, size_t pos)
Matthias Braun's avatar
Matthias Braun committed
235
{
236
	ir_entity **arr = get_member_arr(member);
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

244
static void free_mark_proj(ir_node *node, unsigned 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)) {
258
			free_mark_proj(get_Tuple_pred(pred, get_Proj_num(node)), n, set);
Michael Beck's avatar
Michael Beck committed
259
260
261
262
263
264
265
266
267
268
269
		}
		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
	if (get_irn_link(node) == MARK)
		return; /* already visited */

	set_irn_link(node, MARK);

	switch (get_irn_opcode(node)) {
297
298
	case iro_Member: {
		const ir_entity *ent = get_Member_entity(node);
Michael Beck's avatar
Michael Beck committed
299
		if (is_method_entity(ent)) {
300
301
			for (size_t i = 0, n = get_member_n_methods(node); i < n; ++i) {
				pset_insert_ptr(set, get_member_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
	case iro_Proj:
322
		free_mark_proj(get_Proj_pred(node), get_Proj_num(node), set);
Michael Beck's avatar
Michael Beck committed
323
324
325
326
		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:
344
	case iro_Member:
Michael Beck's avatar
Michael Beck committed
345
	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);
370
		foreach_irn_in_r(node, i, pred) {
Michael Beck's avatar
Michael Beck committed
371
372
373
374
375
376
			if (mode_is_reference(get_irn_mode(pred))) {
				free_mark(pred, set);
			}
		}
		break;
	}
Götz Lindenmaier's avatar
bugfix    
Götz Lindenmaier committed
377
378
}

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

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

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

419
420
/**
 * Add all method addresses in global initializers to the set.
421
422
 *
 * @note
423
 * We do NOT check the type here, just if it's an entity address.
424
425
426
427
428
 * The reason for this is code like:
 *
 * void *p = function;
 *
 * which is sometimes used to anchor functions.
429
 */
Matthias Braun's avatar
Matthias Braun committed
430
static void add_method_address(ir_entity *ent, pset *set)
431
{
432
433
434
435
	if (get_entity_kind(ent) == IR_ENTITY_NORMAL) {
		ir_initializer_t const *const init = get_entity_initializer(ent);
		if (init)
			add_method_address_inititializer(init, set);
Michael Beck's avatar
Michael Beck committed
436
	}
437
438
}

Michael Beck's avatar
Michael Beck committed
439
440
441
442
/**
 * 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
443
444
 * the datastructures for sel_methods must be constructed before calling
 * get_free_methods().
Michael Beck's avatar
Michael Beck committed
445
 */
446
static size_t get_free_methods(ir_entity ***free_methods)
Götz Lindenmaier's avatar
bugfix    
Götz Lindenmaier committed
447
{
Matthias Braun's avatar
Matthias Braun committed
448
	pset *free_set = pset_new_ptr_default();
Matthias Braun's avatar
Matthias Braun committed
449

450
	foreach_irp_irg(i, irg) {
Matthias Braun's avatar
Matthias Braun committed
451
452
		ir_entity *const ent     = get_irg_entity(irg);
		ir_linkage const linkage = get_entity_linkage(ent);
Matthias Braun's avatar
Matthias Braun committed
453

Michael Beck's avatar
Michael Beck committed
454
		if ((linkage & IR_LINKAGE_HIDDEN_USER) || entity_is_externally_visible(ent)) {
Matthias Braun's avatar
Matthias Braun committed
455
			pset_insert_ptr(free_set, ent);
Michael Beck's avatar
Michael Beck committed
456
		}
Michael Beck's avatar
Michael Beck committed
457

458
		ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
459
		/* Find all method entities that gets "visible" through this graphs,
Michael Beck's avatar
Michael Beck committed
460
		 * for instance because their address is stored. */
461
		irg_walk_graph(irg, firm_clear_link, free_ana_walker, free_set);
462
		ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
Michael Beck's avatar
Michael Beck committed
463
464
	}

Michael Beck's avatar
Michael Beck committed
465
	/* insert all methods that are used in global variables initializers */
Matthias Braun's avatar
Matthias Braun committed
466
467
468
	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
469
470
		add_method_address(ent, free_set);
	}
Matthias Braun's avatar
Matthias Braun committed
471
472
473
	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
474
475
476
477
		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
478
	ir_graph *irg = get_irp_main_irg();
Michael Beck's avatar
Michael Beck committed
479
	if (irg != NULL)
Matthias Braun's avatar
Matthias Braun committed
480
		pset_insert_ptr(free_set, get_irg_entity(irg));
Michael Beck's avatar
Michael Beck committed
481
482

	/* Finally, transform the set into an array. */
Matthias Braun's avatar
Matthias Braun committed
483
484
485
	size_t      length = pset_count(free_set);
	ir_entity **arr    = XMALLOCN(ir_entity*, length);
	size_t      i      = 0;
486
	foreach_pset(free_set, ir_entity, ent) {
Michael Beck's avatar
Michael Beck committed
487
		arr[i++] = ent;
Michael Beck's avatar
Michael Beck committed
488
	}
Matthias Braun's avatar
Matthias Braun committed
489
	del_pset(free_set);
Michael Beck's avatar
Michael Beck committed
490

491
492
	*free_methods = arr;
	return length;
Götz Lindenmaier's avatar
bugfix    
Götz Lindenmaier committed
493
494
495
496
497
498
}

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

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

501
static void callee_ana_proj(ir_node *node, unsigned n, pset *methods)
Matthias Braun's avatar
Matthias Braun committed
502
{
Michael Beck's avatar
Michael Beck committed
503
504
505
506
507
508
509
510
511
	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
512
513
		/* 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
514
		ir_node *pred = get_Proj_pred(node);
Michael Beck's avatar
Michael Beck committed
515
		if (get_irn_link(pred) != MARK) {
Michael Beck's avatar
Michael Beck committed
516
			if (is_Tuple(pred)) {
517
				callee_ana_proj(get_Tuple_pred(pred, get_Proj_num(node)), n, methods);
Michael Beck's avatar
Michael Beck committed
518
			} else {
519
				pset_insert_ptr(methods, get_unknown_entity()); /* free method -> unknown */
Michael Beck's avatar
Michael Beck committed
520
521
522
			}
		}
		break;
Michael Beck's avatar
Michael Beck committed
523
	}
Michael Beck's avatar
Michael Beck committed
524
525
526
527
528
529

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

	default:
530
		pset_insert_ptr(methods, get_unknown_entity()); /* free method -> unknown */
Michael Beck's avatar
Michael Beck committed
531
532
		break;
	}
533
534
}

Michael Beck's avatar
Michael Beck committed
535
/**
Michael Beck's avatar
Michael Beck committed
536
 * Analyse a Call address.
Michael Beck's avatar
Michael Beck committed
537
 *
Michael Beck's avatar
Michael Beck committed
538
539
 * @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
540
 */
Matthias Braun's avatar
Matthias Braun committed
541
static void callee_ana_node(ir_node *node, pset *methods)
Matthias Braun's avatar
Matthias Braun committed
542
{
Michael Beck's avatar
Michael Beck committed
543
544
545
546
547
548
549
550
551
552
	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:
553
		/* A direct address call. We treat this as an external
Michael Beck's avatar
Michael Beck committed
554
		   call and ignore it completely. */
555
		pset_insert_ptr(methods, get_unknown_entity()); /* free method -> unknown */
Michael Beck's avatar
Michael Beck committed
556
		break;
557

558
559
	case iro_Address: {
		ir_entity *ent = get_Address_entity(node);
560
561
		if (is_method_entity(ent))
			pset_insert_ptr(methods, ent);
Michael Beck's avatar
Michael Beck committed
562
		break;
563
564
	}

565
566
	case iro_Member: {
		ir_entity *entity = get_Member_entity(node);
567
568
		if (!is_method_entity(entity))
			break;
Michael Beck's avatar
Michael Beck committed
569
		/* polymorphic method */
570
571
		for (size_t i = 0, n = get_member_n_methods(node); i < n; ++i) {
			ir_entity *ent = get_member_method(node, i);
Michael Beck's avatar
Michael Beck committed
572
			if (ent != NULL) {
Matthias Braun's avatar
Matthias Braun committed
573
				pset_insert_ptr(methods, ent);
Michael Beck's avatar
Michael Beck committed
574
			} else {
575
				pset_insert_ptr(methods, get_unknown_entity());
Michael Beck's avatar
Michael Beck committed
576
577
578
			}
		}
		break;
579
	}
Michael Beck's avatar
Michael Beck committed
580
581
582
583

	case iro_Bad:
		break;

Matthias Braun's avatar
Matthias Braun committed
584
585
	case iro_Phi:
		for (int i = get_Phi_n_preds(node) - 1; i >= 0; --i) {
Michael Beck's avatar
Michael Beck committed
586
587
588
589
590
591
592
593
594
595
			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:
596
		callee_ana_proj(get_Proj_pred(node), get_Proj_num(node), methods);
Michael Beck's avatar
Michael Beck committed
597
598
599
600
601
602
		break;

	case iro_Add:
	case iro_Sub:
	case iro_Conv:
		/* extern */
603
		pset_insert_ptr(methods, get_unknown_entity()); /* free method -> unknown */
Michael Beck's avatar
Michael Beck committed
604
605
606
		break;

	default:
Matthias Braun's avatar
Matthias Braun committed
607
		panic("invalid opcode or opcode not implemented");
Michael Beck's avatar
Michael Beck committed
608
	}
609
610
}

Michael Beck's avatar
Michael Beck committed
611
/**
Michael Beck's avatar
Michael Beck committed
612
 * Walker: Analyses every Call node and calculates an array of possible
Michael Beck's avatar
Michael Beck committed
613
614
 * callees for that call.
 */
Matthias Braun's avatar
Matthias Braun committed
615
616
static void callee_walker(ir_node *call, void *env)
{
Matthias Braun's avatar
Matthias Braun committed
617
	(void)env;
Matthias Braun's avatar
Matthias Braun committed
618
619
620
621
622
623
624
625
626
627
628
629
630
	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
631
		}
Matthias Braun's avatar
Matthias Braun committed
632
		++i;
Michael Beck's avatar
Michael Beck committed
633
	}
634
	cg_set_call_callee_arr(call, ARR_LEN(arr), arr);
Matthias Braun's avatar
Matthias Braun committed
635
636
	DEL_ARR_F(arr);
	del_pset(methods);
637
638
}

Michael Beck's avatar
Michael Beck committed
639
640
641
642
643
/**
 * 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
644
645
static void callee_ana(void)
{
Michael Beck's avatar
Michael Beck committed
646
	/* analyse all graphs */
647
	foreach_irp_irg(i, irg) {
Matthias Braun's avatar
Matthias Braun committed
648
		assure_irg_properties(irg, IR_GRAPH_PROPERTY_NO_TUPLES);
649
		ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
Matthias Braun's avatar
Matthias Braun committed
650
		irg_walk_graph(irg, callee_walker, NULL, NULL);
651
		ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
Michael Beck's avatar
Michael Beck committed
652
653
654
		set_irg_callee_info_state(irg, irg_callee_info_consistent);
	}
	set_irp_callee_info_state(irg_callee_info_consistent);
655
656
}

Götz Lindenmaier's avatar
bugfix    
Götz Lindenmaier committed
657
658
659
/*--------------------------------------------------------------------------*/
/* Cleanup after analyses.                                                  */
/*--------------------------------------------------------------------------*/
660

Götz Lindenmaier's avatar
bugfix    
Götz Lindenmaier committed
661
/** Frees intermediate data structures. */
Matthias Braun's avatar
Matthias Braun committed
662
663
static void sel_methods_dispose(void)
{
Michael Beck's avatar
Michael Beck committed
664
	assert(entities);
665
	foreach_pset(entities, ir_entity, ent) {
Matthias Braun's avatar
Matthias Braun committed
666
667
		ir_entity **arr = (ir_entity**) get_entity_link(ent);
		if (arr != NULL) {
Michael Beck's avatar
Michael Beck committed
668
669
670
671
			DEL_ARR_F(arr);
		}
		set_entity_link(ent, NULL);
	}
Matthias Braun's avatar
Matthias Braun committed
672
	del_pset(entities);
Michael Beck's avatar
Michael Beck committed
673
	entities = NULL;
674
675
}

Matthias Braun's avatar
Matthias Braun committed
676
static void destruct_walker(ir_node *node, void *env)
Matthias Braun's avatar
Matthias Braun committed
677
{
678
679
680
	(void)env;
	if (is_Call(node))
		cg_remove_call_callee_arr(node);
681
682
}

683
size_t cgana(ir_entity ***free_methods)
Matthias Braun's avatar
Matthias Braun committed
684
{
685
686
	/* Optimize Address/Member nodes and compute all methods that implement an
	 * entity. */
Michael Beck's avatar
Michael Beck committed
687
	sel_methods_init();
Matthias Braun's avatar
Matthias Braun committed
688
	size_t length = get_free_methods(free_methods);
Michael Beck's avatar
Michael Beck committed
689
690
	callee_ana();
	sel_methods_dispose();
691
	return length;
692
}
693

Matthias Braun's avatar
Matthias Braun committed
694
695
void free_callee_info(ir_graph *irg)
{
Michael Beck's avatar
Michael Beck committed
696
697
	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
698
699
}

Matthias Braun's avatar
Matthias Braun committed
700
701
void free_irp_callee_info(void)
{
702
703
	foreach_irp_irg(i, irg) {
		free_callee_info(irg);
Michael Beck's avatar
Michael Beck committed
704
	}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
705
706
}

Matthias Braun's avatar
Matthias Braun committed
707
708
void opt_call_addrs(void)
{
709
710
711
712
	/* Optimize the address expressions passed to call nodes.
	 *
	 * This optimization performs the following transformations for
	 * all ir graphs:
713
	 * - All Address operations that refer to intern methods are replaced
714
	 *   by Const operations referring to the corresponding entity.
715
	 * - Member nodes, that select entities that are not overwritten are
716
	 *   replaced by Const nodes referring to the selected entity.
717
	 * - Member nodes, for which no method exists at all are replaced by Bad
718
	 *   nodes.
719
	 * - Member nodes with a pointer input that is an Alloc node are replaced
720
721
722
	 *   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
723
724
	sel_methods_init();
	sel_methods_dispose();
725
}