<div dir="ltr"><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;color:rgb(0,0,0)">​Appreciate all of the quick responses to my ridiculous questions so far. Hoping this one attracts similarly good dis​cussion!</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;color:rgb(0,0,0)"><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;color:rgb(0,0,0)">Let's say I have the following series of instructions:</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;color:rgb(0,0,0)"><br></div><div class="gmail_default"><div class="gmail_default" style="color:rgb(0,0,0)"><font face="monospace, monospace">  %a = load i32, i32* %ptr1</font></div><div class="gmail_default" style="color:rgb(0,0,0)"><font face="monospace, monospace">  %b = load i32, i32* %ptr2</font></div><div class="gmail_default" style="color:rgb(0,0,0)"><font face="monospace, monospace">  %c = add i32 %a, %b</font></div><div class="gmail_default" style="color:rgb(0,0,0)"><font face="monospace, monospace">  store i32 %c, i32* %ptr3</font></div><div class="gmail_default" style="color:rgb(0,0,0);font-family:arial,helvetica,sans-serif"><br></div><div class="gmail_default" style="color:rgb(0,0,0);font-family:arial,helvetica,sans-serif">This gets compiled (roughly) to </div><div class="gmail_default" style="color:rgb(0,0,0)"><font face="monospace, monospace"><br></font></div><div class="gmail_default"><div class="gmail_default"><font color="#000000" face="monospace, monospace">  mov eax, dword ptr [rsp - 4]</font></div><div class="gmail_default"><font color="#000000" face="monospace, monospace">  add eax, dword ptr [rsp - 8]</font></div><div class="gmail_default"><font color="#000000" face="monospace, monospace">  mov dword ptr [rsp - 12], eax</font></div></div><div class="gmail_default" style="color:rgb(0,0,0);font-family:arial,helvetica,sans-serif"><br></div><div class="gmail_default" style="color:rgb(0,0,0);font-family:arial,helvetica,sans-serif">In an opt pass, I would like to replace this series of four instructions with a single intrinsic, @llvm.cache.add, which will represent an add performed in the cache:</div><div class="gmail_default" style="color:rgb(0,0,0);font-family:arial,helvetica,sans-serif"><br></div><div class="gmail_default" style="color:rgb(0,0,0)"><font face="monospace, monospace">  call @llvm.cache.add(%ptr3, %ptr1, %ptr2)</font></div><div class="gmail_default" style="color:rgb(0,0,0);font-family:arial,helvetica,sans-serif"><br></div><div class="gmail_default" style="color:rgb(0,0,0);font-family:arial,helvetica,sans-serif">This intrinsic will compile (preferably) to a single instruction:</div><div class="gmail_default" style="color:rgb(0,0,0);font-family:arial,helvetica,sans-serif"><br></div><div class="gmail_default" style="color:rgb(0,0,0)"><font face="monospace, monospace">  cache_add dword <span style="color:rgb(0,0,0);font-size:small;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-weight:400;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline">[rsp - 12], dword <span style="color:rgb(0,0,0);font-size:small;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-weight:400;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline">[rsp - 4], dword <span style="color:rgb(0,0,0);font-size:small;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-weight:400;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline">[rsp - 8]</span>

</span></span></font></div><div class="gmail_default" style="color:rgb(0,0,0);font-family:arial,helvetica,sans-serif"><br></div><div class="gmail_default" style="color:rgb(0,0,0);font-family:arial,helvetica,sans-serif">When it comes time to generate code for this intrinsic, I am faced with a predicament: </div><div class="gmail_default" style=""><ol style="color:rgb(0,0,0);font-family:arial,helvetica,sans-serif"><li>I need to make sure %ptr3, %ptr1, and %ptr2 are allocated in register(s) (rsp in the example above) and,<br></li><li>I need to know <i>which</i> registers they are allocated in, as this will be needed to actually generate the instruction.</li></ol><div style="color:rgb(0,0,0);font-family:arial,helvetica,sans-serif">Unfortunately, I am stuck on both of these. I have begun researching into the code generation process, but am a little lost in the weeds. I found a page of documentation that seemed important (<a href="https://llvm.org/docs/CodeGenerator.html#mapping-virtual-registers-to-physical-registers">https://llvm.org/docs/CodeGenerator.html#mapping-virtual-registers-to-physical-registers</a>), however, it's a little above my current knowledge. Thus, I have some questions:</div><div style=""><ol style=""><li style=""><font color="#000000" face="arial, helvetica, sans-serif">To solve problem #1, I was planning on simply inserting the `call @llvm.cache.add` instruction <i>after</i> the store instruction in the initial grouping of four instructions. My intent is to ensure that the argument of the store (%ptr3) is defined before its use by `call @llvm.cache.add`. Is this safe?</font></li><li style=""><font color="#000000" face="arial, helvetica, sans-serif">Is the information in problem #2 readily accessible? If I have a value `i32* %ptr1`, is there some way to map that to the expression `[rsp - 4]`? Presumably, this is exactly what happens during code generation for the load and store instructions -- is there an easy way to imitate what they do?</font></li></ol><div><font color="#000000" face="arial, helvetica, sans-serif">Thanks for all the help so far!</font></div><div><font color="#000000" face="arial, helvetica, sans-serif"><br></font></div><div><font color="#000000" face="arial, helvetica, sans-serif">Gus, PSU</font></div></div></div></div></div>