+#ifdef HAVE_ALLOCA_H
+#include <alloca.h>
+#endif
+
+#ifdef HAVE_MALLOC_H
+#include <malloc.h>
+#endif
+
+#ifdef HAVE_STRING_H
+#include <string.h>
+#endif
+
+#include "scalar_replace.h"
+#include "irflag_t.h"
+#include "irouts.h"
+#include "set.h"
+#include "pset.h"
+#include "array.h"
+#include "tv.h"
+#include "ircons_t.h"
+#include "hashptr.h"
+#include "irgwalk.h"
+#include "irgmod.h"
+#include "irnode_t.h"
+#include "irtools.h"
+
+#define SET_VNUM(node, vnum) set_irn_link(node, INT_TO_PTR(vnum))
+#define GET_VNUM(node) (unsigned)PTR_TO_INT(get_irn_link(node))
+
+/**
+ * A path element entry: it is either an entity
+ * or a tarval, because we evaluate only constant array
+ * accesses like a.b.c[8].d
+ */
+typedef union {
+ entity *ent;
+ tarval *tv;
+} path_elem_t;
+
+/**
+ * An access path, used to assign value numbers
+ * to variables that will be scalar replaced
+ */
+typedef struct _path_t {
+ unsigned vnum; /**< the value number */
+ unsigned path_len; /**< the length of the access path */
+ path_elem_t path[1]; /**< the path */
+} path_t;
+
+typedef struct _scalars_t {
+ entity *ent; /**< A entity for scalar replacement. */
+ ir_type *ent_owner; /**< The owner of this entity. */
+} scalars_t;
+
+
+/**
+ * Compare two pathes.
+ *
+ * @return 0 if they are identically
+ */
+static int path_cmp(const void *elt, const void *key, size_t size)
+{
+ const path_t *p1 = elt;
+ const path_t *p2 = key;
+
+ /* we can use memcmp here, because identical tarvals should have identical addresses */
+ return memcmp(p1->path, p2->path, p1->path_len * sizeof(p1->path[0]));
+}
+
+/**
+ * Compare two elements of the scalars_t set.
+ *
+ * @return 0 if they are identically
+ */
+static int ent_cmp(const void *elt, const void *key, size_t size)
+{
+ const scalars_t *c1 = elt;
+ const scalars_t *c2 = key;
+
+ return c1->ent != c2->ent;
+}
+
+/**
+ * Calculate a hash value for a path.
+ */
+static unsigned path_hash(const path_t *path)
+{
+ unsigned hash = 0;
+ unsigned i;
+
+ for (i = 0; i < path->path_len; ++i)
+ hash ^= (unsigned)PTR_TO_INT(path->path[i].ent);
+
+ return hash >> 4;
+}
+
+/**
+ * Returns non-zero, if all indeces of a Sel node are constants.
+ *
+ * @param sel the Sel node that will be checked
+ */
+static int is_const_sel(ir_node *sel) {
+ int i, n = get_Sel_n_indexs(sel);
+
+ for (i = 0; i < n; ++i) {
+ ir_node *idx = get_Sel_index(sel, i);
+
+ if (get_irn_op(idx) != op_Const)
+ return 0;
+ }
+ return 1;
+}
+
+/*
+ * Returns non-zero, if the address of an entity
+ * represented by a Sel node (or it's successor Sels) is taken.
+ */
+int is_address_taken(ir_node *sel)
+{
+ int i;
+
+ if (! is_const_sel(sel))
+ return 1;
+
+ for (i = get_irn_n_outs(sel) - 1; i >= 0; --i) {
+ ir_node *succ = get_irn_out(sel, i);
+
+ switch (get_irn_opcode(succ)) {
+ case iro_Load:
+ /* ok, we just load from that entity */
+ break;
+
+ case iro_Store:
+ /* check that Sel is not the Store's value */
+ if (get_Store_value(succ) == sel)
+ return 1;
+ break;
+
+ case iro_Sel: {
+ /* Check the Sel successor of Sel */
+ int res = is_address_taken(succ);
+
+ if (res)
+ return 1;
+ break;
+ }
+
+ case iro_Call:
+ /* The address of an entity is given as a parameter.
+ * As long as we do not have analyses that can tell what
+ * is done with parameters, think is taken.
+ */
+ return 1;
+
+ default:
+ /* another op, the address is taken */
+ return 1;
+ }
+ }
+ return 0;
+}
+
+/**
+ * Link all leave Sels with the entity.
+ *
+ * @param ent the entity that will be scalar replaced
+ * @param sel a Sel node that selects some fields of this entity
+ */
+static void link_all_leave_sels(entity *ent, ir_node *sel)
+{
+ int i, n, flag = 1;
+
+ n = get_irn_n_outs(sel);
+ for (i = 0; i < n; ++i) {
+ ir_node *succ = get_irn_out(sel, i);
+
+ if (is_Sel(succ)) {
+ link_all_leave_sels(ent, succ);
+ flag = 0;
+ }
+ }
+
+ if (flag) {
+ /* if Sel nodes with memory inputs are used, a entity can be
+ * visited more than once causing a ring here, so we use the
+ * node flag to mark linked nodes
+ */
+ if (irn_visited(sel))
+ return;
+
+ /* we know we are at a leave, because this function is only
+ * called if the address is NOT taken, so succ must be a Load
+ * or a Store node
+ */
+ set_irn_link(sel, get_entity_link(ent));
+ set_entity_link(ent, sel);
+
+ mark_irn_visited(sel);
+ }
+}
+
+/* we need a special address that serves as an address taken marker */
+static char _x;
+static void *ADDRESS_TAKEN = &_x;
+
+/**
+ * Find possible scalar replacements.
+ *
+ * @param irg an IR graph
+ *
+ * This function finds variables on the (members of the) frame type
+ * that can be scalar replaced, because their address is never taken.
+ * If such a variable is found, it's entity link will hold a list of all
+ * Sel nodes, that selects the atomic fields of this entity.
+ * Otherwise, the link will be ADDRESS_TAKEN or NULL.
+ *
+ * @return non-zero if at least one entity could be replaced
+ * potentially
+ */
+static int find_possible_replacements(ir_graph *irg)
+{
+ ir_node *irg_frame = get_irg_frame(irg);
+ int i, n;
+ int res = 0;
+
+ inc_irg_visited(irg);
+
+ n = get_irn_n_outs(irg_frame);
+
+ /*
+ * First, clear the link field of all interesting entities.
+ * Note that we did not rely on the fact that there is only
+ * one Sel node per entity, so we might access one entity
+ * more than once here.
+ * That's why we have need two loops.
+ */
+ for (i = 0; i < n; ++i) {
+ ir_node *succ = get_irn_out(irg_frame, i);
+
+ if (is_Sel(succ)) {
+ entity *ent = get_Sel_entity(succ);
+ set_entity_link(ent, NULL);
+ }
+ }
+
+ /*
+ * Check the ir_graph for Sel nodes. If the entity of Sel
+ * isn't a scalar replacement set the link of this entity
+ * equal ADDRESS_TAKEN.
+ */
+ for (i = 0; i < n; ++i) {
+ ir_node *succ = get_irn_out(irg_frame, i);
+
+ if (is_Sel(succ)) {
+ entity *ent = get_Sel_entity(succ);
+ ir_type *ent_type;
+
+ if (get_entity_link(ent) == ADDRESS_TAKEN)
+ continue;
+
+ ent_type = get_entity_type(ent);
+
+ /* we can handle arrays, structs and atomic types yet */
+ if (is_Array_type(ent_type) || is_Struct_type(ent_type) || is_atomic_type(ent_type)) {
+ if (is_address_taken(succ)) {
+ if (get_entity_link(ent)) /* killing one */
+ --res;
+ set_entity_link(ent, ADDRESS_TAKEN);
+ }
+ else {
+ /* possible found one */
+ if (get_entity_link(ent) == NULL)
+ ++res;
+ link_all_leave_sels(ent, succ);
+ }
+ }
+ }
+ }
+
+ return res;
+}
+
+/**
+ * Return a path from the Sel node sel to it's root.
+ *
+ * @param sel the Sel node
+ * @param len the length of the path so far
+ */
+static path_t *find_path(ir_node *sel, unsigned len)
+{
+ int pos, i, n;
+ path_t *res;
+ ir_node *pred = get_Sel_ptr(sel);
+
+ /* the current Sel node will add some path elements */
+ n = get_Sel_n_indexs(sel);
+ len += n + 1;
+
+ if (! is_Sel(pred)) {
+ /* we found the root */
+
+ res = xmalloc(sizeof(*res) + (len - 1) * sizeof(res->path));
+ res->path_len = len;
+ }
+ else
+ res = find_path(pred, len);
+
+ pos = res->path_len - len;
+
+ res->path[pos++].ent = get_Sel_entity(sel);
+ for (i = 0; i < n; ++i) {
+ ir_node *index = get_Sel_index(sel, i);
+
+ res->path[pos++].tv = get_Const_tarval(index);
+ }
+ return res;
+}
+
+
+/**
+ * Allocate value numbers for the leaves
+ * in our found entities.
+ *
+ * @param sels a set that will contain all Sels that have a value number
+ * @param ent the entity that will be scalar replaced
+ * @param vnum the first value number we can assign
+ * @param modes a flexible array, containing all the modes of
+ * the value numbers.
+ *
+ * @return the next free value number
+ */
+static unsigned allocate_value_numbers(pset *sels, entity *ent, unsigned vnum, ir_mode ***modes)
+{
+ ir_node *sel, *next;
+ path_t *key, *path;
+ set *pathes = new_set(path_cmp, 8);
+
+ /* visit all Sel nodes in the chain of the entity */
+ for (sel = get_entity_link(ent); sel; sel = next) {
+ next = get_irn_link(sel);
+
+ /* we must mark this sel for later */
+ pset_insert_ptr(sels, sel);
+
+ key = find_path(sel, 0);
+ path = set_find(pathes, key, sizeof(*key) + sizeof(key->path[0]) * key->path_len, path_hash(key));
+
+ if (path)
+ SET_VNUM(sel, path->vnum);
+ else {
+ unsigned i;
+
+ key->vnum = vnum++;
+
+ set_insert(pathes, key, sizeof(*key) + sizeof(key->path[0]) * key->path_len, path_hash(key));
+
+ SET_VNUM(sel, key->vnum);
+ ARR_EXTO(ir_mode *, *modes, (key->vnum + 15) & ~15);
+
+ (*modes)[key->vnum] = get_type_mode(get_entity_type(get_Sel_entity(sel)));
+
+ assert((*modes)[key->vnum] && "Value is not atomic");