a3f305da8b3f423893d17b0f5eb6afe940ee9cff
[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
45         be_insn_t *insn = OALLOCZ(obst, be_insn_t);
46
47         insn->irn = irn;
48         if (get_irn_mode(irn) == mode_T) {
49                 ir_node *p;
50
51                 /* This instruction might create more than one def. These are handled
52                    by Proj's, find them. */
53                 foreach_out_edge(irn, edge) {
54                         p = get_edge_src_irn(edge);
55
56                         /* did not work if the result is a ProjT. This should NOT happen
57                            in the backend, but check it for now. */
58                         assert(get_irn_mode(p) != mode_T);
59
60                         if (arch_irn_consider_in_reg_alloc(env->cls, p)) {
61                                 /* found a def: create a new operand */
62                                 o.req             = arch_get_irn_register_req(p);
63                                 o.carrier         = p;
64                                 o.irn             = irn;
65                                 o.pos             = -(get_Proj_proj(p) + 1);
66                                 o.partner         = NULL;
67                                 o.has_constraints = arch_register_req_is(o.req, limited) | (o.req->width > 1);
68                                 obstack_grow(obst, &o, sizeof(o));
69                                 insn->n_ops++;
70                                 insn->has_constraints |= o.has_constraints;
71                         }
72                 }
73         } else if (arch_irn_consider_in_reg_alloc(env->cls, irn)) {
74                 /* only one def, create one operand */
75                 o.req     = arch_get_irn_register_req(irn);
76                 o.carrier = irn;
77                 o.irn     = irn;
78                 o.pos     = -1;
79                 o.partner = NULL;
80                 o.has_constraints = arch_register_req_is(o.req, limited) | (o.req->width > 1);
81                 obstack_grow(obst, &o, sizeof(o));
82                 insn->n_ops++;
83                 insn->has_constraints |= o.has_constraints;
84         }
85
86         insn->use_start = insn->n_ops;
87
88         /* now collect the uses for this node */
89         for (i = 0, n = get_irn_arity(irn); i < n; ++i) {
90                 ir_node *op = get_irn_n(irn, i);
91
92                 if (arch_irn_consider_in_reg_alloc(env->cls, op)) {
93                         /* found a register use, create an operand */
94                         o.req     = arch_get_irn_register_req_in(irn, i);
95                         o.carrier = op;
96                         o.irn     = irn;
97                         o.pos     = i;
98                         o.partner = NULL;
99                         o.has_constraints = arch_register_req_is(o.req, limited);
100                         obstack_grow(obst, &o, sizeof(o));
101                         insn->n_ops++;
102                         insn->has_constraints |= o.has_constraints;
103                 }
104         }
105
106         insn->ops = (be_operand_t*)obstack_finish(obst);
107
108         /* Compute the admissible registers bitsets. */
109         for (i = 0; i < insn->n_ops; ++i) {
110                 be_operand_t              *const op  = &insn->ops[i];
111                 arch_register_req_t const *const req = op->req;
112                 assert(req->cls == env->cls);
113
114                 if (req->type & arch_register_req_type_limited) {
115                         bitset_t *regs = bitset_obstack_alloc(obst, env->cls->n_regs);
116                         rbitset_copy_to_bitset(req->limited, regs);
117                         op->regs = regs;
118                 } else {
119                         op->regs = env->allocatable_regs;
120                 }
121         }
122
123         return insn;
124 }