[LLVMdev] getelementptr on static const struct

Jonas Maebe jonas.maebe at elis.ugent.be
Thu Jan 30 09:46:38 PST 2014


On 29 Jan 2014, at 16:42, Katsuhiro Ueno wrote:

> I compiled the following code by clang -emit-llvm -S:
> 
> #include <stddef.h>
> static const struct t {char t[4]; char s;} p = {{}, 'a'};
> char f() {
>    return ((char*)&p)[offsetof(struct t, s)];
> }
> 
> then I obtained the following LLVM IR:
> 
> %struct.t = type { [4 x i8], i8 }
> @p = constant %struct.t { [4 x i8] zeroinitializer, i8 97 }, align 1
> define signext i8 @f() nounwind ssp uwtable {
>  %1 = load i8* getelementptr inbounds (%struct.t* @p, i32 0, i32 0,
> i64 4), align 1
>  ret i8 %1
> }

The GEP takes the address of the 5th element (= offset 4) of your "char t[4]". It's not entirely clear to me from the description of "inbounds" whether this means the the GEP results in a poison value or not, but it definitely looks wrong.

> By applying the above code to opt -O1 of LLVM 3.4, I obtained:
> 
> %struct.t = type { [4 x i8], i8 }
> @p = constant %struct.t { [4 x i8] zeroinitializer, i8 97 }, align 1
> define signext i8 @f() nounwind readnone ssp uwtable {
>  ret i8 0
> }

Given this result, I guess it is in fact a poison value.


Jonas



More information about the llvm-dev mailing list