SCCPSolver.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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/Analysis/DomTreeUpdater.h"
  22. #include "llvm/Analysis/TargetLibraryInfo.h"
  23. #include "llvm/Analysis/ValueLattice.h"
  24. #include "llvm/Analysis/ValueLatticeUtils.h"
  25. #include "llvm/IR/InstVisitor.h"
  26. #include "llvm/Transforms/Utils/PredicateInfo.h"
  27. #include <cassert>
  28. #include <utility>
  29. #include <vector>
  30. namespace llvm {
  31. /// Helper struct for bundling up the analysis results per function for IPSCCP.
  32. struct AnalysisResultsForFn {
  33. std::unique_ptr<PredicateInfo> PredInfo;
  34. DominatorTree *DT;
  35. PostDominatorTree *PDT;
  36. };
  37. class SCCPInstVisitor;
  38. //===----------------------------------------------------------------------===//
  39. //
  40. /// SCCPSolver - This interface class is a general purpose solver for Sparse
  41. /// Conditional Constant Propagation (SCCP).
  42. ///
  43. class SCCPSolver {
  44. std::unique_ptr<SCCPInstVisitor> Visitor;
  45. public:
  46. SCCPSolver(const DataLayout &DL,
  47. std::function<const TargetLibraryInfo &(Function &)> GetTLI,
  48. LLVMContext &Ctx);
  49. ~SCCPSolver();
  50. void addAnalysis(Function &F, AnalysisResultsForFn A);
  51. /// markBlockExecutable - This method can be used by clients to mark all of
  52. /// the blocks that are known to be intrinsically live in the processed unit.
  53. /// This returns true if the block was not considered live before.
  54. bool markBlockExecutable(BasicBlock *BB);
  55. const PredicateBase *getPredicateInfoFor(Instruction *I);
  56. DomTreeUpdater getDTU(Function &F);
  57. /// trackValueOfGlobalVariable - Clients can use this method to
  58. /// inform the SCCPSolver that it should track loads and stores to the
  59. /// specified global variable if it can. This is only legal to call if
  60. /// performing Interprocedural SCCP.
  61. void trackValueOfGlobalVariable(GlobalVariable *GV);
  62. /// addTrackedFunction - If the SCCP solver is supposed to track calls into
  63. /// and out of the specified function (which cannot have its address taken),
  64. /// this method must be called.
  65. void addTrackedFunction(Function *F);
  66. /// Add function to the list of functions whose return cannot be modified.
  67. void addToMustPreserveReturnsInFunctions(Function *F);
  68. /// Returns true if the return of the given function cannot be modified.
  69. bool mustPreserveReturn(Function *F);
  70. void addArgumentTrackedFunction(Function *F);
  71. /// Returns true if the given function is in the solver's set of
  72. /// argument-tracked functions.
  73. bool isArgumentTrackedFunction(Function *F);
  74. /// Solve - Solve for constants and executable blocks.
  75. void solve();
  76. /// resolvedUndefsIn - While solving the dataflow for a function, we assume
  77. /// that branches on undef values cannot reach any of their successors.
  78. /// However, this is not a safe assumption. After we solve dataflow, this
  79. /// method should be use to handle this. If this returns true, the solver
  80. /// should be rerun.
  81. bool resolvedUndefsIn(Function &F);
  82. bool isBlockExecutable(BasicBlock *BB) const;
  83. // isEdgeFeasible - Return true if the control flow edge from the 'From' basic
  84. // block to the 'To' basic block is currently feasible.
  85. bool isEdgeFeasible(BasicBlock *From, BasicBlock *To) const;
  86. std::vector<ValueLatticeElement> getStructLatticeValueFor(Value *V) const;
  87. void removeLatticeValueFor(Value *V);
  88. const ValueLatticeElement &getLatticeValueFor(Value *V) const;
  89. /// getTrackedRetVals - Get the inferred return value map.
  90. const MapVector<Function *, ValueLatticeElement> &getTrackedRetVals();
  91. /// getTrackedGlobals - Get and return the set of inferred initializers for
  92. /// global variables.
  93. const DenseMap<GlobalVariable *, ValueLatticeElement> &getTrackedGlobals();
  94. /// getMRVFunctionsTracked - Get the set of functions which return multiple
  95. /// values tracked by the pass.
  96. const SmallPtrSet<Function *, 16> getMRVFunctionsTracked();
  97. /// markOverdefined - Mark the specified value overdefined. This
  98. /// works with both scalars and structs.
  99. void markOverdefined(Value *V);
  100. // isStructLatticeConstant - Return true if all the lattice values
  101. // corresponding to elements of the structure are constants,
  102. // false otherwise.
  103. bool isStructLatticeConstant(Function *F, StructType *STy);
  104. /// Helper to return a Constant if \p LV is either a constant or a constant
  105. /// range with a single element.
  106. Constant *getConstant(const ValueLatticeElement &LV) const;
  107. /// Return a reference to the set of argument tracked functions.
  108. SmallPtrSetImpl<Function *> &getArgumentTrackedFunctions();
  109. /// Mark argument \p A constant with value \p C in a new function
  110. /// specialization. The argument's parent function is a specialization of the
  111. /// original function \p F. All other arguments of the specialization inherit
  112. /// the lattice state of their corresponding values in the original function.
  113. void markArgInFuncSpecialization(Function *F, Argument *A, Constant *C);
  114. /// Mark all of the blocks in function \p F non-executable. Clients can used
  115. /// this method to erase a function from the module (e.g., if it has been
  116. /// completely specialized and is no longer needed).
  117. void markFunctionUnreachable(Function *F);
  118. void visit(Instruction *I);
  119. void visitCall(CallInst &I);
  120. };
  121. } // namespace llvm
  122. #endif // LLVM_TRANSFORMS_UTILS_SCCPSOLVER_H
  123. #ifdef __GNUC__
  124. #pragma GCC diagnostic pop
  125. #endif