@@ -278,7 +278,7 @@ fn alloca_maybe_zeroed(cx: block, t: TypeRef, zero: bool) -> ValueRef {
278
278
if cx. unreachable { return llvm:: LLVMGetUndef ( t) ; }
279
279
let initcx = raw_block ( cx. fcx , false , cx. fcx . llstaticallocas ) ;
280
280
let p = Alloca ( initcx, t) ;
281
- if zero { Store ( initcx, C_null ( t ) , p ) ; }
281
+ if zero { memzero ( initcx, p , t ) ; }
282
282
return p;
283
283
}
284
284
@@ -287,10 +287,38 @@ fn zero_mem(cx: block, llptr: ValueRef, t: ty::t) -> block {
287
287
let bcx = cx;
288
288
let ccx = cx. ccx ( ) ;
289
289
let llty = type_of ( ccx, t) ;
290
- Store ( bcx, C_null ( llty ) , llptr ) ;
290
+ memzero ( bcx, llptr , llty ) ;
291
291
return bcx;
292
292
}
293
293
294
+ // Always use this function instead of storing a zero constant to the memory
295
+ // in question. If you store a zero constant, LLVM will drown in vreg
296
+ // allocation for large data structures, and the generated code will be
297
+ // awful. (A telltale sign of this is large quantities of
298
+ // `mov [byte ptr foo],0` in the generated code.)
299
+ fn memzero ( cx : block , llptr : ValueRef , llty : TypeRef ) {
300
+ let _icx = cx. insn_ctxt ( "memzero" ) ;
301
+ let ccx = cx. ccx ( ) ;
302
+
303
+ let intrinsic_key;
304
+ match ccx. sess . targ_cfg . arch {
305
+ session:: arch_x86 | session:: arch_arm => {
306
+ intrinsic_key = ~"llvm. memset . p0i8 . i32 ";
307
+ }
308
+ session:: arch_x86_64 => {
309
+ intrinsic_key = ~"llvm. memset . p0i8 . i64 ";
310
+ }
311
+ }
312
+
313
+ let llintrinsicfn = ccx. intrinsics . get ( intrinsic_key) ;
314
+ let llptr = PointerCast ( cx, llptr, T_ptr ( T_i8 ( ) ) ) ;
315
+ let llzeroval = C_u8 ( 0 ) ;
316
+ let size = IntCast ( cx, llsize_of ( ccx, llty) , ccx. int_type ) ;
317
+ let align = C_i32 ( 1i32 ) ;
318
+ let volatile = C_bool ( false ) ;
319
+ Call ( cx, llintrinsicfn, ~[ llptr, llzeroval, size, align, volatile] ) ;
320
+ }
321
+
294
322
fn arrayalloca ( cx : block , t : TypeRef , v : ValueRef ) -> ValueRef {
295
323
let _icx = cx. insn_ctxt ( "arrayalloca" ) ;
296
324
if cx. unreachable { return llvm:: LLVMGetUndef ( t) ; }
0 commit comments