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