blob: 71e71f79d4c403c0e2216b7f1c018e4f6d33d29b [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 Geoffray6fbce022014-09-10 10:23:58 +0100141 int32_t target_offset = instruction.GetTargetOffset();
142 PotentiallyAddSuspendCheck(target_offset, dex_offset);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100143 HInstruction* first = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
144 HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
Dave Allison20dfc792014-06-16 20:44:29 -0700145 T* comparison = new (arena_) T(first, second);
146 current_block_->AddInstruction(comparison);
147 HInstruction* ifinst = new (arena_) HIf(comparison);
148 current_block_->AddInstruction(ifinst);
Nicolas Geoffray6fbce022014-09-10 10:23:58 +0100149 HBasicBlock* target = FindBlockStartingAt(dex_offset + target_offset);
Dave Allison20dfc792014-06-16 20:44:29 -0700150 DCHECK(target != nullptr);
151 current_block_->AddSuccessor(target);
152 target = FindBlockStartingAt(dex_offset + instruction.SizeInCodeUnits());
153 DCHECK(target != nullptr);
154 current_block_->AddSuccessor(target);
155 current_block_ = nullptr;
156}
157
158template<typename T>
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100159void HGraphBuilder::If_21t(const Instruction& instruction, uint32_t dex_offset) {
Nicolas Geoffray6fbce022014-09-10 10:23:58 +0100160 int32_t target_offset = instruction.GetTargetOffset();
161 PotentiallyAddSuspendCheck(target_offset, dex_offset);
Dave Allison20dfc792014-06-16 20:44:29 -0700162 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
163 T* comparison = new (arena_) T(value, GetIntConstant(0));
164 current_block_->AddInstruction(comparison);
165 HInstruction* ifinst = new (arena_) HIf(comparison);
166 current_block_->AddInstruction(ifinst);
Nicolas Geoffray6fbce022014-09-10 10:23:58 +0100167 fprintf(stderr, "%d and %d\n", dex_offset, target_offset);
168 HBasicBlock* target = FindBlockStartingAt(dex_offset + target_offset);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100169 DCHECK(target != nullptr);
170 current_block_->AddSuccessor(target);
171 target = FindBlockStartingAt(dex_offset + instruction.SizeInCodeUnits());
172 DCHECK(target != nullptr);
173 current_block_->AddSuccessor(target);
174 current_block_ = nullptr;
175}
176
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000177HGraph* HGraphBuilder::BuildGraph(const DexFile::CodeItem& code_item) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000178 if (!CanHandleCodeItem(code_item)) {
179 return nullptr;
180 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000181
182 const uint16_t* code_ptr = code_item.insns_;
183 const uint16_t* code_end = code_item.insns_ + code_item.insns_size_in_code_units_;
184
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000185 // Setup the graph with the entry block and exit block.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000186 graph_ = new (arena_) HGraph(arena_);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000187 entry_block_ = new (arena_) HBasicBlock(graph_);
188 graph_->AddBlock(entry_block_);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000189 exit_block_ = new (arena_) HBasicBlock(graph_);
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000190 graph_->SetEntryBlock(entry_block_);
191 graph_->SetExitBlock(exit_block_);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000192
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000193 InitializeLocals(code_item.registers_size_);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100194 graph_->UpdateMaximumNumberOfOutVRegs(code_item.outs_size_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000195
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000196 // To avoid splitting blocks, we compute ahead of time the instructions that
197 // start a new block, and create these blocks.
198 ComputeBranchTargets(code_ptr, code_end);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000199
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100200 if (!InitializeParameters(code_item.ins_size_)) {
201 return nullptr;
202 }
203
Nicolas Geoffray6fbce022014-09-10 10:23:58 +0100204 // Add the suspend check to the entry block.
205 entry_block_->AddInstruction(new (arena_) HSuspendCheck(0));
206
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000207 size_t dex_offset = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000208 while (code_ptr < code_end) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000209 // Update the current block if dex_offset starts a new block.
210 MaybeUpdateCurrentBlock(dex_offset);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000211 const Instruction& instruction = *Instruction::At(code_ptr);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000212 if (!AnalyzeDexInstruction(instruction, dex_offset)) return nullptr;
213 dex_offset += instruction.SizeInCodeUnits();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000214 code_ptr += instruction.SizeInCodeUnits();
215 }
216
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000217 // Add the exit block at the end to give it the highest id.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000218 graph_->AddBlock(exit_block_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000219 exit_block_->AddInstruction(new (arena_) HExit());
220 entry_block_->AddInstruction(new (arena_) HGoto());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000221 return graph_;
222}
223
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000224void HGraphBuilder::MaybeUpdateCurrentBlock(size_t index) {
225 HBasicBlock* block = FindBlockStartingAt(index);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000226 if (block == nullptr) {
227 return;
228 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000229
230 if (current_block_ != nullptr) {
231 // Branching instructions clear current_block, so we know
232 // the last instruction of the current block is not a branching
233 // instruction. We add an unconditional goto to the found block.
234 current_block_->AddInstruction(new (arena_) HGoto());
235 current_block_->AddSuccessor(block);
236 }
237 graph_->AddBlock(block);
238 current_block_ = block;
239}
240
241void HGraphBuilder::ComputeBranchTargets(const uint16_t* code_ptr, const uint16_t* code_end) {
242 // TODO: Support switch instructions.
243 branch_targets_.SetSize(code_end - code_ptr);
244
245 // Create the first block for the dex instructions, single successor of the entry block.
246 HBasicBlock* block = new (arena_) HBasicBlock(graph_);
247 branch_targets_.Put(0, block);
248 entry_block_->AddSuccessor(block);
249
250 // Iterate over all instructions and find branching instructions. Create blocks for
251 // the locations these instructions branch to.
252 size_t dex_offset = 0;
253 while (code_ptr < code_end) {
254 const Instruction& instruction = *Instruction::At(code_ptr);
255 if (instruction.IsBranch()) {
256 int32_t target = instruction.GetTargetOffset() + dex_offset;
257 // Create a block for the target instruction.
258 if (FindBlockStartingAt(target) == nullptr) {
259 block = new (arena_) HBasicBlock(graph_);
260 branch_targets_.Put(target, block);
261 }
262 dex_offset += instruction.SizeInCodeUnits();
263 code_ptr += instruction.SizeInCodeUnits();
264 if ((code_ptr < code_end) && (FindBlockStartingAt(dex_offset) == nullptr)) {
265 block = new (arena_) HBasicBlock(graph_);
266 branch_targets_.Put(dex_offset, block);
267 }
268 } else {
269 code_ptr += instruction.SizeInCodeUnits();
270 dex_offset += instruction.SizeInCodeUnits();
271 }
272 }
273}
274
275HBasicBlock* HGraphBuilder::FindBlockStartingAt(int32_t index) const {
276 DCHECK_GE(index, 0);
277 return branch_targets_.Get(index);
278}
279
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100280template<typename T>
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100281void HGraphBuilder::Binop_23x(const Instruction& instruction, Primitive::Type type) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100282 HInstruction* first = LoadLocal(instruction.VRegB(), type);
283 HInstruction* second = LoadLocal(instruction.VRegC(), 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>
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100289void HGraphBuilder::Binop_12x(const Instruction& instruction, Primitive::Type type) {
290 HInstruction* first = LoadLocal(instruction.VRegA(), type);
291 HInstruction* second = LoadLocal(instruction.VRegB(), type);
292 current_block_->AddInstruction(new (arena_) T(type, first, second));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100293 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
294}
295
296template<typename T>
297void HGraphBuilder::Binop_22s(const Instruction& instruction, bool reverse) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100298 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
299 HInstruction* second = GetIntConstant(instruction.VRegC_22s());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100300 if (reverse) {
301 std::swap(first, second);
302 }
303 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second));
304 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
305}
306
307template<typename T>
308void HGraphBuilder::Binop_22b(const Instruction& instruction, bool reverse) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100309 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
310 HInstruction* second = GetIntConstant(instruction.VRegC_22b());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100311 if (reverse) {
312 std::swap(first, second);
313 }
314 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second));
315 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
316}
317
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100318void HGraphBuilder::BuildReturn(const Instruction& instruction, Primitive::Type type) {
319 if (type == Primitive::kPrimVoid) {
320 current_block_->AddInstruction(new (arena_) HReturnVoid());
321 } else {
322 HInstruction* value = LoadLocal(instruction.VRegA(), type);
323 current_block_->AddInstruction(new (arena_) HReturn(value));
324 }
325 current_block_->AddSuccessor(exit_block_);
326 current_block_ = nullptr;
327}
328
329bool HGraphBuilder::BuildInvoke(const Instruction& instruction,
330 uint32_t dex_offset,
331 uint32_t method_idx,
332 uint32_t number_of_vreg_arguments,
333 bool is_range,
334 uint32_t* args,
335 uint32_t register_index) {
336 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
337 const DexFile::ProtoId& proto_id = dex_file_->GetProtoId(method_id.proto_idx_);
338 const char* descriptor = dex_file_->StringDataByIdx(proto_id.shorty_idx_);
339 Primitive::Type return_type = Primitive::GetType(descriptor[0]);
340 bool is_instance_call =
341 instruction.Opcode() != Instruction::INVOKE_STATIC
342 && instruction.Opcode() != Instruction::INVOKE_STATIC_RANGE;
343 const size_t number_of_arguments = strlen(descriptor) - (is_instance_call ? 0 : 1);
344
345 // Treat invoke-direct like static calls for now.
346 HInvoke* invoke = new (arena_) HInvokeStatic(
347 arena_, number_of_arguments, return_type, dex_offset, method_idx);
348
349 size_t start_index = 0;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100350 Temporaries temps(graph_, is_instance_call ? 1 : 0);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100351 if (is_instance_call) {
352 HInstruction* arg = LoadLocal(is_range ? register_index : args[0], Primitive::kPrimNot);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100353 HNullCheck* null_check = new (arena_) HNullCheck(arg, dex_offset);
354 current_block_->AddInstruction(null_check);
355 temps.Add(null_check);
356 invoke->SetArgumentAt(0, null_check);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100357 start_index = 1;
358 }
359
360 uint32_t descriptor_index = 1;
361 uint32_t argument_index = start_index;
362 for (size_t i = start_index; i < number_of_vreg_arguments; i++, argument_index++) {
363 Primitive::Type type = Primitive::GetType(descriptor[descriptor_index++]);
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100364 if (!IsTypeSupported(type)) {
365 return false;
366 }
367 if (!is_range && type == Primitive::kPrimLong && args[i] + 1 != args[i + 1]) {
368 LOG(WARNING) << "Non sequential register pair in " << dex_compilation_unit_->GetSymbol()
369 << " at " << dex_offset;
370 // We do not implement non sequential register pair.
371 return false;
372 }
373 HInstruction* arg = LoadLocal(is_range ? register_index + i : args[i], type);
374 invoke->SetArgumentAt(argument_index, arg);
375 if (type == Primitive::kPrimLong) {
376 i++;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100377 }
378 }
379
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100380 if (!IsTypeSupported(return_type)) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100381 return false;
382 }
383
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100384 DCHECK_EQ(argument_index, number_of_arguments);
385 current_block_->AddInstruction(invoke);
386 return true;
387}
388
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100389bool HGraphBuilder::BuildFieldAccess(const Instruction& instruction,
390 uint32_t dex_offset,
391 bool is_put) {
392 uint32_t source_or_dest_reg = instruction.VRegA_22c();
393 uint32_t obj_reg = instruction.VRegB_22c();
394 uint16_t field_index = instruction.VRegC_22c();
395
396 ScopedObjectAccess soa(Thread::Current());
397 StackHandleScope<1> hs(soa.Self());
398 Handle<mirror::ArtField> resolved_field(hs.NewHandle(
399 compiler_driver_->ComputeInstanceFieldInfo(field_index, dex_compilation_unit_, is_put, soa)));
400
401 if (resolved_field.Get() == nullptr) {
402 return false;
403 }
404 if (resolved_field->IsVolatile()) {
405 return false;
406 }
407
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100408 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
409 if (!IsTypeSupported(field_type)) {
410 return false;
411 }
412
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100413 HInstruction* object = LoadLocal(obj_reg, Primitive::kPrimNot);
414 current_block_->AddInstruction(new (arena_) HNullCheck(object, dex_offset));
415 if (is_put) {
416 Temporaries temps(graph_, 1);
417 HInstruction* null_check = current_block_->GetLastInstruction();
418 // We need one temporary for the null check.
419 temps.Add(null_check);
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100420 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100421 current_block_->AddInstruction(new (arena_) HInstanceFieldSet(
422 null_check,
423 value,
Nicolas Geoffray39468442014-09-02 15:17:15 +0100424 field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100425 resolved_field->GetOffset()));
426 } else {
427 current_block_->AddInstruction(new (arena_) HInstanceFieldGet(
428 current_block_->GetLastInstruction(),
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100429 field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100430 resolved_field->GetOffset()));
431
432 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
433 }
434 return true;
435}
436
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100437void HGraphBuilder::BuildArrayAccess(const Instruction& instruction,
438 uint32_t dex_offset,
439 bool is_put,
440 Primitive::Type anticipated_type) {
441 uint8_t source_or_dest_reg = instruction.VRegA_23x();
442 uint8_t array_reg = instruction.VRegB_23x();
443 uint8_t index_reg = instruction.VRegC_23x();
444
445 DCHECK(IsTypeSupported(anticipated_type));
446
447 // We need one temporary for the null check, one for the index, and one for the length.
448 Temporaries temps(graph_, 3);
449
450 HInstruction* object = LoadLocal(array_reg, Primitive::kPrimNot);
451 object = new (arena_) HNullCheck(object, dex_offset);
452 current_block_->AddInstruction(object);
453 temps.Add(object);
454
455 HInstruction* length = new (arena_) HArrayLength(object);
456 current_block_->AddInstruction(length);
457 temps.Add(length);
458 HInstruction* index = LoadLocal(index_reg, Primitive::kPrimInt);
459 index = new (arena_) HBoundsCheck(index, length, dex_offset);
460 current_block_->AddInstruction(index);
461 temps.Add(index);
462 if (is_put) {
463 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type);
464 // TODO: Insert a type check node if the type is Object.
Nicolas Geoffray39468442014-09-02 15:17:15 +0100465 current_block_->AddInstruction(new (arena_) HArraySet(
466 object, index, value, anticipated_type, dex_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100467 } else {
468 current_block_->AddInstruction(new (arena_) HArrayGet(object, index, anticipated_type));
469 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
470 }
471}
472
Nicolas Geoffray6fbce022014-09-10 10:23:58 +0100473void HGraphBuilder::PotentiallyAddSuspendCheck(int32_t target_offset, uint32_t dex_offset) {
474 if (target_offset <= 0) {
475 // Unconditionnally add a suspend check to backward branches. We can remove
476 // them after we recognize loops in the graph.
477 current_block_->AddInstruction(new (arena_) HSuspendCheck(dex_offset));
478 }
479}
480
481bool HGraphBuilder::AnalyzeDexInstruction(const Instruction& instruction, uint32_t dex_offset) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000482 if (current_block_ == nullptr) {
483 return true; // Dead code
484 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000485
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000486 switch (instruction.Opcode()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000487 case Instruction::CONST_4: {
488 int32_t register_index = instruction.VRegA();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100489 HIntConstant* constant = GetIntConstant(instruction.VRegB_11n());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000490 UpdateLocal(register_index, constant);
491 break;
492 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000493
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100494 case Instruction::CONST_16: {
495 int32_t register_index = instruction.VRegA();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100496 HIntConstant* constant = GetIntConstant(instruction.VRegB_21s());
497 UpdateLocal(register_index, constant);
498 break;
499 }
500
Dave Allison20dfc792014-06-16 20:44:29 -0700501 case Instruction::CONST: {
502 int32_t register_index = instruction.VRegA();
503 HIntConstant* constant = GetIntConstant(instruction.VRegB_31i());
504 UpdateLocal(register_index, constant);
505 break;
506 }
507
508 case Instruction::CONST_HIGH16: {
509 int32_t register_index = instruction.VRegA();
510 HIntConstant* constant = GetIntConstant(instruction.VRegB_21h() << 16);
511 UpdateLocal(register_index, constant);
512 break;
513 }
514
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100515 case Instruction::CONST_WIDE_16: {
516 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -0700517 // Get 16 bits of constant value, sign extended to 64 bits.
518 int64_t value = instruction.VRegB_21s();
519 value <<= 48;
520 value >>= 48;
521 HLongConstant* constant = GetLongConstant(value);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100522 UpdateLocal(register_index, constant);
523 break;
524 }
525
526 case Instruction::CONST_WIDE_32: {
527 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -0700528 // Get 32 bits of constant value, sign extended to 64 bits.
529 int64_t value = instruction.VRegB_31i();
530 value <<= 32;
531 value >>= 32;
532 HLongConstant* constant = GetLongConstant(value);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100533 UpdateLocal(register_index, constant);
534 break;
535 }
536
537 case Instruction::CONST_WIDE: {
538 int32_t register_index = instruction.VRegA();
539 HLongConstant* constant = GetLongConstant(instruction.VRegB_51l());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100540 UpdateLocal(register_index, constant);
541 break;
542 }
543
Dave Allison20dfc792014-06-16 20:44:29 -0700544 case Instruction::CONST_WIDE_HIGH16: {
545 int32_t register_index = instruction.VRegA();
546 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
547 HLongConstant* constant = GetLongConstant(value);
548 UpdateLocal(register_index, constant);
549 break;
550 }
551
552 // TODO: these instructions are also used to move floating point values, so what is
553 // the type (int or float)?
554 case Instruction::MOVE:
555 case Instruction::MOVE_FROM16:
556 case Instruction::MOVE_16: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100557 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100558 UpdateLocal(instruction.VRegA(), value);
559 break;
560 }
561
Dave Allison20dfc792014-06-16 20:44:29 -0700562 // TODO: these instructions are also used to move floating point values, so what is
563 // the type (long or double)?
564 case Instruction::MOVE_WIDE:
565 case Instruction::MOVE_WIDE_FROM16:
566 case Instruction::MOVE_WIDE_16: {
567 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimLong);
568 UpdateLocal(instruction.VRegA(), value);
569 break;
570 }
571
572 case Instruction::MOVE_OBJECT:
573 case Instruction::MOVE_OBJECT_16:
574 case Instruction::MOVE_OBJECT_FROM16: {
575 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimNot);
576 UpdateLocal(instruction.VRegA(), value);
577 break;
578 }
579
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000580 case Instruction::RETURN_VOID: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100581 BuildReturn(instruction, Primitive::kPrimVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000582 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000583 }
584
Dave Allison20dfc792014-06-16 20:44:29 -0700585#define IF_XX(comparison, cond) \
586 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_offset); break; \
587 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_offset); break
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100588
Dave Allison20dfc792014-06-16 20:44:29 -0700589 IF_XX(HEqual, EQ);
590 IF_XX(HNotEqual, NE);
591 IF_XX(HLessThan, LT);
592 IF_XX(HLessThanOrEqual, LE);
593 IF_XX(HGreaterThan, GT);
594 IF_XX(HGreaterThanOrEqual, GE);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000595
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000596 case Instruction::GOTO:
597 case Instruction::GOTO_16:
598 case Instruction::GOTO_32: {
Nicolas Geoffray6fbce022014-09-10 10:23:58 +0100599 int32_t offset = instruction.GetTargetOffset();
600 PotentiallyAddSuspendCheck(offset, dex_offset);
601 HBasicBlock* target = FindBlockStartingAt(offset + dex_offset);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000602 DCHECK(target != nullptr);
603 current_block_->AddInstruction(new (arena_) HGoto());
604 current_block_->AddSuccessor(target);
605 current_block_ = nullptr;
606 break;
607 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000608
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100609 case Instruction::RETURN: {
610 BuildReturn(instruction, Primitive::kPrimInt);
611 break;
612 }
613
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100614 case Instruction::RETURN_OBJECT: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100615 BuildReturn(instruction, Primitive::kPrimNot);
616 break;
617 }
618
619 case Instruction::RETURN_WIDE: {
620 BuildReturn(instruction, Primitive::kPrimLong);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000621 break;
622 }
623
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100624 case Instruction::INVOKE_STATIC:
625 case Instruction::INVOKE_DIRECT: {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000626 uint32_t method_idx = instruction.VRegB_35c();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100627 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100628 uint32_t args[5];
Ian Rogers29a26482014-05-02 15:27:29 -0700629 instruction.GetVarArgs(args);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100630 if (!BuildInvoke(instruction, dex_offset, method_idx, number_of_vreg_arguments, false, args, -1)) {
631 return false;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100632 }
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100633 break;
634 }
635
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100636 case Instruction::INVOKE_STATIC_RANGE:
637 case Instruction::INVOKE_DIRECT_RANGE: {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100638 uint32_t method_idx = instruction.VRegB_3rc();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100639 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
640 uint32_t register_index = instruction.VRegC();
641 if (!BuildInvoke(instruction, dex_offset, method_idx,
642 number_of_vreg_arguments, true, nullptr, register_index)) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100643 return false;
644 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000645 break;
646 }
647
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000648 case Instruction::ADD_INT: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100649 Binop_23x<HAdd>(instruction, Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100650 break;
651 }
652
653 case Instruction::ADD_LONG: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100654 Binop_23x<HAdd>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100655 break;
656 }
657
658 case Instruction::SUB_INT: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100659 Binop_23x<HSub>(instruction, Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100660 break;
661 }
662
663 case Instruction::SUB_LONG: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100664 Binop_23x<HSub>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000665 break;
666 }
667
668 case Instruction::ADD_INT_2ADDR: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100669 Binop_12x<HAdd>(instruction, Primitive::kPrimInt);
670 break;
671 }
672
673 case Instruction::ADD_LONG_2ADDR: {
674 Binop_12x<HAdd>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100675 break;
676 }
677
678 case Instruction::SUB_INT_2ADDR: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100679 Binop_12x<HSub>(instruction, Primitive::kPrimInt);
680 break;
681 }
682
683 case Instruction::SUB_LONG_2ADDR: {
684 Binop_12x<HSub>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000685 break;
686 }
687
688 case Instruction::ADD_INT_LIT16: {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100689 Binop_22s<HAdd>(instruction, false);
690 break;
691 }
692
693 case Instruction::RSUB_INT: {
694 Binop_22s<HSub>(instruction, true);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000695 break;
696 }
697
698 case Instruction::ADD_INT_LIT8: {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100699 Binop_22b<HAdd>(instruction, false);
700 break;
701 }
702
703 case Instruction::RSUB_INT_LIT8: {
704 Binop_22b<HSub>(instruction, true);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000705 break;
706 }
707
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100708 case Instruction::NEW_INSTANCE: {
709 current_block_->AddInstruction(
710 new (arena_) HNewInstance(dex_offset, instruction.VRegB_21c()));
711 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
712 break;
713 }
714
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100715 case Instruction::MOVE_RESULT:
Dave Allison20dfc792014-06-16 20:44:29 -0700716 case Instruction::MOVE_RESULT_WIDE:
717 case Instruction::MOVE_RESULT_OBJECT:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100718 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
719 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100720
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100721 case Instruction::CMP_LONG: {
722 Binop_23x<HCompare>(instruction, Primitive::kPrimLong);
723 break;
724 }
725
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000726 case Instruction::NOP:
727 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000728
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100729 case Instruction::IGET:
730 case Instruction::IGET_WIDE:
731 case Instruction::IGET_OBJECT:
732 case Instruction::IGET_BOOLEAN:
733 case Instruction::IGET_BYTE:
734 case Instruction::IGET_CHAR:
735 case Instruction::IGET_SHORT: {
736 if (!BuildFieldAccess(instruction, dex_offset, false)) {
737 return false;
738 }
739 break;
740 }
741
742 case Instruction::IPUT:
743 case Instruction::IPUT_WIDE:
744 case Instruction::IPUT_OBJECT:
745 case Instruction::IPUT_BOOLEAN:
746 case Instruction::IPUT_BYTE:
747 case Instruction::IPUT_CHAR:
748 case Instruction::IPUT_SHORT: {
749 if (!BuildFieldAccess(instruction, dex_offset, true)) {
750 return false;
751 }
752 break;
753 }
754
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100755#define ARRAY_XX(kind, anticipated_type) \
756 case Instruction::AGET##kind: { \
757 BuildArrayAccess(instruction, dex_offset, false, anticipated_type); \
758 break; \
759 } \
760 case Instruction::APUT##kind: { \
761 BuildArrayAccess(instruction, dex_offset, true, anticipated_type); \
762 break; \
763 }
764
765 ARRAY_XX(, Primitive::kPrimInt);
766 ARRAY_XX(_WIDE, Primitive::kPrimLong);
767 ARRAY_XX(_OBJECT, Primitive::kPrimNot);
768 ARRAY_XX(_BOOLEAN, Primitive::kPrimBoolean);
769 ARRAY_XX(_BYTE, Primitive::kPrimByte);
770 ARRAY_XX(_CHAR, Primitive::kPrimChar);
771 ARRAY_XX(_SHORT, Primitive::kPrimShort);
772
Nicolas Geoffray39468442014-09-02 15:17:15 +0100773 case Instruction::ARRAY_LENGTH: {
774 HInstruction* object = LoadLocal(instruction.VRegB_12x(), Primitive::kPrimNot);
775 current_block_->AddInstruction(new (arena_) HArrayLength(object));
776 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction());
777 break;
778 }
779
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000780 default:
781 return false;
782 }
783 return true;
784}
785
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100786HIntConstant* HGraphBuilder::GetIntConstant0() {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000787 if (constant0_ != nullptr) {
788 return constant0_;
789 }
790 constant0_ = new(arena_) HIntConstant(0);
791 entry_block_->AddInstruction(constant0_);
792 return constant0_;
793}
794
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100795HIntConstant* HGraphBuilder::GetIntConstant1() {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000796 if (constant1_ != nullptr) {
797 return constant1_;
798 }
799 constant1_ = new(arena_) HIntConstant(1);
800 entry_block_->AddInstruction(constant1_);
801 return constant1_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000802}
803
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100804HIntConstant* HGraphBuilder::GetIntConstant(int32_t constant) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000805 switch (constant) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100806 case 0: return GetIntConstant0();
807 case 1: return GetIntConstant1();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000808 default: {
809 HIntConstant* instruction = new (arena_) HIntConstant(constant);
810 entry_block_->AddInstruction(instruction);
811 return instruction;
812 }
813 }
814}
815
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100816HLongConstant* HGraphBuilder::GetLongConstant(int64_t constant) {
817 HLongConstant* instruction = new (arena_) HLongConstant(constant);
818 entry_block_->AddInstruction(instruction);
819 return instruction;
820}
821
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000822HLocal* HGraphBuilder::GetLocalAt(int register_index) const {
823 return locals_.Get(register_index);
824}
825
826void HGraphBuilder::UpdateLocal(int register_index, HInstruction* instruction) const {
827 HLocal* local = GetLocalAt(register_index);
828 current_block_->AddInstruction(new (arena_) HStoreLocal(local, instruction));
829}
830
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100831HInstruction* HGraphBuilder::LoadLocal(int register_index, Primitive::Type type) const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000832 HLocal* local = GetLocalAt(register_index);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100833 current_block_->AddInstruction(new (arena_) HLoadLocal(local, type));
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000834 return current_block_->GetLastInstruction();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000835}
836
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000837} // namespace art