1 //===- SparsePropagation.h - Sparse Conditional Property Propagation ------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements an abstract sparse conditional propagation algorithm,
11 // modeled after SCCP, but with a customizable lattice function.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_ANALYSIS_SPARSE_PROPAGATION_H
16 #define LLVM_ANALYSIS_SPARSE_PROPAGATION_H
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/SmallPtrSet.h"
36 template<typename T> class SmallVectorImpl;
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.
45 class AbstractLatticeFunction {
47 typedef void *LatticeVal;
49 LatticeVal UndefVal, OverdefinedVal, UntrackedVal;
51 AbstractLatticeFunction(LatticeVal undefVal, LatticeVal overdefinedVal,
52 LatticeVal untrackedVal) {
54 OverdefinedVal = overdefinedVal;
55 UntrackedVal = untrackedVal;
57 virtual ~AbstractLatticeFunction();
59 LatticeVal getUndefVal() const { return UndefVal; }
60 LatticeVal getOverdefinedVal() const { return OverdefinedVal; }
61 LatticeVal getUntrackedVal() const { return UntrackedVal; }
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) {
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
76 /// IsSpecialCasedPHI - Given a PHI node, determine whether this PHI node is
77 /// one that the we want to handle through ComputeInstructionState.
78 virtual bool IsSpecialCasedPHI(PHINode *PN) {
82 /// GetConstant - If the specified lattice value is representable as an LLVM
83 /// constant value, return it. Otherwise return null. The returned value
84 /// must be in the same LLVM type as Val.
85 virtual Constant *GetConstant(LatticeVal LV, Value *Val, SparseSolver &SS) {
89 /// ComputeArgument - Given a formal argument value, compute and return a
90 /// lattice value corresponding to the specified argument.
91 virtual LatticeVal ComputeArgument(Argument *I) {
92 return getOverdefinedVal(); // always safe
95 /// MergeValues - Compute and return the merge of the two specified lattice
96 /// values. Merging should only move one direction down the lattice to
97 /// guarantee convergence (toward overdefined).
98 virtual LatticeVal MergeValues(LatticeVal X, LatticeVal Y) {
99 return getOverdefinedVal(); // always safe, never useful.
102 /// ComputeInstructionState - Given an instruction and a vector of its operand
103 /// values, compute the result value of the instruction.
104 virtual LatticeVal ComputeInstructionState(Instruction &I, SparseSolver &SS) {
105 return getOverdefinedVal(); // always safe, never useful.
108 /// PrintValue - Render the specified lattice value to the specified stream.
109 virtual void PrintValue(LatticeVal V, raw_ostream &OS);
113 /// SparseSolver - This class is a general purpose solver for Sparse Conditional
114 /// Propagation with a programmable lattice function.
117 typedef AbstractLatticeFunction::LatticeVal LatticeVal;
119 /// LatticeFunc - This is the object that knows the lattice and how to do
120 /// compute transfer functions.
121 AbstractLatticeFunction *LatticeFunc;
123 LLVMContext *Context;
125 DenseMap<Value*, LatticeVal> ValueState; // The state each value is in.
126 SmallPtrSet<BasicBlock*, 16> BBExecutable; // The bbs that are executable.
128 std::vector<Instruction*> InstWorkList; // Worklist of insts to process.
130 std::vector<BasicBlock*> BBWorkList; // The BasicBlock work list
132 /// KnownFeasibleEdges - Entries in this set are edges which have already had
133 /// PHI nodes retriggered.
134 typedef std::pair<BasicBlock*,BasicBlock*> Edge;
135 std::set<Edge> KnownFeasibleEdges;
137 SparseSolver(const SparseSolver&); // DO NOT IMPLEMENT
138 void operator=(const SparseSolver&); // DO NOT IMPLEMENT
140 explicit SparseSolver(AbstractLatticeFunction *Lattice, LLVMContext *C)
141 : LatticeFunc(Lattice), Context(C) {}
146 /// Solve - Solve for constants and executable blocks.
148 void Solve(Function &F);
150 void Print(Function &F, raw_ostream &OS) const;
152 /// getLatticeState - Return the LatticeVal object that corresponds to the
153 /// value. If an value is not in the map, it is returned as untracked,
154 /// unlike the getOrInitValueState method.
155 LatticeVal getLatticeState(Value *V) const {
156 DenseMap<Value*, LatticeVal>::iterator I = ValueState.find(V);
157 return I != ValueState.end() ? I->second : LatticeFunc->getUntrackedVal();
160 /// getOrInitValueState - Return the LatticeVal object that corresponds to the
161 /// value, initializing the value's state if it hasn't been entered into the
162 /// map yet. This function is necessary because not all values should start
163 /// out in the underdefined state... Arguments should be overdefined, and
164 /// constants should be marked as constants.
166 LatticeVal getOrInitValueState(Value *V);
168 /// isEdgeFeasible - Return true if the control flow edge from the 'From'
169 /// basic block to the 'To' basic block is currently feasible. If
170 /// AggressiveUndef is true, then this treats values with unknown lattice
171 /// values as undefined. This is generally only useful when solving the
172 /// lattice, not when querying it.
173 bool isEdgeFeasible(BasicBlock *From, BasicBlock *To,
174 bool AggressiveUndef = false);
176 /// isBlockExecutable - Return true if there are any known feasible
177 /// edges into the basic block. This is generally only useful when
178 /// querying the lattice.
179 bool isBlockExecutable(BasicBlock *BB) const {
180 return BBExecutable.count(BB);
184 /// UpdateState - When the state for some instruction is potentially updated,
185 /// this function notices and adds I to the worklist if needed.
186 void UpdateState(Instruction &Inst, LatticeVal V);
188 /// MarkBlockExecutable - This method can be used by clients to mark all of
189 /// the blocks that are known to be intrinsically live in the processed unit.
190 void MarkBlockExecutable(BasicBlock *BB);
192 /// markEdgeExecutable - Mark a basic block as executable, adding it to the BB
193 /// work list if it is not already executable.
194 void markEdgeExecutable(BasicBlock *Source, BasicBlock *Dest);
196 /// getFeasibleSuccessors - Return a vector of booleans to indicate which
197 /// successors are reachable from a given terminator instruction.
198 void getFeasibleSuccessors(TerminatorInst &TI, SmallVectorImpl<bool> &Succs,
199 bool AggressiveUndef);
201 void visitInst(Instruction &I);
202 void visitPHINode(PHINode &I);
203 void visitTerminatorInst(TerminatorInst &TI);
207 } // end namespace llvm
209 #endif // LLVM_ANALYSIS_SPARSE_PROPAGATION_H