<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
margin-bottom:.0001pt;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:#0563C1;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:#954F72;
text-decoration:underline;}
span.EmailStyle17
{mso-style-type:personal;
font-family:"Calibri",sans-serif;
color:windowtext;}
span.EmailStyle18
{mso-style-type:personal-reply;
font-family:"Calibri",sans-serif;
color:#1F497D;}
.MsoChpDefault
{mso-style-type:export-only;
font-size:10.0pt;}
@page WordSection1
{size:8.5in 11.0in;
margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-US" link="#0563C1" vlink="#954F72">
<div class="WordSection1">
<p class="MsoNormal"><span style="color:#1F497D">Hi Felipe,<o:p></o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D">My understanding is that the type of pointer types in LLVM IR have no semantic meaning. That is, the pointer is just an address and the fact that the type system currently describes the type of the data that
it points to is more or less just a convenience. In fact, there’s an effort underway to make all pointers opaquely typed.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D">So the transformation you’re describing is correct. In spite of all the hoops the original IR is jumping through, a pointer to InnerState and a pointer to the first member of InnerState are in fact pointing to
the same place. If you need to know more than that, I think it needs to be captured in the annotation itself.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D">-Andy<o:p></o:p></span></p>
<p class="MsoNormal"><a name="_MailEndCompose"><span style="color:#1F497D"><o:p> </o:p></span></a></p>
<div>
<div style="border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b>From:</b> llvm-dev [mailto:llvm-dev-bounces@lists.llvm.org]
<b>On Behalf Of </b>De Azevedo Piovezan, Felipe via llvm-dev<br>
<b>Sent:</b> Wednesday, November 07, 2018 10:13 AM<br>
<b>To:</b> llvm-dev@lists.llvm.org<br>
<b>Subject:</b> [llvm-dev] Instcombine and llvm.ptr.annotation<o:p></o:p></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Hi,<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">I’m trying to figure out if this behavior by Instcombine is ok, and I’d appreciate some feedback.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Suppose you have this kind of struct:<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"> %struct.State = type { i32, i32, i32, i32, %"struct.State::InnerState" }<o:p></o:p></p>
<p class="MsoNormal"> %"struct.State::InnerState" = type { i32, i32 }<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Now suppose we have %S which is of type struct.State and we want to use two llvm.ptr.annotations:<o:p></o:p></p>
<p class="MsoNormal">1- one to annotate the last member of struct.State (which is of type struct.State::InnerState)<o:p></o:p></p>
<p class="MsoNormal">2- one to annotate the first member of %"struct.State::InnerState"<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">This is the IR out of clang:<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">define i32 @foo(%struct.State* %S) {<o:p></o:p></p>
<p class="MsoNormal"> %my_inner_state = getelementptr inbounds %struct.State, %struct.State* %S, i32 0, i32 4<o:p></o:p></p>
<p class="MsoNormal"> %0 = bitcast %"struct.State::InnerState"* %my_inner_state to i8*<o:p></o:p></p>
<p class="MsoNormal"> %1 = call i8* @llvm.ptr.annotation.p0i8(i8* %0, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i32 0, i32 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i32 0, i32 0), i32 20)<o:p></o:p></p>
<p class="MsoNormal"> %2 = bitcast i8* %1 to %"struct.State::InnerState"*<o:p></o:p></p>
<p class="MsoNormal"> <b>%inner_member = getelementptr inbounds %"struct.State::InnerState", %"struct.State::InnerState"* %2, i32 0, i32 0<o:p></o:p></b></p>
<p class="MsoNormal"><b> %3 = bitcast i32* %inner_member to i8*<o:p></o:p></b></p>
<p class="MsoNormal"> %4 = call i8* @llvm.ptr.annotation.p0i8(i8* %3, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i32 0, i32 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i32 0, i32 0), i32 15)<o:p></o:p></p>
<p class="MsoNormal">}<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Instcombine is replacing the GEP with all 0 indices + bitcast with the base pointer of the GEP:<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"> %my_inner_state = getelementptr inbounds %struct.State, %struct.State* %S, i64 0, i32 4<o:p></o:p></p>
<p class="MsoNormal"> %0 = bitcast %"struct.State::InnerState"* %my_inner_state to i8*<o:p></o:p></p>
<p class="MsoNormal"> %1 = call i8* @llvm.ptr.annotation.p0i8(i8* %0, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 20)<o:p></o:p></p>
<p class="MsoNormal"> %2 = call i8* @llvm.ptr.annotation.p0i8(i8* %1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 15)<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Now we cannot distinguish between what is actually being annotated. In other words, it looks like the whole %"struct.State::InnerState” is being annotated twice.<o:p></o:p></p>
<p class="MsoNormal">Is this ok?<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Thanks!<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">--<o:p></o:p></p>
<p class="MsoNormal">Felipe de Azevedo Piovezan<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
</body>
</html>