LCOV - code coverage report
Current view: top level - include/llvm/CodeGen - MachineInstrBuilder.h (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 121 126 96.0 %
Date: 2018-07-13 00:08:38 Functions: 12 12 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- CodeGen/MachineInstrBuilder.h - Simplify creation of MIs --*- C++ -*-===//
       2             : //
       3             : //                     The LLVM Compiler Infrastructure
       4             : //
       5             : // This file is distributed under the University of Illinois Open Source
       6             : // License. See LICENSE.TXT for details.
       7             : //
       8             : //===----------------------------------------------------------------------===//
       9             : //
      10             : // This file exposes a function named BuildMI, which is useful for dramatically
      11             : // simplifying how MachineInstr's are created.  It allows use of code like this:
      12             : //
      13             : //   M = BuildMI(MBB, MI, DL, TII.get(X86::ADD8rr), Dst)
      14             : //           .addReg(argVal1)
      15             : //           .addReg(argVal2);
      16             : //
      17             : //===----------------------------------------------------------------------===//
      18             : 
      19             : #ifndef LLVM_CODEGEN_MACHINEINSTRBUILDER_H
      20             : #define LLVM_CODEGEN_MACHINEINSTRBUILDER_H
      21             : 
      22             : #include "llvm/ADT/ArrayRef.h"
      23             : #include "llvm/CodeGen/GlobalISel/Utils.h"
      24             : #include "llvm/CodeGen/MachineBasicBlock.h"
      25             : #include "llvm/CodeGen/MachineFunction.h"
      26             : #include "llvm/CodeGen/MachineInstr.h"
      27             : #include "llvm/CodeGen/MachineInstrBundle.h"
      28             : #include "llvm/CodeGen/MachineOperand.h"
      29             : #include "llvm/CodeGen/TargetRegisterInfo.h"
      30             : #include "llvm/IR/InstrTypes.h"
      31             : #include "llvm/IR/Intrinsics.h"
      32             : #include "llvm/Support/ErrorHandling.h"
      33             : #include <cassert>
      34             : #include <cstdint>
      35             : #include <utility>
      36             : 
      37             : namespace llvm {
      38             : 
      39             : class MCInstrDesc;
      40             : class MDNode;
      41             : 
      42             : namespace RegState {
      43             : 
      44             :   enum {
      45             :     Define         = 0x2,
      46             :     Implicit       = 0x4,
      47             :     Kill           = 0x8,
      48             :     Dead           = 0x10,
      49             :     Undef          = 0x20,
      50             :     EarlyClobber   = 0x40,
      51             :     Debug          = 0x80,
      52             :     InternalRead   = 0x100,
      53             :     Renamable      = 0x200,
      54             :     DefineNoRead   = Define | Undef,
      55             :     ImplicitDefine = Implicit | Define,
      56             :     ImplicitKill   = Implicit | Kill
      57             :   };
      58             : 
      59             : } // end namespace RegState
      60             : 
      61             : class MachineInstrBuilder {
      62             :   MachineFunction *MF = nullptr;
      63             :   MachineInstr *MI = nullptr;
      64             : 
      65             : public:
      66             :   MachineInstrBuilder() = default;
      67             : 
      68             :   /// Create a MachineInstrBuilder for manipulating an existing instruction.
      69             :   /// F must be the machine function that was used to allocate I.
      70     3158054 :   MachineInstrBuilder(MachineFunction &F, MachineInstr *I) : MF(&F), MI(I) {}
      71             :   MachineInstrBuilder(MachineFunction &F, MachineBasicBlock::iterator I)
      72      643528 :       : MF(&F), MI(&*I) {}
      73             : 
      74             :   /// Allow automatic conversion to the machine instruction we are working on.
      75             :   operator MachineInstr*() const { return MI; }
      76             :   MachineInstr *operator->() const { return MI; }
      77             :   operator MachineBasicBlock::iterator() const { return MI; }
      78             : 
      79             :   /// If conversion operators fail, use this method to get the MachineInstr
      80             :   /// explicitly.
      81             :   MachineInstr *getInstr() const { return MI; }
      82             : 
      83             :   /// Add a new virtual register operand.
      84    14041800 :   const MachineInstrBuilder &addReg(unsigned RegNo, unsigned flags = 0,
      85             :                                     unsigned SubReg = 0) const {
      86             :     assert((flags & 0x1) == 0 &&
      87             :            "Passing in 'true' to addReg is forbidden! Use enums instead.");
      88    28083600 :     MI->addOperand(*MF, MachineOperand::CreateReg(RegNo,
      89    14041800 :                                                flags & RegState::Define,
      90    14041800 :                                                flags & RegState::Implicit,
      91             :                                                flags & RegState::Kill,
      92             :                                                flags & RegState::Dead,
      93    14041800 :                                                flags & RegState::Undef,
      94    14041800 :                                                flags & RegState::EarlyClobber,
      95             :                                                SubReg,
      96    14041800 :                                                flags & RegState::Debug,
      97    14041800 :                                                flags & RegState::InternalRead,
      98    98292600 :                                                flags & RegState::Renamable));
      99    14041799 :     return *this;
     100             :   }
     101             : 
     102             :   /// Add a virtual register definition operand.
     103             :   const MachineInstrBuilder &addDef(unsigned RegNo, unsigned Flags = 0,
     104             :                                     unsigned SubReg = 0) const {
     105       16451 :     return addReg(RegNo, Flags | RegState::Define, SubReg);
     106             :   }
     107             : 
     108             :   /// Add a virtual register use operand. It is an error for Flags to contain
     109             :   /// `RegState::Define` when calling this function.
     110             :   const MachineInstrBuilder &addUse(unsigned RegNo, unsigned Flags = 0,
     111             :                                     unsigned SubReg = 0) const {
     112             :     assert(!(Flags & RegState::Define) &&
     113             :            "Misleading addUse defines register, use addReg instead.");
     114       12922 :     return addReg(RegNo, Flags, SubReg);
     115             :   }
     116             : 
     117             :   /// Add a new immediate operand.
     118             :   const MachineInstrBuilder &addImm(int64_t Val) const {
     119    10690969 :     MI->addOperand(*MF, MachineOperand::CreateImm(Val));
     120             :     return *this;
     121             :   }
     122             : 
     123             :   const MachineInstrBuilder &addCImm(const ConstantInt *Val) const {
     124        2267 :     MI->addOperand(*MF, MachineOperand::CreateCImm(Val));
     125             :     return *this;
     126             :   }
     127             : 
     128             :   const MachineInstrBuilder &addFPImm(const ConstantFP *Val) const {
     129         470 :     MI->addOperand(*MF, MachineOperand::CreateFPImm(Val));
     130             :     return *this;
     131             :   }
     132             : 
     133             :   const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB,
     134             :                                     unsigned char TargetFlags = 0) const {
     135     1032660 :     MI->addOperand(*MF, MachineOperand::CreateMBB(MBB, TargetFlags));
     136             :     return *this;
     137             :   }
     138             : 
     139             :   const MachineInstrBuilder &addFrameIndex(int Idx) const {
     140      984974 :     MI->addOperand(*MF, MachineOperand::CreateFI(Idx));
     141             :     return *this;
     142             :   }
     143             : 
     144             :   const MachineInstrBuilder &addConstantPoolIndex(unsigned Idx,
     145             :                                                   int Offset = 0,
     146             :                                           unsigned char TargetFlags = 0) const {
     147       56294 :     MI->addOperand(*MF, MachineOperand::CreateCPI(Idx, Offset, TargetFlags));
     148             :     return *this;
     149             :   }
     150             : 
     151             :   const MachineInstrBuilder &addTargetIndex(unsigned Idx, int64_t Offset = 0,
     152             :                                           unsigned char TargetFlags = 0) const {
     153           0 :     MI->addOperand(*MF, MachineOperand::CreateTargetIndex(Idx, Offset,
     154             :                                                           TargetFlags));
     155             :     return *this;
     156             :   }
     157             : 
     158             :   const MachineInstrBuilder &addJumpTableIndex(unsigned Idx,
     159             :                                           unsigned char TargetFlags = 0) const {
     160         939 :     MI->addOperand(*MF, MachineOperand::CreateJTI(Idx, TargetFlags));
     161             :     return *this;
     162             :   }
     163             : 
     164             :   const MachineInstrBuilder &addGlobalAddress(const GlobalValue *GV,
     165             :                                               int64_t Offset = 0,
     166             :                                           unsigned char TargetFlags = 0) const {
     167     1647277 :     MI->addOperand(*MF, MachineOperand::CreateGA(GV, Offset, TargetFlags));
     168             :     return *this;
     169             :   }
     170             : 
     171             :   const MachineInstrBuilder &addExternalSymbol(const char *FnName,
     172             :                                           unsigned char TargetFlags = 0) const {
     173       56966 :     MI->addOperand(*MF, MachineOperand::CreateES(FnName, TargetFlags));
     174             :     return *this;
     175             :   }
     176             : 
     177             :   const MachineInstrBuilder &addBlockAddress(const BlockAddress *BA,
     178             :                                              int64_t Offset = 0,
     179             :                                           unsigned char TargetFlags = 0) const {
     180         280 :     MI->addOperand(*MF, MachineOperand::CreateBA(BA, Offset, TargetFlags));
     181             :     return *this;
     182             :   }
     183             : 
     184             :   const MachineInstrBuilder &addRegMask(const uint32_t *Mask) const {
     185      447152 :     MI->addOperand(*MF, MachineOperand::CreateRegMask(Mask));
     186             :     return *this;
     187             :   }
     188             : 
     189             :   const MachineInstrBuilder &addMemOperand(MachineMemOperand *MMO) const {
     190      160716 :     MI->addMemOperand(*MF, MMO);
     191             :     return *this;
     192             :   }
     193             : 
     194             :   const MachineInstrBuilder &setMemRefs(MachineInstr::mmo_iterator b,
     195             :                                         MachineInstr::mmo_iterator e) const {
     196     2837913 :     MI->setMemRefs(b, e);
     197             :     return *this;
     198             :   }
     199             : 
     200             :   const MachineInstrBuilder &setMemRefs(std::pair<MachineInstr::mmo_iterator,
     201             :                                         unsigned> MemOperandsRef) const {
     202        3202 :     MI->setMemRefs(MemOperandsRef);
     203             :     return *this;
     204             :   }
     205             : 
     206             :   const MachineInstrBuilder &add(const MachineOperand &MO) const {
     207      778381 :     MI->addOperand(*MF, MO);
     208             :     return *this;
     209             :   }
     210             : 
     211             :   const MachineInstrBuilder &add(ArrayRef<MachineOperand> MOs) const {
     212      276650 :     for (const MachineOperand &MO : MOs) {
     213      110660 :       MI->addOperand(*MF, MO);
     214             :     }
     215             :     return *this;
     216             :   }
     217             : 
     218             :   const MachineInstrBuilder &addMetadata(const MDNode *MD) const {
     219      317826 :     MI->addOperand(*MF, MachineOperand::CreateMetadata(MD));
     220             :     assert((MI->isDebugValue() ? static_cast<bool>(MI->getDebugVariable())
     221             :                                : true) &&
     222             :            "first MDNode argument of a DBG_VALUE not a variable");
     223             :     assert((MI->isDebugLabel() ? static_cast<bool>(MI->getDebugLabel())
     224             :                                : true) &&
     225             :            "first MDNode argument of a DBG_LABEL not a label");
     226             :     return *this;
     227             :   }
     228             : 
     229             :   const MachineInstrBuilder &addCFIIndex(unsigned CFIIndex) const {
     230      547881 :     MI->addOperand(*MF, MachineOperand::CreateCFIIndex(CFIIndex));
     231             :     return *this;
     232             :   }
     233             : 
     234             :   const MachineInstrBuilder &addIntrinsicID(Intrinsic::ID ID) const {
     235          50 :     MI->addOperand(*MF, MachineOperand::CreateIntrinsicID(ID));
     236             :     return *this;
     237             :   }
     238             : 
     239             :   const MachineInstrBuilder &addPredicate(CmpInst::Predicate Pred) const {
     240         206 :     MI->addOperand(*MF, MachineOperand::CreatePredicate(Pred));
     241             :     return *this;
     242             :   }
     243             : 
     244             :   const MachineInstrBuilder &addSym(MCSymbol *Sym,
     245             :                                     unsigned char TargetFlags = 0) const {
     246      214716 :     MI->addOperand(*MF, MachineOperand::CreateMCSymbol(Sym, TargetFlags));
     247             :     return *this;
     248             :   }
     249             : 
     250             :   const MachineInstrBuilder &setMIFlags(unsigned Flags) const {
     251       13760 :     MI->setFlags(Flags);
     252             :     return *this;
     253             :   }
     254             : 
     255             :   const MachineInstrBuilder &setMIFlag(MachineInstr::MIFlag Flag) const {
     256      209995 :     MI->setFlag(Flag);
     257             :     return *this;
     258             :   }
     259             : 
     260             :   // Add a displacement from an existing MachineOperand with an added offset.
     261          46 :   const MachineInstrBuilder &addDisp(const MachineOperand &Disp, int64_t off,
     262             :                                      unsigned char TargetFlags = 0) const {
     263             :     // If caller specifies new TargetFlags then use it, otherwise the
     264             :     // default behavior is to copy the target flags from the existing
     265             :     // MachineOperand. This means if the caller wants to clear the
     266             :     // target flags it needs to do so explicitly.
     267          46 :     if (0 == TargetFlags)
     268          46 :       TargetFlags = Disp.getTargetFlags();
     269             : 
     270          46 :     switch (Disp.getType()) {
     271           0 :       default:
     272           0 :         llvm_unreachable("Unhandled operand type in addDisp()");
     273          22 :       case MachineOperand::MO_Immediate:
     274          44 :         return addImm(Disp.getImm() + off);
     275           6 :       case MachineOperand::MO_ConstantPoolIndex:
     276          18 :         return addConstantPoolIndex(Disp.getIndex(), Disp.getOffset() + off,
     277          12 :                                     TargetFlags);
     278          18 :       case MachineOperand::MO_GlobalAddress:
     279          18 :         return addGlobalAddress(Disp.getGlobal(), Disp.getOffset() + off,
     280          36 :                                 TargetFlags);
     281             :     }
     282             :   }
     283             : 
     284             :   /// Copy all the implicit operands from OtherMI onto this one.
     285             :   const MachineInstrBuilder &
     286             :   copyImplicitOps(const MachineInstr &OtherMI) const {
     287        4682 :     MI->copyImplicitOps(*MF, OtherMI);
     288             :     return *this;
     289             :   }
     290             : 
     291             :   bool constrainAllUses(const TargetInstrInfo &TII,
     292             :                         const TargetRegisterInfo &TRI,
     293             :                         const RegisterBankInfo &RBI) const {
     294           4 :     return constrainSelectedInstRegOperands(*MI, TII, TRI, RBI);
     295             :   }
     296             : };
     297             : 
     298             : /// Builder interface. Specify how to create the initial instruction itself.
     299             : inline MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL,
     300             :                                    const MCInstrDesc &MCID) {
     301     3007036 :   return MachineInstrBuilder(MF, MF.CreateMachineInstr(MCID, DL));
     302             : }
     303             : 
     304             : /// This version of the builder sets up the first operand as a
     305             : /// destination virtual register.
     306      143442 : inline MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL,
     307             :                                    const MCInstrDesc &MCID, unsigned DestReg) {
     308      286884 :   return MachineInstrBuilder(MF, MF.CreateMachineInstr(MCID, DL))
     309      286884 :            .addReg(DestReg, RegState::Define);
     310             : }
     311             : 
     312             : /// This version of the builder inserts the newly-built instruction before
     313             : /// the given position in the given MachineBasicBlock, and sets up the first
     314             : /// operand as a destination virtual register.
     315     2882291 : inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
     316             :                                    MachineBasicBlock::iterator I,
     317             :                                    const DebugLoc &DL, const MCInstrDesc &MCID,
     318             :                                    unsigned DestReg) {
     319     2882291 :   MachineFunction &MF = *BB.getParent();
     320     2882291 :   MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
     321             :   BB.insert(I, MI);
     322     2882291 :   return MachineInstrBuilder(MF, MI).addReg(DestReg, RegState::Define);
     323             : }
     324             : 
     325             : /// This version of the builder inserts the newly-built instruction before
     326             : /// the given position in the given MachineBasicBlock, and sets up the first
     327             : /// operand as a destination virtual register.
     328             : ///
     329             : /// If \c I is inside a bundle, then the newly inserted \a MachineInstr is
     330             : /// added to the same bundle.
     331       10085 : inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
     332             :                                    MachineBasicBlock::instr_iterator I,
     333             :                                    const DebugLoc &DL, const MCInstrDesc &MCID,
     334             :                                    unsigned DestReg) {
     335       10085 :   MachineFunction &MF = *BB.getParent();
     336       10085 :   MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
     337       10085 :   BB.insert(I, MI);
     338       10085 :   return MachineInstrBuilder(MF, MI).addReg(DestReg, RegState::Define);
     339             : }
     340             : 
     341      341833 : inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, MachineInstr &I,
     342             :                                    const DebugLoc &DL, const MCInstrDesc &MCID,
     343             :                                    unsigned DestReg) {
     344             :   // Calling the overload for instr_iterator is always correct.  However, the
     345             :   // definition is not available in headers, so inline the check.
     346      341833 :   if (I.isInsideBundle())
     347           0 :     return BuildMI(BB, MachineBasicBlock::instr_iterator(I), DL, MCID, DestReg);
     348      341833 :   return BuildMI(BB, MachineBasicBlock::iterator(I), DL, MCID, DestReg);
     349             : }
     350             : 
     351             : inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, MachineInstr *I,
     352             :                                    const DebugLoc &DL, const MCInstrDesc &MCID,
     353             :                                    unsigned DestReg) {
     354      325466 :   return BuildMI(BB, *I, DL, MCID, DestReg);
     355             : }
     356             : 
     357             : /// This version of the builder inserts the newly-built instruction before the
     358             : /// given position in the given MachineBasicBlock, and does NOT take a
     359             : /// destination register.
     360     1765193 : inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
     361             :                                    MachineBasicBlock::iterator I,
     362             :                                    const DebugLoc &DL,
     363             :                                    const MCInstrDesc &MCID) {
     364     1765193 :   MachineFunction &MF = *BB.getParent();
     365     1765193 :   MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
     366             :   BB.insert(I, MI);
     367     1765193 :   return MachineInstrBuilder(MF, MI);
     368             : }
     369             : 
     370       34446 : inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
     371             :                                    MachineBasicBlock::instr_iterator I,
     372             :                                    const DebugLoc &DL,
     373             :                                    const MCInstrDesc &MCID) {
     374       34446 :   MachineFunction &MF = *BB.getParent();
     375       34446 :   MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
     376       34446 :   BB.insert(I, MI);
     377       34446 :   return MachineInstrBuilder(MF, MI);
     378             : }
     379             : 
     380      191993 : inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, MachineInstr &I,
     381             :                                    const DebugLoc &DL,
     382             :                                    const MCInstrDesc &MCID) {
     383             :   // Calling the overload for instr_iterator is always correct.  However, the
     384             :   // definition is not available in headers, so inline the check.
     385      191993 :   if (I.isInsideBundle())
     386         567 :     return BuildMI(BB, MachineBasicBlock::instr_iterator(I), DL, MCID);
     387      191426 :   return BuildMI(BB, MachineBasicBlock::iterator(I), DL, MCID);
     388             : }
     389             : 
     390             : inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, MachineInstr *I,
     391             :                                    const DebugLoc &DL,
     392             :                                    const MCInstrDesc &MCID) {
     393       35865 :   return BuildMI(BB, *I, DL, MCID);
     394             : }
     395             : 
     396             : /// This version of the builder inserts the newly-built instruction at the end
     397             : /// of the given MachineBasicBlock, and does NOT take a destination register.
     398             : inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, const DebugLoc &DL,
     399             :                                    const MCInstrDesc &MCID) {
     400      451860 :   return BuildMI(*BB, BB->end(), DL, MCID);
     401             : }
     402             : 
     403             : /// This version of the builder inserts the newly-built instruction at the
     404             : /// end of the given MachineBasicBlock, and sets up the first operand as a
     405             : /// destination virtual register.
     406             : inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, const DebugLoc &DL,
     407             :                                    const MCInstrDesc &MCID, unsigned DestReg) {
     408       54030 :   return BuildMI(*BB, BB->end(), DL, MCID, DestReg);
     409             : }
     410             : 
     411             : /// This version of the builder builds a DBG_VALUE intrinsic
     412             : /// for either a value in a register or a register-indirect
     413             : /// address.  The convention is that a DBG_VALUE is indirect iff the
     414             : /// second operand is an immediate.
     415             : MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL,
     416             :                             const MCInstrDesc &MCID, bool IsIndirect,
     417             :                             unsigned Reg, const MDNode *Variable,
     418             :                             const MDNode *Expr);
     419             : 
     420             : /// This version of the builder builds a DBG_VALUE intrinsic
     421             : /// for a MachineOperand.
     422             : MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL,
     423             :                             const MCInstrDesc &MCID, bool IsIndirect,
     424             :                             MachineOperand &MO, const MDNode *Variable,
     425             :                             const MDNode *Expr);
     426             : 
     427             : /// This version of the builder builds a DBG_VALUE intrinsic
     428             : /// for either a value in a register or a register-indirect
     429             : /// address and inserts it at position I.
     430             : MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
     431             :                             MachineBasicBlock::iterator I, const DebugLoc &DL,
     432             :                             const MCInstrDesc &MCID, bool IsIndirect,
     433             :                             unsigned Reg, const MDNode *Variable,
     434             :                             const MDNode *Expr);
     435             : 
     436             : /// This version of the builder builds a DBG_VALUE intrinsic
     437             : /// for a machine operand and inserts it at position I.
     438             : MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
     439             :                             MachineBasicBlock::iterator I, const DebugLoc &DL,
     440             :                             const MCInstrDesc &MCID, bool IsIndirect,
     441             :                             MachineOperand &MO, const MDNode *Variable,
     442             :                             const MDNode *Expr);
     443             : 
     444             : /// Clone a DBG_VALUE whose value has been spilled to FrameIndex.
     445             : MachineInstr *buildDbgValueForSpill(MachineBasicBlock &BB,
     446             :                                     MachineBasicBlock::iterator I,
     447             :                                     const MachineInstr &Orig, int FrameIndex);
     448             : 
     449             : /// Update a DBG_VALUE whose value has been spilled to FrameIndex. Useful when
     450             : /// modifying an instruction in place while iterating over a basic block.
     451             : void updateDbgValueForSpill(MachineInstr &Orig, int FrameIndex);
     452             : 
     453             : inline unsigned getDefRegState(bool B) {
     454     2122860 :   return B ? RegState::Define : 0;
     455             : }
     456             : inline unsigned getImplRegState(bool B) {
     457       56742 :   return B ? RegState::Implicit : 0;
     458             : }
     459             : inline unsigned getKillRegState(bool B) {
     460     2680982 :   return B ? RegState::Kill : 0;
     461             : }
     462             : inline unsigned getDeadRegState(bool B) {
     463        7433 :   return B ? RegState::Dead : 0;
     464             : }
     465             : inline unsigned getUndefRegState(bool B) {
     466      117541 :   return B ? RegState::Undef : 0;
     467             : }
     468             : inline unsigned getInternalReadRegState(bool B) {
     469        1768 :   return B ? RegState::InternalRead : 0;
     470             : }
     471             : inline unsigned getDebugRegState(bool B) {
     472     2107465 :   return B ? RegState::Debug : 0;
     473             : }
     474             : inline unsigned getRenamableRegState(bool B) {
     475             :   return B ? RegState::Renamable : 0;
     476             : }
     477             : 
     478             : /// Get all register state flags from machine operand \p RegOp.
     479        1665 : inline unsigned getRegState(const MachineOperand &RegOp) {
     480             :   assert(RegOp.isReg() && "Not a register operand");
     481        1665 :   return getDefRegState(RegOp.isDef())                    |
     482        1665 :          getImplRegState(RegOp.isImplicit())              |
     483        1665 :          getKillRegState(RegOp.isKill())                  |
     484        1665 :          getDeadRegState(RegOp.isDead())                  |
     485        1665 :          getUndefRegState(RegOp.isUndef())                |
     486        1665 :          getInternalReadRegState(RegOp.isInternalRead())  |
     487             :          getDebugRegState(RegOp.isDebug())                |
     488             :          getRenamableRegState(
     489        3573 :              TargetRegisterInfo::isPhysicalRegister(RegOp.getReg()) &&
     490        1908 :              RegOp.isRenamable());
     491             : }
     492             : 
     493             : /// Helper class for constructing bundles of MachineInstrs.
     494             : ///
     495             : /// MIBundleBuilder can create a bundle from scratch by inserting new
     496             : /// MachineInstrs one at a time, or it can create a bundle from a sequence of
     497             : /// existing MachineInstrs in a basic block.
     498             : class MIBundleBuilder {
     499             :   MachineBasicBlock &MBB;
     500             :   MachineBasicBlock::instr_iterator Begin;
     501             :   MachineBasicBlock::instr_iterator End;
     502             : 
     503             : public:
     504             :   /// Create an MIBundleBuilder that inserts instructions into a new bundle in
     505             :   /// BB above the bundle or instruction at Pos.
     506             :   MIBundleBuilder(MachineBasicBlock &BB, MachineBasicBlock::iterator Pos)
     507         570 :       : MBB(BB), Begin(Pos.getInstrIterator()), End(Begin) {}
     508             : 
     509             :   /// Create a bundle from the sequence of instructions between B and E.
     510       39766 :   MIBundleBuilder(MachineBasicBlock &BB, MachineBasicBlock::iterator B,
     511             :                   MachineBasicBlock::iterator E)
     512      119298 :       : MBB(BB), Begin(B.getInstrIterator()), End(E.getInstrIterator()) {
     513             :     assert(B != E && "No instructions to bundle");
     514             :     ++B;
     515      146080 :     while (B != E) {
     516             :       MachineInstr &MI = *B;
     517             :       ++B;
     518       53157 :       MI.bundleWithPred();
     519             :     }
     520       39766 :   }
     521             : 
     522             :   /// Create an MIBundleBuilder representing an existing instruction or bundle
     523             :   /// that has MI as its head.
     524             :   explicit MIBundleBuilder(MachineInstr *MI)
     525         171 :       : MBB(*MI->getParent()), Begin(MI),
     526         438 :         End(getBundleEnd(MI->getIterator())) {}
     527             : 
     528             :   /// Return a reference to the basic block containing this bundle.
     529             :   MachineBasicBlock &getMBB() const { return MBB; }
     530             : 
     531             :   /// Return true if no instructions have been inserted in this bundle yet.
     532             :   /// Empty bundles aren't representable in a MachineBasicBlock.
     533             :   bool empty() const { return Begin == End; }
     534             : 
     535             :   /// Return an iterator to the first bundled instruction.
     536             :   MachineBasicBlock::instr_iterator begin() const { return Begin; }
     537             : 
     538             :   /// Return an iterator beyond the last bundled instruction.
     539             :   MachineBasicBlock::instr_iterator end() const { return End; }
     540             : 
     541             :   /// Insert MI into this bundle before I which must point to an instruction in
     542             :   /// the bundle, or end().
     543       25895 :   MIBundleBuilder &insert(MachineBasicBlock::instr_iterator I,
     544             :                           MachineInstr *MI) {
     545       25895 :     MBB.insert(I, MI);
     546       25895 :     if (I == Begin) {
     547       24633 :       if (!empty())
     548       24063 :         MI->bundleWithSucc();
     549       24633 :       Begin = MI->getIterator();
     550       24633 :       return *this;
     551             :     }
     552        1262 :     if (I == End) {
     553        1262 :       MI->bundleWithPred();
     554        1262 :       return *this;
     555             :     }
     556             :     // MI was inserted in the middle of the bundle, so its neighbors' flags are
     557             :     // already fine. Update MI's bundle flags manually.
     558             :     MI->setFlag(MachineInstr::BundledPred);
     559             :     MI->setFlag(MachineInstr::BundledSucc);
     560           0 :     return *this;
     561             :   }
     562             : 
     563             :   /// Insert MI into MBB by prepending it to the instructions in the bundle.
     564             :   /// MI will become the first instruction in the bundle.
     565             :   MIBundleBuilder &prepend(MachineInstr *MI) {
     566       24063 :     return insert(begin(), MI);
     567             :   }
     568             : 
     569             :   /// Insert MI into MBB by appending it to the instructions in the bundle.
     570             :   /// MI will become the last instruction in the bundle.
     571             :   MIBundleBuilder &append(MachineInstr *MI) {
     572        1832 :     return insert(end(), MI);
     573             :   }
     574             : };
     575             : 
     576             : } // end namespace llvm
     577             : 
     578             : #endif // LLVM_CODEGEN_MACHINEINSTRBUILDER_H

Generated by: LCOV version 1.13