9ec884a23a4006cf1ecac0c51624e1bc67fefb64
[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(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                 const arch_register_req_t *req
477                         = ia32_make_register_req(&parsed_constraint, n_out_constraints,
478                                              out_reg_reqs, out_idx);
479                 out_reg_reqs[out_idx] = req;
480
481                 /* multiple constraints for same pos. This can happen for example when
482                  * a =A constraint gets lowered to two constraints: =a and =d for the
483                  * same pos */
484                 if (register_map[pos].valid)
485                         continue;
486
487                 register_map[pos].use_input = 0;
488                 register_map[pos].valid     = 1;
489                 register_map[pos].memory    = 0;
490                 register_map[pos].inout_pos = out_idx;
491                 register_map[pos].mode      = constraint->mode;
492         }
493
494         /* inputs + input constraints */
495         const arch_register_req_t **in_reg_reqs
496                 = OALLOCN(obst, const arch_register_req_t*, n_ins);
497         for (int i = 0; i < n_inputs; ++i) {
498                 constraint_t               parsed_constraint;
499                 ir_node                   *pred         = get_ASM_input(node, i);
500                 const ir_asm_constraint   *constraint   = &in_constraints[i];
501                 ident                     *constr_id    = constraint->constraint;
502                 const char                *c            = get_id_str(constr_id);
503                 unsigned                   pos          = constraint->pos;
504                 int                        is_memory_op = 0;
505                 ir_node                   *input        = NULL;
506
507                 parse_asm_constraints(&parsed_constraint, c, false);
508                 if (parsed_constraint.cls != NULL) {
509                         unsigned r_clobber_bits
510                                 = clobber_bits[parsed_constraint.cls->index];
511                         if (r_clobber_bits != 0) {
512                                 if (parsed_constraint.all_registers_allowed) {
513                                         parsed_constraint.all_registers_allowed = 0;
514                                         be_set_allocatable_regs(irg,
515                                                         parsed_constraint.cls,
516                                                         &parsed_constraint.allowed_registers);
517                                 }
518                                 parsed_constraint.allowed_registers &= ~r_clobber_bits;
519                         }
520                 }
521
522                 const arch_register_req_t *req
523                         = ia32_make_register_req(&parsed_constraint, n_out_constraints,
524                                              out_reg_reqs, i);
525                 in_reg_reqs[i] = req;
526
527                 if (parsed_constraint.immediate_type != '\0') {
528                         char imm_type = parsed_constraint.immediate_type;
529                         input = ia32_try_create_Immediate(pred, imm_type);
530                 }
531
532                 if (input == NULL) {
533                         input = get_new_node(pred);
534
535                         if (parsed_constraint.cls == NULL
536                                         && parsed_constraint.same_as < 0) {
537                                 is_memory_op = 1;
538                                 in_reg_reqs[i] = ia32_reg_classes[CLASS_ia32_gp].class_req;
539                         } else if (parsed_constraint.memory_possible) {
540                                 /* TODO: match Load or Load/Store if memory possible is set */
541                         }
542                 }
543                 in[i] = input;
544
545                 register_map[pos].use_input = 1;
546                 register_map[pos].valid     = 1;
547                 register_map[pos].memory    = is_memory_op;
548                 register_map[pos].inout_pos = i;
549                 register_map[pos].mode      = constraint->mode;
550         }
551
552         assert(n_inputs == n_ins-1);
553         ir_node *mem = get_ASM_mem(node);
554         in[n_inputs]          = be_transform_node(mem);
555         in_reg_reqs[n_inputs] = arch_no_register_req;
556
557         /* parse clobbers */
558         for (size_t c = 0; c < get_ASM_n_clobbers(node); ++c) {
559                 const char                *clobber = get_id_str(clobbers[c]);
560                 const arch_register_req_t *req     = ia32_parse_clobber(clobber);
561                 if (req == NULL)
562                         continue;
563                 out_reg_reqs[out_idx] = req;
564                 ++out_idx;
565         }
566
567         /* Attempt to make ASM node register pressure faithful.
568          * (This does not work for complicated cases yet!)
569          *
570          * Algorithm: Check if there are fewer inputs or outputs (I will call this
571          * the smaller list). Then try to match each constraint of the smaller list
572          * to 1 of the other list. If we can't match it, then we have to add a dummy
573          * input/output to the other list
574          *
575          * FIXME: This is still broken in lots of cases. But at least better than
576          *        before...
577          * FIXME: need to do this per register class...
578          */
579         if (out_arity <= (size_t)n_inputs) {
580                 int       orig_inputs = n_ins;
581                 int       in_size     = n_ins;
582                 bitset_t *used_ins    = bitset_alloca(n_ins);
583                 for (size_t o = 0; o < out_arity; ++o) {
584                         const arch_register_req_t *outreq = out_reg_reqs[o];
585
586                         if (outreq->cls == NULL) {
587                                 continue;
588                         }
589
590                         int i;
591                         for (i = 0; i < orig_inputs; ++i) {
592                                 if (bitset_is_set(used_ins, i))
593                                         continue;
594                                 const arch_register_req_t *inreq = in_reg_reqs[i];
595                                 if (!can_match(outreq, inreq))
596                                         continue;
597                                 bitset_set(used_ins, i);
598                                 break;
599                         }
600                         /* did we find any match? */
601                         if (i < orig_inputs)
602                                 continue;
603
604                         /* we might need more space in the input arrays */
605                         if (n_ins >= in_size) {
606                                 in_size *= 2;
607                                 const arch_register_req_t **new_in_reg_reqs
608                                         = OALLOCN(obst, const arch_register_req_t*,
609                                                           in_size);
610                                 memcpy(new_in_reg_reqs, in_reg_reqs,
611                                        n_ins*sizeof(new_in_reg_reqs[0]));
612                                 ir_node **new_in = ALLOCANZ(ir_node*, in_size);
613                                 memcpy(new_in, in, n_ins*sizeof(new_in[0]));
614
615                                 in_reg_reqs = new_in_reg_reqs;
616                                 in          = new_in;
617                         }
618
619                         /* add a new (dummy) input which occupies the register */
620                         assert(arch_register_req_is(outreq, limited));
621                         in_reg_reqs[n_ins] = outreq;
622                         in[n_ins]          = new_bd_ia32_ProduceVal(NULL, block);
623                         ++n_ins;
624                 }
625         } else {
626                 bitset_t *used_outs      = bitset_alloca(out_arity);
627                 size_t    orig_out_arity = out_arity;
628                 for (int i = 0; i < n_inputs; ++i) {
629                         const arch_register_req_t *inreq = in_reg_reqs[i];
630
631                         if (inreq->cls == NULL)
632                                 continue;
633
634                         size_t o;
635                         for (o = 0; o < orig_out_arity; ++o) {
636                                 const arch_register_req_t *outreq;
637                                 if (bitset_is_set(used_outs, o))
638                                         continue;
639                                 outreq = out_reg_reqs[o];
640                                 if (!can_match(outreq, inreq))
641                                         continue;
642                                 bitset_set(used_outs, i);
643                                 break;
644                         }
645                         /* did we find any match? */
646                         if (o < orig_out_arity)
647                                 continue;
648
649                         /* we might need more space in the output arrays */
650                         if (out_arity >= out_size) {
651                                 const arch_register_req_t **new_out_reg_reqs;
652
653                                 out_size *= 2;
654                                 new_out_reg_reqs
655                                         = OALLOCN(obst, const arch_register_req_t*, out_size);
656                                 memcpy(new_out_reg_reqs, out_reg_reqs,
657                                        out_arity * sizeof(new_out_reg_reqs[0]));
658                                 out_reg_reqs = new_out_reg_reqs;
659                         }
660
661                         /* add a new (dummy) output which occupies the register */
662                         assert(arch_register_req_is(inreq, limited));
663                         out_reg_reqs[out_arity] = inreq;
664                         ++out_arity;
665                 }
666         }
667
668         /* append none register requirement for the memory output */
669         if (out_arity + 1 >= out_size) {
670                 const arch_register_req_t **new_out_reg_reqs;
671
672                 out_size = out_arity + 1;
673                 new_out_reg_reqs
674                         = OALLOCN(obst, const arch_register_req_t*, out_size);
675                 memcpy(new_out_reg_reqs, out_reg_reqs,
676                            out_arity * sizeof(new_out_reg_reqs[0]));
677                 out_reg_reqs = new_out_reg_reqs;
678         }
679
680         /* add a new (dummy) output which occupies the register */
681         out_reg_reqs[out_arity] = arch_no_register_req;
682         ++out_arity;
683
684         ir_node *new_node = new_bd_ia32_Asm(dbgi, new_block, n_ins, in, out_arity,
685                                             get_ASM_text(node), register_map);
686
687         backend_info_t *info = be_get_info(new_node);
688         for (size_t o = 0; o < out_arity; ++o) {
689                 info->out_infos[o].req = out_reg_reqs[o];
690         }
691         arch_set_irn_register_reqs_in(new_node, in_reg_reqs);
692
693         SET_IA32_ORIG_NODE(new_node, node);
694
695         return new_node;
696 }
697
698 ir_node *ia32_gen_CopyB(ir_node *node)
699 {
700         ir_node  *block    = get_new_node(get_nodes_block(node));
701         ir_node  *src      = get_CopyB_src(node);
702         ir_node  *new_src  = get_new_node(src);
703         ir_node  *dst      = get_CopyB_dst(node);
704         ir_node  *new_dst  = get_new_node(dst);
705         ir_node  *mem      = get_CopyB_mem(node);
706         ir_node  *new_mem  = get_new_node(mem);
707         ir_node  *res      = NULL;
708         dbg_info *dbgi     = get_irn_dbg_info(node);
709         int      size      = get_type_size_bytes(get_CopyB_type(node));
710         int      throws_exception = ir_throws_exception(node);
711         int      rem;
712
713         /* If we have to copy more than 32 bytes, we use REP MOVSx and */
714         /* then we need the size explicitly in ECX.                    */
715         if (size >= 32 * 4) {
716                 rem = size & 0x3; /* size % 4 */
717                 size >>= 2;
718
719                 res = new_bd_ia32_Const(dbgi, block, NULL, 0, 0, size);
720
721                 res = new_bd_ia32_CopyB(dbgi, block, new_dst, new_src, res, new_mem, rem);
722         } else {
723                 if (size == 0) {
724                         ir_fprintf(stderr, "Optimization warning copyb %+F with size <4\n",
725                                    node);
726                 }
727                 res = new_bd_ia32_CopyB_i(dbgi, block, new_dst, new_src, new_mem, size);
728         }
729         ir_set_throws_exception(res, throws_exception);
730
731         SET_IA32_ORIG_NODE(res, node);
732
733         return res;
734 }
735
736 ir_node *ia32_gen_Proj_tls(ir_node *node)
737 {
738         ir_node *block = get_new_node(get_nodes_block(node));
739         ir_node *res   = new_bd_ia32_LdTls(NULL, block);
740         return res;
741 }
742
743 ir_node *ia32_gen_Unknown(ir_node *node)
744 {
745         ir_mode  *mode  = get_irn_mode(node);
746         ir_graph *irg   = current_ir_graph;
747         dbg_info *dbgi  = get_irn_dbg_info(node);
748         ir_node  *block = get_irg_start_block(irg);
749         ir_node  *res   = NULL;
750
751         if (mode_is_float(mode)) {
752                 if (ia32_cg_config.use_sse2) {
753                         res = new_bd_ia32_xUnknown(dbgi, block);
754                 } else {
755                         res = new_bd_ia32_fldz(dbgi, block);
756                 }
757         } else if (ia32_mode_needs_gp_reg(mode)) {
758                 res = new_bd_ia32_Unknown(dbgi, block);
759         } else {
760                 panic("unsupported Unknown-Mode");
761         }
762
763         return res;
764 }
765
766 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)
767 {
768         int const same_as = c->same_as;
769         if (same_as >= 0) {
770                 if (same_as >= n_outs)
771                         panic("invalid output number in same_as constraint");
772
773                 struct obstack            *const obst  = get_irg_obstack(current_ir_graph);
774                 arch_register_req_t       *const req   = OALLOC(obst, arch_register_req_t);
775                 arch_register_req_t const *const other = out_reqs[same_as];
776                 *req            = *other;
777                 req->type      |= arch_register_req_type_should_be_same;
778                 req->other_same = 1U << pos;
779
780                 /* Switch constraints. This is because in firm we have same_as
781                  * constraints on the output constraints while in the gcc asm syntax
782                  * they are specified on the input constraints. */
783                 out_reqs[same_as] = req;
784                 return other;
785         }
786
787         /* Pure memory ops. */
788         if (!c->cls)
789                 return arch_no_register_req;
790
791         if (c->allowed_registers == 0 || c->all_registers_allowed)
792                 return c->cls->class_req;
793
794         struct obstack      *const obst    = get_irg_obstack(current_ir_graph);
795         arch_register_req_t *const req     = (arch_register_req_t*)obstack_alloc(obst, sizeof(req[0]) + sizeof(unsigned));
796         unsigned            *const limited = (unsigned*)(req + 1);
797         *limited = c->allowed_registers;
798
799         memset(req, 0, sizeof(req[0]));
800         req->type    = arch_register_req_type_limited;
801         req->cls     = c->cls;
802         req->limited = limited;
803         req->width   = 1;
804         return req;
805 }
806
807 const arch_register_req_t *ia32_parse_clobber(const char *clobber)
808 {
809         if (strcmp(clobber, "memory") == 0 || strcmp(clobber, "cc") == 0)
810                 return NULL;
811
812         arch_register_t const *const reg = ia32_get_clobber_register(clobber);
813         if (!reg)
814                 panic("Register '%s' mentioned in asm clobber is unknown", clobber);
815
816         return reg->single_req;
817 }
818
819
820 int ia32_prevents_AM(ir_node *const block, ir_node *const am_candidate,
821                        ir_node *const other)
822 {
823         if (get_nodes_block(other) != block)
824                 return 0;
825
826         if (is_Sync(other)) {
827                 int i;
828
829                 for (i = get_Sync_n_preds(other) - 1; i >= 0; --i) {
830                         ir_node *const pred = get_Sync_pred(other, i);
831
832                         if (get_nodes_block(pred) != block)
833                                 continue;
834
835                         /* Do not block ourselves from getting eaten */
836                         if (is_Proj(pred) && get_Proj_pred(pred) == am_candidate)
837                                 continue;
838
839                         if (!heights_reachable_in_block(ia32_heights, pred, am_candidate))
840                                 continue;
841
842                         return 1;
843                 }
844
845                 return 0;
846         } else {
847                 /* Do not block ourselves from getting eaten */
848                 if (is_Proj(other) && get_Proj_pred(other) == am_candidate)
849                         return 0;
850
851                 if (!heights_reachable_in_block(ia32_heights, other, am_candidate))
852                         return 0;
853
854                 return 1;
855         }
856 }
857
858 ir_node *ia32_try_create_Immediate(ir_node *node, char immediate_constraint_type)
859 {
860         ir_mode *const mode = get_irn_mode(node);
861         if (!mode_is_int(mode) && !mode_is_reference(mode))
862                 return NULL;
863
864         ir_node *cnst;
865         ir_node *symconst;
866         if (is_Const(node)) {
867                 cnst     = node;
868                 symconst = NULL;
869         } else if (is_SymConst_addr_ent(node)
870                         && get_entity_owner(get_SymConst_entity(node)) != get_tls_type()) {
871                 cnst     = NULL;
872                 symconst = node;
873         } else if (is_Add(node)) {
874                 ir_node *left  = get_Add_left(node);
875                 ir_node *right = get_Add_right(node);
876                 if (is_Const(left) && is_SymConst_addr_ent(right)) {
877                         cnst     = left;
878                         symconst = right;
879                 } else if (is_SymConst_addr_ent(left) && is_Const(right)) {
880                         cnst     = right;
881                         symconst = left;
882                 } else {
883                         return NULL;
884                 }
885         } else {
886                 return NULL;
887         }
888
889         long val = 0;
890         if (cnst != NULL) {
891                 ir_tarval *offset = get_Const_tarval(cnst);
892                 if (!tarval_is_long(offset)) {
893                         ir_fprintf(stderr, "Optimisation Warning: tarval of %+F is not a long?\n", cnst);
894                         return NULL;
895                 }
896
897                 val = get_tarval_long(offset);
898                 if (!check_immediate_constraint(val, immediate_constraint_type))
899                         return NULL;
900         }
901
902         ir_entity *symconst_ent = NULL;
903         if (symconst != NULL) {
904                 /* we need full 32bits for symconsts */
905                 if (immediate_constraint_type != 'i')
906                         return NULL;
907
908                 symconst_ent = get_SymConst_entity(symconst);
909         }
910
911         ir_graph *const irg = get_irn_irg(node);
912         return ia32_create_Immediate(irg, symconst_ent, 0, val);
913 }