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