Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:14

0001 //===- LazyValueInfo.h - Value constraint analysis --------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file defines the interface for lazy computation of value constraint
0010 // information.
0011 //
0012 //===----------------------------------------------------------------------===//
0013 
0014 #ifndef LLVM_ANALYSIS_LAZYVALUEINFO_H
0015 #define LLVM_ANALYSIS_LAZYVALUEINFO_H
0016 
0017 #include "llvm/IR/InstrTypes.h"
0018 #include "llvm/IR/PassManager.h"
0019 #include "llvm/Pass.h"
0020 
0021 namespace llvm {
0022   class AssumptionCache;
0023   class BasicBlock;
0024   class Constant;
0025   class DataLayout;
0026   class DominatorTree;
0027   class Instruction;
0028   class Value;
0029   class Use;
0030   class LazyValueInfoImpl;
0031   /// This pass computes, caches, and vends lazy value constraint information.
0032   class LazyValueInfo {
0033     friend class LazyValueInfoWrapperPass;
0034     AssumptionCache *AC = nullptr;
0035     const DataLayout *DL = nullptr;
0036     LazyValueInfoImpl *PImpl = nullptr;
0037     LazyValueInfo(const LazyValueInfo &) = delete;
0038     void operator=(const LazyValueInfo &) = delete;
0039 
0040     LazyValueInfoImpl *getImpl();
0041     LazyValueInfoImpl &getOrCreateImpl(const Module *M);
0042 
0043   public:
0044     ~LazyValueInfo();
0045     LazyValueInfo() = default;
0046     LazyValueInfo(AssumptionCache *AC_, const DataLayout *DL_)
0047         : AC(AC_), DL(DL_) {}
0048     LazyValueInfo(LazyValueInfo &&Arg)
0049         : AC(Arg.AC), DL(Arg.DL), PImpl(Arg.PImpl) {
0050       Arg.PImpl = nullptr;
0051     }
0052     LazyValueInfo &operator=(LazyValueInfo &&Arg) {
0053       releaseMemory();
0054       AC = Arg.AC;
0055       DL = Arg.DL;
0056       PImpl = Arg.PImpl;
0057       Arg.PImpl = nullptr;
0058       return *this;
0059     }
0060 
0061     // Public query interface.
0062 
0063     /// Determine whether the specified value comparison with a constant is
0064     /// known to be true or false on the specified CFG edge. Pred is a CmpInst
0065     /// predicate.
0066     Constant *getPredicateOnEdge(CmpInst::Predicate Pred, Value *V, Constant *C,
0067                                  BasicBlock *FromBB, BasicBlock *ToBB,
0068                                  Instruction *CxtI = nullptr);
0069 
0070     /// Determine whether the specified value comparison with a constant is
0071     /// known to be true or false at the specified instruction. \p Pred is a
0072     /// CmpInst predicate. If \p UseBlockValue is true, the block value is also
0073     /// taken into account.
0074     Constant *getPredicateAt(CmpInst::Predicate Pred, Value *V, Constant *C,
0075                              Instruction *CxtI, bool UseBlockValue);
0076 
0077     /// Determine whether the specified value comparison is known to be true
0078     /// or false at the specified instruction. While this takes two Value's,
0079     /// it still requires that one of them is a constant.
0080     /// \p Pred is a CmpInst predicate.
0081     /// If \p UseBlockValue is true, the block value is also taken into account.
0082     Constant *getPredicateAt(CmpInst::Predicate Pred, Value *LHS, Value *RHS,
0083                              Instruction *CxtI, bool UseBlockValue);
0084 
0085     /// Determine whether the specified value is known to be a constant at the
0086     /// specified instruction. Return null if not.
0087     Constant *getConstant(Value *V, Instruction *CxtI);
0088 
0089     /// Return the ConstantRange constraint that is known to hold for the
0090     /// specified value at the specified instruction. This may only be called
0091     /// on integer-typed Values.
0092     ConstantRange getConstantRange(Value *V, Instruction *CxtI,
0093                                    bool UndefAllowed);
0094 
0095     /// Return the ConstantRange constraint that is known to hold for the value
0096     /// at a specific use-site.
0097     ConstantRange getConstantRangeAtUse(const Use &U, bool UndefAllowed);
0098 
0099     /// Determine whether the specified value is known to be a
0100     /// constant on the specified edge.  Return null if not.
0101     Constant *getConstantOnEdge(Value *V, BasicBlock *FromBB, BasicBlock *ToBB,
0102                                 Instruction *CxtI = nullptr);
0103 
0104     /// Return the ConstantRage constraint that is known to hold for the
0105     /// specified value on the specified edge. This may be only be called
0106     /// on integer-typed Values.
0107     ConstantRange getConstantRangeOnEdge(Value *V, BasicBlock *FromBB,
0108                                          BasicBlock *ToBB,
0109                                          Instruction *CxtI = nullptr);
0110 
0111     /// Inform the analysis cache that we have threaded an edge from
0112     /// PredBB to OldSucc to be from PredBB to NewSucc instead.
0113     void threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc,
0114                     BasicBlock *NewSucc);
0115 
0116     /// Remove information related to this value from the cache.
0117     void forgetValue(Value *V);
0118 
0119     /// Inform the analysis cache that we have erased a block.
0120     void eraseBlock(BasicBlock *BB);
0121 
0122     /// Complete flush all previously computed values
0123     void clear();
0124 
0125     /// Print the \LazyValueInfo Analysis.
0126     /// We pass in the DTree that is required for identifying which basic blocks
0127     /// we can solve/print for, in the LVIPrinter.
0128     void printLVI(Function &F, DominatorTree &DTree, raw_ostream &OS);
0129 
0130     // For old PM pass. Delete once LazyValueInfoWrapperPass is gone.
0131     void releaseMemory();
0132 
0133     /// Handle invalidation events in the new pass manager.
0134     bool invalidate(Function &F, const PreservedAnalyses &PA,
0135                     FunctionAnalysisManager::Invalidator &Inv);
0136   };
0137 
0138 /// Analysis to compute lazy value information.
0139 class LazyValueAnalysis : public AnalysisInfoMixin<LazyValueAnalysis> {
0140 public:
0141   typedef LazyValueInfo Result;
0142   Result run(Function &F, FunctionAnalysisManager &FAM);
0143 
0144 private:
0145   static AnalysisKey Key;
0146   friend struct AnalysisInfoMixin<LazyValueAnalysis>;
0147 };
0148 
0149 /// Printer pass for the LazyValueAnalysis results.
0150 class LazyValueInfoPrinterPass
0151     : public PassInfoMixin<LazyValueInfoPrinterPass> {
0152   raw_ostream &OS;
0153 
0154 public:
0155   explicit LazyValueInfoPrinterPass(raw_ostream &OS) : OS(OS) {}
0156 
0157   PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
0158 
0159   static bool isRequired() { return true; }
0160 };
0161 
0162 /// Wrapper around LazyValueInfo.
0163 class LazyValueInfoWrapperPass : public FunctionPass {
0164   LazyValueInfoWrapperPass(const LazyValueInfoWrapperPass&) = delete;
0165   void operator=(const LazyValueInfoWrapperPass&) = delete;
0166 public:
0167   static char ID;
0168   LazyValueInfoWrapperPass();
0169   ~LazyValueInfoWrapperPass() override {
0170     assert(!Info.PImpl && "releaseMemory not called");
0171   }
0172 
0173   LazyValueInfo &getLVI();
0174 
0175   void getAnalysisUsage(AnalysisUsage &AU) const override;
0176   void releaseMemory() override;
0177   bool runOnFunction(Function &F) override;
0178 private:
0179   LazyValueInfo Info;
0180 };
0181 
0182 }  // end namespace llvm
0183 
0184 #endif
0185