introduce be_foreach_use and use it
[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 "beabi.h"
36 #include "raw_bitset.h"
37
38 be_insn_t *be_scan_insn(be_chordal_env_t *const env, ir_node *const irn)
39 {
40         struct obstack *const obst = &env->obst;
41         be_operand_t o;
42
43         be_insn_t *insn = OALLOCZ(obst, be_insn_t);
44
45         bool has_constraints = false;
46
47         const arch_register_class_t *cls = env->cls;
48         insn->irn = irn;
49         be_foreach_definition(irn, cls, p,
50                 /* found a def: create a new operand */
51                 arch_register_req_t const *const req = arch_get_irn_register_req(p);
52                 if (arch_register_req_is(req, limited)) {
53                         o.regs          = req->limited;
54                         has_constraints = true;
55                 } else {
56                         o.regs           = env->allocatable_regs->data;
57                         has_constraints |= req->width > 1;
58                 }
59                 o.carrier = p;
60                 o.partner = NULL;
61                 obstack_grow(obst, &o, sizeof(o));
62                 insn->n_ops++;
63         );
64
65         insn->use_start = insn->n_ops;
66
67         /* now collect the uses for this node */
68         be_foreach_use(irn, cls, in_req, op, op_req,
69                 /* found a register use, create an operand */
70                 if (arch_register_req_is(in_req, limited)) {
71                         o.regs          = in_req->limited;
72                         has_constraints = true;
73                 } else {
74                         o.regs = env->allocatable_regs->data;
75                 }
76                 o.carrier = op;
77                 o.partner = NULL;
78                 obstack_grow(obst, &o, sizeof(o));
79                 insn->n_ops++;
80         );
81
82         if (!has_constraints)
83                 return NULL;
84
85         insn->ops = (be_operand_t*)obstack_finish(obst);
86         return insn;
87 }