xref: /llvm-project/llvm/unittests/Analysis/LazyCallGraphTest.cpp (revision c6334579e997f35c00d3fac251d3f8b462737df6)
1 //===- LazyCallGraphTest.cpp - Unit tests for the lazy CG analysis --------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "llvm/Analysis/LazyCallGraph.h"
11 #include "llvm/AsmParser/Parser.h"
12 #include "llvm/IR/Instructions.h"
13 #include "llvm/IR/Function.h"
14 #include "llvm/IR/LLVMContext.h"
15 #include "llvm/IR/Module.h"
16 #include "llvm/Support/ErrorHandling.h"
17 #include "llvm/Support/SourceMgr.h"
18 #include "gtest/gtest.h"
19 #include <memory>
20 
21 using namespace llvm;
22 
23 namespace {
24 
25 std::unique_ptr<Module> parseAssembly(LLVMContext &Context,
26                                       const char *Assembly) {
27   SMDiagnostic Error;
28   std::unique_ptr<Module> M = parseAssemblyString(Assembly, Error, Context);
29 
30   std::string ErrMsg;
31   raw_string_ostream OS(ErrMsg);
32   Error.print("", OS);
33 
34   // A failure here means that the test itself is buggy.
35   if (!M)
36     report_fatal_error(OS.str().c_str());
37 
38   return M;
39 }
40 
41 /*
42    IR forming a call graph with a diamond of triangle-shaped SCCs:
43 
44            d1
45           /  \
46          d3--d2
47         /     \
48        b1     c1
49      /  \    /  \
50     b3--b2  c3--c2
51          \  /
52           a1
53          /  \
54         a3--a2
55 
56    All call edges go up between SCCs, and clockwise around the SCC.
57  */
58 static const char DiamondOfTriangles[] =
59      "define void @a1() {\n"
60      "entry:\n"
61      "  call void @a2()\n"
62      "  call void @b2()\n"
63      "  call void @c3()\n"
64      "  ret void\n"
65      "}\n"
66      "define void @a2() {\n"
67      "entry:\n"
68      "  call void @a3()\n"
69      "  ret void\n"
70      "}\n"
71      "define void @a3() {\n"
72      "entry:\n"
73      "  call void @a1()\n"
74      "  ret void\n"
75      "}\n"
76      "define void @b1() {\n"
77      "entry:\n"
78      "  call void @b2()\n"
79      "  call void @d3()\n"
80      "  ret void\n"
81      "}\n"
82      "define void @b2() {\n"
83      "entry:\n"
84      "  call void @b3()\n"
85      "  ret void\n"
86      "}\n"
87      "define void @b3() {\n"
88      "entry:\n"
89      "  call void @b1()\n"
90      "  ret void\n"
91      "}\n"
92      "define void @c1() {\n"
93      "entry:\n"
94      "  call void @c2()\n"
95      "  call void @d2()\n"
96      "  ret void\n"
97      "}\n"
98      "define void @c2() {\n"
99      "entry:\n"
100      "  call void @c3()\n"
101      "  ret void\n"
102      "}\n"
103      "define void @c3() {\n"
104      "entry:\n"
105      "  call void @c1()\n"
106      "  ret void\n"
107      "}\n"
108      "define void @d1() {\n"
109      "entry:\n"
110      "  call void @d2()\n"
111      "  ret void\n"
112      "}\n"
113      "define void @d2() {\n"
114      "entry:\n"
115      "  call void @d3()\n"
116      "  ret void\n"
117      "}\n"
118      "define void @d3() {\n"
119      "entry:\n"
120      "  call void @d1()\n"
121      "  ret void\n"
122      "}\n";
123 
124 /*
125    IR forming a reference graph with a diamond of triangle-shaped RefSCCs
126 
127            d1
128           /  \
129          d3--d2
130         /     \
131        b1     c1
132      /  \    /  \
133     b3--b2  c3--c2
134          \  /
135           a1
136          /  \
137         a3--a2
138 
139    All call edges go up between RefSCCs, and clockwise around the RefSCC.
140  */
141 static const char DiamondOfTrianglesRefGraph[] =
142      "define void @a1() {\n"
143      "entry:\n"
144      "  %a = alloca void ()*\n"
145      "  store void ()* @a2, void ()** %a\n"
146      "  store void ()* @b2, void ()** %a\n"
147      "  store void ()* @c3, void ()** %a\n"
148      "  ret void\n"
149      "}\n"
150      "define void @a2() {\n"
151      "entry:\n"
152      "  %a = alloca void ()*\n"
153      "  store void ()* @a3, void ()** %a\n"
154      "  ret void\n"
155      "}\n"
156      "define void @a3() {\n"
157      "entry:\n"
158      "  %a = alloca void ()*\n"
159      "  store void ()* @a1, void ()** %a\n"
160      "  ret void\n"
161      "}\n"
162      "define void @b1() {\n"
163      "entry:\n"
164      "  %a = alloca void ()*\n"
165      "  store void ()* @b2, void ()** %a\n"
166      "  store void ()* @d3, void ()** %a\n"
167      "  ret void\n"
168      "}\n"
169      "define void @b2() {\n"
170      "entry:\n"
171      "  %a = alloca void ()*\n"
172      "  store void ()* @b3, void ()** %a\n"
173      "  ret void\n"
174      "}\n"
175      "define void @b3() {\n"
176      "entry:\n"
177      "  %a = alloca void ()*\n"
178      "  store void ()* @b1, void ()** %a\n"
179      "  ret void\n"
180      "}\n"
181      "define void @c1() {\n"
182      "entry:\n"
183      "  %a = alloca void ()*\n"
184      "  store void ()* @c2, void ()** %a\n"
185      "  store void ()* @d2, void ()** %a\n"
186      "  ret void\n"
187      "}\n"
188      "define void @c2() {\n"
189      "entry:\n"
190      "  %a = alloca void ()*\n"
191      "  store void ()* @c3, void ()** %a\n"
192      "  ret void\n"
193      "}\n"
194      "define void @c3() {\n"
195      "entry:\n"
196      "  %a = alloca void ()*\n"
197      "  store void ()* @c1, void ()** %a\n"
198      "  ret void\n"
199      "}\n"
200      "define void @d1() {\n"
201      "entry:\n"
202      "  %a = alloca void ()*\n"
203      "  store void ()* @d2, void ()** %a\n"
204      "  ret void\n"
205      "}\n"
206      "define void @d2() {\n"
207      "entry:\n"
208      "  %a = alloca void ()*\n"
209      "  store void ()* @d3, void ()** %a\n"
210      "  ret void\n"
211      "}\n"
212      "define void @d3() {\n"
213      "entry:\n"
214      "  %a = alloca void ()*\n"
215      "  store void ()* @d1, void ()** %a\n"
216      "  ret void\n"
217      "}\n";
218 
219 TEST(LazyCallGraphTest, BasicGraphFormation) {
220   LLVMContext Context;
221   std::unique_ptr<Module> M = parseAssembly(Context, DiamondOfTriangles);
222   LazyCallGraph CG(*M);
223 
224   // The order of the entry nodes should be stable w.r.t. the source order of
225   // the IR, and everything in our module is an entry node, so just directly
226   // build variables for each node.
227   auto I = CG.begin();
228   LazyCallGraph::Node &A1 = (I++)->getNode(CG);
229   EXPECT_EQ("a1", A1.getFunction().getName());
230   LazyCallGraph::Node &A2 = (I++)->getNode(CG);
231   EXPECT_EQ("a2", A2.getFunction().getName());
232   LazyCallGraph::Node &A3 = (I++)->getNode(CG);
233   EXPECT_EQ("a3", A3.getFunction().getName());
234   LazyCallGraph::Node &B1 = (I++)->getNode(CG);
235   EXPECT_EQ("b1", B1.getFunction().getName());
236   LazyCallGraph::Node &B2 = (I++)->getNode(CG);
237   EXPECT_EQ("b2", B2.getFunction().getName());
238   LazyCallGraph::Node &B3 = (I++)->getNode(CG);
239   EXPECT_EQ("b3", B3.getFunction().getName());
240   LazyCallGraph::Node &C1 = (I++)->getNode(CG);
241   EXPECT_EQ("c1", C1.getFunction().getName());
242   LazyCallGraph::Node &C2 = (I++)->getNode(CG);
243   EXPECT_EQ("c2", C2.getFunction().getName());
244   LazyCallGraph::Node &C3 = (I++)->getNode(CG);
245   EXPECT_EQ("c3", C3.getFunction().getName());
246   LazyCallGraph::Node &D1 = (I++)->getNode(CG);
247   EXPECT_EQ("d1", D1.getFunction().getName());
248   LazyCallGraph::Node &D2 = (I++)->getNode(CG);
249   EXPECT_EQ("d2", D2.getFunction().getName());
250   LazyCallGraph::Node &D3 = (I++)->getNode(CG);
251   EXPECT_EQ("d3", D3.getFunction().getName());
252   EXPECT_EQ(CG.end(), I);
253 
254   // Build vectors and sort them for the rest of the assertions to make them
255   // independent of order.
256   std::vector<std::string> Nodes;
257 
258   for (LazyCallGraph::Edge &E : A1)
259     Nodes.push_back(E.getFunction().getName());
260   std::sort(Nodes.begin(), Nodes.end());
261   EXPECT_EQ("a2", Nodes[0]);
262   EXPECT_EQ("b2", Nodes[1]);
263   EXPECT_EQ("c3", Nodes[2]);
264   Nodes.clear();
265 
266   EXPECT_EQ(A2.end(), std::next(A2.begin()));
267   EXPECT_EQ("a3", A2.begin()->getFunction().getName());
268   EXPECT_EQ(A3.end(), std::next(A3.begin()));
269   EXPECT_EQ("a1", A3.begin()->getFunction().getName());
270 
271   for (LazyCallGraph::Edge &E : B1)
272     Nodes.push_back(E.getFunction().getName());
273   std::sort(Nodes.begin(), Nodes.end());
274   EXPECT_EQ("b2", Nodes[0]);
275   EXPECT_EQ("d3", Nodes[1]);
276   Nodes.clear();
277 
278   EXPECT_EQ(B2.end(), std::next(B2.begin()));
279   EXPECT_EQ("b3", B2.begin()->getFunction().getName());
280   EXPECT_EQ(B3.end(), std::next(B3.begin()));
281   EXPECT_EQ("b1", B3.begin()->getFunction().getName());
282 
283   for (LazyCallGraph::Edge &E : C1)
284     Nodes.push_back(E.getFunction().getName());
285   std::sort(Nodes.begin(), Nodes.end());
286   EXPECT_EQ("c2", Nodes[0]);
287   EXPECT_EQ("d2", Nodes[1]);
288   Nodes.clear();
289 
290   EXPECT_EQ(C2.end(), std::next(C2.begin()));
291   EXPECT_EQ("c3", C2.begin()->getFunction().getName());
292   EXPECT_EQ(C3.end(), std::next(C3.begin()));
293   EXPECT_EQ("c1", C3.begin()->getFunction().getName());
294 
295   EXPECT_EQ(D1.end(), std::next(D1.begin()));
296   EXPECT_EQ("d2", D1.begin()->getFunction().getName());
297   EXPECT_EQ(D2.end(), std::next(D2.begin()));
298   EXPECT_EQ("d3", D2.begin()->getFunction().getName());
299   EXPECT_EQ(D3.end(), std::next(D3.begin()));
300   EXPECT_EQ("d1", D3.begin()->getFunction().getName());
301 
302   // Now lets look at the RefSCCs and SCCs.
303   auto J = CG.postorder_ref_scc_begin();
304 
305   LazyCallGraph::RefSCC &D = *J++;
306   ASSERT_EQ(1, D.size());
307   for (LazyCallGraph::Node &N : *D.begin())
308     Nodes.push_back(N.getFunction().getName());
309   std::sort(Nodes.begin(), Nodes.end());
310   EXPECT_EQ(3u, Nodes.size());
311   EXPECT_EQ("d1", Nodes[0]);
312   EXPECT_EQ("d2", Nodes[1]);
313   EXPECT_EQ("d3", Nodes[2]);
314   Nodes.clear();
315   EXPECT_FALSE(D.isParentOf(D));
316   EXPECT_FALSE(D.isChildOf(D));
317   EXPECT_FALSE(D.isAncestorOf(D));
318   EXPECT_FALSE(D.isDescendantOf(D));
319   EXPECT_EQ(&D, &*CG.postorder_ref_scc_begin());
320 
321   LazyCallGraph::RefSCC &C = *J++;
322   ASSERT_EQ(1, C.size());
323   for (LazyCallGraph::Node &N : *C.begin())
324     Nodes.push_back(N.getFunction().getName());
325   std::sort(Nodes.begin(), Nodes.end());
326   EXPECT_EQ(3u, Nodes.size());
327   EXPECT_EQ("c1", Nodes[0]);
328   EXPECT_EQ("c2", Nodes[1]);
329   EXPECT_EQ("c3", Nodes[2]);
330   Nodes.clear();
331   EXPECT_TRUE(C.isParentOf(D));
332   EXPECT_FALSE(C.isChildOf(D));
333   EXPECT_TRUE(C.isAncestorOf(D));
334   EXPECT_FALSE(C.isDescendantOf(D));
335   EXPECT_EQ(&C, &*std::next(CG.postorder_ref_scc_begin()));
336 
337   LazyCallGraph::RefSCC &B = *J++;
338   ASSERT_EQ(1, B.size());
339   for (LazyCallGraph::Node &N : *B.begin())
340     Nodes.push_back(N.getFunction().getName());
341   std::sort(Nodes.begin(), Nodes.end());
342   EXPECT_EQ(3u, Nodes.size());
343   EXPECT_EQ("b1", Nodes[0]);
344   EXPECT_EQ("b2", Nodes[1]);
345   EXPECT_EQ("b3", Nodes[2]);
346   Nodes.clear();
347   EXPECT_TRUE(B.isParentOf(D));
348   EXPECT_FALSE(B.isChildOf(D));
349   EXPECT_TRUE(B.isAncestorOf(D));
350   EXPECT_FALSE(B.isDescendantOf(D));
351   EXPECT_FALSE(B.isAncestorOf(C));
352   EXPECT_FALSE(C.isAncestorOf(B));
353   EXPECT_EQ(&B, &*std::next(CG.postorder_ref_scc_begin(), 2));
354 
355   LazyCallGraph::RefSCC &A = *J++;
356   ASSERT_EQ(1, A.size());
357   for (LazyCallGraph::Node &N : *A.begin())
358     Nodes.push_back(N.getFunction().getName());
359   std::sort(Nodes.begin(), Nodes.end());
360   EXPECT_EQ(3u, Nodes.size());
361   EXPECT_EQ("a1", Nodes[0]);
362   EXPECT_EQ("a2", Nodes[1]);
363   EXPECT_EQ("a3", Nodes[2]);
364   Nodes.clear();
365   EXPECT_TRUE(A.isParentOf(B));
366   EXPECT_TRUE(A.isParentOf(C));
367   EXPECT_FALSE(A.isParentOf(D));
368   EXPECT_TRUE(A.isAncestorOf(B));
369   EXPECT_TRUE(A.isAncestorOf(C));
370   EXPECT_TRUE(A.isAncestorOf(D));
371   EXPECT_EQ(&A, &*std::next(CG.postorder_ref_scc_begin(), 3));
372 
373   EXPECT_EQ(CG.postorder_ref_scc_end(), J);
374   EXPECT_EQ(J, std::next(CG.postorder_ref_scc_begin(), 4));
375 }
376 
377 static Function &lookupFunction(Module &M, StringRef Name) {
378   for (Function &F : M)
379     if (F.getName() == Name)
380       return F;
381   report_fatal_error("Couldn't find function!");
382 }
383 
384 TEST(LazyCallGraphTest, BasicGraphMutation) {
385   LLVMContext Context;
386   std::unique_ptr<Module> M = parseAssembly(Context, "define void @a() {\n"
387                                                      "entry:\n"
388                                                      "  call void @b()\n"
389                                                      "  call void @c()\n"
390                                                      "  ret void\n"
391                                                      "}\n"
392                                                      "define void @b() {\n"
393                                                      "entry:\n"
394                                                      "  ret void\n"
395                                                      "}\n"
396                                                      "define void @c() {\n"
397                                                      "entry:\n"
398                                                      "  ret void\n"
399                                                      "}\n");
400   LazyCallGraph CG(*M);
401 
402   LazyCallGraph::Node &A = CG.get(lookupFunction(*M, "a"));
403   LazyCallGraph::Node &B = CG.get(lookupFunction(*M, "b"));
404   EXPECT_EQ(2, std::distance(A.begin(), A.end()));
405   EXPECT_EQ(0, std::distance(B.begin(), B.end()));
406 
407   CG.insertEdge(B, lookupFunction(*M, "c"), LazyCallGraph::Edge::Call);
408   EXPECT_EQ(1, std::distance(B.begin(), B.end()));
409   LazyCallGraph::Node &C = B.begin()->getNode(CG);
410   EXPECT_EQ(0, std::distance(C.begin(), C.end()));
411 
412   CG.insertEdge(C, B.getFunction(), LazyCallGraph::Edge::Call);
413   EXPECT_EQ(1, std::distance(C.begin(), C.end()));
414   EXPECT_EQ(&B, C.begin()->getNode());
415 
416   CG.insertEdge(C, C.getFunction(), LazyCallGraph::Edge::Call);
417   EXPECT_EQ(2, std::distance(C.begin(), C.end()));
418   EXPECT_EQ(&B, C.begin()->getNode());
419   EXPECT_EQ(&C, std::next(C.begin())->getNode());
420 
421   CG.removeEdge(C, B.getFunction());
422   EXPECT_EQ(1, std::distance(C.begin(), C.end()));
423   EXPECT_EQ(&C, C.begin()->getNode());
424 
425   CG.removeEdge(C, C.getFunction());
426   EXPECT_EQ(0, std::distance(C.begin(), C.end()));
427 
428   CG.removeEdge(B, C.getFunction());
429   EXPECT_EQ(0, std::distance(B.begin(), B.end()));
430 }
431 
432 TEST(LazyCallGraphTest, InnerSCCFormation) {
433   LLVMContext Context;
434   std::unique_ptr<Module> M = parseAssembly(Context, DiamondOfTriangles);
435   LazyCallGraph CG(*M);
436 
437   // Now mutate the graph to connect every node into a single RefSCC to ensure
438   // that our inner SCC formation handles the rest.
439   CG.insertEdge(lookupFunction(*M, "d1"), lookupFunction(*M, "a1"),
440                 LazyCallGraph::Edge::Ref);
441 
442   // Build vectors and sort them for the rest of the assertions to make them
443   // independent of order.
444   std::vector<std::string> Nodes;
445 
446   // We should build a single RefSCC for the entire graph.
447   auto I = CG.postorder_ref_scc_begin();
448   LazyCallGraph::RefSCC &RC = *I++;
449   EXPECT_EQ(CG.postorder_ref_scc_end(), I);
450 
451   // Now walk the four SCCs which should be in post-order.
452   auto J = RC.begin();
453   LazyCallGraph::SCC &D = *J++;
454   for (LazyCallGraph::Node &N : D)
455     Nodes.push_back(N.getFunction().getName());
456   std::sort(Nodes.begin(), Nodes.end());
457   EXPECT_EQ(3u, Nodes.size());
458   EXPECT_EQ("d1", Nodes[0]);
459   EXPECT_EQ("d2", Nodes[1]);
460   EXPECT_EQ("d3", Nodes[2]);
461   Nodes.clear();
462 
463   LazyCallGraph::SCC &B = *J++;
464   for (LazyCallGraph::Node &N : B)
465     Nodes.push_back(N.getFunction().getName());
466   std::sort(Nodes.begin(), Nodes.end());
467   EXPECT_EQ(3u, Nodes.size());
468   EXPECT_EQ("b1", Nodes[0]);
469   EXPECT_EQ("b2", Nodes[1]);
470   EXPECT_EQ("b3", Nodes[2]);
471   Nodes.clear();
472 
473   LazyCallGraph::SCC &C = *J++;
474   for (LazyCallGraph::Node &N : C)
475     Nodes.push_back(N.getFunction().getName());
476   std::sort(Nodes.begin(), Nodes.end());
477   EXPECT_EQ(3u, Nodes.size());
478   EXPECT_EQ("c1", Nodes[0]);
479   EXPECT_EQ("c2", Nodes[1]);
480   EXPECT_EQ("c3", Nodes[2]);
481   Nodes.clear();
482 
483   LazyCallGraph::SCC &A = *J++;
484   for (LazyCallGraph::Node &N : A)
485     Nodes.push_back(N.getFunction().getName());
486   std::sort(Nodes.begin(), Nodes.end());
487   EXPECT_EQ(3u, Nodes.size());
488   EXPECT_EQ("a1", Nodes[0]);
489   EXPECT_EQ("a2", Nodes[1]);
490   EXPECT_EQ("a3", Nodes[2]);
491   Nodes.clear();
492 
493   EXPECT_EQ(RC.end(), J);
494 }
495 
496 TEST(LazyCallGraphTest, MultiArmSCC) {
497   LLVMContext Context;
498   // Two interlocking cycles. The really useful thing about this SCC is that it
499   // will require Tarjan's DFS to backtrack and finish processing all of the
500   // children of each node in the SCC. Since this involves call edges, both
501   // Tarjan implementations will have to successfully navigate the structure.
502   std::unique_ptr<Module> M = parseAssembly(Context, "define void @f1() {\n"
503                                                      "entry:\n"
504                                                      "  call void @f2()\n"
505                                                      "  call void @f4()\n"
506                                                      "  ret void\n"
507                                                      "}\n"
508                                                      "define void @f2() {\n"
509                                                      "entry:\n"
510                                                      "  call void @f3()\n"
511                                                      "  ret void\n"
512                                                      "}\n"
513                                                      "define void @f3() {\n"
514                                                      "entry:\n"
515                                                      "  call void @f1()\n"
516                                                      "  ret void\n"
517                                                      "}\n"
518                                                      "define void @f4() {\n"
519                                                      "entry:\n"
520                                                      "  call void @f5()\n"
521                                                      "  ret void\n"
522                                                      "}\n"
523                                                      "define void @f5() {\n"
524                                                      "entry:\n"
525                                                      "  call void @f1()\n"
526                                                      "  ret void\n"
527                                                      "}\n");
528   LazyCallGraph CG(*M);
529 
530   // Force the graph to be fully expanded.
531   auto I = CG.postorder_ref_scc_begin();
532   LazyCallGraph::RefSCC &RC = *I++;
533   EXPECT_EQ(CG.postorder_ref_scc_end(), I);
534 
535   LazyCallGraph::Node &N1 = *CG.lookup(lookupFunction(*M, "f1"));
536   LazyCallGraph::Node &N2 = *CG.lookup(lookupFunction(*M, "f2"));
537   LazyCallGraph::Node &N3 = *CG.lookup(lookupFunction(*M, "f3"));
538   LazyCallGraph::Node &N4 = *CG.lookup(lookupFunction(*M, "f4"));
539   LazyCallGraph::Node &N5 = *CG.lookup(lookupFunction(*M, "f4"));
540   EXPECT_EQ(&RC, CG.lookupRefSCC(N1));
541   EXPECT_EQ(&RC, CG.lookupRefSCC(N2));
542   EXPECT_EQ(&RC, CG.lookupRefSCC(N3));
543   EXPECT_EQ(&RC, CG.lookupRefSCC(N4));
544   EXPECT_EQ(&RC, CG.lookupRefSCC(N5));
545 
546   ASSERT_EQ(1, RC.size());
547 
548   LazyCallGraph::SCC &C = *RC.begin();
549   EXPECT_EQ(&C, CG.lookupSCC(N1));
550   EXPECT_EQ(&C, CG.lookupSCC(N2));
551   EXPECT_EQ(&C, CG.lookupSCC(N3));
552   EXPECT_EQ(&C, CG.lookupSCC(N4));
553   EXPECT_EQ(&C, CG.lookupSCC(N5));
554 }
555 
556 TEST(LazyCallGraphTest, OutgoingEdgeMutation) {
557   LLVMContext Context;
558   std::unique_ptr<Module> M = parseAssembly(Context, "define void @a() {\n"
559                                                      "entry:\n"
560                                                      "  call void @b()\n"
561                                                      "  call void @c()\n"
562                                                      "  ret void\n"
563                                                      "}\n"
564                                                      "define void @b() {\n"
565                                                      "entry:\n"
566                                                      "  call void @d()\n"
567                                                      "  ret void\n"
568                                                      "}\n"
569                                                      "define void @c() {\n"
570                                                      "entry:\n"
571                                                      "  call void @d()\n"
572                                                      "  ret void\n"
573                                                      "}\n"
574                                                      "define void @d() {\n"
575                                                      "entry:\n"
576                                                      "  ret void\n"
577                                                      "}\n");
578   LazyCallGraph CG(*M);
579 
580   // Force the graph to be fully expanded.
581   for (LazyCallGraph::RefSCC &RC : CG.postorder_ref_sccs())
582     dbgs() << "Formed RefSCC: " << RC << "\n";
583 
584   LazyCallGraph::Node &A = *CG.lookup(lookupFunction(*M, "a"));
585   LazyCallGraph::Node &B = *CG.lookup(lookupFunction(*M, "b"));
586   LazyCallGraph::Node &C = *CG.lookup(lookupFunction(*M, "c"));
587   LazyCallGraph::Node &D = *CG.lookup(lookupFunction(*M, "d"));
588   LazyCallGraph::SCC &AC = *CG.lookupSCC(A);
589   LazyCallGraph::SCC &BC = *CG.lookupSCC(B);
590   LazyCallGraph::SCC &CC = *CG.lookupSCC(C);
591   LazyCallGraph::SCC &DC = *CG.lookupSCC(D);
592   LazyCallGraph::RefSCC &ARC = *CG.lookupRefSCC(A);
593   LazyCallGraph::RefSCC &BRC = *CG.lookupRefSCC(B);
594   LazyCallGraph::RefSCC &CRC = *CG.lookupRefSCC(C);
595   LazyCallGraph::RefSCC &DRC = *CG.lookupRefSCC(D);
596   EXPECT_TRUE(ARC.isParentOf(BRC));
597   EXPECT_TRUE(AC.isParentOf(BC));
598   EXPECT_TRUE(ARC.isParentOf(CRC));
599   EXPECT_TRUE(AC.isParentOf(CC));
600   EXPECT_FALSE(ARC.isParentOf(DRC));
601   EXPECT_FALSE(AC.isParentOf(DC));
602   EXPECT_TRUE(ARC.isAncestorOf(DRC));
603   EXPECT_TRUE(AC.isAncestorOf(DC));
604   EXPECT_FALSE(DRC.isChildOf(ARC));
605   EXPECT_FALSE(DC.isChildOf(AC));
606   EXPECT_TRUE(DRC.isDescendantOf(ARC));
607   EXPECT_TRUE(DC.isDescendantOf(AC));
608   EXPECT_TRUE(DRC.isChildOf(BRC));
609   EXPECT_TRUE(DC.isChildOf(BC));
610   EXPECT_TRUE(DRC.isChildOf(CRC));
611   EXPECT_TRUE(DC.isChildOf(CC));
612 
613   EXPECT_EQ(2, std::distance(A.begin(), A.end()));
614   ARC.insertOutgoingEdge(A, D, LazyCallGraph::Edge::Call);
615   EXPECT_EQ(3, std::distance(A.begin(), A.end()));
616   const LazyCallGraph::Edge &NewE = A[D];
617   EXPECT_TRUE(NewE);
618   EXPECT_TRUE(NewE.isCall());
619   EXPECT_EQ(&D, NewE.getNode());
620 
621   // Only the parent and child tests sholud have changed. The rest of the graph
622   // remains the same.
623   EXPECT_TRUE(ARC.isParentOf(DRC));
624   EXPECT_TRUE(AC.isParentOf(DC));
625   EXPECT_TRUE(ARC.isAncestorOf(DRC));
626   EXPECT_TRUE(AC.isAncestorOf(DC));
627   EXPECT_TRUE(DRC.isChildOf(ARC));
628   EXPECT_TRUE(DC.isChildOf(AC));
629   EXPECT_TRUE(DRC.isDescendantOf(ARC));
630   EXPECT_TRUE(DC.isDescendantOf(AC));
631   EXPECT_EQ(&AC, CG.lookupSCC(A));
632   EXPECT_EQ(&BC, CG.lookupSCC(B));
633   EXPECT_EQ(&CC, CG.lookupSCC(C));
634   EXPECT_EQ(&DC, CG.lookupSCC(D));
635   EXPECT_EQ(&ARC, CG.lookupRefSCC(A));
636   EXPECT_EQ(&BRC, CG.lookupRefSCC(B));
637   EXPECT_EQ(&CRC, CG.lookupRefSCC(C));
638   EXPECT_EQ(&DRC, CG.lookupRefSCC(D));
639 
640   ARC.switchOutgoingEdgeToRef(A, D);
641   EXPECT_FALSE(NewE.isCall());
642 
643   // Verify the reference graph remains the same but the SCC graph is updated.
644   EXPECT_TRUE(ARC.isParentOf(DRC));
645   EXPECT_FALSE(AC.isParentOf(DC));
646   EXPECT_TRUE(ARC.isAncestorOf(DRC));
647   EXPECT_TRUE(AC.isAncestorOf(DC));
648   EXPECT_TRUE(DRC.isChildOf(ARC));
649   EXPECT_FALSE(DC.isChildOf(AC));
650   EXPECT_TRUE(DRC.isDescendantOf(ARC));
651   EXPECT_TRUE(DC.isDescendantOf(AC));
652   EXPECT_EQ(&AC, CG.lookupSCC(A));
653   EXPECT_EQ(&BC, CG.lookupSCC(B));
654   EXPECT_EQ(&CC, CG.lookupSCC(C));
655   EXPECT_EQ(&DC, CG.lookupSCC(D));
656   EXPECT_EQ(&ARC, CG.lookupRefSCC(A));
657   EXPECT_EQ(&BRC, CG.lookupRefSCC(B));
658   EXPECT_EQ(&CRC, CG.lookupRefSCC(C));
659   EXPECT_EQ(&DRC, CG.lookupRefSCC(D));
660 
661   ARC.switchOutgoingEdgeToCall(A, D);
662   EXPECT_TRUE(NewE.isCall());
663 
664   // Verify the reference graph remains the same but the SCC graph is updated.
665   EXPECT_TRUE(ARC.isParentOf(DRC));
666   EXPECT_TRUE(AC.isParentOf(DC));
667   EXPECT_TRUE(ARC.isAncestorOf(DRC));
668   EXPECT_TRUE(AC.isAncestorOf(DC));
669   EXPECT_TRUE(DRC.isChildOf(ARC));
670   EXPECT_TRUE(DC.isChildOf(AC));
671   EXPECT_TRUE(DRC.isDescendantOf(ARC));
672   EXPECT_TRUE(DC.isDescendantOf(AC));
673   EXPECT_EQ(&AC, CG.lookupSCC(A));
674   EXPECT_EQ(&BC, CG.lookupSCC(B));
675   EXPECT_EQ(&CC, CG.lookupSCC(C));
676   EXPECT_EQ(&DC, CG.lookupSCC(D));
677   EXPECT_EQ(&ARC, CG.lookupRefSCC(A));
678   EXPECT_EQ(&BRC, CG.lookupRefSCC(B));
679   EXPECT_EQ(&CRC, CG.lookupRefSCC(C));
680   EXPECT_EQ(&DRC, CG.lookupRefSCC(D));
681 
682   ARC.removeOutgoingEdge(A, D);
683   EXPECT_EQ(2, std::distance(A.begin(), A.end()));
684 
685   // Now the parent and child tests fail again but the rest remains the same.
686   EXPECT_FALSE(ARC.isParentOf(DRC));
687   EXPECT_FALSE(AC.isParentOf(DC));
688   EXPECT_TRUE(ARC.isAncestorOf(DRC));
689   EXPECT_TRUE(AC.isAncestorOf(DC));
690   EXPECT_FALSE(DRC.isChildOf(ARC));
691   EXPECT_FALSE(DC.isChildOf(AC));
692   EXPECT_TRUE(DRC.isDescendantOf(ARC));
693   EXPECT_TRUE(DC.isDescendantOf(AC));
694   EXPECT_EQ(&AC, CG.lookupSCC(A));
695   EXPECT_EQ(&BC, CG.lookupSCC(B));
696   EXPECT_EQ(&CC, CG.lookupSCC(C));
697   EXPECT_EQ(&DC, CG.lookupSCC(D));
698   EXPECT_EQ(&ARC, CG.lookupRefSCC(A));
699   EXPECT_EQ(&BRC, CG.lookupRefSCC(B));
700   EXPECT_EQ(&CRC, CG.lookupRefSCC(C));
701   EXPECT_EQ(&DRC, CG.lookupRefSCC(D));
702 }
703 
704 TEST(LazyCallGraphTest, IncomingEdgeInsertion) {
705   LLVMContext Context;
706   // We want to ensure we can add edges even across complex diamond graphs, so
707   // we use the diamond of triangles graph defined above. The ascii diagram is
708   // repeated here for easy reference.
709   //
710   //         d1       |
711   //        /  \      |
712   //       d3--d2     |
713   //      /     \     |
714   //     b1     c1    |
715   //   /  \    /  \   |
716   //  b3--b2  c3--c2  |
717   //       \  /       |
718   //        a1        |
719   //       /  \       |
720   //      a3--a2      |
721   //
722   std::unique_ptr<Module> M = parseAssembly(Context, DiamondOfTriangles);
723   LazyCallGraph CG(*M);
724 
725   // Force the graph to be fully expanded.
726   for (LazyCallGraph::RefSCC &RC : CG.postorder_ref_sccs())
727     dbgs() << "Formed RefSCC: " << RC << "\n";
728 
729   LazyCallGraph::Node &A1 = *CG.lookup(lookupFunction(*M, "a1"));
730   LazyCallGraph::Node &A2 = *CG.lookup(lookupFunction(*M, "a2"));
731   LazyCallGraph::Node &A3 = *CG.lookup(lookupFunction(*M, "a3"));
732   LazyCallGraph::Node &B1 = *CG.lookup(lookupFunction(*M, "b1"));
733   LazyCallGraph::Node &B2 = *CG.lookup(lookupFunction(*M, "b2"));
734   LazyCallGraph::Node &B3 = *CG.lookup(lookupFunction(*M, "b3"));
735   LazyCallGraph::Node &C1 = *CG.lookup(lookupFunction(*M, "c1"));
736   LazyCallGraph::Node &C2 = *CG.lookup(lookupFunction(*M, "c2"));
737   LazyCallGraph::Node &C3 = *CG.lookup(lookupFunction(*M, "c3"));
738   LazyCallGraph::Node &D1 = *CG.lookup(lookupFunction(*M, "d1"));
739   LazyCallGraph::Node &D2 = *CG.lookup(lookupFunction(*M, "d2"));
740   LazyCallGraph::Node &D3 = *CG.lookup(lookupFunction(*M, "d3"));
741   LazyCallGraph::RefSCC &ARC = *CG.lookupRefSCC(A1);
742   LazyCallGraph::RefSCC &BRC = *CG.lookupRefSCC(B1);
743   LazyCallGraph::RefSCC &CRC = *CG.lookupRefSCC(C1);
744   LazyCallGraph::RefSCC &DRC = *CG.lookupRefSCC(D1);
745   ASSERT_EQ(&ARC, CG.lookupRefSCC(A2));
746   ASSERT_EQ(&ARC, CG.lookupRefSCC(A3));
747   ASSERT_EQ(&BRC, CG.lookupRefSCC(B2));
748   ASSERT_EQ(&BRC, CG.lookupRefSCC(B3));
749   ASSERT_EQ(&CRC, CG.lookupRefSCC(C2));
750   ASSERT_EQ(&CRC, CG.lookupRefSCC(C3));
751   ASSERT_EQ(&DRC, CG.lookupRefSCC(D2));
752   ASSERT_EQ(&DRC, CG.lookupRefSCC(D3));
753   ASSERT_EQ(1, std::distance(D2.begin(), D2.end()));
754 
755   // Add an edge to make the graph:
756   //
757   //         d1         |
758   //        /  \        |
759   //       d3--d2---.   |
760   //      /     \    |  |
761   //     b1     c1   |  |
762   //   /  \    /  \ /   |
763   //  b3--b2  c3--c2    |
764   //       \  /         |
765   //        a1          |
766   //       /  \         |
767   //      a3--a2        |
768   auto MergedRCs = CRC.insertIncomingRefEdge(D2, C2);
769   // Make sure we connected the nodes.
770   for (LazyCallGraph::Edge E : D2) {
771     if (E.getNode() == &D3)
772       continue;
773     EXPECT_EQ(&C2, E.getNode());
774   }
775   // And marked the D ref-SCC as no longer valid.
776   EXPECT_EQ(1u, MergedRCs.size());
777   EXPECT_EQ(&DRC, MergedRCs[0]);
778 
779   // Make sure we have the correct nodes in the SCC sets.
780   EXPECT_EQ(&ARC, CG.lookupRefSCC(A1));
781   EXPECT_EQ(&ARC, CG.lookupRefSCC(A2));
782   EXPECT_EQ(&ARC, CG.lookupRefSCC(A3));
783   EXPECT_EQ(&BRC, CG.lookupRefSCC(B1));
784   EXPECT_EQ(&BRC, CG.lookupRefSCC(B2));
785   EXPECT_EQ(&BRC, CG.lookupRefSCC(B3));
786   EXPECT_EQ(&CRC, CG.lookupRefSCC(C1));
787   EXPECT_EQ(&CRC, CG.lookupRefSCC(C2));
788   EXPECT_EQ(&CRC, CG.lookupRefSCC(C3));
789   EXPECT_EQ(&CRC, CG.lookupRefSCC(D1));
790   EXPECT_EQ(&CRC, CG.lookupRefSCC(D2));
791   EXPECT_EQ(&CRC, CG.lookupRefSCC(D3));
792 
793   // And that ancestry tests have been updated.
794   EXPECT_TRUE(ARC.isParentOf(CRC));
795   EXPECT_TRUE(BRC.isParentOf(CRC));
796 
797   // And verify the post-order walk reflects the updated structure.
798   auto I = CG.postorder_ref_scc_begin(), E = CG.postorder_ref_scc_end();
799   ASSERT_NE(I, E);
800   EXPECT_EQ(&CRC, &*I) << "Actual RefSCC: " << *I;
801   ASSERT_NE(++I, E);
802   EXPECT_EQ(&BRC, &*I) << "Actual RefSCC: " << *I;
803   ASSERT_NE(++I, E);
804   EXPECT_EQ(&ARC, &*I) << "Actual RefSCC: " << *I;
805   EXPECT_EQ(++I, E);
806 }
807 
808 TEST(LazyCallGraphTest, IncomingEdgeInsertionMidTraversal) {
809   LLVMContext Context;
810   // This is the same fundamental test as the previous, but we perform it
811   // having only partially walked the RefSCCs of the graph.
812   std::unique_ptr<Module> M = parseAssembly(Context, DiamondOfTriangles);
813   LazyCallGraph CG(*M);
814 
815   // Walk the RefSCCs until we find the one containing 'c1'.
816   auto I = CG.postorder_ref_scc_begin(), E = CG.postorder_ref_scc_end();
817   ASSERT_NE(I, E);
818   LazyCallGraph::RefSCC &DRC = *I;
819   ASSERT_NE(&DRC, nullptr);
820   ++I;
821   ASSERT_NE(I, E);
822   LazyCallGraph::RefSCC &CRC = *I;
823   ASSERT_NE(&CRC, nullptr);
824 
825   ASSERT_EQ(nullptr, CG.lookup(lookupFunction(*M, "a1")));
826   ASSERT_EQ(nullptr, CG.lookup(lookupFunction(*M, "a2")));
827   ASSERT_EQ(nullptr, CG.lookup(lookupFunction(*M, "a3")));
828   ASSERT_EQ(nullptr, CG.lookup(lookupFunction(*M, "b1")));
829   ASSERT_EQ(nullptr, CG.lookup(lookupFunction(*M, "b2")));
830   ASSERT_EQ(nullptr, CG.lookup(lookupFunction(*M, "b3")));
831   LazyCallGraph::Node &C1 = *CG.lookup(lookupFunction(*M, "c1"));
832   LazyCallGraph::Node &C2 = *CG.lookup(lookupFunction(*M, "c2"));
833   LazyCallGraph::Node &C3 = *CG.lookup(lookupFunction(*M, "c3"));
834   LazyCallGraph::Node &D1 = *CG.lookup(lookupFunction(*M, "d1"));
835   LazyCallGraph::Node &D2 = *CG.lookup(lookupFunction(*M, "d2"));
836   LazyCallGraph::Node &D3 = *CG.lookup(lookupFunction(*M, "d3"));
837   ASSERT_EQ(&CRC, CG.lookupRefSCC(C1));
838   ASSERT_EQ(&CRC, CG.lookupRefSCC(C2));
839   ASSERT_EQ(&CRC, CG.lookupRefSCC(C3));
840   ASSERT_EQ(&DRC, CG.lookupRefSCC(D1));
841   ASSERT_EQ(&DRC, CG.lookupRefSCC(D2));
842   ASSERT_EQ(&DRC, CG.lookupRefSCC(D3));
843   ASSERT_EQ(1, std::distance(D2.begin(), D2.end()));
844 
845   auto MergedRCs = CRC.insertIncomingRefEdge(D2, C2);
846   // Make sure we connected the nodes.
847   for (LazyCallGraph::Edge E : D2) {
848     if (E.getNode() == &D3)
849       continue;
850     EXPECT_EQ(&C2, E.getNode());
851   }
852   // And marked the D ref-SCC as no longer valid.
853   EXPECT_EQ(1u, MergedRCs.size());
854   EXPECT_EQ(&DRC, MergedRCs[0]);
855 
856   // Make sure we have the correct nodes in the RefSCCs.
857   EXPECT_EQ(&CRC, CG.lookupRefSCC(C1));
858   EXPECT_EQ(&CRC, CG.lookupRefSCC(C2));
859   EXPECT_EQ(&CRC, CG.lookupRefSCC(C3));
860   EXPECT_EQ(&CRC, CG.lookupRefSCC(D1));
861   EXPECT_EQ(&CRC, CG.lookupRefSCC(D2));
862   EXPECT_EQ(&CRC, CG.lookupRefSCC(D3));
863 
864   // Verify that the post-order walk reflects the updated but still incomplete
865   // structure.
866   auto J = CG.postorder_ref_scc_begin();
867   EXPECT_NE(J, E);
868   EXPECT_EQ(&CRC, &*J) << "Actual RefSCC: " << *J;
869   EXPECT_EQ(I, J);
870 
871   // Check that we can form the last two RefSCCs now, and even that we can do
872   // it with alternating iterators.
873   ++J;
874   EXPECT_NE(J, E);
875   LazyCallGraph::RefSCC &BRC = *J;
876   EXPECT_NE(&BRC, nullptr);
877   EXPECT_EQ(&BRC, CG.lookupRefSCC(*CG.lookup(lookupFunction(*M, "b1"))));
878   EXPECT_EQ(&BRC, CG.lookupRefSCC(*CG.lookup(lookupFunction(*M, "b2"))));
879   EXPECT_EQ(&BRC, CG.lookupRefSCC(*CG.lookup(lookupFunction(*M, "b3"))));
880   EXPECT_TRUE(BRC.isParentOf(CRC));
881   ++I;
882   EXPECT_EQ(J, I);
883   EXPECT_EQ(&BRC, &*I) << "Actual RefSCC: " << *I;
884 
885   // Increment I this time to form the new RefSCC, flopping back to the first
886   // iterator.
887   ++I;
888   EXPECT_NE(I, E);
889   LazyCallGraph::RefSCC &ARC = *I;
890   EXPECT_NE(&ARC, nullptr);
891   EXPECT_EQ(&ARC, CG.lookupRefSCC(*CG.lookup(lookupFunction(*M, "a1"))));
892   EXPECT_EQ(&ARC, CG.lookupRefSCC(*CG.lookup(lookupFunction(*M, "a2"))));
893   EXPECT_EQ(&ARC, CG.lookupRefSCC(*CG.lookup(lookupFunction(*M, "a3"))));
894   EXPECT_TRUE(ARC.isParentOf(CRC));
895   ++J;
896   EXPECT_EQ(I, J);
897   EXPECT_EQ(&ARC, &*J) << "Actual RefSCC: " << *J;
898   ++I;
899   EXPECT_EQ(E, I);
900   ++J;
901   EXPECT_EQ(E, J);
902 }
903 
904 TEST(LazyCallGraphTest, IncomingEdgeInsertionRefGraph) {
905   LLVMContext Context;
906   // Another variation of the above test but with all the edges switched to
907   // references rather than calls.
908   std::unique_ptr<Module> M =
909       parseAssembly(Context, DiamondOfTrianglesRefGraph);
910   LazyCallGraph CG(*M);
911 
912   // Force the graph to be fully expanded.
913   for (LazyCallGraph::RefSCC &RC : CG.postorder_ref_sccs())
914     dbgs() << "Formed RefSCC: " << RC << "\n";
915 
916   LazyCallGraph::Node &A1 = *CG.lookup(lookupFunction(*M, "a1"));
917   LazyCallGraph::Node &A2 = *CG.lookup(lookupFunction(*M, "a2"));
918   LazyCallGraph::Node &A3 = *CG.lookup(lookupFunction(*M, "a3"));
919   LazyCallGraph::Node &B1 = *CG.lookup(lookupFunction(*M, "b1"));
920   LazyCallGraph::Node &B2 = *CG.lookup(lookupFunction(*M, "b2"));
921   LazyCallGraph::Node &B3 = *CG.lookup(lookupFunction(*M, "b3"));
922   LazyCallGraph::Node &C1 = *CG.lookup(lookupFunction(*M, "c1"));
923   LazyCallGraph::Node &C2 = *CG.lookup(lookupFunction(*M, "c2"));
924   LazyCallGraph::Node &C3 = *CG.lookup(lookupFunction(*M, "c3"));
925   LazyCallGraph::Node &D1 = *CG.lookup(lookupFunction(*M, "d1"));
926   LazyCallGraph::Node &D2 = *CG.lookup(lookupFunction(*M, "d2"));
927   LazyCallGraph::Node &D3 = *CG.lookup(lookupFunction(*M, "d3"));
928   LazyCallGraph::RefSCC &ARC = *CG.lookupRefSCC(A1);
929   LazyCallGraph::RefSCC &BRC = *CG.lookupRefSCC(B1);
930   LazyCallGraph::RefSCC &CRC = *CG.lookupRefSCC(C1);
931   LazyCallGraph::RefSCC &DRC = *CG.lookupRefSCC(D1);
932   ASSERT_EQ(&ARC, CG.lookupRefSCC(A2));
933   ASSERT_EQ(&ARC, CG.lookupRefSCC(A3));
934   ASSERT_EQ(&BRC, CG.lookupRefSCC(B2));
935   ASSERT_EQ(&BRC, CG.lookupRefSCC(B3));
936   ASSERT_EQ(&CRC, CG.lookupRefSCC(C2));
937   ASSERT_EQ(&CRC, CG.lookupRefSCC(C3));
938   ASSERT_EQ(&DRC, CG.lookupRefSCC(D2));
939   ASSERT_EQ(&DRC, CG.lookupRefSCC(D3));
940   ASSERT_EQ(1, std::distance(D2.begin(), D2.end()));
941 
942   // Add an edge to make the graph:
943   //
944   //         d1         |
945   //        /  \        |
946   //       d3--d2---.   |
947   //      /     \    |  |
948   //     b1     c1   |  |
949   //   /  \    /  \ /   |
950   //  b3--b2  c3--c2    |
951   //       \  /         |
952   //        a1          |
953   //       /  \         |
954   //      a3--a2        |
955   auto MergedRCs = CRC.insertIncomingRefEdge(D2, C2);
956   // Make sure we connected the nodes.
957   for (LazyCallGraph::Edge E : D2) {
958     if (E.getNode() == &D3)
959       continue;
960     EXPECT_EQ(&C2, E.getNode());
961   }
962   // And marked the D ref-SCC as no longer valid.
963   EXPECT_EQ(1u, MergedRCs.size());
964   EXPECT_EQ(&DRC, MergedRCs[0]);
965 
966   // Make sure we have the correct nodes in the SCC sets.
967   EXPECT_EQ(&ARC, CG.lookupRefSCC(A1));
968   EXPECT_EQ(&ARC, CG.lookupRefSCC(A2));
969   EXPECT_EQ(&ARC, CG.lookupRefSCC(A3));
970   EXPECT_EQ(&BRC, CG.lookupRefSCC(B1));
971   EXPECT_EQ(&BRC, CG.lookupRefSCC(B2));
972   EXPECT_EQ(&BRC, CG.lookupRefSCC(B3));
973   EXPECT_EQ(&CRC, CG.lookupRefSCC(C1));
974   EXPECT_EQ(&CRC, CG.lookupRefSCC(C2));
975   EXPECT_EQ(&CRC, CG.lookupRefSCC(C3));
976   EXPECT_EQ(&CRC, CG.lookupRefSCC(D1));
977   EXPECT_EQ(&CRC, CG.lookupRefSCC(D2));
978   EXPECT_EQ(&CRC, CG.lookupRefSCC(D3));
979 
980   // And that ancestry tests have been updated.
981   EXPECT_TRUE(ARC.isParentOf(CRC));
982   EXPECT_TRUE(BRC.isParentOf(CRC));
983 
984   // And verify the post-order walk reflects the updated structure.
985   auto I = CG.postorder_ref_scc_begin(), E = CG.postorder_ref_scc_end();
986   ASSERT_NE(I, E);
987   EXPECT_EQ(&CRC, &*I) << "Actual RefSCC: " << *I;
988   ASSERT_NE(++I, E);
989   EXPECT_EQ(&BRC, &*I) << "Actual RefSCC: " << *I;
990   ASSERT_NE(++I, E);
991   EXPECT_EQ(&ARC, &*I) << "Actual RefSCC: " << *I;
992   EXPECT_EQ(++I, E);
993 }
994 
995 TEST(LazyCallGraphTest, IncomingEdgeInsertionLargeCallCycle) {
996   LLVMContext Context;
997   std::unique_ptr<Module> M = parseAssembly(Context, "define void @a() {\n"
998                                                      "entry:\n"
999                                                      "  call void @b()\n"
1000                                                      "  ret void\n"
1001                                                      "}\n"
1002                                                      "define void @b() {\n"
1003                                                      "entry:\n"
1004                                                      "  call void @c()\n"
1005                                                      "  ret void\n"
1006                                                      "}\n"
1007                                                      "define void @c() {\n"
1008                                                      "entry:\n"
1009                                                      "  call void @d()\n"
1010                                                      "  ret void\n"
1011                                                      "}\n"
1012                                                      "define void @d() {\n"
1013                                                      "entry:\n"
1014                                                      "  ret void\n"
1015                                                      "}\n");
1016   LazyCallGraph CG(*M);
1017 
1018   // Force the graph to be fully expanded.
1019   for (LazyCallGraph::RefSCC &RC : CG.postorder_ref_sccs())
1020     dbgs() << "Formed RefSCC: " << RC << "\n";
1021 
1022   LazyCallGraph::Node &A = *CG.lookup(lookupFunction(*M, "a"));
1023   LazyCallGraph::Node &B = *CG.lookup(lookupFunction(*M, "b"));
1024   LazyCallGraph::Node &C = *CG.lookup(lookupFunction(*M, "c"));
1025   LazyCallGraph::Node &D = *CG.lookup(lookupFunction(*M, "d"));
1026   LazyCallGraph::SCC &AC = *CG.lookupSCC(A);
1027   LazyCallGraph::SCC &BC = *CG.lookupSCC(B);
1028   LazyCallGraph::SCC &CC = *CG.lookupSCC(C);
1029   LazyCallGraph::SCC &DC = *CG.lookupSCC(D);
1030   LazyCallGraph::RefSCC &ARC = *CG.lookupRefSCC(A);
1031   LazyCallGraph::RefSCC &BRC = *CG.lookupRefSCC(B);
1032   LazyCallGraph::RefSCC &CRC = *CG.lookupRefSCC(C);
1033   LazyCallGraph::RefSCC &DRC = *CG.lookupRefSCC(D);
1034 
1035   // Connect the top to the bottom forming a large RefSCC made up mostly of calls.
1036   auto MergedRCs = ARC.insertIncomingRefEdge(D, A);
1037   // Make sure we connected the nodes.
1038   EXPECT_NE(D.begin(), D.end());
1039   EXPECT_EQ(&A, D.begin()->getNode());
1040 
1041   // Check that we have the dead RCs, but ignore the order.
1042   EXPECT_EQ(3u, MergedRCs.size());
1043   EXPECT_NE(find(MergedRCs, &BRC), MergedRCs.end());
1044   EXPECT_NE(find(MergedRCs, &CRC), MergedRCs.end());
1045   EXPECT_NE(find(MergedRCs, &DRC), MergedRCs.end());
1046 
1047   // Make sure the nodes point to the right place now.
1048   EXPECT_EQ(&ARC, CG.lookupRefSCC(A));
1049   EXPECT_EQ(&ARC, CG.lookupRefSCC(B));
1050   EXPECT_EQ(&ARC, CG.lookupRefSCC(C));
1051   EXPECT_EQ(&ARC, CG.lookupRefSCC(D));
1052 
1053   // Check that the SCCs are in postorder.
1054   EXPECT_EQ(4, ARC.size());
1055   EXPECT_EQ(&DC, &ARC[0]);
1056   EXPECT_EQ(&CC, &ARC[1]);
1057   EXPECT_EQ(&BC, &ARC[2]);
1058   EXPECT_EQ(&AC, &ARC[3]);
1059 
1060   // And verify the post-order walk reflects the updated structure.
1061   auto I = CG.postorder_ref_scc_begin(), E = CG.postorder_ref_scc_end();
1062   ASSERT_NE(I, E);
1063   EXPECT_EQ(&ARC, &*I) << "Actual RefSCC: " << *I;
1064   EXPECT_EQ(++I, E);
1065 }
1066 
1067 TEST(LazyCallGraphTest, IncomingEdgeInsertionLargeRefCycle) {
1068   LLVMContext Context;
1069   std::unique_ptr<Module> M =
1070       parseAssembly(Context, "define void @a() {\n"
1071                              "entry:\n"
1072                              "  %p = alloca void ()*\n"
1073                              "  store void ()* @b, void ()** %p\n"
1074                              "  ret void\n"
1075                              "}\n"
1076                              "define void @b() {\n"
1077                              "entry:\n"
1078                              "  %p = alloca void ()*\n"
1079                              "  store void ()* @c, void ()** %p\n"
1080                              "  ret void\n"
1081                              "}\n"
1082                              "define void @c() {\n"
1083                              "entry:\n"
1084                              "  %p = alloca void ()*\n"
1085                              "  store void ()* @d, void ()** %p\n"
1086                              "  ret void\n"
1087                              "}\n"
1088                              "define void @d() {\n"
1089                              "entry:\n"
1090                              "  ret void\n"
1091                              "}\n");
1092   LazyCallGraph CG(*M);
1093 
1094   // Force the graph to be fully expanded.
1095   for (LazyCallGraph::RefSCC &RC : CG.postorder_ref_sccs())
1096     dbgs() << "Formed RefSCC: " << RC << "\n";
1097 
1098   LazyCallGraph::Node &A = *CG.lookup(lookupFunction(*M, "a"));
1099   LazyCallGraph::Node &B = *CG.lookup(lookupFunction(*M, "b"));
1100   LazyCallGraph::Node &C = *CG.lookup(lookupFunction(*M, "c"));
1101   LazyCallGraph::Node &D = *CG.lookup(lookupFunction(*M, "d"));
1102   LazyCallGraph::RefSCC &ARC = *CG.lookupRefSCC(A);
1103   LazyCallGraph::RefSCC &BRC = *CG.lookupRefSCC(B);
1104   LazyCallGraph::RefSCC &CRC = *CG.lookupRefSCC(C);
1105   LazyCallGraph::RefSCC &DRC = *CG.lookupRefSCC(D);
1106 
1107   // Connect the top to the bottom forming a large RefSCC made up just of
1108   // references.
1109   auto MergedRCs = ARC.insertIncomingRefEdge(D, A);
1110   // Make sure we connected the nodes.
1111   EXPECT_NE(D.begin(), D.end());
1112   EXPECT_EQ(&A, D.begin()->getNode());
1113 
1114   // Check that we have the dead RCs, but ignore the order.
1115   EXPECT_EQ(3u, MergedRCs.size());
1116   EXPECT_NE(find(MergedRCs, &BRC), MergedRCs.end());
1117   EXPECT_NE(find(MergedRCs, &CRC), MergedRCs.end());
1118   EXPECT_NE(find(MergedRCs, &DRC), MergedRCs.end());
1119 
1120   // Make sure the nodes point to the right place now.
1121   EXPECT_EQ(&ARC, CG.lookupRefSCC(A));
1122   EXPECT_EQ(&ARC, CG.lookupRefSCC(B));
1123   EXPECT_EQ(&ARC, CG.lookupRefSCC(C));
1124   EXPECT_EQ(&ARC, CG.lookupRefSCC(D));
1125 
1126   // And verify the post-order walk reflects the updated structure.
1127   auto I = CG.postorder_ref_scc_begin(), End = CG.postorder_ref_scc_end();
1128   ASSERT_NE(I, End);
1129   EXPECT_EQ(&ARC, &*I) << "Actual RefSCC: " << *I;
1130   EXPECT_EQ(++I, End);
1131 }
1132 
1133 TEST(LazyCallGraphTest, InlineAndDeleteFunction) {
1134   LLVMContext Context;
1135   // We want to ensure we can delete nodes from relatively complex graphs and
1136   // so use the diamond of triangles graph defined above.
1137   //
1138   // The ascii diagram is repeated here for easy reference.
1139   //
1140   //         d1       |
1141   //        /  \      |
1142   //       d3--d2     |
1143   //      /     \     |
1144   //     b1     c1    |
1145   //   /  \    /  \   |
1146   //  b3--b2  c3--c2  |
1147   //       \  /       |
1148   //        a1        |
1149   //       /  \       |
1150   //      a3--a2      |
1151   //
1152   std::unique_ptr<Module> M = parseAssembly(Context, DiamondOfTriangles);
1153   LazyCallGraph CG(*M);
1154 
1155   // Force the graph to be fully expanded.
1156   for (LazyCallGraph::RefSCC &RC : CG.postorder_ref_sccs())
1157     dbgs() << "Formed RefSCC: " << RC << "\n";
1158 
1159   LazyCallGraph::Node &A1 = *CG.lookup(lookupFunction(*M, "a1"));
1160   LazyCallGraph::Node &A2 = *CG.lookup(lookupFunction(*M, "a2"));
1161   LazyCallGraph::Node &A3 = *CG.lookup(lookupFunction(*M, "a3"));
1162   LazyCallGraph::Node &B1 = *CG.lookup(lookupFunction(*M, "b1"));
1163   LazyCallGraph::Node &B2 = *CG.lookup(lookupFunction(*M, "b2"));
1164   LazyCallGraph::Node &B3 = *CG.lookup(lookupFunction(*M, "b3"));
1165   LazyCallGraph::Node &C1 = *CG.lookup(lookupFunction(*M, "c1"));
1166   LazyCallGraph::Node &C2 = *CG.lookup(lookupFunction(*M, "c2"));
1167   LazyCallGraph::Node &C3 = *CG.lookup(lookupFunction(*M, "c3"));
1168   LazyCallGraph::Node &D1 = *CG.lookup(lookupFunction(*M, "d1"));
1169   LazyCallGraph::Node &D2 = *CG.lookup(lookupFunction(*M, "d2"));
1170   LazyCallGraph::Node &D3 = *CG.lookup(lookupFunction(*M, "d3"));
1171   LazyCallGraph::RefSCC &ARC = *CG.lookupRefSCC(A1);
1172   LazyCallGraph::RefSCC &BRC = *CG.lookupRefSCC(B1);
1173   LazyCallGraph::RefSCC &CRC = *CG.lookupRefSCC(C1);
1174   LazyCallGraph::RefSCC &DRC = *CG.lookupRefSCC(D1);
1175   ASSERT_EQ(&ARC, CG.lookupRefSCC(A2));
1176   ASSERT_EQ(&ARC, CG.lookupRefSCC(A3));
1177   ASSERT_EQ(&BRC, CG.lookupRefSCC(B2));
1178   ASSERT_EQ(&BRC, CG.lookupRefSCC(B3));
1179   ASSERT_EQ(&CRC, CG.lookupRefSCC(C2));
1180   ASSERT_EQ(&CRC, CG.lookupRefSCC(C3));
1181   ASSERT_EQ(&DRC, CG.lookupRefSCC(D2));
1182   ASSERT_EQ(&DRC, CG.lookupRefSCC(D3));
1183   ASSERT_EQ(1, std::distance(D2.begin(), D2.end()));
1184 
1185   // Delete d2 from the graph, as if it had been inlined.
1186   //
1187   //         d1         |
1188   //        / /         |
1189   //       d3--.        |
1190   //      /     \       |
1191   //     b1     c1      |
1192   //   /  \    /  \     |
1193   //  b3--b2  c3--c2    |
1194   //       \  /         |
1195   //        a1          |
1196   //       /  \         |
1197   //      a3--a2        |
1198 
1199   Function &D2F = D2.getFunction();
1200   CallInst *C1Call = nullptr, *D1Call = nullptr;
1201   for (User *U : D2F.users()) {
1202     CallInst *CI = dyn_cast<CallInst>(U);
1203     ASSERT_TRUE(CI) << "Expected a call: " << *U;
1204     if (CI->getParent()->getParent() == &C1.getFunction()) {
1205       ASSERT_EQ(nullptr, C1Call) << "Found too many C1 calls: " << *CI;
1206       C1Call = CI;
1207     } else if (CI->getParent()->getParent() == &D1.getFunction()) {
1208       ASSERT_EQ(nullptr, D1Call) << "Found too many D1 calls: " << *CI;
1209       D1Call = CI;
1210     } else {
1211       FAIL() << "Found an unexpected call instruction: " << *CI;
1212     }
1213   }
1214   ASSERT_NE(C1Call, nullptr);
1215   ASSERT_NE(D1Call, nullptr);
1216   ASSERT_EQ(&D2F, C1Call->getCalledFunction());
1217   ASSERT_EQ(&D2F, D1Call->getCalledFunction());
1218   C1Call->setCalledFunction(&D3.getFunction());
1219   D1Call->setCalledFunction(&D3.getFunction());
1220   ASSERT_EQ(0u, D2F.getNumUses());
1221 
1222   // Insert new edges first.
1223   CRC.insertTrivialCallEdge(C1, D3);
1224   DRC.insertTrivialCallEdge(D1, D3);
1225 
1226   // Then remove the old ones.
1227   LazyCallGraph::SCC &DC = *CG.lookupSCC(D2);
1228   auto NewCs = DRC.switchInternalEdgeToRef(D1, D2);
1229   EXPECT_EQ(&DC, CG.lookupSCC(D2));
1230   EXPECT_EQ(NewCs.end(), std::next(NewCs.begin()));
1231   LazyCallGraph::SCC &NewDC = *NewCs.begin();
1232   EXPECT_EQ(&NewDC, CG.lookupSCC(D1));
1233   EXPECT_EQ(&NewDC, CG.lookupSCC(D3));
1234   auto NewRCs = DRC.removeInternalRefEdge(D1, D2);
1235   EXPECT_EQ(&DRC, CG.lookupRefSCC(D2));
1236   EXPECT_EQ(NewRCs.end(), std::next(NewRCs.begin()));
1237   LazyCallGraph::RefSCC &NewDRC = **NewRCs.begin();
1238   EXPECT_EQ(&NewDRC, CG.lookupRefSCC(D1));
1239   EXPECT_EQ(&NewDRC, CG.lookupRefSCC(D3));
1240   EXPECT_FALSE(NewDRC.isParentOf(DRC));
1241   EXPECT_TRUE(CRC.isParentOf(DRC));
1242   EXPECT_TRUE(CRC.isParentOf(NewDRC));
1243   EXPECT_TRUE(DRC.isParentOf(NewDRC));
1244   CRC.removeOutgoingEdge(C1, D2);
1245   EXPECT_FALSE(CRC.isParentOf(DRC));
1246   EXPECT_TRUE(CRC.isParentOf(NewDRC));
1247   EXPECT_TRUE(DRC.isParentOf(NewDRC));
1248 
1249   // Now that we've updated the call graph, D2 is dead, so remove it.
1250   CG.removeDeadFunction(D2F);
1251 
1252   // Check that the graph still looks the same.
1253   EXPECT_EQ(&ARC, CG.lookupRefSCC(A1));
1254   EXPECT_EQ(&ARC, CG.lookupRefSCC(A2));
1255   EXPECT_EQ(&ARC, CG.lookupRefSCC(A3));
1256   EXPECT_EQ(&BRC, CG.lookupRefSCC(B1));
1257   EXPECT_EQ(&BRC, CG.lookupRefSCC(B2));
1258   EXPECT_EQ(&BRC, CG.lookupRefSCC(B3));
1259   EXPECT_EQ(&CRC, CG.lookupRefSCC(C1));
1260   EXPECT_EQ(&CRC, CG.lookupRefSCC(C2));
1261   EXPECT_EQ(&CRC, CG.lookupRefSCC(C3));
1262   EXPECT_EQ(&NewDRC, CG.lookupRefSCC(D1));
1263   EXPECT_EQ(&NewDRC, CG.lookupRefSCC(D3));
1264   EXPECT_TRUE(CRC.isParentOf(NewDRC));
1265 
1266   // Verify the post-order walk hasn't changed.
1267   auto I = CG.postorder_ref_scc_begin(), E = CG.postorder_ref_scc_end();
1268   ASSERT_NE(I, E);
1269   EXPECT_EQ(&NewDRC, &*I) << "Actual RefSCC: " << *I;
1270   ASSERT_NE(++I, E);
1271   EXPECT_EQ(&CRC, &*I) << "Actual RefSCC: " << *I;
1272   ASSERT_NE(++I, E);
1273   EXPECT_EQ(&BRC, &*I) << "Actual RefSCC: " << *I;
1274   ASSERT_NE(++I, E);
1275   EXPECT_EQ(&ARC, &*I) << "Actual RefSCC: " << *I;
1276   EXPECT_EQ(++I, E);
1277 }
1278 
1279 TEST(LazyCallGraphTest, InlineAndDeleteFunctionMidTraversal) {
1280   LLVMContext Context;
1281   // This is the same fundamental test as the previous, but we perform it
1282   // having only partially walked the RefSCCs of the graph.
1283   //
1284   // The ascii diagram is repeated here for easy reference.
1285   //
1286   //         d1       |
1287   //        /  \      |
1288   //       d3--d2     |
1289   //      /     \     |
1290   //     b1     c1    |
1291   //   /  \    /  \   |
1292   //  b3--b2  c3--c2  |
1293   //       \  /       |
1294   //        a1        |
1295   //       /  \       |
1296   //      a3--a2      |
1297   //
1298   std::unique_ptr<Module> M = parseAssembly(Context, DiamondOfTriangles);
1299   LazyCallGraph CG(*M);
1300 
1301   // Walk the RefSCCs until we find the one containing 'c1'.
1302   auto I = CG.postorder_ref_scc_begin(), E = CG.postorder_ref_scc_end();
1303   ASSERT_NE(I, E);
1304   LazyCallGraph::RefSCC &DRC = *I;
1305   ASSERT_NE(&DRC, nullptr);
1306   ++I;
1307   ASSERT_NE(I, E);
1308   LazyCallGraph::RefSCC &CRC = *I;
1309   ASSERT_NE(&CRC, nullptr);
1310 
1311   ASSERT_EQ(nullptr, CG.lookup(lookupFunction(*M, "a1")));
1312   ASSERT_EQ(nullptr, CG.lookup(lookupFunction(*M, "a2")));
1313   ASSERT_EQ(nullptr, CG.lookup(lookupFunction(*M, "a3")));
1314   ASSERT_EQ(nullptr, CG.lookup(lookupFunction(*M, "b1")));
1315   ASSERT_EQ(nullptr, CG.lookup(lookupFunction(*M, "b2")));
1316   ASSERT_EQ(nullptr, CG.lookup(lookupFunction(*M, "b3")));
1317   LazyCallGraph::Node &C1 = *CG.lookup(lookupFunction(*M, "c1"));
1318   LazyCallGraph::Node &C2 = *CG.lookup(lookupFunction(*M, "c2"));
1319   LazyCallGraph::Node &C3 = *CG.lookup(lookupFunction(*M, "c3"));
1320   LazyCallGraph::Node &D1 = *CG.lookup(lookupFunction(*M, "d1"));
1321   LazyCallGraph::Node &D2 = *CG.lookup(lookupFunction(*M, "d2"));
1322   LazyCallGraph::Node &D3 = *CG.lookup(lookupFunction(*M, "d3"));
1323   ASSERT_EQ(&CRC, CG.lookupRefSCC(C1));
1324   ASSERT_EQ(&CRC, CG.lookupRefSCC(C2));
1325   ASSERT_EQ(&CRC, CG.lookupRefSCC(C3));
1326   ASSERT_EQ(&DRC, CG.lookupRefSCC(D1));
1327   ASSERT_EQ(&DRC, CG.lookupRefSCC(D2));
1328   ASSERT_EQ(&DRC, CG.lookupRefSCC(D3));
1329   ASSERT_EQ(1, std::distance(D2.begin(), D2.end()));
1330 
1331   // Delete d2 from the graph, as if it had been inlined.
1332   //
1333   //         d1         |
1334   //        / /         |
1335   //       d3--.        |
1336   //      /     \       |
1337   //     b1     c1      |
1338   //   /  \    /  \     |
1339   //  b3--b2  c3--c2    |
1340   //       \  /         |
1341   //        a1          |
1342   //       /  \         |
1343   //      a3--a2        |
1344 
1345   Function &D2F = D2.getFunction();
1346   CallInst *C1Call = nullptr, *D1Call = nullptr;
1347   for (User *U : D2F.users()) {
1348     CallInst *CI = dyn_cast<CallInst>(U);
1349     ASSERT_TRUE(CI) << "Expected a call: " << *U;
1350     if (CI->getParent()->getParent() == &C1.getFunction()) {
1351       ASSERT_EQ(nullptr, C1Call) << "Found too many C1 calls: " << *CI;
1352       C1Call = CI;
1353     } else if (CI->getParent()->getParent() == &D1.getFunction()) {
1354       ASSERT_EQ(nullptr, D1Call) << "Found too many D1 calls: " << *CI;
1355       D1Call = CI;
1356     } else {
1357       FAIL() << "Found an unexpected call instruction: " << *CI;
1358     }
1359   }
1360   ASSERT_NE(C1Call, nullptr);
1361   ASSERT_NE(D1Call, nullptr);
1362   ASSERT_EQ(&D2F, C1Call->getCalledFunction());
1363   ASSERT_EQ(&D2F, D1Call->getCalledFunction());
1364   C1Call->setCalledFunction(&D3.getFunction());
1365   D1Call->setCalledFunction(&D3.getFunction());
1366   ASSERT_EQ(0u, D2F.getNumUses());
1367 
1368   // Insert new edges first.
1369   CRC.insertTrivialCallEdge(C1, D3);
1370   DRC.insertTrivialCallEdge(D1, D3);
1371 
1372   // Then remove the old ones.
1373   LazyCallGraph::SCC &DC = *CG.lookupSCC(D2);
1374   auto NewCs = DRC.switchInternalEdgeToRef(D1, D2);
1375   EXPECT_EQ(&DC, CG.lookupSCC(D2));
1376   EXPECT_EQ(NewCs.end(), std::next(NewCs.begin()));
1377   LazyCallGraph::SCC &NewDC = *NewCs.begin();
1378   EXPECT_EQ(&NewDC, CG.lookupSCC(D1));
1379   EXPECT_EQ(&NewDC, CG.lookupSCC(D3));
1380   auto NewRCs = DRC.removeInternalRefEdge(D1, D2);
1381   EXPECT_EQ(&DRC, CG.lookupRefSCC(D2));
1382   EXPECT_EQ(NewRCs.end(), std::next(NewRCs.begin()));
1383   LazyCallGraph::RefSCC &NewDRC = **NewRCs.begin();
1384   EXPECT_EQ(&NewDRC, CG.lookupRefSCC(D1));
1385   EXPECT_EQ(&NewDRC, CG.lookupRefSCC(D3));
1386   EXPECT_FALSE(NewDRC.isParentOf(DRC));
1387   EXPECT_TRUE(CRC.isParentOf(DRC));
1388   EXPECT_TRUE(CRC.isParentOf(NewDRC));
1389   EXPECT_TRUE(DRC.isParentOf(NewDRC));
1390   CRC.removeOutgoingEdge(C1, D2);
1391   EXPECT_FALSE(CRC.isParentOf(DRC));
1392   EXPECT_TRUE(CRC.isParentOf(NewDRC));
1393   EXPECT_TRUE(DRC.isParentOf(NewDRC));
1394 
1395   // Now that we've updated the call graph, D2 is dead, so remove it.
1396   CG.removeDeadFunction(D2F);
1397 
1398   // Check that the graph still looks the same.
1399   EXPECT_EQ(&CRC, CG.lookupRefSCC(C1));
1400   EXPECT_EQ(&CRC, CG.lookupRefSCC(C2));
1401   EXPECT_EQ(&CRC, CG.lookupRefSCC(C3));
1402   EXPECT_EQ(&NewDRC, CG.lookupRefSCC(D1));
1403   EXPECT_EQ(&NewDRC, CG.lookupRefSCC(D3));
1404   EXPECT_TRUE(CRC.isParentOf(NewDRC));
1405 
1406   // Verify that the post-order walk reflects the updated but still incomplete
1407   // structure.
1408   auto J = CG.postorder_ref_scc_begin();
1409   EXPECT_NE(J, E);
1410   EXPECT_EQ(&NewDRC, &*J) << "Actual RefSCC: " << *J;
1411   ++J;
1412   EXPECT_NE(J, E);
1413   EXPECT_EQ(&CRC, &*J) << "Actual RefSCC: " << *J;
1414   EXPECT_EQ(I, J);
1415 
1416   // Check that we can form the last two RefSCCs now, and even that we can do
1417   // it with alternating iterators.
1418   ++J;
1419   EXPECT_NE(J, E);
1420   LazyCallGraph::RefSCC &BRC = *J;
1421   EXPECT_NE(&BRC, nullptr);
1422   EXPECT_EQ(&BRC, CG.lookupRefSCC(*CG.lookup(lookupFunction(*M, "b1"))));
1423   EXPECT_EQ(&BRC, CG.lookupRefSCC(*CG.lookup(lookupFunction(*M, "b2"))));
1424   EXPECT_EQ(&BRC, CG.lookupRefSCC(*CG.lookup(lookupFunction(*M, "b3"))));
1425   EXPECT_TRUE(BRC.isParentOf(NewDRC));
1426   ++I;
1427   EXPECT_EQ(J, I);
1428   EXPECT_EQ(&BRC, &*I) << "Actual RefSCC: " << *I;
1429 
1430   // Increment I this time to form the new RefSCC, flopping back to the first
1431   // iterator.
1432   ++I;
1433   EXPECT_NE(I, E);
1434   LazyCallGraph::RefSCC &ARC = *I;
1435   EXPECT_NE(&ARC, nullptr);
1436   EXPECT_EQ(&ARC, CG.lookupRefSCC(*CG.lookup(lookupFunction(*M, "a1"))));
1437   EXPECT_EQ(&ARC, CG.lookupRefSCC(*CG.lookup(lookupFunction(*M, "a2"))));
1438   EXPECT_EQ(&ARC, CG.lookupRefSCC(*CG.lookup(lookupFunction(*M, "a3"))));
1439   EXPECT_TRUE(ARC.isParentOf(BRC));
1440   EXPECT_TRUE(ARC.isParentOf(CRC));
1441   ++J;
1442   EXPECT_EQ(I, J);
1443   EXPECT_EQ(&ARC, &*J) << "Actual RefSCC: " << *J;
1444   ++I;
1445   EXPECT_EQ(E, I);
1446   ++J;
1447   EXPECT_EQ(E, J);
1448 }
1449 
1450 TEST(LazyCallGraphTest, InternalEdgeMutation) {
1451   LLVMContext Context;
1452   std::unique_ptr<Module> M = parseAssembly(Context, "define void @a() {\n"
1453                                                      "entry:\n"
1454                                                      "  call void @b()\n"
1455                                                      "  ret void\n"
1456                                                      "}\n"
1457                                                      "define void @b() {\n"
1458                                                      "entry:\n"
1459                                                      "  call void @c()\n"
1460                                                      "  ret void\n"
1461                                                      "}\n"
1462                                                      "define void @c() {\n"
1463                                                      "entry:\n"
1464                                                      "  call void @a()\n"
1465                                                      "  ret void\n"
1466                                                      "}\n");
1467   LazyCallGraph CG(*M);
1468 
1469   // Force the graph to be fully expanded.
1470   auto I = CG.postorder_ref_scc_begin();
1471   LazyCallGraph::RefSCC &RC = *I++;
1472   EXPECT_EQ(CG.postorder_ref_scc_end(), I);
1473 
1474   LazyCallGraph::Node &A = *CG.lookup(lookupFunction(*M, "a"));
1475   LazyCallGraph::Node &B = *CG.lookup(lookupFunction(*M, "b"));
1476   LazyCallGraph::Node &C = *CG.lookup(lookupFunction(*M, "c"));
1477   EXPECT_EQ(&RC, CG.lookupRefSCC(A));
1478   EXPECT_EQ(&RC, CG.lookupRefSCC(B));
1479   EXPECT_EQ(&RC, CG.lookupRefSCC(C));
1480   EXPECT_EQ(1, RC.size());
1481   EXPECT_EQ(&*RC.begin(), CG.lookupSCC(A));
1482   EXPECT_EQ(&*RC.begin(), CG.lookupSCC(B));
1483   EXPECT_EQ(&*RC.begin(), CG.lookupSCC(C));
1484 
1485   // Insert an edge from 'a' to 'c'. Nothing changes about the graph.
1486   RC.insertInternalRefEdge(A, C);
1487   EXPECT_EQ(2, std::distance(A.begin(), A.end()));
1488   EXPECT_EQ(&RC, CG.lookupRefSCC(A));
1489   EXPECT_EQ(&RC, CG.lookupRefSCC(B));
1490   EXPECT_EQ(&RC, CG.lookupRefSCC(C));
1491   EXPECT_EQ(1, RC.size());
1492   EXPECT_EQ(&*RC.begin(), CG.lookupSCC(A));
1493   EXPECT_EQ(&*RC.begin(), CG.lookupSCC(B));
1494   EXPECT_EQ(&*RC.begin(), CG.lookupSCC(C));
1495 
1496   // Switch the call edge from 'b' to 'c' to a ref edge. This will break the
1497   // call cycle and cause us to form more SCCs. The RefSCC will remain the same
1498   // though.
1499   RC.switchInternalEdgeToRef(B, C);
1500   EXPECT_EQ(&RC, CG.lookupRefSCC(A));
1501   EXPECT_EQ(&RC, CG.lookupRefSCC(B));
1502   EXPECT_EQ(&RC, CG.lookupRefSCC(C));
1503   auto J = RC.begin();
1504   // The SCCs must be in *post-order* which means successors before
1505   // predecessors. At this point we have call edges from C to A and from A to
1506   // B. The only valid postorder is B, A, C.
1507   EXPECT_EQ(&*J++, CG.lookupSCC(B));
1508   EXPECT_EQ(&*J++, CG.lookupSCC(A));
1509   EXPECT_EQ(&*J++, CG.lookupSCC(C));
1510   EXPECT_EQ(RC.end(), J);
1511 
1512   // Test turning the ref edge from A to C into a call edge. This will form an
1513   // SCC out of A and C. Since we previously had a call edge from C to A, the
1514   // C SCC should be preserved and have A merged into it while the A SCC should
1515   // be invalidated.
1516   LazyCallGraph::SCC &AC = *CG.lookupSCC(A);
1517   LazyCallGraph::SCC &CC = *CG.lookupSCC(C);
1518   auto InvalidatedSCCs = RC.switchInternalEdgeToCall(A, C);
1519   ASSERT_EQ(1u, InvalidatedSCCs.size());
1520   EXPECT_EQ(&AC, InvalidatedSCCs[0]);
1521   EXPECT_EQ(2, CC.size());
1522   EXPECT_EQ(&CC, CG.lookupSCC(A));
1523   EXPECT_EQ(&CC, CG.lookupSCC(C));
1524   J = RC.begin();
1525   EXPECT_EQ(&*J++, CG.lookupSCC(B));
1526   EXPECT_EQ(&*J++, CG.lookupSCC(C));
1527   EXPECT_EQ(RC.end(), J);
1528 }
1529 
1530 TEST(LazyCallGraphTest, InternalEdgeRemoval) {
1531   LLVMContext Context;
1532   // A nice fully connected (including self-edges) RefSCC.
1533   std::unique_ptr<Module> M = parseAssembly(
1534       Context, "define void @a(i8** %ptr) {\n"
1535                "entry:\n"
1536                "  store i8* bitcast (void(i8**)* @a to i8*), i8** %ptr\n"
1537                "  store i8* bitcast (void(i8**)* @b to i8*), i8** %ptr\n"
1538                "  store i8* bitcast (void(i8**)* @c to i8*), i8** %ptr\n"
1539                "  ret void\n"
1540                "}\n"
1541                "define void @b(i8** %ptr) {\n"
1542                "entry:\n"
1543                "  store i8* bitcast (void(i8**)* @a to i8*), i8** %ptr\n"
1544                "  store i8* bitcast (void(i8**)* @b to i8*), i8** %ptr\n"
1545                "  store i8* bitcast (void(i8**)* @c to i8*), i8** %ptr\n"
1546                "  ret void\n"
1547                "}\n"
1548                "define void @c(i8** %ptr) {\n"
1549                "entry:\n"
1550                "  store i8* bitcast (void(i8**)* @a to i8*), i8** %ptr\n"
1551                "  store i8* bitcast (void(i8**)* @b to i8*), i8** %ptr\n"
1552                "  store i8* bitcast (void(i8**)* @c to i8*), i8** %ptr\n"
1553                "  ret void\n"
1554                "}\n");
1555   LazyCallGraph CG(*M);
1556 
1557   // Force the graph to be fully expanded.
1558   auto I = CG.postorder_ref_scc_begin(), E = CG.postorder_ref_scc_end();
1559   LazyCallGraph::RefSCC &RC = *I;
1560   EXPECT_EQ(E, std::next(I));
1561 
1562   LazyCallGraph::Node &A = *CG.lookup(lookupFunction(*M, "a"));
1563   LazyCallGraph::Node &B = *CG.lookup(lookupFunction(*M, "b"));
1564   LazyCallGraph::Node &C = *CG.lookup(lookupFunction(*M, "c"));
1565   EXPECT_EQ(&RC, CG.lookupRefSCC(A));
1566   EXPECT_EQ(&RC, CG.lookupRefSCC(B));
1567   EXPECT_EQ(&RC, CG.lookupRefSCC(C));
1568 
1569   // Remove the edge from b -> a, which should leave the 3 functions still in
1570   // a single connected component because of a -> b -> c -> a.
1571   SmallVector<LazyCallGraph::RefSCC *, 1> NewRCs =
1572       RC.removeInternalRefEdge(B, A);
1573   EXPECT_EQ(0u, NewRCs.size());
1574   EXPECT_EQ(&RC, CG.lookupRefSCC(A));
1575   EXPECT_EQ(&RC, CG.lookupRefSCC(B));
1576   EXPECT_EQ(&RC, CG.lookupRefSCC(C));
1577   auto J = CG.postorder_ref_scc_begin();
1578   EXPECT_EQ(I, J);
1579   EXPECT_EQ(&RC, &*J);
1580   EXPECT_EQ(E, std::next(J));
1581 
1582   // Remove the edge from c -> a, which should leave 'a' in the original RefSCC
1583   // and form a new RefSCC for 'b' and 'c'.
1584   NewRCs = RC.removeInternalRefEdge(C, A);
1585   EXPECT_EQ(1u, NewRCs.size());
1586   EXPECT_EQ(&RC, CG.lookupRefSCC(A));
1587   EXPECT_EQ(1, std::distance(RC.begin(), RC.end()));
1588   LazyCallGraph::RefSCC &RC2 = *CG.lookupRefSCC(B);
1589   EXPECT_EQ(&RC2, CG.lookupRefSCC(C));
1590   EXPECT_EQ(&RC2, NewRCs[0]);
1591   J = CG.postorder_ref_scc_begin();
1592   EXPECT_NE(I, J);
1593   EXPECT_EQ(&RC2, &*J);
1594   ++J;
1595   EXPECT_EQ(I, J);
1596   EXPECT_EQ(&RC, &*J);
1597   ++I;
1598   EXPECT_EQ(E, I);
1599   ++J;
1600   EXPECT_EQ(E, J);
1601 }
1602 
1603 TEST(LazyCallGraphTest, InternalNoOpEdgeRemoval) {
1604   LLVMContext Context;
1605   // A graph with a single cycle formed both from call and reference edges
1606   // which makes the reference edges trivial to delete. The graph looks like:
1607   //
1608   // Reference edges: a -> b -> c -> a
1609   //      Call edges: a -> c -> b -> a
1610   std::unique_ptr<Module> M = parseAssembly(
1611       Context, "define void @a(i8** %ptr) {\n"
1612                "entry:\n"
1613                "  call void @b(i8** %ptr)\n"
1614                "  store i8* bitcast (void(i8**)* @c to i8*), i8** %ptr\n"
1615                "  ret void\n"
1616                "}\n"
1617                "define void @b(i8** %ptr) {\n"
1618                "entry:\n"
1619                "  store i8* bitcast (void(i8**)* @a to i8*), i8** %ptr\n"
1620                "  call void @c(i8** %ptr)\n"
1621                "  ret void\n"
1622                "}\n"
1623                "define void @c(i8** %ptr) {\n"
1624                "entry:\n"
1625                "  call void @a(i8** %ptr)\n"
1626                "  store i8* bitcast (void(i8**)* @b to i8*), i8** %ptr\n"
1627                "  ret void\n"
1628                "}\n");
1629   LazyCallGraph CG(*M);
1630 
1631   // Force the graph to be fully expanded.
1632   auto I = CG.postorder_ref_scc_begin(), E = CG.postorder_ref_scc_end();
1633   LazyCallGraph::RefSCC &RC = *I;
1634   EXPECT_EQ(E, std::next(I));
1635 
1636   LazyCallGraph::SCC &C = *RC.begin();
1637   EXPECT_EQ(RC.end(), std::next(RC.begin()));
1638 
1639   LazyCallGraph::Node &AN = *CG.lookup(lookupFunction(*M, "a"));
1640   LazyCallGraph::Node &BN = *CG.lookup(lookupFunction(*M, "b"));
1641   LazyCallGraph::Node &CN = *CG.lookup(lookupFunction(*M, "c"));
1642   EXPECT_EQ(&RC, CG.lookupRefSCC(AN));
1643   EXPECT_EQ(&RC, CG.lookupRefSCC(BN));
1644   EXPECT_EQ(&RC, CG.lookupRefSCC(CN));
1645   EXPECT_EQ(&C, CG.lookupSCC(AN));
1646   EXPECT_EQ(&C, CG.lookupSCC(BN));
1647   EXPECT_EQ(&C, CG.lookupSCC(CN));
1648 
1649   // Remove the edge from a -> c which doesn't change anything.
1650   SmallVector<LazyCallGraph::RefSCC *, 1> NewRCs =
1651       RC.removeInternalRefEdge(AN, CN);
1652   EXPECT_EQ(0u, NewRCs.size());
1653   EXPECT_EQ(&RC, CG.lookupRefSCC(AN));
1654   EXPECT_EQ(&RC, CG.lookupRefSCC(BN));
1655   EXPECT_EQ(&RC, CG.lookupRefSCC(CN));
1656   EXPECT_EQ(&C, CG.lookupSCC(AN));
1657   EXPECT_EQ(&C, CG.lookupSCC(BN));
1658   EXPECT_EQ(&C, CG.lookupSCC(CN));
1659   auto J = CG.postorder_ref_scc_begin();
1660   EXPECT_EQ(I, J);
1661   EXPECT_EQ(&RC, &*J);
1662   EXPECT_EQ(E, std::next(J));
1663 
1664   // Remove the edge from b -> a and c -> b; again this doesn't change
1665   // anything.
1666   NewRCs = RC.removeInternalRefEdge(BN, AN);
1667   NewRCs = RC.removeInternalRefEdge(CN, BN);
1668   EXPECT_EQ(0u, NewRCs.size());
1669   EXPECT_EQ(&RC, CG.lookupRefSCC(AN));
1670   EXPECT_EQ(&RC, CG.lookupRefSCC(BN));
1671   EXPECT_EQ(&RC, CG.lookupRefSCC(CN));
1672   EXPECT_EQ(&C, CG.lookupSCC(AN));
1673   EXPECT_EQ(&C, CG.lookupSCC(BN));
1674   EXPECT_EQ(&C, CG.lookupSCC(CN));
1675   J = CG.postorder_ref_scc_begin();
1676   EXPECT_EQ(I, J);
1677   EXPECT_EQ(&RC, &*J);
1678   EXPECT_EQ(E, std::next(J));
1679 }
1680 
1681 TEST(LazyCallGraphTest, InternalCallEdgeToRef) {
1682   LLVMContext Context;
1683   // A nice fully connected (including self-edges) SCC (and RefSCC)
1684   std::unique_ptr<Module> M = parseAssembly(Context, "define void @a() {\n"
1685                                                      "entry:\n"
1686                                                      "  call void @a()\n"
1687                                                      "  call void @b()\n"
1688                                                      "  call void @c()\n"
1689                                                      "  ret void\n"
1690                                                      "}\n"
1691                                                      "define void @b() {\n"
1692                                                      "entry:\n"
1693                                                      "  call void @a()\n"
1694                                                      "  call void @b()\n"
1695                                                      "  call void @c()\n"
1696                                                      "  ret void\n"
1697                                                      "}\n"
1698                                                      "define void @c() {\n"
1699                                                      "entry:\n"
1700                                                      "  call void @a()\n"
1701                                                      "  call void @b()\n"
1702                                                      "  call void @c()\n"
1703                                                      "  ret void\n"
1704                                                      "}\n");
1705   LazyCallGraph CG(*M);
1706 
1707   // Force the graph to be fully expanded.
1708   auto I = CG.postorder_ref_scc_begin();
1709   LazyCallGraph::RefSCC &RC = *I++;
1710   EXPECT_EQ(CG.postorder_ref_scc_end(), I);
1711 
1712   EXPECT_EQ(1, RC.size());
1713   LazyCallGraph::SCC &CallC = *RC.begin();
1714 
1715   LazyCallGraph::Node &A = *CG.lookup(lookupFunction(*M, "a"));
1716   LazyCallGraph::Node &B = *CG.lookup(lookupFunction(*M, "b"));
1717   LazyCallGraph::Node &C = *CG.lookup(lookupFunction(*M, "c"));
1718   EXPECT_EQ(&CallC, CG.lookupSCC(A));
1719   EXPECT_EQ(&CallC, CG.lookupSCC(B));
1720   EXPECT_EQ(&CallC, CG.lookupSCC(C));
1721 
1722   // Remove the call edge from b -> a to a ref edge, which should leave the
1723   // 3 functions still in a single connected component because of a -> b ->
1724   // c -> a.
1725   RC.switchInternalEdgeToRef(B, A);
1726   EXPECT_EQ(1, RC.size());
1727   EXPECT_EQ(&CallC, CG.lookupSCC(A));
1728   EXPECT_EQ(&CallC, CG.lookupSCC(B));
1729   EXPECT_EQ(&CallC, CG.lookupSCC(C));
1730 
1731   // Remove the edge from c -> a, which should leave 'a' in the original SCC
1732   // and form a new SCC for 'b' and 'c'.
1733   RC.switchInternalEdgeToRef(C, A);
1734   EXPECT_EQ(2, RC.size());
1735   EXPECT_EQ(&CallC, CG.lookupSCC(A));
1736   LazyCallGraph::SCC &BCallC = *CG.lookupSCC(B);
1737   EXPECT_NE(&BCallC, &CallC);
1738   EXPECT_EQ(&BCallC, CG.lookupSCC(C));
1739   auto J = RC.find(CallC);
1740   EXPECT_EQ(&CallC, &*J);
1741   --J;
1742   EXPECT_EQ(&BCallC, &*J);
1743   EXPECT_EQ(RC.begin(), J);
1744 
1745   // Remove the edge from c -> b, which should leave 'b' in the original SCC
1746   // and form a new SCC for 'c'. It shouldn't change 'a's SCC.
1747   RC.switchInternalEdgeToRef(C, B);
1748   EXPECT_EQ(3, RC.size());
1749   EXPECT_EQ(&CallC, CG.lookupSCC(A));
1750   EXPECT_EQ(&BCallC, CG.lookupSCC(B));
1751   LazyCallGraph::SCC &CCallC = *CG.lookupSCC(C);
1752   EXPECT_NE(&CCallC, &CallC);
1753   EXPECT_NE(&CCallC, &BCallC);
1754   J = RC.find(CallC);
1755   EXPECT_EQ(&CallC, &*J);
1756   --J;
1757   EXPECT_EQ(&BCallC, &*J);
1758   --J;
1759   EXPECT_EQ(&CCallC, &*J);
1760   EXPECT_EQ(RC.begin(), J);
1761 }
1762 
1763 TEST(LazyCallGraphTest, InternalRefEdgeToCall) {
1764   LLVMContext Context;
1765   // Basic tests for making a ref edge a call. This hits the basics of the
1766   // process only.
1767   std::unique_ptr<Module> M =
1768       parseAssembly(Context, "define void @a() {\n"
1769                              "entry:\n"
1770                              "  call void @b()\n"
1771                              "  call void @c()\n"
1772                              "  store void()* @d, void()** undef\n"
1773                              "  ret void\n"
1774                              "}\n"
1775                              "define void @b() {\n"
1776                              "entry:\n"
1777                              "  store void()* @c, void()** undef\n"
1778                              "  call void @d()\n"
1779                              "  ret void\n"
1780                              "}\n"
1781                              "define void @c() {\n"
1782                              "entry:\n"
1783                              "  store void()* @b, void()** undef\n"
1784                              "  call void @d()\n"
1785                              "  ret void\n"
1786                              "}\n"
1787                              "define void @d() {\n"
1788                              "entry:\n"
1789                              "  store void()* @a, void()** undef\n"
1790                              "  ret void\n"
1791                              "}\n");
1792   LazyCallGraph CG(*M);
1793 
1794   // Force the graph to be fully expanded.
1795   auto I = CG.postorder_ref_scc_begin();
1796   LazyCallGraph::RefSCC &RC = *I++;
1797   EXPECT_EQ(CG.postorder_ref_scc_end(), I);
1798 
1799   LazyCallGraph::Node &A = *CG.lookup(lookupFunction(*M, "a"));
1800   LazyCallGraph::Node &B = *CG.lookup(lookupFunction(*M, "b"));
1801   LazyCallGraph::Node &C = *CG.lookup(lookupFunction(*M, "c"));
1802   LazyCallGraph::Node &D = *CG.lookup(lookupFunction(*M, "d"));
1803   LazyCallGraph::SCC &AC = *CG.lookupSCC(A);
1804   LazyCallGraph::SCC &BC = *CG.lookupSCC(B);
1805   LazyCallGraph::SCC &CC = *CG.lookupSCC(C);
1806   LazyCallGraph::SCC &DC = *CG.lookupSCC(D);
1807 
1808   // Check the initial post-order. Note that B and C could be flipped here (and
1809   // in our mutation) without changing the nature of this test.
1810   ASSERT_EQ(4, RC.size());
1811   EXPECT_EQ(&DC, &RC[0]);
1812   EXPECT_EQ(&BC, &RC[1]);
1813   EXPECT_EQ(&CC, &RC[2]);
1814   EXPECT_EQ(&AC, &RC[3]);
1815 
1816   // Switch the ref edge from A -> D to a call edge. This should have no
1817   // effect as it is already in postorder and no new cycles are formed.
1818   auto MergedCs = RC.switchInternalEdgeToCall(A, D);
1819   EXPECT_EQ(0u, MergedCs.size());
1820   ASSERT_EQ(4, RC.size());
1821   EXPECT_EQ(&DC, &RC[0]);
1822   EXPECT_EQ(&BC, &RC[1]);
1823   EXPECT_EQ(&CC, &RC[2]);
1824   EXPECT_EQ(&AC, &RC[3]);
1825 
1826   // Switch B -> C to a call edge. This doesn't form any new cycles but does
1827   // require reordering the SCCs.
1828   MergedCs = RC.switchInternalEdgeToCall(B, C);
1829   EXPECT_EQ(0u, MergedCs.size());
1830   ASSERT_EQ(4, RC.size());
1831   EXPECT_EQ(&DC, &RC[0]);
1832   EXPECT_EQ(&CC, &RC[1]);
1833   EXPECT_EQ(&BC, &RC[2]);
1834   EXPECT_EQ(&AC, &RC[3]);
1835 
1836   // Switch C -> B to a call edge. This forms a cycle and forces merging SCCs.
1837   MergedCs = RC.switchInternalEdgeToCall(C, B);
1838   ASSERT_EQ(1u, MergedCs.size());
1839   EXPECT_EQ(&CC, MergedCs[0]);
1840   ASSERT_EQ(3, RC.size());
1841   EXPECT_EQ(&DC, &RC[0]);
1842   EXPECT_EQ(&BC, &RC[1]);
1843   EXPECT_EQ(&AC, &RC[2]);
1844   EXPECT_EQ(2, BC.size());
1845   EXPECT_EQ(&BC, CG.lookupSCC(B));
1846   EXPECT_EQ(&BC, CG.lookupSCC(C));
1847 }
1848 
1849 TEST(LazyCallGraphTest, InternalRefEdgeToCallNoCycleInterleaved) {
1850   LLVMContext Context;
1851   // Test for having a post-order prior to changing a ref edge to a call edge
1852   // with SCCs connecting to the source and connecting to the target, but not
1853   // connecting to both, interleaved between the source and target. This
1854   // ensures we correctly partition the range rather than simply moving one or
1855   // the other.
1856   std::unique_ptr<Module> M =
1857       parseAssembly(Context, "define void @a() {\n"
1858                              "entry:\n"
1859                              "  call void @b1()\n"
1860                              "  call void @c1()\n"
1861                              "  ret void\n"
1862                              "}\n"
1863                              "define void @b1() {\n"
1864                              "entry:\n"
1865                              "  call void @c1()\n"
1866                              "  call void @b2()\n"
1867                              "  ret void\n"
1868                              "}\n"
1869                              "define void @c1() {\n"
1870                              "entry:\n"
1871                              "  call void @b2()\n"
1872                              "  call void @c2()\n"
1873                              "  ret void\n"
1874                              "}\n"
1875                              "define void @b2() {\n"
1876                              "entry:\n"
1877                              "  call void @c2()\n"
1878                              "  call void @b3()\n"
1879                              "  ret void\n"
1880                              "}\n"
1881                              "define void @c2() {\n"
1882                              "entry:\n"
1883                              "  call void @b3()\n"
1884                              "  call void @c3()\n"
1885                              "  ret void\n"
1886                              "}\n"
1887                              "define void @b3() {\n"
1888                              "entry:\n"
1889                              "  call void @c3()\n"
1890                              "  call void @d()\n"
1891                              "  ret void\n"
1892                              "}\n"
1893                              "define void @c3() {\n"
1894                              "entry:\n"
1895                              "  store void()* @b1, void()** undef\n"
1896                              "  call void @d()\n"
1897                              "  ret void\n"
1898                              "}\n"
1899                              "define void @d() {\n"
1900                              "entry:\n"
1901                              "  store void()* @a, void()** undef\n"
1902                              "  ret void\n"
1903                              "}\n");
1904   LazyCallGraph CG(*M);
1905 
1906   // Force the graph to be fully expanded.
1907   auto I = CG.postorder_ref_scc_begin();
1908   LazyCallGraph::RefSCC &RC = *I++;
1909   EXPECT_EQ(CG.postorder_ref_scc_end(), I);
1910 
1911   LazyCallGraph::Node &A = *CG.lookup(lookupFunction(*M, "a"));
1912   LazyCallGraph::Node &B1 = *CG.lookup(lookupFunction(*M, "b1"));
1913   LazyCallGraph::Node &B2 = *CG.lookup(lookupFunction(*M, "b2"));
1914   LazyCallGraph::Node &B3 = *CG.lookup(lookupFunction(*M, "b3"));
1915   LazyCallGraph::Node &C1 = *CG.lookup(lookupFunction(*M, "c1"));
1916   LazyCallGraph::Node &C2 = *CG.lookup(lookupFunction(*M, "c2"));
1917   LazyCallGraph::Node &C3 = *CG.lookup(lookupFunction(*M, "c3"));
1918   LazyCallGraph::Node &D = *CG.lookup(lookupFunction(*M, "d"));
1919   LazyCallGraph::SCC &AC = *CG.lookupSCC(A);
1920   LazyCallGraph::SCC &B1C = *CG.lookupSCC(B1);
1921   LazyCallGraph::SCC &B2C = *CG.lookupSCC(B2);
1922   LazyCallGraph::SCC &B3C = *CG.lookupSCC(B3);
1923   LazyCallGraph::SCC &C1C = *CG.lookupSCC(C1);
1924   LazyCallGraph::SCC &C2C = *CG.lookupSCC(C2);
1925   LazyCallGraph::SCC &C3C = *CG.lookupSCC(C3);
1926   LazyCallGraph::SCC &DC = *CG.lookupSCC(D);
1927 
1928   // Several call edges are initially present to force a particual post-order.
1929   // Remove them now, leaving an interleaved post-order pattern.
1930   RC.switchInternalEdgeToRef(B3, C3);
1931   RC.switchInternalEdgeToRef(C2, B3);
1932   RC.switchInternalEdgeToRef(B2, C2);
1933   RC.switchInternalEdgeToRef(C1, B2);
1934   RC.switchInternalEdgeToRef(B1, C1);
1935 
1936   // Check the initial post-order. We ensure this order with the extra edges
1937   // that are nuked above.
1938   ASSERT_EQ(8, RC.size());
1939   EXPECT_EQ(&DC, &RC[0]);
1940   EXPECT_EQ(&C3C, &RC[1]);
1941   EXPECT_EQ(&B3C, &RC[2]);
1942   EXPECT_EQ(&C2C, &RC[3]);
1943   EXPECT_EQ(&B2C, &RC[4]);
1944   EXPECT_EQ(&C1C, &RC[5]);
1945   EXPECT_EQ(&B1C, &RC[6]);
1946   EXPECT_EQ(&AC, &RC[7]);
1947 
1948   // Switch C3 -> B1 to a call edge. This doesn't form any new cycles but does
1949   // require reordering the SCCs in the face of tricky internal node
1950   // structures.
1951   auto MergedCs = RC.switchInternalEdgeToCall(C3, B1);
1952   EXPECT_EQ(0u, MergedCs.size());
1953   ASSERT_EQ(8, RC.size());
1954   EXPECT_EQ(&DC, &RC[0]);
1955   EXPECT_EQ(&B3C, &RC[1]);
1956   EXPECT_EQ(&B2C, &RC[2]);
1957   EXPECT_EQ(&B1C, &RC[3]);
1958   EXPECT_EQ(&C3C, &RC[4]);
1959   EXPECT_EQ(&C2C, &RC[5]);
1960   EXPECT_EQ(&C1C, &RC[6]);
1961   EXPECT_EQ(&AC, &RC[7]);
1962 }
1963 
1964 TEST(LazyCallGraphTest, InternalRefEdgeToCallBothPartitionAndMerge) {
1965   LLVMContext Context;
1966   // Test for having a postorder where between the source and target are all
1967   // three kinds of other SCCs:
1968   // 1) One connected to the target only that have to be shifted below the
1969   //    source.
1970   // 2) One connected to the source only that have to be shifted below the
1971   //    target.
1972   // 3) One connected to both source and target that has to remain and get
1973   //    merged away.
1974   //
1975   // To achieve this we construct a heavily connected graph to force
1976   // a particular post-order. Then we remove the forcing edges and connect
1977   // a cycle.
1978   //
1979   // Diagram for the graph we want on the left and the graph we use to force
1980   // the ordering on the right. Edges ponit down or right.
1981   //
1982   //   A    |    A    |
1983   //  / \   |   / \   |
1984   // B   E  |  B   \  |
1985   // |\  |  |  |\  |  |
1986   // | D |  |  C-D-E  |
1987   // |  \|  |  |  \|  |
1988   // C   F  |  \   F  |
1989   //  \ /   |   \ /   |
1990   //   G    |    G    |
1991   //
1992   // And we form a cycle by connecting F to B.
1993   std::unique_ptr<Module> M =
1994       parseAssembly(Context, "define void @a() {\n"
1995                              "entry:\n"
1996                              "  call void @b()\n"
1997                              "  call void @e()\n"
1998                              "  ret void\n"
1999                              "}\n"
2000                              "define void @b() {\n"
2001                              "entry:\n"
2002                              "  call void @c()\n"
2003                              "  call void @d()\n"
2004                              "  ret void\n"
2005                              "}\n"
2006                              "define void @c() {\n"
2007                              "entry:\n"
2008                              "  call void @d()\n"
2009                              "  call void @g()\n"
2010                              "  ret void\n"
2011                              "}\n"
2012                              "define void @d() {\n"
2013                              "entry:\n"
2014                              "  call void @e()\n"
2015                              "  call void @f()\n"
2016                              "  ret void\n"
2017                              "}\n"
2018                              "define void @e() {\n"
2019                              "entry:\n"
2020                              "  call void @f()\n"
2021                              "  ret void\n"
2022                              "}\n"
2023                              "define void @f() {\n"
2024                              "entry:\n"
2025                              "  store void()* @b, void()** undef\n"
2026                              "  call void @g()\n"
2027                              "  ret void\n"
2028                              "}\n"
2029                              "define void @g() {\n"
2030                              "entry:\n"
2031                              "  store void()* @a, void()** undef\n"
2032                              "  ret void\n"
2033                              "}\n");
2034   LazyCallGraph CG(*M);
2035 
2036   // Force the graph to be fully expanded.
2037   auto I = CG.postorder_ref_scc_begin();
2038   LazyCallGraph::RefSCC &RC = *I++;
2039   EXPECT_EQ(CG.postorder_ref_scc_end(), I);
2040 
2041   LazyCallGraph::Node &A = *CG.lookup(lookupFunction(*M, "a"));
2042   LazyCallGraph::Node &B = *CG.lookup(lookupFunction(*M, "b"));
2043   LazyCallGraph::Node &C = *CG.lookup(lookupFunction(*M, "c"));
2044   LazyCallGraph::Node &D = *CG.lookup(lookupFunction(*M, "d"));
2045   LazyCallGraph::Node &E = *CG.lookup(lookupFunction(*M, "e"));
2046   LazyCallGraph::Node &F = *CG.lookup(lookupFunction(*M, "f"));
2047   LazyCallGraph::Node &G = *CG.lookup(lookupFunction(*M, "g"));
2048   LazyCallGraph::SCC &AC = *CG.lookupSCC(A);
2049   LazyCallGraph::SCC &BC = *CG.lookupSCC(B);
2050   LazyCallGraph::SCC &CC = *CG.lookupSCC(C);
2051   LazyCallGraph::SCC &DC = *CG.lookupSCC(D);
2052   LazyCallGraph::SCC &EC = *CG.lookupSCC(E);
2053   LazyCallGraph::SCC &FC = *CG.lookupSCC(F);
2054   LazyCallGraph::SCC &GC = *CG.lookupSCC(G);
2055 
2056   // Remove the extra edges that were used to force a particular post-order.
2057   RC.switchInternalEdgeToRef(C, D);
2058   RC.switchInternalEdgeToRef(D, E);
2059 
2060   // Check the initial post-order. We ensure this order with the extra edges
2061   // that are nuked above.
2062   ASSERT_EQ(7, RC.size());
2063   EXPECT_EQ(&GC, &RC[0]);
2064   EXPECT_EQ(&FC, &RC[1]);
2065   EXPECT_EQ(&EC, &RC[2]);
2066   EXPECT_EQ(&DC, &RC[3]);
2067   EXPECT_EQ(&CC, &RC[4]);
2068   EXPECT_EQ(&BC, &RC[5]);
2069   EXPECT_EQ(&AC, &RC[6]);
2070 
2071   // Switch F -> B to a call edge. This merges B, D, and F into a single SCC,
2072   // and has to place the C and E SCCs on either side of it:
2073   //   A          A    |
2074   //  / \        / \   |
2075   // B   E      |   E  |
2076   // |\  |       \ /   |
2077   // | D |  ->    B    |
2078   // |  \|       / \   |
2079   // C   F      C   |  |
2080   //  \ /        \ /   |
2081   //   G          G    |
2082   auto MergedCs = RC.switchInternalEdgeToCall(F, B);
2083   ASSERT_EQ(2u, MergedCs.size());
2084   EXPECT_EQ(&FC, MergedCs[0]);
2085   EXPECT_EQ(&DC, MergedCs[1]);
2086   EXPECT_EQ(3, BC.size());
2087 
2088   // And make sure the postorder was updated.
2089   ASSERT_EQ(5, RC.size());
2090   EXPECT_EQ(&GC, &RC[0]);
2091   EXPECT_EQ(&CC, &RC[1]);
2092   EXPECT_EQ(&BC, &RC[2]);
2093   EXPECT_EQ(&EC, &RC[3]);
2094   EXPECT_EQ(&AC, &RC[4]);
2095 }
2096 
2097 // Test for IR containing constants using blockaddress constant expressions.
2098 // These are truly unique constructs: constant expressions with non-constant
2099 // operands.
2100 TEST(LazyCallGraphTest, HandleBlockAddress) {
2101   LLVMContext Context;
2102   std::unique_ptr<Module> M =
2103       parseAssembly(Context, "define void @f() {\n"
2104                              "entry:\n"
2105                              "  ret void\n"
2106                              "bb:\n"
2107                              "  unreachable\n"
2108                              "}\n"
2109                              "define void @g(i8** %ptr) {\n"
2110                              "entry:\n"
2111                              "  store i8* blockaddress(@f, %bb), i8** %ptr\n"
2112                              "  ret void\n"
2113                              "}\n");
2114   LazyCallGraph CG(*M);
2115 
2116   auto I = CG.postorder_ref_scc_begin();
2117   LazyCallGraph::RefSCC &FRC = *I++;
2118   LazyCallGraph::RefSCC &GRC = *I++;
2119   EXPECT_EQ(CG.postorder_ref_scc_end(), I);
2120 
2121   LazyCallGraph::Node &F = *CG.lookup(lookupFunction(*M, "f"));
2122   LazyCallGraph::Node &G = *CG.lookup(lookupFunction(*M, "g"));
2123   EXPECT_EQ(&FRC, CG.lookupRefSCC(F));
2124   EXPECT_EQ(&GRC, CG.lookupRefSCC(G));
2125   EXPECT_TRUE(GRC.isParentOf(FRC));
2126 }
2127 
2128 }
2129