CodeGenInstruction.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. //===- CodeGenInstruction.h - Instruction Class Wrapper ---------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file defines a wrapper class for the 'Instruction' TableGen class.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_UTILS_TABLEGEN_CODEGENINSTRUCTION_H
  13. #define LLVM_UTILS_TABLEGEN_CODEGENINSTRUCTION_H
  14. #include "llvm/ADT/BitVector.h"
  15. #include "llvm/ADT/StringRef.h"
  16. #include "llvm/Support/MachineValueType.h"
  17. #include "llvm/Support/SMLoc.h"
  18. #include <cassert>
  19. #include <string>
  20. #include <utility>
  21. #include <vector>
  22. namespace llvm {
  23. template <typename T> class ArrayRef;
  24. class Record;
  25. class DagInit;
  26. class CodeGenTarget;
  27. class CGIOperandList {
  28. public:
  29. class ConstraintInfo {
  30. enum { None, EarlyClobber, Tied } Kind = None;
  31. unsigned OtherTiedOperand = 0;
  32. public:
  33. ConstraintInfo() = default;
  34. static ConstraintInfo getEarlyClobber() {
  35. ConstraintInfo I;
  36. I.Kind = EarlyClobber;
  37. I.OtherTiedOperand = 0;
  38. return I;
  39. }
  40. static ConstraintInfo getTied(unsigned Op) {
  41. ConstraintInfo I;
  42. I.Kind = Tied;
  43. I.OtherTiedOperand = Op;
  44. return I;
  45. }
  46. bool isNone() const { return Kind == None; }
  47. bool isEarlyClobber() const { return Kind == EarlyClobber; }
  48. bool isTied() const { return Kind == Tied; }
  49. unsigned getTiedOperand() const {
  50. assert(isTied());
  51. return OtherTiedOperand;
  52. }
  53. bool operator==(const ConstraintInfo &RHS) const {
  54. if (Kind != RHS.Kind)
  55. return false;
  56. if (Kind == Tied && OtherTiedOperand != RHS.OtherTiedOperand)
  57. return false;
  58. return true;
  59. }
  60. bool operator!=(const ConstraintInfo &RHS) const {
  61. return !(*this == RHS);
  62. }
  63. };
  64. /// OperandInfo - The information we keep track of for each operand in the
  65. /// operand list for a tablegen instruction.
  66. struct OperandInfo {
  67. /// Rec - The definition this operand is declared as.
  68. ///
  69. Record *Rec;
  70. /// Name - If this operand was assigned a symbolic name, this is it,
  71. /// otherwise, it's empty.
  72. std::string Name;
  73. /// PrinterMethodName - The method used to print operands of this type in
  74. /// the asmprinter.
  75. std::string PrinterMethodName;
  76. /// EncoderMethodName - The method used to get the machine operand value
  77. /// for binary encoding. "getMachineOpValue" by default.
  78. std::string EncoderMethodName;
  79. /// OperandType - A value from MCOI::OperandType representing the type of
  80. /// the operand.
  81. std::string OperandType;
  82. /// MIOperandNo - Currently (this is meant to be phased out), some logical
  83. /// operands correspond to multiple MachineInstr operands. In the X86
  84. /// target for example, one address operand is represented as 4
  85. /// MachineOperands. Because of this, the operand number in the
  86. /// OperandList may not match the MachineInstr operand num. Until it
  87. /// does, this contains the MI operand index of this operand.
  88. unsigned MIOperandNo;
  89. unsigned MINumOperands; // The number of operands.
  90. /// DoNotEncode - Bools are set to true in this vector for each operand in
  91. /// the DisableEncoding list. These should not be emitted by the code
  92. /// emitter.
  93. BitVector DoNotEncode;
  94. /// MIOperandInfo - Default MI operand type. Note an operand may be made
  95. /// up of multiple MI operands.
  96. DagInit *MIOperandInfo;
  97. /// Constraint info for this operand. This operand can have pieces, so we
  98. /// track constraint info for each.
  99. std::vector<ConstraintInfo> Constraints;
  100. OperandInfo(Record *R, const std::string &N, const std::string &PMN,
  101. const std::string &EMN, const std::string &OT, unsigned MION,
  102. unsigned MINO, DagInit *MIOI)
  103. : Rec(R), Name(N), PrinterMethodName(PMN), EncoderMethodName(EMN),
  104. OperandType(OT), MIOperandNo(MION), MINumOperands(MINO),
  105. MIOperandInfo(MIOI) {}
  106. /// getTiedOperand - If this operand is tied to another one, return the
  107. /// other operand number. Otherwise, return -1.
  108. int getTiedRegister() const {
  109. for (unsigned j = 0, e = Constraints.size(); j != e; ++j) {
  110. const CGIOperandList::ConstraintInfo &CI = Constraints[j];
  111. if (CI.isTied()) return CI.getTiedOperand();
  112. }
  113. return -1;
  114. }
  115. };
  116. CGIOperandList(Record *D);
  117. Record *TheDef; // The actual record containing this OperandList.
  118. /// NumDefs - Number of def operands declared, this is the number of
  119. /// elements in the instruction's (outs) list.
  120. ///
  121. unsigned NumDefs;
  122. /// OperandList - The list of declared operands, along with their declared
  123. /// type (which is a record).
  124. std::vector<OperandInfo> OperandList;
  125. // Information gleaned from the operand list.
  126. bool isPredicable;
  127. bool hasOptionalDef;
  128. bool isVariadic;
  129. // Provide transparent accessors to the operand list.
  130. bool empty() const { return OperandList.empty(); }
  131. unsigned size() const { return OperandList.size(); }
  132. const OperandInfo &operator[](unsigned i) const { return OperandList[i]; }
  133. OperandInfo &operator[](unsigned i) { return OperandList[i]; }
  134. OperandInfo &back() { return OperandList.back(); }
  135. const OperandInfo &back() const { return OperandList.back(); }
  136. typedef std::vector<OperandInfo>::iterator iterator;
  137. typedef std::vector<OperandInfo>::const_iterator const_iterator;
  138. iterator begin() { return OperandList.begin(); }
  139. const_iterator begin() const { return OperandList.begin(); }
  140. iterator end() { return OperandList.end(); }
  141. const_iterator end() const { return OperandList.end(); }
  142. /// getOperandNamed - Return the index of the operand with the specified
  143. /// non-empty name. If the instruction does not have an operand with the
  144. /// specified name, abort.
  145. unsigned getOperandNamed(StringRef Name) const;
  146. /// hasOperandNamed - Query whether the instruction has an operand of the
  147. /// given name. If so, return true and set OpIdx to the index of the
  148. /// operand. Otherwise, return false.
  149. bool hasOperandNamed(StringRef Name, unsigned &OpIdx) const;
  150. /// ParseOperandName - Parse an operand name like "$foo" or "$foo.bar",
  151. /// where $foo is a whole operand and $foo.bar refers to a suboperand.
  152. /// This aborts if the name is invalid. If AllowWholeOp is true, references
  153. /// to operands with suboperands are allowed, otherwise not.
  154. std::pair<unsigned,unsigned> ParseOperandName(StringRef Op,
  155. bool AllowWholeOp = true);
  156. /// getFlattenedOperandNumber - Flatten a operand/suboperand pair into a
  157. /// flat machineinstr operand #.
  158. unsigned getFlattenedOperandNumber(std::pair<unsigned,unsigned> Op) const {
  159. return OperandList[Op.first].MIOperandNo + Op.second;
  160. }
  161. /// getSubOperandNumber - Unflatten a operand number into an
  162. /// operand/suboperand pair.
  163. std::pair<unsigned,unsigned> getSubOperandNumber(unsigned Op) const {
  164. for (unsigned i = 0; ; ++i) {
  165. assert(i < OperandList.size() && "Invalid flat operand #");
  166. if (OperandList[i].MIOperandNo+OperandList[i].MINumOperands > Op)
  167. return std::make_pair(i, Op-OperandList[i].MIOperandNo);
  168. }
  169. }
  170. /// isFlatOperandNotEmitted - Return true if the specified flat operand #
  171. /// should not be emitted with the code emitter.
  172. bool isFlatOperandNotEmitted(unsigned FlatOpNo) const {
  173. std::pair<unsigned,unsigned> Op = getSubOperandNumber(FlatOpNo);
  174. if (OperandList[Op.first].DoNotEncode.size() > Op.second)
  175. return OperandList[Op.first].DoNotEncode[Op.second];
  176. return false;
  177. }
  178. void ProcessDisableEncoding(StringRef Value);
  179. };
  180. class CodeGenInstruction {
  181. public:
  182. Record *TheDef; // The actual record defining this instruction.
  183. StringRef Namespace; // The namespace the instruction is in.
  184. /// AsmString - The format string used to emit a .s file for the
  185. /// instruction.
  186. std::string AsmString;
  187. /// Operands - This is information about the (ins) and (outs) list specified
  188. /// to the instruction.
  189. CGIOperandList Operands;
  190. /// ImplicitDefs/ImplicitUses - These are lists of registers that are
  191. /// implicitly defined and used by the instruction.
  192. std::vector<Record*> ImplicitDefs, ImplicitUses;
  193. // Various boolean values we track for the instruction.
  194. bool isPreISelOpcode : 1;
  195. bool isReturn : 1;
  196. bool isEHScopeReturn : 1;
  197. bool isBranch : 1;
  198. bool isIndirectBranch : 1;
  199. bool isCompare : 1;
  200. bool isMoveImm : 1;
  201. bool isMoveReg : 1;
  202. bool isBitcast : 1;
  203. bool isSelect : 1;
  204. bool isBarrier : 1;
  205. bool isCall : 1;
  206. bool isAdd : 1;
  207. bool isTrap : 1;
  208. bool canFoldAsLoad : 1;
  209. bool mayLoad : 1;
  210. bool mayLoad_Unset : 1;
  211. bool mayStore : 1;
  212. bool mayStore_Unset : 1;
  213. bool mayRaiseFPException : 1;
  214. bool isPredicable : 1;
  215. bool isConvertibleToThreeAddress : 1;
  216. bool isCommutable : 1;
  217. bool isTerminator : 1;
  218. bool isReMaterializable : 1;
  219. bool hasDelaySlot : 1;
  220. bool usesCustomInserter : 1;
  221. bool hasPostISelHook : 1;
  222. bool hasCtrlDep : 1;
  223. bool isNotDuplicable : 1;
  224. bool hasSideEffects : 1;
  225. bool hasSideEffects_Unset : 1;
  226. bool isAsCheapAsAMove : 1;
  227. bool hasExtraSrcRegAllocReq : 1;
  228. bool hasExtraDefRegAllocReq : 1;
  229. bool isCodeGenOnly : 1;
  230. bool isPseudo : 1;
  231. bool isRegSequence : 1;
  232. bool isExtractSubreg : 1;
  233. bool isInsertSubreg : 1;
  234. bool isConvergent : 1;
  235. bool hasNoSchedulingInfo : 1;
  236. bool FastISelShouldIgnore : 1;
  237. bool hasChain : 1;
  238. bool hasChain_Inferred : 1;
  239. bool variadicOpsAreDefs : 1;
  240. bool isAuthenticated : 1;
  241. std::string DeprecatedReason;
  242. bool HasComplexDeprecationPredicate;
  243. /// Are there any undefined flags?
  244. bool hasUndefFlags() const {
  245. return mayLoad_Unset || mayStore_Unset || hasSideEffects_Unset;
  246. }
  247. // The record used to infer instruction flags, or NULL if no flag values
  248. // have been inferred.
  249. Record *InferredFrom;
  250. CodeGenInstruction(Record *R);
  251. /// HasOneImplicitDefWithKnownVT - If the instruction has at least one
  252. /// implicit def and it has a known VT, return the VT, otherwise return
  253. /// MVT::Other.
  254. MVT::SimpleValueType
  255. HasOneImplicitDefWithKnownVT(const CodeGenTarget &TargetInfo) const;
  256. /// FlattenAsmStringVariants - Flatten the specified AsmString to only
  257. /// include text from the specified variant, returning the new string.
  258. static std::string FlattenAsmStringVariants(StringRef AsmString,
  259. unsigned Variant);
  260. // Is the specified operand in a generic instruction implicitly a pointer.
  261. // This can be used on intructions that use typeN or ptypeN to identify
  262. // operands that should be considered as pointers even though SelectionDAG
  263. // didn't make a distinction between integer and pointers.
  264. bool isOperandAPointer(unsigned i) const {
  265. return isOperandImpl(i, "IsPointer");
  266. }
  267. /// Check if the operand is required to be an immediate.
  268. bool isOperandImmArg(unsigned i) const {
  269. return isOperandImpl(i, "IsImmediate");
  270. }
  271. private:
  272. bool isOperandImpl(unsigned i, StringRef PropertyName) const;
  273. };
  274. /// CodeGenInstAlias - This represents an InstAlias definition.
  275. class CodeGenInstAlias {
  276. public:
  277. Record *TheDef; // The actual record defining this InstAlias.
  278. /// AsmString - The format string used to emit a .s file for the
  279. /// instruction.
  280. std::string AsmString;
  281. /// Result - The result instruction.
  282. DagInit *Result;
  283. /// ResultInst - The instruction generated by the alias (decoded from
  284. /// Result).
  285. CodeGenInstruction *ResultInst;
  286. struct ResultOperand {
  287. private:
  288. std::string Name;
  289. Record *R = nullptr;
  290. int64_t Imm = 0;
  291. public:
  292. enum {
  293. K_Record,
  294. K_Imm,
  295. K_Reg
  296. } Kind;
  297. ResultOperand(std::string N, Record *r)
  298. : Name(std::move(N)), R(r), Kind(K_Record) {}
  299. ResultOperand(int64_t I) : Imm(I), Kind(K_Imm) {}
  300. ResultOperand(Record *r) : R(r), Kind(K_Reg) {}
  301. bool isRecord() const { return Kind == K_Record; }
  302. bool isImm() const { return Kind == K_Imm; }
  303. bool isReg() const { return Kind == K_Reg; }
  304. StringRef getName() const { assert(isRecord()); return Name; }
  305. Record *getRecord() const { assert(isRecord()); return R; }
  306. int64_t getImm() const { assert(isImm()); return Imm; }
  307. Record *getRegister() const { assert(isReg()); return R; }
  308. unsigned getMINumOperands() const;
  309. };
  310. /// ResultOperands - The decoded operands for the result instruction.
  311. std::vector<ResultOperand> ResultOperands;
  312. /// ResultInstOperandIndex - For each operand, this vector holds a pair of
  313. /// indices to identify the corresponding operand in the result
  314. /// instruction. The first index specifies the operand and the second
  315. /// index specifies the suboperand. If there are no suboperands or if all
  316. /// of them are matched by the operand, the second value should be -1.
  317. std::vector<std::pair<unsigned, int> > ResultInstOperandIndex;
  318. CodeGenInstAlias(Record *R, CodeGenTarget &T);
  319. bool tryAliasOpMatch(DagInit *Result, unsigned AliasOpNo,
  320. Record *InstOpRec, bool hasSubOps, ArrayRef<SMLoc> Loc,
  321. CodeGenTarget &T, ResultOperand &ResOp);
  322. };
  323. }
  324. #endif