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