xref: /llvm-project/llvm/unittests/Transforms/IPO/AttributorTest.cpp (revision e9fc399db34c63d8c33c3f2315c9db3b7afcaa40)
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 
TEST_F(AttributorTestBase,IRPPositionCallBaseContext)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 =
36       IRPosition::function(*F, (const llvm::CallBase *)(uintptr_t)0xDEADBEEF);
37   EXPECT_TRUE(Pos.hasCallBaseContext());
38   EXPECT_FALSE(Pos.stripCallBaseContext().hasCallBaseContext());
39 }
40 
TEST_F(AttributorTestBase,TestCast)41 TEST_F(AttributorTestBase, TestCast) {
42   const char *ModuleString = R"(
43     define i32 @foo(i32 %a, i32 %b) {
44     entry:
45       %c = add i32 %a, %b
46       ret i32 %c
47     }
48   )";
49 
50   Module &M = parseModule(ModuleString);
51 
52   SetVector<Function *> Functions;
53   AnalysisGetter AG;
54   for (Function &F : M)
55     Functions.insert(&F);
56 
57   CallGraphUpdater CGUpdater;
58   BumpPtrAllocator Allocator;
59   InformationCache InfoCache(M, AG, Allocator, nullptr);
60   AttributorConfig AC(CGUpdater);
61   Attributor A(Functions, InfoCache, AC);
62 
63   Function *F = M.getFunction("foo");
64 
65   const AbstractAttribute *AA =
66       A.getOrCreateAAFor<AAIsDead>(IRPosition::function(*F));
67 
68   EXPECT_TRUE(AA);
69 
70   const auto *SFail = dyn_cast<AAAlign>(AA);
71   const auto *SSucc = dyn_cast<AAIsDead>(AA);
72 
73   ASSERT_EQ(SFail, nullptr);
74   ASSERT_TRUE(SSucc);
75 }
76 
TEST_F(AttributorTestBase,AAReachabilityTest)77 TEST_F(AttributorTestBase, AAReachabilityTest) {
78   const char *ModuleString = R"(
79     @x = external global i32
80     define void @func4() {
81       store i32 0, i32* @x
82       ret void
83     }
84 
85     define internal void @func3() {
86       store i32 0, i32* @x
87       ret void
88     }
89 
90     define internal void @func8() {
91       store i32 0, i32* @x
92       ret void
93     }
94 
95     define internal void @func2() {
96     entry:
97       call void @func3()
98       ret void
99     }
100 
101     define void @func1() {
102     entry:
103       call void @func2()
104       ret void
105     }
106 
107     declare void @unknown()
108     define internal void @func5(void ()* %ptr) {
109     entry:
110       call void %ptr()
111       call void @unknown()
112       ret void
113     }
114 
115     define void @func6() {
116     entry:
117       store i32 0, i32* @x
118       call void @func5(void ()* @func3)
119       ret void
120     }
121 
122     define void @func7() {
123     entry:
124       call void @func2()
125       call void @func4()
126       ret void
127     }
128 
129     define internal void @func9() {
130     entry:
131       call void @func2()
132       call void @func8()
133       ret void
134     }
135 
136     define void @func10() {
137     entry:
138       call void @func9()
139       call void @func4()
140       ret void
141     }
142 
143   )";
144 
145   Module &M = parseModule(ModuleString);
146 
147   SetVector<Function *> Functions;
148   AnalysisGetter AG;
149   for (Function &F : M)
150     Functions.insert(&F);
151 
152   CallGraphUpdater CGUpdater;
153   BumpPtrAllocator Allocator;
154   InformationCache InfoCache(M, AG, Allocator, nullptr);
155   AttributorConfig AC(CGUpdater);
156   AC.DeleteFns = false;
157   Attributor A(Functions, InfoCache, AC);
158 
159   Function &F1 = *M.getFunction("func1");
160   Function &F3 = *M.getFunction("func3");
161   Function &F4 = *M.getFunction("func4");
162   Function &F6 = *M.getFunction("func6");
163   Function &F7 = *M.getFunction("func7");
164   Function &F9 = *M.getFunction("func9");
165 
166   // call void @func2()
167   CallBase &F7FirstCB = static_cast<CallBase &>(*F7.getEntryBlock().begin());
168   // call void @func2()
169   Instruction &F9FirstInst = *F9.getEntryBlock().begin();
170   // call void @func8
171   Instruction &F9SecondInst = *++(F9.getEntryBlock().begin());
172 
173   const AAInterFnReachability &F1AA =
174       *A.getOrCreateAAFor<AAInterFnReachability>(IRPosition::function(F1));
175 
176   const AAInterFnReachability &F6AA =
177       *A.getOrCreateAAFor<AAInterFnReachability>(IRPosition::function(F6));
178 
179   const AAInterFnReachability &F7AA =
180       *A.getOrCreateAAFor<AAInterFnReachability>(IRPosition::function(F7));
181 
182   const AAInterFnReachability &F9AA =
183       *A.getOrCreateAAFor<AAInterFnReachability>(IRPosition::function(F9));
184 
185   F1AA.canReach(A, F3);
186   F1AA.canReach(A, F4);
187   F6AA.canReach(A, F4);
188   F7AA.instructionCanReach(A, F7FirstCB, F3);
189   F7AA.instructionCanReach(A, F7FirstCB, F4);
190   F9AA.instructionCanReach(A, F9SecondInst, F3);
191   F9AA.instructionCanReach(A, F9FirstInst, F3);
192   F9AA.instructionCanReach(A, F9FirstInst, F4);
193 
194   A.run();
195 
196   ASSERT_TRUE(F1AA.canReach(A, F3));
197   ASSERT_FALSE(F1AA.canReach(A, F4));
198 
199   ASSERT_TRUE(F7AA.instructionCanReach(A, F7FirstCB, F3));
200   ASSERT_TRUE(F7AA.instructionCanReach(A, F7FirstCB, F4));
201 
202   // Assumed to be reacahable, since F6 can reach a function with
203   // a unknown callee.
204   ASSERT_TRUE(F6AA.canReach(A, F4));
205 
206   // The second instruction of F9 can't reach the first call.
207   ASSERT_FALSE(F9AA.instructionCanReach(A, F9SecondInst, F3));
208 
209   // The first instruction of F9 can reach the first call.
210   ASSERT_TRUE(F9AA.instructionCanReach(A, F9FirstInst, F3));
211   // Because func10 calls the func4 after the call to func9 it is reachable but
212   // as it requires backwards logic we would need AA::isPotentiallyReachable.
213   ASSERT_FALSE(F9AA.instructionCanReach(A, F9FirstInst, F4));
214 }
215 
216 } // namespace llvm
217