CodeGenInstruction.h 12 KB

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