<div dir="ltr">Hello LLVM-dev,<div><br></div><div>I think I've found an issue with the MachineSink optimization on a program that uses setjmp.  It looks like MachineSink will happily move a machine instruction into a following machine basic block (not necessarily a successor), even when that later block can be reached through a setjmp.  Here is some example debug output from llc that I'm seeing:</div><div><br></div><div><div><font face="monospace, monospace">Sinking along critical edge.</font></div><div><font face="monospace, monospace">Sink instr %vreg8<def,tied1> = ADD64rr %vreg14<tied0>, %vreg31, %EFLAGS<imp-def,dead>; GR64:%vreg8,%vreg14,%vreg31</font></div><div><font face="monospace, monospace">        into block BB#11:</font></div><div><font face="monospace, monospace">    Predecessors according to CFG: BB#8 BB#10 BB#32</font></div><div><font face="monospace, monospace"> ...</font></div><div><font face="monospace, monospace">        EH_SjLj_Setup <BB#36>, <regmask></font></div><div><font face="monospace, monospace">    Successors according to CFG: BB#34 BB#36</font></div></div><div><div><font face="monospace, monospace">Sinking along critical edge.</font></div><div><font face="monospace, monospace">Sink instr %vreg8<def,tied1> = ADD64rr %vreg14<tied0>, %vreg31, %EFLAGS<imp-def,dead>; GR64:%vreg8,%vreg14,%vreg31</font></div><div><font face="monospace, monospace">        into block BB#35:</font></div><div><font face="monospace, monospace">    Predecessors according to CFG: BB#34 BB#36</font></div><div><font face="monospace, monospace">..</font></div><div><font face="monospace, monospace">    Successors according to CFG: BB#12(12) BB#13(20)</font></div></div><div><br></div><div>In this case, it looks like MachineSink moves the ADD64rr instruction from BB#4 into BB#26, which post-dominates both BB#25 and BB#27.  This movement becomes a problem, I think, when the arguments of the moved instruction are spilled onto the stack.  Here is the final assembly of the affected basic blocks:</div><div><br></div><div><font face="monospace, monospace">.LBB0_19</font></div><div><font face="monospace, monospace">...</font></div><div><div><font face="monospace, monospace">        movq    %rbp, -104(%rbp)</font></div><div><font face="monospace, monospace">        movq    %rsp, -88(%rbp)</font></div><div><font face="monospace, monospace">        movq    $.LBB0_42, 8(%r15)</font></div></div><div><div><font face="monospace, monospace">        #EH_SjLj_Setup  .LBB0_42</font></div><div><font face="monospace, monospace"># BB#20:</font></div><div><font face="monospace, monospace">        movq    -176(%rbp), %rdi        # 8-byte Reload</font></div><div><font face="monospace, monospace">        xorl    %ecx, %ecx</font></div><div><font face="monospace, monospace">        jmp     .LBB0_21</font></div><div><font face="monospace, monospace">.LBB0_42:</font></div><div><font face="monospace, monospace">        movl    $1, %ecx</font></div><div><font face="monospace, monospace">        movq    -176(%rbp), %rdi        # 8-byte Reload</font></div><div><font face="monospace, monospace">.LBB0_21:</font></div><div><font face="monospace, monospace">        movq    -168(%rbp), %rax        # 8-byte Reload</font></div><div><font face="monospace, monospace">        addq    -144(%rbp), %rax        # 8-byte Folded Reload</font></div><div><font face="monospace, monospace">        movq    %rax, -168(%rbp)        # 8-byte Spill</font></div></div><div><br></div><div>In my code, because of the setjmp, basic block .LBB0_21 ends up executing twice, causing the addition to execute twice and produce an incorrect result.</div><div><br></div><div>It seems like moving non-idempotent operations past a setjmp in this way should be incorrect, because a corresponding longjmp can cause that operation to execute twice.  Am I missing something in this reasoning?</div><div><br></div><div>Thank you in advance for your help.  I apologize for not having a small test program handy.  The program in which I found this issue is somewhat large and complex.</div><div><br></div><div>Cheers,</div><div>TB</div></div>