xref: /llvm-project/llvm/unittests/Analysis/GlobalsModRefTest.cpp (revision 9c27b59cec76abea4f3f9261f3ffa73450f239c6)
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/AsmParser/Parser.h"
11 #include "llvm/Support/SourceMgr.h"
12 #include "gtest/gtest.h"
13 
14 using namespace llvm;
15 
16 TEST(GlobalsModRef, OptNone) {
17   StringRef Assembly = R"(
18     define void @f1() optnone {
19       ret void
20     }
21     define void @f2() optnone readnone {
22       ret void
23     }
24     define void @f3() optnone readonly {
25       ret void
26     }
27   )";
28 
29   LLVMContext Context;
30   SMDiagnostic Error;
31   auto M = parseAssemblyString(Assembly, Error, Context);
32   ASSERT_TRUE(M) << "Bad assembly?";
33 
34   const auto &funcs = M->functions();
35   auto I = funcs.begin();
36   ASSERT_NE(I, funcs.end());
37   const Function &F1 = *I;
38   ASSERT_NE(++I, funcs.end());
39   const Function &F2 = *I;
40   ASSERT_NE(++I, funcs.end());
41   const Function &F3 = *I;
42   EXPECT_EQ(++I, funcs.end());
43 
44   Triple Trip(M->getTargetTriple());
45   TargetLibraryInfoImpl TLII(Trip);
46   TargetLibraryInfo TLI(TLII);
47   auto GetTLI = [&TLI](Function &F) -> TargetLibraryInfo & { return TLI; };
48   llvm::CallGraph CG(*M);
49 
50   auto AAR = GlobalsAAResult::analyzeModule(*M, GetTLI, CG);
51 
52   EXPECT_EQ(FMRB_UnknownModRefBehavior, AAR.getModRefBehavior(&F1));
53   EXPECT_EQ(FMRB_DoesNotAccessMemory, AAR.getModRefBehavior(&F2));
54   EXPECT_EQ(FMRB_OnlyReadsMemory, AAR.getModRefBehavior(&F3));
55 }
56