LLVM API Documentation

MachineInstr.h

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