IonMonkey/Global value numbering: Difference between revisions

Jump to navigation Jump to search
no edit summary
(Created page with "GVN is a very powerful optimization for the engine. It folds similar expressions making sure we only have to execute it once, but also folds common expressions. [https://en.wi...")
 
No edit summary
Line 74: Line 74:
</code>
</code>


TODO
This function is used to fold common expressions into simpler or faster variants. The function returns ''this'' when it cannot get folded and the new MIR node when it can get folded. A common uses of this function is to replace MIR nodes that have constant operands and replace this node with the computed value. But it is not limited to constant folding. Specific combinations of MIR nodes can get matched and replaced by this function.
 
=== Examples ===
 
<pre>
MDefinition*
MNot::foldsTo(TempAllocator& alloc)
{
    // 1) Fold constant inputs
    if (MConstant* inputConst = input()->maybeConstantValue()) {
        bool b;
        if (inputConst->valueToBoolean(&b))
            return MConstant::New(alloc, BooleanValue(!b));
    }
 
    // 2) Fold MNot (MNot (MNot x)) to MNot x
    MDefinition* op = getOperand(0);
    if (op->isNot()) {
        MDefinition* opop = op->getOperand(0);
        if (opop->isNot())
            return opop;
    }
 
    // 3) Fold specific input types to constants
    if (input()->type() == MIRType::Undefined || input()->type() == MIRType::Null)
        return MConstant::New(alloc, BooleanValue(true));
    if (input()->type() == MIRType::Symbol)
        return MConstant::New(alloc, BooleanValue(false));
    if (input()->type() == MIRType::Object)
        return MConstant::New(alloc, BooleanValue(false));
 
    return this;
}
</pre>
 
This is an example of the implementation of MNot. Here we can distinguish folding happing based on three different reasons.
 
# The first checks are for constant propagation. If the input is a constant it is possible to just compute the result.
# Second part is checking for a common combination of MIR nodes. We try to find a succession of three MNot and remove the two unneeded MNots.
# Last part shows how we can look at the input type of the MNot and use that information to fold this MNot.
Confirmed users
24

edits

Navigation menu