blob: 66224e43381ac75c944bbf18691509341513edce [file] [log] [blame]
jkummerow@chromium.orgc1184022013-05-28 16:58:15 +00001// Copyright 2013 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#ifndef V8_HYDROGEN_GVN_H_
29#define V8_HYDROGEN_GVN_H_
30
31#include "hydrogen.h"
32#include "hydrogen-instructions.h"
33#include "compiler.h"
34#include "zone.h"
35
36namespace v8 {
37namespace internal {
38
39// Simple sparse set with O(1) add, contains, and clear.
40class SparseSet {
41 public:
42 SparseSet(Zone* zone, int capacity)
43 : capacity_(capacity),
44 length_(0),
45 dense_(zone->NewArray<int>(capacity)),
46 sparse_(zone->NewArray<int>(capacity)) {
47#ifndef NVALGRIND
48 // Initialize the sparse array to make valgrind happy.
49 memset(sparse_, 0, sizeof(sparse_[0]) * capacity);
50#endif
51 }
52
53 bool Contains(int n) const {
54 ASSERT(0 <= n && n < capacity_);
55 int d = sparse_[n];
56 return 0 <= d && d < length_ && dense_[d] == n;
57 }
58
59 bool Add(int n) {
60 if (Contains(n)) return false;
61 dense_[length_] = n;
62 sparse_[n] = length_;
63 ++length_;
64 return true;
65 }
66
67 void Clear() { length_ = 0; }
68
69 private:
70 int capacity_;
71 int length_;
72 int* dense_;
73 int* sparse_;
74
75 DISALLOW_COPY_AND_ASSIGN(SparseSet);
76};
77
78
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +000079// Perform common subexpression elimination and loop-invariant code motion.
80class HGlobalValueNumberingPhase : public HPhase {
jkummerow@chromium.orgc1184022013-05-28 16:58:15 +000081 public:
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +000082 explicit HGlobalValueNumberingPhase(HGraph* graph);
jkummerow@chromium.orgc1184022013-05-28 16:58:15 +000083
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +000084 void Run() {
85 Analyze();
86 // Trigger a second analysis pass to further eliminate duplicate values
87 // that could only be discovered by removing side-effect-generating
88 // instructions during the first pass.
89 if (FLAG_smi_only_arrays && removed_side_effects_) {
90 Analyze();
91 ASSERT(!removed_side_effects_);
92 }
93 }
jkummerow@chromium.orgc1184022013-05-28 16:58:15 +000094
95 private:
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +000096 void Analyze();
jkummerow@chromium.orgc1184022013-05-28 16:58:15 +000097 GVNFlagSet CollectSideEffectsOnPathsToDominatedBlock(
98 HBasicBlock* dominator,
99 HBasicBlock* dominated);
100 void AnalyzeGraph();
101 void ComputeBlockSideEffects();
102 void LoopInvariantCodeMotion();
103 void ProcessLoopBlock(HBasicBlock* block,
104 HBasicBlock* before_loop,
105 GVNFlagSet loop_kills,
106 GVNFlagSet* accumulated_first_time_depends,
107 GVNFlagSet* accumulated_first_time_changes);
108 bool AllowCodeMotion();
109 bool ShouldMove(HInstruction* instr, HBasicBlock* loop_header);
110
jkummerow@chromium.orgc1184022013-05-28 16:58:15 +0000111 bool removed_side_effects_;
112
113 // A map of block IDs to their side effects.
114 ZoneList<GVNFlagSet> block_side_effects_;
115
116 // A map of loop header block IDs to their loop's side effects.
117 ZoneList<GVNFlagSet> loop_side_effects_;
118
119 // Used when collecting side effects on paths from dominator to
120 // dominated.
121 SparseSet visited_on_paths_;
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +0000122
123 DISALLOW_COPY_AND_ASSIGN(HGlobalValueNumberingPhase);
jkummerow@chromium.orgc1184022013-05-28 16:58:15 +0000124};
125
126
127} } // namespace v8::internal
128
129#endif // V8_HYDROGEN_GVN_H_