LLVM  8.0.1
MachineInstrBundle.cpp
Go to the documentation of this file.
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 
11 #include "llvm/ADT/SmallSet.h"
12 #include "llvm/ADT/SmallVector.h"
15 #include "llvm/CodeGen/Passes.h"
20 #include <utility>
21 using namespace llvm;
22 
23 namespace {
24  class UnpackMachineBundles : public MachineFunctionPass {
25  public:
26  static char ID; // Pass identification
27  UnpackMachineBundles(
28  std::function<bool(const MachineFunction &)> Ftor = nullptr)
29  : MachineFunctionPass(ID), PredicateFtor(std::move(Ftor)) {
31  }
32 
33  bool runOnMachineFunction(MachineFunction &MF) override;
34 
35  private:
36  std::function<bool(const MachineFunction &)> PredicateFtor;
37  };
38 } // end anonymous namespace
39 
42 INITIALIZE_PASS(UnpackMachineBundles, "unpack-mi-bundles",
43  "Unpack machine instruction bundles", false, false)
44 
45 bool UnpackMachineBundles::runOnMachineFunction(MachineFunction &MF) {
46  if (PredicateFtor && !PredicateFtor(MF))
47  return false;
48 
49  bool Changed = false;
50  for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
51  MachineBasicBlock *MBB = &*I;
52 
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()) {
60  while (++MII != MIE && MII->isBundledWithPred()) {
61  MII->unbundleFromPred();
62  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 
83  std::function<bool(const MachineFunction &)> Ftor) {
84  return new UnpackMachineBundles(std::move(Ftor));
85 }
86 
87 namespace {
88  class FinalizeMachineBundles : public MachineFunctionPass {
89  public:
90  static char ID; // Pass identification
91  FinalizeMachineBundles() : MachineFunctionPass(ID) {
93  }
94 
95  bool runOnMachineFunction(MachineFunction &MF) override;
96  };
97 } // end anonymous namespace
98 
101 INITIALIZE_PASS(FinalizeMachineBundles, "finalize-mi-bundles",
102  "Finalize machine instruction bundles", false, false)
103 
104 bool FinalizeMachineBundles::runOnMachineFunction(MachineFunction &MF) {
105  return llvm::finalizeBundles(MF);
106 }
107 
108 /// 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.
113  for (auto MII = FirstMI; MII != LastMI; ++MII)
114  if (MII->getDebugLoc().get())
115  return MII->getDebugLoc();
116  return DebugLoc();
117 }
118 
119 /// finalizeBundle - Finalize a machine instruction bundle which includes
120 /// a sequence of instructions starting from FirstMI to LastMI (exclusive).
121 /// 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.
128  assert(FirstMI != LastMI && "Empty bundle?");
129  MIBundleBuilder Bundle(MBB, FirstMI, LastMI);
130 
131  MachineFunction &MF = *MBB.getParent();
134 
135  MachineInstrBuilder MIB =
136  BuildMI(MF, getDebugLoc(FirstMI, LastMI), TII->get(TargetOpcode::BUNDLE));
137  Bundle.prepend(MIB);
138 
139  SmallVector<unsigned, 32> LocalDefs;
140  SmallSet<unsigned, 32> LocalDefSet;
141  SmallSet<unsigned, 8> DeadDefSet;
142  SmallSet<unsigned, 16> KilledDefSet;
143  SmallVector<unsigned, 8> ExternUses;
144  SmallSet<unsigned, 8> ExternUseSet;
145  SmallSet<unsigned, 8> KilledUseSet;
146  SmallSet<unsigned, 8> UndefUseSet;
148  for (auto MII = FirstMI; MII != LastMI; ++MII) {
149  for (unsigned i = 0, e = MII->getNumOperands(); i != e; ++i) {
150  MachineOperand &MO = MII->getOperand(i);
151  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;
162  if (LocalDefSet.count(Reg)) {
163  MO.setIsInternalRead();
164  if (MO.isKill())
165  // Internal def is now killed.
166  KilledDefSet.insert(Reg);
167  } else {
168  if (ExternUseSet.insert(Reg).second) {
169  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 
185  if (LocalDefSet.insert(Reg).second) {
186  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()) {
199  for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
200  unsigned SubReg = *SubRegs;
201  if (LocalDefSet.insert(SubReg).second)
202  LocalDefs.push_back(SubReg);
203  }
204  }
205  }
206 
207  Defs.clear();
208  }
209 
211  for (unsigned i = 0, e = LocalDefs.size(); i != e; ++i) {
212  unsigned Reg = LocalDefs[i];
213  if (Added.insert(Reg).second) {
214  // 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  }
228 
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))
234  if (MII->getFlag(MachineInstr::FrameDestroy))
236  }
237 }
238 
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
242 /// with 'InsideBundle' marker. It returns the MBB instruction iterator that
243 /// points to the end of the bundle.
248  MachineBasicBlock::instr_iterator LastMI = std::next(FirstMI);
249  while (LastMI != E && LastMI->isInsideBundle())
250  ++LastMI;
251  finalizeBundle(MBB, FirstMI, LastMI);
252  return LastMI;
253 }
254 
255 /// finalizeBundles - Finalize instruction bundles in the specified
256 /// MachineFunction. Return true if any bundles are finalized.
258  bool Changed = false;
259  for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
260  MachineBasicBlock &MBB = *I;
263  if (MII == MIE)
264  continue;
265  assert(!MII->isInsideBundle() &&
266  "First instr cannot be inside bundle before finalization!");
267 
268  for (++MII; MII != MIE; ) {
269  if (!MII->isInsideBundle())
270  ++MII;
271  else {
272  MII = finalizeBundle(MBB, std::prev(MII));
273  Changed = true;
274  }
275  }
276  }
277 
278  return Changed;
279 }
280 
281 //===----------------------------------------------------------------------===//
282 // MachineOperand iterator
283 //===----------------------------------------------------------------------===//
284 
287  SmallVectorImpl<std::pair<MachineInstr*, unsigned> > *Ops) {
288  VirtRegInfo RI = { false, false, false };
289  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 }
313 
316  const TargetRegisterInfo *TRI) {
317  bool AllDefsDead = true;
318  PhysRegInfo PRI = {false, false, false, false, false, false, false, false};
319 
321  "analyzePhysReg not given a physical register!");
322  for (; isValid(); ++*this) {
323  MachineOperand &MO = deref();
324 
325  if (MO.isRegMask() && MO.clobbersPhysReg(Reg)) {
326  PRI.Clobbered = true;
327  continue;
328  }
329 
330  if (!MO.isReg())
331  continue;
332 
333  unsigned MOReg = MO.getReg();
334  if (!MOReg || !TargetRegisterInfo::isPhysicalRegister(MOReg))
335  continue;
336 
337  if (!TRI->regsOverlap(MOReg, Reg))
338  continue;
339 
340  bool Covered = TRI->isSuperRegisterEq(Reg, MOReg);
341  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;
352  if (!MO.isDead())
353  AllDefsDead = false;
354  }
355  }
356 
357  if (AllDefsDead) {
358  if (PRI.FullyDefined || PRI.Clobbered)
359  PRI.DeadDef = true;
360  else if (PRI.Defined)
361  PRI.PartialDeadDef = true;
362  }
363 
364  return PRI;
365 }
bool isRegMask() const
isRegMask - Tests if this is a MO_RegisterMask operand.
bool isSuperRegisterEq(unsigned RegA, unsigned RegB) const
Returns true if RegB is a super-register of RegA or if RegB == RegA.
MachineInstr * getParent()
getParent - Return the instruction that this operand belongs to.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
instr_iterator instr_begin()
instr_iterator instr_end()
This class represents lattice values for constants.
Definition: AllocatorList.h:24
bool Tied
Tied - Uses and defs must use the same register.
virtual const TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
unsigned getReg() const
getReg - Returns the register number.
unsigned Reg
void initializeFinalizeMachineBundlesPass(PassRegistry &)
unsigned const TargetRegisterInfo * TRI
A debug info location.
Definition: DebugLoc.h:34
void initializeUnpackMachineBundlesPass(PassRegistry &)
bool erase(const T &V)
Definition: SmallSet.h:208
bool isInternalRead() const
VirtRegInfo analyzeVirtReg(unsigned Reg, SmallVectorImpl< std::pair< MachineInstr *, unsigned > > *Ops=nullptr)
analyzeVirtReg - Analyze how the current instruction or bundle uses a virtual register.
const MachineInstrBuilder & setMIFlag(MachineInstr::MIFlag Flag) const
Definition: BitVector.h:938
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:42
void eraseFromParent()
Unlink &#39;this&#39; from the containing basic block and delete it.
unsigned SubReg
char & FinalizeMachineBundlesID
FinalizeMachineBundles - This pass finalize machine instruction bundles (created earlier, e.g.
PhysRegInfo analyzePhysReg(unsigned Reg, const TargetRegisterInfo *TRI)
analyzePhysReg - Analyze how the current instruction or bundle uses a physical register.
INITIALIZE_PASS(UnpackMachineBundles, "unpack-mi-bundles", "Unpack machine instruction bundles", false, false) bool UnpackMachineBundles
bool readsReg() const
readsReg - Returns true if this operand reads the previous value of its register. ...
MIBundleBuilder & prepend(MachineInstr *MI)
Insert MI into MBB by prepending it to the instructions in the bundle.
bool isBundle() const
virtual const TargetInstrInfo * getInstrInfo() const
unsigned getUndefRegState(bool B)
bool PartialDeadDef
Reg is Defined and all defs of reg or an overlapping register are dead.
VirtRegInfo - Information about a virtual register used by a set of operands.
unsigned getKillRegState(bool B)
TargetInstrInfo - Interface to description of machine instruction set.
unsigned getDeadRegState(bool B)
unsigned getDefRegState(bool B)
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
* if(!EatIfPresent(lltok::kw_thread_local)) return false
ParseOptionalThreadLocal := /*empty.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
bool Writes
Writes - One of the operands writes the virtual register.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
bool Read
Reg or one of its aliases is read.
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition: SmallSet.h:135
bool Clobbered
There is a regmask operand indicating Reg is clobbered.
bool Killed
There is a use operand of reg or a super-register with kill flag set.
void setIsInternalRead(bool Val=true)
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:285
bool isRegTiedToDefOperand(unsigned UseOpIdx, unsigned *DefOpIdx=nullptr) const
Return true if the use operand of the specified index is tied to a def operand.
std::pair< NoneType, bool > insert(const T &V)
insert - Insert an element into the set if it isn&#39;t already there.
Definition: SmallSet.h:181
bool Reads
Reads - One of the operands read the virtual register.
char & UnpackMachineBundlesID
UnpackMachineBundles - This pass unpack machine instruction bundles.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
MCSubRegIterator enumerates all sub-registers of Reg.
size_t size() const
Definition: SmallVector.h:53
static bool isUndef(ArrayRef< int > Mask)
Iterator for intrusive lists based on ilist_node.
bool regsOverlap(unsigned regA, unsigned regB) const
Returns true if the two registers are equal or alias each other.
MachineOperand class - Representation of each machine instruction operand.
bool finalizeBundles(MachineFunction &MF)
finalizeBundles - Finalize instruction bundles in the specified MachineFunction.
bool FullyDefined
Reg or a super-register is defined.
FunctionPass * createUnpackMachineBundles(std::function< bool(const MachineFunction &)> Ftor)
bool FullyRead
Reg or a super-register is read. The full register is read.
static bool clobbersPhysReg(const uint32_t *RegMask, unsigned PhysReg)
clobbersPhysReg - Returns true if this RegMask clobbers PhysReg.
bool isValid() const
isValid - returns true if this iterator is not yet at the end.
Representation of each machine instruction.
Definition: MachineInstr.h:64
static bool isPhysicalRegister(unsigned Reg)
Return true if the specified register number is in the physical register namespace.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
static DebugLoc getDebugLoc(MachineBasicBlock::instr_iterator FirstMI, MachineBasicBlock::instr_iterator LastMI)
Return the first found DebugLoc that has a DILocation, given a range of instructions.
const MCInstrDesc & get(unsigned Opcode) const
Return the machine instruction descriptor that corresponds to the specified instruction opcode...
Definition: MCInstrInfo.h:45
#define I(x, y, z)
Definition: MD5.cpp:58
unsigned getImplRegState(bool B)
const MachineInstrBuilder & addReg(unsigned RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
Information about how a physical register Reg is used by a set of operands.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
Instructions::iterator instr_iterator
bool Defined
Reg or one of its aliases is defined.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
print Print MemDeps of function
IRTranslator LLVM IR MI
Binary functor that adapts to any other binary functor after dereferencing operands.
Definition: STLExtras.h:1402
void finalizeBundle(MachineBasicBlock &MBB, MachineBasicBlock::instr_iterator FirstMI, MachineBasicBlock::instr_iterator LastMI)
finalizeBundle - Finalize a machine instruction bundle which includes a sequence of instructions star...
Helper class for constructing bundles of MachineInstrs.
size_type count(const T &V) const
count - Return 1 if the element is in the set, 0 otherwise.
Definition: SmallSet.h:165