Skip to content

Commit 441313f

Browse files
committed
librustc: emit loop for expr_repeat instead of 2n instructions in [x, ..n]
1 parent a56ec8c commit 441313f

File tree

1 file changed

+41
-16
lines changed

1 file changed

+41
-16
lines changed

src/librustc/middle/trans/tvec.rs

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use middle::ty;
2727
use util::common::indenter;
2828
use util::ppaux::ty_to_str;
2929

30+
use core::option::None;
3031
use core::uint;
3132
use core::vec;
3233
use syntax::ast;
@@ -413,30 +414,54 @@ pub fn write_content(bcx: block,
413414
return bcx;
414415
}
415416

416-
let tmpdatum = unpack_datum!(bcx, {
417+
let elem = unpack_datum!(bcx, {
417418
expr::trans_to_datum(bcx, element)
418419
});
419420

420-
let mut temp_cleanups = ~[];
421+
let next_bcx = sub_block(bcx, ~"expr_repeat: while next");
422+
let loop_bcx = loop_scope_block(bcx, next_bcx, None, ~"expr_repeat", None);
423+
let cond_bcx = scope_block(loop_bcx, None, ~"expr_repeat: loop cond");
424+
let set_bcx = scope_block(loop_bcx, None, ~"expr_repeat: body: set");
425+
let inc_bcx = scope_block(loop_bcx, None, ~"expr_repeat: body: inc");
426+
Br(bcx, loop_bcx.llbb);
421427

422-
for uint::range(0, count) |i| {
423-
let lleltptr = GEPi(bcx, lldest, [i]);
424-
if i < count - 1 {
425-
// Copy all but the last one in.
426-
bcx = tmpdatum.copy_to(bcx, INIT, lleltptr);
427-
} else {
428-
// Move the last one in.
429-
bcx = tmpdatum.move_to(bcx, INIT, lleltptr);
430-
}
431-
add_clean_temp_mem(bcx, lleltptr, vt.unit_ty);
432-
temp_cleanups.push(lleltptr);
428+
let loop_counter = {
429+
// i = 0
430+
let i = alloca(loop_bcx, T_i64());
431+
Store(loop_bcx, C_i64(0), i);
432+
433+
Br(loop_bcx, cond_bcx.llbb);
434+
i
435+
};
436+
437+
{ // i < count
438+
let lhs = Load(cond_bcx, loop_counter);
439+
let rhs = C_integral(T_i64(), count as u64, lib::llvm::False);
440+
let cmp_lr = ICmp(cond_bcx, lib::llvm::IntULT, lhs, rhs);
441+
let zext = ZExt(cond_bcx, cmp_lr, T_i8());
442+
let cond_val = ICmp(cond_bcx, lib::llvm::IntNE, zext, C_u8(0));
443+
444+
CondBr(cond_bcx, cond_val, set_bcx.llbb, next_bcx.llbb);
433445
}
434446

435-
for vec::each(temp_cleanups) |cleanup| {
436-
revoke_clean(bcx, *cleanup);
447+
{ // v[i] = elem
448+
let i = Load(set_bcx, loop_counter);
449+
let lleltptr = InBoundsGEP(set_bcx, lldest, [i]);
450+
let set_bcx = elem.copy_to(set_bcx, INIT, lleltptr);
451+
452+
Br(set_bcx, inc_bcx.llbb);
437453
}
438454

439-
return bcx;
455+
{ // i += 1
456+
let i = Load(inc_bcx, loop_counter);
457+
let plusone = Add(inc_bcx, i, C_i64(1));
458+
Store(inc_bcx, plusone, loop_counter);
459+
460+
Br(inc_bcx, cond_bcx.llbb);
461+
}
462+
463+
return next_bcx;
464+
440465
}
441466
}
442467
}

0 commit comments

Comments
 (0)