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