LLVM API Documentation

MipsCodeEmitter.cpp
Go to the documentation of this file.
00001 //===-- Mips/MipsCodeEmitter.cpp - Convert Mips Code to Machine Code ------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===---------------------------------------------------------------------===//
00009 //
00010 // This file contains the pass that transforms the Mips machine instructions
00011 // into relocatable machine code.
00012 //
00013 //===---------------------------------------------------------------------===//
00014 
00015 #include "Mips.h"
00016 #include "MCTargetDesc/MipsBaseInfo.h"
00017 #include "MipsInstrInfo.h"
00018 #include "MipsRelocations.h"
00019 #include "MipsSubtarget.h"
00020 #include "MipsTargetMachine.h"
00021 #include "llvm/ADT/Statistic.h"
00022 #include "llvm/CodeGen/JITCodeEmitter.h"
00023 #include "llvm/CodeGen/MachineConstantPool.h"
00024 #include "llvm/CodeGen/MachineFunctionPass.h"
00025 #include "llvm/CodeGen/MachineInstr.h"
00026 #include "llvm/CodeGen/MachineInstrBuilder.h"
00027 #include "llvm/CodeGen/MachineJumpTableInfo.h"
00028 #include "llvm/CodeGen/MachineModuleInfo.h"
00029 #include "llvm/CodeGen/MachineOperand.h"
00030 #include "llvm/CodeGen/Passes.h"
00031 #include "llvm/IR/Constants.h"
00032 #include "llvm/IR/DerivedTypes.h"
00033 #include "llvm/PassManager.h"
00034 #include "llvm/Support/Debug.h"
00035 #include "llvm/Support/ErrorHandling.h"
00036 #include "llvm/Support/raw_ostream.h"
00037 #ifndef NDEBUG
00038 #include <iomanip>
00039 #endif
00040 
00041 using namespace llvm;
00042 
00043 #define DEBUG_TYPE "jit"
00044 
00045 STATISTIC(NumEmitted, "Number of machine instructions emitted");
00046 
00047 namespace {
00048 
00049 class MipsCodeEmitter : public MachineFunctionPass {
00050   MipsJITInfo *JTI;
00051   const MipsInstrInfo *II;
00052   const DataLayout *TD;
00053   const MipsSubtarget *Subtarget;
00054   TargetMachine &TM;
00055   JITCodeEmitter &MCE;
00056   const std::vector<MachineConstantPoolEntry> *MCPEs;
00057   const std::vector<MachineJumpTableEntry> *MJTEs;
00058   bool IsPIC;
00059 
00060   void getAnalysisUsage(AnalysisUsage &AU) const {
00061     AU.addRequired<MachineModuleInfo> ();
00062     MachineFunctionPass::getAnalysisUsage(AU);
00063   }
00064 
00065   static char ID;
00066 
00067 public:
00068   MipsCodeEmitter(TargetMachine &tm, JITCodeEmitter &mce)
00069     : MachineFunctionPass(ID), JTI(0), II(0), TD(0),
00070       TM(tm), MCE(mce), MCPEs(0), MJTEs(0),
00071       IsPIC(TM.getRelocationModel() == Reloc::PIC_) {}
00072 
00073   bool runOnMachineFunction(MachineFunction &MF);
00074 
00075   virtual const char *getPassName() const {
00076     return "Mips Machine Code Emitter";
00077   }
00078 
00079   /// getBinaryCodeForInstr - This function, generated by the
00080   /// CodeEmitterGenerator using TableGen, produces the binary encoding for
00081   /// machine instructions.
00082   uint64_t getBinaryCodeForInstr(const MachineInstr &MI) const;
00083 
00084   void emitInstruction(MachineBasicBlock::instr_iterator MI,
00085                        MachineBasicBlock &MBB);
00086 
00087 private:
00088 
00089   void emitWord(unsigned Word);
00090 
00091   /// Routines that handle operands which add machine relocations which are
00092   /// fixed up by the relocation stage.
00093   void emitGlobalAddress(const GlobalValue *GV, unsigned Reloc,
00094                          bool MayNeedFarStub) const;
00095   void emitExternalSymbolAddress(const char *ES, unsigned Reloc) const;
00096   void emitConstPoolAddress(unsigned CPI, unsigned Reloc) const;
00097   void emitJumpTableAddress(unsigned JTIndex, unsigned Reloc) const;
00098   void emitMachineBasicBlock(MachineBasicBlock *BB, unsigned Reloc) const;
00099 
00100   /// getMachineOpValue - Return binary encoding of operand. If the machine
00101   /// operand requires relocation, record the relocation and return zero.
00102   unsigned getMachineOpValue(const MachineInstr &MI,
00103                              const MachineOperand &MO) const;
00104 
00105   unsigned getRelocation(const MachineInstr &MI,
00106                          const MachineOperand &MO) const;
00107 
00108   unsigned getJumpTargetOpValue(const MachineInstr &MI, unsigned OpNo) const;
00109   unsigned getJumpTargetOpValueMM(const MachineInstr &MI, unsigned OpNo) const;
00110   unsigned getBranchTargetOpValueMM(const MachineInstr &MI,
00111                                     unsigned OpNo) const;
00112 
00113   unsigned getBranchTargetOpValue(const MachineInstr &MI, unsigned OpNo) const;
00114   unsigned getMemEncoding(const MachineInstr &MI, unsigned OpNo) const;
00115   unsigned getMemEncodingMMImm12(const MachineInstr &MI, unsigned OpNo) const;
00116   unsigned getMSAMemEncoding(const MachineInstr &MI, unsigned OpNo) const;
00117   unsigned getSizeExtEncoding(const MachineInstr &MI, unsigned OpNo) const;
00118   unsigned getSizeInsEncoding(const MachineInstr &MI, unsigned OpNo) const;
00119   unsigned getLSAImmEncoding(const MachineInstr &MI, unsigned OpNo) const;
00120 
00121   /// Expand pseudo instructions with accumulator register operands.
00122   void expandACCInstr(MachineBasicBlock::instr_iterator MI,
00123                       MachineBasicBlock &MBB, unsigned Opc) const;
00124 
00125   /// \brief Expand pseudo instruction. Return true if MI was expanded.
00126   bool expandPseudos(MachineBasicBlock::instr_iterator &MI,
00127                      MachineBasicBlock &MBB) const;
00128 };
00129 }
00130 
00131 char MipsCodeEmitter::ID = 0;
00132 
00133 bool MipsCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
00134   MipsTargetMachine &Target = static_cast<MipsTargetMachine &>(
00135                                 const_cast<TargetMachine &>(MF.getTarget()));
00136 
00137   JTI = Target.getJITInfo();
00138   II = Target.getInstrInfo();
00139   TD = Target.getDataLayout();
00140   Subtarget = &TM.getSubtarget<MipsSubtarget> ();
00141   MCPEs = &MF.getConstantPool()->getConstants();
00142   MJTEs = 0;
00143   if (MF.getJumpTableInfo()) MJTEs = &MF.getJumpTableInfo()->getJumpTables();
00144   JTI->Initialize(MF, IsPIC, Subtarget->isLittle());
00145   MCE.setModuleInfo(&getAnalysis<MachineModuleInfo> ());
00146 
00147   do {
00148     DEBUG(errs() << "JITTing function '"
00149         << MF.getName() << "'\n");
00150     MCE.startFunction(MF);
00151 
00152     for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
00153         MBB != E; ++MBB){
00154       MCE.StartMachineBasicBlock(MBB);
00155       for (MachineBasicBlock::instr_iterator I = MBB->instr_begin(),
00156            E = MBB->instr_end(); I != E;)
00157         emitInstruction(*I++, *MBB);
00158     }
00159   } while (MCE.finishFunction(MF));
00160 
00161   return false;
00162 }
00163 
00164 unsigned MipsCodeEmitter::getRelocation(const MachineInstr &MI,
00165                                         const MachineOperand &MO) const {
00166   // NOTE: This relocations are for static.
00167   uint64_t TSFlags = MI.getDesc().TSFlags;
00168   uint64_t Form = TSFlags & MipsII::FormMask;
00169   if (Form == MipsII::FrmJ)
00170     return Mips::reloc_mips_26;
00171   if ((Form == MipsII::FrmI || Form == MipsII::FrmFI)
00172        && MI.isBranch())
00173     return Mips::reloc_mips_pc16;
00174   if (Form == MipsII::FrmI && MI.getOpcode() == Mips::LUi)
00175     return Mips::reloc_mips_hi;
00176   return Mips::reloc_mips_lo;
00177 }
00178 
00179 unsigned MipsCodeEmitter::getJumpTargetOpValue(const MachineInstr &MI,
00180                                                unsigned OpNo) const {
00181   MachineOperand MO = MI.getOperand(OpNo);
00182   if (MO.isGlobal())
00183     emitGlobalAddress(MO.getGlobal(), getRelocation(MI, MO), true);
00184   else if (MO.isSymbol())
00185     emitExternalSymbolAddress(MO.getSymbolName(), getRelocation(MI, MO));
00186   else if (MO.isMBB())
00187     emitMachineBasicBlock(MO.getMBB(), getRelocation(MI, MO));
00188   else
00189     llvm_unreachable("Unexpected jump target operand kind.");
00190   return 0;
00191 }
00192 
00193 unsigned MipsCodeEmitter::getJumpTargetOpValueMM(const MachineInstr &MI,
00194                                                  unsigned OpNo) const {
00195   llvm_unreachable("Unimplemented function.");
00196   return 0;
00197 }
00198 
00199 unsigned MipsCodeEmitter::getBranchTargetOpValueMM(const MachineInstr &MI,
00200                                                    unsigned OpNo) const {
00201   llvm_unreachable("Unimplemented function.");
00202   return 0;
00203 }
00204 
00205 unsigned MipsCodeEmitter::getBranchTargetOpValue(const MachineInstr &MI,
00206                                                  unsigned OpNo) const {
00207   MachineOperand MO = MI.getOperand(OpNo);
00208   emitMachineBasicBlock(MO.getMBB(), getRelocation(MI, MO));
00209   return 0;
00210 }
00211 
00212 unsigned MipsCodeEmitter::getMemEncoding(const MachineInstr &MI,
00213                                          unsigned OpNo) const {
00214   // Base register is encoded in bits 20-16, offset is encoded in bits 15-0.
00215   assert(MI.getOperand(OpNo).isReg());
00216   unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo)) << 16;
00217   return (getMachineOpValue(MI, MI.getOperand(OpNo+1)) & 0xFFFF) | RegBits;
00218 }
00219 
00220 unsigned MipsCodeEmitter::getMemEncodingMMImm12(const MachineInstr &MI,
00221                                                 unsigned OpNo) const {
00222   llvm_unreachable("Unimplemented function.");
00223   return 0;
00224 }
00225 
00226 unsigned MipsCodeEmitter::getMSAMemEncoding(const MachineInstr &MI,
00227                                             unsigned OpNo) const {
00228   llvm_unreachable("Unimplemented function.");
00229   return 0;
00230 }
00231 
00232 unsigned MipsCodeEmitter::getSizeExtEncoding(const MachineInstr &MI,
00233                                              unsigned OpNo) const {
00234   // size is encoded as size-1.
00235   return getMachineOpValue(MI, MI.getOperand(OpNo)) - 1;
00236 }
00237 
00238 unsigned MipsCodeEmitter::getSizeInsEncoding(const MachineInstr &MI,
00239                                              unsigned OpNo) const {
00240   // size is encoded as pos+size-1.
00241   return getMachineOpValue(MI, MI.getOperand(OpNo-1)) +
00242          getMachineOpValue(MI, MI.getOperand(OpNo)) - 1;
00243 }
00244 
00245 unsigned MipsCodeEmitter::getLSAImmEncoding(const MachineInstr &MI,
00246                                             unsigned OpNo) const {
00247   llvm_unreachable("Unimplemented function.");
00248   return 0;
00249 }
00250 
00251 /// getMachineOpValue - Return binary encoding of operand. If the machine
00252 /// operand requires relocation, record the relocation and return zero.
00253 unsigned MipsCodeEmitter::getMachineOpValue(const MachineInstr &MI,
00254                                             const MachineOperand &MO) const {
00255   if (MO.isReg())
00256     return TM.getRegisterInfo()->getEncodingValue(MO.getReg());
00257   else if (MO.isImm())
00258     return static_cast<unsigned>(MO.getImm());
00259   else if (MO.isGlobal())
00260     emitGlobalAddress(MO.getGlobal(), getRelocation(MI, MO), true);
00261   else if (MO.isSymbol())
00262     emitExternalSymbolAddress(MO.getSymbolName(), getRelocation(MI, MO));
00263   else if (MO.isCPI())
00264     emitConstPoolAddress(MO.getIndex(), getRelocation(MI, MO));
00265   else if (MO.isJTI())
00266     emitJumpTableAddress(MO.getIndex(), getRelocation(MI, MO));
00267   else if (MO.isMBB())
00268     emitMachineBasicBlock(MO.getMBB(), getRelocation(MI, MO));
00269   else
00270     llvm_unreachable("Unable to encode MachineOperand!");
00271   return 0;
00272 }
00273 
00274 void MipsCodeEmitter::emitGlobalAddress(const GlobalValue *GV, unsigned Reloc,
00275                                         bool MayNeedFarStub) const {
00276   MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
00277                                              const_cast<GlobalValue *>(GV), 0,
00278                                              MayNeedFarStub));
00279 }
00280 
00281 void MipsCodeEmitter::
00282 emitExternalSymbolAddress(const char *ES, unsigned Reloc) const {
00283   MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
00284                                                  Reloc, ES, 0, 0));
00285 }
00286 
00287 void MipsCodeEmitter::emitConstPoolAddress(unsigned CPI, unsigned Reloc) const {
00288   MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
00289                                                     Reloc, CPI, 0, false));
00290 }
00291 
00292 void MipsCodeEmitter::
00293 emitJumpTableAddress(unsigned JTIndex, unsigned Reloc) const {
00294   MCE.addRelocation(MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
00295                                                     Reloc, JTIndex, 0, false));
00296 }
00297 
00298 void MipsCodeEmitter::emitMachineBasicBlock(MachineBasicBlock *BB,
00299                                             unsigned Reloc) const {
00300   MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
00301                                              Reloc, BB));
00302 }
00303 
00304 void MipsCodeEmitter::emitInstruction(MachineBasicBlock::instr_iterator MI,
00305                                       MachineBasicBlock &MBB) {
00306   DEBUG(errs() << "JIT: " << (void*)MCE.getCurrentPCValue() << ":\t" << *MI);
00307 
00308   // Expand pseudo instruction. Skip if MI was not expanded.
00309   if (((MI->getDesc().TSFlags & MipsII::FormMask) == MipsII::Pseudo) &&
00310       !expandPseudos(MI, MBB))
00311     return;
00312 
00313   MCE.processDebugLoc(MI->getDebugLoc(), true);
00314 
00315   emitWord(getBinaryCodeForInstr(*MI));
00316   ++NumEmitted;  // Keep track of the # of mi's emitted
00317 
00318   MCE.processDebugLoc(MI->getDebugLoc(), false);
00319 }
00320 
00321 void MipsCodeEmitter::emitWord(unsigned Word) {
00322   DEBUG(errs() << "  0x";
00323         errs().write_hex(Word) << "\n");
00324   if (Subtarget->isLittle())
00325     MCE.emitWordLE(Word);
00326   else
00327     MCE.emitWordBE(Word);
00328 }
00329 
00330 void MipsCodeEmitter::expandACCInstr(MachineBasicBlock::instr_iterator MI,
00331                                      MachineBasicBlock &MBB,
00332                                      unsigned Opc) const {
00333   // Expand "pseudomult $ac0, $t0, $t1" to "mult $t0, $t1".
00334   BuildMI(MBB, &*MI, MI->getDebugLoc(), II->get(Opc))
00335     .addReg(MI->getOperand(1).getReg()).addReg(MI->getOperand(2).getReg());
00336 }
00337 
00338 bool MipsCodeEmitter::expandPseudos(MachineBasicBlock::instr_iterator &MI,
00339                                     MachineBasicBlock &MBB) const {
00340   switch (MI->getOpcode()) {
00341   case Mips::NOP:
00342     BuildMI(MBB, &*MI, MI->getDebugLoc(), II->get(Mips::SLL), Mips::ZERO)
00343       .addReg(Mips::ZERO).addImm(0);
00344     break;
00345   case Mips::B:
00346     BuildMI(MBB, &*MI, MI->getDebugLoc(), II->get(Mips::BEQ)).addReg(Mips::ZERO)
00347       .addReg(Mips::ZERO).addOperand(MI->getOperand(0));
00348     break;
00349   case Mips::TRAP:
00350     BuildMI(MBB, &*MI, MI->getDebugLoc(), II->get(Mips::BREAK)).addImm(0)
00351       .addImm(0);
00352     break;
00353   case Mips::JALRPseudo:
00354     BuildMI(MBB, &*MI, MI->getDebugLoc(), II->get(Mips::JALR), Mips::RA)
00355       .addReg(MI->getOperand(0).getReg());
00356     break;
00357   case Mips::PseudoMULT:
00358     expandACCInstr(MI, MBB, Mips::MULT);
00359     break;
00360   case Mips::PseudoMULTu:
00361     expandACCInstr(MI, MBB, Mips::MULTu);
00362     break;
00363   case Mips::PseudoSDIV:
00364     expandACCInstr(MI, MBB, Mips::SDIV);
00365     break;
00366   case Mips::PseudoUDIV:
00367     expandACCInstr(MI, MBB, Mips::UDIV);
00368     break;
00369   case Mips::PseudoMADD:
00370     expandACCInstr(MI, MBB, Mips::MADD);
00371     break;
00372   case Mips::PseudoMADDU:
00373     expandACCInstr(MI, MBB, Mips::MADDU);
00374     break;
00375   case Mips::PseudoMSUB:
00376     expandACCInstr(MI, MBB, Mips::MSUB);
00377     break;
00378   case Mips::PseudoMSUBU:
00379     expandACCInstr(MI, MBB, Mips::MSUBU);
00380     break;
00381   default:
00382     return false;
00383   }
00384 
00385   (MI--)->eraseFromBundle();
00386   return true;
00387 }
00388 
00389 /// createMipsJITCodeEmitterPass - Return a pass that emits the collected Mips
00390 /// code to the specified MCE object.
00391 FunctionPass *llvm::createMipsJITCodeEmitterPass(MipsTargetMachine &TM,
00392                                                  JITCodeEmitter &JCE) {
00393   return new MipsCodeEmitter(TM, JCE);
00394 }
00395 
00396 #include "MipsGenCodeEmitter.inc"