RDF: Dead code elimination
[oota-llvm.git] / lib / Target / Hexagon / RDFDeadCode.h
1 //===--- RDFDeadCode.h ----------------------------------------------------===//
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 // The main interface of this class are functions "collect" and "erase".
13 // This allows custom processing of the function being optimized by a
14 // particular consumer. The simplest way to use this class would be to
15 // instantiate an object, and then simply call "collect" and "erase",
16 // passing the result of "getDeadInstrs()" to it.
17 // A more complex scenario would be to call "collect" first, then visit
18 // all post-increment instructions to see if the address update is dead
19 // or not, and if it is, convert the instruction to a non-updating form.
20 // After that "erase" can be called with the set of nodes including both,
21 // dead defs from the updating instructions and the nodes corresponding
22 // to the dead instructions.
23
24 #ifndef RDF_DEADCODE_H
25 #define RDF_DEADCODE_H
26
27 #include "RDFGraph.h"
28 #include "RDFLiveness.h"
29 #include "llvm/ADT/SetVector.h"
30
31 namespace llvm {
32   class MachineRegisterInfo;
33 }
34
35 namespace rdf {
36   struct DeadCodeElimination {
37     DeadCodeElimination(DataFlowGraph &dfg, MachineRegisterInfo &mri)
38       : Trace(false), DFG(dfg), MRI(mri), LV(mri, dfg) {}
39
40     bool collect();
41     bool erase(const SetVector<NodeId> &Nodes);
42     void trace(bool On) { Trace = On; }
43     bool trace() const { return Trace; }
44
45     SetVector<NodeId> getDeadNodes() { return DeadNodes; }
46     SetVector<NodeId> getDeadInstrs() { return DeadInstrs; }
47     DataFlowGraph &getDFG() { return DFG; }
48
49   private:
50     bool Trace;
51     SetVector<NodeId> LiveNodes;
52     SetVector<NodeId> DeadNodes;
53     SetVector<NodeId> DeadInstrs;
54     DataFlowGraph &DFG;
55     MachineRegisterInfo &MRI;
56     Liveness LV;
57
58     bool isLiveInstr(const MachineInstr *MI) const;
59     void scanInstr(NodeAddr<InstrNode*> IA, SetVector<NodeId> &WorkQ);
60     void processDef(NodeAddr<DefNode*> DA, SetVector<NodeId> &WorkQ);
61     void processUse(NodeAddr<UseNode*> UA, SetVector<NodeId> &WorkQ);
62   };
63 }
64
65 #endif