<div dir="ltr">'load volatile i32 addrspace(1)* null' seems fine to me. However, it looks like instcombine will turn:<div><div>define i32 @foo() {</div><div>entry:</div><div> %std_ld.i = load volatile i32, i32 addrspace(1)* null</div><div> ret i32 %std_ld.i</div><div>}</div></div><div><br></div><div>into:</div><div><div>define i32 @foo() {</div><div>entry:</div><div> %std_ld.i = load volatile i32, i32 addrspace(1)* null, align 536870912</div><div> ret i32 %std_ld.i</div><div>}</div></div><div><br></div><div>which is not ok.</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Jun 9, 2015 at 11:34 AM, Benyei, Guy <span dir="ltr"><<a href="mailto:guy.benyei@intel.com" target="_blank">guy.benyei@intel.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div lang="EN-US" link="blue" vlink="purple">
<div>
<p class="MsoNormal"><a name="14dd99a8b3023cdb__MailEndCompose"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1f497d">Thanks David,<u></u><u></u></span></a></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1f497d">It turns out, that the address space I was using was not 0, and yet the pointer was constant folded to null.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1f497d">Here is the sequence:<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal" style="margin-left:.5in"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1f497d">Unoptimized code:<u></u><u></u></span></p>
<p class="MsoNormal" style="margin-left:.5in"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal" style="margin-left:.5in"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1f497d">define i32 @foo() #0 {<u></u><u></u></span></p>
<p class="MsoNormal" style="margin-left:.5in"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1f497d">entry:<u></u><u></u></span></p>
<p class="MsoNormal" style="margin-left:.5in"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1f497d"> %address.addr.i = alloca i32, align 4<u></u><u></u></span></p>
<p class="MsoNormal" style="margin-left:.5in"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1f497d"> %value.i = alloca i32, align 4<u></u><u></u></span></p>
<p class="MsoNormal" style="margin-left:.5in"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1f497d"> store i32 0, i32* %address.addr.i, align 4<u></u><u></u></span></p>
<p class="MsoNormal" style="margin-left:.5in"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1f497d"> %0 = load i32* %address.addr.i, align 4<u></u><u></u></span></p>
<p class="MsoNormal" style="margin-left:.5in"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1f497d"> %1 = inttoptr i32 %0 to i32 addrspace(1)*<u></u><u></u></span></p>
<p class="MsoNormal" style="margin-left:.5in"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1f497d"> %std_ld.i = load volatile i32 addrspace(1)* %1<u></u><u></u></span></p>
<p class="MsoNormal" style="margin-left:.5in"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1f497d"> store i32 %std_ld.i, i32* %value.i, align 4<u></u><u></u></span></p>
<p class="MsoNormal" style="margin-left:.5in"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1f497d"> %2 = load i32* %value.i, align 4<u></u><u></u></span></p>
<p class="MsoNormal" style="margin-left:.5in"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1f497d"> ret i32 %2<u></u><u></u></span></p>
<p class="MsoNormal" style="margin-left:.5in"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1f497d">}<u></u><u></u></span></p>
<p class="MsoNormal" style="margin-left:.5in"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal" style="margin-left:.5in"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1f497d">After optimization (early CSE):<u></u><u></u></span></p>
<p class="MsoNormal" style="margin-left:.5in"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal" style="margin-left:.5in"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1f497d">define i32 @foo() #0 {<u></u><u></u></span></p>
<p class="MsoNormal" style="margin-left:.5in"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1f497d">entry:<u></u><u></u></span></p>
<p class="MsoNormal" style="margin-left:.5in"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1f497d"> %std_ld.i = load volatile i32 addrspace(1)* null, align 536870912<u></u><u></u></span></p>
<p class="MsoNormal" style="margin-left:.5in"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1f497d"> ret i32 %std_ld.i<u></u><u></u></span></p>
<p class="MsoNormal" style="margin-left:.5in"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1f497d">}<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1f497d">The contant folder doesn’t seem to check for address space, it simply checks if the integer in question is zero, and folds the inttoptr to null:<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal" style="margin-left:.5in;text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas;color:#2b91af;background:white">Constant</span><span style="font-size:9.5pt;font-family:Consolas;color:black;background:white">
*llvm::ConstantFoldCastInstruction(</span><span style="font-size:9.5pt;font-family:Consolas;color:blue;background:white">unsigned</span><span style="font-size:9.5pt;font-family:Consolas;color:black;background:white">
</span><span style="font-size:9.5pt;font-family:Consolas;color:gray;background:white">opc</span><span style="font-size:9.5pt;font-family:Consolas;color:black;background:white">,
</span><span style="font-size:9.5pt;font-family:Consolas;color:#2b91af;background:white">Constant</span><span style="font-size:9.5pt;font-family:Consolas;color:black;background:white"> *</span><span style="font-size:9.5pt;font-family:Consolas;color:gray;background:white">V</span><span style="font-size:9.5pt;font-family:Consolas;color:black;background:white">,<u></u><u></u></span></p>
<p class="MsoNormal" style="margin-left:.5in;text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas;color:black;background:white">
</span><span style="font-size:9.5pt;font-family:Consolas;color:#2b91af;background:white">Type</span><span style="font-size:9.5pt;font-family:Consolas;color:black;background:white"> *</span><span style="font-size:9.5pt;font-family:Consolas;color:gray;background:white">DestTy</span><span style="font-size:9.5pt;font-family:Consolas;color:black;background:white">)
{<u></u><u></u></span></p>
<p class="MsoNormal" style="margin-left:.5in;text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas;color:blue;background:white"><u></u> <u></u></span></p>
<p class="MsoNormal" style="margin-left:.5in;text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas;color:blue;background:white">...<u></u><u></u></span></p>
<p class="MsoNormal" style="margin-left:.5in;text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas;color:blue;background:white"><u></u> <u></u></span></p>
<p class="MsoNormal" style="margin-left:.5in;text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas;color:black;background:white">
</span><span style="font-size:9.5pt;font-family:Consolas;color:blue;background:white">if</span><span style="font-size:9.5pt;font-family:Consolas;color:black;background:white"> (</span><span style="font-size:9.5pt;font-family:Consolas;color:gray;background:white">V</span><span style="font-size:9.5pt;font-family:Consolas;color:black;background:white">->isNullValue()
&& !</span><span style="font-size:9.5pt;font-family:Consolas;color:gray;background:white">DestTy</span><span style="font-size:9.5pt;font-family:Consolas;color:black;background:white">->isX86_MMXTy())<u></u><u></u></span></p>
<p class="MsoNormal" style="margin-left:.5in;text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas;color:black;background:white">
</span><span style="font-size:9.5pt;font-family:Consolas;color:blue;background:white">return</span><span style="font-size:9.5pt;font-family:Consolas;color:black;background:white">
</span><span style="font-size:9.5pt;font-family:Consolas;color:#2b91af;background:white">Constant</span><span style="font-size:9.5pt;font-family:Consolas;color:black;background:white">::getNullValue(</span><span style="font-size:9.5pt;font-family:Consolas;color:gray;background:white">DestTy</span><span style="font-size:9.5pt;font-family:Consolas;color:black;background:white">);<u></u><u></u></span></p>
<p class="MsoNormal" style="margin-left:.5in;text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas;color:black;background:white">...<u></u><u></u></span></p>
<p class="MsoNormal" style="margin-left:.5in"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal" style="margin-left:.5in"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1f497d">Is this a bug?<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1f497d">Thanks<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1f497d"> Guy<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1f497d"><u></u> <u></u></span></p>
<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"> David Majnemer [mailto:<a href="mailto:david.majnemer@gmail.com" target="_blank">david.majnemer@gmail.com</a>]
<br>
<b>Sent:</b> Tuesday, June 09, 2015 18:45<br>
<b>To:</b> Benyei, Guy<br>
<b>Cc:</b> <a href="mailto:llvmdev@cs.uiuc.edu" target="_blank">llvmdev@cs.uiuc.edu</a><br>
<b>Subject:</b> Re: [LLVMdev] Constant folding inttoptr i32 0 to null pointer?<u></u><u></u></span></p><div><div class="h5">
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<p class="MsoNormal">On Tue, Jun 9, 2015 at 5:57 AM, Benyei, Guy <<a href="mailto:guy.benyei@intel.com" target="_blank">guy.benyei@intel.com</a>> wrote:<u></u><u></u></p>
<blockquote style="border:none;border-left:solid #cccccc 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-right:0in">
<div>
<div>
<p class="MsoNormal">Hello,<u></u><u></u></p>
<p class="MsoNormal">It seems that ConstantFoldCastInstruction in ConstantFold.cpp folds inttoptr instruction with 0 as operand to a null pointer. It makes sense, when talking about a C-style frontend,
as the C99 spec (6.3.2.3) states:<u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal" style="margin-left:.5in;text-autospace:none">
“An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant.”<u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal">On the other hand, some architectures use 0 as a valid memory location, and this constant folding seems to be possibly harmful when the code actually tries to access the memory
location at address 0.<u></u><u></u></p>
<p class="MsoNormal">Is this behavior intentional? Do I miss something? Will a load from address null try to access address 0, or may it become an undef value?<u></u><u></u></p>
</div>
</div>
</blockquote>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">LLVM assumes that the null pointer in address space zero can never be successfully dereferenced. You must utilize some other address space to dereference a null pointer.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
<blockquote style="border:none;border-left:solid #cccccc 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-right:0in">
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal">Thanks<u></u><u></u></p>
<p class="MsoNormal"> Guy<u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
<p>---------------------------------------------------------------------<br>
Intel Israel (74) Limited<u></u><u></u></p>
<p>This e-mail and any attachments may contain confidential material for<br>
the sole use of the intended recipient(s). Any review or distribution<br>
by others is strictly prohibited. If you are not the intended<br>
recipient, please contact the sender and delete all copies.<u></u><u></u></p>
</div>
<p class="MsoNormal" style="margin-bottom:12.0pt"><br>
_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:LLVMdev@cs.uiuc.edu" target="_blank">LLVMdev@cs.uiuc.edu</a> <a href="http://llvm.cs.uiuc.edu" target="_blank">http://llvm.cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev</a><u></u><u></u></p>
</blockquote>
</div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
</div>
</div></div></div><div><div class="h5">
<p>---------------------------------------------------------------------<br>
Intel Israel (74) Limited</p>
<p>This e-mail and any attachments may contain confidential material for<br>
the sole use of the intended recipient(s). Any review or distribution<br>
by others is strictly prohibited. If you are not the intended<br>
recipient, please contact the sender and delete all copies.</p></div></div></div>
</blockquote></div><br></div>