Skip to content

librustc: Stop zeroing out allocas so much. Cuts 300K off librustc. #4686

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

Closed
wants to merge 3 commits into from
Closed
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
7 changes: 6 additions & 1 deletion src/libcore/rt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
//! Runtime calls emitted by the compiler.

use cast::transmute;
use libc::{c_char, c_void, size_t, uintptr_t};
use libc::{c_char, c_uchar, c_void, size_t, uintptr_t};
use managed::raw::BoxRepr;
use str;
use sys;
Expand Down Expand Up @@ -123,6 +123,11 @@ pub unsafe fn check_not_borrowed(a: *u8) {
}
}

#[lang="strdup_uniq"]
pub unsafe fn strdup_uniq(ptr: *c_uchar, len: uint) -> ~str {
str::raw::from_buf_len(ptr, len)
}

// Local Variables:
// mode: rust;
// fill-column: 78;
Expand Down
10 changes: 8 additions & 2 deletions src/librustc/middle/lang_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,17 @@ pub enum LangItem {
BorrowAsImmFnLangItem, // 30
ReturnToMutFnLangItem, // 31
CheckNotBorrowedFnLangItem, // 32
StrDupUniqFnLangItem, // 33
}

struct LanguageItems {
items: [ Option<def_id> * 33 ]
items: [ Option<def_id> * 34 ]
}

impl LanguageItems {
static pub fn new() -> LanguageItems {
LanguageItems {
items: [ None, ..33 ]
items: [ None, ..34 ]
}
}

Expand Down Expand Up @@ -133,6 +134,7 @@ impl LanguageItems {
30 => "borrow_as_imm",
31 => "return_to_mut",
32 => "check_not_borrowed",
33 => "strdup_uniq",

_ => "???"
}
Expand Down Expand Up @@ -243,6 +245,9 @@ impl LanguageItems {
pub fn check_not_borrowed_fn(&const self) -> def_id {
self.items[CheckNotBorrowedFnLangItem as uint].get()
}
pub fn strdup_uniq_fn(&const self) -> def_id {
self.items[StrDupUniqFnLangItem as uint].get()
}
}

fn LanguageItemCollector(crate: @crate,
Expand Down Expand Up @@ -289,6 +294,7 @@ fn LanguageItemCollector(crate: @crate,
item_refs.insert(~"return_to_mut", ReturnToMutFnLangItem as uint);
item_refs.insert(~"check_not_borrowed",
CheckNotBorrowedFnLangItem as uint);
item_refs.insert(~"strdup_uniq", StrDupUniqFnLangItem as uint);

LanguageItemCollector {
crate: crate,
Expand Down
6 changes: 1 addition & 5 deletions src/librustc/middle/trans/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ fn malloc_raw_dyn(bcx: block,

// Allocate space:
let tydesc = PointerCast(bcx, static_ti.tydesc, T_ptr(T_i8()));
let rval = alloca_zeroed(bcx, T_ptr(T_i8()));
let rval = alloca(bcx, T_ptr(T_i8()));
let bcx = callee::trans_rtcall_or_lang_call(
bcx,
langcall,
Expand Down Expand Up @@ -1504,10 +1504,6 @@ fn alloca(cx: block, t: TypeRef) -> ValueRef {
alloca_maybe_zeroed(cx, t, false)
}

fn alloca_zeroed(cx: block, t: TypeRef) -> ValueRef {
alloca_maybe_zeroed(cx, t, true)
}

fn alloca_maybe_zeroed(cx: block, t: TypeRef, zero: bool) -> ValueRef {
let _icx = cx.insn_ctxt("alloca");
if cx.unreachable {
Expand Down
140 changes: 68 additions & 72 deletions src/librustc/middle/trans/callee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,29 +39,29 @@ use syntax::visit;
// Represents a (possibly monomorphized) top-level fn item or method
// item. Note that this is just the fn-ptr and is not a Rust closure
// value (which is a pair).
struct FnData {
pub struct FnData {
llfn: ValueRef,
}

struct MethodData {
pub struct MethodData {
llfn: ValueRef,
llself: ValueRef,
self_ty: ty::t,
self_mode: ast::rmode
}

enum CalleeData {
pub enum CalleeData {
Closure(Datum),
Fn(FnData),
Method(MethodData)
}

struct Callee {
pub struct Callee {
bcx: block,
data: CalleeData
}

fn trans(bcx: block, expr: @ast::expr) -> Callee {
pub fn trans(bcx: block, expr: @ast::expr) -> Callee {
let _icx = bcx.insn_ctxt("trans_callee");

// pick out special kinds of expressions that can be called:
Expand Down Expand Up @@ -133,17 +133,16 @@ fn trans(bcx: block, expr: @ast::expr) -> Callee {
}
}

fn trans_fn_ref_to_callee(bcx: block,
def_id: ast::def_id,
ref_id: ast::node_id) -> Callee
{
pub fn trans_fn_ref_to_callee(bcx: block,
def_id: ast::def_id,
ref_id: ast::node_id) -> Callee {
Callee {bcx: bcx,
data: Fn(trans_fn_ref(bcx, def_id, ref_id))}
}

fn trans_fn_ref(bcx: block,
def_id: ast::def_id,
ref_id: ast::node_id) -> FnData {
pub fn trans_fn_ref(bcx: block,
def_id: ast::def_id,
ref_id: ast::node_id) -> FnData {
/*!
*
* Translates a reference (with id `ref_id`) to the fn/method
Expand All @@ -158,26 +157,25 @@ fn trans_fn_ref(bcx: block,
trans_fn_ref_with_vtables(bcx, def_id, ref_id, type_params, vtables)
}

fn trans_fn_ref_with_vtables_to_callee(bcx: block,
def_id: ast::def_id,
ref_id: ast::node_id,
+type_params: ~[ty::t],
vtables: Option<typeck::vtable_res>)
-> Callee
{
pub fn trans_fn_ref_with_vtables_to_callee(
bcx: block,
def_id: ast::def_id,
ref_id: ast::node_id,
+type_params: ~[ty::t],
vtables: Option<typeck::vtable_res>)
-> Callee {
Callee {bcx: bcx,
data: Fn(trans_fn_ref_with_vtables(bcx, def_id, ref_id,
type_params, vtables))}
}

fn trans_fn_ref_with_vtables(
bcx: block, //
def_id: ast::def_id, // def id of fn
ref_id: ast::node_id, // node id of use of fn; may be zero if N/A
+type_params: ~[ty::t], // values for fn's ty params
vtables: Option<typeck::vtable_res>)
-> FnData
{
pub fn trans_fn_ref_with_vtables(
bcx: block, //
def_id: ast::def_id, // def id of fn
ref_id: ast::node_id, // node id of use of fn; may be zero if N/A
+type_params: ~[ty::t], // values for fn's ty params
vtables: Option<typeck::vtable_res>)
-> FnData {
//!
//
// Translates a reference to a fn/method item, monomorphizing and
Expand Down Expand Up @@ -289,26 +287,25 @@ fn trans_fn_ref_with_vtables(
// ______________________________________________________________________
// Translating calls

fn trans_call(in_cx: block,
call_ex: @ast::expr,
f: @ast::expr,
args: CallArgs,
id: ast::node_id,
dest: expr::Dest)
-> block
{
pub fn trans_call(in_cx: block,
call_ex: @ast::expr,
f: @ast::expr,
args: CallArgs,
id: ast::node_id,
dest: expr::Dest)
-> block {
let _icx = in_cx.insn_ctxt("trans_call");
trans_call_inner(
in_cx, call_ex.info(), expr_ty(in_cx, f), node_id_type(in_cx, id),
|cx| trans(cx, f), args, dest, DontAutorefArg)
}

fn trans_method_call(in_cx: block,
call_ex: @ast::expr,
rcvr: @ast::expr,
args: CallArgs,
dest: expr::Dest)
-> block {
pub fn trans_method_call(in_cx: block,
call_ex: @ast::expr,
rcvr: @ast::expr,
args: CallArgs,
dest: expr::Dest)
-> block {
let _icx = in_cx.insn_ctxt("trans_method_call");
trans_call_inner(
in_cx,
Expand All @@ -335,8 +332,11 @@ fn trans_method_call(in_cx: block,
DontAutorefArg)
}

fn trans_rtcall_or_lang_call(bcx: block, did: ast::def_id, args: ~[ValueRef],
dest: expr::Dest) -> block {
pub fn trans_rtcall_or_lang_call(bcx: block,
did: ast::def_id,
args: ~[ValueRef],
dest: expr::Dest)
-> block {
let fty = if did.crate == ast::local_crate {
ty::node_id_to_type(bcx.ccx().tcx, did.node)
} else {
Expand All @@ -349,11 +349,12 @@ fn trans_rtcall_or_lang_call(bcx: block, did: ast::def_id, args: ~[ValueRef],
ArgVals(args), dest, DontAutorefArg);
}

fn trans_rtcall_or_lang_call_with_type_params(bcx: block,
did: ast::def_id,
args: ~[ValueRef],
type_params: ~[ty::t],
dest: expr::Dest) -> block {
pub fn trans_rtcall_or_lang_call_with_type_params(bcx: block,
did: ast::def_id,
args: ~[ValueRef],
type_params: ~[ty::t],
dest: expr::Dest)
-> block {
let fty;
if did.crate == ast::local_crate {
fty = ty::node_id_to_type(bcx.tcx(), did.node);
Expand Down Expand Up @@ -389,7 +390,7 @@ fn trans_rtcall_or_lang_call_with_type_params(bcx: block,
ArgVals(args), dest, DontAutorefArg);
}

fn body_contains_ret(body: ast::blk) -> bool {
pub fn body_contains_ret(body: ast::blk) -> bool {
let cx = {mut found: false};
visit::visit_block(body, cx, visit::mk_vt(@visit::Visitor {
visit_item: |_i, _cx, _v| { },
Expand All @@ -407,16 +408,15 @@ fn body_contains_ret(body: ast::blk) -> bool {
}

// See [Note-arg-mode]
fn trans_call_inner(
pub fn trans_call_inner(
++in_cx: block,
call_info: Option<node_info>,
fn_expr_ty: ty::t,
ret_ty: ty::t,
get_callee: fn(block) -> Callee,
args: CallArgs,
dest: expr::Dest,
autoref_arg: AutorefArg) -> block
{
autoref_arg: AutorefArg) -> block {
do base::with_scope(in_cx, call_info, ~"call") |cx| {
let ret_in_loop = match /*bad*/copy args {
ArgExprs(args) => {
Expand Down Expand Up @@ -520,21 +520,19 @@ fn trans_call_inner(
}
}


enum CallArgs {
pub enum CallArgs {
ArgExprs(~[@ast::expr]),
ArgVals(~[ValueRef])
}

fn trans_args(cx: block,
llenv: ValueRef,
+args: CallArgs,
fn_ty: ty::t,
dest: expr::Dest,
ret_flag: Option<ValueRef>,
+autoref_arg: AutorefArg)
-> {bcx: block, args: ~[ValueRef], retslot: ValueRef}
{
pub fn trans_args(cx: block,
llenv: ValueRef,
+args: CallArgs,
fn_ty: ty::t,
dest: expr::Dest,
ret_flag: Option<ValueRef>,
+autoref_arg: AutorefArg)
-> {bcx: block, args: ~[ValueRef], retslot: ValueRef} {
let _icx = cx.insn_ctxt("trans_args");
let mut temp_cleanups = ~[];
let arg_tys = ty::ty_fn_args(fn_ty);
Expand Down Expand Up @@ -594,21 +592,19 @@ fn trans_args(cx: block,
return {bcx: bcx, args: llargs, retslot: llretslot};
}

enum AutorefArg {
pub enum AutorefArg {
DontAutorefArg,
DoAutorefArg
}

// temp_cleanups: cleanups that should run only if failure occurs before the
// call takes place:
fn trans_arg_expr(bcx: block,
formal_ty: ty::arg,
arg_expr: @ast::expr,
temp_cleanups: &mut ~[ValueRef],
ret_flag: Option<ValueRef>,
+autoref_arg: AutorefArg)
-> Result
{
pub fn trans_arg_expr(bcx: block,
formal_ty: ty::arg,
arg_expr: @ast::expr,
temp_cleanups: &mut ~[ValueRef],
ret_flag: Option<ValueRef>,
+autoref_arg: AutorefArg) -> Result {
let _icx = bcx.insn_ctxt("trans_arg_expr");
let ccx = bcx.ccx();

Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/trans/closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ fn make_opaque_cbox_take_glue(

// Allocate memory, update original ptr, and copy existing data
let opaque_tydesc = PointerCast(bcx, tydesc, T_ptr(T_i8()));
let rval = alloca_zeroed(bcx, T_ptr(T_i8()));
let rval = alloca(bcx, T_ptr(T_i8()));
let bcx = callee::trans_rtcall_or_lang_call(
bcx,
bcx.tcx().lang_items.exchange_malloc_fn(),
Expand Down
Loading