updated Header
[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 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include "irgraph_t.h"
25 #include "irmode_t.h"
26 #include "irnode_t.h"
27
28 #include "besched_t.h"
29 #include "beinsn_t.h"
30 #include "beirg_t.h"
31 #include "beabi.h"
32 #include "raw_bitset.h"
33
34 be_insn_t *be_scan_insn(const be_insn_env_t *env, ir_node *irn)
35 {
36         const arch_env_t *arch_env = env->aenv;
37         struct obstack *obst       = env->obst;
38         be_operand_t o;
39         be_insn_t *insn;
40         int i, n;
41         int pre_colored = 0;
42
43         insn = obstack_alloc(obst, sizeof(insn[0]));
44         memset(insn, 0, sizeof(insn[0]));
45
46         insn->irn       = irn;
47         insn->next_insn = sched_next(irn);
48         if(get_irn_mode(irn) == mode_T) {
49                 ir_node *p;
50
51                 for(p = sched_next(irn); is_Proj(p); p = sched_next(p)) {
52                         if(arch_irn_consider_in_reg_alloc(arch_env, env->cls, p)) {
53                                 o.req             = arch_get_register_req(arch_env, p, -1);
54                                 o.carrier         = p;
55                                 o.irn             = irn;
56                                 o.pos             = -(get_Proj_proj(p) + 1);
57                                 o.partner         = NULL;
58                                 o.has_constraints = arch_register_req_is(o.req, limited);
59                                 obstack_grow(obst, &o, sizeof(o));
60                                 insn->n_ops++;
61                                 insn->out_constraints |= o.has_constraints;
62                                 pre_colored += arch_get_irn_register(arch_env, p) != NULL;
63                         }
64                 }
65
66                 insn->next_insn = p;
67         } else if(arch_irn_consider_in_reg_alloc(arch_env, env->cls, irn)) {
68                 o.req     = arch_get_register_req(arch_env, irn, -1);
69                 o.carrier = irn;
70                 o.irn     = irn;
71                 o.pos     = -1;
72                 o.partner = NULL;
73                 o.has_constraints = arch_register_req_is(o.req, limited);
74                 obstack_grow(obst, &o, sizeof(o));
75                 insn->n_ops++;
76                 insn->out_constraints |= o.has_constraints;
77                 pre_colored += arch_get_irn_register(arch_env, irn) != NULL;
78         }
79
80         if(pre_colored > 0) {
81                 assert(pre_colored == insn->n_ops && "partly pre-colored nodes not supported");
82                 insn->pre_colored = 1;
83         }
84         insn->use_start   = insn->n_ops;
85
86         for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
87                 ir_node *op = get_irn_n(irn, i);
88
89                 if(!arch_irn_consider_in_reg_alloc(arch_env, env->cls, op))
90                         continue;
91
92                 o.req     = arch_get_register_req(arch_env, irn, i);
93                 o.carrier = op;
94                 o.irn     = irn;
95                 o.pos     = i;
96                 o.partner = NULL;
97                 o.has_constraints = arch_register_req_is(o.req, limited);
98                 obstack_grow(obst, &o, sizeof(o));
99                 insn->n_ops++;
100                 insn->in_constraints |= o.has_constraints;
101         }
102
103         insn->has_constraints = insn->in_constraints | insn->out_constraints;
104         insn->ops = obstack_finish(obst);
105
106         /* Compute the admissible registers bitsets. */
107         for (i = 0; i < insn->n_ops; ++i) {
108                 be_operand_t *op = &insn->ops[i];
109                 const arch_register_req_t   *req = op->req;
110                 const arch_register_class_t *cls = req->cls;
111                 arch_register_req_type_t    type = req->type;
112
113                 /* If there is no special requirement, we allow current class here */
114                 if (cls == NULL && req->type == arch_register_req_type_none) {
115                         cls  = env->cls;
116                         type = arch_register_req_type_normal;
117                 }
118
119                 assert(cls == env->cls);
120
121                 op->regs = bitset_obstack_alloc(obst, env->cls->n_regs);
122
123                 if (type & arch_register_req_type_limited) {
124                         rbitset_copy_to_bitset(req->limited, op->regs);
125                 } else {
126                         arch_put_non_ignore_regs(arch_env, env->cls, op->regs);
127                         if (env->ignore_colors)
128                                 bitset_andnot(op->regs, env->ignore_colors);
129                 }
130         }
131
132         return insn;
133 }
134
135 be_insn_env_t *be_insn_env_init(be_insn_env_t *ie, const be_irg_t *birg,
136                                 const arch_register_class_t *cls,
137                                 struct obstack *obst)
138 {
139         ie->aenv = be_get_birg_arch_env(birg);
140         ie->cls  = cls;
141         ie->obst = obst;
142         ie->ignore_colors = bitset_obstack_alloc(obst, cls->n_regs);
143         be_abi_put_ignore_regs(birg->abi, cls, ie->ignore_colors);
144
145         return ie;
146 }