Support for constant islands in the ARM JIT.
authorJim Grosbach <grosbach@apple.com>
Tue, 28 Oct 2008 18:25:49 +0000 (18:25 +0000)
committerJim Grosbach <grosbach@apple.com>
Tue, 28 Oct 2008 18:25:49 +0000 (18:25 +0000)
Since the ARM constant pool handling supercedes the standard LLVM constant
pool entirely, the JIT emitter does not allocate space for the constants,
nor initialize the memory. The constant pool is considered part of the
instruction stream.

Likewise, when resolving relocations into the constant pool, a hook into
the target back end is used to resolve from the constant ID# to the
address where the constant is stored.

For now, the support in the ARM emitter is limited to 32-bit integer. Future
patches will expand this to the full range of constants necessary.

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

include/llvm/Target/TargetJITInfo.h
lib/ExecutionEngine/JIT/JITEmitter.cpp
lib/Target/ARM/ARMCodeEmitter.cpp
lib/Target/ARM/ARMJITInfo.h

index 142f0efbd3f5f71e3c22df8f8192ae7056a4589c..f7c9b0d0aba98538af12ae8e06f42a9df81505a7 100644 (file)
@@ -107,6 +107,15 @@ namespace llvm {
     // JIT to manage a GOT for it.
     bool needsGOT() const { return useGOT; }
 
+    /// hasCustomConstantPool - Allows a target to specify that constant
+    /// pool address resolution is handled by the target.
+    virtual bool hasCustomConstantPool() const { return false; }
+
+    /// getCustomConstantPoolEntryAddress - When using a custom constant
+    /// pool, resolve a constant pool index to the address of where the
+    /// entry is stored.
+    virtual intptr_t getCustomConstantPoolEntryAddress(unsigned CPI) const 
+      {return 0;}
   protected:
     bool useGOT;
   };
index 688d4984c20080315d890dab0632eb6b2cf74f8b..44e6638562c123e00079c9efec11c8b1a5cf3b7e 100644 (file)
@@ -1011,6 +1011,11 @@ void* JITEmitter::allocateSpace(intptr_t Size, unsigned Alignment) {
 }
 
 void JITEmitter::emitConstantPool(MachineConstantPool *MCP) {
+  if (TheJIT->getJITInfo().hasCustomConstantPool()) {
+    DOUT << "JIT: Target has custom constant pool handling. Omitting standard "
+            "constant pool\n";
+    return;
+  }
   const std::vector<MachineConstantPoolEntry> &Constants = MCP->getConstants();
   if (Constants.empty()) return;
 
@@ -1124,6 +1129,10 @@ void *JITEmitter::finishFunctionStub(const GlobalValue* F) {
 // method.
 //
 intptr_t JITEmitter::getConstantPoolEntryAddress(unsigned ConstantNum) const {
+  if (TheJIT->getJITInfo().hasCustomConstantPool()) {
+    return TheJIT->getJITInfo().getCustomConstantPoolEntryAddress(ConstantNum);
+  }
+
   assert(ConstantNum < ConstantPool->getConstants().size() &&
          "Invalid ConstantPoolIndex!");
   return (intptr_t)ConstantPoolBase +
index 574c14e00c6fcd666d06ea1a019999ba9e5c9cfd..641e1a1205f19bd8af254ef75c33c5da2c0eae81 100644 (file)
@@ -19,6 +19,8 @@
 #include "ARMRelocations.h"
 #include "ARMSubtarget.h"
 #include "ARMTargetMachine.h"
+#include "llvm/Constants.h"
+#include "llvm/DerivedTypes.h"
 #include "llvm/Function.h"
 #include "llvm/PassManager.h"
 #include "llvm/CodeGen/MachineCodeEmitter.h"
@@ -358,7 +360,29 @@ unsigned ARMCodeEmitter::getAddrMode1SBit(const MachineInstr &MI,
 }
 
 void ARMCodeEmitter::emitConstPoolInstruction(const MachineInstr &MI) {
-  // FIXME
+  unsigned CPID = MI.getOperand(0).getImm();
+  unsigned CPIndex = MI.getOperand(1).getIndex();
+  const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPIndex];
+  
+  //FIXME: Can we get these here?
+  assert (!MCPE.isMachineConstantPoolEntry());
+
+  const Constant *CV = MCPE.Val.ConstVal;  
+  // FIXME: We can get other types here. Need to handle them.
+  // According to the constant island pass, everything is multiples,
+  // of 4-bytes in size, though, so that helps.
+  assert (CV->getType()->isInteger());
+  assert (cast<IntegerType>(CV->getType())->getBitWidth() == 32);
+
+  const ConstantInt *CI = dyn_cast<ConstantInt>(CV);
+  uint32_t Val = *(uint32_t*)CI->getValue().getRawData();
+
+  DOUT << "Constant pool #" << CPID << ", value '" << Val << "' @ " << 
+    (void*)MCE.getCurrentPCValue() << "\n";
+
+  if (JTI)
+    JTI->mapCPIDtoAddress(CPID, MCE.getCurrentPCValue());
+  MCE.emitWordLE(Val);
 }
 
 void ARMCodeEmitter::emitPseudoInstruction(const MachineInstr &MI) {
index de3ff08ad2a269082710019786cf7564ddca187b..62b83030106b229ec3e4b49a213181d52aa4cebb 100644 (file)
 #define ARMJITINFO_H
 
 #include "llvm/Target/TargetJITInfo.h"
+#include <map>
 
 namespace llvm {
   class ARMTargetMachine;
 
   class ARMJITInfo : public TargetJITInfo {
     ARMTargetMachine &TM;
+    std::map<unsigned, intptr_t> CPIDtoAddressMap;
   public:
-    explicit ARMJITInfo(ARMTargetMachine &tm) : TM(tm) {useGOT = 0;}
+    explicit ARMJITInfo(ARMTargetMachine &tm) : TM(tm) { useGOT = false; }
 
     /// replaceMachineCodeForFunction - Make it so that calling the function
     /// whose machine code is at OLD turns into a call to NEW, perhaps by
@@ -45,6 +47,27 @@ namespace llvm {
     /// referenced global symbols.
     virtual void relocate(void *Function, MachineRelocation *MR,
                           unsigned NumRelocs, unsigned char* GOTBase);
+  
+    /// hasCustomConstantPool - Allows a target to specify that constant
+    /// pool address resolution is handled by the target.
+    virtual bool hasCustomConstantPool() const { return true; }
+
+    /// getCustomConstantPoolEntryAddress - The ARM target puts all constant
+    /// pool entries into constant islands. Resolve the constant pool index
+    /// into the address where the constant is stored.
+    virtual intptr_t getCustomConstantPoolEntryAddress(unsigned CPID) const
+      {
+        std::map<unsigned, intptr_t>::const_iterator elem;
+        elem = CPIDtoAddressMap.find(CPID);
+        assert (elem != CPIDtoAddressMap.end());
+        return elem->second;
+      }
+
+    /// mapCPIDtoAddress - Map a Constant Pool Index (CPID) to the address
+    /// where its associated value is stored. When relocations are processed,
+    /// this value will be used to resolve references to the constant.
+    void mapCPIDtoAddress(unsigned CPID, intptr_t address)
+      { CPIDtoAddressMap[CPID] = address; }
   };
 }