Skip to content

Commit 378abdb

Browse files
committed
Simplify the codegen in iter_vec_loop
No need to create a bunch of blocks and a stack allocated temporary just to build a simple loop.
1 parent f1bb6c2 commit 378abdb

File tree

1 file changed

+14
-37
lines changed

1 file changed

+14
-37
lines changed

src/librustc_trans/trans/tvec.rs

Lines changed: 14 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -426,49 +426,26 @@ pub fn iter_vec_loop<'blk, 'tcx, F>(bcx: Block<'blk, 'tcx>,
426426
let _icx = push_ctxt("tvec::iter_vec_loop");
427427
let fcx = bcx.fcx;
428428

429-
let next_bcx = fcx.new_temp_block("expr_repeat: while next");
430429
let loop_bcx = fcx.new_temp_block("expr_repeat");
431-
let cond_bcx = fcx.new_temp_block("expr_repeat: loop cond");
432-
let body_bcx = fcx.new_temp_block("expr_repeat: body: set");
433-
let inc_bcx = fcx.new_temp_block("expr_repeat: body: inc");
434-
Br(bcx, loop_bcx.llbb, DebugLoc::None);
430+
let next_bcx = fcx.new_temp_block("expr_repeat: next");
435431

436-
let loop_counter = {
437-
// i = 0
438-
let i = alloca(loop_bcx, bcx.ccx().int_type(), "__i");
439-
Store(loop_bcx, C_uint(bcx.ccx(), 0us), i);
432+
Br(bcx, loop_bcx.llbb, DebugLoc::None);
440433

441-
Br(loop_bcx, cond_bcx.llbb, DebugLoc::None);
442-
i
443-
};
434+
let loop_counter = Phi(loop_bcx, bcx.ccx().int_type(), &[C_uint(bcx.ccx(), 0us)], &[bcx.llbb]);
444435

445-
{ // i < count
446-
let lhs = Load(cond_bcx, loop_counter);
447-
let rhs = count;
448-
let cond_val = ICmp(cond_bcx, llvm::IntULT, lhs, rhs, DebugLoc::None);
436+
let bcx = loop_bcx;
449437

450-
CondBr(cond_bcx, cond_val, body_bcx.llbb, next_bcx.llbb, DebugLoc::None);
451-
}
452-
453-
{ // loop body
454-
let i = Load(body_bcx, loop_counter);
455-
let lleltptr = if vt.llunit_alloc_size == 0 {
456-
data_ptr
457-
} else {
458-
InBoundsGEP(body_bcx, data_ptr, &[i])
459-
};
460-
let body_bcx = f(body_bcx, lleltptr, vt.unit_ty);
461-
462-
Br(body_bcx, inc_bcx.llbb, DebugLoc::None);
463-
}
464-
465-
{ // i += 1
466-
let i = Load(inc_bcx, loop_counter);
467-
let plusone = Add(inc_bcx, i, C_uint(bcx.ccx(), 1us), DebugLoc::None);
468-
Store(inc_bcx, plusone, loop_counter);
438+
let lleltptr = if vt.llunit_alloc_size == 0 {
439+
data_ptr
440+
} else {
441+
InBoundsGEP(bcx, data_ptr, &[loop_counter])
442+
};
443+
let bcx = f(bcx, lleltptr, vt.unit_ty);
444+
let plusone = Add(bcx, loop_counter, C_uint(bcx.ccx(), 1us), DebugLoc::None);
445+
AddIncomingToPhi(loop_counter, plusone, bcx.llbb);
469446

470-
Br(inc_bcx, cond_bcx.llbb, DebugLoc::None);
471-
}
447+
let cond_val = ICmp(bcx, llvm::IntULT, plusone, count, DebugLoc::None);
448+
CondBr(bcx, cond_val, loop_bcx.llbb, next_bcx.llbb, DebugLoc::None);
472449

473450
next_bcx
474451
}

0 commit comments

Comments
 (0)