LLVM API Documentation

SparsePropagation.h
Go to the documentation of this file.
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_SPARSEPROPAGATION_H
00016 #define LLVM_ANALYSIS_SPARSEPROPAGATION_H
00017 
00018 #include "llvm/ADT/DenseMap.h"
00019 #include "llvm/ADT/SmallPtrSet.h"
00020 #include <set>
00021 #include <vector>
00022 
00023 namespace llvm {
00024   class Value;
00025   class Constant;
00026   class Argument;
00027   class Instruction;
00028   class PHINode;
00029   class TerminatorInst;
00030   class BasicBlock;
00031   class Function;
00032   class SparseSolver;
00033   class raw_ostream;
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   /// IsSpecialCasedPHI - Given a PHI node, determine whether this PHI node is
00076   /// one that the we want to handle through ComputeInstructionState.
00077   virtual bool IsSpecialCasedPHI(PHINode *PN) {
00078     return false;
00079   }
00080   
00081   /// GetConstant - If the specified lattice value is representable as an LLVM
00082   /// constant value, return it.  Otherwise return null.  The returned value
00083   /// must be in the same LLVM type as Val.
00084   virtual Constant *GetConstant(LatticeVal LV, Value *Val, SparseSolver &SS) {
00085     return nullptr;
00086   }
00087 
00088   /// ComputeArgument - Given a formal argument value, compute and return a
00089   /// lattice value corresponding to the specified argument.
00090   virtual LatticeVal ComputeArgument(Argument *I) {
00091     return getOverdefinedVal(); // always safe
00092   }
00093   
00094   /// MergeValues - Compute and return the merge of the two specified lattice
00095   /// values.  Merging should only move one direction down the lattice to
00096   /// guarantee convergence (toward overdefined).
00097   virtual LatticeVal MergeValues(LatticeVal X, LatticeVal Y) {
00098     return getOverdefinedVal(); // always safe, never useful.
00099   }
00100   
00101   /// ComputeInstructionState - Given an instruction and a vector of its operand
00102   /// values, compute the result value of the instruction.
00103   virtual LatticeVal ComputeInstructionState(Instruction &I, SparseSolver &SS) {
00104     return getOverdefinedVal(); // always safe, never useful.
00105   }
00106   
00107   /// PrintValue - Render the specified lattice value to the specified stream.
00108   virtual void PrintValue(LatticeVal V, raw_ostream &OS);
00109 };
00110 
00111   
00112 /// SparseSolver - This class is a general purpose solver for Sparse Conditional
00113 /// Propagation with a programmable lattice function.
00114 ///
00115 class SparseSolver {
00116   typedef AbstractLatticeFunction::LatticeVal LatticeVal;
00117   
00118   /// LatticeFunc - This is the object that knows the lattice and how to do
00119   /// compute transfer functions.
00120   AbstractLatticeFunction *LatticeFunc;
00121   
00122   DenseMap<Value*, LatticeVal> ValueState;  // The state each value is in.
00123   SmallPtrSet<BasicBlock*, 16> BBExecutable;   // The bbs that are executable.
00124   
00125   std::vector<Instruction*> InstWorkList;   // Worklist of insts to process.
00126   
00127   std::vector<BasicBlock*> BBWorkList;  // The BasicBlock work list
00128   
00129   /// KnownFeasibleEdges - Entries in this set are edges which have already had
00130   /// PHI nodes retriggered.
00131   typedef std::pair<BasicBlock*,BasicBlock*> Edge;
00132   std::set<Edge> KnownFeasibleEdges;
00133 
00134   SparseSolver(const SparseSolver&) LLVM_DELETED_FUNCTION;
00135   void operator=(const SparseSolver&) LLVM_DELETED_FUNCTION;
00136 public:
00137   explicit SparseSolver(AbstractLatticeFunction *Lattice)
00138     : LatticeFunc(Lattice) {}
00139   ~SparseSolver() {
00140     delete LatticeFunc;
00141   }
00142   
00143   /// Solve - Solve for constants and executable blocks.
00144   ///
00145   void Solve(Function &F);
00146   
00147   void Print(Function &F, raw_ostream &OS) const;
00148 
00149   /// getLatticeState - Return the LatticeVal object that corresponds to the
00150   /// value.  If an value is not in the map, it is returned as untracked,
00151   /// unlike the getOrInitValueState method.
00152   LatticeVal getLatticeState(Value *V) const {
00153     DenseMap<Value*, LatticeVal>::const_iterator I = ValueState.find(V);
00154     return I != ValueState.end() ? I->second : LatticeFunc->getUntrackedVal();
00155   }
00156   
00157   /// getOrInitValueState - Return the LatticeVal object that corresponds to the
00158   /// value, initializing the value's state if it hasn't been entered into the
00159   /// map yet.   This function is necessary because not all values should start
00160   /// out in the underdefined state... Arguments should be overdefined, and
00161   /// constants should be marked as constants.
00162   ///
00163   LatticeVal getOrInitValueState(Value *V);
00164   
00165   /// isEdgeFeasible - Return true if the control flow edge from the 'From'
00166   /// basic block to the 'To' basic block is currently feasible.  If
00167   /// AggressiveUndef is true, then this treats values with unknown lattice
00168   /// values as undefined.  This is generally only useful when solving the
00169   /// lattice, not when querying it.
00170   bool isEdgeFeasible(BasicBlock *From, BasicBlock *To,
00171                       bool AggressiveUndef = false);
00172 
00173   /// isBlockExecutable - Return true if there are any known feasible
00174   /// edges into the basic block.  This is generally only useful when
00175   /// querying the lattice.
00176   bool isBlockExecutable(BasicBlock *BB) const {
00177     return BBExecutable.count(BB);
00178   }
00179   
00180 private:
00181   /// UpdateState - When the state for some instruction is potentially updated,
00182   /// this function notices and adds I to the worklist if needed.
00183   void UpdateState(Instruction &Inst, LatticeVal V);
00184   
00185   /// MarkBlockExecutable - This method can be used by clients to mark all of
00186   /// the blocks that are known to be intrinsically live in the processed unit.
00187   void MarkBlockExecutable(BasicBlock *BB);
00188   
00189   /// markEdgeExecutable - Mark a basic block as executable, adding it to the BB
00190   /// work list if it is not already executable.
00191   void markEdgeExecutable(BasicBlock *Source, BasicBlock *Dest);
00192   
00193   /// getFeasibleSuccessors - Return a vector of booleans to indicate which
00194   /// successors are reachable from a given terminator instruction.
00195   void getFeasibleSuccessors(TerminatorInst &TI, SmallVectorImpl<bool> &Succs,
00196                              bool AggressiveUndef);
00197   
00198   void visitInst(Instruction &I);
00199   void visitPHINode(PHINode &I);
00200   void visitTerminatorInst(TerminatorInst &TI);
00201 
00202 };
00203 
00204 } // end namespace llvm
00205 
00206 #endif // LLVM_ANALYSIS_SPARSEPROPAGATION_H