InlineAsm.h 16 KB

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