<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Jul 1, 2016 at 3:34 PM, Qin Zhao <span dir="ltr"><<a href="mailto:zhaoqin@google.com" target="_blank">zhaoqin@google.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">zhaoqin added a comment.<br>
<span class=""><br>
In <a href="http://reviews.llvm.org/D21594#472524" rel="noreferrer" target="_blank">http://reviews.llvm.org/D21594#472524</a>, @dberlin wrote:<br>
<br>
> You are right. What will happen is it will not look like a structure field<br>
>  access, and will use i8 or something so it can do the calculation.<br>
>  I'll paste a clang generated example in a bit<br>
<br>
<br>
</span>I just tried a manual test example as below:<br>
<br>
%struct.RT = type { i8, [10 x [20 x i32]], i8 }<br>
%struct.ST = type { i32, double, %struct.RT }<br>
<br>
define i32* @foo(%struct.ST* %s) nounwind uwtable readnone optsize ssp {<br>
entry:<br>
<br>
  %arrayidx = getelementptr inbounds %struct.ST, %struct.ST* %s, i64 0, i32 -1<br>
  ret i32* %arrayidx<br>
<br>
}<br>
<br>
which I tried gep on struct.ST with a negative field.<br>
And I get the error that invalid getelementptr indices:<br></blockquote><div><br></div><div>Right, as I said, i was wrong, you can only get negative indices on pointer and array types.</div><div> </div><div>However, you have a worse situation - not every field access is going to look like an access through a structure type ;)</div><div><br></div><div>Clang will happily generate code like this:<br><span style="color:rgb(80,0,80);font-size:12.8px">  %20 = load %struct.A*, %struct.A** %3, align 8</span><br style="color:rgb(80,0,80);font-size:12.8px"><span style="color:rgb(80,0,80);font-size:12.8px">  %21 = bitcast %struct.A* %20 to i8*</span><br style="color:rgb(80,0,80);font-size:12.8px"><span style="color:rgb(80,0,80);font-size:12.8px">  %22 = getelementptr inbounds i8, i8* %21, i64 %19</span></div><div><span style="color:rgb(80,0,80);font-size:12.8px"><br></span></div><div><span style="color:rgb(80,0,80);font-size:12.8px">....</span></div><div><span style="color:rgb(80,0,80);font-size:12.8px"><br></span></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">
<br>
$ /usr/local/google/home/zhaoqin/Workspace/LLVM/builds/build_x64_rel.git/./bin/opt < /usr/local/google/home/zhaoqin/Workspace/LLVM/llvm.git/test/Instrumentation/EfficiencySanitizer/struct_field_gep.ll -esan -esan-cache-frag -S<br>
/usr/local/google/home/zhaoqin/Workspace/LLVM/builds/build_x64_rel.git/./bin/opt: <stdin>:28:50: error: invalid getelementptr indices<br>
<br>
  %arrayidx = getelementptr inbounds %struct.ST, %struct.ST* %s, i64 0, i32 -1<br>
                                                                                 ^<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><br></div></div>