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