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