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