25 using namespace llvm::PatternMatch;
29 cl::desc(
"Enable verification of assumption cache"),
33 AssumptionCache::getOrInsertAffectedValues(
Value *V) {
36 auto AVI = AffectedValues.find_as(V);
37 if (AVI != AffectedValues.end())
40 auto AVIP = AffectedValues.
insert(
42 return AVIP.first->second;
50 auto AddAffected = [&Affected](
Value *V) {
51 if (isa<Argument>(V)) {
53 }
else if (
auto *
I = dyn_cast<Instruction>(V)) {
61 if (isa<Instruction>(Op) || isa<Argument>(Op))
77 auto AddAffectedFromEq = [&AddAffected](
Value *V) {
101 for (
auto &AV : Affected) {
102 auto &AVV = getOrInsertAffectedValues(AV);
103 if (
std::find(AVV.begin(), AVV.end(), CI) == AVV.end())
108 void AssumptionCache::AffectedValueCallbackVH::deleted() {
109 auto AVI = AC->AffectedValues.find(getValPtr());
110 if (AVI != AC->AffectedValues.end())
111 AC->AffectedValues.erase(AVI);
115 void AssumptionCache::copyAffectedValuesInCache(
Value *OV,
Value *
NV) {
116 auto &NAVV = getOrInsertAffectedValues(NV);
117 auto AVI = AffectedValues.find(OV);
118 if (AVI == AffectedValues.end())
121 for (
auto &
A : AVI->second)
122 if (
std::find(NAVV.begin(), NAVV.end(),
A) == NAVV.end())
126 void AssumptionCache::AffectedValueCallbackVH::allUsesReplacedWith(
Value *NV) {
127 if (!isa<Instruction>(NV) && !isa<Argument>(NV))
132 AC->copyAffectedValuesInCache(getValPtr(), NV);
138 void AssumptionCache::scanFunction() {
139 assert(!Scanned &&
"Tried to scan the function twice!");
140 assert(AssumeHandles.empty() &&
"Already have assumes when scanning!");
146 if (
match(&II, m_Intrinsic<Intrinsic::assume>()))
147 AssumeHandles.push_back(&II);
153 for (
auto &
A : AssumeHandles)
154 updateAffectedValues(cast<CallInst>(
A));
158 assert(
match(CI, m_Intrinsic<Intrinsic::assume>()) &&
159 "Registered call does not call @llvm.assume");
166 AssumeHandles.push_back(CI);
170 "Cannot register @llvm.assume call not in a basic block");
172 "Cannot register @llvm.assume call not in this function");
178 for (
auto &VH : AssumeHandles) {
183 "Cached assumption not inside this function!");
184 assert(
match(cast<CallInst>(VH), m_Intrinsic<Intrinsic::assume>()) &&
185 "Cached something other than a call to @llvm.assume!");
186 assert(AssumptionSet.insert(VH).second &&
187 "Cache contains multiple copies of a call!");
191 updateAffectedValues(CI);
200 OS <<
"Cached assumptions for function: " << F.
getName() <<
"\n";
203 OS <<
" " << *cast<CallInst>(VH)->getArgOperand(0) <<
"\n";
208 void AssumptionCacheTracker::FunctionCallbackVH::deleted() {
209 auto I = ACT->AssumptionCaches.find_as(cast<Function>(getValPtr()));
210 if (
I != ACT->AssumptionCaches.end())
211 ACT->AssumptionCaches.erase(
I);
220 auto I = AssumptionCaches.find_as(&F);
221 if (
I != AssumptionCaches.end())
226 auto IP = AssumptionCaches.insert(std::make_pair(
227 FunctionCallbackVH(&F,
this), llvm::make_unique<AssumptionCache>(F)));
228 assert(IP.second &&
"Scanning function already in the map?");
229 return *IP.first->second;
241 for (
const auto &
I : AssumptionCaches) {
242 for (
auto &VH :
I.second->assumptions())
244 AssumptionSet.
insert(cast<CallInst>(VH));
246 for (
const BasicBlock &B : cast<Function>(*
I.first))
248 if (
match(&II, m_Intrinsic<Intrinsic::assume>()) &&
249 !AssumptionSet.
count(cast<CallInst>(&II)))
261 "Assumption Cache Tracker",
false,
true)
void push_back(const T &Elt)
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
DiagnosticInfoOptimizationBase::Argument NV
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
This class represents a function call, abstracting a target machine's calling convention.
An immutable pass that tracks lazily created AssumptionCache objects.
A cache of .assume calls within a function.
const Function * getParent() const
Return the enclosing method, or null if none.
MutableArrayRef< WeakTrackingVH > assumptions()
Access the list of assumption handles currently tracked for this function.
StringRef getName() const
Return a constant reference to the value's name.
bool match(Val *V, const Pattern &P)
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs...ExtraArgs)
Get the result of an analysis pass for a given IR unit.
not_match< LHS > m_Not(const LHS &L)
class_match< ConstantInt > m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
initializer< Ty > init(const Ty &Val)
A set of analyses that are preserved following a run of a transformation pass.
LLVM Basic Block Representation.
CastClass_match< OpTy, Instruction::BitCast > m_BitCast(const OpTy &Op)
Matches BitCast.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
BinOpPred_match< LHS, RHS, is_bitwiselogic_op > m_BitwiseLogic(const LHS &L, const RHS &R)
Matches bitwise logic operations.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
A function analysis which provides an AssumptionCache.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
This is the shared class of boolean and integer constants.
ImmutablePass class - This class is used to provide information that does not need to be run...
auto find(R &&Range, const T &Val) -> decltype(std::begin(Range))
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly...
~AssumptionCacheTracker() override
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
void updateAffectedValues(CallInst *CI)
Update the cache of values being affected by this assumption (i.e.
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Value * getArgOperand(unsigned i) const
getArgOperand/setArgOperand - Return/set the i-th call argument.
CastClass_match< OpTy, Instruction::PtrToInt > m_PtrToInt(const OpTy &Op)
Matches PtrToInt.
BinOpPred_match< LHS, RHS, is_shift_op > m_Shift(const LHS &L, const RHS &R)
Matches shift operations.
iterator insert(iterator I, T &&Elt)
void registerAssumption(CallInst *CI)
Add an .assume intrinsic to this function's cache.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
static const Function * getParent(const Value *V)
static cl::opt< bool > VerifyAssumptionCache("verify-assumption-cache", cl::Hidden, cl::desc("Enable verification of assumption cache"), cl::init(false))
AssumptionCache & getAssumptionCache(Function &F)
Get the cached assumptions for a function.
A container for analyses that lazily runs them and caches their results.
void initializeAssumptionCacheTrackerPass(PassRegistry &)
void verifyAnalysis() const override
verifyAnalysis() - This member can be implemented by a analysis pass to check state of analysis infor...
This header defines various interfaces for pass management in LLVM.
A special type used by analysis passes to provide an address that identifies that particular analysis...
const BasicBlock * getParent() const
CmpClass_match< LHS, RHS, ICmpInst, ICmpInst::Predicate > m_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R)