1 /*
2  * Copyright (C) 1995-2011 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19 
20 /**
21  * @file
22  * @author      Matthias Braun
23  */
24 #include "config.h"
25 
26 #include <stdbool.h>
27 
28 #include "beinfo.h"
29 #include "bearch.h"
30 #include "benode.h"
31 #include "besched.h"
32 #include "irgwalk.h"
33 #include "irnode_t.h"
34 #include "irdump_t.h"
35 #include "error.h"
36 
37 static copy_attr_func old_phi_copy_attr;
38 
be_info_new_node(ir_graph * irg,ir_node * node)39 void be_info_new_node(ir_graph *irg, ir_node *node)
40 {
41 	struct obstack *obst;
42 	backend_info_t *info;
43 
44 	/* Projs need no be info, all info is fetched from their predecessor */
45 	if (is_Proj(node))
46 		return;
47 
48 	obst = be_get_be_obst(irg);
49 	info = OALLOCZ(obst, backend_info_t);
50 
51 	assert(node->backend_info == NULL);
52 	node->backend_info = info;
53 
54 	/*
55 	 * Set backend info for some middleend nodes which still appear in
56 	 * backend graphs
57 	 */
58 	switch (get_irn_opcode(node)) {
59 	case iro_Block:
60 	case iro_Dummy:
61 	case iro_NoMem:
62 	case iro_Anchor:
63 	case iro_Pin:
64 	case iro_Sync:
65 	case iro_Bad:
66 	case iro_End:
67 	case iro_Unknown:
68 		info->flags |= arch_irn_flags_not_scheduled;
69 		info->out_infos = NEW_ARR_D(reg_out_info_t, obst, 1);
70 		memset(info->out_infos, 0, 1 * sizeof(info->out_infos[0]));
71 		info->out_infos[0].req = arch_no_register_req;
72 		break;
73 	case iro_Phi:
74 		info->out_infos = NEW_ARR_D(reg_out_info_t, obst, 1);
75 		memset(info->out_infos, 0, 1 * sizeof(info->out_infos[0]));
76 		info->out_infos[0].req = arch_no_register_req;
77 		break;
78 	default:
79 		break;
80 	}
81 }
82 
new_phi_copy_attr(ir_graph * irg,const ir_node * old_node,ir_node * new_node)83 static void new_phi_copy_attr(ir_graph *irg, const ir_node *old_node,
84                               ir_node *new_node)
85 {
86 	backend_info_t *old_info = be_get_info(old_node);
87 	backend_info_t *new_info = be_get_info(new_node);
88 
89 	*new_info = *old_info;
90 
91 	old_phi_copy_attr(irg, old_node, new_node);
92 }
93 
be_nodes_equal(const ir_node * node1,const ir_node * node2)94 int be_nodes_equal(const ir_node *node1, const ir_node *node2)
95 {
96 	const backend_info_t *info1 = be_get_info(node1);
97 	const backend_info_t *info2 = be_get_info(node2);
98 	size_t                len   = ARR_LEN(info1->out_infos);
99 	int                   arity = get_irn_arity(node1);
100 	int                   in;
101 	size_t                i;
102 
103 	if (ARR_LEN(info2->out_infos) != len)
104 		return false;
105 
106 	assert(arity == get_irn_arity(node2));
107 
108 	for (in = 0; in < arity; ++in) {
109 		if (info1->in_reqs[in] != info2->in_reqs[in])
110 			return false;
111 	}
112 
113 	for (i = 0; i < len; ++i) {
114 		const reg_out_info_t *out1 = &info1->out_infos[i];
115 		const reg_out_info_t *out2 = &info2->out_infos[i];
116 		if (out1->reg != out2->reg)
117 			return false;
118 		if (!reg_reqs_equal(out1->req, out2->req))
119 			return false;
120 	}
121 
122 	return true;
123 }
124 
init_walker(ir_node * node,void * data)125 static void init_walker(ir_node *node, void *data)
126 {
127 	ir_graph *irg = get_irn_irg(node);
128 	(void) data;
129 	be_info_new_node(irg, node);
130 }
131 
132 static bool initialized = false;
133 
be_info_init(void)134 void be_info_init(void)
135 {
136 	if (initialized)
137 		panic("double initialization of be_info");
138 
139 	old_phi_copy_attr = op_Phi->ops.copy_attr;
140 	op_Phi->ops.copy_attr = new_phi_copy_attr;
141 	initialized = true;
142 
143 	/* phis have register and register requirements now which we want to dump */
144 	assert(op_Phi->ops.dump_node == NULL);
145 	op_Phi->ops.dump_node = be_dump_phi_reg_reqs;
146 }
147 
148 /**
149  * Edge hook to dump the schedule edges.
150  */
sched_edge_hook(FILE * F,const ir_node * irn)151 static void sched_edge_hook(FILE *F, const ir_node *irn)
152 {
153 	if (is_Proj(irn))
154 		return;
155 	if (get_irn_irg(irn)->be_data == NULL)
156 		return;
157 
158 	if (sched_is_scheduled(irn) && sched_has_prev(irn) && !is_Block(irn)) {
159 		ir_node *prev = sched_prev(irn);
160 		fprintf(F, "edge:{sourcename: ");
161 		print_nodeid(F, irn);
162 		fprintf(F, " targetname: ");
163 		print_nodeid(F, prev);
164 		fprintf(F, " color:magenta}\n");
165 	}
166 }
167 
be_info_init_irg(ir_graph * irg)168 void be_info_init_irg(ir_graph *irg)
169 {
170 	irg_walk_anchors(irg, init_walker, NULL, NULL);
171 
172 	set_dump_node_edge_hook(sched_edge_hook);
173 }
174 
be_info_free(void)175 void be_info_free(void)
176 {
177 	if (!initialized)
178 		panic("called without prior init");
179 
180 	assert(op_Phi->ops.copy_attr == new_phi_copy_attr);
181 	op_Phi->ops.copy_attr = old_phi_copy_attr;
182 	initialized = false;
183 
184 	assert(op_Phi->ops.dump_node == be_dump_phi_reg_reqs);
185 	op_Phi->ops.dump_node = NULL;
186 }
187 
be_info_initialized(const ir_graph * irg)188 int be_info_initialized(const ir_graph *irg)
189 {
190 	(void) irg;
191 	return initialized;
192 }
193