1 //===- AttributorTest.cpp - Attributor unit tests ------------------------===//
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/Transforms/IPO/Attributor.h"
10 #include "AttributorTestBase.h"
11 #include "llvm/ADT/StringRef.h"
12 #include "llvm/Analysis/CGSCCPassManager.h"
13 #include "llvm/Analysis/CallGraphSCCPass.h"
14 #include "llvm/Analysis/LoopAnalysisManager.h"
15 #include "llvm/AsmParser/Parser.h"
16 #include "llvm/Support/Allocator.h"
17 #include "llvm/Testing/Support/Error.h"
18 #include "llvm/Transforms/Utils/CallGraphUpdater.h"
19 #include "gtest/gtest.h"
20 #include <memory>
21 
22 namespace llvm {
23 
24 TEST_F(AttributorTestBase, IRPPositionCallBaseContext) {
25   const char *ModuleString = R"(
26     define i32 @foo(i32 %a) {
27     entry:
28       ret i32 %a
29     }
30   )";
31 
32   parseModule(ModuleString);
33 
34   Function *F = M->getFunction("foo");
35   IRPosition Pos = IRPosition::function(*F, (const llvm::CallBase *)0xDEADBEEF);
36   EXPECT_TRUE(Pos.hasCallBaseContext());
37   EXPECT_FALSE(Pos.stripCallBaseContext().hasCallBaseContext());
38 }
39 
40 TEST_F(AttributorTestBase, TestCast) {
41   const char *ModuleString = R"(
42     define i32 @foo(i32 %a, i32 %b) {
43     entry:
44       %c = add i32 %a, %b
45       ret i32 %c
46     }
47   )";
48 
49   Module &M = parseModule(ModuleString);
50 
51   SetVector<Function *> Functions;
52   AnalysisGetter AG;
53   for (Function &F : M)
54     Functions.insert(&F);
55 
56   CallGraphUpdater CGUpdater;
57   BumpPtrAllocator Allocator;
58   InformationCache InfoCache(M, AG, Allocator, nullptr);
59   Attributor A(Functions, InfoCache, CGUpdater);
60 
61   Function *F = M.getFunction("foo");
62 
63   const AbstractAttribute *AA =
64       &A.getOrCreateAAFor<AAIsDead>(IRPosition::function(*F));
65 
66   EXPECT_TRUE(AA);
67 
68   const auto *SFail = dyn_cast<AAAlign>(AA);
69   const auto *SSucc = dyn_cast<AAIsDead>(AA);
70 
71   ASSERT_EQ(SFail, nullptr);
72   ASSERT_TRUE(SSucc);
73 }
74 
75 } // namespace llvm