1 //=======- AliasSetTrackerTest.cpp - Unit test for the Alias Set Tracker -===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Analysis/AliasAnalysis.h"
11 #include "llvm/Analysis/AliasSetTracker.h"
12 #include "llvm/Analysis/TypeBasedAliasAnalysis.h"
13 #include "llvm/AsmParser/Parser.h"
14 #include "llvm/IR/LLVMContext.h"
15 #include "llvm/IR/Module.h"
16 #include "llvm/Support/SourceMgr.h"
17 #include "gtest/gtest.h"
21 TEST(AliasSetTracker
, AliasUnknownInst
) {
22 StringRef Assembly
= R
"(
23 @a = common global i32 0, align 4
24 @b = common global float 0.000000e+00, align 4
26 ; Function Attrs: nounwind ssp uwtable
27 define i32 @read_a() #0 {
28 %1 = load i32, i32* @a, align 4, !tbaa !3
32 ; Function Attrs: nounwind ssp uwtable
33 define void @write_b() #0 {
34 store float 1.000000e+01, float* @b, align 4, !tbaa !7
38 ; Function Attrs: nounwind ssp uwtable
39 define void @test() #0 {
40 %1 = call i32 @read_a(), !tbaa !3
41 call void @write_b(), !tbaa !7
46 !4 = !{!"int", !5, i64 0}
47 !5 = !{!"omnipotent
char", !6, i64 0}
48 !6 = !{!"Simple C
/C
++ TBAA
"}
50 !8 = !{!"float", !5, i64 0}
53 // Parse the IR. The two calls in @test can not access aliasing elements.
56 auto M
= parseAssemblyString(Assembly
, Error
, Context
);
57 ASSERT_TRUE(M
) << "Bad assembly?";
59 // Initialize the alias result.
60 Triple
Trip(M
->getTargetTriple());
61 TargetLibraryInfoImpl
TLII(Trip
);
62 TargetLibraryInfo
TLI(TLII
);
64 TypeBasedAAResult TBAAR
;
65 AA
.addAAResult(TBAAR
);
67 // Initialize the alias set tracker for the @test function.
68 Function
*Test
= M
->getFunction("test");
69 ASSERT_NE(Test
, nullptr);
70 AliasSetTracker
AST(AA
);
71 for (auto &BB
: *Test
)
73 // There should be 2 disjoint alias sets. 1 from each call.
74 ASSERT_EQ((int)AST
.getAliasSets().size(), 2);
76 // Directly test aliasesUnknownInst.
77 // Now every call instruction should only alias one alias set.
78 for (auto &Inst
: *Test
->begin()) {
80 for (AliasSet
&AS
: AST
) {
81 if (!AS
.aliasesUnknownInst(&Inst
, AA
))
83 ASSERT_NE(FoundAS
, true);