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/MC/MCInst.h"
19 #include "llvm/MC/MCRegisterInfo.h"
20 #include "llvm/MC/MCSubtargetInfo.h"
21 #include "llvm/Support/DataTypes.h"
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 MCOperandInfo accessors.
38 /// See the accessors for a description of what these are.
40 LookupPtrRegClass = 0,
45 /// Operand Type - Operands are tagged with one of the values of this enum.
55 /// MCOperandInfo - This holds information about one operand of a machine
56 /// instruction, indicating the register class for register operands, etc.
60 /// RegClass - This specifies the register class enumeration of the operand
61 /// if the operand is a register. If isLookupPtrRegClass is set, then this is
62 /// an index that is passed to TargetRegisterInfo::getPointerRegClass(x) to
63 /// get a dynamic register class.
66 /// Flags - These are flags from the MCOI::OperandFlags enum.
69 /// OperandType - Information about the type of the operand.
72 /// Lower 16 bits are used to specify which constraints are set. The higher 16
73 /// bits are used to specify the value of constraints (4 bits each).
75 /// Currently no other information.
77 /// isLookupPtrRegClass - Set if this operand is a pointer value and it
78 /// requires a callback to look up its register class.
79 bool isLookupPtrRegClass() const {return Flags&(1 <<MCOI::LookupPtrRegClass);}
81 /// isPredicate - Set if this is one of the operands that made up of
82 /// the predicate operand that controls an isPredicable() instruction.
83 bool isPredicate() const { return Flags & (1 << MCOI::Predicate); }
85 /// isOptionalDef - Set if this operand is a optional def.
87 bool isOptionalDef() const { return Flags & (1 << MCOI::OptionalDef); }
91 //===----------------------------------------------------------------------===//
92 // Machine Instruction Flags and Description
93 //===----------------------------------------------------------------------===//
95 /// MCInstrDesc flags - These should be considered private to the
96 /// implementation of the MCInstrDesc class. Clients should use the predicate
97 /// methods on MCInstrDesc, not use these directly. These all correspond to
98 /// bitfields in the MCInstrDesc::Flags field.
120 UnmodeledSideEffects,
135 /// MCInstrDesc - Describe properties that are true of each instruction in the
136 /// target description file. This captures information about side effects,
137 /// register use and many other things. There is one instance of this struct
138 /// for each target instruction class, and the MachineInstr class points to
139 /// this struct directly to describe itself.
142 unsigned short Opcode; // The opcode number
143 unsigned short NumOperands; // Num of args (may be more if variable_ops)
144 unsigned short NumDefs; // Num of args that are definitions
145 unsigned short SchedClass; // enum identifying instr sched class
146 unsigned short Size; // Number of bytes in encoding.
147 unsigned Flags; // Flags identifying machine instr class
148 uint64_t TSFlags; // Target Specific Flag values
149 const uint16_t *ImplicitUses; // Registers implicitly read by this instr
150 const uint16_t *ImplicitDefs; // Registers implicitly defined by this instr
151 const MCOperandInfo *OpInfo; // 'NumOperands' entries about operands
152 uint64_t DeprecatedFeatureMask;// Feature bits that this is deprecated on, if any
153 // A complex method to determine is a certain is deprecated or not, and return
154 // the reason for deprecation.
155 bool (*ComplexDeprecationInfo)(MCInst &, MCSubtargetInfo &, std::string &);
157 /// \brief Returns the value of the specific constraint if
158 /// it is set. Returns -1 if it is not set.
159 int getOperandConstraint(unsigned OpNum,
160 MCOI::OperandConstraint Constraint) const {
161 if (OpNum < NumOperands &&
162 (OpInfo[OpNum].Constraints & (1 << Constraint))) {
163 unsigned Pos = 16 + Constraint * 4;
164 return (int)(OpInfo[OpNum].Constraints >> Pos) & 0xf;
169 /// \brief Returns true if a certain instruction is deprecated and if so
170 /// returns the reason in \p Info.
171 bool getDeprecatedInfo(MCInst &MI, MCSubtargetInfo &STI,
172 std::string &Info) const {
173 if (ComplexDeprecationInfo)
174 return ComplexDeprecationInfo(MI, STI, Info);
175 if ((DeprecatedFeatureMask & STI.getFeatureBits()) != 0) {
176 // FIXME: it would be nice to include the subtarget feature here.
183 /// \brief Return the opcode number for this descriptor.
184 unsigned getOpcode() const {
188 /// \brief Return the number of declared MachineOperands for this
189 /// MachineInstruction. Note that variadic (isVariadic() returns true)
190 /// instructions may have additional operands at the end of the list, and note
191 /// that the machine instruction may include implicit register def/uses as
193 unsigned getNumOperands() const {
197 /// \brief Return the number of MachineOperands that are register
198 /// definitions. Register definitions always occur at the start of the
199 /// machine operand list. This is the number of "outs" in the .td file,
200 /// and does not include implicit defs.
201 unsigned getNumDefs() const {
205 /// \brief Return flags of this instruction.
206 unsigned getFlags() const { return Flags; }
208 /// \brief Return true if this instruction can have a variable number of
209 /// operands. In this case, the variable operands will be after the normal
210 /// operands but before the implicit definitions and uses (if any are
212 bool isVariadic() const {
213 return Flags & (1 << MCID::Variadic);
216 /// \brief Set if this instruction has an optional definition, e.g.
217 /// ARM instructions which can set condition code if 's' bit is set.
218 bool hasOptionalDef() const {
219 return Flags & (1 << MCID::HasOptionalDef);
222 /// \brief Return true if this is a pseudo instruction that doesn't
223 /// correspond to a real machine instruction.
225 bool isPseudo() const {
226 return Flags & (1 << MCID::Pseudo);
229 /// \brief Return true if the instruction is a return.
230 bool isReturn() const {
231 return Flags & (1 << MCID::Return);
234 /// \brief Return true if the instruction is a call.
235 bool isCall() const {
236 return Flags & (1 << MCID::Call);
239 /// \brief Returns true if the specified instruction stops control flow
240 /// from executing the instruction immediately following it. Examples include
241 /// unconditional branches and return instructions.
242 bool isBarrier() const {
243 return Flags & (1 << MCID::Barrier);
246 /// \brief Returns true if this instruction part of the terminator for
247 /// a basic block. Typically this is things like return and branch
250 /// Various passes use this to insert code into the bottom of a basic block,
251 /// but before control flow occurs.
252 bool isTerminator() const {
253 return Flags & (1 << MCID::Terminator);
256 /// \brief Returns true if this is a conditional, unconditional, or
257 /// indirect branch. Predicates below can be used to discriminate between
258 /// these cases, and the TargetInstrInfo::AnalyzeBranch method can be used to
259 /// get more information.
260 bool isBranch() const {
261 return Flags & (1 << MCID::Branch);
264 /// \brief Return true if this is an indirect branch, such as a
265 /// branch through a register.
266 bool isIndirectBranch() const {
267 return Flags & (1 << MCID::IndirectBranch);
270 /// \brief Return true if this is a branch which may fall
271 /// through to the next instruction or may transfer control flow to some other
272 /// block. The TargetInstrInfo::AnalyzeBranch method can be used to get more
273 /// information about this branch.
274 bool isConditionalBranch() const {
275 return isBranch() & !isBarrier() & !isIndirectBranch();
278 /// \brief Return true if this is a branch which always
279 /// transfers control flow to some other block. The
280 /// TargetInstrInfo::AnalyzeBranch method can be used to get more information
281 /// about this branch.
282 bool isUnconditionalBranch() const {
283 return isBranch() & isBarrier() & !isIndirectBranch();
286 /// \brief Return true if this is a branch or an instruction which directly
287 /// writes to the program counter. Considered 'may' affect rather than
288 /// 'does' affect as things like predication are not taken into account.
289 bool mayAffectControlFlow(const MCInst &MI, const MCRegisterInfo &RI) const {
290 if (isBranch() || isCall() || isReturn() || isIndirectBranch())
292 unsigned PC = RI.getProgramCounter();
295 if (hasDefOfPhysReg(MI, PC, RI))
297 // A variadic instruction may define PC in the variable operand list.
298 // There's currently no indication of which entries in a variable
299 // list are defs and which are uses. While that's the case, this function
300 // needs to assume they're defs in order to be conservatively correct.
301 for (int i = NumOperands, e = MI.getNumOperands(); i != e; ++i) {
302 if (MI.getOperand(i).isReg() &&
303 RI.isSubRegisterEq(PC, MI.getOperand(i).getReg()))
309 /// \brief Return true if this instruction has a predicate operand
310 /// that controls execution. It may be set to 'always', or may be set to other
311 /// values. There are various methods in TargetInstrInfo that can be used to
312 /// control and modify the predicate in this instruction.
313 bool isPredicable() const {
314 return Flags & (1 << MCID::Predicable);
317 /// \brief Return true if this instruction is a comparison.
318 bool isCompare() const {
319 return Flags & (1 << MCID::Compare);
322 /// \brief Return true if this instruction is a move immediate
323 /// (including conditional moves) instruction.
324 bool isMoveImmediate() const {
325 return Flags & (1 << MCID::MoveImm);
328 /// \brief Return true if this instruction is a bitcast instruction.
329 bool isBitcast() const {
330 return Flags & (1 << MCID::Bitcast);
333 /// \brief Return true if this is a select instruction.
334 bool isSelect() const {
335 return Flags & (1 << MCID::Select);
338 /// \brief Return true if this instruction cannot be safely
339 /// duplicated. For example, if the instruction has a unique labels attached
340 /// to it, duplicating it would cause multiple definition errors.
341 bool isNotDuplicable() const {
342 return Flags & (1 << MCID::NotDuplicable);
345 /// hasDelaySlot - Returns true if the specified instruction has a delay slot
346 /// which must be filled by the code generator.
347 bool hasDelaySlot() const {
348 return Flags & (1 << MCID::DelaySlot);
351 /// canFoldAsLoad - Return true for instructions that can be folded as
352 /// memory operands in other instructions. The most common use for this
353 /// is instructions that are simple loads from memory that don't modify
354 /// the loaded value in any way, but it can also be used for instructions
355 /// that can be expressed as constant-pool loads, such as V_SETALLONES
356 /// on x86, to allow them to be folded when it is beneficial.
357 /// This should only be set on instructions that return a value in their
358 /// only virtual register definition.
359 bool canFoldAsLoad() const {
360 return Flags & (1 << MCID::FoldableAsLoad);
363 /// \brief Return true if this instruction behaves
364 /// the same way as the generic REG_SEQUENCE instructions.
366 /// dX VMOVDRR rY, rZ
368 /// dX = REG_SEQUENCE rY, ssub_0, rZ, ssub_1.
370 /// Note that for the optimizers to be able to take advantage of
371 /// this property, TargetInstrInfo::getRegSequenceLikeInputs has to be
372 /// override accordingly.
373 bool isRegSequenceLike() const { return Flags & (1 << MCID::RegSequence); }
375 /// \brief Return true if this instruction behaves
376 /// the same way as the generic EXTRACT_SUBREG instructions.
378 /// rX, rY VMOVRRD dZ
379 /// is equivalent to two EXTRACT_SUBREG:
380 /// rX = EXTRACT_SUBREG dZ, ssub_0
381 /// rY = EXTRACT_SUBREG dZ, ssub_1
383 /// Note that for the optimizers to be able to take advantage of
384 /// this property, TargetInstrInfo::getExtractSubregLikeInputs has to be
385 /// override accordingly.
386 bool isExtractSubregLike() const {
387 return Flags & (1 << MCID::ExtractSubreg);
390 /// \brief Return true if this instruction behaves
391 /// the same way as the generic INSERT_SUBREG instructions.
393 /// dX = VSETLNi32 dY, rZ, Imm
394 /// is equivalent to a INSERT_SUBREG:
395 /// dX = INSERT_SUBREG dY, rZ, translateImmToSubIdx(Imm)
397 /// Note that for the optimizers to be able to take advantage of
398 /// this property, TargetInstrInfo::getInsertSubregLikeInputs has to be
399 /// override accordingly.
400 bool isInsertSubregLike() const {
401 return Flags & (1 << MCID::InsertSubreg);
404 //===--------------------------------------------------------------------===//
405 // Side Effect Analysis
406 //===--------------------------------------------------------------------===//
408 /// \brief Return true if this instruction could possibly read memory.
409 /// Instructions with this flag set are not necessarily simple load
410 /// instructions, they may load a value and modify it, for example.
411 bool mayLoad() const {
412 return Flags & (1 << MCID::MayLoad);
416 /// \brief Return true if this instruction could possibly modify memory.
417 /// Instructions with this flag set are not necessarily simple store
418 /// instructions, they may store a modified value based on their operands, or
419 /// may not actually modify anything, for example.
420 bool mayStore() const {
421 return Flags & (1 << MCID::MayStore);
424 /// hasUnmodeledSideEffects - Return true if this instruction has side
425 /// effects that are not modeled by other flags. This does not return true
426 /// for instructions whose effects are captured by:
428 /// 1. Their operand list and implicit definition/use list. Register use/def
429 /// info is explicit for instructions.
430 /// 2. Memory accesses. Use mayLoad/mayStore.
431 /// 3. Calling, branching, returning: use isCall/isReturn/isBranch.
433 /// Examples of side effects would be modifying 'invisible' machine state like
434 /// a control register, flushing a cache, modifying a register invisible to
437 bool hasUnmodeledSideEffects() const {
438 return Flags & (1 << MCID::UnmodeledSideEffects);
441 //===--------------------------------------------------------------------===//
442 // Flags that indicate whether an instruction can be modified by a method.
443 //===--------------------------------------------------------------------===//
445 /// isCommutable - Return true if this may be a 2- or 3-address
446 /// instruction (of the form "X = op Y, Z, ..."), which produces the same
447 /// result if Y and Z are exchanged. If this flag is set, then the
448 /// TargetInstrInfo::commuteInstruction method may be used to hack on the
451 /// Note that this flag may be set on instructions that are only commutable
452 /// sometimes. In these cases, the call to commuteInstruction will fail.
453 /// Also note that some instructions require non-trivial modification to
455 bool isCommutable() const {
456 return Flags & (1 << MCID::Commutable);
459 /// isConvertibleTo3Addr - Return true if this is a 2-address instruction
460 /// which can be changed into a 3-address instruction if needed. Doing this
461 /// transformation can be profitable in the register allocator, because it
462 /// means that the instruction can use a 2-address form if possible, but
463 /// degrade into a less efficient form if the source and dest register cannot
464 /// be assigned to the same register. For example, this allows the x86
465 /// backend to turn a "shl reg, 3" instruction into an LEA instruction, which
466 /// is the same speed as the shift but has bigger code size.
468 /// If this returns true, then the target must implement the
469 /// TargetInstrInfo::convertToThreeAddress method for this instruction, which
470 /// is allowed to fail if the transformation isn't valid for this specific
471 /// instruction (e.g. shl reg, 4 on x86).
473 bool isConvertibleTo3Addr() const {
474 return Flags & (1 << MCID::ConvertibleTo3Addr);
477 /// usesCustomInsertionHook - Return true if this instruction requires
478 /// custom insertion support when the DAG scheduler is inserting it into a
479 /// machine basic block. If this is true for the instruction, it basically
480 /// means that it is a pseudo instruction used at SelectionDAG time that is
481 /// expanded out into magic code by the target when MachineInstrs are formed.
483 /// If this is true, the TargetLoweringInfo::InsertAtEndOfBasicBlock method
484 /// is used to insert this into the MachineBasicBlock.
485 bool usesCustomInsertionHook() const {
486 return Flags & (1 << MCID::UsesCustomInserter);
489 /// hasPostISelHook - Return true if this instruction requires *adjustment*
490 /// after instruction selection by calling a target hook. For example, this
491 /// can be used to fill in ARM 's' optional operand depending on whether
492 /// the conditional flag register is used.
493 bool hasPostISelHook() const {
494 return Flags & (1 << MCID::HasPostISelHook);
497 /// isRematerializable - Returns true if this instruction is a candidate for
498 /// remat. This flag is only used in TargetInstrInfo method
499 /// isTriviallyRematerializable.
501 /// If this flag is set, the isReallyTriviallyReMaterializable()
502 /// or isReallyTriviallyReMaterializableGeneric methods are called to verify
503 /// the instruction is really rematable.
504 bool isRematerializable() const {
505 return Flags & (1 << MCID::Rematerializable);
508 /// isAsCheapAsAMove - Returns true if this instruction has the same cost (or
509 /// less) than a move instruction. This is useful during certain types of
510 /// optimizations (e.g., remat during two-address conversion or machine licm)
511 /// where we would like to remat or hoist the instruction, but not if it costs
512 /// more than moving the instruction into the appropriate register. Note, we
513 /// are not marking copies from and to the same register class with this flag.
515 /// This method could be called by interface TargetInstrInfo::isAsCheapAsAMove
516 /// for different subtargets.
517 bool isAsCheapAsAMove() const {
518 return Flags & (1 << MCID::CheapAsAMove);
521 /// hasExtraSrcRegAllocReq - Returns true if this instruction source operands
522 /// have special register allocation requirements that are not captured by the
523 /// operand register classes. e.g. ARM::STRD's two source registers must be an
524 /// even / odd pair, ARM::STM registers have to be in ascending order.
525 /// Post-register allocation passes should not attempt to change allocations
526 /// for sources of instructions with this flag.
527 bool hasExtraSrcRegAllocReq() const {
528 return Flags & (1 << MCID::ExtraSrcRegAllocReq);
531 /// hasExtraDefRegAllocReq - Returns true if this instruction def operands
532 /// have special register allocation requirements that are not captured by the
533 /// operand register classes. e.g. ARM::LDRD's two def registers must be an
534 /// even / odd pair, ARM::LDM registers have to be in ascending order.
535 /// Post-register allocation passes should not attempt to change allocations
536 /// for definitions of instructions with this flag.
537 bool hasExtraDefRegAllocReq() const {
538 return Flags & (1 << MCID::ExtraDefRegAllocReq);
542 /// getImplicitUses - Return a list of registers that are potentially
543 /// read by any instance of this machine instruction. For example, on X86,
544 /// the "adc" instruction adds two register operands and adds the carry bit in
545 /// from the flags register. In this case, the instruction is marked as
546 /// implicitly reading the flags. Likewise, the variable shift instruction on
547 /// X86 is marked as implicitly reading the 'CL' register, which it always
550 /// This method returns null if the instruction has no implicit uses.
551 const uint16_t *getImplicitUses() const {
555 /// \brief Return the number of implicit uses this instruction has.
556 unsigned getNumImplicitUses() const {
557 if (!ImplicitUses) return 0;
559 for (; ImplicitUses[i]; ++i) /*empty*/;
563 /// getImplicitDefs - Return a list of registers that are potentially
564 /// written by any instance of this machine instruction. For example, on X86,
565 /// many instructions implicitly set the flags register. In this case, they
566 /// are marked as setting the FLAGS. Likewise, many instructions always
567 /// deposit their result in a physical register. For example, the X86 divide
568 /// instruction always deposits the quotient and remainder in the EAX/EDX
569 /// registers. For that instruction, this will return a list containing the
570 /// EAX/EDX/EFLAGS registers.
572 /// This method returns null if the instruction has no implicit defs.
573 const uint16_t *getImplicitDefs() const {
577 /// \brief Return the number of implicit defs this instruct has.
578 unsigned getNumImplicitDefs() const {
579 if (!ImplicitDefs) return 0;
581 for (; ImplicitDefs[i]; ++i) /*empty*/;
585 /// \brief Return true if this instruction implicitly
586 /// uses the specified physical register.
587 bool hasImplicitUseOfPhysReg(unsigned Reg) const {
588 if (const uint16_t *ImpUses = ImplicitUses)
589 for (; *ImpUses; ++ImpUses)
590 if (*ImpUses == Reg) return true;
594 /// \brief Return true if this instruction implicitly
595 /// defines the specified physical register.
596 bool hasImplicitDefOfPhysReg(unsigned Reg,
597 const MCRegisterInfo *MRI = nullptr) const {
598 if (const uint16_t *ImpDefs = ImplicitDefs)
599 for (; *ImpDefs; ++ImpDefs)
600 if (*ImpDefs == Reg || (MRI && MRI->isSubRegister(Reg, *ImpDefs)))
605 /// \brief Return true if this instruction defines the specified physical
606 /// register, either explicitly or implicitly.
607 bool hasDefOfPhysReg(const MCInst &MI, unsigned Reg,
608 const MCRegisterInfo &RI) const {
609 for (int i = 0, e = NumDefs; i != e; ++i)
610 if (MI.getOperand(i).isReg() &&
611 RI.isSubRegisterEq(Reg, MI.getOperand(i).getReg()))
613 return hasImplicitDefOfPhysReg(Reg, &RI);
616 /// \brief Return the scheduling class for this instruction. The
617 /// scheduling class is an index into the InstrItineraryData table. This
618 /// returns zero if there is no known scheduling information for the
620 unsigned getSchedClass() const {
624 /// \brief Return the number of bytes in the encoding of this instruction,
625 /// or zero if the encoding size cannot be known from the opcode.
626 unsigned getSize() const {
630 /// \brief Find the index of the first operand in the
631 /// operand list that is used to represent the predicate. It returns -1 if
633 int findFirstPredOperandIdx() const {
634 if (isPredicable()) {
635 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
636 if (OpInfo[i].isPredicate())
643 } // end namespace llvm