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