10b57cec5SDimitry Andric //===--- RDFDeadCode.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 generic dead code elimination.
100b57cec5SDimitry Andric
110b57cec5SDimitry Andric #include "RDFDeadCode.h"
120b57cec5SDimitry Andric
130b57cec5SDimitry Andric #include "llvm/ADT/SetVector.h"
140b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h"
150b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
160b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
170946e70aSDimitry Andric #include "llvm/CodeGen/RDFGraph.h"
180946e70aSDimitry Andric #include "llvm/CodeGen/RDFLiveness.h"
198bcb0991SDimitry Andric #include "llvm/Support/Debug.h"
200b57cec5SDimitry Andric
210b57cec5SDimitry Andric #include <queue>
220b57cec5SDimitry Andric
230b57cec5SDimitry Andric using namespace llvm;
240b57cec5SDimitry Andric using namespace rdf;
250b57cec5SDimitry Andric
260b57cec5SDimitry Andric // This drastically improves execution time in "collect" over using
270b57cec5SDimitry Andric // SetVector as a work queue, and popping the first element from it.
280b57cec5SDimitry Andric template<typename T> struct DeadCodeElimination::SetQueue {
SetQueueDeadCodeElimination::SetQueue290b57cec5SDimitry Andric SetQueue() : Set(), Queue() {}
300b57cec5SDimitry Andric
emptyDeadCodeElimination::SetQueue310b57cec5SDimitry Andric bool empty() const {
320b57cec5SDimitry Andric return Queue.empty();
330b57cec5SDimitry Andric }
pop_frontDeadCodeElimination::SetQueue340b57cec5SDimitry Andric T pop_front() {
350b57cec5SDimitry Andric T V = Queue.front();
360b57cec5SDimitry Andric Queue.pop();
370b57cec5SDimitry Andric Set.erase(V);
380b57cec5SDimitry Andric return V;
390b57cec5SDimitry Andric }
push_backDeadCodeElimination::SetQueue400b57cec5SDimitry Andric void push_back(T V) {
410b57cec5SDimitry Andric if (Set.count(V))
420b57cec5SDimitry Andric return;
430b57cec5SDimitry Andric Queue.push(V);
440b57cec5SDimitry Andric Set.insert(V);
450b57cec5SDimitry Andric }
460b57cec5SDimitry Andric
470b57cec5SDimitry Andric private:
480b57cec5SDimitry Andric DenseSet<T> Set;
490b57cec5SDimitry Andric std::queue<T> Queue;
500b57cec5SDimitry Andric };
510b57cec5SDimitry Andric
520b57cec5SDimitry Andric
530b57cec5SDimitry Andric // Check if the given instruction has observable side-effects, i.e. if
540b57cec5SDimitry Andric // it should be considered "live". It is safe for this function to be
550b57cec5SDimitry Andric // overly conservative (i.e. return "true" for all instructions), but it
560b57cec5SDimitry Andric // is not safe to return "false" for an instruction that should not be
570b57cec5SDimitry Andric // considered removable.
isLiveInstr(NodeAddr<StmtNode * > S) const58*06c3fb27SDimitry Andric bool DeadCodeElimination::isLiveInstr(NodeAddr<StmtNode *> S) const {
59*06c3fb27SDimitry Andric const MachineInstr *MI = S.Addr->getCode();
600b57cec5SDimitry Andric if (MI->mayStore() || MI->isBranch() || MI->isCall() || MI->isReturn())
610b57cec5SDimitry Andric return true;
620b57cec5SDimitry Andric if (MI->hasOrderedMemoryRef() || MI->hasUnmodeledSideEffects() ||
630b57cec5SDimitry Andric MI->isPosition())
640b57cec5SDimitry Andric return true;
650b57cec5SDimitry Andric if (MI->isPHI())
660b57cec5SDimitry Andric return false;
670b57cec5SDimitry Andric for (auto &Op : MI->operands()) {
680b57cec5SDimitry Andric if (Op.isReg() && MRI.isReserved(Op.getReg()))
690b57cec5SDimitry Andric return true;
700b57cec5SDimitry Andric if (Op.isRegMask()) {
710b57cec5SDimitry Andric const uint32_t *BM = Op.getRegMask();
720b57cec5SDimitry Andric for (unsigned R = 0, RN = DFG.getTRI().getNumRegs(); R != RN; ++R) {
730b57cec5SDimitry Andric if (BM[R/32] & (1u << (R%32)))
740b57cec5SDimitry Andric continue;
750b57cec5SDimitry Andric if (MRI.isReserved(R))
760b57cec5SDimitry Andric return true;
770b57cec5SDimitry Andric }
780b57cec5SDimitry Andric }
790b57cec5SDimitry Andric }
800b57cec5SDimitry Andric return false;
810b57cec5SDimitry Andric }
820b57cec5SDimitry Andric
scanInstr(NodeAddr<InstrNode * > IA,SetQueue<NodeId> & WorkQ)830b57cec5SDimitry Andric void DeadCodeElimination::scanInstr(NodeAddr<InstrNode*> IA,
840b57cec5SDimitry Andric SetQueue<NodeId> &WorkQ) {
850b57cec5SDimitry Andric if (!DFG.IsCode<NodeAttrs::Stmt>(IA))
860b57cec5SDimitry Andric return;
87*06c3fb27SDimitry Andric if (!isLiveInstr(IA))
880b57cec5SDimitry Andric return;
890b57cec5SDimitry Andric for (NodeAddr<RefNode*> RA : IA.Addr->members(DFG)) {
900b57cec5SDimitry Andric if (!LiveNodes.count(RA.Id))
910b57cec5SDimitry Andric WorkQ.push_back(RA.Id);
920b57cec5SDimitry Andric }
930b57cec5SDimitry Andric }
940b57cec5SDimitry Andric
processDef(NodeAddr<DefNode * > DA,SetQueue<NodeId> & WorkQ)950b57cec5SDimitry Andric void DeadCodeElimination::processDef(NodeAddr<DefNode*> DA,
960b57cec5SDimitry Andric SetQueue<NodeId> &WorkQ) {
970b57cec5SDimitry Andric NodeAddr<InstrNode*> IA = DA.Addr->getOwner(DFG);
980b57cec5SDimitry Andric for (NodeAddr<UseNode*> UA : IA.Addr->members_if(DFG.IsUse, DFG)) {
990b57cec5SDimitry Andric if (!LiveNodes.count(UA.Id))
1000b57cec5SDimitry Andric WorkQ.push_back(UA.Id);
1010b57cec5SDimitry Andric }
1020b57cec5SDimitry Andric for (NodeAddr<DefNode*> TA : DFG.getRelatedRefs(IA, DA))
1030b57cec5SDimitry Andric LiveNodes.insert(TA.Id);
1040b57cec5SDimitry Andric }
1050b57cec5SDimitry Andric
processUse(NodeAddr<UseNode * > UA,SetQueue<NodeId> & WorkQ)1060b57cec5SDimitry Andric void DeadCodeElimination::processUse(NodeAddr<UseNode*> UA,
1070b57cec5SDimitry Andric SetQueue<NodeId> &WorkQ) {
1080b57cec5SDimitry Andric for (NodeAddr<DefNode*> DA : LV.getAllReachingDefs(UA)) {
1090b57cec5SDimitry Andric if (!LiveNodes.count(DA.Id))
1100b57cec5SDimitry Andric WorkQ.push_back(DA.Id);
1110b57cec5SDimitry Andric }
1120b57cec5SDimitry Andric }
1130b57cec5SDimitry Andric
1140b57cec5SDimitry Andric // Traverse the DFG and collect the set dead RefNodes and the set of
1150b57cec5SDimitry Andric // dead instructions. Return "true" if any of these sets is non-empty,
1160b57cec5SDimitry Andric // "false" otherwise.
collect()1170b57cec5SDimitry Andric bool DeadCodeElimination::collect() {
1180b57cec5SDimitry Andric // This function works by first finding all live nodes. The dead nodes
1190b57cec5SDimitry Andric // are then the complement of the set of live nodes.
1200b57cec5SDimitry Andric //
1210b57cec5SDimitry Andric // Assume that all nodes are dead. Identify instructions which must be
1220b57cec5SDimitry Andric // considered live, i.e. instructions with observable side-effects, such
1230b57cec5SDimitry Andric // as calls and stores. All arguments of such instructions are considered
1240b57cec5SDimitry Andric // live. For each live def, all operands used in the corresponding
1250b57cec5SDimitry Andric // instruction are considered live. For each live use, all its reaching
1260b57cec5SDimitry Andric // defs are considered live.
1270b57cec5SDimitry Andric LiveNodes.clear();
1280b57cec5SDimitry Andric SetQueue<NodeId> WorkQ;
1290b57cec5SDimitry Andric for (NodeAddr<BlockNode*> BA : DFG.getFunc().Addr->members(DFG))
1300b57cec5SDimitry Andric for (NodeAddr<InstrNode*> IA : BA.Addr->members(DFG))
1310b57cec5SDimitry Andric scanInstr(IA, WorkQ);
1320b57cec5SDimitry Andric
1330b57cec5SDimitry Andric while (!WorkQ.empty()) {
1340b57cec5SDimitry Andric NodeId N = WorkQ.pop_front();
1350b57cec5SDimitry Andric LiveNodes.insert(N);
1360b57cec5SDimitry Andric auto RA = DFG.addr<RefNode*>(N);
1370b57cec5SDimitry Andric if (DFG.IsDef(RA))
1380b57cec5SDimitry Andric processDef(RA, WorkQ);
1390b57cec5SDimitry Andric else
1400b57cec5SDimitry Andric processUse(RA, WorkQ);
1410b57cec5SDimitry Andric }
1420b57cec5SDimitry Andric
1430b57cec5SDimitry Andric if (trace()) {
1440b57cec5SDimitry Andric dbgs() << "Live nodes:\n";
1450b57cec5SDimitry Andric for (NodeId N : LiveNodes) {
1460b57cec5SDimitry Andric auto RA = DFG.addr<RefNode*>(N);
1470b57cec5SDimitry Andric dbgs() << PrintNode<RefNode*>(RA, DFG) << "\n";
1480b57cec5SDimitry Andric }
1490b57cec5SDimitry Andric }
1500b57cec5SDimitry Andric
1510b57cec5SDimitry Andric auto IsDead = [this] (NodeAddr<InstrNode*> IA) -> bool {
1520b57cec5SDimitry Andric for (NodeAddr<DefNode*> DA : IA.Addr->members_if(DFG.IsDef, DFG))
1530b57cec5SDimitry Andric if (LiveNodes.count(DA.Id))
1540b57cec5SDimitry Andric return false;
1550b57cec5SDimitry Andric return true;
1560b57cec5SDimitry Andric };
1570b57cec5SDimitry Andric
1580b57cec5SDimitry Andric for (NodeAddr<BlockNode*> BA : DFG.getFunc().Addr->members(DFG)) {
1590b57cec5SDimitry Andric for (NodeAddr<InstrNode*> IA : BA.Addr->members(DFG)) {
1600b57cec5SDimitry Andric for (NodeAddr<RefNode*> RA : IA.Addr->members(DFG))
1610b57cec5SDimitry Andric if (!LiveNodes.count(RA.Id))
1620b57cec5SDimitry Andric DeadNodes.insert(RA.Id);
1630b57cec5SDimitry Andric if (DFG.IsCode<NodeAttrs::Stmt>(IA))
164*06c3fb27SDimitry Andric if (isLiveInstr(IA) || DFG.hasUntrackedRef(IA))
1650b57cec5SDimitry Andric continue;
1660b57cec5SDimitry Andric if (IsDead(IA)) {
1670b57cec5SDimitry Andric DeadInstrs.insert(IA.Id);
1680b57cec5SDimitry Andric if (trace())
1690b57cec5SDimitry Andric dbgs() << "Dead instr: " << PrintNode<InstrNode*>(IA, DFG) << "\n";
1700b57cec5SDimitry Andric }
1710b57cec5SDimitry Andric }
1720b57cec5SDimitry Andric }
1730b57cec5SDimitry Andric
1740b57cec5SDimitry Andric return !DeadNodes.empty();
1750b57cec5SDimitry Andric }
1760b57cec5SDimitry Andric
1770b57cec5SDimitry Andric // Erase the nodes given in the Nodes set from DFG. In addition to removing
1780b57cec5SDimitry Andric // them from the DFG, if a node corresponds to a statement, the corresponding
1790b57cec5SDimitry Andric // machine instruction is erased from the function.
erase(const SetVector<NodeId> & Nodes)1800b57cec5SDimitry Andric bool DeadCodeElimination::erase(const SetVector<NodeId> &Nodes) {
1810b57cec5SDimitry Andric if (Nodes.empty())
1820b57cec5SDimitry Andric return false;
1830b57cec5SDimitry Andric
1840b57cec5SDimitry Andric // Prepare the actual set of ref nodes to remove: ref nodes from Nodes
1850b57cec5SDimitry Andric // are included directly, for each InstrNode in Nodes, include the set
1860b57cec5SDimitry Andric // of all RefNodes from it.
1870b57cec5SDimitry Andric NodeList DRNs, DINs;
1880b57cec5SDimitry Andric for (auto I : Nodes) {
1890b57cec5SDimitry Andric auto BA = DFG.addr<NodeBase*>(I);
1900b57cec5SDimitry Andric uint16_t Type = BA.Addr->getType();
1910b57cec5SDimitry Andric if (Type == NodeAttrs::Ref) {
1920b57cec5SDimitry Andric DRNs.push_back(DFG.addr<RefNode*>(I));
1930b57cec5SDimitry Andric continue;
1940b57cec5SDimitry Andric }
1950b57cec5SDimitry Andric
1960b57cec5SDimitry Andric // If it's a code node, add all ref nodes from it.
1970b57cec5SDimitry Andric uint16_t Kind = BA.Addr->getKind();
1980b57cec5SDimitry Andric if (Kind == NodeAttrs::Stmt || Kind == NodeAttrs::Phi) {
199e8d8bef9SDimitry Andric append_range(DRNs, NodeAddr<CodeNode*>(BA).Addr->members(DFG));
2000b57cec5SDimitry Andric DINs.push_back(DFG.addr<InstrNode*>(I));
2010b57cec5SDimitry Andric } else {
2020b57cec5SDimitry Andric llvm_unreachable("Unexpected code node");
2030b57cec5SDimitry Andric return false;
2040b57cec5SDimitry Andric }
2050b57cec5SDimitry Andric }
2060b57cec5SDimitry Andric
2070b57cec5SDimitry Andric // Sort the list so that use nodes are removed first. This makes the
2080b57cec5SDimitry Andric // "unlink" functions a bit faster.
2090b57cec5SDimitry Andric auto UsesFirst = [] (NodeAddr<RefNode*> A, NodeAddr<RefNode*> B) -> bool {
2100b57cec5SDimitry Andric uint16_t KindA = A.Addr->getKind(), KindB = B.Addr->getKind();
2110b57cec5SDimitry Andric if (KindA == NodeAttrs::Use && KindB == NodeAttrs::Def)
2120b57cec5SDimitry Andric return true;
2130b57cec5SDimitry Andric if (KindA == NodeAttrs::Def && KindB == NodeAttrs::Use)
2140b57cec5SDimitry Andric return false;
2150b57cec5SDimitry Andric return A.Id < B.Id;
2160b57cec5SDimitry Andric };
2170b57cec5SDimitry Andric llvm::sort(DRNs, UsesFirst);
2180b57cec5SDimitry Andric
2190b57cec5SDimitry Andric if (trace())
2200b57cec5SDimitry Andric dbgs() << "Removing dead ref nodes:\n";
2210b57cec5SDimitry Andric for (NodeAddr<RefNode*> RA : DRNs) {
2220b57cec5SDimitry Andric if (trace())
2230b57cec5SDimitry Andric dbgs() << " " << PrintNode<RefNode*>(RA, DFG) << '\n';
2240b57cec5SDimitry Andric if (DFG.IsUse(RA))
2250b57cec5SDimitry Andric DFG.unlinkUse(RA, true);
2260b57cec5SDimitry Andric else if (DFG.IsDef(RA))
2270b57cec5SDimitry Andric DFG.unlinkDef(RA, true);
2280b57cec5SDimitry Andric }
2290b57cec5SDimitry Andric
2300b57cec5SDimitry Andric // Now, remove all dead instruction nodes.
2310b57cec5SDimitry Andric for (NodeAddr<InstrNode*> IA : DINs) {
2320b57cec5SDimitry Andric NodeAddr<BlockNode*> BA = IA.Addr->getOwner(DFG);
2330b57cec5SDimitry Andric BA.Addr->removeMember(IA, DFG);
2340b57cec5SDimitry Andric if (!DFG.IsCode<NodeAttrs::Stmt>(IA))
2350b57cec5SDimitry Andric continue;
2360b57cec5SDimitry Andric
2370b57cec5SDimitry Andric MachineInstr *MI = NodeAddr<StmtNode*>(IA).Addr->getCode();
2380b57cec5SDimitry Andric if (trace())
2390b57cec5SDimitry Andric dbgs() << "erasing: " << *MI;
2400b57cec5SDimitry Andric MI->eraseFromParent();
2410b57cec5SDimitry Andric }
2420b57cec5SDimitry Andric return true;
2430b57cec5SDimitry Andric }
244