LazyValueInfo.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. /// Determine whether the specified value is known to be a
  92. /// constant on the specified edge. Return null if not.
  93. Constant *getConstantOnEdge(Value *V, BasicBlock *FromBB, BasicBlock *ToBB,
  94. Instruction *CxtI = nullptr);
  95. /// Return the ConstantRage constraint that is known to hold for the
  96. /// specified value on the specified edge. This may be only be called
  97. /// on integer-typed Values.
  98. ConstantRange getConstantRangeOnEdge(Value *V, BasicBlock *FromBB,
  99. BasicBlock *ToBB,
  100. Instruction *CxtI = nullptr);
  101. /// Inform the analysis cache that we have threaded an edge from
  102. /// PredBB to OldSucc to be from PredBB to NewSucc instead.
  103. void threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc, BasicBlock *NewSucc);
  104. /// Inform the analysis cache that we have erased a block.
  105. void eraseBlock(BasicBlock *BB);
  106. /// Print the \LazyValueInfo Analysis.
  107. /// We pass in the DTree that is required for identifying which basic blocks
  108. /// we can solve/print for, in the LVIPrinter.
  109. void printLVI(Function &F, DominatorTree &DTree, raw_ostream &OS);
  110. // For old PM pass. Delete once LazyValueInfoWrapperPass is gone.
  111. void releaseMemory();
  112. /// Handle invalidation events in the new pass manager.
  113. bool invalidate(Function &F, const PreservedAnalyses &PA,
  114. FunctionAnalysisManager::Invalidator &Inv);
  115. };
  116. /// Analysis to compute lazy value information.
  117. class LazyValueAnalysis : public AnalysisInfoMixin<LazyValueAnalysis> {
  118. public:
  119. typedef LazyValueInfo Result;
  120. Result run(Function &F, FunctionAnalysisManager &FAM);
  121. private:
  122. static AnalysisKey Key;
  123. friend struct AnalysisInfoMixin<LazyValueAnalysis>;
  124. };
  125. /// Wrapper around LazyValueInfo.
  126. class LazyValueInfoWrapperPass : public FunctionPass {
  127. LazyValueInfoWrapperPass(const LazyValueInfoWrapperPass&) = delete;
  128. void operator=(const LazyValueInfoWrapperPass&) = delete;
  129. public:
  130. static char ID;
  131. LazyValueInfoWrapperPass();
  132. ~LazyValueInfoWrapperPass() override {
  133. assert(!Info.PImpl && "releaseMemory not called");
  134. }
  135. LazyValueInfo &getLVI();
  136. void getAnalysisUsage(AnalysisUsage &AU) const override;
  137. void releaseMemory() override;
  138. bool runOnFunction(Function &F) override;
  139. private:
  140. LazyValueInfo Info;
  141. };
  142. } // end namespace llvm
  143. #endif
  144. #ifdef __GNUC__
  145. #pragma GCC diagnostic pop
  146. #endif