1 //===- LazyValueInfo.h - Value constraint analysis --------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the interface for lazy computation of value constraint
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_ANALYSIS_LAZYVALUEINFO_H
16 #define LLVM_ANALYSIS_LAZYVALUEINFO_H
18 #include "llvm/IR/PassManager.h"
19 #include "llvm/Pass.h"
22 class AssumptionCache;
28 class TargetLibraryInfo;
31 /// This pass computes, caches, and vends lazy value constraint information.
33 friend class LazyValueInfoWrapperPass;
34 AssumptionCache *AC = nullptr;
35 const DataLayout *DL = nullptr;
36 class TargetLibraryInfo *TLI = nullptr;
37 DominatorTree *DT = nullptr;
38 void *PImpl = nullptr;
39 LazyValueInfo(const LazyValueInfo&) = delete;
40 void operator=(const LazyValueInfo&) = delete;
44 LazyValueInfo(AssumptionCache *AC_, const DataLayout *DL_, TargetLibraryInfo *TLI_,
46 : AC(AC_), DL(DL_), TLI(TLI_), DT(DT_) {}
47 LazyValueInfo(LazyValueInfo &&Arg)
48 : AC(Arg.AC), DL(Arg.DL), TLI(Arg.TLI), DT(Arg.DT), PImpl(Arg.PImpl) {
51 LazyValueInfo &operator=(LazyValueInfo &&Arg) {
62 /// This is used to return true/false/dunno results.
64 Unknown = -1, False = 0, True = 1
67 // Public query interface.
69 /// Determine whether the specified value comparison with a constant is known
70 /// to be true or false on the specified CFG edge.
71 /// Pred is a CmpInst predicate.
72 Tristate getPredicateOnEdge(unsigned Pred, Value *V, Constant *C,
73 BasicBlock *FromBB, BasicBlock *ToBB,
74 Instruction *CxtI = nullptr);
76 /// Determine whether the specified value comparison with a constant is known
77 /// to be true or false at the specified instruction
78 /// (from an assume intrinsic). Pred is a CmpInst predicate.
79 Tristate getPredicateAt(unsigned Pred, Value *V, Constant *C,
82 /// Determine whether the specified value is known to be a
83 /// constant at the end of the specified block. Return null if not.
84 Constant *getConstant(Value *V, BasicBlock *BB, Instruction *CxtI = nullptr);
86 /// Return the ConstantRange constraint that is known to hold for the
87 /// specified value at the end of the specified block. This may only be called
88 /// on integer-typed Values.
89 ConstantRange getConstantRange(Value *V, BasicBlock *BB, Instruction *CxtI = nullptr);
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);
96 /// Inform the analysis cache that we have threaded an edge from
97 /// PredBB to OldSucc to be from PredBB to NewSucc instead.
98 void threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc, BasicBlock *NewSucc);
100 /// Inform the analysis cache that we have erased a block.
101 void eraseBlock(BasicBlock *BB);
103 /// Print the \LazyValueInfoCache.
104 void printCache(Function &F, raw_ostream &OS);
106 // For old PM pass. Delete once LazyValueInfoWrapperPass is gone.
107 void releaseMemory();
109 /// Handle invalidation events in the new pass manager.
110 bool invalidate(Function &F, const PreservedAnalyses &PA,
111 FunctionAnalysisManager::Invalidator &Inv);
114 /// \brief Analysis to compute lazy value information.
115 class LazyValueAnalysis : public AnalysisInfoMixin<LazyValueAnalysis> {
117 typedef LazyValueInfo Result;
118 Result run(Function &F, FunctionAnalysisManager &FAM);
121 static AnalysisKey Key;
122 friend struct AnalysisInfoMixin<LazyValueAnalysis>;
125 /// Wrapper around LazyValueInfo.
126 class LazyValueInfoWrapperPass : public FunctionPass {
127 LazyValueInfoWrapperPass(const LazyValueInfoWrapperPass&) = delete;
128 void operator=(const LazyValueInfoWrapperPass&) = delete;
131 LazyValueInfoWrapperPass() : FunctionPass(ID) {
132 initializeLazyValueInfoWrapperPassPass(*PassRegistry::getPassRegistry());
134 ~LazyValueInfoWrapperPass() override {
135 assert(!Info.PImpl && "releaseMemory not called");
138 LazyValueInfo &getLVI();
140 void getAnalysisUsage(AnalysisUsage &AU) const override;
141 void releaseMemory() override;
142 bool runOnFunction(Function &F) override;
147 } // end namespace llvm