<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 --- - clang crashes when doing 128 bit division"
   href="http://llvm.org/bugs/show_bug.cgi?id=16622">16622</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>clang crashes when doing 128 bit division
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>3.3
          </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>Driver
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>nruslan_devel@yahoo.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=10878" name="attach_10878" title="10-xfXqYg.c file">attachment 10878</a> <a href="attachment.cgi?id=10878&action=edit" title="10-xfXqYg.c file">[details]</a></span>
10-xfXqYg.c file

I have encountered a problem with clang when dividing 128-bit integer by 64-bit
integer to obtain 64-bit result. Platform is x86-64. (GCC seems to work fine
with this code.)

The following example first multiplies two 64-bit integers to obtain 128-bit
result (using clang capabilities) and then using an inline assembly divides it
by 64-bit integer to obtain 64-bit result:

("A" constraint is used for [RDX:RAX] in case of x86-64. It is [EDX:EAX] only
for x86; the crash message mentions 32-bit registers for some reason.)

#include <inttypes.h>

static inline uint64_t divide(__uint128_t a, uint64_t b)
{
    asm (
        "divq %1"
        : "+A" (a)
        : "rm" (b)
        : "cc"
    );
    return (uint64_t) a; /* Just leave the quotient. */
}

uint64_t check_divide(uint64_t a, uint64_t c, uint64_t d)
{
    return divide((__uint128_t) a * c, d);
}

Crash message:

fatal error: error in backend: Cannot select: 0x36567e0: i64 = build_pair
      0x36565e0, 0x36566e0 [ORD=21] [ID=46]
  0x36565e0: i32,ch,glue = CopyFromReg 0x36564e0, 0x3651cd0, 0x36564e0:1
[ORD=21] [ID=44]
    0x3651cd0: i32 = Register %EAX [ORD=21] [ID=14]
    0x36564e0: ch,glue = inlineasm 0x36560e0, 0x3653e50, 0x36520d0, 0x36526d0,
0x36522d0, 0x3651cd0, 0x3653d50, 0x36525d0, 0x36528d0, 0x36561e0, 0x36523d0,
0x3655fe0, 0x36562e0, 0x36563e0, 0x36560e0:1 [ORD=21] [ID=43]
      0x3653e50: i64 = TargetExternalSymbol'divq $1' [ORD=21] [ID=10]
      0x36526d0: i64 = TargetConstant<8> [ORD=21] [ID=12]
      0x36522d0: i32 = TargetConstant<18> [ORD=21] [ID=13]
      0x3651cd0: i32 = Register %EAX [ORD=21] [ID=14]
      0x3653d50: i32 = Register %EDX [ORD=21] [ID=15]
      0x36525d0: i64 = TargetConstant<14> [ORD=21] [ID=16]
      0x36528d0: i64 = FrameIndex<3> [ORD=21] [ID=9]
      0x36561e0: i32 = TargetConstant<-2147483631> [ORD=21] [ID=19]
      0x36523d0: i32 = Register %vreg7 [ORD=21] [ID=17]
      0x3655fe0: i32 = Register %vreg8 [ORD=21] [ID=18]
      0x36562e0: i32 = TargetConstant<12> [ORD=21] [ID=20]
      0x36563e0: i32 = Register %EFLAGS [ORD=21] [ID=21]
      0x36560e0: ch,glue = CopyToReg 0x36524d0, 0x3655fe0, 0x3654050,
0x36527d0:1 [ORD=21] [ID=42]
        0x3655fe0: i32 = Register %vreg8 [ORD=21] [ID=18]
        0x3654050: i32 = truncate 0x3651fd0 [ID=40]
          0x3651fd0: i64 = srl 0x365e8f0, 0x3655ce0 [ID=37]
            0x365e8f0: i64,ch = load 0x3654650, 0x3653c50,
0x3655be0<LD8[%2](align=16)> [ID=36]
              0x3653c50: i64 = FrameIndex<1> [ORD=17] [ID=6]
              0x3655be0: i64 = undef [ORD=13] [ID=3]
            0x3655ce0: i8 = Constant<32> [ID=23]
        0x36527d0: ch,glue = CopyToReg 0x36524d0, 0x36523d0, 0x36521d0 [ORD=21]
[ID=41]
          0x36523d0: i32 = Register %vreg7 [ORD=21] [ID=17]
          0x36521d0: i32 = truncate 0x365e8f0 [ID=38]
            0x365e8f0: i64,ch = load 0x3654650, 0x3653c50,
0x3655be0<LD8[%2](align=16)> [ID=36]
              0x3653c50: i64 = FrameIndex<1> [ORD=17] [ID=6]
              0x3655be0: i64 = undef [ORD=13] [ID=3]
  0x36566e0: i32,ch,glue = CopyFromReg 0x36565e0:1, 0x3653d50, 0x36565e0:2
[ORD=21] [ID=45]
    0x3653d50: i32 = Register %EDX [ORD=21] [ID=15]
    0x36565e0: i32,ch,glue = CopyFromReg 0x36564e0, 0x3651cd0, 0x36564e0:1
[ORD=21] [ID=44]
      0x3651cd0: i32 = Register %EAX [ORD=21] [ID=14]
      0x36564e0: ch,glue = inlineasm 0x36560e0, 0x3653e50, 0x36520d0,
0x36526d0, 0x36522d0, 0x3651cd0, 0x3653d50, 0x36525d0, 0x36528d0, 0x36561e0,
0x36523d0, 0x3655fe0, 0x36562e0, 0x36563e0, 0x36560e0:1 [ORD=21] [ID=43]
        0x3653e50: i64 = TargetExternalSymbol'divq $1' [ORD=21] [ID=10]
        0x36526d0: i64 = TargetConstant<8> [ORD=21] [ID=12]
        0x36522d0: i32 = TargetConstant<18> [ORD=21] [ID=13]
        0x3651cd0: i32 = Register %EAX [ORD=21] [ID=14]
        0x3653d50: i32 = Register %EDX [ORD=21] [ID=15]
        0x36525d0: i64 = TargetConstant<14> [ORD=21] [ID=16]
        0x36528d0: i64 = FrameIndex<3> [ORD=21] [ID=9]
        0x36561e0: i32 = TargetConstant<-2147483631> [ORD=21] [ID=19]
        0x36523d0: i32 = Register %vreg7 [ORD=21] [ID=17]
        0x3655fe0: i32 = Register %vreg8 [ORD=21] [ID=18]
        0x36562e0: i32 = TargetConstant<12> [ORD=21] [ID=20]
        0x36563e0: i32 = Register %EFLAGS [ORD=21] [ID=21]
        0x36560e0: ch,glue = CopyToReg 0x36524d0, 0x3655fe0, 0x3654050,
0x36527d0:1 [ORD=21] [ID=42]
          0x3655fe0: i32 = Register %vreg8 [ORD=21] [ID=18]
          0x3654050: i32 = truncate 0x3651fd0 [ID=40]
            0x3651fd0: i64 = srl 0x365e8f0, 0x3655ce0 [ID=37]
              0x365e8f0: i64,ch = load 0x3654650, 0x3653c50,
0x3655be0<LD8[%2](align=16)> [ID=36]
                0x3653c50: i64 = FrameIndex<1> [ORD=17] [ID=6]
                0x3655be0: i64 = undef [ORD=13] [ID=3]
              0x3655ce0: i8 = Constant<32> [ID=23]
          0x36527d0: ch,glue = CopyToReg 0x36524d0, 0x36523d0, 0x36521d0
[ORD=21] [ID=41]
            0x36523d0: i32 = Register %vreg7 [ORD=21] [ID=17]
            0x36521d0: i32 = truncate 0x365e8f0 [ID=38]
              0x365e8f0: i64,ch = load 0x3654650, 0x3653c50,
0x3655be0<LD8[%2](align=16)> [ID=36]
                0x3653c50: i64 = FrameIndex<1> [ORD=17] [ID=6]
                0x3655be0: i64 = undef [ORD=13] [ID=3]
In function: divide
clang: error: clang frontend command failed with exit code 70 (use -v to see
invocation)
clang version 3.3 (tags/RELEASE_33/final)
Target: x86_64-unknown-linux-gnu
Thread model: posix
clang: note: diagnostic msg: PLEASE submit a bug report to
<a href="http://llvm.org/bugs/">http://llvm.org/bugs/</a> and include the crash backtrace, preprocessed source, and
associated run script.
clang: note: diagnostic msg: 
********************

PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:
Preprocessed source(s) and associated run script(s) are located at:
clang: note: diagnostic msg: /tmp/10-xfXqYg.c
clang: note: diagnostic msg: /tmp/10-xfXqYg.sh
clang: note: diagnostic msg: 

********************</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>