[llvm-commits] CVS: reopt/lib/LightWtProfiling/TraceOptEmitter.cpp
Brian Gaeke
gaeke at cs.uiuc.edu
Mon Apr 12 15:58:01 PDT 2004
Changes in directory reopt/lib/LightWtProfiling:
TraceOptEmitter.cpp added (r1.1)
---
Log message:
This is the MachineCodeEmitter which we'll use to emit optimized traces
to memory. The first version you see here is a hacked-up version of
the target-independent JIT's Emitter.
---
Diffs of the changes: (+194 -0)
Index: reopt/lib/LightWtProfiling/TraceOptEmitter.cpp
diff -c /dev/null reopt/lib/LightWtProfiling/TraceOptEmitter.cpp:1.1
*** /dev/null Mon Apr 12 15:57:56 2004
--- reopt/lib/LightWtProfiling/TraceOptEmitter.cpp Mon Apr 12 15:57:43 2004
***************
*** 0 ****
--- 1,194 ----
+ //===-- TraceOptEmitter.cpp - Write machine code to executable memory -----===//
+ //
+ // The LLVM Compiler Infrastructure
+ //
+ // This file was developed by the LLVM research group and is distributed under
+ // the University of Illinois Open Source License. See LICENSE.TXT for details.
+ //
+ //===----------------------------------------------------------------------===//
+ //
+ // The trace reoptimizer uses this MachineCodeEmitter to output
+ // optimized machine code to memory, so that it can be executed instead of
+ // the corresponding unoptimized code.
+ //
+ // Currently, it uses the same executable memory segment that the
+ // TraceCache would use, but the TraceCache is unaware of it, so they can
+ // not be used together.
+ //
+ //===----------------------------------------------------------------------===//
+
+ #include "reopt/ScratchMemory.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 "Support/Debug.h"
+ #include "Support/DynamicLinker.h"
+ using namespace llvm;
+
+ namespace {
+ /// TraceOptEmitter - The trace reoptimizer's MachineCodeEmitter,
+ /// which is used to output functions to memory for execution.
+ ///
+ class TraceOptEmitter : public MachineCodeEmitter {
+ TargetData &TD;
+
+ // CurBlock - The start of the current block of memory. CurByte - The
+ // current byte being emitted to.
+ unsigned char *CurBlock, *CurByte;
+ unsigned char *FunctionBase, *CurFunctionPtr;
+
+ // ConstantPoolAddresses - Contains the location for each entry in the
+ // constant pool.
+ std::vector<void*> ConstantPoolAddresses;
+ public:
+ TraceOptEmitter (TargetData &_TD) : TD (_TD) {
+ // Re-use the existing DummyFunction area for code emission in the
+ // Reoptimizer. No memory is reserved for stubs.
+ FunctionBase = (unsigned char *) dummyFunction2;
+ // Allocate functions forward from the function base.
+ CurFunctionPtr = FunctionBase;
+ }
+
+ virtual void startFunction(MachineFunction &F);
+ virtual void finishFunction(MachineFunction &F);
+ virtual void emitConstantPool(MachineConstantPool *MCP);
+ virtual void startFunctionStub(const Function &F, unsigned StubSize) {
+ abort ();
+ }
+ virtual void* finishFunctionStub(const Function &F) {
+ abort ();
+ }
+ virtual void emitByte(unsigned char B);
+ virtual void emitWord(unsigned W);
+
+ virtual uint64_t getGlobalValueAddress(GlobalValue *V);
+ virtual uint64_t getGlobalValueAddress(const std::string &Name);
+ virtual uint64_t getConstantPoolEntryAddress(unsigned Entry);
+ virtual uint64_t getCurrentPCValue();
+
+ // forceCompilationOf - Force the compilation of the specified function, and
+ // return its address, because we REALLY need the address now.
+ //
+ // FIXME: This is JIT specific!
+ //
+ virtual uint64_t forceCompilationOf(Function *F);
+ };
+ }
+
+ MachineCodeEmitter *createTraceOptEmitter(TargetData &TD) {
+ return new TraceOptEmitter(TD);
+ }
+
+ void TraceOptEmitter::startFunction(MachineFunction &F) {
+ // Round up to a 64-bit word boundary.
+ CurBlock = (unsigned char*)(((intptr_t)CurFunctionPtr + 7) & ~7);
+ CurByte = CurBlock;
+ }
+
+ void TraceOptEmitter::finishFunction(MachineFunction &F) {
+ assert(CurByte > CurFunctionPtr);
+ CurFunctionPtr = CurByte;
+
+ ConstantPoolAddresses.clear();
+
+ DEBUG(std::cerr << "Finished CodeGen of [" << (void*)CurBlock
+ << "] Function: " << F.getFunction()->getName()
+ << ": " << CurByte-CurBlock << " bytes of text\n");
+ }
+
+ void TraceOptEmitter::emitConstantPool(MachineConstantPool *MCP) {
+ const std::vector<Constant*> &Constants = MCP->getConstants();
+ if (Constants.empty()) return;
+
+ std::vector<unsigned> 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 = TD.getTypeSize(Ty);
+ unsigned Alignment = TD.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) {
+ void *Addr = Pool + ConstantOffset[i];
+
+ // The original code did this:
+ // TheJIT->InitializeMemory(Constants[i], Addr);
+ // We need to steal this code from the ExecutionEngine (or something),
+ // for use in the Reoptimizer.
+ std::cerr << "FIXME: Can't initialize constants spilled to memory yet\n";
+ abort ();
+
+ ConstantPoolAddresses.push_back(Addr);
+ }
+ }
+
+ void TraceOptEmitter::emitByte(unsigned char B) {
+ *CurByte++ = B; // Write the byte to memory
+ }
+
+ void TraceOptEmitter::emitWord(unsigned W) {
+ // This won't work if the endianness of the host and target don't agree! (For
+ // a JIT this can't happen though. :)
+ *(unsigned*)CurByte = W;
+ CurByte += sizeof(unsigned);
+ }
+
+ uint64_t TraceOptEmitter::getGlobalValueAddress(GlobalValue *V) {
+ intptr_t Result = 0;
+ if (V->hasName ()) {
+ Result = (intptr_t)GetAddressOfSymbol (V->getName ());
+ }
+ if (!Result) {
+ std::cerr << "TraceOptEmitter can't find address of GlobalValue: " << *V;
+ abort ();
+ }
+ return Result;
+ }
+
+ uint64_t TraceOptEmitter::getGlobalValueAddress(const std::string &Name) {
+ intptr_t Result = 0;
+ Result = (intptr_t)GetAddressOfSymbol (Name);
+ if (!Result) {
+ std::cerr << "TraceOptEmitter can't find address of \"" << Name << "\"\n";
+ abort ();
+ }
+ return Result;
+ }
+
+ // getConstantPoolEntryAddress - Return the address of the 'ConstantNum' entry
+ // in the constant pool that was last emitted with the 'emitConstantPool'
+ // method.
+ //
+ uint64_t TraceOptEmitter::getConstantPoolEntryAddress(unsigned ConstantNum) {
+ assert(ConstantNum < ConstantPoolAddresses.size() &&
+ "Invalid ConstantPoolIndex!");
+ return (intptr_t)ConstantPoolAddresses[ConstantNum];
+ }
+
+ // getCurrentPCValue - This returns the address that the next emitted byte
+ // will be output to.
+ //
+ uint64_t TraceOptEmitter::getCurrentPCValue() {
+ return (intptr_t)CurByte;
+ }
+
+ uint64_t TraceOptEmitter::forceCompilationOf(Function *F) {
+ return getGlobalValueAddress (F);
+ }
More information about the llvm-commits
mailing list