LLVM API Documentation

MachineInstrBundle.cpp
Go to the documentation of this file.
00001 //===-- lib/CodeGen/MachineInstrBundle.cpp --------------------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #include "llvm/CodeGen/MachineInstrBundle.h"
00011 #include "llvm/ADT/SmallSet.h"
00012 #include "llvm/ADT/SmallVector.h"
00013 #include "llvm/CodeGen/MachineFunctionPass.h"
00014 #include "llvm/CodeGen/MachineInstrBuilder.h"
00015 #include "llvm/CodeGen/Passes.h"
00016 #include "llvm/Target/TargetInstrInfo.h"
00017 #include "llvm/Target/TargetMachine.h"
00018 #include "llvm/Target/TargetRegisterInfo.h"
00019 #include "llvm/Target/TargetSubtargetInfo.h"
00020 using namespace llvm;
00021 
00022 namespace {
00023   class UnpackMachineBundles : public MachineFunctionPass {
00024   public:
00025     static char ID; // Pass identification
00026     UnpackMachineBundles() : MachineFunctionPass(ID) {
00027       initializeUnpackMachineBundlesPass(*PassRegistry::getPassRegistry());
00028     }
00029 
00030     bool runOnMachineFunction(MachineFunction &MF) override;
00031   };
00032 } // end anonymous namespace
00033 
00034 char UnpackMachineBundles::ID = 0;
00035 char &llvm::UnpackMachineBundlesID = UnpackMachineBundles::ID;
00036 INITIALIZE_PASS(UnpackMachineBundles, "unpack-mi-bundles",
00037                 "Unpack machine instruction bundles", false, false)
00038 
00039 bool UnpackMachineBundles::runOnMachineFunction(MachineFunction &MF) {
00040   bool Changed = false;
00041   for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
00042     MachineBasicBlock *MBB = &*I;
00043 
00044     for (MachineBasicBlock::instr_iterator MII = MBB->instr_begin(),
00045            MIE = MBB->instr_end(); MII != MIE; ) {
00046       MachineInstr *MI = &*MII;
00047 
00048       // Remove BUNDLE instruction and the InsideBundle flags from bundled
00049       // instructions.
00050       if (MI->isBundle()) {
00051         while (++MII != MIE && MII->isBundledWithPred()) {
00052           MII->unbundleFromPred();
00053           for (unsigned i = 0, e = MII->getNumOperands(); i != e; ++i) {
00054             MachineOperand &MO = MII->getOperand(i);
00055             if (MO.isReg() && MO.isInternalRead())
00056               MO.setIsInternalRead(false);
00057           }
00058         }
00059         MI->eraseFromParent();
00060 
00061         Changed = true;
00062         continue;
00063       }
00064 
00065       ++MII;
00066     }
00067   }
00068 
00069   return Changed;
00070 }
00071 
00072 
00073 namespace {
00074   class FinalizeMachineBundles : public MachineFunctionPass {
00075   public:
00076     static char ID; // Pass identification
00077     FinalizeMachineBundles() : MachineFunctionPass(ID) {
00078       initializeFinalizeMachineBundlesPass(*PassRegistry::getPassRegistry());
00079     }
00080 
00081     bool runOnMachineFunction(MachineFunction &MF) override;
00082   };
00083 } // end anonymous namespace
00084 
00085 char FinalizeMachineBundles::ID = 0;
00086 char &llvm::FinalizeMachineBundlesID = FinalizeMachineBundles::ID;
00087 INITIALIZE_PASS(FinalizeMachineBundles, "finalize-mi-bundles",
00088                 "Finalize machine instruction bundles", false, false)
00089 
00090 bool FinalizeMachineBundles::runOnMachineFunction(MachineFunction &MF) {
00091   return llvm::finalizeBundles(MF);
00092 }
00093 
00094 
00095 /// finalizeBundle - Finalize a machine instruction bundle which includes
00096 /// a sequence of instructions starting from FirstMI to LastMI (exclusive).
00097 /// This routine adds a BUNDLE instruction to represent the bundle, it adds
00098 /// IsInternalRead markers to MachineOperands which are defined inside the
00099 /// bundle, and it copies externally visible defs and uses to the BUNDLE
00100 /// instruction.
00101 void llvm::finalizeBundle(MachineBasicBlock &MBB,
00102                           MachineBasicBlock::instr_iterator FirstMI,
00103                           MachineBasicBlock::instr_iterator LastMI) {
00104   assert(FirstMI != LastMI && "Empty bundle?");
00105   MIBundleBuilder Bundle(MBB, FirstMI, LastMI);
00106 
00107   const TargetMachine &TM = MBB.getParent()->getTarget();
00108   const TargetInstrInfo *TII = TM.getSubtargetImpl()->getInstrInfo();
00109   const TargetRegisterInfo *TRI = TM.getSubtargetImpl()->getRegisterInfo();
00110 
00111   MachineInstrBuilder MIB = BuildMI(*MBB.getParent(), FirstMI->getDebugLoc(),
00112                                     TII->get(TargetOpcode::BUNDLE));
00113   Bundle.prepend(MIB);
00114 
00115   SmallVector<unsigned, 32> LocalDefs;
00116   SmallSet<unsigned, 32> LocalDefSet;
00117   SmallSet<unsigned, 8> DeadDefSet;
00118   SmallSet<unsigned, 16> KilledDefSet;
00119   SmallVector<unsigned, 8> ExternUses;
00120   SmallSet<unsigned, 8> ExternUseSet;
00121   SmallSet<unsigned, 8> KilledUseSet;
00122   SmallSet<unsigned, 8> UndefUseSet;
00123   SmallVector<MachineOperand*, 4> Defs;
00124   for (; FirstMI != LastMI; ++FirstMI) {
00125     for (unsigned i = 0, e = FirstMI->getNumOperands(); i != e; ++i) {
00126       MachineOperand &MO = FirstMI->getOperand(i);
00127       if (!MO.isReg())
00128         continue;
00129       if (MO.isDef()) {
00130         Defs.push_back(&MO);
00131         continue;
00132       }
00133 
00134       unsigned Reg = MO.getReg();
00135       if (!Reg)
00136         continue;
00137       assert(TargetRegisterInfo::isPhysicalRegister(Reg));
00138       if (LocalDefSet.count(Reg)) {
00139         MO.setIsInternalRead();
00140         if (MO.isKill())
00141           // Internal def is now killed.
00142           KilledDefSet.insert(Reg);
00143       } else {
00144         if (ExternUseSet.insert(Reg)) {
00145           ExternUses.push_back(Reg);
00146           if (MO.isUndef())
00147             UndefUseSet.insert(Reg);
00148         }
00149         if (MO.isKill())
00150           // External def is now killed.
00151           KilledUseSet.insert(Reg);
00152       }
00153     }
00154 
00155     for (unsigned i = 0, e = Defs.size(); i != e; ++i) {
00156       MachineOperand &MO = *Defs[i];
00157       unsigned Reg = MO.getReg();
00158       if (!Reg)
00159         continue;
00160 
00161       if (LocalDefSet.insert(Reg)) {
00162         LocalDefs.push_back(Reg);
00163         if (MO.isDead()) {
00164           DeadDefSet.insert(Reg);
00165         }
00166       } else {
00167         // Re-defined inside the bundle, it's no longer killed.
00168         KilledDefSet.erase(Reg);
00169         if (!MO.isDead())
00170           // Previously defined but dead.
00171           DeadDefSet.erase(Reg);
00172       }
00173 
00174       if (!MO.isDead()) {
00175         for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
00176           unsigned SubReg = *SubRegs;
00177           if (LocalDefSet.insert(SubReg))
00178             LocalDefs.push_back(SubReg);
00179         }
00180       }
00181     }
00182 
00183     Defs.clear();
00184   }
00185 
00186   SmallSet<unsigned, 32> Added;
00187   for (unsigned i = 0, e = LocalDefs.size(); i != e; ++i) {
00188     unsigned Reg = LocalDefs[i];
00189     if (Added.insert(Reg)) {
00190       // If it's not live beyond end of the bundle, mark it dead.
00191       bool isDead = DeadDefSet.count(Reg) || KilledDefSet.count(Reg);
00192       MIB.addReg(Reg, getDefRegState(true) | getDeadRegState(isDead) |
00193                  getImplRegState(true));
00194     }
00195   }
00196 
00197   for (unsigned i = 0, e = ExternUses.size(); i != e; ++i) {
00198     unsigned Reg = ExternUses[i];
00199     bool isKill = KilledUseSet.count(Reg);
00200     bool isUndef = UndefUseSet.count(Reg);
00201     MIB.addReg(Reg, getKillRegState(isKill) | getUndefRegState(isUndef) |
00202                getImplRegState(true));
00203   }
00204 }
00205 
00206 /// finalizeBundle - Same functionality as the previous finalizeBundle except
00207 /// the last instruction in the bundle is not provided as an input. This is
00208 /// used in cases where bundles are pre-determined by marking instructions
00209 /// with 'InsideBundle' marker. It returns the MBB instruction iterator that
00210 /// points to the end of the bundle.
00211 MachineBasicBlock::instr_iterator
00212 llvm::finalizeBundle(MachineBasicBlock &MBB,
00213                      MachineBasicBlock::instr_iterator FirstMI) {
00214   MachineBasicBlock::instr_iterator E = MBB.instr_end();
00215   MachineBasicBlock::instr_iterator LastMI = std::next(FirstMI);
00216   while (LastMI != E && LastMI->isInsideBundle())
00217     ++LastMI;
00218   finalizeBundle(MBB, FirstMI, LastMI);
00219   return LastMI;
00220 }
00221 
00222 /// finalizeBundles - Finalize instruction bundles in the specified
00223 /// MachineFunction. Return true if any bundles are finalized.
00224 bool llvm::finalizeBundles(MachineFunction &MF) {
00225   bool Changed = false;
00226   for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
00227     MachineBasicBlock &MBB = *I;
00228     MachineBasicBlock::instr_iterator MII = MBB.instr_begin();
00229     MachineBasicBlock::instr_iterator MIE = MBB.instr_end();
00230     if (MII == MIE)
00231       continue;
00232     assert(!MII->isInsideBundle() &&
00233            "First instr cannot be inside bundle before finalization!");
00234 
00235     for (++MII; MII != MIE; ) {
00236       if (!MII->isInsideBundle())
00237         ++MII;
00238       else {
00239         MII = finalizeBundle(MBB, std::prev(MII));
00240         Changed = true;
00241       }
00242     }
00243   }
00244 
00245   return Changed;
00246 }
00247 
00248 //===----------------------------------------------------------------------===//
00249 // MachineOperand iterator
00250 //===----------------------------------------------------------------------===//
00251 
00252 MachineOperandIteratorBase::VirtRegInfo
00253 MachineOperandIteratorBase::analyzeVirtReg(unsigned Reg,
00254                     SmallVectorImpl<std::pair<MachineInstr*, unsigned> > *Ops) {
00255   VirtRegInfo RI = { false, false, false };
00256   for(; isValid(); ++*this) {
00257     MachineOperand &MO = deref();
00258     if (!MO.isReg() || MO.getReg() != Reg)
00259       continue;
00260 
00261     // Remember each (MI, OpNo) that refers to Reg.
00262     if (Ops)
00263       Ops->push_back(std::make_pair(MO.getParent(), getOperandNo()));
00264 
00265     // Both defs and uses can read virtual registers.
00266     if (MO.readsReg()) {
00267       RI.Reads = true;
00268       if (MO.isDef())
00269         RI.Tied = true;
00270     }
00271 
00272     // Only defs can write.
00273     if (MO.isDef())
00274       RI.Writes = true;
00275     else if (!RI.Tied && MO.getParent()->isRegTiedToDefOperand(getOperandNo()))
00276       RI.Tied = true;
00277   }
00278   return RI;
00279 }
00280 
00281 MachineOperandIteratorBase::PhysRegInfo
00282 MachineOperandIteratorBase::analyzePhysReg(unsigned Reg,
00283                                            const TargetRegisterInfo *TRI) {
00284   bool AllDefsDead = true;
00285   PhysRegInfo PRI = {false, false, false, false, false, false};
00286 
00287   assert(TargetRegisterInfo::isPhysicalRegister(Reg) &&
00288          "analyzePhysReg not given a physical register!");
00289   for (; isValid(); ++*this) {
00290     MachineOperand &MO = deref();
00291 
00292     if (MO.isRegMask() && MO.clobbersPhysReg(Reg))
00293       PRI.Clobbers = true;    // Regmask clobbers Reg.
00294 
00295     if (!MO.isReg())
00296       continue;
00297 
00298     unsigned MOReg = MO.getReg();
00299     if (!MOReg || !TargetRegisterInfo::isPhysicalRegister(MOReg))
00300       continue;
00301 
00302     bool IsRegOrSuperReg = MOReg == Reg || TRI->isSubRegister(MOReg, Reg);
00303     bool IsRegOrOverlapping = MOReg == Reg || TRI->regsOverlap(MOReg, Reg);
00304 
00305     if (IsRegOrSuperReg && MO.readsReg()) {
00306       // Reg or a super-reg is read, and perhaps killed also.
00307       PRI.Reads = true;
00308       PRI.Kills = MO.isKill();
00309     }
00310 
00311     if (IsRegOrOverlapping && MO.readsReg()) {
00312       PRI.ReadsOverlap = true;// Reg or an overlapping register is read.
00313     }
00314 
00315     if (!MO.isDef())
00316       continue;
00317 
00318     if (IsRegOrSuperReg) {
00319       PRI.Defines = true;     // Reg or a super-register is defined.
00320       if (!MO.isDead())
00321         AllDefsDead = false;
00322     }
00323     if (IsRegOrOverlapping)
00324       PRI.Clobbers = true;    // Reg or an overlapping reg is defined.
00325   }
00326 
00327   if (AllDefsDead && PRI.Defines)
00328     PRI.DefinesDead = true;   // Reg or super-register was defined and was dead.
00329 
00330   return PRI;
00331 }