LLVM API Documentation

X86InstrInfo.h
Go to the documentation of this file.
00001 //===-- X86InstrInfo.h - X86 Instruction Information ------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file contains the X86 implementation of the TargetInstrInfo class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_LIB_TARGET_X86_X86INSTRINFO_H
00015 #define LLVM_LIB_TARGET_X86_X86INSTRINFO_H
00016 
00017 #include "MCTargetDesc/X86BaseInfo.h"
00018 #include "X86RegisterInfo.h"
00019 #include "llvm/ADT/DenseMap.h"
00020 #include "llvm/Target/TargetInstrInfo.h"
00021 
00022 #define GET_INSTRINFO_HEADER
00023 #include "X86GenInstrInfo.inc"
00024 
00025 namespace llvm {
00026   class X86RegisterInfo;
00027   class X86Subtarget;
00028 
00029 namespace X86 {
00030   // X86 specific condition code. These correspond to X86_*_COND in
00031   // X86InstrInfo.td. They must be kept in synch.
00032   enum CondCode {
00033     COND_A  = 0,
00034     COND_AE = 1,
00035     COND_B  = 2,
00036     COND_BE = 3,
00037     COND_E  = 4,
00038     COND_G  = 5,
00039     COND_GE = 6,
00040     COND_L  = 7,
00041     COND_LE = 8,
00042     COND_NE = 9,
00043     COND_NO = 10,
00044     COND_NP = 11,
00045     COND_NS = 12,
00046     COND_O  = 13,
00047     COND_P  = 14,
00048     COND_S  = 15,
00049     LAST_VALID_COND = COND_S,
00050 
00051     // Artificial condition codes. These are used by AnalyzeBranch
00052     // to indicate a block terminated with two conditional branches to
00053     // the same location. This occurs in code using FCMP_OEQ or FCMP_UNE,
00054     // which can't be represented on x86 with a single condition. These
00055     // are never used in MachineInstrs.
00056     COND_NE_OR_P,
00057     COND_NP_OR_E,
00058 
00059     COND_INVALID
00060   };
00061 
00062   // Turn condition code into conditional branch opcode.
00063   unsigned GetCondBranchFromCond(CondCode CC);
00064 
00065   /// \brief Return a set opcode for the given condition and whether it has
00066   /// a memory operand.
00067   unsigned getSETFromCond(CondCode CC, bool HasMemoryOperand = false);
00068 
00069   /// \brief Return a cmov opcode for the given condition, register size in
00070   /// bytes, and operand type.
00071   unsigned getCMovFromCond(CondCode CC, unsigned RegBytes,
00072                            bool HasMemoryOperand = false);
00073 
00074   // Turn CMov opcode into condition code.
00075   CondCode getCondFromCMovOpc(unsigned Opc);
00076 
00077   /// GetOppositeBranchCondition - Return the inverse of the specified cond,
00078   /// e.g. turning COND_E to COND_NE.
00079   CondCode GetOppositeBranchCondition(CondCode CC);
00080 }  // end namespace X86;
00081 
00082 
00083 /// isGlobalStubReference - Return true if the specified TargetFlag operand is
00084 /// a reference to a stub for a global, not the global itself.
00085 inline static bool isGlobalStubReference(unsigned char TargetFlag) {
00086   switch (TargetFlag) {
00087   case X86II::MO_DLLIMPORT: // dllimport stub.
00088   case X86II::MO_GOTPCREL:  // rip-relative GOT reference.
00089   case X86II::MO_GOT:       // normal GOT reference.
00090   case X86II::MO_DARWIN_NONLAZY_PIC_BASE:        // Normal $non_lazy_ptr ref.
00091   case X86II::MO_DARWIN_NONLAZY:                 // Normal $non_lazy_ptr ref.
00092   case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE: // Hidden $non_lazy_ptr ref.
00093     return true;
00094   default:
00095     return false;
00096   }
00097 }
00098 
00099 /// isGlobalRelativeToPICBase - Return true if the specified global value
00100 /// reference is relative to a 32-bit PIC base (X86ISD::GlobalBaseReg).  If this
00101 /// is true, the addressing mode has the PIC base register added in (e.g. EBX).
00102 inline static bool isGlobalRelativeToPICBase(unsigned char TargetFlag) {
00103   switch (TargetFlag) {
00104   case X86II::MO_GOTOFF:                         // isPICStyleGOT: local global.
00105   case X86II::MO_GOT:                            // isPICStyleGOT: other global.
00106   case X86II::MO_PIC_BASE_OFFSET:                // Darwin local global.
00107   case X86II::MO_DARWIN_NONLAZY_PIC_BASE:        // Darwin/32 external global.
00108   case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE: // Darwin/32 hidden global.
00109   case X86II::MO_TLVP:                           // ??? Pretty sure..
00110     return true;
00111   default:
00112     return false;
00113   }
00114 }
00115 
00116 inline static bool isScale(const MachineOperand &MO) {
00117   return MO.isImm() &&
00118     (MO.getImm() == 1 || MO.getImm() == 2 ||
00119      MO.getImm() == 4 || MO.getImm() == 8);
00120 }
00121 
00122 inline static bool isLeaMem(const MachineInstr *MI, unsigned Op) {
00123   if (MI->getOperand(Op).isFI()) return true;
00124   return Op+X86::AddrSegmentReg <= MI->getNumOperands() &&
00125     MI->getOperand(Op+X86::AddrBaseReg).isReg() &&
00126     isScale(MI->getOperand(Op+X86::AddrScaleAmt)) &&
00127     MI->getOperand(Op+X86::AddrIndexReg).isReg() &&
00128     (MI->getOperand(Op+X86::AddrDisp).isImm() ||
00129      MI->getOperand(Op+X86::AddrDisp).isGlobal() ||
00130      MI->getOperand(Op+X86::AddrDisp).isCPI() ||
00131      MI->getOperand(Op+X86::AddrDisp).isJTI());
00132 }
00133 
00134 inline static bool isMem(const MachineInstr *MI, unsigned Op) {
00135   if (MI->getOperand(Op).isFI()) return true;
00136   return Op+X86::AddrNumOperands <= MI->getNumOperands() &&
00137     MI->getOperand(Op+X86::AddrSegmentReg).isReg() &&
00138     isLeaMem(MI, Op);
00139 }
00140 
00141 class X86InstrInfo final : public X86GenInstrInfo {
00142   X86Subtarget &Subtarget;
00143   const X86RegisterInfo RI;
00144 
00145   /// RegOp2MemOpTable3Addr, RegOp2MemOpTable0, RegOp2MemOpTable1,
00146   /// RegOp2MemOpTable2, RegOp2MemOpTable3 - Load / store folding opcode maps.
00147   ///
00148   typedef DenseMap<unsigned,
00149                    std::pair<unsigned, unsigned> > RegOp2MemOpTableType;
00150   RegOp2MemOpTableType RegOp2MemOpTable2Addr;
00151   RegOp2MemOpTableType RegOp2MemOpTable0;
00152   RegOp2MemOpTableType RegOp2MemOpTable1;
00153   RegOp2MemOpTableType RegOp2MemOpTable2;
00154   RegOp2MemOpTableType RegOp2MemOpTable3;
00155 
00156   /// MemOp2RegOpTable - Load / store unfolding opcode map.
00157   ///
00158   typedef DenseMap<unsigned,
00159                    std::pair<unsigned, unsigned> > MemOp2RegOpTableType;
00160   MemOp2RegOpTableType MemOp2RegOpTable;
00161 
00162   static void AddTableEntry(RegOp2MemOpTableType &R2MTable,
00163                             MemOp2RegOpTableType &M2RTable,
00164                             unsigned RegOp, unsigned MemOp, unsigned Flags);
00165 
00166   virtual void anchor();
00167 
00168 public:
00169   explicit X86InstrInfo(X86Subtarget &STI);
00170 
00171   /// getRegisterInfo - TargetInstrInfo is a superset of MRegister info.  As
00172   /// such, whenever a client has an instance of instruction info, it should
00173   /// always be able to get register info as well (through this method).
00174   ///
00175   const X86RegisterInfo &getRegisterInfo() const { return RI; }
00176 
00177   /// isCoalescableExtInstr - Return true if the instruction is a "coalescable"
00178   /// extension instruction. That is, it's like a copy where it's legal for the
00179   /// source to overlap the destination. e.g. X86::MOVSX64rr32. If this returns
00180   /// true, then it's expected the pre-extension value is available as a subreg
00181   /// of the result register. This also returns the sub-register index in
00182   /// SubIdx.
00183   bool isCoalescableExtInstr(const MachineInstr &MI,
00184                              unsigned &SrcReg, unsigned &DstReg,
00185                              unsigned &SubIdx) const override;
00186 
00187   unsigned isLoadFromStackSlot(const MachineInstr *MI,
00188                                int &FrameIndex) const override;
00189   /// isLoadFromStackSlotPostFE - Check for post-frame ptr elimination
00190   /// stack locations as well.  This uses a heuristic so it isn't
00191   /// reliable for correctness.
00192   unsigned isLoadFromStackSlotPostFE(const MachineInstr *MI,
00193                                      int &FrameIndex) const override;
00194 
00195   unsigned isStoreToStackSlot(const MachineInstr *MI,
00196                               int &FrameIndex) const override;
00197   /// isStoreToStackSlotPostFE - Check for post-frame ptr elimination
00198   /// stack locations as well.  This uses a heuristic so it isn't
00199   /// reliable for correctness.
00200   unsigned isStoreToStackSlotPostFE(const MachineInstr *MI,
00201                                     int &FrameIndex) const override;
00202 
00203   bool isReallyTriviallyReMaterializable(const MachineInstr *MI,
00204                                          AliasAnalysis *AA) const override;
00205   void reMaterialize(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
00206                      unsigned DestReg, unsigned SubIdx,
00207                      const MachineInstr *Orig,
00208                      const TargetRegisterInfo &TRI) const override;
00209 
00210   /// Given an operand within a MachineInstr, insert preceding code to put it
00211   /// into the right format for a particular kind of LEA instruction. This may
00212   /// involve using an appropriate super-register instead (with an implicit use
00213   /// of the original) or creating a new virtual register and inserting COPY
00214   /// instructions to get the data into the right class.
00215   ///
00216   /// Reference parameters are set to indicate how caller should add this
00217   /// operand to the LEA instruction.
00218   bool classifyLEAReg(MachineInstr *MI, const MachineOperand &Src,
00219                       unsigned LEAOpcode, bool AllowSP,
00220                       unsigned &NewSrc, bool &isKill,
00221                       bool &isUndef, MachineOperand &ImplicitOp) const;
00222 
00223   /// convertToThreeAddress - This method must be implemented by targets that
00224   /// set the M_CONVERTIBLE_TO_3_ADDR flag.  When this flag is set, the target
00225   /// may be able to convert a two-address instruction into a true
00226   /// three-address instruction on demand.  This allows the X86 target (for
00227   /// example) to convert ADD and SHL instructions into LEA instructions if they
00228   /// would require register copies due to two-addressness.
00229   ///
00230   /// This method returns a null pointer if the transformation cannot be
00231   /// performed, otherwise it returns the new instruction.
00232   ///
00233   MachineInstr *convertToThreeAddress(MachineFunction::iterator &MFI,
00234                                       MachineBasicBlock::iterator &MBBI,
00235                                       LiveVariables *LV) const override;
00236 
00237   /// commuteInstruction - We have a few instructions that must be hacked on to
00238   /// commute them.
00239   ///
00240   MachineInstr *commuteInstruction(MachineInstr *MI, bool NewMI) const override;
00241 
00242   bool findCommutedOpIndices(MachineInstr *MI, unsigned &SrcOpIdx1,
00243                              unsigned &SrcOpIdx2) const override;
00244 
00245   // Branch analysis.
00246   bool isUnpredicatedTerminator(const MachineInstr* MI) const override;
00247   bool AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
00248                      MachineBasicBlock *&FBB,
00249                      SmallVectorImpl<MachineOperand> &Cond,
00250                      bool AllowModify) const override;
00251   unsigned RemoveBranch(MachineBasicBlock &MBB) const override;
00252   unsigned InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
00253                         MachineBasicBlock *FBB,
00254                         const SmallVectorImpl<MachineOperand> &Cond,
00255                         DebugLoc DL) const override;
00256   bool canInsertSelect(const MachineBasicBlock&,
00257                        const SmallVectorImpl<MachineOperand> &Cond,
00258                        unsigned, unsigned, int&, int&, int&) const override;
00259   void insertSelect(MachineBasicBlock &MBB,
00260                     MachineBasicBlock::iterator MI, DebugLoc DL,
00261                     unsigned DstReg,
00262                     const SmallVectorImpl<MachineOperand> &Cond,
00263                     unsigned TrueReg, unsigned FalseReg) const override;
00264   void copyPhysReg(MachineBasicBlock &MBB,
00265                    MachineBasicBlock::iterator MI, DebugLoc DL,
00266                    unsigned DestReg, unsigned SrcReg,
00267                    bool KillSrc) const override;
00268   void storeRegToStackSlot(MachineBasicBlock &MBB,
00269                            MachineBasicBlock::iterator MI,
00270                            unsigned SrcReg, bool isKill, int FrameIndex,
00271                            const TargetRegisterClass *RC,
00272                            const TargetRegisterInfo *TRI) const override;
00273 
00274   void storeRegToAddr(MachineFunction &MF, unsigned SrcReg, bool isKill,
00275                       SmallVectorImpl<MachineOperand> &Addr,
00276                       const TargetRegisterClass *RC,
00277                       MachineInstr::mmo_iterator MMOBegin,
00278                       MachineInstr::mmo_iterator MMOEnd,
00279                       SmallVectorImpl<MachineInstr*> &NewMIs) const;
00280 
00281   void loadRegFromStackSlot(MachineBasicBlock &MBB,
00282                             MachineBasicBlock::iterator MI,
00283                             unsigned DestReg, int FrameIndex,
00284                             const TargetRegisterClass *RC,
00285                             const TargetRegisterInfo *TRI) const override;
00286 
00287   void loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
00288                        SmallVectorImpl<MachineOperand> &Addr,
00289                        const TargetRegisterClass *RC,
00290                        MachineInstr::mmo_iterator MMOBegin,
00291                        MachineInstr::mmo_iterator MMOEnd,
00292                        SmallVectorImpl<MachineInstr*> &NewMIs) const;
00293 
00294   bool expandPostRAPseudo(MachineBasicBlock::iterator MI) const override;
00295 
00296   /// foldMemoryOperand - If this target supports it, fold a load or store of
00297   /// the specified stack slot into the specified machine instruction for the
00298   /// specified operand(s).  If this is possible, the target should perform the
00299   /// folding and return true, otherwise it should return false.  If it folds
00300   /// the instruction, it is likely that the MachineInstruction the iterator
00301   /// references has been changed.
00302   MachineInstr* foldMemoryOperandImpl(MachineFunction &MF,
00303                                       MachineInstr* MI,
00304                                       const SmallVectorImpl<unsigned> &Ops,
00305                                       int FrameIndex) const override;
00306 
00307   /// foldMemoryOperand - Same as the previous version except it allows folding
00308   /// of any load and store from / to any address, not just from a specific
00309   /// stack slot.
00310   MachineInstr* foldMemoryOperandImpl(MachineFunction &MF,
00311                                       MachineInstr* MI,
00312                                       const SmallVectorImpl<unsigned> &Ops,
00313                                       MachineInstr* LoadMI) const override;
00314 
00315   /// canFoldMemoryOperand - Returns true if the specified load / store is
00316   /// folding is possible.
00317   bool canFoldMemoryOperand(const MachineInstr*,
00318                             const SmallVectorImpl<unsigned> &) const override;
00319 
00320   /// unfoldMemoryOperand - Separate a single instruction which folded a load or
00321   /// a store or a load and a store into two or more instruction. If this is
00322   /// possible, returns true as well as the new instructions by reference.
00323   bool unfoldMemoryOperand(MachineFunction &MF, MachineInstr *MI,
00324                          unsigned Reg, bool UnfoldLoad, bool UnfoldStore,
00325                          SmallVectorImpl<MachineInstr*> &NewMIs) const override;
00326 
00327   bool unfoldMemoryOperand(SelectionDAG &DAG, SDNode *N,
00328                            SmallVectorImpl<SDNode*> &NewNodes) const override;
00329 
00330   /// getOpcodeAfterMemoryUnfold - Returns the opcode of the would be new
00331   /// instruction after load / store are unfolded from an instruction of the
00332   /// specified opcode. It returns zero if the specified unfolding is not
00333   /// possible. If LoadRegIndex is non-null, it is filled in with the operand
00334   /// index of the operand which will hold the register holding the loaded
00335   /// value.
00336   unsigned getOpcodeAfterMemoryUnfold(unsigned Opc,
00337                               bool UnfoldLoad, bool UnfoldStore,
00338                               unsigned *LoadRegIndex = nullptr) const override;
00339 
00340   /// areLoadsFromSameBasePtr - This is used by the pre-regalloc scheduler
00341   /// to determine if two loads are loading from the same base address. It
00342   /// should only return true if the base pointers are the same and the
00343   /// only differences between the two addresses are the offset. It also returns
00344   /// the offsets by reference.
00345   bool areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2, int64_t &Offset1,
00346                                int64_t &Offset2) const override;
00347 
00348   /// shouldScheduleLoadsNear - This is a used by the pre-regalloc scheduler to
00349   /// determine (in conjunction with areLoadsFromSameBasePtr) if two loads should
00350   /// be scheduled togther. On some targets if two loads are loading from
00351   /// addresses in the same cache line, it's better if they are scheduled
00352   /// together. This function takes two integers that represent the load offsets
00353   /// from the common base address. It returns true if it decides it's desirable
00354   /// to schedule the two loads together. "NumLoads" is the number of loads that
00355   /// have already been scheduled after Load1.
00356   bool shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
00357                                int64_t Offset1, int64_t Offset2,
00358                                unsigned NumLoads) const override;
00359 
00360   bool shouldScheduleAdjacent(MachineInstr* First,
00361                               MachineInstr *Second) const override;
00362 
00363   void getNoopForMachoTarget(MCInst &NopInst) const override;
00364 
00365   bool
00366   ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const override;
00367 
00368   /// isSafeToMoveRegClassDefs - Return true if it's safe to move a machine
00369   /// instruction that defines the specified register class.
00370   bool isSafeToMoveRegClassDefs(const TargetRegisterClass *RC) const override;
00371 
00372   /// isSafeToClobberEFLAGS - Return true if it's safe insert an instruction tha
00373   /// would clobber the EFLAGS condition register. Note the result may be
00374   /// conservative. If it cannot definitely determine the safety after visiting
00375   /// a few instructions in each direction it assumes it's not safe.
00376   bool isSafeToClobberEFLAGS(MachineBasicBlock &MBB,
00377                              MachineBasicBlock::iterator I) const;
00378 
00379   static bool isX86_64ExtendedReg(const MachineOperand &MO) {
00380     if (!MO.isReg()) return false;
00381     return X86II::isX86_64ExtendedReg(MO.getReg());
00382   }
00383 
00384   /// getGlobalBaseReg - Return a virtual register initialized with the
00385   /// the global base register value. Output instructions required to
00386   /// initialize the register in the function entry block, if necessary.
00387   ///
00388   unsigned getGlobalBaseReg(MachineFunction *MF) const;
00389 
00390   std::pair<uint16_t, uint16_t>
00391   getExecutionDomain(const MachineInstr *MI) const override;
00392 
00393   void setExecutionDomain(MachineInstr *MI, unsigned Domain) const override;
00394 
00395   unsigned
00396     getPartialRegUpdateClearance(const MachineInstr *MI, unsigned OpNum,
00397                                  const TargetRegisterInfo *TRI) const override;
00398   unsigned getUndefRegClearance(const MachineInstr *MI, unsigned &OpNum,
00399                                 const TargetRegisterInfo *TRI) const override;
00400   void breakPartialRegDependency(MachineBasicBlock::iterator MI, unsigned OpNum,
00401                                  const TargetRegisterInfo *TRI) const override;
00402 
00403   MachineInstr* foldMemoryOperandImpl(MachineFunction &MF,
00404                                       MachineInstr* MI,
00405                                       unsigned OpNum,
00406                                       const SmallVectorImpl<MachineOperand> &MOs,
00407                                       unsigned Size, unsigned Alignment) const;
00408 
00409   void
00410   getUnconditionalBranch(MCInst &Branch,
00411                          const MCSymbolRefExpr *BranchTarget) const override;
00412 
00413   void getTrap(MCInst &MI) const override;
00414 
00415   bool isHighLatencyDef(int opc) const override;
00416 
00417   bool hasHighOperandLatency(const InstrItineraryData *ItinData,
00418                              const MachineRegisterInfo *MRI,
00419                              const MachineInstr *DefMI, unsigned DefIdx,
00420                              const MachineInstr *UseMI,
00421                              unsigned UseIdx) const override;
00422 
00423   /// analyzeCompare - For a comparison instruction, return the source registers
00424   /// in SrcReg and SrcReg2 if having two register operands, and the value it
00425   /// compares against in CmpValue. Return true if the comparison instruction
00426   /// can be analyzed.
00427   bool analyzeCompare(const MachineInstr *MI, unsigned &SrcReg,
00428                       unsigned &SrcReg2, int &CmpMask,
00429                       int &CmpValue) const override;
00430 
00431   /// optimizeCompareInstr - Check if there exists an earlier instruction that
00432   /// operates on the same source operands and sets flags in the same way as
00433   /// Compare; remove Compare if possible.
00434   bool optimizeCompareInstr(MachineInstr *CmpInstr, unsigned SrcReg,
00435                             unsigned SrcReg2, int CmpMask, int CmpValue,
00436                             const MachineRegisterInfo *MRI) const override;
00437 
00438   /// optimizeLoadInstr - Try to remove the load by folding it to a register
00439   /// operand at the use. We fold the load instructions if and only if the
00440   /// def and use are in the same BB. We only look at one load and see
00441   /// whether it can be folded into MI. FoldAsLoadDefReg is the virtual register
00442   /// defined by the load we are trying to fold. DefMI returns the machine
00443   /// instruction that defines FoldAsLoadDefReg, and the function returns
00444   /// the machine instruction generated due to folding.
00445   MachineInstr* optimizeLoadInstr(MachineInstr *MI,
00446                                   const MachineRegisterInfo *MRI,
00447                                   unsigned &FoldAsLoadDefReg,
00448                                   MachineInstr *&DefMI) const override;
00449 
00450 private:
00451   MachineInstr * convertToThreeAddressWithLEA(unsigned MIOpc,
00452                                               MachineFunction::iterator &MFI,
00453                                               MachineBasicBlock::iterator &MBBI,
00454                                               LiveVariables *LV) const;
00455 
00456   /// isFrameOperand - Return true and the FrameIndex if the specified
00457   /// operand and follow operands form a reference to the stack frame.
00458   bool isFrameOperand(const MachineInstr *MI, unsigned int Op,
00459                       int &FrameIndex) const;
00460 };
00461 
00462 } // End llvm namespace
00463 
00464 #endif