Unaligned loads

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Unaligned loads

Damien Vincent
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,

Damien


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 {
entry:
  %add.ptr = getelementptr inbounds i8* %ptr, i64 2
  %0 = bitcast i8* %add.ptr to i32*
  %tmp1 = load i32* %0, align 4
  ret i32 %tmp1
}


_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Unaligned loads

John McCall
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.

John.
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev