Skip to content

Commit

Permalink
Make use of __builtin_unreachable
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
jserv committed May 26, 2023
1 parent fa04997 commit fba5802
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
13 changes: 13 additions & 0 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@
#define __ALIGNED(x)
#endif

/* 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).
*/
#if defined(__GNUC__) || defined(__clang__)
#define __UNREACHABLE __builtin_unreachable()
#else /* unspported compilers */
/* clang-format off */
#define __UNREACHABLE do { /* nop */ } while (0)
/* clang-format on */
#endif

/* Non-optimized builds do not have tail-call optimization (TCO). To work
* around this, the compiler attribute 'musttail' is used, which forces TCO
* even without optimizations enabled.
Expand Down
4 changes: 2 additions & 2 deletions src/decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -1411,7 +1411,7 @@ static inline bool op_cmisc_alu(rv_insn_t *ir, const uint32_t insn)
assert(!"Instruction reserved");
break;
default:
assert(!"Should not be reachable");
__UNREACHABLE;
break;
}
break;
Expand Down Expand Up @@ -1598,7 +1598,7 @@ static inline bool op_ccr(rv_insn_t *ir, const uint32_t insn)
}
break;
default:
assert(!"Should be unreachable.");
__UNREACHABLE;
break;
}
return true;
Expand Down

0 comments on commit fba5802

Please sign in to comment.