- small cleanup
[libfirm] / ir / be / ia32 / ia32_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       Handling of ia32 specific firm opcodes.
23  * @author      Christian Wuerdig
24  * @version     $Id$
25  *
26  * This file implements the creation of the achitecture specific firm opcodes
27  * and the corresponding node constructors for the ia32 assembler irg.
28  */
29 #include "config.h"
30
31 #include <stdlib.h>
32
33 #include "irargs_t.h"
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 "iredges.h"
45 #include "error.h"
46 #include "raw_bitset.h"
47 #include "xmalloc.h"
48
49 #include "../bearch_t.h"
50 #include "../beinfo.h"
51
52 #include "bearch_ia32_t.h"
53 #include "ia32_common_transform.h"
54 #include "ia32_nodes_attr.h"
55 #include "ia32_new_nodes.h"
56 #include "gen_ia32_regalloc_if.h"
57 #include "gen_ia32_machine.h"
58
59 /***********************************************************************************
60  *      _                                   _       _             __
61  *     | |                                 (_)     | |           / _|
62  *   __| |_   _ _ __ ___  _ __   ___ _ __   _ _ __ | |_ ___ _ __| |_ __ _  ___ ___
63  *  / _` | | | | '_ ` _ \| '_ \ / _ \ '__| | | '_ \| __/ _ \ '__|  _/ _` |/ __/ _ \
64  * | (_| | |_| | | | | | | |_) |  __/ |    | | | | | ||  __/ |  | || (_| | (_|  __/
65  *  \__,_|\__,_|_| |_| |_| .__/ \___|_|    |_|_| |_|\__\___|_|  |_| \__,_|\___\___|
66  *                       | |
67  *                       |_|
68  ***********************************************************************************/
69
70 /**
71  * Dumps the register requirements for either in or out.
72  */
73 static void dump_reg_req(FILE *F, ir_node *n, const arch_register_req_t **reqs,
74                          int inout) {
75         char *dir = inout ? "out" : "in";
76         int   max = inout ? (int) arch_irn_get_n_outs(n) : get_irn_arity(n);
77         char  buf[1024];
78         int   i;
79
80         memset(buf, 0, sizeof(buf));
81
82         if (reqs) {
83                 for (i = 0; i < max; i++) {
84                         fprintf(F, "%sreq #%d =", dir, i);
85
86                         if (reqs[i]->type == arch_register_req_type_none) {
87                                 fprintf(F, " n/a");
88                         }
89
90                         if (reqs[i]->type & arch_register_req_type_normal) {
91                                 fprintf(F, " %s", reqs[i]->cls->name);
92                         }
93
94                         if (reqs[i]->type & arch_register_req_type_limited) {
95                                 fprintf(F, " %s",
96                                         arch_register_req_format(buf, sizeof(buf), reqs[i], n));
97                         }
98
99                         if (reqs[i]->type & arch_register_req_type_should_be_same) {
100                                 unsigned other = reqs[i]->other_same;
101                                 int i;
102
103                                 ir_fprintf(F, " same as");
104                                 for (i = 0; 1U << i <= other; ++i) {
105                                         if (other & (1U << i)) {
106                                                 ir_fprintf(F, " %+F", get_irn_n(n, i));
107                                         }
108                                 }
109                         }
110
111                         if (reqs[i]->type & arch_register_req_type_must_be_different) {
112                                 unsigned other = reqs[i]->other_different;
113                                 int i;
114
115                                 ir_fprintf(F, " different from");
116                                 for (i = 0; 1U << i <= other; ++i) {
117                                         if (other & (1U << i)) {
118                                                 ir_fprintf(F, " %+F", get_irn_n(n, i));
119                                         }
120                                 }
121                         }
122
123                         fprintf(F, "\n");
124                 }
125
126                 fprintf(F, "\n");
127         }
128         else {
129                 fprintf(F, "%sreq = N/A\n", dir);
130         }
131 }
132
133 /**
134  * Dumper interface for dumping ia32 nodes in vcg.
135  * @param n        the node to dump
136  * @param F        the output file
137  * @param reason   indicates which kind of information should be dumped
138  * @return 0 on success or != 0 on failure
139  */
140 static int ia32_dump_node(ir_node *n, FILE *F, dump_reason_t reason) {
141         ir_mode     *mode = NULL;
142         int          bad  = 0;
143         int          i, n_res, flags;
144         const arch_register_req_t **reqs;
145
146         switch (reason) {
147                 case dump_node_opcode_txt:
148                         fprintf(F, "%s", get_irn_opname(n));
149
150                         if(is_ia32_Immediate(n) || is_ia32_Const(n)) {
151                                 const ia32_immediate_attr_t *attr
152                                         = get_ia32_immediate_attr_const(n);
153
154                                 fputc(' ', F);
155                                 if(attr->symconst) {
156                                         if(attr->sc_sign) {
157                                                 fputc('-', F);
158                                         }
159                                         fputs(get_entity_name(attr->symconst), F);
160                                 }
161                                 if(attr->offset != 0 || attr->symconst == NULL) {
162                                         if(attr->offset > 0 && attr->symconst != NULL) {
163                                                 fputc('+', F);
164                                         }
165                                         fprintf(F, "%ld", attr->offset);
166                                 }
167                         }
168                         else {
169                                 const ia32_attr_t *attr = get_ia32_attr_const(n);
170
171                                 if(attr->am_sc != NULL || attr->am_offs != 0)
172                                         fputs(" [", F);
173
174                                 if(attr->am_sc != NULL) {
175                                         if(attr->data.am_sc_sign) {
176                                                 fputc('-', F);
177                                         }
178                                         fputs(get_entity_name(attr->am_sc), F);
179                                 }
180                                 if(attr->am_offs != 0) {
181                                         if(attr->am_offs > 0 && attr->am_sc != NULL) {
182                                                 fputc('+', F);
183                                         }
184                                         fprintf(F, "%d", attr->am_offs);
185                                 }
186
187                                 if(attr->am_sc != NULL || attr->am_offs != 0)
188                                         fputc(']', F);
189                         }
190                         break;
191
192                 case dump_node_mode_txt:
193                         mode = get_ia32_ls_mode(n);
194                         if (mode != NULL)
195                                 fprintf(F, "[%s]", get_mode_name(mode));
196                         break;
197
198                 case dump_node_nodeattr_txt:
199                         if (! is_ia32_Lea(n)) {
200                                 if (is_ia32_AddrModeS(n)) {
201                                         fprintf(F, "[AM S] ");
202                                 } else if (is_ia32_AddrModeD(n)) {
203                                         fprintf(F, "[AM D] ");
204                                 }
205                         }
206
207                         break;
208
209                 case dump_node_info_txt:
210                         fprintf(F, "=== IA32 attr begin ===\n");
211
212                         /* dump IN requirements */
213                         if (get_irn_arity(n) > 0) {
214                                 reqs = get_ia32_in_req_all(n);
215                                 dump_reg_req(F, n, reqs, 0);
216                         }
217
218                         n_res = arch_irn_get_n_outs(n);
219                         if (n_res > 0) {
220                                 /* dump OUT requirements */
221                                 reqs = get_ia32_out_req_all(n);
222                                 dump_reg_req(F, n, reqs, 1);
223
224                                 /* dump assigned registers */
225                                 for (i = 0; i < n_res; i++) {
226                                         const arch_register_t *reg = arch_irn_get_register(n, i);
227
228                                         fprintf(F, "reg #%d = %s\n", i, reg ? arch_register_get_name(reg) : "n/a");
229                                 }
230                                 fprintf(F, "\n");
231                         }
232
233                         /* dump op type */
234                         fprintf(F, "op = ");
235                         switch (get_ia32_op_type(n)) {
236                                 case ia32_Normal:
237                                         fprintf(F, "Normal");
238                                         break;
239                                 case ia32_AddrModeD:
240                                         fprintf(F, "AM Dest (Load+Store)");
241                                         break;
242                                 case ia32_AddrModeS:
243                                         fprintf(F, "AM Source (Load)");
244                                         break;
245                                 default:
246                                         fprintf(F, "unknown (%d)", get_ia32_op_type(n));
247                                         break;
248                         }
249                         fprintf(F, "\n");
250
251                         /* dump supported am */
252                         fprintf(F, "AM support = ");
253                         switch (get_ia32_am_support(n)) {
254                                 case ia32_am_none:   fputs("none\n",            F); break;
255                                 case ia32_am_unary:  fputs("source (unary)\n",  F); break;
256                                 case ia32_am_binary: fputs("source (binary)\n", F); break;
257
258                                 default:
259                                         fprintf(F, "unknown (%d)\n", get_ia32_am_support(n));
260                                         break;
261                         }
262
263                         /* dump AM offset */
264                         if(get_ia32_am_offs_int(n) != 0) {
265                                 fprintf(F, "AM offset = %d\n", get_ia32_am_offs_int(n));
266                         }
267
268                         /* dump AM symconst */
269                         if(get_ia32_am_sc(n) != NULL) {
270                                 ir_entity *ent = get_ia32_am_sc(n);
271                                 ident *id = get_entity_ld_ident(ent);
272                                 fprintf(F, "AM symconst = %s\n", get_id_str(id));
273                         }
274
275                         /* dump AM scale */
276                         fprintf(F, "AM scale = %d\n", get_ia32_am_scale(n));
277
278                         /* dump pn code */
279                         if (is_ia32_SwitchJmp(n)) {
280                                 fprintf(F, "pn_code = %ld\n", get_ia32_condcode(n));
281                         } else if (is_ia32_CMov(n) || is_ia32_Set(n) || is_ia32_Jcc(n)) {
282                                 ia32_attr_t *attr = get_ia32_attr(n);
283                                 long pnc = get_ia32_condcode(n);
284                                 fprintf(F, "pn_code = 0x%lX (%s)\n", pnc, get_pnc_string(pnc & pn_Cmp_True));
285                                 fprintf(F, "ins_permuted = %u \n", attr->data.ins_permuted);
286                                 fprintf(F, "cmp_unsigned = %u \n", attr->data.cmp_unsigned);
287                         }
288                         else if (is_ia32_CopyB(n) || is_ia32_CopyB_i(n)) {
289                                 fprintf(F, "size = %u\n", get_ia32_copyb_size(n));
290                         }
291
292                         fprintf(F, "n_res = %d\n",         n_res);
293                         fprintf(F, "use_frame = %d\n",     is_ia32_use_frame(n));
294                         fprintf(F, "commutative = %d\n",   is_ia32_commutative(n));
295                         fprintf(F, "need stackent = %d\n", is_ia32_need_stackent(n));
296                         fprintf(F, "is reload = %d\n",     is_ia32_is_reload(n));
297                         fprintf(F, "latency = %d\n",       get_ia32_latency(n));
298
299                         /* dump flags */
300                         fprintf(F, "flags =");
301                         flags = arch_irn_get_flags(n);
302                         if (flags == arch_irn_flags_none) {
303                                 fprintf(F, " none");
304                         }
305                         else {
306                                 if (flags & arch_irn_flags_dont_spill) {
307                                         fprintf(F, " unspillable");
308                                 }
309                                 if (flags & arch_irn_flags_rematerializable) {
310                                         fprintf(F, " remat");
311                                 }
312                                 if (flags & arch_irn_flags_modify_flags) {
313                                         fprintf(F, " modify_flags");
314                                 }
315                         }
316                         fprintf(F, " (%d)\n", flags);
317
318                         /* dump frame entity */
319                         fprintf(F, "frame entity = ");
320                         if (get_ia32_frame_ent(n)) {
321                                 ir_fprintf(F, "%+F", get_ia32_frame_ent(n));
322                         }
323                         else {
324                                 fprintf(F, "n/a");
325                         }
326                         fprintf(F, "\n");
327
328                         /* dump modes */
329                         fprintf(F, "ls_mode = ");
330                         if (get_ia32_ls_mode(n)) {
331                                 ir_fprintf(F, "%+F", get_ia32_ls_mode(n));
332                         }
333                         else {
334                                 fprintf(F, "n/a");
335                         }
336                         fprintf(F, "\n");
337
338 #ifndef NDEBUG
339                         /* dump original ir node name */
340                         fprintf(F, "orig node = ");
341                         if (get_ia32_orig_node(n)) {
342                                 fprintf(F, "%s", get_ia32_orig_node(n));
343                         }
344                         else {
345                                 fprintf(F, "n/a");
346                         }
347                         fprintf(F, "\n");
348 #endif /* NDEBUG */
349
350                         fprintf(F, "=== IA32 attr end ===\n");
351                         /* end of: case dump_node_info_txt */
352                         break;
353         }
354
355         return bad;
356 }
357
358
359
360 /***************************************************************************************************
361  *        _   _                   _       __        _                    _   _               _
362  *       | | | |                 | |     / /       | |                  | | | |             | |
363  *   __ _| |_| |_ _ __   ___  ___| |_   / /_ _  ___| |_   _ __ ___   ___| |_| |__   ___   __| |___
364  *  / _` | __| __| '__| / __|/ _ \ __| / / _` |/ _ \ __| | '_ ` _ \ / _ \ __| '_ \ / _ \ / _` / __|
365  * | (_| | |_| |_| |    \__ \  __/ |_ / / (_| |  __/ |_  | | | | | |  __/ |_| | | | (_) | (_| \__ \
366  *  \__,_|\__|\__|_|    |___/\___|\__/_/ \__, |\___|\__| |_| |_| |_|\___|\__|_| |_|\___/ \__,_|___/
367  *                                        __/ |
368  *                                       |___/
369  ***************************************************************************************************/
370
371 ia32_attr_t *get_ia32_attr(ir_node *node) {
372         assert(is_ia32_irn(node) && "need ia32 node to get ia32 attributes");
373         return (ia32_attr_t *)get_irn_generic_attr(node);
374 }
375
376 const ia32_attr_t *get_ia32_attr_const(const ir_node *node) {
377         assert(is_ia32_irn(node) && "need ia32 node to get ia32 attributes");
378         return (const ia32_attr_t*) get_irn_generic_attr_const(node);
379 }
380
381 ia32_x87_attr_t *get_ia32_x87_attr(ir_node *node) {
382         ia32_attr_t     *attr     = get_ia32_attr(node);
383         ia32_x87_attr_t *x87_attr = CAST_IA32_ATTR(ia32_x87_attr_t, attr);
384         return x87_attr;
385 }
386
387 const ia32_x87_attr_t *get_ia32_x87_attr_const(const ir_node *node) {
388         const ia32_attr_t     *attr     = get_ia32_attr_const(node);
389         const ia32_x87_attr_t *x87_attr = CONST_CAST_IA32_ATTR(ia32_x87_attr_t, attr);
390         return x87_attr;
391 }
392
393 const ia32_asm_attr_t *get_ia32_asm_attr_const(const ir_node *node) {
394         const ia32_attr_t     *attr     = get_ia32_attr_const(node);
395         const ia32_asm_attr_t *asm_attr = CONST_CAST_IA32_ATTR(ia32_asm_attr_t, attr);
396
397         return asm_attr;
398 }
399
400 ia32_immediate_attr_t *get_ia32_immediate_attr(ir_node *node) {
401         ia32_attr_t           *attr      = get_ia32_attr(node);
402         ia32_immediate_attr_t *imm_attr  = CAST_IA32_ATTR(ia32_immediate_attr_t, attr);
403
404         return imm_attr;
405 }
406
407 const ia32_immediate_attr_t *get_ia32_immediate_attr_const(const ir_node *node)
408 {
409         const ia32_attr_t           *attr     = get_ia32_attr_const(node);
410         const ia32_immediate_attr_t *imm_attr = CONST_CAST_IA32_ATTR(ia32_immediate_attr_t, attr);
411
412         return imm_attr;
413 }
414
415 ia32_condcode_attr_t *get_ia32_condcode_attr(ir_node *node) {
416         ia32_attr_t          *attr    = get_ia32_attr(node);
417         ia32_condcode_attr_t *cc_attr = CAST_IA32_ATTR(ia32_condcode_attr_t, attr);
418
419         return cc_attr;
420 }
421
422 const ia32_condcode_attr_t *get_ia32_condcode_attr_const(const ir_node *node) {
423         const ia32_attr_t          *attr    = get_ia32_attr_const(node);
424         const ia32_condcode_attr_t *cc_attr = CONST_CAST_IA32_ATTR(ia32_condcode_attr_t, attr);
425
426         return cc_attr;
427 }
428
429 ia32_call_attr_t *get_ia32_call_attr(ir_node *node)
430 {
431         ia32_attr_t      *attr      = get_ia32_attr(node);
432         ia32_call_attr_t *call_attr = CAST_IA32_ATTR(ia32_call_attr_t, attr);
433
434         return call_attr;
435 }
436
437 const ia32_call_attr_t *get_ia32_call_attr_const(const ir_node *node)
438 {
439         const ia32_attr_t      *attr      = get_ia32_attr_const(node);
440         const ia32_call_attr_t *call_attr = CONST_CAST_IA32_ATTR(ia32_call_attr_t, attr);
441
442         return call_attr;
443 }
444
445 ia32_copyb_attr_t *get_ia32_copyb_attr(ir_node *node) {
446         ia32_attr_t       *attr       = get_ia32_attr(node);
447         ia32_copyb_attr_t *copyb_attr = CAST_IA32_ATTR(ia32_copyb_attr_t, attr);
448
449         return copyb_attr;
450 }
451
452 const ia32_copyb_attr_t *get_ia32_copyb_attr_const(const ir_node *node) {
453         const ia32_attr_t       *attr       = get_ia32_attr_const(node);
454         const ia32_copyb_attr_t *copyb_attr = CONST_CAST_IA32_ATTR(ia32_copyb_attr_t, attr);
455
456         return copyb_attr;
457 }
458
459 ia32_climbframe_attr_t *get_ia32_climbframe_attr(ir_node *node) {
460         ia32_attr_t            *attr            = get_ia32_attr(node);
461         ia32_climbframe_attr_t *climbframe_attr = CAST_IA32_ATTR(ia32_climbframe_attr_t, attr);
462
463         return climbframe_attr;
464 }
465
466 const ia32_climbframe_attr_t *get_ia32_climbframe_attr_const(const ir_node *node) {
467         const ia32_attr_t            *attr            = get_ia32_attr_const(node);
468         const ia32_climbframe_attr_t *climbframe_attr = CONST_CAST_IA32_ATTR(ia32_climbframe_attr_t, attr);
469
470         return climbframe_attr;
471 }
472
473 /**
474  * Gets the type of an ia32 node.
475  */
476 ia32_op_type_t get_ia32_op_type(const ir_node *node) {
477         const ia32_attr_t *attr = get_ia32_attr_const(node);
478         return attr->data.tp;
479 }
480
481 /**
482  * Sets the type of an ia32 node.
483  */
484 void set_ia32_op_type(ir_node *node, ia32_op_type_t tp) {
485         ia32_attr_t *attr = get_ia32_attr(node);
486         attr->data.tp     = tp;
487 }
488
489 ia32_am_type_t get_ia32_am_support(const ir_node *node)
490 {
491         const ia32_attr_t *attr = get_ia32_attr_const(node);
492         return attr->data.am_arity;
493 }
494
495 /**
496  * Sets the supported address mode of an ia32 node
497  */
498 void set_ia32_am_support(ir_node *node, ia32_am_type_t arity)
499 {
500         ia32_attr_t *attr   = get_ia32_attr(node);
501         attr->data.am_arity = arity;
502 }
503
504 /**
505  * Gets the address mode offset as int.
506  */
507 int get_ia32_am_offs_int(const ir_node *node) {
508         const ia32_attr_t *attr = get_ia32_attr_const(node);
509         return attr->am_offs;
510 }
511
512 /**
513  * Sets the address mode offset from an int.
514  */
515 void set_ia32_am_offs_int(ir_node *node, int offset) {
516         ia32_attr_t *attr = get_ia32_attr(node);
517         attr->am_offs = offset;
518 }
519
520 void add_ia32_am_offs_int(ir_node *node, int offset) {
521         ia32_attr_t *attr = get_ia32_attr(node);
522         attr->am_offs += offset;
523 }
524
525 /**
526  * Returns the symconst entity associated to address mode.
527  */
528 ir_entity *get_ia32_am_sc(const ir_node *node) {
529         const ia32_attr_t *attr = get_ia32_attr_const(node);
530         return attr->am_sc;
531 }
532
533 /**
534  * Sets the symconst entity associated to address mode.
535  */
536 void set_ia32_am_sc(ir_node *node, ir_entity *entity) {
537         ia32_attr_t *attr = get_ia32_attr(node);
538         attr->am_sc       = entity;
539 }
540
541 /**
542  * Sets the sign bit for address mode symconst.
543  */
544 void set_ia32_am_sc_sign(ir_node *node) {
545         ia32_attr_t *attr     = get_ia32_attr(node);
546         attr->data.am_sc_sign = 1;
547 }
548
549 /**
550  * Clears the sign bit for address mode symconst.
551  */
552 void clear_ia32_am_sc_sign(ir_node *node) {
553         ia32_attr_t *attr     = get_ia32_attr(node);
554         attr->data.am_sc_sign = 0;
555 }
556
557 /**
558  * Returns the sign bit for address mode symconst.
559  */
560 int is_ia32_am_sc_sign(const ir_node *node) {
561         const ia32_attr_t *attr = get_ia32_attr_const(node);
562         return attr->data.am_sc_sign;
563 }
564
565 /**
566  * Gets the addr mode const.
567  */
568 unsigned get_ia32_am_scale(const ir_node *node) {
569         const ia32_attr_t *attr = get_ia32_attr_const(node);
570         return attr->data.am_scale;
571 }
572
573 /**
574  * Sets the index register scale for address mode.
575  */
576 void set_ia32_am_scale(ir_node *node, unsigned scale) {
577         ia32_attr_t *attr = get_ia32_attr(node);
578         assert(scale <= 3 && "AM scale out of range [0 ... 3]");
579         attr->data.am_scale = scale;
580 }
581
582 void ia32_copy_am_attrs(ir_node *to, const ir_node *from)
583 {
584         set_ia32_ls_mode(to, get_ia32_ls_mode(from));
585         set_ia32_am_scale(to, get_ia32_am_scale(from));
586         set_ia32_am_sc(to, get_ia32_am_sc(from));
587         if(is_ia32_am_sc_sign(from))
588                 set_ia32_am_sc_sign(to);
589         add_ia32_am_offs_int(to, get_ia32_am_offs_int(from));
590         set_ia32_frame_ent(to, get_ia32_frame_ent(from));
591         if (is_ia32_use_frame(from))
592                 set_ia32_use_frame(to);
593 }
594
595 /**
596  * Sets the uses_frame flag.
597  */
598 void set_ia32_use_frame(ir_node *node) {
599         ia32_attr_t *attr    = get_ia32_attr(node);
600         attr->data.use_frame = 1;
601 }
602
603 /**
604  * Clears the uses_frame flag.
605  */
606 void clear_ia32_use_frame(ir_node *node) {
607         ia32_attr_t *attr    = get_ia32_attr(node);
608         attr->data.use_frame = 0;
609 }
610
611 /**
612  * Gets the uses_frame flag.
613  */
614 int is_ia32_use_frame(const ir_node *node) {
615         const ia32_attr_t *attr = get_ia32_attr_const(node);
616         return attr->data.use_frame;
617 }
618
619 /**
620  * Sets node to commutative.
621  */
622 void set_ia32_commutative(ir_node *node) {
623         ia32_attr_t *attr         = get_ia32_attr(node);
624         attr->data.is_commutative = 1;
625 }
626
627 /**
628  * Sets node to non-commutative.
629  */
630 void clear_ia32_commutative(ir_node *node) {
631         ia32_attr_t *attr         = get_ia32_attr(node);
632         attr->data.is_commutative = 0;
633 }
634
635 /**
636  * Checks if node is commutative.
637  */
638 int is_ia32_commutative(const ir_node *node) {
639         const ia32_attr_t *attr = get_ia32_attr_const(node);
640         return attr->data.is_commutative;
641 }
642
643 void set_ia32_need_stackent(ir_node *node) {
644         ia32_attr_t *attr     = get_ia32_attr(node);
645         attr->data.need_stackent = 1;
646 }
647
648 void clear_ia32_need_stackent(ir_node *node) {
649         ia32_attr_t *attr     = get_ia32_attr(node);
650         attr->data.need_stackent = 0;
651 }
652
653 int is_ia32_need_stackent(const ir_node *node) {
654         const ia32_attr_t *attr = get_ia32_attr_const(node);
655         return attr->data.need_stackent;
656 }
657
658 void set_ia32_is_reload(ir_node *node) {
659         ia32_attr_t *attr = get_ia32_attr(node);
660         attr->data.is_reload = 1;
661 }
662
663 int is_ia32_is_reload(const ir_node *node) {
664         const ia32_attr_t *attr = get_ia32_attr_const(node);
665         return attr->data.is_reload;
666 }
667
668 void set_ia32_is_spill(ir_node *node) {
669         ia32_attr_t *attr = get_ia32_attr(node);
670         attr->data.is_spill = 1;
671 }
672
673 int is_ia32_is_spill(const ir_node *node) {
674         const ia32_attr_t *attr = get_ia32_attr_const(node);
675         return attr->data.is_spill;
676 }
677
678 void set_ia32_is_remat(ir_node *node) {
679         ia32_attr_t *attr = get_ia32_attr(node);
680         attr->data.is_remat = 1;
681 }
682
683 int is_ia32_is_remat(const ir_node *node) {
684         const ia32_attr_t *attr = get_ia32_attr_const(node);
685         return attr->data.is_remat;
686 }
687
688 /**
689  * Gets the mode of the stored/loaded value (only set for Store/Load)
690  */
691 ir_mode *get_ia32_ls_mode(const ir_node *node) {
692         const ia32_attr_t *attr = get_ia32_attr_const(node);
693         return attr->ls_mode;
694 }
695
696 /**
697  * Sets the mode of the stored/loaded value (only set for Store/Load)
698  */
699 void set_ia32_ls_mode(ir_node *node, ir_mode *mode) {
700         ia32_attr_t *attr = get_ia32_attr(node);
701         attr->ls_mode     = mode;
702 }
703
704 /**
705  * Gets the frame entity assigned to this node.
706  */
707 ir_entity *get_ia32_frame_ent(const ir_node *node) {
708         const ia32_attr_t *attr = get_ia32_attr_const(node);
709         return attr->frame_ent;
710 }
711
712 /**
713  * Sets the frame entity for this node.
714  */
715 void set_ia32_frame_ent(ir_node *node, ir_entity *ent) {
716         ia32_attr_t *attr = get_ia32_attr(node);
717         attr->frame_ent   = ent;
718         if(ent != NULL)
719                 set_ia32_use_frame(node);
720         else
721                 clear_ia32_use_frame(node);
722 }
723
724
725 /**
726  * Gets the instruction latency.
727  */
728 unsigned get_ia32_latency(const ir_node *node) {
729         const ir_op *op               = get_irn_op(node);
730         const ia32_op_attr_t *op_attr = (ia32_op_attr_t*) get_op_attr(op);
731         return op_attr->latency;
732 }
733
734 /**
735  * Returns the argument register requirements of an ia32 node.
736  */
737 const arch_register_req_t **get_ia32_in_req_all(const ir_node *node) {
738         const ia32_attr_t *attr = get_ia32_attr_const(node);
739         return attr->in_req;
740 }
741
742 /**
743  * Sets the argument register requirements of an ia32 node.
744  */
745 void set_ia32_in_req_all(ir_node *node, const arch_register_req_t **reqs) {
746         ia32_attr_t *attr = get_ia32_attr(node);
747         attr->in_req      = reqs;
748 }
749
750 /**
751  * Returns the result register requirements of an ia32 node.
752  */
753 const arch_register_req_t **get_ia32_out_req_all(const ir_node *node) {
754         const ia32_attr_t *attr = get_ia32_attr_const(node);
755         return attr->out_req;
756 }
757
758 /**
759  * Sets the result register requirements of an ia32 node.
760  */
761 void set_ia32_out_req_all(ir_node *node, const arch_register_req_t **reqs) {
762         ia32_attr_t *attr = get_ia32_attr(node);
763         attr->out_req     = reqs;
764 }
765
766 /**
767  * Returns the argument register requirement at position pos of an ia32 node.
768  */
769 const arch_register_req_t *get_ia32_in_req(const ir_node *node, int pos) {
770         const ia32_attr_t *attr = get_ia32_attr_const(node);
771         if(attr->in_req == NULL)
772                 return arch_no_register_req;
773
774         return attr->in_req[pos];
775 }
776
777 /**
778  * Returns the result register requirement at position pos of an ia32 node.
779  */
780 const arch_register_req_t *get_ia32_out_req(const ir_node *node, int pos) {
781         const ia32_attr_t *attr = get_ia32_attr_const(node);
782         if(attr->out_req == NULL)
783                 return arch_no_register_req;
784
785         return attr->out_req[pos];
786 }
787
788 /**
789  * Sets the OUT register requirements at position pos.
790  */
791 void set_ia32_req_out(ir_node *node, const arch_register_req_t *req, int pos) {
792         ia32_attr_t *attr  = get_ia32_attr(node);
793         attr->out_req[pos] = req;
794 }
795
796 /**
797  * Sets the IN register requirements at position pos.
798  */
799 void set_ia32_req_in(ir_node *node, const arch_register_req_t *req, int pos) {
800         ia32_attr_t *attr = get_ia32_attr(node);
801         attr->in_req[pos] = req;
802 }
803
804 /**
805  * Returns the condition code of a node.
806  */
807 long get_ia32_condcode(const ir_node *node)
808 {
809         const ia32_condcode_attr_t *attr = get_ia32_condcode_attr_const(node);
810         return attr->pn_code;
811 }
812
813 /**
814  * Sets the condition code of a node
815  */
816 void set_ia32_condcode(ir_node *node, long code)
817 {
818         ia32_condcode_attr_t *attr = get_ia32_condcode_attr(node);
819         attr->pn_code = code;
820 }
821
822 /**
823  * Returns the condition code of a node.
824  */
825 unsigned get_ia32_copyb_size(const ir_node *node)
826 {
827         const ia32_copyb_attr_t *attr = get_ia32_copyb_attr_const(node);
828         return attr->size;
829 }
830
831 /**
832  * Get the list of available execution units.
833  */
834 const be_execution_unit_t ***get_ia32_exec_units(const ir_node *node) {
835         const ia32_attr_t *attr = get_ia32_attr_const(node);
836         return attr->exec_units;
837 }
838
839 /**
840  * Get the exception label attribute.
841  */
842 unsigned get_ia32_exc_label(const ir_node *node) {
843         const ia32_attr_t *attr = get_ia32_attr_const(node);
844         return attr->data.has_except_label;
845 }
846
847 /**
848  * Set the exception label attribute.
849  */
850 void set_ia32_exc_label(ir_node *node, unsigned flag) {
851         ia32_attr_t *attr = get_ia32_attr(node);
852         attr->data.has_except_label = flag;
853 }
854
855 /**
856  * Return the exception label id.
857  */
858 ir_label_t get_ia32_exc_label_id(const ir_node *node) {
859         const ia32_attr_t *attr = get_ia32_attr_const(node);
860
861         assert(attr->data.has_except_label);
862         return attr->exc_label;
863 }
864
865 /**
866  * Assign the exception label id.
867  */
868 void set_ia32_exc_label_id(ir_node *node, ir_label_t id) {
869         ia32_attr_t *attr = get_ia32_attr(node);
870
871         assert(attr->data.has_except_label);
872         attr->exc_label = id;
873 }
874
875 #ifndef NDEBUG
876
877 /**
878  * Returns the name of the original ir node.
879  */
880 const char *get_ia32_orig_node(const ir_node *node)
881 {
882         const ia32_attr_t *attr = get_ia32_attr_const(node);
883         return attr->orig_node;
884 }
885
886 static const char *ia32_get_old_node_name(const ir_node *irn)
887 {
888         struct obstack *obst = env_cg->isa->name_obst;
889
890         lc_eoprintf(firm_get_arg_env(), obst, "%+F", irn);
891         obstack_1grow(obst, 0);
892         return obstack_finish(obst);
893 }
894
895 /**
896  * Sets the name of the original ir node.
897  */
898 void set_ia32_orig_node(ir_node *node, const ir_node *old)
899 {
900         const char  *name = ia32_get_old_node_name(old);
901         ia32_attr_t *attr = get_ia32_attr(node);
902         attr->orig_node   = name;
903 }
904
905 #endif /* NDEBUG */
906
907 /******************************************************************************************************
908  *                      _       _         _   _           __                  _   _
909  *                     (_)     | |       | | | |         / _|                | | (_)
910  *  ___ _ __   ___  ___ _  __ _| |   __ _| |_| |_ _ __  | |_ _   _ _ __   ___| |_ _  ___  _ __    ___
911  * / __| '_ \ / _ \/ __| |/ _` | |  / _` | __| __| '__| |  _| | | | '_ \ / __| __| |/ _ \| '_ \  / __|
912  * \__ \ |_) |  __/ (__| | (_| | | | (_| | |_| |_| |    | | | |_| | | | | (__| |_| | (_) | | | | \__ \
913  * |___/ .__/ \___|\___|_|\__,_|_|  \__,_|\__|\__|_|    |_|  \__,_|_| |_|\___|\__|_|\___/|_| |_| |___/
914  *     | |
915  *     |_|
916  ******************************************************************************************************/
917
918 /**
919  * Returns whether or not the node is an AddrModeS node.
920  */
921 int is_ia32_AddrModeS(const ir_node *node) {
922         const ia32_attr_t *attr = get_ia32_attr_const(node);
923         return (attr->data.tp == ia32_AddrModeS);
924 }
925
926 /**
927  * Returns whether or not the node is an AddrModeD node.
928  */
929 int is_ia32_AddrModeD(const ir_node *node) {
930         const ia32_attr_t *attr = get_ia32_attr_const(node);
931         return (attr->data.tp == ia32_AddrModeD);
932 }
933
934 void ia32_swap_left_right(ir_node *node)
935 {
936         ia32_attr_t *attr  = get_ia32_attr(node);
937         ir_node     *left  = get_irn_n(node, n_ia32_binary_left);
938         ir_node     *right = get_irn_n(node, n_ia32_binary_right);
939
940         assert(is_ia32_commutative(node));
941         attr->data.ins_permuted = !attr->data.ins_permuted;
942         set_irn_n(node, n_ia32_binary_left,  right);
943         set_irn_n(node, n_ia32_binary_right, left);
944 }
945
946 /**
947  * Initializes the nodes attributes.
948  */
949 void init_ia32_attributes(ir_node *node, arch_irn_flags_t flags,
950                           const arch_register_req_t **in_reqs,
951                           const arch_register_req_t **out_reqs,
952                           const be_execution_unit_t ***execution_units,
953                           int n_res)
954 {
955         ir_graph        *irg  = get_irn_irg(node);
956         struct obstack  *obst = get_irg_obstack(irg);
957         ia32_attr_t     *attr = get_ia32_attr(node);
958         backend_info_t  *info;
959
960         arch_irn_set_flags(node, flags);
961         set_ia32_in_req_all(node, in_reqs);
962         set_ia32_out_req_all(node, out_reqs);
963
964         attr->exec_units  = execution_units;
965 #ifndef NDEBUG
966         attr->attr_type  |= IA32_ATTR_ia32_attr_t;
967 #endif
968
969         info            = be_get_info(node);
970         info->out_infos = NEW_ARR_D(reg_out_info_t, obst, n_res);
971         memset(info->out_infos, 0, n_res * sizeof(info->out_infos[0]));
972 }
973
974 void
975 init_ia32_x87_attributes(ir_node *res)
976 {
977 #ifndef NDEBUG
978         ia32_attr_t *attr  = get_ia32_attr(res);
979         attr->attr_type   |= IA32_ATTR_ia32_x87_attr_t;
980 #else
981         (void) res;
982 #endif
983         ia32_current_cg->do_x87_sim = 1;
984 }
985
986 void
987 init_ia32_asm_attributes(ir_node *res)
988 {
989 #ifndef NDEBUG
990         ia32_attr_t *attr  = get_ia32_attr(res);
991         attr->attr_type   |= IA32_ATTR_ia32_asm_attr_t;
992 #else
993         (void) res;
994 #endif
995 }
996
997 void
998 init_ia32_immediate_attributes(ir_node *res, ir_entity *symconst,
999                                int symconst_sign, long offset)
1000 {
1001         ia32_immediate_attr_t *attr = get_irn_generic_attr(res);
1002
1003 #ifndef NDEBUG
1004         attr->attr.attr_type  |= IA32_ATTR_ia32_immediate_attr_t;
1005 #endif
1006         attr->symconst = symconst;
1007         attr->sc_sign  = symconst_sign;
1008         attr->offset   = offset;
1009 }
1010
1011 void init_ia32_call_attributes(ir_node* res, unsigned pop, ir_type* call_tp)
1012 {
1013         ia32_call_attr_t *attr = get_irn_generic_attr(res);
1014
1015 #ifndef NDEBUG
1016         attr->attr.attr_type  |= IA32_ATTR_ia32_call_attr_t;
1017 #endif
1018         attr->pop     = pop;
1019         attr->call_tp = call_tp;
1020 }
1021
1022 void
1023 init_ia32_copyb_attributes(ir_node *res, unsigned size) {
1024         ia32_copyb_attr_t *attr = get_irn_generic_attr(res);
1025
1026 #ifndef NDEBUG
1027         attr->attr.attr_type  |= IA32_ATTR_ia32_copyb_attr_t;
1028 #endif
1029         attr->size = size;
1030 }
1031
1032 void
1033 init_ia32_condcode_attributes(ir_node *res, long pnc) {
1034         ia32_condcode_attr_t *attr = get_irn_generic_attr(res);
1035
1036 #ifndef NDEBUG
1037         attr->attr.attr_type  |= IA32_ATTR_ia32_condcode_attr_t;
1038 #endif
1039         attr->pn_code = pnc;
1040 }
1041
1042 void
1043 init_ia32_climbframe_attributes(ir_node *res, unsigned count) {
1044         ia32_climbframe_attr_t *attr = get_irn_generic_attr(res);
1045
1046 #ifndef NDEBUG
1047         attr->attr.attr_type  |= IA32_ATTR_ia32_climbframe_attr_t;
1048 #endif
1049         attr->count = count;
1050 }
1051
1052 /***************************************************************************************
1053  *                  _                            _                   _
1054  *                 | |                          | |                 | |
1055  *  _ __   ___   __| | ___    ___ ___  _ __  ___| |_ _ __ _   _  ___| |_ ___  _ __ ___
1056  * | '_ \ / _ \ / _` |/ _ \  / __/ _ \| '_ \/ __| __| '__| | | |/ __| __/ _ \| '__/ __|
1057  * | | | | (_) | (_| |  __/ | (_| (_) | | | \__ \ |_| |  | |_| | (__| || (_) | |  \__ \
1058  * |_| |_|\___/ \__,_|\___|  \___\___/|_| |_|___/\__|_|   \__,_|\___|\__\___/|_|  |___/
1059  *
1060  ***************************************************************************************/
1061
1062 /* default compare operation to compare attributes */
1063 int ia32_compare_attr(const ia32_attr_t *a, const ia32_attr_t *b)
1064 {
1065         if (a->data.tp != b->data.tp)
1066                 return 1;
1067
1068         if (a->data.am_scale != b->data.am_scale
1069             || a->data.am_sc_sign != b->data.am_sc_sign
1070             || a->am_offs != b->am_offs
1071             || a->am_sc != b->am_sc
1072             || a->ls_mode != b->ls_mode)
1073                 return 1;
1074
1075         /* nodes with not yet assigned entities shouldn't be CSEd (important for
1076          * unsigned int -> double conversions */
1077         if(a->data.use_frame && a->frame_ent == NULL)
1078                 return 1;
1079         if(b->data.use_frame && b->frame_ent == NULL)
1080                 return 1;
1081
1082         if (a->data.use_frame != b->data.use_frame
1083             || a->frame_ent != b->frame_ent)
1084                 return 1;
1085
1086         if (a->data.has_except_label != b->data.has_except_label)
1087                 return 1;
1088
1089         if (a->data.ins_permuted != b->data.ins_permuted
1090                         || a->data.cmp_unsigned != b->data.cmp_unsigned)
1091                 return 1;
1092
1093         return 0;
1094 }
1095
1096 /** Compare nodes attributes for all "normal" nodes. */
1097 static
1098 int ia32_compare_nodes_attr(ir_node *a, ir_node *b)
1099 {
1100         const ia32_attr_t* attr_a = get_ia32_attr_const(a);
1101         const ia32_attr_t* attr_b = get_ia32_attr_const(b);
1102
1103         return ia32_compare_attr(attr_a, attr_b);
1104 }
1105
1106 /** Compare node attributes for nodes with condition code. */
1107 static
1108 int ia32_compare_condcode_attr(ir_node *a, ir_node *b)
1109 {
1110         const ia32_condcode_attr_t *attr_a;
1111         const ia32_condcode_attr_t *attr_b;
1112
1113         if (ia32_compare_nodes_attr(a, b))
1114                 return 1;
1115
1116         attr_a = get_ia32_condcode_attr_const(a);
1117         attr_b = get_ia32_condcode_attr_const(b);
1118
1119         if (attr_a->pn_code != attr_b->pn_code)
1120                 return 1;
1121
1122         return 0;
1123 }
1124
1125 /** Compare node attributes for call nodes. */
1126 static int ia32_compare_call_attr(ir_node *a, ir_node *b)
1127 {
1128         const ia32_call_attr_t *attr_a;
1129         const ia32_call_attr_t *attr_b;
1130
1131         if (ia32_compare_nodes_attr(a, b))
1132                 return 1;
1133
1134         attr_a = get_ia32_call_attr_const(a);
1135         attr_b = get_ia32_call_attr_const(b);
1136
1137         if (attr_a->pop != attr_b->pop)
1138                 return 1;
1139
1140         if (attr_a->call_tp != attr_b->call_tp)
1141                 return 1;
1142
1143         return 0;
1144 }
1145
1146 /** Compare node attributes for CopyB nodes. */
1147 static
1148 int ia32_compare_copyb_attr(ir_node *a, ir_node *b)
1149 {
1150         const ia32_copyb_attr_t *attr_a;
1151         const ia32_copyb_attr_t *attr_b;
1152
1153         if (ia32_compare_nodes_attr(a, b))
1154                 return 1;
1155
1156         attr_a = get_ia32_copyb_attr_const(a);
1157         attr_b = get_ia32_copyb_attr_const(b);
1158
1159         if (attr_a->size != attr_b->size)
1160                 return 1;
1161
1162         return 0;
1163 }
1164
1165
1166 /** Compare ASM node attributes. */
1167 static
1168 int ia32_compare_asm_attr(ir_node *a, ir_node *b)
1169 {
1170         const ia32_asm_attr_t *attr_a;
1171         const ia32_asm_attr_t *attr_b;
1172
1173         if (ia32_compare_nodes_attr(a, b))
1174                 return 1;
1175
1176         attr_a = get_ia32_asm_attr_const(a);
1177         attr_b = get_ia32_asm_attr_const(b);
1178
1179         if(attr_a->asm_text != attr_b->asm_text)
1180                 return 1;
1181
1182         return 0;
1183 }
1184
1185 /**
1186  * Hash function for Immediates
1187  */
1188 static unsigned ia32_hash_Immediate(const ir_node *irn) {
1189         const ia32_immediate_attr_t *a = get_ia32_immediate_attr_const(irn);
1190
1191         return HASH_PTR(a->symconst) + (a->sc_sign << 16) + a->offset;
1192 }
1193
1194 /** Compare node attributes for Immediates. */
1195 static
1196 int ia32_compare_immediate_attr(ir_node *a, ir_node *b)
1197 {
1198         const ia32_immediate_attr_t *attr_a = get_ia32_immediate_attr_const(a);
1199         const ia32_immediate_attr_t *attr_b = get_ia32_immediate_attr_const(b);
1200
1201         if (attr_a->symconst != attr_b->symconst ||
1202             attr_a->sc_sign != attr_b->sc_sign ||
1203             attr_a->offset != attr_b->offset)
1204                 return 1;
1205
1206         return 0;
1207 }
1208
1209 /** Compare node attributes for x87 nodes. */
1210 static
1211 int ia32_compare_x87_attr(ir_node *a, ir_node *b)
1212 {
1213         return ia32_compare_nodes_attr(a, b);
1214 }
1215
1216 /** Compare node attributes for ClimbFrame nodes. */
1217 static
1218 int ia32_compare_climbframe_attr(ir_node *a, ir_node *b)
1219 {
1220         const ia32_climbframe_attr_t *attr_a;
1221         const ia32_climbframe_attr_t *attr_b;
1222
1223         if (ia32_compare_nodes_attr(a, b))
1224                 return 1;
1225
1226         attr_a = get_ia32_climbframe_attr_const(a);
1227         attr_b = get_ia32_climbframe_attr_const(b);
1228
1229         if (attr_a->count != attr_b->count)
1230                 return 1;
1231
1232         return 0;
1233 }
1234
1235 /* copies the ia32 attributes */
1236 static void ia32_copy_attr(const ir_node *old_node, ir_node *new_node)
1237 {
1238         ir_graph          *irg      = get_irn_irg(new_node);
1239         struct obstack    *obst     = get_irg_obstack(irg);
1240         const ia32_attr_t *attr_old = get_ia32_attr_const(old_node);
1241         ia32_attr_t       *attr_new = get_ia32_attr(new_node);
1242         backend_info_t    *old_info = be_get_info(old_node);
1243         backend_info_t    *new_info = be_get_info(new_node);
1244
1245         /* copy the attributes */
1246         memcpy(attr_new, attr_old, get_op_attr_size(get_irn_op(old_node)));
1247
1248         /* copy out flags */
1249         new_info->out_infos =
1250                 DUP_ARR_D(reg_out_info_t, obst, old_info->out_infos);
1251 }
1252
1253 /* Include the generated constructor functions */
1254 #include "gen_ia32_new_nodes.c.inl"