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 unsigned e = InDI->getNumArgs() + OutDI->getNumArgs(); 53 OperandList.reserve(e); 54 for (unsigned i = 0; i != e; ++i){ 55 Init *ArgInit; 56 std::string ArgName; 57 if (i < NumDefs) { 58 ArgInit = OutDI->getArg(i); 59 ArgName = OutDI->getArgName(i); 60 } else { 61 ArgInit = InDI->getArg(i-NumDefs); 62 ArgName = InDI->getArgName(i-NumDefs); 63 } 64 65 DefInit *Arg = dyn_cast<DefInit>(ArgInit); 66 if (!Arg) 67 PrintFatalError("Illegal operand for the '" + R->getName() + "' instruction!"); 68 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"; 74 unsigned NumOps = 1; 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"); 87 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() + 92 "'\n"); 93 94 // If we have MIOpInfo, then we have #operands equal to number of entries 95 // in MIOperandInfo. 96 if (unsigned NumArgs = MIOpInfo->getNumArgs()) 97 NumOps = NumArgs; 98 99 if (Rec->isSubClassOf("PredicateOp")) 100 isPredicable = true; 101 else if (Rec->isSubClassOf("OptionalDefOperand")) 102 hasOptionalDef = true; 103 } else if (Rec->getName() == "variable_ops") { 104 isVariadic = true; 105 continue; 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!"); 112 113 // Check that the operand has a name and that it's unique. 114 if (ArgName.empty()) 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!"); 120 121 OperandList.emplace_back(Rec, ArgName, PrintMethod, EncoderMethod, 122 OperandNamespace + "::" + OperandType, MIOperandNo, 123 NumOps, MIOpInfo); 124 MIOperandNo += NumOps; 125 } 126 127 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); 132 } 133 134 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. 138 /// 139 unsigned CGIOperandList::getOperandNamed(StringRef Name) const { 140 unsigned OpIdx; 141 if (hasOperandNamed(Name, OpIdx)) return OpIdx; 142 PrintFatalError("'" + TheDef->getName() + 143 "' does not have an operand named '$" + Name + "'!"); 144 } 145 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) { 153 OpIdx = i; 154 return true; 155 } 156 return false; 157 } 158 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 + "'"); 163 164 std::string OpName = Op.substr(1); 165 std::string SubOpName; 166 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); 174 } 175 176 unsigned OpIdx = getOperandNamed(OpName); 177 178 if (SubOpName.empty()) { // If no suboperand name was specified: 179 // If one was needed, throw. 180 if (OperandList[OpIdx].MINumOperands > 1 && !AllowWholeOp && 181 SubOpName.empty()) 182 PrintFatalError(TheDef->getName() + ": Illegal to refer to" 183 " whole operand part of complex operand '" + Op + "'"); 184 185 // Otherwise, return the operand. 186 return std::make_pair(OpIdx, 0U); 187 } 188 189 // Find the suboperand number involved. 190 DagInit *MIOpInfo = OperandList[OpIdx].MIOperandInfo; 191 if (!MIOpInfo) 192 PrintFatalError(TheDef->getName() + ": unknown suboperand name in '" + Op + "'"); 193 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); 198 199 // Otherwise, didn't find it! 200 PrintFatalError(TheDef->getName() + ": unknown suboperand name in '" + Op + "'"); 201 return std::make_pair(0U, 0U); 202 } 203 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); 216 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(); 222 return; 223 } 224 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); 230 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); 237 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 + "'"); 242 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); 248 } 249 250 unsigned FlatOpNo = Ops.getFlattenedOperandNumber(SrcOp); 251 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); 257 } 258 259 static void ParseConstraints(const std::string &CStr, CGIOperandList &Ops) { 260 if (CStr.empty()) return; 261 262 const std::string delims(","); 263 std::string::size_type bidx, eidx; 264 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(); 270 271 ParseConstraint(CStr.substr(bidx, eidx - bidx), Ops); 272 bidx = CStr.find_first_not_of(delims, eidx); 273 } 274 } 275 276 void CGIOperandList::ProcessDisableEncoding(std::string DisableEncoding) { 277 while (1) { 278 std::pair<StringRef, StringRef> P = getToken(DisableEncoding, " ,\t"); 279 std::string OpName = P.first; 280 DisableEncoding = P.second; 281 if (OpName.empty()) break; 282 283 // Figure out which operand this is. 284 std::pair<unsigned,unsigned> Op = ParseOperandName(OpName, false); 285 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; 290 } 291 292 } 293 294 //===----------------------------------------------------------------------===// 295 // CodeGenInstruction Implementation 296 //===----------------------------------------------------------------------===// 297 298 CodeGenInstruction::CodeGenInstruction(Record *R) 299 : TheDef(R), Operands(R), InferredFrom(nullptr) { 300 Namespace = R->getValueAsString("Namespace"); 301 AsmString = R->getValueAsString("AsmString"); 302 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 328 bool Unset; 329 mayLoad = R->getValueAsBitOrUnset("mayLoad", Unset); 330 mayLoad_Unset = Unset; 331 mayStore = R->getValueAsBitOrUnset("mayStore", Unset); 332 mayStore_Unset = Unset; 333 hasSideEffects = R->getValueAsBitOrUnset("hasSideEffects", Unset); 334 hasSideEffects_Unset = Unset; 335 336 isAsCheapAsAMove = R->getValueAsBit("isAsCheapAsAMove"); 337 hasExtraSrcRegAllocReq = R->getValueAsBit("hasExtraSrcRegAllocReq"); 338 hasExtraDefRegAllocReq = R->getValueAsBit("hasExtraDefRegAllocReq"); 339 isCodeGenOnly = R->getValueAsBit("isCodeGenOnly"); 340 isPseudo = R->getValueAsBit("isPseudo"); 341 ImplicitDefs = R->getValueAsListOfDefs("Defs"); 342 ImplicitUses = R->getValueAsListOfDefs("Uses"); 343 344 // Parse Constraints. 345 ParseConstraints(R->getValueAsString("Constraints"), Operands); 346 347 // Parse the DisableEncoding field. 348 Operands.ProcessDisableEncoding(R->getValueAsString("DisableEncoding")); 349 350 // First check for a ComplexDeprecationPredicate. 351 if (R->getValue("ComplexDeprecationPredicate")) { 352 HasComplexDeprecationPredicate = true; 353 DeprecatedReason = R->getValueAsString("ComplexDeprecationPredicate"); 354 } else if (RecordVal *Dep = R->getValue("DeprecatedFeatureMask")) { 355 // Check if we have a Subtarget feature mask. 356 HasComplexDeprecationPredicate = false; 357 DeprecatedReason = Dep->getValue()->getAsString(); 358 } else { 359 // This instruction isn't deprecated. 360 HasComplexDeprecationPredicate = false; 361 DeprecatedReason = ""; 362 } 363 } 364 365 /// HasOneImplicitDefWithKnownVT - If the instruction has at least one 366 /// implicit def and it has a known VT, return the VT, otherwise return 367 /// MVT::Other. 368 MVT::SimpleValueType CodeGenInstruction:: 369 HasOneImplicitDefWithKnownVT(const CodeGenTarget &TargetInfo) const { 370 if (ImplicitDefs.empty()) return MVT::Other; 371 372 // Check to see if the first implicit def has a resolvable type. 373 Record *FirstImplicitDef = ImplicitDefs[0]; 374 assert(FirstImplicitDef->isSubClassOf("Register")); 375 const std::vector<MVT::SimpleValueType> &RegVTs = 376 TargetInfo.getRegisterVTs(FirstImplicitDef); 377 if (RegVTs.size() == 1) 378 return RegVTs[0]; 379 return MVT::Other; 380 } 381 382 383 /// FlattenAsmStringVariants - Flatten the specified AsmString to only 384 /// include text from the specified variant, returning the new string. 385 std::string CodeGenInstruction:: 386 FlattenAsmStringVariants(StringRef Cur, unsigned Variant) { 387 std::string Res = ""; 388 389 for (;;) { 390 // Find the start of the next variant string. 391 size_t VariantsStart = 0; 392 for (size_t e = Cur.size(); VariantsStart != e; ++VariantsStart) 393 if (Cur[VariantsStart] == '{' && 394 (VariantsStart == 0 || (Cur[VariantsStart-1] != '$' && 395 Cur[VariantsStart-1] != '\\'))) 396 break; 397 398 // Add the prefix to the result. 399 Res += Cur.slice(0, VariantsStart); 400 if (VariantsStart == Cur.size()) 401 break; 402 403 ++VariantsStart; // Skip the '{'. 404 405 // Scan to the end of the variants string. 406 size_t VariantsEnd = VariantsStart; 407 unsigned NestedBraces = 1; 408 for (size_t e = Cur.size(); VariantsEnd != e; ++VariantsEnd) { 409 if (Cur[VariantsEnd] == '}' && Cur[VariantsEnd-1] != '\\') { 410 if (--NestedBraces == 0) 411 break; 412 } else if (Cur[VariantsEnd] == '{') 413 ++NestedBraces; 414 } 415 416 // Select the Nth variant (or empty). 417 StringRef Selection = Cur.slice(VariantsStart, VariantsEnd); 418 for (unsigned i = 0; i != Variant; ++i) 419 Selection = Selection.split('|').second; 420 Res += Selection.split('|').first; 421 422 assert(VariantsEnd != Cur.size() && 423 "Unterminated variants in assembly string!"); 424 Cur = Cur.substr(VariantsEnd + 1); 425 } 426 427 return Res; 428 } 429 430 431 //===----------------------------------------------------------------------===// 432 /// CodeGenInstAlias Implementation 433 //===----------------------------------------------------------------------===// 434 435 /// tryAliasOpMatch - This is a helper function for the CodeGenInstAlias 436 /// constructor. It checks if an argument in an InstAlias pattern matches 437 /// the corresponding operand of the instruction. It returns true on a 438 /// successful match, with ResOp set to the result operand to be used. 439 bool CodeGenInstAlias::tryAliasOpMatch(DagInit *Result, unsigned AliasOpNo, 440 Record *InstOpRec, bool hasSubOps, 441 ArrayRef<SMLoc> Loc, CodeGenTarget &T, 442 ResultOperand &ResOp) { 443 Init *Arg = Result->getArg(AliasOpNo); 444 DefInit *ADI = dyn_cast<DefInit>(Arg); 445 Record *ResultRecord = ADI ? ADI->getDef() : nullptr; 446 447 if (ADI && ADI->getDef() == InstOpRec) { 448 // If the operand is a record, it must have a name, and the record type 449 // must match up with the instruction's argument type. 450 if (Result->getArgName(AliasOpNo).empty()) 451 PrintFatalError(Loc, "result argument #" + Twine(AliasOpNo) + 452 " must have a name!"); 453 ResOp = ResultOperand(Result->getArgName(AliasOpNo), ResultRecord); 454 return true; 455 } 456 457 // For register operands, the source register class can be a subclass 458 // of the instruction register class, not just an exact match. 459 if (InstOpRec->isSubClassOf("RegisterOperand")) 460 InstOpRec = InstOpRec->getValueAsDef("RegClass"); 461 462 if (ADI && ADI->getDef()->isSubClassOf("RegisterOperand")) 463 ADI = ADI->getDef()->getValueAsDef("RegClass")->getDefInit(); 464 465 if (ADI && ADI->getDef()->isSubClassOf("RegisterClass")) { 466 if (!InstOpRec->isSubClassOf("RegisterClass")) 467 return false; 468 if (!T.getRegisterClass(InstOpRec) 469 .hasSubClass(&T.getRegisterClass(ADI->getDef()))) 470 return false; 471 ResOp = ResultOperand(Result->getArgName(AliasOpNo), ResultRecord); 472 return true; 473 } 474 475 // Handle explicit registers. 476 if (ADI && ADI->getDef()->isSubClassOf("Register")) { 477 if (InstOpRec->isSubClassOf("OptionalDefOperand")) { 478 DagInit *DI = InstOpRec->getValueAsDag("MIOperandInfo"); 479 // The operand info should only have a single (register) entry. We 480 // want the register class of it. 481 InstOpRec = cast<DefInit>(DI->getArg(0))->getDef(); 482 } 483 484 if (!InstOpRec->isSubClassOf("RegisterClass")) 485 return false; 486 487 if (!T.getRegisterClass(InstOpRec) 488 .contains(T.getRegBank().getReg(ADI->getDef()))) 489 PrintFatalError(Loc, "fixed register " + ADI->getDef()->getName() + 490 " is not a member of the " + InstOpRec->getName() + 491 " register class!"); 492 493 if (!Result->getArgName(AliasOpNo).empty()) 494 PrintFatalError(Loc, "result fixed register argument must " 495 "not have a name!"); 496 497 ResOp = ResultOperand(ResultRecord); 498 return true; 499 } 500 501 // Handle "zero_reg" for optional def operands. 502 if (ADI && ADI->getDef()->getName() == "zero_reg") { 503 504 // Check if this is an optional def. 505 // Tied operands where the source is a sub-operand of a complex operand 506 // need to represent both operands in the alias destination instruction. 507 // Allow zero_reg for the tied portion. This can and should go away once 508 // the MC representation of things doesn't use tied operands at all. 509 //if (!InstOpRec->isSubClassOf("OptionalDefOperand")) 510 // throw TGError(Loc, "reg0 used for result that is not an " 511 // "OptionalDefOperand!"); 512 513 ResOp = ResultOperand(static_cast<Record*>(nullptr)); 514 return true; 515 } 516 517 // Literal integers. 518 if (IntInit *II = dyn_cast<IntInit>(Arg)) { 519 if (hasSubOps || !InstOpRec->isSubClassOf("Operand")) 520 return false; 521 // Integer arguments can't have names. 522 if (!Result->getArgName(AliasOpNo).empty()) 523 PrintFatalError(Loc, "result argument #" + Twine(AliasOpNo) + 524 " must not have a name!"); 525 ResOp = ResultOperand(II->getValue()); 526 return true; 527 } 528 529 // Bits<n> (also used for 0bxx literals) 530 if (BitsInit *BI = dyn_cast<BitsInit>(Arg)) { 531 if (hasSubOps || !InstOpRec->isSubClassOf("Operand")) 532 return false; 533 if (!BI->isComplete()) 534 return false; 535 // Convert the bits init to an integer and use that for the result. 536 IntInit *II = 537 dyn_cast_or_null<IntInit>(BI->convertInitializerTo(IntRecTy::get())); 538 if (!II) 539 return false; 540 ResOp = ResultOperand(II->getValue()); 541 return true; 542 } 543 544 // If both are Operands with the same MVT, allow the conversion. It's 545 // up to the user to make sure the values are appropriate, just like 546 // for isel Pat's. 547 if (InstOpRec->isSubClassOf("Operand") && ADI && 548 ADI->getDef()->isSubClassOf("Operand")) { 549 // FIXME: What other attributes should we check here? Identical 550 // MIOperandInfo perhaps? 551 if (InstOpRec->getValueInit("Type") != ADI->getDef()->getValueInit("Type")) 552 return false; 553 ResOp = ResultOperand(Result->getArgName(AliasOpNo), ADI->getDef()); 554 return true; 555 } 556 557 return false; 558 } 559 560 unsigned CodeGenInstAlias::ResultOperand::getMINumOperands() const { 561 if (!isRecord()) 562 return 1; 563 564 Record *Rec = getRecord(); 565 if (!Rec->isSubClassOf("Operand")) 566 return 1; 567 568 DagInit *MIOpInfo = Rec->getValueAsDag("MIOperandInfo"); 569 if (MIOpInfo->getNumArgs() == 0) { 570 // Unspecified, so it defaults to 1 571 return 1; 572 } 573 574 return MIOpInfo->getNumArgs(); 575 } 576 577 CodeGenInstAlias::CodeGenInstAlias(Record *R, unsigned Variant, 578 CodeGenTarget &T) 579 : TheDef(R) { 580 Result = R->getValueAsDag("ResultInst"); 581 AsmString = R->getValueAsString("AsmString"); 582 AsmString = CodeGenInstruction::FlattenAsmStringVariants(AsmString, Variant); 583 584 585 // Verify that the root of the result is an instruction. 586 DefInit *DI = dyn_cast<DefInit>(Result->getOperator()); 587 if (!DI || !DI->getDef()->isSubClassOf("Instruction")) 588 PrintFatalError(R->getLoc(), 589 "result of inst alias should be an instruction"); 590 591 ResultInst = &T.getInstruction(DI->getDef()); 592 593 // NameClass - If argument names are repeated, we need to verify they have 594 // the same class. 595 StringMap<Record*> NameClass; 596 for (unsigned i = 0, e = Result->getNumArgs(); i != e; ++i) { 597 DefInit *ADI = dyn_cast<DefInit>(Result->getArg(i)); 598 if (!ADI || Result->getArgName(i).empty()) 599 continue; 600 // Verify we don't have something like: (someinst GR16:$foo, GR32:$foo) 601 // $foo can exist multiple times in the result list, but it must have the 602 // same type. 603 Record *&Entry = NameClass[Result->getArgName(i)]; 604 if (Entry && Entry != ADI->getDef()) 605 PrintFatalError(R->getLoc(), "result value $" + Result->getArgName(i) + 606 " is both " + Entry->getName() + " and " + 607 ADI->getDef()->getName() + "!"); 608 Entry = ADI->getDef(); 609 } 610 611 // Decode and validate the arguments of the result. 612 unsigned AliasOpNo = 0; 613 for (unsigned i = 0, e = ResultInst->Operands.size(); i != e; ++i) { 614 615 // Tied registers don't have an entry in the result dag unless they're part 616 // of a complex operand, in which case we include them anyways, as we 617 // don't have any other way to specify the whole operand. 618 if (ResultInst->Operands[i].MINumOperands == 1 && 619 ResultInst->Operands[i].getTiedRegister() != -1) 620 continue; 621 622 if (AliasOpNo >= Result->getNumArgs()) 623 PrintFatalError(R->getLoc(), "not enough arguments for instruction!"); 624 625 Record *InstOpRec = ResultInst->Operands[i].Rec; 626 unsigned NumSubOps = ResultInst->Operands[i].MINumOperands; 627 ResultOperand ResOp(static_cast<int64_t>(0)); 628 if (tryAliasOpMatch(Result, AliasOpNo, InstOpRec, (NumSubOps > 1), 629 R->getLoc(), T, ResOp)) { 630 // If this is a simple operand, or a complex operand with a custom match 631 // class, then we can match is verbatim. 632 if (NumSubOps == 1 || 633 (InstOpRec->getValue("ParserMatchClass") && 634 InstOpRec->getValueAsDef("ParserMatchClass") 635 ->getValueAsString("Name") != "Imm")) { 636 ResultOperands.push_back(ResOp); 637 ResultInstOperandIndex.push_back(std::make_pair(i, -1)); 638 ++AliasOpNo; 639 640 // Otherwise, we need to match each of the suboperands individually. 641 } else { 642 DagInit *MIOI = ResultInst->Operands[i].MIOperandInfo; 643 for (unsigned SubOp = 0; SubOp != NumSubOps; ++SubOp) { 644 Record *SubRec = cast<DefInit>(MIOI->getArg(SubOp))->getDef(); 645 646 // Take care to instantiate each of the suboperands with the correct 647 // nomenclature: $foo.bar 648 ResultOperands.emplace_back(Result->getArgName(AliasOpNo) + "." + 649 MIOI->getArgName(SubOp), 650 SubRec); 651 ResultInstOperandIndex.push_back(std::make_pair(i, SubOp)); 652 } 653 ++AliasOpNo; 654 } 655 continue; 656 } 657 658 // If the argument did not match the instruction operand, and the operand 659 // is composed of multiple suboperands, try matching the suboperands. 660 if (NumSubOps > 1) { 661 DagInit *MIOI = ResultInst->Operands[i].MIOperandInfo; 662 for (unsigned SubOp = 0; SubOp != NumSubOps; ++SubOp) { 663 if (AliasOpNo >= Result->getNumArgs()) 664 PrintFatalError(R->getLoc(), "not enough arguments for instruction!"); 665 Record *SubRec = cast<DefInit>(MIOI->getArg(SubOp))->getDef(); 666 if (tryAliasOpMatch(Result, AliasOpNo, SubRec, false, 667 R->getLoc(), T, ResOp)) { 668 ResultOperands.push_back(ResOp); 669 ResultInstOperandIndex.push_back(std::make_pair(i, SubOp)); 670 ++AliasOpNo; 671 } else { 672 PrintFatalError(R->getLoc(), "result argument #" + Twine(AliasOpNo) + 673 " does not match instruction operand class " + 674 (SubOp == 0 ? InstOpRec->getName() :SubRec->getName())); 675 } 676 } 677 continue; 678 } 679 PrintFatalError(R->getLoc(), "result argument #" + Twine(AliasOpNo) + 680 " does not match instruction operand class " + 681 InstOpRec->getName()); 682 } 683 684 if (AliasOpNo != Result->getNumArgs()) 685 PrintFatalError(R->getLoc(), "too many operands for instruction!"); 686 } 687