commit-queue@webkit.org | 9fad495 | 2015-12-12 06:10:07 +0000 | [diff] [blame] | 1 | /* |
mark.lam@apple.com | e668c9e | 2020-04-09 09:27:40 +0000 | [diff] [blame] | 2 | * Copyright (C) 2015-2020 Apple Inc. All rights reserved. |
commit-queue@webkit.org | 9fad495 | 2015-12-12 06:10:07 +0000 | [diff] [blame] | 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 "B3LowerMacrosAfterOptimizations.h" |
| 28 | |
| 29 | #if ENABLE(B3_JIT) |
| 30 | |
keith_miller@apple.com | 504d585 | 2017-09-13 01:31:07 +0000 | [diff] [blame] | 31 | #include "AirArg.h" |
commit-queue@webkit.org | 9fad495 | 2015-12-12 06:10:07 +0000 | [diff] [blame] | 32 | #include "B3BlockInsertionSet.h" |
commit-queue@webkit.org | 868c973 | 2015-12-14 22:44:22 +0000 | [diff] [blame] | 33 | #include "B3CCallValue.h" |
commit-queue@webkit.org | 9fad495 | 2015-12-12 06:10:07 +0000 | [diff] [blame] | 34 | #include "B3ConstDoubleValue.h" |
| 35 | #include "B3ConstFloatValue.h" |
commit-queue@webkit.org | 868c973 | 2015-12-14 22:44:22 +0000 | [diff] [blame] | 36 | #include "B3ConstPtrValue.h" |
commit-queue@webkit.org | 9fad495 | 2015-12-12 06:10:07 +0000 | [diff] [blame] | 37 | #include "B3InsertionSetInlines.h" |
| 38 | #include "B3PhaseScope.h" |
pmatos@igalia.com | b78530b | 2020-05-23 14:57:15 +0000 | [diff] [blame^] | 39 | #include "B3ValueInlines.h" |
commit-queue@webkit.org | 9fad495 | 2015-12-12 06:10:07 +0000 | [diff] [blame] | 40 | |
| 41 | namespace JSC { namespace B3 { |
| 42 | |
keith_miller@apple.com | 504d585 | 2017-09-13 01:31:07 +0000 | [diff] [blame] | 43 | using Arg = Air::Arg; |
| 44 | using Code = Air::Code; |
| 45 | using Tmp = Air::Tmp; |
| 46 | |
commit-queue@webkit.org | 9fad495 | 2015-12-12 06:10:07 +0000 | [diff] [blame] | 47 | namespace { |
| 48 | |
keith_miller@apple.com | 504d585 | 2017-09-13 01:31:07 +0000 | [diff] [blame] | 49 | class LowerMacrosAfterOptimizations { |
commit-queue@webkit.org | 9fad495 | 2015-12-12 06:10:07 +0000 | [diff] [blame] | 50 | public: |
keith_miller@apple.com | 504d585 | 2017-09-13 01:31:07 +0000 | [diff] [blame] | 51 | LowerMacrosAfterOptimizations(Procedure& proc) |
commit-queue@webkit.org | 9fad495 | 2015-12-12 06:10:07 +0000 | [diff] [blame] | 52 | : m_proc(proc) |
| 53 | , m_blockInsertionSet(proc) |
| 54 | , m_insertionSet(proc) |
| 55 | { |
| 56 | } |
| 57 | |
| 58 | bool run() |
| 59 | { |
| 60 | for (BasicBlock* block : m_proc) { |
| 61 | m_block = block; |
| 62 | processCurrentBlock(); |
| 63 | } |
| 64 | m_changed |= m_blockInsertionSet.execute(); |
| 65 | if (m_changed) { |
| 66 | m_proc.resetReachability(); |
| 67 | m_proc.invalidateCFG(); |
| 68 | } |
| 69 | return m_changed; |
| 70 | } |
| 71 | |
| 72 | private: |
| 73 | void processCurrentBlock() |
| 74 | { |
| 75 | for (m_index = 0; m_index < m_block->size(); ++m_index) { |
| 76 | m_value = m_block->at(m_index); |
| 77 | m_origin = m_value->origin(); |
| 78 | switch (m_value->opcode()) { |
| 79 | case Abs: { |
| 80 | // ARM supports this instruction natively. |
| 81 | if (isARM64()) |
| 82 | break; |
| 83 | |
| 84 | Value* mask = nullptr; |
| 85 | if (m_value->type() == Double) |
peavo@outlook.com | 76ed6aa | 2016-01-19 21:49:12 +0000 | [diff] [blame] | 86 | mask = m_insertionSet.insert<ConstDoubleValue>(m_index, m_origin, bitwise_cast<double>(~(1ll << 63))); |
commit-queue@webkit.org | 9fad495 | 2015-12-12 06:10:07 +0000 | [diff] [blame] | 87 | else if (m_value->type() == Float) |
| 88 | mask = m_insertionSet.insert<ConstFloatValue>(m_index, m_origin, bitwise_cast<float>(~(1 << 31))); |
| 89 | else |
| 90 | RELEASE_ASSERT_NOT_REACHED(); |
| 91 | Value* result = m_insertionSet.insert<Value>(m_index, BitAnd, m_origin, m_value->child(0), mask); |
| 92 | m_value->replaceWithIdentity(result); |
| 93 | break; |
| 94 | } |
commit-queue@webkit.org | 868c973 | 2015-12-14 22:44:22 +0000 | [diff] [blame] | 95 | case Ceil: { |
utatane.tea@gmail.com | 9971c63 | 2016-03-01 02:30:46 +0000 | [diff] [blame] | 96 | if (MacroAssembler::supportsFloatingPointRounding()) |
commit-queue@webkit.org | 868c973 | 2015-12-14 22:44:22 +0000 | [diff] [blame] | 97 | break; |
| 98 | |
| 99 | Value* functionAddress = nullptr; |
utatane.tea@gmail.com | eedf188 | 2016-03-17 13:57:03 +0000 | [diff] [blame] | 100 | if (m_value->type() == Double) { |
| 101 | double (*ceilDouble)(double) = ceil; |
mark.lam@apple.com | e668c9e | 2020-04-09 09:27:40 +0000 | [diff] [blame] | 102 | functionAddress = m_insertionSet.insert<ConstPtrValue>(m_index, m_origin, tagCFunction<B3CCallPtrTag>(ceilDouble)); |
utatane.tea@gmail.com | eedf188 | 2016-03-17 13:57:03 +0000 | [diff] [blame] | 103 | } else if (m_value->type() == Float) |
mark.lam@apple.com | e668c9e | 2020-04-09 09:27:40 +0000 | [diff] [blame] | 104 | functionAddress = m_insertionSet.insert<ConstPtrValue>(m_index, m_origin, tagCFunction<B3CCallPtrTag>(ceilf)); |
commit-queue@webkit.org | 868c973 | 2015-12-14 22:44:22 +0000 | [diff] [blame] | 105 | else |
| 106 | RELEASE_ASSERT_NOT_REACHED(); |
| 107 | |
| 108 | Value* result = m_insertionSet.insert<CCallValue>(m_index, |
| 109 | m_value->type(), |
| 110 | m_origin, |
| 111 | Effects::none(), |
| 112 | functionAddress, |
| 113 | m_value->child(0)); |
| 114 | m_value->replaceWithIdentity(result); |
| 115 | break; |
| 116 | } |
utatane.tea@gmail.com | 9971c63 | 2016-03-01 02:30:46 +0000 | [diff] [blame] | 117 | case Floor: { |
| 118 | if (MacroAssembler::supportsFloatingPointRounding()) |
| 119 | break; |
| 120 | |
| 121 | Value* functionAddress = nullptr; |
utatane.tea@gmail.com | eedf188 | 2016-03-17 13:57:03 +0000 | [diff] [blame] | 122 | if (m_value->type() == Double) { |
| 123 | double (*floorDouble)(double) = floor; |
mark.lam@apple.com | e668c9e | 2020-04-09 09:27:40 +0000 | [diff] [blame] | 124 | functionAddress = m_insertionSet.insert<ConstPtrValue>(m_index, m_origin, tagCFunction<B3CCallPtrTag>(floorDouble)); |
utatane.tea@gmail.com | eedf188 | 2016-03-17 13:57:03 +0000 | [diff] [blame] | 125 | } else if (m_value->type() == Float) |
mark.lam@apple.com | e668c9e | 2020-04-09 09:27:40 +0000 | [diff] [blame] | 126 | functionAddress = m_insertionSet.insert<ConstPtrValue>(m_index, m_origin, tagCFunction<B3CCallPtrTag>(floorf)); |
utatane.tea@gmail.com | 9971c63 | 2016-03-01 02:30:46 +0000 | [diff] [blame] | 127 | else |
| 128 | RELEASE_ASSERT_NOT_REACHED(); |
| 129 | |
| 130 | Value* result = m_insertionSet.insert<CCallValue>(m_index, |
| 131 | m_value->type(), |
| 132 | m_origin, |
| 133 | Effects::none(), |
| 134 | functionAddress, |
| 135 | m_value->child(0)); |
| 136 | m_value->replaceWithIdentity(result); |
| 137 | break; |
| 138 | } |
fpizlo@apple.com | 46d3504 | 2016-01-09 00:01:55 +0000 | [diff] [blame] | 139 | case Neg: { |
keith_miller@apple.com | 70c51b8 | 2019-08-02 21:02:05 +0000 | [diff] [blame] | 140 | if (!m_value->type().isFloat()) |
fpizlo@apple.com | 46d3504 | 2016-01-09 00:01:55 +0000 | [diff] [blame] | 141 | break; |
| 142 | |
| 143 | // X86 is odd in that it requires this. |
| 144 | if (!isX86()) |
| 145 | break; |
| 146 | |
| 147 | Value* mask = nullptr; |
| 148 | if (m_value->type() == Double) |
| 149 | mask = m_insertionSet.insert<ConstDoubleValue>(m_index, m_origin, -0.0); |
| 150 | else { |
| 151 | RELEASE_ASSERT(m_value->type() == Float); |
| 152 | mask = m_insertionSet.insert<ConstFloatValue>(m_index, m_origin, -0.0f); |
| 153 | } |
| 154 | |
| 155 | Value* result = m_insertionSet.insert<Value>( |
| 156 | m_index, BitXor, m_origin, m_value->child(0), mask); |
| 157 | m_value->replaceWithIdentity(result); |
| 158 | break; |
| 159 | } |
keith_miller@apple.com | 49cfb9a | 2016-11-17 19:21:42 +0000 | [diff] [blame] | 160 | |
| 161 | case RotL: { |
| 162 | // ARM64 doesn't have a rotate left. |
| 163 | if (isARM64()) { |
fpizlo@apple.com | 42624fe | 2017-03-10 17:49:42 +0000 | [diff] [blame] | 164 | Value* newShift = m_insertionSet.insert<Value>(m_index, Neg, m_value->origin(), m_value->child(1)); |
| 165 | Value* rotate = m_insertionSet.insert<Value>(m_index, RotR, m_value->origin(), m_value->child(0), newShift); |
| 166 | m_value->replaceWithIdentity(rotate); |
| 167 | break; |
keith_miller@apple.com | 49cfb9a | 2016-11-17 19:21:42 +0000 | [diff] [blame] | 168 | } |
| 169 | break; |
| 170 | } |
fpizlo@apple.com | 42624fe | 2017-03-10 17:49:42 +0000 | [diff] [blame] | 171 | |
commit-queue@webkit.org | 9fad495 | 2015-12-12 06:10:07 +0000 | [diff] [blame] | 172 | default: |
| 173 | break; |
| 174 | } |
| 175 | } |
| 176 | m_insertionSet.execute(m_block); |
| 177 | } |
| 178 | |
| 179 | Procedure& m_proc; |
| 180 | BlockInsertionSet m_blockInsertionSet; |
| 181 | InsertionSet m_insertionSet; |
| 182 | BasicBlock* m_block; |
| 183 | unsigned m_index; |
| 184 | Value* m_value; |
| 185 | Origin m_origin; |
| 186 | bool m_changed { false }; |
| 187 | }; |
| 188 | |
| 189 | bool lowerMacrosImpl(Procedure& proc) |
| 190 | { |
keith_miller@apple.com | 504d585 | 2017-09-13 01:31:07 +0000 | [diff] [blame] | 191 | LowerMacrosAfterOptimizations lowerMacros(proc); |
commit-queue@webkit.org | 9fad495 | 2015-12-12 06:10:07 +0000 | [diff] [blame] | 192 | return lowerMacros.run(); |
| 193 | } |
| 194 | |
| 195 | } // anonymous namespace |
| 196 | |
| 197 | bool lowerMacrosAfterOptimizations(Procedure& proc) |
| 198 | { |
| 199 | PhaseScope phaseScope(proc, "lowerMacrosAfterOptimizations"); |
| 200 | bool result = lowerMacrosImpl(proc); |
| 201 | if (shouldValidateIR()) |
| 202 | RELEASE_ASSERT(!lowerMacrosImpl(proc)); |
| 203 | return result; |
| 204 | } |
| 205 | |
| 206 | } } // namespace JSC::B3 |
| 207 | |
| 208 | #endif // ENABLE(B3_JIT) |
| 209 | |