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