Mark vector ctpop, cttz, and ctlz as Expand on x86.
[oota-llvm.git] / lib / Target / X86 / X86JITInfo.cpp
index deeea3d2867c90cb16b96d2272cb226286fd7cd2..b9e5d5ba88ebaceb59e44bc49fa21ea03cba8b72 100644 (file)
 #define DEBUG_TYPE "jit"
 #include "X86JITInfo.h"
 #include "X86Relocations.h"
+#include "X86Subtarget.h"
 #include "llvm/CodeGen/MachineCodeEmitter.h"
 #include "llvm/Config/alloca.h"
 #include <cstdlib>
-#include <iostream>
 using namespace llvm;
 
 #ifdef _MSC_VER
@@ -118,10 +118,9 @@ extern "C" {
   ASMPREFIX "X86CompilationCallback:\n"
     "pushl   %ebp\n"
     "movl    %esp, %ebp\n"    // Standard prologue
-#if FASTCC_NUM_INT_ARGS_INREGS > 0
     "pushl   %eax\n"
-    "pushl   %edx\n"          // Save EAX/EDX
-#endif
+    "pushl   %edx\n"          // Save EAX/EDX/ECX
+    "pushl   %ecx\n"
 #if defined(__APPLE__)
     "andl    $-16, %esp\n"    // Align ESP on 16-byte boundary
 #endif
@@ -131,11 +130,47 @@ extern "C" {
     "movl    %ebp, (%esp)\n"
     "call    " ASMPREFIX "X86CompilationCallback2\n"
     "movl    %ebp, %esp\n"    // Restore ESP
-#if FASTCC_NUM_INT_ARGS_INREGS > 0
-    "subl    $8, %esp\n"
+    "subl    $12, %esp\n"
+    "popl    %ecx\n"
+    "popl    %edx\n"
+    "popl    %eax\n"
+    "popl    %ebp\n"
+    "ret\n");
+
+  // Same as X86CompilationCallback but also saves XMM argument registers.
+  void X86CompilationCallback_SSE(void);
+  asm(
+    ".text\n"
+    ".align 8\n"
+    ".globl " ASMPREFIX  "X86CompilationCallback_SSE\n"
+  ASMPREFIX "X86CompilationCallback_SSE:\n"
+    "pushl   %ebp\n"
+    "movl    %esp, %ebp\n"    // Standard prologue
+    "pushl   %eax\n"
+    "pushl   %edx\n"          // Save EAX/EDX/ECX
+    "pushl   %ecx\n"
+    "andl    $-16, %esp\n"    // Align ESP on 16-byte boundary
+    // Save all XMM arg registers
+    "subl    $64, %esp\n"
+    "movaps  %xmm0, (%esp)\n"
+    "movaps  %xmm1, 16(%esp)\n"
+    "movaps  %xmm2, 32(%esp)\n"
+    "movaps  %xmm3, 48(%esp)\n"
+    "subl    $16, %esp\n"
+    "movl    4(%ebp), %eax\n" // Pass prev frame and return address
+    "movl    %eax, 4(%esp)\n"
+    "movl    %ebp, (%esp)\n"
+    "call    " ASMPREFIX "X86CompilationCallback2\n"
+    "addl    $16, %esp\n"
+    "movaps  48(%esp), %xmm3\n"
+    "movaps  32(%esp), %xmm2\n"
+    "movaps  16(%esp), %xmm1\n"
+    "movaps  (%esp), %xmm0\n"
+    "movl    %ebp, %esp\n"    // Restore ESP
+    "subl    $12, %esp\n"
+    "popl    %ecx\n"
     "popl    %edx\n"
     "popl    %eax\n"
-#endif
     "popl    %ebp\n"
     "ret\n");
 #else
@@ -145,7 +180,9 @@ extern "C" {
     __asm {
       push  eax
       push  edx
+      push  ecx
       call  X86CompilationCallback2
+      pop   ecx
       pop   edx
       pop   eax
       ret
@@ -155,7 +192,7 @@ extern "C" {
 
 #else // Not an i386 host
   void X86CompilationCallback() {
-    std::cerr << "Cannot call X86CompilationCallback() on a non-x86 arch!\n";
+    assert(0 && "Cannot call X86CompilationCallback() on a non-x86 arch!\n");
     abort();
   }
 #endif
@@ -168,9 +205,9 @@ extern "C" {
 #ifdef _MSC_VER
 extern "C" void X86CompilationCallback2() {
   assert(sizeof(size_t) == 4); // FIXME: handle Win64
-  unsigned *RetAddrLoc = (unsigned *)_AddressOfReturnAddress();
-  RetAddrLoc += 3;  // skip over ret addr, edx, eax
-  unsigned RetAddr = *RetAddrLoc;
+  intptr_t *RetAddrLoc = (intptr_t *)_AddressOfReturnAddress();
+  RetAddrLoc += 4;  // skip over ret addr, edx, eax, ecx
+  intptr_t RetAddr = *RetAddrLoc;
 #else
 extern "C" void X86CompilationCallback2(intptr_t *StackPtr, intptr_t RetAddr) {
   intptr_t *RetAddrLoc = &StackPtr[1];
@@ -182,56 +219,125 @@ extern "C" void X86CompilationCallback2(intptr_t *StackPtr, intptr_t RetAddr) {
   bool isStub = ((unsigned char*)RetAddr)[0] == 0xCD;
 
   // The call instruction should have pushed the return value onto the stack...
+#ifdef __x86_64__
+  RetAddr--;     // Backtrack to the reference itself...
+#else
   RetAddr -= 4;  // Backtrack to the reference itself...
+#endif
 
 #if 0
-  DEBUG(std::cerr << "In callback! Addr=" << (void*)RetAddr
-                  << " ESP=" << (void*)StackPtr
-                  << ": Resolving call to function: "
-                  << TheVM->getFunctionReferencedName((void*)RetAddr) << "\n");
+  DOUT << "In callback! Addr=" << (void*)RetAddr
+       << " ESP=" << (void*)StackPtr
+       << ": Resolving call to function: "
+       << TheVM->getFunctionReferencedName((void*)RetAddr) << "\n";
 #endif
 
   // Sanity check to make sure this really is a call instruction.
+#ifdef __x86_64__
+  assert(((unsigned char*)RetAddr)[-2] == 0x41 &&"Not a call instr!");
+  assert(((unsigned char*)RetAddr)[-1] == 0xFF &&"Not a call instr!");
+#else
   assert(((unsigned char*)RetAddr)[-1] == 0xE8 &&"Not a call instr!");
+#endif
 
   intptr_t NewVal = (intptr_t)JITCompilerFunction((void*)RetAddr);
 
   // Rewrite the call target... so that we don't end up here every time we
   // execute the call.
-  *(unsigned *)RetAddr = (unsigned)(NewVal-RetAddr-4);
+#ifdef __x86_64__
+  *(intptr_t *)(RetAddr - 0xa) = NewVal;
+#else
+  *(intptr_t *)RetAddr = (intptr_t)(NewVal-RetAddr-4);
+#endif
 
   if (isStub) {
     // If this is a stub, rewrite the call into an unconditional branch
     // instruction so that two return addresses are not pushed onto the stack
     // when the requested function finally gets called.  This also makes the
     // 0xCD byte (interrupt) dead, so the marker doesn't effect anything.
+#ifdef __x86_64__
+    ((unsigned char*)RetAddr)[0] = (2 | (4 << 3) | (3 << 6));
+#else
     ((unsigned char*)RetAddr)[-1] = 0xE9;
+#endif
   }
 
   // Change the return address to reexecute the call instruction...
+#ifdef __x86_64__
+  *RetAddrLoc -= 0xd;
+#else
   *RetAddrLoc -= 5;
+#endif
 }
 
 TargetJITInfo::LazyResolverFn
 X86JITInfo::getLazyResolverFunction(JITCompilerFn F) {
   JITCompilerFunction = F;
+
+#if (defined(__i386__) || defined(i386) || defined(_M_IX86)) && \
+  !defined(_MSC_VER) && !defined(__x86_64__)
+  unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
+  union {
+    unsigned u[3];
+    char     c[12];
+  } text;
+
+  if (!X86::GetCpuIDAndInfo(0, &EAX, text.u+0, text.u+2, text.u+1)) {
+    // FIXME: support for AMD family of processors.
+    if (memcmp(text.c, "GenuineIntel", 12) == 0) {
+      X86::GetCpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX);
+      if ((EDX >> 25) & 0x1)
+        return X86CompilationCallback_SSE;
+    }
+  }
+#endif
+
   return X86CompilationCallback;
 }
 
 void *X86JITInfo::emitFunctionStub(void *Fn, MachineCodeEmitter &MCE) {
   // Note, we cast to intptr_t here to silence a -pedantic warning that 
   // complains about casting a function pointer to a normal pointer.
-  if (Fn != (void*)(intptr_t)X86CompilationCallback) {
-    MCE.startFunctionStub(5);
+#if (defined(__i386__) || defined(i386) || defined(_M_IX86)) && \
+  !defined(_MSC_VER) && !defined(__x86_64__)
+  bool NotCC = (Fn != (void*)(intptr_t)X86CompilationCallback &&
+                Fn != (void*)(intptr_t)X86CompilationCallback_SSE);
+#else
+  bool NotCC = Fn != (void*)(intptr_t)X86CompilationCallback;
+#endif
+  if (NotCC) {
+#ifdef __x86_64__
+    MCE.startFunctionStub(13, 4);
+    MCE.emitByte(0x49);          // REX prefix
+    MCE.emitByte(0xB8+2);        // movabsq r10
+    MCE.emitWordLE(((unsigned *)&Fn)[0]);
+    MCE.emitWordLE(((unsigned *)&Fn)[1]);
+    MCE.emitByte(0x41);          // REX prefix
+    MCE.emitByte(0xFF);          // jmpq *r10
+    MCE.emitByte(2 | (4 << 3) | (3 << 6));
+#else
+    MCE.startFunctionStub(5, 4);
     MCE.emitByte(0xE9);
     MCE.emitWordLE((intptr_t)Fn-MCE.getCurrentPCValue()-4);
+#endif
     return MCE.finishFunctionStub(0);
   }
 
-  MCE.startFunctionStub(6);
+#ifdef __x86_64__
+  MCE.startFunctionStub(14, 4);
+  MCE.emitByte(0x49);          // REX prefix
+  MCE.emitByte(0xB8+2);        // movabsq r10
+  MCE.emitWordLE(((unsigned *)&Fn)[0]);
+  MCE.emitWordLE(((unsigned *)&Fn)[1]);
+  MCE.emitByte(0x41);          // REX prefix
+  MCE.emitByte(0xFF);          // callq *r10
+  MCE.emitByte(2 | (2 << 3) | (3 << 6));
+#else
+  MCE.startFunctionStub(6, 4);
   MCE.emitByte(0xE8);   // Call with 32 bit pc-rel destination...
 
   MCE.emitWordLE((intptr_t)Fn-MCE.getCurrentPCValue()-4);
+#endif
 
   MCE.emitByte(0xCD);   // Interrupt - Just a marker identifying the stub!
   return MCE.finishFunctionStub(0);
@@ -258,6 +364,9 @@ void X86JITInfo::relocate(void *Function, MachineRelocation *MR,
       // in memory.
       *((unsigned*)RelocPos) += (unsigned)ResultPtr;
       break;
+    case X86::reloc_absolute_dword:
+      *((intptr_t*)RelocPos) += ResultPtr;
+      break;
     }
   }
 }