blob: 95f2e98ac680dd98f97d69b4e778ec8f8c73948e [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 Sehr312f3b22018-03-19 08:39:26 -070020#include "dex/bytecode_utils.h"
David Sehr9e734c72018-01-04 17:56:19 -080021#include "dex/code_item_accessors-inl.h"
22#include "dex/dex_file_exception_helpers.h"
Mathieu Chartierde4b08f2017-07-10 14:13:41 -070023#include "quicken_info.h"
David Brazdil86ea7ee2016-02-16 09:26:07 +000024
25namespace art {
26
Mathieu Chartier808c7a52017-12-15 11:19:33 -080027HBasicBlockBuilder::HBasicBlockBuilder(HGraph* graph,
28 const DexFile* const dex_file,
29 const CodeItemDebugInfoAccessor& accessor,
30 ScopedArenaAllocator* local_allocator)
31 : allocator_(graph->GetAllocator()),
32 graph_(graph),
33 dex_file_(dex_file),
34 code_item_accessor_(accessor),
35 local_allocator_(local_allocator),
36 branch_targets_(code_item_accessor_.HasCodeItem()
37 ? code_item_accessor_.InsnsSizeInCodeUnits()
38 : /* fake dex_pc=0 for intrinsic graph */ 1u,
39 nullptr,
40 local_allocator->Adapter(kArenaAllocGraphBuilder)),
41 throwing_blocks_(kDefaultNumberOfThrowingBlocks,
42 local_allocator->Adapter(kArenaAllocGraphBuilder)),
43 number_of_branches_(0u),
44 quicken_index_for_dex_pc_(std::less<uint32_t>(),
45 local_allocator->Adapter(kArenaAllocGraphBuilder)) {}
46
David Brazdil86ea7ee2016-02-16 09:26:07 +000047HBasicBlock* HBasicBlockBuilder::MaybeCreateBlockAt(uint32_t dex_pc) {
48 return MaybeCreateBlockAt(dex_pc, dex_pc);
49}
50
51HBasicBlock* HBasicBlockBuilder::MaybeCreateBlockAt(uint32_t semantic_dex_pc,
52 uint32_t store_dex_pc) {
53 HBasicBlock* block = branch_targets_[store_dex_pc];
54 if (block == nullptr) {
Vladimir Marko69d310e2017-10-09 14:12:23 +010055 block = new (allocator_) HBasicBlock(graph_, semantic_dex_pc);
David Brazdil86ea7ee2016-02-16 09:26:07 +000056 branch_targets_[store_dex_pc] = block;
57 }
58 DCHECK_EQ(block->GetDexPc(), semantic_dex_pc);
59 return block;
60}
61
62bool HBasicBlockBuilder::CreateBranchTargets() {
63 // Create the first block for the dex instructions, single successor of the entry block.
64 MaybeCreateBlockAt(0u);
65
Mathieu Chartier808c7a52017-12-15 11:19:33 -080066 if (code_item_accessor_.TriesSize() != 0) {
David Brazdil86ea7ee2016-02-16 09:26:07 +000067 // Create branch targets at the start/end of the TryItem range. These are
68 // places where the program might fall through into/out of the a block and
69 // where TryBoundary instructions will be inserted later. Other edges which
70 // enter/exit the try blocks are a result of branches/switches.
Mathieu Chartier808c7a52017-12-15 11:19:33 -080071 for (const DexFile::TryItem& try_item : code_item_accessor_.TryItems()) {
72 uint32_t dex_pc_start = try_item.start_addr_;
73 uint32_t dex_pc_end = dex_pc_start + try_item.insn_count_;
David Brazdil86ea7ee2016-02-16 09:26:07 +000074 MaybeCreateBlockAt(dex_pc_start);
Mathieu Chartier808c7a52017-12-15 11:19:33 -080075 if (dex_pc_end < code_item_accessor_.InsnsSizeInCodeUnits()) {
David Brazdil86ea7ee2016-02-16 09:26:07 +000076 // TODO: Do not create block if the last instruction cannot fall through.
77 MaybeCreateBlockAt(dex_pc_end);
Mathieu Chartier808c7a52017-12-15 11:19:33 -080078 } else if (dex_pc_end == code_item_accessor_.InsnsSizeInCodeUnits()) {
David Brazdil86ea7ee2016-02-16 09:26:07 +000079 // The TryItem spans until the very end of the CodeItem and therefore
80 // cannot have any code afterwards.
81 } else {
82 // The TryItem spans beyond the end of the CodeItem. This is invalid code.
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +000083 VLOG(compiler) << "Not compiled: TryItem spans beyond the end of the CodeItem";
David Brazdil86ea7ee2016-02-16 09:26:07 +000084 return false;
85 }
86 }
87
88 // Create branch targets for exception handlers.
Mathieu Chartier808c7a52017-12-15 11:19:33 -080089 const uint8_t* handlers_ptr = code_item_accessor_.GetCatchHandlerData();
David Brazdil86ea7ee2016-02-16 09:26:07 +000090 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
91 for (uint32_t idx = 0; idx < handlers_size; ++idx) {
92 CatchHandlerIterator iterator(handlers_ptr);
93 for (; iterator.HasNext(); iterator.Next()) {
94 MaybeCreateBlockAt(iterator.GetHandlerAddress());
95 }
96 handlers_ptr = iterator.EndDataPointer();
97 }
98 }
99
100 // Iterate over all instructions and find branching instructions. Create blocks for
101 // the locations these instructions branch to.
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800102 for (const DexInstructionPcPair& pair : code_item_accessor_) {
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800103 const uint32_t dex_pc = pair.DexPc();
104 const Instruction& instruction = pair.Inst();
David Brazdil86ea7ee2016-02-16 09:26:07 +0000105
106 if (instruction.IsBranch()) {
107 number_of_branches_++;
108 MaybeCreateBlockAt(dex_pc + instruction.GetTargetOffset());
109 } else if (instruction.IsSwitch()) {
110 DexSwitchTable table(instruction, dex_pc);
111 for (DexSwitchTableIterator s_it(table); !s_it.Done(); s_it.Advance()) {
112 MaybeCreateBlockAt(dex_pc + s_it.CurrentTargetOffset());
113
114 // Create N-1 blocks where we will insert comparisons of the input value
115 // against the Switch's case keys.
116 if (table.ShouldBuildDecisionTree() && !s_it.IsLast()) {
117 // Store the block under dex_pc of the current key at the switch data
118 // instruction for uniqueness but give it the dex_pc of the SWITCH
119 // instruction which it semantically belongs to.
120 MaybeCreateBlockAt(dex_pc, s_it.GetDexPcForCurrentIndex());
121 }
122 }
123 } else if (instruction.Opcode() == Instruction::MOVE_EXCEPTION) {
124 // End the basic block after MOVE_EXCEPTION. This simplifies the later
125 // stage of TryBoundary-block insertion.
126 } else {
127 continue;
128 }
129
130 if (instruction.CanFlowThrough()) {
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800131 DexInstructionIterator next(std::next(DexInstructionIterator(pair)));
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800132 if (next == code_item_accessor_.end()) {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000133 // In the normal case we should never hit this but someone can artificially forge a dex
134 // file to fall-through out the method code. In this case we bail out compilation.
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +0000135 VLOG(compiler) << "Not compiled: Fall-through beyond the CodeItem";
David Brazdil86ea7ee2016-02-16 09:26:07 +0000136 return false;
David Brazdil86ea7ee2016-02-16 09:26:07 +0000137 }
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800138 MaybeCreateBlockAt(next.DexPc());
David Brazdil86ea7ee2016-02-16 09:26:07 +0000139 }
140 }
141
142 return true;
143}
144
145void HBasicBlockBuilder::ConnectBasicBlocks() {
146 HBasicBlock* block = graph_->GetEntryBlock();
147 graph_->AddBlock(block);
148
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700149 size_t quicken_index = 0;
David Brazdil86ea7ee2016-02-16 09:26:07 +0000150 bool is_throwing_block = false;
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700151 // Calculate the qucikening index here instead of CreateBranchTargets since it's easier to
152 // calculate in dex_pc order.
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800153 for (const DexInstructionPcPair& pair : code_item_accessor_) {
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800154 const uint32_t dex_pc = pair.DexPc();
155 const Instruction& instruction = pair.Inst();
David Brazdil86ea7ee2016-02-16 09:26:07 +0000156
157 // Check if this dex_pc address starts a new basic block.
158 HBasicBlock* next_block = GetBlockAt(dex_pc);
159 if (next_block != nullptr) {
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700160 // We only need quicken index entries for basic block boundaries.
161 quicken_index_for_dex_pc_.Put(dex_pc, quicken_index);
David Brazdil86ea7ee2016-02-16 09:26:07 +0000162 if (block != nullptr) {
163 // Last instruction did not end its basic block but a new one starts here.
164 // It must have been a block falling through into the next one.
165 block->AddSuccessor(next_block);
166 }
167 block = next_block;
168 is_throwing_block = false;
169 graph_->AddBlock(block);
170 }
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700171 // Make sure to increment this before the continues.
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800172 if (QuickenInfoTable::NeedsIndexForInstruction(&instruction)) {
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700173 ++quicken_index;
174 }
David Brazdil86ea7ee2016-02-16 09:26:07 +0000175
176 if (block == nullptr) {
177 // Ignore dead code.
178 continue;
179 }
180
David Brazdil86ea7ee2016-02-16 09:26:07 +0000181 if (!is_throwing_block && IsThrowingDexInstruction(instruction)) {
182 DCHECK(!ContainsElement(throwing_blocks_, block));
183 is_throwing_block = true;
184 throwing_blocks_.push_back(block);
185 }
186
187 if (instruction.IsBranch()) {
188 uint32_t target_dex_pc = dex_pc + instruction.GetTargetOffset();
189 block->AddSuccessor(GetBlockAt(target_dex_pc));
190 } else if (instruction.IsReturn() || (instruction.Opcode() == Instruction::THROW)) {
191 block->AddSuccessor(graph_->GetExitBlock());
192 } else if (instruction.IsSwitch()) {
193 DexSwitchTable table(instruction, dex_pc);
194 for (DexSwitchTableIterator s_it(table); !s_it.Done(); s_it.Advance()) {
195 uint32_t target_dex_pc = dex_pc + s_it.CurrentTargetOffset();
196 block->AddSuccessor(GetBlockAt(target_dex_pc));
197
198 if (table.ShouldBuildDecisionTree() && !s_it.IsLast()) {
199 uint32_t next_case_dex_pc = s_it.GetDexPcForCurrentIndex();
200 HBasicBlock* next_case_block = GetBlockAt(next_case_dex_pc);
201 block->AddSuccessor(next_case_block);
202 block = next_case_block;
203 graph_->AddBlock(block);
204 }
205 }
206 } else {
207 // Remaining code only applies to instructions which end their basic block.
208 continue;
209 }
210
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800211 // Go to the next instruction in case we read dex PC below.
David Brazdil86ea7ee2016-02-16 09:26:07 +0000212 if (instruction.CanFlowThrough()) {
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800213 block->AddSuccessor(GetBlockAt(std::next(DexInstructionIterator(pair)).DexPc()));
David Brazdil86ea7ee2016-02-16 09:26:07 +0000214 }
215
216 // The basic block ends here. Do not add any more instructions.
217 block = nullptr;
218 }
219
220 graph_->AddBlock(graph_->GetExitBlock());
221}
222
223// Returns the TryItem stored for `block` or nullptr if there is no info for it.
224static const DexFile::TryItem* GetTryItem(
225 HBasicBlock* block,
Vladimir Marko69d310e2017-10-09 14:12:23 +0100226 const ScopedArenaSafeMap<uint32_t, const DexFile::TryItem*>& try_block_info) {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000227 auto iterator = try_block_info.find(block->GetBlockId());
228 return (iterator == try_block_info.end()) ? nullptr : iterator->second;
229}
230
231// Iterates over the exception handlers of `try_item`, finds the corresponding
232// catch blocks and makes them successors of `try_boundary`. The order of
233// successors matches the order in which runtime exception delivery searches
234// for a handler.
235static void LinkToCatchBlocks(HTryBoundary* try_boundary,
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800236 const CodeItemDataAccessor& accessor,
David Brazdil86ea7ee2016-02-16 09:26:07 +0000237 const DexFile::TryItem* try_item,
Vladimir Marko69d310e2017-10-09 14:12:23 +0100238 const ScopedArenaSafeMap<uint32_t, HBasicBlock*>& catch_blocks) {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800239 for (CatchHandlerIterator it(accessor.GetCatchHandlerData(try_item->handler_off_));
240 it.HasNext();
241 it.Next()) {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000242 try_boundary->AddExceptionHandler(catch_blocks.Get(it.GetHandlerAddress()));
243 }
244}
245
246bool HBasicBlockBuilder::MightHaveLiveNormalPredecessors(HBasicBlock* catch_block) {
247 if (kIsDebugBuild) {
248 DCHECK_NE(catch_block->GetDexPc(), kNoDexPc) << "Should not be called on synthetic blocks";
249 DCHECK(!graph_->GetEntryBlock()->GetSuccessors().empty())
250 << "Basic blocks must have been created and connected";
251 for (HBasicBlock* predecessor : catch_block->GetPredecessors()) {
252 DCHECK(!predecessor->IsSingleTryBoundary())
253 << "TryBoundary blocks must not have not been created yet";
254 }
255 }
256
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800257 const Instruction& first = code_item_accessor_.InstructionAt(catch_block->GetDexPc());
David Brazdil86ea7ee2016-02-16 09:26:07 +0000258 if (first.Opcode() == Instruction::MOVE_EXCEPTION) {
259 // Verifier guarantees that if a catch block begins with MOVE_EXCEPTION then
260 // it has no live normal predecessors.
261 return false;
262 } else if (catch_block->GetPredecessors().empty()) {
263 // Normal control-flow edges have already been created. Since block's list of
264 // predecessors is empty, it cannot have any live or dead normal predecessors.
265 return false;
266 }
267
268 // The catch block has normal predecessors but we do not know which are live
269 // and which will be removed during the initial DCE. Return `true` to signal
270 // that it may have live normal predecessors.
271 return true;
272}
273
274void HBasicBlockBuilder::InsertTryBoundaryBlocks() {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800275 if (code_item_accessor_.TriesSize() == 0) {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000276 return;
277 }
278
279 // Keep a map of all try blocks and their respective TryItems. We do not use
280 // the block's pointer but rather its id to ensure deterministic iteration.
Vladimir Marko69d310e2017-10-09 14:12:23 +0100281 ScopedArenaSafeMap<uint32_t, const DexFile::TryItem*> try_block_info(
282 std::less<uint32_t>(), local_allocator_->Adapter(kArenaAllocGraphBuilder));
David Brazdil86ea7ee2016-02-16 09:26:07 +0000283
284 // Obtain TryItem information for blocks with throwing instructions, and split
285 // blocks which are both try & catch to simplify the graph.
286 for (HBasicBlock* block : graph_->GetBlocks()) {
287 if (block->GetDexPc() == kNoDexPc) {
288 continue;
289 }
290
291 // Do not bother creating exceptional edges for try blocks which have no
292 // throwing instructions. In that case we simply assume that the block is
293 // not covered by a TryItem. This prevents us from creating a throw-catch
294 // loop for synchronized blocks.
295 if (ContainsElement(throwing_blocks_, block)) {
296 // Try to find a TryItem covering the block.
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800297 const DexFile::TryItem* try_item = code_item_accessor_.FindTryItem(block->GetDexPc());
298 if (try_item != nullptr) {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000299 // Block throwing and in a TryItem. Store the try block information.
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800300 try_block_info.Put(block->GetBlockId(), try_item);
David Brazdil86ea7ee2016-02-16 09:26:07 +0000301 }
302 }
303 }
304
305 // Map from a handler dex_pc to the corresponding catch block.
Vladimir Marko69d310e2017-10-09 14:12:23 +0100306 ScopedArenaSafeMap<uint32_t, HBasicBlock*> catch_blocks(
307 std::less<uint32_t>(), local_allocator_->Adapter(kArenaAllocGraphBuilder));
David Brazdil86ea7ee2016-02-16 09:26:07 +0000308
309 // Iterate over catch blocks, create artifical landing pads if necessary to
310 // simplify the CFG, and set metadata.
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800311 const uint8_t* handlers_ptr = code_item_accessor_.GetCatchHandlerData();
David Brazdil86ea7ee2016-02-16 09:26:07 +0000312 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
313 for (uint32_t idx = 0; idx < handlers_size; ++idx) {
314 CatchHandlerIterator iterator(handlers_ptr);
315 for (; iterator.HasNext(); iterator.Next()) {
316 uint32_t address = iterator.GetHandlerAddress();
317 if (catch_blocks.find(address) != catch_blocks.end()) {
318 // Catch block already processed.
319 continue;
320 }
321
322 // Check if we should create an artifical landing pad for the catch block.
323 // We create one if the catch block is also a try block because we do not
324 // have a strategy for inserting TryBoundaries on exceptional edges.
325 // We also create one if the block might have normal predecessors so as to
326 // simplify register allocation.
327 HBasicBlock* catch_block = GetBlockAt(address);
328 bool is_try_block = (try_block_info.find(catch_block->GetBlockId()) != try_block_info.end());
329 if (is_try_block || MightHaveLiveNormalPredecessors(catch_block)) {
Vladimir Marko69d310e2017-10-09 14:12:23 +0100330 HBasicBlock* new_catch_block = new (allocator_) HBasicBlock(graph_, address);
331 new_catch_block->AddInstruction(new (allocator_) HGoto(address));
David Brazdil86ea7ee2016-02-16 09:26:07 +0000332 new_catch_block->AddSuccessor(catch_block);
333 graph_->AddBlock(new_catch_block);
334 catch_block = new_catch_block;
335 }
336
337 catch_blocks.Put(address, catch_block);
338 catch_block->SetTryCatchInformation(
Vladimir Marko69d310e2017-10-09 14:12:23 +0100339 new (allocator_) TryCatchInformation(iterator.GetHandlerTypeIndex(), *dex_file_));
David Brazdil86ea7ee2016-02-16 09:26:07 +0000340 }
341 handlers_ptr = iterator.EndDataPointer();
342 }
343
344 // Do a pass over the try blocks and insert entering TryBoundaries where at
345 // least one predecessor is not covered by the same TryItem as the try block.
346 // We do not split each edge separately, but rather create one boundary block
347 // that all predecessors are relinked to. This preserves loop headers (b/23895756).
Vladimir Marko7d157fc2017-05-10 16:29:23 +0100348 for (const auto& entry : try_block_info) {
349 uint32_t block_id = entry.first;
350 const DexFile::TryItem* try_item = entry.second;
351 HBasicBlock* try_block = graph_->GetBlocks()[block_id];
David Brazdil86ea7ee2016-02-16 09:26:07 +0000352 for (HBasicBlock* predecessor : try_block->GetPredecessors()) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +0100353 if (GetTryItem(predecessor, try_block_info) != try_item) {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000354 // Found a predecessor not covered by the same TryItem. Insert entering
355 // boundary block.
Vladimir Marko69d310e2017-10-09 14:12:23 +0100356 HTryBoundary* try_entry = new (allocator_) HTryBoundary(
357 HTryBoundary::BoundaryKind::kEntry, try_block->GetDexPc());
David Brazdil86ea7ee2016-02-16 09:26:07 +0000358 try_block->CreateImmediateDominator()->AddInstruction(try_entry);
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800359 LinkToCatchBlocks(try_entry, code_item_accessor_, try_item, catch_blocks);
David Brazdil86ea7ee2016-02-16 09:26:07 +0000360 break;
361 }
362 }
363 }
364
365 // Do a second pass over the try blocks and insert exit TryBoundaries where
366 // the successor is not in the same TryItem.
Vladimir Marko7d157fc2017-05-10 16:29:23 +0100367 for (const auto& entry : try_block_info) {
368 uint32_t block_id = entry.first;
369 const DexFile::TryItem* try_item = entry.second;
370 HBasicBlock* try_block = graph_->GetBlocks()[block_id];
David Brazdil86ea7ee2016-02-16 09:26:07 +0000371 // NOTE: Do not use iterators because SplitEdge would invalidate them.
372 for (size_t i = 0, e = try_block->GetSuccessors().size(); i < e; ++i) {
373 HBasicBlock* successor = try_block->GetSuccessors()[i];
374
375 // If the successor is a try block, all of its predecessors must be
376 // covered by the same TryItem. Otherwise the previous pass would have
377 // created a non-throwing boundary block.
378 if (GetTryItem(successor, try_block_info) != nullptr) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +0100379 DCHECK_EQ(try_item, GetTryItem(successor, try_block_info));
David Brazdil86ea7ee2016-02-16 09:26:07 +0000380 continue;
381 }
382
383 // Insert TryBoundary and link to catch blocks.
384 HTryBoundary* try_exit =
Vladimir Marko69d310e2017-10-09 14:12:23 +0100385 new (allocator_) HTryBoundary(HTryBoundary::BoundaryKind::kExit, successor->GetDexPc());
David Brazdil86ea7ee2016-02-16 09:26:07 +0000386 graph_->SplitEdge(try_block, successor)->AddInstruction(try_exit);
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800387 LinkToCatchBlocks(try_exit, code_item_accessor_, try_item, catch_blocks);
David Brazdil86ea7ee2016-02-16 09:26:07 +0000388 }
389 }
390}
391
392bool HBasicBlockBuilder::Build() {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800393 DCHECK(code_item_accessor_.HasCodeItem());
David Brazdil86ea7ee2016-02-16 09:26:07 +0000394 DCHECK(graph_->GetBlocks().empty());
395
Vladimir Marko69d310e2017-10-09 14:12:23 +0100396 graph_->SetEntryBlock(new (allocator_) HBasicBlock(graph_, kNoDexPc));
397 graph_->SetExitBlock(new (allocator_) HBasicBlock(graph_, kNoDexPc));
David Brazdil86ea7ee2016-02-16 09:26:07 +0000398
399 // TODO(dbrazdil): Do CreateBranchTargets and ConnectBasicBlocks in one pass.
400 if (!CreateBranchTargets()) {
401 return false;
402 }
403
404 ConnectBasicBlocks();
405 InsertTryBoundaryBlocks();
406
407 return true;
408}
409
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000410void HBasicBlockBuilder::BuildIntrinsic() {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800411 DCHECK(!code_item_accessor_.HasCodeItem());
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000412 DCHECK(graph_->GetBlocks().empty());
413
414 // Create blocks.
415 HBasicBlock* entry_block = new (allocator_) HBasicBlock(graph_, kNoDexPc);
416 HBasicBlock* exit_block = new (allocator_) HBasicBlock(graph_, kNoDexPc);
417 HBasicBlock* body = MaybeCreateBlockAt(/* semantic_dex_pc */ kNoDexPc, /* store_dex_pc */ 0u);
418
419 // Add blocks to the graph.
420 graph_->AddBlock(entry_block);
421 graph_->AddBlock(body);
422 graph_->AddBlock(exit_block);
423 graph_->SetEntryBlock(entry_block);
424 graph_->SetExitBlock(exit_block);
425
426 // Connect blocks.
427 entry_block->AddSuccessor(body);
428 body->AddSuccessor(exit_block);
429}
430
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700431size_t HBasicBlockBuilder::GetQuickenIndex(uint32_t dex_pc) const {
432 return quicken_index_for_dex_pc_.Get(dex_pc);
433}
434
David Brazdil86ea7ee2016-02-16 09:26:07 +0000435} // namespace art