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