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
28#include "llvm/MC/MCInstrDesc.h"
29#include "llvm/MC/MCRegister.h"
31#include "llvm/Support/Debug.h"
32
33using namespace llvm;
34
35namespace {
36
37class BreakFalseDeps {
38private:
39 MachineFunction *MF = nullptr;
40 const TargetInstrInfo *TII = nullptr;
41 const TargetRegisterInfo *TRI = nullptr;
42 RegisterClassInfo RegClassInfo;
43
44 /// List of undefined register reads in this block in forward order.
45 std::vector<std::pair<MachineInstr *, unsigned>> UndefReads;
46
47 /// Storage for register unit liveness.
48 LivePhysRegs LiveRegSet;
49
50 ReachingDefInfo *RDI = nullptr;
51
52 /// True if the pass insert instructions or updates registers, false
53 /// otherwise.
54 bool Changed = false;
55
56public:
57 BreakFalseDeps(ReachingDefInfo *RDI) : RDI(RDI) {}
58
59 bool run(MachineFunction &CurMF);
60
61private:
62 /// Process he given basic block.
63 void processBasicBlock(MachineBasicBlock *MBB);
64
65 /// Update def-ages for registers defined by MI.
66 /// Also break dependencies on partial defs and undef uses.
67 void processDefs(MachineInstr *MI);
68
69 /// Helps avoid false dependencies on undef registers by updating the
70 /// machine instructions' undef operand to use a register that the instruction
71 /// is truly dependent on, or use a register with clearance higher than Pref.
72 /// Returns true if it was able to find a true dependency, thus not requiring
73 /// a dependency breaking instruction regardless of clearance.
74 bool pickBestRegisterForUndef(MachineInstr *MI, unsigned OpIdx,
75 unsigned Pref);
76
77 /// Return true to if it makes sense to break dependence on a partial
78 /// def or undef use.
79 bool shouldBreakDependence(MachineInstr *, unsigned OpIdx, unsigned Pref);
80
81 /// Break false dependencies on undefined register reads.
82 /// Walk the block backward computing precise liveness. This is expensive, so
83 /// we only do it on demand. Note that the occurrence of undefined register
84 /// reads that should be broken is very rare, but when they occur we may have
85 /// many in a single block.
86 void processUndefReads(MachineBasicBlock *);
87};
88
89class BreakFalseDepsLegacy : public MachineFunctionPass {
90public:
91 static char ID; // Pass identification, replacement for typeid
92
93 BreakFalseDepsLegacy() : MachineFunctionPass(ID) {}
94
95 void getAnalysisUsage(AnalysisUsage &AU) const override {
96 AU.setPreservesCFG();
97 AU.addRequired<ReachingDefInfoWrapperPass>();
99 }
100
101 bool runOnMachineFunction(MachineFunction &MF) override;
102
103 MachineFunctionProperties getRequiredProperties() const override {
104 return MachineFunctionProperties().setNoVRegs();
105 }
106};
107
108} // namespace
109
110#define DEBUG_TYPE "break-false-deps"
111
112char BreakFalseDepsLegacy::ID = 0;
113INITIALIZE_PASS_BEGIN(BreakFalseDepsLegacy, DEBUG_TYPE, "BreakFalseDeps", false,
114 false)
116INITIALIZE_PASS_END(BreakFalseDepsLegacy, DEBUG_TYPE, "BreakFalseDeps", false,
117 false)
118
120 return new BreakFalseDepsLegacy();
121}
122
123bool BreakFalseDeps::pickBestRegisterForUndef(MachineInstr *MI, unsigned OpIdx,
124 unsigned Pref) {
125
126 // We can't change tied operands.
127 if (MI->isRegTiedToDefOperand(OpIdx))
128 return false;
129
130 MachineOperand &MO = MI->getOperand(OpIdx);
131 assert(MO.isUndef() && "Expected undef machine operand");
132
133 // We can't change registers that aren't renamable.
134 if (!MO.isRenamable())
135 return false;
136
137 MCRegister OriginalReg = MO.getReg().asMCReg();
138
139 // Update only undef operands that have reg units that are mapped to one root.
140 for (MCRegUnit Unit : TRI->regunits(OriginalReg)) {
141 unsigned NumRoots = 0;
142 for (MCRegUnitRootIterator Root(Unit, TRI); Root.isValid(); ++Root) {
143 NumRoots++;
144 if (NumRoots > 1)
145 return false;
146 }
147 }
148
149 // Get the undef operand's register class
150 const TargetRegisterClass *OpRC = TII->getRegClass(MI->getDesc(), OpIdx);
151 assert(OpRC && "Not a valid register class");
152
153 // If the instruction has a true dependency, we can hide the false depdency
154 // behind it.
155 for (MachineOperand &CurrMO : MI->all_uses()) {
156 if (CurrMO.isUndef() || !OpRC->contains(CurrMO.getReg()))
157 continue;
158 // We found a true dependency - replace the undef register with the true
159 // dependency.
160 MO.setReg(CurrMO.getReg());
161 Changed = true;
162 return true;
163 }
164
165 // Go over all registers in the register class and find the register with
166 // max clearance or clearance higher than Pref.
167 unsigned MaxClearance = 0;
168 unsigned MaxClearanceReg = OriginalReg;
169 ArrayRef<MCPhysReg> Order = RegClassInfo.getOrder(OpRC);
170 for (MCPhysReg Reg : Order) {
171 unsigned Clearance = RDI->getClearance(MI, Reg);
172 if (Clearance <= MaxClearance)
173 continue;
174 MaxClearance = Clearance;
175 MaxClearanceReg = Reg;
176
177 if (MaxClearance > Pref)
178 break;
179 }
180
181 // Update the operand if we found a register with better clearance.
182 if (MaxClearanceReg != OriginalReg) {
183 MO.setReg(MaxClearanceReg);
184 Changed = true;
185 }
186
187 return false;
188}
189
190bool BreakFalseDeps::shouldBreakDependence(MachineInstr *MI, unsigned OpIdx,
191 unsigned Pref) {
192 MCRegister Reg = MI->getOperand(OpIdx).getReg().asMCReg();
193 unsigned Clearance = RDI->getClearance(MI, Reg);
194 LLVM_DEBUG(dbgs() << "Clearance: " << Clearance << ", want " << Pref);
195
196 if (Pref > Clearance) {
197 LLVM_DEBUG(dbgs() << ": Break dependency.\n");
198 return true;
199 }
200 LLVM_DEBUG(dbgs() << ": OK .\n");
201 return false;
202}
203
204void BreakFalseDeps::processDefs(MachineInstr *MI) {
205 assert(!MI->isDebugInstr() && "Won't process debug values");
206
207 const MCInstrDesc &MCID = MI->getDesc();
208
209 // Break dependence on undef uses. Do this before updating LiveRegs below.
210 // This can remove a false dependence with no additional instructions.
211 for (unsigned i = MCID.getNumDefs(), e = MCID.getNumOperands(); i != e; ++i) {
212 MachineOperand &MO = MI->getOperand(i);
213 if (!MO.isReg() || !MO.getReg() || !MO.isUse() || !MO.isUndef())
214 continue;
215
216 unsigned Pref = TII->getUndefRegClearance(*MI, i, TRI);
217 if (Pref) {
218 bool HadTrueDependency = pickBestRegisterForUndef(MI, i, Pref);
219 // We don't need to bother trying to break a dependency if this
220 // instruction has a true dependency on that register through another
221 // operand - we'll have to wait for it to be available regardless.
222 if (!HadTrueDependency && shouldBreakDependence(MI, i, Pref))
223 UndefReads.push_back(std::make_pair(MI, i));
224 }
225 }
226
227 // The code below allows the target to create a new instruction to break the
228 // dependence. That opposes the goal of minimizing size, so bail out now.
229 if (MF->getFunction().hasMinSize())
230 return;
231
232 for (unsigned i = 0,
233 e = MI->isVariadic() ? MI->getNumOperands() : MCID.getNumDefs();
234 i != e; ++i) {
235 MachineOperand &MO = MI->getOperand(i);
236 if (!MO.isReg() || !MO.getReg())
237 continue;
238 if (MO.isUse())
239 continue;
240 // Check clearance before partial register updates.
241 unsigned Pref = TII->getPartialRegUpdateClearance(*MI, i, TRI);
242 if (Pref && shouldBreakDependence(MI, i, Pref)) {
243 TII->breakPartialRegDependency(*MI, i, TRI);
244 Changed = true;
245 }
246 }
247}
248
249void BreakFalseDeps::processUndefReads(MachineBasicBlock *MBB) {
250 if (UndefReads.empty())
251 return;
252
253 // The code below allows the target to create a new instruction to break the
254 // dependence. That opposes the goal of minimizing size, so bail out now.
255 if (MF->getFunction().hasMinSize())
256 return;
257
258 // Collect this block's live out register units.
259 LiveRegSet.init(*TRI);
260 // We do not need to care about pristine registers as they are just preserved
261 // but not actually used in the function.
262 LiveRegSet.addLiveOutsNoPristines(*MBB);
263
264 MachineInstr *UndefMI = UndefReads.back().first;
265 unsigned OpIdx = UndefReads.back().second;
266
267 for (MachineInstr &I : llvm::reverse(*MBB)) {
268 // Update liveness, including the current instruction's defs.
269 LiveRegSet.stepBackward(I);
270
271 if (UndefMI == &I) {
272 if (!LiveRegSet.contains(UndefMI->getOperand(OpIdx).getReg())) {
273 TII->breakPartialRegDependency(*UndefMI, OpIdx, TRI);
274 Changed = true;
275 }
276
277 UndefReads.pop_back();
278 if (UndefReads.empty())
279 return;
280
281 UndefMI = UndefReads.back().first;
282 OpIdx = UndefReads.back().second;
283 }
284 }
285}
286
287void BreakFalseDeps::processBasicBlock(MachineBasicBlock *MBB) {
288 UndefReads.clear();
289 // If this block is not done, it makes little sense to make any decisions
290 // based on clearance information. We need to make a second pass anyway,
291 // and by then we'll have better information, so we can avoid doing the work
292 // to try and break dependencies now.
293 for (MachineInstr &MI : *MBB) {
294 if (!MI.isDebugInstr())
295 processDefs(&MI);
296 }
297 processUndefReads(MBB);
298}
299
300bool BreakFalseDeps::run(MachineFunction &CurMF) {
301 MF = &CurMF;
302 TII = MF->getSubtarget().getInstrInfo();
304
305 RegClassInfo.runOnMachineFunction(CurMF, /*Rev=*/true);
306
307 LLVM_DEBUG(dbgs() << "********** BREAK FALSE DEPENDENCIES **********\n");
308
309 // Skip Dead blocks due to ReachingDefAnalysis has no idea about instructions
310 // in them.
311 df_iterator_default_set<MachineBasicBlock *> Reachable;
312 for (MachineBasicBlock *MBB : depth_first_ext(&CurMF, Reachable))
313 (void)MBB /* Mark all reachable blocks */;
314
315 // Traverse the basic blocks.
316 for (MachineBasicBlock &MBB : CurMF)
317 if (Reachable.count(&MBB))
319
320 return Changed;
321}
322
323bool BreakFalseDepsLegacy::runOnMachineFunction(MachineFunction &MF) {
324 if (skipFunction(MF.getFunction()))
325 return false;
326
327 ReachingDefInfo *RDI = &getAnalysis<ReachingDefInfoWrapperPass>().getRDI();
328 BreakFalseDeps Impl(RDI);
329 return Impl.run(MF);
330}
331
332PreservedAnalyses
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
ReachingDefInfo & RDI
MachineBasicBlock & MBB
BreakFalseDeps
This file contains the declaration of the BreakFalseDepsPass class, used to identify and avoid false ...
This file builds on the ADT/GraphTraits.h file to build generic depth first graph iterator.
#define DEBUG_TYPE
const HexagonInstrInfo * TII
#define _
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:119
static bool processBasicBlock(MachineBasicBlock &MBB, BlockStateMap &BlockStates, DirtySuccessorsWorkList &DirtySuccessors, bool IsX86INTR, const TargetInstrInfo *TII)
Loop over all of the instructions in the basic block, inserting vzeroupper instructions before functi...
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
AnalysisUsage & addRequired()
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition Pass.cpp:270
LLVM_ABI PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
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:711
LLVM_ABI 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.
LLVM_ABI 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
An RAII based helper class to modify MachineFunctionProperties when running pass.
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.
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Definition Analysis.h:151
This class provides the reaching def analysis.
LLVM_ABI 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.
Changed
DXILDebugInfoMap run(Module &M)
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)
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
auto reverse(ContainerTy &&C)
Definition STLExtras.h:407
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
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 >
LLVM_ABI FunctionPass * createBreakFalseDepsLegacyPass()
Creates Break False Dependencies pass.