1 //=======- CaptureTrackingTest.cpp - Unit test for the Capture Tracking ---===//
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 //===----------------------------------------------------------------------===//
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
TEST(CaptureTracking,MaxUsesToExplore)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<ReturnInst>(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 }
78