Skip to content

Commit 5565f5f

Browse files
committed
Memoize trans::adt::represent_type
1 parent 1eeff65 commit 5565f5f

File tree

9 files changed

+56
-47
lines changed

9 files changed

+56
-47
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
do val_opt.iter |&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: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,17 @@ struct Struct {
4343
}
4444

4545

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

51-
pub fn represent_type(cx: @CrateContext, t: ty::t) -> Repr {
50+
pub fn represent_type(cx: @CrateContext, t: ty::t) -> @Repr {
5251
debug!("Representing: %s", ty_to_str(cx.tcx, t));
53-
// XXX: cache this
54-
match ty::get(t).sty {
52+
match cx.adt_reprs.find(&t) {
53+
Some(repr) => return *repr,
54+
None => { }
55+
}
56+
let repr = @match ty::get(t).sty {
5557
ty::ty_tup(ref elems) => {
5658
Univariant(mk_struct(cx, *elems), NoDtor)
5759
}
@@ -97,7 +99,9 @@ pub fn represent_type(cx: @CrateContext, t: ty::t) -> Repr {
9799
}
98100
}
99101
_ => cx.sess.bug(~"adt::represent_type called on non-ADT type")
100-
}
102+
};
103+
cx.adt_reprs.insert(t, repr);
104+
return repr;
101105
}
102106

103107
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
@@ -74,6 +74,7 @@ use util::ppaux;
7474

7575
use core::either;
7676
use core::hash;
77+
use core::hashmap::linear::LinearMap;
7778
use core::int;
7879
use core::io;
7980
use core::libc::{c_uint, c_ulonglong};
@@ -652,7 +653,7 @@ pub fn iter_structural_ty(cx: block, av: ValueRef, t: ty::t,
652653
let repr = adt::represent_type(cx.ccx(), t);
653654
do expr::with_field_tys(cx.tcx(), t, None) |discr, field_tys| {
654655
for vec::eachi(field_tys) |i, field_ty| {
655-
let llfld_a = adt::trans_GEP(cx, &repr, av, discr, i);
656+
let llfld_a = adt::trans_GEP(cx, repr, av, discr, i);
656657
cx = f(cx, llfld_a, field_ty.mt.ty);
657658
}
658659
}
@@ -665,7 +666,7 @@ pub fn iter_structural_ty(cx: block, av: ValueRef, t: ty::t,
665666
ty::ty_tup(args) => {
666667
let repr = adt::represent_type(cx.ccx(), t);
667668
for vec::eachi(args) |i, arg| {
668-
let llfld_a = adt::trans_GEP(cx, &repr, av, 0, i);
669+
let llfld_a = adt::trans_GEP(cx, repr, av, 0, i);
669670
cx = f(cx, llfld_a, *arg);
670671
}
671672
}
@@ -679,9 +680,9 @@ pub fn iter_structural_ty(cx: block, av: ValueRef, t: ty::t,
679680
// NB: we must hit the discriminant first so that structural
680681
// comparison know not to proceed when the discriminants differ.
681682

682-
match adt::trans_switch(cx, &repr, av) {
683+
match adt::trans_switch(cx, repr, av) {
683684
(_match::single, None) => {
684-
cx = iter_variant(cx, &repr, av, variants[0],
685+
cx = iter_variant(cx, repr, av, variants[0],
685686
substs.tps, f);
686687
}
687688
(_match::switch, Some(lldiscrim_a)) => {
@@ -697,9 +698,9 @@ pub fn iter_structural_ty(cx: block, av: ValueRef, t: ty::t,
697698
sub_block(cx, ~"enum-iter-variant-" +
698699
int::to_str(variant.disr_val));
699700
let variant_cx =
700-
iter_variant(variant_cx, &repr, av, *variant,
701+
iter_variant(variant_cx, repr, av, *variant,
701702
substs.tps, f);
702-
match adt::trans_case(cx, &repr, variant.disr_val) {
703+
match adt::trans_case(cx, repr, variant.disr_val) {
703704
_match::single_result(r) => {
704705
AddCase(llswitch, r.val, variant_cx.llbb)
705706
}
@@ -1870,9 +1871,9 @@ pub fn trans_enum_variant(ccx: @CrateContext,
18701871
ty::node_id_to_type(ccx.tcx, enum_id));
18711872
let repr = adt::represent_type(ccx, enum_ty);
18721873

1873-
adt::trans_set_discr(bcx, &repr, fcx.llretptr, disr);
1874+
adt::trans_set_discr(bcx, repr, fcx.llretptr, disr);
18741875
for vec::eachi(args) |i, va| {
1875-
let lldestptr = adt::trans_GEP(bcx, &repr, fcx.llretptr, disr, i);
1876+
let lldestptr = adt::trans_GEP(bcx, repr, fcx.llretptr, disr, i);
18761877

18771878
// If this argument to this function is a enum, it'll have come in to
18781879
// this function as an opaque blob due to the way that type_of()
@@ -1942,7 +1943,7 @@ pub fn trans_tuple_struct(ccx: @CrateContext,
19421943
let repr = adt::represent_type(ccx, tup_ty);
19431944
19441945
for fields.eachi |i, field| {
1945-
let lldestptr = adt::trans_GEP(bcx, &repr, fcx.llretptr, 0, i);
1946+
let lldestptr = adt::trans_GEP(bcx, repr, fcx.llretptr, 0, i);
19461947
let llarg = match fcx.llargs.get(&field.node.id) {
19471948
local_mem(x) => x,
19481949
_ => {
@@ -3054,6 +3055,7 @@ pub fn trans_crate(sess: session::Session,
30543055
module_data: HashMap(),
30553056
lltypes: ty::new_ty_hash(),
30563057
llsizingtypes: ty::new_ty_hash(),
3058+
adt_reprs: @mut LinearMap::new(),
30573059
names: new_namegen(sess.parse_sess.interner),
30583060
next_addrspace: new_addrspace_gen(),
30593061
symbol_hasher: symbol_hasher,

src/librustc/middle/trans/common.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use metadata::common::LinkMeta;
2727
use metadata::{csearch};
2828
use middle::astencode;
2929
use middle::resolve;
30+
use middle::trans::adt;
3031
use middle::trans::base;
3132
use middle::trans::build;
3233
use middle::trans::callee;
@@ -47,6 +48,7 @@ use util::ppaux::{expr_repr, ty_to_str};
4748
use core::cast;
4849
use core::cmp;
4950
use core::hash;
51+
use core::hashmap::linear::LinearMap;
5052
use core::libc::{c_uint, c_longlong, c_ulonglong};
5153
use core::ptr;
5254
use core::str;
@@ -206,6 +208,7 @@ pub struct CrateContext {
206208
module_data: HashMap<~str, ValueRef>,
207209
lltypes: HashMap<ty::t, TypeRef>,
208210
llsizingtypes: HashMap<ty::t, TypeRef>,
211+
adt_reprs: @mut LinearMap<ty::t, @adt::Repr>,
209212
names: namegen,
210213
next_addrspace: addrspace_gen,
211214
symbol_hasher: @hash::State,

src/librustc/middle/trans/consts.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ pub fn const_expr(cx: @CrateContext, e: @ast::expr) -> ValueRef {
226226
let (bt, bv) = const_autoderef(cx, bt, bv);
227227
do expr::with_field_tys(cx.tcx, bt, None) |discr, field_tys| {
228228
let ix = ty::field_idx_strict(cx.tcx, field, field_tys);
229-
adt::const_get_element(cx, &brepr, bv, discr, ix)
229+
adt::const_get_element(cx, brepr, bv, discr, ix)
230230
}
231231
}
232232

@@ -322,12 +322,12 @@ pub fn const_expr(cx: @CrateContext, e: @ast::expr) -> ValueRef {
322322
ast::expr_tup(es) => {
323323
let ety = ty::expr_ty(cx.tcx, e);
324324
let repr = adt::represent_type(cx, ety);
325-
adt::trans_const(cx, &repr, 0, es.map(|e| const_expr(cx, *e)))
325+
adt::trans_const(cx, repr, 0, es.map(|e| const_expr(cx, *e)))
326326
}
327327
ast::expr_rec(ref fs, None) => {
328328
let ety = ty::expr_ty(cx.tcx, e);
329329
let repr = adt::represent_type(cx, ety);
330-
adt::trans_const(cx, &repr, 0,
330+
adt::trans_const(cx, repr, 0,
331331
fs.map(|f| const_expr(cx, f.node.expr)))
332332
}
333333
ast::expr_struct(_, ref fs, None) => {
@@ -344,7 +344,7 @@ pub fn const_expr(cx: @CrateContext, e: @ast::expr) -> ValueRef {
344344
}
345345
}
346346
});
347-
adt::trans_const(cx, &repr, discr, cs)
347+
adt::trans_const(cx, repr, discr, cs)
348348
}
349349
}
350350
ast::expr_vec(es, ast::m_imm) => {
@@ -399,7 +399,7 @@ pub fn const_expr(cx: @CrateContext, e: @ast::expr) -> ValueRef {
399399
let vinfo = ty::enum_variant_with_id(cx.tcx,
400400
enum_did,
401401
variant_did);
402-
adt::trans_const(cx, &repr, vinfo.disr_val, [])
402+
adt::trans_const(cx, repr, vinfo.disr_val, [])
403403
}
404404
Some(ast::def_struct(_)) => {
405405
let ety = ty::expr_ty(cx.tcx, e);
@@ -417,7 +417,7 @@ pub fn const_expr(cx: @CrateContext, e: @ast::expr) -> ValueRef {
417417
Some(ast::def_struct(_)) => {
418418
let ety = ty::expr_ty(cx.tcx, e);
419419
let repr = adt::represent_type(cx, ety);
420-
adt::trans_const(cx, &repr, 0,
420+
adt::trans_const(cx, repr, 0,
421421
args.map(|a| const_expr(cx, *a)))
422422
}
423423
Some(ast::def_variant(enum_did, variant_did)) => {
@@ -426,7 +426,7 @@ pub fn const_expr(cx: @CrateContext, e: @ast::expr) -> ValueRef {
426426
let vinfo = ty::enum_variant_with_id(cx.tcx,
427427
enum_did,
428428
variant_did);
429-
adt::trans_const(cx, &repr, vinfo.disr_val,
429+
adt::trans_const(cx, repr, vinfo.disr_val,
430430
args.map(|a| const_expr(cx, *a)))
431431
}
432432
_ => 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
@@ -682,15 +682,15 @@ pub impl Datum {
682682
}
683683

684684
let repr = adt::represent_type(ccx, self.ty);
685-
assert adt::is_newtypeish(&repr);
685+
assert adt::is_newtypeish(repr);
686686
let ty = ty::subst(ccx.tcx, substs, variants[0].args[0]);
687687
return match self.mode {
688688
ByRef => {
689689
// Recast lv.val as a pointer to the newtype
690690
// rather than a ptr to the enum type.
691691
(
692692
Some(Datum {
693-
val: adt::trans_GEP(bcx, &repr, self.val,
693+
val: adt::trans_GEP(bcx, repr, self.val,
694694
0, 0),
695695
ty: ty,
696696
mode: ByRef,
@@ -722,7 +722,7 @@ pub impl Datum {
722722
}
723723

724724
let repr = adt::represent_type(ccx, self.ty);
725-
assert adt::is_newtypeish(&repr);
725+
assert adt::is_newtypeish(repr);
726726
let ty = fields[0].mt.ty;
727727
return match self.mode {
728728
ByRef => {
@@ -732,7 +732,7 @@ pub impl Datum {
732732
// destructors.
733733
(
734734
Some(Datum {
735-
val: adt::trans_GEP(bcx, &repr, self.val,
735+
val: adt::trans_GEP(bcx, repr, self.val,
736736
0, 0),
737737
ty: ty,
738738
mode: ByRef,

src/librustc/middle/trans/expr.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ fn trans_rvalue_dps_unadjusted(bcx: block, expr: @ast::expr,
576576
}
577577
ast::expr_tup(ref args) => {
578578
let repr = adt::represent_type(bcx.ccx(), expr_ty(bcx, expr));
579-
return trans_adt(bcx, &repr, 0, args.mapi(|i, arg| (i, *arg)),
579+
return trans_adt(bcx, repr, 0, args.mapi(|i, arg| (i, *arg)),
580580
None, dest);
581581
}
582582
ast::expr_lit(@codemap::spanned {node: ast::lit_str(s), _}) => {
@@ -708,7 +708,7 @@ fn trans_def_dps_unadjusted(bcx: block, ref_expr: @ast::expr,
708708
// Nullary variant.
709709
let ty = expr_ty(bcx, ref_expr);
710710
let repr = adt::represent_type(ccx, ty);
711-
adt::trans_set_discr(bcx, &repr, lldest,
711+
adt::trans_set_discr(bcx, repr, lldest,
712712
variant_info.disr_val);
713713
return bcx;
714714
}
@@ -813,7 +813,7 @@ fn trans_lvalue_unadjusted(bcx: block, expr: @ast::expr) -> DatumBlock {
813813
datum: do base_datum.get_element(bcx,
814814
field_tys[ix].mt.ty,
815815
ZeroMem) |srcval| {
816-
adt::trans_GEP(bcx, &repr, srcval, discr, ix)
816+
adt::trans_GEP(bcx, repr, srcval, discr, ix)
817817
},
818818
bcx: bcx
819819
}
@@ -1144,7 +1144,7 @@ fn trans_rec_or_struct(bcx: block,
11441144
};
11451145

11461146
let repr = adt::represent_type(bcx.ccx(), ty);
1147-
trans_adt(bcx, &repr, discr, numbered_fields, optbase, dest)
1147+
trans_adt(bcx, repr, discr, numbered_fields, optbase, dest)
11481148
}
11491149
}
11501150

@@ -1597,7 +1597,7 @@ fn trans_imm_cast(bcx: block, expr: @ast::expr,
15971597
(cast_enum, cast_float) => {
15981598
let bcx = bcx;
15991599
let repr = adt::represent_type(ccx, t_in);
1600-
let lldiscrim_a = adt::trans_cast_to_int(bcx, &repr, llexpr);
1600+
let lldiscrim_a = adt::trans_cast_to_int(bcx, repr, llexpr);
16011601
match k_out {
16021602
cast_integral => int_cast(bcx, ll_t_out,
16031603
val_ty(lldiscrim_a),

src/librustc/middle/trans/glue.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ pub fn trans_struct_drop(bcx: block,
469469
take_ref: bool)
470470
-> block {
471471
let repr = adt::represent_type(bcx.ccx(), t);
472-
let drop_flag = adt::trans_drop_flag_ptr(bcx, &repr, v0);
472+
let drop_flag = adt::trans_drop_flag_ptr(bcx, repr, v0);
473473
do with_cond(bcx, IsNotNull(bcx, Load(bcx, drop_flag))) |cx| {
474474
let mut bcx = cx;
475475

@@ -507,7 +507,7 @@ pub fn trans_struct_drop(bcx: block,
507507
ty::struct_mutable_fields(bcx.tcx(), class_did,
508508
substs);
509509
for vec::eachi(field_tys) |i, fld| {
510-
let llfld_a = adt::trans_GEP(bcx, &repr, v0, 0, i);
510+
let llfld_a = adt::trans_GEP(bcx, repr, v0, 0, i);
511511
bcx = drop_ty(bcx, llfld_a, fld.mt.ty);
512512
}
513513

0 commit comments

Comments
 (0)