+/**
+ * Returns an entity if the address ptr points to a constant one.
+ */
+static 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)) {
+ return get_SymConst_entity(ptr);
+ }
+ else if (op == op_Sel) {
+ entity *ent = get_Sel_entity(ptr);
+ ir_type *tp = get_entity_owner(ent);
+
+ /* Do not fiddle with polymorphism. */
+ if (is_Class_type(get_entity_owner(ent)) &&
+ ((get_entity_n_overwrites(ent) != 0) ||
+ (get_entity_n_overwrittenby(ent) != 0) ) )
+ return NULL;
+
+ if (variability_constant == get_entity_variability(ent))
+ return ent;
+
+ if (is_Array_type(tp)) {
+ /* check bounds */
+ int i, n;
+
+ for (i = 0, n = get_Sel_n_indexs(ptr); i < n; ++i) {
+ ir_node *bound;
+ tarval *tlower, *tupper;
+ ir_node *index = get_Sel_index(ptr, i);
+ tarval *tv = computed_value(index);
+
+ /* check if the index is constant */
+ if (tv == tarval_bad)
+ return NULL;
+
+ bound = get_array_lower_bound(tp, i);
+ tlower = computed_value(bound);
+ bound = get_array_upper_bound(tp, i);
+ tupper = computed_value(bound);
+
+ if (tlower == tarval_bad || tupper == tarval_bad)
+ return NULL;
+
+ if (tarval_cmp(tv, tlower) & pn_Cmp_Lt)
+ return NULL;
+ if (tarval_cmp(tupper, tv) & pn_Cmp_Lt)
+ return NULL;
+
+ /* ok, bounds check finished */
+ }
+ }
+
+ /* try next */
+ ptr = get_Sel_ptr(ptr);
+ }
+ else
+ return NULL;
+ }
+}
+
+/**
+ * Return the Selection index of a Sel node from dimension n
+ */
+static long get_Sel_array_index_long(ir_node *n, int dim) {
+ ir_node *index = get_Sel_index(n, dim);
+ assert(get_irn_op(index) == op_Const);
+ return get_tarval_long(get_Const_tarval(index));
+}
+
+/**
+ * Returns the accessed component graph path for an
+ * node computing an address.
+ *
+ * @param ptr the node computing the address
+ * @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) {
+ compound_graph_path *res = NULL;
+ entity *root, *field;
+ int path_len, pos;
+
+ if (get_irn_op(ptr) == op_SymConst) {
+ /* 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.
+ */
+ 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);
+ /* it's a Sel, go up until we find the root */
+ res = rec_get_accessed_path(get_Sel_ptr(ptr), depth+1);
+
+ /* fill up the step in the path at the current position */
+ field = get_Sel_entity(ptr);
+ path_len = get_compound_graph_path_length(res);
+ pos = path_len - depth - 1;
+ set_compound_graph_path_node(res, pos, field);
+
+ if (is_Array_type(get_entity_owner(field))) {
+ 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));
+ }
+ }
+ return res;
+}
+
+/** 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);
+}
+
+/* forward */
+static void reduce_adr_usage(ir_node *ptr);
+
+/**
+ * Update a Load that may lost it's usage.
+ */
+static void handle_load_update(ir_node *load) {
+ ldst_info_t *info = get_irn_link(load);
+
+ /* do NOT touch volatile loads for now */
+ if (get_Load_volatility(load) == volatility_is_volatile)
+ return;
+
+ if (! info->projs[pn_Load_res] && ! info->projs[pn_Load_X_except]) {
+ 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 */
+ exchange(info->projs[pn_Load_M], mem);
+ reduce_adr_usage(ptr);
+ }
+}
+
+/**
+ * A Use of an address node is vanished. Check if this was a Proj
+ * node and update the counters.
+ */
+static void reduce_adr_usage(ir_node *ptr) {
+ int use_count = get_irn_n_uses(ptr);
+ --use_count;
+ assert(use_count >= 0);
+ set_irn_n_uses(ptr, use_count);
+
+ if (is_Proj(ptr)) {
+ if (use_count <= 0) {
+ /* this Proj is now dead, update the Load/Store info */
+ ir_node *pred = get_Proj_pred(ptr);
+ opcode code = get_irn_opcode(pred);
+
+ if (code == iro_Load) {
+ ldst_info_t *info = get_irn_link(pred);
+ info->projs[get_Proj_proj(ptr)] = NULL;
+
+ /* this node lost it's result proj, handle that */
+ handle_load_update(pred);
+ }
+ }
+ }
+}
+
+/**
+ * Follow the memory chain as long as there are only Loads
+ * and try to replace current Load or Store by a previous one.
+ * Note that in unreachable loops it might happen that we reach
+ * load again, as well as we can fall into a cycle.
+ * We break such cycles using a special visited flag.
+ *
+ * INC_MASTER() must be called before dive into
+ */
+static unsigned follow_Load_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);
+
+ for (pred = curr; load != pred; pred = skip_Proj(get_Load_mem(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 ...
+ */
+
+ if (get_irn_op(pred) == op_Store && get_Store_ptr(pred) == ptr &&
+ 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 block. 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 block :-(
+ * 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] && !info->projs[pn_Load_X_except]) ||
+ get_nodes_block(load) == get_nodes_block(pred)) {
+ ir_node *value = get_Store_value(pred);
+
+ DBG_OPT_RAW(load, value);
+ 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_res])
+ exchange(info->projs[pn_Load_res], value);
+
+ reduce_adr_usage(ptr);
+ return res | DF_CHANGED;
+ }
+ }
+ else if (get_irn_op(pred) == op_Load && get_Load_ptr(pred) == ptr &&
+ get_Load_mode(pred) == load_mode) {
+ /*
+ * a Load after a Load -- a read after read.
+ * We may remove the second Load, if it does not have an exception handler
+ * OR they are in the same block. In the later case the Load cannot
+ * throw an exception when the previous Load was quiet.
+ *
+ * Here, there is no need to check if the previous Load has an exception
+ * hander because they would have exact the same exception...
+ */
+ if (! info->projs[pn_Load_X_except] || get_nodes_block(load) == get_nodes_block(pred)) {
+ DBG_OPT_RAR(load, pred);
+
+ if (pred_info->projs[pn_Load_res]) {
+ /* we need a data proj from the previous load for this optimization */
+ if (info->projs[pn_Load_res])
+ exchange(info->projs[pn_Load_res], pred_info->projs[pn_Load_res]);
+
+ if (info->projs[pn_Load_M])
+ exchange(info->projs[pn_Load_M], mem);
+ }
+ else {
+ if (info->projs[pn_Load_res]) {
+ set_Proj_pred(info->projs[pn_Load_res], pred);
+ set_nodes_block(info->projs[pn_Load_res], get_nodes_block(pred));
+ pred_info->projs[pn_Load_res] = info->projs[pn_Load_res];
+ }
+ if (info->projs[pn_Load_M]) {
+ /* Actually, this if should not be necessary. Construct the Loads
+ properly!!! */
+ 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;
+ }
+
+ reduce_adr_usage(ptr);
+ return res |= DF_CHANGED;
+ }
+ }
+
+ /* follow only Load chains */
+ if (get_irn_op(pred) != op_Load)
+ break;
+
+ /* check for cycles */
+ if (NODE_VISITED(pred_info))
+ break;
+ MARK_NODE(pred_info);
+ }
+
+ if (get_irn_op(pred) == op_Sync) {
+ int i;
+
+ /* handle all Sync predecessors */
+ for (i = get_Sync_n_preds(pred) - 1; i >= 0; --i) {
+ res |= follow_Load_chain(load, skip_Proj(get_Sync_pred(pred, i)));
+ if (res)
+ break;
+ }
+ }
+
+ return res;
+}
+