1 //===- llvm/CodeGen/MachineInstr.h - MachineInstr class ---------*- C++ -*-===// 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 contains the declaration of the MachineInstr class, which is the 10 // basic representation for all target dependent machine instructions used by 11 // the back end. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #ifndef LLVM_CODEGEN_MACHINEINSTR_H 16 #define LLVM_CODEGEN_MACHINEINSTR_H 17 18 #include "llvm/ADT/DenseMapInfo.h" 19 #include "llvm/ADT/PointerSumType.h" 20 #include "llvm/ADT/SmallSet.h" 21 #include "llvm/ADT/ilist.h" 22 #include "llvm/ADT/ilist_node.h" 23 #include "llvm/ADT/iterator_range.h" 24 #include "llvm/CodeGen/MachineMemOperand.h" 25 #include "llvm/CodeGen/MachineOperand.h" 26 #include "llvm/CodeGen/TargetOpcodes.h" 27 #include "llvm/IR/DebugLoc.h" 28 #include "llvm/IR/InlineAsm.h" 29 #include "llvm/MC/MCInstrDesc.h" 30 #include "llvm/MC/MCSymbol.h" 31 #include "llvm/Support/ArrayRecycler.h" 32 #include "llvm/Support/TrailingObjects.h" 33 #include <algorithm> 34 #include <cassert> 35 #include <cstdint> 36 #include <utility> 37 38 namespace llvm { 39 40 class DILabel; 41 class Instruction; 42 class MDNode; 43 class AAResults; 44 template <typename T> class ArrayRef; 45 class DIExpression; 46 class DILocalVariable; 47 class MachineBasicBlock; 48 class MachineFunction; 49 class MachineRegisterInfo; 50 class ModuleSlotTracker; 51 class raw_ostream; 52 template <typename T> class SmallVectorImpl; 53 class SmallBitVector; 54 class StringRef; 55 class TargetInstrInfo; 56 class TargetRegisterClass; 57 class TargetRegisterInfo; 58 59 //===----------------------------------------------------------------------===// 60 /// Representation of each machine instruction. 61 /// 62 /// This class isn't a POD type, but it must have a trivial destructor. When a 63 /// MachineFunction is deleted, all the contained MachineInstrs are deallocated 64 /// without having their destructor called. 65 /// 66 class MachineInstr 67 : public ilist_node_with_parent<MachineInstr, MachineBasicBlock, 68 ilist_sentinel_tracking<true>> { 69 public: 70 using mmo_iterator = ArrayRef<MachineMemOperand *>::iterator; 71 72 /// Flags to specify different kinds of comments to output in 73 /// assembly code. These flags carry semantic information not 74 /// otherwise easily derivable from the IR text. 75 /// 76 enum CommentFlag { 77 ReloadReuse = 0x1, // higher bits are reserved for target dep comments. 78 NoSchedComment = 0x2, 79 TAsmComments = 0x4 // Target Asm comments should start from this value. 80 }; 81 82 enum MIFlag { 83 NoFlags = 0, 84 FrameSetup = 1 << 0, // Instruction is used as a part of 85 // function frame setup code. 86 FrameDestroy = 1 << 1, // Instruction is used as a part of 87 // function frame destruction code. 88 BundledPred = 1 << 2, // Instruction has bundled predecessors. 89 BundledSucc = 1 << 3, // Instruction has bundled successors. 90 FmNoNans = 1 << 4, // Instruction does not support Fast 91 // math nan values. 92 FmNoInfs = 1 << 5, // Instruction does not support Fast 93 // math infinity values. 94 FmNsz = 1 << 6, // Instruction is not required to retain 95 // signed zero values. 96 FmArcp = 1 << 7, // Instruction supports Fast math 97 // reciprocal approximations. 98 FmContract = 1 << 8, // Instruction supports Fast math 99 // contraction operations like fma. 100 FmAfn = 1 << 9, // Instruction may map to Fast math 101 // instrinsic approximation. 102 FmReassoc = 1 << 10, // Instruction supports Fast math 103 // reassociation of operand order. 104 NoUWrap = 1 << 11, // Instruction supports binary operator 105 // no unsigned wrap. 106 NoSWrap = 1 << 12, // Instruction supports binary operator 107 // no signed wrap. 108 IsExact = 1 << 13, // Instruction supports division is 109 // known to be exact. 110 NoFPExcept = 1 << 14, // Instruction does not raise 111 // floatint-point exceptions. 112 NoMerge = 1 << 15, // Passes that drop source location info 113 // (e.g. branch folding) should skip 114 // this instruction. 115 }; 116 117 private: 118 const MCInstrDesc *MCID; // Instruction descriptor. 119 MachineBasicBlock *Parent = nullptr; // Pointer to the owning basic block. 120 121 // Operands are allocated by an ArrayRecycler. 122 MachineOperand *Operands = nullptr; // Pointer to the first operand. 123 unsigned NumOperands = 0; // Number of operands on instruction. 124 125 uint16_t Flags = 0; // Various bits of additional 126 // information about machine 127 // instruction. 128 129 uint8_t AsmPrinterFlags = 0; // Various bits of information used by 130 // the AsmPrinter to emit helpful 131 // comments. This is *not* semantic 132 // information. Do not use this for 133 // anything other than to convey comment 134 // information to AsmPrinter. 135 136 // OperandCapacity has uint8_t size, so it should be next to AsmPrinterFlags 137 // to properly pack. 138 using OperandCapacity = ArrayRecycler<MachineOperand>::Capacity; 139 OperandCapacity CapOperands; // Capacity of the Operands array. 140 141 /// Internal implementation detail class that provides out-of-line storage for 142 /// extra info used by the machine instruction when this info cannot be stored 143 /// in-line within the instruction itself. 144 /// 145 /// This has to be defined eagerly due to the implementation constraints of 146 /// `PointerSumType` where it is used. 147 class ExtraInfo final 148 : TrailingObjects<ExtraInfo, MachineMemOperand *, MCSymbol *, MDNode *> { 149 public: 150 static ExtraInfo *create(BumpPtrAllocator &Allocator, 151 ArrayRef<MachineMemOperand *> MMOs, 152 MCSymbol *PreInstrSymbol = nullptr, 153 MCSymbol *PostInstrSymbol = nullptr, 154 MDNode *HeapAllocMarker = nullptr) { 155 bool HasPreInstrSymbol = PreInstrSymbol != nullptr; 156 bool HasPostInstrSymbol = PostInstrSymbol != nullptr; 157 bool HasHeapAllocMarker = HeapAllocMarker != nullptr; 158 auto *Result = new (Allocator.Allocate( 159 totalSizeToAlloc<MachineMemOperand *, MCSymbol *, MDNode *>( 160 MMOs.size(), HasPreInstrSymbol + HasPostInstrSymbol, 161 HasHeapAllocMarker), 162 alignof(ExtraInfo))) 163 ExtraInfo(MMOs.size(), HasPreInstrSymbol, HasPostInstrSymbol, 164 HasHeapAllocMarker); 165 166 // Copy the actual data into the trailing objects. 167 std::copy(MMOs.begin(), MMOs.end(), 168 Result->getTrailingObjects<MachineMemOperand *>()); 169 170 if (HasPreInstrSymbol) 171 Result->getTrailingObjects<MCSymbol *>()[0] = PreInstrSymbol; 172 if (HasPostInstrSymbol) 173 Result->getTrailingObjects<MCSymbol *>()[HasPreInstrSymbol] = 174 PostInstrSymbol; 175 if (HasHeapAllocMarker) 176 Result->getTrailingObjects<MDNode *>()[0] = HeapAllocMarker; 177 178 return Result; 179 } 180 181 ArrayRef<MachineMemOperand *> getMMOs() const { 182 return makeArrayRef(getTrailingObjects<MachineMemOperand *>(), NumMMOs); 183 } 184 185 MCSymbol *getPreInstrSymbol() const { 186 return HasPreInstrSymbol ? getTrailingObjects<MCSymbol *>()[0] : nullptr; 187 } 188 189 MCSymbol *getPostInstrSymbol() const { 190 return HasPostInstrSymbol 191 ? getTrailingObjects<MCSymbol *>()[HasPreInstrSymbol] 192 : nullptr; 193 } 194 195 MDNode *getHeapAllocMarker() const { 196 return HasHeapAllocMarker ? getTrailingObjects<MDNode *>()[0] : nullptr; 197 } 198 199 private: 200 friend TrailingObjects; 201 202 // Description of the extra info, used to interpret the actual optional 203 // data appended. 204 // 205 // Note that this is not terribly space optimized. This leaves a great deal 206 // of flexibility to fit more in here later. 207 const int NumMMOs; 208 const bool HasPreInstrSymbol; 209 const bool HasPostInstrSymbol; 210 const bool HasHeapAllocMarker; 211 212 // Implement the `TrailingObjects` internal API. 213 size_t numTrailingObjects(OverloadToken<MachineMemOperand *>) const { 214 return NumMMOs; 215 } 216 size_t numTrailingObjects(OverloadToken<MCSymbol *>) const { 217 return HasPreInstrSymbol + HasPostInstrSymbol; 218 } 219 size_t numTrailingObjects(OverloadToken<MDNode *>) const { 220 return HasHeapAllocMarker; 221 } 222 223 // Just a boring constructor to allow us to initialize the sizes. Always use 224 // the `create` routine above. 225 ExtraInfo(int NumMMOs, bool HasPreInstrSymbol, bool HasPostInstrSymbol, 226 bool HasHeapAllocMarker) 227 : NumMMOs(NumMMOs), HasPreInstrSymbol(HasPreInstrSymbol), 228 HasPostInstrSymbol(HasPostInstrSymbol), 229 HasHeapAllocMarker(HasHeapAllocMarker) {} 230 }; 231 232 /// Enumeration of the kinds of inline extra info available. It is important 233 /// that the `MachineMemOperand` inline kind has a tag value of zero to make 234 /// it accessible as an `ArrayRef`. 235 enum ExtraInfoInlineKinds { 236 EIIK_MMO = 0, 237 EIIK_PreInstrSymbol, 238 EIIK_PostInstrSymbol, 239 EIIK_OutOfLine 240 }; 241 242 // We store extra information about the instruction here. The common case is 243 // expected to be nothing or a single pointer (typically a MMO or a symbol). 244 // We work to optimize this common case by storing it inline here rather than 245 // requiring a separate allocation, but we fall back to an allocation when 246 // multiple pointers are needed. 247 PointerSumType<ExtraInfoInlineKinds, 248 PointerSumTypeMember<EIIK_MMO, MachineMemOperand *>, 249 PointerSumTypeMember<EIIK_PreInstrSymbol, MCSymbol *>, 250 PointerSumTypeMember<EIIK_PostInstrSymbol, MCSymbol *>, 251 PointerSumTypeMember<EIIK_OutOfLine, ExtraInfo *>> 252 Info; 253 254 DebugLoc DbgLoc; // Source line information. 255 256 /// Unique instruction number. Used by DBG_INSTR_REFs to refer to the values 257 /// defined by this instruction. 258 unsigned DebugInstrNum; 259 260 // Intrusive list support 261 friend struct ilist_traits<MachineInstr>; 262 friend struct ilist_callback_traits<MachineBasicBlock>; 263 void setParent(MachineBasicBlock *P) { Parent = P; } 264 265 /// This constructor creates a copy of the given 266 /// MachineInstr in the given MachineFunction. 267 MachineInstr(MachineFunction &, const MachineInstr &); 268 269 /// This constructor create a MachineInstr and add the implicit operands. 270 /// It reserves space for number of operands specified by 271 /// MCInstrDesc. An explicit DebugLoc is supplied. 272 MachineInstr(MachineFunction &, const MCInstrDesc &TID, DebugLoc DL, 273 bool NoImp = false); 274 275 // MachineInstrs are pool-allocated and owned by MachineFunction. 276 friend class MachineFunction; 277 278 void 279 dumprImpl(const MachineRegisterInfo &MRI, unsigned Depth, unsigned MaxDepth, 280 SmallPtrSetImpl<const MachineInstr *> &AlreadySeenInstrs) const; 281 282 public: 283 MachineInstr(const MachineInstr &) = delete; 284 MachineInstr &operator=(const MachineInstr &) = delete; 285 // Use MachineFunction::DeleteMachineInstr() instead. 286 ~MachineInstr() = delete; 287 288 const MachineBasicBlock* getParent() const { return Parent; } 289 MachineBasicBlock* getParent() { return Parent; } 290 291 /// Move the instruction before \p MovePos. 292 void moveBefore(MachineInstr *MovePos); 293 294 /// Return the function that contains the basic block that this instruction 295 /// belongs to. 296 /// 297 /// Note: this is undefined behaviour if the instruction does not have a 298 /// parent. 299 const MachineFunction *getMF() const; 300 MachineFunction *getMF() { 301 return const_cast<MachineFunction *>( 302 static_cast<const MachineInstr *>(this)->getMF()); 303 } 304 305 /// Return the asm printer flags bitvector. 306 uint8_t getAsmPrinterFlags() const { return AsmPrinterFlags; } 307 308 /// Clear the AsmPrinter bitvector. 309 void clearAsmPrinterFlags() { AsmPrinterFlags = 0; } 310 311 /// Return whether an AsmPrinter flag is set. 312 bool getAsmPrinterFlag(CommentFlag Flag) const { 313 return AsmPrinterFlags & Flag; 314 } 315 316 /// Set a flag for the AsmPrinter. 317 void setAsmPrinterFlag(uint8_t Flag) { 318 AsmPrinterFlags |= Flag; 319 } 320 321 /// Clear specific AsmPrinter flags. 322 void clearAsmPrinterFlag(CommentFlag Flag) { 323 AsmPrinterFlags &= ~Flag; 324 } 325 326 /// Return the MI flags bitvector. 327 uint16_t getFlags() const { 328 return Flags; 329 } 330 331 /// Return whether an MI flag is set. 332 bool getFlag(MIFlag Flag) const { 333 return Flags & Flag; 334 } 335 336 /// Set a MI flag. 337 void setFlag(MIFlag Flag) { 338 Flags |= (uint16_t)Flag; 339 } 340 341 void setFlags(unsigned flags) { 342 // Filter out the automatically maintained flags. 343 unsigned Mask = BundledPred | BundledSucc; 344 Flags = (Flags & Mask) | (flags & ~Mask); 345 } 346 347 /// clearFlag - Clear a MI flag. 348 void clearFlag(MIFlag Flag) { 349 Flags &= ~((uint16_t)Flag); 350 } 351 352 /// Return true if MI is in a bundle (but not the first MI in a bundle). 353 /// 354 /// A bundle looks like this before it's finalized: 355 /// ---------------- 356 /// | MI | 357 /// ---------------- 358 /// | 359 /// ---------------- 360 /// | MI * | 361 /// ---------------- 362 /// | 363 /// ---------------- 364 /// | MI * | 365 /// ---------------- 366 /// In this case, the first MI starts a bundle but is not inside a bundle, the 367 /// next 2 MIs are considered "inside" the bundle. 368 /// 369 /// After a bundle is finalized, it looks like this: 370 /// ---------------- 371 /// | Bundle | 372 /// ---------------- 373 /// | 374 /// ---------------- 375 /// | MI * | 376 /// ---------------- 377 /// | 378 /// ---------------- 379 /// | MI * | 380 /// ---------------- 381 /// | 382 /// ---------------- 383 /// | MI * | 384 /// ---------------- 385 /// The first instruction has the special opcode "BUNDLE". It's not "inside" 386 /// a bundle, but the next three MIs are. 387 bool isInsideBundle() const { 388 return getFlag(BundledPred); 389 } 390 391 /// Return true if this instruction part of a bundle. This is true 392 /// if either itself or its following instruction is marked "InsideBundle". 393 bool isBundled() const { 394 return isBundledWithPred() || isBundledWithSucc(); 395 } 396 397 /// Return true if this instruction is part of a bundle, and it is not the 398 /// first instruction in the bundle. 399 bool isBundledWithPred() const { return getFlag(BundledPred); } 400 401 /// Return true if this instruction is part of a bundle, and it is not the 402 /// last instruction in the bundle. 403 bool isBundledWithSucc() const { return getFlag(BundledSucc); } 404 405 /// Bundle this instruction with its predecessor. This can be an unbundled 406 /// instruction, or it can be the first instruction in a bundle. 407 void bundleWithPred(); 408 409 /// Bundle this instruction with its successor. This can be an unbundled 410 /// instruction, or it can be the last instruction in a bundle. 411 void bundleWithSucc(); 412 413 /// Break bundle above this instruction. 414 void unbundleFromPred(); 415 416 /// Break bundle below this instruction. 417 void unbundleFromSucc(); 418 419 /// Returns the debug location id of this MachineInstr. 420 const DebugLoc &getDebugLoc() const { return DbgLoc; } 421 422 /// Return the operand containing the offset to be used if this DBG_VALUE 423 /// instruction is indirect; will be an invalid register if this value is 424 /// not indirect, and an immediate with value 0 otherwise. 425 const MachineOperand &getDebugOffset() const { 426 assert(isNonListDebugValue() && "not a DBG_VALUE"); 427 return getOperand(1); 428 } 429 MachineOperand &getDebugOffset() { 430 assert(isNonListDebugValue() && "not a DBG_VALUE"); 431 return getOperand(1); 432 } 433 434 /// Return the operand for the debug variable referenced by 435 /// this DBG_VALUE instruction. 436 const MachineOperand &getDebugVariableOp() const; 437 MachineOperand &getDebugVariableOp(); 438 439 /// Return the debug variable referenced by 440 /// this DBG_VALUE instruction. 441 const DILocalVariable *getDebugVariable() const; 442 443 /// Return the operand for the complex address expression referenced by 444 /// this DBG_VALUE instruction. 445 const MachineOperand &getDebugExpressionOp() const; 446 MachineOperand &getDebugExpressionOp(); 447 448 /// Return the complex address expression referenced by 449 /// this DBG_VALUE instruction. 450 const DIExpression *getDebugExpression() const; 451 452 /// Return the debug label referenced by 453 /// this DBG_LABEL instruction. 454 const DILabel *getDebugLabel() const; 455 456 /// Fetch the instruction number of this MachineInstr. If it does not have 457 /// one already, a new and unique number will be assigned. 458 unsigned getDebugInstrNum(); 459 460 /// Fetch instruction number of this MachineInstr -- but before it's inserted 461 /// into \p MF. Needed for transformations that create an instruction but 462 /// don't immediately insert them. 463 unsigned getDebugInstrNum(MachineFunction &MF); 464 465 /// Examine the instruction number of this MachineInstr. May be zero if 466 /// it hasn't been assigned a number yet. 467 unsigned peekDebugInstrNum() const { return DebugInstrNum; } 468 469 /// Set instruction number of this MachineInstr. Avoid using unless you're 470 /// deserializing this information. 471 void setDebugInstrNum(unsigned Num) { DebugInstrNum = Num; } 472 473 /// Drop any variable location debugging information associated with this 474 /// instruction. Use when an instruction is modified in such a way that it no 475 /// longer defines the value it used to. Variable locations using that value 476 /// will be dropped. 477 void dropDebugNumber() { DebugInstrNum = 0; } 478 479 /// Emit an error referring to the source location of this instruction. 480 /// This should only be used for inline assembly that is somehow 481 /// impossible to compile. Other errors should have been handled much 482 /// earlier. 483 /// 484 /// If this method returns, the caller should try to recover from the error. 485 void emitError(StringRef Msg) const; 486 487 /// Returns the target instruction descriptor of this MachineInstr. 488 const MCInstrDesc &getDesc() const { return *MCID; } 489 490 /// Returns the opcode of this MachineInstr. 491 unsigned getOpcode() const { return MCID->Opcode; } 492 493 /// Retuns the total number of operands. 494 unsigned getNumOperands() const { return NumOperands; } 495 496 /// Returns the total number of operands which are debug locations. 497 unsigned getNumDebugOperands() const { 498 return std::distance(debug_operands().begin(), debug_operands().end()); 499 } 500 501 const MachineOperand& getOperand(unsigned i) const { 502 assert(i < getNumOperands() && "getOperand() out of range!"); 503 return Operands[i]; 504 } 505 MachineOperand& getOperand(unsigned i) { 506 assert(i < getNumOperands() && "getOperand() out of range!"); 507 return Operands[i]; 508 } 509 510 MachineOperand &getDebugOperand(unsigned Index) { 511 assert(Index < getNumDebugOperands() && "getDebugOperand() out of range!"); 512 return *(debug_operands().begin() + Index); 513 } 514 const MachineOperand &getDebugOperand(unsigned Index) const { 515 assert(Index < getNumDebugOperands() && "getDebugOperand() out of range!"); 516 return *(debug_operands().begin() + Index); 517 } 518 519 SmallSet<Register, 4> getUsedDebugRegs() const { 520 assert(isDebugValue() && "not a DBG_VALUE*"); 521 SmallSet<Register, 4> UsedRegs; 522 for (const auto &MO : debug_operands()) 523 if (MO.isReg() && MO.getReg()) 524 UsedRegs.insert(MO.getReg()); 525 return UsedRegs; 526 } 527 528 /// Returns whether this debug value has at least one debug operand with the 529 /// register \p Reg. 530 bool hasDebugOperandForReg(Register Reg) const { 531 return any_of(debug_operands(), [Reg](const MachineOperand &Op) { 532 return Op.isReg() && Op.getReg() == Reg; 533 }); 534 } 535 536 /// Returns a range of all of the operands that correspond to a debug use of 537 /// \p Reg. 538 template <typename Operand, typename Instruction> 539 static iterator_range< 540 filter_iterator<Operand *, std::function<bool(Operand &Op)>>> 541 getDebugOperandsForReg(Instruction *MI, Register Reg) { 542 std::function<bool(Operand & Op)> OpUsesReg( 543 [Reg](Operand &Op) { return Op.isReg() && Op.getReg() == Reg; }); 544 return make_filter_range(MI->debug_operands(), OpUsesReg); 545 } 546 iterator_range<filter_iterator<const MachineOperand *, 547 std::function<bool(const MachineOperand &Op)>>> 548 getDebugOperandsForReg(Register Reg) const { 549 return MachineInstr::getDebugOperandsForReg<const MachineOperand, 550 const MachineInstr>(this, Reg); 551 } 552 iterator_range<filter_iterator<MachineOperand *, 553 std::function<bool(MachineOperand &Op)>>> 554 getDebugOperandsForReg(Register Reg) { 555 return MachineInstr::getDebugOperandsForReg<MachineOperand, MachineInstr>( 556 this, Reg); 557 } 558 559 bool isDebugOperand(const MachineOperand *Op) const { 560 return Op >= adl_begin(debug_operands()) && Op <= adl_end(debug_operands()); 561 } 562 563 unsigned getDebugOperandIndex(const MachineOperand *Op) const { 564 assert(isDebugOperand(Op) && "Expected a debug operand."); 565 return std::distance(adl_begin(debug_operands()), Op); 566 } 567 568 /// Returns the total number of definitions. 569 unsigned getNumDefs() const { 570 return getNumExplicitDefs() + MCID->getNumImplicitDefs(); 571 } 572 573 /// Returns true if the instruction has implicit definition. 574 bool hasImplicitDef() const { 575 for (unsigned I = getNumExplicitOperands(), E = getNumOperands(); 576 I != E; ++I) { 577 const MachineOperand &MO = getOperand(I); 578 if (MO.isDef() && MO.isImplicit()) 579 return true; 580 } 581 return false; 582 } 583 584 /// Returns the implicit operands number. 585 unsigned getNumImplicitOperands() const { 586 return getNumOperands() - getNumExplicitOperands(); 587 } 588 589 /// Return true if operand \p OpIdx is a subregister index. 590 bool isOperandSubregIdx(unsigned OpIdx) const { 591 assert(getOperand(OpIdx).getType() == MachineOperand::MO_Immediate && 592 "Expected MO_Immediate operand type."); 593 if (isExtractSubreg() && OpIdx == 2) 594 return true; 595 if (isInsertSubreg() && OpIdx == 3) 596 return true; 597 if (isRegSequence() && OpIdx > 1 && (OpIdx % 2) == 0) 598 return true; 599 if (isSubregToReg() && OpIdx == 3) 600 return true; 601 return false; 602 } 603 604 /// Returns the number of non-implicit operands. 605 unsigned getNumExplicitOperands() const; 606 607 /// Returns the number of non-implicit definitions. 608 unsigned getNumExplicitDefs() const; 609 610 /// iterator/begin/end - Iterate over all operands of a machine instruction. 611 using mop_iterator = MachineOperand *; 612 using const_mop_iterator = const MachineOperand *; 613 614 mop_iterator operands_begin() { return Operands; } 615 mop_iterator operands_end() { return Operands + NumOperands; } 616 617 const_mop_iterator operands_begin() const { return Operands; } 618 const_mop_iterator operands_end() const { return Operands + NumOperands; } 619 620 iterator_range<mop_iterator> operands() { 621 return make_range(operands_begin(), operands_end()); 622 } 623 iterator_range<const_mop_iterator> operands() const { 624 return make_range(operands_begin(), operands_end()); 625 } 626 iterator_range<mop_iterator> explicit_operands() { 627 return make_range(operands_begin(), 628 operands_begin() + getNumExplicitOperands()); 629 } 630 iterator_range<const_mop_iterator> explicit_operands() const { 631 return make_range(operands_begin(), 632 operands_begin() + getNumExplicitOperands()); 633 } 634 iterator_range<mop_iterator> implicit_operands() { 635 return make_range(explicit_operands().end(), operands_end()); 636 } 637 iterator_range<const_mop_iterator> implicit_operands() const { 638 return make_range(explicit_operands().end(), operands_end()); 639 } 640 /// Returns a range over all operands that are used to determine the variable 641 /// location for this DBG_VALUE instruction. 642 iterator_range<mop_iterator> debug_operands() { 643 assert(isDebugValue() && "Must be a debug value instruction."); 644 return isDebugValueList() 645 ? make_range(operands_begin() + 2, operands_end()) 646 : make_range(operands_begin(), operands_begin() + 1); 647 } 648 /// \copydoc debug_operands() 649 iterator_range<const_mop_iterator> debug_operands() const { 650 assert(isDebugValue() && "Must be a debug value instruction."); 651 return isDebugValueList() 652 ? make_range(operands_begin() + 2, operands_end()) 653 : make_range(operands_begin(), operands_begin() + 1); 654 } 655 /// Returns a range over all explicit operands that are register definitions. 656 /// Implicit definition are not included! 657 iterator_range<mop_iterator> defs() { 658 return make_range(operands_begin(), 659 operands_begin() + getNumExplicitDefs()); 660 } 661 /// \copydoc defs() 662 iterator_range<const_mop_iterator> defs() const { 663 return make_range(operands_begin(), 664 operands_begin() + getNumExplicitDefs()); 665 } 666 /// Returns a range that includes all operands that are register uses. 667 /// This may include unrelated operands which are not register uses. 668 iterator_range<mop_iterator> uses() { 669 return make_range(operands_begin() + getNumExplicitDefs(), operands_end()); 670 } 671 /// \copydoc uses() 672 iterator_range<const_mop_iterator> uses() const { 673 return make_range(operands_begin() + getNumExplicitDefs(), operands_end()); 674 } 675 iterator_range<mop_iterator> explicit_uses() { 676 return make_range(operands_begin() + getNumExplicitDefs(), 677 operands_begin() + getNumExplicitOperands()); 678 } 679 iterator_range<const_mop_iterator> explicit_uses() const { 680 return make_range(operands_begin() + getNumExplicitDefs(), 681 operands_begin() + getNumExplicitOperands()); 682 } 683 684 /// Returns the number of the operand iterator \p I points to. 685 unsigned getOperandNo(const_mop_iterator I) const { 686 return I - operands_begin(); 687 } 688 689 /// Access to memory operands of the instruction. If there are none, that does 690 /// not imply anything about whether the function accesses memory. Instead, 691 /// the caller must behave conservatively. 692 ArrayRef<MachineMemOperand *> memoperands() const { 693 if (!Info) 694 return {}; 695 696 if (Info.is<EIIK_MMO>()) 697 return makeArrayRef(Info.getAddrOfZeroTagPointer(), 1); 698 699 if (ExtraInfo *EI = Info.get<EIIK_OutOfLine>()) 700 return EI->getMMOs(); 701 702 return {}; 703 } 704 705 /// Access to memory operands of the instruction. 706 /// 707 /// If `memoperands_begin() == memoperands_end()`, that does not imply 708 /// anything about whether the function accesses memory. Instead, the caller 709 /// must behave conservatively. 710 mmo_iterator memoperands_begin() const { return memoperands().begin(); } 711 712 /// Access to memory operands of the instruction. 713 /// 714 /// If `memoperands_begin() == memoperands_end()`, that does not imply 715 /// anything about whether the function accesses memory. Instead, the caller 716 /// must behave conservatively. 717 mmo_iterator memoperands_end() const { return memoperands().end(); } 718 719 /// Return true if we don't have any memory operands which described the 720 /// memory access done by this instruction. If this is true, calling code 721 /// must be conservative. 722 bool memoperands_empty() const { return memoperands().empty(); } 723 724 /// Return true if this instruction has exactly one MachineMemOperand. 725 bool hasOneMemOperand() const { return memoperands().size() == 1; } 726 727 /// Return the number of memory operands. 728 unsigned getNumMemOperands() const { return memoperands().size(); } 729 730 /// Helper to extract a pre-instruction symbol if one has been added. 731 MCSymbol *getPreInstrSymbol() const { 732 if (!Info) 733 return nullptr; 734 if (MCSymbol *S = Info.get<EIIK_PreInstrSymbol>()) 735 return S; 736 if (ExtraInfo *EI = Info.get<EIIK_OutOfLine>()) 737 return EI->getPreInstrSymbol(); 738 739 return nullptr; 740 } 741 742 /// Helper to extract a post-instruction symbol if one has been added. 743 MCSymbol *getPostInstrSymbol() const { 744 if (!Info) 745 return nullptr; 746 if (MCSymbol *S = Info.get<EIIK_PostInstrSymbol>()) 747 return S; 748 if (ExtraInfo *EI = Info.get<EIIK_OutOfLine>()) 749 return EI->getPostInstrSymbol(); 750 751 return nullptr; 752 } 753 754 /// Helper to extract a heap alloc marker if one has been added. 755 MDNode *getHeapAllocMarker() const { 756 if (!Info) 757 return nullptr; 758 if (ExtraInfo *EI = Info.get<EIIK_OutOfLine>()) 759 return EI->getHeapAllocMarker(); 760 761 return nullptr; 762 } 763 764 /// API for querying MachineInstr properties. They are the same as MCInstrDesc 765 /// queries but they are bundle aware. 766 767 enum QueryType { 768 IgnoreBundle, // Ignore bundles 769 AnyInBundle, // Return true if any instruction in bundle has property 770 AllInBundle // Return true if all instructions in bundle have property 771 }; 772 773 /// Return true if the instruction (or in the case of a bundle, 774 /// the instructions inside the bundle) has the specified property. 775 /// The first argument is the property being queried. 776 /// The second argument indicates whether the query should look inside 777 /// instruction bundles. 778 bool hasProperty(unsigned MCFlag, QueryType Type = AnyInBundle) const { 779 assert(MCFlag < 64 && 780 "MCFlag out of range for bit mask in getFlags/hasPropertyInBundle."); 781 // Inline the fast path for unbundled or bundle-internal instructions. 782 if (Type == IgnoreBundle || !isBundled() || isBundledWithPred()) 783 return getDesc().getFlags() & (1ULL << MCFlag); 784 785 // If this is the first instruction in a bundle, take the slow path. 786 return hasPropertyInBundle(1ULL << MCFlag, Type); 787 } 788 789 /// Return true if this is an instruction that should go through the usual 790 /// legalization steps. 791 bool isPreISelOpcode(QueryType Type = IgnoreBundle) const { 792 return hasProperty(MCID::PreISelOpcode, Type); 793 } 794 795 /// Return true if this instruction can have a variable number of operands. 796 /// In this case, the variable operands will be after the normal 797 /// operands but before the implicit definitions and uses (if any are 798 /// present). 799 bool isVariadic(QueryType Type = IgnoreBundle) const { 800 return hasProperty(MCID::Variadic, Type); 801 } 802 803 /// Set if this instruction has an optional definition, e.g. 804 /// ARM instructions which can set condition code if 's' bit is set. 805 bool hasOptionalDef(QueryType Type = IgnoreBundle) const { 806 return hasProperty(MCID::HasOptionalDef, Type); 807 } 808 809 /// Return true if this is a pseudo instruction that doesn't 810 /// correspond to a real machine instruction. 811 bool isPseudo(QueryType Type = IgnoreBundle) const { 812 return hasProperty(MCID::Pseudo, Type); 813 } 814 815 bool isReturn(QueryType Type = AnyInBundle) const { 816 return hasProperty(MCID::Return, Type); 817 } 818 819 /// Return true if this is an instruction that marks the end of an EH scope, 820 /// i.e., a catchpad or a cleanuppad instruction. 821 bool isEHScopeReturn(QueryType Type = AnyInBundle) const { 822 return hasProperty(MCID::EHScopeReturn, Type); 823 } 824 825 bool isCall(QueryType Type = AnyInBundle) const { 826 return hasProperty(MCID::Call, Type); 827 } 828 829 /// Return true if this is a call instruction that may have an associated 830 /// call site entry in the debug info. 831 bool isCandidateForCallSiteEntry(QueryType Type = IgnoreBundle) const; 832 /// Return true if copying, moving, or erasing this instruction requires 833 /// updating Call Site Info (see \ref copyCallSiteInfo, \ref moveCallSiteInfo, 834 /// \ref eraseCallSiteInfo). 835 bool shouldUpdateCallSiteInfo() const; 836 837 /// Returns true if the specified instruction stops control flow 838 /// from executing the instruction immediately following it. Examples include 839 /// unconditional branches and return instructions. 840 bool isBarrier(QueryType Type = AnyInBundle) const { 841 return hasProperty(MCID::Barrier, Type); 842 } 843 844 /// Returns true if this instruction part of the terminator for a basic block. 845 /// Typically this is things like return and branch instructions. 846 /// 847 /// Various passes use this to insert code into the bottom of a basic block, 848 /// but before control flow occurs. 849 bool isTerminator(QueryType Type = AnyInBundle) const { 850 return hasProperty(MCID::Terminator, Type); 851 } 852 853 /// Returns true if this is a conditional, unconditional, or indirect branch. 854 /// Predicates below can be used to discriminate between 855 /// these cases, and the TargetInstrInfo::analyzeBranch method can be used to 856 /// get more information. 857 bool isBranch(QueryType Type = AnyInBundle) const { 858 return hasProperty(MCID::Branch, Type); 859 } 860 861 /// Return true if this is an indirect branch, such as a 862 /// branch through a register. 863 bool isIndirectBranch(QueryType Type = AnyInBundle) const { 864 return hasProperty(MCID::IndirectBranch, Type); 865 } 866 867 /// Return true if this is a branch which may fall 868 /// through to the next instruction or may transfer control flow to some other 869 /// block. The TargetInstrInfo::analyzeBranch method can be used to get more 870 /// information about this branch. 871 bool isConditionalBranch(QueryType Type = AnyInBundle) const { 872 return isBranch(Type) && !isBarrier(Type) && !isIndirectBranch(Type); 873 } 874 875 /// Return true if this is a branch which always 876 /// transfers control flow to some other block. The 877 /// TargetInstrInfo::analyzeBranch method can be used to get more information 878 /// about this branch. 879 bool isUnconditionalBranch(QueryType Type = AnyInBundle) const { 880 return isBranch(Type) && isBarrier(Type) && !isIndirectBranch(Type); 881 } 882 883 /// Return true if this instruction has a predicate operand that 884 /// controls execution. It may be set to 'always', or may be set to other 885 /// values. There are various methods in TargetInstrInfo that can be used to 886 /// control and modify the predicate in this instruction. 887 bool isPredicable(QueryType Type = AllInBundle) const { 888 // If it's a bundle than all bundled instructions must be predicable for this 889 // to return true. 890 return hasProperty(MCID::Predicable, Type); 891 } 892 893 /// Return true if this instruction is a comparison. 894 bool isCompare(QueryType Type = IgnoreBundle) const { 895 return hasProperty(MCID::Compare, Type); 896 } 897 898 /// Return true if this instruction is a move immediate 899 /// (including conditional moves) instruction. 900 bool isMoveImmediate(QueryType Type = IgnoreBundle) const { 901 return hasProperty(MCID::MoveImm, Type); 902 } 903 904 /// Return true if this instruction is a register move. 905 /// (including moving values from subreg to reg) 906 bool isMoveReg(QueryType Type = IgnoreBundle) const { 907 return hasProperty(MCID::MoveReg, Type); 908 } 909 910 /// Return true if this instruction is a bitcast instruction. 911 bool isBitcast(QueryType Type = IgnoreBundle) const { 912 return hasProperty(MCID::Bitcast, Type); 913 } 914 915 /// Return true if this instruction is a select instruction. 916 bool isSelect(QueryType Type = IgnoreBundle) const { 917 return hasProperty(MCID::Select, Type); 918 } 919 920 /// Return true if this instruction cannot be safely duplicated. 921 /// For example, if the instruction has a unique labels attached 922 /// to it, duplicating it would cause multiple definition errors. 923 bool isNotDuplicable(QueryType Type = AnyInBundle) const { 924 return hasProperty(MCID::NotDuplicable, Type); 925 } 926 927 /// Return true if this instruction is convergent. 928 /// Convergent instructions can not be made control-dependent on any 929 /// additional values. 930 bool isConvergent(QueryType Type = AnyInBundle) const { 931 if (isInlineAsm()) { 932 unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm(); 933 if (ExtraInfo & InlineAsm::Extra_IsConvergent) 934 return true; 935 } 936 return hasProperty(MCID::Convergent, Type); 937 } 938 939 /// Returns true if the specified instruction has a delay slot 940 /// which must be filled by the code generator. 941 bool hasDelaySlot(QueryType Type = AnyInBundle) const { 942 return hasProperty(MCID::DelaySlot, Type); 943 } 944 945 /// Return true for instructions that can be folded as 946 /// memory operands in other instructions. The most common use for this 947 /// is instructions that are simple loads from memory that don't modify 948 /// the loaded value in any way, but it can also be used for instructions 949 /// that can be expressed as constant-pool loads, such as V_SETALLONES 950 /// on x86, to allow them to be folded when it is beneficial. 951 /// This should only be set on instructions that return a value in their 952 /// only virtual register definition. 953 bool canFoldAsLoad(QueryType Type = IgnoreBundle) const { 954 return hasProperty(MCID::FoldableAsLoad, Type); 955 } 956 957 /// Return true if this instruction behaves 958 /// the same way as the generic REG_SEQUENCE instructions. 959 /// E.g., on ARM, 960 /// dX VMOVDRR rY, rZ 961 /// is equivalent to 962 /// dX = REG_SEQUENCE rY, ssub_0, rZ, ssub_1. 963 /// 964 /// Note that for the optimizers to be able to take advantage of 965 /// this property, TargetInstrInfo::getRegSequenceLikeInputs has to be 966 /// override accordingly. 967 bool isRegSequenceLike(QueryType Type = IgnoreBundle) const { 968 return hasProperty(MCID::RegSequence, Type); 969 } 970 971 /// Return true if this instruction behaves 972 /// the same way as the generic EXTRACT_SUBREG instructions. 973 /// E.g., on ARM, 974 /// rX, rY VMOVRRD dZ 975 /// is equivalent to two EXTRACT_SUBREG: 976 /// rX = EXTRACT_SUBREG dZ, ssub_0 977 /// rY = EXTRACT_SUBREG dZ, ssub_1 978 /// 979 /// Note that for the optimizers to be able to take advantage of 980 /// this property, TargetInstrInfo::getExtractSubregLikeInputs has to be 981 /// override accordingly. 982 bool isExtractSubregLike(QueryType Type = IgnoreBundle) const { 983 return hasProperty(MCID::ExtractSubreg, Type); 984 } 985 986 /// Return true if this instruction behaves 987 /// the same way as the generic INSERT_SUBREG instructions. 988 /// E.g., on ARM, 989 /// dX = VSETLNi32 dY, rZ, Imm 990 /// is equivalent to a INSERT_SUBREG: 991 /// dX = INSERT_SUBREG dY, rZ, translateImmToSubIdx(Imm) 992 /// 993 /// Note that for the optimizers to be able to take advantage of 994 /// this property, TargetInstrInfo::getInsertSubregLikeInputs has to be 995 /// override accordingly. 996 bool isInsertSubregLike(QueryType Type = IgnoreBundle) const { 997 return hasProperty(MCID::InsertSubreg, Type); 998 } 999 1000 //===--------------------------------------------------------------------===// 1001 // Side Effect Analysis 1002 //===--------------------------------------------------------------------===// 1003 1004 /// Return true if this instruction could possibly read memory. 1005 /// Instructions with this flag set are not necessarily simple load 1006 /// instructions, they may load a value and modify it, for example. 1007 bool mayLoad(QueryType Type = AnyInBundle) const { 1008 if (isInlineAsm()) { 1009 unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm(); 1010 if (ExtraInfo & InlineAsm::Extra_MayLoad) 1011 return true; 1012 } 1013 return hasProperty(MCID::MayLoad, Type); 1014 } 1015 1016 /// Return true if this instruction could possibly modify memory. 1017 /// Instructions with this flag set are not necessarily simple store 1018 /// instructions, they may store a modified value based on their operands, or 1019 /// may not actually modify anything, for example. 1020 bool mayStore(QueryType Type = AnyInBundle) const { 1021 if (isInlineAsm()) { 1022 unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm(); 1023 if (ExtraInfo & InlineAsm::Extra_MayStore) 1024 return true; 1025 } 1026 return hasProperty(MCID::MayStore, Type); 1027 } 1028 1029 /// Return true if this instruction could possibly read or modify memory. 1030 bool mayLoadOrStore(QueryType Type = AnyInBundle) const { 1031 return mayLoad(Type) || mayStore(Type); 1032 } 1033 1034 /// Return true if this instruction could possibly raise a floating-point 1035 /// exception. This is the case if the instruction is a floating-point 1036 /// instruction that can in principle raise an exception, as indicated 1037 /// by the MCID::MayRaiseFPException property, *and* at the same time, 1038 /// the instruction is used in a context where we expect floating-point 1039 /// exceptions are not disabled, as indicated by the NoFPExcept MI flag. 1040 bool mayRaiseFPException() const { 1041 return hasProperty(MCID::MayRaiseFPException) && 1042 !getFlag(MachineInstr::MIFlag::NoFPExcept); 1043 } 1044 1045 //===--------------------------------------------------------------------===// 1046 // Flags that indicate whether an instruction can be modified by a method. 1047 //===--------------------------------------------------------------------===// 1048 1049 /// Return true if this may be a 2- or 3-address 1050 /// instruction (of the form "X = op Y, Z, ..."), which produces the same 1051 /// result if Y and Z are exchanged. If this flag is set, then the 1052 /// TargetInstrInfo::commuteInstruction method may be used to hack on the 1053 /// instruction. 1054 /// 1055 /// Note that this flag may be set on instructions that are only commutable 1056 /// sometimes. In these cases, the call to commuteInstruction will fail. 1057 /// Also note that some instructions require non-trivial modification to 1058 /// commute them. 1059 bool isCommutable(QueryType Type = IgnoreBundle) const { 1060 return hasProperty(MCID::Commutable, Type); 1061 } 1062 1063 /// Return true if this is a 2-address instruction 1064 /// which can be changed into a 3-address instruction if needed. Doing this 1065 /// transformation can be profitable in the register allocator, because it 1066 /// means that the instruction can use a 2-address form if possible, but 1067 /// degrade into a less efficient form if the source and dest register cannot 1068 /// be assigned to the same register. For example, this allows the x86 1069 /// backend to turn a "shl reg, 3" instruction into an LEA instruction, which 1070 /// is the same speed as the shift but has bigger code size. 1071 /// 1072 /// If this returns true, then the target must implement the 1073 /// TargetInstrInfo::convertToThreeAddress method for this instruction, which 1074 /// is allowed to fail if the transformation isn't valid for this specific 1075 /// instruction (e.g. shl reg, 4 on x86). 1076 /// 1077 bool isConvertibleTo3Addr(QueryType Type = IgnoreBundle) const { 1078 return hasProperty(MCID::ConvertibleTo3Addr, Type); 1079 } 1080 1081 /// Return true if this instruction requires 1082 /// custom insertion support when the DAG scheduler is inserting it into a 1083 /// machine basic block. If this is true for the instruction, it basically 1084 /// means that it is a pseudo instruction used at SelectionDAG time that is 1085 /// expanded out into magic code by the target when MachineInstrs are formed. 1086 /// 1087 /// If this is true, the TargetLoweringInfo::InsertAtEndOfBasicBlock method 1088 /// is used to insert this into the MachineBasicBlock. 1089 bool usesCustomInsertionHook(QueryType Type = IgnoreBundle) const { 1090 return hasProperty(MCID::UsesCustomInserter, Type); 1091 } 1092 1093 /// Return true if this instruction requires *adjustment* 1094 /// after instruction selection by calling a target hook. For example, this 1095 /// can be used to fill in ARM 's' optional operand depending on whether 1096 /// the conditional flag register is used. 1097 bool hasPostISelHook(QueryType Type = IgnoreBundle) const { 1098 return hasProperty(MCID::HasPostISelHook, Type); 1099 } 1100 1101 /// Returns true if this instruction is a candidate for remat. 1102 /// This flag is deprecated, please don't use it anymore. If this 1103 /// flag is set, the isReallyTriviallyReMaterializable() method is called to 1104 /// verify the instruction is really rematable. 1105 bool isRematerializable(QueryType Type = AllInBundle) const { 1106 // It's only possible to re-mat a bundle if all bundled instructions are 1107 // re-materializable. 1108 return hasProperty(MCID::Rematerializable, Type); 1109 } 1110 1111 /// Returns true if this instruction has the same cost (or less) than a move 1112 /// instruction. This is useful during certain types of optimizations 1113 /// (e.g., remat during two-address conversion or machine licm) 1114 /// where we would like to remat or hoist the instruction, but not if it costs 1115 /// more than moving the instruction into the appropriate register. Note, we 1116 /// are not marking copies from and to the same register class with this flag. 1117 bool isAsCheapAsAMove(QueryType Type = AllInBundle) const { 1118 // Only returns true for a bundle if all bundled instructions are cheap. 1119 return hasProperty(MCID::CheapAsAMove, Type); 1120 } 1121 1122 /// Returns true if this instruction source operands 1123 /// have special register allocation requirements that are not captured by the 1124 /// operand register classes. e.g. ARM::STRD's two source registers must be an 1125 /// even / odd pair, ARM::STM registers have to be in ascending order. 1126 /// Post-register allocation passes should not attempt to change allocations 1127 /// for sources of instructions with this flag. 1128 bool hasExtraSrcRegAllocReq(QueryType Type = AnyInBundle) const { 1129 return hasProperty(MCID::ExtraSrcRegAllocReq, Type); 1130 } 1131 1132 /// Returns true if this instruction def operands 1133 /// have special register allocation requirements that are not captured by the 1134 /// operand register classes. e.g. ARM::LDRD's two def registers must be an 1135 /// even / odd pair, ARM::LDM registers have to be in ascending order. 1136 /// Post-register allocation passes should not attempt to change allocations 1137 /// for definitions of instructions with this flag. 1138 bool hasExtraDefRegAllocReq(QueryType Type = AnyInBundle) const { 1139 return hasProperty(MCID::ExtraDefRegAllocReq, Type); 1140 } 1141 1142 enum MICheckType { 1143 CheckDefs, // Check all operands for equality 1144 CheckKillDead, // Check all operands including kill / dead markers 1145 IgnoreDefs, // Ignore all definitions 1146 IgnoreVRegDefs // Ignore virtual register definitions 1147 }; 1148 1149 /// Return true if this instruction is identical to \p Other. 1150 /// Two instructions are identical if they have the same opcode and all their 1151 /// operands are identical (with respect to MachineOperand::isIdenticalTo()). 1152 /// Note that this means liveness related flags (dead, undef, kill) do not 1153 /// affect the notion of identical. 1154 bool isIdenticalTo(const MachineInstr &Other, 1155 MICheckType Check = CheckDefs) const; 1156 1157 /// Unlink 'this' from the containing basic block, and return it without 1158 /// deleting it. 1159 /// 1160 /// This function can not be used on bundled instructions, use 1161 /// removeFromBundle() to remove individual instructions from a bundle. 1162 MachineInstr *removeFromParent(); 1163 1164 /// Unlink this instruction from its basic block and return it without 1165 /// deleting it. 1166 /// 1167 /// If the instruction is part of a bundle, the other instructions in the 1168 /// bundle remain bundled. 1169 MachineInstr *removeFromBundle(); 1170 1171 /// Unlink 'this' from the containing basic block and delete it. 1172 /// 1173 /// If this instruction is the header of a bundle, the whole bundle is erased. 1174 /// This function can not be used for instructions inside a bundle, use 1175 /// eraseFromBundle() to erase individual bundled instructions. 1176 void eraseFromParent(); 1177 1178 /// Unlink 'this' form its basic block and delete it. 1179 /// 1180 /// If the instruction is part of a bundle, the other instructions in the 1181 /// bundle remain bundled. 1182 void eraseFromBundle(); 1183 1184 bool isEHLabel() const { return getOpcode() == TargetOpcode::EH_LABEL; } 1185 bool isGCLabel() const { return getOpcode() == TargetOpcode::GC_LABEL; } 1186 bool isAnnotationLabel() const { 1187 return getOpcode() == TargetOpcode::ANNOTATION_LABEL; 1188 } 1189 1190 /// Returns true if the MachineInstr represents a label. 1191 bool isLabel() const { 1192 return isEHLabel() || isGCLabel() || isAnnotationLabel(); 1193 } 1194 1195 bool isCFIInstruction() const { 1196 return getOpcode() == TargetOpcode::CFI_INSTRUCTION; 1197 } 1198 1199 bool isPseudoProbe() const { 1200 return getOpcode() == TargetOpcode::PSEUDO_PROBE; 1201 } 1202 1203 // True if the instruction represents a position in the function. 1204 bool isPosition() const { return isLabel() || isCFIInstruction(); } 1205 1206 bool isNonListDebugValue() const { 1207 return getOpcode() == TargetOpcode::DBG_VALUE; 1208 } 1209 bool isDebugValueList() const { 1210 return getOpcode() == TargetOpcode::DBG_VALUE_LIST; 1211 } 1212 bool isDebugValue() const { 1213 return isNonListDebugValue() || isDebugValueList(); 1214 } 1215 bool isDebugLabel() const { return getOpcode() == TargetOpcode::DBG_LABEL; } 1216 bool isDebugRef() const { return getOpcode() == TargetOpcode::DBG_INSTR_REF; } 1217 bool isDebugPHI() const { return getOpcode() == TargetOpcode::DBG_PHI; } 1218 bool isDebugInstr() const { 1219 return isDebugValue() || isDebugLabel() || isDebugRef() || isDebugPHI(); 1220 } 1221 bool isDebugOrPseudoInstr() const { 1222 return isDebugInstr() || isPseudoProbe(); 1223 } 1224 1225 bool isDebugOffsetImm() const { 1226 return isNonListDebugValue() && getDebugOffset().isImm(); 1227 } 1228 1229 /// A DBG_VALUE is indirect iff the location operand is a register and 1230 /// the offset operand is an immediate. 1231 bool isIndirectDebugValue() const { 1232 return isDebugOffsetImm() && getDebugOperand(0).isReg(); 1233 } 1234 1235 /// A DBG_VALUE is an entry value iff its debug expression contains the 1236 /// DW_OP_LLVM_entry_value operation. 1237 bool isDebugEntryValue() const; 1238 1239 /// Return true if the instruction is a debug value which describes a part of 1240 /// a variable as unavailable. 1241 bool isUndefDebugValue() const { 1242 if (!isDebugValue()) 1243 return false; 1244 // If any $noreg locations are given, this DV is undef. 1245 for (const MachineOperand &Op : debug_operands()) 1246 if (Op.isReg() && !Op.getReg().isValid()) 1247 return true; 1248 return false; 1249 } 1250 1251 bool isPHI() const { 1252 return getOpcode() == TargetOpcode::PHI || 1253 getOpcode() == TargetOpcode::G_PHI; 1254 } 1255 bool isKill() const { return getOpcode() == TargetOpcode::KILL; } 1256 bool isImplicitDef() const { return getOpcode()==TargetOpcode::IMPLICIT_DEF; } 1257 bool isInlineAsm() const { 1258 return getOpcode() == TargetOpcode::INLINEASM || 1259 getOpcode() == TargetOpcode::INLINEASM_BR; 1260 } 1261 1262 /// FIXME: Seems like a layering violation that the AsmDialect, which is X86 1263 /// specific, be attached to a generic MachineInstr. 1264 bool isMSInlineAsm() const { 1265 return isInlineAsm() && getInlineAsmDialect() == InlineAsm::AD_Intel; 1266 } 1267 1268 bool isStackAligningInlineAsm() const; 1269 InlineAsm::AsmDialect getInlineAsmDialect() const; 1270 1271 bool isInsertSubreg() const { 1272 return getOpcode() == TargetOpcode::INSERT_SUBREG; 1273 } 1274 1275 bool isSubregToReg() const { 1276 return getOpcode() == TargetOpcode::SUBREG_TO_REG; 1277 } 1278 1279 bool isRegSequence() const { 1280 return getOpcode() == TargetOpcode::REG_SEQUENCE; 1281 } 1282 1283 bool isBundle() const { 1284 return getOpcode() == TargetOpcode::BUNDLE; 1285 } 1286 1287 bool isCopy() const { 1288 return getOpcode() == TargetOpcode::COPY; 1289 } 1290 1291 bool isFullCopy() const { 1292 return isCopy() && !getOperand(0).getSubReg() && !getOperand(1).getSubReg(); 1293 } 1294 1295 bool isExtractSubreg() const { 1296 return getOpcode() == TargetOpcode::EXTRACT_SUBREG; 1297 } 1298 1299 /// Return true if the instruction behaves like a copy. 1300 /// This does not include native copy instructions. 1301 bool isCopyLike() const { 1302 return isCopy() || isSubregToReg(); 1303 } 1304 1305 /// Return true is the instruction is an identity copy. 1306 bool isIdentityCopy() const { 1307 return isCopy() && getOperand(0).getReg() == getOperand(1).getReg() && 1308 getOperand(0).getSubReg() == getOperand(1).getSubReg(); 1309 } 1310 1311 /// Return true if this instruction doesn't produce any output in the form of 1312 /// executable instructions. 1313 bool isMetaInstruction() const { 1314 switch (getOpcode()) { 1315 default: 1316 return false; 1317 case TargetOpcode::IMPLICIT_DEF: 1318 case TargetOpcode::KILL: 1319 case TargetOpcode::CFI_INSTRUCTION: 1320 case TargetOpcode::EH_LABEL: 1321 case TargetOpcode::GC_LABEL: 1322 case TargetOpcode::DBG_VALUE: 1323 case TargetOpcode::DBG_VALUE_LIST: 1324 case TargetOpcode::DBG_INSTR_REF: 1325 case TargetOpcode::DBG_PHI: 1326 case TargetOpcode::DBG_LABEL: 1327 case TargetOpcode::LIFETIME_START: 1328 case TargetOpcode::LIFETIME_END: 1329 case TargetOpcode::PSEUDO_PROBE: 1330 case TargetOpcode::ARITH_FENCE: 1331 return true; 1332 } 1333 } 1334 1335 /// Return true if this is a transient instruction that is either very likely 1336 /// to be eliminated during register allocation (such as copy-like 1337 /// instructions), or if this instruction doesn't have an execution-time cost. 1338 bool isTransient() const { 1339 switch (getOpcode()) { 1340 default: 1341 return isMetaInstruction(); 1342 // Copy-like instructions are usually eliminated during register allocation. 1343 case TargetOpcode::PHI: 1344 case TargetOpcode::G_PHI: 1345 case TargetOpcode::COPY: 1346 case TargetOpcode::INSERT_SUBREG: 1347 case TargetOpcode::SUBREG_TO_REG: 1348 case TargetOpcode::REG_SEQUENCE: 1349 return true; 1350 } 1351 } 1352 1353 /// Return the number of instructions inside the MI bundle, excluding the 1354 /// bundle header. 1355 /// 1356 /// This is the number of instructions that MachineBasicBlock::iterator 1357 /// skips, 0 for unbundled instructions. 1358 unsigned getBundleSize() const; 1359 1360 /// Return true if the MachineInstr reads the specified register. 1361 /// If TargetRegisterInfo is passed, then it also checks if there 1362 /// is a read of a super-register. 1363 /// This does not count partial redefines of virtual registers as reads: 1364 /// %reg1024:6 = OP. 1365 bool readsRegister(Register Reg, 1366 const TargetRegisterInfo *TRI = nullptr) const { 1367 return findRegisterUseOperandIdx(Reg, false, TRI) != -1; 1368 } 1369 1370 /// Return true if the MachineInstr reads the specified virtual register. 1371 /// Take into account that a partial define is a 1372 /// read-modify-write operation. 1373 bool readsVirtualRegister(Register Reg) const { 1374 return readsWritesVirtualRegister(Reg).first; 1375 } 1376 1377 /// Return a pair of bools (reads, writes) indicating if this instruction 1378 /// reads or writes Reg. This also considers partial defines. 1379 /// If Ops is not null, all operand indices for Reg are added. 1380 std::pair<bool,bool> readsWritesVirtualRegister(Register Reg, 1381 SmallVectorImpl<unsigned> *Ops = nullptr) const; 1382 1383 /// Return true if the MachineInstr kills the specified register. 1384 /// If TargetRegisterInfo is passed, then it also checks if there is 1385 /// a kill of a super-register. 1386 bool killsRegister(Register Reg, 1387 const TargetRegisterInfo *TRI = nullptr) const { 1388 return findRegisterUseOperandIdx(Reg, true, TRI) != -1; 1389 } 1390 1391 /// Return true if the MachineInstr fully defines the specified register. 1392 /// If TargetRegisterInfo is passed, then it also checks 1393 /// if there is a def of a super-register. 1394 /// NOTE: It's ignoring subreg indices on virtual registers. 1395 bool definesRegister(Register Reg, 1396 const TargetRegisterInfo *TRI = nullptr) const { 1397 return findRegisterDefOperandIdx(Reg, false, false, TRI) != -1; 1398 } 1399 1400 /// Return true if the MachineInstr modifies (fully define or partially 1401 /// define) the specified register. 1402 /// NOTE: It's ignoring subreg indices on virtual registers. 1403 bool modifiesRegister(Register Reg, 1404 const TargetRegisterInfo *TRI = nullptr) const { 1405 return findRegisterDefOperandIdx(Reg, false, true, TRI) != -1; 1406 } 1407 1408 /// Returns true if the register is dead in this machine instruction. 1409 /// If TargetRegisterInfo is passed, then it also checks 1410 /// if there is a dead def of a super-register. 1411 bool registerDefIsDead(Register Reg, 1412 const TargetRegisterInfo *TRI = nullptr) const { 1413 return findRegisterDefOperandIdx(Reg, true, false, TRI) != -1; 1414 } 1415 1416 /// Returns true if the MachineInstr has an implicit-use operand of exactly 1417 /// the given register (not considering sub/super-registers). 1418 bool hasRegisterImplicitUseOperand(Register Reg) const; 1419 1420 /// Returns the operand index that is a use of the specific register or -1 1421 /// if it is not found. It further tightens the search criteria to a use 1422 /// that kills the register if isKill is true. 1423 int findRegisterUseOperandIdx(Register Reg, bool isKill = false, 1424 const TargetRegisterInfo *TRI = nullptr) const; 1425 1426 /// Wrapper for findRegisterUseOperandIdx, it returns 1427 /// a pointer to the MachineOperand rather than an index. 1428 MachineOperand *findRegisterUseOperand(Register Reg, bool isKill = false, 1429 const TargetRegisterInfo *TRI = nullptr) { 1430 int Idx = findRegisterUseOperandIdx(Reg, isKill, TRI); 1431 return (Idx == -1) ? nullptr : &getOperand(Idx); 1432 } 1433 1434 const MachineOperand *findRegisterUseOperand( 1435 Register Reg, bool isKill = false, 1436 const TargetRegisterInfo *TRI = nullptr) const { 1437 return const_cast<MachineInstr *>(this)-> 1438 findRegisterUseOperand(Reg, isKill, TRI); 1439 } 1440 1441 /// Returns the operand index that is a def of the specified register or 1442 /// -1 if it is not found. If isDead is true, defs that are not dead are 1443 /// skipped. If Overlap is true, then it also looks for defs that merely 1444 /// overlap the specified register. If TargetRegisterInfo is non-null, 1445 /// then it also checks if there is a def of a super-register. 1446 /// This may also return a register mask operand when Overlap is true. 1447 int findRegisterDefOperandIdx(Register Reg, 1448 bool isDead = false, bool Overlap = false, 1449 const TargetRegisterInfo *TRI = nullptr) const; 1450 1451 /// Wrapper for findRegisterDefOperandIdx, it returns 1452 /// a pointer to the MachineOperand rather than an index. 1453 MachineOperand * 1454 findRegisterDefOperand(Register Reg, bool isDead = false, 1455 bool Overlap = false, 1456 const TargetRegisterInfo *TRI = nullptr) { 1457 int Idx = findRegisterDefOperandIdx(Reg, isDead, Overlap, TRI); 1458 return (Idx == -1) ? nullptr : &getOperand(Idx); 1459 } 1460 1461 const MachineOperand * 1462 findRegisterDefOperand(Register Reg, bool isDead = false, 1463 bool Overlap = false, 1464 const TargetRegisterInfo *TRI = nullptr) const { 1465 return const_cast<MachineInstr *>(this)->findRegisterDefOperand( 1466 Reg, isDead, Overlap, TRI); 1467 } 1468 1469 /// Find the index of the first operand in the 1470 /// operand list that is used to represent the predicate. It returns -1 if 1471 /// none is found. 1472 int findFirstPredOperandIdx() const; 1473 1474 /// Find the index of the flag word operand that 1475 /// corresponds to operand OpIdx on an inline asm instruction. Returns -1 if 1476 /// getOperand(OpIdx) does not belong to an inline asm operand group. 1477 /// 1478 /// If GroupNo is not NULL, it will receive the number of the operand group 1479 /// containing OpIdx. 1480 int findInlineAsmFlagIdx(unsigned OpIdx, unsigned *GroupNo = nullptr) const; 1481 1482 /// Compute the static register class constraint for operand OpIdx. 1483 /// For normal instructions, this is derived from the MCInstrDesc. 1484 /// For inline assembly it is derived from the flag words. 1485 /// 1486 /// Returns NULL if the static register class constraint cannot be 1487 /// determined. 1488 const TargetRegisterClass* 1489 getRegClassConstraint(unsigned OpIdx, 1490 const TargetInstrInfo *TII, 1491 const TargetRegisterInfo *TRI) const; 1492 1493 /// Applies the constraints (def/use) implied by this MI on \p Reg to 1494 /// the given \p CurRC. 1495 /// If \p ExploreBundle is set and MI is part of a bundle, all the 1496 /// instructions inside the bundle will be taken into account. In other words, 1497 /// this method accumulates all the constraints of the operand of this MI and 1498 /// the related bundle if MI is a bundle or inside a bundle. 1499 /// 1500 /// Returns the register class that satisfies both \p CurRC and the 1501 /// constraints set by MI. Returns NULL if such a register class does not 1502 /// exist. 1503 /// 1504 /// \pre CurRC must not be NULL. 1505 const TargetRegisterClass *getRegClassConstraintEffectForVReg( 1506 Register Reg, const TargetRegisterClass *CurRC, 1507 const TargetInstrInfo *TII, const TargetRegisterInfo *TRI, 1508 bool ExploreBundle = false) const; 1509 1510 /// Applies the constraints (def/use) implied by the \p OpIdx operand 1511 /// to the given \p CurRC. 1512 /// 1513 /// Returns the register class that satisfies both \p CurRC and the 1514 /// constraints set by \p OpIdx MI. Returns NULL if such a register class 1515 /// does not exist. 1516 /// 1517 /// \pre CurRC must not be NULL. 1518 /// \pre The operand at \p OpIdx must be a register. 1519 const TargetRegisterClass * 1520 getRegClassConstraintEffect(unsigned OpIdx, const TargetRegisterClass *CurRC, 1521 const TargetInstrInfo *TII, 1522 const TargetRegisterInfo *TRI) const; 1523 1524 /// Add a tie between the register operands at DefIdx and UseIdx. 1525 /// The tie will cause the register allocator to ensure that the two 1526 /// operands are assigned the same physical register. 1527 /// 1528 /// Tied operands are managed automatically for explicit operands in the 1529 /// MCInstrDesc. This method is for exceptional cases like inline asm. 1530 void tieOperands(unsigned DefIdx, unsigned UseIdx); 1531 1532 /// Given the index of a tied register operand, find the 1533 /// operand it is tied to. Defs are tied to uses and vice versa. Returns the 1534 /// index of the tied operand which must exist. 1535 unsigned findTiedOperandIdx(unsigned OpIdx) const; 1536 1537 /// Given the index of a register def operand, 1538 /// check if the register def is tied to a source operand, due to either 1539 /// two-address elimination or inline assembly constraints. Returns the 1540 /// first tied use operand index by reference if UseOpIdx is not null. 1541 bool isRegTiedToUseOperand(unsigned DefOpIdx, 1542 unsigned *UseOpIdx = nullptr) const { 1543 const MachineOperand &MO = getOperand(DefOpIdx); 1544 if (!MO.isReg() || !MO.isDef() || !MO.isTied()) 1545 return false; 1546 if (UseOpIdx) 1547 *UseOpIdx = findTiedOperandIdx(DefOpIdx); 1548 return true; 1549 } 1550 1551 /// Return true if the use operand of the specified index is tied to a def 1552 /// operand. It also returns the def operand index by reference if DefOpIdx 1553 /// is not null. 1554 bool isRegTiedToDefOperand(unsigned UseOpIdx, 1555 unsigned *DefOpIdx = nullptr) const { 1556 const MachineOperand &MO = getOperand(UseOpIdx); 1557 if (!MO.isReg() || !MO.isUse() || !MO.isTied()) 1558 return false; 1559 if (DefOpIdx) 1560 *DefOpIdx = findTiedOperandIdx(UseOpIdx); 1561 return true; 1562 } 1563 1564 /// Clears kill flags on all operands. 1565 void clearKillInfo(); 1566 1567 /// Replace all occurrences of FromReg with ToReg:SubIdx, 1568 /// properly composing subreg indices where necessary. 1569 void substituteRegister(Register FromReg, Register ToReg, unsigned SubIdx, 1570 const TargetRegisterInfo &RegInfo); 1571 1572 /// We have determined MI kills a register. Look for the 1573 /// operand that uses it and mark it as IsKill. If AddIfNotFound is true, 1574 /// add a implicit operand if it's not found. Returns true if the operand 1575 /// exists / is added. 1576 bool addRegisterKilled(Register IncomingReg, 1577 const TargetRegisterInfo *RegInfo, 1578 bool AddIfNotFound = false); 1579 1580 /// Clear all kill flags affecting Reg. If RegInfo is provided, this includes 1581 /// all aliasing registers. 1582 void clearRegisterKills(Register Reg, const TargetRegisterInfo *RegInfo); 1583 1584 /// We have determined MI defined a register without a use. 1585 /// Look for the operand that defines it and mark it as IsDead. If 1586 /// AddIfNotFound is true, add a implicit operand if it's not found. Returns 1587 /// true if the operand exists / is added. 1588 bool addRegisterDead(Register Reg, const TargetRegisterInfo *RegInfo, 1589 bool AddIfNotFound = false); 1590 1591 /// Clear all dead flags on operands defining register @p Reg. 1592 void clearRegisterDeads(Register Reg); 1593 1594 /// Mark all subregister defs of register @p Reg with the undef flag. 1595 /// This function is used when we determined to have a subregister def in an 1596 /// otherwise undefined super register. 1597 void setRegisterDefReadUndef(Register Reg, bool IsUndef = true); 1598 1599 /// We have determined MI defines a register. Make sure there is an operand 1600 /// defining Reg. 1601 void addRegisterDefined(Register Reg, 1602 const TargetRegisterInfo *RegInfo = nullptr); 1603 1604 /// Mark every physreg used by this instruction as 1605 /// dead except those in the UsedRegs list. 1606 /// 1607 /// On instructions with register mask operands, also add implicit-def 1608 /// operands for all registers in UsedRegs. 1609 void setPhysRegsDeadExcept(ArrayRef<Register> UsedRegs, 1610 const TargetRegisterInfo &TRI); 1611 1612 /// Return true if it is safe to move this instruction. If 1613 /// SawStore is set to true, it means that there is a store (or call) between 1614 /// the instruction's location and its intended destination. 1615 bool isSafeToMove(AAResults *AA, bool &SawStore) const; 1616 1617 /// Returns true if this instruction's memory access aliases the memory 1618 /// access of Other. 1619 // 1620 /// Assumes any physical registers used to compute addresses 1621 /// have the same value for both instructions. Returns false if neither 1622 /// instruction writes to memory. 1623 /// 1624 /// @param AA Optional alias analysis, used to compare memory operands. 1625 /// @param Other MachineInstr to check aliasing against. 1626 /// @param UseTBAA Whether to pass TBAA information to alias analysis. 1627 bool mayAlias(AAResults *AA, const MachineInstr &Other, bool UseTBAA) const; 1628 1629 /// Return true if this instruction may have an ordered 1630 /// or volatile memory reference, or if the information describing the memory 1631 /// reference is not available. Return false if it is known to have no 1632 /// ordered or volatile memory references. 1633 bool hasOrderedMemoryRef() const; 1634 1635 /// Return true if this load instruction never traps and points to a memory 1636 /// location whose value doesn't change during the execution of this function. 1637 /// 1638 /// Examples include loading a value from the constant pool or from the 1639 /// argument area of a function (if it does not change). If the instruction 1640 /// does multiple loads, this returns true only if all of the loads are 1641 /// dereferenceable and invariant. 1642 bool isDereferenceableInvariantLoad(AAResults *AA) const; 1643 1644 /// If the specified instruction is a PHI that always merges together the 1645 /// same virtual register, return the register, otherwise return 0. 1646 unsigned isConstantValuePHI() const; 1647 1648 /// Return true if this instruction has side effects that are not modeled 1649 /// by mayLoad / mayStore, etc. 1650 /// For all instructions, the property is encoded in MCInstrDesc::Flags 1651 /// (see MCInstrDesc::hasUnmodeledSideEffects(). The only exception is 1652 /// INLINEASM instruction, in which case the side effect property is encoded 1653 /// in one of its operands (see InlineAsm::Extra_HasSideEffect). 1654 /// 1655 bool hasUnmodeledSideEffects() const; 1656 1657 /// Returns true if it is illegal to fold a load across this instruction. 1658 bool isLoadFoldBarrier() const; 1659 1660 /// Return true if all the defs of this instruction are dead. 1661 bool allDefsAreDead() const; 1662 1663 /// Return a valid size if the instruction is a spill instruction. 1664 Optional<unsigned> getSpillSize(const TargetInstrInfo *TII) const; 1665 1666 /// Return a valid size if the instruction is a folded spill instruction. 1667 Optional<unsigned> getFoldedSpillSize(const TargetInstrInfo *TII) const; 1668 1669 /// Return a valid size if the instruction is a restore instruction. 1670 Optional<unsigned> getRestoreSize(const TargetInstrInfo *TII) const; 1671 1672 /// Return a valid size if the instruction is a folded restore instruction. 1673 Optional<unsigned> 1674 getFoldedRestoreSize(const TargetInstrInfo *TII) const; 1675 1676 /// Copy implicit register operands from specified 1677 /// instruction to this instruction. 1678 void copyImplicitOps(MachineFunction &MF, const MachineInstr &MI); 1679 1680 /// Debugging support 1681 /// @{ 1682 /// Determine the generic type to be printed (if needed) on uses and defs. 1683 LLT getTypeToPrint(unsigned OpIdx, SmallBitVector &PrintedTypes, 1684 const MachineRegisterInfo &MRI) const; 1685 1686 /// Return true when an instruction has tied register that can't be determined 1687 /// by the instruction's descriptor. This is useful for MIR printing, to 1688 /// determine whether we need to print the ties or not. 1689 bool hasComplexRegisterTies() const; 1690 1691 /// Print this MI to \p OS. 1692 /// Don't print information that can be inferred from other instructions if 1693 /// \p IsStandalone is false. It is usually true when only a fragment of the 1694 /// function is printed. 1695 /// Only print the defs and the opcode if \p SkipOpers is true. 1696 /// Otherwise, also print operands if \p SkipDebugLoc is true. 1697 /// Otherwise, also print the debug loc, with a terminating newline. 1698 /// \p TII is used to print the opcode name. If it's not present, but the 1699 /// MI is in a function, the opcode will be printed using the function's TII. 1700 void print(raw_ostream &OS, bool IsStandalone = true, bool SkipOpers = false, 1701 bool SkipDebugLoc = false, bool AddNewLine = true, 1702 const TargetInstrInfo *TII = nullptr) const; 1703 void print(raw_ostream &OS, ModuleSlotTracker &MST, bool IsStandalone = true, 1704 bool SkipOpers = false, bool SkipDebugLoc = false, 1705 bool AddNewLine = true, 1706 const TargetInstrInfo *TII = nullptr) const; 1707 void dump() const; 1708 /// Print on dbgs() the current instruction and the instructions defining its 1709 /// operands and so on until we reach \p MaxDepth. 1710 void dumpr(const MachineRegisterInfo &MRI, 1711 unsigned MaxDepth = UINT_MAX) const; 1712 /// @} 1713 1714 //===--------------------------------------------------------------------===// 1715 // Accessors used to build up machine instructions. 1716 1717 /// Add the specified operand to the instruction. If it is an implicit 1718 /// operand, it is added to the end of the operand list. If it is an 1719 /// explicit operand it is added at the end of the explicit operand list 1720 /// (before the first implicit operand). 1721 /// 1722 /// MF must be the machine function that was used to allocate this 1723 /// instruction. 1724 /// 1725 /// MachineInstrBuilder provides a more convenient interface for creating 1726 /// instructions and adding operands. 1727 void addOperand(MachineFunction &MF, const MachineOperand &Op); 1728 1729 /// Add an operand without providing an MF reference. This only works for 1730 /// instructions that are inserted in a basic block. 1731 /// 1732 /// MachineInstrBuilder and the two-argument addOperand(MF, MO) should be 1733 /// preferred. 1734 void addOperand(const MachineOperand &Op); 1735 1736 /// Replace the instruction descriptor (thus opcode) of 1737 /// the current instruction with a new one. 1738 void setDesc(const MCInstrDesc &TID) { MCID = &TID; } 1739 1740 /// Replace current source information with new such. 1741 /// Avoid using this, the constructor argument is preferable. 1742 void setDebugLoc(DebugLoc DL) { 1743 DbgLoc = std::move(DL); 1744 assert(DbgLoc.hasTrivialDestructor() && "Expected trivial destructor"); 1745 } 1746 1747 /// Erase an operand from an instruction, leaving it with one 1748 /// fewer operand than it started with. 1749 void removeOperand(unsigned OpNo); 1750 1751 /// Clear this MachineInstr's memory reference descriptor list. This resets 1752 /// the memrefs to their most conservative state. This should be used only 1753 /// as a last resort since it greatly pessimizes our knowledge of the memory 1754 /// access performed by the instruction. 1755 void dropMemRefs(MachineFunction &MF); 1756 1757 /// Assign this MachineInstr's memory reference descriptor list. 1758 /// 1759 /// Unlike other methods, this *will* allocate them into a new array 1760 /// associated with the provided `MachineFunction`. 1761 void setMemRefs(MachineFunction &MF, ArrayRef<MachineMemOperand *> MemRefs); 1762 1763 /// Add a MachineMemOperand to the machine instruction. 1764 /// This function should be used only occasionally. The setMemRefs function 1765 /// is the primary method for setting up a MachineInstr's MemRefs list. 1766 void addMemOperand(MachineFunction &MF, MachineMemOperand *MO); 1767 1768 /// Clone another MachineInstr's memory reference descriptor list and replace 1769 /// ours with it. 1770 /// 1771 /// Note that `*this` may be the incoming MI! 1772 /// 1773 /// Prefer this API whenever possible as it can avoid allocations in common 1774 /// cases. 1775 void cloneMemRefs(MachineFunction &MF, const MachineInstr &MI); 1776 1777 /// Clone the merge of multiple MachineInstrs' memory reference descriptors 1778 /// list and replace ours with it. 1779 /// 1780 /// Note that `*this` may be one of the incoming MIs! 1781 /// 1782 /// Prefer this API whenever possible as it can avoid allocations in common 1783 /// cases. 1784 void cloneMergedMemRefs(MachineFunction &MF, 1785 ArrayRef<const MachineInstr *> MIs); 1786 1787 /// Set a symbol that will be emitted just prior to the instruction itself. 1788 /// 1789 /// Setting this to a null pointer will remove any such symbol. 1790 /// 1791 /// FIXME: This is not fully implemented yet. 1792 void setPreInstrSymbol(MachineFunction &MF, MCSymbol *Symbol); 1793 1794 /// Set a symbol that will be emitted just after the instruction itself. 1795 /// 1796 /// Setting this to a null pointer will remove any such symbol. 1797 /// 1798 /// FIXME: This is not fully implemented yet. 1799 void setPostInstrSymbol(MachineFunction &MF, MCSymbol *Symbol); 1800 1801 /// Clone another MachineInstr's pre- and post- instruction symbols and 1802 /// replace ours with it. 1803 void cloneInstrSymbols(MachineFunction &MF, const MachineInstr &MI); 1804 1805 /// Set a marker on instructions that denotes where we should create and emit 1806 /// heap alloc site labels. This waits until after instruction selection and 1807 /// optimizations to create the label, so it should still work if the 1808 /// instruction is removed or duplicated. 1809 void setHeapAllocMarker(MachineFunction &MF, MDNode *MD); 1810 1811 /// Return the MIFlags which represent both MachineInstrs. This 1812 /// should be used when merging two MachineInstrs into one. This routine does 1813 /// not modify the MIFlags of this MachineInstr. 1814 uint16_t mergeFlagsWith(const MachineInstr& Other) const; 1815 1816 static uint16_t copyFlagsFromInstruction(const Instruction &I); 1817 1818 /// Copy all flags to MachineInst MIFlags 1819 void copyIRFlags(const Instruction &I); 1820 1821 /// Break any tie involving OpIdx. 1822 void untieRegOperand(unsigned OpIdx) { 1823 MachineOperand &MO = getOperand(OpIdx); 1824 if (MO.isReg() && MO.isTied()) { 1825 getOperand(findTiedOperandIdx(OpIdx)).TiedTo = 0; 1826 MO.TiedTo = 0; 1827 } 1828 } 1829 1830 /// Add all implicit def and use operands to this instruction. 1831 void addImplicitDefUseOperands(MachineFunction &MF); 1832 1833 /// Scan instructions immediately following MI and collect any matching 1834 /// DBG_VALUEs. 1835 void collectDebugValues(SmallVectorImpl<MachineInstr *> &DbgValues); 1836 1837 /// Find all DBG_VALUEs that point to the register def in this instruction 1838 /// and point them to \p Reg instead. 1839 void changeDebugValuesDefReg(Register Reg); 1840 1841 /// Returns the Intrinsic::ID for this instruction. 1842 /// \pre Must have an intrinsic ID operand. 1843 unsigned getIntrinsicID() const { 1844 return getOperand(getNumExplicitDefs()).getIntrinsicID(); 1845 } 1846 1847 /// Sets all register debug operands in this debug value instruction to be 1848 /// undef. 1849 void setDebugValueUndef() { 1850 assert(isDebugValue() && "Must be a debug value instruction."); 1851 for (MachineOperand &MO : debug_operands()) { 1852 if (MO.isReg()) { 1853 MO.setReg(0); 1854 MO.setSubReg(0); 1855 } 1856 } 1857 } 1858 1859 private: 1860 /// If this instruction is embedded into a MachineFunction, return the 1861 /// MachineRegisterInfo object for the current function, otherwise 1862 /// return null. 1863 MachineRegisterInfo *getRegInfo(); 1864 1865 /// Unlink all of the register operands in this instruction from their 1866 /// respective use lists. This requires that the operands already be on their 1867 /// use lists. 1868 void removeRegOperandsFromUseLists(MachineRegisterInfo&); 1869 1870 /// Add all of the register operands in this instruction from their 1871 /// respective use lists. This requires that the operands not be on their 1872 /// use lists yet. 1873 void addRegOperandsToUseLists(MachineRegisterInfo&); 1874 1875 /// Slow path for hasProperty when we're dealing with a bundle. 1876 bool hasPropertyInBundle(uint64_t Mask, QueryType Type) const; 1877 1878 /// Implements the logic of getRegClassConstraintEffectForVReg for the 1879 /// this MI and the given operand index \p OpIdx. 1880 /// If the related operand does not constrained Reg, this returns CurRC. 1881 const TargetRegisterClass *getRegClassConstraintEffectForVRegImpl( 1882 unsigned OpIdx, Register Reg, const TargetRegisterClass *CurRC, 1883 const TargetInstrInfo *TII, const TargetRegisterInfo *TRI) const; 1884 1885 /// Stores extra instruction information inline or allocates as ExtraInfo 1886 /// based on the number of pointers. 1887 void setExtraInfo(MachineFunction &MF, ArrayRef<MachineMemOperand *> MMOs, 1888 MCSymbol *PreInstrSymbol, MCSymbol *PostInstrSymbol, 1889 MDNode *HeapAllocMarker); 1890 }; 1891 1892 /// Special DenseMapInfo traits to compare MachineInstr* by *value* of the 1893 /// instruction rather than by pointer value. 1894 /// The hashing and equality testing functions ignore definitions so this is 1895 /// useful for CSE, etc. 1896 struct MachineInstrExpressionTrait : DenseMapInfo<MachineInstr*> { 1897 static inline MachineInstr *getEmptyKey() { 1898 return nullptr; 1899 } 1900 1901 static inline MachineInstr *getTombstoneKey() { 1902 return reinterpret_cast<MachineInstr*>(-1); 1903 } 1904 1905 static unsigned getHashValue(const MachineInstr* const &MI); 1906 1907 static bool isEqual(const MachineInstr* const &LHS, 1908 const MachineInstr* const &RHS) { 1909 if (RHS == getEmptyKey() || RHS == getTombstoneKey() || 1910 LHS == getEmptyKey() || LHS == getTombstoneKey()) 1911 return LHS == RHS; 1912 return LHS->isIdenticalTo(*RHS, MachineInstr::IgnoreVRegDefs); 1913 } 1914 }; 1915 1916 //===----------------------------------------------------------------------===// 1917 // Debugging Support 1918 1919 inline raw_ostream& operator<<(raw_ostream &OS, const MachineInstr &MI) { 1920 MI.print(OS); 1921 return OS; 1922 } 1923 1924 } // end namespace llvm 1925 1926 #endif // LLVM_CODEGEN_MACHINEINSTR_H 1927