xref: /src/contrib/llvm-project/llvm/lib/CodeGen/BreakFalseDeps.cpp (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
1eb11fae6SDimitry Andric //==- llvm/CodeGen/BreakFalseDeps.cpp - Break False Dependency Fix -*- C++ -*==//
2eb11fae6SDimitry Andric //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6eb11fae6SDimitry Andric //
7eb11fae6SDimitry Andric //===----------------------------------------------------------------------===//
8eb11fae6SDimitry Andric //
9eb11fae6SDimitry Andric /// \file Break False Dependency pass.
10eb11fae6SDimitry Andric ///
11eb11fae6SDimitry Andric /// Some instructions have false dependencies which cause unnecessary stalls.
121d5ae102SDimitry Andric /// For example, instructions may write part of a register and implicitly
13eb11fae6SDimitry Andric /// need to read the other parts of the register. This may cause unwanted
14eb11fae6SDimitry Andric /// stalls preventing otherwise unrelated instructions from executing in
15eb11fae6SDimitry Andric /// parallel in an out-of-order CPU.
161d5ae102SDimitry Andric /// This pass is aimed at identifying and avoiding these dependencies.
17eb11fae6SDimitry Andric //
18eb11fae6SDimitry Andric //===----------------------------------------------------------------------===//
19eb11fae6SDimitry Andric 
207fa27ce4SDimitry Andric #include "llvm/ADT/DepthFirstIterator.h"
21eb11fae6SDimitry Andric #include "llvm/CodeGen/LivePhysRegs.h"
22eb11fae6SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h"
23eb11fae6SDimitry Andric #include "llvm/CodeGen/ReachingDefAnalysis.h"
24eb11fae6SDimitry Andric #include "llvm/CodeGen/RegisterClassInfo.h"
25eb11fae6SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h"
26706b4fc4SDimitry Andric #include "llvm/InitializePasses.h"
27145449b1SDimitry Andric #include "llvm/MC/MCInstrDesc.h"
28145449b1SDimitry Andric #include "llvm/MC/MCRegister.h"
29145449b1SDimitry Andric #include "llvm/MC/MCRegisterInfo.h"
301d5ae102SDimitry Andric #include "llvm/Support/Debug.h"
31eb11fae6SDimitry Andric 
32eb11fae6SDimitry Andric using namespace llvm;
33eb11fae6SDimitry Andric 
34eb11fae6SDimitry Andric namespace llvm {
35eb11fae6SDimitry Andric 
36eb11fae6SDimitry Andric class BreakFalseDeps : public MachineFunctionPass {
37eb11fae6SDimitry Andric private:
387fa27ce4SDimitry Andric   MachineFunction *MF = nullptr;
397fa27ce4SDimitry Andric   const TargetInstrInfo *TII = nullptr;
407fa27ce4SDimitry Andric   const TargetRegisterInfo *TRI = nullptr;
41eb11fae6SDimitry Andric   RegisterClassInfo RegClassInfo;
42eb11fae6SDimitry Andric 
43eb11fae6SDimitry Andric   /// List of undefined register reads in this block in forward order.
44eb11fae6SDimitry Andric   std::vector<std::pair<MachineInstr *, unsigned>> UndefReads;
45eb11fae6SDimitry Andric 
46eb11fae6SDimitry Andric   /// Storage for register unit liveness.
47eb11fae6SDimitry Andric   LivePhysRegs LiveRegSet;
48eb11fae6SDimitry Andric 
497fa27ce4SDimitry Andric   ReachingDefAnalysis *RDA = nullptr;
50eb11fae6SDimitry Andric 
51eb11fae6SDimitry Andric public:
52eb11fae6SDimitry Andric   static char ID; // Pass identification, replacement for typeid
53eb11fae6SDimitry Andric 
BreakFalseDeps()54eb11fae6SDimitry Andric   BreakFalseDeps() : MachineFunctionPass(ID) {
55eb11fae6SDimitry Andric     initializeBreakFalseDepsPass(*PassRegistry::getPassRegistry());
56eb11fae6SDimitry Andric   }
57eb11fae6SDimitry Andric 
getAnalysisUsage(AnalysisUsage & AU) const58eb11fae6SDimitry Andric   void getAnalysisUsage(AnalysisUsage &AU) const override {
59eb11fae6SDimitry Andric     AU.setPreservesAll();
60eb11fae6SDimitry Andric     AU.addRequired<ReachingDefAnalysis>();
61eb11fae6SDimitry Andric     MachineFunctionPass::getAnalysisUsage(AU);
62eb11fae6SDimitry Andric   }
63eb11fae6SDimitry Andric 
64eb11fae6SDimitry Andric   bool runOnMachineFunction(MachineFunction &MF) override;
65eb11fae6SDimitry Andric 
getRequiredProperties() const66eb11fae6SDimitry Andric   MachineFunctionProperties getRequiredProperties() const override {
67eb11fae6SDimitry Andric     return MachineFunctionProperties().set(
68eb11fae6SDimitry Andric       MachineFunctionProperties::Property::NoVRegs);
69eb11fae6SDimitry Andric   }
70eb11fae6SDimitry Andric 
71eb11fae6SDimitry Andric private:
72eb11fae6SDimitry Andric   /// Process he given basic block.
73eb11fae6SDimitry Andric   void processBasicBlock(MachineBasicBlock *MBB);
74eb11fae6SDimitry Andric 
75eb11fae6SDimitry Andric   /// Update def-ages for registers defined by MI.
76eb11fae6SDimitry Andric   /// Also break dependencies on partial defs and undef uses.
77eb11fae6SDimitry Andric   void processDefs(MachineInstr *MI);
78eb11fae6SDimitry Andric 
79eb11fae6SDimitry Andric   /// Helps avoid false dependencies on undef registers by updating the
80eb11fae6SDimitry Andric   /// machine instructions' undef operand to use a register that the instruction
81eb11fae6SDimitry Andric   /// is truly dependent on, or use a register with clearance higher than Pref.
82eb11fae6SDimitry Andric   /// Returns true if it was able to find a true dependency, thus not requiring
83eb11fae6SDimitry Andric   /// a dependency breaking instruction regardless of clearance.
84eb11fae6SDimitry Andric   bool pickBestRegisterForUndef(MachineInstr *MI, unsigned OpIdx,
85eb11fae6SDimitry Andric     unsigned Pref);
86eb11fae6SDimitry Andric 
87eb11fae6SDimitry Andric   /// Return true to if it makes sense to break dependence on a partial
88eb11fae6SDimitry Andric   /// def or undef use.
89eb11fae6SDimitry Andric   bool shouldBreakDependence(MachineInstr *, unsigned OpIdx, unsigned Pref);
90eb11fae6SDimitry Andric 
91eb11fae6SDimitry Andric   /// Break false dependencies on undefined register reads.
92eb11fae6SDimitry Andric   /// Walk the block backward computing precise liveness. This is expensive, so
93eb11fae6SDimitry Andric   /// we only do it on demand. Note that the occurrence of undefined register
94eb11fae6SDimitry Andric   /// reads that should be broken is very rare, but when they occur we may have
95eb11fae6SDimitry Andric   /// many in a single block.
96eb11fae6SDimitry Andric   void processUndefReads(MachineBasicBlock *);
97eb11fae6SDimitry Andric };
98eb11fae6SDimitry Andric 
99eb11fae6SDimitry Andric } // namespace llvm
100eb11fae6SDimitry Andric 
101eb11fae6SDimitry Andric #define DEBUG_TYPE "break-false-deps"
102eb11fae6SDimitry Andric 
103eb11fae6SDimitry Andric char BreakFalseDeps::ID = 0;
104eb11fae6SDimitry Andric INITIALIZE_PASS_BEGIN(BreakFalseDeps, DEBUG_TYPE, "BreakFalseDeps", false, false)
INITIALIZE_PASS_DEPENDENCY(ReachingDefAnalysis)105eb11fae6SDimitry Andric INITIALIZE_PASS_DEPENDENCY(ReachingDefAnalysis)
106eb11fae6SDimitry Andric INITIALIZE_PASS_END(BreakFalseDeps, DEBUG_TYPE, "BreakFalseDeps", false, false)
107eb11fae6SDimitry Andric 
108eb11fae6SDimitry Andric FunctionPass *llvm::createBreakFalseDeps() { return new BreakFalseDeps(); }
109eb11fae6SDimitry Andric 
pickBestRegisterForUndef(MachineInstr * MI,unsigned OpIdx,unsigned Pref)110eb11fae6SDimitry Andric bool BreakFalseDeps::pickBestRegisterForUndef(MachineInstr *MI, unsigned OpIdx,
111eb11fae6SDimitry Andric   unsigned Pref) {
112cfca06d7SDimitry Andric 
113cfca06d7SDimitry Andric   // We can't change tied operands.
114cfca06d7SDimitry Andric   if (MI->isRegTiedToDefOperand(OpIdx))
115cfca06d7SDimitry Andric     return false;
116cfca06d7SDimitry Andric 
117eb11fae6SDimitry Andric   MachineOperand &MO = MI->getOperand(OpIdx);
118eb11fae6SDimitry Andric   assert(MO.isUndef() && "Expected undef machine operand");
119eb11fae6SDimitry Andric 
120cfca06d7SDimitry Andric   // We can't change registers that aren't renamable.
121cfca06d7SDimitry Andric   if (!MO.isRenamable())
122cfca06d7SDimitry Andric     return false;
123cfca06d7SDimitry Andric 
124b60736ecSDimitry Andric   MCRegister OriginalReg = MO.getReg().asMCReg();
125eb11fae6SDimitry Andric 
126eb11fae6SDimitry Andric   // Update only undef operands that have reg units that are mapped to one root.
1277fa27ce4SDimitry Andric   for (MCRegUnit Unit : TRI->regunits(OriginalReg)) {
128eb11fae6SDimitry Andric     unsigned NumRoots = 0;
1297fa27ce4SDimitry Andric     for (MCRegUnitRootIterator Root(Unit, TRI); Root.isValid(); ++Root) {
130eb11fae6SDimitry Andric       NumRoots++;
131eb11fae6SDimitry Andric       if (NumRoots > 1)
132eb11fae6SDimitry Andric         return false;
133eb11fae6SDimitry Andric     }
134eb11fae6SDimitry Andric   }
135eb11fae6SDimitry Andric 
136eb11fae6SDimitry Andric   // Get the undef operand's register class
137eb11fae6SDimitry Andric   const TargetRegisterClass *OpRC =
138eb11fae6SDimitry Andric     TII->getRegClass(MI->getDesc(), OpIdx, TRI, *MF);
139e3b55780SDimitry Andric   assert(OpRC && "Not a valid register class");
140eb11fae6SDimitry Andric 
141eb11fae6SDimitry Andric   // If the instruction has a true dependency, we can hide the false depdency
142eb11fae6SDimitry Andric   // behind it.
1437fa27ce4SDimitry Andric   for (MachineOperand &CurrMO : MI->all_uses()) {
1447fa27ce4SDimitry Andric     if (CurrMO.isUndef() || !OpRC->contains(CurrMO.getReg()))
145eb11fae6SDimitry Andric       continue;
146eb11fae6SDimitry Andric     // We found a true dependency - replace the undef register with the true
147eb11fae6SDimitry Andric     // dependency.
148eb11fae6SDimitry Andric     MO.setReg(CurrMO.getReg());
149eb11fae6SDimitry Andric     return true;
150eb11fae6SDimitry Andric   }
151eb11fae6SDimitry Andric 
152eb11fae6SDimitry Andric   // Go over all registers in the register class and find the register with
153eb11fae6SDimitry Andric   // max clearance or clearance higher than Pref.
154eb11fae6SDimitry Andric   unsigned MaxClearance = 0;
155eb11fae6SDimitry Andric   unsigned MaxClearanceReg = OriginalReg;
156eb11fae6SDimitry Andric   ArrayRef<MCPhysReg> Order = RegClassInfo.getOrder(OpRC);
157eb11fae6SDimitry Andric   for (MCPhysReg Reg : Order) {
158eb11fae6SDimitry Andric     unsigned Clearance = RDA->getClearance(MI, Reg);
159eb11fae6SDimitry Andric     if (Clearance <= MaxClearance)
160eb11fae6SDimitry Andric       continue;
161eb11fae6SDimitry Andric     MaxClearance = Clearance;
162eb11fae6SDimitry Andric     MaxClearanceReg = Reg;
163eb11fae6SDimitry Andric 
164eb11fae6SDimitry Andric     if (MaxClearance > Pref)
165eb11fae6SDimitry Andric       break;
166eb11fae6SDimitry Andric   }
167eb11fae6SDimitry Andric 
168eb11fae6SDimitry Andric   // Update the operand if we found a register with better clearance.
169eb11fae6SDimitry Andric   if (MaxClearanceReg != OriginalReg)
170eb11fae6SDimitry Andric     MO.setReg(MaxClearanceReg);
171eb11fae6SDimitry Andric 
172eb11fae6SDimitry Andric   return false;
173eb11fae6SDimitry Andric }
174eb11fae6SDimitry Andric 
shouldBreakDependence(MachineInstr * MI,unsigned OpIdx,unsigned Pref)175eb11fae6SDimitry Andric bool BreakFalseDeps::shouldBreakDependence(MachineInstr *MI, unsigned OpIdx,
176eb11fae6SDimitry Andric                                            unsigned Pref) {
177b60736ecSDimitry Andric   MCRegister Reg = MI->getOperand(OpIdx).getReg().asMCReg();
178b60736ecSDimitry Andric   unsigned Clearance = RDA->getClearance(MI, Reg);
179eb11fae6SDimitry Andric   LLVM_DEBUG(dbgs() << "Clearance: " << Clearance << ", want " << Pref);
180eb11fae6SDimitry Andric 
181eb11fae6SDimitry Andric   if (Pref > Clearance) {
182eb11fae6SDimitry Andric     LLVM_DEBUG(dbgs() << ": Break dependency.\n");
183eb11fae6SDimitry Andric     return true;
184eb11fae6SDimitry Andric   }
185eb11fae6SDimitry Andric   LLVM_DEBUG(dbgs() << ": OK .\n");
186eb11fae6SDimitry Andric   return false;
187eb11fae6SDimitry Andric }
188eb11fae6SDimitry Andric 
processDefs(MachineInstr * MI)189eb11fae6SDimitry Andric void BreakFalseDeps::processDefs(MachineInstr *MI) {
190eb11fae6SDimitry Andric   assert(!MI->isDebugInstr() && "Won't process debug values");
191eb11fae6SDimitry Andric 
192b60736ecSDimitry Andric   const MCInstrDesc &MCID = MI->getDesc();
193b60736ecSDimitry Andric 
194eb11fae6SDimitry Andric   // Break dependence on undef uses. Do this before updating LiveRegs below.
1951d5ae102SDimitry Andric   // This can remove a false dependence with no additional instructions.
196b60736ecSDimitry Andric   for (unsigned i = MCID.getNumDefs(), e = MCID.getNumOperands(); i != e; ++i) {
197b60736ecSDimitry Andric     MachineOperand &MO = MI->getOperand(i);
198b60736ecSDimitry Andric     if (!MO.isReg() || !MO.getReg() || !MO.isUse() || !MO.isUndef())
199b60736ecSDimitry Andric       continue;
200b60736ecSDimitry Andric 
201b60736ecSDimitry Andric     unsigned Pref = TII->getUndefRegClearance(*MI, i, TRI);
202eb11fae6SDimitry Andric     if (Pref) {
203b60736ecSDimitry Andric       bool HadTrueDependency = pickBestRegisterForUndef(MI, i, Pref);
204eb11fae6SDimitry Andric       // We don't need to bother trying to break a dependency if this
205eb11fae6SDimitry Andric       // instruction has a true dependency on that register through another
206eb11fae6SDimitry Andric       // operand - we'll have to wait for it to be available regardless.
207b60736ecSDimitry Andric       if (!HadTrueDependency && shouldBreakDependence(MI, i, Pref))
208b60736ecSDimitry Andric         UndefReads.push_back(std::make_pair(MI, i));
209b60736ecSDimitry Andric     }
210eb11fae6SDimitry Andric   }
211eb11fae6SDimitry Andric 
2121d5ae102SDimitry Andric   // The code below allows the target to create a new instruction to break the
2131d5ae102SDimitry Andric   // dependence. That opposes the goal of minimizing size, so bail out now.
2141d5ae102SDimitry Andric   if (MF->getFunction().hasMinSize())
2151d5ae102SDimitry Andric     return;
2161d5ae102SDimitry Andric 
217eb11fae6SDimitry Andric   for (unsigned i = 0,
218eb11fae6SDimitry Andric     e = MI->isVariadic() ? MI->getNumOperands() : MCID.getNumDefs();
219eb11fae6SDimitry Andric     i != e; ++i) {
220eb11fae6SDimitry Andric     MachineOperand &MO = MI->getOperand(i);
221eb11fae6SDimitry Andric     if (!MO.isReg() || !MO.getReg())
222eb11fae6SDimitry Andric       continue;
223eb11fae6SDimitry Andric     if (MO.isUse())
224eb11fae6SDimitry Andric       continue;
225eb11fae6SDimitry Andric     // Check clearance before partial register updates.
226eb11fae6SDimitry Andric     unsigned Pref = TII->getPartialRegUpdateClearance(*MI, i, TRI);
227eb11fae6SDimitry Andric     if (Pref && shouldBreakDependence(MI, i, Pref))
228eb11fae6SDimitry Andric       TII->breakPartialRegDependency(*MI, i, TRI);
229eb11fae6SDimitry Andric   }
230eb11fae6SDimitry Andric }
231eb11fae6SDimitry Andric 
processUndefReads(MachineBasicBlock * MBB)232eb11fae6SDimitry Andric void BreakFalseDeps::processUndefReads(MachineBasicBlock *MBB) {
233eb11fae6SDimitry Andric   if (UndefReads.empty())
234eb11fae6SDimitry Andric     return;
235eb11fae6SDimitry Andric 
2361d5ae102SDimitry Andric   // The code below allows the target to create a new instruction to break the
2371d5ae102SDimitry Andric   // dependence. That opposes the goal of minimizing size, so bail out now.
2381d5ae102SDimitry Andric   if (MF->getFunction().hasMinSize())
2391d5ae102SDimitry Andric     return;
2401d5ae102SDimitry Andric 
241eb11fae6SDimitry Andric   // Collect this block's live out register units.
242eb11fae6SDimitry Andric   LiveRegSet.init(*TRI);
243eb11fae6SDimitry Andric   // We do not need to care about pristine registers as they are just preserved
244eb11fae6SDimitry Andric   // but not actually used in the function.
245eb11fae6SDimitry Andric   LiveRegSet.addLiveOutsNoPristines(*MBB);
246eb11fae6SDimitry Andric 
247eb11fae6SDimitry Andric   MachineInstr *UndefMI = UndefReads.back().first;
248eb11fae6SDimitry Andric   unsigned OpIdx = UndefReads.back().second;
249eb11fae6SDimitry Andric 
250c0981da4SDimitry Andric   for (MachineInstr &I : llvm::reverse(*MBB)) {
251eb11fae6SDimitry Andric     // Update liveness, including the current instruction's defs.
252eb11fae6SDimitry Andric     LiveRegSet.stepBackward(I);
253eb11fae6SDimitry Andric 
254eb11fae6SDimitry Andric     if (UndefMI == &I) {
255eb11fae6SDimitry Andric       if (!LiveRegSet.contains(UndefMI->getOperand(OpIdx).getReg()))
256eb11fae6SDimitry Andric         TII->breakPartialRegDependency(*UndefMI, OpIdx, TRI);
257eb11fae6SDimitry Andric 
258eb11fae6SDimitry Andric       UndefReads.pop_back();
259eb11fae6SDimitry Andric       if (UndefReads.empty())
260eb11fae6SDimitry Andric         return;
261eb11fae6SDimitry Andric 
262eb11fae6SDimitry Andric       UndefMI = UndefReads.back().first;
263eb11fae6SDimitry Andric       OpIdx = UndefReads.back().second;
264eb11fae6SDimitry Andric     }
265eb11fae6SDimitry Andric   }
266eb11fae6SDimitry Andric }
267eb11fae6SDimitry Andric 
processBasicBlock(MachineBasicBlock * MBB)268eb11fae6SDimitry Andric void BreakFalseDeps::processBasicBlock(MachineBasicBlock *MBB) {
269eb11fae6SDimitry Andric   UndefReads.clear();
270eb11fae6SDimitry Andric   // If this block is not done, it makes little sense to make any decisions
271eb11fae6SDimitry Andric   // based on clearance information. We need to make a second pass anyway,
272eb11fae6SDimitry Andric   // and by then we'll have better information, so we can avoid doing the work
273eb11fae6SDimitry Andric   // to try and break dependencies now.
274eb11fae6SDimitry Andric   for (MachineInstr &MI : *MBB) {
275eb11fae6SDimitry Andric     if (!MI.isDebugInstr())
276eb11fae6SDimitry Andric       processDefs(&MI);
277eb11fae6SDimitry Andric   }
278eb11fae6SDimitry Andric   processUndefReads(MBB);
279eb11fae6SDimitry Andric }
280eb11fae6SDimitry Andric 
runOnMachineFunction(MachineFunction & mf)281eb11fae6SDimitry Andric bool BreakFalseDeps::runOnMachineFunction(MachineFunction &mf) {
282eb11fae6SDimitry Andric   if (skipFunction(mf.getFunction()))
283eb11fae6SDimitry Andric     return false;
284eb11fae6SDimitry Andric   MF = &mf;
285eb11fae6SDimitry Andric   TII = MF->getSubtarget().getInstrInfo();
286eb11fae6SDimitry Andric   TRI = MF->getSubtarget().getRegisterInfo();
287eb11fae6SDimitry Andric   RDA = &getAnalysis<ReachingDefAnalysis>();
288eb11fae6SDimitry Andric 
289eb11fae6SDimitry Andric   RegClassInfo.runOnMachineFunction(mf);
290eb11fae6SDimitry Andric 
291eb11fae6SDimitry Andric   LLVM_DEBUG(dbgs() << "********** BREAK FALSE DEPENDENCIES **********\n");
292eb11fae6SDimitry Andric 
2937fa27ce4SDimitry Andric   // Skip Dead blocks due to ReachingDefAnalysis has no idea about instructions
2947fa27ce4SDimitry Andric   // in them.
2957fa27ce4SDimitry Andric   df_iterator_default_set<MachineBasicBlock *> Reachable;
2967fa27ce4SDimitry Andric   for (MachineBasicBlock *MBB : depth_first_ext(&mf, Reachable))
2977fa27ce4SDimitry Andric     (void)MBB /* Mark all reachable blocks */;
2987fa27ce4SDimitry Andric 
299eb11fae6SDimitry Andric   // Traverse the basic blocks.
3007fa27ce4SDimitry Andric   for (MachineBasicBlock &MBB : mf)
3017fa27ce4SDimitry Andric     if (Reachable.count(&MBB))
302eb11fae6SDimitry Andric       processBasicBlock(&MBB);
303eb11fae6SDimitry Andric 
304eb11fae6SDimitry Andric   return false;
305eb11fae6SDimitry Andric }
306