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 
12db17bf38SDimitry Andric #include "RDFDeadCode.h"
13444ed5c5SDimitry Andric #include "RDFGraph.h"
14444ed5c5SDimitry Andric #include "RDFLiveness.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 {
SetQueueDeadCodeElimination::SetQueue293ca95b02SDimitry Andric   SetQueue() : Set(), Queue() {}
303ca95b02SDimitry Andric 
emptyDeadCodeElimination::SetQueue313ca95b02SDimitry Andric   bool empty() const {
323ca95b02SDimitry Andric     return Queue.empty();
333ca95b02SDimitry Andric   }
pop_frontDeadCodeElimination::SetQueue343ca95b02SDimitry 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   }
push_backDeadCodeElimination::SetQueue403ca95b02SDimitry 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.
isLiveInstr(const MachineInstr * MI) const58444ed5c5SDimitry Andric bool DeadCodeElimination::isLiveInstr(const MachineInstr *MI) const {
59444ed5c5SDimitry Andric   if (MI->mayStore() || MI->isBranch() || MI->isCall() || MI->isReturn())
60444ed5c5SDimitry Andric     return true;
612cab237bSDimitry Andric   if (MI->hasOrderedMemoryRef() || MI->hasUnmodeledSideEffects() ||
622cab237bSDimitry Andric       MI->isPosition())
63444ed5c5SDimitry Andric     return true;
64444ed5c5SDimitry Andric   if (MI->isPHI())
65444ed5c5SDimitry Andric     return false;
667a7e6055SDimitry Andric   for (auto &Op : MI->operands()) {
67444ed5c5SDimitry Andric     if (Op.isReg() && MRI.isReserved(Op.getReg()))
68444ed5c5SDimitry Andric       return true;
697a7e6055SDimitry Andric     if (Op.isRegMask()) {
707a7e6055SDimitry Andric       const uint32_t *BM = Op.getRegMask();
717a7e6055SDimitry Andric       for (unsigned R = 0, RN = DFG.getTRI().getNumRegs(); R != RN; ++R) {
727a7e6055SDimitry Andric         if (BM[R/32] & (1u << (R%32)))
737a7e6055SDimitry Andric           continue;
747a7e6055SDimitry Andric         if (MRI.isReserved(R))
757a7e6055SDimitry Andric           return true;
767a7e6055SDimitry Andric       }
777a7e6055SDimitry Andric     }
787a7e6055SDimitry Andric   }
79444ed5c5SDimitry Andric   return false;
80444ed5c5SDimitry Andric }
81444ed5c5SDimitry Andric 
scanInstr(NodeAddr<InstrNode * > IA,SetQueue<NodeId> & WorkQ)82444ed5c5SDimitry Andric void DeadCodeElimination::scanInstr(NodeAddr<InstrNode*> IA,
833ca95b02SDimitry Andric       SetQueue<NodeId> &WorkQ) {
84444ed5c5SDimitry Andric   if (!DFG.IsCode<NodeAttrs::Stmt>(IA))
85444ed5c5SDimitry Andric     return;
86444ed5c5SDimitry Andric   if (!isLiveInstr(NodeAddr<StmtNode*>(IA).Addr->getCode()))
87444ed5c5SDimitry Andric     return;
88444ed5c5SDimitry Andric   for (NodeAddr<RefNode*> RA : IA.Addr->members(DFG)) {
89444ed5c5SDimitry Andric     if (!LiveNodes.count(RA.Id))
903ca95b02SDimitry Andric       WorkQ.push_back(RA.Id);
91444ed5c5SDimitry Andric   }
92444ed5c5SDimitry Andric }
93444ed5c5SDimitry Andric 
processDef(NodeAddr<DefNode * > DA,SetQueue<NodeId> & WorkQ)94444ed5c5SDimitry Andric void DeadCodeElimination::processDef(NodeAddr<DefNode*> DA,
953ca95b02SDimitry Andric       SetQueue<NodeId> &WorkQ) {
96444ed5c5SDimitry Andric   NodeAddr<InstrNode*> IA = DA.Addr->getOwner(DFG);
97444ed5c5SDimitry Andric   for (NodeAddr<UseNode*> UA : IA.Addr->members_if(DFG.IsUse, DFG)) {
98444ed5c5SDimitry Andric     if (!LiveNodes.count(UA.Id))
993ca95b02SDimitry Andric       WorkQ.push_back(UA.Id);
100444ed5c5SDimitry Andric   }
101444ed5c5SDimitry Andric   for (NodeAddr<DefNode*> TA : DFG.getRelatedRefs(IA, DA))
102444ed5c5SDimitry Andric     LiveNodes.insert(TA.Id);
103444ed5c5SDimitry Andric }
104444ed5c5SDimitry Andric 
processUse(NodeAddr<UseNode * > UA,SetQueue<NodeId> & WorkQ)105444ed5c5SDimitry Andric void DeadCodeElimination::processUse(NodeAddr<UseNode*> UA,
1063ca95b02SDimitry Andric       SetQueue<NodeId> &WorkQ) {
107444ed5c5SDimitry Andric   for (NodeAddr<DefNode*> DA : LV.getAllReachingDefs(UA)) {
108444ed5c5SDimitry Andric     if (!LiveNodes.count(DA.Id))
1093ca95b02SDimitry Andric       WorkQ.push_back(DA.Id);
110444ed5c5SDimitry Andric   }
111444ed5c5SDimitry Andric }
112444ed5c5SDimitry Andric 
113444ed5c5SDimitry Andric // Traverse the DFG and collect the set dead RefNodes and the set of
114444ed5c5SDimitry Andric // dead instructions. Return "true" if any of these sets is non-empty,
115444ed5c5SDimitry Andric // "false" otherwise.
collect()116444ed5c5SDimitry Andric bool DeadCodeElimination::collect() {
117444ed5c5SDimitry Andric   // This function works by first finding all live nodes. The dead nodes
118444ed5c5SDimitry Andric   // are then the complement of the set of live nodes.
119444ed5c5SDimitry Andric   //
120444ed5c5SDimitry Andric   // Assume that all nodes are dead. Identify instructions which must be
121444ed5c5SDimitry Andric   // considered live, i.e. instructions with observable side-effects, such
122444ed5c5SDimitry Andric   // as calls and stores. All arguments of such instructions are considered
123444ed5c5SDimitry Andric   // live. For each live def, all operands used in the corresponding
124444ed5c5SDimitry Andric   // instruction are considered live. For each live use, all its reaching
125444ed5c5SDimitry Andric   // defs are considered live.
126444ed5c5SDimitry Andric   LiveNodes.clear();
1273ca95b02SDimitry Andric   SetQueue<NodeId> WorkQ;
128444ed5c5SDimitry Andric   for (NodeAddr<BlockNode*> BA : DFG.getFunc().Addr->members(DFG))
129444ed5c5SDimitry Andric     for (NodeAddr<InstrNode*> IA : BA.Addr->members(DFG))
130444ed5c5SDimitry Andric       scanInstr(IA, WorkQ);
131444ed5c5SDimitry Andric 
132444ed5c5SDimitry Andric   while (!WorkQ.empty()) {
1333ca95b02SDimitry Andric     NodeId N = WorkQ.pop_front();
134444ed5c5SDimitry Andric     LiveNodes.insert(N);
135444ed5c5SDimitry Andric     auto RA = DFG.addr<RefNode*>(N);
136444ed5c5SDimitry Andric     if (DFG.IsDef(RA))
137444ed5c5SDimitry Andric       processDef(RA, WorkQ);
138444ed5c5SDimitry Andric     else
139444ed5c5SDimitry Andric       processUse(RA, WorkQ);
140444ed5c5SDimitry Andric   }
141444ed5c5SDimitry Andric 
142444ed5c5SDimitry Andric   if (trace()) {
143444ed5c5SDimitry Andric     dbgs() << "Live nodes:\n";
144444ed5c5SDimitry Andric     for (NodeId N : LiveNodes) {
145444ed5c5SDimitry Andric       auto RA = DFG.addr<RefNode*>(N);
146444ed5c5SDimitry Andric       dbgs() << PrintNode<RefNode*>(RA, DFG) << "\n";
147444ed5c5SDimitry Andric     }
148444ed5c5SDimitry Andric   }
149444ed5c5SDimitry Andric 
150444ed5c5SDimitry Andric   auto IsDead = [this] (NodeAddr<InstrNode*> IA) -> bool {
151444ed5c5SDimitry Andric     for (NodeAddr<DefNode*> DA : IA.Addr->members_if(DFG.IsDef, DFG))
152444ed5c5SDimitry Andric       if (LiveNodes.count(DA.Id))
153444ed5c5SDimitry Andric         return false;
154444ed5c5SDimitry Andric     return true;
155444ed5c5SDimitry Andric   };
156444ed5c5SDimitry Andric 
157444ed5c5SDimitry Andric   for (NodeAddr<BlockNode*> BA : DFG.getFunc().Addr->members(DFG)) {
158444ed5c5SDimitry Andric     for (NodeAddr<InstrNode*> IA : BA.Addr->members(DFG)) {
159444ed5c5SDimitry Andric       for (NodeAddr<RefNode*> RA : IA.Addr->members(DFG))
160444ed5c5SDimitry Andric         if (!LiveNodes.count(RA.Id))
161444ed5c5SDimitry Andric           DeadNodes.insert(RA.Id);
162444ed5c5SDimitry Andric       if (DFG.IsCode<NodeAttrs::Stmt>(IA))
163444ed5c5SDimitry Andric         if (isLiveInstr(NodeAddr<StmtNode*>(IA).Addr->getCode()))
164444ed5c5SDimitry Andric           continue;
165444ed5c5SDimitry Andric       if (IsDead(IA)) {
166444ed5c5SDimitry Andric         DeadInstrs.insert(IA.Id);
167444ed5c5SDimitry Andric         if (trace())
168444ed5c5SDimitry Andric           dbgs() << "Dead instr: " << PrintNode<InstrNode*>(IA, DFG) << "\n";
169444ed5c5SDimitry Andric       }
170444ed5c5SDimitry Andric     }
171444ed5c5SDimitry Andric   }
172444ed5c5SDimitry Andric 
173444ed5c5SDimitry Andric   return !DeadNodes.empty();
174444ed5c5SDimitry Andric }
175444ed5c5SDimitry Andric 
176444ed5c5SDimitry Andric // Erase the nodes given in the Nodes set from DFG. In addition to removing
177444ed5c5SDimitry Andric // them from the DFG, if a node corresponds to a statement, the corresponding
178444ed5c5SDimitry Andric // machine instruction is erased from the function.
erase(const SetVector<NodeId> & Nodes)179444ed5c5SDimitry Andric bool DeadCodeElimination::erase(const SetVector<NodeId> &Nodes) {
180444ed5c5SDimitry Andric   if (Nodes.empty())
181444ed5c5SDimitry Andric     return false;
182444ed5c5SDimitry Andric 
183444ed5c5SDimitry Andric   // Prepare the actual set of ref nodes to remove: ref nodes from Nodes
184444ed5c5SDimitry Andric   // are included directly, for each InstrNode in Nodes, include the set
185444ed5c5SDimitry Andric   // of all RefNodes from it.
186444ed5c5SDimitry Andric   NodeList DRNs, DINs;
187444ed5c5SDimitry Andric   for (auto I : Nodes) {
188444ed5c5SDimitry Andric     auto BA = DFG.addr<NodeBase*>(I);
189444ed5c5SDimitry Andric     uint16_t Type = BA.Addr->getType();
190444ed5c5SDimitry Andric     if (Type == NodeAttrs::Ref) {
191444ed5c5SDimitry Andric       DRNs.push_back(DFG.addr<RefNode*>(I));
192444ed5c5SDimitry Andric       continue;
193444ed5c5SDimitry Andric     }
194444ed5c5SDimitry Andric 
195444ed5c5SDimitry Andric     // If it's a code node, add all ref nodes from it.
196444ed5c5SDimitry Andric     uint16_t Kind = BA.Addr->getKind();
197444ed5c5SDimitry Andric     if (Kind == NodeAttrs::Stmt || Kind == NodeAttrs::Phi) {
198444ed5c5SDimitry Andric       for (auto N : NodeAddr<CodeNode*>(BA).Addr->members(DFG))
199444ed5c5SDimitry Andric         DRNs.push_back(N);
200444ed5c5SDimitry Andric       DINs.push_back(DFG.addr<InstrNode*>(I));
201444ed5c5SDimitry Andric     } else {
202444ed5c5SDimitry Andric       llvm_unreachable("Unexpected code node");
203444ed5c5SDimitry Andric       return false;
204444ed5c5SDimitry Andric     }
205444ed5c5SDimitry Andric   }
206444ed5c5SDimitry Andric 
207444ed5c5SDimitry Andric   // Sort the list so that use nodes are removed first. This makes the
208444ed5c5SDimitry Andric   // "unlink" functions a bit faster.
209444ed5c5SDimitry Andric   auto UsesFirst = [] (NodeAddr<RefNode*> A, NodeAddr<RefNode*> B) -> bool {
210444ed5c5SDimitry Andric     uint16_t KindA = A.Addr->getKind(), KindB = B.Addr->getKind();
211444ed5c5SDimitry Andric     if (KindA == NodeAttrs::Use && KindB == NodeAttrs::Def)
212444ed5c5SDimitry Andric       return true;
213444ed5c5SDimitry Andric     if (KindA == NodeAttrs::Def && KindB == NodeAttrs::Use)
214444ed5c5SDimitry Andric       return false;
215444ed5c5SDimitry Andric     return A.Id < B.Id;
216444ed5c5SDimitry Andric   };
217*b5893f02SDimitry Andric   llvm::sort(DRNs, UsesFirst);
218444ed5c5SDimitry Andric 
219444ed5c5SDimitry Andric   if (trace())
220444ed5c5SDimitry Andric     dbgs() << "Removing dead ref nodes:\n";
221444ed5c5SDimitry Andric   for (NodeAddr<RefNode*> RA : DRNs) {
222444ed5c5SDimitry Andric     if (trace())
223444ed5c5SDimitry Andric       dbgs() << "  " << PrintNode<RefNode*>(RA, DFG) << '\n';
224444ed5c5SDimitry Andric     if (DFG.IsUse(RA))
2253ca95b02SDimitry Andric       DFG.unlinkUse(RA, true);
226444ed5c5SDimitry Andric     else if (DFG.IsDef(RA))
2273ca95b02SDimitry Andric       DFG.unlinkDef(RA, true);
228444ed5c5SDimitry Andric   }
229444ed5c5SDimitry Andric 
230444ed5c5SDimitry Andric   // Now, remove all dead instruction nodes.
231444ed5c5SDimitry Andric   for (NodeAddr<InstrNode*> IA : DINs) {
232444ed5c5SDimitry Andric     NodeAddr<BlockNode*> BA = IA.Addr->getOwner(DFG);
233444ed5c5SDimitry Andric     BA.Addr->removeMember(IA, DFG);
234444ed5c5SDimitry Andric     if (!DFG.IsCode<NodeAttrs::Stmt>(IA))
235444ed5c5SDimitry Andric       continue;
236444ed5c5SDimitry Andric 
237444ed5c5SDimitry Andric     MachineInstr *MI = NodeAddr<StmtNode*>(IA).Addr->getCode();
238444ed5c5SDimitry Andric     if (trace())
239444ed5c5SDimitry Andric       dbgs() << "erasing: " << *MI;
240444ed5c5SDimitry Andric     MI->eraseFromParent();
241444ed5c5SDimitry Andric   }
242444ed5c5SDimitry Andric   return true;
243444ed5c5SDimitry Andric }
244