7841dc26a1b25fb1ecf7758b89ec41f0fea83b44
[oota-llvm.git] / include / llvm / Analysis / SparsePropagation.h
1 //===- SparsePropagation.h - Sparse Conditional Property Propagation ------===//
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 // This file implements an abstract sparse conditional propagation algorithm,
11 // modeled after SCCP, but with a customizable lattice function.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_ANALYSIS_SPARSEPROPAGATION_H
16 #define LLVM_ANALYSIS_SPARSEPROPAGATION_H
17
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/SmallPtrSet.h"
20 #include <set>
21 #include <vector>
22
23 namespace llvm {
24   class Value;
25   class Constant;
26   class Argument;
27   class Instruction;
28   class PHINode;
29   class TerminatorInst;
30   class BasicBlock;
31   class Function;
32   class SparseSolver;
33   class raw_ostream;
34
35   template<typename T> class SmallVectorImpl;
36
37 /// AbstractLatticeFunction - This class is implemented by the dataflow instance
38 /// to specify what the lattice values are and how they handle merges etc.
39 /// This gives the client the power to compute lattice values from instructions,
40 /// constants, etc.  The requirement is that lattice values must all fit into
41 /// a void*.  If a void* is not sufficient, the implementation should use this
42 /// pointer to be a pointer into a uniquing set or something.
43 ///
44 class AbstractLatticeFunction {
45 public:
46   typedef void *LatticeVal;
47
48 private:
49   LatticeVal UndefVal, OverdefinedVal, UntrackedVal;
50
51 public:
52   AbstractLatticeFunction(LatticeVal undefVal, LatticeVal overdefinedVal,
53                           LatticeVal untrackedVal) {
54     UndefVal = undefVal;
55     OverdefinedVal = overdefinedVal;
56     UntrackedVal = untrackedVal;
57   }
58   virtual ~AbstractLatticeFunction();
59
60   LatticeVal getUndefVal()       const { return UndefVal; }
61   LatticeVal getOverdefinedVal() const { return OverdefinedVal; }
62   LatticeVal getUntrackedVal()   const { return UntrackedVal; }
63
64   /// IsUntrackedValue - If the specified Value is something that is obviously
65   /// uninteresting to the analysis (and would always return UntrackedVal),
66   /// this function can return true to avoid pointless work.
67   virtual bool IsUntrackedValue(Value *V) {
68     return false;
69   }
70
71   /// ComputeConstant - Given a constant value, compute and return a lattice
72   /// value corresponding to the specified constant.
73   virtual LatticeVal ComputeConstant(Constant *C) {
74     return getOverdefinedVal(); // always safe
75   }
76
77   /// IsSpecialCasedPHI - Given a PHI node, determine whether this PHI node is
78   /// one that the we want to handle through ComputeInstructionState.
79   virtual bool IsSpecialCasedPHI(PHINode *PN) {
80     return false;
81   }
82
83   /// GetConstant - If the specified lattice value is representable as an LLVM
84   /// constant value, return it.  Otherwise return null.  The returned value
85   /// must be in the same LLVM type as Val.
86   virtual Constant *GetConstant(LatticeVal LV, Value *Val, SparseSolver &SS) {
87     return nullptr;
88   }
89
90   /// ComputeArgument - Given a formal argument value, compute and return a
91   /// lattice value corresponding to the specified argument.
92   virtual LatticeVal ComputeArgument(Argument *I) {
93     return getOverdefinedVal(); // always safe
94   }
95
96   /// MergeValues - Compute and return the merge of the two specified lattice
97   /// values.  Merging should only move one direction down the lattice to
98   /// guarantee convergence (toward overdefined).
99   virtual LatticeVal MergeValues(LatticeVal X, LatticeVal Y) {
100     return getOverdefinedVal(); // always safe, never useful.
101   }
102
103   /// ComputeInstructionState - Given an instruction and a vector of its operand
104   /// values, compute the result value of the instruction.
105   virtual LatticeVal ComputeInstructionState(Instruction &I, SparseSolver &SS) {
106     return getOverdefinedVal(); // always safe, never useful.
107   }
108
109   /// PrintValue - Render the specified lattice value to the specified stream.
110   virtual void PrintValue(LatticeVal V, raw_ostream &OS);
111 };
112
113 /// SparseSolver - This class is a general purpose solver for Sparse Conditional
114 /// Propagation with a programmable lattice function.
115 ///
116 class SparseSolver {
117   typedef AbstractLatticeFunction::LatticeVal LatticeVal;
118
119   /// LatticeFunc - This is the object that knows the lattice and how to do
120   /// compute transfer functions.
121   AbstractLatticeFunction *LatticeFunc;
122
123   DenseMap<Value*, LatticeVal> ValueState;  // The state each value is in.
124   SmallPtrSet<BasicBlock*, 16> BBExecutable;   // The bbs that are executable.
125
126   std::vector<Instruction*> InstWorkList;   // Worklist of insts to process.
127
128   std::vector<BasicBlock*> BBWorkList;  // The BasicBlock work list
129
130   /// KnownFeasibleEdges - Entries in this set are edges which have already had
131   /// PHI nodes retriggered.
132   typedef std::pair<BasicBlock*,BasicBlock*> Edge;
133   std::set<Edge> KnownFeasibleEdges;
134
135   SparseSolver(const SparseSolver&) = delete;
136   void operator=(const SparseSolver&) = delete;
137
138 public:
139   explicit SparseSolver(AbstractLatticeFunction *Lattice)
140     : LatticeFunc(Lattice) {}
141   ~SparseSolver() {
142     delete LatticeFunc;
143   }
144
145   /// Solve - Solve for constants and executable blocks.
146   ///
147   void Solve(Function &F);
148
149   void Print(Function &F, raw_ostream &OS) const;
150
151   /// getLatticeState - Return the LatticeVal object that corresponds to the
152   /// value.  If an value is not in the map, it is returned as untracked,
153   /// unlike the getOrInitValueState method.
154   LatticeVal getLatticeState(Value *V) const {
155     DenseMap<Value*, LatticeVal>::const_iterator I = ValueState.find(V);
156     return I != ValueState.end() ? I->second : LatticeFunc->getUntrackedVal();
157   }
158
159   /// getOrInitValueState - Return the LatticeVal object that corresponds to the
160   /// value, initializing the value's state if it hasn't been entered into the
161   /// map yet.   This function is necessary because not all values should start
162   /// out in the underdefined state... Arguments should be overdefined, and
163   /// constants should be marked as constants.
164   ///
165   LatticeVal getOrInitValueState(Value *V);
166
167   /// isEdgeFeasible - Return true if the control flow edge from the 'From'
168   /// basic block to the 'To' basic block is currently feasible.  If
169   /// AggressiveUndef is true, then this treats values with unknown lattice
170   /// values as undefined.  This is generally only useful when solving the
171   /// lattice, not when querying it.
172   bool isEdgeFeasible(BasicBlock *From, BasicBlock *To,
173                       bool AggressiveUndef = false);
174
175   /// isBlockExecutable - Return true if there are any known feasible
176   /// edges into the basic block.  This is generally only useful when
177   /// querying the lattice.
178   bool isBlockExecutable(BasicBlock *BB) const {
179     return BBExecutable.count(BB);
180   }
181
182 private:
183   /// UpdateState - When the state for some instruction is potentially updated,
184   /// this function notices and adds I to the worklist if needed.
185   void UpdateState(Instruction &Inst, LatticeVal V);
186
187   /// MarkBlockExecutable - This method can be used by clients to mark all of
188   /// the blocks that are known to be intrinsically live in the processed unit.
189   void MarkBlockExecutable(BasicBlock *BB);
190
191   /// markEdgeExecutable - Mark a basic block as executable, adding it to the BB
192   /// work list if it is not already executable.
193   void markEdgeExecutable(BasicBlock *Source, BasicBlock *Dest);
194
195   /// getFeasibleSuccessors - Return a vector of booleans to indicate which
196   /// successors are reachable from a given terminator instruction.
197   void getFeasibleSuccessors(TerminatorInst &TI, SmallVectorImpl<bool> &Succs,
198                              bool AggressiveUndef);
199
200   void visitInst(Instruction &I);
201   void visitPHINode(PHINode &I);
202   void visitTerminatorInst(TerminatorInst &TI);
203 };
204
205 } // end namespace llvm
206
207 #endif // LLVM_ANALYSIS_SPARSEPROPAGATION_H