LLVM 22.0.0git
BreakFalseDeps.cpp
Go to the documentation of this file.
1//==- llvm/CodeGen/BreakFalseDeps.cpp - Break False Dependency Fix -*- C++ -*==//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9/// \file Break False Dependency pass.
10///
11/// Some instructions have false dependencies which cause unnecessary stalls.
12/// For example, instructions may write part of a register and implicitly
13/// need to read the other parts of the register. This may cause unwanted
14/// stalls preventing otherwise unrelated instructions from executing in
15/// parallel in an out-of-order CPU.
16/// This pass is aimed at identifying and avoiding these dependencies.
17//
18//===----------------------------------------------------------------------===//
19
27#include "llvm/MC/MCInstrDesc.h"
28#include "llvm/MC/MCRegister.h"
30#include "llvm/Support/Debug.h"
31
32using namespace llvm;
33
34namespace llvm {
35
37private:
38 MachineFunction *MF = nullptr;
39 const TargetInstrInfo *TII = nullptr;
40 const TargetRegisterInfo *TRI = nullptr;
41 RegisterClassInfo RegClassInfo;
42
43 /// List of undefined register reads in this block in forward order.
44 std::vector<std::pair<MachineInstr *, unsigned>> UndefReads;
45
46 /// Storage for register unit liveness.
47 LivePhysRegs LiveRegSet;
48
49 ReachingDefAnalysis *RDA = nullptr;
50
51public:
52 static char ID; // Pass identification, replacement for typeid
53
57
63
64 bool runOnMachineFunction(MachineFunction &MF) override;
65
67 return MachineFunctionProperties().setNoVRegs();
68 }
69
70private:
71 /// Process he given basic block.
72 void processBasicBlock(MachineBasicBlock *MBB);
73
74 /// Update def-ages for registers defined by MI.
75 /// Also break dependencies on partial defs and undef uses.
76 void processDefs(MachineInstr *MI);
77
78 /// Helps avoid false dependencies on undef registers by updating the
79 /// machine instructions' undef operand to use a register that the instruction
80 /// is truly dependent on, or use a register with clearance higher than Pref.
81 /// Returns true if it was able to find a true dependency, thus not requiring
82 /// a dependency breaking instruction regardless of clearance.
83 bool pickBestRegisterForUndef(MachineInstr *MI, unsigned OpIdx,
84 unsigned Pref);
85
86 /// Return true to if it makes sense to break dependence on a partial
87 /// def or undef use.
88 bool shouldBreakDependence(MachineInstr *, unsigned OpIdx, unsigned Pref);
89
90 /// Break false dependencies on undefined register reads.
91 /// Walk the block backward computing precise liveness. This is expensive, so
92 /// we only do it on demand. Note that the occurrence of undefined register
93 /// reads that should be broken is very rare, but when they occur we may have
94 /// many in a single block.
95 void processUndefReads(MachineBasicBlock *);
96};
97
98} // namespace llvm
99
100#define DEBUG_TYPE "break-false-deps"
101
102char BreakFalseDeps::ID = 0;
103INITIALIZE_PASS_BEGIN(BreakFalseDeps, DEBUG_TYPE, "BreakFalseDeps", false, false)
106
108
109bool BreakFalseDeps::pickBestRegisterForUndef(MachineInstr *MI, unsigned OpIdx,
110 unsigned Pref) {
111
112 // We can't change tied operands.
113 if (MI->isRegTiedToDefOperand(OpIdx))
114 return false;
115
116 MachineOperand &MO = MI->getOperand(OpIdx);
117 assert(MO.isUndef() && "Expected undef machine operand");
118
119 // We can't change registers that aren't renamable.
120 if (!MO.isRenamable())
121 return false;
122
123 MCRegister OriginalReg = MO.getReg().asMCReg();
124
125 // Update only undef operands that have reg units that are mapped to one root.
126 for (MCRegUnit Unit : TRI->regunits(OriginalReg)) {
127 unsigned NumRoots = 0;
128 for (MCRegUnitRootIterator Root(Unit, TRI); Root.isValid(); ++Root) {
129 NumRoots++;
130 if (NumRoots > 1)
131 return false;
132 }
133 }
134
135 // Get the undef operand's register class
136 const TargetRegisterClass *OpRC = TII->getRegClass(MI->getDesc(), OpIdx, TRI);
137 assert(OpRC && "Not a valid register class");
138
139 // If the instruction has a true dependency, we can hide the false depdency
140 // behind it.
141 for (MachineOperand &CurrMO : MI->all_uses()) {
142 if (CurrMO.isUndef() || !OpRC->contains(CurrMO.getReg()))
143 continue;
144 // We found a true dependency - replace the undef register with the true
145 // dependency.
146 MO.setReg(CurrMO.getReg());
147 return true;
148 }
149
150 // Go over all registers in the register class and find the register with
151 // max clearance or clearance higher than Pref.
152 unsigned MaxClearance = 0;
153 unsigned MaxClearanceReg = OriginalReg;
154 ArrayRef<MCPhysReg> Order = RegClassInfo.getOrder(OpRC);
155 for (MCPhysReg Reg : Order) {
156 unsigned Clearance = RDA->getClearance(MI, Reg);
157 if (Clearance <= MaxClearance)
158 continue;
159 MaxClearance = Clearance;
160 MaxClearanceReg = Reg;
161
162 if (MaxClearance > Pref)
163 break;
164 }
165
166 // Update the operand if we found a register with better clearance.
167 if (MaxClearanceReg != OriginalReg)
168 MO.setReg(MaxClearanceReg);
169
170 return false;
171}
172
173bool BreakFalseDeps::shouldBreakDependence(MachineInstr *MI, unsigned OpIdx,
174 unsigned Pref) {
175 MCRegister Reg = MI->getOperand(OpIdx).getReg().asMCReg();
176 unsigned Clearance = RDA->getClearance(MI, Reg);
177 LLVM_DEBUG(dbgs() << "Clearance: " << Clearance << ", want " << Pref);
178
179 if (Pref > Clearance) {
180 LLVM_DEBUG(dbgs() << ": Break dependency.\n");
181 return true;
182 }
183 LLVM_DEBUG(dbgs() << ": OK .\n");
184 return false;
185}
186
187void BreakFalseDeps::processDefs(MachineInstr *MI) {
188 assert(!MI->isDebugInstr() && "Won't process debug values");
189
190 const MCInstrDesc &MCID = MI->getDesc();
191
192 // Break dependence on undef uses. Do this before updating LiveRegs below.
193 // This can remove a false dependence with no additional instructions.
194 for (unsigned i = MCID.getNumDefs(), e = MCID.getNumOperands(); i != e; ++i) {
195 MachineOperand &MO = MI->getOperand(i);
196 if (!MO.isReg() || !MO.getReg() || !MO.isUse() || !MO.isUndef())
197 continue;
198
199 unsigned Pref = TII->getUndefRegClearance(*MI, i, TRI);
200 if (Pref) {
201 bool HadTrueDependency = pickBestRegisterForUndef(MI, i, Pref);
202 // We don't need to bother trying to break a dependency if this
203 // instruction has a true dependency on that register through another
204 // operand - we'll have to wait for it to be available regardless.
205 if (!HadTrueDependency && shouldBreakDependence(MI, i, Pref))
206 UndefReads.push_back(std::make_pair(MI, i));
207 }
208 }
209
210 // The code below allows the target to create a new instruction to break the
211 // dependence. That opposes the goal of minimizing size, so bail out now.
212 if (MF->getFunction().hasMinSize())
213 return;
214
215 for (unsigned i = 0,
216 e = MI->isVariadic() ? MI->getNumOperands() : MCID.getNumDefs();
217 i != e; ++i) {
218 MachineOperand &MO = MI->getOperand(i);
219 if (!MO.isReg() || !MO.getReg())
220 continue;
221 if (MO.isUse())
222 continue;
223 // Check clearance before partial register updates.
224 unsigned Pref = TII->getPartialRegUpdateClearance(*MI, i, TRI);
225 if (Pref && shouldBreakDependence(MI, i, Pref))
226 TII->breakPartialRegDependency(*MI, i, TRI);
227 }
228}
229
230void BreakFalseDeps::processUndefReads(MachineBasicBlock *MBB) {
231 if (UndefReads.empty())
232 return;
233
234 // The code below allows the target to create a new instruction to break the
235 // dependence. That opposes the goal of minimizing size, so bail out now.
236 if (MF->getFunction().hasMinSize())
237 return;
238
239 // Collect this block's live out register units.
240 LiveRegSet.init(*TRI);
241 // We do not need to care about pristine registers as they are just preserved
242 // but not actually used in the function.
243 LiveRegSet.addLiveOutsNoPristines(*MBB);
244
245 MachineInstr *UndefMI = UndefReads.back().first;
246 unsigned OpIdx = UndefReads.back().second;
247
248 for (MachineInstr &I : llvm::reverse(*MBB)) {
249 // Update liveness, including the current instruction's defs.
250 LiveRegSet.stepBackward(I);
251
252 if (UndefMI == &I) {
253 if (!LiveRegSet.contains(UndefMI->getOperand(OpIdx).getReg()))
254 TII->breakPartialRegDependency(*UndefMI, OpIdx, TRI);
255
256 UndefReads.pop_back();
257 if (UndefReads.empty())
258 return;
259
260 UndefMI = UndefReads.back().first;
261 OpIdx = UndefReads.back().second;
262 }
263 }
264}
265
266void BreakFalseDeps::processBasicBlock(MachineBasicBlock *MBB) {
267 UndefReads.clear();
268 // If this block is not done, it makes little sense to make any decisions
269 // based on clearance information. We need to make a second pass anyway,
270 // and by then we'll have better information, so we can avoid doing the work
271 // to try and break dependencies now.
272 for (MachineInstr &MI : *MBB) {
273 if (!MI.isDebugInstr())
274 processDefs(&MI);
275 }
276 processUndefReads(MBB);
277}
278
280 if (skipFunction(mf.getFunction()))
281 return false;
282 MF = &mf;
283 TII = MF->getSubtarget().getInstrInfo();
284 TRI = MF->getSubtarget().getRegisterInfo();
286
287 RegClassInfo.runOnMachineFunction(mf, /*Rev=*/true);
288
289 LLVM_DEBUG(dbgs() << "********** BREAK FALSE DEPENDENCIES **********\n");
290
291 // Skip Dead blocks due to ReachingDefAnalysis has no idea about instructions
292 // in them.
294 for (MachineBasicBlock *MBB : depth_first_ext(&mf, Reachable))
295 (void)MBB /* Mark all reachable blocks */;
296
297 // Traverse the basic blocks.
298 for (MachineBasicBlock &MBB : mf)
299 if (Reachable.count(&MBB))
300 processBasicBlock(&MBB);
301
302 return false;
303}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
This file builds on the ADT/GraphTraits.h file to build generic depth first graph iterator.
#define DEBUG_TYPE
IRTranslator LLVM IR MI
This file implements the LivePhysRegs utility for tracking liveness of physical registers.
#define I(x, y, z)
Definition MD5.cpp:58
Register Reg
Register const TargetRegisterInfo * TRI
MachineInstr unsigned OpIdx
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition PassSupport.h:42
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition PassSupport.h:44
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition PassSupport.h:39
#define LLVM_DEBUG(...)
Definition Debug.h:114
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
void setPreservesAll()
Set by analyses that do not transform their input at all.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
MachineFunctionProperties getRequiredProperties() const override
bool runOnMachineFunction(MachineFunction &MF) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
bool skipFunction(const Function &F) const
Optional passes call this function to check whether the pass should be skipped.
Definition Pass.cpp:188
A set of physical registers with utility functions to track liveness when walking backward/forward th...
unsigned getNumOperands() const
Return the number of declared MachineOperands for this MachineInstruction.
unsigned getNumDefs() const
Return the number of MachineOperands that are register definitions.
MCRegUnitRootIterator enumerates the root registers of a register unit.
bool isValid() const
Check if the iterator is at the end of the list.
Wrapper class representing physical registers. Should be passed by value.
Definition MCRegister.h:33
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
Properties which a MachineFunction may have at a given point in time.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Function & getFunction()
Return the LLVM function that this machine code represents.
Representation of each machine instruction.
const MachineOperand & getOperand(unsigned i) const
MachineOperand class - Representation of each machine instruction operand.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
LLVM_ABI void setReg(Register Reg)
Change the register this operand corresponds to.
LLVM_ABI bool isRenamable() const
isRenamable - Returns true if this register may be renamed, i.e.
Register getReg() const
getReg - Returns the register number.
static LLVM_ABI PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
AnalysisType & getAnalysis() const
getAnalysis<AnalysisType>() - This function is used by subclasses to get to the analysis information ...
This class provides the reaching def analysis.
MCRegister asMCReg() const
Utility to check-convert this value to a MCRegister.
Definition Register.h:102
TargetInstrInfo - Interface to description of machine instruction set.
bool contains(Register Reg) const
Return true if the specified register is included in this register class.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
virtual const TargetInstrInfo * getInstrInfo() const
This is an optimization pass for GlobalISel generic memory operations.
iterator_range< df_ext_iterator< T, SetTy > > depth_first_ext(const T &G, SetTy &S)
LLVM_ABI void initializeBreakFalseDepsPass(PassRegistry &)
auto reverse(ContainerTy &&C)
Definition STLExtras.h:400
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
unsigned MCRegUnit
Register units are used to compute register aliasing.
Definition MCRegister.h:30
LLVM_ABI FunctionPass * createBreakFalseDeps()
Creates Break False Dependencies pass.
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
Definition MCRegister.h:21
ArrayRef(const T &OneElt) -> ArrayRef< T >