InlineAsm.h 13 KB

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