1/*
2 * Copyright (C) 2015-2017 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#pragma once
27
28#if ENABLE(B3_JIT)
29
30#include "CPU.h"
31#include "GPRInfo.h"
32#include "JSExportMacros.h"
33#include "Options.h"
34
35namespace JSC { namespace B3 {
36
37enum B3CompilationMode {
38 B3Mode,
39 AirMode
40};
41
42JS_EXPORT_PRIVATE bool shouldDumpIR(B3CompilationMode);
43bool shouldDumpIRAtEachPhase(B3CompilationMode);
44bool shouldValidateIR();
45bool shouldValidateIRAtEachPhase();
46bool shouldSaveIRBeforePhase();
47
48template<typename BitsType, typename InputType>
49inline bool isIdentical(InputType left, InputType right)
50{
51 BitsType leftBits = bitwise_cast<BitsType>(left);
52 BitsType rightBits = bitwise_cast<BitsType>(right);
53 return leftBits == rightBits;
54}
55
56inline bool isIdentical(int32_t left, int32_t right)
57{
58 return isIdentical<int32_t>(left, right);
59}
60
61inline bool isIdentical(int64_t left, int64_t right)
62{
63 return isIdentical<int64_t>(left, right);
64}
65
66inline bool isIdentical(double left, double right)
67{
68 return isIdentical<int64_t>(left, right);
69}
70
71inline bool isIdentical(float left, float right)
72{
73 return isIdentical<int32_t>(left, right);
74}
75
76template<typename ResultType, typename InputType, typename BitsType>
77inline bool isRepresentableAsImpl(InputType originalValue)
78{
79 // Convert the original value to the desired result type.
80 ResultType result = static_cast<ResultType>(originalValue);
81
82 // Convert the converted value back to the original type. The original value is representable
83 // using the new type if such round-tripping doesn't lose bits.
84 InputType newValue = static_cast<InputType>(result);
85
86 return isIdentical<BitsType>(originalValue, newValue);
87}
88
89template<typename ResultType>
90inline bool isRepresentableAs(int32_t value)
91{
92 return isRepresentableAsImpl<ResultType, int32_t, int32_t>(value);
93}
94
95template<typename ResultType>
96inline bool isRepresentableAs(int64_t value)
97{
98 return isRepresentableAsImpl<ResultType, int64_t, int64_t>(value);
99}
100
101template<typename ResultType>
102inline bool isRepresentableAs(size_t value)
103{
104 return isRepresentableAsImpl<ResultType, size_t, size_t>(value);
105}
106
107template<typename ResultType>
108inline bool isRepresentableAs(double value)
109{
110 return isRepresentableAsImpl<ResultType, double, int64_t>(value);
111}
112
113template<typename IntType>
114static IntType chillDiv(IntType numerator, IntType denominator)
115{
116 if (!denominator)
117 return 0;
118 if (denominator == -1 && numerator == std::numeric_limits<IntType>::min())
119 return std::numeric_limits<IntType>::min();
120 return numerator / denominator;
121}
122
123template<typename IntType>
124static IntType chillMod(IntType numerator, IntType denominator)
125{
126 if (!denominator)
127 return 0;
128 if (denominator == -1 && numerator == std::numeric_limits<IntType>::min())
129 return 0;
130 return numerator % denominator;
131}
132
133template<typename IntType>
134static IntType chillUDiv(IntType numerator, IntType denominator)
135{
136 typedef typename std::make_unsigned<IntType>::type UnsignedIntType;
137 UnsignedIntType unsignedNumerator = static_cast<UnsignedIntType>(numerator);
138 UnsignedIntType unsignedDenominator = static_cast<UnsignedIntType>(denominator);
139 if (!unsignedDenominator)
140 return 0;
141 return unsignedNumerator / unsignedDenominator;
142}
143
144template<typename IntType>
145static IntType chillUMod(IntType numerator, IntType denominator)
146{
147 typedef typename std::make_unsigned<IntType>::type UnsignedIntType;
148 UnsignedIntType unsignedNumerator = static_cast<UnsignedIntType>(numerator);
149 UnsignedIntType unsignedDenominator = static_cast<UnsignedIntType>(denominator);
150 if (!unsignedDenominator)
151 return 0;
152 return unsignedNumerator % unsignedDenominator;
153}
154
155template<typename IntType>
156static IntType rotateRight(IntType value, int32_t shift)
157{
158 typedef typename std::make_unsigned<IntType>::type UnsignedIntType;
159 UnsignedIntType uValue = static_cast<UnsignedIntType>(value);
160 int32_t bits = sizeof(IntType) * 8;
161 int32_t mask = bits - 1;
162 shift &= mask;
163 return (uValue >> shift) | (uValue << ((bits - shift) & mask));
164}
165
166template<typename IntType>
167static IntType rotateLeft(IntType value, int32_t shift)
168{
169 typedef typename std::make_unsigned<IntType>::type UnsignedIntType;
170 UnsignedIntType uValue = static_cast<UnsignedIntType>(value);
171 int32_t bits = sizeof(IntType) * 8;
172 int32_t mask = bits - 1;
173 shift &= mask;
174 return (uValue << shift) | (uValue >> ((bits - shift) & mask));
175}
176
177inline unsigned defaultOptLevel()
178{
179 // This should almost always return 2, but we allow this default to be lowered for testing. Some
180 // components will deliberately set the optLevel.
181 return Options::defaultB3OptLevel();
182}
183
184Optional<GPRReg> pinnedExtendedOffsetAddrRegister();
185
186} } // namespace JSC::B3
187
188#endif // ENABLE(B3_JIT)
189