blob: 7ab77b733fd417ac090c928faaa0bb53aad090f5 [file] [log] [blame]
Vladimir Marko95a05972014-05-30 10:01:32 +01001/*
2 * Copyright (C) 2014 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
17#ifndef ART_COMPILER_DEX_GLOBAL_VALUE_NUMBERING_H_
18#define ART_COMPILER_DEX_GLOBAL_VALUE_NUMBERING_H_
19
20#include "base/macros.h"
21#include "compiler_internals.h"
22#include "utils/scoped_arena_containers.h"
23
24namespace art {
25
26class LocalValueNumbering;
27class MirFieldInfo;
28
29class GlobalValueNumbering {
30 public:
31 GlobalValueNumbering(CompilationUnit* cu, ScopedArenaAllocator* allocator);
32 ~GlobalValueNumbering();
33
34 LocalValueNumbering* PrepareBasicBlock(BasicBlock* bb);
35 bool FinishBasicBlock(BasicBlock* bb);
36
37 // Checks that the value names didn't overflow.
38 bool Good() const {
39 return last_value_ < kNoValue;
40 }
41
42 // Allow modifications.
43 void AllowModifications() {
44 DCHECK(Good());
45 cu_->mir_graph->ClearAllVisitedFlags();
46 modifications_allowed_ = true;
47 }
48
49 bool CanModify() const {
50 // TODO: DCHECK(Good()), see AllowModifications() and NewValueName().
51 return modifications_allowed_ && Good();
52 }
53
54 // GlobalValueNumbering should be allocated on the ArenaStack (or the native stack).
55 static void* operator new(size_t size, ScopedArenaAllocator* allocator) {
56 return allocator->Alloc(sizeof(GlobalValueNumbering), kArenaAllocMIR);
57 }
58
59 // Allow delete-expression to destroy a GlobalValueNumbering object without deallocation.
60 static void operator delete(void* ptr) { UNUSED(ptr); }
61
62 private:
63 static constexpr uint16_t kNoValue = 0xffffu;
64
65 // Allocate a new value name.
66 uint16_t NewValueName() {
67 // TODO: No new values should be needed once we allow modifications.
68 // DCHECK(!modifications_allowed_);
69 ++last_value_;
70 return last_value_;
71 }
72
73 // Key is concatenation of opcode, operand1, operand2 and modifier, value is value name.
74 typedef ScopedArenaSafeMap<uint64_t, uint16_t> ValueMap;
75
76 static uint64_t BuildKey(uint16_t op, uint16_t operand1, uint16_t operand2, uint16_t modifier) {
77 return (static_cast<uint64_t>(op) << 48 | static_cast<uint64_t>(operand1) << 32 |
78 static_cast<uint64_t>(operand2) << 16 | static_cast<uint64_t>(modifier));
79 };
80
81 // Look up a value in the global value map, adding a new entry if there was none before.
82 uint16_t LookupValue(uint16_t op, uint16_t operand1, uint16_t operand2, uint16_t modifier) {
83 uint16_t res;
84 uint64_t key = BuildKey(op, operand1, operand2, modifier);
85 ValueMap::iterator lb = global_value_map_.lower_bound(key);
86 if (lb != global_value_map_.end() && lb->first == key) {
87 res = lb->second;
88 } else {
89 res = NewValueName();
90 global_value_map_.PutBefore(lb, key, res);
91 }
92 return res;
93 };
94
95 // Check if the exact value is stored in the global value map.
96 bool HasValue(uint16_t op, uint16_t operand1, uint16_t operand2, uint16_t modifier,
97 uint16_t value) const {
98 DCHECK(value != 0u || !Good());
99 DCHECK_LE(value, last_value_);
100 // This is equivalent to value == LookupValue(op, operand1, operand2, modifier)
101 // except that it doesn't add an entry to the global value map if it's not there.
102 uint64_t key = BuildKey(op, operand1, operand2, modifier);
103 ValueMap::const_iterator it = global_value_map_.find(key);
104 return (it != global_value_map_.end() && it->second == value);
105 };
106
107 // FieldReference represents a unique resolved field.
108 struct FieldReference {
109 const DexFile* dex_file;
110 uint16_t field_idx;
111 uint16_t type; // See comments for LocalValueNumbering::kFieldTypeCount.
112 };
113
114 struct FieldReferenceComparator {
115 bool operator()(const FieldReference& lhs, const FieldReference& rhs) const {
116 if (lhs.field_idx != rhs.field_idx) {
117 return lhs.field_idx < rhs.field_idx;
118 }
119 // If the field_idx and dex_file match, the type must also match.
120 DCHECK(lhs.dex_file != rhs.dex_file || lhs.type == rhs.type);
121 return lhs.dex_file < rhs.dex_file;
122 }
123 };
124
125 // Maps field key to field id for resolved fields.
126 typedef ScopedArenaSafeMap<FieldReference, uint32_t, FieldReferenceComparator> FieldIndexMap;
127
128 // Get a field id.
129 uint16_t GetFieldId(const MirFieldInfo& field_info, uint16_t type);
130
131 // Get a field type based on field id.
132 uint16_t GetFieldType(uint16_t field_id) {
133 DCHECK_LT(field_id, field_index_reverse_map_.size());
134 return field_index_reverse_map_[field_id]->first.type;
135 }
136
137 struct ArrayLocation {
138 uint16_t base;
139 uint16_t index;
140 };
141
142 struct ArrayLocationComparator {
143 bool operator()(const ArrayLocation& lhs, const ArrayLocation& rhs) const {
144 if (lhs.base != rhs.base) {
145 return lhs.base < rhs.base;
146 }
147 return lhs.index < rhs.index;
148 }
149 };
150
151 typedef ScopedArenaSafeMap<ArrayLocation, uint16_t, ArrayLocationComparator> ArrayLocationMap;
152
153 // Get an array location.
154 uint16_t GetArrayLocation(uint16_t base, uint16_t index);
155
156 // Get the array base from an array location.
157 uint16_t GetArrayLocationBase(uint16_t location) const {
158 return array_location_reverse_map_[location]->first.base;
159 }
160
161 // Get the array index from an array location.
162 uint16_t GetArrayLocationIndex(uint16_t location) const {
163 return array_location_reverse_map_[location]->first.index;
164 }
165
166 // A set of value names.
167 typedef ScopedArenaSet<uint16_t> ValueNameSet;
168
169 // A map from a set of references to the set id.
170 typedef ScopedArenaSafeMap<ValueNameSet, uint16_t> RefSetIdMap;
171
172 uint16_t GetRefSetId(const ValueNameSet& ref_set) {
173 uint16_t res = kNoValue;
174 auto lb = ref_set_map_.lower_bound(ref_set);
175 if (lb != ref_set_map_.end() && !ref_set_map_.key_comp()(ref_set, lb->first)) {
176 res = lb->second;
177 } else {
178 res = NewValueName();
179 ref_set_map_.PutBefore(lb, ref_set, res);
180 }
181 return res;
182 }
183
184 const BasicBlock* GetBasicBlock(uint16_t bb_id) const {
185 return cu_->mir_graph->GetBasicBlock(bb_id);
186 }
187
188 static bool HasNullCheckLastInsn(const BasicBlock* pred_bb, BasicBlockId succ_id);
189
190 bool NullCheckedInAllPredecessors(const ScopedArenaVector<uint16_t>& merge_names) const;
191
192 CompilationUnit* GetCompilationUnit() const {
193 return cu_;
194 }
195
196 MIRGraph* GetMirGraph() const {
197 return cu_->mir_graph.get();
198 }
199
200 ScopedArenaAllocator* Allocator() const {
201 return allocator_;
202 }
203
204 CompilationUnit* const cu_;
205 ScopedArenaAllocator* const allocator_;
206
207 static constexpr uint32_t kMaxRepeatCount = 10u;
208
209 // Track the repeat count to make sure the GVN converges quickly and abort the GVN otherwise.
210 uint32_t repeat_count_;
211
212 // We have 32-bit last_value_ so that we can detect when we run out of value names, see Good().
213 // We usually don't check Good() until the end of LVN unless we're about to modify code.
214 uint32_t last_value_;
215
216 // Marks whether code modifications are allowed. The initial GVN is done without code
217 // modifications to settle the value names. Afterwards, we allow modifications and rerun
218 // LVN once for each BasicBlock.
219 bool modifications_allowed_;
220
221 ValueMap global_value_map_;
222 FieldIndexMap field_index_map_;
223 ScopedArenaVector<const FieldIndexMap::value_type*> field_index_reverse_map_;
224 ArrayLocationMap array_location_map_;
225 ScopedArenaVector<const ArrayLocationMap::value_type*> array_location_reverse_map_;
226 RefSetIdMap ref_set_map_;
227
228 ScopedArenaVector<const LocalValueNumbering*> lvns_; // Owning.
229 std::unique_ptr<LocalValueNumbering> work_lvn_;
230 ScopedArenaVector<const LocalValueNumbering*> merge_lvns_; // Not owning.
231
232 friend class LocalValueNumbering;
233
234 DISALLOW_COPY_AND_ASSIGN(GlobalValueNumbering);
235};
236
237} // namespace art
238
239#endif // ART_COMPILER_DEX_GLOBAL_VALUE_NUMBERING_H_