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