blob: c505efafe2fc7dc57d5f08cfc011c8ab7f18b3bf [file] [log] [blame]
David Brazdil86ea7ee2016-02-16 09:26:07 +00001/*
2 * Copyright (C) 2016 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 "block_builder.h"
18
Andreas Gampe57943812017-12-06 21:39:13 -080019#include "base/logging.h" // FOR VLOG.
David Brazdil86ea7ee2016-02-16 09:26:07 +000020#include "bytecode_utils.h"
Mathieu Chartier808c7a52017-12-15 11:19:33 -080021#include "code_item_accessors-inl.h"
Mathieu Chartierde4b08f2017-07-10 14:13:41 -070022#include "quicken_info.h"
David Brazdil86ea7ee2016-02-16 09:26:07 +000023
24namespace art {
25
Mathieu Chartier808c7a52017-12-15 11:19:33 -080026HBasicBlockBuilder::HBasicBlockBuilder(HGraph* graph,
27 const DexFile* const dex_file,
28 const CodeItemDebugInfoAccessor& accessor,
29 ScopedArenaAllocator* local_allocator)
30 : allocator_(graph->GetAllocator()),
31 graph_(graph),
32 dex_file_(dex_file),
33 code_item_accessor_(accessor),
34 local_allocator_(local_allocator),
35 branch_targets_(code_item_accessor_.HasCodeItem()
36 ? code_item_accessor_.InsnsSizeInCodeUnits()
37 : /* fake dex_pc=0 for intrinsic graph */ 1u,
38 nullptr,
39 local_allocator->Adapter(kArenaAllocGraphBuilder)),
40 throwing_blocks_(kDefaultNumberOfThrowingBlocks,
41 local_allocator->Adapter(kArenaAllocGraphBuilder)),
42 number_of_branches_(0u),
43 quicken_index_for_dex_pc_(std::less<uint32_t>(),
44 local_allocator->Adapter(kArenaAllocGraphBuilder)) {}
45
David Brazdil86ea7ee2016-02-16 09:26:07 +000046HBasicBlock* HBasicBlockBuilder::MaybeCreateBlockAt(uint32_t dex_pc) {
47 return MaybeCreateBlockAt(dex_pc, dex_pc);
48}
49
50HBasicBlock* HBasicBlockBuilder::MaybeCreateBlockAt(uint32_t semantic_dex_pc,
51 uint32_t store_dex_pc) {
52 HBasicBlock* block = branch_targets_[store_dex_pc];
53 if (block == nullptr) {
Vladimir Marko69d310e2017-10-09 14:12:23 +010054 block = new (allocator_) HBasicBlock(graph_, semantic_dex_pc);
David Brazdil86ea7ee2016-02-16 09:26:07 +000055 branch_targets_[store_dex_pc] = block;
56 }
57 DCHECK_EQ(block->GetDexPc(), semantic_dex_pc);
58 return block;
59}
60
61bool HBasicBlockBuilder::CreateBranchTargets() {
62 // Create the first block for the dex instructions, single successor of the entry block.
63 MaybeCreateBlockAt(0u);
64
Mathieu Chartier808c7a52017-12-15 11:19:33 -080065 if (code_item_accessor_.TriesSize() != 0) {
David Brazdil86ea7ee2016-02-16 09:26:07 +000066 // Create branch targets at the start/end of the TryItem range. These are
67 // places where the program might fall through into/out of the a block and
68 // where TryBoundary instructions will be inserted later. Other edges which
69 // enter/exit the try blocks are a result of branches/switches.
Mathieu Chartier808c7a52017-12-15 11:19:33 -080070 for (const DexFile::TryItem& try_item : code_item_accessor_.TryItems()) {
71 uint32_t dex_pc_start = try_item.start_addr_;
72 uint32_t dex_pc_end = dex_pc_start + try_item.insn_count_;
David Brazdil86ea7ee2016-02-16 09:26:07 +000073 MaybeCreateBlockAt(dex_pc_start);
Mathieu Chartier808c7a52017-12-15 11:19:33 -080074 if (dex_pc_end < code_item_accessor_.InsnsSizeInCodeUnits()) {
David Brazdil86ea7ee2016-02-16 09:26:07 +000075 // TODO: Do not create block if the last instruction cannot fall through.
76 MaybeCreateBlockAt(dex_pc_end);
Mathieu Chartier808c7a52017-12-15 11:19:33 -080077 } else if (dex_pc_end == code_item_accessor_.InsnsSizeInCodeUnits()) {
David Brazdil86ea7ee2016-02-16 09:26:07 +000078 // The TryItem spans until the very end of the CodeItem and therefore
79 // cannot have any code afterwards.
80 } else {
81 // The TryItem spans beyond the end of the CodeItem. This is invalid code.
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +000082 VLOG(compiler) << "Not compiled: TryItem spans beyond the end of the CodeItem";
David Brazdil86ea7ee2016-02-16 09:26:07 +000083 return false;
84 }
85 }
86
87 // Create branch targets for exception handlers.
Mathieu Chartier808c7a52017-12-15 11:19:33 -080088 const uint8_t* handlers_ptr = code_item_accessor_.GetCatchHandlerData();
David Brazdil86ea7ee2016-02-16 09:26:07 +000089 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
90 for (uint32_t idx = 0; idx < handlers_size; ++idx) {
91 CatchHandlerIterator iterator(handlers_ptr);
92 for (; iterator.HasNext(); iterator.Next()) {
93 MaybeCreateBlockAt(iterator.GetHandlerAddress());
94 }
95 handlers_ptr = iterator.EndDataPointer();
96 }
97 }
98
99 // Iterate over all instructions and find branching instructions. Create blocks for
100 // the locations these instructions branch to.
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800101 for (const DexInstructionPcPair& pair : code_item_accessor_) {
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800102 const uint32_t dex_pc = pair.DexPc();
103 const Instruction& instruction = pair.Inst();
David Brazdil86ea7ee2016-02-16 09:26:07 +0000104
105 if (instruction.IsBranch()) {
106 number_of_branches_++;
107 MaybeCreateBlockAt(dex_pc + instruction.GetTargetOffset());
108 } else if (instruction.IsSwitch()) {
109 DexSwitchTable table(instruction, dex_pc);
110 for (DexSwitchTableIterator s_it(table); !s_it.Done(); s_it.Advance()) {
111 MaybeCreateBlockAt(dex_pc + s_it.CurrentTargetOffset());
112
113 // Create N-1 blocks where we will insert comparisons of the input value
114 // against the Switch's case keys.
115 if (table.ShouldBuildDecisionTree() && !s_it.IsLast()) {
116 // Store the block under dex_pc of the current key at the switch data
117 // instruction for uniqueness but give it the dex_pc of the SWITCH
118 // instruction which it semantically belongs to.
119 MaybeCreateBlockAt(dex_pc, s_it.GetDexPcForCurrentIndex());
120 }
121 }
122 } else if (instruction.Opcode() == Instruction::MOVE_EXCEPTION) {
123 // End the basic block after MOVE_EXCEPTION. This simplifies the later
124 // stage of TryBoundary-block insertion.
125 } else {
126 continue;
127 }
128
129 if (instruction.CanFlowThrough()) {
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800130 DexInstructionIterator next(std::next(DexInstructionIterator(pair)));
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800131 if (next == code_item_accessor_.end()) {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000132 // In the normal case we should never hit this but someone can artificially forge a dex
133 // file to fall-through out the method code. In this case we bail out compilation.
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +0000134 VLOG(compiler) << "Not compiled: Fall-through beyond the CodeItem";
David Brazdil86ea7ee2016-02-16 09:26:07 +0000135 return false;
David Brazdil86ea7ee2016-02-16 09:26:07 +0000136 }
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800137 MaybeCreateBlockAt(next.DexPc());
David Brazdil86ea7ee2016-02-16 09:26:07 +0000138 }
139 }
140
141 return true;
142}
143
144void HBasicBlockBuilder::ConnectBasicBlocks() {
145 HBasicBlock* block = graph_->GetEntryBlock();
146 graph_->AddBlock(block);
147
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700148 size_t quicken_index = 0;
David Brazdil86ea7ee2016-02-16 09:26:07 +0000149 bool is_throwing_block = false;
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700150 // Calculate the qucikening index here instead of CreateBranchTargets since it's easier to
151 // calculate in dex_pc order.
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800152 for (const DexInstructionPcPair& pair : code_item_accessor_) {
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800153 const uint32_t dex_pc = pair.DexPc();
154 const Instruction& instruction = pair.Inst();
David Brazdil86ea7ee2016-02-16 09:26:07 +0000155
156 // Check if this dex_pc address starts a new basic block.
157 HBasicBlock* next_block = GetBlockAt(dex_pc);
158 if (next_block != nullptr) {
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700159 // We only need quicken index entries for basic block boundaries.
160 quicken_index_for_dex_pc_.Put(dex_pc, quicken_index);
David Brazdil86ea7ee2016-02-16 09:26:07 +0000161 if (block != nullptr) {
162 // Last instruction did not end its basic block but a new one starts here.
163 // It must have been a block falling through into the next one.
164 block->AddSuccessor(next_block);
165 }
166 block = next_block;
167 is_throwing_block = false;
168 graph_->AddBlock(block);
169 }
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700170 // Make sure to increment this before the continues.
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800171 if (QuickenInfoTable::NeedsIndexForInstruction(&instruction)) {
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700172 ++quicken_index;
173 }
David Brazdil86ea7ee2016-02-16 09:26:07 +0000174
175 if (block == nullptr) {
176 // Ignore dead code.
177 continue;
178 }
179
David Brazdil86ea7ee2016-02-16 09:26:07 +0000180 if (!is_throwing_block && IsThrowingDexInstruction(instruction)) {
181 DCHECK(!ContainsElement(throwing_blocks_, block));
182 is_throwing_block = true;
183 throwing_blocks_.push_back(block);
184 }
185
186 if (instruction.IsBranch()) {
187 uint32_t target_dex_pc = dex_pc + instruction.GetTargetOffset();
188 block->AddSuccessor(GetBlockAt(target_dex_pc));
189 } else if (instruction.IsReturn() || (instruction.Opcode() == Instruction::THROW)) {
190 block->AddSuccessor(graph_->GetExitBlock());
191 } else if (instruction.IsSwitch()) {
192 DexSwitchTable table(instruction, dex_pc);
193 for (DexSwitchTableIterator s_it(table); !s_it.Done(); s_it.Advance()) {
194 uint32_t target_dex_pc = dex_pc + s_it.CurrentTargetOffset();
195 block->AddSuccessor(GetBlockAt(target_dex_pc));
196
197 if (table.ShouldBuildDecisionTree() && !s_it.IsLast()) {
198 uint32_t next_case_dex_pc = s_it.GetDexPcForCurrentIndex();
199 HBasicBlock* next_case_block = GetBlockAt(next_case_dex_pc);
200 block->AddSuccessor(next_case_block);
201 block = next_case_block;
202 graph_->AddBlock(block);
203 }
204 }
205 } else {
206 // Remaining code only applies to instructions which end their basic block.
207 continue;
208 }
209
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800210 // Go to the next instruction in case we read dex PC below.
David Brazdil86ea7ee2016-02-16 09:26:07 +0000211 if (instruction.CanFlowThrough()) {
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800212 block->AddSuccessor(GetBlockAt(std::next(DexInstructionIterator(pair)).DexPc()));
David Brazdil86ea7ee2016-02-16 09:26:07 +0000213 }
214
215 // The basic block ends here. Do not add any more instructions.
216 block = nullptr;
217 }
218
219 graph_->AddBlock(graph_->GetExitBlock());
220}
221
222// Returns the TryItem stored for `block` or nullptr if there is no info for it.
223static const DexFile::TryItem* GetTryItem(
224 HBasicBlock* block,
Vladimir Marko69d310e2017-10-09 14:12:23 +0100225 const ScopedArenaSafeMap<uint32_t, const DexFile::TryItem*>& try_block_info) {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000226 auto iterator = try_block_info.find(block->GetBlockId());
227 return (iterator == try_block_info.end()) ? nullptr : iterator->second;
228}
229
230// Iterates over the exception handlers of `try_item`, finds the corresponding
231// catch blocks and makes them successors of `try_boundary`. The order of
232// successors matches the order in which runtime exception delivery searches
233// for a handler.
234static void LinkToCatchBlocks(HTryBoundary* try_boundary,
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800235 const CodeItemDataAccessor& accessor,
David Brazdil86ea7ee2016-02-16 09:26:07 +0000236 const DexFile::TryItem* try_item,
Vladimir Marko69d310e2017-10-09 14:12:23 +0100237 const ScopedArenaSafeMap<uint32_t, HBasicBlock*>& catch_blocks) {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800238 for (CatchHandlerIterator it(accessor.GetCatchHandlerData(try_item->handler_off_));
239 it.HasNext();
240 it.Next()) {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000241 try_boundary->AddExceptionHandler(catch_blocks.Get(it.GetHandlerAddress()));
242 }
243}
244
245bool HBasicBlockBuilder::MightHaveLiveNormalPredecessors(HBasicBlock* catch_block) {
246 if (kIsDebugBuild) {
247 DCHECK_NE(catch_block->GetDexPc(), kNoDexPc) << "Should not be called on synthetic blocks";
248 DCHECK(!graph_->GetEntryBlock()->GetSuccessors().empty())
249 << "Basic blocks must have been created and connected";
250 for (HBasicBlock* predecessor : catch_block->GetPredecessors()) {
251 DCHECK(!predecessor->IsSingleTryBoundary())
252 << "TryBoundary blocks must not have not been created yet";
253 }
254 }
255
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800256 const Instruction& first = code_item_accessor_.InstructionAt(catch_block->GetDexPc());
David Brazdil86ea7ee2016-02-16 09:26:07 +0000257 if (first.Opcode() == Instruction::MOVE_EXCEPTION) {
258 // Verifier guarantees that if a catch block begins with MOVE_EXCEPTION then
259 // it has no live normal predecessors.
260 return false;
261 } else if (catch_block->GetPredecessors().empty()) {
262 // Normal control-flow edges have already been created. Since block's list of
263 // predecessors is empty, it cannot have any live or dead normal predecessors.
264 return false;
265 }
266
267 // The catch block has normal predecessors but we do not know which are live
268 // and which will be removed during the initial DCE. Return `true` to signal
269 // that it may have live normal predecessors.
270 return true;
271}
272
273void HBasicBlockBuilder::InsertTryBoundaryBlocks() {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800274 if (code_item_accessor_.TriesSize() == 0) {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000275 return;
276 }
277
278 // Keep a map of all try blocks and their respective TryItems. We do not use
279 // the block's pointer but rather its id to ensure deterministic iteration.
Vladimir Marko69d310e2017-10-09 14:12:23 +0100280 ScopedArenaSafeMap<uint32_t, const DexFile::TryItem*> try_block_info(
281 std::less<uint32_t>(), local_allocator_->Adapter(kArenaAllocGraphBuilder));
David Brazdil86ea7ee2016-02-16 09:26:07 +0000282
283 // Obtain TryItem information for blocks with throwing instructions, and split
284 // blocks which are both try & catch to simplify the graph.
285 for (HBasicBlock* block : graph_->GetBlocks()) {
286 if (block->GetDexPc() == kNoDexPc) {
287 continue;
288 }
289
290 // Do not bother creating exceptional edges for try blocks which have no
291 // throwing instructions. In that case we simply assume that the block is
292 // not covered by a TryItem. This prevents us from creating a throw-catch
293 // loop for synchronized blocks.
294 if (ContainsElement(throwing_blocks_, block)) {
295 // Try to find a TryItem covering the block.
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800296 const DexFile::TryItem* try_item = code_item_accessor_.FindTryItem(block->GetDexPc());
297 if (try_item != nullptr) {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000298 // Block throwing and in a TryItem. Store the try block information.
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800299 try_block_info.Put(block->GetBlockId(), try_item);
David Brazdil86ea7ee2016-02-16 09:26:07 +0000300 }
301 }
302 }
303
304 // Map from a handler dex_pc to the corresponding catch block.
Vladimir Marko69d310e2017-10-09 14:12:23 +0100305 ScopedArenaSafeMap<uint32_t, HBasicBlock*> catch_blocks(
306 std::less<uint32_t>(), local_allocator_->Adapter(kArenaAllocGraphBuilder));
David Brazdil86ea7ee2016-02-16 09:26:07 +0000307
308 // Iterate over catch blocks, create artifical landing pads if necessary to
309 // simplify the CFG, and set metadata.
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800310 const uint8_t* handlers_ptr = code_item_accessor_.GetCatchHandlerData();
David Brazdil86ea7ee2016-02-16 09:26:07 +0000311 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
312 for (uint32_t idx = 0; idx < handlers_size; ++idx) {
313 CatchHandlerIterator iterator(handlers_ptr);
314 for (; iterator.HasNext(); iterator.Next()) {
315 uint32_t address = iterator.GetHandlerAddress();
316 if (catch_blocks.find(address) != catch_blocks.end()) {
317 // Catch block already processed.
318 continue;
319 }
320
321 // Check if we should create an artifical landing pad for the catch block.
322 // We create one if the catch block is also a try block because we do not
323 // have a strategy for inserting TryBoundaries on exceptional edges.
324 // We also create one if the block might have normal predecessors so as to
325 // simplify register allocation.
326 HBasicBlock* catch_block = GetBlockAt(address);
327 bool is_try_block = (try_block_info.find(catch_block->GetBlockId()) != try_block_info.end());
328 if (is_try_block || MightHaveLiveNormalPredecessors(catch_block)) {
Vladimir Marko69d310e2017-10-09 14:12:23 +0100329 HBasicBlock* new_catch_block = new (allocator_) HBasicBlock(graph_, address);
330 new_catch_block->AddInstruction(new (allocator_) HGoto(address));
David Brazdil86ea7ee2016-02-16 09:26:07 +0000331 new_catch_block->AddSuccessor(catch_block);
332 graph_->AddBlock(new_catch_block);
333 catch_block = new_catch_block;
334 }
335
336 catch_blocks.Put(address, catch_block);
337 catch_block->SetTryCatchInformation(
Vladimir Marko69d310e2017-10-09 14:12:23 +0100338 new (allocator_) TryCatchInformation(iterator.GetHandlerTypeIndex(), *dex_file_));
David Brazdil86ea7ee2016-02-16 09:26:07 +0000339 }
340 handlers_ptr = iterator.EndDataPointer();
341 }
342
343 // Do a pass over the try blocks and insert entering TryBoundaries where at
344 // least one predecessor is not covered by the same TryItem as the try block.
345 // We do not split each edge separately, but rather create one boundary block
346 // that all predecessors are relinked to. This preserves loop headers (b/23895756).
Vladimir Marko7d157fc2017-05-10 16:29:23 +0100347 for (const auto& entry : try_block_info) {
348 uint32_t block_id = entry.first;
349 const DexFile::TryItem* try_item = entry.second;
350 HBasicBlock* try_block = graph_->GetBlocks()[block_id];
David Brazdil86ea7ee2016-02-16 09:26:07 +0000351 for (HBasicBlock* predecessor : try_block->GetPredecessors()) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +0100352 if (GetTryItem(predecessor, try_block_info) != try_item) {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000353 // Found a predecessor not covered by the same TryItem. Insert entering
354 // boundary block.
Vladimir Marko69d310e2017-10-09 14:12:23 +0100355 HTryBoundary* try_entry = new (allocator_) HTryBoundary(
356 HTryBoundary::BoundaryKind::kEntry, try_block->GetDexPc());
David Brazdil86ea7ee2016-02-16 09:26:07 +0000357 try_block->CreateImmediateDominator()->AddInstruction(try_entry);
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800358 LinkToCatchBlocks(try_entry, code_item_accessor_, try_item, catch_blocks);
David Brazdil86ea7ee2016-02-16 09:26:07 +0000359 break;
360 }
361 }
362 }
363
364 // Do a second pass over the try blocks and insert exit TryBoundaries where
365 // the successor is not in the same TryItem.
Vladimir Marko7d157fc2017-05-10 16:29:23 +0100366 for (const auto& entry : try_block_info) {
367 uint32_t block_id = entry.first;
368 const DexFile::TryItem* try_item = entry.second;
369 HBasicBlock* try_block = graph_->GetBlocks()[block_id];
David Brazdil86ea7ee2016-02-16 09:26:07 +0000370 // NOTE: Do not use iterators because SplitEdge would invalidate them.
371 for (size_t i = 0, e = try_block->GetSuccessors().size(); i < e; ++i) {
372 HBasicBlock* successor = try_block->GetSuccessors()[i];
373
374 // If the successor is a try block, all of its predecessors must be
375 // covered by the same TryItem. Otherwise the previous pass would have
376 // created a non-throwing boundary block.
377 if (GetTryItem(successor, try_block_info) != nullptr) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +0100378 DCHECK_EQ(try_item, GetTryItem(successor, try_block_info));
David Brazdil86ea7ee2016-02-16 09:26:07 +0000379 continue;
380 }
381
382 // Insert TryBoundary and link to catch blocks.
383 HTryBoundary* try_exit =
Vladimir Marko69d310e2017-10-09 14:12:23 +0100384 new (allocator_) HTryBoundary(HTryBoundary::BoundaryKind::kExit, successor->GetDexPc());
David Brazdil86ea7ee2016-02-16 09:26:07 +0000385 graph_->SplitEdge(try_block, successor)->AddInstruction(try_exit);
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800386 LinkToCatchBlocks(try_exit, code_item_accessor_, try_item, catch_blocks);
David Brazdil86ea7ee2016-02-16 09:26:07 +0000387 }
388 }
389}
390
391bool HBasicBlockBuilder::Build() {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800392 DCHECK(code_item_accessor_.HasCodeItem());
David Brazdil86ea7ee2016-02-16 09:26:07 +0000393 DCHECK(graph_->GetBlocks().empty());
394
Vladimir Marko69d310e2017-10-09 14:12:23 +0100395 graph_->SetEntryBlock(new (allocator_) HBasicBlock(graph_, kNoDexPc));
396 graph_->SetExitBlock(new (allocator_) HBasicBlock(graph_, kNoDexPc));
David Brazdil86ea7ee2016-02-16 09:26:07 +0000397
398 // TODO(dbrazdil): Do CreateBranchTargets and ConnectBasicBlocks in one pass.
399 if (!CreateBranchTargets()) {
400 return false;
401 }
402
403 ConnectBasicBlocks();
404 InsertTryBoundaryBlocks();
405
406 return true;
407}
408
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000409void HBasicBlockBuilder::BuildIntrinsic() {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800410 DCHECK(!code_item_accessor_.HasCodeItem());
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000411 DCHECK(graph_->GetBlocks().empty());
412
413 // Create blocks.
414 HBasicBlock* entry_block = new (allocator_) HBasicBlock(graph_, kNoDexPc);
415 HBasicBlock* exit_block = new (allocator_) HBasicBlock(graph_, kNoDexPc);
416 HBasicBlock* body = MaybeCreateBlockAt(/* semantic_dex_pc */ kNoDexPc, /* store_dex_pc */ 0u);
417
418 // Add blocks to the graph.
419 graph_->AddBlock(entry_block);
420 graph_->AddBlock(body);
421 graph_->AddBlock(exit_block);
422 graph_->SetEntryBlock(entry_block);
423 graph_->SetExitBlock(exit_block);
424
425 // Connect blocks.
426 entry_block->AddSuccessor(body);
427 body->AddSuccessor(exit_block);
428}
429
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700430size_t HBasicBlockBuilder::GetQuickenIndex(uint32_t dex_pc) const {
431 return quicken_index_for_dex_pc_.Get(dex_pc);
432}
433
David Brazdil86ea7ee2016-02-16 09:26:07 +0000434} // namespace art