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