<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><br><div><div>On Mar 31, 2008, at 4:46 PM, Bobby Powers wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite">Hello, I'm working on using the LLVM JIT for a little project of mine, amazing work first off! I have a question about optimization passes. I initially have this function I've created, in python it looks like this:<div> <br></div><div><span class="Apple-style-span" style="font-family: Verdana; "><pre style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; background-image: initial; background-repeat: initial; background-attachment: initial; -webkit-background-clip: initial; -webkit-background-origin: initial; background-color: rgb(255, 255, 255); background-position: initial initial; "><span class="n"><span class="Apple-style-span" style="font-size: small;">OS_end</span></span><span class=""><span class="Apple-style-span" style="font-size: small;"> </span></span><span class="o" style="font-weight: bold; "><span class="Apple-style-span" style="font-size: small;">=</span></span><span class=""><span class="Apple-style-span" style="font-size: small;"> </span></span><span class="mf" style="color: rgb(0, 153, 153); "><span class="Apple-style-span" style="font-size: small;">50</span></span><span class=""><span class="Apple-style-span" style="font-size: small;">
</span></span><span class="n"><span class="Apple-style-span" style="font-size: small;">OS_start</span></span><span class=""><span class="Apple-style-span" style="font-size: small;"> </span></span><span class="o" style="font-weight: bold; "><span class="Apple-style-span" style="font-size: small;">=</span></span><span class=""><span class="Apple-style-span" style="font-size: small;"> </span></span><span class="mf" style="color: rgb(0, 153, 153); "><span class="Apple-style-span" style="font-size: small;">0</span></span><span class=""><span class="Apple-style-span" style="font-size: small;">
</span></span><span class="n"><span class="Apple-style-span" style="font-size: small;">OS_timestep</span></span><span class=""><span class="Apple-style-span" style="font-size: small;"> </span></span><span class="o" style="font-weight: bold; "><span class="Apple-style-span" style="font-size: small;">=</span></span><span class=""><span class="Apple-style-span" style="font-size: small;"> </span></span><span class="mf" style="color: rgb(0, 153, 153); "><span class="Apple-style-span" style="font-size: small;">1</span></span><span class=""><span class="Apple-style-span" style="font-size: small;">
</span></span><span class="n"><span class="Apple-style-span" style="font-size: small;">birth_rate</span></span><span class=""><span class="Apple-style-span" style="font-size: small;"> </span></span><span class="o" style="font-weight: bold; "><span class="Apple-style-span" style="font-size: small;">=</span></span><span class=""><span class="Apple-style-span" style="font-size: small;"> </span></span><span class="o" style="font-weight: bold; "><span class="Apple-style-span" style="font-size: small;">.</span></span><span class="mf" style="color: rgb(0, 153, 153); "><span class="Apple-style-span" style="font-size: small;">3</span></span><span class=""><span class="Apple-style-span" style="font-size: small;">
</span></span><span class="n"><span class="Apple-style-span" style="font-size: small;">population</span></span><span class=""><span class="Apple-style-span" style="font-size: small;"> </span></span><span class="o" style="font-weight: bold; "><span class="Apple-style-span" style="font-size: small;">=</span></span><span class=""><span class="Apple-style-span" style="font-size: small;"> </span></span><span class="mf" style="color: rgb(0, 153, 153); "><span class="Apple-style-span" style="font-size: small;">30.0</span></span><span class=""><span class="Apple-style-span" style="font-size: small;">
</span></span><span class=""><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-style-span" style="font-size: small;">
</span></font></span><span class="k" style="font-weight: bold; "><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-style-span" style="font-size: small;">for</span></font></span><span class=""><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-style-span" style="font-size: small;"> </span></font></span><span class="n"><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-style-span" style="font-size: small;">time</span></font></span><span class=""><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-style-span" style="font-size: small;"> </span></font></span><span class="ow" style="font-weight: bold; "><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-style-span" style="font-size: small;">in</span></font></span><span class=""><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-style-span" style="font-size: small;"> </span></font></span><span class="nb" style="color: rgb(153, 153, 153); "><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-style-span" style="font-size: small;">range</span></font></span><span class="p"><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-style-span" style="font-size: small;">(</span></font></span><span class="n"><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-style-span" style="font-size: small;">OS_start</span></font></span><span class="p"><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-style-span" style="font-size: small;">,</span></font></span><span class=""><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-style-span" style="font-size: small;"> </span></font></span><span class="n"><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-style-span" style="font-size: small;">OS_end</span></font></span><span class="p"><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-style-span" style="font-size: small;">,</span></font></span><span class=""><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-style-span" style="font-size: small;"> </span></font></span><span class="n"><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-style-span" style="font-size: small;">OS_timestep</span></font></span><span class="p"><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-style-span" style="font-size: small;">):</span></font></span><span class=""><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-style-span" style="font-size: small;">
</span></font></span><span class="n"><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-style-span" style="font-size: small;">births</span></font></span><span class=""><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-style-span" style="font-size: small;"> </span></font></span><span class="o" style="font-weight: bold; "><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-style-span" style="font-size: small;">=</span></font></span><span class=""><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-style-span" style="font-size: small;"> </span></font></span><span class="n"><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-style-span" style="font-size: small;">birth_rate</span></font></span><span class=""><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-style-span" style="font-size: small;"> </span></font></span><span class="o" style="font-weight: bold; "><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-style-span" style="font-size: small;">*</span></font></span><span class=""><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-style-span" style="font-size: small;"> </span></font></span><span class="n"><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-style-span" style="font-size: small;">population</span></font></span><span class=""><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-style-span" style="font-size: small;">
</span></font></span><span class="n"><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-style-span" style="font-size: small;">deaths</span></font></span><span class=""><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-style-span" style="font-size: small;"> </span></font></span><span class="o" style="font-weight: bold; "><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-style-span" style="font-size: small;">=</span></font></span><span class=""><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-style-span" style="font-size: small;"> </span></font></span><span class="mf" style="color: rgb(0, 153, 153); "><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-style-span" style="font-size: small;">0.1</span></font></span><span class=""><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-style-span" style="font-size: small;"> </span></font></span><span class="o" style="font-weight: bold; "><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-style-span" style="font-size: small;">*</span></font></span><span class=""><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-style-span" style="font-size: small;"> </span></font></span><span class="n"><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-style-span" style="font-size: small;">population</span></font></span><span class=""><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-style-span" style="font-size: small;">
</span></font></span><span class="n"><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-style-span" style="font-size: small;">population_NEXT</span></font></span><span class=""><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-style-span" style="font-size: small;"> </span></font></span><span class="o" style="font-weight: bold; "><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-style-span" style="font-size: small;">=</span></font></span><span class=""><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-style-span" style="font-size: small;"> </span></font></span><span class="n"><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-style-span" style="font-size: small;">population</span></font></span><span class=""><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-style-span" style="font-size: small;"> </span></font></span><span class="o" style="font-weight: bold; "><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-style-span" style="font-size: small;">+</span></font></span><span class=""><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-style-span" style="font-size: small;"> </span></font></span><span class="n"><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-style-span" style="font-size: small;">births</span></font></span><span class=""><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-style-span" style="font-size: small;"> </span></font></span><span class="o" style="font-weight: bold; "><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-style-span" style="font-size: small;">-</span></font></span><span class=""><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-style-span" style="font-size: small;"> </span></font></span><span class="n"><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-style-span" style="font-size: small;">deaths</span></font></span><span class=""><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-style-span" style="font-size: small;">
</span></font></span></pre><pre style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; background-image: initial; background-repeat: initial; background-attachment: initial; -webkit-background-clip: initial; -webkit-background-origin: initial; background-color: rgb(255, 255, 255); background-position: initial initial; "><font class="Apple-style-span" face="arial" size="3"><span class="Apple-style-span" style="font-size: 13px;"></span></font></pre><div><font class="Apple-style-span" face="arial"><span class="Apple-style-span" style="white-space: pre; "><br></span></font></div><div><font class="Apple-style-span" face="arial"><span class="Apple-style-span" style="white-space: pre;">The thing is, in forbody it is still doing:</span></font></div> <div><font class="Apple-style-span" face="arial"><span class="Apple-style-span" style="white-space: pre;">subtmp = population + (population * .3) - (population * .1)</span></font></div><div><font class="Apple-style-span" face="arial"><span class="Apple-style-span" style="white-space: pre;"><br> </span></font></div><div><font class="Apple-style-span" face="arial"><span class="Apple-style-span" style="white-space: pre;">ideally I would love to see it reduced to:</span></font></div><div><font class="Apple-style-span" face="arial"><span class="Apple-style-span" style="white-space: pre;">subtmp = 1.2 * population</span></font></div></span></div></blockquote><div><br class="webkit-block-placeholder"></div>This transformation changes the result due to roundoff errors, and would be incorrect. </div><div>In C using %lf the printed values of the 2 expressions diverge on the 84th iteration.<br><div><br class="webkit-block-placeholder"></div><blockquote type="cite"><div><span class="Apple-style-span" style="font-family: Verdana; "><div><font class="Apple-style-span" face="arial"><span class="Apple-style-span" style="white-space: pre;"><span class="Apple-style-span" style="white-space: normal; -webkit-text-stroke-width: -1; ">I've played around with adding a bunch of optimizations that sound<font class="Apple-style-span" face="arial, sans-serif"> good from [</font><span class="Apple-style-span" style="font-family: Verdana; "><font class="Apple-style-span" face="arial, sans-serif"><a href="http://www.llvm.org/docs/Passes.html">http://www.llvm.org/docs/Passes.html</a></font><span class="Apple-style-span" style="white-space: pre; "><font class="Apple-style-span" face="arial, sans-serif">], but I must admit I'm a bit over my head here. Doe</font></span><span class="Apple-style-span" style="font-family: arial; white-space: pre; ">s anyone have any suggestions? Am I missing passes, do I need to restructure the IR, or am I missing some concept?</span></span></span></span></font></div> <div><font class="Apple-style-span" face="arial"><span class="Apple-style-span" style="white-space: pre;"><br></span></font></div><div><font class="Apple-style-span" face="arial"><span class="Apple-style-span" style="white-space: pre;">thanks! (keep up the amazing work)</span></font></div> <div><font class="Apple-style-span" face="arial"><span class="Apple-style-span" style="white-space: pre;">yours,</span></font></div><div><font class="Apple-style-span" face="arial"><span class="Apple-style-span" style="white-space: pre;">Bobby Powers</span></font></div> </span></div> _______________________________________________<br>LLVM Developers mailing list<br>LLVMdev@cs.uiuc.edu <a href="http://llvm.cs.uiuc.edu">http://llvm.cs.uiuc.edu</a><br><a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev">http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev</a><br></blockquote></div><br></body></html>