CodeGenInstruction.cpp revision 360784
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> 21using namespace llvm; 22 23//===----------------------------------------------------------------------===// 24// CGIOperandList Implementation 25//===----------------------------------------------------------------------===// 26 27CGIOperandList::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/// 151unsigned 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. 163bool 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 173std::pair<unsigned,unsigned> 174CGIOperandList::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 228static 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 321static 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 339void 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 361CodeGenInstruction::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 isAuthenticated = R->getValueAsBit("isAuthenticated"); 400 401 bool Unset; 402 mayLoad = R->getValueAsBitOrUnset("mayLoad", Unset); 403 mayLoad_Unset = Unset; 404 mayStore = R->getValueAsBitOrUnset("mayStore", Unset); 405 mayStore_Unset = Unset; 406 mayRaiseFPException = R->getValueAsBit("mayRaiseFPException"); 407 hasSideEffects = R->getValueAsBitOrUnset("hasSideEffects", Unset); 408 hasSideEffects_Unset = Unset; 409 410 isAsCheapAsAMove = R->getValueAsBit("isAsCheapAsAMove"); 411 hasExtraSrcRegAllocReq = R->getValueAsBit("hasExtraSrcRegAllocReq"); 412 hasExtraDefRegAllocReq = R->getValueAsBit("hasExtraDefRegAllocReq"); 413 isCodeGenOnly = R->getValueAsBit("isCodeGenOnly"); 414 isPseudo = R->getValueAsBit("isPseudo"); 415 ImplicitDefs = R->getValueAsListOfDefs("Defs"); 416 ImplicitUses = R->getValueAsListOfDefs("Uses"); 417 418 // This flag is only inferred from the pattern. 419 hasChain = false; 420 hasChain_Inferred = false; 421 422 // Parse Constraints. 423 ParseConstraints(R->getValueAsString("Constraints"), Operands, R); 424 425 // Parse the DisableEncoding field. 426 Operands.ProcessDisableEncoding(R->getValueAsString("DisableEncoding")); 427 428 // First check for a ComplexDeprecationPredicate. 429 if (R->getValue("ComplexDeprecationPredicate")) { 430 HasComplexDeprecationPredicate = true; 431 DeprecatedReason = R->getValueAsString("ComplexDeprecationPredicate"); 432 } else if (RecordVal *Dep = R->getValue("DeprecatedFeatureMask")) { 433 // Check if we have a Subtarget feature mask. 434 HasComplexDeprecationPredicate = false; 435 DeprecatedReason = Dep->getValue()->getAsString(); 436 } else { 437 // This instruction isn't deprecated. 438 HasComplexDeprecationPredicate = false; 439 DeprecatedReason = ""; 440 } 441} 442 443/// HasOneImplicitDefWithKnownVT - If the instruction has at least one 444/// implicit def and it has a known VT, return the VT, otherwise return 445/// MVT::Other. 446MVT::SimpleValueType CodeGenInstruction:: 447HasOneImplicitDefWithKnownVT(const CodeGenTarget &TargetInfo) const { 448 if (ImplicitDefs.empty()) return MVT::Other; 449 450 // Check to see if the first implicit def has a resolvable type. 451 Record *FirstImplicitDef = ImplicitDefs[0]; 452 assert(FirstImplicitDef->isSubClassOf("Register")); 453 const std::vector<ValueTypeByHwMode> &RegVTs = 454 TargetInfo.getRegisterVTs(FirstImplicitDef); 455 if (RegVTs.size() == 1 && RegVTs[0].isSimple()) 456 return RegVTs[0].getSimple().SimpleTy; 457 return MVT::Other; 458} 459 460 461/// FlattenAsmStringVariants - Flatten the specified AsmString to only 462/// include text from the specified variant, returning the new string. 463std::string CodeGenInstruction:: 464FlattenAsmStringVariants(StringRef Cur, unsigned Variant) { 465 std::string Res = ""; 466 467 for (;;) { 468 // Find the start of the next variant string. 469 size_t VariantsStart = 0; 470 for (size_t e = Cur.size(); VariantsStart != e; ++VariantsStart) 471 if (Cur[VariantsStart] == '{' && 472 (VariantsStart == 0 || (Cur[VariantsStart-1] != '$' && 473 Cur[VariantsStart-1] != '\\'))) 474 break; 475 476 // Add the prefix to the result. 477 Res += Cur.slice(0, VariantsStart); 478 if (VariantsStart == Cur.size()) 479 break; 480 481 ++VariantsStart; // Skip the '{'. 482 483 // Scan to the end of the variants string. 484 size_t VariantsEnd = VariantsStart; 485 unsigned NestedBraces = 1; 486 for (size_t e = Cur.size(); VariantsEnd != e; ++VariantsEnd) { 487 if (Cur[VariantsEnd] == '}' && Cur[VariantsEnd-1] != '\\') { 488 if (--NestedBraces == 0) 489 break; 490 } else if (Cur[VariantsEnd] == '{') 491 ++NestedBraces; 492 } 493 494 // Select the Nth variant (or empty). 495 StringRef Selection = Cur.slice(VariantsStart, VariantsEnd); 496 for (unsigned i = 0; i != Variant; ++i) 497 Selection = Selection.split('|').second; 498 Res += Selection.split('|').first; 499 500 assert(VariantsEnd != Cur.size() && 501 "Unterminated variants in assembly string!"); 502 Cur = Cur.substr(VariantsEnd + 1); 503 } 504 505 return Res; 506} 507 508bool CodeGenInstruction::isOperandImpl(unsigned i, 509 StringRef PropertyName) const { 510 DagInit *ConstraintList = TheDef->getValueAsDag("InOperandList"); 511 if (!ConstraintList || i >= ConstraintList->getNumArgs()) 512 return false; 513 514 DefInit *Constraint = dyn_cast<DefInit>(ConstraintList->getArg(i)); 515 if (!Constraint) 516 return false; 517 518 return Constraint->getDef()->isSubClassOf("TypedOperand") && 519 Constraint->getDef()->getValueAsBit(PropertyName); 520} 521 522//===----------------------------------------------------------------------===// 523/// CodeGenInstAlias Implementation 524//===----------------------------------------------------------------------===// 525 526/// tryAliasOpMatch - This is a helper function for the CodeGenInstAlias 527/// constructor. It checks if an argument in an InstAlias pattern matches 528/// the corresponding operand of the instruction. It returns true on a 529/// successful match, with ResOp set to the result operand to be used. 530bool CodeGenInstAlias::tryAliasOpMatch(DagInit *Result, unsigned AliasOpNo, 531 Record *InstOpRec, bool hasSubOps, 532 ArrayRef<SMLoc> Loc, CodeGenTarget &T, 533 ResultOperand &ResOp) { 534 Init *Arg = Result->getArg(AliasOpNo); 535 DefInit *ADI = dyn_cast<DefInit>(Arg); 536 Record *ResultRecord = ADI ? ADI->getDef() : nullptr; 537 538 if (ADI && ADI->getDef() == InstOpRec) { 539 // If the operand is a record, it must have a name, and the record type 540 // must match up with the instruction's argument type. 541 if (!Result->getArgName(AliasOpNo)) 542 PrintFatalError(Loc, "result argument #" + Twine(AliasOpNo) + 543 " must have a name!"); 544 ResOp = ResultOperand(Result->getArgNameStr(AliasOpNo), ResultRecord); 545 return true; 546 } 547 548 // For register operands, the source register class can be a subclass 549 // of the instruction register class, not just an exact match. 550 if (InstOpRec->isSubClassOf("RegisterOperand")) 551 InstOpRec = InstOpRec->getValueAsDef("RegClass"); 552 553 if (ADI && ADI->getDef()->isSubClassOf("RegisterOperand")) 554 ADI = ADI->getDef()->getValueAsDef("RegClass")->getDefInit(); 555 556 if (ADI && ADI->getDef()->isSubClassOf("RegisterClass")) { 557 if (!InstOpRec->isSubClassOf("RegisterClass")) 558 return false; 559 if (!T.getRegisterClass(InstOpRec) 560 .hasSubClass(&T.getRegisterClass(ADI->getDef()))) 561 return false; 562 ResOp = ResultOperand(Result->getArgNameStr(AliasOpNo), ResultRecord); 563 return true; 564 } 565 566 // Handle explicit registers. 567 if (ADI && ADI->getDef()->isSubClassOf("Register")) { 568 if (InstOpRec->isSubClassOf("OptionalDefOperand")) { 569 DagInit *DI = InstOpRec->getValueAsDag("MIOperandInfo"); 570 // The operand info should only have a single (register) entry. We 571 // want the register class of it. 572 InstOpRec = cast<DefInit>(DI->getArg(0))->getDef(); 573 } 574 575 if (!InstOpRec->isSubClassOf("RegisterClass")) 576 return false; 577 578 if (!T.getRegisterClass(InstOpRec) 579 .contains(T.getRegBank().getReg(ADI->getDef()))) 580 PrintFatalError(Loc, "fixed register " + ADI->getDef()->getName() + 581 " is not a member of the " + InstOpRec->getName() + 582 " register class!"); 583 584 if (Result->getArgName(AliasOpNo)) 585 PrintFatalError(Loc, "result fixed register argument must " 586 "not have a name!"); 587 588 ResOp = ResultOperand(ResultRecord); 589 return true; 590 } 591 592 // Handle "zero_reg" for optional def operands. 593 if (ADI && ADI->getDef()->getName() == "zero_reg") { 594 595 // Check if this is an optional def. 596 // Tied operands where the source is a sub-operand of a complex operand 597 // need to represent both operands in the alias destination instruction. 598 // Allow zero_reg for the tied portion. This can and should go away once 599 // the MC representation of things doesn't use tied operands at all. 600 //if (!InstOpRec->isSubClassOf("OptionalDefOperand")) 601 // throw TGError(Loc, "reg0 used for result that is not an " 602 // "OptionalDefOperand!"); 603 604 ResOp = ResultOperand(static_cast<Record*>(nullptr)); 605 return true; 606 } 607 608 // Literal integers. 609 if (IntInit *II = dyn_cast<IntInit>(Arg)) { 610 if (hasSubOps || !InstOpRec->isSubClassOf("Operand")) 611 return false; 612 // Integer arguments can't have names. 613 if (Result->getArgName(AliasOpNo)) 614 PrintFatalError(Loc, "result argument #" + Twine(AliasOpNo) + 615 " must not have a name!"); 616 ResOp = ResultOperand(II->getValue()); 617 return true; 618 } 619 620 // Bits<n> (also used for 0bxx literals) 621 if (BitsInit *BI = dyn_cast<BitsInit>(Arg)) { 622 if (hasSubOps || !InstOpRec->isSubClassOf("Operand")) 623 return false; 624 if (!BI->isComplete()) 625 return false; 626 // Convert the bits init to an integer and use that for the result. 627 IntInit *II = 628 dyn_cast_or_null<IntInit>(BI->convertInitializerTo(IntRecTy::get())); 629 if (!II) 630 return false; 631 ResOp = ResultOperand(II->getValue()); 632 return true; 633 } 634 635 // If both are Operands with the same MVT, allow the conversion. It's 636 // up to the user to make sure the values are appropriate, just like 637 // for isel Pat's. 638 if (InstOpRec->isSubClassOf("Operand") && ADI && 639 ADI->getDef()->isSubClassOf("Operand")) { 640 // FIXME: What other attributes should we check here? Identical 641 // MIOperandInfo perhaps? 642 if (InstOpRec->getValueInit("Type") != ADI->getDef()->getValueInit("Type")) 643 return false; 644 ResOp = ResultOperand(Result->getArgNameStr(AliasOpNo), ADI->getDef()); 645 return true; 646 } 647 648 return false; 649} 650 651unsigned CodeGenInstAlias::ResultOperand::getMINumOperands() const { 652 if (!isRecord()) 653 return 1; 654 655 Record *Rec = getRecord(); 656 if (!Rec->isSubClassOf("Operand")) 657 return 1; 658 659 DagInit *MIOpInfo = Rec->getValueAsDag("MIOperandInfo"); 660 if (MIOpInfo->getNumArgs() == 0) { 661 // Unspecified, so it defaults to 1 662 return 1; 663 } 664 665 return MIOpInfo->getNumArgs(); 666} 667 668CodeGenInstAlias::CodeGenInstAlias(Record *R, CodeGenTarget &T) 669 : TheDef(R) { 670 Result = R->getValueAsDag("ResultInst"); 671 AsmString = R->getValueAsString("AsmString"); 672 673 674 // Verify that the root of the result is an instruction. 675 DefInit *DI = dyn_cast<DefInit>(Result->getOperator()); 676 if (!DI || !DI->getDef()->isSubClassOf("Instruction")) 677 PrintFatalError(R->getLoc(), 678 "result of inst alias should be an instruction"); 679 680 ResultInst = &T.getInstruction(DI->getDef()); 681 682 // NameClass - If argument names are repeated, we need to verify they have 683 // the same class. 684 StringMap<Record*> NameClass; 685 for (unsigned i = 0, e = Result->getNumArgs(); i != e; ++i) { 686 DefInit *ADI = dyn_cast<DefInit>(Result->getArg(i)); 687 if (!ADI || !Result->getArgName(i)) 688 continue; 689 // Verify we don't have something like: (someinst GR16:$foo, GR32:$foo) 690 // $foo can exist multiple times in the result list, but it must have the 691 // same type. 692 Record *&Entry = NameClass[Result->getArgNameStr(i)]; 693 if (Entry && Entry != ADI->getDef()) 694 PrintFatalError(R->getLoc(), "result value $" + Result->getArgNameStr(i) + 695 " is both " + Entry->getName() + " and " + 696 ADI->getDef()->getName() + "!"); 697 Entry = ADI->getDef(); 698 } 699 700 // Decode and validate the arguments of the result. 701 unsigned AliasOpNo = 0; 702 for (unsigned i = 0, e = ResultInst->Operands.size(); i != e; ++i) { 703 704 // Tied registers don't have an entry in the result dag unless they're part 705 // of a complex operand, in which case we include them anyways, as we 706 // don't have any other way to specify the whole operand. 707 if (ResultInst->Operands[i].MINumOperands == 1 && 708 ResultInst->Operands[i].getTiedRegister() != -1) { 709 // Tied operands of different RegisterClass should be explicit within an 710 // instruction's syntax and so cannot be skipped. 711 int TiedOpNum = ResultInst->Operands[i].getTiedRegister(); 712 if (ResultInst->Operands[i].Rec->getName() == 713 ResultInst->Operands[TiedOpNum].Rec->getName()) 714 continue; 715 } 716 717 if (AliasOpNo >= Result->getNumArgs()) 718 PrintFatalError(R->getLoc(), "not enough arguments for instruction!"); 719 720 Record *InstOpRec = ResultInst->Operands[i].Rec; 721 unsigned NumSubOps = ResultInst->Operands[i].MINumOperands; 722 ResultOperand ResOp(static_cast<int64_t>(0)); 723 if (tryAliasOpMatch(Result, AliasOpNo, InstOpRec, (NumSubOps > 1), 724 R->getLoc(), T, ResOp)) { 725 // If this is a simple operand, or a complex operand with a custom match 726 // class, then we can match is verbatim. 727 if (NumSubOps == 1 || 728 (InstOpRec->getValue("ParserMatchClass") && 729 InstOpRec->getValueAsDef("ParserMatchClass") 730 ->getValueAsString("Name") != "Imm")) { 731 ResultOperands.push_back(ResOp); 732 ResultInstOperandIndex.push_back(std::make_pair(i, -1)); 733 ++AliasOpNo; 734 735 // Otherwise, we need to match each of the suboperands individually. 736 } else { 737 DagInit *MIOI = ResultInst->Operands[i].MIOperandInfo; 738 for (unsigned SubOp = 0; SubOp != NumSubOps; ++SubOp) { 739 Record *SubRec = cast<DefInit>(MIOI->getArg(SubOp))->getDef(); 740 741 // Take care to instantiate each of the suboperands with the correct 742 // nomenclature: $foo.bar 743 ResultOperands.emplace_back( 744 Result->getArgName(AliasOpNo)->getAsUnquotedString() + "." + 745 MIOI->getArgName(SubOp)->getAsUnquotedString(), SubRec); 746 ResultInstOperandIndex.push_back(std::make_pair(i, SubOp)); 747 } 748 ++AliasOpNo; 749 } 750 continue; 751 } 752 753 // If the argument did not match the instruction operand, and the operand 754 // is composed of multiple suboperands, try matching the suboperands. 755 if (NumSubOps > 1) { 756 DagInit *MIOI = ResultInst->Operands[i].MIOperandInfo; 757 for (unsigned SubOp = 0; SubOp != NumSubOps; ++SubOp) { 758 if (AliasOpNo >= Result->getNumArgs()) 759 PrintFatalError(R->getLoc(), "not enough arguments for instruction!"); 760 Record *SubRec = cast<DefInit>(MIOI->getArg(SubOp))->getDef(); 761 if (tryAliasOpMatch(Result, AliasOpNo, SubRec, false, 762 R->getLoc(), T, ResOp)) { 763 ResultOperands.push_back(ResOp); 764 ResultInstOperandIndex.push_back(std::make_pair(i, SubOp)); 765 ++AliasOpNo; 766 } else { 767 PrintFatalError(R->getLoc(), "result argument #" + Twine(AliasOpNo) + 768 " does not match instruction operand class " + 769 (SubOp == 0 ? InstOpRec->getName() :SubRec->getName())); 770 } 771 } 772 continue; 773 } 774 PrintFatalError(R->getLoc(), "result argument #" + Twine(AliasOpNo) + 775 " does not match instruction operand class " + 776 InstOpRec->getName()); 777 } 778 779 if (AliasOpNo != Result->getNumArgs()) 780 PrintFatalError(R->getLoc(), "too many operands for instruction!"); 781} 782