xref: /aosp_15_r20/external/llvm/unittests/Analysis/LazyCallGraphTest.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===- LazyCallGraphTest.cpp - Unit tests for the lazy CG analysis --------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker 
10*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/LazyCallGraph.h"
11*9880d681SAndroid Build Coastguard Worker #include "llvm/AsmParser/Parser.h"
12*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Function.h"
13*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/LLVMContext.h"
14*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Module.h"
15*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/ErrorHandling.h"
16*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/SourceMgr.h"
17*9880d681SAndroid Build Coastguard Worker #include "gtest/gtest.h"
18*9880d681SAndroid Build Coastguard Worker #include <memory>
19*9880d681SAndroid Build Coastguard Worker 
20*9880d681SAndroid Build Coastguard Worker using namespace llvm;
21*9880d681SAndroid Build Coastguard Worker 
22*9880d681SAndroid Build Coastguard Worker namespace {
23*9880d681SAndroid Build Coastguard Worker 
parseAssembly(LLVMContext & Context,const char * Assembly)24*9880d681SAndroid Build Coastguard Worker std::unique_ptr<Module> parseAssembly(LLVMContext &Context,
25*9880d681SAndroid Build Coastguard Worker                                       const char *Assembly) {
26*9880d681SAndroid Build Coastguard Worker   SMDiagnostic Error;
27*9880d681SAndroid Build Coastguard Worker   std::unique_ptr<Module> M = parseAssemblyString(Assembly, Error, Context);
28*9880d681SAndroid Build Coastguard Worker 
29*9880d681SAndroid Build Coastguard Worker   std::string ErrMsg;
30*9880d681SAndroid Build Coastguard Worker   raw_string_ostream OS(ErrMsg);
31*9880d681SAndroid Build Coastguard Worker   Error.print("", OS);
32*9880d681SAndroid Build Coastguard Worker 
33*9880d681SAndroid Build Coastguard Worker   // A failure here means that the test itself is buggy.
34*9880d681SAndroid Build Coastguard Worker   if (!M)
35*9880d681SAndroid Build Coastguard Worker     report_fatal_error(OS.str().c_str());
36*9880d681SAndroid Build Coastguard Worker 
37*9880d681SAndroid Build Coastguard Worker   return M;
38*9880d681SAndroid Build Coastguard Worker }
39*9880d681SAndroid Build Coastguard Worker 
40*9880d681SAndroid Build Coastguard Worker /*
41*9880d681SAndroid Build Coastguard Worker    IR forming a call graph with a diamond of triangle-shaped SCCs:
42*9880d681SAndroid Build Coastguard Worker 
43*9880d681SAndroid Build Coastguard Worker            d1
44*9880d681SAndroid Build Coastguard Worker           /  \
45*9880d681SAndroid Build Coastguard Worker          d3--d2
46*9880d681SAndroid Build Coastguard Worker         /     \
47*9880d681SAndroid Build Coastguard Worker        b1     c1
48*9880d681SAndroid Build Coastguard Worker      /  \    /  \
49*9880d681SAndroid Build Coastguard Worker     b3--b2  c3--c2
50*9880d681SAndroid Build Coastguard Worker          \  /
51*9880d681SAndroid Build Coastguard Worker           a1
52*9880d681SAndroid Build Coastguard Worker          /  \
53*9880d681SAndroid Build Coastguard Worker         a3--a2
54*9880d681SAndroid Build Coastguard Worker 
55*9880d681SAndroid Build Coastguard Worker    All call edges go up between SCCs, and clockwise around the SCC.
56*9880d681SAndroid Build Coastguard Worker  */
57*9880d681SAndroid Build Coastguard Worker static const char DiamondOfTriangles[] =
58*9880d681SAndroid Build Coastguard Worker      "define void @a1() {\n"
59*9880d681SAndroid Build Coastguard Worker      "entry:\n"
60*9880d681SAndroid Build Coastguard Worker      "  call void @a2()\n"
61*9880d681SAndroid Build Coastguard Worker      "  call void @b2()\n"
62*9880d681SAndroid Build Coastguard Worker      "  call void @c3()\n"
63*9880d681SAndroid Build Coastguard Worker      "  ret void\n"
64*9880d681SAndroid Build Coastguard Worker      "}\n"
65*9880d681SAndroid Build Coastguard Worker      "define void @a2() {\n"
66*9880d681SAndroid Build Coastguard Worker      "entry:\n"
67*9880d681SAndroid Build Coastguard Worker      "  call void @a3()\n"
68*9880d681SAndroid Build Coastguard Worker      "  ret void\n"
69*9880d681SAndroid Build Coastguard Worker      "}\n"
70*9880d681SAndroid Build Coastguard Worker      "define void @a3() {\n"
71*9880d681SAndroid Build Coastguard Worker      "entry:\n"
72*9880d681SAndroid Build Coastguard Worker      "  call void @a1()\n"
73*9880d681SAndroid Build Coastguard Worker      "  ret void\n"
74*9880d681SAndroid Build Coastguard Worker      "}\n"
75*9880d681SAndroid Build Coastguard Worker      "define void @b1() {\n"
76*9880d681SAndroid Build Coastguard Worker      "entry:\n"
77*9880d681SAndroid Build Coastguard Worker      "  call void @b2()\n"
78*9880d681SAndroid Build Coastguard Worker      "  call void @d3()\n"
79*9880d681SAndroid Build Coastguard Worker      "  ret void\n"
80*9880d681SAndroid Build Coastguard Worker      "}\n"
81*9880d681SAndroid Build Coastguard Worker      "define void @b2() {\n"
82*9880d681SAndroid Build Coastguard Worker      "entry:\n"
83*9880d681SAndroid Build Coastguard Worker      "  call void @b3()\n"
84*9880d681SAndroid Build Coastguard Worker      "  ret void\n"
85*9880d681SAndroid Build Coastguard Worker      "}\n"
86*9880d681SAndroid Build Coastguard Worker      "define void @b3() {\n"
87*9880d681SAndroid Build Coastguard Worker      "entry:\n"
88*9880d681SAndroid Build Coastguard Worker      "  call void @b1()\n"
89*9880d681SAndroid Build Coastguard Worker      "  ret void\n"
90*9880d681SAndroid Build Coastguard Worker      "}\n"
91*9880d681SAndroid Build Coastguard Worker      "define void @c1() {\n"
92*9880d681SAndroid Build Coastguard Worker      "entry:\n"
93*9880d681SAndroid Build Coastguard Worker      "  call void @c2()\n"
94*9880d681SAndroid Build Coastguard Worker      "  call void @d2()\n"
95*9880d681SAndroid Build Coastguard Worker      "  ret void\n"
96*9880d681SAndroid Build Coastguard Worker      "}\n"
97*9880d681SAndroid Build Coastguard Worker      "define void @c2() {\n"
98*9880d681SAndroid Build Coastguard Worker      "entry:\n"
99*9880d681SAndroid Build Coastguard Worker      "  call void @c3()\n"
100*9880d681SAndroid Build Coastguard Worker      "  ret void\n"
101*9880d681SAndroid Build Coastguard Worker      "}\n"
102*9880d681SAndroid Build Coastguard Worker      "define void @c3() {\n"
103*9880d681SAndroid Build Coastguard Worker      "entry:\n"
104*9880d681SAndroid Build Coastguard Worker      "  call void @c1()\n"
105*9880d681SAndroid Build Coastguard Worker      "  ret void\n"
106*9880d681SAndroid Build Coastguard Worker      "}\n"
107*9880d681SAndroid Build Coastguard Worker      "define void @d1() {\n"
108*9880d681SAndroid Build Coastguard Worker      "entry:\n"
109*9880d681SAndroid Build Coastguard Worker      "  call void @d2()\n"
110*9880d681SAndroid Build Coastguard Worker      "  ret void\n"
111*9880d681SAndroid Build Coastguard Worker      "}\n"
112*9880d681SAndroid Build Coastguard Worker      "define void @d2() {\n"
113*9880d681SAndroid Build Coastguard Worker      "entry:\n"
114*9880d681SAndroid Build Coastguard Worker      "  call void @d3()\n"
115*9880d681SAndroid Build Coastguard Worker      "  ret void\n"
116*9880d681SAndroid Build Coastguard Worker      "}\n"
117*9880d681SAndroid Build Coastguard Worker      "define void @d3() {\n"
118*9880d681SAndroid Build Coastguard Worker      "entry:\n"
119*9880d681SAndroid Build Coastguard Worker      "  call void @d1()\n"
120*9880d681SAndroid Build Coastguard Worker      "  ret void\n"
121*9880d681SAndroid Build Coastguard Worker      "}\n";
122*9880d681SAndroid Build Coastguard Worker 
TEST(LazyCallGraphTest,BasicGraphFormation)123*9880d681SAndroid Build Coastguard Worker TEST(LazyCallGraphTest, BasicGraphFormation) {
124*9880d681SAndroid Build Coastguard Worker   LLVMContext Context;
125*9880d681SAndroid Build Coastguard Worker   std::unique_ptr<Module> M = parseAssembly(Context, DiamondOfTriangles);
126*9880d681SAndroid Build Coastguard Worker   LazyCallGraph CG(*M);
127*9880d681SAndroid Build Coastguard Worker 
128*9880d681SAndroid Build Coastguard Worker   // The order of the entry nodes should be stable w.r.t. the source order of
129*9880d681SAndroid Build Coastguard Worker   // the IR, and everything in our module is an entry node, so just directly
130*9880d681SAndroid Build Coastguard Worker   // build variables for each node.
131*9880d681SAndroid Build Coastguard Worker   auto I = CG.begin();
132*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &A1 = (I++)->getNode(CG);
133*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ("a1", A1.getFunction().getName());
134*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &A2 = (I++)->getNode(CG);
135*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ("a2", A2.getFunction().getName());
136*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &A3 = (I++)->getNode(CG);
137*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ("a3", A3.getFunction().getName());
138*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &B1 = (I++)->getNode(CG);
139*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ("b1", B1.getFunction().getName());
140*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &B2 = (I++)->getNode(CG);
141*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ("b2", B2.getFunction().getName());
142*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &B3 = (I++)->getNode(CG);
143*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ("b3", B3.getFunction().getName());
144*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &C1 = (I++)->getNode(CG);
145*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ("c1", C1.getFunction().getName());
146*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &C2 = (I++)->getNode(CG);
147*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ("c2", C2.getFunction().getName());
148*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &C3 = (I++)->getNode(CG);
149*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ("c3", C3.getFunction().getName());
150*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &D1 = (I++)->getNode(CG);
151*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ("d1", D1.getFunction().getName());
152*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &D2 = (I++)->getNode(CG);
153*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ("d2", D2.getFunction().getName());
154*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &D3 = (I++)->getNode(CG);
155*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ("d3", D3.getFunction().getName());
156*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(CG.end(), I);
157*9880d681SAndroid Build Coastguard Worker 
158*9880d681SAndroid Build Coastguard Worker   // Build vectors and sort them for the rest of the assertions to make them
159*9880d681SAndroid Build Coastguard Worker   // independent of order.
160*9880d681SAndroid Build Coastguard Worker   std::vector<std::string> Nodes;
161*9880d681SAndroid Build Coastguard Worker 
162*9880d681SAndroid Build Coastguard Worker   for (LazyCallGraph::Edge &E : A1)
163*9880d681SAndroid Build Coastguard Worker     Nodes.push_back(E.getFunction().getName());
164*9880d681SAndroid Build Coastguard Worker   std::sort(Nodes.begin(), Nodes.end());
165*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ("a2", Nodes[0]);
166*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ("b2", Nodes[1]);
167*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ("c3", Nodes[2]);
168*9880d681SAndroid Build Coastguard Worker   Nodes.clear();
169*9880d681SAndroid Build Coastguard Worker 
170*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(A2.end(), std::next(A2.begin()));
171*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ("a3", A2.begin()->getFunction().getName());
172*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(A3.end(), std::next(A3.begin()));
173*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ("a1", A3.begin()->getFunction().getName());
174*9880d681SAndroid Build Coastguard Worker 
175*9880d681SAndroid Build Coastguard Worker   for (LazyCallGraph::Edge &E : B1)
176*9880d681SAndroid Build Coastguard Worker     Nodes.push_back(E.getFunction().getName());
177*9880d681SAndroid Build Coastguard Worker   std::sort(Nodes.begin(), Nodes.end());
178*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ("b2", Nodes[0]);
179*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ("d3", Nodes[1]);
180*9880d681SAndroid Build Coastguard Worker   Nodes.clear();
181*9880d681SAndroid Build Coastguard Worker 
182*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(B2.end(), std::next(B2.begin()));
183*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ("b3", B2.begin()->getFunction().getName());
184*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(B3.end(), std::next(B3.begin()));
185*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ("b1", B3.begin()->getFunction().getName());
186*9880d681SAndroid Build Coastguard Worker 
187*9880d681SAndroid Build Coastguard Worker   for (LazyCallGraph::Edge &E : C1)
188*9880d681SAndroid Build Coastguard Worker     Nodes.push_back(E.getFunction().getName());
189*9880d681SAndroid Build Coastguard Worker   std::sort(Nodes.begin(), Nodes.end());
190*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ("c2", Nodes[0]);
191*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ("d2", Nodes[1]);
192*9880d681SAndroid Build Coastguard Worker   Nodes.clear();
193*9880d681SAndroid Build Coastguard Worker 
194*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(C2.end(), std::next(C2.begin()));
195*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ("c3", C2.begin()->getFunction().getName());
196*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(C3.end(), std::next(C3.begin()));
197*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ("c1", C3.begin()->getFunction().getName());
198*9880d681SAndroid Build Coastguard Worker 
199*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(D1.end(), std::next(D1.begin()));
200*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ("d2", D1.begin()->getFunction().getName());
201*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(D2.end(), std::next(D2.begin()));
202*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ("d3", D2.begin()->getFunction().getName());
203*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(D3.end(), std::next(D3.begin()));
204*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ("d1", D3.begin()->getFunction().getName());
205*9880d681SAndroid Build Coastguard Worker 
206*9880d681SAndroid Build Coastguard Worker   // Now lets look at the RefSCCs and SCCs.
207*9880d681SAndroid Build Coastguard Worker   auto J = CG.postorder_ref_scc_begin();
208*9880d681SAndroid Build Coastguard Worker 
209*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::RefSCC &D = *J++;
210*9880d681SAndroid Build Coastguard Worker   ASSERT_EQ(1, D.size());
211*9880d681SAndroid Build Coastguard Worker   for (LazyCallGraph::Node &N : *D.begin())
212*9880d681SAndroid Build Coastguard Worker     Nodes.push_back(N.getFunction().getName());
213*9880d681SAndroid Build Coastguard Worker   std::sort(Nodes.begin(), Nodes.end());
214*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(3u, Nodes.size());
215*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ("d1", Nodes[0]);
216*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ("d2", Nodes[1]);
217*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ("d3", Nodes[2]);
218*9880d681SAndroid Build Coastguard Worker   Nodes.clear();
219*9880d681SAndroid Build Coastguard Worker   EXPECT_FALSE(D.isParentOf(D));
220*9880d681SAndroid Build Coastguard Worker   EXPECT_FALSE(D.isChildOf(D));
221*9880d681SAndroid Build Coastguard Worker   EXPECT_FALSE(D.isAncestorOf(D));
222*9880d681SAndroid Build Coastguard Worker   EXPECT_FALSE(D.isDescendantOf(D));
223*9880d681SAndroid Build Coastguard Worker 
224*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::RefSCC &C = *J++;
225*9880d681SAndroid Build Coastguard Worker   ASSERT_EQ(1, C.size());
226*9880d681SAndroid Build Coastguard Worker   for (LazyCallGraph::Node &N : *C.begin())
227*9880d681SAndroid Build Coastguard Worker     Nodes.push_back(N.getFunction().getName());
228*9880d681SAndroid Build Coastguard Worker   std::sort(Nodes.begin(), Nodes.end());
229*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(3u, Nodes.size());
230*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ("c1", Nodes[0]);
231*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ("c2", Nodes[1]);
232*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ("c3", Nodes[2]);
233*9880d681SAndroid Build Coastguard Worker   Nodes.clear();
234*9880d681SAndroid Build Coastguard Worker   EXPECT_TRUE(C.isParentOf(D));
235*9880d681SAndroid Build Coastguard Worker   EXPECT_FALSE(C.isChildOf(D));
236*9880d681SAndroid Build Coastguard Worker   EXPECT_TRUE(C.isAncestorOf(D));
237*9880d681SAndroid Build Coastguard Worker   EXPECT_FALSE(C.isDescendantOf(D));
238*9880d681SAndroid Build Coastguard Worker 
239*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::RefSCC &B = *J++;
240*9880d681SAndroid Build Coastguard Worker   ASSERT_EQ(1, B.size());
241*9880d681SAndroid Build Coastguard Worker   for (LazyCallGraph::Node &N : *B.begin())
242*9880d681SAndroid Build Coastguard Worker     Nodes.push_back(N.getFunction().getName());
243*9880d681SAndroid Build Coastguard Worker   std::sort(Nodes.begin(), Nodes.end());
244*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(3u, Nodes.size());
245*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ("b1", Nodes[0]);
246*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ("b2", Nodes[1]);
247*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ("b3", Nodes[2]);
248*9880d681SAndroid Build Coastguard Worker   Nodes.clear();
249*9880d681SAndroid Build Coastguard Worker   EXPECT_TRUE(B.isParentOf(D));
250*9880d681SAndroid Build Coastguard Worker   EXPECT_FALSE(B.isChildOf(D));
251*9880d681SAndroid Build Coastguard Worker   EXPECT_TRUE(B.isAncestorOf(D));
252*9880d681SAndroid Build Coastguard Worker   EXPECT_FALSE(B.isDescendantOf(D));
253*9880d681SAndroid Build Coastguard Worker   EXPECT_FALSE(B.isAncestorOf(C));
254*9880d681SAndroid Build Coastguard Worker   EXPECT_FALSE(C.isAncestorOf(B));
255*9880d681SAndroid Build Coastguard Worker 
256*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::RefSCC &A = *J++;
257*9880d681SAndroid Build Coastguard Worker   ASSERT_EQ(1, A.size());
258*9880d681SAndroid Build Coastguard Worker   for (LazyCallGraph::Node &N : *A.begin())
259*9880d681SAndroid Build Coastguard Worker     Nodes.push_back(N.getFunction().getName());
260*9880d681SAndroid Build Coastguard Worker   std::sort(Nodes.begin(), Nodes.end());
261*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(3u, Nodes.size());
262*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ("a1", Nodes[0]);
263*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ("a2", Nodes[1]);
264*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ("a3", Nodes[2]);
265*9880d681SAndroid Build Coastguard Worker   Nodes.clear();
266*9880d681SAndroid Build Coastguard Worker   EXPECT_TRUE(A.isParentOf(B));
267*9880d681SAndroid Build Coastguard Worker   EXPECT_TRUE(A.isParentOf(C));
268*9880d681SAndroid Build Coastguard Worker   EXPECT_FALSE(A.isParentOf(D));
269*9880d681SAndroid Build Coastguard Worker   EXPECT_TRUE(A.isAncestorOf(B));
270*9880d681SAndroid Build Coastguard Worker   EXPECT_TRUE(A.isAncestorOf(C));
271*9880d681SAndroid Build Coastguard Worker   EXPECT_TRUE(A.isAncestorOf(D));
272*9880d681SAndroid Build Coastguard Worker 
273*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(CG.postorder_ref_scc_end(), J);
274*9880d681SAndroid Build Coastguard Worker }
275*9880d681SAndroid Build Coastguard Worker 
lookupFunction(Module & M,StringRef Name)276*9880d681SAndroid Build Coastguard Worker static Function &lookupFunction(Module &M, StringRef Name) {
277*9880d681SAndroid Build Coastguard Worker   for (Function &F : M)
278*9880d681SAndroid Build Coastguard Worker     if (F.getName() == Name)
279*9880d681SAndroid Build Coastguard Worker       return F;
280*9880d681SAndroid Build Coastguard Worker   report_fatal_error("Couldn't find function!");
281*9880d681SAndroid Build Coastguard Worker }
282*9880d681SAndroid Build Coastguard Worker 
TEST(LazyCallGraphTest,BasicGraphMutation)283*9880d681SAndroid Build Coastguard Worker TEST(LazyCallGraphTest, BasicGraphMutation) {
284*9880d681SAndroid Build Coastguard Worker   LLVMContext Context;
285*9880d681SAndroid Build Coastguard Worker   std::unique_ptr<Module> M = parseAssembly(Context, "define void @a() {\n"
286*9880d681SAndroid Build Coastguard Worker                                                      "entry:\n"
287*9880d681SAndroid Build Coastguard Worker                                                      "  call void @b()\n"
288*9880d681SAndroid Build Coastguard Worker                                                      "  call void @c()\n"
289*9880d681SAndroid Build Coastguard Worker                                                      "  ret void\n"
290*9880d681SAndroid Build Coastguard Worker                                                      "}\n"
291*9880d681SAndroid Build Coastguard Worker                                                      "define void @b() {\n"
292*9880d681SAndroid Build Coastguard Worker                                                      "entry:\n"
293*9880d681SAndroid Build Coastguard Worker                                                      "  ret void\n"
294*9880d681SAndroid Build Coastguard Worker                                                      "}\n"
295*9880d681SAndroid Build Coastguard Worker                                                      "define void @c() {\n"
296*9880d681SAndroid Build Coastguard Worker                                                      "entry:\n"
297*9880d681SAndroid Build Coastguard Worker                                                      "  ret void\n"
298*9880d681SAndroid Build Coastguard Worker                                                      "}\n");
299*9880d681SAndroid Build Coastguard Worker   LazyCallGraph CG(*M);
300*9880d681SAndroid Build Coastguard Worker 
301*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &A = CG.get(lookupFunction(*M, "a"));
302*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &B = CG.get(lookupFunction(*M, "b"));
303*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(2, std::distance(A.begin(), A.end()));
304*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(0, std::distance(B.begin(), B.end()));
305*9880d681SAndroid Build Coastguard Worker 
306*9880d681SAndroid Build Coastguard Worker   CG.insertEdge(B, lookupFunction(*M, "c"), LazyCallGraph::Edge::Call);
307*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(1, std::distance(B.begin(), B.end()));
308*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &C = B.begin()->getNode(CG);
309*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(0, std::distance(C.begin(), C.end()));
310*9880d681SAndroid Build Coastguard Worker 
311*9880d681SAndroid Build Coastguard Worker   CG.insertEdge(C, B.getFunction(), LazyCallGraph::Edge::Call);
312*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(1, std::distance(C.begin(), C.end()));
313*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&B, C.begin()->getNode());
314*9880d681SAndroid Build Coastguard Worker 
315*9880d681SAndroid Build Coastguard Worker   CG.insertEdge(C, C.getFunction(), LazyCallGraph::Edge::Call);
316*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(2, std::distance(C.begin(), C.end()));
317*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&B, C.begin()->getNode());
318*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&C, std::next(C.begin())->getNode());
319*9880d681SAndroid Build Coastguard Worker 
320*9880d681SAndroid Build Coastguard Worker   CG.removeEdge(C, B.getFunction());
321*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(1, std::distance(C.begin(), C.end()));
322*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&C, C.begin()->getNode());
323*9880d681SAndroid Build Coastguard Worker 
324*9880d681SAndroid Build Coastguard Worker   CG.removeEdge(C, C.getFunction());
325*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(0, std::distance(C.begin(), C.end()));
326*9880d681SAndroid Build Coastguard Worker 
327*9880d681SAndroid Build Coastguard Worker   CG.removeEdge(B, C.getFunction());
328*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(0, std::distance(B.begin(), B.end()));
329*9880d681SAndroid Build Coastguard Worker }
330*9880d681SAndroid Build Coastguard Worker 
TEST(LazyCallGraphTest,InnerSCCFormation)331*9880d681SAndroid Build Coastguard Worker TEST(LazyCallGraphTest, InnerSCCFormation) {
332*9880d681SAndroid Build Coastguard Worker   LLVMContext Context;
333*9880d681SAndroid Build Coastguard Worker   std::unique_ptr<Module> M = parseAssembly(Context, DiamondOfTriangles);
334*9880d681SAndroid Build Coastguard Worker   LazyCallGraph CG(*M);
335*9880d681SAndroid Build Coastguard Worker 
336*9880d681SAndroid Build Coastguard Worker   // Now mutate the graph to connect every node into a single RefSCC to ensure
337*9880d681SAndroid Build Coastguard Worker   // that our inner SCC formation handles the rest.
338*9880d681SAndroid Build Coastguard Worker   CG.insertEdge(lookupFunction(*M, "d1"), lookupFunction(*M, "a1"),
339*9880d681SAndroid Build Coastguard Worker                 LazyCallGraph::Edge::Ref);
340*9880d681SAndroid Build Coastguard Worker 
341*9880d681SAndroid Build Coastguard Worker   // Build vectors and sort them for the rest of the assertions to make them
342*9880d681SAndroid Build Coastguard Worker   // independent of order.
343*9880d681SAndroid Build Coastguard Worker   std::vector<std::string> Nodes;
344*9880d681SAndroid Build Coastguard Worker 
345*9880d681SAndroid Build Coastguard Worker   // We should build a single RefSCC for the entire graph.
346*9880d681SAndroid Build Coastguard Worker   auto I = CG.postorder_ref_scc_begin();
347*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::RefSCC &RC = *I++;
348*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(CG.postorder_ref_scc_end(), I);
349*9880d681SAndroid Build Coastguard Worker 
350*9880d681SAndroid Build Coastguard Worker   // Now walk the four SCCs which should be in post-order.
351*9880d681SAndroid Build Coastguard Worker   auto J = RC.begin();
352*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::SCC &D = *J++;
353*9880d681SAndroid Build Coastguard Worker   for (LazyCallGraph::Node &N : D)
354*9880d681SAndroid Build Coastguard Worker     Nodes.push_back(N.getFunction().getName());
355*9880d681SAndroid Build Coastguard Worker   std::sort(Nodes.begin(), Nodes.end());
356*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(3u, Nodes.size());
357*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ("d1", Nodes[0]);
358*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ("d2", Nodes[1]);
359*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ("d3", Nodes[2]);
360*9880d681SAndroid Build Coastguard Worker   Nodes.clear();
361*9880d681SAndroid Build Coastguard Worker 
362*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::SCC &B = *J++;
363*9880d681SAndroid Build Coastguard Worker   for (LazyCallGraph::Node &N : B)
364*9880d681SAndroid Build Coastguard Worker     Nodes.push_back(N.getFunction().getName());
365*9880d681SAndroid Build Coastguard Worker   std::sort(Nodes.begin(), Nodes.end());
366*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(3u, Nodes.size());
367*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ("b1", Nodes[0]);
368*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ("b2", Nodes[1]);
369*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ("b3", Nodes[2]);
370*9880d681SAndroid Build Coastguard Worker   Nodes.clear();
371*9880d681SAndroid Build Coastguard Worker 
372*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::SCC &C = *J++;
373*9880d681SAndroid Build Coastguard Worker   for (LazyCallGraph::Node &N : C)
374*9880d681SAndroid Build Coastguard Worker     Nodes.push_back(N.getFunction().getName());
375*9880d681SAndroid Build Coastguard Worker   std::sort(Nodes.begin(), Nodes.end());
376*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(3u, Nodes.size());
377*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ("c1", Nodes[0]);
378*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ("c2", Nodes[1]);
379*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ("c3", Nodes[2]);
380*9880d681SAndroid Build Coastguard Worker   Nodes.clear();
381*9880d681SAndroid Build Coastguard Worker 
382*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::SCC &A = *J++;
383*9880d681SAndroid Build Coastguard Worker   for (LazyCallGraph::Node &N : A)
384*9880d681SAndroid Build Coastguard Worker     Nodes.push_back(N.getFunction().getName());
385*9880d681SAndroid Build Coastguard Worker   std::sort(Nodes.begin(), Nodes.end());
386*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(3u, Nodes.size());
387*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ("a1", Nodes[0]);
388*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ("a2", Nodes[1]);
389*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ("a3", Nodes[2]);
390*9880d681SAndroid Build Coastguard Worker   Nodes.clear();
391*9880d681SAndroid Build Coastguard Worker 
392*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(RC.end(), J);
393*9880d681SAndroid Build Coastguard Worker }
394*9880d681SAndroid Build Coastguard Worker 
TEST(LazyCallGraphTest,MultiArmSCC)395*9880d681SAndroid Build Coastguard Worker TEST(LazyCallGraphTest, MultiArmSCC) {
396*9880d681SAndroid Build Coastguard Worker   LLVMContext Context;
397*9880d681SAndroid Build Coastguard Worker   // Two interlocking cycles. The really useful thing about this SCC is that it
398*9880d681SAndroid Build Coastguard Worker   // will require Tarjan's DFS to backtrack and finish processing all of the
399*9880d681SAndroid Build Coastguard Worker   // children of each node in the SCC. Since this involves call edges, both
400*9880d681SAndroid Build Coastguard Worker   // Tarjan implementations will have to successfully navigate the structure.
401*9880d681SAndroid Build Coastguard Worker   std::unique_ptr<Module> M = parseAssembly(Context, "define void @f1() {\n"
402*9880d681SAndroid Build Coastguard Worker                                                      "entry:\n"
403*9880d681SAndroid Build Coastguard Worker                                                      "  call void @f2()\n"
404*9880d681SAndroid Build Coastguard Worker                                                      "  call void @f4()\n"
405*9880d681SAndroid Build Coastguard Worker                                                      "  ret void\n"
406*9880d681SAndroid Build Coastguard Worker                                                      "}\n"
407*9880d681SAndroid Build Coastguard Worker                                                      "define void @f2() {\n"
408*9880d681SAndroid Build Coastguard Worker                                                      "entry:\n"
409*9880d681SAndroid Build Coastguard Worker                                                      "  call void @f3()\n"
410*9880d681SAndroid Build Coastguard Worker                                                      "  ret void\n"
411*9880d681SAndroid Build Coastguard Worker                                                      "}\n"
412*9880d681SAndroid Build Coastguard Worker                                                      "define void @f3() {\n"
413*9880d681SAndroid Build Coastguard Worker                                                      "entry:\n"
414*9880d681SAndroid Build Coastguard Worker                                                      "  call void @f1()\n"
415*9880d681SAndroid Build Coastguard Worker                                                      "  ret void\n"
416*9880d681SAndroid Build Coastguard Worker                                                      "}\n"
417*9880d681SAndroid Build Coastguard Worker                                                      "define void @f4() {\n"
418*9880d681SAndroid Build Coastguard Worker                                                      "entry:\n"
419*9880d681SAndroid Build Coastguard Worker                                                      "  call void @f5()\n"
420*9880d681SAndroid Build Coastguard Worker                                                      "  ret void\n"
421*9880d681SAndroid Build Coastguard Worker                                                      "}\n"
422*9880d681SAndroid Build Coastguard Worker                                                      "define void @f5() {\n"
423*9880d681SAndroid Build Coastguard Worker                                                      "entry:\n"
424*9880d681SAndroid Build Coastguard Worker                                                      "  call void @f1()\n"
425*9880d681SAndroid Build Coastguard Worker                                                      "  ret void\n"
426*9880d681SAndroid Build Coastguard Worker                                                      "}\n");
427*9880d681SAndroid Build Coastguard Worker   LazyCallGraph CG(*M);
428*9880d681SAndroid Build Coastguard Worker 
429*9880d681SAndroid Build Coastguard Worker   // Force the graph to be fully expanded.
430*9880d681SAndroid Build Coastguard Worker   auto I = CG.postorder_ref_scc_begin();
431*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::RefSCC &RC = *I++;
432*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(CG.postorder_ref_scc_end(), I);
433*9880d681SAndroid Build Coastguard Worker 
434*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &N1 = *CG.lookup(lookupFunction(*M, "f1"));
435*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &N2 = *CG.lookup(lookupFunction(*M, "f2"));
436*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &N3 = *CG.lookup(lookupFunction(*M, "f3"));
437*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &N4 = *CG.lookup(lookupFunction(*M, "f4"));
438*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &N5 = *CG.lookup(lookupFunction(*M, "f4"));
439*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&RC, CG.lookupRefSCC(N1));
440*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&RC, CG.lookupRefSCC(N2));
441*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&RC, CG.lookupRefSCC(N3));
442*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&RC, CG.lookupRefSCC(N4));
443*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&RC, CG.lookupRefSCC(N5));
444*9880d681SAndroid Build Coastguard Worker 
445*9880d681SAndroid Build Coastguard Worker   ASSERT_EQ(1, RC.size());
446*9880d681SAndroid Build Coastguard Worker 
447*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::SCC &C = *RC.begin();
448*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&C, CG.lookupSCC(N1));
449*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&C, CG.lookupSCC(N2));
450*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&C, CG.lookupSCC(N3));
451*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&C, CG.lookupSCC(N4));
452*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&C, CG.lookupSCC(N5));
453*9880d681SAndroid Build Coastguard Worker }
454*9880d681SAndroid Build Coastguard Worker 
TEST(LazyCallGraphTest,OutgoingEdgeMutation)455*9880d681SAndroid Build Coastguard Worker TEST(LazyCallGraphTest, OutgoingEdgeMutation) {
456*9880d681SAndroid Build Coastguard Worker   LLVMContext Context;
457*9880d681SAndroid Build Coastguard Worker   std::unique_ptr<Module> M = parseAssembly(Context, "define void @a() {\n"
458*9880d681SAndroid Build Coastguard Worker                                                      "entry:\n"
459*9880d681SAndroid Build Coastguard Worker                                                      "  call void @b()\n"
460*9880d681SAndroid Build Coastguard Worker                                                      "  call void @c()\n"
461*9880d681SAndroid Build Coastguard Worker                                                      "  ret void\n"
462*9880d681SAndroid Build Coastguard Worker                                                      "}\n"
463*9880d681SAndroid Build Coastguard Worker                                                      "define void @b() {\n"
464*9880d681SAndroid Build Coastguard Worker                                                      "entry:\n"
465*9880d681SAndroid Build Coastguard Worker                                                      "  call void @d()\n"
466*9880d681SAndroid Build Coastguard Worker                                                      "  ret void\n"
467*9880d681SAndroid Build Coastguard Worker                                                      "}\n"
468*9880d681SAndroid Build Coastguard Worker                                                      "define void @c() {\n"
469*9880d681SAndroid Build Coastguard Worker                                                      "entry:\n"
470*9880d681SAndroid Build Coastguard Worker                                                      "  call void @d()\n"
471*9880d681SAndroid Build Coastguard Worker                                                      "  ret void\n"
472*9880d681SAndroid Build Coastguard Worker                                                      "}\n"
473*9880d681SAndroid Build Coastguard Worker                                                      "define void @d() {\n"
474*9880d681SAndroid Build Coastguard Worker                                                      "entry:\n"
475*9880d681SAndroid Build Coastguard Worker                                                      "  ret void\n"
476*9880d681SAndroid Build Coastguard Worker                                                      "}\n");
477*9880d681SAndroid Build Coastguard Worker   LazyCallGraph CG(*M);
478*9880d681SAndroid Build Coastguard Worker 
479*9880d681SAndroid Build Coastguard Worker   // Force the graph to be fully expanded.
480*9880d681SAndroid Build Coastguard Worker   for (LazyCallGraph::RefSCC &RC : CG.postorder_ref_sccs())
481*9880d681SAndroid Build Coastguard Worker     (void)RC;
482*9880d681SAndroid Build Coastguard Worker 
483*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &A = *CG.lookup(lookupFunction(*M, "a"));
484*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &B = *CG.lookup(lookupFunction(*M, "b"));
485*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &C = *CG.lookup(lookupFunction(*M, "c"));
486*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &D = *CG.lookup(lookupFunction(*M, "d"));
487*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::SCC &AC = *CG.lookupSCC(A);
488*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::SCC &BC = *CG.lookupSCC(B);
489*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::SCC &CC = *CG.lookupSCC(C);
490*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::SCC &DC = *CG.lookupSCC(D);
491*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::RefSCC &ARC = *CG.lookupRefSCC(A);
492*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::RefSCC &BRC = *CG.lookupRefSCC(B);
493*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::RefSCC &CRC = *CG.lookupRefSCC(C);
494*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::RefSCC &DRC = *CG.lookupRefSCC(D);
495*9880d681SAndroid Build Coastguard Worker   EXPECT_TRUE(ARC.isParentOf(BRC));
496*9880d681SAndroid Build Coastguard Worker   EXPECT_TRUE(ARC.isParentOf(CRC));
497*9880d681SAndroid Build Coastguard Worker   EXPECT_FALSE(ARC.isParentOf(DRC));
498*9880d681SAndroid Build Coastguard Worker   EXPECT_TRUE(ARC.isAncestorOf(DRC));
499*9880d681SAndroid Build Coastguard Worker   EXPECT_FALSE(DRC.isChildOf(ARC));
500*9880d681SAndroid Build Coastguard Worker   EXPECT_TRUE(DRC.isDescendantOf(ARC));
501*9880d681SAndroid Build Coastguard Worker   EXPECT_TRUE(DRC.isChildOf(BRC));
502*9880d681SAndroid Build Coastguard Worker   EXPECT_TRUE(DRC.isChildOf(CRC));
503*9880d681SAndroid Build Coastguard Worker 
504*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(2, std::distance(A.begin(), A.end()));
505*9880d681SAndroid Build Coastguard Worker   ARC.insertOutgoingEdge(A, D, LazyCallGraph::Edge::Call);
506*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(3, std::distance(A.begin(), A.end()));
507*9880d681SAndroid Build Coastguard Worker   const LazyCallGraph::Edge &NewE = A[D];
508*9880d681SAndroid Build Coastguard Worker   EXPECT_TRUE(NewE);
509*9880d681SAndroid Build Coastguard Worker   EXPECT_TRUE(NewE.isCall());
510*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&D, NewE.getNode());
511*9880d681SAndroid Build Coastguard Worker 
512*9880d681SAndroid Build Coastguard Worker   // Only the parent and child tests sholud have changed. The rest of the graph
513*9880d681SAndroid Build Coastguard Worker   // remains the same.
514*9880d681SAndroid Build Coastguard Worker   EXPECT_TRUE(ARC.isParentOf(DRC));
515*9880d681SAndroid Build Coastguard Worker   EXPECT_TRUE(ARC.isAncestorOf(DRC));
516*9880d681SAndroid Build Coastguard Worker   EXPECT_TRUE(DRC.isChildOf(ARC));
517*9880d681SAndroid Build Coastguard Worker   EXPECT_TRUE(DRC.isDescendantOf(ARC));
518*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&AC, CG.lookupSCC(A));
519*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&BC, CG.lookupSCC(B));
520*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&CC, CG.lookupSCC(C));
521*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&DC, CG.lookupSCC(D));
522*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&ARC, CG.lookupRefSCC(A));
523*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&BRC, CG.lookupRefSCC(B));
524*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&CRC, CG.lookupRefSCC(C));
525*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&DRC, CG.lookupRefSCC(D));
526*9880d681SAndroid Build Coastguard Worker 
527*9880d681SAndroid Build Coastguard Worker   ARC.switchOutgoingEdgeToRef(A, D);
528*9880d681SAndroid Build Coastguard Worker   EXPECT_FALSE(NewE.isCall());
529*9880d681SAndroid Build Coastguard Worker 
530*9880d681SAndroid Build Coastguard Worker   // Verify the graph remains the same.
531*9880d681SAndroid Build Coastguard Worker   EXPECT_TRUE(ARC.isParentOf(DRC));
532*9880d681SAndroid Build Coastguard Worker   EXPECT_TRUE(ARC.isAncestorOf(DRC));
533*9880d681SAndroid Build Coastguard Worker   EXPECT_TRUE(DRC.isChildOf(ARC));
534*9880d681SAndroid Build Coastguard Worker   EXPECT_TRUE(DRC.isDescendantOf(ARC));
535*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&AC, CG.lookupSCC(A));
536*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&BC, CG.lookupSCC(B));
537*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&CC, CG.lookupSCC(C));
538*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&DC, CG.lookupSCC(D));
539*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&ARC, CG.lookupRefSCC(A));
540*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&BRC, CG.lookupRefSCC(B));
541*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&CRC, CG.lookupRefSCC(C));
542*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&DRC, CG.lookupRefSCC(D));
543*9880d681SAndroid Build Coastguard Worker 
544*9880d681SAndroid Build Coastguard Worker   ARC.switchOutgoingEdgeToCall(A, D);
545*9880d681SAndroid Build Coastguard Worker   EXPECT_TRUE(NewE.isCall());
546*9880d681SAndroid Build Coastguard Worker 
547*9880d681SAndroid Build Coastguard Worker   // Verify the graph remains the same.
548*9880d681SAndroid Build Coastguard Worker   EXPECT_TRUE(ARC.isParentOf(DRC));
549*9880d681SAndroid Build Coastguard Worker   EXPECT_TRUE(ARC.isAncestorOf(DRC));
550*9880d681SAndroid Build Coastguard Worker   EXPECT_TRUE(DRC.isChildOf(ARC));
551*9880d681SAndroid Build Coastguard Worker   EXPECT_TRUE(DRC.isDescendantOf(ARC));
552*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&AC, CG.lookupSCC(A));
553*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&BC, CG.lookupSCC(B));
554*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&CC, CG.lookupSCC(C));
555*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&DC, CG.lookupSCC(D));
556*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&ARC, CG.lookupRefSCC(A));
557*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&BRC, CG.lookupRefSCC(B));
558*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&CRC, CG.lookupRefSCC(C));
559*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&DRC, CG.lookupRefSCC(D));
560*9880d681SAndroid Build Coastguard Worker 
561*9880d681SAndroid Build Coastguard Worker   ARC.removeOutgoingEdge(A, D);
562*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(2, std::distance(A.begin(), A.end()));
563*9880d681SAndroid Build Coastguard Worker 
564*9880d681SAndroid Build Coastguard Worker   // Now the parent and child tests fail again but the rest remains the same.
565*9880d681SAndroid Build Coastguard Worker   EXPECT_FALSE(ARC.isParentOf(DRC));
566*9880d681SAndroid Build Coastguard Worker   EXPECT_TRUE(ARC.isAncestorOf(DRC));
567*9880d681SAndroid Build Coastguard Worker   EXPECT_FALSE(DRC.isChildOf(ARC));
568*9880d681SAndroid Build Coastguard Worker   EXPECT_TRUE(DRC.isDescendantOf(ARC));
569*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&AC, CG.lookupSCC(A));
570*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&BC, CG.lookupSCC(B));
571*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&CC, CG.lookupSCC(C));
572*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&DC, CG.lookupSCC(D));
573*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&ARC, CG.lookupRefSCC(A));
574*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&BRC, CG.lookupRefSCC(B));
575*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&CRC, CG.lookupRefSCC(C));
576*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&DRC, CG.lookupRefSCC(D));
577*9880d681SAndroid Build Coastguard Worker }
578*9880d681SAndroid Build Coastguard Worker 
TEST(LazyCallGraphTest,IncomingEdgeInsertion)579*9880d681SAndroid Build Coastguard Worker TEST(LazyCallGraphTest, IncomingEdgeInsertion) {
580*9880d681SAndroid Build Coastguard Worker   LLVMContext Context;
581*9880d681SAndroid Build Coastguard Worker   // We want to ensure we can add edges even across complex diamond graphs, so
582*9880d681SAndroid Build Coastguard Worker   // we use the diamond of triangles graph defined above. The ascii diagram is
583*9880d681SAndroid Build Coastguard Worker   // repeated here for easy reference.
584*9880d681SAndroid Build Coastguard Worker   //
585*9880d681SAndroid Build Coastguard Worker   //         d1       |
586*9880d681SAndroid Build Coastguard Worker   //        /  \      |
587*9880d681SAndroid Build Coastguard Worker   //       d3--d2     |
588*9880d681SAndroid Build Coastguard Worker   //      /     \     |
589*9880d681SAndroid Build Coastguard Worker   //     b1     c1    |
590*9880d681SAndroid Build Coastguard Worker   //   /  \    /  \   |
591*9880d681SAndroid Build Coastguard Worker   //  b3--b2  c3--c2  |
592*9880d681SAndroid Build Coastguard Worker   //       \  /       |
593*9880d681SAndroid Build Coastguard Worker   //        a1        |
594*9880d681SAndroid Build Coastguard Worker   //       /  \       |
595*9880d681SAndroid Build Coastguard Worker   //      a3--a2      |
596*9880d681SAndroid Build Coastguard Worker   //
597*9880d681SAndroid Build Coastguard Worker   std::unique_ptr<Module> M = parseAssembly(Context, DiamondOfTriangles);
598*9880d681SAndroid Build Coastguard Worker   LazyCallGraph CG(*M);
599*9880d681SAndroid Build Coastguard Worker 
600*9880d681SAndroid Build Coastguard Worker   // Force the graph to be fully expanded.
601*9880d681SAndroid Build Coastguard Worker   for (LazyCallGraph::RefSCC &RC : CG.postorder_ref_sccs())
602*9880d681SAndroid Build Coastguard Worker     (void)RC;
603*9880d681SAndroid Build Coastguard Worker 
604*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &A1 = *CG.lookup(lookupFunction(*M, "a1"));
605*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &A2 = *CG.lookup(lookupFunction(*M, "a2"));
606*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &A3 = *CG.lookup(lookupFunction(*M, "a3"));
607*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &B1 = *CG.lookup(lookupFunction(*M, "b1"));
608*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &B2 = *CG.lookup(lookupFunction(*M, "b2"));
609*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &B3 = *CG.lookup(lookupFunction(*M, "b3"));
610*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &C1 = *CG.lookup(lookupFunction(*M, "c1"));
611*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &C2 = *CG.lookup(lookupFunction(*M, "c2"));
612*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &C3 = *CG.lookup(lookupFunction(*M, "c3"));
613*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &D1 = *CG.lookup(lookupFunction(*M, "d1"));
614*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &D2 = *CG.lookup(lookupFunction(*M, "d2"));
615*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &D3 = *CG.lookup(lookupFunction(*M, "d3"));
616*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::RefSCC &ARC = *CG.lookupRefSCC(A1);
617*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::RefSCC &BRC = *CG.lookupRefSCC(B1);
618*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::RefSCC &CRC = *CG.lookupRefSCC(C1);
619*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::RefSCC &DRC = *CG.lookupRefSCC(D1);
620*9880d681SAndroid Build Coastguard Worker   ASSERT_EQ(&ARC, CG.lookupRefSCC(A2));
621*9880d681SAndroid Build Coastguard Worker   ASSERT_EQ(&ARC, CG.lookupRefSCC(A3));
622*9880d681SAndroid Build Coastguard Worker   ASSERT_EQ(&BRC, CG.lookupRefSCC(B2));
623*9880d681SAndroid Build Coastguard Worker   ASSERT_EQ(&BRC, CG.lookupRefSCC(B3));
624*9880d681SAndroid Build Coastguard Worker   ASSERT_EQ(&CRC, CG.lookupRefSCC(C2));
625*9880d681SAndroid Build Coastguard Worker   ASSERT_EQ(&CRC, CG.lookupRefSCC(C3));
626*9880d681SAndroid Build Coastguard Worker   ASSERT_EQ(&DRC, CG.lookupRefSCC(D2));
627*9880d681SAndroid Build Coastguard Worker   ASSERT_EQ(&DRC, CG.lookupRefSCC(D3));
628*9880d681SAndroid Build Coastguard Worker   ASSERT_EQ(1, std::distance(D2.begin(), D2.end()));
629*9880d681SAndroid Build Coastguard Worker 
630*9880d681SAndroid Build Coastguard Worker   // Add an edge to make the graph:
631*9880d681SAndroid Build Coastguard Worker   //
632*9880d681SAndroid Build Coastguard Worker   //         d1         |
633*9880d681SAndroid Build Coastguard Worker   //        /  \        |
634*9880d681SAndroid Build Coastguard Worker   //       d3--d2---.   |
635*9880d681SAndroid Build Coastguard Worker   //      /     \    |  |
636*9880d681SAndroid Build Coastguard Worker   //     b1     c1   |  |
637*9880d681SAndroid Build Coastguard Worker   //   /  \    /  \ /   |
638*9880d681SAndroid Build Coastguard Worker   //  b3--b2  c3--c2    |
639*9880d681SAndroid Build Coastguard Worker   //       \  /         |
640*9880d681SAndroid Build Coastguard Worker   //        a1          |
641*9880d681SAndroid Build Coastguard Worker   //       /  \         |
642*9880d681SAndroid Build Coastguard Worker   //      a3--a2        |
643*9880d681SAndroid Build Coastguard Worker   auto MergedRCs = CRC.insertIncomingRefEdge(D2, C2);
644*9880d681SAndroid Build Coastguard Worker   // Make sure we connected the nodes.
645*9880d681SAndroid Build Coastguard Worker   for (LazyCallGraph::Edge E : D2) {
646*9880d681SAndroid Build Coastguard Worker     if (E.getNode() == &D3)
647*9880d681SAndroid Build Coastguard Worker       continue;
648*9880d681SAndroid Build Coastguard Worker     EXPECT_EQ(&C2, E.getNode());
649*9880d681SAndroid Build Coastguard Worker   }
650*9880d681SAndroid Build Coastguard Worker   // And marked the D ref-SCC as no longer valid.
651*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(1u, MergedRCs.size());
652*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&DRC, MergedRCs[0]);
653*9880d681SAndroid Build Coastguard Worker 
654*9880d681SAndroid Build Coastguard Worker   // Make sure we have the correct nodes in the SCC sets.
655*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&ARC, CG.lookupRefSCC(A1));
656*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&ARC, CG.lookupRefSCC(A2));
657*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&ARC, CG.lookupRefSCC(A3));
658*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&BRC, CG.lookupRefSCC(B1));
659*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&BRC, CG.lookupRefSCC(B2));
660*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&BRC, CG.lookupRefSCC(B3));
661*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&CRC, CG.lookupRefSCC(C1));
662*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&CRC, CG.lookupRefSCC(C2));
663*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&CRC, CG.lookupRefSCC(C3));
664*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&CRC, CG.lookupRefSCC(D1));
665*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&CRC, CG.lookupRefSCC(D2));
666*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&CRC, CG.lookupRefSCC(D3));
667*9880d681SAndroid Build Coastguard Worker 
668*9880d681SAndroid Build Coastguard Worker   // And that ancestry tests have been updated.
669*9880d681SAndroid Build Coastguard Worker   EXPECT_TRUE(ARC.isParentOf(CRC));
670*9880d681SAndroid Build Coastguard Worker   EXPECT_TRUE(BRC.isParentOf(CRC));
671*9880d681SAndroid Build Coastguard Worker }
672*9880d681SAndroid Build Coastguard Worker 
TEST(LazyCallGraphTest,IncomingEdgeInsertionMidTraversal)673*9880d681SAndroid Build Coastguard Worker TEST(LazyCallGraphTest, IncomingEdgeInsertionMidTraversal) {
674*9880d681SAndroid Build Coastguard Worker   LLVMContext Context;
675*9880d681SAndroid Build Coastguard Worker   // This is the same fundamental test as the previous, but we perform it
676*9880d681SAndroid Build Coastguard Worker   // having only partially walked the RefSCCs of the graph.
677*9880d681SAndroid Build Coastguard Worker   std::unique_ptr<Module> M = parseAssembly(Context, DiamondOfTriangles);
678*9880d681SAndroid Build Coastguard Worker   LazyCallGraph CG(*M);
679*9880d681SAndroid Build Coastguard Worker 
680*9880d681SAndroid Build Coastguard Worker   // Walk the RefSCCs until we find the one containing 'c1'.
681*9880d681SAndroid Build Coastguard Worker   auto I = CG.postorder_ref_scc_begin(), E = CG.postorder_ref_scc_end();
682*9880d681SAndroid Build Coastguard Worker   ASSERT_NE(I, E);
683*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::RefSCC &DRC = *I;
684*9880d681SAndroid Build Coastguard Worker   ASSERT_NE(&DRC, nullptr);
685*9880d681SAndroid Build Coastguard Worker   ++I;
686*9880d681SAndroid Build Coastguard Worker   ASSERT_NE(I, E);
687*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::RefSCC &CRC = *I;
688*9880d681SAndroid Build Coastguard Worker   ASSERT_NE(&CRC, nullptr);
689*9880d681SAndroid Build Coastguard Worker 
690*9880d681SAndroid Build Coastguard Worker   ASSERT_EQ(nullptr, CG.lookup(lookupFunction(*M, "a1")));
691*9880d681SAndroid Build Coastguard Worker   ASSERT_EQ(nullptr, CG.lookup(lookupFunction(*M, "a2")));
692*9880d681SAndroid Build Coastguard Worker   ASSERT_EQ(nullptr, CG.lookup(lookupFunction(*M, "a3")));
693*9880d681SAndroid Build Coastguard Worker   ASSERT_EQ(nullptr, CG.lookup(lookupFunction(*M, "b1")));
694*9880d681SAndroid Build Coastguard Worker   ASSERT_EQ(nullptr, CG.lookup(lookupFunction(*M, "b2")));
695*9880d681SAndroid Build Coastguard Worker   ASSERT_EQ(nullptr, CG.lookup(lookupFunction(*M, "b3")));
696*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &C1 = *CG.lookup(lookupFunction(*M, "c1"));
697*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &C2 = *CG.lookup(lookupFunction(*M, "c2"));
698*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &C3 = *CG.lookup(lookupFunction(*M, "c3"));
699*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &D1 = *CG.lookup(lookupFunction(*M, "d1"));
700*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &D2 = *CG.lookup(lookupFunction(*M, "d2"));
701*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &D3 = *CG.lookup(lookupFunction(*M, "d3"));
702*9880d681SAndroid Build Coastguard Worker   ASSERT_EQ(&CRC, CG.lookupRefSCC(C1));
703*9880d681SAndroid Build Coastguard Worker   ASSERT_EQ(&CRC, CG.lookupRefSCC(C2));
704*9880d681SAndroid Build Coastguard Worker   ASSERT_EQ(&CRC, CG.lookupRefSCC(C3));
705*9880d681SAndroid Build Coastguard Worker   ASSERT_EQ(&DRC, CG.lookupRefSCC(D1));
706*9880d681SAndroid Build Coastguard Worker   ASSERT_EQ(&DRC, CG.lookupRefSCC(D2));
707*9880d681SAndroid Build Coastguard Worker   ASSERT_EQ(&DRC, CG.lookupRefSCC(D3));
708*9880d681SAndroid Build Coastguard Worker   ASSERT_EQ(1, std::distance(D2.begin(), D2.end()));
709*9880d681SAndroid Build Coastguard Worker 
710*9880d681SAndroid Build Coastguard Worker   auto MergedRCs = CRC.insertIncomingRefEdge(D2, C2);
711*9880d681SAndroid Build Coastguard Worker   // Make sure we connected the nodes.
712*9880d681SAndroid Build Coastguard Worker   for (LazyCallGraph::Edge E : D2) {
713*9880d681SAndroid Build Coastguard Worker     if (E.getNode() == &D3)
714*9880d681SAndroid Build Coastguard Worker       continue;
715*9880d681SAndroid Build Coastguard Worker     EXPECT_EQ(&C2, E.getNode());
716*9880d681SAndroid Build Coastguard Worker   }
717*9880d681SAndroid Build Coastguard Worker   // And marked the D ref-SCC as no longer valid.
718*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(1u, MergedRCs.size());
719*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&DRC, MergedRCs[0]);
720*9880d681SAndroid Build Coastguard Worker 
721*9880d681SAndroid Build Coastguard Worker   // Make sure we have the correct nodes in the RefSCCs.
722*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&CRC, CG.lookupRefSCC(C1));
723*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&CRC, CG.lookupRefSCC(C2));
724*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&CRC, CG.lookupRefSCC(C3));
725*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&CRC, CG.lookupRefSCC(D1));
726*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&CRC, CG.lookupRefSCC(D2));
727*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&CRC, CG.lookupRefSCC(D3));
728*9880d681SAndroid Build Coastguard Worker 
729*9880d681SAndroid Build Coastguard Worker   // Check that we can form the last two RefSCCs now in a coherent way.
730*9880d681SAndroid Build Coastguard Worker   ++I;
731*9880d681SAndroid Build Coastguard Worker   EXPECT_NE(I, E);
732*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::RefSCC &BRC = *I;
733*9880d681SAndroid Build Coastguard Worker   EXPECT_NE(&BRC, nullptr);
734*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&BRC, CG.lookupRefSCC(*CG.lookup(lookupFunction(*M, "b1"))));
735*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&BRC, CG.lookupRefSCC(*CG.lookup(lookupFunction(*M, "b2"))));
736*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&BRC, CG.lookupRefSCC(*CG.lookup(lookupFunction(*M, "b3"))));
737*9880d681SAndroid Build Coastguard Worker   EXPECT_TRUE(BRC.isParentOf(CRC));
738*9880d681SAndroid Build Coastguard Worker   ++I;
739*9880d681SAndroid Build Coastguard Worker   EXPECT_NE(I, E);
740*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::RefSCC &ARC = *I;
741*9880d681SAndroid Build Coastguard Worker   EXPECT_NE(&ARC, nullptr);
742*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&ARC, CG.lookupRefSCC(*CG.lookup(lookupFunction(*M, "a1"))));
743*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&ARC, CG.lookupRefSCC(*CG.lookup(lookupFunction(*M, "a2"))));
744*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&ARC, CG.lookupRefSCC(*CG.lookup(lookupFunction(*M, "a3"))));
745*9880d681SAndroid Build Coastguard Worker   EXPECT_TRUE(ARC.isParentOf(CRC));
746*9880d681SAndroid Build Coastguard Worker   ++I;
747*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(E, I);
748*9880d681SAndroid Build Coastguard Worker }
749*9880d681SAndroid Build Coastguard Worker 
TEST(LazyCallGraphTest,InternalEdgeMutation)750*9880d681SAndroid Build Coastguard Worker TEST(LazyCallGraphTest, InternalEdgeMutation) {
751*9880d681SAndroid Build Coastguard Worker   LLVMContext Context;
752*9880d681SAndroid Build Coastguard Worker   std::unique_ptr<Module> M = parseAssembly(Context, "define void @a() {\n"
753*9880d681SAndroid Build Coastguard Worker                                                      "entry:\n"
754*9880d681SAndroid Build Coastguard Worker                                                      "  call void @b()\n"
755*9880d681SAndroid Build Coastguard Worker                                                      "  ret void\n"
756*9880d681SAndroid Build Coastguard Worker                                                      "}\n"
757*9880d681SAndroid Build Coastguard Worker                                                      "define void @b() {\n"
758*9880d681SAndroid Build Coastguard Worker                                                      "entry:\n"
759*9880d681SAndroid Build Coastguard Worker                                                      "  call void @c()\n"
760*9880d681SAndroid Build Coastguard Worker                                                      "  ret void\n"
761*9880d681SAndroid Build Coastguard Worker                                                      "}\n"
762*9880d681SAndroid Build Coastguard Worker                                                      "define void @c() {\n"
763*9880d681SAndroid Build Coastguard Worker                                                      "entry:\n"
764*9880d681SAndroid Build Coastguard Worker                                                      "  call void @a()\n"
765*9880d681SAndroid Build Coastguard Worker                                                      "  ret void\n"
766*9880d681SAndroid Build Coastguard Worker                                                      "}\n");
767*9880d681SAndroid Build Coastguard Worker   LazyCallGraph CG(*M);
768*9880d681SAndroid Build Coastguard Worker 
769*9880d681SAndroid Build Coastguard Worker   // Force the graph to be fully expanded.
770*9880d681SAndroid Build Coastguard Worker   auto I = CG.postorder_ref_scc_begin();
771*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::RefSCC &RC = *I++;
772*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(CG.postorder_ref_scc_end(), I);
773*9880d681SAndroid Build Coastguard Worker 
774*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &A = *CG.lookup(lookupFunction(*M, "a"));
775*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &B = *CG.lookup(lookupFunction(*M, "b"));
776*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &C = *CG.lookup(lookupFunction(*M, "c"));
777*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&RC, CG.lookupRefSCC(A));
778*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&RC, CG.lookupRefSCC(B));
779*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&RC, CG.lookupRefSCC(C));
780*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(1, RC.size());
781*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&*RC.begin(), CG.lookupSCC(A));
782*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&*RC.begin(), CG.lookupSCC(B));
783*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&*RC.begin(), CG.lookupSCC(C));
784*9880d681SAndroid Build Coastguard Worker 
785*9880d681SAndroid Build Coastguard Worker   // Insert an edge from 'a' to 'c'. Nothing changes about the graph.
786*9880d681SAndroid Build Coastguard Worker   RC.insertInternalRefEdge(A, C);
787*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(2, std::distance(A.begin(), A.end()));
788*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&RC, CG.lookupRefSCC(A));
789*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&RC, CG.lookupRefSCC(B));
790*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&RC, CG.lookupRefSCC(C));
791*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(1, RC.size());
792*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&*RC.begin(), CG.lookupSCC(A));
793*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&*RC.begin(), CG.lookupSCC(B));
794*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&*RC.begin(), CG.lookupSCC(C));
795*9880d681SAndroid Build Coastguard Worker 
796*9880d681SAndroid Build Coastguard Worker   // Switch the call edge from 'b' to 'c' to a ref edge. This will break the
797*9880d681SAndroid Build Coastguard Worker   // call cycle and cause us to form more SCCs. The RefSCC will remain the same
798*9880d681SAndroid Build Coastguard Worker   // though.
799*9880d681SAndroid Build Coastguard Worker   RC.switchInternalEdgeToRef(B, C);
800*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&RC, CG.lookupRefSCC(A));
801*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&RC, CG.lookupRefSCC(B));
802*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&RC, CG.lookupRefSCC(C));
803*9880d681SAndroid Build Coastguard Worker   auto J = RC.begin();
804*9880d681SAndroid Build Coastguard Worker   // The SCCs must be in *post-order* which means successors before
805*9880d681SAndroid Build Coastguard Worker   // predecessors. At this point we have call edges from C to A and from A to
806*9880d681SAndroid Build Coastguard Worker   // B. The only valid postorder is B, A, C.
807*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&*J++, CG.lookupSCC(B));
808*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&*J++, CG.lookupSCC(A));
809*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&*J++, CG.lookupSCC(C));
810*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(RC.end(), J);
811*9880d681SAndroid Build Coastguard Worker 
812*9880d681SAndroid Build Coastguard Worker   // Test turning the ref edge from A to C into a call edge. This will form an
813*9880d681SAndroid Build Coastguard Worker   // SCC out of A and C. Since we previously had a call edge from C to A, the
814*9880d681SAndroid Build Coastguard Worker   // C SCC should be preserved and have A merged into it while the A SCC should
815*9880d681SAndroid Build Coastguard Worker   // be invalidated.
816*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::SCC &AC = *CG.lookupSCC(A);
817*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::SCC &CC = *CG.lookupSCC(C);
818*9880d681SAndroid Build Coastguard Worker   auto InvalidatedSCCs = RC.switchInternalEdgeToCall(A, C);
819*9880d681SAndroid Build Coastguard Worker   ASSERT_EQ(1u, InvalidatedSCCs.size());
820*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&AC, InvalidatedSCCs[0]);
821*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(2, CC.size());
822*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&CC, CG.lookupSCC(A));
823*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&CC, CG.lookupSCC(C));
824*9880d681SAndroid Build Coastguard Worker   J = RC.begin();
825*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&*J++, CG.lookupSCC(B));
826*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&*J++, CG.lookupSCC(C));
827*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(RC.end(), J);
828*9880d681SAndroid Build Coastguard Worker }
829*9880d681SAndroid Build Coastguard Worker 
TEST(LazyCallGraphTest,InternalEdgeRemoval)830*9880d681SAndroid Build Coastguard Worker TEST(LazyCallGraphTest, InternalEdgeRemoval) {
831*9880d681SAndroid Build Coastguard Worker   LLVMContext Context;
832*9880d681SAndroid Build Coastguard Worker   // A nice fully connected (including self-edges) RefSCC.
833*9880d681SAndroid Build Coastguard Worker   std::unique_ptr<Module> M = parseAssembly(
834*9880d681SAndroid Build Coastguard Worker       Context, "define void @a(i8** %ptr) {\n"
835*9880d681SAndroid Build Coastguard Worker                "entry:\n"
836*9880d681SAndroid Build Coastguard Worker                "  store i8* bitcast (void(i8**)* @a to i8*), i8** %ptr\n"
837*9880d681SAndroid Build Coastguard Worker                "  store i8* bitcast (void(i8**)* @b to i8*), i8** %ptr\n"
838*9880d681SAndroid Build Coastguard Worker                "  store i8* bitcast (void(i8**)* @c to i8*), i8** %ptr\n"
839*9880d681SAndroid Build Coastguard Worker                "  ret void\n"
840*9880d681SAndroid Build Coastguard Worker                "}\n"
841*9880d681SAndroid Build Coastguard Worker                "define void @b(i8** %ptr) {\n"
842*9880d681SAndroid Build Coastguard Worker                "entry:\n"
843*9880d681SAndroid Build Coastguard Worker                "  store i8* bitcast (void(i8**)* @a to i8*), i8** %ptr\n"
844*9880d681SAndroid Build Coastguard Worker                "  store i8* bitcast (void(i8**)* @b to i8*), i8** %ptr\n"
845*9880d681SAndroid Build Coastguard Worker                "  store i8* bitcast (void(i8**)* @c to i8*), i8** %ptr\n"
846*9880d681SAndroid Build Coastguard Worker                "  ret void\n"
847*9880d681SAndroid Build Coastguard Worker                "}\n"
848*9880d681SAndroid Build Coastguard Worker                "define void @c(i8** %ptr) {\n"
849*9880d681SAndroid Build Coastguard Worker                "entry:\n"
850*9880d681SAndroid Build Coastguard Worker                "  store i8* bitcast (void(i8**)* @a to i8*), i8** %ptr\n"
851*9880d681SAndroid Build Coastguard Worker                "  store i8* bitcast (void(i8**)* @b to i8*), i8** %ptr\n"
852*9880d681SAndroid Build Coastguard Worker                "  store i8* bitcast (void(i8**)* @c to i8*), i8** %ptr\n"
853*9880d681SAndroid Build Coastguard Worker                "  ret void\n"
854*9880d681SAndroid Build Coastguard Worker                "}\n");
855*9880d681SAndroid Build Coastguard Worker   LazyCallGraph CG(*M);
856*9880d681SAndroid Build Coastguard Worker 
857*9880d681SAndroid Build Coastguard Worker   // Force the graph to be fully expanded.
858*9880d681SAndroid Build Coastguard Worker   auto I = CG.postorder_ref_scc_begin();
859*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::RefSCC &RC = *I++;
860*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(CG.postorder_ref_scc_end(), I);
861*9880d681SAndroid Build Coastguard Worker 
862*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &A = *CG.lookup(lookupFunction(*M, "a"));
863*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &B = *CG.lookup(lookupFunction(*M, "b"));
864*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &C = *CG.lookup(lookupFunction(*M, "c"));
865*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&RC, CG.lookupRefSCC(A));
866*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&RC, CG.lookupRefSCC(B));
867*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&RC, CG.lookupRefSCC(C));
868*9880d681SAndroid Build Coastguard Worker 
869*9880d681SAndroid Build Coastguard Worker   // Remove the edge from b -> a, which should leave the 3 functions still in
870*9880d681SAndroid Build Coastguard Worker   // a single connected component because of a -> b -> c -> a.
871*9880d681SAndroid Build Coastguard Worker   SmallVector<LazyCallGraph::RefSCC *, 1> NewRCs =
872*9880d681SAndroid Build Coastguard Worker       RC.removeInternalRefEdge(B, A);
873*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(0u, NewRCs.size());
874*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&RC, CG.lookupRefSCC(A));
875*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&RC, CG.lookupRefSCC(B));
876*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&RC, CG.lookupRefSCC(C));
877*9880d681SAndroid Build Coastguard Worker 
878*9880d681SAndroid Build Coastguard Worker   // Remove the edge from c -> a, which should leave 'a' in the original RefSCC
879*9880d681SAndroid Build Coastguard Worker   // and form a new RefSCC for 'b' and 'c'.
880*9880d681SAndroid Build Coastguard Worker   NewRCs = RC.removeInternalRefEdge(C, A);
881*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(1u, NewRCs.size());
882*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&RC, CG.lookupRefSCC(A));
883*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(1, std::distance(RC.begin(), RC.end()));
884*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::RefSCC *RC2 = CG.lookupRefSCC(B);
885*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(RC2, CG.lookupRefSCC(C));
886*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(RC2, NewRCs[0]);
887*9880d681SAndroid Build Coastguard Worker }
888*9880d681SAndroid Build Coastguard Worker 
TEST(LazyCallGraphTest,InternalCallEdgeToRef)889*9880d681SAndroid Build Coastguard Worker TEST(LazyCallGraphTest, InternalCallEdgeToRef) {
890*9880d681SAndroid Build Coastguard Worker   LLVMContext Context;
891*9880d681SAndroid Build Coastguard Worker   // A nice fully connected (including self-edges) SCC (and RefSCC)
892*9880d681SAndroid Build Coastguard Worker   std::unique_ptr<Module> M = parseAssembly(Context, "define void @a() {\n"
893*9880d681SAndroid Build Coastguard Worker                                                      "entry:\n"
894*9880d681SAndroid Build Coastguard Worker                                                      "  call void @a()\n"
895*9880d681SAndroid Build Coastguard Worker                                                      "  call void @b()\n"
896*9880d681SAndroid Build Coastguard Worker                                                      "  call void @c()\n"
897*9880d681SAndroid Build Coastguard Worker                                                      "  ret void\n"
898*9880d681SAndroid Build Coastguard Worker                                                      "}\n"
899*9880d681SAndroid Build Coastguard Worker                                                      "define void @b() {\n"
900*9880d681SAndroid Build Coastguard Worker                                                      "entry:\n"
901*9880d681SAndroid Build Coastguard Worker                                                      "  call void @a()\n"
902*9880d681SAndroid Build Coastguard Worker                                                      "  call void @b()\n"
903*9880d681SAndroid Build Coastguard Worker                                                      "  call void @c()\n"
904*9880d681SAndroid Build Coastguard Worker                                                      "  ret void\n"
905*9880d681SAndroid Build Coastguard Worker                                                      "}\n"
906*9880d681SAndroid Build Coastguard Worker                                                      "define void @c() {\n"
907*9880d681SAndroid Build Coastguard Worker                                                      "entry:\n"
908*9880d681SAndroid Build Coastguard Worker                                                      "  call void @a()\n"
909*9880d681SAndroid Build Coastguard Worker                                                      "  call void @b()\n"
910*9880d681SAndroid Build Coastguard Worker                                                      "  call void @c()\n"
911*9880d681SAndroid Build Coastguard Worker                                                      "  ret void\n"
912*9880d681SAndroid Build Coastguard Worker                                                      "}\n");
913*9880d681SAndroid Build Coastguard Worker   LazyCallGraph CG(*M);
914*9880d681SAndroid Build Coastguard Worker 
915*9880d681SAndroid Build Coastguard Worker   // Force the graph to be fully expanded.
916*9880d681SAndroid Build Coastguard Worker   auto I = CG.postorder_ref_scc_begin();
917*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::RefSCC &RC = *I++;
918*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(CG.postorder_ref_scc_end(), I);
919*9880d681SAndroid Build Coastguard Worker 
920*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(1, RC.size());
921*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::SCC &CallC = *RC.begin();
922*9880d681SAndroid Build Coastguard Worker 
923*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &A = *CG.lookup(lookupFunction(*M, "a"));
924*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &B = *CG.lookup(lookupFunction(*M, "b"));
925*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &C = *CG.lookup(lookupFunction(*M, "c"));
926*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&CallC, CG.lookupSCC(A));
927*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&CallC, CG.lookupSCC(B));
928*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&CallC, CG.lookupSCC(C));
929*9880d681SAndroid Build Coastguard Worker 
930*9880d681SAndroid Build Coastguard Worker   // Remove the call edge from b -> a to a ref edge, which should leave the
931*9880d681SAndroid Build Coastguard Worker   // 3 functions still in a single connected component because of a -> b ->
932*9880d681SAndroid Build Coastguard Worker   // c -> a.
933*9880d681SAndroid Build Coastguard Worker   RC.switchInternalEdgeToRef(B, A);
934*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(1, RC.size());
935*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&CallC, CG.lookupSCC(A));
936*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&CallC, CG.lookupSCC(B));
937*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&CallC, CG.lookupSCC(C));
938*9880d681SAndroid Build Coastguard Worker 
939*9880d681SAndroid Build Coastguard Worker   // Remove the edge from c -> a, which should leave 'a' in the original SCC
940*9880d681SAndroid Build Coastguard Worker   // and form a new SCC for 'b' and 'c'.
941*9880d681SAndroid Build Coastguard Worker   RC.switchInternalEdgeToRef(C, A);
942*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(2, RC.size());
943*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&CallC, CG.lookupSCC(A));
944*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::SCC &BCallC = *CG.lookupSCC(B);
945*9880d681SAndroid Build Coastguard Worker   EXPECT_NE(&BCallC, &CallC);
946*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&BCallC, CG.lookupSCC(C));
947*9880d681SAndroid Build Coastguard Worker   auto J = RC.find(CallC);
948*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&CallC, &*J);
949*9880d681SAndroid Build Coastguard Worker   --J;
950*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&BCallC, &*J);
951*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(RC.begin(), J);
952*9880d681SAndroid Build Coastguard Worker 
953*9880d681SAndroid Build Coastguard Worker   // Remove the edge from c -> b, which should leave 'b' in the original SCC
954*9880d681SAndroid Build Coastguard Worker   // and form a new SCC for 'c'. It shouldn't change 'a's SCC.
955*9880d681SAndroid Build Coastguard Worker   RC.switchInternalEdgeToRef(C, B);
956*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(3, RC.size());
957*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&CallC, CG.lookupSCC(A));
958*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&BCallC, CG.lookupSCC(B));
959*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::SCC &CCallC = *CG.lookupSCC(C);
960*9880d681SAndroid Build Coastguard Worker   EXPECT_NE(&CCallC, &CallC);
961*9880d681SAndroid Build Coastguard Worker   EXPECT_NE(&CCallC, &BCallC);
962*9880d681SAndroid Build Coastguard Worker   J = RC.find(CallC);
963*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&CallC, &*J);
964*9880d681SAndroid Build Coastguard Worker   --J;
965*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&BCallC, &*J);
966*9880d681SAndroid Build Coastguard Worker   --J;
967*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&CCallC, &*J);
968*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(RC.begin(), J);
969*9880d681SAndroid Build Coastguard Worker }
970*9880d681SAndroid Build Coastguard Worker 
TEST(LazyCallGraphTest,InternalRefEdgeToCall)971*9880d681SAndroid Build Coastguard Worker TEST(LazyCallGraphTest, InternalRefEdgeToCall) {
972*9880d681SAndroid Build Coastguard Worker   LLVMContext Context;
973*9880d681SAndroid Build Coastguard Worker   // Basic tests for making a ref edge a call. This hits the basics of the
974*9880d681SAndroid Build Coastguard Worker   // process only.
975*9880d681SAndroid Build Coastguard Worker   std::unique_ptr<Module> M =
976*9880d681SAndroid Build Coastguard Worker       parseAssembly(Context, "define void @a() {\n"
977*9880d681SAndroid Build Coastguard Worker                              "entry:\n"
978*9880d681SAndroid Build Coastguard Worker                              "  call void @b()\n"
979*9880d681SAndroid Build Coastguard Worker                              "  call void @c()\n"
980*9880d681SAndroid Build Coastguard Worker                              "  store void()* @d, void()** undef\n"
981*9880d681SAndroid Build Coastguard Worker                              "  ret void\n"
982*9880d681SAndroid Build Coastguard Worker                              "}\n"
983*9880d681SAndroid Build Coastguard Worker                              "define void @b() {\n"
984*9880d681SAndroid Build Coastguard Worker                              "entry:\n"
985*9880d681SAndroid Build Coastguard Worker                              "  store void()* @c, void()** undef\n"
986*9880d681SAndroid Build Coastguard Worker                              "  call void @d()\n"
987*9880d681SAndroid Build Coastguard Worker                              "  ret void\n"
988*9880d681SAndroid Build Coastguard Worker                              "}\n"
989*9880d681SAndroid Build Coastguard Worker                              "define void @c() {\n"
990*9880d681SAndroid Build Coastguard Worker                              "entry:\n"
991*9880d681SAndroid Build Coastguard Worker                              "  store void()* @b, void()** undef\n"
992*9880d681SAndroid Build Coastguard Worker                              "  call void @d()\n"
993*9880d681SAndroid Build Coastguard Worker                              "  ret void\n"
994*9880d681SAndroid Build Coastguard Worker                              "}\n"
995*9880d681SAndroid Build Coastguard Worker                              "define void @d() {\n"
996*9880d681SAndroid Build Coastguard Worker                              "entry:\n"
997*9880d681SAndroid Build Coastguard Worker                              "  store void()* @a, void()** undef\n"
998*9880d681SAndroid Build Coastguard Worker                              "  ret void\n"
999*9880d681SAndroid Build Coastguard Worker                              "}\n");
1000*9880d681SAndroid Build Coastguard Worker   LazyCallGraph CG(*M);
1001*9880d681SAndroid Build Coastguard Worker 
1002*9880d681SAndroid Build Coastguard Worker   // Force the graph to be fully expanded.
1003*9880d681SAndroid Build Coastguard Worker   auto I = CG.postorder_ref_scc_begin();
1004*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::RefSCC &RC = *I++;
1005*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(CG.postorder_ref_scc_end(), I);
1006*9880d681SAndroid Build Coastguard Worker 
1007*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &A = *CG.lookup(lookupFunction(*M, "a"));
1008*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &B = *CG.lookup(lookupFunction(*M, "b"));
1009*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &C = *CG.lookup(lookupFunction(*M, "c"));
1010*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &D = *CG.lookup(lookupFunction(*M, "d"));
1011*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::SCC &AC = *CG.lookupSCC(A);
1012*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::SCC &BC = *CG.lookupSCC(B);
1013*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::SCC &CC = *CG.lookupSCC(C);
1014*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::SCC &DC = *CG.lookupSCC(D);
1015*9880d681SAndroid Build Coastguard Worker 
1016*9880d681SAndroid Build Coastguard Worker   // Check the initial post-order. Note that B and C could be flipped here (and
1017*9880d681SAndroid Build Coastguard Worker   // in our mutation) without changing the nature of this test.
1018*9880d681SAndroid Build Coastguard Worker   ASSERT_EQ(4, RC.size());
1019*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&DC, &RC[0]);
1020*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&BC, &RC[1]);
1021*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&CC, &RC[2]);
1022*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&AC, &RC[3]);
1023*9880d681SAndroid Build Coastguard Worker 
1024*9880d681SAndroid Build Coastguard Worker   // Switch the ref edge from A -> D to a call edge. This should have no
1025*9880d681SAndroid Build Coastguard Worker   // effect as it is already in postorder and no new cycles are formed.
1026*9880d681SAndroid Build Coastguard Worker   auto MergedCs = RC.switchInternalEdgeToCall(A, D);
1027*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(0u, MergedCs.size());
1028*9880d681SAndroid Build Coastguard Worker   ASSERT_EQ(4, RC.size());
1029*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&DC, &RC[0]);
1030*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&BC, &RC[1]);
1031*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&CC, &RC[2]);
1032*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&AC, &RC[3]);
1033*9880d681SAndroid Build Coastguard Worker 
1034*9880d681SAndroid Build Coastguard Worker   // Switch B -> C to a call edge. This doesn't form any new cycles but does
1035*9880d681SAndroid Build Coastguard Worker   // require reordering the SCCs.
1036*9880d681SAndroid Build Coastguard Worker   MergedCs = RC.switchInternalEdgeToCall(B, C);
1037*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(0u, MergedCs.size());
1038*9880d681SAndroid Build Coastguard Worker   ASSERT_EQ(4, RC.size());
1039*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&DC, &RC[0]);
1040*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&CC, &RC[1]);
1041*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&BC, &RC[2]);
1042*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&AC, &RC[3]);
1043*9880d681SAndroid Build Coastguard Worker 
1044*9880d681SAndroid Build Coastguard Worker   // Switch C -> B to a call edge. This forms a cycle and forces merging SCCs.
1045*9880d681SAndroid Build Coastguard Worker   MergedCs = RC.switchInternalEdgeToCall(C, B);
1046*9880d681SAndroid Build Coastguard Worker   ASSERT_EQ(1u, MergedCs.size());
1047*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&CC, MergedCs[0]);
1048*9880d681SAndroid Build Coastguard Worker   ASSERT_EQ(3, RC.size());
1049*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&DC, &RC[0]);
1050*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&BC, &RC[1]);
1051*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&AC, &RC[2]);
1052*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(2, BC.size());
1053*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&BC, CG.lookupSCC(B));
1054*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&BC, CG.lookupSCC(C));
1055*9880d681SAndroid Build Coastguard Worker }
1056*9880d681SAndroid Build Coastguard Worker 
TEST(LazyCallGraphTest,InternalRefEdgeToCallNoCycleInterleaved)1057*9880d681SAndroid Build Coastguard Worker TEST(LazyCallGraphTest, InternalRefEdgeToCallNoCycleInterleaved) {
1058*9880d681SAndroid Build Coastguard Worker   LLVMContext Context;
1059*9880d681SAndroid Build Coastguard Worker   // Test for having a post-order prior to changing a ref edge to a call edge
1060*9880d681SAndroid Build Coastguard Worker   // with SCCs connecting to the source and connecting to the target, but not
1061*9880d681SAndroid Build Coastguard Worker   // connecting to both, interleaved between the source and target. This
1062*9880d681SAndroid Build Coastguard Worker   // ensures we correctly partition the range rather than simply moving one or
1063*9880d681SAndroid Build Coastguard Worker   // the other.
1064*9880d681SAndroid Build Coastguard Worker   std::unique_ptr<Module> M =
1065*9880d681SAndroid Build Coastguard Worker       parseAssembly(Context, "define void @a() {\n"
1066*9880d681SAndroid Build Coastguard Worker                              "entry:\n"
1067*9880d681SAndroid Build Coastguard Worker                              "  call void @b1()\n"
1068*9880d681SAndroid Build Coastguard Worker                              "  call void @c1()\n"
1069*9880d681SAndroid Build Coastguard Worker                              "  ret void\n"
1070*9880d681SAndroid Build Coastguard Worker                              "}\n"
1071*9880d681SAndroid Build Coastguard Worker                              "define void @b1() {\n"
1072*9880d681SAndroid Build Coastguard Worker                              "entry:\n"
1073*9880d681SAndroid Build Coastguard Worker                              "  call void @c1()\n"
1074*9880d681SAndroid Build Coastguard Worker                              "  call void @b2()\n"
1075*9880d681SAndroid Build Coastguard Worker                              "  ret void\n"
1076*9880d681SAndroid Build Coastguard Worker                              "}\n"
1077*9880d681SAndroid Build Coastguard Worker                              "define void @c1() {\n"
1078*9880d681SAndroid Build Coastguard Worker                              "entry:\n"
1079*9880d681SAndroid Build Coastguard Worker                              "  call void @b2()\n"
1080*9880d681SAndroid Build Coastguard Worker                              "  call void @c2()\n"
1081*9880d681SAndroid Build Coastguard Worker                              "  ret void\n"
1082*9880d681SAndroid Build Coastguard Worker                              "}\n"
1083*9880d681SAndroid Build Coastguard Worker                              "define void @b2() {\n"
1084*9880d681SAndroid Build Coastguard Worker                              "entry:\n"
1085*9880d681SAndroid Build Coastguard Worker                              "  call void @c2()\n"
1086*9880d681SAndroid Build Coastguard Worker                              "  call void @b3()\n"
1087*9880d681SAndroid Build Coastguard Worker                              "  ret void\n"
1088*9880d681SAndroid Build Coastguard Worker                              "}\n"
1089*9880d681SAndroid Build Coastguard Worker                              "define void @c2() {\n"
1090*9880d681SAndroid Build Coastguard Worker                              "entry:\n"
1091*9880d681SAndroid Build Coastguard Worker                              "  call void @b3()\n"
1092*9880d681SAndroid Build Coastguard Worker                              "  call void @c3()\n"
1093*9880d681SAndroid Build Coastguard Worker                              "  ret void\n"
1094*9880d681SAndroid Build Coastguard Worker                              "}\n"
1095*9880d681SAndroid Build Coastguard Worker                              "define void @b3() {\n"
1096*9880d681SAndroid Build Coastguard Worker                              "entry:\n"
1097*9880d681SAndroid Build Coastguard Worker                              "  call void @c3()\n"
1098*9880d681SAndroid Build Coastguard Worker                              "  call void @d()\n"
1099*9880d681SAndroid Build Coastguard Worker                              "  ret void\n"
1100*9880d681SAndroid Build Coastguard Worker                              "}\n"
1101*9880d681SAndroid Build Coastguard Worker                              "define void @c3() {\n"
1102*9880d681SAndroid Build Coastguard Worker                              "entry:\n"
1103*9880d681SAndroid Build Coastguard Worker                              "  store void()* @b1, void()** undef\n"
1104*9880d681SAndroid Build Coastguard Worker                              "  call void @d()\n"
1105*9880d681SAndroid Build Coastguard Worker                              "  ret void\n"
1106*9880d681SAndroid Build Coastguard Worker                              "}\n"
1107*9880d681SAndroid Build Coastguard Worker                              "define void @d() {\n"
1108*9880d681SAndroid Build Coastguard Worker                              "entry:\n"
1109*9880d681SAndroid Build Coastguard Worker                              "  store void()* @a, void()** undef\n"
1110*9880d681SAndroid Build Coastguard Worker                              "  ret void\n"
1111*9880d681SAndroid Build Coastguard Worker                              "}\n");
1112*9880d681SAndroid Build Coastguard Worker   LazyCallGraph CG(*M);
1113*9880d681SAndroid Build Coastguard Worker 
1114*9880d681SAndroid Build Coastguard Worker   // Force the graph to be fully expanded.
1115*9880d681SAndroid Build Coastguard Worker   auto I = CG.postorder_ref_scc_begin();
1116*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::RefSCC &RC = *I++;
1117*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(CG.postorder_ref_scc_end(), I);
1118*9880d681SAndroid Build Coastguard Worker 
1119*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &A = *CG.lookup(lookupFunction(*M, "a"));
1120*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &B1 = *CG.lookup(lookupFunction(*M, "b1"));
1121*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &B2 = *CG.lookup(lookupFunction(*M, "b2"));
1122*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &B3 = *CG.lookup(lookupFunction(*M, "b3"));
1123*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &C1 = *CG.lookup(lookupFunction(*M, "c1"));
1124*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &C2 = *CG.lookup(lookupFunction(*M, "c2"));
1125*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &C3 = *CG.lookup(lookupFunction(*M, "c3"));
1126*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &D = *CG.lookup(lookupFunction(*M, "d"));
1127*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::SCC &AC = *CG.lookupSCC(A);
1128*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::SCC &B1C = *CG.lookupSCC(B1);
1129*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::SCC &B2C = *CG.lookupSCC(B2);
1130*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::SCC &B3C = *CG.lookupSCC(B3);
1131*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::SCC &C1C = *CG.lookupSCC(C1);
1132*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::SCC &C2C = *CG.lookupSCC(C2);
1133*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::SCC &C3C = *CG.lookupSCC(C3);
1134*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::SCC &DC = *CG.lookupSCC(D);
1135*9880d681SAndroid Build Coastguard Worker 
1136*9880d681SAndroid Build Coastguard Worker   // Several call edges are initially present to force a particual post-order.
1137*9880d681SAndroid Build Coastguard Worker   // Remove them now, leaving an interleaved post-order pattern.
1138*9880d681SAndroid Build Coastguard Worker   RC.switchInternalEdgeToRef(B3, C3);
1139*9880d681SAndroid Build Coastguard Worker   RC.switchInternalEdgeToRef(C2, B3);
1140*9880d681SAndroid Build Coastguard Worker   RC.switchInternalEdgeToRef(B2, C2);
1141*9880d681SAndroid Build Coastguard Worker   RC.switchInternalEdgeToRef(C1, B2);
1142*9880d681SAndroid Build Coastguard Worker   RC.switchInternalEdgeToRef(B1, C1);
1143*9880d681SAndroid Build Coastguard Worker 
1144*9880d681SAndroid Build Coastguard Worker   // Check the initial post-order. We ensure this order with the extra edges
1145*9880d681SAndroid Build Coastguard Worker   // that are nuked above.
1146*9880d681SAndroid Build Coastguard Worker   ASSERT_EQ(8, RC.size());
1147*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&DC, &RC[0]);
1148*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&C3C, &RC[1]);
1149*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&B3C, &RC[2]);
1150*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&C2C, &RC[3]);
1151*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&B2C, &RC[4]);
1152*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&C1C, &RC[5]);
1153*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&B1C, &RC[6]);
1154*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&AC, &RC[7]);
1155*9880d681SAndroid Build Coastguard Worker 
1156*9880d681SAndroid Build Coastguard Worker   // Switch C3 -> B1 to a call edge. This doesn't form any new cycles but does
1157*9880d681SAndroid Build Coastguard Worker   // require reordering the SCCs in the face of tricky internal node
1158*9880d681SAndroid Build Coastguard Worker   // structures.
1159*9880d681SAndroid Build Coastguard Worker   auto MergedCs = RC.switchInternalEdgeToCall(C3, B1);
1160*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(0u, MergedCs.size());
1161*9880d681SAndroid Build Coastguard Worker   ASSERT_EQ(8, RC.size());
1162*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&DC, &RC[0]);
1163*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&B3C, &RC[1]);
1164*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&B2C, &RC[2]);
1165*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&B1C, &RC[3]);
1166*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&C3C, &RC[4]);
1167*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&C2C, &RC[5]);
1168*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&C1C, &RC[6]);
1169*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&AC, &RC[7]);
1170*9880d681SAndroid Build Coastguard Worker }
1171*9880d681SAndroid Build Coastguard Worker 
TEST(LazyCallGraphTest,InternalRefEdgeToCallBothPartitionAndMerge)1172*9880d681SAndroid Build Coastguard Worker TEST(LazyCallGraphTest, InternalRefEdgeToCallBothPartitionAndMerge) {
1173*9880d681SAndroid Build Coastguard Worker   LLVMContext Context;
1174*9880d681SAndroid Build Coastguard Worker   // Test for having a postorder where between the source and target are all
1175*9880d681SAndroid Build Coastguard Worker   // three kinds of other SCCs:
1176*9880d681SAndroid Build Coastguard Worker   // 1) One connected to the target only that have to be shifted below the
1177*9880d681SAndroid Build Coastguard Worker   //    source.
1178*9880d681SAndroid Build Coastguard Worker   // 2) One connected to the source only that have to be shifted below the
1179*9880d681SAndroid Build Coastguard Worker   //    target.
1180*9880d681SAndroid Build Coastguard Worker   // 3) One connected to both source and target that has to remain and get
1181*9880d681SAndroid Build Coastguard Worker   //    merged away.
1182*9880d681SAndroid Build Coastguard Worker   //
1183*9880d681SAndroid Build Coastguard Worker   // To achieve this we construct a heavily connected graph to force
1184*9880d681SAndroid Build Coastguard Worker   // a particular post-order. Then we remove the forcing edges and connect
1185*9880d681SAndroid Build Coastguard Worker   // a cycle.
1186*9880d681SAndroid Build Coastguard Worker   //
1187*9880d681SAndroid Build Coastguard Worker   // Diagram for the graph we want on the left and the graph we use to force
1188*9880d681SAndroid Build Coastguard Worker   // the ordering on the right. Edges ponit down or right.
1189*9880d681SAndroid Build Coastguard Worker   //
1190*9880d681SAndroid Build Coastguard Worker   //   A    |    A    |
1191*9880d681SAndroid Build Coastguard Worker   //  / \   |   / \   |
1192*9880d681SAndroid Build Coastguard Worker   // B   E  |  B   \  |
1193*9880d681SAndroid Build Coastguard Worker   // |\  |  |  |\  |  |
1194*9880d681SAndroid Build Coastguard Worker   // | D |  |  C-D-E  |
1195*9880d681SAndroid Build Coastguard Worker   // |  \|  |  |  \|  |
1196*9880d681SAndroid Build Coastguard Worker   // C   F  |  \   F  |
1197*9880d681SAndroid Build Coastguard Worker   //  \ /   |   \ /   |
1198*9880d681SAndroid Build Coastguard Worker   //   G    |    G    |
1199*9880d681SAndroid Build Coastguard Worker   //
1200*9880d681SAndroid Build Coastguard Worker   // And we form a cycle by connecting F to B.
1201*9880d681SAndroid Build Coastguard Worker   std::unique_ptr<Module> M =
1202*9880d681SAndroid Build Coastguard Worker       parseAssembly(Context, "define void @a() {\n"
1203*9880d681SAndroid Build Coastguard Worker                              "entry:\n"
1204*9880d681SAndroid Build Coastguard Worker                              "  call void @b()\n"
1205*9880d681SAndroid Build Coastguard Worker                              "  call void @e()\n"
1206*9880d681SAndroid Build Coastguard Worker                              "  ret void\n"
1207*9880d681SAndroid Build Coastguard Worker                              "}\n"
1208*9880d681SAndroid Build Coastguard Worker                              "define void @b() {\n"
1209*9880d681SAndroid Build Coastguard Worker                              "entry:\n"
1210*9880d681SAndroid Build Coastguard Worker                              "  call void @c()\n"
1211*9880d681SAndroid Build Coastguard Worker                              "  call void @d()\n"
1212*9880d681SAndroid Build Coastguard Worker                              "  ret void\n"
1213*9880d681SAndroid Build Coastguard Worker                              "}\n"
1214*9880d681SAndroid Build Coastguard Worker                              "define void @c() {\n"
1215*9880d681SAndroid Build Coastguard Worker                              "entry:\n"
1216*9880d681SAndroid Build Coastguard Worker                              "  call void @d()\n"
1217*9880d681SAndroid Build Coastguard Worker                              "  call void @g()\n"
1218*9880d681SAndroid Build Coastguard Worker                              "  ret void\n"
1219*9880d681SAndroid Build Coastguard Worker                              "}\n"
1220*9880d681SAndroid Build Coastguard Worker                              "define void @d() {\n"
1221*9880d681SAndroid Build Coastguard Worker                              "entry:\n"
1222*9880d681SAndroid Build Coastguard Worker                              "  call void @e()\n"
1223*9880d681SAndroid Build Coastguard Worker                              "  call void @f()\n"
1224*9880d681SAndroid Build Coastguard Worker                              "  ret void\n"
1225*9880d681SAndroid Build Coastguard Worker                              "}\n"
1226*9880d681SAndroid Build Coastguard Worker                              "define void @e() {\n"
1227*9880d681SAndroid Build Coastguard Worker                              "entry:\n"
1228*9880d681SAndroid Build Coastguard Worker                              "  call void @f()\n"
1229*9880d681SAndroid Build Coastguard Worker                              "  ret void\n"
1230*9880d681SAndroid Build Coastguard Worker                              "}\n"
1231*9880d681SAndroid Build Coastguard Worker                              "define void @f() {\n"
1232*9880d681SAndroid Build Coastguard Worker                              "entry:\n"
1233*9880d681SAndroid Build Coastguard Worker                              "  store void()* @b, void()** undef\n"
1234*9880d681SAndroid Build Coastguard Worker                              "  call void @g()\n"
1235*9880d681SAndroid Build Coastguard Worker                              "  ret void\n"
1236*9880d681SAndroid Build Coastguard Worker                              "}\n"
1237*9880d681SAndroid Build Coastguard Worker                              "define void @g() {\n"
1238*9880d681SAndroid Build Coastguard Worker                              "entry:\n"
1239*9880d681SAndroid Build Coastguard Worker                              "  store void()* @a, void()** undef\n"
1240*9880d681SAndroid Build Coastguard Worker                              "  ret void\n"
1241*9880d681SAndroid Build Coastguard Worker                              "}\n");
1242*9880d681SAndroid Build Coastguard Worker   LazyCallGraph CG(*M);
1243*9880d681SAndroid Build Coastguard Worker 
1244*9880d681SAndroid Build Coastguard Worker   // Force the graph to be fully expanded.
1245*9880d681SAndroid Build Coastguard Worker   auto I = CG.postorder_ref_scc_begin();
1246*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::RefSCC &RC = *I++;
1247*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(CG.postorder_ref_scc_end(), I);
1248*9880d681SAndroid Build Coastguard Worker 
1249*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &A = *CG.lookup(lookupFunction(*M, "a"));
1250*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &B = *CG.lookup(lookupFunction(*M, "b"));
1251*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &C = *CG.lookup(lookupFunction(*M, "c"));
1252*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &D = *CG.lookup(lookupFunction(*M, "d"));
1253*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &E = *CG.lookup(lookupFunction(*M, "e"));
1254*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &F = *CG.lookup(lookupFunction(*M, "f"));
1255*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::Node &G = *CG.lookup(lookupFunction(*M, "g"));
1256*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::SCC &AC = *CG.lookupSCC(A);
1257*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::SCC &BC = *CG.lookupSCC(B);
1258*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::SCC &CC = *CG.lookupSCC(C);
1259*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::SCC &DC = *CG.lookupSCC(D);
1260*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::SCC &EC = *CG.lookupSCC(E);
1261*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::SCC &FC = *CG.lookupSCC(F);
1262*9880d681SAndroid Build Coastguard Worker   LazyCallGraph::SCC &GC = *CG.lookupSCC(G);
1263*9880d681SAndroid Build Coastguard Worker 
1264*9880d681SAndroid Build Coastguard Worker   // Remove the extra edges that were used to force a particular post-order.
1265*9880d681SAndroid Build Coastguard Worker   RC.switchInternalEdgeToRef(C, D);
1266*9880d681SAndroid Build Coastguard Worker   RC.switchInternalEdgeToRef(D, E);
1267*9880d681SAndroid Build Coastguard Worker 
1268*9880d681SAndroid Build Coastguard Worker   // Check the initial post-order. We ensure this order with the extra edges
1269*9880d681SAndroid Build Coastguard Worker   // that are nuked above.
1270*9880d681SAndroid Build Coastguard Worker   ASSERT_EQ(7, RC.size());
1271*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&GC, &RC[0]);
1272*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&FC, &RC[1]);
1273*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&EC, &RC[2]);
1274*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&DC, &RC[3]);
1275*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&CC, &RC[4]);
1276*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&BC, &RC[5]);
1277*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&AC, &RC[6]);
1278*9880d681SAndroid Build Coastguard Worker 
1279*9880d681SAndroid Build Coastguard Worker   // Switch F -> B to a call edge. This merges B, D, and F into a single SCC,
1280*9880d681SAndroid Build Coastguard Worker   // and has to place the C and E SCCs on either side of it:
1281*9880d681SAndroid Build Coastguard Worker   //   A          A    |
1282*9880d681SAndroid Build Coastguard Worker   //  / \        / \   |
1283*9880d681SAndroid Build Coastguard Worker   // B   E      |   E  |
1284*9880d681SAndroid Build Coastguard Worker   // |\  |       \ /   |
1285*9880d681SAndroid Build Coastguard Worker   // | D |  ->    B    |
1286*9880d681SAndroid Build Coastguard Worker   // |  \|       / \   |
1287*9880d681SAndroid Build Coastguard Worker   // C   F      C   |  |
1288*9880d681SAndroid Build Coastguard Worker   //  \ /        \ /   |
1289*9880d681SAndroid Build Coastguard Worker   //   G          G    |
1290*9880d681SAndroid Build Coastguard Worker   auto MergedCs = RC.switchInternalEdgeToCall(F, B);
1291*9880d681SAndroid Build Coastguard Worker   ASSERT_EQ(2u, MergedCs.size());
1292*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&FC, MergedCs[0]);
1293*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&DC, MergedCs[1]);
1294*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(3, BC.size());
1295*9880d681SAndroid Build Coastguard Worker 
1296*9880d681SAndroid Build Coastguard Worker   // And make sure the postorder was updated.
1297*9880d681SAndroid Build Coastguard Worker   ASSERT_EQ(5, RC.size());
1298*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&GC, &RC[0]);
1299*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&CC, &RC[1]);
1300*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&BC, &RC[2]);
1301*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&EC, &RC[3]);
1302*9880d681SAndroid Build Coastguard Worker   EXPECT_EQ(&AC, &RC[4]);
1303*9880d681SAndroid Build Coastguard Worker }
1304*9880d681SAndroid Build Coastguard Worker 
1305*9880d681SAndroid Build Coastguard Worker }
1306