LLVM API Documentation
00001 //===-- llvm/CodeGen/MachineInstr.h - MachineInstr class --------*- C++ -*-===// 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 declaration of the MachineInstr class, which is the 00011 // basic representation for all target dependent machine instructions used by 00012 // the back end. 00013 // 00014 //===----------------------------------------------------------------------===// 00015 00016 #ifndef LLVM_CODEGEN_MACHINEINSTR_H 00017 #define LLVM_CODEGEN_MACHINEINSTR_H 00018 00019 #include "llvm/ADT/ilist.h" 00020 #include "llvm/ADT/ilist_node.h" 00021 #include "llvm/ADT/STLExtras.h" 00022 #include "llvm/CodeGen/MachineOperand.h" 00023 #include "llvm/CodeGen/MachineMemOperand.h" 00024 #include "llvm/Target/TargetInstrDesc.h" 00025 #include "llvm/Support/DebugLoc.h" 00026 #include <list> 00027 #include <vector> 00028 00029 namespace llvm { 00030 00031 class TargetInstrDesc; 00032 class TargetInstrInfo; 00033 class TargetRegisterInfo; 00034 class MachineFunction; 00035 00036 //===----------------------------------------------------------------------===// 00037 /// MachineInstr - Representation of each machine instruction. 00038 /// 00039 class MachineInstr : public ilist_node<MachineInstr> { 00040 const TargetInstrDesc *TID; // Instruction descriptor. 00041 unsigned short NumImplicitOps; // Number of implicit operands (which 00042 // are determined at construction time). 00043 00044 std::vector<MachineOperand> Operands; // the operands 00045 std::list<MachineMemOperand> MemOperands; // information on memory references 00046 MachineBasicBlock *Parent; // Pointer to the owning basic block. 00047 DebugLoc debugLoc; // Source line information. 00048 00049 // OperandComplete - Return true if it's illegal to add a new operand 00050 bool OperandsComplete() const; 00051 00052 MachineInstr(const MachineInstr&); // DO NOT IMPLEMENT 00053 void operator=(const MachineInstr&); // DO NOT IMPLEMENT 00054 00055 // Intrusive list support 00056 friend struct ilist_traits<MachineInstr>; 00057 friend struct ilist_traits<MachineBasicBlock>; 00058 void setParent(MachineBasicBlock *P) { Parent = P; } 00059 00060 /// MachineInstr ctor - This constructor creates a copy of the given 00061 /// MachineInstr in the given MachineFunction. 00062 MachineInstr(MachineFunction &, const MachineInstr &); 00063 00064 /// MachineInstr ctor - This constructor creates a dummy MachineInstr with 00065 /// TID NULL and no operands. 00066 MachineInstr(); 00067 00068 // The next two constructors have DebugLoc and non-DebugLoc versions; 00069 // over time, the non-DebugLoc versions should be phased out and eventually 00070 // removed. 00071 00072 /// MachineInstr ctor - This constructor create a MachineInstr and add the 00073 /// implicit operands. It reserves space for number of operands specified by 00074 /// TargetInstrDesc. The version with a DebugLoc should be preferred. 00075 explicit MachineInstr(const TargetInstrDesc &TID, bool NoImp = false); 00076 00077 /// MachineInstr ctor - Work exactly the same as the ctor above, except that 00078 /// the MachineInstr is created and added to the end of the specified basic 00079 /// block. The version with a DebugLoc should be preferred. 00080 /// 00081 MachineInstr(MachineBasicBlock *MBB, const TargetInstrDesc &TID); 00082 00083 /// MachineInstr ctor - This constructor create a MachineInstr and add the 00084 /// implicit operands. It reserves space for number of operands specified by 00085 /// TargetInstrDesc. An explicit DebugLoc is supplied. 00086 explicit MachineInstr(const TargetInstrDesc &TID, const DebugLoc dl, 00087 bool NoImp = false); 00088 00089 /// MachineInstr ctor - Work exactly the same as the ctor above, except that 00090 /// the MachineInstr is created and added to the end of the specified basic 00091 /// block. 00092 /// 00093 MachineInstr(MachineBasicBlock *MBB, const DebugLoc dl, 00094 const TargetInstrDesc &TID); 00095 00096 ~MachineInstr(); 00097 00098 // MachineInstrs are pool-allocated and owned by MachineFunction. 00099 friend class MachineFunction; 00100 00101 public: 00102 const MachineBasicBlock* getParent() const { return Parent; } 00103 MachineBasicBlock* getParent() { return Parent; } 00104 00105 /// getDebugLoc - Returns the debug location id of this MachineInstr. 00106 /// 00107 DebugLoc getDebugLoc() const { return debugLoc; } 00108 00109 /// getDesc - Returns the target instruction descriptor of this 00110 /// MachineInstr. 00111 const TargetInstrDesc &getDesc() const { return *TID; } 00112 00113 /// getOpcode - Returns the opcode of this MachineInstr. 00114 /// 00115 int getOpcode() const { return TID->Opcode; } 00116 00117 /// Access to explicit operands of the instruction. 00118 /// 00119 unsigned getNumOperands() const { return (unsigned)Operands.size(); } 00120 00121 const MachineOperand& getOperand(unsigned i) const { 00122 assert(i < getNumOperands() && "getOperand() out of range!"); 00123 return Operands[i]; 00124 } 00125 MachineOperand& getOperand(unsigned i) { 00126 assert(i < getNumOperands() && "getOperand() out of range!"); 00127 return Operands[i]; 00128 } 00129 00130 /// getNumExplicitOperands - Returns the number of non-implicit operands. 00131 /// 00132 unsigned getNumExplicitOperands() const; 00133 00134 /// Access to memory operands of the instruction 00135 std::list<MachineMemOperand>::iterator memoperands_begin() 00136 { return MemOperands.begin(); } 00137 std::list<MachineMemOperand>::iterator memoperands_end() 00138 { return MemOperands.end(); } 00139 std::list<MachineMemOperand>::const_iterator memoperands_begin() const 00140 { return MemOperands.begin(); } 00141 std::list<MachineMemOperand>::const_iterator memoperands_end() const 00142 { return MemOperands.end(); } 00143 bool memoperands_empty() const { return MemOperands.empty(); } 00144 00145 /// hasOneMemOperand - Return true if this instruction has exactly one 00146 /// MachineMemOperand. 00147 bool hasOneMemOperand() const { 00148 return !memoperands_empty() && 00149 next(memoperands_begin()) == memoperands_end(); 00150 } 00151 00152 /// isIdenticalTo - Return true if this instruction is identical to (same 00153 /// opcode and same operands as) the specified instruction. 00154 bool isIdenticalTo(const MachineInstr *Other) const { 00155 if (Other->getOpcode() != getOpcode() || 00156 Other->getNumOperands() != getNumOperands()) 00157 return false; 00158 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) 00159 if (!getOperand(i).isIdenticalTo(Other->getOperand(i))) 00160 return false; 00161 return true; 00162 } 00163 00164 /// removeFromParent - This method unlinks 'this' from the containing basic 00165 /// block, and returns it, but does not delete it. 00166 MachineInstr *removeFromParent(); 00167 00168 /// eraseFromParent - This method unlinks 'this' from the containing basic 00169 /// block and deletes it. 00170 void eraseFromParent(); 00171 00172 /// isLabel - Returns true if the MachineInstr represents a label. 00173 /// 00174 bool isLabel() const; 00175 00176 /// isDebugLabel - Returns true if the MachineInstr represents a debug label. 00177 /// 00178 bool isDebugLabel() const; 00179 00180 /// readsRegister - Return true if the MachineInstr reads the specified 00181 /// register. If TargetRegisterInfo is passed, then it also checks if there 00182 /// is a read of a super-register. 00183 bool readsRegister(unsigned Reg, const TargetRegisterInfo *TRI = NULL) const { 00184 return findRegisterUseOperandIdx(Reg, false, TRI) != -1; 00185 } 00186 00187 /// killsRegister - Return true if the MachineInstr kills the specified 00188 /// register. If TargetRegisterInfo is passed, then it also checks if there is 00189 /// a kill of a super-register. 00190 bool killsRegister(unsigned Reg, const TargetRegisterInfo *TRI = NULL) const { 00191 return findRegisterUseOperandIdx(Reg, true, TRI) != -1; 00192 } 00193 00194 /// modifiesRegister - Return true if the MachineInstr modifies the 00195 /// specified register. If TargetRegisterInfo is passed, then it also checks 00196 /// if there is a def of a super-register. 00197 bool modifiesRegister(unsigned Reg, 00198 const TargetRegisterInfo *TRI = NULL) const { 00199 return findRegisterDefOperandIdx(Reg, false, TRI) != -1; 00200 } 00201 00202 /// registerDefIsDead - Returns true if the register is dead in this machine 00203 /// instruction. If TargetRegisterInfo is passed, then it also checks 00204 /// if there is a dead def of a super-register. 00205 bool registerDefIsDead(unsigned Reg, 00206 const TargetRegisterInfo *TRI = NULL) const { 00207 return findRegisterDefOperandIdx(Reg, true, TRI) != -1; 00208 } 00209 00210 /// findRegisterUseOperandIdx() - Returns the operand index that is a use of 00211 /// the specific register or -1 if it is not found. It further tightening 00212 /// the search criteria to a use that kills the register if isKill is true. 00213 int findRegisterUseOperandIdx(unsigned Reg, bool isKill = false, 00214 const TargetRegisterInfo *TRI = NULL) const; 00215 00216 /// findRegisterUseOperand - Wrapper for findRegisterUseOperandIdx, it returns 00217 /// a pointer to the MachineOperand rather than an index. 00218 MachineOperand *findRegisterUseOperand(unsigned Reg, bool isKill = false, 00219 const TargetRegisterInfo *TRI = NULL) { 00220 int Idx = findRegisterUseOperandIdx(Reg, isKill, TRI); 00221 return (Idx == -1) ? NULL : &getOperand(Idx); 00222 } 00223 00224 /// findRegisterDefOperandIdx() - Returns the operand index that is a def of 00225 /// the specified register or -1 if it is not found. If isDead is true, defs 00226 /// that are not dead are skipped. If TargetRegisterInfo is non-null, then it 00227 /// also checks if there is a def of a super-register. 00228 int findRegisterDefOperandIdx(unsigned Reg, bool isDead = false, 00229 const TargetRegisterInfo *TRI = NULL) const; 00230 00231 /// findRegisterDefOperand - Wrapper for findRegisterDefOperandIdx, it returns 00232 /// a pointer to the MachineOperand rather than an index. 00233 MachineOperand *findRegisterDefOperand(unsigned Reg, bool isDead = false, 00234 const TargetRegisterInfo *TRI = NULL) { 00235 int Idx = findRegisterDefOperandIdx(Reg, isDead, TRI); 00236 return (Idx == -1) ? NULL : &getOperand(Idx); 00237 } 00238 00239 /// findFirstPredOperandIdx() - Find the index of the first operand in the 00240 /// operand list that is used to represent the predicate. It returns -1 if 00241 /// none is found. 00242 int findFirstPredOperandIdx() const; 00243 00244 /// isRegTiedToUseOperand - Given the index of a register def operand, 00245 /// check if the register def is tied to a source operand, due to either 00246 /// two-address elimination or inline assembly constraints. Returns the 00247 /// first tied use operand index by reference is UseOpIdx is not null. 00248 bool isRegTiedToUseOperand(unsigned DefOpIdx, unsigned *UseOpIdx = 0) const; 00249 00250 /// isRegTiedToDefOperand - Return true if the use operand of the specified 00251 /// index is tied to an def operand. It also returns the def operand index by 00252 /// reference if DefOpIdx is not null. 00253 bool isRegTiedToDefOperand(unsigned UseOpIdx, unsigned *DefOpIdx = 0) const; 00254 00255 /// copyKillDeadInfo - Copies kill / dead operand properties from MI. 00256 /// 00257 void copyKillDeadInfo(const MachineInstr *MI); 00258 00259 /// copyPredicates - Copies predicate operand(s) from MI. 00260 void copyPredicates(const MachineInstr *MI); 00261 00262 /// addRegisterKilled - We have determined MI kills a register. Look for the 00263 /// operand that uses it and mark it as IsKill. If AddIfNotFound is true, 00264 /// add a implicit operand if it's not found. Returns true if the operand 00265 /// exists / is added. 00266 bool addRegisterKilled(unsigned IncomingReg, 00267 const TargetRegisterInfo *RegInfo, 00268 bool AddIfNotFound = false); 00269 00270 /// addRegisterDead - We have determined MI defined a register without a use. 00271 /// Look for the operand that defines it and mark it as IsDead. If 00272 /// AddIfNotFound is true, add a implicit operand if it's not found. Returns 00273 /// true if the operand exists / is added. 00274 bool addRegisterDead(unsigned IncomingReg, const TargetRegisterInfo *RegInfo, 00275 bool AddIfNotFound = false); 00276 00277 /// isSafeToMove - Return true if it is safe to move this instruction. If 00278 /// SawStore is set to true, it means that there is a store (or call) between 00279 /// the instruction's location and its intended destination. 00280 bool isSafeToMove(const TargetInstrInfo *TII, bool &SawStore) const; 00281 00282 /// isSafeToReMat - Return true if it's safe to rematerialize the specified 00283 /// instruction which defined the specified register instead of copying it. 00284 bool isSafeToReMat(const TargetInstrInfo *TII, unsigned DstReg) const; 00285 00286 /// hasVolatileMemoryRef - Return true if this instruction may have a 00287 /// volatile memory reference, or if the information describing the 00288 /// memory reference is not available. Return false if it is known to 00289 /// have no volatile memory references. 00290 bool hasVolatileMemoryRef() const; 00291 00292 // 00293 // Debugging support 00294 // 00295 void print(std::ostream *OS, const TargetMachine *TM) const { 00296 if (OS) print(*OS, TM); 00297 } 00298 void print(std::ostream &OS, const TargetMachine *TM = 0) const; 00299 void print(std::ostream *OS) const { if (OS) print(*OS); } 00300 void print(raw_ostream *OS, const TargetMachine *TM) const { 00301 if (OS) print(*OS, TM); 00302 } 00303 void print(raw_ostream &OS, const TargetMachine *TM = 0) const; 00304 void print(raw_ostream *OS) const { if (OS) print(*OS); } 00305 void dump() const; 00306 00307 //===--------------------------------------------------------------------===// 00308 // Accessors used to build up machine instructions. 00309 00310 /// addOperand - Add the specified operand to the instruction. If it is an 00311 /// implicit operand, it is added to the end of the operand list. If it is 00312 /// an explicit operand it is added at the end of the explicit operand list 00313 /// (before the first implicit operand). 00314 void addOperand(const MachineOperand &Op); 00315 00316 /// setDesc - Replace the instruction descriptor (thus opcode) of 00317 /// the current instruction with a new one. 00318 /// 00319 void setDesc(const TargetInstrDesc &tid) { TID = &tid; } 00320 00321 /// setDebugLoc - Replace current source information with new such. 00322 /// Avoid using this, the constructor argument is preferable. 00323 /// 00324 void setDebugLoc(const DebugLoc dl) { debugLoc = dl; } 00325 00326 /// RemoveOperand - Erase an operand from an instruction, leaving it with one 00327 /// fewer operand than it started with. 00328 /// 00329 void RemoveOperand(unsigned i); 00330 00331 /// addMemOperand - Add a MachineMemOperand to the machine instruction, 00332 /// referencing arbitrary storage. 00333 void addMemOperand(MachineFunction &MF, 00334 const MachineMemOperand &MO); 00335 00336 /// clearMemOperands - Erase all of this MachineInstr's MachineMemOperands. 00337 void clearMemOperands(MachineFunction &MF); 00338 00339 private: 00340 /// getRegInfo - If this instruction is embedded into a MachineFunction, 00341 /// return the MachineRegisterInfo object for the current function, otherwise 00342 /// return null. 00343 MachineRegisterInfo *getRegInfo(); 00344 00345 /// addImplicitDefUseOperands - Add all implicit def and use operands to 00346 /// this instruction. 00347 void addImplicitDefUseOperands(); 00348 00349 /// RemoveRegOperandsFromUseLists - Unlink all of the register operands in 00350 /// this instruction from their respective use lists. This requires that the 00351 /// operands already be on their use lists. 00352 void RemoveRegOperandsFromUseLists(); 00353 00354 /// AddRegOperandsToUseLists - Add all of the register operands in 00355 /// this instruction from their respective use lists. This requires that the 00356 /// operands not be on their use lists yet. 00357 void AddRegOperandsToUseLists(MachineRegisterInfo &RegInfo); 00358 }; 00359 00360 //===----------------------------------------------------------------------===// 00361 // Debugging Support 00362 00363 inline std::ostream& operator<<(std::ostream &OS, const MachineInstr &MI) { 00364 MI.print(OS); 00365 return OS; 00366 } 00367 00368 inline raw_ostream& operator<<(raw_ostream &OS, const MachineInstr &MI) { 00369 MI.print(OS); 00370 return OS; 00371 } 00372 00373 } // End llvm namespace 00374 00375 #endif