move analyser is smarter; refs #11053

This commit is contained in:
Andreas Rumpf 2019-04-18 20:55:59 +02:00
commit a394d31746
2 changed files with 17 additions and 9 deletions

View file

@ -634,11 +634,14 @@ proc isAnalysableFieldAccess*(n: PNode; owner: PSym): bool =
# XXX Allow closure deref operations here if we know # XXX Allow closure deref operations here if we know
# the owner controlled the closure allocation? # the owner controlled the closure allocation?
result = n.kind == nkSym and n.sym.owner == owner and result = n.kind == nkSym and n.sym.owner == owner and
owner.kind != skModule and (n.sym.kind != skParam or isSinkParam(n.sym)) owner.kind != skModule and
(n.sym.kind != skParam or isSinkParam(n.sym)) # or n.sym.typ.kind == tyVar)
proc genDef(c: var Con; n: PNode) = proc genDef(c: var Con; n: PNode) =
if n.kind == nkSym and n.sym.kind in InterestingSyms: if n.kind == nkSym and n.sym.kind in InterestingSyms:
c.code.add Instr(n: n, kind: def, sym: n.sym) c.code.add Instr(n: n, kind: def, sym: n.sym)
elif isAnalysableFieldAccess(n, c.owner):
c.code.add Instr(n: n, kind: def, sym: nil)
proc canRaise(fn: PNode): bool = proc canRaise(fn: PNode): bool =
const magicsThatCanRaise = { const magicsThatCanRaise = {

View file

@ -317,13 +317,12 @@ proc makePtrType(c: Con, baseType: PType): PType =
proc genOp(c: Con; t: PType; kind: TTypeAttachedOp; dest, ri: PNode): PNode = proc genOp(c: Con; t: PType; kind: TTypeAttachedOp; dest, ri: PNode): PNode =
var op = t.attachedOps[kind] var op = t.attachedOps[kind]
when false: if op == nil:
if op == nil: # give up and find the canonical type instead:
# give up and find the canonical type instead: let h = sighashes.hashType(t, {CoType, CoConsiderOwned})
let h = sighashes.hashType(t, {CoType, CoConsiderOwned}) let canon = c.graph.canonTypes.getOrDefault(h)
let canon = c.graph.canonTypes.getOrDefault(h) if canon != nil:
if canon != nil: op = canon.attachedOps[kind]
op = canon.attachedOps[kind]
if op == nil: if op == nil:
globalError(c.graph.config, dest.info, "internal error: '" & AttachedOpToStr[kind] & globalError(c.graph.config, dest.info, "internal error: '" & AttachedOpToStr[kind] &
@ -535,9 +534,15 @@ proc moveOrCopy(dest, ri: PNode; c: var Con): PNode =
if ri[0].kind == nkSym and isUnpackedTuple(ri[0].sym): if ri[0].kind == nkSym and isUnpackedTuple(ri[0].sym):
# unpacking of tuple: move out the elements # unpacking of tuple: move out the elements
result = genSink(c, dest.typ, dest, ri) result = genSink(c, dest.typ, dest, ri)
result.add p(ri, c)
elif isAnalysableFieldAccess(ri, c.owner) and isLastRead(ri, c):
# Rule 3: `=sink`(x, z); wasMoved(z)
var snk = genSink(c, dest.typ, dest, ri)
snk.add ri
result = newTree(nkStmtList, snk, genWasMoved(ri, c))
else: else:
result = genCopy(c, dest.typ, dest, ri) result = genCopy(c, dest.typ, dest, ri)
result.add p(ri, c) result.add p(ri, c)
of nkStmtListExpr: of nkStmtListExpr:
result = newNodeI(nkStmtList, ri.info) result = newNodeI(nkStmtList, ri.info)
for i in 0..ri.len-2: for i in 0..ri.len-2: