| 1 | /* |
| 2 | * Copyright (C) 2015-2016 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 "B3BasicBlock.h" |
| 28 | |
| 29 | #if ENABLE(B3_JIT) |
| 30 | |
| 31 | #include "B3BasicBlockInlines.h" |
| 32 | #include "B3BasicBlockUtils.h" |
| 33 | #include "B3Procedure.h" |
| 34 | #include "B3ValueInlines.h" |
| 35 | #include <wtf/ListDump.h> |
| 36 | |
| 37 | namespace JSC { namespace B3 { |
| 38 | |
| 39 | const char* const BasicBlock::dumpPrefix = "#" ; |
| 40 | |
| 41 | BasicBlock::BasicBlock(unsigned index, double frequency) |
| 42 | : m_index(index) |
| 43 | , m_frequency(frequency) |
| 44 | { |
| 45 | } |
| 46 | |
| 47 | BasicBlock::~BasicBlock() |
| 48 | { |
| 49 | } |
| 50 | |
| 51 | void BasicBlock::append(Value* value) |
| 52 | { |
| 53 | m_values.append(value); |
| 54 | value->owner = this; |
| 55 | } |
| 56 | |
| 57 | void BasicBlock::appendNonTerminal(Value* value) |
| 58 | { |
| 59 | m_values.append(m_values.last()); |
| 60 | m_values[m_values.size() - 2] = value; |
| 61 | value->owner = this; |
| 62 | } |
| 63 | |
| 64 | void BasicBlock::removeLast(Procedure& proc) |
| 65 | { |
| 66 | ASSERT(!m_values.isEmpty()); |
| 67 | proc.deleteValue(m_values.takeLast()); |
| 68 | } |
| 69 | |
| 70 | void BasicBlock::replaceLast(Procedure& proc, Value* value) |
| 71 | { |
| 72 | removeLast(proc); |
| 73 | append(value); |
| 74 | } |
| 75 | |
| 76 | Value* BasicBlock::appendIntConstant(Procedure& proc, Origin origin, Type type, int64_t value) |
| 77 | { |
| 78 | Value* result = proc.addIntConstant(origin, type, value); |
| 79 | append(result); |
| 80 | return result; |
| 81 | } |
| 82 | |
| 83 | Value* BasicBlock::appendIntConstant(Procedure& proc, Value* likeValue, int64_t value) |
| 84 | { |
| 85 | return appendIntConstant(proc, likeValue->origin(), likeValue->type(), value); |
| 86 | } |
| 87 | |
| 88 | Value* BasicBlock::appendBoolConstant(Procedure& proc, Origin origin, bool value) |
| 89 | { |
| 90 | return appendIntConstant(proc, origin, Int32, value ? 1 : 0); |
| 91 | } |
| 92 | |
| 93 | void BasicBlock::clearSuccessors() |
| 94 | { |
| 95 | m_successors.clear(); |
| 96 | } |
| 97 | |
| 98 | void BasicBlock::appendSuccessor(FrequentedBlock target) |
| 99 | { |
| 100 | m_successors.append(target); |
| 101 | } |
| 102 | |
| 103 | void BasicBlock::setSuccessors(FrequentedBlock target) |
| 104 | { |
| 105 | m_successors.resize(1); |
| 106 | m_successors[0] = target; |
| 107 | } |
| 108 | |
| 109 | void BasicBlock::setSuccessors(FrequentedBlock taken, FrequentedBlock notTaken) |
| 110 | { |
| 111 | m_successors.resize(2); |
| 112 | m_successors[0] = taken; |
| 113 | m_successors[1] = notTaken; |
| 114 | } |
| 115 | |
| 116 | bool BasicBlock::replaceSuccessor(BasicBlock* from, BasicBlock* to) |
| 117 | { |
| 118 | bool result = false; |
| 119 | for (BasicBlock*& successor : successorBlocks()) { |
| 120 | if (successor == from) { |
| 121 | successor = to; |
| 122 | result = true; |
| 123 | |
| 124 | // Keep looping because a successor may be mentioned multiple times, like in a Switch. |
| 125 | } |
| 126 | } |
| 127 | return result; |
| 128 | } |
| 129 | |
| 130 | bool BasicBlock::addPredecessor(BasicBlock* block) |
| 131 | { |
| 132 | return B3::addPredecessor(this, block); |
| 133 | } |
| 134 | |
| 135 | bool BasicBlock::removePredecessor(BasicBlock* block) |
| 136 | { |
| 137 | return B3::removePredecessor(this, block); |
| 138 | } |
| 139 | |
| 140 | bool BasicBlock::replacePredecessor(BasicBlock* from, BasicBlock* to) |
| 141 | { |
| 142 | return B3::replacePredecessor(this, from, to); |
| 143 | } |
| 144 | |
| 145 | void BasicBlock::updatePredecessorsAfter() |
| 146 | { |
| 147 | B3::updatePredecessorsAfter(this); |
| 148 | } |
| 149 | |
| 150 | void BasicBlock::dump(PrintStream& out) const |
| 151 | { |
| 152 | out.print(dumpPrefix, m_index); |
| 153 | } |
| 154 | |
| 155 | void BasicBlock::deepDump(const Procedure& proc, PrintStream& out) const |
| 156 | { |
| 157 | out.print("BB" , *this, ": ; frequency = " , m_frequency, "\n" ); |
| 158 | if (predecessors().size()) |
| 159 | out.print(" Predecessors: " , pointerListDump(predecessors()), "\n" ); |
| 160 | for (Value* value : *this) |
| 161 | out.print(" " , B3::deepDump(proc, value), "\n" ); |
| 162 | if (!successors().isEmpty()) { |
| 163 | out.print(" Successors: " ); |
| 164 | if (size()) |
| 165 | last()->dumpSuccessors(this, out); |
| 166 | else |
| 167 | out.print(listDump(successors())); |
| 168 | out.print("\n" ); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | Value* BasicBlock::appendNewControlValue(Procedure& proc, Opcode opcode, Origin origin) |
| 173 | { |
| 174 | RELEASE_ASSERT(opcode == Oops || opcode == Return); |
| 175 | clearSuccessors(); |
| 176 | return appendNew<Value>(proc, opcode, origin); |
| 177 | } |
| 178 | |
| 179 | Value* BasicBlock::appendNewControlValue(Procedure& proc, Opcode opcode, Origin origin, Value* value) |
| 180 | { |
| 181 | RELEASE_ASSERT(opcode == Return); |
| 182 | clearSuccessors(); |
| 183 | return appendNew<Value>(proc, opcode, origin, value); |
| 184 | } |
| 185 | |
| 186 | Value* BasicBlock::appendNewControlValue(Procedure& proc, Opcode opcode, Origin origin, const FrequentedBlock& target) |
| 187 | { |
| 188 | RELEASE_ASSERT(opcode == Jump); |
| 189 | setSuccessors(target); |
| 190 | return appendNew<Value>(proc, opcode, origin); |
| 191 | } |
| 192 | |
| 193 | Value* BasicBlock::appendNewControlValue(Procedure& proc, Opcode opcode, Origin origin, Value* predicate, const FrequentedBlock& taken, const FrequentedBlock& notTaken) |
| 194 | { |
| 195 | RELEASE_ASSERT(opcode == Branch); |
| 196 | setSuccessors(taken, notTaken); |
| 197 | return appendNew<Value>(proc, opcode, origin, predicate); |
| 198 | } |
| 199 | |
| 200 | } } // namespace JSC::B3 |
| 201 | |
| 202 | #endif // ENABLE(B3_JIT) |
| 203 | |