<p dir="ltr">You are right. What will happen is it will not look like a structure field access, and will use i8 or something so it can do the calculation.<br>
I'll paste a clang generated example in a bit</p>
<br><div class="gmail_quote"><div dir="ltr">On Fri, Jul 1, 2016, 11:13 AM Qin Zhao <<a href="mailto:zhaoqin@google.com">zhaoqin@google.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">zhaoqin added a comment.<br>
<br>
In <a href="http://reviews.llvm.org/D21594#472370" rel="noreferrer" target="_blank">http://reviews.llvm.org/D21594#472370</a>, @dberlin wrote:<br>
<br>
> > I do not think a negative index for struct field indexing is valid.<br>
><br>
> ><br>
><br>
> > ><br>
><br>
> ><br>
><br>
> ><br>
><br>
> > You are incorrect :)<br>
><br>
<br>
<br>
I see, thanks! Update CL.<br>
<br>
> ><br>
<br>
><br>
<br>
><br>
<br>
> FWIW, this is a common mistake.<br>
<br>
>  Please remember that LLVM level structs and GEP do not have the same rules<br>
<br>
>  and pretty much no relation to C-level structs.<br>
<br>
><br>
<br>
> GEP is just an indexing operation. Unlike C, i can form pointers to<br>
<br>
>  wherever i want, and even if i mark them inbounds, your only guarantee is<br>
<br>
>  that they are in the object somewhere, you have no guarantee on whether i<br>
<br>
>  use positive or negative gep indices to generate those pointers and either<br>
<br>
>  way is just as valid.<br>
<br>
>  As mentioned, there are plenty of cases frontends will generate negative<br>
<br>
>  gep indices for normal source level field accesses of C-level structs.<br>
<br>
<br>
I understand that GEP is just an indexing operation, which I treat it like LEA instr in x86.<br>
One thing I do not understand is how the negative index would work for a struct type.<br>
For array, because we know the element size, we can use positive or negative indexing to calculate an address/offset from the base.<br>
However for struct, if the index is not within the struct field range, i.e., negative or too large, how does the compiler know how the offset/address is calculated?<br>
For example, if the index is -1, how does the compiler decide the first negative struct field size?<br>
I would expect the compiler will generate an error on fail to compute the offset.<br>
Will there be a default size if no field size information available for the out of range index?<br>
<br>
<br>
<a href="http://reviews.llvm.org/D21594" rel="noreferrer" target="_blank">http://reviews.llvm.org/D21594</a><br>
<br>
<br>
<br>
</blockquote></div>