warning fixes and use of attribute copy function
[libfirm] / ir / be / beinsn.c
1 /*
2  * Copyright (C) 1995-2007 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         const arch_env_t *arch_env = env->aenv;
50         struct obstack *obst       = env->obst;
51         int i, n;
52
53         for (i = 0, n = get_irn_arity(mach_op); i < n; ++i) {
54                 ir_node *op = get_irn_n(mach_op, i);
55
56                 if (is_irn_machine_operand(op)) {
57                         add_machine_operands(env, insn, op);
58                 } else if (arch_irn_consider_in_reg_alloc(arch_env, env->cls, op)) {
59                         be_operand_t o;
60
61                         /* found a register use, create an operand */
62                         o.req     = arch_get_register_req(arch_env, mach_op, i);
63                         o.carrier = op;
64                         o.irn     = insn->irn;
65                         o.pos     = i;
66                         o.partner = NULL;
67                         o.has_constraints = arch_register_req_is(o.req, limited);
68                         obstack_grow(obst, &o, sizeof(o));
69                         insn->n_ops++;
70                         insn->in_constraints |= o.has_constraints;
71                 }
72         }
73 }
74
75 /**
76  * Create a be_insn_t for an IR node.
77  *
78  * @param env      the insn construction environment
79  * @param irn      the irn for which the be_insn should be build
80  *
81  * @return the be_insn for the IR node
82  */
83 be_insn_t *be_scan_insn(const be_insn_env_t *env, ir_node *irn)
84 {
85         const arch_env_t *arch_env = env->aenv;
86         struct obstack *obst       = env->obst;
87         be_operand_t o;
88         be_insn_t *insn;
89         int i, n;
90         int pre_colored = 0;
91
92         insn = obstack_alloc(obst, sizeof(insn[0]));
93         memset(insn, 0, sizeof(insn[0]));
94
95         insn->irn       = irn;
96         insn->next_insn = sched_next(irn);
97         if (get_irn_mode(irn) == mode_T) {
98                 const ir_edge_t *edge;
99                 ir_node *p;
100
101                 /* This instruction might create more than one def. These are handled
102                    by Proj's, find them. */
103                 foreach_out_edge(irn, edge) {
104                         p = get_edge_src_irn(edge);
105
106                         /* did not work if the result is a ProjT. This should NOT happen
107                            in the backend, but check it for now. */
108                         assert(get_irn_mode(p) != mode_T);
109
110                         if (arch_irn_consider_in_reg_alloc(arch_env, env->cls, p)) {
111                                 /* found a def: create a new operand */
112                                 o.req             = arch_get_register_req(arch_env, p, -1);
113                                 o.carrier         = p;
114                                 o.irn             = irn;
115                                 o.pos             = -(get_Proj_proj(p) + 1);
116                                 o.partner         = NULL;
117                                 o.has_constraints = arch_register_req_is(o.req, limited);
118                                 obstack_grow(obst, &o, sizeof(o));
119                                 insn->n_ops++;
120                                 insn->out_constraints |= o.has_constraints;
121                                 pre_colored += arch_get_irn_register(arch_env, p) != NULL;
122                         }
123                 }
124
125                 /* FIXME: Until yet, Proj's are scheduled, so we need to find the first
126                    non-Proj instruction in the schedule */
127                 for (p = sched_next(irn); is_Proj(p); p = sched_next(p));
128                 insn->next_insn = p;
129
130         } else if (arch_irn_consider_in_reg_alloc(arch_env, env->cls, irn)) {
131                 /* only one def, create one operand */
132                 o.req     = arch_get_register_req(arch_env, irn, -1);
133                 o.carrier = irn;
134                 o.irn     = irn;
135                 o.pos     = -1;
136                 o.partner = NULL;
137                 o.has_constraints = arch_register_req_is(o.req, limited);
138                 obstack_grow(obst, &o, sizeof(o));
139                 insn->n_ops++;
140                 insn->out_constraints |= o.has_constraints;
141                 pre_colored += arch_get_irn_register(arch_env, irn) != NULL;
142         }
143
144         if (pre_colored > 0) {
145                 assert(pre_colored == insn->n_ops && "partly pre-colored nodes not supported");
146                 insn->pre_colored = 1;
147         }
148         insn->use_start   = insn->n_ops;
149
150         /* now collect the uses for this node */
151         for (i = 0, n = get_irn_arity(irn); i < n; ++i) {
152                 ir_node *op = get_irn_n(irn, i);
153
154                 if (is_irn_machine_operand(op)) {
155                         add_machine_operands(env, insn, op);
156                 } else if (arch_irn_consider_in_reg_alloc(arch_env, env->cls, op)) {
157                         /* found a register use, create an operand */
158                         o.req     = arch_get_register_req(arch_env, irn, i);
159                         o.carrier = op;
160                         o.irn     = irn;
161                         o.pos     = i;
162                         o.partner = NULL;
163                         o.has_constraints = arch_register_req_is(o.req, limited);
164                         obstack_grow(obst, &o, sizeof(o));
165                         insn->n_ops++;
166                         insn->in_constraints |= o.has_constraints;
167                 }
168         }
169
170         insn->has_constraints = insn->in_constraints | insn->out_constraints;
171         insn->ops = obstack_finish(obst);
172
173         /* Compute the admissible registers bitsets. */
174         for (i = 0; i < insn->n_ops; ++i) {
175                 be_operand_t *op = &insn->ops[i];
176                 const arch_register_req_t   *req = op->req;
177                 const arch_register_class_t *cls = req->cls;
178                 arch_register_req_type_t    type = req->type;
179
180                 /* If there is no special requirement, we allow current class here */
181                 if (cls == NULL && req->type == arch_register_req_type_none) {
182                         cls  = env->cls;
183                         type = arch_register_req_type_normal;
184                 }
185
186                 assert(cls == env->cls);
187
188                 op->regs = bitset_obstack_alloc(obst, env->cls->n_regs);
189
190                 if (type & arch_register_req_type_limited) {
191                         rbitset_copy_to_bitset(req->limited, op->regs);
192                 } else {
193                         arch_put_non_ignore_regs(arch_env, env->cls, op->regs);
194                         if (env->ignore_colors)
195                                 bitset_andnot(op->regs, env->ignore_colors);
196                 }
197         }
198
199         return insn;
200 }
201
202 be_insn_env_t *be_insn_env_init(be_insn_env_t *ie, const be_irg_t *birg,
203                                 const arch_register_class_t *cls,
204                                 struct obstack *obst)
205 {
206         ie->aenv = be_get_birg_arch_env(birg);
207         ie->cls  = cls;
208         ie->obst = obst;
209         ie->ignore_colors = bitset_obstack_alloc(obst, cls->n_regs);
210         be_abi_put_ignore_regs(birg->abi, cls, ie->ignore_colors);
211
212         return ie;
213 }