Skip to content

Commit 8c7ee00

Browse files
authored
Merge pull request #5067 from smowton/smowton/fix/natural-loops-multiple-backedges
Natural loops: do not insist that loops only have one backedge
2 parents 6139b6c + 8ce372d commit 8c7ee00

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
int main(int argc, char **argv)
2+
{
3+
loop_header:
4+
--argc;
5+
if(argc == 1)
6+
goto loop_header;
7+
else if(argc == 2)
8+
goto loop_header;
9+
return argc;
10+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
main.c
3+
--show-natural-loops
4+
0 is head of \{ 0, 1, 2, 4 \}
5+
^EXIT=0$
6+
^SIGNAL=0$
7+
--
8+
^warning: ignoring

src/analyses/natural_loops.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,12 @@ void natural_loops_templatet<P, T>::compute_natural_loop(T m, T n)
241241
std::stack<T> stack;
242242

243243
auto insert_result = loop_map.emplace(n, natural_loopt{*this});
244-
INVARIANT(insert_result.second, "each loop head should only be visited once");
244+
// Note the emplace *may* access a loop that already exists: this happens when
245+
// a given header has more than one incoming edge, such as
246+
// head: if(x) goto head; else goto head;
247+
// In this case this compute routine is run twice, one for each backedge, with
248+
// each adding whatever instructions can reach this 'm' (the program point
249+
// that branches to the loop header, 'n').
245250
natural_loopt &loop = insert_result.first->second;
246251

247252
loop.insert_instruction(n);

0 commit comments

Comments
 (0)