<html>
    <head>
      <base href="http://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 --- - [x86_64] CodeGen generates an instruction that is impossible to encode in x86_64."
   href="http://llvm.org/bugs/show_bug.cgi?id=17902">17902</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>[x86_64] CodeGen generates an instruction that is impossible to encode in x86_64.
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>new-bugs
          </td>
        </tr>

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

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

        <tr>
          <th>OS</th>
          <td>Linux
          </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>new bugs
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>madhusudancs@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvmbugs@cs.uiuc.edu
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=11527" name="attach_11527" title="C snippet to reproduce the bug">attachment 11527</a> <a href="attachment.cgi?id=11527&action=edit" title="C snippet to reproduce the bug">[details]</a></span>
C snippet to reproduce the bug

I ran into a subtle bug related to x86_64 instruction encoding when using LLVM
JIT. (I am using LLVM 3.3 release version. There is no 3.3 in the Version field
above, so I have chosen trunk as the version).

I am attaching the super-reduced test case (the C file) that reproduces the
bug. Just to be on the same page, I ran the following commands to obtain the
.ll, .o and .s files:

$ clang -cc1 -O1 -emit-llvm x86encodingbug.c
$ llc -O0 --regalloc=fast -filetype=obj x86encodingbug.ll
$ llc -O0 --regalloc=fast -filetype=asm x86encodingbug.ll

If we compare line x86encodingbug.s:62 with the equivalent instruction in the
x86encodingbug.o file (through objdump -S) the bug is kind of evident.

Intended instruction:         movb    %ah, (%rdi,%r8)
Actual instruction:           mov    %spl,(%rdi,%r8,1)

Note the %ah vs %spl register.

Running this code through lli using the following command line arguments:

$ lli -O0 --regalloc=fast x86encodingbug.ll 0 a
String: �

where the expected result was "String: 7" (ASCII code of 'a' is 97).

Notes in page 977 (or Vol. 2A 3-503) of Intel's manual:
<a href="http://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-software-developer-manual-325462.pdf">http://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-software-developer-manual-325462.pdf</a>
states that this instruction cannot be encoded:

"""
***In 64-bit mode, r/m8 can not be encoded to access the following byte
registers if a REX prefix is used: AH, BH, CH, DH.
"""

x86's RegisterInfo Target Descriptor
(llvm/lib/Target/X86/X86RegisterInfo.td:60) also mentions this and yet an
instruction which cannot be encoded is generated.

This was probably a regression introduced by the commits that fixed <a class="bz_bug_link 
          bz_status_RESOLVED  bz_closed"
   title="RESOLVED FIXED - llc -O3: invalid assembler: Error: can't encode register '%dh' in an instruction requiring REX prefix."
   href="show_bug.cgi?id=6151">bug #6151</a>.</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>