blob: 614e826fa6dad254f68ca7da2572d10966b0b7be [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#include "global_value_numbering.h"
18
19#include "local_value_numbering.h"
20
21namespace art {
22
23GlobalValueNumbering::GlobalValueNumbering(CompilationUnit* cu, ScopedArenaAllocator* allocator)
24 : cu_(cu),
25 allocator_(allocator),
26 repeat_count_(0u),
27 last_value_(0u),
28 modifications_allowed_(false),
29 global_value_map_(std::less<uint64_t>(), allocator->Adapter()),
30 field_index_map_(FieldReferenceComparator(), allocator->Adapter()),
31 field_index_reverse_map_(allocator->Adapter()),
32 array_location_map_(ArrayLocationComparator(), allocator->Adapter()),
33 array_location_reverse_map_(allocator->Adapter()),
34 ref_set_map_(std::less<ValueNameSet>(), allocator->Adapter()),
35 lvns_(cu_->mir_graph->GetNumBlocks(), nullptr, allocator->Adapter()),
36 work_lvn_(nullptr),
37 merge_lvns_(allocator->Adapter()) {
38 cu_->mir_graph->ClearAllVisitedFlags();
39}
40
41GlobalValueNumbering::~GlobalValueNumbering() {
42 STLDeleteElements(&lvns_);
43}
44
45LocalValueNumbering* GlobalValueNumbering::PrepareBasicBlock(BasicBlock* bb) {
46 if (UNLIKELY(!Good())) {
47 return nullptr;
48 }
49 if (bb->data_flow_info == nullptr) {
50 return nullptr;
51 }
52 if (bb->block_type == kEntryBlock) {
53 repeat_count_ += 1u;
54 if (repeat_count_ > kMaxRepeatCount) {
55 last_value_ = kNoValue; // Make bad.
56 return nullptr;
57 }
58 }
59 if (bb->block_type == kExitBlock) {
60 DCHECK(bb->first_mir_insn == nullptr);
61 return nullptr;
62 }
63 if (bb->visited) {
64 return nullptr;
65 }
66 DCHECK(work_lvn_.get() == nullptr);
67 work_lvn_.reset(new (allocator_) LocalValueNumbering(this, bb->id));
68 if (bb->block_type == kEntryBlock) {
69 if ((cu_->access_flags & kAccStatic) == 0) {
70 // If non-static method, mark "this" as non-null
71 int this_reg = cu_->num_dalvik_registers - cu_->num_ins;
72 work_lvn_->SetSRegNullChecked(this_reg);
73 }
74 } else {
75 // Merge all incoming arcs.
76 // To avoid repeated allocation on the ArenaStack, reuse a single vector kept as a member.
77 DCHECK(merge_lvns_.empty());
78 GrowableArray<BasicBlockId>::Iterator iter(bb->predecessors);
79 for (BasicBlock* pred_bb = cu_->mir_graph->GetBasicBlock(iter.Next());
80 pred_bb != nullptr; pred_bb = cu_->mir_graph->GetBasicBlock(iter.Next())) {
81 if (lvns_[pred_bb->id] != nullptr) {
82 merge_lvns_.push_back(lvns_[pred_bb->id]);
83 }
84 }
85 // Determine merge type.
86 LocalValueNumbering::MergeType merge_type = LocalValueNumbering::kNormalMerge;
87 if (bb->catch_entry) {
88 merge_type = LocalValueNumbering::kCatchMerge;
89 } else if (bb->last_mir_insn != nullptr &&
90 (bb->last_mir_insn->dalvikInsn.opcode == Instruction::RETURN ||
91 bb->last_mir_insn->dalvikInsn.opcode == Instruction::RETURN_OBJECT ||
92 bb->last_mir_insn->dalvikInsn.opcode == Instruction::RETURN_WIDE) &&
93 (bb->first_mir_insn == bb->last_mir_insn ||
94 (bb->first_mir_insn->next == bb->last_mir_insn &&
95 static_cast<int>(bb->first_mir_insn->dalvikInsn.opcode) == kMirOpPhi))) {
96 merge_type = LocalValueNumbering::kReturnMerge;
97 }
98 // At least one predecessor must have been processed before this bb.
99 CHECK(!merge_lvns_.empty());
100 if (merge_lvns_.size() == 1u) {
101 work_lvn_->MergeOne(*merge_lvns_[0], merge_type);
102 BasicBlock* pred_bb = cu_->mir_graph->GetBasicBlock(merge_lvns_[0]->Id());
103 if (HasNullCheckLastInsn(pred_bb, bb->id)) {
104 work_lvn_->SetSRegNullChecked(pred_bb->last_mir_insn->ssa_rep->uses[0]);
105 }
106 } else {
107 work_lvn_->Merge(merge_type);
108 }
109 }
110 return work_lvn_.get();
111}
112
113bool GlobalValueNumbering::FinishBasicBlock(BasicBlock* bb) {
114 DCHECK(work_lvn_ != nullptr);
115 DCHECK(bb->id == work_lvn_->Id());
116 merge_lvns_.clear();
117
118 bool change = false;
119 // Look for a branch to self or an already processed child.
120 // (No need to repeat the LVN if all children are processed later.)
121 ChildBlockIterator iter(bb, cu_->mir_graph.get());
122 for (BasicBlock* child = iter.Next(); child != nullptr; child = iter.Next()) {
123 if (child == bb || lvns_[child->id] != nullptr) {
124 // If we found an already processed child, check if the LVN actually differs.
125 change = (lvns_[bb->id] == nullptr || !lvns_[bb->id]->Equals(*work_lvn_));
126 break;
127 }
128 }
129
130 std::unique_ptr<const LocalValueNumbering> old_lvn(lvns_[bb->id]);
131 lvns_[bb->id] = work_lvn_.release();
132
133 bb->visited = true;
134 if (change) {
135 ChildBlockIterator iter(bb, cu_->mir_graph.get());
136 for (BasicBlock* child = iter.Next(); child != nullptr; child = iter.Next()) {
137 child->visited = false;
138 }
139 }
140 return change;
141}
142
143uint16_t GlobalValueNumbering::GetFieldId(const MirFieldInfo& field_info, uint16_t type) {
144 FieldReference key = { field_info.DeclaringDexFile(), field_info.DeclaringFieldIndex(), type };
145 auto lb = field_index_map_.lower_bound(key);
146 if (lb != field_index_map_.end() && !field_index_map_.key_comp()(key, lb->first)) {
147 return lb->second;
148 }
149 DCHECK_LT(field_index_map_.size(), kNoValue);
150 uint16_t id = field_index_map_.size();
151 auto it = field_index_map_.PutBefore(lb, key, id);
152 field_index_reverse_map_.push_back(&*it);
153 return id;
154}
155
156uint16_t GlobalValueNumbering::GetArrayLocation(uint16_t base, uint16_t index) {
157 auto cmp = array_location_map_.key_comp();
158 ArrayLocation key = { base, index };
159 auto lb = array_location_map_.lower_bound(key);
160 if (lb != array_location_map_.end() && !cmp(key, lb->first)) {
161 return lb->second;
162 }
163 uint16_t location = static_cast<uint16_t>(array_location_reverse_map_.size());
164 DCHECK_EQ(location, array_location_reverse_map_.size()); // No overflow.
165 auto it = array_location_map_.PutBefore(lb, key, location);
166 array_location_reverse_map_.push_back(&*it);
167 return location;
168}
169
170bool GlobalValueNumbering::HasNullCheckLastInsn(const BasicBlock* pred_bb,
171 BasicBlockId succ_id) {
172 if (pred_bb->block_type != kDalvikByteCode || pred_bb->last_mir_insn == nullptr) {
173 return false;
174 }
175 Instruction::Code last_opcode = pred_bb->last_mir_insn->dalvikInsn.opcode;
176 return ((last_opcode == Instruction::IF_EQZ && pred_bb->fall_through == succ_id) ||
177 (last_opcode == Instruction::IF_NEZ && pred_bb->taken == succ_id));
178}
179
180bool GlobalValueNumbering::NullCheckedInAllPredecessors(
181 const ScopedArenaVector<uint16_t>& merge_names) const {
182 // Implicit parameters:
183 // - *work_lvn: the LVN for which we're checking predecessors.
184 // - merge_lvns_: the predecessor LVNs.
185 DCHECK_EQ(merge_lvns_.size(), merge_names.size());
186 for (size_t i = 0, size = merge_lvns_.size(); i != size; ++i) {
187 const LocalValueNumbering* pred_lvn = merge_lvns_[i];
188 uint16_t value_name = merge_names[i];
189 if (!pred_lvn->IsValueNullChecked(value_name)) {
190 // Check if the predecessor has an IF_EQZ/IF_NEZ as the last insn.
191 const BasicBlock* pred_bb = cu_->mir_graph->GetBasicBlock(pred_lvn->Id());
192 if (!HasNullCheckLastInsn(pred_bb, work_lvn_->Id())) {
193 return false;
194 }
195 // IF_EQZ/IF_NEZ checks some sreg, see if that sreg contains the value_name.
196 int s_reg = pred_bb->last_mir_insn->ssa_rep->uses[0];
197 if (!pred_lvn->IsSregValue(s_reg, value_name)) {
198 return false;
199 }
200 }
201 }
202 return true;
203}
204
205} // namespace art