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