Skip to content

Commit ee4ba44

Browse files
committed
Issue #3402: Load immediate rvalues right away
Should lead to smaller stack frames, hopefully reducing the perf hits we saw
1 parent fe9f055 commit ee4ba44

File tree

2 files changed

+73
-13
lines changed

2 files changed

+73
-13
lines changed

src/rustc/middle/trans/datum.rs

Lines changed: 61 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -373,18 +373,39 @@ impl Datum {
373373
self.source)
374374
}
375375

376+
fn to_value_datum(bcx: block) -> Datum {
377+
/*!
378+
*
379+
* Yields a by-ref form of this datum. This may involve
380+
* creation of a temporary stack slot. The value returned by
381+
* this function is not separately rooted from this datum, so
382+
* it will not live longer than the current datum. */
383+
384+
match self.mode {
385+
ByValue => self,
386+
ByRef => {
387+
Datum {val: self.to_value_llval(bcx), mode: ByValue,
388+
ty: self.ty, source: FromRvalue}
389+
}
390+
}
391+
}
392+
376393
fn to_value_llval(bcx: block) -> ValueRef {
377394
/*!
378395
*
379396
* Yields the value itself. */
380397

381-
match self.mode {
382-
ByValue => self.val,
383-
ByRef => Load(bcx, self.val)
398+
if ty::type_is_nil(self.ty) || ty::type_is_bot(self.ty) {
399+
C_nil()
400+
} else {
401+
match self.mode {
402+
ByValue => self.val,
403+
ByRef => Load(bcx, self.val)
404+
}
384405
}
385406
}
386407

387-
fn to_ref(bcx: block) -> Datum {
408+
fn to_ref_datum(bcx: block) -> Datum {
388409
/*!
389410
*
390411
* Yields a by-ref form of this datum. This may involve
@@ -405,25 +426,52 @@ impl Datum {
405426
match self.mode {
406427
ByRef => self.val,
407428
ByValue => {
408-
let slot = alloc_ty(bcx, self.ty);
409-
Store(bcx, self.val, slot);
410-
slot
429+
if ty::type_is_nil(self.ty) || ty::type_is_bot(self.ty) {
430+
C_null(T_ptr(type_of::type_of(bcx.ccx(), self.ty)))
431+
} else {
432+
let slot = alloc_ty(bcx, self.ty);
433+
Store(bcx, self.val, slot);
434+
slot
435+
}
411436
}
412437
}
413438
}
414439

415-
fn to_appropriate_llval(bcx: block) -> ValueRef {
440+
fn appropriate_mode() -> DatumMode {
416441
/*!
417442
*
418-
* Yields something that is by value if the type is immediate
419-
* and by ref otherwise. */
443+
* Indicates the "appropriate" mode for this value,
444+
* which is either by ref or by value, depending
445+
* on whether type is iimmediate or what. */
420446

421447
if ty::type_is_nil(self.ty) || ty::type_is_bot(self.ty) {
422-
self.to_value_llval(bcx)
448+
ByValue
423449
} else if ty::type_is_immediate(self.ty) {
424-
self.to_value_llval(bcx)
450+
ByValue
425451
} else {
426-
self.to_ref_llval(bcx)
452+
ByRef
453+
}
454+
}
455+
456+
fn to_appropriate_llval(bcx: block) -> ValueRef {
457+
/*!
458+
*
459+
* Yields an llvalue with the `appropriate_mode()`. */
460+
461+
match self.appropriate_mode() {
462+
ByValue => self.to_value_llval(bcx),
463+
ByRef => self.to_ref_llval(bcx)
464+
}
465+
}
466+
467+
fn to_appropriate_datum(bcx: block) -> Datum {
468+
/*!
469+
*
470+
* Yields a datum with the `appropriate_mode()`. */
471+
472+
match self.appropriate_mode() {
473+
ByValue => self.to_value_datum(bcx),
474+
ByRef => self.to_ref_datum(bcx)
427475
}
428476
}
429477

src/rustc/middle/trans/expr.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,18 @@ fn trans_to_datum(bcx: block, expr: @ast::expr) -> DatumBlock {
206206
} else {
207207
let scratch = scratch_datum(bcx, ty, false);
208208
bcx = trans_rvalue_dps(bcx, expr, SaveIn(scratch.val));
209+
210+
// Note: this is not obviously a good idea. It causes
211+
// immediate values to be loaded immediately after a
212+
// return from a call or other similar expression,
213+
// which in turn leads to alloca's having shorter
214+
// lifetimes and hence larger stack frames. However,
215+
// in turn it can lead to more register pressure.
216+
// Still, in practice it seems to increase
217+
// performance, since we have fewer problems with
218+
// morestack churn.
219+
let scratch = scratch.to_appropriate_datum(bcx);
220+
209221
scratch.add_clean(bcx);
210222
return DatumBlock {bcx: bcx, datum: scratch};
211223
}

0 commit comments

Comments
 (0)