blob: c502b0cb37294fa71fe7a8502dc287fa81f14398 [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) {
346 std::fill_n(unresolved_sfield_version_, kFieldTypeCount, 0u);
347 std::fill_n(unresolved_ifield_version_, kFieldTypeCount, 0u);
348}
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_);
395 std::fill_n(unresolved_sfield_version_, kFieldTypeCount, global_memory_version_);
396 std::fill_n(unresolved_ifield_version_, kFieldTypeCount, global_memory_version_);
397 PruneNonAliasingRefsForCatch();
398 return;
399 }
400
401 DCHECK(merge_type == kNormalMerge);
402 global_memory_version_ = other.global_memory_version_;
403 std::copy_n(other.unresolved_ifield_version_, kFieldTypeCount, unresolved_ifield_version_);
404 std::copy_n(other.unresolved_sfield_version_, kFieldTypeCount, unresolved_sfield_version_);
405 sfield_value_map_ = other.sfield_value_map_;
Vladimir Markob19955d2014-07-29 12:04:10 +0100406 CopyAliasingValuesMap(&aliasing_ifield_value_map_, other.aliasing_ifield_value_map_);
407 CopyAliasingValuesMap(&aliasing_array_value_map_, other.aliasing_array_value_map_);
Vladimir Marko95a05972014-05-30 10:01:32 +0100408 escaped_refs_ = other.escaped_refs_;
409 escaped_ifield_clobber_set_ = other.escaped_ifield_clobber_set_;
410 escaped_array_clobber_set_ = other.escaped_array_clobber_set_;
411}
412
413bool LocalValueNumbering::SameMemoryVersion(const LocalValueNumbering& other) const {
414 return
415 global_memory_version_ == other.global_memory_version_ &&
416 std::equal(unresolved_ifield_version_, unresolved_ifield_version_ + kFieldTypeCount,
417 other.unresolved_ifield_version_) &&
418 std::equal(unresolved_sfield_version_, unresolved_sfield_version_ + kFieldTypeCount,
419 other.unresolved_sfield_version_);
420}
421
422uint16_t LocalValueNumbering::NewMemoryVersion(uint16_t* new_version) {
423 if (*new_version == kNoValue) {
424 *new_version = gvn_->LookupValue(kMergeBlockMemoryVersionBumpOp, 0u, 0u, id_);
425 }
426 return *new_version;
427}
428
429void LocalValueNumbering::MergeMemoryVersions(bool clobbered_catch) {
430 DCHECK_GE(gvn_->merge_lvns_.size(), 2u);
431 const LocalValueNumbering* cmp = gvn_->merge_lvns_[0];
432 // Check if the global version has changed.
433 bool new_global_version = clobbered_catch;
434 if (!new_global_version) {
435 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
436 if (lvn->global_memory_version_ != cmp->global_memory_version_) {
437 // Use a new version for everything.
438 new_global_version = true;
439 break;
440 }
441 }
442 }
443 if (new_global_version) {
444 global_memory_version_ = NewMemoryVersion(&merge_new_memory_version_);
445 std::fill_n(unresolved_sfield_version_, kFieldTypeCount, merge_new_memory_version_);
446 std::fill_n(unresolved_ifield_version_, kFieldTypeCount, merge_new_memory_version_);
447 } else {
448 // Initialize with a copy of memory versions from the comparison LVN.
449 global_memory_version_ = cmp->global_memory_version_;
450 std::copy_n(cmp->unresolved_ifield_version_, kFieldTypeCount, unresolved_ifield_version_);
451 std::copy_n(cmp->unresolved_sfield_version_, kFieldTypeCount, unresolved_sfield_version_);
452 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
453 if (lvn == cmp) {
454 continue;
455 }
456 for (size_t i = 0; i != kFieldTypeCount; ++i) {
457 if (lvn->unresolved_ifield_version_[i] != cmp->unresolved_ifield_version_[i]) {
458 unresolved_ifield_version_[i] = NewMemoryVersion(&merge_new_memory_version_);
459 }
460 if (lvn->unresolved_sfield_version_[i] != cmp->unresolved_sfield_version_[i]) {
461 unresolved_sfield_version_[i] = NewMemoryVersion(&merge_new_memory_version_);
462 }
463 }
464 }
465 }
466}
467
468void LocalValueNumbering::PruneNonAliasingRefsForCatch() {
469 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
470 const BasicBlock* bb = gvn_->GetBasicBlock(lvn->Id());
Vladimir Marko11ca6122014-07-17 20:50:07 +0100471 if (UNLIKELY(bb->taken == id_) || UNLIKELY(bb->fall_through == id_)) {
472 // Non-exceptional path to a catch handler means that the catch block was actually
473 // empty and all exceptional paths lead to the shared path after that empty block.
474 continue;
475 }
Vladimir Marko95a05972014-05-30 10:01:32 +0100476 DCHECK_EQ(bb->taken, kNullBlock);
477 DCHECK_NE(bb->fall_through, kNullBlock);
478 const BasicBlock* fall_through_bb = gvn_->GetBasicBlock(bb->fall_through);
479 const MIR* mir = fall_through_bb->first_mir_insn;
480 DCHECK(mir != nullptr);
481 // Only INVOKEs can leak and clobber non-aliasing references if they throw.
Jean Christophe Beylerfb0ea2d2014-07-29 13:20:42 -0700482 if ((mir->dalvikInsn.FlagsOf() & Instruction::kInvoke) != 0) {
Vladimir Markoa4426cf2014-10-22 17:15:53 +0100483 HandleInvokeArgs(mir, lvn);
Vladimir Marko95a05972014-05-30 10:01:32 +0100484 }
485 }
486}
487
488
489template <typename Set, Set LocalValueNumbering::* set_ptr>
490void LocalValueNumbering::IntersectSets() {
491 DCHECK_GE(gvn_->merge_lvns_.size(), 2u);
492
493 // Find the LVN with the least entries in the set.
494 const LocalValueNumbering* least_entries_lvn = gvn_->merge_lvns_[0];
495 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
496 if ((lvn->*set_ptr).size() < (least_entries_lvn->*set_ptr).size()) {
497 least_entries_lvn = lvn;
498 }
499 }
500
501 // For each key check if it's in all the LVNs.
502 for (const auto& key : least_entries_lvn->*set_ptr) {
503 bool checked = true;
504 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
505 if (lvn != least_entries_lvn && (lvn->*set_ptr).count(key) == 0u) {
506 checked = false;
507 break;
508 }
509 }
510 if (checked) {
511 (this->*set_ptr).emplace_hint((this->*set_ptr).end(), key);
512 }
513 }
514}
515
Vladimir Markob19955d2014-07-29 12:04:10 +0100516void LocalValueNumbering::CopyLiveSregValues(SregValueMap* dest, const SregValueMap& src) {
517 auto dest_end = dest->end();
518 ArenaBitVector* live_in_v = gvn_->GetMirGraph()->GetBasicBlock(id_)->data_flow_info->live_in_v;
519 DCHECK(live_in_v != nullptr);
520 for (const auto& entry : src) {
521 bool live = live_in_v->IsBitSet(gvn_->GetMirGraph()->SRegToVReg(entry.first));
522 if (live) {
523 dest->PutBefore(dest_end, entry.first, entry.second);
524 }
525 }
526}
527
528template <LocalValueNumbering::SregValueMap LocalValueNumbering::* map_ptr>
529void LocalValueNumbering::IntersectSregValueMaps() {
Vladimir Marko95a05972014-05-30 10:01:32 +0100530 DCHECK_GE(gvn_->merge_lvns_.size(), 2u);
531
532 // Find the LVN with the least entries in the set.
533 const LocalValueNumbering* least_entries_lvn = gvn_->merge_lvns_[0];
534 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
535 if ((lvn->*map_ptr).size() < (least_entries_lvn->*map_ptr).size()) {
536 least_entries_lvn = lvn;
537 }
538 }
539
540 // For each key check if it's in all the LVNs.
Vladimir Markob19955d2014-07-29 12:04:10 +0100541 ArenaBitVector* live_in_v = gvn_->GetMirGraph()->GetBasicBlock(id_)->data_flow_info->live_in_v;
542 DCHECK(live_in_v != nullptr);
Vladimir Marko95a05972014-05-30 10:01:32 +0100543 for (const auto& entry : least_entries_lvn->*map_ptr) {
Vladimir Markob19955d2014-07-29 12:04:10 +0100544 bool live_and_same = live_in_v->IsBitSet(gvn_->GetMirGraph()->SRegToVReg(entry.first));
545 if (live_and_same) {
546 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
547 if (lvn != least_entries_lvn) {
548 auto it = (lvn->*map_ptr).find(entry.first);
549 if (it == (lvn->*map_ptr).end() || !(it->second == entry.second)) {
550 live_and_same = false;
551 break;
552 }
Vladimir Marko95a05972014-05-30 10:01:32 +0100553 }
554 }
555 }
Vladimir Markob19955d2014-07-29 12:04:10 +0100556 if (live_and_same) {
Vladimir Marko95a05972014-05-30 10:01:32 +0100557 (this->*map_ptr).PutBefore((this->*map_ptr).end(), entry.first, entry.second);
558 }
559 }
560}
561
562// Intersect maps as sets. The value type must be equality-comparable.
563template <typename Map>
564void LocalValueNumbering::InPlaceIntersectMaps(Map* work_map, const Map& other_map) {
565 auto work_it = work_map->begin(), work_end = work_map->end();
566 auto cmp = work_map->value_comp();
567 for (const auto& entry : other_map) {
568 while (work_it != work_end &&
569 (cmp(*work_it, entry) ||
570 (!cmp(entry, *work_it) && !(work_it->second == entry.second)))) {
571 work_it = work_map->erase(work_it);
572 }
Vladimir Marko55fff042014-07-10 12:42:52 +0100573 if (work_it == work_end) {
574 return;
575 }
576 ++work_it;
Vladimir Marko95a05972014-05-30 10:01:32 +0100577 }
578}
579
580template <typename Set, Set LocalValueNumbering::*set_ptr, void (LocalValueNumbering::*MergeFn)(
581 const typename Set::value_type& entry, typename Set::iterator hint)>
582void LocalValueNumbering::MergeSets() {
583 auto cmp = (this->*set_ptr).value_comp();
584 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
585 auto my_it = (this->*set_ptr).begin(), my_end = (this->*set_ptr).end();
586 for (const auto& entry : lvn->*set_ptr) {
587 while (my_it != my_end && cmp(*my_it, entry)) {
588 ++my_it;
589 }
590 if (my_it != my_end && !cmp(entry, *my_it)) {
591 // Already handled.
592 ++my_it;
593 } else {
594 // Merge values for this field_id.
595 (this->*MergeFn)(entry, my_it); // my_it remains valid across inserts to std::set/SafeMap.
596 }
597 }
598 }
599}
600
601void LocalValueNumbering::IntersectAliasingValueLocations(AliasingValues* work_values,
602 const AliasingValues* values) {
603 auto cmp = work_values->load_value_map.key_comp();
604 auto work_it = work_values->load_value_map.begin(), work_end = work_values->load_value_map.end();
605 auto store_it = values->store_loc_set.begin(), store_end = values->store_loc_set.end();
606 auto load_it = values->load_value_map.begin(), load_end = values->load_value_map.end();
607 while (store_it != store_end || load_it != load_end) {
608 uint16_t loc;
609 if (store_it != store_end && (load_it == load_end || *store_it < load_it->first)) {
610 loc = *store_it;
611 ++store_it;
612 } else {
613 loc = load_it->first;
614 ++load_it;
615 DCHECK(store_it == store_end || cmp(loc, *store_it));
616 }
617 while (work_it != work_end && cmp(work_it->first, loc)) {
618 work_it = work_values->load_value_map.erase(work_it);
619 }
620 if (work_it != work_end && !cmp(loc, work_it->first)) {
621 // The location matches, keep it.
622 ++work_it;
623 }
624 }
625 while (work_it != work_end) {
626 work_it = work_values->load_value_map.erase(work_it);
627 }
628}
629
630void LocalValueNumbering::MergeEscapedRefs(const ValueNameSet::value_type& entry,
631 ValueNameSet::iterator hint) {
632 // See if the ref is either escaped or non-aliasing in each predecessor.
633 bool is_escaped = true;
634 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
635 if (lvn->non_aliasing_refs_.count(entry) == 0u &&
636 lvn->escaped_refs_.count(entry) == 0u) {
637 is_escaped = false;
638 break;
639 }
640 }
641 if (is_escaped) {
642 escaped_refs_.emplace_hint(hint, entry);
643 }
644}
645
646void LocalValueNumbering::MergeEscapedIFieldTypeClobberSets(
647 const EscapedIFieldClobberSet::value_type& entry, EscapedIFieldClobberSet::iterator hint) {
648 // Insert only type-clobber entries (field_id == kNoValue) of escaped refs.
649 if (entry.field_id == kNoValue && escaped_refs_.count(entry.base) != 0u) {
650 escaped_ifield_clobber_set_.emplace_hint(hint, entry);
651 }
652}
653
654void LocalValueNumbering::MergeEscapedIFieldClobberSets(
655 const EscapedIFieldClobberSet::value_type& entry, EscapedIFieldClobberSet::iterator hint) {
656 // Insert only those entries of escaped refs that are not overridden by a type clobber.
657 if (!(hint == escaped_ifield_clobber_set_.end() &&
658 hint->base == entry.base && hint->type == entry.type) &&
659 escaped_refs_.count(entry.base) != 0u) {
660 escaped_ifield_clobber_set_.emplace_hint(hint, entry);
661 }
662}
663
664void LocalValueNumbering::MergeEscapedArrayClobberSets(
665 const EscapedArrayClobberSet::value_type& entry, EscapedArrayClobberSet::iterator hint) {
666 if (escaped_refs_.count(entry.base) != 0u) {
667 escaped_array_clobber_set_.emplace_hint(hint, entry);
668 }
669}
670
Vladimir Marko2d2365c2014-08-19 18:08:39 +0100671void LocalValueNumbering::MergeNullChecked() {
672 DCHECK_GE(gvn_->merge_lvns_.size(), 2u);
673
674 // Find the LVN with the least entries in the set.
675 const LocalValueNumbering* least_entries_lvn = gvn_->merge_lvns_[0];
676 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
677 if (lvn->null_checked_.size() < least_entries_lvn->null_checked_.size()) {
678 least_entries_lvn = lvn;
679 }
680 }
681
682 // For each null-checked value name check if it's null-checked in all the LVNs.
683 for (const auto& value_name : least_entries_lvn->null_checked_) {
684 // Merge null_checked_ for this ref.
685 merge_names_.clear();
686 merge_names_.resize(gvn_->merge_lvns_.size(), value_name);
687 if (gvn_->NullCheckedInAllPredecessors(merge_names_)) {
688 null_checked_.insert(null_checked_.end(), value_name);
689 }
690 }
691
692 // Now check if the least_entries_lvn has a null-check as the last insn.
693 const BasicBlock* least_entries_bb = gvn_->GetBasicBlock(least_entries_lvn->Id());
694 if (gvn_->HasNullCheckLastInsn(least_entries_bb, id_)) {
695 int s_reg = least_entries_bb->last_mir_insn->ssa_rep->uses[0];
Vladimir Markoa4426cf2014-10-22 17:15:53 +0100696 uint32_t value_name = least_entries_lvn->GetOperandValue(s_reg);
Vladimir Marko2d2365c2014-08-19 18:08:39 +0100697 merge_names_.clear();
698 merge_names_.resize(gvn_->merge_lvns_.size(), value_name);
699 if (gvn_->NullCheckedInAllPredecessors(merge_names_)) {
700 null_checked_.insert(value_name);
701 }
Vladimir Marko95a05972014-05-30 10:01:32 +0100702 }
703}
704
Razvan A Lupusorue0951142014-11-14 14:36:55 -0800705void LocalValueNumbering::MergeDivZeroChecked() {
706 DCHECK_GE(gvn_->merge_lvns_.size(), 2u);
707
708 // Find the LVN with the least entries in the set.
709 const LocalValueNumbering* least_entries_lvn = gvn_->merge_lvns_[0];
710 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
711 if (lvn->div_zero_checked_.size() < least_entries_lvn->div_zero_checked_.size()) {
712 least_entries_lvn = lvn;
713 }
714 }
715
716 // For each div-zero value name check if it's div-zero checked in all the LVNs.
717 for (const auto& value_name : least_entries_lvn->div_zero_checked_) {
718 // Merge null_checked_ for this ref.
719 merge_names_.clear();
720 merge_names_.resize(gvn_->merge_lvns_.size(), value_name);
721 if (gvn_->DivZeroCheckedInAllPredecessors(merge_names_)) {
722 div_zero_checked_.insert(div_zero_checked_.end(), value_name);
723 }
724 }
725}
726
Vladimir Marko95a05972014-05-30 10:01:32 +0100727void LocalValueNumbering::MergeSFieldValues(const SFieldToValueMap::value_type& entry,
728 SFieldToValueMap::iterator hint) {
729 uint16_t field_id = entry.first;
730 merge_names_.clear();
731 uint16_t value_name = kNoValue;
732 bool same_values = true;
733 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
734 // Get the value name as in HandleSGet() but don't modify *lvn.
735 auto it = lvn->sfield_value_map_.find(field_id);
736 if (it != lvn->sfield_value_map_.end()) {
737 value_name = it->second;
738 } else {
Vladimir Markoaf6925b2014-10-31 16:37:32 +0000739 uint16_t type = gvn_->GetSFieldType(field_id);
Vladimir Marko95a05972014-05-30 10:01:32 +0100740 value_name = gvn_->LookupValue(kResolvedSFieldOp, field_id,
741 lvn->unresolved_sfield_version_[type],
742 lvn->global_memory_version_);
743 }
744
745 same_values = same_values && (merge_names_.empty() || value_name == merge_names_.back());
746 merge_names_.push_back(value_name);
747 }
748 if (same_values) {
749 // value_name already contains the result.
750 } else {
751 auto lb = merge_map_.lower_bound(merge_names_);
752 if (lb != merge_map_.end() && !merge_map_.key_comp()(merge_names_, lb->first)) {
753 value_name = lb->second;
754 } else {
755 value_name = gvn_->LookupValue(kMergeBlockSFieldVersionBumpOp, field_id, id_, kNoValue);
756 merge_map_.PutBefore(lb, merge_names_, value_name);
757 if (gvn_->NullCheckedInAllPredecessors(merge_names_)) {
758 null_checked_.insert(value_name);
759 }
760 }
761 }
762 sfield_value_map_.PutBefore(hint, field_id, value_name);
763}
764
765void LocalValueNumbering::MergeNonAliasingIFieldValues(const IFieldLocToValueMap::value_type& entry,
766 IFieldLocToValueMap::iterator hint) {
767 uint16_t field_loc = entry.first;
768 merge_names_.clear();
769 uint16_t value_name = kNoValue;
770 bool same_values = true;
771 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
772 // Get the value name as in HandleIGet() but don't modify *lvn.
773 auto it = lvn->non_aliasing_ifield_value_map_.find(field_loc);
774 if (it != lvn->non_aliasing_ifield_value_map_.end()) {
775 value_name = it->second;
776 } else {
777 value_name = gvn_->LookupValue(kNonAliasingIFieldInitialOp, field_loc, kNoValue, kNoValue);
778 }
779
780 same_values = same_values && (merge_names_.empty() || value_name == merge_names_.back());
781 merge_names_.push_back(value_name);
782 }
783 if (same_values) {
784 // value_name already contains the result.
785 } else {
786 auto lb = merge_map_.lower_bound(merge_names_);
787 if (lb != merge_map_.end() && !merge_map_.key_comp()(merge_names_, lb->first)) {
788 value_name = lb->second;
789 } else {
790 value_name = gvn_->LookupValue(kMergeBlockNonAliasingIFieldVersionBumpOp, field_loc,
791 id_, kNoValue);
792 merge_map_.PutBefore(lb, merge_names_, value_name);
793 if (gvn_->NullCheckedInAllPredecessors(merge_names_)) {
794 null_checked_.insert(value_name);
795 }
796 }
797 }
798 non_aliasing_ifield_value_map_.PutBefore(hint, field_loc, value_name);
799}
800
801template <typename Map, Map LocalValueNumbering::*map_ptr, typename Versions>
802void LocalValueNumbering::MergeAliasingValues(const typename Map::value_type& entry,
803 typename Map::iterator hint) {
804 const typename Map::key_type& key = entry.first;
805
Vladimir Markob19955d2014-07-29 12:04:10 +0100806 auto it = (this->*map_ptr).PutBefore(hint, key, AliasingValues(this));
Vladimir Marko95a05972014-05-30 10:01:32 +0100807 AliasingValues* my_values = &it->second;
808
809 const AliasingValues* cmp_values = nullptr;
810 bool same_version = !Versions::HasNewBaseVersion(gvn_, this, key);
811 uint16_t load_memory_version_for_same_version = kNoValue;
812 if (same_version) {
813 // Find the first non-null values.
814 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800815 auto value = (lvn->*map_ptr).find(key);
816 if (value != (lvn->*map_ptr).end()) {
817 cmp_values = &value->second;
Vladimir Marko95a05972014-05-30 10:01:32 +0100818 break;
819 }
820 }
821 DCHECK(cmp_values != nullptr); // There must be at least one non-null values.
822
823 // Check if we have identical memory versions, i.e. the global memory version, unresolved
824 // field version and the values' memory_version_before_stores, last_stored_value
825 // and store_loc_set are identical.
826 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800827 auto value = (lvn->*map_ptr).find(key);
828 if (value == (lvn->*map_ptr).end()) {
Vladimir Marko95a05972014-05-30 10:01:32 +0100829 if (cmp_values->memory_version_before_stores != kNoValue) {
830 same_version = false;
831 break;
832 }
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800833 } else if (cmp_values->last_stored_value != value->second.last_stored_value ||
834 cmp_values->memory_version_before_stores != value->second.memory_version_before_stores ||
835 cmp_values->store_loc_set != value->second.store_loc_set) {
Vladimir Marko95a05972014-05-30 10:01:32 +0100836 same_version = false;
837 break;
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800838 } else if (value->second.last_load_memory_version != kNoValue) {
Vladimir Marko95a05972014-05-30 10:01:32 +0100839 DCHECK(load_memory_version_for_same_version == kNoValue ||
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800840 load_memory_version_for_same_version == value->second.last_load_memory_version);
841 load_memory_version_for_same_version = value->second.last_load_memory_version;
Vladimir Marko95a05972014-05-30 10:01:32 +0100842 }
843 }
844 }
845
846 if (same_version) {
847 // Copy the identical values.
848 my_values->memory_version_before_stores = cmp_values->memory_version_before_stores;
849 my_values->last_stored_value = cmp_values->last_stored_value;
850 my_values->store_loc_set = cmp_values->store_loc_set;
851 my_values->last_load_memory_version = load_memory_version_for_same_version;
852 // Merge load values seen in all incoming arcs (i.e. an intersection).
853 if (!cmp_values->load_value_map.empty()) {
854 my_values->load_value_map = cmp_values->load_value_map;
855 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800856 auto value = (lvn->*map_ptr).find(key);
857 if (value == (lvn->*map_ptr).end() || value->second.load_value_map.empty()) {
Vladimir Marko95a05972014-05-30 10:01:32 +0100858 my_values->load_value_map.clear();
859 break;
860 }
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800861 InPlaceIntersectMaps(&my_values->load_value_map, value->second.load_value_map);
Vladimir Marko95a05972014-05-30 10:01:32 +0100862 if (my_values->load_value_map.empty()) {
863 break;
864 }
865 }
866 }
867 } else {
868 // Bump version number for the merge.
869 my_values->memory_version_before_stores = my_values->last_load_memory_version =
870 Versions::LookupMergeBlockValue(gvn_, id_, key);
871
872 // Calculate the locations that have been either read from or written to in each incoming LVN.
873 bool first_lvn = true;
874 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800875 auto value = (lvn->*map_ptr).find(key);
876 if (value == (lvn->*map_ptr).end()) {
Vladimir Marko95a05972014-05-30 10:01:32 +0100877 my_values->load_value_map.clear();
878 break;
879 }
880 if (first_lvn) {
881 first_lvn = false;
882 // Copy the first LVN's locations. Values will be overwritten later.
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800883 my_values->load_value_map = value->second.load_value_map;
884 for (uint16_t location : value->second.store_loc_set) {
Vladimir Marko95a05972014-05-30 10:01:32 +0100885 my_values->load_value_map.Put(location, 0u);
886 }
887 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800888 IntersectAliasingValueLocations(my_values, &value->second);
Vladimir Marko95a05972014-05-30 10:01:32 +0100889 }
890 }
891 // Calculate merged values for the intersection.
892 for (auto& load_value_entry : my_values->load_value_map) {
893 uint16_t location = load_value_entry.first;
894 bool same_values = true;
895 uint16_t value_name = kNoValue;
896 merge_names_.clear();
897 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
898 value_name = Versions::LookupMergeValue(gvn_, lvn, key, location);
899 same_values = same_values && (merge_names_.empty() || value_name == merge_names_.back());
900 merge_names_.push_back(value_name);
901 }
902 if (same_values) {
903 // value_name already contains the result.
904 } else {
905 auto lb = merge_map_.lower_bound(merge_names_);
906 if (lb != merge_map_.end() && !merge_map_.key_comp()(merge_names_, lb->first)) {
907 value_name = lb->second;
908 } else {
909 // NOTE: In addition to the key and id_ which don't change on an LVN recalculation
910 // during GVN, we also add location which can actually change on recalculation, so the
911 // value_name below may change. This could lead to an infinite loop if the location
912 // value name always changed when the refereced value name changes. However, given that
913 // we assign unique value names for other merges, such as Phis, such a dependency is
914 // not possible in a well-formed SSA graph.
915 value_name = Versions::LookupMergeLocationValue(gvn_, id_, key, location);
916 merge_map_.PutBefore(lb, merge_names_, value_name);
917 if (gvn_->NullCheckedInAllPredecessors(merge_names_)) {
918 null_checked_.insert(value_name);
919 }
920 }
921 }
922 load_value_entry.second = value_name;
923 }
924 }
925}
926
927void LocalValueNumbering::Merge(MergeType merge_type) {
928 DCHECK_GE(gvn_->merge_lvns_.size(), 2u);
929
Vladimir Markob19955d2014-07-29 12:04:10 +0100930 IntersectSregValueMaps<&LocalValueNumbering::sreg_value_map_>();
931 IntersectSregValueMaps<&LocalValueNumbering::sreg_wide_value_map_>();
Vladimir Marko95a05972014-05-30 10:01:32 +0100932 if (merge_type == kReturnMerge) {
933 // RETURN or PHI+RETURN. We need only sreg value maps.
934 return;
935 }
936
937 MergeMemoryVersions(merge_type == kCatchMerge);
938
939 // Merge non-aliasing maps/sets.
Vladimir Marko95a05972014-05-30 10:01:32 +0100940 IntersectSets<ValueNameSet, &LocalValueNumbering::non_aliasing_refs_>();
Vladimir Marko55fff042014-07-10 12:42:52 +0100941 if (!non_aliasing_refs_.empty() && merge_type == kCatchMerge) {
942 PruneNonAliasingRefsForCatch();
943 }
944 if (!non_aliasing_refs_.empty()) {
945 MergeSets<IFieldLocToValueMap, &LocalValueNumbering::non_aliasing_ifield_value_map_,
946 &LocalValueNumbering::MergeNonAliasingIFieldValues>();
947 MergeSets<NonAliasingArrayValuesMap, &LocalValueNumbering::non_aliasing_array_value_map_,
948 &LocalValueNumbering::MergeAliasingValues<
949 NonAliasingArrayValuesMap, &LocalValueNumbering::non_aliasing_array_value_map_,
950 NonAliasingArrayVersions>>();
951 }
Vladimir Marko95a05972014-05-30 10:01:32 +0100952
953 // We won't do anything complicated for range checks, just calculate the intersection.
954 IntersectSets<RangeCheckSet, &LocalValueNumbering::range_checked_>();
955
956 // Merge null_checked_. We may later insert more, such as merged object field values.
Vladimir Marko2d2365c2014-08-19 18:08:39 +0100957 MergeNullChecked();
Vladimir Marko95a05972014-05-30 10:01:32 +0100958
Razvan A Lupusorue0951142014-11-14 14:36:55 -0800959 // Now merge the div_zero_checked_.
960 MergeDivZeroChecked();
961
Vladimir Marko95a05972014-05-30 10:01:32 +0100962 if (merge_type == kCatchMerge) {
963 // Memory is clobbered. New memory version already created, don't merge aliasing locations.
Vladimir Marko95a05972014-05-30 10:01:32 +0100964 return;
965 }
966
967 DCHECK(merge_type == kNormalMerge);
968
969 // Merge escaped refs and clobber sets.
970 MergeSets<ValueNameSet, &LocalValueNumbering::escaped_refs_,
971 &LocalValueNumbering::MergeEscapedRefs>();
972 if (!escaped_refs_.empty()) {
973 MergeSets<EscapedIFieldClobberSet, &LocalValueNumbering::escaped_ifield_clobber_set_,
974 &LocalValueNumbering::MergeEscapedIFieldTypeClobberSets>();
975 MergeSets<EscapedIFieldClobberSet, &LocalValueNumbering::escaped_ifield_clobber_set_,
976 &LocalValueNumbering::MergeEscapedIFieldClobberSets>();
977 MergeSets<EscapedArrayClobberSet, &LocalValueNumbering::escaped_array_clobber_set_,
978 &LocalValueNumbering::MergeEscapedArrayClobberSets>();
979 }
980
981 MergeSets<SFieldToValueMap, &LocalValueNumbering::sfield_value_map_,
982 &LocalValueNumbering::MergeSFieldValues>();
983 MergeSets<AliasingIFieldValuesMap, &LocalValueNumbering::aliasing_ifield_value_map_,
984 &LocalValueNumbering::MergeAliasingValues<
985 AliasingIFieldValuesMap, &LocalValueNumbering::aliasing_ifield_value_map_,
986 AliasingIFieldVersions>>();
987 MergeSets<AliasingArrayValuesMap, &LocalValueNumbering::aliasing_array_value_map_,
988 &LocalValueNumbering::MergeAliasingValues<
989 AliasingArrayValuesMap, &LocalValueNumbering::aliasing_array_value_map_,
990 AliasingArrayVersions>>();
Vladimir Markof59f18b2014-02-17 15:53:57 +0000991}
992
Vladimir Markoa4426cf2014-10-22 17:15:53 +0100993void LocalValueNumbering::PrepareEntryBlock() {
994 uint32_t vreg = gvn_->GetMirGraph()->GetFirstInVR();
995 CompilationUnit* cu = gvn_->GetCompilationUnit();
996 const char* shorty = cu->shorty;
997 ++shorty; // Skip return value.
998 if ((cu->access_flags & kAccStatic) == 0) {
999 // If non-static method, mark "this" as non-null
1000 uint16_t value_name = GetOperandValue(vreg);
1001 ++vreg;
1002 null_checked_.insert(value_name);
1003 }
1004 for ( ; *shorty != 0; ++shorty, ++vreg) {
1005 if (*shorty == 'J' || *shorty == 'D') {
1006 uint16_t value_name = GetOperandValueWide(vreg);
1007 SetOperandValueWide(vreg, value_name);
1008 ++vreg;
1009 }
1010 }
1011}
1012
Vladimir Markof59f18b2014-02-17 15:53:57 +00001013uint16_t LocalValueNumbering::MarkNonAliasingNonNull(MIR* mir) {
1014 uint16_t res = GetOperandValue(mir->ssa_rep->defs[0]);
Vladimir Markof59f18b2014-02-17 15:53:57 +00001015 DCHECK(null_checked_.find(res) == null_checked_.end());
1016 null_checked_.insert(res);
1017 non_aliasing_refs_.insert(res);
1018 return res;
1019}
1020
Vladimir Marko95a05972014-05-30 10:01:32 +01001021bool LocalValueNumbering::IsNonAliasing(uint16_t reg) const {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001022 return non_aliasing_refs_.find(reg) != non_aliasing_refs_.end();
Vladimir Markof59f18b2014-02-17 15:53:57 +00001023}
1024
Vladimir Marko95a05972014-05-30 10:01:32 +01001025bool LocalValueNumbering::IsNonAliasingIField(uint16_t reg, uint16_t field_id,
1026 uint16_t type) const {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001027 if (IsNonAliasing(reg)) {
1028 return true;
1029 }
Vladimir Marko95a05972014-05-30 10:01:32 +01001030 if (escaped_refs_.find(reg) == escaped_refs_.end()) {
1031 return false;
1032 }
1033 // Check for IPUTs to unresolved fields.
1034 EscapedIFieldClobberKey key1 = { reg, type, kNoValue };
1035 if (escaped_ifield_clobber_set_.find(key1) != escaped_ifield_clobber_set_.end()) {
1036 return false;
1037 }
1038 // Check for aliased IPUTs to the same field.
1039 EscapedIFieldClobberKey key2 = { reg, type, field_id };
1040 return escaped_ifield_clobber_set_.find(key2) == escaped_ifield_clobber_set_.end();
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001041}
1042
Vladimir Marko95a05972014-05-30 10:01:32 +01001043bool LocalValueNumbering::IsNonAliasingArray(uint16_t reg, uint16_t type) const {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001044 if (IsNonAliasing(reg)) {
1045 return true;
1046 }
Vladimir Marko95a05972014-05-30 10:01:32 +01001047 if (escaped_refs_.count(reg) == 0u) {
1048 return false;
1049 }
1050 // Check for aliased APUTs.
1051 EscapedArrayClobberKey key = { reg, type };
1052 return escaped_array_clobber_set_.find(key) == escaped_array_clobber_set_.end();
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001053}
1054
Vladimir Markof59f18b2014-02-17 15:53:57 +00001055void LocalValueNumbering::HandleNullCheck(MIR* mir, uint16_t reg) {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001056 auto lb = null_checked_.lower_bound(reg);
1057 if (lb != null_checked_.end() && *lb == reg) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001058 if (LIKELY(gvn_->CanModify())) {
1059 if (gvn_->GetCompilationUnit()->verbose) {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001060 LOG(INFO) << "Removing null check for 0x" << std::hex << mir->offset;
1061 }
1062 mir->optimization_flags |= MIR_IGNORE_NULL_CHECK;
Vladimir Markof59f18b2014-02-17 15:53:57 +00001063 }
Vladimir Markof59f18b2014-02-17 15:53:57 +00001064 } else {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001065 null_checked_.insert(lb, reg);
Vladimir Markof59f18b2014-02-17 15:53:57 +00001066 }
1067}
1068
1069void LocalValueNumbering::HandleRangeCheck(MIR* mir, uint16_t array, uint16_t index) {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001070 RangeCheckKey key = { array, index };
1071 auto lb = range_checked_.lower_bound(key);
1072 if (lb != range_checked_.end() && !RangeCheckKeyComparator()(key, *lb)) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001073 if (LIKELY(gvn_->CanModify())) {
1074 if (gvn_->GetCompilationUnit()->verbose) {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001075 LOG(INFO) << "Removing range check for 0x" << std::hex << mir->offset;
1076 }
1077 mir->optimization_flags |= MIR_IGNORE_RANGE_CHECK;
Vladimir Markof59f18b2014-02-17 15:53:57 +00001078 }
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001079 } else {
1080 // Mark range check completed.
1081 range_checked_.insert(lb, key);
Vladimir Markof59f18b2014-02-17 15:53:57 +00001082 }
Vladimir Markof59f18b2014-02-17 15:53:57 +00001083}
1084
Razvan A Lupusorue0951142014-11-14 14:36:55 -08001085void LocalValueNumbering::HandleDivZeroCheck(MIR* mir, uint16_t reg) {
1086 auto lb = div_zero_checked_.lower_bound(reg);
1087 if (lb != div_zero_checked_.end() && *lb == reg) {
1088 if (LIKELY(gvn_->CanModify())) {
1089 if (gvn_->GetCompilationUnit()->verbose) {
1090 LOG(INFO) << "Removing div zero check for 0x" << std::hex << mir->offset;
1091 }
1092 mir->optimization_flags |= MIR_IGNORE_DIV_ZERO_CHECK;
1093 }
1094 } else {
1095 div_zero_checked_.insert(lb, reg);
1096 }
1097}
1098
Vladimir Markof59f18b2014-02-17 15:53:57 +00001099void LocalValueNumbering::HandlePutObject(MIR* mir) {
1100 // If we're storing a non-aliasing reference, stop tracking it as non-aliasing now.
1101 uint16_t base = GetOperandValue(mir->ssa_rep->uses[0]);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001102 HandleEscapingRef(base);
1103}
1104
1105void LocalValueNumbering::HandleEscapingRef(uint16_t base) {
1106 auto it = non_aliasing_refs_.find(base);
1107 if (it != non_aliasing_refs_.end()) {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001108 non_aliasing_refs_.erase(it);
Vladimir Marko95a05972014-05-30 10:01:32 +01001109 escaped_refs_.insert(base);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001110 }
1111}
1112
Vladimir Markoa4426cf2014-10-22 17:15:53 +01001113void LocalValueNumbering::HandleInvokeArgs(const MIR* mir, const LocalValueNumbering* mir_lvn) {
1114 const int32_t* uses = mir->ssa_rep->uses;
1115 const int32_t* uses_end = uses + mir->ssa_rep->num_uses;
1116 while (uses != uses_end) {
1117 uint16_t sreg = *uses;
1118 ++uses;
1119 // Avoid LookupValue() so that we don't store new values in the global value map.
1120 auto local_it = mir_lvn->sreg_value_map_.find(sreg);
1121 if (local_it != mir_lvn->sreg_value_map_.end()) {
1122 non_aliasing_refs_.erase(local_it->second);
1123 } else {
1124 uint16_t value_name = gvn_->FindValue(kNoValue, sreg, kNoValue, kNoValue);
1125 if (value_name != kNoValue) {
1126 non_aliasing_refs_.erase(value_name);
1127 }
1128 }
1129 }
1130}
1131
Vladimir Marko95a05972014-05-30 10:01:32 +01001132uint16_t LocalValueNumbering::HandlePhi(MIR* mir) {
1133 if (gvn_->merge_lvns_.empty()) {
1134 // Running LVN without a full GVN?
1135 return kNoValue;
1136 }
Vladimir Marko95a05972014-05-30 10:01:32 +01001137 int32_t* uses = mir->ssa_rep->uses;
1138 // Try to find out if this is merging wide regs.
1139 if (mir->ssa_rep->defs[0] != 0 &&
1140 sreg_wide_value_map_.count(mir->ssa_rep->defs[0] - 1) != 0u) {
1141 // This is the high part of a wide reg. Ignore the Phi.
1142 return kNoValue;
1143 }
Vladimir Markoa4426cf2014-10-22 17:15:53 +01001144 BasicBlockId* incoming = mir->meta.phi_incoming;
1145 int16_t pos = 0;
1146 // Check if we're merging a wide value based on the first merged LVN.
1147 const LocalValueNumbering* first_lvn = gvn_->merge_lvns_[0];
1148 DCHECK_LT(pos, mir->ssa_rep->num_uses);
1149 while (incoming[pos] != first_lvn->Id()) {
1150 ++pos;
1151 DCHECK_LT(pos, mir->ssa_rep->num_uses);
Vladimir Marko95a05972014-05-30 10:01:32 +01001152 }
Vladimir Markoa4426cf2014-10-22 17:15:53 +01001153 int first_s_reg = uses[pos];
1154 bool wide = (first_lvn->sreg_wide_value_map_.count(first_s_reg) != 0u);
Vladimir Marko95a05972014-05-30 10:01:32 +01001155 // Iterate over *merge_lvns_ and skip incoming sregs for BBs without associated LVN.
1156 uint16_t value_name = kNoValue;
1157 merge_names_.clear();
Vladimir Marko95a05972014-05-30 10:01:32 +01001158 bool same_values = true;
1159 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
1160 DCHECK_LT(pos, mir->ssa_rep->num_uses);
1161 while (incoming[pos] != lvn->Id()) {
1162 ++pos;
1163 DCHECK_LT(pos, mir->ssa_rep->num_uses);
1164 }
1165 int s_reg = uses[pos];
1166 ++pos;
1167 value_name = wide ? lvn->GetOperandValueWide(s_reg) : lvn->GetOperandValue(s_reg);
1168
1169 same_values = same_values && (merge_names_.empty() || value_name == merge_names_.back());
1170 merge_names_.push_back(value_name);
1171 }
1172 if (same_values) {
1173 // value_name already contains the result.
1174 } else {
1175 auto lb = merge_map_.lower_bound(merge_names_);
1176 if (lb != merge_map_.end() && !merge_map_.key_comp()(merge_names_, lb->first)) {
1177 value_name = lb->second;
1178 } else {
1179 value_name = gvn_->LookupValue(kNoValue, mir->ssa_rep->defs[0], kNoValue, kNoValue);
1180 merge_map_.PutBefore(lb, merge_names_, value_name);
1181 if (!wide && gvn_->NullCheckedInAllPredecessors(merge_names_)) {
1182 null_checked_.insert(value_name);
1183 }
Razvan A Lupusorue0951142014-11-14 14:36:55 -08001184 if (gvn_->DivZeroCheckedInAllPredecessors(merge_names_)) {
1185 div_zero_checked_.insert(value_name);
1186 }
Vladimir Marko95a05972014-05-30 10:01:32 +01001187 }
1188 }
1189 if (wide) {
1190 SetOperandValueWide(mir->ssa_rep->defs[0], value_name);
1191 } else {
1192 SetOperandValue(mir->ssa_rep->defs[0], value_name);
1193 }
1194 return value_name;
1195}
1196
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001197uint16_t LocalValueNumbering::HandleAGet(MIR* mir, uint16_t opcode) {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001198 uint16_t array = GetOperandValue(mir->ssa_rep->uses[0]);
1199 HandleNullCheck(mir, array);
1200 uint16_t index = GetOperandValue(mir->ssa_rep->uses[1]);
1201 HandleRangeCheck(mir, array, index);
Vladimir Markoaf6925b2014-10-31 16:37:32 +00001202 uint16_t type = AGetMemAccessType(static_cast<Instruction::Code>(opcode));
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001203 // Establish value number for loaded register.
1204 uint16_t res;
1205 if (IsNonAliasingArray(array, type)) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001206 res = HandleAliasingValuesGet<NonAliasingArrayVersions>(&non_aliasing_array_value_map_,
1207 array, index);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001208 } else {
Vladimir Marko95a05972014-05-30 10:01:32 +01001209 uint16_t location = gvn_->GetArrayLocation(array, index);
1210 res = HandleAliasingValuesGet<AliasingArrayVersions>(&aliasing_array_value_map_,
1211 type, location);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001212 }
1213 if (opcode == Instruction::AGET_WIDE) {
1214 SetOperandValueWide(mir->ssa_rep->defs[0], res);
1215 } else {
1216 SetOperandValue(mir->ssa_rep->defs[0], res);
1217 }
1218 return res;
1219}
1220
1221void LocalValueNumbering::HandleAPut(MIR* mir, uint16_t opcode) {
1222 int array_idx = (opcode == Instruction::APUT_WIDE) ? 2 : 1;
1223 int index_idx = array_idx + 1;
1224 uint16_t array = GetOperandValue(mir->ssa_rep->uses[array_idx]);
1225 HandleNullCheck(mir, array);
1226 uint16_t index = GetOperandValue(mir->ssa_rep->uses[index_idx]);
1227 HandleRangeCheck(mir, array, index);
1228
Vladimir Markoaf6925b2014-10-31 16:37:32 +00001229 uint16_t type = APutMemAccessType(static_cast<Instruction::Code>(opcode));
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001230 uint16_t value = (opcode == Instruction::APUT_WIDE)
1231 ? GetOperandValueWide(mir->ssa_rep->uses[0])
1232 : GetOperandValue(mir->ssa_rep->uses[0]);
1233 if (IsNonAliasing(array)) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001234 bool put_is_live = HandleAliasingValuesPut<NonAliasingArrayVersions>(
1235 &non_aliasing_array_value_map_, array, index, value);
1236 if (!put_is_live) {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001237 // This APUT can be eliminated, it stores the same value that's already in the field.
1238 // TODO: Eliminate the APUT.
1239 return;
1240 }
Vladimir Marko95a05972014-05-30 10:01:32 +01001241 } else {
1242 uint16_t location = gvn_->GetArrayLocation(array, index);
1243 bool put_is_live = HandleAliasingValuesPut<AliasingArrayVersions>(
1244 &aliasing_array_value_map_, type, location, value);
1245 if (!put_is_live) {
1246 // This APUT can be eliminated, it stores the same value that's already in the field.
1247 // TODO: Eliminate the APUT.
1248 return;
1249 }
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001250
Vladimir Marko95a05972014-05-30 10:01:32 +01001251 // Clobber all escaped array refs for this type.
1252 for (uint16_t escaped_array : escaped_refs_) {
1253 EscapedArrayClobberKey clobber_key = { escaped_array, type };
1254 escaped_array_clobber_set_.insert(clobber_key);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001255 }
1256 }
1257}
1258
1259uint16_t LocalValueNumbering::HandleIGet(MIR* mir, uint16_t opcode) {
1260 uint16_t base = GetOperandValue(mir->ssa_rep->uses[0]);
1261 HandleNullCheck(mir, base);
Vladimir Marko95a05972014-05-30 10:01:32 +01001262 const MirFieldInfo& field_info = gvn_->GetMirGraph()->GetIFieldLoweringInfo(mir);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001263 uint16_t res;
1264 if (!field_info.IsResolved() || field_info.IsVolatile()) {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001265 // Unresolved fields may be volatile, so handle them as such to be safe.
Vladimir Markofa236452014-09-29 17:58:10 +01001266 HandleInvokeOrClInitOrAcquireOp(mir); // Volatile GETs have acquire semantics.
1267 // Volatile fields always get a new memory version; field id is irrelevant.
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001268 // Use result s_reg - will be unique.
Vladimir Marko95a05972014-05-30 10:01:32 +01001269 res = gvn_->LookupValue(kNoValue, mir->ssa_rep->defs[0], kNoValue, kNoValue);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001270 } else {
Vladimir Markoaf6925b2014-10-31 16:37:32 +00001271 uint16_t type = IGetMemAccessType(static_cast<Instruction::Code>(opcode));
1272 uint16_t field_id = gvn_->GetIFieldId(mir);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001273 if (IsNonAliasingIField(base, field_id, type)) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001274 uint16_t loc = gvn_->LookupValue(kNonAliasingIFieldLocOp, base, field_id, type);
1275 auto lb = non_aliasing_ifield_value_map_.lower_bound(loc);
1276 if (lb != non_aliasing_ifield_value_map_.end() && lb->first == loc) {
1277 res = lb->second;
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001278 } else {
Vladimir Marko95a05972014-05-30 10:01:32 +01001279 res = gvn_->LookupValue(kNonAliasingIFieldInitialOp, loc, kNoValue, kNoValue);
1280 non_aliasing_ifield_value_map_.PutBefore(lb, loc, res);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001281 }
Vladimir Marko95a05972014-05-30 10:01:32 +01001282 } else {
1283 res = HandleAliasingValuesGet<AliasingIFieldVersions>(&aliasing_ifield_value_map_,
1284 field_id, base);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001285 }
1286 }
1287 if (opcode == Instruction::IGET_WIDE) {
1288 SetOperandValueWide(mir->ssa_rep->defs[0], res);
1289 } else {
1290 SetOperandValue(mir->ssa_rep->defs[0], res);
1291 }
1292 return res;
1293}
1294
1295void LocalValueNumbering::HandleIPut(MIR* mir, uint16_t opcode) {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001296 int base_reg = (opcode == Instruction::IPUT_WIDE) ? 2 : 1;
1297 uint16_t base = GetOperandValue(mir->ssa_rep->uses[base_reg]);
1298 HandleNullCheck(mir, base);
Vladimir Markoaf6925b2014-10-31 16:37:32 +00001299 uint16_t type = IPutMemAccessType(static_cast<Instruction::Code>(opcode));
Vladimir Marko95a05972014-05-30 10:01:32 +01001300 const MirFieldInfo& field_info = gvn_->GetMirGraph()->GetIFieldLoweringInfo(mir);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001301 if (!field_info.IsResolved()) {
1302 // Unresolved fields always alias with everything of the same type.
1303 // Use mir->offset as modifier; without elaborate inlining, it will be unique.
1304 unresolved_ifield_version_[type] =
Vladimir Marko95a05972014-05-30 10:01:32 +01001305 gvn_->LookupValue(kUnresolvedIFieldOp, kNoValue, kNoValue, mir->offset);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001306
Vladimir Marko95a05972014-05-30 10:01:32 +01001307 // For simplicity, treat base as escaped now.
1308 HandleEscapingRef(base);
1309
1310 // Clobber all fields of escaped references of the same type.
1311 for (uint16_t escaped_ref : escaped_refs_) {
1312 EscapedIFieldClobberKey clobber_key = { escaped_ref, type, kNoValue };
1313 escaped_ifield_clobber_set_.insert(clobber_key);
1314 }
1315
1316 // Aliasing fields of the same type may have been overwritten.
1317 auto it = aliasing_ifield_value_map_.begin(), end = aliasing_ifield_value_map_.end();
1318 while (it != end) {
Vladimir Markoaf6925b2014-10-31 16:37:32 +00001319 if (gvn_->GetIFieldType(it->first) != type) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001320 ++it;
1321 } else {
1322 it = aliasing_ifield_value_map_.erase(it);
1323 }
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001324 }
1325 } else if (field_info.IsVolatile()) {
1326 // Nothing to do, resolved volatile fields always get a new memory version anyway and
1327 // can't alias with resolved non-volatile fields.
1328 } else {
Vladimir Markoaf6925b2014-10-31 16:37:32 +00001329 uint16_t field_id = gvn_->GetIFieldId(mir);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001330 uint16_t value = (opcode == Instruction::IPUT_WIDE)
1331 ? GetOperandValueWide(mir->ssa_rep->uses[0])
1332 : GetOperandValue(mir->ssa_rep->uses[0]);
1333 if (IsNonAliasing(base)) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001334 uint16_t loc = gvn_->LookupValue(kNonAliasingIFieldLocOp, base, field_id, type);
1335 auto lb = non_aliasing_ifield_value_map_.lower_bound(loc);
1336 if (lb != non_aliasing_ifield_value_map_.end() && lb->first == loc) {
1337 if (lb->second == value) {
1338 // This IPUT can be eliminated, it stores the same value that's already in the field.
1339 // TODO: Eliminate the IPUT.
1340 return;
1341 }
1342 lb->second = value; // Overwrite.
1343 } else {
1344 non_aliasing_ifield_value_map_.PutBefore(lb, loc, value);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001345 }
Vladimir Marko95a05972014-05-30 10:01:32 +01001346 } else {
1347 bool put_is_live = HandleAliasingValuesPut<AliasingIFieldVersions>(
1348 &aliasing_ifield_value_map_, field_id, base, value);
1349 if (!put_is_live) {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001350 // This IPUT can be eliminated, it stores the same value that's already in the field.
1351 // TODO: Eliminate the IPUT.
1352 return;
1353 }
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001354
Vladimir Marko95a05972014-05-30 10:01:32 +01001355 // Clobber all fields of escaped references for this field.
1356 for (uint16_t escaped_ref : escaped_refs_) {
1357 EscapedIFieldClobberKey clobber_key = { escaped_ref, type, field_id };
1358 escaped_ifield_clobber_set_.insert(clobber_key);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001359 }
1360 }
1361 }
1362}
1363
1364uint16_t LocalValueNumbering::HandleSGet(MIR* mir, uint16_t opcode) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001365 const MirSFieldLoweringInfo& field_info = gvn_->GetMirGraph()->GetSFieldLoweringInfo(mir);
Vladimir Markofa236452014-09-29 17:58:10 +01001366 if (!field_info.IsResolved() || field_info.IsVolatile() ||
Vladimir Marko66c6d7b2014-10-16 15:41:48 +01001367 (!field_info.IsClassInitialized() &&
1368 (mir->optimization_flags & MIR_CLASS_IS_INITIALIZED) == 0)) {
Vladimir Markofa236452014-09-29 17:58:10 +01001369 // Volatile SGETs (and unresolved fields are potentially volatile) have acquire semantics
1370 // and class initialization can call arbitrary functions, we need to wipe aliasing values.
1371 HandleInvokeOrClInitOrAcquireOp(mir);
Vladimir Markof418f322014-07-09 14:45:36 +01001372 }
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001373 uint16_t res;
1374 if (!field_info.IsResolved() || field_info.IsVolatile()) {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001375 // Unresolved fields may be volatile, so handle them as such to be safe.
Vladimir Markofa236452014-09-29 17:58:10 +01001376 // Volatile fields always get a new memory version; field id is irrelevant.
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001377 // Use result s_reg - will be unique.
Vladimir Marko95a05972014-05-30 10:01:32 +01001378 res = gvn_->LookupValue(kNoValue, mir->ssa_rep->defs[0], kNoValue, kNoValue);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001379 } else {
Vladimir Markoaf6925b2014-10-31 16:37:32 +00001380 uint16_t type = SGetMemAccessType(static_cast<Instruction::Code>(opcode));
1381 uint16_t field_id = gvn_->GetSFieldId(mir);
Vladimir Marko95a05972014-05-30 10:01:32 +01001382 auto lb = sfield_value_map_.lower_bound(field_id);
1383 if (lb != sfield_value_map_.end() && lb->first == field_id) {
1384 res = lb->second;
1385 } else {
1386 // Resolved non-volatile static fields can alias with non-resolved fields of the same type,
1387 // so we need to use unresolved_sfield_version_[type] in addition to global_memory_version_
1388 // to determine the version of the field.
1389 res = gvn_->LookupValue(kResolvedSFieldOp, field_id,
1390 unresolved_sfield_version_[type], global_memory_version_);
1391 sfield_value_map_.PutBefore(lb, field_id, res);
1392 }
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001393 }
1394 if (opcode == Instruction::SGET_WIDE) {
1395 SetOperandValueWide(mir->ssa_rep->defs[0], res);
1396 } else {
1397 SetOperandValue(mir->ssa_rep->defs[0], res);
1398 }
1399 return res;
1400}
1401
1402void LocalValueNumbering::HandleSPut(MIR* mir, uint16_t opcode) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001403 const MirSFieldLoweringInfo& field_info = gvn_->GetMirGraph()->GetSFieldLoweringInfo(mir);
Vladimir Marko66c6d7b2014-10-16 15:41:48 +01001404 if (!field_info.IsClassInitialized() &&
1405 (mir->optimization_flags & MIR_CLASS_IS_INITIALIZED) == 0) {
Vladimir Markof418f322014-07-09 14:45:36 +01001406 // Class initialization can call arbitrary functions, we need to wipe aliasing values.
Vladimir Markofa236452014-09-29 17:58:10 +01001407 HandleInvokeOrClInitOrAcquireOp(mir);
Vladimir Markof418f322014-07-09 14:45:36 +01001408 }
Vladimir Markoaf6925b2014-10-31 16:37:32 +00001409 uint16_t type = SPutMemAccessType(static_cast<Instruction::Code>(opcode));
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001410 if (!field_info.IsResolved()) {
1411 // Unresolved fields always alias with everything of the same type.
1412 // Use mir->offset as modifier; without elaborate inlining, it will be unique.
1413 unresolved_sfield_version_[type] =
Vladimir Marko95a05972014-05-30 10:01:32 +01001414 gvn_->LookupValue(kUnresolvedSFieldOp, kNoValue, kNoValue, mir->offset);
1415 RemoveSFieldsForType(type);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001416 } else if (field_info.IsVolatile()) {
1417 // Nothing to do, resolved volatile fields always get a new memory version anyway and
1418 // can't alias with resolved non-volatile fields.
1419 } else {
Vladimir Markoaf6925b2014-10-31 16:37:32 +00001420 uint16_t field_id = gvn_->GetSFieldId(mir);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001421 uint16_t value = (opcode == Instruction::SPUT_WIDE)
1422 ? GetOperandValueWide(mir->ssa_rep->uses[0])
1423 : GetOperandValue(mir->ssa_rep->uses[0]);
1424 // Resolved non-volatile static fields can alias with non-resolved fields of the same type,
1425 // so we need to use unresolved_sfield_version_[type] in addition to global_memory_version_
1426 // to determine the version of the field.
Vladimir Marko95a05972014-05-30 10:01:32 +01001427 auto lb = sfield_value_map_.lower_bound(field_id);
1428 if (lb != sfield_value_map_.end() && lb->first == field_id) {
1429 if (lb->second == value) {
1430 // This SPUT can be eliminated, it stores the same value that's already in the field.
1431 // TODO: Eliminate the SPUT.
1432 return;
1433 }
1434 lb->second = value; // Overwrite.
1435 } else {
1436 sfield_value_map_.PutBefore(lb, field_id, value);
1437 }
1438 }
1439}
1440
1441void LocalValueNumbering::RemoveSFieldsForType(uint16_t type) {
1442 // Erase all static fields of this type from the sfield_value_map_.
1443 for (auto it = sfield_value_map_.begin(), end = sfield_value_map_.end(); it != end; ) {
Vladimir Markoaf6925b2014-10-31 16:37:32 +00001444 if (gvn_->GetSFieldType(it->first) == type) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001445 it = sfield_value_map_.erase(it);
1446 } else {
1447 ++it;
1448 }
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001449 }
Vladimir Markof59f18b2014-02-17 15:53:57 +00001450}
buzbee2502e002012-12-31 16:05:53 -08001451
Vladimir Markofa236452014-09-29 17:58:10 +01001452void LocalValueNumbering::HandleInvokeOrClInitOrAcquireOp(MIR* mir) {
Vladimir Markof418f322014-07-09 14:45:36 +01001453 // Use mir->offset as modifier; without elaborate inlining, it will be unique.
Vladimir Marko95a05972014-05-30 10:01:32 +01001454 global_memory_version_ =
1455 gvn_->LookupValue(kInvokeMemoryVersionBumpOp, 0u, 0u, mir->offset);
1456 // All static fields and instance fields and array elements of aliasing references,
1457 // including escaped references, may have been modified.
1458 sfield_value_map_.clear();
1459 aliasing_ifield_value_map_.clear();
1460 aliasing_array_value_map_.clear();
1461 escaped_refs_.clear();
1462 escaped_ifield_clobber_set_.clear();
1463 escaped_array_clobber_set_.clear();
Vladimir Markof418f322014-07-09 14:45:36 +01001464}
1465
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001466uint16_t LocalValueNumbering::GetValueNumber(MIR* mir) {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001467 uint16_t res = kNoValue;
buzbee2502e002012-12-31 16:05:53 -08001468 uint16_t opcode = mir->dalvikInsn.opcode;
1469 switch (opcode) {
1470 case Instruction::NOP:
1471 case Instruction::RETURN_VOID:
1472 case Instruction::RETURN:
1473 case Instruction::RETURN_OBJECT:
1474 case Instruction::RETURN_WIDE:
buzbee2502e002012-12-31 16:05:53 -08001475 case Instruction::GOTO:
1476 case Instruction::GOTO_16:
1477 case Instruction::GOTO_32:
1478 case Instruction::CHECK_CAST:
1479 case Instruction::THROW:
1480 case Instruction::FILL_ARRAY_DATA:
buzbee2502e002012-12-31 16:05:53 -08001481 case Instruction::PACKED_SWITCH:
1482 case Instruction::SPARSE_SWITCH:
1483 case Instruction::IF_EQ:
1484 case Instruction::IF_NE:
1485 case Instruction::IF_LT:
1486 case Instruction::IF_GE:
1487 case Instruction::IF_GT:
1488 case Instruction::IF_LE:
1489 case Instruction::IF_EQZ:
1490 case Instruction::IF_NEZ:
1491 case Instruction::IF_LTZ:
1492 case Instruction::IF_GEZ:
1493 case Instruction::IF_GTZ:
1494 case Instruction::IF_LEZ:
buzbee2502e002012-12-31 16:05:53 -08001495 case kMirOpFusedCmplFloat:
1496 case kMirOpFusedCmpgFloat:
1497 case kMirOpFusedCmplDouble:
1498 case kMirOpFusedCmpgDouble:
1499 case kMirOpFusedCmpLong:
1500 // Nothing defined - take no action.
1501 break;
1502
Vladimir Marko95a05972014-05-30 10:01:32 +01001503 case Instruction::MONITOR_ENTER:
1504 HandleNullCheck(mir, GetOperandValue(mir->ssa_rep->uses[0]));
Vladimir Markofa236452014-09-29 17:58:10 +01001505 HandleInvokeOrClInitOrAcquireOp(mir); // Acquire operation.
Vladimir Marko95a05972014-05-30 10:01:32 +01001506 break;
1507
1508 case Instruction::MONITOR_EXIT:
1509 HandleNullCheck(mir, GetOperandValue(mir->ssa_rep->uses[0]));
1510 // If we're running GVN and CanModify(), uneliminated null check indicates bytecode error.
Vladimir Marko415ac882014-09-30 18:09:14 +01001511 if ((mir->optimization_flags & MIR_IGNORE_NULL_CHECK) == 0 &&
1512 gvn_->work_lvn_ != nullptr && gvn_->CanModify()) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001513 LOG(WARNING) << "Bytecode error: MONITOR_EXIT is still null checked at 0x" << std::hex
1514 << mir->offset << " in " << PrettyMethod(gvn_->cu_->method_idx, *gvn_->cu_->dex_file);
1515 }
1516 break;
1517
Vladimir Markof59f18b2014-02-17 15:53:57 +00001518 case Instruction::FILLED_NEW_ARRAY:
1519 case Instruction::FILLED_NEW_ARRAY_RANGE:
1520 // Nothing defined but the result will be unique and non-null.
1521 if (mir->next != nullptr && mir->next->dalvikInsn.opcode == Instruction::MOVE_RESULT_OBJECT) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001522 uint16_t array = MarkNonAliasingNonNull(mir->next);
1523 // Do not SetOperandValue(), we'll do that when we process the MOVE_RESULT_OBJECT.
1524 if (kLocalValueNumberingEnableFilledNewArrayTracking && mir->ssa_rep->num_uses != 0u) {
1525 AliasingValues* values = GetAliasingValues(&non_aliasing_array_value_map_, array);
1526 // Clear the value if we got a merged version in a loop.
Vladimir Markob19955d2014-07-29 12:04:10 +01001527 *values = AliasingValues(this);
Vladimir Marko95a05972014-05-30 10:01:32 +01001528 for (size_t i = 0u, count = mir->ssa_rep->num_uses; i != count; ++i) {
1529 DCHECK_EQ(High16Bits(i), 0u);
1530 uint16_t index = gvn_->LookupValue(Instruction::CONST, i, 0u, 0);
1531 uint16_t value = GetOperandValue(mir->ssa_rep->uses[i]);
1532 values->load_value_map.Put(index, value);
1533 RangeCheckKey key = { array, index };
1534 range_checked_.insert(key);
1535 }
1536 }
Vladimir Markof59f18b2014-02-17 15:53:57 +00001537 // The MOVE_RESULT_OBJECT will be processed next and we'll return the value name then.
1538 }
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001539 // All args escaped (if references).
1540 for (size_t i = 0u, count = mir->ssa_rep->num_uses; i != count; ++i) {
1541 uint16_t reg = GetOperandValue(mir->ssa_rep->uses[i]);
1542 HandleEscapingRef(reg);
1543 }
Vladimir Markof59f18b2014-02-17 15:53:57 +00001544 break;
1545
Vladimir Markoa78e66a2014-10-16 13:38:44 +01001546 case kMirOpNullCheck:
1547 HandleNullCheck(mir, GetOperandValue(mir->ssa_rep->uses[0]));
1548 break;
1549
Vladimir Markof59f18b2014-02-17 15:53:57 +00001550 case Instruction::INVOKE_DIRECT:
1551 case Instruction::INVOKE_DIRECT_RANGE:
1552 case Instruction::INVOKE_VIRTUAL:
1553 case Instruction::INVOKE_VIRTUAL_RANGE:
1554 case Instruction::INVOKE_SUPER:
1555 case Instruction::INVOKE_SUPER_RANGE:
1556 case Instruction::INVOKE_INTERFACE:
1557 case Instruction::INVOKE_INTERFACE_RANGE: {
1558 // Nothing defined but handle the null check.
1559 uint16_t reg = GetOperandValue(mir->ssa_rep->uses[0]);
1560 HandleNullCheck(mir, reg);
1561 }
Ian Rogersfc787ec2014-10-09 21:56:44 -07001562 FALLTHROUGH_INTENDED;
Vladimir Markof59f18b2014-02-17 15:53:57 +00001563 case Instruction::INVOKE_STATIC:
1564 case Instruction::INVOKE_STATIC_RANGE:
Vladimir Markoff0ac472014-10-02 17:24:53 +01001565 // Make ref args aliasing.
Vladimir Markoa4426cf2014-10-22 17:15:53 +01001566 HandleInvokeArgs(mir, this);
Vladimir Markoff0ac472014-10-02 17:24:53 +01001567 HandleInvokeOrClInitOrAcquireOp(mir);
Vladimir Markof59f18b2014-02-17 15:53:57 +00001568 break;
1569
buzbee2502e002012-12-31 16:05:53 -08001570 case Instruction::MOVE_RESULT:
1571 case Instruction::MOVE_RESULT_OBJECT:
1572 case Instruction::INSTANCE_OF:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001573 // 1 result, treat as unique each time, use result s_reg - will be unique.
1574 res = GetOperandValue(mir->ssa_rep->defs[0]);
1575 SetOperandValue(mir->ssa_rep->defs[0], res);
1576 break;
1577 case Instruction::MOVE_EXCEPTION:
buzbee2502e002012-12-31 16:05:53 -08001578 case Instruction::NEW_INSTANCE:
buzbee2502e002012-12-31 16:05:53 -08001579 case Instruction::CONST_CLASS:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001580 case Instruction::NEW_ARRAY:
Vladimir Markob3e527b2014-04-04 12:37:07 +01001581 // 1 result, treat as unique each time, use result s_reg - will be unique.
1582 res = MarkNonAliasingNonNull(mir);
Vladimir Marko95a05972014-05-30 10:01:32 +01001583 SetOperandValue(mir->ssa_rep->defs[0], res);
buzbee2502e002012-12-31 16:05:53 -08001584 break;
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001585 case Instruction::CONST_STRING:
1586 case Instruction::CONST_STRING_JUMBO:
1587 // These strings are internalized, so assign value based on the string pool index.
Vladimir Marko95a05972014-05-30 10:01:32 +01001588 res = gvn_->LookupValue(Instruction::CONST_STRING, Low16Bits(mir->dalvikInsn.vB),
1589 High16Bits(mir->dalvikInsn.vB), 0);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001590 SetOperandValue(mir->ssa_rep->defs[0], res);
1591 null_checked_.insert(res); // May already be there.
1592 // NOTE: Hacking the contents of an internalized string via reflection is possible
1593 // but the behavior is undefined. Therefore, we consider the string constant and
1594 // the reference non-aliasing.
1595 // TUNING: We could keep this property even if the reference "escapes".
1596 non_aliasing_refs_.insert(res); // May already be there.
1597 break;
Vladimir Markof59f18b2014-02-17 15:53:57 +00001598 case Instruction::MOVE_RESULT_WIDE:
Vladimir Markob3e527b2014-04-04 12:37:07 +01001599 // 1 wide result, treat as unique each time, use result s_reg - will be unique.
1600 res = GetOperandValueWide(mir->ssa_rep->defs[0]);
1601 SetOperandValueWide(mir->ssa_rep->defs[0], res);
buzbee2502e002012-12-31 16:05:53 -08001602 break;
1603
1604 case kMirOpPhi:
Vladimir Marko95a05972014-05-30 10:01:32 +01001605 res = HandlePhi(mir);
buzbee2502e002012-12-31 16:05:53 -08001606 break;
1607
1608 case Instruction::MOVE:
1609 case Instruction::MOVE_OBJECT:
1610 case Instruction::MOVE_16:
1611 case Instruction::MOVE_OBJECT_16:
1612 case Instruction::MOVE_FROM16:
1613 case Instruction::MOVE_OBJECT_FROM16:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001614 case kMirOpCopy:
1615 // Just copy value number of source to value number of result.
1616 res = GetOperandValue(mir->ssa_rep->uses[0]);
1617 SetOperandValue(mir->ssa_rep->defs[0], res);
buzbee2502e002012-12-31 16:05:53 -08001618 break;
1619
1620 case Instruction::MOVE_WIDE:
1621 case Instruction::MOVE_WIDE_16:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001622 case Instruction::MOVE_WIDE_FROM16:
1623 // Just copy value number of source to value number of result.
1624 res = GetOperandValueWide(mir->ssa_rep->uses[0]);
1625 SetOperandValueWide(mir->ssa_rep->defs[0], res);
buzbee2502e002012-12-31 16:05:53 -08001626 break;
1627
1628 case Instruction::CONST:
1629 case Instruction::CONST_4:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001630 case Instruction::CONST_16:
Vladimir Marko95a05972014-05-30 10:01:32 +01001631 res = gvn_->LookupValue(Instruction::CONST, Low16Bits(mir->dalvikInsn.vB),
1632 High16Bits(mir->dalvikInsn.vB), 0);
Vladimir Markof59f18b2014-02-17 15:53:57 +00001633 SetOperandValue(mir->ssa_rep->defs[0], res);
buzbee2502e002012-12-31 16:05:53 -08001634 break;
1635
Vladimir Markof59f18b2014-02-17 15:53:57 +00001636 case Instruction::CONST_HIGH16:
Vladimir Marko95a05972014-05-30 10:01:32 +01001637 res = gvn_->LookupValue(Instruction::CONST, 0, mir->dalvikInsn.vB, 0);
Vladimir Markof59f18b2014-02-17 15:53:57 +00001638 SetOperandValue(mir->ssa_rep->defs[0], res);
buzbee2502e002012-12-31 16:05:53 -08001639 break;
1640
1641 case Instruction::CONST_WIDE_16:
1642 case Instruction::CONST_WIDE_32: {
Vladimir Marko95a05972014-05-30 10:01:32 +01001643 uint16_t low_res = gvn_->LookupValue(Instruction::CONST, Low16Bits(mir->dalvikInsn.vB),
1644 High16Bits(mir->dalvikInsn.vB >> 16), 1);
buzbee2502e002012-12-31 16:05:53 -08001645 uint16_t high_res;
1646 if (mir->dalvikInsn.vB & 0x80000000) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001647 high_res = gvn_->LookupValue(Instruction::CONST, 0xffff, 0xffff, 2);
buzbee2502e002012-12-31 16:05:53 -08001648 } else {
Vladimir Marko95a05972014-05-30 10:01:32 +01001649 high_res = gvn_->LookupValue(Instruction::CONST, 0, 0, 2);
buzbee2502e002012-12-31 16:05:53 -08001650 }
Vladimir Marko95a05972014-05-30 10:01:32 +01001651 res = gvn_->LookupValue(Instruction::CONST, low_res, high_res, 3);
Vladimir Markof59f18b2014-02-17 15:53:57 +00001652 SetOperandValueWide(mir->ssa_rep->defs[0], res);
buzbee2502e002012-12-31 16:05:53 -08001653 }
1654 break;
1655
1656 case Instruction::CONST_WIDE: {
1657 uint32_t low_word = Low32Bits(mir->dalvikInsn.vB_wide);
1658 uint32_t high_word = High32Bits(mir->dalvikInsn.vB_wide);
Vladimir Marko95a05972014-05-30 10:01:32 +01001659 uint16_t low_res = gvn_->LookupValue(Instruction::CONST, Low16Bits(low_word),
1660 High16Bits(low_word), 1);
1661 uint16_t high_res = gvn_->LookupValue(Instruction::CONST, Low16Bits(high_word),
1662 High16Bits(high_word), 2);
1663 res = gvn_->LookupValue(Instruction::CONST, low_res, high_res, 3);
buzbee2502e002012-12-31 16:05:53 -08001664 SetOperandValueWide(mir->ssa_rep->defs[0], res);
1665 }
1666 break;
1667
1668 case Instruction::CONST_WIDE_HIGH16: {
Vladimir Marko95a05972014-05-30 10:01:32 +01001669 uint16_t low_res = gvn_->LookupValue(Instruction::CONST, 0, 0, 1);
1670 uint16_t high_res = gvn_->LookupValue(Instruction::CONST, 0,
1671 Low16Bits(mir->dalvikInsn.vB), 2);
1672 res = gvn_->LookupValue(Instruction::CONST, low_res, high_res, 3);
buzbee2502e002012-12-31 16:05:53 -08001673 SetOperandValueWide(mir->ssa_rep->defs[0], res);
1674 }
1675 break;
1676
Vladimir Marko95a05972014-05-30 10:01:32 +01001677 case Instruction::ARRAY_LENGTH: {
1678 // Handle the null check.
1679 uint16_t reg = GetOperandValue(mir->ssa_rep->uses[0]);
1680 HandleNullCheck(mir, reg);
1681 }
Ian Rogersfc787ec2014-10-09 21:56:44 -07001682 FALLTHROUGH_INTENDED;
buzbee2502e002012-12-31 16:05:53 -08001683 case Instruction::NEG_INT:
1684 case Instruction::NOT_INT:
1685 case Instruction::NEG_FLOAT:
1686 case Instruction::INT_TO_BYTE:
1687 case Instruction::INT_TO_SHORT:
1688 case Instruction::INT_TO_CHAR:
1689 case Instruction::INT_TO_FLOAT:
1690 case Instruction::FLOAT_TO_INT: {
1691 // res = op + 1 operand
1692 uint16_t operand1 = GetOperandValue(mir->ssa_rep->uses[0]);
Vladimir Marko95a05972014-05-30 10:01:32 +01001693 res = gvn_->LookupValue(opcode, operand1, kNoValue, kNoValue);
buzbee2502e002012-12-31 16:05:53 -08001694 SetOperandValue(mir->ssa_rep->defs[0], res);
1695 }
1696 break;
1697
1698 case Instruction::LONG_TO_FLOAT:
1699 case Instruction::LONG_TO_INT:
1700 case Instruction::DOUBLE_TO_FLOAT:
1701 case Instruction::DOUBLE_TO_INT: {
1702 // res = op + 1 wide operand
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001703 uint16_t operand1 = GetOperandValueWide(mir->ssa_rep->uses[0]);
Vladimir Marko95a05972014-05-30 10:01:32 +01001704 res = gvn_->LookupValue(opcode, operand1, kNoValue, kNoValue);
buzbee2502e002012-12-31 16:05:53 -08001705 SetOperandValue(mir->ssa_rep->defs[0], res);
1706 }
1707 break;
1708
buzbee2502e002012-12-31 16:05:53 -08001709 case Instruction::DOUBLE_TO_LONG:
1710 case Instruction::LONG_TO_DOUBLE:
1711 case Instruction::NEG_LONG:
1712 case Instruction::NOT_LONG:
1713 case Instruction::NEG_DOUBLE: {
1714 // wide res = op + 1 wide operand
1715 uint16_t operand1 = GetOperandValueWide(mir->ssa_rep->uses[0]);
Vladimir Marko95a05972014-05-30 10:01:32 +01001716 res = gvn_->LookupValue(opcode, operand1, kNoValue, kNoValue);
buzbee2502e002012-12-31 16:05:53 -08001717 SetOperandValueWide(mir->ssa_rep->defs[0], res);
1718 }
1719 break;
1720
1721 case Instruction::FLOAT_TO_DOUBLE:
1722 case Instruction::FLOAT_TO_LONG:
1723 case Instruction::INT_TO_DOUBLE:
1724 case Instruction::INT_TO_LONG: {
1725 // wide res = op + 1 operand
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001726 uint16_t operand1 = GetOperandValue(mir->ssa_rep->uses[0]);
Vladimir Marko95a05972014-05-30 10:01:32 +01001727 res = gvn_->LookupValue(opcode, operand1, kNoValue, kNoValue);
buzbee2502e002012-12-31 16:05:53 -08001728 SetOperandValueWide(mir->ssa_rep->defs[0], res);
1729 }
1730 break;
1731
1732 case Instruction::CMPL_DOUBLE:
1733 case Instruction::CMPG_DOUBLE:
1734 case Instruction::CMP_LONG: {
1735 // res = op + 2 wide operands
1736 uint16_t operand1 = GetOperandValueWide(mir->ssa_rep->uses[0]);
1737 uint16_t operand2 = GetOperandValueWide(mir->ssa_rep->uses[2]);
Vladimir Marko95a05972014-05-30 10:01:32 +01001738 res = gvn_->LookupValue(opcode, operand1, operand2, kNoValue);
buzbee2502e002012-12-31 16:05:53 -08001739 SetOperandValue(mir->ssa_rep->defs[0], res);
1740 }
1741 break;
1742
Razvan A Lupusorue0951142014-11-14 14:36:55 -08001743 case Instruction::DIV_INT:
1744 case Instruction::DIV_INT_2ADDR:
1745 case Instruction::REM_INT:
1746 case Instruction::REM_INT_2ADDR:
1747 HandleDivZeroCheck(mir, GetOperandValue(mir->ssa_rep->uses[1]));
1748 FALLTHROUGH_INTENDED;
1749
buzbee2502e002012-12-31 16:05:53 -08001750 case Instruction::CMPG_FLOAT:
1751 case Instruction::CMPL_FLOAT:
1752 case Instruction::ADD_INT:
1753 case Instruction::ADD_INT_2ADDR:
1754 case Instruction::MUL_INT:
1755 case Instruction::MUL_INT_2ADDR:
1756 case Instruction::AND_INT:
1757 case Instruction::AND_INT_2ADDR:
1758 case Instruction::OR_INT:
1759 case Instruction::OR_INT_2ADDR:
1760 case Instruction::XOR_INT:
1761 case Instruction::XOR_INT_2ADDR:
1762 case Instruction::SUB_INT:
1763 case Instruction::SUB_INT_2ADDR:
buzbee2502e002012-12-31 16:05:53 -08001764 case Instruction::SHL_INT:
1765 case Instruction::SHL_INT_2ADDR:
1766 case Instruction::SHR_INT:
1767 case Instruction::SHR_INT_2ADDR:
1768 case Instruction::USHR_INT:
1769 case Instruction::USHR_INT_2ADDR: {
1770 // res = op + 2 operands
1771 uint16_t operand1 = GetOperandValue(mir->ssa_rep->uses[0]);
1772 uint16_t operand2 = GetOperandValue(mir->ssa_rep->uses[1]);
Vladimir Marko95a05972014-05-30 10:01:32 +01001773 res = gvn_->LookupValue(opcode, operand1, operand2, kNoValue);
buzbee2502e002012-12-31 16:05:53 -08001774 SetOperandValue(mir->ssa_rep->defs[0], res);
1775 }
1776 break;
1777
Razvan A Lupusorue0951142014-11-14 14:36:55 -08001778 case Instruction::DIV_LONG:
1779 case Instruction::REM_LONG:
1780 case Instruction::DIV_LONG_2ADDR:
1781 case Instruction::REM_LONG_2ADDR:
1782 HandleDivZeroCheck(mir, GetOperandValueWide(mir->ssa_rep->uses[2]));
1783 FALLTHROUGH_INTENDED;
1784
buzbee2502e002012-12-31 16:05:53 -08001785 case Instruction::ADD_LONG:
1786 case Instruction::SUB_LONG:
1787 case Instruction::MUL_LONG:
buzbee2502e002012-12-31 16:05:53 -08001788 case Instruction::AND_LONG:
1789 case Instruction::OR_LONG:
1790 case Instruction::XOR_LONG:
1791 case Instruction::ADD_LONG_2ADDR:
1792 case Instruction::SUB_LONG_2ADDR:
1793 case Instruction::MUL_LONG_2ADDR:
buzbee2502e002012-12-31 16:05:53 -08001794 case Instruction::AND_LONG_2ADDR:
1795 case Instruction::OR_LONG_2ADDR:
1796 case Instruction::XOR_LONG_2ADDR:
1797 case Instruction::ADD_DOUBLE:
1798 case Instruction::SUB_DOUBLE:
1799 case Instruction::MUL_DOUBLE:
1800 case Instruction::DIV_DOUBLE:
1801 case Instruction::REM_DOUBLE:
1802 case Instruction::ADD_DOUBLE_2ADDR:
1803 case Instruction::SUB_DOUBLE_2ADDR:
1804 case Instruction::MUL_DOUBLE_2ADDR:
1805 case Instruction::DIV_DOUBLE_2ADDR:
1806 case Instruction::REM_DOUBLE_2ADDR: {
1807 // wide res = op + 2 wide operands
1808 uint16_t operand1 = GetOperandValueWide(mir->ssa_rep->uses[0]);
1809 uint16_t operand2 = GetOperandValueWide(mir->ssa_rep->uses[2]);
Vladimir Marko95a05972014-05-30 10:01:32 +01001810 res = gvn_->LookupValue(opcode, operand1, operand2, kNoValue);
buzbee2502e002012-12-31 16:05:53 -08001811 SetOperandValueWide(mir->ssa_rep->defs[0], res);
1812 }
1813 break;
1814
1815 case Instruction::SHL_LONG:
1816 case Instruction::SHR_LONG:
1817 case Instruction::USHR_LONG:
1818 case Instruction::SHL_LONG_2ADDR:
1819 case Instruction::SHR_LONG_2ADDR:
1820 case Instruction::USHR_LONG_2ADDR: {
1821 // wide res = op + 1 wide operand + 1 operand
1822 uint16_t operand1 = GetOperandValueWide(mir->ssa_rep->uses[0]);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001823 uint16_t operand2 = GetOperandValue(mir->ssa_rep->uses[2]);
Vladimir Marko95a05972014-05-30 10:01:32 +01001824 res = gvn_->LookupValue(opcode, operand1, operand2, kNoValue);
buzbee2502e002012-12-31 16:05:53 -08001825 SetOperandValueWide(mir->ssa_rep->defs[0], res);
1826 }
1827 break;
1828
1829 case Instruction::ADD_FLOAT:
1830 case Instruction::SUB_FLOAT:
1831 case Instruction::MUL_FLOAT:
1832 case Instruction::DIV_FLOAT:
1833 case Instruction::REM_FLOAT:
1834 case Instruction::ADD_FLOAT_2ADDR:
1835 case Instruction::SUB_FLOAT_2ADDR:
1836 case Instruction::MUL_FLOAT_2ADDR:
1837 case Instruction::DIV_FLOAT_2ADDR:
1838 case Instruction::REM_FLOAT_2ADDR: {
1839 // res = op + 2 operands
1840 uint16_t operand1 = GetOperandValue(mir->ssa_rep->uses[0]);
1841 uint16_t operand2 = GetOperandValue(mir->ssa_rep->uses[1]);
Vladimir Marko95a05972014-05-30 10:01:32 +01001842 res = gvn_->LookupValue(opcode, operand1, operand2, kNoValue);
buzbee2502e002012-12-31 16:05:53 -08001843 SetOperandValue(mir->ssa_rep->defs[0], res);
1844 }
1845 break;
1846
1847 case Instruction::RSUB_INT:
1848 case Instruction::ADD_INT_LIT16:
1849 case Instruction::MUL_INT_LIT16:
1850 case Instruction::DIV_INT_LIT16:
1851 case Instruction::REM_INT_LIT16:
1852 case Instruction::AND_INT_LIT16:
1853 case Instruction::OR_INT_LIT16:
1854 case Instruction::XOR_INT_LIT16:
1855 case Instruction::ADD_INT_LIT8:
1856 case Instruction::RSUB_INT_LIT8:
1857 case Instruction::MUL_INT_LIT8:
1858 case Instruction::DIV_INT_LIT8:
1859 case Instruction::REM_INT_LIT8:
1860 case Instruction::AND_INT_LIT8:
1861 case Instruction::OR_INT_LIT8:
1862 case Instruction::XOR_INT_LIT8:
1863 case Instruction::SHL_INT_LIT8:
1864 case Instruction::SHR_INT_LIT8:
1865 case Instruction::USHR_INT_LIT8: {
nikolay serdjukee40aa42014-03-25 12:21:29 +07001866 // Same as res = op + 2 operands, except use vC as operand 2
buzbee2502e002012-12-31 16:05:53 -08001867 uint16_t operand1 = GetOperandValue(mir->ssa_rep->uses[0]);
Vladimir Marko95a05972014-05-30 10:01:32 +01001868 uint16_t operand2 = gvn_->LookupValue(Instruction::CONST, mir->dalvikInsn.vC, 0, 0);
1869 res = gvn_->LookupValue(opcode, operand1, operand2, kNoValue);
buzbee2502e002012-12-31 16:05:53 -08001870 SetOperandValue(mir->ssa_rep->defs[0], res);
1871 }
1872 break;
1873
buzbee2502e002012-12-31 16:05:53 -08001874 case Instruction::AGET_OBJECT:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001875 case Instruction::AGET:
1876 case Instruction::AGET_WIDE:
buzbee2502e002012-12-31 16:05:53 -08001877 case Instruction::AGET_BOOLEAN:
1878 case Instruction::AGET_BYTE:
1879 case Instruction::AGET_CHAR:
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001880 case Instruction::AGET_SHORT:
1881 res = HandleAGet(mir, opcode);
buzbee2502e002012-12-31 16:05:53 -08001882 break;
1883
buzbee2502e002012-12-31 16:05:53 -08001884 case Instruction::APUT_OBJECT:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001885 HandlePutObject(mir);
Ian Rogersfc787ec2014-10-09 21:56:44 -07001886 FALLTHROUGH_INTENDED;
Vladimir Markof59f18b2014-02-17 15:53:57 +00001887 case Instruction::APUT:
1888 case Instruction::APUT_WIDE:
buzbee2502e002012-12-31 16:05:53 -08001889 case Instruction::APUT_BYTE:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001890 case Instruction::APUT_BOOLEAN:
1891 case Instruction::APUT_SHORT:
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001892 case Instruction::APUT_CHAR:
1893 HandleAPut(mir, opcode);
buzbee2502e002012-12-31 16:05:53 -08001894 break;
1895
1896 case Instruction::IGET_OBJECT:
buzbee2502e002012-12-31 16:05:53 -08001897 case Instruction::IGET:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001898 case Instruction::IGET_WIDE:
buzbee2502e002012-12-31 16:05:53 -08001899 case Instruction::IGET_BOOLEAN:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001900 case Instruction::IGET_BYTE:
1901 case Instruction::IGET_CHAR:
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001902 case Instruction::IGET_SHORT:
1903 res = HandleIGet(mir, opcode);
buzbee2502e002012-12-31 16:05:53 -08001904 break;
1905
buzbee2502e002012-12-31 16:05:53 -08001906 case Instruction::IPUT_OBJECT:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001907 HandlePutObject(mir);
Ian Rogersfc787ec2014-10-09 21:56:44 -07001908 FALLTHROUGH_INTENDED;
buzbee2502e002012-12-31 16:05:53 -08001909 case Instruction::IPUT:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001910 case Instruction::IPUT_WIDE:
buzbee2502e002012-12-31 16:05:53 -08001911 case Instruction::IPUT_BOOLEAN:
1912 case Instruction::IPUT_BYTE:
1913 case Instruction::IPUT_CHAR:
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001914 case Instruction::IPUT_SHORT:
1915 HandleIPut(mir, opcode);
buzbee2502e002012-12-31 16:05:53 -08001916 break;
1917
1918 case Instruction::SGET_OBJECT:
1919 case Instruction::SGET:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001920 case Instruction::SGET_WIDE:
buzbee2502e002012-12-31 16:05:53 -08001921 case Instruction::SGET_BOOLEAN:
1922 case Instruction::SGET_BYTE:
1923 case Instruction::SGET_CHAR:
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001924 case Instruction::SGET_SHORT:
1925 res = HandleSGet(mir, opcode);
buzbee2502e002012-12-31 16:05:53 -08001926 break;
1927
1928 case Instruction::SPUT_OBJECT:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001929 HandlePutObject(mir);
Ian Rogersfc787ec2014-10-09 21:56:44 -07001930 FALLTHROUGH_INTENDED;
buzbee2502e002012-12-31 16:05:53 -08001931 case Instruction::SPUT:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001932 case Instruction::SPUT_WIDE:
buzbee2502e002012-12-31 16:05:53 -08001933 case Instruction::SPUT_BOOLEAN:
1934 case Instruction::SPUT_BYTE:
1935 case Instruction::SPUT_CHAR:
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001936 case Instruction::SPUT_SHORT:
1937 HandleSPut(mir, opcode);
buzbee2502e002012-12-31 16:05:53 -08001938 break;
buzbee2502e002012-12-31 16:05:53 -08001939 }
1940 return res;
1941}
1942
1943} // namespace art