1/*
2 * Copyright (C) 2013-2015 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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.
12 *
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.
24 */
25
26#include "config.h"
27#include "DFGBasicBlock.h"
28
29#if ENABLE(DFG_JIT)
30
31#include "JSCInlines.h"
32
33namespace JSC { namespace DFG {
34
35BasicBlock::BasicBlock(BytecodeIndex bytecodeBegin, unsigned numArguments, unsigned numLocals, float executionCount)
36 : bytecodeBegin(bytecodeBegin)
37 , index(NoBlock)
38 , cfaStructureClobberStateAtHead(StructuresAreWatched)
39 , cfaStructureClobberStateAtTail(StructuresAreWatched)
40 , cfaBranchDirection(InvalidBranchDirection)
41 , cfaHasVisited(false)
42 , cfaShouldRevisit(false)
43 , cfaDidFinish(true)
44 , intersectionOfCFAHasVisited(true)
45 , isOSRTarget(false)
46 , isCatchEntrypoint(false)
47#if !ASSERT_DISABLED
48 , isLinked(false)
49#endif
50 , isReachable(false)
51 , variablesAtHead(numArguments, numLocals)
52 , variablesAtTail(numArguments, numLocals)
53 , valuesAtHead(numArguments, numLocals)
54 , valuesAtTail(numArguments, numLocals)
55 , intersectionOfPastValuesAtHead(numArguments, numLocals, AbstractValue::fullTop())
56 , executionCount(executionCount)
57{
58}
59
60BasicBlock::~BasicBlock()
61{
62}
63
64void BasicBlock::ensureLocals(unsigned newNumLocals)
65{
66 variablesAtHead.ensureLocals(newNumLocals);
67 variablesAtTail.ensureLocals(newNumLocals);
68 valuesAtHead.ensureLocals(newNumLocals);
69 valuesAtTail.ensureLocals(newNumLocals);
70 intersectionOfPastValuesAtHead.ensureLocals(newNumLocals, AbstractValue::fullTop());
71}
72
73void BasicBlock::replaceTerminal(Graph& graph, Node* node)
74{
75 NodeAndIndex result = findTerminal();
76 if (!result)
77 append(node);
78 else {
79 m_nodes.insert(result.index + 1, node);
80 result.node->remove(graph);
81 }
82
83 ASSERT(terminal());
84}
85
86bool BasicBlock::isInPhis(Node* node) const
87{
88 for (size_t i = 0; i < phis.size(); ++i) {
89 if (phis[i] == node)
90 return true;
91 }
92 return false;
93}
94
95bool BasicBlock::isInBlock(Node* myNode) const
96{
97 for (size_t i = 0; i < numNodes(); ++i) {
98 if (node(i) == myNode)
99 return true;
100 }
101 return false;
102}
103
104void BasicBlock::removePredecessor(BasicBlock* block)
105{
106 for (unsigned i = 0; i < predecessors.size(); ++i) {
107 if (predecessors[i] != block)
108 continue;
109 predecessors[i] = predecessors.last();
110 predecessors.removeLast();
111 return;
112 }
113 RELEASE_ASSERT_NOT_REACHED();
114}
115
116void BasicBlock::replacePredecessor(BasicBlock* from, BasicBlock* to)
117{
118 for (unsigned i = predecessors.size(); i--;) {
119 if (predecessors[i] != from)
120 continue;
121 predecessors[i] = to;
122 return;
123 }
124 RELEASE_ASSERT_NOT_REACHED();
125}
126
127void BasicBlock::dump(PrintStream& out) const
128{
129 out.print("#", index);
130}
131
132BasicBlock::SSAData::SSAData(BasicBlock* block)
133{
134 availabilityAtHead.m_locals = Operands<Availability>(OperandsLike, block->variablesAtHead);
135 availabilityAtTail.m_locals = Operands<Availability>(OperandsLike, block->variablesAtHead);
136}
137
138BasicBlock::SSAData::~SSAData() { }
139
140} } // namespace JSC::DFG
141
142#endif // ENABLE(DFG_JIT)
143
144