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