blob: df554cdade11fe473b4effc4f4b804c1999c8864 [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:
Vladimir Marko415ac882014-09-30 18:09:14 +010031 enum Mode {
32 kModeGvn,
33 kModeGvnPostProcessing,
34 kModeLvn
35 };
36
37 static bool Skip(CompilationUnit* cu) {
38 return (cu->disable_opt & (1u << kGlobalValueNumbering)) != 0u ||
39 cu->mir_graph->GetMaxNestedLoops() > kMaxAllowedNestedLoops;
40 }
41
42 GlobalValueNumbering(CompilationUnit* cu, ScopedArenaAllocator* allocator, Mode mode);
Vladimir Marko95a05972014-05-30 10:01:32 +010043 ~GlobalValueNumbering();
44
Vladimir Marko55fff042014-07-10 12:42:52 +010045 // Prepare LVN for the basic block.
Vladimir Markob19955d2014-07-29 12:04:10 +010046 LocalValueNumbering* PrepareBasicBlock(BasicBlock* bb,
47 ScopedArenaAllocator* allocator = nullptr);
Vladimir Marko55fff042014-07-10 12:42:52 +010048
49 // Finish processing the basic block.
Vladimir Marko95a05972014-05-30 10:01:32 +010050 bool FinishBasicBlock(BasicBlock* bb);
51
52 // Checks that the value names didn't overflow.
53 bool Good() const {
54 return last_value_ < kNoValue;
55 }
56
57 // Allow modifications.
Vladimir Marko415ac882014-09-30 18:09:14 +010058 void StartPostProcessing() {
Vladimir Marko95a05972014-05-30 10:01:32 +010059 DCHECK(Good());
Vladimir Marko415ac882014-09-30 18:09:14 +010060 DCHECK_EQ(mode_, kModeGvn);
61 mode_ = kModeGvnPostProcessing;
Vladimir Marko95a05972014-05-30 10:01:32 +010062 }
63
64 bool CanModify() const {
Vladimir Marko95a05972014-05-30 10:01:32 +010065 return modifications_allowed_ && Good();
66 }
67
68 // GlobalValueNumbering should be allocated on the ArenaStack (or the native stack).
69 static void* operator new(size_t size, ScopedArenaAllocator* allocator) {
Vladimir Markob19955d2014-07-29 12:04:10 +010070 return allocator->Alloc(sizeof(GlobalValueNumbering), kArenaAllocMisc);
Vladimir Marko95a05972014-05-30 10:01:32 +010071 }
72
73 // Allow delete-expression to destroy a GlobalValueNumbering object without deallocation.
74 static void operator delete(void* ptr) { UNUSED(ptr); }
75
76 private:
77 static constexpr uint16_t kNoValue = 0xffffu;
78
79 // Allocate a new value name.
80 uint16_t NewValueName() {
Vladimir Marko415ac882014-09-30 18:09:14 +010081 DCHECK_NE(mode_, kModeGvnPostProcessing);
Vladimir Marko95a05972014-05-30 10:01:32 +010082 ++last_value_;
83 return last_value_;
84 }
85
86 // Key is concatenation of opcode, operand1, operand2 and modifier, value is value name.
87 typedef ScopedArenaSafeMap<uint64_t, uint16_t> ValueMap;
88
89 static uint64_t BuildKey(uint16_t op, uint16_t operand1, uint16_t operand2, uint16_t modifier) {
90 return (static_cast<uint64_t>(op) << 48 | static_cast<uint64_t>(operand1) << 32 |
91 static_cast<uint64_t>(operand2) << 16 | static_cast<uint64_t>(modifier));
Andreas Gampec8ccf682014-09-29 20:07:43 -070092 }
Vladimir Marko95a05972014-05-30 10:01:32 +010093
94 // Look up a value in the global value map, adding a new entry if there was none before.
95 uint16_t LookupValue(uint16_t op, uint16_t operand1, uint16_t operand2, uint16_t modifier) {
96 uint16_t res;
97 uint64_t key = BuildKey(op, operand1, operand2, modifier);
98 ValueMap::iterator lb = global_value_map_.lower_bound(key);
99 if (lb != global_value_map_.end() && lb->first == key) {
100 res = lb->second;
101 } else {
102 res = NewValueName();
103 global_value_map_.PutBefore(lb, key, res);
104 }
105 return res;
Andreas Gampec8ccf682014-09-29 20:07:43 -0700106 }
Vladimir Marko95a05972014-05-30 10:01:32 +0100107
108 // Check if the exact value is stored in the global value map.
109 bool HasValue(uint16_t op, uint16_t operand1, uint16_t operand2, uint16_t modifier,
110 uint16_t value) const {
111 DCHECK(value != 0u || !Good());
112 DCHECK_LE(value, last_value_);
113 // This is equivalent to value == LookupValue(op, operand1, operand2, modifier)
114 // except that it doesn't add an entry to the global value map if it's not there.
115 uint64_t key = BuildKey(op, operand1, operand2, modifier);
116 ValueMap::const_iterator it = global_value_map_.find(key);
117 return (it != global_value_map_.end() && it->second == value);
Andreas Gampec8ccf682014-09-29 20:07:43 -0700118 }
Vladimir Marko95a05972014-05-30 10:01:32 +0100119
120 // FieldReference represents a unique resolved field.
121 struct FieldReference {
122 const DexFile* dex_file;
123 uint16_t field_idx;
124 uint16_t type; // See comments for LocalValueNumbering::kFieldTypeCount.
125 };
126
127 struct FieldReferenceComparator {
128 bool operator()(const FieldReference& lhs, const FieldReference& rhs) const {
129 if (lhs.field_idx != rhs.field_idx) {
130 return lhs.field_idx < rhs.field_idx;
131 }
132 // If the field_idx and dex_file match, the type must also match.
133 DCHECK(lhs.dex_file != rhs.dex_file || lhs.type == rhs.type);
134 return lhs.dex_file < rhs.dex_file;
135 }
136 };
137
138 // Maps field key to field id for resolved fields.
139 typedef ScopedArenaSafeMap<FieldReference, uint32_t, FieldReferenceComparator> FieldIndexMap;
140
141 // Get a field id.
142 uint16_t GetFieldId(const MirFieldInfo& field_info, uint16_t type);
143
144 // Get a field type based on field id.
145 uint16_t GetFieldType(uint16_t field_id) {
146 DCHECK_LT(field_id, field_index_reverse_map_.size());
147 return field_index_reverse_map_[field_id]->first.type;
148 }
149
150 struct ArrayLocation {
151 uint16_t base;
152 uint16_t index;
153 };
154
155 struct ArrayLocationComparator {
156 bool operator()(const ArrayLocation& lhs, const ArrayLocation& rhs) const {
157 if (lhs.base != rhs.base) {
158 return lhs.base < rhs.base;
159 }
160 return lhs.index < rhs.index;
161 }
162 };
163
164 typedef ScopedArenaSafeMap<ArrayLocation, uint16_t, ArrayLocationComparator> ArrayLocationMap;
165
166 // Get an array location.
167 uint16_t GetArrayLocation(uint16_t base, uint16_t index);
168
169 // Get the array base from an array location.
170 uint16_t GetArrayLocationBase(uint16_t location) const {
171 return array_location_reverse_map_[location]->first.base;
172 }
173
174 // Get the array index from an array location.
175 uint16_t GetArrayLocationIndex(uint16_t location) const {
176 return array_location_reverse_map_[location]->first.index;
177 }
178
179 // A set of value names.
180 typedef ScopedArenaSet<uint16_t> ValueNameSet;
181
182 // A map from a set of references to the set id.
183 typedef ScopedArenaSafeMap<ValueNameSet, uint16_t> RefSetIdMap;
184
185 uint16_t GetRefSetId(const ValueNameSet& ref_set) {
186 uint16_t res = kNoValue;
187 auto lb = ref_set_map_.lower_bound(ref_set);
188 if (lb != ref_set_map_.end() && !ref_set_map_.key_comp()(ref_set, lb->first)) {
189 res = lb->second;
190 } else {
191 res = NewValueName();
192 ref_set_map_.PutBefore(lb, ref_set, res);
193 }
194 return res;
195 }
196
197 const BasicBlock* GetBasicBlock(uint16_t bb_id) const {
Vladimir Marko55fff042014-07-10 12:42:52 +0100198 return mir_graph_->GetBasicBlock(bb_id);
Vladimir Marko95a05972014-05-30 10:01:32 +0100199 }
200
201 static bool HasNullCheckLastInsn(const BasicBlock* pred_bb, BasicBlockId succ_id);
202
203 bool NullCheckedInAllPredecessors(const ScopedArenaVector<uint16_t>& merge_names) const;
204
205 CompilationUnit* GetCompilationUnit() const {
206 return cu_;
207 }
208
209 MIRGraph* GetMirGraph() const {
Vladimir Marko55fff042014-07-10 12:42:52 +0100210 return mir_graph_;
Vladimir Marko95a05972014-05-30 10:01:32 +0100211 }
212
213 ScopedArenaAllocator* Allocator() const {
214 return allocator_;
215 }
216
217 CompilationUnit* const cu_;
Vladimir Marko55fff042014-07-10 12:42:52 +0100218 MIRGraph* mir_graph_;
Vladimir Marko95a05972014-05-30 10:01:32 +0100219 ScopedArenaAllocator* const allocator_;
220
Vladimir Marko415ac882014-09-30 18:09:14 +0100221 // The maximum number of nested loops that we accept for GVN.
222 static constexpr size_t kMaxAllowedNestedLoops = 6u;
223
Vladimir Marko55fff042014-07-10 12:42:52 +0100224 // The number of BBs that we need to process grows exponentially with the number
225 // of nested loops. Don't allow excessive processing for too many nested loops or
226 // otherwise expensive methods.
227 static constexpr uint32_t kMaxBbsToProcessMultiplyFactor = 20u;
Vladimir Marko95a05972014-05-30 10:01:32 +0100228
Vladimir Marko55fff042014-07-10 12:42:52 +0100229 uint32_t bbs_processed_;
Vladimir Marko2d2365c2014-08-19 18:08:39 +0100230 uint32_t max_bbs_to_process_; // Doesn't apply after the main GVN has converged.
Vladimir Marko95a05972014-05-30 10:01:32 +0100231
232 // We have 32-bit last_value_ so that we can detect when we run out of value names, see Good().
233 // We usually don't check Good() until the end of LVN unless we're about to modify code.
234 uint32_t last_value_;
235
236 // Marks whether code modifications are allowed. The initial GVN is done without code
237 // modifications to settle the value names. Afterwards, we allow modifications and rerun
238 // LVN once for each BasicBlock.
239 bool modifications_allowed_;
240
Vladimir Marko415ac882014-09-30 18:09:14 +0100241 // Specifies the mode of operation.
242 Mode mode_;
243
Vladimir Marko95a05972014-05-30 10:01:32 +0100244 ValueMap global_value_map_;
245 FieldIndexMap field_index_map_;
246 ScopedArenaVector<const FieldIndexMap::value_type*> field_index_reverse_map_;
247 ArrayLocationMap array_location_map_;
248 ScopedArenaVector<const ArrayLocationMap::value_type*> array_location_reverse_map_;
249 RefSetIdMap ref_set_map_;
250
251 ScopedArenaVector<const LocalValueNumbering*> lvns_; // Owning.
252 std::unique_ptr<LocalValueNumbering> work_lvn_;
253 ScopedArenaVector<const LocalValueNumbering*> merge_lvns_; // Not owning.
254
255 friend class LocalValueNumbering;
256
257 DISALLOW_COPY_AND_ASSIGN(GlobalValueNumbering);
258};
259
260} // namespace art
261
262#endif // ART_COMPILER_DEX_GLOBAL_VALUE_NUMBERING_H_