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