1 //===- CodeGenInstruction.cpp - CodeGen Instruction Class Wrapper ---------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements the CodeGenInstruction class. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "CodeGenInstruction.h" 14 #include "CodeGenTarget.h" 15 #include "llvm/ADT/StringExtras.h" 16 #include "llvm/TableGen/Error.h" 17 #include "llvm/TableGen/Record.h" 18 #include <set> 19 using namespace llvm; 20 21 //===----------------------------------------------------------------------===// 22 // CGIOperandList Implementation 23 //===----------------------------------------------------------------------===// 24 25 CGIOperandList::CGIOperandList(const Record *R) : TheDef(R) { 26 isPredicable = false; 27 hasOptionalDef = false; 28 isVariadic = false; 29 30 const DagInit *OutDI = R->getValueAsDag("OutOperandList"); 31 32 if (const DefInit *Init = dyn_cast<DefInit>(OutDI->getOperator())) { 33 if (Init->getDef()->getName() != "outs") 34 PrintFatalError(R->getLoc(), 35 R->getName() + 36 ": invalid def name for output list: use 'outs'"); 37 } else 38 PrintFatalError(R->getLoc(), 39 R->getName() + ": invalid output list: use 'outs'"); 40 41 NumDefs = OutDI->getNumArgs(); 42 43 const DagInit *InDI = R->getValueAsDag("InOperandList"); 44 if (const DefInit *Init = dyn_cast<DefInit>(InDI->getOperator())) { 45 if (Init->getDef()->getName() != "ins") 46 PrintFatalError(R->getLoc(), 47 R->getName() + 48 ": invalid def name for input list: use 'ins'"); 49 } else 50 PrintFatalError(R->getLoc(), 51 R->getName() + ": invalid input list: use 'ins'"); 52 53 unsigned MIOperandNo = 0; 54 std::set<std::string> OperandNames; 55 unsigned e = InDI->getNumArgs() + OutDI->getNumArgs(); 56 OperandList.reserve(e); 57 bool VariadicOuts = false; 58 for (unsigned i = 0; i != e; ++i) { 59 const Init *ArgInit; 60 StringRef ArgName; 61 if (i < NumDefs) { 62 ArgInit = OutDI->getArg(i); 63 ArgName = OutDI->getArgNameStr(i); 64 } else { 65 ArgInit = InDI->getArg(i - NumDefs); 66 ArgName = InDI->getArgNameStr(i - NumDefs); 67 } 68 69 const DagInit *SubArgDag = dyn_cast<DagInit>(ArgInit); 70 if (SubArgDag) 71 ArgInit = SubArgDag->getOperator(); 72 73 const DefInit *Arg = dyn_cast<DefInit>(ArgInit); 74 if (!Arg) 75 PrintFatalError(R->getLoc(), "Illegal operand for the '" + R->getName() + 76 "' instruction!"); 77 78 const Record *Rec = Arg->getDef(); 79 std::string PrintMethod = "printOperand"; 80 std::string EncoderMethod; 81 std::string OperandType = "OPERAND_UNKNOWN"; 82 std::string OperandNamespace = "MCOI"; 83 unsigned NumOps = 1; 84 const DagInit *MIOpInfo = nullptr; 85 if (Rec->isSubClassOf("RegisterOperand")) { 86 PrintMethod = std::string(Rec->getValueAsString("PrintMethod")); 87 OperandType = std::string(Rec->getValueAsString("OperandType")); 88 OperandNamespace = std::string(Rec->getValueAsString("OperandNamespace")); 89 EncoderMethod = std::string(Rec->getValueAsString("EncoderMethod")); 90 } else if (Rec->isSubClassOf("Operand")) { 91 PrintMethod = std::string(Rec->getValueAsString("PrintMethod")); 92 OperandType = std::string(Rec->getValueAsString("OperandType")); 93 OperandNamespace = std::string(Rec->getValueAsString("OperandNamespace")); 94 // If there is an explicit encoder method, use it. 95 EncoderMethod = std::string(Rec->getValueAsString("EncoderMethod")); 96 MIOpInfo = Rec->getValueAsDag("MIOperandInfo"); 97 98 // Verify that MIOpInfo has an 'ops' root value. 99 if (!isa<DefInit>(MIOpInfo->getOperator()) || 100 cast<DefInit>(MIOpInfo->getOperator())->getDef()->getName() != "ops") 101 PrintFatalError(R->getLoc(), 102 "Bad value for MIOperandInfo in operand '" + 103 Rec->getName() + "'\n"); 104 105 // If we have MIOpInfo, then we have #operands equal to number of entries 106 // in MIOperandInfo. 107 if (unsigned NumArgs = MIOpInfo->getNumArgs()) 108 NumOps = NumArgs; 109 110 if (Rec->isSubClassOf("PredicateOp")) 111 isPredicable = true; 112 else if (Rec->isSubClassOf("OptionalDefOperand")) 113 hasOptionalDef = true; 114 } else if (Rec->getName() == "variable_ops") { 115 if (i < NumDefs) 116 VariadicOuts = true; 117 isVariadic = true; 118 continue; 119 } else if (Rec->isSubClassOf("RegisterClass")) { 120 OperandType = "OPERAND_REGISTER"; 121 } else if (!Rec->isSubClassOf("PointerLikeRegClass") && 122 !Rec->isSubClassOf("unknown_class")) { 123 PrintFatalError(R->getLoc(), "Unknown operand class '" + Rec->getName() + 124 "' in '" + R->getName() + 125 "' instruction!"); 126 } 127 128 // Check that the operand has a name and that it's unique. 129 if (ArgName.empty()) 130 PrintFatalError(R->getLoc(), "In instruction '" + R->getName() + 131 "', operand #" + Twine(i) + 132 " has no name!"); 133 if (!OperandNames.insert(std::string(ArgName)).second) 134 PrintFatalError(R->getLoc(), 135 "In instruction '" + R->getName() + "', operand #" + 136 Twine(i) + 137 " has the same name as a previous operand!"); 138 139 OperandInfo &OpInfo = OperandList.emplace_back( 140 Rec, std::string(ArgName), std::string(std::move(PrintMethod)), 141 OperandNamespace + "::" + OperandType, MIOperandNo, NumOps, MIOpInfo); 142 143 if (SubArgDag) { 144 if (SubArgDag->getNumArgs() != NumOps) { 145 PrintFatalError(R->getLoc(), "In instruction '" + R->getName() + 146 "', operand #" + Twine(i) + " has " + 147 Twine(SubArgDag->getNumArgs()) + 148 " sub-arg names, expected " + 149 Twine(NumOps) + "."); 150 } 151 152 for (unsigned j = 0; j < NumOps; ++j) { 153 if (!isa<UnsetInit>(SubArgDag->getArg(j))) 154 PrintFatalError(R->getLoc(), 155 "In instruction '" + R->getName() + "', operand #" + 156 Twine(i) + " sub-arg #" + Twine(j) + 157 " has unexpected operand (expected only $name)."); 158 159 StringRef SubArgName = SubArgDag->getArgNameStr(j); 160 if (SubArgName.empty()) 161 PrintFatalError(R->getLoc(), "In instruction '" + R->getName() + 162 "', operand #" + Twine(i) + 163 " has no name!"); 164 if (!OperandNames.insert(std::string(SubArgName)).second) 165 PrintFatalError(R->getLoc(), 166 "In instruction '" + R->getName() + "', operand #" + 167 Twine(i) + " sub-arg #" + Twine(j) + 168 " has the same name as a previous operand!"); 169 170 if (auto MaybeEncoderMethod = 171 cast<DefInit>(MIOpInfo->getArg(j)) 172 ->getDef() 173 ->getValueAsOptionalString("EncoderMethod")) { 174 OpInfo.EncoderMethodNames[j] = *MaybeEncoderMethod; 175 } 176 177 OpInfo.SubOpNames[j] = SubArgName; 178 SubOpAliases[SubArgName] = {i, j}; 179 } 180 } else if (!EncoderMethod.empty()) { 181 // If we have no explicit sub-op dag, but have an top-level encoder 182 // method, the single encoder will multiple sub-ops, itself. 183 OpInfo.EncoderMethodNames[0] = std::move(EncoderMethod); 184 for (unsigned j = 1; j < NumOps; ++j) 185 OpInfo.DoNotEncode[j] = true; 186 } 187 188 MIOperandNo += NumOps; 189 } 190 191 if (VariadicOuts) 192 --NumDefs; 193 } 194 195 /// getOperandNamed - Return the index of the operand with the specified 196 /// non-empty name. If the instruction does not have an operand with the 197 /// specified name, abort. 198 /// 199 unsigned CGIOperandList::getOperandNamed(StringRef Name) const { 200 unsigned OpIdx; 201 if (hasOperandNamed(Name, OpIdx)) 202 return OpIdx; 203 PrintFatalError(TheDef->getLoc(), "'" + TheDef->getName() + 204 "' does not have an operand named '$" + 205 Name + "'!"); 206 } 207 208 /// hasOperandNamed - Query whether the instruction has an operand of the 209 /// given name. If so, return true and set OpIdx to the index of the 210 /// operand. Otherwise, return false. 211 bool CGIOperandList::hasOperandNamed(StringRef Name, unsigned &OpIdx) const { 212 assert(!Name.empty() && "Cannot search for operand with no name!"); 213 for (unsigned i = 0, e = OperandList.size(); i != e; ++i) 214 if (OperandList[i].Name == Name) { 215 OpIdx = i; 216 return true; 217 } 218 return false; 219 } 220 221 bool CGIOperandList::hasSubOperandAlias( 222 StringRef Name, std::pair<unsigned, unsigned> &SubOp) const { 223 assert(!Name.empty() && "Cannot search for operand with no name!"); 224 auto SubOpIter = SubOpAliases.find(Name); 225 if (SubOpIter != SubOpAliases.end()) { 226 SubOp = SubOpIter->second; 227 return true; 228 } 229 return false; 230 } 231 232 std::pair<unsigned, unsigned> 233 CGIOperandList::ParseOperandName(StringRef Op, bool AllowWholeOp) { 234 if (!Op.starts_with("$")) 235 PrintFatalError(TheDef->getLoc(), 236 TheDef->getName() + ": Illegal operand name: '" + Op + "'"); 237 238 StringRef OpName = Op.substr(1); 239 StringRef SubOpName; 240 241 // Check to see if this is $foo.bar. 242 StringRef::size_type DotIdx = OpName.find_first_of('.'); 243 if (DotIdx != StringRef::npos) { 244 SubOpName = OpName.substr(DotIdx + 1); 245 if (SubOpName.empty()) 246 PrintFatalError(TheDef->getLoc(), 247 TheDef->getName() + 248 ": illegal empty suboperand name in '" + Op + "'"); 249 OpName = OpName.substr(0, DotIdx); 250 } 251 252 unsigned OpIdx; 253 254 if (std::pair<unsigned, unsigned> SubOp; hasSubOperandAlias(OpName, SubOp)) { 255 // Found a name for a piece of an operand, just return it directly. 256 if (!SubOpName.empty()) { 257 PrintFatalError( 258 TheDef->getLoc(), 259 TheDef->getName() + 260 ": Cannot use dotted suboperand name within suboperand '" + 261 OpName + "'"); 262 } 263 return SubOp; 264 } 265 266 OpIdx = getOperandNamed(OpName); 267 268 if (SubOpName.empty()) { // If no suboperand name was specified: 269 // If one was needed, throw. 270 if (OperandList[OpIdx].MINumOperands > 1 && !AllowWholeOp && 271 SubOpName.empty()) 272 PrintFatalError(TheDef->getLoc(), 273 TheDef->getName() + 274 ": Illegal to refer to" 275 " whole operand part of complex operand '" + 276 Op + "'"); 277 278 // Otherwise, return the operand. 279 return {OpIdx, 0U}; 280 } 281 282 // Find the suboperand number involved. 283 const DagInit *MIOpInfo = OperandList[OpIdx].MIOperandInfo; 284 if (!MIOpInfo) 285 PrintFatalError(TheDef->getLoc(), TheDef->getName() + 286 ": unknown suboperand name in '" + 287 Op + "'"); 288 289 // Find the operand with the right name. 290 for (unsigned i = 0, e = MIOpInfo->getNumArgs(); i != e; ++i) 291 if (MIOpInfo->getArgNameStr(i) == SubOpName) 292 return {OpIdx, i}; 293 294 // Otherwise, didn't find it! 295 PrintFatalError(TheDef->getLoc(), TheDef->getName() + 296 ": unknown suboperand name in '" + Op + 297 "'"); 298 return {0U, 0U}; 299 } 300 301 static void ParseConstraint(StringRef CStr, CGIOperandList &Ops, 302 const Record *Rec) { 303 // EARLY_CLOBBER: @early $reg 304 StringRef::size_type wpos = CStr.find_first_of(" \t"); 305 StringRef::size_type start = CStr.find_first_not_of(" \t"); 306 StringRef Tok = CStr.substr(start, wpos - start); 307 if (Tok == "@earlyclobber") { 308 StringRef Name = CStr.substr(wpos + 1); 309 wpos = Name.find_first_not_of(" \t"); 310 if (wpos == StringRef::npos) 311 PrintFatalError(Rec->getLoc(), 312 "Illegal format for @earlyclobber constraint in '" + 313 Rec->getName() + "': '" + CStr + "'"); 314 Name = Name.substr(wpos); 315 std::pair<unsigned, unsigned> Op = Ops.ParseOperandName(Name, false); 316 317 // Build the string for the operand 318 if (!Ops[Op.first].Constraints[Op.second].isNone()) 319 PrintFatalError(Rec->getLoc(), "Operand '" + Name + "' of '" + 320 Rec->getName() + 321 "' cannot have multiple constraints!"); 322 Ops[Op.first].Constraints[Op.second] = 323 CGIOperandList::ConstraintInfo::getEarlyClobber(); 324 return; 325 } 326 327 // Only other constraint is "TIED_TO" for now. 328 StringRef::size_type pos = CStr.find_first_of('='); 329 if (pos == StringRef::npos || pos == 0 || 330 CStr.find_first_of(" \t", pos) != (pos + 1) || 331 CStr.find_last_of(" \t", pos) != (pos - 1)) 332 PrintFatalError(Rec->getLoc(), "Unrecognized constraint '" + CStr + 333 "' in '" + Rec->getName() + "'"); 334 start = CStr.find_first_not_of(" \t"); 335 336 // TIED_TO: $src1 = $dst 337 wpos = CStr.find_first_of(" \t", start); 338 if (wpos == StringRef::npos || wpos > pos) 339 PrintFatalError(Rec->getLoc(), 340 "Illegal format for tied-to constraint in '" + 341 Rec->getName() + "': '" + CStr + "'"); 342 StringRef LHSOpName = CStr.substr(start, wpos - start); 343 std::pair<unsigned, unsigned> LHSOp = Ops.ParseOperandName(LHSOpName, false); 344 345 wpos = CStr.find_first_not_of(" \t", pos + 1); 346 if (wpos == StringRef::npos) 347 PrintFatalError(Rec->getLoc(), 348 "Illegal format for tied-to constraint: '" + CStr + "'"); 349 350 StringRef RHSOpName = CStr.substr(wpos); 351 std::pair<unsigned, unsigned> RHSOp = Ops.ParseOperandName(RHSOpName, false); 352 353 // Sort the operands into order, which should put the output one 354 // first. But keep the original order, for use in diagnostics. 355 bool FirstIsDest = (LHSOp < RHSOp); 356 std::pair<unsigned, unsigned> DestOp = (FirstIsDest ? LHSOp : RHSOp); 357 StringRef DestOpName = (FirstIsDest ? LHSOpName : RHSOpName); 358 std::pair<unsigned, unsigned> SrcOp = (FirstIsDest ? RHSOp : LHSOp); 359 StringRef SrcOpName = (FirstIsDest ? RHSOpName : LHSOpName); 360 361 // Ensure one operand is a def and the other is a use. 362 if (DestOp.first >= Ops.NumDefs) 363 PrintFatalError(Rec->getLoc(), "Input operands '" + LHSOpName + "' and '" + 364 RHSOpName + "' of '" + Rec->getName() + 365 "' cannot be tied!"); 366 if (SrcOp.first < Ops.NumDefs) 367 PrintFatalError(Rec->getLoc(), "Output operands '" + LHSOpName + "' and '" + 368 RHSOpName + "' of '" + Rec->getName() + 369 "' cannot be tied!"); 370 371 // The constraint has to go on the operand with higher index, i.e. 372 // the source one. Check there isn't another constraint there 373 // already. 374 if (!Ops[SrcOp.first].Constraints[SrcOp.second].isNone()) 375 PrintFatalError(Rec->getLoc(), "Operand '" + SrcOpName + "' of '" + 376 Rec->getName() + 377 "' cannot have multiple constraints!"); 378 379 unsigned DestFlatOpNo = Ops.getFlattenedOperandNumber(DestOp); 380 auto NewConstraint = CGIOperandList::ConstraintInfo::getTied(DestFlatOpNo); 381 382 // Check that the earlier operand is not the target of another tie 383 // before making it the target of this one. 384 for (const CGIOperandList::OperandInfo &Op : Ops) { 385 for (unsigned i = 0; i < Op.MINumOperands; i++) 386 if (Op.Constraints[i] == NewConstraint) 387 PrintFatalError(Rec->getLoc(), 388 "Operand '" + DestOpName + "' of '" + Rec->getName() + 389 "' cannot have multiple operands tied to it!"); 390 } 391 392 Ops[SrcOp.first].Constraints[SrcOp.second] = NewConstraint; 393 } 394 395 static void ParseConstraints(StringRef CStr, CGIOperandList &Ops, 396 const Record *Rec) { 397 if (CStr.empty()) 398 return; 399 400 StringRef delims(","); 401 StringRef::size_type bidx, eidx; 402 403 bidx = CStr.find_first_not_of(delims); 404 while (bidx != StringRef::npos) { 405 eidx = CStr.find_first_of(delims, bidx); 406 if (eidx == StringRef::npos) 407 eidx = CStr.size(); 408 409 ParseConstraint(CStr.substr(bidx, eidx - bidx), Ops, Rec); 410 bidx = CStr.find_first_not_of(delims, eidx); 411 } 412 } 413 414 void CGIOperandList::ProcessDisableEncoding(StringRef DisableEncoding) { 415 while (true) { 416 StringRef OpName; 417 std::tie(OpName, DisableEncoding) = getToken(DisableEncoding, " ,\t"); 418 if (OpName.empty()) 419 break; 420 421 // Figure out which operand this is. 422 std::pair<unsigned, unsigned> Op = ParseOperandName(OpName, false); 423 424 // Mark the operand as not-to-be encoded. 425 OperandList[Op.first].DoNotEncode[Op.second] = true; 426 } 427 } 428 429 //===----------------------------------------------------------------------===// 430 // CodeGenInstruction Implementation 431 //===----------------------------------------------------------------------===// 432 433 CodeGenInstruction::CodeGenInstruction(const Record *R) 434 : TheDef(R), Operands(R), InferredFrom(nullptr) { 435 Namespace = R->getValueAsString("Namespace"); 436 AsmString = std::string(R->getValueAsString("AsmString")); 437 438 isPreISelOpcode = R->getValueAsBit("isPreISelOpcode"); 439 isReturn = R->getValueAsBit("isReturn"); 440 isEHScopeReturn = R->getValueAsBit("isEHScopeReturn"); 441 isBranch = R->getValueAsBit("isBranch"); 442 isIndirectBranch = R->getValueAsBit("isIndirectBranch"); 443 isCompare = R->getValueAsBit("isCompare"); 444 isMoveImm = R->getValueAsBit("isMoveImm"); 445 isMoveReg = R->getValueAsBit("isMoveReg"); 446 isBitcast = R->getValueAsBit("isBitcast"); 447 isSelect = R->getValueAsBit("isSelect"); 448 isBarrier = R->getValueAsBit("isBarrier"); 449 isCall = R->getValueAsBit("isCall"); 450 isAdd = R->getValueAsBit("isAdd"); 451 isTrap = R->getValueAsBit("isTrap"); 452 canFoldAsLoad = R->getValueAsBit("canFoldAsLoad"); 453 isPredicable = !R->getValueAsBit("isUnpredicable") && 454 (Operands.isPredicable || R->getValueAsBit("isPredicable")); 455 isConvertibleToThreeAddress = R->getValueAsBit("isConvertibleToThreeAddress"); 456 isCommutable = R->getValueAsBit("isCommutable"); 457 isTerminator = R->getValueAsBit("isTerminator"); 458 isReMaterializable = R->getValueAsBit("isReMaterializable"); 459 hasDelaySlot = R->getValueAsBit("hasDelaySlot"); 460 usesCustomInserter = R->getValueAsBit("usesCustomInserter"); 461 hasPostISelHook = R->getValueAsBit("hasPostISelHook"); 462 hasCtrlDep = R->getValueAsBit("hasCtrlDep"); 463 isNotDuplicable = R->getValueAsBit("isNotDuplicable"); 464 isRegSequence = R->getValueAsBit("isRegSequence"); 465 isExtractSubreg = R->getValueAsBit("isExtractSubreg"); 466 isInsertSubreg = R->getValueAsBit("isInsertSubreg"); 467 isConvergent = R->getValueAsBit("isConvergent"); 468 hasNoSchedulingInfo = R->getValueAsBit("hasNoSchedulingInfo"); 469 FastISelShouldIgnore = R->getValueAsBit("FastISelShouldIgnore"); 470 variadicOpsAreDefs = R->getValueAsBit("variadicOpsAreDefs"); 471 isAuthenticated = R->getValueAsBit("isAuthenticated"); 472 473 bool Unset; 474 mayLoad = R->getValueAsBitOrUnset("mayLoad", Unset); 475 mayLoad_Unset = Unset; 476 mayStore = R->getValueAsBitOrUnset("mayStore", Unset); 477 mayStore_Unset = Unset; 478 mayRaiseFPException = R->getValueAsBit("mayRaiseFPException"); 479 hasSideEffects = R->getValueAsBitOrUnset("hasSideEffects", Unset); 480 hasSideEffects_Unset = Unset; 481 482 isAsCheapAsAMove = R->getValueAsBit("isAsCheapAsAMove"); 483 hasExtraSrcRegAllocReq = R->getValueAsBit("hasExtraSrcRegAllocReq"); 484 hasExtraDefRegAllocReq = R->getValueAsBit("hasExtraDefRegAllocReq"); 485 isCodeGenOnly = R->getValueAsBit("isCodeGenOnly"); 486 isPseudo = R->getValueAsBit("isPseudo"); 487 isMeta = R->getValueAsBit("isMeta"); 488 ImplicitDefs = R->getValueAsListOfDefs("Defs"); 489 ImplicitUses = R->getValueAsListOfDefs("Uses"); 490 491 // This flag is only inferred from the pattern. 492 hasChain = false; 493 hasChain_Inferred = false; 494 495 // Parse Constraints. 496 ParseConstraints(R->getValueAsString("Constraints"), Operands, R); 497 498 // Parse the DisableEncoding field. 499 Operands.ProcessDisableEncoding(R->getValueAsString("DisableEncoding")); 500 501 // First check for a ComplexDeprecationPredicate. 502 if (R->getValue("ComplexDeprecationPredicate")) { 503 HasComplexDeprecationPredicate = true; 504 DeprecatedReason = 505 std::string(R->getValueAsString("ComplexDeprecationPredicate")); 506 } else if (const RecordVal *Dep = R->getValue("DeprecatedFeatureMask")) { 507 // Check if we have a Subtarget feature mask. 508 HasComplexDeprecationPredicate = false; 509 DeprecatedReason = Dep->getValue()->getAsString(); 510 } else { 511 // This instruction isn't deprecated. 512 HasComplexDeprecationPredicate = false; 513 DeprecatedReason = ""; 514 } 515 } 516 517 /// HasOneImplicitDefWithKnownVT - If the instruction has at least one 518 /// implicit def and it has a known VT, return the VT, otherwise return 519 /// MVT::Other. 520 MVT::SimpleValueType CodeGenInstruction::HasOneImplicitDefWithKnownVT( 521 const CodeGenTarget &TargetInfo) const { 522 if (ImplicitDefs.empty()) 523 return MVT::Other; 524 525 // Check to see if the first implicit def has a resolvable type. 526 const Record *FirstImplicitDef = ImplicitDefs[0]; 527 assert(FirstImplicitDef->isSubClassOf("Register")); 528 const std::vector<ValueTypeByHwMode> &RegVTs = 529 TargetInfo.getRegisterVTs(FirstImplicitDef); 530 if (RegVTs.size() == 1 && RegVTs[0].isSimple()) 531 return RegVTs[0].getSimple().SimpleTy; 532 return MVT::Other; 533 } 534 535 /// FlattenAsmStringVariants - Flatten the specified AsmString to only 536 /// include text from the specified variant, returning the new string. 537 std::string CodeGenInstruction::FlattenAsmStringVariants(StringRef Cur, 538 unsigned Variant) { 539 std::string Res; 540 541 for (;;) { 542 // Find the start of the next variant string. 543 size_t VariantsStart = 0; 544 for (size_t e = Cur.size(); VariantsStart != e; ++VariantsStart) 545 if (Cur[VariantsStart] == '{' && 546 (VariantsStart == 0 || 547 (Cur[VariantsStart - 1] != '$' && Cur[VariantsStart - 1] != '\\'))) 548 break; 549 550 // Add the prefix to the result. 551 Res += Cur.slice(0, VariantsStart); 552 if (VariantsStart == Cur.size()) 553 break; 554 555 ++VariantsStart; // Skip the '{'. 556 557 // Scan to the end of the variants string. 558 size_t VariantsEnd = VariantsStart; 559 unsigned NestedBraces = 1; 560 for (size_t e = Cur.size(); VariantsEnd != e; ++VariantsEnd) { 561 if (Cur[VariantsEnd] == '}' && Cur[VariantsEnd - 1] != '\\') { 562 if (--NestedBraces == 0) 563 break; 564 } else if (Cur[VariantsEnd] == '{') 565 ++NestedBraces; 566 } 567 568 // Select the Nth variant (or empty). 569 StringRef Selection = 570 Cur.substr(VariantsStart, VariantsEnd - VariantsStart); 571 for (unsigned i = 0; i != Variant; ++i) 572 Selection = Selection.split('|').second; 573 Res += Selection.split('|').first; 574 575 assert(VariantsEnd != Cur.size() && 576 "Unterminated variants in assembly string!"); 577 Cur = Cur.substr(VariantsEnd + 1); 578 } 579 580 return Res; 581 } 582 583 bool CodeGenInstruction::isOperandImpl(StringRef OpListName, unsigned i, 584 StringRef PropertyName) const { 585 const DagInit *ConstraintList = TheDef->getValueAsDag(OpListName); 586 if (!ConstraintList || i >= ConstraintList->getNumArgs()) 587 return false; 588 589 const DefInit *Constraint = dyn_cast<DefInit>(ConstraintList->getArg(i)); 590 if (!Constraint) 591 return false; 592 593 return Constraint->getDef()->isSubClassOf("TypedOperand") && 594 Constraint->getDef()->getValueAsBit(PropertyName); 595 } 596