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