<div dir="ltr"><div class="gmail_default" style="font-size:small">Here is an example:</div><div class="gmail_default" style="font-size:small"><br></div><div class="gmail_default" style="font-size:small">I can define two classes: A and Apad:</div><div class="gmail_default" style="font-size:small"><br></div><div class="gmail_default"><div class="gmail_default">class A {</div><div class="gmail_default"> bool b1, b2;</div><div class="gmail_default"> double d1;</div><div class="gmail_default"> int i1;</div><div class="gmail_default">}; </div><div class="gmail_default"><br></div><div class="gmail_default">class Apad {</div><div class="gmail_default"> bool b1, b2;</div><div class="gmail_default"> bool pad1[6];</div><div class="gmail_default"> double d1;</div><div class="gmail_default"> int i1;</div><div class="gmail_default"> bool pad2[4];</div><div class="gmail_default">}; </div><div style="font-size:small"><br></div><div style="font-size:small">A and Apad will have the same layout, from the LLVM IR level:</div><div style="font-size:small"><br></div><div>%class.A = type <{ i8, i8, [6 x i8], double, i32, [4 x i8] }><br></div><div>%class.Apad = type { i8, i8, [6 x i8], double, i32, [4 x i8] }<br></div></div><div class="gmail_extra"><br clear="all"><div><div class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr">Regards,<div>Hu Hong</div></div></div></div>
<br><div class="gmail_quote">On 29 December 2016 at 10:44, Hong Hu <span dir="ltr"><<a href="mailto:huhong789@gmail.com" target="_blank">huhong789@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div style="font-size:small">Thanks, Eli.</div><div style="font-size:small"><br></div><div style="font-size:small">Next question is how to get the layout of the original C++ class from LLVM IR? </div><div class="gmail_extra"><br clear="all"><div><div class="m_3927231417433861438gmail_signature" data-smartmail="gmail_signature"><div dir="ltr">Regards,<div>Hu Hong</div></div></div></div><div><div class="h5">
<br><div class="gmail_quote">On 29 December 2016 at 01:57, Friedman, Eli <span dir="ltr"><<a href="mailto:efriedma@codeaurora.org" target="_blank">efriedma@codeaurora.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div bgcolor="#FFFFFF" text="#000000"><span>
<div class="m_3927231417433861438m_8347775751878978904moz-cite-prefix">On 12/28/2016 5:41 AM, Hong Hu via
llvm-dev wrote:<br>
</div>
<blockquote type="cite">
<div dir="ltr">
<div style="font-size:small">Hi all,</div>
<div style="font-size:small"><br>
</div>
<div style="font-size:small">I'm writing a
pass to understand the memory access to C++ class members. For
each GetElementPtr instruction, I check the second index to
the class pointer, to figure out which member it is intended
to access. </div>
<div style="font-size:small"><br>
</div>
<div style="font-size:small">However, due
to the structure padding, there are some fake members inserted
into the structure. For example, when GEP works on the 5th
element of the padded structure, it may in fact works on the
originally 3rd one, if there are two paddings before the
original 3rd member. </div>
<div style="font-size:small"><br>
</div>
<div style="font-size:small">Is there any
way to map this "5th" access to the original "3rd" one? For
example, some APIs to tell whether one member is a real
member, or a padded one?</div>
<br>
</div>
</blockquote>
<br></span>
I would suggest converting the index of the struct GEP into an
offset in bytes; see StructLayout::getElementOffset<wbr>. You can then
compare that to the layout of the original C++ class.<span class="m_3927231417433861438HOEnZb"><font color="#888888"><br>
<br>
-Eli<br>
<pre class="m_3927231417433861438m_8347775751878978904moz-signature" cols="72">--
Employee of Qualcomm Innovation Center, Inc.
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project</pre>
</font></span></div>
</blockquote></div><br></div></div></div></div>
</blockquote></div><br></div></div>