ldst2.c 22.4 KB
Newer Older
1
/*
Michael Beck's avatar
Michael Beck committed
2
 * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 *
 * 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   parallelizing Load/Store optimisation
 * @author  Christoph Mallon
Michael Beck's avatar
Michael Beck committed
24
 * @version $Id: $
25
26
27
 */
#include "config.h"

28
29
#include "iroptimize.h"

30
#include "array_t.h"
31
32
33
34
35
36
37
38
39
40
#include "debug.h"
#include "ircons.h"
#include "irgraph.h"
#include "irgmod.h"
#include "irgopt.h"
#include "irgwalk.h"
#include "irmemory.h"
#include "irnode.h"
#include "irnodeset.h"
#include "obst.h"
41
#include "irdump.h"
Michael Beck's avatar
Michael Beck committed
42
#include "irflag_t.h"
Michael Beck's avatar
Michael Beck committed
43
#include "irprintf.h"
44

45
#if +0
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#define OPTIMISE_LOAD_AFTER_LOAD


#define UNIMPLEMENTED abort();


DEBUG_ONLY(static firm_dbg_module_t *dbg);


static struct obstack obst;
static size_t count_addrs;
static ir_node** addrs;


static void AddressCollector(ir_node* node, void* env)
{
	ir_nodeset_t* addrs_set = env;
	ir_node* addr;
	if (is_Load(node)) {
		addr = get_Load_ptr(node);
	} else if (is_Store(node)) {
		addr = get_Store_ptr(node);
	} else {
		return;
	}
	ir_nodeset_insert(addrs_set, addr);
}


/* Collects all unique addresses used by load and store nodes of a graph and
 * puts them into an array for later use */
static void CollectAddresses(ir_graph* irg)
{
	ir_nodeset_t addrs_set;

	ir_nodeset_init(&addrs_set);
	irg_walk_graph(irg, AddressCollector, NULL, &addrs_set);

	count_addrs = ir_nodeset_size(&addrs_set);
85
	DB((dbg, LEVEL_1, "===> %+F uses %u unique addresses\n", irg, (unsigned int)count_addrs));
86
87
88
89
90
91
92
93
94
	if (count_addrs != 0) {
		ir_nodeset_iterator_t addr_iter;
		size_t i;

		addrs = NEW_ARR_D(ir_node*, &obst, count_addrs);
		ir_nodeset_iterator_init(&addr_iter, &addrs_set);
		for (i = 0; i < count_addrs; i++) {
			ir_node* addr = ir_nodeset_iterator_next(&addr_iter);
			assert(addr != NULL);
95
			set_irn_link(addr, (void *)i);
96
97
98
99
100
101
102
103
104
105
106
			addrs[i] = addr;
			DB((dbg, LEVEL_2, "===> Collected unique symbolic address %+F\n", addr));
		}
	}
}


static void AliasSetAdder(ir_node* block, void* env)
{
	ir_nodeset_t* alias_set;
	size_t i;
Matthias Braun's avatar
Matthias Braun committed
107
	(void) env;
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137

	alias_set = NEW_ARR_D(ir_nodeset_t, &obst, count_addrs);
	for (i = 0; i < count_addrs; i++) {
		ir_nodeset_init(&alias_set[i]);
	}
	set_irn_link(block, alias_set);
}


static void SetStartAddressesTop(ir_graph* irg)
{
	ir_node* initial_mem;
	ir_node* start_block;
	ir_nodeset_t* start_addrs;
	size_t i;

	initial_mem = get_irg_initial_mem(irg);
	start_block = get_irg_start_block(irg);
	start_addrs = get_irn_link(start_block);
	for (i = 0; i < count_addrs; i++) {
		ir_nodeset_insert(&start_addrs[i], initial_mem);
	}
	mark_Block_block_visited(start_block);
}


static void AliasSetDestroyer(ir_node* block, void* env)
{
	ir_nodeset_t* alias_set = get_irn_link(block);
	size_t i;
Matthias Braun's avatar
Matthias Braun committed
138
	(void) env;
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157

	for (i = 0; i < count_addrs; i++) {
		ir_nodeset_destroy(&alias_set[i]);
	}
}


static ir_alias_relation AliasTest(ir_graph* irg, ir_node* addr, ir_mode* mode, ir_node* other)
{
	ir_node* other_addr;
	ir_mode* other_mode;

	if (is_Proj(other)) other = get_Proj_pred(other);

	if (is_Load(other)) {
		other_addr = get_Load_ptr(other);
	} else if (is_Store(other)) {
		other_addr = get_Store_ptr(other);
	} else {
Michael Beck's avatar
Michael Beck committed
158
		return ir_may_alias;
159
160
161
162
163
164
165
	}

	other_mode = get_irn_mode(other);
	return get_alias_relation(irg, addr, mode, other_addr, other_mode);
}


166
167
168
169
170
171
172
173
static int in_cmp(void const* va, void const* vb)
{
	ir_node const* const a = *(ir_node const*const*)va;
	ir_node const* const b = *(ir_node const*const*)vb;
	return get_irn_idx(a) - get_irn_idx(b);
}


174
175
176
177
static ir_node* GenerateSync(ir_graph* irg, ir_node* block, ir_nodeset_t* after_set)
{
	size_t set_size = ir_nodeset_size(after_set);
	ir_nodeset_iterator_t iter;
178

179
	assert(set_size != 0);
180

181
182
183
184
185
186
187
188
189
190
191
	ir_nodeset_iterator_init(&iter, after_set);
	if (set_size == 1) {
		return ir_nodeset_iterator_next(&iter);
	} else {
		ir_node** in;
		size_t i;

		NEW_ARR_A(ir_node*, in, set_size);
		for (i = 0; i < set_size; i++) {
			in[i] = ir_nodeset_iterator_next(&iter);
		}
192
		qsort(in, set_size, sizeof(*in), in_cmp);
193
194
195
196
197
198
199
200
201
		return new_r_Sync(irg, block, set_size, in);
	}
}


static ir_node** unfinished_phis;


static void PlaceMemPhis(ir_graph* irg, ir_node* block, ir_node* phi)
202
{
203
204
205
206
	int unfinished = 0;
	size_t block_n_preds = get_Block_n_cfgpreds(block);
	ir_nodeset_t* thissets;
	ir_node** in;
207
208
209
210
	size_t i;
	size_t j;

	thissets = get_irn_link(block);
211
	NEW_ARR_A(ir_node*, in, block_n_preds);
212
213
214
	for (j = 0; j < count_addrs; j++) {
		ir_node* new_phi;

215
216
217
218
		for (i = 0; i < block_n_preds; i++) {
			ir_node* pred_block = get_nodes_block(get_Phi_pred(phi, i)); // TODO get_Block_cfgpred_block(block, i);
			ir_nodeset_t* predsets = get_irn_link(pred_block);
			size_t predset_size = ir_nodeset_size(&predsets[j]);
219

220
221
222
			if (predset_size == 0) {
				in[i] = new_r_Unknown(irg, mode_M);
				unfinished = 1;
223
			} else {
224
				in[i] = GenerateSync(irg, pred_block, &predsets[j]);
225
226
			}
		}
227
228
229
230
231
		new_phi = new_r_Phi(irg, block, block_n_preds, in, mode_M);
		if (unfinished) {
			set_irn_link(new_phi, unfinished_phis[j]);
			unfinished_phis[j] = new_phi;
		}
232
233
		ir_nodeset_insert(&thissets[j], new_phi);
	}
234
235
236
237
238
239
240
241
242
243
244
245
246
247
}


static int WalkMem(ir_graph* irg, ir_node* node, ir_node* last_block);


static void WalkMemPhi(ir_graph* irg, ir_node* block, ir_node* phi)
{
	size_t n = get_Phi_n_preds(phi);
	size_t i;

	for (i = 0; i < n; i++) {
		WalkMem(irg, get_Phi_pred(phi, i), block);
	}
248

249
	PlaceMemPhis(irg, block, phi);
250
251
252
253
254
255
256
	exchange(phi, new_Bad());
}


static void PlaceLoad(ir_graph* irg, ir_node* block, ir_node* load, ir_node* memory)
{
	ir_node* addr = get_Load_ptr(load);
257
	size_t addr_idx = (size_t)get_irn_link(addr);
258
259
260
261
262
263
264
265
266
267
	ir_nodeset_t* interfere_sets = get_irn_link(block);
	ir_nodeset_t* interfere_set = &interfere_sets[addr_idx];
	size_t size = ir_nodeset_size(interfere_set);
	ir_nodeset_iterator_t interfere_iter;
	size_t i;

	assert(size > 0);
	ir_nodeset_iterator_init(&interfere_iter, interfere_set);
	if (size == 1) {
		ir_node* after = ir_nodeset_iterator_next(&interfere_iter);
268
		assert(!is_Proj(after) || !is_Load(get_Proj_pred(after)));
269
270
271
272
		DB((dbg, LEVEL_3, "===> %+F must be executed after %+F\n", load, after));
		set_Load_mem(load, after);
	} else {
		ir_node** after_set;
273
274
275
		ir_node* after;
		ir_node* mem;
		size_t i;
276
277

		NEW_ARR_A(ir_node*, after_set, size);
278
279
		i = 0;
		while ((mem = ir_nodeset_iterator_next(&interfere_iter)) != NULL) {
280
281
282
283
284
285
286
287
288
			if (is_Proj(mem)) {
				ir_node* pred = get_Proj_pred(mem);
				if (is_Load(pred)) {
#ifdef OPTIMISE_LOAD_AFTER_LOAD
					if (get_Load_ptr(pred) == addr && get_Load_mode(pred) == get_Load_mode(load)) {
						exchange(load, pred);
						return;
					}
#endif
289
					continue;
290
291
				}
			}
292
			DB((dbg, LEVEL_3, "===> %+F must be executed after %+F\n", load, mem));
293
			after_set[i++] = mem;
294
		}
295
296
297
298
299
300
301
		assert(i != 0);
		if (i == 1) {
			after = after_set[0];
		} else {
			after = new_r_Sync(irg, block, i, after_set);
		}
		set_Load_mem(load, after);
302
303
304
305
306
307
308
309
310
	}

	for (i = 0; i < count_addrs; i++) {
		ir_mode* mode = get_Load_mode(load);
		ir_node* other_addr = addrs[i];
		ir_mode* other_mode = mode; // XXX second mode is nonsense
		ir_alias_relation rel = get_alias_relation(irg, addr, mode, other_addr, other_mode);

		DB((dbg, LEVEL_3, "===> Testing for alias between %+F and %+F. Relation is %d\n", addr, other_addr, rel));
Michael Beck's avatar
Michael Beck committed
311
		if (rel == ir_no_alias) {
312
313
314
315
316
317
318
319
320
321
322
323
			continue;
		}
		DB((dbg, LEVEL_3, "===> %+F potentially aliases address %+F\n", load, other_addr));

		ir_nodeset_insert(&interfere_sets[i], memory);
	}
}


static void PlaceStore(ir_graph* irg, ir_node* block, ir_node* store, ir_node* memory)
{
	ir_node* addr = get_Store_ptr(store);
324
	size_t addr_idx = (size_t)get_irn_link(addr);
325
326
	ir_nodeset_t* interfere_sets = get_irn_link(block);
	ir_nodeset_t* interfere_set = &interfere_sets[addr_idx];
327
	ir_node* after;
328
329
	size_t i;

330
331
	after = GenerateSync(irg, block, interfere_set);
	set_Store_mem(store, after);
332
333

	for (i = 0; i < count_addrs; i++) {
334
		ir_nodeset_iterator_t interfere_iter;
335
336
337
338
339
340
341
		ir_mode* mode = get_irn_mode(get_Store_value(store));
		ir_node* other_addr = addrs[i];
		ir_mode* other_mode = mode; // XXX second mode is nonsense
		ir_alias_relation rel = get_alias_relation(irg, addr, mode, other_addr, other_mode);
		ir_node* other_node;

		DB((dbg, LEVEL_3, "===> Testing for alias between %+F and %+F. Relation is %d\n", addr, other_addr, rel));
Michael Beck's avatar
Michael Beck committed
342
		if (rel == ir_no_alias) {
343
344
345
346
347
348
			continue;
		}
		DB((dbg, LEVEL_3, "===> %+F potentially aliases address %+F\n", store, other_addr));

		ir_nodeset_iterator_init(&interfere_iter, &interfere_sets[i]);
		while ((other_node = ir_nodeset_iterator_next(&interfere_iter)) != NULL) {
Michael Beck's avatar
Michael Beck committed
349
			if (AliasTest(irg, addr, mode, other_node) != ir_no_alias) {
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
				DB((dbg, LEVEL_3, "===> Removing %+F from execute-after set of %+F due to %+F\n", other_node, addrs[i], store));
				ir_nodeset_remove_iterator(&interfere_sets[i], &interfere_iter);
			}
		}

		ir_nodeset_insert(&interfere_sets[i], memory);
	}
}


static int WalkMem(ir_graph* irg, ir_node* node, ir_node* last_block)
{
	int block_change = 0;
	ir_node* block = get_nodes_block(node);
	ir_node* pred;
	ir_node* memory = node;
	ir_nodeset_t* addr_sets;

	if (block != last_block) {
369
		DB((dbg, LEVEL_3, "===> Changing block from %+F to %+F\n", last_block, block));
370
		block_change = 1;
371
		if (!Block_block_visited(block)) {
372
373
374
375
376
377
378
379
380
381
382
383
			mark_Block_block_visited(block);
		} else {
			DB((dbg, LEVEL_2, "===> Hit already visited block at %+F\n", node));
			return block_change;
		}
	}

	// Skip projs
	if (is_Proj(node)) node = get_Proj_pred(node);

	if (is_Phi(node)) {
		WalkMemPhi(irg, block, node);
384
		return block_change;
385
386
387
388
389
390
391
392
393
394
	} else if (is_Sync(node)) {
		UNIMPLEMENTED
	} else if (is_Return(node)) {
		pred = get_Return_mem(node);
	} else {
		pred = get_fragile_op_mem(node);
	}

	if (WalkMem(irg, pred, block)) {
		// There was a block change
395
		size_t block_arity = get_Block_n_cfgpreds(block);
396
397

		DB((dbg, LEVEL_3, "===> There is a block change before %+F\n", node));
398
		if (block_arity == 1) {
399
			// Just one predecessor, inherit its alias sets
400
401
			ir_node* pred_block = get_nodes_block(pred);
			ir_nodeset_t* predsets = get_irn_link(pred_block);
402
403
404
405
406
			ir_nodeset_t* thissets = get_irn_link(block);
			size_t i;

			DB((dbg, LEVEL_3, "===> Copying the only predecessor's address sets\n"));

407
408
409
410
			if (ir_nodeset_size(&predsets[0]) == 0) {
				ir_node* unknown;

				DB((dbg, LEVEL_3, "===> The predecessor was not finished yet\n"));
411
				assert(Block_block_visited(pred_block));
412

413
414
415
				unknown = new_r_Unknown(irg, mode_M);
				for (i = 0; i < count_addrs; i++) {
					ir_node* phi_unk = new_r_Phi(irg, block, 1, &unknown, mode_M);
416
					DB((dbg, LEVEL_3, "===> Placing unfinished %+F for %+F in %+F\n", phi_unk, addrs[i], block));
417
418
419
420
421
422
423
424
425
426
427
428
429
					set_irn_link(phi_unk, unfinished_phis[i]);
					unfinished_phis[i] = phi_unk;
					ir_nodeset_insert(&thissets[i], phi_unk);
				}
			} else {
				for (i = 0; i < count_addrs; i++) {
					ir_nodeset_iterator_t prediter;
					ir_node* addr;

					ir_nodeset_iterator_init(&prediter, &predsets[i]);
					while ((addr = ir_nodeset_iterator_next(&prediter)) != NULL) {
						ir_nodeset_insert(&thissets[i], addr);
					}
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
				}
			}
		}
	}

	DB((dbg, LEVEL_3, "===> Detotalising %+F\n", node));

	addr_sets = get_irn_link(block);

	if (is_Load(node)) {
		PlaceLoad(irg, block, node, memory);
	} else if (is_Store(node)) {
		PlaceStore(irg, block, node, memory);
	} else {
		ir_nodeset_t sync_set;
		size_t i;
446
		ir_node* after;
447
448
449
450
451
452
453
454
455
456
457
458
459
460

		DB((dbg, LEVEL_3, "===> Fallback: %+F aliases everything\n", node));

		ir_nodeset_init(&sync_set);
		for (i = 0; i < count_addrs; i++) {
			ir_nodeset_iterator_t iter;
			ir_node* mem;

			ir_nodeset_iterator_init(&iter, &addr_sets[i]);
			while ((mem = ir_nodeset_iterator_next(&iter)) != NULL) {
				ir_nodeset_insert(&sync_set, mem);
			}
		}

461
		after = GenerateSync(irg, block, &sync_set);
462
		set_irn_n(node, 0, after); // XXX unnice way to set the memory input
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477

		for (i = 0; i < count_addrs; i++) {
			ir_nodeset_iterator_t iter;
			ir_nodeset_iterator_init(&iter, &addr_sets[i]);
			while (ir_nodeset_iterator_next(&iter) != NULL) {
				ir_nodeset_remove_iterator(&addr_sets[i], &iter);
			}
			ir_nodeset_insert(&addr_sets[i], memory);
		}
	}

	return block_change;
}


478
479
480
481
482
483
484
485
486
487
488
489
490
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
static void FinalisePhis(ir_graph* irg)
{
	size_t i;

	for (i = 0; i < count_addrs; i++) {
		ir_node* next_phi;
		ir_node* phi;

		for (phi = unfinished_phis[i]; phi != NULL; phi = next_phi) {
			ir_node* block = get_nodes_block(phi);
			size_t block_n_preds = get_Block_n_cfgpreds(block);

			next_phi = get_irn_link(phi);

			DB((dbg, LEVEL_4, "===> Finialising phi %+F in %+F\n", phi, block));

			if (block_n_preds == 1) {
				ir_node* pred_block = get_Block_cfgpred_block(block, 0);
				ir_nodeset_t* pred_sets = get_irn_link(pred_block);
				ir_node* after = GenerateSync(irg, pred_block, &pred_sets[i]);

				assert(is_Unknown(get_Phi_pred(phi, 0)));
				exchange(phi, after);
			} else {
				ir_node** in;
				size_t j;

				NEW_ARR_A(ir_node*, in, block_n_preds);
				for (j = 0; j < block_n_preds; j++) {
					ir_node* pred_block = get_Block_cfgpred_block(block, j);
					ir_nodeset_t* pred_sets = get_irn_link(pred_block);

					if (is_Unknown(get_Phi_pred(phi, j))) {
						set_Phi_pred(phi, j, GenerateSync(irg, pred_block, &pred_sets[i]));
					}
				}
			}
		}
	}
}


520
521
522
523
524
525
static void Detotalise(ir_graph* irg)
{
	ir_node* end_block = get_irg_end_block(irg);
	size_t npreds = get_Block_n_cfgpreds(end_block);
	size_t i;

526
	unfinished_phis = XMALLOCN(ir_node, count_addrs);
527
528
529
530
	for (i = 0; i < count_addrs; i++) {
		unfinished_phis[i] = NULL;
	}

531
532
533
534
535
536
	for (i = 0; i < npreds; i++) {
		ir_node* pred = get_Block_cfgpred(end_block, i);
		assert(is_Return(pred));
		DB((dbg, LEVEL_2, "===> Starting memory walk at %+F\n", pred));
		WalkMem(irg, pred, NULL);
	}
537
538
539

	FinalisePhis(irg);
	xfree(unfinished_phis);
540
}
541
#endif
542
543


Matthias Braun's avatar
Matthias Braun committed
544
#if 0
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
static void AddSyncPreds(ir_nodeset_t* preds, ir_node* sync)
{
	size_t n = get_Sync_n_preds(sync);
	size_t i;

	for (i = 0; i < n; i++) {
		ir_node* pred = get_Sync_pred(sync, i);
		if (is_Sync(pred)) {
			AddSyncPreds(preds, pred);
		} else {
			ir_nodeset_insert(preds, pred);
		}
	}
}

static void NormaliseSync(ir_node* node, void* env)
{
	ir_nodeset_t preds;
	ir_nodeset_iterator_t iter;
	ir_node** in;
	size_t count_preds;
	size_t i;
Matthias Braun's avatar
Matthias Braun committed
567
	(void) env;
568
569
570
571
572
573
574

	if (!is_Sync(node)) return;

	ir_nodeset_init(&preds);
	AddSyncPreds(&preds, node);

	count_preds = ir_nodeset_size(&preds);
Michael Beck's avatar
Michael Beck committed
575
	if (count_preds != (unsigned)get_Sync_n_preds(node)) {
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
		NEW_ARR_A(ir_node*, in, count_preds);
		ir_nodeset_iterator_init(&iter, &preds);
		for (i = 0; i < count_preds; i++) {
			ir_node* pred = ir_nodeset_iterator_next(&iter);
			assert(pred != NULL);
			in[i] = pred;
		}
		set_irn_in(node, count_preds, in);
	}

	ir_nodeset_destroy(&preds);
}

void opt_ldst2(ir_graph* irg)
{
	FIRM_DBG_REGISTER(dbg, "firm.opt.ldst2");
	DB((dbg, LEVEL_1, "===> Performing load/store optimisation on %+F\n", irg));

	normalize_one_return(irg);
595
	dump_ir_block_graph(irg, "-prefluffig");
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611

	obstack_init(&obst);

	if (1 /* XXX */ || get_opt_alias_analysis()) {
		assure_irg_address_taken_computed(irg);
		assure_irp_globals_address_taken_computed();
	}


	CollectAddresses(irg);
	if (count_addrs == 0) return;

	irg_block_walk_graph(irg, AliasSetAdder, NULL, NULL);
	inc_irg_block_visited(irg);
	SetStartAddressesTop(irg);
	Detotalise(irg);
612
613
	dump_ir_block_graph(irg, "-fluffig");

614
615
616
	irg_block_walk_graph(irg, AliasSetDestroyer, NULL, NULL);
	obstack_free(&obst, NULL);

617
	normalize_proj_nodes(irg);
618
	irg_walk_graph(irg, NormaliseSync, NULL, NULL);
619
  optimize_graph_df(irg);
620
	irg_walk_graph(irg, NormaliseSync, NULL, NULL);
621
	dump_ir_block_graph(irg, "-postfluffig");
622
}
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
#endif


typedef struct parallelise_info
{
	ir_node      *origin_block;
	ir_node      *origin_ptr;
	ir_mode      *origin_mode;
	ir_nodeset_t  this_mem;
	ir_nodeset_t  user_mem;
} parallelise_info;


static void parallelise_load(parallelise_info *pi, ir_node *irn)
{
638
639
640
641
	/* There is no point in investigating the same subgraph twice */
	if (ir_nodeset_contains(&pi->user_mem, irn))
		return;

642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
	//ir_fprintf(stderr, "considering %+F\n", irn);
	if (get_nodes_block(irn) == pi->origin_block) {
		if (is_Proj(irn)) {
			ir_node *pred = get_Proj_pred(irn);
			if (is_Load(pred) &&
					get_Load_volatility(pred) == volatility_non_volatile) {
				ir_node *mem = get_Load_mem(pred);
				//ir_nodeset_insert(&pi->this_mem, mem);
				ir_nodeset_insert(&pi->user_mem, irn);
				//ir_fprintf(stderr, "adding %+F to user set\n", irn);
				parallelise_load(pi, mem);
				return;
			} else if (is_Store(pred) &&
					get_Store_volatility(pred) == volatility_non_volatile) {
				ir_mode *org_mode   = pi->origin_mode;
				ir_node *org_ptr    = pi->origin_ptr;
				ir_mode *store_mode = get_irn_mode(get_Store_value(pred));
				ir_node *store_ptr  = get_Store_ptr(pred);
Michael Beck's avatar
Michael Beck committed
660
				if (get_alias_relation(current_ir_graph, org_ptr, org_mode, store_ptr, store_mode) == ir_no_alias) {
661
					ir_node *mem = get_Store_mem(pred);
Matthias Braun's avatar
Matthias Braun committed
662
					//ir_fprintf(stderr, "Ld after St: %+F (%+F) does not alias %+F (%+F)\n", org_ptr, org_mode, store_ptr, store_mode);
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
					ir_nodeset_insert(&pi->user_mem, irn);
					//ir_fprintf(stderr, "adding %+F to user set\n", irn);
					parallelise_load(pi, mem);
					return;
				}
			}
		} else if (is_Sync(irn)) {
			int n = get_Sync_n_preds(irn);
			int i;

			for (i = 0; i < n; ++i) {
				ir_node *sync_pred = get_Sync_pred(irn, i);
				parallelise_load(pi, sync_pred);
			}
			return;
		}
	}
	ir_nodeset_insert(&pi->this_mem, irn);
	//ir_fprintf(stderr, "adding %+F to this set\n", irn);
}


static void parallelise_store(parallelise_info *pi, ir_node *irn)
{
687
688
689
690
	/* There is no point in investigating the same subgraph twice */
	if (ir_nodeset_contains(&pi->user_mem, irn))
		return;

691
692
693
694
695
696
697
698
699
700
	//ir_fprintf(stderr, "considering %+F\n", irn);
	if (get_nodes_block(irn) == pi->origin_block) {
		if (is_Proj(irn)) {
			ir_node *pred = get_Proj_pred(irn);
			if (is_Load(pred) &&
					get_Load_volatility(pred) == volatility_non_volatile) {
				ir_mode *org_mode  = pi->origin_mode;
				ir_node *org_ptr   = pi->origin_ptr;
				ir_mode *load_mode = get_Load_mode(pred);
				ir_node *load_ptr  = get_Load_ptr(pred);
Michael Beck's avatar
Michael Beck committed
701
				if (get_alias_relation(current_ir_graph, org_ptr, org_mode, load_ptr, load_mode) == ir_no_alias) {
702
					ir_node *mem = get_Load_mem(pred);
Matthias Braun's avatar
Matthias Braun committed
703
					//ir_fprintf(stderr, "St after Ld: %+F (%+F) does not alias %+F (%+F)\n", org_ptr, org_mode, load_ptr, load_mode);
704
705
706
707
708
709
710
711
712
713
714
					ir_nodeset_insert(&pi->user_mem, irn);
					//ir_fprintf(stderr, "adding %+F to user set\n", irn);
					parallelise_store(pi, mem);
					return;
				}
			} else if (is_Store(pred) &&
					get_Store_volatility(pred) == volatility_non_volatile) {
				ir_mode *org_mode   = pi->origin_mode;
				ir_node *org_ptr    = pi->origin_ptr;
				ir_mode *store_mode = get_irn_mode(get_Store_value(pred));
				ir_node *store_ptr  = get_Store_ptr(pred);
Michael Beck's avatar
Michael Beck committed
715
				if (get_alias_relation(current_ir_graph, org_ptr, org_mode, store_ptr, store_mode) == ir_no_alias) {
Michael Beck's avatar
Michael Beck committed
716
717
					ir_node *mem;

Matthias Braun's avatar
Matthias Braun committed
718
					//ir_fprintf(stderr, "St after St: %+F (%+F) does not alias %+F (%+F)\n", org_ptr, org_mode, store_ptr, store_mode);
719
720
					ir_nodeset_insert(&pi->user_mem, irn);
					//ir_fprintf(stderr, "adding %+F to user set\n", irn);
Michael Beck's avatar
Michael Beck committed
721
					mem = get_Store_mem(pred);
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
					parallelise_store(pi, mem);
					return;
				}
			}
		} else if (is_Sync(irn)) {
			int n = get_Sync_n_preds(irn);
			int i;

			for (i = 0; i < n; ++i) {
				ir_node *sync_pred = get_Sync_pred(irn, i);
				parallelise_store(pi, sync_pred);
			}
			return;
		}
	}
	ir_nodeset_insert(&pi->this_mem, irn);
	//ir_fprintf(stderr, "adding %+F to this set\n", irn);
}


static void walker(ir_node *proj, void *env)
{
	ir_node          *mem_op;
	ir_node          *pred;
	ir_node          *block;
	int               n;
	parallelise_info  pi;

	(void)env;

	if (!is_Proj(proj)) return;
	if (get_irn_mode(proj) != mode_M) return;

	mem_op = get_Proj_pred(proj);
	if (is_Load(mem_op)) {
		if (get_Load_volatility(mem_op) != volatility_non_volatile) return;

		block = get_nodes_block(mem_op);
		pred  = get_Load_mem(mem_op);
		//ir_fprintf(stderr, "starting parallelise at %+F for %+F\n", pred, proj);

		pi.origin_block = block,
		pi.origin_ptr   = get_Load_ptr(mem_op);
		pi.origin_mode  = get_Load_mode(mem_op);
		ir_nodeset_init(&pi.this_mem);
		ir_nodeset_init(&pi.user_mem);

		parallelise_load(&pi, pred);
	} else if (is_Store(mem_op)) {
		if (get_Store_volatility(mem_op) != volatility_non_volatile) return;

		block = get_nodes_block(mem_op);
		pred  = get_Store_mem(mem_op);
		//ir_fprintf(stderr, "starting parallelise at %+F for %+F\n", pred, proj);

		pi.origin_block = block,
		pi.origin_ptr   = get_Store_ptr(mem_op);
		pi.origin_mode  = get_irn_mode(get_Store_value(mem_op));
		ir_nodeset_init(&pi.this_mem);
		ir_nodeset_init(&pi.user_mem);

		parallelise_store(&pi, pred);
	} else {
		return;
	}

	n = ir_nodeset_size(&pi.user_mem);
Michael Beck's avatar
typo    
Michael Beck committed
789
	if (n != 0) { /* nothing happened otherwise */
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
		ir_graph               *irg  = current_ir_graph;
		ir_node                *sync;
		ir_node               **in;
		ir_nodeset_iterator_t   iter;
		int                     i;

		++n;
		//ir_fprintf(stderr, "creating sync for users of %+F with %d inputs\n", proj, n);
		NEW_ARR_A(ir_node*, in, n);
		i = 0;
		in[i++] = new_r_Unknown(irg, mode_M);
		ir_nodeset_iterator_init(&iter, &pi.user_mem);
		for (;;) {
			ir_node* p = ir_nodeset_iterator_next(&iter);
			if (p == NULL) break;
			in[i++] = p;
		}
		assert(i == n);
		sync = new_r_Sync(irg, block, n, in);
		exchange(proj, sync);

		assert(pn_Load_M == pn_Store_M);
		proj = new_r_Proj(irg, block, mem_op, mode_M, pn_Load_M);
		set_Sync_pred(sync, 0, proj);

		n = ir_nodeset_size(&pi.this_mem);
		//ir_fprintf(stderr, "creating sync for %+F with %d inputs\n", mem_op, n);
		ir_nodeset_iterator_init(&iter, &pi.this_mem);
		if (n == 1) {
			sync = ir_nodeset_iterator_next(&iter);
		} else {
			NEW_ARR_A(ir_node*, in, n);
			i = 0;
			for (;;) {
				ir_node* p = ir_nodeset_iterator_next(&iter);
				if (p == NULL) break;
				in[i++] = p;
			}
			assert(i == n);
			sync = new_r_Sync(irg, block, n, in);
		}
		set_memop_mem(mem_op, sync);
	}

	ir_nodeset_destroy(&pi.this_mem);
	ir_nodeset_destroy(&pi.user_mem);
}


839
void opt_sync(ir_graph *irg)
840
{
841
842
	//assure_irg_entity_usage_computed(irg);
	//assure_irp_globals_entity_usage_computed();
843
844

	irg_walk_graph(irg, NULL, walker, NULL);
845
	//optimize_graph_df(irg);
846
847
	//irg_walk_graph(irg, NormaliseSync, NULL, NULL);
}