2 * Copyright (C) 2011, 2012 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "DFGDriver.h"
35 #include "DFGArgumentsSimplificationPhase.h"
36 #include "DFGByteCodeParser.h"
37 #include "DFGCFAPhase.h"
38 #include "DFGCFGSimplificationPhase.h"
39 #include "DFGCSEPhase.h"
40 #include "DFGConstantFoldingPhase.h"
41 #include "DFGFixupPhase.h"
42 #include "DFGJITCompiler.h"
43 #include "DFGPredictionPropagationPhase.h"
44 #include "DFGStructureCheckHoistingPhase.h"
45 #include "DFGValidate.h"
46 #include "DFGVirtualRegisterAllocationPhase.h"
49 namespace JSC { namespace DFG {
51 static unsigned numCompilations;
53 unsigned getNumCompilations()
55 return numCompilations;
58 enum CompileMode { CompileFunction, CompileOther };
59 inline bool compile(CompileMode compileMode, ExecState* exec, CodeBlock* codeBlock, JITCode& jitCode, MacroAssemblerCodePtr* jitCodeWithArityCheck, unsigned osrEntryBytecodeIndex)
61 SamplingRegion samplingRegion("DFG Compilation (Driver)");
66 ASSERT(codeBlock->alternative());
67 ASSERT(codeBlock->alternative()->getJITType() == JITCode::BaselineJIT);
69 ASSERT(osrEntryBytecodeIndex != UINT_MAX);
71 if (!Options::useDFGJIT())
74 #if DFG_ENABLE(DEBUG_VERBOSE)
75 dataLogF("DFG compiling code block %p(%p) for executable %p, number of instructions = %u.\n", codeBlock, codeBlock->alternative(), codeBlock->ownerExecutable(), codeBlock->instructionCount());
78 // Derive our set of must-handle values. The compilation must be at least conservative
79 // enough to allow for OSR entry with these values.
80 unsigned numVarsWithValues;
81 if (osrEntryBytecodeIndex)
82 numVarsWithValues = codeBlock->m_numVars;
84 numVarsWithValues = 0;
85 Operands<JSValue> mustHandleValues(codeBlock->numParameters(), numVarsWithValues);
86 for (size_t i = 0; i < mustHandleValues.size(); ++i) {
87 int operand = mustHandleValues.operandForIndex(i);
88 if (operandIsArgument(operand)
89 && !operandToArgument(operand)
90 && compileMode == CompileFunction
91 && codeBlock->specializationKind() == CodeForConstruct) {
92 // Ugh. If we're in a constructor, the 'this' argument may hold garbage. It will
93 // also never be used. It doesn't matter what we put into the value for this,
94 // but it has to be an actual value that can be grokked by subsequent DFG passes,
95 // so we sanitize it here by turning it into Undefined.
96 mustHandleValues[i] = jsUndefined();
98 mustHandleValues[i] = exec->uncheckedR(operand).jsValue();
101 Graph dfg(exec->globalData(), codeBlock, osrEntryBytecodeIndex, mustHandleValues);
102 if (!parse(exec, dfg))
105 if (compileMode == CompileFunction)
106 dfg.predictArgumentTypes();
108 // By this point the DFG bytecode parser will have potentially mutated various tables
109 // in the CodeBlock. This is a good time to perform an early shrink, which is more
110 // powerful than a late one. It's safe to do so because we haven't generated any code
111 // that references any of the tables directly, yet.
112 codeBlock->shrinkToFit(CodeBlock::EarlyShrink);
115 performPredictionPropagation(dfg);
117 performStructureCheckHoisting(dfg);
119 dfg.m_fixpointState = FixpointNotConverged;
121 #if DFG_ENABLE(DEBUG_VERBOSE)
122 dataLogF("DFG beginning optimization fixpoint iteration #%u.\n", cnt);
124 bool changed = false;
126 changed |= performConstantFolding(dfg);
127 changed |= performArgumentsSimplification(dfg);
128 changed |= performCFGSimplification(dfg);
129 changed |= performCSE(dfg);
132 dfg.resetExitStates();
135 dfg.m_fixpointState = FixpointConverged;
137 #if DFG_ENABLE(DEBUG_VERBOSE)
138 dataLogF("DFG optimization fixpoint converged in %u iterations.\n", cnt);
140 performVirtualRegisterAllocation(dfg);
142 GraphDumpMode modeForFinalValidate = DumpGraph;
143 #if DFG_ENABLE(DEBUG_VERBOSE)
144 dataLogF("Graph after optimization:\n");
146 modeForFinalValidate = DontDumpGraph;
148 validate(dfg, modeForFinalValidate);
150 JITCompiler dataFlowJIT(dfg);
152 if (compileMode == CompileFunction) {
153 ASSERT(jitCodeWithArityCheck);
155 result = dataFlowJIT.compileFunction(jitCode, *jitCodeWithArityCheck);
157 ASSERT(compileMode == CompileOther);
158 ASSERT(!jitCodeWithArityCheck);
160 result = dataFlowJIT.compile(jitCode);
166 bool tryCompile(ExecState* exec, CodeBlock* codeBlock, JITCode& jitCode, unsigned bytecodeIndex)
168 return compile(CompileOther, exec, codeBlock, jitCode, 0, bytecodeIndex);
171 bool tryCompileFunction(ExecState* exec, CodeBlock* codeBlock, JITCode& jitCode, MacroAssemblerCodePtr& jitCodeWithArityCheck, unsigned bytecodeIndex)
173 return compile(CompileFunction, exec, codeBlock, jitCode, &jitCodeWithArityCheck, bytecodeIndex);
176 } } // namespace JSC::DFG
178 #endif // ENABLE(DFG_JIT)