+/* FIXME MMB: the memcpy is very strange */
+static int *resize (int *buf, int new_size) {
+ int *new_buf = (int *)calloc(new_size, sizeof(*new_buf));
+ memcpy(new_buf, buf, new_size>1);
+ free(buf);
+ return new_buf;
+}
+
+/* We sort the elements by placing them at their bit offset in an
+ array where each entry represents one bit called permutation. In
+ fact, we do not place the values themselves, as we would have to
+ copy two things, the value and the path. We only remember the
+ position in the old order. Each value should have a distinct
+ position in the permutation.
+
+ A second iteration now permutes the actual elements into two
+ new arrays. */
+void sort_compound_ent_values(entity *ent) {
+ type *tp;
+ int i, n_vals;
+ int tp_size;
+ int size;
+ int *permutation;
+
+ int next;
+ ir_node **my_values;
+ compound_graph_path **my_paths;
+
+ assert(get_type_state(get_entity_type(ent)) == layout_fixed);
+
+ tp = get_entity_type(ent);
+ n_vals = get_compound_ent_n_values(ent);
+ tp_size = get_type_size_bits(tp);
+
+ if (!is_compound_type(tp) ||
+ (ent->variability == variability_uninitialized) ||
+ (get_type_state(tp) != layout_fixed) ||
+ (n_vals == 0) ) return;
+
+ /* estimated upper bound for size. Better: use flexible array ... */
+ size = ((tp_size > (n_vals * 32)) ? tp_size : (n_vals * 32)) * 4;
+ permutation = calloc(size, sizeof(*permutation));
+ for (i = 0; i < n_vals; ++i) {
+ int pos = get_compound_ent_value_offset_bits(ent, i);
+ while (pos >= size) {
+ size = size + size;
+ permutation = resize(permutation, size);
+ }
+ assert(pos < size);
+ assert(permutation[pos] == 0 && "two values with the same offset");
+ permutation[pos] = i + 1; /* We initialized with 0, so we can not distinguish entry 0.
+ So inc all entries by one. */
+ //fprintf(stderr, "i: %d, pos: %d \n", i, pos);
+ }
+
+ next = 0;
+ my_values = NEW_ARR_F(ir_node *, n_vals);
+ my_paths = NEW_ARR_F(compound_graph_path *, n_vals);
+ for (i = 0; i < size; ++i) {
+ int pos = permutation[i];
+ if (pos) {
+ //fprintf(stderr, "pos: %d i: %d next %d \n", i, pos, next);
+ assert(next < n_vals);
+ pos--; /* We increased the pos by one */
+ my_values[next] = get_compound_ent_value (ent, pos);
+ my_paths [next] = get_compound_ent_value_path(ent, pos);
+ next++;
+ }
+ }
+ free(permutation);
+
+ DEL_ARR_F(ent->values);
+ ent->values = my_values;
+ DEL_ARR_F(ent->val_paths);
+ ent->val_paths = my_paths;
+}
+
+int
+(get_entity_offset_bytes)(const entity *ent) {
+ return __get_entity_offset_bytes(ent);
+}
+
+int
+(get_entity_offset_bits)(const entity *ent) {
+ return __get_entity_offset_bits(ent);
+}
+
+void
+(set_entity_offset_bytes)(entity *ent, int offset) {
+ __set_entity_offset_bytes(ent, offset);
+}
+
+void
+(set_entity_offset_bits)(entity *ent, int offset) {
+ __set_entity_offset_bits(ent, offset);
+}
+
+void
+add_entity_overwrites(entity *ent, entity *overwritten) {
+ assert(ent && is_class_type(get_entity_owner(ent)));
+ ARR_APP1(entity *, ent->overwrites, overwritten);
+ ARR_APP1(entity *, overwritten->overwrittenby, ent);
+}
+
+int
+get_entity_n_overwrites(entity *ent) {
+ assert(ent && is_class_type(get_entity_owner(ent)));
+ return (ARR_LEN(ent->overwrites));
+}
+
+int
+get_entity_overwrites_index(entity *ent, entity *overwritten) {
+ int i;
+ assert(ent && is_class_type(get_entity_owner(ent)));
+ for (i = 0; i < get_entity_n_overwrites(ent); i++)
+ if (get_entity_overwrites(ent, i) == overwritten)
+ return i;
+ return -1;
+}
+
+entity *