clarify comment
[libfirm] / ir / be / beinfo.c
1 /*
2  * Copyright (C) 1995-2008 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  * @version     $Id$
24  */
25 #include "config.h"
26
27 #include <stdbool.h>
28
29 #include "beinfo.h"
30 #include "bearch.h"
31 #include "benode_t.h"
32 #include "irgwalk.h"
33 #include "irnode_t.h"
34 #include "error.h"
35
36 static copy_attr_func  old_phi_copy_attr;
37
38 void be_info_new_node(ir_node *node)
39 {
40         struct obstack *obst;
41         backend_info_t *info;
42         sched_info_t   *sinfo;
43
44         if (is_Anchor(node))
45                 return;
46
47         if (is_Proj(node)) // Projs need no be info, their tuple holds all information
48                 return;
49
50         obst  = get_irg_obstack(current_ir_graph);
51         info  = obstack_alloc(obst, sizeof(*info));
52         sinfo = &info->sched_info;
53
54         memset(info, 0, sizeof(*info));
55         sinfo->next = NULL;
56         sinfo->prev = NULL;
57
58         if (is_Phi(node)) {
59                 info->out_infos = NEW_ARR_D(reg_out_info_t, obst, 1);
60                 memset(info->out_infos, 0, 1 * sizeof(info->out_infos[0]));
61         }
62         assert(node->backend_info == NULL);
63         node->backend_info = info;
64 }
65
66 static void new_Phi_copy_attr(const ir_node *old_node, ir_node *new_node)
67 {
68         struct obstack *obst  = get_irg_obstack(get_irn_irg(new_node));
69         backend_info_t *old_info = be_get_info(old_node);
70         backend_info_t *new_info = be_get_info(new_node);
71
72         old_phi_copy_attr(old_node, new_node);
73         new_info->out_infos = DUP_ARR_D(reg_out_info_t, obst, old_info->out_infos);
74 }
75
76 int be_info_equal(const ir_node *node1, const ir_node *node2)
77 {
78         backend_info_t *info1 = be_get_info(node1);
79         backend_info_t *info2 = be_get_info(node2);
80         int             len   = ARR_LEN(info1->out_infos);
81         int             i;
82
83         if (ARR_LEN(info2->out_infos) != len)
84                 return 0;
85
86         for (i = 0; i < len; ++i) {
87                 const reg_out_info_t *out1 = &info1->out_infos[i];
88                 const reg_out_info_t *out2 = &info2->out_infos[i];
89                 if (out1->reg != out2->reg)
90                         return 0;
91                 if (!reg_reqs_equal(out1->req, out2->req))
92                         return 0;
93         }
94
95         /* TODO: in reqs */
96
97         return 1;
98 }
99
100 static void init_walker(ir_node *node, void *data)
101 {
102         (void) data;
103         be_info_new_node(node);
104 }
105
106 static bool initialized = false;
107
108 void be_info_init(void)
109 {
110         if (initialized)
111                 panic("double initialization of be_info");
112
113         old_phi_copy_attr = op_Phi->ops.copy_attr;
114         op_Phi->ops.copy_attr = new_Phi_copy_attr;
115         initialized = true;
116
117         /* phis have register and register requirements now which we want to dump */
118         assert(op_Phi->ops.dump_node == NULL);
119         op_Phi->ops.dump_node = be_dump_phi_reg_reqs;
120 }
121
122 void be_info_init_irg(ir_graph *irg)
123 {
124         irg_walk_anchors(irg, init_walker, NULL, NULL);
125 }
126
127 void be_info_free(void)
128 {
129         if (!initialized)
130                 panic("be_info_free called without prior init");
131
132         assert(op_Phi->ops.copy_attr == new_Phi_copy_attr);
133         op_Phi->ops.copy_attr = old_phi_copy_attr;
134         initialized = false;
135
136         assert(op_Phi->ops.dump_node == be_dump_phi_reg_reqs);
137         op_Phi->ops.dump_node = NULL;
138 }
139
140 int be_info_initialized(const ir_graph *irg)
141 {
142         (void) irg;
143         return initialized;
144 }