Eliminate IncRef/DecRef pair when returning a local

In case a tracelet ends with CGetL + RetC, and IncRef/DecRef pair is
generated for the local being returned if it's ref-counted.  The
current ref-count optimization pass was unable to eliminate this, and
this diff tweaks the way HhbcTranslator generates the IR to enable the
optimization to kick in.

Here's the pattern generated without this diff:

CGetL 0
 t0 = LdLoc 0
 t1 = IncRef t0
RetC
 ExitWhenSurprised -> L
 DecRef t0
 RetVal t1
 ...

The source of t0 is not an IncRef, so the optimization doesn't apply.

With this diff, the IR is generated as:

CGetL 0
 t0 = LdLoc 0
 t1 = IncRef t0
RetC
 ExitWhenSurprised -> L
 DecRef t1
 RetVal t0
 ...

This enables the DecRef to be proved non-zero (becoming a DecRefNZ),
so it can be eliminated and the IncRef is sunk to the exit at L.
Esse commit está contido em:
ottoni
2013-03-15 11:20:55 -07:00
commit de Sara Golemon
commit e2b5b1f6f4
@@ -1653,14 +1653,34 @@ void HhbcTranslator::emitRet(Type type, bool freeInline) {
SSATmp* sp;
if (freeInline) {
/*
* In case retVal comes from a local, the logic below tweaks the code
* so that retVal is DecRef'd and the corresponding local's SSATmp is
* returned. This enables the ref-count optimization to eliminate the
* IncRef/DecRef pair in the main trace.
*/
SSATmp* retValSrcLoc = nullptr;
IRInstruction* retValSrcInstr = retVal->getInstruction();
if (retValSrcInstr->getOpcode() == IncRef) {
retValSrcLoc = retValSrcInstr->getSrc(0);
if (retValSrcLoc->getInstruction()->getOpcode() != LdLoc) {
retValSrcLoc = nullptr;
}
}
int retValLocId = retValSrcLoc ?
retValSrcLoc->getInstruction()->getExtra<LocalId>()->locId : -1;
for (int id = curFunc->numLocals() - 1; id >= 0; --id) {
/*
* TODO(#1980291): this doesn't correctly handle
* debug_backtrace.
*/
m_tb->genDecRefLoc(id);
if (retValLocId == id) {
m_tb->genDecRef(retVal);
} else {
m_tb->genDecRefLoc(id);
}
}
m_tb->genRetVal(retVal);
m_tb->genRetVal(retValSrcLoc ? retValSrcLoc : retVal);
sp = m_tb->genRetAdjustStack();
} else {
sp = m_tb->genGenericRetDecRefs(retVal, curFunc->numLocals());