| 1 |
/* Find prime paths |
| 2 |
Copyright (C) 2024-2025 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
This file is part of GCC. |
| 5 |
|
| 6 |
GCC is free software; you can redistribute it and/or modify it under |
| 7 |
the terms of the GNU General Public License as published by the Free |
| 8 |
Software Foundation; either version 3, or (at your option) any later |
| 9 |
version. |
| 10 |
|
| 11 |
GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 |
WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 |
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 |
for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with GCC; see the file COPYING3. If not see |
| 18 |
<http://www.gnu.org/licenses/>. */ |
| 19 |
|
| 20 |
#include "config.h" |
| 21 |
#include "system.h" |
| 22 |
#include "coretypes.h" |
| 23 |
#include "obstack.h" |
| 24 |
#include "sbitmap.h" |
| 25 |
#include "vec.h" |
| 26 |
#include "graphds.h" |
| 27 |
#include "selftest.h" |
| 28 |
|
| 29 |
namespace |
| 30 |
{ |
| 31 |
|
| 32 |
/* Counter for the number of candidate paths to generate before giving up. It |
| 33 |
is neater to use a global because it has to be checked deep in helper |
| 34 |
functions, which may also suffer under path explosion. It is a heuristic |
| 35 |
guaranteed to overshoot the number of actual paths (which is difficult to |
| 36 |
estimate), and is intended to give up on (absurdly) large functions with |
| 37 |
millions of paths, not be a high fidelity rejection mechanism. This is |
| 38 |
essentially an exception. */ |
| 39 |
size_t approx_limit; |
| 40 |
|
| 41 |
/* Reset the threshold to APPROX when a function is too complex and finding |
| 42 |
paths should give up. */ |
| 43 |
void |
| 44 |
limit_reset (size_t approx) |
| 45 |
{ |
| 46 |
approx_limit = approx; |
| 47 |
} |
| 48 |
|
| 49 |
/* Record approximately APPROX new paths. Returns true if the limit is |
| 50 |
exceeded and coverage should give up. */ |
| 51 |
bool |
| 52 |
limit_checked_add (size_t approx) |
| 53 |
{ |
| 54 |
approx_limit -= approx < approx_limit ? approx : approx_limit; |
| 55 |
return approx_limit == 0; |
| 56 |
}; |
| 57 |
|
| 58 |
/* Check if adding APPROX would exceed the path limit. This is necessary when |
| 59 |
(pessimistically counted) trie insertions would exceed the limit and yields |
| 60 |
a partial result, when the path count would drop below the limit again once |
| 61 |
redundancies are eliminated. */ |
| 62 |
bool |
| 63 |
limit_exceed_p (size_t approx) |
| 64 |
{ |
| 65 |
return approx > approx_limit; |
| 66 |
} |
| 67 |
|
| 68 |
/* A silly RAII wrapper for struct graph. The prime_paths function has multiple |
| 69 |
returns, and this helps reliably clean up. */ |
| 70 |
struct auto_graph |
| 71 |
{ |
| 72 |
auto_graph (struct graph *graph) : ptr (graph) {} |
| 73 |
auto_graph (const auto_graph &) = delete; |
| 74 |
~auto_graph () { free_graph (ptr); } |
| 75 |
operator struct graph* () { return ptr; } |
| 76 |
struct graph* operator -> () { return ptr; } |
| 77 |
graph *ptr; |
| 78 |
}; |
| 79 |
|
| 80 |
/* A silly RAII wrapper for an sbitmap vector. The prime_paths function has |
| 81 |
multiple returns, and this helps reliably clean up. */ |
| 82 |
struct auto_sbitmap_vector |
| 83 |
{ |
| 84 |
auto_sbitmap_vector (sbitmap *s) : ptr (s) {} |
| 85 |
auto_sbitmap_vector (const auto_sbitmap_vector &) = delete; |
| 86 |
~auto_sbitmap_vector () { sbitmap_vector_free (ptr); } |
| 87 |
operator sbitmap* () { return ptr; } |
| 88 |
sbitmap* ptr; |
| 89 |
}; |
| 90 |
|
| 91 |
/* A silly RAII wrpaper for automatically releasing a vec<vec<int>>. */ |
| 92 |
struct auto_vec_vec : vec<vec<int>> |
| 93 |
{ |
| 94 |
~auto_vec_vec () { release_vec_vec (*this); } |
| 95 |
}; |
| 96 |
|
| 97 |
/* A silly RAII wrpaper for automatically releasing a vec<vec<vec<int>>>. */ |
| 98 |
struct auto_vec_vec_vec : vec<vec<vec<int>>> |
| 99 |
{ |
| 100 |
~auto_vec_vec_vec () |
| 101 |
{ |
| 102 |
for (vec<vec<int>> &v : *this) |
| 103 |
release_vec_vec (v); |
| 104 |
release (); |
| 105 |
} |
| 106 |
}; |
| 107 |
|
| 108 |
/* A trivial key/value pair for a short linear map type. */ |
| 109 |
struct xpair |
| 110 |
{ |
| 111 |
int key; |
| 112 |
unsigned val; |
| 113 |
}; |
| 114 |
|
| 115 |
/* A node in a trie, optimized for mid-sized alphabets possibly larger than 256 |
| 116 |
but not much more. Finding the prime paths ends up creating a large amount |
| 117 |
of these nodes so space and access costs matters a lot. |
| 118 |
|
| 119 |
The node does not explicitly store its own key (CFG vertex ID/basic block |
| 120 |
index), nor does it store pointers to its successors. Rather, it stores the |
| 121 |
key+offset pairs for its successors the root trie object, and in a sense |
| 122 |
behaves like near pointers. This makes the trie vertices small and |
| 123 |
reloctable, and removes the need for pointer chasing when releasing the trie. |
| 124 |
|
| 125 |
The union of near/far is essentially a short-vector optimization, switching |
| 126 |
to a heap-allocated vector when necessary. This happens relatively rarely |
| 127 |
(usually maxes out at 1-2%), and the vertices that have more than 2 sucessors |
| 128 |
also tend to have more than 4. The root vertex tends to use the dynamic |
| 129 |
vector because the subpaths are recorded as the successors of the root. |
| 130 |
|
| 131 |
Conceptually, this is a small map from vertex-id -> index and the API is |
| 132 |
modelled as such. The insert and search functions are unrolled by hand when |
| 133 |
using the small vector. This has a noticable performance impact on insert in |
| 134 |
particular, and is not too complex since we know we are limited to 2 |
| 135 |
elements. |
| 136 |
|
| 137 |
Vertices are tagged with endofpath and inserted. If endofpath is set, the |
| 138 |
path from the root to this vertex is a complete path. If inserted is set |
| 139 |
then the vertex is a part of proper path (one given to insert) and not |
| 140 |
generated as a suffix. For example: |
| 141 |
|
| 142 |
insert ([2 4 6]) |
| 143 |
insert ([9 7 2 4 6]) |
| 144 |
insert ([2 4 6 8]) |
| 145 |
|
| 146 |
The inserted flags for [2 4 6] are not cleared, because otherwise [2 4 6 8] |
| 147 |
would be dropped when only following inserted vertices. The endofpath flag |
| 148 |
in [2 4 6] is cleared when the suffixes of [9 7 2 4 6] are inserted. |
| 149 |
|
| 150 |
The node will be inserted into a vec, and should be trivial. Instances |
| 151 |
should be value-initialized to zero-intialized state. */ |
| 152 |
struct trie_node |
| 153 |
{ |
| 154 |
unsigned length () const |
| 155 |
{ return !heaped ? len : far.length (); } |
| 156 |
|
| 157 |
const xpair *begin () const |
| 158 |
{ return !heaped ? near : far.begin (); } |
| 159 |
|
| 160 |
const xpair *end () const |
| 161 |
{ return !heaped ? (near + len) : far.end (); } |
| 162 |
|
| 163 |
/* Get the ith successor. This is used for traversal and not lookup, and |
| 164 |
should only be used by the iterator. */ |
| 165 |
const xpair &at (unsigned i) const |
| 166 |
{ return !heaped ? near[i] : far[i]; } |
| 167 |
|
| 168 |
const xpair *get (int key) const; |
| 169 |
void put (int key, unsigned val); |
| 170 |
|
| 171 |
unsigned near_lower_bound (int key) const; |
| 172 |
|
| 173 |
/* Experimentally I found that using a union with 2 elements in the near array |
| 174 |
to be faster than 4 or without the union (very slightly). A lot of trie |
| 175 |
vertices will be created, and vast majority of vertices will have 1 or 2 |
| 176 |
successors (straight line or if-then), and the cost of size and copying |
| 177 |
adds up. */ |
| 178 |
union |
| 179 |
{ |
| 180 |
xpair near[2]; |
| 181 |
vec<xpair> far; |
| 182 |
}; |
| 183 |
unsigned len : 8; |
| 184 |
unsigned endofpath : 1; |
| 185 |
unsigned inserted : 1; |
| 186 |
unsigned heaped : 1; |
| 187 |
}; |
| 188 |
|
| 189 |
/* Compare LHS.key < RHS.key, for use with vec.lower_bound. */ |
| 190 |
bool |
| 191 |
xpair_less (const xpair& lhs, const xpair& rhs) |
| 192 |
{ |
| 193 |
return lhs.key < rhs.key; |
| 194 |
} |
| 195 |
|
| 196 |
/* Compare LHS.key to RHS.key, for use with vec.bsearch. */ |
| 197 |
int |
| 198 |
xpair_cmp (const void *lhs, const void *rhs) |
| 199 |
{ |
| 200 |
return ((const xpair*)lhs)->key - ((const xpair*)rhs)->key; |
| 201 |
} |
| 202 |
|
| 203 |
/* Get a pointer to the element at KEY if it exists, otherwise NULL. */ |
| 204 |
const xpair* |
| 205 |
trie_node::get (int key) const |
| 206 |
{ |
| 207 |
if (!heaped) |
| 208 |
{ |
| 209 |
if (len == 0) return NULL; |
| 210 |
if (len >= 1 && key == near[0].key) return near + 0; |
| 211 |
if (len >= 2 && key == near[1].key) return near + 1; |
| 212 |
return NULL; |
| 213 |
} |
| 214 |
else |
| 215 |
{ |
| 216 |
xpair kv; |
| 217 |
kv.key = key; |
| 218 |
return const_cast <vec<xpair>&> (far).bsearch (&kv, xpair_cmp); |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
/* Put ("emplace") VAL at KEY, extending the paths that pass through this |
| 223 |
vertex. This function assumes that KEY is not already a successor, and does |
| 224 |
not perform this check. get () should be called and checked for NULL putting |
| 225 |
with this function. Put maintains the order of the successors. */ |
| 226 |
void |
| 227 |
trie_node::put (int key, unsigned val) |
| 228 |
{ |
| 229 |
xpair kv; |
| 230 |
kv.key = key; |
| 231 |
kv.val = val; |
| 232 |
if (!heaped) |
| 233 |
{ |
| 234 |
const unsigned i = near_lower_bound (key); |
| 235 |
if (len < 2) |
| 236 |
{ |
| 237 |
near[1] = near[0]; |
| 238 |
near[i] = kv; |
| 239 |
len += 1; |
| 240 |
} |
| 241 |
else |
| 242 |
{ |
| 243 |
/* This insert is the 3rd element, which does not fit in the embedded |
| 244 |
storage, so we must create a vector and convert to a far node. */ |
| 245 |
vec<xpair> xs {}; |
| 246 |
xs.reserve (13); |
| 247 |
xs.quick_grow (3); |
| 248 |
gcc_checking_assert (i <= 2); |
| 249 |
if (i == 0) |
| 250 |
{ |
| 251 |
xs[0] = kv; |
| 252 |
xs[1] = near[0]; |
| 253 |
xs[2] = near[1]; |
| 254 |
} |
| 255 |
else if (i == 1) |
| 256 |
{ |
| 257 |
xs[0] = near[0]; |
| 258 |
xs[1] = kv; |
| 259 |
xs[2] = near[1]; |
| 260 |
} |
| 261 |
else |
| 262 |
{ |
| 263 |
xs[0] = near[0]; |
| 264 |
xs[1] = near[1]; |
| 265 |
xs[2] = kv; |
| 266 |
} |
| 267 |
|
| 268 |
far = xs; |
| 269 |
heaped = 1; |
| 270 |
} |
| 271 |
} |
| 272 |
else |
| 273 |
{ |
| 274 |
const unsigned i = far.lower_bound (kv, xpair_less); |
| 275 |
far.safe_insert (i, kv); |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
/* Get the index to the last element that compares less than KEY, similar to |
| 280 |
vec.lower_bound. This assumes the near vector is active, and is for internal |
| 281 |
use. */ |
| 282 |
unsigned |
| 283 |
trie_node::near_lower_bound (int key) const |
| 284 |
{ |
| 285 |
gcc_checking_assert (!heaped); |
| 286 |
if (len == 0) return 0; |
| 287 |
if (len >= 1 && key < near[0].key) return 0; |
| 288 |
if (len >= 2 && key < near[1].key) return 1; |
| 289 |
return len; |
| 290 |
} |
| 291 |
|
| 292 |
/* The trie is a major workhorse for this algorithm. It has two key properties |
| 293 |
- set-like subpath elimination and sorted output. |
| 294 |
|
| 295 |
Many evaluated paths will be non-prime, that is, a sequence of vertices that |
| 296 |
is also fully embedded in a longer sequence of vertices. For example the |
| 297 |
path [3 4 5 8] is a subpath of both [2 3 4 5 8] and [3 4 5 8 10]. The |
| 298 |
insert_with_suffix function maintains this property so that inserting a |
| 299 |
subpath into the trie is effectively a no-op, and inserting a superpath will |
| 300 |
effectively remove (unmark) the subpath. Sometimes it can be guaranteed that |
| 301 |
no redundant (subpaths) will be generated, in which case the insert function |
| 302 |
can be used. The insert function is really only set insert, only becoming a |
| 303 |
no-op for identical paths, which will be a lot faster. |
| 304 |
|
| 305 |
Paths can be extracted with an iterator, which will output paths in |
| 306 |
lexicographically sorted order. This is an important property because the |
| 307 |
index of a path in the sorted set will be used by the coverage to record when |
| 308 |
a path is taken and completed. The iterator has different behavior than the |
| 309 |
standard C++ iterators, and to avoid mixups the interface is deliberately |
| 310 |
different. The iterator has a (large) stack which is not cheap to copy, and |
| 311 |
if the stack is shallow copied it would mean iterator copies have non-local |
| 312 |
effects. */ |
| 313 |
struct trie |
| 314 |
{ |
| 315 |
struct iter; |
| 316 |
trie (); |
| 317 |
trie (const trie &o); |
| 318 |
trie (trie &&o); |
| 319 |
~trie (); |
| 320 |
|
| 321 |
bool insert (const vec<int>&); |
| 322 |
bool insert (const array_slice<const int>); |
| 323 |
bool hard_insert (const array_slice<const int>); |
| 324 |
bool insert_with_suffix (const array_slice<const int>); |
| 325 |
bool insert_suffix (const array_slice<const int>); |
| 326 |
|
| 327 |
void merge (const trie&); |
| 328 |
|
| 329 |
iter paths (vec<int>&) const; |
| 330 |
iter paths (vec<int>&, int from) const; |
| 331 |
|
| 332 |
vec<vec<int>> paths () const; |
| 333 |
|
| 334 |
size_t size () const { return len; } |
| 335 |
|
| 336 |
vec<trie_node> vertices; |
| 337 |
size_t len; |
| 338 |
|
| 339 |
/* An iterator for the paths of the trie. The iterator yields all paths in |
| 340 |
lexicographical order. The iterator will be invalidated on any insertion |
| 341 |
into the trie. The iterator should not be constructed directly, but |
| 342 |
through the paths functions on the trie. It is essentially an explicit |
| 343 |
stack depth-first traversal. |
| 344 |
|
| 345 |
The iter fills a user-provided buffer which should only be read as a when |
| 346 |
the iter is active. Whenever next returns true the buffer is filled with |
| 347 |
the current path. Uses will generally look like this: |
| 348 |
|
| 349 |
vec<int> path {}; |
| 350 |
auto iter = trie.paths (path); |
| 351 |
while (iter.next ()) |
| 352 |
use_path (path); |
| 353 |
*/ |
| 354 |
struct iter |
| 355 |
{ |
| 356 |
iter (vec<int>&, const vec<trie_node>&); |
| 357 |
iter (int first, vec<int>& path, const vec<trie_node> &vertices); |
| 358 |
~iter () |
| 359 |
{ stack.release (); } |
| 360 |
|
| 361 |
bool next (); |
| 362 |
bool next (int); |
| 363 |
bool next (bool); |
| 364 |
|
| 365 |
|
| 366 |
/* This is the analog of the stack frame when implementing a recursive |
| 367 |
depth-first path traversal and collecting paths to the leafs: |
| 368 |
|
| 369 |
for (auto successor : vertex[ix]) |
| 370 |
{ |
| 371 |
path.push (successor.value); |
| 372 |
collect (successor.ix, successor.begin, successor.end, path) |
| 373 |
path.pop (); |
| 374 |
} |
| 375 |
|
| 376 |
Using size_t + 2x unsigned helped make the frame more compact and faster |
| 377 |
than pointers. */ |
| 378 |
struct frame |
| 379 |
{ |
| 380 |
/* The index of this frame's vertex, so that vertices[ix]. */ |
| 381 |
size_t ix; |
| 382 |
/* The index of the current active successor of vertices[ix]. */ |
| 383 |
unsigned itr; |
| 384 |
/* The end of vertices[ix] successors. When itr == end, vertex[ix] is |
| 385 |
exhausted. */ |
| 386 |
unsigned end; |
| 387 |
}; |
| 388 |
|
| 389 |
/* User provided buffer to fill with the paths. */ |
| 390 |
vec<int> &path; |
| 391 |
/* Direct reference to the trie vertices vector. */ |
| 392 |
const vec<trie_node> &vertices; |
| 393 |
/* The call stack. */ |
| 394 |
vec<frame> stack; |
| 395 |
/* Yield flag. If this is true then next () is permitted to and return a |
| 396 |
new value. If this is false, a value has already been yielded and next |
| 397 |
must first reset the state before building the next value. */ |
| 398 |
bool yield = true; |
| 399 |
|
| 400 |
iter (const iter& o) : path (o.path), vertices (o.vertices), |
| 401 |
stack (o.stack.copy ()), yield (o.yield) |
| 402 |
{ |
| 403 |
} |
| 404 |
|
| 405 |
/* Delete the copy assignment as the iter stores references and would cause |
| 406 |
bad bugs. It is not necessary for the iterator to work well. To support |
| 407 |
these the references would need to be (explicit) pointers. */ |
| 408 |
iter& operator = (const iter& o) = delete; |
| 409 |
}; |
| 410 |
}; |
| 411 |
|
| 412 |
/* Construct an iterator filling BUFFER. */ |
| 413 |
trie::iter |
| 414 |
trie::paths (vec<int> &buffer) const |
| 415 |
{ |
| 416 |
buffer.truncate (0); |
| 417 |
return iter (buffer, vertices); |
| 418 |
} |
| 419 |
|
| 420 |
/* Construct an iterator filling BUFFER for paths starting at FROM. */ |
| 421 |
trie::iter |
| 422 |
trie::paths (vec<int>& buffer, int from) const |
| 423 |
{ |
| 424 |
buffer.truncate (0); |
| 425 |
return iter (from, buffer, vertices); |
| 426 |
} |
| 427 |
|
| 428 |
/* Default construct a new trie. */ |
| 429 |
trie::trie () : vertices (vec<trie_node> {}), len (0) |
| 430 |
{ |
| 431 |
vertices.safe_push (trie_node {}); |
| 432 |
vertices[0].inserted = true; |
| 433 |
} |
| 434 |
|
| 435 |
/* Copy construct a new trie. */ |
| 436 |
trie::trie (const trie &o) : vertices (o.vertices.copy ()), len (o.len) |
| 437 |
{ |
| 438 |
} |
| 439 |
|
| 440 |
/* Move construct a new trie. */ |
| 441 |
trie::trie (trie &&o) : vertices (o.vertices), len (o.len) |
| 442 |
{ |
| 443 |
o.vertices = {}; |
| 444 |
o.len = 0; |
| 445 |
} |
| 446 |
|
| 447 |
/* Destroy a trie and release all the heaped resources. */ |
| 448 |
trie::~trie () |
| 449 |
{ |
| 450 |
for (trie_node &v : vertices) |
| 451 |
if (v.heaped) |
| 452 |
v.far.release (); |
| 453 |
vertices.release (); |
| 454 |
} |
| 455 |
|
| 456 |
/* Insert PATH into the trie. */ |
| 457 |
bool |
| 458 |
trie::insert (const vec<int>& path) |
| 459 |
{ |
| 460 |
return insert (array_slice <const int> (path)); |
| 461 |
} |
| 462 |
|
| 463 |
/* Insert the PATH into the trie. Duplicate entries will not be entered twice. |
| 464 |
If PATH is a subpath of another path this will not be detected or if there is |
| 465 |
a path previously inserted that is a subpath of PATH then this redundancy |
| 466 |
will not be eliminated. For that behavior, call insert_with_suffix. */ |
| 467 |
bool |
| 468 |
trie::insert (array_slice<const int> path) |
| 469 |
{ |
| 470 |
size_t index = 0; |
| 471 |
size_t partition = 0; |
| 472 |
for (const int v : path) |
| 473 |
{ |
| 474 |
trie_node ¤t = vertices[index]; |
| 475 |
current.inserted = true; |
| 476 |
partition++; |
| 477 |
|
| 478 |
const auto *xp = current.get (v); |
| 479 |
if (xp) |
| 480 |
{ |
| 481 |
index = xp->val; |
| 482 |
} |
| 483 |
else |
| 484 |
{ |
| 485 |
/* A new vertex on this path has been created, which means the rest of |
| 486 |
the path will also have to be created. Drain the path and create |
| 487 |
the remaining vertices in a single operation. */ |
| 488 |
unsigned ix = vertices.length (); |
| 489 |
current.put (v, ix); |
| 490 |
current.endofpath = false; |
| 491 |
|
| 492 |
array_slice<const int> tail (path.begin () + partition, |
| 493 |
path.size () - partition); |
| 494 |
vertices.safe_grow_cleared (1 + ix + tail.size ()); |
| 495 |
|
| 496 |
for (const int v : tail) |
| 497 |
{ |
| 498 |
trie_node &last = vertices[ix]; |
| 499 |
ix += 1; |
| 500 |
last.put (v, ix); |
| 501 |
last.inserted = true; |
| 502 |
} |
| 503 |
|
| 504 |
vertices.last ().endofpath = true; |
| 505 |
vertices.last ().inserted = true; |
| 506 |
len += 1; |
| 507 |
return true; |
| 508 |
} |
| 509 |
} |
| 510 |
|
| 511 |
return false; |
| 512 |
} |
| 513 |
|
| 514 |
/* hard_insert is like insert, except it does not overwrite any endofpath flags, |
| 515 |
and records the endofpath flag even when a superpath of PATH has been |
| 516 |
inserted previously. This effectively disables subpath elimination. */ |
| 517 |
bool |
| 518 |
trie::hard_insert (array_slice<const int> path) |
| 519 |
{ |
| 520 |
size_t index = 0; |
| 521 |
size_t partition = 0; |
| 522 |
for (const int v : path) |
| 523 |
{ |
| 524 |
trie_node ¤t = vertices[index]; |
| 525 |
current.inserted = true; |
| 526 |
partition++; |
| 527 |
|
| 528 |
const auto *xp = current.get (v); |
| 529 |
if (xp) |
| 530 |
{ |
| 531 |
index = xp->val; |
| 532 |
} |
| 533 |
else |
| 534 |
{ |
| 535 |
unsigned ix = vertices.length (); |
| 536 |
current.put (v, ix); |
| 537 |
|
| 538 |
array_slice<const int> tail (path.begin () + partition, |
| 539 |
path.size () - partition); |
| 540 |
vertices.safe_grow_cleared (1 + ix + tail.size ()); |
| 541 |
|
| 542 |
for (const int v : tail) |
| 543 |
{ |
| 544 |
trie_node &last = vertices[ix]; |
| 545 |
ix += 1; |
| 546 |
last.put (v, ix); |
| 547 |
last.inserted = true; |
| 548 |
} |
| 549 |
|
| 550 |
vertices.last ().endofpath = true; |
| 551 |
vertices.last ().inserted = true; |
| 552 |
len += 1; |
| 553 |
return true; |
| 554 |
} |
| 555 |
} |
| 556 |
|
| 557 |
vertices[index].endofpath = true; |
| 558 |
return false; |
| 559 |
} |
| 560 |
|
| 561 |
/* Insert a suffixes of PATH. This is identical to insert except that it is |
| 562 |
assumed that PATH is a subpath, and that the inserted path should clear the |
| 563 |
inserted and endofpath flags. This function should only be called by |
| 564 |
insert_with_suffix. */ |
| 565 |
bool |
| 566 |
trie::insert_suffix (array_slice<const int> path) |
| 567 |
{ |
| 568 |
size_t index = 0; |
| 569 |
size_t partition = 0; |
| 570 |
for (const int v : path) |
| 571 |
{ |
| 572 |
trie_node ¤t = vertices[index]; |
| 573 |
current.endofpath = false; |
| 574 |
partition++; |
| 575 |
|
| 576 |
const auto *xp = current.get (v); |
| 577 |
if (xp) |
| 578 |
{ |
| 579 |
index = xp->val; |
| 580 |
} |
| 581 |
else |
| 582 |
{ |
| 583 |
/* A new vertex on this path has been created, which means the rest of |
| 584 |
the path will also have to be created. Drain the path and create |
| 585 |
the remaining vertices in a single operation. */ |
| 586 |
unsigned ix = vertices.length (); |
| 587 |
current.put (v, ix); |
| 588 |
|
| 589 |
array_slice<const int> tail (path.begin () + partition, |
| 590 |
path.size () - partition); |
| 591 |
vertices.safe_grow_cleared (1 + ix + tail.size ()); |
| 592 |
|
| 593 |
for (const int v : tail) |
| 594 |
{ |
| 595 |
vertices[ix].put (v, ix + 1); |
| 596 |
ix += 1; |
| 597 |
} |
| 598 |
|
| 599 |
return true; |
| 600 |
} |
| 601 |
} |
| 602 |
|
| 603 |
vertices[index].endofpath = false; |
| 604 |
return false; |
| 605 |
} |
| 606 |
|
| 607 |
/* Insert the paths from OTHER into this trie. */ |
| 608 |
void |
| 609 |
trie::merge (const trie& other) |
| 610 |
{ |
| 611 |
auto_vec<int, 32> p {}; |
| 612 |
iter itr = other.paths (p); |
| 613 |
while (itr.next ()) |
| 614 |
insert_with_suffix (p); |
| 615 |
} |
| 616 |
|
| 617 |
/* Insert PATH and all its subpaths into the trie. This function implements the |
| 618 |
redundancy property of the trie - if an inserted path is either a subpath or |
| 619 |
superpath of some other path then only the longest will keep its inserted |
| 620 |
flag. */ |
| 621 |
bool |
| 622 |
trie::insert_with_suffix (array_slice<const int> path) |
| 623 |
{ |
| 624 |
bool inserted = insert (path); |
| 625 |
path = array_slice<const int> (path.begin () + 1, path.size () - 1); |
| 626 |
while (inserted && !path.empty ()) |
| 627 |
{ |
| 628 |
inserted = insert_suffix (path); |
| 629 |
path = array_slice<const int> (path.begin () + 1, path.size () - 1); |
| 630 |
} |
| 631 |
return inserted; |
| 632 |
} |
| 633 |
|
| 634 |
/* Convert the paths of a trie to a vec-of-vec. */ |
| 635 |
vec<vec<int>> |
| 636 |
trie::paths () const |
| 637 |
{ |
| 638 |
vec<int> path {}; |
| 639 |
vec<vec<int>> all {}; |
| 640 |
auto iter = paths (path); |
| 641 |
while (iter.next ()) |
| 642 |
all.safe_push (path.copy ()); |
| 643 |
return all; |
| 644 |
} |
| 645 |
|
| 646 |
/* Create an iterator over VERTICES with the caller-provided buffer PATH. */ |
| 647 |
trie::iter::iter (vec<int> &path, const vec<trie_node> &vertices) : path (path), |
| 648 |
vertices (vertices), stack (vec<frame> {}) |
| 649 |
{ |
| 650 |
gcc_checking_assert (!vertices.is_empty ()); |
| 651 |
stack.reserve (13); |
| 652 |
frame f; |
| 653 |
f.ix = 0; |
| 654 |
f.itr = 0; |
| 655 |
f.end = vertices[0].length (); |
| 656 |
stack.quick_push (f); |
| 657 |
} |
| 658 |
|
| 659 |
/* Create an iterator over VERTICES with the caller-provided buffer PATH for all |
| 660 |
paths and subpaths (suffixes) starting in FROM. Note that FROM will not be |
| 661 |
in the output buffer PATH, mainly because non-rooted paths are used when |
| 662 |
splicing with paths that end in FROM. */ |
| 663 |
trie::iter::iter (int from, vec<int> &path, const vec<trie_node> &vertices) : |
| 664 |
path (path), vertices (vertices), stack (vec<frame> {}) |
| 665 |
{ |
| 666 |
gcc_checking_assert (!vertices.is_empty ()); |
| 667 |
stack.reserve (13); |
| 668 |
|
| 669 |
auto *xp = vertices[0].get (from); |
| 670 |
if (!xp) |
| 671 |
{ |
| 672 |
/* No paths start with FROM, so construct an iterator where next () always |
| 673 |
returns false. */ |
| 674 |
frame f; |
| 675 |
f.ix = 0; |
| 676 |
f.itr = 0; |
| 677 |
f.end = 0; |
| 678 |
stack.safe_push (f); |
| 679 |
return; |
| 680 |
} |
| 681 |
|
| 682 |
frame f; |
| 683 |
f.ix = xp->val; |
| 684 |
f.itr = 0; |
| 685 |
f.end = vertices[f.ix].length (); |
| 686 |
stack.safe_push (f); |
| 687 |
} |
| 688 |
|
| 689 |
/* Find the next complete prime path in the trie and write it to the caller's |
| 690 |
buffer. Returns true if a path is written and false if the iterator is |
| 691 |
exhausted, in which case no path is written and the contents of the buffer is |
| 692 |
garbage. */ |
| 693 |
bool |
| 694 |
trie::iter::next () |
| 695 |
{ |
| 696 |
while (true) |
| 697 |
{ |
| 698 |
frame &top = stack.last (); |
| 699 |
const trie_node &vertex = vertices[top.ix]; |
| 700 |
|
| 701 |
if (vertex.endofpath && yield |
| 702 |
&& (top.itr != top.end || vertex.length () == 0)) |
| 703 |
{ |
| 704 |
yield = false; |
| 705 |
return true; |
| 706 |
} |
| 707 |
|
| 708 |
yield = true; |
| 709 |
|
| 710 |
if (top.itr != top.end) |
| 711 |
{ |
| 712 |
const xpair succ = vertex.at (top.itr); |
| 713 |
const trie_node &next = vertices[succ.val]; |
| 714 |
top.itr++; |
| 715 |
|
| 716 |
if (!next.inserted) |
| 717 |
continue; |
| 718 |
|
| 719 |
frame f {}; |
| 720 |
f.ix = succ.val; |
| 721 |
f.itr = 0; |
| 722 |
f.end = next.length (); |
| 723 |
path.safe_push (succ.key); |
| 724 |
stack.safe_push (f); |
| 725 |
} |
| 726 |
else |
| 727 |
{ |
| 728 |
stack.pop (); |
| 729 |
if (stack.is_empty ()) |
| 730 |
return false; |
| 731 |
path.pop (); |
| 732 |
} |
| 733 |
} |
| 734 |
} |
| 735 |
|
| 736 |
/* Find the next path in the trie that would continue (but does not include) |
| 737 |
LIMIT. If the trie contains the paths [2 4 6 8 9] [2 4 6 8 10] and [2 4 5 |
| 738 |
8], iter.next (8) would yield [2 4 6] and [2 4 5]. Returns true if a path is |
| 739 |
written and false if the iterator is exhausted, in which case no path is |
| 740 |
written and the contents of the buffer is garbage. */ |
| 741 |
bool |
| 742 |
trie::iter::next (int limit) |
| 743 |
{ |
| 744 |
while (true) |
| 745 |
{ |
| 746 |
frame &top = stack.last (); |
| 747 |
const trie_node &vertex = vertices[top.ix]; |
| 748 |
|
| 749 |
if (yield && top.itr != top.end) |
| 750 |
{ |
| 751 |
const xpair succ = vertex.at (top.itr); |
| 752 |
const trie_node &next = vertices[succ.val]; |
| 753 |
const int key = succ.key; |
| 754 |
const int val = succ.val; |
| 755 |
top.itr++; |
| 756 |
|
| 757 |
if (!next.inserted) |
| 758 |
continue; |
| 759 |
|
| 760 |
if (key == limit) |
| 761 |
{ |
| 762 |
if (path.is_empty ()) |
| 763 |
continue; |
| 764 |
yield = false; |
| 765 |
return true; |
| 766 |
} |
| 767 |
|
| 768 |
frame f {}; |
| 769 |
f.ix = val; |
| 770 |
f.itr = 0; |
| 771 |
f.end = next.length (); |
| 772 |
path.safe_push (key); |
| 773 |
stack.safe_push (f); |
| 774 |
} |
| 775 |
else |
| 776 |
{ |
| 777 |
yield = true; |
| 778 |
stack.pop (); |
| 779 |
if (stack.is_empty ()) |
| 780 |
return false; |
| 781 |
path.pop (); |
| 782 |
} |
| 783 |
} |
| 784 |
} |
| 785 |
|
| 786 |
/* Find the next path in among all paths including subpaths/suffixes. This is |
| 787 |
mainly useful in combination with trie.paths (from) for finding the paths |
| 788 |
that go through some vertex. */ |
| 789 |
bool |
| 790 |
trie::iter::next (bool) |
| 791 |
{ |
| 792 |
while (true) |
| 793 |
{ |
| 794 |
frame &top = stack.last (); |
| 795 |
const trie_node &vertex = vertices[top.ix]; |
| 796 |
|
| 797 |
if (yield && vertex.length () == 0) |
| 798 |
{ |
| 799 |
yield = false; |
| 800 |
return true; |
| 801 |
} |
| 802 |
|
| 803 |
yield = true; |
| 804 |
|
| 805 |
if (top.itr != top.end) |
| 806 |
{ |
| 807 |
const xpair succ = vertex.at (top.itr); |
| 808 |
const trie_node &next = vertices[succ.val]; |
| 809 |
top.itr++; |
| 810 |
|
| 811 |
frame f {}; |
| 812 |
f.ix = succ.val; |
| 813 |
f.itr = 0; |
| 814 |
f.end = next.length (); |
| 815 |
path.safe_push (succ.key); |
| 816 |
stack.safe_push (f); |
| 817 |
} |
| 818 |
else |
| 819 |
{ |
| 820 |
stack.pop (); |
| 821 |
if (stack.is_empty ()) |
| 822 |
return false; |
| 823 |
path.pop (); |
| 824 |
} |
| 825 |
} |
| 826 |
} |
| 827 |
|
| 828 |
/* Return the index of NEEDLE in HAYSTACK, or (size_t)-1 if not found. */ |
| 829 |
template <typename T> |
| 830 |
size_t |
| 831 |
index_of (T needle, const vec <T> &haystack) |
| 832 |
{ |
| 833 |
size_t len = haystack.length (); |
| 834 |
for (size_t i = 0; i != len; ++i) |
| 835 |
if (haystack[i] == needle) |
| 836 |
return i; |
| 837 |
return (size_t)-1; |
| 838 |
} |
| 839 |
|
| 840 |
/* Check if there is an edge in GRAPH from SRC to DST. */ |
| 841 |
bool |
| 842 |
edge_p (const struct graph *graph, int src, int dest) |
| 843 |
{ |
| 844 |
for (struct graph_edge *e = graph->vertices[src].succ; e; e = e->succ_next) |
| 845 |
if (e->dest == dest) |
| 846 |
return true; |
| 847 |
return false; |
| 848 |
} |
| 849 |
|
| 850 |
/* Check if PATH is a cycle starting (and ending) with V. */ |
| 851 |
bool |
| 852 |
cycle_p (const vec<int>& path, int v) |
| 853 |
{ |
| 854 |
return path[0] == v && path[path.length ()-1] == v; |
| 855 |
} |
| 856 |
|
| 857 |
/* Find the SCC entry-exit paths, the simple paths from ENTRY to EXIT, and add |
| 858 |
them to OUT. PRIME_PATHS is the prime paths of the SCC. Paths are hard |
| 859 |
inserted into OUT, which disables subpath eliminiation and essentially makes |
| 860 |
OUT a compact set. This is important to not eliminate paths from ENTRY to |
| 861 |
EXIT which are traversed by other ENTRY/EXIT pairs. Duplicated entries are |
| 862 |
removed. */ |
| 863 |
void |
| 864 |
scc_entry_exit_paths (const vec<vec<int>> &internal_pp, int entry, int exit, |
| 865 |
trie &out) |
| 866 |
{ |
| 867 |
if (entry == exit) |
| 868 |
{ |
| 869 |
out.hard_insert (array_slice <const int> (&entry, 1)); |
| 870 |
return; |
| 871 |
} |
| 872 |
|
| 873 |
for (const vec<int> &path : internal_pp) |
| 874 |
{ |
| 875 |
const size_t Ven = index_of (entry, path); |
| 876 |
const size_t Vex = index_of (exit, path); |
| 877 |
|
| 878 |
if (Ven == (size_t)-1 || Vex == (size_t)-1 || Vex <= Ven) |
| 879 |
continue; |
| 880 |
|
| 881 |
const size_t len = (Vex + 1) - Ven; |
| 882 |
array_slice <const int> p (path.begin () + Ven, len); |
| 883 |
out.hard_insert (p); |
| 884 |
} |
| 885 |
} |
| 886 |
|
| 887 |
/* Find the SCC exit paths, the simple paths that starts in a non-entry vertex |
| 888 |
in the SCC and ends in EXIT and are not a cycles. INTERNAL_PP are the |
| 889 |
internal prime paths for the SCC with EXIT as an exit vertex. |
| 890 |
|
| 891 |
Fazli claims the path must not be a subpath of another exit path in the SCC, |
| 892 |
but this is only half true: see gcov-29.c/pathcov005a. Subpaths must survive |
| 893 |
if they end in a different exit vertex than the superpath, so the hard_insert |
| 894 |
is important. */ |
| 895 |
void |
| 896 |
scc_exit_paths (const vec<vec<int>> &prime_paths, int exit, trie &out) |
| 897 |
{ |
| 898 |
trie trie; |
| 899 |
for (const vec<int> &path : prime_paths) |
| 900 |
{ |
| 901 |
const size_t Vex = index_of (exit, path); |
| 902 |
if (Vex == (size_t)-1 || cycle_p (path, exit)) |
| 903 |
continue; |
| 904 |
array_slice <const int> p (path.begin (), Vex + 1); |
| 905 |
trie.insert_with_suffix (p); |
| 906 |
} |
| 907 |
|
| 908 |
auto_vec<int> path {}; |
| 909 |
auto iter = trie.paths (path); |
| 910 |
while (iter.next ()) |
| 911 |
out.hard_insert (path); |
| 912 |
} |
| 913 |
|
| 914 |
/* Find the SCC entry paths, the simple paths that start in the entry vertex |
| 915 |
ENTRY and are not cycles. INTERNAL_PP are the internal prime paths for the |
| 916 |
SCC with ENTRY as an entry vertex. The paths are inserted into OUT. */ |
| 917 |
void |
| 918 |
scc_entry_paths (const vec<vec<int>> &internal_pp, int entry, trie &trie) |
| 919 |
{ |
| 920 |
for (const vec<int> &path : internal_pp) |
| 921 |
{ |
| 922 |
const size_t Ven = index_of (entry, path); |
| 923 |
if (Ven == (size_t)-1 || cycle_p (path, entry)) |
| 924 |
continue; |
| 925 |
array_slice <const int> p (path.begin () + Ven, path.length () - Ven); |
| 926 |
trie.insert (p); |
| 927 |
} |
| 928 |
} |
| 929 |
|
| 930 |
/* Worker for cfg_complete_prime_paths. ITR is the current id for the current |
| 931 |
path. END is end of the path so that when ITR == END, the walk is completed. |
| 932 |
EDGES is the matrix of edges where EDGES[src][dst] is set if there is an edge |
| 933 |
from src to dest. PATH is the vertices that make up this walk so far. TRIE |
| 934 |
is the output trie where paths are inserted. SCC_ENEX_PATHS are the |
| 935 |
entry-exit paths found by the scc_entry_exit_paths function. */ |
| 936 |
void |
| 937 |
cfg_complete_prime_paths1 (const int *itr, const int *end, |
| 938 |
const sbitmap *edges, |
| 939 |
const vec<vec<vec<int>>> &scc_enex_paths, |
| 940 |
vec<int> &path, trie &trie) |
| 941 |
{ |
| 942 |
if (itr == end) |
| 943 |
{ |
| 944 |
trie.insert_with_suffix (path); |
| 945 |
return; |
| 946 |
} |
| 947 |
|
| 948 |
const unsigned pathlen = path.length (); |
| 949 |
const sbitmap succs = edges[path.last ()]; |
| 950 |
for (const vec<int> &enex : scc_enex_paths[*itr]) |
| 951 |
{ |
| 952 |
if (!bitmap_bit_p (succs, enex[0])) |
| 953 |
continue; |
| 954 |
|
| 955 |
path.safe_splice (enex); |
| 956 |
cfg_complete_prime_paths1 (itr + 1, end, edges, scc_enex_paths, |
| 957 |
path, trie); |
| 958 |
path.truncate (pathlen); |
| 959 |
if (limit_exceed_p (trie.size ())) |
| 960 |
return; |
| 961 |
} |
| 962 |
} |
| 963 |
|
| 964 |
/* Find the complete prime paths of the CFG, the prime paths that start in the |
| 965 |
entry vertex and end in the exit vertex. */ |
| 966 |
trie |
| 967 |
cfg_complete_prime_paths (const sbitmap *edges, |
| 968 |
const vec<trie> &scc_entry_exit_paths, |
| 969 |
const trie &ccfg_prime_paths) |
| 970 |
{ |
| 971 |
trie trie; |
| 972 |
auto_vec<int, 16> path {}; |
| 973 |
auto_vec<int, 16> cfgpp {}; |
| 974 |
auto_vec_vec_vec scc_enex {}; |
| 975 |
scc_enex.reserve (scc_entry_exit_paths.length ()); |
| 976 |
|
| 977 |
for (size_t i = 0; i != scc_entry_exit_paths.length (); ++i) |
| 978 |
{ |
| 979 |
scc_enex.quick_push (vec<vec<int>> {}); |
| 980 |
auto iter = scc_entry_exit_paths[i].paths (path); |
| 981 |
while (iter.next ()) |
| 982 |
scc_enex[i].safe_push (path.copy ()); |
| 983 |
} |
| 984 |
|
| 985 |
auto iter = ccfg_prime_paths.paths (cfgpp); |
| 986 |
while (!limit_exceed_p (trie.size ()) && iter.next ()) |
| 987 |
for (const vec<int> &enex : scc_enex[cfgpp[0]]) |
| 988 |
{ |
| 989 |
path.truncate (0); |
| 990 |
path.safe_splice (enex); |
| 991 |
cfg_complete_prime_paths1 (cfgpp.begin () + 1, cfgpp.end (), edges, |
| 992 |
scc_enex, path, trie); |
| 993 |
if (limit_exceed_p (trie.size ())) |
| 994 |
return trie; |
| 995 |
} |
| 996 |
|
| 997 |
return trie; |
| 998 |
} |
| 999 |
|
| 1000 |
/* Find the SCC exit prime paths, the prime paths that start from a strongly |
| 1001 |
connected component and end in the end vertex. SCCS is a vector where |
| 1002 |
SCCS[i] = SCC (vertex_i) so that if vertex[2].component == 5 then SCCS[2] == |
| 1003 |
5. SCC_EXIT_PATHS is the output of scc_exit_paths (). COMPLETE_PRIME_PATHS |
| 1004 |
is the output of cfg_complete_prime_paths (). |
| 1005 |
|
| 1006 |
This function can suffer under path explosion and will terminate early if |
| 1007 |
the number of inserts in COMPLETE_PRIME_PATHS exceeds approx_limit. */ |
| 1008 |
trie |
| 1009 |
scc_exit_prime_paths (const struct graph *cfg, const trie &scc_exit_paths, |
| 1010 |
const trie &complete_prime_paths) |
| 1011 |
{ |
| 1012 |
trie trie; |
| 1013 |
auto_vec<int, 8> path {}; |
| 1014 |
auto_vec<int, 8> r {}; |
| 1015 |
auto_vec<int, 8> q {}; |
| 1016 |
|
| 1017 |
auto exiter = scc_exit_paths.paths (q); |
| 1018 |
while (exiter.next ()) |
| 1019 |
{ |
| 1020 |
const int Vex = q.last (); |
| 1021 |
auto iter = complete_prime_paths.paths (r, Vex); |
| 1022 |
while (iter.next (true)) |
| 1023 |
{ |
| 1024 |
/* There could be multiple Vex in the SCC. Even if scc_exit_paths |
| 1025 |
did not kill the subpaths, this trie probably would. It can be |
| 1026 |
assumed that all vertices in q are in the same SCC. |
| 1027 |
|
| 1028 |
This is an important note, as the Fazli and Afsharchi paper does |
| 1029 |
not properly capture this subtlety. */ |
| 1030 |
const int p0 = Vex; |
| 1031 |
const int p1 = r[0]; |
| 1032 |
|
| 1033 |
if (cfg->vertices[p0].component == cfg->vertices[p1].component) |
| 1034 |
continue; |
| 1035 |
|
| 1036 |
path.truncate (0); |
| 1037 |
path.reserve (q.length () + r.length ()); |
| 1038 |
path.splice (q); |
| 1039 |
path.splice (r); |
| 1040 |
/* This can probably insert without subpath elimination because: |
| 1041 |
1. Conflicts are *really* rare (see patmatch in tree.c), but they |
| 1042 |
do happen. |
| 1043 |
2. The output of this function is "filtered" through another trie |
| 1044 |
anyway so the redundant paths generated here will be eliminated |
| 1045 |
in the consumers at a very low extra cost. */ |
| 1046 |
trie.insert (path); |
| 1047 |
if (limit_exceed_p (trie.size ())) |
| 1048 |
return trie; |
| 1049 |
} |
| 1050 |
} |
| 1051 |
|
| 1052 |
return trie; |
| 1053 |
} |
| 1054 |
|
| 1055 |
/* Check if PATH in CFG enters the VERTEX's SCC through VERTEX. */ |
| 1056 |
bool |
| 1057 |
enters_through_p (const struct graph *cfg, const vec<int> &path, int vertex) |
| 1058 |
{ |
| 1059 |
gcc_checking_assert (!path.is_empty ()); |
| 1060 |
const int last = path.address()[path.length ()-1]; |
| 1061 |
if (cfg->vertices[last].component == cfg->vertices[vertex].component) |
| 1062 |
return false; |
| 1063 |
return edge_p (cfg, last, vertex); |
| 1064 |
}; |
| 1065 |
|
| 1066 |
/* Worker for scc_entry_prime_paths. CFG is the CFG for the function, |
| 1067 |
SCC_ENTRY_PATHS the accumulated scc_entry_paths for all the SCCs, PRIME_PATHS |
| 1068 |
is either the result of cfg_complete_prime_paths or exit_prime_paths, and OUT |
| 1069 |
the output trie. |
| 1070 |
|
| 1071 |
This function can suffer under path explosion and will terminate early if |
| 1072 |
the number of inserts in OUT exceeds approx_limit. */ |
| 1073 |
void |
| 1074 |
scc_entry_prime_paths1 (const struct graph *cfg, const trie &scc_entry_paths, |
| 1075 |
const trie &prime_paths, trie &out) |
| 1076 |
{ |
| 1077 |
auto_vec<int, 8> p {}; |
| 1078 |
auto_vec<int, 8> q {}; |
| 1079 |
auto_vec<int, 8> path {}; |
| 1080 |
auto itr = scc_entry_paths.paths (q); |
| 1081 |
while (itr.next ()) |
| 1082 |
{ |
| 1083 |
const int Ven = q[0]; |
| 1084 |
/* TODO: This might benefit from a reversed trie lookup. */ |
| 1085 |
auto xitr = prime_paths.paths (p); |
| 1086 |
while (xitr.next (Ven)) |
| 1087 |
{ |
| 1088 |
if (!enters_through_p (cfg, p, Ven)) |
| 1089 |
continue; |
| 1090 |
|
| 1091 |
path.truncate (0); |
| 1092 |
path.reserve (p.length () + q.length ()); |
| 1093 |
path.splice (p); |
| 1094 |
path.splice (q); |
| 1095 |
out.insert_with_suffix (path); |
| 1096 |
if (limit_exceed_p (out.size ())) |
| 1097 |
return; |
| 1098 |
} |
| 1099 |
} |
| 1100 |
} |
| 1101 |
|
| 1102 |
/* Find the entry prime paths - the prime paths that start in the root and end |
| 1103 |
in a strongly connected component. CFG is the CFG for this function, |
| 1104 |
SCC_ENTRY_PATHS the accumulated scc_entry_paths for all the SCCs, |
| 1105 |
COMPLETE_PRIME_PATHS the result of cfg_complete_prime_paths, and |
| 1106 |
EXIT_PRIME_PATHS result of exit_prime_paths. |
| 1107 |
|
| 1108 |
This function can suffer under path explosion and will terminate early if |
| 1109 |
the return value grows beyond approx_limit. */ |
| 1110 |
trie |
| 1111 |
scc_entry_prime_paths (const struct graph *cfg, |
| 1112 |
const trie &scc_entry_paths, |
| 1113 |
const trie &complete_prime_paths, |
| 1114 |
const trie &exit_prime_paths) |
| 1115 |
{ |
| 1116 |
trie trie; |
| 1117 |
scc_entry_prime_paths1 (cfg, scc_entry_paths, complete_prime_paths, trie); |
| 1118 |
scc_entry_prime_paths1 (cfg, scc_entry_paths, exit_prime_paths, trie); |
| 1119 |
return trie; |
| 1120 |
} |
| 1121 |
|
| 1122 |
/* Build a new control flow graph from the strongly connected components, so |
| 1123 |
that every node in the CCFG is a strongly connected component in the original |
| 1124 |
CFG. NSSC is the number of vertices in the new graph, and the return value |
| 1125 |
of graphds_ssc. */ |
| 1126 |
struct graph* |
| 1127 |
build_ccfg (struct graph *cfg, int nscc) |
| 1128 |
{ |
| 1129 |
struct graph *ccfg = new_graph (nscc); |
| 1130 |
for (int i = 0; i != cfg->n_vertices; ++i) |
| 1131 |
{ |
| 1132 |
struct vertex v = cfg->vertices[i]; |
| 1133 |
for (struct graph_edge *e = v.succ; e; e = e->succ_next) |
| 1134 |
{ |
| 1135 |
int src = v.component; |
| 1136 |
int dest = cfg->vertices[e->dest].component; |
| 1137 |
if (src != dest && !edge_p (ccfg, src, dest)) |
| 1138 |
add_edge (ccfg, src, dest); |
| 1139 |
} |
| 1140 |
} |
| 1141 |
|
| 1142 |
return ccfg; |
| 1143 |
} |
| 1144 |
|
| 1145 |
/* Create a new graph from CFG where the edges between strongly connected |
| 1146 |
components removed. */ |
| 1147 |
struct graph* |
| 1148 |
disconnect_sccs (struct graph *cfg) |
| 1149 |
{ |
| 1150 |
struct graph *ccfg = new_graph (cfg->n_vertices); |
| 1151 |
const struct vertex *vertices = cfg->vertices; |
| 1152 |
for (int i = 0; i != cfg->n_vertices; ++i) |
| 1153 |
{ |
| 1154 |
ccfg->vertices[i].data = &cfg->vertices[i]; |
| 1155 |
for (struct graph_edge *e = vertices[i].succ; e; e = e->succ_next) |
| 1156 |
if (vertices[e->src].component == vertices[e->dest].component) |
| 1157 |
add_edge (ccfg, e->src, e->dest)->data = e; |
| 1158 |
} |
| 1159 |
return ccfg; |
| 1160 |
} |
| 1161 |
|
| 1162 |
/* Check if vertex I in CFG is the entry vertex of a strongly connected |
| 1163 |
component. A vertex is an entry vertex if 1) there are no predecessors |
| 1164 |
(i.e. the root vertex is always an entry vertex) or 2) a predecessor belongs |
| 1165 |
to a different SCC. */ |
| 1166 |
bool |
| 1167 |
scc_entry_vertex_p (struct graph *cfg, size_t i) |
| 1168 |
{ |
| 1169 |
if (!cfg->vertices[i].pred) |
| 1170 |
return true; |
| 1171 |
const int scc = cfg->vertices[i].component; |
| 1172 |
for (struct graph_edge *e = cfg->vertices[i].pred; e; e = e->pred_next) |
| 1173 |
if (cfg->vertices[e->src].component != scc) |
| 1174 |
return true; |
| 1175 |
return false; |
| 1176 |
} |
| 1177 |
|
| 1178 |
/* Check if vertex I in CFG is an exit vertex of a strongly connected component. |
| 1179 |
A vertex is an exit vertex if 1) there are no successors (i.e. the sink is |
| 1180 |
always an exit vertex) or 2) if a successor belongs to a different SCC. */ |
| 1181 |
bool |
| 1182 |
scc_exit_vertex_p (struct graph *cfg, size_t i) |
| 1183 |
{ |
| 1184 |
if (!cfg->vertices[i].succ) |
| 1185 |
return true; |
| 1186 |
const int scc = cfg->vertices[i].component; |
| 1187 |
for (struct graph_edge *e = cfg->vertices[i].succ; e; e = e->succ_next) |
| 1188 |
if (cfg->vertices[e->dest].component != scc) |
| 1189 |
return true; |
| 1190 |
return false; |
| 1191 |
} |
| 1192 |
|
| 1193 |
/* Worker for simple_paths. Find all the simple paths in CFG starting at NODE |
| 1194 |
and insert into OUT. This is a DFS where the search stops when entering a |
| 1195 |
vertex already in SEEN. PATH is the sequence of ids for the vertices taken |
| 1196 |
from the from the root to NODE. When the number of inserts reaches LIMIT |
| 1197 |
the function aborts and returns so the caller can report that it is giving |
| 1198 |
up because the function is too complex. |
| 1199 |
|
| 1200 |
This function can suffer under path explosion and will terminate early if |
| 1201 |
the number of inserts in OUT exceeds approx_limit. */ |
| 1202 |
void |
| 1203 |
simple_paths1 (const struct graph *cfg, int node, sbitmap seen, vec<int> &path, |
| 1204 |
trie &out) |
| 1205 |
{ |
| 1206 |
if (limit_exceed_p (out.size ())) |
| 1207 |
return; |
| 1208 |
|
| 1209 |
if (!bitmap_set_bit (seen, node)) |
| 1210 |
{ |
| 1211 |
if (path[0] == node) |
| 1212 |
path.quick_push (node); |
| 1213 |
out.insert (path); |
| 1214 |
if (path[0] == node) |
| 1215 |
path.pop (); |
| 1216 |
return; |
| 1217 |
} |
| 1218 |
path.quick_push (node); |
| 1219 |
|
| 1220 |
struct graph_edge *succs = cfg->vertices[node].succ; |
| 1221 |
if (!succs) |
| 1222 |
{ |
| 1223 |
out.insert (path); |
| 1224 |
bitmap_clear_bit (seen, node); |
| 1225 |
path.pop (); |
| 1226 |
return; |
| 1227 |
} |
| 1228 |
|
| 1229 |
for (struct graph_edge *e = succs; e; e = e->succ_next) |
| 1230 |
simple_paths1 (cfg, e->dest, seen, path, out); |
| 1231 |
|
| 1232 |
bitmap_clear_bit (seen, node); |
| 1233 |
path.pop (); |
| 1234 |
} |
| 1235 |
|
| 1236 |
/* Find all the simple paths in CFG starting at ROOT and insert into OUT. A |
| 1237 |
simple path is a sequence of vertices without any duplicated vertices (i.e. |
| 1238 |
no loops). SEEN should be an sbitmap of CFG->n_vertices size. PATH and |
| 1239 |
SEEN will be cleared entry and is for buffer reuse between calls. When the |
| 1240 |
number of inserts reaches LIMIT the function aborts and returns so the |
| 1241 |
caller can report that it is giving up because the function is too complex. |
| 1242 |
Note that there might be fewer prime paths than inserts, but if the number |
| 1243 |
of inserts alone is larger than LIMIT the function is very complex and would |
| 1244 |
take too long to compile in later stages. |
| 1245 |
|
| 1246 |
This function can suffer under path explosion and will terminate early if |
| 1247 |
the number of inserts in OUT exceeds approx_limit. Since OUT is often |
| 1248 |
shared between calls it is ok to use in a loop, and only check the size of |
| 1249 |
OUT after the loop terminates. */ |
| 1250 |
void |
| 1251 |
simple_paths (struct graph *cfg, int root, sbitmap seen, vec<int> &path, |
| 1252 |
trie &out) |
| 1253 |
{ |
| 1254 |
bitmap_clear (seen); |
| 1255 |
path.reserve (cfg->n_vertices); |
| 1256 |
path.truncate (0); |
| 1257 |
simple_paths1 (cfg, root, seen, path, out); |
| 1258 |
} |
| 1259 |
|
| 1260 |
/* Merge the tries T1, T2, T3, and set of paths VECS into the larges trie. |
| 1261 |
Returns a reference to the trie merged into. Merging tries and resolving |
| 1262 |
redundant paths is the slowest step (at least in the sense it works on the |
| 1263 |
largest input), and merging into a partial result reduces the work |
| 1264 |
accordingly. For large problems this is a massive improvement, which in the |
| 1265 |
worst cases (where all tries but one are empty or almost empty) speed up |
| 1266 |
30-40%. */ |
| 1267 |
trie& |
| 1268 |
merge (trie &t1, trie &t2, trie &t3, vec<vec<vec<int>>> &vecs) |
| 1269 |
{ |
| 1270 |
trie *dst = nullptr; |
| 1271 |
const size_t s1 = t1.size (); |
| 1272 |
const size_t s2 = t2.size (); |
| 1273 |
const size_t s3 = t3.size (); |
| 1274 |
|
| 1275 |
if (s1 >= s2 && s1 >= s3) |
| 1276 |
{ |
| 1277 |
dst = &t1; |
| 1278 |
t1.merge (t2); |
| 1279 |
t1.merge (t3); |
| 1280 |
} |
| 1281 |
else if (s2 >= s1 && s2 >= s3) |
| 1282 |
{ |
| 1283 |
dst = &t2; |
| 1284 |
t2.merge (t1); |
| 1285 |
t2.merge (t3); |
| 1286 |
} |
| 1287 |
else |
| 1288 |
{ |
| 1289 |
dst = &t3; |
| 1290 |
t3.merge (t1); |
| 1291 |
t3.merge (t2); |
| 1292 |
} |
| 1293 |
|
| 1294 |
gcc_checking_assert (dst); |
| 1295 |
for (const vec<vec<int>> &v2 : vecs) |
| 1296 |
for (const vec<int> &v1 : v2) |
| 1297 |
dst->insert_with_suffix (v1); |
| 1298 |
return *dst; |
| 1299 |
} |
| 1300 |
|
| 1301 |
/* Store the edges of CFG in a matrix of bitmaps so that bit_p (edges[src], |
| 1302 |
dest) is true if there is an edge from src to dest. This is faster and more |
| 1303 |
convenient than walking the linked list of successors in hot loops. The |
| 1304 |
vector will have N bitmaps of N bits where N is the number of vertices in |
| 1305 |
CFG. */ |
| 1306 |
sbitmap* |
| 1307 |
edge_matrix (const struct graph *cfg) |
| 1308 |
{ |
| 1309 |
sbitmap *edges = sbitmap_vector_alloc (cfg->n_vertices, cfg->n_vertices); |
| 1310 |
bitmap_vector_clear (edges, cfg->n_vertices); |
| 1311 |
for (int i = 0; i != cfg->n_vertices; ++i) |
| 1312 |
for (graph_edge *e = cfg->vertices[i].succ; e; e = e->succ_next) |
| 1313 |
bitmap_set_bit (edges[e->src], e->dest); |
| 1314 |
return edges; |
| 1315 |
} |
| 1316 |
|
| 1317 |
} // namespace |
| 1318 |
|
| 1319 |
/* Find the prime paths for CFG. The search gives up after approximate |
| 1320 |
PATHLIMIT probable paths have been generated to address path explosions. |
| 1321 |
The PATHLIMIT flag is typically controlled by -fpath-coverage-limit. This |
| 1322 |
function is a part of -fpath-coverage and will also be called from gcov. |
| 1323 |
The paths are returned in lexicographical order based on node (basic block) |
| 1324 |
ID. If the path limit was exceeded, an empty vector is returned. |
| 1325 |
|
| 1326 |
A simple path is a path where all vertices are unique, except possibly the |
| 1327 |
first and last. A prime path is a maximal-length simple path which is not a |
| 1328 |
part of any other simple path. Prime paths strike a good balance between |
| 1329 |
coverage thoroughness, loops (requiring them to be taken and skipped), and |
| 1330 |
number of paths. |
| 1331 |
|
| 1332 |
The algorithm is based on Fazli & Afsharchi's "A Time and Space-Efficient |
| 1333 |
Compositional Method for Prime and Test Paths Generation" (2019), combined |
| 1334 |
with a suffix trie for removing duplicate or redundant paths. An auxillary |
| 1335 |
graph of the strongly connected components (SCCs) is built. Then, the prime |
| 1336 |
paths of the SCCs composes the prime paths of each SCC with the prime paths |
| 1337 |
of this auxillary graph. This can drastically cut the number of redundant |
| 1338 |
paths generated compared to a naive algorithm. |
| 1339 |
|
| 1340 |
This does not work for all graphs. Some structures, e.g. when most of the |
| 1341 |
graph is inside a single SCC, cause the algorithm to degenerate to a naive |
| 1342 |
one. The same happens for functions with many SCCs that are either |
| 1343 |
singletons or very small. Those cases will be slower with respect to the |
| 1344 |
number of paths, but still fast enough if the path limit is kept reasonably |
| 1345 |
low (a few hundred thousand). */ |
| 1346 |
vec<vec<int>> |
| 1347 |
prime_paths (struct graph *cfg, size_t pathlimit) |
| 1348 |
{ |
| 1349 |
const int nscc = graphds_scc (cfg, NULL); |
| 1350 |
auto_graph disconnected (disconnect_sccs (cfg)); |
| 1351 |
auto_graph ccfg (build_ccfg (cfg, nscc)); |
| 1352 |
auto_sbitmap_vector edges (edge_matrix (cfg)); |
| 1353 |
|
| 1354 |
auto_sbitmap seen (cfg->n_vertices); |
| 1355 |
auto_vec<int, 8> pathbuf {}; |
| 1356 |
|
| 1357 |
limit_reset (pathlimit); |
| 1358 |
|
| 1359 |
/* Store an SCC-ID -> vertices mapping to quickly find the vertices that |
| 1360 |
make up a strongly connected component. */ |
| 1361 |
auto_vec_vec sccs {}; |
| 1362 |
sccs.safe_grow_cleared (ccfg->n_vertices); |
| 1363 |
for (int i = 0; i != cfg->n_vertices; ++i) |
| 1364 |
sccs[cfg->vertices[i].component].safe_push (i); |
| 1365 |
|
| 1366 |
auto_vec_vec_vec scc_internal_pp {}; |
| 1367 |
scc_internal_pp.safe_grow_cleared (nscc); |
| 1368 |
for (int i = 0; i != nscc; ++i) |
| 1369 |
{ |
| 1370 |
trie internal_pp; |
| 1371 |
for (int V : sccs[i]) |
| 1372 |
simple_paths (disconnected, V, seen, pathbuf, internal_pp); |
| 1373 |
if (limit_exceed_p (internal_pp.size ())) |
| 1374 |
return {}; |
| 1375 |
scc_internal_pp[i] = internal_pp.paths (); |
| 1376 |
if (limit_checked_add (scc_internal_pp[i].length ())) |
| 1377 |
return {}; |
| 1378 |
} |
| 1379 |
|
| 1380 |
auto_vec<trie, 8> scc_enex_paths (nscc); |
| 1381 |
scc_enex_paths.safe_grow_cleared (nscc); |
| 1382 |
trie scc_en_paths; |
| 1383 |
trie scc_ex_paths; |
| 1384 |
|
| 1385 |
for (int i = 0; i != ccfg->n_vertices; ++i) |
| 1386 |
{ |
| 1387 |
for (int Ven : sccs[i]) |
| 1388 |
{ |
| 1389 |
if (!scc_entry_vertex_p (cfg, Ven)) |
| 1390 |
continue; |
| 1391 |
|
| 1392 |
for (int Vex : sccs[i]) |
| 1393 |
{ |
| 1394 |
if (!scc_exit_vertex_p (cfg, Vex)) |
| 1395 |
continue; |
| 1396 |
scc_entry_exit_paths (scc_internal_pp[i], Ven, Vex, |
| 1397 |
scc_enex_paths[i]); |
| 1398 |
} |
| 1399 |
} |
| 1400 |
} |
| 1401 |
|
| 1402 |
for (int i = 0; i != cfg->n_vertices; ++i) |
| 1403 |
{ |
| 1404 |
const int scc = cfg->vertices[i].component; |
| 1405 |
if (scc_entry_vertex_p (cfg, i)) |
| 1406 |
scc_entry_paths (scc_internal_pp[scc], i, scc_en_paths); |
| 1407 |
|
| 1408 |
if (scc_exit_vertex_p (cfg, i)) |
| 1409 |
scc_exit_paths (scc_internal_pp[scc], i, scc_ex_paths); |
| 1410 |
} |
| 1411 |
|
| 1412 |
/* In the presence of abnormal edges (like longjmp) it is possible to have |
| 1413 |
multiple "entry points" in function -- build ccfg prime paths starting at |
| 1414 |
any vertex without predecessor. For most graphs this will only be the |
| 1415 |
ENTRY_BLOCK. */ |
| 1416 |
trie ccfg_prime_paths; |
| 1417 |
for (int i = 0; i != ccfg->n_vertices; ++i) |
| 1418 |
if (!ccfg->vertices[i].pred) |
| 1419 |
simple_paths (ccfg, i, seen, pathbuf, ccfg_prime_paths); |
| 1420 |
if (limit_exceed_p (ccfg_prime_paths.size ())) |
| 1421 |
return {}; |
| 1422 |
|
| 1423 |
trie complete_prime_paths = cfg_complete_prime_paths (edges, scc_enex_paths, |
| 1424 |
ccfg_prime_paths); |
| 1425 |
if (limit_checked_add (complete_prime_paths.size ())) |
| 1426 |
return {}; |
| 1427 |
trie exit_prime_paths = scc_exit_prime_paths (cfg, scc_ex_paths, |
| 1428 |
complete_prime_paths); |
| 1429 |
if (limit_checked_add (exit_prime_paths.size ())) |
| 1430 |
return {}; |
| 1431 |
trie entry_prime_paths = scc_entry_prime_paths (cfg, scc_en_paths, |
| 1432 |
complete_prime_paths, |
| 1433 |
exit_prime_paths); |
| 1434 |
if (limit_checked_add (entry_prime_paths.size ())) |
| 1435 |
return {}; |
| 1436 |
|
| 1437 |
trie &merged = merge (complete_prime_paths, entry_prime_paths, |
| 1438 |
exit_prime_paths, scc_internal_pp); |
| 1439 |
if (merged.size () > pathlimit) |
| 1440 |
return {}; |
| 1441 |
|
| 1442 |
return merged.paths (); |
| 1443 |
} |
| 1444 |
|
| 1445 |
#if CHECKING_P |
| 1446 |
|
| 1447 |
namespace selftest |
| 1448 |
{ |
| 1449 |
|
| 1450 |
/* Check if the trie contains PATH. */ |
| 1451 |
static bool |
| 1452 |
contains (const trie &trie, array_slice<const int> path) |
| 1453 |
{ |
| 1454 |
size_t index = 0; |
| 1455 |
for (int id : path) |
| 1456 |
{ |
| 1457 |
const trie_node ¤t = trie.vertices[index]; |
| 1458 |
if (!current.inserted) |
| 1459 |
return false; |
| 1460 |
const auto *xp = current.get (id); |
| 1461 |
if (!xp) |
| 1462 |
return false; |
| 1463 |
index = xp->val; |
| 1464 |
} |
| 1465 |
return trie.vertices[index].inserted && trie.vertices[index].endofpath; |
| 1466 |
} |
| 1467 |
|
| 1468 |
static bool |
| 1469 |
equal_p (array_slice<const int> lhs, array_slice<const int> rhs) |
| 1470 |
{ |
| 1471 |
if (lhs.size () != rhs.size ()) |
| 1472 |
return false; |
| 1473 |
|
| 1474 |
size_t length = lhs.size (); |
| 1475 |
for (size_t i = 0; i != length; ++i) |
| 1476 |
if (lhs[i] != rhs[i]) |
| 1477 |
return false; |
| 1478 |
return true; |
| 1479 |
} |
| 1480 |
|
| 1481 |
static bool |
| 1482 |
any_equal_p (const array_slice<const int> &needle, |
| 1483 |
const vec<vec<int>> &haystack) |
| 1484 |
{ |
| 1485 |
for (const vec<int> &x : haystack) |
| 1486 |
if (equal_p (needle, array_slice <const int> (x))) |
| 1487 |
return true; |
| 1488 |
return false; |
| 1489 |
} |
| 1490 |
|
| 1491 |
static size_t |
| 1492 |
count (const trie &trie) |
| 1493 |
{ |
| 1494 |
size_t n = 0; |
| 1495 |
auto_vec<int> path {}; |
| 1496 |
auto iter = trie.paths (path); |
| 1497 |
while (iter.next ()) |
| 1498 |
n += 1; |
| 1499 |
return n; |
| 1500 |
} |
| 1501 |
|
| 1502 |
static vec<vec<int>> |
| 1503 |
simple_paths (struct graph *cfg, trie &trie, int root = 0) |
| 1504 |
{ |
| 1505 |
auto_sbitmap seen (cfg->n_vertices); |
| 1506 |
auto_vec<int> path; |
| 1507 |
simple_paths (cfg, root, seen, path, trie); |
| 1508 |
return trie.paths (); |
| 1509 |
} |
| 1510 |
|
| 1511 |
/* Create a CFG that roughly corresponds to this program: |
| 1512 |
|
| 1513 |
int binary_search(int a[], int len, int from, int to, int key) |
| 1514 |
{ |
| 1515 |
int low = from; |
| 1516 |
int high = to - 1; |
| 1517 |
|
| 1518 |
while (low <= high) |
| 1519 |
{ |
| 1520 |
int mid = (low + high) >> 1; |
| 1521 |
long midVal = a[mid]; |
| 1522 |
|
| 1523 |
if (midVal < key) |
| 1524 |
low = mid + 1; |
| 1525 |
else if (midVal > key) |
| 1526 |
high = mid - 1; |
| 1527 |
else |
| 1528 |
return mid; // key found |
| 1529 |
} |
| 1530 |
return -1; |
| 1531 |
} |
| 1532 |
|
| 1533 |
This program would emit a CFG very similar to the CFG used by Fazli & |
| 1534 |
Afsharchi (2019). The selftest cases are built from the partial paths used |
| 1535 |
in that paper. */ |
| 1536 |
static struct graph* |
| 1537 |
binary_search_cfg () |
| 1538 |
{ |
| 1539 |
struct graph *g = new_graph (11); |
| 1540 |
add_edge (g, 0, 1); |
| 1541 |
add_edge (g, 1, 2); |
| 1542 |
add_edge (g, 2, 3); |
| 1543 |
add_edge (g, 2, 4); |
| 1544 |
add_edge (g, 3, 10); |
| 1545 |
add_edge (g, 4, 5); |
| 1546 |
add_edge (g, 4, 6); |
| 1547 |
add_edge (g, 5, 7); |
| 1548 |
add_edge (g, 6, 8); |
| 1549 |
add_edge (g, 6, 9); |
| 1550 |
add_edge (g, 7, 2); |
| 1551 |
add_edge (g, 8, 10); |
| 1552 |
add_edge (g, 9, 7); |
| 1553 |
graphds_scc (g, NULL); |
| 1554 |
return g; |
| 1555 |
} |
| 1556 |
|
| 1557 |
/* Test a full run of the algorithm against a known graph (binary-search). */ |
| 1558 |
static void |
| 1559 |
test_prime_paths () |
| 1560 |
{ |
| 1561 |
auto_graph g (binary_search_cfg ()); |
| 1562 |
vec<vec<int>> paths = prime_paths (g, 100); |
| 1563 |
const int p01[] = { 0, 1, 2, 3, 10 }; |
| 1564 |
const int p02[] = { 0, 1, 2, 4, 6, 8, 10 }; |
| 1565 |
const int p03[] = { 5, 7, 2, 4, 6, 9 }; |
| 1566 |
const int p04[] = { 4, 6, 9, 7, 2, 4 }; |
| 1567 |
const int p05[] = { 2, 4, 6, 9, 7, 2 }; |
| 1568 |
const int p06[] = { 6, 9, 7, 2, 4, 6 }; |
| 1569 |
const int p07[] = { 9, 7, 2, 4, 6, 9 }; |
| 1570 |
const int p08[] = { 7, 2, 4, 6, 9, 7 }; |
| 1571 |
const int p09[] = { 6, 9, 7, 2, 4, 5 }; |
| 1572 |
const int p10[] = { 4, 5, 7, 2, 4 }; |
| 1573 |
const int p11[] = { 2, 4, 5, 7, 2 }; |
| 1574 |
const int p12[] = { 5, 7, 2, 4, 5 }; |
| 1575 |
const int p13[] = { 7, 2, 4, 5, 7 }; |
| 1576 |
const int p14[] = { 4, 6, 9, 7, 2, 3, 10 }; |
| 1577 |
const int p15[] = { 5, 7, 2, 4, 6, 8, 10 }; |
| 1578 |
const int p16[] = { 9, 7, 2, 4, 6, 8, 10 }; |
| 1579 |
const int p17[] = { 4, 5, 7, 2, 3, 10 }; |
| 1580 |
const int p18[] = { 0, 1, 2, 4, 6, 9, 7 }; |
| 1581 |
const int p19[] = { 0, 1, 2, 4, 5, 7 }; |
| 1582 |
|
| 1583 |
ASSERT_EQ (paths.length (), 19); |
| 1584 |
ASSERT_TRUE (any_equal_p (p01, paths)); |
| 1585 |
ASSERT_TRUE (any_equal_p (p02, paths)); |
| 1586 |
ASSERT_TRUE (any_equal_p (p03, paths)); |
| 1587 |
ASSERT_TRUE (any_equal_p (p04, paths)); |
| 1588 |
ASSERT_TRUE (any_equal_p (p05, paths)); |
| 1589 |
ASSERT_TRUE (any_equal_p (p06, paths)); |
| 1590 |
ASSERT_TRUE (any_equal_p (p07, paths)); |
| 1591 |
ASSERT_TRUE (any_equal_p (p08, paths)); |
| 1592 |
ASSERT_TRUE (any_equal_p (p09, paths)); |
| 1593 |
ASSERT_TRUE (any_equal_p (p10, paths)); |
| 1594 |
ASSERT_TRUE (any_equal_p (p11, paths)); |
| 1595 |
ASSERT_TRUE (any_equal_p (p12, paths)); |
| 1596 |
ASSERT_TRUE (any_equal_p (p13, paths)); |
| 1597 |
ASSERT_TRUE (any_equal_p (p14, paths)); |
| 1598 |
ASSERT_TRUE (any_equal_p (p15, paths)); |
| 1599 |
ASSERT_TRUE (any_equal_p (p16, paths)); |
| 1600 |
ASSERT_TRUE (any_equal_p (p17, paths)); |
| 1601 |
ASSERT_TRUE (any_equal_p (p18, paths)); |
| 1602 |
ASSERT_TRUE (any_equal_p (p19, paths)); |
| 1603 |
release_vec_vec (paths); |
| 1604 |
} |
| 1605 |
|
| 1606 |
/* The strongly connected component graph for binary_search looks like |
| 1607 |
this, using the vertex numbers from the original graph: |
| 1608 |
|
| 1609 |
START |
| 1610 |
| |
| 1611 |
1 |
| 1612 |
| |
| 1613 |
2 (SCC) |
| 1614 |
/ \ |
| 1615 |
3 8 |
| 1616 |
\ / |
| 1617 |
END |
| 1618 |
|
| 1619 |
The components gets renumbered by graphds_scc, so the ccfg looks like |
| 1620 |
this. The actual numbers don't matter as long as the structure of the |
| 1621 |
graph is preserved, and this test is now sensitive to the numbering set |
| 1622 |
by graphds_scc. It does not have to be - if that function should reverse |
| 1623 |
the numbering this test must be updated. |
| 1624 |
|
| 1625 |
5 |
| 1626 |
| |
| 1627 |
4 |
| 1628 |
| |
| 1629 |
3 (SCC) |
| 1630 |
/ \ |
| 1631 |
2 1 |
| 1632 |
\ / |
| 1633 |
0 |
| 1634 |
*/ |
| 1635 |
static void |
| 1636 |
test_build_ccfg () |
| 1637 |
{ |
| 1638 |
auto_graph cfg (binary_search_cfg ()); |
| 1639 |
const int nscc = graphds_scc (cfg, NULL); |
| 1640 |
auto_graph ccfg (build_ccfg (cfg, nscc)); |
| 1641 |
ASSERT_EQ (6, nscc); |
| 1642 |
|
| 1643 |
ASSERT_TRUE (edge_p (ccfg, 5, 4)); |
| 1644 |
ASSERT_TRUE (edge_p (ccfg, 4, 3)); |
| 1645 |
ASSERT_TRUE (edge_p (ccfg, 3, 2)); |
| 1646 |
ASSERT_TRUE (edge_p (ccfg, 3, 1)); |
| 1647 |
ASSERT_TRUE (edge_p (ccfg, 2, 0)); |
| 1648 |
ASSERT_TRUE (edge_p (ccfg, 1, 0)); |
| 1649 |
} |
| 1650 |
|
| 1651 |
/* This test checks some basic assumptions on finding the strongly connected |
| 1652 |
components and disconnecting the graph by removing all edges between SCCs. |
| 1653 |
Creating a single auxillary graph simplifies the bookkeeping. */ |
| 1654 |
static void |
| 1655 |
test_split_components () |
| 1656 |
{ |
| 1657 |
auto_graph cfg (binary_search_cfg ()); |
| 1658 |
int nscc = graphds_scc (cfg, NULL); |
| 1659 |
auto_graph ccfg (disconnect_sccs (cfg)); |
| 1660 |
|
| 1661 |
vec<vec<int>> entries {}; |
| 1662 |
vec<vec<int>> exits {}; |
| 1663 |
entries.safe_grow_cleared (nscc); |
| 1664 |
exits.safe_grow_cleared (nscc); |
| 1665 |
|
| 1666 |
for (int i = 0; i != cfg->n_vertices; ++i) |
| 1667 |
{ |
| 1668 |
if (scc_entry_vertex_p (cfg, i)) |
| 1669 |
entries[cfg->vertices[i].component].safe_push (i); |
| 1670 |
if (scc_exit_vertex_p (cfg, i)) |
| 1671 |
exits[cfg->vertices[i].component].safe_push (i); |
| 1672 |
} |
| 1673 |
|
| 1674 |
const int p10[] = { 10 }; |
| 1675 |
const int p08[] = { 8 }; |
| 1676 |
const int p03[] = { 3 }; |
| 1677 |
const int p02[] = { 2 }; |
| 1678 |
const int p01[] = { 1 }; |
| 1679 |
const int p00[] = { 0 }; |
| 1680 |
const int p26[] = { 2, 6 }; |
| 1681 |
|
| 1682 |
ASSERT_EQ (entries.length (), 6); |
| 1683 |
ASSERT_TRUE (any_equal_p (p10, entries)); |
| 1684 |
ASSERT_TRUE (any_equal_p (p08, entries)); |
| 1685 |
ASSERT_TRUE (any_equal_p (p03, entries)); |
| 1686 |
ASSERT_TRUE (any_equal_p (p02, entries)); |
| 1687 |
ASSERT_TRUE (any_equal_p (p01, entries)); |
| 1688 |
ASSERT_TRUE (any_equal_p (p00, entries)); |
| 1689 |
|
| 1690 |
ASSERT_EQ (exits.length (), 6); |
| 1691 |
ASSERT_TRUE (any_equal_p (p10, exits)); |
| 1692 |
ASSERT_TRUE (any_equal_p (p08, exits)); |
| 1693 |
ASSERT_TRUE (any_equal_p (p03, exits)); |
| 1694 |
ASSERT_TRUE (any_equal_p (p26, exits)); |
| 1695 |
ASSERT_TRUE (any_equal_p (p01, exits)); |
| 1696 |
ASSERT_TRUE (any_equal_p (p00, exits)); |
| 1697 |
|
| 1698 |
/* The result of disconnect_sccs () disconnects the graph into its strongly |
| 1699 |
connected components. The subgraphs are either singletons (a single |
| 1700 |
vertex with no edges) or graphs with cycles. The SCC internal prime |
| 1701 |
paths can be found by running a DFS from every SCC vertex, terminating |
| 1702 |
on a duplicated vertex. This may create some redundant paths still, |
| 1703 |
which must be filtered out. |
| 1704 |
|
| 1705 |
Singletons can either be detected and skipped (requires counting the |
| 1706 |
components) or filtered after. For this test case they are skipped |
| 1707 |
because other graph inconsistencies are easier to detect. */ |
| 1708 |
|
| 1709 |
/* Count and check singleton components. */ |
| 1710 |
vec<int> scc_size {}; |
| 1711 |
scc_size.safe_grow_cleared (nscc); |
| 1712 |
for (int i = 0; i != cfg->n_vertices; ++i) |
| 1713 |
scc_size[cfg->vertices[i].component]++; |
| 1714 |
ASSERT_EQ (nscc, 6); |
| 1715 |
ASSERT_EQ (scc_size[0], 1); |
| 1716 |
ASSERT_EQ (scc_size[1], 1); |
| 1717 |
ASSERT_EQ (scc_size[2], 1); |
| 1718 |
ASSERT_EQ (scc_size[3], 6); |
| 1719 |
ASSERT_EQ (scc_size[4], 1); |
| 1720 |
ASSERT_EQ (scc_size[5], 1); |
| 1721 |
|
| 1722 |
/* Manually unroll the loop finding the simple paths starting at the |
| 1723 |
vertices in the SCCs. In this case there is only the one SCC. */ |
| 1724 |
trie ccfg_paths; |
| 1725 |
simple_paths (ccfg, ccfg_paths, 2); |
| 1726 |
simple_paths (ccfg, ccfg_paths, 4); |
| 1727 |
simple_paths (ccfg, ccfg_paths, 5); |
| 1728 |
simple_paths (ccfg, ccfg_paths, 6); |
| 1729 |
simple_paths (ccfg, ccfg_paths, 7); |
| 1730 |
simple_paths (ccfg, ccfg_paths, 9); |
| 1731 |
/* Then in+out of trie. */ |
| 1732 |
vec<vec<int>> xscc_internal_pp = ccfg_paths.paths (); |
| 1733 |
trie scc_internal_pp; |
| 1734 |
for (auto &p : xscc_internal_pp) |
| 1735 |
scc_internal_pp.insert_with_suffix (p); |
| 1736 |
|
| 1737 |
const int pp01[] = { 5, 7, 2, 4, 6, 9 }; |
| 1738 |
const int pp02[] = { 4, 5, 7, 2, 4 }; |
| 1739 |
const int pp03[] = { 4, 6, 9, 7, 2, 4 }; |
| 1740 |
const int pp04[] = { 2, 4, 5, 7, 2 }; |
| 1741 |
const int pp05[] = { 2, 4, 6, 9, 7, 2 }; |
| 1742 |
const int pp06[] = { 5, 7, 2, 4, 5 }; |
| 1743 |
const int pp07[] = { 6, 9, 7, 2, 4, 6 }; |
| 1744 |
const int pp08[] = { 7, 2, 4, 5, 7 }; |
| 1745 |
const int pp09[] = { 9, 7, 2, 4, 6, 9 }; |
| 1746 |
const int pp10[] = { 7, 2, 4, 6, 9, 7 }; |
| 1747 |
const int pp11[] = { 6, 9, 7, 2, 4, 5 }; |
| 1748 |
|
| 1749 |
ASSERT_EQ (count (scc_internal_pp), 11); |
| 1750 |
ASSERT_TRUE (contains (scc_internal_pp, pp01)); |
| 1751 |
ASSERT_TRUE (contains (scc_internal_pp, pp02)); |
| 1752 |
ASSERT_TRUE (contains (scc_internal_pp, pp03)); |
| 1753 |
ASSERT_TRUE (contains (scc_internal_pp, pp04)); |
| 1754 |
ASSERT_TRUE (contains (scc_internal_pp, pp05)); |
| 1755 |
ASSERT_TRUE (contains (scc_internal_pp, pp06)); |
| 1756 |
ASSERT_TRUE (contains (scc_internal_pp, pp07)); |
| 1757 |
ASSERT_TRUE (contains (scc_internal_pp, pp08)); |
| 1758 |
ASSERT_TRUE (contains (scc_internal_pp, pp09)); |
| 1759 |
ASSERT_TRUE (contains (scc_internal_pp, pp10)); |
| 1760 |
ASSERT_TRUE (contains (scc_internal_pp, pp11)); |
| 1761 |
} |
| 1762 |
|
| 1763 |
/* The remaining tests deconstructs the algorithm and runs only a single phase |
| 1764 |
with good inputs at that point. This makes it easier to pinpoint where |
| 1765 |
things go wrong, and helps show in steps how the algorithm works and the |
| 1766 |
phases relate. |
| 1767 |
|
| 1768 |
The phases and their inputs and outputs are bazed on Fazli & Afshsarchi. */ |
| 1769 |
|
| 1770 |
static void |
| 1771 |
test_scc_internal_prime_paths () |
| 1772 |
{ |
| 1773 |
/* This graph has only the SCC subgraph. The result of running prime-paths |
| 1774 |
on it should be the SCC internal prime paths of the full graph. */ |
| 1775 |
auto_graph scc (new_graph (11)); |
| 1776 |
add_edge (scc, 0, 2); |
| 1777 |
add_edge (scc, 2, 4); |
| 1778 |
add_edge (scc, 4, 5); |
| 1779 |
add_edge (scc, 4, 6); |
| 1780 |
add_edge (scc, 5, 7); |
| 1781 |
add_edge (scc, 6, 9); |
| 1782 |
add_edge (scc, 9, 7); |
| 1783 |
add_edge (scc, 7, 2); |
| 1784 |
|
| 1785 |
vec<vec<int>> paths = prime_paths (scc, 100); |
| 1786 |
const int p01[] = { 5, 7, 2, 4, 6, 9 }; |
| 1787 |
const int p02[] = { 4, 6, 9, 7, 2, 4 }; |
| 1788 |
const int p03[] = { 2, 4, 6, 9, 7, 2 }; |
| 1789 |
const int p04[] = { 6, 9, 7, 2, 4, 6 }; |
| 1790 |
const int p05[] = { 9, 7, 2, 4, 6, 9 }; |
| 1791 |
const int p06[] = { 7, 2, 4, 6, 9, 7 }; |
| 1792 |
const int p07[] = { 6, 9, 7, 2, 4, 5 }; |
| 1793 |
const int p08[] = { 4, 5, 7, 2, 4 }; |
| 1794 |
const int p09[] = { 2, 4, 5, 7, 2 }; |
| 1795 |
const int p10[] = { 5, 7, 2, 4, 5 }; |
| 1796 |
const int p11[] = { 7, 2, 4, 5, 7 }; |
| 1797 |
|
| 1798 |
ASSERT_TRUE (any_equal_p (p01, paths)); |
| 1799 |
ASSERT_TRUE (any_equal_p (p02, paths)); |
| 1800 |
ASSERT_TRUE (any_equal_p (p03, paths)); |
| 1801 |
ASSERT_TRUE (any_equal_p (p04, paths)); |
| 1802 |
ASSERT_TRUE (any_equal_p (p05, paths)); |
| 1803 |
ASSERT_TRUE (any_equal_p (p06, paths)); |
| 1804 |
ASSERT_TRUE (any_equal_p (p07, paths)); |
| 1805 |
ASSERT_TRUE (any_equal_p (p08, paths)); |
| 1806 |
ASSERT_TRUE (any_equal_p (p09, paths)); |
| 1807 |
ASSERT_TRUE (any_equal_p (p10, paths)); |
| 1808 |
ASSERT_TRUE (any_equal_p (p11, paths)); |
| 1809 |
release_vec_vec (paths); |
| 1810 |
} |
| 1811 |
|
| 1812 |
/* Test the entry/exit path helpers for the strongly connected component in |
| 1813 |
binary_search. The SCC has one entry (2, the loop header) and two exits (2, |
| 1814 |
6, the loop exit and return). */ |
| 1815 |
static void |
| 1816 |
test_scc_entry_exit_paths () |
| 1817 |
{ |
| 1818 |
auto_graph scc (new_graph (11)); |
| 1819 |
add_edge (scc, 2, 4); |
| 1820 |
add_edge (scc, 4, 5); |
| 1821 |
add_edge (scc, 4, 6); |
| 1822 |
add_edge (scc, 5, 7); |
| 1823 |
add_edge (scc, 6, 9); |
| 1824 |
add_edge (scc, 9, 7); |
| 1825 |
add_edge (scc, 7, 2); |
| 1826 |
|
| 1827 |
trie scc_internal_trie; |
| 1828 |
simple_paths (scc, scc_internal_trie, 2); |
| 1829 |
simple_paths (scc, scc_internal_trie, 4); |
| 1830 |
simple_paths (scc, scc_internal_trie, 5); |
| 1831 |
simple_paths (scc, scc_internal_trie, 6); |
| 1832 |
simple_paths (scc, scc_internal_trie, 7); |
| 1833 |
simple_paths (scc, scc_internal_trie, 9); |
| 1834 |
vec<vec<int>> scc_prime_paths = scc_internal_trie.paths (); |
| 1835 |
|
| 1836 |
trie entry_exits {}; |
| 1837 |
scc_entry_exit_paths (scc_prime_paths, 2, 2, entry_exits); |
| 1838 |
scc_entry_exit_paths (scc_prime_paths, 2, 6, entry_exits); |
| 1839 |
|
| 1840 |
const int p01[] = { 2 }; |
| 1841 |
const int p02[] = { 2, 4, 6 }; |
| 1842 |
|
| 1843 |
ASSERT_EQ (count (entry_exits), 2); |
| 1844 |
ASSERT_TRUE (contains (entry_exits, p01)); |
| 1845 |
ASSERT_TRUE (contains (entry_exits, p02)); |
| 1846 |
|
| 1847 |
trie exits; |
| 1848 |
scc_exit_paths (scc_prime_paths, 2, exits); |
| 1849 |
scc_exit_paths (scc_prime_paths, 6, exits); |
| 1850 |
|
| 1851 |
const int p03[] = { 4, 6, 9, 7, 2 }; |
| 1852 |
const int p04[] = { 5, 7, 2, 4, 6 }; |
| 1853 |
const int p05[] = { 9, 7, 2, 4, 6 }; |
| 1854 |
const int p06[] = { 4, 5, 7, 2 }; |
| 1855 |
|
| 1856 |
ASSERT_EQ (count (exits), 4); |
| 1857 |
ASSERT_TRUE (contains (exits, p03)); |
| 1858 |
ASSERT_TRUE (contains (exits, p04)); |
| 1859 |
ASSERT_TRUE (contains (exits, p05)); |
| 1860 |
ASSERT_TRUE (contains (exits, p06)); |
| 1861 |
|
| 1862 |
trie entries; |
| 1863 |
scc_entry_paths (scc_prime_paths, 2, entries); |
| 1864 |
|
| 1865 |
const int p07[] = { 2, 4, 6, 9, 7 }; |
| 1866 |
const int p08[] = { 2, 4, 5, 7 }; |
| 1867 |
ASSERT_EQ (count (entries), 2); |
| 1868 |
ASSERT_TRUE (contains (entries, p07)); |
| 1869 |
ASSERT_TRUE (contains (entries, p08)); |
| 1870 |
|
| 1871 |
release_vec_vec (scc_prime_paths); |
| 1872 |
} |
| 1873 |
|
| 1874 |
static void |
| 1875 |
test_complete_prime_paths () |
| 1876 |
{ |
| 1877 |
const int ccfgpp0[] = { 0, 1, 2, 3, 5 }; |
| 1878 |
const int ccfgpp1[] = { 0, 1, 2, 4, 5 }; |
| 1879 |
trie ccfg_prime_paths {}; |
| 1880 |
ccfg_prime_paths.insert (ccfgpp0); |
| 1881 |
ccfg_prime_paths.insert (ccfgpp1); |
| 1882 |
|
| 1883 |
const int scc0[] = { 2 }; |
| 1884 |
const int scc1[] = { 2, 4, 6 }; |
| 1885 |
|
| 1886 |
const int ccfg_single[] = { 0, 1, 3, 8, 10 }; |
| 1887 |
|
| 1888 |
auto_graph cfg (binary_search_cfg ()); |
| 1889 |
auto_sbitmap_vector edges (sbitmap_vector_alloc (cfg->n_vertices, |
| 1890 |
cfg->n_vertices)); |
| 1891 |
bitmap_vector_clear (edges, cfg->n_vertices); |
| 1892 |
for (int i = 0; i != cfg->n_vertices; ++i) |
| 1893 |
for (graph_edge *e = cfg->vertices[i].succ; e; e = e->succ_next) |
| 1894 |
bitmap_set_bit (edges[e->src], e->dest); |
| 1895 |
|
| 1896 |
vec<trie> ccfg_paths {}; |
| 1897 |
ccfg_paths.safe_grow_cleared (6); |
| 1898 |
ccfg_paths[0].insert (array_slice <const int> (ccfg_single + 0, 1)); |
| 1899 |
ccfg_paths[1].insert (array_slice <const int> (ccfg_single + 1, 1)); |
| 1900 |
ccfg_paths[3].insert (array_slice <const int> (ccfg_single + 2, 1)); |
| 1901 |
ccfg_paths[4].insert (array_slice <const int> (ccfg_single + 3, 1)); |
| 1902 |
ccfg_paths[5].insert (array_slice <const int> (ccfg_single + 4, 1)); |
| 1903 |
|
| 1904 |
/* The paths for the SCC would need to be updated in ccfg pre pass. This |
| 1905 |
feels like a clumsy interface and should maybe be disconnected from the |
| 1906 |
struct graph. */ |
| 1907 |
ccfg_paths[2].hard_insert (scc0); |
| 1908 |
ccfg_paths[2].hard_insert (scc1); |
| 1909 |
|
| 1910 |
trie cpp = cfg_complete_prime_paths (edges, ccfg_paths, ccfg_prime_paths); |
| 1911 |
|
| 1912 |
const int p01[] = { 0, 1, 2, 3, 10 }; |
| 1913 |
const int p02[] = { 0, 1, 2, 4, 6, 8, 10 }; |
| 1914 |
|
| 1915 |
ASSERT_EQ (count (cpp), 2); |
| 1916 |
ASSERT_TRUE (contains (cpp, p01)); |
| 1917 |
ASSERT_TRUE (contains (cpp, p02)); |
| 1918 |
} |
| 1919 |
|
| 1920 |
static vec<int> |
| 1921 |
binary_search_scc_map () |
| 1922 |
{ |
| 1923 |
vec<int> sccs {}; |
| 1924 |
sccs.safe_grow (11); |
| 1925 |
sccs[0] = 0; |
| 1926 |
sccs[1] = 1; |
| 1927 |
sccs[2] = 2; |
| 1928 |
sccs[3] = 3; |
| 1929 |
sccs[4] = 2; |
| 1930 |
sccs[5] = 2; |
| 1931 |
sccs[6] = 2; |
| 1932 |
sccs[7] = 2; |
| 1933 |
sccs[8] = 4; |
| 1934 |
sccs[9] = 2; |
| 1935 |
sccs[10] = 5; |
| 1936 |
return sccs; |
| 1937 |
} |
| 1938 |
|
| 1939 |
static void |
| 1940 |
test_exit_prime_paths () |
| 1941 |
{ |
| 1942 |
const int cpp01[] = { 0, 1, 2, 3, 10 }; |
| 1943 |
const int cpp02[] = { 0, 1, 2, 4, 6, 8, 10 }; |
| 1944 |
trie complete_prime_paths {}; |
| 1945 |
complete_prime_paths.insert_with_suffix (cpp01); |
| 1946 |
complete_prime_paths.insert_with_suffix (cpp02); |
| 1947 |
|
| 1948 |
const int ep01[] = { 4, 6, 9, 7, 2 }; |
| 1949 |
const int ep02[] = { 5, 7, 2, 4, 6 }; |
| 1950 |
const int ep03[] = { 9, 7, 2, 4, 6 }; |
| 1951 |
const int ep04[] = { 4, 5, 7, 2 }; |
| 1952 |
trie exit_paths; |
| 1953 |
exit_paths.insert (ep01); |
| 1954 |
exit_paths.insert (ep02); |
| 1955 |
exit_paths.insert (ep03); |
| 1956 |
exit_paths.insert (ep04); |
| 1957 |
|
| 1958 |
auto_graph cfg (binary_search_cfg ()); |
| 1959 |
trie epp = scc_exit_prime_paths (cfg, exit_paths, complete_prime_paths); |
| 1960 |
|
| 1961 |
const int pp01[] = { 4, 6, 9, 7, 2, 3, 10 }; |
| 1962 |
const int pp02[] = { 5, 7, 2, 4, 6, 8, 10 }; |
| 1963 |
const int pp03[] = { 9, 7, 2, 4, 6, 8, 10 }; |
| 1964 |
const int pp04[] = { 4, 5, 7, 2, 3, 10 }; |
| 1965 |
|
| 1966 |
ASSERT_EQ (count (epp), 4); |
| 1967 |
ASSERT_TRUE (contains (epp, pp01)); |
| 1968 |
ASSERT_TRUE (contains (epp, pp02)); |
| 1969 |
ASSERT_TRUE (contains (epp, pp03)); |
| 1970 |
ASSERT_TRUE (contains (epp, pp04)); |
| 1971 |
} |
| 1972 |
|
| 1973 |
static void |
| 1974 |
test_entry_prime_paths () |
| 1975 |
{ |
| 1976 |
auto_graph cfg (binary_search_cfg ()); |
| 1977 |
|
| 1978 |
static int sccep01[] = { 2, 4, 6, 9, 7 }; |
| 1979 |
static int sccep02[] = { 2, 4, 5, 7 }; |
| 1980 |
trie scc_entry_paths; |
| 1981 |
scc_entry_paths.insert (sccep01); |
| 1982 |
scc_entry_paths.insert (sccep02); |
| 1983 |
|
| 1984 |
const int cpp01[] = { 0, 1, 2, 3, 10 }; |
| 1985 |
const int cpp02[] = { 0, 1, 2, 4, 6, 8, 10 }; |
| 1986 |
trie complete_prime_paths {}; |
| 1987 |
complete_prime_paths.insert (cpp01); |
| 1988 |
complete_prime_paths.insert (cpp02); |
| 1989 |
|
| 1990 |
const int ep01[] = { 4, 6, 9, 7, 2, 3, 10 }; |
| 1991 |
const int ep02[] = { 5, 7, 2, 4, 6, 8, 10 }; |
| 1992 |
const int ep03[] = { 9, 7, 2, 4, 6, 8, 10 }; |
| 1993 |
const int ep04[] = { 4, 5, 7, 2, 3, 10 }; |
| 1994 |
trie exit_prime_paths {}; |
| 1995 |
exit_prime_paths.insert (ep01); |
| 1996 |
exit_prime_paths.insert (ep02); |
| 1997 |
exit_prime_paths.insert (ep03); |
| 1998 |
exit_prime_paths.insert (ep04); |
| 1999 |
|
| 2000 |
vec<int> sccs = binary_search_scc_map (); |
| 2001 |
|
| 2002 |
trie epp = scc_entry_prime_paths (cfg, scc_entry_paths, |
| 2003 |
complete_prime_paths, |
| 2004 |
exit_prime_paths); |
| 2005 |
|
| 2006 |
/* The 0 (start node) does not show up in the Fazli & Afsharchi paper and |
| 2007 |
kinda, but has no real impact on the result. The prime-paths functions |
| 2008 |
do not care about these vertices, but the path-coverage instrumentation |
| 2009 |
filters out the ENTRY/EXIT blocks from all the paths. */ |
| 2010 |
const int pp01[] = { 0, 1, 2, 4, 6, 9, 7 }; |
| 2011 |
const int pp02[] = { 0, 1, 2, 4, 5, 7 }; |
| 2012 |
ASSERT_EQ (count (epp), 2); |
| 2013 |
ASSERT_TRUE (contains (epp, pp01)); |
| 2014 |
ASSERT_TRUE (contains (epp, pp02)); |
| 2015 |
} |
| 2016 |
|
| 2017 |
/* A straight-line graph with one vertex should yield a single path of length 1 |
| 2018 |
with just the non-exit non-entry node in it. */ |
| 2019 |
void |
| 2020 |
test_singleton_path () |
| 2021 |
{ |
| 2022 |
auto_graph cfg (new_graph (3)); |
| 2023 |
add_edge (cfg, 0, 2); |
| 2024 |
add_edge (cfg, 2, 1); |
| 2025 |
vec<vec<int>> paths = prime_paths (cfg, 100); |
| 2026 |
|
| 2027 |
ASSERT_EQ (paths.length (), 1); |
| 2028 |
ASSERT_EQ (paths[0].length (), 3); |
| 2029 |
ASSERT_EQ (paths[0][0], 0); |
| 2030 |
ASSERT_EQ (paths[0][1], 2); |
| 2031 |
ASSERT_EQ (paths[0][2], 1); |
| 2032 |
release_vec_vec (paths); |
| 2033 |
} |
| 2034 |
|
| 2035 |
void |
| 2036 |
path_coverage_cc_tests () |
| 2037 |
{ |
| 2038 |
limit_reset (250000); |
| 2039 |
test_prime_paths (); |
| 2040 |
test_build_ccfg (); |
| 2041 |
test_split_components (); |
| 2042 |
test_scc_internal_prime_paths (); |
| 2043 |
test_scc_entry_exit_paths (); |
| 2044 |
test_complete_prime_paths (); |
| 2045 |
test_exit_prime_paths (); |
| 2046 |
test_entry_prime_paths (); |
| 2047 |
test_singleton_path (); |
| 2048 |
} |
| 2049 |
|
| 2050 |
} // namespace selftest |
| 2051 |
|
| 2052 |
#endif /* #if CHECKING_P */ |