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