CodeGenInstruction.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. //===- CodeGenInstruction.cpp - CodeGen Instruction 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 file implements the CodeGenInstruction class.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "CodeGenInstruction.h"
  14. #include "CodeGenTarget.h"
  15. #include "llvm/ADT/STLExtras.h"
  16. #include "llvm/ADT/StringExtras.h"
  17. #include "llvm/ADT/StringMap.h"
  18. #include "llvm/TableGen/Error.h"
  19. #include "llvm/TableGen/Record.h"
  20. #include <set>
  21. using namespace llvm;
  22. //===----------------------------------------------------------------------===//
  23. // CGIOperandList Implementation
  24. //===----------------------------------------------------------------------===//
  25. CGIOperandList::CGIOperandList(Record *R) : TheDef(R) {
  26. isPredicable = false;
  27. hasOptionalDef = false;
  28. isVariadic = false;
  29. DagInit *OutDI = R->getValueAsDag("OutOperandList");
  30. if (DefInit *Init = dyn_cast<DefInit>(OutDI->getOperator())) {
  31. if (Init->getDef()->getName() != "outs")
  32. PrintFatalError(R->getName() + ": invalid def name for output list: use 'outs'");
  33. } else
  34. PrintFatalError(R->getName() + ": invalid output list: use 'outs'");
  35. NumDefs = OutDI->getNumArgs();
  36. DagInit *InDI = R->getValueAsDag("InOperandList");
  37. if (DefInit *Init = dyn_cast<DefInit>(InDI->getOperator())) {
  38. if (Init->getDef()->getName() != "ins")
  39. PrintFatalError(R->getName() + ": invalid def name for input list: use 'ins'");
  40. } else
  41. PrintFatalError(R->getName() + ": invalid input list: use 'ins'");
  42. unsigned MIOperandNo = 0;
  43. std::set<std::string> OperandNames;
  44. for (unsigned i = 0, e = InDI->getNumArgs()+OutDI->getNumArgs(); i != e; ++i){
  45. Init *ArgInit;
  46. std::string ArgName;
  47. if (i < NumDefs) {
  48. ArgInit = OutDI->getArg(i);
  49. ArgName = OutDI->getArgName(i);
  50. } else {
  51. ArgInit = InDI->getArg(i-NumDefs);
  52. ArgName = InDI->getArgName(i-NumDefs);
  53. }
  54. DefInit *Arg = dyn_cast<DefInit>(ArgInit);
  55. if (!Arg)
  56. PrintFatalError("Illegal operand for the '" + R->getName() + "' instruction!");
  57. Record *Rec = Arg->getDef();
  58. std::string PrintMethod = "printOperand";
  59. std::string EncoderMethod;
  60. std::string OperandType = "OPERAND_UNKNOWN";
  61. std::string OperandNamespace = "MCOI";
  62. unsigned NumOps = 1;
  63. DagInit *MIOpInfo = nullptr;
  64. if (Rec->isSubClassOf("RegisterOperand")) {
  65. PrintMethod = Rec->getValueAsString("PrintMethod");
  66. OperandType = Rec->getValueAsString("OperandType");
  67. OperandNamespace = Rec->getValueAsString("OperandNamespace");
  68. } else if (Rec->isSubClassOf("Operand")) {
  69. PrintMethod = Rec->getValueAsString("PrintMethod");
  70. OperandType = Rec->getValueAsString("OperandType");
  71. // If there is an explicit encoder method, use it.
  72. EncoderMethod = Rec->getValueAsString("EncoderMethod");
  73. MIOpInfo = Rec->getValueAsDag("MIOperandInfo");
  74. // Verify that MIOpInfo has an 'ops' root value.
  75. if (!isa<DefInit>(MIOpInfo->getOperator()) ||
  76. cast<DefInit>(MIOpInfo->getOperator())->getDef()->getName() != "ops")
  77. PrintFatalError("Bad value for MIOperandInfo in operand '" + Rec->getName() +
  78. "'\n");
  79. // If we have MIOpInfo, then we have #operands equal to number of entries
  80. // in MIOperandInfo.
  81. if (unsigned NumArgs = MIOpInfo->getNumArgs())
  82. NumOps = NumArgs;
  83. if (Rec->isSubClassOf("PredicateOp"))
  84. isPredicable = true;
  85. else if (Rec->isSubClassOf("OptionalDefOperand"))
  86. hasOptionalDef = true;
  87. } else if (Rec->getName() == "variable_ops") {
  88. isVariadic = true;
  89. continue;
  90. } else if (Rec->isSubClassOf("RegisterClass")) {
  91. OperandType = "OPERAND_REGISTER";
  92. } else if (!Rec->isSubClassOf("PointerLikeRegClass") &&
  93. !Rec->isSubClassOf("unknown_class"))
  94. PrintFatalError("Unknown operand class '" + Rec->getName() +
  95. "' in '" + R->getName() + "' instruction!");
  96. // Check that the operand has a name and that it's unique.
  97. if (ArgName.empty())
  98. PrintFatalError("In instruction '" + R->getName() + "', operand #" +
  99. Twine(i) + " has no name!");
  100. if (!OperandNames.insert(ArgName).second)
  101. PrintFatalError("In instruction '" + R->getName() + "', operand #" +
  102. Twine(i) + " has the same name as a previous operand!");
  103. OperandList.emplace_back(Rec, ArgName, PrintMethod, EncoderMethod,
  104. OperandNamespace + "::" + OperandType, MIOperandNo,
  105. NumOps, MIOpInfo);
  106. MIOperandNo += NumOps;
  107. }
  108. // Make sure the constraints list for each operand is large enough to hold
  109. // constraint info, even if none is present.
  110. for (unsigned i = 0, e = OperandList.size(); i != e; ++i)
  111. OperandList[i].Constraints.resize(OperandList[i].MINumOperands);
  112. }
  113. /// getOperandNamed - Return the index of the operand with the specified
  114. /// non-empty name. If the instruction does not have an operand with the
  115. /// specified name, abort.
  116. ///
  117. unsigned CGIOperandList::getOperandNamed(StringRef Name) const {
  118. unsigned OpIdx;
  119. if (hasOperandNamed(Name, OpIdx)) return OpIdx;
  120. PrintFatalError("'" + TheDef->getName() +
  121. "' does not have an operand named '$" + Name + "'!");
  122. }
  123. /// hasOperandNamed - Query whether the instruction has an operand of the
  124. /// given name. If so, return true and set OpIdx to the index of the
  125. /// operand. Otherwise, return false.
  126. bool CGIOperandList::hasOperandNamed(StringRef Name, unsigned &OpIdx) const {
  127. assert(!Name.empty() && "Cannot search for operand with no name!");
  128. for (unsigned i = 0, e = OperandList.size(); i != e; ++i)
  129. if (OperandList[i].Name == Name) {
  130. OpIdx = i;
  131. return true;
  132. }
  133. return false;
  134. }
  135. std::pair<unsigned,unsigned>
  136. CGIOperandList::ParseOperandName(const std::string &Op, bool AllowWholeOp) {
  137. if (Op.empty() || Op[0] != '$')
  138. PrintFatalError(TheDef->getName() + ": Illegal operand name: '" + Op + "'");
  139. std::string OpName = Op.substr(1);
  140. std::string SubOpName;
  141. // Check to see if this is $foo.bar.
  142. std::string::size_type DotIdx = OpName.find_first_of(".");
  143. if (DotIdx != std::string::npos) {
  144. SubOpName = OpName.substr(DotIdx+1);
  145. if (SubOpName.empty())
  146. PrintFatalError(TheDef->getName() + ": illegal empty suboperand name in '" +Op +"'");
  147. OpName = OpName.substr(0, DotIdx);
  148. }
  149. unsigned OpIdx = getOperandNamed(OpName);
  150. if (SubOpName.empty()) { // If no suboperand name was specified:
  151. // If one was needed, throw.
  152. if (OperandList[OpIdx].MINumOperands > 1 && !AllowWholeOp &&
  153. SubOpName.empty())
  154. PrintFatalError(TheDef->getName() + ": Illegal to refer to"
  155. " whole operand part of complex operand '" + Op + "'");
  156. // Otherwise, return the operand.
  157. return std::make_pair(OpIdx, 0U);
  158. }
  159. // Find the suboperand number involved.
  160. DagInit *MIOpInfo = OperandList[OpIdx].MIOperandInfo;
  161. if (!MIOpInfo)
  162. PrintFatalError(TheDef->getName() + ": unknown suboperand name in '" + Op + "'");
  163. // Find the operand with the right name.
  164. for (unsigned i = 0, e = MIOpInfo->getNumArgs(); i != e; ++i)
  165. if (MIOpInfo->getArgName(i) == SubOpName)
  166. return std::make_pair(OpIdx, i);
  167. // Otherwise, didn't find it!
  168. PrintFatalError(TheDef->getName() + ": unknown suboperand name in '" + Op + "'");
  169. return std::make_pair(0U, 0U);
  170. }
  171. static void ParseConstraint(const std::string &CStr, CGIOperandList &Ops) {
  172. // EARLY_CLOBBER: @early $reg
  173. std::string::size_type wpos = CStr.find_first_of(" \t");
  174. std::string::size_type start = CStr.find_first_not_of(" \t");
  175. std::string Tok = CStr.substr(start, wpos - start);
  176. if (Tok == "@earlyclobber") {
  177. std::string Name = CStr.substr(wpos+1);
  178. wpos = Name.find_first_not_of(" \t");
  179. if (wpos == std::string::npos)
  180. PrintFatalError("Illegal format for @earlyclobber constraint: '" + CStr + "'");
  181. Name = Name.substr(wpos);
  182. std::pair<unsigned,unsigned> Op = Ops.ParseOperandName(Name, false);
  183. // Build the string for the operand
  184. if (!Ops[Op.first].Constraints[Op.second].isNone())
  185. PrintFatalError("Operand '" + Name + "' cannot have multiple constraints!");
  186. Ops[Op.first].Constraints[Op.second] =
  187. CGIOperandList::ConstraintInfo::getEarlyClobber();
  188. return;
  189. }
  190. // Only other constraint is "TIED_TO" for now.
  191. std::string::size_type pos = CStr.find_first_of('=');
  192. assert(pos != std::string::npos && "Unrecognized constraint");
  193. start = CStr.find_first_not_of(" \t");
  194. std::string Name = CStr.substr(start, pos - start);
  195. // TIED_TO: $src1 = $dst
  196. wpos = Name.find_first_of(" \t");
  197. if (wpos == std::string::npos)
  198. PrintFatalError("Illegal format for tied-to constraint: '" + CStr + "'");
  199. std::string DestOpName = Name.substr(0, wpos);
  200. std::pair<unsigned,unsigned> DestOp = Ops.ParseOperandName(DestOpName, false);
  201. Name = CStr.substr(pos+1);
  202. wpos = Name.find_first_not_of(" \t");
  203. if (wpos == std::string::npos)
  204. PrintFatalError("Illegal format for tied-to constraint: '" + CStr + "'");
  205. std::string SrcOpName = Name.substr(wpos);
  206. std::pair<unsigned,unsigned> SrcOp = Ops.ParseOperandName(SrcOpName, false);
  207. if (SrcOp > DestOp) {
  208. std::swap(SrcOp, DestOp);
  209. std::swap(SrcOpName, DestOpName);
  210. }
  211. unsigned FlatOpNo = Ops.getFlattenedOperandNumber(SrcOp);
  212. if (!Ops[DestOp.first].Constraints[DestOp.second].isNone())
  213. PrintFatalError("Operand '" + DestOpName +
  214. "' cannot have multiple constraints!");
  215. Ops[DestOp.first].Constraints[DestOp.second] =
  216. CGIOperandList::ConstraintInfo::getTied(FlatOpNo);
  217. }
  218. static void ParseConstraints(const std::string &CStr, CGIOperandList &Ops) {
  219. if (CStr.empty()) return;
  220. const std::string delims(",");
  221. std::string::size_type bidx, eidx;
  222. bidx = CStr.find_first_not_of(delims);
  223. while (bidx != std::string::npos) {
  224. eidx = CStr.find_first_of(delims, bidx);
  225. if (eidx == std::string::npos)
  226. eidx = CStr.length();
  227. ParseConstraint(CStr.substr(bidx, eidx - bidx), Ops);
  228. bidx = CStr.find_first_not_of(delims, eidx);
  229. }
  230. }
  231. void CGIOperandList::ProcessDisableEncoding(std::string DisableEncoding) {
  232. while (1) {
  233. std::pair<StringRef, StringRef> P = getToken(DisableEncoding, " ,\t");
  234. std::string OpName = P.first;
  235. DisableEncoding = P.second;
  236. if (OpName.empty()) break;
  237. // Figure out which operand this is.
  238. std::pair<unsigned,unsigned> Op = ParseOperandName(OpName, false);
  239. // Mark the operand as not-to-be encoded.
  240. if (Op.second >= OperandList[Op.first].DoNotEncode.size())
  241. OperandList[Op.first].DoNotEncode.resize(Op.second+1);
  242. OperandList[Op.first].DoNotEncode[Op.second] = true;
  243. }
  244. }
  245. //===----------------------------------------------------------------------===//
  246. // CodeGenInstruction Implementation
  247. //===----------------------------------------------------------------------===//
  248. CodeGenInstruction::CodeGenInstruction(Record *R)
  249. : TheDef(R), Operands(R), InferredFrom(nullptr) {
  250. Namespace = R->getValueAsString("Namespace");
  251. AsmString = R->getValueAsString("AsmString");
  252. isReturn = R->getValueAsBit("isReturn");
  253. isBranch = R->getValueAsBit("isBranch");
  254. isIndirectBranch = R->getValueAsBit("isIndirectBranch");
  255. isCompare = R->getValueAsBit("isCompare");
  256. isMoveImm = R->getValueAsBit("isMoveImm");
  257. isBitcast = R->getValueAsBit("isBitcast");
  258. isSelect = R->getValueAsBit("isSelect");
  259. isBarrier = R->getValueAsBit("isBarrier");
  260. isCall = R->getValueAsBit("isCall");
  261. canFoldAsLoad = R->getValueAsBit("canFoldAsLoad");
  262. isPredicable = Operands.isPredicable || R->getValueAsBit("isPredicable");
  263. isConvertibleToThreeAddress = R->getValueAsBit("isConvertibleToThreeAddress");
  264. isCommutable = R->getValueAsBit("isCommutable");
  265. isTerminator = R->getValueAsBit("isTerminator");
  266. isReMaterializable = R->getValueAsBit("isReMaterializable");
  267. hasDelaySlot = R->getValueAsBit("hasDelaySlot");
  268. usesCustomInserter = R->getValueAsBit("usesCustomInserter");
  269. hasPostISelHook = R->getValueAsBit("hasPostISelHook");
  270. hasCtrlDep = R->getValueAsBit("hasCtrlDep");
  271. isNotDuplicable = R->getValueAsBit("isNotDuplicable");
  272. isRegSequence = R->getValueAsBit("isRegSequence");
  273. isExtractSubreg = R->getValueAsBit("isExtractSubreg");
  274. isInsertSubreg = R->getValueAsBit("isInsertSubreg");
  275. isConvergent = R->getValueAsBit("isConvergent");
  276. bool Unset;
  277. mayLoad = R->getValueAsBitOrUnset("mayLoad", Unset);
  278. mayLoad_Unset = Unset;
  279. mayStore = R->getValueAsBitOrUnset("mayStore", Unset);
  280. mayStore_Unset = Unset;
  281. hasSideEffects = R->getValueAsBitOrUnset("hasSideEffects", Unset);
  282. hasSideEffects_Unset = Unset;
  283. isAsCheapAsAMove = R->getValueAsBit("isAsCheapAsAMove");
  284. hasExtraSrcRegAllocReq = R->getValueAsBit("hasExtraSrcRegAllocReq");
  285. hasExtraDefRegAllocReq = R->getValueAsBit("hasExtraDefRegAllocReq");
  286. isCodeGenOnly = R->getValueAsBit("isCodeGenOnly");
  287. isPseudo = R->getValueAsBit("isPseudo");
  288. ImplicitDefs = R->getValueAsListOfDefs("Defs");
  289. ImplicitUses = R->getValueAsListOfDefs("Uses");
  290. // Parse Constraints.
  291. ParseConstraints(R->getValueAsString("Constraints"), Operands);
  292. // Parse the DisableEncoding field.
  293. Operands.ProcessDisableEncoding(R->getValueAsString("DisableEncoding"));
  294. // First check for a ComplexDeprecationPredicate.
  295. if (R->getValue("ComplexDeprecationPredicate")) {
  296. HasComplexDeprecationPredicate = true;
  297. DeprecatedReason = R->getValueAsString("ComplexDeprecationPredicate");
  298. } else if (RecordVal *Dep = R->getValue("DeprecatedFeatureMask")) {
  299. // Check if we have a Subtarget feature mask.
  300. HasComplexDeprecationPredicate = false;
  301. DeprecatedReason = Dep->getValue()->getAsString();
  302. } else {
  303. // This instruction isn't deprecated.
  304. HasComplexDeprecationPredicate = false;
  305. DeprecatedReason = "";
  306. }
  307. }
  308. /// HasOneImplicitDefWithKnownVT - If the instruction has at least one
  309. /// implicit def and it has a known VT, return the VT, otherwise return
  310. /// MVT::Other.
  311. MVT::SimpleValueType CodeGenInstruction::
  312. HasOneImplicitDefWithKnownVT(const CodeGenTarget &TargetInfo) const {
  313. if (ImplicitDefs.empty()) return MVT::Other;
  314. // Check to see if the first implicit def has a resolvable type.
  315. Record *FirstImplicitDef = ImplicitDefs[0];
  316. assert(FirstImplicitDef->isSubClassOf("Register"));
  317. const std::vector<MVT::SimpleValueType> &RegVTs =
  318. TargetInfo.getRegisterVTs(FirstImplicitDef);
  319. if (RegVTs.size() == 1)
  320. return RegVTs[0];
  321. return MVT::Other;
  322. }
  323. /// FlattenAsmStringVariants - Flatten the specified AsmString to only
  324. /// include text from the specified variant, returning the new string.
  325. std::string CodeGenInstruction::
  326. FlattenAsmStringVariants(StringRef Cur, unsigned Variant) {
  327. std::string Res = "";
  328. for (;;) {
  329. // Find the start of the next variant string.
  330. size_t VariantsStart = 0;
  331. for (size_t e = Cur.size(); VariantsStart != e; ++VariantsStart)
  332. if (Cur[VariantsStart] == '{' &&
  333. (VariantsStart == 0 || (Cur[VariantsStart-1] != '$' &&
  334. Cur[VariantsStart-1] != '\\')))
  335. break;
  336. // Add the prefix to the result.
  337. Res += Cur.slice(0, VariantsStart);
  338. if (VariantsStart == Cur.size())
  339. break;
  340. ++VariantsStart; // Skip the '{'.
  341. // Scan to the end of the variants string.
  342. size_t VariantsEnd = VariantsStart;
  343. unsigned NestedBraces = 1;
  344. for (size_t e = Cur.size(); VariantsEnd != e; ++VariantsEnd) {
  345. if (Cur[VariantsEnd] == '}' && Cur[VariantsEnd-1] != '\\') {
  346. if (--NestedBraces == 0)
  347. break;
  348. } else if (Cur[VariantsEnd] == '{')
  349. ++NestedBraces;
  350. }
  351. // Select the Nth variant (or empty).
  352. StringRef Selection = Cur.slice(VariantsStart, VariantsEnd);
  353. for (unsigned i = 0; i != Variant; ++i)
  354. Selection = Selection.split('|').second;
  355. Res += Selection.split('|').first;
  356. assert(VariantsEnd != Cur.size() &&
  357. "Unterminated variants in assembly string!");
  358. Cur = Cur.substr(VariantsEnd + 1);
  359. }
  360. return Res;
  361. }
  362. //===----------------------------------------------------------------------===//
  363. /// CodeGenInstAlias Implementation
  364. //===----------------------------------------------------------------------===//
  365. /// tryAliasOpMatch - This is a helper function for the CodeGenInstAlias
  366. /// constructor. It checks if an argument in an InstAlias pattern matches
  367. /// the corresponding operand of the instruction. It returns true on a
  368. /// successful match, with ResOp set to the result operand to be used.
  369. bool CodeGenInstAlias::tryAliasOpMatch(DagInit *Result, unsigned AliasOpNo,
  370. Record *InstOpRec, bool hasSubOps,
  371. ArrayRef<SMLoc> Loc, CodeGenTarget &T,
  372. ResultOperand &ResOp) {
  373. Init *Arg = Result->getArg(AliasOpNo);
  374. DefInit *ADI = dyn_cast<DefInit>(Arg);
  375. Record *ResultRecord = ADI ? ADI->getDef() : nullptr;
  376. if (ADI && ADI->getDef() == InstOpRec) {
  377. // If the operand is a record, it must have a name, and the record type
  378. // must match up with the instruction's argument type.
  379. if (Result->getArgName(AliasOpNo).empty())
  380. PrintFatalError(Loc, "result argument #" + Twine(AliasOpNo) +
  381. " must have a name!");
  382. ResOp = ResultOperand(Result->getArgName(AliasOpNo), ResultRecord);
  383. return true;
  384. }
  385. // For register operands, the source register class can be a subclass
  386. // of the instruction register class, not just an exact match.
  387. if (InstOpRec->isSubClassOf("RegisterOperand"))
  388. InstOpRec = InstOpRec->getValueAsDef("RegClass");
  389. if (ADI && ADI->getDef()->isSubClassOf("RegisterOperand"))
  390. ADI = ADI->getDef()->getValueAsDef("RegClass")->getDefInit();
  391. if (ADI && ADI->getDef()->isSubClassOf("RegisterClass")) {
  392. if (!InstOpRec->isSubClassOf("RegisterClass"))
  393. return false;
  394. if (!T.getRegisterClass(InstOpRec)
  395. .hasSubClass(&T.getRegisterClass(ADI->getDef())))
  396. return false;
  397. ResOp = ResultOperand(Result->getArgName(AliasOpNo), ResultRecord);
  398. return true;
  399. }
  400. // Handle explicit registers.
  401. if (ADI && ADI->getDef()->isSubClassOf("Register")) {
  402. if (InstOpRec->isSubClassOf("OptionalDefOperand")) {
  403. DagInit *DI = InstOpRec->getValueAsDag("MIOperandInfo");
  404. // The operand info should only have a single (register) entry. We
  405. // want the register class of it.
  406. InstOpRec = cast<DefInit>(DI->getArg(0))->getDef();
  407. }
  408. if (!InstOpRec->isSubClassOf("RegisterClass"))
  409. return false;
  410. if (!T.getRegisterClass(InstOpRec)
  411. .contains(T.getRegBank().getReg(ADI->getDef())))
  412. PrintFatalError(Loc, "fixed register " + ADI->getDef()->getName() +
  413. " is not a member of the " + InstOpRec->getName() +
  414. " register class!");
  415. if (!Result->getArgName(AliasOpNo).empty())
  416. PrintFatalError(Loc, "result fixed register argument must "
  417. "not have a name!");
  418. ResOp = ResultOperand(ResultRecord);
  419. return true;
  420. }
  421. // Handle "zero_reg" for optional def operands.
  422. if (ADI && ADI->getDef()->getName() == "zero_reg") {
  423. // Check if this is an optional def.
  424. // Tied operands where the source is a sub-operand of a complex operand
  425. // need to represent both operands in the alias destination instruction.
  426. // Allow zero_reg for the tied portion. This can and should go away once
  427. // the MC representation of things doesn't use tied operands at all.
  428. //if (!InstOpRec->isSubClassOf("OptionalDefOperand"))
  429. // throw TGError(Loc, "reg0 used for result that is not an "
  430. // "OptionalDefOperand!");
  431. ResOp = ResultOperand(static_cast<Record*>(nullptr));
  432. return true;
  433. }
  434. // Literal integers.
  435. if (IntInit *II = dyn_cast<IntInit>(Arg)) {
  436. if (hasSubOps || !InstOpRec->isSubClassOf("Operand"))
  437. return false;
  438. // Integer arguments can't have names.
  439. if (!Result->getArgName(AliasOpNo).empty())
  440. PrintFatalError(Loc, "result argument #" + Twine(AliasOpNo) +
  441. " must not have a name!");
  442. ResOp = ResultOperand(II->getValue());
  443. return true;
  444. }
  445. // Bits<n> (also used for 0bxx literals)
  446. if (BitsInit *BI = dyn_cast<BitsInit>(Arg)) {
  447. if (hasSubOps || !InstOpRec->isSubClassOf("Operand"))
  448. return false;
  449. if (!BI->isComplete())
  450. return false;
  451. // Convert the bits init to an integer and use that for the result.
  452. IntInit *II =
  453. dyn_cast_or_null<IntInit>(BI->convertInitializerTo(IntRecTy::get()));
  454. if (!II)
  455. return false;
  456. ResOp = ResultOperand(II->getValue());
  457. return true;
  458. }
  459. // If both are Operands with the same MVT, allow the conversion. It's
  460. // up to the user to make sure the values are appropriate, just like
  461. // for isel Pat's.
  462. if (InstOpRec->isSubClassOf("Operand") && ADI &&
  463. ADI->getDef()->isSubClassOf("Operand")) {
  464. // FIXME: What other attributes should we check here? Identical
  465. // MIOperandInfo perhaps?
  466. if (InstOpRec->getValueInit("Type") != ADI->getDef()->getValueInit("Type"))
  467. return false;
  468. ResOp = ResultOperand(Result->getArgName(AliasOpNo), ADI->getDef());
  469. return true;
  470. }
  471. return false;
  472. }
  473. unsigned CodeGenInstAlias::ResultOperand::getMINumOperands() const {
  474. if (!isRecord())
  475. return 1;
  476. Record *Rec = getRecord();
  477. if (!Rec->isSubClassOf("Operand"))
  478. return 1;
  479. DagInit *MIOpInfo = Rec->getValueAsDag("MIOperandInfo");
  480. if (MIOpInfo->getNumArgs() == 0) {
  481. // Unspecified, so it defaults to 1
  482. return 1;
  483. }
  484. return MIOpInfo->getNumArgs();
  485. }
  486. CodeGenInstAlias::CodeGenInstAlias(Record *R, unsigned Variant,
  487. CodeGenTarget &T)
  488. : TheDef(R) {
  489. Result = R->getValueAsDag("ResultInst");
  490. AsmString = R->getValueAsString("AsmString");
  491. AsmString = CodeGenInstruction::FlattenAsmStringVariants(AsmString, Variant);
  492. // Verify that the root of the result is an instruction.
  493. DefInit *DI = dyn_cast<DefInit>(Result->getOperator());
  494. if (!DI || !DI->getDef()->isSubClassOf("Instruction"))
  495. PrintFatalError(R->getLoc(),
  496. "result of inst alias should be an instruction");
  497. ResultInst = &T.getInstruction(DI->getDef());
  498. // NameClass - If argument names are repeated, we need to verify they have
  499. // the same class.
  500. StringMap<Record*> NameClass;
  501. for (unsigned i = 0, e = Result->getNumArgs(); i != e; ++i) {
  502. DefInit *ADI = dyn_cast<DefInit>(Result->getArg(i));
  503. if (!ADI || Result->getArgName(i).empty())
  504. continue;
  505. // Verify we don't have something like: (someinst GR16:$foo, GR32:$foo)
  506. // $foo can exist multiple times in the result list, but it must have the
  507. // same type.
  508. Record *&Entry = NameClass[Result->getArgName(i)];
  509. if (Entry && Entry != ADI->getDef())
  510. PrintFatalError(R->getLoc(), "result value $" + Result->getArgName(i) +
  511. " is both " + Entry->getName() + " and " +
  512. ADI->getDef()->getName() + "!");
  513. Entry = ADI->getDef();
  514. }
  515. // Decode and validate the arguments of the result.
  516. unsigned AliasOpNo = 0;
  517. for (unsigned i = 0, e = ResultInst->Operands.size(); i != e; ++i) {
  518. // Tied registers don't have an entry in the result dag unless they're part
  519. // of a complex operand, in which case we include them anyways, as we
  520. // don't have any other way to specify the whole operand.
  521. if (ResultInst->Operands[i].MINumOperands == 1 &&
  522. ResultInst->Operands[i].getTiedRegister() != -1)
  523. continue;
  524. if (AliasOpNo >= Result->getNumArgs())
  525. PrintFatalError(R->getLoc(), "not enough arguments for instruction!");
  526. Record *InstOpRec = ResultInst->Operands[i].Rec;
  527. unsigned NumSubOps = ResultInst->Operands[i].MINumOperands;
  528. ResultOperand ResOp(static_cast<int64_t>(0));
  529. if (tryAliasOpMatch(Result, AliasOpNo, InstOpRec, (NumSubOps > 1),
  530. R->getLoc(), T, ResOp)) {
  531. // If this is a simple operand, or a complex operand with a custom match
  532. // class, then we can match is verbatim.
  533. if (NumSubOps == 1 ||
  534. (InstOpRec->getValue("ParserMatchClass") &&
  535. InstOpRec->getValueAsDef("ParserMatchClass")
  536. ->getValueAsString("Name") != "Imm")) {
  537. ResultOperands.push_back(ResOp);
  538. ResultInstOperandIndex.push_back(std::make_pair(i, -1));
  539. ++AliasOpNo;
  540. // Otherwise, we need to match each of the suboperands individually.
  541. } else {
  542. DagInit *MIOI = ResultInst->Operands[i].MIOperandInfo;
  543. for (unsigned SubOp = 0; SubOp != NumSubOps; ++SubOp) {
  544. Record *SubRec = cast<DefInit>(MIOI->getArg(SubOp))->getDef();
  545. // Take care to instantiate each of the suboperands with the correct
  546. // nomenclature: $foo.bar
  547. ResultOperands.emplace_back(Result->getArgName(AliasOpNo) + "." +
  548. MIOI->getArgName(SubOp),
  549. SubRec);
  550. ResultInstOperandIndex.push_back(std::make_pair(i, SubOp));
  551. }
  552. ++AliasOpNo;
  553. }
  554. continue;
  555. }
  556. // If the argument did not match the instruction operand, and the operand
  557. // is composed of multiple suboperands, try matching the suboperands.
  558. if (NumSubOps > 1) {
  559. DagInit *MIOI = ResultInst->Operands[i].MIOperandInfo;
  560. for (unsigned SubOp = 0; SubOp != NumSubOps; ++SubOp) {
  561. if (AliasOpNo >= Result->getNumArgs())
  562. PrintFatalError(R->getLoc(), "not enough arguments for instruction!");
  563. Record *SubRec = cast<DefInit>(MIOI->getArg(SubOp))->getDef();
  564. if (tryAliasOpMatch(Result, AliasOpNo, SubRec, false,
  565. R->getLoc(), T, ResOp)) {
  566. ResultOperands.push_back(ResOp);
  567. ResultInstOperandIndex.push_back(std::make_pair(i, SubOp));
  568. ++AliasOpNo;
  569. } else {
  570. PrintFatalError(R->getLoc(), "result argument #" + Twine(AliasOpNo) +
  571. " does not match instruction operand class " +
  572. (SubOp == 0 ? InstOpRec->getName() :SubRec->getName()));
  573. }
  574. }
  575. continue;
  576. }
  577. PrintFatalError(R->getLoc(), "result argument #" + Twine(AliasOpNo) +
  578. " does not match instruction operand class " +
  579. InstOpRec->getName());
  580. }
  581. if (AliasOpNo != Result->getNumArgs())
  582. PrintFatalError(R->getLoc(), "too many operands for instruction!");
  583. }