- Slight change to finalizeBundle() interface. LastMI is not exclusive (pointing
[oota-llvm.git] / lib / CodeGen / MachineInstrBundle.cpp
1 //===-- lib/CodeGen/MachineInstrBundle.cpp --------------------------------===//
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 #include "llvm/CodeGen/MachineInstrBundle.h"
11 #include "llvm/CodeGen/MachineInstrBuilder.h"
12 #include "llvm/CodeGen/Passes.h"
13 #include "llvm/CodeGen/MachineFunctionPass.h"
14 #include "llvm/Target/TargetInstrInfo.h"
15 #include "llvm/Target/TargetMachine.h"
16 #include "llvm/Target/TargetRegisterInfo.h"
17 #include "llvm/ADT/SmallSet.h"
18 #include "llvm/ADT/SmallVector.h"
19 using namespace llvm;
20
21 namespace {
22   class UnpackMachineBundles : public MachineFunctionPass {
23   public:
24     static char ID; // Pass identification
25     UnpackMachineBundles() : MachineFunctionPass(ID) {
26       initializeUnpackMachineBundlesPass(*PassRegistry::getPassRegistry());
27     }
28
29     virtual bool runOnMachineFunction(MachineFunction &MF);
30   };
31 } // end anonymous namespace
32
33 char UnpackMachineBundles::ID = 0;
34 INITIALIZE_PASS(UnpackMachineBundles, "unpack-mi-bundle",
35                 "Unpack machine instruction bundles", false, false)
36
37 FunctionPass *llvm::createUnpackMachineBundlesPass() {
38   return new UnpackMachineBundles();
39 }
40
41 bool UnpackMachineBundles::runOnMachineFunction(MachineFunction &MF) {
42   bool Changed = false;
43   for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
44     MachineBasicBlock *MBB = &*I;
45
46     for (MachineBasicBlock::instr_iterator MII = MBB->instr_begin(),
47            MIE = MBB->instr_end(); MII != MIE; ) {
48       MachineInstr *MI = &*MII;
49
50       // Remove BUNDLE instruction and the InsideBundle flags from bundled
51       // instructions.
52       if (MI->isBundle()) {
53         while (++MII != MIE && MII->isInsideBundle()) {
54           MII->setIsInsideBundle(false);
55           for (unsigned i = 0, e = MII->getNumOperands(); i != e; ++i) {
56             MachineOperand &MO = MII->getOperand(i);
57             if (MO.isReg() && MO.isInternalRead())
58               MO.setIsInternalRead(false);
59           }
60         }
61         MI->eraseFromParent();
62
63         Changed = true;
64         continue;
65       }
66
67       ++MII;
68     }
69   }
70
71   return Changed;
72 }
73
74 /// finalizeBundle - Finalize a machine instruction bundle which includes
75 /// a sequence of instructions starting from FirstMI to LastMI (exclusive).
76 /// This routine adds a BUNDLE instruction to represent the bundle, it adds
77 /// IsInternalRead markers to MachineOperands which are defined inside the
78 /// bundle, and it copies externally visible defs and uses to the BUNDLE
79 /// instruction.
80 void llvm::finalizeBundle(MachineBasicBlock &MBB,
81                           MachineBasicBlock::instr_iterator FirstMI,
82                           MachineBasicBlock::instr_iterator LastMI) {
83   assert(FirstMI != LastMI && "Empty bundle?");
84
85   const TargetMachine &TM = MBB.getParent()->getTarget();
86   const TargetInstrInfo *TII = TM.getInstrInfo();
87   const TargetRegisterInfo *TRI = TM.getRegisterInfo();
88
89   MachineInstrBuilder MIB = BuildMI(MBB, FirstMI, FirstMI->getDebugLoc(),
90                                     TII->get(TargetOpcode::BUNDLE));
91
92   SmallVector<unsigned, 8> LocalDefs;
93   SmallSet<unsigned, 8> LocalDefSet;
94   SmallSet<unsigned, 8> DeadDefSet;
95   SmallSet<unsigned, 8> KilledDefSet;
96   SmallVector<unsigned, 8> ExternUses;
97   SmallSet<unsigned, 8> ExternUseSet;
98   SmallSet<unsigned, 8> KilledUseSet;
99   SmallSet<unsigned, 8> UndefUseSet;
100   SmallVector<MachineOperand*, 4> Defs;
101   for (; FirstMI != LastMI; ++FirstMI) {
102     for (unsigned i = 0, e = FirstMI->getNumOperands(); i != e; ++i) {
103       MachineOperand &MO = FirstMI->getOperand(i);
104       if (!MO.isReg())
105         continue;
106       if (MO.isDef()) {
107         Defs.push_back(&MO);
108         continue;
109       }
110
111       unsigned Reg = MO.getReg();
112       if (!Reg)
113         continue;
114       assert(TargetRegisterInfo::isPhysicalRegister(Reg));
115       if (LocalDefSet.count(Reg)) {
116         MO.setIsInternalRead();
117         if (MO.isKill())
118           // Internal def is now killed.
119           KilledDefSet.insert(Reg);
120       } else {
121         if (ExternUseSet.insert(Reg)) {
122           ExternUses.push_back(Reg);
123           if (MO.isUndef())
124             UndefUseSet.insert(Reg);
125         }
126         if (MO.isKill())
127           // External def is now killed.
128           KilledUseSet.insert(Reg);
129       }
130     }
131
132     for (unsigned i = 0, e = Defs.size(); i != e; ++i) {
133       MachineOperand &MO = *Defs[i];
134       unsigned Reg = MO.getReg();
135       if (!Reg)
136         continue;
137
138       if (LocalDefSet.insert(Reg)) {
139         LocalDefs.push_back(Reg);
140         if (MO.isDead()) {
141           DeadDefSet.insert(Reg);
142         }
143       } else {
144         // Re-defined inside the bundle, it's no longer killed.
145         KilledDefSet.erase(Reg);
146         if (!MO.isDead())
147           // Previously defined but dead.
148           DeadDefSet.erase(Reg);
149       }
150
151       if (!MO.isDead()) {
152         for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
153              unsigned SubReg = *SubRegs; ++SubRegs) {
154           if (LocalDefSet.insert(SubReg))
155             LocalDefs.push_back(SubReg);
156         }
157       }
158     }
159
160     FirstMI->setIsInsideBundle();
161     Defs.clear();
162   }
163
164   SmallSet<unsigned, 8> Added;
165   for (unsigned i = 0, e = LocalDefs.size(); i != e; ++i) {
166     unsigned Reg = LocalDefs[i];
167     if (Added.insert(Reg)) {
168       // If it's not live beyond end of the bundle, mark it dead.
169       bool isDead = DeadDefSet.count(Reg) || KilledDefSet.count(Reg);
170       MIB.addReg(Reg, getDefRegState(true) | getDeadRegState(isDead) |
171                  getImplRegState(true));
172     }
173   }
174
175   for (unsigned i = 0, e = ExternUses.size(); i != e; ++i) {
176     unsigned Reg = ExternUses[i];
177     bool isKill = KilledUseSet.count(Reg);
178     bool isUndef = UndefUseSet.count(Reg);
179     MIB.addReg(Reg, getKillRegState(isKill) | getUndefRegState(isUndef) |
180                getImplRegState(true));
181   }
182 }
183
184 /// finalizeBundle - Same functionality as the previous finalizeBundle except
185 /// the last instruction in the bundle is not provided as an input. This is
186 /// used in cases where bundles are pre-determined by marking instructions
187 /// with 'InsideBundle' marker.
188 void llvm::finalizeBundle(MachineBasicBlock &MBB,
189                           MachineBasicBlock::instr_iterator FirstMI) {
190   MachineBasicBlock::instr_iterator E = MBB.instr_end();
191   MachineBasicBlock::instr_iterator LastMI = llvm::next(FirstMI);
192   while (LastMI != E && LastMI->isInsideBundle())
193     ++LastMI;
194   finalizeBundle(MBB, FirstMI, LastMI);
195 }