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