LLVM API Documentation

MCInstrDesc.h
Go to the documentation of this file.
00001 //===-- llvm/MC/MCInstrDesc.h - Instruction Descriptors -*- 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 defines the MCOperandInfo and MCInstrDesc classes, which
00011 // are used to describe target instructions and their operands.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_MC_MCINSTRDESC_H
00016 #define LLVM_MC_MCINSTRDESC_H
00017 
00018 #include "llvm/MC/MCInst.h"
00019 #include "llvm/MC/MCRegisterInfo.h"
00020 #include "llvm/Support/DataTypes.h"
00021 
00022 namespace llvm {
00023 
00024 //===----------------------------------------------------------------------===//
00025 // Machine Operand Flags and Description
00026 //===----------------------------------------------------------------------===//
00027 
00028 namespace MCOI {
00029   // Operand constraints
00030   enum OperandConstraint {
00031     TIED_TO = 0,    // Must be allocated the same register as.
00032     EARLY_CLOBBER   // Operand is an early clobber register operand
00033   };
00034 
00035   /// OperandFlags - These are flags set on operands, but should be considered
00036   /// private, all access should go through the MCOperandInfo accessors.
00037   /// See the accessors for a description of what these are.
00038   enum OperandFlags {
00039     LookupPtrRegClass = 0,
00040     Predicate,
00041     OptionalDef
00042   };
00043 
00044   /// Operand Type - Operands are tagged with one of the values of this enum.
00045   enum OperandType {
00046     OPERAND_UNKNOWN,
00047     OPERAND_IMMEDIATE,
00048     OPERAND_REGISTER,
00049     OPERAND_MEMORY,
00050     OPERAND_PCREL
00051   };
00052 }
00053 
00054 /// MCOperandInfo - This holds information about one operand of a machine
00055 /// instruction, indicating the register class for register operands, etc.
00056 ///
00057 class MCOperandInfo {
00058 public:
00059   /// RegClass - This specifies the register class enumeration of the operand
00060   /// if the operand is a register.  If isLookupPtrRegClass is set, then this is
00061   /// an index that is passed to TargetRegisterInfo::getPointerRegClass(x) to
00062   /// get a dynamic register class.
00063   int16_t RegClass;
00064 
00065   /// Flags - These are flags from the MCOI::OperandFlags enum.
00066   uint8_t Flags;
00067 
00068   /// OperandType - Information about the type of the operand.
00069   uint8_t OperandType;
00070 
00071   /// Lower 16 bits are used to specify which constraints are set. The higher 16
00072   /// bits are used to specify the value of constraints (4 bits each).
00073   uint32_t Constraints;
00074   /// Currently no other information.
00075 
00076   /// isLookupPtrRegClass - Set if this operand is a pointer value and it
00077   /// requires a callback to look up its register class.
00078   bool isLookupPtrRegClass() const {return Flags&(1 <<MCOI::LookupPtrRegClass);}
00079 
00080   /// isPredicate - Set if this is one of the operands that made up of
00081   /// the predicate operand that controls an isPredicable() instruction.
00082   bool isPredicate() const { return Flags & (1 << MCOI::Predicate); }
00083 
00084   /// isOptionalDef - Set if this operand is a optional def.
00085   ///
00086   bool isOptionalDef() const { return Flags & (1 << MCOI::OptionalDef); }
00087 };
00088 
00089 
00090 //===----------------------------------------------------------------------===//
00091 // Machine Instruction Flags and Description
00092 //===----------------------------------------------------------------------===//
00093 
00094 /// MCInstrDesc flags - These should be considered private to the
00095 /// implementation of the MCInstrDesc class.  Clients should use the predicate
00096 /// methods on MCInstrDesc, not use these directly.  These all correspond to
00097 /// bitfields in the MCInstrDesc::Flags field.
00098 namespace MCID {
00099   enum {
00100     Variadic = 0,
00101     HasOptionalDef,
00102     Pseudo,
00103     Return,
00104     Call,
00105     Barrier,
00106     Terminator,
00107     Branch,
00108     IndirectBranch,
00109     Compare,
00110     MoveImm,
00111     Bitcast,
00112     Select,
00113     DelaySlot,
00114     FoldableAsLoad,
00115     MayLoad,
00116     MayStore,
00117     Predicable,
00118     NotDuplicable,
00119     UnmodeledSideEffects,
00120     Commutable,
00121     ConvertibleTo3Addr,
00122     UsesCustomInserter,
00123     HasPostISelHook,
00124     Rematerializable,
00125     CheapAsAMove,
00126     ExtraSrcRegAllocReq,
00127     ExtraDefRegAllocReq
00128   };
00129 }
00130 
00131 /// MCInstrDesc - Describe properties that are true of each instruction in the
00132 /// target description file.  This captures information about side effects,
00133 /// register use and many other things.  There is one instance of this struct
00134 /// for each target instruction class, and the MachineInstr class points to
00135 /// this struct directly to describe itself.
00136 class MCInstrDesc {
00137 public:
00138   unsigned short  Opcode;        // The opcode number
00139   unsigned short  NumOperands;   // Num of args (may be more if variable_ops)
00140   unsigned short  NumDefs;       // Num of args that are definitions
00141   unsigned short  SchedClass;    // enum identifying instr sched class
00142   unsigned short  Size;          // Number of bytes in encoding.
00143   unsigned        Flags;         // Flags identifying machine instr class
00144   uint64_t        TSFlags;       // Target Specific Flag values
00145   const uint16_t *ImplicitUses;  // Registers implicitly read by this instr
00146   const uint16_t *ImplicitDefs;  // Registers implicitly defined by this instr
00147   const MCOperandInfo *OpInfo;   // 'NumOperands' entries about operands
00148 
00149   /// \brief Returns the value of the specific constraint if
00150   /// it is set. Returns -1 if it is not set.
00151   int getOperandConstraint(unsigned OpNum,
00152                            MCOI::OperandConstraint Constraint) const {
00153     if (OpNum < NumOperands &&
00154         (OpInfo[OpNum].Constraints & (1 << Constraint))) {
00155       unsigned Pos = 16 + Constraint * 4;
00156       return (int)(OpInfo[OpNum].Constraints >> Pos) & 0xf;
00157     }
00158     return -1;
00159   }
00160 
00161   /// \brief Return the opcode number for this descriptor.
00162   unsigned getOpcode() const {
00163     return Opcode;
00164   }
00165 
00166   /// \brief Return the number of declared MachineOperands for this
00167   /// MachineInstruction.  Note that variadic (isVariadic() returns true)
00168   /// instructions may have additional operands at the end of the list, and note
00169   /// that the machine instruction may include implicit register def/uses as
00170   /// well.
00171   unsigned getNumOperands() const {
00172     return NumOperands;
00173   }
00174 
00175   /// \brief Return the number of MachineOperands that are register
00176   /// definitions.  Register definitions always occur at the start of the
00177   /// machine operand list.  This is the number of "outs" in the .td file,
00178   /// and does not include implicit defs.
00179   unsigned getNumDefs() const {
00180     return NumDefs;
00181   }
00182 
00183   /// \brief Return flags of this instruction.
00184   unsigned getFlags() const { return Flags; }
00185 
00186   /// \brief Return true if this instruction can have a variable number of
00187   /// operands.  In this case, the variable operands will be after the normal
00188   /// operands but before the implicit definitions and uses (if any are
00189   /// present).
00190   bool isVariadic() const {
00191     return Flags & (1 << MCID::Variadic);
00192   }
00193 
00194   /// \brief Set if this instruction has an optional definition, e.g.
00195   /// ARM instructions which can set condition code if 's' bit is set.
00196   bool hasOptionalDef() const {
00197     return Flags & (1 << MCID::HasOptionalDef);
00198   }
00199 
00200   /// \brief Return true if this is a pseudo instruction that doesn't
00201   /// correspond to a real machine instruction.
00202   ///
00203   bool isPseudo() const {
00204     return Flags & (1 << MCID::Pseudo);
00205   }
00206 
00207   /// \brief Return true if the instruction is a return.
00208   bool isReturn() const {
00209     return Flags & (1 << MCID::Return);
00210   }
00211 
00212   /// \brief  Return true if the instruction is a call.
00213   bool isCall() const {
00214     return Flags & (1 << MCID::Call);
00215   }
00216 
00217   /// \brief Returns true if the specified instruction stops control flow
00218   /// from executing the instruction immediately following it.  Examples include
00219   /// unconditional branches and return instructions.
00220   bool isBarrier() const {
00221     return Flags & (1 << MCID::Barrier);
00222   }
00223 
00224   /// \brief Returns true if this instruction part of the terminator for
00225   /// a basic block.  Typically this is things like return and branch
00226   /// instructions.
00227   ///
00228   /// Various passes use this to insert code into the bottom of a basic block,
00229   /// but before control flow occurs.
00230   bool isTerminator() const {
00231     return Flags & (1 << MCID::Terminator);
00232   }
00233 
00234   /// \brief Returns true if this is a conditional, unconditional, or
00235   /// indirect branch.  Predicates below can be used to discriminate between
00236   /// these cases, and the TargetInstrInfo::AnalyzeBranch method can be used to
00237   /// get more information.
00238   bool isBranch() const {
00239     return Flags & (1 << MCID::Branch);
00240   }
00241 
00242   /// \brief Return true if this is an indirect branch, such as a
00243   /// branch through a register.
00244   bool isIndirectBranch() const {
00245     return Flags & (1 << MCID::IndirectBranch);
00246   }
00247 
00248   /// \brief Return true if this is a branch which may fall
00249   /// through to the next instruction or may transfer control flow to some other
00250   /// block.  The TargetInstrInfo::AnalyzeBranch method can be used to get more
00251   /// information about this branch.
00252   bool isConditionalBranch() const {
00253     return isBranch() & !isBarrier() & !isIndirectBranch();
00254   }
00255 
00256   /// \brief Return true if this is a branch which always
00257   /// transfers control flow to some other block.  The
00258   /// TargetInstrInfo::AnalyzeBranch method can be used to get more information
00259   /// about this branch.
00260   bool isUnconditionalBranch() const {
00261     return isBranch() & isBarrier() & !isIndirectBranch();
00262   }
00263 
00264   /// \brief Return true if this is a branch or an instruction which directly
00265   /// writes to the program counter. Considered 'may' affect rather than
00266   /// 'does' affect as things like predication are not taken into account.
00267   bool mayAffectControlFlow(const MCInst &MI, const MCRegisterInfo &RI) const {
00268     if (isBranch() || isCall() || isReturn() || isIndirectBranch())
00269       return true;
00270     unsigned PC = RI.getProgramCounter();
00271     if (PC == 0) return false;
00272     return hasDefOfPhysReg(MI, PC, RI);
00273   }
00274 
00275   /// \brief Return true if this instruction has a predicate operand
00276   /// that controls execution. It may be set to 'always', or may be set to other
00277   /// values. There are various methods in TargetInstrInfo that can be used to
00278   /// control and modify the predicate in this instruction.
00279   bool isPredicable() const {
00280     return Flags & (1 << MCID::Predicable);
00281   }
00282 
00283   /// \brief Return true if this instruction is a comparison.
00284   bool isCompare() const {
00285     return Flags & (1 << MCID::Compare);
00286   }
00287 
00288   /// \brief Return true if this instruction is a move immediate
00289   /// (including conditional moves) instruction.
00290   bool isMoveImmediate() const {
00291     return Flags & (1 << MCID::MoveImm);
00292   }
00293 
00294   /// \brief Return true if this instruction is a bitcast instruction.
00295   bool isBitcast() const {
00296     return Flags & (1 << MCID::Bitcast);
00297   }
00298 
00299   /// \brief Return true if this is a select instruction.
00300   bool isSelect() const {
00301     return Flags & (1 << MCID::Select);
00302   }
00303 
00304   /// \brief Return true if this instruction cannot be safely
00305   /// duplicated.  For example, if the instruction has a unique labels attached
00306   /// to it, duplicating it would cause multiple definition errors.
00307   bool isNotDuplicable() const {
00308     return Flags & (1 << MCID::NotDuplicable);
00309   }
00310 
00311   /// hasDelaySlot - Returns true if the specified instruction has a delay slot
00312   /// which must be filled by the code generator.
00313   bool hasDelaySlot() const {
00314     return Flags & (1 << MCID::DelaySlot);
00315   }
00316 
00317   /// canFoldAsLoad - Return true for instructions that can be folded as
00318   /// memory operands in other instructions. The most common use for this
00319   /// is instructions that are simple loads from memory that don't modify
00320   /// the loaded value in any way, but it can also be used for instructions
00321   /// that can be expressed as constant-pool loads, such as V_SETALLONES
00322   /// on x86, to allow them to be folded when it is beneficial.
00323   /// This should only be set on instructions that return a value in their
00324   /// only virtual register definition.
00325   bool canFoldAsLoad() const {
00326     return Flags & (1 << MCID::FoldableAsLoad);
00327   }
00328 
00329   //===--------------------------------------------------------------------===//
00330   // Side Effect Analysis
00331   //===--------------------------------------------------------------------===//
00332 
00333   /// \brief Return true if this instruction could possibly read memory.
00334   /// Instructions with this flag set are not necessarily simple load
00335   /// instructions, they may load a value and modify it, for example.
00336   bool mayLoad() const {
00337     return Flags & (1 << MCID::MayLoad);
00338   }
00339 
00340 
00341   /// \brief Return true if this instruction could possibly modify memory.
00342   /// Instructions with this flag set are not necessarily simple store
00343   /// instructions, they may store a modified value based on their operands, or
00344   /// may not actually modify anything, for example.
00345   bool mayStore() const {
00346     return Flags & (1 << MCID::MayStore);
00347   }
00348 
00349   /// hasUnmodeledSideEffects - Return true if this instruction has side
00350   /// effects that are not modeled by other flags.  This does not return true
00351   /// for instructions whose effects are captured by:
00352   ///
00353   ///  1. Their operand list and implicit definition/use list.  Register use/def
00354   ///     info is explicit for instructions.
00355   ///  2. Memory accesses.  Use mayLoad/mayStore.
00356   ///  3. Calling, branching, returning: use isCall/isReturn/isBranch.
00357   ///
00358   /// Examples of side effects would be modifying 'invisible' machine state like
00359   /// a control register, flushing a cache, modifying a register invisible to
00360   /// LLVM, etc.
00361   ///
00362   bool hasUnmodeledSideEffects() const {
00363     return Flags & (1 << MCID::UnmodeledSideEffects);
00364   }
00365 
00366   //===--------------------------------------------------------------------===//
00367   // Flags that indicate whether an instruction can be modified by a method.
00368   //===--------------------------------------------------------------------===//
00369 
00370   /// isCommutable - Return true if this may be a 2- or 3-address
00371   /// instruction (of the form "X = op Y, Z, ..."), which produces the same
00372   /// result if Y and Z are exchanged.  If this flag is set, then the
00373   /// TargetInstrInfo::commuteInstruction method may be used to hack on the
00374   /// instruction.
00375   ///
00376   /// Note that this flag may be set on instructions that are only commutable
00377   /// sometimes.  In these cases, the call to commuteInstruction will fail.
00378   /// Also note that some instructions require non-trivial modification to
00379   /// commute them.
00380   bool isCommutable() const {
00381     return Flags & (1 << MCID::Commutable);
00382   }
00383 
00384   /// isConvertibleTo3Addr - Return true if this is a 2-address instruction
00385   /// which can be changed into a 3-address instruction if needed.  Doing this
00386   /// transformation can be profitable in the register allocator, because it
00387   /// means that the instruction can use a 2-address form if possible, but
00388   /// degrade into a less efficient form if the source and dest register cannot
00389   /// be assigned to the same register.  For example, this allows the x86
00390   /// backend to turn a "shl reg, 3" instruction into an LEA instruction, which
00391   /// is the same speed as the shift but has bigger code size.
00392   ///
00393   /// If this returns true, then the target must implement the
00394   /// TargetInstrInfo::convertToThreeAddress method for this instruction, which
00395   /// is allowed to fail if the transformation isn't valid for this specific
00396   /// instruction (e.g. shl reg, 4 on x86).
00397   ///
00398   bool isConvertibleTo3Addr() const {
00399     return Flags & (1 << MCID::ConvertibleTo3Addr);
00400   }
00401 
00402   /// usesCustomInsertionHook - Return true if this instruction requires
00403   /// custom insertion support when the DAG scheduler is inserting it into a
00404   /// machine basic block.  If this is true for the instruction, it basically
00405   /// means that it is a pseudo instruction used at SelectionDAG time that is
00406   /// expanded out into magic code by the target when MachineInstrs are formed.
00407   ///
00408   /// If this is true, the TargetLoweringInfo::InsertAtEndOfBasicBlock method
00409   /// is used to insert this into the MachineBasicBlock.
00410   bool usesCustomInsertionHook() const {
00411     return Flags & (1 << MCID::UsesCustomInserter);
00412   }
00413 
00414   /// hasPostISelHook - Return true if this instruction requires *adjustment*
00415   /// after instruction selection by calling a target hook. For example, this
00416   /// can be used to fill in ARM 's' optional operand depending on whether
00417   /// the conditional flag register is used.
00418   bool hasPostISelHook() const {
00419     return Flags & (1 << MCID::HasPostISelHook);
00420   }
00421 
00422   /// isRematerializable - Returns true if this instruction is a candidate for
00423   /// remat.  This flag is deprecated, please don't use it anymore.  If this
00424   /// flag is set, the isReallyTriviallyReMaterializable() method is called to
00425   /// verify the instruction is really rematable.
00426   bool isRematerializable() const {
00427     return Flags & (1 << MCID::Rematerializable);
00428   }
00429 
00430   /// isAsCheapAsAMove - Returns true if this instruction has the same cost (or
00431   /// less) than a move instruction. This is useful during certain types of
00432   /// optimizations (e.g., remat during two-address conversion or machine licm)
00433   /// where we would like to remat or hoist the instruction, but not if it costs
00434   /// more than moving the instruction into the appropriate register. Note, we
00435   /// are not marking copies from and to the same register class with this flag.
00436   bool isAsCheapAsAMove() const {
00437     return Flags & (1 << MCID::CheapAsAMove);
00438   }
00439 
00440   /// hasExtraSrcRegAllocReq - Returns true if this instruction source operands
00441   /// have special register allocation requirements that are not captured by the
00442   /// operand register classes. e.g. ARM::STRD's two source registers must be an
00443   /// even / odd pair, ARM::STM registers have to be in ascending order.
00444   /// Post-register allocation passes should not attempt to change allocations
00445   /// for sources of instructions with this flag.
00446   bool hasExtraSrcRegAllocReq() const {
00447     return Flags & (1 << MCID::ExtraSrcRegAllocReq);
00448   }
00449 
00450   /// hasExtraDefRegAllocReq - Returns true if this instruction def operands
00451   /// have special register allocation requirements that are not captured by the
00452   /// operand register classes. e.g. ARM::LDRD's two def registers must be an
00453   /// even / odd pair, ARM::LDM registers have to be in ascending order.
00454   /// Post-register allocation passes should not attempt to change allocations
00455   /// for definitions of instructions with this flag.
00456   bool hasExtraDefRegAllocReq() const {
00457     return Flags & (1 << MCID::ExtraDefRegAllocReq);
00458   }
00459 
00460 
00461   /// getImplicitUses - Return a list of registers that are potentially
00462   /// read by any instance of this machine instruction.  For example, on X86,
00463   /// the "adc" instruction adds two register operands and adds the carry bit in
00464   /// from the flags register.  In this case, the instruction is marked as
00465   /// implicitly reading the flags.  Likewise, the variable shift instruction on
00466   /// X86 is marked as implicitly reading the 'CL' register, which it always
00467   /// does.
00468   ///
00469   /// This method returns null if the instruction has no implicit uses.
00470   const uint16_t *getImplicitUses() const {
00471     return ImplicitUses;
00472   }
00473 
00474   /// \brief Return the number of implicit uses this instruction has.
00475   unsigned getNumImplicitUses() const {
00476     if (ImplicitUses == 0) return 0;
00477     unsigned i = 0;
00478     for (; ImplicitUses[i]; ++i) /*empty*/;
00479     return i;
00480   }
00481 
00482   /// getImplicitDefs - Return a list of registers that are potentially
00483   /// written by any instance of this machine instruction.  For example, on X86,
00484   /// many instructions implicitly set the flags register.  In this case, they
00485   /// are marked as setting the FLAGS.  Likewise, many instructions always
00486   /// deposit their result in a physical register.  For example, the X86 divide
00487   /// instruction always deposits the quotient and remainder in the EAX/EDX
00488   /// registers.  For that instruction, this will return a list containing the
00489   /// EAX/EDX/EFLAGS registers.
00490   ///
00491   /// This method returns null if the instruction has no implicit defs.
00492   const uint16_t *getImplicitDefs() const {
00493     return ImplicitDefs;
00494   }
00495 
00496   /// \brief Return the number of implicit defs this instruct has.
00497   unsigned getNumImplicitDefs() const {
00498     if (ImplicitDefs == 0) return 0;
00499     unsigned i = 0;
00500     for (; ImplicitDefs[i]; ++i) /*empty*/;
00501     return i;
00502   }
00503 
00504   /// \brief Return true if this instruction implicitly
00505   /// uses the specified physical register.
00506   bool hasImplicitUseOfPhysReg(unsigned Reg) const {
00507     if (const uint16_t *ImpUses = ImplicitUses)
00508       for (; *ImpUses; ++ImpUses)
00509         if (*ImpUses == Reg) return true;
00510     return false;
00511   }
00512 
00513   /// \brief Return true if this instruction implicitly
00514   /// defines the specified physical register.
00515   bool hasImplicitDefOfPhysReg(unsigned Reg,
00516                                const MCRegisterInfo *MRI = 0) const {
00517     if (const uint16_t *ImpDefs = ImplicitDefs)
00518       for (; *ImpDefs; ++ImpDefs)
00519         if (*ImpDefs == Reg || (MRI && MRI->isSubRegister(Reg, *ImpDefs)))
00520             return true;
00521     return false;
00522   }
00523 
00524   /// \brief Return true if this instruction defines the specified physical
00525   /// register, either explicitly or implicitly.
00526   bool hasDefOfPhysReg(const MCInst &MI, unsigned Reg,
00527                        const MCRegisterInfo &RI) const {
00528     for (int i = 0, e = NumDefs; i != e; ++i)
00529       if (MI.getOperand(i).isReg() &&
00530           RI.isSubRegisterEq(Reg, MI.getOperand(i).getReg()))
00531         return true;
00532     return hasImplicitDefOfPhysReg(Reg, &RI);
00533   }
00534 
00535   /// \brief Return the scheduling class for this instruction.  The
00536   /// scheduling class is an index into the InstrItineraryData table.  This
00537   /// returns zero if there is no known scheduling information for the
00538   /// instruction.
00539   unsigned getSchedClass() const {
00540     return SchedClass;
00541   }
00542 
00543   /// \brief Return the number of bytes in the encoding of this instruction,
00544   /// or zero if the encoding size cannot be known from the opcode.
00545   unsigned getSize() const {
00546     return Size;
00547   }
00548 
00549   /// \brief Find the index of the first operand in the
00550   /// operand list that is used to represent the predicate. It returns -1 if
00551   /// none is found.
00552   int findFirstPredOperandIdx() const {
00553     if (isPredicable()) {
00554       for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
00555         if (OpInfo[i].isPredicate())
00556           return i;
00557     }
00558     return -1;
00559   }
00560 };
00561 
00562 } // end namespace llvm
00563 
00564 #endif