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