1 //===- MIParser.cpp - Machine instructions parser implementation ----------===// 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 parsing of machine instructions. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "MIParser.h" 15 #include "MILexer.h" 16 #include "llvm/ADT/StringMap.h" 17 #include "llvm/AsmParser/Parser.h" 18 #include "llvm/AsmParser/SlotMapping.h" 19 #include "llvm/CodeGen/MachineBasicBlock.h" 20 #include "llvm/CodeGen/MachineFunction.h" 21 #include "llvm/CodeGen/MachineFrameInfo.h" 22 #include "llvm/CodeGen/MachineInstr.h" 23 #include "llvm/CodeGen/MachineInstrBuilder.h" 24 #include "llvm/CodeGen/MachineMemOperand.h" 25 #include "llvm/CodeGen/MachineModuleInfo.h" 26 #include "llvm/IR/Instructions.h" 27 #include "llvm/IR/Constants.h" 28 #include "llvm/IR/Module.h" 29 #include "llvm/IR/ModuleSlotTracker.h" 30 #include "llvm/IR/ValueSymbolTable.h" 31 #include "llvm/Support/raw_ostream.h" 32 #include "llvm/Support/SourceMgr.h" 33 #include "llvm/Target/TargetSubtargetInfo.h" 34 #include "llvm/Target/TargetInstrInfo.h" 35 36 using namespace llvm; 37 38 namespace { 39 40 /// A wrapper struct around the 'MachineOperand' struct that includes a source 41 /// range and other attributes. 42 struct ParsedMachineOperand { 43 MachineOperand Operand; 44 StringRef::iterator Begin; 45 StringRef::iterator End; 46 Optional<unsigned> TiedDefIdx; 47 48 ParsedMachineOperand(const MachineOperand &Operand, StringRef::iterator Begin, 49 StringRef::iterator End, Optional<unsigned> &TiedDefIdx) 50 : Operand(Operand), Begin(Begin), End(End), TiedDefIdx(TiedDefIdx) { 51 if (TiedDefIdx) 52 assert(Operand.isReg() && Operand.isUse() && 53 "Only used register operands can be tied"); 54 } 55 }; 56 57 class MIParser { 58 SourceMgr &SM; 59 MachineFunction &MF; 60 SMDiagnostic &Error; 61 StringRef Source, CurrentSource; 62 MIToken Token; 63 const PerFunctionMIParsingState &PFS; 64 /// Maps from indices to unnamed global values and metadata nodes. 65 const SlotMapping &IRSlots; 66 /// Maps from instruction names to op codes. 67 StringMap<unsigned> Names2InstrOpCodes; 68 /// Maps from register names to registers. 69 StringMap<unsigned> Names2Regs; 70 /// Maps from register mask names to register masks. 71 StringMap<const uint32_t *> Names2RegMasks; 72 /// Maps from subregister names to subregister indices. 73 StringMap<unsigned> Names2SubRegIndices; 74 /// Maps from slot numbers to function's unnamed basic blocks. 75 DenseMap<unsigned, const BasicBlock *> Slots2BasicBlocks; 76 /// Maps from slot numbers to function's unnamed values. 77 DenseMap<unsigned, const Value *> Slots2Values; 78 /// Maps from target index names to target indices. 79 StringMap<int> Names2TargetIndices; 80 /// Maps from direct target flag names to the direct target flag values. 81 StringMap<unsigned> Names2DirectTargetFlags; 82 /// Maps from direct target flag names to the bitmask target flag values. 83 StringMap<unsigned> Names2BitmaskTargetFlags; 84 85 public: 86 MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error, 87 StringRef Source, const PerFunctionMIParsingState &PFS, 88 const SlotMapping &IRSlots); 89 90 void lex(); 91 92 /// Report an error at the current location with the given message. 93 /// 94 /// This function always return true. 95 bool error(const Twine &Msg); 96 97 /// Report an error at the given location with the given message. 98 /// 99 /// This function always return true. 100 bool error(StringRef::iterator Loc, const Twine &Msg); 101 102 bool 103 parseBasicBlockDefinitions(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots); 104 bool parseBasicBlocks(); 105 bool parse(MachineInstr *&MI); 106 bool parseStandaloneMBB(MachineBasicBlock *&MBB); 107 bool parseStandaloneNamedRegister(unsigned &Reg); 108 bool parseStandaloneVirtualRegister(unsigned &Reg); 109 bool parseStandaloneStackObject(int &FI); 110 bool parseStandaloneMDNode(MDNode *&Node); 111 112 bool 113 parseBasicBlockDefinition(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots); 114 bool parseBasicBlock(MachineBasicBlock &MBB); 115 bool parseBasicBlockLiveins(MachineBasicBlock &MBB); 116 bool parseBasicBlockSuccessors(MachineBasicBlock &MBB); 117 118 bool parseRegister(unsigned &Reg); 119 bool parseRegisterFlag(unsigned &Flags); 120 bool parseSubRegisterIndex(unsigned &SubReg); 121 bool parseRegisterTiedDefIndex(unsigned &TiedDefIdx); 122 bool parseRegisterOperand(MachineOperand &Dest, 123 Optional<unsigned> &TiedDefIdx, bool IsDef = false); 124 bool parseImmediateOperand(MachineOperand &Dest); 125 bool parseIRConstant(StringRef::iterator Loc, StringRef Source, 126 const Constant *&C); 127 bool parseIRConstant(StringRef::iterator Loc, const Constant *&C); 128 bool parseTypedImmediateOperand(MachineOperand &Dest); 129 bool parseFPImmediateOperand(MachineOperand &Dest); 130 bool parseMBBReference(MachineBasicBlock *&MBB); 131 bool parseMBBOperand(MachineOperand &Dest); 132 bool parseStackFrameIndex(int &FI); 133 bool parseStackObjectOperand(MachineOperand &Dest); 134 bool parseFixedStackFrameIndex(int &FI); 135 bool parseFixedStackObjectOperand(MachineOperand &Dest); 136 bool parseGlobalValue(GlobalValue *&GV); 137 bool parseGlobalAddressOperand(MachineOperand &Dest); 138 bool parseConstantPoolIndexOperand(MachineOperand &Dest); 139 bool parseJumpTableIndexOperand(MachineOperand &Dest); 140 bool parseExternalSymbolOperand(MachineOperand &Dest); 141 bool parseMDNode(MDNode *&Node); 142 bool parseMetadataOperand(MachineOperand &Dest); 143 bool parseCFIOffset(int &Offset); 144 bool parseCFIRegister(unsigned &Reg); 145 bool parseCFIOperand(MachineOperand &Dest); 146 bool parseIRBlock(BasicBlock *&BB, const Function &F); 147 bool parseBlockAddressOperand(MachineOperand &Dest); 148 bool parseTargetIndexOperand(MachineOperand &Dest); 149 bool parseLiveoutRegisterMaskOperand(MachineOperand &Dest); 150 bool parseMachineOperand(MachineOperand &Dest, 151 Optional<unsigned> &TiedDefIdx); 152 bool parseMachineOperandAndTargetFlags(MachineOperand &Dest, 153 Optional<unsigned> &TiedDefIdx); 154 bool parseOffset(int64_t &Offset); 155 bool parseAlignment(unsigned &Alignment); 156 bool parseOperandsOffset(MachineOperand &Op); 157 bool parseIRValue(const Value *&V); 158 bool parseMemoryOperandFlag(unsigned &Flags); 159 bool parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV); 160 bool parseMachinePointerInfo(MachinePointerInfo &Dest); 161 bool parseMachineMemoryOperand(MachineMemOperand *&Dest); 162 163 private: 164 /// Convert the integer literal in the current token into an unsigned integer. 165 /// 166 /// Return true if an error occurred. 167 bool getUnsigned(unsigned &Result); 168 169 /// Convert the integer literal in the current token into an uint64. 170 /// 171 /// Return true if an error occurred. 172 bool getUint64(uint64_t &Result); 173 174 /// If the current token is of the given kind, consume it and return false. 175 /// Otherwise report an error and return true. 176 bool expectAndConsume(MIToken::TokenKind TokenKind); 177 178 /// If the current token is of the given kind, consume it and return true. 179 /// Otherwise return false. 180 bool consumeIfPresent(MIToken::TokenKind TokenKind); 181 182 void initNames2InstrOpCodes(); 183 184 /// Try to convert an instruction name to an opcode. Return true if the 185 /// instruction name is invalid. 186 bool parseInstrName(StringRef InstrName, unsigned &OpCode); 187 188 bool parseInstruction(unsigned &OpCode, unsigned &Flags); 189 190 bool assignRegisterTies(MachineInstr &MI, 191 ArrayRef<ParsedMachineOperand> Operands); 192 193 bool verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands, 194 const MCInstrDesc &MCID); 195 196 void initNames2Regs(); 197 198 /// Try to convert a register name to a register number. Return true if the 199 /// register name is invalid. 200 bool getRegisterByName(StringRef RegName, unsigned &Reg); 201 202 void initNames2RegMasks(); 203 204 /// Check if the given identifier is a name of a register mask. 205 /// 206 /// Return null if the identifier isn't a register mask. 207 const uint32_t *getRegMask(StringRef Identifier); 208 209 void initNames2SubRegIndices(); 210 211 /// Check if the given identifier is a name of a subregister index. 212 /// 213 /// Return 0 if the name isn't a subregister index class. 214 unsigned getSubRegIndex(StringRef Name); 215 216 const BasicBlock *getIRBlock(unsigned Slot); 217 const BasicBlock *getIRBlock(unsigned Slot, const Function &F); 218 219 const Value *getIRValue(unsigned Slot); 220 221 void initNames2TargetIndices(); 222 223 /// Try to convert a name of target index to the corresponding target index. 224 /// 225 /// Return true if the name isn't a name of a target index. 226 bool getTargetIndex(StringRef Name, int &Index); 227 228 void initNames2DirectTargetFlags(); 229 230 /// Try to convert a name of a direct target flag to the corresponding 231 /// target flag. 232 /// 233 /// Return true if the name isn't a name of a direct flag. 234 bool getDirectTargetFlag(StringRef Name, unsigned &Flag); 235 236 void initNames2BitmaskTargetFlags(); 237 238 /// Try to convert a name of a bitmask target flag to the corresponding 239 /// target flag. 240 /// 241 /// Return true if the name isn't a name of a bitmask target flag. 242 bool getBitmaskTargetFlag(StringRef Name, unsigned &Flag); 243 }; 244 245 } // end anonymous namespace 246 247 MIParser::MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error, 248 StringRef Source, const PerFunctionMIParsingState &PFS, 249 const SlotMapping &IRSlots) 250 : SM(SM), MF(MF), Error(Error), Source(Source), CurrentSource(Source), 251 PFS(PFS), IRSlots(IRSlots) {} 252 253 void MIParser::lex() { 254 CurrentSource = lexMIToken( 255 CurrentSource, Token, 256 [this](StringRef::iterator Loc, const Twine &Msg) { error(Loc, Msg); }); 257 } 258 259 bool MIParser::error(const Twine &Msg) { return error(Token.location(), Msg); } 260 261 bool MIParser::error(StringRef::iterator Loc, const Twine &Msg) { 262 assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size())); 263 const MemoryBuffer &Buffer = *SM.getMemoryBuffer(SM.getMainFileID()); 264 if (Loc >= Buffer.getBufferStart() && Loc <= Buffer.getBufferEnd()) { 265 // Create an ordinary diagnostic when the source manager's buffer is the 266 // source string. 267 Error = SM.GetMessage(SMLoc::getFromPointer(Loc), SourceMgr::DK_Error, Msg); 268 return true; 269 } 270 // Create a diagnostic for a YAML string literal. 271 Error = SMDiagnostic(SM, SMLoc(), Buffer.getBufferIdentifier(), 1, 272 Loc - Source.data(), SourceMgr::DK_Error, Msg.str(), 273 Source, None, None); 274 return true; 275 } 276 277 static const char *toString(MIToken::TokenKind TokenKind) { 278 switch (TokenKind) { 279 case MIToken::comma: 280 return "','"; 281 case MIToken::equal: 282 return "'='"; 283 case MIToken::colon: 284 return "':'"; 285 case MIToken::lparen: 286 return "'('"; 287 case MIToken::rparen: 288 return "')'"; 289 default: 290 return "<unknown token>"; 291 } 292 } 293 294 bool MIParser::expectAndConsume(MIToken::TokenKind TokenKind) { 295 if (Token.isNot(TokenKind)) 296 return error(Twine("expected ") + toString(TokenKind)); 297 lex(); 298 return false; 299 } 300 301 bool MIParser::consumeIfPresent(MIToken::TokenKind TokenKind) { 302 if (Token.isNot(TokenKind)) 303 return false; 304 lex(); 305 return true; 306 } 307 308 bool MIParser::parseBasicBlockDefinition( 309 DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) { 310 assert(Token.is(MIToken::MachineBasicBlockLabel)); 311 unsigned ID = 0; 312 if (getUnsigned(ID)) 313 return true; 314 auto Loc = Token.location(); 315 auto Name = Token.stringValue(); 316 lex(); 317 bool HasAddressTaken = false; 318 bool IsLandingPad = false; 319 unsigned Alignment = 0; 320 BasicBlock *BB = nullptr; 321 if (consumeIfPresent(MIToken::lparen)) { 322 do { 323 // TODO: Report an error when multiple same attributes are specified. 324 switch (Token.kind()) { 325 case MIToken::kw_address_taken: 326 HasAddressTaken = true; 327 lex(); 328 break; 329 case MIToken::kw_landing_pad: 330 IsLandingPad = true; 331 lex(); 332 break; 333 case MIToken::kw_align: 334 if (parseAlignment(Alignment)) 335 return true; 336 break; 337 case MIToken::IRBlock: 338 // TODO: Report an error when both name and ir block are specified. 339 if (parseIRBlock(BB, *MF.getFunction())) 340 return true; 341 lex(); 342 break; 343 default: 344 break; 345 } 346 } while (consumeIfPresent(MIToken::comma)); 347 if (expectAndConsume(MIToken::rparen)) 348 return true; 349 } 350 if (expectAndConsume(MIToken::colon)) 351 return true; 352 353 if (!Name.empty()) { 354 BB = dyn_cast_or_null<BasicBlock>( 355 MF.getFunction()->getValueSymbolTable().lookup(Name)); 356 if (!BB) 357 return error(Loc, Twine("basic block '") + Name + 358 "' is not defined in the function '" + 359 MF.getName() + "'"); 360 } 361 auto *MBB = MF.CreateMachineBasicBlock(BB); 362 MF.insert(MF.end(), MBB); 363 bool WasInserted = MBBSlots.insert(std::make_pair(ID, MBB)).second; 364 if (!WasInserted) 365 return error(Loc, Twine("redefinition of machine basic block with id #") + 366 Twine(ID)); 367 if (Alignment) 368 MBB->setAlignment(Alignment); 369 if (HasAddressTaken) 370 MBB->setHasAddressTaken(); 371 MBB->setIsEHPad(IsLandingPad); 372 return false; 373 } 374 375 bool MIParser::parseBasicBlockDefinitions( 376 DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) { 377 lex(); 378 // Skip until the first machine basic block. 379 while (Token.is(MIToken::Newline)) 380 lex(); 381 if (Token.isErrorOrEOF()) 382 return Token.isError(); 383 if (Token.isNot(MIToken::MachineBasicBlockLabel)) 384 return error("expected a basic block definition before instructions"); 385 unsigned BraceDepth = 0; 386 do { 387 if (parseBasicBlockDefinition(MBBSlots)) 388 return true; 389 bool IsAfterNewline = false; 390 // Skip until the next machine basic block. 391 while (true) { 392 if ((Token.is(MIToken::MachineBasicBlockLabel) && IsAfterNewline) || 393 Token.isErrorOrEOF()) 394 break; 395 else if (Token.is(MIToken::MachineBasicBlockLabel)) 396 return error("basic block definition should be located at the start of " 397 "the line"); 398 else if (consumeIfPresent(MIToken::Newline)) { 399 IsAfterNewline = true; 400 continue; 401 } 402 IsAfterNewline = false; 403 if (Token.is(MIToken::lbrace)) 404 ++BraceDepth; 405 if (Token.is(MIToken::rbrace)) { 406 if (!BraceDepth) 407 return error("extraneous closing brace ('}')"); 408 --BraceDepth; 409 } 410 lex(); 411 } 412 // Verify that we closed all of the '{' at the end of a file or a block. 413 if (!Token.isError() && BraceDepth) 414 return error("expected '}'"); // FIXME: Report a note that shows '{'. 415 } while (!Token.isErrorOrEOF()); 416 return Token.isError(); 417 } 418 419 bool MIParser::parseBasicBlockLiveins(MachineBasicBlock &MBB) { 420 assert(Token.is(MIToken::kw_liveins)); 421 lex(); 422 if (expectAndConsume(MIToken::colon)) 423 return true; 424 if (Token.isNewlineOrEOF()) // Allow an empty list of liveins. 425 return false; 426 do { 427 if (Token.isNot(MIToken::NamedRegister)) 428 return error("expected a named register"); 429 unsigned Reg = 0; 430 if (parseRegister(Reg)) 431 return true; 432 MBB.addLiveIn(Reg); 433 lex(); 434 } while (consumeIfPresent(MIToken::comma)); 435 return false; 436 } 437 438 bool MIParser::parseBasicBlockSuccessors(MachineBasicBlock &MBB) { 439 assert(Token.is(MIToken::kw_successors)); 440 lex(); 441 if (expectAndConsume(MIToken::colon)) 442 return true; 443 if (Token.isNewlineOrEOF()) // Allow an empty list of successors. 444 return false; 445 do { 446 if (Token.isNot(MIToken::MachineBasicBlock)) 447 return error("expected a machine basic block reference"); 448 MachineBasicBlock *SuccMBB = nullptr; 449 if (parseMBBReference(SuccMBB)) 450 return true; 451 lex(); 452 unsigned Weight = 0; 453 if (consumeIfPresent(MIToken::lparen)) { 454 if (Token.isNot(MIToken::IntegerLiteral)) 455 return error("expected an integer literal after '('"); 456 if (getUnsigned(Weight)) 457 return true; 458 lex(); 459 if (expectAndConsume(MIToken::rparen)) 460 return true; 461 } 462 MBB.addSuccessor(SuccMBB, BranchProbability::getRaw(Weight)); 463 } while (consumeIfPresent(MIToken::comma)); 464 MBB.normalizeSuccProbs(); 465 return false; 466 } 467 468 bool MIParser::parseBasicBlock(MachineBasicBlock &MBB) { 469 // Skip the definition. 470 assert(Token.is(MIToken::MachineBasicBlockLabel)); 471 lex(); 472 if (consumeIfPresent(MIToken::lparen)) { 473 while (Token.isNot(MIToken::rparen) && !Token.isErrorOrEOF()) 474 lex(); 475 consumeIfPresent(MIToken::rparen); 476 } 477 consumeIfPresent(MIToken::colon); 478 479 // Parse the liveins and successors. 480 // N.B: Multiple lists of successors and liveins are allowed and they're 481 // merged into one. 482 // Example: 483 // liveins: %edi 484 // liveins: %esi 485 // 486 // is equivalent to 487 // liveins: %edi, %esi 488 while (true) { 489 if (Token.is(MIToken::kw_successors)) { 490 if (parseBasicBlockSuccessors(MBB)) 491 return true; 492 } else if (Token.is(MIToken::kw_liveins)) { 493 if (parseBasicBlockLiveins(MBB)) 494 return true; 495 } else if (consumeIfPresent(MIToken::Newline)) { 496 continue; 497 } else 498 break; 499 if (!Token.isNewlineOrEOF()) 500 return error("expected line break at the end of a list"); 501 lex(); 502 } 503 504 // Parse the instructions. 505 bool IsInBundle = false; 506 MachineInstr *PrevMI = nullptr; 507 while (true) { 508 if (Token.is(MIToken::MachineBasicBlockLabel) || Token.is(MIToken::Eof)) 509 return false; 510 else if (consumeIfPresent(MIToken::Newline)) 511 continue; 512 if (consumeIfPresent(MIToken::rbrace)) { 513 // The first parsing pass should verify that all closing '}' have an 514 // opening '{'. 515 assert(IsInBundle); 516 IsInBundle = false; 517 continue; 518 } 519 MachineInstr *MI = nullptr; 520 if (parse(MI)) 521 return true; 522 MBB.insert(MBB.end(), MI); 523 if (IsInBundle) { 524 PrevMI->setFlag(MachineInstr::BundledSucc); 525 MI->setFlag(MachineInstr::BundledPred); 526 } 527 PrevMI = MI; 528 if (Token.is(MIToken::lbrace)) { 529 if (IsInBundle) 530 return error("nested instruction bundles are not allowed"); 531 lex(); 532 // This instruction is the start of the bundle. 533 MI->setFlag(MachineInstr::BundledSucc); 534 IsInBundle = true; 535 if (!Token.is(MIToken::Newline)) 536 // The next instruction can be on the same line. 537 continue; 538 } 539 assert(Token.isNewlineOrEOF() && "MI is not fully parsed"); 540 lex(); 541 } 542 return false; 543 } 544 545 bool MIParser::parseBasicBlocks() { 546 lex(); 547 // Skip until the first machine basic block. 548 while (Token.is(MIToken::Newline)) 549 lex(); 550 if (Token.isErrorOrEOF()) 551 return Token.isError(); 552 // The first parsing pass should have verified that this token is a MBB label 553 // in the 'parseBasicBlockDefinitions' method. 554 assert(Token.is(MIToken::MachineBasicBlockLabel)); 555 do { 556 MachineBasicBlock *MBB = nullptr; 557 if (parseMBBReference(MBB)) 558 return true; 559 if (parseBasicBlock(*MBB)) 560 return true; 561 // The method 'parseBasicBlock' should parse the whole block until the next 562 // block or the end of file. 563 assert(Token.is(MIToken::MachineBasicBlockLabel) || Token.is(MIToken::Eof)); 564 } while (Token.isNot(MIToken::Eof)); 565 return false; 566 } 567 568 bool MIParser::parse(MachineInstr *&MI) { 569 // Parse any register operands before '=' 570 MachineOperand MO = MachineOperand::CreateImm(0); 571 SmallVector<ParsedMachineOperand, 8> Operands; 572 while (Token.isRegister() || Token.isRegisterFlag()) { 573 auto Loc = Token.location(); 574 Optional<unsigned> TiedDefIdx; 575 if (parseRegisterOperand(MO, TiedDefIdx, /*IsDef=*/true)) 576 return true; 577 Operands.push_back( 578 ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx)); 579 if (Token.isNot(MIToken::comma)) 580 break; 581 lex(); 582 } 583 if (!Operands.empty() && expectAndConsume(MIToken::equal)) 584 return true; 585 586 unsigned OpCode, Flags = 0; 587 if (Token.isError() || parseInstruction(OpCode, Flags)) 588 return true; 589 590 // Parse the remaining machine operands. 591 while (!Token.isNewlineOrEOF() && Token.isNot(MIToken::kw_debug_location) && 592 Token.isNot(MIToken::coloncolon) && Token.isNot(MIToken::lbrace)) { 593 auto Loc = Token.location(); 594 Optional<unsigned> TiedDefIdx; 595 if (parseMachineOperandAndTargetFlags(MO, TiedDefIdx)) 596 return true; 597 Operands.push_back( 598 ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx)); 599 if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) || 600 Token.is(MIToken::lbrace)) 601 break; 602 if (Token.isNot(MIToken::comma)) 603 return error("expected ',' before the next machine operand"); 604 lex(); 605 } 606 607 DebugLoc DebugLocation; 608 if (Token.is(MIToken::kw_debug_location)) { 609 lex(); 610 if (Token.isNot(MIToken::exclaim)) 611 return error("expected a metadata node after 'debug-location'"); 612 MDNode *Node = nullptr; 613 if (parseMDNode(Node)) 614 return true; 615 DebugLocation = DebugLoc(Node); 616 } 617 618 // Parse the machine memory operands. 619 SmallVector<MachineMemOperand *, 2> MemOperands; 620 if (Token.is(MIToken::coloncolon)) { 621 lex(); 622 while (!Token.isNewlineOrEOF()) { 623 MachineMemOperand *MemOp = nullptr; 624 if (parseMachineMemoryOperand(MemOp)) 625 return true; 626 MemOperands.push_back(MemOp); 627 if (Token.isNewlineOrEOF()) 628 break; 629 if (Token.isNot(MIToken::comma)) 630 return error("expected ',' before the next machine memory operand"); 631 lex(); 632 } 633 } 634 635 const auto &MCID = MF.getSubtarget().getInstrInfo()->get(OpCode); 636 if (!MCID.isVariadic()) { 637 // FIXME: Move the implicit operand verification to the machine verifier. 638 if (verifyImplicitOperands(Operands, MCID)) 639 return true; 640 } 641 642 // TODO: Check for extraneous machine operands. 643 MI = MF.CreateMachineInstr(MCID, DebugLocation, /*NoImplicit=*/true); 644 MI->setFlags(Flags); 645 for (const auto &Operand : Operands) 646 MI->addOperand(MF, Operand.Operand); 647 if (assignRegisterTies(*MI, Operands)) 648 return true; 649 if (MemOperands.empty()) 650 return false; 651 MachineInstr::mmo_iterator MemRefs = 652 MF.allocateMemRefsArray(MemOperands.size()); 653 std::copy(MemOperands.begin(), MemOperands.end(), MemRefs); 654 MI->setMemRefs(MemRefs, MemRefs + MemOperands.size()); 655 return false; 656 } 657 658 bool MIParser::parseStandaloneMBB(MachineBasicBlock *&MBB) { 659 lex(); 660 if (Token.isNot(MIToken::MachineBasicBlock)) 661 return error("expected a machine basic block reference"); 662 if (parseMBBReference(MBB)) 663 return true; 664 lex(); 665 if (Token.isNot(MIToken::Eof)) 666 return error( 667 "expected end of string after the machine basic block reference"); 668 return false; 669 } 670 671 bool MIParser::parseStandaloneNamedRegister(unsigned &Reg) { 672 lex(); 673 if (Token.isNot(MIToken::NamedRegister)) 674 return error("expected a named register"); 675 if (parseRegister(Reg)) 676 return true; 677 lex(); 678 if (Token.isNot(MIToken::Eof)) 679 return error("expected end of string after the register reference"); 680 return false; 681 } 682 683 bool MIParser::parseStandaloneVirtualRegister(unsigned &Reg) { 684 lex(); 685 if (Token.isNot(MIToken::VirtualRegister)) 686 return error("expected a virtual register"); 687 if (parseRegister(Reg)) 688 return true; 689 lex(); 690 if (Token.isNot(MIToken::Eof)) 691 return error("expected end of string after the register reference"); 692 return false; 693 } 694 695 bool MIParser::parseStandaloneStackObject(int &FI) { 696 lex(); 697 if (Token.isNot(MIToken::StackObject)) 698 return error("expected a stack object"); 699 if (parseStackFrameIndex(FI)) 700 return true; 701 if (Token.isNot(MIToken::Eof)) 702 return error("expected end of string after the stack object reference"); 703 return false; 704 } 705 706 bool MIParser::parseStandaloneMDNode(MDNode *&Node) { 707 lex(); 708 if (Token.isNot(MIToken::exclaim)) 709 return error("expected a metadata node"); 710 if (parseMDNode(Node)) 711 return true; 712 if (Token.isNot(MIToken::Eof)) 713 return error("expected end of string after the metadata node"); 714 return false; 715 } 716 717 static const char *printImplicitRegisterFlag(const MachineOperand &MO) { 718 assert(MO.isImplicit()); 719 return MO.isDef() ? "implicit-def" : "implicit"; 720 } 721 722 static std::string getRegisterName(const TargetRegisterInfo *TRI, 723 unsigned Reg) { 724 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "expected phys reg"); 725 return StringRef(TRI->getName(Reg)).lower(); 726 } 727 728 /// Return true if the parsed machine operands contain a given machine operand. 729 static bool isImplicitOperandIn(const MachineOperand &ImplicitOperand, 730 ArrayRef<ParsedMachineOperand> Operands) { 731 for (const auto &I : Operands) { 732 if (ImplicitOperand.isIdenticalTo(I.Operand)) 733 return true; 734 } 735 return false; 736 } 737 738 bool MIParser::verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands, 739 const MCInstrDesc &MCID) { 740 if (MCID.isCall()) 741 // We can't verify call instructions as they can contain arbitrary implicit 742 // register and register mask operands. 743 return false; 744 745 // Gather all the expected implicit operands. 746 SmallVector<MachineOperand, 4> ImplicitOperands; 747 if (MCID.ImplicitDefs) 748 for (const MCPhysReg *ImpDefs = MCID.getImplicitDefs(); *ImpDefs; ++ImpDefs) 749 ImplicitOperands.push_back( 750 MachineOperand::CreateReg(*ImpDefs, true, true)); 751 if (MCID.ImplicitUses) 752 for (const MCPhysReg *ImpUses = MCID.getImplicitUses(); *ImpUses; ++ImpUses) 753 ImplicitOperands.push_back( 754 MachineOperand::CreateReg(*ImpUses, false, true)); 755 756 const auto *TRI = MF.getSubtarget().getRegisterInfo(); 757 assert(TRI && "Expected target register info"); 758 for (const auto &I : ImplicitOperands) { 759 if (isImplicitOperandIn(I, Operands)) 760 continue; 761 return error(Operands.empty() ? Token.location() : Operands.back().End, 762 Twine("missing implicit register operand '") + 763 printImplicitRegisterFlag(I) + " %" + 764 getRegisterName(TRI, I.getReg()) + "'"); 765 } 766 return false; 767 } 768 769 bool MIParser::parseInstruction(unsigned &OpCode, unsigned &Flags) { 770 if (Token.is(MIToken::kw_frame_setup)) { 771 Flags |= MachineInstr::FrameSetup; 772 lex(); 773 } 774 if (Token.isNot(MIToken::Identifier)) 775 return error("expected a machine instruction"); 776 StringRef InstrName = Token.stringValue(); 777 if (parseInstrName(InstrName, OpCode)) 778 return error(Twine("unknown machine instruction name '") + InstrName + "'"); 779 lex(); 780 return false; 781 } 782 783 bool MIParser::parseRegister(unsigned &Reg) { 784 switch (Token.kind()) { 785 case MIToken::underscore: 786 Reg = 0; 787 break; 788 case MIToken::NamedRegister: { 789 StringRef Name = Token.stringValue(); 790 if (getRegisterByName(Name, Reg)) 791 return error(Twine("unknown register name '") + Name + "'"); 792 break; 793 } 794 case MIToken::VirtualRegister: { 795 unsigned ID; 796 if (getUnsigned(ID)) 797 return true; 798 const auto RegInfo = PFS.VirtualRegisterSlots.find(ID); 799 if (RegInfo == PFS.VirtualRegisterSlots.end()) 800 return error(Twine("use of undefined virtual register '%") + Twine(ID) + 801 "'"); 802 Reg = RegInfo->second; 803 break; 804 } 805 // TODO: Parse other register kinds. 806 default: 807 llvm_unreachable("The current token should be a register"); 808 } 809 return false; 810 } 811 812 bool MIParser::parseRegisterFlag(unsigned &Flags) { 813 const unsigned OldFlags = Flags; 814 switch (Token.kind()) { 815 case MIToken::kw_implicit: 816 Flags |= RegState::Implicit; 817 break; 818 case MIToken::kw_implicit_define: 819 Flags |= RegState::ImplicitDefine; 820 break; 821 case MIToken::kw_def: 822 Flags |= RegState::Define; 823 break; 824 case MIToken::kw_dead: 825 Flags |= RegState::Dead; 826 break; 827 case MIToken::kw_killed: 828 Flags |= RegState::Kill; 829 break; 830 case MIToken::kw_undef: 831 Flags |= RegState::Undef; 832 break; 833 case MIToken::kw_internal: 834 Flags |= RegState::InternalRead; 835 break; 836 case MIToken::kw_early_clobber: 837 Flags |= RegState::EarlyClobber; 838 break; 839 case MIToken::kw_debug_use: 840 Flags |= RegState::Debug; 841 break; 842 default: 843 llvm_unreachable("The current token should be a register flag"); 844 } 845 if (OldFlags == Flags) 846 // We know that the same flag is specified more than once when the flags 847 // weren't modified. 848 return error("duplicate '" + Token.stringValue() + "' register flag"); 849 lex(); 850 return false; 851 } 852 853 bool MIParser::parseSubRegisterIndex(unsigned &SubReg) { 854 assert(Token.is(MIToken::colon)); 855 lex(); 856 if (Token.isNot(MIToken::Identifier)) 857 return error("expected a subregister index after ':'"); 858 auto Name = Token.stringValue(); 859 SubReg = getSubRegIndex(Name); 860 if (!SubReg) 861 return error(Twine("use of unknown subregister index '") + Name + "'"); 862 lex(); 863 return false; 864 } 865 866 bool MIParser::parseRegisterTiedDefIndex(unsigned &TiedDefIdx) { 867 if (!consumeIfPresent(MIToken::kw_tied_def)) 868 return error("expected 'tied-def' after '('"); 869 if (Token.isNot(MIToken::IntegerLiteral)) 870 return error("expected an integer literal after 'tied-def'"); 871 if (getUnsigned(TiedDefIdx)) 872 return true; 873 lex(); 874 if (expectAndConsume(MIToken::rparen)) 875 return true; 876 return false; 877 } 878 879 bool MIParser::assignRegisterTies(MachineInstr &MI, 880 ArrayRef<ParsedMachineOperand> Operands) { 881 SmallVector<std::pair<unsigned, unsigned>, 4> TiedRegisterPairs; 882 for (unsigned I = 0, E = Operands.size(); I != E; ++I) { 883 if (!Operands[I].TiedDefIdx) 884 continue; 885 // The parser ensures that this operand is a register use, so we just have 886 // to check the tied-def operand. 887 unsigned DefIdx = Operands[I].TiedDefIdx.getValue(); 888 if (DefIdx >= E) 889 return error(Operands[I].Begin, 890 Twine("use of invalid tied-def operand index '" + 891 Twine(DefIdx) + "'; instruction has only ") + 892 Twine(E) + " operands"); 893 const auto &DefOperand = Operands[DefIdx].Operand; 894 if (!DefOperand.isReg() || !DefOperand.isDef()) 895 // FIXME: add note with the def operand. 896 return error(Operands[I].Begin, 897 Twine("use of invalid tied-def operand index '") + 898 Twine(DefIdx) + "'; the operand #" + Twine(DefIdx) + 899 " isn't a defined register"); 900 // Check that the tied-def operand wasn't tied elsewhere. 901 for (const auto &TiedPair : TiedRegisterPairs) { 902 if (TiedPair.first == DefIdx) 903 return error(Operands[I].Begin, 904 Twine("the tied-def operand #") + Twine(DefIdx) + 905 " is already tied with another register operand"); 906 } 907 TiedRegisterPairs.push_back(std::make_pair(DefIdx, I)); 908 } 909 // FIXME: Verify that for non INLINEASM instructions, the def and use tied 910 // indices must be less than tied max. 911 for (const auto &TiedPair : TiedRegisterPairs) 912 MI.tieOperands(TiedPair.first, TiedPair.second); 913 return false; 914 } 915 916 bool MIParser::parseRegisterOperand(MachineOperand &Dest, 917 Optional<unsigned> &TiedDefIdx, 918 bool IsDef) { 919 unsigned Reg; 920 unsigned Flags = IsDef ? RegState::Define : 0; 921 while (Token.isRegisterFlag()) { 922 if (parseRegisterFlag(Flags)) 923 return true; 924 } 925 if (!Token.isRegister()) 926 return error("expected a register after register flags"); 927 if (parseRegister(Reg)) 928 return true; 929 lex(); 930 unsigned SubReg = 0; 931 if (Token.is(MIToken::colon)) { 932 if (parseSubRegisterIndex(SubReg)) 933 return true; 934 } 935 if ((Flags & RegState::Define) == 0 && consumeIfPresent(MIToken::lparen)) { 936 unsigned Idx; 937 if (parseRegisterTiedDefIndex(Idx)) 938 return true; 939 TiedDefIdx = Idx; 940 } 941 Dest = MachineOperand::CreateReg( 942 Reg, Flags & RegState::Define, Flags & RegState::Implicit, 943 Flags & RegState::Kill, Flags & RegState::Dead, Flags & RegState::Undef, 944 Flags & RegState::EarlyClobber, SubReg, Flags & RegState::Debug, 945 Flags & RegState::InternalRead); 946 return false; 947 } 948 949 bool MIParser::parseImmediateOperand(MachineOperand &Dest) { 950 assert(Token.is(MIToken::IntegerLiteral)); 951 const APSInt &Int = Token.integerValue(); 952 if (Int.getMinSignedBits() > 64) 953 return error("integer literal is too large to be an immediate operand"); 954 Dest = MachineOperand::CreateImm(Int.getExtValue()); 955 lex(); 956 return false; 957 } 958 959 bool MIParser::parseIRConstant(StringRef::iterator Loc, StringRef StringValue, 960 const Constant *&C) { 961 auto Source = StringValue.str(); // The source has to be null terminated. 962 SMDiagnostic Err; 963 C = parseConstantValue(Source.c_str(), Err, *MF.getFunction()->getParent(), 964 &IRSlots); 965 if (!C) 966 return error(Loc + Err.getColumnNo(), Err.getMessage()); 967 return false; 968 } 969 970 bool MIParser::parseIRConstant(StringRef::iterator Loc, const Constant *&C) { 971 if (parseIRConstant(Loc, StringRef(Loc, Token.range().end() - Loc), C)) 972 return true; 973 lex(); 974 return false; 975 } 976 977 bool MIParser::parseTypedImmediateOperand(MachineOperand &Dest) { 978 assert(Token.is(MIToken::IntegerType)); 979 auto Loc = Token.location(); 980 lex(); 981 if (Token.isNot(MIToken::IntegerLiteral)) 982 return error("expected an integer literal"); 983 const Constant *C = nullptr; 984 if (parseIRConstant(Loc, C)) 985 return true; 986 Dest = MachineOperand::CreateCImm(cast<ConstantInt>(C)); 987 return false; 988 } 989 990 bool MIParser::parseFPImmediateOperand(MachineOperand &Dest) { 991 auto Loc = Token.location(); 992 lex(); 993 if (Token.isNot(MIToken::FloatingPointLiteral)) 994 return error("expected a floating point literal"); 995 const Constant *C = nullptr; 996 if (parseIRConstant(Loc, C)) 997 return true; 998 Dest = MachineOperand::CreateFPImm(cast<ConstantFP>(C)); 999 return false; 1000 } 1001 1002 bool MIParser::getUnsigned(unsigned &Result) { 1003 assert(Token.hasIntegerValue() && "Expected a token with an integer value"); 1004 const uint64_t Limit = uint64_t(std::numeric_limits<unsigned>::max()) + 1; 1005 uint64_t Val64 = Token.integerValue().getLimitedValue(Limit); 1006 if (Val64 == Limit) 1007 return error("expected 32-bit integer (too large)"); 1008 Result = Val64; 1009 return false; 1010 } 1011 1012 bool MIParser::parseMBBReference(MachineBasicBlock *&MBB) { 1013 assert(Token.is(MIToken::MachineBasicBlock) || 1014 Token.is(MIToken::MachineBasicBlockLabel)); 1015 unsigned Number; 1016 if (getUnsigned(Number)) 1017 return true; 1018 auto MBBInfo = PFS.MBBSlots.find(Number); 1019 if (MBBInfo == PFS.MBBSlots.end()) 1020 return error(Twine("use of undefined machine basic block #") + 1021 Twine(Number)); 1022 MBB = MBBInfo->second; 1023 if (!Token.stringValue().empty() && Token.stringValue() != MBB->getName()) 1024 return error(Twine("the name of machine basic block #") + Twine(Number) + 1025 " isn't '" + Token.stringValue() + "'"); 1026 return false; 1027 } 1028 1029 bool MIParser::parseMBBOperand(MachineOperand &Dest) { 1030 MachineBasicBlock *MBB; 1031 if (parseMBBReference(MBB)) 1032 return true; 1033 Dest = MachineOperand::CreateMBB(MBB); 1034 lex(); 1035 return false; 1036 } 1037 1038 bool MIParser::parseStackFrameIndex(int &FI) { 1039 assert(Token.is(MIToken::StackObject)); 1040 unsigned ID; 1041 if (getUnsigned(ID)) 1042 return true; 1043 auto ObjectInfo = PFS.StackObjectSlots.find(ID); 1044 if (ObjectInfo == PFS.StackObjectSlots.end()) 1045 return error(Twine("use of undefined stack object '%stack.") + Twine(ID) + 1046 "'"); 1047 StringRef Name; 1048 if (const auto *Alloca = 1049 MF.getFrameInfo()->getObjectAllocation(ObjectInfo->second)) 1050 Name = Alloca->getName(); 1051 if (!Token.stringValue().empty() && Token.stringValue() != Name) 1052 return error(Twine("the name of the stack object '%stack.") + Twine(ID) + 1053 "' isn't '" + Token.stringValue() + "'"); 1054 lex(); 1055 FI = ObjectInfo->second; 1056 return false; 1057 } 1058 1059 bool MIParser::parseStackObjectOperand(MachineOperand &Dest) { 1060 int FI; 1061 if (parseStackFrameIndex(FI)) 1062 return true; 1063 Dest = MachineOperand::CreateFI(FI); 1064 return false; 1065 } 1066 1067 bool MIParser::parseFixedStackFrameIndex(int &FI) { 1068 assert(Token.is(MIToken::FixedStackObject)); 1069 unsigned ID; 1070 if (getUnsigned(ID)) 1071 return true; 1072 auto ObjectInfo = PFS.FixedStackObjectSlots.find(ID); 1073 if (ObjectInfo == PFS.FixedStackObjectSlots.end()) 1074 return error(Twine("use of undefined fixed stack object '%fixed-stack.") + 1075 Twine(ID) + "'"); 1076 lex(); 1077 FI = ObjectInfo->second; 1078 return false; 1079 } 1080 1081 bool MIParser::parseFixedStackObjectOperand(MachineOperand &Dest) { 1082 int FI; 1083 if (parseFixedStackFrameIndex(FI)) 1084 return true; 1085 Dest = MachineOperand::CreateFI(FI); 1086 return false; 1087 } 1088 1089 bool MIParser::parseGlobalValue(GlobalValue *&GV) { 1090 switch (Token.kind()) { 1091 case MIToken::NamedGlobalValue: { 1092 const Module *M = MF.getFunction()->getParent(); 1093 GV = M->getNamedValue(Token.stringValue()); 1094 if (!GV) 1095 return error(Twine("use of undefined global value '") + Token.range() + 1096 "'"); 1097 break; 1098 } 1099 case MIToken::GlobalValue: { 1100 unsigned GVIdx; 1101 if (getUnsigned(GVIdx)) 1102 return true; 1103 if (GVIdx >= IRSlots.GlobalValues.size()) 1104 return error(Twine("use of undefined global value '@") + Twine(GVIdx) + 1105 "'"); 1106 GV = IRSlots.GlobalValues[GVIdx]; 1107 break; 1108 } 1109 default: 1110 llvm_unreachable("The current token should be a global value"); 1111 } 1112 return false; 1113 } 1114 1115 bool MIParser::parseGlobalAddressOperand(MachineOperand &Dest) { 1116 GlobalValue *GV = nullptr; 1117 if (parseGlobalValue(GV)) 1118 return true; 1119 lex(); 1120 Dest = MachineOperand::CreateGA(GV, /*Offset=*/0); 1121 if (parseOperandsOffset(Dest)) 1122 return true; 1123 return false; 1124 } 1125 1126 bool MIParser::parseConstantPoolIndexOperand(MachineOperand &Dest) { 1127 assert(Token.is(MIToken::ConstantPoolItem)); 1128 unsigned ID; 1129 if (getUnsigned(ID)) 1130 return true; 1131 auto ConstantInfo = PFS.ConstantPoolSlots.find(ID); 1132 if (ConstantInfo == PFS.ConstantPoolSlots.end()) 1133 return error("use of undefined constant '%const." + Twine(ID) + "'"); 1134 lex(); 1135 Dest = MachineOperand::CreateCPI(ID, /*Offset=*/0); 1136 if (parseOperandsOffset(Dest)) 1137 return true; 1138 return false; 1139 } 1140 1141 bool MIParser::parseJumpTableIndexOperand(MachineOperand &Dest) { 1142 assert(Token.is(MIToken::JumpTableIndex)); 1143 unsigned ID; 1144 if (getUnsigned(ID)) 1145 return true; 1146 auto JumpTableEntryInfo = PFS.JumpTableSlots.find(ID); 1147 if (JumpTableEntryInfo == PFS.JumpTableSlots.end()) 1148 return error("use of undefined jump table '%jump-table." + Twine(ID) + "'"); 1149 lex(); 1150 Dest = MachineOperand::CreateJTI(JumpTableEntryInfo->second); 1151 return false; 1152 } 1153 1154 bool MIParser::parseExternalSymbolOperand(MachineOperand &Dest) { 1155 assert(Token.is(MIToken::ExternalSymbol)); 1156 const char *Symbol = MF.createExternalSymbolName(Token.stringValue()); 1157 lex(); 1158 Dest = MachineOperand::CreateES(Symbol); 1159 if (parseOperandsOffset(Dest)) 1160 return true; 1161 return false; 1162 } 1163 1164 bool MIParser::parseMDNode(MDNode *&Node) { 1165 assert(Token.is(MIToken::exclaim)); 1166 auto Loc = Token.location(); 1167 lex(); 1168 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned()) 1169 return error("expected metadata id after '!'"); 1170 unsigned ID; 1171 if (getUnsigned(ID)) 1172 return true; 1173 auto NodeInfo = IRSlots.MetadataNodes.find(ID); 1174 if (NodeInfo == IRSlots.MetadataNodes.end()) 1175 return error(Loc, "use of undefined metadata '!" + Twine(ID) + "'"); 1176 lex(); 1177 Node = NodeInfo->second.get(); 1178 return false; 1179 } 1180 1181 bool MIParser::parseMetadataOperand(MachineOperand &Dest) { 1182 MDNode *Node = nullptr; 1183 if (parseMDNode(Node)) 1184 return true; 1185 Dest = MachineOperand::CreateMetadata(Node); 1186 return false; 1187 } 1188 1189 bool MIParser::parseCFIOffset(int &Offset) { 1190 if (Token.isNot(MIToken::IntegerLiteral)) 1191 return error("expected a cfi offset"); 1192 if (Token.integerValue().getMinSignedBits() > 32) 1193 return error("expected a 32 bit integer (the cfi offset is too large)"); 1194 Offset = (int)Token.integerValue().getExtValue(); 1195 lex(); 1196 return false; 1197 } 1198 1199 bool MIParser::parseCFIRegister(unsigned &Reg) { 1200 if (Token.isNot(MIToken::NamedRegister)) 1201 return error("expected a cfi register"); 1202 unsigned LLVMReg; 1203 if (parseRegister(LLVMReg)) 1204 return true; 1205 const auto *TRI = MF.getSubtarget().getRegisterInfo(); 1206 assert(TRI && "Expected target register info"); 1207 int DwarfReg = TRI->getDwarfRegNum(LLVMReg, true); 1208 if (DwarfReg < 0) 1209 return error("invalid DWARF register"); 1210 Reg = (unsigned)DwarfReg; 1211 lex(); 1212 return false; 1213 } 1214 1215 bool MIParser::parseCFIOperand(MachineOperand &Dest) { 1216 auto Kind = Token.kind(); 1217 lex(); 1218 auto &MMI = MF.getMMI(); 1219 int Offset; 1220 unsigned Reg; 1221 unsigned CFIIndex; 1222 switch (Kind) { 1223 case MIToken::kw_cfi_same_value: 1224 if (parseCFIRegister(Reg)) 1225 return true; 1226 CFIIndex = 1227 MMI.addFrameInst(MCCFIInstruction::createSameValue(nullptr, Reg)); 1228 break; 1229 case MIToken::kw_cfi_offset: 1230 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) || 1231 parseCFIOffset(Offset)) 1232 return true; 1233 CFIIndex = 1234 MMI.addFrameInst(MCCFIInstruction::createOffset(nullptr, Reg, Offset)); 1235 break; 1236 case MIToken::kw_cfi_def_cfa_register: 1237 if (parseCFIRegister(Reg)) 1238 return true; 1239 CFIIndex = 1240 MMI.addFrameInst(MCCFIInstruction::createDefCfaRegister(nullptr, Reg)); 1241 break; 1242 case MIToken::kw_cfi_def_cfa_offset: 1243 if (parseCFIOffset(Offset)) 1244 return true; 1245 // NB: MCCFIInstruction::createDefCfaOffset negates the offset. 1246 CFIIndex = MMI.addFrameInst( 1247 MCCFIInstruction::createDefCfaOffset(nullptr, -Offset)); 1248 break; 1249 case MIToken::kw_cfi_def_cfa: 1250 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) || 1251 parseCFIOffset(Offset)) 1252 return true; 1253 // NB: MCCFIInstruction::createDefCfa negates the offset. 1254 CFIIndex = 1255 MMI.addFrameInst(MCCFIInstruction::createDefCfa(nullptr, Reg, -Offset)); 1256 break; 1257 default: 1258 // TODO: Parse the other CFI operands. 1259 llvm_unreachable("The current token should be a cfi operand"); 1260 } 1261 Dest = MachineOperand::CreateCFIIndex(CFIIndex); 1262 return false; 1263 } 1264 1265 bool MIParser::parseIRBlock(BasicBlock *&BB, const Function &F) { 1266 switch (Token.kind()) { 1267 case MIToken::NamedIRBlock: { 1268 BB = dyn_cast_or_null<BasicBlock>( 1269 F.getValueSymbolTable().lookup(Token.stringValue())); 1270 if (!BB) 1271 return error(Twine("use of undefined IR block '") + Token.range() + "'"); 1272 break; 1273 } 1274 case MIToken::IRBlock: { 1275 unsigned SlotNumber = 0; 1276 if (getUnsigned(SlotNumber)) 1277 return true; 1278 BB = const_cast<BasicBlock *>(getIRBlock(SlotNumber, F)); 1279 if (!BB) 1280 return error(Twine("use of undefined IR block '%ir-block.") + 1281 Twine(SlotNumber) + "'"); 1282 break; 1283 } 1284 default: 1285 llvm_unreachable("The current token should be an IR block reference"); 1286 } 1287 return false; 1288 } 1289 1290 bool MIParser::parseBlockAddressOperand(MachineOperand &Dest) { 1291 assert(Token.is(MIToken::kw_blockaddress)); 1292 lex(); 1293 if (expectAndConsume(MIToken::lparen)) 1294 return true; 1295 if (Token.isNot(MIToken::GlobalValue) && 1296 Token.isNot(MIToken::NamedGlobalValue)) 1297 return error("expected a global value"); 1298 GlobalValue *GV = nullptr; 1299 if (parseGlobalValue(GV)) 1300 return true; 1301 auto *F = dyn_cast<Function>(GV); 1302 if (!F) 1303 return error("expected an IR function reference"); 1304 lex(); 1305 if (expectAndConsume(MIToken::comma)) 1306 return true; 1307 BasicBlock *BB = nullptr; 1308 if (Token.isNot(MIToken::IRBlock) && Token.isNot(MIToken::NamedIRBlock)) 1309 return error("expected an IR block reference"); 1310 if (parseIRBlock(BB, *F)) 1311 return true; 1312 lex(); 1313 if (expectAndConsume(MIToken::rparen)) 1314 return true; 1315 Dest = MachineOperand::CreateBA(BlockAddress::get(F, BB), /*Offset=*/0); 1316 if (parseOperandsOffset(Dest)) 1317 return true; 1318 return false; 1319 } 1320 1321 bool MIParser::parseTargetIndexOperand(MachineOperand &Dest) { 1322 assert(Token.is(MIToken::kw_target_index)); 1323 lex(); 1324 if (expectAndConsume(MIToken::lparen)) 1325 return true; 1326 if (Token.isNot(MIToken::Identifier)) 1327 return error("expected the name of the target index"); 1328 int Index = 0; 1329 if (getTargetIndex(Token.stringValue(), Index)) 1330 return error("use of undefined target index '" + Token.stringValue() + "'"); 1331 lex(); 1332 if (expectAndConsume(MIToken::rparen)) 1333 return true; 1334 Dest = MachineOperand::CreateTargetIndex(unsigned(Index), /*Offset=*/0); 1335 if (parseOperandsOffset(Dest)) 1336 return true; 1337 return false; 1338 } 1339 1340 bool MIParser::parseLiveoutRegisterMaskOperand(MachineOperand &Dest) { 1341 assert(Token.is(MIToken::kw_liveout)); 1342 const auto *TRI = MF.getSubtarget().getRegisterInfo(); 1343 assert(TRI && "Expected target register info"); 1344 uint32_t *Mask = MF.allocateRegisterMask(TRI->getNumRegs()); 1345 lex(); 1346 if (expectAndConsume(MIToken::lparen)) 1347 return true; 1348 while (true) { 1349 if (Token.isNot(MIToken::NamedRegister)) 1350 return error("expected a named register"); 1351 unsigned Reg = 0; 1352 if (parseRegister(Reg)) 1353 return true; 1354 lex(); 1355 Mask[Reg / 32] |= 1U << (Reg % 32); 1356 // TODO: Report an error if the same register is used more than once. 1357 if (Token.isNot(MIToken::comma)) 1358 break; 1359 lex(); 1360 } 1361 if (expectAndConsume(MIToken::rparen)) 1362 return true; 1363 Dest = MachineOperand::CreateRegLiveOut(Mask); 1364 return false; 1365 } 1366 1367 bool MIParser::parseMachineOperand(MachineOperand &Dest, 1368 Optional<unsigned> &TiedDefIdx) { 1369 switch (Token.kind()) { 1370 case MIToken::kw_implicit: 1371 case MIToken::kw_implicit_define: 1372 case MIToken::kw_def: 1373 case MIToken::kw_dead: 1374 case MIToken::kw_killed: 1375 case MIToken::kw_undef: 1376 case MIToken::kw_internal: 1377 case MIToken::kw_early_clobber: 1378 case MIToken::kw_debug_use: 1379 case MIToken::underscore: 1380 case MIToken::NamedRegister: 1381 case MIToken::VirtualRegister: 1382 return parseRegisterOperand(Dest, TiedDefIdx); 1383 case MIToken::IntegerLiteral: 1384 return parseImmediateOperand(Dest); 1385 case MIToken::IntegerType: 1386 return parseTypedImmediateOperand(Dest); 1387 case MIToken::kw_half: 1388 case MIToken::kw_float: 1389 case MIToken::kw_double: 1390 case MIToken::kw_x86_fp80: 1391 case MIToken::kw_fp128: 1392 case MIToken::kw_ppc_fp128: 1393 return parseFPImmediateOperand(Dest); 1394 case MIToken::MachineBasicBlock: 1395 return parseMBBOperand(Dest); 1396 case MIToken::StackObject: 1397 return parseStackObjectOperand(Dest); 1398 case MIToken::FixedStackObject: 1399 return parseFixedStackObjectOperand(Dest); 1400 case MIToken::GlobalValue: 1401 case MIToken::NamedGlobalValue: 1402 return parseGlobalAddressOperand(Dest); 1403 case MIToken::ConstantPoolItem: 1404 return parseConstantPoolIndexOperand(Dest); 1405 case MIToken::JumpTableIndex: 1406 return parseJumpTableIndexOperand(Dest); 1407 case MIToken::ExternalSymbol: 1408 return parseExternalSymbolOperand(Dest); 1409 case MIToken::exclaim: 1410 return parseMetadataOperand(Dest); 1411 case MIToken::kw_cfi_same_value: 1412 case MIToken::kw_cfi_offset: 1413 case MIToken::kw_cfi_def_cfa_register: 1414 case MIToken::kw_cfi_def_cfa_offset: 1415 case MIToken::kw_cfi_def_cfa: 1416 return parseCFIOperand(Dest); 1417 case MIToken::kw_blockaddress: 1418 return parseBlockAddressOperand(Dest); 1419 case MIToken::kw_target_index: 1420 return parseTargetIndexOperand(Dest); 1421 case MIToken::kw_liveout: 1422 return parseLiveoutRegisterMaskOperand(Dest); 1423 case MIToken::Error: 1424 return true; 1425 case MIToken::Identifier: 1426 if (const auto *RegMask = getRegMask(Token.stringValue())) { 1427 Dest = MachineOperand::CreateRegMask(RegMask); 1428 lex(); 1429 break; 1430 } 1431 // fallthrough 1432 default: 1433 // FIXME: Parse the MCSymbol machine operand. 1434 return error("expected a machine operand"); 1435 } 1436 return false; 1437 } 1438 1439 bool MIParser::parseMachineOperandAndTargetFlags( 1440 MachineOperand &Dest, Optional<unsigned> &TiedDefIdx) { 1441 unsigned TF = 0; 1442 bool HasTargetFlags = false; 1443 if (Token.is(MIToken::kw_target_flags)) { 1444 HasTargetFlags = true; 1445 lex(); 1446 if (expectAndConsume(MIToken::lparen)) 1447 return true; 1448 if (Token.isNot(MIToken::Identifier)) 1449 return error("expected the name of the target flag"); 1450 if (getDirectTargetFlag(Token.stringValue(), TF)) { 1451 if (getBitmaskTargetFlag(Token.stringValue(), TF)) 1452 return error("use of undefined target flag '" + Token.stringValue() + 1453 "'"); 1454 } 1455 lex(); 1456 while (Token.is(MIToken::comma)) { 1457 lex(); 1458 if (Token.isNot(MIToken::Identifier)) 1459 return error("expected the name of the target flag"); 1460 unsigned BitFlag = 0; 1461 if (getBitmaskTargetFlag(Token.stringValue(), BitFlag)) 1462 return error("use of undefined target flag '" + Token.stringValue() + 1463 "'"); 1464 // TODO: Report an error when using a duplicate bit target flag. 1465 TF |= BitFlag; 1466 lex(); 1467 } 1468 if (expectAndConsume(MIToken::rparen)) 1469 return true; 1470 } 1471 auto Loc = Token.location(); 1472 if (parseMachineOperand(Dest, TiedDefIdx)) 1473 return true; 1474 if (!HasTargetFlags) 1475 return false; 1476 if (Dest.isReg()) 1477 return error(Loc, "register operands can't have target flags"); 1478 Dest.setTargetFlags(TF); 1479 return false; 1480 } 1481 1482 bool MIParser::parseOffset(int64_t &Offset) { 1483 if (Token.isNot(MIToken::plus) && Token.isNot(MIToken::minus)) 1484 return false; 1485 StringRef Sign = Token.range(); 1486 bool IsNegative = Token.is(MIToken::minus); 1487 lex(); 1488 if (Token.isNot(MIToken::IntegerLiteral)) 1489 return error("expected an integer literal after '" + Sign + "'"); 1490 if (Token.integerValue().getMinSignedBits() > 64) 1491 return error("expected 64-bit integer (too large)"); 1492 Offset = Token.integerValue().getExtValue(); 1493 if (IsNegative) 1494 Offset = -Offset; 1495 lex(); 1496 return false; 1497 } 1498 1499 bool MIParser::parseAlignment(unsigned &Alignment) { 1500 assert(Token.is(MIToken::kw_align)); 1501 lex(); 1502 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned()) 1503 return error("expected an integer literal after 'align'"); 1504 if (getUnsigned(Alignment)) 1505 return true; 1506 lex(); 1507 return false; 1508 } 1509 1510 bool MIParser::parseOperandsOffset(MachineOperand &Op) { 1511 int64_t Offset = 0; 1512 if (parseOffset(Offset)) 1513 return true; 1514 Op.setOffset(Offset); 1515 return false; 1516 } 1517 1518 bool MIParser::parseIRValue(const Value *&V) { 1519 switch (Token.kind()) { 1520 case MIToken::NamedIRValue: { 1521 V = MF.getFunction()->getValueSymbolTable().lookup(Token.stringValue()); 1522 break; 1523 } 1524 case MIToken::IRValue: { 1525 unsigned SlotNumber = 0; 1526 if (getUnsigned(SlotNumber)) 1527 return true; 1528 V = getIRValue(SlotNumber); 1529 break; 1530 } 1531 case MIToken::NamedGlobalValue: 1532 case MIToken::GlobalValue: { 1533 GlobalValue *GV = nullptr; 1534 if (parseGlobalValue(GV)) 1535 return true; 1536 V = GV; 1537 break; 1538 } 1539 case MIToken::QuotedIRValue: { 1540 const Constant *C = nullptr; 1541 if (parseIRConstant(Token.location(), Token.stringValue(), C)) 1542 return true; 1543 V = C; 1544 break; 1545 } 1546 default: 1547 llvm_unreachable("The current token should be an IR block reference"); 1548 } 1549 if (!V) 1550 return error(Twine("use of undefined IR value '") + Token.range() + "'"); 1551 return false; 1552 } 1553 1554 bool MIParser::getUint64(uint64_t &Result) { 1555 assert(Token.hasIntegerValue()); 1556 if (Token.integerValue().getActiveBits() > 64) 1557 return error("expected 64-bit integer (too large)"); 1558 Result = Token.integerValue().getZExtValue(); 1559 return false; 1560 } 1561 1562 bool MIParser::parseMemoryOperandFlag(unsigned &Flags) { 1563 const unsigned OldFlags = Flags; 1564 switch (Token.kind()) { 1565 case MIToken::kw_volatile: 1566 Flags |= MachineMemOperand::MOVolatile; 1567 break; 1568 case MIToken::kw_non_temporal: 1569 Flags |= MachineMemOperand::MONonTemporal; 1570 break; 1571 case MIToken::kw_invariant: 1572 Flags |= MachineMemOperand::MOInvariant; 1573 break; 1574 // TODO: parse the target specific memory operand flags. 1575 default: 1576 llvm_unreachable("The current token should be a memory operand flag"); 1577 } 1578 if (OldFlags == Flags) 1579 // We know that the same flag is specified more than once when the flags 1580 // weren't modified. 1581 return error("duplicate '" + Token.stringValue() + "' memory operand flag"); 1582 lex(); 1583 return false; 1584 } 1585 1586 bool MIParser::parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV) { 1587 switch (Token.kind()) { 1588 case MIToken::kw_stack: 1589 PSV = MF.getPSVManager().getStack(); 1590 break; 1591 case MIToken::kw_got: 1592 PSV = MF.getPSVManager().getGOT(); 1593 break; 1594 case MIToken::kw_jump_table: 1595 PSV = MF.getPSVManager().getJumpTable(); 1596 break; 1597 case MIToken::kw_constant_pool: 1598 PSV = MF.getPSVManager().getConstantPool(); 1599 break; 1600 case MIToken::FixedStackObject: { 1601 int FI; 1602 if (parseFixedStackFrameIndex(FI)) 1603 return true; 1604 PSV = MF.getPSVManager().getFixedStack(FI); 1605 // The token was already consumed, so use return here instead of break. 1606 return false; 1607 } 1608 case MIToken::kw_call_entry: { 1609 lex(); 1610 switch (Token.kind()) { 1611 case MIToken::GlobalValue: 1612 case MIToken::NamedGlobalValue: { 1613 GlobalValue *GV = nullptr; 1614 if (parseGlobalValue(GV)) 1615 return true; 1616 PSV = MF.getPSVManager().getGlobalValueCallEntry(GV); 1617 break; 1618 } 1619 case MIToken::ExternalSymbol: 1620 PSV = MF.getPSVManager().getExternalSymbolCallEntry( 1621 MF.createExternalSymbolName(Token.stringValue())); 1622 break; 1623 default: 1624 return error( 1625 "expected a global value or an external symbol after 'call-entry'"); 1626 } 1627 break; 1628 } 1629 default: 1630 llvm_unreachable("The current token should be pseudo source value"); 1631 } 1632 lex(); 1633 return false; 1634 } 1635 1636 bool MIParser::parseMachinePointerInfo(MachinePointerInfo &Dest) { 1637 if (Token.is(MIToken::kw_constant_pool) || Token.is(MIToken::kw_stack) || 1638 Token.is(MIToken::kw_got) || Token.is(MIToken::kw_jump_table) || 1639 Token.is(MIToken::FixedStackObject) || Token.is(MIToken::kw_call_entry)) { 1640 const PseudoSourceValue *PSV = nullptr; 1641 if (parseMemoryPseudoSourceValue(PSV)) 1642 return true; 1643 int64_t Offset = 0; 1644 if (parseOffset(Offset)) 1645 return true; 1646 Dest = MachinePointerInfo(PSV, Offset); 1647 return false; 1648 } 1649 if (Token.isNot(MIToken::NamedIRValue) && Token.isNot(MIToken::IRValue) && 1650 Token.isNot(MIToken::GlobalValue) && 1651 Token.isNot(MIToken::NamedGlobalValue) && 1652 Token.isNot(MIToken::QuotedIRValue)) 1653 return error("expected an IR value reference"); 1654 const Value *V = nullptr; 1655 if (parseIRValue(V)) 1656 return true; 1657 if (!V->getType()->isPointerTy()) 1658 return error("expected a pointer IR value"); 1659 lex(); 1660 int64_t Offset = 0; 1661 if (parseOffset(Offset)) 1662 return true; 1663 Dest = MachinePointerInfo(V, Offset); 1664 return false; 1665 } 1666 1667 bool MIParser::parseMachineMemoryOperand(MachineMemOperand *&Dest) { 1668 if (expectAndConsume(MIToken::lparen)) 1669 return true; 1670 unsigned Flags = 0; 1671 while (Token.isMemoryOperandFlag()) { 1672 if (parseMemoryOperandFlag(Flags)) 1673 return true; 1674 } 1675 if (Token.isNot(MIToken::Identifier) || 1676 (Token.stringValue() != "load" && Token.stringValue() != "store")) 1677 return error("expected 'load' or 'store' memory operation"); 1678 if (Token.stringValue() == "load") 1679 Flags |= MachineMemOperand::MOLoad; 1680 else 1681 Flags |= MachineMemOperand::MOStore; 1682 lex(); 1683 1684 if (Token.isNot(MIToken::IntegerLiteral)) 1685 return error("expected the size integer literal after memory operation"); 1686 uint64_t Size; 1687 if (getUint64(Size)) 1688 return true; 1689 lex(); 1690 1691 const char *Word = Flags & MachineMemOperand::MOLoad ? "from" : "into"; 1692 if (Token.isNot(MIToken::Identifier) || Token.stringValue() != Word) 1693 return error(Twine("expected '") + Word + "'"); 1694 lex(); 1695 1696 MachinePointerInfo Ptr = MachinePointerInfo(); 1697 if (parseMachinePointerInfo(Ptr)) 1698 return true; 1699 unsigned BaseAlignment = Size; 1700 AAMDNodes AAInfo; 1701 MDNode *Range = nullptr; 1702 while (consumeIfPresent(MIToken::comma)) { 1703 switch (Token.kind()) { 1704 case MIToken::kw_align: 1705 if (parseAlignment(BaseAlignment)) 1706 return true; 1707 break; 1708 case MIToken::md_tbaa: 1709 lex(); 1710 if (parseMDNode(AAInfo.TBAA)) 1711 return true; 1712 break; 1713 case MIToken::md_alias_scope: 1714 lex(); 1715 if (parseMDNode(AAInfo.Scope)) 1716 return true; 1717 break; 1718 case MIToken::md_noalias: 1719 lex(); 1720 if (parseMDNode(AAInfo.NoAlias)) 1721 return true; 1722 break; 1723 case MIToken::md_range: 1724 lex(); 1725 if (parseMDNode(Range)) 1726 return true; 1727 break; 1728 // TODO: Report an error on duplicate metadata nodes. 1729 default: 1730 return error("expected 'align' or '!tbaa' or '!alias.scope' or " 1731 "'!noalias' or '!range'"); 1732 } 1733 } 1734 if (expectAndConsume(MIToken::rparen)) 1735 return true; 1736 Dest = 1737 MF.getMachineMemOperand(Ptr, Flags, Size, BaseAlignment, AAInfo, Range); 1738 return false; 1739 } 1740 1741 void MIParser::initNames2InstrOpCodes() { 1742 if (!Names2InstrOpCodes.empty()) 1743 return; 1744 const auto *TII = MF.getSubtarget().getInstrInfo(); 1745 assert(TII && "Expected target instruction info"); 1746 for (unsigned I = 0, E = TII->getNumOpcodes(); I < E; ++I) 1747 Names2InstrOpCodes.insert(std::make_pair(StringRef(TII->getName(I)), I)); 1748 } 1749 1750 bool MIParser::parseInstrName(StringRef InstrName, unsigned &OpCode) { 1751 initNames2InstrOpCodes(); 1752 auto InstrInfo = Names2InstrOpCodes.find(InstrName); 1753 if (InstrInfo == Names2InstrOpCodes.end()) 1754 return true; 1755 OpCode = InstrInfo->getValue(); 1756 return false; 1757 } 1758 1759 void MIParser::initNames2Regs() { 1760 if (!Names2Regs.empty()) 1761 return; 1762 // The '%noreg' register is the register 0. 1763 Names2Regs.insert(std::make_pair("noreg", 0)); 1764 const auto *TRI = MF.getSubtarget().getRegisterInfo(); 1765 assert(TRI && "Expected target register info"); 1766 for (unsigned I = 0, E = TRI->getNumRegs(); I < E; ++I) { 1767 bool WasInserted = 1768 Names2Regs.insert(std::make_pair(StringRef(TRI->getName(I)).lower(), I)) 1769 .second; 1770 (void)WasInserted; 1771 assert(WasInserted && "Expected registers to be unique case-insensitively"); 1772 } 1773 } 1774 1775 bool MIParser::getRegisterByName(StringRef RegName, unsigned &Reg) { 1776 initNames2Regs(); 1777 auto RegInfo = Names2Regs.find(RegName); 1778 if (RegInfo == Names2Regs.end()) 1779 return true; 1780 Reg = RegInfo->getValue(); 1781 return false; 1782 } 1783 1784 void MIParser::initNames2RegMasks() { 1785 if (!Names2RegMasks.empty()) 1786 return; 1787 const auto *TRI = MF.getSubtarget().getRegisterInfo(); 1788 assert(TRI && "Expected target register info"); 1789 ArrayRef<const uint32_t *> RegMasks = TRI->getRegMasks(); 1790 ArrayRef<const char *> RegMaskNames = TRI->getRegMaskNames(); 1791 assert(RegMasks.size() == RegMaskNames.size()); 1792 for (size_t I = 0, E = RegMasks.size(); I < E; ++I) 1793 Names2RegMasks.insert( 1794 std::make_pair(StringRef(RegMaskNames[I]).lower(), RegMasks[I])); 1795 } 1796 1797 const uint32_t *MIParser::getRegMask(StringRef Identifier) { 1798 initNames2RegMasks(); 1799 auto RegMaskInfo = Names2RegMasks.find(Identifier); 1800 if (RegMaskInfo == Names2RegMasks.end()) 1801 return nullptr; 1802 return RegMaskInfo->getValue(); 1803 } 1804 1805 void MIParser::initNames2SubRegIndices() { 1806 if (!Names2SubRegIndices.empty()) 1807 return; 1808 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); 1809 for (unsigned I = 1, E = TRI->getNumSubRegIndices(); I < E; ++I) 1810 Names2SubRegIndices.insert( 1811 std::make_pair(StringRef(TRI->getSubRegIndexName(I)).lower(), I)); 1812 } 1813 1814 unsigned MIParser::getSubRegIndex(StringRef Name) { 1815 initNames2SubRegIndices(); 1816 auto SubRegInfo = Names2SubRegIndices.find(Name); 1817 if (SubRegInfo == Names2SubRegIndices.end()) 1818 return 0; 1819 return SubRegInfo->getValue(); 1820 } 1821 1822 static void initSlots2BasicBlocks( 1823 const Function &F, 1824 DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) { 1825 ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false); 1826 MST.incorporateFunction(F); 1827 for (auto &BB : F) { 1828 if (BB.hasName()) 1829 continue; 1830 int Slot = MST.getLocalSlot(&BB); 1831 if (Slot == -1) 1832 continue; 1833 Slots2BasicBlocks.insert(std::make_pair(unsigned(Slot), &BB)); 1834 } 1835 } 1836 1837 static const BasicBlock *getIRBlockFromSlot( 1838 unsigned Slot, 1839 const DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) { 1840 auto BlockInfo = Slots2BasicBlocks.find(Slot); 1841 if (BlockInfo == Slots2BasicBlocks.end()) 1842 return nullptr; 1843 return BlockInfo->second; 1844 } 1845 1846 const BasicBlock *MIParser::getIRBlock(unsigned Slot) { 1847 if (Slots2BasicBlocks.empty()) 1848 initSlots2BasicBlocks(*MF.getFunction(), Slots2BasicBlocks); 1849 return getIRBlockFromSlot(Slot, Slots2BasicBlocks); 1850 } 1851 1852 const BasicBlock *MIParser::getIRBlock(unsigned Slot, const Function &F) { 1853 if (&F == MF.getFunction()) 1854 return getIRBlock(Slot); 1855 DenseMap<unsigned, const BasicBlock *> CustomSlots2BasicBlocks; 1856 initSlots2BasicBlocks(F, CustomSlots2BasicBlocks); 1857 return getIRBlockFromSlot(Slot, CustomSlots2BasicBlocks); 1858 } 1859 1860 static void mapValueToSlot(const Value *V, ModuleSlotTracker &MST, 1861 DenseMap<unsigned, const Value *> &Slots2Values) { 1862 int Slot = MST.getLocalSlot(V); 1863 if (Slot == -1) 1864 return; 1865 Slots2Values.insert(std::make_pair(unsigned(Slot), V)); 1866 } 1867 1868 /// Creates the mapping from slot numbers to function's unnamed IR values. 1869 static void initSlots2Values(const Function &F, 1870 DenseMap<unsigned, const Value *> &Slots2Values) { 1871 ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false); 1872 MST.incorporateFunction(F); 1873 for (const auto &Arg : F.args()) 1874 mapValueToSlot(&Arg, MST, Slots2Values); 1875 for (const auto &BB : F) { 1876 mapValueToSlot(&BB, MST, Slots2Values); 1877 for (const auto &I : BB) 1878 mapValueToSlot(&I, MST, Slots2Values); 1879 } 1880 } 1881 1882 const Value *MIParser::getIRValue(unsigned Slot) { 1883 if (Slots2Values.empty()) 1884 initSlots2Values(*MF.getFunction(), Slots2Values); 1885 auto ValueInfo = Slots2Values.find(Slot); 1886 if (ValueInfo == Slots2Values.end()) 1887 return nullptr; 1888 return ValueInfo->second; 1889 } 1890 1891 void MIParser::initNames2TargetIndices() { 1892 if (!Names2TargetIndices.empty()) 1893 return; 1894 const auto *TII = MF.getSubtarget().getInstrInfo(); 1895 assert(TII && "Expected target instruction info"); 1896 auto Indices = TII->getSerializableTargetIndices(); 1897 for (const auto &I : Indices) 1898 Names2TargetIndices.insert(std::make_pair(StringRef(I.second), I.first)); 1899 } 1900 1901 bool MIParser::getTargetIndex(StringRef Name, int &Index) { 1902 initNames2TargetIndices(); 1903 auto IndexInfo = Names2TargetIndices.find(Name); 1904 if (IndexInfo == Names2TargetIndices.end()) 1905 return true; 1906 Index = IndexInfo->second; 1907 return false; 1908 } 1909 1910 void MIParser::initNames2DirectTargetFlags() { 1911 if (!Names2DirectTargetFlags.empty()) 1912 return; 1913 const auto *TII = MF.getSubtarget().getInstrInfo(); 1914 assert(TII && "Expected target instruction info"); 1915 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags(); 1916 for (const auto &I : Flags) 1917 Names2DirectTargetFlags.insert( 1918 std::make_pair(StringRef(I.second), I.first)); 1919 } 1920 1921 bool MIParser::getDirectTargetFlag(StringRef Name, unsigned &Flag) { 1922 initNames2DirectTargetFlags(); 1923 auto FlagInfo = Names2DirectTargetFlags.find(Name); 1924 if (FlagInfo == Names2DirectTargetFlags.end()) 1925 return true; 1926 Flag = FlagInfo->second; 1927 return false; 1928 } 1929 1930 void MIParser::initNames2BitmaskTargetFlags() { 1931 if (!Names2BitmaskTargetFlags.empty()) 1932 return; 1933 const auto *TII = MF.getSubtarget().getInstrInfo(); 1934 assert(TII && "Expected target instruction info"); 1935 auto Flags = TII->getSerializableBitmaskMachineOperandTargetFlags(); 1936 for (const auto &I : Flags) 1937 Names2BitmaskTargetFlags.insert( 1938 std::make_pair(StringRef(I.second), I.first)); 1939 } 1940 1941 bool MIParser::getBitmaskTargetFlag(StringRef Name, unsigned &Flag) { 1942 initNames2BitmaskTargetFlags(); 1943 auto FlagInfo = Names2BitmaskTargetFlags.find(Name); 1944 if (FlagInfo == Names2BitmaskTargetFlags.end()) 1945 return true; 1946 Flag = FlagInfo->second; 1947 return false; 1948 } 1949 1950 bool llvm::parseMachineBasicBlockDefinitions(MachineFunction &MF, StringRef Src, 1951 PerFunctionMIParsingState &PFS, 1952 const SlotMapping &IRSlots, 1953 SMDiagnostic &Error) { 1954 SourceMgr SM; 1955 SM.AddNewSourceBuffer( 1956 MemoryBuffer::getMemBuffer(Src, "", /*RequiresNullTerminator=*/false), 1957 SMLoc()); 1958 return MIParser(SM, MF, Error, Src, PFS, IRSlots) 1959 .parseBasicBlockDefinitions(PFS.MBBSlots); 1960 } 1961 1962 bool llvm::parseMachineInstructions(MachineFunction &MF, StringRef Src, 1963 const PerFunctionMIParsingState &PFS, 1964 const SlotMapping &IRSlots, 1965 SMDiagnostic &Error) { 1966 SourceMgr SM; 1967 SM.AddNewSourceBuffer( 1968 MemoryBuffer::getMemBuffer(Src, "", /*RequiresNullTerminator=*/false), 1969 SMLoc()); 1970 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parseBasicBlocks(); 1971 } 1972 1973 bool llvm::parseMBBReference(MachineBasicBlock *&MBB, SourceMgr &SM, 1974 MachineFunction &MF, StringRef Src, 1975 const PerFunctionMIParsingState &PFS, 1976 const SlotMapping &IRSlots, SMDiagnostic &Error) { 1977 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parseStandaloneMBB(MBB); 1978 } 1979 1980 bool llvm::parseNamedRegisterReference(unsigned &Reg, SourceMgr &SM, 1981 MachineFunction &MF, StringRef Src, 1982 const PerFunctionMIParsingState &PFS, 1983 const SlotMapping &IRSlots, 1984 SMDiagnostic &Error) { 1985 return MIParser(SM, MF, Error, Src, PFS, IRSlots) 1986 .parseStandaloneNamedRegister(Reg); 1987 } 1988 1989 bool llvm::parseVirtualRegisterReference(unsigned &Reg, SourceMgr &SM, 1990 MachineFunction &MF, StringRef Src, 1991 const PerFunctionMIParsingState &PFS, 1992 const SlotMapping &IRSlots, 1993 SMDiagnostic &Error) { 1994 return MIParser(SM, MF, Error, Src, PFS, IRSlots) 1995 .parseStandaloneVirtualRegister(Reg); 1996 } 1997 1998 bool llvm::parseStackObjectReference(int &FI, SourceMgr &SM, 1999 MachineFunction &MF, StringRef Src, 2000 const PerFunctionMIParsingState &PFS, 2001 const SlotMapping &IRSlots, 2002 SMDiagnostic &Error) { 2003 return MIParser(SM, MF, Error, Src, PFS, IRSlots) 2004 .parseStandaloneStackObject(FI); 2005 } 2006 2007 bool llvm::parseMDNode(MDNode *&Node, SourceMgr &SM, MachineFunction &MF, 2008 StringRef Src, const PerFunctionMIParsingState &PFS, 2009 const SlotMapping &IRSlots, SMDiagnostic &Error) { 2010 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parseStandaloneMDNode(Node); 2011 } 2012