Unaligned loads

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)
Thank you,


target datalayout = “e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64”
target triple = “x86_64-unknown-linux-gnu”

define i32 @f(i8* nocapture %ptr) nounwind readonly {
%add.ptr = getelementptr inbounds i8* %ptr, i64 2
%0 = bitcast i8* %add.ptr to i32*
%tmp1 = load i32* %0, align 4
ret i32 %tmp1

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.