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/ADT/SmallSet.h"
00018 #include "llvm/ADT/DenseMap.h"
00019 #include "llvm/CodeGen/DFAPacketizer.h"
00020 #include "llvm/CodeGen/MachineFunction.h"
00021 #include "llvm/CodeGen/MachineCombinerPattern.h"
00022 #include "llvm/MC/MCInstrInfo.h"
00023 #include "llvm/Target/TargetRegisterInfo.h"
00024 
00025 namespace llvm {
00026 
00027 class InstrItineraryData;
00028 class LiveVariables;
00029 class MCAsmInfo;
00030 class MachineMemOperand;
00031 class MachineRegisterInfo;
00032 class MDNode;
00033 class MCInst;
00034 struct MCSchedModel;
00035 class MCSymbolRefExpr;
00036 class SDNode;
00037 class ScheduleHazardRecognizer;
00038 class SelectionDAG;
00039 class ScheduleDAG;
00040 class TargetRegisterClass;
00041 class TargetRegisterInfo;
00042 class BranchProbability;
00043 class TargetSubtargetInfo;
00044 
00045 template<class T> class SmallVectorImpl;
00046 
00047 
00048 //---------------------------------------------------------------------------
00049 ///
00050 /// TargetInstrInfo - Interface to description of machine instruction set
00051 ///
00052 class TargetInstrInfo : public MCInstrInfo {
00053   TargetInstrInfo(const TargetInstrInfo &) LLVM_DELETED_FUNCTION;
00054   void operator=(const TargetInstrInfo &) LLVM_DELETED_FUNCTION;
00055 public:
00056   TargetInstrInfo(int CFSetupOpcode = -1, int CFDestroyOpcode = -1)
00057     : CallFrameSetupOpcode(CFSetupOpcode),
00058       CallFrameDestroyOpcode(CFDestroyOpcode) {
00059   }
00060 
00061   virtual ~TargetInstrInfo();
00062 
00063   /// getRegClass - Givem a machine instruction descriptor, returns the register
00064   /// class constraint for OpNum, or NULL.
00065   const TargetRegisterClass *getRegClass(const MCInstrDesc &TID,
00066                                          unsigned OpNum,
00067                                          const TargetRegisterInfo *TRI,
00068                                          const MachineFunction &MF) const;
00069 
00070   /// isTriviallyReMaterializable - Return true if the instruction is trivially
00071   /// rematerializable, meaning it has no side effects and requires no operands
00072   /// that aren't always available.
00073   bool isTriviallyReMaterializable(const MachineInstr *MI,
00074                                    AliasAnalysis *AA = nullptr) const {
00075     return MI->getOpcode() == TargetOpcode::IMPLICIT_DEF ||
00076            (MI->getDesc().isRematerializable() &&
00077             (isReallyTriviallyReMaterializable(MI, AA) ||
00078              isReallyTriviallyReMaterializableGeneric(MI, AA)));
00079   }
00080 
00081 protected:
00082   /// isReallyTriviallyReMaterializable - For instructions with opcodes for
00083   /// which the M_REMATERIALIZABLE flag is set, this hook lets the target
00084   /// specify whether the instruction is actually trivially rematerializable,
00085   /// taking into consideration its operands. This predicate must return false
00086   /// if the instruction has any side effects other than producing a value, or
00087   /// if it requres any address registers that are not always available.
00088   virtual bool isReallyTriviallyReMaterializable(const MachineInstr *MI,
00089                                                  AliasAnalysis *AA) const {
00090     return false;
00091   }
00092 
00093 private:
00094   /// isReallyTriviallyReMaterializableGeneric - For instructions with opcodes
00095   /// for which the M_REMATERIALIZABLE flag is set and the target hook
00096   /// isReallyTriviallyReMaterializable returns false, this function does
00097   /// target-independent tests to determine if the instruction is really
00098   /// trivially rematerializable.
00099   bool isReallyTriviallyReMaterializableGeneric(const MachineInstr *MI,
00100                                                 AliasAnalysis *AA) const;
00101 
00102 public:
00103   /// getCallFrameSetup/DestroyOpcode - These methods return the opcode of the
00104   /// frame setup/destroy instructions if they exist (-1 otherwise).  Some
00105   /// targets use pseudo instructions in order to abstract away the difference
00106   /// between operating with a frame pointer and operating without, through the
00107   /// use of these two instructions.
00108   ///
00109   int getCallFrameSetupOpcode() const { return CallFrameSetupOpcode; }
00110   int getCallFrameDestroyOpcode() const { return CallFrameDestroyOpcode; }
00111 
00112   /// isCoalescableExtInstr - Return true if the instruction is a "coalescable"
00113   /// extension instruction. That is, it's like a copy where it's legal for the
00114   /// source to overlap the destination. e.g. X86::MOVSX64rr32. If this returns
00115   /// true, then it's expected the pre-extension value is available as a subreg
00116   /// of the result register. This also returns the sub-register index in
00117   /// SubIdx.
00118   virtual bool isCoalescableExtInstr(const MachineInstr &MI,
00119                                      unsigned &SrcReg, unsigned &DstReg,
00120                                      unsigned &SubIdx) const {
00121     return false;
00122   }
00123 
00124   /// isLoadFromStackSlot - If the specified machine instruction is a direct
00125   /// load from a stack slot, return the virtual or physical register number of
00126   /// the destination along with the FrameIndex of the loaded stack slot.  If
00127   /// not, return 0.  This predicate must return 0 if the instruction has
00128   /// any side effects other than loading from the stack slot.
00129   virtual unsigned isLoadFromStackSlot(const MachineInstr *MI,
00130                                        int &FrameIndex) const {
00131     return 0;
00132   }
00133 
00134   /// isLoadFromStackSlotPostFE - Check for post-frame ptr elimination
00135   /// stack locations as well.  This uses a heuristic so it isn't
00136   /// reliable for correctness.
00137   virtual unsigned isLoadFromStackSlotPostFE(const MachineInstr *MI,
00138                                              int &FrameIndex) const {
00139     return 0;
00140   }
00141 
00142   /// hasLoadFromStackSlot - If the specified machine instruction has
00143   /// a load from a stack slot, return true along with the FrameIndex
00144   /// of the loaded stack slot and the machine mem operand containing
00145   /// the reference.  If not, return false.  Unlike
00146   /// isLoadFromStackSlot, this returns true for any instructions that
00147   /// loads from the stack.  This is just a hint, as some cases may be
00148   /// missed.
00149   virtual bool hasLoadFromStackSlot(const MachineInstr *MI,
00150                                     const MachineMemOperand *&MMO,
00151                                     int &FrameIndex) const;
00152 
00153   /// isStoreToStackSlot - If the specified machine instruction is a direct
00154   /// store to a stack slot, return the virtual or physical register number of
00155   /// the source reg along with the FrameIndex of the loaded stack slot.  If
00156   /// not, return 0.  This predicate must return 0 if the instruction has
00157   /// any side effects other than storing to the stack slot.
00158   virtual unsigned isStoreToStackSlot(const MachineInstr *MI,
00159                                       int &FrameIndex) const {
00160     return 0;
00161   }
00162 
00163   /// isStoreToStackSlotPostFE - Check for post-frame ptr elimination
00164   /// stack locations as well.  This uses a heuristic so it isn't
00165   /// reliable for correctness.
00166   virtual unsigned isStoreToStackSlotPostFE(const MachineInstr *MI,
00167                                             int &FrameIndex) const {
00168     return 0;
00169   }
00170 
00171   /// hasStoreToStackSlot - If the specified machine instruction has a
00172   /// store to a stack slot, return true along with the FrameIndex of
00173   /// the loaded stack slot and the machine mem operand containing the
00174   /// reference.  If not, return false.  Unlike isStoreToStackSlot,
00175   /// this returns true for any instructions that stores to the
00176   /// stack.  This is just a hint, as some cases may be missed.
00177   virtual bool hasStoreToStackSlot(const MachineInstr *MI,
00178                                    const MachineMemOperand *&MMO,
00179                                    int &FrameIndex) const;
00180 
00181   /// isStackSlotCopy - Return true if the specified machine instruction
00182   /// is a copy of one stack slot to another and has no other effect.
00183   /// Provide the identity of the two frame indices.
00184   virtual bool isStackSlotCopy(const MachineInstr *MI, int &DestFrameIndex,
00185                                int &SrcFrameIndex) const {
00186     return false;
00187   }
00188 
00189   /// Compute the size in bytes and offset within a stack slot of a spilled
00190   /// register or subregister.
00191   ///
00192   /// \param [out] Size in bytes of the spilled value.
00193   /// \param [out] Offset in bytes within the stack slot.
00194   /// \returns true if both Size and Offset are successfully computed.
00195   ///
00196   /// Not all subregisters have computable spill slots. For example,
00197   /// subregisters registers may not be byte-sized, and a pair of discontiguous
00198   /// subregisters has no single offset.
00199   ///
00200   /// Targets with nontrivial bigendian implementations may need to override
00201   /// this, particularly to support spilled vector registers.
00202   virtual bool getStackSlotRange(const TargetRegisterClass *RC, unsigned SubIdx,
00203                                  unsigned &Size, unsigned &Offset,
00204                                  const TargetMachine *TM) const;
00205 
00206   /// isAsCheapAsAMove - Return true if the instruction is as cheap as a move
00207   /// instruction.
00208   ///
00209   /// Targets for different archs need to override this, and different
00210   /// micro-architectures can also be finely tuned inside.
00211   virtual bool isAsCheapAsAMove(const MachineInstr *MI) const {
00212     return MI->isAsCheapAsAMove();
00213   }
00214 
00215   /// reMaterialize - Re-issue the specified 'original' instruction at the
00216   /// specific location targeting a new destination register.
00217   /// The register in Orig->getOperand(0).getReg() will be substituted by
00218   /// DestReg:SubIdx. Any existing subreg index is preserved or composed with
00219   /// SubIdx.
00220   virtual void reMaterialize(MachineBasicBlock &MBB,
00221                              MachineBasicBlock::iterator MI,
00222                              unsigned DestReg, unsigned SubIdx,
00223                              const MachineInstr *Orig,
00224                              const TargetRegisterInfo &TRI) const;
00225 
00226   /// duplicate - Create a duplicate of the Orig instruction in MF. This is like
00227   /// MachineFunction::CloneMachineInstr(), but the target may update operands
00228   /// that are required to be unique.
00229   ///
00230   /// The instruction must be duplicable as indicated by isNotDuplicable().
00231   virtual MachineInstr *duplicate(MachineInstr *Orig,
00232                                   MachineFunction &MF) const;
00233 
00234   /// convertToThreeAddress - This method must be implemented by targets that
00235   /// set the M_CONVERTIBLE_TO_3_ADDR flag.  When this flag is set, the target
00236   /// may be able to convert a two-address instruction into one or more true
00237   /// three-address instructions on demand.  This allows the X86 target (for
00238   /// example) to convert ADD and SHL instructions into LEA instructions if they
00239   /// would require register copies due to two-addressness.
00240   ///
00241   /// This method returns a null pointer if the transformation cannot be
00242   /// performed, otherwise it returns the last new instruction.
00243   ///
00244   virtual MachineInstr *
00245   convertToThreeAddress(MachineFunction::iterator &MFI,
00246                    MachineBasicBlock::iterator &MBBI, LiveVariables *LV) const {
00247     return nullptr;
00248   }
00249 
00250   /// commuteInstruction - If a target has any instructions that are
00251   /// commutable but require converting to different instructions or making
00252   /// non-trivial changes to commute them, this method can overloaded to do
00253   /// that.  The default implementation simply swaps the commutable operands.
00254   /// If NewMI is false, MI is modified in place and returned; otherwise, a
00255   /// new machine instruction is created and returned.  Do not call this
00256   /// method for a non-commutable instruction, but there may be some cases
00257   /// where this method fails and returns null.
00258   virtual MachineInstr *commuteInstruction(MachineInstr *MI,
00259                                            bool NewMI = false) const;
00260 
00261   /// findCommutedOpIndices - If specified MI is commutable, return the two
00262   /// operand indices that would swap value. Return false if the instruction
00263   /// is not in a form which this routine understands.
00264   virtual bool findCommutedOpIndices(MachineInstr *MI, unsigned &SrcOpIdx1,
00265                                      unsigned &SrcOpIdx2) const;
00266 
00267   /// A pair composed of a register and a sub-register index.
00268   /// Used to give some type checking when modeling Reg:SubReg.
00269   struct RegSubRegPair {
00270     unsigned Reg;
00271     unsigned SubReg;
00272     RegSubRegPair(unsigned Reg = 0, unsigned SubReg = 0)
00273         : Reg(Reg), SubReg(SubReg) {}
00274   };
00275   /// A pair composed of a pair of a register and a sub-register index,
00276   /// and another sub-register index.
00277   /// Used to give some type checking when modeling Reg:SubReg1, SubReg2.
00278   struct RegSubRegPairAndIdx : RegSubRegPair {
00279     unsigned SubIdx;
00280     RegSubRegPairAndIdx(unsigned Reg = 0, unsigned SubReg = 0,
00281                         unsigned SubIdx = 0)
00282         : RegSubRegPair(Reg, SubReg), SubIdx(SubIdx) {}
00283   };
00284 
00285   /// Build the equivalent inputs of a REG_SEQUENCE for the given \p MI
00286   /// and \p DefIdx.
00287   /// \p [out] InputRegs of the equivalent REG_SEQUENCE. Each element of
00288   /// the list is modeled as <Reg:SubReg, SubIdx>.
00289   /// E.g., REG_SEQUENCE vreg1:sub1, sub0, vreg2, sub1 would produce
00290   /// two elements:
00291   /// - vreg1:sub1, sub0
00292   /// - vreg2<:0>, sub1
00293   ///
00294   /// \returns true if it is possible to build such an input sequence
00295   /// with the pair \p MI, \p DefIdx. False otherwise.
00296   ///
00297   /// \pre MI.isRegSequence() or MI.isRegSequenceLike().
00298   ///
00299   /// \note The generic implementation does not provide any support for
00300   /// MI.isRegSequenceLike(). In other words, one has to override
00301   /// getRegSequenceLikeInputs for target specific instructions.
00302   bool
00303   getRegSequenceInputs(const MachineInstr &MI, unsigned DefIdx,
00304                        SmallVectorImpl<RegSubRegPairAndIdx> &InputRegs) const;
00305 
00306   /// Build the equivalent inputs of a EXTRACT_SUBREG for the given \p MI
00307   /// and \p DefIdx.
00308   /// \p [out] InputReg of the equivalent EXTRACT_SUBREG.
00309   /// E.g., EXTRACT_SUBREG vreg1:sub1, sub0, sub1 would produce:
00310   /// - vreg1:sub1, sub0
00311   ///
00312   /// \returns true if it is possible to build such an input sequence
00313   /// with the pair \p MI, \p DefIdx. False otherwise.
00314   ///
00315   /// \pre MI.isExtractSubreg() or MI.isExtractSubregLike().
00316   ///
00317   /// \note The generic implementation does not provide any support for
00318   /// MI.isExtractSubregLike(). In other words, one has to override
00319   /// getExtractSubregLikeInputs for target specific instructions.
00320   bool
00321   getExtractSubregInputs(const MachineInstr &MI, unsigned DefIdx,
00322                          RegSubRegPairAndIdx &InputReg) const;
00323 
00324   /// Build the equivalent inputs of a INSERT_SUBREG for the given \p MI
00325   /// and \p DefIdx.
00326   /// \p [out] BaseReg and \p [out] InsertedReg contain
00327   /// the equivalent inputs of INSERT_SUBREG.
00328   /// E.g., INSERT_SUBREG vreg0:sub0, vreg1:sub1, sub3 would produce:
00329   /// - BaseReg: vreg0:sub0
00330   /// - InsertedReg: vreg1:sub1, sub3
00331   ///
00332   /// \returns true if it is possible to build such an input sequence
00333   /// with the pair \p MI, \p DefIdx. False otherwise.
00334   ///
00335   /// \pre MI.isInsertSubreg() or MI.isInsertSubregLike().
00336   ///
00337   /// \note The generic implementation does not provide any support for
00338   /// MI.isInsertSubregLike(). In other words, one has to override
00339   /// getInsertSubregLikeInputs for target specific instructions.
00340   bool
00341   getInsertSubregInputs(const MachineInstr &MI, unsigned DefIdx,
00342                         RegSubRegPair &BaseReg,
00343                         RegSubRegPairAndIdx &InsertedReg) const;
00344 
00345 
00346   /// produceSameValue - Return true if two machine instructions would produce
00347   /// identical values. By default, this is only true when the two instructions
00348   /// are deemed identical except for defs. If this function is called when the
00349   /// IR is still in SSA form, the caller can pass the MachineRegisterInfo for
00350   /// aggressive checks.
00351   virtual bool produceSameValue(const MachineInstr *MI0,
00352                                 const MachineInstr *MI1,
00353                                 const MachineRegisterInfo *MRI = nullptr) const;
00354 
00355   /// AnalyzeBranch - Analyze the branching code at the end of MBB, returning
00356   /// true if it cannot be understood (e.g. it's a switch dispatch or isn't
00357   /// implemented for a target).  Upon success, this returns false and returns
00358   /// with the following information in various cases:
00359   ///
00360   /// 1. If this block ends with no branches (it just falls through to its succ)
00361   ///    just return false, leaving TBB/FBB null.
00362   /// 2. If this block ends with only an unconditional branch, it sets TBB to be
00363   ///    the destination block.
00364   /// 3. If this block ends with a conditional branch and it falls through to a
00365   ///    successor block, it sets TBB to be the branch destination block and a
00366   ///    list of operands that evaluate the condition. These operands can be
00367   ///    passed to other TargetInstrInfo methods to create new branches.
00368   /// 4. If this block ends with a conditional branch followed by an
00369   ///    unconditional branch, it returns the 'true' destination in TBB, the
00370   ///    'false' destination in FBB, and a list of operands that evaluate the
00371   ///    condition.  These operands can be passed to other TargetInstrInfo
00372   ///    methods to create new branches.
00373   ///
00374   /// Note that RemoveBranch and InsertBranch must be implemented to support
00375   /// cases where this method returns success.
00376   ///
00377   /// If AllowModify is true, then this routine is allowed to modify the basic
00378   /// block (e.g. delete instructions after the unconditional branch).
00379   ///
00380   virtual bool AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
00381                              MachineBasicBlock *&FBB,
00382                              SmallVectorImpl<MachineOperand> &Cond,
00383                              bool AllowModify = false) const {
00384     return true;
00385   }
00386 
00387   /// RemoveBranch - Remove the branching code at the end of the specific MBB.
00388   /// This is only invoked in cases where AnalyzeBranch returns success. It
00389   /// returns the number of instructions that were removed.
00390   virtual unsigned RemoveBranch(MachineBasicBlock &MBB) const {
00391     llvm_unreachable("Target didn't implement TargetInstrInfo::RemoveBranch!");
00392   }
00393 
00394   /// InsertBranch - Insert branch code into the end of the specified
00395   /// MachineBasicBlock.  The operands to this method are the same as those
00396   /// returned by AnalyzeBranch.  This is only invoked in cases where
00397   /// AnalyzeBranch returns success. It returns the number of instructions
00398   /// inserted.
00399   ///
00400   /// It is also invoked by tail merging to add unconditional branches in
00401   /// cases where AnalyzeBranch doesn't apply because there was no original
00402   /// branch to analyze.  At least this much must be implemented, else tail
00403   /// merging needs to be disabled.
00404   virtual unsigned InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
00405                                 MachineBasicBlock *FBB,
00406                                 const SmallVectorImpl<MachineOperand> &Cond,
00407                                 DebugLoc DL) const {
00408     llvm_unreachable("Target didn't implement TargetInstrInfo::InsertBranch!");
00409   }
00410 
00411   /// ReplaceTailWithBranchTo - Delete the instruction OldInst and everything
00412   /// after it, replacing it with an unconditional branch to NewDest. This is
00413   /// used by the tail merging pass.
00414   virtual void ReplaceTailWithBranchTo(MachineBasicBlock::iterator Tail,
00415                                        MachineBasicBlock *NewDest) const;
00416 
00417   /// getUnconditionalBranch - Get an instruction that performs an unconditional
00418   /// branch to the given symbol.
00419   virtual void
00420   getUnconditionalBranch(MCInst &MI,
00421                          const MCSymbolRefExpr *BranchTarget) const {
00422     llvm_unreachable("Target didn't implement "
00423                      "TargetInstrInfo::getUnconditionalBranch!");
00424   }
00425 
00426   /// getTrap - Get a machine trap instruction
00427   virtual void getTrap(MCInst &MI) const {
00428     llvm_unreachable("Target didn't implement TargetInstrInfo::getTrap!");
00429   }
00430 
00431   /// isLegalToSplitMBBAt - Return true if it's legal to split the given basic
00432   /// block at the specified instruction (i.e. instruction would be the start
00433   /// of a new basic block).
00434   virtual bool isLegalToSplitMBBAt(MachineBasicBlock &MBB,
00435                                    MachineBasicBlock::iterator MBBI) const {
00436     return true;
00437   }
00438 
00439   /// isProfitableToIfCvt - Return true if it's profitable to predicate
00440   /// instructions with accumulated instruction latency of "NumCycles"
00441   /// of the specified basic block, where the probability of the instructions
00442   /// being executed is given by Probability, and Confidence is a measure
00443   /// of our confidence that it will be properly predicted.
00444   virtual
00445   bool isProfitableToIfCvt(MachineBasicBlock &MBB, unsigned NumCycles,
00446                            unsigned ExtraPredCycles,
00447                            const BranchProbability &Probability) const {
00448     return false;
00449   }
00450 
00451   /// isProfitableToIfCvt - Second variant of isProfitableToIfCvt, this one
00452   /// checks for the case where two basic blocks from true and false path
00453   /// of a if-then-else (diamond) are predicated on mutally exclusive
00454   /// predicates, where the probability of the true path being taken is given
00455   /// by Probability, and Confidence is a measure of our confidence that it
00456   /// will be properly predicted.
00457   virtual bool
00458   isProfitableToIfCvt(MachineBasicBlock &TMBB,
00459                       unsigned NumTCycles, unsigned ExtraTCycles,
00460                       MachineBasicBlock &FMBB,
00461                       unsigned NumFCycles, unsigned ExtraFCycles,
00462                       const BranchProbability &Probability) const {
00463     return false;
00464   }
00465 
00466   /// isProfitableToDupForIfCvt - Return true if it's profitable for
00467   /// if-converter to duplicate instructions of specified accumulated
00468   /// instruction latencies in the specified MBB to enable if-conversion.
00469   /// The probability of the instructions being executed is given by
00470   /// Probability, and Confidence is a measure of our confidence that it
00471   /// will be properly predicted.
00472   virtual bool
00473   isProfitableToDupForIfCvt(MachineBasicBlock &MBB, unsigned NumCycles,
00474                             const BranchProbability &Probability) const {
00475     return false;
00476   }
00477 
00478   /// isProfitableToUnpredicate - Return true if it's profitable to unpredicate
00479   /// one side of a 'diamond', i.e. two sides of if-else predicated on mutually
00480   /// exclusive predicates.
00481   /// e.g.
00482   ///   subeq  r0, r1, #1
00483   ///   addne  r0, r1, #1
00484   /// =>
00485   ///   sub    r0, r1, #1
00486   ///   addne  r0, r1, #1
00487   ///
00488   /// This may be profitable is conditional instructions are always executed.
00489   virtual bool isProfitableToUnpredicate(MachineBasicBlock &TMBB,
00490                                          MachineBasicBlock &FMBB) const {
00491     return false;
00492   }
00493 
00494   /// canInsertSelect - Return true if it is possible to insert a select
00495   /// instruction that chooses between TrueReg and FalseReg based on the
00496   /// condition code in Cond.
00497   ///
00498   /// When successful, also return the latency in cycles from TrueReg,
00499   /// FalseReg, and Cond to the destination register. In most cases, a select
00500   /// instruction will be 1 cycle, so CondCycles = TrueCycles = FalseCycles = 1
00501   ///
00502   /// Some x86 implementations have 2-cycle cmov instructions.
00503   ///
00504   /// @param MBB         Block where select instruction would be inserted.
00505   /// @param Cond        Condition returned by AnalyzeBranch.
00506   /// @param TrueReg     Virtual register to select when Cond is true.
00507   /// @param FalseReg    Virtual register to select when Cond is false.
00508   /// @param CondCycles  Latency from Cond+Branch to select output.
00509   /// @param TrueCycles  Latency from TrueReg to select output.
00510   /// @param FalseCycles Latency from FalseReg to select output.
00511   virtual bool canInsertSelect(const MachineBasicBlock &MBB,
00512                                const SmallVectorImpl<MachineOperand> &Cond,
00513                                unsigned TrueReg, unsigned FalseReg,
00514                                int &CondCycles,
00515                                int &TrueCycles, int &FalseCycles) const {
00516     return false;
00517   }
00518 
00519   /// insertSelect - Insert a select instruction into MBB before I that will
00520   /// copy TrueReg to DstReg when Cond is true, and FalseReg to DstReg when
00521   /// Cond is false.
00522   ///
00523   /// This function can only be called after canInsertSelect() returned true.
00524   /// The condition in Cond comes from AnalyzeBranch, and it can be assumed
00525   /// that the same flags or registers required by Cond are available at the
00526   /// insertion point.
00527   ///
00528   /// @param MBB      Block where select instruction should be inserted.
00529   /// @param I        Insertion point.
00530   /// @param DL       Source location for debugging.
00531   /// @param DstReg   Virtual register to be defined by select instruction.
00532   /// @param Cond     Condition as computed by AnalyzeBranch.
00533   /// @param TrueReg  Virtual register to copy when Cond is true.
00534   /// @param FalseReg Virtual register to copy when Cons is false.
00535   virtual void insertSelect(MachineBasicBlock &MBB,
00536                             MachineBasicBlock::iterator I, DebugLoc DL,
00537                             unsigned DstReg,
00538                             const SmallVectorImpl<MachineOperand> &Cond,
00539                             unsigned TrueReg, unsigned FalseReg) const {
00540     llvm_unreachable("Target didn't implement TargetInstrInfo::insertSelect!");
00541   }
00542 
00543   /// analyzeSelect - Analyze the given select instruction, returning true if
00544   /// it cannot be understood. It is assumed that MI->isSelect() is true.
00545   ///
00546   /// When successful, return the controlling condition and the operands that
00547   /// determine the true and false result values.
00548   ///
00549   ///   Result = SELECT Cond, TrueOp, FalseOp
00550   ///
00551   /// Some targets can optimize select instructions, for example by predicating
00552   /// the instruction defining one of the operands. Such targets should set
00553   /// Optimizable.
00554   ///
00555   /// @param         MI Select instruction to analyze.
00556   /// @param Cond    Condition controlling the select.
00557   /// @param TrueOp  Operand number of the value selected when Cond is true.
00558   /// @param FalseOp Operand number of the value selected when Cond is false.
00559   /// @param Optimizable Returned as true if MI is optimizable.
00560   /// @returns False on success.
00561   virtual bool analyzeSelect(const MachineInstr *MI,
00562                              SmallVectorImpl<MachineOperand> &Cond,
00563                              unsigned &TrueOp, unsigned &FalseOp,
00564                              bool &Optimizable) const {
00565     assert(MI && MI->getDesc().isSelect() && "MI must be a select instruction");
00566     return true;
00567   }
00568 
00569   /// optimizeSelect - Given a select instruction that was understood by
00570   /// analyzeSelect and returned Optimizable = true, attempt to optimize MI by
00571   /// merging it with one of its operands. Returns NULL on failure.
00572   ///
00573   /// When successful, returns the new select instruction. The client is
00574   /// responsible for deleting MI.
00575   ///
00576   /// If both sides of the select can be optimized, PreferFalse is used to pick
00577   /// a side.
00578   ///
00579   /// @param MI          Optimizable select instruction.
00580   /// @param PreferFalse Try to optimize FalseOp instead of TrueOp.
00581   /// @returns Optimized instruction or NULL.
00582   virtual MachineInstr *optimizeSelect(MachineInstr *MI,
00583                                        bool PreferFalse = false) const {
00584     // This function must be implemented if Optimizable is ever set.
00585     llvm_unreachable("Target must implement TargetInstrInfo::optimizeSelect!");
00586   }
00587 
00588   /// copyPhysReg - Emit instructions to copy a pair of physical registers.
00589   ///
00590   /// This function should support copies within any legal register class as
00591   /// well as any cross-class copies created during instruction selection.
00592   ///
00593   /// The source and destination registers may overlap, which may require a
00594   /// careful implementation when multiple copy instructions are required for
00595   /// large registers. See for example the ARM target.
00596   virtual void copyPhysReg(MachineBasicBlock &MBB,
00597                            MachineBasicBlock::iterator MI, DebugLoc DL,
00598                            unsigned DestReg, unsigned SrcReg,
00599                            bool KillSrc) const {
00600     llvm_unreachable("Target didn't implement TargetInstrInfo::copyPhysReg!");
00601   }
00602 
00603   /// storeRegToStackSlot - Store the specified register of the given register
00604   /// class to the specified stack frame index. The store instruction is to be
00605   /// added to the given machine basic block before the specified machine
00606   /// instruction. If isKill is true, the register operand is the last use and
00607   /// must be marked kill.
00608   virtual void storeRegToStackSlot(MachineBasicBlock &MBB,
00609                                    MachineBasicBlock::iterator MI,
00610                                    unsigned SrcReg, bool isKill, int FrameIndex,
00611                                    const TargetRegisterClass *RC,
00612                                    const TargetRegisterInfo *TRI) const {
00613     llvm_unreachable("Target didn't implement "
00614                      "TargetInstrInfo::storeRegToStackSlot!");
00615   }
00616 
00617   /// loadRegFromStackSlot - Load the specified register of the given register
00618   /// class from the specified stack frame index. The load instruction is to be
00619   /// added to the given machine basic block before the specified machine
00620   /// instruction.
00621   virtual void loadRegFromStackSlot(MachineBasicBlock &MBB,
00622                                     MachineBasicBlock::iterator MI,
00623                                     unsigned DestReg, int FrameIndex,
00624                                     const TargetRegisterClass *RC,
00625                                     const TargetRegisterInfo *TRI) const {
00626     llvm_unreachable("Target didn't implement "
00627                      "TargetInstrInfo::loadRegFromStackSlot!");
00628   }
00629 
00630   /// expandPostRAPseudo - This function is called for all pseudo instructions
00631   /// that remain after register allocation. Many pseudo instructions are
00632   /// created to help register allocation. This is the place to convert them
00633   /// into real instructions. The target can edit MI in place, or it can insert
00634   /// new instructions and erase MI. The function should return true if
00635   /// anything was changed.
00636   virtual bool expandPostRAPseudo(MachineBasicBlock::iterator MI) const {
00637     return false;
00638   }
00639 
00640   /// foldMemoryOperand - Attempt to fold a load or store of the specified stack
00641   /// slot into the specified machine instruction for the specified operand(s).
00642   /// If this is possible, a new instruction is returned with the specified
00643   /// operand folded, otherwise NULL is returned.
00644   /// The new instruction is inserted before MI, and the client is responsible
00645   /// for removing the old instruction.
00646   MachineInstr* foldMemoryOperand(MachineBasicBlock::iterator MI,
00647                                   const SmallVectorImpl<unsigned> &Ops,
00648                                   int FrameIndex) const;
00649 
00650   /// foldMemoryOperand - Same as the previous version except it allows folding
00651   /// of any load and store from / to any address, not just from a specific
00652   /// stack slot.
00653   MachineInstr* foldMemoryOperand(MachineBasicBlock::iterator MI,
00654                                   const SmallVectorImpl<unsigned> &Ops,
00655                                   MachineInstr* LoadMI) const;
00656 
00657   /// hasPattern - return true when there is potentially a faster code sequence
00658   /// for an instruction chain ending in \p Root. All potential pattern are
00659   /// returned in the \p Pattern vector. Pattern should be sorted in priority
00660   /// order since the pattern evaluator stops checking as soon as it finds a
00661   /// faster sequence.
00662   /// \param Root - Instruction that could be combined with one of its operands
00663   /// \param Pattern - Vector of possible combination pattern
00664 
00665   virtual bool hasPattern(
00666       MachineInstr &Root,
00667       SmallVectorImpl<MachineCombinerPattern::MC_PATTERN> &Pattern) const {
00668     return false;
00669   }
00670 
00671   /// genAlternativeCodeSequence - when hasPattern() finds a pattern this
00672   /// function generates the instructions that could replace the original code
00673   /// sequence. The client has to decide whether the actual replacementment is
00674   /// beneficial or not.
00675   /// \param Root - Instruction that could be combined with one of its operands
00676   /// \param P - Combination pattern for Root
00677   /// \param InsInstrs - Vector of new instructions that implement P
00678   /// \param DelInstrs - Old instructions, including Root, that could be replaced
00679   /// by InsInstr
00680   /// \param InstrIdxForVirtReg - map of virtual register to instruction in
00681   /// InsInstr that defines it
00682   virtual void genAlternativeCodeSequence(
00683       MachineInstr &Root, MachineCombinerPattern::MC_PATTERN P,
00684       SmallVectorImpl<MachineInstr *> &InsInstrs,
00685       SmallVectorImpl<MachineInstr *> &DelInstrs,
00686       DenseMap<unsigned, unsigned> &InstrIdxForVirtReg) const {
00687     return;
00688   }
00689 
00690   /// useMachineCombiner - return true when a target supports MachineCombiner
00691   virtual bool useMachineCombiner() const { return false; }
00692 
00693 protected:
00694   /// foldMemoryOperandImpl - Target-dependent implementation for
00695   /// foldMemoryOperand. Target-independent code in foldMemoryOperand will
00696   /// take care of adding a MachineMemOperand to the newly created instruction.
00697   virtual MachineInstr* foldMemoryOperandImpl(MachineFunction &MF,
00698                                           MachineInstr* MI,
00699                                           const SmallVectorImpl<unsigned> &Ops,
00700                                           int FrameIndex) const {
00701     return nullptr;
00702   }
00703 
00704   /// foldMemoryOperandImpl - Target-dependent implementation for
00705   /// foldMemoryOperand. Target-independent code in foldMemoryOperand will
00706   /// take care of adding a MachineMemOperand to the newly created instruction.
00707   virtual MachineInstr* foldMemoryOperandImpl(MachineFunction &MF,
00708                                               MachineInstr* MI,
00709                                           const SmallVectorImpl<unsigned> &Ops,
00710                                               MachineInstr* LoadMI) const {
00711     return nullptr;
00712   }
00713 
00714   /// \brief Target-dependent implementation of getRegSequenceInputs.
00715   ///
00716   /// \returns true if it is possible to build the equivalent
00717   /// REG_SEQUENCE inputs with the pair \p MI, \p DefIdx. False otherwise.
00718   ///
00719   /// \pre MI.isRegSequenceLike().
00720   ///
00721   /// \see TargetInstrInfo::getRegSequenceInputs.
00722   virtual bool getRegSequenceLikeInputs(
00723       const MachineInstr &MI, unsigned DefIdx,
00724       SmallVectorImpl<RegSubRegPairAndIdx> &InputRegs) const {
00725     return false;
00726   }
00727 
00728   /// \brief Target-dependent implementation of getExtractSubregInputs.
00729   ///
00730   /// \returns true if it is possible to build the equivalent
00731   /// EXTRACT_SUBREG inputs with the pair \p MI, \p DefIdx. False otherwise.
00732   ///
00733   /// \pre MI.isExtractSubregLike().
00734   ///
00735   /// \see TargetInstrInfo::getExtractSubregInputs.
00736   virtual bool getExtractSubregLikeInputs(
00737       const MachineInstr &MI, unsigned DefIdx,
00738       RegSubRegPairAndIdx &InputReg) const {
00739     return false;
00740   }
00741 
00742   /// \brief Target-dependent implementation of getInsertSubregInputs.
00743   ///
00744   /// \returns true if it is possible to build the equivalent
00745   /// INSERT_SUBREG inputs with the pair \p MI, \p DefIdx. False otherwise.
00746   ///
00747   /// \pre MI.isInsertSubregLike().
00748   ///
00749   /// \see TargetInstrInfo::getInsertSubregInputs.
00750   virtual bool
00751   getInsertSubregLikeInputs(const MachineInstr &MI, unsigned DefIdx,
00752                             RegSubRegPair &BaseReg,
00753                             RegSubRegPairAndIdx &InsertedReg) const {
00754     return false;
00755   }
00756 
00757 public:
00758   /// canFoldMemoryOperand - Returns true for the specified load / store if
00759   /// folding is possible.
00760   virtual
00761   bool canFoldMemoryOperand(const MachineInstr *MI,
00762                             const SmallVectorImpl<unsigned> &Ops) const;
00763 
00764   /// unfoldMemoryOperand - Separate a single instruction which folded a load or
00765   /// a store or a load and a store into two or more instruction. If this is
00766   /// possible, returns true as well as the new instructions by reference.
00767   virtual bool unfoldMemoryOperand(MachineFunction &MF, MachineInstr *MI,
00768                                 unsigned Reg, bool UnfoldLoad, bool UnfoldStore,
00769                                  SmallVectorImpl<MachineInstr*> &NewMIs) const{
00770     return false;
00771   }
00772 
00773   virtual bool unfoldMemoryOperand(SelectionDAG &DAG, SDNode *N,
00774                                    SmallVectorImpl<SDNode*> &NewNodes) const {
00775     return false;
00776   }
00777 
00778   /// getOpcodeAfterMemoryUnfold - Returns the opcode of the would be new
00779   /// instruction after load / store are unfolded from an instruction of the
00780   /// specified opcode. It returns zero if the specified unfolding is not
00781   /// possible. If LoadRegIndex is non-null, it is filled in with the operand
00782   /// index of the operand which will hold the register holding the loaded
00783   /// value.
00784   virtual unsigned getOpcodeAfterMemoryUnfold(unsigned Opc,
00785                                       bool UnfoldLoad, bool UnfoldStore,
00786                                       unsigned *LoadRegIndex = nullptr) const {
00787     return 0;
00788   }
00789 
00790   /// areLoadsFromSameBasePtr - This is used by the pre-regalloc scheduler
00791   /// to determine if two loads are loading from the same base address. It
00792   /// should only return true if the base pointers are the same and the
00793   /// only differences between the two addresses are the offset. It also returns
00794   /// the offsets by reference.
00795   virtual bool areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2,
00796                                     int64_t &Offset1, int64_t &Offset2) const {
00797     return false;
00798   }
00799 
00800   /// shouldScheduleLoadsNear - This is a used by the pre-regalloc scheduler to
00801   /// determine (in conjunction with areLoadsFromSameBasePtr) if two loads should
00802   /// be scheduled togther. On some targets if two loads are loading from
00803   /// addresses in the same cache line, it's better if they are scheduled
00804   /// together. This function takes two integers that represent the load offsets
00805   /// from the common base address. It returns true if it decides it's desirable
00806   /// to schedule the two loads together. "NumLoads" is the number of loads that
00807   /// have already been scheduled after Load1.
00808   virtual bool shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
00809                                        int64_t Offset1, int64_t Offset2,
00810                                        unsigned NumLoads) const {
00811     return false;
00812   }
00813 
00814   /// \brief Get the base register and byte offset of a load/store instr.
00815   virtual bool getLdStBaseRegImmOfs(MachineInstr *LdSt,
00816                                     unsigned &BaseReg, unsigned &Offset,
00817                                     const TargetRegisterInfo *TRI) const {
00818     return false;
00819   }
00820 
00821   virtual bool enableClusterLoads() const { return false; }
00822 
00823   virtual bool shouldClusterLoads(MachineInstr *FirstLdSt,
00824                                   MachineInstr *SecondLdSt,
00825                                   unsigned NumLoads) const {
00826     return false;
00827   }
00828 
00829   /// \brief Can this target fuse the given instructions if they are scheduled
00830   /// adjacent.
00831   virtual bool shouldScheduleAdjacent(MachineInstr* First,
00832                                       MachineInstr *Second) const {
00833     return false;
00834   }
00835 
00836   /// ReverseBranchCondition - Reverses the branch condition of the specified
00837   /// condition list, returning false on success and true if it cannot be
00838   /// reversed.
00839   virtual
00840   bool ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
00841     return true;
00842   }
00843 
00844   /// insertNoop - Insert a noop into the instruction stream at the specified
00845   /// point.
00846   virtual void insertNoop(MachineBasicBlock &MBB,
00847                           MachineBasicBlock::iterator MI) const;
00848 
00849 
00850   /// Return the noop instruction to use for a noop.
00851   virtual void getNoopForMachoTarget(MCInst &NopInst) const;
00852 
00853 
00854   /// isPredicated - Returns true if the instruction is already predicated.
00855   ///
00856   virtual bool isPredicated(const MachineInstr *MI) const {
00857     return false;
00858   }
00859 
00860   /// isUnpredicatedTerminator - Returns true if the instruction is a
00861   /// terminator instruction that has not been predicated.
00862   virtual bool isUnpredicatedTerminator(const MachineInstr *MI) const;
00863 
00864   /// PredicateInstruction - Convert the instruction into a predicated
00865   /// instruction. It returns true if the operation was successful.
00866   virtual
00867   bool PredicateInstruction(MachineInstr *MI,
00868                         const SmallVectorImpl<MachineOperand> &Pred) const;
00869 
00870   /// SubsumesPredicate - Returns true if the first specified predicate
00871   /// subsumes the second, e.g. GE subsumes GT.
00872   virtual
00873   bool SubsumesPredicate(const SmallVectorImpl<MachineOperand> &Pred1,
00874                          const SmallVectorImpl<MachineOperand> &Pred2) const {
00875     return false;
00876   }
00877 
00878   /// DefinesPredicate - If the specified instruction defines any predicate
00879   /// or condition code register(s) used for predication, returns true as well
00880   /// as the definition predicate(s) by reference.
00881   virtual bool DefinesPredicate(MachineInstr *MI,
00882                                 std::vector<MachineOperand> &Pred) const {
00883     return false;
00884   }
00885 
00886   /// isPredicable - Return true if the specified instruction can be predicated.
00887   /// By default, this returns true for every instruction with a
00888   /// PredicateOperand.
00889   virtual bool isPredicable(MachineInstr *MI) const {
00890     return MI->getDesc().isPredicable();
00891   }
00892 
00893   /// isSafeToMoveRegClassDefs - Return true if it's safe to move a machine
00894   /// instruction that defines the specified register class.
00895   virtual bool isSafeToMoveRegClassDefs(const TargetRegisterClass *RC) const {
00896     return true;
00897   }
00898 
00899   /// isSchedulingBoundary - Test if the given instruction should be
00900   /// considered a scheduling boundary. This primarily includes labels and
00901   /// terminators.
00902   virtual bool isSchedulingBoundary(const MachineInstr *MI,
00903                                     const MachineBasicBlock *MBB,
00904                                     const MachineFunction &MF) const;
00905 
00906   /// Measure the specified inline asm to determine an approximation of its
00907   /// length.
00908   virtual unsigned getInlineAsmLength(const char *Str,
00909                                       const MCAsmInfo &MAI) const;
00910 
00911   /// CreateTargetHazardRecognizer - Allocate and return a hazard recognizer to
00912   /// use for this target when scheduling the machine instructions before
00913   /// register allocation.
00914   virtual ScheduleHazardRecognizer*
00915   CreateTargetHazardRecognizer(const TargetSubtargetInfo *STI,
00916                                const ScheduleDAG *DAG) const;
00917 
00918   /// CreateTargetMIHazardRecognizer - Allocate and return a hazard recognizer
00919   /// to use for this target when scheduling the machine instructions before
00920   /// register allocation.
00921   virtual ScheduleHazardRecognizer*
00922   CreateTargetMIHazardRecognizer(const InstrItineraryData*,
00923                                  const ScheduleDAG *DAG) const;
00924 
00925   /// CreateTargetPostRAHazardRecognizer - Allocate and return a hazard
00926   /// recognizer to use for this target when scheduling the machine instructions
00927   /// after register allocation.
00928   virtual ScheduleHazardRecognizer*
00929   CreateTargetPostRAHazardRecognizer(const InstrItineraryData*,
00930                                      const ScheduleDAG *DAG) const;
00931 
00932   /// Provide a global flag for disabling the PreRA hazard recognizer that
00933   /// targets may choose to honor.
00934   bool usePreRAHazardRecognizer() const;
00935 
00936   /// analyzeCompare - For a comparison instruction, return the source registers
00937   /// in SrcReg and SrcReg2 if having two register operands, and the value it
00938   /// compares against in CmpValue. Return true if the comparison instruction
00939   /// can be analyzed.
00940   virtual bool analyzeCompare(const MachineInstr *MI,
00941                               unsigned &SrcReg, unsigned &SrcReg2,
00942                               int &Mask, int &Value) const {
00943     return false;
00944   }
00945 
00946   /// optimizeCompareInstr - See if the comparison instruction can be converted
00947   /// into something more efficient. E.g., on ARM most instructions can set the
00948   /// flags register, obviating the need for a separate CMP.
00949   virtual bool optimizeCompareInstr(MachineInstr *CmpInstr,
00950                                     unsigned SrcReg, unsigned SrcReg2,
00951                                     int Mask, int Value,
00952                                     const MachineRegisterInfo *MRI) const {
00953     return false;
00954   }
00955 
00956   /// optimizeLoadInstr - Try to remove the load by folding it to a register
00957   /// operand at the use. We fold the load instructions if and only if the
00958   /// def and use are in the same BB. We only look at one load and see
00959   /// whether it can be folded into MI. FoldAsLoadDefReg is the virtual register
00960   /// defined by the load we are trying to fold. DefMI returns the machine
00961   /// instruction that defines FoldAsLoadDefReg, and the function returns
00962   /// the machine instruction generated due to folding.
00963   virtual MachineInstr* optimizeLoadInstr(MachineInstr *MI,
00964                         const MachineRegisterInfo *MRI,
00965                         unsigned &FoldAsLoadDefReg,
00966                         MachineInstr *&DefMI) const {
00967     return nullptr;
00968   }
00969 
00970   /// FoldImmediate - 'Reg' is known to be defined by a move immediate
00971   /// instruction, try to fold the immediate into the use instruction.
00972   /// If MRI->hasOneNonDBGUse(Reg) is true, and this function returns true,
00973   /// then the caller may assume that DefMI has been erased from its parent
00974   /// block. The caller may assume that it will not be erased by this
00975   /// function otherwise.
00976   virtual bool FoldImmediate(MachineInstr *UseMI, MachineInstr *DefMI,
00977                              unsigned Reg, MachineRegisterInfo *MRI) const {
00978     return false;
00979   }
00980 
00981   /// getNumMicroOps - Return the number of u-operations the given machine
00982   /// instruction will be decoded to on the target cpu. The itinerary's
00983   /// IssueWidth is the number of microops that can be dispatched each
00984   /// cycle. An instruction with zero microops takes no dispatch resources.
00985   virtual unsigned getNumMicroOps(const InstrItineraryData *ItinData,
00986                                   const MachineInstr *MI) const;
00987 
00988   /// isZeroCost - Return true for pseudo instructions that don't consume any
00989   /// machine resources in their current form. These are common cases that the
00990   /// scheduler should consider free, rather than conservatively handling them
00991   /// as instructions with no itinerary.
00992   bool isZeroCost(unsigned Opcode) const {
00993     return Opcode <= TargetOpcode::COPY;
00994   }
00995 
00996   virtual int getOperandLatency(const InstrItineraryData *ItinData,
00997                                 SDNode *DefNode, unsigned DefIdx,
00998                                 SDNode *UseNode, unsigned UseIdx) const;
00999 
01000   /// getOperandLatency - Compute and return the use operand latency of a given
01001   /// pair of def and use.
01002   /// In most cases, the static scheduling itinerary was enough to determine the
01003   /// operand latency. But it may not be possible for instructions with variable
01004   /// number of defs / uses.
01005   ///
01006   /// This is a raw interface to the itinerary that may be directly overriden by
01007   /// a target. Use computeOperandLatency to get the best estimate of latency.
01008   virtual int getOperandLatency(const InstrItineraryData *ItinData,
01009                                 const MachineInstr *DefMI, unsigned DefIdx,
01010                                 const MachineInstr *UseMI,
01011                                 unsigned UseIdx) const;
01012 
01013   /// computeOperandLatency - Compute and return the latency of the given data
01014   /// dependent def and use when the operand indices are already known.
01015   unsigned computeOperandLatency(const InstrItineraryData *ItinData,
01016                                  const MachineInstr *DefMI, unsigned DefIdx,
01017                                  const MachineInstr *UseMI, unsigned UseIdx)
01018     const;
01019 
01020   /// getInstrLatency - Compute the instruction latency of a given instruction.
01021   /// If the instruction has higher cost when predicated, it's returned via
01022   /// PredCost.
01023   virtual unsigned getInstrLatency(const InstrItineraryData *ItinData,
01024                                    const MachineInstr *MI,
01025                                    unsigned *PredCost = nullptr) const;
01026 
01027   virtual unsigned getPredicationCost(const MachineInstr *MI) const;
01028 
01029   virtual int getInstrLatency(const InstrItineraryData *ItinData,
01030                               SDNode *Node) const;
01031 
01032   /// Return the default expected latency for a def based on it's opcode.
01033   unsigned defaultDefLatency(const MCSchedModel &SchedModel,
01034                              const MachineInstr *DefMI) const;
01035 
01036   int computeDefOperandLatency(const InstrItineraryData *ItinData,
01037                                const MachineInstr *DefMI) const;
01038 
01039   /// isHighLatencyDef - Return true if this opcode has high latency to its
01040   /// result.
01041   virtual bool isHighLatencyDef(int opc) const { return false; }
01042 
01043   /// hasHighOperandLatency - Compute operand latency between a def of 'Reg'
01044   /// and an use in the current loop, return true if the target considered
01045   /// it 'high'. This is used by optimization passes such as machine LICM to
01046   /// determine whether it makes sense to hoist an instruction out even in
01047   /// high register pressure situation.
01048   virtual
01049   bool hasHighOperandLatency(const InstrItineraryData *ItinData,
01050                              const MachineRegisterInfo *MRI,
01051                              const MachineInstr *DefMI, unsigned DefIdx,
01052                              const MachineInstr *UseMI, unsigned UseIdx) const {
01053     return false;
01054   }
01055 
01056   /// hasLowDefLatency - Compute operand latency of a def of 'Reg', return true
01057   /// if the target considered it 'low'.
01058   virtual
01059   bool hasLowDefLatency(const InstrItineraryData *ItinData,
01060                         const MachineInstr *DefMI, unsigned DefIdx) const;
01061 
01062   /// verifyInstruction - Perform target specific instruction verification.
01063   virtual
01064   bool verifyInstruction(const MachineInstr *MI, StringRef &ErrInfo) const {
01065     return true;
01066   }
01067 
01068   /// getExecutionDomain - Return the current execution domain and bit mask of
01069   /// possible domains for instruction.
01070   ///
01071   /// Some micro-architectures have multiple execution domains, and multiple
01072   /// opcodes that perform the same operation in different domains.  For
01073   /// example, the x86 architecture provides the por, orps, and orpd
01074   /// instructions that all do the same thing.  There is a latency penalty if a
01075   /// register is written in one domain and read in another.
01076   ///
01077   /// This function returns a pair (domain, mask) containing the execution
01078   /// domain of MI, and a bit mask of possible domains.  The setExecutionDomain
01079   /// function can be used to change the opcode to one of the domains in the
01080   /// bit mask.  Instructions whose execution domain can't be changed should
01081   /// return a 0 mask.
01082   ///
01083   /// The execution domain numbers don't have any special meaning except domain
01084   /// 0 is used for instructions that are not associated with any interesting
01085   /// execution domain.
01086   ///
01087   virtual std::pair<uint16_t, uint16_t>
01088   getExecutionDomain(const MachineInstr *MI) const {
01089     return std::make_pair(0, 0);
01090   }
01091 
01092   /// setExecutionDomain - Change the opcode of MI to execute in Domain.
01093   ///
01094   /// The bit (1 << Domain) must be set in the mask returned from
01095   /// getExecutionDomain(MI).
01096   ///
01097   virtual void setExecutionDomain(MachineInstr *MI, unsigned Domain) const {}
01098 
01099 
01100   /// getPartialRegUpdateClearance - Returns the preferred minimum clearance
01101   /// before an instruction with an unwanted partial register update.
01102   ///
01103   /// Some instructions only write part of a register, and implicitly need to
01104   /// read the other parts of the register.  This may cause unwanted stalls
01105   /// preventing otherwise unrelated instructions from executing in parallel in
01106   /// an out-of-order CPU.
01107   ///
01108   /// For example, the x86 instruction cvtsi2ss writes its result to bits
01109   /// [31:0] of the destination xmm register. Bits [127:32] are unaffected, so
01110   /// the instruction needs to wait for the old value of the register to become
01111   /// available:
01112   ///
01113   ///   addps %xmm1, %xmm0
01114   ///   movaps %xmm0, (%rax)
01115   ///   cvtsi2ss %rbx, %xmm0
01116   ///
01117   /// In the code above, the cvtsi2ss instruction needs to wait for the addps
01118   /// instruction before it can issue, even though the high bits of %xmm0
01119   /// probably aren't needed.
01120   ///
01121   /// This hook returns the preferred clearance before MI, measured in
01122   /// instructions.  Other defs of MI's operand OpNum are avoided in the last N
01123   /// instructions before MI.  It should only return a positive value for
01124   /// unwanted dependencies.  If the old bits of the defined register have
01125   /// useful values, or if MI is determined to otherwise read the dependency,
01126   /// the hook should return 0.
01127   ///
01128   /// The unwanted dependency may be handled by:
01129   ///
01130   /// 1. Allocating the same register for an MI def and use.  That makes the
01131   ///    unwanted dependency identical to a required dependency.
01132   ///
01133   /// 2. Allocating a register for the def that has no defs in the previous N
01134   ///    instructions.
01135   ///
01136   /// 3. Calling breakPartialRegDependency() with the same arguments.  This
01137   ///    allows the target to insert a dependency breaking instruction.
01138   ///
01139   virtual unsigned
01140   getPartialRegUpdateClearance(const MachineInstr *MI, unsigned OpNum,
01141                                const TargetRegisterInfo *TRI) const {
01142     // The default implementation returns 0 for no partial register dependency.
01143     return 0;
01144   }
01145 
01146   /// \brief Return the minimum clearance before an instruction that reads an
01147   /// unused register.
01148   ///
01149   /// For example, AVX instructions may copy part of an register operand into
01150   /// the unused high bits of the destination register.
01151   ///
01152   /// vcvtsi2sdq %rax, %xmm0<undef>, %xmm14
01153   ///
01154   /// In the code above, vcvtsi2sdq copies %xmm0[127:64] into %xmm14 creating a
01155   /// false dependence on any previous write to %xmm0.
01156   ///
01157   /// This hook works similarly to getPartialRegUpdateClearance, except that it
01158   /// does not take an operand index. Instead sets \p OpNum to the index of the
01159   /// unused register.
01160   virtual unsigned getUndefRegClearance(const MachineInstr *MI, unsigned &OpNum,
01161                                         const TargetRegisterInfo *TRI) const {
01162     // The default implementation returns 0 for no undef register dependency.
01163     return 0;
01164   }
01165 
01166   /// breakPartialRegDependency - Insert a dependency-breaking instruction
01167   /// before MI to eliminate an unwanted dependency on OpNum.
01168   ///
01169   /// If it wasn't possible to avoid a def in the last N instructions before MI
01170   /// (see getPartialRegUpdateClearance), this hook will be called to break the
01171   /// unwanted dependency.
01172   ///
01173   /// On x86, an xorps instruction can be used as a dependency breaker:
01174   ///
01175   ///   addps %xmm1, %xmm0
01176   ///   movaps %xmm0, (%rax)
01177   ///   xorps %xmm0, %xmm0
01178   ///   cvtsi2ss %rbx, %xmm0
01179   ///
01180   /// An <imp-kill> operand should be added to MI if an instruction was
01181   /// inserted.  This ties the instructions together in the post-ra scheduler.
01182   ///
01183   virtual void
01184   breakPartialRegDependency(MachineBasicBlock::iterator MI, unsigned OpNum,
01185                             const TargetRegisterInfo *TRI) const {}
01186 
01187   /// Create machine specific model for scheduling.
01188   virtual DFAPacketizer*
01189     CreateTargetScheduleState(const TargetMachine*, const ScheduleDAG*) const {
01190     return nullptr;
01191   }
01192 
01193   // areMemAccessesTriviallyDisjoint - Sometimes, it is possible for the target
01194   // to tell, even without aliasing information, that two MIs access different
01195   // memory addresses. This function returns true if two MIs access different
01196   // memory addresses, and false otherwise.
01197   virtual bool
01198   areMemAccessesTriviallyDisjoint(MachineInstr *MIa, MachineInstr *MIb,
01199                                   AliasAnalysis *AA = nullptr) const {
01200     assert(MIa && (MIa->mayLoad() || MIa->mayStore()) &&
01201            "MIa must load from or modify a memory location");
01202     assert(MIb && (MIb->mayLoad() || MIb->mayStore()) &&
01203            "MIb must load from or modify a memory location");
01204     return false;
01205   }
01206 
01207 private:
01208   int CallFrameSetupOpcode, CallFrameDestroyOpcode;
01209 };
01210 
01211 } // End llvm namespace
01212 
01213 #endif