mirror of
https://github.com/RPCS3/llvm-mirror.git
synced 2025-02-16 10:18:09 +00:00
![Chandler Carruth](/assets/img/avatar_default.png)
with the new pass manager, and no longer relying on analysis groups. This builds essentially a ground-up new AA infrastructure stack for LLVM. The core ideas are the same that are used throughout the new pass manager: type erased polymorphism and direct composition. The design is as follows: - FunctionAAResults is a type-erasing alias analysis results aggregation interface to walk a single query across a range of results from different alias analyses. Currently this is function-specific as we always assume that aliasing queries are *within* a function. - AAResultBase is a CRTP utility providing stub implementations of various parts of the alias analysis result concept, notably in several cases in terms of other more general parts of the interface. This can be used to implement only a narrow part of the interface rather than the entire interface. This isn't really ideal, this logic should be hoisted into FunctionAAResults as currently it will cause a significant amount of redundant work, but it faithfully models the behavior of the prior infrastructure. - All the alias analysis passes are ported to be wrapper passes for the legacy PM and new-style analysis passes for the new PM with a shared result object. In some cases (most notably CFL), this is an extremely naive approach that we should revisit when we can specialize for the new pass manager. - BasicAA has been restructured to reflect that it is much more fundamentally a function analysis because it uses dominator trees and loop info that need to be constructed for each function. All of the references to getting alias analysis results have been updated to use the new aggregation interface. All the preservation and other pass management code has been updated accordingly. The way the FunctionAAResultsWrapperPass works is to detect the available alias analyses when run, and add them to the results object. This means that we should be able to continue to respect when various passes are added to the pipeline, for example adding CFL or adding TBAA passes should just cause their results to be available and to get folded into this. The exception to this rule is BasicAA which really needs to be a function pass due to using dominator trees and loop info. As a consequence, the FunctionAAResultsWrapperPass directly depends on BasicAA and always includes it in the aggregation. This has significant implications for preserving analyses. Generally, most passes shouldn't bother preserving FunctionAAResultsWrapperPass because rebuilding the results just updates the set of known AA passes. The exception to this rule are LoopPass instances which need to preserve all the function analyses that the loop pass manager will end up needing. This means preserving both BasicAAWrapperPass and the aggregating FunctionAAResultsWrapperPass. Now, when preserving an alias analysis, you do so by directly preserving that analysis. This is only necessary for non-immutable-pass-provided alias analyses though, and there are only three of interest: BasicAA, GlobalsAA (formerly GlobalsModRef), and SCEVAA. Usually BasicAA is preserved when needed because it (like DominatorTree and LoopInfo) is marked as a CFG-only pass. I've expanded GlobalsAA into the preserved set everywhere we previously were preserving all of AliasAnalysis, and I've added SCEVAA in the intersection of that with where we preserve SCEV itself. One significant challenge to all of this is that the CGSCC passes were actually using the alias analysis implementations by taking advantage of a pretty amazing set of loop holes in the old pass manager's analysis management code which allowed analysis groups to slide through in many cases. Moving away from analysis groups makes this problem much more obvious. To fix it, I've leveraged the flexibility the design of the new PM components provides to just directly construct the relevant alias analyses for the relevant functions in the IPO passes that need them. This is a bit hacky, but should go away with the new pass manager, and is already in many ways cleaner than the prior state. Another significant challenge is that various facilities of the old alias analysis infrastructure just don't fit any more. The most significant of these is the alias analysis 'counter' pass. That pass relied on the ability to snoop on AA queries at different points in the analysis group chain. Instead, I'm planning to build printing functionality directly into the aggregation layer. I've not included that in this patch merely to keep it smaller. Note that all of this needs a nearly complete rewrite of the AA documentation. I'm planning to do that, but I'd like to make sure the new design settles, and to flesh out a bit more of what it looks like in the new pass manager first. Differential Revision: http://reviews.llvm.org/D12080 llvm-svn: 247167
212 lines
7.5 KiB
C++
212 lines
7.5 KiB
C++
//===- ScopedNoAliasAA.cpp - Scoped No-Alias Alias Analysis ---------------===//
|
|
//
|
|
// The LLVM Compiler Infrastructure
|
|
//
|
|
// This file is distributed under the University of Illinois Open Source
|
|
// License. See LICENSE.TXT for details.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This file defines the ScopedNoAlias alias-analysis pass, which implements
|
|
// metadata-based scoped no-alias support.
|
|
//
|
|
// Alias-analysis scopes are defined by an id (which can be a string or some
|
|
// other metadata node), a domain node, and an optional descriptive string.
|
|
// A domain is defined by an id (which can be a string or some other metadata
|
|
// node), and an optional descriptive string.
|
|
//
|
|
// !dom0 = metadata !{ metadata !"domain of foo()" }
|
|
// !scope1 = metadata !{ metadata !scope1, metadata !dom0, metadata !"scope 1" }
|
|
// !scope2 = metadata !{ metadata !scope2, metadata !dom0, metadata !"scope 2" }
|
|
//
|
|
// Loads and stores can be tagged with an alias-analysis scope, and also, with
|
|
// a noalias tag for a specific scope:
|
|
//
|
|
// ... = load %ptr1, !alias.scope !{ !scope1 }
|
|
// ... = load %ptr2, !alias.scope !{ !scope1, !scope2 }, !noalias !{ !scope1 }
|
|
//
|
|
// When evaluating an aliasing query, if one of the instructions is associated
|
|
// has a set of noalias scopes in some domain that is superset of the alias
|
|
// scopes in that domain of some other instruction, then the two memory
|
|
// accesses are assumed not to alias.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "llvm/Analysis/ScopedNoAliasAA.h"
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
|
#include "llvm/Analysis/TargetLibraryInfo.h"
|
|
#include "llvm/IR/Constants.h"
|
|
#include "llvm/IR/LLVMContext.h"
|
|
#include "llvm/IR/Metadata.h"
|
|
#include "llvm/IR/Module.h"
|
|
#include "llvm/Pass.h"
|
|
#include "llvm/Support/CommandLine.h"
|
|
using namespace llvm;
|
|
|
|
// A handy option for disabling scoped no-alias functionality. The same effect
|
|
// can also be achieved by stripping the associated metadata tags from IR, but
|
|
// this option is sometimes more convenient.
|
|
static cl::opt<bool> EnableScopedNoAlias("enable-scoped-noalias",
|
|
cl::init(true));
|
|
|
|
namespace {
|
|
/// AliasScopeNode - This is a simple wrapper around an MDNode which provides
|
|
/// a higher-level interface by hiding the details of how alias analysis
|
|
/// information is encoded in its operands.
|
|
class AliasScopeNode {
|
|
const MDNode *Node;
|
|
|
|
public:
|
|
AliasScopeNode() : Node(0) {}
|
|
explicit AliasScopeNode(const MDNode *N) : Node(N) {}
|
|
|
|
/// getNode - Get the MDNode for this AliasScopeNode.
|
|
const MDNode *getNode() const { return Node; }
|
|
|
|
/// getDomain - Get the MDNode for this AliasScopeNode's domain.
|
|
const MDNode *getDomain() const {
|
|
if (Node->getNumOperands() < 2)
|
|
return nullptr;
|
|
return dyn_cast_or_null<MDNode>(Node->getOperand(1));
|
|
}
|
|
};
|
|
} // End of anonymous namespace
|
|
|
|
AliasResult ScopedNoAliasAAResult::alias(const MemoryLocation &LocA,
|
|
const MemoryLocation &LocB) {
|
|
if (!EnableScopedNoAlias)
|
|
return AAResultBase::alias(LocA, LocB);
|
|
|
|
// Get the attached MDNodes.
|
|
const MDNode *AScopes = LocA.AATags.Scope, *BScopes = LocB.AATags.Scope;
|
|
|
|
const MDNode *ANoAlias = LocA.AATags.NoAlias, *BNoAlias = LocB.AATags.NoAlias;
|
|
|
|
if (!mayAliasInScopes(AScopes, BNoAlias))
|
|
return NoAlias;
|
|
|
|
if (!mayAliasInScopes(BScopes, ANoAlias))
|
|
return NoAlias;
|
|
|
|
// If they may alias, chain to the next AliasAnalysis.
|
|
return AAResultBase::alias(LocA, LocB);
|
|
}
|
|
|
|
ModRefInfo ScopedNoAliasAAResult::getModRefInfo(ImmutableCallSite CS,
|
|
const MemoryLocation &Loc) {
|
|
if (!EnableScopedNoAlias)
|
|
return AAResultBase::getModRefInfo(CS, Loc);
|
|
|
|
if (!mayAliasInScopes(Loc.AATags.Scope, CS.getInstruction()->getMetadata(
|
|
LLVMContext::MD_noalias)))
|
|
return MRI_NoModRef;
|
|
|
|
if (!mayAliasInScopes(
|
|
CS.getInstruction()->getMetadata(LLVMContext::MD_alias_scope),
|
|
Loc.AATags.NoAlias))
|
|
return MRI_NoModRef;
|
|
|
|
return AAResultBase::getModRefInfo(CS, Loc);
|
|
}
|
|
|
|
ModRefInfo ScopedNoAliasAAResult::getModRefInfo(ImmutableCallSite CS1,
|
|
ImmutableCallSite CS2) {
|
|
if (!EnableScopedNoAlias)
|
|
return AAResultBase::getModRefInfo(CS1, CS2);
|
|
|
|
if (!mayAliasInScopes(
|
|
CS1.getInstruction()->getMetadata(LLVMContext::MD_alias_scope),
|
|
CS2.getInstruction()->getMetadata(LLVMContext::MD_noalias)))
|
|
return MRI_NoModRef;
|
|
|
|
if (!mayAliasInScopes(
|
|
CS2.getInstruction()->getMetadata(LLVMContext::MD_alias_scope),
|
|
CS1.getInstruction()->getMetadata(LLVMContext::MD_noalias)))
|
|
return MRI_NoModRef;
|
|
|
|
return AAResultBase::getModRefInfo(CS1, CS2);
|
|
}
|
|
|
|
void ScopedNoAliasAAResult::collectMDInDomain(
|
|
const MDNode *List, const MDNode *Domain,
|
|
SmallPtrSetImpl<const MDNode *> &Nodes) const {
|
|
for (unsigned i = 0, ie = List->getNumOperands(); i != ie; ++i)
|
|
if (const MDNode *MD = dyn_cast<MDNode>(List->getOperand(i)))
|
|
if (AliasScopeNode(MD).getDomain() == Domain)
|
|
Nodes.insert(MD);
|
|
}
|
|
|
|
bool ScopedNoAliasAAResult::mayAliasInScopes(const MDNode *Scopes,
|
|
const MDNode *NoAlias) const {
|
|
if (!Scopes || !NoAlias)
|
|
return true;
|
|
|
|
// Collect the set of scope domains relevant to the noalias scopes.
|
|
SmallPtrSet<const MDNode *, 16> Domains;
|
|
for (unsigned i = 0, ie = NoAlias->getNumOperands(); i != ie; ++i)
|
|
if (const MDNode *NAMD = dyn_cast<MDNode>(NoAlias->getOperand(i)))
|
|
if (const MDNode *Domain = AliasScopeNode(NAMD).getDomain())
|
|
Domains.insert(Domain);
|
|
|
|
// We alias unless, for some domain, the set of noalias scopes in that domain
|
|
// is a superset of the set of alias scopes in that domain.
|
|
for (const MDNode *Domain : Domains) {
|
|
SmallPtrSet<const MDNode *, 16> NANodes, ScopeNodes;
|
|
collectMDInDomain(NoAlias, Domain, NANodes);
|
|
collectMDInDomain(Scopes, Domain, ScopeNodes);
|
|
if (!ScopeNodes.size())
|
|
continue;
|
|
|
|
// To not alias, all of the nodes in ScopeNodes must be in NANodes.
|
|
bool FoundAll = true;
|
|
for (const MDNode *SMD : ScopeNodes)
|
|
if (!NANodes.count(SMD)) {
|
|
FoundAll = false;
|
|
break;
|
|
}
|
|
|
|
if (FoundAll)
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
ScopedNoAliasAAResult ScopedNoAliasAA::run(Function &F,
|
|
AnalysisManager<Function> *AM) {
|
|
return ScopedNoAliasAAResult(AM->getResult<TargetLibraryAnalysis>(F));
|
|
}
|
|
|
|
char ScopedNoAliasAA::PassID;
|
|
|
|
char ScopedNoAliasAAWrapperPass::ID = 0;
|
|
INITIALIZE_PASS_BEGIN(ScopedNoAliasAAWrapperPass, "scoped-noalias",
|
|
"Scoped NoAlias Alias Analysis", false, true)
|
|
INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
|
|
INITIALIZE_PASS_END(ScopedNoAliasAAWrapperPass, "scoped-noalias",
|
|
"Scoped NoAlias Alias Analysis", false, true)
|
|
|
|
ImmutablePass *llvm::createScopedNoAliasAAWrapperPass() {
|
|
return new ScopedNoAliasAAWrapperPass();
|
|
}
|
|
|
|
ScopedNoAliasAAWrapperPass::ScopedNoAliasAAWrapperPass() : ImmutablePass(ID) {
|
|
initializeScopedNoAliasAAWrapperPassPass(*PassRegistry::getPassRegistry());
|
|
}
|
|
|
|
bool ScopedNoAliasAAWrapperPass::doInitialization(Module &M) {
|
|
Result.reset(new ScopedNoAliasAAResult(
|
|
getAnalysis<TargetLibraryInfoWrapperPass>().getTLI()));
|
|
return false;
|
|
}
|
|
|
|
bool ScopedNoAliasAAWrapperPass::doFinalization(Module &M) {
|
|
Result.reset();
|
|
return false;
|
|
}
|
|
|
|
void ScopedNoAliasAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
|
|
AU.setPreservesAll();
|
|
AU.addRequired<TargetLibraryInfoWrapperPass>();
|
|
}
|