10b57cec5SDimitry Andric //==- llvm/CodeGen/BreakFalseDeps.cpp - Break False Dependency Fix -*- C++ -*==//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric /// \file Break False Dependency pass.
100b57cec5SDimitry Andric ///
110b57cec5SDimitry Andric /// Some instructions have false dependencies which cause unnecessary stalls.
128bcb0991SDimitry Andric /// For example, instructions may write part of a register and implicitly
130b57cec5SDimitry Andric /// need to read the other parts of the register. This may cause unwanted
140b57cec5SDimitry Andric /// stalls preventing otherwise unrelated instructions from executing in
150b57cec5SDimitry Andric /// parallel in an out-of-order CPU.
168bcb0991SDimitry Andric /// This pass is aimed at identifying and avoiding these dependencies.
170b57cec5SDimitry Andric //
180b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
190b57cec5SDimitry Andric
20*06c3fb27SDimitry Andric #include "llvm/ADT/DepthFirstIterator.h"
210b57cec5SDimitry Andric #include "llvm/CodeGen/LivePhysRegs.h"
220b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h"
230b57cec5SDimitry Andric #include "llvm/CodeGen/ReachingDefAnalysis.h"
240b57cec5SDimitry Andric #include "llvm/CodeGen/RegisterClassInfo.h"
250b57cec5SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h"
26480093f4SDimitry Andric #include "llvm/InitializePasses.h"
2781ad6265SDimitry Andric #include "llvm/MC/MCInstrDesc.h"
2881ad6265SDimitry Andric #include "llvm/MC/MCRegister.h"
2981ad6265SDimitry Andric #include "llvm/MC/MCRegisterInfo.h"
308bcb0991SDimitry Andric #include "llvm/Support/Debug.h"
310b57cec5SDimitry Andric
320b57cec5SDimitry Andric using namespace llvm;
330b57cec5SDimitry Andric
340b57cec5SDimitry Andric namespace llvm {
350b57cec5SDimitry Andric
360b57cec5SDimitry Andric class BreakFalseDeps : public MachineFunctionPass {
370b57cec5SDimitry Andric private:
38*06c3fb27SDimitry Andric MachineFunction *MF = nullptr;
39*06c3fb27SDimitry Andric const TargetInstrInfo *TII = nullptr;
40*06c3fb27SDimitry Andric const TargetRegisterInfo *TRI = nullptr;
410b57cec5SDimitry Andric RegisterClassInfo RegClassInfo;
420b57cec5SDimitry Andric
430b57cec5SDimitry Andric /// List of undefined register reads in this block in forward order.
440b57cec5SDimitry Andric std::vector<std::pair<MachineInstr *, unsigned>> UndefReads;
450b57cec5SDimitry Andric
460b57cec5SDimitry Andric /// Storage for register unit liveness.
470b57cec5SDimitry Andric LivePhysRegs LiveRegSet;
480b57cec5SDimitry Andric
49*06c3fb27SDimitry Andric ReachingDefAnalysis *RDA = nullptr;
500b57cec5SDimitry Andric
510b57cec5SDimitry Andric public:
520b57cec5SDimitry Andric static char ID; // Pass identification, replacement for typeid
530b57cec5SDimitry Andric
BreakFalseDeps()540b57cec5SDimitry Andric BreakFalseDeps() : MachineFunctionPass(ID) {
550b57cec5SDimitry Andric initializeBreakFalseDepsPass(*PassRegistry::getPassRegistry());
560b57cec5SDimitry Andric }
570b57cec5SDimitry Andric
getAnalysisUsage(AnalysisUsage & AU) const580b57cec5SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override {
590b57cec5SDimitry Andric AU.setPreservesAll();
600b57cec5SDimitry Andric AU.addRequired<ReachingDefAnalysis>();
610b57cec5SDimitry Andric MachineFunctionPass::getAnalysisUsage(AU);
620b57cec5SDimitry Andric }
630b57cec5SDimitry Andric
640b57cec5SDimitry Andric bool runOnMachineFunction(MachineFunction &MF) override;
650b57cec5SDimitry Andric
getRequiredProperties() const660b57cec5SDimitry Andric MachineFunctionProperties getRequiredProperties() const override {
670b57cec5SDimitry Andric return MachineFunctionProperties().set(
680b57cec5SDimitry Andric MachineFunctionProperties::Property::NoVRegs);
690b57cec5SDimitry Andric }
700b57cec5SDimitry Andric
710b57cec5SDimitry Andric private:
720b57cec5SDimitry Andric /// Process he given basic block.
730b57cec5SDimitry Andric void processBasicBlock(MachineBasicBlock *MBB);
740b57cec5SDimitry Andric
750b57cec5SDimitry Andric /// Update def-ages for registers defined by MI.
760b57cec5SDimitry Andric /// Also break dependencies on partial defs and undef uses.
770b57cec5SDimitry Andric void processDefs(MachineInstr *MI);
780b57cec5SDimitry Andric
790b57cec5SDimitry Andric /// Helps avoid false dependencies on undef registers by updating the
800b57cec5SDimitry Andric /// machine instructions' undef operand to use a register that the instruction
810b57cec5SDimitry Andric /// is truly dependent on, or use a register with clearance higher than Pref.
820b57cec5SDimitry Andric /// Returns true if it was able to find a true dependency, thus not requiring
830b57cec5SDimitry Andric /// a dependency breaking instruction regardless of clearance.
840b57cec5SDimitry Andric bool pickBestRegisterForUndef(MachineInstr *MI, unsigned OpIdx,
850b57cec5SDimitry Andric unsigned Pref);
860b57cec5SDimitry Andric
870b57cec5SDimitry Andric /// Return true to if it makes sense to break dependence on a partial
880b57cec5SDimitry Andric /// def or undef use.
890b57cec5SDimitry Andric bool shouldBreakDependence(MachineInstr *, unsigned OpIdx, unsigned Pref);
900b57cec5SDimitry Andric
910b57cec5SDimitry Andric /// Break false dependencies on undefined register reads.
920b57cec5SDimitry Andric /// Walk the block backward computing precise liveness. This is expensive, so
930b57cec5SDimitry Andric /// we only do it on demand. Note that the occurrence of undefined register
940b57cec5SDimitry Andric /// reads that should be broken is very rare, but when they occur we may have
950b57cec5SDimitry Andric /// many in a single block.
960b57cec5SDimitry Andric void processUndefReads(MachineBasicBlock *);
970b57cec5SDimitry Andric };
980b57cec5SDimitry Andric
990b57cec5SDimitry Andric } // namespace llvm
1000b57cec5SDimitry Andric
1010b57cec5SDimitry Andric #define DEBUG_TYPE "break-false-deps"
1020b57cec5SDimitry Andric
1030b57cec5SDimitry Andric char BreakFalseDeps::ID = 0;
1040b57cec5SDimitry Andric INITIALIZE_PASS_BEGIN(BreakFalseDeps, DEBUG_TYPE, "BreakFalseDeps", false, false)
INITIALIZE_PASS_DEPENDENCY(ReachingDefAnalysis)1050b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(ReachingDefAnalysis)
1060b57cec5SDimitry Andric INITIALIZE_PASS_END(BreakFalseDeps, DEBUG_TYPE, "BreakFalseDeps", false, false)
1070b57cec5SDimitry Andric
1080b57cec5SDimitry Andric FunctionPass *llvm::createBreakFalseDeps() { return new BreakFalseDeps(); }
1090b57cec5SDimitry Andric
pickBestRegisterForUndef(MachineInstr * MI,unsigned OpIdx,unsigned Pref)1100b57cec5SDimitry Andric bool BreakFalseDeps::pickBestRegisterForUndef(MachineInstr *MI, unsigned OpIdx,
1110b57cec5SDimitry Andric unsigned Pref) {
1125ffd83dbSDimitry Andric
1135ffd83dbSDimitry Andric // We can't change tied operands.
1145ffd83dbSDimitry Andric if (MI->isRegTiedToDefOperand(OpIdx))
1155ffd83dbSDimitry Andric return false;
1165ffd83dbSDimitry Andric
1170b57cec5SDimitry Andric MachineOperand &MO = MI->getOperand(OpIdx);
1180b57cec5SDimitry Andric assert(MO.isUndef() && "Expected undef machine operand");
1190b57cec5SDimitry Andric
1205ffd83dbSDimitry Andric // We can't change registers that aren't renamable.
1215ffd83dbSDimitry Andric if (!MO.isRenamable())
1225ffd83dbSDimitry Andric return false;
1235ffd83dbSDimitry Andric
124e8d8bef9SDimitry Andric MCRegister OriginalReg = MO.getReg().asMCReg();
1250b57cec5SDimitry Andric
1260b57cec5SDimitry Andric // Update only undef operands that have reg units that are mapped to one root.
127*06c3fb27SDimitry Andric for (MCRegUnit Unit : TRI->regunits(OriginalReg)) {
1280b57cec5SDimitry Andric unsigned NumRoots = 0;
129*06c3fb27SDimitry Andric for (MCRegUnitRootIterator Root(Unit, TRI); Root.isValid(); ++Root) {
1300b57cec5SDimitry Andric NumRoots++;
1310b57cec5SDimitry Andric if (NumRoots > 1)
1320b57cec5SDimitry Andric return false;
1330b57cec5SDimitry Andric }
1340b57cec5SDimitry Andric }
1350b57cec5SDimitry Andric
1360b57cec5SDimitry Andric // Get the undef operand's register class
1370b57cec5SDimitry Andric const TargetRegisterClass *OpRC =
1380b57cec5SDimitry Andric TII->getRegClass(MI->getDesc(), OpIdx, TRI, *MF);
139bdd1243dSDimitry Andric assert(OpRC && "Not a valid register class");
1400b57cec5SDimitry Andric
1410b57cec5SDimitry Andric // If the instruction has a true dependency, we can hide the false depdency
1420b57cec5SDimitry Andric // behind it.
143*06c3fb27SDimitry Andric for (MachineOperand &CurrMO : MI->all_uses()) {
144*06c3fb27SDimitry Andric if (CurrMO.isUndef() || !OpRC->contains(CurrMO.getReg()))
1450b57cec5SDimitry Andric continue;
1460b57cec5SDimitry Andric // We found a true dependency - replace the undef register with the true
1470b57cec5SDimitry Andric // dependency.
1480b57cec5SDimitry Andric MO.setReg(CurrMO.getReg());
1490b57cec5SDimitry Andric return true;
1500b57cec5SDimitry Andric }
1510b57cec5SDimitry Andric
1520b57cec5SDimitry Andric // Go over all registers in the register class and find the register with
1530b57cec5SDimitry Andric // max clearance or clearance higher than Pref.
1540b57cec5SDimitry Andric unsigned MaxClearance = 0;
1550b57cec5SDimitry Andric unsigned MaxClearanceReg = OriginalReg;
1560b57cec5SDimitry Andric ArrayRef<MCPhysReg> Order = RegClassInfo.getOrder(OpRC);
1570b57cec5SDimitry Andric for (MCPhysReg Reg : Order) {
1580b57cec5SDimitry Andric unsigned Clearance = RDA->getClearance(MI, Reg);
1590b57cec5SDimitry Andric if (Clearance <= MaxClearance)
1600b57cec5SDimitry Andric continue;
1610b57cec5SDimitry Andric MaxClearance = Clearance;
1620b57cec5SDimitry Andric MaxClearanceReg = Reg;
1630b57cec5SDimitry Andric
1640b57cec5SDimitry Andric if (MaxClearance > Pref)
1650b57cec5SDimitry Andric break;
1660b57cec5SDimitry Andric }
1670b57cec5SDimitry Andric
1680b57cec5SDimitry Andric // Update the operand if we found a register with better clearance.
1690b57cec5SDimitry Andric if (MaxClearanceReg != OriginalReg)
1700b57cec5SDimitry Andric MO.setReg(MaxClearanceReg);
1710b57cec5SDimitry Andric
1720b57cec5SDimitry Andric return false;
1730b57cec5SDimitry Andric }
1740b57cec5SDimitry Andric
shouldBreakDependence(MachineInstr * MI,unsigned OpIdx,unsigned Pref)1750b57cec5SDimitry Andric bool BreakFalseDeps::shouldBreakDependence(MachineInstr *MI, unsigned OpIdx,
1760b57cec5SDimitry Andric unsigned Pref) {
177e8d8bef9SDimitry Andric MCRegister Reg = MI->getOperand(OpIdx).getReg().asMCReg();
178e8d8bef9SDimitry Andric unsigned Clearance = RDA->getClearance(MI, Reg);
1790b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Clearance: " << Clearance << ", want " << Pref);
1800b57cec5SDimitry Andric
1810b57cec5SDimitry Andric if (Pref > Clearance) {
1820b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << ": Break dependency.\n");
1830b57cec5SDimitry Andric return true;
1840b57cec5SDimitry Andric }
1850b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << ": OK .\n");
1860b57cec5SDimitry Andric return false;
1870b57cec5SDimitry Andric }
1880b57cec5SDimitry Andric
processDefs(MachineInstr * MI)1890b57cec5SDimitry Andric void BreakFalseDeps::processDefs(MachineInstr *MI) {
1900b57cec5SDimitry Andric assert(!MI->isDebugInstr() && "Won't process debug values");
1910b57cec5SDimitry Andric
192e8d8bef9SDimitry Andric const MCInstrDesc &MCID = MI->getDesc();
193e8d8bef9SDimitry Andric
1940b57cec5SDimitry Andric // Break dependence on undef uses. Do this before updating LiveRegs below.
1958bcb0991SDimitry Andric // This can remove a false dependence with no additional instructions.
196e8d8bef9SDimitry Andric for (unsigned i = MCID.getNumDefs(), e = MCID.getNumOperands(); i != e; ++i) {
197e8d8bef9SDimitry Andric MachineOperand &MO = MI->getOperand(i);
198e8d8bef9SDimitry Andric if (!MO.isReg() || !MO.getReg() || !MO.isUse() || !MO.isUndef())
199e8d8bef9SDimitry Andric continue;
200e8d8bef9SDimitry Andric
201e8d8bef9SDimitry Andric unsigned Pref = TII->getUndefRegClearance(*MI, i, TRI);
2020b57cec5SDimitry Andric if (Pref) {
203e8d8bef9SDimitry Andric bool HadTrueDependency = pickBestRegisterForUndef(MI, i, Pref);
2040b57cec5SDimitry Andric // We don't need to bother trying to break a dependency if this
2050b57cec5SDimitry Andric // instruction has a true dependency on that register through another
2060b57cec5SDimitry Andric // operand - we'll have to wait for it to be available regardless.
207e8d8bef9SDimitry Andric if (!HadTrueDependency && shouldBreakDependence(MI, i, Pref))
208e8d8bef9SDimitry Andric UndefReads.push_back(std::make_pair(MI, i));
209e8d8bef9SDimitry Andric }
2100b57cec5SDimitry Andric }
2110b57cec5SDimitry Andric
2128bcb0991SDimitry Andric // The code below allows the target to create a new instruction to break the
2138bcb0991SDimitry Andric // dependence. That opposes the goal of minimizing size, so bail out now.
2148bcb0991SDimitry Andric if (MF->getFunction().hasMinSize())
2158bcb0991SDimitry Andric return;
2168bcb0991SDimitry Andric
2170b57cec5SDimitry Andric for (unsigned i = 0,
2180b57cec5SDimitry Andric e = MI->isVariadic() ? MI->getNumOperands() : MCID.getNumDefs();
2190b57cec5SDimitry Andric i != e; ++i) {
2200b57cec5SDimitry Andric MachineOperand &MO = MI->getOperand(i);
2210b57cec5SDimitry Andric if (!MO.isReg() || !MO.getReg())
2220b57cec5SDimitry Andric continue;
2230b57cec5SDimitry Andric if (MO.isUse())
2240b57cec5SDimitry Andric continue;
2250b57cec5SDimitry Andric // Check clearance before partial register updates.
2260b57cec5SDimitry Andric unsigned Pref = TII->getPartialRegUpdateClearance(*MI, i, TRI);
2270b57cec5SDimitry Andric if (Pref && shouldBreakDependence(MI, i, Pref))
2280b57cec5SDimitry Andric TII->breakPartialRegDependency(*MI, i, TRI);
2290b57cec5SDimitry Andric }
2300b57cec5SDimitry Andric }
2310b57cec5SDimitry Andric
processUndefReads(MachineBasicBlock * MBB)2320b57cec5SDimitry Andric void BreakFalseDeps::processUndefReads(MachineBasicBlock *MBB) {
2330b57cec5SDimitry Andric if (UndefReads.empty())
2340b57cec5SDimitry Andric return;
2350b57cec5SDimitry Andric
2368bcb0991SDimitry Andric // The code below allows the target to create a new instruction to break the
2378bcb0991SDimitry Andric // dependence. That opposes the goal of minimizing size, so bail out now.
2388bcb0991SDimitry Andric if (MF->getFunction().hasMinSize())
2398bcb0991SDimitry Andric return;
2408bcb0991SDimitry Andric
2410b57cec5SDimitry Andric // Collect this block's live out register units.
2420b57cec5SDimitry Andric LiveRegSet.init(*TRI);
2430b57cec5SDimitry Andric // We do not need to care about pristine registers as they are just preserved
2440b57cec5SDimitry Andric // but not actually used in the function.
2450b57cec5SDimitry Andric LiveRegSet.addLiveOutsNoPristines(*MBB);
2460b57cec5SDimitry Andric
2470b57cec5SDimitry Andric MachineInstr *UndefMI = UndefReads.back().first;
2480b57cec5SDimitry Andric unsigned OpIdx = UndefReads.back().second;
2490b57cec5SDimitry Andric
250349cc55cSDimitry Andric for (MachineInstr &I : llvm::reverse(*MBB)) {
2510b57cec5SDimitry Andric // Update liveness, including the current instruction's defs.
2520b57cec5SDimitry Andric LiveRegSet.stepBackward(I);
2530b57cec5SDimitry Andric
2540b57cec5SDimitry Andric if (UndefMI == &I) {
2550b57cec5SDimitry Andric if (!LiveRegSet.contains(UndefMI->getOperand(OpIdx).getReg()))
2560b57cec5SDimitry Andric TII->breakPartialRegDependency(*UndefMI, OpIdx, TRI);
2570b57cec5SDimitry Andric
2580b57cec5SDimitry Andric UndefReads.pop_back();
2590b57cec5SDimitry Andric if (UndefReads.empty())
2600b57cec5SDimitry Andric return;
2610b57cec5SDimitry Andric
2620b57cec5SDimitry Andric UndefMI = UndefReads.back().first;
2630b57cec5SDimitry Andric OpIdx = UndefReads.back().second;
2640b57cec5SDimitry Andric }
2650b57cec5SDimitry Andric }
2660b57cec5SDimitry Andric }
2670b57cec5SDimitry Andric
processBasicBlock(MachineBasicBlock * MBB)2680b57cec5SDimitry Andric void BreakFalseDeps::processBasicBlock(MachineBasicBlock *MBB) {
2690b57cec5SDimitry Andric UndefReads.clear();
2700b57cec5SDimitry Andric // If this block is not done, it makes little sense to make any decisions
2710b57cec5SDimitry Andric // based on clearance information. We need to make a second pass anyway,
2720b57cec5SDimitry Andric // and by then we'll have better information, so we can avoid doing the work
2730b57cec5SDimitry Andric // to try and break dependencies now.
2740b57cec5SDimitry Andric for (MachineInstr &MI : *MBB) {
2750b57cec5SDimitry Andric if (!MI.isDebugInstr())
2760b57cec5SDimitry Andric processDefs(&MI);
2770b57cec5SDimitry Andric }
2780b57cec5SDimitry Andric processUndefReads(MBB);
2790b57cec5SDimitry Andric }
2800b57cec5SDimitry Andric
runOnMachineFunction(MachineFunction & mf)2810b57cec5SDimitry Andric bool BreakFalseDeps::runOnMachineFunction(MachineFunction &mf) {
2820b57cec5SDimitry Andric if (skipFunction(mf.getFunction()))
2830b57cec5SDimitry Andric return false;
2840b57cec5SDimitry Andric MF = &mf;
2850b57cec5SDimitry Andric TII = MF->getSubtarget().getInstrInfo();
2860b57cec5SDimitry Andric TRI = MF->getSubtarget().getRegisterInfo();
2870b57cec5SDimitry Andric RDA = &getAnalysis<ReachingDefAnalysis>();
2880b57cec5SDimitry Andric
2890b57cec5SDimitry Andric RegClassInfo.runOnMachineFunction(mf);
2900b57cec5SDimitry Andric
2910b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "********** BREAK FALSE DEPENDENCIES **********\n");
2920b57cec5SDimitry Andric
293*06c3fb27SDimitry Andric // Skip Dead blocks due to ReachingDefAnalysis has no idea about instructions
294*06c3fb27SDimitry Andric // in them.
295*06c3fb27SDimitry Andric df_iterator_default_set<MachineBasicBlock *> Reachable;
296*06c3fb27SDimitry Andric for (MachineBasicBlock *MBB : depth_first_ext(&mf, Reachable))
297*06c3fb27SDimitry Andric (void)MBB /* Mark all reachable blocks */;
298*06c3fb27SDimitry Andric
2990b57cec5SDimitry Andric // Traverse the basic blocks.
300*06c3fb27SDimitry Andric for (MachineBasicBlock &MBB : mf)
301*06c3fb27SDimitry Andric if (Reachable.count(&MBB))
3020b57cec5SDimitry Andric processBasicBlock(&MBB);
3030b57cec5SDimitry Andric
3040b57cec5SDimitry Andric return false;
3050b57cec5SDimitry Andric }
306