From 727a5d543d662231224002c3345f78b0bebf8c6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Steinbrink?= Date: Tue, 25 Aug 2015 18:29:24 +0200 Subject: [PATCH] Prefer alloc_ty() instead of alloca() where possible --- src/librustc_trans/trans/_match.rs | 3 +-- src/librustc_trans/trans/base.rs | 3 +-- src/librustc_trans/trans/datum.rs | 7 ++----- src/librustc_trans/trans/expr.rs | 6 ++---- src/librustc_trans/trans/foreign.rs | 5 +---- src/librustc_trans/trans/glue.rs | 2 +- src/librustc_trans/trans/tvec.rs | 3 +-- 7 files changed, 9 insertions(+), 20 deletions(-) diff --git a/src/librustc_trans/trans/_match.rs b/src/librustc_trans/trans/_match.rs index 57bac4d4f81bb..e964afc7b699e 100644 --- a/src/librustc_trans/trans/_match.rs +++ b/src/librustc_trans/trans/_match.rs @@ -1196,8 +1196,7 @@ fn compile_submatch_continue<'a, 'p, 'blk, 'tcx>(mut bcx: Block<'blk, 'tcx>, let unsized_ty = def.struct_variant().fields.last().map(|field| { monomorphize::field_ty(bcx.tcx(), substs, field) }).unwrap(); - let llty = type_of::type_of(bcx.ccx(), unsized_ty); - let scratch = alloca(bcx, llty, "__struct_field_fat_ptr"); + let scratch = alloc_ty(bcx, unsized_ty, "__struct_field_fat_ptr"); let data = adt::trans_field_ptr(bcx, &*repr, struct_val, 0, arg_count); let len = Load(bcx, expr::get_meta(bcx, val.val)); Store(bcx, data, expr::get_dataptr(bcx, scratch)); diff --git a/src/librustc_trans/trans/base.rs b/src/librustc_trans/trans/base.rs index 09cab294c47d5..28047ee5812e7 100644 --- a/src/librustc_trans/trans/base.rs +++ b/src/librustc_trans/trans/base.rs @@ -1020,8 +1020,7 @@ pub fn alloc_ty<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, t: Ty<'tcx>, name: &str) -> let ccx = bcx.ccx(); let ty = type_of::type_of(ccx, t); assert!(!t.has_param_types()); - let val = alloca(bcx, ty, name); - return val; + alloca(bcx, ty, name) } pub fn alloca(cx: Block, ty: Type, name: &str) -> ValueRef { diff --git a/src/librustc_trans/trans/datum.rs b/src/librustc_trans/trans/datum.rs index 44bebe71235b6..e0a65c93669c4 100644 --- a/src/librustc_trans/trans/datum.rs +++ b/src/librustc_trans/trans/datum.rs @@ -101,7 +101,6 @@ use trans::cleanup; use trans::cleanup::{CleanupMethods, DropHintDatum, DropHintMethods}; use trans::expr; use trans::tvec; -use trans::type_of; use middle::ty::Ty; use std::fmt; @@ -302,8 +301,7 @@ pub fn lvalue_scratch_datum<'blk, 'tcx, A, F>(bcx: Block<'blk, 'tcx>, -> DatumBlock<'blk, 'tcx, Lvalue> where F: FnOnce(A, Block<'blk, 'tcx>, ValueRef) -> Block<'blk, 'tcx>, { - let llty = type_of::type_of(bcx.ccx(), ty); - let scratch = alloca(bcx, llty, name); + let scratch = alloc_ty(bcx, ty, name); // Subtle. Populate the scratch memory *before* scheduling cleanup. call_lifetime_start(bcx, scratch); @@ -323,8 +321,7 @@ pub fn rvalue_scratch_datum<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, ty: Ty<'tcx>, name: &str) -> Datum<'tcx, Rvalue> { - let llty = type_of::type_of(bcx.ccx(), ty); - let scratch = alloca(bcx, llty, name); + let scratch = alloc_ty(bcx, ty, name); call_lifetime_start(bcx, scratch); Datum::new(scratch, ty, Rvalue::new(ByRef)) } diff --git a/src/librustc_trans/trans/expr.rs b/src/librustc_trans/trans/expr.rs index 4736bbad5c264..b17b3e1395aac 100644 --- a/src/librustc_trans/trans/expr.rs +++ b/src/librustc_trans/trans/expr.rs @@ -246,8 +246,7 @@ pub fn trans<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, // Maybe just get the value directly, instead of loading it? immediate_rvalue(load_ty(bcx, global, const_ty), const_ty) } else { - let llty = type_of::type_of(bcx.ccx(), const_ty); - let scratch = alloca(bcx, llty, "const"); + let scratch = alloc_ty(bcx, const_ty, "const"); call_lifetime_start(bcx, scratch); let lldest = if !const_ty.is_structural() { // Cast pointer to slot, because constants have different types. @@ -410,9 +409,8 @@ fn apply_adjustments<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, datum.to_rvalue_datum(bcx, "__coerce_source")); let target = bcx.monomorphize(&target); - let llty = type_of::type_of(bcx.ccx(), target); - let scratch = alloca(bcx, llty, "__coerce_target"); + let scratch = alloc_ty(bcx, target, "__coerce_target"); call_lifetime_start(bcx, scratch); let target_datum = Datum::new(scratch, target, Rvalue::new(ByRef)); diff --git a/src/librustc_trans/trans/foreign.rs b/src/librustc_trans/trans/foreign.rs index fa4151348fde2..4949539c136d0 100644 --- a/src/librustc_trans/trans/foreign.rs +++ b/src/librustc_trans/trans/foreign.rs @@ -296,10 +296,7 @@ pub fn trans_native_call<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, // Ensure that we always have the Rust value indirectly, // because it makes bitcasting easier. if !rust_indirect { - let scratch = - base::alloca(bcx, - type_of::type_of(ccx, passed_arg_tys[i]), - "__arg"); + let scratch = base::alloc_ty(bcx, passed_arg_tys[i], "__arg"); if type_is_fat_ptr(ccx.tcx(), passed_arg_tys[i]) { Store(bcx, llargs_rust[i + offset], expr::get_dataptr(bcx, scratch)); Store(bcx, llargs_rust[i + offset + 1], expr::get_meta(bcx, scratch)); diff --git a/src/librustc_trans/trans/glue.rs b/src/librustc_trans/trans/glue.rs index 63ac662d4e3e3..7a7e043687153 100644 --- a/src/librustc_trans/trans/glue.rs +++ b/src/librustc_trans/trans/glue.rs @@ -187,7 +187,7 @@ pub fn drop_ty_immediate<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, skip_dtor: bool) -> Block<'blk, 'tcx> { let _icx = push_ctxt("drop_ty_immediate"); - let vp = alloca(bcx, type_of(bcx.ccx(), t), ""); + let vp = alloc_ty(bcx, t, ""); call_lifetime_start(bcx, vp); store_ty(bcx, v, vp, t); let bcx = drop_ty_core(bcx, vp, t, debug_loc, skip_dtor, None); diff --git a/src/librustc_trans/trans/tvec.rs b/src/librustc_trans/trans/tvec.rs index 41c6f827958ec..019c38869b27d 100644 --- a/src/librustc_trans/trans/tvec.rs +++ b/src/librustc_trans/trans/tvec.rs @@ -106,11 +106,10 @@ pub fn trans_slice_vec<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, debug!(" vt={}, count={}", vt.to_string(ccx), count); let fixed_ty = bcx.tcx().mk_array(vt.unit_ty, count); - let llfixed_ty = type_of::type_of(bcx.ccx(), fixed_ty); // Always create an alloca even if zero-sized, to preserve // the non-null invariant of the inner slice ptr - let llfixed = base::alloca(bcx, llfixed_ty, ""); + let llfixed = base::alloc_ty(bcx, fixed_ty, ""); call_lifetime_start(bcx, llfixed); if count > 0 {