besched: Add and use sched_replace().
[libfirm] / ir / be / ia32 / ia32_address_mode.c
index ce63d98..6d72e33 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
+ * Copyright (C) 1995-2010 University of Karlsruhe.  All right reserved.
  *
  * This file is part of libFirm.
  *
@@ -22,7 +22,6 @@
  * @brief       This file contains functions for matching firm graphs for
  *              nodes that can be used as address mode for x86 instructions
  * @author      Matthias Braun
- * @version     $Id$
  */
 #include "config.h"
 
@@ -36,8 +35,8 @@
 #include "iredges_t.h"
 #include "irgwalk.h"
 
-#include "../benode.h"
-#include "../belive.h"
+#include "benode.h"
+#include "belive.h"
 
 #define AGGRESSIVE_AM
 
@@ -55,7 +54,7 @@ static bitset_t *non_address_mode_nodes;
  *
  * @return non-zero if the DAG represents an immediate, 0 else
  */
-static int do_is_immediate(const ir_node *node, int *symconsts, int negate)
+static bool do_is_immediate(const ir_node *node, int *symconsts, bool negate)
 {
        ir_node *left;
        ir_node *right;
@@ -67,61 +66,48 @@ static int do_is_immediate(const ir_node *node, int *symconsts, int negate)
 #ifdef DEBUG_libfirm
                        ir_fprintf(stderr,
                                   "Optimisation warning tarval of %+F(%+F) is not a long.\n",
-                                  node, current_ir_graph);
+                                  node, get_irn_irg(node));
 #endif
-                       return 0;
+                       return false;
                }
-               return 1;
+               return true;
        case iro_SymConst:
                /* the first SymConst of a DAG can be fold into an immediate */
 #ifndef SUPPORT_NEGATIVE_SYMCONSTS
                /* unfortunately the assembler/linker doesn't support -symconst */
                if (negate)
-                       return 0;
+                       return false;
 #endif
-
                if (get_SymConst_kind(node) != symconst_addr_ent)
-                       return 0;
-               (*symconsts)++;
-               if (*symconsts > 1)
-                       return 0;
-
-               return 1;
+                       return false;
+               if (++*symconsts > 1)
+                       return false;
+
+               return true;
+       case iro_Unknown:
+               /* we can use '0' for Unknowns */
+               return true;
        case iro_Add:
        case iro_Sub:
                /* Add's and Sub's are typically supported as long as both operands are
                 * immediates */
                if (ia32_is_non_address_mode_node(node))
-                       return 0;
+                       return false;
 
-               left  = get_binop_left(node);
-               right = get_binop_right(node);
+               left = get_binop_left(node);
                if (!do_is_immediate(left, symconsts, negate))
-                       return 0;
+                       return false;
+               right = get_binop_right(node);
                if (!do_is_immediate(right, symconsts, is_Sub(node) ? !negate : negate))
-                       return 0;
+                       return false;
 
-               return 1;
+               return true;
        default:
                /* all other nodes are NO immediates */
-               return 0;
+               return false;
        }
 }
 
-/**
- * Checks if a DAG with a single root node can be represented as a simple immediate.
- *
- * @param node  the node
- *
- * @return non-zero if the DAG represents an immediate, 0 else
- */
-#if 0
-static int is_immediate_simple(const ir_node *node) {
-       int symconsts = 0;
-       return do_is_immediate(node, &symconsts, 0);
-}
-#endif
-
 /**
  * Check if a DAG starting with root node can be folded into an address mode
  * as an immediate.
@@ -130,7 +116,7 @@ static int is_immediate_simple(const ir_node *node) {
  * @param node    the node
  * @param negate  if set, the immediate must be negated
  */
-static int is_immediate(ia32_address_t *addr, const ir_node *node, int negate)
+static int is_immediate(ia32_address_t *addr, const ir_node *node, bool negate)
 {
        int symconsts = (addr->symconst_ent != NULL);
        return do_is_immediate(node, &symconsts, negate);
@@ -143,12 +129,12 @@ static int is_immediate(ia32_address_t *addr, const ir_node *node, int negate)
  * @param node    the node
  * @param negate  if set, the immediate must be negated
  */
-static void eat_immediate(ia32_address_t *addr, ir_node *node, int negate)
+static void eat_immediate(ia32_address_t *addr, ir_node *node, bool negate)
 {
-       tarval  *tv;
-       ir_node *left;
-       ir_node *right;
-       long    val;
+       ir_tarval *tv;
+       ir_node   *left;
+       ir_node   *right;
+       long      val;
 
        switch (get_irn_opcode(node)) {
        case iro_Const:
@@ -167,23 +153,27 @@ static void eat_immediate(ia32_address_t *addr, ir_node *node, int negate)
                        panic("Internal error: more than 1 symconst in address calculation");
                }
                addr->symconst_ent  = get_SymConst_entity(node);
+               if (get_entity_owner(addr->symconst_ent) == get_tls_type())
+                       addr->tls_segment = true;
 #ifndef SUPPORT_NEGATIVE_SYMCONSTS
                assert(!negate);
 #endif
                addr->symconst_sign = negate;
                break;
+       case iro_Unknown:
+               break;
        case iro_Add:
                assert(!ia32_is_non_address_mode_node(node));
                left  = get_Add_left(node);
-               right = get_Add_right(node);
                eat_immediate(addr, left, negate);
+               right = get_Add_right(node);
                eat_immediate(addr, right, negate);
                break;
        case iro_Sub:
                assert(!ia32_is_non_address_mode_node(node));
                left  = get_Sub_left(node);
-               right = get_Sub_right(node);
                eat_immediate(addr, left, negate);
+               right = get_Sub_right(node);
                eat_immediate(addr, right, !negate);
                break;
        default:
@@ -214,11 +204,11 @@ static ir_node *eat_immediates(ia32_address_t *addr, ir_node *node,
 
                if (is_immediate(addr, left, 0)) {
                        eat_immediate(addr, left, 0);
-                       return eat_immediates(addr, right, 0);
+                       return eat_immediates(addr, right, ia32_create_am_normal);
                }
                if (is_immediate(addr, right, 0)) {
                        eat_immediate(addr, right, 0);
-                       return eat_immediates(addr, left, 0);
+                       return eat_immediates(addr, left, ia32_create_am_normal);
                }
        } else if (is_Sub(node)) {
                ir_node *left  = get_Sub_left(node);
@@ -226,7 +216,7 @@ static ir_node *eat_immediates(ia32_address_t *addr, ir_node *node,
 
                if (is_immediate(addr, right, 1)) {
                        eat_immediate(addr, right, 1);
-                       return eat_immediates(addr, left, 0);
+                       return eat_immediates(addr, left, ia32_create_am_normal);
                }
        }
 
@@ -247,8 +237,8 @@ static int eat_shl(ia32_address_t *addr, ir_node *node)
        long     val;
 
        if (is_Shl(node)) {
-               ir_node *right = get_Shl_right(node);
-               tarval  *tv;
+               ir_node   *right = get_Shl_right(node);
+               ir_tarval *tv;
 
                /* we can use shl with 1, 2 or 3 shift */
                if (!is_Const(right))
@@ -300,7 +290,6 @@ static int eat_shl(ia32_address_t *addr, ir_node *node)
 /* Create an address mode for a given node. */
 void ia32_create_address_mode(ia32_address_t *addr, ir_node *node, ia32_create_am_flags_t flags)
 {
-       int      res = 0;
        ir_node *eat_imms;
 
        if (is_immediate(addr, node, 0)) {
@@ -328,7 +317,6 @@ void ia32_create_address_mode(ia32_address_t *addr, ir_node *node, ia32_create_a
                        eat_imms = ia32_skip_downconv(eat_imms);
                }
 
-               res  = 1;
                node = eat_imms;
 #ifndef AGGRESSIVE_AM
                if (get_irn_n_edges(node) > 1) {
@@ -428,10 +416,13 @@ int ia32_is_non_address_mode_node(ir_node const *node)
        return bitset_is_set(non_address_mode_nodes, get_irn_idx(node));
 }
 
+/**
+ * Check if a given value is last used (i.e. die after) the block of some
+ * other node.
+ */
 static int value_last_used_here(be_lv_t *lv, ir_node *here, ir_node *value)
 {
-       ir_node         *block = get_nodes_block(here);
-       const ir_edge_t *edge;
+       ir_node *block = get_nodes_block(here);
 
        /* If the value is live end it is for sure it does not die here */
        if (be_is_live_end(lv, block, value)) return 0;
@@ -449,13 +440,19 @@ static int value_last_used_here(be_lv_t *lv, ir_node *here, ir_node *value)
        return 1;
 }
 
+static bool simple_is_immediate(const ir_node *node)
+{
+       int symconsts = 0;
+       return do_is_immediate(node, &symconsts, false);
+}
+
 /**
  * Walker: mark those nodes that cannot be part of an address mode because
  * their value must be accessed through a register
  */
 static void mark_non_address_nodes(ir_node *node, void *env)
 {
-       be_lv_t *lv = env;
+       be_lv_t *lv = (be_lv_t*)env;
        int      arity;
        int      i;
        ir_node *val;
@@ -494,12 +491,17 @@ static void mark_non_address_nodes(ir_node *node, void *env)
                left  = get_binop_left(node);
                right = get_binop_right(node);
 
-               /* Fold AM if any of the two operands does not die here.  This duplicates
+               /* if any of the operands is an immediate then this will not
+                * increase register pressure */
+               if (simple_is_immediate(left) || simple_is_immediate(right))
+                       return;
+
+               /* Fold AM if any of the two operands does not die here. This duplicates
                 * an addition and has the same register pressure for the case that only
                 * one operand dies, but is faster (on Pentium 4).
                 * && instead of || only folds AM if both operands do not die here */
                if (!value_last_used_here(lv, node, left) ||
-                               !value_last_used_here(lv, node, right)) {
+                   !value_last_used_here(lv, node, right)) {
                        return;
                }
 
@@ -520,10 +522,12 @@ static void mark_non_address_nodes(ir_node *node, void *env)
        }
 }
 
-void ia32_calculate_non_address_mode_nodes(be_irg_t *birg)
+void ia32_calculate_non_address_mode_nodes(ir_graph *irg)
 {
-       ir_graph *irg = be_get_birg_irg(birg);
-       be_lv_t  *lv  = be_assure_liveness(birg);
+       be_lv_t *lv;
+
+       be_assure_live_chk(irg);
+       lv = be_get_irg_liveness(irg);
 
        non_address_mode_nodes = bitset_malloc(get_irg_last_idx(irg));