+void initializeAATestPassPass(PassRegistry&);
+void initializeTestCustomAAWrapperPassPass(PassRegistry&);
+}
+
+namespace {
+struct AATestPass : FunctionPass {
+ static char ID;
+ AATestPass() : FunctionPass(ID) {
+ initializeAATestPassPass(*PassRegistry::getPassRegistry());
+ }
+
+ void getAnalysisUsage(AnalysisUsage &AU) const override {
+ AU.addRequired<AAResultsWrapperPass>();
+ AU.setPreservesAll();
+ }
+
+ bool runOnFunction(Function &F) override {
+ AliasAnalysis &AA = getAnalysis<AAResultsWrapperPass>().getAAResults();
+
+ SetVector<Value *> Pointers;
+ for (Argument &A : F.args())
+ if (A.getType()->isPointerTy())
+ Pointers.insert(&A);
+ for (Instruction &I : instructions(F))
+ if (I.getType()->isPointerTy())
+ Pointers.insert(&I);
+
+ for (Value *P1 : Pointers)
+ for (Value *P2 : Pointers)
+ (void)AA.alias(P1, MemoryLocation::UnknownSize, P2,
+ MemoryLocation::UnknownSize);
+
+ return false;
+ }
+};
+}
+
+char AATestPass::ID = 0;
+INITIALIZE_PASS_BEGIN(AATestPass, "aa-test-pas", "Alias Analysis Test Pass",
+ false, true)
+INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
+INITIALIZE_PASS_END(AATestPass, "aa-test-pass", "Alias Analysis Test Pass",
+ false, true)
+
+namespace {
+/// A test customizable AA result. It merely accepts a callback to run whenever
+/// it receives an alias query. Useful for testing that a particular AA result
+/// is reached.
+struct TestCustomAAResult : AAResultBase<TestCustomAAResult> {
+ friend AAResultBase<TestCustomAAResult>;
+
+ std::function<void()> CB;
+
+ explicit TestCustomAAResult(const TargetLibraryInfo &TLI,
+ std::function<void()> CB)
+ : AAResultBase(TLI), CB(std::move(CB)) {}
+ TestCustomAAResult(TestCustomAAResult &&Arg)
+ : AAResultBase(std::move(Arg)), CB(std::move(Arg.CB)) {}
+
+ bool invalidate(Function &, const PreservedAnalyses &) { return false; }
+
+ AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB) {
+ CB();
+ return MayAlias;
+ }
+};
+}
+
+namespace {
+/// A wrapper pass for the legacy pass manager to use with the above custom AA
+/// result.
+class TestCustomAAWrapperPass : public ImmutablePass {
+ std::function<void()> CB;
+ std::unique_ptr<TestCustomAAResult> Result;
+
+public:
+ static char ID;
+
+ explicit TestCustomAAWrapperPass(
+ std::function<void()> CB = std::function<void()>())
+ : ImmutablePass(ID), CB(std::move(CB)) {
+ initializeTestCustomAAWrapperPassPass(*PassRegistry::getPassRegistry());
+ }
+
+ void getAnalysisUsage(AnalysisUsage &AU) const override {
+ AU.setPreservesAll();
+ AU.addRequired<TargetLibraryInfoWrapperPass>();
+ }
+
+ bool doInitialization(Module &M) override {
+ Result.reset(new TestCustomAAResult(
+ getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(), std::move(CB)));
+ return true;
+ }
+
+ bool doFinalization(Module &M) override {
+ Result.reset();
+ return true;
+ }
+
+ TestCustomAAResult &getResult() { return *Result; }
+ const TestCustomAAResult &getResult() const { return *Result; }
+};
+}
+
+char TestCustomAAWrapperPass::ID = 0;
+INITIALIZE_PASS_BEGIN(TestCustomAAWrapperPass, "test-custom-aa",
+ "Test Custom AA Wrapper Pass", false, true)
+INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
+INITIALIZE_PASS_END(TestCustomAAWrapperPass, "test-custom-aa",
+ "Test Custom AA Wrapper Pass", false, true)
+