Use backtracking in find_original_value() for the flags emitter.
[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_must_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                                 ia32_attr_t *attr = get_ia32_attr(n);
292                                 long pnc = get_ia32_condcode(n);
293                                 fprintf(F, "pn_code = 0x%lX (%s)\n", pnc, get_pnc_string(pnc & pn_Cmp_True));
294                                 fprintf(F, "ins_permuted = %u \n", attr->data.ins_permuted);
295                                 fprintf(F, "cmp_unsigned = %u \n", attr->data.cmp_unsigned);
296                         }
297                         else if (is_ia32_CopyB(n) || is_ia32_CopyB_i(n)) {
298                                 fprintf(F, "size = %u\n", get_ia32_copyb_size(n));
299                         }
300
301                         /* dump n_res */
302                         fprintf(F, "n_res = %d\n", get_ia32_n_res(n));
303
304                         /* dump use_frame */
305                         fprintf(F, "use_frame = %d\n", is_ia32_use_frame(n));
306
307                         /* commutative */
308                         fprintf(F, "commutative = %d\n", is_ia32_commutative(n));
309
310                         /* need stackent */
311                         fprintf(F, "need stackent = %d\n", is_ia32_need_stackent(n));
312
313                         /* dump latency */
314                         fprintf(F, "latency = %d\n", get_ia32_latency(n));
315
316                         /* dump flags */
317                         fprintf(F, "flags =");
318                         flags = get_ia32_flags(n);
319                         if (flags == arch_irn_flags_none) {
320                                 fprintf(F, " none");
321                         }
322                         else {
323                                 if (flags & arch_irn_flags_dont_spill) {
324                                         fprintf(F, " unspillable");
325                                 }
326                                 if (flags & arch_irn_flags_rematerializable) {
327                                         fprintf(F, " remat");
328                                 }
329                                 if (flags & arch_irn_flags_ignore) {
330                                         fprintf(F, " ignore");
331                                 }
332                                 if (flags & arch_irn_flags_modify_sp) {
333                                         fprintf(F, " modify_sp");
334                                 }
335                                 if (flags & arch_irn_flags_modify_flags) {
336                                         fprintf(F, " modify_flags");
337                                 }
338                         }
339                         fprintf(F, " (%d)\n", flags);
340
341                         /* dump frame entity */
342                         fprintf(F, "frame entity = ");
343                         if (get_ia32_frame_ent(n)) {
344                                 ir_fprintf(F, "%+F", get_ia32_frame_ent(n));
345                         }
346                         else {
347                                 fprintf(F, "n/a");
348                         }
349                         fprintf(F, "\n");
350
351                         /* dump modes */
352                         fprintf(F, "ls_mode = ");
353                         if (get_ia32_ls_mode(n)) {
354                                 ir_fprintf(F, "%+F", get_ia32_ls_mode(n));
355                         }
356                         else {
357                                 fprintf(F, "n/a");
358                         }
359                         fprintf(F, "\n");
360
361 #ifndef NDEBUG
362                         /* dump original ir node name */
363                         fprintf(F, "orig node = ");
364                         if (get_ia32_orig_node(n)) {
365                                 fprintf(F, "%s", get_ia32_orig_node(n));
366                         }
367                         else {
368                                 fprintf(F, "n/a");
369                         }
370                         fprintf(F, "\n");
371 #endif /* NDEBUG */
372
373                         fprintf(F, "=== IA32 attr end ===\n");
374                         /* end of: case dump_node_info_txt */
375                         break;
376         }
377
378         return bad;
379 }
380
381
382
383 /***************************************************************************************************
384  *        _   _                   _       __        _                    _   _               _
385  *       | | | |                 | |     / /       | |                  | | | |             | |
386  *   __ _| |_| |_ _ __   ___  ___| |_   / /_ _  ___| |_   _ __ ___   ___| |_| |__   ___   __| |___
387  *  / _` | __| __| '__| / __|/ _ \ __| / / _` |/ _ \ __| | '_ ` _ \ / _ \ __| '_ \ / _ \ / _` / __|
388  * | (_| | |_| |_| |    \__ \  __/ |_ / / (_| |  __/ |_  | | | | | |  __/ |_| | | | (_) | (_| \__ \
389  *  \__,_|\__|\__|_|    |___/\___|\__/_/ \__, |\___|\__| |_| |_| |_|\___|\__|_| |_|\___/ \__,_|___/
390  *                                        __/ |
391  *                                       |___/
392  ***************************************************************************************************/
393
394 ia32_attr_t *get_ia32_attr(ir_node *node) {
395         assert(is_ia32_irn(node) && "need ia32 node to get ia32 attributes");
396         return (ia32_attr_t *)get_irn_generic_attr(node);
397 }
398
399 const ia32_attr_t *get_ia32_attr_const(const ir_node *node) {
400         assert(is_ia32_irn(node) && "need ia32 node to get ia32 attributes");
401         return (const ia32_attr_t*) get_irn_generic_attr_const(node);
402 }
403
404 ia32_x87_attr_t *get_ia32_x87_attr(ir_node *node) {
405         ia32_attr_t     *attr     = get_ia32_attr(node);
406         ia32_x87_attr_t *x87_attr = CAST_IA32_ATTR(ia32_x87_attr_t, attr);
407         return x87_attr;
408 }
409
410 const ia32_x87_attr_t *get_ia32_x87_attr_const(const ir_node *node) {
411         const ia32_attr_t     *attr     = get_ia32_attr_const(node);
412         const ia32_x87_attr_t *x87_attr = CONST_CAST_IA32_ATTR(ia32_x87_attr_t, attr);
413         return x87_attr;
414 }
415
416 const ia32_asm_attr_t *get_ia32_asm_attr_const(const ir_node *node) {
417         const ia32_attr_t     *attr     = get_ia32_attr_const(node);
418         const ia32_asm_attr_t *asm_attr = CONST_CAST_IA32_ATTR(ia32_asm_attr_t, attr);
419
420         return asm_attr;
421 }
422
423 ia32_immediate_attr_t *get_ia32_immediate_attr(ir_node *node) {
424         ia32_attr_t           *attr      = get_ia32_attr(node);
425         ia32_immediate_attr_t *imm_attr  = CAST_IA32_ATTR(ia32_immediate_attr_t, attr);
426
427         return imm_attr;
428 }
429
430 const ia32_immediate_attr_t *get_ia32_immediate_attr_const(const ir_node *node)
431 {
432         const ia32_attr_t           *attr     = get_ia32_attr_const(node);
433         const ia32_immediate_attr_t *imm_attr = CONST_CAST_IA32_ATTR(ia32_immediate_attr_t, attr);
434
435         return imm_attr;
436 }
437
438 ia32_condcode_attr_t *get_ia32_condcode_attr(ir_node *node) {
439         ia32_attr_t          *attr    = get_ia32_attr(node);
440         ia32_condcode_attr_t *cc_attr = CAST_IA32_ATTR(ia32_condcode_attr_t, attr);
441
442         return cc_attr;
443 }
444
445 const ia32_condcode_attr_t *get_ia32_condcode_attr_const(const ir_node *node) {
446         const ia32_attr_t          *attr    = get_ia32_attr_const(node);
447         const ia32_condcode_attr_t *cc_attr = CONST_CAST_IA32_ATTR(ia32_condcode_attr_t, attr);
448
449         return cc_attr;
450 }
451
452 ia32_copyb_attr_t *get_ia32_copyb_attr(ir_node *node) {
453         ia32_attr_t       *attr       = get_ia32_attr(node);
454         ia32_copyb_attr_t *copyb_attr = CAST_IA32_ATTR(ia32_copyb_attr_t, attr);
455
456         return copyb_attr;
457 }
458
459 const ia32_copyb_attr_t *get_ia32_copyb_attr_const(const ir_node *node) {
460         const ia32_attr_t       *attr       = get_ia32_attr_const(node);
461         const ia32_copyb_attr_t *copyb_attr = CONST_CAST_IA32_ATTR(ia32_copyb_attr_t, attr);
462
463         return copyb_attr;
464 }
465
466 /**
467  * Gets the type of an ia32 node.
468  */
469 ia32_op_type_t get_ia32_op_type(const ir_node *node) {
470         const ia32_attr_t *attr = get_ia32_attr_const(node);
471         return attr->data.tp;
472 }
473
474 /**
475  * Sets the type of an ia32 node.
476  */
477 void set_ia32_op_type(ir_node *node, ia32_op_type_t tp) {
478         ia32_attr_t *attr = get_ia32_attr(node);
479         attr->data.tp     = tp;
480 }
481
482 /**
483  * Gets the supported address mode of an ia32 node
484  */
485 ia32_am_type_t get_ia32_am_support(const ir_node *node) {
486         const ia32_attr_t *attr = get_ia32_attr_const(node);
487         return attr->data.am_support;
488 }
489
490 ia32_am_arity_t get_ia32_am_arity(const ir_node *node) {
491         const ia32_attr_t *attr = get_ia32_attr_const(node);
492         return attr->data.am_arity;
493 }
494
495 /**
496  * Sets the supported address mode of an ia32 node
497  */
498 void set_ia32_am_support(ir_node *node, ia32_am_type_t am_tp,
499                          ia32_am_arity_t arity) {
500         ia32_attr_t *attr     = get_ia32_attr(node);
501         attr->data.am_support = am_tp;
502         attr->data.am_arity   = arity;
503
504         assert((am_tp == ia32_am_None && arity == ia32_am_arity_none) ||
505                (am_tp != ia32_am_None &&
506                ((arity == ia32_am_unary) || (arity == ia32_am_binary) || (arity == ia32_am_ternary))));
507 }
508
509 /**
510  * Gets the address mode offset as int.
511  */
512 int get_ia32_am_offs_int(const ir_node *node) {
513         const ia32_attr_t *attr = get_ia32_attr_const(node);
514         return attr->am_offs;
515 }
516
517 /**
518  * Sets the address mode offset from an int.
519  */
520 void set_ia32_am_offs_int(ir_node *node, int offset) {
521         ia32_attr_t *attr = get_ia32_attr(node);
522         attr->am_offs = offset;
523 }
524
525 void add_ia32_am_offs_int(ir_node *node, int offset) {
526         ia32_attr_t *attr = get_ia32_attr(node);
527         attr->am_offs += offset;
528 }
529
530 /**
531  * Returns the symconst entity associated to address mode.
532  */
533 ir_entity *get_ia32_am_sc(const ir_node *node) {
534         const ia32_attr_t *attr = get_ia32_attr_const(node);
535         return attr->am_sc;
536 }
537
538 /**
539  * Sets the symconst entity associated to address mode.
540  */
541 void set_ia32_am_sc(ir_node *node, ir_entity *entity) {
542         ia32_attr_t *attr = get_ia32_attr(node);
543         attr->am_sc       = entity;
544 }
545
546 /**
547  * Sets the sign bit for address mode symconst.
548  */
549 void set_ia32_am_sc_sign(ir_node *node) {
550         ia32_attr_t *attr     = get_ia32_attr(node);
551         attr->data.am_sc_sign = 1;
552 }
553
554 /**
555  * Clears the sign bit for address mode symconst.
556  */
557 void clear_ia32_am_sc_sign(ir_node *node) {
558         ia32_attr_t *attr     = get_ia32_attr(node);
559         attr->data.am_sc_sign = 0;
560 }
561
562 /**
563  * Returns the sign bit for address mode symconst.
564  */
565 int is_ia32_am_sc_sign(const ir_node *node) {
566         const ia32_attr_t *attr = get_ia32_attr_const(node);
567         return attr->data.am_sc_sign;
568 }
569
570 /**
571  * Gets the addr mode const.
572  */
573 int get_ia32_am_scale(const ir_node *node) {
574         const ia32_attr_t *attr = get_ia32_attr_const(node);
575         return attr->data.am_scale;
576 }
577
578 /**
579  * Sets the index register scale for address mode.
580  */
581 void set_ia32_am_scale(ir_node *node, int scale) {
582         ia32_attr_t *attr = get_ia32_attr(node);
583         assert(0 <= scale && scale < 4 && "AM scale out of range");
584         attr->data.am_scale = scale;
585 }
586
587 void ia32_copy_am_attrs(ir_node *to, const ir_node *from)
588 {
589         set_ia32_ls_mode(to, get_ia32_ls_mode(from));
590         set_ia32_am_scale(to, get_ia32_am_scale(from));
591         set_ia32_am_sc(to, get_ia32_am_sc(from));
592         if(is_ia32_am_sc_sign(from))
593                 set_ia32_am_sc_sign(to);
594         add_ia32_am_offs_int(to, get_ia32_am_offs_int(from));
595         set_ia32_frame_ent(to, get_ia32_frame_ent(from));
596         if (is_ia32_use_frame(from))
597                 set_ia32_use_frame(to);
598 }
599
600 /**
601  * Sets the uses_frame flag.
602  */
603 void set_ia32_use_frame(ir_node *node) {
604         ia32_attr_t *attr    = get_ia32_attr(node);
605         attr->data.use_frame = 1;
606 }
607
608 /**
609  * Clears the uses_frame flag.
610  */
611 void clear_ia32_use_frame(ir_node *node) {
612         ia32_attr_t *attr    = get_ia32_attr(node);
613         attr->data.use_frame = 0;
614 }
615
616 /**
617  * Gets the uses_frame flag.
618  */
619 int is_ia32_use_frame(const ir_node *node) {
620         const ia32_attr_t *attr = get_ia32_attr_const(node);
621         return attr->data.use_frame;
622 }
623
624 /**
625  * Sets node to commutative.
626  */
627 void set_ia32_commutative(ir_node *node) {
628         ia32_attr_t *attr         = get_ia32_attr(node);
629         attr->data.is_commutative = 1;
630 }
631
632 /**
633  * Sets node to non-commutative.
634  */
635 void clear_ia32_commutative(ir_node *node) {
636         ia32_attr_t *attr         = get_ia32_attr(node);
637         attr->data.is_commutative = 0;
638 }
639
640 /**
641  * Checks if node is commutative.
642  */
643 int is_ia32_commutative(const ir_node *node) {
644         const ia32_attr_t *attr = get_ia32_attr_const(node);
645         return attr->data.is_commutative;
646 }
647
648 void set_ia32_need_stackent(ir_node *node) {
649         ia32_attr_t *attr     = get_ia32_attr(node);
650         attr->data.need_stackent = 1;
651 }
652
653 void clear_ia32_need_stackent(ir_node *node) {
654         ia32_attr_t *attr     = get_ia32_attr(node);
655         attr->data.need_stackent = 0;
656 }
657
658 int is_ia32_need_stackent(const ir_node *node) {
659         const ia32_attr_t *attr = get_ia32_attr_const(node);
660         return attr->data.need_stackent;
661 }
662
663 /**
664  * Gets the mode of the stored/loaded value (only set for Store/Load)
665  */
666 ir_mode *get_ia32_ls_mode(const ir_node *node) {
667         const ia32_attr_t *attr = get_ia32_attr_const(node);
668         return attr->ls_mode;
669 }
670
671 /**
672  * Sets the mode of the stored/loaded value (only set for Store/Load)
673  */
674 void set_ia32_ls_mode(ir_node *node, ir_mode *mode) {
675         ia32_attr_t *attr = get_ia32_attr(node);
676         attr->ls_mode     = mode;
677 }
678
679 /**
680  * Gets the frame entity assigned to this node.
681  */
682 ir_entity *get_ia32_frame_ent(const ir_node *node) {
683         const ia32_attr_t *attr = get_ia32_attr_const(node);
684         return attr->frame_ent;
685 }
686
687 /**
688  * Sets the frame entity for this node.
689  */
690 void set_ia32_frame_ent(ir_node *node, ir_entity *ent) {
691         ia32_attr_t *attr = get_ia32_attr(node);
692         attr->frame_ent   = ent;
693         if(ent != NULL)
694                 set_ia32_use_frame(node);
695         else
696                 clear_ia32_use_frame(node);
697 }
698
699
700 /**
701  * Gets the instruction latency.
702  */
703 unsigned get_ia32_latency(const ir_node *node) {
704         const ir_op *op               = get_irn_op(node);
705         const ia32_op_attr_t *op_attr = (ia32_op_attr_t*) get_op_attr(op);
706         return op_attr->latency;
707 }
708
709 /**
710  * Returns the argument register requirements of an ia32 node.
711  */
712 const arch_register_req_t **get_ia32_in_req_all(const ir_node *node) {
713         const ia32_attr_t *attr = get_ia32_attr_const(node);
714         return attr->in_req;
715 }
716
717 /**
718  * Sets the argument register requirements of an ia32 node.
719  */
720 void set_ia32_in_req_all(ir_node *node, const arch_register_req_t **reqs) {
721         ia32_attr_t *attr = get_ia32_attr(node);
722         attr->in_req      = reqs;
723 }
724
725 /**
726  * Returns the result register requirements of an ia32 node.
727  */
728 const arch_register_req_t **get_ia32_out_req_all(const ir_node *node) {
729         const ia32_attr_t *attr = get_ia32_attr_const(node);
730         return attr->out_req;
731 }
732
733 /**
734  * Sets the result register requirements of an ia32 node.
735  */
736 void set_ia32_out_req_all(ir_node *node, const arch_register_req_t **reqs) {
737         ia32_attr_t *attr = get_ia32_attr(node);
738         attr->out_req     = reqs;
739 }
740
741 /**
742  * Returns the argument register requirement at position pos of an ia32 node.
743  */
744 const arch_register_req_t *get_ia32_in_req(const ir_node *node, int pos) {
745         const ia32_attr_t *attr = get_ia32_attr_const(node);
746         if(attr->in_req == NULL)
747                 return arch_no_register_req;
748
749         return attr->in_req[pos];
750 }
751
752 /**
753  * Returns the result register requirement at position pos of an ia32 node.
754  */
755 const arch_register_req_t *get_ia32_out_req(const ir_node *node, int pos) {
756         const ia32_attr_t *attr = get_ia32_attr_const(node);
757         if(attr->out_req == NULL)
758                 return arch_no_register_req;
759
760         return attr->out_req[pos];
761 }
762
763 /**
764  * Sets the OUT register requirements at position pos.
765  */
766 void set_ia32_req_out(ir_node *node, const arch_register_req_t *req, int pos) {
767         ia32_attr_t *attr  = get_ia32_attr(node);
768         attr->out_req[pos] = req;
769 }
770
771 /**
772  * Sets the IN register requirements at position pos.
773  */
774 void set_ia32_req_in(ir_node *node, const arch_register_req_t *req, int pos) {
775         ia32_attr_t *attr = get_ia32_attr(node);
776         attr->in_req[pos] = req;
777 }
778
779 /**
780  * Returns the register flag of an ia32 node.
781  */
782 arch_irn_flags_t get_ia32_flags(const ir_node *node) {
783         const ia32_attr_t *attr = get_ia32_attr_const(node);
784         return attr->data.flags;
785 }
786
787 /**
788  * Sets the register flag of an ia32 node.
789  */
790 void set_ia32_flags(ir_node *node, arch_irn_flags_t flags) {
791         ia32_attr_t *attr = get_ia32_attr(node);
792         attr->data.flags  = flags;
793 }
794
795 void add_ia32_flags(ir_node *node, arch_irn_flags_t flags) {
796         ia32_attr_t *attr  = get_ia32_attr(node);
797         attr->data.flags  |= flags;
798 }
799
800 /**
801  * Returns the result register slots of an ia32 node.
802  */
803 const arch_register_t **get_ia32_slots(const ir_node *node) {
804         const ia32_attr_t *attr = get_ia32_attr_const(node);
805         return attr->slots;
806 }
807
808 /**
809  * Returns the number of results.
810  */
811 int get_ia32_n_res(const ir_node *node) {
812         const ia32_attr_t *attr = get_ia32_attr_const(node);
813         return ARR_LEN(attr->slots);
814 }
815
816 /**
817  * Returns the condition code of a node.
818  */
819 long get_ia32_condcode(const ir_node *node)
820 {
821         const ia32_condcode_attr_t *attr = get_ia32_condcode_attr_const(node);
822         return attr->pn_code;
823 }
824
825 /**
826  * Sets the condition code of a node
827  */
828 void set_ia32_condcode(ir_node *node, long code)
829 {
830         ia32_condcode_attr_t *attr = get_ia32_condcode_attr(node);
831         attr->pn_code = code;
832 }
833
834 /**
835  * Returns the condition code of a node.
836  */
837 unsigned get_ia32_copyb_size(const ir_node *node)
838 {
839         const ia32_copyb_attr_t *attr = get_ia32_copyb_attr_const(node);
840         return attr->size;
841 }
842
843 /**
844  * Sets the flags for the n'th out.
845  */
846 void set_ia32_out_flags(ir_node *node, arch_irn_flags_t flags, int pos) {
847         ia32_attr_t *attr = get_ia32_attr(node);
848         assert(pos < ARR_LEN(attr->out_flags) && "Invalid OUT position.");
849         attr->out_flags[pos] = flags;
850 }
851
852 /**
853  * Gets the flags for the n'th out.
854  */
855 arch_irn_flags_t get_ia32_out_flags(const ir_node *node, int pos) {
856         const ia32_attr_t *attr = get_ia32_attr_const(node);
857         assert(pos < ARR_LEN(attr->out_flags) && "Invalid OUT position.");
858         return attr->out_flags[pos];
859 }
860
861 /**
862  * Get the list of available execution units.
863  */
864 const be_execution_unit_t ***get_ia32_exec_units(const ir_node *node) {
865         const ia32_attr_t *attr = get_ia32_attr_const(node);
866         return attr->exec_units;
867 }
868
869 /**
870  * Get the exception label attribute.
871  */
872 unsigned get_ia32_exc_label(const ir_node *node) {
873         const ia32_attr_t *attr = get_ia32_attr_const(node);
874         return attr->data.has_except_label;
875 }
876
877 /**
878  * Set the exception label attribute.
879  */
880 void set_ia32_exc_label(ir_node *node, unsigned flag) {
881         ia32_attr_t *attr = get_ia32_attr(node);
882         attr->data.has_except_label = flag;
883 }
884
885 /**
886  * Return the exception label id.
887  */
888 ir_label_t get_ia32_exc_label_id(const ir_node *node) {
889         const ia32_attr_t *attr = get_ia32_attr_const(node);
890
891         assert(attr->data.has_except_label);
892         return attr->exc_label;
893 }
894
895 /**
896  * Assign the exception label id.
897  */
898 void set_ia32_exc_label_id(ir_node *node, ir_label_t id) {
899         ia32_attr_t *attr = get_ia32_attr(node);
900
901         assert(attr->data.has_except_label);
902         attr->exc_label = id;
903 }
904
905 #ifndef NDEBUG
906
907 /**
908  * Returns the name of the original ir node.
909  */
910 const char *get_ia32_orig_node(const ir_node *node) {
911         const ia32_attr_t *attr = get_ia32_attr_const(node);
912         return attr->orig_node;
913 }
914
915 /**
916  * Sets the name of the original ir node.
917  */
918 void set_ia32_orig_node(ir_node *node, const char *name) {
919         ia32_attr_t *attr = get_ia32_attr(node);
920         attr->orig_node   = name;
921 }
922
923 #endif /* NDEBUG */
924
925 /******************************************************************************************************
926  *                      _       _         _   _           __                  _   _
927  *                     (_)     | |       | | | |         / _|                | | (_)
928  *  ___ _ __   ___  ___ _  __ _| |   __ _| |_| |_ _ __  | |_ _   _ _ __   ___| |_ _  ___  _ __    ___
929  * / __| '_ \ / _ \/ __| |/ _` | |  / _` | __| __| '__| |  _| | | | '_ \ / __| __| |/ _ \| '_ \  / __|
930  * \__ \ |_) |  __/ (__| | (_| | | | (_| | |_| |_| |    | | | |_| | | | | (__| |_| | (_) | | | | \__ \
931  * |___/ .__/ \___|\___|_|\__,_|_|  \__,_|\__|\__|_|    |_|  \__,_|_| |_|\___|\__|_|\___/|_| |_| |___/
932  *     | |
933  *     |_|
934  ******************************************************************************************************/
935
936 /**
937  * Returns whether or not the node is an AddrModeS node.
938  */
939 int is_ia32_AddrModeS(const ir_node *node) {
940         const ia32_attr_t *attr = get_ia32_attr_const(node);
941         return (attr->data.tp == ia32_AddrModeS);
942 }
943
944 /**
945  * Returns whether or not the node is an AddrModeD node.
946  */
947 int is_ia32_AddrModeD(const ir_node *node) {
948         const ia32_attr_t *attr = get_ia32_attr_const(node);
949         return (attr->data.tp == ia32_AddrModeD);
950 }
951
952 /**
953  * Checks if node is a Load or xLoad/vfLoad.
954  */
955 int is_ia32_Ld(const ir_node *node) {
956         int op = get_ia32_irn_opcode(node);
957         return op == iro_ia32_Load ||
958                op == iro_ia32_xLoad ||
959                op == iro_ia32_vfld ||
960                op == iro_ia32_fld;
961 }
962
963 /**
964  * Checks if node is a Store or xStore/vfStore.
965  */
966 int is_ia32_St(const ir_node *node) {
967         int op = get_ia32_irn_opcode(node);
968         return op == iro_ia32_Store ||
969                op == iro_ia32_Store8Bit ||
970                op == iro_ia32_xStore ||
971                op == iro_ia32_vfst ||
972                op == iro_ia32_fst ||
973                op == iro_ia32_fstp;
974 }
975
976 /**
977  * Returns the name of the OUT register at position pos.
978  */
979 const char *get_ia32_out_reg_name(const ir_node *node, int pos) {
980         const ia32_attr_t *attr = get_ia32_attr_const(node);
981
982         assert(pos < ARR_LEN(attr->slots) && "Invalid OUT position.");
983         assert(attr->slots[pos]  && "No register assigned");
984
985         return arch_register_get_name(attr->slots[pos]);
986 }
987
988 /**
989  * Returns the index of the OUT register at position pos within its register class.
990  */
991 int get_ia32_out_regnr(const ir_node *node, int pos) {
992         const ia32_attr_t *attr = get_ia32_attr_const(node);
993
994         assert(pos < ARR_LEN(attr->slots) && "Invalid OUT position.");
995         assert(attr->slots[pos]  && "No register assigned");
996
997         return arch_register_get_index(attr->slots[pos]);
998 }
999
1000 void ia32_swap_left_right(ir_node *node)
1001 {
1002         ia32_attr_t *attr  = get_ia32_attr(node);
1003         ir_node     *left  = get_irn_n(node, n_ia32_binary_left);
1004         ir_node     *right = get_irn_n(node, n_ia32_binary_right);
1005
1006         assert(is_ia32_commutative(node));
1007         attr->data.ins_permuted = !attr->data.ins_permuted;
1008         set_irn_n(node, n_ia32_binary_left,  right);
1009         set_irn_n(node, n_ia32_binary_right, left);
1010 }
1011
1012 /**
1013  * Returns the OUT register at position pos.
1014  */
1015 const arch_register_t *get_ia32_out_reg(const ir_node *node, int pos) {
1016         const ia32_attr_t *attr = get_ia32_attr_const(node);
1017
1018         assert(pos < ARR_LEN(attr->slots) && "Invalid OUT position.");
1019         assert(attr->slots[pos]  && "No register assigned");
1020
1021         return attr->slots[pos];
1022 }
1023
1024 /**
1025  * Initializes the nodes attributes.
1026  */
1027 void init_ia32_attributes(ir_node *node, arch_irn_flags_t flags,
1028                           const arch_register_req_t **in_reqs,
1029                           const arch_register_req_t **out_reqs,
1030                           const be_execution_unit_t ***execution_units,
1031                           int n_res)
1032 {
1033         ir_graph        *irg  = get_irn_irg(node);
1034         struct obstack  *obst = get_irg_obstack(irg);
1035         ia32_attr_t     *attr = get_ia32_attr(node);
1036
1037         set_ia32_flags(node, flags);
1038         set_ia32_in_req_all(node, in_reqs);
1039         set_ia32_out_req_all(node, out_reqs);
1040
1041         attr->exec_units  = execution_units;
1042 #ifndef NDEBUG
1043         attr->attr_type  |= IA32_ATTR_ia32_attr_t;
1044 #endif
1045
1046         attr->out_flags = NEW_ARR_D(int, obst, n_res);
1047         memset(attr->out_flags, 0, n_res * sizeof(attr->out_flags[0]));
1048
1049         attr->slots = NEW_ARR_D(const arch_register_t*, obst, n_res);
1050         /* void* cast to suppress an incorrect warning on MSVC */
1051         memset((void*)attr->slots, 0, n_res * sizeof(attr->slots[0]));
1052 }
1053
1054 void
1055 init_ia32_x87_attributes(ir_node *res)
1056 {
1057 #ifndef NDEBUG
1058         ia32_attr_t *attr  = get_ia32_attr(res);
1059         attr->attr_type   |= IA32_ATTR_ia32_x87_attr_t;
1060 #else
1061         (void) res;
1062 #endif
1063         ia32_current_cg->do_x87_sim = 1;
1064 }
1065
1066 void
1067 init_ia32_asm_attributes(ir_node *res)
1068 {
1069 #ifndef NDEBUG
1070         ia32_attr_t *attr  = get_ia32_attr(res);
1071         attr->attr_type   |= IA32_ATTR_ia32_asm_attr_t;
1072 #else
1073         (void) res;
1074 #endif
1075 }
1076
1077 void
1078 init_ia32_immediate_attributes(ir_node *res, ir_entity *symconst,
1079                                int symconst_sign, long offset)
1080 {
1081         ia32_immediate_attr_t *attr = get_irn_generic_attr(res);
1082
1083 #ifndef NDEBUG
1084         attr->attr.attr_type  |= IA32_ATTR_ia32_immediate_attr_t;
1085 #endif
1086         attr->symconst = symconst;
1087         attr->sc_sign  = symconst_sign;
1088         attr->offset   = offset;
1089 }
1090
1091 void
1092 init_ia32_copyb_attributes(ir_node *res, unsigned size) {
1093         ia32_copyb_attr_t *attr = get_irn_generic_attr(res);
1094
1095 #ifndef NDEBUG
1096         attr->attr.attr_type  |= IA32_ATTR_ia32_copyb_attr_t;
1097 #endif
1098         attr->size = size;
1099 }
1100
1101 void
1102 init_ia32_condcode_attributes(ir_node *res, long pnc) {
1103         ia32_condcode_attr_t *attr = get_irn_generic_attr(res);
1104
1105 #ifndef NDEBUG
1106         attr->attr.attr_type  |= IA32_ATTR_ia32_condcode_attr_t;
1107 #endif
1108         attr->pn_code = pnc;
1109 }
1110
1111 ir_node *get_ia32_result_proj(const ir_node *node)
1112 {
1113         const ir_edge_t *edge;
1114
1115         foreach_out_edge(node, edge) {
1116                 ir_node *proj = get_edge_src_irn(edge);
1117                 if(get_Proj_proj(proj) == 0) {
1118                         return proj;
1119                 }
1120         }
1121         return NULL;
1122 }
1123
1124 /***************************************************************************************
1125  *                  _                            _                   _
1126  *                 | |                          | |                 | |
1127  *  _ __   ___   __| | ___    ___ ___  _ __  ___| |_ _ __ _   _  ___| |_ ___  _ __ ___
1128  * | '_ \ / _ \ / _` |/ _ \  / __/ _ \| '_ \/ __| __| '__| | | |/ __| __/ _ \| '__/ __|
1129  * | | | | (_) | (_| |  __/ | (_| (_) | | | \__ \ |_| |  | |_| | (__| || (_) | |  \__ \
1130  * |_| |_|\___/ \__,_|\___|  \___\___/|_| |_|___/\__|_|   \__,_|\___|\__\___/|_|  |___/
1131  *
1132  ***************************************************************************************/
1133
1134 /* default compare operation to compare attributes */
1135 int ia32_compare_attr(const ia32_attr_t *a, const ia32_attr_t *b) {
1136         if (a->data.tp != b->data.tp)
1137                 return 1;
1138
1139         if (a->data.am_scale != b->data.am_scale
1140             || a->data.am_sc_sign != b->data.am_sc_sign
1141             || a->am_offs != b->am_offs
1142             || a->am_sc != b->am_sc
1143             || a->ls_mode != b->ls_mode)
1144                 return 1;
1145
1146         /* nodes with not yet assigned entities shouldn't be CSEd (important for
1147          * unsigned int -> double conversions */
1148         if(a->data.use_frame && a->frame_ent == NULL)
1149                 return 1;
1150         if(b->data.use_frame && b->frame_ent == NULL)
1151                 return 1;
1152
1153         if (a->data.use_frame != b->data.use_frame
1154             || a->frame_ent != b->frame_ent)
1155                 return 1;
1156
1157         if (a->data.tp != b->data.tp)
1158                 return 1;
1159
1160         if (a->data.has_except_label != b->data.has_except_label)
1161                 return 1;
1162
1163         if (a->data.ins_permuted != b->data.ins_permuted
1164                         || a->data.cmp_unsigned != b->data.cmp_unsigned)
1165                 return 1;
1166
1167         return 0;
1168 }
1169
1170 /** Compare nodes attributes for all "normal" nodes. */
1171 static
1172 int ia32_compare_nodes_attr(ir_node *a, ir_node *b)
1173 {
1174         const ia32_attr_t* attr_a = get_ia32_attr_const(a);
1175         const ia32_attr_t* attr_b = get_ia32_attr_const(b);
1176
1177         return ia32_compare_attr(attr_a, attr_b);
1178 }
1179
1180 /** Compare node attributes for nodes with condition code. */
1181 static
1182 int ia32_compare_condcode_attr(ir_node *a, ir_node *b)
1183 {
1184         const ia32_condcode_attr_t *attr_a;
1185         const ia32_condcode_attr_t *attr_b;
1186
1187         if (ia32_compare_nodes_attr(a, b))
1188                 return 1;
1189
1190         attr_a = get_ia32_condcode_attr_const(a);
1191         attr_b = get_ia32_condcode_attr_const(b);
1192
1193         if(attr_a->pn_code != attr_b->pn_code)
1194                 return 1;
1195
1196         return 0;
1197 }
1198
1199 /** Compare node attributes for CopyB nodes. */
1200 static
1201 int ia32_compare_copyb_attr(ir_node *a, ir_node *b)
1202 {
1203         const ia32_copyb_attr_t *attr_a;
1204         const ia32_copyb_attr_t *attr_b;
1205
1206         if (ia32_compare_nodes_attr(a, b))
1207                 return 1;
1208
1209         attr_a = get_ia32_copyb_attr_const(a);
1210         attr_b = get_ia32_copyb_attr_const(b);
1211
1212         if(attr_a->size != attr_b->size)
1213                 return 1;
1214
1215         return 0;
1216 }
1217
1218
1219 /** Compare ASM node attributes. */
1220 static
1221 int ia32_compare_asm_attr(ir_node *a, ir_node *b)
1222 {
1223         const ia32_asm_attr_t *attr_a;
1224         const ia32_asm_attr_t *attr_b;
1225
1226         if(ia32_compare_nodes_attr(a, b))
1227                 return 1;
1228
1229         attr_a = get_ia32_asm_attr_const(a);
1230         attr_b = get_ia32_asm_attr_const(b);
1231
1232         if(attr_a->asm_text != attr_b->asm_text)
1233                 return 1;
1234
1235         return 0;
1236 }
1237
1238 /**
1239  * Hash function for Immediates
1240  */
1241 static unsigned ia32_hash_Immediate(const ir_node *irn) {
1242         const ia32_immediate_attr_t *a = get_ia32_immediate_attr_const(irn);
1243
1244         return HASH_PTR(a->symconst) + (a->sc_sign << 16) + a->offset;
1245 }
1246
1247 /** Compare node attributes for Immediates. */
1248 static
1249 int ia32_compare_immediate_attr(ir_node *a, ir_node *b)
1250 {
1251         const ia32_immediate_attr_t *attr_a = get_ia32_immediate_attr_const(a);
1252         const ia32_immediate_attr_t *attr_b = get_ia32_immediate_attr_const(b);
1253
1254         if(attr_a->symconst != attr_b->symconst ||
1255            attr_a->sc_sign != attr_b->sc_sign ||
1256            attr_a->offset != attr_b->offset)
1257                 return 1;
1258
1259         return 0;
1260 }
1261
1262 /** Compare node attributes for x87 nodes. */
1263 static
1264 int ia32_compare_x87_attr(ir_node *a, ir_node *b)
1265 {
1266         return ia32_compare_nodes_attr(a, b);
1267 }
1268
1269
1270 /* copies the ia32 attributes */
1271 static void ia32_copy_attr(const ir_node *old_node, ir_node *new_node)
1272 {
1273         ir_graph          *irg      = get_irn_irg(new_node);
1274         struct obstack    *obst     = get_irg_obstack(irg);
1275         const ia32_attr_t *attr_old = get_ia32_attr_const(old_node);
1276         ia32_attr_t       *attr_new = get_ia32_attr(new_node);
1277
1278         /* copy the attributes */
1279         memcpy(attr_new, attr_old, get_op_attr_size(get_irn_op(old_node)));
1280
1281         /* copy out flags */
1282         attr_new->out_flags =
1283                 DUP_ARR_D(int, obst, attr_old->out_flags);
1284         /* copy register assignments */
1285         attr_new->slots =
1286                 DUP_ARR_D(arch_register_t*, obst, attr_old->slots);
1287 }
1288
1289 /* Include the generated constructor functions */
1290 #include "gen_ia32_new_nodes.c.inl"