diff options
| author | Jed Davis <jld@panix.com> | 2013-02-25 01:49:21 -0800 |
|---|---|---|
| committer | Jed Davis <jld@panix.com> | 2013-03-06 20:41:58 -0800 |
| commit | a9026c7f19d0418c8c0d4d401640bdd15b2e1d7e (patch) | |
| tree | bb71a6b04a6f1b0dde03fc6af6de6f1f538175af | |
| parent | a8237a46f134fa00d24ef1874abc0a4b45640cbf (diff) | |
| download | rust-a9026c7f19d0418c8c0d4d401640bdd15b2e1d7e.tar.gz rust-a9026c7f19d0418c8c0d4d401640bdd15b2e1d7e.zip | |
Memoize trans::adt::represent_type
| -rw-r--r-- | src/librustc/middle/trans/_match.rs | 22 | ||||
| -rw-r--r-- | src/librustc/middle/trans/adt.rs | 17 | ||||
| -rw-r--r-- | src/librustc/middle/trans/base.rs | 20 | ||||
| -rw-r--r-- | src/librustc/middle/trans/common.rs | 3 | ||||
| -rw-r--r-- | src/librustc/middle/trans/consts.rs | 16 | ||||
| -rw-r--r-- | src/librustc/middle/trans/datum.rs | 8 | ||||
| -rw-r--r-- | src/librustc/middle/trans/expr.rs | 10 | ||||
| -rw-r--r-- | src/librustc/middle/trans/glue.rs | 4 | ||||
| -rw-r--r-- | src/librustc/middle/trans/type_of.rs | 6 |
9 files changed, 58 insertions, 48 deletions
diff --git a/src/librustc/middle/trans/_match.rs b/src/librustc/middle/trans/_match.rs index 5a9d6c3b6c6..96afc83904d 100644 --- a/src/librustc/middle/trans/_match.rs +++ b/src/librustc/middle/trans/_match.rs @@ -192,7 +192,7 @@ pub enum Lit { // range) pub enum Opt { lit(Lit), - var(/* disr val */int, adt::Repr), + var(/* disr val */int, @adt::Repr), range(@ast::expr, @ast::expr), vec_len_eq(uint), vec_len_ge(uint) @@ -268,7 +268,7 @@ pub fn trans_opt(bcx: block, o: &Opt) -> opt_result { let llval = consts::get_const_val(bcx.ccx(), lit_id); return single_result(rslt(bcx, llval)); } - var(disr_val, ref repr) => { + var(disr_val, repr) => { return adt::trans_case(bcx, repr, disr_val); } range(l1, l2) => { @@ -1274,7 +1274,7 @@ pub fn compile_submatch(bcx: block, do expr::with_field_tys(tcx, pat_ty, None) |discr, field_tys| { let rec_vals = rec_fields.map(|field_name| { let ix = ty::field_idx_strict(tcx, *field_name, field_tys); - adt::trans_GEP(bcx, &pat_repr, val, discr, ix) + adt::trans_GEP(bcx, pat_repr, val, discr, ix) }); compile_submatch( bcx, @@ -1293,7 +1293,7 @@ pub fn compile_submatch(bcx: block, _ => ccx.sess.bug(~"non-tuple type in tuple pattern") }; let tup_vals = do vec::from_fn(n_tup_elts) |i| { - adt::trans_GEP(bcx, &tup_repr, val, 0, i) + adt::trans_GEP(bcx, tup_repr, val, 0, i) }; compile_submatch(bcx, enter_tup(bcx, dm, m, col, val, n_tup_elts), vec::append(tup_vals, vals_left), chk); @@ -1315,7 +1315,7 @@ pub fn compile_submatch(bcx: block, let struct_repr = adt::represent_type(bcx.ccx(), struct_ty); let llstructvals = do vec::from_fn(struct_element_count) |i| { - adt::trans_GEP(bcx, &struct_repr, val, 0, i) + adt::trans_GEP(bcx, struct_repr, val, 0, i) }; compile_submatch(bcx, enter_tuple_struct(bcx, dm, m, col, val, @@ -1359,7 +1359,7 @@ pub fn compile_submatch(bcx: block, let mut test_val = val; if opts.len() > 0u { match opts[0] { - var(_, ref repr) => { + var(_, repr) => { let (the_kind, val_opt) = adt::trans_switch(bcx, repr, val); kind = the_kind; for val_opt.each |&tval| { test_val = tval; } @@ -1511,7 +1511,7 @@ pub fn compile_submatch(bcx: block, let mut size = 0u; let mut unpacked = ~[]; match *opt { - var(disr_val, ref repr) => { + var(disr_val, repr) => { let ExtractedBlock {vals: argvals, bcx: new_bcx} = extract_variant_args(opt_cx, repr, disr_val, val); size = argvals.len(); @@ -1731,7 +1731,7 @@ pub fn bind_irrefutable_pat(bcx: block, enum_id, var_id); let args = extract_variant_args(bcx, - &repr, + repr, vinfo.disr_val, val); for sub_pats.each |sub_pat| { @@ -1753,7 +1753,7 @@ pub fn bind_irrefutable_pat(bcx: block, // This is the tuple struct case. let repr = adt::represent_node(bcx, pat.id); for vec::eachi(elems) |i, elem| { - let fldptr = adt::trans_GEP(bcx, &repr, + let fldptr = adt::trans_GEP(bcx, repr, val, 0, i); bcx = bind_irrefutable_pat(bcx, *elem, @@ -1776,7 +1776,7 @@ pub fn bind_irrefutable_pat(bcx: block, do expr::with_field_tys(tcx, pat_ty, None) |discr, field_tys| { for vec::each(fields) |f| { let ix = ty::field_idx_strict(tcx, f.ident, field_tys); - let fldptr = adt::trans_GEP(bcx, &pat_repr, val, + let fldptr = adt::trans_GEP(bcx, pat_repr, val, discr, ix); bcx = bind_irrefutable_pat(bcx, f.pat, @@ -1789,7 +1789,7 @@ pub fn bind_irrefutable_pat(bcx: block, ast::pat_tup(elems) => { let repr = adt::represent_node(bcx, pat.id); for vec::eachi(elems) |i, elem| { - let fldptr = adt::trans_GEP(bcx, &repr, val, 0, i); + let fldptr = adt::trans_GEP(bcx, repr, val, 0, i); bcx = bind_irrefutable_pat(bcx, *elem, fldptr, diff --git a/src/librustc/middle/trans/adt.rs b/src/librustc/middle/trans/adt.rs index 971a8db215a..00c620c7c8f 100644 --- a/src/librustc/middle/trans/adt.rs +++ b/src/librustc/middle/trans/adt.rs @@ -8,6 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +use core::container::Map; use core::libc::c_ulonglong; use core::option::{Option, Some, None}; use core::vec; @@ -43,15 +44,17 @@ struct Struct { } -pub fn represent_node(bcx: block, node: ast::node_id) - -> Repr { +pub fn represent_node(bcx: block, node: ast::node_id) -> @Repr { represent_type(bcx.ccx(), node_id_type(bcx, node)) } -pub fn represent_type(cx: @CrateContext, t: ty::t) -> Repr { +pub fn represent_type(cx: @CrateContext, t: ty::t) -> @Repr { debug!("Representing: %s", ty_to_str(cx.tcx, t)); - // XXX: cache this - match ty::get(t).sty { + match cx.adt_reprs.find(&t) { + Some(repr) => return *repr, + None => { } + } + let repr = @match ty::get(t).sty { ty::ty_tup(ref elems) => { Univariant(mk_struct(cx, *elems), NoDtor) } @@ -97,7 +100,9 @@ pub fn represent_type(cx: @CrateContext, t: ty::t) -> Repr { } } _ => cx.sess.bug(~"adt::represent_type called on non-ADT type") - } + }; + cx.adt_reprs.insert(t, repr); + return repr; } fn mk_struct(cx: @CrateContext, tys: &[ty::t]) -> Struct { diff --git a/src/librustc/middle/trans/base.rs b/src/librustc/middle/trans/base.rs index fb7d5bba1e5..ae1441731e0 100644 --- a/src/librustc/middle/trans/base.rs +++ b/src/librustc/middle/trans/base.rs @@ -67,6 +67,7 @@ use util::ppaux::{ty_to_str, ty_to_short_str}; use util::ppaux; use core::hash; +use core::hashmap::linear::LinearMap; use core::int; use core::io; use core::libc::{c_uint, c_ulonglong}; @@ -641,7 +642,7 @@ pub fn iter_structural_ty(cx: block, av: ValueRef, t: ty::t, let repr = adt::represent_type(cx.ccx(), t); do expr::with_field_tys(cx.tcx(), t, None) |discr, field_tys| { for vec::eachi(field_tys) |i, field_ty| { - let llfld_a = adt::trans_GEP(cx, &repr, av, discr, i); + let llfld_a = adt::trans_GEP(cx, repr, av, discr, i); cx = f(cx, llfld_a, field_ty.mt.ty); } } @@ -654,7 +655,7 @@ pub fn iter_structural_ty(cx: block, av: ValueRef, t: ty::t, ty::ty_tup(args) => { let repr = adt::represent_type(cx.ccx(), t); for vec::eachi(args) |i, arg| { - let llfld_a = adt::trans_GEP(cx, &repr, av, 0, i); + let llfld_a = adt::trans_GEP(cx, repr, av, 0, i); cx = f(cx, llfld_a, *arg); } } @@ -668,9 +669,9 @@ pub fn iter_structural_ty(cx: block, av: ValueRef, t: ty::t, // NB: we must hit the discriminant first so that structural // comparison know not to proceed when the discriminants differ. - match adt::trans_switch(cx, &repr, av) { + match adt::trans_switch(cx, repr, av) { (_match::single, None) => { - cx = iter_variant(cx, &repr, av, variants[0], + cx = iter_variant(cx, repr, av, variants[0], substs.tps, f); } (_match::switch, Some(lldiscrim_a)) => { @@ -686,9 +687,9 @@ pub fn iter_structural_ty(cx: block, av: ValueRef, t: ty::t, sub_block(cx, ~"enum-iter-variant-" + int::to_str(variant.disr_val)); let variant_cx = - iter_variant(variant_cx, &repr, av, *variant, + iter_variant(variant_cx, repr, av, *variant, substs.tps, f); - match adt::trans_case(cx, &repr, variant.disr_val) { + match adt::trans_case(cx, repr, variant.disr_val) { _match::single_result(r) => { AddCase(llswitch, r.val, variant_cx.llbb) } @@ -1863,9 +1864,9 @@ pub fn trans_enum_variant(ccx: @CrateContext, ty::node_id_to_type(ccx.tcx, enum_id)); let repr = adt::represent_type(ccx, enum_ty); - adt::trans_set_discr(bcx, &repr, fcx.llretptr, disr); + adt::trans_set_discr(bcx, repr, fcx.llretptr, disr); for vec::eachi(args) |i, va| { - let lldestptr = adt::trans_GEP(bcx, &repr, fcx.llretptr, disr, i); + let lldestptr = adt::trans_GEP(bcx, repr, fcx.llretptr, disr, i); // If this argument to this function is a enum, it'll have come in to // this function as an opaque blob due to the way that type_of() @@ -1935,7 +1936,7 @@ pub fn trans_tuple_struct(ccx: @CrateContext, let repr = adt::represent_type(ccx, tup_ty); for fields.eachi |i, field| { - let lldestptr = adt::trans_GEP(bcx, &repr, fcx.llretptr, 0, i); + let lldestptr = adt::trans_GEP(bcx, repr, fcx.llretptr, 0, i); let llarg = match fcx.llargs.get(&field.node.id) { local_mem(x) => x, _ => { @@ -3050,6 +3051,7 @@ pub fn trans_crate(sess: session::Session, module_data: HashMap(), lltypes: ty::new_ty_hash(), llsizingtypes: ty::new_ty_hash(), + adt_reprs: @mut LinearMap::new(), names: new_namegen(sess.parse_sess.interner), next_addrspace: new_addrspace_gen(), symbol_hasher: symbol_hasher, diff --git a/src/librustc/middle/trans/common.rs b/src/librustc/middle/trans/common.rs index 109a4985d3c..a363a950f9b 100644 --- a/src/librustc/middle/trans/common.rs +++ b/src/librustc/middle/trans/common.rs @@ -26,6 +26,7 @@ use lib; use metadata::common::LinkMeta; use middle::astencode; use middle::resolve; +use middle::trans::adt; use middle::trans::base; use middle::trans::build; use middle::trans::callee; @@ -44,6 +45,7 @@ use util::ppaux::{expr_repr, ty_to_str}; use core::cast; use core::hash; +use core::hashmap::linear::LinearMap; use core::libc::{c_uint, c_longlong, c_ulonglong}; use core::ptr; use core::str; @@ -203,6 +205,7 @@ pub struct CrateContext { module_data: HashMap<~str, ValueRef>, lltypes: HashMap<ty::t, TypeRef>, llsizingtypes: HashMap<ty::t, TypeRef>, + adt_reprs: @mut LinearMap<ty::t, @adt::Repr>, names: namegen, next_addrspace: addrspace_gen, symbol_hasher: @hash::State, diff --git a/src/librustc/middle/trans/consts.rs b/src/librustc/middle/trans/consts.rs index c42db4a90a1..cc5236609ab 100644 --- a/src/librustc/middle/trans/consts.rs +++ b/src/librustc/middle/trans/consts.rs @@ -245,7 +245,7 @@ fn const_expr_unchecked(cx: @CrateContext, e: @ast::expr) -> ValueRef { let (bt, bv) = const_autoderef(cx, bt, bv); do expr::with_field_tys(cx.tcx, bt, None) |discr, field_tys| { let ix = ty::field_idx_strict(cx.tcx, field, field_tys); - adt::const_get_element(cx, &brepr, bv, discr, ix) + adt::const_get_element(cx, brepr, bv, discr, ix) } } @@ -326,7 +326,7 @@ fn const_expr_unchecked(cx: @CrateContext, e: @ast::expr) -> ValueRef { (expr::cast_enum, expr::cast_integral) | (expr::cast_enum, expr::cast_float) => { let repr = adt::represent_type(cx, basety); - let iv = C_int(cx, adt::const_get_discrim(cx, &repr, v)); + let iv = C_int(cx, adt::const_get_discrim(cx, repr, v)); let ety_cast = expr::cast_type_kind(ety); match ety_cast { expr::cast_integral => { @@ -356,12 +356,12 @@ fn const_expr_unchecked(cx: @CrateContext, e: @ast::expr) -> ValueRef { ast::expr_tup(es) => { let ety = ty::expr_ty(cx.tcx, e); let repr = adt::represent_type(cx, ety); - adt::trans_const(cx, &repr, 0, es.map(|e| const_expr(cx, *e))) + adt::trans_const(cx, repr, 0, es.map(|e| const_expr(cx, *e))) } ast::expr_rec(ref fs, None) => { let ety = ty::expr_ty(cx.tcx, e); let repr = adt::represent_type(cx, ety); - adt::trans_const(cx, &repr, 0, + adt::trans_const(cx, repr, 0, fs.map(|f| const_expr(cx, f.node.expr))) } ast::expr_struct(_, ref fs, None) => { @@ -378,7 +378,7 @@ fn const_expr_unchecked(cx: @CrateContext, e: @ast::expr) -> ValueRef { } } }); - adt::trans_const(cx, &repr, discr, cs) + adt::trans_const(cx, repr, discr, cs) } } ast::expr_vec(es, ast::m_imm) => { @@ -442,7 +442,7 @@ fn const_expr_unchecked(cx: @CrateContext, e: @ast::expr) -> ValueRef { let vinfo = ty::enum_variant_with_id(cx.tcx, enum_did, variant_did); - adt::trans_const(cx, &repr, vinfo.disr_val, []) + adt::trans_const(cx, repr, vinfo.disr_val, []) } Some(ast::def_struct(_)) => { let ety = ty::expr_ty(cx.tcx, e); @@ -460,7 +460,7 @@ fn const_expr_unchecked(cx: @CrateContext, e: @ast::expr) -> ValueRef { Some(ast::def_struct(_)) => { let ety = ty::expr_ty(cx.tcx, e); let repr = adt::represent_type(cx, ety); - adt::trans_const(cx, &repr, 0, + adt::trans_const(cx, repr, 0, args.map(|a| const_expr(cx, *a))) } Some(ast::def_variant(enum_did, variant_did)) => { @@ -469,7 +469,7 @@ fn const_expr_unchecked(cx: @CrateContext, e: @ast::expr) -> ValueRef { let vinfo = ty::enum_variant_with_id(cx.tcx, enum_did, variant_did); - adt::trans_const(cx, &repr, vinfo.disr_val, + adt::trans_const(cx, repr, vinfo.disr_val, args.map(|a| const_expr(cx, *a))) } _ => cx.sess.span_bug(e.span, ~"expected a struct or \ diff --git a/src/librustc/middle/trans/datum.rs b/src/librustc/middle/trans/datum.rs index 79fd1dde541..7590d009f94 100644 --- a/src/librustc/middle/trans/datum.rs +++ b/src/librustc/middle/trans/datum.rs @@ -679,7 +679,7 @@ pub impl Datum { } let repr = adt::represent_type(ccx, self.ty); - assert adt::is_newtypeish(&repr); + assert adt::is_newtypeish(repr); let ty = ty::subst(ccx.tcx, substs, variants[0].args[0]); return match self.mode { ByRef => { @@ -687,7 +687,7 @@ pub impl Datum { // rather than a ptr to the enum type. ( Some(Datum { - val: adt::trans_GEP(bcx, &repr, self.val, + val: adt::trans_GEP(bcx, repr, self.val, 0, 0), ty: ty, mode: ByRef, @@ -719,7 +719,7 @@ pub impl Datum { } let repr = adt::represent_type(ccx, self.ty); - assert adt::is_newtypeish(&repr); + assert adt::is_newtypeish(repr); let ty = fields[0].mt.ty; return match self.mode { ByRef => { @@ -729,7 +729,7 @@ pub impl Datum { // destructors. ( Some(Datum { - val: adt::trans_GEP(bcx, &repr, self.val, + val: adt::trans_GEP(bcx, repr, self.val, 0, 0), ty: ty, mode: ByRef, diff --git a/src/librustc/middle/trans/expr.rs b/src/librustc/middle/trans/expr.rs index 72926f91af5..3df34e53ae3 100644 --- a/src/librustc/middle/trans/expr.rs +++ b/src/librustc/middle/trans/expr.rs @@ -604,7 +604,7 @@ fn trans_rvalue_dps_unadjusted(bcx: block, expr: @ast::expr, } ast::expr_tup(ref args) => { let repr = adt::represent_type(bcx.ccx(), expr_ty(bcx, expr)); - return trans_adt(bcx, &repr, 0, args.mapi(|i, arg| (i, *arg)), + return trans_adt(bcx, repr, 0, args.mapi(|i, arg| (i, *arg)), None, dest); } ast::expr_lit(@codemap::spanned {node: ast::lit_str(s), _}) => { @@ -726,7 +726,7 @@ fn trans_def_dps_unadjusted(bcx: block, ref_expr: @ast::expr, // Nullary variant. let ty = expr_ty(bcx, ref_expr); let repr = adt::represent_type(ccx, ty); - adt::trans_set_discr(bcx, &repr, lldest, + adt::trans_set_discr(bcx, repr, lldest, variant_info.disr_val); return bcx; } @@ -891,7 +891,7 @@ fn trans_lvalue_unadjusted(bcx: block, expr: @ast::expr) -> DatumBlock { datum: do base_datum.get_element(bcx, field_tys[ix].mt.ty, ZeroMem) |srcval| { - adt::trans_GEP(bcx, &repr, srcval, discr, ix) + adt::trans_GEP(bcx, repr, srcval, discr, ix) }, bcx: bcx } @@ -1192,7 +1192,7 @@ fn trans_rec_or_struct(bcx: block, }; let repr = adt::represent_type(bcx.ccx(), ty); - trans_adt(bcx, &repr, discr, numbered_fields, optbase, dest) + trans_adt(bcx, repr, discr, numbered_fields, optbase, dest) } } @@ -1645,7 +1645,7 @@ fn trans_imm_cast(bcx: block, expr: @ast::expr, (cast_enum, cast_float) => { let bcx = bcx; let repr = adt::represent_type(ccx, t_in); - let lldiscrim_a = adt::trans_cast_to_int(bcx, &repr, llexpr); + let lldiscrim_a = adt::trans_cast_to_int(bcx, repr, llexpr); match k_out { cast_integral => int_cast(bcx, ll_t_out, val_ty(lldiscrim_a), diff --git a/src/librustc/middle/trans/glue.rs b/src/librustc/middle/trans/glue.rs index f2ad0465e5b..44f5e9df58b 100644 --- a/src/librustc/middle/trans/glue.rs +++ b/src/librustc/middle/trans/glue.rs @@ -469,7 +469,7 @@ pub fn trans_struct_drop(bcx: block, take_ref: bool) -> block { let repr = adt::represent_type(bcx.ccx(), t); - let drop_flag = adt::trans_drop_flag_ptr(bcx, &repr, v0); + let drop_flag = adt::trans_drop_flag_ptr(bcx, repr, v0); do with_cond(bcx, IsNotNull(bcx, Load(bcx, drop_flag))) |cx| { let mut bcx = cx; @@ -507,7 +507,7 @@ pub fn trans_struct_drop(bcx: block, ty::struct_mutable_fields(bcx.tcx(), class_did, substs); for vec::eachi(field_tys) |i, fld| { - let llfld_a = adt::trans_GEP(bcx, &repr, v0, 0, i); + let llfld_a = adt::trans_GEP(bcx, repr, v0, 0, i); bcx = drop_ty(bcx, llfld_a, fld.mt.ty); } diff --git a/src/librustc/middle/trans/type_of.rs b/src/librustc/middle/trans/type_of.rs index 1f14a24b0ae..06c183f20e4 100644 --- a/src/librustc/middle/trans/type_of.rs +++ b/src/librustc/middle/trans/type_of.rs @@ -147,7 +147,7 @@ pub fn sizing_type_of(cx: @CrateContext, t: ty::t) -> TypeRef { ty::ty_tup(*) | ty::ty_rec(*) | ty::ty_struct(*) | ty::ty_enum(*) => { let repr = adt::represent_type(cx, t); - T_struct(adt::sizing_fields_of(cx, &repr)) + T_struct(adt::sizing_fields_of(cx, repr)) } ty::ty_self | ty::ty_infer(*) | ty::ty_param(*) | ty::ty_err(*) => { @@ -244,7 +244,7 @@ pub fn type_of(cx: @CrateContext, t: ty::t) -> TypeRef { ty::ty_type => T_ptr(cx.tydesc_type), ty::ty_tup(*) | ty::ty_rec(*) => { let repr = adt::represent_type(cx, t); - T_struct(adt::fields_of(cx, &repr)) + T_struct(adt::fields_of(cx, repr)) } ty::ty_opaque_closure_ptr(_) => T_opaque_box_ptr(cx), ty::ty_struct(did, ref substs) => { @@ -269,7 +269,7 @@ pub fn type_of(cx: @CrateContext, t: ty::t) -> TypeRef { match ty::get(t).sty { ty::ty_enum(*) | ty::ty_struct(*) => { let repr = adt::represent_type(cx, t); - common::set_struct_body(llty, adt::fields_of(cx, &repr)); + common::set_struct_body(llty, adt::fields_of(cx, repr)); } _ => () } |
