LLVM API Documentation
00001 //===- SparsePropagation.h - Sparse Conditional Property Propagation ------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements an abstract sparse conditional propagation algorithm, 00011 // modeled after SCCP, but with a customizable lattice function. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #ifndef LLVM_ANALYSIS_SPARSE_PROPAGATION_H 00016 #define LLVM_ANALYSIS_SPARSE_PROPAGATION_H 00017 00018 #include "llvm/ADT/DenseMap.h" 00019 #include "llvm/ADT/SmallPtrSet.h" 00020 #include <iosfwd> 00021 #include <vector> 00022 #include <set> 00023 00024 namespace llvm { 00025 class Value; 00026 class Constant; 00027 class Argument; 00028 class Instruction; 00029 class PHINode; 00030 class TerminatorInst; 00031 class BasicBlock; 00032 class Function; 00033 class SparseSolver; 00034 00035 template<typename T> class SmallVectorImpl; 00036 00037 /// AbstractLatticeFunction - This class is implemented by the dataflow instance 00038 /// to specify what the lattice values are and how they handle merges etc. 00039 /// This gives the client the power to compute lattice values from instructions, 00040 /// constants, etc. The requirement is that lattice values must all fit into 00041 /// a void*. If a void* is not sufficient, the implementation should use this 00042 /// pointer to be a pointer into a uniquing set or something. 00043 /// 00044 class AbstractLatticeFunction { 00045 public: 00046 typedef void *LatticeVal; 00047 private: 00048 LatticeVal UndefVal, OverdefinedVal, UntrackedVal; 00049 public: 00050 AbstractLatticeFunction(LatticeVal undefVal, LatticeVal overdefinedVal, 00051 LatticeVal untrackedVal) { 00052 UndefVal = undefVal; 00053 OverdefinedVal = overdefinedVal; 00054 UntrackedVal = untrackedVal; 00055 } 00056 virtual ~AbstractLatticeFunction(); 00057 00058 LatticeVal getUndefVal() const { return UndefVal; } 00059 LatticeVal getOverdefinedVal() const { return OverdefinedVal; } 00060 LatticeVal getUntrackedVal() const { return UntrackedVal; } 00061 00062 /// IsUntrackedValue - If the specified Value is something that is obviously 00063 /// uninteresting to the analysis (and would always return UntrackedVal), 00064 /// this function can return true to avoid pointless work. 00065 virtual bool IsUntrackedValue(Value *V) { 00066 return false; 00067 } 00068 00069 /// ComputeConstant - Given a constant value, compute and return a lattice 00070 /// value corresponding to the specified constant. 00071 virtual LatticeVal ComputeConstant(Constant *C) { 00072 return getOverdefinedVal(); // always safe 00073 } 00074 00075 /// GetConstant - If the specified lattice value is representable as an LLVM 00076 /// constant value, return it. Otherwise return null. The returned value 00077 /// must be in the same LLVM type as Val. 00078 virtual Constant *GetConstant(LatticeVal LV, Value *Val, SparseSolver &SS) { 00079 return 0; 00080 } 00081 00082 /// ComputeArgument - Given a formal argument value, compute and return a 00083 /// lattice value corresponding to the specified argument. 00084 virtual LatticeVal ComputeArgument(Argument *I) { 00085 return getOverdefinedVal(); // always safe 00086 } 00087 00088 /// MergeValues - Compute and return the merge of the two specified lattice 00089 /// values. Merging should only move one direction down the lattice to 00090 /// guarantee convergence (toward overdefined). 00091 virtual LatticeVal MergeValues(LatticeVal X, LatticeVal Y) { 00092 return getOverdefinedVal(); // always safe, never useful. 00093 } 00094 00095 /// ComputeInstructionState - Given an instruction and a vector of its operand 00096 /// values, compute the result value of the instruction. 00097 virtual LatticeVal ComputeInstructionState(Instruction &I, SparseSolver &SS) { 00098 return getOverdefinedVal(); // always safe, never useful. 00099 } 00100 00101 /// PrintValue - Render the specified lattice value to the specified stream. 00102 virtual void PrintValue(LatticeVal V, std::ostream &OS); 00103 }; 00104 00105 00106 /// SparseSolver - This class is a general purpose solver for Sparse Conditional 00107 /// Propagation with a programmable lattice function. 00108 /// 00109 class SparseSolver { 00110 typedef AbstractLatticeFunction::LatticeVal LatticeVal; 00111 00112 /// LatticeFunc - This is the object that knows the lattice and how to do 00113 /// compute transfer functions. 00114 AbstractLatticeFunction *LatticeFunc; 00115 00116 DenseMap<Value*, LatticeVal> ValueState; // The state each value is in. 00117 SmallPtrSet<BasicBlock*, 16> BBExecutable; // The bbs that are executable. 00118 00119 std::vector<Instruction*> InstWorkList; // Worklist of insts to process. 00120 00121 std::vector<BasicBlock*> BBWorkList; // The BasicBlock work list 00122 00123 /// KnownFeasibleEdges - Entries in this set are edges which have already had 00124 /// PHI nodes retriggered. 00125 typedef std::pair<BasicBlock*,BasicBlock*> Edge; 00126 std::set<Edge> KnownFeasibleEdges; 00127 00128 SparseSolver(const SparseSolver&); // DO NOT IMPLEMENT 00129 void operator=(const SparseSolver&); // DO NOT IMPLEMENT 00130 public: 00131 explicit SparseSolver(AbstractLatticeFunction *Lattice) 00132 : LatticeFunc(Lattice) {} 00133 ~SparseSolver() { 00134 delete LatticeFunc; 00135 } 00136 00137 /// Solve - Solve for constants and executable blocks. 00138 /// 00139 void Solve(Function &F); 00140 00141 void Print(Function &F, std::ostream &OS); 00142 00143 /// getLatticeState - Return the LatticeVal object that corresponds to the 00144 /// value. If an value is not in the map, it is returned as untracked, 00145 /// unlike the getOrInitValueState method. 00146 LatticeVal getLatticeState(Value *V) const { 00147 DenseMap<Value*, LatticeVal>::iterator I = ValueState.find(V); 00148 return I != ValueState.end() ? I->second : LatticeFunc->getUntrackedVal(); 00149 } 00150 00151 /// getOrInitValueState - Return the LatticeVal object that corresponds to the 00152 /// value, initializing the value's state if it hasn't been entered into the 00153 /// map yet. This function is necessary because not all values should start 00154 /// out in the underdefined state... Arguments should be overdefined, and 00155 /// constants should be marked as constants. 00156 /// 00157 LatticeVal getOrInitValueState(Value *V); 00158 00159 /// isEdgeFeasible - Return true if the control flow edge from the 'From' 00160 /// basic block to the 'To' basic block is currently feasible. If 00161 /// AggressiveUndef is true, then this treats values with unknown lattice 00162 /// values as undefined. This is generally only useful when solving the 00163 /// lattice, not when querying it. 00164 bool isEdgeFeasible(BasicBlock *From, BasicBlock *To, 00165 bool AggressiveUndef = false); 00166 00167 private: 00168 /// UpdateState - When the state for some instruction is potentially updated, 00169 /// this function notices and adds I to the worklist if needed. 00170 void UpdateState(Instruction &Inst, LatticeVal V); 00171 00172 /// MarkBlockExecutable - This method can be used by clients to mark all of 00173 /// the blocks that are known to be intrinsically live in the processed unit. 00174 void MarkBlockExecutable(BasicBlock *BB); 00175 00176 /// markEdgeExecutable - Mark a basic block as executable, adding it to the BB 00177 /// work list if it is not already executable. 00178 void markEdgeExecutable(BasicBlock *Source, BasicBlock *Dest); 00179 00180 /// getFeasibleSuccessors - Return a vector of booleans to indicate which 00181 /// successors are reachable from a given terminator instruction. 00182 void getFeasibleSuccessors(TerminatorInst &TI, SmallVectorImpl<bool> &Succs, 00183 bool AggressiveUndef); 00184 00185 void visitInst(Instruction &I); 00186 void visitPHINode(PHINode &I); 00187 void visitTerminatorInst(TerminatorInst &TI); 00188 00189 }; 00190 00191 } // end namespace llvm 00192 00193 #endif // LLVM_ANALYSIS_SPARSE_PROPAGATION_H
This web site is hosted by the Computer Science Department at the University of Illinois at Urbana-Champaign.