blob: 114346dd5a566f9665cf6c5927827b452964957d [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;
904 bool same_values = true;
905 uint16_t value_name = kNoValue;
906 merge_names_.clear();
907 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 Markob19955d2014-07-29 12:04:10 +0100940 IntersectSregValueMaps<&LocalValueNumbering::sreg_value_map_>();
941 IntersectSregValueMaps<&LocalValueNumbering::sreg_wide_value_map_>();
Vladimir Marko95a05972014-05-30 10:01:32 +0100942 if (merge_type == kReturnMerge) {
943 // RETURN or PHI+RETURN. We need only sreg value maps.
944 return;
945 }
946
947 MergeMemoryVersions(merge_type == kCatchMerge);
948
949 // Merge non-aliasing maps/sets.
Vladimir Marko95a05972014-05-30 10:01:32 +0100950 IntersectSets<ValueNameSet, &LocalValueNumbering::non_aliasing_refs_>();
Vladimir Marko55fff042014-07-10 12:42:52 +0100951 if (!non_aliasing_refs_.empty() && merge_type == kCatchMerge) {
952 PruneNonAliasingRefsForCatch();
953 }
954 if (!non_aliasing_refs_.empty()) {
955 MergeSets<IFieldLocToValueMap, &LocalValueNumbering::non_aliasing_ifield_value_map_,
956 &LocalValueNumbering::MergeNonAliasingIFieldValues>();
957 MergeSets<NonAliasingArrayValuesMap, &LocalValueNumbering::non_aliasing_array_value_map_,
958 &LocalValueNumbering::MergeAliasingValues<
959 NonAliasingArrayValuesMap, &LocalValueNumbering::non_aliasing_array_value_map_,
960 NonAliasingArrayVersions>>();
961 }
Vladimir Marko95a05972014-05-30 10:01:32 +0100962
963 // We won't do anything complicated for range checks, just calculate the intersection.
964 IntersectSets<RangeCheckSet, &LocalValueNumbering::range_checked_>();
965
966 // Merge null_checked_. We may later insert more, such as merged object field values.
Vladimir Marko2d2365c2014-08-19 18:08:39 +0100967 MergeNullChecked();
Vladimir Marko95a05972014-05-30 10:01:32 +0100968
Razvan A Lupusorue0951142014-11-14 14:36:55 -0800969 // Now merge the div_zero_checked_.
970 MergeDivZeroChecked();
971
Vladimir Marko95a05972014-05-30 10:01:32 +0100972 if (merge_type == kCatchMerge) {
973 // Memory is clobbered. New memory version already created, don't merge aliasing locations.
Vladimir Marko95a05972014-05-30 10:01:32 +0100974 return;
975 }
976
977 DCHECK(merge_type == kNormalMerge);
978
979 // Merge escaped refs and clobber sets.
980 MergeSets<ValueNameSet, &LocalValueNumbering::escaped_refs_,
981 &LocalValueNumbering::MergeEscapedRefs>();
982 if (!escaped_refs_.empty()) {
983 MergeSets<EscapedIFieldClobberSet, &LocalValueNumbering::escaped_ifield_clobber_set_,
984 &LocalValueNumbering::MergeEscapedIFieldTypeClobberSets>();
985 MergeSets<EscapedIFieldClobberSet, &LocalValueNumbering::escaped_ifield_clobber_set_,
986 &LocalValueNumbering::MergeEscapedIFieldClobberSets>();
987 MergeSets<EscapedArrayClobberSet, &LocalValueNumbering::escaped_array_clobber_set_,
988 &LocalValueNumbering::MergeEscapedArrayClobberSets>();
989 }
990
991 MergeSets<SFieldToValueMap, &LocalValueNumbering::sfield_value_map_,
992 &LocalValueNumbering::MergeSFieldValues>();
993 MergeSets<AliasingIFieldValuesMap, &LocalValueNumbering::aliasing_ifield_value_map_,
994 &LocalValueNumbering::MergeAliasingValues<
995 AliasingIFieldValuesMap, &LocalValueNumbering::aliasing_ifield_value_map_,
996 AliasingIFieldVersions>>();
997 MergeSets<AliasingArrayValuesMap, &LocalValueNumbering::aliasing_array_value_map_,
998 &LocalValueNumbering::MergeAliasingValues<
999 AliasingArrayValuesMap, &LocalValueNumbering::aliasing_array_value_map_,
1000 AliasingArrayVersions>>();
Vladimir Markof59f18b2014-02-17 15:53:57 +00001001}
1002
Vladimir Markoa4426cf2014-10-22 17:15:53 +01001003void LocalValueNumbering::PrepareEntryBlock() {
1004 uint32_t vreg = gvn_->GetMirGraph()->GetFirstInVR();
1005 CompilationUnit* cu = gvn_->GetCompilationUnit();
1006 const char* shorty = cu->shorty;
1007 ++shorty; // Skip return value.
1008 if ((cu->access_flags & kAccStatic) == 0) {
1009 // If non-static method, mark "this" as non-null
1010 uint16_t value_name = GetOperandValue(vreg);
1011 ++vreg;
1012 null_checked_.insert(value_name);
1013 }
1014 for ( ; *shorty != 0; ++shorty, ++vreg) {
1015 if (*shorty == 'J' || *shorty == 'D') {
1016 uint16_t value_name = GetOperandValueWide(vreg);
1017 SetOperandValueWide(vreg, value_name);
1018 ++vreg;
1019 }
1020 }
1021}
1022
Vladimir Markof59f18b2014-02-17 15:53:57 +00001023uint16_t LocalValueNumbering::MarkNonAliasingNonNull(MIR* mir) {
1024 uint16_t res = GetOperandValue(mir->ssa_rep->defs[0]);
Vladimir Markof59f18b2014-02-17 15:53:57 +00001025 DCHECK(null_checked_.find(res) == null_checked_.end());
1026 null_checked_.insert(res);
1027 non_aliasing_refs_.insert(res);
1028 return res;
1029}
1030
Vladimir Marko95a05972014-05-30 10:01:32 +01001031bool LocalValueNumbering::IsNonAliasing(uint16_t reg) const {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001032 return non_aliasing_refs_.find(reg) != non_aliasing_refs_.end();
Vladimir Markof59f18b2014-02-17 15:53:57 +00001033}
1034
Vladimir Marko95a05972014-05-30 10:01:32 +01001035bool LocalValueNumbering::IsNonAliasingIField(uint16_t reg, uint16_t field_id,
1036 uint16_t type) const {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001037 if (IsNonAliasing(reg)) {
1038 return true;
1039 }
Vladimir Marko95a05972014-05-30 10:01:32 +01001040 if (escaped_refs_.find(reg) == escaped_refs_.end()) {
1041 return false;
1042 }
1043 // Check for IPUTs to unresolved fields.
1044 EscapedIFieldClobberKey key1 = { reg, type, kNoValue };
1045 if (escaped_ifield_clobber_set_.find(key1) != escaped_ifield_clobber_set_.end()) {
1046 return false;
1047 }
1048 // Check for aliased IPUTs to the same field.
1049 EscapedIFieldClobberKey key2 = { reg, type, field_id };
1050 return escaped_ifield_clobber_set_.find(key2) == escaped_ifield_clobber_set_.end();
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001051}
1052
Vladimir Marko95a05972014-05-30 10:01:32 +01001053bool LocalValueNumbering::IsNonAliasingArray(uint16_t reg, uint16_t type) const {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001054 if (IsNonAliasing(reg)) {
1055 return true;
1056 }
Vladimir Marko95a05972014-05-30 10:01:32 +01001057 if (escaped_refs_.count(reg) == 0u) {
1058 return false;
1059 }
1060 // Check for aliased APUTs.
1061 EscapedArrayClobberKey key = { reg, type };
1062 return escaped_array_clobber_set_.find(key) == escaped_array_clobber_set_.end();
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001063}
1064
Vladimir Markof59f18b2014-02-17 15:53:57 +00001065void LocalValueNumbering::HandleNullCheck(MIR* mir, uint16_t reg) {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001066 auto lb = null_checked_.lower_bound(reg);
1067 if (lb != null_checked_.end() && *lb == reg) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001068 if (LIKELY(gvn_->CanModify())) {
1069 if (gvn_->GetCompilationUnit()->verbose) {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001070 LOG(INFO) << "Removing null check for 0x" << std::hex << mir->offset;
1071 }
1072 mir->optimization_flags |= MIR_IGNORE_NULL_CHECK;
Vladimir Markof59f18b2014-02-17 15:53:57 +00001073 }
Vladimir Markof59f18b2014-02-17 15:53:57 +00001074 } else {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001075 null_checked_.insert(lb, reg);
Vladimir Markof59f18b2014-02-17 15:53:57 +00001076 }
1077}
1078
1079void LocalValueNumbering::HandleRangeCheck(MIR* mir, uint16_t array, uint16_t index) {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001080 RangeCheckKey key = { array, index };
1081 auto lb = range_checked_.lower_bound(key);
1082 if (lb != range_checked_.end() && !RangeCheckKeyComparator()(key, *lb)) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001083 if (LIKELY(gvn_->CanModify())) {
1084 if (gvn_->GetCompilationUnit()->verbose) {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001085 LOG(INFO) << "Removing range check for 0x" << std::hex << mir->offset;
1086 }
1087 mir->optimization_flags |= MIR_IGNORE_RANGE_CHECK;
Vladimir Markof59f18b2014-02-17 15:53:57 +00001088 }
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001089 } else {
1090 // Mark range check completed.
1091 range_checked_.insert(lb, key);
Vladimir Markof59f18b2014-02-17 15:53:57 +00001092 }
Vladimir Markof59f18b2014-02-17 15:53:57 +00001093}
1094
Razvan A Lupusorue0951142014-11-14 14:36:55 -08001095void LocalValueNumbering::HandleDivZeroCheck(MIR* mir, uint16_t reg) {
1096 auto lb = div_zero_checked_.lower_bound(reg);
1097 if (lb != div_zero_checked_.end() && *lb == reg) {
1098 if (LIKELY(gvn_->CanModify())) {
1099 if (gvn_->GetCompilationUnit()->verbose) {
1100 LOG(INFO) << "Removing div zero check for 0x" << std::hex << mir->offset;
1101 }
1102 mir->optimization_flags |= MIR_IGNORE_DIV_ZERO_CHECK;
1103 }
1104 } else {
1105 div_zero_checked_.insert(lb, reg);
1106 }
1107}
1108
Vladimir Markof59f18b2014-02-17 15:53:57 +00001109void LocalValueNumbering::HandlePutObject(MIR* mir) {
1110 // If we're storing a non-aliasing reference, stop tracking it as non-aliasing now.
1111 uint16_t base = GetOperandValue(mir->ssa_rep->uses[0]);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001112 HandleEscapingRef(base);
Vladimir Marko743b98c2014-11-24 19:45:41 +00001113 if (gvn_->CanModify() && null_checked_.count(base) != 0u) {
1114 if (gvn_->GetCompilationUnit()->verbose) {
1115 LOG(INFO) << "Removing GC card mark value null check for 0x" << std::hex << mir->offset;
1116 }
1117 mir->optimization_flags |= MIR_STORE_NON_NULL_VALUE;
1118 }
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001119}
1120
1121void LocalValueNumbering::HandleEscapingRef(uint16_t base) {
1122 auto it = non_aliasing_refs_.find(base);
1123 if (it != non_aliasing_refs_.end()) {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001124 non_aliasing_refs_.erase(it);
Vladimir Marko95a05972014-05-30 10:01:32 +01001125 escaped_refs_.insert(base);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001126 }
1127}
1128
Vladimir Markoa4426cf2014-10-22 17:15:53 +01001129void LocalValueNumbering::HandleInvokeArgs(const MIR* mir, const LocalValueNumbering* mir_lvn) {
1130 const int32_t* uses = mir->ssa_rep->uses;
1131 const int32_t* uses_end = uses + mir->ssa_rep->num_uses;
1132 while (uses != uses_end) {
1133 uint16_t sreg = *uses;
1134 ++uses;
1135 // Avoid LookupValue() so that we don't store new values in the global value map.
1136 auto local_it = mir_lvn->sreg_value_map_.find(sreg);
1137 if (local_it != mir_lvn->sreg_value_map_.end()) {
1138 non_aliasing_refs_.erase(local_it->second);
1139 } else {
1140 uint16_t value_name = gvn_->FindValue(kNoValue, sreg, kNoValue, kNoValue);
1141 if (value_name != kNoValue) {
1142 non_aliasing_refs_.erase(value_name);
1143 }
1144 }
1145 }
1146}
1147
Vladimir Marko95a05972014-05-30 10:01:32 +01001148uint16_t LocalValueNumbering::HandlePhi(MIR* mir) {
1149 if (gvn_->merge_lvns_.empty()) {
1150 // Running LVN without a full GVN?
1151 return kNoValue;
1152 }
Vladimir Marko95a05972014-05-30 10:01:32 +01001153 int32_t* uses = mir->ssa_rep->uses;
1154 // Try to find out if this is merging wide regs.
1155 if (mir->ssa_rep->defs[0] != 0 &&
1156 sreg_wide_value_map_.count(mir->ssa_rep->defs[0] - 1) != 0u) {
1157 // This is the high part of a wide reg. Ignore the Phi.
1158 return kNoValue;
1159 }
Vladimir Markoa4426cf2014-10-22 17:15:53 +01001160 BasicBlockId* incoming = mir->meta.phi_incoming;
1161 int16_t pos = 0;
1162 // Check if we're merging a wide value based on the first merged LVN.
1163 const LocalValueNumbering* first_lvn = gvn_->merge_lvns_[0];
1164 DCHECK_LT(pos, mir->ssa_rep->num_uses);
1165 while (incoming[pos] != first_lvn->Id()) {
1166 ++pos;
1167 DCHECK_LT(pos, mir->ssa_rep->num_uses);
Vladimir Marko95a05972014-05-30 10:01:32 +01001168 }
Vladimir Markoa4426cf2014-10-22 17:15:53 +01001169 int first_s_reg = uses[pos];
1170 bool wide = (first_lvn->sreg_wide_value_map_.count(first_s_reg) != 0u);
Vladimir Marko95a05972014-05-30 10:01:32 +01001171 // Iterate over *merge_lvns_ and skip incoming sregs for BBs without associated LVN.
1172 uint16_t value_name = kNoValue;
1173 merge_names_.clear();
Vladimir Marko95a05972014-05-30 10:01:32 +01001174 bool same_values = true;
1175 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
1176 DCHECK_LT(pos, mir->ssa_rep->num_uses);
1177 while (incoming[pos] != lvn->Id()) {
1178 ++pos;
1179 DCHECK_LT(pos, mir->ssa_rep->num_uses);
1180 }
1181 int s_reg = uses[pos];
1182 ++pos;
1183 value_name = wide ? lvn->GetOperandValueWide(s_reg) : lvn->GetOperandValue(s_reg);
1184
1185 same_values = same_values && (merge_names_.empty() || value_name == merge_names_.back());
1186 merge_names_.push_back(value_name);
1187 }
1188 if (same_values) {
1189 // value_name already contains the result.
1190 } else {
1191 auto lb = merge_map_.lower_bound(merge_names_);
1192 if (lb != merge_map_.end() && !merge_map_.key_comp()(merge_names_, lb->first)) {
1193 value_name = lb->second;
1194 } else {
1195 value_name = gvn_->LookupValue(kNoValue, mir->ssa_rep->defs[0], kNoValue, kNoValue);
1196 merge_map_.PutBefore(lb, merge_names_, value_name);
1197 if (!wide && gvn_->NullCheckedInAllPredecessors(merge_names_)) {
1198 null_checked_.insert(value_name);
1199 }
Razvan A Lupusorue0951142014-11-14 14:36:55 -08001200 if (gvn_->DivZeroCheckedInAllPredecessors(merge_names_)) {
1201 div_zero_checked_.insert(value_name);
1202 }
Vladimir Marko95a05972014-05-30 10:01:32 +01001203 }
1204 }
1205 if (wide) {
1206 SetOperandValueWide(mir->ssa_rep->defs[0], value_name);
1207 } else {
1208 SetOperandValue(mir->ssa_rep->defs[0], value_name);
1209 }
1210 return value_name;
1211}
1212
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001213uint16_t LocalValueNumbering::HandleAGet(MIR* mir, uint16_t opcode) {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001214 uint16_t array = GetOperandValue(mir->ssa_rep->uses[0]);
1215 HandleNullCheck(mir, array);
1216 uint16_t index = GetOperandValue(mir->ssa_rep->uses[1]);
1217 HandleRangeCheck(mir, array, index);
Vladimir Markoaf6925b2014-10-31 16:37:32 +00001218 uint16_t type = AGetMemAccessType(static_cast<Instruction::Code>(opcode));
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001219 // Establish value number for loaded register.
1220 uint16_t res;
1221 if (IsNonAliasingArray(array, type)) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001222 res = HandleAliasingValuesGet<NonAliasingArrayVersions>(&non_aliasing_array_value_map_,
1223 array, index);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001224 } else {
Vladimir Marko95a05972014-05-30 10:01:32 +01001225 uint16_t location = gvn_->GetArrayLocation(array, index);
1226 res = HandleAliasingValuesGet<AliasingArrayVersions>(&aliasing_array_value_map_,
1227 type, location);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001228 }
1229 if (opcode == Instruction::AGET_WIDE) {
1230 SetOperandValueWide(mir->ssa_rep->defs[0], res);
1231 } else {
1232 SetOperandValue(mir->ssa_rep->defs[0], res);
1233 }
1234 return res;
1235}
1236
1237void LocalValueNumbering::HandleAPut(MIR* mir, uint16_t opcode) {
1238 int array_idx = (opcode == Instruction::APUT_WIDE) ? 2 : 1;
1239 int index_idx = array_idx + 1;
1240 uint16_t array = GetOperandValue(mir->ssa_rep->uses[array_idx]);
1241 HandleNullCheck(mir, array);
1242 uint16_t index = GetOperandValue(mir->ssa_rep->uses[index_idx]);
1243 HandleRangeCheck(mir, array, index);
1244
Vladimir Markoaf6925b2014-10-31 16:37:32 +00001245 uint16_t type = APutMemAccessType(static_cast<Instruction::Code>(opcode));
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001246 uint16_t value = (opcode == Instruction::APUT_WIDE)
1247 ? GetOperandValueWide(mir->ssa_rep->uses[0])
1248 : GetOperandValue(mir->ssa_rep->uses[0]);
1249 if (IsNonAliasing(array)) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001250 bool put_is_live = HandleAliasingValuesPut<NonAliasingArrayVersions>(
1251 &non_aliasing_array_value_map_, array, index, value);
1252 if (!put_is_live) {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001253 // This APUT can be eliminated, it stores the same value that's already in the field.
1254 // TODO: Eliminate the APUT.
1255 return;
1256 }
Vladimir Marko95a05972014-05-30 10:01:32 +01001257 } else {
1258 uint16_t location = gvn_->GetArrayLocation(array, index);
1259 bool put_is_live = HandleAliasingValuesPut<AliasingArrayVersions>(
1260 &aliasing_array_value_map_, type, location, value);
1261 if (!put_is_live) {
1262 // This APUT can be eliminated, it stores the same value that's already in the field.
1263 // TODO: Eliminate the APUT.
1264 return;
1265 }
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001266
Vladimir Marko95a05972014-05-30 10:01:32 +01001267 // Clobber all escaped array refs for this type.
1268 for (uint16_t escaped_array : escaped_refs_) {
1269 EscapedArrayClobberKey clobber_key = { escaped_array, type };
1270 escaped_array_clobber_set_.insert(clobber_key);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001271 }
1272 }
1273}
1274
1275uint16_t LocalValueNumbering::HandleIGet(MIR* mir, uint16_t opcode) {
1276 uint16_t base = GetOperandValue(mir->ssa_rep->uses[0]);
1277 HandleNullCheck(mir, base);
Vladimir Marko95a05972014-05-30 10:01:32 +01001278 const MirFieldInfo& field_info = gvn_->GetMirGraph()->GetIFieldLoweringInfo(mir);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001279 uint16_t res;
1280 if (!field_info.IsResolved() || field_info.IsVolatile()) {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001281 // Unresolved fields may be volatile, so handle them as such to be safe.
Vladimir Markofa236452014-09-29 17:58:10 +01001282 HandleInvokeOrClInitOrAcquireOp(mir); // Volatile GETs have acquire semantics.
1283 // Volatile fields always get a new memory version; field id is irrelevant.
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001284 // Use result s_reg - will be unique.
Vladimir Marko95a05972014-05-30 10:01:32 +01001285 res = gvn_->LookupValue(kNoValue, mir->ssa_rep->defs[0], kNoValue, kNoValue);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001286 } else {
Vladimir Markoaf6925b2014-10-31 16:37:32 +00001287 uint16_t type = IGetMemAccessType(static_cast<Instruction::Code>(opcode));
1288 uint16_t field_id = gvn_->GetIFieldId(mir);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001289 if (IsNonAliasingIField(base, field_id, type)) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001290 uint16_t loc = gvn_->LookupValue(kNonAliasingIFieldLocOp, base, field_id, type);
1291 auto lb = non_aliasing_ifield_value_map_.lower_bound(loc);
1292 if (lb != non_aliasing_ifield_value_map_.end() && lb->first == loc) {
1293 res = lb->second;
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001294 } else {
Vladimir Marko95a05972014-05-30 10:01:32 +01001295 res = gvn_->LookupValue(kNonAliasingIFieldInitialOp, loc, kNoValue, kNoValue);
1296 non_aliasing_ifield_value_map_.PutBefore(lb, loc, res);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001297 }
Vladimir Marko95a05972014-05-30 10:01:32 +01001298 } else {
1299 res = HandleAliasingValuesGet<AliasingIFieldVersions>(&aliasing_ifield_value_map_,
1300 field_id, base);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001301 }
1302 }
1303 if (opcode == Instruction::IGET_WIDE) {
1304 SetOperandValueWide(mir->ssa_rep->defs[0], res);
1305 } else {
1306 SetOperandValue(mir->ssa_rep->defs[0], res);
1307 }
1308 return res;
1309}
1310
1311void LocalValueNumbering::HandleIPut(MIR* mir, uint16_t opcode) {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001312 int base_reg = (opcode == Instruction::IPUT_WIDE) ? 2 : 1;
1313 uint16_t base = GetOperandValue(mir->ssa_rep->uses[base_reg]);
1314 HandleNullCheck(mir, base);
Vladimir Markoaf6925b2014-10-31 16:37:32 +00001315 uint16_t type = IPutMemAccessType(static_cast<Instruction::Code>(opcode));
Vladimir Marko95a05972014-05-30 10:01:32 +01001316 const MirFieldInfo& field_info = gvn_->GetMirGraph()->GetIFieldLoweringInfo(mir);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001317 if (!field_info.IsResolved()) {
1318 // Unresolved fields always alias with everything of the same type.
1319 // Use mir->offset as modifier; without elaborate inlining, it will be unique.
1320 unresolved_ifield_version_[type] =
Vladimir Marko95a05972014-05-30 10:01:32 +01001321 gvn_->LookupValue(kUnresolvedIFieldOp, kNoValue, kNoValue, mir->offset);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001322
Vladimir Marko95a05972014-05-30 10:01:32 +01001323 // For simplicity, treat base as escaped now.
1324 HandleEscapingRef(base);
1325
1326 // Clobber all fields of escaped references of the same type.
1327 for (uint16_t escaped_ref : escaped_refs_) {
1328 EscapedIFieldClobberKey clobber_key = { escaped_ref, type, kNoValue };
1329 escaped_ifield_clobber_set_.insert(clobber_key);
1330 }
1331
1332 // Aliasing fields of the same type may have been overwritten.
1333 auto it = aliasing_ifield_value_map_.begin(), end = aliasing_ifield_value_map_.end();
1334 while (it != end) {
Vladimir Markoaf6925b2014-10-31 16:37:32 +00001335 if (gvn_->GetIFieldType(it->first) != type) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001336 ++it;
1337 } else {
1338 it = aliasing_ifield_value_map_.erase(it);
1339 }
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001340 }
1341 } else if (field_info.IsVolatile()) {
1342 // Nothing to do, resolved volatile fields always get a new memory version anyway and
1343 // can't alias with resolved non-volatile fields.
1344 } else {
Vladimir Markoaf6925b2014-10-31 16:37:32 +00001345 uint16_t field_id = gvn_->GetIFieldId(mir);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001346 uint16_t value = (opcode == Instruction::IPUT_WIDE)
1347 ? GetOperandValueWide(mir->ssa_rep->uses[0])
1348 : GetOperandValue(mir->ssa_rep->uses[0]);
1349 if (IsNonAliasing(base)) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001350 uint16_t loc = gvn_->LookupValue(kNonAliasingIFieldLocOp, base, field_id, type);
1351 auto lb = non_aliasing_ifield_value_map_.lower_bound(loc);
1352 if (lb != non_aliasing_ifield_value_map_.end() && lb->first == loc) {
1353 if (lb->second == value) {
1354 // This IPUT can be eliminated, it stores the same value that's already in the field.
1355 // TODO: Eliminate the IPUT.
1356 return;
1357 }
1358 lb->second = value; // Overwrite.
1359 } else {
1360 non_aliasing_ifield_value_map_.PutBefore(lb, loc, value);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001361 }
Vladimir Marko95a05972014-05-30 10:01:32 +01001362 } else {
1363 bool put_is_live = HandleAliasingValuesPut<AliasingIFieldVersions>(
1364 &aliasing_ifield_value_map_, field_id, base, value);
1365 if (!put_is_live) {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001366 // This IPUT can be eliminated, it stores the same value that's already in the field.
1367 // TODO: Eliminate the IPUT.
1368 return;
1369 }
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001370
Vladimir Marko95a05972014-05-30 10:01:32 +01001371 // Clobber all fields of escaped references for this field.
1372 for (uint16_t escaped_ref : escaped_refs_) {
1373 EscapedIFieldClobberKey clobber_key = { escaped_ref, type, field_id };
1374 escaped_ifield_clobber_set_.insert(clobber_key);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001375 }
1376 }
1377 }
1378}
1379
1380uint16_t LocalValueNumbering::HandleSGet(MIR* mir, uint16_t opcode) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001381 const MirSFieldLoweringInfo& field_info = gvn_->GetMirGraph()->GetSFieldLoweringInfo(mir);
Vladimir Markofa236452014-09-29 17:58:10 +01001382 if (!field_info.IsResolved() || field_info.IsVolatile() ||
Vladimir Marko66c6d7b2014-10-16 15:41:48 +01001383 (!field_info.IsClassInitialized() &&
1384 (mir->optimization_flags & MIR_CLASS_IS_INITIALIZED) == 0)) {
Vladimir Markofa236452014-09-29 17:58:10 +01001385 // Volatile SGETs (and unresolved fields are potentially volatile) have acquire semantics
1386 // and class initialization can call arbitrary functions, we need to wipe aliasing values.
1387 HandleInvokeOrClInitOrAcquireOp(mir);
Vladimir Markof418f322014-07-09 14:45:36 +01001388 }
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001389 uint16_t res;
1390 if (!field_info.IsResolved() || field_info.IsVolatile()) {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001391 // Unresolved fields may be volatile, so handle them as such to be safe.
Vladimir Markofa236452014-09-29 17:58:10 +01001392 // Volatile fields always get a new memory version; field id is irrelevant.
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001393 // Use result s_reg - will be unique.
Vladimir Marko95a05972014-05-30 10:01:32 +01001394 res = gvn_->LookupValue(kNoValue, mir->ssa_rep->defs[0], kNoValue, kNoValue);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001395 } else {
Vladimir Markoaf6925b2014-10-31 16:37:32 +00001396 uint16_t type = SGetMemAccessType(static_cast<Instruction::Code>(opcode));
1397 uint16_t field_id = gvn_->GetSFieldId(mir);
Vladimir Marko95a05972014-05-30 10:01:32 +01001398 auto lb = sfield_value_map_.lower_bound(field_id);
1399 if (lb != sfield_value_map_.end() && lb->first == field_id) {
1400 res = lb->second;
1401 } else {
1402 // Resolved non-volatile static fields can alias with non-resolved fields of the same type,
1403 // so we need to use unresolved_sfield_version_[type] in addition to global_memory_version_
1404 // to determine the version of the field.
1405 res = gvn_->LookupValue(kResolvedSFieldOp, field_id,
1406 unresolved_sfield_version_[type], global_memory_version_);
1407 sfield_value_map_.PutBefore(lb, field_id, res);
1408 }
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001409 }
1410 if (opcode == Instruction::SGET_WIDE) {
1411 SetOperandValueWide(mir->ssa_rep->defs[0], res);
1412 } else {
1413 SetOperandValue(mir->ssa_rep->defs[0], res);
1414 }
1415 return res;
1416}
1417
1418void LocalValueNumbering::HandleSPut(MIR* mir, uint16_t opcode) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001419 const MirSFieldLoweringInfo& field_info = gvn_->GetMirGraph()->GetSFieldLoweringInfo(mir);
Vladimir Marko66c6d7b2014-10-16 15:41:48 +01001420 if (!field_info.IsClassInitialized() &&
1421 (mir->optimization_flags & MIR_CLASS_IS_INITIALIZED) == 0) {
Vladimir Markof418f322014-07-09 14:45:36 +01001422 // Class initialization can call arbitrary functions, we need to wipe aliasing values.
Vladimir Markofa236452014-09-29 17:58:10 +01001423 HandleInvokeOrClInitOrAcquireOp(mir);
Vladimir Markof418f322014-07-09 14:45:36 +01001424 }
Vladimir Markoaf6925b2014-10-31 16:37:32 +00001425 uint16_t type = SPutMemAccessType(static_cast<Instruction::Code>(opcode));
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001426 if (!field_info.IsResolved()) {
1427 // Unresolved fields always alias with everything of the same type.
1428 // Use mir->offset as modifier; without elaborate inlining, it will be unique.
1429 unresolved_sfield_version_[type] =
Vladimir Marko95a05972014-05-30 10:01:32 +01001430 gvn_->LookupValue(kUnresolvedSFieldOp, kNoValue, kNoValue, mir->offset);
1431 RemoveSFieldsForType(type);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001432 } else if (field_info.IsVolatile()) {
1433 // Nothing to do, resolved volatile fields always get a new memory version anyway and
1434 // can't alias with resolved non-volatile fields.
1435 } else {
Vladimir Markoaf6925b2014-10-31 16:37:32 +00001436 uint16_t field_id = gvn_->GetSFieldId(mir);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001437 uint16_t value = (opcode == Instruction::SPUT_WIDE)
1438 ? GetOperandValueWide(mir->ssa_rep->uses[0])
1439 : GetOperandValue(mir->ssa_rep->uses[0]);
1440 // Resolved non-volatile static fields can alias with non-resolved fields of the same type,
1441 // so we need to use unresolved_sfield_version_[type] in addition to global_memory_version_
1442 // to determine the version of the field.
Vladimir Marko95a05972014-05-30 10:01:32 +01001443 auto lb = sfield_value_map_.lower_bound(field_id);
1444 if (lb != sfield_value_map_.end() && lb->first == field_id) {
1445 if (lb->second == value) {
1446 // This SPUT can be eliminated, it stores the same value that's already in the field.
1447 // TODO: Eliminate the SPUT.
1448 return;
1449 }
1450 lb->second = value; // Overwrite.
1451 } else {
1452 sfield_value_map_.PutBefore(lb, field_id, value);
1453 }
1454 }
1455}
1456
1457void LocalValueNumbering::RemoveSFieldsForType(uint16_t type) {
1458 // Erase all static fields of this type from the sfield_value_map_.
1459 for (auto it = sfield_value_map_.begin(), end = sfield_value_map_.end(); it != end; ) {
Vladimir Markoaf6925b2014-10-31 16:37:32 +00001460 if (gvn_->GetSFieldType(it->first) == type) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001461 it = sfield_value_map_.erase(it);
1462 } else {
1463 ++it;
1464 }
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001465 }
Vladimir Markof59f18b2014-02-17 15:53:57 +00001466}
buzbee2502e002012-12-31 16:05:53 -08001467
Vladimir Markofa236452014-09-29 17:58:10 +01001468void LocalValueNumbering::HandleInvokeOrClInitOrAcquireOp(MIR* mir) {
Vladimir Markof418f322014-07-09 14:45:36 +01001469 // Use mir->offset as modifier; without elaborate inlining, it will be unique.
Vladimir Marko95a05972014-05-30 10:01:32 +01001470 global_memory_version_ =
1471 gvn_->LookupValue(kInvokeMemoryVersionBumpOp, 0u, 0u, mir->offset);
1472 // All static fields and instance fields and array elements of aliasing references,
1473 // including escaped references, may have been modified.
1474 sfield_value_map_.clear();
1475 aliasing_ifield_value_map_.clear();
1476 aliasing_array_value_map_.clear();
1477 escaped_refs_.clear();
1478 escaped_ifield_clobber_set_.clear();
1479 escaped_array_clobber_set_.clear();
Vladimir Markof418f322014-07-09 14:45:36 +01001480}
1481
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001482uint16_t LocalValueNumbering::GetValueNumber(MIR* mir) {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001483 uint16_t res = kNoValue;
buzbee2502e002012-12-31 16:05:53 -08001484 uint16_t opcode = mir->dalvikInsn.opcode;
1485 switch (opcode) {
1486 case Instruction::NOP:
1487 case Instruction::RETURN_VOID:
1488 case Instruction::RETURN:
1489 case Instruction::RETURN_OBJECT:
1490 case Instruction::RETURN_WIDE:
buzbee2502e002012-12-31 16:05:53 -08001491 case Instruction::GOTO:
1492 case Instruction::GOTO_16:
1493 case Instruction::GOTO_32:
1494 case Instruction::CHECK_CAST:
1495 case Instruction::THROW:
1496 case Instruction::FILL_ARRAY_DATA:
buzbee2502e002012-12-31 16:05:53 -08001497 case Instruction::PACKED_SWITCH:
1498 case Instruction::SPARSE_SWITCH:
1499 case Instruction::IF_EQ:
1500 case Instruction::IF_NE:
1501 case Instruction::IF_LT:
1502 case Instruction::IF_GE:
1503 case Instruction::IF_GT:
1504 case Instruction::IF_LE:
1505 case Instruction::IF_EQZ:
1506 case Instruction::IF_NEZ:
1507 case Instruction::IF_LTZ:
1508 case Instruction::IF_GEZ:
1509 case Instruction::IF_GTZ:
1510 case Instruction::IF_LEZ:
buzbee2502e002012-12-31 16:05:53 -08001511 case kMirOpFusedCmplFloat:
1512 case kMirOpFusedCmpgFloat:
1513 case kMirOpFusedCmplDouble:
1514 case kMirOpFusedCmpgDouble:
1515 case kMirOpFusedCmpLong:
1516 // Nothing defined - take no action.
1517 break;
1518
Vladimir Marko95a05972014-05-30 10:01:32 +01001519 case Instruction::MONITOR_ENTER:
1520 HandleNullCheck(mir, GetOperandValue(mir->ssa_rep->uses[0]));
Vladimir Markofa236452014-09-29 17:58:10 +01001521 HandleInvokeOrClInitOrAcquireOp(mir); // Acquire operation.
Vladimir Marko95a05972014-05-30 10:01:32 +01001522 break;
1523
1524 case Instruction::MONITOR_EXIT:
1525 HandleNullCheck(mir, GetOperandValue(mir->ssa_rep->uses[0]));
1526 // If we're running GVN and CanModify(), uneliminated null check indicates bytecode error.
Vladimir Marko415ac882014-09-30 18:09:14 +01001527 if ((mir->optimization_flags & MIR_IGNORE_NULL_CHECK) == 0 &&
1528 gvn_->work_lvn_ != nullptr && gvn_->CanModify()) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001529 LOG(WARNING) << "Bytecode error: MONITOR_EXIT is still null checked at 0x" << std::hex
1530 << mir->offset << " in " << PrettyMethod(gvn_->cu_->method_idx, *gvn_->cu_->dex_file);
1531 }
1532 break;
1533
Vladimir Markof59f18b2014-02-17 15:53:57 +00001534 case Instruction::FILLED_NEW_ARRAY:
1535 case Instruction::FILLED_NEW_ARRAY_RANGE:
1536 // Nothing defined but the result will be unique and non-null.
1537 if (mir->next != nullptr && mir->next->dalvikInsn.opcode == Instruction::MOVE_RESULT_OBJECT) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001538 uint16_t array = MarkNonAliasingNonNull(mir->next);
1539 // Do not SetOperandValue(), we'll do that when we process the MOVE_RESULT_OBJECT.
1540 if (kLocalValueNumberingEnableFilledNewArrayTracking && mir->ssa_rep->num_uses != 0u) {
1541 AliasingValues* values = GetAliasingValues(&non_aliasing_array_value_map_, array);
1542 // Clear the value if we got a merged version in a loop.
Vladimir Markob19955d2014-07-29 12:04:10 +01001543 *values = AliasingValues(this);
Vladimir Marko95a05972014-05-30 10:01:32 +01001544 for (size_t i = 0u, count = mir->ssa_rep->num_uses; i != count; ++i) {
1545 DCHECK_EQ(High16Bits(i), 0u);
1546 uint16_t index = gvn_->LookupValue(Instruction::CONST, i, 0u, 0);
1547 uint16_t value = GetOperandValue(mir->ssa_rep->uses[i]);
1548 values->load_value_map.Put(index, value);
1549 RangeCheckKey key = { array, index };
1550 range_checked_.insert(key);
1551 }
1552 }
Vladimir Markof59f18b2014-02-17 15:53:57 +00001553 // The MOVE_RESULT_OBJECT will be processed next and we'll return the value name then.
1554 }
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001555 // All args escaped (if references).
1556 for (size_t i = 0u, count = mir->ssa_rep->num_uses; i != count; ++i) {
1557 uint16_t reg = GetOperandValue(mir->ssa_rep->uses[i]);
1558 HandleEscapingRef(reg);
1559 }
Vladimir Markof59f18b2014-02-17 15:53:57 +00001560 break;
1561
Vladimir Markoa78e66a2014-10-16 13:38:44 +01001562 case kMirOpNullCheck:
1563 HandleNullCheck(mir, GetOperandValue(mir->ssa_rep->uses[0]));
1564 break;
1565
Vladimir Markof59f18b2014-02-17 15:53:57 +00001566 case Instruction::INVOKE_DIRECT:
1567 case Instruction::INVOKE_DIRECT_RANGE:
1568 case Instruction::INVOKE_VIRTUAL:
1569 case Instruction::INVOKE_VIRTUAL_RANGE:
1570 case Instruction::INVOKE_SUPER:
1571 case Instruction::INVOKE_SUPER_RANGE:
1572 case Instruction::INVOKE_INTERFACE:
1573 case Instruction::INVOKE_INTERFACE_RANGE: {
1574 // Nothing defined but handle the null check.
1575 uint16_t reg = GetOperandValue(mir->ssa_rep->uses[0]);
1576 HandleNullCheck(mir, reg);
1577 }
Ian Rogersfc787ec2014-10-09 21:56:44 -07001578 FALLTHROUGH_INTENDED;
Vladimir Markof59f18b2014-02-17 15:53:57 +00001579 case Instruction::INVOKE_STATIC:
1580 case Instruction::INVOKE_STATIC_RANGE:
Vladimir Markoff0ac472014-10-02 17:24:53 +01001581 // Make ref args aliasing.
Vladimir Markoa4426cf2014-10-22 17:15:53 +01001582 HandleInvokeArgs(mir, this);
Vladimir Markoff0ac472014-10-02 17:24:53 +01001583 HandleInvokeOrClInitOrAcquireOp(mir);
Vladimir Markof59f18b2014-02-17 15:53:57 +00001584 break;
1585
buzbee2502e002012-12-31 16:05:53 -08001586 case Instruction::MOVE_RESULT:
1587 case Instruction::MOVE_RESULT_OBJECT:
1588 case Instruction::INSTANCE_OF:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001589 // 1 result, treat as unique each time, use result s_reg - will be unique.
1590 res = GetOperandValue(mir->ssa_rep->defs[0]);
1591 SetOperandValue(mir->ssa_rep->defs[0], res);
1592 break;
1593 case Instruction::MOVE_EXCEPTION:
buzbee2502e002012-12-31 16:05:53 -08001594 case Instruction::NEW_INSTANCE:
buzbee2502e002012-12-31 16:05:53 -08001595 case Instruction::CONST_CLASS:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001596 case Instruction::NEW_ARRAY:
Vladimir Markob3e527b2014-04-04 12:37:07 +01001597 // 1 result, treat as unique each time, use result s_reg - will be unique.
1598 res = MarkNonAliasingNonNull(mir);
Vladimir Marko95a05972014-05-30 10:01:32 +01001599 SetOperandValue(mir->ssa_rep->defs[0], res);
buzbee2502e002012-12-31 16:05:53 -08001600 break;
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001601 case Instruction::CONST_STRING:
1602 case Instruction::CONST_STRING_JUMBO:
1603 // These strings are internalized, so assign value based on the string pool index.
Vladimir Marko95a05972014-05-30 10:01:32 +01001604 res = gvn_->LookupValue(Instruction::CONST_STRING, Low16Bits(mir->dalvikInsn.vB),
1605 High16Bits(mir->dalvikInsn.vB), 0);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001606 SetOperandValue(mir->ssa_rep->defs[0], res);
1607 null_checked_.insert(res); // May already be there.
1608 // NOTE: Hacking the contents of an internalized string via reflection is possible
1609 // but the behavior is undefined. Therefore, we consider the string constant and
1610 // the reference non-aliasing.
1611 // TUNING: We could keep this property even if the reference "escapes".
1612 non_aliasing_refs_.insert(res); // May already be there.
1613 break;
Vladimir Markof59f18b2014-02-17 15:53:57 +00001614 case Instruction::MOVE_RESULT_WIDE:
Vladimir Markob3e527b2014-04-04 12:37:07 +01001615 // 1 wide result, treat as unique each time, use result s_reg - will be unique.
1616 res = GetOperandValueWide(mir->ssa_rep->defs[0]);
1617 SetOperandValueWide(mir->ssa_rep->defs[0], res);
buzbee2502e002012-12-31 16:05:53 -08001618 break;
1619
1620 case kMirOpPhi:
Vladimir Marko95a05972014-05-30 10:01:32 +01001621 res = HandlePhi(mir);
buzbee2502e002012-12-31 16:05:53 -08001622 break;
1623
1624 case Instruction::MOVE:
1625 case Instruction::MOVE_OBJECT:
1626 case Instruction::MOVE_16:
1627 case Instruction::MOVE_OBJECT_16:
1628 case Instruction::MOVE_FROM16:
1629 case Instruction::MOVE_OBJECT_FROM16:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001630 case kMirOpCopy:
1631 // Just copy value number of source to value number of result.
1632 res = GetOperandValue(mir->ssa_rep->uses[0]);
1633 SetOperandValue(mir->ssa_rep->defs[0], res);
buzbee2502e002012-12-31 16:05:53 -08001634 break;
1635
1636 case Instruction::MOVE_WIDE:
1637 case Instruction::MOVE_WIDE_16:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001638 case Instruction::MOVE_WIDE_FROM16:
1639 // Just copy value number of source to value number of result.
1640 res = GetOperandValueWide(mir->ssa_rep->uses[0]);
1641 SetOperandValueWide(mir->ssa_rep->defs[0], res);
buzbee2502e002012-12-31 16:05:53 -08001642 break;
1643
1644 case Instruction::CONST:
1645 case Instruction::CONST_4:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001646 case Instruction::CONST_16:
Vladimir Marko95a05972014-05-30 10:01:32 +01001647 res = gvn_->LookupValue(Instruction::CONST, Low16Bits(mir->dalvikInsn.vB),
1648 High16Bits(mir->dalvikInsn.vB), 0);
Vladimir Markof59f18b2014-02-17 15:53:57 +00001649 SetOperandValue(mir->ssa_rep->defs[0], res);
buzbee2502e002012-12-31 16:05:53 -08001650 break;
1651
Vladimir Markof59f18b2014-02-17 15:53:57 +00001652 case Instruction::CONST_HIGH16:
Vladimir Marko95a05972014-05-30 10:01:32 +01001653 res = gvn_->LookupValue(Instruction::CONST, 0, mir->dalvikInsn.vB, 0);
Vladimir Markof59f18b2014-02-17 15:53:57 +00001654 SetOperandValue(mir->ssa_rep->defs[0], res);
buzbee2502e002012-12-31 16:05:53 -08001655 break;
1656
1657 case Instruction::CONST_WIDE_16:
1658 case Instruction::CONST_WIDE_32: {
Vladimir Marko95a05972014-05-30 10:01:32 +01001659 uint16_t low_res = gvn_->LookupValue(Instruction::CONST, Low16Bits(mir->dalvikInsn.vB),
1660 High16Bits(mir->dalvikInsn.vB >> 16), 1);
buzbee2502e002012-12-31 16:05:53 -08001661 uint16_t high_res;
1662 if (mir->dalvikInsn.vB & 0x80000000) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001663 high_res = gvn_->LookupValue(Instruction::CONST, 0xffff, 0xffff, 2);
buzbee2502e002012-12-31 16:05:53 -08001664 } else {
Vladimir Marko95a05972014-05-30 10:01:32 +01001665 high_res = gvn_->LookupValue(Instruction::CONST, 0, 0, 2);
buzbee2502e002012-12-31 16:05:53 -08001666 }
Vladimir Marko95a05972014-05-30 10:01:32 +01001667 res = gvn_->LookupValue(Instruction::CONST, low_res, high_res, 3);
Vladimir Markof59f18b2014-02-17 15:53:57 +00001668 SetOperandValueWide(mir->ssa_rep->defs[0], res);
buzbee2502e002012-12-31 16:05:53 -08001669 }
1670 break;
1671
1672 case Instruction::CONST_WIDE: {
1673 uint32_t low_word = Low32Bits(mir->dalvikInsn.vB_wide);
1674 uint32_t high_word = High32Bits(mir->dalvikInsn.vB_wide);
Vladimir Marko95a05972014-05-30 10:01:32 +01001675 uint16_t low_res = gvn_->LookupValue(Instruction::CONST, Low16Bits(low_word),
1676 High16Bits(low_word), 1);
1677 uint16_t high_res = gvn_->LookupValue(Instruction::CONST, Low16Bits(high_word),
1678 High16Bits(high_word), 2);
1679 res = gvn_->LookupValue(Instruction::CONST, low_res, high_res, 3);
buzbee2502e002012-12-31 16:05:53 -08001680 SetOperandValueWide(mir->ssa_rep->defs[0], res);
1681 }
1682 break;
1683
1684 case Instruction::CONST_WIDE_HIGH16: {
Vladimir Marko95a05972014-05-30 10:01:32 +01001685 uint16_t low_res = gvn_->LookupValue(Instruction::CONST, 0, 0, 1);
1686 uint16_t high_res = gvn_->LookupValue(Instruction::CONST, 0,
1687 Low16Bits(mir->dalvikInsn.vB), 2);
1688 res = gvn_->LookupValue(Instruction::CONST, low_res, high_res, 3);
buzbee2502e002012-12-31 16:05:53 -08001689 SetOperandValueWide(mir->ssa_rep->defs[0], res);
1690 }
1691 break;
1692
Vladimir Marko95a05972014-05-30 10:01:32 +01001693 case Instruction::ARRAY_LENGTH: {
1694 // Handle the null check.
1695 uint16_t reg = GetOperandValue(mir->ssa_rep->uses[0]);
1696 HandleNullCheck(mir, reg);
1697 }
Ian Rogersfc787ec2014-10-09 21:56:44 -07001698 FALLTHROUGH_INTENDED;
buzbee2502e002012-12-31 16:05:53 -08001699 case Instruction::NEG_INT:
1700 case Instruction::NOT_INT:
1701 case Instruction::NEG_FLOAT:
1702 case Instruction::INT_TO_BYTE:
1703 case Instruction::INT_TO_SHORT:
1704 case Instruction::INT_TO_CHAR:
1705 case Instruction::INT_TO_FLOAT:
1706 case Instruction::FLOAT_TO_INT: {
1707 // res = op + 1 operand
1708 uint16_t operand1 = GetOperandValue(mir->ssa_rep->uses[0]);
Vladimir Marko95a05972014-05-30 10:01:32 +01001709 res = gvn_->LookupValue(opcode, operand1, kNoValue, kNoValue);
buzbee2502e002012-12-31 16:05:53 -08001710 SetOperandValue(mir->ssa_rep->defs[0], res);
1711 }
1712 break;
1713
1714 case Instruction::LONG_TO_FLOAT:
1715 case Instruction::LONG_TO_INT:
1716 case Instruction::DOUBLE_TO_FLOAT:
1717 case Instruction::DOUBLE_TO_INT: {
1718 // res = op + 1 wide operand
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001719 uint16_t operand1 = GetOperandValueWide(mir->ssa_rep->uses[0]);
Vladimir Marko95a05972014-05-30 10:01:32 +01001720 res = gvn_->LookupValue(opcode, operand1, kNoValue, kNoValue);
buzbee2502e002012-12-31 16:05:53 -08001721 SetOperandValue(mir->ssa_rep->defs[0], res);
1722 }
1723 break;
1724
buzbee2502e002012-12-31 16:05:53 -08001725 case Instruction::DOUBLE_TO_LONG:
1726 case Instruction::LONG_TO_DOUBLE:
1727 case Instruction::NEG_LONG:
1728 case Instruction::NOT_LONG:
1729 case Instruction::NEG_DOUBLE: {
1730 // wide res = op + 1 wide operand
1731 uint16_t operand1 = GetOperandValueWide(mir->ssa_rep->uses[0]);
Vladimir Marko95a05972014-05-30 10:01:32 +01001732 res = gvn_->LookupValue(opcode, operand1, kNoValue, kNoValue);
buzbee2502e002012-12-31 16:05:53 -08001733 SetOperandValueWide(mir->ssa_rep->defs[0], res);
1734 }
1735 break;
1736
1737 case Instruction::FLOAT_TO_DOUBLE:
1738 case Instruction::FLOAT_TO_LONG:
1739 case Instruction::INT_TO_DOUBLE:
1740 case Instruction::INT_TO_LONG: {
1741 // wide res = op + 1 operand
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001742 uint16_t operand1 = GetOperandValue(mir->ssa_rep->uses[0]);
Vladimir Marko95a05972014-05-30 10:01:32 +01001743 res = gvn_->LookupValue(opcode, operand1, kNoValue, kNoValue);
buzbee2502e002012-12-31 16:05:53 -08001744 SetOperandValueWide(mir->ssa_rep->defs[0], res);
1745 }
1746 break;
1747
1748 case Instruction::CMPL_DOUBLE:
1749 case Instruction::CMPG_DOUBLE:
1750 case Instruction::CMP_LONG: {
1751 // res = op + 2 wide operands
1752 uint16_t operand1 = GetOperandValueWide(mir->ssa_rep->uses[0]);
1753 uint16_t operand2 = GetOperandValueWide(mir->ssa_rep->uses[2]);
Vladimir Marko95a05972014-05-30 10:01:32 +01001754 res = gvn_->LookupValue(opcode, operand1, operand2, kNoValue);
buzbee2502e002012-12-31 16:05:53 -08001755 SetOperandValue(mir->ssa_rep->defs[0], res);
1756 }
1757 break;
1758
Razvan A Lupusorue0951142014-11-14 14:36:55 -08001759 case Instruction::DIV_INT:
1760 case Instruction::DIV_INT_2ADDR:
1761 case Instruction::REM_INT:
1762 case Instruction::REM_INT_2ADDR:
1763 HandleDivZeroCheck(mir, GetOperandValue(mir->ssa_rep->uses[1]));
1764 FALLTHROUGH_INTENDED;
1765
buzbee2502e002012-12-31 16:05:53 -08001766 case Instruction::CMPG_FLOAT:
1767 case Instruction::CMPL_FLOAT:
1768 case Instruction::ADD_INT:
1769 case Instruction::ADD_INT_2ADDR:
1770 case Instruction::MUL_INT:
1771 case Instruction::MUL_INT_2ADDR:
1772 case Instruction::AND_INT:
1773 case Instruction::AND_INT_2ADDR:
1774 case Instruction::OR_INT:
1775 case Instruction::OR_INT_2ADDR:
1776 case Instruction::XOR_INT:
1777 case Instruction::XOR_INT_2ADDR:
1778 case Instruction::SUB_INT:
1779 case Instruction::SUB_INT_2ADDR:
buzbee2502e002012-12-31 16:05:53 -08001780 case Instruction::SHL_INT:
1781 case Instruction::SHL_INT_2ADDR:
1782 case Instruction::SHR_INT:
1783 case Instruction::SHR_INT_2ADDR:
1784 case Instruction::USHR_INT:
1785 case Instruction::USHR_INT_2ADDR: {
1786 // res = op + 2 operands
1787 uint16_t operand1 = GetOperandValue(mir->ssa_rep->uses[0]);
1788 uint16_t operand2 = GetOperandValue(mir->ssa_rep->uses[1]);
Vladimir Marko95a05972014-05-30 10:01:32 +01001789 res = gvn_->LookupValue(opcode, operand1, operand2, kNoValue);
buzbee2502e002012-12-31 16:05:53 -08001790 SetOperandValue(mir->ssa_rep->defs[0], res);
1791 }
1792 break;
1793
Razvan A Lupusorue0951142014-11-14 14:36:55 -08001794 case Instruction::DIV_LONG:
1795 case Instruction::REM_LONG:
1796 case Instruction::DIV_LONG_2ADDR:
1797 case Instruction::REM_LONG_2ADDR:
1798 HandleDivZeroCheck(mir, GetOperandValueWide(mir->ssa_rep->uses[2]));
1799 FALLTHROUGH_INTENDED;
1800
buzbee2502e002012-12-31 16:05:53 -08001801 case Instruction::ADD_LONG:
1802 case Instruction::SUB_LONG:
1803 case Instruction::MUL_LONG:
buzbee2502e002012-12-31 16:05:53 -08001804 case Instruction::AND_LONG:
1805 case Instruction::OR_LONG:
1806 case Instruction::XOR_LONG:
1807 case Instruction::ADD_LONG_2ADDR:
1808 case Instruction::SUB_LONG_2ADDR:
1809 case Instruction::MUL_LONG_2ADDR:
buzbee2502e002012-12-31 16:05:53 -08001810 case Instruction::AND_LONG_2ADDR:
1811 case Instruction::OR_LONG_2ADDR:
1812 case Instruction::XOR_LONG_2ADDR:
1813 case Instruction::ADD_DOUBLE:
1814 case Instruction::SUB_DOUBLE:
1815 case Instruction::MUL_DOUBLE:
1816 case Instruction::DIV_DOUBLE:
1817 case Instruction::REM_DOUBLE:
1818 case Instruction::ADD_DOUBLE_2ADDR:
1819 case Instruction::SUB_DOUBLE_2ADDR:
1820 case Instruction::MUL_DOUBLE_2ADDR:
1821 case Instruction::DIV_DOUBLE_2ADDR:
1822 case Instruction::REM_DOUBLE_2ADDR: {
1823 // wide res = op + 2 wide operands
1824 uint16_t operand1 = GetOperandValueWide(mir->ssa_rep->uses[0]);
1825 uint16_t operand2 = GetOperandValueWide(mir->ssa_rep->uses[2]);
Vladimir Marko95a05972014-05-30 10:01:32 +01001826 res = gvn_->LookupValue(opcode, operand1, operand2, kNoValue);
buzbee2502e002012-12-31 16:05:53 -08001827 SetOperandValueWide(mir->ssa_rep->defs[0], res);
1828 }
1829 break;
1830
1831 case Instruction::SHL_LONG:
1832 case Instruction::SHR_LONG:
1833 case Instruction::USHR_LONG:
1834 case Instruction::SHL_LONG_2ADDR:
1835 case Instruction::SHR_LONG_2ADDR:
1836 case Instruction::USHR_LONG_2ADDR: {
1837 // wide res = op + 1 wide operand + 1 operand
1838 uint16_t operand1 = GetOperandValueWide(mir->ssa_rep->uses[0]);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001839 uint16_t operand2 = GetOperandValue(mir->ssa_rep->uses[2]);
Vladimir Marko95a05972014-05-30 10:01:32 +01001840 res = gvn_->LookupValue(opcode, operand1, operand2, kNoValue);
buzbee2502e002012-12-31 16:05:53 -08001841 SetOperandValueWide(mir->ssa_rep->defs[0], res);
1842 }
1843 break;
1844
1845 case Instruction::ADD_FLOAT:
1846 case Instruction::SUB_FLOAT:
1847 case Instruction::MUL_FLOAT:
1848 case Instruction::DIV_FLOAT:
1849 case Instruction::REM_FLOAT:
1850 case Instruction::ADD_FLOAT_2ADDR:
1851 case Instruction::SUB_FLOAT_2ADDR:
1852 case Instruction::MUL_FLOAT_2ADDR:
1853 case Instruction::DIV_FLOAT_2ADDR:
1854 case Instruction::REM_FLOAT_2ADDR: {
1855 // res = op + 2 operands
1856 uint16_t operand1 = GetOperandValue(mir->ssa_rep->uses[0]);
1857 uint16_t operand2 = GetOperandValue(mir->ssa_rep->uses[1]);
Vladimir Marko95a05972014-05-30 10:01:32 +01001858 res = gvn_->LookupValue(opcode, operand1, operand2, kNoValue);
buzbee2502e002012-12-31 16:05:53 -08001859 SetOperandValue(mir->ssa_rep->defs[0], res);
1860 }
1861 break;
1862
1863 case Instruction::RSUB_INT:
1864 case Instruction::ADD_INT_LIT16:
1865 case Instruction::MUL_INT_LIT16:
1866 case Instruction::DIV_INT_LIT16:
1867 case Instruction::REM_INT_LIT16:
1868 case Instruction::AND_INT_LIT16:
1869 case Instruction::OR_INT_LIT16:
1870 case Instruction::XOR_INT_LIT16:
1871 case Instruction::ADD_INT_LIT8:
1872 case Instruction::RSUB_INT_LIT8:
1873 case Instruction::MUL_INT_LIT8:
1874 case Instruction::DIV_INT_LIT8:
1875 case Instruction::REM_INT_LIT8:
1876 case Instruction::AND_INT_LIT8:
1877 case Instruction::OR_INT_LIT8:
1878 case Instruction::XOR_INT_LIT8:
1879 case Instruction::SHL_INT_LIT8:
1880 case Instruction::SHR_INT_LIT8:
1881 case Instruction::USHR_INT_LIT8: {
nikolay serdjukee40aa42014-03-25 12:21:29 +07001882 // Same as res = op + 2 operands, except use vC as operand 2
buzbee2502e002012-12-31 16:05:53 -08001883 uint16_t operand1 = GetOperandValue(mir->ssa_rep->uses[0]);
Vladimir Marko95a05972014-05-30 10:01:32 +01001884 uint16_t operand2 = gvn_->LookupValue(Instruction::CONST, mir->dalvikInsn.vC, 0, 0);
1885 res = gvn_->LookupValue(opcode, operand1, operand2, kNoValue);
buzbee2502e002012-12-31 16:05:53 -08001886 SetOperandValue(mir->ssa_rep->defs[0], res);
1887 }
1888 break;
1889
buzbee2502e002012-12-31 16:05:53 -08001890 case Instruction::AGET_OBJECT:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001891 case Instruction::AGET:
1892 case Instruction::AGET_WIDE:
buzbee2502e002012-12-31 16:05:53 -08001893 case Instruction::AGET_BOOLEAN:
1894 case Instruction::AGET_BYTE:
1895 case Instruction::AGET_CHAR:
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001896 case Instruction::AGET_SHORT:
1897 res = HandleAGet(mir, opcode);
buzbee2502e002012-12-31 16:05:53 -08001898 break;
1899
buzbee2502e002012-12-31 16:05:53 -08001900 case Instruction::APUT_OBJECT:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001901 HandlePutObject(mir);
Ian Rogersfc787ec2014-10-09 21:56:44 -07001902 FALLTHROUGH_INTENDED;
Vladimir Markof59f18b2014-02-17 15:53:57 +00001903 case Instruction::APUT:
1904 case Instruction::APUT_WIDE:
buzbee2502e002012-12-31 16:05:53 -08001905 case Instruction::APUT_BYTE:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001906 case Instruction::APUT_BOOLEAN:
1907 case Instruction::APUT_SHORT:
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001908 case Instruction::APUT_CHAR:
1909 HandleAPut(mir, opcode);
buzbee2502e002012-12-31 16:05:53 -08001910 break;
1911
1912 case Instruction::IGET_OBJECT:
buzbee2502e002012-12-31 16:05:53 -08001913 case Instruction::IGET:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001914 case Instruction::IGET_WIDE:
buzbee2502e002012-12-31 16:05:53 -08001915 case Instruction::IGET_BOOLEAN:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001916 case Instruction::IGET_BYTE:
1917 case Instruction::IGET_CHAR:
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001918 case Instruction::IGET_SHORT:
1919 res = HandleIGet(mir, opcode);
buzbee2502e002012-12-31 16:05:53 -08001920 break;
1921
buzbee2502e002012-12-31 16:05:53 -08001922 case Instruction::IPUT_OBJECT:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001923 HandlePutObject(mir);
Ian Rogersfc787ec2014-10-09 21:56:44 -07001924 FALLTHROUGH_INTENDED;
buzbee2502e002012-12-31 16:05:53 -08001925 case Instruction::IPUT:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001926 case Instruction::IPUT_WIDE:
buzbee2502e002012-12-31 16:05:53 -08001927 case Instruction::IPUT_BOOLEAN:
1928 case Instruction::IPUT_BYTE:
1929 case Instruction::IPUT_CHAR:
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001930 case Instruction::IPUT_SHORT:
1931 HandleIPut(mir, opcode);
buzbee2502e002012-12-31 16:05:53 -08001932 break;
1933
1934 case Instruction::SGET_OBJECT:
1935 case Instruction::SGET:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001936 case Instruction::SGET_WIDE:
buzbee2502e002012-12-31 16:05:53 -08001937 case Instruction::SGET_BOOLEAN:
1938 case Instruction::SGET_BYTE:
1939 case Instruction::SGET_CHAR:
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001940 case Instruction::SGET_SHORT:
1941 res = HandleSGet(mir, opcode);
buzbee2502e002012-12-31 16:05:53 -08001942 break;
1943
1944 case Instruction::SPUT_OBJECT:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001945 HandlePutObject(mir);
Ian Rogersfc787ec2014-10-09 21:56:44 -07001946 FALLTHROUGH_INTENDED;
buzbee2502e002012-12-31 16:05:53 -08001947 case Instruction::SPUT:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001948 case Instruction::SPUT_WIDE:
buzbee2502e002012-12-31 16:05:53 -08001949 case Instruction::SPUT_BOOLEAN:
1950 case Instruction::SPUT_BYTE:
1951 case Instruction::SPUT_CHAR:
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001952 case Instruction::SPUT_SHORT:
1953 HandleSPut(mir, opcode);
buzbee2502e002012-12-31 16:05:53 -08001954 break;
buzbee2502e002012-12-31 16:05:53 -08001955 }
1956 return res;
1957}
1958
1959} // namespace art