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 isAdd = R->getValueAsBit("isAdd"); 313 canFoldAsLoad = R->getValueAsBit("canFoldAsLoad"); 314 isPredicable = Operands.isPredicable || R->getValueAsBit("isPredicable"); 315 isConvertibleToThreeAddress = R->getValueAsBit("isConvertibleToThreeAddress"); 316 isCommutable = R->getValueAsBit("isCommutable"); 317 isTerminator = R->getValueAsBit("isTerminator"); 318 isReMaterializable = R->getValueAsBit("isReMaterializable"); 319 hasDelaySlot = R->getValueAsBit("hasDelaySlot"); 320 usesCustomInserter = R->getValueAsBit("usesCustomInserter"); 321 hasPostISelHook = R->getValueAsBit("hasPostISelHook"); 322 hasCtrlDep = R->getValueAsBit("hasCtrlDep"); 323 isNotDuplicable = R->getValueAsBit("isNotDuplicable"); 324 isRegSequence = R->getValueAsBit("isRegSequence"); 325 isExtractSubreg = R->getValueAsBit("isExtractSubreg"); 326 isInsertSubreg = R->getValueAsBit("isInsertSubreg"); 327 isConvergent = R->getValueAsBit("isConvergent"); 328 hasNoSchedulingInfo = R->getValueAsBit("hasNoSchedulingInfo"); 329 330 bool Unset; 331 mayLoad = R->getValueAsBitOrUnset("mayLoad", Unset); 332 mayLoad_Unset = Unset; 333 mayStore = R->getValueAsBitOrUnset("mayStore", Unset); 334 mayStore_Unset = Unset; 335 hasSideEffects = R->getValueAsBitOrUnset("hasSideEffects", Unset); 336 hasSideEffects_Unset = Unset; 337 338 isAsCheapAsAMove = R->getValueAsBit("isAsCheapAsAMove"); 339 hasExtraSrcRegAllocReq = R->getValueAsBit("hasExtraSrcRegAllocReq"); 340 hasExtraDefRegAllocReq = R->getValueAsBit("hasExtraDefRegAllocReq"); 341 isCodeGenOnly = R->getValueAsBit("isCodeGenOnly"); 342 isPseudo = R->getValueAsBit("isPseudo"); 343 ImplicitDefs = R->getValueAsListOfDefs("Defs"); 344 ImplicitUses = R->getValueAsListOfDefs("Uses"); 345 346 // Parse Constraints. 347 ParseConstraints(R->getValueAsString("Constraints"), Operands); 348 349 // Parse the DisableEncoding field. 350 Operands.ProcessDisableEncoding(R->getValueAsString("DisableEncoding")); 351 352 // First check for a ComplexDeprecationPredicate. 353 if (R->getValue("ComplexDeprecationPredicate")) { 354 HasComplexDeprecationPredicate = true; 355 DeprecatedReason = R->getValueAsString("ComplexDeprecationPredicate"); 356 } else if (RecordVal *Dep = R->getValue("DeprecatedFeatureMask")) { 357 // Check if we have a Subtarget feature mask. 358 HasComplexDeprecationPredicate = false; 359 DeprecatedReason = Dep->getValue()->getAsString(); 360 } else { 361 // This instruction isn't deprecated. 362 HasComplexDeprecationPredicate = false; 363 DeprecatedReason = ""; 364 } 365 } 366 367 /// HasOneImplicitDefWithKnownVT - If the instruction has at least one 368 /// implicit def and it has a known VT, return the VT, otherwise return 369 /// MVT::Other. 370 MVT::SimpleValueType CodeGenInstruction:: 371 HasOneImplicitDefWithKnownVT(const CodeGenTarget &TargetInfo) const { 372 if (ImplicitDefs.empty()) return MVT::Other; 373 374 // Check to see if the first implicit def has a resolvable type. 375 Record *FirstImplicitDef = ImplicitDefs[0]; 376 assert(FirstImplicitDef->isSubClassOf("Register")); 377 const std::vector<MVT::SimpleValueType> &RegVTs = 378 TargetInfo.getRegisterVTs(FirstImplicitDef); 379 if (RegVTs.size() == 1) 380 return RegVTs[0]; 381 return MVT::Other; 382 } 383 384 385 /// FlattenAsmStringVariants - Flatten the specified AsmString to only 386 /// include text from the specified variant, returning the new string. 387 std::string CodeGenInstruction:: 388 FlattenAsmStringVariants(StringRef Cur, unsigned Variant) { 389 std::string Res = ""; 390 391 for (;;) { 392 // Find the start of the next variant string. 393 size_t VariantsStart = 0; 394 for (size_t e = Cur.size(); VariantsStart != e; ++VariantsStart) 395 if (Cur[VariantsStart] == '{' && 396 (VariantsStart == 0 || (Cur[VariantsStart-1] != '$' && 397 Cur[VariantsStart-1] != '\\'))) 398 break; 399 400 // Add the prefix to the result. 401 Res += Cur.slice(0, VariantsStart); 402 if (VariantsStart == Cur.size()) 403 break; 404 405 ++VariantsStart; // Skip the '{'. 406 407 // Scan to the end of the variants string. 408 size_t VariantsEnd = VariantsStart; 409 unsigned NestedBraces = 1; 410 for (size_t e = Cur.size(); VariantsEnd != e; ++VariantsEnd) { 411 if (Cur[VariantsEnd] == '}' && Cur[VariantsEnd-1] != '\\') { 412 if (--NestedBraces == 0) 413 break; 414 } else if (Cur[VariantsEnd] == '{') 415 ++NestedBraces; 416 } 417 418 // Select the Nth variant (or empty). 419 StringRef Selection = Cur.slice(VariantsStart, VariantsEnd); 420 for (unsigned i = 0; i != Variant; ++i) 421 Selection = Selection.split('|').second; 422 Res += Selection.split('|').first; 423 424 assert(VariantsEnd != Cur.size() && 425 "Unterminated variants in assembly string!"); 426 Cur = Cur.substr(VariantsEnd + 1); 427 } 428 429 return Res; 430 } 431 432 433 //===----------------------------------------------------------------------===// 434 /// CodeGenInstAlias Implementation 435 //===----------------------------------------------------------------------===// 436 437 /// tryAliasOpMatch - This is a helper function for the CodeGenInstAlias 438 /// constructor. It checks if an argument in an InstAlias pattern matches 439 /// the corresponding operand of the instruction. It returns true on a 440 /// successful match, with ResOp set to the result operand to be used. 441 bool CodeGenInstAlias::tryAliasOpMatch(DagInit *Result, unsigned AliasOpNo, 442 Record *InstOpRec, bool hasSubOps, 443 ArrayRef<SMLoc> Loc, CodeGenTarget &T, 444 ResultOperand &ResOp) { 445 Init *Arg = Result->getArg(AliasOpNo); 446 DefInit *ADI = dyn_cast<DefInit>(Arg); 447 Record *ResultRecord = ADI ? ADI->getDef() : nullptr; 448 449 if (ADI && ADI->getDef() == InstOpRec) { 450 // If the operand is a record, it must have a name, and the record type 451 // must match up with the instruction's argument type. 452 if (Result->getArgName(AliasOpNo).empty()) 453 PrintFatalError(Loc, "result argument #" + Twine(AliasOpNo) + 454 " must have a name!"); 455 ResOp = ResultOperand(Result->getArgName(AliasOpNo), ResultRecord); 456 return true; 457 } 458 459 // For register operands, the source register class can be a subclass 460 // of the instruction register class, not just an exact match. 461 if (InstOpRec->isSubClassOf("RegisterOperand")) 462 InstOpRec = InstOpRec->getValueAsDef("RegClass"); 463 464 if (ADI && ADI->getDef()->isSubClassOf("RegisterOperand")) 465 ADI = ADI->getDef()->getValueAsDef("RegClass")->getDefInit(); 466 467 if (ADI && ADI->getDef()->isSubClassOf("RegisterClass")) { 468 if (!InstOpRec->isSubClassOf("RegisterClass")) 469 return false; 470 if (!T.getRegisterClass(InstOpRec) 471 .hasSubClass(&T.getRegisterClass(ADI->getDef()))) 472 return false; 473 ResOp = ResultOperand(Result->getArgName(AliasOpNo), ResultRecord); 474 return true; 475 } 476 477 // Handle explicit registers. 478 if (ADI && ADI->getDef()->isSubClassOf("Register")) { 479 if (InstOpRec->isSubClassOf("OptionalDefOperand")) { 480 DagInit *DI = InstOpRec->getValueAsDag("MIOperandInfo"); 481 // The operand info should only have a single (register) entry. We 482 // want the register class of it. 483 InstOpRec = cast<DefInit>(DI->getArg(0))->getDef(); 484 } 485 486 if (!InstOpRec->isSubClassOf("RegisterClass")) 487 return false; 488 489 if (!T.getRegisterClass(InstOpRec) 490 .contains(T.getRegBank().getReg(ADI->getDef()))) 491 PrintFatalError(Loc, "fixed register " + ADI->getDef()->getName() + 492 " is not a member of the " + InstOpRec->getName() + 493 " register class!"); 494 495 if (!Result->getArgName(AliasOpNo).empty()) 496 PrintFatalError(Loc, "result fixed register argument must " 497 "not have a name!"); 498 499 ResOp = ResultOperand(ResultRecord); 500 return true; 501 } 502 503 // Handle "zero_reg" for optional def operands. 504 if (ADI && ADI->getDef()->getName() == "zero_reg") { 505 506 // Check if this is an optional def. 507 // Tied operands where the source is a sub-operand of a complex operand 508 // need to represent both operands in the alias destination instruction. 509 // Allow zero_reg for the tied portion. This can and should go away once 510 // the MC representation of things doesn't use tied operands at all. 511 //if (!InstOpRec->isSubClassOf("OptionalDefOperand")) 512 // throw TGError(Loc, "reg0 used for result that is not an " 513 // "OptionalDefOperand!"); 514 515 ResOp = ResultOperand(static_cast<Record*>(nullptr)); 516 return true; 517 } 518 519 // Literal integers. 520 if (IntInit *II = dyn_cast<IntInit>(Arg)) { 521 if (hasSubOps || !InstOpRec->isSubClassOf("Operand")) 522 return false; 523 // Integer arguments can't have names. 524 if (!Result->getArgName(AliasOpNo).empty()) 525 PrintFatalError(Loc, "result argument #" + Twine(AliasOpNo) + 526 " must not have a name!"); 527 ResOp = ResultOperand(II->getValue()); 528 return true; 529 } 530 531 // Bits<n> (also used for 0bxx literals) 532 if (BitsInit *BI = dyn_cast<BitsInit>(Arg)) { 533 if (hasSubOps || !InstOpRec->isSubClassOf("Operand")) 534 return false; 535 if (!BI->isComplete()) 536 return false; 537 // Convert the bits init to an integer and use that for the result. 538 IntInit *II = 539 dyn_cast_or_null<IntInit>(BI->convertInitializerTo(IntRecTy::get())); 540 if (!II) 541 return false; 542 ResOp = ResultOperand(II->getValue()); 543 return true; 544 } 545 546 // If both are Operands with the same MVT, allow the conversion. It's 547 // up to the user to make sure the values are appropriate, just like 548 // for isel Pat's. 549 if (InstOpRec->isSubClassOf("Operand") && ADI && 550 ADI->getDef()->isSubClassOf("Operand")) { 551 // FIXME: What other attributes should we check here? Identical 552 // MIOperandInfo perhaps? 553 if (InstOpRec->getValueInit("Type") != ADI->getDef()->getValueInit("Type")) 554 return false; 555 ResOp = ResultOperand(Result->getArgName(AliasOpNo), ADI->getDef()); 556 return true; 557 } 558 559 return false; 560 } 561 562 unsigned CodeGenInstAlias::ResultOperand::getMINumOperands() const { 563 if (!isRecord()) 564 return 1; 565 566 Record *Rec = getRecord(); 567 if (!Rec->isSubClassOf("Operand")) 568 return 1; 569 570 DagInit *MIOpInfo = Rec->getValueAsDag("MIOperandInfo"); 571 if (MIOpInfo->getNumArgs() == 0) { 572 // Unspecified, so it defaults to 1 573 return 1; 574 } 575 576 return MIOpInfo->getNumArgs(); 577 } 578 579 CodeGenInstAlias::CodeGenInstAlias(Record *R, unsigned Variant, 580 CodeGenTarget &T) 581 : TheDef(R) { 582 Result = R->getValueAsDag("ResultInst"); 583 AsmString = R->getValueAsString("AsmString"); 584 AsmString = CodeGenInstruction::FlattenAsmStringVariants(AsmString, Variant); 585 586 587 // Verify that the root of the result is an instruction. 588 DefInit *DI = dyn_cast<DefInit>(Result->getOperator()); 589 if (!DI || !DI->getDef()->isSubClassOf("Instruction")) 590 PrintFatalError(R->getLoc(), 591 "result of inst alias should be an instruction"); 592 593 ResultInst = &T.getInstruction(DI->getDef()); 594 595 // NameClass - If argument names are repeated, we need to verify they have 596 // the same class. 597 StringMap<Record*> NameClass; 598 for (unsigned i = 0, e = Result->getNumArgs(); i != e; ++i) { 599 DefInit *ADI = dyn_cast<DefInit>(Result->getArg(i)); 600 if (!ADI || Result->getArgName(i).empty()) 601 continue; 602 // Verify we don't have something like: (someinst GR16:$foo, GR32:$foo) 603 // $foo can exist multiple times in the result list, but it must have the 604 // same type. 605 Record *&Entry = NameClass[Result->getArgName(i)]; 606 if (Entry && Entry != ADI->getDef()) 607 PrintFatalError(R->getLoc(), "result value $" + Result->getArgName(i) + 608 " is both " + Entry->getName() + " and " + 609 ADI->getDef()->getName() + "!"); 610 Entry = ADI->getDef(); 611 } 612 613 // Decode and validate the arguments of the result. 614 unsigned AliasOpNo = 0; 615 for (unsigned i = 0, e = ResultInst->Operands.size(); i != e; ++i) { 616 617 // Tied registers don't have an entry in the result dag unless they're part 618 // of a complex operand, in which case we include them anyways, as we 619 // don't have any other way to specify the whole operand. 620 if (ResultInst->Operands[i].MINumOperands == 1 && 621 ResultInst->Operands[i].getTiedRegister() != -1) 622 continue; 623 624 if (AliasOpNo >= Result->getNumArgs()) 625 PrintFatalError(R->getLoc(), "not enough arguments for instruction!"); 626 627 Record *InstOpRec = ResultInst->Operands[i].Rec; 628 unsigned NumSubOps = ResultInst->Operands[i].MINumOperands; 629 ResultOperand ResOp(static_cast<int64_t>(0)); 630 if (tryAliasOpMatch(Result, AliasOpNo, InstOpRec, (NumSubOps > 1), 631 R->getLoc(), T, ResOp)) { 632 // If this is a simple operand, or a complex operand with a custom match 633 // class, then we can match is verbatim. 634 if (NumSubOps == 1 || 635 (InstOpRec->getValue("ParserMatchClass") && 636 InstOpRec->getValueAsDef("ParserMatchClass") 637 ->getValueAsString("Name") != "Imm")) { 638 ResultOperands.push_back(ResOp); 639 ResultInstOperandIndex.push_back(std::make_pair(i, -1)); 640 ++AliasOpNo; 641 642 // Otherwise, we need to match each of the suboperands individually. 643 } else { 644 DagInit *MIOI = ResultInst->Operands[i].MIOperandInfo; 645 for (unsigned SubOp = 0; SubOp != NumSubOps; ++SubOp) { 646 Record *SubRec = cast<DefInit>(MIOI->getArg(SubOp))->getDef(); 647 648 // Take care to instantiate each of the suboperands with the correct 649 // nomenclature: $foo.bar 650 ResultOperands.emplace_back(Result->getArgName(AliasOpNo) + "." + 651 MIOI->getArgName(SubOp), 652 SubRec); 653 ResultInstOperandIndex.push_back(std::make_pair(i, SubOp)); 654 } 655 ++AliasOpNo; 656 } 657 continue; 658 } 659 660 // If the argument did not match the instruction operand, and the operand 661 // is composed of multiple suboperands, try matching the suboperands. 662 if (NumSubOps > 1) { 663 DagInit *MIOI = ResultInst->Operands[i].MIOperandInfo; 664 for (unsigned SubOp = 0; SubOp != NumSubOps; ++SubOp) { 665 if (AliasOpNo >= Result->getNumArgs()) 666 PrintFatalError(R->getLoc(), "not enough arguments for instruction!"); 667 Record *SubRec = cast<DefInit>(MIOI->getArg(SubOp))->getDef(); 668 if (tryAliasOpMatch(Result, AliasOpNo, SubRec, false, 669 R->getLoc(), T, ResOp)) { 670 ResultOperands.push_back(ResOp); 671 ResultInstOperandIndex.push_back(std::make_pair(i, SubOp)); 672 ++AliasOpNo; 673 } else { 674 PrintFatalError(R->getLoc(), "result argument #" + Twine(AliasOpNo) + 675 " does not match instruction operand class " + 676 (SubOp == 0 ? InstOpRec->getName() :SubRec->getName())); 677 } 678 } 679 continue; 680 } 681 PrintFatalError(R->getLoc(), "result argument #" + Twine(AliasOpNo) + 682 " does not match instruction operand class " + 683 InstOpRec->getName()); 684 } 685 686 if (AliasOpNo != Result->getNumArgs()) 687 PrintFatalError(R->getLoc(), "too many operands for instruction!"); 688 } 689