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
00092   /// and leave the source and dest operands in the passed parameters.
00093   virtual bool isMoveInstr(const MachineInstr& MI,
00094                            unsigned& sourceReg,
00095                            unsigned& destReg) 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   virtual bool AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
00199                              MachineBasicBlock *&FBB,
00200                              SmallVectorImpl<MachineOperand> &Cond) const {
00201     return true;
00202   }
00203   
00204   /// RemoveBranch - Remove the branching code at the end of the specific MBB.
00205   /// This is only invoked in cases where AnalyzeBranch returns success. It
00206   /// returns the number of instructions that were removed.
00207   virtual unsigned RemoveBranch(MachineBasicBlock &MBB) const {
00208     assert(0 && "Target didn't implement TargetInstrInfo::RemoveBranch!"); 
00209     return 0;
00210   }
00211   
00212   /// InsertBranch - Insert a branch into the end of the specified
00213   /// MachineBasicBlock.  This operands to this method are the same as those
00214   /// returned by AnalyzeBranch.  This is invoked in cases where AnalyzeBranch
00215   /// returns success and when an unconditional branch (TBB is non-null, FBB is
00216   /// null, Cond is empty) needs to be inserted. It returns the number of
00217   /// instructions inserted.
00218   virtual unsigned InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
00219                             MachineBasicBlock *FBB,
00220                             const SmallVectorImpl<MachineOperand> &Cond) const {
00221     assert(0 && "Target didn't implement TargetInstrInfo::InsertBranch!"); 
00222     return 0;
00223   }
00224   
00225   /// copyRegToReg - Emit instructions to copy between a pair of registers. It
00226   /// returns false if the target does not how to copy between the specified
00227   /// registers.
00228   virtual bool copyRegToReg(MachineBasicBlock &MBB,
00229                             MachineBasicBlock::iterator MI,
00230                             unsigned DestReg, unsigned SrcReg,
00231                             const TargetRegisterClass *DestRC,
00232                             const TargetRegisterClass *SrcRC) const {
00233     assert(0 && "Target didn't implement TargetInstrInfo::copyRegToReg!");
00234     return false;
00235   }
00236   
00237   /// storeRegToStackSlot - Store the specified register of the given register
00238   /// class to the specified stack frame index. The store instruction is to be
00239   /// added to the given machine basic block before the specified machine
00240   /// instruction. If isKill is true, the register operand is the last use and
00241   /// must be marked kill.
00242   virtual void storeRegToStackSlot(MachineBasicBlock &MBB,
00243                                    MachineBasicBlock::iterator MI,
00244                                    unsigned SrcReg, bool isKill, int FrameIndex,
00245                                    const TargetRegisterClass *RC) const {
00246     assert(0 && "Target didn't implement TargetInstrInfo::storeRegToStackSlot!");
00247   }
00248 
00249   /// storeRegToAddr - Store the specified register of the given register class
00250   /// to the specified address. The store instruction is to be added to the
00251   /// given machine basic block before the specified machine instruction. If
00252   /// isKill is true, the register operand is the last use and must be marked
00253   /// kill.
00254   virtual void storeRegToAddr(MachineFunction &MF, unsigned SrcReg, bool isKill,
00255                               SmallVectorImpl<MachineOperand> &Addr,
00256                               const TargetRegisterClass *RC,
00257                               SmallVectorImpl<MachineInstr*> &NewMIs) const {
00258     assert(0 && "Target didn't implement TargetInstrInfo::storeRegToAddr!");
00259   }
00260 
00261   /// loadRegFromStackSlot - Load the specified register of the given register
00262   /// class from the specified stack frame index. The load instruction is to be
00263   /// added to the given machine basic block before the specified machine
00264   /// instruction.
00265   virtual void loadRegFromStackSlot(MachineBasicBlock &MBB,
00266                                     MachineBasicBlock::iterator MI,
00267                                     unsigned DestReg, int FrameIndex,
00268                                     const TargetRegisterClass *RC) const {
00269     assert(0 && "Target didn't implement TargetInstrInfo::loadRegFromStackSlot!");
00270   }
00271 
00272   /// loadRegFromAddr - Load the specified register of the given register class
00273   /// class from the specified address. The load instruction is to be added to
00274   /// the given machine basic block before the specified machine instruction.
00275   virtual void loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
00276                                SmallVectorImpl<MachineOperand> &Addr,
00277                                const TargetRegisterClass *RC,
00278                                SmallVectorImpl<MachineInstr*> &NewMIs) const {
00279     assert(0 && "Target didn't implement TargetInstrInfo::loadRegFromAddr!");
00280   }
00281   
00282   /// spillCalleeSavedRegisters - Issues instruction(s) to spill all callee
00283   /// saved registers and returns true if it isn't possible / profitable to do
00284   /// so by issuing a series of store instructions via
00285   /// storeRegToStackSlot(). Returns false otherwise.
00286   virtual bool spillCalleeSavedRegisters(MachineBasicBlock &MBB,
00287                                          MachineBasicBlock::iterator MI,
00288                                 const std::vector<CalleeSavedInfo> &CSI) const {
00289     return false;
00290   }
00291 
00292   /// restoreCalleeSavedRegisters - Issues instruction(s) to restore all callee
00293   /// saved registers and returns true if it isn't possible / profitable to do
00294   /// so by issuing a series of load instructions via loadRegToStackSlot().
00295   /// Returns false otherwise.
00296   virtual bool restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
00297                                            MachineBasicBlock::iterator MI,
00298                                 const std::vector<CalleeSavedInfo> &CSI) const {
00299     return false;
00300   }
00301   
00302   /// foldMemoryOperand - Attempt to fold a load or store of the specified stack
00303   /// slot into the specified machine instruction for the specified operand(s).
00304   /// If this is possible, a new instruction is returned with the specified
00305   /// operand folded, otherwise NULL is returned. The client is responsible for
00306   /// removing the old instruction and adding the new one in the instruction
00307   /// stream.
00308   MachineInstr* foldMemoryOperand(MachineFunction &MF,
00309                                   MachineInstr* MI,
00310                                   const SmallVectorImpl<unsigned> &Ops,
00311                                   int FrameIndex) const;
00312 
00313   /// foldMemoryOperand - Same as the previous version except it allows folding
00314   /// of any load and store from / to any address, not just from a specific
00315   /// stack slot.
00316   MachineInstr* foldMemoryOperand(MachineFunction &MF,
00317                                   MachineInstr* MI,
00318                                   const SmallVectorImpl<unsigned> &Ops,
00319                                   MachineInstr* LoadMI) const;
00320 
00321 protected:
00322   /// foldMemoryOperandImpl - Target-dependent implementation for
00323   /// foldMemoryOperand. Target-independent code in foldMemoryOperand will
00324   /// take care of adding a MachineMemOperand to the newly created instruction.
00325   virtual MachineInstr* foldMemoryOperandImpl(MachineFunction &MF,
00326                                           MachineInstr* MI,
00327                                           const SmallVectorImpl<unsigned> &Ops,
00328                                           int FrameIndex) const {
00329     return 0;
00330   }
00331 
00332   /// foldMemoryOperandImpl - Target-dependent implementation for
00333   /// foldMemoryOperand. Target-independent code in foldMemoryOperand will
00334   /// take care of adding a MachineMemOperand to the newly created instruction.
00335   virtual MachineInstr* foldMemoryOperandImpl(MachineFunction &MF,
00336                                               MachineInstr* MI,
00337                                               const SmallVectorImpl<unsigned> &Ops,
00338                                               MachineInstr* LoadMI) const {
00339     return 0;
00340   }
00341 
00342 public:
00343   /// canFoldMemoryOperand - Returns true for the specified load / store if
00344   /// folding is possible.
00345   virtual
00346   bool canFoldMemoryOperand(const MachineInstr *MI,
00347                             const SmallVectorImpl<unsigned> &Ops) const {
00348     return false;
00349   }
00350 
00351   /// unfoldMemoryOperand - Separate a single instruction which folded a load or
00352   /// a store or a load and a store into two or more instruction. If this is
00353   /// possible, returns true as well as the new instructions by reference.
00354   virtual bool unfoldMemoryOperand(MachineFunction &MF, MachineInstr *MI,
00355                                 unsigned Reg, bool UnfoldLoad, bool UnfoldStore,
00356                                   SmallVectorImpl<MachineInstr*> &NewMIs) const{
00357     return false;
00358   }
00359 
00360   virtual bool unfoldMemoryOperand(SelectionDAG &DAG, SDNode *N,
00361                                    SmallVectorImpl<SDNode*> &NewNodes) const {
00362     return false;
00363   }
00364 
00365   /// getOpcodeAfterMemoryUnfold - Returns the opcode of the would be new
00366   /// instruction after load / store are unfolded from an instruction of the
00367   /// specified opcode. It returns zero if the specified unfolding is not
00368   /// possible.
00369   virtual unsigned getOpcodeAfterMemoryUnfold(unsigned Opc,
00370                                       bool UnfoldLoad, bool UnfoldStore) const {
00371     return 0;
00372   }
00373   
00374   /// BlockHasNoFallThrough - Return true if the specified block does not
00375   /// fall-through into its successor block.  This is primarily used when a
00376   /// branch is unanalyzable.  It is useful for things like unconditional
00377   /// indirect branches (jump tables).
00378   virtual bool BlockHasNoFallThrough(const MachineBasicBlock &MBB) const {
00379     return false;
00380   }
00381   
00382   /// ReverseBranchCondition - Reverses the branch condition of the specified
00383   /// condition list, returning false on success and true if it cannot be
00384   /// reversed.
00385   virtual
00386   bool ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
00387     return true;
00388   }
00389   
00390   /// insertNoop - Insert a noop into the instruction stream at the specified
00391   /// point.
00392   virtual void insertNoop(MachineBasicBlock &MBB, 
00393                           MachineBasicBlock::iterator MI) const {
00394     assert(0 && "Target didn't implement insertNoop!");
00395     abort();
00396   }
00397 
00398   /// isPredicated - Returns true if the instruction is already predicated.
00399   ///
00400   virtual bool isPredicated(const MachineInstr *MI) const {
00401     return false;
00402   }
00403 
00404   /// isUnpredicatedTerminator - Returns true if the instruction is a
00405   /// terminator instruction that has not been predicated.
00406   virtual bool isUnpredicatedTerminator(const MachineInstr *MI) const;
00407 
00408   /// PredicateInstruction - Convert the instruction into a predicated
00409   /// instruction. It returns true if the operation was successful.
00410   virtual
00411   bool PredicateInstruction(MachineInstr *MI,
00412                         const SmallVectorImpl<MachineOperand> &Pred) const = 0;
00413 
00414   /// SubsumesPredicate - Returns true if the first specified predicate
00415   /// subsumes the second, e.g. GE subsumes GT.
00416   virtual
00417   bool SubsumesPredicate(const SmallVectorImpl<MachineOperand> &Pred1,
00418                          const SmallVectorImpl<MachineOperand> &Pred2) const {
00419     return false;
00420   }
00421 
00422   /// DefinesPredicate - If the specified instruction defines any predicate
00423   /// or condition code register(s) used for predication, returns true as well
00424   /// as the definition predicate(s) by reference.
00425   virtual bool DefinesPredicate(MachineInstr *MI,
00426                                 std::vector<MachineOperand> &Pred) const {
00427     return false;
00428   }
00429 
00430   /// IgnoreRegisterClassBarriers - Returns true if pre-register allocation
00431   /// live interval splitting pass should ignore barriers of the specified
00432   /// register class.
00433   virtual bool IgnoreRegisterClassBarriers(const TargetRegisterClass *RC) const{
00434     return true;
00435   }
00436 
00437   /// getPointerRegClass - Returns a TargetRegisterClass used for pointer
00438   /// values.
00439   virtual const TargetRegisterClass *getPointerRegClass() const {
00440     assert(0 && "Target didn't implement getPointerRegClass!");
00441     abort();
00442     return 0; // Must return a value in order to compile with VS 2005
00443   }
00444 
00445   /// GetInstSize - Returns the size of the specified Instruction.
00446   /// 
00447   virtual unsigned GetInstSizeInBytes(const MachineInstr *MI) const {
00448     assert(0 && "Target didn't implement TargetInstrInfo::GetInstSize!");
00449     return 0;
00450   }
00451 
00452   /// GetFunctionSizeInBytes - Returns the size of the specified MachineFunction.
00453   /// 
00454   virtual unsigned GetFunctionSizeInBytes(const MachineFunction &MF) const = 0;
00455 };
00456 
00457 /// TargetInstrInfoImpl - This is the default implementation of
00458 /// TargetInstrInfo, which just provides a couple of default implementations
00459 /// for various methods.  This separated out because it is implemented in
00460 /// libcodegen, not in libtarget.
00461 class TargetInstrInfoImpl : public TargetInstrInfo {
00462 protected:
00463   TargetInstrInfoImpl(const TargetInstrDesc *desc, unsigned NumOpcodes)
00464   : TargetInstrInfo(desc, NumOpcodes) {}
00465 public:
00466   virtual MachineInstr *commuteInstruction(MachineInstr *MI,
00467                                            bool NewMI = false) const;
00468   virtual bool CommuteChangesDestination(MachineInstr *MI,
00469                                          unsigned &OpIdx) const;
00470   virtual bool PredicateInstruction(MachineInstr *MI,
00471                             const SmallVectorImpl<MachineOperand> &Pred) const;
00472   virtual void reMaterialize(MachineBasicBlock &MBB,
00473                              MachineBasicBlock::iterator MI,
00474                              unsigned DestReg,
00475                              const MachineInstr *Orig) const;
00476   virtual unsigned GetFunctionSizeInBytes(const MachineFunction &MF) const;
00477 };
00478 
00479 } // End llvm namespace
00480 
00481 #endif



This web site is hosted by the Computer Science Department at the University of Illinois at Urbana-Champaign.