blob: e0c4e27b1991da1b830e20f13145658f86627568 [file] [log] [blame]
buzbee2502e002012-12-31 16:05:53 -08001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
buzbee311ca162013-02-28 15:56:43 -080017#include "local_value_numbering.h"
buzbee2502e002012-12-31 16:05:53 -080018
Vladimir Marko95a05972014-05-30 10:01:32 +010019#include "global_value_numbering.h"
Vladimir Markobe0e5462014-02-26 11:24:15 +000020#include "mir_field_info.h"
Vladimir Markof59f18b2014-02-17 15:53:57 +000021#include "mir_graph.h"
22
buzbee2502e002012-12-31 16:05:53 -080023namespace art {
24
Vladimir Marko2ac01fc2014-05-22 12:09:08 +010025namespace { // anonymous namespace
26
27// Operations used for value map keys instead of actual opcode.
Vladimir Marko95a05972014-05-30 10:01:32 +010028static constexpr uint16_t kInvokeMemoryVersionBumpOp = Instruction::INVOKE_VIRTUAL;
29static constexpr uint16_t kUnresolvedSFieldOp = Instruction::SGET;
30static constexpr uint16_t kResolvedSFieldOp = Instruction::SGET_WIDE;
31static constexpr uint16_t kUnresolvedIFieldOp = Instruction::IGET;
32static constexpr uint16_t kNonAliasingIFieldLocOp = Instruction::IGET_WIDE;
33static constexpr uint16_t kNonAliasingIFieldInitialOp = Instruction::IGET_OBJECT;
34static constexpr uint16_t kAliasingIFieldOp = Instruction::IGET_BOOLEAN;
35static constexpr uint16_t kAliasingIFieldStartVersionOp = Instruction::IGET_BYTE;
36static constexpr uint16_t kAliasingIFieldBumpVersionOp = Instruction::IGET_CHAR;
Vladimir Marko2ac01fc2014-05-22 12:09:08 +010037static constexpr uint16_t kNonAliasingArrayOp = Instruction::AGET;
38static constexpr uint16_t kNonAliasingArrayStartVersionOp = Instruction::AGET_WIDE;
Vladimir Marko95a05972014-05-30 10:01:32 +010039static constexpr uint16_t kNonAliasingArrayBumpVersionOp = Instruction::AGET_OBJECT;
40static constexpr uint16_t kAliasingArrayOp = Instruction::AGET_BOOLEAN;
41static constexpr uint16_t kAliasingArrayStartVersionOp = Instruction::AGET_BYTE;
42static constexpr uint16_t kAliasingArrayBumpVersionOp = Instruction::AGET_CHAR;
43static constexpr uint16_t kMergeBlockMemoryVersionBumpOp = Instruction::INVOKE_VIRTUAL_RANGE;
44static constexpr uint16_t kMergeBlockAliasingIFieldVersionBumpOp = Instruction::IPUT;
45static constexpr uint16_t kMergeBlockAliasingIFieldMergeLocationOp = Instruction::IPUT_WIDE;
46static constexpr uint16_t kMergeBlockNonAliasingArrayVersionBumpOp = Instruction::APUT;
47static constexpr uint16_t kMergeBlockNonAliasingArrayMergeLocationOp = Instruction::APUT_WIDE;
48static constexpr uint16_t kMergeBlockAliasingArrayVersionBumpOp = Instruction::APUT_OBJECT;
49static constexpr uint16_t kMergeBlockAliasingArrayMergeLocationOp = Instruction::APUT_BOOLEAN;
50static constexpr uint16_t kMergeBlockNonAliasingIFieldVersionBumpOp = Instruction::APUT_BYTE;
51static constexpr uint16_t kMergeBlockSFieldVersionBumpOp = Instruction::APUT_CHAR;
Vladimir Marko2ac01fc2014-05-22 12:09:08 +010052
53} // anonymous namespace
54
Vladimir Marko95a05972014-05-30 10:01:32 +010055class LocalValueNumbering::AliasingIFieldVersions {
56 public:
57 static uint16_t StartMemoryVersion(GlobalValueNumbering* gvn, const LocalValueNumbering* lvn,
58 uint16_t field_id) {
Vladimir Markoaf6925b2014-10-31 16:37:32 +000059 uint16_t type = gvn->GetIFieldType(field_id);
Vladimir Marko95a05972014-05-30 10:01:32 +010060 return gvn->LookupValue(kAliasingIFieldStartVersionOp, field_id,
61 lvn->global_memory_version_, lvn->unresolved_ifield_version_[type]);
62 }
63
64 static uint16_t BumpMemoryVersion(GlobalValueNumbering* gvn, uint16_t old_version,
65 uint16_t store_ref_set_id, uint16_t stored_value) {
66 return gvn->LookupValue(kAliasingIFieldBumpVersionOp, old_version,
67 store_ref_set_id, stored_value);
68 }
69
70 static uint16_t LookupGlobalValue(GlobalValueNumbering* gvn,
71 uint16_t field_id, uint16_t base, uint16_t memory_version) {
72 return gvn->LookupValue(kAliasingIFieldOp, field_id, base, memory_version);
73 }
74
75 static uint16_t LookupMergeValue(GlobalValueNumbering* gvn, const LocalValueNumbering* lvn,
76 uint16_t field_id, uint16_t base) {
77 // If the base/field_id is non-aliasing in lvn, use the non-aliasing value.
Vladimir Markoaf6925b2014-10-31 16:37:32 +000078 uint16_t type = gvn->GetIFieldType(field_id);
Vladimir Marko95a05972014-05-30 10:01:32 +010079 if (lvn->IsNonAliasingIField(base, field_id, type)) {
80 uint16_t loc = gvn->LookupValue(kNonAliasingIFieldLocOp, base, field_id, type);
81 auto lb = lvn->non_aliasing_ifield_value_map_.find(loc);
82 return (lb != lvn->non_aliasing_ifield_value_map_.end())
83 ? lb->second
84 : gvn->LookupValue(kNonAliasingIFieldInitialOp, loc, kNoValue, kNoValue);
85 }
86 return AliasingValuesMergeGet<AliasingIFieldVersions>(
87 gvn, lvn, &lvn->aliasing_ifield_value_map_, field_id, base);
88 }
89
90 static bool HasNewBaseVersion(GlobalValueNumbering* gvn, const LocalValueNumbering* lvn,
91 uint16_t field_id) {
Vladimir Markoaf6925b2014-10-31 16:37:32 +000092 uint16_t type = gvn->GetIFieldType(field_id);
Vladimir Marko95a05972014-05-30 10:01:32 +010093 return lvn->unresolved_ifield_version_[type] == lvn->merge_new_memory_version_ ||
94 lvn->global_memory_version_ == lvn->merge_new_memory_version_;
95 }
96
97 static uint16_t LookupMergeBlockValue(GlobalValueNumbering* gvn, uint16_t lvn_id,
98 uint16_t field_id) {
99 return gvn->LookupValue(kMergeBlockAliasingIFieldVersionBumpOp, field_id, kNoValue, lvn_id);
100 }
101
102 static uint16_t LookupMergeLocationValue(GlobalValueNumbering* gvn, uint16_t lvn_id,
103 uint16_t field_id, uint16_t base) {
104 return gvn->LookupValue(kMergeBlockAliasingIFieldMergeLocationOp, field_id, base, lvn_id);
105 }
106};
107
108class LocalValueNumbering::NonAliasingArrayVersions {
109 public:
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700110 static uint16_t StartMemoryVersion(GlobalValueNumbering* gvn,
111 const LocalValueNumbering* lvn ATTRIBUTE_UNUSED,
Vladimir Marko95a05972014-05-30 10:01:32 +0100112 uint16_t array) {
113 return gvn->LookupValue(kNonAliasingArrayStartVersionOp, array, kNoValue, kNoValue);
114 }
115
116 static uint16_t BumpMemoryVersion(GlobalValueNumbering* gvn, uint16_t old_version,
117 uint16_t store_ref_set_id, uint16_t stored_value) {
118 return gvn->LookupValue(kNonAliasingArrayBumpVersionOp, old_version,
119 store_ref_set_id, stored_value);
120 }
121
122 static uint16_t LookupGlobalValue(GlobalValueNumbering* gvn,
123 uint16_t array, uint16_t index, uint16_t memory_version) {
124 return gvn->LookupValue(kNonAliasingArrayOp, array, index, memory_version);
125 }
126
127 static uint16_t LookupMergeValue(GlobalValueNumbering* gvn, const LocalValueNumbering* lvn,
128 uint16_t array, uint16_t index) {
129 return AliasingValuesMergeGet<NonAliasingArrayVersions>(
130 gvn, lvn, &lvn->non_aliasing_array_value_map_, array, index);
131 }
132
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700133 static bool HasNewBaseVersion(GlobalValueNumbering* gvn ATTRIBUTE_UNUSED,
134 const LocalValueNumbering* lvn ATTRIBUTE_UNUSED,
135 uint16_t array ATTRIBUTE_UNUSED) {
Vladimir Marko95a05972014-05-30 10:01:32 +0100136 return false; // Not affected by global_memory_version_.
137 }
138
139 static uint16_t LookupMergeBlockValue(GlobalValueNumbering* gvn, uint16_t lvn_id,
140 uint16_t array) {
141 return gvn->LookupValue(kMergeBlockNonAliasingArrayVersionBumpOp, array, kNoValue, lvn_id);
142 }
143
144 static uint16_t LookupMergeLocationValue(GlobalValueNumbering* gvn, uint16_t lvn_id,
145 uint16_t array, uint16_t index) {
146 return gvn->LookupValue(kMergeBlockNonAliasingArrayMergeLocationOp, array, index, lvn_id);
147 }
148};
149
150class LocalValueNumbering::AliasingArrayVersions {
151 public:
152 static uint16_t StartMemoryVersion(GlobalValueNumbering* gvn, const LocalValueNumbering* lvn,
153 uint16_t type) {
154 return gvn->LookupValue(kAliasingArrayStartVersionOp, type, lvn->global_memory_version_,
155 kNoValue);
156 }
157
158 static uint16_t BumpMemoryVersion(GlobalValueNumbering* gvn, uint16_t old_version,
159 uint16_t store_ref_set_id, uint16_t stored_value) {
160 return gvn->LookupValue(kAliasingArrayBumpVersionOp, old_version,
161 store_ref_set_id, stored_value);
162 }
163
164 static uint16_t LookupGlobalValue(GlobalValueNumbering* gvn,
165 uint16_t type, uint16_t location, uint16_t memory_version) {
166 return gvn->LookupValue(kAliasingArrayOp, type, location, memory_version);
167 }
168
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700169 static uint16_t LookupMergeValue(GlobalValueNumbering* gvn ATTRIBUTE_UNUSED,
170 const LocalValueNumbering* lvn,
171 uint16_t type ATTRIBUTE_UNUSED, uint16_t location) {
Vladimir Marko95a05972014-05-30 10:01:32 +0100172 // If the location is non-aliasing in lvn, use the non-aliasing value.
173 uint16_t array = gvn->GetArrayLocationBase(location);
174 if (lvn->IsNonAliasingArray(array, type)) {
175 uint16_t index = gvn->GetArrayLocationIndex(location);
176 return NonAliasingArrayVersions::LookupMergeValue(gvn, lvn, array, index);
177 }
178 return AliasingValuesMergeGet<AliasingArrayVersions>(
179 gvn, lvn, &lvn->aliasing_array_value_map_, type, location);
180 }
181
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700182 static bool HasNewBaseVersion(GlobalValueNumbering* gvn ATTRIBUTE_UNUSED,
183 const LocalValueNumbering* lvn,
184 uint16_t type ATTRIBUTE_UNUSED) {
185 UNUSED(gvn);
186 UNUSED(type);
Vladimir Marko95a05972014-05-30 10:01:32 +0100187 return lvn->global_memory_version_ == lvn->merge_new_memory_version_;
188 }
189
190 static uint16_t LookupMergeBlockValue(GlobalValueNumbering* gvn, uint16_t lvn_id,
191 uint16_t type) {
192 return gvn->LookupValue(kMergeBlockAliasingArrayVersionBumpOp, type, kNoValue, lvn_id);
193 }
194
195 static uint16_t LookupMergeLocationValue(GlobalValueNumbering* gvn, uint16_t lvn_id,
196 uint16_t type, uint16_t location) {
197 return gvn->LookupValue(kMergeBlockAliasingArrayMergeLocationOp, type, location, lvn_id);
198 }
199};
200
201template <typename Map>
202LocalValueNumbering::AliasingValues* LocalValueNumbering::GetAliasingValues(
203 Map* map, const typename Map::key_type& key) {
204 auto lb = map->lower_bound(key);
205 if (lb == map->end() || map->key_comp()(key, lb->first)) {
Vladimir Markob19955d2014-07-29 12:04:10 +0100206 lb = map->PutBefore(lb, key, AliasingValues(this));
Vladimir Marko95a05972014-05-30 10:01:32 +0100207 }
208 return &lb->second;
Vladimir Marko2ac01fc2014-05-22 12:09:08 +0100209}
210
Vladimir Marko95a05972014-05-30 10:01:32 +0100211template <typename Versions, typename KeyType>
212void LocalValueNumbering::UpdateAliasingValuesLoadVersion(const KeyType& key,
213 AliasingValues* values) {
214 if (values->last_load_memory_version == kNoValue) {
215 // Get the start version that accounts for aliasing with unresolved fields of the same
216 // type and make it unique for the field by including the field_id.
217 uint16_t memory_version = values->memory_version_before_stores;
218 if (memory_version == kNoValue) {
219 memory_version = Versions::StartMemoryVersion(gvn_, this, key);
220 }
221 if (!values->store_loc_set.empty()) {
222 uint16_t ref_set_id = gvn_->GetRefSetId(values->store_loc_set);
223 memory_version = Versions::BumpMemoryVersion(gvn_, memory_version, ref_set_id,
224 values->last_stored_value);
225 }
226 values->last_load_memory_version = memory_version;
Vladimir Markof59f18b2014-02-17 15:53:57 +0000227 }
Vladimir Marko95a05972014-05-30 10:01:32 +0100228}
229
230template <typename Versions, typename Map>
231uint16_t LocalValueNumbering::AliasingValuesMergeGet(GlobalValueNumbering* gvn,
232 const LocalValueNumbering* lvn,
233 Map* map, const typename Map::key_type& key,
234 uint16_t location) {
235 // Retrieve the value name that we would get from
236 // const_cast<LocalValueNumbering*>(lvn)->HandleAliasingValueGet(map. key, location)
237 // but don't modify the map.
238 uint16_t value_name;
239 auto it = map->find(key);
240 if (it == map->end()) {
241 uint16_t start_version = Versions::StartMemoryVersion(gvn, lvn, key);
242 value_name = Versions::LookupGlobalValue(gvn, key, location, start_version);
243 } else if (it->second.store_loc_set.count(location) != 0u) {
244 value_name = it->second.last_stored_value;
245 } else {
246 auto load_it = it->second.load_value_map.find(location);
247 if (load_it != it->second.load_value_map.end()) {
248 value_name = load_it->second;
249 } else {
250 value_name = Versions::LookupGlobalValue(gvn, key, location, it->second.last_load_memory_version);
251 }
252 }
253 return value_name;
254}
255
256template <typename Versions, typename Map>
257uint16_t LocalValueNumbering::HandleAliasingValuesGet(Map* map, const typename Map::key_type& key,
258 uint16_t location) {
259 // Retrieve the value name for IGET/SGET/AGET, update the map with new value if any.
260 uint16_t res;
261 AliasingValues* values = GetAliasingValues(map, key);
262 if (values->store_loc_set.count(location) != 0u) {
263 res = values->last_stored_value;
264 } else {
265 UpdateAliasingValuesLoadVersion<Versions>(key, values);
266 auto lb = values->load_value_map.lower_bound(location);
267 if (lb != values->load_value_map.end() && lb->first == location) {
268 res = lb->second;
269 } else {
270 res = Versions::LookupGlobalValue(gvn_, key, location, values->last_load_memory_version);
271 values->load_value_map.PutBefore(lb, location, res);
272 }
273 }
274 return res;
275}
276
277template <typename Versions, typename Map>
278bool LocalValueNumbering::HandleAliasingValuesPut(Map* map, const typename Map::key_type& key,
279 uint16_t location, uint16_t value) {
280 AliasingValues* values = GetAliasingValues(map, key);
281 auto load_values_it = values->load_value_map.find(location);
282 if (load_values_it != values->load_value_map.end() && load_values_it->second == value) {
283 // This insn can be eliminated, it stores the same value that's already in the field.
284 return false;
285 }
286 if (value == values->last_stored_value) {
287 auto store_loc_lb = values->store_loc_set.lower_bound(location);
288 if (store_loc_lb != values->store_loc_set.end() && *store_loc_lb == location) {
289 // This insn can be eliminated, it stores the same value that's already in the field.
290 return false;
291 }
292 values->store_loc_set.emplace_hint(store_loc_lb, location);
293 } else {
294 UpdateAliasingValuesLoadVersion<Versions>(key, values);
295 values->memory_version_before_stores = values->last_load_memory_version;
296 values->last_stored_value = value;
297 values->store_loc_set.clear();
298 values->store_loc_set.insert(location);
299 }
300 // Clear the last load memory version and remove all potentially overwritten values.
301 values->last_load_memory_version = kNoValue;
302 auto it = values->load_value_map.begin(), end = values->load_value_map.end();
303 while (it != end) {
304 if (it->second == value) {
305 ++it;
306 } else {
307 it = values->load_value_map.erase(it);
308 }
309 }
310 return true;
311}
312
Vladimir Markob19955d2014-07-29 12:04:10 +0100313template <typename K>
314void LocalValueNumbering::CopyAliasingValuesMap(ScopedArenaSafeMap<K, AliasingValues>* dest,
315 const ScopedArenaSafeMap<K, AliasingValues>& src) {
316 // We need each new AliasingValues (or rather its map members) to be constructed
317 // with our allocator, rather than the allocator of the source.
318 for (const auto& entry : src) {
319 auto it = dest->PutBefore(dest->end(), entry.first, AliasingValues(this));
320 it->second = entry.second; // Map assignments preserve current allocator.
321 }
322}
323
324LocalValueNumbering::LocalValueNumbering(GlobalValueNumbering* gvn, uint16_t id,
325 ScopedArenaAllocator* allocator)
Vladimir Marko95a05972014-05-30 10:01:32 +0100326 : gvn_(gvn),
327 id_(id),
Vladimir Markob19955d2014-07-29 12:04:10 +0100328 sreg_value_map_(std::less<uint16_t>(), allocator->Adapter()),
329 sreg_wide_value_map_(std::less<uint16_t>(), allocator->Adapter()),
330 sfield_value_map_(std::less<uint16_t>(), allocator->Adapter()),
331 non_aliasing_ifield_value_map_(std::less<uint16_t>(), allocator->Adapter()),
332 aliasing_ifield_value_map_(std::less<uint16_t>(), allocator->Adapter()),
333 non_aliasing_array_value_map_(std::less<uint16_t>(), allocator->Adapter()),
334 aliasing_array_value_map_(std::less<uint16_t>(), allocator->Adapter()),
Vladimir Marko95a05972014-05-30 10:01:32 +0100335 global_memory_version_(0u),
Vladimir Markob19955d2014-07-29 12:04:10 +0100336 non_aliasing_refs_(std::less<uint16_t>(), allocator->Adapter()),
337 escaped_refs_(std::less<uint16_t>(), allocator->Adapter()),
338 escaped_ifield_clobber_set_(EscapedIFieldClobberKeyComparator(), allocator->Adapter()),
339 escaped_array_clobber_set_(EscapedArrayClobberKeyComparator(), allocator->Adapter()),
340 range_checked_(RangeCheckKeyComparator() , allocator->Adapter()),
341 null_checked_(std::less<uint16_t>(), allocator->Adapter()),
Razvan A Lupusorue0951142014-11-14 14:36:55 -0800342 div_zero_checked_(std::less<uint16_t>(), allocator->Adapter()),
Vladimir Markob19955d2014-07-29 12:04:10 +0100343 merge_names_(allocator->Adapter()),
344 merge_map_(std::less<ScopedArenaVector<BasicBlockId>>(), allocator->Adapter()),
Vladimir Marko95a05972014-05-30 10:01:32 +0100345 merge_new_memory_version_(kNoValue) {
Vladimir Marko321b9872014-11-24 16:33:51 +0000346 std::fill_n(unresolved_sfield_version_, arraysize(unresolved_sfield_version_), 0u);
347 std::fill_n(unresolved_ifield_version_, arraysize(unresolved_ifield_version_), 0u);
Vladimir Marko95a05972014-05-30 10:01:32 +0100348}
349
350bool LocalValueNumbering::Equals(const LocalValueNumbering& other) const {
351 DCHECK(gvn_ == other.gvn_);
352 // Compare the maps/sets and memory versions.
353 return sreg_value_map_ == other.sreg_value_map_ &&
354 sreg_wide_value_map_ == other.sreg_wide_value_map_ &&
355 sfield_value_map_ == other.sfield_value_map_ &&
356 non_aliasing_ifield_value_map_ == other.non_aliasing_ifield_value_map_ &&
357 aliasing_ifield_value_map_ == other.aliasing_ifield_value_map_ &&
358 non_aliasing_array_value_map_ == other.non_aliasing_array_value_map_ &&
359 aliasing_array_value_map_ == other.aliasing_array_value_map_ &&
360 SameMemoryVersion(other) &&
361 non_aliasing_refs_ == other.non_aliasing_refs_ &&
362 escaped_refs_ == other.escaped_refs_ &&
363 escaped_ifield_clobber_set_ == other.escaped_ifield_clobber_set_ &&
364 escaped_array_clobber_set_ == other.escaped_array_clobber_set_ &&
365 range_checked_ == other.range_checked_ &&
Razvan A Lupusorue0951142014-11-14 14:36:55 -0800366 null_checked_ == other.null_checked_ &&
367 div_zero_checked_ == other.div_zero_checked_;
Vladimir Marko95a05972014-05-30 10:01:32 +0100368}
369
370void LocalValueNumbering::MergeOne(const LocalValueNumbering& other, MergeType merge_type) {
Vladimir Markob19955d2014-07-29 12:04:10 +0100371 CopyLiveSregValues(&sreg_value_map_, other.sreg_value_map_);
372 CopyLiveSregValues(&sreg_wide_value_map_, other.sreg_wide_value_map_);
Vladimir Marko95a05972014-05-30 10:01:32 +0100373
374 if (merge_type == kReturnMerge) {
375 // RETURN or PHI+RETURN. We need only sreg value maps.
376 return;
377 }
378
379 non_aliasing_ifield_value_map_ = other.non_aliasing_ifield_value_map_;
Vladimir Markob19955d2014-07-29 12:04:10 +0100380 CopyAliasingValuesMap(&non_aliasing_array_value_map_, other.non_aliasing_array_value_map_);
Vladimir Marko95a05972014-05-30 10:01:32 +0100381 non_aliasing_refs_ = other.non_aliasing_refs_;
382 range_checked_ = other.range_checked_;
383 null_checked_ = other.null_checked_;
Razvan A Lupusorue0951142014-11-14 14:36:55 -0800384 div_zero_checked_ = other.div_zero_checked_;
Vladimir Marko95a05972014-05-30 10:01:32 +0100385
Vladimir Markoa4426cf2014-10-22 17:15:53 +0100386 const BasicBlock* pred_bb = gvn_->GetBasicBlock(other.Id());
387 if (GlobalValueNumbering::HasNullCheckLastInsn(pred_bb, Id())) {
388 int s_reg = pred_bb->last_mir_insn->ssa_rep->uses[0];
389 null_checked_.insert(other.GetOperandValue(s_reg));
390 }
391
Vladimir Marko95a05972014-05-30 10:01:32 +0100392 if (merge_type == kCatchMerge) {
393 // Memory is clobbered. Use new memory version and don't merge aliasing locations.
394 global_memory_version_ = NewMemoryVersion(&merge_new_memory_version_);
Vladimir Marko321b9872014-11-24 16:33:51 +0000395 std::fill_n(unresolved_sfield_version_, arraysize(unresolved_sfield_version_),
396 global_memory_version_);
397 std::fill_n(unresolved_ifield_version_, arraysize(unresolved_ifield_version_),
398 global_memory_version_);
Vladimir Marko95a05972014-05-30 10:01:32 +0100399 PruneNonAliasingRefsForCatch();
400 return;
401 }
402
403 DCHECK(merge_type == kNormalMerge);
404 global_memory_version_ = other.global_memory_version_;
Vladimir Marko321b9872014-11-24 16:33:51 +0000405 std::copy_n(other.unresolved_ifield_version_, arraysize(unresolved_sfield_version_),
406 unresolved_ifield_version_);
407 std::copy_n(other.unresolved_sfield_version_, arraysize(unresolved_ifield_version_),
408 unresolved_sfield_version_);
Vladimir Marko95a05972014-05-30 10:01:32 +0100409 sfield_value_map_ = other.sfield_value_map_;
Vladimir Markob19955d2014-07-29 12:04:10 +0100410 CopyAliasingValuesMap(&aliasing_ifield_value_map_, other.aliasing_ifield_value_map_);
411 CopyAliasingValuesMap(&aliasing_array_value_map_, other.aliasing_array_value_map_);
Vladimir Marko95a05972014-05-30 10:01:32 +0100412 escaped_refs_ = other.escaped_refs_;
413 escaped_ifield_clobber_set_ = other.escaped_ifield_clobber_set_;
414 escaped_array_clobber_set_ = other.escaped_array_clobber_set_;
415}
416
417bool LocalValueNumbering::SameMemoryVersion(const LocalValueNumbering& other) const {
418 return
419 global_memory_version_ == other.global_memory_version_ &&
Vladimir Marko321b9872014-11-24 16:33:51 +0000420 std::equal(unresolved_ifield_version_,
421 unresolved_ifield_version_ + arraysize(unresolved_ifield_version_),
Vladimir Marko95a05972014-05-30 10:01:32 +0100422 other.unresolved_ifield_version_) &&
Vladimir Marko321b9872014-11-24 16:33:51 +0000423 std::equal(unresolved_sfield_version_,
424 unresolved_sfield_version_ + arraysize(unresolved_sfield_version_),
Vladimir Marko95a05972014-05-30 10:01:32 +0100425 other.unresolved_sfield_version_);
426}
427
428uint16_t LocalValueNumbering::NewMemoryVersion(uint16_t* new_version) {
429 if (*new_version == kNoValue) {
430 *new_version = gvn_->LookupValue(kMergeBlockMemoryVersionBumpOp, 0u, 0u, id_);
431 }
432 return *new_version;
433}
434
435void LocalValueNumbering::MergeMemoryVersions(bool clobbered_catch) {
436 DCHECK_GE(gvn_->merge_lvns_.size(), 2u);
437 const LocalValueNumbering* cmp = gvn_->merge_lvns_[0];
438 // Check if the global version has changed.
439 bool new_global_version = clobbered_catch;
440 if (!new_global_version) {
441 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
442 if (lvn->global_memory_version_ != cmp->global_memory_version_) {
443 // Use a new version for everything.
444 new_global_version = true;
445 break;
446 }
447 }
448 }
449 if (new_global_version) {
450 global_memory_version_ = NewMemoryVersion(&merge_new_memory_version_);
Vladimir Marko321b9872014-11-24 16:33:51 +0000451 std::fill_n(unresolved_sfield_version_, arraysize(unresolved_sfield_version_),
452 merge_new_memory_version_);
453 std::fill_n(unresolved_ifield_version_, arraysize(unresolved_ifield_version_),
454 merge_new_memory_version_);
Vladimir Marko95a05972014-05-30 10:01:32 +0100455 } else {
456 // Initialize with a copy of memory versions from the comparison LVN.
457 global_memory_version_ = cmp->global_memory_version_;
Vladimir Marko321b9872014-11-24 16:33:51 +0000458 std::copy_n(cmp->unresolved_ifield_version_, arraysize(unresolved_sfield_version_),
459 unresolved_ifield_version_);
460 std::copy_n(cmp->unresolved_sfield_version_, arraysize(unresolved_ifield_version_),
461 unresolved_sfield_version_);
Vladimir Marko95a05972014-05-30 10:01:32 +0100462 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
463 if (lvn == cmp) {
464 continue;
465 }
Vladimir Marko321b9872014-11-24 16:33:51 +0000466 for (size_t i = 0; i != kDexMemAccessTypeCount; ++i) {
Vladimir Marko95a05972014-05-30 10:01:32 +0100467 if (lvn->unresolved_ifield_version_[i] != cmp->unresolved_ifield_version_[i]) {
468 unresolved_ifield_version_[i] = NewMemoryVersion(&merge_new_memory_version_);
469 }
470 if (lvn->unresolved_sfield_version_[i] != cmp->unresolved_sfield_version_[i]) {
471 unresolved_sfield_version_[i] = NewMemoryVersion(&merge_new_memory_version_);
472 }
473 }
474 }
475 }
476}
477
478void LocalValueNumbering::PruneNonAliasingRefsForCatch() {
479 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
480 const BasicBlock* bb = gvn_->GetBasicBlock(lvn->Id());
Vladimir Marko11ca6122014-07-17 20:50:07 +0100481 if (UNLIKELY(bb->taken == id_) || UNLIKELY(bb->fall_through == id_)) {
482 // Non-exceptional path to a catch handler means that the catch block was actually
483 // empty and all exceptional paths lead to the shared path after that empty block.
484 continue;
485 }
Vladimir Marko95a05972014-05-30 10:01:32 +0100486 DCHECK_EQ(bb->taken, kNullBlock);
487 DCHECK_NE(bb->fall_through, kNullBlock);
488 const BasicBlock* fall_through_bb = gvn_->GetBasicBlock(bb->fall_through);
489 const MIR* mir = fall_through_bb->first_mir_insn;
490 DCHECK(mir != nullptr);
491 // Only INVOKEs can leak and clobber non-aliasing references if they throw.
Jean Christophe Beylerfb0ea2d2014-07-29 13:20:42 -0700492 if ((mir->dalvikInsn.FlagsOf() & Instruction::kInvoke) != 0) {
Vladimir Markoa4426cf2014-10-22 17:15:53 +0100493 HandleInvokeArgs(mir, lvn);
Vladimir Marko95a05972014-05-30 10:01:32 +0100494 }
495 }
496}
497
498
499template <typename Set, Set LocalValueNumbering::* set_ptr>
500void LocalValueNumbering::IntersectSets() {
501 DCHECK_GE(gvn_->merge_lvns_.size(), 2u);
502
503 // Find the LVN with the least entries in the set.
504 const LocalValueNumbering* least_entries_lvn = gvn_->merge_lvns_[0];
505 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
506 if ((lvn->*set_ptr).size() < (least_entries_lvn->*set_ptr).size()) {
507 least_entries_lvn = lvn;
508 }
509 }
510
511 // For each key check if it's in all the LVNs.
512 for (const auto& key : least_entries_lvn->*set_ptr) {
513 bool checked = true;
514 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
515 if (lvn != least_entries_lvn && (lvn->*set_ptr).count(key) == 0u) {
516 checked = false;
517 break;
518 }
519 }
520 if (checked) {
521 (this->*set_ptr).emplace_hint((this->*set_ptr).end(), key);
522 }
523 }
524}
525
Vladimir Markob19955d2014-07-29 12:04:10 +0100526void LocalValueNumbering::CopyLiveSregValues(SregValueMap* dest, const SregValueMap& src) {
527 auto dest_end = dest->end();
528 ArenaBitVector* live_in_v = gvn_->GetMirGraph()->GetBasicBlock(id_)->data_flow_info->live_in_v;
529 DCHECK(live_in_v != nullptr);
530 for (const auto& entry : src) {
531 bool live = live_in_v->IsBitSet(gvn_->GetMirGraph()->SRegToVReg(entry.first));
532 if (live) {
533 dest->PutBefore(dest_end, entry.first, entry.second);
534 }
535 }
536}
537
538template <LocalValueNumbering::SregValueMap LocalValueNumbering::* map_ptr>
539void LocalValueNumbering::IntersectSregValueMaps() {
Vladimir Marko95a05972014-05-30 10:01:32 +0100540 DCHECK_GE(gvn_->merge_lvns_.size(), 2u);
541
542 // Find the LVN with the least entries in the set.
543 const LocalValueNumbering* least_entries_lvn = gvn_->merge_lvns_[0];
544 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
545 if ((lvn->*map_ptr).size() < (least_entries_lvn->*map_ptr).size()) {
546 least_entries_lvn = lvn;
547 }
548 }
549
550 // For each key check if it's in all the LVNs.
Vladimir Markob19955d2014-07-29 12:04:10 +0100551 ArenaBitVector* live_in_v = gvn_->GetMirGraph()->GetBasicBlock(id_)->data_flow_info->live_in_v;
552 DCHECK(live_in_v != nullptr);
Vladimir Marko95a05972014-05-30 10:01:32 +0100553 for (const auto& entry : least_entries_lvn->*map_ptr) {
Vladimir Markob19955d2014-07-29 12:04:10 +0100554 bool live_and_same = live_in_v->IsBitSet(gvn_->GetMirGraph()->SRegToVReg(entry.first));
555 if (live_and_same) {
556 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
557 if (lvn != least_entries_lvn) {
558 auto it = (lvn->*map_ptr).find(entry.first);
559 if (it == (lvn->*map_ptr).end() || !(it->second == entry.second)) {
560 live_and_same = false;
561 break;
562 }
Vladimir Marko95a05972014-05-30 10:01:32 +0100563 }
564 }
565 }
Vladimir Markob19955d2014-07-29 12:04:10 +0100566 if (live_and_same) {
Vladimir Marko95a05972014-05-30 10:01:32 +0100567 (this->*map_ptr).PutBefore((this->*map_ptr).end(), entry.first, entry.second);
568 }
569 }
570}
571
572// Intersect maps as sets. The value type must be equality-comparable.
573template <typename Map>
574void LocalValueNumbering::InPlaceIntersectMaps(Map* work_map, const Map& other_map) {
575 auto work_it = work_map->begin(), work_end = work_map->end();
576 auto cmp = work_map->value_comp();
577 for (const auto& entry : other_map) {
578 while (work_it != work_end &&
579 (cmp(*work_it, entry) ||
580 (!cmp(entry, *work_it) && !(work_it->second == entry.second)))) {
581 work_it = work_map->erase(work_it);
582 }
Vladimir Marko55fff042014-07-10 12:42:52 +0100583 if (work_it == work_end) {
584 return;
585 }
586 ++work_it;
Vladimir Marko95a05972014-05-30 10:01:32 +0100587 }
588}
589
590template <typename Set, Set LocalValueNumbering::*set_ptr, void (LocalValueNumbering::*MergeFn)(
591 const typename Set::value_type& entry, typename Set::iterator hint)>
592void LocalValueNumbering::MergeSets() {
593 auto cmp = (this->*set_ptr).value_comp();
594 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
595 auto my_it = (this->*set_ptr).begin(), my_end = (this->*set_ptr).end();
596 for (const auto& entry : lvn->*set_ptr) {
597 while (my_it != my_end && cmp(*my_it, entry)) {
598 ++my_it;
599 }
600 if (my_it != my_end && !cmp(entry, *my_it)) {
601 // Already handled.
602 ++my_it;
603 } else {
604 // Merge values for this field_id.
605 (this->*MergeFn)(entry, my_it); // my_it remains valid across inserts to std::set/SafeMap.
606 }
607 }
608 }
609}
610
611void LocalValueNumbering::IntersectAliasingValueLocations(AliasingValues* work_values,
612 const AliasingValues* values) {
613 auto cmp = work_values->load_value_map.key_comp();
614 auto work_it = work_values->load_value_map.begin(), work_end = work_values->load_value_map.end();
615 auto store_it = values->store_loc_set.begin(), store_end = values->store_loc_set.end();
616 auto load_it = values->load_value_map.begin(), load_end = values->load_value_map.end();
617 while (store_it != store_end || load_it != load_end) {
618 uint16_t loc;
619 if (store_it != store_end && (load_it == load_end || *store_it < load_it->first)) {
620 loc = *store_it;
621 ++store_it;
622 } else {
623 loc = load_it->first;
624 ++load_it;
625 DCHECK(store_it == store_end || cmp(loc, *store_it));
626 }
627 while (work_it != work_end && cmp(work_it->first, loc)) {
628 work_it = work_values->load_value_map.erase(work_it);
629 }
630 if (work_it != work_end && !cmp(loc, work_it->first)) {
631 // The location matches, keep it.
632 ++work_it;
633 }
634 }
635 while (work_it != work_end) {
636 work_it = work_values->load_value_map.erase(work_it);
637 }
638}
639
640void LocalValueNumbering::MergeEscapedRefs(const ValueNameSet::value_type& entry,
641 ValueNameSet::iterator hint) {
642 // See if the ref is either escaped or non-aliasing in each predecessor.
643 bool is_escaped = true;
644 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
645 if (lvn->non_aliasing_refs_.count(entry) == 0u &&
646 lvn->escaped_refs_.count(entry) == 0u) {
647 is_escaped = false;
648 break;
649 }
650 }
651 if (is_escaped) {
652 escaped_refs_.emplace_hint(hint, entry);
653 }
654}
655
656void LocalValueNumbering::MergeEscapedIFieldTypeClobberSets(
657 const EscapedIFieldClobberSet::value_type& entry, EscapedIFieldClobberSet::iterator hint) {
658 // Insert only type-clobber entries (field_id == kNoValue) of escaped refs.
659 if (entry.field_id == kNoValue && escaped_refs_.count(entry.base) != 0u) {
660 escaped_ifield_clobber_set_.emplace_hint(hint, entry);
661 }
662}
663
664void LocalValueNumbering::MergeEscapedIFieldClobberSets(
665 const EscapedIFieldClobberSet::value_type& entry, EscapedIFieldClobberSet::iterator hint) {
666 // Insert only those entries of escaped refs that are not overridden by a type clobber.
667 if (!(hint == escaped_ifield_clobber_set_.end() &&
668 hint->base == entry.base && hint->type == entry.type) &&
669 escaped_refs_.count(entry.base) != 0u) {
670 escaped_ifield_clobber_set_.emplace_hint(hint, entry);
671 }
672}
673
674void LocalValueNumbering::MergeEscapedArrayClobberSets(
675 const EscapedArrayClobberSet::value_type& entry, EscapedArrayClobberSet::iterator hint) {
676 if (escaped_refs_.count(entry.base) != 0u) {
677 escaped_array_clobber_set_.emplace_hint(hint, entry);
678 }
679}
680
Vladimir Marko2d2365c2014-08-19 18:08:39 +0100681void LocalValueNumbering::MergeNullChecked() {
682 DCHECK_GE(gvn_->merge_lvns_.size(), 2u);
683
684 // Find the LVN with the least entries in the set.
685 const LocalValueNumbering* least_entries_lvn = gvn_->merge_lvns_[0];
686 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
687 if (lvn->null_checked_.size() < least_entries_lvn->null_checked_.size()) {
688 least_entries_lvn = lvn;
689 }
690 }
691
692 // For each null-checked value name check if it's null-checked in all the LVNs.
693 for (const auto& value_name : least_entries_lvn->null_checked_) {
694 // Merge null_checked_ for this ref.
695 merge_names_.clear();
696 merge_names_.resize(gvn_->merge_lvns_.size(), value_name);
697 if (gvn_->NullCheckedInAllPredecessors(merge_names_)) {
698 null_checked_.insert(null_checked_.end(), value_name);
699 }
700 }
701
702 // Now check if the least_entries_lvn has a null-check as the last insn.
703 const BasicBlock* least_entries_bb = gvn_->GetBasicBlock(least_entries_lvn->Id());
704 if (gvn_->HasNullCheckLastInsn(least_entries_bb, id_)) {
705 int s_reg = least_entries_bb->last_mir_insn->ssa_rep->uses[0];
Vladimir Markoa4426cf2014-10-22 17:15:53 +0100706 uint32_t value_name = least_entries_lvn->GetOperandValue(s_reg);
Vladimir Marko2d2365c2014-08-19 18:08:39 +0100707 merge_names_.clear();
708 merge_names_.resize(gvn_->merge_lvns_.size(), value_name);
709 if (gvn_->NullCheckedInAllPredecessors(merge_names_)) {
710 null_checked_.insert(value_name);
711 }
Vladimir Marko95a05972014-05-30 10:01:32 +0100712 }
713}
714
Razvan A Lupusorue0951142014-11-14 14:36:55 -0800715void LocalValueNumbering::MergeDivZeroChecked() {
716 DCHECK_GE(gvn_->merge_lvns_.size(), 2u);
717
718 // Find the LVN with the least entries in the set.
719 const LocalValueNumbering* least_entries_lvn = gvn_->merge_lvns_[0];
720 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
721 if (lvn->div_zero_checked_.size() < least_entries_lvn->div_zero_checked_.size()) {
722 least_entries_lvn = lvn;
723 }
724 }
725
726 // For each div-zero value name check if it's div-zero checked in all the LVNs.
727 for (const auto& value_name : least_entries_lvn->div_zero_checked_) {
728 // Merge null_checked_ for this ref.
729 merge_names_.clear();
730 merge_names_.resize(gvn_->merge_lvns_.size(), value_name);
731 if (gvn_->DivZeroCheckedInAllPredecessors(merge_names_)) {
732 div_zero_checked_.insert(div_zero_checked_.end(), value_name);
733 }
734 }
735}
736
Vladimir Marko95a05972014-05-30 10:01:32 +0100737void LocalValueNumbering::MergeSFieldValues(const SFieldToValueMap::value_type& entry,
738 SFieldToValueMap::iterator hint) {
739 uint16_t field_id = entry.first;
740 merge_names_.clear();
741 uint16_t value_name = kNoValue;
742 bool same_values = true;
743 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
744 // Get the value name as in HandleSGet() but don't modify *lvn.
745 auto it = lvn->sfield_value_map_.find(field_id);
746 if (it != lvn->sfield_value_map_.end()) {
747 value_name = it->second;
748 } else {
Vladimir Markoaf6925b2014-10-31 16:37:32 +0000749 uint16_t type = gvn_->GetSFieldType(field_id);
Vladimir Marko95a05972014-05-30 10:01:32 +0100750 value_name = gvn_->LookupValue(kResolvedSFieldOp, field_id,
751 lvn->unresolved_sfield_version_[type],
752 lvn->global_memory_version_);
753 }
754
755 same_values = same_values && (merge_names_.empty() || value_name == merge_names_.back());
756 merge_names_.push_back(value_name);
757 }
758 if (same_values) {
759 // value_name already contains the result.
760 } else {
761 auto lb = merge_map_.lower_bound(merge_names_);
762 if (lb != merge_map_.end() && !merge_map_.key_comp()(merge_names_, lb->first)) {
763 value_name = lb->second;
764 } else {
765 value_name = gvn_->LookupValue(kMergeBlockSFieldVersionBumpOp, field_id, id_, kNoValue);
766 merge_map_.PutBefore(lb, merge_names_, value_name);
767 if (gvn_->NullCheckedInAllPredecessors(merge_names_)) {
768 null_checked_.insert(value_name);
769 }
770 }
771 }
772 sfield_value_map_.PutBefore(hint, field_id, value_name);
773}
774
775void LocalValueNumbering::MergeNonAliasingIFieldValues(const IFieldLocToValueMap::value_type& entry,
776 IFieldLocToValueMap::iterator hint) {
777 uint16_t field_loc = entry.first;
778 merge_names_.clear();
779 uint16_t value_name = kNoValue;
780 bool same_values = true;
781 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
782 // Get the value name as in HandleIGet() but don't modify *lvn.
783 auto it = lvn->non_aliasing_ifield_value_map_.find(field_loc);
784 if (it != lvn->non_aliasing_ifield_value_map_.end()) {
785 value_name = it->second;
786 } else {
787 value_name = gvn_->LookupValue(kNonAliasingIFieldInitialOp, field_loc, kNoValue, kNoValue);
788 }
789
790 same_values = same_values && (merge_names_.empty() || value_name == merge_names_.back());
791 merge_names_.push_back(value_name);
792 }
793 if (same_values) {
794 // value_name already contains the result.
795 } else {
796 auto lb = merge_map_.lower_bound(merge_names_);
797 if (lb != merge_map_.end() && !merge_map_.key_comp()(merge_names_, lb->first)) {
798 value_name = lb->second;
799 } else {
800 value_name = gvn_->LookupValue(kMergeBlockNonAliasingIFieldVersionBumpOp, field_loc,
801 id_, kNoValue);
802 merge_map_.PutBefore(lb, merge_names_, value_name);
803 if (gvn_->NullCheckedInAllPredecessors(merge_names_)) {
804 null_checked_.insert(value_name);
805 }
806 }
807 }
808 non_aliasing_ifield_value_map_.PutBefore(hint, field_loc, value_name);
809}
810
811template <typename Map, Map LocalValueNumbering::*map_ptr, typename Versions>
812void LocalValueNumbering::MergeAliasingValues(const typename Map::value_type& entry,
813 typename Map::iterator hint) {
814 const typename Map::key_type& key = entry.first;
815
Vladimir Markob19955d2014-07-29 12:04:10 +0100816 auto it = (this->*map_ptr).PutBefore(hint, key, AliasingValues(this));
Vladimir Marko95a05972014-05-30 10:01:32 +0100817 AliasingValues* my_values = &it->second;
818
819 const AliasingValues* cmp_values = nullptr;
820 bool same_version = !Versions::HasNewBaseVersion(gvn_, this, key);
821 uint16_t load_memory_version_for_same_version = kNoValue;
822 if (same_version) {
823 // Find the first non-null values.
824 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800825 auto value = (lvn->*map_ptr).find(key);
826 if (value != (lvn->*map_ptr).end()) {
827 cmp_values = &value->second;
Vladimir Marko95a05972014-05-30 10:01:32 +0100828 break;
829 }
830 }
831 DCHECK(cmp_values != nullptr); // There must be at least one non-null values.
832
833 // Check if we have identical memory versions, i.e. the global memory version, unresolved
834 // field version and the values' memory_version_before_stores, last_stored_value
835 // and store_loc_set are identical.
836 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800837 auto value = (lvn->*map_ptr).find(key);
838 if (value == (lvn->*map_ptr).end()) {
Vladimir Marko95a05972014-05-30 10:01:32 +0100839 if (cmp_values->memory_version_before_stores != kNoValue) {
840 same_version = false;
841 break;
842 }
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800843 } else if (cmp_values->last_stored_value != value->second.last_stored_value ||
844 cmp_values->memory_version_before_stores != value->second.memory_version_before_stores ||
845 cmp_values->store_loc_set != value->second.store_loc_set) {
Vladimir Marko95a05972014-05-30 10:01:32 +0100846 same_version = false;
847 break;
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800848 } else if (value->second.last_load_memory_version != kNoValue) {
Vladimir Marko95a05972014-05-30 10:01:32 +0100849 DCHECK(load_memory_version_for_same_version == kNoValue ||
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800850 load_memory_version_for_same_version == value->second.last_load_memory_version);
851 load_memory_version_for_same_version = value->second.last_load_memory_version;
Vladimir Marko95a05972014-05-30 10:01:32 +0100852 }
853 }
854 }
855
856 if (same_version) {
857 // Copy the identical values.
858 my_values->memory_version_before_stores = cmp_values->memory_version_before_stores;
859 my_values->last_stored_value = cmp_values->last_stored_value;
860 my_values->store_loc_set = cmp_values->store_loc_set;
861 my_values->last_load_memory_version = load_memory_version_for_same_version;
862 // Merge load values seen in all incoming arcs (i.e. an intersection).
863 if (!cmp_values->load_value_map.empty()) {
864 my_values->load_value_map = cmp_values->load_value_map;
865 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800866 auto value = (lvn->*map_ptr).find(key);
867 if (value == (lvn->*map_ptr).end() || value->second.load_value_map.empty()) {
Vladimir Marko95a05972014-05-30 10:01:32 +0100868 my_values->load_value_map.clear();
869 break;
870 }
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800871 InPlaceIntersectMaps(&my_values->load_value_map, value->second.load_value_map);
Vladimir Marko95a05972014-05-30 10:01:32 +0100872 if (my_values->load_value_map.empty()) {
873 break;
874 }
875 }
876 }
877 } else {
878 // Bump version number for the merge.
879 my_values->memory_version_before_stores = my_values->last_load_memory_version =
880 Versions::LookupMergeBlockValue(gvn_, id_, key);
881
882 // Calculate the locations that have been either read from or written to in each incoming LVN.
883 bool first_lvn = true;
884 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800885 auto value = (lvn->*map_ptr).find(key);
886 if (value == (lvn->*map_ptr).end()) {
Vladimir Marko95a05972014-05-30 10:01:32 +0100887 my_values->load_value_map.clear();
888 break;
889 }
890 if (first_lvn) {
891 first_lvn = false;
892 // Copy the first LVN's locations. Values will be overwritten later.
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800893 my_values->load_value_map = value->second.load_value_map;
894 for (uint16_t location : value->second.store_loc_set) {
Vladimir Marko95a05972014-05-30 10:01:32 +0100895 my_values->load_value_map.Put(location, 0u);
896 }
897 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800898 IntersectAliasingValueLocations(my_values, &value->second);
Vladimir Marko95a05972014-05-30 10:01:32 +0100899 }
900 }
901 // Calculate merged values for the intersection.
902 for (auto& load_value_entry : my_values->load_value_map) {
903 uint16_t location = load_value_entry.first;
904 bool same_values = true;
905 uint16_t value_name = kNoValue;
906 merge_names_.clear();
907 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
908 value_name = Versions::LookupMergeValue(gvn_, lvn, key, location);
909 same_values = same_values && (merge_names_.empty() || value_name == merge_names_.back());
910 merge_names_.push_back(value_name);
911 }
912 if (same_values) {
913 // value_name already contains the result.
914 } else {
915 auto lb = merge_map_.lower_bound(merge_names_);
916 if (lb != merge_map_.end() && !merge_map_.key_comp()(merge_names_, lb->first)) {
917 value_name = lb->second;
918 } else {
919 // NOTE: In addition to the key and id_ which don't change on an LVN recalculation
920 // during GVN, we also add location which can actually change on recalculation, so the
921 // value_name below may change. This could lead to an infinite loop if the location
922 // value name always changed when the refereced value name changes. However, given that
923 // we assign unique value names for other merges, such as Phis, such a dependency is
924 // not possible in a well-formed SSA graph.
925 value_name = Versions::LookupMergeLocationValue(gvn_, id_, key, location);
926 merge_map_.PutBefore(lb, merge_names_, value_name);
927 if (gvn_->NullCheckedInAllPredecessors(merge_names_)) {
928 null_checked_.insert(value_name);
929 }
930 }
931 }
932 load_value_entry.second = value_name;
933 }
934 }
935}
936
937void LocalValueNumbering::Merge(MergeType merge_type) {
938 DCHECK_GE(gvn_->merge_lvns_.size(), 2u);
939
Vladimir Markob19955d2014-07-29 12:04:10 +0100940 IntersectSregValueMaps<&LocalValueNumbering::sreg_value_map_>();
941 IntersectSregValueMaps<&LocalValueNumbering::sreg_wide_value_map_>();
Vladimir Marko95a05972014-05-30 10:01:32 +0100942 if (merge_type == kReturnMerge) {
943 // RETURN or PHI+RETURN. We need only sreg value maps.
944 return;
945 }
946
947 MergeMemoryVersions(merge_type == kCatchMerge);
948
949 // Merge non-aliasing maps/sets.
Vladimir Marko95a05972014-05-30 10:01:32 +0100950 IntersectSets<ValueNameSet, &LocalValueNumbering::non_aliasing_refs_>();
Vladimir Marko55fff042014-07-10 12:42:52 +0100951 if (!non_aliasing_refs_.empty() && merge_type == kCatchMerge) {
952 PruneNonAliasingRefsForCatch();
953 }
954 if (!non_aliasing_refs_.empty()) {
955 MergeSets<IFieldLocToValueMap, &LocalValueNumbering::non_aliasing_ifield_value_map_,
956 &LocalValueNumbering::MergeNonAliasingIFieldValues>();
957 MergeSets<NonAliasingArrayValuesMap, &LocalValueNumbering::non_aliasing_array_value_map_,
958 &LocalValueNumbering::MergeAliasingValues<
959 NonAliasingArrayValuesMap, &LocalValueNumbering::non_aliasing_array_value_map_,
960 NonAliasingArrayVersions>>();
961 }
Vladimir Marko95a05972014-05-30 10:01:32 +0100962
963 // We won't do anything complicated for range checks, just calculate the intersection.
964 IntersectSets<RangeCheckSet, &LocalValueNumbering::range_checked_>();
965
966 // Merge null_checked_. We may later insert more, such as merged object field values.
Vladimir Marko2d2365c2014-08-19 18:08:39 +0100967 MergeNullChecked();
Vladimir Marko95a05972014-05-30 10:01:32 +0100968
Razvan A Lupusorue0951142014-11-14 14:36:55 -0800969 // Now merge the div_zero_checked_.
970 MergeDivZeroChecked();
971
Vladimir Marko95a05972014-05-30 10:01:32 +0100972 if (merge_type == kCatchMerge) {
973 // Memory is clobbered. New memory version already created, don't merge aliasing locations.
Vladimir Marko95a05972014-05-30 10:01:32 +0100974 return;
975 }
976
977 DCHECK(merge_type == kNormalMerge);
978
979 // Merge escaped refs and clobber sets.
980 MergeSets<ValueNameSet, &LocalValueNumbering::escaped_refs_,
981 &LocalValueNumbering::MergeEscapedRefs>();
982 if (!escaped_refs_.empty()) {
983 MergeSets<EscapedIFieldClobberSet, &LocalValueNumbering::escaped_ifield_clobber_set_,
984 &LocalValueNumbering::MergeEscapedIFieldTypeClobberSets>();
985 MergeSets<EscapedIFieldClobberSet, &LocalValueNumbering::escaped_ifield_clobber_set_,
986 &LocalValueNumbering::MergeEscapedIFieldClobberSets>();
987 MergeSets<EscapedArrayClobberSet, &LocalValueNumbering::escaped_array_clobber_set_,
988 &LocalValueNumbering::MergeEscapedArrayClobberSets>();
989 }
990
991 MergeSets<SFieldToValueMap, &LocalValueNumbering::sfield_value_map_,
992 &LocalValueNumbering::MergeSFieldValues>();
993 MergeSets<AliasingIFieldValuesMap, &LocalValueNumbering::aliasing_ifield_value_map_,
994 &LocalValueNumbering::MergeAliasingValues<
995 AliasingIFieldValuesMap, &LocalValueNumbering::aliasing_ifield_value_map_,
996 AliasingIFieldVersions>>();
997 MergeSets<AliasingArrayValuesMap, &LocalValueNumbering::aliasing_array_value_map_,
998 &LocalValueNumbering::MergeAliasingValues<
999 AliasingArrayValuesMap, &LocalValueNumbering::aliasing_array_value_map_,
1000 AliasingArrayVersions>>();
Vladimir Markof59f18b2014-02-17 15:53:57 +00001001}
1002
Vladimir Markoa4426cf2014-10-22 17:15:53 +01001003void LocalValueNumbering::PrepareEntryBlock() {
1004 uint32_t vreg = gvn_->GetMirGraph()->GetFirstInVR();
1005 CompilationUnit* cu = gvn_->GetCompilationUnit();
1006 const char* shorty = cu->shorty;
1007 ++shorty; // Skip return value.
1008 if ((cu->access_flags & kAccStatic) == 0) {
1009 // If non-static method, mark "this" as non-null
1010 uint16_t value_name = GetOperandValue(vreg);
1011 ++vreg;
1012 null_checked_.insert(value_name);
1013 }
1014 for ( ; *shorty != 0; ++shorty, ++vreg) {
1015 if (*shorty == 'J' || *shorty == 'D') {
1016 uint16_t value_name = GetOperandValueWide(vreg);
1017 SetOperandValueWide(vreg, value_name);
1018 ++vreg;
1019 }
1020 }
1021}
1022
Vladimir Markof59f18b2014-02-17 15:53:57 +00001023uint16_t LocalValueNumbering::MarkNonAliasingNonNull(MIR* mir) {
1024 uint16_t res = GetOperandValue(mir->ssa_rep->defs[0]);
Vladimir Markof59f18b2014-02-17 15:53:57 +00001025 DCHECK(null_checked_.find(res) == null_checked_.end());
1026 null_checked_.insert(res);
1027 non_aliasing_refs_.insert(res);
1028 return res;
1029}
1030
Vladimir Marko95a05972014-05-30 10:01:32 +01001031bool LocalValueNumbering::IsNonAliasing(uint16_t reg) const {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001032 return non_aliasing_refs_.find(reg) != non_aliasing_refs_.end();
Vladimir Markof59f18b2014-02-17 15:53:57 +00001033}
1034
Vladimir Marko95a05972014-05-30 10:01:32 +01001035bool LocalValueNumbering::IsNonAliasingIField(uint16_t reg, uint16_t field_id,
1036 uint16_t type) const {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001037 if (IsNonAliasing(reg)) {
1038 return true;
1039 }
Vladimir Marko95a05972014-05-30 10:01:32 +01001040 if (escaped_refs_.find(reg) == escaped_refs_.end()) {
1041 return false;
1042 }
1043 // Check for IPUTs to unresolved fields.
1044 EscapedIFieldClobberKey key1 = { reg, type, kNoValue };
1045 if (escaped_ifield_clobber_set_.find(key1) != escaped_ifield_clobber_set_.end()) {
1046 return false;
1047 }
1048 // Check for aliased IPUTs to the same field.
1049 EscapedIFieldClobberKey key2 = { reg, type, field_id };
1050 return escaped_ifield_clobber_set_.find(key2) == escaped_ifield_clobber_set_.end();
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001051}
1052
Vladimir Marko95a05972014-05-30 10:01:32 +01001053bool LocalValueNumbering::IsNonAliasingArray(uint16_t reg, uint16_t type) const {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001054 if (IsNonAliasing(reg)) {
1055 return true;
1056 }
Vladimir Marko95a05972014-05-30 10:01:32 +01001057 if (escaped_refs_.count(reg) == 0u) {
1058 return false;
1059 }
1060 // Check for aliased APUTs.
1061 EscapedArrayClobberKey key = { reg, type };
1062 return escaped_array_clobber_set_.find(key) == escaped_array_clobber_set_.end();
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001063}
1064
Vladimir Markof59f18b2014-02-17 15:53:57 +00001065void LocalValueNumbering::HandleNullCheck(MIR* mir, uint16_t reg) {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001066 auto lb = null_checked_.lower_bound(reg);
1067 if (lb != null_checked_.end() && *lb == reg) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001068 if (LIKELY(gvn_->CanModify())) {
1069 if (gvn_->GetCompilationUnit()->verbose) {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001070 LOG(INFO) << "Removing null check for 0x" << std::hex << mir->offset;
1071 }
1072 mir->optimization_flags |= MIR_IGNORE_NULL_CHECK;
Vladimir Markof59f18b2014-02-17 15:53:57 +00001073 }
Vladimir Markof59f18b2014-02-17 15:53:57 +00001074 } else {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001075 null_checked_.insert(lb, reg);
Vladimir Markof59f18b2014-02-17 15:53:57 +00001076 }
1077}
1078
1079void LocalValueNumbering::HandleRangeCheck(MIR* mir, uint16_t array, uint16_t index) {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001080 RangeCheckKey key = { array, index };
1081 auto lb = range_checked_.lower_bound(key);
1082 if (lb != range_checked_.end() && !RangeCheckKeyComparator()(key, *lb)) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001083 if (LIKELY(gvn_->CanModify())) {
1084 if (gvn_->GetCompilationUnit()->verbose) {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001085 LOG(INFO) << "Removing range check for 0x" << std::hex << mir->offset;
1086 }
1087 mir->optimization_flags |= MIR_IGNORE_RANGE_CHECK;
Vladimir Markof59f18b2014-02-17 15:53:57 +00001088 }
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001089 } else {
1090 // Mark range check completed.
1091 range_checked_.insert(lb, key);
Vladimir Markof59f18b2014-02-17 15:53:57 +00001092 }
Vladimir Markof59f18b2014-02-17 15:53:57 +00001093}
1094
Razvan A Lupusorue0951142014-11-14 14:36:55 -08001095void LocalValueNumbering::HandleDivZeroCheck(MIR* mir, uint16_t reg) {
1096 auto lb = div_zero_checked_.lower_bound(reg);
1097 if (lb != div_zero_checked_.end() && *lb == reg) {
1098 if (LIKELY(gvn_->CanModify())) {
1099 if (gvn_->GetCompilationUnit()->verbose) {
1100 LOG(INFO) << "Removing div zero check for 0x" << std::hex << mir->offset;
1101 }
1102 mir->optimization_flags |= MIR_IGNORE_DIV_ZERO_CHECK;
1103 }
1104 } else {
1105 div_zero_checked_.insert(lb, reg);
1106 }
1107}
1108
Vladimir Markof59f18b2014-02-17 15:53:57 +00001109void LocalValueNumbering::HandlePutObject(MIR* mir) {
1110 // If we're storing a non-aliasing reference, stop tracking it as non-aliasing now.
1111 uint16_t base = GetOperandValue(mir->ssa_rep->uses[0]);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001112 HandleEscapingRef(base);
1113}
1114
1115void LocalValueNumbering::HandleEscapingRef(uint16_t base) {
1116 auto it = non_aliasing_refs_.find(base);
1117 if (it != non_aliasing_refs_.end()) {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001118 non_aliasing_refs_.erase(it);
Vladimir Marko95a05972014-05-30 10:01:32 +01001119 escaped_refs_.insert(base);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001120 }
1121}
1122
Vladimir Markoa4426cf2014-10-22 17:15:53 +01001123void LocalValueNumbering::HandleInvokeArgs(const MIR* mir, const LocalValueNumbering* mir_lvn) {
1124 const int32_t* uses = mir->ssa_rep->uses;
1125 const int32_t* uses_end = uses + mir->ssa_rep->num_uses;
1126 while (uses != uses_end) {
1127 uint16_t sreg = *uses;
1128 ++uses;
1129 // Avoid LookupValue() so that we don't store new values in the global value map.
1130 auto local_it = mir_lvn->sreg_value_map_.find(sreg);
1131 if (local_it != mir_lvn->sreg_value_map_.end()) {
1132 non_aliasing_refs_.erase(local_it->second);
1133 } else {
1134 uint16_t value_name = gvn_->FindValue(kNoValue, sreg, kNoValue, kNoValue);
1135 if (value_name != kNoValue) {
1136 non_aliasing_refs_.erase(value_name);
1137 }
1138 }
1139 }
1140}
1141
Vladimir Marko95a05972014-05-30 10:01:32 +01001142uint16_t LocalValueNumbering::HandlePhi(MIR* mir) {
1143 if (gvn_->merge_lvns_.empty()) {
1144 // Running LVN without a full GVN?
1145 return kNoValue;
1146 }
Vladimir Marko95a05972014-05-30 10:01:32 +01001147 int32_t* uses = mir->ssa_rep->uses;
1148 // Try to find out if this is merging wide regs.
1149 if (mir->ssa_rep->defs[0] != 0 &&
1150 sreg_wide_value_map_.count(mir->ssa_rep->defs[0] - 1) != 0u) {
1151 // This is the high part of a wide reg. Ignore the Phi.
1152 return kNoValue;
1153 }
Vladimir Markoa4426cf2014-10-22 17:15:53 +01001154 BasicBlockId* incoming = mir->meta.phi_incoming;
1155 int16_t pos = 0;
1156 // Check if we're merging a wide value based on the first merged LVN.
1157 const LocalValueNumbering* first_lvn = gvn_->merge_lvns_[0];
1158 DCHECK_LT(pos, mir->ssa_rep->num_uses);
1159 while (incoming[pos] != first_lvn->Id()) {
1160 ++pos;
1161 DCHECK_LT(pos, mir->ssa_rep->num_uses);
Vladimir Marko95a05972014-05-30 10:01:32 +01001162 }
Vladimir Markoa4426cf2014-10-22 17:15:53 +01001163 int first_s_reg = uses[pos];
1164 bool wide = (first_lvn->sreg_wide_value_map_.count(first_s_reg) != 0u);
Vladimir Marko95a05972014-05-30 10:01:32 +01001165 // Iterate over *merge_lvns_ and skip incoming sregs for BBs without associated LVN.
1166 uint16_t value_name = kNoValue;
1167 merge_names_.clear();
Vladimir Marko95a05972014-05-30 10:01:32 +01001168 bool same_values = true;
1169 for (const LocalValueNumbering* lvn : gvn_->merge_lvns_) {
1170 DCHECK_LT(pos, mir->ssa_rep->num_uses);
1171 while (incoming[pos] != lvn->Id()) {
1172 ++pos;
1173 DCHECK_LT(pos, mir->ssa_rep->num_uses);
1174 }
1175 int s_reg = uses[pos];
1176 ++pos;
1177 value_name = wide ? lvn->GetOperandValueWide(s_reg) : lvn->GetOperandValue(s_reg);
1178
1179 same_values = same_values && (merge_names_.empty() || value_name == merge_names_.back());
1180 merge_names_.push_back(value_name);
1181 }
1182 if (same_values) {
1183 // value_name already contains the result.
1184 } else {
1185 auto lb = merge_map_.lower_bound(merge_names_);
1186 if (lb != merge_map_.end() && !merge_map_.key_comp()(merge_names_, lb->first)) {
1187 value_name = lb->second;
1188 } else {
1189 value_name = gvn_->LookupValue(kNoValue, mir->ssa_rep->defs[0], kNoValue, kNoValue);
1190 merge_map_.PutBefore(lb, merge_names_, value_name);
1191 if (!wide && gvn_->NullCheckedInAllPredecessors(merge_names_)) {
1192 null_checked_.insert(value_name);
1193 }
Razvan A Lupusorue0951142014-11-14 14:36:55 -08001194 if (gvn_->DivZeroCheckedInAllPredecessors(merge_names_)) {
1195 div_zero_checked_.insert(value_name);
1196 }
Vladimir Marko95a05972014-05-30 10:01:32 +01001197 }
1198 }
1199 if (wide) {
1200 SetOperandValueWide(mir->ssa_rep->defs[0], value_name);
1201 } else {
1202 SetOperandValue(mir->ssa_rep->defs[0], value_name);
1203 }
1204 return value_name;
1205}
1206
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001207uint16_t LocalValueNumbering::HandleAGet(MIR* mir, uint16_t opcode) {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001208 uint16_t array = GetOperandValue(mir->ssa_rep->uses[0]);
1209 HandleNullCheck(mir, array);
1210 uint16_t index = GetOperandValue(mir->ssa_rep->uses[1]);
1211 HandleRangeCheck(mir, array, index);
Vladimir Markoaf6925b2014-10-31 16:37:32 +00001212 uint16_t type = AGetMemAccessType(static_cast<Instruction::Code>(opcode));
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001213 // Establish value number for loaded register.
1214 uint16_t res;
1215 if (IsNonAliasingArray(array, type)) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001216 res = HandleAliasingValuesGet<NonAliasingArrayVersions>(&non_aliasing_array_value_map_,
1217 array, index);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001218 } else {
Vladimir Marko95a05972014-05-30 10:01:32 +01001219 uint16_t location = gvn_->GetArrayLocation(array, index);
1220 res = HandleAliasingValuesGet<AliasingArrayVersions>(&aliasing_array_value_map_,
1221 type, location);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001222 }
1223 if (opcode == Instruction::AGET_WIDE) {
1224 SetOperandValueWide(mir->ssa_rep->defs[0], res);
1225 } else {
1226 SetOperandValue(mir->ssa_rep->defs[0], res);
1227 }
1228 return res;
1229}
1230
1231void LocalValueNumbering::HandleAPut(MIR* mir, uint16_t opcode) {
1232 int array_idx = (opcode == Instruction::APUT_WIDE) ? 2 : 1;
1233 int index_idx = array_idx + 1;
1234 uint16_t array = GetOperandValue(mir->ssa_rep->uses[array_idx]);
1235 HandleNullCheck(mir, array);
1236 uint16_t index = GetOperandValue(mir->ssa_rep->uses[index_idx]);
1237 HandleRangeCheck(mir, array, index);
1238
Vladimir Markoaf6925b2014-10-31 16:37:32 +00001239 uint16_t type = APutMemAccessType(static_cast<Instruction::Code>(opcode));
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001240 uint16_t value = (opcode == Instruction::APUT_WIDE)
1241 ? GetOperandValueWide(mir->ssa_rep->uses[0])
1242 : GetOperandValue(mir->ssa_rep->uses[0]);
1243 if (IsNonAliasing(array)) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001244 bool put_is_live = HandleAliasingValuesPut<NonAliasingArrayVersions>(
1245 &non_aliasing_array_value_map_, array, index, value);
1246 if (!put_is_live) {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001247 // 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 Marko95a05972014-05-30 10:01:32 +01001251 } else {
1252 uint16_t location = gvn_->GetArrayLocation(array, index);
1253 bool put_is_live = HandleAliasingValuesPut<AliasingArrayVersions>(
1254 &aliasing_array_value_map_, type, location, value);
1255 if (!put_is_live) {
1256 // This APUT can be eliminated, it stores the same value that's already in the field.
1257 // TODO: Eliminate the APUT.
1258 return;
1259 }
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001260
Vladimir Marko95a05972014-05-30 10:01:32 +01001261 // Clobber all escaped array refs for this type.
1262 for (uint16_t escaped_array : escaped_refs_) {
1263 EscapedArrayClobberKey clobber_key = { escaped_array, type };
1264 escaped_array_clobber_set_.insert(clobber_key);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001265 }
1266 }
1267}
1268
1269uint16_t LocalValueNumbering::HandleIGet(MIR* mir, uint16_t opcode) {
1270 uint16_t base = GetOperandValue(mir->ssa_rep->uses[0]);
1271 HandleNullCheck(mir, base);
Vladimir Marko95a05972014-05-30 10:01:32 +01001272 const MirFieldInfo& field_info = gvn_->GetMirGraph()->GetIFieldLoweringInfo(mir);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001273 uint16_t res;
1274 if (!field_info.IsResolved() || field_info.IsVolatile()) {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001275 // Unresolved fields may be volatile, so handle them as such to be safe.
Vladimir Markofa236452014-09-29 17:58:10 +01001276 HandleInvokeOrClInitOrAcquireOp(mir); // Volatile GETs have acquire semantics.
1277 // Volatile fields always get a new memory version; field id is irrelevant.
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001278 // Use result s_reg - will be unique.
Vladimir Marko95a05972014-05-30 10:01:32 +01001279 res = gvn_->LookupValue(kNoValue, mir->ssa_rep->defs[0], kNoValue, kNoValue);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001280 } else {
Vladimir Markoaf6925b2014-10-31 16:37:32 +00001281 uint16_t type = IGetMemAccessType(static_cast<Instruction::Code>(opcode));
1282 uint16_t field_id = gvn_->GetIFieldId(mir);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001283 if (IsNonAliasingIField(base, field_id, type)) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001284 uint16_t loc = gvn_->LookupValue(kNonAliasingIFieldLocOp, base, field_id, type);
1285 auto lb = non_aliasing_ifield_value_map_.lower_bound(loc);
1286 if (lb != non_aliasing_ifield_value_map_.end() && lb->first == loc) {
1287 res = lb->second;
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001288 } else {
Vladimir Marko95a05972014-05-30 10:01:32 +01001289 res = gvn_->LookupValue(kNonAliasingIFieldInitialOp, loc, kNoValue, kNoValue);
1290 non_aliasing_ifield_value_map_.PutBefore(lb, loc, res);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001291 }
Vladimir Marko95a05972014-05-30 10:01:32 +01001292 } else {
1293 res = HandleAliasingValuesGet<AliasingIFieldVersions>(&aliasing_ifield_value_map_,
1294 field_id, base);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001295 }
1296 }
1297 if (opcode == Instruction::IGET_WIDE) {
1298 SetOperandValueWide(mir->ssa_rep->defs[0], res);
1299 } else {
1300 SetOperandValue(mir->ssa_rep->defs[0], res);
1301 }
1302 return res;
1303}
1304
1305void LocalValueNumbering::HandleIPut(MIR* mir, uint16_t opcode) {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001306 int base_reg = (opcode == Instruction::IPUT_WIDE) ? 2 : 1;
1307 uint16_t base = GetOperandValue(mir->ssa_rep->uses[base_reg]);
1308 HandleNullCheck(mir, base);
Vladimir Markoaf6925b2014-10-31 16:37:32 +00001309 uint16_t type = IPutMemAccessType(static_cast<Instruction::Code>(opcode));
Vladimir Marko95a05972014-05-30 10:01:32 +01001310 const MirFieldInfo& field_info = gvn_->GetMirGraph()->GetIFieldLoweringInfo(mir);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001311 if (!field_info.IsResolved()) {
1312 // Unresolved fields always alias with everything of the same type.
1313 // Use mir->offset as modifier; without elaborate inlining, it will be unique.
1314 unresolved_ifield_version_[type] =
Vladimir Marko95a05972014-05-30 10:01:32 +01001315 gvn_->LookupValue(kUnresolvedIFieldOp, kNoValue, kNoValue, mir->offset);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001316
Vladimir Marko95a05972014-05-30 10:01:32 +01001317 // For simplicity, treat base as escaped now.
1318 HandleEscapingRef(base);
1319
1320 // Clobber all fields of escaped references of the same type.
1321 for (uint16_t escaped_ref : escaped_refs_) {
1322 EscapedIFieldClobberKey clobber_key = { escaped_ref, type, kNoValue };
1323 escaped_ifield_clobber_set_.insert(clobber_key);
1324 }
1325
1326 // Aliasing fields of the same type may have been overwritten.
1327 auto it = aliasing_ifield_value_map_.begin(), end = aliasing_ifield_value_map_.end();
1328 while (it != end) {
Vladimir Markoaf6925b2014-10-31 16:37:32 +00001329 if (gvn_->GetIFieldType(it->first) != type) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001330 ++it;
1331 } else {
1332 it = aliasing_ifield_value_map_.erase(it);
1333 }
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001334 }
1335 } else if (field_info.IsVolatile()) {
1336 // Nothing to do, resolved volatile fields always get a new memory version anyway and
1337 // can't alias with resolved non-volatile fields.
1338 } else {
Vladimir Markoaf6925b2014-10-31 16:37:32 +00001339 uint16_t field_id = gvn_->GetIFieldId(mir);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001340 uint16_t value = (opcode == Instruction::IPUT_WIDE)
1341 ? GetOperandValueWide(mir->ssa_rep->uses[0])
1342 : GetOperandValue(mir->ssa_rep->uses[0]);
1343 if (IsNonAliasing(base)) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001344 uint16_t loc = gvn_->LookupValue(kNonAliasingIFieldLocOp, base, field_id, type);
1345 auto lb = non_aliasing_ifield_value_map_.lower_bound(loc);
1346 if (lb != non_aliasing_ifield_value_map_.end() && lb->first == loc) {
1347 if (lb->second == value) {
1348 // This IPUT can be eliminated, it stores the same value that's already in the field.
1349 // TODO: Eliminate the IPUT.
1350 return;
1351 }
1352 lb->second = value; // Overwrite.
1353 } else {
1354 non_aliasing_ifield_value_map_.PutBefore(lb, loc, value);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001355 }
Vladimir Marko95a05972014-05-30 10:01:32 +01001356 } else {
1357 bool put_is_live = HandleAliasingValuesPut<AliasingIFieldVersions>(
1358 &aliasing_ifield_value_map_, field_id, base, value);
1359 if (!put_is_live) {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001360 // This IPUT can be eliminated, it stores the same value that's already in the field.
1361 // TODO: Eliminate the IPUT.
1362 return;
1363 }
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001364
Vladimir Marko95a05972014-05-30 10:01:32 +01001365 // Clobber all fields of escaped references for this field.
1366 for (uint16_t escaped_ref : escaped_refs_) {
1367 EscapedIFieldClobberKey clobber_key = { escaped_ref, type, field_id };
1368 escaped_ifield_clobber_set_.insert(clobber_key);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001369 }
1370 }
1371 }
1372}
1373
1374uint16_t LocalValueNumbering::HandleSGet(MIR* mir, uint16_t opcode) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001375 const MirSFieldLoweringInfo& field_info = gvn_->GetMirGraph()->GetSFieldLoweringInfo(mir);
Vladimir Markofa236452014-09-29 17:58:10 +01001376 if (!field_info.IsResolved() || field_info.IsVolatile() ||
Vladimir Marko66c6d7b2014-10-16 15:41:48 +01001377 (!field_info.IsClassInitialized() &&
1378 (mir->optimization_flags & MIR_CLASS_IS_INITIALIZED) == 0)) {
Vladimir Markofa236452014-09-29 17:58:10 +01001379 // Volatile SGETs (and unresolved fields are potentially volatile) have acquire semantics
1380 // and class initialization can call arbitrary functions, we need to wipe aliasing values.
1381 HandleInvokeOrClInitOrAcquireOp(mir);
Vladimir Markof418f322014-07-09 14:45:36 +01001382 }
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001383 uint16_t res;
1384 if (!field_info.IsResolved() || field_info.IsVolatile()) {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001385 // Unresolved fields may be volatile, so handle them as such to be safe.
Vladimir Markofa236452014-09-29 17:58:10 +01001386 // Volatile fields always get a new memory version; field id is irrelevant.
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001387 // Use result s_reg - will be unique.
Vladimir Marko95a05972014-05-30 10:01:32 +01001388 res = gvn_->LookupValue(kNoValue, mir->ssa_rep->defs[0], kNoValue, kNoValue);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001389 } else {
Vladimir Markoaf6925b2014-10-31 16:37:32 +00001390 uint16_t type = SGetMemAccessType(static_cast<Instruction::Code>(opcode));
1391 uint16_t field_id = gvn_->GetSFieldId(mir);
Vladimir Marko95a05972014-05-30 10:01:32 +01001392 auto lb = sfield_value_map_.lower_bound(field_id);
1393 if (lb != sfield_value_map_.end() && lb->first == field_id) {
1394 res = lb->second;
1395 } else {
1396 // Resolved non-volatile static fields can alias with non-resolved fields of the same type,
1397 // so we need to use unresolved_sfield_version_[type] in addition to global_memory_version_
1398 // to determine the version of the field.
1399 res = gvn_->LookupValue(kResolvedSFieldOp, field_id,
1400 unresolved_sfield_version_[type], global_memory_version_);
1401 sfield_value_map_.PutBefore(lb, field_id, res);
1402 }
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001403 }
1404 if (opcode == Instruction::SGET_WIDE) {
1405 SetOperandValueWide(mir->ssa_rep->defs[0], res);
1406 } else {
1407 SetOperandValue(mir->ssa_rep->defs[0], res);
1408 }
1409 return res;
1410}
1411
1412void LocalValueNumbering::HandleSPut(MIR* mir, uint16_t opcode) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001413 const MirSFieldLoweringInfo& field_info = gvn_->GetMirGraph()->GetSFieldLoweringInfo(mir);
Vladimir Marko66c6d7b2014-10-16 15:41:48 +01001414 if (!field_info.IsClassInitialized() &&
1415 (mir->optimization_flags & MIR_CLASS_IS_INITIALIZED) == 0) {
Vladimir Markof418f322014-07-09 14:45:36 +01001416 // Class initialization can call arbitrary functions, we need to wipe aliasing values.
Vladimir Markofa236452014-09-29 17:58:10 +01001417 HandleInvokeOrClInitOrAcquireOp(mir);
Vladimir Markof418f322014-07-09 14:45:36 +01001418 }
Vladimir Markoaf6925b2014-10-31 16:37:32 +00001419 uint16_t type = SPutMemAccessType(static_cast<Instruction::Code>(opcode));
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001420 if (!field_info.IsResolved()) {
1421 // Unresolved fields always alias with everything of the same type.
1422 // Use mir->offset as modifier; without elaborate inlining, it will be unique.
1423 unresolved_sfield_version_[type] =
Vladimir Marko95a05972014-05-30 10:01:32 +01001424 gvn_->LookupValue(kUnresolvedSFieldOp, kNoValue, kNoValue, mir->offset);
1425 RemoveSFieldsForType(type);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001426 } else if (field_info.IsVolatile()) {
1427 // Nothing to do, resolved volatile fields always get a new memory version anyway and
1428 // can't alias with resolved non-volatile fields.
1429 } else {
Vladimir Markoaf6925b2014-10-31 16:37:32 +00001430 uint16_t field_id = gvn_->GetSFieldId(mir);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001431 uint16_t value = (opcode == Instruction::SPUT_WIDE)
1432 ? GetOperandValueWide(mir->ssa_rep->uses[0])
1433 : GetOperandValue(mir->ssa_rep->uses[0]);
1434 // Resolved non-volatile static fields can alias with non-resolved fields of the same type,
1435 // so we need to use unresolved_sfield_version_[type] in addition to global_memory_version_
1436 // to determine the version of the field.
Vladimir Marko95a05972014-05-30 10:01:32 +01001437 auto lb = sfield_value_map_.lower_bound(field_id);
1438 if (lb != sfield_value_map_.end() && lb->first == field_id) {
1439 if (lb->second == value) {
1440 // This SPUT can be eliminated, it stores the same value that's already in the field.
1441 // TODO: Eliminate the SPUT.
1442 return;
1443 }
1444 lb->second = value; // Overwrite.
1445 } else {
1446 sfield_value_map_.PutBefore(lb, field_id, value);
1447 }
1448 }
1449}
1450
1451void LocalValueNumbering::RemoveSFieldsForType(uint16_t type) {
1452 // Erase all static fields of this type from the sfield_value_map_.
1453 for (auto it = sfield_value_map_.begin(), end = sfield_value_map_.end(); it != end; ) {
Vladimir Markoaf6925b2014-10-31 16:37:32 +00001454 if (gvn_->GetSFieldType(it->first) == type) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001455 it = sfield_value_map_.erase(it);
1456 } else {
1457 ++it;
1458 }
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001459 }
Vladimir Markof59f18b2014-02-17 15:53:57 +00001460}
buzbee2502e002012-12-31 16:05:53 -08001461
Vladimir Markofa236452014-09-29 17:58:10 +01001462void LocalValueNumbering::HandleInvokeOrClInitOrAcquireOp(MIR* mir) {
Vladimir Markof418f322014-07-09 14:45:36 +01001463 // Use mir->offset as modifier; without elaborate inlining, it will be unique.
Vladimir Marko95a05972014-05-30 10:01:32 +01001464 global_memory_version_ =
1465 gvn_->LookupValue(kInvokeMemoryVersionBumpOp, 0u, 0u, mir->offset);
1466 // All static fields and instance fields and array elements of aliasing references,
1467 // including escaped references, may have been modified.
1468 sfield_value_map_.clear();
1469 aliasing_ifield_value_map_.clear();
1470 aliasing_array_value_map_.clear();
1471 escaped_refs_.clear();
1472 escaped_ifield_clobber_set_.clear();
1473 escaped_array_clobber_set_.clear();
Vladimir Markof418f322014-07-09 14:45:36 +01001474}
1475
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001476uint16_t LocalValueNumbering::GetValueNumber(MIR* mir) {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001477 uint16_t res = kNoValue;
buzbee2502e002012-12-31 16:05:53 -08001478 uint16_t opcode = mir->dalvikInsn.opcode;
1479 switch (opcode) {
1480 case Instruction::NOP:
1481 case Instruction::RETURN_VOID:
1482 case Instruction::RETURN:
1483 case Instruction::RETURN_OBJECT:
1484 case Instruction::RETURN_WIDE:
buzbee2502e002012-12-31 16:05:53 -08001485 case Instruction::GOTO:
1486 case Instruction::GOTO_16:
1487 case Instruction::GOTO_32:
1488 case Instruction::CHECK_CAST:
1489 case Instruction::THROW:
1490 case Instruction::FILL_ARRAY_DATA:
buzbee2502e002012-12-31 16:05:53 -08001491 case Instruction::PACKED_SWITCH:
1492 case Instruction::SPARSE_SWITCH:
1493 case Instruction::IF_EQ:
1494 case Instruction::IF_NE:
1495 case Instruction::IF_LT:
1496 case Instruction::IF_GE:
1497 case Instruction::IF_GT:
1498 case Instruction::IF_LE:
1499 case Instruction::IF_EQZ:
1500 case Instruction::IF_NEZ:
1501 case Instruction::IF_LTZ:
1502 case Instruction::IF_GEZ:
1503 case Instruction::IF_GTZ:
1504 case Instruction::IF_LEZ:
buzbee2502e002012-12-31 16:05:53 -08001505 case kMirOpFusedCmplFloat:
1506 case kMirOpFusedCmpgFloat:
1507 case kMirOpFusedCmplDouble:
1508 case kMirOpFusedCmpgDouble:
1509 case kMirOpFusedCmpLong:
1510 // Nothing defined - take no action.
1511 break;
1512
Vladimir Marko95a05972014-05-30 10:01:32 +01001513 case Instruction::MONITOR_ENTER:
1514 HandleNullCheck(mir, GetOperandValue(mir->ssa_rep->uses[0]));
Vladimir Markofa236452014-09-29 17:58:10 +01001515 HandleInvokeOrClInitOrAcquireOp(mir); // Acquire operation.
Vladimir Marko95a05972014-05-30 10:01:32 +01001516 break;
1517
1518 case Instruction::MONITOR_EXIT:
1519 HandleNullCheck(mir, GetOperandValue(mir->ssa_rep->uses[0]));
1520 // If we're running GVN and CanModify(), uneliminated null check indicates bytecode error.
Vladimir Marko415ac882014-09-30 18:09:14 +01001521 if ((mir->optimization_flags & MIR_IGNORE_NULL_CHECK) == 0 &&
1522 gvn_->work_lvn_ != nullptr && gvn_->CanModify()) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001523 LOG(WARNING) << "Bytecode error: MONITOR_EXIT is still null checked at 0x" << std::hex
1524 << mir->offset << " in " << PrettyMethod(gvn_->cu_->method_idx, *gvn_->cu_->dex_file);
1525 }
1526 break;
1527
Vladimir Markof59f18b2014-02-17 15:53:57 +00001528 case Instruction::FILLED_NEW_ARRAY:
1529 case Instruction::FILLED_NEW_ARRAY_RANGE:
1530 // Nothing defined but the result will be unique and non-null.
1531 if (mir->next != nullptr && mir->next->dalvikInsn.opcode == Instruction::MOVE_RESULT_OBJECT) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001532 uint16_t array = MarkNonAliasingNonNull(mir->next);
1533 // Do not SetOperandValue(), we'll do that when we process the MOVE_RESULT_OBJECT.
1534 if (kLocalValueNumberingEnableFilledNewArrayTracking && mir->ssa_rep->num_uses != 0u) {
1535 AliasingValues* values = GetAliasingValues(&non_aliasing_array_value_map_, array);
1536 // Clear the value if we got a merged version in a loop.
Vladimir Markob19955d2014-07-29 12:04:10 +01001537 *values = AliasingValues(this);
Vladimir Marko95a05972014-05-30 10:01:32 +01001538 for (size_t i = 0u, count = mir->ssa_rep->num_uses; i != count; ++i) {
1539 DCHECK_EQ(High16Bits(i), 0u);
1540 uint16_t index = gvn_->LookupValue(Instruction::CONST, i, 0u, 0);
1541 uint16_t value = GetOperandValue(mir->ssa_rep->uses[i]);
1542 values->load_value_map.Put(index, value);
1543 RangeCheckKey key = { array, index };
1544 range_checked_.insert(key);
1545 }
1546 }
Vladimir Markof59f18b2014-02-17 15:53:57 +00001547 // The MOVE_RESULT_OBJECT will be processed next and we'll return the value name then.
1548 }
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001549 // All args escaped (if references).
1550 for (size_t i = 0u, count = mir->ssa_rep->num_uses; i != count; ++i) {
1551 uint16_t reg = GetOperandValue(mir->ssa_rep->uses[i]);
1552 HandleEscapingRef(reg);
1553 }
Vladimir Markof59f18b2014-02-17 15:53:57 +00001554 break;
1555
Vladimir Markoa78e66a2014-10-16 13:38:44 +01001556 case kMirOpNullCheck:
1557 HandleNullCheck(mir, GetOperandValue(mir->ssa_rep->uses[0]));
1558 break;
1559
Vladimir Markof59f18b2014-02-17 15:53:57 +00001560 case Instruction::INVOKE_DIRECT:
1561 case Instruction::INVOKE_DIRECT_RANGE:
1562 case Instruction::INVOKE_VIRTUAL:
1563 case Instruction::INVOKE_VIRTUAL_RANGE:
1564 case Instruction::INVOKE_SUPER:
1565 case Instruction::INVOKE_SUPER_RANGE:
1566 case Instruction::INVOKE_INTERFACE:
1567 case Instruction::INVOKE_INTERFACE_RANGE: {
1568 // Nothing defined but handle the null check.
1569 uint16_t reg = GetOperandValue(mir->ssa_rep->uses[0]);
1570 HandleNullCheck(mir, reg);
1571 }
Ian Rogersfc787ec2014-10-09 21:56:44 -07001572 FALLTHROUGH_INTENDED;
Vladimir Markof59f18b2014-02-17 15:53:57 +00001573 case Instruction::INVOKE_STATIC:
1574 case Instruction::INVOKE_STATIC_RANGE:
Vladimir Markoff0ac472014-10-02 17:24:53 +01001575 // Make ref args aliasing.
Vladimir Markoa4426cf2014-10-22 17:15:53 +01001576 HandleInvokeArgs(mir, this);
Vladimir Markoff0ac472014-10-02 17:24:53 +01001577 HandleInvokeOrClInitOrAcquireOp(mir);
Vladimir Markof59f18b2014-02-17 15:53:57 +00001578 break;
1579
buzbee2502e002012-12-31 16:05:53 -08001580 case Instruction::MOVE_RESULT:
1581 case Instruction::MOVE_RESULT_OBJECT:
1582 case Instruction::INSTANCE_OF:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001583 // 1 result, treat as unique each time, use result s_reg - will be unique.
1584 res = GetOperandValue(mir->ssa_rep->defs[0]);
1585 SetOperandValue(mir->ssa_rep->defs[0], res);
1586 break;
1587 case Instruction::MOVE_EXCEPTION:
buzbee2502e002012-12-31 16:05:53 -08001588 case Instruction::NEW_INSTANCE:
buzbee2502e002012-12-31 16:05:53 -08001589 case Instruction::CONST_CLASS:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001590 case Instruction::NEW_ARRAY:
Vladimir Markob3e527b2014-04-04 12:37:07 +01001591 // 1 result, treat as unique each time, use result s_reg - will be unique.
1592 res = MarkNonAliasingNonNull(mir);
Vladimir Marko95a05972014-05-30 10:01:32 +01001593 SetOperandValue(mir->ssa_rep->defs[0], res);
buzbee2502e002012-12-31 16:05:53 -08001594 break;
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001595 case Instruction::CONST_STRING:
1596 case Instruction::CONST_STRING_JUMBO:
1597 // These strings are internalized, so assign value based on the string pool index.
Vladimir Marko95a05972014-05-30 10:01:32 +01001598 res = gvn_->LookupValue(Instruction::CONST_STRING, Low16Bits(mir->dalvikInsn.vB),
1599 High16Bits(mir->dalvikInsn.vB), 0);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001600 SetOperandValue(mir->ssa_rep->defs[0], res);
1601 null_checked_.insert(res); // May already be there.
1602 // NOTE: Hacking the contents of an internalized string via reflection is possible
1603 // but the behavior is undefined. Therefore, we consider the string constant and
1604 // the reference non-aliasing.
1605 // TUNING: We could keep this property even if the reference "escapes".
1606 non_aliasing_refs_.insert(res); // May already be there.
1607 break;
Vladimir Markof59f18b2014-02-17 15:53:57 +00001608 case Instruction::MOVE_RESULT_WIDE:
Vladimir Markob3e527b2014-04-04 12:37:07 +01001609 // 1 wide result, treat as unique each time, use result s_reg - will be unique.
1610 res = GetOperandValueWide(mir->ssa_rep->defs[0]);
1611 SetOperandValueWide(mir->ssa_rep->defs[0], res);
buzbee2502e002012-12-31 16:05:53 -08001612 break;
1613
1614 case kMirOpPhi:
Vladimir Marko95a05972014-05-30 10:01:32 +01001615 res = HandlePhi(mir);
buzbee2502e002012-12-31 16:05:53 -08001616 break;
1617
1618 case Instruction::MOVE:
1619 case Instruction::MOVE_OBJECT:
1620 case Instruction::MOVE_16:
1621 case Instruction::MOVE_OBJECT_16:
1622 case Instruction::MOVE_FROM16:
1623 case Instruction::MOVE_OBJECT_FROM16:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001624 case kMirOpCopy:
1625 // Just copy value number of source to value number of result.
1626 res = GetOperandValue(mir->ssa_rep->uses[0]);
1627 SetOperandValue(mir->ssa_rep->defs[0], res);
buzbee2502e002012-12-31 16:05:53 -08001628 break;
1629
1630 case Instruction::MOVE_WIDE:
1631 case Instruction::MOVE_WIDE_16:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001632 case Instruction::MOVE_WIDE_FROM16:
1633 // Just copy value number of source to value number of result.
1634 res = GetOperandValueWide(mir->ssa_rep->uses[0]);
1635 SetOperandValueWide(mir->ssa_rep->defs[0], res);
buzbee2502e002012-12-31 16:05:53 -08001636 break;
1637
1638 case Instruction::CONST:
1639 case Instruction::CONST_4:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001640 case Instruction::CONST_16:
Vladimir Marko95a05972014-05-30 10:01:32 +01001641 res = gvn_->LookupValue(Instruction::CONST, Low16Bits(mir->dalvikInsn.vB),
1642 High16Bits(mir->dalvikInsn.vB), 0);
Vladimir Markof59f18b2014-02-17 15:53:57 +00001643 SetOperandValue(mir->ssa_rep->defs[0], res);
buzbee2502e002012-12-31 16:05:53 -08001644 break;
1645
Vladimir Markof59f18b2014-02-17 15:53:57 +00001646 case Instruction::CONST_HIGH16:
Vladimir Marko95a05972014-05-30 10:01:32 +01001647 res = gvn_->LookupValue(Instruction::CONST, 0, mir->dalvikInsn.vB, 0);
Vladimir Markof59f18b2014-02-17 15:53:57 +00001648 SetOperandValue(mir->ssa_rep->defs[0], res);
buzbee2502e002012-12-31 16:05:53 -08001649 break;
1650
1651 case Instruction::CONST_WIDE_16:
1652 case Instruction::CONST_WIDE_32: {
Vladimir Marko95a05972014-05-30 10:01:32 +01001653 uint16_t low_res = gvn_->LookupValue(Instruction::CONST, Low16Bits(mir->dalvikInsn.vB),
1654 High16Bits(mir->dalvikInsn.vB >> 16), 1);
buzbee2502e002012-12-31 16:05:53 -08001655 uint16_t high_res;
1656 if (mir->dalvikInsn.vB & 0x80000000) {
Vladimir Marko95a05972014-05-30 10:01:32 +01001657 high_res = gvn_->LookupValue(Instruction::CONST, 0xffff, 0xffff, 2);
buzbee2502e002012-12-31 16:05:53 -08001658 } else {
Vladimir Marko95a05972014-05-30 10:01:32 +01001659 high_res = gvn_->LookupValue(Instruction::CONST, 0, 0, 2);
buzbee2502e002012-12-31 16:05:53 -08001660 }
Vladimir Marko95a05972014-05-30 10:01:32 +01001661 res = gvn_->LookupValue(Instruction::CONST, low_res, high_res, 3);
Vladimir Markof59f18b2014-02-17 15:53:57 +00001662 SetOperandValueWide(mir->ssa_rep->defs[0], res);
buzbee2502e002012-12-31 16:05:53 -08001663 }
1664 break;
1665
1666 case Instruction::CONST_WIDE: {
1667 uint32_t low_word = Low32Bits(mir->dalvikInsn.vB_wide);
1668 uint32_t high_word = High32Bits(mir->dalvikInsn.vB_wide);
Vladimir Marko95a05972014-05-30 10:01:32 +01001669 uint16_t low_res = gvn_->LookupValue(Instruction::CONST, Low16Bits(low_word),
1670 High16Bits(low_word), 1);
1671 uint16_t high_res = gvn_->LookupValue(Instruction::CONST, Low16Bits(high_word),
1672 High16Bits(high_word), 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
1678 case Instruction::CONST_WIDE_HIGH16: {
Vladimir Marko95a05972014-05-30 10:01:32 +01001679 uint16_t low_res = gvn_->LookupValue(Instruction::CONST, 0, 0, 1);
1680 uint16_t high_res = gvn_->LookupValue(Instruction::CONST, 0,
1681 Low16Bits(mir->dalvikInsn.vB), 2);
1682 res = gvn_->LookupValue(Instruction::CONST, low_res, high_res, 3);
buzbee2502e002012-12-31 16:05:53 -08001683 SetOperandValueWide(mir->ssa_rep->defs[0], res);
1684 }
1685 break;
1686
Vladimir Marko95a05972014-05-30 10:01:32 +01001687 case Instruction::ARRAY_LENGTH: {
1688 // Handle the null check.
1689 uint16_t reg = GetOperandValue(mir->ssa_rep->uses[0]);
1690 HandleNullCheck(mir, reg);
1691 }
Ian Rogersfc787ec2014-10-09 21:56:44 -07001692 FALLTHROUGH_INTENDED;
buzbee2502e002012-12-31 16:05:53 -08001693 case Instruction::NEG_INT:
1694 case Instruction::NOT_INT:
1695 case Instruction::NEG_FLOAT:
1696 case Instruction::INT_TO_BYTE:
1697 case Instruction::INT_TO_SHORT:
1698 case Instruction::INT_TO_CHAR:
1699 case Instruction::INT_TO_FLOAT:
1700 case Instruction::FLOAT_TO_INT: {
1701 // res = op + 1 operand
1702 uint16_t operand1 = GetOperandValue(mir->ssa_rep->uses[0]);
Vladimir Marko95a05972014-05-30 10:01:32 +01001703 res = gvn_->LookupValue(opcode, operand1, kNoValue, kNoValue);
buzbee2502e002012-12-31 16:05:53 -08001704 SetOperandValue(mir->ssa_rep->defs[0], res);
1705 }
1706 break;
1707
1708 case Instruction::LONG_TO_FLOAT:
1709 case Instruction::LONG_TO_INT:
1710 case Instruction::DOUBLE_TO_FLOAT:
1711 case Instruction::DOUBLE_TO_INT: {
1712 // res = op + 1 wide operand
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001713 uint16_t operand1 = GetOperandValueWide(mir->ssa_rep->uses[0]);
Vladimir Marko95a05972014-05-30 10:01:32 +01001714 res = gvn_->LookupValue(opcode, operand1, kNoValue, kNoValue);
buzbee2502e002012-12-31 16:05:53 -08001715 SetOperandValue(mir->ssa_rep->defs[0], res);
1716 }
1717 break;
1718
buzbee2502e002012-12-31 16:05:53 -08001719 case Instruction::DOUBLE_TO_LONG:
1720 case Instruction::LONG_TO_DOUBLE:
1721 case Instruction::NEG_LONG:
1722 case Instruction::NOT_LONG:
1723 case Instruction::NEG_DOUBLE: {
1724 // wide res = op + 1 wide operand
1725 uint16_t operand1 = GetOperandValueWide(mir->ssa_rep->uses[0]);
Vladimir Marko95a05972014-05-30 10:01:32 +01001726 res = gvn_->LookupValue(opcode, operand1, kNoValue, kNoValue);
buzbee2502e002012-12-31 16:05:53 -08001727 SetOperandValueWide(mir->ssa_rep->defs[0], res);
1728 }
1729 break;
1730
1731 case Instruction::FLOAT_TO_DOUBLE:
1732 case Instruction::FLOAT_TO_LONG:
1733 case Instruction::INT_TO_DOUBLE:
1734 case Instruction::INT_TO_LONG: {
1735 // wide res = op + 1 operand
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001736 uint16_t operand1 = GetOperandValue(mir->ssa_rep->uses[0]);
Vladimir Marko95a05972014-05-30 10:01:32 +01001737 res = gvn_->LookupValue(opcode, operand1, kNoValue, kNoValue);
buzbee2502e002012-12-31 16:05:53 -08001738 SetOperandValueWide(mir->ssa_rep->defs[0], res);
1739 }
1740 break;
1741
1742 case Instruction::CMPL_DOUBLE:
1743 case Instruction::CMPG_DOUBLE:
1744 case Instruction::CMP_LONG: {
1745 // res = op + 2 wide operands
1746 uint16_t operand1 = GetOperandValueWide(mir->ssa_rep->uses[0]);
1747 uint16_t operand2 = GetOperandValueWide(mir->ssa_rep->uses[2]);
Vladimir Marko95a05972014-05-30 10:01:32 +01001748 res = gvn_->LookupValue(opcode, operand1, operand2, kNoValue);
buzbee2502e002012-12-31 16:05:53 -08001749 SetOperandValue(mir->ssa_rep->defs[0], res);
1750 }
1751 break;
1752
Razvan A Lupusorue0951142014-11-14 14:36:55 -08001753 case Instruction::DIV_INT:
1754 case Instruction::DIV_INT_2ADDR:
1755 case Instruction::REM_INT:
1756 case Instruction::REM_INT_2ADDR:
1757 HandleDivZeroCheck(mir, GetOperandValue(mir->ssa_rep->uses[1]));
1758 FALLTHROUGH_INTENDED;
1759
buzbee2502e002012-12-31 16:05:53 -08001760 case Instruction::CMPG_FLOAT:
1761 case Instruction::CMPL_FLOAT:
1762 case Instruction::ADD_INT:
1763 case Instruction::ADD_INT_2ADDR:
1764 case Instruction::MUL_INT:
1765 case Instruction::MUL_INT_2ADDR:
1766 case Instruction::AND_INT:
1767 case Instruction::AND_INT_2ADDR:
1768 case Instruction::OR_INT:
1769 case Instruction::OR_INT_2ADDR:
1770 case Instruction::XOR_INT:
1771 case Instruction::XOR_INT_2ADDR:
1772 case Instruction::SUB_INT:
1773 case Instruction::SUB_INT_2ADDR:
buzbee2502e002012-12-31 16:05:53 -08001774 case Instruction::SHL_INT:
1775 case Instruction::SHL_INT_2ADDR:
1776 case Instruction::SHR_INT:
1777 case Instruction::SHR_INT_2ADDR:
1778 case Instruction::USHR_INT:
1779 case Instruction::USHR_INT_2ADDR: {
1780 // res = op + 2 operands
1781 uint16_t operand1 = GetOperandValue(mir->ssa_rep->uses[0]);
1782 uint16_t operand2 = GetOperandValue(mir->ssa_rep->uses[1]);
Vladimir Marko95a05972014-05-30 10:01:32 +01001783 res = gvn_->LookupValue(opcode, operand1, operand2, kNoValue);
buzbee2502e002012-12-31 16:05:53 -08001784 SetOperandValue(mir->ssa_rep->defs[0], res);
1785 }
1786 break;
1787
Razvan A Lupusorue0951142014-11-14 14:36:55 -08001788 case Instruction::DIV_LONG:
1789 case Instruction::REM_LONG:
1790 case Instruction::DIV_LONG_2ADDR:
1791 case Instruction::REM_LONG_2ADDR:
1792 HandleDivZeroCheck(mir, GetOperandValueWide(mir->ssa_rep->uses[2]));
1793 FALLTHROUGH_INTENDED;
1794
buzbee2502e002012-12-31 16:05:53 -08001795 case Instruction::ADD_LONG:
1796 case Instruction::SUB_LONG:
1797 case Instruction::MUL_LONG:
buzbee2502e002012-12-31 16:05:53 -08001798 case Instruction::AND_LONG:
1799 case Instruction::OR_LONG:
1800 case Instruction::XOR_LONG:
1801 case Instruction::ADD_LONG_2ADDR:
1802 case Instruction::SUB_LONG_2ADDR:
1803 case Instruction::MUL_LONG_2ADDR:
buzbee2502e002012-12-31 16:05:53 -08001804 case Instruction::AND_LONG_2ADDR:
1805 case Instruction::OR_LONG_2ADDR:
1806 case Instruction::XOR_LONG_2ADDR:
1807 case Instruction::ADD_DOUBLE:
1808 case Instruction::SUB_DOUBLE:
1809 case Instruction::MUL_DOUBLE:
1810 case Instruction::DIV_DOUBLE:
1811 case Instruction::REM_DOUBLE:
1812 case Instruction::ADD_DOUBLE_2ADDR:
1813 case Instruction::SUB_DOUBLE_2ADDR:
1814 case Instruction::MUL_DOUBLE_2ADDR:
1815 case Instruction::DIV_DOUBLE_2ADDR:
1816 case Instruction::REM_DOUBLE_2ADDR: {
1817 // wide res = op + 2 wide operands
1818 uint16_t operand1 = GetOperandValueWide(mir->ssa_rep->uses[0]);
1819 uint16_t operand2 = GetOperandValueWide(mir->ssa_rep->uses[2]);
Vladimir Marko95a05972014-05-30 10:01:32 +01001820 res = gvn_->LookupValue(opcode, operand1, operand2, kNoValue);
buzbee2502e002012-12-31 16:05:53 -08001821 SetOperandValueWide(mir->ssa_rep->defs[0], res);
1822 }
1823 break;
1824
1825 case Instruction::SHL_LONG:
1826 case Instruction::SHR_LONG:
1827 case Instruction::USHR_LONG:
1828 case Instruction::SHL_LONG_2ADDR:
1829 case Instruction::SHR_LONG_2ADDR:
1830 case Instruction::USHR_LONG_2ADDR: {
1831 // wide res = op + 1 wide operand + 1 operand
1832 uint16_t operand1 = GetOperandValueWide(mir->ssa_rep->uses[0]);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001833 uint16_t operand2 = GetOperandValue(mir->ssa_rep->uses[2]);
Vladimir Marko95a05972014-05-30 10:01:32 +01001834 res = gvn_->LookupValue(opcode, operand1, operand2, kNoValue);
buzbee2502e002012-12-31 16:05:53 -08001835 SetOperandValueWide(mir->ssa_rep->defs[0], res);
1836 }
1837 break;
1838
1839 case Instruction::ADD_FLOAT:
1840 case Instruction::SUB_FLOAT:
1841 case Instruction::MUL_FLOAT:
1842 case Instruction::DIV_FLOAT:
1843 case Instruction::REM_FLOAT:
1844 case Instruction::ADD_FLOAT_2ADDR:
1845 case Instruction::SUB_FLOAT_2ADDR:
1846 case Instruction::MUL_FLOAT_2ADDR:
1847 case Instruction::DIV_FLOAT_2ADDR:
1848 case Instruction::REM_FLOAT_2ADDR: {
1849 // res = op + 2 operands
1850 uint16_t operand1 = GetOperandValue(mir->ssa_rep->uses[0]);
1851 uint16_t operand2 = GetOperandValue(mir->ssa_rep->uses[1]);
Vladimir Marko95a05972014-05-30 10:01:32 +01001852 res = gvn_->LookupValue(opcode, operand1, operand2, kNoValue);
buzbee2502e002012-12-31 16:05:53 -08001853 SetOperandValue(mir->ssa_rep->defs[0], res);
1854 }
1855 break;
1856
1857 case Instruction::RSUB_INT:
1858 case Instruction::ADD_INT_LIT16:
1859 case Instruction::MUL_INT_LIT16:
1860 case Instruction::DIV_INT_LIT16:
1861 case Instruction::REM_INT_LIT16:
1862 case Instruction::AND_INT_LIT16:
1863 case Instruction::OR_INT_LIT16:
1864 case Instruction::XOR_INT_LIT16:
1865 case Instruction::ADD_INT_LIT8:
1866 case Instruction::RSUB_INT_LIT8:
1867 case Instruction::MUL_INT_LIT8:
1868 case Instruction::DIV_INT_LIT8:
1869 case Instruction::REM_INT_LIT8:
1870 case Instruction::AND_INT_LIT8:
1871 case Instruction::OR_INT_LIT8:
1872 case Instruction::XOR_INT_LIT8:
1873 case Instruction::SHL_INT_LIT8:
1874 case Instruction::SHR_INT_LIT8:
1875 case Instruction::USHR_INT_LIT8: {
nikolay serdjukee40aa42014-03-25 12:21:29 +07001876 // Same as res = op + 2 operands, except use vC as operand 2
buzbee2502e002012-12-31 16:05:53 -08001877 uint16_t operand1 = GetOperandValue(mir->ssa_rep->uses[0]);
Vladimir Marko95a05972014-05-30 10:01:32 +01001878 uint16_t operand2 = gvn_->LookupValue(Instruction::CONST, mir->dalvikInsn.vC, 0, 0);
1879 res = gvn_->LookupValue(opcode, operand1, operand2, kNoValue);
buzbee2502e002012-12-31 16:05:53 -08001880 SetOperandValue(mir->ssa_rep->defs[0], res);
1881 }
1882 break;
1883
buzbee2502e002012-12-31 16:05:53 -08001884 case Instruction::AGET_OBJECT:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001885 case Instruction::AGET:
1886 case Instruction::AGET_WIDE:
buzbee2502e002012-12-31 16:05:53 -08001887 case Instruction::AGET_BOOLEAN:
1888 case Instruction::AGET_BYTE:
1889 case Instruction::AGET_CHAR:
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001890 case Instruction::AGET_SHORT:
1891 res = HandleAGet(mir, opcode);
buzbee2502e002012-12-31 16:05:53 -08001892 break;
1893
buzbee2502e002012-12-31 16:05:53 -08001894 case Instruction::APUT_OBJECT:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001895 HandlePutObject(mir);
Ian Rogersfc787ec2014-10-09 21:56:44 -07001896 FALLTHROUGH_INTENDED;
Vladimir Markof59f18b2014-02-17 15:53:57 +00001897 case Instruction::APUT:
1898 case Instruction::APUT_WIDE:
buzbee2502e002012-12-31 16:05:53 -08001899 case Instruction::APUT_BYTE:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001900 case Instruction::APUT_BOOLEAN:
1901 case Instruction::APUT_SHORT:
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001902 case Instruction::APUT_CHAR:
1903 HandleAPut(mir, opcode);
buzbee2502e002012-12-31 16:05:53 -08001904 break;
1905
1906 case Instruction::IGET_OBJECT:
buzbee2502e002012-12-31 16:05:53 -08001907 case Instruction::IGET:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001908 case Instruction::IGET_WIDE:
buzbee2502e002012-12-31 16:05:53 -08001909 case Instruction::IGET_BOOLEAN:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001910 case Instruction::IGET_BYTE:
1911 case Instruction::IGET_CHAR:
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001912 case Instruction::IGET_SHORT:
1913 res = HandleIGet(mir, opcode);
buzbee2502e002012-12-31 16:05:53 -08001914 break;
1915
buzbee2502e002012-12-31 16:05:53 -08001916 case Instruction::IPUT_OBJECT:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001917 HandlePutObject(mir);
Ian Rogersfc787ec2014-10-09 21:56:44 -07001918 FALLTHROUGH_INTENDED;
buzbee2502e002012-12-31 16:05:53 -08001919 case Instruction::IPUT:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001920 case Instruction::IPUT_WIDE:
buzbee2502e002012-12-31 16:05:53 -08001921 case Instruction::IPUT_BOOLEAN:
1922 case Instruction::IPUT_BYTE:
1923 case Instruction::IPUT_CHAR:
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001924 case Instruction::IPUT_SHORT:
1925 HandleIPut(mir, opcode);
buzbee2502e002012-12-31 16:05:53 -08001926 break;
1927
1928 case Instruction::SGET_OBJECT:
1929 case Instruction::SGET:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001930 case Instruction::SGET_WIDE:
buzbee2502e002012-12-31 16:05:53 -08001931 case Instruction::SGET_BOOLEAN:
1932 case Instruction::SGET_BYTE:
1933 case Instruction::SGET_CHAR:
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001934 case Instruction::SGET_SHORT:
1935 res = HandleSGet(mir, opcode);
buzbee2502e002012-12-31 16:05:53 -08001936 break;
1937
1938 case Instruction::SPUT_OBJECT:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001939 HandlePutObject(mir);
Ian Rogersfc787ec2014-10-09 21:56:44 -07001940 FALLTHROUGH_INTENDED;
buzbee2502e002012-12-31 16:05:53 -08001941 case Instruction::SPUT:
Vladimir Markof59f18b2014-02-17 15:53:57 +00001942 case Instruction::SPUT_WIDE:
buzbee2502e002012-12-31 16:05:53 -08001943 case Instruction::SPUT_BOOLEAN:
1944 case Instruction::SPUT_BYTE:
1945 case Instruction::SPUT_CHAR:
Vladimir Marko2ac01fc2014-05-22 12:09:08 +01001946 case Instruction::SPUT_SHORT:
1947 HandleSPut(mir, opcode);
buzbee2502e002012-12-31 16:05:53 -08001948 break;
buzbee2502e002012-12-31 16:05:53 -08001949 }
1950 return res;
1951}
1952
1953} // namespace art