blob: d6776807775c9a4444ef50eea58e52e2083fe2b7 [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) {
Vladimir Markoaf6925b2014-10-31 16:37:32 +000059 uint16_t type = gvn->GetIFieldType(field_id);
Vladimir Marko95a05972014-05-30 10:01:32 +010060 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.
Vladimir Markoaf6925b2014-10-31 16:37:32 +000078 uint16_t type = gvn->GetIFieldType(field_id);
Vladimir Marko95a05972014-05-30 10:01:32 +010079 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) {
Vladimir Markoaf6925b2014-10-31 16:37:32 +000092 uint16_t type = gvn->GetIFieldType(field_id);
Vladimir Marko95a05972014-05-30 10:01:32 +010093 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:
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700110 static uint16_t StartMemoryVersion(GlobalValueNumbering* gvn,
111 const LocalValueNumbering* lvn ATTRIBUTE_UNUSED,
Vladimir Marko95a05972014-05-30 10:01:32 +0100112 uint16_t array) {
113 return gvn->LookupValue(kNonAliasingArrayStartVersionOp, array, kNoValue, kNoValue);
114 }
115
116 static uint16_t BumpMemoryVersion(GlobalValueNumbering* gvn, uint16_t old_version,
117 uint16_t store_ref_set_id, uint16_t stored_value) {
118 return gvn->LookupValue(kNonAliasingArrayBumpVersionOp, old_version,
119 store_ref_set_id, stored_value);
120 }
121
122 static uint16_t LookupGlobalValue(GlobalValueNumbering* gvn,
123 uint16_t array, uint16_t index, uint16_t memory_version) {
124 return gvn->LookupValue(kNonAliasingArrayOp, array, index, memory_version);
125 }
126
127 static uint16_t LookupMergeValue(GlobalValueNumbering* gvn, const LocalValueNumbering* lvn,
128 uint16_t array, uint16_t index) {
129 return AliasingValuesMergeGet<NonAliasingArrayVersions>(
130 gvn, lvn, &lvn->non_aliasing_array_value_map_, array, index);
131 }
132
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700133 static bool HasNewBaseVersion(GlobalValueNumbering* gvn ATTRIBUTE_UNUSED,
134 const LocalValueNumbering* lvn ATTRIBUTE_UNUSED,
135 uint16_t array ATTRIBUTE_UNUSED) {
Vladimir Marko95a05972014-05-30 10:01:32 +0100136 return false; // Not affected by global_memory_version_.
137 }
138
139 static uint16_t LookupMergeBlockValue(GlobalValueNumbering* gvn, uint16_t lvn_id,
140 uint16_t array) {
141 return gvn->LookupValue(kMergeBlockNonAliasingArrayVersionBumpOp, array, kNoValue, lvn_id);
142 }
143
144 static uint16_t LookupMergeLocationValue(GlobalValueNumbering* gvn, uint16_t lvn_id,
145 uint16_t array, uint16_t index) {
146 return gvn->LookupValue(kMergeBlockNonAliasingArrayMergeLocationOp, array, index, lvn_id);
147 }
148};
149
150class LocalValueNumbering::AliasingArrayVersions {
151 public:
152 static uint16_t StartMemoryVersion(GlobalValueNumbering* gvn, const LocalValueNumbering* lvn,
153 uint16_t type) {
154 return gvn->LookupValue(kAliasingArrayStartVersionOp, type, lvn->global_memory_version_,
155 kNoValue);
156 }
157
158 static uint16_t BumpMemoryVersion(GlobalValueNumbering* gvn, uint16_t old_version,
159 uint16_t store_ref_set_id, uint16_t stored_value) {
160 return gvn->LookupValue(kAliasingArrayBumpVersionOp, old_version,
161 store_ref_set_id, stored_value);
162 }
163
164 static uint16_t LookupGlobalValue(GlobalValueNumbering* gvn,
165 uint16_t type, uint16_t location, uint16_t memory_version) {
166 return gvn->LookupValue(kAliasingArrayOp, type, location, memory_version);
167 }
168
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700169 static uint16_t LookupMergeValue(GlobalValueNumbering* gvn ATTRIBUTE_UNUSED,
170 const LocalValueNumbering* lvn,
171 uint16_t type ATTRIBUTE_UNUSED, uint16_t location) {
Vladimir Marko95a05972014-05-30 10:01:32 +0100172 // If the location is non-aliasing in lvn, use the non-aliasing value.
173 uint16_t array = gvn->GetArrayLocationBase(location);
174 if (lvn->IsNonAliasingArray(array, type)) {
175 uint16_t index = gvn->GetArrayLocationIndex(location);
176 return NonAliasingArrayVersions::LookupMergeValue(gvn, lvn, array, index);
177 }
178 return AliasingValuesMergeGet<AliasingArrayVersions>(
179 gvn, lvn, &lvn->aliasing_array_value_map_, type, location);
180 }
181
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700182 static bool HasNewBaseVersion(GlobalValueNumbering* gvn ATTRIBUTE_UNUSED,
183 const LocalValueNumbering* lvn,
184 uint16_t type ATTRIBUTE_UNUSED) {
185 UNUSED(gvn);
186 UNUSED(type);
Vladimir Marko95a05972014-05-30 10:01:32 +0100187 return lvn->global_memory_version_ == lvn->merge_new_memory_version_;
188 }
189
190 static uint16_t LookupMergeBlockValue(GlobalValueNumbering* gvn, uint16_t lvn_id,
191 uint16_t type) {
192 return gvn->LookupValue(kMergeBlockAliasingArrayVersionBumpOp, type, kNoValue, lvn_id);
193 }
194
195 static uint16_t LookupMergeLocationValue(GlobalValueNumbering* gvn, uint16_t lvn_id,
196 uint16_t type, uint16_t location) {
197 return gvn->LookupValue(kMergeBlockAliasingArrayMergeLocationOp, type, location, lvn_id);
198 }
199};
200
201template <typename Map>
202LocalValueNumbering::AliasingValues* LocalValueNumbering::GetAliasingValues(
203 Map* map, const typename Map::key_type& key) {
204 auto lb = map->lower_bound(key);
205 if (lb == map->end() || map->key_comp()(key, lb->first)) {
Vladimir Markob19955d2014-07-29 12:04:10 +0100206 lb = map->PutBefore(lb, key, AliasingValues(this));
Vladimir Marko95a05972014-05-30 10:01:32 +0100207 }
208 return &lb->second;
Vladimir Marko2ac01fc2014-05-22 12:09:08 +0100209}
210
Vladimir Marko95a05972014-05-30 10:01:32 +0100211template <typename Versions, typename KeyType>
212void LocalValueNumbering::UpdateAliasingValuesLoadVersion(const KeyType& key,
213 AliasingValues* values) {
214 if (values->last_load_memory_version == kNoValue) {
215 // Get the start version that accounts for aliasing with unresolved fields of the same
216 // type and make it unique for the field by including the field_id.
217 uint16_t memory_version = values->memory_version_before_stores;
218 if (memory_version == kNoValue) {
219 memory_version = Versions::StartMemoryVersion(gvn_, this, key);
220 }
221 if (!values->store_loc_set.empty()) {
222 uint16_t ref_set_id = gvn_->GetRefSetId(values->store_loc_set);
223 memory_version = Versions::BumpMemoryVersion(gvn_, memory_version, ref_set_id,
224 values->last_stored_value);
225 }
226 values->last_load_memory_version = memory_version;
Vladimir Markof59f18b2014-02-17 15:53:57 +0000227 }
Vladimir Marko95a05972014-05-30 10:01:32 +0100228}
229
230template <typename Versions, typename Map>
231uint16_t LocalValueNumbering::AliasingValuesMergeGet(GlobalValueNumbering* gvn,
232 const LocalValueNumbering* lvn,
233 Map* map, const typename Map::key_type& key,
234 uint16_t location) {
235 // Retrieve the value name that we would get from
236 // const_cast<LocalValueNumbering*>(lvn)->HandleAliasingValueGet(map. key, location)
237 // but don't modify the map.
238 uint16_t value_name;
239 auto it = map->find(key);
240 if (it == map->end()) {
241 uint16_t start_version = Versions::StartMemoryVersion(gvn, lvn, key);
242 value_name = Versions::LookupGlobalValue(gvn, key, location, start_version);
243 } else if (it->second.store_loc_set.count(location) != 0u) {
244 value_name = it->second.last_stored_value;
245 } else {
246 auto load_it = it->second.load_value_map.find(location);
247 if (load_it != it->second.load_value_map.end()) {
248 value_name = load_it->second;
249 } else {
250 value_name = Versions::LookupGlobalValue(gvn, key, location, it->second.last_load_memory_version);
251 }
252 }
253 return value_name;
254}
255
256template <typename Versions, typename Map>
257uint16_t LocalValueNumbering::HandleAliasingValuesGet(Map* map, const typename Map::key_type& key,
258 uint16_t location) {
259 // Retrieve the value name for IGET/SGET/AGET, update the map with new value if any.
260 uint16_t res;
261 AliasingValues* values = GetAliasingValues(map, key);
262 if (values->store_loc_set.count(location) != 0u) {
263 res = values->last_stored_value;
264 } else {
265 UpdateAliasingValuesLoadVersion<Versions>(key, values);
266 auto lb = values->load_value_map.lower_bound(location);
267 if (lb != values->load_value_map.end() && lb->first == location) {
268 res = lb->second;
269 } else {
270 res = Versions::LookupGlobalValue(gvn_, key, location, values->last_load_memory_version);
271 values->load_value_map.PutBefore(lb, location, res);
272 }
273 }
274 return res;
275}
276
277template <typename Versions, typename Map>
278bool LocalValueNumbering::HandleAliasingValuesPut(Map* map, const typename Map::key_type& key,
279 uint16_t location, uint16_t value) {
280 AliasingValues* values = GetAliasingValues(map, key);
281 auto load_values_it = values->load_value_map.find(location);
282 if (load_values_it != values->load_value_map.end() && load_values_it->second == value) {
283 // This insn can be eliminated, it stores the same value that's already in the field.
284 return false;
285 }
286 if (value == values->last_stored_value) {
287 auto store_loc_lb = values->store_loc_set.lower_bound(location);
288 if (store_loc_lb != values->store_loc_set.end() && *store_loc_lb == location) {
289 // This insn can be eliminated, it stores the same value that's already in the field.
290 return false;
291 }
292 values->store_loc_set.emplace_hint(store_loc_lb, location);
293 } else {
294 UpdateAliasingValuesLoadVersion<Versions>(key, values);
295 values->memory_version_before_stores = values->last_load_memory_version;
296 values->last_stored_value = value;
297 values->store_loc_set.clear();
298 values->store_loc_set.insert(location);
299 }
300 // Clear the last load memory version and remove all potentially overwritten values.
301 values->last_load_memory_version = kNoValue;
302 auto it = values->load_value_map.begin(), end = values->load_value_map.end();
303 while (it != end) {
304 if (it->second == value) {
305 ++it;
306 } else {
307 it = values->load_value_map.erase(it);
308 }
309 }
310 return true;
311}
312
Vladimir Markob19955d2014-07-29 12:04:10 +0100313template <typename K>
314void LocalValueNumbering::CopyAliasingValuesMap(ScopedArenaSafeMap<K, AliasingValues>* dest,
315 const ScopedArenaSafeMap<K, AliasingValues>& src) {
316 // We need each new AliasingValues (or rather its map members) to be constructed
317 // with our allocator, rather than the allocator of the source.
318 for (const auto& entry : src) {
319 auto it = dest->PutBefore(dest->end(), entry.first, AliasingValues(this));
320 it->second = entry.second; // Map assignments preserve current allocator.
321 }
322}
323
324LocalValueNumbering::LocalValueNumbering(GlobalValueNumbering* gvn, uint16_t id,
325 ScopedArenaAllocator* allocator)
Vladimir Marko95a05972014-05-30 10:01:32 +0100326 : gvn_(gvn),
327 id_(id),
Vladimir Markob19955d2014-07-29 12:04:10 +0100328 sreg_value_map_(std::less<uint16_t>(), allocator->Adapter()),
329 sreg_wide_value_map_(std::less<uint16_t>(), allocator->Adapter()),
330 sfield_value_map_(std::less<uint16_t>(), allocator->Adapter()),
331 non_aliasing_ifield_value_map_(std::less<uint16_t>(), allocator->Adapter()),
332 aliasing_ifield_value_map_(std::less<uint16_t>(), allocator->Adapter()),
333 non_aliasing_array_value_map_(std::less<uint16_t>(), allocator->Adapter()),
334 aliasing_array_value_map_(std::less<uint16_t>(), allocator->Adapter()),
Vladimir Marko95a05972014-05-30 10:01:32 +0100335 global_memory_version_(0u),
Vladimir Markob19955d2014-07-29 12:04:10 +0100336 non_aliasing_refs_(std::less<uint16_t>(), allocator->Adapter()),
337 escaped_refs_(std::less<uint16_t>(), allocator->Adapter()),
338 escaped_ifield_clobber_set_(EscapedIFieldClobberKeyComparator(), allocator->Adapter()),
339 escaped_array_clobber_set_(EscapedArrayClobberKeyComparator(), allocator->Adapter()),
340 range_checked_(RangeCheckKeyComparator() , allocator->Adapter()),
341 null_checked_(std::less<uint16_t>(), allocator->Adapter()),
Razvan A Lupusorue0951142014-11-14 14:36:55 -0800342 div_zero_checked_(std::less<uint16_t>(), allocator->Adapter()),
Vladimir Markob19955d2014-07-29 12:04:10 +0100343 merge_names_(allocator->Adapter()),
344 merge_map_(std::less<ScopedArenaVector<BasicBlockId>>(), allocator->Adapter()),
Vladimir Marko95a05972014-05-30 10:01:32 +0100345 merge_new_memory_version_(kNoValue) {
Vladimir Marko321b9872014-11-24 16:33:51 +0000346 std::fill_n(unresolved_sfield_version_, arraysize(unresolved_sfield_version_), 0u);
347 std::fill_n(unresolved_ifield_version_, arraysize(unresolved_ifield_version_), 0u);
Vladimir Marko95a05972014-05-30 10:01:32 +0100348}
349
350bool LocalValueNumbering::Equals(const LocalValueNumbering& other) const {
351 DCHECK(gvn_ == other.gvn_);
352 // Compare the maps/sets and memory versions.
353 return sreg_value_map_ == other.sreg_value_map_ &&
354 sreg_wide_value_map_ == other.sreg_wide_value_map_ &&
355 sfield_value_map_ == other.sfield_value_map_ &&
356 non_aliasing_ifield_value_map_ == other.non_aliasing_ifield_value_map_ &&
357 aliasing_ifield_value_map_ == other.aliasing_ifield_value_map_ &&
358 non_aliasing_array_value_map_ == other.non_aliasing_array_value_map_ &&
359 aliasing_array_value_map_ == other.aliasing_array_value_map_ &&
360 SameMemoryVersion(other) &&
361 non_aliasing_refs_ == other.non_aliasing_refs_ &&
362 escaped_refs_ == other.escaped_refs_ &&
363 escaped_ifield_clobber_set_ == other.escaped_ifield_clobber_set_ &&
364 escaped_array_clobber_set_ == other.escaped_array_clobber_set_ &&
365 range_checked_ == other.range_checked_ &&
Razvan A Lupusorue0951142014-11-14 14:36:55 -0800366 null_checked_ == other.null_checked_ &&
367 div_zero_checked_ == other.div_zero_checked_;
Vladimir Marko95a05972014-05-30 10:01:32 +0100368}
369
370void LocalValueNumbering::MergeOne(const LocalValueNumbering& other, MergeType merge_type) {
Vladimir Markob19955d2014-07-29 12:04:10 +0100371 CopyLiveSregValues(&sreg_value_map_, other.sreg_value_map_);
372 CopyLiveSregValues(&sreg_wide_value_map_, other.sreg_wide_value_map_);
Vladimir Marko95a05972014-05-30 10:01:32 +0100373
374 if (merge_type == kReturnMerge) {
375 // RETURN or PHI+RETURN. We need only sreg value maps.
376 return;
377 }
378
379 non_aliasing_ifield_value_map_ = other.non_aliasing_ifield_value_map_;
Vladimir Markob19955d2014-07-29 12:04:10 +0100380 CopyAliasingValuesMap(&non_aliasing_array_value_map_, other.non_aliasing_array_value_map_);
Vladimir Marko95a05972014-05-30 10:01:32 +0100381 non_aliasing_refs_ = other.non_aliasing_refs_;
382 range_checked_ = other.range_checked_;
383 null_checked_ = other.null_checked_;
Razvan A Lupusorue0951142014-11-14 14:36:55 -0800384 div_zero_checked_ = other.div_zero_checked_;
Vladimir Marko95a05972014-05-30 10:01:32 +0100385
Vladimir Markoa4426cf2014-10-22 17:15:53 +0100386 const BasicBlock* pred_bb = gvn_->GetBasicBlock(other.Id());
387 if (GlobalValueNumbering::HasNullCheckLastInsn(pred_bb, Id())) {
388 int s_reg = pred_bb->last_mir_insn->ssa_rep->uses[0];
389 null_checked_.insert(other.GetOperandValue(s_reg));
390 }
391
Vladimir Marko95a05972014-05-30 10:01:32 +0100392 if (merge_type == kCatchMerge) {
393 // Memory is clobbered. Use new memory version and don't merge aliasing locations.
394 global_memory_version_ = NewMemoryVersion(&merge_new_memory_version_);
Vladimir Marko321b9872014-11-24 16:33:51 +0000395 std::fill_n(unresolved_sfield_version_, arraysize(unresolved_sfield_version_),
396 global_memory_version_);
397 std::fill_n(unresolved_ifield_version_, arraysize(unresolved_ifield_version_),
398 global_memory_version_);
Vladimir Marko95a05972014-05-30 10:01:32 +0100399 PruneNonAliasingRefsForCatch();
400 return;
401 }
402
403 DCHECK(merge_type == kNormalMerge);
404 global_memory_version_ = other.global_memory_version_;
Vladimir Marko321b9872014-11-24 16:33:51 +0000405 std::copy_n(other.unresolved_ifield_version_, arraysize(unresolved_sfield_version_),
406 unresolved_ifield_version_);
407 std::copy_n(other.unresolved_sfield_version_, arraysize(unresolved_ifield_version_),
408 unresolved_sfield_version_);
Vladimir Marko95a05972014-05-30 10:01:32 +0100409 sfield_value_map_ = other.sfield_value_map_;
Vladimir Markob19955d2014-07-29 12:04:10 +0100410 CopyAliasingValuesMap(&aliasing_ifield_value_map_, other.aliasing_ifield_value_map_);
411 CopyAliasingValuesMap(&aliasing_array_value_map_, other.aliasing_array_value_map_);
Vladimir Marko95a05972014-05-30 10:01:32 +0100412 escaped_refs_ = other.escaped_refs_;
413 escaped_ifield_clobber_set_ = other.escaped_ifield_clobber_set_;
414 escaped_array_clobber_set_ = other.escaped_array_clobber_set_;
415}
416
417bool LocalValueNumbering::SameMemoryVersion(const LocalValueNumbering& other) const {
418 return
419 global_memory_version_ == other.global_memory_version_ &&
Vladimir Marko321b9872014-11-24 16:33:51 +0000420 std::equal(unresolved_ifield_version_,
421 unresolved_ifield_version_ + arraysize(unresolved_ifield_version_),
Vladimir Marko95a05972014-05-30 10:01:32 +0100422 other.unresolved_ifield_version_) &&
Vladimir Marko321b9872014-11-24 16:33:51 +0000423 std::equal(unresolved_sfield_version_,
424 unresolved_sfield_version_ + arraysize(unresolved_sfield_version_),
Vladimir Marko95a05972014-05-30 10:01:32 +0100425 other.unresolved_sfield_version_);
426}
427
428uint16_t LocalValueNumbering::NewMemoryVersion(uint16_t* new_version) {
429 if (*new_version == kNoValue) {
430 *new_version = gvn_->LookupValue(kMergeBlockMemoryVersionBumpOp, 0u, 0u, id_);
431 }
432 return *new_version;
433}
434
435void LocalValueNumbering::MergeMemoryVersions(bool clobbered_catch) {
436 DCHECK_GE(gvn_->merge_lvns_.size(), 2u);
437 const LocalValueNumbering* cmp = gvn_->merge_lvns_[0];
438 // Check if the global version has changed.
439 bool new_global_version = clobbered_catch;
440 if (!new_global_version) {
441 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
442 if (lvn->global_memory_version_ != cmp->global_memory_version_) {
443 // Use a new version for everything.
444 new_global_version = true;
445 break;
446 }
447 }
448 }
449 if (new_global_version) {
450 global_memory_version_ = NewMemoryVersion(&merge_new_memory_version_);
Vladimir Marko321b9872014-11-24 16:33:51 +0000451 std::fill_n(unresolved_sfield_version_, arraysize(unresolved_sfield_version_),
452 merge_new_memory_version_);
453 std::fill_n(unresolved_ifield_version_, arraysize(unresolved_ifield_version_),
454 merge_new_memory_version_);
Vladimir Marko95a05972014-05-30 10:01:32 +0100455 } else {
456 // Initialize with a copy of memory versions from the comparison LVN.
457 global_memory_version_ = cmp->global_memory_version_;
Vladimir Marko321b9872014-11-24 16:33:51 +0000458 std::copy_n(cmp->unresolved_ifield_version_, arraysize(unresolved_sfield_version_),
459 unresolved_ifield_version_);
460 std::copy_n(cmp->unresolved_sfield_version_, arraysize(unresolved_ifield_version_),
461 unresolved_sfield_version_);
Vladimir Marko95a05972014-05-30 10:01:32 +0100462 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
463 if (lvn == cmp) {
464 continue;
465 }
Vladimir Marko321b9872014-11-24 16:33:51 +0000466 for (size_t i = 0; i != kDexMemAccessTypeCount; ++i) {
Vladimir Marko95a05972014-05-30 10:01:32 +0100467 if (lvn->unresolved_ifield_version_[i] != cmp->unresolved_ifield_version_[i]) {
468 unresolved_ifield_version_[i] = NewMemoryVersion(&merge_new_memory_version_);
469 }
470 if (lvn->unresolved_sfield_version_[i] != cmp->unresolved_sfield_version_[i]) {
471 unresolved_sfield_version_[i] = NewMemoryVersion(&merge_new_memory_version_);
472 }
473 }
474 }
475 }
476}
477
478void LocalValueNumbering::PruneNonAliasingRefsForCatch() {
479 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
480 const BasicBlock* bb = gvn_->GetBasicBlock(lvn->Id());
Vladimir Marko11ca6122014-07-17 20:50:07 +0100481 if (UNLIKELY(bb->taken == id_) || UNLIKELY(bb->fall_through == id_)) {
482 // Non-exceptional path to a catch handler means that the catch block was actually
483 // empty and all exceptional paths lead to the shared path after that empty block.
484 continue;
485 }
Vladimir Marko95a05972014-05-30 10:01:32 +0100486 DCHECK_EQ(bb->taken, kNullBlock);
487 DCHECK_NE(bb->fall_through, kNullBlock);
488 const BasicBlock* fall_through_bb = gvn_->GetBasicBlock(bb->fall_through);
489 const MIR* mir = fall_through_bb->first_mir_insn;
490 DCHECK(mir != nullptr);
491 // Only INVOKEs can leak and clobber non-aliasing references if they throw.
Jean Christophe Beylerfb0ea2d2014-07-29 13:20:42 -0700492 if ((mir->dalvikInsn.FlagsOf() & Instruction::kInvoke) != 0) {
Vladimir Markoa4426cf2014-10-22 17:15:53 +0100493 HandleInvokeArgs(mir, lvn);
Vladimir Marko95a05972014-05-30 10:01:32 +0100494 }
495 }
496}
497
498
499template <typename Set, Set LocalValueNumbering::* set_ptr>
500void LocalValueNumbering::IntersectSets() {
501 DCHECK_GE(gvn_->merge_lvns_.size(), 2u);
502
503 // Find the LVN with the least entries in the set.
504 const LocalValueNumbering* least_entries_lvn = gvn_->merge_lvns_[0];
505 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
506 if ((lvn->*set_ptr).size() < (least_entries_lvn->*set_ptr).size()) {
507 least_entries_lvn = lvn;
508 }
509 }
510
511 // For each key check if it's in all the LVNs.
512 for (const auto& key : least_entries_lvn->*set_ptr) {
513 bool checked = true;
514 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
515 if (lvn != least_entries_lvn && (lvn->*set_ptr).count(key) == 0u) {
516 checked = false;
517 break;
518 }
519 }
520 if (checked) {
521 (this->*set_ptr).emplace_hint((this->*set_ptr).end(), key);
522 }
523 }
524}
525
Vladimir Markob19955d2014-07-29 12:04:10 +0100526void LocalValueNumbering::CopyLiveSregValues(SregValueMap* dest, const SregValueMap& src) {
527 auto dest_end = dest->end();
528 ArenaBitVector* live_in_v = gvn_->GetMirGraph()->GetBasicBlock(id_)->data_flow_info->live_in_v;
529 DCHECK(live_in_v != nullptr);
530 for (const auto& entry : src) {
531 bool live = live_in_v->IsBitSet(gvn_->GetMirGraph()->SRegToVReg(entry.first));
532 if (live) {
533 dest->PutBefore(dest_end, entry.first, entry.second);
534 }
535 }
536}
537
538template <LocalValueNumbering::SregValueMap LocalValueNumbering::* map_ptr>
539void LocalValueNumbering::IntersectSregValueMaps() {
Vladimir Marko95a05972014-05-30 10:01:32 +0100540 DCHECK_GE(gvn_->merge_lvns_.size(), 2u);
541
542 // Find the LVN with the least entries in the set.
543 const LocalValueNumbering* least_entries_lvn = gvn_->merge_lvns_[0];
544 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
545 if ((lvn->*map_ptr).size() < (least_entries_lvn->*map_ptr).size()) {
546 least_entries_lvn = lvn;
547 }
548 }
549
550 // For each key check if it's in all the LVNs.
Vladimir Markob19955d2014-07-29 12:04:10 +0100551 ArenaBitVector* live_in_v = gvn_->GetMirGraph()->GetBasicBlock(id_)->data_flow_info->live_in_v;
552 DCHECK(live_in_v != nullptr);
Vladimir Marko95a05972014-05-30 10:01:32 +0100553 for (const auto& entry : least_entries_lvn->*map_ptr) {
Vladimir Markob19955d2014-07-29 12:04:10 +0100554 bool live_and_same = live_in_v->IsBitSet(gvn_->GetMirGraph()->SRegToVReg(entry.first));
555 if (live_and_same) {
556 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
557 if (lvn != least_entries_lvn) {
558 auto it = (lvn->*map_ptr).find(entry.first);
559 if (it == (lvn->*map_ptr).end() || !(it->second == entry.second)) {
560 live_and_same = false;
561 break;
562 }
Vladimir Marko95a05972014-05-30 10:01:32 +0100563 }
564 }
565 }
Vladimir Markob19955d2014-07-29 12:04:10 +0100566 if (live_and_same) {
Vladimir Marko95a05972014-05-30 10:01:32 +0100567 (this->*map_ptr).PutBefore((this->*map_ptr).end(), entry.first, entry.second);
568 }
569 }
570}
571
572// Intersect maps as sets. The value type must be equality-comparable.
573template <typename Map>
574void LocalValueNumbering::InPlaceIntersectMaps(Map* work_map, const Map& other_map) {
575 auto work_it = work_map->begin(), work_end = work_map->end();
576 auto cmp = work_map->value_comp();
577 for (const auto& entry : other_map) {
578 while (work_it != work_end &&
579 (cmp(*work_it, entry) ||
580 (!cmp(entry, *work_it) && !(work_it->second == entry.second)))) {
581 work_it = work_map->erase(work_it);
582 }
Vladimir Marko55fff042014-07-10 12:42:52 +0100583 if (work_it == work_end) {
584 return;
585 }
586 ++work_it;
Vladimir Marko95a05972014-05-30 10:01:32 +0100587 }
588}
589
590template <typename Set, Set LocalValueNumbering::*set_ptr, void (LocalValueNumbering::*MergeFn)(
591 const typename Set::value_type& entry, typename Set::iterator hint)>
592void LocalValueNumbering::MergeSets() {
593 auto cmp = (this->*set_ptr).value_comp();
594 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
595 auto my_it = (this->*set_ptr).begin(), my_end = (this->*set_ptr).end();
596 for (const auto& entry : lvn->*set_ptr) {
597 while (my_it != my_end && cmp(*my_it, entry)) {
598 ++my_it;
599 }
600 if (my_it != my_end && !cmp(entry, *my_it)) {
601 // Already handled.
602 ++my_it;
603 } else {
604 // Merge values for this field_id.
605 (this->*MergeFn)(entry, my_it); // my_it remains valid across inserts to std::set/SafeMap.
606 }
607 }
608 }
609}
610
611void LocalValueNumbering::IntersectAliasingValueLocations(AliasingValues* work_values,
612 const AliasingValues* values) {
613 auto cmp = work_values->load_value_map.key_comp();
614 auto work_it = work_values->load_value_map.begin(), work_end = work_values->load_value_map.end();
615 auto store_it = values->store_loc_set.begin(), store_end = values->store_loc_set.end();
616 auto load_it = values->load_value_map.begin(), load_end = values->load_value_map.end();
617 while (store_it != store_end || load_it != load_end) {
618 uint16_t loc;
619 if (store_it != store_end && (load_it == load_end || *store_it < load_it->first)) {
620 loc = *store_it;
621 ++store_it;
622 } else {
623 loc = load_it->first;
624 ++load_it;
625 DCHECK(store_it == store_end || cmp(loc, *store_it));
626 }
627 while (work_it != work_end && cmp(work_it->first, loc)) {
628 work_it = work_values->load_value_map.erase(work_it);
629 }
630 if (work_it != work_end && !cmp(loc, work_it->first)) {
631 // The location matches, keep it.
632 ++work_it;
633 }
634 }
635 while (work_it != work_end) {
636 work_it = work_values->load_value_map.erase(work_it);
637 }
638}
639
640void LocalValueNumbering::MergeEscapedRefs(const ValueNameSet::value_type& entry,
641 ValueNameSet::iterator hint) {
642 // See if the ref is either escaped or non-aliasing in each predecessor.
643 bool is_escaped = true;
644 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
645 if (lvn->non_aliasing_refs_.count(entry) == 0u &&
646 lvn->escaped_refs_.count(entry) == 0u) {
647 is_escaped = false;
648 break;
649 }
650 }
651 if (is_escaped) {
652 escaped_refs_.emplace_hint(hint, entry);
653 }
654}
655
656void LocalValueNumbering::MergeEscapedIFieldTypeClobberSets(
657 const EscapedIFieldClobberSet::value_type& entry, EscapedIFieldClobberSet::iterator hint) {
658 // Insert only type-clobber entries (field_id == kNoValue) of escaped refs.
659 if (entry.field_id == kNoValue && escaped_refs_.count(entry.base) != 0u) {
660 escaped_ifield_clobber_set_.emplace_hint(hint, entry);
661 }
662}
663
664void LocalValueNumbering::MergeEscapedIFieldClobberSets(
665 const EscapedIFieldClobberSet::value_type& entry, EscapedIFieldClobberSet::iterator hint) {
666 // Insert only those entries of escaped refs that are not overridden by a type clobber.
667 if (!(hint == escaped_ifield_clobber_set_.end() &&
668 hint->base == entry.base && hint->type == entry.type) &&
669 escaped_refs_.count(entry.base) != 0u) {
670 escaped_ifield_clobber_set_.emplace_hint(hint, entry);
671 }
672}
673
674void LocalValueNumbering::MergeEscapedArrayClobberSets(
675 const EscapedArrayClobberSet::value_type& entry, EscapedArrayClobberSet::iterator hint) {
676 if (escaped_refs_.count(entry.base) != 0u) {
677 escaped_array_clobber_set_.emplace_hint(hint, entry);
678 }
679}
680
Vladimir Marko2d2365c2014-08-19 18:08:39 +0100681void LocalValueNumbering::MergeNullChecked() {
682 DCHECK_GE(gvn_->merge_lvns_.size(), 2u);
683
684 // Find the LVN with the least entries in the set.
685 const LocalValueNumbering* least_entries_lvn = gvn_->merge_lvns_[0];
686 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
687 if (lvn->null_checked_.size() < least_entries_lvn->null_checked_.size()) {
688 least_entries_lvn = lvn;
689 }
690 }
691
692 // For each null-checked value name check if it's null-checked in all the LVNs.
693 for (const auto& value_name : least_entries_lvn->null_checked_) {
694 // Merge null_checked_ for this ref.
695 merge_names_.clear();
696 merge_names_.resize(gvn_->merge_lvns_.size(), value_name);
697 if (gvn_->NullCheckedInAllPredecessors(merge_names_)) {
698 null_checked_.insert(null_checked_.end(), value_name);
699 }
700 }
701
702 // Now check if the least_entries_lvn has a null-check as the last insn.
703 const BasicBlock* least_entries_bb = gvn_->GetBasicBlock(least_entries_lvn->Id());
704 if (gvn_->HasNullCheckLastInsn(least_entries_bb, id_)) {
705 int s_reg = least_entries_bb->last_mir_insn->ssa_rep->uses[0];
Vladimir Markoa4426cf2014-10-22 17:15:53 +0100706 uint32_t value_name = least_entries_lvn->GetOperandValue(s_reg);
Vladimir Marko2d2365c2014-08-19 18:08:39 +0100707 merge_names_.clear();
708 merge_names_.resize(gvn_->merge_lvns_.size(), value_name);
709 if (gvn_->NullCheckedInAllPredecessors(merge_names_)) {
710 null_checked_.insert(value_name);
711 }
Vladimir Marko95a05972014-05-30 10:01:32 +0100712 }
713}
714
Razvan A Lupusorue0951142014-11-14 14:36:55 -0800715void LocalValueNumbering::MergeDivZeroChecked() {
716 DCHECK_GE(gvn_->merge_lvns_.size(), 2u);
717
718 // Find the LVN with the least entries in the set.
719 const LocalValueNumbering* least_entries_lvn = gvn_->merge_lvns_[0];
720 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
721 if (lvn->div_zero_checked_.size() < least_entries_lvn->div_zero_checked_.size()) {
722 least_entries_lvn = lvn;
723 }
724 }
725
726 // For each div-zero value name check if it's div-zero checked in all the LVNs.
727 for (const auto& value_name : least_entries_lvn->div_zero_checked_) {
728 // Merge null_checked_ for this ref.
729 merge_names_.clear();
730 merge_names_.resize(gvn_->merge_lvns_.size(), value_name);
731 if (gvn_->DivZeroCheckedInAllPredecessors(merge_names_)) {
732 div_zero_checked_.insert(div_zero_checked_.end(), value_name);
733 }
734 }
735}
736
Vladimir Marko95a05972014-05-30 10:01:32 +0100737void LocalValueNumbering::MergeSFieldValues(const SFieldToValueMap::value_type& entry,
738 SFieldToValueMap::iterator hint) {
739 uint16_t field_id = entry.first;
740 merge_names_.clear();
741 uint16_t value_name = kNoValue;
742 bool same_values = true;
743 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
744 // Get the value name as in HandleSGet() but don't modify *lvn.
745 auto it = lvn->sfield_value_map_.find(field_id);
746 if (it != lvn->sfield_value_map_.end()) {
747 value_name = it->second;
748 } else {
Vladimir Markoaf6925b2014-10-31 16:37:32 +0000749 uint16_t type = gvn_->GetSFieldType(field_id);
Vladimir Marko95a05972014-05-30 10:01:32 +0100750 value_name = gvn_->LookupValue(kResolvedSFieldOp, field_id,
751 lvn->unresolved_sfield_version_[type],
752 lvn->global_memory_version_);
753 }
754
755 same_values = same_values && (merge_names_.empty() || value_name == merge_names_.back());
756 merge_names_.push_back(value_name);
757 }
758 if (same_values) {
759 // value_name already contains the result.
760 } else {
761 auto lb = merge_map_.lower_bound(merge_names_);
762 if (lb != merge_map_.end() && !merge_map_.key_comp()(merge_names_, lb->first)) {
763 value_name = lb->second;
764 } else {
765 value_name = gvn_->LookupValue(kMergeBlockSFieldVersionBumpOp, field_id, id_, kNoValue);
766 merge_map_.PutBefore(lb, merge_names_, value_name);
767 if (gvn_->NullCheckedInAllPredecessors(merge_names_)) {
768 null_checked_.insert(value_name);
769 }
770 }
771 }
772 sfield_value_map_.PutBefore(hint, field_id, value_name);
773}
774
775void LocalValueNumbering::MergeNonAliasingIFieldValues(const IFieldLocToValueMap::value_type& entry,
776 IFieldLocToValueMap::iterator hint) {
777 uint16_t field_loc = entry.first;
778 merge_names_.clear();
779 uint16_t value_name = kNoValue;
780 bool same_values = true;
781 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
782 // Get the value name as in HandleIGet() but don't modify *lvn.
783 auto it = lvn->non_aliasing_ifield_value_map_.find(field_loc);
784 if (it != lvn->non_aliasing_ifield_value_map_.end()) {
785 value_name = it->second;
786 } else {
787 value_name = gvn_->LookupValue(kNonAliasingIFieldInitialOp, field_loc, kNoValue, kNoValue);
788 }
789
790 same_values = same_values && (merge_names_.empty() || value_name == merge_names_.back());
791 merge_names_.push_back(value_name);
792 }
793 if (same_values) {
794 // value_name already contains the result.
795 } else {
796 auto lb = merge_map_.lower_bound(merge_names_);
797 if (lb != merge_map_.end() && !merge_map_.key_comp()(merge_names_, lb->first)) {
798 value_name = lb->second;
799 } else {
800 value_name = gvn_->LookupValue(kMergeBlockNonAliasingIFieldVersionBumpOp, field_loc,
801 id_, kNoValue);
802 merge_map_.PutBefore(lb, merge_names_, value_name);
803 if (gvn_->NullCheckedInAllPredecessors(merge_names_)) {
804 null_checked_.insert(value_name);
805 }
806 }
807 }
808 non_aliasing_ifield_value_map_.PutBefore(hint, field_loc, value_name);
809}
810
811template <typename Map, Map LocalValueNumbering::*map_ptr, typename Versions>
812void LocalValueNumbering::MergeAliasingValues(const typename Map::value_type& entry,
813 typename Map::iterator hint) {
814 const typename Map::key_type& key = entry.first;
815
Vladimir Markob19955d2014-07-29 12:04:10 +0100816 auto it = (this->*map_ptr).PutBefore(hint, key, AliasingValues(this));
Vladimir Marko95a05972014-05-30 10:01:32 +0100817 AliasingValues* my_values = &it->second;
818
819 const AliasingValues* cmp_values = nullptr;
820 bool same_version = !Versions::HasNewBaseVersion(gvn_, this, key);
821 uint16_t load_memory_version_for_same_version = kNoValue;
822 if (same_version) {
823 // Find the first non-null values.
824 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800825 auto value = (lvn->*map_ptr).find(key);
826 if (value != (lvn->*map_ptr).end()) {
827 cmp_values = &value->second;
Vladimir Marko95a05972014-05-30 10:01:32 +0100828 break;
829 }
830 }
831 DCHECK(cmp_values != nullptr); // There must be at least one non-null values.
832
833 // Check if we have identical memory versions, i.e. the global memory version, unresolved
834 // field version and the values' memory_version_before_stores, last_stored_value
835 // and store_loc_set are identical.
836 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800837 auto value = (lvn->*map_ptr).find(key);
838 if (value == (lvn->*map_ptr).end()) {
Vladimir Marko95a05972014-05-30 10:01:32 +0100839 if (cmp_values->memory_version_before_stores != kNoValue) {
840 same_version = false;
841 break;
842 }
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800843 } else if (cmp_values->last_stored_value != value->second.last_stored_value ||
844 cmp_values->memory_version_before_stores != value->second.memory_version_before_stores ||
845 cmp_values->store_loc_set != value->second.store_loc_set) {
Vladimir Marko95a05972014-05-30 10:01:32 +0100846 same_version = false;
847 break;
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800848 } else if (value->second.last_load_memory_version != kNoValue) {
Vladimir Marko95a05972014-05-30 10:01:32 +0100849 DCHECK(load_memory_version_for_same_version == kNoValue ||
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800850 load_memory_version_for_same_version == value->second.last_load_memory_version);
851 load_memory_version_for_same_version = value->second.last_load_memory_version;
Vladimir Marko95a05972014-05-30 10:01:32 +0100852 }
853 }
854 }
855
856 if (same_version) {
857 // Copy the identical values.
858 my_values->memory_version_before_stores = cmp_values->memory_version_before_stores;
859 my_values->last_stored_value = cmp_values->last_stored_value;
860 my_values->store_loc_set = cmp_values->store_loc_set;
861 my_values->last_load_memory_version = load_memory_version_for_same_version;
862 // Merge load values seen in all incoming arcs (i.e. an intersection).
863 if (!cmp_values->load_value_map.empty()) {
864 my_values->load_value_map = cmp_values->load_value_map;
865 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800866 auto value = (lvn->*map_ptr).find(key);
867 if (value == (lvn->*map_ptr).end() || value->second.load_value_map.empty()) {
Vladimir Marko95a05972014-05-30 10:01:32 +0100868 my_values->load_value_map.clear();
869 break;
870 }
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800871 InPlaceIntersectMaps(&my_values->load_value_map, value->second.load_value_map);
Vladimir Marko95a05972014-05-30 10:01:32 +0100872 if (my_values->load_value_map.empty()) {
873 break;
874 }
875 }
876 }
877 } else {
878 // Bump version number for the merge.
879 my_values->memory_version_before_stores = my_values->last_load_memory_version =
880 Versions::LookupMergeBlockValue(gvn_, id_, key);
881
882 // Calculate the locations that have been either read from or written to in each incoming LVN.
883 bool first_lvn = true;
884 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800885 auto value = (lvn->*map_ptr).find(key);
886 if (value == (lvn->*map_ptr).end()) {
Vladimir Marko95a05972014-05-30 10:01:32 +0100887 my_values->load_value_map.clear();
888 break;
889 }
890 if (first_lvn) {
891 first_lvn = false;
892 // Copy the first LVN's locations. Values will be overwritten later.
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800893 my_values->load_value_map = value->second.load_value_map;
894 for (uint16_t location : value->second.store_loc_set) {
Vladimir Marko95a05972014-05-30 10:01:32 +0100895 my_values->load_value_map.Put(location, 0u);
896 }
897 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800898 IntersectAliasingValueLocations(my_values, &value->second);
Vladimir Marko95a05972014-05-30 10:01:32 +0100899 }
900 }
901 // Calculate merged values for the intersection.
902 for (auto& load_value_entry : my_values->load_value_map) {
903 uint16_t location = load_value_entry.first;
Vladimir Marko95a05972014-05-30 10:01:32 +0100904 merge_names_.clear();
Vladimir Marko7a01dc22015-01-02 17:00:44 +0000905 uint16_t value_name = kNoValue;
906 bool same_values = true;
Vladimir Marko95a05972014-05-30 10:01:32 +0100907 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
908 value_name = Versions::LookupMergeValue(gvn_, lvn, key, location);
909 same_values = same_values && (merge_names_.empty() || value_name == merge_names_.back());
910 merge_names_.push_back(value_name);
911 }
912 if (same_values) {
913 // value_name already contains the result.
914 } else {
915 auto lb = merge_map_.lower_bound(merge_names_);
916 if (lb != merge_map_.end() && !merge_map_.key_comp()(merge_names_, lb->first)) {
917 value_name = lb->second;
918 } else {
919 // NOTE: In addition to the key and id_ which don't change on an LVN recalculation
920 // during GVN, we also add location which can actually change on recalculation, so the
921 // value_name below may change. This could lead to an infinite loop if the location
922 // value name always changed when the refereced value name changes. However, given that
923 // we assign unique value names for other merges, such as Phis, such a dependency is
924 // not possible in a well-formed SSA graph.
925 value_name = Versions::LookupMergeLocationValue(gvn_, id_, key, location);
926 merge_map_.PutBefore(lb, merge_names_, value_name);
927 if (gvn_->NullCheckedInAllPredecessors(merge_names_)) {
928 null_checked_.insert(value_name);
929 }
930 }
931 }
932 load_value_entry.second = value_name;
933 }
934 }
935}
936
937void LocalValueNumbering::Merge(MergeType merge_type) {
938 DCHECK_GE(gvn_->merge_lvns_.size(), 2u);
939
Vladimir Marko7a01dc22015-01-02 17:00:44 +0000940 // Always reserve space in merge_names_. Even if we don't use it in Merge() we may need it
941 // in GetStartingVregValueNumberImpl() when the merge_names_'s allocator is not the top.
942 merge_names_.reserve(gvn_->merge_lvns_.size());
943
Vladimir Markob19955d2014-07-29 12:04:10 +0100944 IntersectSregValueMaps<&LocalValueNumbering::sreg_value_map_>();
945 IntersectSregValueMaps<&LocalValueNumbering::sreg_wide_value_map_>();
Vladimir Marko95a05972014-05-30 10:01:32 +0100946 if (merge_type == kReturnMerge) {
947 // RETURN or PHI+RETURN. We need only sreg value maps.
948 return;
949 }
950
951 MergeMemoryVersions(merge_type == kCatchMerge);
952
953 // Merge non-aliasing maps/sets.
Vladimir Marko95a05972014-05-30 10:01:32 +0100954 IntersectSets<ValueNameSet, &LocalValueNumbering::non_aliasing_refs_>();
Vladimir Marko55fff042014-07-10 12:42:52 +0100955 if (!non_aliasing_refs_.empty() && merge_type == kCatchMerge) {
956 PruneNonAliasingRefsForCatch();
957 }
958 if (!non_aliasing_refs_.empty()) {
959 MergeSets<IFieldLocToValueMap, &LocalValueNumbering::non_aliasing_ifield_value_map_,
960 &LocalValueNumbering::MergeNonAliasingIFieldValues>();
961 MergeSets<NonAliasingArrayValuesMap, &LocalValueNumbering::non_aliasing_array_value_map_,
962 &LocalValueNumbering::MergeAliasingValues<
963 NonAliasingArrayValuesMap, &LocalValueNumbering::non_aliasing_array_value_map_,
964 NonAliasingArrayVersions>>();
965 }
Vladimir Marko95a05972014-05-30 10:01:32 +0100966
967 // We won't do anything complicated for range checks, just calculate the intersection.
968 IntersectSets<RangeCheckSet, &LocalValueNumbering::range_checked_>();
969
970 // Merge null_checked_. We may later insert more, such as merged object field values.
Vladimir Marko2d2365c2014-08-19 18:08:39 +0100971 MergeNullChecked();
Vladimir Marko95a05972014-05-30 10:01:32 +0100972
Razvan A Lupusorue0951142014-11-14 14:36:55 -0800973 // Now merge the div_zero_checked_.
974 MergeDivZeroChecked();
975
Vladimir Marko95a05972014-05-30 10:01:32 +0100976 if (merge_type == kCatchMerge) {
977 // Memory is clobbered. New memory version already created, don't merge aliasing locations.
Vladimir Marko95a05972014-05-30 10:01:32 +0100978 return;
979 }
980
981 DCHECK(merge_type == kNormalMerge);
982
983 // Merge escaped refs and clobber sets.
984 MergeSets<ValueNameSet, &LocalValueNumbering::escaped_refs_,
985 &LocalValueNumbering::MergeEscapedRefs>();
986 if (!escaped_refs_.empty()) {
987 MergeSets<EscapedIFieldClobberSet, &LocalValueNumbering::escaped_ifield_clobber_set_,
988 &LocalValueNumbering::MergeEscapedIFieldTypeClobberSets>();
989 MergeSets<EscapedIFieldClobberSet, &LocalValueNumbering::escaped_ifield_clobber_set_,
990 &LocalValueNumbering::MergeEscapedIFieldClobberSets>();
991 MergeSets<EscapedArrayClobberSet, &LocalValueNumbering::escaped_array_clobber_set_,
992 &LocalValueNumbering::MergeEscapedArrayClobberSets>();
993 }
994
995 MergeSets<SFieldToValueMap, &LocalValueNumbering::sfield_value_map_,
996 &LocalValueNumbering::MergeSFieldValues>();
997 MergeSets<AliasingIFieldValuesMap, &LocalValueNumbering::aliasing_ifield_value_map_,
998 &LocalValueNumbering::MergeAliasingValues<
999 AliasingIFieldValuesMap, &LocalValueNumbering::aliasing_ifield_value_map_,
1000 AliasingIFieldVersions>>();
1001 MergeSets<AliasingArrayValuesMap, &LocalValueNumbering::aliasing_array_value_map_,
1002 &LocalValueNumbering::MergeAliasingValues<
1003 AliasingArrayValuesMap, &LocalValueNumbering::aliasing_array_value_map_,
1004 AliasingArrayVersions>>();
Vladimir Markof59f18b2014-02-17 15:53:57 +00001005}
1006
Vladimir Markoa4426cf2014-10-22 17:15:53 +01001007void LocalValueNumbering::PrepareEntryBlock() {
1008 uint32_t vreg = gvn_->GetMirGraph()->GetFirstInVR();
1009 CompilationUnit* cu = gvn_->GetCompilationUnit();
1010 const char* shorty = cu->shorty;
1011 ++shorty; // Skip return value.
1012 if ((cu->access_flags & kAccStatic) == 0) {
1013 // If non-static method, mark "this" as non-null
1014 uint16_t value_name = GetOperandValue(vreg);
1015 ++vreg;
1016 null_checked_.insert(value_name);
1017 }
1018 for ( ; *shorty != 0; ++shorty, ++vreg) {
1019 if (*shorty == 'J' || *shorty == 'D') {
1020 uint16_t value_name = GetOperandValueWide(vreg);
1021 SetOperandValueWide(vreg, value_name);
1022 ++vreg;
1023 }
1024 }
1025}
1026
Vladimir Markof59f18b2014-02-17 15:53:57 +00001027uint16_t LocalValueNumbering::MarkNonAliasingNonNull(MIR* mir) {
1028 uint16_t res = GetOperandValue(mir->ssa_rep->defs[0]);
Vladimir Markof59f18b2014-02-17 15:53:57 +00001029 DCHECK(null_checked_.find(res) == null_checked_.end());
1030 null_checked_.insert(res);
1031 non_aliasing_refs_.insert(res);
1032 return res;
1033}
1034
Vladimir Marko95a05972014-05-30 10:01:32 +01001035bool LocalValueNumbering::IsNonAliasing(uint16_t reg) const {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001036 return non_aliasing_refs_.find(reg) != non_aliasing_refs_.end();
Vladimir Markof59f18b2014-02-17 15:53:57 +00001037}
1038
Vladimir Marko95a05972014-05-30 10:01:32 +01001039bool LocalValueNumbering::IsNonAliasingIField(uint16_t reg, uint16_t field_id,
1040 uint16_t type) const {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001041 if (IsNonAliasing(reg)) {
1042 return true;
1043 }
Vladimir Marko95a05972014-05-30 10:01:32 +01001044 if (escaped_refs_.find(reg) == escaped_refs_.end()) {
1045 return false;
1046 }
1047 // Check for IPUTs to unresolved fields.
1048 EscapedIFieldClobberKey key1 = { reg, type, kNoValue };
1049 if (escaped_ifield_clobber_set_.find(key1) != escaped_ifield_clobber_set_.end()) {
1050 return false;
1051 }
1052 // Check for aliased IPUTs to the same field.
1053 EscapedIFieldClobberKey key2 = { reg, type, field_id };
1054 return escaped_ifield_clobber_set_.find(key2) == escaped_ifield_clobber_set_.end();
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001055}
1056
Vladimir Marko95a05972014-05-30 10:01:32 +01001057bool LocalValueNumbering::IsNonAliasingArray(uint16_t reg, uint16_t type) const {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001058 if (IsNonAliasing(reg)) {
1059 return true;
1060 }
Vladimir Marko95a05972014-05-30 10:01:32 +01001061 if (escaped_refs_.count(reg) == 0u) {
1062 return false;
1063 }
1064 // Check for aliased APUTs.
1065 EscapedArrayClobberKey key = { reg, type };
1066 return escaped_array_clobber_set_.find(key) == escaped_array_clobber_set_.end();
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001067}
1068
Vladimir Markof59f18b2014-02-17 15:53:57 +00001069void LocalValueNumbering::HandleNullCheck(MIR* mir, uint16_t reg) {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001070 auto lb = null_checked_.lower_bound(reg);
1071 if (lb != null_checked_.end() && *lb == reg) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001072 if (LIKELY(gvn_->CanModify())) {
1073 if (gvn_->GetCompilationUnit()->verbose) {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001074 LOG(INFO) << "Removing null check for 0x" << std::hex << mir->offset;
1075 }
1076 mir->optimization_flags |= MIR_IGNORE_NULL_CHECK;
Vladimir Markof59f18b2014-02-17 15:53:57 +00001077 }
Vladimir Markof59f18b2014-02-17 15:53:57 +00001078 } else {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001079 null_checked_.insert(lb, reg);
Vladimir Markof59f18b2014-02-17 15:53:57 +00001080 }
1081}
1082
1083void LocalValueNumbering::HandleRangeCheck(MIR* mir, uint16_t array, uint16_t index) {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001084 RangeCheckKey key = { array, index };
1085 auto lb = range_checked_.lower_bound(key);
1086 if (lb != range_checked_.end() && !RangeCheckKeyComparator()(key, *lb)) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001087 if (LIKELY(gvn_->CanModify())) {
1088 if (gvn_->GetCompilationUnit()->verbose) {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001089 LOG(INFO) << "Removing range check for 0x" << std::hex << mir->offset;
1090 }
1091 mir->optimization_flags |= MIR_IGNORE_RANGE_CHECK;
Vladimir Markof59f18b2014-02-17 15:53:57 +00001092 }
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001093 } else {
1094 // Mark range check completed.
1095 range_checked_.insert(lb, key);
Vladimir Markof59f18b2014-02-17 15:53:57 +00001096 }
Vladimir Markof59f18b2014-02-17 15:53:57 +00001097}
1098
Razvan A Lupusorue0951142014-11-14 14:36:55 -08001099void LocalValueNumbering::HandleDivZeroCheck(MIR* mir, uint16_t reg) {
1100 auto lb = div_zero_checked_.lower_bound(reg);
1101 if (lb != div_zero_checked_.end() && *lb == reg) {
1102 if (LIKELY(gvn_->CanModify())) {
1103 if (gvn_->GetCompilationUnit()->verbose) {
1104 LOG(INFO) << "Removing div zero check for 0x" << std::hex << mir->offset;
1105 }
1106 mir->optimization_flags |= MIR_IGNORE_DIV_ZERO_CHECK;
1107 }
1108 } else {
1109 div_zero_checked_.insert(lb, reg);
1110 }
1111}
1112
Vladimir Markof59f18b2014-02-17 15:53:57 +00001113void LocalValueNumbering::HandlePutObject(MIR* mir) {
1114 // If we're storing a non-aliasing reference, stop tracking it as non-aliasing now.
1115 uint16_t base = GetOperandValue(mir->ssa_rep->uses[0]);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001116 HandleEscapingRef(base);
Vladimir Marko743b98c2014-11-24 19:45:41 +00001117 if (gvn_->CanModify() && null_checked_.count(base) != 0u) {
1118 if (gvn_->GetCompilationUnit()->verbose) {
1119 LOG(INFO) << "Removing GC card mark value null check for 0x" << std::hex << mir->offset;
1120 }
1121 mir->optimization_flags |= MIR_STORE_NON_NULL_VALUE;
1122 }
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001123}
1124
1125void LocalValueNumbering::HandleEscapingRef(uint16_t base) {
1126 auto it = non_aliasing_refs_.find(base);
1127 if (it != non_aliasing_refs_.end()) {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001128 non_aliasing_refs_.erase(it);
Vladimir Marko95a05972014-05-30 10:01:32 +01001129 escaped_refs_.insert(base);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001130 }
1131}
1132
Vladimir Markoa4426cf2014-10-22 17:15:53 +01001133void LocalValueNumbering::HandleInvokeArgs(const MIR* mir, const LocalValueNumbering* mir_lvn) {
1134 const int32_t* uses = mir->ssa_rep->uses;
1135 const int32_t* uses_end = uses + mir->ssa_rep->num_uses;
1136 while (uses != uses_end) {
1137 uint16_t sreg = *uses;
1138 ++uses;
1139 // Avoid LookupValue() so that we don't store new values in the global value map.
1140 auto local_it = mir_lvn->sreg_value_map_.find(sreg);
1141 if (local_it != mir_lvn->sreg_value_map_.end()) {
1142 non_aliasing_refs_.erase(local_it->second);
1143 } else {
1144 uint16_t value_name = gvn_->FindValue(kNoValue, sreg, kNoValue, kNoValue);
1145 if (value_name != kNoValue) {
1146 non_aliasing_refs_.erase(value_name);
1147 }
1148 }
1149 }
1150}
1151
Vladimir Marko95a05972014-05-30 10:01:32 +01001152uint16_t LocalValueNumbering::HandlePhi(MIR* mir) {
1153 if (gvn_->merge_lvns_.empty()) {
1154 // Running LVN without a full GVN?
1155 return kNoValue;
1156 }
Vladimir Marko95a05972014-05-30 10:01:32 +01001157 int32_t* uses = mir->ssa_rep->uses;
1158 // Try to find out if this is merging wide regs.
1159 if (mir->ssa_rep->defs[0] != 0 &&
1160 sreg_wide_value_map_.count(mir->ssa_rep->defs[0] - 1) != 0u) {
1161 // This is the high part of a wide reg. Ignore the Phi.
1162 return kNoValue;
1163 }
Vladimir Markoa4426cf2014-10-22 17:15:53 +01001164 BasicBlockId* incoming = mir->meta.phi_incoming;
1165 int16_t pos = 0;
1166 // Check if we're merging a wide value based on the first merged LVN.
1167 const LocalValueNumbering* first_lvn = gvn_->merge_lvns_[0];
1168 DCHECK_LT(pos, mir->ssa_rep->num_uses);
1169 while (incoming[pos] != first_lvn->Id()) {
1170 ++pos;
1171 DCHECK_LT(pos, mir->ssa_rep->num_uses);
Vladimir Marko95a05972014-05-30 10:01:32 +01001172 }
Vladimir Markoa4426cf2014-10-22 17:15:53 +01001173 int first_s_reg = uses[pos];
1174 bool wide = (first_lvn->sreg_wide_value_map_.count(first_s_reg) != 0u);
Vladimir Marko95a05972014-05-30 10:01:32 +01001175 // Iterate over *merge_lvns_ and skip incoming sregs for BBs without associated LVN.
Vladimir Marko95a05972014-05-30 10:01:32 +01001176 merge_names_.clear();
Vladimir Marko7a01dc22015-01-02 17:00:44 +00001177 uint16_t value_name = kNoValue;
Vladimir Marko95a05972014-05-30 10:01:32 +01001178 bool same_values = true;
1179 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
1180 DCHECK_LT(pos, mir->ssa_rep->num_uses);
1181 while (incoming[pos] != lvn->Id()) {
1182 ++pos;
1183 DCHECK_LT(pos, mir->ssa_rep->num_uses);
1184 }
1185 int s_reg = uses[pos];
1186 ++pos;
1187 value_name = wide ? lvn->GetOperandValueWide(s_reg) : lvn->GetOperandValue(s_reg);
1188
1189 same_values = same_values && (merge_names_.empty() || value_name == merge_names_.back());
1190 merge_names_.push_back(value_name);
1191 }
1192 if (same_values) {
1193 // value_name already contains the result.
1194 } else {
1195 auto lb = merge_map_.lower_bound(merge_names_);
1196 if (lb != merge_map_.end() && !merge_map_.key_comp()(merge_names_, lb->first)) {
1197 value_name = lb->second;
1198 } else {
1199 value_name = gvn_->LookupValue(kNoValue, mir->ssa_rep->defs[0], kNoValue, kNoValue);
1200 merge_map_.PutBefore(lb, merge_names_, value_name);
1201 if (!wide && gvn_->NullCheckedInAllPredecessors(merge_names_)) {
1202 null_checked_.insert(value_name);
1203 }
Razvan A Lupusorue0951142014-11-14 14:36:55 -08001204 if (gvn_->DivZeroCheckedInAllPredecessors(merge_names_)) {
1205 div_zero_checked_.insert(value_name);
1206 }
Vladimir Marko95a05972014-05-30 10:01:32 +01001207 }
1208 }
1209 if (wide) {
1210 SetOperandValueWide(mir->ssa_rep->defs[0], value_name);
1211 } else {
1212 SetOperandValue(mir->ssa_rep->defs[0], value_name);
1213 }
1214 return value_name;
1215}
1216
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001217uint16_t LocalValueNumbering::HandleAGet(MIR* mir, uint16_t opcode) {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001218 uint16_t array = GetOperandValue(mir->ssa_rep->uses[0]);
1219 HandleNullCheck(mir, array);
1220 uint16_t index = GetOperandValue(mir->ssa_rep->uses[1]);
1221 HandleRangeCheck(mir, array, index);
Vladimir Markoaf6925b2014-10-31 16:37:32 +00001222 uint16_t type = AGetMemAccessType(static_cast<Instruction::Code>(opcode));
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001223 // Establish value number for loaded register.
1224 uint16_t res;
1225 if (IsNonAliasingArray(array, type)) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001226 res = HandleAliasingValuesGet<NonAliasingArrayVersions>(&non_aliasing_array_value_map_,
1227 array, index);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001228 } else {
Vladimir Marko95a05972014-05-30 10:01:32 +01001229 uint16_t location = gvn_->GetArrayLocation(array, index);
1230 res = HandleAliasingValuesGet<AliasingArrayVersions>(&aliasing_array_value_map_,
1231 type, location);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001232 }
1233 if (opcode == Instruction::AGET_WIDE) {
1234 SetOperandValueWide(mir->ssa_rep->defs[0], res);
1235 } else {
1236 SetOperandValue(mir->ssa_rep->defs[0], res);
1237 }
1238 return res;
1239}
1240
1241void LocalValueNumbering::HandleAPut(MIR* mir, uint16_t opcode) {
1242 int array_idx = (opcode == Instruction::APUT_WIDE) ? 2 : 1;
1243 int index_idx = array_idx + 1;
1244 uint16_t array = GetOperandValue(mir->ssa_rep->uses[array_idx]);
1245 HandleNullCheck(mir, array);
1246 uint16_t index = GetOperandValue(mir->ssa_rep->uses[index_idx]);
1247 HandleRangeCheck(mir, array, index);
1248
Vladimir Markoaf6925b2014-10-31 16:37:32 +00001249 uint16_t type = APutMemAccessType(static_cast<Instruction::Code>(opcode));
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001250 uint16_t value = (opcode == Instruction::APUT_WIDE)
1251 ? GetOperandValueWide(mir->ssa_rep->uses[0])
1252 : GetOperandValue(mir->ssa_rep->uses[0]);
1253 if (IsNonAliasing(array)) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001254 bool put_is_live = HandleAliasingValuesPut<NonAliasingArrayVersions>(
1255 &non_aliasing_array_value_map_, array, index, value);
1256 if (!put_is_live) {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001257 // This APUT can be eliminated, it stores the same value that's already in the field.
1258 // TODO: Eliminate the APUT.
1259 return;
1260 }
Vladimir Marko95a05972014-05-30 10:01:32 +01001261 } else {
1262 uint16_t location = gvn_->GetArrayLocation(array, index);
1263 bool put_is_live = HandleAliasingValuesPut<AliasingArrayVersions>(
1264 &aliasing_array_value_map_, type, location, value);
1265 if (!put_is_live) {
1266 // This APUT can be eliminated, it stores the same value that's already in the field.
1267 // TODO: Eliminate the APUT.
1268 return;
1269 }
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001270
Vladimir Marko95a05972014-05-30 10:01:32 +01001271 // Clobber all escaped array refs for this type.
1272 for (uint16_t escaped_array : escaped_refs_) {
1273 EscapedArrayClobberKey clobber_key = { escaped_array, type };
1274 escaped_array_clobber_set_.insert(clobber_key);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001275 }
1276 }
1277}
1278
1279uint16_t LocalValueNumbering::HandleIGet(MIR* mir, uint16_t opcode) {
1280 uint16_t base = GetOperandValue(mir->ssa_rep->uses[0]);
1281 HandleNullCheck(mir, base);
Vladimir Marko95a05972014-05-30 10:01:32 +01001282 const MirFieldInfo& field_info = gvn_->GetMirGraph()->GetIFieldLoweringInfo(mir);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001283 uint16_t res;
1284 if (!field_info.IsResolved() || field_info.IsVolatile()) {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001285 // Unresolved fields may be volatile, so handle them as such to be safe.
Vladimir Markofa236452014-09-29 17:58:10 +01001286 HandleInvokeOrClInitOrAcquireOp(mir); // Volatile GETs have acquire semantics.
1287 // Volatile fields always get a new memory version; field id is irrelevant.
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001288 // Use result s_reg - will be unique.
Vladimir Marko95a05972014-05-30 10:01:32 +01001289 res = gvn_->LookupValue(kNoValue, mir->ssa_rep->defs[0], kNoValue, kNoValue);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001290 } else {
Vladimir Markoaf6925b2014-10-31 16:37:32 +00001291 uint16_t type = IGetMemAccessType(static_cast<Instruction::Code>(opcode));
1292 uint16_t field_id = gvn_->GetIFieldId(mir);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001293 if (IsNonAliasingIField(base, field_id, type)) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001294 uint16_t loc = gvn_->LookupValue(kNonAliasingIFieldLocOp, base, field_id, type);
1295 auto lb = non_aliasing_ifield_value_map_.lower_bound(loc);
1296 if (lb != non_aliasing_ifield_value_map_.end() && lb->first == loc) {
1297 res = lb->second;
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001298 } else {
Vladimir Marko95a05972014-05-30 10:01:32 +01001299 res = gvn_->LookupValue(kNonAliasingIFieldInitialOp, loc, kNoValue, kNoValue);
1300 non_aliasing_ifield_value_map_.PutBefore(lb, loc, res);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001301 }
Vladimir Marko95a05972014-05-30 10:01:32 +01001302 } else {
1303 res = HandleAliasingValuesGet<AliasingIFieldVersions>(&aliasing_ifield_value_map_,
1304 field_id, base);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001305 }
1306 }
1307 if (opcode == Instruction::IGET_WIDE) {
1308 SetOperandValueWide(mir->ssa_rep->defs[0], res);
1309 } else {
1310 SetOperandValue(mir->ssa_rep->defs[0], res);
1311 }
1312 return res;
1313}
1314
1315void LocalValueNumbering::HandleIPut(MIR* mir, uint16_t opcode) {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001316 int base_reg = (opcode == Instruction::IPUT_WIDE) ? 2 : 1;
1317 uint16_t base = GetOperandValue(mir->ssa_rep->uses[base_reg]);
1318 HandleNullCheck(mir, base);
Vladimir Markoaf6925b2014-10-31 16:37:32 +00001319 uint16_t type = IPutMemAccessType(static_cast<Instruction::Code>(opcode));
Vladimir Marko95a05972014-05-30 10:01:32 +01001320 const MirFieldInfo& field_info = gvn_->GetMirGraph()->GetIFieldLoweringInfo(mir);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001321 if (!field_info.IsResolved()) {
1322 // Unresolved fields always alias with everything of the same type.
1323 // Use mir->offset as modifier; without elaborate inlining, it will be unique.
1324 unresolved_ifield_version_[type] =
Vladimir Marko95a05972014-05-30 10:01:32 +01001325 gvn_->LookupValue(kUnresolvedIFieldOp, kNoValue, kNoValue, mir->offset);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001326
Vladimir Marko95a05972014-05-30 10:01:32 +01001327 // For simplicity, treat base as escaped now.
1328 HandleEscapingRef(base);
1329
1330 // Clobber all fields of escaped references of the same type.
1331 for (uint16_t escaped_ref : escaped_refs_) {
1332 EscapedIFieldClobberKey clobber_key = { escaped_ref, type, kNoValue };
1333 escaped_ifield_clobber_set_.insert(clobber_key);
1334 }
1335
1336 // Aliasing fields of the same type may have been overwritten.
1337 auto it = aliasing_ifield_value_map_.begin(), end = aliasing_ifield_value_map_.end();
1338 while (it != end) {
Vladimir Markoaf6925b2014-10-31 16:37:32 +00001339 if (gvn_->GetIFieldType(it->first) != type) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001340 ++it;
1341 } else {
1342 it = aliasing_ifield_value_map_.erase(it);
1343 }
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001344 }
1345 } else if (field_info.IsVolatile()) {
1346 // Nothing to do, resolved volatile fields always get a new memory version anyway and
1347 // can't alias with resolved non-volatile fields.
1348 } else {
Vladimir Markoaf6925b2014-10-31 16:37:32 +00001349 uint16_t field_id = gvn_->GetIFieldId(mir);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001350 uint16_t value = (opcode == Instruction::IPUT_WIDE)
1351 ? GetOperandValueWide(mir->ssa_rep->uses[0])
1352 : GetOperandValue(mir->ssa_rep->uses[0]);
1353 if (IsNonAliasing(base)) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001354 uint16_t loc = gvn_->LookupValue(kNonAliasingIFieldLocOp, base, field_id, type);
1355 auto lb = non_aliasing_ifield_value_map_.lower_bound(loc);
1356 if (lb != non_aliasing_ifield_value_map_.end() && lb->first == loc) {
1357 if (lb->second == value) {
1358 // This IPUT can be eliminated, it stores the same value that's already in the field.
1359 // TODO: Eliminate the IPUT.
1360 return;
1361 }
1362 lb->second = value; // Overwrite.
1363 } else {
1364 non_aliasing_ifield_value_map_.PutBefore(lb, loc, value);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001365 }
Vladimir Marko95a05972014-05-30 10:01:32 +01001366 } else {
1367 bool put_is_live = HandleAliasingValuesPut<AliasingIFieldVersions>(
1368 &aliasing_ifield_value_map_, field_id, base, value);
1369 if (!put_is_live) {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001370 // This IPUT can be eliminated, it stores the same value that's already in the field.
1371 // TODO: Eliminate the IPUT.
1372 return;
1373 }
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001374
Vladimir Marko95a05972014-05-30 10:01:32 +01001375 // Clobber all fields of escaped references for this field.
1376 for (uint16_t escaped_ref : escaped_refs_) {
1377 EscapedIFieldClobberKey clobber_key = { escaped_ref, type, field_id };
1378 escaped_ifield_clobber_set_.insert(clobber_key);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001379 }
1380 }
1381 }
1382}
1383
1384uint16_t LocalValueNumbering::HandleSGet(MIR* mir, uint16_t opcode) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001385 const MirSFieldLoweringInfo& field_info = gvn_->GetMirGraph()->GetSFieldLoweringInfo(mir);
Vladimir Markofa236452014-09-29 17:58:10 +01001386 if (!field_info.IsResolved() || field_info.IsVolatile() ||
Vladimir Marko66c6d7b2014-10-16 15:41:48 +01001387 (!field_info.IsClassInitialized() &&
1388 (mir->optimization_flags & MIR_CLASS_IS_INITIALIZED) == 0)) {
Vladimir Markofa236452014-09-29 17:58:10 +01001389 // Volatile SGETs (and unresolved fields are potentially volatile) have acquire semantics
1390 // and class initialization can call arbitrary functions, we need to wipe aliasing values.
1391 HandleInvokeOrClInitOrAcquireOp(mir);
Vladimir Markof418f322014-07-09 14:45:36 +01001392 }
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001393 uint16_t res;
1394 if (!field_info.IsResolved() || field_info.IsVolatile()) {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001395 // Unresolved fields may be volatile, so handle them as such to be safe.
Vladimir Markofa236452014-09-29 17:58:10 +01001396 // Volatile fields always get a new memory version; field id is irrelevant.
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001397 // Use result s_reg - will be unique.
Vladimir Marko95a05972014-05-30 10:01:32 +01001398 res = gvn_->LookupValue(kNoValue, mir->ssa_rep->defs[0], kNoValue, kNoValue);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001399 } else {
Vladimir Markoaf6925b2014-10-31 16:37:32 +00001400 uint16_t type = SGetMemAccessType(static_cast<Instruction::Code>(opcode));
1401 uint16_t field_id = gvn_->GetSFieldId(mir);
Vladimir Marko95a05972014-05-30 10:01:32 +01001402 auto lb = sfield_value_map_.lower_bound(field_id);
1403 if (lb != sfield_value_map_.end() && lb->first == field_id) {
1404 res = lb->second;
1405 } else {
1406 // Resolved non-volatile static fields can alias with non-resolved fields of the same type,
1407 // so we need to use unresolved_sfield_version_[type] in addition to global_memory_version_
1408 // to determine the version of the field.
1409 res = gvn_->LookupValue(kResolvedSFieldOp, field_id,
1410 unresolved_sfield_version_[type], global_memory_version_);
1411 sfield_value_map_.PutBefore(lb, field_id, res);
1412 }
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001413 }
1414 if (opcode == Instruction::SGET_WIDE) {
1415 SetOperandValueWide(mir->ssa_rep->defs[0], res);
1416 } else {
1417 SetOperandValue(mir->ssa_rep->defs[0], res);
1418 }
1419 return res;
1420}
1421
1422void LocalValueNumbering::HandleSPut(MIR* mir, uint16_t opcode) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001423 const MirSFieldLoweringInfo& field_info = gvn_->GetMirGraph()->GetSFieldLoweringInfo(mir);
Vladimir Marko66c6d7b2014-10-16 15:41:48 +01001424 if (!field_info.IsClassInitialized() &&
1425 (mir->optimization_flags & MIR_CLASS_IS_INITIALIZED) == 0) {
Vladimir Markof418f322014-07-09 14:45:36 +01001426 // Class initialization can call arbitrary functions, we need to wipe aliasing values.
Vladimir Markofa236452014-09-29 17:58:10 +01001427 HandleInvokeOrClInitOrAcquireOp(mir);
Vladimir Markof418f322014-07-09 14:45:36 +01001428 }
Vladimir Markoaf6925b2014-10-31 16:37:32 +00001429 uint16_t type = SPutMemAccessType(static_cast<Instruction::Code>(opcode));
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001430 if (!field_info.IsResolved()) {
1431 // Unresolved fields always alias with everything of the same type.
1432 // Use mir->offset as modifier; without elaborate inlining, it will be unique.
1433 unresolved_sfield_version_[type] =
Vladimir Marko95a05972014-05-30 10:01:32 +01001434 gvn_->LookupValue(kUnresolvedSFieldOp, kNoValue, kNoValue, mir->offset);
1435 RemoveSFieldsForType(type);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001436 } else if (field_info.IsVolatile()) {
1437 // Nothing to do, resolved volatile fields always get a new memory version anyway and
1438 // can't alias with resolved non-volatile fields.
1439 } else {
Vladimir Markoaf6925b2014-10-31 16:37:32 +00001440 uint16_t field_id = gvn_->GetSFieldId(mir);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001441 uint16_t value = (opcode == Instruction::SPUT_WIDE)
1442 ? GetOperandValueWide(mir->ssa_rep->uses[0])
1443 : GetOperandValue(mir->ssa_rep->uses[0]);
1444 // Resolved non-volatile static fields can alias with non-resolved fields of the same type,
1445 // so we need to use unresolved_sfield_version_[type] in addition to global_memory_version_
1446 // to determine the version of the field.
Vladimir Marko95a05972014-05-30 10:01:32 +01001447 auto lb = sfield_value_map_.lower_bound(field_id);
1448 if (lb != sfield_value_map_.end() && lb->first == field_id) {
1449 if (lb->second == value) {
1450 // This SPUT can be eliminated, it stores the same value that's already in the field.
1451 // TODO: Eliminate the SPUT.
1452 return;
1453 }
1454 lb->second = value; // Overwrite.
1455 } else {
1456 sfield_value_map_.PutBefore(lb, field_id, value);
1457 }
1458 }
1459}
1460
1461void LocalValueNumbering::RemoveSFieldsForType(uint16_t type) {
1462 // Erase all static fields of this type from the sfield_value_map_.
1463 for (auto it = sfield_value_map_.begin(), end = sfield_value_map_.end(); it != end; ) {
Vladimir Markoaf6925b2014-10-31 16:37:32 +00001464 if (gvn_->GetSFieldType(it->first) == type) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001465 it = sfield_value_map_.erase(it);
1466 } else {
1467 ++it;
1468 }
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001469 }
Vladimir Markof59f18b2014-02-17 15:53:57 +00001470}
buzbee2502e002012-12-31 16:05:53 -08001471
Vladimir Markofa236452014-09-29 17:58:10 +01001472void LocalValueNumbering::HandleInvokeOrClInitOrAcquireOp(MIR* mir) {
Vladimir Markof418f322014-07-09 14:45:36 +01001473 // Use mir->offset as modifier; without elaborate inlining, it will be unique.
Vladimir Marko95a05972014-05-30 10:01:32 +01001474 global_memory_version_ =
1475 gvn_->LookupValue(kInvokeMemoryVersionBumpOp, 0u, 0u, mir->offset);
1476 // All static fields and instance fields and array elements of aliasing references,
1477 // including escaped references, may have been modified.
1478 sfield_value_map_.clear();
1479 aliasing_ifield_value_map_.clear();
1480 aliasing_array_value_map_.clear();
1481 escaped_refs_.clear();
1482 escaped_ifield_clobber_set_.clear();
1483 escaped_array_clobber_set_.clear();
Vladimir Markof418f322014-07-09 14:45:36 +01001484}
1485
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001486uint16_t LocalValueNumbering::GetValueNumber(MIR* mir) {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001487 uint16_t res = kNoValue;
buzbee2502e002012-12-31 16:05:53 -08001488 uint16_t opcode = mir->dalvikInsn.opcode;
1489 switch (opcode) {
1490 case Instruction::NOP:
1491 case Instruction::RETURN_VOID:
1492 case Instruction::RETURN:
1493 case Instruction::RETURN_OBJECT:
1494 case Instruction::RETURN_WIDE:
buzbee2502e002012-12-31 16:05:53 -08001495 case Instruction::GOTO:
1496 case Instruction::GOTO_16:
1497 case Instruction::GOTO_32:
1498 case Instruction::CHECK_CAST:
1499 case Instruction::THROW:
1500 case Instruction::FILL_ARRAY_DATA:
buzbee2502e002012-12-31 16:05:53 -08001501 case Instruction::PACKED_SWITCH:
1502 case Instruction::SPARSE_SWITCH:
1503 case Instruction::IF_EQ:
1504 case Instruction::IF_NE:
1505 case Instruction::IF_LT:
1506 case Instruction::IF_GE:
1507 case Instruction::IF_GT:
1508 case Instruction::IF_LE:
1509 case Instruction::IF_EQZ:
1510 case Instruction::IF_NEZ:
1511 case Instruction::IF_LTZ:
1512 case Instruction::IF_GEZ:
1513 case Instruction::IF_GTZ:
1514 case Instruction::IF_LEZ:
buzbee2502e002012-12-31 16:05:53 -08001515 case kMirOpFusedCmplFloat:
1516 case kMirOpFusedCmpgFloat:
1517 case kMirOpFusedCmplDouble:
1518 case kMirOpFusedCmpgDouble:
1519 case kMirOpFusedCmpLong:
1520 // Nothing defined - take no action.
1521 break;
1522
Vladimir Marko95a05972014-05-30 10:01:32 +01001523 case Instruction::MONITOR_ENTER:
1524 HandleNullCheck(mir, GetOperandValue(mir->ssa_rep->uses[0]));
Vladimir Markofa236452014-09-29 17:58:10 +01001525 HandleInvokeOrClInitOrAcquireOp(mir); // Acquire operation.
Vladimir Marko95a05972014-05-30 10:01:32 +01001526 break;
1527
1528 case Instruction::MONITOR_EXIT:
1529 HandleNullCheck(mir, GetOperandValue(mir->ssa_rep->uses[0]));
1530 // If we're running GVN and CanModify(), uneliminated null check indicates bytecode error.
Vladimir Marko415ac882014-09-30 18:09:14 +01001531 if ((mir->optimization_flags & MIR_IGNORE_NULL_CHECK) == 0 &&
1532 gvn_->work_lvn_ != nullptr && gvn_->CanModify()) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001533 LOG(WARNING) << "Bytecode error: MONITOR_EXIT is still null checked at 0x" << std::hex
1534 << mir->offset << " in " << PrettyMethod(gvn_->cu_->method_idx, *gvn_->cu_->dex_file);
1535 }
1536 break;
1537
Vladimir Markof59f18b2014-02-17 15:53:57 +00001538 case Instruction::FILLED_NEW_ARRAY:
1539 case Instruction::FILLED_NEW_ARRAY_RANGE:
1540 // Nothing defined but the result will be unique and non-null.
1541 if (mir->next != nullptr && mir->next->dalvikInsn.opcode == Instruction::MOVE_RESULT_OBJECT) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001542 uint16_t array = MarkNonAliasingNonNull(mir->next);
1543 // Do not SetOperandValue(), we'll do that when we process the MOVE_RESULT_OBJECT.
1544 if (kLocalValueNumberingEnableFilledNewArrayTracking && mir->ssa_rep->num_uses != 0u) {
1545 AliasingValues* values = GetAliasingValues(&non_aliasing_array_value_map_, array);
1546 // Clear the value if we got a merged version in a loop.
Vladimir Markob19955d2014-07-29 12:04:10 +01001547 *values = AliasingValues(this);
Vladimir Marko95a05972014-05-30 10:01:32 +01001548 for (size_t i = 0u, count = mir->ssa_rep->num_uses; i != count; ++i) {
1549 DCHECK_EQ(High16Bits(i), 0u);
1550 uint16_t index = gvn_->LookupValue(Instruction::CONST, i, 0u, 0);
1551 uint16_t value = GetOperandValue(mir->ssa_rep->uses[i]);
1552 values->load_value_map.Put(index, value);
1553 RangeCheckKey key = { array, index };
1554 range_checked_.insert(key);
1555 }
1556 }
Vladimir Markof59f18b2014-02-17 15:53:57 +00001557 // The MOVE_RESULT_OBJECT will be processed next and we'll return the value name then.
1558 }
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001559 // All args escaped (if references).
1560 for (size_t i = 0u, count = mir->ssa_rep->num_uses; i != count; ++i) {
1561 uint16_t reg = GetOperandValue(mir->ssa_rep->uses[i]);
1562 HandleEscapingRef(reg);
1563 }
Vladimir Markof59f18b2014-02-17 15:53:57 +00001564 break;
1565
Vladimir Markoa78e66a2014-10-16 13:38:44 +01001566 case kMirOpNullCheck:
1567 HandleNullCheck(mir, GetOperandValue(mir->ssa_rep->uses[0]));
1568 break;
1569
Vladimir Markof59f18b2014-02-17 15:53:57 +00001570 case Instruction::INVOKE_DIRECT:
1571 case Instruction::INVOKE_DIRECT_RANGE:
1572 case Instruction::INVOKE_VIRTUAL:
1573 case Instruction::INVOKE_VIRTUAL_RANGE:
1574 case Instruction::INVOKE_SUPER:
1575 case Instruction::INVOKE_SUPER_RANGE:
1576 case Instruction::INVOKE_INTERFACE:
1577 case Instruction::INVOKE_INTERFACE_RANGE: {
1578 // Nothing defined but handle the null check.
1579 uint16_t reg = GetOperandValue(mir->ssa_rep->uses[0]);
1580 HandleNullCheck(mir, reg);
1581 }
Ian Rogersfc787ec2014-10-09 21:56:44 -07001582 FALLTHROUGH_INTENDED;
Vladimir Markof59f18b2014-02-17 15:53:57 +00001583 case Instruction::INVOKE_STATIC:
1584 case Instruction::INVOKE_STATIC_RANGE:
Vladimir Markoff0ac472014-10-02 17:24:53 +01001585 // Make ref args aliasing.
Vladimir Markoa4426cf2014-10-22 17:15:53 +01001586 HandleInvokeArgs(mir, this);
Vladimir Markoff0ac472014-10-02 17:24:53 +01001587 HandleInvokeOrClInitOrAcquireOp(mir);
Vladimir Markof59f18b2014-02-17 15:53:57 +00001588 break;
1589
buzbee2502e002012-12-31 16:05:53 -08001590 case Instruction::MOVE_RESULT:
1591 case Instruction::MOVE_RESULT_OBJECT:
1592 case Instruction::INSTANCE_OF:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001593 // 1 result, treat as unique each time, use result s_reg - will be unique.
1594 res = GetOperandValue(mir->ssa_rep->defs[0]);
1595 SetOperandValue(mir->ssa_rep->defs[0], res);
1596 break;
1597 case Instruction::MOVE_EXCEPTION:
buzbee2502e002012-12-31 16:05:53 -08001598 case Instruction::NEW_INSTANCE:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001599 case Instruction::NEW_ARRAY:
Vladimir Markob3e527b2014-04-04 12:37:07 +01001600 // 1 result, treat as unique each time, use result s_reg - will be unique.
1601 res = MarkNonAliasingNonNull(mir);
Vladimir Marko95a05972014-05-30 10:01:32 +01001602 SetOperandValue(mir->ssa_rep->defs[0], res);
buzbee2502e002012-12-31 16:05:53 -08001603 break;
Vladimir Marko7a01dc22015-01-02 17:00:44 +00001604 case Instruction::CONST_CLASS:
1605 DCHECK_EQ(Low16Bits(mir->dalvikInsn.vB), mir->dalvikInsn.vB);
1606 res = gvn_->LookupValue(Instruction::CONST_CLASS, mir->dalvikInsn.vB, 0, 0);
1607 SetOperandValue(mir->ssa_rep->defs[0], res);
1608 null_checked_.insert(res);
1609 non_aliasing_refs_.insert(res);
1610 break;
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001611 case Instruction::CONST_STRING:
1612 case Instruction::CONST_STRING_JUMBO:
1613 // These strings are internalized, so assign value based on the string pool index.
Vladimir Marko95a05972014-05-30 10:01:32 +01001614 res = gvn_->LookupValue(Instruction::CONST_STRING, Low16Bits(mir->dalvikInsn.vB),
1615 High16Bits(mir->dalvikInsn.vB), 0);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001616 SetOperandValue(mir->ssa_rep->defs[0], res);
1617 null_checked_.insert(res); // May already be there.
1618 // NOTE: Hacking the contents of an internalized string via reflection is possible
1619 // but the behavior is undefined. Therefore, we consider the string constant and
1620 // the reference non-aliasing.
1621 // TUNING: We could keep this property even if the reference "escapes".
1622 non_aliasing_refs_.insert(res); // May already be there.
1623 break;
Vladimir Markof59f18b2014-02-17 15:53:57 +00001624 case Instruction::MOVE_RESULT_WIDE:
Vladimir Markob3e527b2014-04-04 12:37:07 +01001625 // 1 wide result, treat as unique each time, use result s_reg - will be unique.
1626 res = GetOperandValueWide(mir->ssa_rep->defs[0]);
1627 SetOperandValueWide(mir->ssa_rep->defs[0], res);
buzbee2502e002012-12-31 16:05:53 -08001628 break;
1629
1630 case kMirOpPhi:
Vladimir Marko95a05972014-05-30 10:01:32 +01001631 res = HandlePhi(mir);
buzbee2502e002012-12-31 16:05:53 -08001632 break;
1633
1634 case Instruction::MOVE:
1635 case Instruction::MOVE_OBJECT:
1636 case Instruction::MOVE_16:
1637 case Instruction::MOVE_OBJECT_16:
1638 case Instruction::MOVE_FROM16:
1639 case Instruction::MOVE_OBJECT_FROM16:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001640 case kMirOpCopy:
1641 // Just copy value number of source to value number of result.
1642 res = GetOperandValue(mir->ssa_rep->uses[0]);
1643 SetOperandValue(mir->ssa_rep->defs[0], res);
buzbee2502e002012-12-31 16:05:53 -08001644 break;
1645
1646 case Instruction::MOVE_WIDE:
1647 case Instruction::MOVE_WIDE_16:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001648 case Instruction::MOVE_WIDE_FROM16:
1649 // Just copy value number of source to value number of result.
1650 res = GetOperandValueWide(mir->ssa_rep->uses[0]);
1651 SetOperandValueWide(mir->ssa_rep->defs[0], res);
buzbee2502e002012-12-31 16:05:53 -08001652 break;
1653
Vladimir Marko7a01dc22015-01-02 17:00:44 +00001654 case Instruction::CONST_HIGH16:
1655 if (mir->dalvikInsn.vB != 0) {
1656 res = gvn_->LookupValue(Instruction::CONST, 0, mir->dalvikInsn.vB, 0);
1657 SetOperandValue(mir->ssa_rep->defs[0], res);
1658 break;
1659 }
1660 FALLTHROUGH_INTENDED;
buzbee2502e002012-12-31 16:05:53 -08001661 case Instruction::CONST:
1662 case Instruction::CONST_4:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001663 case Instruction::CONST_16:
Vladimir Marko7a01dc22015-01-02 17:00:44 +00001664 if (mir->dalvikInsn.vB == 0 && gvn_->GetMirGraph()->GetRawDest(mir).ref) {
1665 res = GlobalValueNumbering::kNullValue;
1666 } else {
1667 res = gvn_->LookupValue(Instruction::CONST, Low16Bits(mir->dalvikInsn.vB),
1668 High16Bits(mir->dalvikInsn.vB), 0);
1669 }
Vladimir Markof59f18b2014-02-17 15:53:57 +00001670 SetOperandValue(mir->ssa_rep->defs[0], res);
buzbee2502e002012-12-31 16:05:53 -08001671 break;
1672
1673 case Instruction::CONST_WIDE_16:
1674 case Instruction::CONST_WIDE_32: {
Vladimir Marko95a05972014-05-30 10:01:32 +01001675 uint16_t low_res = gvn_->LookupValue(Instruction::CONST, Low16Bits(mir->dalvikInsn.vB),
Serguei Katkov47d2e482015-02-06 17:15:36 +06001676 High16Bits(mir->dalvikInsn.vB), 1);
buzbee2502e002012-12-31 16:05:53 -08001677 uint16_t high_res;
1678 if (mir->dalvikInsn.vB & 0x80000000) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001679 high_res = gvn_->LookupValue(Instruction::CONST, 0xffff, 0xffff, 2);
buzbee2502e002012-12-31 16:05:53 -08001680 } else {
Vladimir Marko95a05972014-05-30 10:01:32 +01001681 high_res = gvn_->LookupValue(Instruction::CONST, 0, 0, 2);
buzbee2502e002012-12-31 16:05:53 -08001682 }
Vladimir Marko95a05972014-05-30 10:01:32 +01001683 res = gvn_->LookupValue(Instruction::CONST, low_res, high_res, 3);
Vladimir Markof59f18b2014-02-17 15:53:57 +00001684 SetOperandValueWide(mir->ssa_rep->defs[0], res);
buzbee2502e002012-12-31 16:05:53 -08001685 }
1686 break;
1687
1688 case Instruction::CONST_WIDE: {
1689 uint32_t low_word = Low32Bits(mir->dalvikInsn.vB_wide);
1690 uint32_t high_word = High32Bits(mir->dalvikInsn.vB_wide);
Vladimir Marko95a05972014-05-30 10:01:32 +01001691 uint16_t low_res = gvn_->LookupValue(Instruction::CONST, Low16Bits(low_word),
1692 High16Bits(low_word), 1);
1693 uint16_t high_res = gvn_->LookupValue(Instruction::CONST, Low16Bits(high_word),
1694 High16Bits(high_word), 2);
1695 res = gvn_->LookupValue(Instruction::CONST, low_res, high_res, 3);
buzbee2502e002012-12-31 16:05:53 -08001696 SetOperandValueWide(mir->ssa_rep->defs[0], res);
1697 }
1698 break;
1699
1700 case Instruction::CONST_WIDE_HIGH16: {
Vladimir Marko95a05972014-05-30 10:01:32 +01001701 uint16_t low_res = gvn_->LookupValue(Instruction::CONST, 0, 0, 1);
1702 uint16_t high_res = gvn_->LookupValue(Instruction::CONST, 0,
1703 Low16Bits(mir->dalvikInsn.vB), 2);
1704 res = gvn_->LookupValue(Instruction::CONST, low_res, high_res, 3);
buzbee2502e002012-12-31 16:05:53 -08001705 SetOperandValueWide(mir->ssa_rep->defs[0], res);
1706 }
1707 break;
1708
Vladimir Marko95a05972014-05-30 10:01:32 +01001709 case Instruction::ARRAY_LENGTH: {
1710 // Handle the null check.
1711 uint16_t reg = GetOperandValue(mir->ssa_rep->uses[0]);
1712 HandleNullCheck(mir, reg);
1713 }
Ian Rogersfc787ec2014-10-09 21:56:44 -07001714 FALLTHROUGH_INTENDED;
buzbee2502e002012-12-31 16:05:53 -08001715 case Instruction::NEG_INT:
1716 case Instruction::NOT_INT:
1717 case Instruction::NEG_FLOAT:
1718 case Instruction::INT_TO_BYTE:
1719 case Instruction::INT_TO_SHORT:
1720 case Instruction::INT_TO_CHAR:
1721 case Instruction::INT_TO_FLOAT:
1722 case Instruction::FLOAT_TO_INT: {
1723 // res = op + 1 operand
1724 uint16_t operand1 = GetOperandValue(mir->ssa_rep->uses[0]);
Vladimir Marko95a05972014-05-30 10:01:32 +01001725 res = gvn_->LookupValue(opcode, operand1, kNoValue, kNoValue);
buzbee2502e002012-12-31 16:05:53 -08001726 SetOperandValue(mir->ssa_rep->defs[0], res);
1727 }
1728 break;
1729
1730 case Instruction::LONG_TO_FLOAT:
1731 case Instruction::LONG_TO_INT:
1732 case Instruction::DOUBLE_TO_FLOAT:
1733 case Instruction::DOUBLE_TO_INT: {
1734 // res = op + 1 wide operand
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001735 uint16_t operand1 = GetOperandValueWide(mir->ssa_rep->uses[0]);
Vladimir Marko95a05972014-05-30 10:01:32 +01001736 res = gvn_->LookupValue(opcode, operand1, kNoValue, kNoValue);
buzbee2502e002012-12-31 16:05:53 -08001737 SetOperandValue(mir->ssa_rep->defs[0], res);
1738 }
1739 break;
1740
buzbee2502e002012-12-31 16:05:53 -08001741 case Instruction::DOUBLE_TO_LONG:
1742 case Instruction::LONG_TO_DOUBLE:
1743 case Instruction::NEG_LONG:
1744 case Instruction::NOT_LONG:
1745 case Instruction::NEG_DOUBLE: {
1746 // wide res = op + 1 wide operand
1747 uint16_t operand1 = GetOperandValueWide(mir->ssa_rep->uses[0]);
Vladimir Marko95a05972014-05-30 10:01:32 +01001748 res = gvn_->LookupValue(opcode, operand1, kNoValue, kNoValue);
buzbee2502e002012-12-31 16:05:53 -08001749 SetOperandValueWide(mir->ssa_rep->defs[0], res);
1750 }
1751 break;
1752
1753 case Instruction::FLOAT_TO_DOUBLE:
1754 case Instruction::FLOAT_TO_LONG:
1755 case Instruction::INT_TO_DOUBLE:
1756 case Instruction::INT_TO_LONG: {
1757 // wide res = op + 1 operand
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001758 uint16_t operand1 = GetOperandValue(mir->ssa_rep->uses[0]);
Vladimir Marko95a05972014-05-30 10:01:32 +01001759 res = gvn_->LookupValue(opcode, operand1, kNoValue, kNoValue);
buzbee2502e002012-12-31 16:05:53 -08001760 SetOperandValueWide(mir->ssa_rep->defs[0], res);
1761 }
1762 break;
1763
1764 case Instruction::CMPL_DOUBLE:
1765 case Instruction::CMPG_DOUBLE:
1766 case Instruction::CMP_LONG: {
1767 // res = op + 2 wide operands
1768 uint16_t operand1 = GetOperandValueWide(mir->ssa_rep->uses[0]);
1769 uint16_t operand2 = GetOperandValueWide(mir->ssa_rep->uses[2]);
Vladimir Marko95a05972014-05-30 10:01:32 +01001770 res = gvn_->LookupValue(opcode, operand1, operand2, kNoValue);
buzbee2502e002012-12-31 16:05:53 -08001771 SetOperandValue(mir->ssa_rep->defs[0], res);
1772 }
1773 break;
1774
Razvan A Lupusorue0951142014-11-14 14:36:55 -08001775 case Instruction::DIV_INT:
1776 case Instruction::DIV_INT_2ADDR:
1777 case Instruction::REM_INT:
1778 case Instruction::REM_INT_2ADDR:
1779 HandleDivZeroCheck(mir, GetOperandValue(mir->ssa_rep->uses[1]));
1780 FALLTHROUGH_INTENDED;
1781
buzbee2502e002012-12-31 16:05:53 -08001782 case Instruction::CMPG_FLOAT:
1783 case Instruction::CMPL_FLOAT:
1784 case Instruction::ADD_INT:
1785 case Instruction::ADD_INT_2ADDR:
1786 case Instruction::MUL_INT:
1787 case Instruction::MUL_INT_2ADDR:
1788 case Instruction::AND_INT:
1789 case Instruction::AND_INT_2ADDR:
1790 case Instruction::OR_INT:
1791 case Instruction::OR_INT_2ADDR:
1792 case Instruction::XOR_INT:
1793 case Instruction::XOR_INT_2ADDR:
1794 case Instruction::SUB_INT:
1795 case Instruction::SUB_INT_2ADDR:
buzbee2502e002012-12-31 16:05:53 -08001796 case Instruction::SHL_INT:
1797 case Instruction::SHL_INT_2ADDR:
1798 case Instruction::SHR_INT:
1799 case Instruction::SHR_INT_2ADDR:
1800 case Instruction::USHR_INT:
1801 case Instruction::USHR_INT_2ADDR: {
1802 // res = op + 2 operands
1803 uint16_t operand1 = GetOperandValue(mir->ssa_rep->uses[0]);
1804 uint16_t operand2 = GetOperandValue(mir->ssa_rep->uses[1]);
Vladimir Marko95a05972014-05-30 10:01:32 +01001805 res = gvn_->LookupValue(opcode, operand1, operand2, kNoValue);
buzbee2502e002012-12-31 16:05:53 -08001806 SetOperandValue(mir->ssa_rep->defs[0], res);
1807 }
1808 break;
1809
Razvan A Lupusorue0951142014-11-14 14:36:55 -08001810 case Instruction::DIV_LONG:
1811 case Instruction::REM_LONG:
1812 case Instruction::DIV_LONG_2ADDR:
1813 case Instruction::REM_LONG_2ADDR:
1814 HandleDivZeroCheck(mir, GetOperandValueWide(mir->ssa_rep->uses[2]));
1815 FALLTHROUGH_INTENDED;
1816
buzbee2502e002012-12-31 16:05:53 -08001817 case Instruction::ADD_LONG:
1818 case Instruction::SUB_LONG:
1819 case Instruction::MUL_LONG:
buzbee2502e002012-12-31 16:05:53 -08001820 case Instruction::AND_LONG:
1821 case Instruction::OR_LONG:
1822 case Instruction::XOR_LONG:
1823 case Instruction::ADD_LONG_2ADDR:
1824 case Instruction::SUB_LONG_2ADDR:
1825 case Instruction::MUL_LONG_2ADDR:
buzbee2502e002012-12-31 16:05:53 -08001826 case Instruction::AND_LONG_2ADDR:
1827 case Instruction::OR_LONG_2ADDR:
1828 case Instruction::XOR_LONG_2ADDR:
1829 case Instruction::ADD_DOUBLE:
1830 case Instruction::SUB_DOUBLE:
1831 case Instruction::MUL_DOUBLE:
1832 case Instruction::DIV_DOUBLE:
1833 case Instruction::REM_DOUBLE:
1834 case Instruction::ADD_DOUBLE_2ADDR:
1835 case Instruction::SUB_DOUBLE_2ADDR:
1836 case Instruction::MUL_DOUBLE_2ADDR:
1837 case Instruction::DIV_DOUBLE_2ADDR:
1838 case Instruction::REM_DOUBLE_2ADDR: {
1839 // wide res = op + 2 wide operands
1840 uint16_t operand1 = GetOperandValueWide(mir->ssa_rep->uses[0]);
1841 uint16_t operand2 = GetOperandValueWide(mir->ssa_rep->uses[2]);
Vladimir Marko95a05972014-05-30 10:01:32 +01001842 res = gvn_->LookupValue(opcode, operand1, operand2, kNoValue);
buzbee2502e002012-12-31 16:05:53 -08001843 SetOperandValueWide(mir->ssa_rep->defs[0], res);
1844 }
1845 break;
1846
1847 case Instruction::SHL_LONG:
1848 case Instruction::SHR_LONG:
1849 case Instruction::USHR_LONG:
1850 case Instruction::SHL_LONG_2ADDR:
1851 case Instruction::SHR_LONG_2ADDR:
1852 case Instruction::USHR_LONG_2ADDR: {
1853 // wide res = op + 1 wide operand + 1 operand
1854 uint16_t operand1 = GetOperandValueWide(mir->ssa_rep->uses[0]);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001855 uint16_t operand2 = GetOperandValue(mir->ssa_rep->uses[2]);
Vladimir Marko95a05972014-05-30 10:01:32 +01001856 res = gvn_->LookupValue(opcode, operand1, operand2, kNoValue);
buzbee2502e002012-12-31 16:05:53 -08001857 SetOperandValueWide(mir->ssa_rep->defs[0], res);
1858 }
1859 break;
1860
1861 case Instruction::ADD_FLOAT:
1862 case Instruction::SUB_FLOAT:
1863 case Instruction::MUL_FLOAT:
1864 case Instruction::DIV_FLOAT:
1865 case Instruction::REM_FLOAT:
1866 case Instruction::ADD_FLOAT_2ADDR:
1867 case Instruction::SUB_FLOAT_2ADDR:
1868 case Instruction::MUL_FLOAT_2ADDR:
1869 case Instruction::DIV_FLOAT_2ADDR:
1870 case Instruction::REM_FLOAT_2ADDR: {
1871 // res = op + 2 operands
1872 uint16_t operand1 = GetOperandValue(mir->ssa_rep->uses[0]);
1873 uint16_t operand2 = GetOperandValue(mir->ssa_rep->uses[1]);
Vladimir Marko95a05972014-05-30 10:01:32 +01001874 res = gvn_->LookupValue(opcode, operand1, operand2, kNoValue);
buzbee2502e002012-12-31 16:05:53 -08001875 SetOperandValue(mir->ssa_rep->defs[0], res);
1876 }
1877 break;
1878
1879 case Instruction::RSUB_INT:
1880 case Instruction::ADD_INT_LIT16:
1881 case Instruction::MUL_INT_LIT16:
1882 case Instruction::DIV_INT_LIT16:
1883 case Instruction::REM_INT_LIT16:
1884 case Instruction::AND_INT_LIT16:
1885 case Instruction::OR_INT_LIT16:
1886 case Instruction::XOR_INT_LIT16:
1887 case Instruction::ADD_INT_LIT8:
1888 case Instruction::RSUB_INT_LIT8:
1889 case Instruction::MUL_INT_LIT8:
1890 case Instruction::DIV_INT_LIT8:
1891 case Instruction::REM_INT_LIT8:
1892 case Instruction::AND_INT_LIT8:
1893 case Instruction::OR_INT_LIT8:
1894 case Instruction::XOR_INT_LIT8:
1895 case Instruction::SHL_INT_LIT8:
1896 case Instruction::SHR_INT_LIT8:
1897 case Instruction::USHR_INT_LIT8: {
nikolay serdjukee40aa42014-03-25 12:21:29 +07001898 // Same as res = op + 2 operands, except use vC as operand 2
buzbee2502e002012-12-31 16:05:53 -08001899 uint16_t operand1 = GetOperandValue(mir->ssa_rep->uses[0]);
Vladimir Marko95a05972014-05-30 10:01:32 +01001900 uint16_t operand2 = gvn_->LookupValue(Instruction::CONST, mir->dalvikInsn.vC, 0, 0);
1901 res = gvn_->LookupValue(opcode, operand1, operand2, kNoValue);
buzbee2502e002012-12-31 16:05:53 -08001902 SetOperandValue(mir->ssa_rep->defs[0], res);
1903 }
1904 break;
1905
buzbee2502e002012-12-31 16:05:53 -08001906 case Instruction::AGET_OBJECT:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001907 case Instruction::AGET:
1908 case Instruction::AGET_WIDE:
buzbee2502e002012-12-31 16:05:53 -08001909 case Instruction::AGET_BOOLEAN:
1910 case Instruction::AGET_BYTE:
1911 case Instruction::AGET_CHAR:
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001912 case Instruction::AGET_SHORT:
1913 res = HandleAGet(mir, opcode);
buzbee2502e002012-12-31 16:05:53 -08001914 break;
1915
buzbee2502e002012-12-31 16:05:53 -08001916 case Instruction::APUT_OBJECT:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001917 HandlePutObject(mir);
Ian Rogersfc787ec2014-10-09 21:56:44 -07001918 FALLTHROUGH_INTENDED;
Vladimir Markof59f18b2014-02-17 15:53:57 +00001919 case Instruction::APUT:
1920 case Instruction::APUT_WIDE:
buzbee2502e002012-12-31 16:05:53 -08001921 case Instruction::APUT_BYTE:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001922 case Instruction::APUT_BOOLEAN:
1923 case Instruction::APUT_SHORT:
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001924 case Instruction::APUT_CHAR:
1925 HandleAPut(mir, opcode);
buzbee2502e002012-12-31 16:05:53 -08001926 break;
1927
1928 case Instruction::IGET_OBJECT:
buzbee2502e002012-12-31 16:05:53 -08001929 case Instruction::IGET:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001930 case Instruction::IGET_WIDE:
buzbee2502e002012-12-31 16:05:53 -08001931 case Instruction::IGET_BOOLEAN:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001932 case Instruction::IGET_BYTE:
1933 case Instruction::IGET_CHAR:
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001934 case Instruction::IGET_SHORT:
1935 res = HandleIGet(mir, opcode);
buzbee2502e002012-12-31 16:05:53 -08001936 break;
1937
buzbee2502e002012-12-31 16:05:53 -08001938 case Instruction::IPUT_OBJECT:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001939 HandlePutObject(mir);
Ian Rogersfc787ec2014-10-09 21:56:44 -07001940 FALLTHROUGH_INTENDED;
buzbee2502e002012-12-31 16:05:53 -08001941 case Instruction::IPUT:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001942 case Instruction::IPUT_WIDE:
buzbee2502e002012-12-31 16:05:53 -08001943 case Instruction::IPUT_BOOLEAN:
1944 case Instruction::IPUT_BYTE:
1945 case Instruction::IPUT_CHAR:
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001946 case Instruction::IPUT_SHORT:
1947 HandleIPut(mir, opcode);
buzbee2502e002012-12-31 16:05:53 -08001948 break;
1949
1950 case Instruction::SGET_OBJECT:
1951 case Instruction::SGET:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001952 case Instruction::SGET_WIDE:
buzbee2502e002012-12-31 16:05:53 -08001953 case Instruction::SGET_BOOLEAN:
1954 case Instruction::SGET_BYTE:
1955 case Instruction::SGET_CHAR:
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001956 case Instruction::SGET_SHORT:
1957 res = HandleSGet(mir, opcode);
buzbee2502e002012-12-31 16:05:53 -08001958 break;
1959
1960 case Instruction::SPUT_OBJECT:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001961 HandlePutObject(mir);
Ian Rogersfc787ec2014-10-09 21:56:44 -07001962 FALLTHROUGH_INTENDED;
buzbee2502e002012-12-31 16:05:53 -08001963 case Instruction::SPUT:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001964 case Instruction::SPUT_WIDE:
buzbee2502e002012-12-31 16:05:53 -08001965 case Instruction::SPUT_BOOLEAN:
1966 case Instruction::SPUT_BYTE:
1967 case Instruction::SPUT_CHAR:
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001968 case Instruction::SPUT_SHORT:
1969 HandleSPut(mir, opcode);
buzbee2502e002012-12-31 16:05:53 -08001970 break;
buzbee2502e002012-12-31 16:05:53 -08001971 }
1972 return res;
1973}
1974
Vladimir Marko7a01dc22015-01-02 17:00:44 +00001975uint16_t LocalValueNumbering::GetEndingVregValueNumberImpl(int v_reg, bool wide) const {
1976 const BasicBlock* bb = gvn_->GetBasicBlock(Id());
1977 DCHECK(bb != nullptr);
1978 int s_reg = bb->data_flow_info->vreg_to_ssa_map_exit[v_reg];
1979 if (s_reg == INVALID_SREG) {
1980 return kNoValue;
1981 }
1982 if (wide) {
1983 int high_s_reg = bb->data_flow_info->vreg_to_ssa_map_exit[v_reg + 1];
1984 if (high_s_reg != s_reg + 1) {
1985 return kNoValue; // High word has been overwritten.
1986 }
1987 return GetSregValueWide(s_reg);
1988 } else {
1989 return GetSregValue(s_reg);
1990 }
1991}
1992
1993uint16_t LocalValueNumbering::GetStartingVregValueNumberImpl(int v_reg, bool wide) const {
1994 DCHECK_EQ(gvn_->mode_, GlobalValueNumbering::kModeGvnPostProcessing);
1995 DCHECK(gvn_->CanModify());
1996 const BasicBlock* bb = gvn_->GetBasicBlock(Id());
1997 DCHECK(bb != nullptr);
1998 DCHECK_NE(bb->predecessors.size(), 0u);
1999 if (bb->predecessors.size() == 1u) {
2000 return gvn_->GetLvn(bb->predecessors[0])->GetEndingVregValueNumberImpl(v_reg, wide);
2001 }
2002 merge_names_.clear();
2003 uint16_t value_name = kNoValue;
2004 bool same_values = true;
2005 for (BasicBlockId pred_id : bb->predecessors) {
2006 value_name = gvn_->GetLvn(pred_id)->GetEndingVregValueNumberImpl(v_reg, wide);
2007 if (value_name == kNoValue) {
2008 return kNoValue;
2009 }
2010 same_values = same_values && (merge_names_.empty() || value_name == merge_names_.back());
2011 merge_names_.push_back(value_name);
2012 }
2013 if (same_values) {
2014 // value_name already contains the result.
2015 } else {
2016 auto lb = merge_map_.lower_bound(merge_names_);
2017 if (lb != merge_map_.end() && !merge_map_.key_comp()(merge_names_, lb->first)) {
2018 value_name = lb->second;
2019 } else {
2020 value_name = kNoValue; // We never assigned a value name to this set of merged names.
2021 }
2022 }
2023 return value_name;
2024}
2025
buzbee2502e002012-12-31 16:05:53 -08002026} // namespace art