From c1273daa3495295ada88558dadc57a64da92acfe Mon Sep 17 00:00:00 2001 From: jackbackrack Date: Tue, 28 Sep 2021 11:25:24 -0700 Subject: [PATCH 1/2] add tuple get optimization --- compiler/stz-el.stanza | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/compiler/stz-el.stanza b/compiler/stz-el.stanza index 88035b3cf..841851a54 100644 --- a/compiler/stz-el.stanza +++ b/compiler/stz-el.stanza @@ -2540,13 +2540,13 @@ defn box-unbox-fold (epackage:EPackage, gvt:VarTable) -> EPackage : ;Compute binding table. ;Every entry in the binding table, x => e, indicates that ;x is defined exactly once by the EObject or ENewObject instruction. - defn binding-table (e:EBody) -> IntTable : - val table = IntListTable() + defn binding-table (e:EBody) -> IntTable : + val table = IntListTable() val remove-set = IntSet() for i in ins(e) do : - match(i:EObject|ENewObject) : add(table, n(x(i)), i) + match(i:EObject|ENewObject|ETuple) : add(table, n(x(i)), i) else : do(add{remove-set, n(_)}, varlocs(i)) - to-inttable $ + to-inttable $ for entry in table seq? : if remove-set[key(entry)] : None() else if length(value(entry)) == 1 : One(key(entry) => head(value(entry))) @@ -2563,7 +2563,7 @@ defn box-unbox-fold (epackage:EPackage, gvt:VarTable) -> EPackage : ;field. Calls fail() if not a match. defn unbox-object-get (e:EObjectGet, vt:VarTable, - bindings:IntTable) -> [EVarLoc, EImm] : + bindings:IntTable) -> [EVarLoc, EImm] : val v = y(e) as? EVar val o = get?(bindings, n(v)) as? ENewObject val value = ys(o)[index(e)] @@ -2575,7 +2575,7 @@ defn box-unbox-fold (epackage:EPackage, gvt:VarTable) -> EPackage : ;Calls fail() if not a match. defn unbox-load (e:ELoad, vt:VarTable, - bindings:IntTable, ) -> [EVarLoc, EImm] : + bindings:IntTable, ) -> [EVarLoc, EImm] : val field = loc(e) as? EField val v = y(loc(field) as? EDeref) as? EVar val o = get?(bindings, n(v)) as? EObject @@ -2584,6 +2584,18 @@ defn box-unbox-fold (epackage:EPackage, gvt:VarTable) -> EPackage : fail() when not immutable?(vt,value) [x(e), value] + ;If the given ETupleGet expression corresponds to a retrieval + ;of a tuple element of a known tuple then return the destination and the field. + ;Calls fail() if not a match. + defn unbox-tuple-get (e:ETupleGet, + vt:VarTable, + bindings:IntTable, ) -> [EVarLoc, EImm] : + val v = y(e) as? EVar + val t = get?(bindings, n(v)) as? ETuple + val value = ys(t)[index(e)] + println("--- TUPLE GET OPT %_ OF %_ -> %_" % [e, t, value]) + [x(e), value] + ;Perform unboxing folds in the given body. ;Assumes that all nested bodies have already been folded. defn fold-in-body (e:EBody, vt:VarTable) -> EBody : @@ -2603,6 +2615,11 @@ defn box-unbox-fold (epackage:EPackage, gvt:VarTable) -> EPackage : val [x, v] = unbox-load(i, vt, bindings) EDef(x, v, false) else : i + (i:ETupleGet) : + attempt : + val [x, v] = unbox-tuple-get(i, vt, bindings) + EDef(x, v, false) + else : i (i) : i ;Attempt to fold all instructions. From 29d5d188db158b8c1e9e9dcb0842f5530b99848d Mon Sep 17 00:00:00 2001 From: jackbackrack Date: Tue, 28 Sep 2021 11:34:22 -0700 Subject: [PATCH 2/2] turn off debug printing --- compiler/stz-el.stanza | 1 - 1 file changed, 1 deletion(-) diff --git a/compiler/stz-el.stanza b/compiler/stz-el.stanza index 841851a54..dac48269b 100644 --- a/compiler/stz-el.stanza +++ b/compiler/stz-el.stanza @@ -2593,7 +2593,6 @@ defn box-unbox-fold (epackage:EPackage, gvt:VarTable) -> EPackage : val v = y(e) as? EVar val t = get?(bindings, n(v)) as? ETuple val value = ys(t)[index(e)] - println("--- TUPLE GET OPT %_ OF %_ -> %_" % [e, t, value]) [x(e), value] ;Perform unboxing folds in the given body.