LLVM API Documentation

ARMBaseInstrInfo.h

Go to the documentation of this file.
00001 //===- ARMBaseInstrInfo.h - ARM Base 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 Base ARM implementation of the TargetInstrInfo class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef ARMBASEINSTRUCTIONINFO_H
00015 #define ARMBASEINSTRUCTIONINFO_H
00016 
00017 #include "ARM.h"
00018 #include "ARMRegisterInfo.h"
00019 #include "llvm/CodeGen/MachineInstrBuilder.h"
00020 #include "llvm/Target/TargetInstrInfo.h"
00021 
00022 namespace llvm {
00023 
00024 /// ARMII - This namespace holds all of the target specific flags that
00025 /// instruction info tracks.
00026 ///
00027 namespace ARMII {
00028   enum {
00029     //===------------------------------------------------------------------===//
00030     // Instruction Flags.
00031 
00032     //===------------------------------------------------------------------===//
00033     // This four-bit field describes the addressing mode used.
00034 
00035     AddrModeMask  = 0xf,
00036     AddrModeNone    = 0,
00037     AddrMode1       = 1,
00038     AddrMode2       = 2,
00039     AddrMode3       = 3,
00040     AddrMode4       = 4,
00041     AddrMode5       = 5,
00042     AddrMode6       = 6,
00043     AddrModeT1_1    = 7,
00044     AddrModeT1_2    = 8,
00045     AddrModeT1_4    = 9,
00046     AddrModeT1_s    = 10, // i8 * 4 for pc and sp relative data
00047     AddrModeT2_i12  = 11,
00048     AddrModeT2_i8   = 12,
00049     AddrModeT2_so   = 13,
00050     AddrModeT2_pc   = 14, // +/- i12 for pc relative data
00051     AddrModeT2_i8s4 = 15, // i8 * 4
00052 
00053     // Size* - Flags to keep track of the size of an instruction.
00054     SizeShift     = 4,
00055     SizeMask      = 7 << SizeShift,
00056     SizeSpecial   = 1,   // 0 byte pseudo or special case.
00057     Size8Bytes    = 2,
00058     Size4Bytes    = 3,
00059     Size2Bytes    = 4,
00060 
00061     // IndexMode - Unindex, pre-indexed, or post-indexed. Only valid for load
00062     // and store ops
00063     IndexModeShift = 7,
00064     IndexModeMask  = 3 << IndexModeShift,
00065     IndexModePre   = 1,
00066     IndexModePost  = 2,
00067 
00068     //===------------------------------------------------------------------===//
00069     // Instruction encoding formats.
00070     //
00071     FormShift     = 9,
00072     FormMask      = 0x3f << FormShift,
00073 
00074     // Pseudo instructions
00075     Pseudo        = 0  << FormShift,
00076 
00077     // Multiply instructions
00078     MulFrm        = 1  << FormShift,
00079 
00080     // Branch instructions
00081     BrFrm         = 2  << FormShift,
00082     BrMiscFrm     = 3  << FormShift,
00083 
00084     // Data Processing instructions
00085     DPFrm         = 4  << FormShift,
00086     DPSoRegFrm    = 5  << FormShift,
00087 
00088     // Load and Store
00089     LdFrm         = 6  << FormShift,
00090     StFrm         = 7  << FormShift,
00091     LdMiscFrm     = 8  << FormShift,
00092     StMiscFrm     = 9  << FormShift,
00093     LdStMulFrm    = 10 << FormShift,
00094 
00095     // Miscellaneous arithmetic instructions
00096     ArithMiscFrm  = 11 << FormShift,
00097 
00098     // Extend instructions
00099     ExtFrm        = 12 << FormShift,
00100 
00101     // VFP formats
00102     VFPUnaryFrm   = 13 << FormShift,
00103     VFPBinaryFrm  = 14 << FormShift,
00104     VFPConv1Frm   = 15 << FormShift,
00105     VFPConv2Frm   = 16 << FormShift,
00106     VFPConv3Frm   = 17 << FormShift,
00107     VFPConv4Frm   = 18 << FormShift,
00108     VFPConv5Frm   = 19 << FormShift,
00109     VFPLdStFrm    = 20 << FormShift,
00110     VFPLdStMulFrm = 21 << FormShift,
00111     VFPMiscFrm    = 22 << FormShift,
00112 
00113     // Thumb format
00114     ThumbFrm      = 23 << FormShift,
00115 
00116     // NEON format
00117     NEONFrm       = 24 << FormShift,
00118     NEONGetLnFrm  = 25 << FormShift,
00119     NEONSetLnFrm  = 26 << FormShift,
00120     NEONDupFrm    = 27 << FormShift,
00121 
00122     //===------------------------------------------------------------------===//
00123     // Misc flags.
00124 
00125     // UnaryDP - Indicates this is a unary data processing instruction, i.e.
00126     // it doesn't have a Rn operand.
00127     UnaryDP       = 1 << 15,
00128 
00129     // Xform16Bit - Indicates this Thumb2 instruction may be transformed into
00130     // a 16-bit Thumb instruction if certain conditions are met.
00131     Xform16Bit    = 1 << 16,
00132 
00133     //===------------------------------------------------------------------===//
00134     // Field shifts - such shifts are used to set field while generating
00135     // machine instructions.
00136     M_BitShift     = 5,
00137     ShiftImmShift  = 5,
00138     ShiftShift     = 7,
00139     N_BitShift     = 7,
00140     ImmHiShift     = 8,
00141     SoRotImmShift  = 8,
00142     RegRsShift     = 8,
00143     ExtRotImmShift = 10,
00144     RegRdLoShift   = 12,
00145     RegRdShift     = 12,
00146     RegRdHiShift   = 16,
00147     RegRnShift     = 16,
00148     S_BitShift     = 20,
00149     W_BitShift     = 21,
00150     AM3_I_BitShift = 22,
00151     D_BitShift     = 22,
00152     U_BitShift     = 23,
00153     P_BitShift     = 24,
00154     I_BitShift     = 25,
00155     CondShift      = 28
00156   };
00157 }
00158 
00159 class ARMBaseInstrInfo : public TargetInstrInfoImpl {
00160 protected:
00161   // Can be only subclassed.
00162   explicit ARMBaseInstrInfo();
00163 public:
00164   // Return the non-pre/post incrementing version of 'Opc'. Return 0
00165   // if there is not such an opcode.
00166   virtual unsigned getUnindexedOpcode(unsigned Opc) const =0;
00167 
00168   // Return true if the block does not fall through.
00169   virtual bool BlockHasNoFallThrough(const MachineBasicBlock &MBB) const =0;
00170 
00171   virtual MachineInstr *convertToThreeAddress(MachineFunction::iterator &MFI,
00172                                               MachineBasicBlock::iterator &MBBI,
00173                                               LiveVariables *LV) const;
00174 
00175   virtual const ARMBaseRegisterInfo &getRegisterInfo() const =0;
00176 
00177   // Branch analysis.
00178   virtual bool AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
00179                              MachineBasicBlock *&FBB,
00180                              SmallVectorImpl<MachineOperand> &Cond,
00181                              bool AllowModify) const;
00182   virtual unsigned RemoveBranch(MachineBasicBlock &MBB) const;
00183   virtual unsigned InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
00184                                 MachineBasicBlock *FBB,
00185                             const SmallVectorImpl<MachineOperand> &Cond) const;
00186 
00187   virtual
00188   bool ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const;
00189 
00190   // Predication support.
00191   bool isPredicated(const MachineInstr *MI) const {
00192     int PIdx = MI->findFirstPredOperandIdx();
00193     return PIdx != -1 && MI->getOperand(PIdx).getImm() != ARMCC::AL;
00194   }
00195 
00196   ARMCC::CondCodes getPredicate(const MachineInstr *MI) const {
00197     int PIdx = MI->findFirstPredOperandIdx();
00198     return PIdx != -1 ? (ARMCC::CondCodes)MI->getOperand(PIdx).getImm()
00199                       : ARMCC::AL;
00200   }
00201 
00202   virtual
00203   bool PredicateInstruction(MachineInstr *MI,
00204                             const SmallVectorImpl<MachineOperand> &Pred) const;
00205 
00206   virtual
00207   bool SubsumesPredicate(const SmallVectorImpl<MachineOperand> &Pred1,
00208                          const SmallVectorImpl<MachineOperand> &Pred2) const;
00209 
00210   virtual bool DefinesPredicate(MachineInstr *MI,
00211                                 std::vector<MachineOperand> &Pred) const;
00212 
00213   /// GetInstSize - Returns the size of the specified MachineInstr.
00214   ///
00215   virtual unsigned GetInstSizeInBytes(const MachineInstr* MI) const;
00216 
00217   /// Return true if the instruction is a register to register move and return
00218   /// the source and dest operands and their sub-register indices by reference.
00219   virtual bool isMoveInstr(const MachineInstr &MI,
00220                            unsigned &SrcReg, unsigned &DstReg,
00221                            unsigned &SrcSubIdx, unsigned &DstSubIdx) const;
00222 
00223   virtual unsigned isLoadFromStackSlot(const MachineInstr *MI,
00224                                        int &FrameIndex) const;
00225   virtual unsigned isStoreToStackSlot(const MachineInstr *MI,
00226                                       int &FrameIndex) const;
00227 
00228   virtual bool copyRegToReg(MachineBasicBlock &MBB,
00229                             MachineBasicBlock::iterator I,
00230                             unsigned DestReg, unsigned SrcReg,
00231                             const TargetRegisterClass *DestRC,
00232                             const TargetRegisterClass *SrcRC) const;
00233 
00234   virtual void storeRegToStackSlot(MachineBasicBlock &MBB,
00235                                    MachineBasicBlock::iterator MBBI,
00236                                    unsigned SrcReg, bool isKill, int FrameIndex,
00237                                    const TargetRegisterClass *RC) const;
00238 
00239   virtual void loadRegFromStackSlot(MachineBasicBlock &MBB,
00240                                     MachineBasicBlock::iterator MBBI,
00241                                     unsigned DestReg, int FrameIndex,
00242                                     const TargetRegisterClass *RC) const;
00243 
00244   virtual bool canFoldMemoryOperand(const MachineInstr *MI,
00245                                     const SmallVectorImpl<unsigned> &Ops) const;
00246 
00247   virtual MachineInstr* foldMemoryOperandImpl(MachineFunction &MF,
00248                                               MachineInstr* MI,
00249                                               const SmallVectorImpl<unsigned> &Ops,
00250                                               int FrameIndex) const;
00251 
00252   virtual MachineInstr* foldMemoryOperandImpl(MachineFunction &MF,
00253                                               MachineInstr* MI,
00254                                               const SmallVectorImpl<unsigned> &Ops,
00255                                               MachineInstr* LoadMI) const;
00256 
00257 };
00258 
00259 static inline
00260 const MachineInstrBuilder &AddDefaultPred(const MachineInstrBuilder &MIB) {
00261   return MIB.addImm((int64_t)ARMCC::AL).addReg(0);
00262 }
00263 
00264 static inline
00265 const MachineInstrBuilder &AddDefaultCC(const MachineInstrBuilder &MIB) {
00266   return MIB.addReg(0);
00267 }
00268 
00269 static inline
00270 const MachineInstrBuilder &AddDefaultT1CC(const MachineInstrBuilder &MIB,
00271                                           bool isDead = false) {
00272   return MIB.addReg(ARM::CPSR, getDefRegState(true) | getDeadRegState(isDead));
00273 }
00274 
00275 static inline
00276 const MachineInstrBuilder &AddNoT1CC(const MachineInstrBuilder &MIB) {
00277   return MIB.addReg(0);
00278 }
00279 
00280 static inline
00281 bool isUncondBranchOpcode(int Opc) {
00282   return Opc == ARM::B || Opc == ARM::tB || Opc == ARM::t2B;
00283 }
00284 
00285 static inline
00286 bool isCondBranchOpcode(int Opc) {
00287   return Opc == ARM::Bcc || Opc == ARM::tBcc || Opc == ARM::t2Bcc;
00288 }
00289 
00290 static inline
00291 bool isJumpTableBranchOpcode(int Opc) {
00292   return Opc == ARM::BR_JTr || Opc == ARM::BR_JTm || Opc == ARM::BR_JTadd ||
00293     Opc == ARM::tBR_JTr || Opc == ARM::t2BR_JT;
00294 }
00295 
00296 /// getInstrPredicate - If instruction is predicated, returns its predicate
00297 /// condition, otherwise returns AL. It also returns the condition code
00298 /// register by reference.
00299 ARMCC::CondCodes getInstrPredicate(MachineInstr *MI, unsigned &PredReg);
00300 
00301 int getMatchingCondBranchOpcode(int Opc);
00302 
00303 /// emitARMRegPlusImmediate / emitT2RegPlusImmediate - Emits a series of
00304 /// instructions to materializea destreg = basereg + immediate in ARM / Thumb2
00305 /// code.
00306 void emitARMRegPlusImmediate(MachineBasicBlock &MBB,
00307                              MachineBasicBlock::iterator &MBBI, DebugLoc dl,
00308                              unsigned DestReg, unsigned BaseReg, int NumBytes,
00309                              ARMCC::CondCodes Pred, unsigned PredReg,
00310                              const ARMBaseInstrInfo &TII);
00311 
00312 void emitT2RegPlusImmediate(MachineBasicBlock &MBB,
00313                             MachineBasicBlock::iterator &MBBI, DebugLoc dl,
00314                             unsigned DestReg, unsigned BaseReg, int NumBytes,
00315                             ARMCC::CondCodes Pred, unsigned PredReg,
00316                             const ARMBaseInstrInfo &TII);
00317 
00318 
00319 /// rewriteARMFrameIndex / rewriteT2FrameIndex -
00320 /// Rewrite MI to access 'Offset' bytes from the FP. Return the offset that
00321 /// could not be handled directly in MI.
00322 int rewriteARMFrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
00323                                unsigned FrameReg, int Offset,
00324                                const ARMBaseInstrInfo &TII);
00325 
00326 int rewriteT2FrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
00327                         unsigned FrameReg, int Offset,
00328                         const ARMBaseInstrInfo &TII);
00329 
00330 } // End llvm namespace
00331 
00332 #endif