MachineInstrBundle.cpp revision ef2887d3486a1814e5a4c1c1c6acc7d815334c80
180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//===-- lib/CodeGen/MachineInstrBundle.cpp --------------------------------===//
280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//
380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//                     The LLVM Compiler Infrastructure
480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//
580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru// This file is distributed under the University of Illinois Open Source
680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru// License. See LICENSE.TXT for details.
780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//
880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//===----------------------------------------------------------------------===//
980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "llvm/CodeGen/MachineInstrBundle.h"
1180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "llvm/CodeGen/MachineInstrBuilder.h"
1280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "llvm/CodeGen/Passes.h"
1380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "llvm/CodeGen/MachineFunctionPass.h"
1480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "llvm/Target/TargetInstrInfo.h"
1580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "llvm/Target/TargetMachine.h"
1658190644c30e1c4aa8e527f3503c58f841e0fcf3Derek Sollenberger#include "llvm/Target/TargetRegisterInfo.h"
1780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "llvm/ADT/SmallSet.h"
187839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenberger#include "llvm/ADT/SmallVector.h"
197839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenbergerusing namespace llvm;
207839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenberger
2180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querunamespace {
2280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru  class UnpackMachineBundles : public MachineFunctionPass {
2380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru  public:
2480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    static char ID; // Pass identification
2580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    UnpackMachineBundles() : MachineFunctionPass(ID) {
26d686ac77c2c485c4a3302eda9c1de597a6f8c568Derek Sollenberger      initializeUnpackMachineBundlesPass(*PassRegistry::getPassRegistry());
2780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
2880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
2980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    virtual bool runOnMachineFunction(MachineFunction &MF);
3080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru  };
3180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru} // end anonymous namespace
3280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
3380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruchar UnpackMachineBundles::ID = 0;
3480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste QueruINITIALIZE_PASS(UnpackMachineBundles, "unpack-mi-bundles",
3580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                "Unpack machine instruction bundles", false, false)
3680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
3780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste QueruFunctionPass *llvm::createUnpackMachineBundlesPass() {
3880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru  return new UnpackMachineBundles();
3980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
4080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
4180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querubool UnpackMachineBundles::runOnMachineFunction(MachineFunction &MF) {
4280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru  bool Changed = false;
4380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru  for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
4480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    MachineBasicBlock *MBB = &*I;
4580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
4658190644c30e1c4aa8e527f3503c58f841e0fcf3Derek Sollenberger    for (MachineBasicBlock::instr_iterator MII = MBB->instr_begin(),
4758190644c30e1c4aa8e527f3503c58f841e0fcf3Derek Sollenberger           MIE = MBB->instr_end(); MII != MIE; ) {
4880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru      MachineInstr *MI = &*MII;
4980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
5080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru      // Remove BUNDLE instruction and the InsideBundle flags from bundled
5180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru      // instructions.
5280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru      if (MI->isBundle()) {
5358190644c30e1c4aa8e527f3503c58f841e0fcf3Derek Sollenberger        while (++MII != MIE && MII->isInsideBundle()) {
5480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru          MII->setIsInsideBundle(false);
5558190644c30e1c4aa8e527f3503c58f841e0fcf3Derek Sollenberger          for (unsigned i = 0, e = MII->getNumOperands(); i != e; ++i) {
5658190644c30e1c4aa8e527f3503c58f841e0fcf3Derek Sollenberger            MachineOperand &MO = MII->getOperand(i);
5758190644c30e1c4aa8e527f3503c58f841e0fcf3Derek Sollenberger            if (MO.isReg() && MO.isInternalRead())
5858190644c30e1c4aa8e527f3503c58f841e0fcf3Derek Sollenberger              MO.setIsInternalRead(false);
5958190644c30e1c4aa8e527f3503c58f841e0fcf3Derek Sollenberger          }
6058190644c30e1c4aa8e527f3503c58f841e0fcf3Derek Sollenberger        }
6158190644c30e1c4aa8e527f3503c58f841e0fcf3Derek Sollenberger        MI->eraseFromParent();
6258190644c30e1c4aa8e527f3503c58f841e0fcf3Derek Sollenberger
6380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        Changed = true;
6480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        continue;
6580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru      }
6680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
6780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru      ++MII;
6880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
6980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru  }
7080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
7180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru  return Changed;
7280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
7380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
7480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
7580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querunamespace {
7680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru  class FinalizeMachineBundles : public MachineFunctionPass {
7780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru  public:
7880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    static char ID; // Pass identification
7980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    FinalizeMachineBundles() : MachineFunctionPass(ID) {
8080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru      initializeFinalizeMachineBundlesPass(*PassRegistry::getPassRegistry());
8180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
8280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
8380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    virtual bool runOnMachineFunction(MachineFunction &MF);
8480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru  };
8580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru} // end anonymous namespace
8680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
8780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruchar FinalizeMachineBundles::ID = 0;
8880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste QueruINITIALIZE_PASS(FinalizeMachineBundles, "finalize-mi-bundles",
8980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                "Finalize machine instruction bundles", false, false)
9080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
9180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste QueruFunctionPass *llvm::createFinalizeMachineBundlesPass() {
9280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru  return new FinalizeMachineBundles();
9380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
9480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
9580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querubool FinalizeMachineBundles::runOnMachineFunction(MachineFunction &MF) {
9680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru  return llvm::finalizeBundles(MF);
9780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
9880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
9980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
10080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/// finalizeBundle - Finalize a machine instruction bundle which includes
10180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/// a sequence of instructions starting from FirstMI to LastMI (exclusive).
10280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/// This routine adds a BUNDLE instruction to represent the bundle, it adds
10380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/// IsInternalRead markers to MachineOperands which are defined inside the
10480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/// bundle, and it copies externally visible defs and uses to the BUNDLE
10580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/// instruction.
10680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruvoid llvm::finalizeBundle(MachineBasicBlock &MBB,
10780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                          MachineBasicBlock::instr_iterator FirstMI,
10880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                          MachineBasicBlock::instr_iterator LastMI) {
10980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru  assert(FirstMI != LastMI && "Empty bundle?");
11080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
11180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru  const TargetMachine &TM = MBB.getParent()->getTarget();
11280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru  const TargetInstrInfo *TII = TM.getInstrInfo();
11380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru  const TargetRegisterInfo *TRI = TM.getRegisterInfo();
11480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
11580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru  MachineInstrBuilder MIB = BuildMI(MBB, FirstMI, FirstMI->getDebugLoc(),
11680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                                    TII->get(TargetOpcode::BUNDLE));
11780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
11880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru  SmallVector<unsigned, 8> LocalDefs;
11980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru  SmallSet<unsigned, 8> LocalDefSet;
12080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru  SmallSet<unsigned, 8> DeadDefSet;
12180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru  SmallSet<unsigned, 8> KilledDefSet;
12280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru  SmallVector<unsigned, 8> ExternUses;
12380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru  SmallSet<unsigned, 8> ExternUseSet;
12458190644c30e1c4aa8e527f3503c58f841e0fcf3Derek Sollenberger  SmallSet<unsigned, 8> KilledUseSet;
12580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru  SmallSet<unsigned, 8> UndefUseSet;
12680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru  SmallVector<MachineOperand*, 4> Defs;
12780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru  for (; FirstMI != LastMI; ++FirstMI) {
12880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    for (unsigned i = 0, e = FirstMI->getNumOperands(); i != e; ++i) {
12980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru      MachineOperand &MO = FirstMI->getOperand(i);
13080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru      if (!MO.isReg())
13180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        continue;
13280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru      if (MO.isDef()) {
13380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        Defs.push_back(&MO);
13480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        continue;
13580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru      }
13680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
13780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru      unsigned Reg = MO.getReg();
13880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru      if (!Reg)
13980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        continue;
14080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru      assert(TargetRegisterInfo::isPhysicalRegister(Reg));
14180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru      if (LocalDefSet.count(Reg)) {
14280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        MO.setIsInternalRead();
14380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        if (MO.isKill())
14480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru          // Internal def is now killed.
14580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru          KilledDefSet.insert(Reg);
14680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru      } else {
14780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        if (ExternUseSet.insert(Reg)) {
14880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru          ExternUses.push_back(Reg);
14980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru          if (MO.isUndef())
15080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            UndefUseSet.insert(Reg);
15180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        }
15280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        if (MO.isKill())
15380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru          // External def is now killed.
15480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru          KilledUseSet.insert(Reg);
15580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru      }
15680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
15780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
15880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    for (unsigned i = 0, e = Defs.size(); i != e; ++i) {
15980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru      MachineOperand &MO = *Defs[i];
16080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru      unsigned Reg = MO.getReg();
16180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru      if (!Reg)
16280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        continue;
16380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
16480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru      if (LocalDefSet.insert(Reg)) {
16580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        LocalDefs.push_back(Reg);
16680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        if (MO.isDead()) {
16780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru          DeadDefSet.insert(Reg);
16880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        }
16980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru      } else {
17080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        // Re-defined inside the bundle, it's no longer killed.
17180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        KilledDefSet.erase(Reg);
17280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        if (!MO.isDead())
17380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru          // Previously defined but dead.
17480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru          DeadDefSet.erase(Reg);
17580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru      }
17680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
17780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru      if (!MO.isDead()) {
17880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
17980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru             unsigned SubReg = *SubRegs; ++SubRegs) {
18080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru          if (LocalDefSet.insert(SubReg))
18180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            LocalDefs.push_back(SubReg);
18280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        }
18380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru      }
18480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
18580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
18680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    FirstMI->setIsInsideBundle();
18780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    Defs.clear();
18880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru  }
18980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
19080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru  SmallSet<unsigned, 8> Added;
19180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru  for (unsigned i = 0, e = LocalDefs.size(); i != e; ++i) {
19280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    unsigned Reg = LocalDefs[i];
19380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    if (Added.insert(Reg)) {
19480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru      // If it's not live beyond end of the bundle, mark it dead.
19580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru      bool isDead = DeadDefSet.count(Reg) || KilledDefSet.count(Reg);
19680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru      MIB.addReg(Reg, getDefRegState(true) | getDeadRegState(isDead) |
19780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                 getImplRegState(true));
19880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
19980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru  }
20080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
20180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru  for (unsigned i = 0, e = ExternUses.size(); i != e; ++i) {
20280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    unsigned Reg = ExternUses[i];
20380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    bool isKill = KilledUseSet.count(Reg);
20480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    bool isUndef = UndefUseSet.count(Reg);
20580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    MIB.addReg(Reg, getKillRegState(isKill) | getUndefRegState(isUndef) |
20680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru               getImplRegState(true));
20780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru  }
20880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
20980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
21080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/// finalizeBundle - Same functionality as the previous finalizeBundle except
21180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/// the last instruction in the bundle is not provided as an input. This is
21280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/// used in cases where bundles are pre-determined by marking instructions
21380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/// with 'InsideBundle' marker. It returns the MBB instruction iterator that
21480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/// points to the end of the bundle.
21580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste QueruMachineBasicBlock::instr_iterator
21680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querullvm::finalizeBundle(MachineBasicBlock &MBB,
21780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                     MachineBasicBlock::instr_iterator FirstMI) {
21880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru  MachineBasicBlock::instr_iterator E = MBB.instr_end();
21980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru  MachineBasicBlock::instr_iterator LastMI = llvm::next(FirstMI);
22080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru  while (LastMI != E && LastMI->isInsideBundle())
22180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    ++LastMI;
22280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru  finalizeBundle(MBB, FirstMI, LastMI);
22380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru  return LastMI;
22480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
22580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
22680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/// finalizeBundles - Finalize instruction bundles in the specified
22780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/// MachineFunction. Return true if any bundles are finalized.
22880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querubool llvm::finalizeBundles(MachineFunction &MF) {
22980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru  bool Changed = false;
23080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru  for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
23180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    MachineBasicBlock &MBB = *I;
23280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
23380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    MachineBasicBlock::instr_iterator MII = MBB.instr_begin();
23480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    assert(!MII->isInsideBundle() &&
23580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru           "First instr cannot be inside bundle before finalization!");
23680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
23780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    MachineBasicBlock::instr_iterator MIE = MBB.instr_end();
23880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    for (++MII; MII != MIE; ) {
23980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru      if (!MII->isInsideBundle())
24080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        ++MII;
24180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru      else {
24280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        MII = finalizeBundle(MBB, llvm::prior(MII));
24380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        Changed = true;
24480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru      }
24580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
24680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru  }
24780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
24880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru  return Changed;
24980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
25080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru