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