llvm.org GIT mirror llvm / a25a344
[CaptureTracking] Add a unit test for MaxUsesToExplore git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@350351 91177308-0d34-0410-b5e6-96231b3b80d8 Artur Pilipenko 8 months ago
2 changed file(s) with 79 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
1111 BlockFrequencyInfoTest.cpp
1212 BranchProbabilityInfoTest.cpp
1313 CallGraphTest.cpp
14 CaptureTrackingTest.cpp
1415 CFGTest.cpp
1516 CGSCCPassManagerTest.cpp
1617 DivergenceAnalysisTest.cpp
0 //=======- CaptureTrackingTest.cpp - Unit test for the Capture Tracking ---===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/Analysis/CaptureTracking.h"
10 #include "llvm/Analysis/OrderedBasicBlock.h"
11 #include "llvm/AsmParser/Parser.h"
12 #include "llvm/IR/Dominators.h"
13 #include "llvm/IR/Instructions.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
21 TEST(CaptureTracking, MaxUsesToExplore) {
22 StringRef Assembly = R"(
23 ; Function Attrs: nounwind ssp uwtable
24 declare void @doesnt_capture(i8* nocapture, i8* nocapture, i8* nocapture,
25 i8* nocapture, i8* nocapture)
26
27 ; %arg has 5 uses
28 define void @test_few_uses(i8* %arg) {
29 call void @doesnt_capture(i8* %arg, i8* %arg, i8* %arg, i8* %arg, i8* %arg)
30 ret void
31 }
32
33 ; %arg has 50 uses
34 define void @test_many_uses(i8* %arg) {
35 call void @doesnt_capture(i8* %arg, i8* %arg, i8* %arg, i8* %arg, i8* %arg)
36 call void @doesnt_capture(i8* %arg, i8* %arg, i8* %arg, i8* %arg, i8* %arg)
37 call void @doesnt_capture(i8* %arg, i8* %arg, i8* %arg, i8* %arg, i8* %arg)
38 call void @doesnt_capture(i8* %arg, i8* %arg, i8* %arg, i8* %arg, i8* %arg)
39 call void @doesnt_capture(i8* %arg, i8* %arg, i8* %arg, i8* %arg, i8* %arg)
40 call void @doesnt_capture(i8* %arg, i8* %arg, i8* %arg, i8* %arg, i8* %arg)
41 call void @doesnt_capture(i8* %arg, i8* %arg, i8* %arg, i8* %arg, i8* %arg)
42 call void @doesnt_capture(i8* %arg, i8* %arg, i8* %arg, i8* %arg, i8* %arg)
43 call void @doesnt_capture(i8* %arg, i8* %arg, i8* %arg, i8* %arg, i8* %arg)
44 call void @doesnt_capture(i8* %arg, i8* %arg, i8* %arg, i8* %arg, i8* %arg)
45 ret void
46 }
47 )";
48
49 LLVMContext Context;
50 SMDiagnostic Error;
51 auto M = parseAssemblyString(Assembly, Error, Context);
52 ASSERT_TRUE(M) << "Bad assembly?";
53
54 auto Test = [&M](const char *FName, unsigned FalseMaxUsesLimit,
55 unsigned TrueMaxUsesLimit) {
56 Function *F = M->getFunction(FName);
57 ASSERT_NE(F, nullptr);
58 Value *Arg = &*F->arg_begin();
59 ASSERT_NE(Arg, nullptr);
60 ASSERT_FALSE(PointerMayBeCaptured(Arg, true, true, FalseMaxUsesLimit));
61 ASSERT_TRUE(PointerMayBeCaptured(Arg, true, true, TrueMaxUsesLimit));
62
63 BasicBlock *EntryBB = &F->getEntryBlock();
64 DominatorTree DT(*F);
65 OrderedBasicBlock OBB(EntryBB);
66
67 Instruction *Ret = EntryBB->getTerminator();
68 ASSERT_TRUE(isa(Ret));
69 ASSERT_FALSE(PointerMayBeCapturedBefore(Arg, true, true, Ret, &DT, false,
70 &OBB, FalseMaxUsesLimit));
71 ASSERT_TRUE(PointerMayBeCapturedBefore(Arg, true, true, Ret, &DT, false,
72 &OBB, TrueMaxUsesLimit));
73 };
74
75 Test("test_few_uses", 6, 4);
76 Test("test_many_uses", 50, 30);
77 }