Remove the unused attribute const arch_env_t *arch_env from struct be_insn_env_t.
[libfirm] / ir / be / beinsn.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  * @brief       A data structure to treat nodes and node-proj collections uniformly.
23  * @author      Sebastian Hack
24  * @version     $Id$
25  */
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include "irgraph_t.h"
31 #include "irmode_t.h"
32 #include "irnode_t.h"
33 #include "iredges.h"
34
35 #include "besched_t.h"
36 #include "beinsn_t.h"
37 #include "beirg_t.h"
38 #include "beabi.h"
39 #include "raw_bitset.h"
40
41 /**
42  * Add machine operands to the instruction uses.
43  *
44  * @param env      the insn construction environment
45  * @param insn     the be_insn that is build
46  * @param mach_op  the machine operand for which uses are added
47  */
48 static void add_machine_operands(const be_insn_env_t *env, be_insn_t *insn, ir_node *mach_op) {
49         struct obstack *obst = env->obst;
50         int i, n;
51
52         for (i = 0, n = get_irn_arity(mach_op); i < n; ++i) {
53                 ir_node *op = get_irn_n(mach_op, i);
54
55                 if (is_irn_machine_operand(op)) {
56                         add_machine_operands(env, insn, op);
57                 } else if (arch_irn_consider_in_reg_alloc(env->cls, op)) {
58                         be_operand_t o;
59
60                         /* found a register use, create an operand */
61                         o.req     = arch_get_register_req(mach_op, i);
62                         o.carrier = op;
63                         o.irn     = insn->irn;
64                         o.pos     = i;
65                         o.partner = NULL;
66                         o.has_constraints = arch_register_req_is(o.req, limited);
67                         obstack_grow(obst, &o, sizeof(o));
68                         insn->n_ops++;
69                         insn->in_constraints |= o.has_constraints;
70                 }
71         }
72 }
73
74 /**
75  * Create a be_insn_t for an IR node.
76  *
77  * @param env      the insn construction environment
78  * @param irn      the irn for which the be_insn should be build
79  *
80  * @return the be_insn for the IR node
81  */
82 be_insn_t *be_scan_insn(const be_insn_env_t *env, ir_node *irn)
83 {
84         struct obstack *obst = env->obst;
85         be_operand_t o;
86         be_insn_t *insn;
87         int i, n;
88         int pre_colored = 0;
89
90         insn = obstack_alloc(obst, sizeof(insn[0]));
91         memset(insn, 0, sizeof(insn[0]));
92
93         insn->irn       = irn;
94         insn->next_insn = sched_next(irn);
95         if (get_irn_mode(irn) == mode_T) {
96                 const ir_edge_t *edge;
97                 ir_node *p;
98
99                 /* This instruction might create more than one def. These are handled
100                    by Proj's, find them. */
101                 foreach_out_edge(irn, edge) {
102                         p = get_edge_src_irn(edge);
103
104                         /* did not work if the result is a ProjT. This should NOT happen
105                            in the backend, but check it for now. */
106                         assert(get_irn_mode(p) != mode_T);
107
108                         if (arch_irn_consider_in_reg_alloc(env->cls, p)) {
109                                 /* found a def: create a new operand */
110                                 o.req             = arch_get_register_req(p, -1);
111                                 o.carrier         = p;
112                                 o.irn             = irn;
113                                 o.pos             = -(get_Proj_proj(p) + 1);
114                                 o.partner         = NULL;
115                                 o.has_constraints = arch_register_req_is(o.req, limited);
116                                 obstack_grow(obst, &o, sizeof(o));
117                                 insn->n_ops++;
118                                 insn->out_constraints |= o.has_constraints;
119                                 pre_colored += arch_get_irn_register(p) != NULL;
120                         }
121                 }
122         } else if (arch_irn_consider_in_reg_alloc(env->cls, irn)) {
123                 /* only one def, create one operand */
124                 o.req     = arch_get_register_req(irn, -1);
125                 o.carrier = irn;
126                 o.irn     = irn;
127                 o.pos     = -1;
128                 o.partner = NULL;
129                 o.has_constraints = arch_register_req_is(o.req, limited);
130                 obstack_grow(obst, &o, sizeof(o));
131                 insn->n_ops++;
132                 insn->out_constraints |= o.has_constraints;
133                 pre_colored += arch_get_irn_register(irn) != NULL;
134         }
135
136         if (pre_colored > 0) {
137                 assert(pre_colored == insn->n_ops && "partly pre-colored nodes not supported");
138                 insn->pre_colored = 1;
139         }
140         insn->use_start   = insn->n_ops;
141
142         /* now collect the uses for this node */
143         for (i = 0, n = get_irn_arity(irn); i < n; ++i) {
144                 ir_node *op = get_irn_n(irn, i);
145
146                 if (is_irn_machine_operand(op)) {
147                         add_machine_operands(env, insn, op);
148                 } else if (arch_irn_consider_in_reg_alloc(env->cls, op)) {
149                         /* found a register use, create an operand */
150                         o.req     = arch_get_register_req(irn, i);
151                         o.carrier = op;
152                         o.irn     = irn;
153                         o.pos     = i;
154                         o.partner = NULL;
155                         o.has_constraints = arch_register_req_is(o.req, limited);
156                         obstack_grow(obst, &o, sizeof(o));
157                         insn->n_ops++;
158                         insn->in_constraints |= o.has_constraints;
159                 }
160         }
161
162         insn->has_constraints = insn->in_constraints | insn->out_constraints;
163         insn->ops = obstack_finish(obst);
164
165         /* Compute the admissible registers bitsets. */
166         for (i = 0; i < insn->n_ops; ++i) {
167                 be_operand_t *op = &insn->ops[i];
168                 const arch_register_req_t   *req = op->req;
169                 const arch_register_class_t *cls = req->cls;
170                 arch_register_req_type_t    type = req->type;
171
172                 /* If there is no special requirement, we allow current class here */
173                 if (cls == NULL && req->type == arch_register_req_type_none) {
174                         cls  = env->cls;
175                         type = arch_register_req_type_normal;
176                 }
177
178                 assert(cls == env->cls);
179
180                 op->regs = bitset_obstack_alloc(obst, env->cls->n_regs);
181
182                 if (type & arch_register_req_type_limited) {
183                         rbitset_copy_to_bitset(req->limited, op->regs);
184                 } else {
185                         arch_put_non_ignore_regs(env->cls, op->regs);
186                         if (env->ignore_colors)
187                                 bitset_andnot(op->regs, env->ignore_colors);
188                 }
189         }
190
191         return insn;
192 }
193
194 be_insn_env_t *be_insn_env_init(be_insn_env_t *ie, const be_irg_t *birg,
195                                 const arch_register_class_t *cls,
196                                 struct obstack *obst)
197 {
198         ie->cls  = cls;
199         ie->obst = obst;
200         ie->ignore_colors = bitset_obstack_alloc(obst, cls->n_regs);
201         be_abi_put_ignore_regs(birg->abi, cls, ie->ignore_colors);
202
203         return ie;
204 }