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