1 //===-- llvm/MC/MCInstrDesc.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 MCOperandInfo and MCInstrDesc classes, which
11 // are used to describe target instructions and their operands.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_MC_MCINSTRDESC_H
16 #define LLVM_MC_MCINSTRDESC_H
18 #include "llvm/Support/DataTypes.h"
24 class MCSubtargetInfo;
27 //===----------------------------------------------------------------------===//
28 // Machine Operand Flags and Description
29 //===----------------------------------------------------------------------===//
32 // Operand constraints
33 enum OperandConstraint {
34 TIED_TO = 0, // Must be allocated the same register as.
35 EARLY_CLOBBER // Operand is an early clobber register operand
38 /// \brief These are flags set on operands, but should be considered
39 /// private, all access should go through the MCOperandInfo accessors.
40 /// See the accessors for a description of what these are.
41 enum OperandFlags { LookupPtrRegClass = 0, Predicate, OptionalDef };
43 /// \brief Operands are tagged with one of the values of this enum.
46 OPERAND_IMMEDIATE = 1,
50 OPERAND_FIRST_TARGET = 5
54 /// \brief This holds information about one operand of a machine instruction,
55 /// indicating the register class for register operands, etc.
58 /// \brief This specifies the register class enumeration of the operand
59 /// if the operand is a register. If isLookupPtrRegClass is set, then this is
60 /// an index that is passed to TargetRegisterInfo::getPointerRegClass(x) to
61 /// get a dynamic register class.
64 /// \brief These are flags from the MCOI::OperandFlags enum.
67 /// \brief Information about the type of the operand.
69 /// \brief The lower 16 bits are used to specify which constraints are set.
70 /// The higher 16 bits are used to specify the value of constraints (4 bits
74 /// \brief Set if this operand is a pointer value and it requires a callback
75 /// to look up its register class.
76 bool isLookupPtrRegClass() const {
77 return Flags & (1 << MCOI::LookupPtrRegClass);
80 /// \brief Set if this is one of the operands that made up of the predicate
81 /// operand that controls an isPredicable() instruction.
82 bool isPredicate() const { return Flags & (1 << MCOI::Predicate); }
84 /// \brief Set if this operand is a optional def.
85 bool isOptionalDef() const { return Flags & (1 << MCOI::OptionalDef); }
88 //===----------------------------------------------------------------------===//
89 // Machine Instruction Flags and Description
90 //===----------------------------------------------------------------------===//
93 /// \brief These should be considered private to the implementation of the
94 /// MCInstrDesc class. Clients should use the predicate methods on MCInstrDesc,
95 /// not use these directly. These all correspond to bitfields in the
96 /// MCInstrDesc::Flags field.
117 UnmodeledSideEffects,
133 /// \brief Describe properties that are true of each instruction in the target
134 /// description file. This captures information about side effects, register
135 /// use and many other things. There is one instance of this struct for each
136 /// target instruction class, and the MachineInstr class points to this struct
137 /// directly to describe itself.
140 unsigned short Opcode; // The opcode number
141 unsigned short NumOperands; // Num of args (may be more if variable_ops)
142 unsigned char NumDefs; // Num of args that are definitions
143 unsigned char Size; // Number of bytes in encoding.
144 unsigned short SchedClass; // enum identifying instr sched class
145 uint64_t Flags; // Flags identifying machine instr class
146 uint64_t TSFlags; // Target Specific Flag values
147 const uint16_t *ImplicitUses; // Registers implicitly read by this instr
148 const uint16_t *ImplicitDefs; // Registers implicitly defined by this instr
149 const MCOperandInfo *OpInfo; // 'NumOperands' entries about operands
150 // Subtarget feature that this is deprecated on, if any
151 // -1 implies this is not deprecated by any single feature. It may still be
152 // deprecated due to a "complex" reason, below.
153 int64_t DeprecatedFeature;
155 // A complex method to determine is a certain is deprecated or not, and return
156 // the reason for deprecation.
157 bool (*ComplexDeprecationInfo)(MCInst &, MCSubtargetInfo &, std::string &);
159 /// \brief Returns the value of the specific constraint if
160 /// it is set. Returns -1 if it is not set.
161 int getOperandConstraint(unsigned OpNum,
162 MCOI::OperandConstraint Constraint) const {
163 if (OpNum < NumOperands &&
164 (OpInfo[OpNum].Constraints & (1 << Constraint))) {
165 unsigned Pos = 16 + Constraint * 4;
166 return (int)(OpInfo[OpNum].Constraints >> Pos) & 0xf;
171 /// \brief Returns true if a certain instruction is deprecated and if so
172 /// returns the reason in \p Info.
173 bool getDeprecatedInfo(MCInst &MI, MCSubtargetInfo &STI,
174 std::string &Info) const;
176 /// \brief Return the opcode number for this descriptor.
177 unsigned getOpcode() const { return Opcode; }
179 /// \brief Return the number of declared MachineOperands for this
180 /// MachineInstruction. Note that variadic (isVariadic() returns true)
181 /// instructions may have additional operands at the end of the list, and note
182 /// that the machine instruction may include implicit register def/uses as
184 unsigned getNumOperands() const { return NumOperands; }
186 /// \brief Return the number of MachineOperands that are register
187 /// definitions. Register definitions always occur at the start of the
188 /// machine operand list. This is the number of "outs" in the .td file,
189 /// and does not include implicit defs.
190 unsigned getNumDefs() const { return NumDefs; }
192 /// \brief Return flags of this instruction.
193 unsigned getFlags() const { return Flags; }
195 /// \brief Return true if this instruction can have a variable number of
196 /// operands. In this case, the variable operands will be after the normal
197 /// operands but before the implicit definitions and uses (if any are
199 bool isVariadic() const { return Flags & (1 << MCID::Variadic); }
201 /// \brief Set if this instruction has an optional definition, e.g.
202 /// ARM instructions which can set condition code if 's' bit is set.
203 bool hasOptionalDef() const { return Flags & (1 << MCID::HasOptionalDef); }
205 /// \brief Return true if this is a pseudo instruction that doesn't
206 /// correspond to a real machine instruction.
207 bool isPseudo() const { return Flags & (1 << MCID::Pseudo); }
209 /// \brief Return true if the instruction is a return.
210 bool isReturn() const { return Flags & (1 << MCID::Return); }
212 /// \brief Return true if the instruction is a call.
213 bool isCall() const { return Flags & (1 << MCID::Call); }
215 /// \brief Returns true if the specified instruction stops control flow
216 /// from executing the instruction immediately following it. Examples include
217 /// unconditional branches and return instructions.
218 bool isBarrier() const { return Flags & (1 << MCID::Barrier); }
220 /// \brief Returns true if this instruction part of the terminator for
221 /// a basic block. Typically this is things like return and branch
224 /// Various passes use this to insert code into the bottom of a basic block,
225 /// but before control flow occurs.
226 bool isTerminator() const { return Flags & (1 << MCID::Terminator); }
228 /// \brief Returns true if this is a conditional, unconditional, or
229 /// indirect branch. Predicates below can be used to discriminate between
230 /// these cases, and the TargetInstrInfo::AnalyzeBranch method can be used to
231 /// get more information.
232 bool isBranch() const { return Flags & (1 << MCID::Branch); }
234 /// \brief Return true if this is an indirect branch, such as a
235 /// branch through a register.
236 bool isIndirectBranch() const { return Flags & (1 << MCID::IndirectBranch); }
238 /// \brief Return true if this is a branch which may fall
239 /// through to the next instruction or may transfer control flow to some other
240 /// block. The TargetInstrInfo::AnalyzeBranch method can be used to get more
241 /// information about this branch.
242 bool isConditionalBranch() const {
243 return isBranch() & !isBarrier() & !isIndirectBranch();
246 /// \brief Return true if this is a branch which always
247 /// transfers control flow to some other block. The
248 /// TargetInstrInfo::AnalyzeBranch method can be used to get more information
249 /// about this branch.
250 bool isUnconditionalBranch() const {
251 return isBranch() & isBarrier() & !isIndirectBranch();
254 /// \brief Return true if this is a branch or an instruction which directly
255 /// writes to the program counter. Considered 'may' affect rather than
256 /// 'does' affect as things like predication are not taken into account.
257 bool mayAffectControlFlow(const MCInst &MI, const MCRegisterInfo &RI) const;
259 /// \brief Return true if this instruction has a predicate operand
260 /// that controls execution. It may be set to 'always', or may be set to other
261 /// values. There are various methods in TargetInstrInfo that can be used to
262 /// control and modify the predicate in this instruction.
263 bool isPredicable() const { return Flags & (1 << MCID::Predicable); }
265 /// \brief Return true if this instruction is a comparison.
266 bool isCompare() const { return Flags & (1 << MCID::Compare); }
268 /// \brief Return true if this instruction is a move immediate
269 /// (including conditional moves) instruction.
270 bool isMoveImmediate() const { return Flags & (1 << MCID::MoveImm); }
272 /// \brief Return true if this instruction is a bitcast instruction.
273 bool isBitcast() const { return Flags & (1 << MCID::Bitcast); }
275 /// \brief Return true if this is a select instruction.
276 bool isSelect() const { return Flags & (1 << MCID::Select); }
278 /// \brief Return true if this instruction cannot be safely
279 /// duplicated. For example, if the instruction has a unique labels attached
280 /// to it, duplicating it would cause multiple definition errors.
281 bool isNotDuplicable() const { return Flags & (1 << MCID::NotDuplicable); }
283 /// \brief Returns true if the specified instruction has a delay slot which
284 /// must be filled by the code generator.
285 bool hasDelaySlot() const { return Flags & (1 << MCID::DelaySlot); }
287 /// \brief Return true for instructions that can be folded as memory operands
288 /// in other instructions. The most common use for this is instructions that
289 /// are simple loads from memory that don't modify the loaded value in any
290 /// way, but it can also be used for instructions that can be expressed as
291 /// constant-pool loads, such as V_SETALLONES on x86, to allow them to be
292 /// folded when it is beneficial. This should only be set on instructions
293 /// that return a value in their only virtual register definition.
294 bool canFoldAsLoad() const { return Flags & (1 << MCID::FoldableAsLoad); }
296 /// \brief Return true if this instruction behaves
297 /// the same way as the generic REG_SEQUENCE instructions.
299 /// dX VMOVDRR rY, rZ
301 /// dX = REG_SEQUENCE rY, ssub_0, rZ, ssub_1.
303 /// Note that for the optimizers to be able to take advantage of
304 /// this property, TargetInstrInfo::getRegSequenceLikeInputs has to be
305 /// override accordingly.
306 bool isRegSequenceLike() const { return Flags & (1 << MCID::RegSequence); }
308 /// \brief Return true if this instruction behaves
309 /// the same way as the generic EXTRACT_SUBREG instructions.
311 /// rX, rY VMOVRRD dZ
312 /// is equivalent to two EXTRACT_SUBREG:
313 /// rX = EXTRACT_SUBREG dZ, ssub_0
314 /// rY = EXTRACT_SUBREG dZ, ssub_1
316 /// Note that for the optimizers to be able to take advantage of
317 /// this property, TargetInstrInfo::getExtractSubregLikeInputs has to be
318 /// override accordingly.
319 bool isExtractSubregLike() const {
320 return Flags & (1 << MCID::ExtractSubreg);
323 /// \brief Return true if this instruction behaves
324 /// the same way as the generic INSERT_SUBREG instructions.
326 /// dX = VSETLNi32 dY, rZ, Imm
327 /// is equivalent to a INSERT_SUBREG:
328 /// dX = INSERT_SUBREG dY, rZ, translateImmToSubIdx(Imm)
330 /// Note that for the optimizers to be able to take advantage of
331 /// this property, TargetInstrInfo::getInsertSubregLikeInputs has to be
332 /// override accordingly.
333 bool isInsertSubregLike() const { return Flags & (1 << MCID::InsertSubreg); }
336 /// \brief Return true if this instruction is convergent.
338 /// Convergent instructions may only be moved to locations that are
339 /// control-equivalent to their original positions.
340 bool isConvergent() const { return Flags & (1 << MCID::Convergent); }
342 //===--------------------------------------------------------------------===//
343 // Side Effect Analysis
344 //===--------------------------------------------------------------------===//
346 /// \brief Return true if this instruction could possibly read memory.
347 /// Instructions with this flag set are not necessarily simple load
348 /// instructions, they may load a value and modify it, for example.
349 bool mayLoad() const { return Flags & (1 << MCID::MayLoad); }
351 /// \brief Return true if this instruction could possibly modify memory.
352 /// Instructions with this flag set are not necessarily simple store
353 /// instructions, they may store a modified value based on their operands, or
354 /// may not actually modify anything, for example.
355 bool mayStore() const { return Flags & (1 << MCID::MayStore); }
357 /// \brief Return true if this instruction has side
358 /// effects that are not modeled by other flags. This does not return true
359 /// for instructions whose effects are captured by:
361 /// 1. Their operand list and implicit definition/use list. Register use/def
362 /// info is explicit for instructions.
363 /// 2. Memory accesses. Use mayLoad/mayStore.
364 /// 3. Calling, branching, returning: use isCall/isReturn/isBranch.
366 /// Examples of side effects would be modifying 'invisible' machine state like
367 /// a control register, flushing a cache, modifying a register invisible to
369 bool hasUnmodeledSideEffects() const {
370 return Flags & (1 << MCID::UnmodeledSideEffects);
373 //===--------------------------------------------------------------------===//
374 // Flags that indicate whether an instruction can be modified by a method.
375 //===--------------------------------------------------------------------===//
377 /// \brief Return true if this may be a 2- or 3-address instruction (of the
378 /// form "X = op Y, Z, ..."), which produces the same result if Y and Z are
379 /// exchanged. If this flag is set, then the
380 /// TargetInstrInfo::commuteInstruction method may be used to hack on the
383 /// Note that this flag may be set on instructions that are only commutable
384 /// sometimes. In these cases, the call to commuteInstruction will fail.
385 /// Also note that some instructions require non-trivial modification to
387 bool isCommutable() const { return Flags & (1 << MCID::Commutable); }
389 /// \brief Return true if this is a 2-address instruction which can be changed
390 /// into a 3-address instruction if needed. Doing this transformation can be
391 /// profitable in the register allocator, because it means that the
392 /// instruction can use a 2-address form if possible, but degrade into a less
393 /// efficient form if the source and dest register cannot be assigned to the
394 /// same register. For example, this allows the x86 backend to turn a "shl
395 /// reg, 3" instruction into an LEA instruction, which is the same speed as
396 /// the shift but has bigger code size.
398 /// If this returns true, then the target must implement the
399 /// TargetInstrInfo::convertToThreeAddress method for this instruction, which
400 /// is allowed to fail if the transformation isn't valid for this specific
401 /// instruction (e.g. shl reg, 4 on x86).
403 bool isConvertibleTo3Addr() const {
404 return Flags & (1 << MCID::ConvertibleTo3Addr);
407 /// \brief Return true if this instruction requires custom insertion support
408 /// when the DAG scheduler is inserting it into a machine basic block. If
409 /// this is true for the instruction, it basically means that it is a pseudo
410 /// instruction used at SelectionDAG time that is expanded out into magic code
411 /// by the target when MachineInstrs are formed.
413 /// If this is true, the TargetLoweringInfo::InsertAtEndOfBasicBlock method
414 /// is used to insert this into the MachineBasicBlock.
415 bool usesCustomInsertionHook() const {
416 return Flags & (1 << MCID::UsesCustomInserter);
419 /// \brief Return true if this instruction requires *adjustment* after
420 /// instruction selection by calling a target hook. For example, this can be
421 /// used to fill in ARM 's' optional operand depending on whether the
422 /// conditional flag register is used.
423 bool hasPostISelHook() const { return Flags & (1 << MCID::HasPostISelHook); }
425 /// \brief Returns true if this instruction is a candidate for remat. This
426 /// flag is only used in TargetInstrInfo method isTriviallyRematerializable.
428 /// If this flag is set, the isReallyTriviallyReMaterializable()
429 /// or isReallyTriviallyReMaterializableGeneric methods are called to verify
430 /// the instruction is really rematable.
431 bool isRematerializable() const {
432 return Flags & (1 << MCID::Rematerializable);
435 /// \brief Returns true if this instruction has the same cost (or less) than a
436 /// move instruction. This is useful during certain types of optimizations
437 /// (e.g., remat during two-address conversion or machine licm) where we would
438 /// like to remat or hoist the instruction, but not if it costs more than
439 /// moving the instruction into the appropriate register. Note, we are not
440 /// marking copies from and to the same register class with this flag.
442 /// This method could be called by interface TargetInstrInfo::isAsCheapAsAMove
443 /// for different subtargets.
444 bool isAsCheapAsAMove() const { return Flags & (1 << MCID::CheapAsAMove); }
446 /// \brief Returns true if this instruction source operands have special
447 /// register allocation requirements that are not captured by the operand
448 /// register classes. e.g. ARM::STRD's two source registers must be an even /
449 /// odd pair, ARM::STM registers have to be in ascending order. Post-register
450 /// allocation passes should not attempt to change allocations for sources of
451 /// instructions with this flag.
452 bool hasExtraSrcRegAllocReq() const {
453 return Flags & (1 << MCID::ExtraSrcRegAllocReq);
456 /// \brief Returns true if this instruction def operands have special register
457 /// allocation requirements that are not captured by the operand register
458 /// classes. e.g. ARM::LDRD's two def registers must be an even / odd pair,
459 /// ARM::LDM registers have to be in ascending order. Post-register
460 /// allocation passes should not attempt to change allocations for definitions
461 /// of instructions with this flag.
462 bool hasExtraDefRegAllocReq() const {
463 return Flags & (1 << MCID::ExtraDefRegAllocReq);
466 /// \brief Return a list of registers that are potentially read by any
467 /// instance of this machine instruction. For example, on X86, the "adc"
468 /// instruction adds two register operands and adds the carry bit in from the
469 /// flags register. In this case, the instruction is marked as implicitly
470 /// reading the flags. Likewise, the variable shift instruction on X86 is
471 /// marked as implicitly reading the 'CL' register, which it always does.
473 /// This method returns null if the instruction has no implicit uses.
474 const uint16_t *getImplicitUses() const { return ImplicitUses; }
476 /// \brief Return the number of implicit uses this instruction has.
477 unsigned getNumImplicitUses() const {
481 for (; ImplicitUses[i]; ++i) /*empty*/
486 /// \brief Return a list of registers that are potentially written by any
487 /// instance of this machine instruction. For example, on X86, many
488 /// instructions implicitly set the flags register. In this case, they are
489 /// marked as setting the FLAGS. Likewise, many instructions always deposit
490 /// their result in a physical register. For example, the X86 divide
491 /// instruction always deposits the quotient and remainder in the EAX/EDX
492 /// registers. For that instruction, this will return a list containing the
493 /// EAX/EDX/EFLAGS registers.
495 /// This method returns null if the instruction has no implicit defs.
496 const uint16_t *getImplicitDefs() const { return ImplicitDefs; }
498 /// \brief Return the number of implicit defs this instruct has.
499 unsigned getNumImplicitDefs() const {
503 for (; ImplicitDefs[i]; ++i) /*empty*/
508 /// \brief Return true if this instruction implicitly
509 /// uses the specified physical register.
510 bool hasImplicitUseOfPhysReg(unsigned Reg) const {
511 if (const uint16_t *ImpUses = ImplicitUses)
512 for (; *ImpUses; ++ImpUses)
518 /// \brief Return true if this instruction implicitly
519 /// defines the specified physical register.
520 bool hasImplicitDefOfPhysReg(unsigned Reg,
521 const MCRegisterInfo *MRI = nullptr) const;
523 /// \brief Return the scheduling class for this instruction. The
524 /// scheduling class is an index into the InstrItineraryData table. This
525 /// returns zero if there is no known scheduling information for the
527 unsigned getSchedClass() const { return SchedClass; }
529 /// \brief Return the number of bytes in the encoding of this instruction,
530 /// or zero if the encoding size cannot be known from the opcode.
531 unsigned getSize() const { return Size; }
533 /// \brief Find the index of the first operand in the
534 /// operand list that is used to represent the predicate. It returns -1 if
536 int findFirstPredOperandIdx() const {
537 if (isPredicable()) {
538 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
539 if (OpInfo[i].isPredicate())
547 /// \brief Return true if this instruction defines the specified physical
548 /// register, either explicitly or implicitly.
549 bool hasDefOfPhysReg(const MCInst &MI, unsigned Reg,
550 const MCRegisterInfo &RI) const;
553 } // end namespace llvm