1 //===- LazyCallGraphTest.cpp - Unit tests for the lazy CG analysis --------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Analysis/LazyCallGraph.h"
11 #include "llvm/AsmParser/Parser.h"
12 #include "llvm/IR/Function.h"
13 #include "llvm/IR/LLVMContext.h"
14 #include "llvm/IR/Module.h"
15 #include "llvm/Support/ErrorHandling.h"
16 #include "llvm/Support/SourceMgr.h"
17 #include "gtest/gtest.h"
24 std::unique_ptr<Module> parseAssembly(const char *Assembly) {
25 auto M = make_unique<Module>("Module", getGlobalContext());
29 ParseAssemblyString(Assembly, M.get(), Error, M->getContext()) == M.get();
32 raw_string_ostream OS(ErrMsg);
35 // A failure here means that the test itself is buggy.
37 report_fatal_error(OS.str().c_str());
42 // IR forming a call graph with a diamond of triangle-shaped SCCs:
56 // All call edges go up between SCCs, and clockwise around the SCC.
57 static const char DiamondOfTriangles[] =
58 "define void @a1() {\n"
65 "define void @a2() {\n"
70 "define void @a3() {\n"
75 "define void @b1() {\n"
81 "define void @b2() {\n"
86 "define void @b3() {\n"
91 "define void @c1() {\n"
97 "define void @c2() {\n"
102 "define void @c3() {\n"
107 "define void @d1() {\n"
112 "define void @d2() {\n"
117 "define void @d3() {\n"
123 TEST(LazyCallGraphTest, BasicGraphFormation) {
124 std::unique_ptr<Module> M = parseAssembly(DiamondOfTriangles);
125 LazyCallGraph CG(*M);
127 // The order of the entry nodes should be stable w.r.t. the source order of
128 // the IR, and everything in our module is an entry node, so just directly
129 // build variables for each node.
131 LazyCallGraph::Node &A1 = *I++;
132 EXPECT_EQ("a1", A1.getFunction().getName());
133 LazyCallGraph::Node &A2 = *I++;
134 EXPECT_EQ("a2", A2.getFunction().getName());
135 LazyCallGraph::Node &A3 = *I++;
136 EXPECT_EQ("a3", A3.getFunction().getName());
137 LazyCallGraph::Node &B1 = *I++;
138 EXPECT_EQ("b1", B1.getFunction().getName());
139 LazyCallGraph::Node &B2 = *I++;
140 EXPECT_EQ("b2", B2.getFunction().getName());
141 LazyCallGraph::Node &B3 = *I++;
142 EXPECT_EQ("b3", B3.getFunction().getName());
143 LazyCallGraph::Node &C1 = *I++;
144 EXPECT_EQ("c1", C1.getFunction().getName());
145 LazyCallGraph::Node &C2 = *I++;
146 EXPECT_EQ("c2", C2.getFunction().getName());
147 LazyCallGraph::Node &C3 = *I++;
148 EXPECT_EQ("c3", C3.getFunction().getName());
149 LazyCallGraph::Node &D1 = *I++;
150 EXPECT_EQ("d1", D1.getFunction().getName());
151 LazyCallGraph::Node &D2 = *I++;
152 EXPECT_EQ("d2", D2.getFunction().getName());
153 LazyCallGraph::Node &D3 = *I++;
154 EXPECT_EQ("d3", D3.getFunction().getName());
155 EXPECT_EQ(CG.end(), I);
157 // Build vectors and sort them for the rest of the assertions to make them
158 // independent of order.
159 std::vector<std::string> Nodes;
161 for (LazyCallGraph::Node &N : A1)
162 Nodes.push_back(N.getFunction().getName());
163 std::sort(Nodes.begin(), Nodes.end());
164 EXPECT_EQ("a2", Nodes[0]);
165 EXPECT_EQ("b2", Nodes[1]);
166 EXPECT_EQ("c3", Nodes[2]);
169 EXPECT_EQ(A2.end(), std::next(A2.begin()));
170 EXPECT_EQ("a3", A2.begin()->getFunction().getName());
171 EXPECT_EQ(A3.end(), std::next(A3.begin()));
172 EXPECT_EQ("a1", A3.begin()->getFunction().getName());
174 for (LazyCallGraph::Node &N : B1)
175 Nodes.push_back(N.getFunction().getName());
176 std::sort(Nodes.begin(), Nodes.end());
177 EXPECT_EQ("b2", Nodes[0]);
178 EXPECT_EQ("d3", Nodes[1]);
181 EXPECT_EQ(B2.end(), std::next(B2.begin()));
182 EXPECT_EQ("b3", B2.begin()->getFunction().getName());
183 EXPECT_EQ(B3.end(), std::next(B3.begin()));
184 EXPECT_EQ("b1", B3.begin()->getFunction().getName());
186 for (LazyCallGraph::Node &N : C1)
187 Nodes.push_back(N.getFunction().getName());
188 std::sort(Nodes.begin(), Nodes.end());
189 EXPECT_EQ("c2", Nodes[0]);
190 EXPECT_EQ("d2", Nodes[1]);
193 EXPECT_EQ(C2.end(), std::next(C2.begin()));
194 EXPECT_EQ("c3", C2.begin()->getFunction().getName());
195 EXPECT_EQ(C3.end(), std::next(C3.begin()));
196 EXPECT_EQ("c1", C3.begin()->getFunction().getName());
198 EXPECT_EQ(D1.end(), std::next(D1.begin()));
199 EXPECT_EQ("d2", D1.begin()->getFunction().getName());
200 EXPECT_EQ(D2.end(), std::next(D2.begin()));
201 EXPECT_EQ("d3", D2.begin()->getFunction().getName());
202 EXPECT_EQ(D3.end(), std::next(D3.begin()));
203 EXPECT_EQ("d1", D3.begin()->getFunction().getName());
205 // Now lets look at the SCCs.
206 auto SCCI = CG.postorder_scc_begin();
208 LazyCallGraph::SCC &D = *SCCI++;
209 for (LazyCallGraph::Node *N : D)
210 Nodes.push_back(N->getFunction().getName());
211 std::sort(Nodes.begin(), Nodes.end());
212 EXPECT_EQ(3u, Nodes.size());
213 EXPECT_EQ("d1", Nodes[0]);
214 EXPECT_EQ("d2", Nodes[1]);
215 EXPECT_EQ("d3", Nodes[2]);
217 EXPECT_FALSE(D.isParentOf(D));
218 EXPECT_FALSE(D.isChildOf(D));
219 EXPECT_FALSE(D.isAncestorOf(D));
220 EXPECT_FALSE(D.isDescendantOf(D));
222 LazyCallGraph::SCC &C = *SCCI++;
223 for (LazyCallGraph::Node *N : C)
224 Nodes.push_back(N->getFunction().getName());
225 std::sort(Nodes.begin(), Nodes.end());
226 EXPECT_EQ(3u, Nodes.size());
227 EXPECT_EQ("c1", Nodes[0]);
228 EXPECT_EQ("c2", Nodes[1]);
229 EXPECT_EQ("c3", Nodes[2]);
231 EXPECT_TRUE(C.isParentOf(D));
232 EXPECT_FALSE(C.isChildOf(D));
233 EXPECT_TRUE(C.isAncestorOf(D));
234 EXPECT_FALSE(C.isDescendantOf(D));
236 LazyCallGraph::SCC &B = *SCCI++;
237 for (LazyCallGraph::Node *N : B)
238 Nodes.push_back(N->getFunction().getName());
239 std::sort(Nodes.begin(), Nodes.end());
240 EXPECT_EQ(3u, Nodes.size());
241 EXPECT_EQ("b1", Nodes[0]);
242 EXPECT_EQ("b2", Nodes[1]);
243 EXPECT_EQ("b3", Nodes[2]);
245 EXPECT_TRUE(B.isParentOf(D));
246 EXPECT_FALSE(B.isChildOf(D));
247 EXPECT_TRUE(B.isAncestorOf(D));
248 EXPECT_FALSE(B.isDescendantOf(D));
249 EXPECT_FALSE(B.isAncestorOf(C));
250 EXPECT_FALSE(C.isAncestorOf(B));
252 LazyCallGraph::SCC &A = *SCCI++;
253 for (LazyCallGraph::Node *N : A)
254 Nodes.push_back(N->getFunction().getName());
255 std::sort(Nodes.begin(), Nodes.end());
256 EXPECT_EQ(3u, Nodes.size());
257 EXPECT_EQ("a1", Nodes[0]);
258 EXPECT_EQ("a2", Nodes[1]);
259 EXPECT_EQ("a3", Nodes[2]);
261 EXPECT_TRUE(A.isParentOf(B));
262 EXPECT_TRUE(A.isParentOf(C));
263 EXPECT_FALSE(A.isParentOf(D));
264 EXPECT_TRUE(A.isAncestorOf(B));
265 EXPECT_TRUE(A.isAncestorOf(C));
266 EXPECT_TRUE(A.isAncestorOf(D));
268 EXPECT_EQ(CG.postorder_scc_end(), SCCI);
271 static Function &lookupFunction(Module &M, StringRef Name) {
272 for (Function &F : M)
273 if (F.getName() == Name)
275 report_fatal_error("Couldn't find function!");
278 TEST(LazyCallGraphTest, BasicGraphMutation) {
279 std::unique_ptr<Module> M = parseAssembly(
280 "define void @a() {\n"
286 "define void @b() {\n"
290 "define void @c() {\n"
294 LazyCallGraph CG(*M);
296 LazyCallGraph::Node &A = CG.get(lookupFunction(*M, "a"));
297 LazyCallGraph::Node &B = CG.get(lookupFunction(*M, "b"));
298 EXPECT_EQ(2, std::distance(A.begin(), A.end()));
299 EXPECT_EQ(0, std::distance(B.begin(), B.end()));
301 CG.insertEdge(B, lookupFunction(*M, "c"));
302 EXPECT_EQ(1, std::distance(B.begin(), B.end()));
303 LazyCallGraph::Node &C = *B.begin();
304 EXPECT_EQ(0, std::distance(C.begin(), C.end()));
306 CG.insertEdge(C, B.getFunction());
307 EXPECT_EQ(1, std::distance(C.begin(), C.end()));
308 EXPECT_EQ(&B, &*C.begin());
310 CG.insertEdge(C, C.getFunction());
311 EXPECT_EQ(2, std::distance(C.begin(), C.end()));
312 EXPECT_EQ(&B, &*C.begin());
313 EXPECT_EQ(&C, &*std::next(C.begin()));
315 CG.removeEdge(C, B.getFunction());
316 EXPECT_EQ(1, std::distance(C.begin(), C.end()));
317 EXPECT_EQ(&C, &*C.begin());
319 CG.removeEdge(C, C.getFunction());
320 EXPECT_EQ(0, std::distance(C.begin(), C.end()));
322 CG.removeEdge(B, C.getFunction());
323 EXPECT_EQ(0, std::distance(B.begin(), B.end()));
326 TEST(LazyCallGraphTest, MultiArmSCC) {
327 // Two interlocking cycles. The really useful thing about this SCC is that it
328 // will require Tarjan's DFS to backtrack and finish processing all of the
329 // children of each node in the SCC.
330 std::unique_ptr<Module> M = parseAssembly(
331 "define void @a() {\n"
337 "define void @b() {\n"
342 "define void @c() {\n"
347 "define void @d() {\n"
352 "define void @e() {\n"
357 LazyCallGraph CG(*M);
359 // Force the graph to be fully expanded.
360 auto SCCI = CG.postorder_scc_begin();
361 LazyCallGraph::SCC &SCC = *SCCI++;
362 EXPECT_EQ(CG.postorder_scc_end(), SCCI);
364 LazyCallGraph::Node &A = *CG.lookup(lookupFunction(*M, "a"));
365 LazyCallGraph::Node &B = *CG.lookup(lookupFunction(*M, "b"));
366 LazyCallGraph::Node &C = *CG.lookup(lookupFunction(*M, "c"));
367 LazyCallGraph::Node &D = *CG.lookup(lookupFunction(*M, "d"));
368 LazyCallGraph::Node &E = *CG.lookup(lookupFunction(*M, "e"));
369 EXPECT_EQ(&SCC, CG.lookupSCC(A));
370 EXPECT_EQ(&SCC, CG.lookupSCC(B));
371 EXPECT_EQ(&SCC, CG.lookupSCC(C));
372 EXPECT_EQ(&SCC, CG.lookupSCC(D));
373 EXPECT_EQ(&SCC, CG.lookupSCC(E));
376 TEST(LazyCallGraphTest, OutgoingSCCEdgeInsertion) {
377 std::unique_ptr<Module> M = parseAssembly(
378 "define void @a() {\n"
384 "define void @b() {\n"
389 "define void @c() {\n"
394 "define void @d() {\n"
398 LazyCallGraph CG(*M);
400 // Force the graph to be fully expanded.
401 for (LazyCallGraph::SCC &C : CG.postorder_sccs())
404 LazyCallGraph::Node &A = *CG.lookup(lookupFunction(*M, "a"));
405 LazyCallGraph::Node &B = *CG.lookup(lookupFunction(*M, "b"));
406 LazyCallGraph::Node &C = *CG.lookup(lookupFunction(*M, "c"));
407 LazyCallGraph::Node &D = *CG.lookup(lookupFunction(*M, "d"));
408 LazyCallGraph::SCC &AC = *CG.lookupSCC(A);
409 LazyCallGraph::SCC &BC = *CG.lookupSCC(B);
410 LazyCallGraph::SCC &CC = *CG.lookupSCC(C);
411 LazyCallGraph::SCC &DC = *CG.lookupSCC(D);
412 EXPECT_TRUE(AC.isAncestorOf(BC));
413 EXPECT_TRUE(AC.isAncestorOf(CC));
414 EXPECT_TRUE(AC.isAncestorOf(DC));
415 EXPECT_TRUE(DC.isDescendantOf(AC));
416 EXPECT_TRUE(DC.isDescendantOf(BC));
417 EXPECT_TRUE(DC.isDescendantOf(CC));
419 EXPECT_EQ(2, std::distance(A.begin(), A.end()));
420 AC.insertOutgoingEdge(A, D);
421 EXPECT_EQ(3, std::distance(A.begin(), A.end()));
422 EXPECT_TRUE(AC.isParentOf(DC));
423 EXPECT_EQ(&AC, CG.lookupSCC(A));
424 EXPECT_EQ(&BC, CG.lookupSCC(B));
425 EXPECT_EQ(&CC, CG.lookupSCC(C));
426 EXPECT_EQ(&DC, CG.lookupSCC(D));
429 TEST(LazyCallGraphTest, InterSCCEdgeRemoval) {
430 std::unique_ptr<Module> M = parseAssembly(
431 "define void @a() {\n"
436 "define void @b() {\n"
440 LazyCallGraph CG(*M);
442 // Force the graph to be fully expanded.
443 for (LazyCallGraph::SCC &C : CG.postorder_sccs())
446 LazyCallGraph::Node &A = *CG.lookup(lookupFunction(*M, "a"));
447 LazyCallGraph::Node &B = *CG.lookup(lookupFunction(*M, "b"));
448 LazyCallGraph::SCC &AC = *CG.lookupSCC(A);
449 LazyCallGraph::SCC &BC = *CG.lookupSCC(B);
451 EXPECT_EQ("b", A.begin()->getFunction().getName());
452 EXPECT_EQ(B.end(), B.begin());
453 EXPECT_EQ(&AC, &*BC.parent_begin());
455 AC.removeInterSCCEdge(A, B);
457 EXPECT_EQ(A.end(), A.begin());
458 EXPECT_EQ(B.end(), B.begin());
459 EXPECT_EQ(BC.parent_end(), BC.parent_begin());
462 TEST(LazyCallGraphTest, IntraSCCEdgeInsertion) {
463 std::unique_ptr<Module> M1 = parseAssembly(
464 "define void @a() {\n"
469 "define void @b() {\n"
474 "define void @c() {\n"
479 LazyCallGraph CG1(*M1);
481 // Force the graph to be fully expanded.
482 auto SCCI = CG1.postorder_scc_begin();
483 LazyCallGraph::SCC &SCC = *SCCI++;
484 EXPECT_EQ(CG1.postorder_scc_end(), SCCI);
486 LazyCallGraph::Node &A = *CG1.lookup(lookupFunction(*M1, "a"));
487 LazyCallGraph::Node &B = *CG1.lookup(lookupFunction(*M1, "b"));
488 LazyCallGraph::Node &C = *CG1.lookup(lookupFunction(*M1, "c"));
489 EXPECT_EQ(&SCC, CG1.lookupSCC(A));
490 EXPECT_EQ(&SCC, CG1.lookupSCC(B));
491 EXPECT_EQ(&SCC, CG1.lookupSCC(C));
493 // Insert an edge from 'a' to 'c'. Nothing changes about the SCCs.
494 SCC.insertIntraSCCEdge(A, C);
495 EXPECT_EQ(2, std::distance(A.begin(), A.end()));
496 EXPECT_EQ(&SCC, CG1.lookupSCC(A));
497 EXPECT_EQ(&SCC, CG1.lookupSCC(B));
498 EXPECT_EQ(&SCC, CG1.lookupSCC(C));
500 // Insert a self edge from 'a' back to 'a'.
501 SCC.insertIntraSCCEdge(A, A);
502 EXPECT_EQ(3, std::distance(A.begin(), A.end()));
503 EXPECT_EQ(&SCC, CG1.lookupSCC(A));
504 EXPECT_EQ(&SCC, CG1.lookupSCC(B));
505 EXPECT_EQ(&SCC, CG1.lookupSCC(C));
508 TEST(LazyCallGraphTest, IntraSCCEdgeRemoval) {
509 // A nice fully connected (including self-edges) SCC.
510 std::unique_ptr<Module> M1 = parseAssembly(
511 "define void @a() {\n"
518 "define void @b() {\n"
525 "define void @c() {\n"
532 LazyCallGraph CG1(*M1);
534 // Force the graph to be fully expanded.
535 auto SCCI = CG1.postorder_scc_begin();
536 LazyCallGraph::SCC &SCC = *SCCI++;
537 EXPECT_EQ(CG1.postorder_scc_end(), SCCI);
539 LazyCallGraph::Node &A = *CG1.lookup(lookupFunction(*M1, "a"));
540 LazyCallGraph::Node &B = *CG1.lookup(lookupFunction(*M1, "b"));
541 LazyCallGraph::Node &C = *CG1.lookup(lookupFunction(*M1, "c"));
542 EXPECT_EQ(&SCC, CG1.lookupSCC(A));
543 EXPECT_EQ(&SCC, CG1.lookupSCC(B));
544 EXPECT_EQ(&SCC, CG1.lookupSCC(C));
546 // Remove the edge from b -> a, which should leave the 3 functions still in
547 // a single connected component because of a -> b -> c -> a.
548 SmallVector<LazyCallGraph::SCC *, 1> NewSCCs = SCC.removeIntraSCCEdge(B, A);
549 EXPECT_EQ(0u, NewSCCs.size());
550 EXPECT_EQ(&SCC, CG1.lookupSCC(A));
551 EXPECT_EQ(&SCC, CG1.lookupSCC(B));
552 EXPECT_EQ(&SCC, CG1.lookupSCC(C));
554 // Remove the edge from c -> a, which should leave 'a' in the original SCC
555 // and form a new SCC for 'b' and 'c'.
556 NewSCCs = SCC.removeIntraSCCEdge(C, A);
557 EXPECT_EQ(1u, NewSCCs.size());
558 EXPECT_EQ(&SCC, CG1.lookupSCC(A));
559 EXPECT_EQ(1, std::distance(SCC.begin(), SCC.end()));
560 LazyCallGraph::SCC *SCC2 = CG1.lookupSCC(B);
561 EXPECT_EQ(SCC2, CG1.lookupSCC(C));
562 EXPECT_EQ(SCC2, NewSCCs[0]);