Skip to content

Commit 423843e

Browse files
committed
Don't perform swap when src == dst. #5041
1 parent 831840a commit 423843e

File tree

2 files changed

+67
-4
lines changed

2 files changed

+67
-4
lines changed

src/librustc/middle/trans/expr.rs

+22-4
Original file line numberDiff line numberDiff line change
@@ -491,11 +491,29 @@ fn trans_rvalue_stmt_unadjusted(bcx: block, expr: @ast::expr) -> block {
491491
ast::expr_swap(dst, src) => {
492492
let dst_datum = unpack_datum!(bcx, trans_lvalue(bcx, dst));
493493
let src_datum = unpack_datum!(bcx, trans_lvalue(bcx, src));
494-
let scratch = scratch_datum(bcx, dst_datum.ty, false);
495494

496-
let bcx = dst_datum.move_to_datum(bcx, INIT, scratch);
497-
let bcx = src_datum.move_to_datum(bcx, INIT, dst_datum);
498-
return scratch.move_to_datum(bcx, INIT, src_datum);
495+
// If the source and destination are the same, then don't swap.
496+
// Avoids performing an overlapping memcpy
497+
let dst_datum_ref = dst_datum.to_ref_llval(bcx);
498+
let src_datum_ref = src_datum.to_ref_llval(bcx);
499+
let cmp = ICmp(bcx, lib::llvm::IntEQ,
500+
src_datum_ref,
501+
dst_datum_ref);
502+
503+
let swap_cx = base::sub_block(bcx, ~"swap");
504+
let next_cx = base::sub_block(bcx, ~"next");
505+
506+
CondBr(bcx, cmp, next_cx.llbb, swap_cx.llbb);
507+
508+
let scratch = scratch_datum(swap_cx, dst_datum.ty, false);
509+
510+
let swap_cx = dst_datum.move_to_datum(swap_cx, INIT, scratch);
511+
let swap_cx = src_datum.move_to_datum(swap_cx, INIT, dst_datum);
512+
let swap_cx = scratch.move_to_datum(swap_cx, INIT, src_datum);
513+
514+
Br(swap_cx, next_cx.llbb);
515+
516+
return next_cx;
499517
}
500518
ast::expr_assign_op(op, dst, src) => {
501519
return trans_assign_op(bcx, expr, op, dst, src);

src/test/run-pass/swap-overlapping.rs

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Issue #5041 - avoid overlapping memcpy when src and dest of a swap are the same
12+
13+
pub fn main() {
14+
let mut test = TestDescAndFn {
15+
desc: TestDesc {
16+
name: DynTestName(~"test"),
17+
should_fail: false
18+
},
19+
testfn: DynTestFn(|| ()),
20+
};
21+
do_swap(&mut test);
22+
}
23+
24+
fn do_swap(test: &mut TestDescAndFn) {
25+
*test <-> *test;
26+
}
27+
28+
pub enum TestName {
29+
DynTestName(~str)
30+
}
31+
32+
pub enum TestFn {
33+
DynTestFn(~fn()),
34+
DynBenchFn(~fn(&mut int))
35+
}
36+
37+
pub struct TestDesc {
38+
name: TestName,
39+
should_fail: bool
40+
}
41+
42+
pub struct TestDescAndFn {
43+
desc: TestDesc,
44+
testfn: TestFn,
45+
}

0 commit comments

Comments
 (0)