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 | #include "global_value_numbering.h" |
| 18 | |
| 19 | #include "local_value_numbering.h" |
| 20 | |
| 21 | namespace art { |
| 22 | |
Vladimir Marko | 415ac88 | 2014-09-30 18:09:14 +0100 | [diff] [blame] | 23 | GlobalValueNumbering::GlobalValueNumbering(CompilationUnit* cu, ScopedArenaAllocator* allocator, |
| 24 | Mode mode) |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 25 | : cu_(cu), |
Vladimir Marko | 55fff04 | 2014-07-10 12:42:52 +0100 | [diff] [blame] | 26 | mir_graph_(cu->mir_graph.get()), |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 27 | allocator_(allocator), |
Vladimir Marko | 55fff04 | 2014-07-10 12:42:52 +0100 | [diff] [blame] | 28 | bbs_processed_(0u), |
| 29 | max_bbs_to_process_(kMaxBbsToProcessMultiplyFactor * mir_graph_->GetNumReachableBlocks()), |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 30 | last_value_(0u), |
Vladimir Marko | 415ac88 | 2014-09-30 18:09:14 +0100 | [diff] [blame] | 31 | modifications_allowed_(true), |
| 32 | mode_(mode), |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 33 | global_value_map_(std::less<uint64_t>(), allocator->Adapter()), |
| 34 | field_index_map_(FieldReferenceComparator(), allocator->Adapter()), |
| 35 | field_index_reverse_map_(allocator->Adapter()), |
| 36 | array_location_map_(ArrayLocationComparator(), allocator->Adapter()), |
| 37 | array_location_reverse_map_(allocator->Adapter()), |
| 38 | ref_set_map_(std::less<ValueNameSet>(), allocator->Adapter()), |
Vladimir Marko | 55fff04 | 2014-07-10 12:42:52 +0100 | [diff] [blame] | 39 | lvns_(mir_graph_->GetNumBlocks(), nullptr, allocator->Adapter()), |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 40 | work_lvn_(nullptr), |
| 41 | merge_lvns_(allocator->Adapter()) { |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | GlobalValueNumbering::~GlobalValueNumbering() { |
| 45 | STLDeleteElements(&lvns_); |
| 46 | } |
| 47 | |
Vladimir Marko | b19955d | 2014-07-29 12:04:10 +0100 | [diff] [blame] | 48 | LocalValueNumbering* GlobalValueNumbering::PrepareBasicBlock(BasicBlock* bb, |
| 49 | ScopedArenaAllocator* allocator) { |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 50 | if (UNLIKELY(!Good())) { |
| 51 | return nullptr; |
| 52 | } |
Vladimir Marko | 415ac88 | 2014-09-30 18:09:14 +0100 | [diff] [blame] | 53 | if (bb->block_type != kDalvikByteCode && bb->block_type != kEntryBlock) { |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 54 | DCHECK(bb->first_mir_insn == nullptr); |
| 55 | return nullptr; |
| 56 | } |
Vladimir Marko | 415ac88 | 2014-09-30 18:09:14 +0100 | [diff] [blame] | 57 | if (mode_ == kModeGvn && UNLIKELY(bbs_processed_ == max_bbs_to_process_)) { |
Vladimir Marko | 2d2365c | 2014-08-19 18:08:39 +0100 | [diff] [blame] | 58 | // If we're still trying to converge, stop now. Otherwise, proceed to apply optimizations. |
Vladimir Marko | 415ac88 | 2014-09-30 18:09:14 +0100 | [diff] [blame] | 59 | last_value_ = kNoValue; // Make bad. |
| 60 | return nullptr; |
| 61 | } |
| 62 | if (mode_ == kModeGvnPostProcessing && |
| 63 | mir_graph_->GetTopologicalSortOrderLoopHeadStack()->empty()) { |
| 64 | // Modifications outside loops are performed during the main phase. |
| 65 | return nullptr; |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 66 | } |
Vladimir Marko | b19955d | 2014-07-29 12:04:10 +0100 | [diff] [blame] | 67 | if (allocator == nullptr) { |
| 68 | allocator = allocator_; |
| 69 | } |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 70 | DCHECK(work_lvn_.get() == nullptr); |
Vladimir Marko | b19955d | 2014-07-29 12:04:10 +0100 | [diff] [blame] | 71 | work_lvn_.reset(new (allocator) LocalValueNumbering(this, bb->id, allocator)); |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 72 | if (bb->block_type == kEntryBlock) { |
Vladimir Marko | a4426cf | 2014-10-22 17:15:53 +0100 | [diff] [blame] | 73 | work_lvn_->PrepareEntryBlock(); |
Vladimir Marko | 415ac88 | 2014-09-30 18:09:14 +0100 | [diff] [blame] | 74 | DCHECK(bb->first_mir_insn == nullptr); // modifications_allowed_ is irrelevant. |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 75 | } else { |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 76 | // To avoid repeated allocation on the ArenaStack, reuse a single vector kept as a member. |
| 77 | DCHECK(merge_lvns_.empty()); |
Vladimir Marko | 55fff04 | 2014-07-10 12:42:52 +0100 | [diff] [blame] | 78 | // If we're running the full GVN, the RepeatingTopologicalSortIterator keeps the loop |
| 79 | // head stack in the MIRGraph up to date and for a loop head we need to check whether |
| 80 | // we're making the initial computation and need to merge only preceding blocks in the |
| 81 | // topological order, or we're recalculating a loop head and need to merge all incoming |
| 82 | // LVNs. When we're not at a loop head (including having an empty loop head stack) all |
| 83 | // predecessors should be preceding blocks and we shall merge all of them anyway. |
Vladimir Marko | 55fff04 | 2014-07-10 12:42:52 +0100 | [diff] [blame] | 84 | bool use_all_predecessors = true; |
| 85 | uint16_t loop_head_idx = 0u; // Used only if !use_all_predecessors. |
Vladimir Marko | 415ac88 | 2014-09-30 18:09:14 +0100 | [diff] [blame] | 86 | if (mode_ == kModeGvn && mir_graph_->GetTopologicalSortOrderLoopHeadStack()->size() != 0) { |
Vladimir Marko | 55fff04 | 2014-07-10 12:42:52 +0100 | [diff] [blame] | 87 | // Full GVN inside a loop, see if we're at the loop head for the first time. |
Vladimir Marko | 415ac88 | 2014-09-30 18:09:14 +0100 | [diff] [blame] | 88 | modifications_allowed_ = false; |
Vladimir Marko | e39c54e | 2014-09-22 14:50:02 +0100 | [diff] [blame] | 89 | auto top = mir_graph_->GetTopologicalSortOrderLoopHeadStack()->back(); |
Vladimir Marko | 55fff04 | 2014-07-10 12:42:52 +0100 | [diff] [blame] | 90 | loop_head_idx = top.first; |
| 91 | bool recalculating = top.second; |
| 92 | use_all_predecessors = recalculating || |
Vladimir Marko | e39c54e | 2014-09-22 14:50:02 +0100 | [diff] [blame] | 93 | loop_head_idx != mir_graph_->GetTopologicalSortOrderIndexes()[bb->id]; |
Vladimir Marko | 415ac88 | 2014-09-30 18:09:14 +0100 | [diff] [blame] | 94 | } else { |
| 95 | modifications_allowed_ = true; |
Vladimir Marko | 55fff04 | 2014-07-10 12:42:52 +0100 | [diff] [blame] | 96 | } |
Vladimir Marko | e39c54e | 2014-09-22 14:50:02 +0100 | [diff] [blame] | 97 | for (BasicBlockId pred_id : bb->predecessors) { |
| 98 | DCHECK_NE(pred_id, NullBasicBlockId); |
| 99 | if (lvns_[pred_id] != nullptr && |
Vladimir Marko | 55fff04 | 2014-07-10 12:42:52 +0100 | [diff] [blame] | 100 | (use_all_predecessors || |
Vladimir Marko | e39c54e | 2014-09-22 14:50:02 +0100 | [diff] [blame] | 101 | mir_graph_->GetTopologicalSortOrderIndexes()[pred_id] < loop_head_idx)) { |
| 102 | merge_lvns_.push_back(lvns_[pred_id]); |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | // Determine merge type. |
| 106 | LocalValueNumbering::MergeType merge_type = LocalValueNumbering::kNormalMerge; |
| 107 | if (bb->catch_entry) { |
| 108 | merge_type = LocalValueNumbering::kCatchMerge; |
| 109 | } else if (bb->last_mir_insn != nullptr && |
Vladimir Marko | 55fff04 | 2014-07-10 12:42:52 +0100 | [diff] [blame] | 110 | (bb->last_mir_insn->dalvikInsn.opcode == Instruction::RETURN_VOID || |
| 111 | bb->last_mir_insn->dalvikInsn.opcode == Instruction::RETURN || |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 112 | bb->last_mir_insn->dalvikInsn.opcode == Instruction::RETURN_OBJECT || |
| 113 | bb->last_mir_insn->dalvikInsn.opcode == Instruction::RETURN_WIDE) && |
| 114 | (bb->first_mir_insn == bb->last_mir_insn || |
Vladimir Marko | 55fff04 | 2014-07-10 12:42:52 +0100 | [diff] [blame] | 115 | (static_cast<int>(bb->first_mir_insn->dalvikInsn.opcode) == kMirOpPhi && |
| 116 | (bb->first_mir_insn->next == bb->last_mir_insn || |
| 117 | (static_cast<int>(bb->first_mir_insn->next->dalvikInsn.opcode) == kMirOpPhi && |
| 118 | bb->first_mir_insn->next->next == bb->last_mir_insn))))) { |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 119 | merge_type = LocalValueNumbering::kReturnMerge; |
| 120 | } |
| 121 | // At least one predecessor must have been processed before this bb. |
| 122 | CHECK(!merge_lvns_.empty()); |
| 123 | if (merge_lvns_.size() == 1u) { |
| 124 | work_lvn_->MergeOne(*merge_lvns_[0], merge_type); |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 125 | } else { |
| 126 | work_lvn_->Merge(merge_type); |
| 127 | } |
| 128 | } |
| 129 | return work_lvn_.get(); |
| 130 | } |
| 131 | |
| 132 | bool GlobalValueNumbering::FinishBasicBlock(BasicBlock* bb) { |
| 133 | DCHECK(work_lvn_ != nullptr); |
Vladimir Marko | 55fff04 | 2014-07-10 12:42:52 +0100 | [diff] [blame] | 134 | DCHECK_EQ(bb->id, work_lvn_->Id()); |
| 135 | ++bbs_processed_; |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 136 | merge_lvns_.clear(); |
| 137 | |
Vladimir Marko | b19955d | 2014-07-29 12:04:10 +0100 | [diff] [blame] | 138 | bool change = (lvns_[bb->id] == nullptr) || !lvns_[bb->id]->Equals(*work_lvn_); |
| 139 | if (change) { |
| 140 | std::unique_ptr<const LocalValueNumbering> old_lvn(lvns_[bb->id]); |
| 141 | lvns_[bb->id] = work_lvn_.release(); |
| 142 | } else { |
| 143 | work_lvn_.reset(); |
| 144 | } |
| 145 | return change; |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | uint16_t GlobalValueNumbering::GetFieldId(const MirFieldInfo& field_info, uint16_t type) { |
| 149 | FieldReference key = { field_info.DeclaringDexFile(), field_info.DeclaringFieldIndex(), type }; |
| 150 | auto lb = field_index_map_.lower_bound(key); |
| 151 | if (lb != field_index_map_.end() && !field_index_map_.key_comp()(key, lb->first)) { |
| 152 | return lb->second; |
| 153 | } |
| 154 | DCHECK_LT(field_index_map_.size(), kNoValue); |
| 155 | uint16_t id = field_index_map_.size(); |
| 156 | auto it = field_index_map_.PutBefore(lb, key, id); |
| 157 | field_index_reverse_map_.push_back(&*it); |
| 158 | return id; |
| 159 | } |
| 160 | |
| 161 | uint16_t GlobalValueNumbering::GetArrayLocation(uint16_t base, uint16_t index) { |
| 162 | auto cmp = array_location_map_.key_comp(); |
| 163 | ArrayLocation key = { base, index }; |
| 164 | auto lb = array_location_map_.lower_bound(key); |
| 165 | if (lb != array_location_map_.end() && !cmp(key, lb->first)) { |
| 166 | return lb->second; |
| 167 | } |
| 168 | uint16_t location = static_cast<uint16_t>(array_location_reverse_map_.size()); |
| 169 | DCHECK_EQ(location, array_location_reverse_map_.size()); // No overflow. |
| 170 | auto it = array_location_map_.PutBefore(lb, key, location); |
| 171 | array_location_reverse_map_.push_back(&*it); |
| 172 | return location; |
| 173 | } |
| 174 | |
| 175 | bool GlobalValueNumbering::HasNullCheckLastInsn(const BasicBlock* pred_bb, |
| 176 | BasicBlockId succ_id) { |
| 177 | if (pred_bb->block_type != kDalvikByteCode || pred_bb->last_mir_insn == nullptr) { |
| 178 | return false; |
| 179 | } |
| 180 | Instruction::Code last_opcode = pred_bb->last_mir_insn->dalvikInsn.opcode; |
| 181 | return ((last_opcode == Instruction::IF_EQZ && pred_bb->fall_through == succ_id) || |
| 182 | (last_opcode == Instruction::IF_NEZ && pred_bb->taken == succ_id)); |
| 183 | } |
| 184 | |
| 185 | bool GlobalValueNumbering::NullCheckedInAllPredecessors( |
| 186 | const ScopedArenaVector<uint16_t>& merge_names) const { |
| 187 | // Implicit parameters: |
| 188 | // - *work_lvn: the LVN for which we're checking predecessors. |
| 189 | // - merge_lvns_: the predecessor LVNs. |
| 190 | DCHECK_EQ(merge_lvns_.size(), merge_names.size()); |
| 191 | for (size_t i = 0, size = merge_lvns_.size(); i != size; ++i) { |
| 192 | const LocalValueNumbering* pred_lvn = merge_lvns_[i]; |
| 193 | uint16_t value_name = merge_names[i]; |
| 194 | if (!pred_lvn->IsValueNullChecked(value_name)) { |
| 195 | // Check if the predecessor has an IF_EQZ/IF_NEZ as the last insn. |
Vladimir Marko | 55fff04 | 2014-07-10 12:42:52 +0100 | [diff] [blame] | 196 | const BasicBlock* pred_bb = mir_graph_->GetBasicBlock(pred_lvn->Id()); |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 197 | if (!HasNullCheckLastInsn(pred_bb, work_lvn_->Id())) { |
| 198 | return false; |
| 199 | } |
| 200 | // IF_EQZ/IF_NEZ checks some sreg, see if that sreg contains the value_name. |
| 201 | int s_reg = pred_bb->last_mir_insn->ssa_rep->uses[0]; |
| 202 | if (!pred_lvn->IsSregValue(s_reg, value_name)) { |
| 203 | return false; |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | return true; |
| 208 | } |
| 209 | |
| 210 | } // namespace art |