LLVM  3.7.0
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 using namespace llvm;
21 
22 namespace {
23  class UnpackMachineBundles : public MachineFunctionPass {
24  public:
25  static char ID; // Pass identification
26  UnpackMachineBundles(std::function<bool(const Function &)> Ftor = nullptr)
27  : MachineFunctionPass(ID), PredicateFtor(Ftor) {
29  }
30 
31  bool runOnMachineFunction(MachineFunction &MF) override;
32 
33  private:
34  std::function<bool(const Function &)> PredicateFtor;
35  };
36 } // end anonymous namespace
37 
40 INITIALIZE_PASS(UnpackMachineBundles, "unpack-mi-bundles",
41  "Unpack machine instruction bundles", false, false)
42 
43 bool UnpackMachineBundles::runOnMachineFunction(MachineFunction &MF) {
44  if (PredicateFtor && !PredicateFtor(*MF.getFunction()))
45  return false;
46 
47  bool Changed = false;
48  for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
49  MachineBasicBlock *MBB = &*I;
50 
52  MIE = MBB->instr_end(); MII != MIE; ) {
53  MachineInstr *MI = &*MII;
54 
55  // Remove BUNDLE instruction and the InsideBundle flags from bundled
56  // instructions.
57  if (MI->isBundle()) {
58  while (++MII != MIE && MII->isBundledWithPred()) {
59  MII->unbundleFromPred();
60  for (unsigned i = 0, e = MII->getNumOperands(); i != e; ++i) {
61  MachineOperand &MO = MII->getOperand(i);
62  if (MO.isReg() && MO.isInternalRead())
63  MO.setIsInternalRead(false);
64  }
65  }
66  MI->eraseFromParent();
67 
68  Changed = true;
69  continue;
70  }
71 
72  ++MII;
73  }
74  }
75 
76  return Changed;
77 }
78 
81  return new UnpackMachineBundles(Ftor);
82 }
83 
84 namespace {
85  class FinalizeMachineBundles : public MachineFunctionPass {
86  public:
87  static char ID; // Pass identification
88  FinalizeMachineBundles() : MachineFunctionPass(ID) {
90  }
91 
92  bool runOnMachineFunction(MachineFunction &MF) override;
93  };
94 } // end anonymous namespace
95 
98 INITIALIZE_PASS(FinalizeMachineBundles, "finalize-mi-bundles",
99  "Finalize machine instruction bundles", false, false)
100 
101 bool FinalizeMachineBundles::runOnMachineFunction(MachineFunction &MF) {
102  return llvm::finalizeBundles(MF);
103 }
104 
105 
106 /// finalizeBundle - Finalize a machine instruction bundle which includes
107 /// a sequence of instructions starting from FirstMI to LastMI (exclusive).
108 /// This routine adds a BUNDLE instruction to represent the bundle, it adds
109 /// IsInternalRead markers to MachineOperands which are defined inside the
110 /// bundle, and it copies externally visible defs and uses to the BUNDLE
111 /// instruction.
115  assert(FirstMI != LastMI && "Empty bundle?");
116  MIBundleBuilder Bundle(MBB, FirstMI, LastMI);
117 
118  MachineFunction &MF = *MBB.getParent();
120  const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
121 
122  MachineInstrBuilder MIB =
123  BuildMI(MF, FirstMI->getDebugLoc(), TII->get(TargetOpcode::BUNDLE));
124  Bundle.prepend(MIB);
125 
126  SmallVector<unsigned, 32> LocalDefs;
127  SmallSet<unsigned, 32> LocalDefSet;
128  SmallSet<unsigned, 8> DeadDefSet;
129  SmallSet<unsigned, 16> KilledDefSet;
130  SmallVector<unsigned, 8> ExternUses;
131  SmallSet<unsigned, 8> ExternUseSet;
132  SmallSet<unsigned, 8> KilledUseSet;
133  SmallSet<unsigned, 8> UndefUseSet;
135  for (; FirstMI != LastMI; ++FirstMI) {
136  for (unsigned i = 0, e = FirstMI->getNumOperands(); i != e; ++i) {
137  MachineOperand &MO = FirstMI->getOperand(i);
138  if (!MO.isReg())
139  continue;
140  if (MO.isDef()) {
141  Defs.push_back(&MO);
142  continue;
143  }
144 
145  unsigned Reg = MO.getReg();
146  if (!Reg)
147  continue;
149  if (LocalDefSet.count(Reg)) {
150  MO.setIsInternalRead();
151  if (MO.isKill())
152  // Internal def is now killed.
153  KilledDefSet.insert(Reg);
154  } else {
155  if (ExternUseSet.insert(Reg).second) {
156  ExternUses.push_back(Reg);
157  if (MO.isUndef())
158  UndefUseSet.insert(Reg);
159  }
160  if (MO.isKill())
161  // External def is now killed.
162  KilledUseSet.insert(Reg);
163  }
164  }
165 
166  for (unsigned i = 0, e = Defs.size(); i != e; ++i) {
167  MachineOperand &MO = *Defs[i];
168  unsigned Reg = MO.getReg();
169  if (!Reg)
170  continue;
171 
172  if (LocalDefSet.insert(Reg).second) {
173  LocalDefs.push_back(Reg);
174  if (MO.isDead()) {
175  DeadDefSet.insert(Reg);
176  }
177  } else {
178  // Re-defined inside the bundle, it's no longer killed.
179  KilledDefSet.erase(Reg);
180  if (!MO.isDead())
181  // Previously defined but dead.
182  DeadDefSet.erase(Reg);
183  }
184 
185  if (!MO.isDead()) {
186  for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
187  unsigned SubReg = *SubRegs;
188  if (LocalDefSet.insert(SubReg).second)
189  LocalDefs.push_back(SubReg);
190  }
191  }
192  }
193 
194  Defs.clear();
195  }
196 
198  for (unsigned i = 0, e = LocalDefs.size(); i != e; ++i) {
199  unsigned Reg = LocalDefs[i];
200  if (Added.insert(Reg).second) {
201  // If it's not live beyond end of the bundle, mark it dead.
202  bool isDead = DeadDefSet.count(Reg) || KilledDefSet.count(Reg);
203  MIB.addReg(Reg, getDefRegState(true) | getDeadRegState(isDead) |
204  getImplRegState(true));
205  }
206  }
207 
208  for (unsigned i = 0, e = ExternUses.size(); i != e; ++i) {
209  unsigned Reg = ExternUses[i];
210  bool isKill = KilledUseSet.count(Reg);
211  bool isUndef = UndefUseSet.count(Reg);
212  MIB.addReg(Reg, getKillRegState(isKill) | getUndefRegState(isUndef) |
213  getImplRegState(true));
214  }
215 }
216 
217 /// finalizeBundle - Same functionality as the previous finalizeBundle except
218 /// the last instruction in the bundle is not provided as an input. This is
219 /// used in cases where bundles are pre-determined by marking instructions
220 /// with 'InsideBundle' marker. It returns the MBB instruction iterator that
221 /// points to the end of the bundle.
226  MachineBasicBlock::instr_iterator LastMI = std::next(FirstMI);
227  while (LastMI != E && LastMI->isInsideBundle())
228  ++LastMI;
229  finalizeBundle(MBB, FirstMI, LastMI);
230  return LastMI;
231 }
232 
233 /// finalizeBundles - Finalize instruction bundles in the specified
234 /// MachineFunction. Return true if any bundles are finalized.
236  bool Changed = false;
237  for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
238  MachineBasicBlock &MBB = *I;
241  if (MII == MIE)
242  continue;
243  assert(!MII->isInsideBundle() &&
244  "First instr cannot be inside bundle before finalization!");
245 
246  for (++MII; MII != MIE; ) {
247  if (!MII->isInsideBundle())
248  ++MII;
249  else {
250  MII = finalizeBundle(MBB, std::prev(MII));
251  Changed = true;
252  }
253  }
254  }
255 
256  return Changed;
257 }
258 
259 //===----------------------------------------------------------------------===//
260 // MachineOperand iterator
261 //===----------------------------------------------------------------------===//
262 
265  SmallVectorImpl<std::pair<MachineInstr*, unsigned> > *Ops) {
266  VirtRegInfo RI = { false, false, false };
267  for(; isValid(); ++*this) {
268  MachineOperand &MO = deref();
269  if (!MO.isReg() || MO.getReg() != Reg)
270  continue;
271 
272  // Remember each (MI, OpNo) that refers to Reg.
273  if (Ops)
274  Ops->push_back(std::make_pair(MO.getParent(), getOperandNo()));
275 
276  // Both defs and uses can read virtual registers.
277  if (MO.readsReg()) {
278  RI.Reads = true;
279  if (MO.isDef())
280  RI.Tied = true;
281  }
282 
283  // Only defs can write.
284  if (MO.isDef())
285  RI.Writes = true;
286  else if (!RI.Tied && MO.getParent()->isRegTiedToDefOperand(getOperandNo()))
287  RI.Tied = true;
288  }
289  return RI;
290 }
291 
294  const TargetRegisterInfo *TRI) {
295  bool AllDefsDead = true;
296  PhysRegInfo PRI = {false, false, false, false, false, false};
297 
299  "analyzePhysReg not given a physical register!");
300  for (; isValid(); ++*this) {
301  MachineOperand &MO = deref();
302 
303  if (MO.isRegMask() && MO.clobbersPhysReg(Reg))
304  PRI.Clobbers = true; // Regmask clobbers Reg.
305 
306  if (!MO.isReg())
307  continue;
308 
309  unsigned MOReg = MO.getReg();
310  if (!MOReg || !TargetRegisterInfo::isPhysicalRegister(MOReg))
311  continue;
312 
313  bool IsRegOrSuperReg = MOReg == Reg || TRI->isSubRegister(MOReg, Reg);
314  bool IsRegOrOverlapping = MOReg == Reg || TRI->regsOverlap(MOReg, Reg);
315 
316  if (IsRegOrSuperReg && MO.readsReg()) {
317  // Reg or a super-reg is read, and perhaps killed also.
318  PRI.Reads = true;
319  PRI.Kills = MO.isKill();
320  }
321 
322  if (IsRegOrOverlapping && MO.readsReg()) {
323  PRI.ReadsOverlap = true;// Reg or an overlapping register is read.
324  }
325 
326  if (!MO.isDef())
327  continue;
328 
329  if (IsRegOrSuperReg) {
330  PRI.Defines = true; // Reg or a super-register is defined.
331  if (!MO.isDead())
332  AllDefsDead = false;
333  }
334  if (IsRegOrOverlapping)
335  PRI.Clobbers = true; // Reg or an overlapping reg is defined.
336  }
337 
338  if (AllDefsDead && PRI.Defines)
339  PRI.DefinesDead = true; // Reg or super-register was defined and was dead.
340 
341  return PRI;
342 }
const MachineFunction * getParent() const
getParent - Return the MachineFunction containing this basic block.
INITIALIZE_PASS(UnpackMachineBundles,"unpack-mi-bundles","Unpack machine instruction bundles", false, false) bool UnpackMachineBundles
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()
bool Tied
Tied - Uses and defs must use the same register.
bool isValid() const
isValid - returns true if this iterator is not yet at the end.
bool isDead() const
MachineOperand & deref() const
void initializeFinalizeMachineBundlesPass(PassRegistry &)
bool isSubRegister(unsigned RegA, unsigned RegB) const
Returns true if RegB is a sub-register of RegA.
void initializeUnpackMachineBundlesPass(PassRegistry &)
Instructions::iterator instr_iterator
bool erase(const T &V)
Definition: SmallSet.h:96
VirtRegInfo analyzeVirtReg(unsigned Reg, SmallVectorImpl< std::pair< MachineInstr *, unsigned > > *Ops=nullptr)
analyzeVirtReg - Analyze how the current instruction or bundle uses a virtual register.
bool Clobbers
Clobbers - Reg or an overlapping register is defined, or a regmask clobbers Reg.
bool DefinesDead
DefinesDead - All defs of a Reg or a super-register are dead.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
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: APInt.h:33
bool isReg() const
isReg - Tests if this is a MO_Register operand.
void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
Reg
All possible values of the reg field in the ModR/M byte.
bool isUndef() const
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.
bool isKill() const
MIBundleBuilder & prepend(MachineInstr *MI)
Insert MI into MBB by prepending it to the instructions in the bundle.
unsigned getUndefRegState(bool B)
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)
bool isBundle() const
Definition: MachineInstr.h:775
unsigned getDefRegState(bool B)
bool Kills
There is a kill of Reg or a super-register.
bool regsOverlap(unsigned regA, unsigned regB) const
regsOverlap - Returns true if the two registers are equal or alias each other.
* if(!EatIfPresent(lltok::kw_thread_local)) return false
ParseOptionalThreadLocal := /*empty.
bool ReadsOverlap
ReadsOverlap - Reg or an overlapping register is read.
bool Writes
Writes - One of the operands writes the virtual register.
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition: SmallSet.h:32
FunctionPass * createUnpackMachineBundles(std::function< bool(const Function &)> Ftor)
void setIsInternalRead(bool Val=true)
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:294
std::pair< NoneType, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
Definition: SmallSet.h:69
bool Reads
Reads - One of the operands read the virtual register.
MachineInstrBuilder BuildMI(MachineFunction &MF, DebugLoc DL, const MCInstrDesc &MCID)
BuildMI - Builder interface.
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.
const MCInstrDesc & get(unsigned Opcode) const
Return the machine instruction descriptor that corresponds to the specified instruction opcode...
Definition: MCInstrInfo.h:45
bool isRegMask() const
isRegMask - Tests if this is a MO_RegisterMask operand.
size_type count(const T &V) const
count - Return 1 if the element is in the set, 0 otherwise.
Definition: SmallSet.h:53
MachineOperand class - Representation of each machine instruction operand.
bool finalizeBundles(MachineFunction &MF)
finalizeBundles - Finalize instruction bundles in the specified MachineFunction.
bool Reads
Reads - Read or a super-register is read.
BUNDLE - This instruction represents an instruction bundle.
Definition: TargetOpcodes.h:91
static bool clobbersPhysReg(const uint32_t *RegMask, unsigned PhysReg)
clobbersPhysReg - Returns true if this RegMask clobbers PhysReg.
Representation of each machine instruction.
Definition: MachineInstr.h:51
static bool isPhysicalRegister(unsigned Reg)
isPhysicalRegister - Return true if the specified register number is in the physical register namespa...
bool Defines
Defines - Reg or a super-register is defined.
#define I(x, y, z)
Definition: MD5.cpp:54
unsigned getImplRegState(bool B)
PhysRegInfo - Information about a physical register used by a set of operands.
unsigned getReg() const
getReg - Returns the register number.
bool isValid() const
isValid - Returns true until all the operands have been visited.
virtual const TargetInstrInfo * getInstrInfo() const
BasicBlockListType::iterator iterator
bool isRegTiedToDefOperand(unsigned UseOpIdx, unsigned *DefOpIdx=nullptr) const
Return true if the use operand of the specified index is tied to a def operand.
print Print MemDeps of function
virtual const TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
bool readsReg() const
readsReg - Returns true if this operand reads the previous value of its register. ...
const MachineInstrBuilder & addReg(unsigned RegNo, unsigned flags=0, unsigned SubReg=0) const
addReg - Add a new virtual register operand...
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...
unsigned getOperandNo() const
getOperandNo - Returns the number of the current operand relative to its instruction.
bool isInternalRead() const
Helper class for constructing bundles of MachineInstrs.