<div dir="ltr"><div>Hello,</div><div>I did some more digging and I think that those numbers where indeed correct:</div><div><br></div><div>```</div><div>➜  /tmp cat struct.c <br>#include<stdio.h><br>  <br>struct Point<br>{<br>   int x;<br>   char y;<br>   char z;<br>   long w;<br>};<br>  <br>int main()<br>{<br>   struct Point p1;<br>  <br>   // Accessing members of point p1<br>   p1.x = 1;<br>   p1.y = 2;<br>   p1.z = 3;<br>   p1.w = 4;<br><br>   printf("p1: 0x%x\n", &p1);<br>   printf("p1.x: 0x%x\n", &p1.x);<br>   printf("p1.y: 0x%x\n", &p1.y);<br>   printf("p1.z: 0x%x\n", &p1.z);<br>   printf("p1.w: 0x%x\n", &p1.w);<br>   return 0;<br>}<br>➜  /tmp <br></div><div>```</div><div>➜  /tmp ./struct    <br>p1: 0x9791b30<br>p1.x: 0x9791b30<br>p1.y: 0x9791b34<br>p1.z: 0x9791b35<br>p1.w: 0x9791b38<br>➜  /tmp <br></div><div>```</div><div><br></div><div>```</div><div>  %3 = getelementptr inbounds %struct.Point, %struct.Point* %2, i32 0, i32 0<br>ap_offset: 0<br>Accumulated offset: 1<br>ap_offset: 0<br>  %4 = getelementptr inbounds %struct.Point, %struct.Point* %2, i32 0, i32 1<br>ap_offset: 0<br>Accumulated offset: 1<br>ap_offset: 4<br>  %5 = getelementptr inbounds %struct.Point, %struct.Point* %2, i32 0, i32 2<br>ap_offset: 0<br>Accumulated offset: 1<br>ap_offset: 5<br>  %6 = getelementptr inbounds %struct.Point, %struct.Point* %2, i32 0, i32 3<br>ap_offset: 0<br>Accumulated offset: 1<br>ap_offset: 8<br></div><div>```</div><div><br></div><div>Let me know if I'm missing anything.</div><div><br></div><div>Thanks</div><div>Alberto<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Il giorno mar 25 gen 2022 alle ore 20:03 Alberto Barbaro <<a href="mailto:barbaro.alberto@gmail.com">barbaro.alberto@gmail.com</a>> ha scritto:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div>Hi Nikita,</div><div>so I think I made some progress but I'm not quite there yet.</div><div><br></div><div>So the GEP Instruction that I'm interested to analyze are:</div><div><br></div><div>%3 = getelementptr inbounds %struct.Point, %struct.Point* %2, i32 0, i32 0<br>%4 = getelementptr inbounds %struct.Point, %struct.Point* %2, i32 0, i32 1<br>%5 = getelementptr inbounds %struct.Point, %struct.Point* %2, i32 0, i32 2</div><div><br></div><div>and the Point struct is declared in the following way:</div><div><br></div><div>struct Point<br>{<br>   int x;<br>   char y;<br>   long z;<br>};</div><div><br></div><div>Reading some doc online I tried the following:</div><div><br></div><div>Module *M = I.getModule();</div><div>I.dump();</div><div>APInt ap_offset(32, 0, false);<br>std::cout << "ap_offset: " << ap_offset.getSExtValue() << "\n";<br>std::cout << "Accumulated offset: " << I.accumulateConstantOffset(M->getDataLayout(), ap_offset) << "\n";<br>std::cout << "ap_offset: " << ap_offset.getSExtValue() << "\n";<br></div><div><br></div><div>The output is something like:</div><div>  %3 = getelementptr inbounds %struct.Point, %struct.Point* %2, i32 0, i32 0<br>ap_offset: 0<br>Accumulated offset: 1<br>ap_offset: 0<br>  %4 = getelementptr inbounds %struct.Point, %struct.Point* %2, i32 0, i32 1<br>ap_offset: 0<br>Accumulated offset: 1<br>ap_offset: 4<br>  %5 = getelementptr inbounds %struct.Point, %struct.Point* %2, i32 0, i32 2<br>ap_offset: 0<br>Accumulated offset: 1<br>ap_offset: 8<br>  %3 = getelementptr inbounds %struct.Point, %struct.Point* %2, i32 0, i32 0<br>ap_offset: 0<br>Accumulated offset: 1<br>ap_offset: 0<br>  %4 = getelementptr inbounds %struct.Point, %struct.Point* %2, i32 0, i32 1<br>ap_offset: 0<br>Accumulated offset: 1<br>ap_offset: 4<br>  %5 = getelementptr inbounds %struct.Point, %struct.Point* %2, i32 0, i32 2<br>ap_offset: 0<br>Accumulated offset: 1<br>ap_offset: 8<br>  %3 = getelementptr inbounds %struct.Point, %struct.Point* %2, i32 0, i32 0<br>ap_offset: 0<br>Accumulated offset: 1<br>ap_offset: 0<br>  %4 = getelementptr inbounds %struct.Point, %struct.Point* %2, i32 0, i32 1<br>ap_offset: 0<br>Accumulated offset: 1<br>ap_offset: 4<br>  %5 = getelementptr inbounds %struct.Point, %struct.Point* %2, i32 0, i32 2<br>ap_offset: 0<br>Accumulated offset: 1<br>ap_offset: 8</div><div><br></div><div>I think the output is almost correct because the offset is increased by 4 each time... but the struct has char and long elements so the offset does not always look right. I think it due to the fact that the GEP is referring only to i32. <br></div><div><br></div><div>How should I fix this situation?  Few lines of code would be very helpful</div><div><br></div><div>Thanks</div><div>Alberto<br></div><div>    </div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Il giorno mar 25 gen 2022 alle ore 08:25 Nikita Popov <<a href="mailto:nikita.ppv@gmail.com" target="_blank">nikita.ppv@gmail.com</a>> ha scritto:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Jan 25, 2022 at 8:00 AM Alberto Barbaro via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div>Hi all,</div><div>so I'm trying to understand how to manually calculate the offset calculated by a GEP instruction. I found that this question was asked over 6 years ago on stackoverflow[1] as well but never got a real answer.</div><div><br></div><div>Since I need exactly the same, is there anyone willing to help me to understand how to calculate the offset?<br>
</div></div></blockquote><div><br></div><div>You can use GEPOperator::accumulateConstantOffset(). Or more generically, there is Value::stripAndAccumulateConstantOffsets(), which can look through multiple GEPs, bitcasts, etc.</div><div><br></div><div>Regards,</div><div>Nikita <br></div></div></div>
</blockquote></div>
</blockquote></div>