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;
65444ed5c5SDimitry Andric   for (auto &Op : MI->operands())
66444ed5c5SDimitry Andric     if (Op.isReg() && MRI.isReserved(Op.getReg()))
67444ed5c5SDimitry Andric       return true;
68444ed5c5SDimitry Andric   return false;
69444ed5c5SDimitry Andric }
70444ed5c5SDimitry Andric 
71444ed5c5SDimitry Andric void DeadCodeElimination::scanInstr(NodeAddr<InstrNode*> IA,
723ca95b02SDimitry Andric       SetQueue<NodeId> &WorkQ) {
73444ed5c5SDimitry Andric   if (!DFG.IsCode<NodeAttrs::Stmt>(IA))
74444ed5c5SDimitry Andric     return;
75444ed5c5SDimitry Andric   if (!isLiveInstr(NodeAddr<StmtNode*>(IA).Addr->getCode()))
76444ed5c5SDimitry Andric     return;
77444ed5c5SDimitry Andric   for (NodeAddr<RefNode*> RA : IA.Addr->members(DFG)) {
78444ed5c5SDimitry Andric     if (!LiveNodes.count(RA.Id))
793ca95b02SDimitry Andric       WorkQ.push_back(RA.Id);
80444ed5c5SDimitry Andric   }
81444ed5c5SDimitry Andric }
82444ed5c5SDimitry Andric 
83444ed5c5SDimitry Andric void DeadCodeElimination::processDef(NodeAddr<DefNode*> DA,
843ca95b02SDimitry Andric       SetQueue<NodeId> &WorkQ) {
85444ed5c5SDimitry Andric   NodeAddr<InstrNode*> IA = DA.Addr->getOwner(DFG);
86444ed5c5SDimitry Andric   for (NodeAddr<UseNode*> UA : IA.Addr->members_if(DFG.IsUse, DFG)) {
87444ed5c5SDimitry Andric     if (!LiveNodes.count(UA.Id))
883ca95b02SDimitry Andric       WorkQ.push_back(UA.Id);
89444ed5c5SDimitry Andric   }
90444ed5c5SDimitry Andric   for (NodeAddr<DefNode*> TA : DFG.getRelatedRefs(IA, DA))
91444ed5c5SDimitry Andric     LiveNodes.insert(TA.Id);
92444ed5c5SDimitry Andric }
93444ed5c5SDimitry Andric 
94444ed5c5SDimitry Andric void DeadCodeElimination::processUse(NodeAddr<UseNode*> UA,
953ca95b02SDimitry Andric       SetQueue<NodeId> &WorkQ) {
96444ed5c5SDimitry Andric   for (NodeAddr<DefNode*> DA : LV.getAllReachingDefs(UA)) {
97444ed5c5SDimitry Andric     if (!LiveNodes.count(DA.Id))
983ca95b02SDimitry Andric       WorkQ.push_back(DA.Id);
99444ed5c5SDimitry Andric   }
100444ed5c5SDimitry Andric }
101444ed5c5SDimitry Andric 
102444ed5c5SDimitry Andric // Traverse the DFG and collect the set dead RefNodes and the set of
103444ed5c5SDimitry Andric // dead instructions. Return "true" if any of these sets is non-empty,
104444ed5c5SDimitry Andric // "false" otherwise.
105444ed5c5SDimitry Andric bool DeadCodeElimination::collect() {
106444ed5c5SDimitry Andric   // This function works by first finding all live nodes. The dead nodes
107444ed5c5SDimitry Andric   // are then the complement of the set of live nodes.
108444ed5c5SDimitry Andric   //
109444ed5c5SDimitry Andric   // Assume that all nodes are dead. Identify instructions which must be
110444ed5c5SDimitry Andric   // considered live, i.e. instructions with observable side-effects, such
111444ed5c5SDimitry Andric   // as calls and stores. All arguments of such instructions are considered
112444ed5c5SDimitry Andric   // live. For each live def, all operands used in the corresponding
113444ed5c5SDimitry Andric   // instruction are considered live. For each live use, all its reaching
114444ed5c5SDimitry Andric   // defs are considered live.
115444ed5c5SDimitry Andric   LiveNodes.clear();
1163ca95b02SDimitry Andric   SetQueue<NodeId> WorkQ;
117444ed5c5SDimitry Andric   for (NodeAddr<BlockNode*> BA : DFG.getFunc().Addr->members(DFG))
118444ed5c5SDimitry Andric     for (NodeAddr<InstrNode*> IA : BA.Addr->members(DFG))
119444ed5c5SDimitry Andric       scanInstr(IA, WorkQ);
120444ed5c5SDimitry Andric 
121444ed5c5SDimitry Andric   while (!WorkQ.empty()) {
1223ca95b02SDimitry Andric     NodeId N = WorkQ.pop_front();
123444ed5c5SDimitry Andric     LiveNodes.insert(N);
124444ed5c5SDimitry Andric     auto RA = DFG.addr<RefNode*>(N);
125444ed5c5SDimitry Andric     if (DFG.IsDef(RA))
126444ed5c5SDimitry Andric       processDef(RA, WorkQ);
127444ed5c5SDimitry Andric     else
128444ed5c5SDimitry Andric       processUse(RA, WorkQ);
129444ed5c5SDimitry Andric   }
130444ed5c5SDimitry Andric 
131444ed5c5SDimitry Andric   if (trace()) {
132444ed5c5SDimitry Andric     dbgs() << "Live nodes:\n";
133444ed5c5SDimitry Andric     for (NodeId N : LiveNodes) {
134444ed5c5SDimitry Andric       auto RA = DFG.addr<RefNode*>(N);
135444ed5c5SDimitry Andric       dbgs() << PrintNode<RefNode*>(RA, DFG) << "\n";
136444ed5c5SDimitry Andric     }
137444ed5c5SDimitry Andric   }
138444ed5c5SDimitry Andric 
139444ed5c5SDimitry Andric   auto IsDead = [this] (NodeAddr<InstrNode*> IA) -> bool {
140444ed5c5SDimitry Andric     for (NodeAddr<DefNode*> DA : IA.Addr->members_if(DFG.IsDef, DFG))
141444ed5c5SDimitry Andric       if (LiveNodes.count(DA.Id))
142444ed5c5SDimitry Andric         return false;
143444ed5c5SDimitry Andric     return true;
144444ed5c5SDimitry Andric   };
145444ed5c5SDimitry Andric 
146444ed5c5SDimitry Andric   for (NodeAddr<BlockNode*> BA : DFG.getFunc().Addr->members(DFG)) {
147444ed5c5SDimitry Andric     for (NodeAddr<InstrNode*> IA : BA.Addr->members(DFG)) {
148444ed5c5SDimitry Andric       for (NodeAddr<RefNode*> RA : IA.Addr->members(DFG))
149444ed5c5SDimitry Andric         if (!LiveNodes.count(RA.Id))
150444ed5c5SDimitry Andric           DeadNodes.insert(RA.Id);
151444ed5c5SDimitry Andric       if (DFG.IsCode<NodeAttrs::Stmt>(IA))
152444ed5c5SDimitry Andric         if (isLiveInstr(NodeAddr<StmtNode*>(IA).Addr->getCode()))
153444ed5c5SDimitry Andric           continue;
154444ed5c5SDimitry Andric       if (IsDead(IA)) {
155444ed5c5SDimitry Andric         DeadInstrs.insert(IA.Id);
156444ed5c5SDimitry Andric         if (trace())
157444ed5c5SDimitry Andric           dbgs() << "Dead instr: " << PrintNode<InstrNode*>(IA, DFG) << "\n";
158444ed5c5SDimitry Andric       }
159444ed5c5SDimitry Andric     }
160444ed5c5SDimitry Andric   }
161444ed5c5SDimitry Andric 
162444ed5c5SDimitry Andric   return !DeadNodes.empty();
163444ed5c5SDimitry Andric }
164444ed5c5SDimitry Andric 
165444ed5c5SDimitry Andric // Erase the nodes given in the Nodes set from DFG. In addition to removing
166444ed5c5SDimitry Andric // them from the DFG, if a node corresponds to a statement, the corresponding
167444ed5c5SDimitry Andric // machine instruction is erased from the function.
168444ed5c5SDimitry Andric bool DeadCodeElimination::erase(const SetVector<NodeId> &Nodes) {
169444ed5c5SDimitry Andric   if (Nodes.empty())
170444ed5c5SDimitry Andric     return false;
171444ed5c5SDimitry Andric 
172444ed5c5SDimitry Andric   // Prepare the actual set of ref nodes to remove: ref nodes from Nodes
173444ed5c5SDimitry Andric   // are included directly, for each InstrNode in Nodes, include the set
174444ed5c5SDimitry Andric   // of all RefNodes from it.
175444ed5c5SDimitry Andric   NodeList DRNs, DINs;
176444ed5c5SDimitry Andric   for (auto I : Nodes) {
177444ed5c5SDimitry Andric     auto BA = DFG.addr<NodeBase*>(I);
178444ed5c5SDimitry Andric     uint16_t Type = BA.Addr->getType();
179444ed5c5SDimitry Andric     if (Type == NodeAttrs::Ref) {
180444ed5c5SDimitry Andric       DRNs.push_back(DFG.addr<RefNode*>(I));
181444ed5c5SDimitry Andric       continue;
182444ed5c5SDimitry Andric     }
183444ed5c5SDimitry Andric 
184444ed5c5SDimitry Andric     // If it's a code node, add all ref nodes from it.
185444ed5c5SDimitry Andric     uint16_t Kind = BA.Addr->getKind();
186444ed5c5SDimitry Andric     if (Kind == NodeAttrs::Stmt || Kind == NodeAttrs::Phi) {
187444ed5c5SDimitry Andric       for (auto N : NodeAddr<CodeNode*>(BA).Addr->members(DFG))
188444ed5c5SDimitry Andric         DRNs.push_back(N);
189444ed5c5SDimitry Andric       DINs.push_back(DFG.addr<InstrNode*>(I));
190444ed5c5SDimitry Andric     } else {
191444ed5c5SDimitry Andric       llvm_unreachable("Unexpected code node");
192444ed5c5SDimitry Andric       return false;
193444ed5c5SDimitry Andric     }
194444ed5c5SDimitry Andric   }
195444ed5c5SDimitry Andric 
196444ed5c5SDimitry Andric   // Sort the list so that use nodes are removed first. This makes the
197444ed5c5SDimitry Andric   // "unlink" functions a bit faster.
198444ed5c5SDimitry Andric   auto UsesFirst = [] (NodeAddr<RefNode*> A, NodeAddr<RefNode*> B) -> bool {
199444ed5c5SDimitry Andric     uint16_t KindA = A.Addr->getKind(), KindB = B.Addr->getKind();
200444ed5c5SDimitry Andric     if (KindA == NodeAttrs::Use && KindB == NodeAttrs::Def)
201444ed5c5SDimitry Andric       return true;
202444ed5c5SDimitry Andric     if (KindA == NodeAttrs::Def && KindB == NodeAttrs::Use)
203444ed5c5SDimitry Andric       return false;
204444ed5c5SDimitry Andric     return A.Id < B.Id;
205444ed5c5SDimitry Andric   };
206444ed5c5SDimitry Andric   std::sort(DRNs.begin(), DRNs.end(), UsesFirst);
207444ed5c5SDimitry Andric 
208444ed5c5SDimitry Andric   if (trace())
209444ed5c5SDimitry Andric     dbgs() << "Removing dead ref nodes:\n";
210444ed5c5SDimitry Andric   for (NodeAddr<RefNode*> RA : DRNs) {
211444ed5c5SDimitry Andric     if (trace())
212444ed5c5SDimitry Andric       dbgs() << "  " << PrintNode<RefNode*>(RA, DFG) << '\n';
213444ed5c5SDimitry Andric     if (DFG.IsUse(RA))
2143ca95b02SDimitry Andric       DFG.unlinkUse(RA, true);
215444ed5c5SDimitry Andric     else if (DFG.IsDef(RA))
2163ca95b02SDimitry Andric       DFG.unlinkDef(RA, true);
217444ed5c5SDimitry Andric   }
218444ed5c5SDimitry Andric 
219444ed5c5SDimitry Andric   // Now, remove all dead instruction nodes.
220444ed5c5SDimitry Andric   for (NodeAddr<InstrNode*> IA : DINs) {
221444ed5c5SDimitry Andric     NodeAddr<BlockNode*> BA = IA.Addr->getOwner(DFG);
222444ed5c5SDimitry Andric     BA.Addr->removeMember(IA, DFG);
223444ed5c5SDimitry Andric     if (!DFG.IsCode<NodeAttrs::Stmt>(IA))
224444ed5c5SDimitry Andric       continue;
225444ed5c5SDimitry Andric 
226444ed5c5SDimitry Andric     MachineInstr *MI = NodeAddr<StmtNode*>(IA).Addr->getCode();
227444ed5c5SDimitry Andric     if (trace())
228444ed5c5SDimitry Andric       dbgs() << "erasing: " << *MI;
229444ed5c5SDimitry Andric     MI->eraseFromParent();
230444ed5c5SDimitry Andric   }
231444ed5c5SDimitry Andric   return true;
232444ed5c5SDimitry Andric }
233