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 X86INSTRUCTIONINFO_H
00015 #define X86INSTRUCTIONINFO_H
00016 
00017 #include "X86.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 X86TargetMachine;
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 
00050     // Artificial condition codes. These are used by AnalyzeBranch
00051     // to indicate a block terminated with two conditional branches to
00052     // the same location. This occurs in code using FCMP_OEQ or FCMP_UNE,
00053     // which can't be represented on x86 with a single condition. These
00054     // are never used in MachineInstrs.
00055     COND_NE_OR_P,
00056     COND_NP_OR_E,
00057 
00058     COND_INVALID
00059   };
00060 
00061   // Turn condition code into conditional branch opcode.
00062   unsigned GetCondBranchFromCond(CondCode CC);
00063 
00064   // Turn CMov opcode into condition code.
00065   CondCode getCondFromCMovOpc(unsigned Opc);
00066 
00067   /// GetOppositeBranchCondition - Return the inverse of the specified cond,
00068   /// e.g. turning COND_E to COND_NE.
00069   CondCode GetOppositeBranchCondition(X86::CondCode CC);
00070 }  // end namespace X86;
00071 
00072 
00073 /// isGlobalStubReference - Return true if the specified TargetFlag operand is
00074 /// a reference to a stub for a global, not the global itself.
00075 inline static bool isGlobalStubReference(unsigned char TargetFlag) {
00076   switch (TargetFlag) {
00077   case X86II::MO_DLLIMPORT: // dllimport stub.
00078   case X86II::MO_GOTPCREL:  // rip-relative GOT reference.
00079   case X86II::MO_GOT:       // normal GOT reference.
00080   case X86II::MO_DARWIN_NONLAZY_PIC_BASE:        // Normal $non_lazy_ptr ref.
00081   case X86II::MO_DARWIN_NONLAZY:                 // Normal $non_lazy_ptr ref.
00082   case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE: // Hidden $non_lazy_ptr ref.
00083     return true;
00084   default:
00085     return false;
00086   }
00087 }
00088 
00089 /// isGlobalRelativeToPICBase - Return true if the specified global value
00090 /// reference is relative to a 32-bit PIC base (X86ISD::GlobalBaseReg).  If this
00091 /// is true, the addressing mode has the PIC base register added in (e.g. EBX).
00092 inline static bool isGlobalRelativeToPICBase(unsigned char TargetFlag) {
00093   switch (TargetFlag) {
00094   case X86II::MO_GOTOFF:                         // isPICStyleGOT: local global.
00095   case X86II::MO_GOT:                            // isPICStyleGOT: other global.
00096   case X86II::MO_PIC_BASE_OFFSET:                // Darwin local global.
00097   case X86II::MO_DARWIN_NONLAZY_PIC_BASE:        // Darwin/32 external global.
00098   case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE: // Darwin/32 hidden global.
00099   case X86II::MO_TLVP:                           // ??? Pretty sure..
00100     return true;
00101   default:
00102     return false;
00103   }
00104 }
00105 
00106 inline static bool isScale(const MachineOperand &MO) {
00107   return MO.isImm() &&
00108     (MO.getImm() == 1 || MO.getImm() == 2 ||
00109      MO.getImm() == 4 || MO.getImm() == 8);
00110 }
00111 
00112 inline static bool isLeaMem(const MachineInstr *MI, unsigned Op) {
00113   if (MI->getOperand(Op).isFI()) return true;
00114   return Op+4 <= MI->getNumOperands() &&
00115     MI->getOperand(Op  ).isReg() && isScale(MI->getOperand(Op+1)) &&
00116     MI->getOperand(Op+2).isReg() &&
00117     (MI->getOperand(Op+3).isImm() ||
00118      MI->getOperand(Op+3).isGlobal() ||
00119      MI->getOperand(Op+3).isCPI() ||
00120      MI->getOperand(Op+3).isJTI());
00121 }
00122 
00123 inline static bool isMem(const MachineInstr *MI, unsigned Op) {
00124   if (MI->getOperand(Op).isFI()) return true;
00125   return Op+5 <= MI->getNumOperands() &&
00126     MI->getOperand(Op+4).isReg() &&
00127     isLeaMem(MI, Op);
00128 }
00129 
00130 class X86InstrInfo : public X86GenInstrInfo {
00131   X86TargetMachine &TM;
00132   const X86RegisterInfo RI;
00133 
00134   /// RegOp2MemOpTable3Addr, RegOp2MemOpTable0, RegOp2MemOpTable1,
00135   /// RegOp2MemOpTable2, RegOp2MemOpTable3 - Load / store folding opcode maps.
00136   ///
00137   typedef DenseMap<unsigned,
00138                    std::pair<unsigned, unsigned> > RegOp2MemOpTableType;
00139   RegOp2MemOpTableType RegOp2MemOpTable2Addr;
00140   RegOp2MemOpTableType RegOp2MemOpTable0;
00141   RegOp2MemOpTableType RegOp2MemOpTable1;
00142   RegOp2MemOpTableType RegOp2MemOpTable2;
00143   RegOp2MemOpTableType RegOp2MemOpTable3;
00144 
00145   /// MemOp2RegOpTable - Load / store unfolding opcode map.
00146   ///
00147   typedef DenseMap<unsigned,
00148                    std::pair<unsigned, unsigned> > MemOp2RegOpTableType;
00149   MemOp2RegOpTableType MemOp2RegOpTable;
00150 
00151   static void AddTableEntry(RegOp2MemOpTableType &R2MTable,
00152                             MemOp2RegOpTableType &M2RTable,
00153                             unsigned RegOp, unsigned MemOp, unsigned Flags);
00154 
00155 public:
00156   explicit X86InstrInfo(X86TargetMachine &tm);
00157 
00158   /// getRegisterInfo - TargetInstrInfo is a superset of MRegister info.  As
00159   /// such, whenever a client has an instance of instruction info, it should
00160   /// always be able to get register info as well (through this method).
00161   ///
00162   virtual const X86RegisterInfo &getRegisterInfo() const { return RI; }
00163 
00164   /// isCoalescableExtInstr - Return true if the instruction is a "coalescable"
00165   /// extension instruction. That is, it's like a copy where it's legal for the
00166   /// source to overlap the destination. e.g. X86::MOVSX64rr32. If this returns
00167   /// true, then it's expected the pre-extension value is available as a subreg
00168   /// of the result register. This also returns the sub-register index in
00169   /// SubIdx.
00170   virtual bool isCoalescableExtInstr(const MachineInstr &MI,
00171                                      unsigned &SrcReg, unsigned &DstReg,
00172                                      unsigned &SubIdx) const;
00173 
00174   unsigned isLoadFromStackSlot(const MachineInstr *MI, int &FrameIndex) const;
00175   /// isLoadFromStackSlotPostFE - Check for post-frame ptr elimination
00176   /// stack locations as well.  This uses a heuristic so it isn't
00177   /// reliable for correctness.
00178   unsigned isLoadFromStackSlotPostFE(const MachineInstr *MI,
00179                                      int &FrameIndex) const;
00180 
00181   unsigned isStoreToStackSlot(const MachineInstr *MI, int &FrameIndex) const;
00182   /// isStoreToStackSlotPostFE - Check for post-frame ptr elimination
00183   /// stack locations as well.  This uses a heuristic so it isn't
00184   /// reliable for correctness.
00185   unsigned isStoreToStackSlotPostFE(const MachineInstr *MI,
00186                                     int &FrameIndex) const;
00187 
00188   bool isReallyTriviallyReMaterializable(const MachineInstr *MI,
00189                                          AliasAnalysis *AA) const;
00190   void reMaterialize(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
00191                      unsigned DestReg, unsigned SubIdx,
00192                      const MachineInstr *Orig,
00193                      const TargetRegisterInfo &TRI) const;
00194 
00195   /// convertToThreeAddress - This method must be implemented by targets that
00196   /// set the M_CONVERTIBLE_TO_3_ADDR flag.  When this flag is set, the target
00197   /// may be able to convert a two-address instruction into a true
00198   /// three-address instruction on demand.  This allows the X86 target (for
00199   /// example) to convert ADD and SHL instructions into LEA instructions if they
00200   /// would require register copies due to two-addressness.
00201   ///
00202   /// This method returns a null pointer if the transformation cannot be
00203   /// performed, otherwise it returns the new instruction.
00204   ///
00205   virtual MachineInstr *convertToThreeAddress(MachineFunction::iterator &MFI,
00206                                               MachineBasicBlock::iterator &MBBI,
00207                                               LiveVariables *LV) const;
00208 
00209   /// commuteInstruction - We have a few instructions that must be hacked on to
00210   /// commute them.
00211   ///
00212   virtual MachineInstr *commuteInstruction(MachineInstr *MI, bool NewMI) const;
00213 
00214   // Branch analysis.
00215   virtual bool isUnpredicatedTerminator(const MachineInstr* MI) const;
00216   virtual bool AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
00217                              MachineBasicBlock *&FBB,
00218                              SmallVectorImpl<MachineOperand> &Cond,
00219                              bool AllowModify) const;
00220   virtual unsigned RemoveBranch(MachineBasicBlock &MBB) const;
00221   virtual unsigned InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
00222                                 MachineBasicBlock *FBB,
00223                                 const SmallVectorImpl<MachineOperand> &Cond,
00224                                 DebugLoc DL) const;
00225   virtual bool canInsertSelect(const MachineBasicBlock&,
00226                                const SmallVectorImpl<MachineOperand> &Cond,
00227                                unsigned, unsigned, int&, int&, int&) const;
00228   virtual void insertSelect(MachineBasicBlock &MBB,
00229                             MachineBasicBlock::iterator MI, DebugLoc DL,
00230                             unsigned DstReg,
00231                             const SmallVectorImpl<MachineOperand> &Cond,
00232                             unsigned TrueReg, unsigned FalseReg) const;
00233   virtual void copyPhysReg(MachineBasicBlock &MBB,
00234                            MachineBasicBlock::iterator MI, DebugLoc DL,
00235                            unsigned DestReg, unsigned SrcReg,
00236                            bool KillSrc) const;
00237   virtual void storeRegToStackSlot(MachineBasicBlock &MBB,
00238                                    MachineBasicBlock::iterator MI,
00239                                    unsigned SrcReg, bool isKill, int FrameIndex,
00240                                    const TargetRegisterClass *RC,
00241                                    const TargetRegisterInfo *TRI) const;
00242 
00243   virtual void storeRegToAddr(MachineFunction &MF, unsigned SrcReg, bool isKill,
00244                               SmallVectorImpl<MachineOperand> &Addr,
00245                               const TargetRegisterClass *RC,
00246                               MachineInstr::mmo_iterator MMOBegin,
00247                               MachineInstr::mmo_iterator MMOEnd,
00248                               SmallVectorImpl<MachineInstr*> &NewMIs) const;
00249 
00250   virtual void loadRegFromStackSlot(MachineBasicBlock &MBB,
00251                                     MachineBasicBlock::iterator MI,
00252                                     unsigned DestReg, int FrameIndex,
00253                                     const TargetRegisterClass *RC,
00254                                     const TargetRegisterInfo *TRI) const;
00255 
00256   virtual void loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
00257                                SmallVectorImpl<MachineOperand> &Addr,
00258                                const TargetRegisterClass *RC,
00259                                MachineInstr::mmo_iterator MMOBegin,
00260                                MachineInstr::mmo_iterator MMOEnd,
00261                                SmallVectorImpl<MachineInstr*> &NewMIs) const;
00262 
00263   virtual bool expandPostRAPseudo(MachineBasicBlock::iterator MI) const;
00264 
00265   virtual
00266   MachineInstr *emitFrameIndexDebugValue(MachineFunction &MF,
00267                                          int FrameIx, uint64_t Offset,
00268                                          const MDNode *MDPtr,
00269                                          DebugLoc DL) const;
00270 
00271   /// foldMemoryOperand - If this target supports it, fold a load or store of
00272   /// the specified stack slot into the specified machine instruction for the
00273   /// specified operand(s).  If this is possible, the target should perform the
00274   /// folding and return true, otherwise it should return false.  If it folds
00275   /// the instruction, it is likely that the MachineInstruction the iterator
00276   /// references has been changed.
00277   virtual MachineInstr* foldMemoryOperandImpl(MachineFunction &MF,
00278                                               MachineInstr* MI,
00279                                            const SmallVectorImpl<unsigned> &Ops,
00280                                               int FrameIndex) const;
00281 
00282   /// foldMemoryOperand - Same as the previous version except it allows folding
00283   /// of any load and store from / to any address, not just from a specific
00284   /// stack slot.
00285   virtual MachineInstr* foldMemoryOperandImpl(MachineFunction &MF,
00286                                               MachineInstr* MI,
00287                                            const SmallVectorImpl<unsigned> &Ops,
00288                                               MachineInstr* LoadMI) const;
00289 
00290   /// canFoldMemoryOperand - Returns true if the specified load / store is
00291   /// folding is possible.
00292   virtual bool canFoldMemoryOperand(const MachineInstr*,
00293                                     const SmallVectorImpl<unsigned> &) const;
00294 
00295   /// unfoldMemoryOperand - Separate a single instruction which folded a load or
00296   /// a store or a load and a store into two or more instruction. If this is
00297   /// possible, returns true as well as the new instructions by reference.
00298   virtual bool unfoldMemoryOperand(MachineFunction &MF, MachineInstr *MI,
00299                            unsigned Reg, bool UnfoldLoad, bool UnfoldStore,
00300                            SmallVectorImpl<MachineInstr*> &NewMIs) const;
00301 
00302   virtual bool unfoldMemoryOperand(SelectionDAG &DAG, SDNode *N,
00303                            SmallVectorImpl<SDNode*> &NewNodes) const;
00304 
00305   /// getOpcodeAfterMemoryUnfold - Returns the opcode of the would be new
00306   /// instruction after load / store are unfolded from an instruction of the
00307   /// specified opcode. It returns zero if the specified unfolding is not
00308   /// possible. If LoadRegIndex is non-null, it is filled in with the operand
00309   /// index of the operand which will hold the register holding the loaded
00310   /// value.
00311   virtual unsigned getOpcodeAfterMemoryUnfold(unsigned Opc,
00312                                       bool UnfoldLoad, bool UnfoldStore,
00313                                       unsigned *LoadRegIndex = 0) const;
00314 
00315   /// areLoadsFromSameBasePtr - This is used by the pre-regalloc scheduler
00316   /// to determine if two loads are loading from the same base address. It
00317   /// should only return true if the base pointers are the same and the
00318   /// only differences between the two addresses are the offset. It also returns
00319   /// the offsets by reference.
00320   virtual bool areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2,
00321                                        int64_t &Offset1, int64_t &Offset2) const;
00322 
00323   /// shouldScheduleLoadsNear - This is a used by the pre-regalloc scheduler to
00324   /// determine (in conjunction with areLoadsFromSameBasePtr) if two loads should
00325   /// be scheduled togther. On some targets if two loads are loading from
00326   /// addresses in the same cache line, it's better if they are scheduled
00327   /// together. This function takes two integers that represent the load offsets
00328   /// from the common base address. It returns true if it decides it's desirable
00329   /// to schedule the two loads together. "NumLoads" is the number of loads that
00330   /// have already been scheduled after Load1.
00331   virtual bool shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
00332                                        int64_t Offset1, int64_t Offset2,
00333                                        unsigned NumLoads) const;
00334 
00335   virtual void getNoopForMachoTarget(MCInst &NopInst) const;
00336 
00337   virtual
00338   bool ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const;
00339 
00340   /// isSafeToMoveRegClassDefs - Return true if it's safe to move a machine
00341   /// instruction that defines the specified register class.
00342   bool isSafeToMoveRegClassDefs(const TargetRegisterClass *RC) const;
00343 
00344   static bool isX86_64ExtendedReg(const MachineOperand &MO) {
00345     if (!MO.isReg()) return false;
00346     return X86II::isX86_64ExtendedReg(MO.getReg());
00347   }
00348 
00349   /// getGlobalBaseReg - Return a virtual register initialized with the
00350   /// the global base register value. Output instructions required to
00351   /// initialize the register in the function entry block, if necessary.
00352   ///
00353   unsigned getGlobalBaseReg(MachineFunction *MF) const;
00354 
00355   std::pair<uint16_t, uint16_t>
00356   getExecutionDomain(const MachineInstr *MI) const;
00357 
00358   void setExecutionDomain(MachineInstr *MI, unsigned Domain) const;
00359 
00360   unsigned getPartialRegUpdateClearance(const MachineInstr *MI, unsigned OpNum,
00361                                         const TargetRegisterInfo *TRI) const;
00362   void breakPartialRegDependency(MachineBasicBlock::iterator MI, unsigned OpNum,
00363                                  const TargetRegisterInfo *TRI) const;
00364 
00365   MachineInstr* foldMemoryOperandImpl(MachineFunction &MF,
00366                                       MachineInstr* MI,
00367                                       unsigned OpNum,
00368                                       const SmallVectorImpl<MachineOperand> &MOs,
00369                                       unsigned Size, unsigned Alignment) const;
00370 
00371   bool isHighLatencyDef(int opc) const;
00372 
00373   bool hasHighOperandLatency(const InstrItineraryData *ItinData,
00374                              const MachineRegisterInfo *MRI,
00375                              const MachineInstr *DefMI, unsigned DefIdx,
00376                              const MachineInstr *UseMI, unsigned UseIdx) const;
00377 
00378   /// analyzeCompare - For a comparison instruction, return the source registers
00379   /// in SrcReg and SrcReg2 if having two register operands, and the value it
00380   /// compares against in CmpValue. Return true if the comparison instruction
00381   /// can be analyzed.
00382   virtual bool analyzeCompare(const MachineInstr *MI, unsigned &SrcReg,
00383                               unsigned &SrcReg2,
00384                               int &CmpMask, int &CmpValue) const;
00385 
00386   /// optimizeCompareInstr - Check if there exists an earlier instruction that
00387   /// operates on the same source operands and sets flags in the same way as
00388   /// Compare; remove Compare if possible.
00389   virtual bool optimizeCompareInstr(MachineInstr *CmpInstr, unsigned SrcReg,
00390                                     unsigned SrcReg2, int CmpMask, int CmpValue,
00391                                     const MachineRegisterInfo *MRI) const;
00392 
00393   /// optimizeLoadInstr - Try to remove the load by folding it to a register
00394   /// operand at the use. We fold the load instructions if and only if the
00395   /// def and use are in the same BB. We only look at one load and see
00396   /// whether it can be folded into MI. FoldAsLoadDefReg is the virtual register
00397   /// defined by the load we are trying to fold. DefMI returns the machine
00398   /// instruction that defines FoldAsLoadDefReg, and the function returns
00399   /// the machine instruction generated due to folding.
00400   virtual MachineInstr* optimizeLoadInstr(MachineInstr *MI,
00401                         const MachineRegisterInfo *MRI,
00402                         unsigned &FoldAsLoadDefReg,
00403                         MachineInstr *&DefMI) const;
00404 
00405 private:
00406   MachineInstr * convertToThreeAddressWithLEA(unsigned MIOpc,
00407                                               MachineFunction::iterator &MFI,
00408                                               MachineBasicBlock::iterator &MBBI,
00409                                               LiveVariables *LV) const;
00410 
00411   /// isFrameOperand - Return true and the FrameIndex if the specified
00412   /// operand and follow operands form a reference to the stack frame.
00413   bool isFrameOperand(const MachineInstr *MI, unsigned int Op,
00414                       int &FrameIndex) const;
00415 };
00416 
00417 } // End llvm namespace
00418 
00419 #endif