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