MipsCodeEmitter.cpp revision fee62c167b1f731998ff4d315830154d17ec6f85
1//===-- Mips/MipsCodeEmitter.cpp - Convert Mips Code to Machine Code ------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===---------------------------------------------------------------------===// 9// 10// This file contains the pass that transforms the Mips machine instructions 11// into relocatable machine code. 12// 13//===---------------------------------------------------------------------===// 14 15#define DEBUG_TYPE "jit" 16#include "Mips.h" 17#include "MCTargetDesc/MipsBaseInfo.h" 18#include "MipsInstrInfo.h" 19#include "MipsRelocations.h" 20#include "MipsSubtarget.h" 21#include "MipsTargetMachine.h" 22#include "llvm/ADT/Statistic.h" 23#include "llvm/CodeGen/JITCodeEmitter.h" 24#include "llvm/CodeGen/MachineConstantPool.h" 25#include "llvm/CodeGen/MachineFunctionPass.h" 26#include "llvm/CodeGen/MachineInstr.h" 27#include "llvm/CodeGen/MachineJumpTableInfo.h" 28#include "llvm/CodeGen/MachineInstrBuilder.h" 29#include "llvm/CodeGen/MachineModuleInfo.h" 30#include "llvm/CodeGen/MachineOperand.h" 31#include "llvm/CodeGen/Passes.h" 32#include "llvm/IR/Constants.h" 33#include "llvm/IR/DerivedTypes.h" 34#include "llvm/PassManager.h" 35#include "llvm/Support/Debug.h" 36#include "llvm/Support/ErrorHandling.h" 37#include "llvm/Support/raw_ostream.h" 38#ifndef NDEBUG 39#include <iomanip> 40#endif 41 42using namespace llvm; 43 44STATISTIC(NumEmitted, "Number of machine instructions emitted"); 45 46namespace { 47 48class MipsCodeEmitter : public MachineFunctionPass { 49 MipsJITInfo *JTI; 50 const MipsInstrInfo *II; 51 const DataLayout *TD; 52 const MipsSubtarget *Subtarget; 53 TargetMachine &TM; 54 JITCodeEmitter &MCE; 55 const std::vector<MachineConstantPoolEntry> *MCPEs; 56 const std::vector<MachineJumpTableEntry> *MJTEs; 57 bool IsPIC; 58 59 void getAnalysisUsage(AnalysisUsage &AU) const { 60 AU.addRequired<MachineModuleInfo> (); 61 MachineFunctionPass::getAnalysisUsage(AU); 62 } 63 64 static char ID; 65 66public: 67 MipsCodeEmitter(TargetMachine &tm, JITCodeEmitter &mce) 68 : MachineFunctionPass(ID), JTI(0), 69 II((const MipsInstrInfo *) tm.getInstrInfo()), TD(tm.getDataLayout()), 70 TM(tm), MCE(mce), MCPEs(0), MJTEs(0), 71 IsPIC(TM.getRelocationModel() == Reloc::PIC_) {} 72 73 bool runOnMachineFunction(MachineFunction &MF); 74 75 virtual const char *getPassName() const { 76 return "Mips Machine Code Emitter"; 77 } 78 79 /// getBinaryCodeForInstr - This function, generated by the 80 /// CodeEmitterGenerator using TableGen, produces the binary encoding for 81 /// machine instructions. 82 uint64_t getBinaryCodeForInstr(const MachineInstr &MI) const; 83 84 void emitInstruction(MachineBasicBlock::instr_iterator MI, 85 MachineBasicBlock &MBB); 86 87private: 88 89 void emitWord(unsigned Word); 90 91 /// Routines that handle operands which add machine relocations which are 92 /// fixed up by the relocation stage. 93 void emitGlobalAddress(const GlobalValue *GV, unsigned Reloc, 94 bool MayNeedFarStub) const; 95 void emitExternalSymbolAddress(const char *ES, unsigned Reloc) const; 96 void emitConstPoolAddress(unsigned CPI, unsigned Reloc) const; 97 void emitJumpTableAddress(unsigned JTIndex, unsigned Reloc) const; 98 void emitMachineBasicBlock(MachineBasicBlock *BB, unsigned Reloc) const; 99 100 /// getMachineOpValue - Return binary encoding of operand. If the machine 101 /// operand requires relocation, record the relocation and return zero. 102 unsigned getMachineOpValue(const MachineInstr &MI, 103 const MachineOperand &MO) const; 104 105 unsigned getRelocation(const MachineInstr &MI, 106 const MachineOperand &MO) const; 107 108 unsigned getJumpTargetOpValue(const MachineInstr &MI, unsigned OpNo) const; 109 110 unsigned getBranchTargetOpValue(const MachineInstr &MI, unsigned OpNo) const; 111 unsigned getMemEncoding(const MachineInstr &MI, unsigned OpNo) const; 112 unsigned getSizeExtEncoding(const MachineInstr &MI, unsigned OpNo) const; 113 unsigned getSizeInsEncoding(const MachineInstr &MI, unsigned OpNo) const; 114 115 void emitGlobalAddressUnaligned(const GlobalValue *GV, unsigned Reloc, 116 int Offset) const; 117 118 /// Expand pseudo instructions with accumulator register operands. 119 void expandACCInstr(MachineBasicBlock::instr_iterator MI, 120 MachineBasicBlock &MBB, unsigned Opc) const; 121 122 /// \brief Expand pseudo instruction. Return true if MI was expanded. 123 bool expandPseudos(MachineBasicBlock::instr_iterator &MI, 124 MachineBasicBlock &MBB) const; 125}; 126} 127 128char MipsCodeEmitter::ID = 0; 129 130bool MipsCodeEmitter::runOnMachineFunction(MachineFunction &MF) { 131 MipsTargetMachine &Target = static_cast<MipsTargetMachine &>( 132 const_cast<TargetMachine &>(MF.getTarget())); 133 134 JTI = Target.getJITInfo(); 135 II = Target.getInstrInfo(); 136 TD = Target.getDataLayout(); 137 Subtarget = &TM.getSubtarget<MipsSubtarget> (); 138 MCPEs = &MF.getConstantPool()->getConstants(); 139 MJTEs = 0; 140 if (MF.getJumpTableInfo()) MJTEs = &MF.getJumpTableInfo()->getJumpTables(); 141 JTI->Initialize(MF, IsPIC, Subtarget->isLittle()); 142 MCE.setModuleInfo(&getAnalysis<MachineModuleInfo> ()); 143 144 do { 145 DEBUG(errs() << "JITTing function '" 146 << MF.getName() << "'\n"); 147 MCE.startFunction(MF); 148 149 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end(); 150 MBB != E; ++MBB){ 151 MCE.StartMachineBasicBlock(MBB); 152 for (MachineBasicBlock::instr_iterator I = MBB->instr_begin(), 153 E = MBB->instr_end(); I != E;) 154 emitInstruction(*I++, *MBB); 155 } 156 } while (MCE.finishFunction(MF)); 157 158 return false; 159} 160 161unsigned MipsCodeEmitter::getRelocation(const MachineInstr &MI, 162 const MachineOperand &MO) const { 163 // NOTE: This relocations are for static. 164 uint64_t TSFlags = MI.getDesc().TSFlags; 165 uint64_t Form = TSFlags & MipsII::FormMask; 166 if (Form == MipsII::FrmJ) 167 return Mips::reloc_mips_26; 168 if ((Form == MipsII::FrmI || Form == MipsII::FrmFI) 169 && MI.isBranch()) 170 return Mips::reloc_mips_pc16; 171 if (Form == MipsII::FrmI && MI.getOpcode() == Mips::LUi) 172 return Mips::reloc_mips_hi; 173 return Mips::reloc_mips_lo; 174} 175 176unsigned MipsCodeEmitter::getJumpTargetOpValue(const MachineInstr &MI, 177 unsigned OpNo) const { 178 MachineOperand MO = MI.getOperand(OpNo); 179 if (MO.isGlobal()) 180 emitGlobalAddress(MO.getGlobal(), getRelocation(MI, MO), true); 181 else if (MO.isSymbol()) 182 emitExternalSymbolAddress(MO.getSymbolName(), getRelocation(MI, MO)); 183 else if (MO.isMBB()) 184 emitMachineBasicBlock(MO.getMBB(), getRelocation(MI, MO)); 185 else 186 llvm_unreachable("Unexpected jump target operand kind."); 187 return 0; 188} 189 190unsigned MipsCodeEmitter::getBranchTargetOpValue(const MachineInstr &MI, 191 unsigned OpNo) const { 192 MachineOperand MO = MI.getOperand(OpNo); 193 emitMachineBasicBlock(MO.getMBB(), getRelocation(MI, MO)); 194 return 0; 195} 196 197unsigned MipsCodeEmitter::getMemEncoding(const MachineInstr &MI, 198 unsigned OpNo) const { 199 // Base register is encoded in bits 20-16, offset is encoded in bits 15-0. 200 assert(MI.getOperand(OpNo).isReg()); 201 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo)) << 16; 202 return (getMachineOpValue(MI, MI.getOperand(OpNo+1)) & 0xFFFF) | RegBits; 203} 204 205unsigned MipsCodeEmitter::getSizeExtEncoding(const MachineInstr &MI, 206 unsigned OpNo) const { 207 // size is encoded as size-1. 208 return getMachineOpValue(MI, MI.getOperand(OpNo)) - 1; 209} 210 211unsigned MipsCodeEmitter::getSizeInsEncoding(const MachineInstr &MI, 212 unsigned OpNo) const { 213 // size is encoded as pos+size-1. 214 return getMachineOpValue(MI, MI.getOperand(OpNo-1)) + 215 getMachineOpValue(MI, MI.getOperand(OpNo)) - 1; 216} 217 218/// getMachineOpValue - Return binary encoding of operand. If the machine 219/// operand requires relocation, record the relocation and return zero. 220unsigned MipsCodeEmitter::getMachineOpValue(const MachineInstr &MI, 221 const MachineOperand &MO) const { 222 if (MO.isReg()) 223 return TM.getRegisterInfo()->getEncodingValue(MO.getReg()); 224 else if (MO.isImm()) 225 return static_cast<unsigned>(MO.getImm()); 226 else if (MO.isGlobal()) 227 emitGlobalAddress(MO.getGlobal(), getRelocation(MI, MO), true); 228 else if (MO.isSymbol()) 229 emitExternalSymbolAddress(MO.getSymbolName(), getRelocation(MI, MO)); 230 else if (MO.isCPI()) 231 emitConstPoolAddress(MO.getIndex(), getRelocation(MI, MO)); 232 else if (MO.isJTI()) 233 emitJumpTableAddress(MO.getIndex(), getRelocation(MI, MO)); 234 else if (MO.isMBB()) 235 emitMachineBasicBlock(MO.getMBB(), getRelocation(MI, MO)); 236 else 237 llvm_unreachable("Unable to encode MachineOperand!"); 238 return 0; 239} 240 241void MipsCodeEmitter::emitGlobalAddress(const GlobalValue *GV, unsigned Reloc, 242 bool MayNeedFarStub) const { 243 MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc, 244 const_cast<GlobalValue *>(GV), 0, 245 MayNeedFarStub)); 246} 247 248void MipsCodeEmitter::emitGlobalAddressUnaligned(const GlobalValue *GV, 249 unsigned Reloc, int Offset) const { 250 MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc, 251 const_cast<GlobalValue *>(GV), 0, false)); 252 MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset() + Offset, 253 Reloc, const_cast<GlobalValue *>(GV), 0, false)); 254} 255 256void MipsCodeEmitter:: 257emitExternalSymbolAddress(const char *ES, unsigned Reloc) const { 258 MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(), 259 Reloc, ES, 0, 0)); 260} 261 262void MipsCodeEmitter::emitConstPoolAddress(unsigned CPI, unsigned Reloc) const { 263 MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(), 264 Reloc, CPI, 0, false)); 265} 266 267void MipsCodeEmitter:: 268emitJumpTableAddress(unsigned JTIndex, unsigned Reloc) const { 269 MCE.addRelocation(MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(), 270 Reloc, JTIndex, 0, false)); 271} 272 273void MipsCodeEmitter::emitMachineBasicBlock(MachineBasicBlock *BB, 274 unsigned Reloc) const { 275 MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(), 276 Reloc, BB)); 277} 278 279void MipsCodeEmitter::emitInstruction(MachineBasicBlock::instr_iterator MI, 280 MachineBasicBlock &MBB) { 281 DEBUG(errs() << "JIT: " << (void*)MCE.getCurrentPCValue() << ":\t" << *MI); 282 283 // Expand pseudo instruction. Skip if MI was not expanded. 284 if (((MI->getDesc().TSFlags & MipsII::FormMask) == MipsII::Pseudo) && 285 !expandPseudos(MI, MBB)) 286 return; 287 288 MCE.processDebugLoc(MI->getDebugLoc(), true); 289 290 emitWord(getBinaryCodeForInstr(*MI)); 291 ++NumEmitted; // Keep track of the # of mi's emitted 292 293 MCE.processDebugLoc(MI->getDebugLoc(), false); 294} 295 296void MipsCodeEmitter::emitWord(unsigned Word) { 297 DEBUG(errs() << " 0x"; 298 errs().write_hex(Word) << "\n"); 299 if (Subtarget->isLittle()) 300 MCE.emitWordLE(Word); 301 else 302 MCE.emitWordBE(Word); 303} 304 305void MipsCodeEmitter::expandACCInstr(MachineBasicBlock::instr_iterator MI, 306 MachineBasicBlock &MBB, 307 unsigned Opc) const { 308 // Expand "pseudomult $ac0, $t0, $t1" to "mult $t0, $t1". 309 BuildMI(MBB, &*MI, MI->getDebugLoc(), II->get(Opc)) 310 .addReg(MI->getOperand(1).getReg()).addReg(MI->getOperand(2).getReg()); 311} 312 313bool MipsCodeEmitter::expandPseudos(MachineBasicBlock::instr_iterator &MI, 314 MachineBasicBlock &MBB) const { 315 switch (MI->getOpcode()) { 316 case Mips::NOP: 317 BuildMI(MBB, &*MI, MI->getDebugLoc(), II->get(Mips::SLL), Mips::ZERO) 318 .addReg(Mips::ZERO).addImm(0); 319 break; 320 case Mips::JALRPseudo: 321 BuildMI(MBB, &*MI, MI->getDebugLoc(), II->get(Mips::JALR), Mips::RA) 322 .addReg(MI->getOperand(0).getReg()); 323 break; 324 case Mips::PseudoMULT: 325 expandACCInstr(MI, MBB, Mips::MULT); 326 break; 327 case Mips::PseudoMULTu: 328 expandACCInstr(MI, MBB, Mips::MULTu); 329 break; 330 case Mips::PseudoSDIV: 331 expandACCInstr(MI, MBB, Mips::SDIV); 332 break; 333 case Mips::PseudoUDIV: 334 expandACCInstr(MI, MBB, Mips::UDIV); 335 break; 336 case Mips::PseudoMADD: 337 expandACCInstr(MI, MBB, Mips::MADD); 338 break; 339 case Mips::PseudoMADDU: 340 expandACCInstr(MI, MBB, Mips::MADDU); 341 break; 342 case Mips::PseudoMSUB: 343 expandACCInstr(MI, MBB, Mips::MSUB); 344 break; 345 case Mips::PseudoMSUBU: 346 expandACCInstr(MI, MBB, Mips::MSUBU); 347 break; 348 default: 349 return false; 350 } 351 352 (MI--)->eraseFromBundle(); 353 return true; 354} 355 356/// createMipsJITCodeEmitterPass - Return a pass that emits the collected Mips 357/// code to the specified MCE object. 358FunctionPass *llvm::createMipsJITCodeEmitterPass(MipsTargetMachine &TM, 359 JITCodeEmitter &JCE) { 360 return new MipsCodeEmitter(TM, JCE); 361} 362 363#include "MipsGenCodeEmitter.inc" 364