10b57cec5SDimitry Andric //===- RDFCopy.cpp --------------------------------------------------------===//
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 // RDF-based copy propagation.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric
130b57cec5SDimitry Andric #include "RDFCopy.h"
140b57cec5SDimitry Andric #include "llvm/CodeGen/MachineDominators.h"
150b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstr.h"
160b57cec5SDimitry Andric #include "llvm/CodeGen/MachineOperand.h"
170b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
180946e70aSDimitry Andric #include "llvm/CodeGen/RDFGraph.h"
190946e70aSDimitry Andric #include "llvm/CodeGen/RDFLiveness.h"
200946e70aSDimitry Andric #include "llvm/CodeGen/RDFRegisters.h"
210b57cec5SDimitry Andric #include "llvm/CodeGen/TargetOpcodes.h"
220b57cec5SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h"
230b57cec5SDimitry Andric #include "llvm/MC/MCRegisterInfo.h"
240b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h"
250b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
260b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
270b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
280b57cec5SDimitry Andric #include <cassert>
290b57cec5SDimitry Andric #include <cstdint>
300b57cec5SDimitry Andric #include <utility>
310b57cec5SDimitry Andric
320b57cec5SDimitry Andric using namespace llvm;
330b57cec5SDimitry Andric using namespace rdf;
340b57cec5SDimitry Andric
350b57cec5SDimitry Andric #ifndef NDEBUG
360b57cec5SDimitry Andric static cl::opt<unsigned> CpLimit("rdf-cp-limit", cl::init(0), cl::Hidden);
370b57cec5SDimitry Andric static unsigned CpCount = 0;
380b57cec5SDimitry Andric #endif
390b57cec5SDimitry Andric
interpretAsCopy(const MachineInstr * MI,EqualityMap & EM)400b57cec5SDimitry Andric bool CopyPropagation::interpretAsCopy(const MachineInstr *MI, EqualityMap &EM) {
410b57cec5SDimitry Andric unsigned Opc = MI->getOpcode();
420b57cec5SDimitry Andric switch (Opc) {
430b57cec5SDimitry Andric case TargetOpcode::COPY: {
440b57cec5SDimitry Andric const MachineOperand &Dst = MI->getOperand(0);
450b57cec5SDimitry Andric const MachineOperand &Src = MI->getOperand(1);
460b57cec5SDimitry Andric RegisterRef DstR = DFG.makeRegRef(Dst.getReg(), Dst.getSubReg());
470b57cec5SDimitry Andric RegisterRef SrcR = DFG.makeRegRef(Src.getReg(), Src.getSubReg());
488bcb0991SDimitry Andric assert(Register::isPhysicalRegister(DstR.Reg));
498bcb0991SDimitry Andric assert(Register::isPhysicalRegister(SrcR.Reg));
500b57cec5SDimitry Andric const TargetRegisterInfo &TRI = DFG.getTRI();
510b57cec5SDimitry Andric if (TRI.getMinimalPhysRegClass(DstR.Reg) !=
520b57cec5SDimitry Andric TRI.getMinimalPhysRegClass(SrcR.Reg))
530b57cec5SDimitry Andric return false;
54*06c3fb27SDimitry Andric if (!DFG.isTracked(SrcR) || !DFG.isTracked(DstR))
55*06c3fb27SDimitry Andric return false;
560b57cec5SDimitry Andric EM.insert(std::make_pair(DstR, SrcR));
570b57cec5SDimitry Andric return true;
580b57cec5SDimitry Andric }
590b57cec5SDimitry Andric case TargetOpcode::REG_SEQUENCE:
600b57cec5SDimitry Andric llvm_unreachable("Unexpected REG_SEQUENCE");
610b57cec5SDimitry Andric }
620b57cec5SDimitry Andric return false;
630b57cec5SDimitry Andric }
640b57cec5SDimitry Andric
recordCopy(NodeAddr<StmtNode * > SA,EqualityMap & EM)650b57cec5SDimitry Andric void CopyPropagation::recordCopy(NodeAddr<StmtNode*> SA, EqualityMap &EM) {
660b57cec5SDimitry Andric CopyMap.insert(std::make_pair(SA.Id, EM));
670b57cec5SDimitry Andric Copies.push_back(SA.Id);
68*06c3fb27SDimitry Andric
69*06c3fb27SDimitry Andric for (auto I : EM) {
70*06c3fb27SDimitry Andric auto FS = DefM.find(I.second.Reg);
71*06c3fb27SDimitry Andric if (FS == DefM.end() || FS->second.empty())
72*06c3fb27SDimitry Andric continue; // Undefined source
73*06c3fb27SDimitry Andric RDefMap[I.second][SA.Id] = FS->second.top()->Id;
74*06c3fb27SDimitry Andric // Insert DstR into the map.
75*06c3fb27SDimitry Andric RDefMap[I.first];
76*06c3fb27SDimitry Andric }
77*06c3fb27SDimitry Andric }
78*06c3fb27SDimitry Andric
79*06c3fb27SDimitry Andric
updateMap(NodeAddr<InstrNode * > IA)80*06c3fb27SDimitry Andric void CopyPropagation::updateMap(NodeAddr<InstrNode*> IA) {
81*06c3fb27SDimitry Andric RegisterSet RRs(DFG.getPRI());
82*06c3fb27SDimitry Andric for (NodeAddr<RefNode*> RA : IA.Addr->members(DFG))
83*06c3fb27SDimitry Andric RRs.insert(RA.Addr->getRegRef(DFG));
84*06c3fb27SDimitry Andric bool Common = false;
85*06c3fb27SDimitry Andric for (auto &R : RDefMap) {
86*06c3fb27SDimitry Andric if (!RRs.count(R.first))
87*06c3fb27SDimitry Andric continue;
88*06c3fb27SDimitry Andric Common = true;
89*06c3fb27SDimitry Andric break;
90*06c3fb27SDimitry Andric }
91*06c3fb27SDimitry Andric if (!Common)
92*06c3fb27SDimitry Andric return;
93*06c3fb27SDimitry Andric
94*06c3fb27SDimitry Andric for (auto &R : RDefMap) {
95*06c3fb27SDimitry Andric if (!RRs.count(R.first))
96*06c3fb27SDimitry Andric continue;
97*06c3fb27SDimitry Andric auto F = DefM.find(R.first.Reg);
98*06c3fb27SDimitry Andric if (F == DefM.end() || F->second.empty())
99*06c3fb27SDimitry Andric continue;
100*06c3fb27SDimitry Andric R.second[IA.Id] = F->second.top()->Id;
101*06c3fb27SDimitry Andric }
1020b57cec5SDimitry Andric }
1030b57cec5SDimitry Andric
scanBlock(MachineBasicBlock * B)1040b57cec5SDimitry Andric bool CopyPropagation::scanBlock(MachineBasicBlock *B) {
1050b57cec5SDimitry Andric bool Changed = false;
1060b57cec5SDimitry Andric NodeAddr<BlockNode*> BA = DFG.findBlock(B);
107*06c3fb27SDimitry Andric DFG.markBlock(BA.Id, DefM);
1080b57cec5SDimitry Andric
1090b57cec5SDimitry Andric for (NodeAddr<InstrNode*> IA : BA.Addr->members(DFG)) {
1100b57cec5SDimitry Andric if (DFG.IsCode<NodeAttrs::Stmt>(IA)) {
1110b57cec5SDimitry Andric NodeAddr<StmtNode*> SA = IA;
112*06c3fb27SDimitry Andric EqualityMap EM(std::less<RegisterRef>(DFG.getPRI()));
1130b57cec5SDimitry Andric if (interpretAsCopy(SA.Addr->getCode(), EM))
1140b57cec5SDimitry Andric recordCopy(SA, EM);
1150b57cec5SDimitry Andric }
116*06c3fb27SDimitry Andric
117*06c3fb27SDimitry Andric updateMap(IA);
118*06c3fb27SDimitry Andric DFG.pushAllDefs(IA, DefM);
1190b57cec5SDimitry Andric }
1200b57cec5SDimitry Andric
1210b57cec5SDimitry Andric MachineDomTreeNode *N = MDT.getNode(B);
122bdd1243dSDimitry Andric for (auto *I : *N)
1230b57cec5SDimitry Andric Changed |= scanBlock(I->getBlock());
1240b57cec5SDimitry Andric
125*06c3fb27SDimitry Andric DFG.releaseBlock(BA.Id, DefM);
1260b57cec5SDimitry Andric return Changed;
1270b57cec5SDimitry Andric }
1280b57cec5SDimitry Andric
run()1290b57cec5SDimitry Andric bool CopyPropagation::run() {
1300b57cec5SDimitry Andric scanBlock(&DFG.getMF().front());
1310b57cec5SDimitry Andric
1320b57cec5SDimitry Andric if (trace()) {
1330b57cec5SDimitry Andric dbgs() << "Copies:\n";
1340b57cec5SDimitry Andric for (NodeId I : Copies) {
1350b57cec5SDimitry Andric dbgs() << "Instr: " << *DFG.addr<StmtNode*>(I).Addr->getCode();
1360b57cec5SDimitry Andric dbgs() << " eq: {";
137*06c3fb27SDimitry Andric if (CopyMap.count(I)) {
138*06c3fb27SDimitry Andric for (auto J : CopyMap.at(I))
1390b57cec5SDimitry Andric dbgs() << ' ' << Print<RegisterRef>(J.first, DFG) << '='
1400b57cec5SDimitry Andric << Print<RegisterRef>(J.second, DFG);
141*06c3fb27SDimitry Andric }
142*06c3fb27SDimitry Andric dbgs() << " }\n";
143*06c3fb27SDimitry Andric }
144*06c3fb27SDimitry Andric dbgs() << "\nRDef map:\n";
145*06c3fb27SDimitry Andric for (auto R : RDefMap) {
146*06c3fb27SDimitry Andric dbgs() << Print<RegisterRef>(R.first, DFG) << " -> {";
147*06c3fb27SDimitry Andric for (auto &M : R.second)
148*06c3fb27SDimitry Andric dbgs() << ' ' << Print<NodeId>(M.first, DFG) << ':'
149*06c3fb27SDimitry Andric << Print<NodeId>(M.second, DFG);
1500b57cec5SDimitry Andric dbgs() << " }\n";
1510b57cec5SDimitry Andric }
1520b57cec5SDimitry Andric }
1530b57cec5SDimitry Andric
1540b57cec5SDimitry Andric bool Changed = false;
1550b57cec5SDimitry Andric #ifndef NDEBUG
1560b57cec5SDimitry Andric bool HasLimit = CpLimit.getNumOccurrences() > 0;
1570b57cec5SDimitry Andric #endif
1580b57cec5SDimitry Andric
1590b57cec5SDimitry Andric auto MinPhysReg = [this] (RegisterRef RR) -> unsigned {
1600b57cec5SDimitry Andric const TargetRegisterInfo &TRI = DFG.getTRI();
1610b57cec5SDimitry Andric const TargetRegisterClass &RC = *TRI.getMinimalPhysRegClass(RR.Reg);
1620b57cec5SDimitry Andric if ((RC.LaneMask & RR.Mask) == RC.LaneMask)
1630b57cec5SDimitry Andric return RR.Reg;
1640b57cec5SDimitry Andric for (MCSubRegIndexIterator S(RR.Reg, &TRI); S.isValid(); ++S)
1650b57cec5SDimitry Andric if (RR.Mask == TRI.getSubRegIndexLaneMask(S.getSubRegIndex()))
1660b57cec5SDimitry Andric return S.getSubReg();
1670b57cec5SDimitry Andric llvm_unreachable("Should have found a register");
1680b57cec5SDimitry Andric return 0;
1690b57cec5SDimitry Andric };
1700b57cec5SDimitry Andric
171*06c3fb27SDimitry Andric const PhysicalRegisterInfo &PRI = DFG.getPRI();
172*06c3fb27SDimitry Andric
1730b57cec5SDimitry Andric for (NodeId C : Copies) {
1740b57cec5SDimitry Andric #ifndef NDEBUG
1750b57cec5SDimitry Andric if (HasLimit && CpCount >= CpLimit)
1760b57cec5SDimitry Andric break;
1770b57cec5SDimitry Andric #endif
1780b57cec5SDimitry Andric auto SA = DFG.addr<InstrNode*>(C);
1790b57cec5SDimitry Andric auto FS = CopyMap.find(SA.Id);
1800b57cec5SDimitry Andric if (FS == CopyMap.end())
1810b57cec5SDimitry Andric continue;
1820b57cec5SDimitry Andric
1830b57cec5SDimitry Andric EqualityMap &EM = FS->second;
1840b57cec5SDimitry Andric for (NodeAddr<DefNode*> DA : SA.Addr->members_if(DFG.IsDef, DFG)) {
1850b57cec5SDimitry Andric RegisterRef DR = DA.Addr->getRegRef(DFG);
1860b57cec5SDimitry Andric auto FR = EM.find(DR);
1870b57cec5SDimitry Andric if (FR == EM.end())
1880b57cec5SDimitry Andric continue;
1890b57cec5SDimitry Andric RegisterRef SR = FR->second;
190*06c3fb27SDimitry Andric if (PRI.equal_to(DR, SR))
1910b57cec5SDimitry Andric continue;
1920b57cec5SDimitry Andric
193*06c3fb27SDimitry Andric auto &RDefSR = RDefMap[SR];
194*06c3fb27SDimitry Andric NodeId RDefSR_SA = RDefSR[SA.Id];
1950b57cec5SDimitry Andric
1960b57cec5SDimitry Andric for (NodeId N = DA.Addr->getReachedUse(), NextN; N; N = NextN) {
1970b57cec5SDimitry Andric auto UA = DFG.addr<UseNode*>(N);
1980b57cec5SDimitry Andric NextN = UA.Addr->getSibling();
1990b57cec5SDimitry Andric uint16_t F = UA.Addr->getFlags();
2000b57cec5SDimitry Andric if ((F & NodeAttrs::PhiRef) || (F & NodeAttrs::Fixed))
2010b57cec5SDimitry Andric continue;
202*06c3fb27SDimitry Andric if (!PRI.equal_to(UA.Addr->getRegRef(DFG), DR))
2030b57cec5SDimitry Andric continue;
2040b57cec5SDimitry Andric
2050b57cec5SDimitry Andric NodeAddr<InstrNode*> IA = UA.Addr->getOwner(DFG);
2060b57cec5SDimitry Andric assert(DFG.IsCode<NodeAttrs::Stmt>(IA));
207*06c3fb27SDimitry Andric if (RDefSR[IA.Id] != RDefSR_SA)
2080b57cec5SDimitry Andric continue;
2090b57cec5SDimitry Andric
2100b57cec5SDimitry Andric MachineOperand &Op = UA.Addr->getOp();
2110b57cec5SDimitry Andric if (Op.isTied())
2120b57cec5SDimitry Andric continue;
2130b57cec5SDimitry Andric if (trace()) {
2140b57cec5SDimitry Andric dbgs() << "Can replace " << Print<RegisterRef>(DR, DFG)
2150b57cec5SDimitry Andric << " with " << Print<RegisterRef>(SR, DFG) << " in "
2160b57cec5SDimitry Andric << *NodeAddr<StmtNode*>(IA).Addr->getCode();
2170b57cec5SDimitry Andric }
2180b57cec5SDimitry Andric
2190b57cec5SDimitry Andric unsigned NewReg = MinPhysReg(SR);
2200b57cec5SDimitry Andric Op.setReg(NewReg);
2210b57cec5SDimitry Andric Op.setSubReg(0);
2220b57cec5SDimitry Andric DFG.unlinkUse(UA, false);
223*06c3fb27SDimitry Andric if (RDefSR_SA != 0) {
224*06c3fb27SDimitry Andric UA.Addr->linkToDef(UA.Id, DFG.addr<DefNode*>(RDefSR_SA));
2250b57cec5SDimitry Andric } else {
2260b57cec5SDimitry Andric UA.Addr->setReachingDef(0);
2270b57cec5SDimitry Andric UA.Addr->setSibling(0);
2280b57cec5SDimitry Andric }
2290b57cec5SDimitry Andric
2300b57cec5SDimitry Andric Changed = true;
2310b57cec5SDimitry Andric #ifndef NDEBUG
2320b57cec5SDimitry Andric if (HasLimit && CpCount >= CpLimit)
2330b57cec5SDimitry Andric break;
2340b57cec5SDimitry Andric CpCount++;
2350b57cec5SDimitry Andric #endif
2360b57cec5SDimitry Andric
2370b57cec5SDimitry Andric auto FC = CopyMap.find(IA.Id);
2380b57cec5SDimitry Andric if (FC != CopyMap.end()) {
2390b57cec5SDimitry Andric // Update the EM map in the copy's entry.
2400b57cec5SDimitry Andric auto &M = FC->second;
2410b57cec5SDimitry Andric for (auto &J : M) {
242*06c3fb27SDimitry Andric if (!PRI.equal_to(J.second, DR))
2430b57cec5SDimitry Andric continue;
2440b57cec5SDimitry Andric J.second = SR;
2450b57cec5SDimitry Andric break;
2460b57cec5SDimitry Andric }
2470b57cec5SDimitry Andric }
2480b57cec5SDimitry Andric } // for (N in reached-uses)
2490b57cec5SDimitry Andric } // for (DA in defs)
2500b57cec5SDimitry Andric } // for (C in Copies)
2510b57cec5SDimitry Andric
2520b57cec5SDimitry Andric return Changed;
2530b57cec5SDimitry Andric }
254