Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 1 | /* |
| 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" |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 22 | #include "utils/arena_object.h" |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 23 | |
| 24 | namespace art { |
| 25 | |
| 26 | class LocalValueNumbering; |
| 27 | class MirFieldInfo; |
| 28 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 29 | class GlobalValueNumbering : public DeletableArenaObject<kArenaAllocMisc> { |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 30 | public: |
Vladimir Marko | 415ac88 | 2014-09-30 18:09:14 +0100 | [diff] [blame] | 31 | 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 | |
Vladimir Marko | af6925b | 2014-10-31 16:37:32 +0000 | [diff] [blame] | 42 | // Instance and static field id map is held by MIRGraph to avoid multiple recalculations |
| 43 | // when doing LVN. |
| 44 | template <typename Container> // Container of MirIFieldLoweringInfo or MirSFieldLoweringInfo. |
| 45 | static uint16_t* PrepareGvnFieldIds(ScopedArenaAllocator* allocator, |
| 46 | const Container& field_infos); |
| 47 | |
Vladimir Marko | 415ac88 | 2014-09-30 18:09:14 +0100 | [diff] [blame] | 48 | GlobalValueNumbering(CompilationUnit* cu, ScopedArenaAllocator* allocator, Mode mode); |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 49 | ~GlobalValueNumbering(); |
| 50 | |
Vladimir Marko | 55fff04 | 2014-07-10 12:42:52 +0100 | [diff] [blame] | 51 | // Prepare LVN for the basic block. |
Vladimir Marko | b19955d | 2014-07-29 12:04:10 +0100 | [diff] [blame] | 52 | LocalValueNumbering* PrepareBasicBlock(BasicBlock* bb, |
| 53 | ScopedArenaAllocator* allocator = nullptr); |
Vladimir Marko | 55fff04 | 2014-07-10 12:42:52 +0100 | [diff] [blame] | 54 | |
| 55 | // Finish processing the basic block. |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 56 | bool FinishBasicBlock(BasicBlock* bb); |
| 57 | |
| 58 | // Checks that the value names didn't overflow. |
| 59 | bool Good() const { |
| 60 | return last_value_ < kNoValue; |
| 61 | } |
| 62 | |
| 63 | // Allow modifications. |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 64 | void StartPostProcessing(); |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 65 | |
| 66 | bool CanModify() const { |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 67 | return modifications_allowed_ && Good(); |
| 68 | } |
| 69 | |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 70 | private: |
| 71 | static constexpr uint16_t kNoValue = 0xffffu; |
| 72 | |
| 73 | // Allocate a new value name. |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 74 | uint16_t NewValueName(); |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 75 | |
| 76 | // Key is concatenation of opcode, operand1, operand2 and modifier, value is value name. |
| 77 | typedef ScopedArenaSafeMap<uint64_t, uint16_t> ValueMap; |
| 78 | |
| 79 | static uint64_t BuildKey(uint16_t op, uint16_t operand1, uint16_t operand2, uint16_t modifier) { |
| 80 | return (static_cast<uint64_t>(op) << 48 | static_cast<uint64_t>(operand1) << 32 | |
| 81 | static_cast<uint64_t>(operand2) << 16 | static_cast<uint64_t>(modifier)); |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 82 | } |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 83 | |
| 84 | // Look up a value in the global value map, adding a new entry if there was none before. |
| 85 | uint16_t LookupValue(uint16_t op, uint16_t operand1, uint16_t operand2, uint16_t modifier) { |
| 86 | uint16_t res; |
| 87 | uint64_t key = BuildKey(op, operand1, operand2, modifier); |
| 88 | ValueMap::iterator lb = global_value_map_.lower_bound(key); |
| 89 | if (lb != global_value_map_.end() && lb->first == key) { |
| 90 | res = lb->second; |
| 91 | } else { |
| 92 | res = NewValueName(); |
| 93 | global_value_map_.PutBefore(lb, key, res); |
| 94 | } |
| 95 | return res; |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 96 | } |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 97 | |
Vladimir Marko | a4426cf | 2014-10-22 17:15:53 +0100 | [diff] [blame] | 98 | // Look up a value in the global value map, don't add a new entry if there was none before. |
| 99 | uint16_t FindValue(uint16_t op, uint16_t operand1, uint16_t operand2, uint16_t modifier) { |
| 100 | uint16_t res; |
| 101 | uint64_t key = BuildKey(op, operand1, operand2, modifier); |
| 102 | ValueMap::iterator lb = global_value_map_.lower_bound(key); |
| 103 | if (lb != global_value_map_.end() && lb->first == key) { |
| 104 | res = lb->second; |
| 105 | } else { |
| 106 | res = kNoValue; |
| 107 | } |
| 108 | return res; |
| 109 | } |
| 110 | |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 111 | // Check if the exact value is stored in the global value map. |
| 112 | bool HasValue(uint16_t op, uint16_t operand1, uint16_t operand2, uint16_t modifier, |
| 113 | uint16_t value) const { |
| 114 | DCHECK(value != 0u || !Good()); |
| 115 | DCHECK_LE(value, last_value_); |
| 116 | // This is equivalent to value == LookupValue(op, operand1, operand2, modifier) |
| 117 | // except that it doesn't add an entry to the global value map if it's not there. |
| 118 | uint64_t key = BuildKey(op, operand1, operand2, modifier); |
| 119 | ValueMap::const_iterator it = global_value_map_.find(key); |
| 120 | return (it != global_value_map_.end() && it->second == value); |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 121 | } |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 122 | |
Vladimir Marko | af6925b | 2014-10-31 16:37:32 +0000 | [diff] [blame] | 123 | // Get an instance field id. |
| 124 | uint16_t GetIFieldId(MIR* mir) { |
| 125 | return GetMirGraph()->GetGvnIFieldId(mir); |
| 126 | } |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 127 | |
Vladimir Marko | af6925b | 2014-10-31 16:37:32 +0000 | [diff] [blame] | 128 | // Get a static field id. |
| 129 | uint16_t GetSFieldId(MIR* mir) { |
| 130 | return GetMirGraph()->GetGvnSFieldId(mir); |
| 131 | } |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 132 | |
Vladimir Marko | af6925b | 2014-10-31 16:37:32 +0000 | [diff] [blame] | 133 | // Get an instance field type based on field id. |
| 134 | uint16_t GetIFieldType(uint16_t field_id) { |
| 135 | return static_cast<uint16_t>(GetMirGraph()->GetIFieldLoweringInfo(field_id).MemAccessType()); |
| 136 | } |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 137 | |
Vladimir Marko | af6925b | 2014-10-31 16:37:32 +0000 | [diff] [blame] | 138 | // Get a static field type based on field id. |
| 139 | uint16_t GetSFieldType(uint16_t field_id) { |
| 140 | return static_cast<uint16_t>(GetMirGraph()->GetSFieldLoweringInfo(field_id).MemAccessType()); |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | struct ArrayLocation { |
| 144 | uint16_t base; |
| 145 | uint16_t index; |
| 146 | }; |
| 147 | |
| 148 | struct ArrayLocationComparator { |
| 149 | bool operator()(const ArrayLocation& lhs, const ArrayLocation& rhs) const { |
| 150 | if (lhs.base != rhs.base) { |
| 151 | return lhs.base < rhs.base; |
| 152 | } |
| 153 | return lhs.index < rhs.index; |
| 154 | } |
| 155 | }; |
| 156 | |
| 157 | typedef ScopedArenaSafeMap<ArrayLocation, uint16_t, ArrayLocationComparator> ArrayLocationMap; |
| 158 | |
| 159 | // Get an array location. |
| 160 | uint16_t GetArrayLocation(uint16_t base, uint16_t index); |
| 161 | |
| 162 | // Get the array base from an array location. |
| 163 | uint16_t GetArrayLocationBase(uint16_t location) const { |
| 164 | return array_location_reverse_map_[location]->first.base; |
| 165 | } |
| 166 | |
| 167 | // Get the array index from an array location. |
| 168 | uint16_t GetArrayLocationIndex(uint16_t location) const { |
| 169 | return array_location_reverse_map_[location]->first.index; |
| 170 | } |
| 171 | |
| 172 | // A set of value names. |
| 173 | typedef ScopedArenaSet<uint16_t> ValueNameSet; |
| 174 | |
| 175 | // A map from a set of references to the set id. |
| 176 | typedef ScopedArenaSafeMap<ValueNameSet, uint16_t> RefSetIdMap; |
| 177 | |
| 178 | uint16_t GetRefSetId(const ValueNameSet& ref_set) { |
| 179 | uint16_t res = kNoValue; |
| 180 | auto lb = ref_set_map_.lower_bound(ref_set); |
| 181 | if (lb != ref_set_map_.end() && !ref_set_map_.key_comp()(ref_set, lb->first)) { |
| 182 | res = lb->second; |
| 183 | } else { |
| 184 | res = NewValueName(); |
| 185 | ref_set_map_.PutBefore(lb, ref_set, res); |
| 186 | } |
| 187 | return res; |
| 188 | } |
| 189 | |
| 190 | const BasicBlock* GetBasicBlock(uint16_t bb_id) const { |
Vladimir Marko | 55fff04 | 2014-07-10 12:42:52 +0100 | [diff] [blame] | 191 | return mir_graph_->GetBasicBlock(bb_id); |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | static bool HasNullCheckLastInsn(const BasicBlock* pred_bb, BasicBlockId succ_id); |
| 195 | |
| 196 | bool NullCheckedInAllPredecessors(const ScopedArenaVector<uint16_t>& merge_names) const; |
| 197 | |
Razvan A Lupusoru | e095114 | 2014-11-14 14:36:55 -0800 | [diff] [blame] | 198 | bool DivZeroCheckedInAllPredecessors(const ScopedArenaVector<uint16_t>& merge_names) const; |
| 199 | |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 200 | CompilationUnit* GetCompilationUnit() const { |
| 201 | return cu_; |
| 202 | } |
| 203 | |
| 204 | MIRGraph* GetMirGraph() const { |
Vladimir Marko | 55fff04 | 2014-07-10 12:42:52 +0100 | [diff] [blame] | 205 | return mir_graph_; |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | ScopedArenaAllocator* Allocator() const { |
| 209 | return allocator_; |
| 210 | } |
| 211 | |
| 212 | CompilationUnit* const cu_; |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 213 | MIRGraph* const mir_graph_; |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 214 | ScopedArenaAllocator* const allocator_; |
| 215 | |
Vladimir Marko | 415ac88 | 2014-09-30 18:09:14 +0100 | [diff] [blame] | 216 | // The maximum number of nested loops that we accept for GVN. |
| 217 | static constexpr size_t kMaxAllowedNestedLoops = 6u; |
| 218 | |
Vladimir Marko | 55fff04 | 2014-07-10 12:42:52 +0100 | [diff] [blame] | 219 | // The number of BBs that we need to process grows exponentially with the number |
| 220 | // of nested loops. Don't allow excessive processing for too many nested loops or |
| 221 | // otherwise expensive methods. |
| 222 | static constexpr uint32_t kMaxBbsToProcessMultiplyFactor = 20u; |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 223 | |
Vladimir Marko | 55fff04 | 2014-07-10 12:42:52 +0100 | [diff] [blame] | 224 | uint32_t bbs_processed_; |
Vladimir Marko | 2d2365c | 2014-08-19 18:08:39 +0100 | [diff] [blame] | 225 | uint32_t max_bbs_to_process_; // Doesn't apply after the main GVN has converged. |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 226 | |
| 227 | // We have 32-bit last_value_ so that we can detect when we run out of value names, see Good(). |
| 228 | // We usually don't check Good() until the end of LVN unless we're about to modify code. |
| 229 | uint32_t last_value_; |
| 230 | |
| 231 | // Marks whether code modifications are allowed. The initial GVN is done without code |
| 232 | // modifications to settle the value names. Afterwards, we allow modifications and rerun |
| 233 | // LVN once for each BasicBlock. |
| 234 | bool modifications_allowed_; |
| 235 | |
Vladimir Marko | 415ac88 | 2014-09-30 18:09:14 +0100 | [diff] [blame] | 236 | // Specifies the mode of operation. |
| 237 | Mode mode_; |
| 238 | |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 239 | ValueMap global_value_map_; |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 240 | ArrayLocationMap array_location_map_; |
| 241 | ScopedArenaVector<const ArrayLocationMap::value_type*> array_location_reverse_map_; |
| 242 | RefSetIdMap ref_set_map_; |
| 243 | |
| 244 | ScopedArenaVector<const LocalValueNumbering*> lvns_; // Owning. |
| 245 | std::unique_ptr<LocalValueNumbering> work_lvn_; |
| 246 | ScopedArenaVector<const LocalValueNumbering*> merge_lvns_; // Not owning. |
| 247 | |
| 248 | friend class LocalValueNumbering; |
Vladimir Marko | a4426cf | 2014-10-22 17:15:53 +0100 | [diff] [blame] | 249 | friend class GlobalValueNumberingTest; |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 250 | |
| 251 | DISALLOW_COPY_AND_ASSIGN(GlobalValueNumbering); |
| 252 | }; |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 253 | std::ostream& operator<<(std::ostream& os, const GlobalValueNumbering::Mode& rhs); |
| 254 | |
| 255 | inline void GlobalValueNumbering::StartPostProcessing() { |
| 256 | DCHECK(Good()); |
| 257 | DCHECK_EQ(mode_, kModeGvn); |
| 258 | mode_ = kModeGvnPostProcessing; |
| 259 | } |
| 260 | |
| 261 | inline uint16_t GlobalValueNumbering::NewValueName() { |
| 262 | DCHECK_NE(mode_, kModeGvnPostProcessing); |
| 263 | ++last_value_; |
| 264 | return last_value_; |
| 265 | } |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 266 | |
Vladimir Marko | af6925b | 2014-10-31 16:37:32 +0000 | [diff] [blame] | 267 | template <typename Container> // Container of MirIFieldLoweringInfo or MirSFieldLoweringInfo. |
| 268 | uint16_t* GlobalValueNumbering::PrepareGvnFieldIds(ScopedArenaAllocator* allocator, |
| 269 | const Container& field_infos) { |
| 270 | size_t size = field_infos.size(); |
| 271 | uint16_t* field_ids = reinterpret_cast<uint16_t*>(allocator->Alloc(size * sizeof(uint16_t), |
| 272 | kArenaAllocMisc)); |
| 273 | for (size_t i = 0u; i != size; ++i) { |
| 274 | size_t idx = i; |
| 275 | const MirFieldInfo& cur_info = field_infos[i]; |
| 276 | if (cur_info.IsResolved()) { |
| 277 | for (size_t j = 0; j != i; ++j) { |
| 278 | const MirFieldInfo& prev_info = field_infos[j]; |
| 279 | if (prev_info.IsResolved() && |
| 280 | prev_info.DeclaringDexFile() == cur_info.DeclaringDexFile() && |
| 281 | prev_info.DeclaringFieldIndex() == cur_info.DeclaringFieldIndex()) { |
| 282 | DCHECK_EQ(cur_info.MemAccessType(), prev_info.MemAccessType()); |
| 283 | idx = j; |
| 284 | break; |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | field_ids[i] = idx; |
| 289 | } |
| 290 | return field_ids; |
| 291 | } |
| 292 | |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 293 | } // namespace art |
| 294 | |
| 295 | #endif // ART_COMPILER_DEX_GLOBAL_VALUE_NUMBERING_H_ |