[obj2yaml] - Stop triggering UB when dumping corrupted strings.
[llvm-complete.git] / unittests / Analysis / AliasSetTrackerTest.cpp
blob77a5ac47af12e564e5f68d72a2cf7425b9afa6aa
1 //=======- AliasSetTrackerTest.cpp - Unit test for the Alias Set Tracker -===//
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 //===----------------------------------------------------------------------===//
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"
18 using namespace llvm;
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
28 ret i32 %1
31 ; Function Attrs: nounwind ssp uwtable
32 define void @write_b() #0 {
33 store float 1.000000e+01, float* @b, align 4, !tbaa !7
34 ret void
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
41 ret void
44 !3 = !{!4, !4, i64 0}
45 !4 = !{!"int", !5, i64 0}
46 !5 = !{!"omnipotent char", !6, i64 0}
47 !6 = !{!"Simple C/C++ TBAA"}
48 !7 = !{!8, !8, i64 0}
49 !8 = !{!"float", !5, i64 0}
50 )";
52 // Parse the IR. The two calls in @test can not access aliasing elements.
53 LLVMContext Context;
54 SMDiagnostic Error;
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);
62 AAResults AA(TLI);
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)
71 AST.add(BB);
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()) {
78 bool FoundAS = false;
79 for (AliasSet &AS : AST) {
80 if (!Inst.mayReadOrWriteMemory())
81 continue;
82 if (!AS.aliasesUnknownInst(&Inst, AA))
83 continue;
84 ASSERT_NE(FoundAS, true);
85 FoundAS = true;