1444ed5c5SDimitry Andric //===--- RDFDeadCode.cpp --------------------------------------------------===//
2444ed5c5SDimitry Andric //
3444ed5c5SDimitry Andric //                     The LLVM Compiler Infrastructure
4444ed5c5SDimitry Andric //
5444ed5c5SDimitry Andric // This file is distributed under the University of Illinois Open Source
6444ed5c5SDimitry Andric // License. See LICENSE.TXT for details.
7444ed5c5SDimitry Andric //
8444ed5c5SDimitry Andric //===----------------------------------------------------------------------===//
9444ed5c5SDimitry Andric //
10444ed5c5SDimitry Andric // RDF-based generic dead code elimination.
11444ed5c5SDimitry Andric 
12444ed5c5SDimitry Andric #include "RDFGraph.h"
13444ed5c5SDimitry Andric #include "RDFLiveness.h"
14444ed5c5SDimitry Andric #include "RDFDeadCode.h"
15444ed5c5SDimitry Andric 
16444ed5c5SDimitry Andric #include "llvm/ADT/SetVector.h"
17444ed5c5SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h"
18444ed5c5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
19444ed5c5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
20444ed5c5SDimitry Andric 
21444ed5c5SDimitry Andric using namespace llvm;
22444ed5c5SDimitry Andric using namespace rdf;
23444ed5c5SDimitry Andric 
24444ed5c5SDimitry Andric // Check if the given instruction has observable side-effects, i.e. if
25444ed5c5SDimitry Andric // it should be considered "live". It is safe for this function to be
26444ed5c5SDimitry Andric // overly conservative (i.e. return "true" for all instructions), but it
27444ed5c5SDimitry Andric // is not safe to return "false" for an instruction that should not be
28444ed5c5SDimitry Andric // considered removable.
29444ed5c5SDimitry Andric bool DeadCodeElimination::isLiveInstr(const MachineInstr *MI) const {
30444ed5c5SDimitry Andric   if (MI->mayStore() || MI->isBranch() || MI->isCall() || MI->isReturn())
31444ed5c5SDimitry Andric     return true;
32444ed5c5SDimitry Andric   if (MI->hasOrderedMemoryRef() || MI->hasUnmodeledSideEffects())
33444ed5c5SDimitry Andric     return true;
34444ed5c5SDimitry Andric   if (MI->isPHI())
35444ed5c5SDimitry Andric     return false;
36444ed5c5SDimitry Andric   for (auto &Op : MI->operands())
37444ed5c5SDimitry Andric     if (Op.isReg() && MRI.isReserved(Op.getReg()))
38444ed5c5SDimitry Andric       return true;
39444ed5c5SDimitry Andric   return false;
40444ed5c5SDimitry Andric }
41444ed5c5SDimitry Andric 
42444ed5c5SDimitry Andric void DeadCodeElimination::scanInstr(NodeAddr<InstrNode*> IA,
43444ed5c5SDimitry Andric       SetVector<NodeId> &WorkQ) {
44444ed5c5SDimitry Andric   if (!DFG.IsCode<NodeAttrs::Stmt>(IA))
45444ed5c5SDimitry Andric     return;
46444ed5c5SDimitry Andric   if (!isLiveInstr(NodeAddr<StmtNode*>(IA).Addr->getCode()))
47444ed5c5SDimitry Andric     return;
48444ed5c5SDimitry Andric   for (NodeAddr<RefNode*> RA : IA.Addr->members(DFG)) {
49444ed5c5SDimitry Andric     if (!LiveNodes.count(RA.Id))
50444ed5c5SDimitry Andric       WorkQ.insert(RA.Id);
51444ed5c5SDimitry Andric   }
52444ed5c5SDimitry Andric }
53444ed5c5SDimitry Andric 
54444ed5c5SDimitry Andric void DeadCodeElimination::processDef(NodeAddr<DefNode*> DA,
55444ed5c5SDimitry Andric       SetVector<NodeId> &WorkQ) {
56444ed5c5SDimitry Andric   NodeAddr<InstrNode*> IA = DA.Addr->getOwner(DFG);
57444ed5c5SDimitry Andric   for (NodeAddr<UseNode*> UA : IA.Addr->members_if(DFG.IsUse, DFG)) {
58444ed5c5SDimitry Andric     if (!LiveNodes.count(UA.Id))
59444ed5c5SDimitry Andric       WorkQ.insert(UA.Id);
60444ed5c5SDimitry Andric   }
61444ed5c5SDimitry Andric   for (NodeAddr<DefNode*> TA : DFG.getRelatedRefs(IA, DA))
62444ed5c5SDimitry Andric     LiveNodes.insert(TA.Id);
63444ed5c5SDimitry Andric }
64444ed5c5SDimitry Andric 
65444ed5c5SDimitry Andric void DeadCodeElimination::processUse(NodeAddr<UseNode*> UA,
66444ed5c5SDimitry Andric       SetVector<NodeId> &WorkQ) {
67444ed5c5SDimitry Andric   for (NodeAddr<DefNode*> DA : LV.getAllReachingDefs(UA)) {
68444ed5c5SDimitry Andric     if (!LiveNodes.count(DA.Id))
69444ed5c5SDimitry Andric       WorkQ.insert(DA.Id);
70444ed5c5SDimitry Andric   }
71444ed5c5SDimitry Andric }
72444ed5c5SDimitry Andric 
73444ed5c5SDimitry Andric // Traverse the DFG and collect the set dead RefNodes and the set of
74444ed5c5SDimitry Andric // dead instructions. Return "true" if any of these sets is non-empty,
75444ed5c5SDimitry Andric // "false" otherwise.
76444ed5c5SDimitry Andric bool DeadCodeElimination::collect() {
77444ed5c5SDimitry Andric   // This function works by first finding all live nodes. The dead nodes
78444ed5c5SDimitry Andric   // are then the complement of the set of live nodes.
79444ed5c5SDimitry Andric   //
80444ed5c5SDimitry Andric   // Assume that all nodes are dead. Identify instructions which must be
81444ed5c5SDimitry Andric   // considered live, i.e. instructions with observable side-effects, such
82444ed5c5SDimitry Andric   // as calls and stores. All arguments of such instructions are considered
83444ed5c5SDimitry Andric   // live. For each live def, all operands used in the corresponding
84444ed5c5SDimitry Andric   // instruction are considered live. For each live use, all its reaching
85444ed5c5SDimitry Andric   // defs are considered live.
86444ed5c5SDimitry Andric   LiveNodes.clear();
87444ed5c5SDimitry Andric   SetVector<NodeId> WorkQ;
88444ed5c5SDimitry Andric   for (NodeAddr<BlockNode*> BA : DFG.getFunc().Addr->members(DFG))
89444ed5c5SDimitry Andric     for (NodeAddr<InstrNode*> IA : BA.Addr->members(DFG))
90444ed5c5SDimitry Andric       scanInstr(IA, WorkQ);
91444ed5c5SDimitry Andric 
92444ed5c5SDimitry Andric   while (!WorkQ.empty()) {
93444ed5c5SDimitry Andric     NodeId N = *WorkQ.begin();
94444ed5c5SDimitry Andric     WorkQ.remove(N);
95444ed5c5SDimitry Andric     LiveNodes.insert(N);
96444ed5c5SDimitry Andric     auto RA = DFG.addr<RefNode*>(N);
97444ed5c5SDimitry Andric     if (DFG.IsDef(RA))
98444ed5c5SDimitry Andric       processDef(RA, WorkQ);
99444ed5c5SDimitry Andric     else
100444ed5c5SDimitry Andric       processUse(RA, WorkQ);
101444ed5c5SDimitry Andric   }
102444ed5c5SDimitry Andric 
103444ed5c5SDimitry Andric   if (trace()) {
104444ed5c5SDimitry Andric     dbgs() << "Live nodes:\n";
105444ed5c5SDimitry Andric     for (NodeId N : LiveNodes) {
106444ed5c5SDimitry Andric       auto RA = DFG.addr<RefNode*>(N);
107444ed5c5SDimitry Andric       dbgs() << PrintNode<RefNode*>(RA, DFG) << "\n";
108444ed5c5SDimitry Andric     }
109444ed5c5SDimitry Andric   }
110444ed5c5SDimitry Andric 
111444ed5c5SDimitry Andric   auto IsDead = [this] (NodeAddr<InstrNode*> IA) -> bool {
112444ed5c5SDimitry Andric     for (NodeAddr<DefNode*> DA : IA.Addr->members_if(DFG.IsDef, DFG))
113444ed5c5SDimitry Andric       if (LiveNodes.count(DA.Id))
114444ed5c5SDimitry Andric         return false;
115444ed5c5SDimitry Andric     return true;
116444ed5c5SDimitry Andric   };
117444ed5c5SDimitry Andric 
118444ed5c5SDimitry Andric   for (NodeAddr<BlockNode*> BA : DFG.getFunc().Addr->members(DFG)) {
119444ed5c5SDimitry Andric     for (NodeAddr<InstrNode*> IA : BA.Addr->members(DFG)) {
120444ed5c5SDimitry Andric       for (NodeAddr<RefNode*> RA : IA.Addr->members(DFG))
121444ed5c5SDimitry Andric         if (!LiveNodes.count(RA.Id))
122444ed5c5SDimitry Andric           DeadNodes.insert(RA.Id);
123444ed5c5SDimitry Andric       if (DFG.IsCode<NodeAttrs::Stmt>(IA))
124444ed5c5SDimitry Andric         if (isLiveInstr(NodeAddr<StmtNode*>(IA).Addr->getCode()))
125444ed5c5SDimitry Andric           continue;
126444ed5c5SDimitry Andric       if (IsDead(IA)) {
127444ed5c5SDimitry Andric         DeadInstrs.insert(IA.Id);
128444ed5c5SDimitry Andric         if (trace())
129444ed5c5SDimitry Andric           dbgs() << "Dead instr: " << PrintNode<InstrNode*>(IA, DFG) << "\n";
130444ed5c5SDimitry Andric       }
131444ed5c5SDimitry Andric     }
132444ed5c5SDimitry Andric   }
133444ed5c5SDimitry Andric 
134444ed5c5SDimitry Andric   return !DeadNodes.empty();
135444ed5c5SDimitry Andric }
136444ed5c5SDimitry Andric 
137444ed5c5SDimitry Andric // Erase the nodes given in the Nodes set from DFG. In addition to removing
138444ed5c5SDimitry Andric // them from the DFG, if a node corresponds to a statement, the corresponding
139444ed5c5SDimitry Andric // machine instruction is erased from the function.
140444ed5c5SDimitry Andric bool DeadCodeElimination::erase(const SetVector<NodeId> &Nodes) {
141444ed5c5SDimitry Andric   if (Nodes.empty())
142444ed5c5SDimitry Andric     return false;
143444ed5c5SDimitry Andric 
144444ed5c5SDimitry Andric   // Prepare the actual set of ref nodes to remove: ref nodes from Nodes
145444ed5c5SDimitry Andric   // are included directly, for each InstrNode in Nodes, include the set
146444ed5c5SDimitry Andric   // of all RefNodes from it.
147444ed5c5SDimitry Andric   NodeList DRNs, DINs;
148444ed5c5SDimitry Andric   for (auto I : Nodes) {
149444ed5c5SDimitry Andric     auto BA = DFG.addr<NodeBase*>(I);
150444ed5c5SDimitry Andric     uint16_t Type = BA.Addr->getType();
151444ed5c5SDimitry Andric     if (Type == NodeAttrs::Ref) {
152444ed5c5SDimitry Andric       DRNs.push_back(DFG.addr<RefNode*>(I));
153444ed5c5SDimitry Andric       continue;
154444ed5c5SDimitry Andric     }
155444ed5c5SDimitry Andric 
156444ed5c5SDimitry Andric     // If it's a code node, add all ref nodes from it.
157444ed5c5SDimitry Andric     uint16_t Kind = BA.Addr->getKind();
158444ed5c5SDimitry Andric     if (Kind == NodeAttrs::Stmt || Kind == NodeAttrs::Phi) {
159444ed5c5SDimitry Andric       for (auto N : NodeAddr<CodeNode*>(BA).Addr->members(DFG))
160444ed5c5SDimitry Andric         DRNs.push_back(N);
161444ed5c5SDimitry Andric       DINs.push_back(DFG.addr<InstrNode*>(I));
162444ed5c5SDimitry Andric     } else {
163444ed5c5SDimitry Andric       llvm_unreachable("Unexpected code node");
164444ed5c5SDimitry Andric       return false;
165444ed5c5SDimitry Andric     }
166444ed5c5SDimitry Andric   }
167444ed5c5SDimitry Andric 
168444ed5c5SDimitry Andric   // Sort the list so that use nodes are removed first. This makes the
169444ed5c5SDimitry Andric   // "unlink" functions a bit faster.
170444ed5c5SDimitry Andric   auto UsesFirst = [] (NodeAddr<RefNode*> A, NodeAddr<RefNode*> B) -> bool {
171444ed5c5SDimitry Andric     uint16_t KindA = A.Addr->getKind(), KindB = B.Addr->getKind();
172444ed5c5SDimitry Andric     if (KindA == NodeAttrs::Use && KindB == NodeAttrs::Def)
173444ed5c5SDimitry Andric       return true;
174444ed5c5SDimitry Andric     if (KindA == NodeAttrs::Def && KindB == NodeAttrs::Use)
175444ed5c5SDimitry Andric       return false;
176444ed5c5SDimitry Andric     return A.Id < B.Id;
177444ed5c5SDimitry Andric   };
178444ed5c5SDimitry Andric   std::sort(DRNs.begin(), DRNs.end(), UsesFirst);
179444ed5c5SDimitry Andric 
180444ed5c5SDimitry Andric   if (trace())
181444ed5c5SDimitry Andric     dbgs() << "Removing dead ref nodes:\n";
182444ed5c5SDimitry Andric   for (NodeAddr<RefNode*> RA : DRNs) {
183444ed5c5SDimitry Andric     if (trace())
184444ed5c5SDimitry Andric       dbgs() << "  " << PrintNode<RefNode*>(RA, DFG) << '\n';
185444ed5c5SDimitry Andric     if (DFG.IsUse(RA))
186444ed5c5SDimitry Andric       DFG.unlinkUse(RA);
187444ed5c5SDimitry Andric     else if (DFG.IsDef(RA))
188444ed5c5SDimitry Andric       DFG.unlinkDef(RA);
189444ed5c5SDimitry Andric   }
190444ed5c5SDimitry Andric 
191444ed5c5SDimitry Andric   // Now, remove all dead instruction nodes.
192444ed5c5SDimitry Andric   for (NodeAddr<InstrNode*> IA : DINs) {
193444ed5c5SDimitry Andric     NodeAddr<BlockNode*> BA = IA.Addr->getOwner(DFG);
194444ed5c5SDimitry Andric     BA.Addr->removeMember(IA, DFG);
195444ed5c5SDimitry Andric     if (!DFG.IsCode<NodeAttrs::Stmt>(IA))
196444ed5c5SDimitry Andric       continue;
197444ed5c5SDimitry Andric 
198444ed5c5SDimitry Andric     MachineInstr *MI = NodeAddr<StmtNode*>(IA).Addr->getCode();
199444ed5c5SDimitry Andric     if (trace())
200444ed5c5SDimitry Andric       dbgs() << "erasing: " << *MI;
201444ed5c5SDimitry Andric     MI->eraseFromParent();
202444ed5c5SDimitry Andric   }
203444ed5c5SDimitry Andric   return true;
204444ed5c5SDimitry Andric }
205