align mode_E konstants at 16 bytes
[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
34 #include "../betranshlp.h"
35 #include "../beirg_t.h"
36
37 #include "ia32_architecture.h"
38 #include "ia32_common_transform.h"
39 #include "ia32_new_nodes.h"
40
41 #include "gen_ia32_new_nodes.h"
42 #include "gen_ia32_regalloc_if.h"
43
44 /** hold the current code generator during transformation */
45 ia32_code_gen_t *env_cg = NULL;
46
47 heights_t *heights = NULL;
48
49 static const arch_register_req_t no_register_req = {
50         arch_register_req_type_none,
51         NULL,                         /* regclass */
52         NULL,                         /* limit bitset */
53         0,                            /* same pos */
54         0                             /* different pos */
55 };
56
57 static int check_immediate_constraint(long val, char immediate_constraint_type)
58 {
59         switch (immediate_constraint_type) {
60                 case 0:
61                 case 'i': return 1;
62
63                 case 'I': return    0 <= val && val <=  31;
64                 case 'J': return    0 <= val && val <=  63;
65                 case 'K': return -128 <= val && val <= 127;
66                 case 'L': return val == 0xff || val == 0xffff;
67                 case 'M': return    0 <= val && val <=   3;
68                 case 'N': return    0 <= val && val <= 255;
69                 case 'O': return    0 <= val && val <= 127;
70
71                 default: panic("Invalid immediate constraint found");
72         }
73 }
74
75 /* creates a unique ident by adding a number to a tag */
76 ident *ia32_unique_id(const char *tag)
77 {
78         static unsigned id = 0;
79         char str[256];
80
81         snprintf(str, sizeof(str), tag, ++id);
82         return new_id_from_str(str);
83 }
84
85 /**
86  * Get a primitive type for a mode with alignment 16.
87  */
88 static ir_type *ia32_get_prim_type(pmap *types, ir_mode *mode)
89 {
90         pmap_entry *e = pmap_find(types, mode);
91         ir_type *res;
92
93         if (! e) {
94                 char buf[64];
95                 snprintf(buf, sizeof(buf), "prim_type_%s", get_mode_name(mode));
96                 res = new_type_primitive(new_id_from_str(buf), mode);
97                 /* FIXME: this is too much for most cases */
98                 set_type_alignment_bytes(res, 16);
99                 pmap_insert(types, mode, res);
100         }
101         else
102                 res = e->value;
103         return res;
104 }
105
106 ir_entity *create_float_const_entity(ir_node *cnst)
107 {
108         ia32_isa_t *isa = env_cg->isa;
109         tarval *key     = get_Const_tarval(cnst);
110         pmap_entry *e   = pmap_find(isa->tv_ent, key);
111         ir_entity *res;
112         ir_graph *rem;
113
114         if (e == NULL) {
115                 tarval  *tv   = key;
116                 ir_mode *mode = get_tarval_mode(tv);
117                 ir_type *tp;
118
119                 if (! ia32_cg_config.use_sse2) {
120                         /* try to reduce the mode to produce smaller sized entities */
121                         if (mode != mode_F) {
122                                 if (tarval_ieee754_can_conv_lossless(tv, mode_F)) {
123                                         mode = mode_F;
124                                         tv = tarval_convert_to(tv, mode);
125                                 } else if (mode != mode_D) {
126                                         if (tarval_ieee754_can_conv_lossless(tv, mode_D)) {
127                                                 mode = mode_D;
128                                                 tv = tarval_convert_to(tv, mode);
129                                         }
130                                 }
131                         }
132                 }
133
134                 if (mode == get_irn_mode(cnst)) {
135                         /* mode was not changed */
136                         tp = get_Const_type(cnst);
137                         if (tp == firm_unknown_type)
138                                 tp = ia32_get_prim_type(isa->types, mode);
139                 } else
140                         tp = ia32_get_prim_type(isa->types, mode);
141
142                 res = new_entity(get_glob_type(), ia32_unique_id(".LC%u"), tp);
143
144                 /* align mode_E at 16 byte for faster access */
145                 if (get_mode_size_bits(mode) >= 80) {
146                         set_entity_align(res, 16);
147                 }
148
149                 set_entity_ld_ident(res, get_entity_ident(res));
150                 set_entity_visibility(res, visibility_local);
151                 set_entity_variability(res, variability_constant);
152                 set_entity_allocation(res, allocation_static);
153
154                  /* we create a new entity here: It's initialization must resist on the
155                     const code irg */
156                 rem = current_ir_graph;
157                 current_ir_graph = get_const_code_irg();
158                 set_atomic_ent_value(res, new_Const_type(tv, tp));
159                 current_ir_graph = rem;
160
161                 pmap_insert(isa->tv_ent, key, res);
162         } else {
163                 res = e->value;
164         }
165
166         return res;
167 }
168
169 ir_node *create_Immediate(ir_entity *symconst, int symconst_sign, long val)
170 {
171         ir_graph *irg         = current_ir_graph;
172         ir_node  *start_block = get_irg_start_block(irg);
173         ir_node  *immediate   = new_bd_ia32_Immediate(NULL, start_block, symconst,
174                         symconst_sign, val);
175         arch_set_irn_register(immediate, &ia32_gp_regs[REG_GP_NOREG]);
176
177         return immediate;
178 }
179
180 const arch_register_t *ia32_get_clobber_register(const char *clobber)
181 {
182         const arch_register_t       *reg = NULL;
183         int                          c;
184         size_t                       r;
185         const arch_register_class_t *cls;
186
187         /* TODO: construct a hashmap instead of doing linear search for clobber
188          * register */
189         for(c = 0; c < N_CLASSES; ++c) {
190                 cls = & ia32_reg_classes[c];
191                 for(r = 0; r < cls->n_regs; ++r) {
192                         const arch_register_t *temp_reg = arch_register_for_index(cls, r);
193                         if (strcmp(temp_reg->name, clobber) == 0
194                                         || (c == CLASS_ia32_gp && strcmp(temp_reg->name+1, clobber) == 0)) {
195                                 reg = temp_reg;
196                                 break;
197                         }
198                 }
199                 if (reg != NULL)
200                         break;
201         }
202
203         return reg;
204 }
205
206 int ia32_mode_needs_gp_reg(ir_mode *mode) {
207         if (mode == mode_fpcw)
208                 return 0;
209         if (get_mode_size_bits(mode) > 32)
210                 return 0;
211         return mode_is_int(mode) || mode_is_reference(mode) || mode == mode_b;
212 }
213
214 static void parse_asm_constraints(constraint_t *constraint, const char *c,
215                            int is_output)
216 {
217         char                         immediate_type     = '\0';
218         unsigned                     limited            = 0;
219         const arch_register_class_t *cls                = NULL;
220         int                          memory_possible       = 0;
221         int                          all_registers_allowed = 0;
222         int                          p;
223         int                          same_as = -1;
224
225         memset(constraint, 0, sizeof(constraint[0]));
226         constraint->same_as = -1;
227
228         if (*c == 0) {
229                 /* a memory constraint: no need to do anything in backend about it
230                  * (the dependencies are already respected by the memory edge of
231                  * the node) */
232                 return;
233         }
234
235         /* TODO: improve error messages with node and source info. (As users can
236          * easily hit these) */
237         while(*c != 0) {
238                 switch(*c) {
239                 case ' ':
240                 case '\t':
241                 case '\n':
242                         break;
243
244                 /* Skip out/in-out marker */
245                 case '=': break;
246                 case '+': break;
247
248                 case '&': break;
249
250                 case '*':
251                         ++c;
252                         break;
253                 case '#':
254                         while(*c != 0 && *c != ',')
255                                 ++c;
256                         break;
257
258                 case 'a':
259                         assert(cls == NULL || cls == &ia32_reg_classes[CLASS_ia32_gp]);
260                         cls      = &ia32_reg_classes[CLASS_ia32_gp];
261                         limited |= 1 << REG_EAX;
262                         break;
263                 case 'b':
264                         assert(cls == NULL || cls == &ia32_reg_classes[CLASS_ia32_gp]);
265                         cls      = &ia32_reg_classes[CLASS_ia32_gp];
266                         limited |= 1 << REG_EBX;
267                         break;
268                 case 'c':
269                         assert(cls == NULL || cls == &ia32_reg_classes[CLASS_ia32_gp]);
270                         cls      = &ia32_reg_classes[CLASS_ia32_gp];
271                         limited |= 1 << REG_ECX;
272                         break;
273                 case 'd':
274                         assert(cls == NULL || cls == &ia32_reg_classes[CLASS_ia32_gp]);
275                         cls      = &ia32_reg_classes[CLASS_ia32_gp];
276                         limited |= 1 << REG_EDX;
277                         break;
278                 case 'D':
279                         assert(cls == NULL || cls == &ia32_reg_classes[CLASS_ia32_gp]);
280                         cls      = &ia32_reg_classes[CLASS_ia32_gp];
281                         limited |= 1 << REG_EDI;
282                         break;
283                 case 'S':
284                         assert(cls == NULL || cls == &ia32_reg_classes[CLASS_ia32_gp]);
285                         cls      = &ia32_reg_classes[CLASS_ia32_gp];
286                         limited |= 1 << REG_ESI;
287                         break;
288                 case 'Q':
289                 case 'q':
290                         /* q means lower part of the regs only, this makes no
291                          * difference to Q for us (we only assign whole registers) */
292                         assert(cls == NULL || cls == &ia32_reg_classes[CLASS_ia32_gp]);
293                         cls      = &ia32_reg_classes[CLASS_ia32_gp];
294                         limited |= 1 << REG_EAX | 1 << REG_EBX | 1 << REG_ECX |
295                                    1 << REG_EDX;
296                         break;
297                 case 'A':
298                         assert(cls == NULL || cls == &ia32_reg_classes[CLASS_ia32_gp]);
299                         cls      = &ia32_reg_classes[CLASS_ia32_gp];
300                         limited |= 1 << REG_EAX | 1 << REG_EDX;
301                         break;
302                 case 'l':
303                         assert(cls == NULL || cls == &ia32_reg_classes[CLASS_ia32_gp]);
304                         cls      = &ia32_reg_classes[CLASS_ia32_gp];
305                         limited |= 1 << REG_EAX | 1 << REG_EBX | 1 << REG_ECX |
306                                    1 << REG_EDX | 1 << REG_ESI | 1 << REG_EDI |
307                                    1 << REG_EBP;
308                         break;
309
310                 case 'R':
311                 case 'r':
312                 case 'p':
313                         if (cls != NULL && cls != &ia32_reg_classes[CLASS_ia32_gp])
314                                 panic("multiple register classes not supported");
315                         cls                   = &ia32_reg_classes[CLASS_ia32_gp];
316                         all_registers_allowed = 1;
317                         break;
318
319                 case 'f':
320                 case 't':
321                 case 'u':
322                         /* TODO: mark values so the x87 simulator knows about t and u */
323                         if (cls != NULL && cls != &ia32_reg_classes[CLASS_ia32_vfp])
324                                 panic("multiple register classes not supported");
325                         cls                   = &ia32_reg_classes[CLASS_ia32_vfp];
326                         all_registers_allowed = 1;
327                         break;
328
329                 case 'Y':
330                 case 'x':
331                         if (cls != NULL && cls != &ia32_reg_classes[CLASS_ia32_xmm])
332                                 panic("multiple register classes not supproted");
333                         cls                   = &ia32_reg_classes[CLASS_ia32_xmm];
334                         all_registers_allowed = 1;
335                         break;
336
337                 case 'I':
338                 case 'J':
339                 case 'K':
340                 case 'L':
341                 case 'M':
342                 case 'N':
343                 case 'O':
344                         if (cls != NULL && cls != &ia32_reg_classes[CLASS_ia32_gp])
345                                 panic("multiple register classes not supported");
346                         if (immediate_type != '\0')
347                                 panic("multiple immediate types not supported");
348                         cls            = &ia32_reg_classes[CLASS_ia32_gp];
349                         immediate_type = *c;
350                         break;
351                 case 'n':
352                 case 'i':
353                         if (cls != NULL && cls != &ia32_reg_classes[CLASS_ia32_gp])
354                                 panic("multiple register classes not supported");
355                         if (immediate_type != '\0')
356                                 panic("multiple immediate types not supported");
357                         cls            = &ia32_reg_classes[CLASS_ia32_gp];
358                         immediate_type = 'i';
359                         break;
360
361                 case 'X':
362                 case 'g':
363                         if (cls != NULL && cls != &ia32_reg_classes[CLASS_ia32_gp])
364                                 panic("multiple register classes not supported");
365                         if (immediate_type != '\0')
366                                 panic("multiple immediate types not supported");
367                         immediate_type        = 'i';
368                         cls                   = &ia32_reg_classes[CLASS_ia32_gp];
369                         all_registers_allowed = 1;
370                         memory_possible       = 1;
371                         break;
372
373                 case '0':
374                 case '1':
375                 case '2':
376                 case '3':
377                 case '4':
378                 case '5':
379                 case '6':
380                 case '7':
381                 case '8':
382                 case '9':
383                         if (is_output)
384                                 panic("can only specify same constraint on input");
385
386                         sscanf(c, "%d%n", &same_as, &p);
387                         if (same_as >= 0) {
388                                 c += p;
389                                 continue;
390                         }
391                         break;
392
393                 case 'm':
394                 case 'o':
395                 case 'V':
396                         /* memory constraint no need to do anything in backend about it
397                          * (the dependencies are already respected by the memory edge of
398                          * the node) */
399                         memory_possible = 1;
400                         break;
401
402                 case 'E': /* no float consts yet */
403                 case 'F': /* no float consts yet */
404                 case 's': /* makes no sense on x86 */
405                 case '<': /* no autodecrement on x86 */
406                 case '>': /* no autoincrement on x86 */
407                 case 'C': /* sse constant not supported yet */
408                 case 'G': /* 80387 constant not supported yet */
409                 case 'y': /* we don't support mmx registers yet */
410                 case 'Z': /* not available in 32 bit mode */
411                 case 'e': /* not available in 32 bit mode */
412                         panic("unsupported asm constraint '%c' found in (%+F)",
413                               *c, current_ir_graph);
414                         break;
415                 default:
416                         panic("unknown asm constraint '%c' found in (%+F)", *c,
417                               current_ir_graph);
418                         break;
419                 }
420                 ++c;
421         }
422
423         if (same_as >= 0) {
424                 if (cls != NULL)
425                         panic("same as and register constraint not supported");
426                 if (immediate_type != '\0')
427                         panic("same as and immediate constraint not supported");
428         }
429
430         if (cls == NULL && same_as < 0) {
431                 if (!memory_possible)
432                         panic("no constraint specified for assembler input");
433         }
434
435         constraint->same_as               = same_as;
436         constraint->cls                   = cls;
437         constraint->allowed_registers     = limited;
438         constraint->all_registers_allowed = all_registers_allowed;
439         constraint->memory_possible       = memory_possible;
440         constraint->immediate_type        = immediate_type;
441 }
442
443 ir_node *gen_ASM(ir_node *node)
444 {
445         ir_node                    *block = NULL;
446         ir_node                    *new_block = NULL;
447         dbg_info                   *dbgi      = get_irn_dbg_info(node);
448         int                         i, arity;
449         int                         out_idx;
450         ir_node                   **in;
451         ir_node                    *new_node;
452         int                         out_arity;
453         int                         n_out_constraints;
454         int                         n_clobbers;
455         const arch_register_req_t **out_reg_reqs;
456         const arch_register_req_t **in_reg_reqs;
457         ia32_asm_reg_t             *register_map;
458         unsigned                    reg_map_size = 0;
459         struct obstack             *obst;
460         const ir_asm_constraint    *in_constraints;
461         const ir_asm_constraint    *out_constraints;
462         ident                     **clobbers;
463         int                         clobbers_flags = 0;
464         unsigned                    clobber_bits[N_CLASSES];
465
466         memset(&clobber_bits, 0, sizeof(clobber_bits));
467
468         switch (be_transformer) {
469         case TRANSFORMER_DEFAULT:
470                 block     = get_nodes_block(node);
471                 new_block = be_transform_node(block);
472                 break;
473
474 #ifdef FIRM_GRGEN_BE
475         case TRANSFORMER_PBQP:
476         case TRANSFORMER_RAND:
477                 new_block = get_nodes_block(node);
478                 break;
479 #endif
480
481         default:
482                 panic("invalid transformer");
483         }
484
485         /* workaround for lots of buggy code out there as most people think volatile
486          * asm is enough for everything and forget the flags (linux kernel, etc.)
487          */
488         if (get_irn_pinned(node) == op_pin_state_pinned) {
489                 clobbers_flags = 1;
490         }
491
492         arity = get_irn_arity(node);
493         in    = ALLOCANZ(ir_node*, arity);
494
495         clobbers   = get_ASM_clobbers(node);
496         n_clobbers = 0;
497         for (i = 0; i < get_ASM_n_clobbers(node); ++i) {
498                 const arch_register_req_t *req;
499                 const char                *c = get_id_str(clobbers[i]);
500
501                 if (strcmp(c, "memory") == 0)
502                         continue;
503                 if (strcmp(c, "cc") == 0) {
504                         clobbers_flags = 1;
505                         continue;
506                 }
507
508                 req = parse_clobber(c);
509                 clobber_bits[req->cls->index] |= *req->limited;
510
511                 n_clobbers++;
512         }
513         n_out_constraints = get_ASM_n_output_constraints(node);
514         out_arity         = n_out_constraints + n_clobbers;
515
516         in_constraints  = get_ASM_input_constraints(node);
517         out_constraints = get_ASM_output_constraints(node);
518
519         /* determine size of register_map */
520         for (out_idx = 0; out_idx < n_out_constraints; ++out_idx) {
521                 const ir_asm_constraint *constraint = &out_constraints[out_idx];
522                 if (constraint->pos > reg_map_size)
523                         reg_map_size = constraint->pos;
524         }
525         for (i = 0; i < arity; ++i) {
526                 const ir_asm_constraint   *constraint = &in_constraints[i];
527                 if (constraint->pos > reg_map_size)
528                         reg_map_size = constraint->pos;
529         }
530         ++reg_map_size;
531
532         obst         = get_irg_obstack(current_ir_graph);
533         register_map = NEW_ARR_D(ia32_asm_reg_t, obst, reg_map_size);
534         memset(register_map, 0, reg_map_size * sizeof(register_map[0]));
535
536         /* construct output constraints */
537         out_reg_reqs = obstack_alloc(obst, out_arity * sizeof(out_reg_reqs[0]));
538
539         for (out_idx = 0; out_idx < n_out_constraints; ++out_idx) {
540                 const ir_asm_constraint   *constraint = &out_constraints[out_idx];
541                 const char                *c       = get_id_str(constraint->constraint);
542                 unsigned                   pos        = constraint->pos;
543                 constraint_t               parsed_constraint;
544                 const arch_register_req_t *req;
545
546                 parse_asm_constraints(&parsed_constraint, c, 1);
547                 req = make_register_req(&parsed_constraint, n_out_constraints,
548                                         out_reg_reqs, out_idx);
549                 out_reg_reqs[out_idx] = req;
550
551                 register_map[pos].use_input = 0;
552                 register_map[pos].valid     = 1;
553                 register_map[pos].memory    = 0;
554                 register_map[pos].inout_pos = out_idx;
555                 register_map[pos].mode      = constraint->mode;
556         }
557
558         /* inputs + input constraints */
559         in_reg_reqs = obstack_alloc(obst, arity * sizeof(in_reg_reqs[0]));
560         for (i = 0; i < arity; ++i) {
561                 ir_node                   *pred         = get_irn_n(node, i);
562                 const ir_asm_constraint   *constraint   = &in_constraints[i];
563                 ident                     *constr_id    = constraint->constraint;
564                 const char                *c            = get_id_str(constr_id);
565                 unsigned                   pos          = constraint->pos;
566                 int                        is_memory_op = 0;
567                 ir_node                   *input        = NULL;
568                 unsigned                   r_clobber_bits;
569                 constraint_t               parsed_constraint;
570                 const arch_register_req_t *req;
571
572                 parse_asm_constraints(&parsed_constraint, c, 0);
573                 if (parsed_constraint.cls != NULL) {
574                         r_clobber_bits = clobber_bits[parsed_constraint.cls->index];
575                         if (r_clobber_bits != 0) {
576                                 if (parsed_constraint.all_registers_allowed) {
577                                         parsed_constraint.all_registers_allowed = 0;
578                                         be_abi_set_non_ignore_regs(env_cg->birg->abi,
579                                                         parsed_constraint.cls,
580                                                         &parsed_constraint.allowed_registers);
581                                 }
582                                 parsed_constraint.allowed_registers &= ~r_clobber_bits;
583                         }
584                 }
585
586                 req = make_register_req(&parsed_constraint, n_out_constraints,
587                                         out_reg_reqs, i);
588                 in_reg_reqs[i] = req;
589
590                 if (parsed_constraint.immediate_type != '\0') {
591                         char imm_type = parsed_constraint.immediate_type;
592                         input = try_create_Immediate(pred, imm_type);
593                 }
594
595                 if (input == NULL) {
596                         ir_node *pred = NULL;
597                         switch (be_transformer) {
598                         case TRANSFORMER_DEFAULT:
599                                 pred  = get_irn_n(node, i);
600                                 input = be_transform_node(pred);
601                                 break;
602
603 #ifdef FIRM_GRGEN_BE
604                         case TRANSFORMER_PBQP:
605                         case TRANSFORMER_RAND:
606                                 input = get_irn_n(node, i);
607                                 break;
608 #endif
609
610                         default: panic("invalid transformer");
611                         }
612
613                         if (parsed_constraint.cls == NULL
614                                         && parsed_constraint.same_as < 0) {
615                                 is_memory_op = 1;
616                         } else if (parsed_constraint.memory_possible) {
617                                 /* TODO: match Load or Load/Store if memory possible is set */
618                         }
619                 }
620                 in[i] = input;
621
622                 register_map[pos].use_input = 1;
623                 register_map[pos].valid     = 1;
624                 register_map[pos].memory    = is_memory_op;
625                 register_map[pos].inout_pos = i;
626                 register_map[pos].mode      = constraint->mode;
627         }
628
629         /* parse clobbers */
630         for (i = 0; i < get_ASM_n_clobbers(node); ++i) {
631                 const char                *c = get_id_str(clobbers[i]);
632                 const arch_register_req_t *req;
633
634                 if (strcmp(c, "memory") == 0 || strcmp(c, "cc") == 0)
635                         continue;
636
637                 req = parse_clobber(c);
638                 out_reg_reqs[out_idx] = req;
639                 ++out_idx;
640         }
641
642         new_node = new_bd_ia32_Asm(dbgi, new_block, arity, in, out_arity,
643                                    get_ASM_text(node), register_map);
644
645         if (arity == 0)
646                 be_dep_on_frame(new_node);
647
648         set_ia32_out_req_all(new_node, out_reg_reqs);
649         set_ia32_in_req_all(new_node, in_reg_reqs);
650
651         SET_IA32_ORIG_NODE(new_node, node);
652
653         return new_node;
654 }
655
656 ir_node *gen_CopyB(ir_node *node) {
657         ir_node  *block    = NULL;
658         ir_node  *src      = NULL;
659         ir_node  *new_src  = NULL;
660         ir_node  *dst      = NULL;
661         ir_node  *new_dst  = NULL;
662         ir_node  *mem      = NULL;
663         ir_node  *new_mem  = NULL;
664         ir_node  *res      = NULL;
665         dbg_info *dbgi     = get_irn_dbg_info(node);
666         int      size      = get_type_size_bytes(get_CopyB_type(node));
667         int      rem;
668
669         switch (be_transformer) {
670                 case TRANSFORMER_DEFAULT:
671                         block    = be_transform_node(get_nodes_block(node));
672                         src      = get_CopyB_src(node);
673                         new_src  = be_transform_node(src);
674                         dst      = get_CopyB_dst(node);
675                         new_dst  = be_transform_node(dst);
676                         mem      = get_CopyB_mem(node);
677                         new_mem  = be_transform_node(mem);
678                         break;
679
680 #ifdef FIRM_GRGEN_BE
681                 case TRANSFORMER_PBQP:
682                 case TRANSFORMER_RAND:
683                         block    = get_nodes_block(node);
684                         new_src  = get_CopyB_src(node);
685                         new_dst  = get_CopyB_dst(node);
686                         new_mem  = get_CopyB_mem(node);
687                         break;
688 #endif
689
690                 default: panic("invalid transformer");
691         }
692
693         /* If we have to copy more than 32 bytes, we use REP MOVSx and */
694         /* then we need the size explicitly in ECX.                    */
695         if (size >= 32 * 4) {
696                 rem = size & 0x3; /* size % 4 */
697                 size >>= 2;
698
699                 res = new_bd_ia32_Const(dbgi, block, NULL, 0, size);
700                 be_dep_on_frame(res);
701
702                 res = new_bd_ia32_CopyB(dbgi, block, new_dst, new_src, res, new_mem, rem);
703         } else {
704                 if (size == 0) {
705                         ir_fprintf(stderr, "Optimization warning copyb %+F with size <4\n",
706                                    node);
707                 }
708                 res = new_bd_ia32_CopyB_i(dbgi, block, new_dst, new_src, new_mem, size);
709         }
710
711         SET_IA32_ORIG_NODE(res, node);
712
713         return res;
714 }
715
716 ir_node *gen_Proj_tls(ir_node *node) {
717         ir_node  *block = NULL;
718         dbg_info *dbgi  = NULL;
719         ir_node  *res   = NULL;
720
721         switch (be_transformer) {
722                 case TRANSFORMER_DEFAULT:
723                         block = be_transform_node(get_nodes_block(node));
724                         break;
725
726 #ifdef FIRM_GRGEN_BE
727                 case TRANSFORMER_PBQP:
728                 case TRANSFORMER_RAND:
729                         block = get_nodes_block(node);
730                         break;
731 #endif
732
733                 default: panic("invalid transformer");
734         }
735
736         res = new_bd_ia32_LdTls(dbgi, block, mode_Iu);
737
738         return res;
739 }
740
741 ir_node *gen_Unknown(ir_node *node)
742 {
743         ir_mode *mode = get_irn_mode(node);
744
745         if (mode_is_float(mode)) {
746                 if (ia32_cg_config.use_sse2) {
747                         return ia32_new_Unknown_xmm(env_cg);
748                 } else {
749                         /* Unknown nodes are buggy in x87 simulator, use zero for now... */
750                         ir_graph *irg   = current_ir_graph;
751                         dbg_info *dbgi  = get_irn_dbg_info(node);
752                         ir_node  *block = get_irg_start_block(irg);
753                         ir_node  *ret   = new_bd_ia32_vfldz(dbgi, block);
754
755                         be_dep_on_frame(ret);
756                         return ret;
757                 }
758         } else if (ia32_mode_needs_gp_reg(mode)) {
759                 return ia32_new_Unknown_gp(env_cg);
760         } else {
761                 panic("unsupported Unknown-Mode");
762         }
763         return NULL;
764 }
765
766 const arch_register_req_t *make_register_req(const constraint_t *constraint,
767                 int n_outs, const arch_register_req_t **out_reqs, int pos)
768 {
769         struct obstack      *obst    = get_irg_obstack(current_ir_graph);
770         int                  same_as = constraint->same_as;
771         arch_register_req_t *req;
772
773         if (same_as >= 0) {
774                 const arch_register_req_t *other_constr;
775
776                 if (same_as >= n_outs)
777                         panic("invalid output number in same_as constraint");
778
779                 other_constr     = out_reqs[same_as];
780
781                 req              = obstack_alloc(obst, sizeof(req[0]));
782                 *req             = *other_constr;
783                 req->type       |= arch_register_req_type_should_be_same;
784                 req->other_same  = 1U << pos;
785
786                 /* switch constraints. This is because in firm we have same_as
787                  * constraints on the output constraints while in the gcc asm syntax
788                  * they are specified on the input constraints */
789                 out_reqs[same_as] = req;
790                 return other_constr;
791         }
792
793         /* pure memory ops */
794         if (constraint->cls == NULL) {
795                 return &no_register_req;
796         }
797
798         if (constraint->allowed_registers != 0
799                         && !constraint->all_registers_allowed) {
800                 unsigned *limited_ptr;
801
802                 req         = obstack_alloc(obst, sizeof(req[0]) + sizeof(unsigned));
803                 memset(req, 0, sizeof(req[0]));
804                 limited_ptr = (unsigned*) (req+1);
805
806                 req->type    = arch_register_req_type_limited;
807                 *limited_ptr = constraint->allowed_registers;
808                 req->limited = limited_ptr;
809         } else {
810                 req       = obstack_alloc(obst, sizeof(req[0]));
811                 memset(req, 0, sizeof(req[0]));
812                 req->type = arch_register_req_type_normal;
813         }
814         req->cls = constraint->cls;
815
816         return req;
817 }
818
819 const arch_register_req_t *parse_clobber(const char *clobber)
820 {
821         struct obstack        *obst = get_irg_obstack(current_ir_graph);
822         const arch_register_t *reg  = ia32_get_clobber_register(clobber);
823         arch_register_req_t   *req;
824         unsigned              *limited;
825
826         if (reg == NULL) {
827                 panic("Register '%s' mentioned in asm clobber is unknown", clobber);
828         }
829
830         assert(reg->index < 32);
831
832         limited  = obstack_alloc(obst, sizeof(limited[0]));
833         *limited = 1 << reg->index;
834
835         req          = obstack_alloc(obst, sizeof(req[0]));
836         memset(req, 0, sizeof(req[0]));
837         req->type    = arch_register_req_type_limited;
838         req->cls     = arch_register_get_class(reg);
839         req->limited = limited;
840
841         return req;
842 }
843
844
845 int prevents_AM(ir_node *const block, ir_node *const am_candidate,
846                        ir_node *const other)
847 {
848         if (get_nodes_block(other) != block)
849                 return 0;
850
851         if (is_Sync(other)) {
852                 int i;
853
854                 for (i = get_Sync_n_preds(other) - 1; i >= 0; --i) {
855                         ir_node *const pred = get_Sync_pred(other, i);
856
857                         if (get_nodes_block(pred) != block)
858                                 continue;
859
860                         /* Do not block ourselves from getting eaten */
861                         if (is_Proj(pred) && get_Proj_pred(pred) == am_candidate)
862                                 continue;
863
864                         if (!heights_reachable_in_block(heights, pred, am_candidate))
865                                 continue;
866
867                         return 1;
868                 }
869
870                 return 0;
871         } else {
872                 /* Do not block ourselves from getting eaten */
873                 if (is_Proj(other) && get_Proj_pred(other) == am_candidate)
874                         return 0;
875
876                 if (!heights_reachable_in_block(heights, other, am_candidate))
877                         return 0;
878
879                 return 1;
880         }
881 }
882
883 ir_node *try_create_Immediate(ir_node *node, char immediate_constraint_type)
884 {
885         int          minus         = 0;
886         tarval      *offset        = NULL;
887         int          offset_sign   = 0;
888         long         val = 0;
889         ir_entity   *symconst_ent  = NULL;
890         int          symconst_sign = 0;
891         ir_mode     *mode;
892         ir_node     *cnst          = NULL;
893         ir_node     *symconst      = NULL;
894         ir_node     *new_node;
895
896         mode = get_irn_mode(node);
897         if (!mode_is_int(mode) && !mode_is_reference(mode)) {
898                 return NULL;
899         }
900
901         if (is_Minus(node)) {
902                 minus = 1;
903                 node  = get_Minus_op(node);
904         }
905
906         if (is_Const(node)) {
907                 cnst        = node;
908                 symconst    = NULL;
909                 offset_sign = minus;
910         } else if (is_SymConst(node)) {
911                 cnst          = NULL;
912                 symconst      = node;
913                 symconst_sign = minus;
914         } else if (is_Add(node)) {
915                 ir_node *left  = get_Add_left(node);
916                 ir_node *right = get_Add_right(node);
917                 if (is_Const(left) && is_SymConst(right)) {
918                         cnst          = left;
919                         symconst      = right;
920                         symconst_sign = minus;
921                         offset_sign   = minus;
922                 } else if (is_SymConst(left) && is_Const(right)) {
923                         cnst          = right;
924                         symconst      = left;
925                         symconst_sign = minus;
926                         offset_sign   = minus;
927                 }
928         } else if (is_Sub(node)) {
929                 ir_node *left  = get_Sub_left(node);
930                 ir_node *right = get_Sub_right(node);
931                 if (is_Const(left) && is_SymConst(right)) {
932                         cnst          = left;
933                         symconst      = right;
934                         symconst_sign = !minus;
935                         offset_sign   = minus;
936                 } else if (is_SymConst(left) && is_Const(right)) {
937                         cnst          = right;
938                         symconst      = left;
939                         symconst_sign = minus;
940                         offset_sign   = !minus;
941                 }
942         } else {
943                 return NULL;
944         }
945
946         if (cnst != NULL) {
947                 offset = get_Const_tarval(cnst);
948                 if (tarval_is_long(offset)) {
949                         val = get_tarval_long(offset);
950                 } else {
951                         ir_fprintf(stderr, "Optimisation Warning: tarval from %+F is not a "
952                                    "long?\n", cnst);
953                         return NULL;
954                 }
955
956                 if (!check_immediate_constraint(val, immediate_constraint_type))
957                         return NULL;
958         }
959         if (symconst != NULL) {
960                 if (immediate_constraint_type != 0) {
961                         /* we need full 32bits for symconsts */
962                         return NULL;
963                 }
964
965                 /* unfortunately the assembler/linker doesn't support -symconst */
966                 if (symconst_sign)
967                         return NULL;
968
969                 if (get_SymConst_kind(symconst) != symconst_addr_ent)
970                         return NULL;
971                 symconst_ent = get_SymConst_entity(symconst);
972         }
973         if (cnst == NULL && symconst == NULL)
974                 return NULL;
975
976         if (offset_sign && offset != NULL) {
977                 offset = tarval_neg(offset);
978         }
979
980         new_node = create_Immediate(symconst_ent, symconst_sign, val);
981
982         return new_node;
983 }