<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Dec 19, 2016, at 9:51 AM, Nema, Ashutosh via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" class="">llvm-dev@lists.llvm.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class=""><blockquote type="cite" class=""> %reg = alloca i32, i32 0<br class=""></blockquote>Allocating 0 bytes is legal but it's an undefined behavior, please refer:<br class=""><a href="http://llvm.org/docs/LangRef.html#id184" class="">http://llvm.org/docs/LangRef.html#id184</a><br class=""></div></div></blockquote><div><br class=""></div><div>Nit: I believe that when LangRef says “the result is undefined”, it does not mean “undefined behavior”.</div><div>See <a href="http://llvm.org/docs/LangRef.html#undefined-values" class="">http://llvm.org/docs/LangRef.html#undefined-values</a> for more details.</div><div><br class=""></div><blockquote type="cite" class=""><div class=""><div class=""><br class="">You need to correct allocation to get the desired output, i.e.:<br class=""><blockquote type="cite" class=""> %reg = alloca i32, i32 1<br class=""></blockquote></div></div></blockquote><div><br class=""></div><div>The “, i32 1” is optional though (default is 1).</div><div><br class=""></div><div><div>— </div><div>Mehdi</div><div><br class=""></div><br class=""><blockquote type="cite" class=""><div class=""></div></blockquote></div><br class=""><blockquote type="cite" class=""><div class=""><div class=""><br class="">Regards,<br class="">Ashutosh<br class=""><br class=""><blockquote type="cite" class="">-----Original Message-----<br class="">From: llvm-dev [<a href="mailto:llvm-dev-bounces@lists.llvm.org" class="">mailto:llvm-dev-bounces@lists.llvm.org</a>] On Behalf Of<br class="">Stephan Plöger via llvm-dev<br class="">Sent: Monday, December 19, 2016 10:11 PM<br class="">To: <a href="mailto:llvm-dev@lists.llvm.org" class="">llvm-dev@lists.llvm.org</a><br class="">Subject: [llvm-dev] opt -instcombine interesting behavior<br class=""><br class="">Hi all,<br class=""><br class="">I've been playing around with the llvm optimizer and stumbled upon an<br class="">interesting behavior with respect to the "instcombine" option. I tried to<br class="">optimize the following small program (see also attachment small.ll) (only<br class="">with "instcombine"):<br class=""><br class="">clang -v<br class="">clang version 4.0.0 (trunk 288238)<br class="">Target: x86_64-unknown-linux-gnu<br class="">Thread model: posix<br class="">InstalledDir: /usr/local/bin<br class="">Found candidate GCC installation: /usr/lib/gcc/i686-linux-gnu/5 Found<br class="">candidate GCC installation: /usr/lib/gcc/i686-linux-gnu/5.4.0 Found<br class="">candidate GCC installation: /usr/lib/gcc/i686-linux-gnu/6 Found candidate<br class="">GCC installation: /usr/lib/gcc/i686-linux-gnu/6.0.0 Found candidate GCC<br class="">installation: /usr/lib/gcc/x86_64-linux-gnu/5 Found candidate GCC<br class="">installation: /usr/lib/gcc/x86_64-linux-gnu/5.4.0<br class="">Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/6 Found<br class="">candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/6.0.0<br class="">Selected GCC installation: /usr/lib/gcc/x86_64-linux-gnu/5.4.0<br class="">Candidate multilib: .;@m64<br class="">Candidate multilib: 32;@m32<br class="">Candidate multilib: x32;@mx32<br class="">Selected multilib: .;@m64<br class=""><br class=""><br class="">llvm-as small.ll -o small.bc<br class="">opt -instcombine small.bc -o smallOpt.bc<br class=""><br class=""><br class="">define i32 @main() {<br class=""> %reg = alloca i32, i32 0<br class=""> store i32 1, i32* %reg<br class=""> %1 = getelementptr i32, i32* %reg, i32 0<br class=""> %2 = bitcast i32* %1 to [4 x i8]*<br class=""> %3 = getelementptr [4 x i8], [4 x i8]* %2, i32 0, i32 1<br class=""> store i8 1, i8* %3<br class=""> %4 = load i32, i32* %reg<br class=""> ret i32 %4<br class="">}<br class=""><br class="">The program in pseudo code would/should simplified look something like<br class="">this:<br class=""><br class="">int main(){<br class=""> reg = 1;<br class=""> [reg+1] = 1; //Second byte of reg is set to 1<br class=""> return reg;<br class="">}<br class=""><br class="">I assumed it would be optimized to - again pseudo code -:<br class=""><br class="">int main(){<br class=""> return 257;<br class="">}<br class=""><br class="">Instead, the program is optimized to(see also attachment<br class="">smallOpt.ll/smallOpt.bc):<br class=""><br class="">define i32 @main() {<br class=""> ret i32 1<br class="">}<br class=""><br class="">So i verified my assumption by printing '%4' of the original program<br class="">(smallPrint.ll/smallPrint.bc) and the output was 257.<br class="">I also optimized smallPrint.ll/smallPrint.bc , again only with instcombine, but<br class="">the output is changed to 1 (smallPrintOpt.bc).<br class=""><br class="">I also tested it with clang 3.8.0.<br class=""><br class="">What am i missing?<br class=""><br class=""><br class="">Best regards,<br class="">Stephan<br class=""></blockquote>_______________________________________________<br class="">LLVM Developers mailing list<br class=""><a href="mailto:llvm-dev@lists.llvm.org" class="">llvm-dev@lists.llvm.org</a><br class="">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev<br class=""></div></div></blockquote></div><br class=""></body></html>