LLVM API Documentation

MachineInstr.cpp

Go to the documentation of this file.
00001 //===-- MachineInstr.cpp --------------------------------------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // Methods common to all machine instructions.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "llvm/CodeGen/MachineInstr.h"
00015 #include "llvm/CodeGen/MachineFunction.h"
00016 #include "llvm/Target/TargetMachine.h"
00017 #include "llvm/Target/TargetInstrInfo.h"
00018 #include "llvm/Target/MRegisterInfo.h"
00019 #include "llvm/Support/LeakDetector.h"
00020 #include <iostream>
00021 
00022 using namespace llvm;
00023 
00024 // Global variable holding an array of descriptors for machine instructions.
00025 // The actual object needs to be created separately for each target machine.
00026 // This variable is initialized and reset by class TargetInstrInfo.
00027 //
00028 // FIXME: This should be a property of the target so that more than one target
00029 // at a time can be active...
00030 //
00031 namespace llvm {
00032   extern const TargetInstrDescriptor *TargetInstrDescriptors;
00033 }
00034 
00035 /// MachineInstr ctor - This constructor only does a _reserve_ of the operands,
00036 /// not a resize for them.  It is expected that if you use this that you call
00037 /// add* methods below to fill up the operands, instead of the Set methods.
00038 /// Eventually, the "resizing" ctors will be phased out.
00039 ///
00040 MachineInstr::MachineInstr(short opcode, unsigned numOperands)
00041   : Opcode(opcode), parent(0) {
00042   Operands.reserve(numOperands);
00043   // Make sure that we get added to a machine basicblock
00044   LeakDetector::addGarbageObject(this);
00045 }
00046 
00047 /// MachineInstr ctor - Work exactly the same as the ctor above, except that the
00048 /// MachineInstr is created and added to the end of the specified basic block.
00049 ///
00050 MachineInstr::MachineInstr(MachineBasicBlock *MBB, short opcode,
00051                            unsigned numOperands)
00052   : Opcode(opcode), parent(0) {
00053   assert(MBB && "Cannot use inserting ctor with null basic block!");
00054   Operands.reserve(numOperands);
00055   // Make sure that we get added to a machine basicblock
00056   LeakDetector::addGarbageObject(this);
00057   MBB->push_back(this);  // Add instruction to end of basic block!
00058 }
00059 
00060 /// MachineInstr ctor - Copies MachineInstr arg exactly
00061 ///
00062 MachineInstr::MachineInstr(const MachineInstr &MI) {
00063   Opcode = MI.getOpcode();
00064   Operands.reserve(MI.getNumOperands());
00065 
00066   // Add operands
00067   for (unsigned i = 0; i != MI.getNumOperands(); ++i)
00068     Operands.push_back(MI.getOperand(i));
00069 
00070   // Set parent, next, and prev to null
00071   parent = 0;
00072   prev = 0;
00073   next = 0;
00074 }
00075 
00076 
00077 MachineInstr::~MachineInstr() {
00078   LeakDetector::removeGarbageObject(this);
00079 }
00080 
00081 /// removeFromParent - This method unlinks 'this' from the containing basic
00082 /// block, and returns it, but does not delete it.
00083 MachineInstr *MachineInstr::removeFromParent() {
00084   assert(getParent() && "Not embedded in a basic block!");
00085   getParent()->remove(this);
00086   return this;
00087 }
00088 
00089 
00090 /// OperandComplete - Return true if it's illegal to add a new operand
00091 ///
00092 bool MachineInstr::OperandsComplete() const {
00093   int NumOperands = TargetInstrDescriptors[Opcode].numOperands;
00094   if ((TargetInstrDescriptors[Opcode].Flags & M_VARIABLE_OPS) == 0 &&
00095       getNumOperands() >= (unsigned)NumOperands)
00096     return true;  // Broken: we have all the operands of this instruction!
00097   return false;
00098 }
00099 
00100 void MachineInstr::dump() const {
00101   std::cerr << "  " << *this;
00102 }
00103 
00104 static inline void OutputReg(std::ostream &os, unsigned RegNo,
00105                              const MRegisterInfo *MRI = 0) {
00106   if (!RegNo || MRegisterInfo::isPhysicalRegister(RegNo)) {
00107     if (MRI)
00108       os << "%" << MRI->get(RegNo).Name;
00109     else
00110       os << "%mreg(" << RegNo << ")";
00111   } else
00112     os << "%reg" << RegNo;
00113 }
00114 
00115 static void print(const MachineOperand &MO, std::ostream &OS,
00116                   const TargetMachine *TM) {
00117   const MRegisterInfo *MRI = 0;
00118 
00119   if (TM) MRI = TM->getRegisterInfo();
00120 
00121   switch (MO.getType()) {
00122   case MachineOperand::MO_Register:
00123     OutputReg(OS, MO.getReg(), MRI);
00124     break;
00125   case MachineOperand::MO_Immediate:
00126     OS << MO.getImmedValue();
00127     break;
00128   case MachineOperand::MO_MachineBasicBlock:
00129     OS << "mbb<"
00130        << ((Value*)MO.getMachineBasicBlock()->getBasicBlock())->getName()
00131        << "," << (void*)MO.getMachineBasicBlock() << ">";
00132     break;
00133   case MachineOperand::MO_FrameIndex:
00134     OS << "<fi#" << MO.getFrameIndex() << ">";
00135     break;
00136   case MachineOperand::MO_ConstantPoolIndex:
00137     OS << "<cp#" << MO.getConstantPoolIndex() << ">";
00138     break;
00139   case MachineOperand::MO_JumpTableIndex:
00140     OS << "<jt#" << MO.getJumpTableIndex() << ">";
00141     break;
00142   case MachineOperand::MO_GlobalAddress:
00143     OS << "<ga:" << ((Value*)MO.getGlobal())->getName();
00144     if (MO.getOffset()) OS << "+" << MO.getOffset();
00145     OS << ">";
00146     break;
00147   case MachineOperand::MO_ExternalSymbol:
00148     OS << "<es:" << MO.getSymbolName();
00149     if (MO.getOffset()) OS << "+" << MO.getOffset();
00150     OS << ">";
00151     break;
00152   default:
00153     assert(0 && "Unrecognized operand type");
00154   }
00155 }
00156 
00157 void MachineInstr::print(std::ostream &OS, const TargetMachine *TM) const {
00158   unsigned StartOp = 0;
00159 
00160    // Specialize printing if op#0 is definition
00161   if (getNumOperands() && getOperand(0).isDef() && !getOperand(0).isUse()) {
00162     ::print(getOperand(0), OS, TM);
00163     OS << " = ";
00164     ++StartOp;   // Don't print this operand again!
00165   }
00166 
00167   // Must check if Target machine is not null because machine BB could not
00168   // be attached to a Machine function yet
00169   if (TM)
00170     OS << TM->getInstrInfo()->getName(getOpcode());
00171 
00172   for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) {
00173     const MachineOperand& mop = getOperand(i);
00174     if (i != StartOp)
00175       OS << ",";
00176     OS << " ";
00177     ::print(mop, OS, TM);
00178 
00179     if (mop.isDef())
00180       if (mop.isUse())
00181         OS << "<def&use>";
00182       else
00183         OS << "<def>";
00184   }
00185 
00186   OS << "\n";
00187 }
00188 
00189 std::ostream &llvm::operator<<(std::ostream &os, const MachineInstr &MI) {
00190   // If the instruction is embedded into a basic block, we can find the target
00191   // info for the instruction.
00192   if (const MachineBasicBlock *MBB = MI.getParent()) {
00193     const MachineFunction *MF = MBB->getParent();
00194     if (MF)
00195       MI.print(os, &MF->getTarget());
00196     else
00197       MI.print(os, 0);
00198     return os;
00199   }
00200 
00201   // Otherwise, print it out in the "raw" format without symbolic register names
00202   // and such.
00203   os << TargetInstrDescriptors[MI.getOpcode()].Name;
00204 
00205   for (unsigned i = 0, N = MI.getNumOperands(); i < N; i++) {
00206     os << "\t" << MI.getOperand(i);
00207     if (MI.getOperand(i).isDef())
00208       if (MI.getOperand(i).isUse())
00209         os << "<d&u>";
00210       else
00211         os << "<d>";
00212   }
00213 
00214   return os << "\n";
00215 }
00216 
00217 std::ostream &llvm::operator<<(std::ostream &OS, const MachineOperand &MO) {
00218   switch (MO.getType()) {
00219   case MachineOperand::MO_Register:
00220     OutputReg(OS, MO.getReg());
00221     break;
00222   case MachineOperand::MO_Immediate:
00223     OS << (long)MO.getImmedValue();
00224     break;
00225   case MachineOperand::MO_MachineBasicBlock:
00226     OS << "<mbb:"
00227        << ((Value*)MO.getMachineBasicBlock()->getBasicBlock())->getName()
00228        << "@" << (void*)MO.getMachineBasicBlock() << ">";
00229     break;
00230   case MachineOperand::MO_FrameIndex:
00231     OS << "<fi#" << MO.getFrameIndex() << ">";
00232     break;
00233   case MachineOperand::MO_ConstantPoolIndex:
00234     OS << "<cp#" << MO.getConstantPoolIndex() << ">";
00235     break;
00236   case MachineOperand::MO_JumpTableIndex:
00237     OS << "<jt#" << MO.getJumpTableIndex() << ">";
00238     break;
00239   case MachineOperand::MO_GlobalAddress:
00240     OS << "<ga:" << ((Value*)MO.getGlobal())->getName() << ">";
00241     break;
00242   case MachineOperand::MO_ExternalSymbol:
00243     OS << "<es:" << MO.getSymbolName() << ">";
00244     break;
00245   default:
00246     assert(0 && "Unrecognized operand type");
00247     break;
00248   }
00249 
00250   return OS;
00251 }