1 //=======- AliasSetTrackerTest.cpp - Unit test for the Alias Set Tracker -===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Analysis/AliasAnalysis.h"
10 #include "llvm/Analysis/AliasSetTracker.h"
11 #include "llvm/Analysis/TypeBasedAliasAnalysis.h"
12 #include "llvm/AsmParser/Parser.h"
13 #include "llvm/IR/LLVMContext.h"
14 #include "llvm/IR/Module.h"
15 #include "llvm/Support/SourceMgr.h"
16 #include "gtest/gtest.h"
20 TEST(AliasSetTracker
, AliasUnknownInst
) {
21 StringRef Assembly
= R
"(
22 @a = common global i32 0, align 4
23 @b = common global float 0.000000e+00, align 4
25 ; Function Attrs: nounwind ssp uwtable
26 define i32 @read_a() #0 {
27 %1 = load i32, i32* @a, align 4, !tbaa !3
31 ; Function Attrs: nounwind ssp uwtable
32 define void @write_b() #0 {
33 store float 1.000000e+01, float* @b, align 4, !tbaa !7
37 ; Function Attrs: nounwind ssp uwtable
38 define void @test() #0 {
39 %1 = call i32 @read_a(), !tbaa !3
40 call void @write_b(), !tbaa !7
45 !4 = !{!"int", !5, i64 0}
46 !5 = !{!"omnipotent
char", !6, i64 0}
47 !6 = !{!"Simple C
/C
++ TBAA
"}
49 !8 = !{!"float", !5, i64 0}
52 // Parse the IR. The two calls in @test can not access aliasing elements.
55 auto M
= parseAssemblyString(Assembly
, Error
, Context
);
56 ASSERT_TRUE(M
) << "Bad assembly?";
58 // Initialize the alias result.
59 Triple
Trip(M
->getTargetTriple());
60 TargetLibraryInfoImpl
TLII(Trip
);
61 TargetLibraryInfo
TLI(TLII
);
63 TypeBasedAAResult TBAAR
;
64 AA
.addAAResult(TBAAR
);
66 // Initialize the alias set tracker for the @test function.
67 Function
*Test
= M
->getFunction("test");
68 ASSERT_NE(Test
, nullptr);
69 AliasSetTracker
AST(AA
);
70 for (auto &BB
: *Test
)
72 // There should be 2 disjoint alias sets. 1 from each call.
73 ASSERT_EQ((int)AST
.getAliasSets().size(), 2);
75 // Directly test aliasesUnknownInst.
76 // Now every call instruction should only alias one alias set.
77 for (auto &Inst
: *Test
->begin()) {
79 for (AliasSet
&AS
: AST
) {
80 if (!Inst
.mayReadOrWriteMemory())
82 if (!AS
.aliasesUnknownInst(&Inst
, AA
))
84 ASSERT_NE(FoundAS
, true);