LazyValueInfo.h 5.6 KB

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