<html>
    <head>
      <base href="https://llvm.org/bugs/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - Machine code sinking is blocked when SUBREG_TO_REG is seen"
   href="https://llvm.org/bugs/show_bug.cgi?id=28852">28852</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Machine code sinking is blocked when SUBREG_TO_REG is seen
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libraries
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>Common Code Generator Code
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>wmi@google.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>For the testcase 1.c below:
-------------------------------------
unsigned x;

void foo(unsigned long value, unsigned kLengthBits, unsigned* bits, unsigned
long *bit_buffer_64) {
  long tmp = kLengthBits * x + 3;
  long length = tmp * 5;
  int v = 64 - value;
  if (__builtin_expect(*bits < (unsigned)(v), 0)) {
    *bit_buffer_64 += (value + length);
    *bits = length;
  }
}
-------------------------------------

length is only used in a cold branch, so it is beneficial to sink the
computation chain targeted to length to the cold branch. 

llvm moves "tmp * 5" to the cold branch, but doesn't move "kLengthBits * x +
3".
~/workarea/llvm-r277486/dbuild/bin/clang -O2 -S 1.c
foo:                                    # @foo
        .cfi_startproc
# BB#0:                                 # %entry
                                        # kill: %ESI<def> %ESI<kill> %RSI<def>
        imull   x(%rip), %esi
        addl    $3, %esi
        movl    $64, %eax
        subl    %edi, %eax
        cmpl    %eax, (%rdx)
        jb      .LBB0_1
.LBB0_2:                                # %if.end
        retq
.LBB0_1:                                # %if.then
        leaq    (%rsi,%rsi,4), %rax
        addq    %rax, %rdi
        addq    %rdi, (%rcx)
        movl    %eax, (%rdx)
        jmp     .LBB0_2
.Lfunc_end0:
        .size   foo, .Lfunc_end0-foo
        .cfi_endproc

gcc moves all the computations related with length to the cold branch.
~/workarea/gcc-r233722/build/install/bin/gcc -O2 -S 1.c
foo:
.LFB0:
        .cfi_startproc
        movl    $64, %eax
        subl    %edi, %eax
        cmpl    (%rdx), %eax
        ja      .L4
        rep ret
        .p2align 4,,10
        .p2align 3
.L4:
        imull   x(%rip), %esi
        leal    3(%rsi), %eax
        leaq    (%rax,%rax,4), %rax
        leaq    (%rdi,%rax), %rsi
        addq    %rsi, (%rcx)
        movl    %eax, (%rdx)
        ret
        .cfi_endproc

llvm doesn't move "kLengthBits * x + 3" because it see a SUBREG_TO_REG which is
generated by a zext for "kLengthBits * x + 3" from i32 to i64.

MachineSinking pass will stop sink instructions once SUBREG_TO_REG is seen.

  /// Return true if the instruction should be sunk by MachineSink.
  ///
  /// MachineSink determines on its own whether the instruction is safe to
sink;
  /// this gives the target a hook to override the default behavior with
regards
  /// to which instructions should be sunk.
  /// The default behavior is to not sink insert_subreg, subreg_to_reg, and
  /// reg_sequence. These are meant to be close to the source to make it easier
  /// to coalesce.
  virtual bool shouldSink(const MachineInstr &MI) const {
    return !MI.isInsertSubreg() && !MI.isSubregToReg() && !MI.isRegSequence();
  }

The check above was added at r114631.
------------------------------------------------------------------------
r114631 | evancheng | 2010-09-22 23:53:00 -0700 (Wed, 22 Sep 2010) | 3 lines

Don't sink insert_subreg, subreg_to_reg, reg_sequence. They are meant to be
close to their sources to facilitate coalescing.

------------------------------------------------------------------------

Considering the check was added before greedy register allocation was
introduced, is it still necessary for coalescing today?</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>