commit
8f8f0ec2c6
@ -491,11 +491,29 @@ fn trans_rvalue_stmt_unadjusted(bcx: block, expr: @ast::expr) -> block {
|
||||
ast::expr_swap(dst, src) => {
|
||||
let dst_datum = unpack_datum!(bcx, trans_lvalue(bcx, dst));
|
||||
let src_datum = unpack_datum!(bcx, trans_lvalue(bcx, src));
|
||||
let scratch = scratch_datum(bcx, dst_datum.ty, false);
|
||||
|
||||
let bcx = dst_datum.move_to_datum(bcx, INIT, scratch);
|
||||
let bcx = src_datum.move_to_datum(bcx, INIT, dst_datum);
|
||||
return scratch.move_to_datum(bcx, INIT, src_datum);
|
||||
// If the source and destination are the same, then don't swap.
|
||||
// Avoids performing an overlapping memcpy
|
||||
let dst_datum_ref = dst_datum.to_ref_llval(bcx);
|
||||
let src_datum_ref = src_datum.to_ref_llval(bcx);
|
||||
let cmp = ICmp(bcx, lib::llvm::IntEQ,
|
||||
src_datum_ref,
|
||||
dst_datum_ref);
|
||||
|
||||
let swap_cx = base::sub_block(bcx, ~"swap");
|
||||
let next_cx = base::sub_block(bcx, ~"next");
|
||||
|
||||
CondBr(bcx, cmp, next_cx.llbb, swap_cx.llbb);
|
||||
|
||||
let scratch = scratch_datum(swap_cx, dst_datum.ty, false);
|
||||
|
||||
let swap_cx = dst_datum.move_to_datum(swap_cx, INIT, scratch);
|
||||
let swap_cx = src_datum.move_to_datum(swap_cx, INIT, dst_datum);
|
||||
let swap_cx = scratch.move_to_datum(swap_cx, INIT, src_datum);
|
||||
|
||||
Br(swap_cx, next_cx.llbb);
|
||||
|
||||
return next_cx;
|
||||
}
|
||||
ast::expr_assign_op(op, dst, src) => {
|
||||
return trans_assign_op(bcx, expr, op, dst, src);
|
||||
|
45
src/test/run-pass/swap-overlapping.rs
Normal file
45
src/test/run-pass/swap-overlapping.rs
Normal file
@ -0,0 +1,45 @@
|
||||
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// Issue #5041 - avoid overlapping memcpy when src and dest of a swap are the same
|
||||
|
||||
pub fn main() {
|
||||
let mut test = TestDescAndFn {
|
||||
desc: TestDesc {
|
||||
name: DynTestName(~"test"),
|
||||
should_fail: false
|
||||
},
|
||||
testfn: DynTestFn(|| ()),
|
||||
};
|
||||
do_swap(&mut test);
|
||||
}
|
||||
|
||||
fn do_swap(test: &mut TestDescAndFn) {
|
||||
*test <-> *test;
|
||||
}
|
||||
|
||||
pub enum TestName {
|
||||
DynTestName(~str)
|
||||
}
|
||||
|
||||
pub enum TestFn {
|
||||
DynTestFn(~fn()),
|
||||
DynBenchFn(~fn(&mut int))
|
||||
}
|
||||
|
||||
pub struct TestDesc {
|
||||
name: TestName,
|
||||
should_fail: bool
|
||||
}
|
||||
|
||||
pub struct TestDescAndFn {
|
||||
desc: TestDesc,
|
||||
testfn: TestFn,
|
||||
}
|
Loading…
Reference in New Issue
Block a user