SCCPSolver.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- SCCPSolver.h - SCCP Utility ----------------------------- *- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // \file
  15. // This file implements Sparse Conditional Constant Propagation (SCCP) utility.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_TRANSFORMS_UTILS_SCCPSOLVER_H
  19. #define LLVM_TRANSFORMS_UTILS_SCCPSOLVER_H
  20. #include "llvm/ADT/MapVector.h"
  21. #include "llvm/ADT/SmallPtrSet.h"
  22. #include "llvm/ADT/Statistic.h"
  23. #include "llvm/Analysis/DomTreeUpdater.h"
  24. #include "llvm/Transforms/Utils/PredicateInfo.h"
  25. #include <vector>
  26. namespace llvm {
  27. class Argument;
  28. class BasicBlock;
  29. class CallInst;
  30. class Constant;
  31. class DataLayout;
  32. class DominatorTree;
  33. class Function;
  34. class GlobalVariable;
  35. class Instruction;
  36. class LLVMContext;
  37. class LoopInfo;
  38. class PostDominatorTree;
  39. class StructType;
  40. class TargetLibraryInfo;
  41. class Value;
  42. class ValueLatticeElement;
  43. /// Helper struct for bundling up the analysis results per function for IPSCCP.
  44. struct AnalysisResultsForFn {
  45. std::unique_ptr<PredicateInfo> PredInfo;
  46. DominatorTree *DT;
  47. PostDominatorTree *PDT;
  48. LoopInfo *LI;
  49. };
  50. /// Helper struct shared between Function Specialization and SCCP Solver.
  51. struct ArgInfo {
  52. Argument *Formal; // The Formal argument being analysed.
  53. Constant *Actual; // A corresponding actual constant argument.
  54. ArgInfo(Argument *F, Constant *A) : Formal(F), Actual(A) {}
  55. bool operator==(const ArgInfo &Other) const {
  56. return Formal == Other.Formal && Actual == Other.Actual;
  57. }
  58. bool operator!=(const ArgInfo &Other) const { return !(*this == Other); }
  59. friend hash_code hash_value(const ArgInfo &A) {
  60. return hash_combine(hash_value(A.Formal), hash_value(A.Actual));
  61. }
  62. };
  63. class SCCPInstVisitor;
  64. //===----------------------------------------------------------------------===//
  65. //
  66. /// SCCPSolver - This interface class is a general purpose solver for Sparse
  67. /// Conditional Constant Propagation (SCCP).
  68. ///
  69. class SCCPSolver {
  70. std::unique_ptr<SCCPInstVisitor> Visitor;
  71. public:
  72. SCCPSolver(const DataLayout &DL,
  73. std::function<const TargetLibraryInfo &(Function &)> GetTLI,
  74. LLVMContext &Ctx);
  75. ~SCCPSolver();
  76. void addAnalysis(Function &F, AnalysisResultsForFn A);
  77. /// markBlockExecutable - This method can be used by clients to mark all of
  78. /// the blocks that are known to be intrinsically live in the processed unit.
  79. /// This returns true if the block was not considered live before.
  80. bool markBlockExecutable(BasicBlock *BB);
  81. const PredicateBase *getPredicateInfoFor(Instruction *I);
  82. const LoopInfo &getLoopInfo(Function &F);
  83. DomTreeUpdater getDTU(Function &F);
  84. /// trackValueOfGlobalVariable - Clients can use this method to
  85. /// inform the SCCPSolver that it should track loads and stores to the
  86. /// specified global variable if it can. This is only legal to call if
  87. /// performing Interprocedural SCCP.
  88. void trackValueOfGlobalVariable(GlobalVariable *GV);
  89. /// addTrackedFunction - If the SCCP solver is supposed to track calls into
  90. /// and out of the specified function (which cannot have its address taken),
  91. /// this method must be called.
  92. void addTrackedFunction(Function *F);
  93. /// Add function to the list of functions whose return cannot be modified.
  94. void addToMustPreserveReturnsInFunctions(Function *F);
  95. /// Returns true if the return of the given function cannot be modified.
  96. bool mustPreserveReturn(Function *F);
  97. void addArgumentTrackedFunction(Function *F);
  98. /// Returns true if the given function is in the solver's set of
  99. /// argument-tracked functions.
  100. bool isArgumentTrackedFunction(Function *F);
  101. /// Solve - Solve for constants and executable blocks.
  102. void solve();
  103. /// resolvedUndefsIn - While solving the dataflow for a function, we assume
  104. /// that branches on undef values cannot reach any of their successors.
  105. /// However, this is not a safe assumption. After we solve dataflow, this
  106. /// method should be use to handle this. If this returns true, the solver
  107. /// should be rerun.
  108. bool resolvedUndefsIn(Function &F);
  109. void solveWhileResolvedUndefsIn(Module &M);
  110. void solveWhileResolvedUndefsIn(SmallVectorImpl<Function *> &WorkList);
  111. bool isBlockExecutable(BasicBlock *BB) const;
  112. // isEdgeFeasible - Return true if the control flow edge from the 'From' basic
  113. // block to the 'To' basic block is currently feasible.
  114. bool isEdgeFeasible(BasicBlock *From, BasicBlock *To) const;
  115. std::vector<ValueLatticeElement> getStructLatticeValueFor(Value *V) const;
  116. void removeLatticeValueFor(Value *V);
  117. const ValueLatticeElement &getLatticeValueFor(Value *V) const;
  118. /// getTrackedRetVals - Get the inferred return value map.
  119. const MapVector<Function *, ValueLatticeElement> &getTrackedRetVals();
  120. /// getTrackedGlobals - Get and return the set of inferred initializers for
  121. /// global variables.
  122. const DenseMap<GlobalVariable *, ValueLatticeElement> &getTrackedGlobals();
  123. /// getMRVFunctionsTracked - Get the set of functions which return multiple
  124. /// values tracked by the pass.
  125. const SmallPtrSet<Function *, 16> getMRVFunctionsTracked();
  126. /// markOverdefined - Mark the specified value overdefined. This
  127. /// works with both scalars and structs.
  128. void markOverdefined(Value *V);
  129. // isStructLatticeConstant - Return true if all the lattice values
  130. // corresponding to elements of the structure are constants,
  131. // false otherwise.
  132. bool isStructLatticeConstant(Function *F, StructType *STy);
  133. /// Helper to return a Constant if \p LV is either a constant or a constant
  134. /// range with a single element.
  135. Constant *getConstant(const ValueLatticeElement &LV) const;
  136. /// Return a reference to the set of argument tracked functions.
  137. SmallPtrSetImpl<Function *> &getArgumentTrackedFunctions();
  138. /// Mark the constant arguments of a new function specialization. \p F points
  139. /// to the cloned function and \p Args contains a list of constant arguments
  140. /// represented as pairs of {formal,actual} values (the formal argument is
  141. /// associated with the original function definition). All other arguments of
  142. /// the specialization inherit the lattice state of their corresponding values
  143. /// in the original function.
  144. void markArgInFuncSpecialization(Function *F,
  145. const SmallVectorImpl<ArgInfo> &Args);
  146. /// Mark all of the blocks in function \p F non-executable. Clients can used
  147. /// this method to erase a function from the module (e.g., if it has been
  148. /// completely specialized and is no longer needed).
  149. void markFunctionUnreachable(Function *F);
  150. void visit(Instruction *I);
  151. void visitCall(CallInst &I);
  152. bool simplifyInstsInBlock(BasicBlock &BB,
  153. SmallPtrSetImpl<Value *> &InsertedValues,
  154. Statistic &InstRemovedStat,
  155. Statistic &InstReplacedStat);
  156. bool removeNonFeasibleEdges(BasicBlock *BB, DomTreeUpdater &DTU,
  157. BasicBlock *&NewUnreachableBB) const;
  158. bool tryToReplaceWithConstant(Value *V);
  159. // Helper to check if \p LV is either a constant or a constant
  160. // range with a single element. This should cover exactly the same cases as
  161. // the old ValueLatticeElement::isConstant() and is intended to be used in the
  162. // transition to ValueLatticeElement.
  163. static bool isConstant(const ValueLatticeElement &LV);
  164. // Helper to check if \p LV is either overdefined or a constant range with
  165. // more than a single element. This should cover exactly the same cases as the
  166. // old ValueLatticeElement::isOverdefined() and is intended to be used in the
  167. // transition to ValueLatticeElement.
  168. static bool isOverdefined(const ValueLatticeElement &LV);
  169. };
  170. } // namespace llvm
  171. #endif // LLVM_TRANSFORMS_UTILS_SCCPSOLVER_H
  172. #ifdef __GNUC__
  173. #pragma GCC diagnostic pop
  174. #endif