10b57cec5SDimitry Andric //===- llvm/CodeGen/MachineInstrBundle.h - MI bundle utilities --*- C++ -*-===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This file provide utility functions to manipulate machine instruction 100b57cec5SDimitry Andric // bundles. 110b57cec5SDimitry Andric // 120b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 130b57cec5SDimitry Andric 140b57cec5SDimitry Andric #ifndef LLVM_CODEGEN_MACHINEINSTRBUNDLE_H 150b57cec5SDimitry Andric #define LLVM_CODEGEN_MACHINEINSTRBUNDLE_H 160b57cec5SDimitry Andric 170b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h" 180b57cec5SDimitry Andric 190b57cec5SDimitry Andric namespace llvm { 200b57cec5SDimitry Andric 210b57cec5SDimitry Andric /// finalizeBundle - Finalize a machine instruction bundle which includes 220b57cec5SDimitry Andric /// a sequence of instructions starting from FirstMI to LastMI (exclusive). 230b57cec5SDimitry Andric /// This routine adds a BUNDLE instruction to represent the bundle, it adds 240b57cec5SDimitry Andric /// IsInternalRead markers to MachineOperands which are defined inside the 250b57cec5SDimitry Andric /// bundle, and it copies externally visible defs and uses to the BUNDLE 260b57cec5SDimitry Andric /// instruction. 270b57cec5SDimitry Andric void finalizeBundle(MachineBasicBlock &MBB, 280b57cec5SDimitry Andric MachineBasicBlock::instr_iterator FirstMI, 290b57cec5SDimitry Andric MachineBasicBlock::instr_iterator LastMI); 300b57cec5SDimitry Andric 310b57cec5SDimitry Andric /// finalizeBundle - Same functionality as the previous finalizeBundle except 320b57cec5SDimitry Andric /// the last instruction in the bundle is not provided as an input. This is 330b57cec5SDimitry Andric /// used in cases where bundles are pre-determined by marking instructions 340b57cec5SDimitry Andric /// with 'InsideBundle' marker. It returns the MBB instruction iterator that 350b57cec5SDimitry Andric /// points to the end of the bundle. 360b57cec5SDimitry Andric MachineBasicBlock::instr_iterator finalizeBundle(MachineBasicBlock &MBB, 370b57cec5SDimitry Andric MachineBasicBlock::instr_iterator FirstMI); 380b57cec5SDimitry Andric 390b57cec5SDimitry Andric /// finalizeBundles - Finalize instruction bundles in the specified 400b57cec5SDimitry Andric /// MachineFunction. Return true if any bundles are finalized. 410b57cec5SDimitry Andric bool finalizeBundles(MachineFunction &MF); 420b57cec5SDimitry Andric 430b57cec5SDimitry Andric /// Returns an iterator to the first instruction in the bundle containing \p I. 440b57cec5SDimitry Andric inline MachineBasicBlock::instr_iterator getBundleStart( 450b57cec5SDimitry Andric MachineBasicBlock::instr_iterator I) { 460b57cec5SDimitry Andric while (I->isBundledWithPred()) 470b57cec5SDimitry Andric --I; 480b57cec5SDimitry Andric return I; 490b57cec5SDimitry Andric } 500b57cec5SDimitry Andric 510b57cec5SDimitry Andric /// Returns an iterator to the first instruction in the bundle containing \p I. 520b57cec5SDimitry Andric inline MachineBasicBlock::const_instr_iterator getBundleStart( 530b57cec5SDimitry Andric MachineBasicBlock::const_instr_iterator I) { 540b57cec5SDimitry Andric while (I->isBundledWithPred()) 550b57cec5SDimitry Andric --I; 560b57cec5SDimitry Andric return I; 570b57cec5SDimitry Andric } 580b57cec5SDimitry Andric 590b57cec5SDimitry Andric /// Returns an iterator pointing beyond the bundle containing \p I. 600b57cec5SDimitry Andric inline MachineBasicBlock::instr_iterator getBundleEnd( 610b57cec5SDimitry Andric MachineBasicBlock::instr_iterator I) { 620b57cec5SDimitry Andric while (I->isBundledWithSucc()) 630b57cec5SDimitry Andric ++I; 640b57cec5SDimitry Andric ++I; 650b57cec5SDimitry Andric return I; 660b57cec5SDimitry Andric } 670b57cec5SDimitry Andric 680b57cec5SDimitry Andric /// Returns an iterator pointing beyond the bundle containing \p I. 690b57cec5SDimitry Andric inline MachineBasicBlock::const_instr_iterator getBundleEnd( 700b57cec5SDimitry Andric MachineBasicBlock::const_instr_iterator I) { 710b57cec5SDimitry Andric while (I->isBundledWithSucc()) 720b57cec5SDimitry Andric ++I; 730b57cec5SDimitry Andric ++I; 740b57cec5SDimitry Andric return I; 750b57cec5SDimitry Andric } 760b57cec5SDimitry Andric 770b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 78*480093f4SDimitry Andric // MachineBundleOperand iterator 790b57cec5SDimitry Andric // 800b57cec5SDimitry Andric 81*480093f4SDimitry Andric /// MIBundleOperandIteratorBase - Iterator that visits all operands in a bundle 82*480093f4SDimitry Andric /// of MachineInstrs. This class is not intended to be used directly, use one 83*480093f4SDimitry Andric /// of the sub-classes instead. 840b57cec5SDimitry Andric /// 850b57cec5SDimitry Andric /// Intended use: 860b57cec5SDimitry Andric /// 870b57cec5SDimitry Andric /// for (MIBundleOperands MIO(MI); MIO.isValid(); ++MIO) { 880b57cec5SDimitry Andric /// if (!MIO->isReg()) 890b57cec5SDimitry Andric /// continue; 900b57cec5SDimitry Andric /// ... 910b57cec5SDimitry Andric /// } 920b57cec5SDimitry Andric /// 93*480093f4SDimitry Andric template <typename ValueT> 94*480093f4SDimitry Andric class MIBundleOperandIteratorBase 95*480093f4SDimitry Andric : public iterator_facade_base<MIBundleOperandIteratorBase<ValueT>, 96*480093f4SDimitry Andric std::forward_iterator_tag, ValueT> { 970b57cec5SDimitry Andric MachineBasicBlock::instr_iterator InstrI, InstrE; 980b57cec5SDimitry Andric MachineInstr::mop_iterator OpI, OpE; 990b57cec5SDimitry Andric 1000b57cec5SDimitry Andric // If the operands on InstrI are exhausted, advance InstrI to the next 1010b57cec5SDimitry Andric // bundled instruction with operands. 1020b57cec5SDimitry Andric void advance() { 1030b57cec5SDimitry Andric while (OpI == OpE) { 1040b57cec5SDimitry Andric // Don't advance off the basic block, or into a new bundle. 105*480093f4SDimitry Andric if (++InstrI == InstrE || !InstrI->isInsideBundle()) { 106*480093f4SDimitry Andric InstrI = InstrE; 1070b57cec5SDimitry Andric break; 108*480093f4SDimitry Andric } 1090b57cec5SDimitry Andric OpI = InstrI->operands_begin(); 1100b57cec5SDimitry Andric OpE = InstrI->operands_end(); 1110b57cec5SDimitry Andric } 1120b57cec5SDimitry Andric } 1130b57cec5SDimitry Andric 1140b57cec5SDimitry Andric protected: 115*480093f4SDimitry Andric /// MIBundleOperandIteratorBase - Create an iterator that visits all operands 1160b57cec5SDimitry Andric /// on MI, or all operands on every instruction in the bundle containing MI. 1170b57cec5SDimitry Andric /// 1180b57cec5SDimitry Andric /// @param MI The instruction to examine. 1190b57cec5SDimitry Andric /// 120*480093f4SDimitry Andric explicit MIBundleOperandIteratorBase(MachineInstr &MI) { 1210b57cec5SDimitry Andric InstrI = getBundleStart(MI.getIterator()); 1220b57cec5SDimitry Andric InstrE = MI.getParent()->instr_end(); 1230b57cec5SDimitry Andric OpI = InstrI->operands_begin(); 1240b57cec5SDimitry Andric OpE = InstrI->operands_end(); 1250b57cec5SDimitry Andric advance(); 1260b57cec5SDimitry Andric } 1270b57cec5SDimitry Andric 128*480093f4SDimitry Andric /// Constructor for an iterator past the last iteration: both instruction 129*480093f4SDimitry Andric /// iterators point to the end of the BB and OpI == OpE. 130*480093f4SDimitry Andric explicit MIBundleOperandIteratorBase(MachineBasicBlock::instr_iterator InstrE, 131*480093f4SDimitry Andric MachineInstr::mop_iterator OpE) 132*480093f4SDimitry Andric : InstrI(InstrE), InstrE(InstrE), OpI(OpE), OpE(OpE) {} 1330b57cec5SDimitry Andric 1340b57cec5SDimitry Andric public: 1350b57cec5SDimitry Andric /// isValid - Returns true until all the operands have been visited. 1360b57cec5SDimitry Andric bool isValid() const { return OpI != OpE; } 1370b57cec5SDimitry Andric 1380b57cec5SDimitry Andric /// Preincrement. Move to the next operand. 1390b57cec5SDimitry Andric void operator++() { 1400b57cec5SDimitry Andric assert(isValid() && "Cannot advance MIOperands beyond the last operand"); 1410b57cec5SDimitry Andric ++OpI; 1420b57cec5SDimitry Andric advance(); 1430b57cec5SDimitry Andric } 1440b57cec5SDimitry Andric 145*480093f4SDimitry Andric ValueT &operator*() const { return *OpI; } 146*480093f4SDimitry Andric ValueT *operator->() const { return &*OpI; } 147*480093f4SDimitry Andric 148*480093f4SDimitry Andric bool operator==(const MIBundleOperandIteratorBase &Arg) const { 149*480093f4SDimitry Andric // Iterators are equal, if InstrI matches and either OpIs match or OpI == 150*480093f4SDimitry Andric // OpE match for both. The second condition allows us to construct an 'end' 151*480093f4SDimitry Andric // iterator, without finding the last instruction in a bundle up-front. 152*480093f4SDimitry Andric return InstrI == Arg.InstrI && 153*480093f4SDimitry Andric (OpI == Arg.OpI || (OpI == OpE && Arg.OpI == Arg.OpE)); 154*480093f4SDimitry Andric } 1550b57cec5SDimitry Andric /// getOperandNo - Returns the number of the current operand relative to its 1560b57cec5SDimitry Andric /// instruction. 1570b57cec5SDimitry Andric /// 1580b57cec5SDimitry Andric unsigned getOperandNo() const { 1590b57cec5SDimitry Andric return OpI - InstrI->operands_begin(); 1600b57cec5SDimitry Andric } 161*480093f4SDimitry Andric }; 1620b57cec5SDimitry Andric 163*480093f4SDimitry Andric /// MIBundleOperands - Iterate over all operands in a bundle of machine 164*480093f4SDimitry Andric /// instructions. 165*480093f4SDimitry Andric /// 166*480093f4SDimitry Andric class MIBundleOperands : public MIBundleOperandIteratorBase<MachineOperand> { 167*480093f4SDimitry Andric /// Constructor for an iterator past the last iteration. 168*480093f4SDimitry Andric MIBundleOperands(MachineBasicBlock::instr_iterator InstrE, 169*480093f4SDimitry Andric MachineInstr::mop_iterator OpE) 170*480093f4SDimitry Andric : MIBundleOperandIteratorBase(InstrE, OpE) {} 171*480093f4SDimitry Andric 172*480093f4SDimitry Andric public: 173*480093f4SDimitry Andric MIBundleOperands(MachineInstr &MI) : MIBundleOperandIteratorBase(MI) {} 174*480093f4SDimitry Andric 175*480093f4SDimitry Andric /// Returns an iterator past the last iteration. 176*480093f4SDimitry Andric static MIBundleOperands end(const MachineBasicBlock &MBB) { 177*480093f4SDimitry Andric return {const_cast<MachineBasicBlock &>(MBB).instr_end(), 178*480093f4SDimitry Andric const_cast<MachineBasicBlock &>(MBB).instr_begin()->operands_end()}; 179*480093f4SDimitry Andric } 180*480093f4SDimitry Andric }; 181*480093f4SDimitry Andric 182*480093f4SDimitry Andric /// ConstMIBundleOperands - Iterate over all operands in a const bundle of 183*480093f4SDimitry Andric /// machine instructions. 184*480093f4SDimitry Andric /// 185*480093f4SDimitry Andric class ConstMIBundleOperands 186*480093f4SDimitry Andric : public MIBundleOperandIteratorBase<const MachineOperand> { 187*480093f4SDimitry Andric 188*480093f4SDimitry Andric /// Constructor for an iterator past the last iteration. 189*480093f4SDimitry Andric ConstMIBundleOperands(MachineBasicBlock::instr_iterator InstrE, 190*480093f4SDimitry Andric MachineInstr::mop_iterator OpE) 191*480093f4SDimitry Andric : MIBundleOperandIteratorBase(InstrE, OpE) {} 192*480093f4SDimitry Andric 193*480093f4SDimitry Andric public: 194*480093f4SDimitry Andric ConstMIBundleOperands(const MachineInstr &MI) 195*480093f4SDimitry Andric : MIBundleOperandIteratorBase(const_cast<MachineInstr &>(MI)) {} 196*480093f4SDimitry Andric 197*480093f4SDimitry Andric /// Returns an iterator past the last iteration. 198*480093f4SDimitry Andric static ConstMIBundleOperands end(const MachineBasicBlock &MBB) { 199*480093f4SDimitry Andric return {const_cast<MachineBasicBlock &>(MBB).instr_end(), 200*480093f4SDimitry Andric const_cast<MachineBasicBlock &>(MBB).instr_begin()->operands_end()}; 201*480093f4SDimitry Andric } 202*480093f4SDimitry Andric }; 203*480093f4SDimitry Andric 204*480093f4SDimitry Andric inline iterator_range<ConstMIBundleOperands> 205*480093f4SDimitry Andric const_mi_bundle_ops(const MachineInstr &MI) { 206*480093f4SDimitry Andric return make_range(ConstMIBundleOperands(MI), 207*480093f4SDimitry Andric ConstMIBundleOperands::end(*MI.getParent())); 208*480093f4SDimitry Andric } 209*480093f4SDimitry Andric 210*480093f4SDimitry Andric inline iterator_range<MIBundleOperands> mi_bundle_ops(MachineInstr &MI) { 211*480093f4SDimitry Andric return make_range(MIBundleOperands(MI), 212*480093f4SDimitry Andric MIBundleOperands::end(*MI.getParent())); 213*480093f4SDimitry Andric } 214*480093f4SDimitry Andric 215*480093f4SDimitry Andric /// VirtRegInfo - Information about a virtual register used by a set of 216*480093f4SDimitry Andric /// operands. 2170b57cec5SDimitry Andric /// 2180b57cec5SDimitry Andric struct VirtRegInfo { 2190b57cec5SDimitry Andric /// Reads - One of the operands read the virtual register. This does not 2200b57cec5SDimitry Andric /// include undef or internal use operands, see MO::readsReg(). 2210b57cec5SDimitry Andric bool Reads; 2220b57cec5SDimitry Andric 2230b57cec5SDimitry Andric /// Writes - One of the operands writes the virtual register. 2240b57cec5SDimitry Andric bool Writes; 2250b57cec5SDimitry Andric 2260b57cec5SDimitry Andric /// Tied - Uses and defs must use the same register. This can be because of 2270b57cec5SDimitry Andric /// a two-address constraint, or there may be a partial redefinition of a 2280b57cec5SDimitry Andric /// sub-register. 2290b57cec5SDimitry Andric bool Tied; 2300b57cec5SDimitry Andric }; 2310b57cec5SDimitry Andric 232*480093f4SDimitry Andric /// AnalyzeVirtRegInBundle - Analyze how the current instruction or bundle uses 233*480093f4SDimitry Andric /// a virtual register. This function should not be called after operator++(), 234*480093f4SDimitry Andric /// it expects a fresh iterator. 235*480093f4SDimitry Andric /// 236*480093f4SDimitry Andric /// @param Reg The virtual register to analyze. 237*480093f4SDimitry Andric /// @param Ops When set, this vector will receive an (MI, OpNum) entry for 238*480093f4SDimitry Andric /// each operand referring to Reg. 239*480093f4SDimitry Andric /// @returns A filled-in RegInfo struct. 240*480093f4SDimitry Andric VirtRegInfo AnalyzeVirtRegInBundle( 241*480093f4SDimitry Andric MachineInstr &MI, unsigned Reg, 242*480093f4SDimitry Andric SmallVectorImpl<std::pair<MachineInstr *, unsigned>> *Ops = nullptr); 243*480093f4SDimitry Andric 2440b57cec5SDimitry Andric /// Information about how a physical register Reg is used by a set of 2450b57cec5SDimitry Andric /// operands. 2460b57cec5SDimitry Andric struct PhysRegInfo { 2470b57cec5SDimitry Andric /// There is a regmask operand indicating Reg is clobbered. 2480b57cec5SDimitry Andric /// \see MachineOperand::CreateRegMask(). 2490b57cec5SDimitry Andric bool Clobbered; 2500b57cec5SDimitry Andric 2510b57cec5SDimitry Andric /// Reg or one of its aliases is defined. The definition may only cover 2520b57cec5SDimitry Andric /// parts of the register. 2530b57cec5SDimitry Andric bool Defined; 2540b57cec5SDimitry Andric /// Reg or a super-register is defined. The definition covers the full 2550b57cec5SDimitry Andric /// register. 2560b57cec5SDimitry Andric bool FullyDefined; 2570b57cec5SDimitry Andric 2580b57cec5SDimitry Andric /// Reg or one of its aliases is read. The register may only be read 2590b57cec5SDimitry Andric /// partially. 2600b57cec5SDimitry Andric bool Read; 2610b57cec5SDimitry Andric /// Reg or a super-register is read. The full register is read. 2620b57cec5SDimitry Andric bool FullyRead; 2630b57cec5SDimitry Andric 2640b57cec5SDimitry Andric /// Either: 2650b57cec5SDimitry Andric /// - Reg is FullyDefined and all defs of reg or an overlapping 2660b57cec5SDimitry Andric /// register are dead, or 2670b57cec5SDimitry Andric /// - Reg is completely dead because "defined" by a clobber. 2680b57cec5SDimitry Andric bool DeadDef; 2690b57cec5SDimitry Andric 2700b57cec5SDimitry Andric /// Reg is Defined and all defs of reg or an overlapping register are 2710b57cec5SDimitry Andric /// dead. 2720b57cec5SDimitry Andric bool PartialDeadDef; 2730b57cec5SDimitry Andric 2740b57cec5SDimitry Andric /// There is a use operand of reg or a super-register with kill flag set. 2750b57cec5SDimitry Andric bool Killed; 2760b57cec5SDimitry Andric }; 2770b57cec5SDimitry Andric 278*480093f4SDimitry Andric /// AnalyzePhysRegInBundle - Analyze how the current instruction or bundle uses 279*480093f4SDimitry Andric /// a physical register. This function should not be called after operator++(), 2800b57cec5SDimitry Andric /// it expects a fresh iterator. 2810b57cec5SDimitry Andric /// 2820b57cec5SDimitry Andric /// @param Reg The physical register to analyze. 2830b57cec5SDimitry Andric /// @returns A filled-in PhysRegInfo struct. 284*480093f4SDimitry Andric PhysRegInfo AnalyzePhysRegInBundle(const MachineInstr &MI, unsigned Reg, 285*480093f4SDimitry Andric const TargetRegisterInfo *TRI); 2860b57cec5SDimitry Andric 2870b57cec5SDimitry Andric } // End llvm namespace 2880b57cec5SDimitry Andric 2890b57cec5SDimitry Andric #endif 290