/*
 * Copyright (C) 2011 Apple Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */

#include "config.h"
#include "DFGGraph.h"

#if ENABLE(DFG_JIT)
#include "BytecodeKills.h"
#include "BytecodeLivenessAnalysisInlines.h"
#include "CodeBlock.h"
#include "DFGClobbersExitState.h"
#include "DFGJITCode.h"
#include "DFGMayExit.h"
#include "DFGVariableAccessDataDump.h"
#include "FullBytecodeLiveness.h"
#include "FunctionExecutableDump.h"
#include "JIT.h"
#include "JSLexicalEnvironment.h"
#include "Operations.h"
#include <wtf/CommaPrinter.h>
#include <wtf/ListDump.h>

namespace JSC { namespace DFG {

// Creates an array of stringized names.
static const char* dfgOpNames[] = {
#define STRINGIZE_DFG_OP_ENUM(opcode, flags) #opcode ,
    FOR_EACH_DFG_OP(STRINGIZE_DFG_OP_ENUM)
#undef STRINGIZE_DFG_OP_ENUM
};

ALWAYS_INLINE Graph::Graph(JSGlobalData& globalData, CodeBlock* codeBlock, unsigned osrEntryBytecodeIndex, const Operands<JSValue>& mustHandleValues)
: m_vm(globalData)
, m_codeBlock(codeBlock)
, m_profiledBlock(codeBlock->alternative())
, m_allocator(globalData.m_dfgState->m_allocator)
, m_inlineCallFrames(adoptPtr(new InlineCallFrameSet()))
, m_mustHandleAbstractValues(OperandsLike, mustHandleValues)
, m_localVars(0)
, m_nextMachineLocal(0)
, m_parameterSlots(0)
, m_osrEntryBytecodeIndex(osrEntryBytecodeIndex)
, m_mustHandleValues(mustHandleValues)
, m_mustHandleValuesMayIncludeGarbage(true)
, m_identifiers(codeBlock)
, weakReferences(codeBlock)
, m_fixpointState(BeforeFixpoint)
, m_structureRegistrationState(HaveNotStartedRegistering)
, m_form(LoadStore)
, m_unificationState(LocallyUnified)
, m_planStage(PlanStageInitial)
, m_refCountState(EverythingIsLive)
, m_hasExceptionHandlers(false)
{
    ASSERT(m_profiledBlock);

    cleanMustHandleValuesIfNecessary();

    for (unsigned i = m_mustHandleAbstractValues.size(); i--;)
        m_mustHandleAbstractValues[i].setMostSpecific(*this, m_mustHandleValues[i]);

    m_hasDebuggerEnabled = m_profiledBlock->globalObject()->hasDebugger()
        /*|| Options::forceDebuggerBytecodeGeneration()*/;
}

PassOwnPtr<Graph> Graph::create(JSGlobalData& globalData, CodeBlock* codeBlock, unsigned osrEntryBytecodeIndex, const Operands<JSValue>& mustHandleValues)
{
	return adoptPtr(new (std::nothrow) Graph(globalData, codeBlock, osrEntryBytecodeIndex, mustHandleValues));
}

Graph::~Graph()
{
    for (BlockIndex blockIndex = numBlocks(); blockIndex--;) {
        BasicBlock* block = this->block(blockIndex);
        if (!block)
            continue;

        for (unsigned phiIndex = block->phis.size(); phiIndex--;)
            m_allocator.free(block->phis[phiIndex]);
        for (unsigned nodeIndex = block->size(); nodeIndex--;)
            m_allocator.free(block->at(nodeIndex));
    }
    m_allocator.freeAll();
}

const char *Graph::opName(NodeType op)
{
    return dfgOpNames[op];
}

static void printWhiteSpace(PrintStream& out, unsigned amount)
{
    while (amount-- > 0)
        out.print(" ");
}

bool Graph::dumpCodeOrigin(PrintStream& out, const char* prefix, Node*& previousNodeRef, Node* currentNode)
{
    if (!currentNode->origin.semantic)
        return false;

    Node* previousNode = previousNodeRef;
    previousNodeRef = currentNode;

    if (!previousNode)
        return false;
    
    if (previousNode->origin.semantic.inlineCallFrame == currentNode->origin.semantic.inlineCallFrame)
        return false;
    
    Vector<CodeOrigin> previousInlineStack = previousNode->origin.semantic.inlineStack();
    Vector<CodeOrigin> currentInlineStack = currentNode->origin.semantic.inlineStack();
    unsigned commonSize = std::min(previousInlineStack.size(), currentInlineStack.size());
    unsigned indexOfDivergence = commonSize;
    for (unsigned i = 0; i < commonSize; ++i) {
        if (previousInlineStack[i].inlineCallFrame != currentInlineStack[i].inlineCallFrame) {
            indexOfDivergence = i;
            break;
        }
    }
    
    bool hasPrinted = false;
    
    // Print the pops.
    for (unsigned i = previousInlineStack.size(); i-- > indexOfDivergence;) {
        out.print(prefix);
        printWhiteSpace(out, i * 2);
        out.print("<-- ", *previousInlineStack[i].inlineCallFrame, "\n");
        hasPrinted = true;
    }
    
    // Print the pushes.
    for (unsigned i = indexOfDivergence; i < currentInlineStack.size(); ++i) {
        out.print(prefix);
        printWhiteSpace(out, i * 2);
        out.print("--> ", *currentInlineStack[i].inlineCallFrame, "\n");
        hasPrinted = true;
    }
    
    return hasPrinted;
}

int Graph::amountOfNodeWhiteSpace(Node* node)
{
    return (node->origin.semantic.inlineDepth() - 1) * 2;
}

void Graph::printNodeWhiteSpace(PrintStream& out, Node* node)
{
    printWhiteSpace(out, amountOfNodeWhiteSpace(node));
}

void Graph::dump(PrintStream& out, const char* prefix, Node* node)
{
#if 0
    NodeType op = node->op();

    unsigned refCount = node->refCount();
    bool mustGenerate = node->mustGenerate();
    if (mustGenerate)
        --refCount;

    out.print(prefix);
    printNodeWhiteSpace(out, node);

    // Example/explanation of dataflow dump output
    //
    //   14:   <!2:7>  GetByVal(@3, @13)
    //   ^1     ^2 ^3     ^4       ^5
    //
    // (1) The nodeIndex of this operation.
    // (2) The reference count. The number printed is the 'real' count,
    //     not including the 'mustGenerate' ref. If the node is
    //     'mustGenerate' then the count it prefixed with '!'.
    // (3) The virtual register slot assigned to this node.
    // (4) The name of the operation.
    // (5) The arguments to the operation. The may be of the form:
    //         @#   - a NodeIndex referencing a prior node in the graph.
    //         arg# - an argument number.
    //         $#   - the index in the CodeBlock of a constant { for numeric constants the value is displayed | for integers, in both decimal and hex }.
    //         id#  - the index in the CodeBlock of an identifier { if codeBlock is passed to dump(), the string representation is displayed }.
    //         var# - the index of a var on the global object, used by GetGlobalVar/GetGlobalLexicalVariable/PutGlobalVariable operations.
    out.printf("% 4d:<%c%u:", (int)node->index(), mustGenerate ? '!' : ' ', refCount);
	if (node->hasResult() && node->hasVirtualRegister() && VirtualRegister_(node->virtualRegister()).isValid())
		out.print(node->virtualRegister());
	else
		out.print("-");
    out.print(">\t", opName(op), "(");
    CommaPrinter comma;
    if (node->flags() & NodeHasVarArgs) {
        for (unsigned childIdx = node->firstChild(); childIdx < node->firstChild() + node->numChildren(); childIdx++) {
            if (!m_varArgChildren[childIdx])
                continue;
            out.print(comma, m_varArgChildren[childIdx]);
        }
    } else {
        if (!!node->child1() || !!node->child2() || !!node->child3())
            out.print(comma, node->child1());
        if (!!node->child2() || !!node->child3())
            out.print(comma, node->child2());
        if (!!node->child3())
            out.print(comma, node->child3());
    }

    if (toCString(NodeFlagsDump(node->flags())) != "<empty>")
        out.print(comma, NodeFlagsDump(node->flags()));
    if (node->prediction())
        out.print(comma, SpeculationDump(node->prediction()));
    if (node->hasArrayMode())
        out.print(comma, node->arrayMode());
    if (node->hasArithMode())
        out.print(comma, node->arithMode());
    if (node->hasArithRoundingMode())
        out.print(comma, "Rounding:", node->arithRoundingMode());
    if (node->hasScopeOffset())
        out.print(comma, node->scopeOffset());
    if (node->hasDirectArgumentsOffset())
        out.print(comma, node->capturedArgumentsOffset());
    if (node->hasRegisterPointer())
		out.print(comma, "global", "(", RawPointer(node->variablePointer()), ")");
	if (node->hasIdentifier())
        out.print(comma, "id", node->identifierNumber(), "{", m_identifiers[node->identifierNumber()], "}");
    if (node->hasStructureSet()) {
        //for (size_t i = 0; i < node->structureSet().size(); ++i)
        //    out.print(comma, "struct(", RawPointer(node->structureSet()[i]), ": ", IndexingTypeDump(node->structureSet()[i]->indexingType()), ")");
    }
    //if (node->hasStructure())
    //    out.print(comma, "struct(", RawPointer(node->structure()), ": ", IndexingTypeDump(node->structure()->indexingType()), ")");
    //if (node->hasTransition())
	//	out.print(comma, pointerDumpInContext(node->transition(), 0));
    if (node->hasFunction()) {
        out.print(comma, "function(", RawPointer(node->function()), ", ");
        if (node->function()->inherits(JSFunction::info())) {
            JSFunction* function = jsCast<JSFunction*>(node->function());
            if (function->isHostFunction())
                out.print("<host function>");
            else
                out.print(FunctionExecutableDump(function->jsExecutable()));
        } else
            out.print("<not JSFunction>");
        out.print(")");
    }
    if (node->hasExecutable()) {
        if (node->executable()->inherits(FunctionExecutable::info()))
            out.print(comma, "executable(", FunctionExecutableDump(jsCast<FunctionExecutable*>(node->executable())), ")");
        else
            out.print(comma, "executable(not function: ", RawPointer(node->executable()), ")");
    }
    if (node->hasStorageAccessData()) {
        StorageAccessData& storageAccessData = m_storageAccessData[node->storageAccessDataIndex()];
        out.print(comma, "id", storageAccessData.identifierNumber, "{", m_identifiers[storageAccessData.identifierNumber], "}");
        out.print(", ", static_cast<ptrdiff_t>(storageAccessData.offset));
    }
    ASSERT(node->hasVariableAccessData(*this) == node->hasLocal(*this));
    if (node->hasVariableAccessData(*this)) {
        VariableAccessData* variableAccessData = node->variableAccessData();
        int operand = variableAccessData->operand();
        if (operandIsArgument(operand))
            out.print(comma, "arg", operandToArgument(operand), "(", VariableAccessDataDump(*this, variableAccessData), ")");
        else
            out.print(comma, "loc", operandToLocal(operand), "(", VariableAccessDataDump(*this, variableAccessData), ")");

        operand = variableAccessData->machineLocal();
        if (operand != InvalidVirtualRegister) {
            if (operandIsArgument(operand))
                out.print(comma, "machine:arg", operandToArgument(operand));
            else
                out.print(comma, "machine:loc", operandToLocal(operand));
        }
    }
    if (node->hasUnlinkedLocal()) {
        int operand = node->unlinkedLocal();
        if (operandIsArgument(operand))
            out.print(comma, "arg", operandToArgument(operand));
        else
            out.print(comma, "loc", operandToLocal(operand));
    }
    if (node->hasConstantBuffer()) {
        out.print(comma);
        out.print(node->startConstant(), ":[");
        CommaPrinter anotherComma;
        for (unsigned i = 0; i < node->numConstants(); ++i)
            out.print(anotherComma, m_codeBlock->constantBuffer(node->startConstant())[i]);
        out.print("]");
    }
    //if (node->hasIndexingType())
    //    out.print(comma, IndexingTypeDump(node->indexingType()));
    if (node->hasTypedArrayType())
        out.print(comma, node->typedArrayType());
    //if (node->hasExecutionCounter())
    //    out.print(comma, RawPointer(node->executionCounter()));
    if (node->hasWatchpointSet())
        out.print(comma, RawPointer(node->watchpointSet()));
    if (node->hasStoragePointer())
        out.print(comma, RawPointer(node->storagePointer()));
    if (node->hasPhi())
        out.print(comma, "^", node->phi()->index());
    if (node->isConstant()) {
        out.print(comma, "$", node->constantNumber());
        JSValue value = valueOfJSConstant(node);
        out.print(" = ", value);
    }
    if (op == WeakJSConstant)
        out.print(comma, RawPointer(node->weakConstant()), " (structure: ", RawPointer(node->weakConstant()->structure()), ")");
    if (node->isJump())
		out.print(comma, "T:", *node->targetBlock());
	if (node->isBranch())
		out.print(comma, "T:", node->branchData()->taken, ", F:", node->branchData()->notTaken);
	if (node->isSwitch()) {
		SwitchData* data = node->switchData();
		out.print(comma, data->kind);
		//for (unsigned i = 0; i < data->cases.size(); ++i)
		//	out.print(comma, inContext(data->cases[i].value, context), ":", data->cases[i].target);
		out.print(comma, "default:", data->fallThrough);
	}
    if (node->origin.isSet()) {
		out.print(comma, "bc#", node->origin.semantic.bytecodeIndex);
		//if (node->origin.semantic != node->origin.forExit)
		//    out.print(comma, "exit: ", node->origin.forExit);
    }
    
    out.print(")");

	if (node->hasVariableAccessData(*this))
		out.print("  predicting ", SpeculationDump(node->variableAccessData()->prediction()));
	else if (node->hasHeapPrediction())
		out.print("  predicting ", SpeculationDump(node->getHeapPrediction()));
    
    out.print("\n");
#endif
}

bool Graph::terminalsAreValid()
{
	NaturalBlockIterable naturalBlockIterable = blocksInNaturalOrder();
	NaturalBlockIterable::iterator end = naturalBlockIterable.end();

    for (NaturalBlockIterable::iterator it = naturalBlockIterable.begin(); it != end; ++it) {
        if (!(*it)->terminal())
            return false;
    }
    return true;
}

void Graph::dumpBlockHeader(PrintStream& out, const char* prefix, BasicBlock* block, PhiNodeDumpMode phiNodeDumpMode)
{
#if 0
    //out.print(prefix, "Block ", *block, " (", block->at(0)->origin.semantic, "):", block->isReachable ? "" : " (skipped)", block->isOSRTarget ? " (OSR target)" : "", "\n");
    out.print(prefix, "  Predecessors:");
    if (block->executionCount == block->executionCount)
        out.print(prefix, "  Execution count: ", block->executionCount, "\n");
    for (size_t i = 0; i < block->predecessors.size(); ++i)
        out.print(" ", *block->predecessors[i]);
    out.print("\n");
    if (m_dominators.isValid()) {
        out.print(prefix, "  Dominated by:");
        for (size_t i = 0; i < m_blocks.size(); ++i) {
            if (!m_dominators.dominates(i, block->index))
                continue;
            out.print(" #", i);
        }
        out.print("\n");
        out.print(prefix, "  Dominates:");
        for (size_t i = 0; i < m_blocks.size(); ++i) {
            if (!m_dominators.dominates(block->index, i))
                continue;
            out.print(" #", i);
        }
        out.print("\n");
    }
    if (m_naturalLoops.isValid()) {
        if (const NaturalLoop* loop = m_naturalLoops.headerOf(block)) {
            out.print(prefix, "  Loop header, contains:");
            Vector<BlockIndex> sortedBlockList;
            for (unsigned i = 0; i < loop->size(); ++i)
                sortedBlockList.append(loop->at(i)->index);
            std::sort(sortedBlockList.begin(), sortedBlockList.end());
            for (unsigned i = 0; i < sortedBlockList.size(); ++i)
                out.print(" #", sortedBlockList[i]);
            out.print("\n");
        }

        Vector<const NaturalLoop*> containingLoops =
            m_naturalLoops.loopsOf(block);
        if (!containingLoops.isEmpty()) {
            out.print(prefix, "  Containing loop headers:");
            for (unsigned i = 0; i < containingLoops.size(); ++i)
                out.print(" #", *containingLoops[i]->header());
            out.print("\n");
        }
    }
    if (!block->phis.isEmpty()) {
		out.print(prefix, "  Phi Nodes:");
		for (size_t i = 0; i < block->phis.size(); ++i) {
			Node* phiNode = block->phis[i];
			if (!phiNode->shouldGenerate() && phiNodeDumpMode == DumpLivePhisOnly)
				continue;
			out.print(" @", phiNode->index(), "<", phiNode->refCount(), ">->(");
			if (phiNode->child1().node()) {
				out.print("@", phiNode->child1()->index());
				if (phiNode->child2().node()) {
					out.print(", @", phiNode->child2()->index());
					if (phiNode->child3().node())
						out.print(", @", phiNode->child3()->index());
				}
			}
			out.print(")", i + 1 < block->phis.size() ? "," : "");
		}
    }
    out.print("\n");
#endif
}

void Graph::dump(PrintStream& out)
{
#if 0
    //dataLog("DFG for ", CodeBlockWithJITType(m_codeBlock, JITCode::DFGJIT), ":\n");
    dataLog("  Fixpoint state: ", m_fixpointState, "; Form: ", m_form, "; Unification state: ", m_unificationState, "; Ref count state: ", m_refCountState, "\n");
    //if (m_form == SSA)
	//	out.print("  Argument formats: ", listDump(m_argumentFormats), "\n");
	//else
	//	out.print("  Arguments: ", listDump(m_arguments), "\n");
    out.print("  ArgumentPosition size: ", m_argumentPositions.size(), "\n");
    for (size_t i = 0; i < m_argumentPositions.size(); ++i) {
        out.print("    #", i, ": ");
        ArgumentPosition& arguments = m_argumentPositions[i];
        arguments.dump(out, this);
    }
    
    Node* lastNode = 0;
    for (size_t b = 0; b < m_blocks.size(); ++b) {
        BasicBlock* block = m_blocks[b].get();
        if (!block)
            continue;
        dumpBlockHeader(out, "", block, DumpAllPhis);
        out.print("  States: ", block->cfaStructureClobberStateAtHead, "\n");
        switch (m_form) {
        case LoadStore:
        case ThreadedCPS: {
            out.print("  Vars Before: ");
			if (block->cfaHasVisited)
				out.print(block->valuesAtHead);
			else
				out.print("<empty>");
			out.print("\n");
			out.print("  Var Links: ");
			dumpOperands(block->variablesAtHead, out);
			out.print("\n");
			break;
		}

		case SSA: {
#if ENABLE(FTL_JIT)
			RELEASE_ASSERT(block->ssa);
			out.print("  Flush format: ", block->ssa->flushFormatAtHead, "\n");
			out.print("  Availability: ", block->ssa->availabilityAtHead, "\n");
			out.print("  Live: ", nodeListDump(block->ssa->liveAtHead), "\n");
			out.print("  Values: ", nodeMapDump(block->ssa->valuesAtHead), "\n");
#endif
			break;
		} }
        for (size_t i = 0; i < block->size(); ++i) {
            dumpCodeOrigin(out, "", lastNode, block->at(i));
            dump(out, "", block->at(i));
            lastNode = block->at(i);
        }
        switch (m_form) {
        case LoadStore:
        case ThreadedCPS: {
			out.print("  vars after: ");
			if (block->cfaHasVisited)
				out.print(block->valuesAtTail);
			else
				out.print("<empty>");
			out.print("\n");
			out.print("  var links: ");
			dumpOperands(block->variablesAtTail, out);
			out.print("\n");
			break;
		}

		case SSA: {
#if ENABLE(FTL_JIT)
			RELEASE_ASSERT(block->ssa);
			out.print("  Flush format: ", block->ssa->flushFormatAtTail, "\n");
			out.print("  Availability: ", block->ssa->availabilityAtTail, "\n");
			out.print("  Live: ", nodeListDump(block->ssa->liveAtTail), "\n");
			out.print("  Values: ", nodeMapDump(block->ssa->valuesAtTail), "\n");
#endif
			break;
		} }
    }
#endif
}

void Graph::dethread()
{
    if (m_form == LoadStore || m_form == SSA)
        return;
    
    if (logCompilationChanges())
        dataLog("Dethreading DFG graph.\n");
    
    SamplingRegion samplingRegion("DFG Dethreading");
    
    for (BlockIndex blockIndex = m_blocks.size(); blockIndex--;) {
        BasicBlock* block = m_blocks[blockIndex].get();
        if (!block)
            continue;
        for (unsigned phiIndex = block->phis.size(); phiIndex--;) {
            Node* phi = block->phis[phiIndex];
            phi->children.reset();
        }
    }
    
    m_form = LoadStore;
}

void Graph::handleSuccessor(Vector<BasicBlock*, 16>& worklist, BasicBlock* block, BasicBlock* successor)
{
    if (!successor->isReachable) {
        successor->isReachable = true;
        worklist.append(successor);
    }
    
    successor->predecessors.append(block);
}

void Graph::determineReachability()
{
    Vector<BasicBlock*, 16> worklist;
    worklist.append(block(0));
    block(0)->isReachable = true;
    while (!worklist.isEmpty()) {
        BasicBlock* block = worklist.last();
        worklist.removeLast();
        for (unsigned i = block->numSuccessors(); i--;)
            handleSuccessor(worklist, block, block->successor(i));
    }
}

void Graph::resetReachability()
{
    for (BlockIndex blockIndex = m_blocks.size(); blockIndex--;) {
        BasicBlock* block = m_blocks[blockIndex].get();
        if (!block)
            continue;
        block->isReachable = false;
        block->predecessors.clear();
    }
    
    determineReachability();
}

void Graph::killBlockAndItsContents(BasicBlock* block)
{
    for (unsigned phiIndex = block->phis.size(); phiIndex--;)
        m_allocator.free(block->phis[phiIndex]);
    for (unsigned nodeIndex = block->size(); nodeIndex--;)
        m_allocator.free(block->at(nodeIndex));

    killBlock(block);
}

void Graph::killUnreachableBlocks()
{
    for (BlockIndex blockIndex = 0; blockIndex < numBlocks(); ++blockIndex) {
        BasicBlock* block = this->block(blockIndex);
        if (!block)
            continue;
        if (block->isReachable)
            continue;

        killBlockAndItsContents(block);
    }
}

void Graph::invalidateCFG()
{
    m_dominators.invalidate();
    m_naturalLoops.invalidate();
}

void Graph::substituteGetLocal(BasicBlock& block, unsigned startIndexInBlock, VariableAccessData* variableAccessData, Node* newGetLocal)
{
	for (unsigned indexInBlock = startIndexInBlock; indexInBlock < block.size(); ++indexInBlock) {
		Node* node = block[indexInBlock];
		bool shouldContinue = true;
		switch (node->op()) {
		case SetLocal: {
			if (node->local() == variableAccessData->local())
				shouldContinue = false;
			break;
		}

		case GetLocal: {
			if (node->variableAccessData() != variableAccessData)
				continue;
			substitute(block, indexInBlock, node, newGetLocal);
			Node* oldTailNode = block.variablesAtTail.operand(variableAccessData->local());
			if (oldTailNode == node)
				block.variablesAtTail.operand(variableAccessData->local()) = newGetLocal;
			shouldContinue = false;
			break;
		}

		default:
			break;
		}
		if (!shouldContinue)
			break;
	}
}

bool Graph::isStillValid() const
{
    return m_watchpoints.areStillValid(m_vm);
}

// Utilities for pre- and post-order traversals.
namespace {

inline void addForPreOrder(Vector<BasicBlock*>& result, Vector<BasicBlock*, 16>& worklist, BitVector& seen, BasicBlock* block)
{
    if (seen.get(block->index))
        return;

    result.append(block);
    worklist.append(block);
    seen.set(block->index);
}

enum PostOrderTaskKind {
    PostOrderFirstVisit,
    PostOrderAddToResult
};

struct PostOrderTask {
    PostOrderTask(BasicBlock* block = 0, PostOrderTaskKind kind = PostOrderFirstVisit)
        : m_block(block)
        , m_kind(kind)
    {
    }

    BasicBlock* m_block;
    PostOrderTaskKind m_kind;
};

inline void addForPostOrder(Vector<PostOrderTask, 16>& worklist, BitVector& seen, BasicBlock* block)
{
    if (seen.get(block->index))
        return;

    worklist.append(PostOrderTask(block, PostOrderFirstVisit));
    seen.set(block->index);
}

} // anonymous namespace

void Graph::getBlocksInPreOrder(Vector<BasicBlock*>& result)
{
    Vector<BasicBlock*, 16> worklist;
    BitVector seen;
    addForPreOrder(result, worklist, seen, block(0));
    while (!worklist.isEmpty()) {
        BasicBlock* block = worklist.takeLast();
        for (unsigned i = block->numSuccessors(); i--;)
            addForPreOrder(result, worklist, seen, block->successor(i));
    }
}

void Graph::getBlocksInPostOrder(Vector<BasicBlock*>& result)
{
    Vector<PostOrderTask, 16> worklist;
    BitVector seen;
    addForPostOrder(worklist, seen, block(0));
    while (!worklist.isEmpty()) {
        PostOrderTask task = worklist.takeLast();
        switch (task.m_kind) {
        case PostOrderFirstVisit:
            worklist.append(PostOrderTask(task.m_block, PostOrderAddToResult));
            for (unsigned i = task.m_block->numSuccessors(); i--;)
                addForPostOrder(worklist, seen, task.m_block->successor(i));
            break;
        case PostOrderAddToResult:
            result.append(task.m_block);
            break;
        }
    }
}

void Graph::clearFlagsOnAllNodes(NodeFlags flags)
{
    for (BlockIndex blockIndex = numBlocks(); blockIndex--;) {
        BasicBlock* block = m_blocks[blockIndex].get();
        if (!block)
            continue;
        for (unsigned phiIndex = block->phis.size(); phiIndex--;)
            block->phis[phiIndex]->clearFlags(flags);
        for (unsigned nodeIndex = block->size(); nodeIndex--;)
            block->at(nodeIndex)->clearFlags(flags);
    }
}

void Graph::clearReplacements()
{
    for (BlockIndex blockIndex = numBlocks(); blockIndex--;) {
        BasicBlock* block = m_blocks[blockIndex].get();
        if (!block)
            continue;
        for (unsigned phiIndex = block->phis.size(); phiIndex--;)
            block->phis[phiIndex]->setReplacement(0);
        for (unsigned nodeIndex = block->size(); nodeIndex--;)
            block->at(nodeIndex)->setReplacement(0);
    }
}

void Graph::initializeNodeOwners()
{
    for (BlockIndex blockIndex = numBlocks(); blockIndex--;) {
        BasicBlock* block = m_blocks[blockIndex].get();
        if (!block)
            continue;
        for (unsigned phiIndex = block->phis.size(); phiIndex--;)
            block->phis[phiIndex]->owner = block;
        for (unsigned nodeIndex = block->size(); nodeIndex--;)
            block->at(nodeIndex)->owner = block;
    }
}

void Graph::clearEpochs()
{
    for (BlockIndex blockIndex = numBlocks(); blockIndex--;) {
        BasicBlock* block = m_blocks[blockIndex].get();
        if (!block)
            continue;
        for (unsigned phiIndex = block->phis.size(); phiIndex--;)
            block->phis[phiIndex]->setEpoch(Epoch());
        for (unsigned nodeIndex = block->size(); nodeIndex--;)
            block->at(nodeIndex)->setEpoch(Epoch());
    }
}

JSArrayBufferView* Graph::tryGetFoldableView(JSValue value)
{
    if (!value)
        return 0;
    JSArrayBufferView* view = jsDynamicCast<JSArrayBufferView*>(value);
    if (!view)
        return 0;
    if (!view->length())
        return 0;
    WTF::loadLoadFence();
    watchpoints().addLazily(view);
    return view;
}

JSArrayBufferView* Graph::tryGetFoldableView(JSValue value, ArrayMode arrayMode)
{
    if (arrayMode.type() != Array::AnyTypedArray && arrayMode.typedArrayType() == NotTypedArray)
        return 0;
    return tryGetFoldableView(value);
}

unsigned Graph::frameRegisterCount()
{
    return m_nextMachineLocal + m_parameterSlots;
}

unsigned Graph::requiredRegisterCountForExit()
{
    unsigned count = JIT::frameRegisterCountFor(m_profiledBlock);
    for (InlineCallFrameSet::iterator iter = m_inlineCallFrames->begin(); !!iter; ++iter) {
        InlineCallFrame* inlineCallFrame = *iter;
        CodeBlock* codeBlock = baselineCodeBlockForInlineCallFrame(inlineCallFrame);
        unsigned requiredCount = VirtualRegister_(inlineCallFrame->stackOffset).toLocal() + 1 + JIT::frameRegisterCountFor(codeBlock);
        count = std::max(count, requiredCount);
    }
    return count;
}

unsigned Graph::requiredRegisterCountForExecutionAndExit()
{
    return std::max(frameRegisterCount(), requiredRegisterCountForExit());
}

JSValue Graph::tryGetConstantProperty(
    JSValue base, const StructureSet& structureSet, PropertyOffset offset)
{
    if (!base || !base.isObject())
        return JSValue();

    JSObject* object = asObject(base);

    for (unsigned i = structureSet.size(); i--;) {
        Structure* structure = structureSet[i];
        assertIsRegistered(structure);

        RefPtr<WatchpointSet> set = structure->propertyReplacementWatchpointSet(offset);
        if (!set || !set->isStillValid())
            return JSValue();

        ASSERT(structure->isValidOffset(offset));
        ASSERT(!structure->isUncacheableDictionary());

        watchpoints().addLazily(set.get());
    }

    // What follows may require some extra thought. We need this load to load a valid JSValue. If
    // our profiling makes sense and we're still on track to generate code that won't be
    // invalidated, then we have nothing to worry about. We do, however, have to worry about
    // loading - and then using - an invalid JSValue in the case that unbeknownst to us our code
    // is doomed.
    //
    // One argument in favor of this code is that it should definitely work because the butterfly
    // is always set before the structure. However, we don't currently have a fence between those
    // stores. It's not clear if this matters, however. We don't ever shrink the property storage.
    // So, for this to fail, you'd need an access on a constant object pointer such that the inline
    // caches told us that the object had a structure that it did not *yet* have, and then later,
    // the object transitioned to that structure that the inline caches had alraedy seen. And then
    // the processor reordered the stores. Seems unlikely and difficult to test. I believe that
    // this is worth revisiting but it isn't worth losing sleep over. Filed:
    // https://bugs.webkit.org/show_bug.cgi?id=134641
    //
    // For now, we just do the minimal thing: defend against the structure right now being
    // incompatible with the getDirect we're trying to do. The easiest way to do that is to
    // determine if the structure belongs to the proven set.

    if (!structureSet.contains(object->structure()))
        return JSValue();

    return object->getDirect(offset);
}

JSValue Graph::tryGetConstantProperty(JSValue base, Structure* structure, PropertyOffset offset)
{
    return tryGetConstantProperty(base, StructureSet(structure), offset);
}

JSValue Graph::tryGetConstantProperty(
    JSValue base, const StructureAbstractValue& structure, PropertyOffset offset)
{
    if (structure.isTop() || structure.isClobbered()) {
        // FIXME: If we just converted the offset to a uid, we could do ObjectPropertyCondition
        // watching to constant-fold the property.
        // https://bugs.webkit.org/show_bug.cgi?id=147271
        return JSValue();
    }

    return tryGetConstantProperty(base, structure.set(), offset);
}

JSValue Graph::tryGetConstantProperty(const AbstractValue& base, PropertyOffset offset)
{
    return tryGetConstantProperty(base.m_value, base.m_structure, offset);
}

JSValue Graph::tryGetConstantClosureVar(JSValue base, ScopeOffset offset)
{
    // This has an awesome concurrency story. See comment for GetGlobalVar in ByteCodeParser.

    if (!base)
        return JSValue();

    JSLexicalEnvironment* activation = jsDynamicCast<JSLexicalEnvironment*>(base);
    if (!activation)
        return JSValue();

    SymbolTable* symbolTable = activation->symbolTable();
    JSValue value;
    WatchpointSet* set;
    {
		//ConcurrentJITLocker locker(symbolTable->m_lock);

		SymbolTableEntry* entry = symbolTable->entryFor(offset);
		if (!entry)
			return JSValue();

		set = entry->watchpointSet();
		if (!set)
			return JSValue();

		if (set->state() != IsWatched)
			return JSValue();

		ASSERT(entry->scopeOffset() == offset);
		value = activation->variableAt(offset).get();
		if (!value)
			return JSValue();
    }

    watchpoints().addLazily(set);

    return value;
}

JSValue Graph::tryGetConstantClosureVar(const AbstractValue& value, ScopeOffset offset)
{
    return tryGetConstantClosureVar(value.m_value, offset);
}

JSValue Graph::tryGetConstantClosureVar(Node* node, ScopeOffset offset)
{
    if (!node->hasConstant())
        return JSValue();
    return tryGetConstantClosureVar(valueOfJSConstant(node), offset);
}

bool Graph::watchCondition(const ObjectPropertyCondition& key)
{
    if (!key.isWatchable(m_vm))
        return false;

    weakReferences.addLazily(key.object());
    if (key.hasPrototype())
        weakReferences.addLazily(key.prototype());
    if (key.hasRequiredValue())
        weakReferences.addLazily(key.requiredValue());

    watchpoints().addLazily(key);

    if (key.kind() == PropertyCondition::Presence)
        m_safeToLoad.add(std::make_pair(key.object(), key.offset()));

    return true;
}

bool Graph::watchConditions(const ObjectPropertyConditionSet& keys)
{
    if (!keys.isValid())
        return false;

    for(ObjectPropertyConditionSet::iterator it = keys.begin(); it != keys.end(); ++it) {
        if (!watchCondition(*it))
            return false;
    }
    return true;
}

bool Graph::isSafeToLoad(JSObject* base, PropertyOffset offset)
{
    return m_safeToLoad.contains(std::make_pair(base, offset));
}

FullBytecodeLiveness& Graph::livenessFor(CodeBlock* codeBlock)
{
    HashMap<CodeBlock*, OwnPtr<FullBytecodeLiveness> >::iterator iter = m_bytecodeLiveness.find(codeBlock);
    if (iter != m_bytecodeLiveness.end())
        return *iter->second;

    OwnPtr<FullBytecodeLiveness> liveness = adoptPtr(new FullBytecodeLiveness());
    codeBlock->livenessAnalysis().computeFullLiveness(*liveness);
    FullBytecodeLiveness& result = *liveness;
    m_bytecodeLiveness.add(codeBlock, liveness.release());
    return result;
}

FullBytecodeLiveness& Graph::livenessFor(InlineCallFrame* inlineCallFrame)
{
    return livenessFor(baselineCodeBlockFor(inlineCallFrame));
}

BytecodeKills& Graph::killsFor(CodeBlock* codeBlock)
{
    HashMap<CodeBlock*, OwnPtr<BytecodeKills>>::iterator iter = m_bytecodeKills.find(codeBlock);
    if (iter != m_bytecodeKills.end())
        return *iter->second;

    OwnPtr<BytecodeKills> kills = adoptPtr(new BytecodeKills());
    codeBlock->livenessAnalysis().computeKills(*kills);
    BytecodeKills& result = *kills;
    m_bytecodeKills.add(codeBlock, kills.release());
    return result;
}

BytecodeKills& Graph::killsFor(InlineCallFrame* inlineCallFrame)
{
    return killsFor(baselineCodeBlockFor(inlineCallFrame));
}

bool Graph::isLiveInBytecode(int operand, CodeOrigin codeOrigin)
{
    for (;;) {
		VirtualRegister_ reg = VirtualRegister_(
			operand - codeOrigin.stackOffset());

		if (operand < codeOrigin.stackOffset() + JSStack::CallFrameHeaderSize) {
            if (reg.isArgument()) {
                RELEASE_ASSERT(reg.offset() < JSStack::CallFrameHeaderSize);

                if (!codeOrigin.inlineCallFrame->isClosureCall)
                    return false;

                if (reg.offset() == JSStack::Callee)
                    return true;

                return false;
            }

            return livenessFor(codeOrigin.inlineCallFrame).operandIsLive(
                reg.offset(), codeOrigin.bytecodeIndex);
        }

        InlineCallFrame* inlineCallFrame = codeOrigin.inlineCallFrame;
        if (!inlineCallFrame)
            return true;

        // Arguments are always live. This would be redundant if it wasn't for our
        // op_call_varargs inlining.
        if (reg.isArgument()
            && static_cast<size_t>(reg.toArgument()) < inlineCallFrame->arguments.size())
            return true;

        // We need to handle tail callers because we may decide to exit to the
        // the return bytecode following the tail call.
        codeOrigin = inlineCallFrame->directCaller;
    }

    RELEASE_ASSERT_NOT_REACHED();
    return false;
}

namespace {
struct LocalsLiveInBytecodeFunctor {
	LocalsLiveInBytecodeFunctor(BitVector& result)
	:m_result(result)
	{
	}

    void operator()(int reg)
    {
        ASSERT(VirtualRegister_(reg).isLocal());
        m_result.quickSet(VirtualRegister_(reg).toLocal());
    }

    BitVector& m_result;
};
}

BitVector Graph::localsLiveInBytecode(CodeOrigin codeOrigin)
{
    BitVector result;
    result.ensureSize(block(0)->variablesAtHead.numberOfLocals());

    LocalsLiveInBytecodeFunctor functor(result);

    forAllLocalsLiveInBytecode(
        codeOrigin,
        functor);
    return result;
}

static ValueProfile* valueProfileFor(Graph& graph, CodeBlock* profiledBlock, Node* node)
{
    if (!operandIsArgument(node->local()))
        return 0;
    int argument = operandToArgument(node->local());
    Node* argumentNode = graph.m_arguments[argument];
	if (!argumentNode)
		return 0;
	if (node->variableAccessData() != argumentNode->variableAccessData())
		return 0;
    return profiledBlock->valueProfileForArgument(argument);
}

MethodOfGettingAValueProfile Graph::methodOfGettingAValueProfileFor(Node* currentNode, Node* operandNode)
{
    for (Node* node = operandNode; node;) {
        // currentNode is null when we're doing speculation checks for checkArgumentTypes().
    	if (!currentNode || node->origin.semantic != currentNode->origin.semantic) {
			CodeBlock* profiledBlock = baselineCodeBlockFor(node->origin.semantic);

			if (node->hasLocal(*this)) {
				ValueProfile* result = valueProfileFor(*this, profiledBlock, node);
				if (result)
					return MethodOfGettingAValueProfile(result);

				if (node->op() == GetLocal) {
					return MethodOfGettingAValueProfile::fromLazyOperand(
						profiledBlock,
						LazyOperandValueProfileKey(
								node->origin.semantic.bytecodeIndex, node->local()));
				}
			}

			if (node->hasHeapPrediction())
				return MethodOfGettingAValueProfile(profiledBlock->valueProfileForBytecodeOffset(node->origin.semantic.bytecodeIndex));
        }

        switch (node->op()) {
        case BooleanToNumber:
        case Identity:
        case ValueRep:
        case DoubleRep:
        case Int52Rep:
            node = node->child1().node();
            break;
        default:
            node = 0;
        }
    }

    return MethodOfGettingAValueProfile();
}

bool Graph::isStringPrototypeMethodSane(JSGlobalObject* globalObject, UniquedStringImpl* uid)
{
    ObjectPropertyConditionSet conditions = generateConditionsForPrototypeEquivalenceConcurrently(m_vm, globalObject, globalObject->stringObjectStructure(), globalObject->stringPrototype(), uid);

    if (!conditions.isValid())
        return false;

    ObjectPropertyCondition equivalenceCondition = conditions.slotBaseCondition();
    RELEASE_ASSERT(equivalenceCondition.hasRequiredValue());
    JSFunction* function = jsDynamicCast<JSFunction*>(equivalenceCondition.condition().requiredValue());
    if (!function)
        return false;

    if (function->executable()->intrinsicFor(CodeForCall) != StringPrototypeValueOfIntrinsic)
        return false;

    return watchConditions(conditions);
}

bool Graph::canOptimizeStringObjectAccess(const CodeOrigin& codeOrigin)
{
    if (hasExitSite(codeOrigin, BadCache) || hasExitSite(codeOrigin, BadWeakConstantCache))
        return false;

    JSGlobalObject* globalObject = globalObjectFor(codeOrigin);
    Structure* stringObjectStructure = globalObject->stringObjectStructure();
    registerStructure(stringObjectStructure);
    ASSERT(stringObjectStructure->storedPrototype().isObject());
    ASSERT(stringObjectStructure->storedPrototype().asCell()->classInfo() == StringPrototype::info());

    if (!watchConditions(generateConditionsForPropertyMissConcurrently(m_vm, globalObject, stringObjectStructure, m_vm.propertyNames->toPrimitiveSymbol.impl())))
        return false;

    // We're being conservative here. We want DFG's ToString on StringObject to be
    // used in both numeric contexts (that would call valueOf()) and string contexts
    // (that would call toString()). We don't want the DFG to have to distinguish
    // between the two, just because that seems like it would get confusing. So we
    // just require both methods to be sane.
    if (!isStringPrototypeMethodSane(globalObject, m_vm.propertyNames->valueOf.impl()))
		return false;
	return isStringPrototypeMethodSane(globalObject, m_vm.propertyNames->toString.impl());
}

StructureRegistrationResult Graph::registerStructure(Structure* structure)
{
    weakReferences.addLazily(structure);
    if (watchpoints().consider(structure))
        return StructureRegisteredAndWatched;
    return StructureRegisteredNormally;
}

void Graph::registerAndWatchStructureTransition(Structure* structure)
{
    weakReferences.addLazily(structure);
    watchpoints().addLazily(structure->transitionWatchpointSet());
}

void Graph::convertToConstant(Node* node, JSValue value)
{
    if (value.isCell())
    	assertIsRegistered(value.asCell()->structure());
    if (value.isObject())
        node->convertToWeakConstant(value.asCell());
    else
        convertToConstant(node, constantRegisterForConstant(value));
}

void Graph::convertToConstant(Node* node, JSCell* cell)
{
   	assertIsRegistered(cell->structure());
    if (cell->isObject())
        node->convertToWeakConstant(cell);
    else
        convertToConstant(node, constantRegisterForConstant(cell));
}

void Graph::convertToConstant(Node* node, JSString* string)
{
   	assertIsRegistered(string->structure());
    convertToConstant(node, constantRegisterForConstant(string));
}

void Graph::cleanMustHandleValuesIfNecessary()
{
    //LockHolder locker(mustHandleValueCleaningLock);

    if (!m_mustHandleValuesMayIncludeGarbage)
        return;

    m_mustHandleValuesMayIncludeGarbage = false;

    if (!m_codeBlock)
        return;

    if (!m_mustHandleValues.numberOfLocals())
        return;

    FastBitVector liveness = m_codeBlock->alternative()->livenessAnalysis().getLivenessInfoAtBytecodeOffset(m_osrEntryBytecodeIndex);

    for (unsigned local = m_mustHandleValues.numberOfLocals(); local--;) {
        if (!liveness.get(local))
            m_mustHandleValues.local(local) = jsUndefined();
    }
}

#if !ASSERT_DISABLED
void Graph::assertIsRegistered(Structure* structure)
{
    // It's convenient to be able to call this with a maybe-null structure.
    if (!structure)
        return;

    DFG_ASSERT(*this, 0, weakReferences.contains(structure));

    if (!structure->dfgShouldWatch())
        return;
    if (watchpoints().isWatched(structure->transitionWatchpointSet()))
        return;

    DFG_CRASH(*this, 0, toCString("Structure ", pointerDump(structure), " is watchable but isn't being watched.").data());
}
#endif // !ASSERT_DISABLED

void Graph::handleAssertionFailure(
    Node* node, const char* file, int line, const char* function, const char* assertion)
{
    startCrashing();
    dataLog("DFG ASSERTION FAILED: ", assertion, "\n");
    dataLog(file, "(", line, ") : ", function, "\n");
    dataLog("\n");
    if (node) {
        dataLog("While handling node ", node, "\n");
        dataLog("\n");
    }
    dataLog("Graph at time of failure:\n");
    dump();
    dataLog("\n");
    dataLog("DFG ASSERTION FAILED: ", assertion, "\n");
    dataLog(file, "(", line, ") : ", function, "\n");
    CRASH();
}

} } // namespace JSC::DFG

#endif // ENABLE(DFG_JIT)
