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