Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1 | /* |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 2 | * Copyright (C) 2014 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 | |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 17 | #include "builder.h" |
| 18 | |
| 19 | #include "class_linker.h" |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 20 | #include "dex_file.h" |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 21 | #include "dex_file-inl.h" |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 22 | #include "dex_instruction.h" |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 23 | #include "dex_instruction-inl.h" |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 24 | #include "driver/compiler_driver-inl.h" |
| 25 | #include "mirror/art_field.h" |
| 26 | #include "mirror/art_field-inl.h" |
| 27 | #include "mirror/class_loader.h" |
| 28 | #include "mirror/dex_cache.h" |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 29 | #include "nodes.h" |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 30 | #include "primitive.h" |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 31 | #include "scoped_thread_state_change.h" |
| 32 | #include "thread.h" |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 33 | |
| 34 | namespace art { |
| 35 | |
Nicolas Geoffray | f12feb8 | 2014-07-17 18:32:41 +0100 | [diff] [blame] | 36 | /** |
| 37 | * Helper class to add HTemporary instructions. This class is used when |
| 38 | * converting a DEX instruction to multiple HInstruction, and where those |
| 39 | * instructions do not die at the following instruction, but instead spans |
| 40 | * multiple instructions. |
| 41 | */ |
| 42 | class Temporaries : public ValueObject { |
| 43 | public: |
| 44 | Temporaries(HGraph* graph, size_t count) : graph_(graph), count_(count), index_(0) { |
| 45 | graph_->UpdateNumberOfTemporaries(count_); |
| 46 | } |
| 47 | |
| 48 | void Add(HInstruction* instruction) { |
| 49 | // We currently only support vreg size temps. |
| 50 | DCHECK(instruction->GetType() != Primitive::kPrimLong |
| 51 | && instruction->GetType() != Primitive::kPrimDouble); |
| 52 | HInstruction* temp = new (graph_->GetArena()) HTemporary(index_++); |
| 53 | instruction->GetBlock()->AddInstruction(temp); |
| 54 | DCHECK(temp->GetPrevious() == instruction); |
| 55 | } |
| 56 | |
| 57 | private: |
| 58 | HGraph* const graph_; |
| 59 | |
| 60 | // The total number of temporaries that will be used. |
| 61 | const size_t count_; |
| 62 | |
| 63 | // Current index in the temporary stack, updated by `Add`. |
| 64 | size_t index_; |
| 65 | }; |
| 66 | |
Nicolas Geoffray | abed4d0 | 2014-07-14 15:24:11 +0100 | [diff] [blame] | 67 | static bool IsTypeSupported(Primitive::Type type) { |
| 68 | return type != Primitive::kPrimFloat && type != Primitive::kPrimDouble; |
| 69 | } |
| 70 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 71 | void HGraphBuilder::InitializeLocals(uint16_t count) { |
| 72 | graph_->SetNumberOfVRegs(count); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 73 | locals_.SetSize(count); |
| 74 | for (int i = 0; i < count; i++) { |
| 75 | HLocal* local = new (arena_) HLocal(i); |
| 76 | entry_block_->AddInstruction(local); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 77 | locals_.Put(i, local); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 81 | bool HGraphBuilder::InitializeParameters(uint16_t number_of_parameters) { |
| 82 | // dex_compilation_unit_ is null only when unit testing. |
| 83 | if (dex_compilation_unit_ == nullptr) { |
| 84 | return true; |
| 85 | } |
| 86 | |
| 87 | graph_->SetNumberOfInVRegs(number_of_parameters); |
| 88 | const char* shorty = dex_compilation_unit_->GetShorty(); |
| 89 | int locals_index = locals_.Size() - number_of_parameters; |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 90 | int parameter_index = 0; |
| 91 | |
| 92 | if (!dex_compilation_unit_->IsStatic()) { |
| 93 | // Add the implicit 'this' argument, not expressed in the signature. |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 94 | HParameterValue* parameter = |
| 95 | new (arena_) HParameterValue(parameter_index++, Primitive::kPrimNot); |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 96 | entry_block_->AddInstruction(parameter); |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 97 | HLocal* local = GetLocalAt(locals_index++); |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 98 | entry_block_->AddInstruction(new (arena_) HStoreLocal(local, parameter)); |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 99 | number_of_parameters--; |
| 100 | } |
| 101 | |
| 102 | uint32_t pos = 1; |
| 103 | for (int i = 0; i < number_of_parameters; i++) { |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 104 | HParameterValue* parameter = |
| 105 | new (arena_) HParameterValue(parameter_index++, Primitive::GetType(shorty[pos++])); |
| 106 | entry_block_->AddInstruction(parameter); |
| 107 | HLocal* local = GetLocalAt(locals_index++); |
| 108 | // Store the parameter value in the local that the dex code will use |
| 109 | // to reference that parameter. |
| 110 | entry_block_->AddInstruction(new (arena_) HStoreLocal(local, parameter)); |
| 111 | bool is_wide = (parameter->GetType() == Primitive::kPrimLong) |
| 112 | || (parameter->GetType() == Primitive::kPrimDouble); |
| 113 | if (is_wide) { |
| 114 | i++; |
| 115 | locals_index++; |
| 116 | parameter_index++; |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | return true; |
| 120 | } |
| 121 | |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 122 | template<typename T> |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 123 | void HGraphBuilder::If_22t(const Instruction& instruction, uint32_t dex_offset) { |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 124 | int32_t target_offset = instruction.GetTargetOffset(); |
| 125 | PotentiallyAddSuspendCheck(target_offset, dex_offset); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 126 | HInstruction* first = LoadLocal(instruction.VRegA(), Primitive::kPrimInt); |
| 127 | HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt); |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 128 | T* comparison = new (arena_) T(first, second); |
| 129 | current_block_->AddInstruction(comparison); |
| 130 | HInstruction* ifinst = new (arena_) HIf(comparison); |
| 131 | current_block_->AddInstruction(ifinst); |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 132 | HBasicBlock* target = FindBlockStartingAt(dex_offset + target_offset); |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 133 | DCHECK(target != nullptr); |
| 134 | current_block_->AddSuccessor(target); |
| 135 | target = FindBlockStartingAt(dex_offset + instruction.SizeInCodeUnits()); |
| 136 | DCHECK(target != nullptr); |
| 137 | current_block_->AddSuccessor(target); |
| 138 | current_block_ = nullptr; |
| 139 | } |
| 140 | |
| 141 | template<typename T> |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 142 | void HGraphBuilder::If_21t(const Instruction& instruction, uint32_t dex_offset) { |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 143 | int32_t target_offset = instruction.GetTargetOffset(); |
| 144 | PotentiallyAddSuspendCheck(target_offset, dex_offset); |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 145 | HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt); |
| 146 | T* comparison = new (arena_) T(value, GetIntConstant(0)); |
| 147 | current_block_->AddInstruction(comparison); |
| 148 | HInstruction* ifinst = new (arena_) HIf(comparison); |
| 149 | current_block_->AddInstruction(ifinst); |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 150 | HBasicBlock* target = FindBlockStartingAt(dex_offset + target_offset); |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 151 | DCHECK(target != nullptr); |
| 152 | current_block_->AddSuccessor(target); |
| 153 | target = FindBlockStartingAt(dex_offset + instruction.SizeInCodeUnits()); |
| 154 | DCHECK(target != nullptr); |
| 155 | current_block_->AddSuccessor(target); |
| 156 | current_block_ = nullptr; |
| 157 | } |
| 158 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 159 | HGraph* HGraphBuilder::BuildGraph(const DexFile::CodeItem& code_item) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 160 | const uint16_t* code_ptr = code_item.insns_; |
| 161 | const uint16_t* code_end = code_item.insns_ + code_item.insns_size_in_code_units_; |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 162 | code_start_ = code_ptr; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 163 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 164 | // Setup the graph with the entry block and exit block. |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 165 | graph_ = new (arena_) HGraph(arena_); |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 166 | entry_block_ = new (arena_) HBasicBlock(graph_, 0); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 167 | graph_->AddBlock(entry_block_); |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 168 | exit_block_ = new (arena_) HBasicBlock(graph_, kNoDexPc); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 169 | graph_->SetEntryBlock(entry_block_); |
| 170 | graph_->SetExitBlock(exit_block_); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 171 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 172 | InitializeLocals(code_item.registers_size_); |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 173 | graph_->UpdateMaximumNumberOfOutVRegs(code_item.outs_size_); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 174 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 175 | // To avoid splitting blocks, we compute ahead of time the instructions that |
| 176 | // start a new block, and create these blocks. |
| 177 | ComputeBranchTargets(code_ptr, code_end); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 178 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 179 | // Also create blocks for catch handlers. |
| 180 | if (code_item.tries_size_ != 0) { |
| 181 | const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(code_item, 0); |
| 182 | uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr); |
| 183 | for (uint32_t idx = 0; idx < handlers_size; ++idx) { |
| 184 | CatchHandlerIterator iterator(handlers_ptr); |
| 185 | for (; iterator.HasNext(); iterator.Next()) { |
| 186 | uint32_t address = iterator.GetHandlerAddress(); |
| 187 | HBasicBlock* block = FindBlockStartingAt(address); |
| 188 | if (block == nullptr) { |
| 189 | block = new (arena_) HBasicBlock(graph_, address); |
| 190 | branch_targets_.Put(address, block); |
| 191 | } |
| 192 | block->SetIsCatchBlock(); |
| 193 | } |
| 194 | handlers_ptr = iterator.EndDataPointer(); |
| 195 | } |
| 196 | } |
| 197 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 198 | if (!InitializeParameters(code_item.ins_size_)) { |
| 199 | return nullptr; |
| 200 | } |
| 201 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 202 | size_t dex_offset = 0; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 203 | while (code_ptr < code_end) { |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 204 | // Update the current block if dex_offset starts a new block. |
| 205 | MaybeUpdateCurrentBlock(dex_offset); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 206 | const Instruction& instruction = *Instruction::At(code_ptr); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 207 | if (!AnalyzeDexInstruction(instruction, dex_offset)) return nullptr; |
| 208 | dex_offset += instruction.SizeInCodeUnits(); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 209 | code_ptr += instruction.SizeInCodeUnits(); |
| 210 | } |
| 211 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 212 | // 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] | 213 | graph_->AddBlock(exit_block_); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 214 | exit_block_->AddInstruction(new (arena_) HExit()); |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 215 | // Add the suspend check to the entry block. |
| 216 | entry_block_->AddInstruction(new (arena_) HSuspendCheck(0)); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 217 | entry_block_->AddInstruction(new (arena_) HGoto()); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 218 | return graph_; |
| 219 | } |
| 220 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 221 | void HGraphBuilder::MaybeUpdateCurrentBlock(size_t index) { |
| 222 | HBasicBlock* block = FindBlockStartingAt(index); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 223 | if (block == nullptr) { |
| 224 | return; |
| 225 | } |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 226 | |
| 227 | if (current_block_ != nullptr) { |
| 228 | // Branching instructions clear current_block, so we know |
| 229 | // the last instruction of the current block is not a branching |
| 230 | // instruction. We add an unconditional goto to the found block. |
| 231 | current_block_->AddInstruction(new (arena_) HGoto()); |
| 232 | current_block_->AddSuccessor(block); |
| 233 | } |
| 234 | graph_->AddBlock(block); |
| 235 | current_block_ = block; |
| 236 | } |
| 237 | |
| 238 | void HGraphBuilder::ComputeBranchTargets(const uint16_t* code_ptr, const uint16_t* code_end) { |
| 239 | // TODO: Support switch instructions. |
| 240 | branch_targets_.SetSize(code_end - code_ptr); |
| 241 | |
| 242 | // Create the first block for the dex instructions, single successor of the entry block. |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 243 | HBasicBlock* block = new (arena_) HBasicBlock(graph_, 0); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 244 | branch_targets_.Put(0, block); |
| 245 | entry_block_->AddSuccessor(block); |
| 246 | |
| 247 | // Iterate over all instructions and find branching instructions. Create blocks for |
| 248 | // the locations these instructions branch to. |
| 249 | size_t dex_offset = 0; |
| 250 | while (code_ptr < code_end) { |
| 251 | const Instruction& instruction = *Instruction::At(code_ptr); |
| 252 | if (instruction.IsBranch()) { |
| 253 | int32_t target = instruction.GetTargetOffset() + dex_offset; |
| 254 | // Create a block for the target instruction. |
| 255 | if (FindBlockStartingAt(target) == nullptr) { |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 256 | block = new (arena_) HBasicBlock(graph_, target); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 257 | branch_targets_.Put(target, block); |
| 258 | } |
| 259 | dex_offset += instruction.SizeInCodeUnits(); |
| 260 | code_ptr += instruction.SizeInCodeUnits(); |
| 261 | if ((code_ptr < code_end) && (FindBlockStartingAt(dex_offset) == nullptr)) { |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 262 | block = new (arena_) HBasicBlock(graph_, dex_offset); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 263 | branch_targets_.Put(dex_offset, block); |
| 264 | } |
| 265 | } else { |
| 266 | code_ptr += instruction.SizeInCodeUnits(); |
| 267 | dex_offset += instruction.SizeInCodeUnits(); |
| 268 | } |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | HBasicBlock* HGraphBuilder::FindBlockStartingAt(int32_t index) const { |
| 273 | DCHECK_GE(index, 0); |
| 274 | return branch_targets_.Get(index); |
| 275 | } |
| 276 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 277 | template<typename T> |
Roland Levillain | 88cb175 | 2014-10-20 16:36:47 +0100 | [diff] [blame] | 278 | void HGraphBuilder::Unop_12x(const Instruction& instruction, Primitive::Type type) { |
| 279 | HInstruction* first = LoadLocal(instruction.VRegB(), type); |
| 280 | current_block_->AddInstruction(new (arena_) T(type, first)); |
| 281 | UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction()); |
| 282 | } |
| 283 | |
Roland Levillain | dff1f28 | 2014-11-05 14:15:05 +0000 | [diff] [blame] | 284 | void HGraphBuilder::Conversion_12x(const Instruction& instruction, |
| 285 | Primitive::Type input_type, |
| 286 | Primitive::Type result_type) { |
| 287 | HInstruction* first = LoadLocal(instruction.VRegB(), input_type); |
| 288 | current_block_->AddInstruction(new (arena_) HTypeConversion(result_type, first)); |
| 289 | UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction()); |
| 290 | } |
| 291 | |
Roland Levillain | 88cb175 | 2014-10-20 16:36:47 +0100 | [diff] [blame] | 292 | template<typename T> |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 293 | void HGraphBuilder::Binop_23x(const Instruction& instruction, Primitive::Type type) { |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 294 | HInstruction* first = LoadLocal(instruction.VRegB(), type); |
| 295 | HInstruction* second = LoadLocal(instruction.VRegC(), type); |
| 296 | current_block_->AddInstruction(new (arena_) T(type, first, second)); |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 297 | UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction()); |
| 298 | } |
| 299 | |
| 300 | template<typename T> |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 301 | void HGraphBuilder::Binop_12x(const Instruction& instruction, Primitive::Type type) { |
| 302 | HInstruction* first = LoadLocal(instruction.VRegA(), type); |
| 303 | HInstruction* second = LoadLocal(instruction.VRegB(), type); |
| 304 | current_block_->AddInstruction(new (arena_) T(type, first, second)); |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 305 | UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction()); |
| 306 | } |
| 307 | |
| 308 | template<typename T> |
| 309 | void HGraphBuilder::Binop_22s(const Instruction& instruction, bool reverse) { |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 310 | HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt); |
| 311 | HInstruction* second = GetIntConstant(instruction.VRegC_22s()); |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 312 | if (reverse) { |
| 313 | std::swap(first, second); |
| 314 | } |
| 315 | current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second)); |
| 316 | UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction()); |
| 317 | } |
| 318 | |
| 319 | template<typename T> |
| 320 | void HGraphBuilder::Binop_22b(const Instruction& instruction, bool reverse) { |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 321 | HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt); |
| 322 | HInstruction* second = GetIntConstant(instruction.VRegC_22b()); |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 323 | if (reverse) { |
| 324 | std::swap(first, second); |
| 325 | } |
| 326 | current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second)); |
| 327 | UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction()); |
| 328 | } |
| 329 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 330 | void HGraphBuilder::BuildReturn(const Instruction& instruction, Primitive::Type type) { |
| 331 | if (type == Primitive::kPrimVoid) { |
| 332 | current_block_->AddInstruction(new (arena_) HReturnVoid()); |
| 333 | } else { |
| 334 | HInstruction* value = LoadLocal(instruction.VRegA(), type); |
| 335 | current_block_->AddInstruction(new (arena_) HReturn(value)); |
| 336 | } |
| 337 | current_block_->AddSuccessor(exit_block_); |
| 338 | current_block_ = nullptr; |
| 339 | } |
| 340 | |
| 341 | bool HGraphBuilder::BuildInvoke(const Instruction& instruction, |
| 342 | uint32_t dex_offset, |
| 343 | uint32_t method_idx, |
| 344 | uint32_t number_of_vreg_arguments, |
| 345 | bool is_range, |
| 346 | uint32_t* args, |
| 347 | uint32_t register_index) { |
Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 348 | Instruction::Code opcode = instruction.Opcode(); |
| 349 | InvokeType invoke_type; |
| 350 | switch (opcode) { |
| 351 | case Instruction::INVOKE_STATIC: |
| 352 | case Instruction::INVOKE_STATIC_RANGE: |
| 353 | invoke_type = kStatic; |
| 354 | break; |
| 355 | case Instruction::INVOKE_DIRECT: |
| 356 | case Instruction::INVOKE_DIRECT_RANGE: |
| 357 | invoke_type = kDirect; |
| 358 | break; |
| 359 | case Instruction::INVOKE_VIRTUAL: |
| 360 | case Instruction::INVOKE_VIRTUAL_RANGE: |
| 361 | invoke_type = kVirtual; |
| 362 | break; |
| 363 | case Instruction::INVOKE_INTERFACE: |
| 364 | case Instruction::INVOKE_INTERFACE_RANGE: |
| 365 | invoke_type = kInterface; |
| 366 | break; |
| 367 | case Instruction::INVOKE_SUPER_RANGE: |
| 368 | case Instruction::INVOKE_SUPER: |
| 369 | invoke_type = kSuper; |
| 370 | break; |
| 371 | default: |
| 372 | LOG(FATAL) << "Unexpected invoke op: " << opcode; |
| 373 | return false; |
| 374 | } |
| 375 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 376 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
| 377 | const DexFile::ProtoId& proto_id = dex_file_->GetProtoId(method_id.proto_idx_); |
| 378 | const char* descriptor = dex_file_->StringDataByIdx(proto_id.shorty_idx_); |
| 379 | Primitive::Type return_type = Primitive::GetType(descriptor[0]); |
Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 380 | bool is_instance_call = invoke_type != kStatic; |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 381 | const size_t number_of_arguments = strlen(descriptor) - (is_instance_call ? 0 : 1); |
| 382 | |
Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 383 | HInvoke* invoke = nullptr; |
| 384 | if (invoke_type == kVirtual) { |
| 385 | MethodReference target_method(dex_file_, method_idx); |
| 386 | uintptr_t direct_code; |
| 387 | uintptr_t direct_method; |
| 388 | int vtable_index; |
| 389 | // TODO: Add devirtualization support. |
| 390 | compiler_driver_->ComputeInvokeInfo(dex_compilation_unit_, dex_offset, true, true, |
| 391 | &invoke_type, &target_method, &vtable_index, |
| 392 | &direct_code, &direct_method); |
| 393 | if (vtable_index == -1) { |
| 394 | return false; |
| 395 | } |
| 396 | invoke = new (arena_) HInvokeVirtual( |
| 397 | arena_, number_of_arguments, return_type, dex_offset, vtable_index); |
| 398 | } else { |
| 399 | // Treat invoke-direct like static calls for now. |
| 400 | invoke = new (arena_) HInvokeStatic( |
| 401 | arena_, number_of_arguments, return_type, dex_offset, method_idx); |
| 402 | } |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 403 | |
| 404 | size_t start_index = 0; |
Nicolas Geoffray | f12feb8 | 2014-07-17 18:32:41 +0100 | [diff] [blame] | 405 | Temporaries temps(graph_, is_instance_call ? 1 : 0); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 406 | if (is_instance_call) { |
| 407 | HInstruction* arg = LoadLocal(is_range ? register_index : args[0], Primitive::kPrimNot); |
Nicolas Geoffray | f12feb8 | 2014-07-17 18:32:41 +0100 | [diff] [blame] | 408 | HNullCheck* null_check = new (arena_) HNullCheck(arg, dex_offset); |
| 409 | current_block_->AddInstruction(null_check); |
| 410 | temps.Add(null_check); |
| 411 | invoke->SetArgumentAt(0, null_check); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 412 | start_index = 1; |
| 413 | } |
| 414 | |
| 415 | uint32_t descriptor_index = 1; |
| 416 | uint32_t argument_index = start_index; |
| 417 | for (size_t i = start_index; i < number_of_vreg_arguments; i++, argument_index++) { |
| 418 | Primitive::Type type = Primitive::GetType(descriptor[descriptor_index++]); |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 419 | bool is_wide = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble); |
| 420 | if (!is_range && is_wide && args[i] + 1 != args[i + 1]) { |
Nicolas Geoffray | abed4d0 | 2014-07-14 15:24:11 +0100 | [diff] [blame] | 421 | LOG(WARNING) << "Non sequential register pair in " << dex_compilation_unit_->GetSymbol() |
| 422 | << " at " << dex_offset; |
| 423 | // We do not implement non sequential register pair. |
| 424 | return false; |
| 425 | } |
| 426 | HInstruction* arg = LoadLocal(is_range ? register_index + i : args[i], type); |
| 427 | invoke->SetArgumentAt(argument_index, arg); |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 428 | if (is_wide) { |
Nicolas Geoffray | abed4d0 | 2014-07-14 15:24:11 +0100 | [diff] [blame] | 429 | i++; |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 430 | } |
| 431 | } |
| 432 | |
| 433 | DCHECK_EQ(argument_index, number_of_arguments); |
| 434 | current_block_->AddInstruction(invoke); |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 435 | latest_result_ = invoke; |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 436 | return true; |
| 437 | } |
| 438 | |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 439 | bool HGraphBuilder::BuildInstanceFieldAccess(const Instruction& instruction, |
| 440 | uint32_t dex_offset, |
| 441 | bool is_put) { |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 442 | uint32_t source_or_dest_reg = instruction.VRegA_22c(); |
| 443 | uint32_t obj_reg = instruction.VRegB_22c(); |
| 444 | uint16_t field_index = instruction.VRegC_22c(); |
| 445 | |
| 446 | ScopedObjectAccess soa(Thread::Current()); |
| 447 | StackHandleScope<1> hs(soa.Self()); |
| 448 | Handle<mirror::ArtField> resolved_field(hs.NewHandle( |
| 449 | compiler_driver_->ComputeInstanceFieldInfo(field_index, dex_compilation_unit_, is_put, soa))); |
| 450 | |
| 451 | if (resolved_field.Get() == nullptr) { |
| 452 | return false; |
| 453 | } |
| 454 | if (resolved_field->IsVolatile()) { |
| 455 | return false; |
| 456 | } |
| 457 | |
Nicolas Geoffray | abed4d0 | 2014-07-14 15:24:11 +0100 | [diff] [blame] | 458 | Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType(); |
| 459 | if (!IsTypeSupported(field_type)) { |
| 460 | return false; |
| 461 | } |
| 462 | |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 463 | HInstruction* object = LoadLocal(obj_reg, Primitive::kPrimNot); |
| 464 | current_block_->AddInstruction(new (arena_) HNullCheck(object, dex_offset)); |
| 465 | if (is_put) { |
| 466 | Temporaries temps(graph_, 1); |
| 467 | HInstruction* null_check = current_block_->GetLastInstruction(); |
| 468 | // We need one temporary for the null check. |
| 469 | temps.Add(null_check); |
Nicolas Geoffray | abed4d0 | 2014-07-14 15:24:11 +0100 | [diff] [blame] | 470 | HInstruction* value = LoadLocal(source_or_dest_reg, field_type); |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 471 | current_block_->AddInstruction(new (arena_) HInstanceFieldSet( |
| 472 | null_check, |
| 473 | value, |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 474 | field_type, |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 475 | resolved_field->GetOffset())); |
| 476 | } else { |
| 477 | current_block_->AddInstruction(new (arena_) HInstanceFieldGet( |
| 478 | current_block_->GetLastInstruction(), |
Nicolas Geoffray | abed4d0 | 2014-07-14 15:24:11 +0100 | [diff] [blame] | 479 | field_type, |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 480 | resolved_field->GetOffset())); |
| 481 | |
| 482 | UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction()); |
| 483 | } |
| 484 | return true; |
| 485 | } |
| 486 | |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 487 | |
| 488 | |
| 489 | bool HGraphBuilder::BuildStaticFieldAccess(const Instruction& instruction, |
| 490 | uint32_t dex_offset, |
| 491 | bool is_put) { |
| 492 | uint32_t source_or_dest_reg = instruction.VRegA_21c(); |
| 493 | uint16_t field_index = instruction.VRegB_21c(); |
| 494 | |
| 495 | uint32_t storage_index; |
| 496 | bool is_referrers_class; |
| 497 | bool is_initialized; |
| 498 | bool is_volatile; |
| 499 | MemberOffset field_offset(0u); |
| 500 | Primitive::Type field_type; |
| 501 | |
| 502 | bool fast_path = compiler_driver_->ComputeStaticFieldInfo(field_index, |
| 503 | dex_compilation_unit_, |
| 504 | is_put, |
| 505 | &field_offset, |
| 506 | &storage_index, |
| 507 | &is_referrers_class, |
| 508 | &is_volatile, |
| 509 | &is_initialized, |
| 510 | &field_type); |
| 511 | if (!fast_path) { |
| 512 | return false; |
| 513 | } |
| 514 | |
| 515 | if (is_volatile) { |
| 516 | return false; |
| 517 | } |
| 518 | |
| 519 | if (!IsTypeSupported(field_type)) { |
| 520 | return false; |
| 521 | } |
| 522 | |
| 523 | HLoadClass* constant = new (arena_) HLoadClass( |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 524 | storage_index, is_referrers_class, dex_offset); |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 525 | current_block_->AddInstruction(constant); |
| 526 | |
| 527 | HInstruction* cls = constant; |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 528 | if (!is_initialized) { |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 529 | cls = new (arena_) HClinitCheck(constant, dex_offset); |
| 530 | current_block_->AddInstruction(cls); |
| 531 | } |
| 532 | |
| 533 | if (is_put) { |
| 534 | // We need to keep the class alive before loading the value. |
| 535 | Temporaries temps(graph_, 1); |
| 536 | temps.Add(cls); |
| 537 | HInstruction* value = LoadLocal(source_or_dest_reg, field_type); |
| 538 | DCHECK_EQ(value->GetType(), field_type); |
| 539 | current_block_->AddInstruction( |
| 540 | new (arena_) HStaticFieldSet(cls, value, field_type, field_offset)); |
| 541 | } else { |
| 542 | current_block_->AddInstruction(new (arena_) HStaticFieldGet(cls, field_type, field_offset)); |
| 543 | UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction()); |
| 544 | } |
| 545 | return true; |
| 546 | } |
| 547 | |
Calin Juravle | 865fc88 | 2014-11-06 17:09:03 +0000 | [diff] [blame] | 548 | void HGraphBuilder::BuildCheckedDiv(uint16_t out_reg, |
| 549 | uint16_t first_reg, |
| 550 | uint16_t second_reg, |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 551 | uint32_t dex_offset, |
| 552 | Primitive::Type type, |
| 553 | bool second_is_lit) { |
| 554 | DCHECK(type == Primitive::kPrimInt); |
| 555 | |
Calin Juravle | 865fc88 | 2014-11-06 17:09:03 +0000 | [diff] [blame] | 556 | HInstruction* first = LoadLocal(first_reg, type); |
| 557 | HInstruction* second = second_is_lit ? GetIntConstant(second_reg) : LoadLocal(second_reg, type); |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 558 | if (!second->IsIntConstant() || (second->AsIntConstant()->GetValue() == 0)) { |
| 559 | second = new (arena_) HDivZeroCheck(second, dex_offset); |
| 560 | Temporaries temps(graph_, 1); |
| 561 | current_block_->AddInstruction(second); |
| 562 | temps.Add(current_block_->GetLastInstruction()); |
| 563 | } |
| 564 | |
| 565 | current_block_->AddInstruction(new (arena_) HDiv(type, first, second)); |
Calin Juravle | 865fc88 | 2014-11-06 17:09:03 +0000 | [diff] [blame] | 566 | UpdateLocal(out_reg, current_block_->GetLastInstruction()); |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 567 | } |
| 568 | |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 569 | void HGraphBuilder::BuildArrayAccess(const Instruction& instruction, |
| 570 | uint32_t dex_offset, |
| 571 | bool is_put, |
| 572 | Primitive::Type anticipated_type) { |
| 573 | uint8_t source_or_dest_reg = instruction.VRegA_23x(); |
| 574 | uint8_t array_reg = instruction.VRegB_23x(); |
| 575 | uint8_t index_reg = instruction.VRegC_23x(); |
| 576 | |
| 577 | DCHECK(IsTypeSupported(anticipated_type)); |
| 578 | |
| 579 | // We need one temporary for the null check, one for the index, and one for the length. |
| 580 | Temporaries temps(graph_, 3); |
| 581 | |
| 582 | HInstruction* object = LoadLocal(array_reg, Primitive::kPrimNot); |
| 583 | object = new (arena_) HNullCheck(object, dex_offset); |
| 584 | current_block_->AddInstruction(object); |
| 585 | temps.Add(object); |
| 586 | |
| 587 | HInstruction* length = new (arena_) HArrayLength(object); |
| 588 | current_block_->AddInstruction(length); |
| 589 | temps.Add(length); |
| 590 | HInstruction* index = LoadLocal(index_reg, Primitive::kPrimInt); |
| 591 | index = new (arena_) HBoundsCheck(index, length, dex_offset); |
| 592 | current_block_->AddInstruction(index); |
| 593 | temps.Add(index); |
| 594 | if (is_put) { |
| 595 | HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type); |
| 596 | // TODO: Insert a type check node if the type is Object. |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 597 | current_block_->AddInstruction(new (arena_) HArraySet( |
| 598 | object, index, value, anticipated_type, dex_offset)); |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 599 | } else { |
| 600 | current_block_->AddInstruction(new (arena_) HArrayGet(object, index, anticipated_type)); |
| 601 | UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction()); |
| 602 | } |
| 603 | } |
| 604 | |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 605 | void HGraphBuilder::BuildFilledNewArray(uint32_t dex_offset, |
| 606 | uint32_t type_index, |
| 607 | uint32_t number_of_vreg_arguments, |
| 608 | bool is_range, |
| 609 | uint32_t* args, |
| 610 | uint32_t register_index) { |
| 611 | HInstruction* length = GetIntConstant(number_of_vreg_arguments); |
| 612 | HInstruction* object = new (arena_) HNewArray(length, dex_offset, type_index); |
| 613 | current_block_->AddInstruction(object); |
| 614 | |
| 615 | const char* descriptor = dex_file_->StringByTypeIdx(type_index); |
| 616 | DCHECK_EQ(descriptor[0], '[') << descriptor; |
| 617 | char primitive = descriptor[1]; |
| 618 | DCHECK(primitive == 'I' |
| 619 | || primitive == 'L' |
| 620 | || primitive == '[') << descriptor; |
| 621 | bool is_reference_array = (primitive == 'L') || (primitive == '['); |
| 622 | Primitive::Type type = is_reference_array ? Primitive::kPrimNot : Primitive::kPrimInt; |
| 623 | |
| 624 | Temporaries temps(graph_, 1); |
| 625 | temps.Add(object); |
| 626 | for (size_t i = 0; i < number_of_vreg_arguments; ++i) { |
| 627 | HInstruction* value = LoadLocal(is_range ? register_index + i : args[i], type); |
| 628 | HInstruction* index = GetIntConstant(i); |
| 629 | current_block_->AddInstruction( |
| 630 | new (arena_) HArraySet(object, index, value, type, dex_offset)); |
| 631 | } |
| 632 | latest_result_ = object; |
| 633 | } |
| 634 | |
| 635 | template <typename T> |
| 636 | void HGraphBuilder::BuildFillArrayData(HInstruction* object, |
| 637 | const T* data, |
| 638 | uint32_t element_count, |
| 639 | Primitive::Type anticipated_type, |
| 640 | uint32_t dex_offset) { |
| 641 | for (uint32_t i = 0; i < element_count; ++i) { |
| 642 | HInstruction* index = GetIntConstant(i); |
| 643 | HInstruction* value = GetIntConstant(data[i]); |
| 644 | current_block_->AddInstruction(new (arena_) HArraySet( |
| 645 | object, index, value, anticipated_type, dex_offset)); |
| 646 | } |
| 647 | } |
| 648 | |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 649 | void HGraphBuilder::BuildFillArrayData(const Instruction& instruction, uint32_t dex_offset) { |
| 650 | Temporaries temps(graph_, 1); |
| 651 | HInstruction* array = LoadLocal(instruction.VRegA_31t(), Primitive::kPrimNot); |
| 652 | HNullCheck* null_check = new (arena_) HNullCheck(array, dex_offset); |
| 653 | current_block_->AddInstruction(null_check); |
| 654 | temps.Add(null_check); |
| 655 | |
| 656 | HInstruction* length = new (arena_) HArrayLength(null_check); |
| 657 | current_block_->AddInstruction(length); |
| 658 | |
| 659 | int32_t payload_offset = instruction.VRegB_31t() + dex_offset; |
| 660 | const Instruction::ArrayDataPayload* payload = |
| 661 | reinterpret_cast<const Instruction::ArrayDataPayload*>(code_start_ + payload_offset); |
| 662 | const uint8_t* data = payload->data; |
| 663 | uint32_t element_count = payload->element_count; |
| 664 | |
| 665 | // Implementation of this DEX instruction seems to be that the bounds check is |
| 666 | // done before doing any stores. |
| 667 | HInstruction* last_index = GetIntConstant(payload->element_count - 1); |
| 668 | current_block_->AddInstruction(new (arena_) HBoundsCheck(last_index, length, dex_offset)); |
| 669 | |
| 670 | switch (payload->element_width) { |
| 671 | case 1: |
| 672 | BuildFillArrayData(null_check, |
| 673 | reinterpret_cast<const int8_t*>(data), |
| 674 | element_count, |
| 675 | Primitive::kPrimByte, |
| 676 | dex_offset); |
| 677 | break; |
| 678 | case 2: |
| 679 | BuildFillArrayData(null_check, |
| 680 | reinterpret_cast<const int16_t*>(data), |
| 681 | element_count, |
| 682 | Primitive::kPrimShort, |
| 683 | dex_offset); |
| 684 | break; |
| 685 | case 4: |
| 686 | BuildFillArrayData(null_check, |
| 687 | reinterpret_cast<const int32_t*>(data), |
| 688 | element_count, |
| 689 | Primitive::kPrimInt, |
| 690 | dex_offset); |
| 691 | break; |
| 692 | case 8: |
| 693 | BuildFillWideArrayData(null_check, |
| 694 | reinterpret_cast<const int64_t*>(data), |
| 695 | element_count, |
| 696 | dex_offset); |
| 697 | break; |
| 698 | default: |
| 699 | LOG(FATAL) << "Unknown element width for " << payload->element_width; |
| 700 | } |
| 701 | } |
| 702 | |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 703 | void HGraphBuilder::BuildFillWideArrayData(HInstruction* object, |
Nicolas Geoffray | 8d6ae52 | 2014-10-23 18:32:13 +0100 | [diff] [blame] | 704 | const int64_t* data, |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 705 | uint32_t element_count, |
| 706 | uint32_t dex_offset) { |
| 707 | for (uint32_t i = 0; i < element_count; ++i) { |
| 708 | HInstruction* index = GetIntConstant(i); |
| 709 | HInstruction* value = GetLongConstant(data[i]); |
| 710 | current_block_->AddInstruction(new (arena_) HArraySet( |
| 711 | object, index, value, Primitive::kPrimLong, dex_offset)); |
| 712 | } |
| 713 | } |
| 714 | |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 715 | void HGraphBuilder::PotentiallyAddSuspendCheck(int32_t target_offset, uint32_t dex_offset) { |
| 716 | if (target_offset <= 0) { |
| 717 | // Unconditionnally add a suspend check to backward branches. We can remove |
| 718 | // them after we recognize loops in the graph. |
| 719 | current_block_->AddInstruction(new (arena_) HSuspendCheck(dex_offset)); |
| 720 | } |
| 721 | } |
| 722 | |
| 723 | bool HGraphBuilder::AnalyzeDexInstruction(const Instruction& instruction, uint32_t dex_offset) { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 724 | if (current_block_ == nullptr) { |
| 725 | return true; // Dead code |
| 726 | } |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 727 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 728 | switch (instruction.Opcode()) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 729 | case Instruction::CONST_4: { |
| 730 | int32_t register_index = instruction.VRegA(); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 731 | HIntConstant* constant = GetIntConstant(instruction.VRegB_11n()); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 732 | UpdateLocal(register_index, constant); |
| 733 | break; |
| 734 | } |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 735 | |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 736 | case Instruction::CONST_16: { |
| 737 | int32_t register_index = instruction.VRegA(); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 738 | HIntConstant* constant = GetIntConstant(instruction.VRegB_21s()); |
| 739 | UpdateLocal(register_index, constant); |
| 740 | break; |
| 741 | } |
| 742 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 743 | case Instruction::CONST: { |
| 744 | int32_t register_index = instruction.VRegA(); |
| 745 | HIntConstant* constant = GetIntConstant(instruction.VRegB_31i()); |
| 746 | UpdateLocal(register_index, constant); |
| 747 | break; |
| 748 | } |
| 749 | |
| 750 | case Instruction::CONST_HIGH16: { |
| 751 | int32_t register_index = instruction.VRegA(); |
| 752 | HIntConstant* constant = GetIntConstant(instruction.VRegB_21h() << 16); |
| 753 | UpdateLocal(register_index, constant); |
| 754 | break; |
| 755 | } |
| 756 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 757 | case Instruction::CONST_WIDE_16: { |
| 758 | int32_t register_index = instruction.VRegA(); |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 759 | // Get 16 bits of constant value, sign extended to 64 bits. |
| 760 | int64_t value = instruction.VRegB_21s(); |
| 761 | value <<= 48; |
| 762 | value >>= 48; |
| 763 | HLongConstant* constant = GetLongConstant(value); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 764 | UpdateLocal(register_index, constant); |
| 765 | break; |
| 766 | } |
| 767 | |
| 768 | case Instruction::CONST_WIDE_32: { |
| 769 | int32_t register_index = instruction.VRegA(); |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 770 | // Get 32 bits of constant value, sign extended to 64 bits. |
| 771 | int64_t value = instruction.VRegB_31i(); |
| 772 | value <<= 32; |
| 773 | value >>= 32; |
| 774 | HLongConstant* constant = GetLongConstant(value); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 775 | UpdateLocal(register_index, constant); |
| 776 | break; |
| 777 | } |
| 778 | |
| 779 | case Instruction::CONST_WIDE: { |
| 780 | int32_t register_index = instruction.VRegA(); |
| 781 | HLongConstant* constant = GetLongConstant(instruction.VRegB_51l()); |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 782 | UpdateLocal(register_index, constant); |
| 783 | break; |
| 784 | } |
| 785 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 786 | case Instruction::CONST_WIDE_HIGH16: { |
| 787 | int32_t register_index = instruction.VRegA(); |
| 788 | int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48; |
| 789 | HLongConstant* constant = GetLongConstant(value); |
| 790 | UpdateLocal(register_index, constant); |
| 791 | break; |
| 792 | } |
| 793 | |
| 794 | // TODO: these instructions are also used to move floating point values, so what is |
| 795 | // the type (int or float)? |
| 796 | case Instruction::MOVE: |
| 797 | case Instruction::MOVE_FROM16: |
| 798 | case Instruction::MOVE_16: { |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 799 | HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimInt); |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 800 | UpdateLocal(instruction.VRegA(), value); |
| 801 | break; |
| 802 | } |
| 803 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 804 | // TODO: these instructions are also used to move floating point values, so what is |
| 805 | // the type (long or double)? |
| 806 | case Instruction::MOVE_WIDE: |
| 807 | case Instruction::MOVE_WIDE_FROM16: |
| 808 | case Instruction::MOVE_WIDE_16: { |
| 809 | HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimLong); |
| 810 | UpdateLocal(instruction.VRegA(), value); |
| 811 | break; |
| 812 | } |
| 813 | |
| 814 | case Instruction::MOVE_OBJECT: |
| 815 | case Instruction::MOVE_OBJECT_16: |
| 816 | case Instruction::MOVE_OBJECT_FROM16: { |
| 817 | HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimNot); |
| 818 | UpdateLocal(instruction.VRegA(), value); |
| 819 | break; |
| 820 | } |
| 821 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 822 | case Instruction::RETURN_VOID: { |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 823 | BuildReturn(instruction, Primitive::kPrimVoid); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 824 | break; |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 825 | } |
| 826 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 827 | #define IF_XX(comparison, cond) \ |
| 828 | case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_offset); break; \ |
| 829 | case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_offset); break |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 830 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 831 | IF_XX(HEqual, EQ); |
| 832 | IF_XX(HNotEqual, NE); |
| 833 | IF_XX(HLessThan, LT); |
| 834 | IF_XX(HLessThanOrEqual, LE); |
| 835 | IF_XX(HGreaterThan, GT); |
| 836 | IF_XX(HGreaterThanOrEqual, GE); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 837 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 838 | case Instruction::GOTO: |
| 839 | case Instruction::GOTO_16: |
| 840 | case Instruction::GOTO_32: { |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 841 | int32_t offset = instruction.GetTargetOffset(); |
| 842 | PotentiallyAddSuspendCheck(offset, dex_offset); |
| 843 | HBasicBlock* target = FindBlockStartingAt(offset + dex_offset); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 844 | DCHECK(target != nullptr); |
| 845 | current_block_->AddInstruction(new (arena_) HGoto()); |
| 846 | current_block_->AddSuccessor(target); |
| 847 | current_block_ = nullptr; |
| 848 | break; |
| 849 | } |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 850 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 851 | case Instruction::RETURN: { |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 852 | DCHECK_NE(return_type_, Primitive::kPrimNot); |
| 853 | DCHECK_NE(return_type_, Primitive::kPrimLong); |
| 854 | DCHECK_NE(return_type_, Primitive::kPrimDouble); |
| 855 | BuildReturn(instruction, return_type_); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 856 | break; |
| 857 | } |
| 858 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 859 | case Instruction::RETURN_OBJECT: { |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 860 | DCHECK(return_type_ == Primitive::kPrimNot); |
| 861 | BuildReturn(instruction, return_type_); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 862 | break; |
| 863 | } |
| 864 | |
| 865 | case Instruction::RETURN_WIDE: { |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 866 | DCHECK(return_type_ == Primitive::kPrimDouble || return_type_ == Primitive::kPrimLong); |
| 867 | BuildReturn(instruction, return_type_); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 868 | break; |
| 869 | } |
| 870 | |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 871 | case Instruction::INVOKE_STATIC: |
Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 872 | case Instruction::INVOKE_DIRECT: |
| 873 | case Instruction::INVOKE_VIRTUAL: { |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 874 | uint32_t method_idx = instruction.VRegB_35c(); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 875 | uint32_t number_of_vreg_arguments = instruction.VRegA_35c(); |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 876 | uint32_t args[5]; |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 877 | instruction.GetVarArgs(args); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 878 | if (!BuildInvoke(instruction, dex_offset, method_idx, number_of_vreg_arguments, false, args, -1)) { |
| 879 | return false; |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 880 | } |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 881 | break; |
| 882 | } |
| 883 | |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 884 | case Instruction::INVOKE_STATIC_RANGE: |
Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 885 | case Instruction::INVOKE_DIRECT_RANGE: |
| 886 | case Instruction::INVOKE_VIRTUAL_RANGE: { |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 887 | uint32_t method_idx = instruction.VRegB_3rc(); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 888 | uint32_t number_of_vreg_arguments = instruction.VRegA_3rc(); |
| 889 | uint32_t register_index = instruction.VRegC(); |
| 890 | if (!BuildInvoke(instruction, dex_offset, method_idx, |
| 891 | number_of_vreg_arguments, true, nullptr, register_index)) { |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 892 | return false; |
| 893 | } |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 894 | break; |
| 895 | } |
| 896 | |
Roland Levillain | 88cb175 | 2014-10-20 16:36:47 +0100 | [diff] [blame] | 897 | case Instruction::NEG_INT: { |
| 898 | Unop_12x<HNeg>(instruction, Primitive::kPrimInt); |
| 899 | break; |
| 900 | } |
| 901 | |
Roland Levillain | 2e07b4f | 2014-10-23 18:12:09 +0100 | [diff] [blame] | 902 | case Instruction::NEG_LONG: { |
| 903 | Unop_12x<HNeg>(instruction, Primitive::kPrimLong); |
| 904 | break; |
| 905 | } |
| 906 | |
Roland Levillain | 3dbcb38 | 2014-10-28 17:30:07 +0000 | [diff] [blame] | 907 | case Instruction::NEG_FLOAT: { |
| 908 | Unop_12x<HNeg>(instruction, Primitive::kPrimFloat); |
| 909 | break; |
| 910 | } |
| 911 | |
| 912 | case Instruction::NEG_DOUBLE: { |
| 913 | Unop_12x<HNeg>(instruction, Primitive::kPrimDouble); |
| 914 | break; |
| 915 | } |
| 916 | |
Roland Levillain | 1cc5f251 | 2014-10-22 18:06:21 +0100 | [diff] [blame] | 917 | case Instruction::NOT_INT: { |
| 918 | Unop_12x<HNot>(instruction, Primitive::kPrimInt); |
| 919 | break; |
| 920 | } |
| 921 | |
Roland Levillain | 7056643 | 2014-10-24 16:20:17 +0100 | [diff] [blame] | 922 | case Instruction::NOT_LONG: { |
| 923 | Unop_12x<HNot>(instruction, Primitive::kPrimLong); |
| 924 | break; |
| 925 | } |
| 926 | |
Roland Levillain | dff1f28 | 2014-11-05 14:15:05 +0000 | [diff] [blame] | 927 | case Instruction::INT_TO_LONG: { |
| 928 | Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimLong); |
| 929 | break; |
| 930 | } |
| 931 | |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 932 | case Instruction::ADD_INT: { |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 933 | Binop_23x<HAdd>(instruction, Primitive::kPrimInt); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 934 | break; |
| 935 | } |
| 936 | |
| 937 | case Instruction::ADD_LONG: { |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 938 | Binop_23x<HAdd>(instruction, Primitive::kPrimLong); |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 939 | break; |
| 940 | } |
| 941 | |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 942 | case Instruction::ADD_DOUBLE: { |
| 943 | Binop_23x<HAdd>(instruction, Primitive::kPrimDouble); |
| 944 | break; |
| 945 | } |
| 946 | |
| 947 | case Instruction::ADD_FLOAT: { |
| 948 | Binop_23x<HAdd>(instruction, Primitive::kPrimFloat); |
| 949 | break; |
| 950 | } |
| 951 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 952 | case Instruction::SUB_INT: { |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 953 | Binop_23x<HSub>(instruction, Primitive::kPrimInt); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 954 | break; |
| 955 | } |
| 956 | |
| 957 | case Instruction::SUB_LONG: { |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 958 | Binop_23x<HSub>(instruction, Primitive::kPrimLong); |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 959 | break; |
| 960 | } |
| 961 | |
Calin Juravle | 096cc02 | 2014-10-23 17:01:13 +0100 | [diff] [blame] | 962 | case Instruction::SUB_FLOAT: { |
| 963 | Binop_23x<HSub>(instruction, Primitive::kPrimFloat); |
| 964 | break; |
| 965 | } |
| 966 | |
| 967 | case Instruction::SUB_DOUBLE: { |
| 968 | Binop_23x<HSub>(instruction, Primitive::kPrimDouble); |
| 969 | break; |
| 970 | } |
| 971 | |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 972 | case Instruction::ADD_INT_2ADDR: { |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 973 | Binop_12x<HAdd>(instruction, Primitive::kPrimInt); |
| 974 | break; |
| 975 | } |
| 976 | |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 977 | case Instruction::MUL_INT: { |
| 978 | Binop_23x<HMul>(instruction, Primitive::kPrimInt); |
| 979 | break; |
| 980 | } |
| 981 | |
| 982 | case Instruction::MUL_LONG: { |
| 983 | Binop_23x<HMul>(instruction, Primitive::kPrimLong); |
| 984 | break; |
| 985 | } |
| 986 | |
Calin Juravle | b5bfa96 | 2014-10-21 18:02:24 +0100 | [diff] [blame] | 987 | case Instruction::MUL_FLOAT: { |
| 988 | Binop_23x<HMul>(instruction, Primitive::kPrimFloat); |
| 989 | break; |
| 990 | } |
| 991 | |
| 992 | case Instruction::MUL_DOUBLE: { |
| 993 | Binop_23x<HMul>(instruction, Primitive::kPrimDouble); |
| 994 | break; |
| 995 | } |
| 996 | |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 997 | case Instruction::DIV_INT: { |
Calin Juravle | 865fc88 | 2014-11-06 17:09:03 +0000 | [diff] [blame] | 998 | BuildCheckedDiv(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(), |
| 999 | dex_offset, Primitive::kPrimInt, false); |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 1000 | break; |
| 1001 | } |
| 1002 | |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 1003 | case Instruction::DIV_FLOAT: { |
| 1004 | Binop_23x<HDiv>(instruction, Primitive::kPrimFloat); |
| 1005 | break; |
| 1006 | } |
| 1007 | |
| 1008 | case Instruction::DIV_DOUBLE: { |
| 1009 | Binop_23x<HDiv>(instruction, Primitive::kPrimDouble); |
| 1010 | break; |
| 1011 | } |
| 1012 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 1013 | case Instruction::ADD_LONG_2ADDR: { |
| 1014 | Binop_12x<HAdd>(instruction, Primitive::kPrimLong); |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 1015 | break; |
| 1016 | } |
| 1017 | |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 1018 | case Instruction::ADD_DOUBLE_2ADDR: { |
| 1019 | Binop_12x<HAdd>(instruction, Primitive::kPrimDouble); |
| 1020 | break; |
| 1021 | } |
| 1022 | |
| 1023 | case Instruction::ADD_FLOAT_2ADDR: { |
| 1024 | Binop_12x<HAdd>(instruction, Primitive::kPrimFloat); |
| 1025 | break; |
| 1026 | } |
| 1027 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 1028 | case Instruction::SUB_INT_2ADDR: { |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 1029 | Binop_12x<HSub>(instruction, Primitive::kPrimInt); |
| 1030 | break; |
| 1031 | } |
| 1032 | |
| 1033 | case Instruction::SUB_LONG_2ADDR: { |
| 1034 | Binop_12x<HSub>(instruction, Primitive::kPrimLong); |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 1035 | break; |
| 1036 | } |
| 1037 | |
Calin Juravle | 096cc02 | 2014-10-23 17:01:13 +0100 | [diff] [blame] | 1038 | case Instruction::SUB_FLOAT_2ADDR: { |
| 1039 | Binop_12x<HSub>(instruction, Primitive::kPrimFloat); |
| 1040 | break; |
| 1041 | } |
| 1042 | |
| 1043 | case Instruction::SUB_DOUBLE_2ADDR: { |
| 1044 | Binop_12x<HSub>(instruction, Primitive::kPrimDouble); |
| 1045 | break; |
| 1046 | } |
| 1047 | |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 1048 | case Instruction::MUL_INT_2ADDR: { |
| 1049 | Binop_12x<HMul>(instruction, Primitive::kPrimInt); |
| 1050 | break; |
| 1051 | } |
| 1052 | |
| 1053 | case Instruction::MUL_LONG_2ADDR: { |
| 1054 | Binop_12x<HMul>(instruction, Primitive::kPrimLong); |
| 1055 | break; |
| 1056 | } |
| 1057 | |
Calin Juravle | b5bfa96 | 2014-10-21 18:02:24 +0100 | [diff] [blame] | 1058 | case Instruction::MUL_FLOAT_2ADDR: { |
| 1059 | Binop_12x<HMul>(instruction, Primitive::kPrimFloat); |
| 1060 | break; |
| 1061 | } |
| 1062 | |
| 1063 | case Instruction::MUL_DOUBLE_2ADDR: { |
| 1064 | Binop_12x<HMul>(instruction, Primitive::kPrimDouble); |
| 1065 | break; |
| 1066 | } |
| 1067 | |
Calin Juravle | 865fc88 | 2014-11-06 17:09:03 +0000 | [diff] [blame] | 1068 | case Instruction::DIV_INT_2ADDR: { |
| 1069 | BuildCheckedDiv(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(), |
| 1070 | dex_offset, Primitive::kPrimInt, false); |
| 1071 | break; |
| 1072 | } |
| 1073 | |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 1074 | case Instruction::DIV_FLOAT_2ADDR: { |
| 1075 | Binop_12x<HDiv>(instruction, Primitive::kPrimFloat); |
| 1076 | break; |
| 1077 | } |
| 1078 | |
| 1079 | case Instruction::DIV_DOUBLE_2ADDR: { |
| 1080 | Binop_12x<HDiv>(instruction, Primitive::kPrimDouble); |
| 1081 | break; |
| 1082 | } |
| 1083 | |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 1084 | case Instruction::ADD_INT_LIT16: { |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 1085 | Binop_22s<HAdd>(instruction, false); |
| 1086 | break; |
| 1087 | } |
| 1088 | |
| 1089 | case Instruction::RSUB_INT: { |
| 1090 | Binop_22s<HSub>(instruction, true); |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 1091 | break; |
| 1092 | } |
| 1093 | |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 1094 | case Instruction::MUL_INT_LIT16: { |
| 1095 | Binop_22s<HMul>(instruction, false); |
| 1096 | break; |
| 1097 | } |
| 1098 | |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 1099 | case Instruction::ADD_INT_LIT8: { |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 1100 | Binop_22b<HAdd>(instruction, false); |
| 1101 | break; |
| 1102 | } |
| 1103 | |
| 1104 | case Instruction::RSUB_INT_LIT8: { |
| 1105 | Binop_22b<HSub>(instruction, true); |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 1106 | break; |
| 1107 | } |
| 1108 | |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 1109 | case Instruction::MUL_INT_LIT8: { |
| 1110 | Binop_22b<HMul>(instruction, false); |
| 1111 | break; |
| 1112 | } |
| 1113 | |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 1114 | case Instruction::DIV_INT_LIT16: |
| 1115 | case Instruction::DIV_INT_LIT8: { |
Calin Juravle | 865fc88 | 2014-11-06 17:09:03 +0000 | [diff] [blame] | 1116 | BuildCheckedDiv(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(), |
| 1117 | dex_offset, Primitive::kPrimInt, true); |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 1118 | break; |
| 1119 | } |
| 1120 | |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 1121 | case Instruction::NEW_INSTANCE: { |
| 1122 | current_block_->AddInstruction( |
| 1123 | new (arena_) HNewInstance(dex_offset, instruction.VRegB_21c())); |
| 1124 | UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction()); |
| 1125 | break; |
| 1126 | } |
| 1127 | |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 1128 | case Instruction::NEW_ARRAY: { |
| 1129 | HInstruction* length = LoadLocal(instruction.VRegB_22c(), Primitive::kPrimInt); |
| 1130 | current_block_->AddInstruction( |
| 1131 | new (arena_) HNewArray(length, dex_offset, instruction.VRegC_22c())); |
| 1132 | UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction()); |
| 1133 | break; |
| 1134 | } |
| 1135 | |
| 1136 | case Instruction::FILLED_NEW_ARRAY: { |
| 1137 | uint32_t number_of_vreg_arguments = instruction.VRegA_35c(); |
| 1138 | uint32_t type_index = instruction.VRegB_35c(); |
| 1139 | uint32_t args[5]; |
| 1140 | instruction.GetVarArgs(args); |
| 1141 | BuildFilledNewArray(dex_offset, type_index, number_of_vreg_arguments, false, args, 0); |
| 1142 | break; |
| 1143 | } |
| 1144 | |
| 1145 | case Instruction::FILLED_NEW_ARRAY_RANGE: { |
| 1146 | uint32_t number_of_vreg_arguments = instruction.VRegA_3rc(); |
| 1147 | uint32_t type_index = instruction.VRegB_3rc(); |
| 1148 | uint32_t register_index = instruction.VRegC_3rc(); |
| 1149 | BuildFilledNewArray( |
| 1150 | dex_offset, type_index, number_of_vreg_arguments, true, nullptr, register_index); |
| 1151 | break; |
| 1152 | } |
| 1153 | |
| 1154 | case Instruction::FILL_ARRAY_DATA: { |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 1155 | BuildFillArrayData(instruction, dex_offset); |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 1156 | break; |
| 1157 | } |
| 1158 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 1159 | case Instruction::MOVE_RESULT: |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1160 | case Instruction::MOVE_RESULT_WIDE: |
| 1161 | case Instruction::MOVE_RESULT_OBJECT: |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 1162 | UpdateLocal(instruction.VRegA(), latest_result_); |
| 1163 | latest_result_ = nullptr; |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 1164 | break; |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 1165 | |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 1166 | case Instruction::CMP_LONG: { |
| 1167 | Binop_23x<HCompare>(instruction, Primitive::kPrimLong); |
| 1168 | break; |
| 1169 | } |
| 1170 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 1171 | case Instruction::NOP: |
| 1172 | break; |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1173 | |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 1174 | case Instruction::IGET: |
| 1175 | case Instruction::IGET_WIDE: |
| 1176 | case Instruction::IGET_OBJECT: |
| 1177 | case Instruction::IGET_BOOLEAN: |
| 1178 | case Instruction::IGET_BYTE: |
| 1179 | case Instruction::IGET_CHAR: |
| 1180 | case Instruction::IGET_SHORT: { |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 1181 | if (!BuildInstanceFieldAccess(instruction, dex_offset, false)) { |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 1182 | return false; |
| 1183 | } |
| 1184 | break; |
| 1185 | } |
| 1186 | |
| 1187 | case Instruction::IPUT: |
| 1188 | case Instruction::IPUT_WIDE: |
| 1189 | case Instruction::IPUT_OBJECT: |
| 1190 | case Instruction::IPUT_BOOLEAN: |
| 1191 | case Instruction::IPUT_BYTE: |
| 1192 | case Instruction::IPUT_CHAR: |
| 1193 | case Instruction::IPUT_SHORT: { |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 1194 | if (!BuildInstanceFieldAccess(instruction, dex_offset, true)) { |
| 1195 | return false; |
| 1196 | } |
| 1197 | break; |
| 1198 | } |
| 1199 | |
| 1200 | case Instruction::SGET: |
| 1201 | case Instruction::SGET_WIDE: |
| 1202 | case Instruction::SGET_OBJECT: |
| 1203 | case Instruction::SGET_BOOLEAN: |
| 1204 | case Instruction::SGET_BYTE: |
| 1205 | case Instruction::SGET_CHAR: |
| 1206 | case Instruction::SGET_SHORT: { |
| 1207 | if (!BuildStaticFieldAccess(instruction, dex_offset, false)) { |
| 1208 | return false; |
| 1209 | } |
| 1210 | break; |
| 1211 | } |
| 1212 | |
| 1213 | case Instruction::SPUT: |
| 1214 | case Instruction::SPUT_WIDE: |
| 1215 | case Instruction::SPUT_OBJECT: |
| 1216 | case Instruction::SPUT_BOOLEAN: |
| 1217 | case Instruction::SPUT_BYTE: |
| 1218 | case Instruction::SPUT_CHAR: |
| 1219 | case Instruction::SPUT_SHORT: { |
| 1220 | if (!BuildStaticFieldAccess(instruction, dex_offset, true)) { |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 1221 | return false; |
| 1222 | } |
| 1223 | break; |
| 1224 | } |
| 1225 | |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 1226 | #define ARRAY_XX(kind, anticipated_type) \ |
| 1227 | case Instruction::AGET##kind: { \ |
| 1228 | BuildArrayAccess(instruction, dex_offset, false, anticipated_type); \ |
| 1229 | break; \ |
| 1230 | } \ |
| 1231 | case Instruction::APUT##kind: { \ |
| 1232 | BuildArrayAccess(instruction, dex_offset, true, anticipated_type); \ |
| 1233 | break; \ |
| 1234 | } |
| 1235 | |
| 1236 | ARRAY_XX(, Primitive::kPrimInt); |
| 1237 | ARRAY_XX(_WIDE, Primitive::kPrimLong); |
| 1238 | ARRAY_XX(_OBJECT, Primitive::kPrimNot); |
| 1239 | ARRAY_XX(_BOOLEAN, Primitive::kPrimBoolean); |
| 1240 | ARRAY_XX(_BYTE, Primitive::kPrimByte); |
| 1241 | ARRAY_XX(_CHAR, Primitive::kPrimChar); |
| 1242 | ARRAY_XX(_SHORT, Primitive::kPrimShort); |
| 1243 | |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 1244 | case Instruction::ARRAY_LENGTH: { |
| 1245 | HInstruction* object = LoadLocal(instruction.VRegB_12x(), Primitive::kPrimNot); |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 1246 | // No need for a temporary for the null check, it is the only input of the following |
| 1247 | // instruction. |
| 1248 | object = new (arena_) HNullCheck(object, dex_offset); |
| 1249 | current_block_->AddInstruction(object); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 1250 | current_block_->AddInstruction(new (arena_) HArrayLength(object)); |
| 1251 | UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction()); |
| 1252 | break; |
| 1253 | } |
| 1254 | |
Nicolas Geoffray | b5f62b3 | 2014-10-30 10:58:41 +0000 | [diff] [blame] | 1255 | case Instruction::CONST_STRING: { |
| 1256 | current_block_->AddInstruction(new (arena_) HLoadString(instruction.VRegB_21c(), dex_offset)); |
| 1257 | UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction()); |
| 1258 | break; |
| 1259 | } |
| 1260 | |
| 1261 | case Instruction::CONST_STRING_JUMBO: { |
| 1262 | current_block_->AddInstruction(new (arena_) HLoadString(instruction.VRegB_31c(), dex_offset)); |
| 1263 | UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction()); |
| 1264 | break; |
| 1265 | } |
| 1266 | |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 1267 | case Instruction::CONST_CLASS: { |
| 1268 | uint16_t type_index = instruction.VRegB_21c(); |
| 1269 | bool type_known_final; |
| 1270 | bool type_known_abstract; |
| 1271 | bool is_referrers_class; |
| 1272 | bool can_access = compiler_driver_->CanAccessTypeWithoutChecks( |
| 1273 | dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index, |
| 1274 | &type_known_final, &type_known_abstract, &is_referrers_class); |
| 1275 | if (!can_access) { |
| 1276 | return false; |
| 1277 | } |
| 1278 | current_block_->AddInstruction( |
| 1279 | new (arena_) HLoadClass(instruction.VRegB_21c(), is_referrers_class, dex_offset)); |
| 1280 | UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction()); |
| 1281 | break; |
| 1282 | } |
| 1283 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 1284 | case Instruction::MOVE_EXCEPTION: { |
| 1285 | current_block_->AddInstruction(new (arena_) HLoadException()); |
| 1286 | UpdateLocal(instruction.VRegA_11x(), current_block_->GetLastInstruction()); |
| 1287 | break; |
| 1288 | } |
| 1289 | |
| 1290 | case Instruction::THROW: { |
| 1291 | HInstruction* exception = LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot); |
| 1292 | current_block_->AddInstruction(new (arena_) HThrow(exception, dex_offset)); |
| 1293 | // A throw instruction must branch to the exit block. |
| 1294 | current_block_->AddSuccessor(exit_block_); |
| 1295 | // We finished building this block. Set the current block to null to avoid |
| 1296 | // adding dead instructions to it. |
| 1297 | current_block_ = nullptr; |
| 1298 | break; |
| 1299 | } |
| 1300 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1301 | default: |
| 1302 | return false; |
| 1303 | } |
| 1304 | return true; |
| 1305 | } |
| 1306 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 1307 | HIntConstant* HGraphBuilder::GetIntConstant0() { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1308 | if (constant0_ != nullptr) { |
| 1309 | return constant0_; |
| 1310 | } |
| 1311 | constant0_ = new(arena_) HIntConstant(0); |
| 1312 | entry_block_->AddInstruction(constant0_); |
| 1313 | return constant0_; |
| 1314 | } |
| 1315 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 1316 | HIntConstant* HGraphBuilder::GetIntConstant1() { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1317 | if (constant1_ != nullptr) { |
| 1318 | return constant1_; |
| 1319 | } |
| 1320 | constant1_ = new(arena_) HIntConstant(1); |
| 1321 | entry_block_->AddInstruction(constant1_); |
| 1322 | return constant1_; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1323 | } |
| 1324 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 1325 | HIntConstant* HGraphBuilder::GetIntConstant(int32_t constant) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1326 | switch (constant) { |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 1327 | case 0: return GetIntConstant0(); |
| 1328 | case 1: return GetIntConstant1(); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1329 | default: { |
| 1330 | HIntConstant* instruction = new (arena_) HIntConstant(constant); |
| 1331 | entry_block_->AddInstruction(instruction); |
| 1332 | return instruction; |
| 1333 | } |
| 1334 | } |
| 1335 | } |
| 1336 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 1337 | HLongConstant* HGraphBuilder::GetLongConstant(int64_t constant) { |
| 1338 | HLongConstant* instruction = new (arena_) HLongConstant(constant); |
| 1339 | entry_block_->AddInstruction(instruction); |
| 1340 | return instruction; |
| 1341 | } |
| 1342 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1343 | HLocal* HGraphBuilder::GetLocalAt(int register_index) const { |
| 1344 | return locals_.Get(register_index); |
| 1345 | } |
| 1346 | |
| 1347 | void HGraphBuilder::UpdateLocal(int register_index, HInstruction* instruction) const { |
| 1348 | HLocal* local = GetLocalAt(register_index); |
| 1349 | current_block_->AddInstruction(new (arena_) HStoreLocal(local, instruction)); |
| 1350 | } |
| 1351 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 1352 | HInstruction* HGraphBuilder::LoadLocal(int register_index, Primitive::Type type) const { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1353 | HLocal* local = GetLocalAt(register_index); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 1354 | current_block_->AddInstruction(new (arena_) HLoadLocal(local, type)); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 1355 | return current_block_->GetLastInstruction(); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1356 | } |
| 1357 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1358 | } // namespace art |