- Clean up formal argument lowering code. Prepare for vector pass by value work.
[oota-llvm.git] / lib / Target / X86 / X86CodeEmitter.cpp
index 0c0ed538ab2023bca8c13389e7ca06a33e7e0707..bac310cd6b5b932225329f9282260ee968027c77 100644 (file)
@@ -35,8 +35,8 @@ namespace {
   class Emitter : public MachineFunctionPass {
     const X86InstrInfo  *II;
     MachineCodeEmitter  &MCE;
-    std::map<const MachineBasicBlock*, unsigned> BasicBlockAddrs;
-    std::vector<std::pair<const MachineBasicBlock *, unsigned> > BBRefs;
+    std::map<MachineBasicBlock*, uint64_t> BasicBlockAddrs;
+    std::vector<std::pair<MachineBasicBlock *, unsigned> > BBRefs;
   public:
     explicit Emitter(MachineCodeEmitter &mce) : II(0), MCE(mce) {}
     Emitter(MachineCodeEmitter &mce, const X86InstrInfo& ii)
@@ -51,9 +51,8 @@ namespace {
     void emitInstruction(const MachineInstr &MI);
 
   private:
-    void emitBasicBlock(const MachineBasicBlock &MBB);
-
-    void emitPCRelativeBlockAddress(const MachineBasicBlock *BB);
+    void emitBasicBlock(MachineBasicBlock &MBB);
+    void emitPCRelativeBlockAddress(MachineBasicBlock *MBB);
     void emitPCRelativeValue(unsigned Address);
     void emitGlobalAddressForCall(GlobalValue *GV, bool isTailCall);
     void emitGlobalAddressForPtr(GlobalValue *GV, int Disp = 0);
@@ -77,12 +76,17 @@ FunctionPass *llvm::createX86CodeEmitterPass(MachineCodeEmitter &MCE) {
 }
 
 bool Emitter::runOnMachineFunction(MachineFunction &MF) {
+  assert((MF.getTarget().getRelocationModel() != Reloc::Default ||
+          MF.getTarget().getRelocationModel() != Reloc::Static) &&
+         "JIT relocation model must be set to static or default!");
   II = ((X86TargetMachine&)MF.getTarget()).getInstrInfo();
 
   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);
   MCE.finishFunction(MF);
 
   // Resolve all forward branches now...
@@ -96,8 +100,7 @@ bool Emitter::runOnMachineFunction(MachineFunction &MF) {
   return false;
 }
 
-void Emitter::emitBasicBlock(const MachineBasicBlock &MBB) {
-  assert(!PICEnabled && "CodeEmitter does not support PIC!");
+void Emitter::emitBasicBlock(MachineBasicBlock &MBB) {
   if (uint64_t Addr = MCE.getCurrentPCValue())
     BasicBlockAddrs[&MBB] = Addr;
 
@@ -117,11 +120,10 @@ void Emitter::emitPCRelativeValue(unsigned Address) {
 /// (because this is a forward branch), it keeps track of the information
 /// necessary to resolve this address later (and emits a dummy value).
 ///
-void Emitter::emitPCRelativeBlockAddress(const MachineBasicBlock *MBB) {
+void Emitter::emitPCRelativeBlockAddress(MachineBasicBlock *MBB) {
   // If this is a backwards branch, we already know the address of the target,
   // so just emit the value.
-  std::map<const MachineBasicBlock*, unsigned>::iterator I =
-    BasicBlockAddrs.find(MBB);
+  std::map<MachineBasicBlock*,uint64_t>::iterator I = BasicBlockAddrs.find(MBB);
   if (I != BasicBlockAddrs.end()) {
     emitPCRelativeValue(I->second);
   } else {
@@ -237,6 +239,11 @@ void Emitter::emitMemModRMByte(const MachineInstr &MI,
   if (Op3.isGlobalAddress()) {
     GV = Op3.getGlobal();
     DispVal = Op3.getOffset();
+  } else if (Op3.isConstantPoolIndex()) {
+    DispVal += MCE.getConstantPoolEntryAddress(Op3.getConstantPoolIndex());
+    DispVal += Op3.getOffset();
+  } else if (Op3.isJumpTableIndex()) {
+    DispVal += MCE.getJumpTableEntryAddress(Op3.getJumpTableIndex());
   } else {
     DispVal = Op3.getImmedValue();
   }
@@ -245,16 +252,7 @@ void Emitter::emitMemModRMByte(const MachineInstr &MI,
   const MachineOperand &Scale    = MI.getOperand(Op+1);
   const MachineOperand &IndexReg = MI.getOperand(Op+2);
 
-  unsigned BaseReg = 0;
-
-  if (Base.isConstantPoolIndex()) {
-    // Emit a direct address reference [disp32] where the displacement of the
-    // constant pool entry is controlled by the MCE.
-    assert(!GV && "Constant Pool reference cannot be relative to global!");
-    DispVal += MCE.getConstantPoolEntryAddress(Base.getConstantPoolIndex());
-  } else {
-    BaseReg = Base.getReg();
-  }
+  unsigned BaseReg = Base.getReg();
 
   // Is a SIB byte needed?
   if (IndexReg.getReg() == 0 && BaseReg != X86::ESP) {
@@ -402,6 +400,8 @@ void Emitter::emitInstruction(const MachineInstr &MI) {
     case X86::IMPLICIT_DEF_R32:
     case X86::IMPLICIT_DEF_FR32:
     case X86::IMPLICIT_DEF_FR64:
+    case X86::IMPLICIT_DEF_VR64:
+    case X86::IMPLICIT_DEF_VR128:
     case X86::FP_REG_KILL:
       break;
     }
@@ -447,6 +447,10 @@ void Emitter::emitInstruction(const MachineInstr &MI) {
         assert(sizeOfImm(Desc) == 4 &&
                "Don't know how to emit non-pointer values!");
         emitExternalSymbolAddress(MO1.getSymbolName(), false, false);
+      } else if (MO1.isJumpTableIndex()) {
+        assert(sizeOfImm(Desc) == 4 &&
+               "Don't know how to emit non-pointer values!");
+        emitConstant(MCE.getJumpTableEntryAddress(MO1.getJumpTableIndex()), 4);
       } else {
         emitConstant(MO1.getImmedValue(), sizeOfImm(Desc));
       }