25 using namespace llvm::PatternMatch;
30 auto AVI = AffectedValues.find_as(V);
31 if (AVI != AffectedValues.end())
34 auto AVIP = AffectedValues.
insert({
36 return AVIP.first->second;
44 auto AddAffected = [&Affected](
Value *V) {
45 if (isa<Argument>(V)) {
47 }
else if (
auto *
I = dyn_cast<Instruction>(V)) {
50 if (
I->getOpcode() == Instruction::BitCast ||
51 I->getOpcode() == Instruction::PtrToInt) {
52 auto *
Op =
I->getOperand(0);
53 if (isa<Instruction>(
Op) || isa<Argument>(
Op))
69 auto AddAffectedFromEq = [&AddAffected](
Value *V) {
100 for (
auto &AV : Affected) {
101 auto &AVV = getOrInsertAffectedValues(AV);
102 if (
std::find(AVV.begin(), AVV.end(), CI) == AVV.end())
107 void AssumptionCache::AffectedValueCallbackVH::deleted() {
108 auto AVI = AC->AffectedValues.find(getValPtr());
109 if (AVI != AC->AffectedValues.end())
110 AC->AffectedValues.erase(AVI);
114 void AssumptionCache::copyAffectedValuesInCache(
Value *OV,
Value *
NV) {
115 auto &NAVV = getOrInsertAffectedValues(NV);
116 auto AVI = AffectedValues.find(OV);
117 if (AVI == AffectedValues.end())
120 for (
auto &
A : AVI->second)
121 if (
std::find(NAVV.begin(), NAVV.end(),
A) == NAVV.end())
125 void AssumptionCache::AffectedValueCallbackVH::allUsesReplacedWith(
Value *NV) {
126 if (!isa<Instruction>(NV) && !isa<Argument>(NV))
131 AC->copyAffectedValuesInCache(getValPtr(), NV);
137 void AssumptionCache::scanFunction() {
138 assert(!Scanned &&
"Tried to scan the function twice!");
139 assert(AssumeHandles.empty() &&
"Already have assumes when scanning!");
145 if (
match(&II, m_Intrinsic<Intrinsic::assume>()))
146 AssumeHandles.push_back(&II);
152 for (
auto &
A : AssumeHandles)
153 updateAffectedValues(cast<CallInst>(
A));
157 assert(
match(CI, m_Intrinsic<Intrinsic::assume>()) &&
158 "Registered call does not call @llvm.assume");
165 AssumeHandles.push_back(CI);
169 "Cannot register @llvm.assume call not in a basic block");
171 "Cannot register @llvm.assume call not in this function");
177 for (
auto &VH : AssumeHandles) {
182 "Cached assumption not inside this function!");
183 assert(
match(cast<CallInst>(VH), m_Intrinsic<Intrinsic::assume>()) &&
184 "Cached something other than a call to @llvm.assume!");
185 assert(AssumptionSet.insert(VH).second &&
186 "Cache contains multiple copies of a call!");
190 updateAffectedValues(CI);
199 OS <<
"Cached assumptions for function: " << F.
getName() <<
"\n";
202 OS <<
" " << *cast<CallInst>(VH)->getArgOperand(0) <<
"\n";
207 void AssumptionCacheTracker::FunctionCallbackVH::deleted() {
208 auto I = ACT->AssumptionCaches.find_as(cast<Function>(getValPtr()));
209 if (
I != ACT->AssumptionCaches.end())
210 ACT->AssumptionCaches.erase(
I);
219 auto I = AssumptionCaches.find_as(&F);
220 if (
I != AssumptionCaches.end())
225 auto IP = AssumptionCaches.insert(std::make_pair(
226 FunctionCallbackVH(&F,
this), llvm::make_unique<AssumptionCache>(F)));
227 assert(IP.second &&
"Scanning function already in the map?");
228 return *IP.first->second;
234 for (
const auto &
I : AssumptionCaches) {
235 for (
auto &VH :
I.second->assumptions())
237 AssumptionSet.
insert(cast<CallInst>(VH));
239 for (
const BasicBlock &B : cast<Function>(*
I.first))
241 if (
match(&II, m_Intrinsic<Intrinsic::assume>()))
243 "Assumption in scanned function not in cache");
255 "Assumption Cache Tracker",
false,
true)
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
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
This class represents a function call, abstracting a target machine's calling convention.
size_type count(PtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
An immutable pass that tracks lazily created AssumptionCache objects.
BinaryOp_match< LHS, RHS, Instruction::AShr > m_AShr(const LHS &L, const RHS &R)
A cache of .assume calls within a function.
const Function * getParent() const
Return the enclosing method, or null if none.
StringRef getName() const
Return a constant reference to the value's name.
bool match(Val *V, const Pattern &P)
BinaryOp_match< LHS, RHS, Instruction::Xor > m_Xor(const LHS &L, const RHS &R)
not_match< LHS > m_Not(const LHS &L)
match_combine_or< LTy, RTy > m_CombineOr(const LTy &L, const RTy &R)
Combine two pattern matchers matching L || R.
class_match< ConstantInt > m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
static GCRegistry::Add< OcamlGC > B("ocaml","ocaml 3.10-compatible GC")
BinaryOp_match< LHS, RHS, Instruction::LShr > m_LShr(const LHS &L, const RHS &R)
A set of analyses that are preserved following a run of a transformation pass.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs...ExtraArgs)
Get the result of an analysis pass for a given IR unit.
LLVM Basic Block Representation.
BinaryOp_match< LHS, RHS, Instruction::Or > m_Or(const LHS &L, const RHS &R)
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
MutableArrayRef< WeakVH > assumptions()
Access the list of assumption handles currently tracked for this function.
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.
static GCRegistry::Add< ShadowStackGC > C("shadow-stack","Very portable GC for uncooperative code generators")
Value * getArgOperand(unsigned i) const
getArgOperand/setArgOperand - Return/set the i-th call argument.
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)
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.
static GCRegistry::Add< ErlangGC > A("erlang","erlang-compatible garbage collector")
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)