Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "gvn.h" |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 18 | |
Mathieu Chartier | e5d80f8 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 19 | #include "base/arena_bit_vector.h" |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 20 | #include "base/arena_containers.h" |
| 21 | #include "base/bit_vector-inl.h" |
Nicolas Geoffray | 827eedb | 2015-01-26 15:18:36 +0000 | [diff] [blame] | 22 | #include "side_effects_analysis.h" |
David Brazdil | 6d340c4 | 2015-03-03 11:54:54 +0000 | [diff] [blame] | 23 | #include "utils.h" |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 24 | |
| 25 | namespace art { |
| 26 | |
Nicolas Geoffray | 827eedb | 2015-01-26 15:18:36 +0000 | [diff] [blame] | 27 | /** |
Nicolas Geoffray | 827eedb | 2015-01-26 15:18:36 +0000 | [diff] [blame] | 28 | * A ValueSet holds instructions that can replace other instructions. It is updated |
| 29 | * through the `Add` method, and the `Kill` method. The `Kill` method removes |
| 30 | * instructions that are affected by the given side effect. |
| 31 | * |
| 32 | * The `Lookup` method returns an equivalent instruction to the given instruction |
| 33 | * if there is one in the set. In GVN, we would say those instructions have the |
| 34 | * same "number". |
| 35 | */ |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 36 | class ValueSet : public ArenaObject<kArenaAllocGvn> { |
Nicolas Geoffray | 827eedb | 2015-01-26 15:18:36 +0000 | [diff] [blame] | 37 | public: |
David Brazdil | 6d340c4 | 2015-03-03 11:54:54 +0000 | [diff] [blame] | 38 | // Constructs an empty ValueSet which owns all its buckets. |
Nicolas Geoffray | 827eedb | 2015-01-26 15:18:36 +0000 | [diff] [blame] | 39 | explicit ValueSet(ArenaAllocator* allocator) |
David Brazdil | 6d340c4 | 2015-03-03 11:54:54 +0000 | [diff] [blame] | 40 | : allocator_(allocator), |
| 41 | num_buckets_(kMinimumNumberOfBuckets), |
Vladimir Marko | 5233f93 | 2015-09-29 19:01:15 +0100 | [diff] [blame] | 42 | buckets_(allocator->AllocArray<Node*>(num_buckets_, kArenaAllocGvn)), |
Vladimir Marko | f6a35de | 2016-03-21 12:01:50 +0000 | [diff] [blame^] | 43 | buckets_owned_(allocator, num_buckets_, false, kArenaAllocGvn), |
David Brazdil | 6d340c4 | 2015-03-03 11:54:54 +0000 | [diff] [blame] | 44 | num_entries_(0) { |
| 45 | // ArenaAllocator returns zeroed memory, so no need to set buckets to null. |
| 46 | DCHECK(IsPowerOfTwo(num_buckets_)); |
| 47 | buckets_owned_.SetInitialBits(num_buckets_); |
| 48 | } |
| 49 | |
| 50 | // Copy constructor. Depending on the load factor, it will either make a deep |
| 51 | // copy (all buckets owned) or a shallow one (buckets pointing to the parent). |
| 52 | ValueSet(ArenaAllocator* allocator, const ValueSet& to_copy) |
| 53 | : allocator_(allocator), |
| 54 | num_buckets_(to_copy.IdealBucketCount()), |
Vladimir Marko | 5233f93 | 2015-09-29 19:01:15 +0100 | [diff] [blame] | 55 | buckets_(allocator->AllocArray<Node*>(num_buckets_, kArenaAllocGvn)), |
Vladimir Marko | f6a35de | 2016-03-21 12:01:50 +0000 | [diff] [blame^] | 56 | buckets_owned_(allocator, num_buckets_, false, kArenaAllocGvn), |
David Brazdil | 6d340c4 | 2015-03-03 11:54:54 +0000 | [diff] [blame] | 57 | num_entries_(to_copy.num_entries_) { |
| 58 | // ArenaAllocator returns zeroed memory, so entries of buckets_ and |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 59 | // buckets_owned_ are initialized to null and false, respectively. |
David Brazdil | 6d340c4 | 2015-03-03 11:54:54 +0000 | [diff] [blame] | 60 | DCHECK(IsPowerOfTwo(num_buckets_)); |
| 61 | if (num_buckets_ == to_copy.num_buckets_) { |
| 62 | // Hash table remains the same size. We copy the bucket pointers and leave |
| 63 | // all buckets_owned_ bits false. |
| 64 | memcpy(buckets_, to_copy.buckets_, num_buckets_ * sizeof(Node*)); |
| 65 | } else { |
| 66 | // Hash table size changes. We copy and rehash all entries, and set all |
| 67 | // buckets_owned_ bits to true. |
| 68 | for (size_t i = 0; i < to_copy.num_buckets_; ++i) { |
| 69 | for (Node* node = to_copy.buckets_[i]; node != nullptr; node = node->GetNext()) { |
| 70 | size_t new_index = BucketIndex(node->GetHashCode()); |
| 71 | buckets_[new_index] = node->Dup(allocator_, buckets_[new_index]); |
| 72 | } |
| 73 | } |
| 74 | buckets_owned_.SetInitialBits(num_buckets_); |
Nicolas Geoffray | 827eedb | 2015-01-26 15:18:36 +0000 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
| 78 | // Adds an instruction in the set. |
| 79 | void Add(HInstruction* instruction) { |
| 80 | DCHECK(Lookup(instruction) == nullptr); |
David Brazdil | 6d340c4 | 2015-03-03 11:54:54 +0000 | [diff] [blame] | 81 | size_t hash_code = HashCode(instruction); |
| 82 | size_t index = BucketIndex(hash_code); |
| 83 | |
| 84 | if (!buckets_owned_.IsBitSet(index)) { |
| 85 | CloneBucket(index); |
Nicolas Geoffray | 827eedb | 2015-01-26 15:18:36 +0000 | [diff] [blame] | 86 | } |
David Brazdil | 6d340c4 | 2015-03-03 11:54:54 +0000 | [diff] [blame] | 87 | buckets_[index] = new (allocator_) Node(instruction, hash_code, buckets_[index]); |
| 88 | ++num_entries_; |
Nicolas Geoffray | 827eedb | 2015-01-26 15:18:36 +0000 | [diff] [blame] | 89 | } |
| 90 | |
David Brazdil | 6d340c4 | 2015-03-03 11:54:54 +0000 | [diff] [blame] | 91 | // If in the set, returns an equivalent instruction to the given instruction. |
| 92 | // Returns null otherwise. |
Nicolas Geoffray | 827eedb | 2015-01-26 15:18:36 +0000 | [diff] [blame] | 93 | HInstruction* Lookup(HInstruction* instruction) const { |
David Brazdil | 6d340c4 | 2015-03-03 11:54:54 +0000 | [diff] [blame] | 94 | size_t hash_code = HashCode(instruction); |
| 95 | size_t index = BucketIndex(hash_code); |
Nicolas Geoffray | 827eedb | 2015-01-26 15:18:36 +0000 | [diff] [blame] | 96 | |
David Brazdil | 6d340c4 | 2015-03-03 11:54:54 +0000 | [diff] [blame] | 97 | for (Node* node = buckets_[index]; node != nullptr; node = node->GetNext()) { |
Nicolas Geoffray | 827eedb | 2015-01-26 15:18:36 +0000 | [diff] [blame] | 98 | if (node->GetHashCode() == hash_code) { |
David Brazdil | 6d340c4 | 2015-03-03 11:54:54 +0000 | [diff] [blame] | 99 | HInstruction* existing = node->GetInstruction(); |
Nicolas Geoffray | 827eedb | 2015-01-26 15:18:36 +0000 | [diff] [blame] | 100 | if (existing->Equals(instruction)) { |
| 101 | return existing; |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | return nullptr; |
| 106 | } |
| 107 | |
David Brazdil | 6d340c4 | 2015-03-03 11:54:54 +0000 | [diff] [blame] | 108 | // Returns whether instruction is in the set. |
| 109 | bool Contains(HInstruction* instruction) const { |
| 110 | size_t hash_code = HashCode(instruction); |
| 111 | size_t index = BucketIndex(hash_code); |
Nicolas Geoffray | 827eedb | 2015-01-26 15:18:36 +0000 | [diff] [blame] | 112 | |
David Brazdil | 6d340c4 | 2015-03-03 11:54:54 +0000 | [diff] [blame] | 113 | for (Node* node = buckets_[index]; node != nullptr; node = node->GetNext()) { |
| 114 | if (node->GetInstruction() == instruction) { |
| 115 | return true; |
Nicolas Geoffray | 827eedb | 2015-01-26 15:18:36 +0000 | [diff] [blame] | 116 | } |
| 117 | } |
David Brazdil | 6d340c4 | 2015-03-03 11:54:54 +0000 | [diff] [blame] | 118 | return false; |
Nicolas Geoffray | 827eedb | 2015-01-26 15:18:36 +0000 | [diff] [blame] | 119 | } |
| 120 | |
David Brazdil | 6d340c4 | 2015-03-03 11:54:54 +0000 | [diff] [blame] | 121 | // Removes all instructions in the set affected by the given side effects. |
Nicolas Geoffray | 827eedb | 2015-01-26 15:18:36 +0000 | [diff] [blame] | 122 | void Kill(SideEffects side_effects) { |
David Brazdil | 6d340c4 | 2015-03-03 11:54:54 +0000 | [diff] [blame] | 123 | DeleteAllImpureWhich([side_effects](Node* node) { |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 124 | return node->GetInstruction()->GetSideEffects().MayDependOn(side_effects); |
David Brazdil | 6d340c4 | 2015-03-03 11:54:54 +0000 | [diff] [blame] | 125 | }); |
| 126 | } |
Nicolas Geoffray | 827eedb | 2015-01-26 15:18:36 +0000 | [diff] [blame] | 127 | |
Nicolas Geoffray | 15bd228 | 2016-01-05 15:55:41 +0000 | [diff] [blame] | 128 | void Clear() { |
| 129 | num_entries_ = 0; |
| 130 | for (size_t i = 0; i < num_buckets_; ++i) { |
| 131 | buckets_[i] = nullptr; |
| 132 | } |
| 133 | buckets_owned_.SetInitialBits(num_buckets_); |
| 134 | } |
| 135 | |
David Brazdil | 6d340c4 | 2015-03-03 11:54:54 +0000 | [diff] [blame] | 136 | // Updates this set by intersecting with instructions in a predecessor's set. |
| 137 | void IntersectWith(ValueSet* predecessor) { |
| 138 | if (IsEmpty()) { |
| 139 | return; |
| 140 | } else if (predecessor->IsEmpty()) { |
| 141 | Clear(); |
| 142 | } else { |
| 143 | // Pure instructions do not need to be tested because only impure |
| 144 | // instructions can be killed. |
| 145 | DeleteAllImpureWhich([predecessor](Node* node) { |
| 146 | return !predecessor->Contains(node->GetInstruction()); |
| 147 | }); |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | |
David Brazdil | 6d340c4 | 2015-03-03 11:54:54 +0000 | [diff] [blame] | 151 | bool IsEmpty() const { return num_entries_ == 0; } |
| 152 | size_t GetNumberOfEntries() const { return num_entries_; } |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 153 | |
David Brazdil | 6d340c4 | 2015-03-03 11:54:54 +0000 | [diff] [blame] | 154 | private: |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 155 | class Node : public ArenaObject<kArenaAllocGvn> { |
David Brazdil | 6d340c4 | 2015-03-03 11:54:54 +0000 | [diff] [blame] | 156 | public: |
| 157 | Node(HInstruction* instruction, size_t hash_code, Node* next) |
| 158 | : instruction_(instruction), hash_code_(hash_code), next_(next) {} |
| 159 | |
| 160 | size_t GetHashCode() const { return hash_code_; } |
| 161 | HInstruction* GetInstruction() const { return instruction_; } |
| 162 | Node* GetNext() const { return next_; } |
| 163 | void SetNext(Node* node) { next_ = node; } |
| 164 | |
| 165 | Node* Dup(ArenaAllocator* allocator, Node* new_next = nullptr) { |
| 166 | return new (allocator) Node(instruction_, hash_code_, new_next); |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 167 | } |
| 168 | |
David Brazdil | 6d340c4 | 2015-03-03 11:54:54 +0000 | [diff] [blame] | 169 | private: |
| 170 | HInstruction* const instruction_; |
| 171 | const size_t hash_code_; |
| 172 | Node* next_; |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 173 | |
David Brazdil | 6d340c4 | 2015-03-03 11:54:54 +0000 | [diff] [blame] | 174 | DISALLOW_COPY_AND_ASSIGN(Node); |
| 175 | }; |
| 176 | |
| 177 | // Creates our own copy of a bucket that is currently pointing to a parent. |
| 178 | // This algorithm can be called while iterating over the bucket because it |
| 179 | // preserves the order of entries in the bucket and will return the clone of |
| 180 | // the given 'iterator'. |
| 181 | Node* CloneBucket(size_t index, Node* iterator = nullptr) { |
| 182 | DCHECK(!buckets_owned_.IsBitSet(index)); |
| 183 | Node* clone_current = nullptr; |
| 184 | Node* clone_previous = nullptr; |
| 185 | Node* clone_iterator = nullptr; |
| 186 | for (Node* node = buckets_[index]; node != nullptr; node = node->GetNext()) { |
| 187 | clone_current = node->Dup(allocator_, nullptr); |
| 188 | if (node == iterator) { |
| 189 | clone_iterator = clone_current; |
| 190 | } |
| 191 | if (clone_previous == nullptr) { |
| 192 | buckets_[index] = clone_current; |
| 193 | } else { |
| 194 | clone_previous->SetNext(clone_current); |
| 195 | } |
| 196 | clone_previous = clone_current; |
| 197 | } |
| 198 | buckets_owned_.SetBit(index); |
| 199 | return clone_iterator; |
Nicolas Geoffray | 827eedb | 2015-01-26 15:18:36 +0000 | [diff] [blame] | 200 | } |
| 201 | |
David Brazdil | 6d340c4 | 2015-03-03 11:54:54 +0000 | [diff] [blame] | 202 | // Iterates over buckets with impure instructions (even indices) and deletes |
| 203 | // the ones on which 'cond' returns true. |
| 204 | template<typename Functor> |
| 205 | void DeleteAllImpureWhich(Functor cond) { |
| 206 | for (size_t i = 0; i < num_buckets_; i += 2) { |
| 207 | Node* node = buckets_[i]; |
| 208 | Node* previous = nullptr; |
| 209 | |
| 210 | if (node == nullptr) { |
| 211 | continue; |
Nicolas Geoffray | 827eedb | 2015-01-26 15:18:36 +0000 | [diff] [blame] | 212 | } |
David Brazdil | 6d340c4 | 2015-03-03 11:54:54 +0000 | [diff] [blame] | 213 | |
| 214 | if (!buckets_owned_.IsBitSet(i)) { |
| 215 | // Bucket is not owned but maybe we won't need to change it at all. |
| 216 | // Iterate as long as the entries don't satisfy 'cond'. |
| 217 | while (node != nullptr) { |
| 218 | if (cond(node)) { |
| 219 | // We do need to delete an entry but we do not own the bucket. |
| 220 | // Clone the bucket, make sure 'previous' and 'node' point to |
| 221 | // the cloned entries and break. |
| 222 | previous = CloneBucket(i, previous); |
| 223 | node = (previous == nullptr) ? buckets_[i] : previous->GetNext(); |
| 224 | break; |
Nicolas Geoffray | 827eedb | 2015-01-26 15:18:36 +0000 | [diff] [blame] | 225 | } |
David Brazdil | 6d340c4 | 2015-03-03 11:54:54 +0000 | [diff] [blame] | 226 | previous = node; |
| 227 | node = node->GetNext(); |
Nicolas Geoffray | 827eedb | 2015-01-26 15:18:36 +0000 | [diff] [blame] | 228 | } |
| 229 | } |
David Brazdil | 6d340c4 | 2015-03-03 11:54:54 +0000 | [diff] [blame] | 230 | |
| 231 | // By this point we either own the bucket and can start deleting entries, |
| 232 | // or we do not own it but no entries matched 'cond'. |
| 233 | DCHECK(buckets_owned_.IsBitSet(i) || node == nullptr); |
| 234 | |
| 235 | // We iterate over the remainder of entries and delete those that match |
| 236 | // the given condition. |
| 237 | while (node != nullptr) { |
| 238 | Node* next = node->GetNext(); |
| 239 | if (cond(node)) { |
| 240 | if (previous == nullptr) { |
| 241 | buckets_[i] = next; |
| 242 | } else { |
| 243 | previous->SetNext(next); |
| 244 | } |
| 245 | } else { |
| 246 | previous = node; |
| 247 | } |
| 248 | node = next; |
| 249 | } |
Nicolas Geoffray | 827eedb | 2015-01-26 15:18:36 +0000 | [diff] [blame] | 250 | } |
| 251 | } |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 252 | |
David Brazdil | 6d340c4 | 2015-03-03 11:54:54 +0000 | [diff] [blame] | 253 | // Computes a bucket count such that the load factor is reasonable. |
| 254 | // This is estimated as (num_entries_ * 1.5) and rounded up to nearest pow2. |
| 255 | size_t IdealBucketCount() const { |
| 256 | size_t bucket_count = RoundUpToPowerOfTwo(num_entries_ + (num_entries_ >> 1)); |
| 257 | if (bucket_count > kMinimumNumberOfBuckets) { |
| 258 | return bucket_count; |
| 259 | } else { |
| 260 | return kMinimumNumberOfBuckets; |
| 261 | } |
| 262 | } |
Nicolas Geoffray | 827eedb | 2015-01-26 15:18:36 +0000 | [diff] [blame] | 263 | |
Nicolas Geoffray | 15bd228 | 2016-01-05 15:55:41 +0000 | [diff] [blame] | 264 | // Generates a hash code for an instruction. |
David Brazdil | 6d340c4 | 2015-03-03 11:54:54 +0000 | [diff] [blame] | 265 | size_t HashCode(HInstruction* instruction) const { |
| 266 | size_t hash_code = instruction->ComputeHashCode(); |
Nicolas Geoffray | 15bd228 | 2016-01-05 15:55:41 +0000 | [diff] [blame] | 267 | // Pure instructions are put into odd buckets to speed up deletion. Note that in the |
| 268 | // case of irreducible loops, we don't put pure instructions in odd buckets, as we |
| 269 | // need to delete them when entering the loop. |
| 270 | if (instruction->GetSideEffects().HasDependencies() || |
| 271 | instruction->GetBlock()->GetGraph()->HasIrreducibleLoops()) { |
David Brazdil | 6d340c4 | 2015-03-03 11:54:54 +0000 | [diff] [blame] | 272 | return (hash_code << 1) | 0; |
| 273 | } else { |
| 274 | return (hash_code << 1) | 1; |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | // Converts a hash code to a bucket index. |
| 279 | size_t BucketIndex(size_t hash_code) const { |
| 280 | return hash_code & (num_buckets_ - 1); |
| 281 | } |
Nicolas Geoffray | 827eedb | 2015-01-26 15:18:36 +0000 | [diff] [blame] | 282 | |
| 283 | ArenaAllocator* const allocator_; |
| 284 | |
David Brazdil | 6d340c4 | 2015-03-03 11:54:54 +0000 | [diff] [blame] | 285 | // The internal bucket implementation of the set. |
| 286 | size_t const num_buckets_; |
| 287 | Node** const buckets_; |
Nicolas Geoffray | 827eedb | 2015-01-26 15:18:36 +0000 | [diff] [blame] | 288 | |
David Brazdil | 6d340c4 | 2015-03-03 11:54:54 +0000 | [diff] [blame] | 289 | // Flags specifying which buckets were copied into the set from its parent. |
| 290 | // If a flag is not set, the corresponding bucket points to entries in the |
| 291 | // parent and must be cloned prior to making changes. |
| 292 | ArenaBitVector buckets_owned_; |
| 293 | |
| 294 | // The number of entries in the set. |
| 295 | size_t num_entries_; |
| 296 | |
| 297 | static constexpr size_t kMinimumNumberOfBuckets = 8; |
Nicolas Geoffray | 827eedb | 2015-01-26 15:18:36 +0000 | [diff] [blame] | 298 | |
| 299 | DISALLOW_COPY_AND_ASSIGN(ValueSet); |
| 300 | }; |
| 301 | |
| 302 | /** |
| 303 | * Optimization phase that removes redundant instruction. |
| 304 | */ |
| 305 | class GlobalValueNumberer : public ValueObject { |
| 306 | public: |
| 307 | GlobalValueNumberer(ArenaAllocator* allocator, |
| 308 | HGraph* graph, |
| 309 | const SideEffectsAnalysis& side_effects) |
| 310 | : graph_(graph), |
| 311 | allocator_(allocator), |
| 312 | side_effects_(side_effects), |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 313 | sets_(graph->GetBlocks().size(), nullptr, allocator->Adapter(kArenaAllocGvn)) {} |
Nicolas Geoffray | 827eedb | 2015-01-26 15:18:36 +0000 | [diff] [blame] | 314 | |
| 315 | void Run(); |
| 316 | |
| 317 | private: |
| 318 | // Per-block GVN. Will also update the ValueSet of the dominated and |
| 319 | // successor blocks. |
| 320 | void VisitBasicBlock(HBasicBlock* block); |
| 321 | |
| 322 | HGraph* graph_; |
| 323 | ArenaAllocator* const allocator_; |
| 324 | const SideEffectsAnalysis& side_effects_; |
| 325 | |
| 326 | // ValueSet for blocks. Initially null, but for an individual block they |
| 327 | // are allocated and populated by the dominator, and updated by all blocks |
| 328 | // in the path from the dominator to the block. |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 329 | ArenaVector<ValueSet*> sets_; |
Nicolas Geoffray | 827eedb | 2015-01-26 15:18:36 +0000 | [diff] [blame] | 330 | |
| 331 | DISALLOW_COPY_AND_ASSIGN(GlobalValueNumberer); |
| 332 | }; |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 333 | |
Nicolas Geoffray | 86dde16 | 2015-01-26 12:54:33 +0000 | [diff] [blame] | 334 | void GlobalValueNumberer::Run() { |
| 335 | DCHECK(side_effects_.HasRun()); |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 336 | sets_[graph_->GetEntryBlock()->GetBlockId()] = new (allocator_) ValueSet(allocator_); |
Nicolas Geoffray | 86dde16 | 2015-01-26 12:54:33 +0000 | [diff] [blame] | 337 | |
| 338 | // Use the reverse post order to ensure the non back-edge predecessors of a block are |
| 339 | // visited before the block itself. |
| 340 | for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) { |
| 341 | VisitBasicBlock(it.Current()); |
| 342 | } |
| 343 | } |
| 344 | |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 345 | void GlobalValueNumberer::VisitBasicBlock(HBasicBlock* block) { |
Nicolas Geoffray | dbca6fa | 2014-11-27 12:01:59 +0000 | [diff] [blame] | 346 | ValueSet* set = nullptr; |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 347 | const ArenaVector<HBasicBlock*>& predecessors = block->GetPredecessors(); |
| 348 | if (predecessors.size() == 0 || predecessors[0]->IsEntryBlock()) { |
Nicolas Geoffray | dbca6fa | 2014-11-27 12:01:59 +0000 | [diff] [blame] | 349 | // The entry block should only accumulate constant instructions, and |
| 350 | // the builder puts constants only in the entry block. |
| 351 | // Therefore, there is no need to propagate the value set to the next block. |
| 352 | set = new (allocator_) ValueSet(allocator_); |
| 353 | } else { |
| 354 | HBasicBlock* dominator = block->GetDominator(); |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 355 | ValueSet* dominator_set = sets_[dominator->GetBlockId()]; |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 356 | if (dominator->GetSuccessors().size() == 1) { |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 357 | DCHECK_EQ(dominator->GetSuccessors()[0], block); |
David Brazdil | 6d340c4 | 2015-03-03 11:54:54 +0000 | [diff] [blame] | 358 | set = dominator_set; |
| 359 | } else { |
Nicolas Geoffray | dbca6fa | 2014-11-27 12:01:59 +0000 | [diff] [blame] | 360 | // We have to copy if the dominator has other successors, or `block` is not a successor |
| 361 | // of the dominator. |
David Brazdil | 6d340c4 | 2015-03-03 11:54:54 +0000 | [diff] [blame] | 362 | set = new (allocator_) ValueSet(allocator_, *dominator_set); |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 363 | } |
Nicolas Geoffray | dbca6fa | 2014-11-27 12:01:59 +0000 | [diff] [blame] | 364 | if (!set->IsEmpty()) { |
| 365 | if (block->IsLoopHeader()) { |
Nicolas Geoffray | 15bd228 | 2016-01-05 15:55:41 +0000 | [diff] [blame] | 366 | if (block->GetLoopInformation()->IsIrreducible()) { |
| 367 | // To satisfy our linear scan algorithm, no instruction should flow in an irreducible |
| 368 | // loop header. |
| 369 | set->Clear(); |
| 370 | } else { |
| 371 | DCHECK_EQ(block->GetDominator(), block->GetLoopInformation()->GetPreHeader()); |
| 372 | set->Kill(side_effects_.GetLoopEffects(block)); |
| 373 | } |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 374 | } else if (predecessors.size() > 1) { |
| 375 | for (HBasicBlock* predecessor : predecessors) { |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 376 | set->IntersectWith(sets_[predecessor->GetBlockId()]); |
Nicolas Geoffray | dbca6fa | 2014-11-27 12:01:59 +0000 | [diff] [blame] | 377 | if (set->IsEmpty()) { |
| 378 | break; |
| 379 | } |
| 380 | } |
| 381 | } |
| 382 | } |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 383 | } |
| 384 | |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 385 | sets_[block->GetBlockId()] = set; |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 386 | |
| 387 | HInstruction* current = block->GetFirstInstruction(); |
| 388 | while (current != nullptr) { |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 389 | // Save the next instruction in case `current` is removed from the graph. |
| 390 | HInstruction* next = current->GetNext(); |
Nicolas Geoffray | 729645a | 2015-11-19 13:29:02 +0000 | [diff] [blame] | 391 | // Do not kill the set with the side effects of the instruction just now: if |
| 392 | // the instruction is GVN'ed, we don't need to kill. |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 393 | if (current->CanBeMoved()) { |
Mingyao Yang | dc5ac73 | 2015-02-25 11:28:05 -0800 | [diff] [blame] | 394 | if (current->IsBinaryOperation() && current->AsBinaryOperation()->IsCommutative()) { |
| 395 | // For commutative ops, (x op y) will be treated the same as (y op x) |
| 396 | // after fixed ordering. |
| 397 | current->AsBinaryOperation()->OrderInputs(); |
| 398 | } |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 399 | HInstruction* existing = set->Lookup(current); |
| 400 | if (existing != nullptr) { |
Mingyao Yang | dc5ac73 | 2015-02-25 11:28:05 -0800 | [diff] [blame] | 401 | // This replacement doesn't make more OrderInputs() necessary since |
| 402 | // current is either used by an instruction that it dominates, |
| 403 | // which hasn't been visited yet due to the order we visit instructions. |
| 404 | // Or current is used by a phi, and we don't do OrderInputs() on a phi anyway. |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 405 | current->ReplaceWith(existing); |
| 406 | current->GetBlock()->RemoveInstruction(current); |
| 407 | } else { |
Nicolas Geoffray | 729645a | 2015-11-19 13:29:02 +0000 | [diff] [blame] | 408 | set->Kill(current->GetSideEffects()); |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 409 | set->Add(current); |
| 410 | } |
Nicolas Geoffray | 729645a | 2015-11-19 13:29:02 +0000 | [diff] [blame] | 411 | } else { |
| 412 | set->Kill(current->GetSideEffects()); |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 413 | } |
| 414 | current = next; |
| 415 | } |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 416 | } |
| 417 | |
Nicolas Geoffray | 827eedb | 2015-01-26 15:18:36 +0000 | [diff] [blame] | 418 | void GVNOptimization::Run() { |
| 419 | GlobalValueNumberer gvn(graph_->GetArena(), graph_, side_effects_); |
| 420 | gvn.Run(); |
| 421 | } |
| 422 | |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 423 | } // namespace art |