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