Resolve constness warning.
[libfirm] / ir / lower / lower_dw.c
index 7dc3254..0b9da80 100644 (file)
@@ -22,7 +22,6 @@
  * @brief   Lower double word operations, i.e. 64bit -> 32bit, 32bit -> 16bit etc.
  * @date    8.10.2004
  * @author  Michael Beck
- * @version $Id$
  */
 #include "config.h"
 
 #include <stdbool.h>
 #include <assert.h>
 
+#include "be.h"
 #include "error.h"
 #include "lowering.h"
 #include "irnode_t.h"
+#include "irnodeset.h"
 #include "irgraph_t.h"
 #include "irmode_t.h"
 #include "iropt_t.h"
@@ -46,6 +47,7 @@
 #include "irgwalk.h"
 #include "ircons.h"
 #include "irflag.h"
+#include "iroptimize.h"
 #include "irtools.h"
 #include "debug.h"
 #include "set.h"
@@ -65,9 +67,15 @@ static set *conv_types;
 /** A map from a method type to its lowered type. */
 static pmap *lowered_type;
 
+/** A map from a builtin type to its lower and higher type. */
+static pmap *lowered_builtin_type_high;
+static pmap *lowered_builtin_type_low;
+
 /** The types for the binop and unop intrinsics. */
 static ir_type *binop_tp_u, *binop_tp_s, *unop_tp_u, *unop_tp_s, *tp_s, *tp_u;
 
+static ir_nodeset_t created_mux_nodes;
+
 /** the debug handle */
 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
 
@@ -133,7 +141,7 @@ static ir_type *get_conv_type(ir_mode *imode, ir_mode *omode)
        key.omode = omode;
        key.mtd   = NULL;
 
-       entry = (conv_tp_entry_t*)set_insert(conv_types, &key, sizeof(key), HASH_PTR(imode) ^ HASH_PTR(omode));
+       entry = (conv_tp_entry_t*)set_insert(conv_types, &key, sizeof(key), hash_ptr(imode) ^ hash_ptr(omode));
        if (! entry->mtd) {
                int n_param = 1, n_res = 1;
 
@@ -148,8 +156,13 @@ static ir_type *get_conv_type(ir_mode *imode, ir_mode *omode)
                /* set param types and result types */
                n_param = 0;
                if (imode == env->high_signed) {
-                       set_method_param_type(mtd, n_param++, tp_u);
-                       set_method_param_type(mtd, n_param++, tp_s);
+                       if (env->params->little_endian) {
+                               set_method_param_type(mtd, n_param++, tp_u);
+                               set_method_param_type(mtd, n_param++, tp_s);
+                       } else {
+                               set_method_param_type(mtd, n_param++, tp_s);
+                               set_method_param_type(mtd, n_param++, tp_u);
+                       }
                } else if (imode == env->high_unsigned) {
                        set_method_param_type(mtd, n_param++, tp_u);
                        set_method_param_type(mtd, n_param++, tp_u);
@@ -160,8 +173,13 @@ static ir_type *get_conv_type(ir_mode *imode, ir_mode *omode)
 
                n_res = 0;
                if (omode == env->high_signed) {
-                       set_method_res_type(mtd, n_res++, tp_u);
-                       set_method_res_type(mtd, n_res++, tp_s);
+                       if (env->params->little_endian) {
+                               set_method_res_type(mtd, n_res++, tp_u);
+                               set_method_res_type(mtd, n_res++, tp_s);
+                       } else {
+                               set_method_res_type(mtd, n_res++, tp_s);
+                               set_method_res_type(mtd, n_res++, tp_u);
+                       }
                } else if (omode == env->high_unsigned) {
                        set_method_res_type(mtd, n_res++, tp_u);
                        set_method_res_type(mtd, n_res++, tp_u);
@@ -496,7 +514,7 @@ static ir_node *get_intrinsic_address(ir_type *method, ir_op *op,
        key.ent   = NULL;
 
        entry = (op_mode_entry_t*)set_insert(intrinsic_fkt, &key, sizeof(key),
-                               HASH_PTR(op) ^ HASH_PTR(imode) ^ (HASH_PTR(omode) << 8));
+                               hash_ptr(op) ^ hash_ptr(imode) ^ (hash_ptr(omode) << 8));
        if (! entry->ent) {
                /* create a new one */
                ent = env->params->create_intrinsic(method, op, imode, omode, env->params->ctx);
@@ -961,14 +979,14 @@ static void lower_Shl(ir_node *node, ir_mode *mode)
         * (and can't handle anything else) */
        if (modulo_shift != get_mode_size_bits(shr_mode)
                        || modulo_shift2<<1 != modulo_shift) {
-               panic("Shr lowering only implemented for modulo shift shr operations");
+               panic("Shl lowering only implemented for modulo shift shl operations");
        }
        if (!is_po2(modulo_shift) || !is_po2(modulo_shift2)) {
-               panic("Shr lowering only implemented for power-of-2 modes");
+               panic("Shl lowering only implemented for power-of-2 modes");
        }
        /* without 2-complement the -x instead of (bit_width-x) trick won't work */
        if (get_mode_arithmetic(shr_mode) != irma_twos_complement) {
-               panic("Shr lowering only implemented for two-complement modes");
+               panic("Shl lowering only implemented for two-complement modes");
        }
 
        /* if the right operand is a 64bit value, we're only interested in the
@@ -1189,6 +1207,23 @@ static void lower_Not(ir_node *node, ir_mode *mode)
        ir_set_dw_lowered(node, res_low, res_high);
 }
 
+static void lower_Proj(ir_node *node, ir_mode *op_mode)
+{
+       ir_mode *mode = get_irn_mode(node);
+       ir_node *pred;
+       (void)op_mode;
+       if (mode != env->high_signed && mode != env->high_unsigned)
+               return;
+       /* skip tuples */
+       pred = get_Proj_pred(node);
+       if (is_Tuple(pred)) {
+               long                   pn    = get_Proj_proj(node);
+               ir_node               *op    = get_irn_n(pred, pn);
+               const lower64_entry_t *entry = get_node_entry(op);
+               ir_set_dw_lowered(node, entry->low_word, entry->high_word);
+       }
+}
+
 static bool is_equality_cmp(const ir_node *node)
 {
        ir_relation relation = get_Cmp_relation(node);
@@ -1219,6 +1254,18 @@ static ir_node *get_cfop_destination(const ir_node *cfop)
        return get_edge_src_irn(first);
 }
 
+static void lower_Switch(ir_node *node, ir_mode *high_mode)
+{
+       ir_node *selector = get_Switch_selector(node);
+       ir_mode *mode     = get_irn_mode(selector);
+       (void)high_mode;
+       if (mode == env->high_signed || mode == env->high_unsigned) {
+               /* we can't really handle Switch with 64bit offsets */
+               panic("Switch with 64bit jumptable not supported");
+       }
+       lower_node(selector);
+}
+
 /**
  * Translate a Cond.
  */
@@ -1226,7 +1273,6 @@ static void lower_Cond(ir_node *node, ir_mode *high_mode)
 {
        ir_node *left, *right, *block;
        ir_node *sel = get_Cond_selector(node);
-       ir_mode *m = get_irn_mode(sel);
        ir_mode *cmp_mode;
        const lower64_entry_t *lentry, *rentry;
        ir_node  *projT = NULL, *projF = NULL;
@@ -1241,15 +1287,6 @@ static void lower_Cond(ir_node *node, ir_mode *high_mode)
 
        (void) high_mode;
 
-       if (m != mode_b) {
-               if (m == env->high_signed || m == env->high_unsigned) {
-                       /* bad we can't really handle Switch with 64bit offsets */
-                       panic("Cond with 64bit jumptable not supported");
-               }
-               lower_node(sel);
-               return;
-       }
-
        if (!is_Cmp(sel)) {
                lower_node(sel);
                return;
@@ -1482,8 +1519,13 @@ static void lower_Conv_to_Ll(ir_node *node)
                set_irn_pinned(call, get_irn_pinned(node));
                irn = new_r_Proj(call, mode_T, pn_Call_T_result);
 
-               res_low  = new_r_Proj(irn, low_unsigned, 0);
-               res_high = new_r_Proj(irn, low_signed, 1);
+               if (env->params->little_endian) {
+                       res_low  = new_r_Proj(irn, low_unsigned, 0);
+                       res_high = new_r_Proj(irn, low_signed, 1);
+               } else {
+                       res_low  = new_r_Proj(irn, low_unsigned, 1);
+                       res_high = new_r_Proj(irn, low_signed,   0);
+               }
        }
        ir_set_dw_lowered(node, res_low, res_high);
 }
@@ -1521,8 +1563,13 @@ static void lower_Conv_from_Ll(ir_node *node)
                ir_node *res;
 
                irn   = get_intrinsic_address(mtp, get_irn_op(node), imode, omode);
-               in[0] = entry->low_word;
-               in[1] = entry->high_word;
+               if (env->params->little_endian) {
+                       in[0] = entry->low_word;
+                       in[1] = entry->high_word;
+               } else {
+                       in[0] = entry->high_word;
+                       in[1] = entry->low_word;
+               }
 
                call = new_rd_Call(dbg, block, get_irg_no_mem(irg), irn, 2, in, mtp);
                set_irn_pinned(call, get_irn_pinned(node));
@@ -1620,12 +1667,8 @@ static void lower_Conv(ir_node *node, ir_mode *mode)
        }
 }
 
-static void fix_parameter_entities(ir_graph *irg)
+static void fix_parameter_entities(ir_graph *irg, ir_type *orig_mtp)
 {
-       ir_entity *entity   = get_irg_entity(irg);
-       ir_type   *mtp      = get_entity_type(entity);
-       ir_type   *orig_mtp = get_type_link(mtp);
-
        size_t      orig_n_params      = get_method_n_params(orig_mtp);
        ir_entity **parameter_entities;
 
@@ -1699,6 +1742,9 @@ static ir_type *lower_mtp(ir_type *mtp)
        res = (ir_type*)pmap_get(lowered_type, mtp);
        if (res != NULL)
                return res;
+       if (type_visited(mtp))
+               return mtp;
+       mark_type_visited(mtp);
 
        orig_n_params   = get_method_n_params(mtp);
        orig_n_res      = get_method_n_ress(mtp);
@@ -1799,6 +1845,7 @@ static ir_type *lower_mtp(ir_type *mtp)
        set_higher_type(res, mtp);
        set_type_link(res, mtp);
 
+       mark_type_visited(res);
        pmap_insert(lowered_type, mtp, res);
        return res;
 }
@@ -1894,7 +1941,7 @@ static void lower_Start(ir_node *node, ir_mode *high_mode)
                }
        }
 
-       /* lower method type */
+       /* find args Proj */
        args = NULL;
        foreach_out_edge(node, edge) {
                ir_node *proj = get_edge_src_irn(edge);
@@ -1918,6 +1965,7 @@ static void lower_Start(ir_node *node, ir_mode *high_mode)
                ir_mode *mode_h;
                ir_node *res_low;
                ir_node *res_high;
+               int      old_cse;
                dbg_info *dbg;
 
                if (!is_Proj(proj))
@@ -1935,6 +1983,9 @@ static void lower_Start(ir_node *node, ir_mode *high_mode)
                        continue;
                }
 
+               /* Switch off CSE or we might get an already existing Proj. */
+               old_cse = get_opt_cse();
+               set_opt_cse(0);
                dbg = get_irn_dbg_info(proj);
                if (env->params->little_endian) {
                        res_low  = new_rd_Proj(dbg, pred, mode_l, new_projs[proj_nr]);
@@ -1943,6 +1994,7 @@ static void lower_Start(ir_node *node, ir_mode *high_mode)
                        res_high = new_rd_Proj(dbg, pred, mode_h, new_projs[proj_nr]);
                        res_low  = new_rd_Proj(dbg, pred, mode_l, new_projs[proj_nr] + 1);
                }
+               set_opt_cse(old_cse);
                ir_set_dw_lowered(proj, res_low, res_high);
        }
 }
@@ -2310,6 +2362,413 @@ static void lower_ASM(ir_node *asmn, ir_mode *mode)
        }
 }
 
+/**
+ * Lower the builtin type to its higher part.
+ *
+ * @param mtp  the builtin type to lower
+ *
+ * @return the lowered type
+ */
+static ir_type *lower_Builtin_type_high(ir_type *mtp)
+{
+       ir_type *res;
+       size_t   i;
+       size_t   n_params;
+       size_t   n_results;
+       bool     must_be_lowered;
+
+       res = (ir_type*)pmap_get(lowered_builtin_type_high, mtp);
+       if (res != NULL)
+               return res;
+
+       n_params        = get_method_n_params(mtp);
+       n_results       = get_method_n_ress(mtp);
+       must_be_lowered = false;
+
+       /* check for double word parameter */
+       for (i = n_params; i > 0;) {
+               ir_type *tp = get_method_param_type(mtp, --i);
+
+               if (is_Primitive_type(tp)) {
+                       ir_mode *mode = get_type_mode(tp);
+
+                       if (mode == env->high_signed || mode == env->high_unsigned) {
+                               must_be_lowered = true;
+                               break;
+                       }
+               }
+       }
+
+       if (!must_be_lowered) {
+               set_type_link(mtp, NULL);
+               return mtp;
+       }
+
+       res = new_d_type_method(n_params, n_results, get_type_dbg_info(mtp));
+
+       /* set param types and result types */
+       for (i = 0; i < n_params; ++i) {
+               ir_type *tp = get_method_param_type(mtp, i);
+
+               if (is_Primitive_type(tp)) {
+                       ir_mode *mode = get_type_mode(tp);
+
+                       if (mode == env->high_signed) {
+                               if (env->params->little_endian) {
+                                       set_method_param_type(res, i, tp_u);
+                               } else {
+                                       set_method_param_type(res, i, tp_s);
+                               }
+                       } else if (mode == env->high_unsigned) {
+                               set_method_param_type(res, i, tp_u);
+                       } else {
+                               set_method_param_type(res, i, tp);
+                       }
+               } else {
+                       set_method_param_type(res, i, tp);
+               }
+       }
+       for (i = n_results = 0; i < n_results; ++i) {
+               ir_type *tp = get_method_res_type(mtp, i);
+
+               if (is_Primitive_type(tp)) {
+                       ir_mode *mode = get_type_mode(tp);
+
+                       if (mode == env->high_signed) {
+                               if (env->params->little_endian) {
+                                       set_method_res_type(res, i, tp_u);
+                               } else {
+                                       set_method_res_type(res, i, tp_s);
+                               }
+                       } else if (mode == env->high_unsigned) {
+                               set_method_res_type(res, i, tp_u);
+                       } else {
+                               set_method_res_type(res, i, tp);
+                       }
+               } else {
+                       set_method_res_type(res, i, tp);
+               }
+       }
+
+       set_method_variadicity(res, get_method_variadicity(mtp));
+       set_method_calling_convention(res, get_method_calling_convention(mtp));
+       set_method_additional_properties(res, get_method_additional_properties(mtp));
+
+       pmap_insert(lowered_builtin_type_high, mtp, res);
+       return res;
+}
+
+/**
+ * Lower the builtin type to its lower part.
+ *
+ * @param mtp  the builtin type to lower
+ *
+ * @return the lowered type
+ */
+static ir_type *lower_Builtin_type_low(ir_type *mtp)
+{
+       ir_type *res;
+       size_t   i;
+       size_t   n_params;
+       size_t   n_results;
+       bool     must_be_lowered;
+
+       res = (ir_type*)pmap_get(lowered_builtin_type_low, mtp);
+       if (res != NULL)
+               return res;
+
+       n_params        = get_method_n_params(mtp);
+       n_results       = get_method_n_ress(mtp);
+       must_be_lowered = false;
+
+       /* check for double word parameter */
+       for (i = n_params; i > 0;) {
+               ir_type *tp = get_method_param_type(mtp, --i);
+
+               if (is_Primitive_type(tp)) {
+                       ir_mode *mode = get_type_mode(tp);
+
+                       if (mode == env->high_signed || mode == env->high_unsigned) {
+                               must_be_lowered = true;
+                               break;
+                       }
+               }
+       }
+
+       if (!must_be_lowered) {
+               set_type_link(mtp, NULL);
+               return mtp;
+       }
+
+       res = new_d_type_method(n_params, n_results, get_type_dbg_info(mtp));
+
+       /* set param types and result types */
+       for (i = 0; i < n_params; ++i) {
+               ir_type *tp = get_method_param_type(mtp, i);
+
+               if (is_Primitive_type(tp)) {
+                       ir_mode *mode = get_type_mode(tp);
+
+                       if (mode == env->high_signed) {
+                               if (env->params->little_endian) {
+                                       set_method_param_type(res, i, tp_s);
+                               } else {
+                                       set_method_param_type(res, i, tp_u);
+                               }
+                       } else if (mode == env->high_unsigned) {
+                               set_method_param_type(res, i, tp_u);
+                       } else {
+                               set_method_param_type(res, i, tp);
+                       }
+               } else {
+                       set_method_param_type(res, i, tp);
+               }
+       }
+       for (i = 0; i < n_results; ++i) {
+               ir_type *tp = get_method_res_type(mtp, i);
+
+               if (is_Primitive_type(tp)) {
+                       ir_mode *mode = get_type_mode(tp);
+
+                       if (mode == env->high_signed) {
+                               if (env->params->little_endian) {
+                                       set_method_res_type(res, i, tp_s);
+                               } else {
+                                       set_method_res_type(res, i, tp_u);
+                               }
+                       } else if (mode == env->high_unsigned) {
+                               set_method_res_type(res, i, tp_u);
+                       } else {
+                               set_method_res_type(res, i, tp);
+                       }
+               } else {
+                       set_method_res_type(res, i, tp);
+               }
+       }
+
+       set_method_variadicity(res, get_method_variadicity(mtp));
+       set_method_calling_convention(res, get_method_calling_convention(mtp));
+       set_method_additional_properties(res, get_method_additional_properties(mtp));
+
+       pmap_insert(lowered_builtin_type_low, mtp, res);
+       return res;
+}
+
+/**
+ * lowers a builtin which reduces a 64bit value to a simple summary value
+ * (popcount, ffs, ...)
+ */
+static void lower_reduce_builtin(ir_node *builtin, ir_mode *mode)
+{
+       ir_builtin_kind  kind         = get_Builtin_kind(builtin);
+       ir_node         *operand      = get_Builtin_param(builtin, 0);
+       ir_mode         *operand_mode = get_irn_mode(operand);
+       if (operand_mode != env->high_signed && operand_mode != env->high_unsigned)
+               return;
+
+       {
+       arch_allow_ifconv_func  allow_ifconv      = be_get_backend_param()->allow_ifconv;
+       int                     arity             = get_irn_arity(builtin);
+       dbg_info               *dbgi              = get_irn_dbg_info(builtin);
+       ir_graph               *irg               = get_irn_irg(builtin);
+       ir_type                *type              = get_Builtin_type(builtin);
+       ir_type                *lowered_type_high = lower_Builtin_type_high(type);
+       ir_type                *lowered_type_low  = lower_Builtin_type_low(type);
+       ir_type                *result_type       = get_method_res_type(lowered_type_low, 0);
+       ir_mode                *result_mode       = get_type_mode(result_type);
+       ir_node                *block             = get_nodes_block(builtin);
+       ir_node                *mem               = get_Builtin_mem(builtin);
+       const lower64_entry_t  *entry             = get_node_entry(operand);
+       ir_mode                *high_mode         = get_irn_mode(entry->high_word);
+       ir_node                *in_high[1]        = {entry->high_word};
+       ir_node                *in_low[1]         = {entry->low_word};
+       ir_node                *res;
+
+       assert(is_NoMem(mem));
+       assert(arity == 2);
+
+       switch (kind) {
+       case ir_bk_ffs: {
+               ir_node               *number_of_bits = new_r_Const_long(irg, result_mode, get_mode_size_bits(env->low_unsigned));
+               ir_node               *zero_high      = new_rd_Const(dbgi, irg, get_mode_null(high_mode));
+               ir_node               *zero_unsigned  = new_rd_Const(dbgi, irg, get_mode_null(env->low_unsigned));
+               ir_node               *zero_result    = new_rd_Const(dbgi, irg, get_mode_null(result_mode));
+               ir_node               *cmp_low        = new_rd_Cmp(dbgi, block, entry->low_word, zero_unsigned, ir_relation_equal);
+               ir_node               *cmp_high       = new_rd_Cmp(dbgi, block, entry->high_word, zero_high, ir_relation_equal);
+               ir_node               *ffs_high       = new_rd_Builtin(dbgi, block, mem, 1, in_high, kind, lowered_type_high);
+               ir_node               *high_proj      = new_r_Proj(ffs_high, result_mode, pn_Builtin_max+1);
+               ir_node               *high           = new_rd_Add(dbgi, block, high_proj, number_of_bits, result_mode);
+               ir_node               *ffs_low        = new_rd_Builtin(dbgi, block, mem, 1, in_low, kind, lowered_type_low);
+               ir_node               *low            = new_r_Proj(ffs_low, result_mode, pn_Builtin_max+1);
+               ir_node               *mux_high       = new_rd_Mux(dbgi, block, cmp_high, high, zero_result, result_mode);
+
+               if (! allow_ifconv(cmp_high, high, zero_result))
+                       ir_nodeset_insert(&created_mux_nodes, mux_high);
+
+               res = new_rd_Mux(dbgi, block, cmp_low, low, mux_high, result_mode);
+
+               if (! allow_ifconv(cmp_low, low, mux_high))
+                       ir_nodeset_insert(&created_mux_nodes, res);
+               break;
+       }
+       case ir_bk_clz: {
+               ir_node               *zero           = new_rd_Const(dbgi, irg, get_mode_null(high_mode));
+               ir_node               *cmp_high       = new_rd_Cmp(dbgi, block, entry->high_word, zero, ir_relation_equal);
+               ir_node               *clz_high       = new_rd_Builtin(dbgi, block, mem, 1, in_high, kind, lowered_type_high);
+               ir_node               *high           = new_r_Proj(clz_high, result_mode, pn_Builtin_max+1);
+               ir_node               *clz_low        = new_rd_Builtin(dbgi, block, mem, 1, in_low, kind, lowered_type_low);
+               ir_node               *low_proj       = new_r_Proj(clz_low, result_mode, pn_Builtin_max+1);
+               ir_node               *number_of_bits = new_r_Const_long(irg, result_mode, get_mode_size_bits(mode));
+               ir_node               *low            = new_rd_Add(dbgi, block, low_proj, number_of_bits, result_mode);
+
+               res = new_rd_Mux(dbgi, block, cmp_high, high, low, result_mode);
+
+               if (! allow_ifconv(cmp_high, high, low))
+                       ir_nodeset_insert(&created_mux_nodes, res);
+               break;
+       }
+       case ir_bk_ctz: {
+               ir_node               *zero_unsigned  = new_rd_Const(dbgi, irg, get_mode_null(env->low_unsigned));
+               ir_node               *cmp_low        = new_rd_Cmp(dbgi, block, entry->low_word, zero_unsigned, ir_relation_equal);
+               ir_node               *ffs_high       = new_rd_Builtin(dbgi, block, mem, 1, in_high, kind, lowered_type_high);
+               ir_node               *high_proj      = new_r_Proj(ffs_high, result_mode, pn_Builtin_max+1);
+               ir_node               *number_of_bits = new_r_Const_long(irg, result_mode, get_mode_size_bits(env->low_unsigned));
+               ir_node               *high           = new_rd_Add(dbgi, block, high_proj, number_of_bits, result_mode);
+               ir_node               *ffs_low        = new_rd_Builtin(dbgi, block, mem, 1, in_low, kind, lowered_type_low);
+               ir_node               *low            = new_r_Proj(ffs_low, result_mode, pn_Builtin_max+1);
+
+               res = new_rd_Mux(dbgi, block, cmp_low, low, high, result_mode);
+
+               if (! allow_ifconv(cmp_low, low, high))
+                       ir_nodeset_insert(&created_mux_nodes, res);
+               break;
+       }
+       case ir_bk_popcount: {
+               ir_node               *popcount_high = new_rd_Builtin(dbgi, block, mem, 1, in_high, kind, lowered_type_high);
+               ir_node               *popcount_low  = new_rd_Builtin(dbgi, block, mem, 1, in_low, kind, lowered_type_low);
+               ir_node               *high          = new_r_Proj(popcount_high, result_mode, pn_Builtin_max+1);
+               ir_node               *low           = new_r_Proj(popcount_low, result_mode, pn_Builtin_max+1);
+
+               res = new_rd_Add(dbgi, block, high, low, result_mode);
+               break;
+       }
+       case ir_bk_parity: {
+               ir_node  *parity_high;
+               ir_node  *parity_low;
+               ir_node  *high;
+               ir_node  *low;
+
+               assert(arity == 2);
+
+               parity_high = new_rd_Builtin(dbgi, block, mem, 1, in_high, kind, lowered_type_high);
+               high        = new_r_Proj(parity_high, result_mode, pn_Builtin_max+1);
+               parity_low  = new_rd_Builtin(dbgi, block, mem, 1, in_low, kind, lowered_type_low);
+               low         = new_r_Proj(parity_low, result_mode, pn_Builtin_max+1);
+               res         = new_rd_Eor(dbgi, block, high, low, result_mode);
+               break;
+       }
+       default:
+               panic("unexpected builtin");
+       }
+
+       turn_into_tuple(builtin, 2);
+       set_irn_n(builtin, pn_Builtin_M, mem);
+       set_irn_n(builtin, pn_Builtin_max+1, res);
+       }
+}
+
+/**
+ * lowers builtins performing arithmetic (bswap)
+ */
+static void lower_arithmetic_builtin(ir_node *builtin, ir_mode *mode)
+{
+       ir_builtin_kind  kind         = get_Builtin_kind(builtin);
+       ir_node         *operand      = get_Builtin_param(builtin, 0);
+       ir_mode         *operand_mode = get_irn_mode(operand);
+       (void) mode;
+       if (operand_mode != env->high_signed && operand_mode != env->high_unsigned)
+               return;
+
+       {
+       dbg_info              *dbgi              = get_irn_dbg_info(builtin);
+       ir_type               *type              = get_Builtin_type(builtin);
+       ir_type               *lowered_type_high = lower_Builtin_type_high(type);
+       ir_type               *lowered_type_low  = lower_Builtin_type_low(type);
+       ir_node               *block             = get_nodes_block(builtin);
+       ir_node               *mem               = get_Builtin_mem(builtin);
+       const lower64_entry_t *entry             = get_node_entry(operand);
+       ir_mode               *mode_high         = get_irn_mode(entry->high_word);
+       const ir_edge_t       *edge;
+       const ir_edge_t       *next;
+       ir_node               *res_high;
+       ir_node               *res_low;
+
+       switch (kind) {
+       case ir_bk_bswap: {
+               ir_node               *in_high[1] = { entry->high_word };
+               ir_node               *in_low[1]  = { entry->low_word };
+               ir_node               *swap_high  = new_rd_Builtin(dbgi, block, mem, 1, in_high, kind, lowered_type_high);
+               ir_node               *swap_low   = new_rd_Builtin(dbgi, block, mem, 1, in_low, kind, lowered_type_low);
+               ir_node               *high       = new_r_Proj(swap_high, mode_high, pn_Builtin_max+1);
+               ir_node               *low        = new_r_Proj(swap_low, env->low_unsigned, pn_Builtin_max+1);
+               if (mode_high == env->low_signed) {
+                       res_high = new_rd_Conv(dbgi, block, low, env->low_signed);
+                       res_low  = new_rd_Conv(dbgi, block, high, env->low_unsigned);
+               } else {
+                       res_high = low;
+                       res_low  = high;
+               }
+               break;
+       }
+       default:
+               panic("unexpected builtin");
+       }
+
+       /* search result Proj */
+       foreach_out_edge_safe(builtin, edge, next) {
+               ir_node *proj = get_edge_src_irn(edge);
+               if (!is_Proj(proj))
+                       continue;
+
+               if (get_Proj_proj(proj) == pn_Builtin_max+1) {
+                       ir_set_dw_lowered(proj, res_low, res_high);
+               }
+       }
+       }
+}
+
+/**
+ * Lower double word builtins.
+ */
+static void lower_Builtin(ir_node *builtin, ir_mode *mode)
+{
+       ir_builtin_kind kind = get_Builtin_kind(builtin);
+
+       switch (kind) {
+       case ir_bk_trap:
+       case ir_bk_debugbreak:
+       case ir_bk_return_address:
+       case ir_bk_frame_address:
+       case ir_bk_prefetch:
+       case ir_bk_inport:
+       case ir_bk_outport:
+       case ir_bk_inner_trampoline:
+               /* Nothing to do. */
+               return;
+       case ir_bk_bswap:
+               lower_arithmetic_builtin(builtin, mode);
+               return;
+       case ir_bk_ffs:
+       case ir_bk_clz:
+       case ir_bk_ctz:
+       case ir_bk_popcount:
+       case ir_bk_parity:
+               lower_reduce_builtin(builtin, mode);
+               return;
+       }
+       panic("unknown builtin");
+}
+
 /**
  * check for opcodes that must always be lowered.
  */
@@ -2317,6 +2776,7 @@ static bool always_lower(unsigned code)
 {
        switch (code) {
        case iro_ASM:
+       case iro_Builtin:
        case iro_Proj:
        case iro_Start:
        case iro_Call:
@@ -2368,14 +2828,14 @@ static void setup_modes(void)
        unsigned           size_bits           = env->params->doubleword_size;
        ir_mode           *doubleword_signed   = NULL;
        ir_mode           *doubleword_unsigned = NULL;
-       size_t             n_modes             = get_irp_n_modes();
+       size_t             n_modes             = ir_get_n_modes();
        ir_mode_arithmetic arithmetic;
        unsigned           modulo_shift;
        size_t             i;
 
        /* search for doubleword modes... */
        for (i = 0; i < n_modes; ++i) {
-               ir_mode *mode = get_irp_mode(i);
+               ir_mode *mode = ir_get_mode(i);
                if (!mode_is_int(mode))
                        continue;
                if (get_mode_size_bits(mode) != size_bits)
@@ -2429,10 +2889,10 @@ static void setup_modes(void)
        /* produce lowered modes */
        env->high_signed   = doubleword_signed;
        env->high_unsigned = doubleword_unsigned;
-       env->low_signed    = new_ir_mode("WS", irms_int_number, size_bits, 1,
-                                        arithmetic, modulo_shift);
-       env->low_unsigned  = new_ir_mode("WU", irms_int_number, size_bits, 0,
-                                        arithmetic, modulo_shift);
+       env->low_signed    = new_int_mode("WS", arithmetic, size_bits, 1,
+                                         modulo_shift);
+       env->low_unsigned  = new_int_mode("WU", arithmetic, size_bits, 0,
+                                         modulo_shift);
 }
 
 static void enqueue_preds(ir_node *node)
@@ -2546,7 +3006,7 @@ static void lower_irg(ir_graph *irg)
                set_entity_type(ent, lowered_mtp);
                env->flags |= MUST_BE_LOWERED;
 
-               fix_parameter_entities(irg);
+               fix_parameter_entities(irg, mtp);
        }
 
        /* first step: link all nodes and allocate data */
@@ -2580,8 +3040,8 @@ static void lower_irg(ir_graph *irg)
 
                if (env->flags & CF_CHANGED) {
                        /* control flow changed, dominance info is invalid */
-                       clear_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_DOMINANCE);
-                       set_irg_extblk_inconsistent(irg);
+                       clear_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_DOMINANCE
+                                          | IR_GRAPH_STATE_VALID_EXTENDED_BLOCKS);
                }
                edges_deactivate(irg);
        }
@@ -2601,11 +3061,12 @@ void ir_prepare_dw_lowering(const lwrdw_param_t *new_param)
 
        param = new_param;
 
-       clear_irp_opcodes_generic_func();
+       ir_clear_opcodes_generic_func();
        ir_register_dw_lower_function(op_ASM,     lower_ASM);
        ir_register_dw_lower_function(op_Add,     lower_binop);
        ir_register_dw_lower_function(op_And,     lower_And);
        ir_register_dw_lower_function(op_Bad,     lower_Bad);
+       ir_register_dw_lower_function(op_Builtin, lower_Builtin);
        ir_register_dw_lower_function(op_Call,    lower_Call);
        ir_register_dw_lower_function(op_Cmp,     lower_Cmp);
        ir_register_dw_lower_function(op_Cond,    lower_Cond);
@@ -2620,6 +3081,7 @@ void ir_prepare_dw_lowering(const lwrdw_param_t *new_param)
        ir_register_dw_lower_function(op_Mux,     lower_Mux);
        ir_register_dw_lower_function(op_Not,     lower_Not);
        ir_register_dw_lower_function(op_Or,      lower_Or);
+       ir_register_dw_lower_function(op_Proj,    lower_Proj);
        ir_register_dw_lower_function(op_Return,  lower_Return);
        ir_register_dw_lower_function(op_Shl,     lower_Shl);
        ir_register_dw_lower_function(op_Shr,     lower_Shr);
@@ -2627,9 +3089,18 @@ void ir_prepare_dw_lowering(const lwrdw_param_t *new_param)
        ir_register_dw_lower_function(op_Start,   lower_Start);
        ir_register_dw_lower_function(op_Store,   lower_Store);
        ir_register_dw_lower_function(op_Sub,     lower_binop);
+       ir_register_dw_lower_function(op_Switch,  lower_Switch);
        ir_register_dw_lower_function(op_Unknown, lower_Unknown);
 }
 
+/**
+ * Callback to lower only the Mux nodes we created.
+ */
+static int lower_mux_cb(ir_node *mux)
+{
+       return ir_nodeset_contains(&created_mux_nodes, mux);
+}
+
 /*
  * Do the lowering.
  */
@@ -2651,6 +3122,10 @@ void ir_lower_dw_ops(void)
                conv_types = new_set(cmp_conv_tp, 16);
        if (! lowered_type)
                lowered_type = pmap_create();
+       if (! lowered_builtin_type_low)
+               lowered_builtin_type_low = pmap_create();
+       if (! lowered_builtin_type_high)
+               lowered_builtin_type_high = pmap_create();
 
        /* create a primitive unsigned and signed type */
        if (! tp_u)
@@ -2714,13 +3189,22 @@ void ir_lower_dw_ops(void)
        lenv.first_id      = new_id_from_chars(param->little_endian ? ".l" : ".h", 2);
        lenv.next_id       = new_id_from_chars(param->little_endian ? ".h" : ".l", 2);
 
-       irp_reserve_resources(irp, IRP_RESOURCE_TYPE_LINK);
+       irp_reserve_resources(irp, IRP_RESOURCE_TYPE_LINK | IRP_RESOURCE_TYPE_VISITED);
+       inc_master_type_visited();
        /* transform all graphs */
        for (i = 0, n = get_irp_n_irgs(); i < n; ++i) {
                ir_graph *irg = get_irp_irg(i);
+
+               ir_nodeset_init(&created_mux_nodes);
+
                lower_irg(irg);
+
+               if (ir_nodeset_size(&created_mux_nodes) > 0)
+                       lower_mux(irg, lower_mux_cb);
+
+               ir_nodeset_destroy(&created_mux_nodes);
        }
-       irp_free_resources(irp, IRP_RESOURCE_TYPE_LINK);
+       irp_free_resources(irp, IRP_RESOURCE_TYPE_LINK | IRP_RESOURCE_TYPE_VISITED);
        del_pdeq(lenv.waitq);
 
        env = NULL;