iredges.c 27.1 KB
Newer Older
Christian Würdig's avatar
Christian Würdig committed
1
/*
2
 * Copyright (C) 1995-2011 University of Karlsruhe.  All right reserved.
Christian Würdig's avatar
Christian Würdig committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 *
 * 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.
 */

20
/**
Matthias Braun's avatar
Matthias Braun committed
21
22
23
24
 * @file
 * @brief   Always available outs.
 * @author  Sebastian Hack, Michael Beck, Andreas Schoesser
 * @date    14.1.2005
yb9976's avatar
yb9976 committed
25
 * @brief
Matthias Braun's avatar
Matthias Braun committed
26
27
 *   This are out-edges (also called def-use edges) that are dynamically
 *   updated as the graph changes.
28
 */
29
#include "config.h"
30
31

#include "irnode_t.h"
32
#include "iropt_t.h"
Sebastian Hack's avatar
Sebastian Hack committed
33
#include "iredgekinds.h"
34
#include "iredges_t.h"
Michael Beck's avatar
Michael Beck committed
35
#include "irgwalk.h"
36
37
38
39
#include "irdump_t.h"
#include "irprintf.h"
#include "debug.h"
#include "set.h"
Christian Würdig's avatar
Christian Würdig committed
40
#include "bitset.h"
41
#include "error.h"
42
#include "irpass_t.h"
43

44
45
46
47
48
49
50
51
52
53
#include "iredgeset.h"
#include "hashptr.h"

#define DO_REHASH
#define SCALAR_RETURN
#define HashSet                   ir_edgeset_t
#define HashSetIterator           ir_edgeset_iterator_t
#define ValueType                 ir_edge_t*
#define NullValue                 NULL
#define DeletedValue              ((ir_edge_t*)-1)
54
#define Hash(this,key)            (hash_ptr(key->src) ^ (key->pos * 40013))
55
56
57
58
#define KeysEqual(this,key1,key2) ((key1->src) == (key2->src) && (key1->pos == key2->pos))
#define SetRangeEmpty(ptr,size)   memset(ptr, 0, (size) * sizeof((ptr)[0]))

#define hashset_init            ir_edgeset_init
59
void ir_edgeset_init_size(ir_edgeset_t *self, size_t size);
60
61
62
63
#define hashset_init_size       ir_edgeset_init_size
#define hashset_destroy         ir_edgeset_destroy
#define hashset_insert          ir_edgeset_insert
#define hashset_remove          ir_edgeset_remove
64
ir_edge_t *ir_edgeset_find(const ir_edgeset_t *self, const ir_edge_t*);
65
#define hashset_find            ir_edgeset_find
66
size_t ir_edgeset_size(const ir_edgeset_t *self);
67
68
69
70
71
72
73
#define hashset_size            ir_edgeset_size
#define hashset_iterator_init   ir_edgeset_iterator_init
#define hashset_iterator_next   ir_edgeset_iterator_next
#define hashset_remove_iterator ir_edgeset_remove_iterator

#include "hashset.c"

Sebastian Hack's avatar
Sebastian Hack committed
74
/**
Matthias Braun's avatar
Matthias Braun committed
75
76
77
78
 * A function that allows for setting an edge.
 * This abstraction is necessary since different edge kind have
 * different methods of setting edges.
 */
Sebastian Hack's avatar
Sebastian Hack committed
79
80
typedef void (set_edge_func_t)(ir_node *src, int pos, ir_node *tgt);

81
82
83
84
/**
 * A function that returns the "arity" of a given edge kind
 * for a node.
 */
Sebastian Hack's avatar
Sebastian Hack committed
85
86
typedef int (get_edge_src_arity_func_t)(const ir_node *src);

87
88
89
/**
 * A function that returns the pos'th edge of a given edge kind for a node.
 */
Sebastian Hack's avatar
Sebastian Hack committed
90
91
92
typedef ir_node *(get_edge_src_n_func_t)(const ir_node *src, int pos);

/**
Matthias Braun's avatar
Matthias Braun committed
93
94
 * Additional data for an edge kind.
 */
Sebastian Hack's avatar
Sebastian Hack committed
95
typedef struct {
96
97
98
99
100
	const char                *name;       /**< name of this edge kind */
	set_edge_func_t           *set_edge;   /**< the set_edge function */
	int                       first_idx;   /**< index of the first possible edge */
	get_edge_src_arity_func_t *get_arity;  /**< the get_arity function */
	get_edge_src_n_func_t     *get_n;      /**< the get_n function */
Sebastian Hack's avatar
Sebastian Hack committed
101
102
} ir_edge_kind_info_t;

103
104
105
/**
 * Get the predecessor block.
 */
106
107
static ir_node *get_block_n(const ir_node *block, int pos)
{
108
109
	if (is_Block(block))
		return get_Block_cfgpred_block(block, pos);
110
111
	/* might be a Bad */
	return NULL;
Sebastian Hack's avatar
Sebastian Hack committed
112
113
}

114
115
116
117
118
119
120
static ir_node *get_irn_safe_n(const ir_node *node, int n)
{
	if (n == -1 && is_Block(node))
		return NULL;
	return get_irn_n(node, n);
}

Sebastian Hack's avatar
Sebastian Hack committed
121
static const ir_edge_kind_info_t edge_kind_info[EDGE_KIND_LAST] = {
122
123
124
	{ "normal"     , set_irn_n,   -1, get_irn_arity,  get_irn_safe_n },
	{ "block succs", NULL,         0, get_irn_arity,  get_block_n    },
	{ "dependency",  set_irn_dep,  0, get_irn_deps,   get_irn_dep    }
Sebastian Hack's avatar
Sebastian Hack committed
125
126
};

127
#define foreach_tgt(irn, i, n, kind) for (i = edge_kind_info[kind].first_idx, n = edge_kind_info[kind].get_arity(irn); i < n; ++i)
Sebastian Hack's avatar
Sebastian Hack committed
128
129
130
#define get_n(irn, pos, kind)        (edge_kind_info[kind].get_n(irn, pos))
#define get_kind_str(kind)           (edge_kind_info[kind].name)

131
DEBUG_ONLY(static firm_dbg_module_t *dbg;)
132

133
134
135
136
137
138
/**
 * This flag is set to 1, if the edges get initialized for an irg.
 * Then register additional data is forbidden.
 */
static int edges_used = 0;

139
140
141
142
/**
 * Summed size of all users private data
 */

143
static size_t edges_private_size = 0;
144
#define EDGE_SIZE (sizeof(ir_edge_t) + edges_private_size)
145

146
147
148
149
150
/**
 * If set to 1, the list heads are checked every time an edge is changed.
 */
static int edges_dbg = 0;

151
152
153
/**
 * Returns an ID for the given edge.
 */
154
155
static inline long edge_get_id(const ir_edge_t *e)
{
156
157
	return (long)e;
}
158

159
size_t edges_register_private_data(size_t n)
160
{
161
	size_t res = edges_private_size;
162
163
164
165
166
167
168

	assert(!edges_used && "you cannot register private edge data, if edges have been initialized");

	edges_private_size += n;
	return res;
}

169
170
void edges_reset_private_data(ir_graph *irg, int offset, unsigned size)
{
171
	irg_edge_info_t       *info = get_irg_edge_info(irg, EDGE_KIND_NORMAL);
172
173
	ir_edge_t             *edge;
	ir_edgeset_iterator_t  iter;
174

175
	foreach_ir_edgeset(&info->edges, edge, iter) {
176
177
178
179
		memset(edge + sizeof(*edge) + offset, 0, size);
	}
}

180
181
182
183
#define TIMES37(x) (((x) << 5) + ((x) << 2) + (x))

#define get_irn_out_list_head(irn) (&get_irn_out_info(irn)->outs)

184
#define edge_hash(edge) (TIMES37((edge)->pos) + hash_ptr((edge)->src))
185

186
187
void edges_init_graph_kind(ir_graph *irg, ir_edge_kind_t kind)
{
188
	if (edges_activated_kind(irg, kind)) {
189
		irg_edge_info_t *info = get_irg_edge_info(irg, kind);
Michael Beck's avatar
Michael Beck committed
190
		size_t amount = irg->estimated_node_count * 2;
191

192
		edges_used = 1;
193
		if (info->allocated) {
194
195
196
			amount = ir_edgeset_size(&info->edges);
			ir_edgeset_destroy(&info->edges);
			obstack_free(&info->edges_obst, NULL);
197
		}
198
		obstack_init(&info->edges_obst);
199
		INIT_LIST_HEAD(&info->free_edges);
200
201
		ir_edgeset_init_size(&info->edges, amount);
		info->allocated = 1;
202
203
204
	}
}

Matthias Braun's avatar
Matthias Braun committed
205
const ir_edge_t *get_irn_edge_kind(const ir_node *src, int pos, ir_edge_kind_t kind)
Sebastian Hack's avatar
Sebastian Hack committed
206
{
Matthias Braun's avatar
Matthias Braun committed
207
	ir_graph *irg = get_irn_irg(src);
208
	if (edges_activated_kind(irg, kind)) {
209
		irg_edge_info_t *info = get_irg_edge_info(irg, kind);
210
		ir_edge_t       key;
Sebastian Hack's avatar
Sebastian Hack committed
211

212
		key.src = (ir_node *)src;
Michael Beck's avatar
Michael Beck committed
213
		key.pos = pos;
214
215

		return ir_edgeset_find(&info->edges, &key);
Sebastian Hack's avatar
Sebastian Hack committed
216
217
218
219
220
	}

	return NULL;
}

Michael Beck's avatar
Michael Beck committed
221
222
/**
 * Change the out count
223
224
225
 *
 * @param tgt  the edge target
 * @param kind the kind of the edge
Michael Beck's avatar
Michael Beck committed
226
 */
227
228
static inline void edge_change_cnt(ir_node *tgt, ir_edge_kind_t kind, int ofs)
{
229
	irn_edge_info_t *info = get_irn_edge_info(tgt, kind);
Michael Beck's avatar
Michael Beck committed
230
231
232
	info->out_count += ofs;
}

233
234
235
236
/**
 * Verify the edge list of a node, ie. ensure it's a loop:
 * head -> e_1 -> ... -> e_n -> head
 */
237
static inline void verify_list_head(ir_node *irn, ir_edge_kind_t kind)
238
{
239
240
241
242
	int                    err     = 0;
	int                    num     = 0;
	pset                   *lh_set = pset_new_ptr(16);
	const struct list_head *head   = &get_irn_edge_info(irn, kind)->outs_head;
243
244
245
246
247
248
249
250
251
	const struct list_head *pos;

	list_for_each(pos, head) {
		if (pset_find_ptr(lh_set, pos)) {
			const ir_edge_t *edge = list_entry(pos, ir_edge_t, list);

			ir_fprintf(stderr, "EDGE Verifier: edge list broken (self loop not to head) for %+F:\n", irn);
			fprintf(stderr, "- at list entry %d\n", num);
			if (edge->invalid)
252
				fprintf(stderr, "- edge(%ld) is invalid\n", edge_get_id(edge));
253
			if (edge->src)
254
				ir_fprintf(stderr, "- edge(%ld) %+F(%d)\n", edge_get_id(edge), edge->src, edge->pos);
255
256
257
258
259
260
261
262
263
264
265
266
			err = 1;
			break;
		}
		num++;
		pset_insert_ptr(lh_set, pos);
	}

	del_pset(lh_set);

	assert(err == 0);
}

267
268
269
270
271
272
273
274
275
276
void edges_dump_kind(ir_graph *irg, ir_edge_kind_t kind)
{
	irg_edge_info_t *info;
	ir_edgeset_t    *edges;
	ir_edgeset_iterator_t iter;
	ir_edge_t      *e;

	if (!edges_activated_kind(irg, kind))
		return;

277
	info  = get_irg_edge_info(irg, kind);
278
279
280
281
282
283
	edges = &info->edges;
	foreach_ir_edgeset(edges, e, iter) {
		ir_printf("%+F %d %d\n", e->src, e->pos, e->invalid);
	}
}

284
285
286
void edges_notify_edge_kind(ir_node *src, int pos, ir_node *tgt,
                            ir_node *old_tgt, ir_edge_kind_t kind,
                            ir_graph *irg)
287
{
288
	const char      *msg = "";
289
	irg_edge_info_t *info;
290
291
	ir_edgeset_t    *edges;
	ir_edge_t        templ;
292

Matthias Braun's avatar
Matthias Braun committed
293
	assert(edges_activated_kind(irg, kind));
294
295
296
297

	/*
	 * Only do something, if the old and new target differ.
	 */
298
	if (tgt == old_tgt)
299
300
		return;

301
	info  = get_irg_edge_info(irg, kind);
302
	edges = &info->edges;
303
304

	/* Initialize the edge template to search in the set. */
305
306
	templ.src = src;
	templ.pos = pos;
307

308
309
310
311
312
	/*
	 * If the target is NULL, the edge shall be deleted.
	 */
	if (tgt == NULL) {
		/* search the edge in the set. */
313
		ir_edge_t *edge = ir_edgeset_find(edges, &templ);
314
315
316
317
318

		/* mark the edge invalid if it was found */
		if (edge) {
			msg = "deleting";
			list_del(&edge->list);
319
			ir_edgeset_remove(edges, edge);
320
			list_add(&edge->list, &info->free_edges);
321
322
323
324
			edge->invalid = 1;
			edge->pos = -2;
			edge->src = NULL;
			edge_change_cnt(old_tgt, kind, -1);
325
326
		} else {
			/* If the edge was not found issue a warning on the debug stream */
327
328
			msg = "edge to delete not found!\n";
		}
329
330
331
332
333
334
335
	} else {
		/*
		 * The target is not NULL and the old target differs
		 * from the new target, the edge shall be moved (if the
		 * old target was != NULL) or added (if the old target was
		 * NULL).
		 */
336
		struct list_head *head = &get_irn_edge_info(tgt, kind)->outs_head;
337

338
339
		assert(head->next && head->prev &&
				"target list head must have been initialized");
340

341
342
		/* If the old target is not null, the edge is moved. */
		if (old_tgt) {
343
			ir_edge_t *edge = ir_edgeset_find(edges, &templ);
344
345
			assert(edge && "edge to redirect not found!");
			assert(! edge->invalid && "Invalid edge encountered");
346

347
			msg = "redirecting";
348

349
350
			list_move(&edge->list, head);
			edge_change_cnt(old_tgt, kind, -1);
351
352
		} else {
			/* The old target was NULL, thus, the edge is newly created. */
353
			ir_edge_t *new_edge;
354
355
356
			ir_edge_t *edge;

			if (list_empty(&info->free_edges)) {
357
				edge = (ir_edge_t*)obstack_alloc(&info->edges_obst, EDGE_SIZE);
358
359
360
361
362
363
364
365
366
367
368
369
			} else {
				edge = list_entry(info->free_edges.next, ir_edge_t, list);
				list_del(&edge->list);
			}

			edge->src       = src;
			edge->pos       = pos;
			edge->invalid   = 0;
			edge->present   = 0;
			edge->kind      = kind;
			edge->list.next = NULL;
			edge->list.prev = NULL;
Michael Beck's avatar
Michael Beck committed
370
			memset(edge + 1, 0, edges_private_size);
371
372

			new_edge = ir_edgeset_insert(edges, edge);
373
374
			if (new_edge != edge) {
				panic("new edge exists already");
375
			}
376

377
378
379
			msg = "adding";
			list_add(&edge->list, head);
		}
380

381
		edge_change_cnt(tgt, kind, +1);
382
	}
383

384
#ifndef DEBUG_libfirm
385
386
387
	/* verify list heads */
	if (edges_dbg) {
		if (tgt)
388
			verify_list_head(tgt, kind);
389
		if (old_tgt)
390
			verify_list_head(old_tgt, kind);
391
	}
392
#endif
393

Sebastian Hack's avatar
Sebastian Hack committed
394
	DBG((dbg, LEVEL_5, "announce out edge: %+F %d-> %+F(%+F): %s\n", src, pos, tgt, old_tgt, msg));
395
396
}

Matthias Braun's avatar
Matthias Braun committed
397
398
void edges_notify_edge(ir_node *src, int pos, ir_node *tgt, ir_node *old_tgt,
                       ir_graph *irg)
399
{
400
	if (edges_activated_kind(irg, EDGE_KIND_NORMAL)) {
Matthias Braun's avatar
Matthias Braun committed
401
402
403
		edges_notify_edge_kind(src, pos, tgt, old_tgt, EDGE_KIND_NORMAL, irg);
	}

404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
	if (edges_activated_kind(irg, EDGE_KIND_BLOCK)) {
		if (is_Block(src)) {
			ir_node *bl_old = old_tgt ? get_nodes_block(old_tgt) : NULL;
			ir_node *bl_tgt = NULL;

			if (tgt)
				bl_tgt = is_Bad(tgt) ? tgt : get_nodes_block(tgt);

			edges_notify_edge_kind(src, pos, bl_tgt, bl_old, EDGE_KIND_BLOCK, irg);
		} else if (get_irn_mode(src) == mode_X && old_tgt != NULL && is_Block(old_tgt)) {
			/* moving a jump node from one block to another */
			const ir_edge_t *edge;
			const ir_edge_t *next;
			foreach_out_edge_kind_safe(old_tgt, edge, next, EDGE_KIND_BLOCK) {
				ir_node *succ       = get_edge_src_irn(edge);
419
420
				int      succ_pos   = get_edge_src_pos(edge);
				ir_node *block_pred = get_Block_cfgpred(succ, succ_pos);
421
422
				if (block_pred != src)
					continue;
423
				edges_notify_edge_kind(succ, succ_pos, tgt, old_tgt,
424
425
426
				                       EDGE_KIND_BLOCK, irg);
			}
		}
Sebastian Hack's avatar
Sebastian Hack committed
427
428
429
	}
}

430
431
432
433
434
435
436
/**
 * Delete all in edges of a given kind from the node old.
 *
 * @param old   the node
 * @param kind  the kind of edges to remove
 * @param irg   the irg of the old node
 */
Matthias Braun's avatar
Matthias Braun committed
437
static void edges_node_deleted_kind(ir_node *old, ir_edge_kind_t kind)
Sebastian Hack's avatar
Sebastian Hack committed
438
{
Matthias Braun's avatar
Matthias Braun committed
439
	int i, n;
Matthias Braun's avatar
Matthias Braun committed
440
	ir_graph *irg = get_irn_irg(old);
441

442
	if (!edges_activated_kind(irg, kind))
Matthias Braun's avatar
Matthias Braun committed
443
		return;
444

Matthias Braun's avatar
Matthias Braun committed
445
446
447
448
449
	DBG((dbg, LEVEL_5, "node deleted (kind: %s): %+F\n", get_kind_str(kind), old));

	foreach_tgt(old, i, n, kind) {
		ir_node *old_tgt = get_n(old, i, kind);
		edges_notify_edge_kind(old, i, NULL, old_tgt, kind, irg);
450
451
452
	}
}

Michael Beck's avatar
Michael Beck committed
453
454
455
456
457
458
459
/**
 * A node might be revivaled by CSE. Assure its edges.
 *
 * @param irn   the node
 * @param kind  the kind of edges to remove
 * @param irg   the irg of the old node
 */
Matthias Braun's avatar
Matthias Braun committed
460
static void edges_node_revival_kind(ir_node *irn, ir_edge_kind_t kind)
Michael Beck's avatar
Michael Beck committed
461
462
463
{
	irn_edge_info_t *info;
	int             i, n;
Matthias Braun's avatar
Matthias Braun committed
464
	ir_graph        *irg = get_irn_irg(irn);
Michael Beck's avatar
Michael Beck committed
465
466
467
468

	if (!edges_activated_kind(irg, kind))
		return;

469
	info = get_irn_edge_info(irn, kind);
Michael Beck's avatar
Michael Beck committed
470
471
472
473
474
475
476
477
478
479
480
481
	if (info->edges_built)
		return;

	DBG((dbg, LEVEL_5, "node revivaled (kind: %s): %+F\n", get_kind_str(kind), irn));

	foreach_tgt(irn, i, n, kind) {
		ir_node *tgt = get_n(irn, i, kind);
		edges_notify_edge_kind(irn, i, tgt, NULL, kind, irg);
	}
	info->edges_built = 1;
}

482
typedef struct build_walker {
Sebastian Hack's avatar
Sebastian Hack committed
483
	ir_edge_kind_t kind;
Christian Würdig's avatar
Christian Würdig committed
484
	bitset_t       *reachable;
485
	unsigned       problem_found;
486
} build_walker;
487

Michael Beck's avatar
Michael Beck committed
488
489
490
/**
 * Post-Walker: notify all edges
 */
491
492
static void build_edges_walker(ir_node *irn, void *data)
{
493
	build_walker          *w = (build_walker*)data;
494
495
	int                   i, n;
	ir_edge_kind_t        kind = w->kind;
Matthias Braun's avatar
Matthias Braun committed
496
	ir_graph              *irg = get_irn_irg(irn);
497

498
499
500
501
	foreach_tgt(irn, i, n, kind) {
		ir_node *pred = get_n(irn, i, kind);
		edges_notify_edge_kind(irn, i, pred, NULL, kind, irg);
	}
502
	get_irn_edge_info(irn, kind)->edges_built = 1;
503
504
}

Michael Beck's avatar
Michael Beck committed
505
506
507
508
/**
 * Pre-Walker: initializes the list-heads and set the out-count
 * of all nodes to 0.
 */
509
510
static void init_lh_walker(ir_node *irn, void *data)
{
511
512
	build_walker   *w    = (build_walker*)data;
	ir_edge_kind_t  kind = w->kind;
513
	list_head      *head = &get_irn_edge_info(irn, kind)->outs_head;
514
	INIT_LIST_HEAD(head);
515
516
	get_irn_edge_info(irn, kind)->edges_built = 0;
	get_irn_edge_info(irn, kind)->out_count   = 0;
517
518
519
520
521
522
523
524
525
526
527
528
529
}

/**
 * Pre-Walker: initializes the list-heads and set the out-count
 * of all nodes to 0.
 *
 * Additionally touches DEP nodes, as they might be DEAD.
 * THIS IS UGLY, but I don't find a better way until we
 *
 * a) ensure that dead nodes are not used as input
 * b) it might be sufficient to add those stupid NO_REG nodes
 * to the anchor
 */
530
531
static void init_lh_walker_dep(ir_node *irn, void *data)
{
532
533
	build_walker   *w    = (build_walker*)data;
	ir_edge_kind_t  kind = w->kind;
534
	list_head      *head = &get_irn_edge_info(irn, kind)->outs_head;
535
	int             i;
536
537

	INIT_LIST_HEAD(head);
538
539
	get_irn_edge_info(irn, kind)->edges_built = 0;
	get_irn_edge_info(irn, kind)->out_count   = 0;
540
541
542
543

	for (i = get_irn_deps(irn) - 1; i >= 0; --i) {
		ir_node *dep = get_irn_dep(irn, i);

544
		head = &get_irn_edge_info(dep, kind)->outs_head;
545
546

		INIT_LIST_HEAD(head);
547
548
		get_irn_edge_info(dep, kind)->edges_built = 0;
		get_irn_edge_info(dep, kind)->out_count   = 0;
549
	}
550
551
}

552
553
554
555
556
typedef struct visitor_info_t {
	irg_walk_func *visit;
	void *data;
} visitor_info_t;

557
558
559
560
/**
 * Visitor: initializes the list-heads and set the out-count
 * of all nodes to 0 of nodes that are not seen so far.
 */
561
562
static void visitor(ir_node *irn, void *data)
{
563
	visitor_info_t *info = (visitor_info_t*)data;
564

565
566
	if (is_Deleted(irn))
		return;
567
568
	if (!is_Block(irn) && is_Deleted(get_nodes_block(irn)))
		return;
569

Michael Beck's avatar
Michael Beck committed
570
	if (!irn_visited_else_mark(irn)) {
571
		info->visit(irn, info->data);
572
573
574
	}
}

Sebastian Hack's avatar
Sebastian Hack committed
575
void edges_activate_kind(ir_graph *irg, ir_edge_kind_t kind)
576
{
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
	/*
	 * Build the initial edge set.
	 * Beware, this is not a simple task because it suffers from two
	 * difficulties:
	 * - the anchor set allows access to Nodes that may not be reachable from
	 *   the End node
	 * - the identities add nodes to the "root set" that are not yet reachable
	 *   from End. However, after some transformations, the CSE may revival these
	 *   nodes
	 *
	 * These problems can be fixed using different strategies:
	 * - Add an age flag to every node. Whenever the edge of a node is older
	 *   then the current edge, invalidate the edges of this node.
	 *   While this would help for revivaled nodes, it increases memory and runtime.
	 * - Delete the identities set.
	 *   Solves the revival problem, but may increase the memory consumption, as
	 *   nodes cannot be revivaled at all.
	 * - Manually iterate over the identities root set. This did not consume more memory
	 *   but increase the computation time because the |identities| >= |V|
	 *
	 * Currently, we use the last option.
	 */
Sebastian Hack's avatar
Sebastian Hack committed
599
	struct build_walker w;
600
	irg_edge_info_t     *info = get_irg_edge_info(irg, kind);
601
	visitor_info_t      visit;
Sebastian Hack's avatar
Sebastian Hack committed
602
603

	w.kind = kind;
604

605
606
	visit.data = &w;

607
608
	assert(!info->activated);

609
	info->activated = 1;
Sebastian Hack's avatar
Sebastian Hack committed
610
	edges_init_graph_kind(irg, kind);
611
	if (kind == EDGE_KIND_DEP) {
612
613
614
615
		irg_walk_anchors(irg, init_lh_walker_dep, NULL, &w);
		/* Argh: Dep nodes might be dead, so we MUST visit identities first */
		visit.visit = init_lh_walker_dep;
		visit_all_identities(irg, visitor, &visit);
616
617
		irg_walk_anchors(irg, NULL, build_edges_walker, &w);
	} else {
618
619
		visit.visit = init_lh_walker;
		visit_all_identities(irg, visitor, &visit);
620
		irg_walk_anchors(irg, init_lh_walker, build_edges_walker, &w);
621
	}
622
623
}

Sebastian Hack's avatar
Sebastian Hack committed
624
void edges_deactivate_kind(ir_graph *irg, ir_edge_kind_t kind)
625
{
626
	irg_edge_info_t *info = get_irg_edge_info(irg, kind);
627
628

	info->activated = 0;
629
630
631
632
	if (info->allocated) {
		obstack_free(&info->edges_obst, NULL);
		ir_edgeset_destroy(&info->edges);
		info->allocated = 0;
633
	}
634
	clear_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_OUT_EDGES);
635
636
}

Sebastian Hack's avatar
Sebastian Hack committed
637
int (edges_activated_kind)(const ir_graph *irg, ir_edge_kind_t kind)
638
{
639
	return edges_activated_kind_(irg, kind);
640
641
}

Matthias Braun's avatar
Matthias Braun committed
642
void edges_reroute_kind(ir_node *from, ir_node *to, ir_edge_kind_t kind)
643
{
Matthias Braun's avatar
Matthias Braun committed
644
	ir_graph *irg = get_irn_irg(from);
Sebastian Hack's avatar
Sebastian Hack committed
645
646
	set_edge_func_t *set_edge = edge_kind_info[kind].set_edge;

647
	if (set_edge && edges_activated_kind(irg, kind)) {
648
		struct list_head *head = &get_irn_edge_info(from, kind)->outs_head;
649

Sebastian Hack's avatar
Sebastian Hack committed
650
		DBG((dbg, LEVEL_5, "reroute from %+F to %+F\n", from, to));
651

652
		while (head != head->next) {
653
654
			ir_edge_t *edge = list_entry(head->next, ir_edge_t, list);
			assert(edge->pos >= -1);
Sebastian Hack's avatar
Sebastian Hack committed
655
			set_edge(edge->src, edge->pos, to);
656
657
658
659
660
661
		}
	}
}

static void verify_set_presence(ir_node *irn, void *data)
{
662
	build_walker *w     = (build_walker*)data;
Matthias Braun's avatar
Matthias Braun committed
663
	ir_graph     *irg   = get_irn_irg(irn);
664
	ir_edgeset_t *edges = &get_irg_edge_info(irg, w->kind)->edges;
665
666
	int i, n;

Sebastian Hack's avatar
Sebastian Hack committed
667
668
	foreach_tgt(irn, i, n, w->kind) {
		ir_edge_t templ, *e;
669

Sebastian Hack's avatar
Sebastian Hack committed
670
671
		templ.src = irn;
		templ.pos = i;
672

673
		e = ir_edgeset_find(edges, &templ);
674
		if (e != NULL) {
675
			e->present = 1;
676
		} else {
677
678
			w->problem_found = 1;
		}
679
680
681
682
683
	}
}

static void verify_list_presence(ir_node *irn, void *data)
{
684
685
	build_walker    *w = (build_walker*)data;
	const ir_edge_t *e;
686

Christian Würdig's avatar
Christian Würdig committed
687
688
	bitset_set(w->reachable, get_irn_idx(irn));

689
	/* check list heads */
690
	verify_list_head(irn, w->kind);
691

Sebastian Hack's avatar
Sebastian Hack committed
692
	foreach_out_edge_kind(irn, e, w->kind) {
Christian Würdig's avatar
Christian Würdig committed
693
694
695
696
697
698
699
700
701
		ir_node *tgt;

		if (w->kind == EDGE_KIND_NORMAL && get_irn_arity(e->src) <= e->pos) {
			w->problem_found = 1;
			continue;
		}

		tgt = get_n(e->src, e->pos, w->kind);

702
703
704
		if (irn != tgt) {
			w->problem_found = 1;
		}
705
706
707
	}
}

708
int edges_verify_kind(ir_graph *irg, ir_edge_kind_t kind)
709
{
Sebastian Hack's avatar
Sebastian Hack committed
710
	struct build_walker w;
711
	ir_edgeset_t        *edges = &get_irg_edge_info(irg, kind)->edges;
712
	ir_edge_t           *e;
713
	ir_edgeset_iterator_t  iter;
714

715
	w.kind          = kind;
Christian Würdig's avatar
Christian Würdig committed
716
	w.reachable     = bitset_alloca(get_irg_last_idx(irg));
717
	w.problem_found = 0;
Sebastian Hack's avatar
Sebastian Hack committed
718

719
	/* Clear the present bit in all edges available. */
720
	foreach_ir_edgeset(edges, e, iter) {
721
		e->present = 0;
722
	}
723

Sebastian Hack's avatar
Sebastian Hack committed
724
	irg_walk_graph(irg, verify_set_presence, verify_list_presence, &w);
725
726
727
728
729
730

	/*
	 * Dump all edges which are not invalid and not present.
	 * These edges are superfluous and their presence in the
	 * edge set is wrong.
	 */
731
	foreach_ir_edgeset(edges, e, iter) {
Christian Würdig's avatar
Christian Würdig committed
732
		if (! e->invalid && ! e->present && bitset_is_set(w.reachable, get_irn_idx(e->src))) {
733
			w.problem_found = 1;
734
			ir_fprintf(stderr, "Edge Verifier: edge(%ld) %+F,%d is superfluous\n", edge_get_id(e), e->src, e->pos);
735
736
737
738
739
740
		}
	}

	return w.problem_found;
}

Christian Würdig's avatar
Christian Würdig committed
741
742
#define IGNORE_NODE(irn) (is_Bad((irn)) || is_Block((irn)))

743
744
745
/**
 * Clear link field of all nodes.
 */
746
747
static void clear_links(ir_node *irn, void *env)
{
748
	bitset_t     *bs;
Matthias Braun's avatar
Matthias Braun committed
749
750
	ir_graph     *irg;
	(void) env;
Christian Würdig's avatar
Christian Würdig committed
751

752
753
	if (IGNORE_NODE(irn)) {
		set_irn_link(irn, NULL);
754
		return;
755
	}
756

Matthias Braun's avatar
Matthias Braun committed
757
758
	irg = get_irn_irg(irn);
	bs  = bitset_malloc(get_irg_last_idx(irg));
Christian Würdig's avatar
Christian Würdig committed
759
	set_irn_link(irn, bs);
760
761
762
763
764
}

/**
 * Increases count (stored in link field) for all operands of a node.
 */
765
766
static void count_user(ir_node *irn, void *env)
{
767
	int i;
Christian Würdig's avatar
Christian Würdig committed
768
	int first;
769
	(void) env;
770

771
	first = is_Block(irn) ? 0 : -1;
Christian Würdig's avatar
Christian Würdig committed
772
773
	for (i = get_irn_arity(irn) - 1; i >= first; --i) {
		ir_node  *op = get_irn_n(irn, i);
774
		bitset_t *bs = (bitset_t*)get_irn_link(op);
775

Christian Würdig's avatar
Christian Würdig committed
776
777
		if (bs)
			bitset_set(bs, get_irn_idx(irn));
778
779
780
	}
}

781
782
783
/**
 * Verifies if collected count, number of edges in list and stored edge count are in sync.
 */
784
785
static void verify_edge_counter(ir_node *irn, void *env)
{
786
	build_walker           *w = (build_walker*)env;
Christian Würdig's avatar
Christian Würdig committed
787
788
789
790
	bitset_t               *bs;
	int                    list_cnt;
	int                    ref_cnt;
	int                    edge_cnt;
791
	size_t                 idx;
Christian Würdig's avatar
Christian Würdig committed
792
	const struct list_head *head;
793
	const struct list_head *pos;
Matthias Braun's avatar
Matthias Braun committed
794
	ir_graph               *irg;
795

Christian Würdig's avatar
Christian Würdig committed
796
	if (IGNORE_NODE(irn))
797
798
		return;

799
	bs       = (bitset_t*)get_irn_link(irn);
Christian Würdig's avatar
Christian Würdig committed
800
	list_cnt = 0;
801
	ref_cnt  = 0;
802
803
	edge_cnt = get_irn_edge_info(irn, EDGE_KIND_NORMAL)->out_count;
	head     = &get_irn_edge_info(irn, EDGE_KIND_NORMAL)->outs_head;
Christian Würdig's avatar
Christian Würdig committed
804

805
	/* We can iterate safely here, list heads have already been verified. */
Christian Würdig's avatar
Christian Würdig committed
806
	list_for_each(pos, head) {
807
		++list_cnt;
Christian Würdig's avatar
Christian Würdig committed
808
	}
809

Matthias Braun's avatar
Matthias Braun committed
810
811
	/* check all nodes that reference us and count edges that point number
	 * of ins that actually point to us */
Matthias Braun's avatar
Matthias Braun committed
812
	irg = get_irn_irg(irn);
Matthias Braun's avatar
Matthias Braun committed
813
814
815
	ref_cnt = 0;
	bitset_foreach(bs, idx) {
		int i, arity;
Matthias Braun's avatar
Matthias Braun committed
816
		ir_node *src = get_idx_irn(irg, idx);
Matthias Braun's avatar
Matthias Braun committed
817
818

		arity = get_irn_arity(src);
819
		for (i = 0; i < arity; ++i) {
Matthias Braun's avatar
Matthias Braun committed
820
			ir_node *in = get_irn_n(src, i);
821
822
			if (in == irn)
				++ref_cnt;
Matthias Braun's avatar
Matthias Braun committed
823
824
825
		}
	}

826
827
	if (edge_cnt != list_cnt) {
		w->problem_found = 1;
Christian Würdig's avatar
Christian Würdig committed
828
		ir_fprintf(stderr, "Edge Verifier: edge count is %d, but %d edge(s) are recorded in list at %+F\n",
829
830
831
832
833
			edge_cnt, list_cnt, irn);
	}

	if (ref_cnt != list_cnt) {
		w->problem_found = 1;
Matthias Braun's avatar
Matthias Braun committed
834
		ir_fprintf(stderr, "Edge Verifier: %+F reachable by %d node(s), but the list contains %d edge(s)\n",
835
836
			irn, ref_cnt, list_cnt);
	}
Christian Würdig's avatar
Christian Würdig committed
837
838

	bitset_free(bs);
839
840
}

841
842
int edges_verify(ir_graph *irg)
{
843
	struct build_walker w;
Christian Würdig's avatar
Christian Würdig committed
844
	int    problem_found = 0;
845

Christian Würdig's avatar
Christian Würdig committed
846
847
	/* verify normal edges only */
	problem_found  = edges_verify_kind(irg, EDGE_KIND_NORMAL);
848
849
850
851
852

	w.kind          = EDGE_KIND_NORMAL;
	w.problem_found = 0;

	/* verify counter */
Christian Würdig's avatar
Christian Würdig committed
853
	irg_walk_anchors(irg, clear_links, count_user, &w);
Matthias Braun's avatar
Matthias Braun committed
854
	irg_walk_anchors(irg, NULL, verify_edge_counter, &w);
855
856
857
858

	return problem_found ? 1 : w.problem_found;
}

859
typedef struct pass_t {
860
861
	ir_graph_pass_t pass;
	unsigned        assert_on_problem;
862
} pass_t;
863
864
865
866

/**
 * Wrapper to edges_verify to be run as an ir_graph pass.
 */
867
868
static int edges_verify_wrapper(ir_graph *irg, void *context)
{
869
870
	pass_t *pass           = (pass_t*)context;
	int     problems_found = edges_verify(irg);
871
872
873
874
875
	/* do NOT rerun the pass if verify is ok :-) */
	assert(problems_found && pass->assert_on_problem);
	return 0;
}

876
877
ir_graph_pass_t *irg_verify_edges_pass(const char *name, unsigned assert_on_problem)
{
878
	pass_t *pass = XMALLOCZ(pass_t);
879
880
881
882
883
884
885
886
887
888
889
890

	def_graph_pass_constructor(
		&pass->pass, name ? name : "edges_verify", edges_verify_wrapper);

	/* neither dump nor verify */
	pass->pass.dump_irg   = (DUMP_ON_IRG_FUNC)ir_prog_no_dump;
	pass->pass.verify_irg = (RUN_ON_IRG_FUNC)ir_prog_no_verify;

	pass->assert_on_problem = assert_on_problem;
	return &pass->pass;
}

891
892
void init_edges(void)
{
893
	FIRM_DBG_REGISTER(dbg, DBG_EDGES);
894
895
}

896
897
void edges_init_dbg(int do_dbg)
{
898
899
900
	edges_dbg = do_dbg;
}

901
902
void edges_activate(ir_graph *irg)
{
Sebastian Hack's avatar
Sebastian Hack committed
903
904
	edges_activate_kind(irg, EDGE_KIND_NORMAL);
	edges_activate_kind(irg, EDGE_KIND_BLOCK);
905
906
	if (get_irg_phase_state(irg) == phase_backend)
		edges_activate_kind(irg, EDGE_KIND_DEP);
Sebastian Hack's avatar
Sebastian Hack committed
907
908
}

909
910
void edges_deactivate(ir_graph *irg)
{
911
912
	if (get_irg_phase_state(irg) == phase_backend)
		edges_deactivate_kind(irg, EDGE_KIND_DEP);
Sebastian Hack's avatar
Sebastian Hack committed
913
	edges_deactivate_kind(irg, EDGE_KIND_BLOCK);
914
	edges_deactivate_kind(irg, EDGE_KIND_NORMAL);
Sebastian Hack's avatar
Sebastian Hack committed
915
916
}

917
void assure_edges(ir_graph *irg)
918
{
919
920
	assure_edges_kind(irg, EDGE_KIND_BLOCK);
	assure_edges_kind(irg, EDGE_KIND_NORMAL);
921
	add_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_OUT_EDGES);
Sebastian Hack's avatar
Sebastian Hack committed
922
923
}

924
void assure_edges_kind(ir_graph *irg, ir_edge_kind_t kind)
925
{
926
	if (!edges_activated_kind(irg, kind))
Michael Beck's avatar
Michael Beck committed
927
928
929
		edges_activate_kind(irg, kind);
}

Matthias Braun's avatar
Matthias Braun committed
930
void edges_node_deleted(ir_node *irn)
931
{
Matthias Braun's avatar
Matthias Braun committed
932
933
	edges_node_deleted_kind(irn, EDGE_KIND_NORMAL);
	edges_node_deleted_kind(irn, EDGE_KIND_BLOCK);
Sebastian Hack's avatar
Sebastian Hack committed
934
935
}

Matthias Braun's avatar
Matthias Braun committed
936
void edges_node_revival(ir_node *irn)
937
{
Matthias Braun's avatar
Matthias Braun committed
938
939
	edges_node_revival_kind(irn, EDGE_KIND_NORMAL);
	edges_node_revival_kind(irn, EDGE_KIND_BLOCK);
Michael Beck's avatar
Michael Beck committed
940
}
941

942
943
const ir_edge_t *(get_irn_out_edge_first_kind)(const ir_node *irn, ir_edge_kind_t kind)
{
944
	return get_irn_out_edge_first_kind_(irn, kind);
Sebastian Hack's avatar