2
0

CodeGenTarget.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. //===- CodeGenTarget.cpp - CodeGen Target Class Wrapper -------------------===//
  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 wraps target description classes used by the various code
  11. // generation TableGen backends. This makes it easier to access the data and
  12. // provides a single place that needs to check it for validity. All of these
  13. // classes abort on error conditions.
  14. //
  15. //===----------------------------------------------------------------------===//
  16. #include "CodeGenTarget.h"
  17. #include "CodeGenIntrinsics.h"
  18. #include "CodeGenSchedule.h"
  19. #include "llvm/ADT/STLExtras.h"
  20. #include "llvm/ADT/StringExtras.h"
  21. #include "llvm/Support/CommandLine.h"
  22. #include "llvm/TableGen/Error.h"
  23. #include "llvm/TableGen/Record.h"
  24. #include <algorithm>
  25. using namespace llvm;
  26. static cl::opt<unsigned>
  27. AsmParserNum("asmparsernum", cl::init(0),
  28. cl::desc("Make -gen-asm-parser emit assembly parser #N"));
  29. static cl::opt<unsigned>
  30. AsmWriterNum("asmwriternum", cl::init(0),
  31. cl::desc("Make -gen-asm-writer emit assembly writer #N"));
  32. /// getValueType - Return the MVT::SimpleValueType that the specified TableGen
  33. /// record corresponds to.
  34. MVT::SimpleValueType llvm::getValueType(Record *Rec) {
  35. return (MVT::SimpleValueType)Rec->getValueAsInt("Value");
  36. }
  37. std::string llvm::getName(MVT::SimpleValueType T) {
  38. switch (T) {
  39. case MVT::Other: return "UNKNOWN";
  40. case MVT::iPTR: return "TLI.getPointerTy()";
  41. case MVT::iPTRAny: return "TLI.getPointerTy()";
  42. default: return getEnumName(T);
  43. }
  44. }
  45. std::string llvm::getEnumName(MVT::SimpleValueType T) {
  46. switch (T) {
  47. case MVT::Other: return "MVT::Other";
  48. case MVT::i1: return "MVT::i1";
  49. case MVT::i8: return "MVT::i8";
  50. case MVT::i16: return "MVT::i16";
  51. case MVT::i32: return "MVT::i32";
  52. case MVT::i64: return "MVT::i64";
  53. case MVT::i128: return "MVT::i128";
  54. case MVT::Any: return "MVT::Any";
  55. case MVT::iAny: return "MVT::iAny";
  56. case MVT::fAny: return "MVT::fAny";
  57. case MVT::vAny: return "MVT::vAny";
  58. case MVT::f16: return "MVT::f16";
  59. case MVT::f32: return "MVT::f32";
  60. case MVT::f64: return "MVT::f64";
  61. case MVT::f80: return "MVT::f80";
  62. case MVT::f128: return "MVT::f128";
  63. case MVT::ppcf128: return "MVT::ppcf128";
  64. case MVT::x86mmx: return "MVT::x86mmx";
  65. case MVT::Glue: return "MVT::Glue";
  66. case MVT::isVoid: return "MVT::isVoid";
  67. case MVT::v2i1: return "MVT::v2i1";
  68. case MVT::v4i1: return "MVT::v4i1";
  69. case MVT::v8i1: return "MVT::v8i1";
  70. case MVT::v16i1: return "MVT::v16i1";
  71. case MVT::v32i1: return "MVT::v32i1";
  72. case MVT::v64i1: return "MVT::v64i1";
  73. case MVT::v1i8: return "MVT::v1i8";
  74. case MVT::v2i8: return "MVT::v2i8";
  75. case MVT::v4i8: return "MVT::v4i8";
  76. case MVT::v8i8: return "MVT::v8i8";
  77. case MVT::v16i8: return "MVT::v16i8";
  78. case MVT::v32i8: return "MVT::v32i8";
  79. case MVT::v64i8: return "MVT::v64i8";
  80. case MVT::v1i16: return "MVT::v1i16";
  81. case MVT::v2i16: return "MVT::v2i16";
  82. case MVT::v4i16: return "MVT::v4i16";
  83. case MVT::v8i16: return "MVT::v8i16";
  84. case MVT::v16i16: return "MVT::v16i16";
  85. case MVT::v32i16: return "MVT::v32i16";
  86. case MVT::v1i32: return "MVT::v1i32";
  87. case MVT::v2i32: return "MVT::v2i32";
  88. case MVT::v4i32: return "MVT::v4i32";
  89. case MVT::v8i32: return "MVT::v8i32";
  90. case MVT::v16i32: return "MVT::v16i32";
  91. case MVT::v1i64: return "MVT::v1i64";
  92. case MVT::v2i64: return "MVT::v2i64";
  93. case MVT::v4i64: return "MVT::v4i64";
  94. case MVT::v8i64: return "MVT::v8i64";
  95. case MVT::v16i64: return "MVT::v16i64";
  96. case MVT::v1i128: return "MVT::v1i128";
  97. case MVT::v2f16: return "MVT::v2f16";
  98. case MVT::v4f16: return "MVT::v4f16";
  99. case MVT::v8f16: return "MVT::v8f16";
  100. case MVT::v1f32: return "MVT::v1f32";
  101. case MVT::v2f32: return "MVT::v2f32";
  102. case MVT::v4f32: return "MVT::v4f32";
  103. case MVT::v8f32: return "MVT::v8f32";
  104. case MVT::v16f32: return "MVT::v16f32";
  105. case MVT::v1f64: return "MVT::v1f64";
  106. case MVT::v2f64: return "MVT::v2f64";
  107. case MVT::v4f64: return "MVT::v4f64";
  108. case MVT::v8f64: return "MVT::v8f64";
  109. case MVT::Metadata: return "MVT::Metadata";
  110. case MVT::iPTR: return "MVT::iPTR";
  111. case MVT::iPTRAny: return "MVT::iPTRAny";
  112. case MVT::Untyped: return "MVT::Untyped";
  113. default: llvm_unreachable("ILLEGAL VALUE TYPE!");
  114. }
  115. }
  116. /// getQualifiedName - Return the name of the specified record, with a
  117. /// namespace qualifier if the record contains one.
  118. ///
  119. std::string llvm::getQualifiedName(const Record *R) {
  120. std::string Namespace;
  121. if (R->getValue("Namespace"))
  122. Namespace = R->getValueAsString("Namespace");
  123. if (Namespace.empty()) return R->getName();
  124. return Namespace + "::" + R->getName();
  125. }
  126. /// getTarget - Return the current instance of the Target class.
  127. ///
  128. CodeGenTarget::CodeGenTarget(RecordKeeper &records)
  129. : Records(records) {
  130. std::vector<Record*> Targets = Records.getAllDerivedDefinitions("Target");
  131. if (Targets.size() == 0)
  132. PrintFatalError("ERROR: No 'Target' subclasses defined!");
  133. if (Targets.size() != 1)
  134. PrintFatalError("ERROR: Multiple subclasses of Target defined!");
  135. TargetRec = Targets[0];
  136. }
  137. CodeGenTarget::~CodeGenTarget() {
  138. }
  139. const std::string &CodeGenTarget::getName() const {
  140. return TargetRec->getName();
  141. }
  142. std::string CodeGenTarget::getInstNamespace() const {
  143. for (const CodeGenInstruction *Inst : instructions()) {
  144. // Make sure not to pick up "TargetOpcode" by accidentally getting
  145. // the namespace off the PHI instruction or something.
  146. if (Inst->Namespace != "TargetOpcode")
  147. return Inst->Namespace;
  148. }
  149. return "";
  150. }
  151. Record *CodeGenTarget::getInstructionSet() const {
  152. return TargetRec->getValueAsDef("InstructionSet");
  153. }
  154. /// getAsmParser - Return the AssemblyParser definition for this target.
  155. ///
  156. Record *CodeGenTarget::getAsmParser() const {
  157. std::vector<Record*> LI = TargetRec->getValueAsListOfDefs("AssemblyParsers");
  158. if (AsmParserNum >= LI.size())
  159. PrintFatalError("Target does not have an AsmParser #" +
  160. Twine(AsmParserNum) + "!");
  161. return LI[AsmParserNum];
  162. }
  163. /// getAsmParserVariant - Return the AssmblyParserVariant definition for
  164. /// this target.
  165. ///
  166. Record *CodeGenTarget::getAsmParserVariant(unsigned i) const {
  167. std::vector<Record*> LI =
  168. TargetRec->getValueAsListOfDefs("AssemblyParserVariants");
  169. if (i >= LI.size())
  170. PrintFatalError("Target does not have an AsmParserVariant #" + Twine(i) +
  171. "!");
  172. return LI[i];
  173. }
  174. /// getAsmParserVariantCount - Return the AssmblyParserVariant definition
  175. /// available for this target.
  176. ///
  177. unsigned CodeGenTarget::getAsmParserVariantCount() const {
  178. std::vector<Record*> LI =
  179. TargetRec->getValueAsListOfDefs("AssemblyParserVariants");
  180. return LI.size();
  181. }
  182. /// getAsmWriter - Return the AssemblyWriter definition for this target.
  183. ///
  184. Record *CodeGenTarget::getAsmWriter() const {
  185. std::vector<Record*> LI = TargetRec->getValueAsListOfDefs("AssemblyWriters");
  186. if (AsmWriterNum >= LI.size())
  187. PrintFatalError("Target does not have an AsmWriter #" +
  188. Twine(AsmWriterNum) + "!");
  189. return LI[AsmWriterNum];
  190. }
  191. CodeGenRegBank &CodeGenTarget::getRegBank() const {
  192. if (!RegBank)
  193. RegBank = llvm::make_unique<CodeGenRegBank>(Records);
  194. return *RegBank;
  195. }
  196. void CodeGenTarget::ReadRegAltNameIndices() const {
  197. RegAltNameIndices = Records.getAllDerivedDefinitions("RegAltNameIndex");
  198. std::sort(RegAltNameIndices.begin(), RegAltNameIndices.end(), LessRecord());
  199. }
  200. /// getRegisterByName - If there is a register with the specific AsmName,
  201. /// return it.
  202. const CodeGenRegister *CodeGenTarget::getRegisterByName(StringRef Name) const {
  203. const StringMap<CodeGenRegister*> &Regs = getRegBank().getRegistersByName();
  204. StringMap<CodeGenRegister*>::const_iterator I = Regs.find(Name);
  205. if (I == Regs.end())
  206. return nullptr;
  207. return I->second;
  208. }
  209. std::vector<MVT::SimpleValueType> CodeGenTarget::
  210. getRegisterVTs(Record *R) const {
  211. const CodeGenRegister *Reg = getRegBank().getReg(R);
  212. std::vector<MVT::SimpleValueType> Result;
  213. for (const auto &RC : getRegBank().getRegClasses()) {
  214. if (RC.contains(Reg)) {
  215. ArrayRef<MVT::SimpleValueType> InVTs = RC.getValueTypes();
  216. Result.insert(Result.end(), InVTs.begin(), InVTs.end());
  217. }
  218. }
  219. // Remove duplicates.
  220. array_pod_sort(Result.begin(), Result.end());
  221. Result.erase(std::unique(Result.begin(), Result.end()), Result.end());
  222. return Result;
  223. }
  224. void CodeGenTarget::ReadLegalValueTypes() const {
  225. for (const auto &RC : getRegBank().getRegClasses())
  226. LegalValueTypes.insert(LegalValueTypes.end(), RC.VTs.begin(), RC.VTs.end());
  227. // Remove duplicates.
  228. std::sort(LegalValueTypes.begin(), LegalValueTypes.end());
  229. LegalValueTypes.erase(std::unique(LegalValueTypes.begin(),
  230. LegalValueTypes.end()),
  231. LegalValueTypes.end());
  232. }
  233. CodeGenSchedModels &CodeGenTarget::getSchedModels() const {
  234. if (!SchedModels)
  235. SchedModels = llvm::make_unique<CodeGenSchedModels>(Records, *this);
  236. return *SchedModels;
  237. }
  238. void CodeGenTarget::ReadInstructions() const {
  239. std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
  240. if (Insts.size() <= 2)
  241. PrintFatalError("No 'Instruction' subclasses defined!");
  242. // Parse the instructions defined in the .td file.
  243. for (unsigned i = 0, e = Insts.size(); i != e; ++i)
  244. Instructions[Insts[i]] = llvm::make_unique<CodeGenInstruction>(Insts[i]);
  245. }
  246. static const CodeGenInstruction *
  247. GetInstByName(const char *Name,
  248. const DenseMap<const Record*,
  249. std::unique_ptr<CodeGenInstruction>> &Insts,
  250. RecordKeeper &Records) {
  251. const Record *Rec = Records.getDef(Name);
  252. const auto I = Insts.find(Rec);
  253. if (!Rec || I == Insts.end())
  254. PrintFatalError(Twine("Could not find '") + Name + "' instruction!");
  255. return I->second.get();
  256. }
  257. /// \brief Return all of the instructions defined by the target, ordered by
  258. /// their enum value.
  259. void CodeGenTarget::ComputeInstrsByEnum() const {
  260. // The ordering here must match the ordering in TargetOpcodes.h.
  261. static const char *const FixedInstrs[] = {
  262. "PHI", "INLINEASM", "CFI_INSTRUCTION", "EH_LABEL",
  263. "GC_LABEL", "KILL", "EXTRACT_SUBREG", "INSERT_SUBREG",
  264. "IMPLICIT_DEF", "SUBREG_TO_REG", "COPY_TO_REGCLASS", "DBG_VALUE",
  265. "REG_SEQUENCE", "COPY", "BUNDLE", "LIFETIME_START",
  266. "LIFETIME_END", "STACKMAP", "PATCHPOINT", "LOAD_STACK_GUARD",
  267. "STATEPOINT", "LOCAL_ESCAPE", "FAULTING_LOAD_OP",
  268. nullptr};
  269. const auto &Insts = getInstructions();
  270. for (const char *const *p = FixedInstrs; *p; ++p) {
  271. const CodeGenInstruction *Instr = GetInstByName(*p, Insts, Records);
  272. assert(Instr && "Missing target independent instruction");
  273. assert(Instr->Namespace == "TargetOpcode" && "Bad namespace");
  274. InstrsByEnum.push_back(Instr);
  275. }
  276. unsigned EndOfPredefines = InstrsByEnum.size();
  277. for (const auto &I : Insts) {
  278. const CodeGenInstruction *CGI = I.second.get();
  279. if (CGI->Namespace != "TargetOpcode")
  280. InstrsByEnum.push_back(CGI);
  281. }
  282. assert(InstrsByEnum.size() == Insts.size() && "Missing predefined instr");
  283. // All of the instructions are now in random order based on the map iteration.
  284. // Sort them by name.
  285. std::sort(InstrsByEnum.begin() + EndOfPredefines, InstrsByEnum.end(),
  286. [](const CodeGenInstruction *Rec1, const CodeGenInstruction *Rec2) {
  287. return Rec1->TheDef->getName() < Rec2->TheDef->getName();
  288. });
  289. }
  290. /// isLittleEndianEncoding - Return whether this target encodes its instruction
  291. /// in little-endian format, i.e. bits laid out in the order [0..n]
  292. ///
  293. bool CodeGenTarget::isLittleEndianEncoding() const {
  294. return getInstructionSet()->getValueAsBit("isLittleEndianEncoding");
  295. }
  296. /// reverseBitsForLittleEndianEncoding - For little-endian instruction bit
  297. /// encodings, reverse the bit order of all instructions.
  298. void CodeGenTarget::reverseBitsForLittleEndianEncoding() {
  299. if (!isLittleEndianEncoding())
  300. return;
  301. std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
  302. for (Record *R : Insts) {
  303. if (R->getValueAsString("Namespace") == "TargetOpcode" ||
  304. R->getValueAsBit("isPseudo"))
  305. continue;
  306. BitsInit *BI = R->getValueAsBitsInit("Inst");
  307. unsigned numBits = BI->getNumBits();
  308. SmallVector<Init *, 16> NewBits(numBits);
  309. for (unsigned bit = 0, end = numBits / 2; bit != end; ++bit) {
  310. unsigned bitSwapIdx = numBits - bit - 1;
  311. Init *OrigBit = BI->getBit(bit);
  312. Init *BitSwap = BI->getBit(bitSwapIdx);
  313. NewBits[bit] = BitSwap;
  314. NewBits[bitSwapIdx] = OrigBit;
  315. }
  316. if (numBits % 2) {
  317. unsigned middle = (numBits + 1) / 2;
  318. NewBits[middle] = BI->getBit(middle);
  319. }
  320. BitsInit *NewBI = BitsInit::get(NewBits);
  321. // Update the bits in reversed order so that emitInstrOpBits will get the
  322. // correct endianness.
  323. R->getValue("Inst")->setValue(NewBI);
  324. }
  325. }
  326. /// guessInstructionProperties - Return true if it's OK to guess instruction
  327. /// properties instead of raising an error.
  328. ///
  329. /// This is configurable as a temporary migration aid. It will eventually be
  330. /// permanently false.
  331. bool CodeGenTarget::guessInstructionProperties() const {
  332. return getInstructionSet()->getValueAsBit("guessInstructionProperties");
  333. }
  334. //===----------------------------------------------------------------------===//
  335. // ComplexPattern implementation
  336. //
  337. ComplexPattern::ComplexPattern(Record *R) {
  338. Ty = ::getValueType(R->getValueAsDef("Ty"));
  339. NumOperands = R->getValueAsInt("NumOperands");
  340. SelectFunc = R->getValueAsString("SelectFunc");
  341. RootNodes = R->getValueAsListOfDefs("RootNodes");
  342. // Parse the properties.
  343. Properties = 0;
  344. std::vector<Record*> PropList = R->getValueAsListOfDefs("Properties");
  345. for (unsigned i = 0, e = PropList.size(); i != e; ++i)
  346. if (PropList[i]->getName() == "SDNPHasChain") {
  347. Properties |= 1 << SDNPHasChain;
  348. } else if (PropList[i]->getName() == "SDNPOptInGlue") {
  349. Properties |= 1 << SDNPOptInGlue;
  350. } else if (PropList[i]->getName() == "SDNPMayStore") {
  351. Properties |= 1 << SDNPMayStore;
  352. } else if (PropList[i]->getName() == "SDNPMayLoad") {
  353. Properties |= 1 << SDNPMayLoad;
  354. } else if (PropList[i]->getName() == "SDNPSideEffect") {
  355. Properties |= 1 << SDNPSideEffect;
  356. } else if (PropList[i]->getName() == "SDNPMemOperand") {
  357. Properties |= 1 << SDNPMemOperand;
  358. } else if (PropList[i]->getName() == "SDNPVariadic") {
  359. Properties |= 1 << SDNPVariadic;
  360. } else if (PropList[i]->getName() == "SDNPWantRoot") {
  361. Properties |= 1 << SDNPWantRoot;
  362. } else if (PropList[i]->getName() == "SDNPWantParent") {
  363. Properties |= 1 << SDNPWantParent;
  364. } else {
  365. PrintFatalError("Unsupported SD Node property '" +
  366. PropList[i]->getName() + "' on ComplexPattern '" +
  367. R->getName() + "'!");
  368. }
  369. }
  370. //===----------------------------------------------------------------------===//
  371. // CodeGenIntrinsic Implementation
  372. //===----------------------------------------------------------------------===//
  373. std::vector<CodeGenIntrinsic> llvm::LoadIntrinsics(const RecordKeeper &RC,
  374. bool TargetOnly) {
  375. std::vector<Record*> I = RC.getAllDerivedDefinitions("Intrinsic");
  376. std::vector<CodeGenIntrinsic> Result;
  377. for (unsigned i = 0, e = I.size(); i != e; ++i) {
  378. bool isTarget = I[i]->getValueAsBit("isTarget");
  379. if (isTarget == TargetOnly)
  380. Result.push_back(CodeGenIntrinsic(I[i]));
  381. }
  382. return Result;
  383. }
  384. CodeGenIntrinsic::CodeGenIntrinsic(Record *R) {
  385. TheDef = R;
  386. std::string DefName = R->getName();
  387. ModRef = ReadWriteMem;
  388. isOverloaded = false;
  389. isCommutative = false;
  390. canThrow = false;
  391. isNoReturn = false;
  392. isNoDuplicate = false;
  393. isConvergent = false;
  394. if (DefName.size() <= 4 ||
  395. std::string(DefName.begin(), DefName.begin() + 4) != "int_")
  396. PrintFatalError("Intrinsic '" + DefName + "' does not start with 'int_'!");
  397. EnumName = std::string(DefName.begin()+4, DefName.end());
  398. if (R->getValue("GCCBuiltinName")) // Ignore a missing GCCBuiltinName field.
  399. GCCBuiltinName = R->getValueAsString("GCCBuiltinName");
  400. if (R->getValue("MSBuiltinName")) // Ignore a missing MSBuiltinName field.
  401. MSBuiltinName = R->getValueAsString("MSBuiltinName");
  402. TargetPrefix = R->getValueAsString("TargetPrefix");
  403. Name = R->getValueAsString("LLVMName");
  404. if (Name == "") {
  405. // If an explicit name isn't specified, derive one from the DefName.
  406. Name = "llvm.";
  407. for (unsigned i = 0, e = EnumName.size(); i != e; ++i)
  408. Name += (EnumName[i] == '_') ? '.' : EnumName[i];
  409. } else {
  410. // Verify it starts with "llvm.".
  411. if (Name.size() <= 5 ||
  412. std::string(Name.begin(), Name.begin() + 5) != "llvm.")
  413. PrintFatalError("Intrinsic '" + DefName + "'s name does not start with 'llvm.'!");
  414. }
  415. // If TargetPrefix is specified, make sure that Name starts with
  416. // "llvm.<targetprefix>.".
  417. if (!TargetPrefix.empty()) {
  418. if (Name.size() < 6+TargetPrefix.size() ||
  419. std::string(Name.begin() + 5, Name.begin() + 6 + TargetPrefix.size())
  420. != (TargetPrefix + "."))
  421. PrintFatalError("Intrinsic '" + DefName + "' does not start with 'llvm." +
  422. TargetPrefix + ".'!");
  423. }
  424. // Parse the list of return types.
  425. std::vector<MVT::SimpleValueType> OverloadedVTs;
  426. ListInit *TypeList = R->getValueAsListInit("RetTypes");
  427. for (unsigned i = 0, e = TypeList->size(); i != e; ++i) {
  428. Record *TyEl = TypeList->getElementAsRecord(i);
  429. assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!");
  430. MVT::SimpleValueType VT;
  431. if (TyEl->isSubClassOf("LLVMMatchType")) {
  432. unsigned MatchTy = TyEl->getValueAsInt("Number");
  433. assert(MatchTy < OverloadedVTs.size() &&
  434. "Invalid matching number!");
  435. VT = OverloadedVTs[MatchTy];
  436. // It only makes sense to use the extended and truncated vector element
  437. // variants with iAny types; otherwise, if the intrinsic is not
  438. // overloaded, all the types can be specified directly.
  439. assert(((!TyEl->isSubClassOf("LLVMExtendedType") &&
  440. !TyEl->isSubClassOf("LLVMTruncatedType")) ||
  441. VT == MVT::iAny || VT == MVT::vAny) &&
  442. "Expected iAny or vAny type");
  443. } else {
  444. VT = getValueType(TyEl->getValueAsDef("VT"));
  445. }
  446. if (MVT(VT).isOverloaded()) {
  447. OverloadedVTs.push_back(VT);
  448. isOverloaded = true;
  449. }
  450. // Reject invalid types.
  451. if (VT == MVT::isVoid)
  452. PrintFatalError("Intrinsic '" + DefName + " has void in result type list!");
  453. IS.RetVTs.push_back(VT);
  454. IS.RetTypeDefs.push_back(TyEl);
  455. }
  456. // Parse the list of parameter types.
  457. TypeList = R->getValueAsListInit("ParamTypes");
  458. for (unsigned i = 0, e = TypeList->size(); i != e; ++i) {
  459. Record *TyEl = TypeList->getElementAsRecord(i);
  460. assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!");
  461. MVT::SimpleValueType VT;
  462. if (TyEl->isSubClassOf("LLVMMatchType")) {
  463. unsigned MatchTy = TyEl->getValueAsInt("Number");
  464. assert(MatchTy < OverloadedVTs.size() &&
  465. "Invalid matching number!");
  466. VT = OverloadedVTs[MatchTy];
  467. // It only makes sense to use the extended and truncated vector element
  468. // variants with iAny types; otherwise, if the intrinsic is not
  469. // overloaded, all the types can be specified directly.
  470. assert(((!TyEl->isSubClassOf("LLVMExtendedType") &&
  471. !TyEl->isSubClassOf("LLVMTruncatedType") &&
  472. !TyEl->isSubClassOf("LLVMVectorSameWidth") &&
  473. !TyEl->isSubClassOf("LLVMPointerToElt")) ||
  474. VT == MVT::iAny || VT == MVT::vAny) &&
  475. "Expected iAny or vAny type");
  476. } else
  477. VT = getValueType(TyEl->getValueAsDef("VT"));
  478. if (MVT(VT).isOverloaded()) {
  479. OverloadedVTs.push_back(VT);
  480. isOverloaded = true;
  481. }
  482. // Reject invalid types.
  483. if (VT == MVT::isVoid && i != e-1 /*void at end means varargs*/)
  484. PrintFatalError("Intrinsic '" + DefName + " has void in result type list!");
  485. IS.ParamVTs.push_back(VT);
  486. IS.ParamTypeDefs.push_back(TyEl);
  487. }
  488. // Parse the intrinsic properties.
  489. ListInit *PropList = R->getValueAsListInit("Properties");
  490. for (unsigned i = 0, e = PropList->size(); i != e; ++i) {
  491. Record *Property = PropList->getElementAsRecord(i);
  492. assert(Property->isSubClassOf("IntrinsicProperty") &&
  493. "Expected a property!");
  494. if (Property->getName() == "IntrNoMem")
  495. ModRef = NoMem;
  496. else if (Property->getName() == "IntrReadArgMem")
  497. ModRef = ReadArgMem;
  498. else if (Property->getName() == "IntrReadMem")
  499. ModRef = ReadMem;
  500. else if (Property->getName() == "IntrReadWriteArgMem")
  501. ModRef = ReadWriteArgMem;
  502. else if (Property->getName() == "Commutative")
  503. isCommutative = true;
  504. else if (Property->getName() == "Throws")
  505. canThrow = true;
  506. else if (Property->getName() == "IntrNoDuplicate")
  507. isNoDuplicate = true;
  508. else if (Property->getName() == "IntrConvergent")
  509. isConvergent = true;
  510. else if (Property->getName() == "IntrNoReturn")
  511. isNoReturn = true;
  512. else if (Property->isSubClassOf("NoCapture")) {
  513. unsigned ArgNo = Property->getValueAsInt("ArgNo");
  514. ArgumentAttributes.push_back(std::make_pair(ArgNo, NoCapture));
  515. } else if (Property->isSubClassOf("ReadOnly")) {
  516. unsigned ArgNo = Property->getValueAsInt("ArgNo");
  517. ArgumentAttributes.push_back(std::make_pair(ArgNo, ReadOnly));
  518. } else if (Property->isSubClassOf("ReadNone")) {
  519. unsigned ArgNo = Property->getValueAsInt("ArgNo");
  520. ArgumentAttributes.push_back(std::make_pair(ArgNo, ReadNone));
  521. } else
  522. llvm_unreachable("Unknown property!");
  523. }
  524. // Sort the argument attributes for later benefit.
  525. std::sort(ArgumentAttributes.begin(), ArgumentAttributes.end());
  526. }