1 //===- CodeGenInstruction.cpp - CodeGen Instruction Class Wrapper ---------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the CodeGenInstruction class.
12 //===----------------------------------------------------------------------===//
14 #include "CodeGenInstruction.h"
15 #include "CodeGenTarget.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/ADT/StringExtras.h"
18 #include "llvm/ADT/StringMap.h"
19 #include "llvm/TableGen/Error.h"
20 #include "llvm/TableGen/Record.h"
24 //===----------------------------------------------------------------------===//
25 // CGIOperandList Implementation
26 //===----------------------------------------------------------------------===//
28 CGIOperandList::CGIOperandList(Record *R) : TheDef(R) {
30 hasOptionalDef = false;
33 DagInit *OutDI = R->getValueAsDag("OutOperandList");
35 if (DefInit *Init = dyn_cast<DefInit>(OutDI->getOperator())) {
36 if (Init->getDef()->getName() != "outs")
37 PrintFatalError(R->getName() + ": invalid def name for output list: use 'outs'");
39 PrintFatalError(R->getName() + ": invalid output list: use 'outs'");
41 NumDefs = OutDI->getNumArgs();
43 DagInit *InDI = R->getValueAsDag("InOperandList");
44 if (DefInit *Init = dyn_cast<DefInit>(InDI->getOperator())) {
45 if (Init->getDef()->getName() != "ins")
46 PrintFatalError(R->getName() + ": invalid def name for input list: use 'ins'");
48 PrintFatalError(R->getName() + ": invalid input list: use 'ins'");
50 unsigned MIOperandNo = 0;
51 std::set<std::string> OperandNames;
52 unsigned e = InDI->getNumArgs() + OutDI->getNumArgs();
53 OperandList.reserve(e);
54 for (unsigned i = 0; i != e; ++i){
58 ArgInit = OutDI->getArg(i);
59 ArgName = OutDI->getArgName(i);
61 ArgInit = InDI->getArg(i-NumDefs);
62 ArgName = InDI->getArgName(i-NumDefs);
65 DefInit *Arg = dyn_cast<DefInit>(ArgInit);
67 PrintFatalError("Illegal operand for the '" + R->getName() + "' instruction!");
69 Record *Rec = Arg->getDef();
70 std::string PrintMethod = "printOperand";
71 std::string EncoderMethod;
72 std::string OperandType = "OPERAND_UNKNOWN";
73 std::string OperandNamespace = "MCOI";
75 DagInit *MIOpInfo = nullptr;
76 if (Rec->isSubClassOf("RegisterOperand")) {
77 PrintMethod = Rec->getValueAsString("PrintMethod");
78 OperandType = Rec->getValueAsString("OperandType");
79 OperandNamespace = Rec->getValueAsString("OperandNamespace");
80 } else if (Rec->isSubClassOf("Operand")) {
81 PrintMethod = Rec->getValueAsString("PrintMethod");
82 OperandType = Rec->getValueAsString("OperandType");
83 OperandNamespace = Rec->getValueAsString("OperandNamespace");
84 // If there is an explicit encoder method, use it.
85 EncoderMethod = Rec->getValueAsString("EncoderMethod");
86 MIOpInfo = Rec->getValueAsDag("MIOperandInfo");
88 // Verify that MIOpInfo has an 'ops' root value.
89 if (!isa<DefInit>(MIOpInfo->getOperator()) ||
90 cast<DefInit>(MIOpInfo->getOperator())->getDef()->getName() != "ops")
91 PrintFatalError("Bad value for MIOperandInfo in operand '" + Rec->getName() +
94 // If we have MIOpInfo, then we have #operands equal to number of entries
96 if (unsigned NumArgs = MIOpInfo->getNumArgs())
99 if (Rec->isSubClassOf("PredicateOp"))
101 else if (Rec->isSubClassOf("OptionalDefOperand"))
102 hasOptionalDef = true;
103 } else if (Rec->getName() == "variable_ops") {
106 } else if (Rec->isSubClassOf("RegisterClass")) {
107 OperandType = "OPERAND_REGISTER";
108 } else if (!Rec->isSubClassOf("PointerLikeRegClass") &&
109 !Rec->isSubClassOf("unknown_class"))
110 PrintFatalError("Unknown operand class '" + Rec->getName() +
111 "' in '" + R->getName() + "' instruction!");
113 // Check that the operand has a name and that it's unique.
115 PrintFatalError("In instruction '" + R->getName() + "', operand #" +
116 Twine(i) + " has no name!");
117 if (!OperandNames.insert(ArgName).second)
118 PrintFatalError("In instruction '" + R->getName() + "', operand #" +
119 Twine(i) + " has the same name as a previous operand!");
121 OperandList.emplace_back(Rec, ArgName, PrintMethod, EncoderMethod,
122 OperandNamespace + "::" + OperandType, MIOperandNo,
124 MIOperandNo += NumOps;
128 // Make sure the constraints list for each operand is large enough to hold
129 // constraint info, even if none is present.
130 for (unsigned i = 0, e = OperandList.size(); i != e; ++i)
131 OperandList[i].Constraints.resize(OperandList[i].MINumOperands);
135 /// getOperandNamed - Return the index of the operand with the specified
136 /// non-empty name. If the instruction does not have an operand with the
137 /// specified name, abort.
139 unsigned CGIOperandList::getOperandNamed(StringRef Name) const {
141 if (hasOperandNamed(Name, OpIdx)) return OpIdx;
142 PrintFatalError("'" + TheDef->getName() +
143 "' does not have an operand named '$" + Name + "'!");
146 /// hasOperandNamed - Query whether the instruction has an operand of the
147 /// given name. If so, return true and set OpIdx to the index of the
148 /// operand. Otherwise, return false.
149 bool CGIOperandList::hasOperandNamed(StringRef Name, unsigned &OpIdx) const {
150 assert(!Name.empty() && "Cannot search for operand with no name!");
151 for (unsigned i = 0, e = OperandList.size(); i != e; ++i)
152 if (OperandList[i].Name == Name) {
159 std::pair<unsigned,unsigned>
160 CGIOperandList::ParseOperandName(const std::string &Op, bool AllowWholeOp) {
161 if (Op.empty() || Op[0] != '$')
162 PrintFatalError(TheDef->getName() + ": Illegal operand name: '" + Op + "'");
164 std::string OpName = Op.substr(1);
165 std::string SubOpName;
167 // Check to see if this is $foo.bar.
168 std::string::size_type DotIdx = OpName.find_first_of(".");
169 if (DotIdx != std::string::npos) {
170 SubOpName = OpName.substr(DotIdx+1);
171 if (SubOpName.empty())
172 PrintFatalError(TheDef->getName() + ": illegal empty suboperand name in '" +Op +"'");
173 OpName = OpName.substr(0, DotIdx);
176 unsigned OpIdx = getOperandNamed(OpName);
178 if (SubOpName.empty()) { // If no suboperand name was specified:
179 // If one was needed, throw.
180 if (OperandList[OpIdx].MINumOperands > 1 && !AllowWholeOp &&
182 PrintFatalError(TheDef->getName() + ": Illegal to refer to"
183 " whole operand part of complex operand '" + Op + "'");
185 // Otherwise, return the operand.
186 return std::make_pair(OpIdx, 0U);
189 // Find the suboperand number involved.
190 DagInit *MIOpInfo = OperandList[OpIdx].MIOperandInfo;
192 PrintFatalError(TheDef->getName() + ": unknown suboperand name in '" + Op + "'");
194 // Find the operand with the right name.
195 for (unsigned i = 0, e = MIOpInfo->getNumArgs(); i != e; ++i)
196 if (MIOpInfo->getArgName(i) == SubOpName)
197 return std::make_pair(OpIdx, i);
199 // Otherwise, didn't find it!
200 PrintFatalError(TheDef->getName() + ": unknown suboperand name in '" + Op + "'");
201 return std::make_pair(0U, 0U);
204 static void ParseConstraint(const std::string &CStr, CGIOperandList &Ops) {
205 // EARLY_CLOBBER: @early $reg
206 std::string::size_type wpos = CStr.find_first_of(" \t");
207 std::string::size_type start = CStr.find_first_not_of(" \t");
208 std::string Tok = CStr.substr(start, wpos - start);
209 if (Tok == "@earlyclobber") {
210 std::string Name = CStr.substr(wpos+1);
211 wpos = Name.find_first_not_of(" \t");
212 if (wpos == std::string::npos)
213 PrintFatalError("Illegal format for @earlyclobber constraint: '" + CStr + "'");
214 Name = Name.substr(wpos);
215 std::pair<unsigned,unsigned> Op = Ops.ParseOperandName(Name, false);
217 // Build the string for the operand
218 if (!Ops[Op.first].Constraints[Op.second].isNone())
219 PrintFatalError("Operand '" + Name + "' cannot have multiple constraints!");
220 Ops[Op.first].Constraints[Op.second] =
221 CGIOperandList::ConstraintInfo::getEarlyClobber();
225 // Only other constraint is "TIED_TO" for now.
226 std::string::size_type pos = CStr.find_first_of('=');
227 assert(pos != std::string::npos && "Unrecognized constraint");
228 start = CStr.find_first_not_of(" \t");
229 std::string Name = CStr.substr(start, pos - start);
231 // TIED_TO: $src1 = $dst
232 wpos = Name.find_first_of(" \t");
233 if (wpos == std::string::npos)
234 PrintFatalError("Illegal format for tied-to constraint: '" + CStr + "'");
235 std::string DestOpName = Name.substr(0, wpos);
236 std::pair<unsigned,unsigned> DestOp = Ops.ParseOperandName(DestOpName, false);
238 Name = CStr.substr(pos+1);
239 wpos = Name.find_first_not_of(" \t");
240 if (wpos == std::string::npos)
241 PrintFatalError("Illegal format for tied-to constraint: '" + CStr + "'");
243 std::string SrcOpName = Name.substr(wpos);
244 std::pair<unsigned,unsigned> SrcOp = Ops.ParseOperandName(SrcOpName, false);
245 if (SrcOp > DestOp) {
246 std::swap(SrcOp, DestOp);
247 std::swap(SrcOpName, DestOpName);
250 unsigned FlatOpNo = Ops.getFlattenedOperandNumber(SrcOp);
252 if (!Ops[DestOp.first].Constraints[DestOp.second].isNone())
253 PrintFatalError("Operand '" + DestOpName +
254 "' cannot have multiple constraints!");
255 Ops[DestOp.first].Constraints[DestOp.second] =
256 CGIOperandList::ConstraintInfo::getTied(FlatOpNo);
259 static void ParseConstraints(const std::string &CStr, CGIOperandList &Ops) {
260 if (CStr.empty()) return;
262 const std::string delims(",");
263 std::string::size_type bidx, eidx;
265 bidx = CStr.find_first_not_of(delims);
266 while (bidx != std::string::npos) {
267 eidx = CStr.find_first_of(delims, bidx);
268 if (eidx == std::string::npos)
269 eidx = CStr.length();
271 ParseConstraint(CStr.substr(bidx, eidx - bidx), Ops);
272 bidx = CStr.find_first_not_of(delims, eidx);
276 void CGIOperandList::ProcessDisableEncoding(std::string DisableEncoding) {
278 std::pair<StringRef, StringRef> P = getToken(DisableEncoding, " ,\t");
279 std::string OpName = P.first;
280 DisableEncoding = P.second;
281 if (OpName.empty()) break;
283 // Figure out which operand this is.
284 std::pair<unsigned,unsigned> Op = ParseOperandName(OpName, false);
286 // Mark the operand as not-to-be encoded.
287 if (Op.second >= OperandList[Op.first].DoNotEncode.size())
288 OperandList[Op.first].DoNotEncode.resize(Op.second+1);
289 OperandList[Op.first].DoNotEncode[Op.second] = true;
294 //===----------------------------------------------------------------------===//
295 // CodeGenInstruction Implementation
296 //===----------------------------------------------------------------------===//
298 CodeGenInstruction::CodeGenInstruction(Record *R)
299 : TheDef(R), Operands(R), InferredFrom(nullptr) {
300 Namespace = R->getValueAsString("Namespace");
301 AsmString = R->getValueAsString("AsmString");
303 isReturn = R->getValueAsBit("isReturn");
304 isBranch = R->getValueAsBit("isBranch");
305 isIndirectBranch = R->getValueAsBit("isIndirectBranch");
306 isCompare = R->getValueAsBit("isCompare");
307 isMoveImm = R->getValueAsBit("isMoveImm");
308 isBitcast = R->getValueAsBit("isBitcast");
309 isSelect = R->getValueAsBit("isSelect");
310 isBarrier = R->getValueAsBit("isBarrier");
311 isCall = R->getValueAsBit("isCall");
312 canFoldAsLoad = R->getValueAsBit("canFoldAsLoad");
313 isPredicable = Operands.isPredicable || R->getValueAsBit("isPredicable");
314 isConvertibleToThreeAddress = R->getValueAsBit("isConvertibleToThreeAddress");
315 isCommutable = R->getValueAsBit("isCommutable");
316 isTerminator = R->getValueAsBit("isTerminator");
317 isReMaterializable = R->getValueAsBit("isReMaterializable");
318 hasDelaySlot = R->getValueAsBit("hasDelaySlot");
319 usesCustomInserter = R->getValueAsBit("usesCustomInserter");
320 hasPostISelHook = R->getValueAsBit("hasPostISelHook");
321 hasCtrlDep = R->getValueAsBit("hasCtrlDep");
322 isNotDuplicable = R->getValueAsBit("isNotDuplicable");
323 isRegSequence = R->getValueAsBit("isRegSequence");
324 isExtractSubreg = R->getValueAsBit("isExtractSubreg");
325 isInsertSubreg = R->getValueAsBit("isInsertSubreg");
326 isConvergent = R->getValueAsBit("isConvergent");
327 hasNoSchedulingInfo = R->getValueAsBit("hasNoSchedulingInfo");
330 mayLoad = R->getValueAsBitOrUnset("mayLoad", Unset);
331 mayLoad_Unset = Unset;
332 mayStore = R->getValueAsBitOrUnset("mayStore", Unset);
333 mayStore_Unset = Unset;
334 hasSideEffects = R->getValueAsBitOrUnset("hasSideEffects", Unset);
335 hasSideEffects_Unset = Unset;
337 isAsCheapAsAMove = R->getValueAsBit("isAsCheapAsAMove");
338 hasExtraSrcRegAllocReq = R->getValueAsBit("hasExtraSrcRegAllocReq");
339 hasExtraDefRegAllocReq = R->getValueAsBit("hasExtraDefRegAllocReq");
340 isCodeGenOnly = R->getValueAsBit("isCodeGenOnly");
341 isPseudo = R->getValueAsBit("isPseudo");
342 ImplicitDefs = R->getValueAsListOfDefs("Defs");
343 ImplicitUses = R->getValueAsListOfDefs("Uses");
345 // Parse Constraints.
346 ParseConstraints(R->getValueAsString("Constraints"), Operands);
348 // Parse the DisableEncoding field.
349 Operands.ProcessDisableEncoding(R->getValueAsString("DisableEncoding"));
351 // First check for a ComplexDeprecationPredicate.
352 if (R->getValue("ComplexDeprecationPredicate")) {
353 HasComplexDeprecationPredicate = true;
354 DeprecatedReason = R->getValueAsString("ComplexDeprecationPredicate");
355 } else if (RecordVal *Dep = R->getValue("DeprecatedFeatureMask")) {
356 // Check if we have a Subtarget feature mask.
357 HasComplexDeprecationPredicate = false;
358 DeprecatedReason = Dep->getValue()->getAsString();
360 // This instruction isn't deprecated.
361 HasComplexDeprecationPredicate = false;
362 DeprecatedReason = "";
366 /// HasOneImplicitDefWithKnownVT - If the instruction has at least one
367 /// implicit def and it has a known VT, return the VT, otherwise return
369 MVT::SimpleValueType CodeGenInstruction::
370 HasOneImplicitDefWithKnownVT(const CodeGenTarget &TargetInfo) const {
371 if (ImplicitDefs.empty()) return MVT::Other;
373 // Check to see if the first implicit def has a resolvable type.
374 Record *FirstImplicitDef = ImplicitDefs[0];
375 assert(FirstImplicitDef->isSubClassOf("Register"));
376 const std::vector<MVT::SimpleValueType> &RegVTs =
377 TargetInfo.getRegisterVTs(FirstImplicitDef);
378 if (RegVTs.size() == 1)
384 /// FlattenAsmStringVariants - Flatten the specified AsmString to only
385 /// include text from the specified variant, returning the new string.
386 std::string CodeGenInstruction::
387 FlattenAsmStringVariants(StringRef Cur, unsigned Variant) {
388 std::string Res = "";
391 // Find the start of the next variant string.
392 size_t VariantsStart = 0;
393 for (size_t e = Cur.size(); VariantsStart != e; ++VariantsStart)
394 if (Cur[VariantsStart] == '{' &&
395 (VariantsStart == 0 || (Cur[VariantsStart-1] != '$' &&
396 Cur[VariantsStart-1] != '\\')))
399 // Add the prefix to the result.
400 Res += Cur.slice(0, VariantsStart);
401 if (VariantsStart == Cur.size())
404 ++VariantsStart; // Skip the '{'.
406 // Scan to the end of the variants string.
407 size_t VariantsEnd = VariantsStart;
408 unsigned NestedBraces = 1;
409 for (size_t e = Cur.size(); VariantsEnd != e; ++VariantsEnd) {
410 if (Cur[VariantsEnd] == '}' && Cur[VariantsEnd-1] != '\\') {
411 if (--NestedBraces == 0)
413 } else if (Cur[VariantsEnd] == '{')
417 // Select the Nth variant (or empty).
418 StringRef Selection = Cur.slice(VariantsStart, VariantsEnd);
419 for (unsigned i = 0; i != Variant; ++i)
420 Selection = Selection.split('|').second;
421 Res += Selection.split('|').first;
423 assert(VariantsEnd != Cur.size() &&
424 "Unterminated variants in assembly string!");
425 Cur = Cur.substr(VariantsEnd + 1);
432 //===----------------------------------------------------------------------===//
433 /// CodeGenInstAlias Implementation
434 //===----------------------------------------------------------------------===//
436 /// tryAliasOpMatch - This is a helper function for the CodeGenInstAlias
437 /// constructor. It checks if an argument in an InstAlias pattern matches
438 /// the corresponding operand of the instruction. It returns true on a
439 /// successful match, with ResOp set to the result operand to be used.
440 bool CodeGenInstAlias::tryAliasOpMatch(DagInit *Result, unsigned AliasOpNo,
441 Record *InstOpRec, bool hasSubOps,
442 ArrayRef<SMLoc> Loc, CodeGenTarget &T,
443 ResultOperand &ResOp) {
444 Init *Arg = Result->getArg(AliasOpNo);
445 DefInit *ADI = dyn_cast<DefInit>(Arg);
446 Record *ResultRecord = ADI ? ADI->getDef() : nullptr;
448 if (ADI && ADI->getDef() == InstOpRec) {
449 // If the operand is a record, it must have a name, and the record type
450 // must match up with the instruction's argument type.
451 if (Result->getArgName(AliasOpNo).empty())
452 PrintFatalError(Loc, "result argument #" + Twine(AliasOpNo) +
453 " must have a name!");
454 ResOp = ResultOperand(Result->getArgName(AliasOpNo), ResultRecord);
458 // For register operands, the source register class can be a subclass
459 // of the instruction register class, not just an exact match.
460 if (InstOpRec->isSubClassOf("RegisterOperand"))
461 InstOpRec = InstOpRec->getValueAsDef("RegClass");
463 if (ADI && ADI->getDef()->isSubClassOf("RegisterOperand"))
464 ADI = ADI->getDef()->getValueAsDef("RegClass")->getDefInit();
466 if (ADI && ADI->getDef()->isSubClassOf("RegisterClass")) {
467 if (!InstOpRec->isSubClassOf("RegisterClass"))
469 if (!T.getRegisterClass(InstOpRec)
470 .hasSubClass(&T.getRegisterClass(ADI->getDef())))
472 ResOp = ResultOperand(Result->getArgName(AliasOpNo), ResultRecord);
476 // Handle explicit registers.
477 if (ADI && ADI->getDef()->isSubClassOf("Register")) {
478 if (InstOpRec->isSubClassOf("OptionalDefOperand")) {
479 DagInit *DI = InstOpRec->getValueAsDag("MIOperandInfo");
480 // The operand info should only have a single (register) entry. We
481 // want the register class of it.
482 InstOpRec = cast<DefInit>(DI->getArg(0))->getDef();
485 if (!InstOpRec->isSubClassOf("RegisterClass"))
488 if (!T.getRegisterClass(InstOpRec)
489 .contains(T.getRegBank().getReg(ADI->getDef())))
490 PrintFatalError(Loc, "fixed register " + ADI->getDef()->getName() +
491 " is not a member of the " + InstOpRec->getName() +
494 if (!Result->getArgName(AliasOpNo).empty())
495 PrintFatalError(Loc, "result fixed register argument must "
498 ResOp = ResultOperand(ResultRecord);
502 // Handle "zero_reg" for optional def operands.
503 if (ADI && ADI->getDef()->getName() == "zero_reg") {
505 // Check if this is an optional def.
506 // Tied operands where the source is a sub-operand of a complex operand
507 // need to represent both operands in the alias destination instruction.
508 // Allow zero_reg for the tied portion. This can and should go away once
509 // the MC representation of things doesn't use tied operands at all.
510 //if (!InstOpRec->isSubClassOf("OptionalDefOperand"))
511 // throw TGError(Loc, "reg0 used for result that is not an "
512 // "OptionalDefOperand!");
514 ResOp = ResultOperand(static_cast<Record*>(nullptr));
519 if (IntInit *II = dyn_cast<IntInit>(Arg)) {
520 if (hasSubOps || !InstOpRec->isSubClassOf("Operand"))
522 // Integer arguments can't have names.
523 if (!Result->getArgName(AliasOpNo).empty())
524 PrintFatalError(Loc, "result argument #" + Twine(AliasOpNo) +
525 " must not have a name!");
526 ResOp = ResultOperand(II->getValue());
530 // Bits<n> (also used for 0bxx literals)
531 if (BitsInit *BI = dyn_cast<BitsInit>(Arg)) {
532 if (hasSubOps || !InstOpRec->isSubClassOf("Operand"))
534 if (!BI->isComplete())
536 // Convert the bits init to an integer and use that for the result.
538 dyn_cast_or_null<IntInit>(BI->convertInitializerTo(IntRecTy::get()));
541 ResOp = ResultOperand(II->getValue());
545 // If both are Operands with the same MVT, allow the conversion. It's
546 // up to the user to make sure the values are appropriate, just like
548 if (InstOpRec->isSubClassOf("Operand") && ADI &&
549 ADI->getDef()->isSubClassOf("Operand")) {
550 // FIXME: What other attributes should we check here? Identical
551 // MIOperandInfo perhaps?
552 if (InstOpRec->getValueInit("Type") != ADI->getDef()->getValueInit("Type"))
554 ResOp = ResultOperand(Result->getArgName(AliasOpNo), ADI->getDef());
561 unsigned CodeGenInstAlias::ResultOperand::getMINumOperands() const {
565 Record *Rec = getRecord();
566 if (!Rec->isSubClassOf("Operand"))
569 DagInit *MIOpInfo = Rec->getValueAsDag("MIOperandInfo");
570 if (MIOpInfo->getNumArgs() == 0) {
571 // Unspecified, so it defaults to 1
575 return MIOpInfo->getNumArgs();
578 CodeGenInstAlias::CodeGenInstAlias(Record *R, unsigned Variant,
581 Result = R->getValueAsDag("ResultInst");
582 AsmString = R->getValueAsString("AsmString");
583 AsmString = CodeGenInstruction::FlattenAsmStringVariants(AsmString, Variant);
586 // Verify that the root of the result is an instruction.
587 DefInit *DI = dyn_cast<DefInit>(Result->getOperator());
588 if (!DI || !DI->getDef()->isSubClassOf("Instruction"))
589 PrintFatalError(R->getLoc(),
590 "result of inst alias should be an instruction");
592 ResultInst = &T.getInstruction(DI->getDef());
594 // NameClass - If argument names are repeated, we need to verify they have
596 StringMap<Record*> NameClass;
597 for (unsigned i = 0, e = Result->getNumArgs(); i != e; ++i) {
598 DefInit *ADI = dyn_cast<DefInit>(Result->getArg(i));
599 if (!ADI || Result->getArgName(i).empty())
601 // Verify we don't have something like: (someinst GR16:$foo, GR32:$foo)
602 // $foo can exist multiple times in the result list, but it must have the
604 Record *&Entry = NameClass[Result->getArgName(i)];
605 if (Entry && Entry != ADI->getDef())
606 PrintFatalError(R->getLoc(), "result value $" + Result->getArgName(i) +
607 " is both " + Entry->getName() + " and " +
608 ADI->getDef()->getName() + "!");
609 Entry = ADI->getDef();
612 // Decode and validate the arguments of the result.
613 unsigned AliasOpNo = 0;
614 for (unsigned i = 0, e = ResultInst->Operands.size(); i != e; ++i) {
616 // Tied registers don't have an entry in the result dag unless they're part
617 // of a complex operand, in which case we include them anyways, as we
618 // don't have any other way to specify the whole operand.
619 if (ResultInst->Operands[i].MINumOperands == 1 &&
620 ResultInst->Operands[i].getTiedRegister() != -1)
623 if (AliasOpNo >= Result->getNumArgs())
624 PrintFatalError(R->getLoc(), "not enough arguments for instruction!");
626 Record *InstOpRec = ResultInst->Operands[i].Rec;
627 unsigned NumSubOps = ResultInst->Operands[i].MINumOperands;
628 ResultOperand ResOp(static_cast<int64_t>(0));
629 if (tryAliasOpMatch(Result, AliasOpNo, InstOpRec, (NumSubOps > 1),
630 R->getLoc(), T, ResOp)) {
631 // If this is a simple operand, or a complex operand with a custom match
632 // class, then we can match is verbatim.
633 if (NumSubOps == 1 ||
634 (InstOpRec->getValue("ParserMatchClass") &&
635 InstOpRec->getValueAsDef("ParserMatchClass")
636 ->getValueAsString("Name") != "Imm")) {
637 ResultOperands.push_back(ResOp);
638 ResultInstOperandIndex.push_back(std::make_pair(i, -1));
641 // Otherwise, we need to match each of the suboperands individually.
643 DagInit *MIOI = ResultInst->Operands[i].MIOperandInfo;
644 for (unsigned SubOp = 0; SubOp != NumSubOps; ++SubOp) {
645 Record *SubRec = cast<DefInit>(MIOI->getArg(SubOp))->getDef();
647 // Take care to instantiate each of the suboperands with the correct
648 // nomenclature: $foo.bar
649 ResultOperands.emplace_back(Result->getArgName(AliasOpNo) + "." +
650 MIOI->getArgName(SubOp),
652 ResultInstOperandIndex.push_back(std::make_pair(i, SubOp));
659 // If the argument did not match the instruction operand, and the operand
660 // is composed of multiple suboperands, try matching the suboperands.
662 DagInit *MIOI = ResultInst->Operands[i].MIOperandInfo;
663 for (unsigned SubOp = 0; SubOp != NumSubOps; ++SubOp) {
664 if (AliasOpNo >= Result->getNumArgs())
665 PrintFatalError(R->getLoc(), "not enough arguments for instruction!");
666 Record *SubRec = cast<DefInit>(MIOI->getArg(SubOp))->getDef();
667 if (tryAliasOpMatch(Result, AliasOpNo, SubRec, false,
668 R->getLoc(), T, ResOp)) {
669 ResultOperands.push_back(ResOp);
670 ResultInstOperandIndex.push_back(std::make_pair(i, SubOp));
673 PrintFatalError(R->getLoc(), "result argument #" + Twine(AliasOpNo) +
674 " does not match instruction operand class " +
675 (SubOp == 0 ? InstOpRec->getName() :SubRec->getName()));
680 PrintFatalError(R->getLoc(), "result argument #" + Twine(AliasOpNo) +
681 " does not match instruction operand class " +
682 InstOpRec->getName());
685 if (AliasOpNo != Result->getNumArgs())
686 PrintFatalError(R->getLoc(), "too many operands for instruction!");