CodeGenInstruction.h 14 KB

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