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