summaryrefslogtreecommitdiffhomepage
path: root/ir/opt/opt_frame.c
blob: 67d9b4b6194253c3aa3b1cd03b082947afb5fae7 (plain)
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
39
40
41
42
43
44
45
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
/*
 * This file is part of libFirm.
 * Copyright (C) 2012 University of Karlsruhe.
 */

/**
 * @file
 * @brief   Optimize the frame type.
 * @date    15.03.2006
 * @author  Michael Beck
 * @brief
 *   Optimize the frame type by removing unused type members.
 */
#include "iredges_t.h"
#include "irgraph_t.h"
#include "iroptimize.h"
#include "irouts_t.h"
#include "type_t.h"

/*
 * Optimize the frame type of an irg by removing
 * never touched entities.
 */
void opt_frame_irg(ir_graph *irg)
{
	ir_type *frame_tp = get_irg_frame_type(irg);
	size_t   n        = get_compound_n_members(frame_tp);
	if (n <= 0)
		return;

	assure_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_OUTS);
	irp_reserve_resources(irp, IRP_RESOURCE_ENTITY_LINK);

	/* clear all entity links */
	for (size_t i = n; i-- > 0;) {
		ir_entity *entity = get_compound_member(frame_tp, i);
		set_entity_link(entity, NULL);
	}

	/* mark all used entities */
	ir_node *frame = get_irg_frame(irg);
	foreach_irn_out_r(frame, o, sel) {
		if (is_Member(sel)) {
			ir_entity *entity = get_Member_entity(sel);
			/* only entities on the frame */
			if (get_entity_owner(entity) == frame_tp)
				set_entity_link(entity, entity);
		}
	}

	/* link unused ones */
	ir_entity *list = NULL;
	for (size_t i = n; i-- > 0;) {
		ir_entity *entity = get_compound_member(frame_tp, i);
		if (get_entity_link(entity) == NULL) {
			set_entity_link(entity, list);
			list = entity;
		}
	}

	if (list != NULL) {
		/* delete list members */
		for (ir_entity *entity = list, *next; entity != NULL; entity = next) {
			next = (ir_entity*)get_entity_link(entity);
			free_entity(entity);
		}
		/* we changed the frame type, its layout should be redefined */
		set_type_state(frame_tp, layout_undefined);
	}
	irp_free_resources(irp, IRP_RESOURCE_ENTITY_LINK);

	/* we changed the type, this affects none of the currently known graph
	 * properties, but I don't use ALL because I don't know if someone adds
	 * type-based properties at some point */
	confirm_irg_properties(irg,
		IR_GRAPH_PROPERTIES_CONTROL_FLOW
		| IR_GRAPH_PROPERTY_NO_BADS
		| IR_GRAPH_PROPERTY_NO_TUPLES
		| IR_GRAPH_PROPERTY_CONSISTENT_OUT_EDGES
		| IR_GRAPH_PROPERTY_CONSISTENT_OUTS
		| IR_GRAPH_PROPERTY_CONSISTENT_ENTITY_USAGE
		| IR_GRAPH_PROPERTY_MANY_RETURNS);
}