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 hasNoSchedulingInfo = R->getValueAsBit("hasNoSchedulingInfo"); 328 329 bool Unset; 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; 336 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"); 344 345 // Parse Constraints. 346 ParseConstraints(R->getValueAsString("Constraints"), Operands); 347 348 // Parse the DisableEncoding field. 349 Operands.ProcessDisableEncoding(R->getValueAsString("DisableEncoding")); 350 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(); 359 } else { 360 // This instruction isn't deprecated. 361 HasComplexDeprecationPredicate = false; 362 DeprecatedReason = ""; 363 } 364 } 365 366 /// HasOneImplicitDefWithKnownVT - If the instruction has at least one 367 /// implicit def and it has a known VT, return the VT, otherwise return 368 /// MVT::Other. 369 MVT::SimpleValueType CodeGenInstruction:: 370 HasOneImplicitDefWithKnownVT(const CodeGenTarget &TargetInfo) const { 371 if (ImplicitDefs.empty()) return MVT::Other; 372 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) 379 return RegVTs[0]; 380 return MVT::Other; 381 } 382 383 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 = ""; 389 390 for (;;) { 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] != '\\'))) 397 break; 398 399 // Add the prefix to the result. 400 Res += Cur.slice(0, VariantsStart); 401 if (VariantsStart == Cur.size()) 402 break; 403 404 ++VariantsStart; // Skip the '{'. 405 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) 412 break; 413 } else if (Cur[VariantsEnd] == '{') 414 ++NestedBraces; 415 } 416 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; 422 423 assert(VariantsEnd != Cur.size() && 424 "Unterminated variants in assembly string!"); 425 Cur = Cur.substr(VariantsEnd + 1); 426 } 427 428 return Res; 429 } 430 431 432 //===----------------------------------------------------------------------===// 433 /// CodeGenInstAlias Implementation 434 //===----------------------------------------------------------------------===// 435 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; 447 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); 455 return true; 456 } 457 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"); 462 463 if (ADI && ADI->getDef()->isSubClassOf("RegisterOperand")) 464 ADI = ADI->getDef()->getValueAsDef("RegClass")->getDefInit(); 465 466 if (ADI && ADI->getDef()->isSubClassOf("RegisterClass")) { 467 if (!InstOpRec->isSubClassOf("RegisterClass")) 468 return false; 469 if (!T.getRegisterClass(InstOpRec) 470 .hasSubClass(&T.getRegisterClass(ADI->getDef()))) 471 return false; 472 ResOp = ResultOperand(Result->getArgName(AliasOpNo), ResultRecord); 473 return true; 474 } 475 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(); 483 } 484 485 if (!InstOpRec->isSubClassOf("RegisterClass")) 486 return false; 487 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() + 492 " register class!"); 493 494 if (!Result->getArgName(AliasOpNo).empty()) 495 PrintFatalError(Loc, "result fixed register argument must " 496 "not have a name!"); 497 498 ResOp = ResultOperand(ResultRecord); 499 return true; 500 } 501 502 // Handle "zero_reg" for optional def operands. 503 if (ADI && ADI->getDef()->getName() == "zero_reg") { 504 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!"); 513 514 ResOp = ResultOperand(static_cast<Record*>(nullptr)); 515 return true; 516 } 517 518 // Literal integers. 519 if (IntInit *II = dyn_cast<IntInit>(Arg)) { 520 if (hasSubOps || !InstOpRec->isSubClassOf("Operand")) 521 return false; 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()); 527 return true; 528 } 529 530 // Bits<n> (also used for 0bxx literals) 531 if (BitsInit *BI = dyn_cast<BitsInit>(Arg)) { 532 if (hasSubOps || !InstOpRec->isSubClassOf("Operand")) 533 return false; 534 if (!BI->isComplete()) 535 return false; 536 // Convert the bits init to an integer and use that for the result. 537 IntInit *II = 538 dyn_cast_or_null<IntInit>(BI->convertInitializerTo(IntRecTy::get())); 539 if (!II) 540 return false; 541 ResOp = ResultOperand(II->getValue()); 542 return true; 543 } 544 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 547 // for isel Pat's. 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")) 553 return false; 554 ResOp = ResultOperand(Result->getArgName(AliasOpNo), ADI->getDef()); 555 return true; 556 } 557 558 return false; 559 } 560 561 unsigned CodeGenInstAlias::ResultOperand::getMINumOperands() const { 562 if (!isRecord()) 563 return 1; 564 565 Record *Rec = getRecord(); 566 if (!Rec->isSubClassOf("Operand")) 567 return 1; 568 569 DagInit *MIOpInfo = Rec->getValueAsDag("MIOperandInfo"); 570 if (MIOpInfo->getNumArgs() == 0) { 571 // Unspecified, so it defaults to 1 572 return 1; 573 } 574 575 return MIOpInfo->getNumArgs(); 576 } 577 578 CodeGenInstAlias::CodeGenInstAlias(Record *R, unsigned Variant, 579 CodeGenTarget &T) 580 : TheDef(R) { 581 Result = R->getValueAsDag("ResultInst"); 582 AsmString = R->getValueAsString("AsmString"); 583 AsmString = CodeGenInstruction::FlattenAsmStringVariants(AsmString, Variant); 584 585 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"); 591 592 ResultInst = &T.getInstruction(DI->getDef()); 593 594 // NameClass - If argument names are repeated, we need to verify they have 595 // the same class. 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()) 600 continue; 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 603 // same type. 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(); 610 } 611 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) { 615 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) 621 continue; 622 623 if (AliasOpNo >= Result->getNumArgs()) 624 PrintFatalError(R->getLoc(), "not enough arguments for instruction!"); 625 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)); 639 ++AliasOpNo; 640 641 // Otherwise, we need to match each of the suboperands individually. 642 } else { 643 DagInit *MIOI = ResultInst->Operands[i].MIOperandInfo; 644 for (unsigned SubOp = 0; SubOp != NumSubOps; ++SubOp) { 645 Record *SubRec = cast<DefInit>(MIOI->getArg(SubOp))->getDef(); 646 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), 651 SubRec); 652 ResultInstOperandIndex.push_back(std::make_pair(i, SubOp)); 653 } 654 ++AliasOpNo; 655 } 656 continue; 657 } 658 659 // If the argument did not match the instruction operand, and the operand 660 // is composed of multiple suboperands, try matching the suboperands. 661 if (NumSubOps > 1) { 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)); 671 ++AliasOpNo; 672 } else { 673 PrintFatalError(R->getLoc(), "result argument #" + Twine(AliasOpNo) + 674 " does not match instruction operand class " + 675 (SubOp == 0 ? InstOpRec->getName() :SubRec->getName())); 676 } 677 } 678 continue; 679 } 680 PrintFatalError(R->getLoc(), "result argument #" + Twine(AliasOpNo) + 681 " does not match instruction operand class " + 682 InstOpRec->getName()); 683 } 684 685 if (AliasOpNo != Result->getNumArgs()) 686 PrintFatalError(R->getLoc(), "too many operands for instruction!"); 687 } 688