<div dir="ltr">A late pass that adds CFI after block layout sounds like the right approach.</div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Jan 11, 2016 at 8:04 AM, Violeta Vukobrat <span dir="ltr"><<a href="mailto:violeta.vukobrat@rt-rk.com" target="_blank">violeta.vukobrat@rt-rk.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi all,<br>
<br>
We have been working on a solution for the problem of incorrect unwind info in function epilogue, reported in: <a href="http://lists.llvm.org/pipermail/llvm-dev/2015-August/089378.html" rel="noreferrer" target="_blank">http://lists.llvm.org/pipermail/llvm-dev/2015-August/089378.html</a>.<br>
The cause of the problem are missing CFI instructions that update the rule for calculating CFA in the epilogue. We added the instructions for updating the CFA calculation rule in the X86FrameLowering::emitEpilogue() method:<br>
 - for the case without FP, we added a cfi_def_cfa_offset instruction each time the stack pointer is changed<br>
 - and for the case when FP is used, we added a cfi_def_cfa instruction when frame pointer is 'pop'-ed (to use stack pointer register and initial offset for CFA calculation from then on).<br>
These changes enabled the generation of correct unwind info for the function epilogue, for the case when epilogue is the last basic block in a function.<br>
<br>
However, there are a few cases that cause problems with this solution, and all of them have epilogue somewhere in the middle of the function, and not as the last basic block:<br>
  1. Problem when there are EH instructions below the epilogue.<br>
    Example: a few tests in test-suite that have EH that physically comes after epilogue block. In the following example:<br>
<br>
   0:    53                       push   %rbx<br>
   1:    0f b6 05 00 00 00 00     movzbl 0x0(%rip),%eax        # 8 <_Z6throwsv+0x8><br>
   8:    83 e0 01                 and    $0x1,%eax<br>
   b:    83 f8 01                 cmp    $0x1,%eax<br>
   e:    74 07                    je     17 <_Z6throwsv+0x17><br>
  10:    b8 7b 00 00 00           mov    $0x7b,%eax<br>
  15:    5b                       pop    %rbx<br>
  16:    c3                       retq<br>
  17:    bf 04 00 00 00           mov    $0x4,%edi<br>
  1c:    e8 00 00 00 00           callq  21 <_Z6throwsv+0x21><br>
  21:    c7 00 07 00 00 00        movl   $0x7,(%rax)<br>
  27:    be 00 00 00 00           mov    $0x0,%esi<br>
                                ...<br>
<br>
    code after 'retq' instruction (location 16) has the CFA calculation rule set after the 'pop' instruction at (location 15). However, it should have the CFA calculation rule set by the prologue ('push' instruction at location 0), and not by the epilogue.<br>
  2. Problems that shrink wrapping pass can potentially cause:<br>
     2.1. Place prologue and epilogue somewhere in the middle of the function (not as the first/last blocks)<br>
     2.2. Separate the 'return' instruction from the rest of the epilogue. Its physical position in the function can be above or below the epilogue.<br>
     2.3. After inserting prologue and epilogue code, some of the later passes can reorder them and merge them with other basic blocks.<br>
  3. Problem when multiple epilogues exist in a function.<br>
<br>
In order to solve the described problems, and provide correct rules for calculating CFA at each instruction in the function, we started implementing a new pass. This pass should run after all basic block merging and reordering are done (once basic blocks are in their final order).<br>
  The pass goes through all basic blocks in the function and sets the correct cfi_def_cfa_offset at the beginning of each basic block. Because this introduces excess cfi instructions, the ones that are not necessary are eliminated (e.g. consecutive cfi_def_cfa_offset instructions with same offset value).<br>
  This is a work in progress, as we still need to add support for setting correct register at the beginning of each basic block (that is needed because we use cfi_def_cfa instruction for the case when FP is used).<br>
  We plan to upload a patch for review once we have a working solution, but for now, we wanted to see if anyone has any ideas or comments. Do you agree that this pass implementation is a right way to solve this issue?<span class="HOEnZb"><font color="#888888"><br>
<br>
-- <br>
Violeta Vukobrat<br>
Software Engineer<br>
RT-RK Computer Based Systems LLC<br>
<a href="http://www.rt-rk.com" rel="noreferrer" target="_blank">www.rt-rk.com</a><br>
<br>
</font></span></blockquote></div><br></div>