ldstopt.c 63.9 KB
Newer Older
Christian Würdig's avatar
Christian Würdig committed
1
2
/*
 * This file is part of libFirm.
3
 * Copyright (C) 2012 University of Karlsruhe.
Christian Würdig's avatar
Christian Würdig committed
4
5
 */

Michael Beck's avatar
Michael Beck committed
6
7
8
9
/**
 * @file
 * @brief   Load/Store optimizations.
 * @author  Michael Beck
Michael Beck's avatar
Michael Beck committed
10
 */
11
#include <string.h>
Michael Beck's avatar
Michael Beck committed
12

Matthias Braun's avatar
Matthias Braun committed
13
14
15
16
#include "array.h"
#include "be.h"
#include "dbginfo_t.h"
#include "debug.h"
17
#include "entity_t.h"
Matthias Braun's avatar
Matthias Braun committed
18
#include "panic.h"
19
#include "ircons_t.h"
Matthias Braun's avatar
Matthias Braun committed
20
21
#include "iredges.h"
#include "irflag_t.h"
22
#include "irgmod.h"
Matthias Braun's avatar
Matthias Braun committed
23
24
#include "irgopt.h"
#include "irgraph_t.h"
25
26
#include "irgwalk.h"
#include "irhooks.h"
27
#include "irmemory.h"
Matthias Braun's avatar
Matthias Braun committed
28
29
#include "irmode_t.h"
#include "irnode_t.h"
30
#include "irnodehashmap.h"
Matthias Braun's avatar
Matthias Braun committed
31
32
33
34
#include "iropt_dbg.h"
#include "iropt_t.h"
#include "iroptimize.h"
#include "irtools.h"
35
#include "set.h"
Matthias Braun's avatar
Matthias Braun committed
36
37
#include "tv_t.h"
#include "type_t.h"
38
#include "util.h"
39
40
41

/** The debug handle. */
DEBUG_ONLY(static firm_dbg_module_t *dbg;)
42

43
#define MAX_PROJ MAX(MAX((unsigned)pn_Load_max, (unsigned)pn_Store_max), (unsigned)pn_Call_max)
Michael Beck's avatar
Michael Beck committed
44

Matthias Braun's avatar
Matthias Braun committed
45
46
typedef enum changes_t {
	NO_CHANGES = 0,
47
48
49
50
51
	DF_CHANGED = (1 << 0), /**< data flow changed */
	CF_CHANGED = (1 << 1), /**< control flow changed */
	/** nodes have been created but are not reachable. This is a bad hack
	 * try hard to avoid it! */
	NODES_CREATED = (1 << 2),
Matthias Braun's avatar
Matthias Braun committed
52
} changes_t;
53

Michael Beck's avatar
Michael Beck committed
54
55
56
/**
 * walker environment
 */
57
typedef struct walk_env_t {
Matthias Braun's avatar
Matthias Braun committed
58
59
	struct obstack obst;    /**< list of all stores */
	changes_t      changes; /**< a bitmask of graph changes */
Michael Beck's avatar
Michael Beck committed
60
61
} walk_env_t;

62
/** A Load/Store info. */
63
typedef struct ldst_info_t {
Matthias Braun's avatar
Matthias Braun committed
64
65
66
67
	ir_node  *projs[MAX_PROJ+1]; /**< list of Proj's of this node */
	ir_node  *exc_block;         /**< the exception block if available */
	int      exc_idx;            /**< predecessor index in exception block */
	unsigned visited;            /**< visited counter for breaking loops */
Michael Beck's avatar
Michael Beck committed
68
69
} ldst_info_t;

70
71
72
73
74
75
76
77
78
79
80
81
typedef struct base_offset_t {
	ir_node *base;
	long     offset;
} base_offset_t;

typedef struct track_load_env_t {
	ir_node      *load;
	base_offset_t base_offset;
	ir_node      *ptr; /* deprecated: alternative representation of
	                      base_offset */
} track_load_env_t;

82
/**
83
 * flags for control flow.
84
 */
Matthias Braun's avatar
Matthias Braun committed
85
86
87
88
typedef enum block_flags_t {
	BLOCK_HAS_COND = (1 << 0), /**< Block has conditional control flow */
	BLOCK_HAS_EXC  = (1 << 1), /**< Block has exceptional control flow */
} block_flags_t;
89
90

/**
91
 * a Block info.
92
 */
93
typedef struct block_info_t {
Matthias Braun's avatar
Matthias Braun committed
94
	block_flags_t flags;  /**< flags for the block */
95
96
} block_info_t;

97
/** the master visited flag for loop detection. */
Matthias Braun's avatar
Matthias Braun committed
98
static unsigned master_visited;
99
100
101
102
103

#define INC_MASTER()       ++master_visited
#define MARK_NODE(info)    (info)->visited = master_visited
#define NODE_VISITED(info) (info)->visited >= master_visited

Michael Beck's avatar
Michael Beck committed
104
105
106
/**
 * get the Load/Store info of a node
 */
107
108
static ldst_info_t *get_ldst_info(ir_node *node, struct obstack *obst)
{
109
	ldst_info_t *info = (ldst_info_t*)get_irn_link(node);
Matthias Braun's avatar
Matthias Braun committed
110
	if (info == NULL) {
111
		info = OALLOCZ(obst, ldst_info_t);
112
113
114
		set_irn_link(node, info);
	}
	return info;
115
}
Michael Beck's avatar
Michael Beck committed
116

117
118
119
/**
 * get the Block info of a node
 */
120
121
static block_info_t *get_block_info(ir_node *node, struct obstack *obst)
{
122
	block_info_t *info = (block_info_t*)get_irn_link(node);
Matthias Braun's avatar
Matthias Braun committed
123
	if (info == NULL) {
124
		info = OALLOCZ(obst, block_info_t);
125
126
127
		set_irn_link(node, info);
	}
	return info;
128
}
129

Michael Beck's avatar
Michael Beck committed
130
/**
Michael Beck's avatar
Michael Beck committed
131
 * update the projection info for a Load/Store
Michael Beck's avatar
Michael Beck committed
132
 */
Matthias Braun's avatar
Matthias Braun committed
133
static changes_t update_projs(ldst_info_t *info, ir_node *proj)
Michael Beck's avatar
Michael Beck committed
134
{
135
136
	unsigned nr = get_Proj_num(proj);
	assert(nr <= MAX_PROJ);
Michael Beck's avatar
Michael Beck committed
137

Matthias Braun's avatar
Matthias Braun committed
138
	if (info->projs[nr] != NULL) {
139
140
141
		/* there is already one, do CSE */
		exchange(proj, info->projs[nr]);
		return DF_CHANGED;
Matthias Braun's avatar
Matthias Braun committed
142
	} else {
143
		info->projs[nr] = proj;
Matthias Braun's avatar
Matthias Braun committed
144
		return NO_CHANGES;
145
	}
146
}
Michael Beck's avatar
Michael Beck committed
147
148

/**
149
150
151
152
153
 * update the exception block info for a Load/Store node.
 *
 * @param info   the load/store info struct
 * @param block  the exception handler block for this load/store
 * @param pos    the control flow input of the block
Michael Beck's avatar
Michael Beck committed
154
 */
Matthias Braun's avatar
Matthias Braun committed
155
static void update_exc(ldst_info_t *info, ir_node *block, int pos)
Michael Beck's avatar
Michael Beck committed
156
{
Matthias Braun's avatar
Matthias Braun committed
157
	assert(info->exc_block == NULL);
158
159
	info->exc_block = block;
	info->exc_idx   = pos;
160
}
Michael Beck's avatar
Michael Beck committed
161
162

/**
163
 * walker, collects all Proj/Load/Store/Call/CopyB nodes
164
 *
165
 * walks from Start -> End
Michael Beck's avatar
Michael Beck committed
166
 */
Michael Beck's avatar
Michael Beck committed
167
static void collect_nodes(ir_node *node, void *env)
Michael Beck's avatar
Michael Beck committed
168
{
Matthias Braun's avatar
Matthias Braun committed
169
170
	walk_env_t *wenv   = (walk_env_t *)env;
	unsigned    opcode = get_irn_opcode(node);
171

172
	if (opcode == iro_Proj) {
Matthias Braun's avatar
Matthias Braun committed
173
		ir_node *pred = get_Proj_pred(node);
174
		opcode = get_irn_opcode(pred);
175

176
		if (opcode == iro_Load || opcode == iro_Store || opcode == iro_Call) {
Andreas Fried's avatar
Andreas Fried committed
177
			ldst_info_t *ldst_info = get_ldst_info(pred, &wenv->obst);
178
179
180
181

			wenv->changes |= update_projs(ldst_info, node);

			/*
182
183
184
185
186
			 * Place the Proj's to the same block as the
			 * predecessor Load. This is always ok and prevents
			 * "non-SSA" form after optimizations if the Proj
			 * is in a wrong block.
			 */
Andreas Fried's avatar
Andreas Fried committed
187
188
			ir_node *blk      = get_nodes_block(node);
			ir_node *pred_blk = get_nodes_block(pred);
189
190
191
192
193
			if (blk != pred_blk) {
				wenv->changes |= DF_CHANGED;
				set_nodes_block(node, pred_blk);
			}
		}
194
	} else if (opcode == iro_Block) {
Matthias Braun's avatar
Matthias Braun committed
195
196
		for (int i = get_Block_n_cfgpreds(node); i-- > 0; ) {
			bool     is_exc = false;
Andreas Fried's avatar
Andreas Fried committed
197
198
			ir_node *proj   = get_Block_cfgpred(node, i);
			ir_node *pred   = proj;
199
200
201
202
203

			/* ignore Bad predecessors, they will be removed later */
			if (is_Bad(pred))
				continue;

Matthias Braun's avatar
Matthias Braun committed
204
205
206
207
			if (is_Proj(proj)) {
				pred   = get_Proj_pred(proj);
				is_exc = is_x_except_Proj(proj);
			}
Andreas Fried's avatar
Andreas Fried committed
208
209
			ir_node      *pred_block = get_nodes_block(pred);
			block_info_t *bl_info    = get_block_info(pred_block, &wenv->obst);
210

211
			if (is_fragile_op(pred) && is_exc)
212
213
214
215
				bl_info->flags |= BLOCK_HAS_EXC;
			else if (is_irn_forking(pred))
				bl_info->flags |= BLOCK_HAS_COND;

216
			opcode = get_irn_opcode(pred);
Matthias Braun's avatar
Matthias Braun committed
217
218
			if (is_exc && (opcode == iro_Load || opcode == iro_Store
			               || opcode == iro_Call)) {
Andreas Fried's avatar
Andreas Fried committed
219
				ldst_info_t *ldst_info = get_ldst_info(pred, &wenv->obst);
220

Matthias Braun's avatar
Matthias Braun committed
221
				update_exc(ldst_info, node, i);
222
223
			}
		}
224
225
	} else if (is_memop(node)) {
		/* Just initialize a ldst_info */
226
		(void) get_ldst_info(node, &wenv->obst);
227
	}
228
}
Michael Beck's avatar
Michael Beck committed
229

230
/* forward */
231
static void reduce_node_usage(ir_node *ptr);
232
233

/**
Christoph Mallon's avatar
Christoph Mallon committed
234
 * Update a Load that may have lost its users.
235
 */
236
237
static void handle_load_update(ir_node *load)
{
238
239
240
	/* do NOT touch volatile loads for now */
	if (get_Load_volatility(load) == volatility_is_volatile)
		return;
241

Matthias Braun's avatar
Matthias Braun committed
242
243
	ldst_info_t *info = (ldst_info_t*)get_irn_link(load);
	if (!info->projs[pn_Load_res] && !info->projs[pn_Load_X_except]) {
244
245
		ir_node *ptr = get_Load_ptr(load);
		ir_node *mem = get_Load_mem(load);
246

Christoph Mallon's avatar
Christoph Mallon committed
247
		/* a Load whose value is neither used nor exception checked, remove it */
Andreas Zwinkau's avatar
Andreas Zwinkau committed
248
		exchange(info->projs[pn_Load_M], mem);
Matthias Braun's avatar
Matthias Braun committed
249
250
251
252
		if (info->projs[pn_Load_X_regular]) {
			ir_node *jmp = new_r_Jmp(get_nodes_block(load));
			exchange(info->projs[pn_Load_X_regular], jmp);
		}
253
		kill_node(load);
254
		reduce_node_usage(ptr);
255
	}
256
}
257
258

/**
259
 * A use of a node has vanished. Check if this was a Proj
260
261
 * node and update the counters.
 */
262
static void reduce_node_usage(ir_node *ptr)
263
{
264
265
266
267
	if (!is_Proj(ptr))
		return;
	if (get_irn_n_edges(ptr) > 0)
		return;
268

269
	/* this Proj is dead now */
Matthias Braun's avatar
Matthias Braun committed
270
	ir_node *pred = get_Proj_pred(ptr);
271
	if (is_Load(pred)) {
272
		ldst_info_t *info = (ldst_info_t*)get_irn_link(pred);
273
		info->projs[get_Proj_num(ptr)] = NULL;
274

275
276
		/* this node lost its result proj, handle that */
		handle_load_update(pred);
277
	}
278
}
279

280
281
282
283
/**
 * Kill a Load or Store and all other nodes which are not needed after
 * it has been killed.
 */
Matthias Braun's avatar
Matthias Braun committed
284
285
static void kill_and_reduce_usage(ir_node *node)
{
286
	ir_node *ptr;
Matthias Braun's avatar
Matthias Braun committed
287
	ir_node *value;
yb9976's avatar
yb9976 committed
288
	switch (get_irn_opcode(node)) {
289
	case iro_Load:
Matthias Braun's avatar
Matthias Braun committed
290
291
		ptr   = get_Load_ptr(node);
		value = NULL;
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
		break;
	case iro_Store:
		ptr   = get_Store_ptr(node);
		value = get_Store_value(node);
		break;
	default:
		panic("Cannot handle node %+F", node);
	}

	kill_node(node);
	reduce_node_usage(ptr);
	if (value != NULL) {
		reduce_node_usage(value);
	}
}

308
static void get_base_and_offset(ir_node *ptr, base_offset_t *base_offset)
309
{
310
311
312
313
	/* TODO: long might not be enough, we should probably use some tarval
	 * thingy, or at least detect long overflows and abort */
	long     offset = 0;
	ir_mode *mode   = get_irn_mode(ptr);
Michael Beck's avatar
Michael Beck committed
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
	for (;;) {
		if (is_Add(ptr)) {
			ir_node *l = get_Add_left(ptr);
			ir_node *r = get_Add_right(ptr);

			if (get_irn_mode(l) != mode || !is_Const(r))
				break;

			offset += get_tarval_long(get_Const_tarval(r));
			ptr     = l;
		} else if (is_Sub(ptr)) {
			ir_node *l = get_Sub_left(ptr);
			ir_node *r = get_Sub_right(ptr);

			if (get_irn_mode(l) != mode || !is_Const(r))
				break;

			offset -= get_tarval_long(get_Const_tarval(r));
			ptr     = l;
		} else if (is_Sel(ptr)) {
334
335
336
337
338
339
340
341
342
343
344
345
			ir_node *index = get_Sel_index(ptr);
			if (!is_Const(index))
				break;

			ir_type *type         = get_Sel_type(ptr);
			ir_type *element_type = get_array_element_type(type);
			if (get_type_state(element_type) != layout_fixed)
				break;

			/* TODO: may overflow here */
			int size = get_type_size_bytes(element_type);
			offset += size * get_tarval_long(get_Const_tarval(index));
Michael Beck's avatar
Michael Beck committed
346
			ptr = get_Sel_ptr(ptr);
347
348
349
350
351
352
353
		} else if (is_Member(ptr)) {
			ir_entity *entity = get_Member_entity(ptr);
			ir_type   *owner  = get_entity_owner(entity);
			if (get_type_state(owner) != layout_fixed)
				break;
			offset += get_entity_offset(entity);
			ptr = get_Member_ptr(ptr);
Michael Beck's avatar
Michael Beck committed
354
		} else
355
356
357
			break;
	}

358
359
	base_offset->offset = offset;
	base_offset->base   = ptr;
360
361
}

362
363
364
365
366
367
368
369
370
371
/**
 * This is called for load-after-load and load-after-store.
 * If possible the value of the previous load/store is transformed in a way
 * so the 2nd load can be left out/replaced by arithmetic on the previous
 * value.
 */
static ir_node *transform_previous_value(ir_mode *const load_mode,
	const base_offset_t *const load_bo, ir_mode *const prev_mode,
	const base_offset_t *const prev_bo, ir_node *const prev_value,
	ir_node *const block)
372
{
373
374
	if (load_bo->base != prev_bo->base)
		return NULL;
375

376
377
378
379
380
381
382
383
	/* ensure the load value is completely contained in the previous one */
	long delta = load_bo->offset - prev_bo->offset;
	if (delta < 0)
		return NULL;
	long load_mode_len = get_mode_size_bytes(load_mode);
	long prev_mode_len = get_mode_size_bytes(prev_mode);
	if (delta+load_mode_len > prev_mode_len)
		return NULL;
384

385
386
387
388
389
	/* simple case: previous value has the same mode */
	if (load_mode == prev_mode)
		return prev_value;

	/* two complement values can be transformed with bitops */
390
391
392
393
	ir_mode_arithmetic prev_arithmetic = get_mode_arithmetic(prev_mode);
	ir_mode_arithmetic load_arithmetic = get_mode_arithmetic(load_mode);
	if (prev_arithmetic == irma_twos_complement &&
		load_arithmetic == irma_twos_complement) {
394
395
396
397
398
399
		/* produce a shift to adjust offset delta */
		unsigned const shift = be_get_backend_param()->byte_order_big_endian
			? prev_mode_len - load_mode_len - delta
			: delta;
		ir_node *new_value = prev_value;
		if (shift != 0) {
400
			ir_graph *const irg   = get_irn_irg(block);
401
402
			ir_node  *const cnst  = new_r_Const_long(irg, mode_Iu, shift * 8);
			new_value = new_r_Shr(block, new_value, cnst, prev_mode);
403
		}
404
405

		return new_r_Conv(block, new_value, load_mode);
406
407
408
	} else if(prev_arithmetic != load_arithmetic
	          && load_mode_len == prev_mode_len) {
		return new_r_Bitcast(block, prev_value, load_mode);
409
410
	}

411
412
413
	/* we would need some kind of bitcast to handle non two complement values */
	return NULL;
}
Michael Beck's avatar
Michael Beck committed
414

415
416
static changes_t replace_load(ir_node *load, ir_node *new_value)
{
Matthias Braun's avatar
Matthias Braun committed
417
	const ldst_info_t *info = (ldst_info_t*)get_irn_link(load);
418
419
420
	if (info->projs[pn_Load_M])
		exchange(info->projs[pn_Load_M], get_Load_mem(load));

Matthias Braun's avatar
Matthias Braun committed
421
	changes_t res = NO_CHANGES;
422
	/* no exception */
423
	if (info->projs[pn_Load_X_except] != NULL) {
424
		ir_graph *irg = get_irn_irg(load);
Matthias Braun's avatar
Matthias Braun committed
425
426
		ir_node  *bad = new_r_Bad(irg, mode_X);
		exchange(info->projs[pn_Load_X_except], bad);
427
		res |= CF_CHANGED;
428
429

		assert(info->projs[pn_Load_X_regular] != NULL);
Matthias Braun's avatar
Matthias Braun committed
430
431
		ir_node *jmp = new_r_Jmp(get_nodes_block(load));
		exchange(info->projs[pn_Load_X_regular], jmp);
432
433
	}

434
435
436
	/* loads without user should already be optimized away */
	assert(info->projs[pn_Load_res] != NULL);
	exchange(info->projs[pn_Load_res], new_value);
437

438
	kill_and_reduce_usage(load);
439
440
441
	return res | DF_CHANGED;
}

442
443
444
445
446
/**
 * returns false if op cannot be reached through the X_regular proj of
 * @p prev_op or if there are no exceptions possible.
 */
static bool on_regular_path(ir_node *op, ir_node *prev_op)
447
{
448
449
450
451
452
	/* TODO: create a real test, for now we just make sure the previous node
	 * does not throw an exception. */
	(void)op;
	return !is_fragile_op(prev_op) || !ir_throws_exception(prev_op);
}
453

454
455
456
457
458
static changes_t try_load_after_store(track_load_env_t *env, ir_node *store)
{
	ir_node *const load = env->load;
	if (!on_regular_path(load, store))
		return NO_CHANGES;
459

460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
	ir_node      *store_ptr = get_Store_ptr(store);
	base_offset_t base_offset;
	get_base_and_offset(store_ptr, &base_offset);

	ir_mode *const load_mode   = get_Load_mode(load);
	ir_mode *const store_mode  = get_irn_mode(get_Store_value(store));
	ir_node *const store_value = get_Store_value(store);
	ir_node *const block       = get_nodes_block(load);

	/* load value completely contained in previsou store? */
	ir_node *const new_value
		= transform_previous_value(load_mode, &env->base_offset, store_mode,
		                           &base_offset, store_value, block);
	if (new_value == NULL)
		return NO_CHANGES;
475

476
477
478
	DBG_OPT_RAW(load, new_value);
	return replace_load(load, new_value);
}
479

480
481
482
483
484
static changes_t try_load_after_load(track_load_env_t *env, ir_node *prev_load)
{
	ir_node *const load = env->load;
	if (!on_regular_path(load, prev_load))
		return NO_CHANGES;
485

Matthias Braun's avatar
Matthias Braun committed
486
	const ldst_info_t *info = (ldst_info_t*)get_irn_link(prev_load);
487
488
489
490
	ir_node *const prev_value = info->projs[pn_Load_res];
	/* the other load is unused and will get removed later anyway */
	if (prev_value == NULL)
		return NO_CHANGES;
491

492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
	ir_node *const prev_ptr = get_Load_ptr(prev_load);
	base_offset_t base_offset;
	get_base_and_offset(prev_ptr, &base_offset);
	ir_mode *const load_mode = get_Load_mode(load);
	ir_mode *const prev_mode = get_Load_mode(prev_load);
	ir_node *const block     = get_nodes_block(load);

	/* load value completely contained in previous load? */
	ir_node *const new_value
		= transform_previous_value(load_mode, &env->base_offset, prev_mode,
		                           &base_offset, prev_value, block);
	if (new_value == NULL)
		return NO_CHANGES;

	DBG_OPT_RAR(prev_load, load);
	return replace_load(load, new_value);
}

static bool try_update_ptr_CopyB(track_load_env_t *env, ir_node *copyb)
{
	ir_node *copyb_dst = get_CopyB_dst(copyb);
	base_offset_t dst_base_offset;
	get_base_and_offset(copyb_dst, &dst_base_offset);
	if (dst_base_offset.base != env->base_offset.base)
		return false;

	/* see if bytes loaded are fully contained in the CopyB */
	if (env->base_offset.offset < dst_base_offset.offset)
		return false;

	ir_type *copyb_type = get_CopyB_type(copyb);
	long     n_copy     = get_type_size_bytes(copyb_type);
	ir_node *copyb_src  = get_CopyB_src(copyb);
	base_offset_t src_base_offset;
	get_base_and_offset(copyb_src, &src_base_offset);

	long     delta     = env->base_offset.offset - dst_base_offset.offset;
	ir_node *load      = env->load;
	ir_mode *load_mode = get_Load_mode(load);
	long     load_size = get_mode_size_bytes(load_mode);
	if (delta + load_size > n_copy)
		return false;

	/* track src input */
	env->base_offset.base   = src_base_offset.base;
	env->base_offset.offset = src_base_offset.offset + delta;
538
539
540
541
542

	/*
	 * Everything is OK, we can replace
	 *   ptr = (load_base_ptr + load_offset)
	 * with
543
	 *   new_load_ptr = (src_base_ptr + delta)
544
545
546
	 */
	ir_graph *irg          = get_irn_irg(load);
	ir_node  *block        = get_nodes_block(load);
547
	ir_mode  *mode_ref     = get_irn_mode(src_base_offset.base);
548
	ir_mode  *mode_ref_int = get_reference_mode_unsigned_eq(mode_ref);
549
550
551
552
553
	ir_node  *cnst         = new_r_Const_long(irg, mode_ref_int,
	                                          src_base_offset.offset + delta);
	ir_node  *new_load_ptr = new_r_Add(block, src_base_offset.base, cnst, mode_P);
	env->ptr = new_load_ptr;
	return true;
554
555
}

556
557
558
559
560
561
562
563
/* Note on terminology:
 * In the following functions, two types are associated with each Load
 * and Store: load_type/store_type are the types of the actual
 * operations (derived from the modes of the IR nodes), whereas
 * load_objt/store_objt are the types of the objects in memory which are
 * accessed by the Load/Store nodes.
 */

564
565
566
567
/**
 * Follow the memory chain as long as there are only Loads,
 * alias free Stores, and constant Calls and try to replace the
 * current Load by a previous ones.
Michael Beck's avatar
Michael Beck committed
568
569
570
571
572
573
 * Note that in unreachable loops it might happen that we reach
 * load again, as well as we can fall into a cycle.
 * We break such cycles using a special visited flag.
 *
 * INC_MASTER() must be called before dive into
 */
574
static changes_t follow_load_mem_chain(track_load_env_t *env, ir_node *start)
575
{
576
	ir_node *load      = env->load;
Matthias Braun's avatar
Matthias Braun committed
577
578
	ir_type *load_type = get_Load_type(load);
	unsigned load_size = get_mode_size_bytes(get_Load_mode(load));
579

580
581
582
583
	ir_node  *node = start;
	changes_t res  = NO_CHANGES;
	for (;;) {
		ldst_info_t *node_info = (ldst_info_t*)get_irn_link(node);
Michael Beck's avatar
Michael Beck committed
584

585
586
587
		if (is_Store(node)) {
			/* first try load-after-store */
			changes_t changes = try_load_after_store(env, node);
Matthias Braun's avatar
Matthias Braun committed
588
			if (changes != NO_CHANGES)
589
				return changes | res;
590

591
			/* check if we can pass through this store */
592
593
			const ir_node *ptr        = get_Store_ptr(node);
			const ir_node *value      = get_Store_value(node);
Matthias Braun's avatar
Matthias Braun committed
594
595
			const ir_type *store_type = get_Store_type(node);
			unsigned       store_size = get_mode_size_bytes(get_irn_mode(value));
596
			ir_alias_relation rel = get_alias_relation(
Matthias Braun's avatar
Matthias Braun committed
597
598
				ptr, store_type, store_size,
				env->ptr, load_type, load_size);
599
			/* if the might be an alias, we cannot pass this Store */
Michael Beck's avatar
Michael Beck committed
600
			if (rel != ir_no_alias)
601
				break;
602
603
604
605
606
607
608
609
610
			node = skip_Proj(get_Store_mem(node));
		} else if (is_Load(node)) {
			/* try load-after-load */
			changes_t changes = try_load_after_load(env, node);
			if (changes != NO_CHANGES)
				return changes | res;
			/* we can skip any load */
			node = skip_Proj(get_Load_mem(node));
		} else if (is_CopyB(node)) {
611
612
613
614
615
616
617
618
			/*
			 * We cannot replace the Load with another
			 * Load from the CopyB's source directly,
			 * because there may be Stores in between,
			 * destroying the source data. However, we can
			 * use the source address from this point
			 * onwards for further optimizations.
			 */
619
620
621
622
623
624
			bool updated = try_update_ptr_CopyB(env, node);
			if (updated) {
				/* Special case: If new_ptr points to
				 * a constant, we *can* replace the
				 * Load immediately.
				 */
625
				res |= NODES_CREATED;
Matthias Braun's avatar
Matthias Braun committed
626
				ir_mode *load_mode = get_Load_mode(load);
627
628
629
				ir_node *new_value = predict_load(env->ptr, load_mode);
				if (new_value != NULL)
					return replace_load(load, new_value) | res;
630
631
			}

632
			/* check aliasing with the CopyB */
Matthias Braun's avatar
Matthias Braun committed
633
634
635
			ir_node *dst  = get_CopyB_dst(node);
			ir_type *type = get_CopyB_type(node);
			unsigned size = get_type_size_bytes(type);
636
			ir_alias_relation rel = get_alias_relation(
Matthias Braun's avatar
Matthias Braun committed
637
638
				dst, type, size,
				env->ptr, load_type, load_size);
639
640
641
642
			/* possible alias => we cannot continue */
			if (rel != ir_no_alias)
				break;
			node = skip_Proj(get_CopyB_mem(node));
643
644
		} else if (is_irn_const_memory(node)) {
			node = skip_Proj(get_memop_mem(node));
645
		} else {
646
647
			/* be conservative about any other node and assume aliasing
			 * that changes the loaded value */
648
649
650
651
			break;
		}

		/* check for cycles */
652
		if (NODE_VISITED(node_info))
653
			break;
654
		MARK_NODE(node_info);
655
656
	}

657
	if (is_Sync(node)) {
658
		/* handle all Sync predecessors */
659
660
661
662
		foreach_irn_in(node, i, in) {
			ir_node *skipped = skip_Proj(in);
			res |= follow_load_mem_chain(env, skipped);
			if ((res & ~NODES_CREATED) != NO_CHANGES)
Matthias Braun's avatar
Matthias Braun committed
663
				break;
664
665
666
		}
	}
	return res;
667
}
Michael Beck's avatar
Michael Beck committed
668

669
670
ir_node *can_replace_load_by_const(const ir_node *load, ir_node *c)
{
671
672
673
674
	ir_mode  *c_mode = get_irn_mode(c);
	ir_mode  *l_mode = get_Load_mode(load);
	ir_node  *block  = get_nodes_block(load);
	dbg_info *dbgi   = get_irn_dbg_info(load);
675
	ir_node  *res    = duplicate_subgraph(dbgi, c, block);
676
677
678
679
680

	if (c_mode != l_mode) {
		/* check, if the mode matches OR can be easily converted info */
		if (is_reinterpret_cast(c_mode, l_mode)) {
			/* copy the value from the const code irg and cast it */
681
			res = new_rd_Conv(dbgi, block, res, l_mode);
Andreas Zwinkau's avatar
Andreas Zwinkau committed
682
683
		} else {
			return NULL;
684
685
686
		}
	}
	return res;
687
}
688

Michael Beck's avatar
Michael Beck committed
689
690
/**
 * optimize a Load
691
692
 *
 * @param load  the Load node
Michael Beck's avatar
Michael Beck committed
693
 */
Matthias Braun's avatar
Matthias Braun committed
694
static changes_t optimize_load(ir_node *load)
Michael Beck's avatar
Michael Beck committed
695
{
Matthias Braun's avatar
Matthias Braun committed
696
697
	const ldst_info_t *info = (ldst_info_t*)get_irn_link(load);
	changes_t          res  = NO_CHANGES;
698
699
700

	/* do NOT touch volatile loads for now */
	if (get_Load_volatility(load) == volatility_is_volatile)
Matthias Braun's avatar
Matthias Braun committed
701
		return NO_CHANGES;
702
703

	/* the address of the load to be optimized */
Andreas Fried's avatar
Andreas Fried committed
704
	ir_node *ptr = get_Load_ptr(load);
705
706

	/* The mem of the Load. Must still be returned after optimization. */
Andreas Fried's avatar
Andreas Fried committed
707
	ir_node *mem = get_Load_mem(load);
708

709
	if (info->projs[pn_Load_res] == NULL
710
711
	    && info->projs[pn_Load_X_except] == NULL) {
		assert(info->projs[pn_Load_X_regular] == NULL);
712
		/* the value is never used and we don't care about exceptions, remove */
713
		exchange(info->projs[pn_Load_M], mem);
714
		kill_and_reduce_usage(load);
715
716
717
		return res | DF_CHANGED;
	}

718
719
720
721
722
723
724
725
	track_load_env_t env;
	get_base_and_offset(ptr, &env.base_offset);
	env.ptr = ptr;

	/* Check, if the base address of this load is used more than once.
	 * If not, we won't find another store/load/CopyB anyway.
	 * TODO: can we miss values with multiple users in between? */
	if (get_irn_n_edges(ptr) <= 1 && get_irn_n_edges(env.base_offset.base) <= 1)
726
727
728
729
730
731
732
733
734
735
		return res;

	/*
	 * follow the memory chain as long as there are only Loads
	 * and try to replace current Load or Store by a previous one.
	 * Note that in unreachable loops it might happen that we reach
	 * load again, as well as we can fall into a cycle.
	 * We break such cycles using a special visited flag.
	 */
	INC_MASTER();
736
737
	env.load = load;
	res = follow_load_mem_chain(&env, skip_Proj(mem));
738
	return res;
739
}
Michael Beck's avatar
Michael Beck committed
740

741
742
743
/**
 * Check whether small is a part of large (starting at same address).
 */
Matthias Braun's avatar
Matthias Braun committed
744
static bool is_partially_same(ir_node *small, ir_node *large)
745
{
Matthias Braun's avatar
Matthias Braun committed
746
747
	const ir_mode *sm = get_irn_mode(small);
	const ir_mode *lm = get_irn_mode(large);
748
749
750
751
752

	/* FIXME: Check endianness */
	return is_Conv(small) && get_Conv_op(small) == large
	    && get_mode_size_bytes(sm) < get_mode_size_bytes(lm)
	    && get_mode_arithmetic(sm) == irma_twos_complement
753
	    && get_mode_arithmetic(lm) == irma_twos_complement;
754
}
755

Michael Beck's avatar
Michael Beck committed
756
/**
Matthias Braun's avatar
Matthias Braun committed
757
758
 * follow the memory chain as long as there are only Loads and alias free
 * Stores.
Michael Beck's avatar
Michael Beck committed
759
760
 * INC_MASTER() must be called before dive into
 */
Matthias Braun's avatar
Matthias Braun committed
761
static changes_t follow_store_mem_chain(ir_node *store, ir_node *start,
762
                                        bool had_split)
763
{
Matthias Braun's avatar
Matthias Braun committed
764
765
766
767
768
	changes_t    res   = NO_CHANGES;
	ldst_info_t *info  = (ldst_info_t*)get_irn_link(store);
	ir_node     *ptr   = get_Store_ptr(store);
	ir_node     *mem   = get_Store_mem(store);
	ir_node     *value = get_Store_value(store);
Matthias Braun's avatar
Matthias Braun committed
769
770
	ir_type     *type  = get_Store_type(store);
	unsigned     size  = get_mode_size_bytes(get_irn_mode(value));
Matthias Braun's avatar
Matthias Braun committed
771
772
	ir_node     *block = get_nodes_block(store);

Matthias Braun's avatar
Matthias Braun committed
773
774
775
	ir_node *node = start;
	while (node != store) {
		ldst_info_t *node_info = (ldst_info_t*)get_irn_link(node);
776
777
778
779

		/*
		 * BEWARE: one might think that checking the modes is useless, because
		 * if the pointers are identical, they refer to the same object.
Matthias Braun's avatar
Matthias Braun committed
780
781
782
783
784
		 * This is only true in strong typed languages, not is C were the
		 * following is possible *(ir_type1 *)p = a; *(ir_type2 *)p = b ...
		 * However, if the size of the mode that is written is bigger or equal
		 * the size of the old one, the old value is completely overwritten and
		 * can be killed ...
785
		 */
Matthias Braun's avatar
Matthias Braun committed
786
787
		if (is_Store(node) && !had_split && get_Store_ptr(node) == ptr &&
		    get_nodes_block(node) == block) {
788
			/*
Matthias Braun's avatar
Matthias Braun committed
789
			 * a Store after a Store in the same Block -- a write after write.
790
791
792
793
794
795
			 */

			/*
			 * We may remove the first Store, if the old value is completely
			 * overwritten or the old value is a part of the new value,
			 * and if it does not have an exception handler.
796
797
798
			 *
			 * TODO: What, if both have the same exception handler ???
			 */
Matthias Braun's avatar
Matthias Braun committed
799
800
801
			if (get_Store_volatility(node) != volatility_is_volatile
			    && !node_info->projs[pn_Store_X_except]) {
				ir_node *predvalue = get_Store_value(node);
802
				ir_mode *predmode  = get_irn_mode(predvalue);
Matthias Braun's avatar
Matthias Braun committed
803
				unsigned predsize  = get_mode_size_bytes(predmode);
804

Matthias Braun's avatar
Matthias Braun committed
805
				if (predsize <= size || is_partially_same(predvalue, value)) {
Matthias Braun's avatar
Matthias Braun committed
806
807
808
809
810
					DBG_OPT_WAW(node, store);
					DB((dbg, LEVEL_1, "  killing store %+F (override by %+F)\n",
					    node, store));
					exchange(node_info->projs[pn_Store_M], get_Store_mem(node));
					kill_and_reduce_usage(node);
811
812
813
814
815
816
817
818
819
820
821
822
					return DF_CHANGED;
				}
			}

			/*
			 * We may remove the Store, if the old value already contains
			 * the new value, and if it does not have an exception handler.
			 *
			 * TODO: What, if both have the same exception handler ???
			 */
			if (get_Store_volatility(store) != volatility_is_volatile
			        && !info->projs[pn_Store_X_except]) {
Matthias Braun's avatar
Matthias Braun committed
823
				ir_node *predvalue = get_Store_value(node);
824

825
				if (is_partially_same(value, predvalue)) {
Matthias Braun's avatar
Matthias Braun committed
826
827
828
					DBG_OPT_WAW(node, store);
					DB((dbg, LEVEL_1, "  killing store %+F (override by %+F)\n",
					    node, store));
829
					exchange(info->projs[pn_Store_M], mem);
830
					kill_and_reduce_usage(store);
831
832
					return DF_CHANGED;
				}
833
			}
Matthias Braun's avatar
Matthias Braun committed
834
835
		} else if (is_Load(node) && get_Load_ptr(node) == ptr &&
		           value == node_info->projs[pn_Load_res]) {
836
			/*
837
838
839
840
			 * a Store of a value just loaded from the same address
			 * -- a write after read.
			 * We may remove the Store, if it does not have an exception
			 * handler.
841
			 */
Matthias Braun's avatar
Matthias Braun committed
842
			if (!info->projs[pn_Store_X_except]) {
Matthias Braun's avatar
Matthias Braun committed
843
844
845
846
				DBG_OPT_WAR(store, node);
				DB((dbg, LEVEL_1,
				    "  killing store %+F (read %+F from same address)\n",
				    store, node));
847
				exchange(info->projs[pn_Store_M], mem);
848
				kill_and_reduce_usage(store);
849
850
851
852
				return DF_CHANGED;
			}
		}

Matthias Braun's avatar
Matthias Braun committed
853
		if (is_Store(node)) {
Michael Beck's avatar
Michael Beck committed
854
			/* check if we can pass through this store */
Matthias Braun's avatar
Matthias Braun committed
855
856
			ir_node *store_ptr   = get_Store_ptr(node);
			ir_node *store_value = get_Store_value(node);
Matthias Braun's avatar
Matthias Braun committed
857
858
			unsigned store_size  = get_mode_size_bytes(get_irn_mode(store_value));
			ir_type *store_type  = get_Store_type(node);
859
			ir_alias_relation rel = get_alias_relation(
Matthias Braun's avatar
Matthias Braun committed
860
861
				store_ptr, store_type, store_size,
				ptr, type, size);
862
			/* if the might be an alias, we cannot pass this Store */
Michael Beck's avatar
Michael Beck committed
863
			if (rel != ir_no_alias)
864
				break;
Matthias Braun's avatar
Matthias Braun committed
865
866
867
			node = skip_Proj(get_Store_mem(node));
		} else if (is_Load(node)) {
			ir_node *load_ptr  = get_Load_ptr(node);
Matthias Braun's avatar
Matthias Braun committed
868
869
			ir_type *load_type = get_Load_type(node);
			unsigned load_size = get_mode_size_bytes(get_Load_mode(node));
870
			ir_alias_relation rel = get_alias_relation(
Matthias Braun's avatar
Matthias Braun committed
871
872
				load_ptr, load_type, load_size,
				ptr, type, size);
Michael Beck's avatar
Michael Beck committed
873
			if (rel != ir_no_alias)
874
875
				break;

Matthias Braun's avatar
Matthias Braun committed
876
877
878
879
			node = skip_Proj(get_Load_mem(node));
		} else if (is_CopyB(node)) {
			ir_node *copyb_src  = get_CopyB_src(node);
			ir_type *copyb_type = get_CopyB_type(node);
Matthias Braun's avatar
Matthias Braun committed
880
			unsigned copyb_size = get_type_size_bytes(copyb_type);
881
			ir_alias_relation src_rel = get_alias_relation(
Matthias Braun's avatar
Matthias Braun committed
882
883
				copyb_src, copyb_type, copyb_size,
				ptr, type, size);
Matthias Braun's avatar
Matthias Braun committed
884
885
886
			if (src_rel != ir_no_alias)
				break;
			ir_node *copyb_dst = get_CopyB_dst(node);
887
			ir_alias_relation dst_rel = get_alias_relation(
Matthias Braun's avatar
Matthias Braun committed
888
889
				copyb_dst, copyb_type, copyb_size,
				ptr, type, size);
Matthias Braun's avatar
Matthias Braun committed
890
			if (dst_rel != ir_no_alias)
891
				break;
892
893
894
895
896
897
		} else {
			/* follow only Load chains */
			break;
		}

		/* check for cycles */
Matthias Braun's avatar
Matthias Braun committed
898
		if (NODE_VISITED(node_info))
899
			break;
Matthias Braun's avatar
Matthias Braun committed
900
		MARK_NODE(node_info);
901
902
	}

Matthias Braun's avatar
Matthias Braun committed
903
	if (is_Sync(node)) {
904
		/* handle all Sync predecessors */
Matthias Braun's avatar
Matthias Braun committed
905
906
907
908
		foreach_irn_in(node, i, in) {
			ir_node *skipped = skip_Proj(in);
			res |= follow_store_mem_chain(store, skipped, true);
			if (res != NO_CHANGES)
909
910
911
912
				break;
		}
	}
	return res;
913
}
Michael Beck's avatar
Michael Beck committed
914

915
916
static ir_entity *find_entity(ir_node *ptr)
{
917
	switch (get_irn_opcode(ptr)) {
918
919
920
921
922
	case iro_Address:
		return get_Address_entity(ptr);

	case iro_Offset:
		return get_Offset_entity(ptr);
923

924
925
	case iro_Member: {
		ir_node *pred = get_Member_ptr(ptr);
926
		if (get_irg_frame(get_irn_irg(ptr)) == pred)
927
			return get_Member_entity(ptr);
928
929
930
931
932
933
934
935

		return find_entity(pred);
	}
	case iro_Sub:
	case iro_Add: {
		ir_node *left = get_binop_left(ptr);
		if (mode_is_reference(get_irn_mode(left)))
			return find_entity(left);
Matthias Braun's avatar
Matthias Braun committed
936
		ir_node *right = get_binop_right(ptr);
937
938
939
940
941
942
943
944
945
		if (mode_is_reference(get_irn_mode(right)))
			return find_entity(right);
		return NULL;
	}
	default:
		return NULL;
	}
}

Michael Beck's avatar
Michael Beck committed
946
947
/**
 * optimize a Store
948
949
 *
 * @param store  the Store node
Michael Beck's avatar
Michael Beck committed
950
 */
Matthias Braun's avatar
Matthias Braun committed
951
static changes_t optimize_store(ir_node *store)
952
{
953
	if (get_Store_volatility(store) == volatility_is_volatile)
Matthias Braun's avatar
Matthias Braun committed
954
		return NO_CHANGES;
955

956
957
	/* Check, if the address of this Store is used more than once.
	 * If not, this Store cannot be removed in any case. */
Matthias Braun's avatar
Matthias Braun committed
958
	ir_node *ptr = get_Store_ptr(store);
959
	if (get_irn_n_edges(ptr) <= 1)
Matthias Braun's avatar
Matthias Braun committed
960
		return NO_CHANGES;
961

962
	ir_node *mem = get_Store_mem(store);
Michael Beck's avatar
Michael Beck committed
963

964
965
	/* follow the memory chain as long as there are only Loads */
	INC_MASTER();
Michael Beck's avatar
Michael Beck committed
966

967
	return follow_store_mem_chain(store, skip_Proj(mem), false);
968
}
Michael Beck's avatar
Michael Beck committed
969

970
971
972
973
/**
 * Checks whether @c ptr of type @c ptr_type lies completely within an
 * object of type @c struct_type starting at @c struct_ptr;
 */
Matthias Braun's avatar
Matthias Braun committed
974
975
static bool ptr_is_in_struct(ir_node *ptr, ir_type *ptr_type,
                             ir_node *struct_ptr, ir_type *struct_type)
976
{