1 //=======- AliasSetTrackerTest.cpp - Unit test for the Alias Set Tracker -===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
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"
18
19 using namespace llvm;
20
TEST(AliasSetTracker,AliasUnknownInst)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
25
26 ; Function Attrs: nounwind ssp uwtable
27 define i32 @read_a() #0 {
28 %1 = load i32, i32* @a, align 4, !tbaa !3
29 ret i32 %1
30 }
31
32 ; Function Attrs: nounwind ssp uwtable
33 define void @write_b() #0 {
34 store float 1.000000e+01, float* @b, align 4, !tbaa !7
35 ret void
36 }
37
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
42 ret void
43 }
44
45 !3 = !{!4, !4, i64 0}
46 !4 = !{!"int", !5, i64 0}
47 !5 = !{!"omnipotent char", !6, i64 0}
48 !6 = !{!"Simple C/C++ TBAA"}
49 !7 = !{!8, !8, i64 0}
50 !8 = !{!"float", !5, i64 0}
51 )";
52
53 // Parse the IR. The two calls in @test can not access aliasing elements.
54 LLVMContext Context;
55 SMDiagnostic Error;
56 auto M = parseAssemblyString(Assembly, Error, Context);
57 ASSERT_TRUE(M) << "Bad assembly?";
58
59 // Initialize the alias result.
60 Triple Trip(M->getTargetTriple());
61 TargetLibraryInfoImpl TLII(Trip);
62 TargetLibraryInfo TLI(TLII);
63 AAResults AA(TLI);
64 TypeBasedAAResult TBAAR;
65 AA.addAAResult(TBAAR);
66
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)
72 AST.add(BB);
73 // There should be 2 disjoint alias sets. 1 from each call.
74 ASSERT_EQ((int)AST.getAliasSets().size(), 2);
75
76 // Directly test aliasesUnknownInst.
77 // Now every call instruction should only alias one alias set.
78 for (auto &Inst : *Test->begin()) {
79 bool FoundAS = false;
80 for (AliasSet &AS : AST) {
81 if (!AS.aliasesUnknownInst(&Inst, AA))
82 continue;
83 ASSERT_NE(FoundAS, true);
84 FoundAS = true;
85 }
86 }
87 }
88