<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 --- - integrated assembler rejects inlined add w9,w9,#-1 in AtomicIncrement"
   href="https://llvm.org/bugs/show_bug.cgi?id=23460">23460</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>integrated assembler rejects inlined add w9,w9,#-1 in AtomicIncrement
          </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>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>Backend: AArch64
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>chh@google.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>This test case shows that clang/llvm -target aarch64-lunix-android
failed to inline assembly code in NoBarrier_AtomicIncrement,
when the increment is -1.

There was no error when NoBarrier_AtomicIncrement is not inlined
or when -no-integrated-as is used.

$ cat a.cc

typedef int int32_t; 
typedef unsigned int Atomic32;
#ifdef NO_INLINE
__attribute__((noinline))
#endif
Atomic32 NoBarrier_AtomicIncrement(volatile Atomic32* ptr,
                                          Atomic32 increment) {
  Atomic32 result;
  int32_t temp;
  __asm__ __volatile__ (
    "0:                                       \n\t"
    "ldxr %w[result], %[ptr]                  \n\t"
    "add %w[result], %w[result], %w[increment]\n\t"
    "stxr %w[temp], %w[result], %[ptr]        \n\t"
    "cbnz %w[temp], 0b                        \n\t"
    : [result]"=&r" (result),
      [temp]"=&r" (temp),
      [ptr]"+Q" (*ptr)
    : [increment]"IJr" (increment)
    : "memory"
  );
  return result;
}
Atomic32* ptr;
void foo() {
  NoBarrier_AtomicIncrement(ptr, 1);
  NoBarrier_AtomicIncrement(ptr, -1);
}



$ clang++ -target aarch64-linux-android a.cc -S -O2 -o /tmp/x.O2.s
$.cc:12:49: error: expected compatible register, symbol or integer in range [0,
4095]
    "ldxr %w[result], %[ptr]                  \n\t" 
                                                ^
<inline asm>:3:14: note: instantiated into assembly here
        add w9, w9, #-1                         
                    ^
1 error generated.  

$ clang++ -target aarch64-linux-android a.cc -S -O2 -no-integrated-as -o
/tmp/x.O2.s

$ clang++ -target aarch64-linux-android a.cc -S -O2 -DNO_INLINE -o /tmp/x.O2.s


This code pattern appears in Android Open Source
external/v8/src/base/atomicops_internals_arm64_gcc.h,
which is now compiled with -no-integrated-as flag.


When compiled with -no-integrated-as:
The instruction
    add w9, w9, #-1
was correctly converted into
    sub w9, w9, #0x1


When compiled with -DNO_INLINE, the embedded assembly code is:
0000000000000000 <_Z25NoBarrier_AtomicIncrementPVjj>:
   0:   885f7c08    ldxr    w8, [x0]
   4:   0b010108    add w8, w8, w1
   8:   88097c08    stxr    w9, w8, [x0]
   c:   35ffffa9    cbnz    w9, 0 <_Z25NoBarrier_AtomicIncrementPVjj>
  10:   2a0803e0    mov w0, w8
  14:   d65f03c0    ret
and -1 was passed in w1.</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>