LLVM API Documentation

TargetInstrInfo.h

Go to the documentation of this file.
00001 //===-- llvm/Target/TargetInstrInfo.h - Instruction Info --------*- 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 describes the target machine instruction set to the code generator.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_TARGET_TARGETINSTRINFO_H
00015 #define LLVM_TARGET_TARGETINSTRINFO_H
00016 
00017 #include "llvm/Target/TargetInstrDesc.h"
00018 #include "llvm/CodeGen/MachineFunction.h"
00019 
00020 namespace llvm {
00021 
00022 class TargetRegisterClass;
00023 class LiveVariables;
00024 class CalleeSavedInfo;
00025 class SDNode;
00026 class SelectionDAG;
00027 
00028 template<class T> class SmallVectorImpl;
00029 
00030 
00031 //---------------------------------------------------------------------------
00032 ///
00033 /// TargetInstrInfo - Interface to description of machine instruction set
00034 ///
00035 class TargetInstrInfo {
00036   const TargetInstrDesc *Descriptors; // Raw array to allow static init'n
00037   unsigned NumOpcodes;                // Number of entries in the desc array
00038 
00039   TargetInstrInfo(const TargetInstrInfo &);  // DO NOT IMPLEMENT
00040   void operator=(const TargetInstrInfo &);   // DO NOT IMPLEMENT
00041 public:
00042   TargetInstrInfo(const TargetInstrDesc *desc, unsigned NumOpcodes);
00043   virtual ~TargetInstrInfo();
00044 
00045   // Invariant opcodes: All instruction sets have these as their low opcodes.
00046   enum { 
00047     PHI = 0,
00048     INLINEASM = 1,
00049     DBG_LABEL = 2,
00050     EH_LABEL = 3,
00051     GC_LABEL = 4,
00052     DECLARE = 5,
00053     EXTRACT_SUBREG = 6,
00054     INSERT_SUBREG = 7,
00055     IMPLICIT_DEF = 8,
00056     SUBREG_TO_REG = 9
00057   };
00058 
00059   unsigned getNumOpcodes() const { return NumOpcodes; }
00060 
00061   /// get - Return the machine instruction descriptor that corresponds to the
00062   /// specified instruction opcode.
00063   ///
00064   const TargetInstrDesc &get(unsigned Opcode) const {
00065     assert(Opcode < NumOpcodes && "Invalid opcode!");
00066     return Descriptors[Opcode];
00067   }
00068 
00069   /// isTriviallyReMaterializable - Return true if the instruction is trivially
00070   /// rematerializable, meaning it has no side effects and requires no operands
00071   /// that aren't always available.
00072   bool isTriviallyReMaterializable(const MachineInstr *MI) const {
00073     return MI->getDesc().isRematerializable() &&
00074            isReallyTriviallyReMaterializable(MI);
00075   }
00076 
00077 protected:
00078   /// isReallyTriviallyReMaterializable - For instructions with opcodes for
00079   /// which the M_REMATERIALIZABLE flag is set, this function tests whether the
00080   /// instruction itself is actually trivially rematerializable, considering
00081   /// its operands.  This is used for targets that have instructions that are
00082   /// only trivially rematerializable for specific uses.  This predicate must
00083   /// return false if the instruction has any side effects other than
00084   /// producing a value, or if it requres any address registers that are not
00085   /// always available.
00086   virtual bool isReallyTriviallyReMaterializable(const MachineInstr *MI) const {
00087     return true;
00088   }
00089 
00090 public:
00091   /// Return true if the instruction is a register to register move and return
00092   /// the source and dest operands and their sub-register indices by reference.
00093   virtual bool isMoveInstr(const MachineInstr& MI,
00094                            unsigned& SrcReg, unsigned& DstReg,
00095                            unsigned& SrcSubIdx, unsigned& DstSubIdx) const {
00096     return false;
00097   }
00098   
00099   /// isLoadFromStackSlot - If the specified machine instruction is a direct
00100   /// load from a stack slot, return the virtual or physical register number of
00101   /// the destination along with the FrameIndex of the loaded stack slot.  If
00102   /// not, return 0.  This predicate must return 0 if the instruction has
00103   /// any side effects other than loading from the stack slot.
00104   virtual unsigned isLoadFromStackSlot(const MachineInstr *MI,
00105                                        int &FrameIndex) const {
00106     return 0;
00107   }
00108   
00109   /// isStoreToStackSlot - If the specified machine instruction is a direct
00110   /// store to a stack slot, return the virtual or physical register number of
00111   /// the source reg along with the FrameIndex of the loaded stack slot.  If
00112   /// not, return 0.  This predicate must return 0 if the instruction has
00113   /// any side effects other than storing to the stack slot.
00114   virtual unsigned isStoreToStackSlot(const MachineInstr *MI,
00115                                       int &FrameIndex) const {
00116     return 0;
00117   }
00118 
00119   /// reMaterialize - Re-issue the specified 'original' instruction at the
00120   /// specific location targeting a new destination register.
00121   virtual void reMaterialize(MachineBasicBlock &MBB,
00122                              MachineBasicBlock::iterator MI,
00123                              unsigned DestReg,
00124                              const MachineInstr *Orig) const = 0;
00125 
00126   /// isInvariantLoad - Return true if the specified instruction (which is
00127   /// marked mayLoad) is loading from a location whose value is invariant across
00128   /// the function.  For example, loading a value from the constant pool or from
00129   /// from the argument area of a function if it does not change.  This should
00130   /// only return true of *all* loads the instruction does are invariant (if it
00131   /// does multiple loads).
00132   virtual bool isInvariantLoad(const MachineInstr *MI) const {
00133     return false;
00134   }
00135   
00136   /// convertToThreeAddress - This method must be implemented by targets that
00137   /// set the M_CONVERTIBLE_TO_3_ADDR flag.  When this flag is set, the target
00138   /// may be able to convert a two-address instruction into one or more true
00139   /// three-address instructions on demand.  This allows the X86 target (for
00140   /// example) to convert ADD and SHL instructions into LEA instructions if they
00141   /// would require register copies due to two-addressness.
00142   ///
00143   /// This method returns a null pointer if the transformation cannot be
00144   /// performed, otherwise it returns the last new instruction.
00145   ///
00146   virtual MachineInstr *
00147   convertToThreeAddress(MachineFunction::iterator &MFI,
00148                    MachineBasicBlock::iterator &MBBI, LiveVariables *LV) const {
00149     return 0;
00150   }
00151 
00152   /// commuteInstruction - If a target has any instructions that are commutable,
00153   /// but require converting to a different instruction or making non-trivial
00154   /// changes to commute them, this method can overloaded to do this.  The
00155   /// default implementation of this method simply swaps the first two operands
00156   /// of MI and returns it.
00157   ///
00158   /// If a target wants to make more aggressive changes, they can construct and
00159   /// return a new machine instruction.  If an instruction cannot commute, it
00160   /// can also return null.
00161   ///
00162   /// If NewMI is true, then a new machine instruction must be created.
00163   ///
00164   virtual MachineInstr *commuteInstruction(MachineInstr *MI,
00165                                            bool NewMI = false) const = 0;
00166 
00167   /// CommuteChangesDestination - Return true if commuting the specified
00168   /// instruction will also changes the destination operand. Also return the
00169   /// current operand index of the would be new destination register by
00170   /// reference. This can happen when the commutable instruction is also a
00171   /// two-address instruction.
00172   virtual bool CommuteChangesDestination(MachineInstr *MI,
00173                                          unsigned &OpIdx) const = 0;
00174 
00175   /// AnalyzeBranch - Analyze the branching code at the end of MBB, returning
00176   /// true if it cannot be understood (e.g. it's a switch dispatch or isn't
00177   /// implemented for a target).  Upon success, this returns false and returns
00178   /// with the following information in various cases:
00179   ///
00180   /// 1. If this block ends with no branches (it just falls through to its succ)
00181   ///    just return false, leaving TBB/FBB null.
00182   /// 2. If this block ends with only an unconditional branch, it sets TBB to be
00183   ///    the destination block.
00184   /// 3. If this block ends with an conditional branch and it falls through to
00185   ///    an successor block, it sets TBB to be the branch destination block and a
00186   ///    list of operands that evaluate the condition. These
00187   ///    operands can be passed to other TargetInstrInfo methods to create new
00188   ///    branches.
00189   /// 4. If this block ends with an conditional branch and an unconditional
00190   ///    block, it returns the 'true' destination in TBB, the 'false' destination
00191   ///    in FBB, and a list of operands that evaluate the condition. These
00192   ///    operands can be passed to other TargetInstrInfo methods to create new
00193   ///    branches.
00194   ///
00195   /// Note that RemoveBranch and InsertBranch must be implemented to support
00196   /// cases where this method returns success.
00197   ///
00198   /// If AllowModify is true, then this routine is allowed to modify the basic
00199   /// block (e.g. delete instructions after the unconditional branch).
00200   ///
00201   virtual bool AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
00202                              MachineBasicBlock *&FBB,
00203                              SmallVectorImpl<MachineOperand> &Cond,
00204                              bool AllowModify = false) const {
00205     return true;
00206   }
00207   
00208   /// RemoveBranch - Remove the branching code at the end of the specific MBB.
00209   /// This is only invoked in cases where AnalyzeBranch returns success. It
00210   /// returns the number of instructions that were removed.
00211   virtual unsigned RemoveBranch(MachineBasicBlock &MBB) const {
00212     assert(0 && "Target didn't implement TargetInstrInfo::RemoveBranch!"); 
00213     return 0;
00214   }
00215   
00216   /// InsertBranch - Insert a branch into the end of the specified
00217   /// MachineBasicBlock.  This operands to this method are the same as those
00218   /// returned by AnalyzeBranch.  This is invoked in cases where AnalyzeBranch
00219   /// returns success and when an unconditional branch (TBB is non-null, FBB is
00220   /// null, Cond is empty) needs to be inserted. It returns the number of
00221   /// instructions inserted.
00222   virtual unsigned InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
00223                             MachineBasicBlock *FBB,
00224                             const SmallVectorImpl<MachineOperand> &Cond) const {
00225     assert(0 && "Target didn't implement TargetInstrInfo::InsertBranch!"); 
00226     return 0;
00227   }
00228   
00229   /// copyRegToReg - Emit instructions to copy between a pair of registers. It
00230   /// returns false if the target does not how to copy between the specified
00231   /// registers.
00232   virtual bool copyRegToReg(MachineBasicBlock &MBB,
00233                             MachineBasicBlock::iterator MI,
00234                             unsigned DestReg, unsigned SrcReg,
00235                             const TargetRegisterClass *DestRC,
00236                             const TargetRegisterClass *SrcRC) const {
00237     assert(0 && "Target didn't implement TargetInstrInfo::copyRegToReg!");
00238     return false;
00239   }
00240   
00241   /// storeRegToStackSlot - Store the specified register of the given register
00242   /// class to the specified stack frame index. The store instruction is to be
00243   /// added to the given machine basic block before the specified machine
00244   /// instruction. If isKill is true, the register operand is the last use and
00245   /// must be marked kill.
00246   virtual void storeRegToStackSlot(MachineBasicBlock &MBB,
00247                                    MachineBasicBlock::iterator MI,
00248                                    unsigned SrcReg, bool isKill, int FrameIndex,
00249                                    const TargetRegisterClass *RC) const {
00250     assert(0 && "Target didn't implement TargetInstrInfo::storeRegToStackSlot!");
00251   }
00252 
00253   /// storeRegToAddr - Store the specified register of the given register class
00254   /// to the specified address. The store instruction is to be added to the
00255   /// given machine basic block before the specified machine instruction. If
00256   /// isKill is true, the register operand is the last use and must be marked
00257   /// kill.
00258   virtual void storeRegToAddr(MachineFunction &MF, unsigned SrcReg, bool isKill,
00259                               SmallVectorImpl<MachineOperand> &Addr,
00260                               const TargetRegisterClass *RC,
00261                               SmallVectorImpl<MachineInstr*> &NewMIs) const {
00262     assert(0 && "Target didn't implement TargetInstrInfo::storeRegToAddr!");
00263   }
00264 
00265   /// loadRegFromStackSlot - Load the specified register of the given register
00266   /// class from the specified stack frame index. The load instruction is to be
00267   /// added to the given machine basic block before the specified machine
00268   /// instruction.
00269   virtual void loadRegFromStackSlot(MachineBasicBlock &MBB,
00270                                     MachineBasicBlock::iterator MI,
00271                                     unsigned DestReg, int FrameIndex,
00272                                     const TargetRegisterClass *RC) const {
00273     assert(0 && "Target didn't implement TargetInstrInfo::loadRegFromStackSlot!");
00274   }
00275 
00276   /// loadRegFromAddr - Load the specified register of the given register class
00277   /// class from the specified address. The load instruction is to be added to
00278   /// the given machine basic block before the specified machine instruction.
00279   virtual void loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
00280                                SmallVectorImpl<MachineOperand> &Addr,
00281                                const TargetRegisterClass *RC,
00282                                SmallVectorImpl<MachineInstr*> &NewMIs) const {
00283     assert(0 && "Target didn't implement TargetInstrInfo::loadRegFromAddr!");
00284   }
00285   
00286   /// spillCalleeSavedRegisters - Issues instruction(s) to spill all callee
00287   /// saved registers and returns true if it isn't possible / profitable to do
00288   /// so by issuing a series of store instructions via
00289   /// storeRegToStackSlot(). Returns false otherwise.
00290   virtual bool spillCalleeSavedRegisters(MachineBasicBlock &MBB,
00291                                          MachineBasicBlock::iterator MI,
00292                                 const std::vector<CalleeSavedInfo> &CSI) const {
00293     return false;
00294   }
00295 
00296   /// restoreCalleeSavedRegisters - Issues instruction(s) to restore all callee
00297   /// saved registers and returns true if it isn't possible / profitable to do
00298   /// so by issuing a series of load instructions via loadRegToStackSlot().
00299   /// Returns false otherwise.
00300   virtual bool restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
00301                                            MachineBasicBlock::iterator MI,
00302                                 const std::vector<CalleeSavedInfo> &CSI) const {
00303     return false;
00304   }
00305   
00306   /// foldMemoryOperand - Attempt to fold a load or store of the specified stack
00307   /// slot into the specified machine instruction for the specified operand(s).
00308   /// If this is possible, a new instruction is returned with the specified
00309   /// operand folded, otherwise NULL is returned. The client is responsible for
00310   /// removing the old instruction and adding the new one in the instruction
00311   /// stream.
00312   MachineInstr* foldMemoryOperand(MachineFunction &MF,
00313                                   MachineInstr* MI,
00314                                   const SmallVectorImpl<unsigned> &Ops,
00315                                   int FrameIndex) const;
00316 
00317   /// foldMemoryOperand - Same as the previous version except it allows folding
00318   /// of any load and store from / to any address, not just from a specific
00319   /// stack slot.
00320   MachineInstr* foldMemoryOperand(MachineFunction &MF,
00321                                   MachineInstr* MI,
00322                                   const SmallVectorImpl<unsigned> &Ops,
00323                                   MachineInstr* LoadMI) const;
00324 
00325 protected:
00326   /// foldMemoryOperandImpl - Target-dependent implementation for
00327   /// foldMemoryOperand. Target-independent code in foldMemoryOperand will
00328   /// take care of adding a MachineMemOperand to the newly created instruction.
00329   virtual MachineInstr* foldMemoryOperandImpl(MachineFunction &MF,
00330                                           MachineInstr* MI,
00331                                           const SmallVectorImpl<unsigned> &Ops,
00332                                           int FrameIndex) const {
00333     return 0;
00334   }
00335 
00336   /// foldMemoryOperandImpl - Target-dependent implementation for
00337   /// foldMemoryOperand. Target-independent code in foldMemoryOperand will
00338   /// take care of adding a MachineMemOperand to the newly created instruction.
00339   virtual MachineInstr* foldMemoryOperandImpl(MachineFunction &MF,
00340                                               MachineInstr* MI,
00341                                               const SmallVectorImpl<unsigned> &Ops,
00342                                               MachineInstr* LoadMI) const {
00343     return 0;
00344   }
00345 
00346 public:
00347   /// canFoldMemoryOperand - Returns true for the specified load / store if
00348   /// folding is possible.
00349   virtual
00350   bool canFoldMemoryOperand(const MachineInstr *MI,
00351                             const SmallVectorImpl<unsigned> &Ops) const {
00352     return false;
00353   }
00354 
00355   /// unfoldMemoryOperand - Separate a single instruction which folded a load or
00356   /// a store or a load and a store into two or more instruction. If this is
00357   /// possible, returns true as well as the new instructions by reference.
00358   virtual bool unfoldMemoryOperand(MachineFunction &MF, MachineInstr *MI,
00359                                 unsigned Reg, bool UnfoldLoad, bool UnfoldStore,
00360                                   SmallVectorImpl<MachineInstr*> &NewMIs) const{
00361     return false;
00362   }
00363 
00364   virtual bool unfoldMemoryOperand(SelectionDAG &DAG, SDNode *N,
00365                                    SmallVectorImpl<SDNode*> &NewNodes) const {
00366     return false;
00367   }
00368 
00369   /// getOpcodeAfterMemoryUnfold - Returns the opcode of the would be new
00370   /// instruction after load / store are unfolded from an instruction of the
00371   /// specified opcode. It returns zero if the specified unfolding is not
00372   /// possible.
00373   virtual unsigned getOpcodeAfterMemoryUnfold(unsigned Opc,
00374                                       bool UnfoldLoad, bool UnfoldStore) const {
00375     return 0;
00376   }
00377   
00378   /// BlockHasNoFallThrough - Return true if the specified block does not
00379   /// fall-through into its successor block.  This is primarily used when a
00380   /// branch is unanalyzable.  It is useful for things like unconditional
00381   /// indirect branches (jump tables).
00382   virtual bool BlockHasNoFallThrough(const MachineBasicBlock &MBB) const {
00383     return false;
00384   }
00385   
00386   /// ReverseBranchCondition - Reverses the branch condition of the specified
00387   /// condition list, returning false on success and true if it cannot be
00388   /// reversed.
00389   virtual
00390   bool ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
00391     return true;
00392   }
00393   
00394   /// insertNoop - Insert a noop into the instruction stream at the specified
00395   /// point.
00396   virtual void insertNoop(MachineBasicBlock &MBB, 
00397                           MachineBasicBlock::iterator MI) const {
00398     assert(0 && "Target didn't implement insertNoop!");
00399     abort();
00400   }
00401 
00402   /// isPredicated - Returns true if the instruction is already predicated.
00403   ///
00404   virtual bool isPredicated(const MachineInstr *MI) const {
00405     return false;
00406   }
00407 
00408   /// isUnpredicatedTerminator - Returns true if the instruction is a
00409   /// terminator instruction that has not been predicated.
00410   virtual bool isUnpredicatedTerminator(const MachineInstr *MI) const;
00411 
00412   /// PredicateInstruction - Convert the instruction into a predicated
00413   /// instruction. It returns true if the operation was successful.
00414   virtual
00415   bool PredicateInstruction(MachineInstr *MI,
00416                         const SmallVectorImpl<MachineOperand> &Pred) const = 0;
00417 
00418   /// SubsumesPredicate - Returns true if the first specified predicate
00419   /// subsumes the second, e.g. GE subsumes GT.
00420   virtual
00421   bool SubsumesPredicate(const SmallVectorImpl<MachineOperand> &Pred1,
00422                          const SmallVectorImpl<MachineOperand> &Pred2) const {
00423     return false;
00424   }
00425 
00426   /// DefinesPredicate - If the specified instruction defines any predicate
00427   /// or condition code register(s) used for predication, returns true as well
00428   /// as the definition predicate(s) by reference.
00429   virtual bool DefinesPredicate(MachineInstr *MI,
00430                                 std::vector<MachineOperand> &Pred) const {
00431     return false;
00432   }
00433 
00434   /// IgnoreRegisterClassBarriers - Returns true if pre-register allocation
00435   /// live interval splitting pass should ignore barriers of the specified
00436   /// register class.
00437   virtual bool IgnoreRegisterClassBarriers(const TargetRegisterClass *RC) const{
00438     return true;
00439   }
00440 
00441   /// getPointerRegClass - Returns a TargetRegisterClass used for pointer
00442   /// values.
00443   virtual const TargetRegisterClass *getPointerRegClass() const {
00444     assert(0 && "Target didn't implement getPointerRegClass!");
00445     abort();
00446     return 0; // Must return a value in order to compile with VS 2005
00447   }
00448 
00449   /// GetInstSize - Returns the size of the specified Instruction.
00450   /// 
00451   virtual unsigned GetInstSizeInBytes(const MachineInstr *MI) const {
00452     assert(0 && "Target didn't implement TargetInstrInfo::GetInstSize!");
00453     return 0;
00454   }
00455 
00456   /// GetFunctionSizeInBytes - Returns the size of the specified MachineFunction.
00457   /// 
00458   virtual unsigned GetFunctionSizeInBytes(const MachineFunction &MF) const = 0;
00459 };
00460 
00461 /// TargetInstrInfoImpl - This is the default implementation of
00462 /// TargetInstrInfo, which just provides a couple of default implementations
00463 /// for various methods.  This separated out because it is implemented in
00464 /// libcodegen, not in libtarget.
00465 class TargetInstrInfoImpl : public TargetInstrInfo {
00466 protected:
00467   TargetInstrInfoImpl(const TargetInstrDesc *desc, unsigned NumOpcodes)
00468   : TargetInstrInfo(desc, NumOpcodes) {}
00469 public:
00470   virtual MachineInstr *commuteInstruction(MachineInstr *MI,
00471                                            bool NewMI = false) const;
00472   virtual bool CommuteChangesDestination(MachineInstr *MI,
00473                                          unsigned &OpIdx) const;
00474   virtual bool PredicateInstruction(MachineInstr *MI,
00475                             const SmallVectorImpl<MachineOperand> &Pred) const;
00476   virtual void reMaterialize(MachineBasicBlock &MBB,
00477                              MachineBasicBlock::iterator MI,
00478                              unsigned DestReg,
00479                              const MachineInstr *Orig) const;
00480   virtual unsigned GetFunctionSizeInBytes(const MachineFunction &MF) const;
00481 };
00482 
00483 } // End llvm namespace
00484 
00485 #endif