beschedrss.c 60.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
 * Implementation of a register saturating list scheduler
 * as described in: Sid-Ahmed-Ali Touati
 * Register Saturation in Superscalar and VLIW Codes
 *
 * @license This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
 * @author  Christian Wuerdig
 * @date    29.08.2006
 * @cvs-id  $Id$
 */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <limits.h>

#include "obst.h"
#include "debug.h"

#include "irgraph_t.h"
#include "irnode_t.h"
#include "iredges_t.h"
#include "ircons_t.h"
#include "irphase_t.h"
#include "irgwalk.h"
#include "irtools.h"
#include "irbitset.h"
#include "irprintf.h"
#include "bipartite.h"
#include "hungarian.h"
#include "plist.h"

#include "height.h"

#include "beabi.h"
#include "benode_t.h"
#include "besched_t.h"

Christian Würdig's avatar
Christian Würdig committed
39
#define ARR_LEN_SAFE(arr) ((arr) != NULL ? ARR_LEN((arr)) : 0)
40
41

#define HASH_RSS_EDGE(edge) ((get_irn_node_nr((edge)->src) << 16) | (get_irn_node_nr((edge)->tgt) & 0xFFFF))
42
#define BSEARCH_IRN_ARR(val, arr) \
Christian Würdig's avatar
Christian Würdig committed
43
	bsearch(&(val), (arr), ARR_LEN_SAFE((arr)), sizeof((arr)[0]), cmp_irn_idx)
44

Christian Würdig's avatar
Christian Würdig committed
45
#define BLOCK_IDX_MAP(rss, irn) bsearch_for_index(get_irn_idx((irn)), (rss)->idx_map, ARR_LEN_SAFE((rss)->idx_map), 1)
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
85
86
87
88
89
90
91
92

/* Represents a child with associated costs */
typedef struct _child {
	ir_node *irn;
	float   cost;
} child_t;

/* We need edges for several purposes. */
typedef struct _rss_edge {
	ir_node *src;
	ir_node *tgt;
	void    *next;
} rss_edge_t;

/* Represents a connected bipartite component. */
typedef struct _cbc {
	nodeset *parents;       /**< = S  a set of value producers */
	nodeset *children;      /**< = T  a set of value consumers */
	pset    *kill_edges;    /**< = E  a set of edges (t in T, s in S) such as each s in S gets killed by at least one t in T */
	int     nr;             /**< a deterministic index for set insertion (used as hash) */
} cbc_t;

/* Represents a disjoint value DAG. */
typedef struct _dvg {
	nodeset *nodes;
	pset    *edges;
} dvg_t;

/* Represents a chain of nodes. */
typedef struct _chain {
	plist_t *elements;   /**< List of chain elements */
	int     nr;          /**< a deterministic index for set insertion (used as hash) */
} chain_t;

typedef struct _rss_irn {
	plist_t  *consumer_list;    /**< List of consumers */
	ir_node **consumer;         /**< Sorted consumer array (needed for faster access) */

	plist_t  *parent_list;      /**< List of parents */
	ir_node **parents;          /**< Sorted parent array (needed for faster access) */

	plist_t  *descendant_list;  /**< List of descendants */
	ir_node **descendants;      /**< Sorted descendant array (needed for faster access) */

	plist_t  *pkiller_list;     /**< List of potential killers */
	ir_node **pkillers;         /**< Sorted pkiller array (needed for faster access) */

Christian Würdig's avatar
Christian Würdig committed
93
#if 0
94
95
96
	plist_t  *dvg_desc_list;    /**< List of all descendants in the DVG */
	ir_node **dvg_desc;         /**< Sorted dvg descendant array (needed for faster access) */

97
98
99
	plist_t  *dvg_pkiller_list; /**< List of potential killers in the DVG */
	ir_node **dvg_pkiller;      /**< Sorted dvg pkiller array (needed for faster access) */

100
	plist_t  *dvg_user_list;    /**< List of users in the disjoint value DAG DVG */
Christian Würdig's avatar
Christian Würdig committed
101
102
#endif
	plist_t  *kill_value_list;  /**< List of values getting potentially killed by this node */
103
104
105
106
107
108
109
110

	ir_node  *killer;           /**< The selected unique killer */
	ir_node  *irn;              /**< The corresponding firm node to this rss_irn */

	chain_t  *chain;            /**< The chain, this node is associated to */

	unsigned live_out : 1;      /**< irn has consumers outside of it's block */
	unsigned visited  : 1;      /**< visited flag for bipartite decomposition */
111
112
	unsigned havedesc : 1;      /**< visited flag collect descendants */
	unsigned havecons : 1;      /**< visited flag collect consumer */
113
114
115
116
	unsigned handled  : 1;      /**< flag indicating whether or not the list structures have been build */
	unsigned dumped   : 1;      /**< flag indication whether or not this node was dumped */
} rss_irn_t;

Christian Würdig's avatar
Christian Würdig committed
117
118
119
120
121
122
123
124
125
/* Represents a serialization edge with associated costs. */
typedef struct _serialization {
	rss_irn_t  *u;       /* the top node */
	rss_irn_t  *v;       /* the node about to be serialized after u */
	rss_edge_t *edge;    /* the edge selected for the serialization */
	int        omega1;   /* estimated: available regs - RS reduction */
	int        omega2;   /* increase in critical path length */
} serialization_t;

126
127
128
129
130
131
132
133
134
typedef struct _rss {
	phase_t          ph;              /**< Phase to hold some data */
	heights_t        *h;              /**< The current height object */
	ir_graph         *irg;            /**< The irg to preprocess */
	plist_t          *nodes;          /**< The list of interesting nodes */
	const arch_env_t *arch_env;       /**< The architecture environment */
	be_abi_irg_t     *abi;            /**< The abi for this irg */
	pset             *cbc_set;        /**< A set of connected bipartite components */
	ir_node          *block;          /**< The current block in progress. */
135
136
	int              *idx_map;        /**< Mapping irn indices to per block indices */
	unsigned         max_height;      /**< maximum height in the current block */
137
138
139
140
141
142
143
144
145
146
147
	const arch_register_class_t *cls; /**< The current register class */
	DEBUG_ONLY(firm_dbg_module_t *dbg);
} rss_t;

#define get_rss_irn(rss, irn)  (phase_get_or_set_irn_data(&rss->ph, irn))

/**
 * We need some special nodes:
 * a source and a sink for all live-in and live-out values of a block
 */

148
enum {
149
150
151
152
153
154
155
156
157
158
159
	iro_rss_Source,
	iro_rss_Sink,
	iro_rss_last
};

static ir_node *_source = NULL;
static ir_node *_sink   = NULL;

#define is_Source(irn) ((irn) == _source)
#define is_Sink(irn)   ((irn) == _sink)

Christian Würdig's avatar
Christian Würdig committed
160
161
162
163
164
165
166
167
168
169
170
/******************************************************************************
 *  _          _                    __                  _   _
 * | |        | |                  / _|                | | (_)
 * | |__   ___| |_ __   ___ _ __  | |_ _   _ _ __   ___| |_ _  ___  _ __  ___
 * | '_ \ / _ \ | '_ \ / _ \ '__| |  _| | | | '_ \ / __| __| |/ _ \| '_ \/ __|
 * | | | |  __/ | |_) |  __/ |    | | | |_| | | | | (__| |_| | (_) | | | \__ \
 * |_| |_|\___|_| .__/ \___|_|    |_|  \__,_|_| |_|\___|\__|_|\___/|_| |_|___/
 *            | |
 *            |_|
 ******************************************************************************/

171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/**
 * Acquire opcodes and create source and sink nodes.
 */
static void init_rss_special_nodes(ir_graph *irg) {
	ir_node *block         = get_irg_start_block(irg);
	int     iro_rss_base   = get_next_ir_opcodes(iro_rss_last);
	ir_op   *op_rss_Source = new_ir_op(iro_rss_base + iro_rss_Source, "rss_Source", op_pin_state_pinned, irop_flag_none, oparity_zero, 0, 0, NULL);
	ir_op   *op_rss_Sink   = new_ir_op(iro_rss_base + iro_rss_Sink,   "rss_Sink",   op_pin_state_pinned, irop_flag_none, oparity_zero, 0, 0, NULL);
	_source                = new_ir_node(NULL, irg, block, op_rss_Source, mode_ANY, 0, NULL);
	_sink                  = new_ir_node(NULL, irg, block, op_rss_Sink, mode_ANY, 0, NULL);
}

static int cmp_int(const void *a, const void *b) {
	const int *i1 = a;
	const int *i2 = b;

	return QSORT_CMP(*i1, *i2);
}

static int cmp_child_costs(const void *a, const void *b) {
	const child_t *c1 = a;
	const child_t *c2 = b;

	return QSORT_CMP(c1->cost, c2->cost);
}

static int cmp_irn_idx(const void *a, const void *b) {
	const ir_node *n1 = *(ir_node **)a;
	const ir_node *n2 = *(ir_node **)b;

	return QSORT_CMP(get_irn_idx(n1), get_irn_idx(n2));
}

static int cmp_rss_edges(const void *a, const void *b) {
	const rss_edge_t *e1 = a;
	const rss_edge_t *e2 = b;

	return (e1->src != e2->src) || (e1->tgt != e2->tgt);
}

static int bsearch_for_index(int key, int *arr, size_t len, int force) {
	int left = 0;
	int right = len;

	while (right >= left) {
		int idx = (left + right) / 2;

		if (key < arr[idx])
			right = idx - 1;
		else if (key > arr[idx])
			left = idx + 1;
		else
			return idx;
	}

	if (force)
		assert(0 && "Something is wrong, key not found.");
	return -1;
}

static ir_node **build_sorted_array_from_list(plist_t *irn_list, struct obstack *obst) {
	plist_element_t *el;
	int     i     = 0;
	int     len   = plist_count(irn_list);
	ir_node **arr = NEW_ARR_D(ir_node *, obst, len);

	/* copy the list into the array */
	foreach_plist(irn_list, el) {
		arr[i++] = plist_element_get_value(el);
	}

	/* sort the array by node index */
	qsort(arr, len, sizeof(arr[0]), cmp_irn_idx);

	return arr;
}

Christian Würdig's avatar
Christian Würdig committed
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*****************************************************
 *      _      _                       _
 *     | |    | |                     (_)
 *   __| | ___| |__  _   _  __ _  __ _ _ _ __   __ _
 *  / _` |/ _ \ '_ \| | | |/ _` |/ _` | | '_ \ / _` |
 * | (_| |  __/ |_) | |_| | (_| | (_| | | | | | (_| |
 *  \__,_|\___|_.__/ \__,_|\__, |\__, |_|_| |_|\__, |
 *                          __/ | __/ |         __/ |
 *                         |___/ |___/         |___/
 *
 *****************************************************/

static void dump_nodeset(nodeset *ns, const char *prefix) {
	ir_node *irn;
	foreach_nodeset(ns, irn) {
		ir_printf("%s%+F\n", prefix, irn);
	}
}

267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
static void build_file_name(rss_t *rss, const char *suffix, size_t suf_len, char *buf, size_t len) {
	const char *irg_name;

	memset(buf, 0, len);
	irg_name = get_entity_name(get_irg_entity(rss->irg));
	snprintf(buf, len - suf_len, "%s-%s-block-%d",
		irg_name, arch_register_class_name(rss->cls), get_irn_node_nr(rss->block));
	strcat(buf, suffix);
}

/* Dumps all collected bipartite components of current irg as vcg. */
static void debug_vcg_dump_bipartite(rss_t *rss) {
	cbc_t *cbc;
	FILE  *f;
	char  file_name[256];
	static const char suffix[] = "-RSS-CBC.vcg";

	build_file_name(rss, suffix, sizeof(suffix), file_name, sizeof(file_name));
	f = fopen(file_name, "w");

	ir_fprintf(f, "graph: { title: \"connected bipartite component graph of %+F\"\n", rss->irg);
	fprintf(f, "display_edge_labels: no\n");
	fprintf(f, "layoutalgorithm: mindepth\n");
	fprintf(f, "manhattan_edges: yes\n\n");

	foreach_pset(rss->cbc_set, cbc) {
		ir_node    *n;
		rss_edge_t *ke;

		fprintf(f, "graph: { titel: \"cbc %d\" label: \"cbc %d\" status:clustered color:yellow\n", cbc->nr, cbc->nr);
		foreach_nodeset(cbc->parents, n) {
			ir_fprintf(f, "node: { title: \"n%d_%d\" label: \"%+F\" }\n", get_irn_node_nr(n), cbc->nr, n);
		}
		foreach_nodeset(cbc->children, n) {
			ir_fprintf(f, "node: { title: \"n%d_%d\" label: \"%+F\" }\n", get_irn_node_nr(n), cbc->nr, n);
		}
		foreach_pset(cbc->kill_edges, ke) {
			ir_fprintf(f, "edge: { sourcename: \"n%d_%d\" targetname: \"n%d_%d\" }\n",
				get_irn_node_nr(ke->src), cbc->nr, get_irn_node_nr(ke->tgt), cbc->nr);
		}
		fprintf(f, "}\n\n");
	}
	fprintf(f, "}\n");
	fclose(f);
}

/* Dump the computed killing function as vcg. */
static void debug_vcg_dump_kill(rss_t *rss) {
Christian Würdig's avatar
Christian Würdig committed
315
316
	FILE            *f;
	char            file_name[256];
317
	plist_element_t *el;
Christian Würdig's avatar
Christian Würdig committed
318
	static const char suffix[] = "-RSS-KILL.vcg";
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359

	build_file_name(rss, suffix, sizeof(suffix), file_name, sizeof(file_name));
	f = fopen(file_name, "w");

	ir_fprintf(f, "graph: { title: \"computed kill graph of %+F, block %d\"\n", rss->irg, get_irn_node_nr(rss->block));
	fprintf(f, "display_edge_labels: no\n");
	fprintf(f, "layoutalgorithm: mindepth\n");
	fprintf(f, "manhattan_edges: yes\n\n");

	/* first: reset dumped flag of all nodes */
	foreach_plist(rss->nodes, el) {
		ir_node   *irn  = plist_element_get_value(el);
		rss_irn_t *rirn = get_rss_irn(rss, irn);
		rirn->dumped = 0;
	}

	/* dump all nodes and their killers */
	foreach_plist(rss->nodes, el) {
		ir_node   *irn     = plist_element_get_value(el);
		rss_irn_t *rirn    = get_rss_irn(rss, irn);
		rss_irn_t *pk_rirn = get_rss_irn(rss, rirn->killer);

		if (! rirn->dumped) {
			ir_fprintf(f, "node: { title: \"n%d\" label: \"%+F\" }\n", get_irn_node_nr(irn), irn);
			rirn->dumped = 1;
		}

		if (! pk_rirn->dumped) {
			ir_fprintf(f, "node: { title: \"n%d\" label: \"%+F\" }\n", get_irn_node_nr(rirn->killer), rirn->killer);
			pk_rirn->dumped = 1;
		}

		ir_fprintf(f, "edge: { sourcename: \"n%d\" targetname: \"n%d\" }\n",
			get_irn_node_nr(rirn->killer), get_irn_node_nr(irn));
	}

	fprintf(f, "}\n");
	fclose(f);
}

/* Dumps the potential killing DAG (PKG) as vcg. */
Christian Würdig's avatar
Christian Würdig committed
360
361
362
363
364
365
366
367
368
369
370
371
372
373
static void debug_vcg_dump_pkg(rss_t *rss, nodeset *max_ac, int iteration) {
	FILE              *f;
	char              file_name[256];
	char              *suffix   = alloca(32);
	static const char suffix1[] = "-RSS-PKG.vcg";
	static const char suffix2[] = "-RSS-PKG-MAXAC.vcg";
	plist_element_t   *el;

	if (! max_ac) {
		snprintf(suffix, 32, "%s", suffix1);
	}
	else {
		snprintf(suffix, 32, "-%0.2d%s", iteration, suffix2);
	}
374

Christian Würdig's avatar
Christian Würdig committed
375
	build_file_name(rss, suffix, strlen(suffix) + 1, file_name, sizeof(file_name));
376
377
378
379
380
381
382
383
384
385
	f = fopen(file_name, "w");

	ir_fprintf(f, "graph: { title: \"potential killing DAG of %+F, block %d\"\n", rss->irg, get_irn_node_nr(rss->block));
	fprintf(f, "display_edge_labels: no\n");
	fprintf(f, "layoutalgorithm: mindepth\n");
	fprintf(f, "manhattan_edges: yes\n\n");

	foreach_plist(rss->nodes, el) {
		ir_node   *irn  = plist_element_get_value(el);
		rss_irn_t *rirn = get_rss_irn(rss, irn);
Christian Würdig's avatar
Christian Würdig committed
386
		char      *c1   = "";
387
388
		plist_element_t *k_el;

Christian Würdig's avatar
Christian Würdig committed
389
390
391
392
393
		/* dump selected saturating values in yellow */
		if (max_ac && nodeset_find(max_ac, irn))
			c1 = "color:yellow";

		ir_fprintf(f, "node: { title: \"n%d\" label: \"%+F\" %s }\n", get_irn_node_nr(irn), irn, c1);
394
395
396
397
398
		rirn->dumped = 1;

		foreach_plist(rirn->pkiller_list, k_el) {
			ir_node   *pkiller = plist_element_get_value(k_el);
			rss_irn_t *pk_rirn = get_rss_irn(rss, pkiller);
Christian Würdig's avatar
Christian Würdig committed
399
400
401
402
			char      *c2      = "";

			if (max_ac && nodeset_find(max_ac, pkiller))
				c2 = "color:yellow";
403
404

			if (! pk_rirn->dumped) {
Christian Würdig's avatar
Christian Würdig committed
405
				ir_fprintf(f, "node: { title: \"n%d\" label: \"%+F\" %s }\n", get_irn_node_nr(pkiller), pkiller, c2);
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
				pk_rirn->dumped = 1;
			}
			ir_fprintf(f, "edge: { sourcename: \"n%d\" targetname: \"n%d\" }\n",
				get_irn_node_nr(pkiller), get_irn_node_nr(irn));
		}
	}
	fprintf(f, "}\n");
	fclose(f);
}

/* Dumps the disjoint value DAG (DVG) as vcg. */
static void debug_vcg_dump_dvg(rss_t *rss, dvg_t *dvg) {
	static const char suffix[] = "-RSS-DVG.vcg";
	FILE       *f;
	char       file_name[256];
	ir_node    *irn;
	rss_edge_t *edge;

	build_file_name(rss, suffix, sizeof(suffix), file_name, sizeof(file_name));
	f = fopen(file_name, "w");

	ir_fprintf(f, "graph: { title: \"disjoint value DAG of %+F, block %d\"\n", rss->irg, get_irn_node_nr(rss->block));
	fprintf(f, "display_edge_labels: no\n");
	fprintf(f, "layoutalgorithm: mindepth\n");
	fprintf(f, "manhattan_edges: yes\n\n");

	/* dump all nodes */
	foreach_nodeset(dvg->nodes, irn) {
		ir_fprintf(f, "node: { title: \"n%d\" label: \"%+F\" }\n", get_irn_node_nr(irn), irn);
	}

	/* dump all edges */
	foreach_pset(dvg->edges, edge) {
		rss_irn_t *src = get_rss_irn(rss, edge->src);
		rss_irn_t *tgt = get_rss_irn(rss, edge->tgt);

		ir_fprintf(f, "edge: { sourcename: \"n%d\" targetname: \"n%d\" }\n",
			get_irn_node_nr(edge->src), get_irn_node_nr(edge->tgt));
	}

	fprintf(f, "}\n");
	fclose(f);
}

Christian Würdig's avatar
Christian Würdig committed
450
#if 0
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/* Dumps the PKG(DVG). */
static void debug_vcg_dump_dvg_pkiller(rss_t *rss, dvg_t *dvg) {
	static const char suffix[] = "-RSS-DVG-PKG.vcg";
	FILE       *f;
	char       file_name[256];
	ir_node    *irn;

	build_file_name(rss, suffix, sizeof(suffix), file_name, sizeof(file_name));
	f = fopen(file_name, "w");

	ir_fprintf(f, "graph: { title: \"PKG of disjoint value DAG of %+F, block %d\"\n", rss->irg, get_irn_node_nr(rss->block));
	fprintf(f, "display_edge_labels: no\n");
	fprintf(f, "layoutalgorithm: mindepth\n");
	fprintf(f, "manhattan_edges: yes\n\n");

	/* dump all nodes */
	foreach_nodeset(dvg->nodes, irn) {
		ir_fprintf(f, "node: { title: \"n%d\" label: \"%+F\" }\n", get_irn_node_nr(irn), irn);
	}

	/* dump all edges */
	foreach_nodeset(dvg->nodes, irn) {
		rss_irn_t       *node = get_rss_irn(rss, irn);
		plist_element_t *el;

		foreach_plist(node->dvg_pkiller_list, el) {
			ir_fprintf(f, "edge: { sourcename: \"n%d\" targetname: \"n%d\" }\n",
				get_irn_node_nr(plist_element_get_value(el)), get_irn_node_nr(irn));
		}
	}

	fprintf(f, "}\n");
	fclose(f);
}
Christian Würdig's avatar
Christian Würdig committed
485
#endif /* if 0 */
486
487
488
489
490

/**
 * In case there is no rss information for irn, initialize it.
 */
static void *init_rss_irn(phase_t *ph, ir_node *irn, void *old) {
Christian Würdig's avatar
Christian Würdig committed
491
	rss_irn_t *res = old ? old : phase_alloc(ph, sizeof(res[0]));
492
493
494
495
496
497
498
499
500
501
502
503
504

	res->descendant_list  = plist_obstack_new(phase_obst(ph));
	res->descendants      = NULL;

	res->consumer_list    = plist_obstack_new(phase_obst(ph));
	res->consumer         = NULL;

	res->pkiller_list     = plist_obstack_new(phase_obst(ph));
	res->pkillers         = NULL;

	res->parent_list      = plist_obstack_new(phase_obst(ph));
	res->parents          = NULL;

Christian Würdig's avatar
Christian Würdig committed
505
506
	//res->dvg_desc_list    = plist_obstack_new(phase_obst(ph));
	//res->dvg_desc         = NULL;
507
508

	res->kill_value_list  = plist_obstack_new(phase_obst(ph));
Christian Würdig's avatar
Christian Würdig committed
509
510
	//res->dvg_user_list    = plist_obstack_new(phase_obst(ph));
	//res->dvg_pkiller_list = plist_obstack_new(phase_obst(ph));
511
512
513
514
515
516
517
518
519

	res->killer           = NULL;
	res->irn              = irn;
	res->chain            = NULL;

	res->live_out         = 0;
	res->visited          = 0;
	res->handled          = 0;
	res->dumped           = 0;
520
521
	res->havedesc         = 0;
	res->havecons         = 0;
522
523
524
525
526
527
528
529
530

	return res;
}

/**
 * Collect all nodes data dependent on current node.
 */
static void collect_descendants(rss_t *rss, rss_irn_t *rirn, ir_node *irn, int *got_sink) {
	const ir_edge_t *edge;
531
532
533
534
535
536
537
538
539
540
541
542
	rss_irn_t       *cur_node = get_rss_irn(rss, irn);
	ir_node         *block    = rss->block;
	ir_edge_kind_t  ekind[2]  = { EDGE_KIND_NORMAL, EDGE_KIND_DEP };
	int             i;

//	if (cur_node->havedesc)
//		return;
//	cur_node->havedesc = 1;

	/* Stop at Barriers and Keeps */
	if (be_is_Barrier(irn) || be_is_Keep(irn))
		return;
543

Christian Würdig's avatar
Christian Würdig committed
544
545
546
547
	/* loop over normal and dependency edges */
	for (i = 0; i < 2; ++i) {
		foreach_out_edge_kind(irn, edge, ekind[i]) {
			ir_node *user = get_edge_src_irn(edge);
548

Christian Würdig's avatar
Christian Würdig committed
549
550
551
			/* skip ignore nodes as they do not really contribute to register pressure */
			if (arch_irn_is(rss->arch_env, user, ignore))
				continue;
552

553
554
555
			if (is_Proj(user)) {
				if (get_irn_mode(user) != mode_X && arch_get_irn_reg_class(rss->arch_env, user, -1) == rss->cls)
					collect_descendants(rss, rirn, user, got_sink);
Christian Würdig's avatar
Christian Würdig committed
556
			}
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
			else {
				/* check if user lives in block and is not a control flow node */
				if (get_nodes_block(user) == block) {
					if (! plist_has_value(rirn->descendant_list, user)) {
						plist_insert_back(rirn->descendant_list, user);
						DBG((rss->dbg, LEVEL_2, "\t\tdescendant %+F\n", user));
					}
					collect_descendants(rss, rirn, user, got_sink);
				}
				else if (! *got_sink) {
					/* user lives out of block: add sink as descendant if not already done */
					plist_insert_back(rirn->descendant_list, _sink);
					*got_sink = 1;
					DBG((rss->dbg, LEVEL_2, "\t\tdescendant %+F\n", _sink));
				}
572
573
574
575
576
577
578
579
			}
		}
	}
}

/**
 * Handles a single consumer.
 */
580
static void collect_single_consumer(rss_t *rss, rss_irn_t *rss_irn, ir_node *consumer, int *got_sink) {
Christian Würdig's avatar
Christian Würdig committed
581
582
	ir_node        *block   = rss->block;
	ir_edge_kind_t ekind[2] = { EDGE_KIND_NORMAL, EDGE_KIND_DEP };
583

584
	assert(! is_Proj(consumer) && "Cannot handle Projs");
Christian Würdig's avatar
Christian Würdig committed
585

586
587
	if (get_nodes_block(consumer) == block) {
		if (! arch_irn_is(rss->arch_env, consumer, ignore)) {
588
			plist_insert_back(rss_irn->consumer_list, consumer);
Christian Würdig's avatar
Christian Würdig committed
589
			DBG((rss->dbg, LEVEL_2, "\t\tconsumer %+F\n", consumer));
590
591
592
593
		}
	}
	else {
		rss_irn->live_out = 1;
Christian Würdig's avatar
Christian Würdig committed
594
		DBG((rss->dbg, LEVEL_2, "\t\tlive out %+F", consumer));
595
		if (! *got_sink) {
596
			plist_insert_back(rss_irn->consumer_list, _sink);
597
			*got_sink = 1;
Christian Würdig's avatar
Christian Würdig committed
598
			DB((rss->dbg, LEVEL_2, ", %+F added instead", _sink));
599
		}
Christian Würdig's avatar
Christian Würdig committed
600
		DB((rss->dbg, LEVEL_2, "\n"));
601
602
603
604
605
606
	}
}

/**
 * Collect all nodes consuming the value(s) produced by current node.
 */
607
static void collect_consumer(rss_t *rss, rss_irn_t *rss_irn, ir_node *irn, int *got_sink) {
608
	const ir_edge_t *edge;
Christian Würdig's avatar
Christian Würdig committed
609
	int             i;
610
611
612
613
614
615
	ir_edge_kind_t  ekind[2]  = { EDGE_KIND_NORMAL, EDGE_KIND_DEP };
	rss_irn_t       *cur_node = get_rss_irn(rss, irn);

	if (cur_node->havecons)
		return;
	cur_node->havecons = 1;
Christian Würdig's avatar
Christian Würdig committed
616
617
618
619

	for (i = 0; i < 2; ++i) {
		foreach_out_edge_kind(irn, edge, ekind[i]) {
			ir_node *consumer = get_edge_src_irn(edge);
620
621
622
623
624
625
626

			if (is_Proj(consumer)) {
				if (arch_get_irn_reg_class(rss->arch_env, consumer, -1) == rss->cls)
					collect_consumer(rss, rss_irn, consumer, got_sink);
			}
			else
				collect_single_consumer(rss, rss_irn, consumer, got_sink);
Christian Würdig's avatar
Christian Würdig committed
627
		}
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
	}
}

#if 0
/**
 * We need to build the consumer and descendant list for _source.
 */
static void collect_node_info_source(rss_t *rss) {
	const ir_edge_t *edge;
	rss_irn_t       *rirn  = get_rss_irn(rss, _source);
	ir_node         *block = rss->block;

	if (rirn->handled)
		return;

	foreach_out_edge(block, edge) {
		ir_node *irn = get_edge_src_irn(edge);
		int     i;

		for (i = get_irn_arity(n) - 1; i >= 0; --i) {

		}
	}
}

static void reset_node_info(rss_irn_t *rss_irn) {
	/* Beware: array data resides on phase obstack, so it gets removed automatically */

	plist_clear(rss_irn->consumer_list);
	rss_irn->consumer = NULL;

	plist_clear(rss_irn->parent_list);
	rss_irn->parents = NULL;

	plist_clear(rss_irn->descendant_list);
	rss_irn->descendants = NULL;

	plist_clear(rss_irn->pkiller_list);
	rss_irn->pkillers = NULL;

	plist_clear(rss_irn->kill_value_list);

	rss_irn->killer   = NULL;
	rss_irn->live_out = 0;
	rss_irn->visited  = 0;
	rss_irn->handled  = 0;
}
Christian Würdig's avatar
Christian Würdig committed
675
#endif /* if 0 */
676
677
678
679
680
681
682
683

/**
 * Collects all consumer and descendant of a irn.
 */
static void collect_node_info(rss_t *rss, ir_node *irn) {
	rss_irn_t *rss_irn = get_rss_irn(rss, irn);
	int       got_sink;

684
	assert(! is_Proj(irn) && "Cannot handle Projs.");
685
686
687

	/* check if node info is already available */
	if (rss_irn->handled)
Christian Würdig's avatar
Christian Würdig committed
688
		phase_reinit_single_irn_data(&rss->ph, irn);
689

Christian Würdig's avatar
Christian Würdig committed
690
	DBG((rss->dbg, LEVEL_1, "\tcomputing consumers of %+F:\n", irn));
691
692

	/* collect all consumer */
693
694
	got_sink = 0;
	collect_consumer(rss, rss_irn, irn, &got_sink);
695
696
697
698

	/* build sorted consumer array */
	rss_irn->consumer = build_sorted_array_from_list(rss_irn->consumer_list, phase_obst(&rss->ph));

Christian Würdig's avatar
Christian Würdig committed
699
	DBG((rss->dbg, LEVEL_1, "\tcompute descendants of %+F:\n", irn));
700
701
702

	/* collect descendants */
	got_sink = 0;
703
	collect_descendants(rss, rss_irn, irn, &got_sink);
704
705
706
707
708
709
710
711
712

	/* build sorted descendant array */
	rss_irn->descendants = build_sorted_array_from_list(rss_irn->descendant_list, phase_obst(&rss->ph));

	rss_irn->handled = 1;
}

/**
 * Checks if v is a potential killer of u.
713
 * v is in pkill(u) iff descendants(v) cut consumer(u) is v
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
 *
 * @param rss   The rss object
 * @param v      The node to check for killer
 * @param u      The potentially killed value
 * @return 1 if v is in pkill(u), 0 otherwise
 */
static int is_potential_killer(rss_t *rss, rss_irn_t *v, rss_irn_t *u) {
	plist_t *list;
	ir_node **arr;
	plist_element_t *el;

	assert(is_Sink(v->irn) || ((plist_count(v->descendant_list) > 0 && v->descendants) || 1));
	assert(is_Sink(u->irn) || ((plist_count(u->consumer_list)   > 0 && u->consumer)    || 1));

	/* as we loop over the list: loop over the shorter one */
	if (plist_count(v->descendant_list) > plist_count(u->consumer_list)) {
		list = u->consumer_list;
		arr  = v->descendants;
	}
	else {
		list = v->descendant_list;
		arr  = u->consumer;
	}

	/* for each list element: try to find element in array */
	foreach_plist(list, el) {
740
741
742
743
		ir_node *irn   = plist_element_get_value(el);
		ir_node *match = BSEARCH_IRN_ARR(irn, arr);

		if (match && match != irn)
744
745
746
747
748
749
			return 0;
	}

	return 1;
}

Christian Würdig's avatar
Christian Würdig committed
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
789
790
/**
 * Update descendants, consumer and pkiller list for the given irn.
 */
static void update_node_info(rss_t *rss, ir_node *irn, ir_node *pk_irn) {
	rss_irn_t *node    = get_rss_irn(rss, irn);
	rss_irn_t *pkiller = get_rss_irn(rss, pk_irn);

	/* add consumer and rebuild the consumer array */
	if (! plist_has_value(node->consumer_list, pk_irn)) {
		plist_insert_back(node->consumer_list, pk_irn);
		node->consumer = build_sorted_array_from_list(node->consumer_list, phase_obst(&rss->ph));
	}

	/* add pk_irn as descendant, add it's descendants to irn's and rebuild array */
	if (! plist_has_value(node->descendant_list, pk_irn)) {
		plist_element_t *el;

		plist_insert_back(node->descendant_list, pk_irn);

		foreach_plist(pkiller->descendant_list, el) {
			ir_node *desc = plist_element_get_value(el);

			if (! plist_has_value(node->descendant_list, desc))
				plist_insert_back(node->descendant_list, desc);
		}

		node->descendants = build_sorted_array_from_list(node->descendant_list, phase_obst(&rss->ph));
	}

#if 0
	/* check, if pkiller is a potential killer of irn */
	if (is_potential_killer(rss, pkiller, node)) {
		if (! plist_has_value(node->pkiller_list, pk_irn))
			plist_insert_back(node->pkiller_list, pk_irn);
		if (! plist_has_value(pkiller->kill_value_list, irn))
			plist_insert_back(pkiller->kill_value_list, irn);
		DBG((rss->dbg, LEVEL_1, "\tpotential killer %+F added to %+F\n", pk_irn, irn));
	}
#endif
}

791
792
793
794
795
796
797
798
799
800
/**
 * Compute the potential killing set PK.
 */
static void compute_pkill_set(rss_t *rss) {
	plist_element_t *u_el, *v_el;

	foreach_plist(rss->nodes, u_el) {
		ir_node   *u_irn = plist_element_get_value(u_el);
		rss_irn_t *u     = get_rss_irn(rss, u_irn);

Christian Würdig's avatar
Christian Würdig committed
801
		DBG((rss->dbg, LEVEL_1, "\tcomputing potential killers of %+F:\n", u_irn));
802
803
804
805
806
807
808
809
810
811
812
813

		/* check each consumer if it is a potential killer  */
		foreach_plist(u->consumer_list, v_el) {
			ir_node   *v_irn = plist_element_get_value(v_el);
			rss_irn_t *v     = get_rss_irn(rss, v_irn);

			/* check, if v is a potential killer of u */
			if (is_potential_killer(rss, v, u)) {
				if (! plist_has_value(u->pkiller_list, v_irn))
					plist_insert_back(u->pkiller_list, v_irn);
				if (! plist_has_value(v->kill_value_list, u_irn))
					plist_insert_back(v->kill_value_list, u_irn);
Christian Würdig's avatar
Christian Würdig committed
814
				DBG((rss->dbg, LEVEL_2, "\t\tpotential killer %+F\n", v_irn));
815
816
817
818
819
820
821
			}
		}

		u->killer = _sink;
	}

	DEBUG_ONLY(
Christian Würdig's avatar
Christian Würdig committed
822
823
		if (firm_dbg_get_mask(rss->dbg))
			debug_vcg_dump_pkg(rss, NULL, 0);
824
825
826
827
828
829
830
831
832
833
834
835
836
	)
}

/**
 * Build set of killing edges (from values to their potential killers)
 */
static void build_kill_edges(rss_t *rss, pset *epk) {
	plist_element_t *el, *k_el;

	foreach_plist(rss->nodes, el) {
		ir_node    *irn  = plist_element_get_value(el);
		rss_irn_t *rirn = get_rss_irn(rss, irn);

Christian Würdig's avatar
Christian Würdig committed
837
838
		DBG((rss->dbg, LEVEL_3, "\t\tbuilding kill edges for %+F:\n", irn));

839
840
841
842
843
844
845
846
		foreach_plist(rirn->pkiller_list, k_el) {
			ir_node    *pkiller = plist_element_get_value(k_el);
			rss_edge_t *ke      = obstack_alloc(phase_obst(&rss->ph), sizeof(*ke));

			ke->src  = irn;
			ke->tgt  = pkiller;
			ke->next = NULL;

Christian Würdig's avatar
Christian Würdig committed
847
848
			DBG((rss->dbg, LEVEL_3, "\t\t\ttarget %+F\n", pkiller));

849
850
851
852
853
854
855
856
857
858
			pset_insert(epk, ke, HASH_RSS_EDGE(ke));
		}
	}
}

/* print the given cbc for debugging purpose */
static void debug_print_cbc(firm_dbg_module_t *mod, cbc_t *cbc) {
	ir_node    *n;
	rss_edge_t *ke;

Christian Würdig's avatar
Christian Würdig committed
859
	DBG((mod, LEVEL_3, "\t\tS = set of parents:\n"));
860
	foreach_nodeset(cbc->parents, n) {
Christian Würdig's avatar
Christian Würdig committed
861
		DBG((mod, LEVEL_3, "\t\t\t%+F\n", n));
862
	}
Christian Würdig's avatar
Christian Würdig committed
863
	DBG((mod, LEVEL_3, "\t\tT = set of children:\n"));
864
	foreach_nodeset(cbc->children, n) {
Christian Würdig's avatar
Christian Würdig committed
865
		DBG((mod, LEVEL_3, "\t\t\t%+F\n", n));
866
	}
Christian Würdig's avatar
Christian Würdig committed
867
	DBG((mod, LEVEL_3, "\t\tE = Edges from producers to consumers\n"));
868
	foreach_pset(cbc->kill_edges, ke) {
Christian Würdig's avatar
Christian Würdig committed
869
		DBG((mod, LEVEL_3, "\t\t\t%+F -> %+F\n", ke->src, ke->tgt));
870
871
872
873
874
875
876
877
878
879
880
881
882
883
	}
}

/**
 * Construct the bipartite decomposition.
 * Sid-Ahmed-Ali Touati, Phd Thesis
 * Register Pressure in Instruction Level Parallelism, p. 71
 */
static void compute_bipartite_decomposition(rss_t *rss) {
	pset *epk    = new_pset(cmp_rss_edges, 10);
	int  cur_num = 0;

	plist_element_t *el;

Christian Würdig's avatar
Christian Würdig committed
884
	DBG((rss->dbg, LEVEL_1, "\tcomputing bipartite decomposition:\n"));
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902

	build_kill_edges(rss, epk);

	foreach_plist(rss->nodes, el) {
		ir_node   *u_irn   = plist_element_get_value(el);
		rss_irn_t *u       = get_rss_irn(rss, u_irn);
		int       p_change = 1;
		int       c_change = 1;

		cbc_t           *cbc;
		plist_element_t *el2;
		rss_edge_t      *k_edge;
		rss_edge_t      *kedge_root = NULL;
		ir_node         *t_irn, *s_irn;

		if (u->visited || u_irn == _sink)
			continue;

Christian Würdig's avatar
Christian Würdig committed
903
		DBG((rss->dbg, LEVEL_2, "\t\t%+F choosen:\n", u_irn));
904
905
906
907
908
909
910

		cbc     = obstack_alloc(phase_obst(&rss->ph), sizeof(*cbc));
		cbc->nr = cur_num++;

		/* initialize S_cb */
		cbc->parents = new_nodeset(5);
		nodeset_insert(cbc->parents, u_irn);
Christian Würdig's avatar
Christian Würdig committed
911
		DBG((rss->dbg, LEVEL_3, "\t\t\t%+F added to parents (init)\n", u_irn));
912
913
914
915
916
917
918
919
920
921

		/* E_cb = empty */
		cbc->kill_edges = new_pset(cmp_rss_edges, 5);

		/* each parent gets killed by at least one value from children */

		/* T_cb = PK_successors(u) */
		cbc->children = new_nodeset(5);
		foreach_plist(u->pkiller_list, el2) {
			nodeset_insert(cbc->children, plist_element_get_value(el2));
Christian Würdig's avatar
Christian Würdig committed
922
			DBG((rss->dbg, LEVEL_3, "\t\t\t%+F added to children (init)\n", plist_element_get_value(el2)));
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
		}

		/*
			Now: insert the parents of all children into the parent set
			and insert the children of all parents into the children set
			until the sets don't change any more
		*/
		while (p_change || c_change) {
			p_change = c_change = 0;

			/* accumulate parents */
			foreach_nodeset(cbc->children, t_irn) {
				rss_irn_t *t = get_rss_irn(rss, t_irn);
				plist_element_t *kvl_el;

Christian Würdig's avatar
Christian Würdig committed
938
939
				foreach_pset(epk, k_edge) {
					ir_node *val = k_edge->src;
940

Christian Würdig's avatar
Christian Würdig committed
941
					if (k_edge->src == t_irn && ! nodeset_find(cbc->parents, k_edge->src)) {
942
943
						nodeset_insert(cbc->parents, val);
						p_change = 1;
Christian Würdig's avatar
Christian Würdig committed
944
						DBG((rss->dbg, LEVEL_3, "\t\t\t%+F added to parents\n", val));
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
					}
				}
			}

			/* accumulate children */
			foreach_nodeset(cbc->parents, s_irn) {
				rss_irn_t *s = get_rss_irn(rss, s_irn);
				plist_element_t *pkl_el;

				foreach_plist(s->pkiller_list, pkl_el) {
					ir_node *val = plist_element_get_value(pkl_el);

					if (! nodeset_find(cbc->children, val)) {
						nodeset_insert(cbc->children, val);
						c_change = 1;
Christian Würdig's avatar
Christian Würdig committed
960
						DBG((rss->dbg, LEVEL_3, "\t\t\t%+F added to children\n", val));
961
962
963
964
965
966
967
968
969
970
971
972
					}
				}
			}
		}

		/* mark all parent values as visited */
		foreach_nodeset(cbc->parents, s_irn) {
			rss_irn_t *s = get_rss_irn(rss, s_irn);
			s->visited = 1;
			/* assure bipartite property */
			if (nodeset_find(cbc->children, s_irn)) {
				nodeset_remove(cbc->children, s_irn);
Christian Würdig's avatar
Christian Würdig committed
973
				DBG((rss->dbg, LEVEL_3, "\t\t\t%+F removed from to children\n", s_irn));
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
			}
		}

		/* update edges */
		foreach_pset(epk, k_edge) {
			if (nodeset_find(cbc->parents, k_edge->src) && nodeset_find(cbc->children, k_edge->tgt)) {
				pset_insert(cbc->kill_edges, k_edge, HASH_RSS_EDGE(k_edge));
				/*
					Link all k_edges which are about to be removed together.
					Beware: pset_remove kills the iterator.
				*/
				k_edge->next = kedge_root;
				kedge_root   = k_edge;
			}
		}

		/* remove all linked k_edges */
		for (; kedge_root; kedge_root = kedge_root->next) {
			pset_remove(epk, kedge_root, HASH_RSS_EDGE(kedge_root));
		}

		/* add the connected bipartite component */
996
997
998
999
1000
		if (nodeset_count(cbc->parents) > 0 && nodeset_count(cbc->children) > 0 && pset_count(cbc->kill_edges) > 0)
			pset_insert(rss->cbc_set, cbc, (unsigned)cbc->nr);
		DBG((rss->dbg, LEVEL_2, "\tbipartite component %d inserted:\n", cbc->nr));
		DEBUG_ONLY(
			if (firm_dbg_get_mask(rss->dbg) & LEVEL_2)
For faster browsing, not all history is shown. View entire blame