<div dir="ltr">Real CPUs don't have the ability to directly calculate expressions such as your nested set of calls. If you write it that way, the internals of the compiler, and the eventual machine code, will be much more like the first version.<div><br></div><div>But (unless you have "volatile", or -O0) there are no loads or stores there. On any reasonable modern machine the values will all be in registers. Only var3 will typically even need any special handling to save it for later, but that will be in a register.</div><div><br></div><div>e.g. on Thumb, and assuming this is the entire function, you want to return var8 as the result, and the vars can be stored in integer registers, and the expressions are not too complex:</div><div><br></div><div>push r4,lr</div><div>mov r0,expression1 // more likely an add, sub, etc with r0 as the destination</div><div>mov r1,expression2</div><div>bl call // leaves result in r0</div><div>mov r4,r0</div><div>mov r0,expression3</div><div>mov r1,expression4</div><div>bl call</div><div>bl call</div><div>mov r1,r0</div><div>mov r0,r4</div><div>bl call</div><div>pop r4,pc</div><div><br></div><div>The only instructions here which do loads or stores to memory are the push and the pop. Even the calls don't touch memory (unless those functions are not self-contained and need to call other functions).</div><div><br></div><div>Note that in Thumb and ARM code any function is free to do anything it wants with r0,r1,r2,r3 ("caller save" or "volatile"). They do not need to be saved, and you can't expect anything you leave there to still be there after you call another function. r4,r5,r6,r7 ("callee save") you can assume will still have their original values after you call a function, and if you use them then you have to save and restore the value you found there,</div><div><br></div><div>This is typical of RISC and newer CPU conventions. PowerPC, MIPS, SPARC and even x86_64 are essentially the same, other than some not using a "link register" style of function call.</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Nov 18, 2014 at 5:35 PM, Hayden Livingston <span dir="ltr"><<a href="mailto:halivingston@gmail.com" target="_blank">halivingston@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>This is actually not a LLVM issue, but more an issue of my higher language,that I'm trying to target LLVM on. I'm seeing that if I do a naive translation of my language, it generates additional load stores.</div><div><br></div><div>For example (in my high level language):</div><div><br></div><div>  var1 = expression1;</div><div>  var2 = expression2;</div><div>  var3 = call(var1, var2);</div><div>  var4 = expression3;</div><div>  var5 = expression4;</div><div>  var6 = call(var4, var5);</div><div>  var7 = call(var6);</div><div>  var8 = call(var3, var7);</div><div><br></div><div>This can be effectively represented in my high level language as</div><div><br></div><div>var8 = call(call(expression1, expression2), call(call(expression3, expression4));</div><div><br></div><div>When I think about it naively. I would write code that would start from var8 and traverse and try to substitute all "vars" with expressions.</div><div><br></div><div>I can imagine not always wanting to do this, for example if the expression is duplicated, maybe I do want to pay for the load/store. And maybe sometimes the semantics are such that I need to reuse it.<br></div><div><br></div><div>Is there a formal algorithm or classical technique that describes this?</div><span class="HOEnZb"><font color="#888888"><div><br></div><div>Hayden</div></font></span></div>
<br>_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:LLVMdev@cs.uiuc.edu">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><br>
<br></blockquote></div><br></div>