Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: add likely and unlikely intrinsics #36181

Merged
merged 1 commit into from
Sep 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/libcore/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,20 @@ extern "rust-intrinsic" {
/// own, or if it does not enable any significant optimizations.
pub fn assume(b: bool);

#[cfg(not(stage0))]
/// Hints to the compiler that branch condition is likely to be true.
/// Returns the value passed to it.
///
/// Any use other than with `if` statements will probably not have an effect.
pub fn likely(b: bool) -> bool;

#[cfg(not(stage0))]
/// Hints to the compiler that branch condition is likely to be false.
/// Returns the value passed to it.
///
/// Any use other than with `if` statements will probably not have an effect.
pub fn unlikely(b: bool) -> bool;

/// Executes a breakpoint trap, for inspection by a debugger.
pub fn breakpoint();

Expand Down
8 changes: 8 additions & 0 deletions src/librustc_trans/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,14 @@ pub fn trans_intrinsic_call<'a, 'blk, 'tcx>(mut bcx: Block<'blk, 'tcx>,
(Some(llfn), _) => {
Call(bcx, llfn, &llargs, call_debug_location)
}
(_, "likely") => {
let expect = ccx.get_intrinsic(&("llvm.expect.i1"));
Call(bcx, expect, &[llargs[0], C_bool(ccx, true)], call_debug_location)
}
(_, "unlikely") => {
let expect = ccx.get_intrinsic(&("llvm.expect.i1"));
Call(bcx, expect, &[llargs[0], C_bool(ccx, false)], call_debug_location)
}
(_, "try") => {
bcx = try_intrinsic(bcx, llargs[0], llargs[1], llargs[2], llresult,
call_debug_location);
Expand Down
2 changes: 2 additions & 0 deletions src/librustc_typeck/check/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,8 @@ pub fn check_intrinsic_type(ccx: &CrateCtxt, it: &hir::ForeignItem) {
(1, vec![param(ccx, 0), param(ccx, 0)], param(ccx, 0)),

"assume" => (0, vec![tcx.types.bool], tcx.mk_nil()),
"likely" => (0, vec![tcx.types.bool], tcx.types.bool),
"unlikely" => (0, vec![tcx.types.bool], tcx.types.bool),

"discriminant_value" => (1, vec![
tcx.mk_imm_ref(tcx.mk_region(ty::ReLateBound(ty::DebruijnIndex::new(1),
Expand Down
41 changes: 41 additions & 0 deletions src/test/codegen/likely.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// compile-flags: -C no-prepopulate-passes

#![crate_type = "lib"]
#![feature(core_intrinsics)]

use std::intrinsics::{likely,unlikely};

#[no_mangle]
pub fn check_likely(x: i32, y: i32) -> Option<i32> {
unsafe {
// CHECK: call i1 @llvm.expect.i1(i1 %{{.*}}, i1 true)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you check some surroundings around this intrinsic too? I.e. that icmp or something goes before the intrinsic and that branch goes right after the intrinsic?

It would look something along the lines of

CHECK: [[cond:%[0-9]+]] = icmp ...
CHECK-NEXT: call i1 @llvm.expect.i1(i1 [[cond]], i1 true)
CHECK-NEXT: br [[cond]] ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems there's a couple of casts and things in between. This is the relevant part of the likely.ll file:

start:
  %2 = icmp eq i32 %0, %1
  %3 = call i1 @llvm.expect.i1(i1 %2, i1 true)
  %4 = zext i1 %3 to i8
  store i8 %4, i8* %tmp_ret
  %5 = load i8, i8* %tmp_ret, !range !0
  %6 = trunc i8 %5 to i1
  br label %bb1

bb1:
  br i1 %6, label %bb2, label %bb3

if likely(x == y) {
None
} else {
Some(x + y)
}
}
}

#[no_mangle]
pub fn check_unlikely(x: i32, y: i32) -> Option<i32> {
unsafe {
// CHECK: call i1 @llvm.expect.i1(i1 %{{.*}}, i1 false)
if unlikely(x == y) {
None
} else {
Some(x + y)
}
}
}