blob: d9df23fd47d350bb7b896bc4b0239685b6068dab [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()) {
Aart Bikd99f2032018-04-06 15:24:35 -0700110 number_of_branches_++; // count as at least one branch (b/77652521)
David Brazdil86ea7ee2016-02-16 09:26:07 +0000111 DexSwitchTable table(instruction, dex_pc);
112 for (DexSwitchTableIterator s_it(table); !s_it.Done(); s_it.Advance()) {
113 MaybeCreateBlockAt(dex_pc + s_it.CurrentTargetOffset());
114
115 // Create N-1 blocks where we will insert comparisons of the input value
116 // against the Switch's case keys.
117 if (table.ShouldBuildDecisionTree() && !s_it.IsLast()) {
118 // Store the block under dex_pc of the current key at the switch data
119 // instruction for uniqueness but give it the dex_pc of the SWITCH
120 // instruction which it semantically belongs to.
121 MaybeCreateBlockAt(dex_pc, s_it.GetDexPcForCurrentIndex());
122 }
123 }
124 } else if (instruction.Opcode() == Instruction::MOVE_EXCEPTION) {
125 // End the basic block after MOVE_EXCEPTION. This simplifies the later
126 // stage of TryBoundary-block insertion.
127 } else {
128 continue;
129 }
130
131 if (instruction.CanFlowThrough()) {
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800132 DexInstructionIterator next(std::next(DexInstructionIterator(pair)));
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800133 if (next == code_item_accessor_.end()) {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000134 // In the normal case we should never hit this but someone can artificially forge a dex
135 // file to fall-through out the method code. In this case we bail out compilation.
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +0000136 VLOG(compiler) << "Not compiled: Fall-through beyond the CodeItem";
David Brazdil86ea7ee2016-02-16 09:26:07 +0000137 return false;
David Brazdil86ea7ee2016-02-16 09:26:07 +0000138 }
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800139 MaybeCreateBlockAt(next.DexPc());
David Brazdil86ea7ee2016-02-16 09:26:07 +0000140 }
141 }
142
143 return true;
144}
145
146void HBasicBlockBuilder::ConnectBasicBlocks() {
147 HBasicBlock* block = graph_->GetEntryBlock();
148 graph_->AddBlock(block);
149
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700150 size_t quicken_index = 0;
David Brazdil86ea7ee2016-02-16 09:26:07 +0000151 bool is_throwing_block = false;
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700152 // Calculate the qucikening index here instead of CreateBranchTargets since it's easier to
153 // calculate in dex_pc order.
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800154 for (const DexInstructionPcPair& pair : code_item_accessor_) {
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800155 const uint32_t dex_pc = pair.DexPc();
156 const Instruction& instruction = pair.Inst();
David Brazdil86ea7ee2016-02-16 09:26:07 +0000157
158 // Check if this dex_pc address starts a new basic block.
159 HBasicBlock* next_block = GetBlockAt(dex_pc);
160 if (next_block != nullptr) {
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700161 // We only need quicken index entries for basic block boundaries.
162 quicken_index_for_dex_pc_.Put(dex_pc, quicken_index);
David Brazdil86ea7ee2016-02-16 09:26:07 +0000163 if (block != nullptr) {
164 // Last instruction did not end its basic block but a new one starts here.
165 // It must have been a block falling through into the next one.
166 block->AddSuccessor(next_block);
167 }
168 block = next_block;
169 is_throwing_block = false;
170 graph_->AddBlock(block);
171 }
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700172 // Make sure to increment this before the continues.
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800173 if (QuickenInfoTable::NeedsIndexForInstruction(&instruction)) {
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700174 ++quicken_index;
175 }
David Brazdil86ea7ee2016-02-16 09:26:07 +0000176
177 if (block == nullptr) {
178 // Ignore dead code.
179 continue;
180 }
181
David Brazdil86ea7ee2016-02-16 09:26:07 +0000182 if (!is_throwing_block && IsThrowingDexInstruction(instruction)) {
183 DCHECK(!ContainsElement(throwing_blocks_, block));
184 is_throwing_block = true;
185 throwing_blocks_.push_back(block);
186 }
187
188 if (instruction.IsBranch()) {
189 uint32_t target_dex_pc = dex_pc + instruction.GetTargetOffset();
190 block->AddSuccessor(GetBlockAt(target_dex_pc));
191 } else if (instruction.IsReturn() || (instruction.Opcode() == Instruction::THROW)) {
192 block->AddSuccessor(graph_->GetExitBlock());
193 } else if (instruction.IsSwitch()) {
194 DexSwitchTable table(instruction, dex_pc);
195 for (DexSwitchTableIterator s_it(table); !s_it.Done(); s_it.Advance()) {
196 uint32_t target_dex_pc = dex_pc + s_it.CurrentTargetOffset();
197 block->AddSuccessor(GetBlockAt(target_dex_pc));
198
199 if (table.ShouldBuildDecisionTree() && !s_it.IsLast()) {
200 uint32_t next_case_dex_pc = s_it.GetDexPcForCurrentIndex();
201 HBasicBlock* next_case_block = GetBlockAt(next_case_dex_pc);
202 block->AddSuccessor(next_case_block);
203 block = next_case_block;
204 graph_->AddBlock(block);
205 }
206 }
207 } else {
208 // Remaining code only applies to instructions which end their basic block.
209 continue;
210 }
211
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800212 // Go to the next instruction in case we read dex PC below.
David Brazdil86ea7ee2016-02-16 09:26:07 +0000213 if (instruction.CanFlowThrough()) {
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800214 block->AddSuccessor(GetBlockAt(std::next(DexInstructionIterator(pair)).DexPc()));
David Brazdil86ea7ee2016-02-16 09:26:07 +0000215 }
216
217 // The basic block ends here. Do not add any more instructions.
218 block = nullptr;
219 }
220
221 graph_->AddBlock(graph_->GetExitBlock());
222}
223
224// Returns the TryItem stored for `block` or nullptr if there is no info for it.
225static const DexFile::TryItem* GetTryItem(
226 HBasicBlock* block,
Vladimir Marko69d310e2017-10-09 14:12:23 +0100227 const ScopedArenaSafeMap<uint32_t, const DexFile::TryItem*>& try_block_info) {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000228 auto iterator = try_block_info.find(block->GetBlockId());
229 return (iterator == try_block_info.end()) ? nullptr : iterator->second;
230}
231
232// Iterates over the exception handlers of `try_item`, finds the corresponding
233// catch blocks and makes them successors of `try_boundary`. The order of
234// successors matches the order in which runtime exception delivery searches
235// for a handler.
236static void LinkToCatchBlocks(HTryBoundary* try_boundary,
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800237 const CodeItemDataAccessor& accessor,
David Brazdil86ea7ee2016-02-16 09:26:07 +0000238 const DexFile::TryItem* try_item,
Vladimir Marko69d310e2017-10-09 14:12:23 +0100239 const ScopedArenaSafeMap<uint32_t, HBasicBlock*>& catch_blocks) {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800240 for (CatchHandlerIterator it(accessor.GetCatchHandlerData(try_item->handler_off_));
241 it.HasNext();
242 it.Next()) {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000243 try_boundary->AddExceptionHandler(catch_blocks.Get(it.GetHandlerAddress()));
244 }
245}
246
247bool HBasicBlockBuilder::MightHaveLiveNormalPredecessors(HBasicBlock* catch_block) {
248 if (kIsDebugBuild) {
249 DCHECK_NE(catch_block->GetDexPc(), kNoDexPc) << "Should not be called on synthetic blocks";
250 DCHECK(!graph_->GetEntryBlock()->GetSuccessors().empty())
251 << "Basic blocks must have been created and connected";
252 for (HBasicBlock* predecessor : catch_block->GetPredecessors()) {
253 DCHECK(!predecessor->IsSingleTryBoundary())
254 << "TryBoundary blocks must not have not been created yet";
255 }
256 }
257
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800258 const Instruction& first = code_item_accessor_.InstructionAt(catch_block->GetDexPc());
David Brazdil86ea7ee2016-02-16 09:26:07 +0000259 if (first.Opcode() == Instruction::MOVE_EXCEPTION) {
260 // Verifier guarantees that if a catch block begins with MOVE_EXCEPTION then
261 // it has no live normal predecessors.
262 return false;
263 } else if (catch_block->GetPredecessors().empty()) {
264 // Normal control-flow edges have already been created. Since block's list of
265 // predecessors is empty, it cannot have any live or dead normal predecessors.
266 return false;
267 }
268
269 // The catch block has normal predecessors but we do not know which are live
270 // and which will be removed during the initial DCE. Return `true` to signal
271 // that it may have live normal predecessors.
272 return true;
273}
274
275void HBasicBlockBuilder::InsertTryBoundaryBlocks() {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800276 if (code_item_accessor_.TriesSize() == 0) {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000277 return;
278 }
279
280 // Keep a map of all try blocks and their respective TryItems. We do not use
281 // the block's pointer but rather its id to ensure deterministic iteration.
Vladimir Marko69d310e2017-10-09 14:12:23 +0100282 ScopedArenaSafeMap<uint32_t, const DexFile::TryItem*> try_block_info(
283 std::less<uint32_t>(), local_allocator_->Adapter(kArenaAllocGraphBuilder));
David Brazdil86ea7ee2016-02-16 09:26:07 +0000284
285 // Obtain TryItem information for blocks with throwing instructions, and split
286 // blocks which are both try & catch to simplify the graph.
287 for (HBasicBlock* block : graph_->GetBlocks()) {
288 if (block->GetDexPc() == kNoDexPc) {
289 continue;
290 }
291
292 // Do not bother creating exceptional edges for try blocks which have no
293 // throwing instructions. In that case we simply assume that the block is
294 // not covered by a TryItem. This prevents us from creating a throw-catch
295 // loop for synchronized blocks.
296 if (ContainsElement(throwing_blocks_, block)) {
297 // Try to find a TryItem covering the block.
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800298 const DexFile::TryItem* try_item = code_item_accessor_.FindTryItem(block->GetDexPc());
299 if (try_item != nullptr) {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000300 // Block throwing and in a TryItem. Store the try block information.
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800301 try_block_info.Put(block->GetBlockId(), try_item);
David Brazdil86ea7ee2016-02-16 09:26:07 +0000302 }
303 }
304 }
305
306 // Map from a handler dex_pc to the corresponding catch block.
Vladimir Marko69d310e2017-10-09 14:12:23 +0100307 ScopedArenaSafeMap<uint32_t, HBasicBlock*> catch_blocks(
308 std::less<uint32_t>(), local_allocator_->Adapter(kArenaAllocGraphBuilder));
David Brazdil86ea7ee2016-02-16 09:26:07 +0000309
310 // Iterate over catch blocks, create artifical landing pads if necessary to
311 // simplify the CFG, and set metadata.
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800312 const uint8_t* handlers_ptr = code_item_accessor_.GetCatchHandlerData();
David Brazdil86ea7ee2016-02-16 09:26:07 +0000313 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
314 for (uint32_t idx = 0; idx < handlers_size; ++idx) {
315 CatchHandlerIterator iterator(handlers_ptr);
316 for (; iterator.HasNext(); iterator.Next()) {
317 uint32_t address = iterator.GetHandlerAddress();
318 if (catch_blocks.find(address) != catch_blocks.end()) {
319 // Catch block already processed.
320 continue;
321 }
322
323 // Check if we should create an artifical landing pad for the catch block.
324 // We create one if the catch block is also a try block because we do not
325 // have a strategy for inserting TryBoundaries on exceptional edges.
326 // We also create one if the block might have normal predecessors so as to
327 // simplify register allocation.
328 HBasicBlock* catch_block = GetBlockAt(address);
329 bool is_try_block = (try_block_info.find(catch_block->GetBlockId()) != try_block_info.end());
330 if (is_try_block || MightHaveLiveNormalPredecessors(catch_block)) {
Vladimir Marko69d310e2017-10-09 14:12:23 +0100331 HBasicBlock* new_catch_block = new (allocator_) HBasicBlock(graph_, address);
332 new_catch_block->AddInstruction(new (allocator_) HGoto(address));
David Brazdil86ea7ee2016-02-16 09:26:07 +0000333 new_catch_block->AddSuccessor(catch_block);
334 graph_->AddBlock(new_catch_block);
335 catch_block = new_catch_block;
336 }
337
338 catch_blocks.Put(address, catch_block);
339 catch_block->SetTryCatchInformation(
Vladimir Marko69d310e2017-10-09 14:12:23 +0100340 new (allocator_) TryCatchInformation(iterator.GetHandlerTypeIndex(), *dex_file_));
David Brazdil86ea7ee2016-02-16 09:26:07 +0000341 }
342 handlers_ptr = iterator.EndDataPointer();
343 }
344
345 // Do a pass over the try blocks and insert entering TryBoundaries where at
346 // least one predecessor is not covered by the same TryItem as the try block.
347 // We do not split each edge separately, but rather create one boundary block
348 // that all predecessors are relinked to. This preserves loop headers (b/23895756).
Vladimir Marko7d157fc2017-05-10 16:29:23 +0100349 for (const auto& entry : try_block_info) {
350 uint32_t block_id = entry.first;
351 const DexFile::TryItem* try_item = entry.second;
352 HBasicBlock* try_block = graph_->GetBlocks()[block_id];
David Brazdil86ea7ee2016-02-16 09:26:07 +0000353 for (HBasicBlock* predecessor : try_block->GetPredecessors()) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +0100354 if (GetTryItem(predecessor, try_block_info) != try_item) {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000355 // Found a predecessor not covered by the same TryItem. Insert entering
356 // boundary block.
Vladimir Marko69d310e2017-10-09 14:12:23 +0100357 HTryBoundary* try_entry = new (allocator_) HTryBoundary(
358 HTryBoundary::BoundaryKind::kEntry, try_block->GetDexPc());
David Brazdil86ea7ee2016-02-16 09:26:07 +0000359 try_block->CreateImmediateDominator()->AddInstruction(try_entry);
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800360 LinkToCatchBlocks(try_entry, code_item_accessor_, try_item, catch_blocks);
David Brazdil86ea7ee2016-02-16 09:26:07 +0000361 break;
362 }
363 }
364 }
365
366 // Do a second pass over the try blocks and insert exit TryBoundaries where
367 // the successor is not in the same TryItem.
Vladimir Marko7d157fc2017-05-10 16:29:23 +0100368 for (const auto& entry : try_block_info) {
369 uint32_t block_id = entry.first;
370 const DexFile::TryItem* try_item = entry.second;
371 HBasicBlock* try_block = graph_->GetBlocks()[block_id];
David Brazdil86ea7ee2016-02-16 09:26:07 +0000372 // NOTE: Do not use iterators because SplitEdge would invalidate them.
373 for (size_t i = 0, e = try_block->GetSuccessors().size(); i < e; ++i) {
374 HBasicBlock* successor = try_block->GetSuccessors()[i];
375
376 // If the successor is a try block, all of its predecessors must be
377 // covered by the same TryItem. Otherwise the previous pass would have
378 // created a non-throwing boundary block.
379 if (GetTryItem(successor, try_block_info) != nullptr) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +0100380 DCHECK_EQ(try_item, GetTryItem(successor, try_block_info));
David Brazdil86ea7ee2016-02-16 09:26:07 +0000381 continue;
382 }
383
384 // Insert TryBoundary and link to catch blocks.
385 HTryBoundary* try_exit =
Vladimir Marko69d310e2017-10-09 14:12:23 +0100386 new (allocator_) HTryBoundary(HTryBoundary::BoundaryKind::kExit, successor->GetDexPc());
David Brazdil86ea7ee2016-02-16 09:26:07 +0000387 graph_->SplitEdge(try_block, successor)->AddInstruction(try_exit);
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800388 LinkToCatchBlocks(try_exit, code_item_accessor_, try_item, catch_blocks);
David Brazdil86ea7ee2016-02-16 09:26:07 +0000389 }
390 }
391}
392
393bool HBasicBlockBuilder::Build() {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800394 DCHECK(code_item_accessor_.HasCodeItem());
David Brazdil86ea7ee2016-02-16 09:26:07 +0000395 DCHECK(graph_->GetBlocks().empty());
396
Vladimir Marko69d310e2017-10-09 14:12:23 +0100397 graph_->SetEntryBlock(new (allocator_) HBasicBlock(graph_, kNoDexPc));
398 graph_->SetExitBlock(new (allocator_) HBasicBlock(graph_, kNoDexPc));
David Brazdil86ea7ee2016-02-16 09:26:07 +0000399
400 // TODO(dbrazdil): Do CreateBranchTargets and ConnectBasicBlocks in one pass.
401 if (!CreateBranchTargets()) {
402 return false;
403 }
404
405 ConnectBasicBlocks();
406 InsertTryBoundaryBlocks();
407
408 return true;
409}
410
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000411void HBasicBlockBuilder::BuildIntrinsic() {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800412 DCHECK(!code_item_accessor_.HasCodeItem());
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000413 DCHECK(graph_->GetBlocks().empty());
414
415 // Create blocks.
416 HBasicBlock* entry_block = new (allocator_) HBasicBlock(graph_, kNoDexPc);
417 HBasicBlock* exit_block = new (allocator_) HBasicBlock(graph_, kNoDexPc);
418 HBasicBlock* body = MaybeCreateBlockAt(/* semantic_dex_pc */ kNoDexPc, /* store_dex_pc */ 0u);
419
420 // Add blocks to the graph.
421 graph_->AddBlock(entry_block);
422 graph_->AddBlock(body);
423 graph_->AddBlock(exit_block);
424 graph_->SetEntryBlock(entry_block);
425 graph_->SetExitBlock(exit_block);
426
427 // Connect blocks.
428 entry_block->AddSuccessor(body);
429 body->AddSuccessor(exit_block);
430}
431
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700432size_t HBasicBlockBuilder::GetQuickenIndex(uint32_t dex_pc) const {
433 return quicken_index_for_dex_pc_.Get(dex_pc);
434}
435
David Brazdil86ea7ee2016-02-16 09:26:07 +0000436} // namespace art