blob: 70f97dedaaa5f20f5f052ffd385b8cab60f6b697 [file] [log] [blame]
Evan Chengddfd1372011-12-14 02:11:42 +00001//===-- 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"
19using namespace llvm;
20
21namespace {
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
33char UnpackMachineBundles::ID = 0;
Andrew Trick1dd8c852012-02-08 21:23:13 +000034char &llvm::UnpackMachineBundlesID = UnpackMachineBundles::ID;
Evan Chengef2887d2012-01-19 07:47:03 +000035INITIALIZE_PASS(UnpackMachineBundles, "unpack-mi-bundles",
Evan Chengddfd1372011-12-14 02:11:42 +000036 "Unpack machine instruction bundles", false, false)
37
Evan Chengddfd1372011-12-14 02:11:42 +000038bool UnpackMachineBundles::runOnMachineFunction(MachineFunction &MF) {
39 bool Changed = false;
40 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
41 MachineBasicBlock *MBB = &*I;
42
43 for (MachineBasicBlock::instr_iterator MII = MBB->instr_begin(),
44 MIE = MBB->instr_end(); MII != MIE; ) {
45 MachineInstr *MI = &*MII;
46
47 // Remove BUNDLE instruction and the InsideBundle flags from bundled
48 // instructions.
49 if (MI->isBundle()) {
50 while (++MII != MIE && MII->isInsideBundle()) {
51 MII->setIsInsideBundle(false);
52 for (unsigned i = 0, e = MII->getNumOperands(); i != e; ++i) {
53 MachineOperand &MO = MII->getOperand(i);
54 if (MO.isReg() && MO.isInternalRead())
55 MO.setIsInternalRead(false);
56 }
57 }
58 MI->eraseFromParent();
59
60 Changed = true;
61 continue;
62 }
63
64 ++MII;
65 }
66 }
67
68 return Changed;
69}
70
Evan Chengef2887d2012-01-19 07:47:03 +000071
72namespace {
73 class FinalizeMachineBundles : public MachineFunctionPass {
74 public:
75 static char ID; // Pass identification
76 FinalizeMachineBundles() : MachineFunctionPass(ID) {
77 initializeFinalizeMachineBundlesPass(*PassRegistry::getPassRegistry());
78 }
79
80 virtual bool runOnMachineFunction(MachineFunction &MF);
81 };
82} // end anonymous namespace
83
84char FinalizeMachineBundles::ID = 0;
Andrew Trick1dd8c852012-02-08 21:23:13 +000085char &llvm::FinalizeMachineBundlesID = FinalizeMachineBundles::ID;
Evan Chengef2887d2012-01-19 07:47:03 +000086INITIALIZE_PASS(FinalizeMachineBundles, "finalize-mi-bundles",
87 "Finalize machine instruction bundles", false, false)
88
Evan Chengef2887d2012-01-19 07:47:03 +000089bool FinalizeMachineBundles::runOnMachineFunction(MachineFunction &MF) {
90 return llvm::finalizeBundles(MF);
91}
92
93
Evan Cheng9b159712012-01-19 00:06:10 +000094/// finalizeBundle - Finalize a machine instruction bundle which includes
Evan Chengbca15f92012-01-19 00:46:06 +000095/// a sequence of instructions starting from FirstMI to LastMI (exclusive).
Evan Chengddfd1372011-12-14 02:11:42 +000096/// This routine adds a BUNDLE instruction to represent the bundle, it adds
97/// IsInternalRead markers to MachineOperands which are defined inside the
98/// bundle, and it copies externally visible defs and uses to the BUNDLE
99/// instruction.
Evan Cheng9b159712012-01-19 00:06:10 +0000100void llvm::finalizeBundle(MachineBasicBlock &MBB,
Evan Chengddfd1372011-12-14 02:11:42 +0000101 MachineBasicBlock::instr_iterator FirstMI,
102 MachineBasicBlock::instr_iterator LastMI) {
Evan Chengbca15f92012-01-19 00:46:06 +0000103 assert(FirstMI != LastMI && "Empty bundle?");
104
Evan Chengddfd1372011-12-14 02:11:42 +0000105 const TargetMachine &TM = MBB.getParent()->getTarget();
106 const TargetInstrInfo *TII = TM.getInstrInfo();
107 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
108
109 MachineInstrBuilder MIB = BuildMI(MBB, FirstMI, FirstMI->getDebugLoc(),
110 TII->get(TargetOpcode::BUNDLE));
111
Michael Ilseman8dcc9942012-09-17 18:31:15 +0000112 SmallVector<unsigned, 32> LocalDefs;
113 SmallSet<unsigned, 32> LocalDefSet;
Evan Chengddfd1372011-12-14 02:11:42 +0000114 SmallSet<unsigned, 8> DeadDefSet;
Michael Ilseman8dcc9942012-09-17 18:31:15 +0000115 SmallSet<unsigned, 16> KilledDefSet;
Evan Chengddfd1372011-12-14 02:11:42 +0000116 SmallVector<unsigned, 8> ExternUses;
117 SmallSet<unsigned, 8> ExternUseSet;
118 SmallSet<unsigned, 8> KilledUseSet;
119 SmallSet<unsigned, 8> UndefUseSet;
120 SmallVector<MachineOperand*, 4> Defs;
Evan Chengbca15f92012-01-19 00:46:06 +0000121 for (; FirstMI != LastMI; ++FirstMI) {
Evan Chengddfd1372011-12-14 02:11:42 +0000122 for (unsigned i = 0, e = FirstMI->getNumOperands(); i != e; ++i) {
123 MachineOperand &MO = FirstMI->getOperand(i);
124 if (!MO.isReg())
125 continue;
126 if (MO.isDef()) {
127 Defs.push_back(&MO);
128 continue;
129 }
130
131 unsigned Reg = MO.getReg();
132 if (!Reg)
133 continue;
134 assert(TargetRegisterInfo::isPhysicalRegister(Reg));
135 if (LocalDefSet.count(Reg)) {
136 MO.setIsInternalRead();
137 if (MO.isKill())
138 // Internal def is now killed.
139 KilledDefSet.insert(Reg);
140 } else {
141 if (ExternUseSet.insert(Reg)) {
142 ExternUses.push_back(Reg);
143 if (MO.isUndef())
144 UndefUseSet.insert(Reg);
145 }
146 if (MO.isKill())
147 // External def is now killed.
148 KilledUseSet.insert(Reg);
149 }
150 }
151
152 for (unsigned i = 0, e = Defs.size(); i != e; ++i) {
153 MachineOperand &MO = *Defs[i];
154 unsigned Reg = MO.getReg();
155 if (!Reg)
156 continue;
157
158 if (LocalDefSet.insert(Reg)) {
159 LocalDefs.push_back(Reg);
160 if (MO.isDead()) {
161 DeadDefSet.insert(Reg);
162 }
163 } else {
164 // Re-defined inside the bundle, it's no longer killed.
165 KilledDefSet.erase(Reg);
166 if (!MO.isDead())
167 // Previously defined but dead.
168 DeadDefSet.erase(Reg);
169 }
170
171 if (!MO.isDead()) {
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000172 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
173 unsigned SubReg = *SubRegs;
Evan Chengddfd1372011-12-14 02:11:42 +0000174 if (LocalDefSet.insert(SubReg))
175 LocalDefs.push_back(SubReg);
176 }
177 }
178 }
179
180 FirstMI->setIsInsideBundle();
181 Defs.clear();
Evan Chengbca15f92012-01-19 00:46:06 +0000182 }
Evan Chengddfd1372011-12-14 02:11:42 +0000183
Michael Ilseman8dcc9942012-09-17 18:31:15 +0000184 SmallSet<unsigned, 32> Added;
Evan Chengddfd1372011-12-14 02:11:42 +0000185 for (unsigned i = 0, e = LocalDefs.size(); i != e; ++i) {
186 unsigned Reg = LocalDefs[i];
187 if (Added.insert(Reg)) {
188 // If it's not live beyond end of the bundle, mark it dead.
189 bool isDead = DeadDefSet.count(Reg) || KilledDefSet.count(Reg);
190 MIB.addReg(Reg, getDefRegState(true) | getDeadRegState(isDead) |
191 getImplRegState(true));
192 }
193 }
194
195 for (unsigned i = 0, e = ExternUses.size(); i != e; ++i) {
196 unsigned Reg = ExternUses[i];
197 bool isKill = KilledUseSet.count(Reg);
198 bool isUndef = UndefUseSet.count(Reg);
199 MIB.addReg(Reg, getKillRegState(isKill) | getUndefRegState(isUndef) |
200 getImplRegState(true));
201 }
202}
Evan Chengbca15f92012-01-19 00:46:06 +0000203
204/// finalizeBundle - Same functionality as the previous finalizeBundle except
205/// the last instruction in the bundle is not provided as an input. This is
206/// used in cases where bundles are pre-determined by marking instructions
Evan Chenga2e435c2012-01-19 06:13:10 +0000207/// with 'InsideBundle' marker. It returns the MBB instruction iterator that
208/// points to the end of the bundle.
209MachineBasicBlock::instr_iterator
210llvm::finalizeBundle(MachineBasicBlock &MBB,
211 MachineBasicBlock::instr_iterator FirstMI) {
Evan Chengbca15f92012-01-19 00:46:06 +0000212 MachineBasicBlock::instr_iterator E = MBB.instr_end();
213 MachineBasicBlock::instr_iterator LastMI = llvm::next(FirstMI);
214 while (LastMI != E && LastMI->isInsideBundle())
215 ++LastMI;
216 finalizeBundle(MBB, FirstMI, LastMI);
Evan Chenga2e435c2012-01-19 06:13:10 +0000217 return LastMI;
Evan Chengbca15f92012-01-19 00:46:06 +0000218}
Evan Chengef2887d2012-01-19 07:47:03 +0000219
220/// finalizeBundles - Finalize instruction bundles in the specified
221/// MachineFunction. Return true if any bundles are finalized.
222bool llvm::finalizeBundles(MachineFunction &MF) {
223 bool Changed = false;
224 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
225 MachineBasicBlock &MBB = *I;
226
227 MachineBasicBlock::instr_iterator MII = MBB.instr_begin();
228 assert(!MII->isInsideBundle() &&
229 "First instr cannot be inside bundle before finalization!");
230
231 MachineBasicBlock::instr_iterator MIE = MBB.instr_end();
Evan Cheng8250d732012-03-06 02:00:52 +0000232 if (MII == MIE)
233 continue;
Evan Chengef2887d2012-01-19 07:47:03 +0000234 for (++MII; MII != MIE; ) {
235 if (!MII->isInsideBundle())
236 ++MII;
237 else {
238 MII = finalizeBundle(MBB, llvm::prior(MII));
239 Changed = true;
240 }
241 }
242 }
243
244 return Changed;
245}
Jakob Stoklund Olesena36fe732012-02-29 01:40:37 +0000246
247//===----------------------------------------------------------------------===//
248// MachineOperand iterator
249//===----------------------------------------------------------------------===//
250
James Molloyb17cf292012-09-12 10:03:31 +0000251MachineOperandIteratorBase::VirtRegInfo
Jakob Stoklund Olesena36fe732012-02-29 01:40:37 +0000252MachineOperandIteratorBase::analyzeVirtReg(unsigned Reg,
253 SmallVectorImpl<std::pair<MachineInstr*, unsigned> > *Ops) {
James Molloyb17cf292012-09-12 10:03:31 +0000254 VirtRegInfo RI = { false, false, false };
Jakob Stoklund Olesena36fe732012-02-29 01:40:37 +0000255 for(; isValid(); ++*this) {
256 MachineOperand &MO = deref();
257 if (!MO.isReg() || MO.getReg() != Reg)
258 continue;
259
260 // Remember each (MI, OpNo) that refers to Reg.
261 if (Ops)
262 Ops->push_back(std::make_pair(MO.getParent(), getOperandNo()));
263
264 // Both defs and uses can read virtual registers.
265 if (MO.readsReg()) {
266 RI.Reads = true;
267 if (MO.isDef())
268 RI.Tied = true;
269 }
270
271 // Only defs can write.
272 if (MO.isDef())
273 RI.Writes = true;
274 else if (!RI.Tied && MO.getParent()->isRegTiedToDefOperand(getOperandNo()))
275 RI.Tied = true;
276 }
277 return RI;
278}
James Molloyb17cf292012-09-12 10:03:31 +0000279
280MachineOperandIteratorBase::PhysRegInfo
281MachineOperandIteratorBase::analyzePhysReg(unsigned Reg,
282 const TargetRegisterInfo *TRI) {
283 bool AllDefsDead = true;
Tim Northover310f2482012-11-20 09:56:11 +0000284 PhysRegInfo PRI = {false, false, false, false, false, false};
James Molloyb17cf292012-09-12 10:03:31 +0000285
286 assert(TargetRegisterInfo::isPhysicalRegister(Reg) &&
287 "analyzePhysReg not given a physical register!");
288 for (; isValid(); ++*this) {
289 MachineOperand &MO = deref();
290
291 if (MO.isRegMask() && MO.clobbersPhysReg(Reg))
292 PRI.Clobbers = true; // Regmask clobbers Reg.
293
294 if (!MO.isReg())
295 continue;
296
297 unsigned MOReg = MO.getReg();
298 if (!MOReg || !TargetRegisterInfo::isPhysicalRegister(MOReg))
299 continue;
300
301 bool IsRegOrSuperReg = MOReg == Reg || TRI->isSubRegister(MOReg, Reg);
302 bool IsRegOrOverlapping = MOReg == Reg || TRI->regsOverlap(MOReg, Reg);
303
304 if (IsRegOrSuperReg && MO.readsReg()) {
305 // Reg or a super-reg is read, and perhaps killed also.
306 PRI.Reads = true;
307 PRI.Kills = MO.isKill();
Tim Northover310f2482012-11-20 09:56:11 +0000308 }
309
310 if (IsRegOrOverlapping && MO.readsReg()) {
James Molloyb17cf292012-09-12 10:03:31 +0000311 PRI.ReadsOverlap = true;// Reg or an overlapping register is read.
312 }
Michael Ilseman2b943022012-09-17 18:25:23 +0000313
James Molloyb17cf292012-09-12 10:03:31 +0000314 if (!MO.isDef())
315 continue;
316
317 if (IsRegOrSuperReg) {
318 PRI.Defines = true; // Reg or a super-register is defined.
319 if (!MO.isDead())
320 AllDefsDead = false;
321 }
322 if (IsRegOrOverlapping)
323 PRI.Clobbers = true; // Reg or an overlapping reg is defined.
324 }
325
326 if (AllDefsDead && PRI.Defines)
327 PRI.DefinesDead = true; // Reg or super-register was defined and was dead.
328
329 return PRI;
330}