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