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