2 * Copyright (C) 2011-2015 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 "DFGCSEPhase.h"
31 #include "DFGAbstractHeap.h"
32 #include "DFGBlockMapInlines.h"
33 #include "DFGClobberSet.h"
34 #include "DFGClobberize.h"
35 #include "DFGDominators.h"
36 #include "DFGEdgeUsesStructure.h"
39 #include "JSCInlines.h"
42 namespace JSC { namespace DFG {
44 // This file contains two CSE implementations: local and global. LocalCSE typically runs when we're
45 // in DFG mode, i.e. we want to compile quickly. LocalCSE contains a lot of optimizations for
46 // compile time. GlobalCSE, on the other hand, is fairly straight-forward. It will find more
47 // optimization opportunities by virtue of being global.
51 namespace DFGCSEPhaseInternal {
52 static const bool verbose = false;
55 class ImpureDataSlot {
56 WTF_MAKE_NONCOPYABLE(ImpureDataSlot);
57 WTF_MAKE_FAST_ALLOCATED;
59 ImpureDataSlot(HeapLocation key, LazyNode value, unsigned hash)
60 : key(key), value(value), hash(hash)
68 struct ImpureDataSlotHash : public DefaultHash<std::unique_ptr<ImpureDataSlot>>::Hash {
69 static unsigned hash(const std::unique_ptr<ImpureDataSlot>& key)
74 static bool equal(const std::unique_ptr<ImpureDataSlot>& a, const std::unique_ptr<ImpureDataSlot>& b)
76 // The ImpureDataSlot are unique per table per HeapLocation. This lets us compare the key
77 // by just comparing the pointers of the unique ImpureDataSlots.
78 ASSERT(a != b || a->key == b->key);
83 struct ImpureDataTranslator {
84 static unsigned hash(const HeapLocation& key)
89 static bool equal(const std::unique_ptr<ImpureDataSlot>& slot, const HeapLocation& key)
93 if (HashTraits<std::unique_ptr<ImpureDataSlot>>::isDeletedValue(slot))
95 return slot->key == key;
98 static void translate(std::unique_ptr<ImpureDataSlot>& slot, const HeapLocation& key, unsigned hashCode)
100 new (NotNull, std::addressof(slot)) std::unique_ptr<ImpureDataSlot>(new ImpureDataSlot {key, LazyNode(), hashCode});
105 WTF_MAKE_FAST_ALLOCATED;
106 WTF_MAKE_NONCOPYABLE(ImpureMap);
108 ImpureMap() = default;
110 ImpureMap(ImpureMap&& other)
112 m_abstractHeapStackMap.swap(other.m_abstractHeapStackMap);
113 m_fallbackStackMap.swap(other.m_fallbackStackMap);
114 m_heapMap.swap(other.m_heapMap);
116 m_debugImpureData.swap(other.m_debugImpureData);
120 const ImpureDataSlot* add(const HeapLocation& location, const LazyNode& node)
122 const ImpureDataSlot* result = addImpl(location, node);
125 auto addResult = m_debugImpureData.add(location, node);
126 ASSERT(!!result == !addResult.isNewEntry);
131 LazyNode get(const HeapLocation& location) const
133 LazyNode result = getImpl(location);
135 ASSERT(result == m_debugImpureData.get(location));
140 void clobber(AbstractHeap heap)
142 switch (heap.kind()) {
150 ASSERT(!heap.payload().isTop());
151 ASSERT(heap.payload().value() == heap.payload().value32());
152 m_abstractHeapStackMap.remove(heap.payload().value32());
153 clobber(m_fallbackStackMap, heap);
157 clobber(m_heapMap, heap);
161 m_debugImpureData.removeIf([heap](const HashMap<HeapLocation, LazyNode>::KeyValuePairType& pair) -> bool {
162 return heap.overlaps(pair.key.heap());
164 ASSERT(m_debugImpureData.size()
166 + m_abstractHeapStackMap.size()
167 + m_fallbackStackMap.size()));
169 const bool verifyClobber = false;
171 for (auto& pair : m_debugImpureData)
172 ASSERT(!!get(pair.key));
179 m_abstractHeapStackMap.clear();
180 m_fallbackStackMap.clear();
183 m_debugImpureData.clear();
188 typedef HashSet<std::unique_ptr<ImpureDataSlot>, ImpureDataSlotHash> Map;
190 const ImpureDataSlot* addImpl(const HeapLocation& location, const LazyNode& node)
192 switch (location.heap().kind()) {
195 RELEASE_ASSERT_NOT_REACHED();
197 AbstractHeap abstractHeap = location.heap();
198 if (abstractHeap.payload().isTop())
199 return add(m_fallbackStackMap, location, node);
200 ASSERT(abstractHeap.payload().value() == abstractHeap.payload().value32());
201 auto addResult = m_abstractHeapStackMap.add(abstractHeap.payload().value32(), nullptr);
202 if (addResult.isNewEntry) {
203 addResult.iterator->value.reset(new ImpureDataSlot {location, node, 0});
206 if (addResult.iterator->value->key == location)
207 return addResult.iterator->value.get();
208 return add(m_fallbackStackMap, location, node);
211 return add(m_heapMap, location, node);
216 LazyNode getImpl(const HeapLocation& location) const
218 switch (location.heap().kind()) {
221 RELEASE_ASSERT_NOT_REACHED();
223 ASSERT(location.heap().payload().value() == location.heap().payload().value32());
224 auto iterator = m_abstractHeapStackMap.find(location.heap().payload().value32());
225 if (iterator != m_abstractHeapStackMap.end()
226 && iterator->value->key == location)
227 return iterator->value->value;
228 return get(m_fallbackStackMap, location);
231 return get(m_heapMap, location);
236 static const ImpureDataSlot* add(Map& map, const HeapLocation& location, const LazyNode& node)
238 auto result = map.add<ImpureDataTranslator>(location);
239 if (result.isNewEntry) {
240 (*result.iterator)->value = node;
243 return result.iterator->get();
246 static LazyNode get(const Map& map, const HeapLocation& location)
248 auto iterator = map.find<ImpureDataTranslator>(location);
249 if (iterator != map.end())
250 return (*iterator)->value;
254 static void clobber(Map& map, AbstractHeap heap)
256 map.removeIf([heap](const std::unique_ptr<ImpureDataSlot>& slot) -> bool {
257 return heap.overlaps(slot->key.heap());
261 // The majority of Impure Stack Slotsare unique per value.
262 // This is very useful for fast clobber(), we can just remove the slot addressed by AbstractHeap
265 // When there are conflict, any additional HeapLocation is added in the fallback map.
266 // This works well because fallbackStackMap remains tiny.
268 // One cannot assume a unique ImpureData is in m_abstractHeapStackMap. It may have been
269 // a duplicate in the past and now only live in m_fallbackStackMap.
271 // Obviously, TOP always goes into m_fallbackStackMap since it does not have a unique value.
272 HashMap<int32_t, std::unique_ptr<ImpureDataSlot>, DefaultHash<int32_t>::Hash, WTF::SignedWithZeroKeyHashTraits<int32_t>> m_abstractHeapStackMap;
273 Map m_fallbackStackMap;
278 HashMap<HeapLocation, LazyNode> m_debugImpureData;
282 class LocalCSEPhase : public Phase {
284 LocalCSEPhase(Graph& graph)
285 : Phase(graph, "local common subexpression elimination")
286 , m_smallBlock(graph)
287 , m_largeBlock(graph)
293 ASSERT(m_graph.m_fixpointState == FixpointNotConverged);
294 ASSERT(m_graph.m_form == ThreadedCPS || m_graph.m_form == LoadStore);
296 bool changed = false;
298 m_graph.clearReplacements();
300 for (BlockIndex blockIndex = m_graph.numBlocks(); blockIndex--;) {
301 BasicBlock* block = m_graph.block(blockIndex);
305 if (block->size() <= SmallMaps::capacity)
306 changed |= m_smallBlock.run(block);
308 changed |= m_largeBlock.run(block);
317 // This permits SmallMaps to be used for blocks that have up to 100 nodes. In practice,
318 // fewer than half of the nodes in a block have pure defs, and even fewer have impure defs.
319 // Thus, a capacity limit of 100 probably means that somewhere around ~40 things may end up
320 // in one of these "small" list-based maps. That number still seems largeish, except that
321 // the overhead of HashMaps can be quite high currently: clearing them, or even removing
322 // enough things from them, deletes (or resizes) their backing store eagerly. Hence
323 // HashMaps induce a lot of malloc traffic.
324 static const unsigned capacity = 100;
338 void write(AbstractHeap heap)
340 if (heap.kind() == SideState)
343 for (unsigned i = 0; i < m_impureLength; ++i) {
344 if (heap.overlaps(m_impureMap[i].key.heap()))
345 m_impureMap[i--] = m_impureMap[--m_impureLength];
349 Node* addPure(PureValue value, Node* node)
351 for (unsigned i = m_pureLength; i--;) {
352 if (m_pureMap[i].key == value)
353 return m_pureMap[i].value;
356 ASSERT(m_pureLength < capacity);
357 m_pureMap[m_pureLength++] = WTF::KeyValuePair<PureValue, Node*>(value, node);
361 LazyNode findReplacement(HeapLocation location)
363 for (unsigned i = m_impureLength; i--;) {
364 if (m_impureMap[i].key == location)
365 return m_impureMap[i].value;
370 LazyNode addImpure(HeapLocation location, LazyNode node)
372 // FIXME: If we are using small maps, we must not def() derived values.
373 // For now the only derived values we def() are constant-based.
374 if (location.index() && !location.index().isNode())
376 if (LazyNode result = findReplacement(location))
378 ASSERT(m_impureLength < capacity);
379 m_impureMap[m_impureLength++] = WTF::KeyValuePair<HeapLocation, LazyNode>(location, node);
384 WTF::KeyValuePair<PureValue, Node*> m_pureMap[capacity];
385 WTF::KeyValuePair<HeapLocation, LazyNode> m_impureMap[capacity];
386 unsigned m_pureLength;
387 unsigned m_impureLength;
402 void write(AbstractHeap heap)
404 m_impureMap.clobber(heap);
407 Node* addPure(PureValue value, Node* node)
409 auto result = m_pureMap.add(value, node);
410 if (result.isNewEntry)
412 return result.iterator->value;
415 LazyNode findReplacement(HeapLocation location)
417 return m_impureMap.get(location);
420 LazyNode addImpure(const HeapLocation& location, const LazyNode& node)
422 if (const ImpureDataSlot* slot = m_impureMap.add(location, node))
428 HashMap<PureValue, Node*> m_pureMap;
429 ImpureMap m_impureMap;
432 template<typename Maps>
435 BlockCSE(Graph& graph)
437 , m_insertionSet(graph)
441 bool run(BasicBlock* block)
447 for (unsigned nodeIndex = 0; nodeIndex < block->size(); ++nodeIndex) {
448 m_node = block->at(nodeIndex);
449 m_graph.performSubstitution(m_node);
451 if (m_node->op() == Identity || m_node->op() == IdentityWithProfile) {
452 m_node->replaceWith(m_node->child1().node());
455 // This rule only makes sense for local CSE, since in SSA form we have already
456 // factored the bounds check out of the PutByVal. It's kind of gross, but we
457 // still have reason to believe that PutByValAlias is a good optimization and
458 // that it's better to do it with a single node rather than separating out the
460 if (m_node->op() == PutByVal || m_node->op() == PutByValDirect) {
463 Node* base = m_graph.varArgChild(m_node, 0).node();
464 Node* index = m_graph.varArgChild(m_node, 1).node();
465 LocationKind indexedPropertyLoc = indexedPropertyLocForResultType(m_node->result());
467 ArrayMode mode = m_node->arrayMode();
468 switch (mode.type()) {
470 if (!mode.isInBounds())
473 indexedPropertyLoc, IndexedInt32Properties, base, index);
477 if (!mode.isInBounds())
480 indexedPropertyLoc, IndexedDoubleProperties, base, index);
483 case Array::Contiguous:
484 if (!mode.isInBounds())
487 indexedPropertyLoc, IndexedContiguousProperties, base, index);
490 case Array::Int8Array:
491 case Array::Int16Array:
492 case Array::Int32Array:
493 case Array::Uint8Array:
494 case Array::Uint8ClampedArray:
495 case Array::Uint16Array:
496 case Array::Uint32Array:
497 case Array::Float32Array:
498 case Array::Float64Array:
499 if (!mode.isInBounds())
502 indexedPropertyLoc, TypedArrayProperties, base, index);
509 if (!!heap && m_maps.findReplacement(heap))
510 m_node->setOp(PutByValAlias);
513 clobberize(m_graph, m_node, *this);
517 m_insertionSet.execute(block);
522 void read(AbstractHeap) { }
524 void write(AbstractHeap heap)
529 void def(PureValue value)
531 Node* match = m_maps.addPure(value, m_node);
535 m_node->replaceWith(match);
539 void def(const HeapLocation& location, const LazyNode& value)
541 LazyNode match = m_maps.addImpure(location, value);
545 if (m_node->op() == GetLocal) {
546 // Usually the CPS rethreading phase does this. But it's OK for us to mess with
547 // locals so long as:
549 // - We dethread the graph. Any changes we make may invalidate the assumptions of
550 // our CPS form, particularly if this GetLocal is linked to the variablesAtTail.
552 // - We don't introduce a Phantom for the child of the GetLocal. This wouldn't be
553 // totally wrong but it would pessimize the code. Just because there is a
554 // GetLocal doesn't mean that the child was live. Simply rerouting the all uses
555 // of this GetLocal will preserve the live-at-exit information just fine.
557 // We accomplish the latter by just clearing the child; then the Phantom that we
558 // introduce won't have children and so it will eventually just be deleted.
560 m_node->child1() = Edge();
564 if (value.isNode() && value.asNode() == m_node) {
565 match.ensureIsNode(m_insertionSet, m_block, 0)->owner = m_block;
566 ASSERT(match.isNode());
567 m_node->replaceWith(match.asNode());
581 InsertionSet m_insertionSet;
584 BlockCSE<SmallMaps> m_smallBlock;
585 BlockCSE<LargeMaps> m_largeBlock;
588 class GlobalCSEPhase : public Phase {
590 GlobalCSEPhase(Graph& graph)
591 : Phase(graph, "global common subexpression elimination")
592 , m_impureDataMap(graph)
593 , m_insertionSet(graph)
599 ASSERT(m_graph.m_fixpointState == FixpointNotConverged);
600 ASSERT(m_graph.m_form == SSA);
602 m_graph.initializeNodeOwners();
603 m_graph.ensureSSADominators();
605 m_preOrder = m_graph.blocksInPreOrder();
607 // First figure out what gets clobbered by blocks. Node that this uses the preOrder list
608 // for convenience only.
609 for (unsigned i = m_preOrder.size(); i--;) {
610 m_block = m_preOrder[i];
611 m_impureData = &m_impureDataMap[m_block];
612 for (unsigned nodeIndex = m_block->size(); nodeIndex--;)
613 addWrites(m_graph, m_block->at(nodeIndex), m_impureData->writes);
616 // Based on my experience doing this before, what follows might have to be made iterative.
617 // Right now it doesn't have to be iterative because everything is dominator-bsed. But when
618 // validation is enabled, we check if iterating would find new CSE opportunities.
620 bool changed = iterate();
622 // FIXME: It should be possible to assert that CSE will not find any new opportunities if you
623 // run it a second time. Unfortunately, we cannot assert this right now. Note that if we did
624 // this, we'd have to first reset all of our state.
625 // https://bugs.webkit.org/show_bug.cgi?id=145853
632 if (DFGCSEPhaseInternal::verbose)
633 dataLog("Performing iteration.\n");
636 m_graph.clearReplacements();
638 for (unsigned i = 0; i < m_preOrder.size(); ++i) {
639 m_block = m_preOrder[i];
640 m_impureData = &m_impureDataMap[m_block];
641 m_writesSoFar.clear();
643 if (DFGCSEPhaseInternal::verbose)
644 dataLog("Processing block ", *m_block, ":\n");
646 for (unsigned nodeIndex = 0; nodeIndex < m_block->size(); ++nodeIndex) {
647 m_nodeIndex = nodeIndex;
648 m_node = m_block->at(nodeIndex);
649 if (DFGCSEPhaseInternal::verbose)
650 dataLog(" Looking at node ", m_node, ":\n");
652 m_graph.performSubstitution(m_node);
654 if (m_node->op() == Identity || m_node->op() == IdentityWithProfile) {
655 m_node->replaceWith(m_node->child1().node());
658 clobberize(m_graph, m_node, *this);
661 m_insertionSet.execute(m_block);
663 m_impureData->didVisit = true;
669 void read(AbstractHeap) { }
671 void write(AbstractHeap heap)
673 m_impureData->availableAtTail.clobber(heap);
674 m_writesSoFar.add(heap);
677 void def(PureValue value)
679 // With pure values we do not have to worry about the possibility of some control flow path
680 // clobbering the value. So, we just search for all of the like values that have been
681 // computed. We pick one that is in a block that dominates ours. Note that this means that
682 // a PureValue will map to a list of nodes, since there may be many places in the control
683 // flow graph that compute a value but only one of them that dominates us. We may build up
684 // a large list of nodes that compute some value in the case of gnarly control flow. This
687 auto result = m_pureValues.add(value, Vector<Node*>());
688 if (result.isNewEntry) {
689 result.iterator->value.append(m_node);
693 for (unsigned i = result.iterator->value.size(); i--;) {
694 Node* candidate = result.iterator->value[i];
695 if (m_graph.m_ssaDominators->dominates(candidate->owner, m_block)) {
696 m_node->replaceWith(candidate);
702 result.iterator->value.append(m_node);
705 LazyNode findReplacement(HeapLocation location)
707 // At this instant, our "availableAtTail" reflects the set of things that are available in
708 // this block so far. We check this map to find block-local CSE opportunities before doing
710 LazyNode match = m_impureData->availableAtTail.get(location);
712 if (DFGCSEPhaseInternal::verbose)
713 dataLog(" Found local match: ", match, "\n");
717 // If it's not available at this point in the block, and at some prior point in the block
718 // we have clobbered this heap location, then there is no point in doing a global search.
719 if (m_writesSoFar.overlaps(location.heap())) {
720 if (DFGCSEPhaseInternal::verbose)
721 dataLog(" Not looking globally because of local clobber: ", m_writesSoFar, "\n");
725 // This perfoms a backward search over the control flow graph to find some possible
726 // non-local def() that matches our heap location. Such a match is only valid if there does
727 // not exist any path from that def() to our block that contains a write() that overlaps
728 // our heap. This algorithm looks for both of these things (the matching def and the
729 // overlapping writes) in one backwards DFS pass.
731 // This starts by looking at the starting block's predecessors, and then it continues along
732 // their predecessors. As soon as this finds a possible def() - one that defines the heap
733 // location we want while dominating our starting block - it assumes that this one must be
734 // the match. It then lets the DFS over predecessors complete, but it doesn't add the
735 // def()'s predecessors; this ensures that any blocks we visit thereafter are on some path
736 // from the def() to us. As soon as the DFG finds a write() that overlaps the location's
737 // heap, it stops, assuming that there is no possible match. Note that the write() case may
738 // trigger before we find a def(), or after. Either way, the write() case causes this
739 // function to immediately return nullptr.
741 // If the write() is found before we find the def(), then we know that any def() we would
742 // find would have a path to us that trips over the write() and hence becomes invalid. This
743 // is just a direct outcome of us looking for a def() that dominates us. Given a block A
744 // that dominates block B - so that A is the one that would have the def() and B is our
745 // starting block - we know that any other block must either be on the path from A to B, or
746 // it must be on a path from the root to A, but not both. So, if we haven't found A yet but
747 // we already have found a block C that has a write(), then C must be on some path from A
748 // to B, which means that A's def() is invalid for our purposes. Hence, before we find the
749 // def(), stopping on write() is the right thing to do.
751 // Stopping on write() is also the right thing to do after we find the def(). After we find
752 // the def(), we don't add that block's predecessors to the search worklist. That means
753 // that henceforth the only blocks we will see in the search are blocks on the path from
754 // the def() to us. If any such block has a write() that clobbers our heap then we should
757 // Hence this graph search algorithm ends up being deceptively simple: any overlapping
758 // write() causes us to immediately return nullptr, and a matching def() means that we just
759 // record it and neglect to visit its precessors.
761 Vector<BasicBlock*, 8> worklist;
762 Vector<BasicBlock*, 8> seenList;
765 for (unsigned i = m_block->predecessors.size(); i--;) {
766 BasicBlock* predecessor = m_block->predecessors[i];
767 if (!seen.get(predecessor->index)) {
768 worklist.append(predecessor);
769 seen.set(predecessor->index);
773 while (!worklist.isEmpty()) {
774 BasicBlock* block = worklist.takeLast();
775 seenList.append(block);
777 if (DFGCSEPhaseInternal::verbose)
778 dataLog(" Searching in block ", *block, "\n");
779 ImpureBlockData& data = m_impureDataMap[block];
781 // We require strict domination because this would only see things in our own block if
782 // they came *after* our position in the block. Clearly, while our block dominates
783 // itself, the things in the block after us don't dominate us.
784 if (m_graph.m_ssaDominators->strictlyDominates(block, m_block)) {
785 if (DFGCSEPhaseInternal::verbose)
786 dataLog(" It strictly dominates.\n");
787 DFG_ASSERT(m_graph, m_node, data.didVisit);
788 DFG_ASSERT(m_graph, m_node, !match);
789 match = data.availableAtTail.get(location);
790 if (DFGCSEPhaseInternal::verbose)
791 dataLog(" Availability: ", match, "\n");
793 // Don't examine the predecessors of a match. At this point we just want to
794 // establish that other blocks on the path from here to there don't clobber
795 // the location we're interested in.
800 if (DFGCSEPhaseInternal::verbose)
801 dataLog(" Dealing with write set ", data.writes, "\n");
802 if (data.writes.overlaps(location.heap())) {
803 if (DFGCSEPhaseInternal::verbose)
804 dataLog(" Clobbered.\n");
808 for (unsigned i = block->predecessors.size(); i--;) {
809 BasicBlock* predecessor = block->predecessors[i];
810 if (!seen.get(predecessor->index)) {
811 worklist.append(predecessor);
812 seen.set(predecessor->index);
820 // Cache the results for next time. We cache them both for this block and for all of our
821 // predecessors, since even though we've already visited our predecessors, our predecessors
822 // probably have successors other than us.
823 // FIXME: Consider caching failed searches as well, when match is null. It's not clear that
824 // the reduction in compile time would warrant the increase in complexity, though.
825 // https://bugs.webkit.org/show_bug.cgi?id=134876
826 for (BasicBlock* block : seenList)
827 m_impureDataMap[block].availableAtTail.add(location, match);
828 m_impureData->availableAtTail.add(location, match);
833 void def(HeapLocation location, LazyNode value)
835 if (DFGCSEPhaseInternal::verbose)
836 dataLog(" Got heap location def: ", location, " -> ", value, "\n");
838 LazyNode match = findReplacement(location);
840 if (DFGCSEPhaseInternal::verbose)
841 dataLog(" Got match: ", match, "\n");
844 if (DFGCSEPhaseInternal::verbose)
845 dataLog(" Adding at-tail mapping: ", location, " -> ", value, "\n");
846 auto result = m_impureData->availableAtTail.add(location, value);
847 ASSERT_UNUSED(result, !result);
851 if (value.isNode() && value.asNode() == m_node) {
852 if (!match.isNode()) {
853 // We need to properly record the constant in order to use an existing one if applicable.
854 // This ensures that re-running GCSE will not find new optimizations.
855 match.ensureIsNode(m_insertionSet, m_block, m_nodeIndex)->owner = m_block;
856 auto result = m_pureValues.add(PureValue(match.asNode(), match->constant()), Vector<Node*>());
857 bool replaced = false;
858 if (!result.isNewEntry) {
859 for (unsigned i = result.iterator->value.size(); i--;) {
860 Node* candidate = result.iterator->value[i];
861 if (m_graph.m_ssaDominators->dominates(candidate->owner, m_block)) {
863 match->replaceWith(candidate);
864 match.setNode(candidate);
871 result.iterator->value.append(match.asNode());
873 ASSERT(match.asNode());
874 m_node->replaceWith(match.asNode());
879 struct ImpureBlockData {
886 ImpureMap availableAtTail;
890 Vector<BasicBlock*> m_preOrder;
892 PureMultiMap m_pureValues;
893 BlockMap<ImpureBlockData> m_impureDataMap;
897 unsigned m_nodeIndex;
898 ImpureBlockData* m_impureData;
899 ClobberSet m_writesSoFar;
900 InsertionSet m_insertionSet;
905 } // anonymous namespace
907 bool performLocalCSE(Graph& graph)
909 return runPhase<LocalCSEPhase>(graph);
912 bool performGlobalCSE(Graph& graph)
914 return runPhase<GlobalCSEPhase>(graph);
917 } } // namespace JSC::DFG
919 #endif // ENABLE(DFG_JIT)