CodeGenInstruction.h 14 KB

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