LLVM API Documentation

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

JITEmitter.cpp

Go to the documentation of this file.
00001 //===-- JITEmitter.cpp - Write machine code to executable memory ----------===//
00002 // 
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 // 
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines a MachineCodeEmitter object that is used by the JIT to
00011 // write machine code to memory and remember where relocatable values are.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #define DEBUG_TYPE "jit"
00016 #include "JIT.h"
00017 #include "llvm/Constant.h"
00018 #include "llvm/Module.h"
00019 #include "llvm/CodeGen/MachineCodeEmitter.h"
00020 #include "llvm/CodeGen/MachineFunction.h"
00021 #include "llvm/CodeGen/MachineConstantPool.h"
00022 #include "llvm/CodeGen/MachineRelocation.h"
00023 #include "llvm/Target/TargetData.h"
00024 #include "llvm/Target/TargetJITInfo.h"
00025 #include "llvm/Support/Debug.h"
00026 #include "llvm/ADT/Statistic.h"
00027 #include "llvm/System/Memory.h"
00028 using namespace llvm;
00029 
00030 namespace {
00031   Statistic<> NumBytes("jit", "Number of bytes of machine code compiled");
00032   JIT *TheJIT = 0;
00033 }
00034 
00035 
00036 //===----------------------------------------------------------------------===//
00037 // JITMemoryManager code.
00038 //
00039 namespace {
00040   /// JITMemoryManager - Manage memory for the JIT code generation in a logical,
00041   /// sane way.  This splits a large block of MAP_NORESERVE'd memory into two
00042   /// sections, one for function stubs, one for the functions themselves.  We
00043   /// have to do this because we may need to emit a function stub while in the
00044   /// middle of emitting a function, and we don't know how large the function we
00045   /// are emitting is.  This never bothers to release the memory, because when
00046   /// we are ready to destroy the JIT, the program exits.
00047   class JITMemoryManager {
00048     sys::MemoryBlock  MemBlock;  // Virtual memory block allocated RWX
00049     unsigned char *MemBase;      // Base of block of memory, start of stub mem
00050     unsigned char *FunctionBase; // Start of the function body area
00051     unsigned char *CurStubPtr, *CurFunctionPtr;
00052   public:
00053     JITMemoryManager();
00054     
00055     inline unsigned char *allocateStub(unsigned StubSize);
00056     inline unsigned char *startFunctionBody();
00057     inline void endFunctionBody(unsigned char *FunctionEnd);    
00058   };
00059 }
00060 
00061 JITMemoryManager::JITMemoryManager() {
00062   // Allocate a 16M block of memory...
00063   MemBlock = sys::Memory::AllocateRWX((16 << 20));
00064   MemBase = reinterpret_cast<unsigned char*>(MemBlock.base());
00065   FunctionBase = MemBase + 512*1024; // Use 512k for stubs
00066 
00067   // Allocate stubs backwards from the function base, allocate functions forward
00068   // from the function base.
00069   CurStubPtr = CurFunctionPtr = FunctionBase;
00070 }
00071 
00072 unsigned char *JITMemoryManager::allocateStub(unsigned StubSize) {
00073   CurStubPtr -= StubSize;
00074   if (CurStubPtr < MemBase) {
00075     std::cerr << "JIT ran out of memory for function stubs!\n";
00076     abort();
00077   }
00078   return CurStubPtr;
00079 }
00080 
00081 unsigned char *JITMemoryManager::startFunctionBody() {
00082   // Round up to an even multiple of 8 bytes, this should eventually be target
00083   // specific.
00084   return (unsigned char*)(((intptr_t)CurFunctionPtr + 7) & ~7);
00085 }
00086 
00087 void JITMemoryManager::endFunctionBody(unsigned char *FunctionEnd) {
00088   assert(FunctionEnd > CurFunctionPtr);
00089   CurFunctionPtr = FunctionEnd;
00090 }
00091 
00092 //===----------------------------------------------------------------------===//
00093 // JIT lazy compilation code.
00094 //
00095 namespace {
00096   /// JITResolver - Keep track of, and resolve, call sites for functions that
00097   /// have not yet been compiled.
00098   class JITResolver {
00099     /// MCE - The MachineCodeEmitter to use to emit stubs with.
00100     MachineCodeEmitter &MCE;
00101 
00102     /// LazyResolverFn - The target lazy resolver function that we actually
00103     /// rewrite instructions to use.
00104     TargetJITInfo::LazyResolverFn LazyResolverFn;
00105 
00106     // FunctionToStubMap - Keep track of the stub created for a particular
00107     // function so that we can reuse them if necessary.
00108     std::map<Function*, void*> FunctionToStubMap;
00109 
00110     // StubToFunctionMap - Keep track of the function that each stub corresponds
00111     // to.
00112     std::map<void*, Function*> StubToFunctionMap;
00113 
00114   public:
00115     JITResolver(MachineCodeEmitter &mce) : MCE(mce) {
00116       LazyResolverFn =
00117         TheJIT->getJITInfo().getLazyResolverFunction(JITCompilerFn);
00118     }
00119 
00120     /// getFunctionStub - This returns a pointer to a function stub, creating
00121     /// one on demand as needed.
00122     void *getFunctionStub(Function *F);
00123 
00124     /// AddCallbackAtLocation - If the target is capable of rewriting an
00125     /// instruction without the use of a stub, record the location of the use so
00126     /// we know which function is being used at the location.
00127     void *AddCallbackAtLocation(Function *F, void *Location) {
00128       /// Get the target-specific JIT resolver function.
00129       StubToFunctionMap[Location] = F;
00130       return (void*)LazyResolverFn;
00131     }
00132 
00133     /// JITCompilerFn - This function is called to resolve a stub to a compiled
00134     /// address.  If the LLVM Function corresponding to the stub has not yet
00135     /// been compiled, this function compiles it first.
00136     static void *JITCompilerFn(void *Stub);
00137   };
00138 }
00139 
00140 /// getJITResolver - This function returns the one instance of the JIT resolver.
00141 ///
00142 static JITResolver &getJITResolver(MachineCodeEmitter *MCE = 0) {
00143   static JITResolver TheJITResolver(*MCE);
00144   return TheJITResolver;
00145 }
00146 
00147 /// getFunctionStub - This returns a pointer to a function stub, creating
00148 /// one on demand as needed.
00149 void *JITResolver::getFunctionStub(Function *F) {
00150   // If we already have a stub for this function, recycle it.
00151   void *&Stub = FunctionToStubMap[F];
00152   if (Stub) return Stub;
00153 
00154   // Call the lazy resolver function unless we already KNOW it is an external
00155   // function, in which case we just skip the lazy resolution step.
00156   void *Actual = (void*)LazyResolverFn;
00157   if (F->hasExternalLinkage())
00158     Actual = TheJIT->getPointerToFunction(F);
00159     
00160   // Otherwise, codegen a new stub.  For now, the stub will call the lazy
00161   // resolver function.
00162   Stub = TheJIT->getJITInfo().emitFunctionStub(Actual, MCE);
00163 
00164   if (F->hasExternalLinkage()) {
00165     // If we are getting the stub for an external function, we really want the
00166     // address of the stub in the GlobalAddressMap for the JIT, not the address
00167     // of the external function.
00168     TheJIT->updateGlobalMapping(F, Stub);
00169   }
00170 
00171   DEBUG(std::cerr << "JIT: Stub emitted at [" << Stub << "] for function '"
00172                   << F->getName() << "'\n");
00173 
00174   // Finally, keep track of the stub-to-Function mapping so that the
00175   // JITCompilerFn knows which function to compile!
00176   StubToFunctionMap[Stub] = F;
00177   return Stub;
00178 }
00179 
00180 /// JITCompilerFn - This function is called when a lazy compilation stub has
00181 /// been entered.  It looks up which function this stub corresponds to, compiles
00182 /// it if necessary, then returns the resultant function pointer.
00183 void *JITResolver::JITCompilerFn(void *Stub) {
00184   JITResolver &JR = getJITResolver();
00185   
00186   // The address given to us for the stub may not be exactly right, it might be
00187   // a little bit after the stub.  As such, use upper_bound to find it.
00188   std::map<void*, Function*>::iterator I =
00189     JR.StubToFunctionMap.upper_bound(Stub);
00190   assert(I != JR.StubToFunctionMap.begin() && "This is not a known stub!");
00191   Function *F = (--I)->second;
00192 
00193   // The target function will rewrite the stub so that the compilation callback
00194   // function is no longer called from this stub.
00195   JR.StubToFunctionMap.erase(I);
00196 
00197   DEBUG(std::cerr << "JIT: Lazily resolving function '" << F->getName()
00198                   << "' In stub ptr = " << Stub << " actual ptr = "
00199                   << I->first << "\n");
00200 
00201   void *Result = TheJIT->getPointerToFunction(F);
00202 
00203   // We don't need to reuse this stub in the future, as F is now compiled.
00204   JR.FunctionToStubMap.erase(F);
00205 
00206   // FIXME: We could rewrite all references to this stub if we knew them.
00207   return Result;
00208 }
00209 
00210 
00211 // getPointerToFunctionOrStub - If the specified function has been
00212 // code-gen'd, return a pointer to the function.  If not, compile it, or use
00213 // a stub to implement lazy compilation if available.
00214 //
00215 void *JIT::getPointerToFunctionOrStub(Function *F) {
00216   // If we have already code generated the function, just return the address.
00217   if (void *Addr = getPointerToGlobalIfAvailable(F))
00218     return Addr;
00219 
00220   // Get a stub if the target supports it
00221   return getJITResolver(MCE).getFunctionStub(F);
00222 }
00223 
00224 
00225 
00226 //===----------------------------------------------------------------------===//
00227 // JITEmitter code.
00228 //
00229 namespace {
00230   /// JITEmitter - The JIT implementation of the MachineCodeEmitter, which is
00231   /// used to output functions to memory for execution.
00232   class JITEmitter : public MachineCodeEmitter {
00233     JITMemoryManager MemMgr;
00234 
00235     // CurBlock - The start of the current block of memory.  CurByte - The
00236     // current byte being emitted to.
00237     unsigned char *CurBlock, *CurByte;
00238 
00239     // When outputting a function stub in the context of some other function, we
00240     // save CurBlock and CurByte here.
00241     unsigned char *SavedCurBlock, *SavedCurByte;
00242 
00243     // ConstantPoolAddresses - Contains the location for each entry in the
00244     // constant pool.
00245     std::vector<void*> ConstantPoolAddresses;
00246 
00247     /// Relocations - These are the relocations that the function needs, as
00248     /// emitted.
00249     std::vector<MachineRelocation> Relocations;
00250   public:
00251     JITEmitter(JIT &jit) { TheJIT = &jit; }
00252 
00253     virtual void startFunction(MachineFunction &F);
00254     virtual void finishFunction(MachineFunction &F);
00255     virtual void emitConstantPool(MachineConstantPool *MCP);
00256     virtual void startFunctionStub(unsigned StubSize);
00257     virtual void* finishFunctionStub(const Function *F);
00258     virtual void emitByte(unsigned char B);
00259     virtual void emitWord(unsigned W);
00260     virtual void emitWordAt(unsigned W, unsigned *Ptr);
00261 
00262     virtual void addRelocation(const MachineRelocation &MR) {
00263       Relocations.push_back(MR);
00264     }
00265 
00266     virtual uint64_t getCurrentPCValue();
00267     virtual uint64_t getCurrentPCOffset();
00268     virtual uint64_t getConstantPoolEntryAddress(unsigned Entry);
00269 
00270   private:
00271     void *getPointerToGlobal(GlobalValue *GV, void *Reference, bool NoNeedStub);
00272   };
00273 }
00274 
00275 MachineCodeEmitter *JIT::createEmitter(JIT &jit) {
00276   return new JITEmitter(jit);
00277 }
00278 
00279 void *JITEmitter::getPointerToGlobal(GlobalValue *V, void *Reference,
00280                                      bool DoesntNeedStub) {
00281   if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
00282     /// FIXME: If we straightened things out, this could actually emit the
00283     /// global immediately instead of queuing it for codegen later!
00284     return TheJIT->getOrEmitGlobalVariable(GV);
00285   }
00286 
00287   // If we have already compiled the function, return a pointer to its body.
00288   Function *F = cast<Function>(V);
00289   void *ResultPtr = TheJIT->getPointerToGlobalIfAvailable(F);
00290   if (ResultPtr) return ResultPtr;
00291 
00292   if (F->hasExternalLinkage() && F->isExternal()) {
00293     // If this is an external function pointer, we can force the JIT to
00294     // 'compile' it, which really just adds it to the map.
00295     if (DoesntNeedStub)
00296       return TheJIT->getPointerToFunction(F);
00297 
00298     return getJITResolver(this).getFunctionStub(F);
00299   }
00300 
00301   // Okay, the function has not been compiled yet, if the target callback
00302   // mechanism is capable of rewriting the instruction directly, prefer to do
00303   // that instead of emitting a stub.
00304   if (DoesntNeedStub)
00305     return getJITResolver(this).AddCallbackAtLocation(F, Reference);
00306 
00307   // Otherwise, we have to emit a lazy resolving stub.
00308   return getJITResolver(this).getFunctionStub(F);
00309 }
00310 
00311 void JITEmitter::startFunction(MachineFunction &F) {
00312   CurByte = CurBlock = MemMgr.startFunctionBody();
00313   TheJIT->addGlobalMapping(F.getFunction(), CurBlock);
00314 }
00315 
00316 void JITEmitter::finishFunction(MachineFunction &F) {
00317   MemMgr.endFunctionBody(CurByte);
00318   ConstantPoolAddresses.clear();
00319   NumBytes += CurByte-CurBlock;
00320 
00321   if (!Relocations.empty()) {
00322     // Resolve the relocations to concrete pointers.
00323     for (unsigned i = 0, e = Relocations.size(); i != e; ++i) {
00324       MachineRelocation &MR = Relocations[i];
00325       void *ResultPtr;
00326       if (MR.isString())
00327         ResultPtr = TheJIT->getPointerToNamedFunction(MR.getString());
00328       else
00329         ResultPtr = getPointerToGlobal(MR.getGlobalValue(),
00330                                        CurBlock+MR.getMachineCodeOffset(),
00331                                        MR.doesntNeedFunctionStub());
00332       MR.setResultPointer(ResultPtr);
00333     }
00334 
00335     TheJIT->getJITInfo().relocate(CurBlock, &Relocations[0],
00336                                   Relocations.size());
00337   }
00338 
00339   DEBUG(std::cerr << "JIT: Finished CodeGen of [" << (void*)CurBlock
00340                   << "] Function: " << F.getFunction()->getName()
00341                   << ": " << CurByte-CurBlock << " bytes of text, "
00342                   << Relocations.size() << " relocations\n");
00343   Relocations.clear();
00344 }
00345 
00346 void JITEmitter::emitConstantPool(MachineConstantPool *MCP) {
00347   const std::vector<Constant*> &Constants = MCP->getConstants();
00348   if (Constants.empty()) return;
00349 
00350   std::vector<unsigned> ConstantOffset;
00351   ConstantOffset.reserve(Constants.size());
00352 
00353   // Calculate how much space we will need for all the constants, and the offset
00354   // each one will live in.
00355   unsigned TotalSize = 0;
00356   for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
00357     const Type *Ty = Constants[i]->getType();
00358     unsigned Size      = TheJIT->getTargetData().getTypeSize(Ty);
00359     unsigned Alignment = TheJIT->getTargetData().getTypeAlignment(Ty);
00360     // Make sure to take into account the alignment requirements of the type.
00361     TotalSize = (TotalSize + Alignment-1) & ~(Alignment-1);
00362 
00363     // Remember the offset this element lives at.
00364     ConstantOffset.push_back(TotalSize);
00365     TotalSize += Size;   // Reserve space for the constant.
00366   }
00367 
00368   // Now that we know how much memory to allocate, do so.
00369   char *Pool = new char[TotalSize];
00370 
00371   // Actually output all of the constants, and remember their addresses.
00372   for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
00373     void *Addr = Pool + ConstantOffset[i];
00374     TheJIT->InitializeMemory(Constants[i], Addr);
00375     ConstantPoolAddresses.push_back(Addr);
00376   }
00377 }
00378 
00379 void JITEmitter::startFunctionStub(unsigned StubSize) {
00380   SavedCurBlock = CurBlock;  SavedCurByte = CurByte;
00381   CurByte = CurBlock = MemMgr.allocateStub(StubSize);
00382 }
00383 
00384 void *JITEmitter::finishFunctionStub(const Function *F) {
00385   NumBytes += CurByte-CurBlock;
00386   std::swap(CurBlock, SavedCurBlock);
00387   CurByte = SavedCurByte;
00388   return SavedCurBlock;
00389 }
00390 
00391 void JITEmitter::emitByte(unsigned char B) {
00392   *CurByte++ = B;   // Write the byte to memory
00393 }
00394 
00395 void JITEmitter::emitWord(unsigned W) {
00396   // This won't work if the endianness of the host and target don't agree!  (For
00397   // a JIT this can't happen though.  :)
00398   *(unsigned*)CurByte = W;
00399   CurByte += sizeof(unsigned);
00400 }
00401 
00402 void JITEmitter::emitWordAt(unsigned W, unsigned *Ptr) {
00403   *Ptr = W;
00404 }
00405 
00406 // getConstantPoolEntryAddress - Return the address of the 'ConstantNum' entry
00407 // in the constant pool that was last emitted with the 'emitConstantPool'
00408 // method.
00409 //
00410 uint64_t JITEmitter::getConstantPoolEntryAddress(unsigned ConstantNum) {
00411   assert(ConstantNum < ConstantPoolAddresses.size() &&
00412    "Invalid ConstantPoolIndex!");
00413   return (intptr_t)ConstantPoolAddresses[ConstantNum];
00414 }
00415 
00416 // getCurrentPCValue - This returns the address that the next emitted byte
00417 // will be output to.
00418 //
00419 uint64_t JITEmitter::getCurrentPCValue() {
00420   return (intptr_t)CurByte;
00421 }
00422 
00423 uint64_t JITEmitter::getCurrentPCOffset() {
00424   return (intptr_t)CurByte-(intptr_t)CurBlock;
00425 }
00426 
00427 // getPointerToNamedFunction - This function is used as a global wrapper to
00428 // JIT::getPointerToNamedFunction for the purpose of resolving symbols when
00429 // bugpoint is debugging the JIT. In that scenario, we are loading an .so and
00430 // need to resolve function(s) that are being mis-codegenerated, so we need to
00431 // resolve their addresses at runtime, and this is the way to do it.
00432 extern "C" {
00433   void *getPointerToNamedFunction(const char *Name) {
00434     Module &M = TheJIT->getModule();
00435     if (Function *F = M.getNamedFunction(Name))
00436       return TheJIT->getPointerToFunction(F);
00437     return TheJIT->getPointerToNamedFunction(Name);
00438   }
00439 }