__builtin_expect hint ignored


For the code below generated assembly is the worst possible - test 1, else test 3, else test 2 with expected value of 2 last ... Whats going on?

long case_2();
long case_3();
long case_1();
long case_else();
long test(long a) {
    switch (__builtin_expect(a, 2)) {
        case 2: return case_2();
        case 3: return case_3();
        case 1: return case_1();
        case 0:
        case 4:
        case 5: return case_else();
        default: __builtin_unreachable();

I see this with clang trunk on the website too. Perhaps of interest to llvm-dev?

Interestingly, clang 3.6 seemed to do the right thing, but 3.7 onwards ignores __builtin_expect for this particular case.

All versions of gcc and Intel both generate jump tables, which I assume is even less optimal in this case.