Index: bolt/lib/Passes/MCF.cpp =================================================================== --- bolt/lib/Passes/MCF.cpp +++ bolt/lib/Passes/MCF.cpp @@ -264,6 +264,7 @@ continue; Pred->getBranchInfo(*BB).Count = Guessed; + GuessedArcs.insert(std::make_pair(Pred, BB)); return true; } llvm_unreachable("Expected unguessed arc"); Index: bolt/test/Inputs/guess.c =================================================================== --- /dev/null +++ bolt/test/Inputs/guess.c @@ -0,0 +1,28 @@ +// Verify that bolt will not fall in the endless loop when using +// the fdata file in case of enabling iterative-guess option. + +// RUN: %clang %cflags %p/Inputs/guess.c -o %t.exe +// RUN: llvm-bolt %t.exe -o %t -data=%p/Inputs/guess.fdata --iterative-guess + +// The problem arises in the following situation +// BB0 -- BB1 BB0 -> BB1 +// \ | BB1 -> BB2 +// \ | BB0 -> BB2 +// \ | +// BB2 + +int test(int a, int b) { + if (b == 1) { + a = b + 1; + } + return a; +} + +int main() { + long long i = 5000000000; + while (i > 0) { + test(2, 1); + i--; + } + return 0; +} Index: bolt/test/Inputs/guess.fdata =================================================================== --- /dev/null +++ bolt/test/Inputs/guess.fdata @@ -0,0 +1,8 @@ +no_lbr cycles:u: +1 test 20 12 +1 test c 5778 +1 test 30 5494 +1 test 8 1 +1 main 34 15379 +1 main 44 12401 +1 main 38 16433 Index: bolt/test/iterative-guess.test =================================================================== --- /dev/null +++ bolt/test/iterative-guess.test @@ -0,0 +1,7 @@ +# Check that bolt will not fall in the endless loop when using +# the fdata file in case of enabling iterative-guess option. +# The fdata file is generated based on aarch64, this test need to +# be run in aarch64 to make sense + +RUN: %clang %cflags %S/Inputs/guess.c -o %t.exe +RUN: llvm-bolt %t.exe -o %t -data=%S/Inputs/guess.fdata --iterative-guess