blob: 8b7ae20b95b04bf41537cc7f7f6f065748df0927 [file] [log] [blame]
buzbee2502e002012-12-31 16:05:53 -08001/*
2 * Copyright (C) 2012 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
buzbee311ca162013-02-28 15:56:43 -080017#include "local_value_numbering.h"
buzbee2502e002012-12-31 16:05:53 -080018
Vladimir Marko95a05972014-05-30 10:01:32 +010019#include "global_value_numbering.h"
Vladimir Markobe0e5462014-02-26 11:24:15 +000020#include "mir_field_info.h"
Vladimir Markof59f18b2014-02-17 15:53:57 +000021#include "mir_graph.h"
22
buzbee2502e002012-12-31 16:05:53 -080023namespace art {
24
Vladimir Marko2ac01fc2014-05-22 12:09:08 +010025namespace { // anonymous namespace
26
27// Operations used for value map keys instead of actual opcode.
Vladimir Marko95a05972014-05-30 10:01:32 +010028static constexpr uint16_t kInvokeMemoryVersionBumpOp = Instruction::INVOKE_VIRTUAL;
29static constexpr uint16_t kUnresolvedSFieldOp = Instruction::SGET;
30static constexpr uint16_t kResolvedSFieldOp = Instruction::SGET_WIDE;
31static constexpr uint16_t kUnresolvedIFieldOp = Instruction::IGET;
32static constexpr uint16_t kNonAliasingIFieldLocOp = Instruction::IGET_WIDE;
33static constexpr uint16_t kNonAliasingIFieldInitialOp = Instruction::IGET_OBJECT;
34static constexpr uint16_t kAliasingIFieldOp = Instruction::IGET_BOOLEAN;
35static constexpr uint16_t kAliasingIFieldStartVersionOp = Instruction::IGET_BYTE;
36static constexpr uint16_t kAliasingIFieldBumpVersionOp = Instruction::IGET_CHAR;
Vladimir Marko2ac01fc2014-05-22 12:09:08 +010037static constexpr uint16_t kNonAliasingArrayOp = Instruction::AGET;
38static constexpr uint16_t kNonAliasingArrayStartVersionOp = Instruction::AGET_WIDE;
Vladimir Marko95a05972014-05-30 10:01:32 +010039static constexpr uint16_t kNonAliasingArrayBumpVersionOp = Instruction::AGET_OBJECT;
40static constexpr uint16_t kAliasingArrayOp = Instruction::AGET_BOOLEAN;
41static constexpr uint16_t kAliasingArrayStartVersionOp = Instruction::AGET_BYTE;
42static constexpr uint16_t kAliasingArrayBumpVersionOp = Instruction::AGET_CHAR;
43static constexpr uint16_t kMergeBlockMemoryVersionBumpOp = Instruction::INVOKE_VIRTUAL_RANGE;
44static constexpr uint16_t kMergeBlockAliasingIFieldVersionBumpOp = Instruction::IPUT;
45static constexpr uint16_t kMergeBlockAliasingIFieldMergeLocationOp = Instruction::IPUT_WIDE;
46static constexpr uint16_t kMergeBlockNonAliasingArrayVersionBumpOp = Instruction::APUT;
47static constexpr uint16_t kMergeBlockNonAliasingArrayMergeLocationOp = Instruction::APUT_WIDE;
48static constexpr uint16_t kMergeBlockAliasingArrayVersionBumpOp = Instruction::APUT_OBJECT;
49static constexpr uint16_t kMergeBlockAliasingArrayMergeLocationOp = Instruction::APUT_BOOLEAN;
50static constexpr uint16_t kMergeBlockNonAliasingIFieldVersionBumpOp = Instruction::APUT_BYTE;
51static constexpr uint16_t kMergeBlockSFieldVersionBumpOp = Instruction::APUT_CHAR;
Vladimir Marko2ac01fc2014-05-22 12:09:08 +010052
53} // anonymous namespace
54
Vladimir Marko95a05972014-05-30 10:01:32 +010055class LocalValueNumbering::AliasingIFieldVersions {
56 public:
57 static uint16_t StartMemoryVersion(GlobalValueNumbering* gvn, const LocalValueNumbering* lvn,
58 uint16_t field_id) {
59 uint16_t type = gvn->GetFieldType(field_id);
60 return gvn->LookupValue(kAliasingIFieldStartVersionOp, field_id,
61 lvn->global_memory_version_, lvn->unresolved_ifield_version_[type]);
62 }
63
64 static uint16_t BumpMemoryVersion(GlobalValueNumbering* gvn, uint16_t old_version,
65 uint16_t store_ref_set_id, uint16_t stored_value) {
66 return gvn->LookupValue(kAliasingIFieldBumpVersionOp, old_version,
67 store_ref_set_id, stored_value);
68 }
69
70 static uint16_t LookupGlobalValue(GlobalValueNumbering* gvn,
71 uint16_t field_id, uint16_t base, uint16_t memory_version) {
72 return gvn->LookupValue(kAliasingIFieldOp, field_id, base, memory_version);
73 }
74
75 static uint16_t LookupMergeValue(GlobalValueNumbering* gvn, const LocalValueNumbering* lvn,
76 uint16_t field_id, uint16_t base) {
77 // If the base/field_id is non-aliasing in lvn, use the non-aliasing value.
78 uint16_t type = gvn->GetFieldType(field_id);
79 if (lvn->IsNonAliasingIField(base, field_id, type)) {
80 uint16_t loc = gvn->LookupValue(kNonAliasingIFieldLocOp, base, field_id, type);
81 auto lb = lvn->non_aliasing_ifield_value_map_.find(loc);
82 return (lb != lvn->non_aliasing_ifield_value_map_.end())
83 ? lb->second
84 : gvn->LookupValue(kNonAliasingIFieldInitialOp, loc, kNoValue, kNoValue);
85 }
86 return AliasingValuesMergeGet<AliasingIFieldVersions>(
87 gvn, lvn, &lvn->aliasing_ifield_value_map_, field_id, base);
88 }
89
90 static bool HasNewBaseVersion(GlobalValueNumbering* gvn, const LocalValueNumbering* lvn,
91 uint16_t field_id) {
92 uint16_t type = gvn->GetFieldType(field_id);
93 return lvn->unresolved_ifield_version_[type] == lvn->merge_new_memory_version_ ||
94 lvn->global_memory_version_ == lvn->merge_new_memory_version_;
95 }
96
97 static uint16_t LookupMergeBlockValue(GlobalValueNumbering* gvn, uint16_t lvn_id,
98 uint16_t field_id) {
99 return gvn->LookupValue(kMergeBlockAliasingIFieldVersionBumpOp, field_id, kNoValue, lvn_id);
100 }
101
102 static uint16_t LookupMergeLocationValue(GlobalValueNumbering* gvn, uint16_t lvn_id,
103 uint16_t field_id, uint16_t base) {
104 return gvn->LookupValue(kMergeBlockAliasingIFieldMergeLocationOp, field_id, base, lvn_id);
105 }
106};
107
108class LocalValueNumbering::NonAliasingArrayVersions {
109 public:
110 static uint16_t StartMemoryVersion(GlobalValueNumbering* gvn, const LocalValueNumbering* lvn,
111 uint16_t array) {
112 return gvn->LookupValue(kNonAliasingArrayStartVersionOp, array, kNoValue, kNoValue);
113 }
114
115 static uint16_t BumpMemoryVersion(GlobalValueNumbering* gvn, uint16_t old_version,
116 uint16_t store_ref_set_id, uint16_t stored_value) {
117 return gvn->LookupValue(kNonAliasingArrayBumpVersionOp, old_version,
118 store_ref_set_id, stored_value);
119 }
120
121 static uint16_t LookupGlobalValue(GlobalValueNumbering* gvn,
122 uint16_t array, uint16_t index, uint16_t memory_version) {
123 return gvn->LookupValue(kNonAliasingArrayOp, array, index, memory_version);
124 }
125
126 static uint16_t LookupMergeValue(GlobalValueNumbering* gvn, const LocalValueNumbering* lvn,
127 uint16_t array, uint16_t index) {
128 return AliasingValuesMergeGet<NonAliasingArrayVersions>(
129 gvn, lvn, &lvn->non_aliasing_array_value_map_, array, index);
130 }
131
132 static bool HasNewBaseVersion(GlobalValueNumbering* gvn, const LocalValueNumbering* lvn,
133 uint16_t array) {
134 return false; // Not affected by global_memory_version_.
135 }
136
137 static uint16_t LookupMergeBlockValue(GlobalValueNumbering* gvn, uint16_t lvn_id,
138 uint16_t array) {
139 return gvn->LookupValue(kMergeBlockNonAliasingArrayVersionBumpOp, array, kNoValue, lvn_id);
140 }
141
142 static uint16_t LookupMergeLocationValue(GlobalValueNumbering* gvn, uint16_t lvn_id,
143 uint16_t array, uint16_t index) {
144 return gvn->LookupValue(kMergeBlockNonAliasingArrayMergeLocationOp, array, index, lvn_id);
145 }
146};
147
148class LocalValueNumbering::AliasingArrayVersions {
149 public:
150 static uint16_t StartMemoryVersion(GlobalValueNumbering* gvn, const LocalValueNumbering* lvn,
151 uint16_t type) {
152 return gvn->LookupValue(kAliasingArrayStartVersionOp, type, lvn->global_memory_version_,
153 kNoValue);
154 }
155
156 static uint16_t BumpMemoryVersion(GlobalValueNumbering* gvn, uint16_t old_version,
157 uint16_t store_ref_set_id, uint16_t stored_value) {
158 return gvn->LookupValue(kAliasingArrayBumpVersionOp, old_version,
159 store_ref_set_id, stored_value);
160 }
161
162 static uint16_t LookupGlobalValue(GlobalValueNumbering* gvn,
163 uint16_t type, uint16_t location, uint16_t memory_version) {
164 return gvn->LookupValue(kAliasingArrayOp, type, location, memory_version);
165 }
166
167 static uint16_t LookupMergeValue(GlobalValueNumbering* gvn, const LocalValueNumbering* lvn,
168 uint16_t type, uint16_t location) {
169 // If the location is non-aliasing in lvn, use the non-aliasing value.
170 uint16_t array = gvn->GetArrayLocationBase(location);
171 if (lvn->IsNonAliasingArray(array, type)) {
172 uint16_t index = gvn->GetArrayLocationIndex(location);
173 return NonAliasingArrayVersions::LookupMergeValue(gvn, lvn, array, index);
174 }
175 return AliasingValuesMergeGet<AliasingArrayVersions>(
176 gvn, lvn, &lvn->aliasing_array_value_map_, type, location);
177 }
178
179 static bool HasNewBaseVersion(GlobalValueNumbering* gvn, const LocalValueNumbering* lvn,
180 uint16_t type) {
181 return lvn->global_memory_version_ == lvn->merge_new_memory_version_;
182 }
183
184 static uint16_t LookupMergeBlockValue(GlobalValueNumbering* gvn, uint16_t lvn_id,
185 uint16_t type) {
186 return gvn->LookupValue(kMergeBlockAliasingArrayVersionBumpOp, type, kNoValue, lvn_id);
187 }
188
189 static uint16_t LookupMergeLocationValue(GlobalValueNumbering* gvn, uint16_t lvn_id,
190 uint16_t type, uint16_t location) {
191 return gvn->LookupValue(kMergeBlockAliasingArrayMergeLocationOp, type, location, lvn_id);
192 }
193};
194
195template <typename Map>
196LocalValueNumbering::AliasingValues* LocalValueNumbering::GetAliasingValues(
197 Map* map, const typename Map::key_type& key) {
198 auto lb = map->lower_bound(key);
199 if (lb == map->end() || map->key_comp()(key, lb->first)) {
Vladimir Markob19955d2014-07-29 12:04:10 +0100200 lb = map->PutBefore(lb, key, AliasingValues(this));
Vladimir Marko95a05972014-05-30 10:01:32 +0100201 }
202 return &lb->second;
Vladimir Marko2ac01fc2014-05-22 12:09:08 +0100203}
204
Vladimir Marko95a05972014-05-30 10:01:32 +0100205template <typename Versions, typename KeyType>
206void LocalValueNumbering::UpdateAliasingValuesLoadVersion(const KeyType& key,
207 AliasingValues* values) {
208 if (values->last_load_memory_version == kNoValue) {
209 // Get the start version that accounts for aliasing with unresolved fields of the same
210 // type and make it unique for the field by including the field_id.
211 uint16_t memory_version = values->memory_version_before_stores;
212 if (memory_version == kNoValue) {
213 memory_version = Versions::StartMemoryVersion(gvn_, this, key);
214 }
215 if (!values->store_loc_set.empty()) {
216 uint16_t ref_set_id = gvn_->GetRefSetId(values->store_loc_set);
217 memory_version = Versions::BumpMemoryVersion(gvn_, memory_version, ref_set_id,
218 values->last_stored_value);
219 }
220 values->last_load_memory_version = memory_version;
Vladimir Markof59f18b2014-02-17 15:53:57 +0000221 }
Vladimir Marko95a05972014-05-30 10:01:32 +0100222}
223
224template <typename Versions, typename Map>
225uint16_t LocalValueNumbering::AliasingValuesMergeGet(GlobalValueNumbering* gvn,
226 const LocalValueNumbering* lvn,
227 Map* map, const typename Map::key_type& key,
228 uint16_t location) {
229 // Retrieve the value name that we would get from
230 // const_cast<LocalValueNumbering*>(lvn)->HandleAliasingValueGet(map. key, location)
231 // but don't modify the map.
232 uint16_t value_name;
233 auto it = map->find(key);
234 if (it == map->end()) {
235 uint16_t start_version = Versions::StartMemoryVersion(gvn, lvn, key);
236 value_name = Versions::LookupGlobalValue(gvn, key, location, start_version);
237 } else if (it->second.store_loc_set.count(location) != 0u) {
238 value_name = it->second.last_stored_value;
239 } else {
240 auto load_it = it->second.load_value_map.find(location);
241 if (load_it != it->second.load_value_map.end()) {
242 value_name = load_it->second;
243 } else {
244 value_name = Versions::LookupGlobalValue(gvn, key, location, it->second.last_load_memory_version);
245 }
246 }
247 return value_name;
248}
249
250template <typename Versions, typename Map>
251uint16_t LocalValueNumbering::HandleAliasingValuesGet(Map* map, const typename Map::key_type& key,
252 uint16_t location) {
253 // Retrieve the value name for IGET/SGET/AGET, update the map with new value if any.
254 uint16_t res;
255 AliasingValues* values = GetAliasingValues(map, key);
256 if (values->store_loc_set.count(location) != 0u) {
257 res = values->last_stored_value;
258 } else {
259 UpdateAliasingValuesLoadVersion<Versions>(key, values);
260 auto lb = values->load_value_map.lower_bound(location);
261 if (lb != values->load_value_map.end() && lb->first == location) {
262 res = lb->second;
263 } else {
264 res = Versions::LookupGlobalValue(gvn_, key, location, values->last_load_memory_version);
265 values->load_value_map.PutBefore(lb, location, res);
266 }
267 }
268 return res;
269}
270
271template <typename Versions, typename Map>
272bool LocalValueNumbering::HandleAliasingValuesPut(Map* map, const typename Map::key_type& key,
273 uint16_t location, uint16_t value) {
274 AliasingValues* values = GetAliasingValues(map, key);
275 auto load_values_it = values->load_value_map.find(location);
276 if (load_values_it != values->load_value_map.end() && load_values_it->second == value) {
277 // This insn can be eliminated, it stores the same value that's already in the field.
278 return false;
279 }
280 if (value == values->last_stored_value) {
281 auto store_loc_lb = values->store_loc_set.lower_bound(location);
282 if (store_loc_lb != values->store_loc_set.end() && *store_loc_lb == location) {
283 // This insn can be eliminated, it stores the same value that's already in the field.
284 return false;
285 }
286 values->store_loc_set.emplace_hint(store_loc_lb, location);
287 } else {
288 UpdateAliasingValuesLoadVersion<Versions>(key, values);
289 values->memory_version_before_stores = values->last_load_memory_version;
290 values->last_stored_value = value;
291 values->store_loc_set.clear();
292 values->store_loc_set.insert(location);
293 }
294 // Clear the last load memory version and remove all potentially overwritten values.
295 values->last_load_memory_version = kNoValue;
296 auto it = values->load_value_map.begin(), end = values->load_value_map.end();
297 while (it != end) {
298 if (it->second == value) {
299 ++it;
300 } else {
301 it = values->load_value_map.erase(it);
302 }
303 }
304 return true;
305}
306
Vladimir Markob19955d2014-07-29 12:04:10 +0100307template <typename K>
308void LocalValueNumbering::CopyAliasingValuesMap(ScopedArenaSafeMap<K, AliasingValues>* dest,
309 const ScopedArenaSafeMap<K, AliasingValues>& src) {
310 // We need each new AliasingValues (or rather its map members) to be constructed
311 // with our allocator, rather than the allocator of the source.
312 for (const auto& entry : src) {
313 auto it = dest->PutBefore(dest->end(), entry.first, AliasingValues(this));
314 it->second = entry.second; // Map assignments preserve current allocator.
315 }
316}
317
318LocalValueNumbering::LocalValueNumbering(GlobalValueNumbering* gvn, uint16_t id,
319 ScopedArenaAllocator* allocator)
Vladimir Marko95a05972014-05-30 10:01:32 +0100320 : gvn_(gvn),
321 id_(id),
Vladimir Markob19955d2014-07-29 12:04:10 +0100322 sreg_value_map_(std::less<uint16_t>(), allocator->Adapter()),
323 sreg_wide_value_map_(std::less<uint16_t>(), allocator->Adapter()),
324 sfield_value_map_(std::less<uint16_t>(), allocator->Adapter()),
325 non_aliasing_ifield_value_map_(std::less<uint16_t>(), allocator->Adapter()),
326 aliasing_ifield_value_map_(std::less<uint16_t>(), allocator->Adapter()),
327 non_aliasing_array_value_map_(std::less<uint16_t>(), allocator->Adapter()),
328 aliasing_array_value_map_(std::less<uint16_t>(), allocator->Adapter()),
Vladimir Marko95a05972014-05-30 10:01:32 +0100329 global_memory_version_(0u),
Vladimir Markob19955d2014-07-29 12:04:10 +0100330 non_aliasing_refs_(std::less<uint16_t>(), allocator->Adapter()),
331 escaped_refs_(std::less<uint16_t>(), allocator->Adapter()),
332 escaped_ifield_clobber_set_(EscapedIFieldClobberKeyComparator(), allocator->Adapter()),
333 escaped_array_clobber_set_(EscapedArrayClobberKeyComparator(), allocator->Adapter()),
334 range_checked_(RangeCheckKeyComparator() , allocator->Adapter()),
335 null_checked_(std::less<uint16_t>(), allocator->Adapter()),
336 merge_names_(allocator->Adapter()),
337 merge_map_(std::less<ScopedArenaVector<BasicBlockId>>(), allocator->Adapter()),
Vladimir Marko95a05972014-05-30 10:01:32 +0100338 merge_new_memory_version_(kNoValue) {
339 std::fill_n(unresolved_sfield_version_, kFieldTypeCount, 0u);
340 std::fill_n(unresolved_ifield_version_, kFieldTypeCount, 0u);
341}
342
343bool LocalValueNumbering::Equals(const LocalValueNumbering& other) const {
344 DCHECK(gvn_ == other.gvn_);
345 // Compare the maps/sets and memory versions.
346 return sreg_value_map_ == other.sreg_value_map_ &&
347 sreg_wide_value_map_ == other.sreg_wide_value_map_ &&
348 sfield_value_map_ == other.sfield_value_map_ &&
349 non_aliasing_ifield_value_map_ == other.non_aliasing_ifield_value_map_ &&
350 aliasing_ifield_value_map_ == other.aliasing_ifield_value_map_ &&
351 non_aliasing_array_value_map_ == other.non_aliasing_array_value_map_ &&
352 aliasing_array_value_map_ == other.aliasing_array_value_map_ &&
353 SameMemoryVersion(other) &&
354 non_aliasing_refs_ == other.non_aliasing_refs_ &&
355 escaped_refs_ == other.escaped_refs_ &&
356 escaped_ifield_clobber_set_ == other.escaped_ifield_clobber_set_ &&
357 escaped_array_clobber_set_ == other.escaped_array_clobber_set_ &&
358 range_checked_ == other.range_checked_ &&
359 null_checked_ == other.null_checked_;
360}
361
362void LocalValueNumbering::MergeOne(const LocalValueNumbering& other, MergeType merge_type) {
Vladimir Markob19955d2014-07-29 12:04:10 +0100363 CopyLiveSregValues(&sreg_value_map_, other.sreg_value_map_);
364 CopyLiveSregValues(&sreg_wide_value_map_, other.sreg_wide_value_map_);
Vladimir Marko95a05972014-05-30 10:01:32 +0100365
366 if (merge_type == kReturnMerge) {
367 // RETURN or PHI+RETURN. We need only sreg value maps.
368 return;
369 }
370
371 non_aliasing_ifield_value_map_ = other.non_aliasing_ifield_value_map_;
Vladimir Markob19955d2014-07-29 12:04:10 +0100372 CopyAliasingValuesMap(&non_aliasing_array_value_map_, other.non_aliasing_array_value_map_);
Vladimir Marko95a05972014-05-30 10:01:32 +0100373 non_aliasing_refs_ = other.non_aliasing_refs_;
374 range_checked_ = other.range_checked_;
375 null_checked_ = other.null_checked_;
376
377 if (merge_type == kCatchMerge) {
378 // Memory is clobbered. Use new memory version and don't merge aliasing locations.
379 global_memory_version_ = NewMemoryVersion(&merge_new_memory_version_);
380 std::fill_n(unresolved_sfield_version_, kFieldTypeCount, global_memory_version_);
381 std::fill_n(unresolved_ifield_version_, kFieldTypeCount, global_memory_version_);
382 PruneNonAliasingRefsForCatch();
383 return;
384 }
385
386 DCHECK(merge_type == kNormalMerge);
387 global_memory_version_ = other.global_memory_version_;
388 std::copy_n(other.unresolved_ifield_version_, kFieldTypeCount, unresolved_ifield_version_);
389 std::copy_n(other.unresolved_sfield_version_, kFieldTypeCount, unresolved_sfield_version_);
390 sfield_value_map_ = other.sfield_value_map_;
Vladimir Markob19955d2014-07-29 12:04:10 +0100391 CopyAliasingValuesMap(&aliasing_ifield_value_map_, other.aliasing_ifield_value_map_);
392 CopyAliasingValuesMap(&aliasing_array_value_map_, other.aliasing_array_value_map_);
Vladimir Marko95a05972014-05-30 10:01:32 +0100393 escaped_refs_ = other.escaped_refs_;
394 escaped_ifield_clobber_set_ = other.escaped_ifield_clobber_set_;
395 escaped_array_clobber_set_ = other.escaped_array_clobber_set_;
396}
397
398bool LocalValueNumbering::SameMemoryVersion(const LocalValueNumbering& other) const {
399 return
400 global_memory_version_ == other.global_memory_version_ &&
401 std::equal(unresolved_ifield_version_, unresolved_ifield_version_ + kFieldTypeCount,
402 other.unresolved_ifield_version_) &&
403 std::equal(unresolved_sfield_version_, unresolved_sfield_version_ + kFieldTypeCount,
404 other.unresolved_sfield_version_);
405}
406
407uint16_t LocalValueNumbering::NewMemoryVersion(uint16_t* new_version) {
408 if (*new_version == kNoValue) {
409 *new_version = gvn_->LookupValue(kMergeBlockMemoryVersionBumpOp, 0u, 0u, id_);
410 }
411 return *new_version;
412}
413
414void LocalValueNumbering::MergeMemoryVersions(bool clobbered_catch) {
415 DCHECK_GE(gvn_->merge_lvns_.size(), 2u);
416 const LocalValueNumbering* cmp = gvn_->merge_lvns_[0];
417 // Check if the global version has changed.
418 bool new_global_version = clobbered_catch;
419 if (!new_global_version) {
420 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
421 if (lvn->global_memory_version_ != cmp->global_memory_version_) {
422 // Use a new version for everything.
423 new_global_version = true;
424 break;
425 }
426 }
427 }
428 if (new_global_version) {
429 global_memory_version_ = NewMemoryVersion(&merge_new_memory_version_);
430 std::fill_n(unresolved_sfield_version_, kFieldTypeCount, merge_new_memory_version_);
431 std::fill_n(unresolved_ifield_version_, kFieldTypeCount, merge_new_memory_version_);
432 } else {
433 // Initialize with a copy of memory versions from the comparison LVN.
434 global_memory_version_ = cmp->global_memory_version_;
435 std::copy_n(cmp->unresolved_ifield_version_, kFieldTypeCount, unresolved_ifield_version_);
436 std::copy_n(cmp->unresolved_sfield_version_, kFieldTypeCount, unresolved_sfield_version_);
437 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
438 if (lvn == cmp) {
439 continue;
440 }
441 for (size_t i = 0; i != kFieldTypeCount; ++i) {
442 if (lvn->unresolved_ifield_version_[i] != cmp->unresolved_ifield_version_[i]) {
443 unresolved_ifield_version_[i] = NewMemoryVersion(&merge_new_memory_version_);
444 }
445 if (lvn->unresolved_sfield_version_[i] != cmp->unresolved_sfield_version_[i]) {
446 unresolved_sfield_version_[i] = NewMemoryVersion(&merge_new_memory_version_);
447 }
448 }
449 }
450 }
451}
452
453void LocalValueNumbering::PruneNonAliasingRefsForCatch() {
454 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
455 const BasicBlock* bb = gvn_->GetBasicBlock(lvn->Id());
Vladimir Marko11ca6122014-07-17 20:50:07 +0100456 if (UNLIKELY(bb->taken == id_) || UNLIKELY(bb->fall_through == id_)) {
457 // Non-exceptional path to a catch handler means that the catch block was actually
458 // empty and all exceptional paths lead to the shared path after that empty block.
459 continue;
460 }
Vladimir Marko95a05972014-05-30 10:01:32 +0100461 DCHECK_EQ(bb->taken, kNullBlock);
462 DCHECK_NE(bb->fall_through, kNullBlock);
463 const BasicBlock* fall_through_bb = gvn_->GetBasicBlock(bb->fall_through);
464 const MIR* mir = fall_through_bb->first_mir_insn;
465 DCHECK(mir != nullptr);
466 // Only INVOKEs can leak and clobber non-aliasing references if they throw.
Jean Christophe Beylerfb0ea2d2014-07-29 13:20:42 -0700467 if ((mir->dalvikInsn.FlagsOf() & Instruction::kInvoke) != 0) {
Vladimir Marko95a05972014-05-30 10:01:32 +0100468 for (uint16_t i = 0u; i != mir->ssa_rep->num_uses; ++i) {
469 uint16_t value_name = lvn->GetOperandValue(mir->ssa_rep->uses[i]);
470 non_aliasing_refs_.erase(value_name);
471 }
472 }
473 }
474}
475
476
477template <typename Set, Set LocalValueNumbering::* set_ptr>
478void LocalValueNumbering::IntersectSets() {
479 DCHECK_GE(gvn_->merge_lvns_.size(), 2u);
480
481 // Find the LVN with the least entries in the set.
482 const LocalValueNumbering* least_entries_lvn = gvn_->merge_lvns_[0];
483 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
484 if ((lvn->*set_ptr).size() < (least_entries_lvn->*set_ptr).size()) {
485 least_entries_lvn = lvn;
486 }
487 }
488
489 // For each key check if it's in all the LVNs.
490 for (const auto& key : least_entries_lvn->*set_ptr) {
491 bool checked = true;
492 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
493 if (lvn != least_entries_lvn && (lvn->*set_ptr).count(key) == 0u) {
494 checked = false;
495 break;
496 }
497 }
498 if (checked) {
499 (this->*set_ptr).emplace_hint((this->*set_ptr).end(), key);
500 }
501 }
502}
503
Vladimir Markob19955d2014-07-29 12:04:10 +0100504void LocalValueNumbering::CopyLiveSregValues(SregValueMap* dest, const SregValueMap& src) {
505 auto dest_end = dest->end();
506 ArenaBitVector* live_in_v = gvn_->GetMirGraph()->GetBasicBlock(id_)->data_flow_info->live_in_v;
507 DCHECK(live_in_v != nullptr);
508 for (const auto& entry : src) {
509 bool live = live_in_v->IsBitSet(gvn_->GetMirGraph()->SRegToVReg(entry.first));
510 if (live) {
511 dest->PutBefore(dest_end, entry.first, entry.second);
512 }
513 }
514}
515
516template <LocalValueNumbering::SregValueMap LocalValueNumbering::* map_ptr>
517void LocalValueNumbering::IntersectSregValueMaps() {
Vladimir Marko95a05972014-05-30 10:01:32 +0100518 DCHECK_GE(gvn_->merge_lvns_.size(), 2u);
519
520 // Find the LVN with the least entries in the set.
521 const LocalValueNumbering* least_entries_lvn = gvn_->merge_lvns_[0];
522 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
523 if ((lvn->*map_ptr).size() < (least_entries_lvn->*map_ptr).size()) {
524 least_entries_lvn = lvn;
525 }
526 }
527
528 // For each key check if it's in all the LVNs.
Vladimir Markob19955d2014-07-29 12:04:10 +0100529 ArenaBitVector* live_in_v = gvn_->GetMirGraph()->GetBasicBlock(id_)->data_flow_info->live_in_v;
530 DCHECK(live_in_v != nullptr);
Vladimir Marko95a05972014-05-30 10:01:32 +0100531 for (const auto& entry : least_entries_lvn->*map_ptr) {
Vladimir Markob19955d2014-07-29 12:04:10 +0100532 bool live_and_same = live_in_v->IsBitSet(gvn_->GetMirGraph()->SRegToVReg(entry.first));
533 if (live_and_same) {
534 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
535 if (lvn != least_entries_lvn) {
536 auto it = (lvn->*map_ptr).find(entry.first);
537 if (it == (lvn->*map_ptr).end() || !(it->second == entry.second)) {
538 live_and_same = false;
539 break;
540 }
Vladimir Marko95a05972014-05-30 10:01:32 +0100541 }
542 }
543 }
Vladimir Markob19955d2014-07-29 12:04:10 +0100544 if (live_and_same) {
Vladimir Marko95a05972014-05-30 10:01:32 +0100545 (this->*map_ptr).PutBefore((this->*map_ptr).end(), entry.first, entry.second);
546 }
547 }
548}
549
550// Intersect maps as sets. The value type must be equality-comparable.
551template <typename Map>
552void LocalValueNumbering::InPlaceIntersectMaps(Map* work_map, const Map& other_map) {
553 auto work_it = work_map->begin(), work_end = work_map->end();
554 auto cmp = work_map->value_comp();
555 for (const auto& entry : other_map) {
556 while (work_it != work_end &&
557 (cmp(*work_it, entry) ||
558 (!cmp(entry, *work_it) && !(work_it->second == entry.second)))) {
559 work_it = work_map->erase(work_it);
560 }
Vladimir Marko55fff042014-07-10 12:42:52 +0100561 if (work_it == work_end) {
562 return;
563 }
564 ++work_it;
Vladimir Marko95a05972014-05-30 10:01:32 +0100565 }
566}
567
568template <typename Set, Set LocalValueNumbering::*set_ptr, void (LocalValueNumbering::*MergeFn)(
569 const typename Set::value_type& entry, typename Set::iterator hint)>
570void LocalValueNumbering::MergeSets() {
571 auto cmp = (this->*set_ptr).value_comp();
572 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
573 auto my_it = (this->*set_ptr).begin(), my_end = (this->*set_ptr).end();
574 for (const auto& entry : lvn->*set_ptr) {
575 while (my_it != my_end && cmp(*my_it, entry)) {
576 ++my_it;
577 }
578 if (my_it != my_end && !cmp(entry, *my_it)) {
579 // Already handled.
580 ++my_it;
581 } else {
582 // Merge values for this field_id.
583 (this->*MergeFn)(entry, my_it); // my_it remains valid across inserts to std::set/SafeMap.
584 }
585 }
586 }
587}
588
589void LocalValueNumbering::IntersectAliasingValueLocations(AliasingValues* work_values,
590 const AliasingValues* values) {
591 auto cmp = work_values->load_value_map.key_comp();
592 auto work_it = work_values->load_value_map.begin(), work_end = work_values->load_value_map.end();
593 auto store_it = values->store_loc_set.begin(), store_end = values->store_loc_set.end();
594 auto load_it = values->load_value_map.begin(), load_end = values->load_value_map.end();
595 while (store_it != store_end || load_it != load_end) {
596 uint16_t loc;
597 if (store_it != store_end && (load_it == load_end || *store_it < load_it->first)) {
598 loc = *store_it;
599 ++store_it;
600 } else {
601 loc = load_it->first;
602 ++load_it;
603 DCHECK(store_it == store_end || cmp(loc, *store_it));
604 }
605 while (work_it != work_end && cmp(work_it->first, loc)) {
606 work_it = work_values->load_value_map.erase(work_it);
607 }
608 if (work_it != work_end && !cmp(loc, work_it->first)) {
609 // The location matches, keep it.
610 ++work_it;
611 }
612 }
613 while (work_it != work_end) {
614 work_it = work_values->load_value_map.erase(work_it);
615 }
616}
617
618void LocalValueNumbering::MergeEscapedRefs(const ValueNameSet::value_type& entry,
619 ValueNameSet::iterator hint) {
620 // See if the ref is either escaped or non-aliasing in each predecessor.
621 bool is_escaped = true;
622 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
623 if (lvn->non_aliasing_refs_.count(entry) == 0u &&
624 lvn->escaped_refs_.count(entry) == 0u) {
625 is_escaped = false;
626 break;
627 }
628 }
629 if (is_escaped) {
630 escaped_refs_.emplace_hint(hint, entry);
631 }
632}
633
634void LocalValueNumbering::MergeEscapedIFieldTypeClobberSets(
635 const EscapedIFieldClobberSet::value_type& entry, EscapedIFieldClobberSet::iterator hint) {
636 // Insert only type-clobber entries (field_id == kNoValue) of escaped refs.
637 if (entry.field_id == kNoValue && escaped_refs_.count(entry.base) != 0u) {
638 escaped_ifield_clobber_set_.emplace_hint(hint, entry);
639 }
640}
641
642void LocalValueNumbering::MergeEscapedIFieldClobberSets(
643 const EscapedIFieldClobberSet::value_type& entry, EscapedIFieldClobberSet::iterator hint) {
644 // Insert only those entries of escaped refs that are not overridden by a type clobber.
645 if (!(hint == escaped_ifield_clobber_set_.end() &&
646 hint->base == entry.base && hint->type == entry.type) &&
647 escaped_refs_.count(entry.base) != 0u) {
648 escaped_ifield_clobber_set_.emplace_hint(hint, entry);
649 }
650}
651
652void LocalValueNumbering::MergeEscapedArrayClobberSets(
653 const EscapedArrayClobberSet::value_type& entry, EscapedArrayClobberSet::iterator hint) {
654 if (escaped_refs_.count(entry.base) != 0u) {
655 escaped_array_clobber_set_.emplace_hint(hint, entry);
656 }
657}
658
Vladimir Marko2d2365c2014-08-19 18:08:39 +0100659void LocalValueNumbering::MergeNullChecked() {
660 DCHECK_GE(gvn_->merge_lvns_.size(), 2u);
661
662 // Find the LVN with the least entries in the set.
663 const LocalValueNumbering* least_entries_lvn = gvn_->merge_lvns_[0];
664 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
665 if (lvn->null_checked_.size() < least_entries_lvn->null_checked_.size()) {
666 least_entries_lvn = lvn;
667 }
668 }
669
670 // For each null-checked value name check if it's null-checked in all the LVNs.
671 for (const auto& value_name : least_entries_lvn->null_checked_) {
672 // Merge null_checked_ for this ref.
673 merge_names_.clear();
674 merge_names_.resize(gvn_->merge_lvns_.size(), value_name);
675 if (gvn_->NullCheckedInAllPredecessors(merge_names_)) {
676 null_checked_.insert(null_checked_.end(), value_name);
677 }
678 }
679
680 // Now check if the least_entries_lvn has a null-check as the last insn.
681 const BasicBlock* least_entries_bb = gvn_->GetBasicBlock(least_entries_lvn->Id());
682 if (gvn_->HasNullCheckLastInsn(least_entries_bb, id_)) {
683 int s_reg = least_entries_bb->last_mir_insn->ssa_rep->uses[0];
684 uint32_t value_name = least_entries_lvn->GetSRegValueName(s_reg);
685 merge_names_.clear();
686 merge_names_.resize(gvn_->merge_lvns_.size(), value_name);
687 if (gvn_->NullCheckedInAllPredecessors(merge_names_)) {
688 null_checked_.insert(value_name);
689 }
Vladimir Marko95a05972014-05-30 10:01:32 +0100690 }
691}
692
693void LocalValueNumbering::MergeSFieldValues(const SFieldToValueMap::value_type& entry,
694 SFieldToValueMap::iterator hint) {
695 uint16_t field_id = entry.first;
696 merge_names_.clear();
697 uint16_t value_name = kNoValue;
698 bool same_values = true;
699 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
700 // Get the value name as in HandleSGet() but don't modify *lvn.
701 auto it = lvn->sfield_value_map_.find(field_id);
702 if (it != lvn->sfield_value_map_.end()) {
703 value_name = it->second;
704 } else {
705 uint16_t type = gvn_->GetFieldType(field_id);
706 value_name = gvn_->LookupValue(kResolvedSFieldOp, field_id,
707 lvn->unresolved_sfield_version_[type],
708 lvn->global_memory_version_);
709 }
710
711 same_values = same_values && (merge_names_.empty() || value_name == merge_names_.back());
712 merge_names_.push_back(value_name);
713 }
714 if (same_values) {
715 // value_name already contains the result.
716 } else {
717 auto lb = merge_map_.lower_bound(merge_names_);
718 if (lb != merge_map_.end() && !merge_map_.key_comp()(merge_names_, lb->first)) {
719 value_name = lb->second;
720 } else {
721 value_name = gvn_->LookupValue(kMergeBlockSFieldVersionBumpOp, field_id, id_, kNoValue);
722 merge_map_.PutBefore(lb, merge_names_, value_name);
723 if (gvn_->NullCheckedInAllPredecessors(merge_names_)) {
724 null_checked_.insert(value_name);
725 }
726 }
727 }
728 sfield_value_map_.PutBefore(hint, field_id, value_name);
729}
730
731void LocalValueNumbering::MergeNonAliasingIFieldValues(const IFieldLocToValueMap::value_type& entry,
732 IFieldLocToValueMap::iterator hint) {
733 uint16_t field_loc = entry.first;
734 merge_names_.clear();
735 uint16_t value_name = kNoValue;
736 bool same_values = true;
737 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
738 // Get the value name as in HandleIGet() but don't modify *lvn.
739 auto it = lvn->non_aliasing_ifield_value_map_.find(field_loc);
740 if (it != lvn->non_aliasing_ifield_value_map_.end()) {
741 value_name = it->second;
742 } else {
743 value_name = gvn_->LookupValue(kNonAliasingIFieldInitialOp, field_loc, kNoValue, kNoValue);
744 }
745
746 same_values = same_values && (merge_names_.empty() || value_name == merge_names_.back());
747 merge_names_.push_back(value_name);
748 }
749 if (same_values) {
750 // value_name already contains the result.
751 } else {
752 auto lb = merge_map_.lower_bound(merge_names_);
753 if (lb != merge_map_.end() && !merge_map_.key_comp()(merge_names_, lb->first)) {
754 value_name = lb->second;
755 } else {
756 value_name = gvn_->LookupValue(kMergeBlockNonAliasingIFieldVersionBumpOp, field_loc,
757 id_, kNoValue);
758 merge_map_.PutBefore(lb, merge_names_, value_name);
759 if (gvn_->NullCheckedInAllPredecessors(merge_names_)) {
760 null_checked_.insert(value_name);
761 }
762 }
763 }
764 non_aliasing_ifield_value_map_.PutBefore(hint, field_loc, value_name);
765}
766
767template <typename Map, Map LocalValueNumbering::*map_ptr, typename Versions>
768void LocalValueNumbering::MergeAliasingValues(const typename Map::value_type& entry,
769 typename Map::iterator hint) {
770 const typename Map::key_type& key = entry.first;
771
Vladimir Markob19955d2014-07-29 12:04:10 +0100772 auto it = (this->*map_ptr).PutBefore(hint, key, AliasingValues(this));
Vladimir Marko95a05972014-05-30 10:01:32 +0100773 AliasingValues* my_values = &it->second;
774
775 const AliasingValues* cmp_values = nullptr;
776 bool same_version = !Versions::HasNewBaseVersion(gvn_, this, key);
777 uint16_t load_memory_version_for_same_version = kNoValue;
778 if (same_version) {
779 // Find the first non-null values.
780 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
781 auto it = (lvn->*map_ptr).find(key);
782 if (it != (lvn->*map_ptr).end()) {
783 cmp_values = &it->second;
784 break;
785 }
786 }
787 DCHECK(cmp_values != nullptr); // There must be at least one non-null values.
788
789 // Check if we have identical memory versions, i.e. the global memory version, unresolved
790 // field version and the values' memory_version_before_stores, last_stored_value
791 // and store_loc_set are identical.
792 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
793 auto it = (lvn->*map_ptr).find(key);
794 if (it == (lvn->*map_ptr).end()) {
795 if (cmp_values->memory_version_before_stores != kNoValue) {
796 same_version = false;
797 break;
798 }
799 } else if (cmp_values->last_stored_value != it->second.last_stored_value ||
800 cmp_values->memory_version_before_stores != it->second.memory_version_before_stores ||
801 cmp_values->store_loc_set != it->second.store_loc_set) {
802 same_version = false;
803 break;
804 } else if (it->second.last_load_memory_version != kNoValue) {
805 DCHECK(load_memory_version_for_same_version == kNoValue ||
806 load_memory_version_for_same_version == it->second.last_load_memory_version);
807 load_memory_version_for_same_version = it->second.last_load_memory_version;
808 }
809 }
810 }
811
812 if (same_version) {
813 // Copy the identical values.
814 my_values->memory_version_before_stores = cmp_values->memory_version_before_stores;
815 my_values->last_stored_value = cmp_values->last_stored_value;
816 my_values->store_loc_set = cmp_values->store_loc_set;
817 my_values->last_load_memory_version = load_memory_version_for_same_version;
818 // Merge load values seen in all incoming arcs (i.e. an intersection).
819 if (!cmp_values->load_value_map.empty()) {
820 my_values->load_value_map = cmp_values->load_value_map;
821 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
822 auto it = (lvn->*map_ptr).find(key);
823 if (it == (lvn->*map_ptr).end() || it->second.load_value_map.empty()) {
824 my_values->load_value_map.clear();
825 break;
826 }
827 InPlaceIntersectMaps(&my_values->load_value_map, it->second.load_value_map);
828 if (my_values->load_value_map.empty()) {
829 break;
830 }
831 }
832 }
833 } else {
834 // Bump version number for the merge.
835 my_values->memory_version_before_stores = my_values->last_load_memory_version =
836 Versions::LookupMergeBlockValue(gvn_, id_, key);
837
838 // Calculate the locations that have been either read from or written to in each incoming LVN.
839 bool first_lvn = true;
840 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
841 auto it = (lvn->*map_ptr).find(key);
842 if (it == (lvn->*map_ptr).end()) {
843 my_values->load_value_map.clear();
844 break;
845 }
846 if (first_lvn) {
847 first_lvn = false;
848 // Copy the first LVN's locations. Values will be overwritten later.
849 my_values->load_value_map = it->second.load_value_map;
850 for (uint16_t location : it->second.store_loc_set) {
851 my_values->load_value_map.Put(location, 0u);
852 }
853 } else {
854 IntersectAliasingValueLocations(my_values, &it->second);
855 }
856 }
857 // Calculate merged values for the intersection.
858 for (auto& load_value_entry : my_values->load_value_map) {
859 uint16_t location = load_value_entry.first;
860 bool same_values = true;
861 uint16_t value_name = kNoValue;
862 merge_names_.clear();
863 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
864 value_name = Versions::LookupMergeValue(gvn_, lvn, key, location);
865 same_values = same_values && (merge_names_.empty() || value_name == merge_names_.back());
866 merge_names_.push_back(value_name);
867 }
868 if (same_values) {
869 // value_name already contains the result.
870 } else {
871 auto lb = merge_map_.lower_bound(merge_names_);
872 if (lb != merge_map_.end() && !merge_map_.key_comp()(merge_names_, lb->first)) {
873 value_name = lb->second;
874 } else {
875 // NOTE: In addition to the key and id_ which don't change on an LVN recalculation
876 // during GVN, we also add location which can actually change on recalculation, so the
877 // value_name below may change. This could lead to an infinite loop if the location
878 // value name always changed when the refereced value name changes. However, given that
879 // we assign unique value names for other merges, such as Phis, such a dependency is
880 // not possible in a well-formed SSA graph.
881 value_name = Versions::LookupMergeLocationValue(gvn_, id_, key, location);
882 merge_map_.PutBefore(lb, merge_names_, value_name);
883 if (gvn_->NullCheckedInAllPredecessors(merge_names_)) {
884 null_checked_.insert(value_name);
885 }
886 }
887 }
888 load_value_entry.second = value_name;
889 }
890 }
891}
892
893void LocalValueNumbering::Merge(MergeType merge_type) {
894 DCHECK_GE(gvn_->merge_lvns_.size(), 2u);
895
Vladimir Markob19955d2014-07-29 12:04:10 +0100896 IntersectSregValueMaps<&LocalValueNumbering::sreg_value_map_>();
897 IntersectSregValueMaps<&LocalValueNumbering::sreg_wide_value_map_>();
Vladimir Marko95a05972014-05-30 10:01:32 +0100898 if (merge_type == kReturnMerge) {
899 // RETURN or PHI+RETURN. We need only sreg value maps.
900 return;
901 }
902
903 MergeMemoryVersions(merge_type == kCatchMerge);
904
905 // Merge non-aliasing maps/sets.
Vladimir Marko95a05972014-05-30 10:01:32 +0100906 IntersectSets<ValueNameSet, &LocalValueNumbering::non_aliasing_refs_>();
Vladimir Marko55fff042014-07-10 12:42:52 +0100907 if (!non_aliasing_refs_.empty() && merge_type == kCatchMerge) {
908 PruneNonAliasingRefsForCatch();
909 }
910 if (!non_aliasing_refs_.empty()) {
911 MergeSets<IFieldLocToValueMap, &LocalValueNumbering::non_aliasing_ifield_value_map_,
912 &LocalValueNumbering::MergeNonAliasingIFieldValues>();
913 MergeSets<NonAliasingArrayValuesMap, &LocalValueNumbering::non_aliasing_array_value_map_,
914 &LocalValueNumbering::MergeAliasingValues<
915 NonAliasingArrayValuesMap, &LocalValueNumbering::non_aliasing_array_value_map_,
916 NonAliasingArrayVersions>>();
917 }
Vladimir Marko95a05972014-05-30 10:01:32 +0100918
919 // We won't do anything complicated for range checks, just calculate the intersection.
920 IntersectSets<RangeCheckSet, &LocalValueNumbering::range_checked_>();
921
922 // Merge null_checked_. We may later insert more, such as merged object field values.
Vladimir Marko2d2365c2014-08-19 18:08:39 +0100923 MergeNullChecked();
Vladimir Marko95a05972014-05-30 10:01:32 +0100924
925 if (merge_type == kCatchMerge) {
926 // Memory is clobbered. New memory version already created, don't merge aliasing locations.
Vladimir Marko95a05972014-05-30 10:01:32 +0100927 return;
928 }
929
930 DCHECK(merge_type == kNormalMerge);
931
932 // Merge escaped refs and clobber sets.
933 MergeSets<ValueNameSet, &LocalValueNumbering::escaped_refs_,
934 &LocalValueNumbering::MergeEscapedRefs>();
935 if (!escaped_refs_.empty()) {
936 MergeSets<EscapedIFieldClobberSet, &LocalValueNumbering::escaped_ifield_clobber_set_,
937 &LocalValueNumbering::MergeEscapedIFieldTypeClobberSets>();
938 MergeSets<EscapedIFieldClobberSet, &LocalValueNumbering::escaped_ifield_clobber_set_,
939 &LocalValueNumbering::MergeEscapedIFieldClobberSets>();
940 MergeSets<EscapedArrayClobberSet, &LocalValueNumbering::escaped_array_clobber_set_,
941 &LocalValueNumbering::MergeEscapedArrayClobberSets>();
942 }
943
944 MergeSets<SFieldToValueMap, &LocalValueNumbering::sfield_value_map_,
945 &LocalValueNumbering::MergeSFieldValues>();
946 MergeSets<AliasingIFieldValuesMap, &LocalValueNumbering::aliasing_ifield_value_map_,
947 &LocalValueNumbering::MergeAliasingValues<
948 AliasingIFieldValuesMap, &LocalValueNumbering::aliasing_ifield_value_map_,
949 AliasingIFieldVersions>>();
950 MergeSets<AliasingArrayValuesMap, &LocalValueNumbering::aliasing_array_value_map_,
951 &LocalValueNumbering::MergeAliasingValues<
952 AliasingArrayValuesMap, &LocalValueNumbering::aliasing_array_value_map_,
953 AliasingArrayVersions>>();
Vladimir Markof59f18b2014-02-17 15:53:57 +0000954}
955
Vladimir Markof59f18b2014-02-17 15:53:57 +0000956uint16_t LocalValueNumbering::MarkNonAliasingNonNull(MIR* mir) {
957 uint16_t res = GetOperandValue(mir->ssa_rep->defs[0]);
Vladimir Markof59f18b2014-02-17 15:53:57 +0000958 DCHECK(null_checked_.find(res) == null_checked_.end());
959 null_checked_.insert(res);
960 non_aliasing_refs_.insert(res);
961 return res;
962}
963
Vladimir Marko95a05972014-05-30 10:01:32 +0100964bool LocalValueNumbering::IsNonAliasing(uint16_t reg) const {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +0100965 return non_aliasing_refs_.find(reg) != non_aliasing_refs_.end();
Vladimir Markof59f18b2014-02-17 15:53:57 +0000966}
967
Vladimir Marko95a05972014-05-30 10:01:32 +0100968bool LocalValueNumbering::IsNonAliasingIField(uint16_t reg, uint16_t field_id,
969 uint16_t type) const {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +0100970 if (IsNonAliasing(reg)) {
971 return true;
972 }
Vladimir Marko95a05972014-05-30 10:01:32 +0100973 if (escaped_refs_.find(reg) == escaped_refs_.end()) {
974 return false;
975 }
976 // Check for IPUTs to unresolved fields.
977 EscapedIFieldClobberKey key1 = { reg, type, kNoValue };
978 if (escaped_ifield_clobber_set_.find(key1) != escaped_ifield_clobber_set_.end()) {
979 return false;
980 }
981 // Check for aliased IPUTs to the same field.
982 EscapedIFieldClobberKey key2 = { reg, type, field_id };
983 return escaped_ifield_clobber_set_.find(key2) == escaped_ifield_clobber_set_.end();
Vladimir Marko2ac01fc2014-05-22 12:09:08 +0100984}
985
Vladimir Marko95a05972014-05-30 10:01:32 +0100986bool LocalValueNumbering::IsNonAliasingArray(uint16_t reg, uint16_t type) const {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +0100987 if (IsNonAliasing(reg)) {
988 return true;
989 }
Vladimir Marko95a05972014-05-30 10:01:32 +0100990 if (escaped_refs_.count(reg) == 0u) {
991 return false;
992 }
993 // Check for aliased APUTs.
994 EscapedArrayClobberKey key = { reg, type };
995 return escaped_array_clobber_set_.find(key) == escaped_array_clobber_set_.end();
Vladimir Marko2ac01fc2014-05-22 12:09:08 +0100996}
997
Vladimir Markof59f18b2014-02-17 15:53:57 +0000998void LocalValueNumbering::HandleNullCheck(MIR* mir, uint16_t reg) {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +0100999 auto lb = null_checked_.lower_bound(reg);
1000 if (lb != null_checked_.end() && *lb == reg) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001001 if (LIKELY(gvn_->CanModify())) {
1002 if (gvn_->GetCompilationUnit()->verbose) {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001003 LOG(INFO) << "Removing null check for 0x" << std::hex << mir->offset;
1004 }
1005 mir->optimization_flags |= MIR_IGNORE_NULL_CHECK;
Vladimir Markof59f18b2014-02-17 15:53:57 +00001006 }
Vladimir Markof59f18b2014-02-17 15:53:57 +00001007 } else {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001008 null_checked_.insert(lb, reg);
Vladimir Markof59f18b2014-02-17 15:53:57 +00001009 }
1010}
1011
1012void LocalValueNumbering::HandleRangeCheck(MIR* mir, uint16_t array, uint16_t index) {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001013 RangeCheckKey key = { array, index };
1014 auto lb = range_checked_.lower_bound(key);
1015 if (lb != range_checked_.end() && !RangeCheckKeyComparator()(key, *lb)) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001016 if (LIKELY(gvn_->CanModify())) {
1017 if (gvn_->GetCompilationUnit()->verbose) {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001018 LOG(INFO) << "Removing range check for 0x" << std::hex << mir->offset;
1019 }
1020 mir->optimization_flags |= MIR_IGNORE_RANGE_CHECK;
Vladimir Markof59f18b2014-02-17 15:53:57 +00001021 }
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001022 } else {
1023 // Mark range check completed.
1024 range_checked_.insert(lb, key);
Vladimir Markof59f18b2014-02-17 15:53:57 +00001025 }
Vladimir Markof59f18b2014-02-17 15:53:57 +00001026}
1027
1028void LocalValueNumbering::HandlePutObject(MIR* mir) {
1029 // If we're storing a non-aliasing reference, stop tracking it as non-aliasing now.
1030 uint16_t base = GetOperandValue(mir->ssa_rep->uses[0]);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001031 HandleEscapingRef(base);
1032}
1033
1034void LocalValueNumbering::HandleEscapingRef(uint16_t base) {
1035 auto it = non_aliasing_refs_.find(base);
1036 if (it != non_aliasing_refs_.end()) {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001037 non_aliasing_refs_.erase(it);
Vladimir Marko95a05972014-05-30 10:01:32 +01001038 escaped_refs_.insert(base);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001039 }
1040}
1041
Vladimir Marko95a05972014-05-30 10:01:32 +01001042uint16_t LocalValueNumbering::HandlePhi(MIR* mir) {
1043 if (gvn_->merge_lvns_.empty()) {
1044 // Running LVN without a full GVN?
1045 return kNoValue;
1046 }
1047 int16_t num_uses = mir->ssa_rep->num_uses;
1048 int32_t* uses = mir->ssa_rep->uses;
1049 // Try to find out if this is merging wide regs.
1050 if (mir->ssa_rep->defs[0] != 0 &&
1051 sreg_wide_value_map_.count(mir->ssa_rep->defs[0] - 1) != 0u) {
1052 // This is the high part of a wide reg. Ignore the Phi.
1053 return kNoValue;
1054 }
1055 bool wide = false;
1056 for (int16_t i = 0; i != num_uses; ++i) {
1057 if (sreg_wide_value_map_.count(uses[i]) != 0u) {
1058 wide = true;
1059 break;
1060 }
1061 }
1062 // Iterate over *merge_lvns_ and skip incoming sregs for BBs without associated LVN.
1063 uint16_t value_name = kNoValue;
1064 merge_names_.clear();
1065 BasicBlockId* incoming = mir->meta.phi_incoming;
1066 int16_t pos = 0;
1067 bool same_values = true;
1068 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
1069 DCHECK_LT(pos, mir->ssa_rep->num_uses);
1070 while (incoming[pos] != lvn->Id()) {
1071 ++pos;
1072 DCHECK_LT(pos, mir->ssa_rep->num_uses);
1073 }
1074 int s_reg = uses[pos];
1075 ++pos;
1076 value_name = wide ? lvn->GetOperandValueWide(s_reg) : lvn->GetOperandValue(s_reg);
1077
1078 same_values = same_values && (merge_names_.empty() || value_name == merge_names_.back());
1079 merge_names_.push_back(value_name);
1080 }
1081 if (same_values) {
1082 // value_name already contains the result.
1083 } else {
1084 auto lb = merge_map_.lower_bound(merge_names_);
1085 if (lb != merge_map_.end() && !merge_map_.key_comp()(merge_names_, lb->first)) {
1086 value_name = lb->second;
1087 } else {
1088 value_name = gvn_->LookupValue(kNoValue, mir->ssa_rep->defs[0], kNoValue, kNoValue);
1089 merge_map_.PutBefore(lb, merge_names_, value_name);
1090 if (!wide && gvn_->NullCheckedInAllPredecessors(merge_names_)) {
1091 null_checked_.insert(value_name);
1092 }
1093 }
1094 }
1095 if (wide) {
1096 SetOperandValueWide(mir->ssa_rep->defs[0], value_name);
1097 } else {
1098 SetOperandValue(mir->ssa_rep->defs[0], value_name);
1099 }
1100 return value_name;
1101}
1102
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001103uint16_t LocalValueNumbering::HandleAGet(MIR* mir, uint16_t opcode) {
1104 // uint16_t type = opcode - Instruction::AGET;
1105 uint16_t array = GetOperandValue(mir->ssa_rep->uses[0]);
1106 HandleNullCheck(mir, array);
1107 uint16_t index = GetOperandValue(mir->ssa_rep->uses[1]);
1108 HandleRangeCheck(mir, array, index);
1109 uint16_t type = opcode - Instruction::AGET;
1110 // Establish value number for loaded register.
1111 uint16_t res;
1112 if (IsNonAliasingArray(array, type)) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001113 res = HandleAliasingValuesGet<NonAliasingArrayVersions>(&non_aliasing_array_value_map_,
1114 array, index);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001115 } else {
Vladimir Marko95a05972014-05-30 10:01:32 +01001116 uint16_t location = gvn_->GetArrayLocation(array, index);
1117 res = HandleAliasingValuesGet<AliasingArrayVersions>(&aliasing_array_value_map_,
1118 type, location);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001119 }
1120 if (opcode == Instruction::AGET_WIDE) {
1121 SetOperandValueWide(mir->ssa_rep->defs[0], res);
1122 } else {
1123 SetOperandValue(mir->ssa_rep->defs[0], res);
1124 }
1125 return res;
1126}
1127
1128void LocalValueNumbering::HandleAPut(MIR* mir, uint16_t opcode) {
1129 int array_idx = (opcode == Instruction::APUT_WIDE) ? 2 : 1;
1130 int index_idx = array_idx + 1;
1131 uint16_t array = GetOperandValue(mir->ssa_rep->uses[array_idx]);
1132 HandleNullCheck(mir, array);
1133 uint16_t index = GetOperandValue(mir->ssa_rep->uses[index_idx]);
1134 HandleRangeCheck(mir, array, index);
1135
1136 uint16_t type = opcode - Instruction::APUT;
1137 uint16_t value = (opcode == Instruction::APUT_WIDE)
1138 ? GetOperandValueWide(mir->ssa_rep->uses[0])
1139 : GetOperandValue(mir->ssa_rep->uses[0]);
1140 if (IsNonAliasing(array)) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001141 bool put_is_live = HandleAliasingValuesPut<NonAliasingArrayVersions>(
1142 &non_aliasing_array_value_map_, array, index, value);
1143 if (!put_is_live) {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001144 // This APUT can be eliminated, it stores the same value that's already in the field.
1145 // TODO: Eliminate the APUT.
1146 return;
1147 }
Vladimir Marko95a05972014-05-30 10:01:32 +01001148 } else {
1149 uint16_t location = gvn_->GetArrayLocation(array, index);
1150 bool put_is_live = HandleAliasingValuesPut<AliasingArrayVersions>(
1151 &aliasing_array_value_map_, type, location, value);
1152 if (!put_is_live) {
1153 // This APUT can be eliminated, it stores the same value that's already in the field.
1154 // TODO: Eliminate the APUT.
1155 return;
1156 }
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001157
Vladimir Marko95a05972014-05-30 10:01:32 +01001158 // Clobber all escaped array refs for this type.
1159 for (uint16_t escaped_array : escaped_refs_) {
1160 EscapedArrayClobberKey clobber_key = { escaped_array, type };
1161 escaped_array_clobber_set_.insert(clobber_key);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001162 }
1163 }
1164}
1165
1166uint16_t LocalValueNumbering::HandleIGet(MIR* mir, uint16_t opcode) {
1167 uint16_t base = GetOperandValue(mir->ssa_rep->uses[0]);
1168 HandleNullCheck(mir, base);
Vladimir Marko95a05972014-05-30 10:01:32 +01001169 const MirFieldInfo& field_info = gvn_->GetMirGraph()->GetIFieldLoweringInfo(mir);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001170 uint16_t res;
1171 if (!field_info.IsResolved() || field_info.IsVolatile()) {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001172 // Unresolved fields may be volatile, so handle them as such to be safe.
Vladimir Markofa236452014-09-29 17:58:10 +01001173 HandleInvokeOrClInitOrAcquireOp(mir); // Volatile GETs have acquire semantics.
1174 // Volatile fields always get a new memory version; field id is irrelevant.
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001175 // Use result s_reg - will be unique.
Vladimir Marko95a05972014-05-30 10:01:32 +01001176 res = gvn_->LookupValue(kNoValue, mir->ssa_rep->defs[0], kNoValue, kNoValue);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001177 } else {
1178 uint16_t type = opcode - Instruction::IGET;
Vladimir Marko95a05972014-05-30 10:01:32 +01001179 uint16_t field_id = gvn_->GetFieldId(field_info, type);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001180 if (IsNonAliasingIField(base, field_id, type)) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001181 uint16_t loc = gvn_->LookupValue(kNonAliasingIFieldLocOp, base, field_id, type);
1182 auto lb = non_aliasing_ifield_value_map_.lower_bound(loc);
1183 if (lb != non_aliasing_ifield_value_map_.end() && lb->first == loc) {
1184 res = lb->second;
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001185 } else {
Vladimir Marko95a05972014-05-30 10:01:32 +01001186 res = gvn_->LookupValue(kNonAliasingIFieldInitialOp, loc, kNoValue, kNoValue);
1187 non_aliasing_ifield_value_map_.PutBefore(lb, loc, res);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001188 }
Vladimir Marko95a05972014-05-30 10:01:32 +01001189 } else {
1190 res = HandleAliasingValuesGet<AliasingIFieldVersions>(&aliasing_ifield_value_map_,
1191 field_id, base);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001192 }
1193 }
1194 if (opcode == Instruction::IGET_WIDE) {
1195 SetOperandValueWide(mir->ssa_rep->defs[0], res);
1196 } else {
1197 SetOperandValue(mir->ssa_rep->defs[0], res);
1198 }
1199 return res;
1200}
1201
1202void LocalValueNumbering::HandleIPut(MIR* mir, uint16_t opcode) {
1203 uint16_t type = opcode - Instruction::IPUT;
1204 int base_reg = (opcode == Instruction::IPUT_WIDE) ? 2 : 1;
1205 uint16_t base = GetOperandValue(mir->ssa_rep->uses[base_reg]);
1206 HandleNullCheck(mir, base);
Vladimir Marko95a05972014-05-30 10:01:32 +01001207 const MirFieldInfo& field_info = gvn_->GetMirGraph()->GetIFieldLoweringInfo(mir);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001208 if (!field_info.IsResolved()) {
1209 // Unresolved fields always alias with everything of the same type.
1210 // Use mir->offset as modifier; without elaborate inlining, it will be unique.
1211 unresolved_ifield_version_[type] =
Vladimir Marko95a05972014-05-30 10:01:32 +01001212 gvn_->LookupValue(kUnresolvedIFieldOp, kNoValue, kNoValue, mir->offset);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001213
Vladimir Marko95a05972014-05-30 10:01:32 +01001214 // For simplicity, treat base as escaped now.
1215 HandleEscapingRef(base);
1216
1217 // Clobber all fields of escaped references of the same type.
1218 for (uint16_t escaped_ref : escaped_refs_) {
1219 EscapedIFieldClobberKey clobber_key = { escaped_ref, type, kNoValue };
1220 escaped_ifield_clobber_set_.insert(clobber_key);
1221 }
1222
1223 // Aliasing fields of the same type may have been overwritten.
1224 auto it = aliasing_ifield_value_map_.begin(), end = aliasing_ifield_value_map_.end();
1225 while (it != end) {
1226 if (gvn_->GetFieldType(it->first) != type) {
1227 ++it;
1228 } else {
1229 it = aliasing_ifield_value_map_.erase(it);
1230 }
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001231 }
1232 } else if (field_info.IsVolatile()) {
1233 // Nothing to do, resolved volatile fields always get a new memory version anyway and
1234 // can't alias with resolved non-volatile fields.
1235 } else {
Vladimir Marko95a05972014-05-30 10:01:32 +01001236 uint16_t field_id = gvn_->GetFieldId(field_info, type);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001237 uint16_t value = (opcode == Instruction::IPUT_WIDE)
1238 ? GetOperandValueWide(mir->ssa_rep->uses[0])
1239 : GetOperandValue(mir->ssa_rep->uses[0]);
1240 if (IsNonAliasing(base)) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001241 uint16_t loc = gvn_->LookupValue(kNonAliasingIFieldLocOp, base, field_id, type);
1242 auto lb = non_aliasing_ifield_value_map_.lower_bound(loc);
1243 if (lb != non_aliasing_ifield_value_map_.end() && lb->first == loc) {
1244 if (lb->second == value) {
1245 // This IPUT can be eliminated, it stores the same value that's already in the field.
1246 // TODO: Eliminate the IPUT.
1247 return;
1248 }
1249 lb->second = value; // Overwrite.
1250 } else {
1251 non_aliasing_ifield_value_map_.PutBefore(lb, loc, value);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001252 }
Vladimir Marko95a05972014-05-30 10:01:32 +01001253 } else {
1254 bool put_is_live = HandleAliasingValuesPut<AliasingIFieldVersions>(
1255 &aliasing_ifield_value_map_, field_id, base, value);
1256 if (!put_is_live) {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001257 // This IPUT can be eliminated, it stores the same value that's already in the field.
1258 // TODO: Eliminate the IPUT.
1259 return;
1260 }
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001261
Vladimir Marko95a05972014-05-30 10:01:32 +01001262 // Clobber all fields of escaped references for this field.
1263 for (uint16_t escaped_ref : escaped_refs_) {
1264 EscapedIFieldClobberKey clobber_key = { escaped_ref, type, field_id };
1265 escaped_ifield_clobber_set_.insert(clobber_key);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001266 }
1267 }
1268 }
1269}
1270
1271uint16_t LocalValueNumbering::HandleSGet(MIR* mir, uint16_t opcode) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001272 const MirSFieldLoweringInfo& field_info = gvn_->GetMirGraph()->GetSFieldLoweringInfo(mir);
Vladimir Markofa236452014-09-29 17:58:10 +01001273 if (!field_info.IsResolved() || field_info.IsVolatile() ||
1274 (!field_info.IsInitialized() && (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0)) {
1275 // Volatile SGETs (and unresolved fields are potentially volatile) have acquire semantics
1276 // and class initialization can call arbitrary functions, we need to wipe aliasing values.
1277 HandleInvokeOrClInitOrAcquireOp(mir);
Vladimir Markof418f322014-07-09 14:45:36 +01001278 }
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001279 uint16_t res;
1280 if (!field_info.IsResolved() || field_info.IsVolatile()) {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001281 // Unresolved fields may be volatile, so handle them as such to be safe.
Vladimir Markofa236452014-09-29 17:58:10 +01001282 // Volatile fields always get a new memory version; field id is irrelevant.
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001283 // Use result s_reg - will be unique.
Vladimir Marko95a05972014-05-30 10:01:32 +01001284 res = gvn_->LookupValue(kNoValue, mir->ssa_rep->defs[0], kNoValue, kNoValue);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001285 } else {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001286 uint16_t type = opcode - Instruction::SGET;
Vladimir Marko95a05972014-05-30 10:01:32 +01001287 uint16_t field_id = gvn_->GetFieldId(field_info, type);
1288 auto lb = sfield_value_map_.lower_bound(field_id);
1289 if (lb != sfield_value_map_.end() && lb->first == field_id) {
1290 res = lb->second;
1291 } else {
1292 // Resolved non-volatile static fields can alias with non-resolved fields of the same type,
1293 // so we need to use unresolved_sfield_version_[type] in addition to global_memory_version_
1294 // to determine the version of the field.
1295 res = gvn_->LookupValue(kResolvedSFieldOp, field_id,
1296 unresolved_sfield_version_[type], global_memory_version_);
1297 sfield_value_map_.PutBefore(lb, field_id, res);
1298 }
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001299 }
1300 if (opcode == Instruction::SGET_WIDE) {
1301 SetOperandValueWide(mir->ssa_rep->defs[0], res);
1302 } else {
1303 SetOperandValue(mir->ssa_rep->defs[0], res);
1304 }
1305 return res;
1306}
1307
1308void LocalValueNumbering::HandleSPut(MIR* mir, uint16_t opcode) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001309 const MirSFieldLoweringInfo& field_info = gvn_->GetMirGraph()->GetSFieldLoweringInfo(mir);
Vladimir Markof418f322014-07-09 14:45:36 +01001310 if (!field_info.IsInitialized() && (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
1311 // Class initialization can call arbitrary functions, we need to wipe aliasing values.
Vladimir Markofa236452014-09-29 17:58:10 +01001312 HandleInvokeOrClInitOrAcquireOp(mir);
Vladimir Markof418f322014-07-09 14:45:36 +01001313 }
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001314 uint16_t type = opcode - Instruction::SPUT;
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001315 if (!field_info.IsResolved()) {
1316 // Unresolved fields always alias with everything of the same type.
1317 // Use mir->offset as modifier; without elaborate inlining, it will be unique.
1318 unresolved_sfield_version_[type] =
Vladimir Marko95a05972014-05-30 10:01:32 +01001319 gvn_->LookupValue(kUnresolvedSFieldOp, kNoValue, kNoValue, mir->offset);
1320 RemoveSFieldsForType(type);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001321 } else if (field_info.IsVolatile()) {
1322 // Nothing to do, resolved volatile fields always get a new memory version anyway and
1323 // can't alias with resolved non-volatile fields.
1324 } else {
Vladimir Marko95a05972014-05-30 10:01:32 +01001325 uint16_t field_id = gvn_->GetFieldId(field_info, type);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001326 uint16_t value = (opcode == Instruction::SPUT_WIDE)
1327 ? GetOperandValueWide(mir->ssa_rep->uses[0])
1328 : GetOperandValue(mir->ssa_rep->uses[0]);
1329 // Resolved non-volatile static fields can alias with non-resolved fields of the same type,
1330 // so we need to use unresolved_sfield_version_[type] in addition to global_memory_version_
1331 // to determine the version of the field.
Vladimir Marko95a05972014-05-30 10:01:32 +01001332 auto lb = sfield_value_map_.lower_bound(field_id);
1333 if (lb != sfield_value_map_.end() && lb->first == field_id) {
1334 if (lb->second == value) {
1335 // This SPUT can be eliminated, it stores the same value that's already in the field.
1336 // TODO: Eliminate the SPUT.
1337 return;
1338 }
1339 lb->second = value; // Overwrite.
1340 } else {
1341 sfield_value_map_.PutBefore(lb, field_id, value);
1342 }
1343 }
1344}
1345
1346void LocalValueNumbering::RemoveSFieldsForType(uint16_t type) {
1347 // Erase all static fields of this type from the sfield_value_map_.
1348 for (auto it = sfield_value_map_.begin(), end = sfield_value_map_.end(); it != end; ) {
1349 if (gvn_->GetFieldType(it->first) == type) {
1350 it = sfield_value_map_.erase(it);
1351 } else {
1352 ++it;
1353 }
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001354 }
Vladimir Markof59f18b2014-02-17 15:53:57 +00001355}
buzbee2502e002012-12-31 16:05:53 -08001356
Vladimir Markofa236452014-09-29 17:58:10 +01001357void LocalValueNumbering::HandleInvokeOrClInitOrAcquireOp(MIR* mir) {
Vladimir Markof418f322014-07-09 14:45:36 +01001358 // Use mir->offset as modifier; without elaborate inlining, it will be unique.
Vladimir Marko95a05972014-05-30 10:01:32 +01001359 global_memory_version_ =
1360 gvn_->LookupValue(kInvokeMemoryVersionBumpOp, 0u, 0u, mir->offset);
1361 // All static fields and instance fields and array elements of aliasing references,
1362 // including escaped references, may have been modified.
1363 sfield_value_map_.clear();
1364 aliasing_ifield_value_map_.clear();
1365 aliasing_array_value_map_.clear();
1366 escaped_refs_.clear();
1367 escaped_ifield_clobber_set_.clear();
1368 escaped_array_clobber_set_.clear();
Vladimir Markof418f322014-07-09 14:45:36 +01001369}
1370
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001371uint16_t LocalValueNumbering::GetValueNumber(MIR* mir) {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001372 uint16_t res = kNoValue;
buzbee2502e002012-12-31 16:05:53 -08001373 uint16_t opcode = mir->dalvikInsn.opcode;
1374 switch (opcode) {
1375 case Instruction::NOP:
1376 case Instruction::RETURN_VOID:
1377 case Instruction::RETURN:
1378 case Instruction::RETURN_OBJECT:
1379 case Instruction::RETURN_WIDE:
buzbee2502e002012-12-31 16:05:53 -08001380 case Instruction::GOTO:
1381 case Instruction::GOTO_16:
1382 case Instruction::GOTO_32:
1383 case Instruction::CHECK_CAST:
1384 case Instruction::THROW:
1385 case Instruction::FILL_ARRAY_DATA:
buzbee2502e002012-12-31 16:05:53 -08001386 case Instruction::PACKED_SWITCH:
1387 case Instruction::SPARSE_SWITCH:
1388 case Instruction::IF_EQ:
1389 case Instruction::IF_NE:
1390 case Instruction::IF_LT:
1391 case Instruction::IF_GE:
1392 case Instruction::IF_GT:
1393 case Instruction::IF_LE:
1394 case Instruction::IF_EQZ:
1395 case Instruction::IF_NEZ:
1396 case Instruction::IF_LTZ:
1397 case Instruction::IF_GEZ:
1398 case Instruction::IF_GTZ:
1399 case Instruction::IF_LEZ:
buzbee2502e002012-12-31 16:05:53 -08001400 case kMirOpFusedCmplFloat:
1401 case kMirOpFusedCmpgFloat:
1402 case kMirOpFusedCmplDouble:
1403 case kMirOpFusedCmpgDouble:
1404 case kMirOpFusedCmpLong:
1405 // Nothing defined - take no action.
1406 break;
1407
Vladimir Marko95a05972014-05-30 10:01:32 +01001408 case Instruction::MONITOR_ENTER:
1409 HandleNullCheck(mir, GetOperandValue(mir->ssa_rep->uses[0]));
Vladimir Markofa236452014-09-29 17:58:10 +01001410 HandleInvokeOrClInitOrAcquireOp(mir); // Acquire operation.
Vladimir Marko95a05972014-05-30 10:01:32 +01001411 break;
1412
1413 case Instruction::MONITOR_EXIT:
1414 HandleNullCheck(mir, GetOperandValue(mir->ssa_rep->uses[0]));
1415 // If we're running GVN and CanModify(), uneliminated null check indicates bytecode error.
Vladimir Marko415ac882014-09-30 18:09:14 +01001416 if ((mir->optimization_flags & MIR_IGNORE_NULL_CHECK) == 0 &&
1417 gvn_->work_lvn_ != nullptr && gvn_->CanModify()) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001418 LOG(WARNING) << "Bytecode error: MONITOR_EXIT is still null checked at 0x" << std::hex
1419 << mir->offset << " in " << PrettyMethod(gvn_->cu_->method_idx, *gvn_->cu_->dex_file);
1420 }
1421 break;
1422
Vladimir Markof59f18b2014-02-17 15:53:57 +00001423 case Instruction::FILLED_NEW_ARRAY:
1424 case Instruction::FILLED_NEW_ARRAY_RANGE:
1425 // Nothing defined but the result will be unique and non-null.
1426 if (mir->next != nullptr && mir->next->dalvikInsn.opcode == Instruction::MOVE_RESULT_OBJECT) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001427 uint16_t array = MarkNonAliasingNonNull(mir->next);
1428 // Do not SetOperandValue(), we'll do that when we process the MOVE_RESULT_OBJECT.
1429 if (kLocalValueNumberingEnableFilledNewArrayTracking && mir->ssa_rep->num_uses != 0u) {
1430 AliasingValues* values = GetAliasingValues(&non_aliasing_array_value_map_, array);
1431 // Clear the value if we got a merged version in a loop.
Vladimir Markob19955d2014-07-29 12:04:10 +01001432 *values = AliasingValues(this);
Vladimir Marko95a05972014-05-30 10:01:32 +01001433 for (size_t i = 0u, count = mir->ssa_rep->num_uses; i != count; ++i) {
1434 DCHECK_EQ(High16Bits(i), 0u);
1435 uint16_t index = gvn_->LookupValue(Instruction::CONST, i, 0u, 0);
1436 uint16_t value = GetOperandValue(mir->ssa_rep->uses[i]);
1437 values->load_value_map.Put(index, value);
1438 RangeCheckKey key = { array, index };
1439 range_checked_.insert(key);
1440 }
1441 }
Vladimir Markof59f18b2014-02-17 15:53:57 +00001442 // The MOVE_RESULT_OBJECT will be processed next and we'll return the value name then.
1443 }
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001444 // All args escaped (if references).
1445 for (size_t i = 0u, count = mir->ssa_rep->num_uses; i != count; ++i) {
1446 uint16_t reg = GetOperandValue(mir->ssa_rep->uses[i]);
1447 HandleEscapingRef(reg);
1448 }
Vladimir Markof59f18b2014-02-17 15:53:57 +00001449 break;
1450
Vladimir Markoa78e66a2014-10-16 13:38:44 +01001451 case kMirOpNullCheck:
1452 HandleNullCheck(mir, GetOperandValue(mir->ssa_rep->uses[0]));
1453 break;
1454
Vladimir Markof59f18b2014-02-17 15:53:57 +00001455 case Instruction::INVOKE_DIRECT:
1456 case Instruction::INVOKE_DIRECT_RANGE:
1457 case Instruction::INVOKE_VIRTUAL:
1458 case Instruction::INVOKE_VIRTUAL_RANGE:
1459 case Instruction::INVOKE_SUPER:
1460 case Instruction::INVOKE_SUPER_RANGE:
1461 case Instruction::INVOKE_INTERFACE:
1462 case Instruction::INVOKE_INTERFACE_RANGE: {
1463 // Nothing defined but handle the null check.
1464 uint16_t reg = GetOperandValue(mir->ssa_rep->uses[0]);
1465 HandleNullCheck(mir, reg);
1466 }
Ian Rogersfc787ec2014-10-09 21:56:44 -07001467 FALLTHROUGH_INTENDED;
Vladimir Markof59f18b2014-02-17 15:53:57 +00001468 case Instruction::INVOKE_STATIC:
1469 case Instruction::INVOKE_STATIC_RANGE:
Vladimir Markoff0ac472014-10-02 17:24:53 +01001470 // Make ref args aliasing.
1471 for (size_t i = 0u, count = mir->ssa_rep->num_uses; i != count; ++i) {
1472 uint16_t reg = GetOperandValue(mir->ssa_rep->uses[i]);
1473 non_aliasing_refs_.erase(reg);
Vladimir Marko9820b7c2014-01-02 16:40:37 +00001474 }
Vladimir Markoff0ac472014-10-02 17:24:53 +01001475 HandleInvokeOrClInitOrAcquireOp(mir);
Vladimir Markof59f18b2014-02-17 15:53:57 +00001476 break;
1477
buzbee2502e002012-12-31 16:05:53 -08001478 case Instruction::MOVE_RESULT:
1479 case Instruction::MOVE_RESULT_OBJECT:
1480 case Instruction::INSTANCE_OF:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001481 // 1 result, treat as unique each time, use result s_reg - will be unique.
1482 res = GetOperandValue(mir->ssa_rep->defs[0]);
1483 SetOperandValue(mir->ssa_rep->defs[0], res);
1484 break;
1485 case Instruction::MOVE_EXCEPTION:
buzbee2502e002012-12-31 16:05:53 -08001486 case Instruction::NEW_INSTANCE:
buzbee2502e002012-12-31 16:05:53 -08001487 case Instruction::CONST_CLASS:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001488 case Instruction::NEW_ARRAY:
Vladimir Markob3e527b2014-04-04 12:37:07 +01001489 // 1 result, treat as unique each time, use result s_reg - will be unique.
1490 res = MarkNonAliasingNonNull(mir);
Vladimir Marko95a05972014-05-30 10:01:32 +01001491 SetOperandValue(mir->ssa_rep->defs[0], res);
buzbee2502e002012-12-31 16:05:53 -08001492 break;
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001493 case Instruction::CONST_STRING:
1494 case Instruction::CONST_STRING_JUMBO:
1495 // These strings are internalized, so assign value based on the string pool index.
Vladimir Marko95a05972014-05-30 10:01:32 +01001496 res = gvn_->LookupValue(Instruction::CONST_STRING, Low16Bits(mir->dalvikInsn.vB),
1497 High16Bits(mir->dalvikInsn.vB), 0);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001498 SetOperandValue(mir->ssa_rep->defs[0], res);
1499 null_checked_.insert(res); // May already be there.
1500 // NOTE: Hacking the contents of an internalized string via reflection is possible
1501 // but the behavior is undefined. Therefore, we consider the string constant and
1502 // the reference non-aliasing.
1503 // TUNING: We could keep this property even if the reference "escapes".
1504 non_aliasing_refs_.insert(res); // May already be there.
1505 break;
Vladimir Markof59f18b2014-02-17 15:53:57 +00001506 case Instruction::MOVE_RESULT_WIDE:
Vladimir Markob3e527b2014-04-04 12:37:07 +01001507 // 1 wide result, treat as unique each time, use result s_reg - will be unique.
1508 res = GetOperandValueWide(mir->ssa_rep->defs[0]);
1509 SetOperandValueWide(mir->ssa_rep->defs[0], res);
buzbee2502e002012-12-31 16:05:53 -08001510 break;
1511
1512 case kMirOpPhi:
Vladimir Marko95a05972014-05-30 10:01:32 +01001513 res = HandlePhi(mir);
buzbee2502e002012-12-31 16:05:53 -08001514 break;
1515
1516 case Instruction::MOVE:
1517 case Instruction::MOVE_OBJECT:
1518 case Instruction::MOVE_16:
1519 case Instruction::MOVE_OBJECT_16:
1520 case Instruction::MOVE_FROM16:
1521 case Instruction::MOVE_OBJECT_FROM16:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001522 case kMirOpCopy:
1523 // Just copy value number of source to value number of result.
1524 res = GetOperandValue(mir->ssa_rep->uses[0]);
1525 SetOperandValue(mir->ssa_rep->defs[0], res);
buzbee2502e002012-12-31 16:05:53 -08001526 break;
1527
1528 case Instruction::MOVE_WIDE:
1529 case Instruction::MOVE_WIDE_16:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001530 case Instruction::MOVE_WIDE_FROM16:
1531 // Just copy value number of source to value number of result.
1532 res = GetOperandValueWide(mir->ssa_rep->uses[0]);
1533 SetOperandValueWide(mir->ssa_rep->defs[0], res);
buzbee2502e002012-12-31 16:05:53 -08001534 break;
1535
1536 case Instruction::CONST:
1537 case Instruction::CONST_4:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001538 case Instruction::CONST_16:
Vladimir Marko95a05972014-05-30 10:01:32 +01001539 res = gvn_->LookupValue(Instruction::CONST, Low16Bits(mir->dalvikInsn.vB),
1540 High16Bits(mir->dalvikInsn.vB), 0);
Vladimir Markof59f18b2014-02-17 15:53:57 +00001541 SetOperandValue(mir->ssa_rep->defs[0], res);
buzbee2502e002012-12-31 16:05:53 -08001542 break;
1543
Vladimir Markof59f18b2014-02-17 15:53:57 +00001544 case Instruction::CONST_HIGH16:
Vladimir Marko95a05972014-05-30 10:01:32 +01001545 res = gvn_->LookupValue(Instruction::CONST, 0, mir->dalvikInsn.vB, 0);
Vladimir Markof59f18b2014-02-17 15:53:57 +00001546 SetOperandValue(mir->ssa_rep->defs[0], res);
buzbee2502e002012-12-31 16:05:53 -08001547 break;
1548
1549 case Instruction::CONST_WIDE_16:
1550 case Instruction::CONST_WIDE_32: {
Vladimir Marko95a05972014-05-30 10:01:32 +01001551 uint16_t low_res = gvn_->LookupValue(Instruction::CONST, Low16Bits(mir->dalvikInsn.vB),
1552 High16Bits(mir->dalvikInsn.vB >> 16), 1);
buzbee2502e002012-12-31 16:05:53 -08001553 uint16_t high_res;
1554 if (mir->dalvikInsn.vB & 0x80000000) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001555 high_res = gvn_->LookupValue(Instruction::CONST, 0xffff, 0xffff, 2);
buzbee2502e002012-12-31 16:05:53 -08001556 } else {
Vladimir Marko95a05972014-05-30 10:01:32 +01001557 high_res = gvn_->LookupValue(Instruction::CONST, 0, 0, 2);
buzbee2502e002012-12-31 16:05:53 -08001558 }
Vladimir Marko95a05972014-05-30 10:01:32 +01001559 res = gvn_->LookupValue(Instruction::CONST, low_res, high_res, 3);
Vladimir Markof59f18b2014-02-17 15:53:57 +00001560 SetOperandValueWide(mir->ssa_rep->defs[0], res);
buzbee2502e002012-12-31 16:05:53 -08001561 }
1562 break;
1563
1564 case Instruction::CONST_WIDE: {
1565 uint32_t low_word = Low32Bits(mir->dalvikInsn.vB_wide);
1566 uint32_t high_word = High32Bits(mir->dalvikInsn.vB_wide);
Vladimir Marko95a05972014-05-30 10:01:32 +01001567 uint16_t low_res = gvn_->LookupValue(Instruction::CONST, Low16Bits(low_word),
1568 High16Bits(low_word), 1);
1569 uint16_t high_res = gvn_->LookupValue(Instruction::CONST, Low16Bits(high_word),
1570 High16Bits(high_word), 2);
1571 res = gvn_->LookupValue(Instruction::CONST, low_res, high_res, 3);
buzbee2502e002012-12-31 16:05:53 -08001572 SetOperandValueWide(mir->ssa_rep->defs[0], res);
1573 }
1574 break;
1575
1576 case Instruction::CONST_WIDE_HIGH16: {
Vladimir Marko95a05972014-05-30 10:01:32 +01001577 uint16_t low_res = gvn_->LookupValue(Instruction::CONST, 0, 0, 1);
1578 uint16_t high_res = gvn_->LookupValue(Instruction::CONST, 0,
1579 Low16Bits(mir->dalvikInsn.vB), 2);
1580 res = gvn_->LookupValue(Instruction::CONST, low_res, high_res, 3);
buzbee2502e002012-12-31 16:05:53 -08001581 SetOperandValueWide(mir->ssa_rep->defs[0], res);
1582 }
1583 break;
1584
Vladimir Marko95a05972014-05-30 10:01:32 +01001585 case Instruction::ARRAY_LENGTH: {
1586 // Handle the null check.
1587 uint16_t reg = GetOperandValue(mir->ssa_rep->uses[0]);
1588 HandleNullCheck(mir, reg);
1589 }
Ian Rogersfc787ec2014-10-09 21:56:44 -07001590 FALLTHROUGH_INTENDED;
buzbee2502e002012-12-31 16:05:53 -08001591 case Instruction::NEG_INT:
1592 case Instruction::NOT_INT:
1593 case Instruction::NEG_FLOAT:
1594 case Instruction::INT_TO_BYTE:
1595 case Instruction::INT_TO_SHORT:
1596 case Instruction::INT_TO_CHAR:
1597 case Instruction::INT_TO_FLOAT:
1598 case Instruction::FLOAT_TO_INT: {
1599 // res = op + 1 operand
1600 uint16_t operand1 = GetOperandValue(mir->ssa_rep->uses[0]);
Vladimir Marko95a05972014-05-30 10:01:32 +01001601 res = gvn_->LookupValue(opcode, operand1, kNoValue, kNoValue);
buzbee2502e002012-12-31 16:05:53 -08001602 SetOperandValue(mir->ssa_rep->defs[0], res);
1603 }
1604 break;
1605
1606 case Instruction::LONG_TO_FLOAT:
1607 case Instruction::LONG_TO_INT:
1608 case Instruction::DOUBLE_TO_FLOAT:
1609 case Instruction::DOUBLE_TO_INT: {
1610 // res = op + 1 wide operand
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001611 uint16_t operand1 = GetOperandValueWide(mir->ssa_rep->uses[0]);
Vladimir Marko95a05972014-05-30 10:01:32 +01001612 res = gvn_->LookupValue(opcode, operand1, kNoValue, kNoValue);
buzbee2502e002012-12-31 16:05:53 -08001613 SetOperandValue(mir->ssa_rep->defs[0], res);
1614 }
1615 break;
1616
buzbee2502e002012-12-31 16:05:53 -08001617 case Instruction::DOUBLE_TO_LONG:
1618 case Instruction::LONG_TO_DOUBLE:
1619 case Instruction::NEG_LONG:
1620 case Instruction::NOT_LONG:
1621 case Instruction::NEG_DOUBLE: {
1622 // wide res = op + 1 wide operand
1623 uint16_t operand1 = GetOperandValueWide(mir->ssa_rep->uses[0]);
Vladimir Marko95a05972014-05-30 10:01:32 +01001624 res = gvn_->LookupValue(opcode, operand1, kNoValue, kNoValue);
buzbee2502e002012-12-31 16:05:53 -08001625 SetOperandValueWide(mir->ssa_rep->defs[0], res);
1626 }
1627 break;
1628
1629 case Instruction::FLOAT_TO_DOUBLE:
1630 case Instruction::FLOAT_TO_LONG:
1631 case Instruction::INT_TO_DOUBLE:
1632 case Instruction::INT_TO_LONG: {
1633 // wide res = op + 1 operand
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001634 uint16_t operand1 = GetOperandValue(mir->ssa_rep->uses[0]);
Vladimir Marko95a05972014-05-30 10:01:32 +01001635 res = gvn_->LookupValue(opcode, operand1, kNoValue, kNoValue);
buzbee2502e002012-12-31 16:05:53 -08001636 SetOperandValueWide(mir->ssa_rep->defs[0], res);
1637 }
1638 break;
1639
1640 case Instruction::CMPL_DOUBLE:
1641 case Instruction::CMPG_DOUBLE:
1642 case Instruction::CMP_LONG: {
1643 // res = op + 2 wide operands
1644 uint16_t operand1 = GetOperandValueWide(mir->ssa_rep->uses[0]);
1645 uint16_t operand2 = GetOperandValueWide(mir->ssa_rep->uses[2]);
Vladimir Marko95a05972014-05-30 10:01:32 +01001646 res = gvn_->LookupValue(opcode, operand1, operand2, kNoValue);
buzbee2502e002012-12-31 16:05:53 -08001647 SetOperandValue(mir->ssa_rep->defs[0], res);
1648 }
1649 break;
1650
1651 case Instruction::CMPG_FLOAT:
1652 case Instruction::CMPL_FLOAT:
1653 case Instruction::ADD_INT:
1654 case Instruction::ADD_INT_2ADDR:
1655 case Instruction::MUL_INT:
1656 case Instruction::MUL_INT_2ADDR:
1657 case Instruction::AND_INT:
1658 case Instruction::AND_INT_2ADDR:
1659 case Instruction::OR_INT:
1660 case Instruction::OR_INT_2ADDR:
1661 case Instruction::XOR_INT:
1662 case Instruction::XOR_INT_2ADDR:
1663 case Instruction::SUB_INT:
1664 case Instruction::SUB_INT_2ADDR:
1665 case Instruction::DIV_INT:
1666 case Instruction::DIV_INT_2ADDR:
1667 case Instruction::REM_INT:
1668 case Instruction::REM_INT_2ADDR:
1669 case Instruction::SHL_INT:
1670 case Instruction::SHL_INT_2ADDR:
1671 case Instruction::SHR_INT:
1672 case Instruction::SHR_INT_2ADDR:
1673 case Instruction::USHR_INT:
1674 case Instruction::USHR_INT_2ADDR: {
1675 // res = op + 2 operands
1676 uint16_t operand1 = GetOperandValue(mir->ssa_rep->uses[0]);
1677 uint16_t operand2 = GetOperandValue(mir->ssa_rep->uses[1]);
Vladimir Marko95a05972014-05-30 10:01:32 +01001678 res = gvn_->LookupValue(opcode, operand1, operand2, kNoValue);
buzbee2502e002012-12-31 16:05:53 -08001679 SetOperandValue(mir->ssa_rep->defs[0], res);
1680 }
1681 break;
1682
1683 case Instruction::ADD_LONG:
1684 case Instruction::SUB_LONG:
1685 case Instruction::MUL_LONG:
1686 case Instruction::DIV_LONG:
1687 case Instruction::REM_LONG:
1688 case Instruction::AND_LONG:
1689 case Instruction::OR_LONG:
1690 case Instruction::XOR_LONG:
1691 case Instruction::ADD_LONG_2ADDR:
1692 case Instruction::SUB_LONG_2ADDR:
1693 case Instruction::MUL_LONG_2ADDR:
1694 case Instruction::DIV_LONG_2ADDR:
1695 case Instruction::REM_LONG_2ADDR:
1696 case Instruction::AND_LONG_2ADDR:
1697 case Instruction::OR_LONG_2ADDR:
1698 case Instruction::XOR_LONG_2ADDR:
1699 case Instruction::ADD_DOUBLE:
1700 case Instruction::SUB_DOUBLE:
1701 case Instruction::MUL_DOUBLE:
1702 case Instruction::DIV_DOUBLE:
1703 case Instruction::REM_DOUBLE:
1704 case Instruction::ADD_DOUBLE_2ADDR:
1705 case Instruction::SUB_DOUBLE_2ADDR:
1706 case Instruction::MUL_DOUBLE_2ADDR:
1707 case Instruction::DIV_DOUBLE_2ADDR:
1708 case Instruction::REM_DOUBLE_2ADDR: {
1709 // wide res = op + 2 wide operands
1710 uint16_t operand1 = GetOperandValueWide(mir->ssa_rep->uses[0]);
1711 uint16_t operand2 = GetOperandValueWide(mir->ssa_rep->uses[2]);
Vladimir Marko95a05972014-05-30 10:01:32 +01001712 res = gvn_->LookupValue(opcode, operand1, operand2, kNoValue);
buzbee2502e002012-12-31 16:05:53 -08001713 SetOperandValueWide(mir->ssa_rep->defs[0], res);
1714 }
1715 break;
1716
1717 case Instruction::SHL_LONG:
1718 case Instruction::SHR_LONG:
1719 case Instruction::USHR_LONG:
1720 case Instruction::SHL_LONG_2ADDR:
1721 case Instruction::SHR_LONG_2ADDR:
1722 case Instruction::USHR_LONG_2ADDR: {
1723 // wide res = op + 1 wide operand + 1 operand
1724 uint16_t operand1 = GetOperandValueWide(mir->ssa_rep->uses[0]);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001725 uint16_t operand2 = GetOperandValue(mir->ssa_rep->uses[2]);
Vladimir Marko95a05972014-05-30 10:01:32 +01001726 res = gvn_->LookupValue(opcode, operand1, operand2, kNoValue);
buzbee2502e002012-12-31 16:05:53 -08001727 SetOperandValueWide(mir->ssa_rep->defs[0], res);
1728 }
1729 break;
1730
1731 case Instruction::ADD_FLOAT:
1732 case Instruction::SUB_FLOAT:
1733 case Instruction::MUL_FLOAT:
1734 case Instruction::DIV_FLOAT:
1735 case Instruction::REM_FLOAT:
1736 case Instruction::ADD_FLOAT_2ADDR:
1737 case Instruction::SUB_FLOAT_2ADDR:
1738 case Instruction::MUL_FLOAT_2ADDR:
1739 case Instruction::DIV_FLOAT_2ADDR:
1740 case Instruction::REM_FLOAT_2ADDR: {
1741 // res = op + 2 operands
1742 uint16_t operand1 = GetOperandValue(mir->ssa_rep->uses[0]);
1743 uint16_t operand2 = GetOperandValue(mir->ssa_rep->uses[1]);
Vladimir Marko95a05972014-05-30 10:01:32 +01001744 res = gvn_->LookupValue(opcode, operand1, operand2, kNoValue);
buzbee2502e002012-12-31 16:05:53 -08001745 SetOperandValue(mir->ssa_rep->defs[0], res);
1746 }
1747 break;
1748
1749 case Instruction::RSUB_INT:
1750 case Instruction::ADD_INT_LIT16:
1751 case Instruction::MUL_INT_LIT16:
1752 case Instruction::DIV_INT_LIT16:
1753 case Instruction::REM_INT_LIT16:
1754 case Instruction::AND_INT_LIT16:
1755 case Instruction::OR_INT_LIT16:
1756 case Instruction::XOR_INT_LIT16:
1757 case Instruction::ADD_INT_LIT8:
1758 case Instruction::RSUB_INT_LIT8:
1759 case Instruction::MUL_INT_LIT8:
1760 case Instruction::DIV_INT_LIT8:
1761 case Instruction::REM_INT_LIT8:
1762 case Instruction::AND_INT_LIT8:
1763 case Instruction::OR_INT_LIT8:
1764 case Instruction::XOR_INT_LIT8:
1765 case Instruction::SHL_INT_LIT8:
1766 case Instruction::SHR_INT_LIT8:
1767 case Instruction::USHR_INT_LIT8: {
nikolay serdjukee40aa42014-03-25 12:21:29 +07001768 // Same as res = op + 2 operands, except use vC as operand 2
buzbee2502e002012-12-31 16:05:53 -08001769 uint16_t operand1 = GetOperandValue(mir->ssa_rep->uses[0]);
Vladimir Marko95a05972014-05-30 10:01:32 +01001770 uint16_t operand2 = gvn_->LookupValue(Instruction::CONST, mir->dalvikInsn.vC, 0, 0);
1771 res = gvn_->LookupValue(opcode, operand1, operand2, kNoValue);
buzbee2502e002012-12-31 16:05:53 -08001772 SetOperandValue(mir->ssa_rep->defs[0], res);
1773 }
1774 break;
1775
buzbee2502e002012-12-31 16:05:53 -08001776 case Instruction::AGET_OBJECT:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001777 case Instruction::AGET:
1778 case Instruction::AGET_WIDE:
buzbee2502e002012-12-31 16:05:53 -08001779 case Instruction::AGET_BOOLEAN:
1780 case Instruction::AGET_BYTE:
1781 case Instruction::AGET_CHAR:
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001782 case Instruction::AGET_SHORT:
1783 res = HandleAGet(mir, opcode);
buzbee2502e002012-12-31 16:05:53 -08001784 break;
1785
buzbee2502e002012-12-31 16:05:53 -08001786 case Instruction::APUT_OBJECT:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001787 HandlePutObject(mir);
Ian Rogersfc787ec2014-10-09 21:56:44 -07001788 FALLTHROUGH_INTENDED;
Vladimir Markof59f18b2014-02-17 15:53:57 +00001789 case Instruction::APUT:
1790 case Instruction::APUT_WIDE:
buzbee2502e002012-12-31 16:05:53 -08001791 case Instruction::APUT_BYTE:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001792 case Instruction::APUT_BOOLEAN:
1793 case Instruction::APUT_SHORT:
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001794 case Instruction::APUT_CHAR:
1795 HandleAPut(mir, opcode);
buzbee2502e002012-12-31 16:05:53 -08001796 break;
1797
1798 case Instruction::IGET_OBJECT:
buzbee2502e002012-12-31 16:05:53 -08001799 case Instruction::IGET:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001800 case Instruction::IGET_WIDE:
buzbee2502e002012-12-31 16:05:53 -08001801 case Instruction::IGET_BOOLEAN:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001802 case Instruction::IGET_BYTE:
1803 case Instruction::IGET_CHAR:
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001804 case Instruction::IGET_SHORT:
1805 res = HandleIGet(mir, opcode);
buzbee2502e002012-12-31 16:05:53 -08001806 break;
1807
buzbee2502e002012-12-31 16:05:53 -08001808 case Instruction::IPUT_OBJECT:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001809 HandlePutObject(mir);
Ian Rogersfc787ec2014-10-09 21:56:44 -07001810 FALLTHROUGH_INTENDED;
buzbee2502e002012-12-31 16:05:53 -08001811 case Instruction::IPUT:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001812 case Instruction::IPUT_WIDE:
buzbee2502e002012-12-31 16:05:53 -08001813 case Instruction::IPUT_BOOLEAN:
1814 case Instruction::IPUT_BYTE:
1815 case Instruction::IPUT_CHAR:
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001816 case Instruction::IPUT_SHORT:
1817 HandleIPut(mir, opcode);
buzbee2502e002012-12-31 16:05:53 -08001818 break;
1819
1820 case Instruction::SGET_OBJECT:
1821 case Instruction::SGET:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001822 case Instruction::SGET_WIDE:
buzbee2502e002012-12-31 16:05:53 -08001823 case Instruction::SGET_BOOLEAN:
1824 case Instruction::SGET_BYTE:
1825 case Instruction::SGET_CHAR:
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001826 case Instruction::SGET_SHORT:
1827 res = HandleSGet(mir, opcode);
buzbee2502e002012-12-31 16:05:53 -08001828 break;
1829
1830 case Instruction::SPUT_OBJECT:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001831 HandlePutObject(mir);
Ian Rogersfc787ec2014-10-09 21:56:44 -07001832 FALLTHROUGH_INTENDED;
buzbee2502e002012-12-31 16:05:53 -08001833 case Instruction::SPUT:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001834 case Instruction::SPUT_WIDE:
buzbee2502e002012-12-31 16:05:53 -08001835 case Instruction::SPUT_BOOLEAN:
1836 case Instruction::SPUT_BYTE:
1837 case Instruction::SPUT_CHAR:
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001838 case Instruction::SPUT_SHORT:
1839 HandleSPut(mir, opcode);
buzbee2502e002012-12-31 16:05:53 -08001840 break;
buzbee2502e002012-12-31 16:05:53 -08001841 }
1842 return res;
1843}
1844
1845} // namespace art