LazyValueInfo.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- LazyValueInfo.h - Value constraint analysis --------------*- 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. // This file defines the interface for lazy computation of value constraint
  15. // information.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_ANALYSIS_LAZYVALUEINFO_H
  19. #define LLVM_ANALYSIS_LAZYVALUEINFO_H
  20. #include "llvm/IR/PassManager.h"
  21. #include "llvm/Pass.h"
  22. namespace llvm {
  23. class AssumptionCache;
  24. class Constant;
  25. class ConstantRange;
  26. class DataLayout;
  27. class DominatorTree;
  28. class Instruction;
  29. class TargetLibraryInfo;
  30. class Value;
  31. /// This pass computes, caches, and vends lazy value constraint information.
  32. class LazyValueInfo {
  33. friend class LazyValueInfoWrapperPass;
  34. AssumptionCache *AC = nullptr;
  35. const DataLayout *DL = nullptr;
  36. class TargetLibraryInfo *TLI = nullptr;
  37. void *PImpl = nullptr;
  38. LazyValueInfo(const LazyValueInfo&) = delete;
  39. void operator=(const LazyValueInfo&) = delete;
  40. public:
  41. ~LazyValueInfo();
  42. LazyValueInfo() = default;
  43. LazyValueInfo(AssumptionCache *AC_, const DataLayout *DL_,
  44. TargetLibraryInfo *TLI_)
  45. : AC(AC_), DL(DL_), TLI(TLI_) {}
  46. LazyValueInfo(LazyValueInfo &&Arg)
  47. : AC(Arg.AC), DL(Arg.DL), TLI(Arg.TLI), PImpl(Arg.PImpl) {
  48. Arg.PImpl = nullptr;
  49. }
  50. LazyValueInfo &operator=(LazyValueInfo &&Arg) {
  51. releaseMemory();
  52. AC = Arg.AC;
  53. DL = Arg.DL;
  54. TLI = Arg.TLI;
  55. PImpl = Arg.PImpl;
  56. Arg.PImpl = nullptr;
  57. return *this;
  58. }
  59. /// This is used to return true/false/dunno results.
  60. enum Tristate {
  61. Unknown = -1, False = 0, True = 1
  62. };
  63. // Public query interface.
  64. /// Determine whether the specified value comparison with a constant is known
  65. /// to be true or false on the specified CFG edge.
  66. /// Pred is a CmpInst predicate.
  67. Tristate getPredicateOnEdge(unsigned Pred, Value *V, Constant *C,
  68. BasicBlock *FromBB, BasicBlock *ToBB,
  69. Instruction *CxtI = nullptr);
  70. /// Determine whether the specified value comparison with a constant is known
  71. /// to be true or false at the specified instruction.
  72. /// \p Pred is a CmpInst predicate. If \p UseBlockValue is true, the block
  73. /// value is also taken into account.
  74. Tristate getPredicateAt(unsigned Pred, Value *V, Constant *C,
  75. Instruction *CxtI, bool UseBlockValue);
  76. /// Determine whether the specified value comparison is known to be true
  77. /// or false at the specified instruction. While this takes two Value's,
  78. /// it still requires that one of them is a constant.
  79. /// \p Pred is a CmpInst predicate.
  80. /// If \p UseBlockValue is true, the block value is also taken into account.
  81. Tristate getPredicateAt(unsigned Pred, Value *LHS, Value *RHS,
  82. Instruction *CxtI, bool UseBlockValue);
  83. /// Determine whether the specified value is known to be a constant at the
  84. /// specified instruction. Return null if not.
  85. Constant *getConstant(Value *V, Instruction *CxtI);
  86. /// Return the ConstantRange constraint that is known to hold for the
  87. /// specified value at the specified instruction. This may only be called
  88. /// on integer-typed Values.
  89. ConstantRange getConstantRange(Value *V, Instruction *CxtI,
  90. bool UndefAllowed = true);
  91. /// Return the ConstantRange constraint that is known to hold for the value
  92. /// at a specific use-site.
  93. ConstantRange getConstantRangeAtUse(const Use &U, bool UndefAllowed = true);
  94. /// Determine whether the specified value is known to be a
  95. /// constant on the specified edge. Return null if not.
  96. Constant *getConstantOnEdge(Value *V, BasicBlock *FromBB, BasicBlock *ToBB,
  97. Instruction *CxtI = nullptr);
  98. /// Return the ConstantRage constraint that is known to hold for the
  99. /// specified value on the specified edge. This may be only be called
  100. /// on integer-typed Values.
  101. ConstantRange getConstantRangeOnEdge(Value *V, BasicBlock *FromBB,
  102. BasicBlock *ToBB,
  103. Instruction *CxtI = nullptr);
  104. /// Inform the analysis cache that we have threaded an edge from
  105. /// PredBB to OldSucc to be from PredBB to NewSucc instead.
  106. void threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc, BasicBlock *NewSucc);
  107. /// Inform the analysis cache that we have erased a block.
  108. void eraseBlock(BasicBlock *BB);
  109. /// Complete flush all previously computed values
  110. void clear(const Module *M);
  111. /// Print the \LazyValueInfo Analysis.
  112. /// We pass in the DTree that is required for identifying which basic blocks
  113. /// we can solve/print for, in the LVIPrinter.
  114. void printLVI(Function &F, DominatorTree &DTree, raw_ostream &OS);
  115. // For old PM pass. Delete once LazyValueInfoWrapperPass is gone.
  116. void releaseMemory();
  117. /// Handle invalidation events in the new pass manager.
  118. bool invalidate(Function &F, const PreservedAnalyses &PA,
  119. FunctionAnalysisManager::Invalidator &Inv);
  120. };
  121. /// Analysis to compute lazy value information.
  122. class LazyValueAnalysis : public AnalysisInfoMixin<LazyValueAnalysis> {
  123. public:
  124. typedef LazyValueInfo Result;
  125. Result run(Function &F, FunctionAnalysisManager &FAM);
  126. private:
  127. static AnalysisKey Key;
  128. friend struct AnalysisInfoMixin<LazyValueAnalysis>;
  129. };
  130. /// Wrapper around LazyValueInfo.
  131. class LazyValueInfoWrapperPass : public FunctionPass {
  132. LazyValueInfoWrapperPass(const LazyValueInfoWrapperPass&) = delete;
  133. void operator=(const LazyValueInfoWrapperPass&) = delete;
  134. public:
  135. static char ID;
  136. LazyValueInfoWrapperPass();
  137. ~LazyValueInfoWrapperPass() override {
  138. assert(!Info.PImpl && "releaseMemory not called");
  139. }
  140. LazyValueInfo &getLVI();
  141. void getAnalysisUsage(AnalysisUsage &AU) const override;
  142. void releaseMemory() override;
  143. bool runOnFunction(Function &F) override;
  144. private:
  145. LazyValueInfo Info;
  146. };
  147. } // end namespace llvm
  148. #endif
  149. #ifdef __GNUC__
  150. #pragma GCC diagnostic pop
  151. #endif