Write and read FIRM profiling information in little-endian format.
[libfirm] / ir / ir / irop.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   Representation of opcode of intermediate operation.
23  * @author  Christian Schaefer, Goetz Lindenmaier, Michael Beck
24  */
25 #include "config.h"
26
27 #include <string.h>
28
29 #include "irop_t.h"
30 #include "irnode_t.h"
31 #include "irhooks.h"
32 #include "irbackedge_t.h"
33
34 #include "iropt_t.h"
35 #include "irverify_t.h"
36 #include "reassoc_t.h"
37
38 #include "xmalloc.h"
39
40 void be_init_op(void);
41
42 /** the available next opcode */
43 static unsigned next_iro = iro_MaxOpcode;
44
45 /*
46  * Copies all attributes stored in the old node to the new node.
47  * Assumes both have the same opcode and sufficient size.
48  */
49 void default_copy_attr(ir_graph *irg, const ir_node *old_node,
50                        ir_node *new_node)
51 {
52         unsigned size = firm_add_node_size;
53         (void) irg;
54
55         assert(get_irn_op(old_node) == get_irn_op(new_node));
56         memcpy(&new_node->attr, &old_node->attr, get_op_attr_size(get_irn_op(old_node)));
57
58         if (size > 0) {
59                 /* copy additional node data */
60                 memcpy(get_irn_data(new_node, void, size), get_irn_data(old_node, void, size), size);
61         }
62 }
63
64 /**
65  * Copies all Call attributes stored in the old node to the new node.
66  */
67 static void call_copy_attr(ir_graph *irg, const ir_node *old_node,
68                            ir_node *new_node)
69 {
70         default_copy_attr(irg, old_node, new_node);
71         remove_Call_callee_arr(new_node);
72 }
73
74 /**
75  * Copies all Block attributes stored in the old node to the new node.
76  */
77 static void block_copy_attr(ir_graph *irg, const ir_node *old_node,
78                             ir_node *new_node)
79 {
80         default_copy_attr(irg, old_node, new_node);
81         new_node->attr.block.irg.irg       = irg;
82         new_node->attr.block.phis          = NULL;
83         new_node->attr.block.cg_backedge   = NULL;
84         new_node->attr.block.backedge      = new_backedge_arr(irg->obst, get_irn_arity(new_node));
85         new_node->attr.block.block_visited = 0;
86         memset(&new_node->attr.block.dom, 0, sizeof(new_node->attr.block.dom));
87         memset(&new_node->attr.block.pdom, 0, sizeof(new_node->attr.block.pdom));
88         /* It should be safe to copy the entity here, as it has no back-link to the old block.
89          * It serves just as a label number, so copying a labeled block results in an exact copy.
90          * This is at least what we need for DCE to work. */
91         new_node->attr.block.entity         = old_node->attr.block.entity;
92         new_node->attr.block.phis           = NULL;
93         INIT_LIST_HEAD(&new_node->attr.block.succ_head);
94 }
95
96 /**
97  * Copies all phi attributes stored in old node to the new node
98  */
99 static void phi_copy_attr(ir_graph *irg, const ir_node *old_node,
100                           ir_node *new_node)
101 {
102         default_copy_attr(irg, old_node, new_node);
103         new_node->attr.phi.next       = NULL;
104         new_node->attr.phi.u.backedge = new_backedge_arr(irg->obst, get_irn_arity(new_node));
105 }
106
107 /**
108  * Copies all ASM attributes stored in old node to the new node
109  */
110 static void ASM_copy_attr(ir_graph *irg, const ir_node *old_node,
111                           ir_node *new_node)
112 {
113         default_copy_attr(irg, old_node, new_node);
114         new_node->attr.assem.input_constraints  = DUP_ARR_D(ir_asm_constraint, irg->obst, old_node->attr.assem.input_constraints);
115         new_node->attr.assem.output_constraints = DUP_ARR_D(ir_asm_constraint, irg->obst, old_node->attr.assem.output_constraints);
116         new_node->attr.assem.clobbers = DUP_ARR_D(ident*, irg->obst, old_node->attr.assem.clobbers);
117 }
118
119 static void switch_copy_attr(ir_graph *irg, const ir_node *old_node,
120                              ir_node *new_node)
121 {
122         const ir_switch_table *table = get_Switch_table(old_node);
123         new_node->attr.switcha.table = ir_switch_table_duplicate(irg, table);
124         new_node->attr.switcha.n_outs = old_node->attr.switcha.n_outs;
125 }
126
127 /**
128  * Sets the default copy_attr operation for an ir_ops
129  *
130  * @param code   the opcode for the default operation
131  * @param ops    the operations initialized
132  *
133  * @return
134  *    The operations.
135  */
136 static void firm_set_default_copy_attr(unsigned code, ir_op_ops *ops)
137 {
138         switch (code) {
139         case iro_Call:   ops->copy_attr = call_copy_attr;   break;
140         case iro_Block:  ops->copy_attr = block_copy_attr;  break;
141         case iro_Phi:    ops->copy_attr = phi_copy_attr;    break;
142         case iro_ASM:    ops->copy_attr = ASM_copy_attr;    break;
143         case iro_Switch: ops->copy_attr = switch_copy_attr; break;
144         default:
145                 if (ops->copy_attr == NULL)
146                         ops->copy_attr = default_copy_attr;
147         }
148 }
149
150 /*
151  * Sets the default operation for an ir_ops.
152  */
153 static void set_default_operations(unsigned code, ir_op_ops *ops)
154 {
155         firm_set_default_hash(code, ops);
156         firm_set_default_computed_value(code, ops);
157         firm_set_default_equivalent_node(code, ops);
158         firm_set_default_transform_node(code, ops);
159         firm_set_default_node_cmp_attr(code, ops);
160         firm_set_default_get_type_attr(code, ops);
161         firm_set_default_get_entity_attr(code, ops);
162         firm_set_default_copy_attr(code, ops);
163         firm_set_default_verifier(code, ops);
164         firm_set_default_reassoc(code, ops);
165 }
166
167 /* Creates a new ir operation. */
168 ir_op *new_ir_op(unsigned code, const char *name, op_pin_state p,
169                  unsigned flags, op_arity opar, int op_index, size_t attr_size,
170                  const ir_op_ops *ops)
171 {
172         ir_op *res = XMALLOCZ(ir_op);
173
174         res->code      = code;
175         res->name      = new_id_from_chars(name, strlen(name));
176         res->pin_state = p;
177         res->attr_size = attr_size;
178         res->flags     = flags;
179         res->opar      = opar;
180         res->op_index  = op_index;
181         res->tag       = 0;
182
183         if (ops)
184                 res->ops = *ops;
185         else /* no given ops, set all operations to NULL */
186                 memset(&res->ops, 0, sizeof(res->ops));
187
188         set_default_operations(code, &res->ops);
189
190         add_irp_opcode(res);
191
192         hook_new_ir_op(res);
193         return res;
194 }
195
196 void free_ir_op(ir_op *code)
197 {
198         hook_free_ir_op(code);
199
200         remove_irp_opcode(code);
201         free(code);
202 }
203
204 void ir_op_set_fragile_indices(ir_op *op, int fragile_mem_index,
205                                int pn_x_regular, int pn_x_except)
206 {
207         op->fragile_mem_index = fragile_mem_index;
208         op->pn_x_regular = pn_x_regular;
209         op->pn_x_except = pn_x_except;
210 }
211
212 /* Returns the string for the opcode. */
213 const char *get_op_name (const ir_op *op)
214 {
215         return get_id_str(op->name);
216 }
217
218 unsigned (get_op_code)(const ir_op *op)
219 {
220   return get_op_code_(op);
221 }
222
223 ident *(get_op_ident)(const ir_op *op)
224 {
225   return get_op_ident_(op);
226 }
227
228 const char *get_op_pin_state_name(op_pin_state s)
229 {
230         switch (s) {
231 #define XXX(s) case s: return #s
232         XXX(op_pin_state_floats);
233         XXX(op_pin_state_pinned);
234         XXX(op_pin_state_exc_pinned);
235         XXX(op_pin_state_mem_pinned);
236 #undef XXX
237         }
238         return "<none>";
239 }
240
241 op_pin_state (get_op_pinned)(const ir_op *op)
242 {
243         return get_op_pinned_(op);
244 }
245
246 /* Sets op_pin_state_pinned in the opcode.  Setting it to floating has no effect
247    for Phi, Block and control flow nodes. */
248 void set_op_pinned(ir_op *op, op_pin_state pinned)
249 {
250         if (op == op_Block || op == op_Phi || is_op_cfopcode(op)) return;
251         op->pin_state = pinned;
252 }
253
254 /* retrieve the next free opcode */
255 unsigned get_next_ir_opcode(void)
256 {
257         return next_iro++;
258 }
259
260 /* Returns the next free n IR opcode number, allows to register a bunch of user ops */
261 unsigned get_next_ir_opcodes(unsigned num)
262 {
263         unsigned base = next_iro;
264         next_iro += num;
265         return base;
266 }
267
268 /* Returns the generic function pointer from an ir operation. */
269 op_func (get_generic_function_ptr)(const ir_op *op)
270 {
271         return get_generic_function_ptr_(op);
272 }
273
274 /* Store a generic function pointer into an ir operation. */
275 void (set_generic_function_ptr)(ir_op *op, op_func func)
276 {
277         set_generic_function_ptr_(op, func);
278 }
279
280 /* Returns the ir_op_ops of an ir_op. */
281 const ir_op_ops *(get_op_ops)(const ir_op *op)
282 {
283         return get_op_ops_(op);
284 }
285
286 irop_flags get_op_flags(const ir_op *op)
287 {
288         return (irop_flags)op->flags;
289 }
290
291 #include "gen_irop.c.inl"