InlineAsm.h 17 KB

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