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