blob: 4ce23d725b208135a9c77c47dc068e959e3feb56 [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002 * 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 Geoffraye5038322014-07-04 09:41:32 +010017#include "builder.h"
18
19#include "class_linker.h"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000020#include "dex_file.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000021#include "dex_file-inl.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000022#include "dex_instruction.h"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000023#include "dex_instruction-inl.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010024#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 Geoffray818f2102014-02-18 16:43:35 +000029#include "nodes.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000030#include "primitive.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010031#include "scoped_thread_state_change.h"
32#include "thread.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000033
34namespace art {
35
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010036/**
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 */
42class 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 Geoffrayabed4d02014-07-14 15:24:11 +010067static bool IsTypeSupported(Primitive::Type type) {
68 return type != Primitive::kPrimFloat && type != Primitive::kPrimDouble;
69}
70
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010071void HGraphBuilder::InitializeLocals(uint16_t count) {
72 graph_->SetNumberOfVRegs(count);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000073 locals_.SetSize(count);
74 for (int i = 0; i < count; i++) {
75 HLocal* local = new (arena_) HLocal(i);
76 entry_block_->AddInstruction(local);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000077 locals_.Put(i, local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000078 }
79}
80
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010081bool 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 Geoffrayf583e592014-04-07 13:20:42 +010090 int parameter_index = 0;
91
92 if (!dex_compilation_unit_->IsStatic()) {
93 // Add the implicit 'this' argument, not expressed in the signature.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010094 HParameterValue* parameter =
95 new (arena_) HParameterValue(parameter_index++, Primitive::kPrimNot);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +010096 entry_block_->AddInstruction(parameter);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010097 HLocal* local = GetLocalAt(locals_index++);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +010098 entry_block_->AddInstruction(new (arena_) HStoreLocal(local, parameter));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010099 number_of_parameters--;
100 }
101
102 uint32_t pos = 1;
103 for (int i = 0; i < number_of_parameters; i++) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100104 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 Geoffrayf583e592014-04-07 13:20:42 +0100117 }
118 }
119 return true;
120}
121
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000122static bool CanHandleCodeItem(const DexFile::CodeItem& code_item) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000123 if (code_item.tries_size_ > 0) {
124 return false;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000125 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000126 return true;
127}
128
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100129template<typename T>
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100130void HGraphBuilder::If_22t(const Instruction& instruction, uint32_t dex_offset) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000131 int32_t target_offset = instruction.GetTargetOffset();
132 PotentiallyAddSuspendCheck(target_offset, dex_offset);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100133 HInstruction* first = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
134 HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
Dave Allison20dfc792014-06-16 20:44:29 -0700135 T* comparison = new (arena_) T(first, second);
136 current_block_->AddInstruction(comparison);
137 HInstruction* ifinst = new (arena_) HIf(comparison);
138 current_block_->AddInstruction(ifinst);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000139 HBasicBlock* target = FindBlockStartingAt(dex_offset + target_offset);
Dave Allison20dfc792014-06-16 20:44:29 -0700140 DCHECK(target != nullptr);
141 current_block_->AddSuccessor(target);
142 target = FindBlockStartingAt(dex_offset + instruction.SizeInCodeUnits());
143 DCHECK(target != nullptr);
144 current_block_->AddSuccessor(target);
145 current_block_ = nullptr;
146}
147
148template<typename T>
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100149void HGraphBuilder::If_21t(const Instruction& instruction, uint32_t dex_offset) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000150 int32_t target_offset = instruction.GetTargetOffset();
151 PotentiallyAddSuspendCheck(target_offset, dex_offset);
Dave Allison20dfc792014-06-16 20:44:29 -0700152 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
153 T* comparison = new (arena_) T(value, GetIntConstant(0));
154 current_block_->AddInstruction(comparison);
155 HInstruction* ifinst = new (arena_) HIf(comparison);
156 current_block_->AddInstruction(ifinst);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000157 HBasicBlock* target = FindBlockStartingAt(dex_offset + target_offset);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100158 DCHECK(target != nullptr);
159 current_block_->AddSuccessor(target);
160 target = FindBlockStartingAt(dex_offset + instruction.SizeInCodeUnits());
161 DCHECK(target != nullptr);
162 current_block_->AddSuccessor(target);
163 current_block_ = nullptr;
164}
165
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000166HGraph* HGraphBuilder::BuildGraph(const DexFile::CodeItem& code_item) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000167 if (!CanHandleCodeItem(code_item)) {
168 return nullptr;
169 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000170
171 const uint16_t* code_ptr = code_item.insns_;
172 const uint16_t* code_end = code_item.insns_ + code_item.insns_size_in_code_units_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100173 code_start_ = code_ptr;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000174
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000175 // Setup the graph with the entry block and exit block.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000176 graph_ = new (arena_) HGraph(arena_);
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100177 entry_block_ = new (arena_) HBasicBlock(graph_, 0);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000178 graph_->AddBlock(entry_block_);
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100179 exit_block_ = new (arena_) HBasicBlock(graph_, kNoDexPc);
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000180 graph_->SetEntryBlock(entry_block_);
181 graph_->SetExitBlock(exit_block_);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000182
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000183 InitializeLocals(code_item.registers_size_);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100184 graph_->UpdateMaximumNumberOfOutVRegs(code_item.outs_size_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000185
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000186 // To avoid splitting blocks, we compute ahead of time the instructions that
187 // start a new block, and create these blocks.
188 ComputeBranchTargets(code_ptr, code_end);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000189
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100190 if (!InitializeParameters(code_item.ins_size_)) {
191 return nullptr;
192 }
193
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000194 size_t dex_offset = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000195 while (code_ptr < code_end) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000196 // Update the current block if dex_offset starts a new block.
197 MaybeUpdateCurrentBlock(dex_offset);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000198 const Instruction& instruction = *Instruction::At(code_ptr);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000199 if (!AnalyzeDexInstruction(instruction, dex_offset)) return nullptr;
200 dex_offset += instruction.SizeInCodeUnits();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000201 code_ptr += instruction.SizeInCodeUnits();
202 }
203
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000204 // Add the exit block at the end to give it the highest id.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000205 graph_->AddBlock(exit_block_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000206 exit_block_->AddInstruction(new (arena_) HExit());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000207 // Add the suspend check to the entry block.
208 entry_block_->AddInstruction(new (arena_) HSuspendCheck(0));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000209 entry_block_->AddInstruction(new (arena_) HGoto());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000210 return graph_;
211}
212
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000213void HGraphBuilder::MaybeUpdateCurrentBlock(size_t index) {
214 HBasicBlock* block = FindBlockStartingAt(index);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000215 if (block == nullptr) {
216 return;
217 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000218
219 if (current_block_ != nullptr) {
220 // Branching instructions clear current_block, so we know
221 // the last instruction of the current block is not a branching
222 // instruction. We add an unconditional goto to the found block.
223 current_block_->AddInstruction(new (arena_) HGoto());
224 current_block_->AddSuccessor(block);
225 }
226 graph_->AddBlock(block);
227 current_block_ = block;
228}
229
230void HGraphBuilder::ComputeBranchTargets(const uint16_t* code_ptr, const uint16_t* code_end) {
231 // TODO: Support switch instructions.
232 branch_targets_.SetSize(code_end - code_ptr);
233
234 // Create the first block for the dex instructions, single successor of the entry block.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100235 HBasicBlock* block = new (arena_) HBasicBlock(graph_, 0);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000236 branch_targets_.Put(0, block);
237 entry_block_->AddSuccessor(block);
238
239 // Iterate over all instructions and find branching instructions. Create blocks for
240 // the locations these instructions branch to.
241 size_t dex_offset = 0;
242 while (code_ptr < code_end) {
243 const Instruction& instruction = *Instruction::At(code_ptr);
244 if (instruction.IsBranch()) {
245 int32_t target = instruction.GetTargetOffset() + dex_offset;
246 // Create a block for the target instruction.
247 if (FindBlockStartingAt(target) == nullptr) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100248 block = new (arena_) HBasicBlock(graph_, target);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000249 branch_targets_.Put(target, block);
250 }
251 dex_offset += instruction.SizeInCodeUnits();
252 code_ptr += instruction.SizeInCodeUnits();
253 if ((code_ptr < code_end) && (FindBlockStartingAt(dex_offset) == nullptr)) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100254 block = new (arena_) HBasicBlock(graph_, dex_offset);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000255 branch_targets_.Put(dex_offset, block);
256 }
257 } else {
258 code_ptr += instruction.SizeInCodeUnits();
259 dex_offset += instruction.SizeInCodeUnits();
260 }
261 }
262}
263
264HBasicBlock* HGraphBuilder::FindBlockStartingAt(int32_t index) const {
265 DCHECK_GE(index, 0);
266 return branch_targets_.Get(index);
267}
268
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100269template<typename T>
Roland Levillain88cb1752014-10-20 16:36:47 +0100270void HGraphBuilder::Unop_12x(const Instruction& instruction, Primitive::Type type) {
271 HInstruction* first = LoadLocal(instruction.VRegB(), type);
272 current_block_->AddInstruction(new (arena_) T(type, first));
273 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
274}
275
Roland Levillaindff1f282014-11-05 14:15:05 +0000276void HGraphBuilder::Conversion_12x(const Instruction& instruction,
277 Primitive::Type input_type,
278 Primitive::Type result_type) {
279 HInstruction* first = LoadLocal(instruction.VRegB(), input_type);
280 current_block_->AddInstruction(new (arena_) HTypeConversion(result_type, first));
281 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
282}
283
Roland Levillain88cb1752014-10-20 16:36:47 +0100284template<typename T>
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100285void HGraphBuilder::Binop_23x(const Instruction& instruction, Primitive::Type type) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100286 HInstruction* first = LoadLocal(instruction.VRegB(), type);
287 HInstruction* second = LoadLocal(instruction.VRegC(), type);
288 current_block_->AddInstruction(new (arena_) T(type, first, second));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100289 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
290}
291
292template<typename T>
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100293void HGraphBuilder::Binop_12x(const Instruction& instruction, Primitive::Type type) {
294 HInstruction* first = LoadLocal(instruction.VRegA(), type);
295 HInstruction* second = LoadLocal(instruction.VRegB(), type);
296 current_block_->AddInstruction(new (arena_) T(type, first, second));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100297 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
298}
299
300template<typename T>
301void HGraphBuilder::Binop_22s(const Instruction& instruction, bool reverse) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100302 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
303 HInstruction* second = GetIntConstant(instruction.VRegC_22s());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100304 if (reverse) {
305 std::swap(first, second);
306 }
307 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second));
308 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
309}
310
311template<typename T>
312void HGraphBuilder::Binop_22b(const Instruction& instruction, bool reverse) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100313 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
314 HInstruction* second = GetIntConstant(instruction.VRegC_22b());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100315 if (reverse) {
316 std::swap(first, second);
317 }
318 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second));
319 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
320}
321
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100322void HGraphBuilder::BuildReturn(const Instruction& instruction, Primitive::Type type) {
323 if (type == Primitive::kPrimVoid) {
324 current_block_->AddInstruction(new (arena_) HReturnVoid());
325 } else {
326 HInstruction* value = LoadLocal(instruction.VRegA(), type);
327 current_block_->AddInstruction(new (arena_) HReturn(value));
328 }
329 current_block_->AddSuccessor(exit_block_);
330 current_block_ = nullptr;
331}
332
333bool HGraphBuilder::BuildInvoke(const Instruction& instruction,
334 uint32_t dex_offset,
335 uint32_t method_idx,
336 uint32_t number_of_vreg_arguments,
337 bool is_range,
338 uint32_t* args,
339 uint32_t register_index) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100340 Instruction::Code opcode = instruction.Opcode();
341 InvokeType invoke_type;
342 switch (opcode) {
343 case Instruction::INVOKE_STATIC:
344 case Instruction::INVOKE_STATIC_RANGE:
345 invoke_type = kStatic;
346 break;
347 case Instruction::INVOKE_DIRECT:
348 case Instruction::INVOKE_DIRECT_RANGE:
349 invoke_type = kDirect;
350 break;
351 case Instruction::INVOKE_VIRTUAL:
352 case Instruction::INVOKE_VIRTUAL_RANGE:
353 invoke_type = kVirtual;
354 break;
355 case Instruction::INVOKE_INTERFACE:
356 case Instruction::INVOKE_INTERFACE_RANGE:
357 invoke_type = kInterface;
358 break;
359 case Instruction::INVOKE_SUPER_RANGE:
360 case Instruction::INVOKE_SUPER:
361 invoke_type = kSuper;
362 break;
363 default:
364 LOG(FATAL) << "Unexpected invoke op: " << opcode;
365 return false;
366 }
367
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100368 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
369 const DexFile::ProtoId& proto_id = dex_file_->GetProtoId(method_id.proto_idx_);
370 const char* descriptor = dex_file_->StringDataByIdx(proto_id.shorty_idx_);
371 Primitive::Type return_type = Primitive::GetType(descriptor[0]);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100372 bool is_instance_call = invoke_type != kStatic;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100373 const size_t number_of_arguments = strlen(descriptor) - (is_instance_call ? 0 : 1);
374
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100375 HInvoke* invoke = nullptr;
376 if (invoke_type == kVirtual) {
377 MethodReference target_method(dex_file_, method_idx);
378 uintptr_t direct_code;
379 uintptr_t direct_method;
380 int vtable_index;
381 // TODO: Add devirtualization support.
382 compiler_driver_->ComputeInvokeInfo(dex_compilation_unit_, dex_offset, true, true,
383 &invoke_type, &target_method, &vtable_index,
384 &direct_code, &direct_method);
385 if (vtable_index == -1) {
386 return false;
387 }
388 invoke = new (arena_) HInvokeVirtual(
389 arena_, number_of_arguments, return_type, dex_offset, vtable_index);
390 } else {
391 // Treat invoke-direct like static calls for now.
392 invoke = new (arena_) HInvokeStatic(
393 arena_, number_of_arguments, return_type, dex_offset, method_idx);
394 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100395
396 size_t start_index = 0;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100397 Temporaries temps(graph_, is_instance_call ? 1 : 0);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100398 if (is_instance_call) {
399 HInstruction* arg = LoadLocal(is_range ? register_index : args[0], Primitive::kPrimNot);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100400 HNullCheck* null_check = new (arena_) HNullCheck(arg, dex_offset);
401 current_block_->AddInstruction(null_check);
402 temps.Add(null_check);
403 invoke->SetArgumentAt(0, null_check);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100404 start_index = 1;
405 }
406
407 uint32_t descriptor_index = 1;
408 uint32_t argument_index = start_index;
409 for (size_t i = start_index; i < number_of_vreg_arguments; i++, argument_index++) {
410 Primitive::Type type = Primitive::GetType(descriptor[descriptor_index++]);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100411 bool is_wide = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble);
412 if (!is_range && is_wide && args[i] + 1 != args[i + 1]) {
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100413 LOG(WARNING) << "Non sequential register pair in " << dex_compilation_unit_->GetSymbol()
414 << " at " << dex_offset;
415 // We do not implement non sequential register pair.
416 return false;
417 }
418 HInstruction* arg = LoadLocal(is_range ? register_index + i : args[i], type);
419 invoke->SetArgumentAt(argument_index, arg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100420 if (is_wide) {
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100421 i++;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100422 }
423 }
424
425 DCHECK_EQ(argument_index, number_of_arguments);
426 current_block_->AddInstruction(invoke);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100427 latest_result_ = invoke;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100428 return true;
429}
430
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100431bool HGraphBuilder::BuildInstanceFieldAccess(const Instruction& instruction,
432 uint32_t dex_offset,
433 bool is_put) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100434 uint32_t source_or_dest_reg = instruction.VRegA_22c();
435 uint32_t obj_reg = instruction.VRegB_22c();
436 uint16_t field_index = instruction.VRegC_22c();
437
438 ScopedObjectAccess soa(Thread::Current());
439 StackHandleScope<1> hs(soa.Self());
440 Handle<mirror::ArtField> resolved_field(hs.NewHandle(
441 compiler_driver_->ComputeInstanceFieldInfo(field_index, dex_compilation_unit_, is_put, soa)));
442
443 if (resolved_field.Get() == nullptr) {
444 return false;
445 }
446 if (resolved_field->IsVolatile()) {
447 return false;
448 }
449
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100450 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
451 if (!IsTypeSupported(field_type)) {
452 return false;
453 }
454
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100455 HInstruction* object = LoadLocal(obj_reg, Primitive::kPrimNot);
456 current_block_->AddInstruction(new (arena_) HNullCheck(object, dex_offset));
457 if (is_put) {
458 Temporaries temps(graph_, 1);
459 HInstruction* null_check = current_block_->GetLastInstruction();
460 // We need one temporary for the null check.
461 temps.Add(null_check);
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100462 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100463 current_block_->AddInstruction(new (arena_) HInstanceFieldSet(
464 null_check,
465 value,
Nicolas Geoffray39468442014-09-02 15:17:15 +0100466 field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100467 resolved_field->GetOffset()));
468 } else {
469 current_block_->AddInstruction(new (arena_) HInstanceFieldGet(
470 current_block_->GetLastInstruction(),
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100471 field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100472 resolved_field->GetOffset()));
473
474 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
475 }
476 return true;
477}
478
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100479
480
481bool HGraphBuilder::BuildStaticFieldAccess(const Instruction& instruction,
482 uint32_t dex_offset,
483 bool is_put) {
484 uint32_t source_or_dest_reg = instruction.VRegA_21c();
485 uint16_t field_index = instruction.VRegB_21c();
486
487 uint32_t storage_index;
488 bool is_referrers_class;
489 bool is_initialized;
490 bool is_volatile;
491 MemberOffset field_offset(0u);
492 Primitive::Type field_type;
493
494 bool fast_path = compiler_driver_->ComputeStaticFieldInfo(field_index,
495 dex_compilation_unit_,
496 is_put,
497 &field_offset,
498 &storage_index,
499 &is_referrers_class,
500 &is_volatile,
501 &is_initialized,
502 &field_type);
503 if (!fast_path) {
504 return false;
505 }
506
507 if (is_volatile) {
508 return false;
509 }
510
511 if (!IsTypeSupported(field_type)) {
512 return false;
513 }
514
515 HLoadClass* constant = new (arena_) HLoadClass(
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000516 storage_index, is_referrers_class, dex_offset);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100517 current_block_->AddInstruction(constant);
518
519 HInstruction* cls = constant;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000520 if (!is_initialized) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100521 cls = new (arena_) HClinitCheck(constant, dex_offset);
522 current_block_->AddInstruction(cls);
523 }
524
525 if (is_put) {
526 // We need to keep the class alive before loading the value.
527 Temporaries temps(graph_, 1);
528 temps.Add(cls);
529 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
530 DCHECK_EQ(value->GetType(), field_type);
531 current_block_->AddInstruction(
532 new (arena_) HStaticFieldSet(cls, value, field_type, field_offset));
533 } else {
534 current_block_->AddInstruction(new (arena_) HStaticFieldGet(cls, field_type, field_offset));
535 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
536 }
537 return true;
538}
539
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100540void HGraphBuilder::BuildArrayAccess(const Instruction& instruction,
541 uint32_t dex_offset,
542 bool is_put,
543 Primitive::Type anticipated_type) {
544 uint8_t source_or_dest_reg = instruction.VRegA_23x();
545 uint8_t array_reg = instruction.VRegB_23x();
546 uint8_t index_reg = instruction.VRegC_23x();
547
548 DCHECK(IsTypeSupported(anticipated_type));
549
550 // We need one temporary for the null check, one for the index, and one for the length.
551 Temporaries temps(graph_, 3);
552
553 HInstruction* object = LoadLocal(array_reg, Primitive::kPrimNot);
554 object = new (arena_) HNullCheck(object, dex_offset);
555 current_block_->AddInstruction(object);
556 temps.Add(object);
557
558 HInstruction* length = new (arena_) HArrayLength(object);
559 current_block_->AddInstruction(length);
560 temps.Add(length);
561 HInstruction* index = LoadLocal(index_reg, Primitive::kPrimInt);
562 index = new (arena_) HBoundsCheck(index, length, dex_offset);
563 current_block_->AddInstruction(index);
564 temps.Add(index);
565 if (is_put) {
566 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type);
567 // TODO: Insert a type check node if the type is Object.
Nicolas Geoffray39468442014-09-02 15:17:15 +0100568 current_block_->AddInstruction(new (arena_) HArraySet(
569 object, index, value, anticipated_type, dex_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100570 } else {
571 current_block_->AddInstruction(new (arena_) HArrayGet(object, index, anticipated_type));
572 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
573 }
574}
575
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100576void HGraphBuilder::BuildFilledNewArray(uint32_t dex_offset,
577 uint32_t type_index,
578 uint32_t number_of_vreg_arguments,
579 bool is_range,
580 uint32_t* args,
581 uint32_t register_index) {
582 HInstruction* length = GetIntConstant(number_of_vreg_arguments);
583 HInstruction* object = new (arena_) HNewArray(length, dex_offset, type_index);
584 current_block_->AddInstruction(object);
585
586 const char* descriptor = dex_file_->StringByTypeIdx(type_index);
587 DCHECK_EQ(descriptor[0], '[') << descriptor;
588 char primitive = descriptor[1];
589 DCHECK(primitive == 'I'
590 || primitive == 'L'
591 || primitive == '[') << descriptor;
592 bool is_reference_array = (primitive == 'L') || (primitive == '[');
593 Primitive::Type type = is_reference_array ? Primitive::kPrimNot : Primitive::kPrimInt;
594
595 Temporaries temps(graph_, 1);
596 temps.Add(object);
597 for (size_t i = 0; i < number_of_vreg_arguments; ++i) {
598 HInstruction* value = LoadLocal(is_range ? register_index + i : args[i], type);
599 HInstruction* index = GetIntConstant(i);
600 current_block_->AddInstruction(
601 new (arena_) HArraySet(object, index, value, type, dex_offset));
602 }
603 latest_result_ = object;
604}
605
606template <typename T>
607void HGraphBuilder::BuildFillArrayData(HInstruction* object,
608 const T* data,
609 uint32_t element_count,
610 Primitive::Type anticipated_type,
611 uint32_t dex_offset) {
612 for (uint32_t i = 0; i < element_count; ++i) {
613 HInstruction* index = GetIntConstant(i);
614 HInstruction* value = GetIntConstant(data[i]);
615 current_block_->AddInstruction(new (arena_) HArraySet(
616 object, index, value, anticipated_type, dex_offset));
617 }
618}
619
620void HGraphBuilder::BuildFillWideArrayData(HInstruction* object,
Nicolas Geoffray8d6ae522014-10-23 18:32:13 +0100621 const int64_t* data,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100622 uint32_t element_count,
623 uint32_t dex_offset) {
624 for (uint32_t i = 0; i < element_count; ++i) {
625 HInstruction* index = GetIntConstant(i);
626 HInstruction* value = GetLongConstant(data[i]);
627 current_block_->AddInstruction(new (arena_) HArraySet(
628 object, index, value, Primitive::kPrimLong, dex_offset));
629 }
630}
631
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000632void HGraphBuilder::PotentiallyAddSuspendCheck(int32_t target_offset, uint32_t dex_offset) {
633 if (target_offset <= 0) {
634 // Unconditionnally add a suspend check to backward branches. We can remove
635 // them after we recognize loops in the graph.
636 current_block_->AddInstruction(new (arena_) HSuspendCheck(dex_offset));
637 }
638}
639
640bool HGraphBuilder::AnalyzeDexInstruction(const Instruction& instruction, uint32_t dex_offset) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000641 if (current_block_ == nullptr) {
642 return true; // Dead code
643 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000644
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000645 switch (instruction.Opcode()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000646 case Instruction::CONST_4: {
647 int32_t register_index = instruction.VRegA();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100648 HIntConstant* constant = GetIntConstant(instruction.VRegB_11n());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000649 UpdateLocal(register_index, constant);
650 break;
651 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000652
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100653 case Instruction::CONST_16: {
654 int32_t register_index = instruction.VRegA();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100655 HIntConstant* constant = GetIntConstant(instruction.VRegB_21s());
656 UpdateLocal(register_index, constant);
657 break;
658 }
659
Dave Allison20dfc792014-06-16 20:44:29 -0700660 case Instruction::CONST: {
661 int32_t register_index = instruction.VRegA();
662 HIntConstant* constant = GetIntConstant(instruction.VRegB_31i());
663 UpdateLocal(register_index, constant);
664 break;
665 }
666
667 case Instruction::CONST_HIGH16: {
668 int32_t register_index = instruction.VRegA();
669 HIntConstant* constant = GetIntConstant(instruction.VRegB_21h() << 16);
670 UpdateLocal(register_index, constant);
671 break;
672 }
673
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100674 case Instruction::CONST_WIDE_16: {
675 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -0700676 // Get 16 bits of constant value, sign extended to 64 bits.
677 int64_t value = instruction.VRegB_21s();
678 value <<= 48;
679 value >>= 48;
680 HLongConstant* constant = GetLongConstant(value);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100681 UpdateLocal(register_index, constant);
682 break;
683 }
684
685 case Instruction::CONST_WIDE_32: {
686 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -0700687 // Get 32 bits of constant value, sign extended to 64 bits.
688 int64_t value = instruction.VRegB_31i();
689 value <<= 32;
690 value >>= 32;
691 HLongConstant* constant = GetLongConstant(value);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100692 UpdateLocal(register_index, constant);
693 break;
694 }
695
696 case Instruction::CONST_WIDE: {
697 int32_t register_index = instruction.VRegA();
698 HLongConstant* constant = GetLongConstant(instruction.VRegB_51l());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100699 UpdateLocal(register_index, constant);
700 break;
701 }
702
Dave Allison20dfc792014-06-16 20:44:29 -0700703 case Instruction::CONST_WIDE_HIGH16: {
704 int32_t register_index = instruction.VRegA();
705 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
706 HLongConstant* constant = GetLongConstant(value);
707 UpdateLocal(register_index, constant);
708 break;
709 }
710
711 // TODO: these instructions are also used to move floating point values, so what is
712 // the type (int or float)?
713 case Instruction::MOVE:
714 case Instruction::MOVE_FROM16:
715 case Instruction::MOVE_16: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100716 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100717 UpdateLocal(instruction.VRegA(), value);
718 break;
719 }
720
Dave Allison20dfc792014-06-16 20:44:29 -0700721 // TODO: these instructions are also used to move floating point values, so what is
722 // the type (long or double)?
723 case Instruction::MOVE_WIDE:
724 case Instruction::MOVE_WIDE_FROM16:
725 case Instruction::MOVE_WIDE_16: {
726 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimLong);
727 UpdateLocal(instruction.VRegA(), value);
728 break;
729 }
730
731 case Instruction::MOVE_OBJECT:
732 case Instruction::MOVE_OBJECT_16:
733 case Instruction::MOVE_OBJECT_FROM16: {
734 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimNot);
735 UpdateLocal(instruction.VRegA(), value);
736 break;
737 }
738
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000739 case Instruction::RETURN_VOID: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100740 BuildReturn(instruction, Primitive::kPrimVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000741 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000742 }
743
Dave Allison20dfc792014-06-16 20:44:29 -0700744#define IF_XX(comparison, cond) \
745 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_offset); break; \
746 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_offset); break
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100747
Dave Allison20dfc792014-06-16 20:44:29 -0700748 IF_XX(HEqual, EQ);
749 IF_XX(HNotEqual, NE);
750 IF_XX(HLessThan, LT);
751 IF_XX(HLessThanOrEqual, LE);
752 IF_XX(HGreaterThan, GT);
753 IF_XX(HGreaterThanOrEqual, GE);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000754
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000755 case Instruction::GOTO:
756 case Instruction::GOTO_16:
757 case Instruction::GOTO_32: {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000758 int32_t offset = instruction.GetTargetOffset();
759 PotentiallyAddSuspendCheck(offset, dex_offset);
760 HBasicBlock* target = FindBlockStartingAt(offset + dex_offset);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000761 DCHECK(target != nullptr);
762 current_block_->AddInstruction(new (arena_) HGoto());
763 current_block_->AddSuccessor(target);
764 current_block_ = nullptr;
765 break;
766 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000767
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100768 case Instruction::RETURN: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100769 DCHECK_NE(return_type_, Primitive::kPrimNot);
770 DCHECK_NE(return_type_, Primitive::kPrimLong);
771 DCHECK_NE(return_type_, Primitive::kPrimDouble);
772 BuildReturn(instruction, return_type_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100773 break;
774 }
775
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100776 case Instruction::RETURN_OBJECT: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100777 DCHECK(return_type_ == Primitive::kPrimNot);
778 BuildReturn(instruction, return_type_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100779 break;
780 }
781
782 case Instruction::RETURN_WIDE: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100783 DCHECK(return_type_ == Primitive::kPrimDouble || return_type_ == Primitive::kPrimLong);
784 BuildReturn(instruction, return_type_);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000785 break;
786 }
787
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100788 case Instruction::INVOKE_STATIC:
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100789 case Instruction::INVOKE_DIRECT:
790 case Instruction::INVOKE_VIRTUAL: {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000791 uint32_t method_idx = instruction.VRegB_35c();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100792 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100793 uint32_t args[5];
Ian Rogers29a26482014-05-02 15:27:29 -0700794 instruction.GetVarArgs(args);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100795 if (!BuildInvoke(instruction, dex_offset, method_idx, number_of_vreg_arguments, false, args, -1)) {
796 return false;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100797 }
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100798 break;
799 }
800
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100801 case Instruction::INVOKE_STATIC_RANGE:
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100802 case Instruction::INVOKE_DIRECT_RANGE:
803 case Instruction::INVOKE_VIRTUAL_RANGE: {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100804 uint32_t method_idx = instruction.VRegB_3rc();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100805 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
806 uint32_t register_index = instruction.VRegC();
807 if (!BuildInvoke(instruction, dex_offset, method_idx,
808 number_of_vreg_arguments, true, nullptr, register_index)) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100809 return false;
810 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000811 break;
812 }
813
Roland Levillain88cb1752014-10-20 16:36:47 +0100814 case Instruction::NEG_INT: {
815 Unop_12x<HNeg>(instruction, Primitive::kPrimInt);
816 break;
817 }
818
Roland Levillain2e07b4f2014-10-23 18:12:09 +0100819 case Instruction::NEG_LONG: {
820 Unop_12x<HNeg>(instruction, Primitive::kPrimLong);
821 break;
822 }
823
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100824 case Instruction::NOT_INT: {
825 Unop_12x<HNot>(instruction, Primitive::kPrimInt);
826 break;
827 }
828
Roland Levillain70566432014-10-24 16:20:17 +0100829 case Instruction::NOT_LONG: {
830 Unop_12x<HNot>(instruction, Primitive::kPrimLong);
831 break;
832 }
833
Roland Levillaindff1f282014-11-05 14:15:05 +0000834 case Instruction::INT_TO_LONG: {
835 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimLong);
836 break;
837 }
838
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000839 case Instruction::ADD_INT: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100840 Binop_23x<HAdd>(instruction, Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100841 break;
842 }
843
844 case Instruction::ADD_LONG: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100845 Binop_23x<HAdd>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100846 break;
847 }
848
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100849 case Instruction::ADD_DOUBLE: {
850 Binop_23x<HAdd>(instruction, Primitive::kPrimDouble);
851 break;
852 }
853
854 case Instruction::ADD_FLOAT: {
855 Binop_23x<HAdd>(instruction, Primitive::kPrimFloat);
856 break;
857 }
858
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100859 case Instruction::SUB_INT: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100860 Binop_23x<HSub>(instruction, Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100861 break;
862 }
863
864 case Instruction::SUB_LONG: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100865 Binop_23x<HSub>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000866 break;
867 }
868
Calin Juravle096cc022014-10-23 17:01:13 +0100869 case Instruction::SUB_FLOAT: {
870 Binop_23x<HSub>(instruction, Primitive::kPrimFloat);
871 break;
872 }
873
874 case Instruction::SUB_DOUBLE: {
875 Binop_23x<HSub>(instruction, Primitive::kPrimDouble);
876 break;
877 }
878
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000879 case Instruction::ADD_INT_2ADDR: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100880 Binop_12x<HAdd>(instruction, Primitive::kPrimInt);
881 break;
882 }
883
Calin Juravle34bacdf2014-10-07 20:23:36 +0100884 case Instruction::MUL_INT: {
885 Binop_23x<HMul>(instruction, Primitive::kPrimInt);
886 break;
887 }
888
889 case Instruction::MUL_LONG: {
890 Binop_23x<HMul>(instruction, Primitive::kPrimLong);
891 break;
892 }
893
Calin Juravleb5bfa962014-10-21 18:02:24 +0100894 case Instruction::MUL_FLOAT: {
895 Binop_23x<HMul>(instruction, Primitive::kPrimFloat);
896 break;
897 }
898
899 case Instruction::MUL_DOUBLE: {
900 Binop_23x<HMul>(instruction, Primitive::kPrimDouble);
901 break;
902 }
903
Calin Juravle7c4954d2014-10-28 16:57:40 +0000904 case Instruction::DIV_FLOAT: {
905 Binop_23x<HDiv>(instruction, Primitive::kPrimFloat);
906 break;
907 }
908
909 case Instruction::DIV_DOUBLE: {
910 Binop_23x<HDiv>(instruction, Primitive::kPrimDouble);
911 break;
912 }
913
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100914 case Instruction::ADD_LONG_2ADDR: {
915 Binop_12x<HAdd>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100916 break;
917 }
918
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100919 case Instruction::ADD_DOUBLE_2ADDR: {
920 Binop_12x<HAdd>(instruction, Primitive::kPrimDouble);
921 break;
922 }
923
924 case Instruction::ADD_FLOAT_2ADDR: {
925 Binop_12x<HAdd>(instruction, Primitive::kPrimFloat);
926 break;
927 }
928
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100929 case Instruction::SUB_INT_2ADDR: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100930 Binop_12x<HSub>(instruction, Primitive::kPrimInt);
931 break;
932 }
933
934 case Instruction::SUB_LONG_2ADDR: {
935 Binop_12x<HSub>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000936 break;
937 }
938
Calin Juravle096cc022014-10-23 17:01:13 +0100939 case Instruction::SUB_FLOAT_2ADDR: {
940 Binop_12x<HSub>(instruction, Primitive::kPrimFloat);
941 break;
942 }
943
944 case Instruction::SUB_DOUBLE_2ADDR: {
945 Binop_12x<HSub>(instruction, Primitive::kPrimDouble);
946 break;
947 }
948
Calin Juravle34bacdf2014-10-07 20:23:36 +0100949 case Instruction::MUL_INT_2ADDR: {
950 Binop_12x<HMul>(instruction, Primitive::kPrimInt);
951 break;
952 }
953
954 case Instruction::MUL_LONG_2ADDR: {
955 Binop_12x<HMul>(instruction, Primitive::kPrimLong);
956 break;
957 }
958
Calin Juravleb5bfa962014-10-21 18:02:24 +0100959 case Instruction::MUL_FLOAT_2ADDR: {
960 Binop_12x<HMul>(instruction, Primitive::kPrimFloat);
961 break;
962 }
963
964 case Instruction::MUL_DOUBLE_2ADDR: {
965 Binop_12x<HMul>(instruction, Primitive::kPrimDouble);
966 break;
967 }
968
Calin Juravle7c4954d2014-10-28 16:57:40 +0000969 case Instruction::DIV_FLOAT_2ADDR: {
970 Binop_12x<HDiv>(instruction, Primitive::kPrimFloat);
971 break;
972 }
973
974 case Instruction::DIV_DOUBLE_2ADDR: {
975 Binop_12x<HDiv>(instruction, Primitive::kPrimDouble);
976 break;
977 }
978
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000979 case Instruction::ADD_INT_LIT16: {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100980 Binop_22s<HAdd>(instruction, false);
981 break;
982 }
983
984 case Instruction::RSUB_INT: {
985 Binop_22s<HSub>(instruction, true);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000986 break;
987 }
988
Calin Juravle34bacdf2014-10-07 20:23:36 +0100989 case Instruction::MUL_INT_LIT16: {
990 Binop_22s<HMul>(instruction, false);
991 break;
992 }
993
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000994 case Instruction::ADD_INT_LIT8: {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100995 Binop_22b<HAdd>(instruction, false);
996 break;
997 }
998
999 case Instruction::RSUB_INT_LIT8: {
1000 Binop_22b<HSub>(instruction, true);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001001 break;
1002 }
1003
Calin Juravle34bacdf2014-10-07 20:23:36 +01001004 case Instruction::MUL_INT_LIT8: {
1005 Binop_22b<HMul>(instruction, false);
1006 break;
1007 }
1008
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001009 case Instruction::NEW_INSTANCE: {
1010 current_block_->AddInstruction(
1011 new (arena_) HNewInstance(dex_offset, instruction.VRegB_21c()));
1012 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
1013 break;
1014 }
1015
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001016 case Instruction::NEW_ARRAY: {
1017 HInstruction* length = LoadLocal(instruction.VRegB_22c(), Primitive::kPrimInt);
1018 current_block_->AddInstruction(
1019 new (arena_) HNewArray(length, dex_offset, instruction.VRegC_22c()));
1020 UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction());
1021 break;
1022 }
1023
1024 case Instruction::FILLED_NEW_ARRAY: {
1025 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
1026 uint32_t type_index = instruction.VRegB_35c();
1027 uint32_t args[5];
1028 instruction.GetVarArgs(args);
1029 BuildFilledNewArray(dex_offset, type_index, number_of_vreg_arguments, false, args, 0);
1030 break;
1031 }
1032
1033 case Instruction::FILLED_NEW_ARRAY_RANGE: {
1034 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
1035 uint32_t type_index = instruction.VRegB_3rc();
1036 uint32_t register_index = instruction.VRegC_3rc();
1037 BuildFilledNewArray(
1038 dex_offset, type_index, number_of_vreg_arguments, true, nullptr, register_index);
1039 break;
1040 }
1041
1042 case Instruction::FILL_ARRAY_DATA: {
1043 Temporaries temps(graph_, 1);
1044 HInstruction* array = LoadLocal(instruction.VRegA_31t(), Primitive::kPrimNot);
1045 HNullCheck* null_check = new (arena_) HNullCheck(array, dex_offset);
1046 current_block_->AddInstruction(null_check);
1047 temps.Add(null_check);
1048
1049 HInstruction* length = new (arena_) HArrayLength(null_check);
1050 current_block_->AddInstruction(length);
1051
1052 int32_t payload_offset = instruction.VRegB_31t() + dex_offset;
1053 const Instruction::ArrayDataPayload* payload =
1054 reinterpret_cast<const Instruction::ArrayDataPayload*>(code_start_ + payload_offset);
1055 const uint8_t* data = payload->data;
1056 uint32_t element_count = payload->element_count;
1057
1058 // Implementation of this DEX instruction seems to be that the bounds check is
1059 // done before doing any stores.
1060 HInstruction* last_index = GetIntConstant(payload->element_count - 1);
1061 current_block_->AddInstruction(new (arena_) HBoundsCheck(last_index, length, dex_offset));
1062
1063 switch (payload->element_width) {
1064 case 1:
Nicolas Geoffray8d6ae522014-10-23 18:32:13 +01001065 BuildFillArrayData(null_check,
1066 reinterpret_cast<const int8_t*>(data),
1067 element_count,
1068 Primitive::kPrimByte,
1069 dex_offset);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001070 break;
1071 case 2:
1072 BuildFillArrayData(null_check,
Nicolas Geoffray8d6ae522014-10-23 18:32:13 +01001073 reinterpret_cast<const int16_t*>(data),
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001074 element_count,
1075 Primitive::kPrimShort,
1076 dex_offset);
1077 break;
1078 case 4:
1079 BuildFillArrayData(null_check,
Nicolas Geoffray8d6ae522014-10-23 18:32:13 +01001080 reinterpret_cast<const int32_t*>(data),
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001081 element_count,
1082 Primitive::kPrimInt,
1083 dex_offset);
1084 break;
1085 case 8:
1086 BuildFillWideArrayData(null_check,
Nicolas Geoffray8d6ae522014-10-23 18:32:13 +01001087 reinterpret_cast<const int64_t*>(data),
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001088 element_count,
1089 dex_offset);
1090 break;
1091 default:
1092 LOG(FATAL) << "Unknown element width for " << payload->element_width;
1093 }
1094 break;
1095 }
1096
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001097 case Instruction::MOVE_RESULT:
Dave Allison20dfc792014-06-16 20:44:29 -07001098 case Instruction::MOVE_RESULT_WIDE:
1099 case Instruction::MOVE_RESULT_OBJECT:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001100 UpdateLocal(instruction.VRegA(), latest_result_);
1101 latest_result_ = nullptr;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001102 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001103
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001104 case Instruction::CMP_LONG: {
1105 Binop_23x<HCompare>(instruction, Primitive::kPrimLong);
1106 break;
1107 }
1108
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001109 case Instruction::NOP:
1110 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001111
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001112 case Instruction::IGET:
1113 case Instruction::IGET_WIDE:
1114 case Instruction::IGET_OBJECT:
1115 case Instruction::IGET_BOOLEAN:
1116 case Instruction::IGET_BYTE:
1117 case Instruction::IGET_CHAR:
1118 case Instruction::IGET_SHORT: {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001119 if (!BuildInstanceFieldAccess(instruction, dex_offset, false)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001120 return false;
1121 }
1122 break;
1123 }
1124
1125 case Instruction::IPUT:
1126 case Instruction::IPUT_WIDE:
1127 case Instruction::IPUT_OBJECT:
1128 case Instruction::IPUT_BOOLEAN:
1129 case Instruction::IPUT_BYTE:
1130 case Instruction::IPUT_CHAR:
1131 case Instruction::IPUT_SHORT: {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001132 if (!BuildInstanceFieldAccess(instruction, dex_offset, true)) {
1133 return false;
1134 }
1135 break;
1136 }
1137
1138 case Instruction::SGET:
1139 case Instruction::SGET_WIDE:
1140 case Instruction::SGET_OBJECT:
1141 case Instruction::SGET_BOOLEAN:
1142 case Instruction::SGET_BYTE:
1143 case Instruction::SGET_CHAR:
1144 case Instruction::SGET_SHORT: {
1145 if (!BuildStaticFieldAccess(instruction, dex_offset, false)) {
1146 return false;
1147 }
1148 break;
1149 }
1150
1151 case Instruction::SPUT:
1152 case Instruction::SPUT_WIDE:
1153 case Instruction::SPUT_OBJECT:
1154 case Instruction::SPUT_BOOLEAN:
1155 case Instruction::SPUT_BYTE:
1156 case Instruction::SPUT_CHAR:
1157 case Instruction::SPUT_SHORT: {
1158 if (!BuildStaticFieldAccess(instruction, dex_offset, true)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001159 return false;
1160 }
1161 break;
1162 }
1163
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001164#define ARRAY_XX(kind, anticipated_type) \
1165 case Instruction::AGET##kind: { \
1166 BuildArrayAccess(instruction, dex_offset, false, anticipated_type); \
1167 break; \
1168 } \
1169 case Instruction::APUT##kind: { \
1170 BuildArrayAccess(instruction, dex_offset, true, anticipated_type); \
1171 break; \
1172 }
1173
1174 ARRAY_XX(, Primitive::kPrimInt);
1175 ARRAY_XX(_WIDE, Primitive::kPrimLong);
1176 ARRAY_XX(_OBJECT, Primitive::kPrimNot);
1177 ARRAY_XX(_BOOLEAN, Primitive::kPrimBoolean);
1178 ARRAY_XX(_BYTE, Primitive::kPrimByte);
1179 ARRAY_XX(_CHAR, Primitive::kPrimChar);
1180 ARRAY_XX(_SHORT, Primitive::kPrimShort);
1181
Nicolas Geoffray39468442014-09-02 15:17:15 +01001182 case Instruction::ARRAY_LENGTH: {
1183 HInstruction* object = LoadLocal(instruction.VRegB_12x(), Primitive::kPrimNot);
1184 current_block_->AddInstruction(new (arena_) HArrayLength(object));
1185 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction());
1186 break;
1187 }
1188
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001189 case Instruction::CONST_STRING: {
1190 current_block_->AddInstruction(new (arena_) HLoadString(instruction.VRegB_21c(), dex_offset));
1191 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
1192 break;
1193 }
1194
1195 case Instruction::CONST_STRING_JUMBO: {
1196 current_block_->AddInstruction(new (arena_) HLoadString(instruction.VRegB_31c(), dex_offset));
1197 UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction());
1198 break;
1199 }
1200
Nicolas Geoffray424f6762014-11-03 14:51:25 +00001201 case Instruction::CONST_CLASS: {
1202 uint16_t type_index = instruction.VRegB_21c();
1203 bool type_known_final;
1204 bool type_known_abstract;
1205 bool is_referrers_class;
1206 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
1207 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index,
1208 &type_known_final, &type_known_abstract, &is_referrers_class);
1209 if (!can_access) {
1210 return false;
1211 }
1212 current_block_->AddInstruction(
1213 new (arena_) HLoadClass(instruction.VRegB_21c(), is_referrers_class, dex_offset));
1214 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
1215 break;
1216 }
1217
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001218 default:
1219 return false;
1220 }
1221 return true;
1222}
1223
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001224HIntConstant* HGraphBuilder::GetIntConstant0() {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001225 if (constant0_ != nullptr) {
1226 return constant0_;
1227 }
1228 constant0_ = new(arena_) HIntConstant(0);
1229 entry_block_->AddInstruction(constant0_);
1230 return constant0_;
1231}
1232
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001233HIntConstant* HGraphBuilder::GetIntConstant1() {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001234 if (constant1_ != nullptr) {
1235 return constant1_;
1236 }
1237 constant1_ = new(arena_) HIntConstant(1);
1238 entry_block_->AddInstruction(constant1_);
1239 return constant1_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001240}
1241
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001242HIntConstant* HGraphBuilder::GetIntConstant(int32_t constant) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001243 switch (constant) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001244 case 0: return GetIntConstant0();
1245 case 1: return GetIntConstant1();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001246 default: {
1247 HIntConstant* instruction = new (arena_) HIntConstant(constant);
1248 entry_block_->AddInstruction(instruction);
1249 return instruction;
1250 }
1251 }
1252}
1253
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001254HLongConstant* HGraphBuilder::GetLongConstant(int64_t constant) {
1255 HLongConstant* instruction = new (arena_) HLongConstant(constant);
1256 entry_block_->AddInstruction(instruction);
1257 return instruction;
1258}
1259
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001260HLocal* HGraphBuilder::GetLocalAt(int register_index) const {
1261 return locals_.Get(register_index);
1262}
1263
1264void HGraphBuilder::UpdateLocal(int register_index, HInstruction* instruction) const {
1265 HLocal* local = GetLocalAt(register_index);
1266 current_block_->AddInstruction(new (arena_) HStoreLocal(local, instruction));
1267}
1268
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001269HInstruction* HGraphBuilder::LoadLocal(int register_index, Primitive::Type type) const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001270 HLocal* local = GetLocalAt(register_index);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001271 current_block_->AddInstruction(new (arena_) HLoadLocal(local, type));
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001272 return current_block_->GetLastInstruction();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001273}
1274
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001275} // namespace art