Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cgen: fix redundant initialization of temporaries #896

Merged
merged 3 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions compiler/backend/ccgcalls.nim
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@ proc fixupCall(p: BProc, le, ri: CgNode, d: var TLoc,
if d.k notin {locTemp, locNone}:
reportObservableStore(p, le, ri)

if d.k == locNone: getTemp(p, typ[0], d, needsInit=true)
elif d.k notin {locTemp} and not hasNoInit(ri):
# reset before pass as 'result' var:
discard "resetLoc(p, d)"
# resetting the result location is the responsibility of the called
# procedure
if d.k == locNone:
getTemp(p, typ[0], d)
pl.add(addrLoc(p.config, d))
pl.add(~");$n")
line(p, cpsStmts, pl)
Expand All @@ -120,7 +120,7 @@ proc fixupCall(p: BProc, le, ri: CgNode, d: var TLoc,
exitCall(p, ri[0], canRaise)
else:
var tmp: TLoc
getTemp(p, typ[0], tmp, needsInit=true)
getTemp(p, typ[0], tmp)
var list: TLoc
initLoc(list, locCall, d.lode, OnUnknown)
list.r = pl
Expand Down Expand Up @@ -237,7 +237,7 @@ proc openArrayLoc(p: BProc, formalType: PType, n: CgNode): Rope =
else: internalError(p.config, "openArrayLoc: " & typeToString(a.t))

proc literalsNeedsTmp(p: BProc, a: TLoc): TLoc =
getTemp(p, a.lode.typ, result, needsInit=false)
getTemp(p, a.lode.typ, result)
genAssignment(p, result, a)

proc genArgStringToCString(p: BProc, n: CgNode): Rope {.inline.} =
Expand Down Expand Up @@ -333,11 +333,10 @@ proc genClosureCall(p: BProc, le, ri: CgNode, d: var TLoc) =
if d.k notin {locTemp, locNone}:
reportObservableStore(p, le, ri)

# resetting the result location is the responsibility of the called
# procedure
if d.k == locNone:
getTemp(p, typ[0], d, needsInit=true)
elif d.k notin {locTemp} and not hasNoInit(ri):
# reset before pass as 'result' var:
discard "resetLoc(p, d)"
getTemp(p, typ[0], d)
pl.add(addrLoc(p.config, d))
genCallPattern()
exitCall(p, ri[0], canRaise)
Expand Down
8 changes: 4 additions & 4 deletions compiler/backend/ccgexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ proc genSetNode(p: BProc, n: CgNode): Rope =

proc genOpenArrayConv(p: BProc; d: TLoc; a: TLoc) =
assert d.k != locNone
# getTemp(p, d.t, d)

case a.t.skipTypes(abstractVar).kind
of tyOpenArray, tyVarargs:
Expand Down Expand Up @@ -909,8 +908,9 @@ proc genStrAppend(p: BProc, e: CgNode, d: var TLoc) =
p.s(cpsStmts).add appends

proc genDefault(p: BProc; n: CgNode; d: var TLoc) =
if d.k == locNone: getTemp(p, n.typ, d, needsInit=true)
else: resetLoc(p, d)
if d.k == locNone:
getTemp(p, n.typ, d)
resetLoc(p, d)

proc rawGenNew(p: BProc, a: var TLoc, sizeExpr: Rope; needsInit: bool; doInitObj = true) =
var sizeExpr = sizeExpr
Expand Down Expand Up @@ -952,7 +952,7 @@ proc genNewSeqOfCap(p: BProc; e: CgNode; d: var TLoc) =
var a: TLoc
initLocExpr(p, e[1], a)
block:
if d.k == locNone: getTemp(p, e.typ, d, needsInit=false)
if d.k == locNone: getTemp(p, e.typ, d)
linefmt(p, cpsStmts, "$1.len = 0; $1.p = ($4*) #newSeqPayload($2, sizeof($3), NIM_ALIGNOF($3));$n",
[d.rdLoc, a.rdLoc, getTypeDesc(p.module, seqtype.lastSon),
getSeqPayloadType(p.module, seqtype),
Expand Down
18 changes: 7 additions & 11 deletions compiler/backend/cgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ proc genObjectInit(p: BProc, section: TCProcSection, t: PType, a: TLoc,
let tmp = defaultValueExpr(p, t, a.lode.info)
genAssignment(p, a, tmp)

proc constructLoc(p: BProc, loc: var TLoc, isTemp = false; doInitObj = true) =
proc constructLoc(p: BProc, loc: var TLoc; doInitObj = true) =
let kind = mapTypeChooser(loc)
case mapType(p.config, loc.t, kind)
of ctChar, ctBool, ctInt, ctInt8, ctInt16, ctInt32, ctInt64,
Expand All @@ -424,21 +424,18 @@ proc constructLoc(p: BProc, loc: var TLoc, isTemp = false; doInitObj = true) =
of ctNimStr, ctNimSeq:
linefmt(p, cpsStmts, "$1.len = 0; $1.p = NIM_NIL;$n", [rdLoc(loc)])
of ctArray, ctStruct, ctNimOpenArray:
if not isTemp:
# don't use nimZeroMem for temporary values for performance if we can
# avoid it
linefmt(p, cpsStmts, "#nimZeroMem((void*)$1, sizeof($2));$n",
[addrLoc(p.config, loc), getTypeDesc(p.module, loc.t, kind)])
linefmt(p, cpsStmts, "#nimZeroMem((void*)$1, sizeof($2));$n",
[addrLoc(p.config, loc), getTypeDesc(p.module, loc.t, kind)])

if doInitObj:
genObjectInit(p, cpsStmts, loc.t, loc, constructObj)
of ctVoid:
unreachable()

proc resetLoc(p: BProc, loc: var TLoc; doInitObj = true) =
# we always want to clear out the destination, so pass `false` for
# ``isTemp``
constructLoc(p, loc, false, doInitObj)
# resetting the loc is achieved by constructing a new empty value inside
# it
constructLoc(p, loc, doInitObj)

proc initLocalVar(p: BProc, v: PSym, immediateAsgn: bool) =
if sfNoInit notin v.flags:
Expand All @@ -452,15 +449,14 @@ proc initLocalVar(p: BProc, v: PSym, immediateAsgn: bool) =
if not immediateAsgn:
constructLoc(p, p.locals[v])

proc getTemp(p: BProc, t: PType, result: var TLoc; needsInit=false) =
proc getTemp(p: BProc, t: PType, result: var TLoc) =
inc(p.labels)
result.r = "T" & rope(p.labels) & "_"
linefmt(p, cpsLocals, "$1 $2;$n", [getTypeDesc(p.module, t, skVar), result.r])
result.k = locTemp
result.lode = lodeTyp t
result.storage = OnStack
result.flags = {}
constructLoc(p, result, not needsInit)
when false:
# XXX Introduce a compiler switch in order to detect these easily.
if getSize(p.config, t) > 1024 * 1024:
Expand Down
Loading