blob: 190eab4a2be7323f289cbbe636f802250fcbb915 [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
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_COMPILER_DEX_LOCAL_VALUE_NUMBERING_H_
18#define ART_COMPILER_DEX_LOCAL_VALUE_NUMBERING_H_
buzbee2502e002012-12-31 16:05:53 -080019
Ian Rogers700a4022014-05-19 16:49:03 -070020#include <memory>
21
buzbee2502e002012-12-31 16:05:53 -080022#include "compiler_internals.h"
Vladimir Marko95a05972014-05-30 10:01:32 +010023#include "global_value_numbering.h"
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000024#include "utils/scoped_arena_allocator.h"
Vladimir Marko69f08ba2014-04-11 12:28:11 +010025#include "utils/scoped_arena_containers.h"
buzbee2502e002012-12-31 16:05:53 -080026
buzbee2502e002012-12-31 16:05:53 -080027namespace art {
28
Vladimir Markof59f18b2014-02-17 15:53:57 +000029class DexFile;
Vladimir Marko95a05972014-05-30 10:01:32 +010030
31// Enable/disable tracking values stored in the FILLED_NEW_ARRAY result.
32static constexpr bool kLocalValueNumberingEnableFilledNewArrayTracking = true;
buzbee2502e002012-12-31 16:05:53 -080033
buzbee311ca162013-02-28 15:56:43 -080034class LocalValueNumbering {
Vladimir Marko95a05972014-05-30 10:01:32 +010035 private:
36 static constexpr uint16_t kNoValue = GlobalValueNumbering::kNoValue;
37
Vladimir Marko2ac01fc2014-05-22 12:09:08 +010038 public:
Vladimir Marko95a05972014-05-30 10:01:32 +010039 LocalValueNumbering(GlobalValueNumbering* gvn, BasicBlockId id);
40
41 BasicBlockId Id() const {
42 return id_;
43 }
44
45 bool Equals(const LocalValueNumbering& other) const;
46
47 // Set non-static method's "this".
48 void SetSRegNullChecked(uint16_t s_reg) {
49 uint16_t value_name = GetOperandValue(s_reg);
50 null_checked_.insert(value_name);
51 }
52
53 bool IsValueNullChecked(uint16_t value_name) const {
54 return null_checked_.find(value_name) != null_checked_.end();
55 }
56
57 bool IsSregValue(uint16_t s_reg, uint16_t value_name) const {
58 auto it = sreg_value_map_.find(s_reg);
59 if (it != sreg_value_map_.end()) {
60 return it->second == value_name;
61 } else {
62 return gvn_->HasValue(kNoValue, s_reg, kNoValue, kNoValue, value_name);
63 }
64 }
65
66 enum MergeType {
67 kNormalMerge,
68 kCatchMerge,
69 kReturnMerge, // RETURN or PHI+RETURN. Merge only sreg maps.
70 };
71
72 void MergeOne(const LocalValueNumbering& other, MergeType merge_type);
73 void Merge(MergeType merge_type); // Merge gvn_->merge_lvns_.
Vladimir Marko2ac01fc2014-05-22 12:09:08 +010074
75 uint16_t GetValueNumber(MIR* mir);
76
77 // LocalValueNumbering should be allocated on the ArenaStack (or the native stack).
78 static void* operator new(size_t size, ScopedArenaAllocator* allocator) {
79 return allocator->Alloc(sizeof(LocalValueNumbering), kArenaAllocMIR);
80 }
81
82 // Allow delete-expression to destroy a LocalValueNumbering object without deallocation.
83 static void operator delete(void* ptr) { UNUSED(ptr); }
84
Vladimir Markof59f18b2014-02-17 15:53:57 +000085 private:
Vladimir Marko95a05972014-05-30 10:01:32 +010086 // A set of value names.
87 typedef GlobalValueNumbering::ValueNameSet ValueNameSet;
Vladimir Marko2ac01fc2014-05-22 12:09:08 +010088
Vladimir Markof59f18b2014-02-17 15:53:57 +000089 // Field types correspond to the ordering of GET/PUT instructions; this order is the same
90 // for IGET, IPUT, SGET, SPUT, AGET and APUT:
91 // op 0
92 // op_WIDE 1
93 // op_OBJECT 2
94 // op_BOOLEAN 3
95 // op_BYTE 4
96 // op_CHAR 5
97 // op_SHORT 6
98 static constexpr size_t kFieldTypeCount = 7;
99
Vladimir Marko95a05972014-05-30 10:01:32 +0100100 // Key is s_reg, value is value name.
101 typedef ScopedArenaSafeMap<uint16_t, uint16_t> SregValueMap;
Vladimir Markof59f18b2014-02-17 15:53:57 +0000102
Vladimir Marko95a05972014-05-30 10:01:32 +0100103 void SetOperandValueImpl(uint16_t s_reg, uint16_t value, SregValueMap* map) {
104 DCHECK_EQ(map->count(s_reg), 0u) << PrettyMethod(gvn_->cu_->method_idx, *gvn_->cu_->dex_file)
105 << " LVN id: " << id_ << ", s_reg: " << s_reg;
106 map->Put(s_reg, value);
107 }
108
109 uint16_t GetOperandValueImpl(int s_reg, const SregValueMap* map) const {
110 uint16_t res = kNoValue;
111 auto lb = map->find(s_reg);
112 if (lb != map->end()) {
113 res = lb->second;
114 } else {
115 // Using the original value; s_reg refers to an input reg.
116 res = gvn_->LookupValue(kNoValue, s_reg, kNoValue, kNoValue);
Vladimir Markof59f18b2014-02-17 15:53:57 +0000117 }
Vladimir Marko95a05972014-05-30 10:01:32 +0100118 return res;
119 }
120
121 void SetOperandValue(uint16_t s_reg, uint16_t value) {
122 SetOperandValueImpl(s_reg, value, &sreg_value_map_);
Vladimir Markof59f18b2014-02-17 15:53:57 +0000123 };
124
Vladimir Marko95a05972014-05-30 10:01:32 +0100125 uint16_t GetOperandValue(int s_reg) const {
126 return GetOperandValueImpl(s_reg, &sreg_value_map_);
127 };
128
129 void SetOperandValueWide(uint16_t s_reg, uint16_t value) {
130 SetOperandValueImpl(s_reg, value, &sreg_wide_value_map_);
131 };
132
133 uint16_t GetOperandValueWide(int s_reg) const {
134 return GetOperandValueImpl(s_reg, &sreg_wide_value_map_);
135 };
Vladimir Marko2ac01fc2014-05-22 12:09:08 +0100136
137 struct RangeCheckKey {
138 uint16_t array;
139 uint16_t index;
Vladimir Marko95a05972014-05-30 10:01:32 +0100140
141 // NOTE: Can't define this at namespace scope for a private struct.
142 bool operator==(const RangeCheckKey& other) const {
143 return array == other.array && index == other.index;
144 }
Vladimir Marko2ac01fc2014-05-22 12:09:08 +0100145 };
146
147 struct RangeCheckKeyComparator {
148 bool operator()(const RangeCheckKey& lhs, const RangeCheckKey& rhs) const {
149 if (lhs.array != rhs.array) {
150 return lhs.array < rhs.array;
151 }
152 return lhs.index < rhs.index;
153 }
154 };
155
156 typedef ScopedArenaSet<RangeCheckKey, RangeCheckKeyComparator> RangeCheckSet;
157
Vladimir Marko95a05972014-05-30 10:01:32 +0100158 // Maps instance field "location" (derived from base, field_id and type) to value name.
159 typedef ScopedArenaSafeMap<uint16_t, uint16_t> IFieldLocToValueMap;
Vladimir Marko2ac01fc2014-05-22 12:09:08 +0100160
Vladimir Marko95a05972014-05-30 10:01:32 +0100161 // Maps static field id to value name
162 typedef ScopedArenaSafeMap<uint16_t, uint16_t> SFieldToValueMap;
163
164 struct EscapedIFieldClobberKey {
165 uint16_t base; // Or array.
Vladimir Markof59f18b2014-02-17 15:53:57 +0000166 uint16_t type;
Vladimir Marko95a05972014-05-30 10:01:32 +0100167 uint16_t field_id; // None (kNoValue) for arrays and unresolved instance field stores.
168
169 // NOTE: Can't define this at namespace scope for a private struct.
170 bool operator==(const EscapedIFieldClobberKey& other) const {
171 return base == other.base && type == other.type && field_id == other.field_id;
172 }
Vladimir Markof59f18b2014-02-17 15:53:57 +0000173 };
174
Vladimir Marko95a05972014-05-30 10:01:32 +0100175 struct EscapedIFieldClobberKeyComparator {
176 bool operator()(const EscapedIFieldClobberKey& lhs, const EscapedIFieldClobberKey& rhs) const {
177 // Compare base first. This makes sequential iteration respect the order of base.
178 if (lhs.base != rhs.base) {
179 return lhs.base < rhs.base;
180 }
181 // Compare type second. This makes the type-clobber entries (field_id == kNoValue) last
182 // for given base and type and makes it easy to prune unnecessary entries when merging
183 // escaped_ifield_clobber_set_ from multiple LVNs.
Vladimir Marko2ac01fc2014-05-22 12:09:08 +0100184 if (lhs.type != rhs.type) {
185 return lhs.type < rhs.type;
Vladimir Markof59f18b2014-02-17 15:53:57 +0000186 }
Vladimir Marko95a05972014-05-30 10:01:32 +0100187 return lhs.field_id < rhs.field_id;
Vladimir Markof59f18b2014-02-17 15:53:57 +0000188 }
189 };
190
Vladimir Marko95a05972014-05-30 10:01:32 +0100191 typedef ScopedArenaSet<EscapedIFieldClobberKey, EscapedIFieldClobberKeyComparator>
192 EscapedIFieldClobberSet;
Vladimir Marko2ac01fc2014-05-22 12:09:08 +0100193
Vladimir Marko95a05972014-05-30 10:01:32 +0100194 struct EscapedArrayClobberKey {
Vladimir Marko2ac01fc2014-05-22 12:09:08 +0100195 uint16_t base;
196 uint16_t type;
Vladimir Marko95a05972014-05-30 10:01:32 +0100197
198 // NOTE: Can't define this at namespace scope for a private struct.
199 bool operator==(const EscapedArrayClobberKey& other) const {
200 return base == other.base && type == other.type;
201 }
Vladimir Marko2ac01fc2014-05-22 12:09:08 +0100202 };
203
Vladimir Marko95a05972014-05-30 10:01:32 +0100204 struct EscapedArrayClobberKeyComparator {
205 bool operator()(const EscapedArrayClobberKey& lhs, const EscapedArrayClobberKey& rhs) const {
206 // Compare base first. This makes sequential iteration respect the order of base.
207 if (lhs.base != rhs.base) {
208 return lhs.base < rhs.base;
Vladimir Marko2ac01fc2014-05-22 12:09:08 +0100209 }
Vladimir Marko95a05972014-05-30 10:01:32 +0100210 return lhs.type < rhs.type;
Vladimir Marko2ac01fc2014-05-22 12:09:08 +0100211 }
212 };
213
Vladimir Marko95a05972014-05-30 10:01:32 +0100214 // Clobber set for previously non-aliasing array refs that escaped.
215 typedef ScopedArenaSet<EscapedArrayClobberKey, EscapedArrayClobberKeyComparator>
216 EscapedArrayClobberSet;
Vladimir Marko2ac01fc2014-05-22 12:09:08 +0100217
Vladimir Marko95a05972014-05-30 10:01:32 +0100218 // Known location values for an aliasing set. The set can be tied to one of:
219 // 1. Instance field. The locations are aliasing references used to access the field.
220 // 2. Non-aliasing array reference. The locations are indexes to the array.
221 // 3. Aliasing array type. The locations are (reference, index) pair ids assigned by GVN.
222 // In each case we keep track of the last stored value, if any, and the set of locations
223 // where it was stored. We also keep track of all values known for the current write state
224 // (load_value_map), which can be known either because they have been loaded since the last
225 // store or because they contained the last_stored_value before the store and thus could not
226 // have changed as a result.
227 struct AliasingValues {
228 explicit AliasingValues(ScopedArenaAllocator* allocator)
229 : memory_version_before_stores(kNoValue),
230 last_stored_value(kNoValue),
231 store_loc_set(std::less<uint16_t>(), allocator->Adapter()),
232 last_load_memory_version(kNoValue),
233 load_value_map(std::less<uint16_t>(), allocator->Adapter()) {
buzbee2502e002012-12-31 16:05:53 -0800234 }
buzbee2502e002012-12-31 16:05:53 -0800235
Vladimir Marko95a05972014-05-30 10:01:32 +0100236 uint16_t memory_version_before_stores; // kNoValue if start version for the field.
237 uint16_t last_stored_value; // Last stored value name, kNoValue if none.
238 ValueNameSet store_loc_set; // Where was last_stored_value stored.
Vladimir Marko2ac01fc2014-05-22 12:09:08 +0100239
Vladimir Marko95a05972014-05-30 10:01:32 +0100240 // Maps refs (other than stored_to) to currently known values for this field other. On write,
241 // anything that differs from the written value is removed as it may be overwritten.
242 uint16_t last_load_memory_version; // kNoValue if not known.
243 ScopedArenaSafeMap<uint16_t, uint16_t> load_value_map;
Vladimir Marko2ac01fc2014-05-22 12:09:08 +0100244
Vladimir Marko95a05972014-05-30 10:01:32 +0100245 // NOTE: Can't define this at namespace scope for a private struct.
246 bool operator==(const AliasingValues& other) const {
247 return memory_version_before_stores == other.memory_version_before_stores &&
248 last_load_memory_version == other.last_load_memory_version &&
249 last_stored_value == other.last_stored_value &&
250 store_loc_set == other.store_loc_set &&
251 load_value_map == other.load_value_map;
buzbee2502e002012-12-31 16:05:53 -0800252 }
253 };
254
Vladimir Marko95a05972014-05-30 10:01:32 +0100255 // Maps instance field id to AliasingValues, locations are object refs.
256 typedef ScopedArenaSafeMap<uint16_t, AliasingValues> AliasingIFieldValuesMap;
buzbee2502e002012-12-31 16:05:53 -0800257
Vladimir Marko95a05972014-05-30 10:01:32 +0100258 // Maps non-aliasing array reference to AliasingValues, locations are array indexes.
259 typedef ScopedArenaSafeMap<uint16_t, AliasingValues> NonAliasingArrayValuesMap;
buzbee2502e002012-12-31 16:05:53 -0800260
Vladimir Marko95a05972014-05-30 10:01:32 +0100261 // Maps aliasing array type to AliasingValues, locations are (array, index) pair ids.
262 typedef ScopedArenaSafeMap<uint16_t, AliasingValues> AliasingArrayValuesMap;
buzbee2502e002012-12-31 16:05:53 -0800263
Vladimir Marko95a05972014-05-30 10:01:32 +0100264 // Helper classes defining versions for updating and merging the AliasingValues maps above.
265 class AliasingIFieldVersions;
266 class NonAliasingArrayVersions;
267 class AliasingArrayVersions;
268
269 template <typename Map>
270 AliasingValues* GetAliasingValues(Map* map, const typename Map::key_type& key);
271
272 template <typename Versions, typename KeyType>
273 void UpdateAliasingValuesLoadVersion(const KeyType& key, AliasingValues* values);
274
275 template <typename Versions, typename Map>
276 static uint16_t AliasingValuesMergeGet(GlobalValueNumbering* gvn,
277 const LocalValueNumbering* lvn,
278 Map* map, const typename Map::key_type& key,
279 uint16_t location);
280
281 template <typename Versions, typename Map>
282 uint16_t HandleAliasingValuesGet(Map* map, const typename Map::key_type& key,
283 uint16_t location);
284
285 template <typename Versions, typename Map>
286 bool HandleAliasingValuesPut(Map* map, const typename Map::key_type& key,
287 uint16_t location, uint16_t value);
288
Vladimir Markof59f18b2014-02-17 15:53:57 +0000289 uint16_t MarkNonAliasingNonNull(MIR* mir);
Vladimir Marko95a05972014-05-30 10:01:32 +0100290 bool IsNonAliasing(uint16_t reg) const;
291 bool IsNonAliasingIField(uint16_t reg, uint16_t field_id, uint16_t type) const;
292 bool IsNonAliasingArray(uint16_t reg, uint16_t type) const;
Vladimir Markof59f18b2014-02-17 15:53:57 +0000293 void HandleNullCheck(MIR* mir, uint16_t reg);
294 void HandleRangeCheck(MIR* mir, uint16_t array, uint16_t index);
295 void HandlePutObject(MIR* mir);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +0100296 void HandleEscapingRef(uint16_t base);
Vladimir Marko95a05972014-05-30 10:01:32 +0100297 uint16_t HandlePhi(MIR* mir);
Vladimir Marko2ac01fc2014-05-22 12:09:08 +0100298 uint16_t HandleAGet(MIR* mir, uint16_t opcode);
299 void HandleAPut(MIR* mir, uint16_t opcode);
300 uint16_t HandleIGet(MIR* mir, uint16_t opcode);
301 void HandleIPut(MIR* mir, uint16_t opcode);
302 uint16_t HandleSGet(MIR* mir, uint16_t opcode);
303 void HandleSPut(MIR* mir, uint16_t opcode);
Vladimir Marko95a05972014-05-30 10:01:32 +0100304 void RemoveSFieldsForType(uint16_t type);
Vladimir Markof418f322014-07-09 14:45:36 +0100305 void HandleInvokeOrClInit(MIR* mir);
Vladimir Markof59f18b2014-02-17 15:53:57 +0000306
Vladimir Marko95a05972014-05-30 10:01:32 +0100307 bool SameMemoryVersion(const LocalValueNumbering& other) const;
Vladimir Marko2ac01fc2014-05-22 12:09:08 +0100308
Vladimir Marko95a05972014-05-30 10:01:32 +0100309 uint16_t NewMemoryVersion(uint16_t* new_version);
310 void MergeMemoryVersions(bool clobbered_catch);
311
312 void PruneNonAliasingRefsForCatch();
313
314 template <typename Set, Set LocalValueNumbering::* set_ptr>
315 void IntersectSets();
316
317 // Intersect maps as sets. The value type must be equality-comparable.
318 template <typename Map, Map LocalValueNumbering::* map_ptr>
319 void IntersectMaps();
320
321 // Intersect maps as sets. The value type must be equality-comparable.
322 template <typename Map>
323 static void InPlaceIntersectMaps(Map* work_map, const Map& other_map);
324
325 template <typename Set, Set LocalValueNumbering::*set_ptr, void (LocalValueNumbering::*MergeFn)(
326 const typename Set::value_type& entry, typename Set::iterator hint)>
327 void MergeSets();
328
329 void IntersectAliasingValueLocations(AliasingValues* work_values, const AliasingValues* values);
330
331 void MergeEscapedRefs(const ValueNameSet::value_type& entry, ValueNameSet::iterator hint);
332 void MergeEscapedIFieldTypeClobberSets(const EscapedIFieldClobberSet::value_type& entry,
333 EscapedIFieldClobberSet::iterator hint);
334 void MergeEscapedIFieldClobberSets(const EscapedIFieldClobberSet::value_type& entry,
335 EscapedIFieldClobberSet::iterator hint);
336 void MergeEscapedArrayClobberSets(const EscapedArrayClobberSet::value_type& entry,
337 EscapedArrayClobberSet::iterator hint);
338 void MergeNullChecked(const ValueNameSet::value_type& entry, ValueNameSet::iterator hint);
339 void MergeSFieldValues(const SFieldToValueMap::value_type& entry,
340 SFieldToValueMap::iterator hint);
341 void MergeNonAliasingIFieldValues(const IFieldLocToValueMap::value_type& entry,
342 IFieldLocToValueMap::iterator hint);
343
344 template <typename Map, Map LocalValueNumbering::*map_ptr, typename Versions>
345 void MergeAliasingValues(const typename Map::value_type& entry, typename Map::iterator hint);
346
347 GlobalValueNumbering* gvn_;
348
349 // We're using the block id as a 16-bit operand value for some lookups.
350 COMPILE_ASSERT(sizeof(BasicBlockId) == sizeof(uint16_t), BasicBlockId_must_be_16_bit);
351 BasicBlockId id_;
Vladimir Marko2ac01fc2014-05-22 12:09:08 +0100352
buzbee2502e002012-12-31 16:05:53 -0800353 SregValueMap sreg_value_map_;
354 SregValueMap sreg_wide_value_map_;
Vladimir Marko95a05972014-05-30 10:01:32 +0100355
356 SFieldToValueMap sfield_value_map_;
357 IFieldLocToValueMap non_aliasing_ifield_value_map_;
358 AliasingIFieldValuesMap aliasing_ifield_value_map_;
359 NonAliasingArrayValuesMap non_aliasing_array_value_map_;
360 AliasingArrayValuesMap aliasing_array_value_map_;
Vladimir Marko2ac01fc2014-05-22 12:09:08 +0100361
362 // Data for dealing with memory clobbering and store/load aliasing.
Vladimir Markof59f18b2014-02-17 15:53:57 +0000363 uint16_t global_memory_version_;
364 uint16_t unresolved_sfield_version_[kFieldTypeCount];
365 uint16_t unresolved_ifield_version_[kFieldTypeCount];
Vladimir Markof59f18b2014-02-17 15:53:57 +0000366 // Value names of references to objects that cannot be reached through a different value name.
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000367 ValueNameSet non_aliasing_refs_;
Vladimir Marko95a05972014-05-30 10:01:32 +0100368 // Previously non-aliasing refs that escaped but can still be used for non-aliasing AGET/IGET.
369 ValueNameSet escaped_refs_;
370 // Blacklists for cases where escaped_refs_ can't be used.
371 EscapedIFieldClobberSet escaped_ifield_clobber_set_;
372 EscapedArrayClobberSet escaped_array_clobber_set_;
Vladimir Marko2ac01fc2014-05-22 12:09:08 +0100373
374 // Range check and null check elimination.
375 RangeCheckSet range_checked_;
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000376 ValueNameSet null_checked_;
377
Vladimir Marko95a05972014-05-30 10:01:32 +0100378 // Reuse one vector for all merges to avoid leaking too much memory on the ArenaStack.
379 ScopedArenaVector<BasicBlockId> merge_names_;
380 // Map to identify when different locations merge the same values.
381 ScopedArenaSafeMap<ScopedArenaVector<BasicBlockId>, uint16_t> merge_map_;
382 // New memory version for merge, kNoValue if all memory versions matched.
383 uint16_t merge_new_memory_version_;
384
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000385 DISALLOW_COPY_AND_ASSIGN(LocalValueNumbering);
buzbee2502e002012-12-31 16:05:53 -0800386};
387
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700388} // namespace art
buzbee2502e002012-12-31 16:05:53 -0800389
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700390#endif // ART_COMPILER_DEX_LOCAL_VALUE_NUMBERING_H_