Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright (C) 2014 The Android Open Source Project |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 18 | #include "dex_file.h" |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame^] | 19 | #include "dex_file-inl.h" |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 20 | #include "dex_instruction.h" |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 21 | #include "dex_instruction-inl.h" |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 22 | #include "builder.h" |
| 23 | #include "nodes.h" |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame^] | 24 | #include "primitive.h" |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 25 | |
| 26 | namespace art { |
| 27 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 28 | void HGraphBuilder::InitializeLocals(int count) { |
| 29 | locals_.SetSize(count); |
| 30 | for (int i = 0; i < count; i++) { |
| 31 | HLocal* local = new (arena_) HLocal(i); |
| 32 | entry_block_->AddInstruction(local); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 33 | locals_.Put(i, local); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 34 | } |
| 35 | } |
| 36 | |
| 37 | static bool CanHandleCodeItem(const DexFile::CodeItem& code_item) { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 38 | if (code_item.tries_size_ > 0) { |
| 39 | return false; |
| 40 | } else if (code_item.outs_size_ > 0) { |
| 41 | return false; |
| 42 | } else if (code_item.ins_size_ > 0) { |
| 43 | return false; |
| 44 | } |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 45 | return true; |
| 46 | } |
| 47 | |
| 48 | HGraph* HGraphBuilder::BuildGraph(const DexFile::CodeItem& code_item) { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 49 | if (!CanHandleCodeItem(code_item)) { |
| 50 | return nullptr; |
| 51 | } |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 52 | |
| 53 | const uint16_t* code_ptr = code_item.insns_; |
| 54 | const uint16_t* code_end = code_item.insns_ + code_item.insns_size_in_code_units_; |
| 55 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 56 | // Setup the graph with the entry block and exit block. |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 57 | graph_ = new (arena_) HGraph(arena_); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 58 | entry_block_ = new (arena_) HBasicBlock(graph_); |
| 59 | graph_->AddBlock(entry_block_); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 60 | exit_block_ = new (arena_) HBasicBlock(graph_); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 61 | graph_->SetEntryBlock(entry_block_); |
| 62 | graph_->SetExitBlock(exit_block_); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 63 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 64 | InitializeLocals(code_item.registers_size_); |
| 65 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 66 | // To avoid splitting blocks, we compute ahead of time the instructions that |
| 67 | // start a new block, and create these blocks. |
| 68 | ComputeBranchTargets(code_ptr, code_end); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 69 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 70 | size_t dex_offset = 0; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 71 | while (code_ptr < code_end) { |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 72 | // Update the current block if dex_offset starts a new block. |
| 73 | MaybeUpdateCurrentBlock(dex_offset); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 74 | const Instruction& instruction = *Instruction::At(code_ptr); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 75 | if (!AnalyzeDexInstruction(instruction, dex_offset)) return nullptr; |
| 76 | dex_offset += instruction.SizeInCodeUnits(); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 77 | code_ptr += instruction.SizeInCodeUnits(); |
| 78 | } |
| 79 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 80 | // Add the exit block at the end to give it the highest id. |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 81 | graph_->AddBlock(exit_block_); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 82 | exit_block_->AddInstruction(new (arena_) HExit()); |
| 83 | entry_block_->AddInstruction(new (arena_) HGoto()); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 84 | return graph_; |
| 85 | } |
| 86 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 87 | void HGraphBuilder::MaybeUpdateCurrentBlock(size_t index) { |
| 88 | HBasicBlock* block = FindBlockStartingAt(index); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 89 | if (block == nullptr) { |
| 90 | return; |
| 91 | } |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 92 | |
| 93 | if (current_block_ != nullptr) { |
| 94 | // Branching instructions clear current_block, so we know |
| 95 | // the last instruction of the current block is not a branching |
| 96 | // instruction. We add an unconditional goto to the found block. |
| 97 | current_block_->AddInstruction(new (arena_) HGoto()); |
| 98 | current_block_->AddSuccessor(block); |
| 99 | } |
| 100 | graph_->AddBlock(block); |
| 101 | current_block_ = block; |
| 102 | } |
| 103 | |
| 104 | void HGraphBuilder::ComputeBranchTargets(const uint16_t* code_ptr, const uint16_t* code_end) { |
| 105 | // TODO: Support switch instructions. |
| 106 | branch_targets_.SetSize(code_end - code_ptr); |
| 107 | |
| 108 | // Create the first block for the dex instructions, single successor of the entry block. |
| 109 | HBasicBlock* block = new (arena_) HBasicBlock(graph_); |
| 110 | branch_targets_.Put(0, block); |
| 111 | entry_block_->AddSuccessor(block); |
| 112 | |
| 113 | // Iterate over all instructions and find branching instructions. Create blocks for |
| 114 | // the locations these instructions branch to. |
| 115 | size_t dex_offset = 0; |
| 116 | while (code_ptr < code_end) { |
| 117 | const Instruction& instruction = *Instruction::At(code_ptr); |
| 118 | if (instruction.IsBranch()) { |
| 119 | int32_t target = instruction.GetTargetOffset() + dex_offset; |
| 120 | // Create a block for the target instruction. |
| 121 | if (FindBlockStartingAt(target) == nullptr) { |
| 122 | block = new (arena_) HBasicBlock(graph_); |
| 123 | branch_targets_.Put(target, block); |
| 124 | } |
| 125 | dex_offset += instruction.SizeInCodeUnits(); |
| 126 | code_ptr += instruction.SizeInCodeUnits(); |
| 127 | if ((code_ptr < code_end) && (FindBlockStartingAt(dex_offset) == nullptr)) { |
| 128 | block = new (arena_) HBasicBlock(graph_); |
| 129 | branch_targets_.Put(dex_offset, block); |
| 130 | } |
| 131 | } else { |
| 132 | code_ptr += instruction.SizeInCodeUnits(); |
| 133 | dex_offset += instruction.SizeInCodeUnits(); |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | HBasicBlock* HGraphBuilder::FindBlockStartingAt(int32_t index) const { |
| 139 | DCHECK_GE(index, 0); |
| 140 | return branch_targets_.Get(index); |
| 141 | } |
| 142 | |
| 143 | bool HGraphBuilder::AnalyzeDexInstruction(const Instruction& instruction, int32_t dex_offset) { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 144 | if (current_block_ == nullptr) { |
| 145 | return true; // Dead code |
| 146 | } |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 147 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 148 | switch (instruction.Opcode()) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 149 | case Instruction::CONST_4: { |
| 150 | int32_t register_index = instruction.VRegA(); |
| 151 | HIntConstant* constant = GetConstant(instruction.VRegB_11n()); |
| 152 | UpdateLocal(register_index, constant); |
| 153 | break; |
| 154 | } |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 155 | |
| 156 | case Instruction::RETURN_VOID: { |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 157 | current_block_->AddInstruction(new (arena_) HReturnVoid()); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 158 | current_block_->AddSuccessor(exit_block_); |
| 159 | current_block_ = nullptr; |
| 160 | break; |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 163 | case Instruction::IF_EQ: { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 164 | HInstruction* first = LoadLocal(instruction.VRegA()); |
| 165 | HInstruction* second = LoadLocal(instruction.VRegB()); |
| 166 | current_block_->AddInstruction(new (arena_) HEqual(first, second)); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 167 | current_block_->AddInstruction(new (arena_) HIf(current_block_->GetLastInstruction())); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 168 | HBasicBlock* target = FindBlockStartingAt(instruction.GetTargetOffset() + dex_offset); |
| 169 | DCHECK(target != nullptr); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 170 | current_block_->AddSuccessor(target); |
| 171 | target = FindBlockStartingAt(dex_offset + instruction.SizeInCodeUnits()); |
| 172 | DCHECK(target != nullptr); |
| 173 | current_block_->AddSuccessor(target); |
| 174 | current_block_ = nullptr; |
| 175 | break; |
| 176 | } |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 177 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 178 | case Instruction::GOTO: |
| 179 | case Instruction::GOTO_16: |
| 180 | case Instruction::GOTO_32: { |
| 181 | HBasicBlock* target = FindBlockStartingAt(instruction.GetTargetOffset() + dex_offset); |
| 182 | DCHECK(target != nullptr); |
| 183 | current_block_->AddInstruction(new (arena_) HGoto()); |
| 184 | current_block_->AddSuccessor(target); |
| 185 | current_block_ = nullptr; |
| 186 | break; |
| 187 | } |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 188 | |
| 189 | case Instruction::RETURN: { |
| 190 | HInstruction* value = LoadLocal(instruction.VRegA()); |
| 191 | current_block_->AddInstruction(new (arena_) HReturn(value)); |
| 192 | current_block_->AddSuccessor(exit_block_); |
| 193 | current_block_ = nullptr; |
| 194 | break; |
| 195 | } |
| 196 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame^] | 197 | case Instruction::INVOKE_STATIC: { |
| 198 | uint32_t method_idx = instruction.VRegB_35c(); |
| 199 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
| 200 | uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_; |
| 201 | const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx); |
| 202 | const size_t number_of_arguments = instruction.VRegA_35c(); |
| 203 | if (number_of_arguments != 0) { |
| 204 | return false; |
| 205 | } |
| 206 | if (Primitive::GetType(descriptor[0]) != Primitive::kPrimVoid) { |
| 207 | return false; |
| 208 | } |
| 209 | current_block_->AddInstruction(new (arena_) HInvokeStatic( |
| 210 | arena_, number_of_arguments, dex_offset, method_idx)); |
| 211 | break; |
| 212 | } |
| 213 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 214 | case Instruction::NOP: |
| 215 | break; |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 216 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 217 | default: |
| 218 | return false; |
| 219 | } |
| 220 | return true; |
| 221 | } |
| 222 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 223 | HIntConstant* HGraphBuilder::GetConstant0() { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 224 | if (constant0_ != nullptr) { |
| 225 | return constant0_; |
| 226 | } |
| 227 | constant0_ = new(arena_) HIntConstant(0); |
| 228 | entry_block_->AddInstruction(constant0_); |
| 229 | return constant0_; |
| 230 | } |
| 231 | |
| 232 | HIntConstant* HGraphBuilder::GetConstant1() { |
| 233 | if (constant1_ != nullptr) { |
| 234 | return constant1_; |
| 235 | } |
| 236 | constant1_ = new(arena_) HIntConstant(1); |
| 237 | entry_block_->AddInstruction(constant1_); |
| 238 | return constant1_; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | HIntConstant* HGraphBuilder::GetConstant(int constant) { |
| 242 | switch (constant) { |
| 243 | case 0: return GetConstant0(); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 244 | case 1: return GetConstant1(); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 245 | default: { |
| 246 | HIntConstant* instruction = new (arena_) HIntConstant(constant); |
| 247 | entry_block_->AddInstruction(instruction); |
| 248 | return instruction; |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | HLocal* HGraphBuilder::GetLocalAt(int register_index) const { |
| 254 | return locals_.Get(register_index); |
| 255 | } |
| 256 | |
| 257 | void HGraphBuilder::UpdateLocal(int register_index, HInstruction* instruction) const { |
| 258 | HLocal* local = GetLocalAt(register_index); |
| 259 | current_block_->AddInstruction(new (arena_) HStoreLocal(local, instruction)); |
| 260 | } |
| 261 | |
| 262 | HInstruction* HGraphBuilder::LoadLocal(int register_index) const { |
| 263 | HLocal* local = GetLocalAt(register_index); |
| 264 | current_block_->AddInstruction(new (arena_) HLoadLocal(local)); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 265 | return current_block_->GetLastInstruction(); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 266 | } |
| 267 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 268 | } // namespace art |