LLVM  5.0.0
RDFDeadCode.cpp
Go to the documentation of this file.
1 //===--- RDFDeadCode.cpp --------------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // RDF-based generic dead code elimination.
11 
12 #include "RDFDeadCode.h"
13 #include "RDFGraph.h"
14 #include "RDFLiveness.h"
15 
16 #include "llvm/ADT/SetVector.h"
20 
21 #include <queue>
22 
23 using namespace llvm;
24 using namespace rdf;
25 
26 // This drastically improves execution time in "collect" over using
27 // SetVector as a work queue, and popping the first element from it.
28 template<typename T> struct DeadCodeElimination::SetQueue {
29  SetQueue() : Set(), Queue() {}
30 
31  bool empty() const {
32  return Queue.empty();
33  }
35  T V = Queue.front();
36  Queue.pop();
37  Set.erase(V);
38  return V;
39  }
40  void push_back(T V) {
41  if (Set.count(V))
42  return;
43  Queue.push(V);
44  Set.insert(V);
45  }
46 
47 private:
48  DenseSet<T> Set;
49  std::queue<T> Queue;
50 };
51 
52 
53 // Check if the given instruction has observable side-effects, i.e. if
54 // it should be considered "live". It is safe for this function to be
55 // overly conservative (i.e. return "true" for all instructions), but it
56 // is not safe to return "false" for an instruction that should not be
57 // considered removable.
58 bool DeadCodeElimination::isLiveInstr(const MachineInstr *MI) const {
59  if (MI->mayStore() || MI->isBranch() || MI->isCall() || MI->isReturn())
60  return true;
62  return true;
63  if (MI->isPHI())
64  return false;
65  for (auto &Op : MI->operands()) {
66  if (Op.isReg() && MRI.isReserved(Op.getReg()))
67  return true;
68  if (Op.isRegMask()) {
69  const uint32_t *BM = Op.getRegMask();
70  for (unsigned R = 0, RN = DFG.getTRI().getNumRegs(); R != RN; ++R) {
71  if (BM[R/32] & (1u << (R%32)))
72  continue;
73  if (MRI.isReserved(R))
74  return true;
75  }
76  }
77  }
78  return false;
79 }
80 
81 void DeadCodeElimination::scanInstr(NodeAddr<InstrNode*> IA,
82  SetQueue<NodeId> &WorkQ) {
83  if (!DFG.IsCode<NodeAttrs::Stmt>(IA))
84  return;
85  if (!isLiveInstr(NodeAddr<StmtNode*>(IA).Addr->getCode()))
86  return;
87  for (NodeAddr<RefNode*> RA : IA.Addr->members(DFG)) {
88  if (!LiveNodes.count(RA.Id))
89  WorkQ.push_back(RA.Id);
90  }
91 }
92 
93 void DeadCodeElimination::processDef(NodeAddr<DefNode*> DA,
94  SetQueue<NodeId> &WorkQ) {
95  NodeAddr<InstrNode*> IA = DA.Addr->getOwner(DFG);
96  for (NodeAddr<UseNode*> UA : IA.Addr->members_if(DFG.IsUse, DFG)) {
97  if (!LiveNodes.count(UA.Id))
98  WorkQ.push_back(UA.Id);
99  }
100  for (NodeAddr<DefNode*> TA : DFG.getRelatedRefs(IA, DA))
101  LiveNodes.insert(TA.Id);
102 }
103 
104 void DeadCodeElimination::processUse(NodeAddr<UseNode*> UA,
105  SetQueue<NodeId> &WorkQ) {
106  for (NodeAddr<DefNode*> DA : LV.getAllReachingDefs(UA)) {
107  if (!LiveNodes.count(DA.Id))
108  WorkQ.push_back(DA.Id);
109  }
110 }
111 
112 // Traverse the DFG and collect the set dead RefNodes and the set of
113 // dead instructions. Return "true" if any of these sets is non-empty,
114 // "false" otherwise.
116  // This function works by first finding all live nodes. The dead nodes
117  // are then the complement of the set of live nodes.
118  //
119  // Assume that all nodes are dead. Identify instructions which must be
120  // considered live, i.e. instructions with observable side-effects, such
121  // as calls and stores. All arguments of such instructions are considered
122  // live. For each live def, all operands used in the corresponding
123  // instruction are considered live. For each live use, all its reaching
124  // defs are considered live.
125  LiveNodes.clear();
126  SetQueue<NodeId> WorkQ;
127  for (NodeAddr<BlockNode*> BA : DFG.getFunc().Addr->members(DFG))
128  for (NodeAddr<InstrNode*> IA : BA.Addr->members(DFG))
129  scanInstr(IA, WorkQ);
130 
131  while (!WorkQ.empty()) {
132  NodeId N = WorkQ.pop_front();
133  LiveNodes.insert(N);
134  auto RA = DFG.addr<RefNode*>(N);
135  if (DFG.IsDef(RA))
136  processDef(RA, WorkQ);
137  else
138  processUse(RA, WorkQ);
139  }
140 
141  if (trace()) {
142  dbgs() << "Live nodes:\n";
143  for (NodeId N : LiveNodes) {
144  auto RA = DFG.addr<RefNode*>(N);
145  dbgs() << PrintNode<RefNode*>(RA, DFG) << "\n";
146  }
147  }
148 
149  auto IsDead = [this] (NodeAddr<InstrNode*> IA) -> bool {
150  for (NodeAddr<DefNode*> DA : IA.Addr->members_if(DFG.IsDef, DFG))
151  if (LiveNodes.count(DA.Id))
152  return false;
153  return true;
154  };
155 
156  for (NodeAddr<BlockNode*> BA : DFG.getFunc().Addr->members(DFG)) {
157  for (NodeAddr<InstrNode*> IA : BA.Addr->members(DFG)) {
158  for (NodeAddr<RefNode*> RA : IA.Addr->members(DFG))
159  if (!LiveNodes.count(RA.Id))
160  DeadNodes.insert(RA.Id);
161  if (DFG.IsCode<NodeAttrs::Stmt>(IA))
162  if (isLiveInstr(NodeAddr<StmtNode*>(IA).Addr->getCode()))
163  continue;
164  if (IsDead(IA)) {
165  DeadInstrs.insert(IA.Id);
166  if (trace())
167  dbgs() << "Dead instr: " << PrintNode<InstrNode*>(IA, DFG) << "\n";
168  }
169  }
170  }
171 
172  return !DeadNodes.empty();
173 }
174 
175 // Erase the nodes given in the Nodes set from DFG. In addition to removing
176 // them from the DFG, if a node corresponds to a statement, the corresponding
177 // machine instruction is erased from the function.
179  if (Nodes.empty())
180  return false;
181 
182  // Prepare the actual set of ref nodes to remove: ref nodes from Nodes
183  // are included directly, for each InstrNode in Nodes, include the set
184  // of all RefNodes from it.
185  NodeList DRNs, DINs;
186  for (auto I : Nodes) {
187  auto BA = DFG.addr<NodeBase*>(I);
188  uint16_t Type = BA.Addr->getType();
189  if (Type == NodeAttrs::Ref) {
190  DRNs.push_back(DFG.addr<RefNode*>(I));
191  continue;
192  }
193 
194  // If it's a code node, add all ref nodes from it.
195  uint16_t Kind = BA.Addr->getKind();
196  if (Kind == NodeAttrs::Stmt || Kind == NodeAttrs::Phi) {
197  for (auto N : NodeAddr<CodeNode*>(BA).Addr->members(DFG))
198  DRNs.push_back(N);
199  DINs.push_back(DFG.addr<InstrNode*>(I));
200  } else {
201  llvm_unreachable("Unexpected code node");
202  return false;
203  }
204  }
205 
206  // Sort the list so that use nodes are removed first. This makes the
207  // "unlink" functions a bit faster.
208  auto UsesFirst = [] (NodeAddr<RefNode*> A, NodeAddr<RefNode*> B) -> bool {
209  uint16_t KindA = A.Addr->getKind(), KindB = B.Addr->getKind();
210  if (KindA == NodeAttrs::Use && KindB == NodeAttrs::Def)
211  return true;
212  if (KindA == NodeAttrs::Def && KindB == NodeAttrs::Use)
213  return false;
214  return A.Id < B.Id;
215  };
216  std::sort(DRNs.begin(), DRNs.end(), UsesFirst);
217 
218  if (trace())
219  dbgs() << "Removing dead ref nodes:\n";
220  for (NodeAddr<RefNode*> RA : DRNs) {
221  if (trace())
222  dbgs() << " " << PrintNode<RefNode*>(RA, DFG) << '\n';
223  if (DFG.IsUse(RA))
224  DFG.unlinkUse(RA, true);
225  else if (DFG.IsDef(RA))
226  DFG.unlinkDef(RA, true);
227  }
228 
229  // Now, remove all dead instruction nodes.
230  for (NodeAddr<InstrNode*> IA : DINs) {
231  NodeAddr<BlockNode*> BA = IA.Addr->getOwner(DFG);
232  BA.Addr->removeMember(IA, DFG);
233  if (!DFG.IsCode<NodeAttrs::Stmt>(IA))
234  continue;
235 
236  MachineInstr *MI = NodeAddr<StmtNode*>(IA).Addr->getCode();
237  if (trace())
238  dbgs() << "erasing: " << *MI;
239  MI->eraseFromParent();
240  }
241  return true;
242 }
NodeList members(const DataFlowGraph &G) const
Definition: RDFGraph.cpp:541
void push_back(const T &Elt)
Definition: SmallVector.h:211
bool isBranch(QueryType Type=AnyInBundle) const
Returns true if this is a conditional, unconditional, or indirect branch.
Definition: MachineInstr.h:456
NodeList getAllReachingDefs(RegisterRef RefRR, NodeAddr< RefNode * > RefA, bool TopShadows, bool FullChain, const RegisterAggr &DefRRs)
Definition: RDFLiveness.cpp:91
bool mayStore(QueryType Type=AnyInBundle) const
Return true if this instruction could possibly modify memory.
Definition: MachineInstr.h:613
bool hasOrderedMemoryRef() const
Return true if this instruction may have an ordered or volatile memory reference, or if the informati...
iterator_range< mop_iterator > operands()
Definition: MachineInstr.h:306
void unlinkUse(NodeAddr< UseNode * > UA, bool RemoveFromOwner)
Definition: RDFGraph.h:769
NodeList getRelatedRefs(NodeAddr< InstrNode * > IA, NodeAddr< RefNode * > RA) const
Definition: RDFGraph.cpp:1139
bool isPHI() const
Definition: MachineInstr.h:795
void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
unsigned getNumRegs() const
Return the number of registers this target has (useful for sizing arrays holding per register informa...
static bool IsDef(const NodeAddr< NodeBase * > BA)
Definition: RDFGraph.h:794
bool empty() const
Determine if the SetVector is empty or not.
Definition: SetVector.h:73
NodeAddr< NodeBase * > getOwner(const DataFlowGraph &G)
Definition: RDFGraph.cpp:547
NodeList members_if(Predicate P, const DataFlowGraph &G) const
Definition: RDFGraph.h:912
const TargetRegisterInfo & getTRI() const
Definition: RDFGraph.h:663
bool isReturn(QueryType Type=AnyInBundle) const
Definition: MachineInstr.h:428
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
uint16_t getKind() const
Definition: RDFGraph.h:456
bool isReserved(unsigned PhysReg) const
isReserved - Returns true when PhysReg is a reserved register.
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
Definition: SmallVector.h:115
#define A
Definition: LargeTest.cpp:12
bool hasUnmodeledSideEffects() const
Return true if this instruction has side effects that are not modeled by mayLoad / mayStore...
static bool IsCode(const NodeAddr< NodeBase * > BA)
Definition: RDFGraph.h:789
#define B
Definition: LargeTest.cpp:24
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:861
const size_t N
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
NodeAddr< FuncNode * > getFunc() const
Definition: RDFGraph.h:660
static bool IsUse(const NodeAddr< NodeBase * > BA)
Definition: RDFGraph.h:799
llvm_unreachable("Unknown RegBank!")
Representation of each machine instruction.
Definition: MachineInstr.h:59
NodeAddr< NodeBase * > getOwner(const DataFlowGraph &G)
Definition: RDFGraph.cpp:448
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
Definition: SmallVector.h:119
#define I(x, y, z)
Definition: MD5.cpp:58
bool isCall(QueryType Type=AnyInBundle) const
Definition: MachineInstr.h:432
bool erase(const SetVector< NodeId > &Nodes)
const unsigned Kind
void removeMember(NodeAddr< NodeBase * > NA, const DataFlowGraph &G)
Definition: RDFGraph.cpp:509
void unlinkDef(NodeAddr< DefNode * > DA, bool RemoveFromOwner)
Definition: RDFGraph.h:775
A vector that has set insertion semantics.
Definition: SetVector.h:41
NodeAddr< T > addr(NodeId N) const
Definition: RDFGraph.h:656
IRTranslator LLVM IR MI
void sort(Policy policy, RandomAccessIterator Start, RandomAccessIterator End, const Comparator &Comp=Comparator())
Definition: Parallel.h:201