LLVM API Documentation

TargetInstrDesc.h

Go to the documentation of this file.
00001 //===-- llvm/Target/TargetInstrDesc.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 TargetOperandInfo and TargetInstrDesc classes, which
00011 // are used to describe target instructions and their operands. 
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_TARGET_TARGETINSTRDESC_H
00016 #define LLVM_TARGET_TARGETINSTRDESC_H
00017 
00018 namespace llvm {
00019 
00020 class TargetRegisterClass;
00021 class TargetRegisterInfo;
00022   
00023 //===----------------------------------------------------------------------===//
00024 // Machine Operand Flags and Description
00025 //===----------------------------------------------------------------------===//
00026   
00027 namespace TOI {
00028   // Operand constraints: only "tied_to" for now.
00029   enum OperandConstraint {
00030     TIED_TO = 0  // Must be allocated the same register as.
00031   };
00032   
00033   /// OperandFlags - These are flags set on operands, but should be considered
00034   /// private, all access should go through the TargetOperandInfo accessors.
00035   /// See the accessors for a description of what these are.
00036   enum OperandFlags {
00037     LookupPtrRegClass = 0,
00038     Predicate,
00039     OptionalDef
00040   };
00041 }
00042 
00043 /// TargetOperandInfo - This holds information about one operand of a machine
00044 /// instruction, indicating the register class for register operands, etc.
00045 ///
00046 class TargetOperandInfo {
00047 public:
00048   /// RegClass - This specifies the register class enumeration of the operand 
00049   /// if the operand is a register.  If isLookupPtrRegClass is set, then this is
00050   /// an index that is passed to TargetRegisterInfo::getPointerRegClass(x) to
00051   /// get a dynamic register class.
00052   ///
00053   /// NOTE: This member should be considered to be private, all access should go
00054   /// through "getRegClass(TRI)" below.
00055   unsigned short RegClass;
00056   
00057   /// Flags - These are flags from the TOI::OperandFlags enum.
00058   unsigned short Flags;
00059   
00060   /// Lower 16 bits are used to specify which constraints are set. The higher 16
00061   /// bits are used to specify the value of constraints (4 bits each).
00062   unsigned Constraints;
00063   /// Currently no other information.
00064   
00065   /// getRegClass - Get the register class for the operand, handling resolution
00066   /// of "symbolic" pointer register classes etc.  If this is not a register
00067   /// operand, this returns null.
00068   const TargetRegisterClass *getRegClass(const TargetRegisterInfo *TRI) const;
00069   
00070   
00071   /// isLookupPtrRegClass - Set if this operand is a pointer value and it
00072   /// requires a callback to look up its register class.
00073   bool isLookupPtrRegClass() const { return Flags&(1 <<TOI::LookupPtrRegClass);}
00074   
00075   /// isPredicate - Set if this is one of the operands that made up of
00076   /// the predicate operand that controls an isPredicable() instruction.
00077   bool isPredicate() const { return Flags & (1 << TOI::Predicate); }
00078   
00079   /// isOptionalDef - Set if this operand is a optional def.
00080   ///
00081   bool isOptionalDef() const { return Flags & (1 << TOI::OptionalDef); }
00082 };
00083 
00084   
00085 //===----------------------------------------------------------------------===//
00086 // Machine Instruction Flags and Description
00087 //===----------------------------------------------------------------------===//
00088 
00089 /// TargetInstrDesc flags - These should be considered private to the
00090 /// implementation of the TargetInstrDesc class.  Clients should use the
00091 /// predicate methods on TargetInstrDesc, not use these directly.  These
00092 /// all correspond to bitfields in the TargetInstrDesc::Flags field.
00093 namespace TID {
00094   enum {
00095     Variadic = 0,
00096     HasOptionalDef,
00097     Return,
00098     Call,
00099     Barrier,
00100     Terminator,
00101     Branch,
00102     IndirectBranch,
00103     Predicable,
00104     NotDuplicable,
00105     DelaySlot,
00106     FoldableAsLoad,
00107     MayLoad,
00108     MayStore,
00109     UnmodeledSideEffects,
00110     Commutable,
00111     ConvertibleTo3Addr,
00112     UsesCustomDAGSchedInserter,
00113     Rematerializable,
00114     CheapAsAMove
00115   };
00116 }
00117 
00118 /// TargetInstrDesc - Describe properties that are true of each
00119 /// instruction in the target description file.  This captures information about
00120 /// side effects, register use and many other things.  There is one instance of
00121 /// this struct for each target instruction class, and the MachineInstr class
00122 /// points to this struct directly to describe itself.
00123 class TargetInstrDesc {
00124 public:
00125   unsigned short  Opcode;        // The opcode number
00126   unsigned short  NumOperands;   // Num of args (may be more if variable_ops)
00127   unsigned short  NumDefs;       // Num of args that are definitions
00128   unsigned short  SchedClass;    // enum identifying instr sched class
00129   const char *    Name;          // Name of the instruction record in td file
00130   unsigned        Flags;         // Flags identifying machine instr class
00131   unsigned        TSFlags;       // Target Specific Flag values
00132   const unsigned *ImplicitUses;  // Registers implicitly read by this instr
00133   const unsigned *ImplicitDefs;  // Registers implicitly defined by this instr
00134   const TargetRegisterClass **RCBarriers; // Reg classes completely "clobbered"
00135   const TargetOperandInfo *OpInfo; // 'NumOperands' entries about operands
00136 
00137   /// getOperandConstraint - Returns the value of the specific constraint if
00138   /// it is set. Returns -1 if it is not set.
00139   int getOperandConstraint(unsigned OpNum,
00140                            TOI::OperandConstraint Constraint) const {
00141     if (OpNum < NumOperands &&
00142         (OpInfo[OpNum].Constraints & (1 << Constraint))) {
00143       unsigned Pos = 16 + Constraint * 4;
00144       return (int)(OpInfo[OpNum].Constraints >> Pos) & 0xf;
00145     }
00146     return -1;
00147   }
00148 
00149   /// getOpcode - Return the opcode number for this descriptor.
00150   unsigned getOpcode() const {
00151     return Opcode;
00152   }
00153   
00154   /// getName - Return the name of the record in the .td file for this
00155   /// instruction, for example "ADD8ri".
00156   const char *getName() const {
00157     return Name;
00158   }
00159   
00160   /// getNumOperands - Return the number of declared MachineOperands for this
00161   /// MachineInstruction.  Note that variadic (isVariadic() returns true)
00162   /// instructions may have additional operands at the end of the list, and note
00163   /// that the machine instruction may include implicit register def/uses as
00164   /// well.
00165   unsigned getNumOperands() const {
00166     return NumOperands;
00167   }
00168   
00169   /// getNumDefs - Return the number of MachineOperands that are register
00170   /// definitions.  Register definitions always occur at the start of the 
00171   /// machine operand list.  This is the number of "outs" in the .td file,
00172   /// and does not include implicit defs.
00173   unsigned getNumDefs() const {
00174     return NumDefs;
00175   }
00176   
00177   /// isVariadic - Return true if this instruction can have a variable number of
00178   /// operands.  In this case, the variable operands will be after the normal
00179   /// operands but before the implicit definitions and uses (if any are
00180   /// present).
00181   bool isVariadic() const {
00182     return Flags & (1 << TID::Variadic);
00183   }
00184   
00185   /// hasOptionalDef - Set if this instruction has an optional definition, e.g.
00186   /// ARM instructions which can set condition code if 's' bit is set.
00187   bool hasOptionalDef() const {
00188     return Flags & (1 << TID::HasOptionalDef);
00189   }
00190   
00191   /// getImplicitUses - Return a list of registers that are potentially
00192   /// read by any instance of this machine instruction.  For example, on X86,
00193   /// the "adc" instruction adds two register operands and adds the carry bit in
00194   /// from the flags register.  In this case, the instruction is marked as
00195   /// implicitly reading the flags.  Likewise, the variable shift instruction on
00196   /// X86 is marked as implicitly reading the 'CL' register, which it always
00197   /// does.
00198   ///
00199   /// This method returns null if the instruction has no implicit uses.
00200   const unsigned *getImplicitUses() const {
00201     return ImplicitUses;
00202   }
00203   
00204   /// getImplicitDefs - Return a list of registers that are potentially
00205   /// written by any instance of this machine instruction.  For example, on X86,
00206   /// many instructions implicitly set the flags register.  In this case, they
00207   /// are marked as setting the FLAGS.  Likewise, many instructions always
00208   /// deposit their result in a physical register.  For example, the X86 divide
00209   /// instruction always deposits the quotient and remainder in the EAX/EDX
00210   /// registers.  For that instruction, this will return a list containing the
00211   /// EAX/EDX/EFLAGS registers.
00212   ///
00213   /// This method returns null if the instruction has no implicit defs.
00214   const unsigned *getImplicitDefs() const {
00215     return ImplicitDefs;
00216   }
00217   
00218   /// hasImplicitUseOfPhysReg - Return true if this instruction implicitly
00219   /// uses the specified physical register.
00220   bool hasImplicitUseOfPhysReg(unsigned Reg) const {
00221     if (const unsigned *ImpUses = ImplicitUses)
00222       for (; *ImpUses; ++ImpUses)
00223         if (*ImpUses == Reg) return true;
00224     return false;
00225   }
00226   
00227   /// hasImplicitDefOfPhysReg - Return true if this instruction implicitly
00228   /// defines the specified physical register.
00229   bool hasImplicitDefOfPhysReg(unsigned Reg) const {
00230     if (const unsigned *ImpDefs = ImplicitDefs)
00231       for (; *ImpDefs; ++ImpDefs)
00232         if (*ImpDefs == Reg) return true;
00233     return false;
00234   }
00235 
00236   /// getRegClassBarriers - Return a list of register classes that are
00237   /// completely clobbered by this machine instruction. For example, on X86
00238   /// the call instructions will completely clobber all the registers in the
00239   /// fp stack and XMM classes.
00240   ///
00241   /// This method returns null if the instruction doesn't completely clobber
00242   /// any register class.
00243   const TargetRegisterClass **getRegClassBarriers() const {
00244     return RCBarriers;
00245   }
00246 
00247   /// getSchedClass - Return the scheduling class for this instruction.  The
00248   /// scheduling class is an index into the InstrItineraryData table.  This
00249   /// returns zero if there is no known scheduling information for the
00250   /// instruction.
00251   ///
00252   unsigned getSchedClass() const {
00253     return SchedClass;
00254   }
00255   
00256   bool isReturn() const {
00257     return Flags & (1 << TID::Return);
00258   }
00259   
00260   bool isCall() const {
00261     return Flags & (1 << TID::Call);
00262   }
00263   
00264   /// isBarrier - Returns true if the specified instruction stops control flow
00265   /// from executing the instruction immediately following it.  Examples include
00266   /// unconditional branches and return instructions.
00267   bool isBarrier() const {
00268     return Flags & (1 << TID::Barrier);
00269   }
00270   
00271   /// isTerminator - Returns true if this instruction part of the terminator for
00272   /// a basic block.  Typically this is things like return and branch
00273   /// instructions.
00274   ///
00275   /// Various passes use this to insert code into the bottom of a basic block,
00276   /// but before control flow occurs.
00277   bool isTerminator() const {
00278     return Flags & (1 << TID::Terminator);
00279   }
00280   
00281   /// isBranch - Returns true if this is a conditional, unconditional, or
00282   /// indirect branch.  Predicates below can be used to discriminate between
00283   /// these cases, and the TargetInstrInfo::AnalyzeBranch method can be used to
00284   /// get more information.
00285   bool isBranch() const {
00286     return Flags & (1 << TID::Branch);
00287   }
00288 
00289   /// isIndirectBranch - Return true if this is an indirect branch, such as a
00290   /// branch through a register.
00291   bool isIndirectBranch() const {
00292     return Flags & (1 << TID::IndirectBranch);
00293   }
00294   
00295   /// isConditionalBranch - Return true if this is a branch which may fall
00296   /// through to the next instruction or may transfer control flow to some other
00297   /// block.  The TargetInstrInfo::AnalyzeBranch method can be used to get more
00298   /// information about this branch.
00299   bool isConditionalBranch() const {
00300     return isBranch() & !isBarrier() & !isIndirectBranch();
00301   }
00302   
00303   /// isUnconditionalBranch - Return true if this is a branch which always
00304   /// transfers control flow to some other block.  The
00305   /// TargetInstrInfo::AnalyzeBranch method can be used to get more information
00306   /// about this branch.
00307   bool isUnconditionalBranch() const {
00308     return isBranch() & isBarrier() & !isIndirectBranch();
00309   }
00310   
00311   // isPredicable - Return true if this instruction has a predicate operand that
00312   // controls execution.  It may be set to 'always', or may be set to other
00313   /// values.   There are various methods in TargetInstrInfo that can be used to
00314   /// control and modify the predicate in this instruction.
00315   bool isPredicable() const {
00316     return Flags & (1 << TID::Predicable);
00317   }
00318   
00319   /// isNotDuplicable - Return true if this instruction cannot be safely
00320   /// duplicated.  For example, if the instruction has a unique labels attached
00321   /// to it, duplicating it would cause multiple definition errors.
00322   bool isNotDuplicable() const {
00323     return Flags & (1 << TID::NotDuplicable);
00324   }
00325   
00326   /// hasDelaySlot - Returns true if the specified instruction has a delay slot
00327   /// which must be filled by the code generator.
00328   bool hasDelaySlot() const {
00329     return Flags & (1 << TID::DelaySlot);
00330   }
00331   
00332   /// canFoldAsLoad - Return true for instructions that can be folded as
00333   /// memory operands in other instructions. The most common use for this
00334   /// is instructions that are simple loads from memory that don't modify
00335   /// the loaded value in any way, but it can also be used for instructions
00336   /// that can be expressed as constant-pool loads, such as V_SETALLONES
00337   /// on x86, to allow them to be folded when it is beneficial.
00338   /// This should only be set on instructions that return a value in their
00339   /// only virtual register definition.
00340   bool canFoldAsLoad() const {
00341     return Flags & (1 << TID::FoldableAsLoad);
00342   }
00343   
00344   //===--------------------------------------------------------------------===//
00345   // Side Effect Analysis
00346   //===--------------------------------------------------------------------===//
00347 
00348   /// mayLoad - Return true if this instruction could possibly read memory.
00349   /// Instructions with this flag set are not necessarily simple load
00350   /// instructions, they may load a value and modify it, for example.
00351   bool mayLoad() const {
00352     return Flags & (1 << TID::MayLoad);
00353   }
00354   
00355   
00356   /// mayStore - Return true if this instruction could possibly modify memory.
00357   /// Instructions with this flag set are not necessarily simple store
00358   /// instructions, they may store a modified value based on their operands, or
00359   /// may not actually modify anything, for example.
00360   bool mayStore() const {
00361     return Flags & (1 << TID::MayStore);
00362   }
00363   
00364   /// hasUnmodeledSideEffects - Return true if this instruction has side
00365   /// effects that are not modeled by other flags.  This does not return true
00366   /// for instructions whose effects are captured by:
00367   ///
00368   ///  1. Their operand list and implicit definition/use list.  Register use/def
00369   ///     info is explicit for instructions.
00370   ///  2. Memory accesses.  Use mayLoad/mayStore.
00371   ///  3. Calling, branching, returning: use isCall/isReturn/isBranch.
00372   ///
00373   /// Examples of side effects would be modifying 'invisible' machine state like
00374   /// a control register, flushing a cache, modifying a register invisible to
00375   /// LLVM, etc.
00376   ///
00377   bool hasUnmodeledSideEffects() const {
00378     return Flags & (1 << TID::UnmodeledSideEffects);
00379   }
00380   
00381   //===--------------------------------------------------------------------===//
00382   // Flags that indicate whether an instruction can be modified by a method.
00383   //===--------------------------------------------------------------------===//
00384   
00385   /// isCommutable - Return true if this may be a 2- or 3-address
00386   /// instruction (of the form "X = op Y, Z, ..."), which produces the same
00387   /// result if Y and Z are exchanged.  If this flag is set, then the 
00388   /// TargetInstrInfo::commuteInstruction method may be used to hack on the
00389   /// instruction.
00390   ///
00391   /// Note that this flag may be set on instructions that are only commutable
00392   /// sometimes.  In these cases, the call to commuteInstruction will fail.
00393   /// Also note that some instructions require non-trivial modification to
00394   /// commute them.
00395   bool isCommutable() const {
00396     return Flags & (1 << TID::Commutable);
00397   }
00398   
00399   /// isConvertibleTo3Addr - Return true if this is a 2-address instruction
00400   /// which can be changed into a 3-address instruction if needed.  Doing this
00401   /// transformation can be profitable in the register allocator, because it
00402   /// means that the instruction can use a 2-address form if possible, but
00403   /// degrade into a less efficient form if the source and dest register cannot
00404   /// be assigned to the same register.  For example, this allows the x86
00405   /// backend to turn a "shl reg, 3" instruction into an LEA instruction, which
00406   /// is the same speed as the shift but has bigger code size.
00407   ///
00408   /// If this returns true, then the target must implement the
00409   /// TargetInstrInfo::convertToThreeAddress method for this instruction, which
00410   /// is allowed to fail if the transformation isn't valid for this specific
00411   /// instruction (e.g. shl reg, 4 on x86).
00412   ///
00413   bool isConvertibleTo3Addr() const {
00414     return Flags & (1 << TID::ConvertibleTo3Addr);
00415   }
00416   
00417   /// usesCustomDAGSchedInsertionHook - Return true if this instruction requires
00418   /// custom insertion support when the DAG scheduler is inserting it into a
00419   /// machine basic block.  If this is true for the instruction, it basically
00420   /// means that it is a pseudo instruction used at SelectionDAG time that is 
00421   /// expanded out into magic code by the target when MachineInstrs are formed.
00422   ///
00423   /// If this is true, the TargetLoweringInfo::InsertAtEndOfBasicBlock method
00424   /// is used to insert this into the MachineBasicBlock.
00425   bool usesCustomDAGSchedInsertionHook() const {
00426     return Flags & (1 << TID::UsesCustomDAGSchedInserter);
00427   }
00428   
00429   /// isRematerializable - Returns true if this instruction is a candidate for
00430   /// remat.  This flag is deprecated, please don't use it anymore.  If this
00431   /// flag is set, the isReallyTriviallyReMaterializable() method is called to
00432   /// verify the instruction is really rematable.
00433   bool isRematerializable() const {
00434     return Flags & (1 << TID::Rematerializable);
00435   }
00436 
00437   /// isAsCheapAsAMove - Returns true if this instruction has the same cost (or
00438   /// less) than a move instruction. This is useful during certain types of
00439   /// optimizations (e.g., remat during two-address conversion or machine licm)
00440   /// where we would like to remat or hoist the instruction, but not if it costs
00441   /// more than moving the instruction into the appropriate register. Note, we
00442   /// are not marking copies from and to the same register class with this flag.
00443   bool isAsCheapAsAMove() const {
00444     return Flags & (1 << TID::CheapAsAMove);
00445   }
00446 };
00447 
00448 } // end namespace llvm
00449 
00450 #endif