1 //===- llvm/Analysis/DomConditionCache.h ------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Cache for branch conditions that affect a certain value for use by
10 // ValueTracking. Unlike AssumptionCache, this class does not perform any
11 // automatic analysis or invalidation. The caller is responsible for registering
12 // all relevant branches (and re-registering them if they change), and for
13 // removing invalidated values from the cache.
14 //
15 //===----------------------------------------------------------------------===//
16 
17 #ifndef LLVM_ANALYSIS_DOMCONDITIONCACHE_H
18 #define LLVM_ANALYSIS_DOMCONDITIONCACHE_H
19 
20 #include "llvm/ADT/ArrayRef.h"
21 #include "llvm/ADT/DenseMap.h"
22 #include "llvm/ADT/DenseMapInfo.h"
23 #include "llvm/ADT/SmallVector.h"
24 #include "llvm/IR/ValueHandle.h"
25 
26 namespace llvm {
27 
28 class Value;
29 class BranchInst;
30 
31 class DomConditionCache {
32 private:
33   /// A map of values about which a branch might be providing information.
34   using AffectedValuesMap = DenseMap<Value *, SmallVector<BranchInst *, 1>>;
35   AffectedValuesMap AffectedValues;
36 
37 public:
38   /// Add a branch condition to the cache.
39   void registerBranch(BranchInst *BI);
40 
41   /// Remove a value from the cache, e.g. because it will be erased.
removeValue(Value * V)42   void removeValue(Value *V) { AffectedValues.erase(V); }
43 
44   /// Access the list of branches which affect this value.
conditionsFor(const Value * V)45   ArrayRef<BranchInst *> conditionsFor(const Value *V) const {
46     auto AVI = AffectedValues.find_as(const_cast<Value *>(V));
47     if (AVI == AffectedValues.end())
48       return ArrayRef<BranchInst *>();
49 
50     return AVI->second;
51   }
52 };
53 
54 } // end namespace llvm
55 
56 #endif // LLVM_ANALYSIS_DOMCONDITIONCACHE_H
57