Skip to content

Commit fba5802

Browse files
committed
Make use of __builtin_unreachable
The purpose of __builtin_unreachable() is to assist the compiler in: - Eliminating dead code that the programmer knows will never be executed. - Linearizing the code by indicating to the compiler that the path is 'cold' (a similar effect can be achieved by calling a noreturn function).
1 parent fa04997 commit fba5802

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/common.h

+13
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,19 @@
2424
#define __ALIGNED(x)
2525
#endif
2626

27+
/* The purpose of __builtin_unreachable() is to assist the compiler in:
28+
* - Eliminating dead code that the programmer knows will never be executed.
29+
* - Linearizing the code by indicating to the compiler that the path is 'cold'
30+
* (a similar effect can be achieved by calling a noreturn function).
31+
*/
32+
#if defined(__GNUC__) || defined(__clang__)
33+
#define __UNREACHABLE __builtin_unreachable()
34+
#else /* unspported compilers */
35+
/* clang-format off */
36+
#define __UNREACHABLE do { /* nop */ } while (0)
37+
/* clang-format on */
38+
#endif
39+
2740
/* Non-optimized builds do not have tail-call optimization (TCO). To work
2841
* around this, the compiler attribute 'musttail' is used, which forces TCO
2942
* even without optimizations enabled.

src/decode.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1411,7 +1411,7 @@ static inline bool op_cmisc_alu(rv_insn_t *ir, const uint32_t insn)
14111411
assert(!"Instruction reserved");
14121412
break;
14131413
default:
1414-
assert(!"Should not be reachable");
1414+
__UNREACHABLE;
14151415
break;
14161416
}
14171417
break;
@@ -1598,7 +1598,7 @@ static inline bool op_ccr(rv_insn_t *ir, const uint32_t insn)
15981598
}
15991599
break;
16001600
default:
1601-
assert(!"Should be unreachable.");
1601+
__UNREACHABLE;
16021602
break;
16031603
}
16041604
return true;

0 commit comments

Comments
 (0)