Put opening curly brace of functions on a separate line.
[libfirm] / ir / opt / ldstopt.c
index c1f214c..ac7137e 100644 (file)
@@ -23,9 +23,7 @@
  * @author  Michael Beck
  * @version $Id$
  */
-#ifdef HAVE_CONFIG_H
-# include "config.h"
-#endif
+#include "config.h"
 
 #include <string.h>
 
 #include "dbginfo_t.h"
 #include "iropt_dbg.h"
 #include "irflag_t.h"
-#include "array.h"
+#include "array_t.h"
 #include "irhooks.h"
 #include "iredges.h"
-#include "irtools.h"
+#include "irpass.h"
 #include "opt_polymorphy.h"
 #include "irmemory.h"
-#include "xmalloc.h"
 #include "irphase_t.h"
 #include "irgopt.h"
+#include "set.h"
 #include "debug.h"
 
 /** The debug handle. */
@@ -111,12 +109,12 @@ static unsigned master_visited = 0;
 /**
  * get the Load/Store info of a node
  */
-static ldst_info_t *get_ldst_info(ir_node *node, struct obstack *obst) {
+static ldst_info_t *get_ldst_info(ir_node *node, struct obstack *obst)
+{
        ldst_info_t *info = get_irn_link(node);
 
        if (! info) {
-               info = obstack_alloc(obst, sizeof(*info));
-               memset(info, 0, sizeof(*info));
+               info = OALLOCZ(obst, ldst_info_t);
                set_irn_link(node, info);
        }
        return info;
@@ -125,12 +123,12 @@ static ldst_info_t *get_ldst_info(ir_node *node, struct obstack *obst) {
 /**
  * get the Block info of a node
  */
-static block_info_t *get_block_info(ir_node *node, struct obstack *obst) {
+static block_info_t *get_block_info(ir_node *node, struct obstack *obst)
+{
        block_info_t *info = get_irn_link(node);
 
        if (! info) {
-               info = obstack_alloc(obst, sizeof(*info));
-               memset(info, 0, sizeof(*info));
+               info = OALLOCZ(obst, block_info_t);
                set_irn_link(node, info);
        }
        return info;
@@ -256,14 +254,9 @@ static void collect_nodes(ir_node *node, void *env)
 static ir_entity *find_constant_entity(ir_node *ptr)
 {
        for (;;) {
-               ir_op *op = get_irn_op(ptr);
-
-               if (op == op_SymConst && (get_SymConst_kind(ptr) == symconst_addr_ent)) {
-                       ir_entity *ent = get_SymConst_entity(ptr);
-                       if (variability_constant == get_entity_variability(ent))
-                               return ent;
-                       return NULL;
-               } else if (op == op_Sel) {
+               if (is_SymConst(ptr) && get_SymConst_kind(ptr) == symconst_addr_ent) {
+                       return get_SymConst_entity(ptr);
+               } else if (is_Sel(ptr)) {
                        ir_entity *ent = get_Sel_entity(ptr);
                        ir_type   *tp  = get_entity_owner(ent);
 
@@ -304,11 +297,36 @@ static ir_entity *find_constant_entity(ir_node *ptr)
                                }
                        }
 
-                       if (variability_constant == get_entity_variability(ent))
+                       if (get_entity_linkage(ent) & IR_LINKAGE_CONSTANT)
                                return ent;
 
                        /* try next */
                        ptr = get_Sel_ptr(ptr);
+               } else if (is_Add(ptr)) {
+                       ir_node *l = get_Add_left(ptr);
+                       ir_node *r = get_Add_right(ptr);
+
+                       if (get_irn_mode(l) == get_irn_mode(ptr) && is_Const(r))
+                               ptr = l;
+                       else if (get_irn_mode(r) == get_irn_mode(ptr) && is_Const(l))
+                               ptr = r;
+                       else
+                               return NULL;
+
+                       /* for now, we support only one addition, reassoc should fold all others */
+                       if (! is_SymConst(ptr) && !is_Sel(ptr))
+                               return NULL;
+               } else if (is_Sub(ptr)) {
+                       ir_node *l = get_Sub_left(ptr);
+                       ir_node *r = get_Sub_right(ptr);
+
+                       if (get_irn_mode(l) == get_irn_mode(ptr) &&     is_Const(r))
+                               ptr = l;
+                       else
+                               return NULL;
+                       /* for now, we support only one substraction, reassoc should fold all others */
+                       if (! is_SymConst(ptr) && !is_Sel(ptr))
+                               return NULL;
                } else
                        return NULL;
        }
@@ -317,7 +335,8 @@ static ir_entity *find_constant_entity(ir_node *ptr)
 /**
  * Return the Selection index of a Sel node from dimension n
  */
-static long get_Sel_array_index_long(ir_node *n, int dim) {
+static long get_Sel_array_index_long(ir_node *n, int dim)
+{
        ir_node *index = get_Sel_index(n, dim);
        assert(is_Const(index));
        return get_tarval_long(get_Const_tarval(index));
@@ -331,23 +350,27 @@ static long get_Sel_array_index_long(ir_node *n, int dim) {
  * @param depth  current depth in steps upward from the root
  *               of the address
  */
-static compound_graph_path *rec_get_accessed_path(ir_node *ptr, int depth) {
+static compound_graph_path *rec_get_accessed_path(ir_node *ptr, int depth)
+{
        compound_graph_path *res = NULL;
-       ir_entity           *root, *field;
-       int                 path_len, pos;
+       ir_entity           *root, *field, *ent;
+       int                 path_len, pos, idx;
+       tarval              *tv;
+       ir_type             *tp;
 
-       if (get_irn_op(ptr) == op_SymConst) {
+       if (is_SymConst(ptr)) {
                /* a SymConst. If the depth is 0, this is an access to a global
                 * entity and we don't need a component path, else we know
-                * at least it's length.
+                * at least its length.
                 */
                assert(get_SymConst_kind(ptr) == symconst_addr_ent);
                root = get_SymConst_entity(ptr);
                res = (depth == 0) ? NULL : new_compound_graph_path(get_entity_type(root), depth);
-       } else {
-               assert(get_irn_op(ptr) == op_Sel);
+       } else if (is_Sel(ptr)) {
                /* it's a Sel, go up until we find the root */
                res = rec_get_accessed_path(get_Sel_ptr(ptr), depth+1);
+               if (res == NULL)
+                       return NULL;
 
                /* fill up the step in the path at the current position */
                field    = get_Sel_entity(ptr);
@@ -359,24 +382,322 @@ static compound_graph_path *rec_get_accessed_path(ir_node *ptr, int depth) {
                        assert(get_Sel_n_indexs(ptr) == 1 && "multi dim arrays not implemented");
                        set_compound_graph_path_array_index(res, pos, get_Sel_array_index_long(ptr, 0));
                }
+       } else if (is_Add(ptr)) {
+               ir_node *l    = get_Add_left(ptr);
+               ir_node *r    = get_Add_right(ptr);
+               ir_mode *mode = get_irn_mode(ptr);
+               tarval  *tmp;
+
+               if (is_Const(r) && get_irn_mode(l) == mode) {
+                       ptr = l;
+                       tv  = get_Const_tarval(r);
+               } else {
+                       ptr = r;
+                       tv  = get_Const_tarval(l);
+               }
+ptr_arith:
+               mode = get_tarval_mode(tv);
+               tmp  = tv;
+
+               /* ptr must be a Sel or a SymConst, this was checked in find_constant_entity() */
+               if (is_Sel(ptr)) {
+                       field = get_Sel_entity(ptr);
+               } else {
+                       field = get_SymConst_entity(ptr);
+               }
+               idx = 0;
+               for (ent = field;;) {
+                       unsigned size;
+                       tarval   *sz, *tv_index, *tlower, *tupper;
+                       ir_node  *bound;
+
+                       tp = get_entity_type(ent);
+                       if (! is_Array_type(tp))
+                               break;
+                       ent = get_array_element_entity(tp);
+                       size = get_type_size_bytes(get_entity_type(ent));
+                       sz   = new_tarval_from_long(size, mode);
+
+                       tv_index = tarval_div(tmp, sz);
+                       tmp      = tarval_mod(tmp, sz);
+
+                       if (tv_index == tarval_bad || tmp == tarval_bad)
+                               return NULL;
+
+                       assert(get_array_n_dimensions(tp) == 1 && "multiarrays not implemented");
+                       bound  = get_array_lower_bound(tp, 0);
+                       tlower = computed_value(bound);
+                       bound  = get_array_upper_bound(tp, 0);
+                       tupper = computed_value(bound);
+
+                       if (tlower == tarval_bad || tupper == tarval_bad)
+                               return NULL;
+
+                       if (tarval_cmp(tv_index, tlower) & pn_Cmp_Lt)
+                               return NULL;
+                       if (tarval_cmp(tupper, tv_index) & pn_Cmp_Lt)
+                               return NULL;
+
+                       /* ok, bounds check finished */
+                       ++idx;
+               }
+               if (! tarval_is_null(tmp)) {
+                       /* access to some struct/union member */
+                       return NULL;
+               }
+
+               /* should be at least ONE array */
+               if (idx == 0)
+                       return NULL;
+
+               res = rec_get_accessed_path(ptr, depth + idx);
+               if (res == NULL)
+                       return NULL;
+
+               path_len = get_compound_graph_path_length(res);
+               pos      = path_len - depth - idx;
+
+               for (ent = field;;) {
+                       unsigned size;
+                       tarval   *sz, *tv_index;
+                       long     index;
+
+                       tp = get_entity_type(ent);
+                       if (! is_Array_type(tp))
+                               break;
+                       ent = get_array_element_entity(tp);
+                       set_compound_graph_path_node(res, pos, ent);
+
+                       size = get_type_size_bytes(get_entity_type(ent));
+                       sz   = new_tarval_from_long(size, mode);
+
+                       tv_index = tarval_div(tv, sz);
+                       tv       = tarval_mod(tv, sz);
+
+                       /* worked above, should work again */
+                       assert(tv_index != tarval_bad && tv != tarval_bad);
+
+                       /* bounds already checked above */
+                       index = get_tarval_long(tv_index);
+                       set_compound_graph_path_array_index(res, pos, index);
+                       ++pos;
+               }
+       } else if (is_Sub(ptr)) {
+               ir_node *l = get_Sub_left(ptr);
+               ir_node *r = get_Sub_right(ptr);
+
+               ptr = l;
+               tv  = get_Const_tarval(r);
+               tv  = tarval_neg(tv);
+               goto ptr_arith;
        }
        return res;
 }  /* rec_get_accessed_path */
 
-/** Returns an access path or NULL.  The access path is only
- *  valid, if the graph is in phase_high and _no_ address computation is used.
+/**
+ * Returns an access path or NULL.  The access path is only
+ * valid, if the graph is in phase_high and _no_ address computation is used.
  */
-static compound_graph_path *get_accessed_path(ir_node *ptr) {
-       return rec_get_accessed_path(ptr, 0);
+static compound_graph_path *get_accessed_path(ir_node *ptr)
+{
+       compound_graph_path *gr = rec_get_accessed_path(ptr, 0);
+       return gr;
 }  /* get_accessed_path */
 
+typedef struct path_entry {
+       ir_entity         *ent;
+       struct path_entry *next;
+       long              index;
+} path_entry;
+
+static ir_node *rec_find_compound_ent_value(ir_node *ptr, path_entry *next)
+{
+       path_entry       entry, *p;
+       ir_entity        *ent, *field;
+       ir_initializer_t *initializer;
+       tarval           *tv;
+       ir_type          *tp;
+       unsigned         n;
+
+       entry.next = next;
+       if (is_SymConst(ptr)) {
+               /* found the root */
+               ent         = get_SymConst_entity(ptr);
+               initializer = get_entity_initializer(ent);
+               for (p = next; p != NULL;) {
+                       if (initializer->kind != IR_INITIALIZER_COMPOUND)
+                               return NULL;
+                       n  = get_initializer_compound_n_entries(initializer);
+                       tp = get_entity_type(ent);
+
+                       if (is_Array_type(tp)) {
+                               ent = get_array_element_entity(tp);
+                               if (ent != p->ent) {
+                                       /* a missing [0] */
+                                       if (0 >= n)
+                                               return NULL;
+                                       initializer = get_initializer_compound_value(initializer, 0);
+                                       continue;
+                               }
+                       }
+                       if (p->index >= (int) n)
+                               return NULL;
+                       initializer = get_initializer_compound_value(initializer, p->index);
+
+                       ent = p->ent;
+                       p   = p->next;
+               }
+               tp = get_entity_type(ent);
+               while (is_Array_type(tp)) {
+                       ent = get_array_element_entity(tp);
+                       tp = get_entity_type(ent);
+                       /* a missing [0] */
+                       n  = get_initializer_compound_n_entries(initializer);
+                       if (0 >= n)
+                               return NULL;
+                       initializer = get_initializer_compound_value(initializer, 0);
+               }
+
+               switch (initializer->kind) {
+               case IR_INITIALIZER_CONST:
+                       return get_initializer_const_value(initializer);
+               case IR_INITIALIZER_TARVAL:
+               case IR_INITIALIZER_NULL:
+               default:
+                       return NULL;
+               }
+       } else if (is_Sel(ptr)) {
+               entry.ent = field = get_Sel_entity(ptr);
+               tp = get_entity_owner(field);
+               if (is_Array_type(tp)) {
+                       assert(get_Sel_n_indexs(ptr) == 1 && "multi dim arrays not implemented");
+                       entry.index = get_Sel_array_index_long(ptr, 0) - get_array_lower_bound_int(tp, 0);
+               } else {
+                       int i, n_members = get_compound_n_members(tp);
+                       for (i = 0; i < n_members; ++i) {
+                               if (get_compound_member(tp, i) == field)
+                                       break;
+                       }
+                       if (i >= n_members) {
+                               /* not found: should NOT happen */
+                               return NULL;
+                       }
+                       entry.index = i;
+               }
+               return rec_find_compound_ent_value(get_Sel_ptr(ptr), &entry);
+       }  else if (is_Add(ptr)) {
+               ir_node  *l = get_Add_left(ptr);
+               ir_node  *r = get_Add_right(ptr);
+               ir_mode  *mode;
+               unsigned pos;
+
+               if (is_Const(r)) {
+                       ptr = l;
+                       tv  = get_Const_tarval(r);
+               } else {
+                       ptr = r;
+                       tv  = get_Const_tarval(l);
+               }
+ptr_arith:
+               mode = get_tarval_mode(tv);
+
+               /* ptr must be a Sel or a SymConst, this was checked in find_constant_entity() */
+               if (is_Sel(ptr)) {
+                       field = get_Sel_entity(ptr);
+               } else {
+                       field = get_SymConst_entity(ptr);
+               }
+
+               /* count needed entries */
+               pos = 0;
+               for (ent = field;;) {
+                       tp = get_entity_type(ent);
+                       if (! is_Array_type(tp))
+                               break;
+                       ent = get_array_element_entity(tp);
+                       ++pos;
+               }
+               /* should be at least ONE entry */
+               if (pos == 0)
+                       return NULL;
+
+               /* allocate the right number of entries */
+               NEW_ARR_A(path_entry, p, pos);
+
+               /* fill them up */
+               pos = 0;
+               for (ent = field;;) {
+                       unsigned size;
+                       tarval   *sz, *tv_index, *tlower, *tupper;
+                       long     index;
+                       ir_node  *bound;
+
+                       tp = get_entity_type(ent);
+                       if (! is_Array_type(tp))
+                               break;
+                       ent = get_array_element_entity(tp);
+                       p[pos].ent  = ent;
+                       p[pos].next = &p[pos + 1];
+
+                       size = get_type_size_bytes(get_entity_type(ent));
+                       sz   = new_tarval_from_long(size, mode);
+
+                       tv_index = tarval_div(tv, sz);
+                       tv       = tarval_mod(tv, sz);
+
+                       if (tv_index == tarval_bad || tv == tarval_bad)
+                               return NULL;
+
+                       assert(get_array_n_dimensions(tp) == 1 && "multiarrays not implemented");
+                       bound  = get_array_lower_bound(tp, 0);
+                       tlower = computed_value(bound);
+                       bound  = get_array_upper_bound(tp, 0);
+                       tupper = computed_value(bound);
+
+                       if (tlower == tarval_bad || tupper == tarval_bad)
+                               return NULL;
+
+                       if (tarval_cmp(tv_index, tlower) & pn_Cmp_Lt)
+                               return NULL;
+                       if (tarval_cmp(tupper, tv_index) & pn_Cmp_Lt)
+                               return NULL;
+
+                       /* ok, bounds check finished */
+                       index = get_tarval_long(tv_index);
+                       p[pos].index = index;
+                       ++pos;
+               }
+               if (! tarval_is_null(tv)) {
+                       /* hmm, wrong access */
+                       return NULL;
+               }
+               p[pos - 1].next = next;
+               return rec_find_compound_ent_value(ptr, p);
+       } else if (is_Sub(ptr)) {
+               ir_node *l = get_Sub_left(ptr);
+               ir_node *r = get_Sub_right(ptr);
+
+               ptr = l;
+               tv  = get_Const_tarval(r);
+               tv  = tarval_neg(tv);
+               goto ptr_arith;
+       }
+       return NULL;
+}
+
+static ir_node *find_compound_ent_value(ir_node *ptr)
+{
+       return rec_find_compound_ent_value(ptr, NULL);
+}
+
 /* forward */
 static void reduce_adr_usage(ir_node *ptr);
 
 /**
- * Update a Load that may lost it's usage.
+ * Update a Load that may have lost its users.
  */
-static void handle_load_update(ir_node *load) {
+static void handle_load_update(ir_node *load)
+{
        ldst_info_t *info = get_irn_link(load);
 
        /* do NOT touch volatile loads for now */
@@ -387,20 +708,21 @@ static void handle_load_update(ir_node *load) {
                ir_node *ptr = get_Load_ptr(load);
                ir_node *mem = get_Load_mem(load);
 
-               /* a Load which value is neither used nor exception checked, remove it */
+               /* a Load whose value is neither used nor exception checked, remove it */
                exchange(info->projs[pn_Load_M], mem);
                if (info->projs[pn_Load_X_regular])
-                       exchange(info->projs[pn_Load_X_regular], new_r_Jmp(current_ir_graph, get_nodes_block(load)));
+                       exchange(info->projs[pn_Load_X_regular], new_r_Jmp(get_nodes_block(load)));
                kill_node(load);
                reduce_adr_usage(ptr);
        }
 }  /* handle_load_update */
 
 /**
- * A Use of an address node is vanished. Check if this was a Proj
+ * A use of an address node has vanished. Check if this was a Proj
  * node and update the counters.
  */
-static void reduce_adr_usage(ir_node *ptr) {
+static void reduce_adr_usage(ir_node *ptr)
+{
        if (is_Proj(ptr)) {
                if (get_irn_n_edges(ptr) <= 0) {
                        /* this Proj is dead now */
@@ -410,7 +732,7 @@ static void reduce_adr_usage(ir_node *ptr) {
                                ldst_info_t *info = get_irn_link(pred);
                                info->projs[get_Proj_proj(ptr)] = NULL;
 
-                               /* this node lost it's result proj, handle that */
+                               /* this node lost its result proj, handle that */
                                handle_load_update(pred);
                        }
                }
@@ -421,7 +743,8 @@ static void reduce_adr_usage(ir_node *ptr) {
  * Check, if an already existing value of mode old_mode can be converted
  * into the needed one new_mode without loss.
  */
-static int can_use_stored_value(ir_mode *old_mode, ir_mode *new_mode) {
+static int can_use_stored_value(ir_mode *old_mode, ir_mode *new_mode)
+{
        if (old_mode == new_mode)
                return 1;
 
@@ -437,7 +760,8 @@ static int can_use_stored_value(ir_mode *old_mode, ir_mode *new_mode) {
 /**
  * Check whether a Call is at least pure, ie. does only read memory.
  */
-static unsigned is_Call_pure(ir_node *call) {
+static unsigned is_Call_pure(ir_node *call)
+{
        ir_type *call_tp = get_Call_type(call);
        unsigned prop = get_method_additional_properties(call_tp);
 
@@ -455,6 +779,143 @@ static unsigned is_Call_pure(ir_node *call) {
        return (prop & (mtp_property_const|mtp_property_pure)) != 0;
 }  /* is_Call_pure */
 
+static ir_node *get_base_and_offset(ir_node *ptr, long *pOffset)
+{
+       ir_mode *mode  = get_irn_mode(ptr);
+       long    offset = 0;
+
+       /* TODO: long might not be enough, we should probably use some tarval thingy... */
+       for (;;) {
+               if (is_Add(ptr)) {
+                       ir_node *l = get_Add_left(ptr);
+                       ir_node *r = get_Add_right(ptr);
+
+                       if (get_irn_mode(l) != mode || !is_Const(r))
+                               break;
+
+                       offset += get_tarval_long(get_Const_tarval(r));
+                       ptr     = l;
+               } else if (is_Sub(ptr)) {
+                       ir_node *l = get_Sub_left(ptr);
+                       ir_node *r = get_Sub_right(ptr);
+
+                       if (get_irn_mode(l) != mode || !is_Const(r))
+                               break;
+
+                       offset -= get_tarval_long(get_Const_tarval(r));
+                       ptr     = l;
+               } else if (is_Sel(ptr)) {
+                       ir_entity *ent = get_Sel_entity(ptr);
+                       ir_type   *tp  = get_entity_owner(ent);
+
+                       if (is_Array_type(tp)) {
+                               int     size;
+                               ir_node *index;
+
+                               /* only one dimensional arrays yet */
+                               if (get_Sel_n_indexs(ptr) != 1)
+                                       break;
+                               index = get_Sel_index(ptr, 0);
+                               if (! is_Const(index))
+                                       break;
+
+                               tp = get_entity_type(ent);
+                               if (get_type_state(tp) != layout_fixed)
+                                       break;
+
+                               size    = get_type_size_bytes(tp);
+                               offset += size * get_tarval_long(get_Const_tarval(index));
+                       } else {
+                               if (get_type_state(tp) != layout_fixed)
+                                       break;
+                               offset += get_entity_offset(ent);
+                       }
+                       ptr = get_Sel_ptr(ptr);
+               } else
+                       break;
+       }
+
+       *pOffset = offset;
+       return ptr;
+}
+
+static int try_load_after_store(ir_node *load,
+               ir_node *load_base_ptr, long load_offset, ir_node *store)
+{
+       ldst_info_t *info;
+       ir_node *store_ptr      = get_Store_ptr(store);
+       long     store_offset;
+       ir_node *store_base_ptr = get_base_and_offset(store_ptr, &store_offset);
+       ir_node *store_value;
+       ir_mode *store_mode;
+       ir_node *load_ptr;
+       ir_mode *load_mode;
+       long     load_mode_len;
+       long     store_mode_len;
+       long     delta;
+       int      res;
+
+       if (load_base_ptr != store_base_ptr)
+               return 0;
+
+       load_mode      = get_Load_mode(load);
+       load_mode_len  = get_mode_size_bytes(load_mode);
+       store_mode     = get_irn_mode(get_Store_value(store));
+       store_mode_len = get_mode_size_bytes(store_mode);
+       delta          = load_offset - store_offset;
+       store_value    = get_Store_value(store);
+
+       if (delta != 0 || store_mode != load_mode) {
+               if (delta < 0 || delta + load_mode_len > store_mode_len)
+                       return 0;
+
+               if (get_mode_arithmetic(store_mode) != irma_twos_complement ||
+                       get_mode_arithmetic(load_mode)  != irma_twos_complement)
+                       return 0;
+
+
+               /* produce a shift to adjust offset delta */
+               if (delta > 0) {
+                       ir_node *cnst;
+
+                       /* FIXME: only true for little endian */
+                       cnst        = new_Const_long(mode_Iu, delta * 8);
+                       store_value = new_r_Shr(get_nodes_block(load),
+                                                                       store_value, cnst, store_mode);
+               }
+
+               /* add an convert if needed */
+               if (store_mode != load_mode) {
+                       store_value = new_r_Conv(get_nodes_block(load), store_value, load_mode);
+               }
+       }
+
+       DBG_OPT_RAW(load, store_value);
+
+       info = get_irn_link(load);
+       if (info->projs[pn_Load_M])
+               exchange(info->projs[pn_Load_M], get_Load_mem(load));
+
+       res = 0;
+       /* no exception */
+       if (info->projs[pn_Load_X_except]) {
+               exchange( info->projs[pn_Load_X_except], new_Bad());
+               res |= CF_CHANGED;
+       }
+       if (info->projs[pn_Load_X_regular]) {
+               exchange( info->projs[pn_Load_X_regular], new_r_Jmp(get_nodes_block(load)));
+               res |= CF_CHANGED;
+       }
+
+       if (info->projs[pn_Load_res])
+               exchange(info->projs[pn_Load_res], store_value);
+
+       load_ptr = get_Load_ptr(load);
+       kill_node(load);
+       reduce_adr_usage(load_ptr);
+       return res | DF_CHANGED;
+}
+
 /**
  * Follow the memory chain as long as there are only Loads,
  * alias free Stores, and constant Calls and try to replace the
@@ -465,68 +926,41 @@ static unsigned is_Call_pure(ir_node *call) {
  *
  * INC_MASTER() must be called before dive into
  */
-static unsigned follow_Mem_chain(ir_node *load, ir_node *curr) {
-       unsigned res = 0;
+static unsigned follow_Mem_chain(ir_node *load, ir_node *curr)
+{
+       unsigned    res = 0;
        ldst_info_t *info = get_irn_link(load);
-       ir_node *pred;
-       ir_node *ptr       = get_Load_ptr(load);
-       ir_node *mem       = get_Load_mem(load);
-       ir_mode *load_mode = get_Load_mode(load);
+       ir_node     *pred;
+       ir_node     *ptr       = get_Load_ptr(load);
+       ir_node     *mem       = get_Load_mem(load);
+       ir_mode     *load_mode = get_Load_mode(load);
 
        for (pred = curr; load != pred; ) {
                ldst_info_t *pred_info = get_irn_link(pred);
 
                /*
-                * BEWARE: one might think that checking the modes is useless, because
-                * if the pointers are identical, they refer to the same object.
-                * This is only true in strong typed languages, not in C were the following
-                * is possible a = *(ir_type1 *)p; b = *(ir_type2 *)p ...
+                * a Load immediately after a Store -- a read after write.
+                * We may remove the Load, if both Load & Store does not have an
+                * exception handler OR they are in the same MacroBlock. In the latter
+                * case the Load cannot throw an exception when the previous Store was
+                * quiet.
+                *
+                * Why we need to check for Store Exception? If the Store cannot
+                * be executed (ROM) the exception handler might simply jump into
+                * the load MacroBlock :-(
+                * We could make it a little bit better if we would know that the
+                * exception handler of the Store jumps directly to the end...
                 */
-               if (is_Store(pred) && get_Store_ptr(pred) == ptr &&
-                   can_use_stored_value(get_irn_mode(get_Store_value(pred)), load_mode)) {
-                       /*
-                        * a Load immediately after a Store -- a read after write.
-                        * We may remove the Load, if both Load & Store does not have an exception handler
-                        * OR they are in the same MacroBlock. In the latter case the Load cannot
-                        * throw an exception when the previous Store was quiet.
-                        *
-                        * Why we need to check for Store Exception? If the Store cannot
-                        * be executed (ROM) the exception handler might simply jump into
-                        * the load MacroBlock :-(
-                        * We could make it a little bit better if we would know that the exception
-                        * handler of the Store jumps directly to the end...
-                        */
-                       if ((pred_info->projs[pn_Store_X_except] == NULL && info->projs[pn_Load_X_except] == NULL) ||
-                           get_nodes_MacroBlock(load) == get_nodes_MacroBlock(pred)) {
-                               ir_node *value = get_Store_value(pred);
-
-                               DBG_OPT_RAW(load, value);
-
-                               /* add an convert if needed */
-                               if (get_irn_mode(get_Store_value(pred)) != load_mode) {
-                                       value = new_r_Conv(current_ir_graph, get_nodes_block(load), value, load_mode);
-                               }
-
-                               if (info->projs[pn_Load_M])
-                                       exchange(info->projs[pn_Load_M], mem);
-
-                               /* no exception */
-                               if (info->projs[pn_Load_X_except]) {
-                                       exchange( info->projs[pn_Load_X_except], new_Bad());
-                                       res |= CF_CHANGED;
-                               }
-                               if (info->projs[pn_Load_X_regular]) {
-                                       exchange( info->projs[pn_Load_X_regular], new_r_Jmp(current_ir_graph, get_nodes_block(load)));
-                                       res |= CF_CHANGED;
-                               }
-
-                               if (info->projs[pn_Load_res])
-                                       exchange(info->projs[pn_Load_res], value);
-
-                               kill_node(load);
-                               reduce_adr_usage(ptr);
-                               return res | DF_CHANGED;
-                       }
+               if (is_Store(pred) && ((pred_info->projs[pn_Store_X_except] == NULL
+                               && info->projs[pn_Load_X_except] == NULL)
+                               || get_nodes_MacroBlock(load) == get_nodes_MacroBlock(pred)))
+               {
+                       long    load_offset;
+                       ir_node *base_ptr = get_base_and_offset(ptr, &load_offset);
+                       int     changes   = try_load_after_store(load, base_ptr, load_offset, pred);
+
+                       if (changes != 0)
+                               return res | changes;
                } else if (is_Load(pred) && get_Load_ptr(pred) == ptr &&
                           can_use_stored_value(get_Load_mode(pred), load_mode)) {
                        /*
@@ -547,13 +981,13 @@ static unsigned follow_Mem_chain(ir_node *load, ir_node *curr) {
                                if (info->projs[pn_Load_res]) {
                                        if (pred_info->projs[pn_Load_res] == NULL) {
                                                /* create a new Proj again */
-                                               pred_info->projs[pn_Load_res] = new_r_Proj(current_ir_graph, get_nodes_block(pred), pred, get_Load_mode(pred), pn_Load_res);
+                                               pred_info->projs[pn_Load_res] = new_r_Proj(get_nodes_block(pred), pred, get_Load_mode(pred), pn_Load_res);
                                        }
                                        value = pred_info->projs[pn_Load_res];
 
                                        /* add an convert if needed */
                                        if (get_Load_mode(pred) != load_mode) {
-                                               value = new_r_Conv(current_ir_graph, get_nodes_block(load), value, load_mode);
+                                               value = new_r_Conv(get_nodes_block(load), value, load_mode);
                                        }
 
                                        exchange(info->projs[pn_Load_res], value);
@@ -568,7 +1002,7 @@ static unsigned follow_Mem_chain(ir_node *load, ir_node *curr) {
                                        res |= CF_CHANGED;
                                }
                                if (info->projs[pn_Load_X_regular]) {
-                                       exchange( info->projs[pn_Load_X_regular], new_r_Jmp(current_ir_graph, get_nodes_block(load)));
+                                       exchange( info->projs[pn_Load_X_regular], new_r_Jmp(get_nodes_block(load)));
                                        res |= CF_CHANGED;
                                }
 
@@ -586,7 +1020,7 @@ static unsigned follow_Mem_chain(ir_node *load, ir_node *curr) {
                                get_irn_mode(get_Store_value(pred)),
                                ptr, load_mode);
                        /* if the might be an alias, we cannot pass this Store */
-                       if (rel != no_alias)
+                       if (rel != ir_no_alias)
                                break;
                        pred = skip_Proj(get_Store_mem(pred));
                } else if (is_Load(pred)) {
@@ -625,6 +1059,34 @@ static unsigned follow_Mem_chain(ir_node *load, ir_node *curr) {
        return res;
 }  /* follow_Mem_chain */
 
+/*
+ * Check if we can replace the load by a given const from
+ * the const code irg.
+ */
+ir_node *can_replace_load_by_const(const ir_node *load, ir_node *c)
+{
+       ir_mode *c_mode = get_irn_mode(c);
+       ir_mode *l_mode = get_Load_mode(load);
+       ir_node *res    = NULL;
+
+       if (c_mode != l_mode) {
+               /* check, if the mode matches OR can be easily converted info */
+               if (is_reinterpret_cast(c_mode, l_mode)) {
+                       /* we can safely cast */
+                       dbg_info *dbg   = get_irn_dbg_info(load);
+                       ir_node  *block = get_nodes_block(load);
+
+                       /* copy the value from the const code irg and cast it */
+                       res = copy_const_value(dbg, c);
+                       res = new_rd_Conv(dbg, block, res, l_mode);
+               }
+       } else {
+               /* copy the value from the const code irg */
+               res = copy_const_value(get_irn_dbg_info(load), c);
+       }
+       return res;
+}  /* can_replace_load_by_const */
+
 /**
  * optimize a Load
  *
@@ -633,9 +1095,10 @@ static unsigned follow_Mem_chain(ir_node *load, ir_node *curr) {
 static unsigned optimize_load(ir_node *load)
 {
        ldst_info_t *info = get_irn_link(load);
-       ir_node *mem, *ptr, *new_node;
-       ir_entity *ent;
-       unsigned res = 0;
+       ir_node     *mem, *ptr, *value;
+       ir_entity   *ent;
+       long        dummy;
+       unsigned    res = 0;
 
        /* do NOT touch volatile loads for now */
        if (get_Load_volatility(load) == volatility_is_volatile)
@@ -653,43 +1116,27 @@ static unsigned optimize_load(ir_node *load)
         * like x = new O; x->t;
         */
        if (info->projs[pn_Load_X_except]) {
-               if (is_Sel(ptr)) {
-                       ir_node *mem = get_Sel_mem(ptr);
-
-                       /* FIXME: works with the current FE, but better use the base */
-                       if (is_Alloc(skip_Proj(mem))) {
-                               /* ok, check the types */
-                               ir_entity *ent    = get_Sel_entity(ptr);
-                               ir_type   *s_type = get_entity_type(ent);
-                               ir_type   *a_type = get_Alloc_type(mem);
-
-                               if (is_SubClass_of(s_type, a_type)) {
-                                       /* ok, condition met: there can't be an exception because
-                                       * Alloc guarantees that enough memory was allocated */
-
-                                       exchange(info->projs[pn_Load_X_except], new_Bad());
-                                       info->projs[pn_Load_X_except] = NULL;
-                                       exchange(info->projs[pn_Load_X_regular], new_r_Jmp(current_ir_graph, get_nodes_block(load)));
-                                       info->projs[pn_Load_X_regular] = NULL;
-                                       res |= CF_CHANGED;
-                               }
-                       }
-               } else if (is_Alloc(skip_Proj(skip_Cast(ptr)))) {
-                               /* simple case: a direct load after an Alloc. Firm Alloc throw
-                                * an exception in case of out-of-memory. So, there is no way for an
-                                * exception in this load.
-                                * This code is constructed by the "exception lowering" in the Jack compiler.
-                                */
-                               exchange(info->projs[pn_Load_X_except], new_Bad());
-                               info->projs[pn_Load_X_except] = NULL;
-                               exchange(info->projs[pn_Load_X_regular], new_r_Jmp(current_ir_graph, get_nodes_block(load)));
-                               info->projs[pn_Load_X_regular] = NULL;
-                               res |= CF_CHANGED;
+               ir_node *addr = ptr;
+
+               /* find base address */
+               while (is_Sel(addr))
+                       addr = get_Sel_ptr(addr);
+               if (is_Alloc(skip_Proj(skip_Cast(addr)))) {
+                       /* simple case: a direct load after an Alloc. Firm Alloc throw
+                        * an exception in case of out-of-memory. So, there is no way for an
+                        * exception in this load.
+                        * This code is constructed by the "exception lowering" in the Jack compiler.
+                        */
+                       exchange(info->projs[pn_Load_X_except], new_Bad());
+                       info->projs[pn_Load_X_except] = NULL;
+                       exchange(info->projs[pn_Load_X_regular], new_r_Jmp(get_nodes_block(load)));
+                       info->projs[pn_Load_X_regular] = NULL;
+                       res |= CF_CHANGED;
                }
        }
 
        /* The mem of the Load. Must still be returned after optimization. */
-       mem  = get_Load_mem(load);
+       mem = get_Load_mem(load);
 
        if (! info->projs[pn_Load_res] && ! info->projs[pn_Load_X_except]) {
                /* a Load which value is neither used nor exception checked, remove it */
@@ -697,7 +1144,7 @@ static unsigned optimize_load(ir_node *load)
 
                if (info->projs[pn_Load_X_regular]) {
                        /* should not happen, but if it does, remove it */
-                       exchange(info->projs[pn_Load_X_regular], new_r_Jmp(current_ir_graph, get_nodes_block(load)));
+                       exchange(info->projs[pn_Load_X_regular], new_r_Jmp(get_nodes_block(load)));
                        res |= CF_CHANGED;
                }
                kill_node(load);
@@ -707,37 +1154,16 @@ static unsigned optimize_load(ir_node *load)
 
        /* Load from a constant polymorphic field, where we can resolve
           polymorphism. */
-       new_node = transform_node_Load(load);
-       if (new_node != load) {
-               if (info->projs[pn_Load_M]) {
-                       exchange(info->projs[pn_Load_M], mem);
-                       info->projs[pn_Load_M] = NULL;
-               }
-               if (info->projs[pn_Load_X_except]) {
-                       exchange(info->projs[pn_Load_X_except], new_Bad());
-                       info->projs[pn_Load_X_except] = NULL;
-                       res |= CF_CHANGED;
-               }
-               if (info->projs[pn_Load_X_regular]) {
-                       exchange(info->projs[pn_Load_X_regular], new_r_Jmp(current_ir_graph, get_nodes_block(load)));
-                       info->projs[pn_Load_X_regular] = NULL;
-                       res |= CF_CHANGED;
-               }
-               if (info->projs[pn_Load_res])
-                       exchange(info->projs[pn_Load_res], new_node);
-
-               kill_node(load);
-               reduce_adr_usage(ptr);
-               return res | DF_CHANGED;
-       }
-
-       /* check if we can determine the entity that will be loaded */
-       ent = find_constant_entity(ptr);
-       if (ent) {
-               if ((allocation_static == get_entity_allocation(ent)) &&
-                       (visibility_external_allocated != get_entity_visibility(ent))) {
-                       /* a static allocation that is not external: there should be NO exception
-                        * when loading. */
+       value = transform_polymorph_Load(load);
+       if (value == load) {
+               value = NULL;
+               /* check if we can determine the entity that will be loaded */
+               ent = find_constant_entity(ptr);
+               if (ent != NULL
+                               && get_entity_visibility(ent) != ir_visibility_external) {
+                       /* a static allocation that is not external: there should be NO
+                        * exception when loading even if we cannot replace the load itself.
+                        */
 
                        /* no exception, clear the info field as it might be checked later again */
                        if (info->projs[pn_Load_X_except]) {
@@ -746,93 +1172,60 @@ static unsigned optimize_load(ir_node *load)
                                res |= CF_CHANGED;
                        }
                        if (info->projs[pn_Load_X_regular]) {
-                               exchange(info->projs[pn_Load_X_regular], new_r_Jmp(current_ir_graph, get_nodes_block(load)));
+                               exchange(info->projs[pn_Load_X_regular], new_r_Jmp(get_nodes_block(load)));
                                info->projs[pn_Load_X_regular] = NULL;
                                res |= CF_CHANGED;
                        }
 
-                       if (variability_constant == get_entity_variability(ent)) {
-                               if (is_atomic_entity(ent)) {
-                                       /* Might not be atomic after
-                                          lowering of Sels.  In this
-                                          case we could also load, but
-                                          it's more complicated. */
-                                       /* more simpler case: we load the content of a constant value:
-                                        * replace it by the constant itself
-                                        */
-
-                                       /* no memory */
-                                       if (info->projs[pn_Load_M]) {
-                                               exchange(info->projs[pn_Load_M], mem);
-                                               res |= DF_CHANGED;
-                                       }
-                                       /* no result :-) */
-                                       if (info->projs[pn_Load_res]) {
-                                               if (is_atomic_entity(ent)) {
-                                                       ir_node *c = copy_const_value(get_irn_dbg_info(load), get_atomic_ent_value(ent));
-
-                                                       DBG_OPT_RC(load, c);
-                                                       exchange(info->projs[pn_Load_res], c);
-                                                       res |= DF_CHANGED;
-                                               }
-                                       }
-                                       kill_node(load);
-                                       reduce_adr_usage(ptr);
-                                       return res;
-                               } else {
+                       if (get_entity_linkage(ent) & IR_LINKAGE_CONSTANT) {
+                               if (ent->initializer != NULL) {
+                                       /* new style initializer */
+                                       value = find_compound_ent_value(ptr);
+                               } else if (entity_has_compound_ent_values(ent)) {
+                                       /* old style initializer */
                                        compound_graph_path *path = get_accessed_path(ptr);
 
-                                       if (path) {
-                                               ir_node *c;
-
+                                       if (path != NULL) {
                                                assert(is_proper_compound_graph_path(path, get_compound_graph_path_length(path)-1));
-                                               /*
-                                               {
-                                                       int j;
-                                                       for (j = 0; j < get_compound_graph_path_length(path); ++j) {
-                                                               ir_entity *node = get_compound_graph_path_node(path, j);
-                                                               fprintf(stdout, ".%s", get_entity_name(node));
-                                                               if (is_Array_type(get_entity_owner(node)))
-                                                                       fprintf(stdout, "[%d]", get_compound_graph_path_array_index(path, j));
-                                                       }
-                                                       printf("\n");
-                                               }
-                                               */
-
-                                               c = get_compound_ent_value_by_path(ent, path);
-                                               free_compound_graph_path(path);
 
-                                               /* printf("  cons: "); DDMN(c); */
-
-                                               if (info->projs[pn_Load_M]) {
-                                                       exchange(info->projs[pn_Load_M], mem);
-                                                       res |= DF_CHANGED;
-                                               }
-                                               if (info->projs[pn_Load_res]) {
-                                                       exchange(info->projs[pn_Load_res], copy_const_value(get_irn_dbg_info(load), c));
-                                                       res |= DF_CHANGED;
-                                               }
-                                               kill_node(load);
-                                               reduce_adr_usage(ptr);
-                                               return res;
-                                       } else {
-                                               /*  We can not determine a correct access path.  E.g., in jack, we load
-                                               a byte from an object to generate an exception.   Happens in test program
-                                               Reflectiontest.
-                                               printf(">>>>>>>>>>>>> Found access to constant entity %s in function %s\n", get_entity_name(ent),
-                                               get_entity_name(get_irg_entity(current_ir_graph)));
-                                               printf("  load: "); DDMN(load);
-                                               printf("  ptr:  "); DDMN(ptr);
-                                               */
+                                               value = get_compound_ent_value_by_path(ent, path);
+                                               DB((dbg, LEVEL_1, "  Constant access at %F%F resulted in %+F\n", ent, path, value));
+                                               free_compound_graph_path(path);
                                        }
                                }
+                               if (value != NULL)
+                                       value = can_replace_load_by_const(load, value);
                        }
                }
        }
+       if (value != NULL) {
+               /* we completely replace the load by this value */
+               if (info->projs[pn_Load_X_except]) {
+                       exchange(info->projs[pn_Load_X_except], new_Bad());
+                       info->projs[pn_Load_X_except] = NULL;
+                       res |= CF_CHANGED;
+               }
+               if (info->projs[pn_Load_X_regular]) {
+                       exchange(info->projs[pn_Load_X_regular], new_r_Jmp(get_nodes_block(load)));
+                       info->projs[pn_Load_X_regular] = NULL;
+                       res |= CF_CHANGED;
+               }
+               if (info->projs[pn_Load_M]) {
+                       exchange(info->projs[pn_Load_M], mem);
+                       res |= DF_CHANGED;
+               }
+               if (info->projs[pn_Load_res]) {
+                       exchange(info->projs[pn_Load_res], value);
+                       res |= DF_CHANGED;
+               }
+               kill_node(load);
+               reduce_adr_usage(ptr);
+               return res;
+       }
 
        /* Check, if the address of this load is used more than once.
-        * If not, this load cannot be removed in any case. */
-       if (get_irn_n_uses(ptr) <= 1)
+        * If not, more load cannot be removed in any case. */
+       if (get_irn_n_uses(ptr) <= 1 && get_irn_n_uses(get_base_and_offset(ptr, &dummy)) <= 1)
                return res;
 
        /*
@@ -856,12 +1249,28 @@ static int is_completely_overwritten(ir_mode *old_mode, ir_mode *new_mode)
        return get_mode_size_bits(new_mode) >= get_mode_size_bits(old_mode);
 }  /* is_completely_overwritten */
 
+/**
+ * Check whether small is a part of large (starting at same address).
+ */
+static int is_partially_same(ir_node *small, ir_node *large)
+{
+       ir_mode *sm = get_irn_mode(small);
+       ir_mode *lm = get_irn_mode(large);
+
+       /* FIXME: Check endianness */
+       return is_Conv(small) && get_Conv_op(small) == large
+           && get_mode_size_bytes(sm) < get_mode_size_bytes(lm)
+           && get_mode_arithmetic(sm) == irma_twos_complement
+           && get_mode_arithmetic(lm) == irma_twos_complement;
+}  /* is_partially_same */
+
 /**
  * follow the memory chain as long as there are only Loads and alias free Stores.
  *
  * INC_MASTER() must be called before dive into
  */
-static unsigned follow_Mem_chain_for_Store(ir_node *store, ir_node *curr) {
+static unsigned follow_Mem_chain_for_Store(ir_node *store, ir_node *curr)
+{
        unsigned res = 0;
        ldst_info_t *info = get_irn_link(store);
        ir_node *pred;
@@ -880,30 +1289,63 @@ static unsigned follow_Mem_chain_for_Store(ir_node *store, ir_node *curr) {
                 * if the pointers are identical, they refer to the same object.
                 * This is only true in strong typed languages, not is C were the following
                 * is possible *(ir_type1 *)p = a; *(ir_type2 *)p = b ...
-                * However, if the mode that is written have a bigger  or equal size the the old
-                * one, the old value is completely overwritten and can be killed ...
+                * However, if the size of the mode that is written is bigger or equal the
+                * size of the old one, the old value is completely overwritten and can be
+                * killed ...
                 */
                if (is_Store(pred) && get_Store_ptr(pred) == ptr &&
-                   get_nodes_MacroBlock(pred) == mblk &&
-                   is_completely_overwritten(get_irn_mode(get_Store_value(pred)), mode)) {
+            get_nodes_MacroBlock(pred) == mblk) {
                        /*
-                        * a Store after a Store in the same block -- a write after write.
-                        * We may remove the first Store, if it does not have an exception handler.
+                        * a Store after a Store in the same MacroBlock -- a write after write.
+                        */
+
+                       /*
+                        * We may remove the first Store, if the old value is completely
+                        * overwritten or the old value is a part of the new value,
+                        * and if it does not have an exception handler.
                         *
                         * TODO: What, if both have the same exception handler ???
                         */
-                       if (get_Store_volatility(pred) != volatility_is_volatile && !pred_info->projs[pn_Store_X_except]) {
-                               DBG_OPT_WAW(pred, store);
-                               exchange(pred_info->projs[pn_Store_M], get_Store_mem(pred));
-                               kill_node(pred);
-                               reduce_adr_usage(ptr);
-                               return DF_CHANGED;
+                       if (get_Store_volatility(pred) != volatility_is_volatile
+                               && !pred_info->projs[pn_Store_X_except]) {
+                               ir_node *predvalue = get_Store_value(pred);
+                               ir_mode *predmode  = get_irn_mode(predvalue);
+
+                               if(is_completely_overwritten(predmode, mode)
+                                       || is_partially_same(predvalue, value)) {
+                                       DBG_OPT_WAW(pred, store);
+                                       exchange(pred_info->projs[pn_Store_M], get_Store_mem(pred));
+                                       kill_node(pred);
+                                       reduce_adr_usage(ptr);
+                                       return DF_CHANGED;
+                               }
+                       }
+
+                       /*
+                        * We may remove the Store, if the old value already contains
+                        * the new value, and if it does not have an exception handler.
+                        *
+                        * TODO: What, if both have the same exception handler ???
+                        */
+                       if (get_Store_volatility(store) != volatility_is_volatile
+                               && !info->projs[pn_Store_X_except]) {
+                               ir_node *predvalue = get_Store_value(pred);
+
+                               if(is_partially_same(value, predvalue)) {
+                                       DBG_OPT_WAW(pred, store);
+                                       exchange(info->projs[pn_Store_M], mem);
+                                       kill_node(store);
+                                       reduce_adr_usage(ptr);
+                                       return DF_CHANGED;
+                               }
                        }
                } else if (is_Load(pred) && get_Load_ptr(pred) == ptr &&
                           value == pred_info->projs[pn_Load_res]) {
                        /*
-                        * a Store of a value after a Load -- a write after read.
-                        * We may remove the second Store, if it does not have an exception handler.
+                        * a Store of a value just loaded from the same address
+                        * -- a write after read.
+                        * We may remove the Store, if it does not have an exception
+                        * handler.
                         */
                        if (! info->projs[pn_Store_X_except]) {
                                DBG_OPT_WAR(store, pred);
@@ -915,17 +1357,23 @@ static unsigned follow_Mem_chain_for_Store(ir_node *store, ir_node *curr) {
                }
 
                if (is_Store(pred)) {
-                       /* check if we can pass thru this store */
+                       /* check if we can pass through this store */
                        ir_alias_relation rel = get_alias_relation(
                                current_ir_graph,
                                get_Store_ptr(pred),
                                get_irn_mode(get_Store_value(pred)),
                                ptr, mode);
                        /* if the might be an alias, we cannot pass this Store */
-                       if (rel != no_alias)
+                       if (rel != ir_no_alias)
                                break;
                        pred = skip_Proj(get_Store_mem(pred));
-               } else if (get_irn_op(pred) == op_Load) {
+               } else if (is_Load(pred)) {
+                       ir_alias_relation rel = get_alias_relation(
+                               current_ir_graph, get_Load_ptr(pred), get_Load_mode(pred),
+                               ptr, mode);
+                       if (rel != ir_no_alias)
+                               break;
+
                        pred = skip_Proj(get_Load_mem(pred));
                } else {
                        /* follow only Load chains */
@@ -951,18 +1399,63 @@ static unsigned follow_Mem_chain_for_Store(ir_node *store, ir_node *curr) {
        return res;
 }  /* follow_Mem_chain_for_Store */
 
+/** find entity used as base for an address calculation */
+static ir_entity *find_entity(ir_node *ptr)
+{
+       switch(get_irn_opcode(ptr)) {
+       case iro_SymConst:
+               return get_SymConst_entity(ptr);
+       case iro_Sel: {
+               ir_node *pred = get_Sel_ptr(ptr);
+               if (get_irg_frame(get_irn_irg(ptr)) == pred)
+                       return get_Sel_entity(ptr);
+
+               return find_entity(pred);
+       }
+       case iro_Sub:
+       case iro_Add: {
+               ir_node *left = get_binop_left(ptr);
+               ir_node *right;
+               if (mode_is_reference(get_irn_mode(left)))
+                       return find_entity(left);
+               right = get_binop_right(ptr);
+               if (mode_is_reference(get_irn_mode(right)))
+                       return find_entity(right);
+               return NULL;
+       }
+       default:
+               return NULL;
+       }
+}
+
 /**
  * optimize a Store
  *
  * @param store  the Store node
  */
-static unsigned optimize_store(ir_node *store) {
-       ir_node *ptr, *mem;
+static unsigned optimize_store(ir_node *store)
+{
+       ir_node   *ptr;
+       ir_node   *mem;
+       ir_entity *entity;
 
        if (get_Store_volatility(store) == volatility_is_volatile)
                return 0;
 
-       ptr = get_Store_ptr(store);
+       ptr    = get_Store_ptr(store);
+       entity = find_entity(ptr);
+
+       /* a store to an entity which is never read is unnecessary */
+       if (entity != NULL && !(get_entity_usage(entity) & ir_usage_read)) {
+               ldst_info_t *info = get_irn_link(store);
+               if (info->projs[pn_Store_X_except] == NULL) {
+                       DB((dbg, LEVEL_1, "  Killing useless %+F to never read entity %+F\n", store, entity));
+                       exchange(info->projs[pn_Store_M], get_Store_mem(store));
+                       kill_node(store);
+                       reduce_adr_usage(ptr);
+                       return DF_CHANGED;
+               }
+       }
 
        /* Check, if the address of this Store is used more than once.
         * If not, this Store cannot be removed in any case. */
@@ -973,6 +1466,7 @@ static unsigned optimize_store(ir_node *store) {
 
        /* follow the memory chain as long as there are only Loads */
        INC_MASTER();
+
        return follow_Mem_chain_for_Store(store, skip_Proj(mem));
 }  /* optimize_store */
 
@@ -1022,7 +1516,7 @@ static unsigned optimize_phi(ir_node *phi, walk_env_t *wenv)
 
        store = skip_Proj(projM);
        old_store = store;
-       if (get_irn_op(store) != op_Store)
+       if (!is_Store(store))
                return 0;
 
        block = get_nodes_block(store);
@@ -1124,10 +1618,10 @@ static unsigned optimize_phi(ir_node *phi, walk_env_t *wenv)
        block = get_nodes_block(phi);
 
        /* second step: create a new memory Phi */
-       phiM = new_rd_Phi(get_irn_dbg_info(phi), current_ir_graph, block, n, inM, mode_M);
+       phiM = new_rd_Phi(get_irn_dbg_info(phi), block, n, inM, mode_M);
 
        /* third step: create a new data Phi */
-       phiD = new_rd_Phi(get_irn_dbg_info(phi), current_ir_graph, block, n, inD, mode);
+       phiD = new_rd_Phi(get_irn_dbg_info(phi), block, n, inD, mode);
 
        /* rewire memory and kill the node */
        for (i = n - 1; i >= 0; --i) {
@@ -1141,19 +1635,19 @@ static unsigned optimize_phi(ir_node *phi, walk_env_t *wenv)
        }
 
        /* fourth step: create the Store */
-       store = new_rd_Store(db, current_ir_graph, block, phiM, ptr, phiD);
+       store = new_rd_Store(db, block, phiM, ptr, phiD, 0);
 #ifdef DO_CACHEOPT
        co_set_irn_name(store, co_get_irn_ident(old_store));
 #endif
 
-       projM = new_rd_Proj(NULL, current_ir_graph, block, store, mode_M, pn_Store_M);
+       projM = new_rd_Proj(NULL, block, store, mode_M, pn_Store_M);
 
        info = get_ldst_info(store, &wenv->obst);
        info->projs[pn_Store_M] = projM;
 
        /* fifths step: repair exception flow */
        if (exc) {
-               ir_node *projX = new_rd_Proj(NULL, current_ir_graph, block, store, mode_X, pn_Store_X_except);
+               ir_node *projX = new_rd_Proj(NULL, block, store, mode_X, pn_Store_X_except);
 
                info->projs[pn_Store_X_except] = projX;
                info->exc_block                = exc;
@@ -1179,7 +1673,8 @@ static unsigned optimize_phi(ir_node *phi, walk_env_t *wenv)
 /**
  * walker, do the optimizations
  */
-static void do_load_store_optimize(ir_node *n, void *env) {
+static void do_load_store_optimize(ir_node *n, void *env)
+{
        walk_env_t *wenv = env;
 
        switch (get_irn_opcode(n)) {
@@ -1210,7 +1705,6 @@ typedef struct scc {
 typedef struct node_entry {
        unsigned DFSnum;    /**< the DFS number of this node */
        unsigned low;       /**< the low number of this node */
-       ir_node  *header;   /**< the header of this node */
        int      in_stack;  /**< flag, set if the node is on the stack */
        ir_node  *next;     /**< link to the next node the the same scc */
        scc      *pscc;     /**< the scc of this node */
@@ -1231,7 +1725,8 @@ typedef struct loop_env {
 /**
 * Gets the node_entry of a node
 */
-static node_entry *get_irn_ne(ir_node *irn, loop_env *env) {
+static node_entry *get_irn_ne(ir_node *irn, loop_env *env)
+{
        ir_phase   *ph = &env->ph;
        node_entry *e  = phase_get_irn_data(&env->ph, irn);
 
@@ -1249,7 +1744,8 @@ static node_entry *get_irn_ne(ir_node *irn, loop_env *env) {
  * @param env   the loop environment
  * @param n     the node to push
  */
-static void push(loop_env *env, ir_node *n) {
+static void push(loop_env *env, ir_node *n)
+{
        node_entry *e;
 
        if (env->tos == ARR_LEN(env->stack)) {
@@ -1268,7 +1764,8 @@ static void push(loop_env *env, ir_node *n) {
  *
  * @return  The topmost node
  */
-static ir_node *pop(loop_env *env) {
+static ir_node *pop(loop_env *env)
+{
        ir_node *n = env->stack[--env->tos];
        node_entry *e = get_irn_ne(n, env);
 
@@ -1283,7 +1780,8 @@ static ir_node *pop(loop_env *env) {
  * @param irn           the node to check
  * @param header_block  the header block of the induction variable
  */
-static int is_rc(ir_node *irn, ir_node *header_block) {
+static int is_rc(ir_node *irn, ir_node *header_block)
+{
        ir_node *block = get_nodes_block(irn);
 
        return (block != header_block) && block_dominates(block, header_block);
@@ -1297,17 +1795,50 @@ struct phi_entry {
        phi_entry *next;
 };
 
+/**
+ * An entry in the avail set.
+ */
+typedef struct avail_entry_t {
+       ir_node *ptr;   /**< the address pointer */
+       ir_mode *mode;  /**< the load mode */
+       ir_node *load;  /**< the associated Load */
+} avail_entry_t;
+
+/**
+ * Compare two avail entries.
+ */
+static int cmp_avail_entry(const void *elt, const void *key, size_t size)
+{
+       const avail_entry_t *a = elt;
+       const avail_entry_t *b = key;
+       (void) size;
+
+       return a->ptr != b->ptr || a->mode != b->mode;
+}  /* cmp_avail_entry */
+
+/**
+ * Calculate the hash value of an avail entry.
+ */
+static unsigned hash_cache_entry(const avail_entry_t *entry)
+{
+       return get_irn_idx(entry->ptr) * 9 + HASH_PTR(entry->mode);
+}  /* hash_cache_entry */
+
 /**
  * Move loops out of loops if possible.
  *
  * @param pscc   the loop described by an SCC
  * @param env    the loop environment
  */
-static void move_loads_out_of_loops(scc *pscc, loop_env *env) {
+static void move_loads_out_of_loops(scc *pscc, loop_env *env)
+{
        ir_node   *phi, *load, *next, *other, *next_other;
        ir_entity *ent;
        int       j;
        phi_entry *phi_list = NULL;
+       set       *avail;
+
+       avail = new_set(cmp_avail_entry, 8);
 
        /* collect all outer memories */
        for (phi = pscc->head; phi != NULL; phi = next) {
@@ -1318,7 +1849,7 @@ static void move_loads_out_of_loops(scc *pscc, loop_env *env) {
                if (! is_Phi(phi))
                        continue;
 
-               assert(get_irn_mode(phi) == mode_M && "DFS geturn non-memory Phi");
+               assert(get_irn_mode(phi) == mode_M && "DFS return non-memory Phi");
 
                for (j = get_irn_arity(phi) - 1; j >= 0; --j) {
                        ir_node    *pred = get_irn_n(phi, j);
@@ -1338,6 +1869,10 @@ static void move_loads_out_of_loops(scc *pscc, loop_env *env) {
        /* no Phis no fun */
        assert(phi_list != NULL && "DFS found a loop without Phi");
 
+       /* for now, we cannot handle more than one input (only reducible cf) */
+       if (phi_list->next != NULL)
+               return;
+
        for (load = pscc->head; load; load = next) {
                ir_mode *load_mode;
                node_entry *ne = get_irn_ne(load, env);
@@ -1351,10 +1886,10 @@ static void move_loads_out_of_loops(scc *pscc, loop_env *env) {
                        if (info->projs[pn_Load_res] == NULL || info->projs[pn_Load_X_regular] != NULL || info->projs[pn_Load_X_except] != NULL)
                                continue;
 
-                       /* for now, we can only handle Load(Global) */
+                       /* for now, we can only move Load(Global) */
                        if (! is_Global(ptr))
                                continue;
-                       ent = get_Global_entity(ptr);
+                       ent       = get_Global_entity(ptr);
                        load_mode = get_Load_mode(load);
                        for (other = pscc->head; other != NULL; other = next_other) {
                                node_entry *ne = get_irn_ne(other, env);
@@ -1367,20 +1902,16 @@ static void move_loads_out_of_loops(scc *pscc, loop_env *env) {
                                                get_irn_mode(get_Store_value(other)),
                                                ptr, load_mode);
                                        /* if the might be an alias, we cannot pass this Store */
-                                       if (rel != no_alias)
+                                       if (rel != ir_no_alias)
                                                break;
                                }
-                               /* only pure Calls are allowed here, so ignore them */
+                               /* only Phis and pure Calls are allowed here, so ignore them */
                        }
                        if (other == NULL) {
                                ldst_info_t *ninfo;
                                phi_entry   *pe;
                                dbg_info    *db;
 
-                               /* for now, we cannot handle more than one input */
-                               if (phi_list->next != NULL)
-                                       return;
-
                                /* yep, no aliasing Store found, Load can be moved */
                                DB((dbg, LEVEL_1, "  Found a Load that could be moved: %+F\n", load));
 
@@ -1391,16 +1922,26 @@ static void move_loads_out_of_loops(scc *pscc, loop_env *env) {
                                        ir_node *blk  = get_nodes_block(phi);
                                        ir_node *pred = get_Block_cfgpred_block(blk, pos);
                                        ir_node *irn, *mem;
+                                       avail_entry_t entry, *res;
 
-                                       pe->load = irn = new_rd_Load(db, current_ir_graph, pred, get_Phi_pred(phi, pos), ptr, load_mode);
+                                       entry.ptr  = ptr;
+                                       entry.mode = load_mode;
+                                       res = set_find(avail, &entry, sizeof(entry), hash_cache_entry(&entry));
+                                       if (res != NULL) {
+                                               irn = res->load;
+                                       } else {
+                                               irn = new_rd_Load(db, pred, get_Phi_pred(phi, pos), ptr, load_mode, 0);
+                                               entry.load = irn;
+                                               set_insert(avail, &entry, sizeof(entry), hash_cache_entry(&entry));
+                                               DB((dbg, LEVEL_1, "  Created %+F in %+F\n", irn, pred));
+                                       }
+                                       pe->load = irn;
                                        ninfo = get_ldst_info(irn, phase_obst(&env->ph));
 
-                                       ninfo->projs[pn_Load_M] = mem = new_r_Proj(current_ir_graph, pred, irn, mode_M, pn_Load_M);
+                                       ninfo->projs[pn_Load_M] = mem = new_r_Proj(pred, irn, mode_M, pn_Load_M);
                                        set_Phi_pred(phi, pos, mem);
 
-                                       ninfo->projs[pn_Load_res] = new_r_Proj(current_ir_graph, pred, irn, load_mode, pn_Load_res);
-
-                                       DB((dbg, LEVEL_1, "  Created %+F in %+F\n", irn, pred));
+                                       ninfo->projs[pn_Load_res] = new_r_Proj(pred, irn, load_mode, pn_Load_res);
                                }
 
                                /* now kill the old Load */
@@ -1411,6 +1952,7 @@ static void move_loads_out_of_loops(scc *pscc, loop_env *env) {
                        }
                }
        }
+       del_set(avail);
 }  /* move_loads_out_of_loops */
 
 /**
@@ -1419,7 +1961,8 @@ static void move_loads_out_of_loops(scc *pscc, loop_env *env) {
  * @param pscc  the SCC
  * @param env   the loop environment
  */
-static void process_loop(scc *pscc, loop_env *env) {
+static void process_loop(scc *pscc, loop_env *env)
+{
        ir_node *irn, *next, *header = NULL;
        node_entry *b, *h = NULL;
        int j, only_phi, num_outside, process = 0;
@@ -1433,13 +1976,12 @@ static void process_loop(scc *pscc, loop_env *env) {
                next = e->next;
                b = get_irn_ne(block, env);
 
-               if (header) {
+               if (header != NULL) {
                        if (h->POnum < b->POnum) {
                                header = block;
                                h      = b;
                        }
-               }
-               else {
+               } else {
                        header = block;
                        h      = b;
                }
@@ -1494,10 +2036,12 @@ static void process_loop(scc *pscc, loop_env *env) {
                                                /* not a memory loop */
                                                goto fail;
                                        }
-                                       if (! out_rc) {
+                                       if (out_rc == NULL) {
+                                               /* first region constant */
                                                out_rc = pred;
                                                ++num_outside;
                                        } else if (out_rc != pred) {
+                                               /* another region constant */
                                                ++num_outside;
                                        }
                                }
@@ -1517,22 +2061,20 @@ static void process_loop(scc *pscc, loop_env *env) {
                for (irn = pscc->head; irn; irn = next) {
                        node_entry *e = get_irn_ne(irn, env);
                        next = e->next;
-                       e->header = NULL;
                        exchange(irn, out_rc);
                }
                env->changes |= DF_CHANGED;
                return;
        }
 
-       /* set the header for every node in this scc */
+#ifdef DEBUG_libfirm
        for (irn = pscc->head; irn; irn = next) {
                node_entry *e = get_irn_ne(irn, env);
-               e->header = header;
                next = e->next;
                DB((dbg, LEVEL_2, " %+F,", irn));
        }
        DB((dbg, LEVEL_2, "\n"));
-
+#endif
        move_loads_out_of_loops(pscc, env);
 
 fail:
@@ -1545,7 +2087,8 @@ fail:
  * @param pscc  the SCC
  * @param env   the loop environment
  */
-static void process_scc(scc *pscc, loop_env *env) {
+static void process_scc(scc *pscc, loop_env *env)
+{
        ir_node *head = pscc->head;
        node_entry *e = get_irn_ne(head, env);
 
@@ -1595,7 +2138,7 @@ static void dfs(ir_node *irn, loop_env *env)
                        ir_node *pred = get_irn_n(irn, i);
                        node_entry *o = get_irn_ne(pred, env);
 
-                       if (irn_not_visited(pred)) {
+                       if (!irn_visited(pred)) {
                                dfs(pred, env);
                                node->low = MIN(node->low, o->low);
                        }
@@ -1606,7 +2149,7 @@ static void dfs(ir_node *irn, loop_env *env)
                ir_node *pred = get_fragile_op_mem(irn);
                node_entry *o = get_irn_ne(pred, env);
 
-               if (irn_not_visited(pred)) {
+               if (!irn_visited(pred)) {
                        dfs(pred, env);
                        node->low = MIN(node->low, o->low);
                }
@@ -1616,7 +2159,7 @@ static void dfs(ir_node *irn, loop_env *env)
                ir_node *pred = get_Proj_pred(irn);
                node_entry *o = get_irn_ne(pred, env);
 
-               if (irn_not_visited(pred)) {
+               if (!irn_visited(pred)) {
                        dfs(pred, env);
                        node->low = MIN(node->low, o->low);
                }
@@ -1652,7 +2195,8 @@ static void dfs(ir_node *irn, loop_env *env)
  * @param irg  the graph to process
  * @param env  the loop environment
  */
-static void do_dfs(ir_graph *irg, loop_env *env) {
+static void do_dfs(ir_graph *irg, loop_env *env)
+{
        ir_graph *rem = current_ir_graph;
        ir_node  *endblk, *end;
        int      i;
@@ -1682,7 +2226,7 @@ static void do_dfs(ir_graph *irg, loop_env *env) {
        for (i = get_End_n_keepalives(end) - 1; i >= 0; --i) {
                ir_node *ka = get_End_keepalive(end, i);
 
-               if (is_Phi(ka) && irn_not_visited(ka))
+               if (is_Phi(ka) && !irn_visited(ka))
                        dfs(ka, env);
        }
        current_ir_graph = rem;
@@ -1691,7 +2235,8 @@ static void do_dfs(ir_graph *irg, loop_env *env) {
 /**
  * Initialize new phase data. We do this always explicit, so return NULL here
  */
-static void *init_loop_data(ir_phase *ph, const ir_node *irn, void *data) {
+static void *init_loop_data(ir_phase *ph, const ir_node *irn, void *data)
+{
        (void)ph;
        (void)irn;
        (void)data;
@@ -1703,7 +2248,8 @@ static void *init_loop_data(ir_phase *ph, const ir_node *irn, void *data) {
  *
  * @param irg  the graph
  */
-static int optimize_loops(ir_graph *irg) {
+static int optimize_loops(ir_graph *irg)
+{
        loop_env env;
 
        env.stack         = NEW_ARR_F(ir_node *, 128);
@@ -1725,7 +2271,8 @@ static int optimize_loops(ir_graph *irg) {
 /*
  * do the load store optimization
  */
-void optimize_load_store(ir_graph *irg) {
+int optimize_load_store(ir_graph *irg)
+{
        walk_env_t env;
 
        FIRM_DBG_REGISTER(dbg, "firm.opt.ldstopt");
@@ -1743,8 +2290,8 @@ void optimize_load_store(ir_graph *irg) {
        assure_postdoms(irg);
 
        if (get_opt_alias_analysis()) {
-               assure_irg_address_taken_computed(irg);
-               assure_irp_globals_address_taken_computed();
+               assure_irg_entity_usage_computed(irg);
+               assure_irp_globals_entity_usage_computed();
        }
 
        obstack_init(&env.obst);
@@ -1764,6 +2311,7 @@ void optimize_load_store(ir_graph *irg) {
        /* Handle graph state */
        if (env.changes) {
                set_irg_outs_inconsistent(irg);
+               set_irg_entity_usage_state(irg, ir_entity_usage_not_computed);
        }
 
        if (env.changes & CF_CHANGED) {
@@ -1771,4 +2319,10 @@ void optimize_load_store(ir_graph *irg) {
                have Bad() predecessors. */
                set_irg_doms_inconsistent(irg);
        }
+       return env.changes != 0;
 }  /* optimize_load_store */
+
+ir_graph_pass_t *optimize_load_store_pass(const char *name)
+{
+       return def_graph_pass_ret(name ? name : "ldst", optimize_load_store);
+}  /* optimize_load_store_pass */