Several related changes:
authorChris Lattner <sabre@nondot.org>
Tue, 2 May 2006 23:22:24 +0000 (23:22 +0000)
committerChris Lattner <sabre@nondot.org>
Tue, 2 May 2006 23:22:24 +0000 (23:22 +0000)
1. Change several methods in the MachineCodeEmitter class to be pure virtual.
2. Suck emitConstantPool/initJumpTableInfo into startFunction, removing them
   from the MachineCodeEmitter interface, and reducing the amount of target-
   specific code.
3. Change the JITEmitter so that it allocates constantpools and jump tables
   *right* next to the functions that they belong to, instead of in a separate
   pool of memory.  This makes all memory for a function be contiguous, and
   means the JITEmitter only tracks one block of memory now.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@28065 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/CodeGen/MachineCodeEmitter.h
lib/CodeGen/ELFWriter.cpp
lib/ExecutionEngine/JIT/JITEmitter.cpp
lib/Target/Alpha/AlphaCodeEmitter.cpp
lib/Target/PowerPC/PPCCodeEmitter.cpp
lib/Target/X86/X86CodeEmitter.cpp

index d26d7e773f454d65c9affc20c856ccc33bd66cf6..f63d6ed0f422f6c5bdf544f05565f957fbe01d36 100644 (file)
@@ -65,31 +65,21 @@ public:
   /// about to be code generated.  This initializes the BufferBegin/End/Ptr
   /// fields.
   ///
-  virtual void startFunction(MachineFunction &F) {}
+  virtual void startFunction(MachineFunction &F) = 0;
 
   /// finishFunction - This callback is invoked when the specified function has
   /// finished code generation.  If a buffer overflow has occurred, this method
   /// returns true (the callee is required to try again), otherwise it returns
   /// false.
   ///
-  virtual bool finishFunction(MachineFunction &F) {
-    return CurBufferPtr == BufferEnd;
-  }
-
-  /// emitConstantPool - This callback is invoked to output the constant pool
-  /// for the function.
-  virtual void emitConstantPool(MachineConstantPool *MCP) {}
-
-  /// initJumpTableInfo - This callback is invoked by the JIT to allocate the
-  /// necessary memory to hold the jump tables.
-  virtual void initJumpTableInfo(MachineJumpTableInfo *MJTI) {}
+  virtual bool finishFunction(MachineFunction &F) = 0;
   
   /// emitJumpTableInfo - This callback is invoked to output the jump tables
   /// for the function.  In addition to a pointer to the MachineJumpTableInfo,
   /// this function also takes a map of MBBs to addresses, so that the final
   /// addresses of the MBBs can be written to the jump tables.
   virtual void emitJumpTableInfo(MachineJumpTableInfo *MJTI,
-                                 std::map<MachineBasicBlock*,uint64_t> &MBBM) {}
+                              std::map<MachineBasicBlock*,uint64_t> &MBBM) = 0;
   
   /// startFunctionStub - This callback is invoked when the JIT needs the
   /// address of a function that has not been code generated yet.  The StubSize
@@ -97,12 +87,12 @@ public:
   /// have constant pools, the can only use the other emitByte*/emitWord*
   /// methods.
   ///
-  virtual void startFunctionStub(unsigned StubSize) {}
+  virtual void startFunctionStub(unsigned StubSize) = 0;
 
   /// finishFunctionStub - This callback is invoked to terminate a function
   /// stub.
   ///
-  virtual void *finishFunctionStub(const Function *F) { return 0; }
+  virtual void *finishFunctionStub(const Function *F) = 0;
 
   /// emitByte - This callback is invoked when a byte needs to be written to the
   /// output stream.
index 483c49059c43b0361967c55f707217203bb026e1..69302a3605d6742d4ddae6c0af629704e34d15e4 100644 (file)
@@ -58,10 +58,6 @@ namespace llvm {
     void startFunction(MachineFunction &F);
     bool finishFunction(MachineFunction &F);
 
-    void emitConstantPool(MachineConstantPool *MCP) {
-      if (MCP->isEmpty()) return;
-      assert(0 && "unimp");
-    }
     void addRelocation(const MachineRelocation &MR) {
       assert(0 && "relo not handled yet!");
     }
@@ -73,6 +69,12 @@ namespace llvm {
       assert(0 && "JT not implementated yet!");
       return 0;
     }
+    
+    virtual void emitJumpTableInfo(MachineJumpTableInfo *MJTI,
+                                   std::map<MachineBasicBlock*,uint64_t> &MBBM){
+      assert(0 && "JT not implementated yet!");
+    }
+
 
     /// JIT SPECIFIC FUNCTIONS - DO NOT IMPLEMENT THESE HERE!
     void startFunctionStub(unsigned StubSize) {
index 47b45a0daf53d7b336abba266fad2e6a8bdbe3b6..7d8848e069819658294fd3f959e53681f3c366f3 100644 (file)
@@ -54,8 +54,7 @@ namespace {
   class JITMemoryManager {
     std::list<sys::MemoryBlock> Blocks; // List of blocks allocated by the JIT
     unsigned char *FunctionBase; // Start of the function body area
-    unsigned char *ConstantBase; // Memory allocated for constant pools
-    unsigned char *CurStubPtr, *CurFunctionPtr, *CurConstantPtr;
+    unsigned char *CurStubPtr, *CurFunctionPtr;
     unsigned char *GOTBase;      // Target Specific reserved memory
 
     // centralize memory block allocation
@@ -65,8 +64,6 @@ namespace {
     ~JITMemoryManager();
 
     inline unsigned char *allocateStub(unsigned StubSize);
-    inline unsigned char *allocateConstant(unsigned ConstantSize,
-                                           unsigned Alignment);
     inline unsigned char *startFunctionBody();
     inline void endFunctionBody(unsigned char *FunctionEnd);
     
@@ -82,21 +79,15 @@ namespace {
 JITMemoryManager::JITMemoryManager(bool useGOT) {
   // Allocate a 16M block of memory for functions
   sys::MemoryBlock FunBlock = getNewMemoryBlock(16 << 20);
-  // Allocate a 1M block of memory for Constants
-  sys::MemoryBlock ConstBlock = getNewMemoryBlock(1 << 20);
 
   Blocks.push_front(FunBlock);
-  Blocks.push_front(ConstBlock);
 
   FunctionBase = reinterpret_cast<unsigned char*>(FunBlock.base());
-  ConstantBase = reinterpret_cast<unsigned char*>(ConstBlock.base());
 
   // Allocate stubs backwards from the base, allocate functions forward
   // from the base.
   CurStubPtr = CurFunctionPtr = FunctionBase + 512*1024;// Use 512k for stubs
 
-  CurConstantPtr = ConstantBase + ConstBlock.size();
-
   // Allocate the GOT.
   GOTBase = NULL;
   if (useGOT) GOTBase = (unsigned char*)malloc(sizeof(void*) * 8192);
@@ -119,23 +110,6 @@ unsigned char *JITMemoryManager::allocateStub(unsigned StubSize) {
   return CurStubPtr;
 }
 
-unsigned char *JITMemoryManager::allocateConstant(unsigned ConstantSize,
-                                                  unsigned Alignment) {
-  // Reserve space and align pointer.
-  CurConstantPtr -= ConstantSize;
-  CurConstantPtr =
-    (unsigned char *)((intptr_t)CurConstantPtr & ~((intptr_t)Alignment - 1));
-
-  if (CurConstantPtr < ConstantBase) {
-    //Either allocate another MB or 2xConstantSize
-    sys::MemoryBlock ConstBlock = getNewMemoryBlock(2 * ConstantSize);
-    ConstantBase = reinterpret_cast<unsigned char*>(ConstBlock.base());
-    CurConstantPtr = ConstantBase + ConstBlock.size();
-    return allocateConstant(ConstantSize, Alignment);
-  }
-  return CurConstantPtr;
-}
-
 unsigned char *JITMemoryManager::startFunctionBody() {
   // Round up to an even multiple of 8 bytes, this should eventually be target
   // specific.
@@ -414,10 +388,12 @@ public:
 
     virtual void startFunction(MachineFunction &F);
     virtual bool finishFunction(MachineFunction &F);
-    virtual void emitConstantPool(MachineConstantPool *MCP);
-    virtual void initJumpTableInfo(MachineJumpTableInfo *MJTI);
+    
+    void emitConstantPool(MachineConstantPool *MCP);
+    void initJumpTableInfo(MachineJumpTableInfo *MJTI);
     virtual void emitJumpTableInfo(MachineJumpTableInfo *MJTI,
                                    std::map<MachineBasicBlock*,uint64_t> &MBBM);
+    
     virtual void startFunctionStub(unsigned StubSize);
     virtual void* finishFunctionStub(const Function *F);
 
@@ -471,10 +447,16 @@ void *JITEmitter::getPointerToGlobal(GlobalValue *V, void *Reference,
 
 void JITEmitter::startFunction(MachineFunction &F) {
   BufferBegin = CurBufferPtr = MemMgr.startFunctionBody();
-  TheJIT->updateGlobalMapping(F.getFunction(), BufferBegin);
   
   /// FIXME: implement out of space handling correctly!
   BufferEnd = (unsigned char*)(intptr_t)~0ULL;
+  
+  emitConstantPool(F.getConstantPool());
+  initJumpTableInfo(F.getJumpTableInfo());
+
+  // About to start emitting the machine code for the function.
+  // FIXME: align it?
+  TheJIT->updateGlobalMapping(F.getFunction(), CurBufferPtr);
 }
 
 bool JITEmitter::finishFunction(MachineFunction &F) {
@@ -548,10 +530,11 @@ void JITEmitter::emitConstantPool(MachineConstantPool *MCP) {
   unsigned Size = Constants.back().Offset;
   Size += TheJIT->getTargetData().getTypeSize(Constants.back().Val->getType());
 
-  ConstantPoolBase = MemMgr.allocateConstant(Size, 
-                                       1 << MCP->getConstantPoolAlignment());
+  ConstantPoolBase = allocateSpace(Size, 1 << MCP->getConstantPoolAlignment());
   ConstantPool = MCP;
-  
+
+  if (ConstantPoolBase == 0) return;  // Buffer overflow.
+
   // Initialize the memory for all of the constant pool entries.
   for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
     void *CAddr = (char*)ConstantPoolBase+Constants[i].Offset;
@@ -563,22 +546,23 @@ void JITEmitter::initJumpTableInfo(MachineJumpTableInfo *MJTI) {
   const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
   if (JT.empty()) return;
   
-  unsigned Size = 0;
-  unsigned EntrySize = MJTI->getEntrySize();
+  unsigned NumEntries = 0;
   for (unsigned i = 0, e = JT.size(); i != e; ++i)
-    Size += JT[i].MBBs.size() * EntrySize;
-  
+    NumEntries += JT[i].MBBs.size();
+
+  unsigned EntrySize = MJTI->getEntrySize();
+
   // Just allocate space for all the jump tables now.  We will fix up the actual
   // MBB entries in the tables after we emit the code for each block, since then
   // we will know the final locations of the MBBs in memory.
   JumpTable = MJTI;
-  JumpTableBase = MemMgr.allocateConstant(Size, MJTI->getAlignment());
+  JumpTableBase = allocateSpace(NumEntries * EntrySize, MJTI->getAlignment());
 }
 
 void JITEmitter::emitJumpTableInfo(MachineJumpTableInfo *MJTI,
                                    std::map<MachineBasicBlock*,uint64_t> &MBBM){
   const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
-  if (JT.empty()) return;
+  if (JT.empty() || JumpTableBase == 0) return;
 
   unsigned Offset = 0;
   unsigned EntrySize = MJTI->getEntrySize();
index a04cd37adcedbe6a9d61d606e4ce28f5d460eb48..1c3398ea07a886bac8509fbd2241f0ce646ccf5c 100644 (file)
@@ -81,7 +81,6 @@ bool AlphaCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
     BasicBlockAddrs.clear();
     
     MCE.startFunction(MF);
-    MCE.emitConstantPool(MF.getConstantPool());
     for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
       emitBasicBlock(*I);
   } while (MCE.finishFunction(MF));
index 6fe682920806e55340f556714d0dd03bcc706518..a01ae99df181c25f6341b41ec58156a15bcb8173 100644 (file)
@@ -90,8 +90,6 @@ bool PPCCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
     BBLocations.clear();
 
     MCE.startFunction(MF);
-    MCE.emitConstantPool(MF.getConstantPool());
-    MCE.initJumpTableInfo(MF.getJumpTableInfo());
     for (MachineFunction::iterator BB = MF.begin(), E = MF.end(); BB != E; ++BB)
       emitBasicBlock(*BB);
     MCE.emitJumpTableInfo(MF.getJumpTableInfo(), BBLocations);
index 175dae5ee1669579249707f54df6d538f544359f..20e17d0dbf52ca2efd4f9b9360b3a02d763e0c01 100644 (file)
@@ -86,8 +86,6 @@ bool Emitter::runOnMachineFunction(MachineFunction &MF) {
     BasicBlockAddrs.clear();
 
     MCE.startFunction(MF);
-    MCE.emitConstantPool(MF.getConstantPool());
-    MCE.initJumpTableInfo(MF.getJumpTableInfo());
     for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
       emitBasicBlock(*I);
     MCE.emitJumpTableInfo(MF.getJumpTableInfo(), BasicBlockAddrs);