cleanup: Remove unnecessary #include "beirg.h".
[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 ( (in->type & arch_register_req_type_limited) == 0
397                 || (out->type & arch_register_req_type_limited) == 0 )
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 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(outreq->type & arch_register_req_type_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(inreq->type & arch_register_req_type_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 const arch_register_req_t *ia32_make_register_req(
767                 const constraint_t *constraint,  int n_outs,
768                 const arch_register_req_t **out_reqs, int 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         } else {
812                 req       = OALLOCZ(obst, arch_register_req_t);
813                 req->type = arch_register_req_type_normal;
814         }
815         req->cls   = constraint->cls;
816         req->width = 1;
817
818         return req;
819 }
820
821 const arch_register_req_t *ia32_parse_clobber(const char *clobber)
822 {
823         if (strcmp(clobber, "memory") == 0 || strcmp(clobber, "cc") == 0)
824                 return NULL;
825
826         struct obstack        *obst = get_irg_obstack(current_ir_graph);
827         const arch_register_t *reg  = ia32_get_clobber_register(clobber);
828         arch_register_req_t   *req;
829         unsigned              *limited;
830
831         if (reg == NULL) {
832                 panic("Register '%s' mentioned in asm clobber is unknown", clobber);
833         }
834
835         assert(reg->index < 32);
836
837         limited  = OALLOC(obst, unsigned);
838         *limited = 1 << reg->index;
839
840         req          = OALLOCZ(obst, arch_register_req_t);
841         req->type    = arch_register_req_type_limited;
842         req->cls     = reg->reg_class;
843         req->limited = limited;
844         req->width   = 1;
845
846         return req;
847 }
848
849
850 int ia32_prevents_AM(ir_node *const block, ir_node *const am_candidate,
851                        ir_node *const other)
852 {
853         if (get_nodes_block(other) != block)
854                 return 0;
855
856         if (is_Sync(other)) {
857                 int i;
858
859                 for (i = get_Sync_n_preds(other) - 1; i >= 0; --i) {
860                         ir_node *const pred = get_Sync_pred(other, i);
861
862                         if (get_nodes_block(pred) != block)
863                                 continue;
864
865                         /* Do not block ourselves from getting eaten */
866                         if (is_Proj(pred) && get_Proj_pred(pred) == am_candidate)
867                                 continue;
868
869                         if (!heights_reachable_in_block(ia32_heights, pred, am_candidate))
870                                 continue;
871
872                         return 1;
873                 }
874
875                 return 0;
876         } else {
877                 /* Do not block ourselves from getting eaten */
878                 if (is_Proj(other) && get_Proj_pred(other) == am_candidate)
879                         return 0;
880
881                 if (!heights_reachable_in_block(ia32_heights, other, am_candidate))
882                         return 0;
883
884                 return 1;
885         }
886 }
887
888 ir_node *ia32_try_create_Immediate(ir_node *node, char immediate_constraint_type)
889 {
890         long       val = 0;
891         ir_entity *symconst_ent  = NULL;
892         ir_mode   *mode;
893         ir_node   *cnst          = NULL;
894         ir_node   *symconst      = NULL;
895         ir_node   *new_node;
896
897         mode = get_irn_mode(node);
898         if (!mode_is_int(mode) && !mode_is_reference(mode)) {
899                 return NULL;
900         }
901
902         if (is_Const(node)) {
903                 cnst     = node;
904                 symconst = NULL;
905         } else if (is_SymConst_addr_ent(node)
906                         && get_entity_owner(get_SymConst_entity(node)) != get_tls_type()) {
907                 cnst     = NULL;
908                 symconst = node;
909         } else if (is_Add(node)) {
910                 ir_node *left  = get_Add_left(node);
911                 ir_node *right = get_Add_right(node);
912                 if (is_Const(left) && is_SymConst_addr_ent(right)) {
913                         cnst     = left;
914                         symconst = right;
915                 } else if (is_SymConst_addr_ent(left) && is_Const(right)) {
916                         cnst     = right;
917                         symconst = left;
918                 }
919         } else {
920                 return NULL;
921         }
922
923         if (cnst != NULL) {
924                 ir_tarval *offset = get_Const_tarval(cnst);
925                 if (!tarval_is_long(offset)) {
926                         ir_fprintf(stderr, "Optimisation Warning: tarval of %+F is not a long?\n", cnst);
927                         return NULL;
928                 }
929
930                 val = get_tarval_long(offset);
931                 if (!check_immediate_constraint(val, immediate_constraint_type))
932                         return NULL;
933         }
934         if (symconst != NULL) {
935                 if (immediate_constraint_type != 0) {
936                         /* we need full 32bits for symconsts */
937                         return NULL;
938                 }
939
940                 symconst_ent = get_SymConst_entity(symconst);
941         }
942         if (cnst == NULL && symconst == NULL)
943                 return NULL;
944
945         new_node = ia32_create_Immediate(symconst_ent, 0, val);
946         return new_node;
947 }