blob: ae378cc8c4647668aa1260222b0574e1b7ab6ef7 [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"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000011#include "llvm/ADT/SmallSet.h"
12#include "llvm/ADT/SmallVector.h"
13#include "llvm/CodeGen/MachineFunctionPass.h"
Evan Chengddfd1372011-12-14 02:11:42 +000014#include "llvm/CodeGen/MachineInstrBuilder.h"
15#include "llvm/CodeGen/Passes.h"
David Blaikie48319232017-11-08 01:01:31 +000016#include "llvm/CodeGen/TargetInstrInfo.h"
David Blaikiee3a9b4c2017-11-17 01:07:10 +000017#include "llvm/CodeGen/TargetRegisterInfo.h"
18#include "llvm/CodeGen/TargetSubtargetInfo.h"
Evan Chengddfd1372011-12-14 02:11:42 +000019#include "llvm/Target/TargetMachine.h"
Benjamin Kramer14aae012016-05-27 14:27:24 +000020#include <utility>
Evan Chengddfd1372011-12-14 02:11:42 +000021using namespace llvm;
22
23namespace {
24 class UnpackMachineBundles : public MachineFunctionPass {
25 public:
26 static char ID; // Pass identification
Matthias Brauncc928282016-10-24 23:23:02 +000027 UnpackMachineBundles(
28 std::function<bool(const MachineFunction &)> Ftor = nullptr)
Benjamin Kramer14aae012016-05-27 14:27:24 +000029 : MachineFunctionPass(ID), PredicateFtor(std::move(Ftor)) {
Evan Chengddfd1372011-12-14 02:11:42 +000030 initializeUnpackMachineBundlesPass(*PassRegistry::getPassRegistry());
31 }
32
Craig Topper9f998de2014-03-07 09:26:03 +000033 bool runOnMachineFunction(MachineFunction &MF) override;
Akira Hatanakafa6bc2e2015-06-08 18:50:43 +000034
35 private:
Matthias Brauncc928282016-10-24 23:23:02 +000036 std::function<bool(const MachineFunction &)> PredicateFtor;
Evan Chengddfd1372011-12-14 02:11:42 +000037 };
38} // end anonymous namespace
39
40char UnpackMachineBundles::ID = 0;
Andrew Trick1dd8c852012-02-08 21:23:13 +000041char &llvm::UnpackMachineBundlesID = UnpackMachineBundles::ID;
Evan Chengef2887d2012-01-19 07:47:03 +000042INITIALIZE_PASS(UnpackMachineBundles, "unpack-mi-bundles",
Evan Chengddfd1372011-12-14 02:11:42 +000043 "Unpack machine instruction bundles", false, false)
44
Evan Chengddfd1372011-12-14 02:11:42 +000045bool UnpackMachineBundles::runOnMachineFunction(MachineFunction &MF) {
Matthias Brauncc928282016-10-24 23:23:02 +000046 if (PredicateFtor && !PredicateFtor(MF))
Akira Hatanakafa6bc2e2015-06-08 18:50:43 +000047 return false;
48
Evan Chengddfd1372011-12-14 02:11:42 +000049 bool Changed = false;
50 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
51 MachineBasicBlock *MBB = &*I;
52
53 for (MachineBasicBlock::instr_iterator MII = MBB->instr_begin(),
54 MIE = MBB->instr_end(); MII != MIE; ) {
55 MachineInstr *MI = &*MII;
56
57 // Remove BUNDLE instruction and the InsideBundle flags from bundled
58 // instructions.
59 if (MI->isBundle()) {
Jakob Stoklund Olesencaf946e2012-12-13 23:23:46 +000060 while (++MII != MIE && MII->isBundledWithPred()) {
61 MII->unbundleFromPred();
Evan Chengddfd1372011-12-14 02:11:42 +000062 for (unsigned i = 0, e = MII->getNumOperands(); i != e; ++i) {
63 MachineOperand &MO = MII->getOperand(i);
64 if (MO.isReg() && MO.isInternalRead())
65 MO.setIsInternalRead(false);
66 }
67 }
68 MI->eraseFromParent();
69
70 Changed = true;
71 continue;
72 }
73
74 ++MII;
75 }
76 }
77
78 return Changed;
79}
80
Akira Hatanakafa6bc2e2015-06-08 18:50:43 +000081FunctionPass *
Matthias Brauncc928282016-10-24 23:23:02 +000082llvm::createUnpackMachineBundles(
83 std::function<bool(const MachineFunction &)> Ftor) {
Benjamin Kramer022a8992016-06-12 16:13:55 +000084 return new UnpackMachineBundles(std::move(Ftor));
Akira Hatanakafa6bc2e2015-06-08 18:50:43 +000085}
Evan Chengef2887d2012-01-19 07:47:03 +000086
87namespace {
88 class FinalizeMachineBundles : public MachineFunctionPass {
89 public:
90 static char ID; // Pass identification
91 FinalizeMachineBundles() : MachineFunctionPass(ID) {
92 initializeFinalizeMachineBundlesPass(*PassRegistry::getPassRegistry());
93 }
94
Craig Topper9f998de2014-03-07 09:26:03 +000095 bool runOnMachineFunction(MachineFunction &MF) override;
Evan Chengef2887d2012-01-19 07:47:03 +000096 };
97} // end anonymous namespace
98
99char FinalizeMachineBundles::ID = 0;
Andrew Trick1dd8c852012-02-08 21:23:13 +0000100char &llvm::FinalizeMachineBundlesID = FinalizeMachineBundles::ID;
Evan Chengef2887d2012-01-19 07:47:03 +0000101INITIALIZE_PASS(FinalizeMachineBundles, "finalize-mi-bundles",
102 "Finalize machine instruction bundles", false, false)
103
Evan Chengef2887d2012-01-19 07:47:03 +0000104bool FinalizeMachineBundles::runOnMachineFunction(MachineFunction &MF) {
105 return llvm::finalizeBundles(MF);
106}
107
Bjorn Pettersson6a7eb842018-08-21 10:59:50 +0000108/// Return the first found DebugLoc that has a DILocation, given a range of
109/// instructions. The search range is from FirstMI to LastMI (exclusive). If no
110/// DILocation is found, then an empty location is returned.
111static DebugLoc getDebugLoc(MachineBasicBlock::instr_iterator FirstMI,
112 MachineBasicBlock::instr_iterator LastMI) {
113 for (auto MII = FirstMI; MII != LastMI; ++MII)
114 if (MII->getDebugLoc().get())
115 return MII->getDebugLoc();
116 return DebugLoc();
117}
Evan Chengef2887d2012-01-19 07:47:03 +0000118
Evan Cheng9b159712012-01-19 00:06:10 +0000119/// finalizeBundle - Finalize a machine instruction bundle which includes
Evan Chengbca15f92012-01-19 00:46:06 +0000120/// a sequence of instructions starting from FirstMI to LastMI (exclusive).
Evan Chengddfd1372011-12-14 02:11:42 +0000121/// This routine adds a BUNDLE instruction to represent the bundle, it adds
122/// IsInternalRead markers to MachineOperands which are defined inside the
123/// bundle, and it copies externally visible defs and uses to the BUNDLE
124/// instruction.
Evan Cheng9b159712012-01-19 00:06:10 +0000125void llvm::finalizeBundle(MachineBasicBlock &MBB,
Evan Chengddfd1372011-12-14 02:11:42 +0000126 MachineBasicBlock::instr_iterator FirstMI,
127 MachineBasicBlock::instr_iterator LastMI) {
Evan Chengbca15f92012-01-19 00:46:06 +0000128 assert(FirstMI != LastMI && "Empty bundle?");
Jakob Stoklund Olesencaf946e2012-12-13 23:23:46 +0000129 MIBundleBuilder Bundle(MBB, FirstMI, LastMI);
Evan Chengbca15f92012-01-19 00:46:06 +0000130
Eric Christopherc026db72014-10-14 06:26:55 +0000131 MachineFunction &MF = *MBB.getParent();
132 const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
133 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
Evan Chengddfd1372011-12-14 02:11:42 +0000134
Eric Christopherc026db72014-10-14 06:26:55 +0000135 MachineInstrBuilder MIB =
Bjorn Pettersson6a7eb842018-08-21 10:59:50 +0000136 BuildMI(MF, getDebugLoc(FirstMI, LastMI), TII->get(TargetOpcode::BUNDLE));
Jakob Stoklund Olesencaf946e2012-12-13 23:23:46 +0000137 Bundle.prepend(MIB);
Evan Chengddfd1372011-12-14 02:11:42 +0000138
Michael Ilseman8dcc9942012-09-17 18:31:15 +0000139 SmallVector<unsigned, 32> LocalDefs;
140 SmallSet<unsigned, 32> LocalDefSet;
Evan Chengddfd1372011-12-14 02:11:42 +0000141 SmallSet<unsigned, 8> DeadDefSet;
Michael Ilseman8dcc9942012-09-17 18:31:15 +0000142 SmallSet<unsigned, 16> KilledDefSet;
Evan Chengddfd1372011-12-14 02:11:42 +0000143 SmallVector<unsigned, 8> ExternUses;
144 SmallSet<unsigned, 8> ExternUseSet;
145 SmallSet<unsigned, 8> KilledUseSet;
146 SmallSet<unsigned, 8> UndefUseSet;
147 SmallVector<MachineOperand*, 4> Defs;
Bjorn Pettersson9a9829f2018-08-25 11:26:17 +0000148 for (auto MII = FirstMI; MII != LastMI; ++MII) {
149 for (unsigned i = 0, e = MII->getNumOperands(); i != e; ++i) {
150 MachineOperand &MO = MII->getOperand(i);
Evan Chengddfd1372011-12-14 02:11:42 +0000151 if (!MO.isReg())
152 continue;
153 if (MO.isDef()) {
154 Defs.push_back(&MO);
155 continue;
156 }
157
158 unsigned Reg = MO.getReg();
159 if (!Reg)
160 continue;
161 assert(TargetRegisterInfo::isPhysicalRegister(Reg));
162 if (LocalDefSet.count(Reg)) {
163 MO.setIsInternalRead();
164 if (MO.isKill())
165 // Internal def is now killed.
166 KilledDefSet.insert(Reg);
167 } else {
David Blaikie5401ba72014-11-19 07:49:26 +0000168 if (ExternUseSet.insert(Reg).second) {
Evan Chengddfd1372011-12-14 02:11:42 +0000169 ExternUses.push_back(Reg);
170 if (MO.isUndef())
171 UndefUseSet.insert(Reg);
172 }
173 if (MO.isKill())
174 // External def is now killed.
175 KilledUseSet.insert(Reg);
176 }
177 }
178
179 for (unsigned i = 0, e = Defs.size(); i != e; ++i) {
180 MachineOperand &MO = *Defs[i];
181 unsigned Reg = MO.getReg();
182 if (!Reg)
183 continue;
184
David Blaikie5401ba72014-11-19 07:49:26 +0000185 if (LocalDefSet.insert(Reg).second) {
Evan Chengddfd1372011-12-14 02:11:42 +0000186 LocalDefs.push_back(Reg);
187 if (MO.isDead()) {
188 DeadDefSet.insert(Reg);
189 }
190 } else {
191 // Re-defined inside the bundle, it's no longer killed.
192 KilledDefSet.erase(Reg);
193 if (!MO.isDead())
194 // Previously defined but dead.
195 DeadDefSet.erase(Reg);
196 }
197
198 if (!MO.isDead()) {
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000199 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
200 unsigned SubReg = *SubRegs;
David Blaikie5401ba72014-11-19 07:49:26 +0000201 if (LocalDefSet.insert(SubReg).second)
Evan Chengddfd1372011-12-14 02:11:42 +0000202 LocalDefs.push_back(SubReg);
203 }
204 }
205 }
206
Evan Chengddfd1372011-12-14 02:11:42 +0000207 Defs.clear();
Evan Chengbca15f92012-01-19 00:46:06 +0000208 }
Evan Chengddfd1372011-12-14 02:11:42 +0000209
Michael Ilseman8dcc9942012-09-17 18:31:15 +0000210 SmallSet<unsigned, 32> Added;
Evan Chengddfd1372011-12-14 02:11:42 +0000211 for (unsigned i = 0, e = LocalDefs.size(); i != e; ++i) {
212 unsigned Reg = LocalDefs[i];
David Blaikie5401ba72014-11-19 07:49:26 +0000213 if (Added.insert(Reg).second) {
Evan Chengddfd1372011-12-14 02:11:42 +0000214 // If it's not live beyond end of the bundle, mark it dead.
215 bool isDead = DeadDefSet.count(Reg) || KilledDefSet.count(Reg);
216 MIB.addReg(Reg, getDefRegState(true) | getDeadRegState(isDead) |
217 getImplRegState(true));
218 }
219 }
220
221 for (unsigned i = 0, e = ExternUses.size(); i != e; ++i) {
222 unsigned Reg = ExternUses[i];
223 bool isKill = KilledUseSet.count(Reg);
224 bool isUndef = UndefUseSet.count(Reg);
225 MIB.addReg(Reg, getKillRegState(isKill) | getUndefRegState(isUndef) |
226 getImplRegState(true));
227 }
Bjorn Pettersson9a9829f2018-08-25 11:26:17 +0000228
229 // Set FrameSetup/FrameDestroy for the bundle. If any of the instructions got
230 // the property, then also set it on the bundle.
231 for (auto MII = FirstMI; MII != LastMI; ++MII) {
232 if (MII->getFlag(MachineInstr::FrameSetup))
233 MIB.setMIFlag(MachineInstr::FrameSetup);
234 if (MII->getFlag(MachineInstr::FrameDestroy))
235 MIB.setMIFlag(MachineInstr::FrameDestroy);
236 }
Evan Chengddfd1372011-12-14 02:11:42 +0000237}
Evan Chengbca15f92012-01-19 00:46:06 +0000238
239/// finalizeBundle - Same functionality as the previous finalizeBundle except
240/// the last instruction in the bundle is not provided as an input. This is
241/// used in cases where bundles are pre-determined by marking instructions
Evan Chenga2e435c2012-01-19 06:13:10 +0000242/// with 'InsideBundle' marker. It returns the MBB instruction iterator that
243/// points to the end of the bundle.
244MachineBasicBlock::instr_iterator
245llvm::finalizeBundle(MachineBasicBlock &MBB,
246 MachineBasicBlock::instr_iterator FirstMI) {
Evan Chengbca15f92012-01-19 00:46:06 +0000247 MachineBasicBlock::instr_iterator E = MBB.instr_end();
Benjamin Kramerd628f192014-03-02 12:27:27 +0000248 MachineBasicBlock::instr_iterator LastMI = std::next(FirstMI);
Evan Chengbca15f92012-01-19 00:46:06 +0000249 while (LastMI != E && LastMI->isInsideBundle())
250 ++LastMI;
251 finalizeBundle(MBB, FirstMI, LastMI);
Evan Chenga2e435c2012-01-19 06:13:10 +0000252 return LastMI;
Evan Chengbca15f92012-01-19 00:46:06 +0000253}
Evan Chengef2887d2012-01-19 07:47:03 +0000254
255/// finalizeBundles - Finalize instruction bundles in the specified
256/// MachineFunction. Return true if any bundles are finalized.
257bool llvm::finalizeBundles(MachineFunction &MF) {
258 bool Changed = false;
259 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
260 MachineBasicBlock &MBB = *I;
Evan Chengef2887d2012-01-19 07:47:03 +0000261 MachineBasicBlock::instr_iterator MII = MBB.instr_begin();
Evan Chengef2887d2012-01-19 07:47:03 +0000262 MachineBasicBlock::instr_iterator MIE = MBB.instr_end();
Evan Cheng8250d732012-03-06 02:00:52 +0000263 if (MII == MIE)
264 continue;
Jakob Stoklund Olesen73a853f2013-01-04 22:17:31 +0000265 assert(!MII->isInsideBundle() &&
266 "First instr cannot be inside bundle before finalization!");
267
Evan Chengef2887d2012-01-19 07:47:03 +0000268 for (++MII; MII != MIE; ) {
269 if (!MII->isInsideBundle())
270 ++MII;
271 else {
Benjamin Kramerd628f192014-03-02 12:27:27 +0000272 MII = finalizeBundle(MBB, std::prev(MII));
Evan Chengef2887d2012-01-19 07:47:03 +0000273 Changed = true;
274 }
275 }
276 }
277
278 return Changed;
279}
Jakob Stoklund Olesena36fe732012-02-29 01:40:37 +0000280
281//===----------------------------------------------------------------------===//
282// MachineOperand iterator
283//===----------------------------------------------------------------------===//
284
James Molloyb17cf292012-09-12 10:03:31 +0000285MachineOperandIteratorBase::VirtRegInfo
Jakob Stoklund Olesena36fe732012-02-29 01:40:37 +0000286MachineOperandIteratorBase::analyzeVirtReg(unsigned Reg,
287 SmallVectorImpl<std::pair<MachineInstr*, unsigned> > *Ops) {
James Molloyb17cf292012-09-12 10:03:31 +0000288 VirtRegInfo RI = { false, false, false };
Jakob Stoklund Olesena36fe732012-02-29 01:40:37 +0000289 for(; isValid(); ++*this) {
290 MachineOperand &MO = deref();
291 if (!MO.isReg() || MO.getReg() != Reg)
292 continue;
293
294 // Remember each (MI, OpNo) that refers to Reg.
295 if (Ops)
296 Ops->push_back(std::make_pair(MO.getParent(), getOperandNo()));
297
298 // Both defs and uses can read virtual registers.
299 if (MO.readsReg()) {
300 RI.Reads = true;
301 if (MO.isDef())
302 RI.Tied = true;
303 }
304
305 // Only defs can write.
306 if (MO.isDef())
307 RI.Writes = true;
308 else if (!RI.Tied && MO.getParent()->isRegTiedToDefOperand(getOperandNo()))
309 RI.Tied = true;
310 }
311 return RI;
312}
James Molloyb17cf292012-09-12 10:03:31 +0000313
314MachineOperandIteratorBase::PhysRegInfo
315MachineOperandIteratorBase::analyzePhysReg(unsigned Reg,
316 const TargetRegisterInfo *TRI) {
317 bool AllDefsDead = true;
Quentin Colombetb62e2472016-04-26 23:14:24 +0000318 PhysRegInfo PRI = {false, false, false, false, false, false, false, false};
James Molloyb17cf292012-09-12 10:03:31 +0000319
320 assert(TargetRegisterInfo::isPhysicalRegister(Reg) &&
321 "analyzePhysReg not given a physical register!");
322 for (; isValid(); ++*this) {
323 MachineOperand &MO = deref();
324
Matthias Braunf43272c2015-12-11 19:42:09 +0000325 if (MO.isRegMask() && MO.clobbersPhysReg(Reg)) {
326 PRI.Clobbered = true;
327 continue;
328 }
James Molloyb17cf292012-09-12 10:03:31 +0000329
330 if (!MO.isReg())
331 continue;
332
333 unsigned MOReg = MO.getReg();
334 if (!MOReg || !TargetRegisterInfo::isPhysicalRegister(MOReg))
335 continue;
336
Matthias Braunf43272c2015-12-11 19:42:09 +0000337 if (!TRI->regsOverlap(MOReg, Reg))
James Molloyb17cf292012-09-12 10:03:31 +0000338 continue;
339
Matthias Braun96b46d12016-01-05 00:45:35 +0000340 bool Covered = TRI->isSuperRegisterEq(Reg, MOReg);
Matthias Braunf43272c2015-12-11 19:42:09 +0000341 if (MO.readsReg()) {
342 PRI.Read = true;
343 if (Covered) {
344 PRI.FullyRead = true;
345 if (MO.isKill())
346 PRI.Killed = true;
347 }
348 } else if (MO.isDef()) {
349 PRI.Defined = true;
350 if (Covered)
351 PRI.FullyDefined = true;
James Molloyb17cf292012-09-12 10:03:31 +0000352 if (!MO.isDead())
353 AllDefsDead = false;
354 }
James Molloyb17cf292012-09-12 10:03:31 +0000355 }
356
Quentin Colombetb62e2472016-04-26 23:14:24 +0000357 if (AllDefsDead) {
358 if (PRI.FullyDefined || PRI.Clobbered)
359 PRI.DeadDef = true;
Quentin Colombet7ec26d42016-04-27 00:16:29 +0000360 else if (PRI.Defined)
Quentin Colombetb62e2472016-04-26 23:14:24 +0000361 PRI.PartialDeadDef = true;
362 }
James Molloyb17cf292012-09-12 10:03:31 +0000363
364 return PRI;
365}