On Mon, Dec 16, 2019 at 3:24 PM Erick Ochoa <erick.oc...@theobroma-systems.com> wrote: > > Hello, > > I am working on a struct reorganization optimization pass. > I am able to identify which structs I want to reorganize > and I am also able to create new structs with these modifications. > The way the new structs are generated is the following code > (I am keeping it high-level for conciseness but feel free to ask > for more details). > > static tree > get_sorted_record(tree record) > { > gcc_assert(TREE_CODE(record) == RECORD_TYPE); > log(2, "printing original"); > print_record(record); > tree copy = copy_record(record); > sort_fields_in_place(copy); > log(2, "printing sorted copy"); > print_record(copy); > return copy; > } > > Output: > > [log](/home/eochoa/code/gcc/gcc/ipa-hello-world.c:void > print_record(tree):183)printing record aStruct > [log](/home/eochoa/code/gcc/gcc/ipa-hello-world.c:void > print_field(tree):199)e,boolean_type,8 > [log](/home/eochoa/code/gcc/gcc/ipa-hello-world.c:void > print_field(tree):199)a,integer_type,32 > [log](/home/eochoa/code/gcc/gcc/ipa-hello-world.c:void > print_field(tree):199)b,integer_type,8 > [log](/home/eochoa/code/gcc/gcc/ipa-hello-world.c:void > print_field(tree):199)c,real_type,32 > [log](/home/eochoa/code/gcc/gcc/ipa-hello-world.c:void > print_field(tree):199)d,real_type,64 > [log](/home/eochoa/code/gcc/gcc/ipa-hello-world.c:void > print_field(tree):199)f,boolean_type,8 > [log](/home/eochoa/code/gcc/gcc/ipa-hello-world.c:void > print_field(tree):199)h,integer_type,64 > [log](/home/eochoa/code/gcc/gcc/ipa-hello-world.c:tree_node* > get_sorted_record(tree):218)printing sorted copy > [log](/home/eochoa/code/gcc/gcc/ipa-hello-world.c:void > print_record(tree):183)printing record aStruct > [log](/home/eochoa/code/gcc/gcc/ipa-hello-world.c:void > print_field(tree):199)e,boolean_type,8 > [log](/home/eochoa/code/gcc/gcc/ipa-hello-world.c:void > print_field(tree):199)b,integer_type,8 > [log](/home/eochoa/code/gcc/gcc/ipa-hello-world.c:void > print_field(tree):199)f,boolean_type,8 > [log](/home/eochoa/code/gcc/gcc/ipa-hello-world.c:void > print_field(tree):199)a,integer_type,32 > [log](/home/eochoa/code/gcc/gcc/ipa-hello-world.c:void > print_field(tree):199)c,real_type,32 > [log](/home/eochoa/code/gcc/gcc/ipa-hello-world.c:void > print_field(tree):199)d,real_type,64 > [log](/home/eochoa/code/gcc/gcc/ipa-hello-world.c:void > print_field(tree):199)h,integer_type,64 > > Basically, we copy the tree of structs of interest. > We modify the DECL_CHAIN so that the fields are in the order that > we want. And finally, we would like to replace trees of type `record` > with `copy`. > > At the moment, IDENTIFIER_POINTER for copy and record are the same. > However, I want to keep my design as general as possible. > If I understand correctly, with the current design it is possible > to change all trees of type record with those of type copy. > However, if one wished to change only a subset of these trees, > wouldn't there be a need for a different IDENTIFIER_NODE in clone? > Otherwise, there would be confusion as to how clone and record > differ. (Their IDENTIFIER_POINTERS are the same.) > > So, is the correct way to specialize a type is to modify the > identifier myself or is there an API that allows me to do so?
This is what I did: + if (TYPE_NAME (type) != NULL) + { + if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE) + tname = IDENTIFIER_POINTER (TYPE_NAME (type)); + else if (DECL_NAME (TYPE_NAME (type)) != NULL) + tname = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))); + } ... + char id[10]; + sprintf(id, "%d", i); + if (tname) + { + name = concat (tname, ".reorg.", id, NULL); + TYPE_NAME (newtype[i]) = get_identifier (name); + free (name); + } Where i is the nth new type for the original type. For the fields that have a new type, I did this: + if (nt[1] != NULL && DECL_NAME (fielddecl)) + { + const char *tname = IDENTIFIER_POINTER (DECL_NAME (fielddecl)); + char id[10]; + char *name; + + sprintf(id, "%d", i); + name = concat (tname, ".reorg.", id, NULL); + DECL_NAME (field) = get_identifier (name); + free (name); + } + else + DECL_NAME (field) = DECL_NAME (fielddecl); Where nt was the new types for that field. Thanks, Andrew Pinski > > Thanks!