1 //===-- llvm/Target/TargetInstrDesc.h - Instruction Descriptors -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the TargetOperandInfo and TargetInstrDesc classes, which
11 // are used to describe target instructions and their operands.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_TARGET_TARGETINSTRDESC_H
16 #define LLVM_TARGET_TARGETINSTRDESC_H
18 #include "llvm/Support/DataTypes.h"
22 class TargetRegisterClass;
23 class TargetRegisterInfo;
25 //===----------------------------------------------------------------------===//
26 // Machine Operand Flags and Description
27 //===----------------------------------------------------------------------===//
30 // Operand constraints
31 enum OperandConstraint {
32 TIED_TO = 0, // Must be allocated the same register as.
33 EARLY_CLOBBER // Operand is an early clobber register operand
36 /// OperandFlags - These are flags set on operands, but should be considered
37 /// private, all access should go through the TargetOperandInfo accessors.
38 /// See the accessors for a description of what these are.
40 LookupPtrRegClass = 0,
46 /// TargetOperandInfo - This holds information about one operand of a machine
47 /// instruction, indicating the register class for register operands, etc.
49 class TargetOperandInfo {
51 /// RegClass - This specifies the register class enumeration of the operand
52 /// if the operand is a register. If isLookupPtrRegClass is set, then this is
53 /// an index that is passed to TargetRegisterInfo::getPointerRegClass(x) to
54 /// get a dynamic register class.
56 /// NOTE: This member should be considered to be private, all access should go
57 /// through "getRegClass(TRI)" below.
60 /// Flags - These are flags from the TOI::OperandFlags enum.
63 /// Lower 16 bits are used to specify which constraints are set. The higher 16
64 /// bits are used to specify the value of constraints (4 bits each).
66 /// Currently no other information.
68 /// getRegClass - Get the register class for the operand, handling resolution
69 /// of "symbolic" pointer register classes etc. If this is not a register
70 /// operand, this returns null.
71 const TargetRegisterClass *getRegClass(const TargetRegisterInfo *TRI) const;
74 /// isLookupPtrRegClass - Set if this operand is a pointer value and it
75 /// requires a callback to look up its register class.
76 bool isLookupPtrRegClass() const { return Flags&(1 <<TOI::LookupPtrRegClass);}
78 /// isPredicate - Set if this is one of the operands that made up of
79 /// the predicate operand that controls an isPredicable() instruction.
80 bool isPredicate() const { return Flags & (1 << TOI::Predicate); }
82 /// isOptionalDef - Set if this operand is a optional def.
84 bool isOptionalDef() const { return Flags & (1 << TOI::OptionalDef); }
88 //===----------------------------------------------------------------------===//
89 // Machine Instruction Flags and Description
90 //===----------------------------------------------------------------------===//
92 /// TargetInstrDesc flags - These should be considered private to the
93 /// implementation of the TargetInstrDesc class. Clients should use the
94 /// predicate methods on TargetInstrDesc, not use these directly. These
95 /// all correspond to bitfields in the TargetInstrDesc::Flags field.
115 UnmodeledSideEffects,
126 /// TargetInstrDesc - Describe properties that are true of each
127 /// instruction in the target description file. This captures information about
128 /// side effects, register use and many other things. There is one instance of
129 /// this struct for each target instruction class, and the MachineInstr class
130 /// points to this struct directly to describe itself.
131 class TargetInstrDesc {
133 unsigned short Opcode; // The opcode number
134 unsigned short NumOperands; // Num of args (may be more if variable_ops)
135 unsigned short NumDefs; // Num of args that are definitions
136 unsigned short SchedClass; // enum identifying instr sched class
137 const char * Name; // Name of the instruction record in td file
138 unsigned Flags; // Flags identifying machine instr class
139 uint64_t TSFlags; // Target Specific Flag values
140 const unsigned *ImplicitUses; // Registers implicitly read by this instr
141 const unsigned *ImplicitDefs; // Registers implicitly defined by this instr
142 const TargetRegisterClass **RCBarriers; // Reg classes completely "clobbered"
143 const TargetOperandInfo *OpInfo; // 'NumOperands' entries about operands
145 /// getOperandConstraint - Returns the value of the specific constraint if
146 /// it is set. Returns -1 if it is not set.
147 int getOperandConstraint(unsigned OpNum,
148 TOI::OperandConstraint Constraint) const {
149 if (OpNum < NumOperands &&
150 (OpInfo[OpNum].Constraints & (1 << Constraint))) {
151 unsigned Pos = 16 + Constraint * 4;
152 return (int)(OpInfo[OpNum].Constraints >> Pos) & 0xf;
157 /// getRegClass - Returns the register class constraint for OpNum, or NULL.
158 const TargetRegisterClass *getRegClass(unsigned OpNum,
159 const TargetRegisterInfo *TRI) const {
160 return OpNum < NumOperands ? OpInfo[OpNum].getRegClass(TRI) : 0;
163 /// getOpcode - Return the opcode number for this descriptor.
164 unsigned getOpcode() const {
168 /// getName - Return the name of the record in the .td file for this
169 /// instruction, for example "ADD8ri".
170 const char *getName() const {
174 /// getNumOperands - Return the number of declared MachineOperands for this
175 /// MachineInstruction. Note that variadic (isVariadic() returns true)
176 /// instructions may have additional operands at the end of the list, and note
177 /// that the machine instruction may include implicit register def/uses as
179 unsigned getNumOperands() const {
183 /// getNumDefs - Return the number of MachineOperands that are register
184 /// definitions. Register definitions always occur at the start of the
185 /// machine operand list. This is the number of "outs" in the .td file,
186 /// and does not include implicit defs.
187 unsigned getNumDefs() const {
191 /// isVariadic - Return true if this instruction can have a variable number of
192 /// operands. In this case, the variable operands will be after the normal
193 /// operands but before the implicit definitions and uses (if any are
195 bool isVariadic() const {
196 return Flags & (1 << TID::Variadic);
199 /// hasOptionalDef - Set if this instruction has an optional definition, e.g.
200 /// ARM instructions which can set condition code if 's' bit is set.
201 bool hasOptionalDef() const {
202 return Flags & (1 << TID::HasOptionalDef);
205 /// getImplicitUses - Return a list of registers that are potentially
206 /// read by any instance of this machine instruction. For example, on X86,
207 /// the "adc" instruction adds two register operands and adds the carry bit in
208 /// from the flags register. In this case, the instruction is marked as
209 /// implicitly reading the flags. Likewise, the variable shift instruction on
210 /// X86 is marked as implicitly reading the 'CL' register, which it always
213 /// This method returns null if the instruction has no implicit uses.
214 const unsigned *getImplicitUses() const {
218 /// getNumImplicitUses - Return the number of implicit uses this instruction
220 unsigned getNumImplicitUses() const {
221 if (ImplicitUses == 0) return 0;
223 for (; ImplicitUses[i]; ++i) /*empty*/;
228 /// getImplicitDefs - Return a list of registers that are potentially
229 /// written by any instance of this machine instruction. For example, on X86,
230 /// many instructions implicitly set the flags register. In this case, they
231 /// are marked as setting the FLAGS. Likewise, many instructions always
232 /// deposit their result in a physical register. For example, the X86 divide
233 /// instruction always deposits the quotient and remainder in the EAX/EDX
234 /// registers. For that instruction, this will return a list containing the
235 /// EAX/EDX/EFLAGS registers.
237 /// This method returns null if the instruction has no implicit defs.
238 const unsigned *getImplicitDefs() const {
242 /// getNumImplicitDefs - Return the number of implicit defs this instruction
244 unsigned getNumImplicitDefs() const {
245 if (ImplicitDefs == 0) return 0;
247 for (; ImplicitDefs[i]; ++i) /*empty*/;
251 /// hasImplicitUseOfPhysReg - Return true if this instruction implicitly
252 /// uses the specified physical register.
253 bool hasImplicitUseOfPhysReg(unsigned Reg) const {
254 if (const unsigned *ImpUses = ImplicitUses)
255 for (; *ImpUses; ++ImpUses)
256 if (*ImpUses == Reg) return true;
260 /// hasImplicitDefOfPhysReg - Return true if this instruction implicitly
261 /// defines the specified physical register.
262 bool hasImplicitDefOfPhysReg(unsigned Reg) const {
263 if (const unsigned *ImpDefs = ImplicitDefs)
264 for (; *ImpDefs; ++ImpDefs)
265 if (*ImpDefs == Reg) return true;
269 /// getRegClassBarriers - Return a list of register classes that are
270 /// completely clobbered by this machine instruction. For example, on X86
271 /// the call instructions will completely clobber all the registers in the
272 /// fp stack and XMM classes.
274 /// This method returns null if the instruction doesn't completely clobber
275 /// any register class.
276 const TargetRegisterClass **getRegClassBarriers() const {
280 /// getSchedClass - Return the scheduling class for this instruction. The
281 /// scheduling class is an index into the InstrItineraryData table. This
282 /// returns zero if there is no known scheduling information for the
285 unsigned getSchedClass() const {
289 bool isReturn() const {
290 return Flags & (1 << TID::Return);
293 bool isCall() const {
294 return Flags & (1 << TID::Call);
297 /// isBarrier - Returns true if the specified instruction stops control flow
298 /// from executing the instruction immediately following it. Examples include
299 /// unconditional branches and return instructions.
300 bool isBarrier() const {
301 return Flags & (1 << TID::Barrier);
304 /// isTerminator - Returns true if this instruction part of the terminator for
305 /// a basic block. Typically this is things like return and branch
308 /// Various passes use this to insert code into the bottom of a basic block,
309 /// but before control flow occurs.
310 bool isTerminator() const {
311 return Flags & (1 << TID::Terminator);
314 /// isBranch - Returns true if this is a conditional, unconditional, or
315 /// indirect branch. Predicates below can be used to discriminate between
316 /// these cases, and the TargetInstrInfo::AnalyzeBranch method can be used to
317 /// get more information.
318 bool isBranch() const {
319 return Flags & (1 << TID::Branch);
322 /// isIndirectBranch - Return true if this is an indirect branch, such as a
323 /// branch through a register.
324 bool isIndirectBranch() const {
325 return Flags & (1 << TID::IndirectBranch);
328 /// isConditionalBranch - Return true if this is a branch which may fall
329 /// through to the next instruction or may transfer control flow to some other
330 /// block. The TargetInstrInfo::AnalyzeBranch method can be used to get more
331 /// information about this branch.
332 bool isConditionalBranch() const {
333 return isBranch() & !isBarrier() & !isIndirectBranch();
336 /// isUnconditionalBranch - Return true if this is a branch which always
337 /// transfers control flow to some other block. The
338 /// TargetInstrInfo::AnalyzeBranch method can be used to get more information
339 /// about this branch.
340 bool isUnconditionalBranch() const {
341 return isBranch() & isBarrier() & !isIndirectBranch();
344 // isPredicable - Return true if this instruction has a predicate operand that
345 // controls execution. It may be set to 'always', or may be set to other
346 /// values. There are various methods in TargetInstrInfo that can be used to
347 /// control and modify the predicate in this instruction.
348 bool isPredicable() const {
349 return Flags & (1 << TID::Predicable);
352 /// isCompare - Return true if this instruction is a comparison.
353 bool isCompare() const {
354 return Flags & (1 << TID::Compare);
357 /// isMoveImmediate - Return true if this instruction is a move immediate
358 /// (including conditional moves) instruction.
359 bool isMoveImmediate() const {
360 return Flags & (1 << TID::MoveImm);
363 /// isBitcast - Return true if this instruction is a bitcast instruction.
365 bool isBitcast() const {
366 return Flags & (1 << TID::Bitcast);
369 /// isNotDuplicable - Return true if this instruction cannot be safely
370 /// duplicated. For example, if the instruction has a unique labels attached
371 /// to it, duplicating it would cause multiple definition errors.
372 bool isNotDuplicable() const {
373 return Flags & (1 << TID::NotDuplicable);
376 /// hasDelaySlot - Returns true if the specified instruction has a delay slot
377 /// which must be filled by the code generator.
378 bool hasDelaySlot() const {
379 return Flags & (1 << TID::DelaySlot);
382 /// canFoldAsLoad - Return true for instructions that can be folded as
383 /// memory operands in other instructions. The most common use for this
384 /// is instructions that are simple loads from memory that don't modify
385 /// the loaded value in any way, but it can also be used for instructions
386 /// that can be expressed as constant-pool loads, such as V_SETALLONES
387 /// on x86, to allow them to be folded when it is beneficial.
388 /// This should only be set on instructions that return a value in their
389 /// only virtual register definition.
390 bool canFoldAsLoad() const {
391 return Flags & (1 << TID::FoldableAsLoad);
394 //===--------------------------------------------------------------------===//
395 // Side Effect Analysis
396 //===--------------------------------------------------------------------===//
398 /// mayLoad - Return true if this instruction could possibly read memory.
399 /// Instructions with this flag set are not necessarily simple load
400 /// instructions, they may load a value and modify it, for example.
401 bool mayLoad() const {
402 return Flags & (1 << TID::MayLoad);
406 /// mayStore - Return true if this instruction could possibly modify memory.
407 /// Instructions with this flag set are not necessarily simple store
408 /// instructions, they may store a modified value based on their operands, or
409 /// may not actually modify anything, for example.
410 bool mayStore() const {
411 return Flags & (1 << TID::MayStore);
414 /// hasUnmodeledSideEffects - Return true if this instruction has side
415 /// effects that are not modeled by other flags. This does not return true
416 /// for instructions whose effects are captured by:
418 /// 1. Their operand list and implicit definition/use list. Register use/def
419 /// info is explicit for instructions.
420 /// 2. Memory accesses. Use mayLoad/mayStore.
421 /// 3. Calling, branching, returning: use isCall/isReturn/isBranch.
423 /// Examples of side effects would be modifying 'invisible' machine state like
424 /// a control register, flushing a cache, modifying a register invisible to
427 bool hasUnmodeledSideEffects() const {
428 return Flags & (1 << TID::UnmodeledSideEffects);
431 //===--------------------------------------------------------------------===//
432 // Flags that indicate whether an instruction can be modified by a method.
433 //===--------------------------------------------------------------------===//
435 /// isCommutable - Return true if this may be a 2- or 3-address
436 /// instruction (of the form "X = op Y, Z, ..."), which produces the same
437 /// result if Y and Z are exchanged. If this flag is set, then the
438 /// TargetInstrInfo::commuteInstruction method may be used to hack on the
441 /// Note that this flag may be set on instructions that are only commutable
442 /// sometimes. In these cases, the call to commuteInstruction will fail.
443 /// Also note that some instructions require non-trivial modification to
445 bool isCommutable() const {
446 return Flags & (1 << TID::Commutable);
449 /// isConvertibleTo3Addr - Return true if this is a 2-address instruction
450 /// which can be changed into a 3-address instruction if needed. Doing this
451 /// transformation can be profitable in the register allocator, because it
452 /// means that the instruction can use a 2-address form if possible, but
453 /// degrade into a less efficient form if the source and dest register cannot
454 /// be assigned to the same register. For example, this allows the x86
455 /// backend to turn a "shl reg, 3" instruction into an LEA instruction, which
456 /// is the same speed as the shift but has bigger code size.
458 /// If this returns true, then the target must implement the
459 /// TargetInstrInfo::convertToThreeAddress method for this instruction, which
460 /// is allowed to fail if the transformation isn't valid for this specific
461 /// instruction (e.g. shl reg, 4 on x86).
463 bool isConvertibleTo3Addr() const {
464 return Flags & (1 << TID::ConvertibleTo3Addr);
467 /// usesCustomInsertionHook - Return true if this instruction requires
468 /// custom insertion support when the DAG scheduler is inserting it into a
469 /// machine basic block. If this is true for the instruction, it basically
470 /// means that it is a pseudo instruction used at SelectionDAG time that is
471 /// expanded out into magic code by the target when MachineInstrs are formed.
473 /// If this is true, the TargetLoweringInfo::InsertAtEndOfBasicBlock method
474 /// is used to insert this into the MachineBasicBlock.
475 bool usesCustomInsertionHook() const {
476 return Flags & (1 << TID::UsesCustomInserter);
479 /// isRematerializable - Returns true if this instruction is a candidate for
480 /// remat. This flag is deprecated, please don't use it anymore. If this
481 /// flag is set, the isReallyTriviallyReMaterializable() method is called to
482 /// verify the instruction is really rematable.
483 bool isRematerializable() const {
484 return Flags & (1 << TID::Rematerializable);
487 /// isAsCheapAsAMove - Returns true if this instruction has the same cost (or
488 /// less) than a move instruction. This is useful during certain types of
489 /// optimizations (e.g., remat during two-address conversion or machine licm)
490 /// where we would like to remat or hoist the instruction, but not if it costs
491 /// more than moving the instruction into the appropriate register. Note, we
492 /// are not marking copies from and to the same register class with this flag.
493 bool isAsCheapAsAMove() const {
494 return Flags & (1 << TID::CheapAsAMove);
497 /// hasExtraSrcRegAllocReq - Returns true if this instruction source operands
498 /// have special register allocation requirements that are not captured by the
499 /// operand register classes. e.g. ARM::STRD's two source registers must be an
500 /// even / odd pair, ARM::STM registers have to be in ascending order.
501 /// Post-register allocation passes should not attempt to change allocations
502 /// for sources of instructions with this flag.
503 bool hasExtraSrcRegAllocReq() const {
504 return Flags & (1 << TID::ExtraSrcRegAllocReq);
507 /// hasExtraDefRegAllocReq - Returns true if this instruction def operands
508 /// have special register allocation requirements that are not captured by the
509 /// operand register classes. e.g. ARM::LDRD's two def registers must be an
510 /// even / odd pair, ARM::LDM registers have to be in ascending order.
511 /// Post-register allocation passes should not attempt to change allocations
512 /// for definitions of instructions with this flag.
513 bool hasExtraDefRegAllocReq() const {
514 return Flags & (1 << TID::ExtraDefRegAllocReq);
518 } // end namespace llvm