make it possible to have different compare functions for different backend node attri...
[libfirm] / ir / be / mips / mips_new_nodes.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 /**
21  * @file
22  * @brief    This file implements the creation of the achitecture specific firm
23  *           opcodes and the coresponding node constructors for the mips
24  *           assembler irg.
25  * @author   Matthias Braun, Mehdi
26  * @version  $Id$
27  */
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #include <stdlib.h>
33
34 #include "irprog_t.h"
35 #include "irgraph_t.h"
36 #include "irnode_t.h"
37 #include "irmode_t.h"
38 #include "ircons_t.h"
39 #include "iropt_t.h"
40 #include "irop.h"
41 #include "firm_common_t.h"
42 #include "irvrfy_t.h"
43 #include "irprintf.h"
44 #include "xmalloc.h"
45
46 #include "../bearch_t.h"
47
48 #include "mips_nodes_attr.h"
49 #include "mips_new_nodes.h"
50 #include "gen_mips_regalloc_if.h"
51
52
53
54 /***********************************************************************************
55  *      _                                   _       _             __
56  *     | |                                 (_)     | |           / _|
57  *   __| |_   _ _ __ ___  _ __   ___ _ __   _ _ __ | |_ ___ _ __| |_ __ _  ___ ___
58  *  / _` | | | | '_ ` _ \| '_ \ / _ \ '__| | | '_ \| __/ _ \ '__|  _/ _` |/ __/ _ \
59  * | (_| | |_| | | | | | | |_) |  __/ |    | | | | | ||  __/ |  | || (_| | (_|  __/
60  *  \__,_|\__,_|_| |_| |_| .__/ \___|_|    |_|_| |_|\__\___|_|  |_| \__,_|\___\___|
61  *                       | |
62  *                       |_|
63  ***********************************************************************************/
64
65 /**
66  * Dumps the register requirements for either in or out.
67  */
68 static void dump_reg_req(FILE *F, ir_node *n, const arch_register_req_t **reqs, int inout) {
69         char *dir = inout ? "out" : "in";
70         int   max = inout ? get_mips_n_res(n) : get_irn_arity(n);
71         char  buf[1024];
72         int   i;
73
74         memset(buf, 0, sizeof(buf));
75
76         if (reqs) {
77                 for (i = 0; i < max; i++) {
78                         fprintf(F, "%sreq #%d =", dir, i);
79
80                         if (reqs[i]->type == arch_register_req_type_none) {
81                                 fprintf(F, " n/a");
82                         }
83
84                         if (reqs[i]->type & arch_register_req_type_normal) {
85                                 fprintf(F, " %s", reqs[i]->cls->name);
86                         }
87
88                         if (reqs[i]->type & arch_register_req_type_limited) {
89                                 fprintf(F, " %s",
90                                         arch_register_req_format(buf, sizeof(buf), reqs[i], n));
91                         }
92
93                         if (reqs[i]->type & arch_register_req_type_should_be_same) {
94                                 ir_fprintf(F, " same as %+F", get_irn_n(n, reqs[i]->other_same));
95                         }
96
97                         if (reqs[i]->type & arch_register_req_type_should_be_different) {
98                                 ir_fprintf(F, " different from %+F", get_irn_n(n, reqs[i]->other_different));
99                         }
100
101                         fprintf(F, "\n");
102                 }
103
104                 fprintf(F, "\n");
105         } else {
106                 fprintf(F, "%sreq = N/A\n", dir);
107         }
108 }
109
110
111 /**
112  * Dumper interface for dumping mips nodes in vcg.
113  * @param n        the node to dump
114  * @param F        the output file
115  * @param reason   indicates which kind of information should be dumped
116  * @return 0 on success or != 0 on failure
117  */
118 static int mips_dump_node(ir_node *n, FILE *F, dump_reason_t reason) {
119         ir_mode     *mode = NULL;
120         int          bad  = 0;
121         int          i;
122         mips_attr_t *attr;
123         char buf[64];
124         const arch_register_req_t **reqs;
125         const arch_register_t     **slots;
126
127         switch (reason) {
128                 case dump_node_opcode_txt:
129                         fprintf(F, "%s", get_irn_opname(n));
130                         break;
131
132                 case dump_node_mode_txt:
133                         mode = get_irn_mode(n);
134
135                         if (mode) {
136                                 fprintf(F, "[%s]", get_mode_name(mode));
137                         }
138                         else {
139                                 fprintf(F, "[?NOMODE?]");
140                         }
141                         break;
142
143                 case dump_node_nodeattr_txt:
144
145                         /* TODO: dump some attributes which should show up */
146                         /* in node name in dump (e.g. consts or the like)  */
147
148                         break;
149
150                 case dump_node_info_txt:
151                         attr = get_mips_attr(n);
152                         fprintf(F, "=== mips attr begin ===\n");
153
154                         /* dump IN requirements */
155                         if (get_irn_arity(n) > 0) {
156                                 reqs = get_mips_in_req_all(n);
157                                 dump_reg_req(F, n, reqs, 0);
158                         }
159
160                         /* dump OUT requirements */
161                         if (ARR_LEN(attr->slots) > 0) {
162                                 reqs = get_mips_out_req_all(n);
163                                 dump_reg_req(F, n, reqs, 1);
164                         }
165
166                         /* dump assigned registers */
167                         slots = get_mips_slots(n);
168                         if (slots && ARR_LEN(attr->slots) > 0) {
169                                 for (i = 0; i < ARR_LEN(attr->slots); i++) {
170                                         if (slots[i]) {
171                                                 fprintf(F, "reg #%d = %s\n", i, slots[i]->name);
172                                         }
173                                         else {
174                                                 fprintf(F, "reg #%d = n/a\n", i);
175                                         }
176                                 }
177                         }
178                         fprintf(F, "\n");
179
180                         /* dump n_res */
181                         fprintf(F, "n_res = %d\n", get_mips_n_res(n));
182
183                         /* dump flags */
184                         fprintf(F, "flags =");
185                         if (attr->flags == arch_irn_flags_none) {
186                                 fprintf(F, " none");
187                         }
188                         else {
189                                 if (attr->flags & arch_irn_flags_dont_spill) {
190                                         fprintf(F, " unspillable");
191                                 }
192                                 if (attr->flags & arch_irn_flags_rematerializable) {
193                                         fprintf(F, " remat");
194                                 }
195                                 if (attr->flags & arch_irn_flags_ignore) {
196                                         fprintf(F, " ignore");
197                                 }
198                         }
199                         fprintf(F, " (%d)\n", attr->flags);
200
201                         if(attr->stack_entity != NULL) {
202                                 fprintf(F, " stack entity %s\n", get_entity_name(attr->stack_entity));
203                         }
204                         if(attr->tv != NULL) {
205                                 tarval_snprintf(buf, sizeof(buf), attr->tv);
206                                 fprintf(F, " tarval %s\n", buf);
207                         }
208                         if(attr->symconst != NULL) {
209                                 fprintf(F, " symconst '%s'\n", get_entity_name(attr->symconst));
210                         }
211
212                         fprintf(F, "=== mips attr end ===\n");
213                         /* end of: case dump_node_info_txt */
214                         break;
215         }
216
217
218         return bad;
219 }
220
221
222
223 /***************************************************************************************************
224  *        _   _                   _       __        _                    _   _               _
225  *       | | | |                 | |     / /       | |                  | | | |             | |
226  *   __ _| |_| |_ _ __   ___  ___| |_   / /_ _  ___| |_   _ __ ___   ___| |_| |__   ___   __| |___
227  *  / _` | __| __| '__| / __|/ _ \ __| / / _` |/ _ \ __| | '_ ` _ \ / _ \ __| '_ \ / _ \ / _` / __|
228  * | (_| | |_| |_| |    \__ \  __/ |_ / / (_| |  __/ |_  | | | | | |  __/ |_| | | | (_) | (_| \__ \
229  *  \__,_|\__|\__|_|    |___/\___|\__/_/ \__, |\___|\__| |_| |_| |_|\___|\__|_| |_|\___/ \__,_|___/
230  *                                        __/ |
231  *                                       |___/
232  ***************************************************************************************************/
233
234 mips_attr_t *get_mips_attr(ir_node *node) {
235         assert(is_mips_irn(node) && "need mips node to get attributes");
236         return (mips_attr_t *) get_irn_generic_attr(node);
237 }
238
239 const mips_attr_t *get_mips_attr_const(const ir_node *node) {
240         assert(is_mips_irn(node) && "need mips node to get attributes");
241         return get_irn_generic_attr_const(node);
242 }
243
244 /**
245  * Returns the argument register requirements of a mips node.
246  */
247 const arch_register_req_t **get_mips_in_req_all(const ir_node *node) {
248         const mips_attr_t *attr = get_mips_attr_const(node);
249         return attr->in_req;
250 }
251
252 /**
253  * Returns the result register requirements of an mips node.
254  */
255 const arch_register_req_t **get_mips_out_req_all(const ir_node *node) {
256         const mips_attr_t *attr = get_mips_attr_const(node);
257         return attr->out_req;
258 }
259
260 /**
261  * Returns the argument register requirement at position pos of an mips node.
262  */
263 const arch_register_req_t *get_mips_in_req(const ir_node *node, int pos) {
264         const mips_attr_t *attr = get_mips_attr_const(node);
265         return attr->in_req[pos];
266 }
267
268 /**
269  * Returns the result register requirement at position pos of an mips node.
270  */
271 const arch_register_req_t *get_mips_out_req(const ir_node *node, int pos) {
272         const mips_attr_t *attr = get_mips_attr_const(node);
273         return attr->out_req[pos];
274 }
275
276 /**
277  * Sets the OUT register requirements at position pos.
278  */
279 void set_mips_req_out(ir_node *node, const arch_register_req_t *req, int pos) {
280         mips_attr_t *attr   = get_mips_attr(node);
281         attr->out_req[pos] = req;
282 }
283
284 /**
285  * Sets the IN register requirements at position pos.
286  */
287 void set_mips_req_in(ir_node *node, const arch_register_req_t *req, int pos) {
288         mips_attr_t *attr  = get_mips_attr(node);
289         attr->in_req[pos] = req;
290 }
291
292 /**
293  * Returns the register flag of an mips node.
294  */
295 arch_irn_flags_t get_mips_flags(const ir_node *node) {
296         const mips_attr_t *attr = get_mips_attr_const(node);
297         return attr->flags;
298 }
299
300 /**
301  * Sets the register flag of an mips node.
302  */
303 void set_mips_flags(ir_node *node, arch_irn_flags_t flags) {
304         mips_attr_t *attr = get_mips_attr(node);
305         attr->flags      = flags;
306 }
307
308 /**
309  * Returns the result register slots of an mips node.
310  */
311 const arch_register_t **get_mips_slots(const ir_node *node) {
312         const mips_attr_t *attr = get_mips_attr_const(node);
313         return attr->slots;
314 }
315
316 /**
317  * Returns the name of the OUT register at position pos.
318  */
319 const char *get_mips_out_reg_name(const ir_node *node, int pos) {
320         const mips_attr_t *attr = get_mips_attr_const(node);
321
322         assert(is_mips_irn(node) && "Not an mips node.");
323         assert(pos < ARR_LEN(attr->slots) && "Invalid OUT position.");
324         assert(attr->slots[pos]  && "No register assigned");
325
326         return arch_register_get_name(attr->slots[pos]);
327 }
328
329 /**
330  * Returns the index of the OUT register at position pos within its register class.
331  */
332 int get_mips_out_regnr(const ir_node *node, int pos) {
333         const mips_attr_t *attr = get_mips_attr_const(node);
334
335         assert(is_mips_irn(node) && "Not an mips node.");
336         assert(pos < ARR_LEN(attr->slots) && "Invalid OUT position.");
337         assert(attr->slots[pos]  && "No register assigned");
338
339         return arch_register_get_index(attr->slots[pos]);
340 }
341
342 /**
343  * Returns the OUT register at position pos.
344  */
345 const arch_register_t *get_mips_out_reg(const ir_node *node, int pos) {
346         const mips_attr_t *attr = get_mips_attr_const(node);
347
348         assert(is_mips_irn(node) && "Not an mips node.");
349         assert(pos < ARR_LEN(attr->slots) && "Invalid OUT position.");
350         assert(attr->slots[pos]  && "No register assigned");
351
352         return attr->slots[pos];
353 }
354
355 /**
356  * Returns the number of results.
357  */
358 int get_mips_n_res(const ir_node *node) {
359         const mips_attr_t *attr = get_mips_attr_const(node);
360         return ARR_LEN(attr->slots);
361 }
362
363 /**
364  * Initializes the nodes attributes.
365  */
366 void init_mips_attributes(ir_node *node, arch_irn_flags_t flags, const arch_register_req_t **in_reqs,
367                                                   const arch_register_req_t **out_reqs,
368                                                                                                   const be_execution_unit_t ***execution_units,
369                                                                                                   int n_res, unsigned latency)
370 {
371         ir_graph       *irg  = get_irn_irg(node);
372         struct obstack *obst = get_irg_obstack(irg);
373         mips_attr_t    *attr = get_mips_attr(node);
374
375         attr->flags   = flags;
376         attr->out_req = out_reqs;
377         attr->in_req  = in_reqs;
378
379         attr->slots = NEW_ARR_D(const arch_register_t*, obst, n_res);
380         memset(attr->slots, 0, n_res * sizeof(attr->slots[0]));
381 }
382
383 static
384 int mips_compare_attr(ir_node *node_a, ir_node *node_b)
385 {
386         const mips_attr_t *a = get_mips_attr_const(node_a);
387         const mips_attr_t *b = get_mips_attr_const(node_b);
388
389         if(a->tv != b->tv)
390                 return 1;
391         if(a->symconst != b->symconst)
392                 return 1;
393         if(a->stack_entity != b->stack_entity)
394                 return 1;
395         if(a->flags != b->flags)
396                 return 1;
397         if(ARR_LEN(a->slots) != ARR_LEN(b->slots))
398                 return 1;
399         if(a->switch_default_pn != b->switch_default_pn)
400                 return 1;
401
402         return 0;
403 }
404
405 /************************************************************************
406  *  ___ _____     _     _ _
407  * |_ _|  ___|__ | | __| (_)_ __   __ _
408  *  | || |_ / _ \| |/ _` | | '_ \ / _` |
409  *  | ||  _| (_) | | (_| | | | | | (_| |
410  * |___|_|  \___/|_|\__,_|_|_| |_|\__, |
411  *                                |___/
412  ************************************************************************/
413
414 #if 0
415 // test if a tarval can be expressed in a 16bit immediate value
416 static int is_tarval_16(ir_node* node)
417 {
418         mips_attr_t *attr = get_mips_attr(node);
419         tarval* tv = attr->tv;
420         long val = get_tarval_long(tv);
421         if(get_mode_sign(get_irn_mode(node))) {
422                 if(val < -32768 || val > 32767)
423                         return 0;
424         } else {
425                 unsigned long uval = (unsigned long) val;
426                 if(uval > 65536)
427                         return 0;
428         }
429
430         return 1;
431 }
432
433 #define MIPS_MAKE_IFOLDING_TRANSFORM(srcnode, inode, commutative)       \
434 ir_node *mips_transform_##srcnode(ir_node* node)                                \
435 {                                                                                                                               \
436         ir_node* op1 = get_irn_n(node, 0);                                                      \
437         ir_node* op2 = get_irn_n(node, 1);                                                      \
438         ir_node* result;                                                                                        \
439         if(op1 == NULL || op2 == NULL)                                                          \
440                 return node;                                                                                    \
441                                                                                                                                 \
442         if((is_mips_lli(op2) || is_mips_lui(op2)) && is_tarval_16(op2)) {       \
443                 mips_attr_t *attr, *op_attr = get_mips_attr(op2);               \
444                 long val = get_tarval_long(op_attr->tv);                                \
445                 result = new_rd_mips_##inode(get_irn_dbg_info(node), get_irn_irg(node), get_nodes_block(node),  \
446                         op1);                                                                       \
447                 attr = get_mips_attr(result);                                                   \
448                 attr->tv = new_tarval_from_long(val, get_mode_sign(get_irn_mode(node)) ? mode_Hs : mode_Hu);    \
449                 return result;                                                                                  \
450         }                                                                                                                       \
451                                                                                                                                 \
452         if(commutative && (is_mips_lli(op1) || is_mips_lui(op1)) && is_tarval_16(op1)) {        \
453                 mips_attr_t *attr, *op_attr = get_mips_attr(op1);       \
454                 long val = get_tarval_long(op_attr->tv);                                \
455                 result = new_rd_mips_##inode(get_irn_dbg_info(node), get_irn_irg(node), get_nodes_block(node),  \
456                         op2);                                                                       \
457                 attr = get_mips_attr(result);                                                   \
458                 attr->tv = new_tarval_from_long(val, get_mode_sign(get_irn_mode(node)) ? mode_Hs : mode_Hu);    \
459                 return result;                                                                                  \
460         }                                                                                                                       \
461                                                                                                                                 \
462         return node;                                                                                            \
463 }
464
465 MIPS_MAKE_IFOLDING_TRANSFORM(addu, addiu, 1)
466 MIPS_MAKE_IFOLDING_TRANSFORM(and, andi, 1)
467 MIPS_MAKE_IFOLDING_TRANSFORM(or, ori, 1)
468 MIPS_MAKE_IFOLDING_TRANSFORM(sra, srai, 0)
469 MIPS_MAKE_IFOLDING_TRANSFORM(xor, xori, 1)
470 MIPS_MAKE_IFOLDING_TRANSFORM(sl, sli, 0)
471 MIPS_MAKE_IFOLDING_TRANSFORM(sr, sri, 0)
472 MIPS_MAKE_IFOLDING_TRANSFORM(slt, slti, 0)
473
474 void mips_init_opcode_transforms(void) {
475         op_mips_addu->ops.transform_node = mips_transform_addu;
476         op_mips_and->ops.transform_node = mips_transform_and;
477         op_mips_or->ops.transform_node  = mips_transform_or;
478         op_mips_sra->ops.transform_node = mips_transform_sra;
479         op_mips_xor->ops.transform_node = mips_transform_xor;
480         op_mips_sl->ops.transform_node  = mips_transform_sl;
481         op_mips_sr->ops.transform_node  = mips_transform_sr;
482         op_mips_slt->ops.transform_node = mips_transform_slt;
483 }
484 #endif
485
486 /***************************************************************************************
487  *                  _                            _                   _
488  *                 | |                          | |                 | |
489  *  _ __   ___   __| | ___    ___ ___  _ __  ___| |_ _ __ _   _  ___| |_ ___  _ __ ___
490  * | '_ \ / _ \ / _` |/ _ \  / __/ _ \| '_ \/ __| __| '__| | | |/ __| __/ _ \| '__/ __|
491  * | | | | (_) | (_| |  __/ | (_| (_) | | | \__ \ |_| |  | |_| | (__| || (_) | |  \__ \
492  * |_| |_|\___/ \__,_|\___|  \___\___/|_| |_|___/\__|_|   \__,_|\___|\__\___/|_|  |___/
493  *
494  ***************************************************************************************/
495
496 /* Include the generated constructor functions */
497 #include "gen_mips_new_nodes.c.inl"