bool HBasicBlockBuilder::CreateBranchTargets() { // Create the first block for the dex instructions, single successor of the entry block.
MaybeCreateBlockAt(0u);
if (code_item_accessor_.TriesSize() != 0) { // Create branch targets at the start/end of the TryItem range. These are // places where the program might fall through into/out of the a block and // where TryBoundary instructions will be inserted later. Other edges which // enter/exit the try blocks are a result of branches/switches. for (const dex::TryItem& try_item : code_item_accessor_.TryItems()) {
uint32_t dex_pc_start = try_item.start_addr_;
uint32_t dex_pc_end = dex_pc_start + try_item.insn_count_;
MaybeCreateBlockAt(dex_pc_start); if (dex_pc_end < code_item_accessor_.InsnsSizeInCodeUnits()) { // TODO: Do not create block if the last instruction cannot fall through.
MaybeCreateBlockAt(dex_pc_end);
} elseif (dex_pc_end == code_item_accessor_.InsnsSizeInCodeUnits()) { // The TryItem spans until the very end of the CodeItem and therefore // cannot have any code afterwards.
} else { // The TryItem spans beyond the end of the CodeItem. This is invalid code.
VLOG(compiler) << "Not compiled: TryItem spans beyond the end of the CodeItem"; returnfalse;
}
}
// Iterate over all instructions and find branching instructions. Create blocks for // the locations these instructions branch to. for (const DexInstructionPcPair& pair : code_item_accessor_) { const uint32_t dex_pc = pair.DexPc(); const Instruction& instruction = pair.Inst();
// Create N-1 blocks where we will insert comparisons of the input value // against the Switch's case keys. if (table.ShouldBuildDecisionTree() && !s_it.IsLast()) { // Store the block under dex_pc of the current key at the switch data // instruction for uniqueness but give it the dex_pc of the SWITCH // instruction which it semantically belongs to.
MaybeCreateBlockAt(dex_pc, s_it.GetDexPcForCurrentIndex());
}
}
} elseif (instruction.Opcode() == Instruction::MOVE_EXCEPTION) { // End the basic block after MOVE_EXCEPTION. This simplifies the later // stage of TryBoundary-block insertion.
} else { continue;
}
if (instruction.CanFlowThrough()) {
DexInstructionIterator next(std::next(DexInstructionIterator(pair))); if (next == code_item_accessor_.end()) { // In the normal case we should never hit this but someone can artificially forge a dex // file to fall-through out the method code. In this case we bail out compilation.
VLOG(compiler) << "Not compiled: Fall-through beyond the CodeItem"; returnfalse;
}
MaybeCreateBlockAt(next.DexPc());
}
}
bool is_throwing_block = false; // Calculate the qucikening index here instead of CreateBranchTargets since it's easier to // calculate in dex_pc order. for (const DexInstructionPcPair& pair : code_item_accessor_) { const uint32_t dex_pc = pair.DexPc(); const Instruction& instruction = pair.Inst();
// Check if this dex_pc address starts a new basic block.
HBasicBlock* next_block = GetBlockAt(dex_pc); if (next_block != nullptr) { if (block != nullptr) { // Last instruction did not end its basic block but a new one starts here. // It must have been a block falling through into the next one.
block->AddSuccessor(next_block);
}
block = next_block;
is_throwing_block = false;
graph_->AddBlock(block);
}
if (block == nullptr) { // Ignore dead code. continue;
}
if (table.ShouldBuildDecisionTree() && !s_it.IsLast()) {
uint32_t next_case_dex_pc = s_it.GetDexPcForCurrentIndex();
HBasicBlock* next_case_block = GetBlockAt(next_case_dex_pc);
block->AddSuccessor(next_case_block);
block = next_case_block;
graph_->AddBlock(block);
}
}
} else { // Remaining code only applies to instructions which end their basic block. continue;
}
// Go to the next instruction in case we read dex PC below. if (instruction.CanFlowThrough()) {
block->AddSuccessor(GetBlockAt(std::next(DexInstructionIterator(pair)).DexPc()));
}
// The basic block ends here. Do not add any more instructions.
block = nullptr;
}
graph_->AddBlock(graph_->GetExitBlock());
}
// Returns the TryItem stored for `block` or nullptr if there is no info for it. staticconst dex::TryItem* GetTryItem(
HBasicBlock* block, const ScopedArenaSafeMap<uint32_t, const dex::TryItem*>& try_block_info) { auto iterator = try_block_info.find(block->GetBlockId()); return (iterator == try_block_info.end()) ? nullptr : iterator->second;
}
// Iterates over the exception handlers of `try_item`, finds the corresponding // catch blocks and makes them successors of `try_boundary`. The order of // successors matches the order in which runtime exception delivery searches // for a handler. staticvoid LinkToCatchBlocks(HTryBoundary* try_boundary, const CodeItemDataAccessor& accessor, const dex::TryItem* try_item, const ScopedArenaSafeMap<uint32_t, HBasicBlock*>& catch_blocks) { for (CatchHandlerIterator it(accessor.GetCatchHandlerData(try_item->handler_off_));
it.HasNext();
it.Next()) {
try_boundary->AddExceptionHandler(catch_blocks.Get(it.GetHandlerAddress()));
}
}
bool HBasicBlockBuilder::MightHaveLiveNormalPredecessors(HBasicBlock* catch_block) { if (kIsDebugBuild) {
DCHECK_NE(catch_block->GetDexPc(), kNoDexPc) << "Should not be called on synthetic blocks";
DCHECK(!graph_->GetEntryBlock()->GetSuccessors().empty())
<< "Basic blocks must have been created and connected"; for (HBasicBlock* predecessor : catch_block->GetPredecessors()) {
DCHECK(!predecessor->IsSingleTryBoundary())
<< "TryBoundary blocks must not have not been created yet";
}
}
const Instruction& first = code_item_accessor_.InstructionAt(catch_block->GetDexPc()); if (first.Opcode() == Instruction::MOVE_EXCEPTION) { // Verifier guarantees that if a catch block begins with MOVE_EXCEPTION then // it has no live normal predecessors. returnfalse;
} elseif (catch_block->GetPredecessors().empty()) { // Normal control-flow edges have already been created. Since block's list of // predecessors is empty, it cannot have any live or dead normal predecessors. returnfalse;
}
// The catch block has normal predecessors but we do not know which are live // and which will be removed during the initial DCE. Return `true` to signal // that it may have live normal predecessors. returntrue;
}
void HBasicBlockBuilder::InsertTryBoundaryBlocks() { if (code_item_accessor_.TriesSize() == 0) { return;
}
// Keep a map of all try blocks and their respective TryItems. We do not use // the block's pointer but rather its id to ensure deterministic iteration.
ScopedArenaSafeMap<uint32_t, const dex::TryItem*> try_block_info(
std::less<uint32_t>(), local_allocator_->Adapter(kArenaAllocGraphBuilder));
// Obtain TryItem information for blocks with throwing instructions, and split // blocks which are both try & catch to simplify the graph. for (HBasicBlock* block : graph_->GetBlocks()) { if (block->GetDexPc() == kNoDexPc) { continue;
}
// Do not bother creating exceptional edges for try blocks which have no // throwing instructions. In that case we simply assume that the block is // not covered by a TryItem. This prevents us from creating a throw-catch // loop for synchronized blocks. if (ContainsElement(throwing_blocks_, block)) { // Try to find a TryItem covering the block. const dex::TryItem* try_item = code_item_accessor_.FindTryItem(block->GetDexPc()); if (try_item != nullptr) { // Block throwing and in a TryItem. Store the try block information.
try_block_info.Put(block->GetBlockId(), try_item);
}
}
}
// Map from a handler dex_pc to the corresponding catch block.
ScopedArenaSafeMap<uint32_t, HBasicBlock*> catch_blocks(
std::less<uint32_t>(), local_allocator_->Adapter(kArenaAllocGraphBuilder));
// Iterate over catch blocks, create artifical landing pads if necessary to // simplify the CFG, and set metadata. const uint8_t* handlers_ptr = code_item_accessor_.GetCatchHandlerData();
uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr); for (uint32_t idx = 0; idx < handlers_size; ++idx) {
CatchHandlerIterator iterator(handlers_ptr); for (; iterator.HasNext(); iterator.Next()) {
uint32_t address = iterator.GetHandlerAddress(); auto existing = catch_blocks.find(address); if (existing != catch_blocks.end()) { // Catch block already processed.
TryCatchInformation* info = existing->second->GetTryCatchInformation(); if (iterator.GetHandlerTypeIndex() != info->GetCatchTypeIndex()) { // The handler is for multiple types. We could record all the types, but // doing class resolution here isn't ideal, and it's unclear whether wasting // the space in TryCatchInformation is worth it.
info->SetInvalidTypeIndex();
} continue;
}
// Check if we should create an artifical landing pad for the catch block. // We create one if the catch block is also a try block because we do not // have a strategy for inserting TryBoundaries on exceptional edges. // We also create one if the block might have normal predecessors so as to // simplify register allocation.
HBasicBlock* catch_block = GetBlockAt(address); bool is_try_block = (try_block_info.find(catch_block->GetBlockId()) != try_block_info.end()); if (is_try_block || MightHaveLiveNormalPredecessors(catch_block)) {
HBasicBlock* new_catch_block = HBasicBlock::Create(allocator_, graph_, address);
new_catch_block->AddInstruction(new (allocator_) HGoto(address));
new_catch_block->AddSuccessor(catch_block);
graph_->AddBlock(new_catch_block);
catch_block = new_catch_block;
}
// Do a pass over the try blocks and insert entering TryBoundaries where at // least one predecessor is not covered by the same TryItem as the try block. // We do not split each edge separately, but rather create one boundary block // that all predecessors are relinked to. This preserves loop headers (b/23895756). for (constauto& entry : try_block_info) {
uint32_t block_id = entry.first; const dex::TryItem* try_item = entry.second;
HBasicBlock* try_block = graph_->GetBlocks()[block_id]; for (HBasicBlock* predecessor : try_block->GetPredecessors()) { if (GetTryItem(predecessor, try_block_info) != try_item) { // Found a predecessor not covered by the same TryItem. Insert entering // boundary block.
HTryBoundary* try_entry = new (allocator_) HTryBoundary(
HTryBoundary::BoundaryKind::kEntry, try_block->GetDexPc());
try_block->CreateImmediateDominator()->AddInstruction(try_entry);
LinkToCatchBlocks(try_entry, code_item_accessor_, try_item, catch_blocks); break;
}
}
}
// Do a second pass over the try blocks and insert exit TryBoundaries where // the successor is not in the same TryItem. for (constauto& entry : try_block_info) {
uint32_t block_id = entry.first; const dex::TryItem* try_item = entry.second;
HBasicBlock* try_block = graph_->GetBlocks()[block_id]; // NOTE: Do not use iterators because SplitEdge would invalidate them. for (size_t i = 0, e = try_block->GetSuccessors().size(); i < e; ++i) {
HBasicBlock* successor = try_block->GetSuccessors()[i];
// If the successor is a try block, all of its predecessors must be // covered by the same TryItem. Otherwise the previous pass would have // created a non-throwing boundary block. if (GetTryItem(successor, try_block_info) != nullptr) {
DCHECK_EQ(try_item, GetTryItem(successor, try_block_info)); continue;
}
// Insert TryBoundary and link to catch blocks.
HTryBoundary* try_exit = new (allocator_) HTryBoundary(HTryBoundary::BoundaryKind::kExit, successor->GetDexPc());
graph_->SplitEdge(try_block, successor)->AddInstruction(try_exit);
LinkToCatchBlocks(try_exit, code_item_accessor_, try_item, catch_blocks);
}
}
}
void HBasicBlockBuilder::InsertSynthesizedLoopsForOsr() {
ArenaSet<uint32_t> targets(allocator_->Adapter(kArenaAllocGraphBuilder)); // Collect basic blocks that are targets of a negative branch. for (const DexInstructionPcPair& pair : code_item_accessor_) { const uint32_t dex_pc = pair.DexPc(); const Instruction& instruction = pair.Inst(); if (instruction.IsBranch()) {
uint32_t target_dex_pc = dex_pc + instruction.GetTargetOffset(); if (target_dex_pc < dex_pc) {
HBasicBlock* block = GetBlockAt(target_dex_pc);
CHECK_NE(kNoDexPc, block->GetDexPc());
targets.insert(block->GetBlockId());
}
} elseif (instruction.IsSwitch()) {
DexSwitchTable table(instruction, dex_pc); for (DexSwitchTableIterator s_it(table); !s_it.Done(); s_it.Advance()) {
uint32_t target_dex_pc = dex_pc + s_it.CurrentTargetOffset(); if (target_dex_pc < dex_pc) {
HBasicBlock* block = GetBlockAt(target_dex_pc);
CHECK_NE(kNoDexPc, block->GetDexPc());
targets.insert(block->GetBlockId());
}
}
}
}
// Insert synthesized loops before the collected blocks. for (uint32_t block_id : targets) {
HBasicBlock* block = graph_->GetBlocks()[block_id];
HBasicBlock* loop_block = HBasicBlock::Create(allocator_, graph_, block->GetDexPc());
graph_->AddBlock(loop_block); while (!block->GetPredecessors().empty()) {
block->GetPredecessors()[0]->ReplaceSuccessor(block, loop_block);
}
loop_block->AddSuccessor(loop_block);
loop_block->AddSuccessor(block); // We loop on false - we know this won't be optimized later on as the loop // is marked irreducible, which disables loop optimizations.
loop_block->AddInstruction(new (allocator_) HIf(graph_->GetIntConstant(0), kNoDexPc));
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.