Skip to content

Commit 7d51ae1

Browse files
committed
Transfered memcpy and memset to BuilderMethods
1 parent a229789 commit 7d51ae1

File tree

8 files changed

+74
-55
lines changed

8 files changed

+74
-55
lines changed

src/librustc_codegen_llvm/abi.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
// except according to those terms.
1010

1111
use llvm::{self, AttributePlace};
12-
use base;
1312
use builder::{Builder, MemFlags};
1413
use common::C_usize;
1514
use context::CodegenCx;
@@ -239,12 +238,13 @@ impl ArgTypeExt<'ll, 'tcx> for ArgType<'tcx, Ty<'tcx>> {
239238
bx.store(val, llscratch, scratch_align);
240239

241240
// ...and then memcpy it to the intended destination.
242-
base::call_memcpy(bx,
243-
bx.pointercast(dst.llval, cx.type_i8p()),
244-
bx.pointercast(llscratch, cx.type_i8p()),
245-
cx.const_usize(self.layout.size.bytes()),
246-
self.layout.align.min(scratch_align),
247-
MemFlags::empty());
241+
bx.call_memcpy(
242+
bx.pointercast(dst.llval, cx.type_i8p()),
243+
bx.pointercast(llscratch, cx.type_i8p()),
244+
cx.const_usize(self.layout.size.bytes()),
245+
self.layout.align.min(scratch_align),
246+
MemFlags::empty()
247+
);
248248

249249
bx.lifetime_end(llscratch, scratch_size);
250250
}

src/librustc_codegen_llvm/base.rs

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -432,33 +432,6 @@ pub fn to_immediate_scalar<'a, 'll, 'tcx, Builder : BuilderMethods<'a, 'll, 'tcx
432432
val
433433
}
434434

435-
pub fn call_memcpy<'a, 'll: 'a, 'tcx: 'll, Builder : BuilderMethods<'a, 'll, 'tcx>>(
436-
bx: &Builder,
437-
dst: <Builder::CodegenCx as Backend>::Value,
438-
src: <Builder::CodegenCx as Backend>::Value,
439-
n_bytes: <Builder::CodegenCx as Backend>::Value,
440-
align: Align,
441-
flags: MemFlags,
442-
) {
443-
if flags.contains(MemFlags::NONTEMPORAL) {
444-
// HACK(nox): This is inefficient but there is no nontemporal memcpy.
445-
let val = bx.load(src, align);
446-
let ptr = bx.pointercast(dst, bx.cx().type_ptr_to(bx.cx().val_ty(val)));
447-
bx.store_with_flags(val, ptr, align, flags);
448-
return;
449-
}
450-
let cx = bx.cx();
451-
let ptr_width = &bx.sess().target.target.target_pointer_width;
452-
let key = format!("llvm.memcpy.p0i8.p0i8.i{}", ptr_width);
453-
let memcpy = cx.get_intrinsic(&key);
454-
let src_ptr = bx.pointercast(src, cx.type_i8p());
455-
let dst_ptr = bx.pointercast(dst, cx.type_i8p());
456-
let size = bx.intcast(n_bytes, cx.type_isize(), false);
457-
let align = cx.const_i32(align.abi() as i32);
458-
let volatile = cx.const_bool(flags.contains(MemFlags::VOLATILE));
459-
bx.call(memcpy, &[dst_ptr, src_ptr, size, align, volatile], None);
460-
}
461-
462435
pub fn memcpy_ty<'a, 'll: 'a, 'tcx: 'll, Builder : BuilderMethods<'a, 'll, 'tcx>>(
463436
bx: &Builder,
464437
dst: <Builder::CodegenCx as Backend>::Value,
@@ -472,22 +445,7 @@ pub fn memcpy_ty<'a, 'll: 'a, 'tcx: 'll, Builder : BuilderMethods<'a, 'll, 'tcx>
472445
return;
473446
}
474447

475-
call_memcpy(bx, dst, src, bx.cx().const_usize(size), align, flags);
476-
}
477-
478-
pub fn call_memset<'a, 'll: 'a, 'tcx: 'll, Builder : BuilderMethods<'a, 'll, 'tcx>>(
479-
bx: &Builder,
480-
ptr: <Builder::CodegenCx as Backend>::Value,
481-
fill_byte: <Builder::CodegenCx as Backend>::Value,
482-
size: <Builder::CodegenCx as Backend>::Value,
483-
align: <Builder::CodegenCx as Backend>::Value,
484-
volatile: bool,
485-
) -> <Builder::CodegenCx as Backend>::Value {
486-
let ptr_width = &bx.sess().target.target.target_pointer_width;
487-
let intrinsic_key = format!("llvm.memset.p0i8.i{}", ptr_width);
488-
let llintrinsicfn = bx.cx().get_intrinsic(&intrinsic_key);
489-
let volatile = bx.cx().const_bool(volatile);
490-
bx.call(llintrinsicfn, &[ptr, fill_byte, size, align, volatile], None)
448+
bx.call_memcpy(dst, src, bx.cx().const_usize(size), align, flags);
491449
}
492450

493451
pub fn codegen_instance<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx, &'a Value>, instance: Instance<'tcx>) {

src/librustc_codegen_llvm/builder.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,6 +1290,48 @@ impl BuilderMethods<'a, 'll, 'tcx>
12901290
}
12911291
}
12921292

1293+
fn call_memcpy(
1294+
&self,
1295+
dst: &'ll Value,
1296+
src: &'ll Value,
1297+
n_bytes: &'ll Value,
1298+
align: Align,
1299+
flags: MemFlags,
1300+
) {
1301+
if flags.contains(MemFlags::NONTEMPORAL) {
1302+
// HACK(nox): This is inefficient but there is no nontemporal memcpy.
1303+
let val = &self.load(src, align);
1304+
let ptr = &self.pointercast(dst, &self.cx().type_ptr_to(&self.cx().val_ty(val)));
1305+
&self.store_with_flags(val, ptr, align, flags);
1306+
return;
1307+
}
1308+
let cx = &self.cx();
1309+
let ptr_width = &self.sess().target.target.target_pointer_width;
1310+
let key = format!("llvm.memcpy.p0i8.p0i8.i{}", ptr_width);
1311+
let memcpy = cx.get_intrinsic(&key);
1312+
let src_ptr = &self.pointercast(src, cx.type_i8p());
1313+
let dst_ptr = &self.pointercast(dst, cx.type_i8p());
1314+
let size = &self.intcast(n_bytes, cx.type_isize(), false);
1315+
let align = cx.const_i32(align.abi() as i32);
1316+
let volatile = cx.const_bool(flags.contains(MemFlags::VOLATILE));
1317+
&self.call(memcpy, &[dst_ptr, src_ptr, size, align, volatile], None);
1318+
}
1319+
1320+
fn call_memset(
1321+
&self,
1322+
ptr: &'ll Value,
1323+
fill_byte: &'ll Value,
1324+
size: &'ll Value,
1325+
align: &'ll Value,
1326+
volatile: bool,
1327+
) -> &'ll Value {
1328+
let ptr_width = &self.sess().target.target.target_pointer_width;
1329+
let intrinsic_key = format!("llvm.memset.p0i8.i{}", ptr_width);
1330+
let llintrinsicfn = &self.cx().get_intrinsic(&intrinsic_key);
1331+
let volatile = &self.cx().const_bool(volatile);
1332+
&self.call(llintrinsicfn, &[ptr, fill_byte, size, align, volatile], None)
1333+
}
1334+
12931335
fn zext(&self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
12941336
self.count_insn("zext");
12951337
unsafe {

src/librustc_codegen_llvm/interfaces/builder.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,25 @@ pub trait BuilderMethods<'a, 'll :'a, 'tcx: 'll> {
583583
args: &[<Self::CodegenCx as Backend>::Value],
584584
bundle: Option<&OperandBundleDef<'ll, <Self::CodegenCx as Backend>::Value>>
585585
) -> <Self::CodegenCx as Backend>::Value;
586+
587+
fn call_memcpy(
588+
&self,
589+
dst: <Self::CodegenCx as Backend>::Value,
590+
src: <Self::CodegenCx as Backend>::Value,
591+
n_bytes: <Self::CodegenCx as Backend>::Value,
592+
align: Align,
593+
flags: MemFlags,
594+
);
595+
596+
fn call_memset(
597+
&self,
598+
ptr: <Self::CodegenCx as Backend>::Value,
599+
fill_byte: <Self::CodegenCx as Backend>::Value,
600+
size: <Self::CodegenCx as Backend>::Value,
601+
align: <Self::CodegenCx as Backend>::Value,
602+
volatile: bool,
603+
) -> <Self::CodegenCx as Backend>::Value;
604+
586605
fn zext(
587606
&self,
588607
val: <Self::CodegenCx as Backend>::Value,

src/librustc_codegen_llvm/intrinsic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,7 @@ fn memset_intrinsic(
743743
let size = cx.const_usize(size.bytes());
744744
let align = cx.const_i32(align.abi() as i32);
745745
let dst = bx.pointercast(dst, cx.type_i8p());
746-
call_memset(bx, dst, val, bx.mul(size, count), align, volatile)
746+
bx.call_memset(dst, val, bx.mul(size, count), align, volatile)
747747
}
748748

749749
fn try_intrinsic(

src/librustc_codegen_llvm/mir/operand.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ impl OperandValue<&'ll Value> {
348348
// Allocate an appropriate region on the stack, and copy the value into it
349349
let (llsize, _) = glue::size_and_align_of_dst(bx, unsized_ty, Some(llextra));
350350
let lldst = bx.array_alloca(bx.cx().type_i8(), llsize, "unsized_tmp", max_align);
351-
base::call_memcpy(bx, lldst, llptr, llsize, min_align, flags);
351+
bx.call_memcpy(lldst, llptr, llsize, min_align, flags);
352352

353353
// Store the allocated region and the extra to the indirect place.
354354
let indirect_operand = OperandValue::Pair(lldst, llextra);

src/librustc_codegen_llvm/mir/place.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ impl PlaceRef<'tcx, &'ll Value> {
384384
let (size, align) = self.layout.size_and_align();
385385
let size = bx.cx().const_usize(size.bytes());
386386
let align = bx.cx().const_u32(align.abi() as u32);
387-
base::call_memset(bx, llptr, fill_byte, size, align, false);
387+
bx.call_memset(llptr, fill_byte, size, align, false);
388388
}
389389

390390
let niche = self.project_field(bx, 0);

src/librustc_codegen_llvm/mir/rvalue.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,14 @@ impl FunctionCx<'a, 'll, 'tcx, &'ll Value> {
110110
// Use llvm.memset.p0i8.* to initialize all zero arrays
111111
if bx.cx().is_const_integral(v) && bx.cx().const_to_uint(v) == 0 {
112112
let fill = bx.cx().const_u8(0);
113-
base::call_memset(&bx, start, fill, size, align, false);
113+
bx.call_memset(start, fill, size, align, false);
114114
return bx;
115115
}
116116

117117
// Use llvm.memset.p0i8.* to initialize byte arrays
118118
let v = base::from_immediate(&bx, v);
119119
if bx.cx().val_ty(v) == bx.cx().type_i8() {
120-
base::call_memset(&bx, start, v, size, align, false);
120+
bx.call_memset(start, v, size, align, false);
121121
return bx;
122122
}
123123
}

0 commit comments

Comments
 (0)