d112294c2ab6da47af8bedbac9fbaf388341c1c4
[libfirm] / ir / be / arm / arm_new_nodes.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  This file implements the creation of the architecture specific firm
23  *         opcodes and the corresponding node constructors for the arm
24  *         assembler irg.
25  * @author Oliver Richter, Tobias Gneist
26  * @version $Id$
27  */
28 #include "config.h"
29
30 #include <stdlib.h>
31
32 #include "irprog_t.h"
33 #include "irgraph_t.h"
34 #include "irnode_t.h"
35 #include "irmode_t.h"
36 #include "ircons_t.h"
37 #include "iropt_t.h"
38 #include "irop.h"
39 #include "firm_common_t.h"
40 #include "irvrfy_t.h"
41 #include "irprintf.h"
42 #include "xmalloc.h"
43
44 #include "../bearch_t.h"
45
46 #include "arm_nodes_attr.h"
47 #include "arm_new_nodes.h"
48 #include "arm_optimize.h"
49
50 #include "../beabi.h"
51 #include "bearch_arm_t.h"
52
53 /**
54  * Returns the shift modifier string.
55  */
56 const char *arm_shf_mod_name(arm_shift_modifier mod) {
57         static const char *names[] = { NULL, NULL, "asr", "lsl", "lsr", "ror", "rrx" };
58         return names[mod];
59 }
60
61 /**
62  * Return the fpa immediate from the encoding.
63  */
64 const char *arm_get_fpa_imm_name(long imm_value) {
65         static const char *fpa_imm[] = {
66                 "0",
67                 "1",
68                 "2",
69                 "3",
70                 "4",
71                 "5",
72                 "10",
73                 "0.5"
74         };
75         return fpa_imm[imm_value];
76 }
77
78 /***********************************************************************************
79  *      _                                   _       _             __
80  *     | |                                 (_)     | |           / _|
81  *   __| |_   _ _ __ ___  _ __   ___ _ __   _ _ __ | |_ ___ _ __| |_ __ _  ___ ___
82  *  / _` | | | | '_ ` _ \| '_ \ / _ \ '__| | | '_ \| __/ _ \ '__|  _/ _` |/ __/ _ \
83  * | (_| | |_| | | | | | | |_) |  __/ |    | | | | | ||  __/ |  | || (_| | (_|  __/
84  *  \__,_|\__,_|_| |_| |_| .__/ \___|_|    |_|_| |_|\__\___|_|  |_| \__,_|\___\___|
85  *                       | |
86  *                       |_|
87  ***********************************************************************************/
88
89 /**
90  * Dumps the register requirements for either in or out.
91  */
92 static void dump_reg_req(FILE *F, const ir_node *node,
93                          const arch_register_req_t **reqs, int inout) {
94         char *dir = inout ? "out" : "in";
95         int   max = inout ? (int) arch_irn_get_n_outs(node) : get_irn_arity(node);
96         char  buf[1024];
97         int   i;
98
99         memset(buf, 0, sizeof(buf));
100
101         if (reqs) {
102                 for (i = 0; i < max; i++) {
103                         fprintf(F, "%sreq #%d =", dir, i);
104
105                         if (reqs[i]->type == arch_register_req_type_none) {
106                                 fprintf(F, " n/a");
107                         }
108
109                         if (reqs[i]->type & arch_register_req_type_normal) {
110                                 fprintf(F, " %s", reqs[i]->cls->name);
111                         }
112
113                         if (reqs[i]->type & arch_register_req_type_limited) {
114                                 fprintf(F, " %s",
115                                         arch_register_req_format(buf, sizeof(buf), reqs[i], node));
116                         }
117
118                         if (reqs[i]->type & arch_register_req_type_should_be_same) {
119                                 const unsigned other = reqs[i]->other_same;
120                                 int i;
121
122                                 ir_fprintf(F, " same as");
123                                 for (i = 0; 1U << i <= other; ++i) {
124                                         if (other & (1U << i)) {
125                                                 ir_fprintf(F, " %+F", get_irn_n(node, i));
126                                         }
127                                 }
128                         }
129
130                         if (reqs[i]->type & arch_register_req_type_must_be_different) {
131                                 const unsigned other = reqs[i]->other_different;
132                                 int i;
133
134                                 ir_fprintf(F, " different from");
135                                 for (i = 0; 1U << i <= other; ++i) {
136                                         if (other & (1U << i)) {
137                                                 ir_fprintf(F, " %+F", get_irn_n(node, i));
138                                         }
139                                 }
140                         }
141
142                         fprintf(F, "\n");
143                 }
144
145                 fprintf(F, "\n");
146         } else {
147                 fprintf(F, "%sreq = N/A\n", dir);
148         }
149 }
150
151 /**
152  * Dumper interface for dumping arm nodes in vcg.
153  * @param n        the node to dump
154  * @param F        the output file
155  * @param reason   indicates which kind of information should be dumped
156  * @return 0 on success or != 0 on failure
157  */
158 static int arm_dump_node(ir_node *n, FILE *F, dump_reason_t reason) {
159         ir_mode     *mode = NULL;
160         int         bad  = 0;
161         int         i, n_res, flags;
162         arm_attr_t  *attr = get_arm_attr(n);
163         const arch_register_req_t **reqs;
164         arm_shift_modifier        mod;
165
166         switch (reason) {
167                 case dump_node_opcode_txt:
168                         fprintf(F, "%s", get_irn_opname(n));
169                         break;
170
171                 case dump_node_mode_txt:
172                         mode = get_irn_mode(n);
173
174                         if (mode) {
175                                 fprintf(F, "[%s]", get_mode_name(mode));
176                         }
177                         else {
178                                 fprintf(F, "[?NOMODE?]");
179                         }
180                         break;
181
182                 case dump_node_nodeattr_txt:
183                         mod = ARM_GET_SHF_MOD(attr);
184                         if (ARM_HAS_SHIFT(mod)) {
185                                 fprintf(F, "[%s #%ld]", arm_shf_mod_name(mod), attr->imm_value);
186                         }
187                         else if (mod == ARM_SHF_IMM) {
188                                 /* immediate */
189                                 fprintf(F, "[#0x%X]", arm_decode_imm_w_shift(attr->imm_value));
190                         }
191                         break;
192
193                 case dump_node_info_txt:
194                         fprintf(F, "=== arm attr begin ===\n");
195
196                         /* dump IN requirements */
197                         if (get_irn_arity(n) > 0) {
198                                 reqs = get_arm_in_req_all(n);
199                                 dump_reg_req(F, n, reqs, 0);
200                         }
201
202                         n_res = arch_irn_get_n_outs(n);
203                         if (n_res > 0) {
204                                 /* dump OUT requirements */
205                                 reqs = get_arm_out_req_all(n);
206                                 dump_reg_req(F, n, reqs, 1);
207
208                                 /* dump assigned registers */
209                                 for (i = 0; i < n_res; i++) {
210                                         const arch_register_t *reg = arch_irn_get_register(n, i);
211
212                                         fprintf(F, "reg #%d = %s\n", i, reg ? arch_register_get_name(reg) : "n/a");
213                                 }
214                                 fprintf(F, "\n");
215                         }
216                         fprintf(F, "\n");
217
218                         /* dump n_res */
219                         fprintf(F, "n_res = %d\n", n_res);
220
221                         /* dump flags */
222                         fprintf(F, "flags =");
223                         flags = arch_irn_get_flags(n);
224                         if (flags == arch_irn_flags_none) {
225                                 fprintf(F, " none");
226                         }
227                         else {
228                                 if (flags & arch_irn_flags_dont_spill) {
229                                         fprintf(F, " unspillable");
230                                 }
231                                 if (flags & arch_irn_flags_rematerializable) {
232                                         fprintf(F, " remat");
233                                 }
234                                 if (flags & arch_irn_flags_modify_flags) {
235                                         fprintf(F, " modify_flags");
236                                 }
237                         }
238                         fprintf(F, " (%d)\n", flags);
239
240                         if (is_arm_CopyB(n)) {
241                                 fprintf(F, "size = %lu\n", get_arm_imm_value(n));
242                         } else {
243                                 long v =  get_arm_imm_value(n);
244                                 if (ARM_GET_FPA_IMM(attr)) {
245                                         fprintf(F, "immediate float value = %s\n", arm_get_fpa_imm_name(v));
246                                 } else {
247                                         fprintf(F, "immediate value = %ld (0x%08lx)\n", v, v);
248                                 }
249                         }
250
251                         if (is_arm_CmpBra(n) && get_arm_CondJmp_proj_num(n) >= 0) {
252                                 fprintf(F, "proj_num = (%d)\n", get_arm_CondJmp_proj_num(n));
253                         }
254                         /* TODO: dump all additional attributes */
255
256                         fprintf(F, "=== arm attr end ===\n");
257                         /* end of: case dump_node_info_txt */
258                         break;
259         }
260         return bad;
261 }
262
263
264
265 /***************************************************************************************************
266  *        _   _                   _       __        _                    _   _               _
267  *       | | | |                 | |     / /       | |                  | | | |             | |
268  *   __ _| |_| |_ _ __   ___  ___| |_   / /_ _  ___| |_   _ __ ___   ___| |_| |__   ___   __| |___
269  *  / _` | __| __| '__| / __|/ _ \ __| / / _` |/ _ \ __| | '_ ` _ \ / _ \ __| '_ \ / _ \ / _` / __|
270  * | (_| | |_| |_| |    \__ \  __/ |_ / / (_| |  __/ |_  | | | | | |  __/ |_| | | | (_) | (_| \__ \
271  *  \__,_|\__|\__|_|    |___/\___|\__/_/ \__, |\___|\__| |_| |_| |_|\___|\__|_| |_|\___/ \__,_|___/
272  *                                        __/ |
273  *                                       |___/
274  ***************************************************************************************************/
275
276 /* Returns the attributes of a generic Arm node. */
277 arm_attr_t *get_arm_attr(ir_node *node) {
278         assert(is_arm_irn(node) && "need arm node to get attributes");
279         return get_irn_generic_attr(node);
280 }
281
282 const arm_attr_t *get_arm_attr_const(const ir_node *node) {
283         assert(is_arm_irn(node) && "need arm node to get attributes");
284         return get_irn_generic_attr_const(node);
285 }
286
287 /**
288  * Returns the attributes of an ARM SymConst node.
289  */
290 arm_SymConst_attr_t *get_arm_SymConst_attr(ir_node *node) {
291         assert(is_arm_SymConst(node));
292         return get_irn_generic_attr(node);
293 }
294
295 const arm_SymConst_attr_t *get_arm_SymConst_attr_const(const ir_node *node) {
296         assert(is_arm_SymConst(node));
297         return get_irn_generic_attr_const(node);
298 }
299
300 static const arm_fpaConst_attr_t *get_arm_fpaConst_attr_const(const ir_node *node) {
301         const arm_attr_t          *attr     = get_arm_attr_const(node);
302         const arm_fpaConst_attr_t *fpa_attr = CONST_CAST_ARM_ATTR(arm_fpaConst_attr_t, attr);
303
304         return fpa_attr;
305 }
306
307 static arm_fpaConst_attr_t *get_arm_fpaConst_attr(ir_node *node) {
308         arm_attr_t          *attr     = get_arm_attr(node);
309         arm_fpaConst_attr_t *fpa_attr = CAST_ARM_ATTR(arm_fpaConst_attr_t, attr);
310
311         return fpa_attr;
312 }
313
314 static int is_arm_CondJmp(const ir_node *node) {
315         int code = get_arm_irn_opcode(node);
316
317         return (code == iro_arm_CmpBra || code == iro_arm_fpaCmfBra ||
318                 code == iro_arm_fpaCnfBra || iro_arm_fpaCmfeBra ||
319                 code == iro_arm_fpaCnfeBra);
320 }
321
322 /* Returns the attributes of a CondJmp node. */
323 arm_CondJmp_attr_t *get_arm_CondJmp_attr(ir_node *node) {
324         assert(is_arm_CondJmp(node));
325         return get_irn_generic_attr(node);
326 }
327
328 const arm_CondJmp_attr_t *get_arm_CondJmp_attr_const(const ir_node *node) {
329         assert(is_arm_CondJmp(node));
330         return get_irn_generic_attr_const(node);
331 }
332
333 /* Returns the attributes of a SwitchJmp node. */
334 arm_SwitchJmp_attr_t *get_arm_SwitchJmp_attr(ir_node *node) {
335         assert(is_arm_SwitchJmp(node));
336         return get_irn_generic_attr(node);
337 }
338
339 const arm_SwitchJmp_attr_t *get_arm_SwitchJmp_attr_const(const ir_node *node) {
340         assert(is_arm_SwitchJmp(node));
341         return get_irn_generic_attr_const(node);
342 }
343
344 /**
345  * Returns the argument register requirements of a arm node.
346  */
347 const arch_register_req_t **get_arm_in_req_all(const ir_node *node) {
348         const arm_attr_t *attr = get_arm_attr_const(node);
349         return attr->in_req;
350 }
351
352 /**
353  * Returns the result register requirements of an arm node.
354  */
355 const arch_register_req_t **get_arm_out_req_all(const ir_node *node) {
356         const arm_attr_t *attr = get_arm_attr_const(node);
357         return attr->out_req;
358 }
359
360 /**
361  * Returns the argument register requirement at position pos of an arm node.
362  */
363 const arch_register_req_t *get_arm_in_req(const ir_node *node, int pos) {
364         const arm_attr_t *attr = get_arm_attr_const(node);
365         return attr->in_req[pos];
366 }
367
368 /**
369  * Returns the result register requirement at position pos of an arm node.
370  */
371 const arch_register_req_t *get_arm_out_req(const ir_node *node, int pos) {
372         const arm_attr_t *attr = get_arm_attr_const(node);
373         return attr->out_req[pos];
374 }
375
376 /**
377  * Sets the OUT register requirements at position pos.
378  */
379 void set_arm_req_out(ir_node *node, const arch_register_req_t *req, int pos) {
380         arm_attr_t *attr   = get_arm_attr(node);
381         attr->out_req[pos] = req;
382 }
383
384 /**
385  * Sets the complete OUT requirements of node.
386  */
387 void set_arm_req_out_all(ir_node *node, const arch_register_req_t **reqs) {
388         arm_attr_t *attr = get_arm_attr(node);
389         attr->out_req    = reqs;
390 }
391
392 /**
393  * Sets the IN register requirements at position pos.
394  */
395 void set_arm_req_in(ir_node *node, const arch_register_req_t *req, int pos) {
396         arm_attr_t *attr  = get_arm_attr(node);
397         attr->in_req[pos] = req;
398 }
399
400 /**
401  * Returns the immediate value
402  */
403 long get_arm_imm_value(const ir_node *node) {
404         const arm_attr_t *attr = get_arm_attr_const(node);
405         return attr->imm_value;
406 }
407
408 /**
409  * Sets the tarval value
410  */
411 void set_arm_imm_value(ir_node *node, long imm_value) {
412         arm_attr_t *attr = get_arm_attr(node);
413         attr->imm_value = imm_value;
414 }
415
416 /**
417  * Returns the fpaConst value
418  */
419 tarval *get_fpaConst_value(const ir_node *node) {
420         const arm_fpaConst_attr_t *attr = get_arm_fpaConst_attr_const(node);
421         return attr->tv;
422 }
423
424 /**
425  * Sets the tarval value
426  */
427 void set_fpaConst_value(ir_node *node, tarval *tv) {
428         arm_fpaConst_attr_t *attr = get_arm_fpaConst_attr(node);
429         attr->tv = tv;
430 }
431
432 /**
433  * Returns the proj num
434  */
435 int get_arm_CondJmp_proj_num(const ir_node *node) {
436         const arm_CondJmp_attr_t *attr = get_arm_CondJmp_attr_const(node);
437         return attr->proj_num;
438 }
439
440 /**
441  * Sets the proj num
442  */
443 void set_arm_CondJmp_proj_num(ir_node *node, int proj_num) {
444         arm_CondJmp_attr_t *attr = get_arm_CondJmp_attr(node);
445         attr->proj_num   = proj_num;
446 }
447
448 /**
449  * Returns the SymConst label
450  */
451 ident *get_arm_symconst_id(const ir_node *node) {
452         const arm_SymConst_attr_t *attr = get_arm_SymConst_attr_const(node);
453         return attr->symconst_id;
454 }
455
456 /**
457  * Sets the SymConst label
458  */
459 void set_arm_symconst_id(ir_node *node, ident *symconst_id) {
460         arm_SymConst_attr_t *attr = get_arm_SymConst_attr(node);
461         attr->symconst_id = symconst_id;
462 }
463
464 /**
465  * Returns the number of projs of a SwitchJmp.
466  */
467 int get_arm_SwitchJmp_n_projs(const ir_node *node) {
468         const arm_SwitchJmp_attr_t *attr = get_arm_SwitchJmp_attr_const(node);
469         return attr->n_projs;
470 }
471
472 /**
473  * Sets the number of projs.
474  */
475 void set_arm_SwitchJmp_n_projs(ir_node *node, int n_projs) {
476         arm_SwitchJmp_attr_t *attr = get_arm_SwitchJmp_attr(node);
477         attr->n_projs = n_projs;
478 }
479
480 /**
481  * Returns the default_proj_num.
482  */
483 long get_arm_SwitchJmp_default_proj_num(const ir_node *node) {
484         const arm_SwitchJmp_attr_t *attr = get_arm_SwitchJmp_attr_const(node);
485         return attr->default_proj_num;
486 }
487
488 /**
489  * Sets the default_proj_num.
490  */
491 void set_arm_SwitchJmp_default_proj_num(ir_node *node, long default_proj_num) {
492         arm_SwitchJmp_attr_t *attr = get_arm_SwitchJmp_attr(node);
493         attr->default_proj_num = default_proj_num;
494 }
495
496 /**
497  * Gets the shift modifier attribute.
498  */
499 arm_shift_modifier get_arm_shift_modifier(const ir_node *node) {
500         const arm_attr_t *attr = get_arm_attr_const(node);
501         return ARM_GET_SHF_MOD(attr);
502 }
503
504 /* Set the ARM machine node attributes to default values. */
505 static void init_arm_attributes(ir_node *node, int flags,
506                          const arch_register_req_t ** in_reqs,
507                                                  const arch_register_req_t ** out_reqs,
508                          const be_execution_unit_t ***execution_units,
509                                                  int n_res) {
510         ir_graph       *irg  = get_irn_irg(node);
511         struct obstack *obst = get_irg_obstack(irg);
512         arm_attr_t     *attr = get_arm_attr(node);
513         backend_info_t *info;
514         (void) execution_units;
515
516         arch_irn_set_flags(node, flags);
517         attr->in_req           = in_reqs;
518         attr->out_req          = out_reqs;
519         attr->instr_fl         = (ARM_COND_AL << 3) | ARM_SHF_NONE;
520         attr->imm_value        = 0;
521
522         info            = be_get_info(node);
523         info->out_infos = NEW_ARR_D(reg_out_info_t, obst, n_res);
524         memset(info->out_infos, 0, n_res * sizeof(info->out_infos[0]));
525 }
526
527 /************************************************
528  *   ___        _   _           _               *
529  *  / _ \ _ __ | |_(_)_ __ ___ (_)_______ _ __  *
530  * | | | | '_ \| __| | '_ ` _ \| |_  / _ \ '__| *
531  * | |_| | |_) | |_| | | | | | | |/ /  __/ |    *
532  *  \___/| .__/ \__|_|_| |_| |_|_/___\___|_|    *
533  *       |_|                                    *
534  ************************************************/
535
536 typedef struct _opt_tuple {
537         ir_op *op_imm_left;             /**< immediate is left */
538         ir_op *op_imm_right;    /**< immediate is right */
539         ir_op *op_shf_left;             /**< shift operand on left */
540         ir_op *op_shf_right;    /**< shift operand on right */
541 } opt_tuple;
542
543 //static const opt_tuple *opt_ops[iro_arm_last];
544
545 void arm_set_optimizers(void) {
546         /*
547 #define STD(op)         p_##op = { op_arm_##op##_i, op_arm_##op##_i, op_arm_##op, op_arm_##op }
548 #define LEFT(op)        p_##op = { op_arm_##op##_i, NULL, op_arm_##op, NULL }
549 #define SET(op)   opt_ops[iro_arm_##op] = &p_##op;
550
551         static const opt_tuple
552                 STD(Add),
553                 STD(And),
554                 STD(Or),
555                 STD(Eor),
556                 LEFT(Bic),
557                 LEFT(Shl),
558                 LEFT(Shr),
559                 LEFT(Shrs),
560                 p_Sub = { op_arm_Sub_i, op_arm_Rsb_i, op_arm_Sub, op_arm_Rsb },
561
562         memset(opt_ops, 0, sizeof(opt_ops));
563         SET(Add);
564         SET(And);
565         SET(Or);
566         SET(Eor);
567         SET(Sub);
568         SET(Bic);
569         SET(Shl);
570         SET(Shr);
571         SET(Shrs);
572         */
573 }
574
575 static int cmp_attr_arm(ir_node *a, ir_node *b) {
576         arm_attr_t *attr_a = get_irn_generic_attr(a);
577         arm_attr_t *attr_b = get_irn_generic_attr(b);
578         return (attr_a->instr_fl != attr_b->instr_fl) || (attr_a->imm_value != attr_b->imm_value);
579 }
580
581 static int cmp_attr_arm_SymConst(ir_node *a, ir_node *b) {
582         const arm_SymConst_attr_t *attr_a;
583         const arm_SymConst_attr_t *attr_b;
584
585         if (cmp_attr_arm(a, b))
586                 return 1;
587
588         attr_a = get_irn_generic_attr_const(a);
589         attr_b = get_irn_generic_attr_const(b);
590         return attr_a->symconst_id != attr_b->symconst_id;
591 }
592
593 static int cmp_attr_arm_CondJmp(ir_node *a, ir_node *b) {
594         (void) a;
595         (void) b;
596         /* never identical */
597         return 1;
598 }
599
600 static int cmp_attr_arm_SwitchJmp(ir_node *a, ir_node *b) {
601         (void) a;
602         (void) b;
603         /* never identical */
604         return 1;
605 }
606
607 static int cmp_attr_arm_fpaConst(ir_node *a, ir_node *b) {
608         const arm_fpaConst_attr_t *attr_a;
609         const arm_fpaConst_attr_t *attr_b;
610
611         if (cmp_attr_arm(a, b))
612                 return 1;
613
614         attr_a = get_arm_fpaConst_attr_const(a);
615         attr_b = get_arm_fpaConst_attr_const(b);
616
617         return attr_a->tv != attr_b->tv;
618 }
619
620 /** copies the ARM attributes of a node. */
621 static void arm_copy_attr(const ir_node *old_node, ir_node *new_node) {
622         ir_graph          *irg     = get_irn_irg(new_node);
623         struct obstack    *obst    = get_irg_obstack(irg);
624         const arm_attr_t *attr_old = get_arm_attr_const(old_node);
625         arm_attr_t       *attr_new = get_arm_attr(new_node);
626         backend_info_t    *old_info = be_get_info(old_node);
627         backend_info_t    *new_info = be_get_info(new_node);
628
629         /* copy the attributes */
630         memcpy(attr_new, attr_old, get_op_attr_size(get_irn_op(old_node)));
631
632         /* copy out flags */
633         new_info->out_infos =
634                 DUP_ARR_D(reg_out_info_t, obst, old_info->out_infos);
635 }
636
637
638
639 /* Include the generated constructor functions */
640 #include "gen_arm_new_nodes.c.inl"