17330f729Sjoerg //==- llvm/CodeGen/BreakFalseDeps.cpp - Break False Dependency Fix -*- C++ -*==//
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg //
97330f729Sjoerg /// \file Break False Dependency pass.
107330f729Sjoerg ///
117330f729Sjoerg /// Some instructions have false dependencies which cause unnecessary stalls.
127330f729Sjoerg /// For example, instructions may write part of a register and implicitly
137330f729Sjoerg /// need to read the other parts of the register. This may cause unwanted
147330f729Sjoerg /// stalls preventing otherwise unrelated instructions from executing in
157330f729Sjoerg /// parallel in an out-of-order CPU.
167330f729Sjoerg /// This pass is aimed at identifying and avoiding these dependencies.
177330f729Sjoerg //
187330f729Sjoerg //===----------------------------------------------------------------------===//
197330f729Sjoerg
207330f729Sjoerg #include "llvm/CodeGen/LivePhysRegs.h"
217330f729Sjoerg #include "llvm/CodeGen/MachineFunctionPass.h"
22*82d56013Sjoerg #include "llvm/CodeGen/MachineRegisterInfo.h"
237330f729Sjoerg #include "llvm/CodeGen/ReachingDefAnalysis.h"
247330f729Sjoerg #include "llvm/CodeGen/RegisterClassInfo.h"
257330f729Sjoerg #include "llvm/CodeGen/TargetInstrInfo.h"
26*82d56013Sjoerg #include "llvm/InitializePasses.h"
277330f729Sjoerg #include "llvm/Support/Debug.h"
287330f729Sjoerg
297330f729Sjoerg using namespace llvm;
307330f729Sjoerg
317330f729Sjoerg namespace llvm {
327330f729Sjoerg
337330f729Sjoerg class BreakFalseDeps : public MachineFunctionPass {
347330f729Sjoerg private:
357330f729Sjoerg MachineFunction *MF;
367330f729Sjoerg const TargetInstrInfo *TII;
377330f729Sjoerg const TargetRegisterInfo *TRI;
387330f729Sjoerg RegisterClassInfo RegClassInfo;
397330f729Sjoerg
407330f729Sjoerg /// List of undefined register reads in this block in forward order.
417330f729Sjoerg std::vector<std::pair<MachineInstr *, unsigned>> UndefReads;
427330f729Sjoerg
437330f729Sjoerg /// Storage for register unit liveness.
447330f729Sjoerg LivePhysRegs LiveRegSet;
457330f729Sjoerg
467330f729Sjoerg ReachingDefAnalysis *RDA;
477330f729Sjoerg
487330f729Sjoerg public:
497330f729Sjoerg static char ID; // Pass identification, replacement for typeid
507330f729Sjoerg
BreakFalseDeps()517330f729Sjoerg BreakFalseDeps() : MachineFunctionPass(ID) {
527330f729Sjoerg initializeBreakFalseDepsPass(*PassRegistry::getPassRegistry());
537330f729Sjoerg }
547330f729Sjoerg
getAnalysisUsage(AnalysisUsage & AU) const557330f729Sjoerg void getAnalysisUsage(AnalysisUsage &AU) const override {
567330f729Sjoerg AU.setPreservesAll();
577330f729Sjoerg AU.addRequired<ReachingDefAnalysis>();
587330f729Sjoerg MachineFunctionPass::getAnalysisUsage(AU);
597330f729Sjoerg }
607330f729Sjoerg
617330f729Sjoerg bool runOnMachineFunction(MachineFunction &MF) override;
627330f729Sjoerg
getRequiredProperties() const637330f729Sjoerg MachineFunctionProperties getRequiredProperties() const override {
647330f729Sjoerg return MachineFunctionProperties().set(
657330f729Sjoerg MachineFunctionProperties::Property::NoVRegs);
667330f729Sjoerg }
677330f729Sjoerg
687330f729Sjoerg private:
697330f729Sjoerg /// Process he given basic block.
707330f729Sjoerg void processBasicBlock(MachineBasicBlock *MBB);
717330f729Sjoerg
727330f729Sjoerg /// Update def-ages for registers defined by MI.
737330f729Sjoerg /// Also break dependencies on partial defs and undef uses.
747330f729Sjoerg void processDefs(MachineInstr *MI);
757330f729Sjoerg
767330f729Sjoerg /// Helps avoid false dependencies on undef registers by updating the
777330f729Sjoerg /// machine instructions' undef operand to use a register that the instruction
787330f729Sjoerg /// is truly dependent on, or use a register with clearance higher than Pref.
797330f729Sjoerg /// Returns true if it was able to find a true dependency, thus not requiring
807330f729Sjoerg /// a dependency breaking instruction regardless of clearance.
817330f729Sjoerg bool pickBestRegisterForUndef(MachineInstr *MI, unsigned OpIdx,
827330f729Sjoerg unsigned Pref);
837330f729Sjoerg
847330f729Sjoerg /// Return true to if it makes sense to break dependence on a partial
857330f729Sjoerg /// def or undef use.
867330f729Sjoerg bool shouldBreakDependence(MachineInstr *, unsigned OpIdx, unsigned Pref);
877330f729Sjoerg
887330f729Sjoerg /// Break false dependencies on undefined register reads.
897330f729Sjoerg /// Walk the block backward computing precise liveness. This is expensive, so
907330f729Sjoerg /// we only do it on demand. Note that the occurrence of undefined register
917330f729Sjoerg /// reads that should be broken is very rare, but when they occur we may have
927330f729Sjoerg /// many in a single block.
937330f729Sjoerg void processUndefReads(MachineBasicBlock *);
947330f729Sjoerg };
957330f729Sjoerg
967330f729Sjoerg } // namespace llvm
977330f729Sjoerg
987330f729Sjoerg #define DEBUG_TYPE "break-false-deps"
997330f729Sjoerg
1007330f729Sjoerg char BreakFalseDeps::ID = 0;
1017330f729Sjoerg INITIALIZE_PASS_BEGIN(BreakFalseDeps, DEBUG_TYPE, "BreakFalseDeps", false, false)
INITIALIZE_PASS_DEPENDENCY(ReachingDefAnalysis)1027330f729Sjoerg INITIALIZE_PASS_DEPENDENCY(ReachingDefAnalysis)
1037330f729Sjoerg INITIALIZE_PASS_END(BreakFalseDeps, DEBUG_TYPE, "BreakFalseDeps", false, false)
1047330f729Sjoerg
1057330f729Sjoerg FunctionPass *llvm::createBreakFalseDeps() { return new BreakFalseDeps(); }
1067330f729Sjoerg
pickBestRegisterForUndef(MachineInstr * MI,unsigned OpIdx,unsigned Pref)1077330f729Sjoerg bool BreakFalseDeps::pickBestRegisterForUndef(MachineInstr *MI, unsigned OpIdx,
1087330f729Sjoerg unsigned Pref) {
109*82d56013Sjoerg
110*82d56013Sjoerg // We can't change tied operands.
111*82d56013Sjoerg if (MI->isRegTiedToDefOperand(OpIdx))
112*82d56013Sjoerg return false;
113*82d56013Sjoerg
1147330f729Sjoerg MachineOperand &MO = MI->getOperand(OpIdx);
1157330f729Sjoerg assert(MO.isUndef() && "Expected undef machine operand");
1167330f729Sjoerg
117*82d56013Sjoerg // We can't change registers that aren't renamable.
118*82d56013Sjoerg if (!MO.isRenamable())
119*82d56013Sjoerg return false;
120*82d56013Sjoerg
121*82d56013Sjoerg MCRegister OriginalReg = MO.getReg().asMCReg();
1227330f729Sjoerg
1237330f729Sjoerg // Update only undef operands that have reg units that are mapped to one root.
1247330f729Sjoerg for (MCRegUnitIterator Unit(OriginalReg, TRI); Unit.isValid(); ++Unit) {
1257330f729Sjoerg unsigned NumRoots = 0;
1267330f729Sjoerg for (MCRegUnitRootIterator Root(*Unit, TRI); Root.isValid(); ++Root) {
1277330f729Sjoerg NumRoots++;
1287330f729Sjoerg if (NumRoots > 1)
1297330f729Sjoerg return false;
1307330f729Sjoerg }
1317330f729Sjoerg }
1327330f729Sjoerg
1337330f729Sjoerg // Get the undef operand's register class
1347330f729Sjoerg const TargetRegisterClass *OpRC =
1357330f729Sjoerg TII->getRegClass(MI->getDesc(), OpIdx, TRI, *MF);
1367330f729Sjoerg
1377330f729Sjoerg // If the instruction has a true dependency, we can hide the false depdency
1387330f729Sjoerg // behind it.
1397330f729Sjoerg for (MachineOperand &CurrMO : MI->operands()) {
1407330f729Sjoerg if (!CurrMO.isReg() || CurrMO.isDef() || CurrMO.isUndef() ||
1417330f729Sjoerg !OpRC->contains(CurrMO.getReg()))
1427330f729Sjoerg continue;
1437330f729Sjoerg // We found a true dependency - replace the undef register with the true
1447330f729Sjoerg // dependency.
1457330f729Sjoerg MO.setReg(CurrMO.getReg());
1467330f729Sjoerg return true;
1477330f729Sjoerg }
1487330f729Sjoerg
1497330f729Sjoerg // Go over all registers in the register class and find the register with
1507330f729Sjoerg // max clearance or clearance higher than Pref.
1517330f729Sjoerg unsigned MaxClearance = 0;
1527330f729Sjoerg unsigned MaxClearanceReg = OriginalReg;
1537330f729Sjoerg ArrayRef<MCPhysReg> Order = RegClassInfo.getOrder(OpRC);
1547330f729Sjoerg for (MCPhysReg Reg : Order) {
1557330f729Sjoerg unsigned Clearance = RDA->getClearance(MI, Reg);
1567330f729Sjoerg if (Clearance <= MaxClearance)
1577330f729Sjoerg continue;
1587330f729Sjoerg MaxClearance = Clearance;
1597330f729Sjoerg MaxClearanceReg = Reg;
1607330f729Sjoerg
1617330f729Sjoerg if (MaxClearance > Pref)
1627330f729Sjoerg break;
1637330f729Sjoerg }
1647330f729Sjoerg
1657330f729Sjoerg // Update the operand if we found a register with better clearance.
1667330f729Sjoerg if (MaxClearanceReg != OriginalReg)
1677330f729Sjoerg MO.setReg(MaxClearanceReg);
1687330f729Sjoerg
1697330f729Sjoerg return false;
1707330f729Sjoerg }
1717330f729Sjoerg
shouldBreakDependence(MachineInstr * MI,unsigned OpIdx,unsigned Pref)1727330f729Sjoerg bool BreakFalseDeps::shouldBreakDependence(MachineInstr *MI, unsigned OpIdx,
1737330f729Sjoerg unsigned Pref) {
174*82d56013Sjoerg MCRegister Reg = MI->getOperand(OpIdx).getReg().asMCReg();
175*82d56013Sjoerg unsigned Clearance = RDA->getClearance(MI, Reg);
1767330f729Sjoerg LLVM_DEBUG(dbgs() << "Clearance: " << Clearance << ", want " << Pref);
1777330f729Sjoerg
1787330f729Sjoerg if (Pref > Clearance) {
1797330f729Sjoerg LLVM_DEBUG(dbgs() << ": Break dependency.\n");
1807330f729Sjoerg return true;
1817330f729Sjoerg }
1827330f729Sjoerg LLVM_DEBUG(dbgs() << ": OK .\n");
1837330f729Sjoerg return false;
1847330f729Sjoerg }
1857330f729Sjoerg
processDefs(MachineInstr * MI)1867330f729Sjoerg void BreakFalseDeps::processDefs(MachineInstr *MI) {
1877330f729Sjoerg assert(!MI->isDebugInstr() && "Won't process debug values");
1887330f729Sjoerg
189*82d56013Sjoerg const MCInstrDesc &MCID = MI->getDesc();
190*82d56013Sjoerg
1917330f729Sjoerg // Break dependence on undef uses. Do this before updating LiveRegs below.
1927330f729Sjoerg // This can remove a false dependence with no additional instructions.
193*82d56013Sjoerg for (unsigned i = MCID.getNumDefs(), e = MCID.getNumOperands(); i != e; ++i) {
194*82d56013Sjoerg MachineOperand &MO = MI->getOperand(i);
195*82d56013Sjoerg if (!MO.isReg() || !MO.getReg() || !MO.isUse() || !MO.isUndef())
196*82d56013Sjoerg continue;
197*82d56013Sjoerg
198*82d56013Sjoerg unsigned Pref = TII->getUndefRegClearance(*MI, i, TRI);
1997330f729Sjoerg if (Pref) {
200*82d56013Sjoerg bool HadTrueDependency = pickBestRegisterForUndef(MI, i, Pref);
2017330f729Sjoerg // We don't need to bother trying to break a dependency if this
2027330f729Sjoerg // instruction has a true dependency on that register through another
2037330f729Sjoerg // operand - we'll have to wait for it to be available regardless.
204*82d56013Sjoerg if (!HadTrueDependency && shouldBreakDependence(MI, i, Pref))
205*82d56013Sjoerg UndefReads.push_back(std::make_pair(MI, i));
206*82d56013Sjoerg }
2077330f729Sjoerg }
2087330f729Sjoerg
2097330f729Sjoerg // The code below allows the target to create a new instruction to break the
2107330f729Sjoerg // dependence. That opposes the goal of minimizing size, so bail out now.
2117330f729Sjoerg if (MF->getFunction().hasMinSize())
2127330f729Sjoerg return;
2137330f729Sjoerg
2147330f729Sjoerg for (unsigned i = 0,
2157330f729Sjoerg e = MI->isVariadic() ? MI->getNumOperands() : MCID.getNumDefs();
2167330f729Sjoerg i != e; ++i) {
2177330f729Sjoerg MachineOperand &MO = MI->getOperand(i);
2187330f729Sjoerg if (!MO.isReg() || !MO.getReg())
2197330f729Sjoerg continue;
2207330f729Sjoerg if (MO.isUse())
2217330f729Sjoerg continue;
2227330f729Sjoerg // Check clearance before partial register updates.
2237330f729Sjoerg unsigned Pref = TII->getPartialRegUpdateClearance(*MI, i, TRI);
2247330f729Sjoerg if (Pref && shouldBreakDependence(MI, i, Pref))
2257330f729Sjoerg TII->breakPartialRegDependency(*MI, i, TRI);
2267330f729Sjoerg }
2277330f729Sjoerg }
2287330f729Sjoerg
processUndefReads(MachineBasicBlock * MBB)2297330f729Sjoerg void BreakFalseDeps::processUndefReads(MachineBasicBlock *MBB) {
2307330f729Sjoerg if (UndefReads.empty())
2317330f729Sjoerg return;
2327330f729Sjoerg
2337330f729Sjoerg // The code below allows the target to create a new instruction to break the
2347330f729Sjoerg // dependence. That opposes the goal of minimizing size, so bail out now.
2357330f729Sjoerg if (MF->getFunction().hasMinSize())
2367330f729Sjoerg return;
2377330f729Sjoerg
2387330f729Sjoerg // Collect this block's live out register units.
2397330f729Sjoerg LiveRegSet.init(*TRI);
2407330f729Sjoerg // We do not need to care about pristine registers as they are just preserved
2417330f729Sjoerg // but not actually used in the function.
2427330f729Sjoerg LiveRegSet.addLiveOutsNoPristines(*MBB);
2437330f729Sjoerg
2447330f729Sjoerg MachineInstr *UndefMI = UndefReads.back().first;
2457330f729Sjoerg unsigned OpIdx = UndefReads.back().second;
2467330f729Sjoerg
2477330f729Sjoerg for (MachineInstr &I : make_range(MBB->rbegin(), MBB->rend())) {
2487330f729Sjoerg // Update liveness, including the current instruction's defs.
2497330f729Sjoerg LiveRegSet.stepBackward(I);
2507330f729Sjoerg
2517330f729Sjoerg if (UndefMI == &I) {
2527330f729Sjoerg if (!LiveRegSet.contains(UndefMI->getOperand(OpIdx).getReg()))
2537330f729Sjoerg TII->breakPartialRegDependency(*UndefMI, OpIdx, TRI);
2547330f729Sjoerg
2557330f729Sjoerg UndefReads.pop_back();
2567330f729Sjoerg if (UndefReads.empty())
2577330f729Sjoerg return;
2587330f729Sjoerg
2597330f729Sjoerg UndefMI = UndefReads.back().first;
2607330f729Sjoerg OpIdx = UndefReads.back().second;
2617330f729Sjoerg }
2627330f729Sjoerg }
2637330f729Sjoerg }
2647330f729Sjoerg
processBasicBlock(MachineBasicBlock * MBB)2657330f729Sjoerg void BreakFalseDeps::processBasicBlock(MachineBasicBlock *MBB) {
2667330f729Sjoerg UndefReads.clear();
2677330f729Sjoerg // If this block is not done, it makes little sense to make any decisions
2687330f729Sjoerg // based on clearance information. We need to make a second pass anyway,
2697330f729Sjoerg // and by then we'll have better information, so we can avoid doing the work
2707330f729Sjoerg // to try and break dependencies now.
2717330f729Sjoerg for (MachineInstr &MI : *MBB) {
2727330f729Sjoerg if (!MI.isDebugInstr())
2737330f729Sjoerg processDefs(&MI);
2747330f729Sjoerg }
2757330f729Sjoerg processUndefReads(MBB);
2767330f729Sjoerg }
2777330f729Sjoerg
runOnMachineFunction(MachineFunction & mf)2787330f729Sjoerg bool BreakFalseDeps::runOnMachineFunction(MachineFunction &mf) {
2797330f729Sjoerg if (skipFunction(mf.getFunction()))
2807330f729Sjoerg return false;
2817330f729Sjoerg MF = &mf;
2827330f729Sjoerg TII = MF->getSubtarget().getInstrInfo();
2837330f729Sjoerg TRI = MF->getSubtarget().getRegisterInfo();
2847330f729Sjoerg RDA = &getAnalysis<ReachingDefAnalysis>();
2857330f729Sjoerg
2867330f729Sjoerg RegClassInfo.runOnMachineFunction(mf);
2877330f729Sjoerg
2887330f729Sjoerg LLVM_DEBUG(dbgs() << "********** BREAK FALSE DEPENDENCIES **********\n");
2897330f729Sjoerg
2907330f729Sjoerg // Traverse the basic blocks.
2917330f729Sjoerg for (MachineBasicBlock &MBB : mf) {
2927330f729Sjoerg processBasicBlock(&MBB);
2937330f729Sjoerg }
2947330f729Sjoerg
2957330f729Sjoerg return false;
2967330f729Sjoerg }
297