mirror of
https://github.com/RPCS3/llvm-mirror.git
synced 2024-10-20 19:42:54 +02:00
84780666b4
accept an Invalidator that allows them to invalidate themselves if their dependencies are in turn invalidated. Rather than recording the dependency graph ahead of time when analysis get results from other analyses, this simply lets each result trigger the immediate invalidation of any analyses they actually depend on. They do this in a way that has three nice properties: 1) They don't have to handle transitive dependencies because the infrastructure will recurse for them. 2) The invalidate methods are still called only once. We just dynamically discover the necessary topological ordering, everything is memoized nicely. 3) The infrastructure still provides a default implementation and can access it so that only analyses which have dependencies need to do anything custom. To make this work at all, the invalidation logic also has to defer the deletion of the result objects themselves so that they can remain alive until we have collected the complete set of results to invalidate. A unittest is added here that has exactly the dependency pattern we are concerned with. It hit the use-after-free described by Sean in much detail in the long thread about analysis invalidation before this change, and even in an intermediate form of this change where we failed to defer the deletion of the result objects. There is an important problem with doing dependency invalidation that *isn't* solved here: we don't *enforce* that results correctly invalidate all the analyses whose results they depend on. I actually looked at what it would take to do that, and it isn't as hard as I had thought but the complexity it introduces seems very likely to outweigh the benefit. The technique would be to provide a base class for an analysis result that would be populated with other results, and automatically provide the invalidate method which immediately does the correct thing. This approach has some nice pros IMO: - Handles the case we care about and nothing else: only *results* that depend on other analyses trigger extra invalidation. - Localized to the result rather than centralized in the analysis manager. - Ties the storage of the reference to another result to the triggering of the invalidation of that analysis. - Still supports extending invalidation in customized ways. But the down sides here are: - Very heavy-weight meta-programming is needed to provide this base class. - Requires a pretty awful API for accessing the dependencies. Ultimately, I fear it will not pull its weight. But we can re-evaluate this at any point if we start discovering consistent problems where the invalidation and dependencies get out of sync. It will fit as a clean layer on top of the facilities in this patch that we can add if and when we need it. Note that I'm not really thrilled with the names for these APIs... The name "Invalidator" seems ok but not great. The method name "invalidate" also. In review some improvements were suggested, but they really need *other* uses of these terms to be updated as well so I'm going to do that in a follow-up commit. I'm working on the actual fixes to various analyses that need to use these, but I want to try to get tests for each of them so we don't regress. And those changes are seperable and obvious so once this goes in I should be able to roll them out throughout LLVM. Many thanks to Sean, Justin, and others for help reviewing here. Differential Revision: https://reviews.llvm.org/D23738 llvm-svn: 288077
161 lines
5.0 KiB
C++
161 lines
5.0 KiB
C++
//=- CFLSteensAliasAnalysis.h - Unification-based Alias Analysis ---*- C++-*-=//
|
|
//
|
|
// The LLVM Compiler Infrastructure
|
|
//
|
|
// This file is distributed under the University of Illinois Open Source
|
|
// License. See LICENSE.TXT for details.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
/// \file
|
|
/// This is the interface for LLVM's unification-based alias analysis
|
|
/// implemented with CFL graph reachability.
|
|
///
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#ifndef LLVM_ANALYSIS_CFLSTEENSALIASANALYSIS_H
|
|
#define LLVM_ANALYSIS_CFLSTEENSALIASANALYSIS_H
|
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
#include "llvm/ADT/None.h"
|
|
#include "llvm/ADT/Optional.h"
|
|
#include "llvm/Analysis/AliasAnalysis.h"
|
|
#include "llvm/IR/Function.h"
|
|
#include "llvm/IR/Module.h"
|
|
#include "llvm/IR/ValueHandle.h"
|
|
#include "llvm/Pass.h"
|
|
#include <forward_list>
|
|
|
|
namespace llvm {
|
|
|
|
class TargetLibraryInfo;
|
|
|
|
namespace cflaa {
|
|
struct AliasSummary;
|
|
}
|
|
|
|
class CFLSteensAAResult : public AAResultBase<CFLSteensAAResult> {
|
|
friend AAResultBase<CFLSteensAAResult>;
|
|
class FunctionInfo;
|
|
|
|
public:
|
|
explicit CFLSteensAAResult(const TargetLibraryInfo &);
|
|
CFLSteensAAResult(CFLSteensAAResult &&Arg);
|
|
~CFLSteensAAResult();
|
|
|
|
/// Handle invalidation events from the new pass manager.
|
|
///
|
|
/// By definition, this result is stateless and so remains valid.
|
|
bool invalidate(Function &, const PreservedAnalyses &,
|
|
FunctionAnalysisManager::Invalidator &) {
|
|
return false;
|
|
}
|
|
|
|
/// \brief Inserts the given Function into the cache.
|
|
void scan(Function *Fn);
|
|
|
|
void evict(Function *Fn);
|
|
|
|
/// \brief Ensures that the given function is available in the cache.
|
|
/// Returns the appropriate entry from the cache.
|
|
const Optional<FunctionInfo> &ensureCached(Function *Fn);
|
|
|
|
/// \brief Get the alias summary for the given function
|
|
/// Return nullptr if the summary is not found or not available
|
|
const cflaa::AliasSummary *getAliasSummary(Function &Fn);
|
|
|
|
AliasResult query(const MemoryLocation &LocA, const MemoryLocation &LocB);
|
|
|
|
AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB) {
|
|
if (LocA.Ptr == LocB.Ptr)
|
|
return LocA.Size == LocB.Size ? MustAlias : PartialAlias;
|
|
|
|
// Comparisons between global variables and other constants should be
|
|
// handled by BasicAA.
|
|
// CFLSteensAA may report NoAlias when comparing a GlobalValue and
|
|
// ConstantExpr, but every query needs to have at least one Value tied to a
|
|
// Function, and neither GlobalValues nor ConstantExprs are.
|
|
if (isa<Constant>(LocA.Ptr) && isa<Constant>(LocB.Ptr))
|
|
return AAResultBase::alias(LocA, LocB);
|
|
|
|
AliasResult QueryResult = query(LocA, LocB);
|
|
if (QueryResult == MayAlias)
|
|
return AAResultBase::alias(LocA, LocB);
|
|
|
|
return QueryResult;
|
|
}
|
|
|
|
private:
|
|
struct FunctionHandle final : public CallbackVH {
|
|
FunctionHandle(Function *Fn, CFLSteensAAResult *Result)
|
|
: CallbackVH(Fn), Result(Result) {
|
|
assert(Fn != nullptr);
|
|
assert(Result != nullptr);
|
|
}
|
|
|
|
void deleted() override { removeSelfFromCache(); }
|
|
void allUsesReplacedWith(Value *) override { removeSelfFromCache(); }
|
|
|
|
private:
|
|
CFLSteensAAResult *Result;
|
|
|
|
void removeSelfFromCache() {
|
|
assert(Result != nullptr);
|
|
auto *Val = getValPtr();
|
|
Result->evict(cast<Function>(Val));
|
|
setValPtr(nullptr);
|
|
}
|
|
};
|
|
|
|
const TargetLibraryInfo &TLI;
|
|
|
|
/// \brief Cached mapping of Functions to their StratifiedSets.
|
|
/// If a function's sets are currently being built, it is marked
|
|
/// in the cache as an Optional without a value. This way, if we
|
|
/// have any kind of recursion, it is discernable from a function
|
|
/// that simply has empty sets.
|
|
DenseMap<Function *, Optional<FunctionInfo>> Cache;
|
|
std::forward_list<FunctionHandle> Handles;
|
|
|
|
FunctionInfo buildSetsFrom(Function *F);
|
|
};
|
|
|
|
/// Analysis pass providing a never-invalidated alias analysis result.
|
|
///
|
|
/// FIXME: We really should refactor CFL to use the analysis more heavily, and
|
|
/// in particular to leverage invalidation to trigger re-computation of sets.
|
|
class CFLSteensAA : public AnalysisInfoMixin<CFLSteensAA> {
|
|
friend AnalysisInfoMixin<CFLSteensAA>;
|
|
static AnalysisKey Key;
|
|
|
|
public:
|
|
typedef CFLSteensAAResult Result;
|
|
|
|
CFLSteensAAResult run(Function &F, FunctionAnalysisManager &AM);
|
|
};
|
|
|
|
/// Legacy wrapper pass to provide the CFLSteensAAResult object.
|
|
class CFLSteensAAWrapperPass : public ImmutablePass {
|
|
std::unique_ptr<CFLSteensAAResult> Result;
|
|
|
|
public:
|
|
static char ID;
|
|
|
|
CFLSteensAAWrapperPass();
|
|
|
|
CFLSteensAAResult &getResult() { return *Result; }
|
|
const CFLSteensAAResult &getResult() const { return *Result; }
|
|
|
|
void initializePass() override;
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override;
|
|
};
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
//
|
|
// createCFLSteensAAWrapperPass - This pass implements a set-based approach to
|
|
// alias analysis.
|
|
//
|
|
ImmutablePass *createCFLSteensAAWrapperPass();
|
|
}
|
|
|
|
#endif
|