LLVM 23.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 {
35
36class BreakFalseDeps : public MachineFunctionPass {
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 ReachingDefInfo *RDI = nullptr;
50
51public:
52 static char ID; // Pass identification, replacement for typeid
53
54 BreakFalseDeps() : MachineFunctionPass(ID) {}
55
56 void getAnalysisUsage(AnalysisUsage &AU) const override {
57 AU.setPreservesAll();
58 AU.addRequired<ReachingDefInfoWrapperPass>();
60 }
61
62 bool runOnMachineFunction(MachineFunction &MF) override;
63
64 MachineFunctionProperties getRequiredProperties() const override {
65 return MachineFunctionProperties().setNoVRegs();
66 }
67
68private:
69 /// Process he given basic block.
70 void processBasicBlock(MachineBasicBlock *MBB);
71
72 /// Update def-ages for registers defined by MI.
73 /// Also break dependencies on partial defs and undef uses.
74 void processDefs(MachineInstr *MI);
75
76 /// Helps avoid false dependencies on undef registers by updating the
77 /// machine instructions' undef operand to use a register that the instruction
78 /// is truly dependent on, or use a register with clearance higher than Pref.
79 /// Returns true if it was able to find a true dependency, thus not requiring
80 /// a dependency breaking instruction regardless of clearance.
81 bool pickBestRegisterForUndef(MachineInstr *MI, unsigned OpIdx,
82 unsigned Pref);
83
84 /// Return true to if it makes sense to break dependence on a partial
85 /// def or undef use.
86 bool shouldBreakDependence(MachineInstr *, unsigned OpIdx, unsigned Pref);
87
88 /// Break false dependencies on undefined register reads.
89 /// Walk the block backward computing precise liveness. This is expensive, so
90 /// we only do it on demand. Note that the occurrence of undefined register
91 /// reads that should be broken is very rare, but when they occur we may have
92 /// many in a single block.
93 void processUndefReads(MachineBasicBlock *);
94};
95
96} // namespace
97
98#define DEBUG_TYPE "break-false-deps"
99
100char BreakFalseDeps::ID = 0;
101INITIALIZE_PASS_BEGIN(BreakFalseDeps, DEBUG_TYPE, "BreakFalseDeps", false, false)
103INITIALIZE_PASS_END(BreakFalseDeps, DEBUG_TYPE, "BreakFalseDeps", false, false)
104
105FunctionPass *llvm::createBreakFalseDeps() { return new BreakFalseDeps(); }
106
107bool BreakFalseDeps::pickBestRegisterForUndef(MachineInstr *MI, unsigned OpIdx,
108 unsigned Pref) {
109
110 // We can't change tied operands.
111 if (MI->isRegTiedToDefOperand(OpIdx))
112 return false;
113
114 MachineOperand &MO = MI->getOperand(OpIdx);
115 assert(MO.isUndef() && "Expected undef machine operand");
116
117 // We can't change registers that aren't renamable.
118 if (!MO.isRenamable())
119 return false;
120
121 MCRegister OriginalReg = MO.getReg().asMCReg();
122
123 // Update only undef operands that have reg units that are mapped to one root.
124 for (MCRegUnit Unit : TRI->regunits(OriginalReg)) {
125 unsigned NumRoots = 0;
126 for (MCRegUnitRootIterator Root(Unit, TRI); Root.isValid(); ++Root) {
127 NumRoots++;
128 if (NumRoots > 1)
129 return false;
130 }
131 }
132
133 // Get the undef operand's register class
134 const TargetRegisterClass *OpRC = TII->getRegClass(MI->getDesc(), OpIdx);
135 assert(OpRC && "Not a valid register class");
136
137 // If the instruction has a true dependency, we can hide the false depdency
138 // behind it.
139 for (MachineOperand &CurrMO : MI->all_uses()) {
140 if (CurrMO.isUndef() || !OpRC->contains(CurrMO.getReg()))
141 continue;
142 // We found a true dependency - replace the undef register with the true
143 // dependency.
144 MO.setReg(CurrMO.getReg());
145 return true;
146 }
147
148 // Go over all registers in the register class and find the register with
149 // max clearance or clearance higher than Pref.
150 unsigned MaxClearance = 0;
151 unsigned MaxClearanceReg = OriginalReg;
152 ArrayRef<MCPhysReg> Order = RegClassInfo.getOrder(OpRC);
153 for (MCPhysReg Reg : Order) {
154 unsigned Clearance = RDI->getClearance(MI, Reg);
155 if (Clearance <= MaxClearance)
156 continue;
157 MaxClearance = Clearance;
158 MaxClearanceReg = Reg;
159
160 if (MaxClearance > Pref)
161 break;
162 }
163
164 // Update the operand if we found a register with better clearance.
165 if (MaxClearanceReg != OriginalReg)
166 MO.setReg(MaxClearanceReg);
167
168 return false;
169}
170
171bool BreakFalseDeps::shouldBreakDependence(MachineInstr *MI, unsigned OpIdx,
172 unsigned Pref) {
173 MCRegister Reg = MI->getOperand(OpIdx).getReg().asMCReg();
174 unsigned Clearance = RDI->getClearance(MI, Reg);
175 LLVM_DEBUG(dbgs() << "Clearance: " << Clearance << ", want " << Pref);
176
177 if (Pref > Clearance) {
178 LLVM_DEBUG(dbgs() << ": Break dependency.\n");
179 return true;
180 }
181 LLVM_DEBUG(dbgs() << ": OK .\n");
182 return false;
183}
184
185void BreakFalseDeps::processDefs(MachineInstr *MI) {
186 assert(!MI->isDebugInstr() && "Won't process debug values");
187
188 const MCInstrDesc &MCID = MI->getDesc();
189
190 // Break dependence on undef uses. Do this before updating LiveRegs below.
191 // This can remove a false dependence with no additional instructions.
192 for (unsigned i = MCID.getNumDefs(), e = MCID.getNumOperands(); i != e; ++i) {
193 MachineOperand &MO = MI->getOperand(i);
194 if (!MO.isReg() || !MO.getReg() || !MO.isUse() || !MO.isUndef())
195 continue;
196
197 unsigned Pref = TII->getUndefRegClearance(*MI, i, TRI);
198 if (Pref) {
199 bool HadTrueDependency = pickBestRegisterForUndef(MI, i, Pref);
200 // We don't need to bother trying to break a dependency if this
201 // instruction has a true dependency on that register through another
202 // operand - we'll have to wait for it to be available regardless.
203 if (!HadTrueDependency && shouldBreakDependence(MI, i, Pref))
204 UndefReads.push_back(std::make_pair(MI, i));
205 }
206 }
207
208 // The code below allows the target to create a new instruction to break the
209 // dependence. That opposes the goal of minimizing size, so bail out now.
210 if (MF->getFunction().hasMinSize())
211 return;
212
213 for (unsigned i = 0,
214 e = MI->isVariadic() ? MI->getNumOperands() : MCID.getNumDefs();
215 i != e; ++i) {
216 MachineOperand &MO = MI->getOperand(i);
217 if (!MO.isReg() || !MO.getReg())
218 continue;
219 if (MO.isUse())
220 continue;
221 // Check clearance before partial register updates.
222 unsigned Pref = TII->getPartialRegUpdateClearance(*MI, i, TRI);
223 if (Pref && shouldBreakDependence(MI, i, Pref))
224 TII->breakPartialRegDependency(*MI, i, TRI);
225 }
226}
227
228void BreakFalseDeps::processUndefReads(MachineBasicBlock *MBB) {
229 if (UndefReads.empty())
230 return;
231
232 // The code below allows the target to create a new instruction to break the
233 // dependence. That opposes the goal of minimizing size, so bail out now.
234 if (MF->getFunction().hasMinSize())
235 return;
236
237 // Collect this block's live out register units.
238 LiveRegSet.init(*TRI);
239 // We do not need to care about pristine registers as they are just preserved
240 // but not actually used in the function.
241 LiveRegSet.addLiveOutsNoPristines(*MBB);
242
243 MachineInstr *UndefMI = UndefReads.back().first;
244 unsigned OpIdx = UndefReads.back().second;
245
246 for (MachineInstr &I : llvm::reverse(*MBB)) {
247 // Update liveness, including the current instruction's defs.
248 LiveRegSet.stepBackward(I);
249
250 if (UndefMI == &I) {
251 if (!LiveRegSet.contains(UndefMI->getOperand(OpIdx).getReg()))
252 TII->breakPartialRegDependency(*UndefMI, OpIdx, TRI);
253
254 UndefReads.pop_back();
255 if (UndefReads.empty())
256 return;
257
258 UndefMI = UndefReads.back().first;
259 OpIdx = UndefReads.back().second;
260 }
261 }
262}
263
264void BreakFalseDeps::processBasicBlock(MachineBasicBlock *MBB) {
265 UndefReads.clear();
266 // If this block is not done, it makes little sense to make any decisions
267 // based on clearance information. We need to make a second pass anyway,
268 // and by then we'll have better information, so we can avoid doing the work
269 // to try and break dependencies now.
270 for (MachineInstr &MI : *MBB) {
271 if (!MI.isDebugInstr())
272 processDefs(&MI);
273 }
274 processUndefReads(MBB);
275}
276
277bool BreakFalseDeps::runOnMachineFunction(MachineFunction &mf) {
278 if (skipFunction(mf.getFunction()))
279 return false;
280 MF = &mf;
281 TII = MF->getSubtarget().getInstrInfo();
283 RDI = &getAnalysis<ReachingDefInfoWrapperPass>().getRDI();
284
285 RegClassInfo.runOnMachineFunction(mf, /*Rev=*/true);
286
287 LLVM_DEBUG(dbgs() << "********** BREAK FALSE DEPENDENCIES **********\n");
288
289 // Skip Dead blocks due to ReachingDefAnalysis has no idea about instructions
290 // in them.
291 df_iterator_default_set<MachineBasicBlock *> Reachable;
292 for (MachineBasicBlock *MBB : depth_first_ext(&mf, Reachable))
293 (void)MBB /* Mark all reachable blocks */;
294
295 // Traverse the basic blocks.
296 for (MachineBasicBlock &MBB : mf)
297 if (Reachable.count(&MBB))
298 processBasicBlock(&MBB);
299
300 return false;
301}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
ReachingDefInfo & RDI
MachineBasicBlock & MBB
This file builds on the ADT/GraphTraits.h file to build generic depth first graph iterator.
#define DEBUG_TYPE
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
This file implements the LivePhysRegs utility for tracking liveness of physical registers.
#define I(x, y, z)
Definition MD5.cpp:57
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
AnalysisUsage & addRequired()
void setPreservesAll()
Set by analyses that do not transform their input at all.
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
bool hasMinSize() const
Optimize this function for minimum size (-Oz).
Definition Function.h:709
void stepBackward(const MachineInstr &MI)
Simulates liveness when stepping backwards over an instruction(bundle).
void init(const TargetRegisterInfo &TRI)
(re-)initializes and clears the set.
void addLiveOutsNoPristines(const MachineBasicBlock &MBB)
Adds all live-out registers of basic block MBB but skips pristine registers.
bool contains(MCRegister Reg) const
Returns true if register Reg is contained in the set.
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:41
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
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.
int getClearance(MachineInstr *MI, Register Reg) const
Provides the clearance - the number of instructions since the closest reaching def instuction of Reg ...
LLVM_ABI void runOnMachineFunction(const MachineFunction &MF, bool Rev=false)
runOnFunction - Prepare to answer questions about MF.
ArrayRef< MCPhysReg > getOrder(const TargetRegisterClass *RC) const
getOrder - Returns the preferred allocation order for RC.
MCRegister asMCReg() const
Utility to check-convert this value to a MCRegister.
Definition Register.h:107
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
bool contains(Register Reg) const
Return true if the specified register is included in this register class.
virtual const TargetInstrInfo * getInstrInfo() const
virtual const TargetRegisterInfo * getRegisterInfo() const =0
Return the target's register information.
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
iterator_range< df_ext_iterator< T, SetTy > > depth_first_ext(const T &G, SetTy &S)
auto reverse(ContainerTy &&C)
Definition STLExtras.h:406
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
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 >