Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //===- PhiValues.h - Phi Value 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 PhiValues class, and associated passes, which can be
0010 // used to find the underlying values of the phis in a function, i.e. the
0011 // non-phi values that can be found by traversing the phi graph.
0012 //
0013 // This information is computed lazily and cached. If new phis are added to the
0014 // function they are handled correctly, but if an existing phi has its operands
0015 // modified PhiValues has to be notified by calling invalidateValue.
0016 //
0017 //===----------------------------------------------------------------------===//
0018 
0019 #ifndef LLVM_ANALYSIS_PHIVALUES_H
0020 #define LLVM_ANALYSIS_PHIVALUES_H
0021 
0022 #include "llvm/ADT/DenseMap.h"
0023 #include "llvm/ADT/DenseSet.h"
0024 #include "llvm/ADT/SetVector.h"
0025 #include "llvm/IR/PassManager.h"
0026 #include "llvm/IR/ValueHandle.h"
0027 #include "llvm/Pass.h"
0028 
0029 namespace llvm {
0030 
0031 class Value;
0032 class PHINode;
0033 class Function;
0034 
0035 /// Class for calculating and caching the underlying values of phis in a
0036 /// function.
0037 ///
0038 /// Initially the PhiValues is empty, and gets incrementally populated whenever
0039 /// it is queried.
0040 class PhiValues {
0041 public:
0042   using ValueSet = SmallSetVector<Value *, 4>;
0043 
0044   /// Construct an empty PhiValues.
0045   PhiValues(const Function &F) : F(F) {}
0046 
0047   /// Get the underlying values of a phi.
0048   ///
0049   /// This returns the cached value if PN has previously been processed,
0050   /// otherwise it processes it first.
0051   const ValueSet &getValuesForPhi(const PHINode *PN);
0052 
0053   /// Notify PhiValues that the cached information using V is no longer valid
0054   ///
0055   /// Whenever a phi has its operands modified the cached values for that phi
0056   /// (and the phis that use that phi) become invalid. A user of PhiValues has
0057   /// to notify it of this by calling invalidateValue on either the operand or
0058   /// the phi, which will then clear the relevant cached information.
0059   void invalidateValue(const Value *V);
0060 
0061   /// Free the memory used by this class.
0062   void releaseMemory();
0063 
0064   /// Print out the values currently in the cache.
0065   void print(raw_ostream &OS) const;
0066 
0067   /// Handle invalidation events in the new pass manager.
0068   bool invalidate(Function &, const PreservedAnalyses &,
0069                   FunctionAnalysisManager::Invalidator &);
0070 
0071 private:
0072   using ConstValueSet = SmallSetVector<const Value *, 4>;
0073 
0074   /// The next depth number to be used by processPhi.
0075   unsigned int NextDepthNumber = 1;
0076 
0077   /// Depth numbers of phis. Phis with the same depth number are part of the
0078   /// same strongly connected component.
0079   DenseMap<const PHINode *, unsigned int> DepthMap;
0080 
0081   /// Non-phi values reachable from each component.
0082   DenseMap<unsigned int, ValueSet> NonPhiReachableMap;
0083 
0084   /// All values reachable from each component.
0085   DenseMap<unsigned int, ConstValueSet> ReachableMap;
0086 
0087   /// A CallbackVH to notify PhiValues when a value is deleted or replaced, so
0088   /// that the cached information for that value can be cleared to avoid
0089   /// dangling pointers to invalid values.
0090   class PhiValuesCallbackVH final : public CallbackVH {
0091     PhiValues *PV;
0092     void deleted() override;
0093     void allUsesReplacedWith(Value *New) override;
0094 
0095   public:
0096     PhiValuesCallbackVH(Value *V, PhiValues *PV = nullptr)
0097         : CallbackVH(V), PV(PV) {}
0098   };
0099 
0100   /// A set of callbacks to the values that processPhi has seen.
0101   DenseSet<PhiValuesCallbackVH, DenseMapInfo<Value *>> TrackedValues;
0102 
0103   /// The function that the PhiValues is for.
0104   const Function &F;
0105 
0106   /// Process a phi so that its entries in the depth and reachable maps are
0107   /// fully populated.
0108   void processPhi(const PHINode *PN, SmallVectorImpl<const PHINode *> &Stack);
0109 };
0110 
0111 /// The analysis pass which yields a PhiValues
0112 ///
0113 /// The analysis does nothing by itself, and just returns an empty PhiValues
0114 /// which will get filled in as it's used.
0115 class PhiValuesAnalysis : public AnalysisInfoMixin<PhiValuesAnalysis> {
0116   friend AnalysisInfoMixin<PhiValuesAnalysis>;
0117   static AnalysisKey Key;
0118 
0119 public:
0120   using Result = PhiValues;
0121   PhiValues run(Function &F, FunctionAnalysisManager &);
0122 };
0123 
0124 /// A pass for printing the PhiValues for a function.
0125 ///
0126 /// This pass doesn't print whatever information the PhiValues happens to hold,
0127 /// but instead first uses the PhiValues to analyze all the phis in the function
0128 /// so the complete information is printed.
0129 class PhiValuesPrinterPass : public PassInfoMixin<PhiValuesPrinterPass> {
0130   raw_ostream &OS;
0131 
0132 public:
0133   explicit PhiValuesPrinterPass(raw_ostream &OS) : OS(OS) {}
0134   PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
0135   static bool isRequired() { return true; }
0136 };
0137 
0138 /// Wrapper pass for the legacy pass manager
0139 class PhiValuesWrapperPass : public FunctionPass {
0140   std::unique_ptr<PhiValues> Result;
0141 
0142 public:
0143   static char ID;
0144   PhiValuesWrapperPass();
0145 
0146   PhiValues &getResult() { return *Result; }
0147   const PhiValues &getResult() const { return *Result; }
0148 
0149   bool runOnFunction(Function &F) override;
0150   void releaseMemory() override;
0151   void getAnalysisUsage(AnalysisUsage &AU) const override;
0152 };
0153 
0154 } // namespace llvm
0155 
0156 #endif