On Mar 30, 2011, at 12:10 PM, Damien Vincent wrote:
> I generated the llvm code for the following simple function (based on clang/llvm 2.8) using:
> clang -S -O2 -emit-llvm memory_unalignedint.c
> int f(char *ptr)
> int r = *((int*)(ptr+2));
> return r;
> And I was wondering why the alignment for the load is set to 4 although the load is not aligned ? (%tmp1 = load i32* %0, align 4)
This load is required to be 4-byte aligned; the fact that the address is computed with pointer arithmetic doesn't matter. The standards-compliant way of loading an unaligned object is memcpy, which LLVM will happily optimize to an unaligned load/store if possible.
We also support the __attribute__((aligned(N))) GCC extension to do this more naturally; this would look something like:
typedef int unaligned_int __attribute__((aligned(1)));
int r = *((unaligned_int*) (ptr+2));
However, we have a pretty large number of bugs in this area, and in particular the load here does not seem to get emitted with align 1.