1d489b62af9fd0031ae52094b9e9c447fd163466
[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  */
25 #include "config.h"
26
27 #include "irgraph_t.h"
28 #include "irmode_t.h"
29 #include "irnode_t.h"
30 #include "iredges.h"
31
32 #include "besched.h"
33 #include "beinsn_t.h"
34 #include "beirg.h"
35 #include "beabi.h"
36 #include "raw_bitset.h"
37
38 /**
39  * Create a be_insn_t for an IR node.
40  *
41  * @param env      the insn construction environment
42  * @param irn      the irn for which the be_insn should be build
43  *
44  * @return the be_insn for the IR node
45  */
46 be_insn_t *be_scan_insn(const be_insn_env_t *env, ir_node *irn)
47 {
48         struct obstack *obst = env->obst;
49         be_operand_t o;
50         int i, n;
51         int pre_colored = 0;
52
53         be_insn_t *insn = OALLOCZ(obst, be_insn_t);
54
55         insn->irn       = irn;
56         insn->next_insn = sched_next(irn);
57         if (get_irn_mode(irn) == mode_T) {
58                 ir_node *p;
59
60                 /* This instruction might create more than one def. These are handled
61                    by Proj's, find them. */
62                 foreach_out_edge(irn, edge) {
63                         p = get_edge_src_irn(edge);
64
65                         /* did not work if the result is a ProjT. This should NOT happen
66                            in the backend, but check it for now. */
67                         assert(get_irn_mode(p) != mode_T);
68
69                         if (arch_irn_consider_in_reg_alloc(env->cls, p)) {
70                                 /* found a def: create a new operand */
71                                 o.req             = arch_get_irn_register_req(p);
72                                 o.carrier         = p;
73                                 o.irn             = irn;
74                                 o.pos             = -(get_Proj_proj(p) + 1);
75                                 o.partner         = NULL;
76                                 o.has_constraints = arch_register_req_is(o.req, limited) | (o.req->width > 1);
77                                 obstack_grow(obst, &o, sizeof(o));
78                                 insn->n_ops++;
79                                 insn->out_constraints |= o.has_constraints;
80                                 pre_colored += arch_get_irn_register(p) != NULL;
81                         }
82                 }
83         } else if (arch_irn_consider_in_reg_alloc(env->cls, irn)) {
84                 /* only one def, create one operand */
85                 o.req     = arch_get_irn_register_req(irn);
86                 o.carrier = irn;
87                 o.irn     = irn;
88                 o.pos     = -1;
89                 o.partner = NULL;
90                 o.has_constraints = arch_register_req_is(o.req, limited) | (o.req->width > 1);
91                 obstack_grow(obst, &o, sizeof(o));
92                 insn->n_ops++;
93                 insn->out_constraints |= o.has_constraints;
94                 pre_colored += arch_get_irn_register(irn) != NULL;
95         }
96
97         if (pre_colored > 0) {
98                 assert(pre_colored == insn->n_ops && "partly pre-colored nodes not supported");
99                 insn->pre_colored = 1;
100         }
101         insn->use_start   = insn->n_ops;
102
103         /* now collect the uses for this node */
104         for (i = 0, n = get_irn_arity(irn); i < n; ++i) {
105                 ir_node *op = get_irn_n(irn, i);
106
107                 if (arch_irn_consider_in_reg_alloc(env->cls, op)) {
108                         /* found a register use, create an operand */
109                         o.req     = arch_get_irn_register_req_in(irn, i);
110                         o.carrier = op;
111                         o.irn     = irn;
112                         o.pos     = i;
113                         o.partner = NULL;
114                         o.has_constraints = arch_register_req_is(o.req, limited);
115                         obstack_grow(obst, &o, sizeof(o));
116                         insn->n_ops++;
117                         insn->in_constraints |= o.has_constraints;
118                 }
119         }
120
121         insn->has_constraints = insn->in_constraints | insn->out_constraints;
122         insn->ops = (be_operand_t*)obstack_finish(obst);
123
124         /* Compute the admissible registers bitsets. */
125         for (i = 0; i < insn->n_ops; ++i) {
126                 be_operand_t *op = &insn->ops[i];
127                 const arch_register_req_t   *req = op->req;
128                 const arch_register_class_t *cls = req->cls;
129                 arch_register_req_type_t    type = req->type;
130
131                 /* If there is no special requirement, we allow current class here */
132                 if (cls == NULL && req->type == arch_register_req_type_none) {
133                         cls  = env->cls;
134                         type = arch_register_req_type_normal;
135                 }
136
137                 assert(cls == env->cls);
138
139                 if (type & arch_register_req_type_limited) {
140                         bitset_t *regs = bitset_obstack_alloc(obst, env->cls->n_regs);
141                         rbitset_copy_to_bitset(req->limited, regs);
142                         op->regs = regs;
143                 } else {
144                         op->regs = env->allocatable_regs;
145                 }
146         }
147
148         return insn;
149 }