fixed CopyB emitter
[libfirm] / ir / be / ia32 / bearch_ia32.c
index 8993933..dcefdcf 100644 (file)
@@ -1,6 +1,6 @@
 /**
  * This is the main ia32 firm backend driver.
- *
+ * @author Christian Wuerdig
  * $Id$
  */
 
@@ -29,8 +29,8 @@
 #include "ircons.h"
 #include "irgmod.h"
 #include "irgopt.h"
-
-#include "bitset.h"
+#include "irbitset.h"
+#include "pdeq.h"
 #include "debug.h"
 
 #include "../beabi.h"                 /* the general register allocator interface */
@@ -69,6 +69,21 @@ ir_node *ia32_new_NoReg_fp(ia32_code_gen_t *cg) {
                USE_SSE2(cg) ? &ia32_xmm_regs[REG_XMM_NOREG] : &ia32_vfp_regs[REG_VFP_NOREG]);
 }
 
+/* returns the first Proj with given mode from mode_T node */
+static ir_node *get_proj_for_mode(ir_node *node, ir_mode *mode) {
+       const ir_edge_t *edge;
+
+       assert(get_irn_mode(node) == mode_T && "Need mode_T node.");
+
+       foreach_out_edge(node, edge) {
+               ir_node *proj = get_edge_src_irn(edge);
+               if (get_irn_mode(proj) == mode)
+                       return proj;
+       }
+
+       return NULL;
+}
+
 /**************************************************
  *                         _ _              _  __
  *                        | | |            (_)/ _|
@@ -232,6 +247,10 @@ static arch_irn_class_t ia32_classify(const void *self, const ir_node *irn) {
                return arch_irn_class_branch;
        else if (is_ia32_Cnst(irn))
                return arch_irn_class_const;
+       else if (is_ia32_Ld(irn))
+               return arch_irn_class_load;
+       else if (is_ia32_St(irn) || is_ia32_Store8Bit(irn))
+               return arch_irn_class_store;
        else if (is_ia32_irn(irn))
                return arch_irn_class_normal;
        else
@@ -292,6 +311,12 @@ static void *ia32_abi_init(const be_abi_call_t *call, const arch_env_t *aenv, ir
        return env;
 }
 
+/**
+ * Put all registers which are saved by the prologue/epilogue in a set.
+ *
+ * @param self  The callback object.
+ * @param s     The result set.
+ */
 static void ia32_abi_dont_save_regs(void *self, pset *s)
 {
        ia32_abi_env_t *env = self;
@@ -301,9 +326,11 @@ static void ia32_abi_dont_save_regs(void *self, pset *s)
 
 /**
  * Generate the routine prologue.
+ *
  * @param self    The callback object.
  * @param mem     A pointer to the mem node. Update this if you define new memory.
  * @param reg_map A map mapping all callee_save/ignore/parameter registers to their defining nodes.
+ *
  * @return        The register which shall be used as a stack frame base.
  *
  * All nodes which define registers in @p reg_map must keep @p reg_map current.
@@ -352,6 +379,7 @@ static const arch_register_t *ia32_abi_prologue(void *self, ir_node **mem, pmap
 /**
  * Generate the routine epilogue.
  * @param self    The callback object.
+ * @param bl      The block for the epilog
  * @param mem     A pointer to the mem node. Update this if you define new memory.
  * @param reg_map A map mapping all callee_save/ignore/parameter registers to their defining nodes.
  * @return        The register which shall be used as a stack frame base.
@@ -373,8 +401,8 @@ static void ia32_abi_epilogue(void *self, ir_node *bl, ir_node **mem, pmap *reg_
                ir_mode *mode_bp = env->isa->bp->reg_class->mode;
                int reg_size     = get_mode_size_bytes(env->isa->bp->reg_class->mode);
 
-               /* AMD processors prefer leave at the end of a routine */
-    if (ARCH_AMD(isa->opt_arch)) {
+               /* gcc always emits a leave at the end of a routine */
+               if (1 || ARCH_AMD(isa->opt_arch)) {
                        ir_node *leave;
 
                        /* leave */
@@ -412,6 +440,7 @@ static void ia32_abi_epilogue(void *self, ir_node *bl, ir_node **mem, pmap *reg_
  */
 static ir_type *ia32_abi_get_between_type(void *self)
 {
+#define IDENT(s) new_id_from_chars(s, sizeof(s)-1)
        static ir_type *omit_fp_between_type = NULL;
        static ir_type *between_type         = NULL;
 
@@ -422,25 +451,204 @@ static ir_type *ia32_abi_get_between_type(void *self)
                entity *ret_addr_ent;
                entity *omit_fp_ret_addr_ent;
 
-               ir_type *old_bp_type   = new_type_primitive(new_id_from_str("bp"), mode_P);
-               ir_type *ret_addr_type = new_type_primitive(new_id_from_str("return_addr"), mode_P);
+               ir_type *old_bp_type   = new_type_primitive(IDENT("bp"), mode_P);
+               ir_type *ret_addr_type = new_type_primitive(IDENT("return_addr"), mode_P);
 
-               between_type           = new_type_class(new_id_from_str("ia32_between_type"));
-               old_bp_ent             = new_entity(between_type, new_id_from_str("old_bp"), old_bp_type);
-               ret_addr_ent           = new_entity(between_type, new_id_from_str("ret_addr"), ret_addr_type);
+               between_type           = new_type_struct(IDENT("ia32_between_type"));
+               old_bp_ent             = new_entity(between_type, IDENT("old_bp"), old_bp_type);
+               ret_addr_ent           = new_entity(between_type, IDENT("ret_addr"), ret_addr_type);
 
                set_entity_offset_bytes(old_bp_ent, 0);
                set_entity_offset_bytes(ret_addr_ent, get_type_size_bytes(old_bp_type));
                set_type_size_bytes(between_type, get_type_size_bytes(old_bp_type) + get_type_size_bytes(ret_addr_type));
+               set_type_state(between_type, layout_fixed);
 
-               omit_fp_between_type   = new_type_class(new_id_from_str("ia32_between_type_omit_fp"));
-               omit_fp_ret_addr_ent   = new_entity(omit_fp_between_type, new_id_from_str("ret_addr"), ret_addr_type);
+               omit_fp_between_type   = new_type_struct(IDENT("ia32_between_type_omit_fp"));
+               omit_fp_ret_addr_ent   = new_entity(omit_fp_between_type, IDENT("ret_addr"), ret_addr_type);
 
                set_entity_offset_bytes(omit_fp_ret_addr_ent, 0);
                set_type_size_bytes(omit_fp_between_type, get_type_size_bytes(ret_addr_type));
+               set_type_state(omit_fp_between_type, layout_fixed);
        }
 
        return env->flags.try_omit_fp ? omit_fp_between_type : between_type;
+#undef IDENT
+}
+
+/**
+ * Get the estimated cycle count for @p irn.
+ *
+ * @param self The this pointer.
+ * @param irn  The node.
+ *
+ * @return     The estimated cycle count for this operation
+ */
+static int ia32_get_op_estimated_cost(const void *self, const ir_node *irn)
+{
+  int cost;
+  switch (get_ia32_irn_opcode(irn)) {
+    case iro_ia32_xDiv:
+    case iro_ia32_DivMod:
+      cost = 8;
+      break;
+
+    case iro_ia32_xLoad:
+    case iro_ia32_l_Load:
+    case iro_ia32_Load:
+    case iro_ia32_Push:
+    case iro_ia32_Pop:
+      cost = 10;
+      break;
+
+    case iro_ia32_xStore:
+    case iro_ia32_l_Store:
+    case iro_ia32_Store:
+    case iro_ia32_Store8Bit:
+      cost = 50;
+      break;
+
+    case iro_ia32_MulS:
+    case iro_ia32_Mul:
+    case iro_ia32_Mulh:
+    case iro_ia32_xMul:
+    case iro_ia32_l_MulS:
+    case iro_ia32_l_Mul:
+      cost = 2;
+      break;
+
+    default:
+      cost = 1;
+  }
+
+  return cost;
+}
+
+/**
+ * Returns the inverse operation if @p irn, recalculating the argument at position @p i.
+ *
+ * @param irn       The original operation
+ * @param i         Index of the argument we want the inverse operation to yield
+ * @param inverse   struct to be filled with the resulting inverse op
+ * @param obstack   The obstack to use for allocation of the returned nodes array
+ * @return          The inverse operation or NULL if operation invertible
+ */
+static arch_inverse_t *ia32_get_inverse(const void *self, const ir_node *irn, int i, arch_inverse_t *inverse, struct obstack *obst) {
+       ir_graph *irg;
+       ir_mode  *mode;
+       ir_node  *block, *noreg, *nomem;
+       int      pnc;
+
+       /* we cannot invert non-ia32 irns */
+       if (! is_ia32_irn(irn))
+               return NULL;
+
+       /* operand must always be a real operand (not base, index or mem) */
+       if (i != 2 && i != 3)
+               return NULL;
+
+       /* we don't invert address mode operations */
+       if (get_ia32_op_type(irn) != ia32_Normal)
+               return NULL;
+
+       irg   = get_irn_irg(irn);
+       block = get_nodes_block(irn);
+       mode  = get_ia32_res_mode(irn);
+       noreg = get_irn_n(irn, 0);
+       nomem = new_r_NoMem(irg);
+
+       /* initialize structure */
+       inverse->nodes = obstack_alloc(obst, 2 * sizeof(inverse->nodes[0]));
+       inverse->costs = 0;
+       inverse->n     = 2;
+
+       switch (get_ia32_irn_opcode(irn)) {
+               case iro_ia32_Add:
+                       if (get_ia32_immop_type(irn) == ia32_ImmConst) {
+                               /* we have an add with a const here */
+                               /* invers == add with negated const */
+                               inverse->nodes[0] = new_rd_ia32_Add(NULL, irg, block, noreg, noreg, get_irn_n(irn, i), noreg, nomem);
+                               pnc               = pn_ia32_Add_res;
+                               inverse->costs   += 1;
+                               copy_ia32_Immop_attr(inverse->nodes[0], (ir_node *)irn);
+                               set_ia32_Immop_tarval(inverse->nodes[0], tarval_neg(get_ia32_Immop_tarval(irn)));
+                               set_ia32_commutative(inverse->nodes[0]);
+                       }
+                       else if (get_ia32_immop_type(irn) == ia32_ImmSymConst) {
+                               /* we have an add with a symconst here */
+                               /* invers == sub with const */
+                               inverse->nodes[0] = new_rd_ia32_Sub(NULL, irg, block, noreg, noreg, get_irn_n(irn, i), noreg, nomem);
+                               pnc               = pn_ia32_Sub_res;
+                               inverse->costs   += 2;
+                               copy_ia32_Immop_attr(inverse->nodes[0], (ir_node *)irn);
+                       }
+                       else {
+                               /* normal add: inverse == sub */
+                               ir_node  *proj = get_irn_out_edge_first(irn)->src;
+                               assert(proj && is_Proj(proj));
+
+                               inverse->nodes[0] = new_rd_ia32_Sub(NULL, irg, block, noreg, noreg, proj, get_irn_n(irn, i ^ 1), nomem);
+                               pnc               = pn_ia32_Sub_res;
+                               inverse->costs   += 2;
+                       }
+                       break;
+               case iro_ia32_Sub:
+                       if (get_ia32_immop_type(irn) != ia32_ImmNone) {
+                               /* we have a sub with a const/symconst here */
+                               /* invers == add with this const */
+                               inverse->nodes[0] = new_rd_ia32_Add(NULL, irg, block, noreg, noreg, get_irn_n(irn, i), noreg, nomem);
+                               pnc               = pn_ia32_Add_res;
+                               inverse->costs   += (get_ia32_immop_type(irn) == ia32_ImmSymConst) ? 5 : 1;
+                               copy_ia32_Immop_attr(inverse->nodes[0], (ir_node *)irn);
+                       }
+                       else {
+                               /* normal sub */
+                               ir_node  *proj = get_irn_out_edge_first(irn)->src;
+                               assert(proj && is_Proj(proj));
+
+                               if (i == 2) {
+                                       inverse->nodes[0] = new_rd_ia32_Add(NULL, irg, block, noreg, noreg, proj, get_irn_n(irn, 3), nomem);
+                               }
+                               else {
+                                       inverse->nodes[0] = new_rd_ia32_Sub(NULL, irg, block, noreg, noreg, get_irn_n(irn, 2), proj, nomem);
+                               }
+                               pnc             = pn_ia32_Sub_res;
+                               inverse->costs += 1;
+                       }
+                       break;
+               case iro_ia32_Eor:
+                       if (get_ia32_immop_type(irn) != ia32_ImmNone) {
+                               /* xor with const: inverse = xor */
+                               inverse->nodes[0] = new_rd_ia32_Eor(NULL, irg, block, noreg, noreg, get_irn_n(irn, i), noreg, nomem);
+                               pnc               = pn_ia32_Eor_res;
+                               inverse->costs   += (get_ia32_immop_type(irn) == ia32_ImmSymConst) ? 5 : 1;
+                               copy_ia32_Immop_attr(inverse->nodes[0], (ir_node *)irn);
+                       }
+                       else {
+                               /* normal xor */
+                               inverse->nodes[0] = new_rd_ia32_Eor(NULL, irg, block, noreg, noreg, (ir_node *)irn, get_irn_n(irn, i), nomem);
+                               pnc               = pn_ia32_Eor_res;
+                               inverse->costs   += 1;
+                       }
+                       break;
+               case iro_ia32_Not:
+                       inverse->nodes[0] = new_rd_ia32_Not(NULL, irg, block, noreg, noreg, get_irn_n(irn, i), nomem);
+                       pnc = pn_ia32_Not_res;
+                       inverse->costs   += 1;
+                       break;
+               case iro_ia32_Minus:
+                       inverse->nodes[0] = new_rd_ia32_Minus(NULL, irg, block, noreg, noreg, get_irn_n(irn, i), nomem);
+                       pnc = pn_ia32_Minus_res;
+                       inverse->costs   += 1;
+                       break;
+               default:
+                       /* inverse operation not supported */
+                       return NULL;
+       }
+
+       set_ia32_res_mode(inverse->nodes[0], mode);
+       inverse->nodes[1] = new_r_Proj(irg, block, inverse->nodes[0], mode, pnc);
+
+       return inverse;
 }
 
 static const be_abi_callbacks_t ia32_abi_callbacks = {
@@ -461,7 +669,9 @@ static const arch_irn_ops_if_t ia32_irn_ops_if = {
        ia32_classify,
        ia32_get_flags,
        ia32_get_frame_entity,
-       ia32_set_stack_bias
+       ia32_set_stack_bias,
+       ia32_get_inverse,
+       ia32_get_op_estimated_cost
 };
 
 ia32_irn_ops_t ia32_irn_ops = {
@@ -488,27 +698,41 @@ ia32_irn_ops_t ia32_irn_ops = {
  */
 static void ia32_prepare_graph(void *self) {
        ia32_code_gen_t *cg = self;
+       dom_front_info_t *dom;
        DEBUG_ONLY(firm_dbg_module_t *old_mod = cg->mod;)
 
        FIRM_DBG_REGISTER(cg->mod, "firm.be.ia32.transform");
+
+       /* 1st: transform constants and psi condition trees */
+       irg_walk_blkwise_graph(cg->irg, ia32_place_consts_set_modes, ia32_transform_psi_cond_tree, cg);
+
+       /* 2nd: transform all remaining nodes */
        ia32_register_transformers();
-       irg_walk_blkwise_graph(cg->irg, ia32_place_consts_set_modes, ia32_transform_node, cg);
-       be_dump(cg->irg, "-transformed", dump_ir_block_graph_sched);
+       dom = be_compute_dominance_frontiers(cg->irg);
+       irg_walk_blkwise_graph(cg->irg, NULL, ia32_transform_node, cg);
+       be_free_dominance_frontiers(dom);
 
-       if (cg->opt & IA32_OPT_DOAM) {
-               edges_deactivate(cg->irg);
-               //dead_node_elimination(cg->irg);
-               edges_activate(cg->irg);
+       if (cg->dump)
+               be_dump(cg->irg, "-transformed", dump_ir_block_graph_sched);
 
-               FIRM_DBG_REGISTER(cg->mod, "firm.be.ia32.am");
+       /* 3rd: optimize address mode */
+       FIRM_DBG_REGISTER(cg->mod, "firm.be.ia32.am");
+       ia32_optimize_addressmode(cg);
 
-               irg_walk_blkwise_graph(cg->irg, NULL, ia32_optimize_am, cg);
+       if (cg->dump)
                be_dump(cg->irg, "-am", dump_ir_block_graph_sched);
-       }
 
        DEBUG_ONLY(cg->mod = old_mod;)
 }
 
+static INLINE int need_constraint_copy(ir_node *irn) {
+       return \
+               ! is_ia32_Lea(irn)          && \
+               ! is_ia32_Conv_I2I(irn)     && \
+               ! is_ia32_Conv_I2I8Bit(irn) && \
+               ! is_ia32_CmpCMov(irn)      && \
+               ! is_ia32_CmpSet(irn);
+}
 
 /**
  * Insert copies for all ia32 nodes where the should_be_same requirement
@@ -534,8 +758,7 @@ static void ia32_finish_node(ir_node *irn, void *env) {
                block = get_nodes_block(irn);
 
                /* check all OUT requirements, if there is a should_be_same */
-               if ((op_tp == ia32_Normal || op_tp == ia32_AddrModeS) &&
-                       ! is_ia32_Lea(irn) && ! is_ia32_Conv_I2I(irn) && ! is_ia32_Conv_I2I8Bit(irn))
+               if ((op_tp == ia32_Normal || op_tp == ia32_AddrModeS) && need_constraint_copy(irn))
                {
                        for (i = 0; i < n_res; i++) {
                                if (arch_register_req_is(&(reqs[i]->req), should_be_same)) {
@@ -586,11 +809,14 @@ insert_copy:
                        }
                }
 
-               /* If we have a CondJmp with immediate, we need to    */
+               /* If we have a CondJmp/CmpSet/xCmpSet with immediate, we need to    */
                /* check if it's the right operand, otherwise we have */
                /* to change it, as CMP doesn't support immediate as  */
                /* left operands.                                     */
-               if (is_ia32_CondJmp(irn) && (is_ia32_ImmConst(irn) || is_ia32_ImmSymConst(irn)) && op_tp == ia32_AddrModeS) {
+               if ((is_ia32_CondJmp(irn) || is_ia32_CmpSet(irn) || is_ia32_xCmpSet(irn)) &&
+                       (is_ia32_ImmConst(irn) || is_ia32_ImmSymConst(irn))                   &&
+                       op_tp == ia32_AddrModeS)
+               {
                        set_ia32_op_type(irn, ia32_AddrModeD);
                        set_ia32_pncode(irn, get_inversed_pnc(get_ia32_pncode(irn)));
                }
@@ -610,17 +836,32 @@ end:
 static void ia32_finish_irg_walker(ir_node *block, void *env) {
        ir_node *irn, *next;
 
-       for (irn = sched_first(block); !sched_is_end(irn); irn = next) {
+       for (irn = sched_first(block); ! sched_is_end(irn); irn = next) {
                next = sched_next(irn);
                ia32_finish_node(irn, env);
        }
 }
 
+static void ia32_push_on_queue_walker(ir_node *block, void *env) {
+       waitq *wq = env;
+       waitq_put(wq, block);
+}
+
+
 /**
  * Add Copy nodes for not fulfilled should_be_equal constraints
  */
 static void ia32_finish_irg(ir_graph *irg, ia32_code_gen_t *cg) {
-       irg_block_walk_graph(irg, NULL, ia32_finish_irg_walker, cg);
+       waitq *wq = new_waitq();
+
+       /* Push the blocks on the waitq because ia32_finish_irg_walker starts more walks ... */
+       irg_block_walk_graph(irg, NULL, ia32_push_on_queue_walker, wq);
+
+       while (! waitq_empty(wq)) {
+               ir_node *block = waitq_get(wq);
+               ia32_finish_irg_walker(block, cg);
+       }
+       del_waitq(wq);
 }
 
 
@@ -631,15 +872,80 @@ static void ia32_finish_irg(ir_graph *irg, ia32_code_gen_t *cg) {
 static void ia32_before_sched(void *self) {
 }
 
+static void remove_unused_nodes(ir_node *irn, bitset_t *already_visited) {
+       int i;
+       ir_mode *mode;
+       ir_node *mem_proj;
+
+       if (is_Block(irn))
+               return;
+
+       mode = get_irn_mode(irn);
+
+       /* check if we already saw this node or the node has more than one user */
+       if (bitset_contains_irn(already_visited, irn) || get_irn_n_edges(irn) > 1)
+               return;
+
+       /* mark irn visited */
+       bitset_add_irn(already_visited, irn);
+
+       /* non-Tuple nodes with one user: ok, return */
+       if (get_irn_n_edges(irn) >= 1 && mode != mode_T)
+               return;
+
+       /* tuple node has one user which is not the mem proj-> ok */
+       if (mode == mode_T && get_irn_n_edges(irn) == 1) {
+               mem_proj = get_proj_for_mode(irn, mode_M);
+               if (! mem_proj)
+                       return;
+       }
+
+       for (i = get_irn_arity(irn) - 1; i >= 0; i--) {
+               ir_node *pred = get_irn_n(irn, i);
+
+               /* do not follow memory edges or we will accidentally remove stores */
+               if (is_Proj(pred) && get_irn_mode(pred) == mode_M)
+                       continue;
+
+               set_irn_n(irn, i, new_Bad());
+
+               /*
+                       The current node is about to be removed: if the predecessor
+                       has only this node as user, it need to be removed as well.
+               */
+               if (get_irn_n_edges(pred) <= 1)
+                       remove_unused_nodes(pred, already_visited);
+       }
+
+       if (sched_is_scheduled(irn))
+               sched_remove(irn);
+}
+
+static void remove_unused_loads_walker(ir_node *irn, void *env) {
+       bitset_t *already_visited = env;
+       if (is_ia32_Ld(irn) && ! bitset_contains_irn(already_visited, irn))
+               remove_unused_nodes(irn, env);
+}
+
 /**
  * Called before the register allocator.
  * Calculate a block schedule here. We need it for the x87
  * simulator and the emitter.
  */
 static void ia32_before_ra(void *self) {
-       ia32_code_gen_t *cg = self;
+       ia32_code_gen_t *cg              = self;
+       bitset_t        *already_visited = bitset_irg_malloc(cg->irg);
 
        cg->blk_sched = sched_create_block_schedule(cg->irg);
+
+       /*
+               Handle special case:
+               There are sometimes unused loads, only pinned by memory.
+               We need to remove those Loads and all other nodes which won't be used
+               after removing the Load from schedule.
+       */
+       irg_walk_graph(cg->irg, remove_unused_loads_walker, NULL, already_visited);
+       bitset_free(already_visited);
 }
 
 
@@ -694,7 +1000,7 @@ static void transform_to_Load(ia32_transform_env_t *env) {
        reg = arch_get_irn_register(env->cg->arch_env, irn);
        arch_set_irn_register(env->cg->arch_env, new_op, reg);
 
-       SET_IA32_ORIG_NODE(new_op, ia32_get_old_node_name(env->cg, new_op));
+       SET_IA32_ORIG_NODE(new_op, ia32_get_old_node_name(env->cg, irn));
 
        exchange(irn, proj);
 }
@@ -743,12 +1049,10 @@ static void transform_to_Store(ia32_transform_env_t *env) {
 
        if (sched_point) {
                sched_add_after(sched_point, new_op);
-               sched_add_after(new_op, proj);
-
                sched_remove(irn);
        }
 
-       SET_IA32_ORIG_NODE(new_op, ia32_get_old_node_name(env->cg, new_op));
+       SET_IA32_ORIG_NODE(new_op, ia32_get_old_node_name(env->cg, irn));
 
        exchange(irn, proj);
 }
@@ -814,7 +1118,7 @@ static void ia32_after_ra(void *self) {
        irg_block_walk_graph(cg->irg, NULL, ia32_after_ra_walker, self);
 
        /* if we do x87 code generation, rewrite all the virtual instructions and registers */
-       if (cg->used_fp == fp_x87) {
+       if (cg->used_fp == fp_x87 || cg->force_sim) {
                x87_simulate_graph(cg->arch_env, cg->irg, cg->blk_sched);
        }
 }
@@ -829,7 +1133,8 @@ static void ia32_codegen(void *self) {
        ir_graph        *irg = cg->irg;
 
        ia32_finish_irg(irg, cg);
-       be_dump(irg, "-finished", dump_ir_block_graph_sched);
+       if (cg->dump)
+               be_dump(irg, "-finished", dump_ir_block_graph_sched);
        ia32_gen_routine(cg->isa->out, irg, cg);
 
        cur_reg_set = NULL;
@@ -873,6 +1178,7 @@ static void *ia32_cg_init(const be_irg_t *birg) {
        cg->gp_to_fp  = NULL;
        cg->fp_kind   = isa->fp_kind;
        cg->used_fp   = fp_none;
+       cg->dump      = (birg->main_env->options->dump_flags & DUMP_BE) ? 1 : 0;
 
        FIRM_DBG_REGISTER(cg->mod, "firm.be.ia32.cg");
 
@@ -916,19 +1222,20 @@ static void *ia32_cg_init(const be_irg_t *birg) {
  * arguments.
  */
 static ia32_isa_t ia32_isa_template = {
-  {
-         &ia32_isa_if,            /* isa interface implementation */
-         &ia32_gp_regs[REG_ESP],  /* stack pointer register */
-         &ia32_gp_regs[REG_EBP],  /* base pointer register */
-         -1,                      /* stack direction */
-  },
+       {
+               &ia32_isa_if,            /* isa interface implementation */
+               &ia32_gp_regs[REG_ESP],  /* stack pointer register */
+               &ia32_gp_regs[REG_EBP],  /* base pointer register */
+               -1,                      /* stack direction */
+       },
        NULL,                    /* 16bit register names */
        NULL,                    /* 8bit register names */
        NULL,                    /* types */
        NULL,                    /* tv_ents */
-       (0 |
+       (0                 |
        IA32_OPT_INCDEC    |     /* optimize add 1, sub 1 into inc/dec               default: on  */
        IA32_OPT_DOAM      |     /* optimize address mode                            default: on  */
+       IA32_OPT_LEA       |     /* optimize for LEAs                                default: on  */
        IA32_OPT_PLACECNST |     /* place constants immediately before instructions, default: on  */
        IA32_OPT_IMMOPS    |     /* operations can use immediates,                   default: on  */
        IA32_OPT_EXTBB),         /* use extended basic block scheduling,             default: on  */
@@ -957,7 +1264,6 @@ static void *ia32_init(FILE *file_handle) {
 
        ia32_register_init(isa);
        ia32_create_opcodes();
-       ia32_register_copy_attr_func();
 
        if ((ARCH_INTEL(isa->arch) && isa->arch < arch_pentium_4) ||
            (ARCH_AMD(isa->arch) && isa->arch < arch_athlon))
@@ -980,14 +1286,14 @@ static void *ia32_init(FILE *file_handle) {
 
        /* patch register names of x87 registers */
        if (USE_x87(isa)) {
-         ia32_st_regs[0].name = "st";
-         ia32_st_regs[1].name = "st(1)";
-         ia32_st_regs[2].name = "st(2)";
-         ia32_st_regs[3].name = "st(3)";
-         ia32_st_regs[4].name = "st(4)";
-         ia32_st_regs[5].name = "st(5)";
-         ia32_st_regs[6].name = "st(6)";
-         ia32_st_regs[7].name = "st(7)";
+               ia32_st_regs[0].name = "st";
+               ia32_st_regs[1].name = "st(1)";
+               ia32_st_regs[2].name = "st(2)";
+               ia32_st_regs[3].name = "st(3)";
+               ia32_st_regs[4].name = "st(4)";
+               ia32_st_regs[5].name = "st(5)";
+               ia32_st_regs[6].name = "st(6)";
+               ia32_st_regs[7].name = "st(7)";
        }
 
 #ifndef NDEBUG
@@ -996,6 +1302,8 @@ static void *ia32_init(FILE *file_handle) {
        isa->name_obst_size = 0;
 #endif /* NDEBUG */
 
+       ia32_handle_intrinsics();
+       ia32_switch_section(NULL, NO_SECTION);
        fprintf(isa->out, "\t.intel_syntax\n");
 
        inited = 1;
@@ -1032,11 +1340,11 @@ static void ia32_done(void *self) {
  * Return the number of register classes for this architecture.
  * We report always these:
  *  - the general purpose registers
- *  - the floating point register set (depending on the unit used for FP)
- *  - MMX/SSE registers (currently not supported)
+ *  - the SSE floating point register set
+ *  - the virtual floating point registers
  */
 static int ia32_get_n_reg_class(const void *self) {
-       return 2;
+       return 3;
 }
 
 /**
@@ -1044,10 +1352,13 @@ static int ia32_get_n_reg_class(const void *self) {
  */
 static const arch_register_class_t *ia32_get_reg_class(const void *self, int i) {
        const ia32_isa_t *isa = self;
-       assert(i >= 0 && i < 2 && "Invalid ia32 register class requested.");
+       assert(i >= 0 && i < 3 && "Invalid ia32 register class requested.");
        if (i == 0)
                return &ia32_reg_classes[CLASS_ia32_gp];
-       return USE_SSE2(isa) ? &ia32_reg_classes[CLASS_ia32_xmm] : &ia32_reg_classes[CLASS_ia32_vfp];
+       else if (i == 1)
+               return &ia32_reg_classes[CLASS_ia32_xmm];
+       else
+               return &ia32_reg_classes[CLASS_ia32_vfp];
 }
 
 /**
@@ -1084,9 +1395,11 @@ static void ia32_get_call_abi(const void *self, ir_type *method_type, be_abi_cal
        const arch_register_t *reg;
        be_abi_call_flags_t call_flags = be_abi_call_get_flags(abi);
 
+       unsigned use_push = !IS_P6_ARCH(isa->opt_arch);
+
        /* set abi flags for calls */
        call_flags.bits.left_to_right         = 0;  /* always last arg first on stack */
-       call_flags.bits.store_args_sequential = 0;  /* use stores instead of push */
+       call_flags.bits.store_args_sequential = use_push;
        /* call_flags.bits.try_omit_fp                 not changed: can handle both settings */
        call_flags.bits.fp_free               = 0;  /* the frame pointer is fixed in IA32 */
        call_flags.bits.call_has_imm          = 1;  /* IA32 calls can have immediate address */
@@ -1173,7 +1486,7 @@ const arch_irn_handler_t *ia32_get_irn_handler(const void *self) {
 }
 
 int ia32_to_appear_in_schedule(void *block_env, const ir_node *irn) {
-       return is_ia32_irn(irn);
+       return is_ia32_irn(irn) ? 1 : -1;
 }
 
 /**
@@ -1207,6 +1520,32 @@ static int ia32_get_reg_class_alignment(const void *self, const arch_register_cl
        return bytes;
 }
 
+static ia32_intrinsic_env_t intrinsic_env = { NULL, NULL };
+
+/**
+ * Returns the libFirm configuration parameter for this backend.
+ */
+static const backend_params *ia32_get_libfirm_params(void) {
+       static const arch_dep_params_t ad = {
+               1, /* also use subs */
+               4, /* maximum shifts */
+               31, /* maximum shift amount */
+
+               1, /* allow Mulhs */
+               1, /* allow Mulus */
+               32  /* Mulh allowed up to 32 bit */
+       };
+       static backend_params p = {
+               NULL,  /* no additional opcodes */
+               NULL,  /* will be set later */
+               1,     /* need dword lowering */
+               ia32_create_intrinsic_fkt,
+               &intrinsic_env,  /* context for ia32_create_intrinsic_fkt */
+       };
+
+       p.dep_param = &ad;
+       return &p;
+}
 #ifdef WITH_LIBCORE
 
 /* instruction set architectures. */
@@ -1252,14 +1591,26 @@ static lc_opt_enum_int_var_t fp_unit_var = {
        &ia32_isa_template.fp_kind, fp_unit_items
 };
 
+static const lc_opt_enum_int_items_t gas_items[] = {
+       { "linux",   ASM_LINUX_GAS },
+       { "mingw",   ASM_MINGW_GAS },
+       { NULL,      0 }
+};
+
+static lc_opt_enum_int_var_t gas_var = {
+       (int *)&asm_flavour, gas_items
+};
+
 static const lc_opt_table_entry_t ia32_options[] = {
-       LC_OPT_ENT_ENUM_INT("arch",   "select the instruction architecture", &arch_var),
-       LC_OPT_ENT_ENUM_INT("opt",    "optimize for instruction architecture", &opt_arch_var),
-       LC_OPT_ENT_ENUM_INT("fpunit", "select the floating point unit", &fp_unit_var),
-       LC_OPT_ENT_NEGBIT("noaddrmode", "do not use address mode", &ia32_isa_template.opt, IA32_OPT_DOAM),
+       LC_OPT_ENT_ENUM_INT("arch",      "select the instruction architecture", &arch_var),
+       LC_OPT_ENT_ENUM_INT("opt",       "optimize for instruction architecture", &opt_arch_var),
+       LC_OPT_ENT_ENUM_INT("fpunit",    "select the floating point unit", &fp_unit_var),
+       LC_OPT_ENT_NEGBIT("noaddrmode",  "do not use address mode", &ia32_isa_template.opt, IA32_OPT_DOAM),
+       LC_OPT_ENT_NEGBIT("nolea",       "do not optimize for LEAs", &ia32_isa_template.opt, IA32_OPT_LEA),
        LC_OPT_ENT_NEGBIT("noplacecnst", "do not place constants", &ia32_isa_template.opt, IA32_OPT_PLACECNST),
-       LC_OPT_ENT_NEGBIT("noimmop", "no operations with immediates", &ia32_isa_template.opt, IA32_OPT_IMMOPS),
-       LC_OPT_ENT_NEGBIT("noextbb", "do not use extended basic block scheduling", &ia32_isa_template.opt, IA32_OPT_EXTBB),
+       LC_OPT_ENT_NEGBIT("noimmop",     "no operations with immediates", &ia32_isa_template.opt, IA32_OPT_IMMOPS),
+       LC_OPT_ENT_NEGBIT("noextbb",     "do not use extended basic block scheduling", &ia32_isa_template.opt, IA32_OPT_EXTBB),
+       LC_OPT_ENT_ENUM_INT("gasmode",   "set the GAS compatibility mode", &gas_var),
        { NULL }
 };
 
@@ -1273,9 +1624,11 @@ static const lc_opt_table_entry_t ia32_options[] = {
  * ia32-fpunit=unit  select floating point unit (x87 or SSE2)
  * ia32-incdec       optimize for inc/dec
  * ia32-noaddrmode   do not use address mode
+ * ia32-nolea        do not optimize for LEAs
  * ia32-noplacecnst  do not place constants,
  * ia32-noimmop      no operations with immediates
  * ia32-noextbb      do not use extended basic block scheduling
+ * ia32-gasmode      set the GAS compatibility mode
  */
 static void ia32_register_options(lc_opt_entry_t *ent)
 {
@@ -1295,6 +1648,7 @@ const arch_isa_if_t ia32_isa_if = {
        ia32_get_code_generator_if,
        ia32_get_list_sched_selector,
        ia32_get_reg_class_alignment,
+       ia32_get_libfirm_params,
 #ifdef WITH_LIBCORE
        ia32_register_options
 #endif