| 1 | /* |
| 2 | * Copyright (C) 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 | #include "config.h" |
| 27 | #include "B3HoistLoopInvariantValues.h" |
| 28 | |
| 29 | #if ENABLE(B3_JIT) |
| 30 | |
| 31 | #include "B3BackwardsDominators.h" |
| 32 | #include "B3BasicBlockInlines.h" |
| 33 | #include "B3Dominators.h" |
| 34 | #include "B3EnsureLoopPreHeaders.h" |
| 35 | #include "B3NaturalLoops.h" |
| 36 | #include "B3PhaseScope.h" |
| 37 | #include "B3ProcedureInlines.h" |
| 38 | #include "B3ValueInlines.h" |
| 39 | #include <wtf/RangeSet.h> |
| 40 | |
| 41 | namespace JSC { namespace B3 { |
| 42 | |
| 43 | bool hoistLoopInvariantValues(Procedure& proc) |
| 44 | { |
| 45 | PhaseScope phaseScope(proc, "hoistLoopInvariantValues" ); |
| 46 | |
| 47 | ensureLoopPreHeaders(proc); |
| 48 | |
| 49 | NaturalLoops& loops = proc.naturalLoops(); |
| 50 | if (!loops.numLoops()) |
| 51 | return false; |
| 52 | |
| 53 | proc.resetValueOwners(); |
| 54 | Dominators& dominators = proc.dominators(); |
| 55 | BackwardsDominators& backwardsDominators = proc.backwardsDominators(); |
| 56 | |
| 57 | // FIXME: We should have a reusable B3::EffectsSet data structure. |
| 58 | // https://bugs.webkit.org/show_bug.cgi?id=174762 |
| 59 | struct LoopData { |
| 60 | RangeSet<HeapRange> writes; |
| 61 | bool writesLocalState { false }; |
| 62 | bool writesPinned { false }; |
| 63 | bool sideExits { false }; |
| 64 | BasicBlock* { nullptr }; |
| 65 | }; |
| 66 | |
| 67 | IndexMap<NaturalLoop, LoopData> data(loops.numLoops()); |
| 68 | |
| 69 | for (unsigned loopIndex = loops.numLoops(); loopIndex--;) { |
| 70 | const NaturalLoop& loop = loops.loop(loopIndex); |
| 71 | for (BasicBlock* predecessor : loop.header()->predecessors()) { |
| 72 | if (loops.belongsTo(predecessor, loop)) |
| 73 | continue; |
| 74 | RELEASE_ASSERT(!data[loop].preHeader); |
| 75 | data[loop].preHeader = predecessor; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | for (BasicBlock* block : proc) { |
| 80 | const NaturalLoop* loop = loops.innerMostLoopOf(block); |
| 81 | if (!loop) |
| 82 | continue; |
| 83 | for (Value* value : *block) { |
| 84 | Effects effects = value->effects(); |
| 85 | data[*loop].writes.add(effects.writes); |
| 86 | data[*loop].writesLocalState |= effects.writesLocalState; |
| 87 | data[*loop].writesPinned |= effects.writesPinned; |
| 88 | data[*loop].sideExits |= effects.exitsSideways; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | for (unsigned loopIndex = loops.numLoops(); loopIndex--;) { |
| 93 | const NaturalLoop& loop = loops.loop(loopIndex); |
| 94 | for (const NaturalLoop* current = loops.innerMostOuterLoop(loop); current; current = loops.innerMostOuterLoop(*current)) { |
| 95 | data[*current].writes.addAll(data[loop].writes); |
| 96 | data[*current].writesLocalState |= data[loop].writesLocalState; |
| 97 | data[*current].writesPinned |= data[loop].writesPinned; |
| 98 | data[*current].sideExits |= data[loop].sideExits; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | bool changed = false; |
| 103 | |
| 104 | // Pre-order ensures that we visit our dominators before we visit ourselves. Otherwise we'd miss some |
| 105 | // hoisting opportunities in complex CFGs. |
| 106 | for (BasicBlock* block : proc.blocksInPreOrder()) { |
| 107 | Vector<const NaturalLoop*> blockLoops = loops.loopsOf(block); |
| 108 | if (blockLoops.isEmpty()) |
| 109 | continue; |
| 110 | |
| 111 | for (Value*& value : *block) { |
| 112 | Effects effects = value->effects(); |
| 113 | |
| 114 | // We never hoist write effects or control constructs. |
| 115 | if (effects.mustExecute()) |
| 116 | continue; |
| 117 | |
| 118 | // Try outermost loop first. |
| 119 | for (unsigned i = blockLoops.size(); i--;) { |
| 120 | const NaturalLoop& loop = *blockLoops[i]; |
| 121 | |
| 122 | bool ok = true; |
| 123 | for (Value* child : value->children()) { |
| 124 | if (!dominators.dominates(child->owner, data[loop].preHeader)) { |
| 125 | ok = false; |
| 126 | break; |
| 127 | } |
| 128 | } |
| 129 | if (!ok) |
| 130 | continue; |
| 131 | |
| 132 | if (effects.controlDependent) { |
| 133 | if (!backwardsDominators.dominates(block, data[loop].preHeader)) |
| 134 | continue; |
| 135 | |
| 136 | // FIXME: This is super conservative. In reality, we just need to make sure that there |
| 137 | // aren't any side exits between here and the pre-header according to backwards search. |
| 138 | // https://bugs.webkit.org/show_bug.cgi?id=174763 |
| 139 | if (data[loop].sideExits) |
| 140 | continue; |
| 141 | } |
| 142 | |
| 143 | if (effects.readsPinned && data[loop].writesPinned) |
| 144 | continue; |
| 145 | |
| 146 | if (effects.readsLocalState && data[loop].writesLocalState) |
| 147 | continue; |
| 148 | |
| 149 | if (data[loop].writes.overlaps(effects.reads)) |
| 150 | continue; |
| 151 | |
| 152 | data[loop].preHeader->appendNonTerminal(value); |
| 153 | value = proc.add<Value>(Nop, Void, value->origin()); |
| 154 | changed = true; |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | return changed; |
| 160 | } |
| 161 | |
| 162 | } } // namespace JSC::B3 |
| 163 | |
| 164 | #endif // ENABLE(B3_JIT) |
| 165 | |
| 166 | |