From 2c0a6a19ef42f2ad547dbc0693e55e082a21ac8b Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Sun, 30 Nov 2003 04:23:21 +0000 Subject: Emit constants to one contiguous block, but this time, respect alignment constraints. If this doesn't work Misha, feel free to revert it. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@10267 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/ExecutionEngine/JIT/JITEmitter.cpp | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) (limited to 'lib/ExecutionEngine') diff --git a/lib/ExecutionEngine/JIT/JITEmitter.cpp b/lib/ExecutionEngine/JIT/JITEmitter.cpp index 32d0651..1d04f99 100644 --- a/lib/ExecutionEngine/JIT/JITEmitter.cpp +++ b/lib/ExecutionEngine/JIT/JITEmitter.cpp @@ -17,11 +17,12 @@ #define _POSIX_MAPPED_FILES #endif #include "VM.h" +#include "llvm/Constant.h" +#include "llvm/Module.h" #include "llvm/CodeGen/MachineCodeEmitter.h" #include "llvm/CodeGen/MachineFunction.h" #include "llvm/CodeGen/MachineConstantPool.h" #include "llvm/Target/TargetData.h" -#include "llvm/Module.h" #include "Support/Debug.h" #include "Support/Statistic.h" #include "Config/unistd.h" @@ -187,11 +188,32 @@ void Emitter::finishFunction(MachineFunction &F) { void Emitter::emitConstantPool(MachineConstantPool *MCP) { const std::vector &Constants = MCP->getConstants(); + if (Constants.empty()) return; + + std::vector ConstantOffset; + ConstantOffset.reserve(Constants.size()); + + // Calculate how much space we will need for all the constants, and the offset + // each one will live in. + unsigned TotalSize = 0; + for (unsigned i = 0, e = Constants.size(); i != e; ++i) { + const Type *Ty = Constants[i]->getType(); + unsigned Size = TheVM->getTargetData().getTypeSize(Ty); + unsigned Alignment = TheVM->getTargetData().getTypeAlignment(Ty); + // Make sure to take into account the alignment requirements of the type. + TotalSize = (TotalSize + Alignment-1) & ~(Alignment-1); + + // Remember the offset this element lives at. + ConstantOffset.push_back(TotalSize); + TotalSize += Size; // Reserve space for the constant. + } + + // Now that we know how much memory to allocate, do so. + char *Pool = new char[TotalSize]; + + // Actually output all of the constants, and remember their addresses. for (unsigned i = 0, e = Constants.size(); i != e; ++i) { - // For now we just allocate some memory on the heap, this can be - // dramatically improved. - const Type *Ty = ((Value*)Constants[i])->getType(); - void *Addr = malloc(TheVM->getTargetData().getTypeSize(Ty)); + void *Addr = Pool + ConstantOffset[i]; TheVM->InitializeMemory(Constants[i], Addr); ConstantPoolAddresses.push_back(Addr); } -- cgit v1.1