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