#include "llvm/Analysis/LazyCallGraph.h"
#include "llvm/ADT/Triple.h"
#include "llvm/AsmParser/Parser.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Verifier.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/SourceMgr.h"
#include "gtest/gtest.h"
#include <memory>
using namespace llvm;
namespace {
std::unique_ptr<Module> parseAssembly(LLVMContext &Context,
const char *Assembly) {
SMDiagnostic Error;
std::unique_ptr<Module> M = parseAssemblyString(Assembly, Error, Context);
std::string ErrMsg;
raw_string_ostream OS(ErrMsg);
Error.print("", OS);
if (!M)
report_fatal_error(OS.str().c_str());
return M;
}
static const char DiamondOfTriangles[] =
"define void @a1() {\n"
"entry:\n"
" call void @a2()\n"
" call void @b2()\n"
" call void @c3()\n"
" ret void\n"
"}\n"
"define void @a2() {\n"
"entry:\n"
" call void @a3()\n"
" ret void\n"
"}\n"
"define void @a3() {\n"
"entry:\n"
" call void @a1()\n"
" ret void\n"
"}\n"
"define void @b1() {\n"
"entry:\n"
" call void @b2()\n"
" call void @d3()\n"
" ret void\n"
"}\n"
"define void @b2() {\n"
"entry:\n"
" call void @b3()\n"
" ret void\n"
"}\n"
"define void @b3() {\n"
"entry:\n"
" call void @b1()\n"
" ret void\n"
"}\n"
"define void @c1() {\n"
"entry:\n"
" call void @c2()\n"
" call void @d2()\n"
" ret void\n"
"}\n"
"define void @c2() {\n"
"entry:\n"
" call void @c3()\n"
" ret void\n"
"}\n"
"define void @c3() {\n"
"entry:\n"
" call void @c1()\n"
" ret void\n"
"}\n"
"define void @d1() {\n"
"entry:\n"
" call void @d2()\n"
" ret void\n"
"}\n"
"define void @d2() {\n"
"entry:\n"
" call void @d3()\n"
" ret void\n"
"}\n"
"define void @d3() {\n"
"entry:\n"
" call void @d1()\n"
" ret void\n"
"}\n";
static const char DiamondOfTrianglesRefGraph[] =
"define void @a1() {\n"
"entry:\n"
" %a = alloca void ()*\n"
" store void ()* @a2, void ()** %a\n"
" store void ()* @b2, void ()** %a\n"
" store void ()* @c3, void ()** %a\n"
" ret void\n"
"}\n"
"define void @a2() {\n"
"entry:\n"
" %a = alloca void ()*\n"
" store void ()* @a3, void ()** %a\n"
" ret void\n"
"}\n"
"define void @a3() {\n"
"entry:\n"
" %a = alloca void ()*\n"
" store void ()* @a1, void ()** %a\n"
" ret void\n"
"}\n"
"define void @b1() {\n"
"entry:\n"
" %a = alloca void ()*\n"
" store void ()* @b2, void ()** %a\n"
" store void ()* @d3, void ()** %a\n"
" ret void\n"
"}\n"
"define void @b2() {\n"
"entry:\n"
" %a = alloca void ()*\n"
" store void ()* @b3, void ()** %a\n"
" ret void\n"
"}\n"
"define void @b3() {\n"
"entry:\n"
" %a = alloca void ()*\n"
" store void ()* @b1, void ()** %a\n"
" ret void\n"
"}\n"
"define void @c1() {\n"
"entry:\n"
" %a = alloca void ()*\n"
" store void ()* @c2, void ()** %a\n"
" store void ()* @d2, void ()** %a\n"
" ret void\n"
"}\n"
"define void @c2() {\n"
"entry:\n"
" %a = alloca void ()*\n"
" store void ()* @c3, void ()** %a\n"
" ret void\n"
"}\n"
"define void @c3() {\n"
"entry:\n"
" %a = alloca void ()*\n"
" store void ()* @c1, void ()** %a\n"
" ret void\n"
"}\n"
"define void @d1() {\n"
"entry:\n"
" %a = alloca void ()*\n"
" store void ()* @d2, void ()** %a\n"
" ret void\n"
"}\n"
"define void @d2() {\n"
"entry:\n"
" %a = alloca void ()*\n"
" store void ()* @d3, void ()** %a\n"
" ret void\n"
"}\n"
"define void @d3() {\n"
"entry:\n"
" %a = alloca void ()*\n"
" store void ()* @d1, void ()** %a\n"
" ret void\n"
"}\n";
static LazyCallGraph buildCG(Module &M) {
TargetLibraryInfoImpl TLII(Triple(M.getTargetTriple()));
TargetLibraryInfo TLI(TLII);
auto GetTLI = [&TLI](Function &F) -> TargetLibraryInfo & { return TLI; };
LazyCallGraph CG(M, GetTLI);
return CG;
}
TEST(LazyCallGraphTest, BasicGraphFormation) {
LLVMContext Context;
std::unique_ptr<Module> M = parseAssembly(Context, DiamondOfTriangles);
LazyCallGraph CG = buildCG(*M);
auto I = CG.begin();
LazyCallGraph::Node &A1 = (I++)->getNode();
EXPECT_EQ("a1", A1.getFunction().getName());
LazyCallGraph::Node &A2 = (I++)->getNode();
EXPECT_EQ("a2", A2.getFunction().getName());
LazyCallGraph::Node &A3 = (I++)->getNode();
EXPECT_EQ("a3", A3.getFunction().getName());
LazyCallGraph::Node &B1 = (I++)->getNode();
EXPECT_EQ("b1", B1.getFunction().getName());
LazyCallGraph::Node &B2 = (I++)->getNode();
EXPECT_EQ("b2", B2.getFunction().getName());
LazyCallGraph::Node &B3 = (I++)->getNode();
EXPECT_EQ("b3", B3.getFunction().getName());
LazyCallGraph::Node &C1 = (I++)->getNode();
EXPECT_EQ("c1", C1.getFunction().getName());
LazyCallGraph::Node &C2 = (I++)->getNode();
EXPECT_EQ("c2", C2.getFunction().getName());
LazyCallGraph::Node &C3 = (I++)->getNode();
EXPECT_EQ("c3", C3.getFunction().getName());
LazyCallGraph::Node &D1 = (I++)->getNode();
EXPECT_EQ("d1", D1.getFunction().getName());
LazyCallGraph::Node &D2 = (I++)->getNode();
EXPECT_EQ("d2", D2.getFunction().getName());
LazyCallGraph::Node &D3 = (I++)->getNode();
EXPECT_EQ("d3", D3.getFunction().getName());
EXPECT_EQ(CG.end(), I);
std::vector<std::string> Nodes;
for (LazyCallGraph::Edge &E : A1.populate())
Nodes.push_back(std::string(E.getFunction().getName()));
llvm::sort(Nodes);
EXPECT_EQ("a2", Nodes[0]);
EXPECT_EQ("b2", Nodes[1]);
EXPECT_EQ("c3", Nodes[2]);
Nodes.clear();
A2.populate();
EXPECT_EQ(A2->end(), std::next(A2->begin()));
EXPECT_EQ("a3", A2->begin()->getFunction().getName());
A3.populate();
EXPECT_EQ(A3->end(), std::next(A3->begin()));
EXPECT_EQ("a1", A3->begin()->getFunction().getName());
for (LazyCallGraph::Edge &E : B1.populate())
Nodes.push_back(std::string(E.getFunction().getName()));
llvm::sort(Nodes);
EXPECT_EQ("b2", Nodes[0]);
EXPECT_EQ("d3", Nodes[1]);
Nodes.clear();
B2.populate();
EXPECT_EQ(B2->end(), std::next(B2->begin()));
EXPECT_EQ("b3", B2->begin()->getFunction().getName());
B3.populate();
EXPECT_EQ(B3->end(), std::next(B3->begin()));
EXPECT_EQ("b1", B3->begin()->getFunction().getName());
for (LazyCallGraph::Edge &E : C1.populate())
Nodes.push_back(std::string(E.getFunction().getName()));
llvm::sort(Nodes);
EXPECT_EQ("c2", Nodes[0]);
EXPECT_EQ("d2", Nodes[1]);
Nodes.clear();
C2.populate();
EXPECT_EQ(C2->end(), std::next(C2->begin()));
EXPECT_EQ("c3", C2->begin()->getFunction().getName());
C3.populate();
EXPECT_EQ(C3->end(), std::next(C3->begin()));
EXPECT_EQ("c1", C3->begin()->getFunction().getName());
D1.populate();
EXPECT_EQ(D1->end(), std::next(D1->begin()));
EXPECT_EQ("d2", D1->begin()->getFunction().getName());
D2.populate();
EXPECT_EQ(D2->end(), std::next(D2->begin()));
EXPECT_EQ("d3", D2->begin()->getFunction().getName());
D3.populate();
EXPECT_EQ(D3->end(), std::next(D3->begin()));
EXPECT_EQ("d1", D3->begin()->getFunction().getName());
CG.buildRefSCCs();
auto J = CG.postorder_ref_scc_begin();
LazyCallGraph::RefSCC &D = *J++;
ASSERT_EQ(1, D.size());
for (LazyCallGraph::Node &N : *D.begin())
Nodes.push_back(std::string(N.getFunction().getName()));
llvm::sort(Nodes);
EXPECT_EQ(3u, Nodes.size());
EXPECT_EQ("d1", Nodes[0]);
EXPECT_EQ("d2", Nodes[1]);
EXPECT_EQ("d3", Nodes[2]);
Nodes.clear();
EXPECT_FALSE(D.isParentOf(D));
EXPECT_FALSE(D.isChildOf(D));
EXPECT_FALSE(D.isAncestorOf(D));
EXPECT_FALSE(D.isDescendantOf(D));
EXPECT_EQ(&D, &*CG.postorder_ref_scc_begin());
LazyCallGraph::RefSCC &B = *J++;
ASSERT_EQ(1, B.size());
for (LazyCallGraph::Node &N : *B.begin())
Nodes.push_back(std::string(N.getFunction().getName()));
llvm::sort(Nodes);
EXPECT_EQ(3u, Nodes.size());
EXPECT_EQ("b1", Nodes[0]);
EXPECT_EQ("b2", Nodes[1]);
EXPECT_EQ("b3", Nodes[2]);
Nodes.clear();
EXPECT_TRUE(B.isParentOf(D));
EXPECT_FALSE(B.isChildOf(D));
EXPECT_TRUE(B.isAncestorOf(D));
EXPECT_FALSE(B.isDescendantOf(D));
EXPECT_EQ(&B, &*std::next(CG.postorder_ref_scc_begin()));
LazyCallGraph::RefSCC &C = *J++;
ASSERT_EQ(1, C.size());
for (LazyCallGraph::Node &N : *C.begin())
Nodes.push_back(std::string(N.getFunction().getName()));
llvm::sort(Nodes);
EXPECT_EQ(3u, Nodes.size());
EXPECT_EQ("c1", Nodes[0]);
EXPECT_EQ("c2", Nodes[1]);
EXPECT_EQ("c3", Nodes[2]);
Nodes.clear();
EXPECT_FALSE(B.isAncestorOf(C));
EXPECT_FALSE(C.isAncestorOf(B));
EXPECT_TRUE(C.isParentOf(D));
EXPECT_FALSE(C.isChildOf(D));
EXPECT_TRUE(C.isAncestorOf(D));
EXPECT_FALSE(C.isDescendantOf(D));
EXPECT_EQ(&C, &*std::next(CG.postorder_ref_scc_begin(), 2));
LazyCallGraph::RefSCC &A = *J++;
ASSERT_EQ(1, A.size());
for (LazyCallGraph::Node &N : *A.begin())
Nodes.push_back(std::string(N.getFunction().getName()));
llvm::sort(Nodes);
EXPECT_EQ(3u, Nodes.size());
EXPECT_EQ("a1", Nodes[0]);
EXPECT_EQ("a2", Nodes[1]);
EXPECT_EQ("a3", Nodes[2]);
Nodes.clear();
EXPECT_TRUE(A.isParentOf(B));
EXPECT_TRUE(A.isParentOf(C));
EXPECT_FALSE(A.isParentOf(D));
EXPECT_TRUE(A.isAncestorOf(B));
EXPECT_TRUE(A.isAncestorOf(C));
EXPECT_TRUE(A.isAncestorOf(D));
EXPECT_EQ(&A, &*std::next(CG.postorder_ref_scc_begin(), 3));
EXPECT_EQ(CG.postorder_ref_scc_end(), J);
EXPECT_EQ(J, std::next(CG.postorder_ref_scc_begin(), 4));
}
static Function &lookupFunction(Module &M, StringRef Name) {
for (Function &F : M)
if (F.getName() == Name)
return F;
report_fatal_error("Couldn't find function!");
}
TEST(LazyCallGraphTest, BasicGraphMutation) {
LLVMContext Context;
std::unique_ptr<Module> M = parseAssembly(Context, "define void @a() {\n"
"entry:\n"
" call void @b()\n"
" call void @c()\n"
" ret void\n"
"}\n"
"define void @b() {\n"
"entry:\n"
" ret void\n"
"}\n"
"define void @c() {\n"
"entry:\n"
" ret void\n"
"}\n");
LazyCallGraph CG = buildCG(*M);
LazyCallGraph::Node &A = CG.get(lookupFunction(*M, "a"));
LazyCallGraph::Node &B = CG.get(lookupFunction(*M, "b"));
A.populate();
EXPECT_EQ(2, std::distance(A->begin(), A->end()));
B.populate();
EXPECT_EQ(0, std::distance(B->begin(), B->end()));
LazyCallGraph::Node &C = CG.get(lookupFunction(*M, "c"));
C.populate();
CG.insertEdge(B, C, LazyCallGraph::Edge::Call);
EXPECT_EQ(1, std::distance(B->begin(), B->end()));
EXPECT_EQ(0, std::distance(C->begin(), C->end()));
CG.insertEdge(C, B, LazyCallGraph::Edge::Call);
EXPECT_EQ(1, std::distance(C->begin(), C->end()));
EXPECT_EQ(&B, &C->begin()->getNode());
CG.insertEdge(C, C, LazyCallGraph::Edge::Call);
EXPECT_EQ(2, std::distance(C->begin(), C->end()));
EXPECT_EQ(&B, &C->begin()->getNode());
EXPECT_EQ(&C, &std::next(C->begin())->getNode());
CG.removeEdge(C, B);
EXPECT_EQ(1, std::distance(C->begin(), C->end()));
EXPECT_EQ(&C, &C->begin()->getNode());
CG.removeEdge(C, C);
EXPECT_EQ(0, std::distance(C->begin(), C->end()));
CG.removeEdge(B, C);
EXPECT_EQ(0, std::distance(B->begin(), B->end()));
}
TEST(LazyCallGraphTest, InnerSCCFormation) {
LLVMContext Context;
std::unique_ptr<Module> M = parseAssembly(Context, DiamondOfTriangles);
LazyCallGraph CG = buildCG(*M);
LazyCallGraph::Node &D1 = CG.get(lookupFunction(*M, "d1"));
LazyCallGraph::Node &A1 = CG.get(lookupFunction(*M, "a1"));
A1.populate();
D1.populate();
CG.insertEdge(D1, A1, LazyCallGraph::Edge::Ref);
std::vector<std::string> Nodes;
CG.buildRefSCCs();
auto I = CG.postorder_ref_scc_begin();
LazyCallGraph::RefSCC &RC = *I++;
EXPECT_EQ(CG.postorder_ref_scc_end(), I);
auto J = RC.begin();
LazyCallGraph::SCC &D = *J++;
for (LazyCallGraph::Node &N : D)
Nodes.push_back(std::string(N.getFunction().getName()));
llvm::sort(Nodes);
EXPECT_EQ(3u, Nodes.size());
EXPECT_EQ("d1", Nodes[0]);
EXPECT_EQ("d2", Nodes[1]);
EXPECT_EQ("d3", Nodes[2]);
Nodes.clear();
LazyCallGraph::SCC &B = *J++;
for (LazyCallGraph::Node &N : B)
Nodes.push_back(std::string(N.getFunction().getName()));
llvm::sort(Nodes);
EXPECT_EQ(3u, Nodes.size());
EXPECT_EQ("b1", Nodes[0]);
EXPECT_EQ("b2", Nodes[1]);
EXPECT_EQ("b3", Nodes[2]);
Nodes.clear();
LazyCallGraph::SCC &C = *J++;
for (LazyCallGraph::Node &N : C)
Nodes.push_back(std::string(N.getFunction().getName()));
llvm::sort(Nodes);
EXPECT_EQ(3u, Nodes.size());
EXPECT_EQ("c1", Nodes[0]);
EXPECT_EQ("c2", Nodes[1]);
EXPECT_EQ("c3", Nodes[2]);
Nodes.clear();
LazyCallGraph::SCC &A = *J++;
for (LazyCallGraph::Node &N : A)
Nodes.push_back(std::string(N.getFunction().getName()));
llvm::sort(Nodes);
EXPECT_EQ(3u, Nodes.size());
EXPECT_EQ("a1", Nodes[0]);
EXPECT_EQ("a2", Nodes[1]);
EXPECT_EQ("a3", Nodes[2]);
Nodes.clear();
EXPECT_EQ(RC.end(), J);
}
TEST(LazyCallGraphTest, MultiArmSCC) {
LLVMContext Context;
std::unique_ptr<Module> M = parseAssembly(Context, "define void @f1() {\n"
"entry:\n"
" call void @f2()\n"
" call void @f4()\n"
" ret void\n"
"}\n"
"define void @f2() {\n"
"entry:\n"
" call void @f3()\n"
" ret void\n"
"}\n"
"define void @f3() {\n"
"entry:\n"
" call void @f1()\n"
" ret void\n"
"}\n"
"define void @f4() {\n"
"entry:\n"
" call void @f5()\n"
" ret void\n"
"}\n"
"define void @f5() {\n"
"entry:\n"
" call void @f1()\n"
" ret void\n"
"}\n");
LazyCallGraph CG = buildCG(*M);
CG.buildRefSCCs();
auto I = CG.postorder_ref_scc_begin();
LazyCallGraph::RefSCC &RC = *I++;
EXPECT_EQ(CG.postorder_ref_scc_end(), I);
LazyCallGraph::Node &N1 = *CG.lookup(lookupFunction(*M, "f1"));
LazyCallGraph::Node &N2 = *CG.lookup(lookupFunction(*M, "f2"));
LazyCallGraph::Node &N3 = *CG.lookup(lookupFunction(*M, "f3"));
LazyCallGraph::Node &N4 = *CG.lookup(lookupFunction(*M, "f4"));
LazyCallGraph::Node &N5 = *CG.lookup(lookupFunction(*M, "f4"));
EXPECT_EQ(&RC, CG.lookupRefSCC(N1));
EXPECT_EQ(&RC, CG.lookupRefSCC(N2));
EXPECT_EQ(&RC, CG.lookupRefSCC(N3));
EXPECT_EQ(&RC, CG.lookupRefSCC(N4));
EXPECT_EQ(&RC, CG.lookupRefSCC(N5));
ASSERT_EQ(1, RC.size());
LazyCallGraph::SCC &C = *RC.begin();
EXPECT_EQ(&C, CG.lookupSCC(N1));
EXPECT_EQ(&C, CG.lookupSCC(N2));
EXPECT_EQ(&C, CG.lookupSCC(N3));
EXPECT_EQ(&C, CG.lookupSCC(N4));
EXPECT_EQ(&C, CG.lookupSCC(N5));
}
TEST(LazyCallGraphTest, OutgoingEdgeMutation) {
LLVMContext Context;
std::unique_ptr<Module> M = parseAssembly(Context, "define void @a() {\n"
"entry:\n"
" call void @b()\n"
" call void @c()\n"
" ret void\n"
"}\n"
"define void @b() {\n"
"entry:\n"
" call void @d()\n"
" ret void\n"
"}\n"
"define void @c() {\n"
"entry:\n"
" call void @d()\n"
" ret void\n"
"}\n"
"define void @d() {\n"
"entry:\n"
" ret void\n"
"}\n");
LazyCallGraph CG = buildCG(*M);
CG.buildRefSCCs();
for (LazyCallGraph::RefSCC &RC : CG.postorder_ref_sccs())
dbgs() << "Formed RefSCC: " << RC << "\n";
LazyCallGraph::Node &A = *CG.lookup(lookupFunction(*M, "a"));
LazyCallGraph::Node &B = *CG.lookup(lookupFunction(*M, "b"));
LazyCallGraph::Node &C = *CG.lookup(lookupFunction(*M, "c"));
LazyCallGraph::Node &D = *CG.lookup(lookupFunction(*M, "d"));
LazyCallGraph::SCC &AC = *CG.lookupSCC(A);
LazyCallGraph::SCC &BC = *CG.lookupSCC(B);
LazyCallGraph::SCC &CC = *CG.lookupSCC(C);
LazyCallGraph::SCC &DC = *CG.lookupSCC(D);
LazyCallGraph::RefSCC &ARC = *CG.lookupRefSCC(A);
LazyCallGraph::RefSCC &BRC = *CG.lookupRefSCC(B);
LazyCallGraph::RefSCC &CRC = *CG.lookupRefSCC(C);
LazyCallGraph::RefSCC &DRC = *CG.lookupRefSCC(D);
EXPECT_TRUE(ARC.isParentOf(BRC));
EXPECT_TRUE(AC.isParentOf(BC));
EXPECT_TRUE(ARC.isParentOf(CRC));
EXPECT_TRUE(AC.isParentOf(CC));
EXPECT_FALSE(ARC.isParentOf(DRC));
EXPECT_FALSE(AC.isParentOf(DC));
EXPECT_TRUE(ARC.isAncestorOf(DRC));
EXPECT_TRUE(AC.isAncestorOf(DC));
EXPECT_FALSE(DRC.isChildOf(ARC));
EXPECT_FALSE(DC.isChildOf(AC));
EXPECT_TRUE(DRC.isDescendantOf(ARC));
EXPECT_TRUE(DC.isDescendantOf(AC));
EXPECT_TRUE(DRC.isChildOf(BRC));
EXPECT_TRUE(DC.isChildOf(BC));
EXPECT_TRUE(DRC.isChildOf(CRC));
EXPECT_TRUE(DC.isChildOf(CC));
EXPECT_EQ(2, std::distance(A->begin(), A->end()));
ARC.insertOutgoingEdge(A, D, LazyCallGraph::Edge::Call);
EXPECT_EQ(3, std::distance(A->begin(), A->end()));
const LazyCallGraph::Edge &NewE = (*A)[D];
EXPECT_TRUE(NewE);
EXPECT_TRUE(NewE.isCall());
EXPECT_EQ(&D, &NewE.getNode());
EXPECT_TRUE(ARC.isParentOf(DRC));
EXPECT_TRUE(AC.isParentOf(DC));
EXPECT_TRUE(ARC.isAncestorOf(DRC));
EXPECT_TRUE(AC.isAncestorOf(DC));
EXPECT_TRUE(DRC.isChildOf(ARC));
EXPECT_TRUE(DC.isChildOf(AC));
EXPECT_TRUE(DRC.isDescendantOf(ARC));
EXPECT_TRUE(DC.isDescendantOf(AC));
EXPECT_EQ(&AC, CG.lookupSCC(A));
EXPECT_EQ(&BC, CG.lookupSCC(B));
EXPECT_EQ(&CC, CG.lookupSCC(C));
EXPECT_EQ(&DC, CG.lookupSCC(D));
EXPECT_EQ(&ARC, CG.lookupRefSCC(A));
EXPECT_EQ(&BRC, CG.lookupRefSCC(B));
EXPECT_EQ(&CRC, CG.lookupRefSCC(C));
EXPECT_EQ(&DRC, CG.lookupRefSCC(D));
ARC.switchOutgoingEdgeToRef(A, D);
EXPECT_FALSE(NewE.isCall());
EXPECT_TRUE(ARC.isParentOf(DRC));
EXPECT_FALSE(AC.isParentOf(DC));
EXPECT_TRUE(ARC.isAncestorOf(DRC));
EXPECT_TRUE(AC.isAncestorOf(DC));
EXPECT_TRUE(DRC.isChildOf(ARC));
EXPECT_FALSE(DC.isChildOf(AC));
EXPECT_TRUE(DRC.isDescendantOf(ARC));
EXPECT_TRUE(DC.isDescendantOf(AC));
EXPECT_EQ(&AC, CG.lookupSCC(A));
EXPECT_EQ(&BC, CG.lookupSCC(B));
EXPECT_EQ(&CC, CG.lookupSCC(C));
EXPECT_EQ(&DC, CG.lookupSCC(D));
EXPECT_EQ(&ARC, CG.lookupRefSCC(A));
EXPECT_EQ(&BRC, CG.lookupRefSCC(B));
EXPECT_EQ(&CRC, CG.lookupRefSCC(C));
EXPECT_EQ(&DRC, CG.lookupRefSCC(D));
ARC.switchOutgoingEdgeToCall(A, D);
EXPECT_TRUE(NewE.isCall());
EXPECT_TRUE(ARC.isParentOf(DRC));
EXPECT_TRUE(AC.isParentOf(DC));
EXPECT_TRUE(ARC.isAncestorOf(DRC));
EXPECT_TRUE(AC.isAncestorOf(DC));
EXPECT_TRUE(DRC.isChildOf(ARC));
EXPECT_TRUE(DC.isChildOf(AC));
EXPECT_TRUE(DRC.isDescendantOf(ARC));
EXPECT_TRUE(DC.isDescendantOf(AC));
EXPECT_EQ(&AC, CG.lookupSCC(A));
EXPECT_EQ(&BC, CG.lookupSCC(B));
EXPECT_EQ(&CC, CG.lookupSCC(C));
EXPECT_EQ(&DC, CG.lookupSCC(D));
EXPECT_EQ(&ARC, CG.lookupRefSCC(A));
EXPECT_EQ(&BRC, CG.lookupRefSCC(B));
EXPECT_EQ(&CRC, CG.lookupRefSCC(C));
EXPECT_EQ(&DRC, CG.lookupRefSCC(D));
ARC.removeOutgoingEdge(A, D);
EXPECT_EQ(2, std::distance(A->begin(), A->end()));
EXPECT_FALSE(ARC.isParentOf(DRC));
EXPECT_FALSE(AC.isParentOf(DC));
EXPECT_TRUE(ARC.isAncestorOf(DRC));
EXPECT_TRUE(AC.isAncestorOf(DC));
EXPECT_FALSE(DRC.isChildOf(ARC));
EXPECT_FALSE(DC.isChildOf(AC));
EXPECT_TRUE(DRC.isDescendantOf(ARC));
EXPECT_TRUE(DC.isDescendantOf(AC));
EXPECT_EQ(&AC, CG.lookupSCC(A));
EXPECT_EQ(&BC, CG.lookupSCC(B));
EXPECT_EQ(&CC, CG.lookupSCC(C));
EXPECT_EQ(&DC, CG.lookupSCC(D));
EXPECT_EQ(&ARC, CG.lookupRefSCC(A));
EXPECT_EQ(&BRC, CG.lookupRefSCC(B));
EXPECT_EQ(&CRC, CG.lookupRefSCC(C));
EXPECT_EQ(&DRC, CG.lookupRefSCC(D));
}
TEST(LazyCallGraphTest, IncomingEdgeInsertion) {
LLVMContext Context;
std::unique_ptr<Module> M = parseAssembly(Context, DiamondOfTriangles);
LazyCallGraph CG = buildCG(*M);
CG.buildRefSCCs();
for (LazyCallGraph::RefSCC &RC : CG.postorder_ref_sccs())
dbgs() << "Formed RefSCC: " << RC << "\n";
LazyCallGraph::Node &A1 = *CG.lookup(lookupFunction(*M, "a1"));
LazyCallGraph::Node &A2 = *CG.lookup(lookupFunction(*M, "a2"));
LazyCallGraph::Node &A3 = *CG.lookup(lookupFunction(*M, "a3"));
LazyCallGraph::Node &B1 = *CG.lookup(lookupFunction(*M, "b1"));
LazyCallGraph::Node &B2 = *CG.lookup(lookupFunction(*M, "b2"));
LazyCallGraph::Node &B3 = *CG.lookup(lookupFunction(*M, "b3"));
LazyCallGraph::Node &C1 = *CG.lookup(lookupFunction(*M, "c1"));
LazyCallGraph::Node &C2 = *CG.lookup(lookupFunction(*M, "c2"));
LazyCallGraph::Node &C3 = *CG.lookup(lookupFunction(*M, "c3"));
LazyCallGraph::Node &D1 = *CG.lookup(lookupFunction(*M, "d1"));
LazyCallGraph::Node &D2 = *CG.lookup(lookupFunction(*M, "d2"));
LazyCallGraph::Node &D3 = *CG.lookup(lookupFunction(*M, "d3"));
LazyCallGraph::RefSCC &ARC = *CG.lookupRefSCC(A1);
LazyCallGraph::RefSCC &BRC = *CG.lookupRefSCC(B1);
LazyCallGraph::RefSCC &CRC = *CG.lookupRefSCC(C1);
LazyCallGraph::RefSCC &DRC = *CG.lookupRefSCC(D1);
ASSERT_EQ(&ARC, CG.lookupRefSCC(A2));
ASSERT_EQ(&ARC, CG.lookupRefSCC(A3));
ASSERT_EQ(&BRC, CG.lookupRefSCC(B2));
ASSERT_EQ(&BRC, CG.lookupRefSCC(B3));
ASSERT_EQ(&CRC, CG.lookupRefSCC(C2));
ASSERT_EQ(&CRC, CG.lookupRefSCC(C3));
ASSERT_EQ(&DRC, CG.lookupRefSCC(D2));
ASSERT_EQ(&DRC, CG.lookupRefSCC(D3));
ASSERT_EQ(1, std::distance(D2->begin(), D2->end()));
auto MergedRCs = CRC.insertIncomingRefEdge(D2, C2);
for (LazyCallGraph::Edge E : *D2) {
if (&E.getNode() == &D3)
continue;
EXPECT_EQ(&C2, &E.getNode());
}
EXPECT_EQ(1u, MergedRCs.size());
EXPECT_EQ(&DRC, MergedRCs[0]);
EXPECT_EQ(&ARC, CG.lookupRefSCC(A1));
EXPECT_EQ(&ARC, CG.lookupRefSCC(A2));
EXPECT_EQ(&ARC, CG.lookupRefSCC(A3));
EXPECT_EQ(&BRC, CG.lookupRefSCC(B1));
EXPECT_EQ(&BRC, CG.lookupRefSCC(B2));
EXPECT_EQ(&BRC, CG.lookupRefSCC(B3));
EXPECT_EQ(&CRC, CG.lookupRefSCC(C1));
EXPECT_EQ(&CRC, CG.lookupRefSCC(C2));
EXPECT_EQ(&CRC, CG.lookupRefSCC(C3));
EXPECT_EQ(&CRC, CG.lookupRefSCC(D1));
EXPECT_EQ(&CRC, CG.lookupRefSCC(D2));
EXPECT_EQ(&CRC, CG.lookupRefSCC(D3));
EXPECT_TRUE(ARC.isParentOf(CRC));
EXPECT_TRUE(BRC.isParentOf(CRC));
auto I = CG.postorder_ref_scc_begin(), E = CG.postorder_ref_scc_end();
ASSERT_NE(I, E);
EXPECT_EQ(&CRC, &*I) << "Actual RefSCC: " << *I;
ASSERT_NE(++I, E);
EXPECT_EQ(&BRC, &*I) << "Actual RefSCC: " << *I;
ASSERT_NE(++I, E);
EXPECT_EQ(&ARC, &*I) << "Actual RefSCC: " << *I;
EXPECT_EQ(++I, E);
}
TEST(LazyCallGraphTest, IncomingEdgeInsertionRefGraph) {
LLVMContext Context;
std::unique_ptr<Module> M =
parseAssembly(Context, DiamondOfTrianglesRefGraph);
LazyCallGraph CG = buildCG(*M);
CG.buildRefSCCs();
for (LazyCallGraph::RefSCC &RC : CG.postorder_ref_sccs())
dbgs() << "Formed RefSCC: " << RC << "\n";
LazyCallGraph::Node &A1 = *CG.lookup(lookupFunction(*M, "a1"));
LazyCallGraph::Node &A2 = *CG.lookup(lookupFunction(*M, "a2"));
LazyCallGraph::Node &A3 = *CG.lookup(lookupFunction(*M, "a3"));
LazyCallGraph::Node &B1 = *CG.lookup(lookupFunction(*M, "b1"));
LazyCallGraph::Node &B2 = *CG.lookup(lookupFunction(*M, "b2"));
LazyCallGraph::Node &B3 = *CG.lookup(lookupFunction(*M, "b3"));
LazyCallGraph::Node &C1 = *CG.lookup(lookupFunction(*M, "c1"));
LazyCallGraph::Node &C2 = *CG.lookup(lookupFunction(*M, "c2"));
LazyCallGraph::Node &C3 = *CG.lookup(lookupFunction(*M, "c3"));
LazyCallGraph::Node &D1 = *CG.lookup(lookupFunction(*M, "d1"));
LazyCallGraph::Node &D2 = *CG.lookup(lookupFunction(*M, "d2"));
LazyCallGraph::Node &D3 = *CG.lookup(lookupFunction(*M, "d3"));
LazyCallGraph::RefSCC &ARC = *CG.lookupRefSCC(A1);
LazyCallGraph::RefSCC &BRC = *CG.lookupRefSCC(B1);
LazyCallGraph::RefSCC &CRC = *CG.lookupRefSCC(C1);
LazyCallGraph::RefSCC &DRC = *CG.lookupRefSCC(D1);
ASSERT_EQ(&ARC, CG.lookupRefSCC(A2));
ASSERT_EQ(&ARC, CG.lookupRefSCC(A3));
ASSERT_EQ(&BRC, CG.lookupRefSCC(B2));
ASSERT_EQ(&BRC, CG.lookupRefSCC(B3));
ASSERT_EQ(&CRC, CG.lookupRefSCC(C2));
ASSERT_EQ(&CRC, CG.lookupRefSCC(C3));
ASSERT_EQ(&DRC, CG.lookupRefSCC(D2));
ASSERT_EQ(&DRC, CG.lookupRefSCC(D3));
ASSERT_EQ(1, std::distance(D2->begin(), D2->end()));
auto MergedRCs = CRC.insertIncomingRefEdge(D2, C2);
for (LazyCallGraph::Edge E : *D2) {
if (&E.getNode() == &D3)
continue;
EXPECT_EQ(&C2, &E.getNode());
}
EXPECT_EQ(1u, MergedRCs.size());
EXPECT_EQ(&DRC, MergedRCs[0]);
EXPECT_EQ(&ARC, CG.lookupRefSCC(A1));
EXPECT_EQ(&ARC, CG.lookupRefSCC(A2));
EXPECT_EQ(&ARC, CG.lookupRefSCC(A3));
EXPECT_EQ(&BRC, CG.lookupRefSCC(B1));
EXPECT_EQ(&BRC, CG.lookupRefSCC(B2));
EXPECT_EQ(&BRC, CG.lookupRefSCC(B3));
EXPECT_EQ(&CRC, CG.lookupRefSCC(C1));
EXPECT_EQ(&CRC, CG.lookupRefSCC(C2));
EXPECT_EQ(&CRC, CG.lookupRefSCC(C3));
EXPECT_EQ(&CRC, CG.lookupRefSCC(D1));
EXPECT_EQ(&CRC, CG.lookupRefSCC(D2));
EXPECT_EQ(&CRC, CG.lookupRefSCC(D3));
EXPECT_TRUE(ARC.isParentOf(CRC));
EXPECT_TRUE(BRC.isParentOf(CRC));
auto I = CG.postorder_ref_scc_begin(), E = CG.postorder_ref_scc_end();
ASSERT_NE(I, E);
EXPECT_EQ(&CRC, &*I) << "Actual RefSCC: " << *I;
ASSERT_NE(++I, E);
EXPECT_EQ(&BRC, &*I) << "Actual RefSCC: " << *I;
ASSERT_NE(++I, E);
EXPECT_EQ(&ARC, &*I) << "Actual RefSCC: " << *I;
EXPECT_EQ(++I, E);
}
TEST(LazyCallGraphTest, IncomingEdgeInsertionLargeCallCycle) {
LLVMContext Context;
std::unique_ptr<Module> M = parseAssembly(Context, "define void @a() {\n"
"entry:\n"
" call void @b()\n"
" ret void\n"
"}\n"
"define void @b() {\n"
"entry:\n"
" call void @c()\n"
" ret void\n"
"}\n"
"define void @c() {\n"
"entry:\n"
" call void @d()\n"
" ret void\n"
"}\n"
"define void @d() {\n"
"entry:\n"
" ret void\n"
"}\n");
LazyCallGraph CG = buildCG(*M);
CG.buildRefSCCs();
for (LazyCallGraph::RefSCC &RC : CG.postorder_ref_sccs())
dbgs() << "Formed RefSCC: " << RC << "\n";
LazyCallGraph::Node &A = *CG.lookup(lookupFunction(*M, "a"));
LazyCallGraph::Node &B = *CG.lookup(lookupFunction(*M, "b"));
LazyCallGraph::Node &C = *CG.lookup(lookupFunction(*M, "c"));
LazyCallGraph::Node &D = *CG.lookup(lookupFunction(*M, "d"));
LazyCallGraph::SCC &AC = *CG.lookupSCC(A);
LazyCallGraph::SCC &BC = *CG.lookupSCC(B);
LazyCallGraph::SCC &CC = *CG.lookupSCC(C);
LazyCallGraph::SCC &DC = *CG.lookupSCC(D);
LazyCallGraph::RefSCC &ARC = *CG.lookupRefSCC(A);
LazyCallGraph::RefSCC &BRC = *CG.lookupRefSCC(B);
LazyCallGraph::RefSCC &CRC = *CG.lookupRefSCC(C);
LazyCallGraph::RefSCC &DRC = *CG.lookupRefSCC(D);
auto MergedRCs = ARC.insertIncomingRefEdge(D, A);
EXPECT_NE(D->begin(), D->end());
EXPECT_EQ(&A, &D->begin()->getNode());
EXPECT_EQ(3u, MergedRCs.size());
EXPECT_NE(find(MergedRCs, &BRC), MergedRCs.end());
EXPECT_NE(find(MergedRCs, &CRC), MergedRCs.end());
EXPECT_NE(find(MergedRCs, &DRC), MergedRCs.end());
EXPECT_EQ(&ARC, CG.lookupRefSCC(A));
EXPECT_EQ(&ARC, CG.lookupRefSCC(B));
EXPECT_EQ(&ARC, CG.lookupRefSCC(C));
EXPECT_EQ(&ARC, CG.lookupRefSCC(D));
EXPECT_EQ(4, ARC.size());
EXPECT_EQ(&DC, &ARC[0]);
EXPECT_EQ(&CC, &ARC[1]);
EXPECT_EQ(&BC, &ARC[2]);
EXPECT_EQ(&AC, &ARC[3]);
auto I = CG.postorder_ref_scc_begin(), E = CG.postorder_ref_scc_end();
ASSERT_NE(I, E);
EXPECT_EQ(&ARC, &*I) << "Actual RefSCC: " << *I;
EXPECT_EQ(++I, E);
}
TEST(LazyCallGraphTest, IncomingEdgeInsertionLargeRefCycle) {
LLVMContext Context;
std::unique_ptr<Module> M =
parseAssembly(Context, "define void @a() {\n"
"entry:\n"
" %p = alloca void ()*\n"
" store void ()* @b, void ()** %p\n"
" ret void\n"
"}\n"
"define void @b() {\n"
"entry:\n"
" %p = alloca void ()*\n"
" store void ()* @c, void ()** %p\n"
" ret void\n"
"}\n"
"define void @c() {\n"
"entry:\n"
" %p = alloca void ()*\n"
" store void ()* @d, void ()** %p\n"
" ret void\n"
"}\n"
"define void @d() {\n"
"entry:\n"
" ret void\n"
"}\n");
LazyCallGraph CG = buildCG(*M);
CG.buildRefSCCs();
for (LazyCallGraph::RefSCC &RC : CG.postorder_ref_sccs())
dbgs() << "Formed RefSCC: " << RC << "\n";
LazyCallGraph::Node &A = *CG.lookup(lookupFunction(*M, "a"));
LazyCallGraph::Node &B = *CG.lookup(lookupFunction(*M, "b"));
LazyCallGraph::Node &C = *CG.lookup(lookupFunction(*M, "c"));
LazyCallGraph::Node &D = *CG.lookup(lookupFunction(*M, "d"));
LazyCallGraph::RefSCC &ARC = *CG.lookupRefSCC(A);
LazyCallGraph::RefSCC &BRC = *CG.lookupRefSCC(B);
LazyCallGraph::RefSCC &CRC = *CG.lookupRefSCC(C);
LazyCallGraph::RefSCC &DRC = *CG.lookupRefSCC(D);
auto MergedRCs = ARC.insertIncomingRefEdge(D, A);
EXPECT_NE(D->begin(), D->end());
EXPECT_EQ(&A, &D->begin()->getNode());
EXPECT_EQ(3u, MergedRCs.size());
EXPECT_NE(find(MergedRCs, &BRC), MergedRCs.end());
EXPECT_NE(find(MergedRCs, &CRC), MergedRCs.end());
EXPECT_NE(find(MergedRCs, &DRC), MergedRCs.end());
EXPECT_EQ(&ARC, CG.lookupRefSCC(A));
EXPECT_EQ(&ARC, CG.lookupRefSCC(B));
EXPECT_EQ(&ARC, CG.lookupRefSCC(C));
EXPECT_EQ(&ARC, CG.lookupRefSCC(D));
auto I = CG.postorder_ref_scc_begin(), End = CG.postorder_ref_scc_end();
ASSERT_NE(I, End);
EXPECT_EQ(&ARC, &*I) << "Actual RefSCC: " << *I;
EXPECT_EQ(++I, End);
}
TEST(LazyCallGraphTest, InlineAndDeleteFunction) {
LLVMContext Context;
std::unique_ptr<Module> M = parseAssembly(Context, DiamondOfTriangles);
LazyCallGraph CG = buildCG(*M);
CG.buildRefSCCs();
for (LazyCallGraph::RefSCC &RC : CG.postorder_ref_sccs())
dbgs() << "Formed RefSCC: " << RC << "\n";
LazyCallGraph::Node &A1 = *CG.lookup(lookupFunction(*M, "a1"));
LazyCallGraph::Node &A2 = *CG.lookup(lookupFunction(*M, "a2"));
LazyCallGraph::Node &A3 = *CG.lookup(lookupFunction(*M, "a3"));
LazyCallGraph::Node &B1 = *CG.lookup(lookupFunction(*M, "b1"));
LazyCallGraph::Node &B2 = *CG.lookup(lookupFunction(*M, "b2"));
LazyCallGraph::Node &B3 = *CG.lookup(lookupFunction(*M, "b3"));
LazyCallGraph::Node &C1 = *CG.lookup(lookupFunction(*M, "c1"));
LazyCallGraph::Node &C2 = *CG.lookup(lookupFunction(*M, "c2"));
LazyCallGraph::Node &C3 = *CG.lookup(lookupFunction(*M, "c3"));
LazyCallGraph::Node &D1 = *CG.lookup(lookupFunction(*M, "d1"));
LazyCallGraph::Node &D2 = *CG.lookup(lookupFunction(*M, "d2"));
LazyCallGraph::Node &D3 = *CG.lookup(lookupFunction(*M, "d3"));
LazyCallGraph::RefSCC &ARC = *CG.lookupRefSCC(A1);
LazyCallGraph::RefSCC &BRC = *CG.lookupRefSCC(B1);
LazyCallGraph::RefSCC &CRC = *CG.lookupRefSCC(C1);
LazyCallGraph::RefSCC &DRC = *CG.lookupRefSCC(D1);
ASSERT_EQ(&ARC, CG.lookupRefSCC(A2));
ASSERT_EQ(&ARC, CG.lookupRefSCC(A3));
ASSERT_EQ(&BRC, CG.lookupRefSCC(B2));
ASSERT_EQ(&BRC, CG.lookupRefSCC(B3));
ASSERT_EQ(&CRC, CG.lookupRefSCC(C2));
ASSERT_EQ(&CRC, CG.lookupRefSCC(C3));
ASSERT_EQ(&DRC, CG.lookupRefSCC(D2));
ASSERT_EQ(&DRC, CG.lookupRefSCC(D3));
ASSERT_EQ(1, std::distance(D2->begin(), D2->end()));
Function &D2F = D2.getFunction();
CallInst *C1Call = nullptr, *D1Call = nullptr;
for (User *U : D2F.users()) {
CallInst *CI = dyn_cast<CallInst>(U);
ASSERT_TRUE(CI) << "Expected a call: " << *U;
if (CI->getParent()->getParent() == &C1.getFunction()) {
ASSERT_EQ(nullptr, C1Call) << "Found too many C1 calls: " << *CI;
C1Call = CI;
} else if (CI->getParent()->getParent() == &D1.getFunction()) {
ASSERT_EQ(nullptr, D1Call) << "Found too many D1 calls: " << *CI;
D1Call = CI;
} else {
FAIL() << "Found an unexpected call instruction: " << *CI;
}
}
ASSERT_NE(C1Call, nullptr);
ASSERT_NE(D1Call, nullptr);
ASSERT_EQ(&D2F, C1Call->getCalledFunction());
ASSERT_EQ(&D2F, D1Call->getCalledFunction());
C1Call->setCalledFunction(&D3.getFunction());
D1Call->setCalledFunction(&D3.getFunction());
ASSERT_EQ(0u, D2F.getNumUses());
CRC.insertTrivialCallEdge(C1, D3);
DRC.insertTrivialCallEdge(D1, D3);
LazyCallGraph::SCC &DC = *CG.lookupSCC(D2);
auto NewCs = DRC.switchInternalEdgeToRef(D1, D2);
EXPECT_EQ(&DC, CG.lookupSCC(D2));
EXPECT_EQ(NewCs.end(), std::next(NewCs.begin()));
LazyCallGraph::SCC &NewDC = *NewCs.begin();
EXPECT_EQ(&NewDC, CG.lookupSCC(D1));
EXPECT_EQ(&NewDC, CG.lookupSCC(D3));
auto NewRCs = DRC.removeInternalRefEdge(D1, {&D2});
ASSERT_EQ(2u, NewRCs.size());
LazyCallGraph::RefSCC &NewDRC = *NewRCs[0];
EXPECT_EQ(&NewDRC, CG.lookupRefSCC(D1));
EXPECT_EQ(&NewDRC, CG.lookupRefSCC(D3));
LazyCallGraph::RefSCC &D2RC = *NewRCs[1];
EXPECT_EQ(&D2RC, CG.lookupRefSCC(D2));
EXPECT_FALSE(NewDRC.isParentOf(D2RC));
EXPECT_TRUE(CRC.isParentOf(D2RC));
EXPECT_TRUE(CRC.isParentOf(NewDRC));
EXPECT_TRUE(D2RC.isParentOf(NewDRC));
CRC.removeOutgoingEdge(C1, D2);
EXPECT_FALSE(CRC.isParentOf(D2RC));
EXPECT_TRUE(CRC.isParentOf(NewDRC));
EXPECT_TRUE(D2RC.isParentOf(NewDRC));
CG.removeDeadFunction(D2F);
EXPECT_EQ(&ARC, CG.lookupRefSCC(A1));
EXPECT_EQ(&ARC, CG.lookupRefSCC(A2));
EXPECT_EQ(&ARC, CG.lookupRefSCC(A3));
EXPECT_EQ(&BRC, CG.lookupRefSCC(B1));
EXPECT_EQ(&BRC, CG.lookupRefSCC(B2));
EXPECT_EQ(&BRC, CG.lookupRefSCC(B3));
EXPECT_EQ(&CRC, CG.lookupRefSCC(C1));
EXPECT_EQ(&CRC, CG.lookupRefSCC(C2));
EXPECT_EQ(&CRC, CG.lookupRefSCC(C3));
EXPECT_EQ(&NewDRC, CG.lookupRefSCC(D1));
EXPECT_EQ(&NewDRC, CG.lookupRefSCC(D3));
EXPECT_TRUE(CRC.isParentOf(NewDRC));
auto I = CG.postorder_ref_scc_begin(), E = CG.postorder_ref_scc_end();
ASSERT_NE(I, E);
EXPECT_EQ(&NewDRC, &*I) << "Actual RefSCC: " << *I;
ASSERT_NE(++I, E);
EXPECT_EQ(&BRC, &*I) << "Actual RefSCC: " << *I;
ASSERT_NE(++I, E);
EXPECT_EQ(&CRC, &*I) << "Actual RefSCC: " << *I;
ASSERT_NE(++I, E);
EXPECT_EQ(&ARC, &*I) << "Actual RefSCC: " << *I;
EXPECT_EQ(++I, E);
}
TEST(LazyCallGraphTest, InternalEdgeMutation) {
LLVMContext Context;
std::unique_ptr<Module> M = parseAssembly(Context, "define void @a() {\n"
"entry:\n"
" call void @b()\n"
" ret void\n"
"}\n"
"define void @b() {\n"
"entry:\n"
" call void @c()\n"
" ret void\n"
"}\n"
"define void @c() {\n"
"entry:\n"
" call void @a()\n"
" ret void\n"
"}\n");
LazyCallGraph CG = buildCG(*M);
CG.buildRefSCCs();
auto I = CG.postorder_ref_scc_begin();
LazyCallGraph::RefSCC &RC = *I++;
EXPECT_EQ(CG.postorder_ref_scc_end(), I);
LazyCallGraph::Node &A = *CG.lookup(lookupFunction(*M, "a"));
LazyCallGraph::Node &B = *CG.lookup(lookupFunction(*M, "b"));
LazyCallGraph::Node &C = *CG.lookup(lookupFunction(*M, "c"));
EXPECT_EQ(&RC, CG.lookupRefSCC(A));
EXPECT_EQ(&RC, CG.lookupRefSCC(B));
EXPECT_EQ(&RC, CG.lookupRefSCC(C));
EXPECT_EQ(1, RC.size());
EXPECT_EQ(&*RC.begin(), CG.lookupSCC(A));
EXPECT_EQ(&*RC.begin(), CG.lookupSCC(B));
EXPECT_EQ(&*RC.begin(), CG.lookupSCC(C));
RC.insertInternalRefEdge(A, C);
EXPECT_EQ(2, std::distance(A->begin(), A->end()));
EXPECT_EQ(&RC, CG.lookupRefSCC(A));
EXPECT_EQ(&RC, CG.lookupRefSCC(B));
EXPECT_EQ(&RC, CG.lookupRefSCC(C));
EXPECT_EQ(1, RC.size());
EXPECT_EQ(&*RC.begin(), CG.lookupSCC(A));
EXPECT_EQ(&*RC.begin(), CG.lookupSCC(B));
EXPECT_EQ(&*RC.begin(), CG.lookupSCC(C));
auto NewCs = RC.switchInternalEdgeToRef(B, C);
EXPECT_EQ(&RC, CG.lookupRefSCC(A));
EXPECT_EQ(&RC, CG.lookupRefSCC(B));
EXPECT_EQ(&RC, CG.lookupRefSCC(C));
auto J = RC.begin();
EXPECT_EQ(&*J++, CG.lookupSCC(B));
EXPECT_EQ(&*J++, CG.lookupSCC(A));
EXPECT_EQ(&*J++, CG.lookupSCC(C));
EXPECT_EQ(RC.end(), J);
EXPECT_EQ(RC.begin(), NewCs.begin());
EXPECT_EQ(std::prev(RC.end()), NewCs.end());
LazyCallGraph::SCC &AC = *CG.lookupSCC(A);
LazyCallGraph::SCC &CC = *CG.lookupSCC(C);
EXPECT_TRUE(RC.switchInternalEdgeToCall(A, C, [&](ArrayRef<LazyCallGraph::SCC *> MergedCs) {
ASSERT_EQ(1u, MergedCs.size());
EXPECT_EQ(&AC, MergedCs[0]);
}));
EXPECT_EQ(2, CC.size());
EXPECT_EQ(&CC, CG.lookupSCC(A));
EXPECT_EQ(&CC, CG.lookupSCC(C));
J = RC.begin();
EXPECT_EQ(&*J++, CG.lookupSCC(B));
EXPECT_EQ(&*J++, CG.lookupSCC(C));
EXPECT_EQ(RC.end(), J);
}
TEST(LazyCallGraphTest, InternalEdgeRemoval) {
LLVMContext Context;
std::unique_ptr<Module> M = parseAssembly(
Context, "define void @a(i8** %ptr) {\n"
"entry:\n"
" store i8* bitcast (void(i8**)* @a to i8*), i8** %ptr\n"
" store i8* bitcast (void(i8**)* @b to i8*), i8** %ptr\n"
" store i8* bitcast (void(i8**)* @c to i8*), i8** %ptr\n"
" ret void\n"
"}\n"
"define void @b(i8** %ptr) {\n"
"entry:\n"
" store i8* bitcast (void(i8**)* @a to i8*), i8** %ptr\n"
" store i8* bitcast (void(i8**)* @b to i8*), i8** %ptr\n"
" store i8* bitcast (void(i8**)* @c to i8*), i8** %ptr\n"
" ret void\n"
"}\n"
"define void @c(i8** %ptr) {\n"
"entry:\n"
" store i8* bitcast (void(i8**)* @a to i8*), i8** %ptr\n"
" store i8* bitcast (void(i8**)* @b to i8*), i8** %ptr\n"
" store i8* bitcast (void(i8**)* @c to i8*), i8** %ptr\n"
" ret void\n"
"}\n");
LazyCallGraph CG = buildCG(*M);
CG.buildRefSCCs();
auto I = CG.postorder_ref_scc_begin(), E = CG.postorder_ref_scc_end();
LazyCallGraph::RefSCC &RC = *I;
EXPECT_EQ(E, std::next(I));
LazyCallGraph::Node &A = *CG.lookup(lookupFunction(*M, "a"));
LazyCallGraph::Node &B = *CG.lookup(lookupFunction(*M, "b"));
LazyCallGraph::Node &C = *CG.lookup(lookupFunction(*M, "c"));
EXPECT_EQ(&RC, CG.lookupRefSCC(A));
EXPECT_EQ(&RC, CG.lookupRefSCC(B));
EXPECT_EQ(&RC, CG.lookupRefSCC(C));
SmallVector<LazyCallGraph::RefSCC *, 1> NewRCs =
RC.removeInternalRefEdge(B, {&A});
EXPECT_EQ(0u, NewRCs.size());
EXPECT_EQ(&RC, CG.lookupRefSCC(A));
EXPECT_EQ(&RC, CG.lookupRefSCC(B));
EXPECT_EQ(&RC, CG.lookupRefSCC(C));
auto J = CG.postorder_ref_scc_begin();
EXPECT_EQ(I, J);
EXPECT_EQ(&RC, &*J);
EXPECT_EQ(E, std::next(J));
++I;
NewRCs = RC.removeInternalRefEdge(C, {&A});
ASSERT_EQ(2u, NewRCs.size());
LazyCallGraph::RefSCC &BCRC = *NewRCs[0];
LazyCallGraph::RefSCC &ARC = *NewRCs[1];
EXPECT_EQ(&ARC, CG.lookupRefSCC(A));
EXPECT_EQ(1, std::distance(ARC.begin(), ARC.end()));
EXPECT_EQ(&BCRC, CG.lookupRefSCC(B));
EXPECT_EQ(&BCRC, CG.lookupRefSCC(C));
J = CG.postorder_ref_scc_begin();
EXPECT_NE(I, J);
EXPECT_EQ(&BCRC, &*J);
++J;
EXPECT_NE(I, J);
EXPECT_EQ(&ARC, &*J);
++J;
EXPECT_EQ(I, J);
EXPECT_EQ(E, J);
}
TEST(LazyCallGraphTest, InternalMultiEdgeRemoval) {
LLVMContext Context;
std::unique_ptr<Module> M = parseAssembly(
Context, "define void @a(i8** %ptr) {\n"
"entry:\n"
" store i8* bitcast (void(i8**)* @a to i8*), i8** %ptr\n"
" store i8* bitcast (void(i8**)* @b to i8*), i8** %ptr\n"
" store i8* bitcast (void(i8**)* @c to i8*), i8** %ptr\n"
" ret void\n"
"}\n"
"define void @b(i8** %ptr) {\n"
"entry:\n"
" store i8* bitcast (void(i8**)* @a to i8*), i8** %ptr\n"
" store i8* bitcast (void(i8**)* @b to i8*), i8** %ptr\n"
" store i8* bitcast (void(i8**)* @c to i8*), i8** %ptr\n"
" ret void\n"
"}\n"
"define void @c(i8** %ptr) {\n"
"entry:\n"
" store i8* bitcast (void(i8**)* @a to i8*), i8** %ptr\n"
" store i8* bitcast (void(i8**)* @b to i8*), i8** %ptr\n"
" store i8* bitcast (void(i8**)* @c to i8*), i8** %ptr\n"
" ret void\n"
"}\n");
LazyCallGraph CG = buildCG(*M);
CG.buildRefSCCs();
auto I = CG.postorder_ref_scc_begin(), E = CG.postorder_ref_scc_end();
LazyCallGraph::RefSCC &RC = *I;
EXPECT_EQ(E, std::next(I));
LazyCallGraph::Node &A = *CG.lookup(lookupFunction(*M, "a"));
LazyCallGraph::Node &B = *CG.lookup(lookupFunction(*M, "b"));
LazyCallGraph::Node &C = *CG.lookup(lookupFunction(*M, "c"));
EXPECT_EQ(&RC, CG.lookupRefSCC(A));
EXPECT_EQ(&RC, CG.lookupRefSCC(B));
EXPECT_EQ(&RC, CG.lookupRefSCC(C));
++I;
SmallVector<LazyCallGraph::RefSCC *, 1> NewRCs =
RC.removeInternalRefEdge(B, {&A, &C});
ASSERT_EQ(2u, NewRCs.size());
LazyCallGraph::RefSCC &BRC = *NewRCs[0];
LazyCallGraph::RefSCC &ACRC = *NewRCs[1];
EXPECT_EQ(&BRC, CG.lookupRefSCC(B));
EXPECT_EQ(1, std::distance(BRC.begin(), BRC.end()));
EXPECT_EQ(&ACRC, CG.lookupRefSCC(A));
EXPECT_EQ(&ACRC, CG.lookupRefSCC(C));
auto J = CG.postorder_ref_scc_begin();
EXPECT_NE(I, J);
EXPECT_EQ(&BRC, &*J);
++J;
EXPECT_NE(I, J);
EXPECT_EQ(&ACRC, &*J);
++J;
EXPECT_EQ(I, J);
EXPECT_EQ(E, J);
}
TEST(LazyCallGraphTest, InternalNoOpEdgeRemoval) {
LLVMContext Context;
std::unique_ptr<Module> M = parseAssembly(
Context, "define void @a(i8** %ptr) {\n"
"entry:\n"
" call void @b(i8** %ptr)\n"
" store i8* bitcast (void(i8**)* @c to i8*), i8** %ptr\n"
" ret void\n"
"}\n"
"define void @b(i8** %ptr) {\n"
"entry:\n"
" store i8* bitcast (void(i8**)* @a to i8*), i8** %ptr\n"
" call void @c(i8** %ptr)\n"
" ret void\n"
"}\n"
"define void @c(i8** %ptr) {\n"
"entry:\n"
" call void @a(i8** %ptr)\n"
" store i8* bitcast (void(i8**)* @b to i8*), i8** %ptr\n"
" ret void\n"
"}\n");
LazyCallGraph CG = buildCG(*M);
CG.buildRefSCCs();
auto I = CG.postorder_ref_scc_begin(), E = CG.postorder_ref_scc_end();
LazyCallGraph::RefSCC &RC = *I;
EXPECT_EQ(E, std::next(I));
LazyCallGraph::SCC &C = *RC.begin();
EXPECT_EQ(RC.end(), std::next(RC.begin()));
LazyCallGraph::Node &AN = *CG.lookup(lookupFunction(*M, "a"));
LazyCallGraph::Node &BN = *CG.lookup(lookupFunction(*M, "b"));
LazyCallGraph::Node &CN = *CG.lookup(lookupFunction(*M, "c"));
EXPECT_EQ(&RC, CG.lookupRefSCC(AN));
EXPECT_EQ(&RC, CG.lookupRefSCC(BN));
EXPECT_EQ(&RC, CG.lookupRefSCC(CN));
EXPECT_EQ(&C, CG.lookupSCC(AN));
EXPECT_EQ(&C, CG.lookupSCC(BN));
EXPECT_EQ(&C, CG.lookupSCC(CN));
SmallVector<LazyCallGraph::RefSCC *, 1> NewRCs =
RC.removeInternalRefEdge(AN, {&CN});
EXPECT_EQ(0u, NewRCs.size());
EXPECT_EQ(&RC, CG.lookupRefSCC(AN));
EXPECT_EQ(&RC, CG.lookupRefSCC(BN));
EXPECT_EQ(&RC, CG.lookupRefSCC(CN));
EXPECT_EQ(&C, CG.lookupSCC(AN));
EXPECT_EQ(&C, CG.lookupSCC(BN));
EXPECT_EQ(&C, CG.lookupSCC(CN));
auto J = CG.postorder_ref_scc_begin();
EXPECT_EQ(I, J);
EXPECT_EQ(&RC, &*J);
EXPECT_EQ(E, std::next(J));
NewRCs = RC.removeInternalRefEdge(BN, {&AN});
NewRCs = RC.removeInternalRefEdge(CN, {&BN});
EXPECT_EQ(0u, NewRCs.size());
EXPECT_EQ(&RC, CG.lookupRefSCC(AN));
EXPECT_EQ(&RC, CG.lookupRefSCC(BN));
EXPECT_EQ(&RC, CG.lookupRefSCC(CN));
EXPECT_EQ(&C, CG.lookupSCC(AN));
EXPECT_EQ(&C, CG.lookupSCC(BN));
EXPECT_EQ(&C, CG.lookupSCC(CN));
J = CG.postorder_ref_scc_begin();
EXPECT_EQ(I, J);
EXPECT_EQ(&RC, &*J);
EXPECT_EQ(E, std::next(J));
}
TEST(LazyCallGraphTest, InternalCallEdgeToRef) {
LLVMContext Context;
std::unique_ptr<Module> M = parseAssembly(Context, "define void @a() {\n"
"entry:\n"
" call void @a()\n"
" call void @b()\n"
" call void @c()\n"
" ret void\n"
"}\n"
"define void @b() {\n"
"entry:\n"
" call void @a()\n"
" call void @b()\n"
" call void @c()\n"
" ret void\n"
"}\n"
"define void @c() {\n"
"entry:\n"
" call void @a()\n"
" call void @b()\n"
" call void @c()\n"
" ret void\n"
"}\n");
LazyCallGraph CG = buildCG(*M);
CG.buildRefSCCs();
auto I = CG.postorder_ref_scc_begin();
LazyCallGraph::RefSCC &RC = *I++;
EXPECT_EQ(CG.postorder_ref_scc_end(), I);
EXPECT_EQ(1, RC.size());
LazyCallGraph::SCC &AC = *RC.begin();
LazyCallGraph::Node &AN = *CG.lookup(lookupFunction(*M, "a"));
LazyCallGraph::Node &BN = *CG.lookup(lookupFunction(*M, "b"));
LazyCallGraph::Node &CN = *CG.lookup(lookupFunction(*M, "c"));
EXPECT_EQ(&AC, CG.lookupSCC(AN));
EXPECT_EQ(&AC, CG.lookupSCC(BN));
EXPECT_EQ(&AC, CG.lookupSCC(CN));
auto NewCs = RC.switchInternalEdgeToRef(BN, AN);
EXPECT_EQ(NewCs.begin(), NewCs.end());
EXPECT_EQ(1, RC.size());
EXPECT_EQ(&AC, CG.lookupSCC(AN));
EXPECT_EQ(&AC, CG.lookupSCC(BN));
EXPECT_EQ(&AC, CG.lookupSCC(CN));
NewCs = RC.switchInternalEdgeToRef(CN, AN);
EXPECT_EQ(1, std::distance(NewCs.begin(), NewCs.end()));
EXPECT_EQ(2, RC.size());
EXPECT_EQ(&AC, CG.lookupSCC(AN));
LazyCallGraph::SCC &BC = *CG.lookupSCC(BN);
EXPECT_NE(&BC, &AC);
EXPECT_EQ(&BC, CG.lookupSCC(CN));
auto J = RC.find(AC);
EXPECT_EQ(&AC, &*J);
--J;
EXPECT_EQ(&BC, &*J);
EXPECT_EQ(RC.begin(), J);
EXPECT_EQ(J, NewCs.begin());
NewCs = RC.switchInternalEdgeToRef(CN, BN);
EXPECT_EQ(1, std::distance(NewCs.begin(), NewCs.end()));
EXPECT_EQ(3, RC.size());
EXPECT_EQ(&AC, CG.lookupSCC(AN));
EXPECT_EQ(&BC, CG.lookupSCC(BN));
LazyCallGraph::SCC &CC = *CG.lookupSCC(CN);
EXPECT_NE(&CC, &AC);
EXPECT_NE(&CC, &BC);
J = RC.find(AC);
EXPECT_EQ(&AC, &*J);
--J;
EXPECT_EQ(&BC, &*J);
--J;
EXPECT_EQ(&CC, &*J);
EXPECT_EQ(RC.begin(), J);
EXPECT_EQ(J, NewCs.begin());
}
TEST(LazyCallGraphTest, InternalRefEdgeToCall) {
LLVMContext Context;
std::unique_ptr<Module> M =
parseAssembly(Context, "define void @a() {\n"
"entry:\n"
" call void @b()\n"
" call void @c()\n"
" store void()* @d, void()** undef\n"
" ret void\n"
"}\n"
"define void @b() {\n"
"entry:\n"
" store void()* @c, void()** undef\n"
" call void @d()\n"
" ret void\n"
"}\n"
"define void @c() {\n"
"entry:\n"
" store void()* @b, void()** undef\n"
" call void @d()\n"
" ret void\n"
"}\n"
"define void @d() {\n"
"entry:\n"
" store void()* @a, void()** undef\n"
" ret void\n"
"}\n");
LazyCallGraph CG = buildCG(*M);
CG.buildRefSCCs();
auto I = CG.postorder_ref_scc_begin();
LazyCallGraph::RefSCC &RC = *I++;
EXPECT_EQ(CG.postorder_ref_scc_end(), I);
LazyCallGraph::Node &A = *CG.lookup(lookupFunction(*M, "a"));
LazyCallGraph::Node &B = *CG.lookup(lookupFunction(*M, "b"));
LazyCallGraph::Node &C = *CG.lookup(lookupFunction(*M, "c"));
LazyCallGraph::Node &D = *CG.lookup(lookupFunction(*M, "d"));
LazyCallGraph::SCC &AC = *CG.lookupSCC(A);
LazyCallGraph::SCC &BC = *CG.lookupSCC(B);
LazyCallGraph::SCC &CC = *CG.lookupSCC(C);
LazyCallGraph::SCC &DC = *CG.lookupSCC(D);
ASSERT_EQ(4, RC.size());
EXPECT_EQ(&DC, &RC[0]);
EXPECT_EQ(&BC, &RC[1]);
EXPECT_EQ(&CC, &RC[2]);
EXPECT_EQ(&AC, &RC[3]);
EXPECT_FALSE(RC.switchInternalEdgeToCall(A, D));
ASSERT_EQ(4, RC.size());
EXPECT_EQ(&DC, &RC[0]);
EXPECT_EQ(&BC, &RC[1]);
EXPECT_EQ(&CC, &RC[2]);
EXPECT_EQ(&AC, &RC[3]);
EXPECT_FALSE(RC.switchInternalEdgeToCall(B, C));
ASSERT_EQ(4, RC.size());
EXPECT_EQ(&DC, &RC[0]);
EXPECT_EQ(&CC, &RC[1]);
EXPECT_EQ(&BC, &RC[2]);
EXPECT_EQ(&AC, &RC[3]);
EXPECT_TRUE(RC.switchInternalEdgeToCall(C, B, [&](ArrayRef<LazyCallGraph::SCC *> MergedCs) {
ASSERT_EQ(1u, MergedCs.size());
EXPECT_EQ(&CC, MergedCs[0]);
}));
ASSERT_EQ(3, RC.size());
EXPECT_EQ(&DC, &RC[0]);
EXPECT_EQ(&BC, &RC[1]);
EXPECT_EQ(&AC, &RC[2]);
EXPECT_EQ(2, BC.size());
EXPECT_EQ(&BC, CG.lookupSCC(B));
EXPECT_EQ(&BC, CG.lookupSCC(C));
}
TEST(LazyCallGraphTest, InternalRefEdgeToCallNoCycleInterleaved) {
LLVMContext Context;
std::unique_ptr<Module> M =
parseAssembly(Context, "define void @a() {\n"
"entry:\n"
" call void @b1()\n"
" call void @c1()\n"
" ret void\n"
"}\n"
"define void @b1() {\n"
"entry:\n"
" call void @c1()\n"
" call void @b2()\n"
" ret void\n"
"}\n"
"define void @c1() {\n"
"entry:\n"
" call void @b2()\n"
" call void @c2()\n"
" ret void\n"
"}\n"
"define void @b2() {\n"
"entry:\n"
" call void @c2()\n"
" call void @b3()\n"
" ret void\n"
"}\n"
"define void @c2() {\n"
"entry:\n"
" call void @b3()\n"
" call void @c3()\n"
" ret void\n"
"}\n"
"define void @b3() {\n"
"entry:\n"
" call void @c3()\n"
" call void @d()\n"
" ret void\n"
"}\n"
"define void @c3() {\n"
"entry:\n"
" store void()* @b1, void()** undef\n"
" call void @d()\n"
" ret void\n"
"}\n"
"define void @d() {\n"
"entry:\n"
" store void()* @a, void()** undef\n"
" ret void\n"
"}\n");
LazyCallGraph CG = buildCG(*M);
CG.buildRefSCCs();
auto I = CG.postorder_ref_scc_begin();
LazyCallGraph::RefSCC &RC = *I++;
EXPECT_EQ(CG.postorder_ref_scc_end(), I);
LazyCallGraph::Node &A = *CG.lookup(lookupFunction(*M, "a"));
LazyCallGraph::Node &B1 = *CG.lookup(lookupFunction(*M, "b1"));
LazyCallGraph::Node &B2 = *CG.lookup(lookupFunction(*M, "b2"));
LazyCallGraph::Node &B3 = *CG.lookup(lookupFunction(*M, "b3"));
LazyCallGraph::Node &C1 = *CG.lookup(lookupFunction(*M, "c1"));
LazyCallGraph::Node &C2 = *CG.lookup(lookupFunction(*M, "c2"));
LazyCallGraph::Node &C3 = *CG.lookup(lookupFunction(*M, "c3"));
LazyCallGraph::Node &D = *CG.lookup(lookupFunction(*M, "d"));
LazyCallGraph::SCC &AC = *CG.lookupSCC(A);
LazyCallGraph::SCC &B1C = *CG.lookupSCC(B1);
LazyCallGraph::SCC &B2C = *CG.lookupSCC(B2);
LazyCallGraph::SCC &B3C = *CG.lookupSCC(B3);
LazyCallGraph::SCC &C1C = *CG.lookupSCC(C1);
LazyCallGraph::SCC &C2C = *CG.lookupSCC(C2);
LazyCallGraph::SCC &C3C = *CG.lookupSCC(C3);
LazyCallGraph::SCC &DC = *CG.lookupSCC(D);
RC.switchTrivialInternalEdgeToRef(B3, C3);
RC.switchTrivialInternalEdgeToRef(C2, B3);
RC.switchTrivialInternalEdgeToRef(B2, C2);
RC.switchTrivialInternalEdgeToRef(C1, B2);
RC.switchTrivialInternalEdgeToRef(B1, C1);
ASSERT_EQ(8, RC.size());
EXPECT_EQ(&DC, &RC[0]);
EXPECT_EQ(&C3C, &RC[1]);
EXPECT_EQ(&B3C, &RC[2]);
EXPECT_EQ(&C2C, &RC[3]);
EXPECT_EQ(&B2C, &RC[4]);
EXPECT_EQ(&C1C, &RC[5]);
EXPECT_EQ(&B1C, &RC[6]);
EXPECT_EQ(&AC, &RC[7]);
EXPECT_FALSE(RC.switchInternalEdgeToCall(C3, B1));
ASSERT_EQ(8, RC.size());
EXPECT_EQ(&DC, &RC[0]);
EXPECT_EQ(&B3C, &RC[1]);
EXPECT_EQ(&B2C, &RC[2]);
EXPECT_EQ(&B1C, &RC[3]);
EXPECT_EQ(&C3C, &RC[4]);
EXPECT_EQ(&C2C, &RC[5]);
EXPECT_EQ(&C1C, &RC[6]);
EXPECT_EQ(&AC, &RC[7]);
}
TEST(LazyCallGraphTest, InternalRefEdgeToCallBothPartitionAndMerge) {
LLVMContext Context;
std::unique_ptr<Module> M =
parseAssembly(Context, "define void @a() {\n"
"entry:\n"
" call void @b()\n"
" call void @e()\n"
" ret void\n"
"}\n"
"define void @b() {\n"
"entry:\n"
" call void @c()\n"
" call void @d()\n"
" ret void\n"
"}\n"
"define void @c() {\n"
"entry:\n"
" call void @d()\n"
" call void @g()\n"
" ret void\n"
"}\n"
"define void @d() {\n"
"entry:\n"
" call void @e()\n"
" call void @f()\n"
" ret void\n"
"}\n"
"define void @e() {\n"
"entry:\n"
" call void @f()\n"
" ret void\n"
"}\n"
"define void @f() {\n"
"entry:\n"
" store void()* @b, void()** undef\n"
" call void @g()\n"
" ret void\n"
"}\n"
"define void @g() {\n"
"entry:\n"
" store void()* @a, void()** undef\n"
" ret void\n"
"}\n");
LazyCallGraph CG = buildCG(*M);
CG.buildRefSCCs();
auto I = CG.postorder_ref_scc_begin();
LazyCallGraph::RefSCC &RC = *I++;
EXPECT_EQ(CG.postorder_ref_scc_end(), I);
LazyCallGraph::Node &A = *CG.lookup(lookupFunction(*M, "a"));
LazyCallGraph::Node &B = *CG.lookup(lookupFunction(*M, "b"));
LazyCallGraph::Node &C = *CG.lookup(lookupFunction(*M, "c"));
LazyCallGraph::Node &D = *CG.lookup(lookupFunction(*M, "d"));
LazyCallGraph::Node &E = *CG.lookup(lookupFunction(*M, "e"));
LazyCallGraph::Node &F = *CG.lookup(lookupFunction(*M, "f"));
LazyCallGraph::Node &G = *CG.lookup(lookupFunction(*M, "g"));
LazyCallGraph::SCC &AC = *CG.lookupSCC(A);
LazyCallGraph::SCC &BC = *CG.lookupSCC(B);
LazyCallGraph::SCC &CC = *CG.lookupSCC(C);
LazyCallGraph::SCC &DC = *CG.lookupSCC(D);
LazyCallGraph::SCC &EC = *CG.lookupSCC(E);
LazyCallGraph::SCC &FC = *CG.lookupSCC(F);
LazyCallGraph::SCC &GC = *CG.lookupSCC(G);
RC.switchTrivialInternalEdgeToRef(C, D);
RC.switchTrivialInternalEdgeToRef(D, E);
ASSERT_EQ(7, RC.size());
EXPECT_EQ(&GC, &RC[0]);
EXPECT_EQ(&FC, &RC[1]);
EXPECT_EQ(&EC, &RC[2]);
EXPECT_EQ(&DC, &RC[3]);
EXPECT_EQ(&CC, &RC[4]);
EXPECT_EQ(&BC, &RC[5]);
EXPECT_EQ(&AC, &RC[6]);
EXPECT_TRUE(RC.switchInternalEdgeToCall(
F, B, [&](ArrayRef<LazyCallGraph::SCC *> MergedCs) {
ASSERT_EQ(2u, MergedCs.size());
EXPECT_EQ(&FC, MergedCs[0]);
EXPECT_EQ(&DC, MergedCs[1]);
}));
EXPECT_EQ(3, BC.size());
ASSERT_EQ(5, RC.size());
EXPECT_EQ(&GC, &RC[0]);
EXPECT_EQ(&CC, &RC[1]);
EXPECT_EQ(&BC, &RC[2]);
EXPECT_EQ(&EC, &RC[3]);
EXPECT_EQ(&AC, &RC[4]);
}
TEST(LazyCallGraphTest, HandleBlockAddress) {
LLVMContext Context;
std::unique_ptr<Module> M =
parseAssembly(Context, "define void @f() {\n"
"entry:\n"
" ret void\n"
"bb:\n"
" unreachable\n"
"}\n"
"define void @g(i8** %ptr) {\n"
"entry:\n"
" store i8* blockaddress(@f, %bb), i8** %ptr\n"
" ret void\n"
"}\n");
LazyCallGraph CG = buildCG(*M);
CG.buildRefSCCs();
auto I = CG.postorder_ref_scc_begin();
LazyCallGraph::RefSCC &FRC = *I++;
LazyCallGraph::RefSCC &GRC = *I++;
EXPECT_EQ(CG.postorder_ref_scc_end(), I);
LazyCallGraph::Node &F = *CG.lookup(lookupFunction(*M, "f"));
LazyCallGraph::Node &G = *CG.lookup(lookupFunction(*M, "g"));
EXPECT_EQ(&FRC, CG.lookupRefSCC(F));
EXPECT_EQ(&GRC, CG.lookupRefSCC(G));
EXPECT_FALSE(GRC.isParentOf(FRC));
EXPECT_FALSE(FRC.isParentOf(GRC));
}
TEST(LazyCallGraphTest, HandleBlockAddress2) {
LLVMContext Context;
std::unique_ptr<Module> M =
parseAssembly(Context, "define void @f() {\n"
" ret void\n"
"}\n"
"define void @g(i8** %ptr) {\n"
"bb:\n"
" store i8* blockaddress(@g, %bb), i8** %ptr\n"
" ret void\n"
"}\n");
LazyCallGraph CG = buildCG(*M);
CG.buildRefSCCs();
auto I = CG.postorder_ref_scc_begin();
LazyCallGraph::RefSCC &FRC = *I++;
LazyCallGraph::RefSCC &GRC = *I++;
EXPECT_EQ(CG.postorder_ref_scc_end(), I);
LazyCallGraph::Node &F = *CG.lookup(lookupFunction(*M, "f"));
LazyCallGraph::Node &G = *CG.lookup(lookupFunction(*M, "g"));
EXPECT_EQ(&FRC, CG.lookupRefSCC(F));
EXPECT_EQ(&GRC, CG.lookupRefSCC(G));
EXPECT_FALSE(GRC.isParentOf(FRC));
EXPECT_FALSE(FRC.isParentOf(GRC));
}
TEST(LazyCallGraphTest, ReplaceNodeFunction) {
LLVMContext Context;
std::unique_ptr<Module> M =
parseAssembly(Context,
"define void @a(i8** %ptr) {\n"
"entry:\n"
" store i8* bitcast (void(i8**)* @d to i8*), i8** %ptr\n"
" ret void\n"
"}\n"
"define void @b(i8** %ptr) {\n"
"entry:\n"
" store i8* bitcast (void(i8**)* @d to i8*), i8** %ptr\n"
" store i8* bitcast (void(i8**)* @d to i8*), i8** %ptr\n"
" call void @d(i8** %ptr)"
" ret void\n"
"}\n"
"define void @c(i8** %ptr) {\n"
"entry:\n"
" call void @d(i8** %ptr)"
" call void @d(i8** %ptr)"
" store i8* bitcast (void(i8**)* @d to i8*), i8** %ptr\n"
" ret void\n"
"}\n"
"define void @d(i8** %ptr) {\n"
"entry:\n"
" store i8* bitcast (void(i8**)* @b to i8*), i8** %ptr\n"
" call void @c(i8** %ptr)"
" call void @d(i8** %ptr)"
" store i8* bitcast (void(i8**)* @d to i8*), i8** %ptr\n"
" ret void\n"
"}\n");
LazyCallGraph CG = buildCG(*M);
CG.buildRefSCCs();
auto I = CG.postorder_ref_scc_begin();
LazyCallGraph::RefSCC &RC1 = *I++;
LazyCallGraph::RefSCC &RC2 = *I++;
EXPECT_EQ(CG.postorder_ref_scc_end(), I);
ASSERT_EQ(2, RC1.size());
LazyCallGraph::SCC &C1 = RC1[0];
LazyCallGraph::SCC &C2 = RC1[1];
LazyCallGraph::Node &AN = *CG.lookup(lookupFunction(*M, "a"));
LazyCallGraph::Node &BN = *CG.lookup(lookupFunction(*M, "b"));
LazyCallGraph::Node &CN = *CG.lookup(lookupFunction(*M, "c"));
LazyCallGraph::Node &DN = *CG.lookup(lookupFunction(*M, "d"));
EXPECT_EQ(&C1, CG.lookupSCC(DN));
EXPECT_EQ(&C1, CG.lookupSCC(CN));
EXPECT_EQ(&C2, CG.lookupSCC(BN));
EXPECT_EQ(&RC1, CG.lookupRefSCC(DN));
EXPECT_EQ(&RC1, CG.lookupRefSCC(CN));
EXPECT_EQ(&RC1, CG.lookupRefSCC(BN));
EXPECT_EQ(&RC2, CG.lookupRefSCC(AN));
Function &D = DN.getFunction();
Function &E = *Function::Create(D.getFunctionType(), D.getLinkage(), "e");
D.getParent()->getFunctionList().insert(D.getIterator(), &E);
D.replaceAllUsesWith(&E);
E.getBasicBlockList().splice(E.begin(), D.getBasicBlockList());
D.arg_begin()->replaceAllUsesWith(&*E.arg_begin());
E.arg_begin()->takeName(&*D.arg_begin());
RC1.replaceNodeFunction(DN, E);
EXPECT_EQ(&E, &DN.getFunction());
EXPECT_EQ(&DN, &(*CN)[DN].getNode());
EXPECT_EQ(&DN, &(*BN)[DN].getNode());
}
TEST(LazyCallGraphTest, RemoveFunctionWithSpurriousRef) {
LLVMContext Context;
std::unique_ptr<Module> M =
parseAssembly(Context,
"define void @a(i8** %ptr) {\n"
"entry:\n"
" store i8* bitcast (void(i8**)* @d to i8*), i8** %ptr\n"
" ret void\n"
"}\n"
"define void @b(i8** %ptr) {\n"
"entry:\n"
" store i8* bitcast (void(i8**)* @c to i8*), i8** %ptr\n"
" ret void\n"
"}\n"
"define void @c(i8** %ptr) {\n"
"entry:\n"
" call void @d(i8** %ptr)"
" ret void\n"
"}\n"
"define void @d(i8** %ptr) {\n"
"entry:\n"
" call void @c(i8** %ptr)"
" store i8* bitcast (void(i8**)* @b to i8*), i8** %ptr\n"
" ret void\n"
"}\n"
"define void @dead() {\n"
"entry:\n"
" ret void\n"
"}\n");
LazyCallGraph CG = buildCG(*M);
LazyCallGraph::Node &AN = CG.get(lookupFunction(*M, "a"));
LazyCallGraph::Node &BN = CG.get(lookupFunction(*M, "b"));
LazyCallGraph::Node &CN = CG.get(lookupFunction(*M, "c"));
LazyCallGraph::Node &DN = CG.get(lookupFunction(*M, "d"));
LazyCallGraph::Node &DeadN = CG.get(lookupFunction(*M, "dead"));
AN.populate();
BN.populate();
CN.populate();
DN.populate();
DeadN.populate();
CG.insertEdge(AN, DeadN, LazyCallGraph::Edge::Ref);
CG.insertEdge(BN, DeadN, LazyCallGraph::Edge::Ref);
CG.insertEdge(CN, DeadN, LazyCallGraph::Edge::Ref);
CG.insertEdge(DN, DeadN, LazyCallGraph::Edge::Ref);
CG.buildRefSCCs();
auto I = CG.postorder_ref_scc_begin();
LazyCallGraph::RefSCC &DeadRC = *I++;
LazyCallGraph::RefSCC &RC1 = *I++;
LazyCallGraph::RefSCC &RC2 = *I++;
EXPECT_EQ(CG.postorder_ref_scc_end(), I);
ASSERT_EQ(2, RC1.size());
LazyCallGraph::SCC &C1 = RC1[0];
LazyCallGraph::SCC &C2 = RC1[1];
EXPECT_EQ(&DeadRC, CG.lookupRefSCC(DeadN));
EXPECT_EQ(&C1, CG.lookupSCC(DN));
EXPECT_EQ(&C1, CG.lookupSCC(CN));
EXPECT_EQ(&C2, CG.lookupSCC(BN));
EXPECT_EQ(&RC1, CG.lookupRefSCC(DN));
EXPECT_EQ(&RC1, CG.lookupRefSCC(CN));
EXPECT_EQ(&RC1, CG.lookupRefSCC(BN));
EXPECT_EQ(&RC2, CG.lookupRefSCC(AN));
CG.removeDeadFunction(DeadN.getFunction());
I = CG.postorder_ref_scc_begin();
EXPECT_EQ(&RC1, &*I++);
EXPECT_EQ(&RC2, &*I++);
EXPECT_EQ(CG.postorder_ref_scc_end(), I);
}
TEST(LazyCallGraphTest, AddSplitFunction1) {
LLVMContext Context;
std::unique_ptr<Module> M = parseAssembly(Context, "define void @f() {\n"
" ret void\n"
"}\n");
LazyCallGraph CG = buildCG(*M);
Function &F = lookupFunction(*M, "f");
LazyCallGraph::Node &FN = CG.get(F);
CG.buildRefSCCs();
auto I = CG.postorder_ref_scc_begin();
LazyCallGraph::RefSCC *ORC = &*I++;
EXPECT_EQ(CG.postorder_ref_scc_end(), I);
auto *G = Function::Create(F.getFunctionType(), F.getLinkage(),
F.getAddressSpace(), "g", F.getParent());
BasicBlock *GBB = BasicBlock::Create(Context, "", G);
(void)ReturnInst::Create(Context, GBB);
(void)CallInst::Create(G, {}, "", &*F.getEntryBlock().begin());
EXPECT_FALSE(verifyModule(*M, &errs()));
CG.addSplitFunction(F, *G);
LazyCallGraph::Node *GN = CG.lookup(*G);
EXPECT_TRUE(GN);
I = CG.postorder_ref_scc_begin();
LazyCallGraph::RefSCC *RC1 = &*I++;
EXPECT_EQ(RC1, CG.lookupRefSCC(*GN));
LazyCallGraph::RefSCC *RC2 = &*I++;
EXPECT_EQ(RC2, ORC);
EXPECT_EQ(RC2, CG.lookupRefSCC(FN));
EXPECT_EQ(CG.postorder_ref_scc_end(), I);
}
TEST(LazyCallGraphTest, AddSplitFunction2) {
LLVMContext Context;
std::unique_ptr<Module> M = parseAssembly(Context, "define void @f() {\n"
" ret void\n"
"}\n");
LazyCallGraph CG = buildCG(*M);
Function &F = lookupFunction(*M, "f");
LazyCallGraph::Node &FN = CG.get(F);
CG.buildRefSCCs();
auto I = CG.postorder_ref_scc_begin();
LazyCallGraph::RefSCC *ORC = &*I++;
EXPECT_EQ(CG.postorder_ref_scc_end(), I);
auto *G = Function::Create(F.getFunctionType(), F.getLinkage(),
F.getAddressSpace(), "g", F.getParent());
BasicBlock *GBB = BasicBlock::Create(Context, "", G);
(void)ReturnInst::Create(Context, GBB);
(void)CastInst::CreatePointerCast(G, Type::getInt8PtrTy(Context), "",
&*F.getEntryBlock().begin());
EXPECT_FALSE(verifyModule(*M, &errs()));
CG.addSplitFunction(F, *G);
LazyCallGraph::Node *GN = CG.lookup(*G);
EXPECT_TRUE(GN);
I = CG.postorder_ref_scc_begin();
LazyCallGraph::RefSCC *RC1 = &*I++;
EXPECT_EQ(RC1, CG.lookupRefSCC(*GN));
LazyCallGraph::RefSCC *RC2 = &*I++;
EXPECT_EQ(RC2, ORC);
EXPECT_EQ(RC2, CG.lookupRefSCC(FN));
EXPECT_EQ(CG.postorder_ref_scc_end(), I);
}
TEST(LazyCallGraphTest, AddSplitFunction3) {
LLVMContext Context;
std::unique_ptr<Module> M = parseAssembly(Context, "define void @f() {\n"
" ret void\n"
"}\n");
LazyCallGraph CG = buildCG(*M);
Function &F = lookupFunction(*M, "f");
LazyCallGraph::Node &FN = CG.get(F);
CG.buildRefSCCs();
auto I = CG.postorder_ref_scc_begin();
LazyCallGraph::RefSCC *ORC = &*I++;
EXPECT_EQ(CG.postorder_ref_scc_end(), I);
auto *G = Function::Create(F.getFunctionType(), F.getLinkage(),
F.getAddressSpace(), "g", F.getParent());
BasicBlock *GBB = BasicBlock::Create(Context, "", G);
(void)CastInst::CreatePointerCast(&F, Type::getInt8PtrTy(Context), "", GBB);
(void)ReturnInst::Create(Context, GBB);
(void)CallInst::Create(G, {}, "", &*F.getEntryBlock().begin());
EXPECT_FALSE(verifyModule(*M, &errs()));
CG.addSplitFunction(F, *G);
LazyCallGraph::Node *GN = CG.lookup(*G);
EXPECT_TRUE(GN);
I = CG.postorder_ref_scc_begin();
LazyCallGraph::RefSCC *RC = &*I++;
EXPECT_EQ(RC, CG.lookupRefSCC(*GN));
EXPECT_EQ(RC, ORC);
EXPECT_EQ(CG.postorder_ref_scc_end(), I);
EXPECT_EQ(2, RC->size());
EXPECT_EQ(CG.lookupSCC(*GN), &(*RC)[0]);
EXPECT_EQ(CG.lookupSCC(FN), &(*RC)[1]);
}
TEST(LazyCallGraphTest, AddSplitFunction4) {
LLVMContext Context;
std::unique_ptr<Module> M = parseAssembly(Context, "define void @f() {\n"
" ret void\n"
"}\n");
LazyCallGraph CG = buildCG(*M);
Function &F = lookupFunction(*M, "f");
LazyCallGraph::Node &FN = CG.get(F);
CG.buildRefSCCs();
auto I = CG.postorder_ref_scc_begin();
LazyCallGraph::RefSCC *ORC = &*I++;
EXPECT_EQ(CG.postorder_ref_scc_end(), I);
auto *G = Function::Create(F.getFunctionType(), F.getLinkage(),
F.getAddressSpace(), "g", F.getParent());
BasicBlock *GBB = BasicBlock::Create(Context, "", G);
(void)CastInst::CreatePointerCast(&F, Type::getInt8PtrTy(Context), "", GBB);
(void)ReturnInst::Create(Context, GBB);
(void)CastInst::CreatePointerCast(G, Type::getInt8PtrTy(Context), "",
&*F.getEntryBlock().begin());
EXPECT_FALSE(verifyModule(*M, &errs()));
CG.addSplitFunction(F, *G);
LazyCallGraph::Node *GN = CG.lookup(*G);
EXPECT_TRUE(GN);
I = CG.postorder_ref_scc_begin();
LazyCallGraph::RefSCC *RC = &*I++;
EXPECT_EQ(RC, CG.lookupRefSCC(*GN));
EXPECT_EQ(RC, ORC);
EXPECT_EQ(CG.postorder_ref_scc_end(), I);
EXPECT_EQ(2, RC->size());
EXPECT_EQ(&CG.lookupSCC(*GN)->getOuterRefSCC(), RC);
EXPECT_EQ(&CG.lookupSCC(FN)->getOuterRefSCC(), RC);
}
TEST(LazyCallGraphTest, AddSplitFunction5) {
LLVMContext Context;
std::unique_ptr<Module> M = parseAssembly(Context, "define void @f() {\n"
" ret void\n"
"}\n");
LazyCallGraph CG = buildCG(*M);
Function &F = lookupFunction(*M, "f");
LazyCallGraph::Node &FN = CG.get(F);
CG.buildRefSCCs();
auto I = CG.postorder_ref_scc_begin();
LazyCallGraph::RefSCC *ORC = &*I++;
EXPECT_EQ(CG.postorder_ref_scc_end(), I);
auto *G = Function::Create(F.getFunctionType(), F.getLinkage(),
F.getAddressSpace(), "g", F.getParent());
BasicBlock *GBB = BasicBlock::Create(Context, "", G);
(void)CallInst::Create(&F, {}, "", GBB);
(void)ReturnInst::Create(Context, GBB);
(void)CastInst::CreatePointerCast(G, Type::getInt8PtrTy(Context), "",
&*F.getEntryBlock().begin());
EXPECT_FALSE(verifyModule(*M, &errs()));
CG.addSplitFunction(F, *G);
LazyCallGraph::Node *GN = CG.lookup(*G);
EXPECT_TRUE(GN);
I = CG.postorder_ref_scc_begin();
LazyCallGraph::RefSCC *RC = &*I++;
EXPECT_EQ(RC, CG.lookupRefSCC(*GN));
EXPECT_EQ(RC, ORC);
EXPECT_EQ(CG.postorder_ref_scc_end(), I);
EXPECT_EQ(2, RC->size());
EXPECT_EQ(CG.lookupSCC(FN), &(*RC)[0]);
EXPECT_EQ(CG.lookupSCC(*GN), &(*RC)[1]);
}
TEST(LazyCallGraphTest, AddSplitFunction6) {
LLVMContext Context;
std::unique_ptr<Module> M = parseAssembly(Context, "define void @f() {\n"
" ret void\n"
"}\n");
LazyCallGraph CG = buildCG(*M);
Function &F = lookupFunction(*M, "f");
LazyCallGraph::Node &FN = CG.get(F);
CG.buildRefSCCs();
auto I = CG.postorder_ref_scc_begin();
LazyCallGraph::RefSCC *ORC = &*I++;
EXPECT_EQ(CG.postorder_ref_scc_end(), I);
auto *G = Function::Create(F.getFunctionType(), F.getLinkage(),
F.getAddressSpace(), "g", F.getParent());
BasicBlock *GBB = BasicBlock::Create(Context, "", G);
(void)CallInst::Create(&F, {}, "", GBB);
(void)ReturnInst::Create(Context, GBB);
(void)CallInst::Create(G, {}, "", &*F.getEntryBlock().begin());
EXPECT_FALSE(verifyModule(*M, &errs()));
CG.addSplitFunction(F, *G);
LazyCallGraph::Node *GN = CG.lookup(*G);
EXPECT_TRUE(GN);
I = CG.postorder_ref_scc_begin();
LazyCallGraph::RefSCC *RC = &*I++;
EXPECT_EQ(RC, CG.lookupRefSCC(*GN));
EXPECT_EQ(RC, ORC);
EXPECT_EQ(CG.postorder_ref_scc_end(), I);
EXPECT_EQ(1, RC->size());
EXPECT_EQ(CG.lookupSCC(FN), &(*RC)[0]);
EXPECT_EQ(CG.lookupSCC(*GN), &(*RC)[0]);
}
TEST(LazyCallGraphTest, AddSplitFunction7) {
LLVMContext Context;
std::unique_ptr<Module> M = parseAssembly(Context, "define void @f() {\n"
" call void @f2()\n"
" ret void\n"
"}\n"
"define void @f2() {\n"
" call void @f()\n"
" ret void\n"
"}\n");
LazyCallGraph CG = buildCG(*M);
Function &F = lookupFunction(*M, "f");
LazyCallGraph::Node &FN = CG.get(F);
Function &F2 = lookupFunction(*M, "f2");
LazyCallGraph::Node &F2N = CG.get(F2);
CG.buildRefSCCs();
auto I = CG.postorder_ref_scc_begin();
LazyCallGraph::RefSCC *ORC = &*I++;
EXPECT_EQ(CG.postorder_ref_scc_end(), I);
auto *G = Function::Create(F.getFunctionType(), F.getLinkage(),
F.getAddressSpace(), "g", F.getParent());
BasicBlock *GBB = BasicBlock::Create(Context, "", G);
(void)CallInst::Create(&F2, {}, "", GBB);
(void)ReturnInst::Create(Context, GBB);
(void)CallInst::Create(G, {}, "", &*F.getEntryBlock().begin());
EXPECT_FALSE(verifyModule(*M, &errs()));
CG.addSplitFunction(F, *G);
LazyCallGraph::Node *GN = CG.lookup(*G);
EXPECT_TRUE(GN);
I = CG.postorder_ref_scc_begin();
LazyCallGraph::RefSCC *RC = &*I++;
EXPECT_EQ(RC, CG.lookupRefSCC(*GN));
EXPECT_EQ(RC, ORC);
EXPECT_EQ(CG.postorder_ref_scc_end(), I);
EXPECT_EQ(1, RC->size());
EXPECT_EQ(CG.lookupSCC(FN), &(*RC)[0]);
EXPECT_EQ(CG.lookupSCC(F2N), &(*RC)[0]);
EXPECT_EQ(CG.lookupSCC(*GN), &(*RC)[0]);
}
TEST(LazyCallGraphTest, AddSplitFunction8) {
LLVMContext Context;
std::unique_ptr<Module> M = parseAssembly(Context, "define void @f() {\n"
" call void @f2()\n"
" ret void\n"
"}\n"
"define void @f2() {\n"
" call void @f()\n"
" ret void\n"
"}\n");
LazyCallGraph CG = buildCG(*M);
Function &F = lookupFunction(*M, "f");
LazyCallGraph::Node &FN = CG.get(F);
Function &F2 = lookupFunction(*M, "f2");
LazyCallGraph::Node &F2N = CG.get(F2);
CG.buildRefSCCs();
auto I = CG.postorder_ref_scc_begin();
LazyCallGraph::RefSCC *ORC = &*I++;
EXPECT_EQ(CG.postorder_ref_scc_end(), I);
auto *G = Function::Create(F.getFunctionType(), F.getLinkage(),
F.getAddressSpace(), "g", F.getParent());
BasicBlock *GBB = BasicBlock::Create(Context, "", G);
(void)CallInst::Create(&F2, {}, "", GBB);
(void)ReturnInst::Create(Context, GBB);
(void)CastInst::CreatePointerCast(G, Type::getInt8PtrTy(Context), "",
&*F.getEntryBlock().begin());
EXPECT_FALSE(verifyModule(*M, &errs()));
CG.addSplitFunction(F, *G);
LazyCallGraph::Node *GN = CG.lookup(*G);
EXPECT_TRUE(GN);
I = CG.postorder_ref_scc_begin();
LazyCallGraph::RefSCC *RC = &*I++;
EXPECT_EQ(RC, CG.lookupRefSCC(*GN));
EXPECT_EQ(RC, ORC);
EXPECT_EQ(CG.postorder_ref_scc_end(), I);
EXPECT_EQ(2, RC->size());
EXPECT_EQ(CG.lookupSCC(FN), &(*RC)[0]);
EXPECT_EQ(CG.lookupSCC(F2N), &(*RC)[0]);
EXPECT_EQ(CG.lookupSCC(*GN), &(*RC)[1]);
}
TEST(LazyCallGraphTest, AddSplitFunction9) {
LLVMContext Context;
std::unique_ptr<Module> M = parseAssembly(Context, "define void @f() {\n"
" call void @f2()\n"
" ret void\n"
"}\n"
"define void @f2() {\n"
" call void @f()\n"
" ret void\n"
"}\n");
LazyCallGraph CG = buildCG(*M);
Function &F = lookupFunction(*M, "f");
LazyCallGraph::Node &FN = CG.get(F);
Function &F2 = lookupFunction(*M, "f2");
LazyCallGraph::Node &F2N = CG.get(F2);
CG.buildRefSCCs();
auto I = CG.postorder_ref_scc_begin();
LazyCallGraph::RefSCC *ORC = &*I++;
EXPECT_EQ(CG.postorder_ref_scc_end(), I);
auto *G = Function::Create(F.getFunctionType(), F.getLinkage(),
F.getAddressSpace(), "g", F.getParent());
BasicBlock *GBB = BasicBlock::Create(Context, "", G);
(void)CastInst::CreatePointerCast(&F2, Type::getInt8PtrTy(Context), "", GBB);
(void)ReturnInst::Create(Context, GBB);
(void)CallInst::Create(G, {}, "", &*F.getEntryBlock().begin());
EXPECT_FALSE(verifyModule(*M, &errs()));
CG.addSplitFunction(F, *G);
LazyCallGraph::Node *GN = CG.lookup(*G);
EXPECT_TRUE(GN);
I = CG.postorder_ref_scc_begin();
LazyCallGraph::RefSCC *RC = &*I++;
EXPECT_EQ(RC, CG.lookupRefSCC(*GN));
EXPECT_EQ(RC, ORC);
EXPECT_EQ(CG.postorder_ref_scc_end(), I);
EXPECT_EQ(2, RC->size());
EXPECT_EQ(CG.lookupSCC(*GN), &(*RC)[0]);
EXPECT_EQ(CG.lookupSCC(FN), &(*RC)[1]);
EXPECT_EQ(CG.lookupSCC(F2N), &(*RC)[1]);
}
TEST(LazyCallGraphTest, AddSplitFunctions1) {
LLVMContext Context;
std::unique_ptr<Module> M = parseAssembly(Context, "define void @f() {\n"
" ret void\n"
"}\n");
LazyCallGraph CG = buildCG(*M);
Function &F = lookupFunction(*M, "f");
LazyCallGraph::Node &FN = CG.get(F);
CG.buildRefSCCs();
auto I = CG.postorder_ref_scc_begin();
LazyCallGraph::RefSCC *ORC = &*I++;
EXPECT_EQ(CG.postorder_ref_scc_end(), I);
auto *G = Function::Create(F.getFunctionType(), F.getLinkage(),
F.getAddressSpace(), "g", F.getParent());
BasicBlock *GBB = BasicBlock::Create(Context, "", G);
(void)ReturnInst::Create(Context, GBB);
(void)CastInst::CreatePointerCast(G, Type::getInt8PtrTy(Context), "",
&*F.getEntryBlock().begin());
EXPECT_FALSE(verifyModule(*M, &errs()));
CG.addSplitRefRecursiveFunctions(F, SmallVector<Function *, 1>({G}));
LazyCallGraph::Node *GN = CG.lookup(*G);
EXPECT_TRUE(GN);
I = CG.postorder_ref_scc_begin();
LazyCallGraph::RefSCC *RC1 = &*I++;
EXPECT_EQ(RC1, CG.lookupRefSCC(*GN));
LazyCallGraph::RefSCC *RC2 = &*I++;
EXPECT_EQ(RC2, ORC);
EXPECT_EQ(RC2, CG.lookupRefSCC(FN));
EXPECT_EQ(CG.postorder_ref_scc_end(), I);
}
TEST(LazyCallGraphTest, AddSplitFunctions2) {
LLVMContext Context;
std::unique_ptr<Module> M = parseAssembly(Context, "define void @f() {\n"
" ret void\n"
"}\n");
LazyCallGraph CG = buildCG(*M);
Function &F = lookupFunction(*M, "f");
LazyCallGraph::Node &FN = CG.get(F);
CG.buildRefSCCs();
auto I = CG.postorder_ref_scc_begin();
LazyCallGraph::RefSCC *ORC = &*I++;
EXPECT_EQ(CG.postorder_ref_scc_end(), I);
auto *G = Function::Create(F.getFunctionType(), F.getLinkage(),
F.getAddressSpace(), "g", F.getParent());
BasicBlock *GBB = BasicBlock::Create(Context, "", G);
(void)CastInst::CreatePointerCast(&F, Type::getInt8PtrTy(Context), "", GBB);
(void)ReturnInst::Create(Context, GBB);
(void)CastInst::CreatePointerCast(G, Type::getInt8PtrTy(Context), "",
&*F.getEntryBlock().begin());
EXPECT_FALSE(verifyModule(*M, &errs()));
CG.addSplitRefRecursiveFunctions(F, SmallVector<Function *, 1>({G}));
LazyCallGraph::Node *GN = CG.lookup(*G);
EXPECT_TRUE(GN);
I = CG.postorder_ref_scc_begin();
LazyCallGraph::RefSCC *RC = &*I++;
EXPECT_EQ(RC, CG.lookupRefSCC(*GN));
EXPECT_EQ(RC, ORC);
EXPECT_EQ(CG.postorder_ref_scc_end(), I);
EXPECT_EQ(2, RC->size());
EXPECT_EQ(&CG.lookupSCC(*GN)->getOuterRefSCC(), RC);
EXPECT_EQ(&CG.lookupSCC(FN)->getOuterRefSCC(), RC);
}
TEST(LazyCallGraphTest, AddSplitFunctions3) {
LLVMContext Context;
std::unique_ptr<Module> M = parseAssembly(Context, "define void @f() {\n"
" ret void\n"
"}\n");
LazyCallGraph CG = buildCG(*M);
Function &F = lookupFunction(*M, "f");
LazyCallGraph::Node &FN = CG.get(F);
CG.buildRefSCCs();
auto I = CG.postorder_ref_scc_begin();
LazyCallGraph::RefSCC *ORC = &*I++;
EXPECT_EQ(CG.postorder_ref_scc_end(), I);
auto *G1 = Function::Create(F.getFunctionType(), F.getLinkage(),
F.getAddressSpace(), "g1", F.getParent());
auto *G2 = Function::Create(F.getFunctionType(), F.getLinkage(),
F.getAddressSpace(), "g2", F.getParent());
BasicBlock *G1BB = BasicBlock::Create(Context, "", G1);
BasicBlock *G2BB = BasicBlock::Create(Context, "", G2);
(void)CastInst::CreatePointerCast(G2, Type::getInt8PtrTy(Context), "", G1BB);
(void)CastInst::CreatePointerCast(G1, Type::getInt8PtrTy(Context), "", G2BB);
(void)ReturnInst::Create(Context, G1BB);
(void)ReturnInst::Create(Context, G2BB);
(void)CastInst::CreatePointerCast(G1, Type::getInt8PtrTy(Context), "",
&*F.getEntryBlock().begin());
(void)CastInst::CreatePointerCast(G2, Type::getInt8PtrTy(Context), "",
&*F.getEntryBlock().begin());
EXPECT_FALSE(verifyModule(*M, &errs()));
CG.addSplitRefRecursiveFunctions(F, SmallVector<Function *, 1>({G1, G2}));
LazyCallGraph::Node *G1N = CG.lookup(*G1);
EXPECT_TRUE(G1N);
LazyCallGraph::Node *G2N = CG.lookup(*G2);
EXPECT_TRUE(G2N);
I = CG.postorder_ref_scc_begin();
LazyCallGraph::RefSCC *RC1 = &*I++;
EXPECT_EQ(2, RC1->size());
EXPECT_EQ(RC1, CG.lookupRefSCC(*G1N));
EXPECT_EQ(RC1, CG.lookupRefSCC(*G2N));
LazyCallGraph::RefSCC *RC2 = &*I++;
EXPECT_EQ(RC2, ORC);
EXPECT_EQ(RC2, CG.lookupRefSCC(FN));
EXPECT_EQ(CG.postorder_ref_scc_end(), I);
}
TEST(LazyCallGraphTest, AddSplitFunctions4) {
LLVMContext Context;
std::unique_ptr<Module> M = parseAssembly(Context, "define void @f() {\n"
" ret void\n"
"}\n");
LazyCallGraph CG = buildCG(*M);
Function &F = lookupFunction(*M, "f");
LazyCallGraph::Node &FN = CG.get(F);
CG.buildRefSCCs();
auto I = CG.postorder_ref_scc_begin();
LazyCallGraph::RefSCC *ORC = &*I++;
EXPECT_EQ(CG.postorder_ref_scc_end(), I);
auto *G1 = Function::Create(F.getFunctionType(), F.getLinkage(),
F.getAddressSpace(), "g1", F.getParent());
auto *G2 = Function::Create(F.getFunctionType(), F.getLinkage(),
F.getAddressSpace(), "g2", F.getParent());
BasicBlock *G1BB = BasicBlock::Create(Context, "", G1);
BasicBlock *G2BB = BasicBlock::Create(Context, "", G2);
(void)CastInst::CreatePointerCast(G2, Type::getInt8PtrTy(Context), "", G1BB);
(void)CastInst::CreatePointerCast(G1, Type::getInt8PtrTy(Context), "", G2BB);
(void)CastInst::CreatePointerCast(&F, Type::getInt8PtrTy(Context), "", G2BB);
(void)ReturnInst::Create(Context, G1BB);
(void)ReturnInst::Create(Context, G2BB);
(void)CastInst::CreatePointerCast(G1, Type::getInt8PtrTy(Context), "",
&*F.getEntryBlock().begin());
(void)CastInst::CreatePointerCast(G2, Type::getInt8PtrTy(Context), "",
&*F.getEntryBlock().begin());
EXPECT_FALSE(verifyModule(*M, &errs()));
CG.addSplitRefRecursiveFunctions(F, SmallVector<Function *, 1>({G1, G2}));
LazyCallGraph::Node *G1N = CG.lookup(*G1);
EXPECT_TRUE(G1N);
LazyCallGraph::Node *G2N = CG.lookup(*G2);
EXPECT_TRUE(G2N);
I = CG.postorder_ref_scc_begin();
LazyCallGraph::RefSCC *RC = &*I++;
EXPECT_EQ(RC, ORC);
EXPECT_EQ(CG.postorder_ref_scc_end(), I);
EXPECT_EQ(3, RC->size());
EXPECT_EQ(&CG.lookupSCC(FN)->getOuterRefSCC(), RC);
EXPECT_EQ(&CG.lookupSCC(*G1N)->getOuterRefSCC(), RC);
EXPECT_EQ(&CG.lookupSCC(*G2N)->getOuterRefSCC(), RC);
EXPECT_EQ(RC, CG.lookupRefSCC(*G1N));
EXPECT_EQ(RC, CG.lookupRefSCC(*G2N));
}
TEST(LazyCallGraphTest, AddSplitFunctions5) {
LLVMContext Context;
std::unique_ptr<Module> M =
parseAssembly(Context, "define void @f() {\n"
" %1 = bitcast void ()* @f2 to i8*\n"
" ret void\n"
"}\n"
"define void @f2() {\n"
" call void @f()\n"
" ret void\n"
"}\n");
LazyCallGraph CG = buildCG(*M);
Function &F = lookupFunction(*M, "f");
LazyCallGraph::Node &FN = CG.get(F);
Function &F2 = lookupFunction(*M, "f2");
LazyCallGraph::Node &F2N = CG.get(F);
CG.buildRefSCCs();
auto I = CG.postorder_ref_scc_begin();
LazyCallGraph::RefSCC *ORC = &*I++;
EXPECT_EQ(CG.postorder_ref_scc_end(), I);
auto *G1 = Function::Create(F.getFunctionType(), F.getLinkage(),
F.getAddressSpace(), "g1", F.getParent());
auto *G2 = Function::Create(F.getFunctionType(), F.getLinkage(),
F.getAddressSpace(), "g2", F.getParent());
BasicBlock *G1BB = BasicBlock::Create(Context, "", G1);
BasicBlock *G2BB = BasicBlock::Create(Context, "", G2);
(void)CastInst::CreatePointerCast(G2, Type::getInt8PtrTy(Context), "", G1BB);
(void)CastInst::CreatePointerCast(G1, Type::getInt8PtrTy(Context), "", G2BB);
(void)CastInst::CreatePointerCast(&F2, Type::getInt8PtrTy(Context), "", G2BB);
(void)ReturnInst::Create(Context, G1BB);
(void)ReturnInst::Create(Context, G2BB);
(void)CastInst::CreatePointerCast(G1, Type::getInt8PtrTy(Context), "",
&*F.getEntryBlock().begin());
(void)CastInst::CreatePointerCast(G2, Type::getInt8PtrTy(Context), "",
&*F.getEntryBlock().begin());
EXPECT_FALSE(verifyModule(*M, &errs()));
CG.addSplitRefRecursiveFunctions(F, SmallVector<Function *, 1>({G1, G2}));
LazyCallGraph::Node *G1N = CG.lookup(*G1);
EXPECT_TRUE(G1N);
LazyCallGraph::Node *G2N = CG.lookup(*G2);
EXPECT_TRUE(G2N);
I = CG.postorder_ref_scc_begin();
LazyCallGraph::RefSCC *RC = &*I++;
EXPECT_EQ(4, RC->size());
EXPECT_EQ(RC, ORC);
EXPECT_EQ(RC, CG.lookupRefSCC(*G1N));
EXPECT_EQ(RC, CG.lookupRefSCC(*G2N));
EXPECT_EQ(RC, CG.lookupRefSCC(FN));
EXPECT_EQ(RC, CG.lookupRefSCC(F2N));
EXPECT_EQ(CG.postorder_ref_scc_end(), I);
}
}