draft for register width specifications
[libfirm] / ir / be / ia32 / ia32_common_transform.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief       This file implements the common parts of IR transformation from
23  *              firm into ia32-Firm.
24  * @author      Matthias Braun, Sebastian Buchwald
25  * @version     $Id: ia32_common_transform.c 21012 2008-08-06 13:35:17Z beck $
26  */
27 #include "config.h"
28
29 #include "error.h"
30 #include "ircons.h"
31 #include "irprintf.h"
32 #include "typerep.h"
33 #include "bitset.h"
34
35 #include "../betranshlp.h"
36 #include "../beirg.h"
37 #include "../beabi.h"
38
39 #include "ia32_architecture.h"
40 #include "ia32_common_transform.h"
41 #include "ia32_new_nodes.h"
42
43 #include "gen_ia32_new_nodes.h"
44 #include "gen_ia32_regalloc_if.h"
45
46 /** hold the current code generator during transformation */
47 ia32_code_gen_t *env_cg = NULL;
48
49 heights_t *heights = NULL;
50
51 static int check_immediate_constraint(long val, char immediate_constraint_type)
52 {
53         switch (immediate_constraint_type) {
54                 case 0:
55                 case 'i': return 1;
56
57                 case 'I': return    0 <= val && val <=  31;
58                 case 'J': return    0 <= val && val <=  63;
59                 case 'K': return -128 <= val && val <= 127;
60                 case 'L': return val == 0xff || val == 0xffff;
61                 case 'M': return    0 <= val && val <=   3;
62                 case 'N': return    0 <= val && val <= 255;
63                 case 'O': return    0 <= val && val <= 127;
64
65                 default: panic("Invalid immediate constraint found");
66         }
67 }
68
69 /**
70  * Get a primitive type for a mode with alignment 16.
71  */
72 static ir_type *ia32_get_prim_type(pmap *types, ir_mode *mode)
73 {
74         ir_type *res = pmap_get(types, mode);
75         if (res != NULL)
76                 return res;
77
78         res = new_type_primitive(mode);
79         if (get_mode_size_bits(mode) >= 80) {
80                 set_type_alignment_bytes(res, 16);
81         }
82         pmap_insert(types, mode, res);
83         return res;
84 }
85
86 ir_entity *create_float_const_entity(ir_node *cnst)
87 {
88         ia32_isa_t       *isa = env_cg->isa;
89         tarval           *tv  = get_Const_tarval(cnst);
90         ir_entity        *res = pmap_get(isa->tv_ent, tv);
91         ir_initializer_t *initializer;
92         ir_mode          *mode;
93         ir_type          *tp;
94
95         if (res != NULL)
96                 return res;
97
98         mode = get_tarval_mode(tv);
99
100         if (! ia32_cg_config.use_sse2) {
101                 /* try to reduce the mode to produce smaller sized entities */
102                 if (mode != mode_F) {
103                         if (tarval_ieee754_can_conv_lossless(tv, mode_F)) {
104                                 mode = mode_F;
105                                 tv = tarval_convert_to(tv, mode);
106                         } else if (mode != mode_D) {
107                                 if (tarval_ieee754_can_conv_lossless(tv, mode_D)) {
108                                         mode = mode_D;
109                                         tv = tarval_convert_to(tv, mode);
110                                 }
111                         }
112                 }
113         }
114
115         tp  = ia32_get_prim_type(isa->types, mode);
116         res = new_entity(get_glob_type(), id_unique("C%u"), tp);
117         set_entity_ld_ident(res, get_entity_ident(res));
118         set_entity_visibility(res, ir_visibility_private);
119         add_entity_linkage(res, IR_LINKAGE_CONSTANT);
120
121         initializer = create_initializer_tarval(tv);
122         set_entity_initializer(res, initializer);
123
124         pmap_insert(isa->tv_ent, tv, res);
125         return res;
126 }
127
128 ir_node *ia32_create_Immediate(ir_entity *symconst, int symconst_sign, long val)
129 {
130         ir_graph *irg         = current_ir_graph;
131         ir_node  *start_block = get_irg_start_block(irg);
132         ir_node  *immediate   = new_bd_ia32_Immediate(NULL, start_block, symconst,
133                         symconst_sign, no_pic_adjust, val);
134         arch_set_irn_register(immediate, &ia32_gp_regs[REG_GP_NOREG]);
135
136         return immediate;
137 }
138
139 const arch_register_t *ia32_get_clobber_register(const char *clobber)
140 {
141         const arch_register_t       *reg = NULL;
142         int                          c;
143         size_t                       r;
144         const arch_register_class_t *cls;
145
146         /* TODO: construct a hashmap instead of doing linear search for clobber
147          * register */
148         for (c = 0; c < N_CLASSES; ++c) {
149                 cls = & ia32_reg_classes[c];
150                 for (r = 0; r < cls->n_regs; ++r) {
151                         const arch_register_t *temp_reg = arch_register_for_index(cls, r);
152                         if (strcmp(temp_reg->name, clobber) == 0
153                                         || (c == CLASS_ia32_gp && strcmp(temp_reg->name+1, clobber) == 0)) {
154                                 reg = temp_reg;
155                                 break;
156                         }
157                 }
158                 if (reg != NULL)
159                         break;
160         }
161
162         return reg;
163 }
164
165 int ia32_mode_needs_gp_reg(ir_mode *mode)
166 {
167         if (mode == mode_fpcw)
168                 return 0;
169         if (get_mode_size_bits(mode) > 32)
170                 return 0;
171         return mode_is_int(mode) || mode_is_reference(mode) || mode == mode_b;
172 }
173
174 static void parse_asm_constraints(constraint_t *constraint, const char *c,
175                            int is_output)
176 {
177         char                         immediate_type     = '\0';
178         unsigned                     limited            = 0;
179         const arch_register_class_t *cls                = NULL;
180         int                          memory_possible       = 0;
181         int                          all_registers_allowed = 0;
182         int                          p;
183         int                          same_as = -1;
184
185         memset(constraint, 0, sizeof(constraint[0]));
186         constraint->same_as = -1;
187
188         if (*c == 0) {
189                 /* a memory constraint: no need to do anything in backend about it
190                  * (the dependencies are already respected by the memory edge of
191                  * the node) */
192                 return;
193         }
194
195         /* TODO: improve error messages with node and source info. (As users can
196          * easily hit these) */
197         while (*c != 0) {
198                 switch (*c) {
199                 case ' ':
200                 case '\t':
201                 case '\n':
202                         break;
203
204                 /* Skip out/in-out marker */
205                 case '=': break;
206                 case '+': break;
207
208                 case '&': break;
209
210                 case '*':
211                         ++c;
212                         break;
213                 case '#':
214                         while (*c != 0 && *c != ',')
215                                 ++c;
216                         break;
217
218                 case 'a':
219                         assert(cls == NULL || cls == &ia32_reg_classes[CLASS_ia32_gp]);
220                         cls      = &ia32_reg_classes[CLASS_ia32_gp];
221                         limited |= 1 << REG_EAX;
222                         break;
223                 case 'b':
224                         assert(cls == NULL || cls == &ia32_reg_classes[CLASS_ia32_gp]);
225                         cls      = &ia32_reg_classes[CLASS_ia32_gp];
226                         limited |= 1 << REG_EBX;
227                         break;
228                 case 'c':
229                         assert(cls == NULL || cls == &ia32_reg_classes[CLASS_ia32_gp]);
230                         cls      = &ia32_reg_classes[CLASS_ia32_gp];
231                         limited |= 1 << REG_ECX;
232                         break;
233                 case 'd':
234                         assert(cls == NULL || cls == &ia32_reg_classes[CLASS_ia32_gp]);
235                         cls      = &ia32_reg_classes[CLASS_ia32_gp];
236                         limited |= 1 << REG_EDX;
237                         break;
238                 case 'D':
239                         assert(cls == NULL || cls == &ia32_reg_classes[CLASS_ia32_gp]);
240                         cls      = &ia32_reg_classes[CLASS_ia32_gp];
241                         limited |= 1 << REG_EDI;
242                         break;
243                 case 'S':
244                         assert(cls == NULL || cls == &ia32_reg_classes[CLASS_ia32_gp]);
245                         cls      = &ia32_reg_classes[CLASS_ia32_gp];
246                         limited |= 1 << REG_ESI;
247                         break;
248                 case 'Q':
249                 case 'q':
250                         /* q means lower part of the regs only, this makes no
251                          * difference to Q for us (we only assign whole registers) */
252                         assert(cls == NULL || cls == &ia32_reg_classes[CLASS_ia32_gp]);
253                         cls      = &ia32_reg_classes[CLASS_ia32_gp];
254                         limited |= 1 << REG_EAX | 1 << REG_EBX | 1 << REG_ECX |
255                                    1 << REG_EDX;
256                         break;
257                 case 'A':
258                         assert(cls == NULL || cls == &ia32_reg_classes[CLASS_ia32_gp]);
259                         cls      = &ia32_reg_classes[CLASS_ia32_gp];
260                         limited |= 1 << REG_EAX | 1 << REG_EDX;
261                         break;
262                 case 'l':
263                         assert(cls == NULL || cls == &ia32_reg_classes[CLASS_ia32_gp]);
264                         cls      = &ia32_reg_classes[CLASS_ia32_gp];
265                         limited |= 1 << REG_EAX | 1 << REG_EBX | 1 << REG_ECX |
266                                    1 << REG_EDX | 1 << REG_ESI | 1 << REG_EDI |
267                                    1 << REG_EBP;
268                         break;
269
270                 case 'R':
271                 case 'r':
272                 case 'p':
273                         if (cls != NULL && cls != &ia32_reg_classes[CLASS_ia32_gp])
274                                 panic("multiple register classes not supported");
275                         cls                   = &ia32_reg_classes[CLASS_ia32_gp];
276                         all_registers_allowed = 1;
277                         break;
278
279                 case 'f':
280                 case 't':
281                 case 'u':
282                         /* TODO: mark values so the x87 simulator knows about t and u */
283                         if (cls != NULL && cls != &ia32_reg_classes[CLASS_ia32_vfp])
284                                 panic("multiple register classes not supported");
285                         cls                   = &ia32_reg_classes[CLASS_ia32_vfp];
286                         all_registers_allowed = 1;
287                         break;
288
289                 case 'Y':
290                 case 'x':
291                         if (cls != NULL && cls != &ia32_reg_classes[CLASS_ia32_xmm])
292                                 panic("multiple register classes not supproted");
293                         cls                   = &ia32_reg_classes[CLASS_ia32_xmm];
294                         all_registers_allowed = 1;
295                         break;
296
297                 case 'I':
298                 case 'J':
299                 case 'K':
300                 case 'L':
301                 case 'M':
302                 case 'N':
303                 case 'O':
304                         if (cls != NULL && cls != &ia32_reg_classes[CLASS_ia32_gp])
305                                 panic("multiple register classes not supported");
306                         if (immediate_type != '\0')
307                                 panic("multiple immediate types not supported");
308                         cls            = &ia32_reg_classes[CLASS_ia32_gp];
309                         immediate_type = *c;
310                         break;
311                 case 'n':
312                 case 'i':
313                         if (cls != NULL && cls != &ia32_reg_classes[CLASS_ia32_gp])
314                                 panic("multiple register classes not supported");
315                         if (immediate_type != '\0')
316                                 panic("multiple immediate types not supported");
317                         cls            = &ia32_reg_classes[CLASS_ia32_gp];
318                         immediate_type = 'i';
319                         break;
320
321                 case 'X':
322                 case 'g':
323                         if (cls != NULL && cls != &ia32_reg_classes[CLASS_ia32_gp])
324                                 panic("multiple register classes not supported");
325                         if (immediate_type != '\0')
326                                 panic("multiple immediate types not supported");
327                         immediate_type        = 'i';
328                         cls                   = &ia32_reg_classes[CLASS_ia32_gp];
329                         all_registers_allowed = 1;
330                         memory_possible       = 1;
331                         break;
332
333                 case '0':
334                 case '1':
335                 case '2':
336                 case '3':
337                 case '4':
338                 case '5':
339                 case '6':
340                 case '7':
341                 case '8':
342                 case '9':
343                         if (is_output)
344                                 panic("can only specify same constraint on input");
345
346                         sscanf(c, "%d%n", &same_as, &p);
347                         if (same_as >= 0) {
348                                 c += p;
349                                 continue;
350                         }
351                         break;
352
353                 case 'm':
354                 case 'o':
355                 case 'V':
356                         /* memory constraint no need to do anything in backend about it
357                          * (the dependencies are already respected by the memory edge of
358                          * the node) */
359                         memory_possible = 1;
360                         break;
361
362                 case 'E': /* no float consts yet */
363                 case 'F': /* no float consts yet */
364                 case 's': /* makes no sense on x86 */
365                 case '<': /* no autodecrement on x86 */
366                 case '>': /* no autoincrement on x86 */
367                 case 'C': /* sse constant not supported yet */
368                 case 'G': /* 80387 constant not supported yet */
369                 case 'y': /* we don't support mmx registers yet */
370                 case 'Z': /* not available in 32 bit mode */
371                 case 'e': /* not available in 32 bit mode */
372                         panic("unsupported asm constraint '%c' found in (%+F)",
373                               *c, current_ir_graph);
374                         break;
375                 default:
376                         panic("unknown asm constraint '%c' found in (%+F)", *c,
377                               current_ir_graph);
378                         break;
379                 }
380                 ++c;
381         }
382
383         if (same_as >= 0) {
384                 if (cls != NULL)
385                         panic("same as and register constraint not supported");
386                 if (immediate_type != '\0')
387                         panic("same as and immediate constraint not supported");
388         }
389
390         if (cls == NULL && same_as < 0) {
391                 if (!memory_possible)
392                         panic("no constraint specified for assembler input");
393         }
394
395         constraint->same_as               = same_as;
396         constraint->cls                   = cls;
397         constraint->allowed_registers     = limited;
398         constraint->all_registers_allowed = all_registers_allowed;
399         constraint->memory_possible       = memory_possible;
400         constraint->immediate_type        = immediate_type;
401 }
402
403 static bool can_match(const arch_register_req_t *in,
404                       const arch_register_req_t *out)
405 {
406         if (in->cls != out->cls)
407                 return false;
408         if ( (in->type & arch_register_req_type_limited) == 0
409                 || (out->type & arch_register_req_type_limited) == 0 )
410                 return true;
411
412         return (*in->limited & *out->limited) != 0;
413 }
414
415 static inline ir_node *get_new_node(ir_node *node)
416 {
417 #ifdef FIRM_GRGEN_BE
418         if (be_transformer == TRANSFORMER_DEFAULT) {
419                 return be_transform_node(node);
420         } else {
421                 return node;
422         }
423 #else
424         return be_transform_node(node);
425 #endif
426 }
427
428 ir_node *gen_ASM(ir_node *node)
429 {
430         ir_node                    *block     = get_nodes_block(node);
431         ir_node                    *new_block = get_new_node(block);
432         dbg_info                   *dbgi      = get_irn_dbg_info(node);
433         int                         i, arity;
434         int                         value_arity;
435         int                         out_idx;
436         ir_node                   **in;
437         ir_node                    *new_node;
438         int                         out_arity;
439         int                         n_out_constraints;
440         int                         n_clobbers;
441         const arch_register_req_t **out_reg_reqs;
442         const arch_register_req_t **in_reg_reqs;
443         ia32_asm_reg_t             *register_map;
444         unsigned                    reg_map_size = 0;
445         struct obstack             *obst;
446         const ir_asm_constraint    *in_constraints;
447         const ir_asm_constraint    *out_constraints;
448         ident                     **clobbers;
449         int                         clobbers_flags = 0;
450         unsigned                    clobber_bits[N_CLASSES];
451         int                         out_size;
452         backend_info_t             *info;
453
454         memset(&clobber_bits, 0, sizeof(clobber_bits));
455
456         /* workaround for lots of buggy code out there as most people think volatile
457          * asm is enough for everything and forget the flags (linux kernel, etc.)
458          */
459         if (get_irn_pinned(node) == op_pin_state_pinned) {
460                 clobbers_flags = 1;
461         }
462
463         arity = get_irn_arity(node);
464         in    = ALLOCANZ(ir_node*, arity);
465
466         clobbers   = get_ASM_clobbers(node);
467         n_clobbers = 0;
468         for (i = 0; i < get_ASM_n_clobbers(node); ++i) {
469                 const arch_register_req_t *req;
470                 const char                *c = get_id_str(clobbers[i]);
471
472                 if (strcmp(c, "memory") == 0)
473                         continue;
474                 if (strcmp(c, "cc") == 0) {
475                         clobbers_flags = 1;
476                         continue;
477                 }
478
479                 req = parse_clobber(c);
480                 clobber_bits[req->cls->index] |= *req->limited;
481
482                 n_clobbers++;
483         }
484         n_out_constraints = get_ASM_n_output_constraints(node);
485         out_arity         = n_out_constraints + n_clobbers;
486
487         in_constraints  = get_ASM_input_constraints(node);
488         out_constraints = get_ASM_output_constraints(node);
489
490         /* determine size of register_map */
491         for (out_idx = 0; out_idx < n_out_constraints; ++out_idx) {
492                 const ir_asm_constraint *constraint = &out_constraints[out_idx];
493                 if (constraint->pos > reg_map_size)
494                         reg_map_size = constraint->pos;
495         }
496         for (i = 0; i < arity; ++i) {
497                 const ir_asm_constraint *constraint = &in_constraints[i];
498                 if (constraint->pos > reg_map_size)
499                         reg_map_size = constraint->pos;
500         }
501         ++reg_map_size;
502
503         obst         = get_irg_obstack(current_ir_graph);
504         register_map = NEW_ARR_D(ia32_asm_reg_t, obst, reg_map_size);
505         memset(register_map, 0, reg_map_size * sizeof(register_map[0]));
506
507         /* construct output constraints */
508         out_size = out_arity + 1;
509         out_reg_reqs = obstack_alloc(obst, out_size * sizeof(out_reg_reqs[0]));
510
511         for (out_idx = 0; out_idx < n_out_constraints; ++out_idx) {
512                 const ir_asm_constraint   *constraint = &out_constraints[out_idx];
513                 const char                *c       = get_id_str(constraint->constraint);
514                 unsigned                   pos        = constraint->pos;
515                 constraint_t               parsed_constraint;
516                 const arch_register_req_t *req;
517
518                 parse_asm_constraints(&parsed_constraint, c, 1);
519                 req = make_register_req(&parsed_constraint, n_out_constraints,
520                                         out_reg_reqs, out_idx);
521                 out_reg_reqs[out_idx] = req;
522
523                 register_map[pos].use_input = 0;
524                 register_map[pos].valid     = 1;
525                 register_map[pos].memory    = 0;
526                 register_map[pos].inout_pos = out_idx;
527                 register_map[pos].mode      = constraint->mode;
528         }
529
530         /* inputs + input constraints */
531         in_reg_reqs = obstack_alloc(obst, arity * sizeof(in_reg_reqs[0]));
532         for (i = 0; i < arity; ++i) {
533                 ir_node                   *pred         = get_irn_n(node, i);
534                 const ir_asm_constraint   *constraint   = &in_constraints[i];
535                 ident                     *constr_id    = constraint->constraint;
536                 const char                *c            = get_id_str(constr_id);
537                 unsigned                   pos          = constraint->pos;
538                 int                        is_memory_op = 0;
539                 ir_node                   *input        = NULL;
540                 unsigned                   r_clobber_bits;
541                 constraint_t               parsed_constraint;
542                 const arch_register_req_t *req;
543
544                 parse_asm_constraints(&parsed_constraint, c, 0);
545                 if (parsed_constraint.cls != NULL) {
546                         r_clobber_bits = clobber_bits[parsed_constraint.cls->index];
547                         if (r_clobber_bits != 0) {
548                                 if (parsed_constraint.all_registers_allowed) {
549                                         parsed_constraint.all_registers_allowed = 0;
550                                         be_abi_set_non_ignore_regs(be_get_irg_abi(env_cg->irg),
551                                                         parsed_constraint.cls,
552                                                         &parsed_constraint.allowed_registers);
553                                 }
554                                 parsed_constraint.allowed_registers &= ~r_clobber_bits;
555                         }
556                 }
557
558                 req = make_register_req(&parsed_constraint, n_out_constraints,
559                                         out_reg_reqs, i);
560                 in_reg_reqs[i] = req;
561
562                 if (parsed_constraint.immediate_type != '\0') {
563                         char imm_type = parsed_constraint.immediate_type;
564                         input = try_create_Immediate(pred, imm_type);
565                 }
566
567                 if (input == NULL) {
568                         ir_node *pred = get_irn_n(node, i);
569                         input = get_new_node(pred);
570
571                         if (parsed_constraint.cls == NULL
572                                         && parsed_constraint.same_as < 0) {
573                                 is_memory_op = 1;
574                         } else if (parsed_constraint.memory_possible) {
575                                 /* TODO: match Load or Load/Store if memory possible is set */
576                         }
577                 }
578                 in[i] = input;
579
580                 register_map[pos].use_input = 1;
581                 register_map[pos].valid     = 1;
582                 register_map[pos].memory    = is_memory_op;
583                 register_map[pos].inout_pos = i;
584                 register_map[pos].mode      = constraint->mode;
585         }
586
587         /* parse clobbers */
588         for (i = 0; i < get_ASM_n_clobbers(node); ++i) {
589                 const char                *c = get_id_str(clobbers[i]);
590                 const arch_register_req_t *req;
591
592                 if (strcmp(c, "memory") == 0 || strcmp(c, "cc") == 0)
593                         continue;
594
595                 req = parse_clobber(c);
596                 out_reg_reqs[out_idx] = req;
597                 ++out_idx;
598         }
599
600         /* count inputs which are real values (and not memory) */
601         value_arity = 0;
602         for (i = 0; i < arity; ++i) {
603                 ir_node *in = get_irn_n(node, i);
604                 if (get_irn_mode(in) == mode_M)
605                         continue;
606                 ++value_arity;
607         }
608
609         /* Attempt to make ASM node register pressure faithful.
610          * (This does not work for complicated cases yet!)
611          *
612          * Algorithm: Check if there are fewer inputs or outputs (I will call this
613          * the smaller list). Then try to match each constraint of the smaller list
614          * to 1 of the other list. If we can't match it, then we have to add a dummy
615          * input/output to the other list
616          *
617          * FIXME: This is still broken in lots of cases. But at least better than
618          *        before...
619          * FIXME: need to do this per register class...
620          */
621         if (out_arity <= value_arity) {
622                 int       orig_arity = arity;
623                 int       in_size    = arity;
624                 int       o;
625                 bitset_t *used_ins = bitset_alloca(arity);
626                 for (o = 0; o < out_arity; ++o) {
627                         int   i;
628                         const arch_register_req_t *outreq = out_reg_reqs[o];
629
630                         if (outreq->cls == NULL) {
631                                 continue;
632                         }
633
634                         for (i = 0; i < orig_arity; ++i) {
635                                 const arch_register_req_t *inreq;
636                                 if (bitset_is_set(used_ins, i))
637                                         continue;
638                                 inreq = in_reg_reqs[i];
639                                 if (!can_match(outreq, inreq))
640                                         continue;
641                                 bitset_set(used_ins, i);
642                                 break;
643                         }
644                         /* did we find any match? */
645                         if (i < orig_arity)
646                                 continue;
647
648                         /* we might need more space in the input arrays */
649                         if (arity >= in_size) {
650                                 const arch_register_req_t **new_in_reg_reqs;
651                                 ir_node             **new_in;
652
653                                 in_size *= 2;
654                                 new_in_reg_reqs
655                                         = obstack_alloc(obst, in_size*sizeof(in_reg_reqs[0]));
656                                 memcpy(new_in_reg_reqs, in_reg_reqs, arity * sizeof(new_in_reg_reqs[0]));
657                                 new_in = ALLOCANZ(ir_node*, in_size);
658                                 memcpy(new_in, in, arity*sizeof(new_in[0]));
659
660                                 in_reg_reqs = new_in_reg_reqs;
661                                 in          = new_in;
662                         }
663
664                         /* add a new (dummy) input which occupies the register */
665                         assert(outreq->type & arch_register_req_type_limited);
666                         in_reg_reqs[arity] = outreq;
667                         in[arity]          = new_bd_ia32_ProduceVal(NULL, block);
668                         be_dep_on_frame(in[arity]);
669                         ++arity;
670                 }
671         } else {
672                 int       i;
673                 bitset_t *used_outs = bitset_alloca(out_arity);
674                 int       orig_out_arity = out_arity;
675                 for (i = 0; i < arity; ++i) {
676                         int   o;
677                         const arch_register_req_t *inreq = in_reg_reqs[i];
678
679                         if (inreq->cls == NULL) {
680                                 continue;
681                         }
682
683                         for (o = 0; o < orig_out_arity; ++o) {
684                                 const arch_register_req_t *outreq;
685                                 if (bitset_is_set(used_outs, o))
686                                         continue;
687                                 outreq = out_reg_reqs[o];
688                                 if (!can_match(outreq, inreq))
689                                         continue;
690                                 bitset_set(used_outs, i);
691                                 break;
692                         }
693                         /* did we find any match? */
694                         if (o < orig_out_arity)
695                                 continue;
696
697                         /* we might need more space in the output arrays */
698                         if (out_arity >= out_size) {
699                                 const arch_register_req_t **new_out_reg_reqs;
700
701                                 out_size *= 2;
702                                 new_out_reg_reqs
703                                         = obstack_alloc(obst, out_size*sizeof(out_reg_reqs[0]));
704                                 memcpy(new_out_reg_reqs, out_reg_reqs,
705                                        out_arity * sizeof(new_out_reg_reqs[0]));
706                                 out_reg_reqs = new_out_reg_reqs;
707                         }
708
709                         /* add a new (dummy) output which occupies the register */
710                         assert(inreq->type & arch_register_req_type_limited);
711                         out_reg_reqs[out_arity] = inreq;
712                         ++out_arity;
713                 }
714         }
715
716         /* append none register requirement for the memory output */
717         if (out_arity + 1 >= out_size) {
718                 const arch_register_req_t **new_out_reg_reqs;
719
720                 out_size = out_arity + 1;
721                 new_out_reg_reqs
722                         = obstack_alloc(obst, out_size*sizeof(out_reg_reqs[0]));
723                 memcpy(new_out_reg_reqs, out_reg_reqs,
724                            out_arity * sizeof(new_out_reg_reqs[0]));
725                 out_reg_reqs = new_out_reg_reqs;
726         }
727
728         /* add a new (dummy) output which occupies the register */
729         out_reg_reqs[out_arity] = arch_no_register_req;
730         ++out_arity;
731
732         new_node = new_bd_ia32_Asm(dbgi, new_block, arity, in, out_arity,
733                                    get_ASM_text(node), register_map);
734
735         if (arity == 0)
736                 be_dep_on_frame(new_node);
737
738         info = be_get_info(new_node);
739         for (i = 0; i < out_arity; ++i) {
740                 info->out_infos[i].req = out_reg_reqs[i];
741         }
742         set_ia32_in_req_all(new_node, in_reg_reqs);
743
744         SET_IA32_ORIG_NODE(new_node, node);
745
746         return new_node;
747 }
748
749 ir_node *gen_CopyB(ir_node *node)
750 {
751         ir_node  *block    = get_new_node(get_nodes_block(node));
752         ir_node  *src      = get_CopyB_src(node);
753         ir_node  *new_src  = get_new_node(src);
754         ir_node  *dst      = get_CopyB_dst(node);
755         ir_node  *new_dst  = get_new_node(dst);
756         ir_node  *mem      = get_CopyB_mem(node);
757         ir_node  *new_mem  = get_new_node(mem);
758         ir_node  *res      = NULL;
759         dbg_info *dbgi     = get_irn_dbg_info(node);
760         int      size      = get_type_size_bytes(get_CopyB_type(node));
761         int      rem;
762
763         /* If we have to copy more than 32 bytes, we use REP MOVSx and */
764         /* then we need the size explicitly in ECX.                    */
765         if (size >= 32 * 4) {
766                 rem = size & 0x3; /* size % 4 */
767                 size >>= 2;
768
769                 res = new_bd_ia32_Const(dbgi, block, NULL, 0, 0, size);
770                 be_dep_on_frame(res);
771
772                 res = new_bd_ia32_CopyB(dbgi, block, new_dst, new_src, res, new_mem, rem);
773         } else {
774                 if (size == 0) {
775                         ir_fprintf(stderr, "Optimization warning copyb %+F with size <4\n",
776                                    node);
777                 }
778                 res = new_bd_ia32_CopyB_i(dbgi, block, new_dst, new_src, new_mem, size);
779         }
780
781         SET_IA32_ORIG_NODE(res, node);
782
783         return res;
784 }
785
786 ir_node *gen_Proj_tls(ir_node *node)
787 {
788         ir_node *block = get_new_node(get_nodes_block(node));
789         ir_node *res   = NULL;
790
791         res = new_bd_ia32_LdTls(NULL, block, mode_Iu);
792
793         return res;
794 }
795
796 ir_node *gen_Unknown(ir_node *node)
797 {
798         ir_mode  *mode  = get_irn_mode(node);
799         ir_graph *irg   = current_ir_graph;
800         dbg_info *dbgi  = get_irn_dbg_info(node);
801         ir_node  *block = get_irg_start_block(irg);
802         ir_node  *res   = NULL;
803
804         if (mode_is_float(mode)) {
805                 if (ia32_cg_config.use_sse2) {
806                         res = new_bd_ia32_xUnknown(dbgi, block);
807                 } else {
808                         res = new_bd_ia32_vfldz(dbgi, block);
809                 }
810         } else if (ia32_mode_needs_gp_reg(mode)) {
811                 res = new_bd_ia32_Unknown(dbgi, block);
812         } else {
813                 panic("unsupported Unknown-Mode");
814         }
815
816         be_dep_on_frame(res);
817         return res;
818 }
819
820 const arch_register_req_t *make_register_req(const constraint_t *constraint,
821                 int n_outs, const arch_register_req_t **out_reqs, int pos)
822 {
823         struct obstack      *obst    = get_irg_obstack(current_ir_graph);
824         int                  same_as = constraint->same_as;
825         arch_register_req_t *req;
826
827         if (same_as >= 0) {
828                 const arch_register_req_t *other_constr;
829
830                 if (same_as >= n_outs)
831                         panic("invalid output number in same_as constraint");
832
833                 other_constr     = out_reqs[same_as];
834
835                 req              = obstack_alloc(obst, sizeof(req[0]));
836                 *req             = *other_constr;
837                 req->type       |= arch_register_req_type_should_be_same;
838                 req->other_same  = 1U << pos;
839
840                 /* switch constraints. This is because in firm we have same_as
841                  * constraints on the output constraints while in the gcc asm syntax
842                  * they are specified on the input constraints */
843                 out_reqs[same_as] = req;
844                 return other_constr;
845         }
846
847         /* pure memory ops */
848         if (constraint->cls == NULL) {
849                 return arch_no_register_req;
850         }
851
852         if (constraint->allowed_registers != 0
853                         && !constraint->all_registers_allowed) {
854                 unsigned *limited_ptr;
855
856                 req         = obstack_alloc(obst, sizeof(req[0]) + sizeof(unsigned));
857                 memset(req, 0, sizeof(req[0]));
858                 limited_ptr = (unsigned*) (req+1);
859
860                 req->type    = arch_register_req_type_limited;
861                 *limited_ptr = constraint->allowed_registers;
862                 req->limited = limited_ptr;
863         } else {
864                 req       = obstack_alloc(obst, sizeof(req[0]));
865                 memset(req, 0, sizeof(req[0]));
866                 req->type = arch_register_req_type_normal;
867         }
868         req->cls = constraint->cls;
869
870         return req;
871 }
872
873 const arch_register_req_t *parse_clobber(const char *clobber)
874 {
875         struct obstack        *obst = get_irg_obstack(current_ir_graph);
876         const arch_register_t *reg  = ia32_get_clobber_register(clobber);
877         arch_register_req_t   *req;
878         unsigned              *limited;
879
880         if (reg == NULL) {
881                 panic("Register '%s' mentioned in asm clobber is unknown", clobber);
882         }
883
884         assert(reg->index < 32);
885
886         limited  = obstack_alloc(obst, sizeof(limited[0]));
887         *limited = 1 << reg->index;
888
889         req          = obstack_alloc(obst, sizeof(req[0]));
890         memset(req, 0, sizeof(req[0]));
891         req->type    = arch_register_req_type_limited;
892         req->cls     = arch_register_get_class(reg);
893         req->limited = limited;
894
895         return req;
896 }
897
898
899 int prevents_AM(ir_node *const block, ir_node *const am_candidate,
900                        ir_node *const other)
901 {
902         if (get_nodes_block(other) != block)
903                 return 0;
904
905         if (is_Sync(other)) {
906                 int i;
907
908                 for (i = get_Sync_n_preds(other) - 1; i >= 0; --i) {
909                         ir_node *const pred = get_Sync_pred(other, i);
910
911                         if (get_nodes_block(pred) != block)
912                                 continue;
913
914                         /* Do not block ourselves from getting eaten */
915                         if (is_Proj(pred) && get_Proj_pred(pred) == am_candidate)
916                                 continue;
917
918                         if (!heights_reachable_in_block(heights, pred, am_candidate))
919                                 continue;
920
921                         return 1;
922                 }
923
924                 return 0;
925         } else {
926                 /* Do not block ourselves from getting eaten */
927                 if (is_Proj(other) && get_Proj_pred(other) == am_candidate)
928                         return 0;
929
930                 if (!heights_reachable_in_block(heights, other, am_candidate))
931                         return 0;
932
933                 return 1;
934         }
935 }
936
937 ir_node *try_create_Immediate(ir_node *node, char immediate_constraint_type)
938 {
939         long         val = 0;
940         ir_entity   *symconst_ent  = NULL;
941         ir_mode     *mode;
942         ir_node     *cnst          = NULL;
943         ir_node     *symconst      = NULL;
944         ir_node     *new_node;
945
946         mode = get_irn_mode(node);
947         if (!mode_is_int(mode) && !mode_is_reference(mode)) {
948                 return NULL;
949         }
950
951         if (is_Const(node)) {
952                 cnst     = node;
953                 symconst = NULL;
954         } else if (is_Global(node)) {
955                 cnst     = NULL;
956                 symconst = node;
957         } else if (is_Add(node)) {
958                 ir_node *left  = get_Add_left(node);
959                 ir_node *right = get_Add_right(node);
960                 if (is_Const(left) && is_Global(right)) {
961                         cnst     = left;
962                         symconst = right;
963                 } else if (is_Global(left) && is_Const(right)) {
964                         cnst     = right;
965                         symconst = left;
966                 }
967         } else {
968                 return NULL;
969         }
970
971         if (cnst != NULL) {
972                 tarval *offset = get_Const_tarval(cnst);
973                 if (!tarval_is_long(offset)) {
974                         ir_fprintf(stderr, "Optimisation Warning: tarval of %+F is not a long?\n", cnst);
975                         return NULL;
976                 }
977
978                 val = get_tarval_long(offset);
979                 if (!check_immediate_constraint(val, immediate_constraint_type))
980                         return NULL;
981         }
982         if (symconst != NULL) {
983                 if (immediate_constraint_type != 0) {
984                         /* we need full 32bits for symconsts */
985                         return NULL;
986                 }
987
988                 symconst_ent = get_Global_entity(symconst);
989         }
990         if (cnst == NULL && symconst == NULL)
991                 return NULL;
992
993         new_node = ia32_create_Immediate(symconst_ent, 0, val);
994         return new_node;
995 }