irmemory.c 36.4 KB
Newer Older
1
/*
2
 * Copyright (C) 1995-2010 University of Karlsruhe.  All right reserved.
Matthias Braun's avatar
Matthias Braun committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 *
 * This file is part of libFirm.
 *
 * This file may be distributed and/or modified under the terms of the
 * GNU General Public License version 2 as published by the Free Software
 * Foundation and appearing in the file LICENSE.GPL included in the
 * packaging of this file.
 *
 * Licensees holding valid libFirm Professional Edition licenses may use
 * this file in accordance with the libFirm Commercial License.
 * Agreement provided with the Software.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE.
 */

/**
 * @file
 * @brief    Memory disambiguator
 * @author   Michael Beck
 * @date     27.12.2006
 * @version  $Id$
26
27
28
29
 */
#include "config.h"

#include <stdlib.h>
30
#include <stdbool.h>
31

32
#include "adt/pmap.h"
33
34
35
#include "irnode_t.h"
#include "irgraph_t.h"
#include "irprog_t.h"
36
#include "irmemory_t.h"
37
38
39
40
41
42
#include "irmemory.h"
#include "irflag.h"
#include "hashptr.h"
#include "irflag.h"
#include "irouts.h"
#include "irgwalk.h"
Matthias Braun's avatar
Matthias Braun committed
43
#include "irprintf.h"
44
#include "debug.h"
45
#include "error.h"
46
#include "typerep.h"
47
#include "irpass.h"
48
49
50

/** The debug handle. */
DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
51
DEBUG_ONLY(static firm_dbg_module_t *dbgcall = NULL;)
52
53
54
55

/** The source language specific language disambiguator function. */
static DISAMBIGUATOR_FUNC language_disambuigator = NULL;

56
57
58
/** The global memory disambiguator options. */
static unsigned global_mem_disamgig_opt = aa_opt_no_opt;

59
/* Returns a human readable name for an alias relation. */
60
61
const char *get_ir_alias_relation_name(ir_alias_relation rel)
{
62
63
#define X(a) case a: return #a
	switch (rel) {
Michael Beck's avatar
Michael Beck committed
64
65
66
	X(ir_no_alias);
	X(ir_may_alias);
	X(ir_sure_alias);
67
68
	default:
		panic("UNKNOWN alias relation");
69
70
71
72
	}
#undef X
}

73
/* Get the memory disambiguator options for a graph. */
74
75
unsigned get_irg_memory_disambiguator_options(const ir_graph *irg)
{
Michael Beck's avatar
Michael Beck committed
76
	unsigned opt = irg->mem_disambig_opt;
77
78
79
80
81
82
	if (opt & aa_opt_inherited)
		return global_mem_disamgig_opt;
	return opt;
}  /* get_irg_memory_disambiguator_options */

/*  Set the memory disambiguator options for a graph. */
83
84
void set_irg_memory_disambiguator_options(ir_graph *irg, unsigned options)
{
Michael Beck's avatar
Michael Beck committed
85
	irg->mem_disambig_opt = options & ~aa_opt_inherited;
86
87
88
}  /* set_irg_memory_disambiguator_options */

/* Set the global disambiguator options for all graphs not having local options. */
89
90
void set_irp_memory_disambiguator_options(unsigned options)
{
91
92
93
	global_mem_disamgig_opt = options;
}  /* set_irp_memory_disambiguator_options */

94
95
96
97
98
99
/* Get the base storage class (ignore modifier) */
ir_storage_class_class_t get_base_sc(ir_storage_class_class_t x)
{
	return x & ~ir_sc_modifiers;
}  /* get_base_sc */

100
/**
101
102
103
104
105
106
 * Find the base address and entity of an Sel node.
 *
 * @param sel  the node
 * @param pEnt after return points to the base entity.
 *
 * @return the base address.
107
 */
108
109
static ir_node *find_base_adr(const ir_node *sel, ir_entity **pEnt)
{
110
111
112
113
114
115
	ir_node *ptr = get_Sel_ptr(sel);

	while (is_Sel(ptr)) {
		sel = ptr;
		ptr = get_Sel_ptr(sel);
	}
116
	*pEnt = get_Sel_entity(sel);
117
118
119
	return ptr;
}  /* find_base_adr */

120
/**
Michael Beck's avatar
Michael Beck committed
121
122
 * Check if a given Const node is greater or equal a given size.
 *
Michael Beck's avatar
Michael Beck committed
123
124
125
 * @param cns   a Const node
 * @param size  a integer size
 *
Michael Beck's avatar
Michael Beck committed
126
 * @return ir_no_alias if the Const is greater, ir_may_alias else
127
 */
128
129
static ir_alias_relation check_const(const ir_node *cns, int size)
{
Matthias Braun's avatar
Matthias Braun committed
130
131
	ir_tarval *tv = get_Const_tarval(cns);
	ir_tarval *tv_size;
Michael Beck's avatar
Michael Beck committed
132
133

	if (size == 0)
Michael Beck's avatar
Michael Beck committed
134
		return tarval_is_null(tv) ? ir_may_alias : ir_no_alias;
Michael Beck's avatar
Michael Beck committed
135
	tv_size = new_tarval_from_long(size, get_tarval_mode(tv));
Michael Beck's avatar
Michael Beck committed
136
	return tarval_cmp(tv_size, tv) & (pn_Cmp_Eq|pn_Cmp_Lt) ? ir_no_alias : ir_may_alias;
Michael Beck's avatar
Michael Beck committed
137
}  /* check_const */
138

Michael Beck's avatar
Michael Beck committed
139
140
141
/**
 * Treat idx1 and idx2 as integer indexes and check if they differ always more than size.
 *
Michael Beck's avatar
Michael Beck committed
142
143
144
145
 * @param idx1  a node representing the first index
 * @param idx2  a node representing the second index
 * @param size  an integer size
 *
Michael Beck's avatar
Michael Beck committed
146
147
148
 * @return ir_sure_alias iff idx1 == idx2
 *         ir_no_alias iff they ALWAYS differ more than size
 *         ir_may_alias else
Michael Beck's avatar
Michael Beck committed
149
 */
150
151
static ir_alias_relation different_index(const ir_node *idx1, const ir_node *idx2, int size)
{
152
	if (idx1 == idx2)
Michael Beck's avatar
Michael Beck committed
153
		return ir_sure_alias;
154
155
	if (is_Const(idx1) && is_Const(idx2)) {
		/* both are const, we can compare them */
Matthias Braun's avatar
Matthias Braun committed
156
157
158
		ir_tarval *tv1 = get_Const_tarval(idx1);
		ir_tarval *tv2 = get_Const_tarval(idx2);
		ir_tarval *tv, *tv_size;
159
		ir_mode *m1, *m2;
Michael Beck's avatar
Michael Beck committed
160
161

		if (size == 0)
Michael Beck's avatar
Michael Beck committed
162
			return tv1 == tv2 ? ir_sure_alias : ir_no_alias;
Michael Beck's avatar
Michael Beck committed
163

164
165
166
167
168
169
170
171
172
173
174
		/* arg, modes may be different */
		m1 = get_tarval_mode(tv1);
		m2 = get_tarval_mode(tv2);
		if (m1 != m2) {
			int size = get_mode_size_bits(m1) - get_mode_size_bits(m2);

			if (size < 0) {
				/* m1 is a small mode, cast up */
				m1 = mode_is_signed(m1) ? find_signed_mode(m2) : find_unsigned_mode(m2);
				if (m1 == NULL) {
					/* should NOT happen, but if it does we give up here */
Michael Beck's avatar
Michael Beck committed
175
					return ir_may_alias;
176
177
178
179
180
181
182
				}
				tv1 = tarval_convert_to(tv1, m1);
			} else if (size > 0) {
				/* m2 is a small mode, cast up */
				m2 = mode_is_signed(m2) ? find_signed_mode(m1) : find_unsigned_mode(m1);
				if (m2 == NULL) {
					/* should NOT happen, but if it does we give up here */
Michael Beck's avatar
Michael Beck committed
183
					return ir_may_alias;
184
185
186
187
188
189
190
				}
				tv2 = tarval_convert_to(tv2, m2);
			}
			/* here the size should be identical, check for signed */
			if (get_mode_sign(m1) != get_mode_sign(m2)) {
				/* find the signed */
				if (mode_is_signed(m2)) {
Matthias Braun's avatar
Matthias Braun committed
191
					ir_tarval *t = tv1;
192
193
194
195
196
197
					ir_mode *tm = m1;
					tv1 = tv2; m1 = m2;
					tv2 = t;   m2 = tm;
				}

				/* m1 is now the signed one */
198
				if (!tarval_is_negative(tv1)) {
199
200
201
202
203
204
205
					/* tv1 is signed, but >= 0, simply cast into unsigned */
					tv1 = tarval_convert_to(tv1, m2);
				} else {
					tv_size = new_tarval_from_long(size, m2);

					if (tarval_cmp(tv2, tv_size) & (pn_Cmp_Eq|pn_Cmp_Gt)) {
						/* tv1 is negative and tv2 >= tv_size, so the difference is bigger than size */
Michael Beck's avatar
Michael Beck committed
206
						return ir_no_alias;
207
208
					}
					/* tv_size > tv2, so we can subtract without overflow */
209
					tv2 = tarval_sub(tv_size, tv2, NULL);
210
211
212
213
214
215
216
217

					/* tv1 is < 0, so we can negate it */
					tv1 = tarval_neg(tv1);

					/* cast it into unsigned. for two-complement it does the right thing for MIN_INT */
					tv1 = tarval_convert_to(tv1, m2);

					/* now we can compare without overflow */
Michael Beck's avatar
Michael Beck committed
218
					return tarval_cmp(tv1, tv2) & (pn_Cmp_Eq|pn_Cmp_Gt) ? ir_no_alias : ir_may_alias;
219
220
221
				}
			}
		}
Michael Beck's avatar
Michael Beck committed
222
		if (tarval_cmp(tv1, tv2) == pn_Cmp_Gt) {
Matthias Braun's avatar
Matthias Braun committed
223
			ir_tarval *t = tv1;
Michael Beck's avatar
Michael Beck committed
224
225
226
227
			tv1 = tv2;
			tv2 = t;
		}
		/* tv1 is now the "smaller" one */
228
		tv      = tarval_sub(tv2, tv1, NULL);
Michael Beck's avatar
Michael Beck committed
229
		tv_size = new_tarval_from_long(size, get_tarval_mode(tv));
Michael Beck's avatar
Michael Beck committed
230
		return tarval_cmp(tv_size, tv) & (pn_Cmp_Eq|pn_Cmp_Lt) ? ir_no_alias : ir_may_alias;
231
232
233
234
235
236
237
238
239
	}

	/* Note: we rely here on the fact that normalization puts constants on the RIGHT side */
	if (is_Add(idx1)) {
		ir_node *l1 = get_Add_left(idx1);
		ir_node *r1 = get_Add_right(idx1);

		if (l1 == idx2) {
			/* x + c == y */
Michael Beck's avatar
Michael Beck committed
240
241
			if (is_Const(r1))
				return check_const(r1, size);
242
243
		}
		if (is_Add(idx2)) {
244
			/* both are Adds, check if they are of x + a == x + b kind */
245
246
247
248
			ir_node *l2 = get_Add_left(idx2);
			ir_node *r2 = get_Add_right(idx2);

			if (l1 == l2)
Michael Beck's avatar
Michael Beck committed
249
				return different_index(r1, r2, size);
250
			else if (l1 == r2)
Michael Beck's avatar
Michael Beck committed
251
				return different_index(r1, l2, size);
252
			else if (r1 == r2)
Michael Beck's avatar
Michael Beck committed
253
				return different_index(l1, l2, size);
254
			else if (r1 == l2)
Michael Beck's avatar
Michael Beck committed
255
				return different_index(l1, r2, size);
256
257
258
259
260
261
262
263
		}
	}
	if (is_Add(idx2)) {
		ir_node *l2 = get_Add_left(idx2);
		ir_node *r2 = get_Add_right(idx2);

		if (l2 == idx1) {
			/* x + c == y */
Michael Beck's avatar
Michael Beck committed
264
265
			if (is_Const(r2))
				return check_const(r2, size);
266
267
268
269
270
271
272
273
274
		}
	}

	if (is_Sub(idx1)) {
		ir_node *l1 = get_Sub_left(idx1);
		ir_node *r1 = get_Sub_right(idx1);

		if (l1 == idx2) {
			/* x - c == y */
Michael Beck's avatar
Michael Beck committed
275
276
			if (is_Const(r1))
				return check_const(r1, size);
277
278
279
		}

		if (is_Sub(idx2)) {
280
			/* both are Subs, check if they are of x - a == x - b kind */
281
282
283
284
			ir_node *l2 = get_Sub_left(idx2);

			if (l1 == l2) {
				ir_node *r2 = get_Sub_right(idx2);
Michael Beck's avatar
Michael Beck committed
285
				return different_index(r1, r2, size);
286
287
288
289
290
291
292
293
294
			}
		}
	}
	if (is_Sub(idx2)) {
		ir_node *l2 = get_Sub_left(idx2);
		ir_node *r2 = get_Sub_right(idx2);

		if (l2 == idx1) {
			/* x - c == y */
Michael Beck's avatar
Michael Beck committed
295
296
			if (is_Const(r2))
				return check_const(r2, size);
297
298
299
		}

	}
Michael Beck's avatar
Michael Beck committed
300
	return ir_may_alias;
301
302
303
}  /* different_index */

/**
304
305
 * Two Sel addresses have the same base address, check if there offsets are
 * different.
306
307
308
309
 *
 * @param adr1  The first address.
 * @param adr2  The second address.
 */
310
311
static ir_alias_relation different_sel_offsets(const ir_node *sel1, const ir_node *sel2)
{
312
	/* seems to be broken */
313
314
	(void) sel1;
	(void) sel2;
315
#if 0
316
317
318
319
320
321
322
323
324
325
326
327
328
	ir_entity *ent1 = get_Sel_entity(sel1);
	ir_entity *ent2 = get_Sel_entity(sel2);
	int i, check_arr = 0;

	if (ent1 == ent2)
		check_arr = 1;
	else {
		ir_type *tp1 = get_entity_type(ent1);
		ir_type *tp2 = get_entity_type(ent2);

		if (tp1 == tp2)
			check_arr = 1;
		else if (get_type_state(tp1) == layout_fixed && get_type_state(tp2) == layout_fixed &&
329
		         get_type_size_bits(tp1) == get_type_size_bits(tp2))
330
331
332
333
334
335
336
337
338
339
340
341
			check_arr = 1;
	}
	if (check_arr) {
		/* we select an entity of same size, check for indexes */
		int n = get_Sel_n_indexs(sel1);
		int have_no = 0;

		if (n > 0 && n == get_Sel_n_indexs(sel2)) {
			/* same non-zero number of indexes, an array access, check */
			for (i = 0; i < n; ++i) {
				ir_node *idx1 = get_Sel_index(sel1, i);
				ir_node *idx2 = get_Sel_index(sel2, i);
Michael Beck's avatar
Michael Beck committed
342
				ir_alias_relation res = different_index(idx1, idx2, 0); /* we can safely IGNORE the size here if it's at least >0 */
343
344
345
346
347
348
349
350
351
352

				if (res == may_alias)
					return may_alias;
				else if (res == no_alias)
					have_no = 1;
			}
			/* if we have at least one no_alias, there is no alias relation, else we have sure */
			return have_no > 0 ? no_alias : sure_alias;
		}
	}
Matthias Braun's avatar
Matthias Braun committed
353
354
#else
	(void) different_index;
355
#endif
Michael Beck's avatar
Michael Beck committed
356
	return ir_may_alias;
357
358
}  /* different_sel_offsets */

359
360
361
/**
 * Determine the alias relation by checking if adr1 and adr2 are pointer
 * to different type.
362
363
364
 *
 * @param adr1    The first address.
 * @param adr2    The second address.
365
 */
Michael Beck's avatar
Michael Beck committed
366
static ir_alias_relation different_types(const ir_node *adr1, const ir_node *adr2)
367
368
369
{
	ir_entity *ent1 = NULL, *ent2 = NULL;

370
371
	if (is_Global(adr1))
		ent1 = get_Global_entity(adr1);
372
373
374
	else if (is_Sel(adr1))
		ent1 = get_Sel_entity(adr1);

375
376
	if (is_Global(adr2))
		ent2 = get_Global_entity(adr2);
377
378
379
380
381
382
383
384
	else if (is_Sel(adr2))
		ent2 = get_Sel_entity(adr2);

	if (ent1 != NULL && ent2 != NULL) {
		ir_type *tp1 = get_entity_type(ent1);
		ir_type *tp2 = get_entity_type(ent2);

		if (tp1 != tp2) {
Michael Beck's avatar
Michael Beck committed
385
386
387
388
			/* do deref until no pointer types are found */
			while (is_Pointer_type(tp1) && is_Pointer_type(tp2)) {
				tp1 = get_pointer_points_to_type(tp1);
				tp2 = get_pointer_points_to_type(tp2);
389
390
391
392
			}

			if (get_type_tpop(tp1) != get_type_tpop(tp2)) {
				/* different type structure */
Michael Beck's avatar
Michael Beck committed
393
				return ir_no_alias;
394
395
396
397
398
			}
			if (is_Class_type(tp1)) {
				/* check class hierarchy */
				if (! is_SubClass_of(tp1, tp2) &&
					! is_SubClass_of(tp2, tp1))
Michael Beck's avatar
Michael Beck committed
399
					return ir_no_alias;
400
401
			} else {
				/* different types */
Michael Beck's avatar
Michael Beck committed
402
				return ir_no_alias;
403
404
405
			}
		}
	}
Michael Beck's avatar
Michael Beck committed
406
	return ir_may_alias;
407
408
}  /* different_types */

409
410
411
412
413
/**
 * Returns non-zero if a node is a result on a malloc-like routine.
 *
 * @param node  the Proj node to test
 */
414
415
static int is_malloc_Result(const ir_node *node)
{
416
417
418
419
420
421
422
	node = get_Proj_pred(node);
	if (! is_Proj(node))
		return 0;
	node = get_Proj_pred(node);
	if (! is_Call(node))
		return 0;
	node = get_Call_ptr(node);
423
424
	if (is_Global(node)) {
		ir_entity *ent = get_Global_entity(node);
425
426
427
428
429
430
431
432

		if (get_entity_additional_properties(ent) & mtp_property_malloc)
			return 1;
		return 0;
	}
	return 0;
}  /* is_malloc_Result */

433
434
ir_storage_class_class_t classify_pointer(const ir_node *irn,
                                          const ir_entity *ent)
435
{
436
	ir_graph *irg = get_irn_irg(irn);
Michael Beck's avatar
Michael Beck committed
437
	ir_storage_class_class_t res = ir_sc_pointer;
438
439
	if (is_Global(irn)) {
		ir_entity *entity = get_Global_entity(irn);
Michael Beck's avatar
Michael Beck committed
440
		res = ir_sc_globalvar;
441
		if (! (get_entity_usage(entity) & ir_usage_address_taken))
Michael Beck's avatar
Michael Beck committed
442
			res |= ir_sc_modifier_nottaken;
Michael Beck's avatar
Fixed:    
Michael Beck committed
443
	} else if (irn == get_irg_frame(irg)) {
Michael Beck's avatar
Michael Beck committed
444
		res = ir_sc_localvar;
445
		if (ent != NULL && !(get_entity_usage(ent) & ir_usage_address_taken))
Michael Beck's avatar
Michael Beck committed
446
			res |= ir_sc_modifier_nottaken;
Michael Beck's avatar
Fixed:    
Michael Beck committed
447
	} else if (irn == get_irg_tls(irg)) {
Michael Beck's avatar
Michael Beck committed
448
		res = ir_sc_tls;
449
		if (ent != NULL && !(get_entity_usage(ent) & ir_usage_address_taken))
Michael Beck's avatar
Michael Beck committed
450
			res |= ir_sc_modifier_nottaken;
451
	} else if (is_Proj(irn) && is_malloc_Result(irn)) {
Michael Beck's avatar
Michael Beck committed
452
		return ir_sc_malloced;
453
454
	} else if (is_Const(irn)) {
		return ir_sc_globaladdr;
455
456
	} else if (is_arg_Proj(irn)) {
		res |= ir_sc_modifier_argument;
457
458
	}

Michael Beck's avatar
Fixed:    
Michael Beck committed
459
	return res;
460
461
}

462
463
464
/**
 * If adr represents a Bitfield Sel, skip it
 */
465
466
static const ir_node *skip_Bitfield_Sels(const ir_node *adr)
{
467
468
469
470
471
472
473
474
475
476
477
	if (is_Sel(adr)) {
		ir_entity *ent     = get_Sel_entity(adr);
		ir_type   *bf_type = get_entity_type(ent);

		/* is it a bitfield type? */
		if (is_Primitive_type(bf_type) && get_primitive_base_type(bf_type) != NULL)
			adr = get_Sel_ptr(adr);
	}
	return adr;
}

478
479
/**
 * Determine the alias relation between two addresses.
Michael Beck's avatar
Michael Beck committed
480
481
482
483
484
485
 *
 * @param addr1  pointer address of the first memory operation
 * @param mode1  the mode of the accessed data through addr1
 * @param addr2  pointer address of the second memory operation
 * @param mode2  the mode of the accessed data through addr2
 *
Michael Beck's avatar
Michael Beck committed
486
 * @return found memory relation
487
488
 */
static ir_alias_relation _get_alias_relation(
Michael Beck's avatar
Michael Beck committed
489
490
	const ir_node *adr1, const ir_mode *mode1,
	const ir_node *adr2, const ir_mode *mode2)
491
{
492
	ir_entity             *ent1, *ent2;
Michael Beck's avatar
Fixed:    
Michael Beck committed
493
494
495
	unsigned              options;
	long                  offset1 = 0;
	long                  offset2 = 0;
Michael Beck's avatar
Michael Beck committed
496
497
498
499
	const ir_node         *base1;
	const ir_node         *base2;
	const ir_node         *orig_adr1 = adr1;
	const ir_node         *orig_adr2 = adr2;
500
	ir_graph              *irg;
Michael Beck's avatar
Fixed:    
Michael Beck committed
501
	unsigned              mode_size;
502
	ir_storage_class_class_t class1, class2, mod1, mod2;
Michael Beck's avatar
Fixed:    
Michael Beck committed
503
	int                   have_const_offsets;
504
505

	if (! get_opt_alias_analysis())
Michael Beck's avatar
Michael Beck committed
506
		return ir_may_alias;
507
508

	if (adr1 == adr2)
Michael Beck's avatar
Michael Beck committed
509
		return ir_sure_alias;
510

511
	irg = get_irn_irg(adr1);
512
513
514
515
	options = get_irg_memory_disambiguator_options(irg);

	/* The Armageddon switch */
	if (options & aa_opt_no_alias)
Michael Beck's avatar
Michael Beck committed
516
		return ir_no_alias;
517

518
519
520
	/* do the addresses have constants offsets?
	 *  Note: nodes are normalized to have constants at right inputs,
	 *        sub X, C is normalized to add X, -C
Michael Beck's avatar
Fixed:    
Michael Beck committed
521
522
	 */
	have_const_offsets = 1;
523
524
	while (is_Add(adr1)) {
		ir_node *add_right = get_Add_right(adr1);
Michael Beck's avatar
Michael Beck committed
525
		if (is_Const(add_right) && !mode_is_reference(get_irn_mode(add_right))) {
Matthias Braun's avatar
Matthias Braun committed
526
			ir_tarval *tv  = get_Const_tarval(add_right);
527
528
			offset1    += get_tarval_long(tv);
			adr1        = get_Add_left(adr1);
Michael Beck's avatar
Fixed:    
Michael Beck committed
529
530
531
532
533
534
		} else if (mode_is_reference(get_irn_mode(add_right))) {
			adr1 = add_right;
			have_const_offsets = 0;
		} else {
			adr1 = get_Add_left(adr1);
			have_const_offsets = 0;
535
536
537
538
		}
	}
	while (is_Add(adr2)) {
		ir_node *add_right = get_Add_right(adr2);
Michael Beck's avatar
Michael Beck committed
539
		if (is_Const(add_right) && !mode_is_reference(get_irn_mode(add_right))) {
Matthias Braun's avatar
Matthias Braun committed
540
			ir_tarval *tv  = get_Const_tarval(add_right);
541
542
			offset2    += get_tarval_long(tv);
			adr2        = get_Add_left(adr2);
Michael Beck's avatar
Fixed:    
Michael Beck committed
543
544
545
546
547
548
		} else if (mode_is_reference(get_irn_mode(add_right))) {
			adr2 = add_right;
			have_const_offsets = 0;
		} else {
			adr2 = get_Add_left(adr2);
			have_const_offsets = 0;
549
		}
550
551
	}

552
553
554
555
	mode_size = get_mode_size_bytes(mode1);
	if (get_mode_size_bytes(mode2) > mode_size) {
		mode_size = get_mode_size_bytes(mode2);
	}
556

Michael Beck's avatar
Fixed:    
Michael Beck committed
557
558
559
560
561
	/* same base address -> compare offsets if possible.
	 * FIXME: type long is not sufficient for this task ...
	 */
	if (adr1 == adr2 && have_const_offsets) {
		if ((unsigned long)labs(offset2 - offset1) >= mode_size)
Michael Beck's avatar
Michael Beck committed
562
			return ir_no_alias;
563
		else
Michael Beck's avatar
Michael Beck committed
564
			return ir_sure_alias;
565
	}
566

567
568
569
	/*
	 * Bitfields can be constructed as Sels from its base address.
	 * As they have different entities, the disambiguator would find that they are
Michael Beck's avatar
Michael Beck committed
570
	 * alias free. While this is true for it's values, it is false for the addresses
571
572
573
574
575
576
	 * (strictly speaking, the Sel's are NOT the addresses of the bitfields).
	 * So, skip those bitfield selecting Sel's.
	 */
	adr1 = skip_Bitfield_Sels(adr1);
	adr2 = skip_Bitfield_Sels(adr2);

Michael Beck's avatar
Michael Beck committed
577
	/* skip Sels */
578
579
	base1 = adr1;
	base2 = adr2;
Michael Beck's avatar
Fixed:    
Michael Beck committed
580
581
	ent1  = NULL;
	ent2  = NULL;
582
	if (is_Sel(adr1)) {
Michael Beck's avatar
Fixed:    
Michael Beck committed
583
		base1 = find_base_adr(adr1, &ent1);
584
585
	}
	if (is_Sel(adr2)) {
Michael Beck's avatar
Fixed:    
Michael Beck committed
586
		base2 = find_base_adr(adr2, &ent2);
587
	}
588

Michael Beck's avatar
Michael Beck committed
589
	/* same base address -> compare Sel entities */
Michael Beck's avatar
Fixed:    
Michael Beck committed
590
	if (base1 == base2 && ent1 != NULL && ent2 != NULL) {
591
		if (ent1 != ent2)
Michael Beck's avatar
Michael Beck committed
592
			return ir_no_alias;
Michael Beck's avatar
Fixed:    
Michael Beck committed
593
594
		else if (have_const_offsets)
			return different_sel_offsets(adr1, adr2);
595
	}
596

597
598
	mod1 = classify_pointer(base1, ent1);
	mod2 = classify_pointer(base2, ent2);
599

600
601
	class1 = get_base_sc(mod1);
	class2 = get_base_sc(mod2);
Michael Beck's avatar
Michael Beck committed
602

603
604
605
606
607
608
	if (class1 == ir_sc_pointer || class2 == ir_sc_pointer) {
		/* swap pointer class to class1 */
		if (class2 == ir_sc_pointer) {
			ir_storage_class_class_t temp = mod1;
			mod1 = mod2;
			mod2 = temp;
609
610
			class1 = get_base_sc(mod1);
			class2 = get_base_sc(mod2);
Michael Beck's avatar
Michael Beck committed
611
		}
612
613
		/* a pointer and an object whose address was never taken */
		if (mod2 & ir_sc_modifier_nottaken) {
Michael Beck's avatar
Michael Beck committed
614
615
			return ir_no_alias;
		}
616
617
618
		if (mod1 & ir_sc_modifier_argument) {
			if ( (options & aa_opt_no_alias_args)
					&& (mod2 & ir_sc_modifier_argument))
619
				return ir_no_alias;
620
621
622
623
			if ( (options & aa_opt_no_alias_args_global)
					&& (class2 == ir_sc_globalvar
						|| class2 == ir_sc_tls
						|| class2 == ir_sc_globaladdr))
624
625
				return ir_no_alias;
		}
Michael Beck's avatar
Michael Beck committed
626
627
628
629
630
631
632
633
634
635
636
637
638
	} else if (class1 != class2) {
		/* two objects from different memory spaces */
		return ir_no_alias;
	} else {
		/* both classes are equal */
		if (class1 == ir_sc_globalvar) {
			ir_entity *entity1 = get_SymConst_entity(base1);
			ir_entity *entity2 = get_SymConst_entity(base2);
			if (entity1 != entity2)
				return ir_no_alias;

			/* for some reason CSE didn't happen yet for the 2 SymConsts... */
			return ir_may_alias;
639
		} else if (class1 == ir_sc_globaladdr) {
Matthias Braun's avatar
Matthias Braun committed
640
641
642
643
			ir_tarval *tv = get_Const_tarval(base1);
			offset1      += get_tarval_long(tv);
			tv            = get_Const_tarval(base2);
			offset2      += get_tarval_long(tv);
644
645
646
647
648

			if ((unsigned long)labs(offset2 - offset1) >= mode_size)
				return ir_no_alias;
			else
				return ir_sure_alias;
Michael Beck's avatar
Michael Beck committed
649
650
651
652
		}
	}

	/* Type based alias analysis */
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
	if (options & aa_opt_type_based) {
		ir_alias_relation rel;

		if (options & aa_opt_byte_type_may_alias) {
			if (get_mode_size_bits(mode1) == 8 || get_mode_size_bits(mode2) == 8) {
				/* One of the modes address a byte. Assume a ir_may_alias and leave
				   the type based check. */
				goto leave_type_based_alias;
			}
		}
		/* cheap check: If the mode sizes did not match, the types MUST be different */
		if (get_mode_size_bits(mode1) != get_mode_size_bits(mode2))
			return ir_no_alias;

		/* cheap test: if only one is a reference mode, no alias */
		if (mode_is_reference(mode1) != mode_is_reference(mode2))
			return ir_no_alias;

Michael Beck's avatar
Michael Beck committed
671
672
673
674
		/* cheap test: if arithmetic is different, no alias */
		if (get_mode_arithmetic(mode1) != get_mode_arithmetic(mode2))
			return ir_no_alias;

675
676
677
678
679
680
681
		/* try rule R5 */
		rel = different_types(orig_adr1, orig_adr2);
		if (rel != ir_may_alias)
			return rel;
leave_type_based_alias:;
	}

682
	/* do we have a language specific memory disambiguator? */
683
	if (language_disambuigator != NULL) {
684
		ir_alias_relation rel = language_disambuigator(orig_adr1, mode1, orig_adr2, mode2);
Michael Beck's avatar
Michael Beck committed
685
		if (rel != ir_may_alias)
686
687
688
689
			return rel;
	}

	/* access points-to information here */
Michael Beck's avatar
Michael Beck committed
690
	return ir_may_alias;
691
692
693
694
695
696
}  /* _get_alias_relation */

/*
 * Determine the alias relation between two addresses.
 */
ir_alias_relation get_alias_relation(
Michael Beck's avatar
Michael Beck committed
697
698
	const ir_node *adr1, const ir_mode *mode1,
	const ir_node *adr2, const ir_mode *mode2)
699
{
700
	ir_alias_relation rel = _get_alias_relation(adr1, mode1, adr2, mode2);
701
	DB((dbg, LEVEL_1, "alias(%+F, %+F) = %s\n", adr1, adr2, get_ir_alias_relation_name(rel)));
702
703
704
705
	return rel;
}  /* get_alias_relation */

/* Set a source language specific memory disambiguator function. */
706
707
void set_language_memory_disambiguator(DISAMBIGUATOR_FUNC func)
{
708
709
710
711
712
713
714
715
	language_disambuigator = func;
}  /* set_language_memory_disambiguator */

/** The result cache for the memory disambiguator. */
static set *result_cache = NULL;

/** An entry in the relation cache. */
typedef struct mem_disambig_entry {
716
717
718
719
	const ir_node     *adr1;    /**< The first address. */
	const ir_mode     *mode1;   /**< The first address mode. */
	const ir_node     *adr2;    /**< The second address. */
	const ir_mode     *mode2;   /**< The second address mode. */
720
721
722
	ir_alias_relation result;   /**< The alias relation result. */
} mem_disambig_entry;

723
#define HASH_ENTRY(adr1, adr2)  (HASH_PTR(adr1) ^ HASH_PTR(adr2))
724
725
726
727

/**
 * Compare two relation cache entries.
 */
728
729
static int cmp_mem_disambig_entry(const void *elt, const void *key, size_t size)
{
730
731
	const mem_disambig_entry *p1 = (const mem_disambig_entry*) elt;
	const mem_disambig_entry *p2 = (const mem_disambig_entry*) key;
Matthias Braun's avatar
Matthias Braun committed
732
	(void) size;
733

Michael Beck's avatar
Michael Beck committed
734
735
	return p1->adr1 == p2->adr1 && p1->adr2 == p2->adr2 &&
	       p1->mode1 == p2->mode1 && p1->mode2 == p2->mode2;
736
737
738
739
740
}  /* cmp_mem_disambig_entry */

/**
 * Initialize the relation cache.
 */
741
742
void mem_disambig_init(void)
{
743
744
745
746
747
748
749
	result_cache = new_set(cmp_mem_disambig_entry, 8);
}  /* mem_disambig_init */

/*
 * Determine the alias relation between two addresses.
 */
ir_alias_relation get_alias_relation_ex(
Michael Beck's avatar
Michael Beck committed
750
751
	const ir_node *adr1, const ir_mode *mode1,
	const ir_node *adr2, const ir_mode *mode2)
752
753
754
{
	mem_disambig_entry key, *entry;

755
756
	ir_fprintf(stderr, "%+F <-> %+F\n", adr1, adr2);

757
	if (! get_opt_alias_analysis())
Michael Beck's avatar
Michael Beck committed
758
		return ir_may_alias;
759
760

	if (get_irn_opcode(adr1) > get_irn_opcode(adr2)) {
Michael Beck's avatar
Michael Beck committed
761
		const ir_node *t = adr1;
762
763
764
765
		adr1 = adr2;
		adr2 = t;
	}

Michael Beck's avatar
Michael Beck committed
766
767
768
769
	key.adr1  = adr1;
	key.adr2  = adr2;
	key.mode1 = mode1;
	key.mode2 = mode2;
770
	entry = (mem_disambig_entry*) set_find(result_cache, &key, sizeof(key), HASH_ENTRY(adr1, adr2));
771
	if (entry != NULL)
772
773
		return entry->result;

774
	key.result = get_alias_relation(adr1, mode1, adr2, mode2);
775
776
777
778
779
780

	set_insert(result_cache, &key, sizeof(key), HASH_ENTRY(adr1, adr2));
	return key.result;
}  /* get_alias_relation_ex */

/* Free the relation cache. */
781
782
void mem_disambig_term(void)
{
783
	if (result_cache != NULL) {
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
		del_set(result_cache);
		result_cache = NULL;
	}
}  /* mem_disambig_term */

/**
 * Check the mode of a Load/Store with the mode of the entity
 * that is accessed.
 * If the mode of the entity and the Load/Store mode do not match, we
 * have the bad reinterpret case:
 *
 * int i;
 * char b = *(char *)&i;
 *
 * We do NOT count this as one value and return address_taken
 * in that case.
 * However, we support an often used case. If the mode is two-complement
 * we allow casts between signed/unsigned.
 *
 * @param mode     the mode of the Load/Store
 * @param ent_mode the mode of the accessed entity
 *
 * @return non-zero if the Load/Store is a hidden cast, zero else
 */
808
809
static int is_hidden_cast(const ir_mode *mode, const ir_mode *ent_mode)
{
810
811
812
	if (ent_mode == NULL)
		return false;

813
814
815
816
817
818
	if (ent_mode != mode) {
		if (ent_mode == NULL ||
			get_mode_size_bits(ent_mode) != get_mode_size_bits(mode) ||
			get_mode_sort(ent_mode) != get_mode_sort(mode) ||
			get_mode_arithmetic(ent_mode) != irma_twos_complement ||
			get_mode_arithmetic(mode) != irma_twos_complement)
819
			return true;
820
	}
821
	return false;
822
823
824
}  /* is_hidden_cast */

/**
Christoph Mallon's avatar
Christoph Mallon committed
825
 * Determine the usage state of a node (or its successor Sels).
826
827
828
 *
 * @param irn  the node
 */
829
830
static ir_entity_usage determine_entity_usage(const ir_node *irn, ir_entity *entity)
{
831
	int       i;
832
833
834
	ir_mode   *emode, *mode;
	ir_node   *value;
	ir_type   *tp;
835
	unsigned   res = 0;
836
837
838
839
840
841

	for (i = get_irn_n_outs(irn) - 1; i >= 0; --i) {
		ir_node *succ = get_irn_out(irn, i);

		switch (get_irn_opcode(succ)) {
		case iro_Load:
842
843
			/* beware: irn might be a Id node here, so irn might be not
			   equal to get_Load_ptr(succ) */
844
845
			res |= ir_usage_read;

846
			/* check if this load is not a hidden conversion */
847
848
			mode  = get_Load_mode(succ);
			emode = get_type_mode(get_entity_type(entity));
849
			if (is_hidden_cast(mode, emode))
850
				res |= ir_usage_reinterpret_cast;
851
852
853
854
			break;

		case iro_Store:
			/* check that the node is not the Store's value */
855
856
857
858
859
860
861
862
863
864
865
866
867
868
			if (irn == get_Store_value(succ)) {
				res |= ir_usage_unknown;
			}
			if (irn == get_Store_ptr(succ)) {
				res |= ir_usage_write;

				/* check if this Store is not a hidden conversion */
				value = get_Store_value(succ);
				mode  = get_irn_mode(value);
				emode = get_type_mode(get_entity_type(entity));
				if (is_hidden_cast(mode, emode))
					res |= ir_usage_reinterpret_cast;
			}
			assert(irn != get_Store_mem(succ));
869
870
			break;

871
872
		case iro_CopyB:
			/* CopyB are like Loads/Stores */
Matthias Braun's avatar
fix    
Matthias Braun committed
873
			tp  = get_entity_type(entity);
874
875
			if (tp != get_CopyB_type(succ)) {
				/* bad, different types, might be a hidden conversion */
876
877
878
879
880
881
882
				res |= ir_usage_reinterpret_cast;
			}
			if (irn == get_CopyB_dst(succ)) {
				res |= ir_usage_write;
			} else {
				assert(irn == get_CopyB_src(succ));
				res |= ir_usage_read;
883
884
885
			}
			break;

886
887
		case iro_Add:
		case iro_Sub:
888
889
890
			/* Check the successor of irn. */
			res |= determine_entity_usage(succ, entity);
			break;
891
		case iro_Sel: {
892
			ir_entity *entity = get_Sel_entity(succ);
Michael Beck's avatar
Michael Beck committed
893
			/* this analysis can't handle unions correctly */
894
			if (is_Union_type(get_entity_owner(entity))) {
Matthias Braun's avatar
fix    
Matthias Braun committed
895
				res |= ir_usage_unknown;
896
897
				break;
			}
898
			/* Check the successor of irn. */
899
			res |= determine_entity_usage(succ, entity);
900
901
902
903
			break;
		}

		case iro_Call:
904
905
906
907
908
909
910
911
912
			if (irn == get_Call_ptr(succ)) {
				/* TODO: we could check for reinterpret casts here...
				 * But I doubt anyone is interested in that bit for
				 * function entities and I'm too lazy to write the code now.
				 */
				res |= ir_usage_read;
			} else {
				assert(irn != get_Call_mem(succ));
				res |= ir_usage_unknown;
913
914
			}
			break;
915

916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
		/* skip identities */
		case iro_Id:
			res |= determine_entity_usage(succ, entity);
			break;

		/* skip tuples */
		case iro_Tuple: {
			int input_nr;
			for (input_nr = get_Tuple_n_preds(succ) - 1; input_nr >= 0;
					--input_nr) {
				ir_node *pred = get_Tuple_pred(succ, input_nr);
				if (pred == irn) {
					int k;
					/* we found one input */
					for (k = get_irn_n_outs(succ) - 1; k >= 0; --k) {
						ir_node *proj = get_irn_out(succ, k);

						if (is_Proj(proj) && get_Proj_proj(proj) == input_nr) {
							res |= determine_entity_usage(proj, entity);
							break;
						}
					}
				}
			}
			break;
		}
942

943
		default:
944
945
			/* another op, we don't know anything (we could do more advanced
			 * things like a dataflow analysis here) */
946
947
			res |= ir_usage_unknown;
			break;
948
949
		}
	}
950

951
	return (ir_entity_usage) res;
952
}
953
954

/**
955
 * Update the usage flags of all frame entities.
956
 */
957
958
static void analyse_irg_entity_usage(ir_graph *irg)
{
959
	ir_type *ft = get_irg_frame_type(irg);
960
	ir_node *irg_frame;
Michael Beck's avatar
Michael Beck committed
961
962
	size_t i, n;
	int j, k, static_link_arg;
963
964

	/* set initial state to not_taken, as this is the "smallest" state */
Michael Beck's avatar
Michael Beck committed
965
	for (i = 0, n = get_class_n_members(ft); i < n; ++i) {
966
		ir_entity *ent = get_class_member(ft, i);
967

968
969
		/* methods can only be analyzed globally */
		if (! is_method_entity(ent)) {
970
			ir_entity_usage flags = ir_usage_none;
Matthias Braun's avatar
Matthias Braun committed
971
972
			if (get_entity_linkage(ent) & IR_LINKAGE_HIDDEN_USER)
				flags = ir_usage_unknown;
973
974
			set_entity_usage(ent, flags);
		}
975
976
977
978
979
980
	}

	assure_irg_outs(irg);

	irg_frame = get_irg_frame(irg);

Michael Beck's avatar
Michael Beck committed
981
982
	for (j = get_irn_n_outs(irg_frame) - 1; j >= 0; --j) {
		ir_node        *succ = get_irn_out(irg_frame, j);
983
		ir_entity      *entity;
984
		unsigned        flags;
985

Michael Beck's avatar
Michael Beck committed
986
		if (!is_Sel(succ))
987
			continue;
988

989
990
991
		entity = get_Sel_entity(succ);
		flags  = get_entity_usage(entity);
		flags |= determine_entity_usage(succ, entity);
992
		set_entity_usage(entity, (ir_entity_usage) flags);
993
	}
994

995
996
	/* check inner functions accessing outer frame */
	static_link_arg = 0;
Michael Beck's avatar
Michael Beck committed
997
	for (i = 0, n = get_class_n_members(ft); i < n; ++i) {
998
		ir_entity *ent = get_class_member(ft, i);
999
1000
		ir_graph  *inner_irg;
		ir_node   *args;