Skip to content

Commit a2dbed9

Browse files
committed
Add likely/unlikely intrinsics
1 parent 89485b2 commit a2dbed9

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

src/libcore/intrinsics.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,4 +602,9 @@ extern "rust-intrinsic" {
602602
/// Returns the value of the discriminant for the variant in 'v',
603603
/// cast to a `u64`; if `T` has no discriminant, returns 0.
604604
pub fn discriminant_value<T>(v: &T) -> u64;
605+
606+
#[cfg(not(stage0))]
607+
pub fn likely(v: bool) -> bool;
608+
#[cfg(not(stage0))]
609+
pub fn unlikely(v: bool) -> bool;
605610
}

src/librustc_trans/trans/intrinsic.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,17 @@ pub fn trans_intrinsic_call<'a, 'blk, 'tcx>(mut bcx: Block<'blk, 'tcx>,
763763
}
764764
}
765765

766+
(_, "likely") => {
767+
let llfn = ccx.get_intrinsic(&("llvm.expect.i1"));
768+
let expected = C_bool(ccx, true);
769+
Call(bcx, llfn, &[llargs[0], expected], None, call_debug_location)
770+
}
771+
(_, "unlikely") => {
772+
let llfn = ccx.get_intrinsic(&("llvm.expect.i1"));
773+
let expected = C_bool(ccx, false);
774+
Call(bcx, llfn, &[llargs[0], expected], None, call_debug_location)
775+
}
776+
766777
// This requires that atomic intrinsics follow a specific naming pattern:
767778
// "atomic_<operation>[_<ordering>]", and no ordering means SeqCst
768779
(_, name) if name.starts_with("atomic_") => {

src/librustc_typeck/check/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5239,6 +5239,7 @@ pub fn check_intrinsic_type(ccx: &CrateCtxt, it: &ast::ForeignItem) {
52395239
tcx.mk_region(ty::ReLateBound(ty::DebruijnIndex::new(1),
52405240
ty::BrAnon(0))),
52415241
param(ccx, 0))], tcx.types.u64),
5242+
"likely" | "unlikely" => (0, vec![tcx.types.bool], tcx.types.bool),
52425243

52435244
ref other => {
52445245
span_err!(tcx.sess, it.span, E0093,

0 commit comments

Comments
 (0)