Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

206 lines
7.8 KiB

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