Skip to content

Commit 0a677bc

Browse files
committed
auto merge of #8462 : thestinger/rust/loop-cleanup, r=cmr
I missed some of this in e7bb33a. Hopefully it's all gone now :).
2 parents 927aff1 + d99d337 commit 0a677bc

File tree

7 files changed

+20
-102
lines changed

7 files changed

+20
-102
lines changed

src/librustc/middle/trans/base.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1624,7 +1624,6 @@ pub fn new_fn_ctxt_w_id(ccx: @mut CrateContext,
16241624
llreturn: None,
16251625
llself: None,
16261626
personality: None,
1627-
loop_ret: None,
16281627
has_immediate_return_value: is_immediate,
16291628
llargs: @mut HashMap::new(),
16301629
lllocals: @mut HashMap::new(),
@@ -1834,8 +1833,7 @@ pub fn trans_closure(ccx: @mut CrateContext,
18341833
id: ast::NodeId,
18351834
attributes: &[ast::Attribute],
18361835
output_type: ty::t,
1837-
maybe_load_env: &fn(@mut FunctionContext),
1838-
finish: &fn(@mut Block)) {
1836+
maybe_load_env: &fn(@mut FunctionContext)) {
18391837
ccx.stats.n_closures += 1;
18401838
let _icx = push_ctxt("trans_closure");
18411839
set_uwtable(llfndecl);
@@ -1885,7 +1883,6 @@ pub fn trans_closure(ccx: @mut CrateContext,
18851883
bcx = controlflow::trans_block(bcx, body, dest);
18861884
}
18871885

1888-
finish(bcx);
18891886
match fcx.llreturn {
18901887
Some(llreturn) => cleanup_and_Br(bcx, bcx_top, llreturn),
18911888
None => bcx = cleanup_block(bcx, Some(bcx_top.llbb))
@@ -1937,8 +1934,7 @@ pub fn trans_fn(ccx: @mut CrateContext,
19371934
&& fcx_has_nonzero_span(fcx) {
19381935
debuginfo::create_function_metadata(fcx);
19391936
}
1940-
},
1941-
|_bcx| { });
1937+
});
19421938
}
19431939

19441940
fn insert_synthetic_type_entries(bcx: @mut Block,

src/librustc/middle/trans/closure.rs

Lines changed: 8 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,13 @@ use middle::moves;
1616
use middle::trans::base::*;
1717
use middle::trans::build::*;
1818
use middle::trans::common::*;
19-
use middle::trans::datum::{Datum, INIT, ByRef, ZeroMem};
19+
use middle::trans::datum::{Datum, INIT};
2020
use middle::trans::expr;
2121
use middle::trans::glue;
2222
use middle::trans::type_of::*;
2323
use middle::ty;
2424
use util::ppaux::ty_to_str;
2525

26-
use middle::trans::type_::Type;
27-
2826
use std::vec;
2927
use syntax::ast;
3028
use syntax::ast_map::path_name;
@@ -259,8 +257,7 @@ pub fn store_environment(bcx: @mut Block,
259257
// collects the upvars and packages them up for store_environment.
260258
pub fn build_closure(bcx0: @mut Block,
261259
cap_vars: &[moves::CaptureVar],
262-
sigil: ast::Sigil,
263-
include_ret_handle: Option<ValueRef>) -> ClosureResult {
260+
sigil: ast::Sigil) -> ClosureResult {
264261
let _icx = push_ctxt("closure::build_closure");
265262

266263
// If we need to, package up the iterator body to call
@@ -288,30 +285,6 @@ pub fn build_closure(bcx0: @mut Block,
288285
}
289286
}
290287

291-
// If this is a `for` loop body, add two special environment
292-
// variables:
293-
for flagptr in include_ret_handle.iter() {
294-
// Flag indicating we have returned (a by-ref bool):
295-
let flag_datum = Datum {val: *flagptr, ty: ty::mk_bool(),
296-
mode: ByRef(ZeroMem)};
297-
env_vals.push(EnvValue {action: EnvRef,
298-
datum: flag_datum});
299-
300-
// Return value (we just pass a by-ref () and cast it later to
301-
// the right thing):
302-
let ret_true = match bcx.fcx.loop_ret {
303-
Some((_, retptr)) => retptr,
304-
None => match bcx.fcx.llretptr {
305-
None => C_null(Type::nil().ptr_to()),
306-
Some(retptr) => PointerCast(bcx, retptr, Type::nil().ptr_to()),
307-
}
308-
};
309-
let ret_datum = Datum {val: ret_true, ty: ty::mk_nil(),
310-
mode: ByRef(ZeroMem)};
311-
env_vals.push(EnvValue {action: EnvRef,
312-
datum: ret_datum});
313-
}
314-
315288
return store_environment(bcx, env_vals, sigil);
316289
}
317290

@@ -321,12 +294,11 @@ pub fn build_closure(bcx0: @mut Block,
321294
pub fn load_environment(fcx: @mut FunctionContext,
322295
cdata_ty: ty::t,
323296
cap_vars: &[moves::CaptureVar],
324-
load_ret_handle: bool,
325297
sigil: ast::Sigil) {
326298
let _icx = push_ctxt("closure::load_environment");
327299

328300
// Don't bother to create the block if there's nothing to load
329-
if cap_vars.len() == 0 && !load_ret_handle {
301+
if cap_vars.len() == 0 {
330302
return;
331303
}
332304

@@ -347,12 +319,6 @@ pub fn load_environment(fcx: @mut FunctionContext,
347319
fcx.llupvars.insert(def_id.node, upvarptr);
348320
i += 1u;
349321
}
350-
if load_ret_handle {
351-
let flagptr = Load(bcx, GEPi(bcx, llcdata, [0u, i]));
352-
let retptr = Load(bcx,
353-
GEPi(bcx, llcdata, [0u, i+1u]));
354-
fcx.loop_ret = Some((flagptr, retptr));
355-
}
356322
}
357323

358324
pub fn trans_expr_fn(bcx: @mut Block,
@@ -361,7 +327,6 @@ pub fn trans_expr_fn(bcx: @mut Block,
361327
body: &ast::Block,
362328
outer_id: ast::NodeId,
363329
user_id: ast::NodeId,
364-
is_loop_body: Option<Option<ValueRef>>,
365330
dest: expr::Dest) -> @mut Block {
366331
/*!
367332
*
@@ -378,7 +343,6 @@ pub fn trans_expr_fn(bcx: @mut Block,
378343
* - `user_id`: The id of the closure as the user expressed it.
379344
Generally the same as `outer_id`
380345
* - `cap_clause`: information about captured variables, if any.
381-
* - `is_loop_body`: `Some()` if this is part of a `for` loop.
382346
* - `dest`: where to write the closure value, which must be a
383347
(fn ptr, env) pair
384348
*/
@@ -405,28 +369,14 @@ pub fn trans_expr_fn(bcx: @mut Block,
405369
"expr_fn");
406370
let llfn = decl_internal_cdecl_fn(ccx.llmod, s, llfnty);
407371

408-
// Always mark inline if this is a loop body. This is important for
409-
// performance on many programs with tight loops.
410-
if is_loop_body.is_some() {
411-
set_always_inline(llfn);
412-
} else {
413-
// Can't hurt.
414-
set_inline_hint(llfn);
415-
}
416-
417-
let real_return_type = if is_loop_body.is_some() {
418-
ty::mk_bool()
419-
} else {
420-
ty::ty_fn_ret(fty)
421-
};
372+
// set an inline hint for all closures
373+
set_inline_hint(llfn);
422374

423375
let Result {bcx: bcx, val: closure} = match sigil {
424376
ast::BorrowedSigil | ast::ManagedSigil | ast::OwnedSigil => {
425377
let cap_vars = ccx.maps.capture_map.get_copy(&user_id);
426-
let ret_handle = match is_loop_body {Some(x) => x,
427-
None => None};
428378
let ClosureResult {llbox, cdata_ty, bcx}
429-
= build_closure(bcx, cap_vars, sigil, ret_handle);
379+
= build_closure(bcx, cap_vars, sigil);
430380
trans_closure(ccx,
431381
sub_path,
432382
decl,
@@ -436,16 +386,8 @@ pub fn trans_expr_fn(bcx: @mut Block,
436386
bcx.fcx.param_substs,
437387
user_id,
438388
[],
439-
real_return_type,
440-
|fcx| load_environment(fcx, cdata_ty, cap_vars,
441-
ret_handle.is_some(), sigil),
442-
|bcx| {
443-
if is_loop_body.is_some() {
444-
Store(bcx,
445-
C_bool(true),
446-
bcx.fcx.llretptr.unwrap());
447-
}
448-
});
389+
ty::ty_fn_ret(fty),
390+
|fcx| load_environment(fcx, cdata_ty, cap_vars, sigil));
449391
rslt(bcx, llbox)
450392
}
451393
};

src/librustc/middle/trans/common.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,6 @@ pub struct FunctionContext {
196196
// The a value alloca'd for calls to upcalls.rust_personality. Used when
197197
// outputting the resume instruction.
198198
personality: Option<ValueRef>,
199-
// If this is a for-loop body that returns, this holds the pointers needed
200-
// for that (flagptr, retptr)
201-
loop_ret: Option<(ValueRef, ValueRef)>,
202199

203200
// True if this function has an immediate return value, false otherwise.
204201
// If this is false, the llretptr will alias the first argument of the

src/librustc/middle/trans/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use back::{upcall};
1313
use driver::session;
1414
use lib::llvm::{ContextRef, ModuleRef, ValueRef};
1515
use lib::llvm::{llvm, TargetData, TypeNames};
16-
use lib::llvm::{mk_target_data, False};
16+
use lib::llvm::mk_target_data;
1717
use metadata::common::LinkMeta;
1818
use middle::astencode;
1919
use middle::resolve;

src/librustc/middle/trans/controlflow.rs

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ use middle::trans::build::*;
2020
use middle::trans::callee;
2121
use middle::trans::common::*;
2222
use middle::trans::expr;
23-
use middle::trans::type_of::*;
2423
use middle::ty;
2524
use util::common::indenter;
2625
use util::ppaux;
@@ -338,29 +337,15 @@ pub fn trans_cont(bcx: @mut Block, label_opt: Option<ident>) -> @mut Block {
338337
pub fn trans_ret(bcx: @mut Block, e: Option<@ast::expr>) -> @mut Block {
339338
let _icx = push_ctxt("trans_ret");
340339
let mut bcx = bcx;
341-
let dest = match bcx.fcx.loop_ret {
342-
Some((flagptr, retptr)) => {
343-
// This is a loop body return. Must set continue flag (our retptr)
344-
// to false, return flag to true, and then store the value in the
345-
// parent's retptr.
346-
Store(bcx, C_bool(true), flagptr);
347-
Store(bcx, C_bool(false), bcx.fcx.llretptr.unwrap());
348-
expr::SaveIn(match e {
349-
Some(x) => PointerCast(bcx, retptr,
350-
type_of(bcx.ccx(), expr_ty(bcx, x)).ptr_to()),
351-
None => retptr
352-
})
353-
}
354-
None => match bcx.fcx.llretptr {
340+
let dest = match bcx.fcx.llretptr {
355341
None => expr::Ignore,
356342
Some(retptr) => expr::SaveIn(retptr),
357-
}
358343
};
359344
match e {
360-
Some(x) => {
361-
bcx = expr::trans_into(bcx, x, dest);
362-
}
363-
_ => ()
345+
Some(x) => {
346+
bcx = expr::trans_into(bcx, x, dest);
347+
}
348+
_ => ()
364349
}
365350
cleanup_and_leave(bcx, None, Some(bcx.fcx.get_llreturn()));
366351
Unreachable(bcx);

src/librustc/middle/trans/expr.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -697,8 +697,7 @@ fn trans_rvalue_dps_unadjusted(bcx: @mut Block, expr: @ast::expr,
697697
expr_to_str(expr, tcx.sess.intr()),
698698
expr_ty.repr(tcx));
699699
return closure::trans_expr_fn(bcx, sigil, decl, body,
700-
expr.id, expr.id,
701-
None, dest);
700+
expr.id, expr.id, dest);
702701
}
703702
ast::expr_do_body(blk) => {
704703
return trans_into(bcx, blk, dest);

src/librustc/middle/typeck/check/regionck.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ fn visit_expr(expr: @ast::expr, (rcx, v): (@mut Rcx, rvt)) {
427427
}
428428

429429
ast::expr_fn_block(*) => {
430-
check_expr_fn_block(rcx, expr, v, false);
430+
check_expr_fn_block(rcx, expr, v);
431431
}
432432

433433
ast::expr_loop(ref body, _) => {
@@ -454,8 +454,7 @@ fn visit_expr(expr: @ast::expr, (rcx, v): (@mut Rcx, rvt)) {
454454

455455
fn check_expr_fn_block(rcx: @mut Rcx,
456456
expr: @ast::expr,
457-
v: rvt,
458-
is_loop_body: bool) {
457+
v: rvt) {
459458
let tcx = rcx.fcx.tcx();
460459
match expr.node {
461460
ast::expr_fn_block(_, ref body) => {
@@ -464,7 +463,7 @@ fn check_expr_fn_block(rcx: @mut Rcx,
464463
ty::ty_closure(
465464
ty::ClosureTy {
466465
sigil: ast::BorrowedSigil, region: region, _}) => {
467-
if get_freevars(tcx, expr.id).is_empty() && !is_loop_body {
466+
if get_freevars(tcx, expr.id).is_empty() {
468467
// No free variables means that the environment
469468
// will be NULL at runtime and hence the closure
470469
// has static lifetime.

0 commit comments

Comments
 (0)