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