]>
git.saurik.com Git - apple/javascriptcore.git/blob - dfg/DFGBinarySwitch.cpp
2 * Copyright (C) 2013 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 "DFGBinarySwitch.h"
31 #include "JSCInlines.h"
33 namespace JSC
{ namespace DFG
{
35 BinarySwitch::BinarySwitch(GPRReg value
, const Vector
<int64_t>& cases
, Type type
)
38 , m_caseIndex(UINT_MAX
)
45 for (unsigned i
= 0; i
< cases
.size(); ++i
)
46 m_cases
.append(Case(cases
[i
], i
));
47 std::sort(m_cases
.begin(), m_cases
.end());
48 build(0, m_cases
.size());
51 bool BinarySwitch::advance(MacroAssembler
& jit
)
53 if (m_cases
.isEmpty()) {
54 m_fallThrough
.append(jit
.jump());
58 if (m_index
== m_branches
.size()) {
59 RELEASE_ASSERT(m_jumpStack
.isEmpty());
64 const BranchCode
& code
= m_branches
[m_index
++];
66 case NotEqualToFallThrough
:
69 m_fallThrough
.append(jit
.branch32(
70 MacroAssembler::NotEqual
, m_value
,
71 MacroAssembler::Imm32(static_cast<int32_t>(m_cases
[code
.index
].value
))));
74 m_fallThrough
.append(jit
.branchPtr(
75 MacroAssembler::NotEqual
, m_value
,
76 MacroAssembler::ImmPtr(bitwise_cast
<const void*>(static_cast<intptr_t>(m_cases
[code
.index
].value
)))));
83 m_jumpStack
.append(jit
.branch32(
84 MacroAssembler::NotEqual
, m_value
,
85 MacroAssembler::Imm32(static_cast<int32_t>(m_cases
[code
.index
].value
))));
88 m_jumpStack
.append(jit
.branchPtr(
89 MacroAssembler::NotEqual
, m_value
,
90 MacroAssembler::ImmPtr(bitwise_cast
<const void*>(static_cast<intptr_t>(m_cases
[code
.index
].value
)))));
97 m_jumpStack
.append(jit
.branch32(
98 MacroAssembler::LessThan
, m_value
,
99 MacroAssembler::Imm32(static_cast<int32_t>(m_cases
[code
.index
].value
))));
102 m_jumpStack
.append(jit
.branchPtr(
103 MacroAssembler::LessThan
, m_value
,
104 MacroAssembler::ImmPtr(bitwise_cast
<const void*>(static_cast<intptr_t>(m_cases
[code
.index
].value
)))));
109 m_jumpStack
.takeLast().link(&jit
);
112 m_caseIndex
= code
.index
;
118 void BinarySwitch::build(unsigned start
, unsigned end
)
120 unsigned size
= end
- start
;
124 RELEASE_ASSERT_NOT_REACHED();
130 && m_cases
[start
- 1].value
== m_cases
[start
].value
- 1
131 && start
+ 1 < m_cases
.size()
132 && m_cases
[start
+ 1].value
== m_cases
[start
].value
+ 1) {
133 m_branches
.append(BranchCode(ExecuteCase
, start
));
137 m_branches
.append(BranchCode(NotEqualToFallThrough
, start
));
138 m_branches
.append(BranchCode(ExecuteCase
, start
));
143 if (m_cases
[start
].value
+ 1 == m_cases
[start
+ 1].value
145 && m_cases
[start
- 1].value
== m_cases
[start
].value
- 1
146 && start
+ 2 < m_cases
.size()
147 && m_cases
[start
+ 2].value
== m_cases
[start
+ 1].value
+ 1) {
148 m_branches
.append(BranchCode(NotEqualToPush
, start
));
149 m_branches
.append(BranchCode(ExecuteCase
, start
));
150 m_branches
.append(BranchCode(Pop
));
151 m_branches
.append(BranchCode(ExecuteCase
, start
+ 1));
155 unsigned firstCase
= start
;
156 unsigned secondCase
= start
+ 1;
158 std::swap(firstCase
, secondCase
);
161 m_branches
.append(BranchCode(NotEqualToPush
, firstCase
));
162 m_branches
.append(BranchCode(ExecuteCase
, firstCase
));
163 m_branches
.append(BranchCode(Pop
));
164 m_branches
.append(BranchCode(NotEqualToFallThrough
, secondCase
));
165 m_branches
.append(BranchCode(ExecuteCase
, secondCase
));
170 unsigned medianIndex
= (start
+ end
) / 2;
172 // Because end is exclusive, in the even case, this rounds up by
173 // default. Hence median bias sometimes flips to subtracing one
174 // in order to get round-down behavior.
175 medianIndex
-= m_medianBias
;
179 RELEASE_ASSERT(medianIndex
> start
);
180 RELEASE_ASSERT(medianIndex
+ 1 < end
);
182 m_branches
.append(BranchCode(LessThanToPush
, medianIndex
));
183 m_branches
.append(BranchCode(NotEqualToPush
, medianIndex
));
184 m_branches
.append(BranchCode(ExecuteCase
, medianIndex
));
186 m_branches
.append(BranchCode(Pop
));
187 build(medianIndex
+ 1, end
);
189 m_branches
.append(BranchCode(Pop
));
190 build(start
, medianIndex
);
195 } } // namespace JSC::DFG
197 #endif // ENABLE(DFG_JIT)