InlineAsm.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/InlineAsm.h - Class to represent inline asm strings -*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This class represents the inline asm strings, which are Value*'s that are
  15. // used as the callee operand of call instructions. InlineAsm's are uniqued
  16. // like constants, and created via InlineAsm::get(...).
  17. //
  18. //===----------------------------------------------------------------------===//
  19. #ifndef LLVM_IR_INLINEASM_H
  20. #define LLVM_IR_INLINEASM_H
  21. #include "llvm/ADT/StringRef.h"
  22. #include "llvm/IR/Value.h"
  23. #include "llvm/Support/ErrorHandling.h"
  24. #include <cassert>
  25. #include <string>
  26. #include <vector>
  27. namespace llvm {
  28. class FunctionType;
  29. class PointerType;
  30. template <class ConstantClass> class ConstantUniqueMap;
  31. class InlineAsm final : public Value {
  32. public:
  33. enum AsmDialect {
  34. AD_ATT,
  35. AD_Intel
  36. };
  37. private:
  38. friend struct InlineAsmKeyType;
  39. friend class ConstantUniqueMap<InlineAsm>;
  40. std::string AsmString, Constraints;
  41. FunctionType *FTy;
  42. bool HasSideEffects;
  43. bool IsAlignStack;
  44. AsmDialect Dialect;
  45. InlineAsm(FunctionType *Ty, const std::string &AsmString,
  46. const std::string &Constraints, bool hasSideEffects,
  47. bool isAlignStack, AsmDialect asmDialect);
  48. /// When the ConstantUniqueMap merges two types and makes two InlineAsms
  49. /// identical, it destroys one of them with this method.
  50. void destroyConstant();
  51. public:
  52. InlineAsm(const InlineAsm &) = delete;
  53. InlineAsm &operator=(const InlineAsm &) = delete;
  54. /// InlineAsm::get - Return the specified uniqued inline asm string.
  55. ///
  56. static InlineAsm *get(FunctionType *Ty, StringRef AsmString,
  57. StringRef Constraints, bool hasSideEffects,
  58. bool isAlignStack = false,
  59. AsmDialect asmDialect = AD_ATT);
  60. bool hasSideEffects() const { return HasSideEffects; }
  61. bool isAlignStack() const { return IsAlignStack; }
  62. AsmDialect getDialect() const { return Dialect; }
  63. /// getType - InlineAsm's are always pointers.
  64. ///
  65. PointerType *getType() const {
  66. return reinterpret_cast<PointerType*>(Value::getType());
  67. }
  68. /// getFunctionType - InlineAsm's are always pointers to functions.
  69. ///
  70. FunctionType *getFunctionType() const;
  71. const std::string &getAsmString() const { return AsmString; }
  72. const std::string &getConstraintString() const { return Constraints; }
  73. /// Verify - This static method can be used by the parser to check to see if
  74. /// the specified constraint string is legal for the type. This returns true
  75. /// if legal, false if not.
  76. ///
  77. static bool Verify(FunctionType *Ty, StringRef Constraints);
  78. // Constraint String Parsing
  79. enum ConstraintPrefix {
  80. isInput, // 'x'
  81. isOutput, // '=x'
  82. isClobber // '~x'
  83. };
  84. using ConstraintCodeVector = std::vector<std::string>;
  85. struct SubConstraintInfo {
  86. /// MatchingInput - If this is not -1, this is an output constraint where an
  87. /// input constraint is required to match it (e.g. "0"). The value is the
  88. /// constraint number that matches this one (for example, if this is
  89. /// constraint #0 and constraint #4 has the value "0", this will be 4).
  90. int MatchingInput = -1;
  91. /// Code - The constraint code, either the register name (in braces) or the
  92. /// constraint letter/number.
  93. ConstraintCodeVector Codes;
  94. /// Default constructor.
  95. SubConstraintInfo() = default;
  96. };
  97. using SubConstraintInfoVector = std::vector<SubConstraintInfo>;
  98. struct ConstraintInfo;
  99. using ConstraintInfoVector = std::vector<ConstraintInfo>;
  100. struct ConstraintInfo {
  101. /// Type - The basic type of the constraint: input/output/clobber
  102. ///
  103. ConstraintPrefix Type = isInput;
  104. /// isEarlyClobber - "&": output operand writes result before inputs are all
  105. /// read. This is only ever set for an output operand.
  106. bool isEarlyClobber = false;
  107. /// MatchingInput - If this is not -1, this is an output constraint where an
  108. /// input constraint is required to match it (e.g. "0"). The value is the
  109. /// constraint number that matches this one (for example, if this is
  110. /// constraint #0 and constraint #4 has the value "0", this will be 4).
  111. int MatchingInput = -1;
  112. /// hasMatchingInput - Return true if this is an output constraint that has
  113. /// a matching input constraint.
  114. bool hasMatchingInput() const { return MatchingInput != -1; }
  115. /// isCommutative - This is set to true for a constraint that is commutative
  116. /// with the next operand.
  117. bool isCommutative = false;
  118. /// isIndirect - True if this operand is an indirect operand. This means
  119. /// that the address of the source or destination is present in the call
  120. /// instruction, instead of it being returned or passed in explicitly. This
  121. /// is represented with a '*' in the asm string.
  122. bool isIndirect = false;
  123. /// Code - The constraint code, either the register name (in braces) or the
  124. /// constraint letter/number.
  125. ConstraintCodeVector Codes;
  126. /// isMultipleAlternative - '|': has multiple-alternative constraints.
  127. bool isMultipleAlternative = false;
  128. /// multipleAlternatives - If there are multiple alternative constraints,
  129. /// this array will contain them. Otherwise it will be empty.
  130. SubConstraintInfoVector multipleAlternatives;
  131. /// The currently selected alternative constraint index.
  132. unsigned currentAlternativeIndex = 0;
  133. /// Default constructor.
  134. ConstraintInfo() = default;
  135. /// Parse - Analyze the specified string (e.g. "=*&{eax}") and fill in the
  136. /// fields in this structure. If the constraint string is not understood,
  137. /// return true, otherwise return false.
  138. bool Parse(StringRef Str, ConstraintInfoVector &ConstraintsSoFar);
  139. /// selectAlternative - Point this constraint to the alternative constraint
  140. /// indicated by the index.
  141. void selectAlternative(unsigned index);
  142. };
  143. /// ParseConstraints - Split up the constraint string into the specific
  144. /// constraints and their prefixes. If this returns an empty vector, and if
  145. /// the constraint string itself isn't empty, there was an error parsing.
  146. static ConstraintInfoVector ParseConstraints(StringRef ConstraintString);
  147. /// ParseConstraints - Parse the constraints of this inlineasm object,
  148. /// returning them the same way that ParseConstraints(str) does.
  149. ConstraintInfoVector ParseConstraints() const {
  150. return ParseConstraints(Constraints);
  151. }
  152. // Methods for support type inquiry through isa, cast, and dyn_cast:
  153. static bool classof(const Value *V) {
  154. return V->getValueID() == Value::InlineAsmVal;
  155. }
  156. // These are helper methods for dealing with flags in the INLINEASM SDNode
  157. // in the backend.
  158. //
  159. // The encoding of the flag word is currently:
  160. // Bits 2-0 - A Kind_* value indicating the kind of the operand.
  161. // Bits 15-3 - The number of SDNode operands associated with this inline
  162. // assembly operand.
  163. // If bit 31 is set:
  164. // Bit 30-16 - The operand number that this operand must match.
  165. // When bits 2-0 are Kind_Mem, the Constraint_* value must be
  166. // obtained from the flags for this operand number.
  167. // Else if bits 2-0 are Kind_Mem:
  168. // Bit 30-16 - A Constraint_* value indicating the original constraint
  169. // code.
  170. // Else:
  171. // Bit 30-16 - The register class ID to use for the operand.
  172. enum : uint32_t {
  173. // Fixed operands on an INLINEASM SDNode.
  174. Op_InputChain = 0,
  175. Op_AsmString = 1,
  176. Op_MDNode = 2,
  177. Op_ExtraInfo = 3, // HasSideEffects, IsAlignStack, AsmDialect.
  178. Op_FirstOperand = 4,
  179. // Fixed operands on an INLINEASM MachineInstr.
  180. MIOp_AsmString = 0,
  181. MIOp_ExtraInfo = 1, // HasSideEffects, IsAlignStack, AsmDialect.
  182. MIOp_FirstOperand = 2,
  183. // Interpretation of the MIOp_ExtraInfo bit field.
  184. Extra_HasSideEffects = 1,
  185. Extra_IsAlignStack = 2,
  186. Extra_AsmDialect = 4,
  187. Extra_MayLoad = 8,
  188. Extra_MayStore = 16,
  189. Extra_IsConvergent = 32,
  190. // Inline asm operands map to multiple SDNode / MachineInstr operands.
  191. // The first operand is an immediate describing the asm operand, the low
  192. // bits is the kind:
  193. Kind_RegUse = 1, // Input register, "r".
  194. Kind_RegDef = 2, // Output register, "=r".
  195. Kind_RegDefEarlyClobber = 3, // Early-clobber output register, "=&r".
  196. Kind_Clobber = 4, // Clobbered register, "~r".
  197. Kind_Imm = 5, // Immediate.
  198. Kind_Mem = 6, // Memory operand, "m".
  199. // Memory constraint codes.
  200. // These could be tablegenerated but there's little need to do that since
  201. // there's plenty of space in the encoding to support the union of all
  202. // constraint codes for all targets.
  203. Constraint_Unknown = 0,
  204. Constraint_es,
  205. Constraint_i,
  206. Constraint_m,
  207. Constraint_o,
  208. Constraint_v,
  209. Constraint_A,
  210. Constraint_Q,
  211. Constraint_R,
  212. Constraint_S,
  213. Constraint_T,
  214. Constraint_Um,
  215. Constraint_Un,
  216. Constraint_Uq,
  217. Constraint_Us,
  218. Constraint_Ut,
  219. Constraint_Uv,
  220. Constraint_Uy,
  221. Constraint_X,
  222. Constraint_Z,
  223. Constraint_ZC,
  224. Constraint_Zy,
  225. Constraints_Max = Constraint_Zy,
  226. Constraints_ShiftAmount = 16,
  227. Flag_MatchingOperand = 0x80000000
  228. };
  229. static unsigned getFlagWord(unsigned Kind, unsigned NumOps) {
  230. assert(((NumOps << 3) & ~0xffff) == 0 && "Too many inline asm operands!");
  231. assert(Kind >= Kind_RegUse && Kind <= Kind_Mem && "Invalid Kind");
  232. return Kind | (NumOps << 3);
  233. }
  234. static bool isRegDefKind(unsigned Flag){ return getKind(Flag) == Kind_RegDef;}
  235. static bool isImmKind(unsigned Flag) { return getKind(Flag) == Kind_Imm; }
  236. static bool isMemKind(unsigned Flag) { return getKind(Flag) == Kind_Mem; }
  237. static bool isRegDefEarlyClobberKind(unsigned Flag) {
  238. return getKind(Flag) == Kind_RegDefEarlyClobber;
  239. }
  240. static bool isClobberKind(unsigned Flag) {
  241. return getKind(Flag) == Kind_Clobber;
  242. }
  243. /// getFlagWordForMatchingOp - Augment an existing flag word returned by
  244. /// getFlagWord with information indicating that this input operand is tied
  245. /// to a previous output operand.
  246. static unsigned getFlagWordForMatchingOp(unsigned InputFlag,
  247. unsigned MatchedOperandNo) {
  248. assert(MatchedOperandNo <= 0x7fff && "Too big matched operand");
  249. assert((InputFlag & ~0xffff) == 0 && "High bits already contain data");
  250. return InputFlag | Flag_MatchingOperand | (MatchedOperandNo << 16);
  251. }
  252. /// getFlagWordForRegClass - Augment an existing flag word returned by
  253. /// getFlagWord with the required register class for the following register
  254. /// operands.
  255. /// A tied use operand cannot have a register class, use the register class
  256. /// from the def operand instead.
  257. static unsigned getFlagWordForRegClass(unsigned InputFlag, unsigned RC) {
  258. // Store RC + 1, reserve the value 0 to mean 'no register class'.
  259. ++RC;
  260. assert(!isImmKind(InputFlag) && "Immediates cannot have a register class");
  261. assert(!isMemKind(InputFlag) && "Memory operand cannot have a register class");
  262. assert(RC <= 0x7fff && "Too large register class ID");
  263. assert((InputFlag & ~0xffff) == 0 && "High bits already contain data");
  264. return InputFlag | (RC << 16);
  265. }
  266. /// Augment an existing flag word returned by getFlagWord with the constraint
  267. /// code for a memory constraint.
  268. static unsigned getFlagWordForMem(unsigned InputFlag, unsigned Constraint) {
  269. assert(isMemKind(InputFlag) && "InputFlag is not a memory constraint!");
  270. assert(Constraint <= 0x7fff && "Too large a memory constraint ID");
  271. assert(Constraint <= Constraints_Max && "Unknown constraint ID");
  272. assert((InputFlag & ~0xffff) == 0 && "High bits already contain data");
  273. return InputFlag | (Constraint << Constraints_ShiftAmount);
  274. }
  275. static unsigned convertMemFlagWordToMatchingFlagWord(unsigned InputFlag) {
  276. assert(isMemKind(InputFlag));
  277. return InputFlag & ~(0x7fff << Constraints_ShiftAmount);
  278. }
  279. static unsigned getKind(unsigned Flags) {
  280. return Flags & 7;
  281. }
  282. static unsigned getMemoryConstraintID(unsigned Flag) {
  283. assert(isMemKind(Flag));
  284. return (Flag >> Constraints_ShiftAmount) & 0x7fff;
  285. }
  286. /// getNumOperandRegisters - Extract the number of registers field from the
  287. /// inline asm operand flag.
  288. static unsigned getNumOperandRegisters(unsigned Flag) {
  289. return (Flag & 0xffff) >> 3;
  290. }
  291. /// isUseOperandTiedToDef - Return true if the flag of the inline asm
  292. /// operand indicates it is an use operand that's matched to a def operand.
  293. static bool isUseOperandTiedToDef(unsigned Flag, unsigned &Idx) {
  294. if ((Flag & Flag_MatchingOperand) == 0)
  295. return false;
  296. Idx = (Flag & ~Flag_MatchingOperand) >> 16;
  297. return true;
  298. }
  299. /// hasRegClassConstraint - Returns true if the flag contains a register
  300. /// class constraint. Sets RC to the register class ID.
  301. static bool hasRegClassConstraint(unsigned Flag, unsigned &RC) {
  302. if (Flag & Flag_MatchingOperand)
  303. return false;
  304. unsigned High = Flag >> 16;
  305. // getFlagWordForRegClass() uses 0 to mean no register class, and otherwise
  306. // stores RC + 1.
  307. if (!High)
  308. return false;
  309. RC = High - 1;
  310. return true;
  311. }
  312. static std::vector<StringRef> getExtraInfoNames(unsigned ExtraInfo) {
  313. std::vector<StringRef> Result;
  314. if (ExtraInfo & InlineAsm::Extra_HasSideEffects)
  315. Result.push_back("sideeffect");
  316. if (ExtraInfo & InlineAsm::Extra_MayLoad)
  317. Result.push_back("mayload");
  318. if (ExtraInfo & InlineAsm::Extra_MayStore)
  319. Result.push_back("maystore");
  320. if (ExtraInfo & InlineAsm::Extra_IsConvergent)
  321. Result.push_back("isconvergent");
  322. if (ExtraInfo & InlineAsm::Extra_IsAlignStack)
  323. Result.push_back("alignstack");
  324. AsmDialect Dialect =
  325. InlineAsm::AsmDialect((ExtraInfo & InlineAsm::Extra_AsmDialect));
  326. if (Dialect == InlineAsm::AD_ATT)
  327. Result.push_back("attdialect");
  328. if (Dialect == InlineAsm::AD_Intel)
  329. Result.push_back("inteldialect");
  330. return Result;
  331. }
  332. static StringRef getKindName(unsigned Kind) {
  333. switch (Kind) {
  334. case InlineAsm::Kind_RegUse:
  335. return "reguse";
  336. case InlineAsm::Kind_RegDef:
  337. return "regdef";
  338. case InlineAsm::Kind_RegDefEarlyClobber:
  339. return "regdef-ec";
  340. case InlineAsm::Kind_Clobber:
  341. return "clobber";
  342. case InlineAsm::Kind_Imm:
  343. return "imm";
  344. case InlineAsm::Kind_Mem:
  345. return "mem";
  346. default:
  347. llvm_unreachable("Unknown operand kind");
  348. }
  349. }
  350. static StringRef getMemConstraintName(unsigned Constraint) {
  351. switch (Constraint) {
  352. case InlineAsm::Constraint_es:
  353. return "es";
  354. case InlineAsm::Constraint_i:
  355. return "i";
  356. case InlineAsm::Constraint_m:
  357. return "m";
  358. case InlineAsm::Constraint_o:
  359. return "o";
  360. case InlineAsm::Constraint_v:
  361. return "v";
  362. case InlineAsm::Constraint_Q:
  363. return "Q";
  364. case InlineAsm::Constraint_R:
  365. return "R";
  366. case InlineAsm::Constraint_S:
  367. return "S";
  368. case InlineAsm::Constraint_T:
  369. return "T";
  370. case InlineAsm::Constraint_Um:
  371. return "Um";
  372. case InlineAsm::Constraint_Un:
  373. return "Un";
  374. case InlineAsm::Constraint_Uq:
  375. return "Uq";
  376. case InlineAsm::Constraint_Us:
  377. return "Us";
  378. case InlineAsm::Constraint_Ut:
  379. return "Ut";
  380. case InlineAsm::Constraint_Uv:
  381. return "Uv";
  382. case InlineAsm::Constraint_Uy:
  383. return "Uy";
  384. case InlineAsm::Constraint_X:
  385. return "X";
  386. case InlineAsm::Constraint_Z:
  387. return "Z";
  388. case InlineAsm::Constraint_ZC:
  389. return "ZC";
  390. case InlineAsm::Constraint_Zy:
  391. return "Zy";
  392. default:
  393. llvm_unreachable("Unknown memory constraint");
  394. }
  395. }
  396. };
  397. } // end namespace llvm
  398. #endif // LLVM_IR_INLINEASM_H
  399. #ifdef __GNUC__
  400. #pragma GCC diagnostic pop
  401. #endif