xref: /llvm-project/llvm/unittests/Analysis/GlobalsModRefTest.cpp (revision 4169338e75cdce73d34063532db598c95ee82ae4)
1 //===--- GlobalsModRefTest.cpp - Mixed TBAA unit tests --------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/Analysis/GlobalsModRef.h"
10 #include "llvm/Analysis/CallGraph.h"
11 #include "llvm/Analysis/TargetLibraryInfo.h"
12 #include "llvm/AsmParser/Parser.h"
13 #include "llvm/IR/Module.h"
14 #include "llvm/Support/SourceMgr.h"
15 #include "llvm/TargetParser/Triple.h"
16 #include "gtest/gtest.h"
17 
18 using namespace llvm;
19 
TEST(GlobalsModRef,OptNone)20 TEST(GlobalsModRef, OptNone) {
21   StringRef Assembly = R"(
22     define void @f1() optnone {
23       ret void
24     }
25     define void @f2() optnone readnone {
26       ret void
27     }
28     define void @f3() optnone readonly {
29       ret void
30     }
31   )";
32 
33   LLVMContext Context;
34   SMDiagnostic Error;
35   auto M = parseAssemblyString(Assembly, Error, Context);
36   ASSERT_TRUE(M) << "Bad assembly?";
37 
38   const auto &funcs = M->functions();
39   auto I = funcs.begin();
40   ASSERT_NE(I, funcs.end());
41   const Function &F1 = *I;
42   ASSERT_NE(++I, funcs.end());
43   const Function &F2 = *I;
44   ASSERT_NE(++I, funcs.end());
45   const Function &F3 = *I;
46   EXPECT_EQ(++I, funcs.end());
47 
48   Triple Trip(M->getTargetTriple());
49   TargetLibraryInfoImpl TLII(Trip);
50   TargetLibraryInfo TLI(TLII);
51   auto GetTLI = [&TLI](Function &F) -> TargetLibraryInfo & { return TLI; };
52   llvm::CallGraph CG(*M);
53 
54   auto AAR = GlobalsAAResult::analyzeModule(*M, GetTLI, CG);
55 
56   EXPECT_EQ(MemoryEffects::unknown(), AAR.getMemoryEffects(&F1));
57   EXPECT_EQ(MemoryEffects::none(), AAR.getMemoryEffects(&F2));
58   EXPECT_EQ(MemoryEffects::readOnly(), AAR.getMemoryEffects(&F3));
59 }
60