From 8a1706610b2cf458fe6e5ed12e227e3ca9dcaa6d Mon Sep 17 00:00:00 2001 From: Jed Davis Date: Fri, 22 Feb 2013 22:03:59 -0800 Subject: [PATCH] Avoid unnecessary casts in struct_GEP --- src/librustc/middle/trans/adt.rs | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/librustc/middle/trans/adt.rs b/src/librustc/middle/trans/adt.rs index 21f113a08aa7..37129dce90c7 100644 --- a/src/librustc/middle/trans/adt.rs +++ b/src/librustc/middle/trans/adt.rs @@ -205,24 +205,28 @@ pub fn trans_GEP(bcx: block, r: &Repr, val: ValueRef, discr: int, ix: uint) NoDtor => val, DtorPresent | DtorAbsent => GEPi(bcx, val, [0, 0]) }; - struct_GEP(bcx, st, val, ix) + struct_GEP(bcx, st, val, ix, false) } General(ref cases) => { struct_GEP(bcx, &cases[discr as uint], - GEPi(bcx, val, [0, 1]), ix) + GEPi(bcx, val, [0, 1]), ix, true) } } } -fn struct_GEP(bcx: block, st: &Struct, val: ValueRef, ix: uint) - -> ValueRef { +fn struct_GEP(bcx: block, st: &Struct, val: ValueRef, ix: uint, + needs_cast: bool) -> ValueRef { let ccx = bcx.ccx(); - let real_llty = T_struct(st.fields.map( - |&ty| type_of::type_of(ccx, ty))); - let cast_val = PointerCast(bcx, val, T_ptr(real_llty)); + let val = if needs_cast { + let real_llty = T_struct(st.fields.map( + |&ty| type_of::type_of(ccx, ty))); + PointerCast(bcx, val, T_ptr(real_llty)) + } else { + val + }; - GEPi(bcx, cast_val, [0, ix]) + GEPi(bcx, val, [0, ix]) } pub fn trans_const(ccx: @CrateContext, r: &Repr, discr: int,