Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

564 lines
22 KiB

  1. //===-- llvm/MC/MCInstrDesc.h - Instruction Descriptors -*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file defines the MCOperandInfo and MCInstrDesc classes, which
  11. // are used to describe target instructions and their operands.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_MC_MCINSTRDESC_H
  15. #define LLVM_MC_MCINSTRDESC_H
  16. #include "llvm/MC/MCInst.h"
  17. #include "llvm/MC/MCRegisterInfo.h"
  18. #include "llvm/Support/DataTypes.h"
  19. namespace llvm {
  20. //===----------------------------------------------------------------------===//
  21. // Machine Operand Flags and Description
  22. //===----------------------------------------------------------------------===//
  23. namespace MCOI {
  24. // Operand constraints
  25. enum OperandConstraint {
  26. TIED_TO = 0, // Must be allocated the same register as.
  27. EARLY_CLOBBER // Operand is an early clobber register operand
  28. };
  29. /// OperandFlags - These are flags set on operands, but should be considered
  30. /// private, all access should go through the MCOperandInfo accessors.
  31. /// See the accessors for a description of what these are.
  32. enum OperandFlags {
  33. LookupPtrRegClass = 0,
  34. Predicate,
  35. OptionalDef
  36. };
  37. /// Operand Type - Operands are tagged with one of the values of this enum.
  38. enum OperandType {
  39. OPERAND_UNKNOWN,
  40. OPERAND_IMMEDIATE,
  41. OPERAND_REGISTER,
  42. OPERAND_MEMORY,
  43. OPERAND_PCREL
  44. };
  45. }
  46. /// MCOperandInfo - This holds information about one operand of a machine
  47. /// instruction, indicating the register class for register operands, etc.
  48. ///
  49. class MCOperandInfo {
  50. public:
  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.
  55. int16_t RegClass;
  56. /// Flags - These are flags from the MCOI::OperandFlags enum.
  57. uint8_t Flags;
  58. /// OperandType - Information about the type of the operand.
  59. uint8_t OperandType;
  60. /// Lower 16 bits are used to specify which constraints are set. The higher 16
  61. /// bits are used to specify the value of constraints (4 bits each).
  62. uint32_t Constraints;
  63. /// Currently no other information.
  64. /// isLookupPtrRegClass - Set if this operand is a pointer value and it
  65. /// requires a callback to look up its register class.
  66. bool isLookupPtrRegClass() const {return Flags&(1 <<MCOI::LookupPtrRegClass);}
  67. /// isPredicate - Set if this is one of the operands that made up of
  68. /// the predicate operand that controls an isPredicable() instruction.
  69. bool isPredicate() const { return Flags & (1 << MCOI::Predicate); }
  70. /// isOptionalDef - Set if this operand is a optional def.
  71. ///
  72. bool isOptionalDef() const { return Flags & (1 << MCOI::OptionalDef); }
  73. };
  74. //===----------------------------------------------------------------------===//
  75. // Machine Instruction Flags and Description
  76. //===----------------------------------------------------------------------===//
  77. /// MCInstrDesc flags - These should be considered private to the
  78. /// implementation of the MCInstrDesc class. Clients should use the predicate
  79. /// methods on MCInstrDesc, not use these directly. These all correspond to
  80. /// bitfields in the MCInstrDesc::Flags field.
  81. namespace MCID {
  82. enum {
  83. Variadic = 0,
  84. HasOptionalDef,
  85. Pseudo,
  86. Return,
  87. Call,
  88. Barrier,
  89. Terminator,
  90. Branch,
  91. IndirectBranch,
  92. Compare,
  93. MoveImm,
  94. Bitcast,
  95. Select,
  96. DelaySlot,
  97. FoldableAsLoad,
  98. MayLoad,
  99. MayStore,
  100. Predicable,
  101. NotDuplicable,
  102. UnmodeledSideEffects,
  103. Commutable,
  104. ConvertibleTo3Addr,
  105. UsesCustomInserter,
  106. HasPostISelHook,
  107. Rematerializable,
  108. CheapAsAMove,
  109. ExtraSrcRegAllocReq,
  110. ExtraDefRegAllocReq
  111. };
  112. }
  113. /// MCInstrDesc - Describe properties that are true of each instruction in the
  114. /// target description file. This captures information about side effects,
  115. /// register use and many other things. There is one instance of this struct
  116. /// for each target instruction class, and the MachineInstr class points to
  117. /// this struct directly to describe itself.
  118. class MCInstrDesc {
  119. public:
  120. unsigned short Opcode; // The opcode number
  121. unsigned short NumOperands; // Num of args (may be more if variable_ops)
  122. unsigned short NumDefs; // Num of args that are definitions
  123. unsigned short SchedClass; // enum identifying instr sched class
  124. unsigned short Size; // Number of bytes in encoding.
  125. unsigned Flags; // Flags identifying machine instr class
  126. uint64_t TSFlags; // Target Specific Flag values
  127. const uint16_t *ImplicitUses; // Registers implicitly read by this instr
  128. const uint16_t *ImplicitDefs; // Registers implicitly defined by this instr
  129. const MCOperandInfo *OpInfo; // 'NumOperands' entries about operands
  130. /// \brief Returns the value of the specific constraint if
  131. /// it is set. Returns -1 if it is not set.
  132. int getOperandConstraint(unsigned OpNum,
  133. MCOI::OperandConstraint Constraint) const {
  134. if (OpNum < NumOperands &&
  135. (OpInfo[OpNum].Constraints & (1 << Constraint))) {
  136. unsigned Pos = 16 + Constraint * 4;
  137. return (int)(OpInfo[OpNum].Constraints >> Pos) & 0xf;
  138. }
  139. return -1;
  140. }
  141. /// \brief Return the opcode number for this descriptor.
  142. unsigned getOpcode() const {
  143. return Opcode;
  144. }
  145. /// \brief Return the number of declared MachineOperands for this
  146. /// MachineInstruction. Note that variadic (isVariadic() returns true)
  147. /// instructions may have additional operands at the end of the list, and note
  148. /// that the machine instruction may include implicit register def/uses as
  149. /// well.
  150. unsigned getNumOperands() const {
  151. return NumOperands;
  152. }
  153. /// \brief Return the number of MachineOperands that are register
  154. /// definitions. Register definitions always occur at the start of the
  155. /// machine operand list. This is the number of "outs" in the .td file,
  156. /// and does not include implicit defs.
  157. unsigned getNumDefs() const {
  158. return NumDefs;
  159. }
  160. /// \brief Return flags of this instruction.
  161. unsigned getFlags() const { return Flags; }
  162. /// \brief Return true if this instruction can have a variable number of
  163. /// operands. In this case, the variable operands will be after the normal
  164. /// operands but before the implicit definitions and uses (if any are
  165. /// present).
  166. bool isVariadic() const {
  167. return Flags & (1 << MCID::Variadic);
  168. }
  169. /// \brief Set if this instruction has an optional definition, e.g.
  170. /// ARM instructions which can set condition code if 's' bit is set.
  171. bool hasOptionalDef() const {
  172. return Flags & (1 << MCID::HasOptionalDef);
  173. }
  174. /// \brief Return true if this is a pseudo instruction that doesn't
  175. /// correspond to a real machine instruction.
  176. ///
  177. bool isPseudo() const {
  178. return Flags & (1 << MCID::Pseudo);
  179. }
  180. /// \brief Return true if the instruction is a return.
  181. bool isReturn() const {
  182. return Flags & (1 << MCID::Return);
  183. }
  184. /// \brief Return true if the instruction is a call.
  185. bool isCall() const {
  186. return Flags & (1 << MCID::Call);
  187. }
  188. /// \brief Returns true if the specified instruction stops control flow
  189. /// from executing the instruction immediately following it. Examples include
  190. /// unconditional branches and return instructions.
  191. bool isBarrier() const {
  192. return Flags & (1 << MCID::Barrier);
  193. }
  194. /// \brief Returns true if this instruction part of the terminator for
  195. /// a basic block. Typically this is things like return and branch
  196. /// instructions.
  197. ///
  198. /// Various passes use this to insert code into the bottom of a basic block,
  199. /// but before control flow occurs.
  200. bool isTerminator() const {
  201. return Flags & (1 << MCID::Terminator);
  202. }
  203. /// \brief Returns true if this is a conditional, unconditional, or
  204. /// indirect branch. Predicates below can be used to discriminate between
  205. /// these cases, and the TargetInstrInfo::AnalyzeBranch method can be used to
  206. /// get more information.
  207. bool isBranch() const {
  208. return Flags & (1 << MCID::Branch);
  209. }
  210. /// \brief Return true if this is an indirect branch, such as a
  211. /// branch through a register.
  212. bool isIndirectBranch() const {
  213. return Flags & (1 << MCID::IndirectBranch);
  214. }
  215. /// \brief Return true if this is a branch which may fall
  216. /// through to the next instruction or may transfer control flow to some other
  217. /// block. The TargetInstrInfo::AnalyzeBranch method can be used to get more
  218. /// information about this branch.
  219. bool isConditionalBranch() const {
  220. return isBranch() & !isBarrier() & !isIndirectBranch();
  221. }
  222. /// \brief Return true if this is a branch which always
  223. /// transfers control flow to some other block. The
  224. /// TargetInstrInfo::AnalyzeBranch method can be used to get more information
  225. /// about this branch.
  226. bool isUnconditionalBranch() const {
  227. return isBranch() & isBarrier() & !isIndirectBranch();
  228. }
  229. /// \brief Return true if this is a branch or an instruction which directly
  230. /// writes to the program counter. Considered 'may' affect rather than
  231. /// 'does' affect as things like predication are not taken into account.
  232. bool mayAffectControlFlow(const MCInst &MI, const MCRegisterInfo &RI) const {
  233. if (isBranch() || isCall() || isReturn() || isIndirectBranch())
  234. return true;
  235. unsigned PC = RI.getProgramCounter();
  236. if (PC == 0) return false;
  237. return hasDefOfPhysReg(MI, PC, RI);
  238. }
  239. /// \brief Return true if this instruction has a predicate operand
  240. /// that controls execution. It may be set to 'always', or may be set to other
  241. /// values. There are various methods in TargetInstrInfo that can be used to
  242. /// control and modify the predicate in this instruction.
  243. bool isPredicable() const {
  244. return Flags & (1 << MCID::Predicable);
  245. }
  246. /// \brief Return true if this instruction is a comparison.
  247. bool isCompare() const {
  248. return Flags & (1 << MCID::Compare);
  249. }
  250. /// \brief Return true if this instruction is a move immediate
  251. /// (including conditional moves) instruction.
  252. bool isMoveImmediate() const {
  253. return Flags & (1 << MCID::MoveImm);
  254. }
  255. /// \brief Return true if this instruction is a bitcast instruction.
  256. bool isBitcast() const {
  257. return Flags & (1 << MCID::Bitcast);
  258. }
  259. /// \brief Return true if this is a select instruction.
  260. bool isSelect() const {
  261. return Flags & (1 << MCID::Select);
  262. }
  263. /// \brief Return true if this instruction cannot be safely
  264. /// duplicated. For example, if the instruction has a unique labels attached
  265. /// to it, duplicating it would cause multiple definition errors.
  266. bool isNotDuplicable() const {
  267. return Flags & (1 << MCID::NotDuplicable);
  268. }
  269. /// hasDelaySlot - Returns true if the specified instruction has a delay slot
  270. /// which must be filled by the code generator.
  271. bool hasDelaySlot() const {
  272. return Flags & (1 << MCID::DelaySlot);
  273. }
  274. /// canFoldAsLoad - Return true for instructions that can be folded as
  275. /// memory operands in other instructions. The most common use for this
  276. /// is instructions that are simple loads from memory that don't modify
  277. /// the loaded value in any way, but it can also be used for instructions
  278. /// that can be expressed as constant-pool loads, such as V_SETALLONES
  279. /// on x86, to allow them to be folded when it is beneficial.
  280. /// This should only be set on instructions that return a value in their
  281. /// only virtual register definition.
  282. bool canFoldAsLoad() const {
  283. return Flags & (1 << MCID::FoldableAsLoad);
  284. }
  285. //===--------------------------------------------------------------------===//
  286. // Side Effect Analysis
  287. //===--------------------------------------------------------------------===//
  288. /// \brief Return true if this instruction could possibly read memory.
  289. /// Instructions with this flag set are not necessarily simple load
  290. /// instructions, they may load a value and modify it, for example.
  291. bool mayLoad() const {
  292. return Flags & (1 << MCID::MayLoad);
  293. }
  294. /// \brief Return true if this instruction could possibly modify memory.
  295. /// Instructions with this flag set are not necessarily simple store
  296. /// instructions, they may store a modified value based on their operands, or
  297. /// may not actually modify anything, for example.
  298. bool mayStore() const {
  299. return Flags & (1 << MCID::MayStore);
  300. }
  301. /// hasUnmodeledSideEffects - Return true if this instruction has side
  302. /// effects that are not modeled by other flags. This does not return true
  303. /// for instructions whose effects are captured by:
  304. ///
  305. /// 1. Their operand list and implicit definition/use list. Register use/def
  306. /// info is explicit for instructions.
  307. /// 2. Memory accesses. Use mayLoad/mayStore.
  308. /// 3. Calling, branching, returning: use isCall/isReturn/isBranch.
  309. ///
  310. /// Examples of side effects would be modifying 'invisible' machine state like
  311. /// a control register, flushing a cache, modifying a register invisible to
  312. /// LLVM, etc.
  313. ///
  314. bool hasUnmodeledSideEffects() const {
  315. return Flags & (1 << MCID::UnmodeledSideEffects);
  316. }
  317. //===--------------------------------------------------------------------===//
  318. // Flags that indicate whether an instruction can be modified by a method.
  319. //===--------------------------------------------------------------------===//
  320. /// isCommutable - Return true if this may be a 2- or 3-address
  321. /// instruction (of the form "X = op Y, Z, ..."), which produces the same
  322. /// result if Y and Z are exchanged. If this flag is set, then the
  323. /// TargetInstrInfo::commuteInstruction method may be used to hack on the
  324. /// instruction.
  325. ///
  326. /// Note that this flag may be set on instructions that are only commutable
  327. /// sometimes. In these cases, the call to commuteInstruction will fail.
  328. /// Also note that some instructions require non-trivial modification to
  329. /// commute them.
  330. bool isCommutable() const {
  331. return Flags & (1 << MCID::Commutable);
  332. }
  333. /// isConvertibleTo3Addr - Return true if this is a 2-address instruction
  334. /// which can be changed into a 3-address instruction if needed. Doing this
  335. /// transformation can be profitable in the register allocator, because it
  336. /// means that the instruction can use a 2-address form if possible, but
  337. /// degrade into a less efficient form if the source and dest register cannot
  338. /// be assigned to the same register. For example, this allows the x86
  339. /// backend to turn a "shl reg, 3" instruction into an LEA instruction, which
  340. /// is the same speed as the shift but has bigger code size.
  341. ///
  342. /// If this returns true, then the target must implement the
  343. /// TargetInstrInfo::convertToThreeAddress method for this instruction, which
  344. /// is allowed to fail if the transformation isn't valid for this specific
  345. /// instruction (e.g. shl reg, 4 on x86).
  346. ///
  347. bool isConvertibleTo3Addr() const {
  348. return Flags & (1 << MCID::ConvertibleTo3Addr);
  349. }
  350. /// usesCustomInsertionHook - Return true if this instruction requires
  351. /// custom insertion support when the DAG scheduler is inserting it into a
  352. /// machine basic block. If this is true for the instruction, it basically
  353. /// means that it is a pseudo instruction used at SelectionDAG time that is
  354. /// expanded out into magic code by the target when MachineInstrs are formed.
  355. ///
  356. /// If this is true, the TargetLoweringInfo::InsertAtEndOfBasicBlock method
  357. /// is used to insert this into the MachineBasicBlock.
  358. bool usesCustomInsertionHook() const {
  359. return Flags & (1 << MCID::UsesCustomInserter);
  360. }
  361. /// hasPostISelHook - Return true if this instruction requires *adjustment*
  362. /// after instruction selection by calling a target hook. For example, this
  363. /// can be used to fill in ARM 's' optional operand depending on whether
  364. /// the conditional flag register is used.
  365. bool hasPostISelHook() const {
  366. return Flags & (1 << MCID::HasPostISelHook);
  367. }
  368. /// isRematerializable - Returns true if this instruction is a candidate for
  369. /// remat. This flag is deprecated, please don't use it anymore. If this
  370. /// flag is set, the isReallyTriviallyReMaterializable() method is called to
  371. /// verify the instruction is really rematable.
  372. bool isRematerializable() const {
  373. return Flags & (1 << MCID::Rematerializable);
  374. }
  375. /// isAsCheapAsAMove - Returns true if this instruction has the same cost (or
  376. /// less) than a move instruction. This is useful during certain types of
  377. /// optimizations (e.g., remat during two-address conversion or machine licm)
  378. /// where we would like to remat or hoist the instruction, but not if it costs
  379. /// more than moving the instruction into the appropriate register. Note, we
  380. /// are not marking copies from and to the same register class with this flag.
  381. bool isAsCheapAsAMove() const {
  382. return Flags & (1 << MCID::CheapAsAMove);
  383. }
  384. /// hasExtraSrcRegAllocReq - Returns true if this instruction source operands
  385. /// have special register allocation requirements that are not captured by the
  386. /// operand register classes. e.g. ARM::STRD's two source registers must be an
  387. /// even / odd pair, ARM::STM registers have to be in ascending order.
  388. /// Post-register allocation passes should not attempt to change allocations
  389. /// for sources of instructions with this flag.
  390. bool hasExtraSrcRegAllocReq() const {
  391. return Flags & (1 << MCID::ExtraSrcRegAllocReq);
  392. }
  393. /// hasExtraDefRegAllocReq - Returns true if this instruction def operands
  394. /// have special register allocation requirements that are not captured by the
  395. /// operand register classes. e.g. ARM::LDRD's two def registers must be an
  396. /// even / odd pair, ARM::LDM registers have to be in ascending order.
  397. /// Post-register allocation passes should not attempt to change allocations
  398. /// for definitions of instructions with this flag.
  399. bool hasExtraDefRegAllocReq() const {
  400. return Flags & (1 << MCID::ExtraDefRegAllocReq);
  401. }
  402. /// getImplicitUses - Return a list of registers that are potentially
  403. /// read by any instance of this machine instruction. For example, on X86,
  404. /// the "adc" instruction adds two register operands and adds the carry bit in
  405. /// from the flags register. In this case, the instruction is marked as
  406. /// implicitly reading the flags. Likewise, the variable shift instruction on
  407. /// X86 is marked as implicitly reading the 'CL' register, which it always
  408. /// does.
  409. ///
  410. /// This method returns null if the instruction has no implicit uses.
  411. const uint16_t *getImplicitUses() const {
  412. return ImplicitUses;
  413. }
  414. /// \brief Return the number of implicit uses this instruction has.
  415. unsigned getNumImplicitUses() const {
  416. if (ImplicitUses == 0) return 0;
  417. unsigned i = 0;
  418. for (; ImplicitUses[i]; ++i) /*empty*/;
  419. return i;
  420. }
  421. /// getImplicitDefs - Return a list of registers that are potentially
  422. /// written by any instance of this machine instruction. For example, on X86,
  423. /// many instructions implicitly set the flags register. In this case, they
  424. /// are marked as setting the FLAGS. Likewise, many instructions always
  425. /// deposit their result in a physical register. For example, the X86 divide
  426. /// instruction always deposits the quotient and remainder in the EAX/EDX
  427. /// registers. For that instruction, this will return a list containing the
  428. /// EAX/EDX/EFLAGS registers.
  429. ///
  430. /// This method returns null if the instruction has no implicit defs.
  431. const uint16_t *getImplicitDefs() const {
  432. return ImplicitDefs;
  433. }
  434. /// \brief Return the number of implicit defs this instruct has.
  435. unsigned getNumImplicitDefs() const {
  436. if (ImplicitDefs == 0) return 0;
  437. unsigned i = 0;
  438. for (; ImplicitDefs[i]; ++i) /*empty*/;
  439. return i;
  440. }
  441. /// \brief Return true if this instruction implicitly
  442. /// uses the specified physical register.
  443. bool hasImplicitUseOfPhysReg(unsigned Reg) const {
  444. if (const uint16_t *ImpUses = ImplicitUses)
  445. for (; *ImpUses; ++ImpUses)
  446. if (*ImpUses == Reg) return true;
  447. return false;
  448. }
  449. /// \brief Return true if this instruction implicitly
  450. /// defines the specified physical register.
  451. bool hasImplicitDefOfPhysReg(unsigned Reg,
  452. const MCRegisterInfo *MRI = 0) const {
  453. if (const uint16_t *ImpDefs = ImplicitDefs)
  454. for (; *ImpDefs; ++ImpDefs)
  455. if (*ImpDefs == Reg || (MRI && MRI->isSubRegister(Reg, *ImpDefs)))
  456. return true;
  457. return false;
  458. }
  459. /// \brief Return true if this instruction defines the specified physical
  460. /// register, either explicitly or implicitly.
  461. bool hasDefOfPhysReg(const MCInst &MI, unsigned Reg,
  462. const MCRegisterInfo &RI) const {
  463. for (int i = 0, e = NumDefs; i != e; ++i)
  464. if (MI.getOperand(i).isReg() &&
  465. RI.isSubRegisterEq(Reg, MI.getOperand(i).getReg()))
  466. return true;
  467. return hasImplicitDefOfPhysReg(Reg, &RI);
  468. }
  469. /// \brief Return the scheduling class for this instruction. The
  470. /// scheduling class is an index into the InstrItineraryData table. This
  471. /// returns zero if there is no known scheduling information for the
  472. /// instruction.
  473. unsigned getSchedClass() const {
  474. return SchedClass;
  475. }
  476. /// \brief Return the number of bytes in the encoding of this instruction,
  477. /// or zero if the encoding size cannot be known from the opcode.
  478. unsigned getSize() const {
  479. return Size;
  480. }
  481. /// \brief Find the index of the first operand in the
  482. /// operand list that is used to represent the predicate. It returns -1 if
  483. /// none is found.
  484. int findFirstPredOperandIdx() const {
  485. if (isPredicable()) {
  486. for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
  487. if (OpInfo[i].isPredicate())
  488. return i;
  489. }
  490. return -1;
  491. }
  492. };
  493. } // end namespace llvm
  494. #endif