b5ca4edf90878bc0aa1e5ca66505c5f705077325
[libfirm] / ir / be / ia32 / ia32_x87.c
1 /*
2  * Copyright (C) 1995-2010 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 x87 support and virtual to stack
23  *              register translation for the ia32 backend.
24  * @author      Michael Beck
25  */
26 #include "config.h"
27
28 #include <assert.h>
29
30 #include "irnode_t.h"
31 #include "irop_t.h"
32 #include "irprog.h"
33 #include "iredges_t.h"
34 #include "irgmod.h"
35 #include "ircons.h"
36 #include "irgwalk.h"
37 #include "obst.h"
38 #include "pmap.h"
39 #include "array_t.h"
40 #include "pdeq.h"
41 #include "irprintf.h"
42 #include "debug.h"
43 #include "error.h"
44
45 #include "belive_t.h"
46 #include "besched.h"
47 #include "benode.h"
48 #include "bearch_ia32_t.h"
49 #include "ia32_new_nodes.h"
50 #include "gen_ia32_new_nodes.h"
51 #include "gen_ia32_regalloc_if.h"
52 #include "ia32_x87.h"
53 #include "ia32_architecture.h"
54
55 /** the debug handle */
56 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
57
58 /* Forward declaration. */
59 typedef struct x87_simulator x87_simulator;
60
61 /**
62  * An exchange template.
63  * Note that our virtual functions have the same inputs
64  * and attributes as the real ones, so we can simple exchange
65  * their opcodes!
66  * Further, x87 supports inverse instructions, so we can handle them.
67  */
68 typedef struct exchange_tmpl {
69         ir_op *normal_op;       /**< the normal one */
70         ir_op *reverse_op;      /**< the reverse one if exists */
71         ir_op *normal_pop_op;   /**< the normal one with tos pop */
72         ir_op *reverse_pop_op;  /**< the reverse one with tos pop */
73 } exchange_tmpl;
74
75 /**
76  * An entry on the simulated x87 stack.
77  */
78 typedef struct st_entry {
79         int     reg_idx;        /**< the virtual register index of this stack value */
80         ir_node *node;          /**< the node that produced this value */
81 } st_entry;
82
83 /**
84  * The x87 state.
85  */
86 typedef struct x87_state {
87         st_entry st[N_ia32_st_REGS]; /**< the register stack */
88         int depth;                   /**< the current stack depth */
89         x87_simulator *sim;          /**< The simulator. */
90 } x87_state;
91
92 /** An empty state, used for blocks without fp instructions. */
93 static x87_state empty = { { {0, NULL}, }, 0, NULL };
94
95 /**
96  * Return values of the instruction simulator functions.
97  */
98 enum {
99         NO_NODE_ADDED = 0,  /**< No node that needs simulation was added. */
100         NODE_ADDED    = 1   /**< A node that must be simulated was added by the simulator
101                                  in the schedule AFTER the current node. */
102 };
103
104 /**
105  * The type of an instruction simulator function.
106  *
107  * @param state  the x87 state
108  * @param n      the node to be simulated
109  *
110  * @return NODE_ADDED    if a node was added AFTER n in schedule that MUST be
111  *                       simulated further
112  *         NO_NODE_ADDED otherwise
113  */
114 typedef int (*sim_func)(x87_state *state, ir_node *n);
115
116 /**
117  * A block state: Every block has a x87 state at the beginning and at the end.
118  */
119 typedef struct blk_state {
120         x87_state *begin;   /**< state at the begin or NULL if not assigned */
121         x87_state *end;     /**< state at the end or NULL if not assigned */
122 } blk_state;
123
124 /** liveness bitset for vfp registers. */
125 typedef unsigned char vfp_liveness;
126
127 /**
128  * The x87 simulator.
129  */
130 struct x87_simulator {
131         struct obstack obst;        /**< An obstack for fast allocating. */
132         pmap *blk_states;           /**< Map blocks to states. */
133         be_lv_t *lv;                /**< intrablock liveness. */
134         vfp_liveness *live;         /**< Liveness information. */
135         unsigned n_idx;             /**< The cached get_irg_last_idx() result. */
136         waitq *worklist;            /**< Worklist of blocks that must be processed. */
137 };
138
139 /**
140  * Returns the current stack depth.
141  *
142  * @param state  the x87 state
143  *
144  * @return the x87 stack depth
145  */
146 static int x87_get_depth(const x87_state *state)
147 {
148         return state->depth;
149 }
150
151 static st_entry *x87_get_entry(x87_state *const state, int const pos)
152 {
153         assert(0 <= pos && pos < state->depth);
154         return &state->st[N_ia32_st_REGS - state->depth + pos];
155 }
156
157 /**
158  * Return the virtual register index at st(pos).
159  *
160  * @param state  the x87 state
161  * @param pos    a stack position
162  *
163  * @return the vfp register index that produced the value at st(pos)
164  */
165 static int x87_get_st_reg(const x87_state *state, int pos)
166 {
167         return x87_get_entry((x87_state*)state, pos)->reg_idx;
168 }
169
170 #ifdef DEBUG_libfirm
171 /**
172  * Dump the stack for debugging.
173  *
174  * @param state  the x87 state
175  */
176 static void x87_dump_stack(const x87_state *state)
177 {
178         for (int i = state->depth; i-- != 0;) {
179                 st_entry const *const entry = x87_get_entry((x87_state*)state, i);
180                 DB((dbg, LEVEL_2, "vf%d(%+F) ", entry->reg_idx, entry->node));
181         }
182         DB((dbg, LEVEL_2, "<-- TOS\n"));
183 }
184 #endif /* DEBUG_libfirm */
185
186 /**
187  * Set a virtual register to st(pos).
188  *
189  * @param state    the x87 state
190  * @param reg_idx  the vfp register index that should be set
191  * @param node     the IR node that produces the value of the vfp register
192  * @param pos      the stack position where the new value should be entered
193  */
194 static void x87_set_st(x87_state *state, int reg_idx, ir_node *node, int pos)
195 {
196         st_entry *const entry = x87_get_entry(state, pos);
197         entry->reg_idx = reg_idx;
198         entry->node    = node;
199
200         DB((dbg, LEVEL_2, "After SET_REG: "));
201         DEBUG_ONLY(x87_dump_stack(state);)
202 }
203
204 /**
205  * Set the tos virtual register.
206  *
207  * @param state    the x87 state
208  * @param reg_idx  the vfp register index that should be set
209  * @param node     the IR node that produces the value of the vfp register
210  */
211 static void x87_set_tos(x87_state *state, int reg_idx, ir_node *node)
212 {
213         x87_set_st(state, reg_idx, node, 0);
214 }
215
216 /**
217  * Swap st(0) with st(pos).
218  *
219  * @param state    the x87 state
220  * @param pos      the stack position to change the tos with
221  */
222 static void x87_fxch(x87_state *state, int pos)
223 {
224         st_entry *const a = x87_get_entry(state, pos);
225         st_entry *const b = x87_get_entry(state, 0);
226         st_entry  const t = *a;
227         *a = *b;
228         *b = t;
229
230         DB((dbg, LEVEL_2, "After FXCH: "));
231         DEBUG_ONLY(x87_dump_stack(state);)
232 }
233
234 /**
235  * Convert a virtual register to the stack index.
236  *
237  * @param state    the x87 state
238  * @param reg_idx  the register vfp index
239  *
240  * @return the stack position where the register is stacked
241  *         or -1 if the virtual register was not found
242  */
243 static int x87_on_stack(const x87_state *state, int reg_idx)
244 {
245         for (int i = 0; i < state->depth; ++i) {
246                 if (x87_get_st_reg(state, i) == reg_idx)
247                         return i;
248         }
249         return -1;
250 }
251
252 /**
253  * Push a virtual Register onto the stack, double pushed allowed.
254  *
255  * @param state     the x87 state
256  * @param reg_idx   the register vfp index
257  * @param node      the node that produces the value of the vfp register
258  */
259 static void x87_push_dbl(x87_state *state, int reg_idx, ir_node *node)
260 {
261         assert(state->depth < N_ia32_st_REGS && "stack overrun");
262
263         ++state->depth;
264         st_entry *const entry = x87_get_entry(state, 0);
265         entry->reg_idx = reg_idx;
266         entry->node    = node;
267
268         DB((dbg, LEVEL_2, "After PUSH: ")); DEBUG_ONLY(x87_dump_stack(state);)
269 }
270
271 /**
272  * Push a virtual Register onto the stack, double pushes are NOT allowed.
273  *
274  * @param state     the x87 state
275  * @param reg_idx   the register vfp index
276  * @param node      the node that produces the value of the vfp register
277  */
278 static void x87_push(x87_state *state, int reg_idx, ir_node *node)
279 {
280         assert(x87_on_stack(state, reg_idx) == -1 && "double push");
281
282         x87_push_dbl(state, reg_idx, node);
283 }
284
285 /**
286  * Pop a virtual Register from the stack.
287  *
288  * @param state     the x87 state
289  */
290 static void x87_pop(x87_state *state)
291 {
292         assert(state->depth > 0 && "stack underrun");
293
294         --state->depth;
295
296         DB((dbg, LEVEL_2, "After POP: ")); DEBUG_ONLY(x87_dump_stack(state);)
297 }
298
299 /**
300  * Empty the fpu stack
301  *
302  * @param state     the x87 state
303  */
304 static void x87_emms(x87_state *state)
305 {
306         state->depth = 0;
307 }
308
309 /**
310  * Returns the block state of a block.
311  *
312  * @param sim    the x87 simulator handle
313  * @param block  the current block
314  *
315  * @return the block state
316  */
317 static blk_state *x87_get_bl_state(x87_simulator *sim, ir_node *block)
318 {
319         blk_state *res = pmap_get(blk_state, sim->blk_states, block);
320
321         if (res == NULL) {
322                 res = OALLOC(&sim->obst, blk_state);
323                 res->begin = NULL;
324                 res->end   = NULL;
325
326                 pmap_insert(sim->blk_states, block, res);
327         }
328
329         return res;
330 }
331
332 /**
333  * Clone a x87 state.
334  *
335  * @param sim    the x87 simulator handle
336  * @param src    the x87 state that will be cloned
337  *
338  * @return a cloned copy of the src state
339  */
340 static x87_state *x87_clone_state(x87_simulator *sim, const x87_state *src)
341 {
342         x87_state *const res = OALLOC(&sim->obst, x87_state);
343         *res = *src;
344         return res;
345 }
346
347 /**
348  * Patch a virtual instruction into a x87 one and return
349  * the node representing the result value.
350  *
351  * @param n   the IR node to patch
352  * @param op  the x87 opcode to patch in
353  */
354 static ir_node *x87_patch_insn(ir_node *n, ir_op *op)
355 {
356         ir_mode *mode = get_irn_mode(n);
357         ir_node *res = n;
358
359         set_irn_op(n, op);
360
361         if (mode == mode_T) {
362                 /* patch all Proj's */
363                 foreach_out_edge(n, edge) {
364                         ir_node *proj = get_edge_src_irn(edge);
365                         if (is_Proj(proj)) {
366                                 mode = get_irn_mode(proj);
367                                 if (mode_is_float(mode)) {
368                                         res = proj;
369                                         set_irn_mode(proj, ia32_reg_classes[CLASS_ia32_st].mode);
370                                 }
371                         }
372                 }
373         } else if (mode_is_float(mode))
374                 set_irn_mode(n, ia32_reg_classes[CLASS_ia32_st].mode);
375         return res;
376 }
377
378 /**
379  * Returns the first Proj of a mode_T node having a given mode.
380  *
381  * @param n  the mode_T node
382  * @param m  the desired mode of the Proj
383  * @return The first Proj of mode @p m found or NULL.
384  */
385 static ir_node *get_irn_Proj_for_mode(ir_node *n, ir_mode *m)
386 {
387         assert(get_irn_mode(n) == mode_T && "Need mode_T node");
388
389         foreach_out_edge(n, edge) {
390                 ir_node *proj = get_edge_src_irn(edge);
391                 if (get_irn_mode(proj) == m)
392                         return proj;
393         }
394
395         return NULL;
396 }
397
398 /**
399  * Wrap the arch_* function here so we can check for errors.
400  */
401 static inline const arch_register_t *x87_get_irn_register(const ir_node *irn)
402 {
403         const arch_register_t *res = arch_get_irn_register(irn);
404
405         assert(res->reg_class == &ia32_reg_classes[CLASS_ia32_vfp]);
406         return res;
407 }
408
409 static inline const arch_register_t *x87_irn_get_register(const ir_node *irn,
410                                                           int pos)
411 {
412         const arch_register_t *res = arch_get_irn_register_out(irn, pos);
413
414         assert(res->reg_class == &ia32_reg_classes[CLASS_ia32_vfp]);
415         return res;
416 }
417
418 static inline const arch_register_t *get_st_reg(int index)
419 {
420         return &ia32_registers[REG_ST0 + index];
421 }
422
423 /* -------------- x87 perm --------------- */
424
425 /**
426  * Creates a fxch for shuffle.
427  *
428  * @param state     the x87 state
429  * @param pos       parameter for fxch
430  * @param block     the block were fxch is inserted
431  *
432  * Creates a new fxch node and reroute the user of the old node
433  * to the fxch.
434  *
435  * @return the fxch node
436  */
437 static ir_node *x87_fxch_shuffle(x87_state *state, int pos, ir_node *block)
438 {
439         ir_node         *fxch;
440         ia32_x87_attr_t *attr;
441
442         fxch = new_bd_ia32_fxch(NULL, block);
443         attr = get_ia32_x87_attr(fxch);
444         attr->x87[0] = get_st_reg(pos);
445         attr->x87[2] = get_st_reg(0);
446
447         keep_alive(fxch);
448
449         x87_fxch(state, pos);
450         return fxch;
451 }
452
453 /**
454  * Calculate the necessary permutations to reach dst_state.
455  *
456  * These permutations are done with fxch instructions and placed
457  * at the end of the block.
458  *
459  * Note that critical edges are removed here, so we need only
460  * a shuffle if the current block has only one successor.
461  *
462  * @param block      the current block
463  * @param state      the current x87 stack state, might be modified
464  * @param dst_state  destination state
465  *
466  * @return state
467  */
468 static x87_state *x87_shuffle(ir_node *block, x87_state *state, const x87_state *dst_state)
469 {
470         int      i, n_cycles, k, ri;
471         unsigned cycles[4], all_mask;
472         char     cycle_idx[4][8];
473         ir_node  *fxch, *before, *after;
474
475         assert(state->depth == dst_state->depth);
476
477         /* Some mathematics here:
478            If we have a cycle of length n that includes the tos,
479            we need n-1 exchange operations.
480            We can always add the tos and restore it, so we need
481            n+1 exchange operations for a cycle not containing the tos.
482            So, the maximum of needed operations is for a cycle of 7
483            not including the tos == 8.
484            This is the same number of ops we would need for using stores,
485            so exchange is cheaper (we save the loads).
486            On the other hand, we might need an additional exchange
487            in the next block to bring one operand on top, so the
488            number of ops in the first case is identical.
489            Further, no more than 4 cycles can exists (4 x 2).
490         */
491         all_mask = (1 << (state->depth)) - 1;
492
493         for (n_cycles = 0; all_mask; ++n_cycles) {
494                 int src_idx, dst_idx;
495
496                 /* find the first free slot */
497                 for (i = 0; i < state->depth; ++i) {
498                         if (all_mask & (1 << i)) {
499                                 all_mask &= ~(1 << i);
500
501                                 /* check if there are differences here */
502                                 if (x87_get_st_reg(state, i) != x87_get_st_reg(dst_state, i))
503                                         break;
504                         }
505                 }
506
507                 if (! all_mask) {
508                         /* no more cycles found */
509                         break;
510                 }
511
512                 k = 0;
513                 cycles[n_cycles] = (1 << i);
514                 cycle_idx[n_cycles][k++] = i;
515                 for (src_idx = i; ; src_idx = dst_idx) {
516                         dst_idx = x87_on_stack(dst_state, x87_get_st_reg(state, src_idx));
517
518                         if ((all_mask & (1 << dst_idx)) == 0)
519                                 break;
520
521                         cycle_idx[n_cycles][k++] = dst_idx;
522                         cycles[n_cycles] |=  (1 << dst_idx);
523                         all_mask       &= ~(1 << dst_idx);
524                 }
525                 cycle_idx[n_cycles][k] = -1;
526         }
527
528         if (n_cycles <= 0) {
529                 /* no permutation needed */
530                 return state;
531         }
532
533         /* Hmm: permutation needed */
534         DB((dbg, LEVEL_2, "\n%+F needs permutation: from\n", block));
535         DEBUG_ONLY(x87_dump_stack(state);)
536         DB((dbg, LEVEL_2, "                  to\n"));
537         DEBUG_ONLY(x87_dump_stack(dst_state);)
538
539
540 #ifdef DEBUG_libfirm
541         DB((dbg, LEVEL_2, "Need %d cycles\n", n_cycles));
542         for (ri = 0; ri < n_cycles; ++ri) {
543                 DB((dbg, LEVEL_2, " Ring %d:\n ", ri));
544                 for (k = 0; cycle_idx[ri][k] != -1; ++k)
545                         DB((dbg, LEVEL_2, " st%d ->", cycle_idx[ri][k]));
546                 DB((dbg, LEVEL_2, "\n"));
547         }
548 #endif
549
550         after = NULL;
551
552         /*
553          * Find the place node must be insert.
554          * We have only one successor block, so the last instruction should
555          * be a jump.
556          */
557         before = sched_last(block);
558         assert(is_cfop(before));
559
560         /* now do the permutations */
561         for (ri = 0; ri < n_cycles; ++ri) {
562                 if ((cycles[ri] & 1) == 0) {
563                         /* this cycle does not include the tos */
564                         fxch = x87_fxch_shuffle(state, cycle_idx[ri][0], block);
565                         if (after)
566                                 sched_add_after(after, fxch);
567                         else
568                                 sched_add_before(before, fxch);
569                         after = fxch;
570                 }
571                 for (k = 1; cycle_idx[ri][k] != -1; ++k) {
572                         fxch = x87_fxch_shuffle(state, cycle_idx[ri][k], block);
573                         if (after)
574                                 sched_add_after(after, fxch);
575                         else
576                                 sched_add_before(before, fxch);
577                         after = fxch;
578                 }
579                 if ((cycles[ri] & 1) == 0) {
580                         /* this cycle does not include the tos */
581                         fxch = x87_fxch_shuffle(state, cycle_idx[ri][0], block);
582                         sched_add_after(after, fxch);
583                 }
584         }
585         return state;
586 }
587
588 /**
589  * Create a fxch node before another node.
590  *
591  * @param state   the x87 state
592  * @param n       the node after the fxch
593  * @param pos     exchange st(pos) with st(0)
594  *
595  * @return the fxch
596  */
597 static ir_node *x87_create_fxch(x87_state *state, ir_node *n, int pos)
598 {
599         ir_node         *fxch;
600         ia32_x87_attr_t *attr;
601         ir_node         *block = get_nodes_block(n);
602
603         x87_fxch(state, pos);
604
605         fxch = new_bd_ia32_fxch(NULL, block);
606         attr = get_ia32_x87_attr(fxch);
607         attr->x87[0] = get_st_reg(pos);
608         attr->x87[2] = get_st_reg(0);
609
610         keep_alive(fxch);
611
612         sched_add_before(n, fxch);
613         DB((dbg, LEVEL_1, "<<< %s %s, %s\n", get_irn_opname(fxch), attr->x87[0]->name, attr->x87[2]->name));
614         return fxch;
615 }
616
617 /**
618  * Create a fpush before node n.
619  *
620  * @param state     the x87 state
621  * @param n         the node after the fpush
622  * @param pos       push st(pos) on stack
623  * @param op_idx    replace input op_idx of n with the fpush result
624  */
625 static void x87_create_fpush(x87_state *state, ir_node *n, int pos, int op_idx)
626 {
627         ir_node               *fpush, *pred = get_irn_n(n, op_idx);
628         ia32_x87_attr_t       *attr;
629         const arch_register_t *out = x87_get_irn_register(pred);
630
631         x87_push_dbl(state, arch_register_get_index(out), pred);
632
633         fpush = new_bd_ia32_fpush(NULL, get_nodes_block(n));
634         attr  = get_ia32_x87_attr(fpush);
635         attr->x87[0] = get_st_reg(pos);
636         attr->x87[2] = get_st_reg(0);
637
638         keep_alive(fpush);
639         sched_add_before(n, fpush);
640
641         DB((dbg, LEVEL_1, "<<< %s %s, %s\n", get_irn_opname(fpush), attr->x87[0]->name, attr->x87[2]->name));
642 }
643
644 /**
645  * Create a fpop before node n.
646  *
647  * @param state   the x87 state
648  * @param n       the node after the fpop
649  * @param num     pop 1 or 2 values
650  *
651  * @return the fpop node
652  */
653 static ir_node *x87_create_fpop(x87_state *state, ir_node *n, int num)
654 {
655         ir_node         *fpop = NULL;
656         ia32_x87_attr_t *attr;
657
658         assert(num > 0);
659         do {
660                 x87_pop(state);
661                 if (ia32_cg_config.use_ffreep)
662                         fpop = new_bd_ia32_ffreep(NULL, get_nodes_block(n));
663                 else
664                         fpop = new_bd_ia32_fpop(NULL, get_nodes_block(n));
665                 attr = get_ia32_x87_attr(fpop);
666                 attr->x87[0] = get_st_reg(0);
667                 attr->x87[1] = get_st_reg(0);
668                 attr->x87[2] = get_st_reg(0);
669
670                 keep_alive(fpop);
671                 sched_add_before(n, fpop);
672                 DB((dbg, LEVEL_1, "<<< %s %s\n", get_irn_opname(fpop), attr->x87[0]->name));
673         } while (--num > 0);
674         return fpop;
675 }
676
677 /* --------------------------------- liveness ------------------------------------------ */
678
679 /**
680  * The liveness transfer function.
681  * Updates a live set over a single step from a given node to its predecessor.
682  * Everything defined at the node is removed from the set, the uses of the node get inserted.
683  *
684  * @param irn      The node at which liveness should be computed.
685  * @param live     The bitset of registers live before @p irn. This set gets modified by updating it to
686  *                 the registers live after irn.
687  *
688  * @return The live bitset.
689  */
690 static vfp_liveness vfp_liveness_transfer(ir_node *irn, vfp_liveness live)
691 {
692         int i, n;
693         const arch_register_class_t *cls = &ia32_reg_classes[CLASS_ia32_vfp];
694
695         if (get_irn_mode(irn) == mode_T) {
696                 foreach_out_edge(irn, edge) {
697                         ir_node *proj = get_edge_src_irn(edge);
698
699                         if (arch_irn_consider_in_reg_alloc(cls, proj)) {
700                                 const arch_register_t *reg = x87_get_irn_register(proj);
701                                 live &= ~(1 << arch_register_get_index(reg));
702                         }
703                 }
704         } else if (arch_irn_consider_in_reg_alloc(cls, irn)) {
705                 const arch_register_t *reg = x87_get_irn_register(irn);
706                 live &= ~(1 << arch_register_get_index(reg));
707         }
708
709         for (i = 0, n = get_irn_arity(irn); i < n; ++i) {
710                 ir_node *op = get_irn_n(irn, i);
711
712                 if (mode_is_float(get_irn_mode(op)) &&
713                                 arch_irn_consider_in_reg_alloc(cls, op)) {
714                         const arch_register_t *reg = x87_get_irn_register(op);
715                         live |= 1 << arch_register_get_index(reg);
716                 }
717         }
718         return live;
719 }
720
721 /**
722  * Put all live virtual registers at the end of a block into a bitset.
723  *
724  * @param sim      the simulator handle
725  * @param bl       the block
726  *
727  * @return The live bitset at the end of this block
728  */
729 static vfp_liveness vfp_liveness_end_of_block(x87_simulator *sim, const ir_node *block)
730 {
731         vfp_liveness live = 0;
732         const arch_register_class_t *cls = &ia32_reg_classes[CLASS_ia32_vfp];
733         const be_lv_t *lv = sim->lv;
734
735         be_lv_foreach(lv, block, be_lv_state_end, node) {
736                 const arch_register_t *reg;
737                 if (!arch_irn_consider_in_reg_alloc(cls, node))
738                         continue;
739
740                 reg = x87_get_irn_register(node);
741                 live |= 1 << arch_register_get_index(reg);
742         }
743
744         return live;
745 }
746
747 /** get the register mask from an arch_register */
748 #define REGMASK(reg)    (1 << (arch_register_get_index(reg)))
749
750 /**
751  * Return a bitset of argument registers which are live at the end of a node.
752  *
753  * @param sim    the simulator handle
754  * @param pos    the node
755  * @param kill   kill mask for the output registers
756  *
757  * @return The live bitset.
758  */
759 static unsigned vfp_live_args_after(x87_simulator *sim, const ir_node *pos, unsigned kill)
760 {
761         unsigned idx = get_irn_idx(pos);
762
763         assert(idx < sim->n_idx);
764         return sim->live[idx] & ~kill;
765 }
766
767 /**
768  * Calculate the liveness for a whole block and cache it.
769  *
770  * @param sim   the simulator handle
771  * @param block the block
772  */
773 static void update_liveness(x87_simulator *sim, ir_node *block)
774 {
775         vfp_liveness live = vfp_liveness_end_of_block(sim, block);
776         unsigned idx;
777
778         /* now iterate through the block backward and cache the results */
779         sched_foreach_reverse(block, irn) {
780                 /* stop at the first Phi: this produces the live-in */
781                 if (is_Phi(irn))
782                         break;
783
784                 idx = get_irn_idx(irn);
785                 sim->live[idx] = live;
786
787                 live = vfp_liveness_transfer(irn, live);
788         }
789         idx = get_irn_idx(block);
790         sim->live[idx] = live;
791 }
792
793 /**
794  * Returns true if a register is live in a set.
795  *
796  * @param reg_idx  the vfp register index
797  * @param live     a live bitset
798  */
799 #define is_vfp_live(reg_idx, live) ((live) & (1 << (reg_idx)))
800
801 #ifdef DEBUG_libfirm
802 /**
803  * Dump liveness info.
804  *
805  * @param live  the live bitset
806  */
807 static void vfp_dump_live(vfp_liveness live)
808 {
809         int i;
810
811         DB((dbg, LEVEL_2, "Live after: "));
812         for (i = 0; i < 8; ++i) {
813                 if (live & (1 << i)) {
814                         DB((dbg, LEVEL_2, "vf%d ", i));
815                 }
816         }
817         DB((dbg, LEVEL_2, "\n"));
818 }
819 #endif /* DEBUG_libfirm */
820
821 /* --------------------------------- simulators ---------------------------------------- */
822
823 /**
824  * Simulate a virtual binop.
825  *
826  * @param state  the x87 state
827  * @param n      the node that should be simulated (and patched)
828  * @param tmpl   the template containing the 4 possible x87 opcodes
829  *
830  * @return NO_NODE_ADDED
831  */
832 static int sim_binop(x87_state *state, ir_node *n, const exchange_tmpl *tmpl)
833 {
834         int op2_idx = 0, op1_idx;
835         int out_idx, do_pop = 0;
836         ia32_x87_attr_t *attr;
837         int permuted;
838         ir_node *patched_insn;
839         ir_op *dst;
840         x87_simulator         *sim     = state->sim;
841         ir_node               *op1     = get_irn_n(n, n_ia32_binary_left);
842         ir_node               *op2     = get_irn_n(n, n_ia32_binary_right);
843         const arch_register_t *op1_reg = x87_get_irn_register(op1);
844         const arch_register_t *op2_reg = x87_get_irn_register(op2);
845         const arch_register_t *out     = x87_irn_get_register(n, pn_ia32_res);
846         int reg_index_1                = arch_register_get_index(op1_reg);
847         int reg_index_2                = arch_register_get_index(op2_reg);
848         vfp_liveness           live    = vfp_live_args_after(sim, n, REGMASK(out));
849         int                    op1_live_after;
850         int                    op2_live_after;
851
852         DB((dbg, LEVEL_1, ">>> %+F %s, %s -> %s\n", n,
853                 arch_register_get_name(op1_reg), arch_register_get_name(op2_reg),
854                 arch_register_get_name(out)));
855         DEBUG_ONLY(vfp_dump_live(live);)
856         DB((dbg, LEVEL_1, "Stack before: "));
857         DEBUG_ONLY(x87_dump_stack(state);)
858
859         op1_idx = x87_on_stack(state, reg_index_1);
860         assert(op1_idx >= 0);
861         op1_live_after = is_vfp_live(reg_index_1, live);
862
863         attr     = get_ia32_x87_attr(n);
864         permuted = attr->attr.data.ins_permuted;
865
866         if (reg_index_2 != REG_VFP_VFP_NOREG) {
867                 assert(!permuted);
868
869                 /* second operand is a vfp register */
870                 op2_idx = x87_on_stack(state, reg_index_2);
871                 assert(op2_idx >= 0);
872                 op2_live_after = is_vfp_live(reg_index_2, live);
873
874                 if (op2_live_after) {
875                         /* Second operand is live. */
876
877                         if (op1_live_after) {
878                                 /* Both operands are live: push the first one.
879                                    This works even for op1 == op2. */
880                                 x87_create_fpush(state, n, op1_idx, n_ia32_binary_right);
881                                 /* now do fxxx (tos=tos X op) */
882                                 op1_idx = 0;
883                                 op2_idx += 1;
884                                 out_idx = 0;
885                                 dst = tmpl->normal_op;
886                         } else {
887                                 /* Second live, first operand is dead here, bring it to tos. */
888                                 if (op1_idx != 0) {
889                                         x87_create_fxch(state, n, op1_idx);
890                                         if (op2_idx == 0)
891                                                 op2_idx = op1_idx;
892                                         op1_idx = 0;
893                                 }
894                                 /* now do fxxx (tos=tos X op) */
895                                 out_idx = 0;
896                                 dst = tmpl->normal_op;
897                         }
898                 } else {
899                         /* Second operand is dead. */
900                         if (op1_live_after) {
901                                 /* First operand is live: bring second to tos. */
902                                 if (op2_idx != 0) {
903                                         x87_create_fxch(state, n, op2_idx);
904                                         if (op1_idx == 0)
905                                                 op1_idx = op2_idx;
906                                         op2_idx = 0;
907                                 }
908                                 /* now do fxxxr (tos = op X tos) */
909                                 out_idx = 0;
910                                 dst = tmpl->reverse_op;
911                         } else {
912                                 /* Both operands are dead here, pop them from the stack. */
913                                 if (op2_idx == 0) {
914                                         if (op1_idx == 0) {
915                                                 /* Both are identically and on tos, no pop needed. */
916                                                 /* here fxxx (tos = tos X tos) */
917                                                 dst = tmpl->normal_op;
918                                                 out_idx = 0;
919                                         } else {
920                                                 /* now do fxxxp (op = op X tos, pop) */
921                                                 dst = tmpl->normal_pop_op;
922                                                 do_pop = 1;
923                                                 out_idx = op1_idx;
924                                         }
925                                 } else if (op1_idx == 0) {
926                                         assert(op1_idx != op2_idx);
927                                         /* now do fxxxrp (op = tos X op, pop) */
928                                         dst = tmpl->reverse_pop_op;
929                                         do_pop = 1;
930                                         out_idx = op2_idx;
931                                 } else {
932                                         /* Bring the second on top. */
933                                         x87_create_fxch(state, n, op2_idx);
934                                         if (op1_idx == op2_idx) {
935                                                 /* Both are identically and on tos now, no pop needed. */
936                                                 op1_idx = 0;
937                                                 op2_idx = 0;
938                                                 /* use fxxx (tos = tos X tos) */
939                                                 dst = tmpl->normal_op;
940                                                 out_idx = 0;
941                                         } else {
942                                                 /* op2 is on tos now */
943                                                 op2_idx = 0;
944                                                 /* use fxxxp (op = op X tos, pop) */
945                                                 dst = tmpl->normal_pop_op;
946                                                 out_idx = op1_idx;
947                                                 do_pop = 1;
948                                         }
949                                 }
950                         }
951                 }
952         } else {
953                 /* second operand is an address mode */
954                 if (op1_live_after) {
955                         /* first operand is live: push it here */
956                         x87_create_fpush(state, n, op1_idx, n_ia32_binary_left);
957                         op1_idx = 0;
958                 } else {
959                         /* first operand is dead: bring it to tos */
960                         if (op1_idx != 0) {
961                                 x87_create_fxch(state, n, op1_idx);
962                                 op1_idx = 0;
963                         }
964                 }
965
966                 /* use fxxx (tos = tos X mem) */
967                 dst = permuted ? tmpl->reverse_op : tmpl->normal_op;
968                 out_idx = 0;
969         }
970
971         patched_insn = x87_patch_insn(n, dst);
972         x87_set_st(state, arch_register_get_index(out), patched_insn, out_idx);
973         if (do_pop) {
974                 x87_pop(state);
975         }
976
977         /* patch the operation */
978         attr->x87[0] = op1_reg = get_st_reg(op1_idx);
979         if (reg_index_2 != REG_VFP_VFP_NOREG) {
980                 attr->x87[1] = op2_reg = get_st_reg(op2_idx);
981         }
982         attr->x87[2] = out = get_st_reg(out_idx);
983
984         if (reg_index_2 != REG_VFP_VFP_NOREG) {
985                 DB((dbg, LEVEL_1, "<<< %s %s, %s -> %s\n", get_irn_opname(n),
986                         arch_register_get_name(op1_reg), arch_register_get_name(op2_reg),
987                         arch_register_get_name(out)));
988         } else {
989                 DB((dbg, LEVEL_1, "<<< %s %s, [AM] -> %s\n", get_irn_opname(n),
990                         arch_register_get_name(op1_reg),
991                         arch_register_get_name(out)));
992         }
993
994         return NO_NODE_ADDED;
995 }
996
997 /**
998  * Simulate a virtual Unop.
999  *
1000  * @param state  the x87 state
1001  * @param n      the node that should be simulated (and patched)
1002  * @param op     the x87 opcode that will replace n's opcode
1003  *
1004  * @return NO_NODE_ADDED
1005  */
1006 static int sim_unop(x87_state *state, ir_node *n, ir_op *op)
1007 {
1008         x87_simulator         *sim = state->sim;
1009         const arch_register_t *op1 = x87_get_irn_register(get_irn_n(n, 0));
1010         const arch_register_t *out = x87_get_irn_register(n);
1011         ia32_x87_attr_t *attr;
1012         unsigned live = vfp_live_args_after(sim, n, REGMASK(out));
1013
1014         DB((dbg, LEVEL_1, ">>> %+F -> %s\n", n, out->name));
1015         DEBUG_ONLY(vfp_dump_live(live);)
1016
1017         int op1_idx = x87_on_stack(state, arch_register_get_index(op1));
1018
1019         if (is_vfp_live(arch_register_get_index(op1), live)) {
1020                 /* push the operand here */
1021                 x87_create_fpush(state, n, op1_idx, 0);
1022                 op1_idx = 0;
1023         } else {
1024                 /* operand is dead, bring it to tos */
1025                 if (op1_idx != 0) {
1026                         x87_create_fxch(state, n, op1_idx);
1027                 }
1028         }
1029
1030         x87_set_tos(state, arch_register_get_index(out), x87_patch_insn(n, op));
1031         attr = get_ia32_x87_attr(n);
1032         attr->x87[0] = op1 = get_st_reg(0);
1033         attr->x87[2] = out = get_st_reg(0);
1034         DB((dbg, LEVEL_1, "<<< %s -> %s\n", get_irn_opname(n), out->name));
1035
1036         return NO_NODE_ADDED;
1037 }
1038
1039 /**
1040  * Simulate a virtual Load instruction.
1041  *
1042  * @param state  the x87 state
1043  * @param n      the node that should be simulated (and patched)
1044  * @param op     the x87 opcode that will replace n's opcode
1045  *
1046  * @return NO_NODE_ADDED
1047  */
1048 static int sim_load(x87_state *state, ir_node *n, ir_op *op, int res_pos)
1049 {
1050         const arch_register_t *out = x87_irn_get_register(n, res_pos);
1051         ia32_x87_attr_t *attr;
1052
1053         DB((dbg, LEVEL_1, ">>> %+F -> %s\n", n, arch_register_get_name(out)));
1054         x87_push(state, arch_register_get_index(out), x87_patch_insn(n, op));
1055         assert(out == x87_irn_get_register(n, res_pos));
1056         attr = get_ia32_x87_attr(n);
1057         attr->x87[2] = out = get_st_reg(0);
1058         DB((dbg, LEVEL_1, "<<< %s -> %s\n", get_irn_opname(n), arch_register_get_name(out)));
1059
1060         return NO_NODE_ADDED;
1061 }
1062
1063 /**
1064  * Rewire all users of @p old_val to @new_val iff they are scheduled after @p store.
1065  *
1066  * @param store   The store
1067  * @param old_val The former value
1068  * @param new_val The new value
1069  */
1070 static void collect_and_rewire_users(ir_node *store, ir_node *old_val, ir_node *new_val)
1071 {
1072         foreach_out_edge_safe(old_val, edge) {
1073                 ir_node *user = get_edge_src_irn(edge);
1074
1075                 if (! user || user == store)
1076                         continue;
1077
1078                 /* if the user is scheduled after the store: rewire */
1079                 if (sched_is_scheduled(user) && sched_comes_after(store, user)) {
1080                         int i;
1081                         /* find the input of the user pointing to the old value */
1082                         for (i = get_irn_arity(user) - 1; i >= 0; i--) {
1083                                 if (get_irn_n(user, i) == old_val)
1084                                         set_irn_n(user, i, new_val);
1085                         }
1086                 }
1087         }
1088 }
1089
1090 /**
1091  * Simulate a virtual Store.
1092  *
1093  * @param state  the x87 state
1094  * @param n      the node that should be simulated (and patched)
1095  * @param op     the x87 store opcode
1096  * @param op_p   the x87 store and pop opcode
1097  */
1098 static int sim_store(x87_state *state, ir_node *n, ir_op *op, ir_op *op_p)
1099 {
1100         ir_node               *val = get_irn_n(n, n_ia32_vfst_val);
1101         const arch_register_t *op2 = x87_get_irn_register(val);
1102         unsigned              live = vfp_live_args_after(state->sim, n, 0);
1103         int                   insn = NO_NODE_ADDED;
1104         ia32_x87_attr_t *attr;
1105         int op2_reg_idx, op2_idx, depth;
1106         int live_after_node;
1107         ir_mode *mode;
1108
1109         op2_reg_idx = arch_register_get_index(op2);
1110         op2_idx = x87_on_stack(state, op2_reg_idx);
1111         live_after_node = is_vfp_live(arch_register_get_index(op2), live);
1112         DB((dbg, LEVEL_1, ">>> %+F %s ->\n", n, arch_register_get_name(op2)));
1113         assert(op2_idx >= 0);
1114
1115         mode  = get_ia32_ls_mode(n);
1116         depth = x87_get_depth(state);
1117
1118         if (live_after_node) {
1119                 /*
1120                         Problem: fst doesn't support 96bit modes (spills), only fstp does
1121                                  fist doesn't support 64bit mode, only fistp
1122                         Solution:
1123                                 - stack not full: push value and fstp
1124                                 - stack full: fstp value and load again
1125                         Note that we cannot test on mode_E, because floats might be 96bit ...
1126                 */
1127                 if (get_mode_size_bits(mode) > 64 || (mode_is_int(mode) && get_mode_size_bits(mode) > 32)) {
1128                         if (depth < N_ia32_st_REGS) {
1129                                 /* ok, we have a free register: push + fstp */
1130                                 x87_create_fpush(state, n, op2_idx, n_ia32_vfst_val);
1131                                 x87_pop(state);
1132                                 x87_patch_insn(n, op_p);
1133                         } else {
1134                                 ir_node  *vfld, *mem, *block, *rproj, *mproj;
1135                                 ir_graph *irg   = get_irn_irg(n);
1136                                 ir_node  *nomem = get_irg_no_mem(irg);
1137
1138                                 /* stack full here: need fstp + load */
1139                                 x87_pop(state);
1140                                 x87_patch_insn(n, op_p);
1141
1142                                 block = get_nodes_block(n);
1143                                 vfld  = new_bd_ia32_vfld(NULL, block, get_irn_n(n, 0), get_irn_n(n, 1), nomem, get_ia32_ls_mode(n));
1144
1145                                 /* copy all attributes */
1146                                 set_ia32_frame_ent(vfld, get_ia32_frame_ent(n));
1147                                 if (is_ia32_use_frame(n))
1148                                         set_ia32_use_frame(vfld);
1149                                 set_ia32_op_type(vfld, ia32_AddrModeS);
1150                                 add_ia32_am_offs_int(vfld, get_ia32_am_offs_int(n));
1151                                 set_ia32_am_sc(vfld, get_ia32_am_sc(n));
1152                                 set_ia32_ls_mode(vfld, get_ia32_ls_mode(n));
1153
1154                                 rproj = new_r_Proj(vfld, get_ia32_ls_mode(vfld), pn_ia32_vfld_res);
1155                                 mproj = new_r_Proj(vfld, mode_M, pn_ia32_vfld_M);
1156                                 mem   = get_irn_Proj_for_mode(n, mode_M);
1157
1158                                 assert(mem && "Store memory not found");
1159
1160                                 arch_set_irn_register(rproj, op2);
1161
1162                                 /* reroute all former users of the store memory to the load memory */
1163                                 edges_reroute(mem, mproj);
1164                                 /* set the memory input of the load to the store memory */
1165                                 set_irn_n(vfld, n_ia32_vfld_mem, mem);
1166
1167                                 sched_add_after(n, vfld);
1168                                 sched_add_after(vfld, rproj);
1169
1170                                 /* rewire all users, scheduled after the store, to the loaded value */
1171                                 collect_and_rewire_users(n, val, rproj);
1172
1173                                 insn = NODE_ADDED;
1174                         }
1175                 } else {
1176                         /* we can only store the tos to memory */
1177                         if (op2_idx != 0)
1178                                 x87_create_fxch(state, n, op2_idx);
1179
1180                         /* mode size 64 or smaller -> use normal fst */
1181                         x87_patch_insn(n, op);
1182                 }
1183         } else {
1184                 /* we can only store the tos to memory */
1185                 if (op2_idx != 0)
1186                         x87_create_fxch(state, n, op2_idx);
1187
1188                 x87_pop(state);
1189                 x87_patch_insn(n, op_p);
1190         }
1191
1192         attr = get_ia32_x87_attr(n);
1193         attr->x87[1] = op2 = get_st_reg(0);
1194         DB((dbg, LEVEL_1, "<<< %s %s ->\n", get_irn_opname(n), arch_register_get_name(op2)));
1195
1196         return insn;
1197 }
1198
1199 #define _GEN_BINOP(op, rev) \
1200 static int sim_##op(x87_state *state, ir_node *n) { \
1201         exchange_tmpl tmpl = { op_ia32_##op, op_ia32_##rev, op_ia32_##op##p, op_ia32_##rev##p }; \
1202         return sim_binop(state, n, &tmpl); \
1203 }
1204
1205 #define GEN_BINOP(op)   _GEN_BINOP(op, op)
1206 #define GEN_BINOPR(op)  _GEN_BINOP(op, op##r)
1207
1208 #define GEN_LOAD(op)                                              \
1209 static int sim_##op(x87_state *state, ir_node *n) {               \
1210         return sim_load(state, n, op_ia32_##op, pn_ia32_v##op##_res); \
1211 }
1212
1213 #define GEN_UNOP(op) \
1214 static int sim_##op(x87_state *state, ir_node *n) { \
1215         return sim_unop(state, n, op_ia32_##op); \
1216 }
1217
1218 #define GEN_STORE(op) \
1219 static int sim_##op(x87_state *state, ir_node *n) { \
1220         return sim_store(state, n, op_ia32_##op, op_ia32_##op##p); \
1221 }
1222
1223 /* all stubs */
1224 GEN_BINOP(fadd)
1225 GEN_BINOPR(fsub)
1226 GEN_BINOP(fmul)
1227 GEN_BINOPR(fdiv)
1228 GEN_BINOP(fprem)
1229
1230 GEN_UNOP(fabs)
1231 GEN_UNOP(fchs)
1232
1233 GEN_LOAD(fld)
1234 GEN_LOAD(fild)
1235 GEN_LOAD(fldz)
1236 GEN_LOAD(fld1)
1237
1238 GEN_STORE(fst)
1239 GEN_STORE(fist)
1240
1241 /**
1242  * Simulate a virtual fisttp.
1243  *
1244  * @param state  the x87 state
1245  * @param n      the node that should be simulated (and patched)
1246  *
1247  * @return NO_NODE_ADDED
1248  */
1249 static int sim_fisttp(x87_state *state, ir_node *n)
1250 {
1251         ir_node               *val = get_irn_n(n, n_ia32_vfst_val);
1252         const arch_register_t *op2 = x87_get_irn_register(val);
1253         ia32_x87_attr_t *attr;
1254         int op2_reg_idx, op2_idx;
1255
1256         op2_reg_idx = arch_register_get_index(op2);
1257         op2_idx     = x87_on_stack(state, op2_reg_idx);
1258         DB((dbg, LEVEL_1, ">>> %+F %s ->\n", n, arch_register_get_name(op2)));
1259         assert(op2_idx >= 0);
1260
1261         /* Note: although the value is still live here, it is destroyed because
1262            of the pop. The register allocator is aware of that and introduced a copy
1263            if the value must be alive. */
1264
1265         /* we can only store the tos to memory */
1266         if (op2_idx != 0)
1267                 x87_create_fxch(state, n, op2_idx);
1268
1269         x87_pop(state);
1270         x87_patch_insn(n, op_ia32_fisttp);
1271
1272         attr = get_ia32_x87_attr(n);
1273         attr->x87[1] = op2 = get_st_reg(0);
1274         DB((dbg, LEVEL_1, "<<< %s %s ->\n", get_irn_opname(n), arch_register_get_name(op2)));
1275
1276         return NO_NODE_ADDED;
1277 }
1278
1279 /**
1280  * Simulate a virtual FtstFnstsw.
1281  *
1282  * @param state  the x87 state
1283  * @param n      the node that should be simulated (and patched)
1284  *
1285  * @return NO_NODE_ADDED
1286  */
1287 static int sim_FtstFnstsw(x87_state *state, ir_node *n)
1288 {
1289         x87_simulator         *sim         = state->sim;
1290         ia32_x87_attr_t       *attr        = get_ia32_x87_attr(n);
1291         ir_node               *op1_node    = get_irn_n(n, n_ia32_vFtstFnstsw_left);
1292         const arch_register_t *reg1        = x87_get_irn_register(op1_node);
1293         int                    reg_index_1 = arch_register_get_index(reg1);
1294         int                    op1_idx     = x87_on_stack(state, reg_index_1);
1295         unsigned               live        = vfp_live_args_after(sim, n, 0);
1296
1297         DB((dbg, LEVEL_1, ">>> %+F %s\n", n, arch_register_get_name(reg1)));
1298         DEBUG_ONLY(vfp_dump_live(live);)
1299         DB((dbg, LEVEL_1, "Stack before: "));
1300         DEBUG_ONLY(x87_dump_stack(state);)
1301         assert(op1_idx >= 0);
1302
1303         if (op1_idx != 0) {
1304                 /* bring the value to tos */
1305                 x87_create_fxch(state, n, op1_idx);
1306                 op1_idx = 0;
1307         }
1308
1309         /* patch the operation */
1310         x87_patch_insn(n, op_ia32_FtstFnstsw);
1311         reg1 = get_st_reg(op1_idx);
1312         attr->x87[0] = reg1;
1313         attr->x87[1] = NULL;
1314         attr->x87[2] = NULL;
1315
1316         if (!is_vfp_live(reg_index_1, live))
1317                 x87_create_fpop(state, sched_next(n), 1);
1318
1319         return NO_NODE_ADDED;
1320 }
1321
1322 /**
1323  * Simulate a Fucom
1324  *
1325  * @param state  the x87 state
1326  * @param n      the node that should be simulated (and patched)
1327  *
1328  * @return NO_NODE_ADDED
1329  */
1330 static int sim_Fucom(x87_state *state, ir_node *n)
1331 {
1332         int op1_idx;
1333         int op2_idx = -1;
1334         ia32_x87_attr_t *attr = get_ia32_x87_attr(n);
1335         ir_op *dst;
1336         x87_simulator         *sim        = state->sim;
1337         ir_node               *op1_node   = get_irn_n(n, n_ia32_vFucomFnstsw_left);
1338         ir_node               *op2_node   = get_irn_n(n, n_ia32_vFucomFnstsw_right);
1339         const arch_register_t *op1        = x87_get_irn_register(op1_node);
1340         const arch_register_t *op2        = x87_get_irn_register(op2_node);
1341         int reg_index_1 = arch_register_get_index(op1);
1342         int                    reg_index_2 = arch_register_get_index(op2);
1343         unsigned               live       = vfp_live_args_after(sim, n, 0);
1344         bool                   permuted   = attr->attr.data.ins_permuted;
1345         bool                   xchg       = false;
1346         int                    pops       = 0;
1347
1348         DB((dbg, LEVEL_1, ">>> %+F %s, %s\n", n,
1349                 arch_register_get_name(op1), arch_register_get_name(op2)));
1350         DEBUG_ONLY(vfp_dump_live(live);)
1351         DB((dbg, LEVEL_1, "Stack before: "));
1352         DEBUG_ONLY(x87_dump_stack(state);)
1353
1354         op1_idx = x87_on_stack(state, reg_index_1);
1355         assert(op1_idx >= 0);
1356
1357         /* BEWARE: check for comp a,a cases, they might happen */
1358         if (reg_index_2 != REG_VFP_VFP_NOREG) {
1359                 /* second operand is a vfp register */
1360                 op2_idx = x87_on_stack(state, reg_index_2);
1361                 assert(op2_idx >= 0);
1362
1363                 if (is_vfp_live(reg_index_2, live)) {
1364                         /* second operand is live */
1365
1366                         if (is_vfp_live(reg_index_1, live)) {
1367                                 /* both operands are live */
1368
1369                                 if (op1_idx == 0) {
1370                                         /* res = tos X op */
1371                                 } else if (op2_idx == 0) {
1372                                         /* res = op X tos */
1373                                         permuted = !permuted;
1374                                         xchg     = true;
1375                                 } else {
1376                                         /* bring the first one to tos */
1377                                         x87_create_fxch(state, n, op1_idx);
1378                                         if (op1_idx == op2_idx) {
1379                                                 op2_idx = 0;
1380                                         } else if (op2_idx == 0) {
1381                                                 op2_idx = op1_idx;
1382                                         }
1383                                         op1_idx = 0;
1384                                         /* res = tos X op */
1385                                 }
1386                         } else {
1387                                 /* second live, first operand is dead here, bring it to tos.
1388                                    This means further, op1_idx != op2_idx. */
1389                                 assert(op1_idx != op2_idx);
1390                                 if (op1_idx != 0) {
1391                                         x87_create_fxch(state, n, op1_idx);
1392                                         if (op2_idx == 0)
1393                                                 op2_idx = op1_idx;
1394                                         op1_idx = 0;
1395                                 }
1396                                 /* res = tos X op, pop */
1397                                 pops = 1;
1398                         }
1399                 } else {
1400                         /* second operand is dead */
1401                         if (is_vfp_live(reg_index_1, live)) {
1402                                 /* first operand is live: bring second to tos.
1403                                    This means further, op1_idx != op2_idx. */
1404                                 assert(op1_idx != op2_idx);
1405                                 if (op2_idx != 0) {
1406                                         x87_create_fxch(state, n, op2_idx);
1407                                         if (op1_idx == 0)
1408                                                 op1_idx = op2_idx;
1409                                         op2_idx = 0;
1410                                 }
1411                                 /* res = op X tos, pop */
1412                                 pops     = 1;
1413                                 permuted = !permuted;
1414                                 xchg     = true;
1415                         } else {
1416                                 /* both operands are dead here, check first for identity. */
1417                                 if (op1_idx == op2_idx) {
1418                                         /* identically, one pop needed */
1419                                         if (op1_idx != 0) {
1420                                                 x87_create_fxch(state, n, op1_idx);
1421                                                 op1_idx = 0;
1422                                                 op2_idx = 0;
1423                                         }
1424                                         /* res = tos X op, pop */
1425                                         pops    = 1;
1426                                 }
1427                                 /* different, move them to st and st(1) and pop both.
1428                                    The tricky part is to get one into st(1).*/
1429                                 else if (op2_idx == 1) {
1430                                         /* good, second operand is already in the right place, move the first */
1431                                         if (op1_idx != 0) {
1432                                                 /* bring the first on top */
1433                                                 x87_create_fxch(state, n, op1_idx);
1434                                                 assert(op2_idx != 0);
1435                                                 op1_idx = 0;
1436                                         }
1437                                         /* res = tos X op, pop, pop */
1438                                         pops = 2;
1439                                 } else if (op1_idx == 1) {
1440                                         /* good, first operand is already in the right place, move the second */
1441                                         if (op2_idx != 0) {
1442                                                 /* bring the first on top */
1443                                                 x87_create_fxch(state, n, op2_idx);
1444                                                 assert(op1_idx != 0);
1445                                                 op2_idx = 0;
1446                                         }
1447                                         /* res = op X tos, pop, pop */
1448                                         permuted = !permuted;
1449                                         xchg     = true;
1450                                         pops     = 2;
1451                                 } else {
1452                                         /* if one is already the TOS, we need two fxch */
1453                                         if (op1_idx == 0) {
1454                                                 /* first one is TOS, move to st(1) */
1455                                                 x87_create_fxch(state, n, 1);
1456                                                 assert(op2_idx != 1);
1457                                                 op1_idx = 1;
1458                                                 x87_create_fxch(state, n, op2_idx);
1459                                                 op2_idx = 0;
1460                                                 /* res = op X tos, pop, pop */
1461                                                 pops     = 2;
1462                                                 permuted = !permuted;
1463                                                 xchg     = true;
1464                                         } else if (op2_idx == 0) {
1465                                                 /* second one is TOS, move to st(1) */
1466                                                 x87_create_fxch(state, n, 1);
1467                                                 assert(op1_idx != 1);
1468                                                 op2_idx = 1;
1469                                                 x87_create_fxch(state, n, op1_idx);
1470                                                 op1_idx = 0;
1471                                                 /* res = tos X op, pop, pop */
1472                                                 pops    = 2;
1473                                         } else {
1474                                                 /* none of them is either TOS or st(1), 3 fxch needed */
1475                                                 x87_create_fxch(state, n, op2_idx);
1476                                                 assert(op1_idx != 0);
1477                                                 x87_create_fxch(state, n, 1);
1478                                                 op2_idx = 1;
1479                                                 x87_create_fxch(state, n, op1_idx);
1480                                                 op1_idx = 0;
1481                                                 /* res = tos X op, pop, pop */
1482                                                 pops    = 2;
1483                                         }
1484                                 }
1485                         }
1486                 }
1487         } else {
1488                 /* second operand is an address mode */
1489                 if (is_vfp_live(reg_index_1, live)) {
1490                         /* first operand is live: bring it to TOS */
1491                         if (op1_idx != 0) {
1492                                 x87_create_fxch(state, n, op1_idx);
1493                                 op1_idx = 0;
1494                         }
1495                 } else {
1496                         /* first operand is dead: bring it to tos */
1497                         if (op1_idx != 0) {
1498                                 x87_create_fxch(state, n, op1_idx);
1499                                 op1_idx = 0;
1500                         }
1501                         pops = 1;
1502                 }
1503         }
1504
1505         /* patch the operation */
1506         if (is_ia32_vFucomFnstsw(n)) {
1507                 int i;
1508
1509                 switch (pops) {
1510                 case 0: dst = op_ia32_FucomFnstsw;   break;
1511                 case 1: dst = op_ia32_FucompFnstsw;  break;
1512                 case 2: dst = op_ia32_FucomppFnstsw; break;
1513                 default: panic("invalid popcount");
1514                 }
1515
1516                 for (i = 0; i < pops; ++i) {
1517                         x87_pop(state);
1518                 }
1519         } else if (is_ia32_vFucomi(n)) {
1520                 switch (pops) {
1521                 case 0: dst = op_ia32_Fucomi;                  break;
1522                 case 1: dst = op_ia32_Fucompi; x87_pop(state); break;
1523                 case 2:
1524                         dst = op_ia32_Fucompi;
1525                         x87_pop(state);
1526                         x87_create_fpop(state, sched_next(n), 1);
1527                         break;
1528                 default: panic("invalid popcount");
1529                 }
1530         } else {
1531                 panic("invalid operation %+F", n);
1532         }
1533
1534         x87_patch_insn(n, dst);
1535         if (xchg) {
1536                 int tmp = op1_idx;
1537                 op1_idx = op2_idx;
1538                 op2_idx = tmp;
1539         }
1540
1541         op1 = get_st_reg(op1_idx);
1542         attr->x87[0] = op1;
1543         if (op2_idx >= 0) {
1544                 op2 = get_st_reg(op2_idx);
1545                 attr->x87[1] = op2;
1546         }
1547         attr->x87[2] = NULL;
1548         attr->attr.data.ins_permuted = permuted;
1549
1550         if (op2_idx >= 0) {
1551                 DB((dbg, LEVEL_1, "<<< %s %s, %s\n", get_irn_opname(n),
1552                         arch_register_get_name(op1), arch_register_get_name(op2)));
1553         } else {
1554                 DB((dbg, LEVEL_1, "<<< %s %s, [AM]\n", get_irn_opname(n),
1555                         arch_register_get_name(op1)));
1556         }
1557
1558         return NO_NODE_ADDED;
1559 }
1560
1561 /**
1562  * Simulate a Keep.
1563  *
1564  * @param state  the x87 state
1565  * @param n      the node that should be simulated (and patched)
1566  *
1567  * @return NO_NODE_ADDED
1568  */
1569 static int sim_Keep(x87_state *state, ir_node *node)
1570 {
1571         const ir_node         *op;
1572         const arch_register_t *op_reg;
1573         int                    reg_id;
1574         int                    op_stack_idx;
1575         unsigned               live;
1576         int                    i, arity;
1577
1578         DB((dbg, LEVEL_1, ">>> %+F\n", node));
1579
1580         arity = get_irn_arity(node);
1581         for (i = 0; i < arity; ++i) {
1582                 op      = get_irn_n(node, i);
1583                 op_reg  = arch_get_irn_register(op);
1584                 if (arch_register_get_class(op_reg) != &ia32_reg_classes[CLASS_ia32_vfp])
1585                         continue;
1586
1587                 reg_id = arch_register_get_index(op_reg);
1588                 live   = vfp_live_args_after(state->sim, node, 0);
1589
1590                 op_stack_idx = x87_on_stack(state, reg_id);
1591                 if (op_stack_idx >= 0 && !is_vfp_live(reg_id, live))
1592                         x87_create_fpop(state, sched_next(node), 1);
1593         }
1594
1595         DB((dbg, LEVEL_1, "Stack after: "));
1596         DEBUG_ONLY(x87_dump_stack(state);)
1597
1598         return NO_NODE_ADDED;
1599 }
1600
1601 /**
1602  * Keep the given node alive by adding a be_Keep.
1603  *
1604  * @param node  the node to kept alive
1605  */
1606 static void keep_float_node_alive(ir_node *node)
1607 {
1608         ir_node *block = get_nodes_block(node);
1609         ir_node *keep  = be_new_Keep(block, 1, &node);
1610
1611         assert(sched_is_scheduled(node));
1612         sched_add_after(node, keep);
1613 }
1614
1615 /**
1616  * Create a copy of a node. Recreate the node if it's a constant.
1617  *
1618  * @param state  the x87 state
1619  * @param n      the node to be copied
1620  *
1621  * @return the copy of n
1622  */
1623 static ir_node *create_Copy(x87_state *state, ir_node *n)
1624 {
1625         dbg_info *n_dbg = get_irn_dbg_info(n);
1626         ir_mode *mode = get_irn_mode(n);
1627         ir_node *block = get_nodes_block(n);
1628         ir_node *pred = get_irn_n(n, 0);
1629         ir_node *(*cnstr)(dbg_info *, ir_node *, ir_mode *) = NULL;
1630         ir_node *res;
1631         const arch_register_t *out;
1632         const arch_register_t *op1;
1633         ia32_x87_attr_t *attr;
1634
1635         /* Do not copy constants, recreate them. */
1636         switch (get_ia32_irn_opcode(pred)) {
1637         case iro_ia32_fldz:
1638                 cnstr = new_bd_ia32_fldz;
1639                 break;
1640         case iro_ia32_fld1:
1641                 cnstr = new_bd_ia32_fld1;
1642                 break;
1643         case iro_ia32_fldpi:
1644                 cnstr = new_bd_ia32_fldpi;
1645                 break;
1646         case iro_ia32_fldl2e:
1647                 cnstr = new_bd_ia32_fldl2e;
1648                 break;
1649         case iro_ia32_fldl2t:
1650                 cnstr = new_bd_ia32_fldl2t;
1651                 break;
1652         case iro_ia32_fldlg2:
1653                 cnstr = new_bd_ia32_fldlg2;
1654                 break;
1655         case iro_ia32_fldln2:
1656                 cnstr = new_bd_ia32_fldln2;
1657                 break;
1658         default:
1659                 break;
1660         }
1661
1662         out = x87_get_irn_register(n);
1663         op1 = x87_get_irn_register(pred);
1664
1665         if (cnstr != NULL) {
1666                 /* copy a constant */
1667                 res = (*cnstr)(n_dbg, block, mode);
1668
1669                 x87_push(state, arch_register_get_index(out), res);
1670
1671                 attr = get_ia32_x87_attr(res);
1672                 attr->x87[2] = get_st_reg(0);
1673         } else {
1674                 int op1_idx = x87_on_stack(state, arch_register_get_index(op1));
1675
1676                 res = new_bd_ia32_fpushCopy(n_dbg, block, pred, mode);
1677
1678                 x87_push(state, arch_register_get_index(out), res);
1679
1680                 attr = get_ia32_x87_attr(res);
1681                 attr->x87[0] = get_st_reg(op1_idx);
1682                 attr->x87[2] = get_st_reg(0);
1683         }
1684         arch_set_irn_register(res, out);
1685
1686         return res;
1687 }
1688
1689 /**
1690  * Simulate a be_Copy.
1691  *
1692  * @param state  the x87 state
1693  * @param n      the node that should be simulated (and patched)
1694  *
1695  * @return NO_NODE_ADDED
1696  */
1697 static int sim_Copy(x87_state *state, ir_node *n)
1698 {
1699         ir_node                     *pred;
1700         const arch_register_t       *out;
1701         const arch_register_t       *op1;
1702         const arch_register_class_t *cls;
1703         ir_node                     *node, *next;
1704         int                         op1_idx, out_idx;
1705         unsigned                    live;
1706
1707         cls = arch_get_irn_reg_class(n);
1708         if (cls != &ia32_reg_classes[CLASS_ia32_vfp])
1709                 return 0;
1710
1711         pred = be_get_Copy_op(n);
1712         out  = x87_get_irn_register(n);
1713         op1  = x87_get_irn_register(pred);
1714         live = vfp_live_args_after(state->sim, n, REGMASK(out));
1715
1716         DB((dbg, LEVEL_1, ">>> %+F %s -> %s\n", n,
1717                 arch_register_get_name(op1), arch_register_get_name(out)));
1718         DEBUG_ONLY(vfp_dump_live(live);)
1719
1720         op1_idx = x87_on_stack(state, arch_register_get_index(op1));
1721
1722         if (is_vfp_live(arch_register_get_index(op1), live)) {
1723                 /* Operand is still live, a real copy. We need here an fpush that can
1724                    hold a a register, so use the fpushCopy or recreate constants */
1725                 node = create_Copy(state, n);
1726
1727                 /* We have to make sure the old value doesn't go dead (which can happen
1728                  * when we recreate constants). As the simulator expected that value in
1729                  * the pred blocks. This is unfortunate as removing it would save us 1
1730                  * instruction, but we would have to rerun all the simulation to get
1731                  * this correct...
1732                  */
1733                 next = sched_next(n);
1734                 sched_remove(n);
1735                 exchange(n, node);
1736                 sched_add_before(next, node);
1737
1738                 if (get_irn_n_edges(pred) == 0) {
1739                         keep_float_node_alive(pred);
1740                 }
1741
1742                 DB((dbg, LEVEL_1, "<<< %+F %s -> ?\n", node, op1->name));
1743         } else {
1744                 out_idx = x87_on_stack(state, arch_register_get_index(out));
1745
1746                 if (out_idx >= 0 && out_idx != op1_idx) {
1747                         /* Matze: out already on stack? how can this happen? */
1748                         panic("invalid stack state");
1749
1750 #if 0
1751                         /* op1 must be killed and placed where out is */
1752                         if (out_idx == 0) {
1753                                 ia32_x87_attr_t *attr;
1754                                 /* best case, simple remove and rename */
1755                                 x87_patch_insn(n, op_ia32_Pop);
1756                                 attr = get_ia32_x87_attr(n);
1757                                 attr->x87[0] = op1 = get_st_reg(0);
1758
1759                                 x87_pop(state);
1760                                 x87_set_st(state, arch_register_get_index(out), n, op1_idx - 1);
1761                         } else {
1762                                 ia32_x87_attr_t *attr;
1763                                 /* move op1 to tos, store and pop it */
1764                                 if (op1_idx != 0) {
1765                                         x87_create_fxch(state, n, op1_idx);
1766                                         op1_idx = 0;
1767                                 }
1768                                 x87_patch_insn(n, op_ia32_Pop);
1769                                 attr = get_ia32_x87_attr(n);
1770                                 attr->x87[0] = op1 = get_st_reg(out_idx);
1771
1772                                 x87_pop(state);
1773                                 x87_set_st(state, arch_register_get_index(out), n, out_idx - 1);
1774                         }
1775                         DB((dbg, LEVEL_1, "<<< %+F %s\n", n, op1->name));
1776 #endif
1777                 } else {
1778                         /* just a virtual copy */
1779                         x87_set_st(state, arch_register_get_index(out), pred, op1_idx);
1780                         /* don't remove the node to keep the verifier quiet :),
1781                            the emitter won't emit any code for the node */
1782 #if 0
1783                         sched_remove(n);
1784                         DB((dbg, LEVEL_1, "<<< KILLED %s\n", get_irn_opname(n)));
1785                         exchange(n, pred);
1786 #endif
1787                 }
1788         }
1789         return NO_NODE_ADDED;
1790 }
1791
1792 /**
1793  * Returns the vf0 result Proj of a Call.
1794  *
1795  * @para call  the Call node
1796  */
1797 static ir_node *get_call_result_proj(ir_node *call)
1798 {
1799         /* search the result proj */
1800         foreach_out_edge(call, edge) {
1801                 ir_node *proj = get_edge_src_irn(edge);
1802                 long pn = get_Proj_proj(proj);
1803
1804                 if (pn == pn_ia32_Call_vf0)
1805                         return proj;
1806         }
1807
1808         panic("result Proj missing");
1809 }
1810
1811 static int sim_Asm(x87_state *const state, ir_node *const n)
1812 {
1813         (void)state;
1814
1815         for (size_t i = get_irn_arity(n); i-- != 0;) {
1816                 arch_register_req_t const *const req = arch_get_irn_register_req_in(n, i);
1817                 if (req->cls == &ia32_reg_classes[CLASS_ia32_vfp])
1818                         panic("cannot handle %+F with x87 constraints", n);
1819         }
1820
1821         for (size_t i = arch_get_irn_n_outs(n); i-- != 0;) {
1822                 arch_register_req_t const *const req = arch_get_irn_register_req_out(n, i);
1823                 if (req->cls == &ia32_reg_classes[CLASS_ia32_vfp])
1824                         panic("cannot handle %+F with x87 constraints", n);
1825         }
1826
1827         return NO_NODE_ADDED;
1828 }
1829
1830 /**
1831  * Simulate a ia32_Call.
1832  *
1833  * @param state      the x87 state
1834  * @param n          the node that should be simulated (and patched)
1835  *
1836  * @return NO_NODE_ADDED
1837  */
1838 static int sim_Call(x87_state *state, ir_node *n)
1839 {
1840         ir_type *call_tp = get_ia32_call_attr_const(n)->call_tp;
1841         ir_type *res_type;
1842         ir_mode *mode;
1843         ir_node *resproj;
1844         const arch_register_t *reg;
1845
1846         DB((dbg, LEVEL_1, ">>> %+F\n", n));
1847
1848         /* at the begin of a call the x87 state should be empty */
1849         assert(state->depth == 0 && "stack not empty before call");
1850
1851         if (get_method_n_ress(call_tp) <= 0)
1852                 goto end_call;
1853
1854         /*
1855          * If the called function returns a float, it is returned in st(0).
1856          * This even happens if the return value is NOT used.
1857          * Moreover, only one return result is supported.
1858          */
1859         res_type = get_method_res_type(call_tp, 0);
1860         mode     = get_type_mode(res_type);
1861
1862         if (mode == NULL || !mode_is_float(mode))
1863                 goto end_call;
1864
1865         resproj = get_call_result_proj(n);
1866
1867         reg = x87_get_irn_register(resproj);
1868         x87_push(state, arch_register_get_index(reg), resproj);
1869
1870 end_call:
1871         DB((dbg, LEVEL_1, "Stack after: "));
1872         DEBUG_ONLY(x87_dump_stack(state);)
1873
1874         return NO_NODE_ADDED;
1875 }
1876
1877 /**
1878  * Simulate a be_Return.
1879  *
1880  * @param state  the x87 state
1881  * @param n      the node that should be simulated (and patched)
1882  *
1883  * @return NO_NODE_ADDED
1884  */
1885 static int sim_Return(x87_state *state, ir_node *n)
1886 {
1887 #ifdef DEBUG_libfirm
1888         /* only floating point return values must reside on stack */
1889         int       n_float_res = 0;
1890         int const n_res       = be_Return_get_n_rets(n);
1891         for (int i = 0; i < n_res; ++i) {
1892                 ir_node *const res = get_irn_n(n, n_be_Return_val + i);
1893                 if (mode_is_float(get_irn_mode(res)))
1894                         ++n_float_res;
1895         }
1896         assert(x87_get_depth(state) == n_float_res);
1897 #endif
1898
1899         /* pop them virtually */
1900         x87_emms(state);
1901         return NO_NODE_ADDED;
1902 }
1903
1904 /**
1905  * Simulate a be_Perm.
1906  *
1907  * @param state  the x87 state
1908  * @param irn    the node that should be simulated (and patched)
1909  *
1910  * @return NO_NODE_ADDED
1911  */
1912 static int sim_Perm(x87_state *state, ir_node *irn)
1913 {
1914         int      i, n;
1915         ir_node *pred = get_irn_n(irn, 0);
1916         int     *stack_pos;
1917
1918         /* handle only floating point Perms */
1919         if (! mode_is_float(get_irn_mode(pred)))
1920                 return NO_NODE_ADDED;
1921
1922         DB((dbg, LEVEL_1, ">>> %+F\n", irn));
1923
1924         /* Perm is a pure virtual instruction on x87.
1925            All inputs must be on the FPU stack and are pairwise
1926            different from each other.
1927            So, all we need to do is to permutate the stack state. */
1928         n = get_irn_arity(irn);
1929         NEW_ARR_A(int, stack_pos, n);
1930
1931         /* collect old stack positions */
1932         for (i = 0; i < n; ++i) {
1933                 const arch_register_t *inreg = x87_get_irn_register(get_irn_n(irn, i));
1934                 int idx = x87_on_stack(state, arch_register_get_index(inreg));
1935
1936                 assert(idx >= 0 && "Perm argument not on x87 stack");
1937
1938                 stack_pos[i] = idx;
1939         }
1940         /* now do the permutation */
1941         foreach_out_edge(irn, edge) {
1942                 ir_node               *proj = get_edge_src_irn(edge);
1943                 const arch_register_t *out  = x87_get_irn_register(proj);
1944                 long                  num   = get_Proj_proj(proj);
1945
1946                 assert(0 <= num && num < n && "More Proj's than Perm inputs");
1947                 x87_set_st(state, arch_register_get_index(out), proj, stack_pos[(unsigned)num]);
1948         }
1949         DB((dbg, LEVEL_1, "<<< %+F\n", irn));
1950
1951         return NO_NODE_ADDED;
1952 }
1953
1954 /**
1955  * Kill any dead registers at block start by popping them from the stack.
1956  *
1957  * @param sim    the simulator handle
1958  * @param block  the current block
1959  * @param state  the x87 state at the begin of the block
1960  */
1961 static void x87_kill_deads(x87_simulator *const sim, ir_node *const block, x87_state *const state)
1962 {
1963         ir_node *first_insn = sched_first(block);
1964         ir_node *keep = NULL;
1965         unsigned live = vfp_live_args_after(sim, block, 0);
1966         unsigned kill_mask;
1967         int i, depth, num_pop;
1968
1969         kill_mask = 0;
1970         depth = x87_get_depth(state);
1971         for (i = depth - 1; i >= 0; --i) {
1972                 int reg = x87_get_st_reg(state, i);
1973
1974                 if (! is_vfp_live(reg, live))
1975                         kill_mask |= (1 << i);
1976         }
1977
1978         if (kill_mask) {
1979                 DB((dbg, LEVEL_1, "Killing deads:\n"));
1980                 DEBUG_ONLY(vfp_dump_live(live);)
1981                 DEBUG_ONLY(x87_dump_stack(state);)
1982
1983                 if (kill_mask != 0 && live == 0) {
1984                         /* special case: kill all registers */
1985                         if (ia32_cg_config.use_femms || ia32_cg_config.use_emms) {
1986                                 if (ia32_cg_config.use_femms) {
1987                                         /* use FEMMS on AMD processors to clear all */
1988                                         keep = new_bd_ia32_femms(NULL, block);
1989                                 } else {
1990                                         /* use EMMS to clear all */
1991                                         keep = new_bd_ia32_emms(NULL, block);
1992                                 }
1993                                 sched_add_before(first_insn, keep);
1994                                 keep_alive(keep);
1995                                 x87_emms(state);
1996                                 return;
1997                         }
1998                 }
1999                 /* now kill registers */
2000                 while (kill_mask) {
2001                         /* we can only kill from TOS, so bring them up */
2002                         if (! (kill_mask & 1)) {
2003                                 /* search from behind, because we can to a double-pop */
2004                                 for (i = depth - 1; i >= 0; --i) {
2005                                         if (kill_mask & (1 << i)) {
2006                                                 kill_mask &= ~(1 << i);
2007                                                 kill_mask |= 1;
2008                                                 break;
2009                                         }
2010                                 }
2011
2012                                 if (keep)
2013                                         x87_set_st(state, -1, keep, i);
2014                                 x87_create_fxch(state, first_insn, i);
2015                         }
2016
2017                         if ((kill_mask & 3) == 3) {
2018                                 /* we can do a double-pop */
2019                                 num_pop = 2;
2020                         }
2021                         else {
2022                                 /* only a single pop */
2023                                 num_pop = 1;
2024                         }
2025
2026                         depth -= num_pop;
2027                         kill_mask >>= num_pop;
2028                         keep = x87_create_fpop(state, first_insn, num_pop);
2029                 }
2030                 keep_alive(keep);
2031         }
2032 }
2033
2034 /**
2035  * Run a simulation and fix all virtual instructions for a block.
2036  *
2037  * @param sim          the simulator handle
2038  * @param block        the current block
2039  */
2040 static void x87_simulate_block(x87_simulator *sim, ir_node *block)
2041 {
2042         ir_node *n, *next;
2043         blk_state *bl_state = x87_get_bl_state(sim, block);
2044         x87_state *state = bl_state->begin;
2045         ir_node *start_block;
2046
2047         assert(state != NULL);
2048         /* already processed? */
2049         if (bl_state->end != NULL)
2050                 return;
2051
2052         DB((dbg, LEVEL_1, "Simulate %+F\n", block));
2053         DB((dbg, LEVEL_2, "State at Block begin:\n "));
2054         DEBUG_ONLY(x87_dump_stack(state);)
2055
2056         /* create a new state, will be changed */
2057         state = x87_clone_state(sim, state);
2058         /* at block begin, kill all dead registers */
2059         x87_kill_deads(sim, block, state);
2060
2061         /* beware, n might change */
2062         for (n = sched_first(block); !sched_is_end(n); n = next) {
2063                 int node_inserted;
2064                 sim_func func;
2065                 ir_op *op = get_irn_op(n);
2066
2067                 /*
2068                  * get the next node to be simulated here.
2069                  * n might be completely removed from the schedule-
2070                  */
2071                 next = sched_next(n);
2072                 if (op->ops.generic != NULL) {
2073                         func = (sim_func)op->ops.generic;
2074
2075                         /* simulate it */
2076                         node_inserted = (*func)(state, n);
2077
2078                         /*
2079                          * sim_func might have added an additional node after n,
2080                          * so update next node
2081                          * beware: n must not be changed by sim_func
2082                          * (i.e. removed from schedule) in this case
2083                          */
2084                         if (node_inserted != NO_NODE_ADDED)
2085                                 next = sched_next(n);
2086                 }
2087         }
2088
2089         start_block = get_irg_start_block(get_irn_irg(block));
2090
2091         DB((dbg, LEVEL_2, "State at Block end:\n ")); DEBUG_ONLY(x87_dump_stack(state);)
2092
2093         /* check if the state must be shuffled */
2094         foreach_block_succ(block, edge) {
2095                 ir_node *succ = get_edge_src_irn(edge);
2096                 blk_state *succ_state;
2097
2098                 if (succ == start_block)
2099                         continue;
2100
2101                 succ_state = x87_get_bl_state(sim, succ);
2102
2103                 if (succ_state->begin == NULL) {
2104                         DB((dbg, LEVEL_2, "Set begin state for succ %+F:\n", succ));
2105                         DEBUG_ONLY(x87_dump_stack(state);)
2106                         succ_state->begin = state;
2107
2108                         waitq_put(sim->worklist, succ);
2109                 } else {
2110                         DB((dbg, LEVEL_2, "succ %+F already has a state, shuffling\n", succ));
2111                         /* There is already a begin state for the successor, bad.
2112                            Do the necessary permutations.
2113                            Note that critical edges are removed, so this is always possible:
2114                            If the successor has more than one possible input, then it must
2115                            be the only one.
2116                          */
2117                         x87_shuffle(block, state, succ_state->begin);
2118                 }
2119         }
2120         bl_state->end = state;
2121 }
2122
2123 /**
2124  * Register a simulator function.
2125  *
2126  * @param op    the opcode to simulate
2127  * @param func  the simulator function for the opcode
2128  */
2129 static void register_sim(ir_op *op, sim_func func)
2130 {
2131         assert(op->ops.generic == NULL);
2132         op->ops.generic = (op_func) func;
2133 }
2134
2135 /**
2136  * Create a new x87 simulator.
2137  *
2138  * @param sim       a simulator handle, will be initialized
2139  * @param irg       the current graph
2140  */
2141 static void x87_init_simulator(x87_simulator *sim, ir_graph *irg)
2142 {
2143         obstack_init(&sim->obst);
2144         sim->blk_states = pmap_create();
2145         sim->n_idx      = get_irg_last_idx(irg);
2146         sim->live       = OALLOCN(&sim->obst, vfp_liveness, sim->n_idx);
2147
2148         DB((dbg, LEVEL_1, "--------------------------------\n"
2149                 "x87 Simulator started for %+F\n", irg));
2150
2151         /* set the generic function pointer of instruction we must simulate */
2152         ir_clear_opcodes_generic_func();
2153
2154         register_sim(op_ia32_Asm,          sim_Asm);
2155         register_sim(op_ia32_Call,         sim_Call);
2156         register_sim(op_ia32_vfld,         sim_fld);
2157         register_sim(op_ia32_vfild,        sim_fild);
2158         register_sim(op_ia32_vfld1,        sim_fld1);
2159         register_sim(op_ia32_vfldz,        sim_fldz);
2160         register_sim(op_ia32_vfadd,        sim_fadd);
2161         register_sim(op_ia32_vfsub,        sim_fsub);
2162         register_sim(op_ia32_vfmul,        sim_fmul);
2163         register_sim(op_ia32_vfdiv,        sim_fdiv);
2164         register_sim(op_ia32_vfprem,       sim_fprem);
2165         register_sim(op_ia32_vfabs,        sim_fabs);
2166         register_sim(op_ia32_vfchs,        sim_fchs);
2167         register_sim(op_ia32_vfist,        sim_fist);
2168         register_sim(op_ia32_vfisttp,      sim_fisttp);
2169         register_sim(op_ia32_vfst,         sim_fst);
2170         register_sim(op_ia32_vFtstFnstsw,  sim_FtstFnstsw);
2171         register_sim(op_ia32_vFucomFnstsw, sim_Fucom);
2172         register_sim(op_ia32_vFucomi,      sim_Fucom);
2173         register_sim(op_be_Copy,           sim_Copy);
2174         register_sim(op_be_Return,         sim_Return);
2175         register_sim(op_be_Perm,           sim_Perm);
2176         register_sim(op_be_Keep,           sim_Keep);
2177 }
2178
2179 /**
2180  * Destroy a x87 simulator.
2181  *
2182  * @param sim  the simulator handle
2183  */
2184 static void x87_destroy_simulator(x87_simulator *sim)
2185 {
2186         pmap_destroy(sim->blk_states);
2187         obstack_free(&sim->obst, NULL);
2188         DB((dbg, LEVEL_1, "x87 Simulator stopped\n\n"));
2189 }
2190
2191 /**
2192  * Pre-block walker: calculate the liveness information for the block
2193  * and store it into the sim->live cache.
2194  */
2195 static void update_liveness_walker(ir_node *block, void *data)
2196 {
2197         x87_simulator *sim = (x87_simulator*)data;
2198         update_liveness(sim, block);
2199 }
2200
2201 /*
2202  * Run a simulation and fix all virtual instructions for a graph.
2203  * Replaces all virtual floating point instructions and registers
2204  * by real ones.
2205  */
2206 void ia32_x87_simulate_graph(ir_graph *irg)
2207 {
2208         /* TODO improve code quality (less executed fxch) by using execfreqs */
2209
2210         ir_node       *block, *start_block;
2211         blk_state     *bl_state;
2212         x87_simulator sim;
2213
2214         /* create the simulator */
2215         x87_init_simulator(&sim, irg);
2216
2217         start_block = get_irg_start_block(irg);
2218         bl_state    = x87_get_bl_state(&sim, start_block);
2219
2220         /* start with the empty state */
2221         empty.sim       = &sim;
2222         bl_state->begin = &empty;
2223
2224         sim.worklist = new_waitq();
2225         waitq_put(sim.worklist, start_block);
2226
2227         be_assure_live_sets(irg);
2228         sim.lv = be_get_irg_liveness(irg);
2229
2230         /* Calculate the liveness for all nodes. We must precalculate this info,
2231          * because the simulator adds new nodes (possible before Phi nodes) which
2232          * would let a lazy calculation fail.
2233          * On the other hand we reduce the computation amount due to
2234          * precaching from O(n^2) to O(n) at the expense of O(n) cache memory.
2235          */
2236         irg_block_walk_graph(irg, update_liveness_walker, NULL, &sim);
2237
2238         /* iterate */
2239         do {
2240                 block = (ir_node*)waitq_get(sim.worklist);
2241                 x87_simulate_block(&sim, block);
2242         } while (! waitq_empty(sim.worklist));
2243
2244         /* kill it */
2245         del_waitq(sim.worklist);
2246         x87_destroy_simulator(&sim);
2247 }
2248
2249 /* Initializes the x87 simulator. */
2250 void ia32_init_x87(void)
2251 {
2252         FIRM_DBG_REGISTER(dbg, "firm.be.ia32.x87");
2253 }