blob: 1f0b3613e72ed4ed839457ce1027c4306fc17ae2 [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
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 Geoffraye5038322014-07-04 09:41:32 +010018#include "builder.h"
19
20#include "class_linker.h"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000021#include "dex_file.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000022#include "dex_file-inl.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000023#include "dex_instruction.h"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000024#include "dex_instruction-inl.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010025#include "driver/compiler_driver-inl.h"
26#include "mirror/art_field.h"
27#include "mirror/art_field-inl.h"
28#include "mirror/class_loader.h"
29#include "mirror/dex_cache.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000030#include "nodes.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000031#include "primitive.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010032#include "scoped_thread_state_change.h"
33#include "thread.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000034
35namespace art {
36
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010037/**
38 * Helper class to add HTemporary instructions. This class is used when
39 * converting a DEX instruction to multiple HInstruction, and where those
40 * instructions do not die at the following instruction, but instead spans
41 * multiple instructions.
42 */
43class Temporaries : public ValueObject {
44 public:
45 Temporaries(HGraph* graph, size_t count) : graph_(graph), count_(count), index_(0) {
46 graph_->UpdateNumberOfTemporaries(count_);
47 }
48
49 void Add(HInstruction* instruction) {
50 // We currently only support vreg size temps.
51 DCHECK(instruction->GetType() != Primitive::kPrimLong
52 && instruction->GetType() != Primitive::kPrimDouble);
53 HInstruction* temp = new (graph_->GetArena()) HTemporary(index_++);
54 instruction->GetBlock()->AddInstruction(temp);
55 DCHECK(temp->GetPrevious() == instruction);
56 }
57
58 private:
59 HGraph* const graph_;
60
61 // The total number of temporaries that will be used.
62 const size_t count_;
63
64 // Current index in the temporary stack, updated by `Add`.
65 size_t index_;
66};
67
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +010068static bool IsTypeSupported(Primitive::Type type) {
69 return type != Primitive::kPrimFloat && type != Primitive::kPrimDouble;
70}
71
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010072void HGraphBuilder::InitializeLocals(uint16_t count) {
73 graph_->SetNumberOfVRegs(count);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000074 locals_.SetSize(count);
75 for (int i = 0; i < count; i++) {
76 HLocal* local = new (arena_) HLocal(i);
77 entry_block_->AddInstruction(local);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000078 locals_.Put(i, local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000079 }
80}
81
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010082bool HGraphBuilder::InitializeParameters(uint16_t number_of_parameters) {
83 // dex_compilation_unit_ is null only when unit testing.
84 if (dex_compilation_unit_ == nullptr) {
85 return true;
86 }
87
88 graph_->SetNumberOfInVRegs(number_of_parameters);
89 const char* shorty = dex_compilation_unit_->GetShorty();
90 int locals_index = locals_.Size() - number_of_parameters;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010091 int parameter_index = 0;
92
93 if (!dex_compilation_unit_->IsStatic()) {
94 // Add the implicit 'this' argument, not expressed in the signature.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010095 HParameterValue* parameter =
96 new (arena_) HParameterValue(parameter_index++, Primitive::kPrimNot);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +010097 entry_block_->AddInstruction(parameter);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010098 HLocal* local = GetLocalAt(locals_index++);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +010099 entry_block_->AddInstruction(new (arena_) HStoreLocal(local, parameter));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100100 number_of_parameters--;
101 }
102
103 uint32_t pos = 1;
104 for (int i = 0; i < number_of_parameters; i++) {
105 switch (shorty[pos++]) {
106 case 'F':
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100107 case 'D': {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100108 return false;
109 }
110
111 default: {
112 // integer and reference parameters.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100113 HParameterValue* parameter =
114 new (arena_) HParameterValue(parameter_index++, Primitive::GetType(shorty[pos - 1]));
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100115 entry_block_->AddInstruction(parameter);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100116 HLocal* local = GetLocalAt(locals_index++);
117 // Store the parameter value in the local that the dex code will use
118 // to reference that parameter.
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100119 entry_block_->AddInstruction(new (arena_) HStoreLocal(local, parameter));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100120 if (parameter->GetType() == Primitive::kPrimLong) {
121 i++;
122 locals_index++;
123 parameter_index++;
124 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100125 break;
126 }
127 }
128 }
129 return true;
130}
131
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000132static bool CanHandleCodeItem(const DexFile::CodeItem& code_item) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000133 if (code_item.tries_size_ > 0) {
134 return false;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000135 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000136 return true;
137}
138
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100139template<typename T>
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100140void HGraphBuilder::If_22t(const Instruction& instruction, uint32_t dex_offset) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100141 HInstruction* first = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
142 HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
Dave Allison20dfc792014-06-16 20:44:29 -0700143 T* comparison = new (arena_) T(first, second);
144 current_block_->AddInstruction(comparison);
145 HInstruction* ifinst = new (arena_) HIf(comparison);
146 current_block_->AddInstruction(ifinst);
147 HBasicBlock* target = FindBlockStartingAt(dex_offset + instruction.GetTargetOffset());
148 DCHECK(target != nullptr);
149 current_block_->AddSuccessor(target);
150 target = FindBlockStartingAt(dex_offset + instruction.SizeInCodeUnits());
151 DCHECK(target != nullptr);
152 current_block_->AddSuccessor(target);
153 current_block_ = nullptr;
154}
155
156template<typename T>
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100157void HGraphBuilder::If_21t(const Instruction& instruction, uint32_t dex_offset) {
Dave Allison20dfc792014-06-16 20:44:29 -0700158 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
159 T* comparison = new (arena_) T(value, GetIntConstant(0));
160 current_block_->AddInstruction(comparison);
161 HInstruction* ifinst = new (arena_) HIf(comparison);
162 current_block_->AddInstruction(ifinst);
163 HBasicBlock* target = FindBlockStartingAt(dex_offset + instruction.GetTargetOffset());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100164 DCHECK(target != nullptr);
165 current_block_->AddSuccessor(target);
166 target = FindBlockStartingAt(dex_offset + instruction.SizeInCodeUnits());
167 DCHECK(target != nullptr);
168 current_block_->AddSuccessor(target);
169 current_block_ = nullptr;
170}
171
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000172HGraph* HGraphBuilder::BuildGraph(const DexFile::CodeItem& code_item) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000173 if (!CanHandleCodeItem(code_item)) {
174 return nullptr;
175 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000176
177 const uint16_t* code_ptr = code_item.insns_;
178 const uint16_t* code_end = code_item.insns_ + code_item.insns_size_in_code_units_;
179
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000180 // Setup the graph with the entry block and exit block.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000181 graph_ = new (arena_) HGraph(arena_);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000182 entry_block_ = new (arena_) HBasicBlock(graph_);
183 graph_->AddBlock(entry_block_);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000184 exit_block_ = new (arena_) HBasicBlock(graph_);
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000185 graph_->SetEntryBlock(entry_block_);
186 graph_->SetExitBlock(exit_block_);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000187
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000188 InitializeLocals(code_item.registers_size_);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100189 graph_->UpdateMaximumNumberOfOutVRegs(code_item.outs_size_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000190
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000191 // To avoid splitting blocks, we compute ahead of time the instructions that
192 // start a new block, and create these blocks.
193 ComputeBranchTargets(code_ptr, code_end);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000194
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100195 if (!InitializeParameters(code_item.ins_size_)) {
196 return nullptr;
197 }
198
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000199 size_t dex_offset = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000200 while (code_ptr < code_end) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000201 // Update the current block if dex_offset starts a new block.
202 MaybeUpdateCurrentBlock(dex_offset);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000203 const Instruction& instruction = *Instruction::At(code_ptr);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000204 if (!AnalyzeDexInstruction(instruction, dex_offset)) return nullptr;
205 dex_offset += instruction.SizeInCodeUnits();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000206 code_ptr += instruction.SizeInCodeUnits();
207 }
208
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000209 // Add the exit block at the end to give it the highest id.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000210 graph_->AddBlock(exit_block_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000211 exit_block_->AddInstruction(new (arena_) HExit());
212 entry_block_->AddInstruction(new (arena_) HGoto());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000213 return graph_;
214}
215
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000216void HGraphBuilder::MaybeUpdateCurrentBlock(size_t index) {
217 HBasicBlock* block = FindBlockStartingAt(index);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000218 if (block == nullptr) {
219 return;
220 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000221
222 if (current_block_ != nullptr) {
223 // Branching instructions clear current_block, so we know
224 // the last instruction of the current block is not a branching
225 // instruction. We add an unconditional goto to the found block.
226 current_block_->AddInstruction(new (arena_) HGoto());
227 current_block_->AddSuccessor(block);
228 }
229 graph_->AddBlock(block);
230 current_block_ = block;
231}
232
233void HGraphBuilder::ComputeBranchTargets(const uint16_t* code_ptr, const uint16_t* code_end) {
234 // TODO: Support switch instructions.
235 branch_targets_.SetSize(code_end - code_ptr);
236
237 // Create the first block for the dex instructions, single successor of the entry block.
238 HBasicBlock* block = new (arena_) HBasicBlock(graph_);
239 branch_targets_.Put(0, block);
240 entry_block_->AddSuccessor(block);
241
242 // Iterate over all instructions and find branching instructions. Create blocks for
243 // the locations these instructions branch to.
244 size_t dex_offset = 0;
245 while (code_ptr < code_end) {
246 const Instruction& instruction = *Instruction::At(code_ptr);
247 if (instruction.IsBranch()) {
248 int32_t target = instruction.GetTargetOffset() + dex_offset;
249 // Create a block for the target instruction.
250 if (FindBlockStartingAt(target) == nullptr) {
251 block = new (arena_) HBasicBlock(graph_);
252 branch_targets_.Put(target, block);
253 }
254 dex_offset += instruction.SizeInCodeUnits();
255 code_ptr += instruction.SizeInCodeUnits();
256 if ((code_ptr < code_end) && (FindBlockStartingAt(dex_offset) == nullptr)) {
257 block = new (arena_) HBasicBlock(graph_);
258 branch_targets_.Put(dex_offset, block);
259 }
260 } else {
261 code_ptr += instruction.SizeInCodeUnits();
262 dex_offset += instruction.SizeInCodeUnits();
263 }
264 }
265}
266
267HBasicBlock* HGraphBuilder::FindBlockStartingAt(int32_t index) const {
268 DCHECK_GE(index, 0);
269 return branch_targets_.Get(index);
270}
271
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100272template<typename T>
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100273void HGraphBuilder::Binop_23x(const Instruction& instruction, Primitive::Type type) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100274 HInstruction* first = LoadLocal(instruction.VRegB(), type);
275 HInstruction* second = LoadLocal(instruction.VRegC(), type);
276 current_block_->AddInstruction(new (arena_) T(type, first, second));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100277 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
278}
279
280template<typename T>
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100281void HGraphBuilder::Binop_12x(const Instruction& instruction, Primitive::Type type) {
282 HInstruction* first = LoadLocal(instruction.VRegA(), type);
283 HInstruction* second = LoadLocal(instruction.VRegB(), type);
284 current_block_->AddInstruction(new (arena_) T(type, first, second));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100285 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
286}
287
288template<typename T>
289void HGraphBuilder::Binop_22s(const Instruction& instruction, bool reverse) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100290 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
291 HInstruction* second = GetIntConstant(instruction.VRegC_22s());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100292 if (reverse) {
293 std::swap(first, second);
294 }
295 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second));
296 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
297}
298
299template<typename T>
300void HGraphBuilder::Binop_22b(const Instruction& instruction, bool reverse) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100301 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
302 HInstruction* second = GetIntConstant(instruction.VRegC_22b());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100303 if (reverse) {
304 std::swap(first, second);
305 }
306 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second));
307 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
308}
309
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100310void HGraphBuilder::BuildReturn(const Instruction& instruction, Primitive::Type type) {
311 if (type == Primitive::kPrimVoid) {
312 current_block_->AddInstruction(new (arena_) HReturnVoid());
313 } else {
314 HInstruction* value = LoadLocal(instruction.VRegA(), type);
315 current_block_->AddInstruction(new (arena_) HReturn(value));
316 }
317 current_block_->AddSuccessor(exit_block_);
318 current_block_ = nullptr;
319}
320
321bool HGraphBuilder::BuildInvoke(const Instruction& instruction,
322 uint32_t dex_offset,
323 uint32_t method_idx,
324 uint32_t number_of_vreg_arguments,
325 bool is_range,
326 uint32_t* args,
327 uint32_t register_index) {
328 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
329 const DexFile::ProtoId& proto_id = dex_file_->GetProtoId(method_id.proto_idx_);
330 const char* descriptor = dex_file_->StringDataByIdx(proto_id.shorty_idx_);
331 Primitive::Type return_type = Primitive::GetType(descriptor[0]);
332 bool is_instance_call =
333 instruction.Opcode() != Instruction::INVOKE_STATIC
334 && instruction.Opcode() != Instruction::INVOKE_STATIC_RANGE;
335 const size_t number_of_arguments = strlen(descriptor) - (is_instance_call ? 0 : 1);
336
337 // Treat invoke-direct like static calls for now.
338 HInvoke* invoke = new (arena_) HInvokeStatic(
339 arena_, number_of_arguments, return_type, dex_offset, method_idx);
340
341 size_t start_index = 0;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100342 Temporaries temps(graph_, is_instance_call ? 1 : 0);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100343 if (is_instance_call) {
344 HInstruction* arg = LoadLocal(is_range ? register_index : args[0], Primitive::kPrimNot);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100345 HNullCheck* null_check = new (arena_) HNullCheck(arg, dex_offset);
346 current_block_->AddInstruction(null_check);
347 temps.Add(null_check);
348 invoke->SetArgumentAt(0, null_check);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100349 start_index = 1;
350 }
351
352 uint32_t descriptor_index = 1;
353 uint32_t argument_index = start_index;
354 for (size_t i = start_index; i < number_of_vreg_arguments; i++, argument_index++) {
355 Primitive::Type type = Primitive::GetType(descriptor[descriptor_index++]);
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100356 if (!IsTypeSupported(type)) {
357 return false;
358 }
359 if (!is_range && type == Primitive::kPrimLong && args[i] + 1 != args[i + 1]) {
360 LOG(WARNING) << "Non sequential register pair in " << dex_compilation_unit_->GetSymbol()
361 << " at " << dex_offset;
362 // We do not implement non sequential register pair.
363 return false;
364 }
365 HInstruction* arg = LoadLocal(is_range ? register_index + i : args[i], type);
366 invoke->SetArgumentAt(argument_index, arg);
367 if (type == Primitive::kPrimLong) {
368 i++;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100369 }
370 }
371
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100372 if (!IsTypeSupported(return_type)) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100373 return false;
374 }
375
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100376 DCHECK_EQ(argument_index, number_of_arguments);
377 current_block_->AddInstruction(invoke);
378 return true;
379}
380
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100381bool HGraphBuilder::BuildFieldAccess(const Instruction& instruction,
382 uint32_t dex_offset,
383 bool is_put) {
384 uint32_t source_or_dest_reg = instruction.VRegA_22c();
385 uint32_t obj_reg = instruction.VRegB_22c();
386 uint16_t field_index = instruction.VRegC_22c();
387
388 ScopedObjectAccess soa(Thread::Current());
389 StackHandleScope<1> hs(soa.Self());
390 Handle<mirror::ArtField> resolved_field(hs.NewHandle(
391 compiler_driver_->ComputeInstanceFieldInfo(field_index, dex_compilation_unit_, is_put, soa)));
392
393 if (resolved_field.Get() == nullptr) {
394 return false;
395 }
396 if (resolved_field->IsVolatile()) {
397 return false;
398 }
399
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100400 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
401 if (!IsTypeSupported(field_type)) {
402 return false;
403 }
404
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100405 HInstruction* object = LoadLocal(obj_reg, Primitive::kPrimNot);
406 current_block_->AddInstruction(new (arena_) HNullCheck(object, dex_offset));
407 if (is_put) {
408 Temporaries temps(graph_, 1);
409 HInstruction* null_check = current_block_->GetLastInstruction();
410 // We need one temporary for the null check.
411 temps.Add(null_check);
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100412 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100413 current_block_->AddInstruction(new (arena_) HInstanceFieldSet(
414 null_check,
415 value,
416 resolved_field->GetOffset()));
417 } else {
418 current_block_->AddInstruction(new (arena_) HInstanceFieldGet(
419 current_block_->GetLastInstruction(),
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100420 field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100421 resolved_field->GetOffset()));
422
423 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
424 }
425 return true;
426}
427
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000428bool HGraphBuilder::AnalyzeDexInstruction(const Instruction& instruction, int32_t dex_offset) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000429 if (current_block_ == nullptr) {
430 return true; // Dead code
431 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000432
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000433 switch (instruction.Opcode()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000434 case Instruction::CONST_4: {
435 int32_t register_index = instruction.VRegA();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100436 HIntConstant* constant = GetIntConstant(instruction.VRegB_11n());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000437 UpdateLocal(register_index, constant);
438 break;
439 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000440
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100441 case Instruction::CONST_16: {
442 int32_t register_index = instruction.VRegA();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100443 HIntConstant* constant = GetIntConstant(instruction.VRegB_21s());
444 UpdateLocal(register_index, constant);
445 break;
446 }
447
Dave Allison20dfc792014-06-16 20:44:29 -0700448 case Instruction::CONST: {
449 int32_t register_index = instruction.VRegA();
450 HIntConstant* constant = GetIntConstant(instruction.VRegB_31i());
451 UpdateLocal(register_index, constant);
452 break;
453 }
454
455 case Instruction::CONST_HIGH16: {
456 int32_t register_index = instruction.VRegA();
457 HIntConstant* constant = GetIntConstant(instruction.VRegB_21h() << 16);
458 UpdateLocal(register_index, constant);
459 break;
460 }
461
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100462 case Instruction::CONST_WIDE_16: {
463 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -0700464 // Get 16 bits of constant value, sign extended to 64 bits.
465 int64_t value = instruction.VRegB_21s();
466 value <<= 48;
467 value >>= 48;
468 HLongConstant* constant = GetLongConstant(value);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100469 UpdateLocal(register_index, constant);
470 break;
471 }
472
473 case Instruction::CONST_WIDE_32: {
474 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -0700475 // Get 32 bits of constant value, sign extended to 64 bits.
476 int64_t value = instruction.VRegB_31i();
477 value <<= 32;
478 value >>= 32;
479 HLongConstant* constant = GetLongConstant(value);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100480 UpdateLocal(register_index, constant);
481 break;
482 }
483
484 case Instruction::CONST_WIDE: {
485 int32_t register_index = instruction.VRegA();
486 HLongConstant* constant = GetLongConstant(instruction.VRegB_51l());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100487 UpdateLocal(register_index, constant);
488 break;
489 }
490
Dave Allison20dfc792014-06-16 20:44:29 -0700491 case Instruction::CONST_WIDE_HIGH16: {
492 int32_t register_index = instruction.VRegA();
493 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
494 HLongConstant* constant = GetLongConstant(value);
495 UpdateLocal(register_index, constant);
496 break;
497 }
498
499 // TODO: these instructions are also used to move floating point values, so what is
500 // the type (int or float)?
501 case Instruction::MOVE:
502 case Instruction::MOVE_FROM16:
503 case Instruction::MOVE_16: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100504 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100505 UpdateLocal(instruction.VRegA(), value);
506 break;
507 }
508
Dave Allison20dfc792014-06-16 20:44:29 -0700509 // TODO: these instructions are also used to move floating point values, so what is
510 // the type (long or double)?
511 case Instruction::MOVE_WIDE:
512 case Instruction::MOVE_WIDE_FROM16:
513 case Instruction::MOVE_WIDE_16: {
514 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimLong);
515 UpdateLocal(instruction.VRegA(), value);
516 break;
517 }
518
519 case Instruction::MOVE_OBJECT:
520 case Instruction::MOVE_OBJECT_16:
521 case Instruction::MOVE_OBJECT_FROM16: {
522 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimNot);
523 UpdateLocal(instruction.VRegA(), value);
524 break;
525 }
526
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000527 case Instruction::RETURN_VOID: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100528 BuildReturn(instruction, Primitive::kPrimVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000529 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000530 }
531
Dave Allison20dfc792014-06-16 20:44:29 -0700532#define IF_XX(comparison, cond) \
533 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_offset); break; \
534 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_offset); break
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100535
Dave Allison20dfc792014-06-16 20:44:29 -0700536 IF_XX(HEqual, EQ);
537 IF_XX(HNotEqual, NE);
538 IF_XX(HLessThan, LT);
539 IF_XX(HLessThanOrEqual, LE);
540 IF_XX(HGreaterThan, GT);
541 IF_XX(HGreaterThanOrEqual, GE);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000542
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000543 case Instruction::GOTO:
544 case Instruction::GOTO_16:
545 case Instruction::GOTO_32: {
546 HBasicBlock* target = FindBlockStartingAt(instruction.GetTargetOffset() + dex_offset);
547 DCHECK(target != nullptr);
548 current_block_->AddInstruction(new (arena_) HGoto());
549 current_block_->AddSuccessor(target);
550 current_block_ = nullptr;
551 break;
552 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000553
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100554 case Instruction::RETURN: {
555 BuildReturn(instruction, Primitive::kPrimInt);
556 break;
557 }
558
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100559 case Instruction::RETURN_OBJECT: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100560 BuildReturn(instruction, Primitive::kPrimNot);
561 break;
562 }
563
564 case Instruction::RETURN_WIDE: {
565 BuildReturn(instruction, Primitive::kPrimLong);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000566 break;
567 }
568
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100569 case Instruction::INVOKE_STATIC:
570 case Instruction::INVOKE_DIRECT: {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000571 uint32_t method_idx = instruction.VRegB_35c();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100572 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100573 uint32_t args[5];
Ian Rogers29a26482014-05-02 15:27:29 -0700574 instruction.GetVarArgs(args);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100575 if (!BuildInvoke(instruction, dex_offset, method_idx, number_of_vreg_arguments, false, args, -1)) {
576 return false;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100577 }
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100578 break;
579 }
580
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100581 case Instruction::INVOKE_STATIC_RANGE:
582 case Instruction::INVOKE_DIRECT_RANGE: {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100583 uint32_t method_idx = instruction.VRegB_3rc();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100584 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
585 uint32_t register_index = instruction.VRegC();
586 if (!BuildInvoke(instruction, dex_offset, method_idx,
587 number_of_vreg_arguments, true, nullptr, register_index)) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100588 return false;
589 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000590 break;
591 }
592
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000593 case Instruction::ADD_INT: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100594 Binop_23x<HAdd>(instruction, Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100595 break;
596 }
597
598 case Instruction::ADD_LONG: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100599 Binop_23x<HAdd>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100600 break;
601 }
602
603 case Instruction::SUB_INT: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100604 Binop_23x<HSub>(instruction, Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100605 break;
606 }
607
608 case Instruction::SUB_LONG: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100609 Binop_23x<HSub>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000610 break;
611 }
612
613 case Instruction::ADD_INT_2ADDR: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100614 Binop_12x<HAdd>(instruction, Primitive::kPrimInt);
615 break;
616 }
617
618 case Instruction::ADD_LONG_2ADDR: {
619 Binop_12x<HAdd>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100620 break;
621 }
622
623 case Instruction::SUB_INT_2ADDR: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100624 Binop_12x<HSub>(instruction, Primitive::kPrimInt);
625 break;
626 }
627
628 case Instruction::SUB_LONG_2ADDR: {
629 Binop_12x<HSub>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000630 break;
631 }
632
633 case Instruction::ADD_INT_LIT16: {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100634 Binop_22s<HAdd>(instruction, false);
635 break;
636 }
637
638 case Instruction::RSUB_INT: {
639 Binop_22s<HSub>(instruction, true);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000640 break;
641 }
642
643 case Instruction::ADD_INT_LIT8: {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100644 Binop_22b<HAdd>(instruction, false);
645 break;
646 }
647
648 case Instruction::RSUB_INT_LIT8: {
649 Binop_22b<HSub>(instruction, true);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000650 break;
651 }
652
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100653 case Instruction::NEW_INSTANCE: {
654 current_block_->AddInstruction(
655 new (arena_) HNewInstance(dex_offset, instruction.VRegB_21c()));
656 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
657 break;
658 }
659
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100660 case Instruction::MOVE_RESULT:
Dave Allison20dfc792014-06-16 20:44:29 -0700661 case Instruction::MOVE_RESULT_WIDE:
662 case Instruction::MOVE_RESULT_OBJECT:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100663 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
664 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100665
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100666 case Instruction::CMP_LONG: {
667 Binop_23x<HCompare>(instruction, Primitive::kPrimLong);
668 break;
669 }
670
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000671 case Instruction::NOP:
672 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000673
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100674 case Instruction::IGET:
675 case Instruction::IGET_WIDE:
676 case Instruction::IGET_OBJECT:
677 case Instruction::IGET_BOOLEAN:
678 case Instruction::IGET_BYTE:
679 case Instruction::IGET_CHAR:
680 case Instruction::IGET_SHORT: {
681 if (!BuildFieldAccess(instruction, dex_offset, false)) {
682 return false;
683 }
684 break;
685 }
686
687 case Instruction::IPUT:
688 case Instruction::IPUT_WIDE:
689 case Instruction::IPUT_OBJECT:
690 case Instruction::IPUT_BOOLEAN:
691 case Instruction::IPUT_BYTE:
692 case Instruction::IPUT_CHAR:
693 case Instruction::IPUT_SHORT: {
694 if (!BuildFieldAccess(instruction, dex_offset, true)) {
695 return false;
696 }
697 break;
698 }
699
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000700 default:
701 return false;
702 }
703 return true;
704}
705
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100706HIntConstant* HGraphBuilder::GetIntConstant0() {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000707 if (constant0_ != nullptr) {
708 return constant0_;
709 }
710 constant0_ = new(arena_) HIntConstant(0);
711 entry_block_->AddInstruction(constant0_);
712 return constant0_;
713}
714
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100715HIntConstant* HGraphBuilder::GetIntConstant1() {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000716 if (constant1_ != nullptr) {
717 return constant1_;
718 }
719 constant1_ = new(arena_) HIntConstant(1);
720 entry_block_->AddInstruction(constant1_);
721 return constant1_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000722}
723
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100724HIntConstant* HGraphBuilder::GetIntConstant(int32_t constant) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000725 switch (constant) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100726 case 0: return GetIntConstant0();
727 case 1: return GetIntConstant1();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000728 default: {
729 HIntConstant* instruction = new (arena_) HIntConstant(constant);
730 entry_block_->AddInstruction(instruction);
731 return instruction;
732 }
733 }
734}
735
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100736HLongConstant* HGraphBuilder::GetLongConstant(int64_t constant) {
737 HLongConstant* instruction = new (arena_) HLongConstant(constant);
738 entry_block_->AddInstruction(instruction);
739 return instruction;
740}
741
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000742HLocal* HGraphBuilder::GetLocalAt(int register_index) const {
743 return locals_.Get(register_index);
744}
745
746void HGraphBuilder::UpdateLocal(int register_index, HInstruction* instruction) const {
747 HLocal* local = GetLocalAt(register_index);
748 current_block_->AddInstruction(new (arena_) HStoreLocal(local, instruction));
749}
750
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100751HInstruction* HGraphBuilder::LoadLocal(int register_index, Primitive::Type type) const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000752 HLocal* local = GetLocalAt(register_index);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100753 current_block_->AddInstruction(new (arena_) HLoadLocal(local, type));
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000754 return current_block_->GetLastInstruction();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000755}
756
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000757} // namespace art