[LLVMbugs] [Bug 9737] New: C backend generates invalid asm code for ntohs compiled with -O3

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Sat Apr 16 06:54:03 PDT 2011


http://llvm.org/bugs/show_bug.cgi?id=9737

           Summary: C backend generates invalid asm code for ntohs
                    compiled with -O3
           Product: new-bugs
           Version: trunk
          Platform: PC
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: new bugs
        AssignedTo: unassignedbugs at nondot.org
        ReportedBy: maemarcus at gmail.com
                CC: llvmbugs at cs.uiuc.edu


Created an attachment (id=6480)
 --> (http://llvm.org/bugs/attachment.cgi?id=6480)
The case of incorrect asm statement generation in llvm C backend

Consider the following sample:

#include <netinet/in.h>
#include <stdint.h>
#include <stdio.h>

int main()
{
    uint16_t val1 = 15;
    uint16_t val2 = ntohs(val1);
    printf("val1 = %u, val2 = %u\n",
        (unsigned int)val1, (unsigned int)val2);
    return 0;
}

The correct result is:

[marcusmae at T61p ntohs]$ gcc ntohs.c -o ./ntohs
[marcusmae at T61p ntohs]$ ./ntohs
val1 = 15, val2 = 3840

Also, same compiled with -O3:

[marcusmae at T61p ntohs]$ gcc -O3 ntohs.c -o ./ntohs
[marcusmae at T61p ntohs]$ ./ntohs
val1 = 15, val2 = 3840

Now, let's pass through emitter & llc:

[marcusmae at T61p ntohs]$ dragonegg-gcc
-fplugin=/opt/llvm/dragonegg/lib64/dragonegg.so -fplugin-arg-dragonegg-emit-ir
-S ntohs.c -o ntohs.ll
[marcusmae at T61p ntohs]$ /opt/llvm/bin/llc -march=c ntohs.ll -o ntohs.ll.c
[marcusmae at T61p ntohs]$ gcc ntohs.ll.c -o ./ntohs-cgen
[marcusmae at T61p ntohs]$ ./ntohs-cgen 
val1 = 15, val2 = 3840

- Fine without optimizations. But if we add -O3:

[marcusmae at T61p ntohs]$ dragonegg-gcc -O3
-fplugin=/opt/llvm/dragonegg/lib64/dragonegg.so -fplugin-arg-dragonegg-emit-ir
-S ntohs.c -o ntohs.ll
[marcusmae at T61p ntohs]$ /opt/llvm/bin/llc -march=c ntohs.ll -o ntohs.ll.c
[marcusmae at T61p ntohs]$ gcc ntohs.ll.c -o ./ntohs-cgen
ntohs.ll.c: In function ‘main’:
ntohs.ll.c:165:36: error: expected ‘:’ or ‘)’ before string constant

There is some trouble with generated asm codes in ntohs.ll.c:

unsigned int main(void) {
  unsigned short llvm_cbe_asmtmp;
  unsigned int llvm_cbe_tmp__1;

  CODE_FOR_MAIN();
  __asm__ volatile ("rorw %%8, %w0"
        :"=r"(llvm_cbe_asmtmp)
        :"0"(((unsigned short )15))"cc");
  llvm_cbe_tmp__1 =  /*tail*/ printf(((&_OC_cst.array[((signed long long
)0ull)])), 15u, (((unsigned int )(unsigned short )llvm_cbe_asmtmp)));
  return 0u;
}

Full ntohs.ll.c attached.

-- 
Configure bugmail: http://llvm.org/bugs/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are on the CC list for the bug.



More information about the llvm-bugs mailing list