blob: 854759b43000991f16a6c4de49dce179dcca1417 [file] [log] [blame]
buzbee2502e002012-12-31 16:05:53 -08001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
buzbee311ca162013-02-28 15:56:43 -080017#include "local_value_numbering.h"
buzbee2502e002012-12-31 16:05:53 -080018
Vladimir Marko95a05972014-05-30 10:01:32 +010019#include "global_value_numbering.h"
Vladimir Markobe0e5462014-02-26 11:24:15 +000020#include "mir_field_info.h"
Vladimir Markof59f18b2014-02-17 15:53:57 +000021#include "mir_graph.h"
22
buzbee2502e002012-12-31 16:05:53 -080023namespace art {
24
Vladimir Marko2ac01fc2014-05-22 12:09:08 +010025namespace { // anonymous namespace
26
27// Operations used for value map keys instead of actual opcode.
Vladimir Marko95a05972014-05-30 10:01:32 +010028static constexpr uint16_t kInvokeMemoryVersionBumpOp = Instruction::INVOKE_VIRTUAL;
29static constexpr uint16_t kUnresolvedSFieldOp = Instruction::SGET;
30static constexpr uint16_t kResolvedSFieldOp = Instruction::SGET_WIDE;
31static constexpr uint16_t kUnresolvedIFieldOp = Instruction::IGET;
32static constexpr uint16_t kNonAliasingIFieldLocOp = Instruction::IGET_WIDE;
33static constexpr uint16_t kNonAliasingIFieldInitialOp = Instruction::IGET_OBJECT;
34static constexpr uint16_t kAliasingIFieldOp = Instruction::IGET_BOOLEAN;
35static constexpr uint16_t kAliasingIFieldStartVersionOp = Instruction::IGET_BYTE;
36static constexpr uint16_t kAliasingIFieldBumpVersionOp = Instruction::IGET_CHAR;
Vladimir Marko2ac01fc2014-05-22 12:09:08 +010037static constexpr uint16_t kNonAliasingArrayOp = Instruction::AGET;
38static constexpr uint16_t kNonAliasingArrayStartVersionOp = Instruction::AGET_WIDE;
Vladimir Marko95a05972014-05-30 10:01:32 +010039static constexpr uint16_t kNonAliasingArrayBumpVersionOp = Instruction::AGET_OBJECT;
40static constexpr uint16_t kAliasingArrayOp = Instruction::AGET_BOOLEAN;
41static constexpr uint16_t kAliasingArrayStartVersionOp = Instruction::AGET_BYTE;
42static constexpr uint16_t kAliasingArrayBumpVersionOp = Instruction::AGET_CHAR;
43static constexpr uint16_t kMergeBlockMemoryVersionBumpOp = Instruction::INVOKE_VIRTUAL_RANGE;
44static constexpr uint16_t kMergeBlockAliasingIFieldVersionBumpOp = Instruction::IPUT;
45static constexpr uint16_t kMergeBlockAliasingIFieldMergeLocationOp = Instruction::IPUT_WIDE;
46static constexpr uint16_t kMergeBlockNonAliasingArrayVersionBumpOp = Instruction::APUT;
47static constexpr uint16_t kMergeBlockNonAliasingArrayMergeLocationOp = Instruction::APUT_WIDE;
48static constexpr uint16_t kMergeBlockAliasingArrayVersionBumpOp = Instruction::APUT_OBJECT;
49static constexpr uint16_t kMergeBlockAliasingArrayMergeLocationOp = Instruction::APUT_BOOLEAN;
50static constexpr uint16_t kMergeBlockNonAliasingIFieldVersionBumpOp = Instruction::APUT_BYTE;
51static constexpr uint16_t kMergeBlockSFieldVersionBumpOp = Instruction::APUT_CHAR;
Vladimir Marko2ac01fc2014-05-22 12:09:08 +010052
53} // anonymous namespace
54
Vladimir Marko95a05972014-05-30 10:01:32 +010055class LocalValueNumbering::AliasingIFieldVersions {
56 public:
57 static uint16_t StartMemoryVersion(GlobalValueNumbering* gvn, const LocalValueNumbering* lvn,
58 uint16_t field_id) {
59 uint16_t type = gvn->GetFieldType(field_id);
60 return gvn->LookupValue(kAliasingIFieldStartVersionOp, field_id,
61 lvn->global_memory_version_, lvn->unresolved_ifield_version_[type]);
62 }
63
64 static uint16_t BumpMemoryVersion(GlobalValueNumbering* gvn, uint16_t old_version,
65 uint16_t store_ref_set_id, uint16_t stored_value) {
66 return gvn->LookupValue(kAliasingIFieldBumpVersionOp, old_version,
67 store_ref_set_id, stored_value);
68 }
69
70 static uint16_t LookupGlobalValue(GlobalValueNumbering* gvn,
71 uint16_t field_id, uint16_t base, uint16_t memory_version) {
72 return gvn->LookupValue(kAliasingIFieldOp, field_id, base, memory_version);
73 }
74
75 static uint16_t LookupMergeValue(GlobalValueNumbering* gvn, const LocalValueNumbering* lvn,
76 uint16_t field_id, uint16_t base) {
77 // If the base/field_id is non-aliasing in lvn, use the non-aliasing value.
78 uint16_t type = gvn->GetFieldType(field_id);
79 if (lvn->IsNonAliasingIField(base, field_id, type)) {
80 uint16_t loc = gvn->LookupValue(kNonAliasingIFieldLocOp, base, field_id, type);
81 auto lb = lvn->non_aliasing_ifield_value_map_.find(loc);
82 return (lb != lvn->non_aliasing_ifield_value_map_.end())
83 ? lb->second
84 : gvn->LookupValue(kNonAliasingIFieldInitialOp, loc, kNoValue, kNoValue);
85 }
86 return AliasingValuesMergeGet<AliasingIFieldVersions>(
87 gvn, lvn, &lvn->aliasing_ifield_value_map_, field_id, base);
88 }
89
90 static bool HasNewBaseVersion(GlobalValueNumbering* gvn, const LocalValueNumbering* lvn,
91 uint16_t field_id) {
92 uint16_t type = gvn->GetFieldType(field_id);
93 return lvn->unresolved_ifield_version_[type] == lvn->merge_new_memory_version_ ||
94 lvn->global_memory_version_ == lvn->merge_new_memory_version_;
95 }
96
97 static uint16_t LookupMergeBlockValue(GlobalValueNumbering* gvn, uint16_t lvn_id,
98 uint16_t field_id) {
99 return gvn->LookupValue(kMergeBlockAliasingIFieldVersionBumpOp, field_id, kNoValue, lvn_id);
100 }
101
102 static uint16_t LookupMergeLocationValue(GlobalValueNumbering* gvn, uint16_t lvn_id,
103 uint16_t field_id, uint16_t base) {
104 return gvn->LookupValue(kMergeBlockAliasingIFieldMergeLocationOp, field_id, base, lvn_id);
105 }
106};
107
108class LocalValueNumbering::NonAliasingArrayVersions {
109 public:
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 {
739 uint16_t type = gvn_->GetFieldType(field_id);
740 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) {
1198 // uint16_t type = opcode - Instruction::AGET;
1199 uint16_t array = GetOperandValue(mir->ssa_rep->uses[0]);
1200 HandleNullCheck(mir, array);
1201 uint16_t index = GetOperandValue(mir->ssa_rep->uses[1]);
1202 HandleRangeCheck(mir, array, index);
1203 uint16_t type = opcode - Instruction::AGET;
1204 // Establish value number for loaded register.
1205 uint16_t res;
1206 if (IsNonAliasingArray(array, type)) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001207 res = HandleAliasingValuesGet<NonAliasingArrayVersions>(&non_aliasing_array_value_map_,
1208 array, index);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001209 } else {
Vladimir Marko95a05972014-05-30 10:01:32 +01001210 uint16_t location = gvn_->GetArrayLocation(array, index);
1211 res = HandleAliasingValuesGet<AliasingArrayVersions>(&aliasing_array_value_map_,
1212 type, location);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001213 }
1214 if (opcode == Instruction::AGET_WIDE) {
1215 SetOperandValueWide(mir->ssa_rep->defs[0], res);
1216 } else {
1217 SetOperandValue(mir->ssa_rep->defs[0], res);
1218 }
1219 return res;
1220}
1221
1222void LocalValueNumbering::HandleAPut(MIR* mir, uint16_t opcode) {
1223 int array_idx = (opcode == Instruction::APUT_WIDE) ? 2 : 1;
1224 int index_idx = array_idx + 1;
1225 uint16_t array = GetOperandValue(mir->ssa_rep->uses[array_idx]);
1226 HandleNullCheck(mir, array);
1227 uint16_t index = GetOperandValue(mir->ssa_rep->uses[index_idx]);
1228 HandleRangeCheck(mir, array, index);
1229
1230 uint16_t type = opcode - Instruction::APUT;
1231 uint16_t value = (opcode == Instruction::APUT_WIDE)
1232 ? GetOperandValueWide(mir->ssa_rep->uses[0])
1233 : GetOperandValue(mir->ssa_rep->uses[0]);
1234 if (IsNonAliasing(array)) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001235 bool put_is_live = HandleAliasingValuesPut<NonAliasingArrayVersions>(
1236 &non_aliasing_array_value_map_, array, index, value);
1237 if (!put_is_live) {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001238 // This APUT can be eliminated, it stores the same value that's already in the field.
1239 // TODO: Eliminate the APUT.
1240 return;
1241 }
Vladimir Marko95a05972014-05-30 10:01:32 +01001242 } else {
1243 uint16_t location = gvn_->GetArrayLocation(array, index);
1244 bool put_is_live = HandleAliasingValuesPut<AliasingArrayVersions>(
1245 &aliasing_array_value_map_, type, location, value);
1246 if (!put_is_live) {
1247 // This APUT can be eliminated, it stores the same value that's already in the field.
1248 // TODO: Eliminate the APUT.
1249 return;
1250 }
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001251
Vladimir Marko95a05972014-05-30 10:01:32 +01001252 // Clobber all escaped array refs for this type.
1253 for (uint16_t escaped_array : escaped_refs_) {
1254 EscapedArrayClobberKey clobber_key = { escaped_array, type };
1255 escaped_array_clobber_set_.insert(clobber_key);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001256 }
1257 }
1258}
1259
1260uint16_t LocalValueNumbering::HandleIGet(MIR* mir, uint16_t opcode) {
1261 uint16_t base = GetOperandValue(mir->ssa_rep->uses[0]);
1262 HandleNullCheck(mir, base);
Vladimir Marko95a05972014-05-30 10:01:32 +01001263 const MirFieldInfo& field_info = gvn_->GetMirGraph()->GetIFieldLoweringInfo(mir);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001264 uint16_t res;
1265 if (!field_info.IsResolved() || field_info.IsVolatile()) {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001266 // Unresolved fields may be volatile, so handle them as such to be safe.
Vladimir Markofa236452014-09-29 17:58:10 +01001267 HandleInvokeOrClInitOrAcquireOp(mir); // Volatile GETs have acquire semantics.
1268 // Volatile fields always get a new memory version; field id is irrelevant.
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001269 // Use result s_reg - will be unique.
Vladimir Marko95a05972014-05-30 10:01:32 +01001270 res = gvn_->LookupValue(kNoValue, mir->ssa_rep->defs[0], kNoValue, kNoValue);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001271 } else {
1272 uint16_t type = opcode - Instruction::IGET;
Vladimir Marko95a05972014-05-30 10:01:32 +01001273 uint16_t field_id = gvn_->GetFieldId(field_info, type);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001274 if (IsNonAliasingIField(base, field_id, type)) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001275 uint16_t loc = gvn_->LookupValue(kNonAliasingIFieldLocOp, base, field_id, type);
1276 auto lb = non_aliasing_ifield_value_map_.lower_bound(loc);
1277 if (lb != non_aliasing_ifield_value_map_.end() && lb->first == loc) {
1278 res = lb->second;
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001279 } else {
Vladimir Marko95a05972014-05-30 10:01:32 +01001280 res = gvn_->LookupValue(kNonAliasingIFieldInitialOp, loc, kNoValue, kNoValue);
1281 non_aliasing_ifield_value_map_.PutBefore(lb, loc, res);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001282 }
Vladimir Marko95a05972014-05-30 10:01:32 +01001283 } else {
1284 res = HandleAliasingValuesGet<AliasingIFieldVersions>(&aliasing_ifield_value_map_,
1285 field_id, base);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001286 }
1287 }
1288 if (opcode == Instruction::IGET_WIDE) {
1289 SetOperandValueWide(mir->ssa_rep->defs[0], res);
1290 } else {
1291 SetOperandValue(mir->ssa_rep->defs[0], res);
1292 }
1293 return res;
1294}
1295
1296void LocalValueNumbering::HandleIPut(MIR* mir, uint16_t opcode) {
1297 uint16_t type = opcode - Instruction::IPUT;
1298 int base_reg = (opcode == Instruction::IPUT_WIDE) ? 2 : 1;
1299 uint16_t base = GetOperandValue(mir->ssa_rep->uses[base_reg]);
1300 HandleNullCheck(mir, base);
Vladimir Marko95a05972014-05-30 10:01:32 +01001301 const MirFieldInfo& field_info = gvn_->GetMirGraph()->GetIFieldLoweringInfo(mir);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001302 if (!field_info.IsResolved()) {
1303 // Unresolved fields always alias with everything of the same type.
1304 // Use mir->offset as modifier; without elaborate inlining, it will be unique.
1305 unresolved_ifield_version_[type] =
Vladimir Marko95a05972014-05-30 10:01:32 +01001306 gvn_->LookupValue(kUnresolvedIFieldOp, kNoValue, kNoValue, mir->offset);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001307
Vladimir Marko95a05972014-05-30 10:01:32 +01001308 // For simplicity, treat base as escaped now.
1309 HandleEscapingRef(base);
1310
1311 // Clobber all fields of escaped references of the same type.
1312 for (uint16_t escaped_ref : escaped_refs_) {
1313 EscapedIFieldClobberKey clobber_key = { escaped_ref, type, kNoValue };
1314 escaped_ifield_clobber_set_.insert(clobber_key);
1315 }
1316
1317 // Aliasing fields of the same type may have been overwritten.
1318 auto it = aliasing_ifield_value_map_.begin(), end = aliasing_ifield_value_map_.end();
1319 while (it != end) {
1320 if (gvn_->GetFieldType(it->first) != type) {
1321 ++it;
1322 } else {
1323 it = aliasing_ifield_value_map_.erase(it);
1324 }
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001325 }
1326 } else if (field_info.IsVolatile()) {
1327 // Nothing to do, resolved volatile fields always get a new memory version anyway and
1328 // can't alias with resolved non-volatile fields.
1329 } else {
Vladimir Marko95a05972014-05-30 10:01:32 +01001330 uint16_t field_id = gvn_->GetFieldId(field_info, type);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001331 uint16_t value = (opcode == Instruction::IPUT_WIDE)
1332 ? GetOperandValueWide(mir->ssa_rep->uses[0])
1333 : GetOperandValue(mir->ssa_rep->uses[0]);
1334 if (IsNonAliasing(base)) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001335 uint16_t loc = gvn_->LookupValue(kNonAliasingIFieldLocOp, base, field_id, type);
1336 auto lb = non_aliasing_ifield_value_map_.lower_bound(loc);
1337 if (lb != non_aliasing_ifield_value_map_.end() && lb->first == loc) {
1338 if (lb->second == value) {
1339 // This IPUT can be eliminated, it stores the same value that's already in the field.
1340 // TODO: Eliminate the IPUT.
1341 return;
1342 }
1343 lb->second = value; // Overwrite.
1344 } else {
1345 non_aliasing_ifield_value_map_.PutBefore(lb, loc, value);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001346 }
Vladimir Marko95a05972014-05-30 10:01:32 +01001347 } else {
1348 bool put_is_live = HandleAliasingValuesPut<AliasingIFieldVersions>(
1349 &aliasing_ifield_value_map_, field_id, base, value);
1350 if (!put_is_live) {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001351 // This IPUT can be eliminated, it stores the same value that's already in the field.
1352 // TODO: Eliminate the IPUT.
1353 return;
1354 }
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001355
Vladimir Marko95a05972014-05-30 10:01:32 +01001356 // Clobber all fields of escaped references for this field.
1357 for (uint16_t escaped_ref : escaped_refs_) {
1358 EscapedIFieldClobberKey clobber_key = { escaped_ref, type, field_id };
1359 escaped_ifield_clobber_set_.insert(clobber_key);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001360 }
1361 }
1362 }
1363}
1364
1365uint16_t LocalValueNumbering::HandleSGet(MIR* mir, uint16_t opcode) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001366 const MirSFieldLoweringInfo& field_info = gvn_->GetMirGraph()->GetSFieldLoweringInfo(mir);
Vladimir Markofa236452014-09-29 17:58:10 +01001367 if (!field_info.IsResolved() || field_info.IsVolatile() ||
Vladimir Marko66c6d7b2014-10-16 15:41:48 +01001368 (!field_info.IsClassInitialized() &&
1369 (mir->optimization_flags & MIR_CLASS_IS_INITIALIZED) == 0)) {
Vladimir Markofa236452014-09-29 17:58:10 +01001370 // Volatile SGETs (and unresolved fields are potentially volatile) have acquire semantics
1371 // and class initialization can call arbitrary functions, we need to wipe aliasing values.
1372 HandleInvokeOrClInitOrAcquireOp(mir);
Vladimir Markof418f322014-07-09 14:45:36 +01001373 }
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001374 uint16_t res;
1375 if (!field_info.IsResolved() || field_info.IsVolatile()) {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001376 // Unresolved fields may be volatile, so handle them as such to be safe.
Vladimir Markofa236452014-09-29 17:58:10 +01001377 // Volatile fields always get a new memory version; field id is irrelevant.
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001378 // Use result s_reg - will be unique.
Vladimir Marko95a05972014-05-30 10:01:32 +01001379 res = gvn_->LookupValue(kNoValue, mir->ssa_rep->defs[0], kNoValue, kNoValue);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001380 } else {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001381 uint16_t type = opcode - Instruction::SGET;
Vladimir Marko95a05972014-05-30 10:01:32 +01001382 uint16_t field_id = gvn_->GetFieldId(field_info, type);
1383 auto lb = sfield_value_map_.lower_bound(field_id);
1384 if (lb != sfield_value_map_.end() && lb->first == field_id) {
1385 res = lb->second;
1386 } else {
1387 // Resolved non-volatile static fields can alias with non-resolved fields of the same type,
1388 // so we need to use unresolved_sfield_version_[type] in addition to global_memory_version_
1389 // to determine the version of the field.
1390 res = gvn_->LookupValue(kResolvedSFieldOp, field_id,
1391 unresolved_sfield_version_[type], global_memory_version_);
1392 sfield_value_map_.PutBefore(lb, field_id, res);
1393 }
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001394 }
1395 if (opcode == Instruction::SGET_WIDE) {
1396 SetOperandValueWide(mir->ssa_rep->defs[0], res);
1397 } else {
1398 SetOperandValue(mir->ssa_rep->defs[0], res);
1399 }
1400 return res;
1401}
1402
1403void LocalValueNumbering::HandleSPut(MIR* mir, uint16_t opcode) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001404 const MirSFieldLoweringInfo& field_info = gvn_->GetMirGraph()->GetSFieldLoweringInfo(mir);
Vladimir Marko66c6d7b2014-10-16 15:41:48 +01001405 if (!field_info.IsClassInitialized() &&
1406 (mir->optimization_flags & MIR_CLASS_IS_INITIALIZED) == 0) {
Vladimir Markof418f322014-07-09 14:45:36 +01001407 // Class initialization can call arbitrary functions, we need to wipe aliasing values.
Vladimir Markofa236452014-09-29 17:58:10 +01001408 HandleInvokeOrClInitOrAcquireOp(mir);
Vladimir Markof418f322014-07-09 14:45:36 +01001409 }
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001410 uint16_t type = opcode - Instruction::SPUT;
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001411 if (!field_info.IsResolved()) {
1412 // Unresolved fields always alias with everything of the same type.
1413 // Use mir->offset as modifier; without elaborate inlining, it will be unique.
1414 unresolved_sfield_version_[type] =
Vladimir Marko95a05972014-05-30 10:01:32 +01001415 gvn_->LookupValue(kUnresolvedSFieldOp, kNoValue, kNoValue, mir->offset);
1416 RemoveSFieldsForType(type);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001417 } else if (field_info.IsVolatile()) {
1418 // Nothing to do, resolved volatile fields always get a new memory version anyway and
1419 // can't alias with resolved non-volatile fields.
1420 } else {
Vladimir Marko95a05972014-05-30 10:01:32 +01001421 uint16_t field_id = gvn_->GetFieldId(field_info, type);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001422 uint16_t value = (opcode == Instruction::SPUT_WIDE)
1423 ? GetOperandValueWide(mir->ssa_rep->uses[0])
1424 : GetOperandValue(mir->ssa_rep->uses[0]);
1425 // Resolved non-volatile static fields can alias with non-resolved fields of the same type,
1426 // so we need to use unresolved_sfield_version_[type] in addition to global_memory_version_
1427 // to determine the version of the field.
Vladimir Marko95a05972014-05-30 10:01:32 +01001428 auto lb = sfield_value_map_.lower_bound(field_id);
1429 if (lb != sfield_value_map_.end() && lb->first == field_id) {
1430 if (lb->second == value) {
1431 // This SPUT can be eliminated, it stores the same value that's already in the field.
1432 // TODO: Eliminate the SPUT.
1433 return;
1434 }
1435 lb->second = value; // Overwrite.
1436 } else {
1437 sfield_value_map_.PutBefore(lb, field_id, value);
1438 }
1439 }
1440}
1441
1442void LocalValueNumbering::RemoveSFieldsForType(uint16_t type) {
1443 // Erase all static fields of this type from the sfield_value_map_.
1444 for (auto it = sfield_value_map_.begin(), end = sfield_value_map_.end(); it != end; ) {
1445 if (gvn_->GetFieldType(it->first) == type) {
1446 it = sfield_value_map_.erase(it);
1447 } else {
1448 ++it;
1449 }
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001450 }
Vladimir Markof59f18b2014-02-17 15:53:57 +00001451}
buzbee2502e002012-12-31 16:05:53 -08001452
Vladimir Markofa236452014-09-29 17:58:10 +01001453void LocalValueNumbering::HandleInvokeOrClInitOrAcquireOp(MIR* mir) {
Vladimir Markof418f322014-07-09 14:45:36 +01001454 // Use mir->offset as modifier; without elaborate inlining, it will be unique.
Vladimir Marko95a05972014-05-30 10:01:32 +01001455 global_memory_version_ =
1456 gvn_->LookupValue(kInvokeMemoryVersionBumpOp, 0u, 0u, mir->offset);
1457 // All static fields and instance fields and array elements of aliasing references,
1458 // including escaped references, may have been modified.
1459 sfield_value_map_.clear();
1460 aliasing_ifield_value_map_.clear();
1461 aliasing_array_value_map_.clear();
1462 escaped_refs_.clear();
1463 escaped_ifield_clobber_set_.clear();
1464 escaped_array_clobber_set_.clear();
Vladimir Markof418f322014-07-09 14:45:36 +01001465}
1466
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001467uint16_t LocalValueNumbering::GetValueNumber(MIR* mir) {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001468 uint16_t res = kNoValue;
buzbee2502e002012-12-31 16:05:53 -08001469 uint16_t opcode = mir->dalvikInsn.opcode;
1470 switch (opcode) {
1471 case Instruction::NOP:
1472 case Instruction::RETURN_VOID:
1473 case Instruction::RETURN:
1474 case Instruction::RETURN_OBJECT:
1475 case Instruction::RETURN_WIDE:
buzbee2502e002012-12-31 16:05:53 -08001476 case Instruction::GOTO:
1477 case Instruction::GOTO_16:
1478 case Instruction::GOTO_32:
1479 case Instruction::CHECK_CAST:
1480 case Instruction::THROW:
1481 case Instruction::FILL_ARRAY_DATA:
buzbee2502e002012-12-31 16:05:53 -08001482 case Instruction::PACKED_SWITCH:
1483 case Instruction::SPARSE_SWITCH:
1484 case Instruction::IF_EQ:
1485 case Instruction::IF_NE:
1486 case Instruction::IF_LT:
1487 case Instruction::IF_GE:
1488 case Instruction::IF_GT:
1489 case Instruction::IF_LE:
1490 case Instruction::IF_EQZ:
1491 case Instruction::IF_NEZ:
1492 case Instruction::IF_LTZ:
1493 case Instruction::IF_GEZ:
1494 case Instruction::IF_GTZ:
1495 case Instruction::IF_LEZ:
buzbee2502e002012-12-31 16:05:53 -08001496 case kMirOpFusedCmplFloat:
1497 case kMirOpFusedCmpgFloat:
1498 case kMirOpFusedCmplDouble:
1499 case kMirOpFusedCmpgDouble:
1500 case kMirOpFusedCmpLong:
1501 // Nothing defined - take no action.
1502 break;
1503
Vladimir Marko95a05972014-05-30 10:01:32 +01001504 case Instruction::MONITOR_ENTER:
1505 HandleNullCheck(mir, GetOperandValue(mir->ssa_rep->uses[0]));
Vladimir Markofa236452014-09-29 17:58:10 +01001506 HandleInvokeOrClInitOrAcquireOp(mir); // Acquire operation.
Vladimir Marko95a05972014-05-30 10:01:32 +01001507 break;
1508
1509 case Instruction::MONITOR_EXIT:
1510 HandleNullCheck(mir, GetOperandValue(mir->ssa_rep->uses[0]));
1511 // If we're running GVN and CanModify(), uneliminated null check indicates bytecode error.
Vladimir Marko415ac882014-09-30 18:09:14 +01001512 if ((mir->optimization_flags & MIR_IGNORE_NULL_CHECK) == 0 &&
1513 gvn_->work_lvn_ != nullptr && gvn_->CanModify()) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001514 LOG(WARNING) << "Bytecode error: MONITOR_EXIT is still null checked at 0x" << std::hex
1515 << mir->offset << " in " << PrettyMethod(gvn_->cu_->method_idx, *gvn_->cu_->dex_file);
1516 }
1517 break;
1518
Vladimir Markof59f18b2014-02-17 15:53:57 +00001519 case Instruction::FILLED_NEW_ARRAY:
1520 case Instruction::FILLED_NEW_ARRAY_RANGE:
1521 // Nothing defined but the result will be unique and non-null.
1522 if (mir->next != nullptr && mir->next->dalvikInsn.opcode == Instruction::MOVE_RESULT_OBJECT) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001523 uint16_t array = MarkNonAliasingNonNull(mir->next);
1524 // Do not SetOperandValue(), we'll do that when we process the MOVE_RESULT_OBJECT.
1525 if (kLocalValueNumberingEnableFilledNewArrayTracking && mir->ssa_rep->num_uses != 0u) {
1526 AliasingValues* values = GetAliasingValues(&non_aliasing_array_value_map_, array);
1527 // Clear the value if we got a merged version in a loop.
Vladimir Markob19955d2014-07-29 12:04:10 +01001528 *values = AliasingValues(this);
Vladimir Marko95a05972014-05-30 10:01:32 +01001529 for (size_t i = 0u, count = mir->ssa_rep->num_uses; i != count; ++i) {
1530 DCHECK_EQ(High16Bits(i), 0u);
1531 uint16_t index = gvn_->LookupValue(Instruction::CONST, i, 0u, 0);
1532 uint16_t value = GetOperandValue(mir->ssa_rep->uses[i]);
1533 values->load_value_map.Put(index, value);
1534 RangeCheckKey key = { array, index };
1535 range_checked_.insert(key);
1536 }
1537 }
Vladimir Markof59f18b2014-02-17 15:53:57 +00001538 // The MOVE_RESULT_OBJECT will be processed next and we'll return the value name then.
1539 }
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001540 // All args escaped (if references).
1541 for (size_t i = 0u, count = mir->ssa_rep->num_uses; i != count; ++i) {
1542 uint16_t reg = GetOperandValue(mir->ssa_rep->uses[i]);
1543 HandleEscapingRef(reg);
1544 }
Vladimir Markof59f18b2014-02-17 15:53:57 +00001545 break;
1546
Vladimir Markoa78e66a2014-10-16 13:38:44 +01001547 case kMirOpNullCheck:
1548 HandleNullCheck(mir, GetOperandValue(mir->ssa_rep->uses[0]));
1549 break;
1550
Vladimir Markof59f18b2014-02-17 15:53:57 +00001551 case Instruction::INVOKE_DIRECT:
1552 case Instruction::INVOKE_DIRECT_RANGE:
1553 case Instruction::INVOKE_VIRTUAL:
1554 case Instruction::INVOKE_VIRTUAL_RANGE:
1555 case Instruction::INVOKE_SUPER:
1556 case Instruction::INVOKE_SUPER_RANGE:
1557 case Instruction::INVOKE_INTERFACE:
1558 case Instruction::INVOKE_INTERFACE_RANGE: {
1559 // Nothing defined but handle the null check.
1560 uint16_t reg = GetOperandValue(mir->ssa_rep->uses[0]);
1561 HandleNullCheck(mir, reg);
1562 }
Ian Rogersfc787ec2014-10-09 21:56:44 -07001563 FALLTHROUGH_INTENDED;
Vladimir Markof59f18b2014-02-17 15:53:57 +00001564 case Instruction::INVOKE_STATIC:
1565 case Instruction::INVOKE_STATIC_RANGE:
Vladimir Markoff0ac472014-10-02 17:24:53 +01001566 // Make ref args aliasing.
Vladimir Markoa4426cf2014-10-22 17:15:53 +01001567 HandleInvokeArgs(mir, this);
Vladimir Markoff0ac472014-10-02 17:24:53 +01001568 HandleInvokeOrClInitOrAcquireOp(mir);
Vladimir Markof59f18b2014-02-17 15:53:57 +00001569 break;
1570
buzbee2502e002012-12-31 16:05:53 -08001571 case Instruction::MOVE_RESULT:
1572 case Instruction::MOVE_RESULT_OBJECT:
1573 case Instruction::INSTANCE_OF:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001574 // 1 result, treat as unique each time, use result s_reg - will be unique.
1575 res = GetOperandValue(mir->ssa_rep->defs[0]);
1576 SetOperandValue(mir->ssa_rep->defs[0], res);
1577 break;
1578 case Instruction::MOVE_EXCEPTION:
buzbee2502e002012-12-31 16:05:53 -08001579 case Instruction::NEW_INSTANCE:
buzbee2502e002012-12-31 16:05:53 -08001580 case Instruction::CONST_CLASS:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001581 case Instruction::NEW_ARRAY:
Vladimir Markob3e527b2014-04-04 12:37:07 +01001582 // 1 result, treat as unique each time, use result s_reg - will be unique.
1583 res = MarkNonAliasingNonNull(mir);
Vladimir Marko95a05972014-05-30 10:01:32 +01001584 SetOperandValue(mir->ssa_rep->defs[0], res);
buzbee2502e002012-12-31 16:05:53 -08001585 break;
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001586 case Instruction::CONST_STRING:
1587 case Instruction::CONST_STRING_JUMBO:
1588 // These strings are internalized, so assign value based on the string pool index.
Vladimir Marko95a05972014-05-30 10:01:32 +01001589 res = gvn_->LookupValue(Instruction::CONST_STRING, Low16Bits(mir->dalvikInsn.vB),
1590 High16Bits(mir->dalvikInsn.vB), 0);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001591 SetOperandValue(mir->ssa_rep->defs[0], res);
1592 null_checked_.insert(res); // May already be there.
1593 // NOTE: Hacking the contents of an internalized string via reflection is possible
1594 // but the behavior is undefined. Therefore, we consider the string constant and
1595 // the reference non-aliasing.
1596 // TUNING: We could keep this property even if the reference "escapes".
1597 non_aliasing_refs_.insert(res); // May already be there.
1598 break;
Vladimir Markof59f18b2014-02-17 15:53:57 +00001599 case Instruction::MOVE_RESULT_WIDE:
Vladimir Markob3e527b2014-04-04 12:37:07 +01001600 // 1 wide result, treat as unique each time, use result s_reg - will be unique.
1601 res = GetOperandValueWide(mir->ssa_rep->defs[0]);
1602 SetOperandValueWide(mir->ssa_rep->defs[0], res);
buzbee2502e002012-12-31 16:05:53 -08001603 break;
1604
1605 case kMirOpPhi:
Vladimir Marko95a05972014-05-30 10:01:32 +01001606 res = HandlePhi(mir);
buzbee2502e002012-12-31 16:05:53 -08001607 break;
1608
1609 case Instruction::MOVE:
1610 case Instruction::MOVE_OBJECT:
1611 case Instruction::MOVE_16:
1612 case Instruction::MOVE_OBJECT_16:
1613 case Instruction::MOVE_FROM16:
1614 case Instruction::MOVE_OBJECT_FROM16:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001615 case kMirOpCopy:
1616 // Just copy value number of source to value number of result.
1617 res = GetOperandValue(mir->ssa_rep->uses[0]);
1618 SetOperandValue(mir->ssa_rep->defs[0], res);
buzbee2502e002012-12-31 16:05:53 -08001619 break;
1620
1621 case Instruction::MOVE_WIDE:
1622 case Instruction::MOVE_WIDE_16:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001623 case Instruction::MOVE_WIDE_FROM16:
1624 // Just copy value number of source to value number of result.
1625 res = GetOperandValueWide(mir->ssa_rep->uses[0]);
1626 SetOperandValueWide(mir->ssa_rep->defs[0], res);
buzbee2502e002012-12-31 16:05:53 -08001627 break;
1628
1629 case Instruction::CONST:
1630 case Instruction::CONST_4:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001631 case Instruction::CONST_16:
Vladimir Marko95a05972014-05-30 10:01:32 +01001632 res = gvn_->LookupValue(Instruction::CONST, Low16Bits(mir->dalvikInsn.vB),
1633 High16Bits(mir->dalvikInsn.vB), 0);
Vladimir Markof59f18b2014-02-17 15:53:57 +00001634 SetOperandValue(mir->ssa_rep->defs[0], res);
buzbee2502e002012-12-31 16:05:53 -08001635 break;
1636
Vladimir Markof59f18b2014-02-17 15:53:57 +00001637 case Instruction::CONST_HIGH16:
Vladimir Marko95a05972014-05-30 10:01:32 +01001638 res = gvn_->LookupValue(Instruction::CONST, 0, mir->dalvikInsn.vB, 0);
Vladimir Markof59f18b2014-02-17 15:53:57 +00001639 SetOperandValue(mir->ssa_rep->defs[0], res);
buzbee2502e002012-12-31 16:05:53 -08001640 break;
1641
1642 case Instruction::CONST_WIDE_16:
1643 case Instruction::CONST_WIDE_32: {
Vladimir Marko95a05972014-05-30 10:01:32 +01001644 uint16_t low_res = gvn_->LookupValue(Instruction::CONST, Low16Bits(mir->dalvikInsn.vB),
1645 High16Bits(mir->dalvikInsn.vB >> 16), 1);
buzbee2502e002012-12-31 16:05:53 -08001646 uint16_t high_res;
1647 if (mir->dalvikInsn.vB & 0x80000000) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001648 high_res = gvn_->LookupValue(Instruction::CONST, 0xffff, 0xffff, 2);
buzbee2502e002012-12-31 16:05:53 -08001649 } else {
Vladimir Marko95a05972014-05-30 10:01:32 +01001650 high_res = gvn_->LookupValue(Instruction::CONST, 0, 0, 2);
buzbee2502e002012-12-31 16:05:53 -08001651 }
Vladimir Marko95a05972014-05-30 10:01:32 +01001652 res = gvn_->LookupValue(Instruction::CONST, low_res, high_res, 3);
Vladimir Markof59f18b2014-02-17 15:53:57 +00001653 SetOperandValueWide(mir->ssa_rep->defs[0], res);
buzbee2502e002012-12-31 16:05:53 -08001654 }
1655 break;
1656
1657 case Instruction::CONST_WIDE: {
1658 uint32_t low_word = Low32Bits(mir->dalvikInsn.vB_wide);
1659 uint32_t high_word = High32Bits(mir->dalvikInsn.vB_wide);
Vladimir Marko95a05972014-05-30 10:01:32 +01001660 uint16_t low_res = gvn_->LookupValue(Instruction::CONST, Low16Bits(low_word),
1661 High16Bits(low_word), 1);
1662 uint16_t high_res = gvn_->LookupValue(Instruction::CONST, Low16Bits(high_word),
1663 High16Bits(high_word), 2);
1664 res = gvn_->LookupValue(Instruction::CONST, low_res, high_res, 3);
buzbee2502e002012-12-31 16:05:53 -08001665 SetOperandValueWide(mir->ssa_rep->defs[0], res);
1666 }
1667 break;
1668
1669 case Instruction::CONST_WIDE_HIGH16: {
Vladimir Marko95a05972014-05-30 10:01:32 +01001670 uint16_t low_res = gvn_->LookupValue(Instruction::CONST, 0, 0, 1);
1671 uint16_t high_res = gvn_->LookupValue(Instruction::CONST, 0,
1672 Low16Bits(mir->dalvikInsn.vB), 2);
1673 res = gvn_->LookupValue(Instruction::CONST, low_res, high_res, 3);
buzbee2502e002012-12-31 16:05:53 -08001674 SetOperandValueWide(mir->ssa_rep->defs[0], res);
1675 }
1676 break;
1677
Vladimir Marko95a05972014-05-30 10:01:32 +01001678 case Instruction::ARRAY_LENGTH: {
1679 // Handle the null check.
1680 uint16_t reg = GetOperandValue(mir->ssa_rep->uses[0]);
1681 HandleNullCheck(mir, reg);
1682 }
Ian Rogersfc787ec2014-10-09 21:56:44 -07001683 FALLTHROUGH_INTENDED;
buzbee2502e002012-12-31 16:05:53 -08001684 case Instruction::NEG_INT:
1685 case Instruction::NOT_INT:
1686 case Instruction::NEG_FLOAT:
1687 case Instruction::INT_TO_BYTE:
1688 case Instruction::INT_TO_SHORT:
1689 case Instruction::INT_TO_CHAR:
1690 case Instruction::INT_TO_FLOAT:
1691 case Instruction::FLOAT_TO_INT: {
1692 // res = op + 1 operand
1693 uint16_t operand1 = GetOperandValue(mir->ssa_rep->uses[0]);
Vladimir Marko95a05972014-05-30 10:01:32 +01001694 res = gvn_->LookupValue(opcode, operand1, kNoValue, kNoValue);
buzbee2502e002012-12-31 16:05:53 -08001695 SetOperandValue(mir->ssa_rep->defs[0], res);
1696 }
1697 break;
1698
1699 case Instruction::LONG_TO_FLOAT:
1700 case Instruction::LONG_TO_INT:
1701 case Instruction::DOUBLE_TO_FLOAT:
1702 case Instruction::DOUBLE_TO_INT: {
1703 // res = op + 1 wide operand
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001704 uint16_t operand1 = GetOperandValueWide(mir->ssa_rep->uses[0]);
Vladimir Marko95a05972014-05-30 10:01:32 +01001705 res = gvn_->LookupValue(opcode, operand1, kNoValue, kNoValue);
buzbee2502e002012-12-31 16:05:53 -08001706 SetOperandValue(mir->ssa_rep->defs[0], res);
1707 }
1708 break;
1709
buzbee2502e002012-12-31 16:05:53 -08001710 case Instruction::DOUBLE_TO_LONG:
1711 case Instruction::LONG_TO_DOUBLE:
1712 case Instruction::NEG_LONG:
1713 case Instruction::NOT_LONG:
1714 case Instruction::NEG_DOUBLE: {
1715 // wide res = op + 1 wide operand
1716 uint16_t operand1 = GetOperandValueWide(mir->ssa_rep->uses[0]);
Vladimir Marko95a05972014-05-30 10:01:32 +01001717 res = gvn_->LookupValue(opcode, operand1, kNoValue, kNoValue);
buzbee2502e002012-12-31 16:05:53 -08001718 SetOperandValueWide(mir->ssa_rep->defs[0], res);
1719 }
1720 break;
1721
1722 case Instruction::FLOAT_TO_DOUBLE:
1723 case Instruction::FLOAT_TO_LONG:
1724 case Instruction::INT_TO_DOUBLE:
1725 case Instruction::INT_TO_LONG: {
1726 // wide res = op + 1 operand
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001727 uint16_t operand1 = GetOperandValue(mir->ssa_rep->uses[0]);
Vladimir Marko95a05972014-05-30 10:01:32 +01001728 res = gvn_->LookupValue(opcode, operand1, kNoValue, kNoValue);
buzbee2502e002012-12-31 16:05:53 -08001729 SetOperandValueWide(mir->ssa_rep->defs[0], res);
1730 }
1731 break;
1732
1733 case Instruction::CMPL_DOUBLE:
1734 case Instruction::CMPG_DOUBLE:
1735 case Instruction::CMP_LONG: {
1736 // res = op + 2 wide operands
1737 uint16_t operand1 = GetOperandValueWide(mir->ssa_rep->uses[0]);
1738 uint16_t operand2 = GetOperandValueWide(mir->ssa_rep->uses[2]);
Vladimir Marko95a05972014-05-30 10:01:32 +01001739 res = gvn_->LookupValue(opcode, operand1, operand2, kNoValue);
buzbee2502e002012-12-31 16:05:53 -08001740 SetOperandValue(mir->ssa_rep->defs[0], res);
1741 }
1742 break;
1743
Razvan A Lupusorue0951142014-11-14 14:36:55 -08001744 case Instruction::DIV_INT:
1745 case Instruction::DIV_INT_2ADDR:
1746 case Instruction::REM_INT:
1747 case Instruction::REM_INT_2ADDR:
1748 HandleDivZeroCheck(mir, GetOperandValue(mir->ssa_rep->uses[1]));
1749 FALLTHROUGH_INTENDED;
1750
buzbee2502e002012-12-31 16:05:53 -08001751 case Instruction::CMPG_FLOAT:
1752 case Instruction::CMPL_FLOAT:
1753 case Instruction::ADD_INT:
1754 case Instruction::ADD_INT_2ADDR:
1755 case Instruction::MUL_INT:
1756 case Instruction::MUL_INT_2ADDR:
1757 case Instruction::AND_INT:
1758 case Instruction::AND_INT_2ADDR:
1759 case Instruction::OR_INT:
1760 case Instruction::OR_INT_2ADDR:
1761 case Instruction::XOR_INT:
1762 case Instruction::XOR_INT_2ADDR:
1763 case Instruction::SUB_INT:
1764 case Instruction::SUB_INT_2ADDR:
buzbee2502e002012-12-31 16:05:53 -08001765 case Instruction::SHL_INT:
1766 case Instruction::SHL_INT_2ADDR:
1767 case Instruction::SHR_INT:
1768 case Instruction::SHR_INT_2ADDR:
1769 case Instruction::USHR_INT:
1770 case Instruction::USHR_INT_2ADDR: {
1771 // res = op + 2 operands
1772 uint16_t operand1 = GetOperandValue(mir->ssa_rep->uses[0]);
1773 uint16_t operand2 = GetOperandValue(mir->ssa_rep->uses[1]);
Vladimir Marko95a05972014-05-30 10:01:32 +01001774 res = gvn_->LookupValue(opcode, operand1, operand2, kNoValue);
buzbee2502e002012-12-31 16:05:53 -08001775 SetOperandValue(mir->ssa_rep->defs[0], res);
1776 }
1777 break;
1778
Razvan A Lupusorue0951142014-11-14 14:36:55 -08001779 case Instruction::DIV_LONG:
1780 case Instruction::REM_LONG:
1781 case Instruction::DIV_LONG_2ADDR:
1782 case Instruction::REM_LONG_2ADDR:
1783 HandleDivZeroCheck(mir, GetOperandValueWide(mir->ssa_rep->uses[2]));
1784 FALLTHROUGH_INTENDED;
1785
buzbee2502e002012-12-31 16:05:53 -08001786 case Instruction::ADD_LONG:
1787 case Instruction::SUB_LONG:
1788 case Instruction::MUL_LONG:
buzbee2502e002012-12-31 16:05:53 -08001789 case Instruction::AND_LONG:
1790 case Instruction::OR_LONG:
1791 case Instruction::XOR_LONG:
1792 case Instruction::ADD_LONG_2ADDR:
1793 case Instruction::SUB_LONG_2ADDR:
1794 case Instruction::MUL_LONG_2ADDR:
buzbee2502e002012-12-31 16:05:53 -08001795 case Instruction::AND_LONG_2ADDR:
1796 case Instruction::OR_LONG_2ADDR:
1797 case Instruction::XOR_LONG_2ADDR:
1798 case Instruction::ADD_DOUBLE:
1799 case Instruction::SUB_DOUBLE:
1800 case Instruction::MUL_DOUBLE:
1801 case Instruction::DIV_DOUBLE:
1802 case Instruction::REM_DOUBLE:
1803 case Instruction::ADD_DOUBLE_2ADDR:
1804 case Instruction::SUB_DOUBLE_2ADDR:
1805 case Instruction::MUL_DOUBLE_2ADDR:
1806 case Instruction::DIV_DOUBLE_2ADDR:
1807 case Instruction::REM_DOUBLE_2ADDR: {
1808 // wide res = op + 2 wide operands
1809 uint16_t operand1 = GetOperandValueWide(mir->ssa_rep->uses[0]);
1810 uint16_t operand2 = GetOperandValueWide(mir->ssa_rep->uses[2]);
Vladimir Marko95a05972014-05-30 10:01:32 +01001811 res = gvn_->LookupValue(opcode, operand1, operand2, kNoValue);
buzbee2502e002012-12-31 16:05:53 -08001812 SetOperandValueWide(mir->ssa_rep->defs[0], res);
1813 }
1814 break;
1815
1816 case Instruction::SHL_LONG:
1817 case Instruction::SHR_LONG:
1818 case Instruction::USHR_LONG:
1819 case Instruction::SHL_LONG_2ADDR:
1820 case Instruction::SHR_LONG_2ADDR:
1821 case Instruction::USHR_LONG_2ADDR: {
1822 // wide res = op + 1 wide operand + 1 operand
1823 uint16_t operand1 = GetOperandValueWide(mir->ssa_rep->uses[0]);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001824 uint16_t operand2 = GetOperandValue(mir->ssa_rep->uses[2]);
Vladimir Marko95a05972014-05-30 10:01:32 +01001825 res = gvn_->LookupValue(opcode, operand1, operand2, kNoValue);
buzbee2502e002012-12-31 16:05:53 -08001826 SetOperandValueWide(mir->ssa_rep->defs[0], res);
1827 }
1828 break;
1829
1830 case Instruction::ADD_FLOAT:
1831 case Instruction::SUB_FLOAT:
1832 case Instruction::MUL_FLOAT:
1833 case Instruction::DIV_FLOAT:
1834 case Instruction::REM_FLOAT:
1835 case Instruction::ADD_FLOAT_2ADDR:
1836 case Instruction::SUB_FLOAT_2ADDR:
1837 case Instruction::MUL_FLOAT_2ADDR:
1838 case Instruction::DIV_FLOAT_2ADDR:
1839 case Instruction::REM_FLOAT_2ADDR: {
1840 // res = op + 2 operands
1841 uint16_t operand1 = GetOperandValue(mir->ssa_rep->uses[0]);
1842 uint16_t operand2 = GetOperandValue(mir->ssa_rep->uses[1]);
Vladimir Marko95a05972014-05-30 10:01:32 +01001843 res = gvn_->LookupValue(opcode, operand1, operand2, kNoValue);
buzbee2502e002012-12-31 16:05:53 -08001844 SetOperandValue(mir->ssa_rep->defs[0], res);
1845 }
1846 break;
1847
1848 case Instruction::RSUB_INT:
1849 case Instruction::ADD_INT_LIT16:
1850 case Instruction::MUL_INT_LIT16:
1851 case Instruction::DIV_INT_LIT16:
1852 case Instruction::REM_INT_LIT16:
1853 case Instruction::AND_INT_LIT16:
1854 case Instruction::OR_INT_LIT16:
1855 case Instruction::XOR_INT_LIT16:
1856 case Instruction::ADD_INT_LIT8:
1857 case Instruction::RSUB_INT_LIT8:
1858 case Instruction::MUL_INT_LIT8:
1859 case Instruction::DIV_INT_LIT8:
1860 case Instruction::REM_INT_LIT8:
1861 case Instruction::AND_INT_LIT8:
1862 case Instruction::OR_INT_LIT8:
1863 case Instruction::XOR_INT_LIT8:
1864 case Instruction::SHL_INT_LIT8:
1865 case Instruction::SHR_INT_LIT8:
1866 case Instruction::USHR_INT_LIT8: {
nikolay serdjukee40aa42014-03-25 12:21:29 +07001867 // Same as res = op + 2 operands, except use vC as operand 2
buzbee2502e002012-12-31 16:05:53 -08001868 uint16_t operand1 = GetOperandValue(mir->ssa_rep->uses[0]);
Vladimir Marko95a05972014-05-30 10:01:32 +01001869 uint16_t operand2 = gvn_->LookupValue(Instruction::CONST, mir->dalvikInsn.vC, 0, 0);
1870 res = gvn_->LookupValue(opcode, operand1, operand2, kNoValue);
buzbee2502e002012-12-31 16:05:53 -08001871 SetOperandValue(mir->ssa_rep->defs[0], res);
1872 }
1873 break;
1874
buzbee2502e002012-12-31 16:05:53 -08001875 case Instruction::AGET_OBJECT:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001876 case Instruction::AGET:
1877 case Instruction::AGET_WIDE:
buzbee2502e002012-12-31 16:05:53 -08001878 case Instruction::AGET_BOOLEAN:
1879 case Instruction::AGET_BYTE:
1880 case Instruction::AGET_CHAR:
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001881 case Instruction::AGET_SHORT:
1882 res = HandleAGet(mir, opcode);
buzbee2502e002012-12-31 16:05:53 -08001883 break;
1884
buzbee2502e002012-12-31 16:05:53 -08001885 case Instruction::APUT_OBJECT:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001886 HandlePutObject(mir);
Ian Rogersfc787ec2014-10-09 21:56:44 -07001887 FALLTHROUGH_INTENDED;
Vladimir Markof59f18b2014-02-17 15:53:57 +00001888 case Instruction::APUT:
1889 case Instruction::APUT_WIDE:
buzbee2502e002012-12-31 16:05:53 -08001890 case Instruction::APUT_BYTE:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001891 case Instruction::APUT_BOOLEAN:
1892 case Instruction::APUT_SHORT:
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001893 case Instruction::APUT_CHAR:
1894 HandleAPut(mir, opcode);
buzbee2502e002012-12-31 16:05:53 -08001895 break;
1896
1897 case Instruction::IGET_OBJECT:
buzbee2502e002012-12-31 16:05:53 -08001898 case Instruction::IGET:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001899 case Instruction::IGET_WIDE:
buzbee2502e002012-12-31 16:05:53 -08001900 case Instruction::IGET_BOOLEAN:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001901 case Instruction::IGET_BYTE:
1902 case Instruction::IGET_CHAR:
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001903 case Instruction::IGET_SHORT:
1904 res = HandleIGet(mir, opcode);
buzbee2502e002012-12-31 16:05:53 -08001905 break;
1906
buzbee2502e002012-12-31 16:05:53 -08001907 case Instruction::IPUT_OBJECT:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001908 HandlePutObject(mir);
Ian Rogersfc787ec2014-10-09 21:56:44 -07001909 FALLTHROUGH_INTENDED;
buzbee2502e002012-12-31 16:05:53 -08001910 case Instruction::IPUT:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001911 case Instruction::IPUT_WIDE:
buzbee2502e002012-12-31 16:05:53 -08001912 case Instruction::IPUT_BOOLEAN:
1913 case Instruction::IPUT_BYTE:
1914 case Instruction::IPUT_CHAR:
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001915 case Instruction::IPUT_SHORT:
1916 HandleIPut(mir, opcode);
buzbee2502e002012-12-31 16:05:53 -08001917 break;
1918
1919 case Instruction::SGET_OBJECT:
1920 case Instruction::SGET:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001921 case Instruction::SGET_WIDE:
buzbee2502e002012-12-31 16:05:53 -08001922 case Instruction::SGET_BOOLEAN:
1923 case Instruction::SGET_BYTE:
1924 case Instruction::SGET_CHAR:
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001925 case Instruction::SGET_SHORT:
1926 res = HandleSGet(mir, opcode);
buzbee2502e002012-12-31 16:05:53 -08001927 break;
1928
1929 case Instruction::SPUT_OBJECT:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001930 HandlePutObject(mir);
Ian Rogersfc787ec2014-10-09 21:56:44 -07001931 FALLTHROUGH_INTENDED;
buzbee2502e002012-12-31 16:05:53 -08001932 case Instruction::SPUT:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001933 case Instruction::SPUT_WIDE:
buzbee2502e002012-12-31 16:05:53 -08001934 case Instruction::SPUT_BOOLEAN:
1935 case Instruction::SPUT_BYTE:
1936 case Instruction::SPUT_CHAR:
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001937 case Instruction::SPUT_SHORT:
1938 HandleSPut(mir, opcode);
buzbee2502e002012-12-31 16:05:53 -08001939 break;
buzbee2502e002012-12-31 16:05:53 -08001940 }
1941 return res;
1942}
1943
1944} // namespace art