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