[llvm-dev] Prevent anonymous union optimization

Антон Кочков via llvm-dev llvm-dev at lists.llvm.org
Thu Jun 1 00:23:50 PDT 2017


Good time of the day!
I'm trying to make some transformations on generated LLVM IR and met a
case, which stops me from properly loading the used types. Basically,
I have some C code, that compiled into IR using clang -c -emit-llvm
and want to process the resulting file.
So, the problem is, that I have 2 nested (via anonymous union) structures:
typedef struct struct1 {
    unsigned int a;
    unsigned int b;
    int32_t c;
} struct1;

typedef struct struct2 {
    int q;
    int p;
    int d;
    union {
        unsigned int f;
        int64_t i;
        double v;
        struct1 m;
        int z;
        int x;
        int y;
    };
}

It generates:

struct.struct2 = type { i32, i32, i32, %union.anon }
union.anon = type { i64, [8 x i8] }
struct.struct1 = type { i32, i32, i32 }

And then performs bitcasts all the time it accesses that structure in the union.

If I change struct1 to
typedef struct struct1 {
    unsigned int a;
    unsigned int b;
    int32_t c;
    int64_t some_stupid_alignment;
} struct1;

Then it generates desired output of

struct.struct2 = type { i32, i32, i32, %union.anon }
union.anon = type { %struct.struct1 }
struct.struct1 = type { i32, i32, i32, i64 }

How to force it generate union.anon = type { %struct.struct1 } without
changing the struct1 (it's an external API so better to avoid the
changes here)?

Best regards,
Anton Kochkov.


More information about the llvm-dev mailing list