<div dir="ltr">Hello Mentors,<div><br></div><div>I am currently finding bug in Local Function related optimization due to which runtime failures are observed in some test cases, as those test cases are containing very large function with recursion and object oriented code so I am not able to find a pattern which is causing failure. So I tried following simple case to understand expected behavior from this optimization.</div><div><br></div><div>Consider following code :</div><div><div><br></div><div>define void @bar() #0 {</div><div>  call void asm sideeffect "movl<span class="" style="white-space:pre"> </span>%ecx, %r15d", "~{r15}"() #0</div><div>  call void @foo()</div><div>  call void asm sideeffect "movl<span class="" style="white-space:pre"> </span>%r15d, %ebx", "~{rbx}"() #0</div><div>  ret void</div><div>}</div><div><br></div><div>define internal void @foo() #0 {</div><div>  call void asm sideeffect "movl<span class="" style="white-space:pre"> </span>%r14d, %r15d", "~{r15}"() #0</div><div>  ret void</div><div>}</div></div><div><br></div><div>and its generated assembly code when IPRA enabled:</div><div><br></div><div><div><span class="" style="white-space:pre">    </span>.section<span class="" style="white-space:pre">  </span>__TEXT,__text,regular,pure_instructions</div><div><span class="" style="white-space:pre">    </span>.macosx_version_min 10, 12</div><div><span class="" style="white-space:pre"> </span>.p2align<span class="" style="white-space:pre">  </span>4, 0x90</div><div>_foo:                                   ## @foo</div><div><span class="" style="white-space:pre">     </span>.cfi_startproc</div><div>## BB#0:</div><div><span class="" style="white-space:pre">      </span>## InlineAsm Start</div><div><span class="" style="white-space:pre"> </span>movl<span class="" style="white-space:pre">      </span>%r14d, %r15d</div><div><span class="" style="white-space:pre">       </span>## InlineAsm End</div><div><span class="" style="white-space:pre">   </span>retq</div><div><span class="" style="white-space:pre">       </span>.cfi_endproc</div><div><br></div><div><span class="" style="white-space:pre">      </span>.globl<span class="" style="white-space:pre">    </span>_bar</div><div><span class="" style="white-space:pre">       </span>.p2align<span class="" style="white-space:pre">  </span>4, 0x90</div><div>_bar:                                   ## @bar</div><div><span class="" style="white-space:pre">     </span>.cfi_startproc</div><div>## BB#0:</div><div><span class="" style="white-space:pre">      </span>pushq<span class="" style="white-space:pre">     </span>%r15</div><div>Ltmp0:</div><div><span class="" style="white-space:pre">  </span>.cfi_def_cfa_offset 16</div><div><span class="" style="white-space:pre">     </span>pushq<span class="" style="white-space:pre">     </span>%rbx</div><div>Ltmp1:</div><div><span class="" style="white-space:pre">  </span>.cfi_def_cfa_offset 24</div><div><span class="" style="white-space:pre">     </span>pushq<span class="" style="white-space:pre">     </span>%rax</div><div>Ltmp2:</div><div><span class="" style="white-space:pre">  </span>.cfi_def_cfa_offset 32</div><div>Ltmp3:</div><div><span class="" style="white-space:pre">        </span>.cfi_offset %rbx, -24</div><div>Ltmp4:</div><div><span class="" style="white-space:pre"> </span>.cfi_offset %r15, -16</div><div><span class="" style="white-space:pre">      </span>## InlineAsm Start</div><div><span class="" style="white-space:pre"> </span>movl<span class="" style="white-space:pre">      </span>%ecx, %r15d</div><div><span class="" style="white-space:pre">        </span>## InlineAsm End</div><div><span class="" style="white-space:pre">   </span>callq<span class="" style="white-space:pre">     </span>_foo</div><div><span class="" style="white-space:pre">       </span>## InlineAsm Start</div><div><span class="" style="white-space:pre"> </span>movl<span class="" style="white-space:pre">      </span>%r15d, %ebx</div><div><span class="" style="white-space:pre">        </span>## InlineAsm End</div><div><span class="" style="white-space:pre">   </span>addq<span class="" style="white-space:pre">      </span>$8, %rsp</div><div><span class="" style="white-space:pre">   </span>popq<span class="" style="white-space:pre">      </span>%rbx</div><div><span class="" style="white-space:pre">       </span>popq<span class="" style="white-space:pre">      </span>%r15</div><div><span class="" style="white-space:pre">       </span>retq</div><div><span class="" style="white-space:pre">       </span>.cfi_endproc</div><div><br></div><div><br></div><div>.subsections_via_symbols</div></div><div><br></div><div>now foo clobbers R15 (which is callee saved) but as foo is local function IPRA will mark R15 as clobbered and foo will not have save/restore for R15 in prologue/epilog . Now for above function code to work correctly in call site of foo in bar save and restore of R15 is expected but I am not able to find a pass in llvm which does that in fact if I am not wrong RegMasks of call site will be used by reg allocators by LiveIntervals::checkRegMaskInterference and due to that if R15 is marked clobbered  by call _foo then R15 will not be used for live-range which is spanned across call _foo. ( that it self is other concerns because it may result in virtual reg spill due to lack of available regs, as while setting callee saved regs none it will be propagated through regmaks) </div><div><br></div><div>Here are my questions related to this example:</div><div>1) Is there any pass or code in LLVM which is responsible for caller saved register for Physical Registers? By looking at InlineSpiller.cpp it is responsible for VReg spilling.</div><div>2) If such pass exists then why R15 is not saved around call __foo?</div><div>3) Why _bar is saving %rax in above code?</div><div><br></div><div>Please help!</div><div><br></div><div>Sincerely,</div><div>Vivek</div><div><br></div></div>