Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:31

0001 //===- CodeGen/MachineInstrBuilder.h - Simplify creation of MIs --*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file exposes a function named BuildMI, which is useful for dramatically
0010 // simplifying how MachineInstr's are created.  It allows use of code like this:
0011 //
0012 //   MIMetadata MIMD(MI);  // Propagates DebugLoc and other metadata
0013 //   M = BuildMI(MBB, MI, MIMD, TII.get(X86::ADD8rr), Dst)
0014 //           .addReg(argVal1)
0015 //           .addReg(argVal2);
0016 //
0017 //===----------------------------------------------------------------------===//
0018 
0019 #ifndef LLVM_CODEGEN_MACHINEINSTRBUILDER_H
0020 #define LLVM_CODEGEN_MACHINEINSTRBUILDER_H
0021 
0022 #include "llvm/ADT/ArrayRef.h"
0023 #include "llvm/CodeGen/GlobalISel/Utils.h"
0024 #include "llvm/CodeGen/MachineBasicBlock.h"
0025 #include "llvm/CodeGen/MachineFunction.h"
0026 #include "llvm/CodeGen/MachineInstr.h"
0027 #include "llvm/CodeGen/MachineInstrBundle.h"
0028 #include "llvm/CodeGen/MachineOperand.h"
0029 #include "llvm/CodeGen/TargetRegisterInfo.h"
0030 #include "llvm/IR/InstrTypes.h"
0031 #include "llvm/IR/Intrinsics.h"
0032 #include "llvm/Support/ErrorHandling.h"
0033 #include <cassert>
0034 #include <cstdint>
0035 
0036 namespace llvm {
0037 
0038 class MCInstrDesc;
0039 class MDNode;
0040 
0041 namespace RegState {
0042 
0043 // Keep this in sync with the table in MIRLangRef.rst.
0044 enum {
0045   /// Register definition.
0046   Define = 0x2,
0047   /// Not emitted register (e.g. carry, or temporary result).
0048   Implicit = 0x4,
0049   /// The last use of a register.
0050   Kill = 0x8,
0051   /// Unused definition.
0052   Dead = 0x10,
0053   /// Value of the register doesn't matter.
0054   Undef = 0x20,
0055   /// Register definition happens before uses.
0056   EarlyClobber = 0x40,
0057   /// Register 'use' is for debugging purpose.
0058   Debug = 0x80,
0059   /// Register reads a value that is defined inside the same instruction or
0060   /// bundle.
0061   InternalRead = 0x100,
0062   /// Register that may be renamed.
0063   Renamable = 0x200,
0064   DefineNoRead = Define | Undef,
0065   ImplicitDefine = Implicit | Define,
0066   ImplicitKill = Implicit | Kill
0067 };
0068 
0069 } // end namespace RegState
0070 
0071 class MachineInstrBuilder {
0072   MachineFunction *MF = nullptr;
0073   MachineInstr *MI = nullptr;
0074 
0075 public:
0076   MachineInstrBuilder() = default;
0077 
0078   /// Create a MachineInstrBuilder for manipulating an existing instruction.
0079   /// F must be the machine function that was used to allocate I.
0080   MachineInstrBuilder(MachineFunction &F, MachineInstr *I) : MF(&F), MI(I) {}
0081   MachineInstrBuilder(MachineFunction &F, MachineBasicBlock::iterator I)
0082       : MF(&F), MI(&*I) {}
0083 
0084   /// Allow automatic conversion to the machine instruction we are working on.
0085   operator MachineInstr*() const { return MI; }
0086   MachineInstr *operator->() const { return MI; }
0087   operator MachineBasicBlock::iterator() const { return MI; }
0088 
0089   /// If conversion operators fail, use this method to get the MachineInstr
0090   /// explicitly.
0091   MachineInstr *getInstr() const { return MI; }
0092 
0093   /// Get the register for the operand index.
0094   /// The operand at the index should be a register (asserted by
0095   /// MachineOperand).
0096   Register getReg(unsigned Idx) const { return MI->getOperand(Idx).getReg(); }
0097 
0098   /// Add a new virtual register operand.
0099   const MachineInstrBuilder &addReg(Register RegNo, unsigned flags = 0,
0100                                     unsigned SubReg = 0) const {
0101     assert((flags & 0x1) == 0 &&
0102            "Passing in 'true' to addReg is forbidden! Use enums instead.");
0103     MI->addOperand(*MF, MachineOperand::CreateReg(RegNo,
0104                                                flags & RegState::Define,
0105                                                flags & RegState::Implicit,
0106                                                flags & RegState::Kill,
0107                                                flags & RegState::Dead,
0108                                                flags & RegState::Undef,
0109                                                flags & RegState::EarlyClobber,
0110                                                SubReg,
0111                                                flags & RegState::Debug,
0112                                                flags & RegState::InternalRead,
0113                                                flags & RegState::Renamable));
0114     return *this;
0115   }
0116 
0117   /// Add a virtual register definition operand.
0118   const MachineInstrBuilder &addDef(Register RegNo, unsigned Flags = 0,
0119                                     unsigned SubReg = 0) const {
0120     return addReg(RegNo, Flags | RegState::Define, SubReg);
0121   }
0122 
0123   /// Add a virtual register use operand. It is an error for Flags to contain
0124   /// `RegState::Define` when calling this function.
0125   const MachineInstrBuilder &addUse(Register RegNo, unsigned Flags = 0,
0126                                     unsigned SubReg = 0) const {
0127     assert(!(Flags & RegState::Define) &&
0128            "Misleading addUse defines register, use addReg instead.");
0129     return addReg(RegNo, Flags, SubReg);
0130   }
0131 
0132   /// Add a new immediate operand.
0133   const MachineInstrBuilder &addImm(int64_t Val) const {
0134     MI->addOperand(*MF, MachineOperand::CreateImm(Val));
0135     return *this;
0136   }
0137 
0138   const MachineInstrBuilder &addCImm(const ConstantInt *Val) const {
0139     MI->addOperand(*MF, MachineOperand::CreateCImm(Val));
0140     return *this;
0141   }
0142 
0143   const MachineInstrBuilder &addFPImm(const ConstantFP *Val) const {
0144     MI->addOperand(*MF, MachineOperand::CreateFPImm(Val));
0145     return *this;
0146   }
0147 
0148   const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB,
0149                                     unsigned TargetFlags = 0) const {
0150     MI->addOperand(*MF, MachineOperand::CreateMBB(MBB, TargetFlags));
0151     return *this;
0152   }
0153 
0154   const MachineInstrBuilder &addFrameIndex(int Idx) const {
0155     MI->addOperand(*MF, MachineOperand::CreateFI(Idx));
0156     return *this;
0157   }
0158 
0159   const MachineInstrBuilder &
0160   addConstantPoolIndex(unsigned Idx, int Offset = 0,
0161                        unsigned TargetFlags = 0) const {
0162     MI->addOperand(*MF, MachineOperand::CreateCPI(Idx, Offset, TargetFlags));
0163     return *this;
0164   }
0165 
0166   const MachineInstrBuilder &addTargetIndex(unsigned Idx, int64_t Offset = 0,
0167                                           unsigned TargetFlags = 0) const {
0168     MI->addOperand(*MF, MachineOperand::CreateTargetIndex(Idx, Offset,
0169                                                           TargetFlags));
0170     return *this;
0171   }
0172 
0173   const MachineInstrBuilder &addJumpTableIndex(unsigned Idx,
0174                                                unsigned TargetFlags = 0) const {
0175     MI->addOperand(*MF, MachineOperand::CreateJTI(Idx, TargetFlags));
0176     return *this;
0177   }
0178 
0179   const MachineInstrBuilder &addGlobalAddress(const GlobalValue *GV,
0180                                               int64_t Offset = 0,
0181                                               unsigned TargetFlags = 0) const {
0182     MI->addOperand(*MF, MachineOperand::CreateGA(GV, Offset, TargetFlags));
0183     return *this;
0184   }
0185 
0186   const MachineInstrBuilder &addExternalSymbol(const char *FnName,
0187                                                unsigned TargetFlags = 0) const {
0188     MI->addOperand(*MF, MachineOperand::CreateES(FnName, TargetFlags));
0189     return *this;
0190   }
0191 
0192   const MachineInstrBuilder &addBlockAddress(const BlockAddress *BA,
0193                                              int64_t Offset = 0,
0194                                              unsigned TargetFlags = 0) const {
0195     MI->addOperand(*MF, MachineOperand::CreateBA(BA, Offset, TargetFlags));
0196     return *this;
0197   }
0198 
0199   const MachineInstrBuilder &addRegMask(const uint32_t *Mask) const {
0200     MI->addOperand(*MF, MachineOperand::CreateRegMask(Mask));
0201     return *this;
0202   }
0203 
0204   const MachineInstrBuilder &addMemOperand(MachineMemOperand *MMO) const {
0205     MI->addMemOperand(*MF, MMO);
0206     return *this;
0207   }
0208 
0209   const MachineInstrBuilder &
0210   setMemRefs(ArrayRef<MachineMemOperand *> MMOs) const {
0211     MI->setMemRefs(*MF, MMOs);
0212     return *this;
0213   }
0214 
0215   const MachineInstrBuilder &cloneMemRefs(const MachineInstr &OtherMI) const {
0216     MI->cloneMemRefs(*MF, OtherMI);
0217     return *this;
0218   }
0219 
0220   const MachineInstrBuilder &
0221   cloneMergedMemRefs(ArrayRef<const MachineInstr *> OtherMIs) const {
0222     MI->cloneMergedMemRefs(*MF, OtherMIs);
0223     return *this;
0224   }
0225 
0226   const MachineInstrBuilder &add(const MachineOperand &MO) const {
0227     MI->addOperand(*MF, MO);
0228     return *this;
0229   }
0230 
0231   const MachineInstrBuilder &add(ArrayRef<MachineOperand> MOs) const {
0232     for (const MachineOperand &MO : MOs) {
0233       MI->addOperand(*MF, MO);
0234     }
0235     return *this;
0236   }
0237 
0238   const MachineInstrBuilder &addMetadata(const MDNode *MD) const {
0239     MI->addOperand(*MF, MachineOperand::CreateMetadata(MD));
0240     assert((MI->isDebugValueLike() ? static_cast<bool>(MI->getDebugVariable())
0241                                    : true) &&
0242            "first MDNode argument of a DBG_VALUE not a variable");
0243     assert((MI->isDebugLabel() ? static_cast<bool>(MI->getDebugLabel())
0244                                : true) &&
0245            "first MDNode argument of a DBG_LABEL not a label");
0246     return *this;
0247   }
0248 
0249   const MachineInstrBuilder &addCFIIndex(unsigned CFIIndex) const {
0250     MI->addOperand(*MF, MachineOperand::CreateCFIIndex(CFIIndex));
0251     return *this;
0252   }
0253 
0254   const MachineInstrBuilder &addIntrinsicID(Intrinsic::ID ID) const {
0255     MI->addOperand(*MF, MachineOperand::CreateIntrinsicID(ID));
0256     return *this;
0257   }
0258 
0259   const MachineInstrBuilder &addPredicate(CmpInst::Predicate Pred) const {
0260     MI->addOperand(*MF, MachineOperand::CreatePredicate(Pred));
0261     return *this;
0262   }
0263 
0264   const MachineInstrBuilder &addShuffleMask(ArrayRef<int> Val) const {
0265     MI->addOperand(*MF, MachineOperand::CreateShuffleMask(Val));
0266     return *this;
0267   }
0268 
0269   const MachineInstrBuilder &addSym(MCSymbol *Sym,
0270                                     unsigned char TargetFlags = 0) const {
0271     MI->addOperand(*MF, MachineOperand::CreateMCSymbol(Sym, TargetFlags));
0272     return *this;
0273   }
0274 
0275   const MachineInstrBuilder &setMIFlags(unsigned Flags) const {
0276     MI->setFlags(Flags);
0277     return *this;
0278   }
0279 
0280   const MachineInstrBuilder &setMIFlag(MachineInstr::MIFlag Flag) const {
0281     MI->setFlag(Flag);
0282     return *this;
0283   }
0284 
0285   const MachineInstrBuilder &setOperandDead(unsigned OpIdx) const {
0286     MI->getOperand(OpIdx).setIsDead();
0287     return *this;
0288   }
0289 
0290   // Add a displacement from an existing MachineOperand with an added offset.
0291   const MachineInstrBuilder &addDisp(const MachineOperand &Disp, int64_t off,
0292                                      unsigned char TargetFlags = 0) const {
0293     // If caller specifies new TargetFlags then use it, otherwise the
0294     // default behavior is to copy the target flags from the existing
0295     // MachineOperand. This means if the caller wants to clear the
0296     // target flags it needs to do so explicitly.
0297     if (0 == TargetFlags)
0298       TargetFlags = Disp.getTargetFlags();
0299 
0300     switch (Disp.getType()) {
0301       default:
0302         llvm_unreachable("Unhandled operand type in addDisp()");
0303       case MachineOperand::MO_Immediate:
0304         return addImm(Disp.getImm() + off);
0305       case MachineOperand::MO_ConstantPoolIndex:
0306         return addConstantPoolIndex(Disp.getIndex(), Disp.getOffset() + off,
0307                                     TargetFlags);
0308       case MachineOperand::MO_GlobalAddress:
0309         return addGlobalAddress(Disp.getGlobal(), Disp.getOffset() + off,
0310                                 TargetFlags);
0311       case MachineOperand::MO_BlockAddress:
0312         return addBlockAddress(Disp.getBlockAddress(), Disp.getOffset() + off,
0313                                TargetFlags);
0314       case MachineOperand::MO_JumpTableIndex:
0315         assert(off == 0 && "cannot create offset into jump tables");
0316         return addJumpTableIndex(Disp.getIndex(), TargetFlags);
0317     }
0318   }
0319 
0320   const MachineInstrBuilder &setPCSections(MDNode *MD) const {
0321     if (MD)
0322       MI->setPCSections(*MF, MD);
0323     return *this;
0324   }
0325 
0326   const MachineInstrBuilder &setMMRAMetadata(MDNode *MMRA) const {
0327     if (MMRA)
0328       MI->setMMRAMetadata(*MF, MMRA);
0329     return *this;
0330   }
0331 
0332   /// Copy all the implicit operands from OtherMI onto this one.
0333   const MachineInstrBuilder &
0334   copyImplicitOps(const MachineInstr &OtherMI) const {
0335     MI->copyImplicitOps(*MF, OtherMI);
0336     return *this;
0337   }
0338 
0339   bool constrainAllUses(const TargetInstrInfo &TII,
0340                         const TargetRegisterInfo &TRI,
0341                         const RegisterBankInfo &RBI) const {
0342     return constrainSelectedInstRegOperands(*MI, TII, TRI, RBI);
0343   }
0344 };
0345 
0346 /// Set of metadata that should be preserved when using BuildMI(). This provides
0347 /// a more convenient way of preserving DebugLoc, PCSections and MMRA.
0348 class MIMetadata {
0349 public:
0350   MIMetadata() = default;
0351   MIMetadata(DebugLoc DL, MDNode *PCSections = nullptr, MDNode *MMRA = nullptr)
0352       : DL(std::move(DL)), PCSections(PCSections), MMRA(MMRA) {}
0353   MIMetadata(const DILocation *DI, MDNode *PCSections = nullptr,
0354              MDNode *MMRA = nullptr)
0355       : DL(DI), PCSections(PCSections), MMRA(MMRA) {}
0356   explicit MIMetadata(const Instruction &From)
0357       : DL(From.getDebugLoc()),
0358         PCSections(From.getMetadata(LLVMContext::MD_pcsections)) {}
0359   explicit MIMetadata(const MachineInstr &From)
0360       : DL(From.getDebugLoc()), PCSections(From.getPCSections()) {}
0361 
0362   const DebugLoc &getDL() const { return DL; }
0363   MDNode *getPCSections() const { return PCSections; }
0364   MDNode *getMMRAMetadata() const { return MMRA; }
0365 
0366 private:
0367   DebugLoc DL;
0368   MDNode *PCSections = nullptr;
0369   MDNode *MMRA = nullptr;
0370 };
0371 
0372 /// Builder interface. Specify how to create the initial instruction itself.
0373 inline MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD,
0374                                    const MCInstrDesc &MCID) {
0375   return MachineInstrBuilder(MF, MF.CreateMachineInstr(MCID, MIMD.getDL()))
0376       .setPCSections(MIMD.getPCSections())
0377       .setMMRAMetadata(MIMD.getMMRAMetadata());
0378 }
0379 
0380 /// This version of the builder sets up the first operand as a
0381 /// destination virtual register.
0382 inline MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD,
0383                                    const MCInstrDesc &MCID, Register DestReg) {
0384   return MachineInstrBuilder(MF, MF.CreateMachineInstr(MCID, MIMD.getDL()))
0385       .setPCSections(MIMD.getPCSections())
0386       .setMMRAMetadata(MIMD.getMMRAMetadata())
0387       .addReg(DestReg, RegState::Define);
0388 }
0389 
0390 /// This version of the builder inserts the newly-built instruction before
0391 /// the given position in the given MachineBasicBlock, and sets up the first
0392 /// operand as a destination virtual register.
0393 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
0394                                    MachineBasicBlock::iterator I,
0395                                    const MIMetadata &MIMD,
0396                                    const MCInstrDesc &MCID, Register DestReg) {
0397   MachineFunction &MF = *BB.getParent();
0398   MachineInstr *MI = MF.CreateMachineInstr(MCID, MIMD.getDL());
0399   BB.insert(I, MI);
0400   return MachineInstrBuilder(MF, MI)
0401       .setPCSections(MIMD.getPCSections())
0402       .setMMRAMetadata(MIMD.getMMRAMetadata())
0403       .addReg(DestReg, RegState::Define);
0404 }
0405 
0406 /// This version of the builder inserts the newly-built instruction before
0407 /// the given position in the given MachineBasicBlock, and sets up the first
0408 /// operand as a destination virtual register.
0409 ///
0410 /// If \c I is inside a bundle, then the newly inserted \a MachineInstr is
0411 /// added to the same bundle.
0412 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
0413                                    MachineBasicBlock::instr_iterator I,
0414                                    const MIMetadata &MIMD,
0415                                    const MCInstrDesc &MCID, Register DestReg) {
0416   MachineFunction &MF = *BB.getParent();
0417   MachineInstr *MI = MF.CreateMachineInstr(MCID, MIMD.getDL());
0418   BB.insert(I, MI);
0419   return MachineInstrBuilder(MF, MI)
0420       .setPCSections(MIMD.getPCSections())
0421       .setMMRAMetadata(MIMD.getMMRAMetadata())
0422       .addReg(DestReg, RegState::Define);
0423 }
0424 
0425 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, MachineInstr &I,
0426                                    const MIMetadata &MIMD,
0427                                    const MCInstrDesc &MCID, Register DestReg) {
0428   // Calling the overload for instr_iterator is always correct.  However, the
0429   // definition is not available in headers, so inline the check.
0430   if (I.isInsideBundle())
0431     return BuildMI(BB, MachineBasicBlock::instr_iterator(I), MIMD, MCID,
0432                    DestReg);
0433   return BuildMI(BB, MachineBasicBlock::iterator(I), MIMD, MCID, DestReg);
0434 }
0435 
0436 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, MachineInstr *I,
0437                                    const MIMetadata &MIMD,
0438                                    const MCInstrDesc &MCID, Register DestReg) {
0439   return BuildMI(BB, *I, MIMD, MCID, DestReg);
0440 }
0441 
0442 /// This version of the builder inserts the newly-built instruction before the
0443 /// given position in the given MachineBasicBlock, and does NOT take a
0444 /// destination register.
0445 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
0446                                    MachineBasicBlock::iterator I,
0447                                    const MIMetadata &MIMD,
0448                                    const MCInstrDesc &MCID) {
0449   MachineFunction &MF = *BB.getParent();
0450   MachineInstr *MI = MF.CreateMachineInstr(MCID, MIMD.getDL());
0451   BB.insert(I, MI);
0452   return MachineInstrBuilder(MF, MI)
0453       .setPCSections(MIMD.getPCSections())
0454       .setMMRAMetadata(MIMD.getMMRAMetadata());
0455 }
0456 
0457 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
0458                                    MachineBasicBlock::instr_iterator I,
0459                                    const MIMetadata &MIMD,
0460                                    const MCInstrDesc &MCID) {
0461   MachineFunction &MF = *BB.getParent();
0462   MachineInstr *MI = MF.CreateMachineInstr(MCID, MIMD.getDL());
0463   BB.insert(I, MI);
0464   return MachineInstrBuilder(MF, MI)
0465       .setPCSections(MIMD.getPCSections())
0466       .setMMRAMetadata(MIMD.getMMRAMetadata());
0467 }
0468 
0469 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, MachineInstr &I,
0470                                    const MIMetadata &MIMD,
0471                                    const MCInstrDesc &MCID) {
0472   // Calling the overload for instr_iterator is always correct.  However, the
0473   // definition is not available in headers, so inline the check.
0474   if (I.isInsideBundle())
0475     return BuildMI(BB, MachineBasicBlock::instr_iterator(I), MIMD, MCID);
0476   return BuildMI(BB, MachineBasicBlock::iterator(I), MIMD, MCID);
0477 }
0478 
0479 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, MachineInstr *I,
0480                                    const MIMetadata &MIMD,
0481                                    const MCInstrDesc &MCID) {
0482   return BuildMI(BB, *I, MIMD, MCID);
0483 }
0484 
0485 /// This version of the builder inserts the newly-built instruction at the end
0486 /// of the given MachineBasicBlock, and does NOT take a destination register.
0487 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
0488                                    const MIMetadata &MIMD,
0489                                    const MCInstrDesc &MCID) {
0490   return BuildMI(*BB, BB->end(), MIMD, MCID);
0491 }
0492 
0493 /// This version of the builder inserts the newly-built instruction at the
0494 /// end of the given MachineBasicBlock, and sets up the first operand as a
0495 /// destination virtual register.
0496 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
0497                                    const MIMetadata &MIMD,
0498                                    const MCInstrDesc &MCID, Register DestReg) {
0499   return BuildMI(*BB, BB->end(), MIMD, MCID, DestReg);
0500 }
0501 
0502 /// This version of the builder builds a DBG_VALUE intrinsic
0503 /// for either a value in a register or a register-indirect
0504 /// address.  The convention is that a DBG_VALUE is indirect iff the
0505 /// second operand is an immediate.
0506 MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL,
0507                             const MCInstrDesc &MCID, bool IsIndirect,
0508                             Register Reg, const MDNode *Variable,
0509                             const MDNode *Expr);
0510 
0511 /// This version of the builder builds a DBG_VALUE or DBG_VALUE_LIST intrinsic
0512 /// for a MachineOperand.
0513 MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL,
0514                             const MCInstrDesc &MCID, bool IsIndirect,
0515                             ArrayRef<MachineOperand> MOs,
0516                             const MDNode *Variable, const MDNode *Expr);
0517 
0518 /// This version of the builder builds a DBG_VALUE intrinsic
0519 /// for either a value in a register or a register-indirect
0520 /// address and inserts it at position I.
0521 MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
0522                             MachineBasicBlock::iterator I, const DebugLoc &DL,
0523                             const MCInstrDesc &MCID, bool IsIndirect,
0524                             Register Reg, const MDNode *Variable,
0525                             const MDNode *Expr);
0526 
0527 /// This version of the builder builds a DBG_VALUE, DBG_INSTR_REF, or
0528 /// DBG_VALUE_LIST intrinsic for a machine operand and inserts it at position I.
0529 MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
0530                             MachineBasicBlock::iterator I, const DebugLoc &DL,
0531                             const MCInstrDesc &MCID, bool IsIndirect,
0532                             ArrayRef<MachineOperand> MOs,
0533                             const MDNode *Variable, const MDNode *Expr);
0534 
0535 /// Clone a DBG_VALUE whose value has been spilled to FrameIndex.
0536 MachineInstr *buildDbgValueForSpill(MachineBasicBlock &BB,
0537                                     MachineBasicBlock::iterator I,
0538                                     const MachineInstr &Orig, int FrameIndex,
0539                                     Register SpillReg);
0540 MachineInstr *buildDbgValueForSpill(
0541     MachineBasicBlock &BB, MachineBasicBlock::iterator I,
0542     const MachineInstr &Orig, int FrameIndex,
0543     const SmallVectorImpl<const MachineOperand *> &SpilledOperands);
0544 
0545 /// Update a DBG_VALUE whose value has been spilled to FrameIndex. Useful when
0546 /// modifying an instruction in place while iterating over a basic block.
0547 void updateDbgValueForSpill(MachineInstr &Orig, int FrameIndex, Register Reg);
0548 
0549 inline unsigned getDefRegState(bool B) {
0550   return B ? RegState::Define : 0;
0551 }
0552 inline unsigned getImplRegState(bool B) {
0553   return B ? RegState::Implicit : 0;
0554 }
0555 inline unsigned getKillRegState(bool B) {
0556   return B ? RegState::Kill : 0;
0557 }
0558 inline unsigned getDeadRegState(bool B) {
0559   return B ? RegState::Dead : 0;
0560 }
0561 inline unsigned getUndefRegState(bool B) {
0562   return B ? RegState::Undef : 0;
0563 }
0564 inline unsigned getInternalReadRegState(bool B) {
0565   return B ? RegState::InternalRead : 0;
0566 }
0567 inline unsigned getDebugRegState(bool B) {
0568   return B ? RegState::Debug : 0;
0569 }
0570 inline unsigned getRenamableRegState(bool B) {
0571   return B ? RegState::Renamable : 0;
0572 }
0573 
0574 /// Get all register state flags from machine operand \p RegOp.
0575 inline unsigned getRegState(const MachineOperand &RegOp) {
0576   assert(RegOp.isReg() && "Not a register operand");
0577   return getDefRegState(RegOp.isDef()) | getImplRegState(RegOp.isImplicit()) |
0578          getKillRegState(RegOp.isKill()) | getDeadRegState(RegOp.isDead()) |
0579          getUndefRegState(RegOp.isUndef()) |
0580          getInternalReadRegState(RegOp.isInternalRead()) |
0581          getDebugRegState(RegOp.isDebug()) |
0582          getRenamableRegState(RegOp.getReg().isPhysical() &&
0583                               RegOp.isRenamable());
0584 }
0585 
0586 /// Helper class for constructing bundles of MachineInstrs.
0587 ///
0588 /// MIBundleBuilder can create a bundle from scratch by inserting new
0589 /// MachineInstrs one at a time, or it can create a bundle from a sequence of
0590 /// existing MachineInstrs in a basic block.
0591 class MIBundleBuilder {
0592   MachineBasicBlock &MBB;
0593   MachineBasicBlock::instr_iterator Begin;
0594   MachineBasicBlock::instr_iterator End;
0595 
0596 public:
0597   /// Create an MIBundleBuilder that inserts instructions into a new bundle in
0598   /// BB above the bundle or instruction at Pos.
0599   MIBundleBuilder(MachineBasicBlock &BB, MachineBasicBlock::iterator Pos)
0600       : MBB(BB), Begin(Pos.getInstrIterator()), End(Begin) {}
0601 
0602   /// Create a bundle from the sequence of instructions between B and E.
0603   MIBundleBuilder(MachineBasicBlock &BB, MachineBasicBlock::iterator B,
0604                   MachineBasicBlock::iterator E)
0605       : MBB(BB), Begin(B.getInstrIterator()), End(E.getInstrIterator()) {
0606     assert(B != E && "No instructions to bundle");
0607     ++B;
0608     while (B != E) {
0609       MachineInstr &MI = *B;
0610       ++B;
0611       MI.bundleWithPred();
0612     }
0613   }
0614 
0615   /// Create an MIBundleBuilder representing an existing instruction or bundle
0616   /// that has MI as its head.
0617   explicit MIBundleBuilder(MachineInstr *MI)
0618       : MBB(*MI->getParent()), Begin(MI),
0619         End(getBundleEnd(MI->getIterator())) {}
0620 
0621   /// Return a reference to the basic block containing this bundle.
0622   MachineBasicBlock &getMBB() const { return MBB; }
0623 
0624   /// Return true if no instructions have been inserted in this bundle yet.
0625   /// Empty bundles aren't representable in a MachineBasicBlock.
0626   bool empty() const { return Begin == End; }
0627 
0628   /// Return an iterator to the first bundled instruction.
0629   MachineBasicBlock::instr_iterator begin() const { return Begin; }
0630 
0631   /// Return an iterator beyond the last bundled instruction.
0632   MachineBasicBlock::instr_iterator end() const { return End; }
0633 
0634   /// Insert MI into this bundle before I which must point to an instruction in
0635   /// the bundle, or end().
0636   MIBundleBuilder &insert(MachineBasicBlock::instr_iterator I,
0637                           MachineInstr *MI) {
0638     MBB.insert(I, MI);
0639     if (I == Begin) {
0640       if (!empty())
0641         MI->bundleWithSucc();
0642       Begin = MI->getIterator();
0643       return *this;
0644     }
0645     if (I == End) {
0646       MI->bundleWithPred();
0647       return *this;
0648     }
0649     // MI was inserted in the middle of the bundle, so its neighbors' flags are
0650     // already fine. Update MI's bundle flags manually.
0651     MI->setFlag(MachineInstr::BundledPred);
0652     MI->setFlag(MachineInstr::BundledSucc);
0653     return *this;
0654   }
0655 
0656   /// Insert MI into MBB by prepending it to the instructions in the bundle.
0657   /// MI will become the first instruction in the bundle.
0658   MIBundleBuilder &prepend(MachineInstr *MI) {
0659     return insert(begin(), MI);
0660   }
0661 
0662   /// Insert MI into MBB by appending it to the instructions in the bundle.
0663   /// MI will become the last instruction in the bundle.
0664   MIBundleBuilder &append(MachineInstr *MI) {
0665     return insert(end(), MI);
0666   }
0667 };
0668 
0669 } // end namespace llvm
0670 
0671 #endif // LLVM_CODEGEN_MACHINEINSTRBUILDER_H