<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=utf-8">
<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:12.0pt;
font-family:"Times New Roman",serif;}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:purple;
text-decoration:underline;}
span.EmailStyle17
{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="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D">Thanks for correcting Mehdi, its “the result is undefined” not “undefined behavior”.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D">Regards,<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D">Ashutosh<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D"><o:p> </o:p></span></p>
<div style="border:none;border-left:solid blue 1.5pt;padding:0in 0in 0in 4.0pt">
<div>
<div style="border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">From:</span></b><span style="font-size:11.0pt;font-family:"Calibri",sans-serif"> mehdi.amini@apple.com [mailto:mehdi.amini@apple.com]
<br>
<b>Sent:</b> Monday, December 19, 2016 11:32 PM<br>
<b>To:</b> Nema, Ashutosh <Ashutosh.Nema@amd.com><br>
<b>Cc:</b> Stephan Plöger <ploegers@cs.uni-bonn.de>; llvm-dev <llvm-dev@lists.llvm.org><br>
<b>Subject:</b> Re: [llvm-dev] opt -instcombine interesting behavior<o:p></o:p></span></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<blockquote style="margin-top:5.0pt;margin-bottom:5.0pt">
<div>
<p class="MsoNormal">On Dec 19, 2016, at 9:51 AM, Nema, Ashutosh via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>> wrote:<o:p></o:p></p>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div>
<blockquote style="margin-top:5.0pt;margin-bottom:5.0pt">
<p class="MsoNormal"> %reg = alloca i32, i32 0<o:p></o:p></p>
</blockquote>
<p class="MsoNormal">Allocating 0 bytes is legal but it's an undefined behavior, please refer:<br>
<a href="http://llvm.org/docs/LangRef.html#id184">http://llvm.org/docs/LangRef.html#id184</a><o:p></o:p></p>
</div>
</div>
</blockquote>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">Nit: I believe that when LangRef says “the result is undefined”, it does not mean “undefined behavior”.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">See <a href="http://llvm.org/docs/LangRef.html#undefined-values">http://llvm.org/docs/LangRef.html#undefined-values</a> for more details.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<blockquote style="margin-top:5.0pt;margin-bottom:5.0pt">
<div>
<div>
<p class="MsoNormal"><br>
You need to correct allocation to get the desired output, i.e.:<br>
<br>
<o:p></o:p></p>
<blockquote style="margin-top:5.0pt;margin-bottom:5.0pt">
<p class="MsoNormal"> %reg = alloca i32, i32 1<o:p></o:p></p>
</blockquote>
</div>
</div>
</blockquote>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">The “, i32 1” is optional though (default is 1).<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<div>
<p class="MsoNormal">— <o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">Mehdi<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<p class="MsoNormal"><br>
<br>
<o:p></o:p></p>
</div>
<p class="MsoNormal"><br>
<br>
<o:p></o:p></p>
<blockquote style="margin-top:5.0pt;margin-bottom:5.0pt">
<div>
<div>
<p class="MsoNormal"><br>
Regards,<br>
Ashutosh<br>
<br>
<br>
<o:p></o:p></p>
<blockquote style="margin-top:5.0pt;margin-bottom:5.0pt">
<p class="MsoNormal">-----Original Message-----<br>
From: llvm-dev [<a href="mailto:llvm-dev-bounces@lists.llvm.org">mailto:llvm-dev-bounces@lists.llvm.org</a>] On Behalf Of<br>
Stephan Plöger via llvm-dev<br>
Sent: Monday, December 19, 2016 10:11 PM<br>
To: <a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a><br>
Subject: [llvm-dev] opt -instcombine interesting behavior<br>
<br>
Hi all,<br>
<br>
I've been playing around with the llvm optimizer and stumbled upon an<br>
interesting behavior with respect to the "instcombine" option. I tried to<br>
optimize the following small program (see also attachment small.ll) (only<br>
with "instcombine"):<br>
<br>
clang -v<br>
clang version 4.0.0 (trunk 288238)<br>
Target: x86_64-unknown-linux-gnu<br>
Thread model: posix<br>
InstalledDir: /usr/local/bin<br>
Found candidate GCC installation: /usr/lib/gcc/i686-linux-gnu/5 Found<br>
candidate GCC installation: /usr/lib/gcc/i686-linux-gnu/5.4.0 Found<br>
candidate GCC installation: /usr/lib/gcc/i686-linux-gnu/6 Found candidate<br>
GCC installation: /usr/lib/gcc/i686-linux-gnu/6.0.0 Found candidate GCC<br>
installation: /usr/lib/gcc/x86_64-linux-gnu/5 Found candidate GCC<br>
installation: /usr/lib/gcc/x86_64-linux-gnu/5.4.0<br>
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/6 Found<br>
candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/6.0.0<br>
Selected GCC installation: /usr/lib/gcc/x86_64-linux-gnu/5.4.0<br>
Candidate multilib: .;@m64<br>
Candidate multilib: 32;@m32<br>
Candidate multilib: x32;@mx32<br>
Selected multilib: .;@m64<br>
<br>
<br>
llvm-as small.ll -o small.bc<br>
opt -instcombine small.bc -o smallOpt.bc<br>
<br>
<br>
define i32 @main() {<br>
%reg = alloca i32, i32 0<br>
store i32 1, i32* %reg<br>
%1 = getelementptr i32, i32* %reg, i32 0<br>
%2 = bitcast i32* %1 to [4 x i8]*<br>
%3 = getelementptr [4 x i8], [4 x i8]* %2, i32 0, i32 1<br>
store i8 1, i8* %3<br>
%4 = load i32, i32* %reg<br>
ret i32 %4<br>
}<br>
<br>
The program in pseudo code would/should simplified look something like<br>
this:<br>
<br>
int main(){<br>
reg = 1;<br>
[reg+1] = 1; //Second byte of reg is set to 1<br>
return reg;<br>
}<br>
<br>
I assumed it would be optimized to - again pseudo code -:<br>
<br>
int main(){<br>
return 257;<br>
}<br>
<br>
Instead, the program is optimized to(see also attachment<br>
smallOpt.ll/smallOpt.bc):<br>
<br>
define i32 @main() {<br>
ret i32 1<br>
}<br>
<br>
So i verified my assumption by printing '%4' of the original program<br>
(smallPrint.ll/smallPrint.bc) and the output was 257.<br>
I also optimized smallPrint.ll/smallPrint.bc , again only with instcombine, but<br>
the output is changed to 1 (smallPrintOpt.bc).<br>
<br>
I also tested it with clang 3.8.0.<br>
<br>
What am i missing?<br>
<br>
<br>
Best regards,<br>
Stephan<o:p></o:p></p>
</blockquote>
<p class="MsoNormal">_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><o:p></o:p></p>
</div>
</div>
</blockquote>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
</div>
</body>
</html>