rm_tuples.c 1.4 KB
Newer Older
Andreas Zwinkau's avatar
Andreas Zwinkau committed
1
2
/*
 * This file is part of libFirm.
3
 * Copyright (C) 2012 Karlsruhe Institute of Technology.
Andreas Zwinkau's avatar
Andreas Zwinkau committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 */

/**
 * @brief    Remove all Tuple nodes from ir graph
 * @author   Andreas Zwinkau
 */
#include "irnode_t.h"
#include "irgopt.h"
#include "irgmod.h"
#include "irgwalk.h"
#include "irgopt.h"

/** Transforms:
 *    a
 *    |
 *   Tuple
 *    |        =>
 *   Proj x          a
 */
static void exchange_tuple_projs(ir_node *node, void *env)
{
25
	bool *changed = (bool*)env;
26
27
	if (!is_Proj(node))
		return;
Andreas Zwinkau's avatar
Andreas Zwinkau committed
28

29
30
31
	/* Handle Tuple(Tuple,...) case. */
	exchange_tuple_projs(get_Proj_pred(node), env);

32
33
34
	ir_node *pred = get_Proj_pred(node);
	if (!is_Tuple(pred))
		return;
Andreas Zwinkau's avatar
Andreas Zwinkau committed
35

36
	unsigned pn         = get_Proj_num(node);
37
38
	ir_node *tuple_pred = get_Tuple_pred(pred, pn);
	exchange(node, tuple_pred);
39
	*changed = true;
Andreas Zwinkau's avatar
Andreas Zwinkau committed
40
41
}

42
void remove_tuples(ir_graph *irg)
Andreas Zwinkau's avatar
Andreas Zwinkau committed
43
{
44
45
	bool changed = false;
	irg_walk_graph(irg, exchange_tuple_projs, NULL, &changed);
yb9976's avatar
yb9976 committed
46

47
48
49
	/* remove Tuples only held by keep-alive edges */
	ir_node *end = get_irg_end(irg);
	for (int i = get_End_n_keepalives(end); i-- > 0; ) {
yb9976's avatar
yb9976 committed
50
		ir_node *irn = get_End_keepalive(end, i);
51
		if (is_Tuple(irn)) {
52
			remove_End_n(end, i);
53
54
			changed = true;
		}
yb9976's avatar
yb9976 committed
55
56
	}

57
58
59
60
	confirm_irg_properties(irg, changed
			? IR_GRAPH_PROPERTIES_CONTROL_FLOW | IR_GRAPH_PROPERTY_ONE_RETURN
			  | IR_GRAPH_PROPERTY_MANY_RETURNS | IR_GRAPH_PROPERTY_NO_BADS
			: IR_GRAPH_PROPERTIES_ALL);
61
	add_irg_properties(irg, IR_GRAPH_PROPERTY_NO_TUPLES);
Andreas Zwinkau's avatar
Andreas Zwinkau committed
62
}