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 
213ca95b02SDimitry Andric #include <queue>
223ca95b02SDimitry Andric 
23444ed5c5SDimitry Andric using namespace llvm;
24444ed5c5SDimitry Andric using namespace rdf;
25444ed5c5SDimitry Andric 
263ca95b02SDimitry Andric // This drastically improves execution time in "collect" over using
273ca95b02SDimitry Andric // SetVector as a work queue, and popping the first element from it.
283ca95b02SDimitry Andric template<typename T> struct DeadCodeElimination::SetQueue {
293ca95b02SDimitry Andric   SetQueue() : Set(), Queue() {}
303ca95b02SDimitry Andric 
313ca95b02SDimitry Andric   bool empty() const {
323ca95b02SDimitry Andric     return Queue.empty();
333ca95b02SDimitry Andric   }
343ca95b02SDimitry Andric   T pop_front() {
353ca95b02SDimitry Andric     T V = Queue.front();
363ca95b02SDimitry Andric     Queue.pop();
373ca95b02SDimitry Andric     Set.erase(V);
383ca95b02SDimitry Andric     return V;
393ca95b02SDimitry Andric   }
403ca95b02SDimitry Andric   void push_back(T V) {
413ca95b02SDimitry Andric     if (Set.count(V))
423ca95b02SDimitry Andric       return;
433ca95b02SDimitry Andric     Queue.push(V);
443ca95b02SDimitry Andric     Set.insert(V);
453ca95b02SDimitry Andric   }
463ca95b02SDimitry Andric 
473ca95b02SDimitry Andric private:
483ca95b02SDimitry Andric   DenseSet<T> Set;
493ca95b02SDimitry Andric   std::queue<T> Queue;
503ca95b02SDimitry Andric };
513ca95b02SDimitry Andric 
523ca95b02SDimitry Andric 
53444ed5c5SDimitry Andric // Check if the given instruction has observable side-effects, i.e. if
54444ed5c5SDimitry Andric // it should be considered "live". It is safe for this function to be
55444ed5c5SDimitry Andric // overly conservative (i.e. return "true" for all instructions), but it
56444ed5c5SDimitry Andric // is not safe to return "false" for an instruction that should not be
57444ed5c5SDimitry Andric // considered removable.
58444ed5c5SDimitry Andric bool DeadCodeElimination::isLiveInstr(const MachineInstr *MI) const {
59444ed5c5SDimitry Andric   if (MI->mayStore() || MI->isBranch() || MI->isCall() || MI->isReturn())
60444ed5c5SDimitry Andric     return true;
61444ed5c5SDimitry Andric   if (MI->hasOrderedMemoryRef() || MI->hasUnmodeledSideEffects())
62444ed5c5SDimitry Andric     return true;
63444ed5c5SDimitry Andric   if (MI->isPHI())
64444ed5c5SDimitry Andric     return false;
657a7e6055SDimitry Andric   for (auto &Op : MI->operands()) {
66444ed5c5SDimitry Andric     if (Op.isReg() && MRI.isReserved(Op.getReg()))
67444ed5c5SDimitry Andric       return true;
687a7e6055SDimitry Andric     if (Op.isRegMask()) {
697a7e6055SDimitry Andric       const uint32_t *BM = Op.getRegMask();
707a7e6055SDimitry Andric       for (unsigned R = 0, RN = DFG.getTRI().getNumRegs(); R != RN; ++R) {
717a7e6055SDimitry Andric         if (BM[R/32] & (1u << (R%32)))
727a7e6055SDimitry Andric           continue;
737a7e6055SDimitry Andric         if (MRI.isReserved(R))
747a7e6055SDimitry Andric           return true;
757a7e6055SDimitry Andric       }
767a7e6055SDimitry Andric     }
777a7e6055SDimitry Andric   }
78444ed5c5SDimitry Andric   return false;
79444ed5c5SDimitry Andric }
80444ed5c5SDimitry Andric 
81444ed5c5SDimitry Andric void DeadCodeElimination::scanInstr(NodeAddr<InstrNode*> IA,
823ca95b02SDimitry Andric       SetQueue<NodeId> &WorkQ) {
83444ed5c5SDimitry Andric   if (!DFG.IsCode<NodeAttrs::Stmt>(IA))
84444ed5c5SDimitry Andric     return;
85444ed5c5SDimitry Andric   if (!isLiveInstr(NodeAddr<StmtNode*>(IA).Addr->getCode()))
86444ed5c5SDimitry Andric     return;
87444ed5c5SDimitry Andric   for (NodeAddr<RefNode*> RA : IA.Addr->members(DFG)) {
88444ed5c5SDimitry Andric     if (!LiveNodes.count(RA.Id))
893ca95b02SDimitry Andric       WorkQ.push_back(RA.Id);
90444ed5c5SDimitry Andric   }
91444ed5c5SDimitry Andric }
92444ed5c5SDimitry Andric 
93444ed5c5SDimitry Andric void DeadCodeElimination::processDef(NodeAddr<DefNode*> DA,
943ca95b02SDimitry Andric       SetQueue<NodeId> &WorkQ) {
95444ed5c5SDimitry Andric   NodeAddr<InstrNode*> IA = DA.Addr->getOwner(DFG);
96444ed5c5SDimitry Andric   for (NodeAddr<UseNode*> UA : IA.Addr->members_if(DFG.IsUse, DFG)) {
97444ed5c5SDimitry Andric     if (!LiveNodes.count(UA.Id))
983ca95b02SDimitry Andric       WorkQ.push_back(UA.Id);
99444ed5c5SDimitry Andric   }
100444ed5c5SDimitry Andric   for (NodeAddr<DefNode*> TA : DFG.getRelatedRefs(IA, DA))
101444ed5c5SDimitry Andric     LiveNodes.insert(TA.Id);
102444ed5c5SDimitry Andric }
103444ed5c5SDimitry Andric 
104444ed5c5SDimitry Andric void DeadCodeElimination::processUse(NodeAddr<UseNode*> UA,
1053ca95b02SDimitry Andric       SetQueue<NodeId> &WorkQ) {
106444ed5c5SDimitry Andric   for (NodeAddr<DefNode*> DA : LV.getAllReachingDefs(UA)) {
107444ed5c5SDimitry Andric     if (!LiveNodes.count(DA.Id))
1083ca95b02SDimitry Andric       WorkQ.push_back(DA.Id);
109444ed5c5SDimitry Andric   }
110444ed5c5SDimitry Andric }
111444ed5c5SDimitry Andric 
112444ed5c5SDimitry Andric // Traverse the DFG and collect the set dead RefNodes and the set of
113444ed5c5SDimitry Andric // dead instructions. Return "true" if any of these sets is non-empty,
114444ed5c5SDimitry Andric // "false" otherwise.
115444ed5c5SDimitry Andric bool DeadCodeElimination::collect() {
116444ed5c5SDimitry Andric   // This function works by first finding all live nodes. The dead nodes
117444ed5c5SDimitry Andric   // are then the complement of the set of live nodes.
118444ed5c5SDimitry Andric   //
119444ed5c5SDimitry Andric   // Assume that all nodes are dead. Identify instructions which must be
120444ed5c5SDimitry Andric   // considered live, i.e. instructions with observable side-effects, such
121444ed5c5SDimitry Andric   // as calls and stores. All arguments of such instructions are considered
122444ed5c5SDimitry Andric   // live. For each live def, all operands used in the corresponding
123444ed5c5SDimitry Andric   // instruction are considered live. For each live use, all its reaching
124444ed5c5SDimitry Andric   // defs are considered live.
125444ed5c5SDimitry Andric   LiveNodes.clear();
1263ca95b02SDimitry Andric   SetQueue<NodeId> WorkQ;
127444ed5c5SDimitry Andric   for (NodeAddr<BlockNode*> BA : DFG.getFunc().Addr->members(DFG))
128444ed5c5SDimitry Andric     for (NodeAddr<InstrNode*> IA : BA.Addr->members(DFG))
129444ed5c5SDimitry Andric       scanInstr(IA, WorkQ);
130444ed5c5SDimitry Andric 
131444ed5c5SDimitry Andric   while (!WorkQ.empty()) {
1323ca95b02SDimitry Andric     NodeId N = WorkQ.pop_front();
133444ed5c5SDimitry Andric     LiveNodes.insert(N);
134444ed5c5SDimitry Andric     auto RA = DFG.addr<RefNode*>(N);
135444ed5c5SDimitry Andric     if (DFG.IsDef(RA))
136444ed5c5SDimitry Andric       processDef(RA, WorkQ);
137444ed5c5SDimitry Andric     else
138444ed5c5SDimitry Andric       processUse(RA, WorkQ);
139444ed5c5SDimitry Andric   }
140444ed5c5SDimitry Andric 
141444ed5c5SDimitry Andric   if (trace()) {
142444ed5c5SDimitry Andric     dbgs() << "Live nodes:\n";
143444ed5c5SDimitry Andric     for (NodeId N : LiveNodes) {
144444ed5c5SDimitry Andric       auto RA = DFG.addr<RefNode*>(N);
145444ed5c5SDimitry Andric       dbgs() << PrintNode<RefNode*>(RA, DFG) << "\n";
146444ed5c5SDimitry Andric     }
147444ed5c5SDimitry Andric   }
148444ed5c5SDimitry Andric 
149444ed5c5SDimitry Andric   auto IsDead = [this] (NodeAddr<InstrNode*> IA) -> bool {
150444ed5c5SDimitry Andric     for (NodeAddr<DefNode*> DA : IA.Addr->members_if(DFG.IsDef, DFG))
151444ed5c5SDimitry Andric       if (LiveNodes.count(DA.Id))
152444ed5c5SDimitry Andric         return false;
153444ed5c5SDimitry Andric     return true;
154444ed5c5SDimitry Andric   };
155444ed5c5SDimitry Andric 
156444ed5c5SDimitry Andric   for (NodeAddr<BlockNode*> BA : DFG.getFunc().Addr->members(DFG)) {
157444ed5c5SDimitry Andric     for (NodeAddr<InstrNode*> IA : BA.Addr->members(DFG)) {
158444ed5c5SDimitry Andric       for (NodeAddr<RefNode*> RA : IA.Addr->members(DFG))
159444ed5c5SDimitry Andric         if (!LiveNodes.count(RA.Id))
160444ed5c5SDimitry Andric           DeadNodes.insert(RA.Id);
161444ed5c5SDimitry Andric       if (DFG.IsCode<NodeAttrs::Stmt>(IA))
162444ed5c5SDimitry Andric         if (isLiveInstr(NodeAddr<StmtNode*>(IA).Addr->getCode()))
163444ed5c5SDimitry Andric           continue;
164444ed5c5SDimitry Andric       if (IsDead(IA)) {
165444ed5c5SDimitry Andric         DeadInstrs.insert(IA.Id);
166444ed5c5SDimitry Andric         if (trace())
167444ed5c5SDimitry Andric           dbgs() << "Dead instr: " << PrintNode<InstrNode*>(IA, DFG) << "\n";
168444ed5c5SDimitry Andric       }
169444ed5c5SDimitry Andric     }
170444ed5c5SDimitry Andric   }
171444ed5c5SDimitry Andric 
172444ed5c5SDimitry Andric   return !DeadNodes.empty();
173444ed5c5SDimitry Andric }
174444ed5c5SDimitry Andric 
175444ed5c5SDimitry Andric // Erase the nodes given in the Nodes set from DFG. In addition to removing
176444ed5c5SDimitry Andric // them from the DFG, if a node corresponds to a statement, the corresponding
177444ed5c5SDimitry Andric // machine instruction is erased from the function.
178444ed5c5SDimitry Andric bool DeadCodeElimination::erase(const SetVector<NodeId> &Nodes) {
179444ed5c5SDimitry Andric   if (Nodes.empty())
180444ed5c5SDimitry Andric     return false;
181444ed5c5SDimitry Andric 
182444ed5c5SDimitry Andric   // Prepare the actual set of ref nodes to remove: ref nodes from Nodes
183444ed5c5SDimitry Andric   // are included directly, for each InstrNode in Nodes, include the set
184444ed5c5SDimitry Andric   // of all RefNodes from it.
185444ed5c5SDimitry Andric   NodeList DRNs, DINs;
186444ed5c5SDimitry Andric   for (auto I : Nodes) {
187444ed5c5SDimitry Andric     auto BA = DFG.addr<NodeBase*>(I);
188444ed5c5SDimitry Andric     uint16_t Type = BA.Addr->getType();
189444ed5c5SDimitry Andric     if (Type == NodeAttrs::Ref) {
190444ed5c5SDimitry Andric       DRNs.push_back(DFG.addr<RefNode*>(I));
191444ed5c5SDimitry Andric       continue;
192444ed5c5SDimitry Andric     }
193444ed5c5SDimitry Andric 
194444ed5c5SDimitry Andric     // If it's a code node, add all ref nodes from it.
195444ed5c5SDimitry Andric     uint16_t Kind = BA.Addr->getKind();
196444ed5c5SDimitry Andric     if (Kind == NodeAttrs::Stmt || Kind == NodeAttrs::Phi) {
197444ed5c5SDimitry Andric       for (auto N : NodeAddr<CodeNode*>(BA).Addr->members(DFG))
198444ed5c5SDimitry Andric         DRNs.push_back(N);
199444ed5c5SDimitry Andric       DINs.push_back(DFG.addr<InstrNode*>(I));
200444ed5c5SDimitry Andric     } else {
201444ed5c5SDimitry Andric       llvm_unreachable("Unexpected code node");
202444ed5c5SDimitry Andric       return false;
203444ed5c5SDimitry Andric     }
204444ed5c5SDimitry Andric   }
205444ed5c5SDimitry Andric 
206444ed5c5SDimitry Andric   // Sort the list so that use nodes are removed first. This makes the
207444ed5c5SDimitry Andric   // "unlink" functions a bit faster.
208444ed5c5SDimitry Andric   auto UsesFirst = [] (NodeAddr<RefNode*> A, NodeAddr<RefNode*> B) -> bool {
209444ed5c5SDimitry Andric     uint16_t KindA = A.Addr->getKind(), KindB = B.Addr->getKind();
210444ed5c5SDimitry Andric     if (KindA == NodeAttrs::Use && KindB == NodeAttrs::Def)
211444ed5c5SDimitry Andric       return true;
212444ed5c5SDimitry Andric     if (KindA == NodeAttrs::Def && KindB == NodeAttrs::Use)
213444ed5c5SDimitry Andric       return false;
214444ed5c5SDimitry Andric     return A.Id < B.Id;
215444ed5c5SDimitry Andric   };
216444ed5c5SDimitry Andric   std::sort(DRNs.begin(), DRNs.end(), UsesFirst);
217444ed5c5SDimitry Andric 
218444ed5c5SDimitry Andric   if (trace())
219444ed5c5SDimitry Andric     dbgs() << "Removing dead ref nodes:\n";
220444ed5c5SDimitry Andric   for (NodeAddr<RefNode*> RA : DRNs) {
221444ed5c5SDimitry Andric     if (trace())
222444ed5c5SDimitry Andric       dbgs() << "  " << PrintNode<RefNode*>(RA, DFG) << '\n';
223444ed5c5SDimitry Andric     if (DFG.IsUse(RA))
2243ca95b02SDimitry Andric       DFG.unlinkUse(RA, true);
225444ed5c5SDimitry Andric     else if (DFG.IsDef(RA))
2263ca95b02SDimitry Andric       DFG.unlinkDef(RA, true);
227444ed5c5SDimitry Andric   }
228444ed5c5SDimitry Andric 
229444ed5c5SDimitry Andric   // Now, remove all dead instruction nodes.
230444ed5c5SDimitry Andric   for (NodeAddr<InstrNode*> IA : DINs) {
231444ed5c5SDimitry Andric     NodeAddr<BlockNode*> BA = IA.Addr->getOwner(DFG);
232444ed5c5SDimitry Andric     BA.Addr->removeMember(IA, DFG);
233444ed5c5SDimitry Andric     if (!DFG.IsCode<NodeAttrs::Stmt>(IA))
234444ed5c5SDimitry Andric       continue;
235444ed5c5SDimitry Andric 
236444ed5c5SDimitry Andric     MachineInstr *MI = NodeAddr<StmtNode*>(IA).Addr->getCode();
237444ed5c5SDimitry Andric     if (trace())
238444ed5c5SDimitry Andric       dbgs() << "erasing: " << *MI;
239444ed5c5SDimitry Andric     MI->eraseFromParent();
240444ed5c5SDimitry Andric   }
241444ed5c5SDimitry Andric   return true;
242444ed5c5SDimitry Andric }
243