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_SPARSEPROPAGATION_H
16 #define LLVM_ANALYSIS_SPARSEPROPAGATION_H
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/SmallPtrSet.h"
35 template <typename T> class SmallVectorImpl;
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.
44 class AbstractLatticeFunction {
46 typedef void *LatticeVal;
49 LatticeVal UndefVal, OverdefinedVal, UntrackedVal;
52 AbstractLatticeFunction(LatticeVal undefVal, LatticeVal overdefinedVal,
53 LatticeVal untrackedVal) {
55 OverdefinedVal = overdefinedVal;
56 UntrackedVal = untrackedVal;
58 virtual ~AbstractLatticeFunction();
60 LatticeVal getUndefVal() const { return UndefVal; }
61 LatticeVal getOverdefinedVal() const { return OverdefinedVal; }
62 LatticeVal getUntrackedVal() const { return UntrackedVal; }
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) { return false; }
69 /// ComputeConstant - Given a constant value, compute and return a lattice
70 /// value corresponding to the specified constant.
71 virtual LatticeVal ComputeConstant(Constant *C) {
72 return getOverdefinedVal(); // always safe
75 /// IsSpecialCasedPHI - Given a PHI node, determine whether this PHI node is
76 /// one that the we want to handle through ComputeInstructionState.
77 virtual bool IsSpecialCasedPHI(PHINode *PN) { return false; }
79 /// GetConstant - If the specified lattice value is representable as an LLVM
80 /// constant value, return it. Otherwise return null. The returned value
81 /// must be in the same LLVM type as Val.
82 virtual Constant *GetConstant(LatticeVal LV, Value *Val, SparseSolver &SS) {
86 /// ComputeArgument - Given a formal argument value, compute and return a
87 /// lattice value corresponding to the specified argument.
88 virtual LatticeVal ComputeArgument(Argument *I) {
89 return getOverdefinedVal(); // always safe
92 /// MergeValues - Compute and return the merge of the two specified lattice
93 /// values. Merging should only move one direction down the lattice to
94 /// guarantee convergence (toward overdefined).
95 virtual LatticeVal MergeValues(LatticeVal X, LatticeVal Y) {
96 return getOverdefinedVal(); // always safe, never useful.
99 /// ComputeInstructionState - Given an instruction and a vector of its operand
100 /// values, compute the result value of the instruction.
101 virtual LatticeVal ComputeInstructionState(Instruction &I, SparseSolver &SS) {
102 return getOverdefinedVal(); // always safe, never useful.
105 /// PrintValue - Render the specified lattice value to the specified stream.
106 virtual void PrintValue(LatticeVal V, raw_ostream &OS);
109 /// SparseSolver - This class is a general purpose solver for Sparse Conditional
110 /// Propagation with a programmable lattice function.
113 typedef AbstractLatticeFunction::LatticeVal LatticeVal;
115 /// LatticeFunc - This is the object that knows the lattice and how to do
116 /// compute transfer functions.
117 AbstractLatticeFunction *LatticeFunc;
119 DenseMap<Value *, LatticeVal> ValueState; // The state each value is in.
120 SmallPtrSet<BasicBlock *, 16> BBExecutable; // The bbs that are executable.
122 std::vector<Instruction *> InstWorkList; // Worklist of insts to process.
124 std::vector<BasicBlock *> BBWorkList; // The BasicBlock work list
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;
131 SparseSolver(const SparseSolver&) = delete;
132 void operator=(const SparseSolver&) = delete;
135 explicit SparseSolver(AbstractLatticeFunction *Lattice)
136 : LatticeFunc(Lattice) {}
137 ~SparseSolver() { delete LatticeFunc; }
139 /// Solve - Solve for constants and executable blocks.
141 void Solve(Function &F);
143 void Print(Function &F, raw_ostream &OS) const;
145 /// getLatticeState - Return the LatticeVal object that corresponds to the
146 /// value. If an value is not in the map, it is returned as untracked,
147 /// unlike the getOrInitValueState method.
148 LatticeVal getLatticeState(Value *V) const {
149 DenseMap<Value*, LatticeVal>::const_iterator I = ValueState.find(V);
150 return I != ValueState.end() ? I->second : LatticeFunc->getUntrackedVal();
153 /// getOrInitValueState - Return the LatticeVal object that corresponds to the
154 /// value, initializing the value's state if it hasn't been entered into the
155 /// map yet. This function is necessary because not all values should start
156 /// out in the underdefined state... Arguments should be overdefined, and
157 /// constants should be marked as constants.
159 LatticeVal getOrInitValueState(Value *V);
161 /// isEdgeFeasible - Return true if the control flow edge from the 'From'
162 /// basic block to the 'To' basic block is currently feasible. If
163 /// AggressiveUndef is true, then this treats values with unknown lattice
164 /// values as undefined. This is generally only useful when solving the
165 /// lattice, not when querying it.
166 bool isEdgeFeasible(BasicBlock *From, BasicBlock *To,
167 bool AggressiveUndef = false);
169 /// isBlockExecutable - Return true if there are any known feasible
170 /// edges into the basic block. This is generally only useful when
171 /// querying the lattice.
172 bool isBlockExecutable(BasicBlock *BB) const {
173 return BBExecutable.count(BB);
177 /// UpdateState - When the state for some instruction is potentially updated,
178 /// this function notices and adds I to the worklist if needed.
179 void UpdateState(Instruction &Inst, LatticeVal V);
181 /// MarkBlockExecutable - This method can be used by clients to mark all of
182 /// the blocks that are known to be intrinsically live in the processed unit.
183 void MarkBlockExecutable(BasicBlock *BB);
185 /// markEdgeExecutable - Mark a basic block as executable, adding it to the BB
186 /// work list if it is not already executable.
187 void markEdgeExecutable(BasicBlock *Source, BasicBlock *Dest);
189 /// getFeasibleSuccessors - Return a vector of booleans to indicate which
190 /// successors are reachable from a given terminator instruction.
191 void getFeasibleSuccessors(TerminatorInst &TI, SmallVectorImpl<bool> &Succs,
192 bool AggressiveUndef);
194 void visitInst(Instruction &I);
195 void visitPHINode(PHINode &I);
196 void visitTerminatorInst(TerminatorInst &TI);
199 } // end namespace llvm
201 #endif // LLVM_ANALYSIS_SPARSEPROPAGATION_H