Skip to content

Commit a9026c7

Browse files
committed
Memoize trans::adt::represent_type
1 parent a8237a4 commit a9026c7

File tree

9 files changed

+58
-48
lines changed

9 files changed

+58
-48
lines changed

src/librustc/middle/trans/_match.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ pub enum Lit {
192192
// range)
193193
pub enum Opt {
194194
lit(Lit),
195-
var(/* disr val */int, adt::Repr),
195+
var(/* disr val */int, @adt::Repr),
196196
range(@ast::expr, @ast::expr),
197197
vec_len_eq(uint),
198198
vec_len_ge(uint)
@@ -268,7 +268,7 @@ pub fn trans_opt(bcx: block, o: &Opt) -> opt_result {
268268
let llval = consts::get_const_val(bcx.ccx(), lit_id);
269269
return single_result(rslt(bcx, llval));
270270
}
271-
var(disr_val, ref repr) => {
271+
var(disr_val, repr) => {
272272
return adt::trans_case(bcx, repr, disr_val);
273273
}
274274
range(l1, l2) => {
@@ -1274,7 +1274,7 @@ pub fn compile_submatch(bcx: block,
12741274
do expr::with_field_tys(tcx, pat_ty, None) |discr, field_tys| {
12751275
let rec_vals = rec_fields.map(|field_name| {
12761276
let ix = ty::field_idx_strict(tcx, *field_name, field_tys);
1277-
adt::trans_GEP(bcx, &pat_repr, val, discr, ix)
1277+
adt::trans_GEP(bcx, pat_repr, val, discr, ix)
12781278
});
12791279
compile_submatch(
12801280
bcx,
@@ -1293,7 +1293,7 @@ pub fn compile_submatch(bcx: block,
12931293
_ => ccx.sess.bug(~"non-tuple type in tuple pattern")
12941294
};
12951295
let tup_vals = do vec::from_fn(n_tup_elts) |i| {
1296-
adt::trans_GEP(bcx, &tup_repr, val, 0, i)
1296+
adt::trans_GEP(bcx, tup_repr, val, 0, i)
12971297
};
12981298
compile_submatch(bcx, enter_tup(bcx, dm, m, col, val, n_tup_elts),
12991299
vec::append(tup_vals, vals_left), chk);
@@ -1315,7 +1315,7 @@ pub fn compile_submatch(bcx: block,
13151315
13161316
let struct_repr = adt::represent_type(bcx.ccx(), struct_ty);
13171317
let llstructvals = do vec::from_fn(struct_element_count) |i| {
1318-
adt::trans_GEP(bcx, &struct_repr, val, 0, i)
1318+
adt::trans_GEP(bcx, struct_repr, val, 0, i)
13191319
};
13201320
compile_submatch(bcx,
13211321
enter_tuple_struct(bcx, dm, m, col, val,
@@ -1359,7 +1359,7 @@ pub fn compile_submatch(bcx: block,
13591359
let mut test_val = val;
13601360
if opts.len() > 0u {
13611361
match opts[0] {
1362-
var(_, ref repr) => {
1362+
var(_, repr) => {
13631363
let (the_kind, val_opt) = adt::trans_switch(bcx, repr, val);
13641364
kind = the_kind;
13651365
for val_opt.each |&tval| { test_val = tval; }
@@ -1511,7 +1511,7 @@ pub fn compile_submatch(bcx: block,
15111511
let mut size = 0u;
15121512
let mut unpacked = ~[];
15131513
match *opt {
1514-
var(disr_val, ref repr) => {
1514+
var(disr_val, repr) => {
15151515
let ExtractedBlock {vals: argvals, bcx: new_bcx} =
15161516
extract_variant_args(opt_cx, repr, disr_val, val);
15171517
size = argvals.len();
@@ -1731,7 +1731,7 @@ pub fn bind_irrefutable_pat(bcx: block,
17311731
enum_id,
17321732
var_id);
17331733
let args = extract_variant_args(bcx,
1734-
&repr,
1734+
repr,
17351735
vinfo.disr_val,
17361736
val);
17371737
for sub_pats.each |sub_pat| {
@@ -1753,7 +1753,7 @@ pub fn bind_irrefutable_pat(bcx: block,
17531753
// This is the tuple struct case.
17541754
let repr = adt::represent_node(bcx, pat.id);
17551755
for vec::eachi(elems) |i, elem| {
1756-
let fldptr = adt::trans_GEP(bcx, &repr,
1756+
let fldptr = adt::trans_GEP(bcx, repr,
17571757
val, 0, i);
17581758
bcx = bind_irrefutable_pat(bcx,
17591759
*elem,
@@ -1776,7 +1776,7 @@ pub fn bind_irrefutable_pat(bcx: block,
17761776
do expr::with_field_tys(tcx, pat_ty, None) |discr, field_tys| {
17771777
for vec::each(fields) |f| {
17781778
let ix = ty::field_idx_strict(tcx, f.ident, field_tys);
1779-
let fldptr = adt::trans_GEP(bcx, &pat_repr, val,
1779+
let fldptr = adt::trans_GEP(bcx, pat_repr, val,
17801780
discr, ix);
17811781
bcx = bind_irrefutable_pat(bcx,
17821782
f.pat,
@@ -1789,7 +1789,7 @@ pub fn bind_irrefutable_pat(bcx: block,
17891789
ast::pat_tup(elems) => {
17901790
let repr = adt::represent_node(bcx, pat.id);
17911791
for vec::eachi(elems) |i, elem| {
1792-
let fldptr = adt::trans_GEP(bcx, &repr, val, 0, i);
1792+
let fldptr = adt::trans_GEP(bcx, repr, val, 0, i);
17931793
bcx = bind_irrefutable_pat(bcx,
17941794
*elem,
17951795
fldptr,

src/librustc/middle/trans/adt.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
use core::container::Map;
1112
use core::libc::c_ulonglong;
1213
use core::option::{Option, Some, None};
1314
use core::vec;
@@ -43,15 +44,17 @@ struct Struct {
4344
}
4445

4546

46-
pub fn represent_node(bcx: block, node: ast::node_id)
47-
-> Repr {
47+
pub fn represent_node(bcx: block, node: ast::node_id) -> @Repr {
4848
represent_type(bcx.ccx(), node_id_type(bcx, node))
4949
}
5050

51-
pub fn represent_type(cx: @CrateContext, t: ty::t) -> Repr {
51+
pub fn represent_type(cx: @CrateContext, t: ty::t) -> @Repr {
5252
debug!("Representing: %s", ty_to_str(cx.tcx, t));
53-
// XXX: cache this
54-
match ty::get(t).sty {
53+
match cx.adt_reprs.find(&t) {
54+
Some(repr) => return *repr,
55+
None => { }
56+
}
57+
let repr = @match ty::get(t).sty {
5558
ty::ty_tup(ref elems) => {
5659
Univariant(mk_struct(cx, *elems), NoDtor)
5760
}
@@ -97,7 +100,9 @@ pub fn represent_type(cx: @CrateContext, t: ty::t) -> Repr {
97100
}
98101
}
99102
_ => cx.sess.bug(~"adt::represent_type called on non-ADT type")
100-
}
103+
};
104+
cx.adt_reprs.insert(t, repr);
105+
return repr;
101106
}
102107

103108
fn mk_struct(cx: @CrateContext, tys: &[ty::t]) -> Struct {

src/librustc/middle/trans/base.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ use util::ppaux::{ty_to_str, ty_to_short_str};
6767
use util::ppaux;
6868

6969
use core::hash;
70+
use core::hashmap::linear::LinearMap;
7071
use core::int;
7172
use core::io;
7273
use core::libc::{c_uint, c_ulonglong};
@@ -641,7 +642,7 @@ pub fn iter_structural_ty(cx: block, av: ValueRef, t: ty::t,
641642
let repr = adt::represent_type(cx.ccx(), t);
642643
do expr::with_field_tys(cx.tcx(), t, None) |discr, field_tys| {
643644
for vec::eachi(field_tys) |i, field_ty| {
644-
let llfld_a = adt::trans_GEP(cx, &repr, av, discr, i);
645+
let llfld_a = adt::trans_GEP(cx, repr, av, discr, i);
645646
cx = f(cx, llfld_a, field_ty.mt.ty);
646647
}
647648
}
@@ -654,7 +655,7 @@ pub fn iter_structural_ty(cx: block, av: ValueRef, t: ty::t,
654655
ty::ty_tup(args) => {
655656
let repr = adt::represent_type(cx.ccx(), t);
656657
for vec::eachi(args) |i, arg| {
657-
let llfld_a = adt::trans_GEP(cx, &repr, av, 0, i);
658+
let llfld_a = adt::trans_GEP(cx, repr, av, 0, i);
658659
cx = f(cx, llfld_a, *arg);
659660
}
660661
}
@@ -668,9 +669,9 @@ pub fn iter_structural_ty(cx: block, av: ValueRef, t: ty::t,
668669
// NB: we must hit the discriminant first so that structural
669670
// comparison know not to proceed when the discriminants differ.
670671

671-
match adt::trans_switch(cx, &repr, av) {
672+
match adt::trans_switch(cx, repr, av) {
672673
(_match::single, None) => {
673-
cx = iter_variant(cx, &repr, av, variants[0],
674+
cx = iter_variant(cx, repr, av, variants[0],
674675
substs.tps, f);
675676
}
676677
(_match::switch, Some(lldiscrim_a)) => {
@@ -686,9 +687,9 @@ pub fn iter_structural_ty(cx: block, av: ValueRef, t: ty::t,
686687
sub_block(cx, ~"enum-iter-variant-" +
687688
int::to_str(variant.disr_val));
688689
let variant_cx =
689-
iter_variant(variant_cx, &repr, av, *variant,
690+
iter_variant(variant_cx, repr, av, *variant,
690691
substs.tps, f);
691-
match adt::trans_case(cx, &repr, variant.disr_val) {
692+
match adt::trans_case(cx, repr, variant.disr_val) {
692693
_match::single_result(r) => {
693694
AddCase(llswitch, r.val, variant_cx.llbb)
694695
}
@@ -1863,9 +1864,9 @@ pub fn trans_enum_variant(ccx: @CrateContext,
18631864
ty::node_id_to_type(ccx.tcx, enum_id));
18641865
let repr = adt::represent_type(ccx, enum_ty);
18651866

1866-
adt::trans_set_discr(bcx, &repr, fcx.llretptr, disr);
1867+
adt::trans_set_discr(bcx, repr, fcx.llretptr, disr);
18671868
for vec::eachi(args) |i, va| {
1868-
let lldestptr = adt::trans_GEP(bcx, &repr, fcx.llretptr, disr, i);
1869+
let lldestptr = adt::trans_GEP(bcx, repr, fcx.llretptr, disr, i);
18691870

18701871
// If this argument to this function is a enum, it'll have come in to
18711872
// this function as an opaque blob due to the way that type_of()
@@ -1935,7 +1936,7 @@ pub fn trans_tuple_struct(ccx: @CrateContext,
19351936
let repr = adt::represent_type(ccx, tup_ty);
19361937
19371938
for fields.eachi |i, field| {
1938-
let lldestptr = adt::trans_GEP(bcx, &repr, fcx.llretptr, 0, i);
1939+
let lldestptr = adt::trans_GEP(bcx, repr, fcx.llretptr, 0, i);
19391940
let llarg = match fcx.llargs.get(&field.node.id) {
19401941
local_mem(x) => x,
19411942
_ => {
@@ -3050,6 +3051,7 @@ pub fn trans_crate(sess: session::Session,
30503051
module_data: HashMap(),
30513052
lltypes: ty::new_ty_hash(),
30523053
llsizingtypes: ty::new_ty_hash(),
3054+
adt_reprs: @mut LinearMap::new(),
30533055
names: new_namegen(sess.parse_sess.interner),
30543056
next_addrspace: new_addrspace_gen(),
30553057
symbol_hasher: symbol_hasher,

src/librustc/middle/trans/common.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use lib;
2626
use metadata::common::LinkMeta;
2727
use middle::astencode;
2828
use middle::resolve;
29+
use middle::trans::adt;
2930
use middle::trans::base;
3031
use middle::trans::build;
3132
use middle::trans::callee;
@@ -44,6 +45,7 @@ use util::ppaux::{expr_repr, ty_to_str};
4445

4546
use core::cast;
4647
use core::hash;
48+
use core::hashmap::linear::LinearMap;
4749
use core::libc::{c_uint, c_longlong, c_ulonglong};
4850
use core::ptr;
4951
use core::str;
@@ -203,6 +205,7 @@ pub struct CrateContext {
203205
module_data: HashMap<~str, ValueRef>,
204206
lltypes: HashMap<ty::t, TypeRef>,
205207
llsizingtypes: HashMap<ty::t, TypeRef>,
208+
adt_reprs: @mut LinearMap<ty::t, @adt::Repr>,
206209
names: namegen,
207210
next_addrspace: addrspace_gen,
208211
symbol_hasher: @hash::State,

src/librustc/middle/trans/consts.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ fn const_expr_unchecked(cx: @CrateContext, e: @ast::expr) -> ValueRef {
245245
let (bt, bv) = const_autoderef(cx, bt, bv);
246246
do expr::with_field_tys(cx.tcx, bt, None) |discr, field_tys| {
247247
let ix = ty::field_idx_strict(cx.tcx, field, field_tys);
248-
adt::const_get_element(cx, &brepr, bv, discr, ix)
248+
adt::const_get_element(cx, brepr, bv, discr, ix)
249249
}
250250
}
251251

@@ -326,7 +326,7 @@ fn const_expr_unchecked(cx: @CrateContext, e: @ast::expr) -> ValueRef {
326326
(expr::cast_enum, expr::cast_integral) |
327327
(expr::cast_enum, expr::cast_float) => {
328328
let repr = adt::represent_type(cx, basety);
329-
let iv = C_int(cx, adt::const_get_discrim(cx, &repr, v));
329+
let iv = C_int(cx, adt::const_get_discrim(cx, repr, v));
330330
let ety_cast = expr::cast_type_kind(ety);
331331
match ety_cast {
332332
expr::cast_integral => {
@@ -356,12 +356,12 @@ fn const_expr_unchecked(cx: @CrateContext, e: @ast::expr) -> ValueRef {
356356
ast::expr_tup(es) => {
357357
let ety = ty::expr_ty(cx.tcx, e);
358358
let repr = adt::represent_type(cx, ety);
359-
adt::trans_const(cx, &repr, 0, es.map(|e| const_expr(cx, *e)))
359+
adt::trans_const(cx, repr, 0, es.map(|e| const_expr(cx, *e)))
360360
}
361361
ast::expr_rec(ref fs, None) => {
362362
let ety = ty::expr_ty(cx.tcx, e);
363363
let repr = adt::represent_type(cx, ety);
364-
adt::trans_const(cx, &repr, 0,
364+
adt::trans_const(cx, repr, 0,
365365
fs.map(|f| const_expr(cx, f.node.expr)))
366366
}
367367
ast::expr_struct(_, ref fs, None) => {
@@ -378,7 +378,7 @@ fn const_expr_unchecked(cx: @CrateContext, e: @ast::expr) -> ValueRef {
378378
}
379379
}
380380
});
381-
adt::trans_const(cx, &repr, discr, cs)
381+
adt::trans_const(cx, repr, discr, cs)
382382
}
383383
}
384384
ast::expr_vec(es, ast::m_imm) => {
@@ -442,7 +442,7 @@ fn const_expr_unchecked(cx: @CrateContext, e: @ast::expr) -> ValueRef {
442442
let vinfo = ty::enum_variant_with_id(cx.tcx,
443443
enum_did,
444444
variant_did);
445-
adt::trans_const(cx, &repr, vinfo.disr_val, [])
445+
adt::trans_const(cx, repr, vinfo.disr_val, [])
446446
}
447447
Some(ast::def_struct(_)) => {
448448
let ety = ty::expr_ty(cx.tcx, e);
@@ -460,7 +460,7 @@ fn const_expr_unchecked(cx: @CrateContext, e: @ast::expr) -> ValueRef {
460460
Some(ast::def_struct(_)) => {
461461
let ety = ty::expr_ty(cx.tcx, e);
462462
let repr = adt::represent_type(cx, ety);
463-
adt::trans_const(cx, &repr, 0,
463+
adt::trans_const(cx, repr, 0,
464464
args.map(|a| const_expr(cx, *a)))
465465
}
466466
Some(ast::def_variant(enum_did, variant_did)) => {
@@ -469,7 +469,7 @@ fn const_expr_unchecked(cx: @CrateContext, e: @ast::expr) -> ValueRef {
469469
let vinfo = ty::enum_variant_with_id(cx.tcx,
470470
enum_did,
471471
variant_did);
472-
adt::trans_const(cx, &repr, vinfo.disr_val,
472+
adt::trans_const(cx, repr, vinfo.disr_val,
473473
args.map(|a| const_expr(cx, *a)))
474474
}
475475
_ => cx.sess.span_bug(e.span, ~"expected a struct or \

src/librustc/middle/trans/datum.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -679,15 +679,15 @@ pub impl Datum {
679679
}
680680

681681
let repr = adt::represent_type(ccx, self.ty);
682-
assert adt::is_newtypeish(&repr);
682+
assert adt::is_newtypeish(repr);
683683
let ty = ty::subst(ccx.tcx, substs, variants[0].args[0]);
684684
return match self.mode {
685685
ByRef => {
686686
// Recast lv.val as a pointer to the newtype
687687
// rather than a ptr to the enum type.
688688
(
689689
Some(Datum {
690-
val: adt::trans_GEP(bcx, &repr, self.val,
690+
val: adt::trans_GEP(bcx, repr, self.val,
691691
0, 0),
692692
ty: ty,
693693
mode: ByRef,
@@ -719,7 +719,7 @@ pub impl Datum {
719719
}
720720

721721
let repr = adt::represent_type(ccx, self.ty);
722-
assert adt::is_newtypeish(&repr);
722+
assert adt::is_newtypeish(repr);
723723
let ty = fields[0].mt.ty;
724724
return match self.mode {
725725
ByRef => {
@@ -729,7 +729,7 @@ pub impl Datum {
729729
// destructors.
730730
(
731731
Some(Datum {
732-
val: adt::trans_GEP(bcx, &repr, self.val,
732+
val: adt::trans_GEP(bcx, repr, self.val,
733733
0, 0),
734734
ty: ty,
735735
mode: ByRef,

src/librustc/middle/trans/expr.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ fn trans_rvalue_dps_unadjusted(bcx: block, expr: @ast::expr,
604604
}
605605
ast::expr_tup(ref args) => {
606606
let repr = adt::represent_type(bcx.ccx(), expr_ty(bcx, expr));
607-
return trans_adt(bcx, &repr, 0, args.mapi(|i, arg| (i, *arg)),
607+
return trans_adt(bcx, repr, 0, args.mapi(|i, arg| (i, *arg)),
608608
None, dest);
609609
}
610610
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,
726726
// Nullary variant.
727727
let ty = expr_ty(bcx, ref_expr);
728728
let repr = adt::represent_type(ccx, ty);
729-
adt::trans_set_discr(bcx, &repr, lldest,
729+
adt::trans_set_discr(bcx, repr, lldest,
730730
variant_info.disr_val);
731731
return bcx;
732732
}
@@ -891,7 +891,7 @@ fn trans_lvalue_unadjusted(bcx: block, expr: @ast::expr) -> DatumBlock {
891891
datum: do base_datum.get_element(bcx,
892892
field_tys[ix].mt.ty,
893893
ZeroMem) |srcval| {
894-
adt::trans_GEP(bcx, &repr, srcval, discr, ix)
894+
adt::trans_GEP(bcx, repr, srcval, discr, ix)
895895
},
896896
bcx: bcx
897897
}
@@ -1192,7 +1192,7 @@ fn trans_rec_or_struct(bcx: block,
11921192
};
11931193

11941194
let repr = adt::represent_type(bcx.ccx(), ty);
1195-
trans_adt(bcx, &repr, discr, numbered_fields, optbase, dest)
1195+
trans_adt(bcx, repr, discr, numbered_fields, optbase, dest)
11961196
}
11971197
}
11981198

@@ -1645,7 +1645,7 @@ fn trans_imm_cast(bcx: block, expr: @ast::expr,
16451645
(cast_enum, cast_float) => {
16461646
let bcx = bcx;
16471647
let repr = adt::represent_type(ccx, t_in);
1648-
let lldiscrim_a = adt::trans_cast_to_int(bcx, &repr, llexpr);
1648+
let lldiscrim_a = adt::trans_cast_to_int(bcx, repr, llexpr);
16491649
match k_out {
16501650
cast_integral => int_cast(bcx, ll_t_out,
16511651
val_ty(lldiscrim_a),

0 commit comments

Comments
 (0)