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