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