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