blob: 8baf3c5f89ad1b492891d465428870a481c65d68 [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 Geoffrayb55f8352014-04-07 15:26:35 +0100122template<typename T>
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100123void HGraphBuilder::If_22t(const Instruction& instruction, uint32_t dex_offset) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000124 int32_t target_offset = instruction.GetTargetOffset();
125 PotentiallyAddSuspendCheck(target_offset, dex_offset);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100126 HInstruction* first = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
127 HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
Dave Allison20dfc792014-06-16 20:44:29 -0700128 T* comparison = new (arena_) T(first, second);
129 current_block_->AddInstruction(comparison);
130 HInstruction* ifinst = new (arena_) HIf(comparison);
131 current_block_->AddInstruction(ifinst);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000132 HBasicBlock* target = FindBlockStartingAt(dex_offset + target_offset);
Dave Allison20dfc792014-06-16 20:44:29 -0700133 DCHECK(target != nullptr);
134 current_block_->AddSuccessor(target);
135 target = FindBlockStartingAt(dex_offset + instruction.SizeInCodeUnits());
136 DCHECK(target != nullptr);
137 current_block_->AddSuccessor(target);
138 current_block_ = nullptr;
139}
140
141template<typename T>
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100142void HGraphBuilder::If_21t(const Instruction& instruction, uint32_t dex_offset) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000143 int32_t target_offset = instruction.GetTargetOffset();
144 PotentiallyAddSuspendCheck(target_offset, dex_offset);
Dave Allison20dfc792014-06-16 20:44:29 -0700145 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
146 T* comparison = new (arena_) T(value, GetIntConstant(0));
147 current_block_->AddInstruction(comparison);
148 HInstruction* ifinst = new (arena_) HIf(comparison);
149 current_block_->AddInstruction(ifinst);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000150 HBasicBlock* target = FindBlockStartingAt(dex_offset + target_offset);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100151 DCHECK(target != nullptr);
152 current_block_->AddSuccessor(target);
153 target = FindBlockStartingAt(dex_offset + instruction.SizeInCodeUnits());
154 DCHECK(target != nullptr);
155 current_block_->AddSuccessor(target);
156 current_block_ = nullptr;
157}
158
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000159HGraph* HGraphBuilder::BuildGraph(const DexFile::CodeItem& code_item) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000160 const uint16_t* code_ptr = code_item.insns_;
161 const uint16_t* code_end = code_item.insns_ + code_item.insns_size_in_code_units_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100162 code_start_ = code_ptr;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000163
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000164 // Setup the graph with the entry block and exit block.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000165 graph_ = new (arena_) HGraph(arena_);
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100166 entry_block_ = new (arena_) HBasicBlock(graph_, 0);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000167 graph_->AddBlock(entry_block_);
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100168 exit_block_ = new (arena_) HBasicBlock(graph_, kNoDexPc);
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000169 graph_->SetEntryBlock(entry_block_);
170 graph_->SetExitBlock(exit_block_);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000171
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000172 InitializeLocals(code_item.registers_size_);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100173 graph_->UpdateMaximumNumberOfOutVRegs(code_item.outs_size_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000174
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000175 // To avoid splitting blocks, we compute ahead of time the instructions that
176 // start a new block, and create these blocks.
177 ComputeBranchTargets(code_ptr, code_end);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000178
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000179 // Also create blocks for catch handlers.
180 if (code_item.tries_size_ != 0) {
181 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(code_item, 0);
182 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
183 for (uint32_t idx = 0; idx < handlers_size; ++idx) {
184 CatchHandlerIterator iterator(handlers_ptr);
185 for (; iterator.HasNext(); iterator.Next()) {
186 uint32_t address = iterator.GetHandlerAddress();
187 HBasicBlock* block = FindBlockStartingAt(address);
188 if (block == nullptr) {
189 block = new (arena_) HBasicBlock(graph_, address);
190 branch_targets_.Put(address, block);
191 }
192 block->SetIsCatchBlock();
193 }
194 handlers_ptr = iterator.EndDataPointer();
195 }
196 }
197
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100198 if (!InitializeParameters(code_item.ins_size_)) {
199 return nullptr;
200 }
201
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000202 size_t dex_offset = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000203 while (code_ptr < code_end) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000204 // Update the current block if dex_offset starts a new block.
205 MaybeUpdateCurrentBlock(dex_offset);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000206 const Instruction& instruction = *Instruction::At(code_ptr);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000207 if (!AnalyzeDexInstruction(instruction, dex_offset)) return nullptr;
208 dex_offset += instruction.SizeInCodeUnits();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000209 code_ptr += instruction.SizeInCodeUnits();
210 }
211
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000212 // Add the exit block at the end to give it the highest id.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000213 graph_->AddBlock(exit_block_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000214 exit_block_->AddInstruction(new (arena_) HExit());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000215 // Add the suspend check to the entry block.
216 entry_block_->AddInstruction(new (arena_) HSuspendCheck(0));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000217 entry_block_->AddInstruction(new (arena_) HGoto());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000218 return graph_;
219}
220
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000221void HGraphBuilder::MaybeUpdateCurrentBlock(size_t index) {
222 HBasicBlock* block = FindBlockStartingAt(index);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000223 if (block == nullptr) {
224 return;
225 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000226
227 if (current_block_ != nullptr) {
228 // Branching instructions clear current_block, so we know
229 // the last instruction of the current block is not a branching
230 // instruction. We add an unconditional goto to the found block.
231 current_block_->AddInstruction(new (arena_) HGoto());
232 current_block_->AddSuccessor(block);
233 }
234 graph_->AddBlock(block);
235 current_block_ = block;
236}
237
238void HGraphBuilder::ComputeBranchTargets(const uint16_t* code_ptr, const uint16_t* code_end) {
239 // TODO: Support switch instructions.
240 branch_targets_.SetSize(code_end - code_ptr);
241
242 // Create the first block for the dex instructions, single successor of the entry block.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100243 HBasicBlock* block = new (arena_) HBasicBlock(graph_, 0);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000244 branch_targets_.Put(0, block);
245 entry_block_->AddSuccessor(block);
246
247 // Iterate over all instructions and find branching instructions. Create blocks for
248 // the locations these instructions branch to.
249 size_t dex_offset = 0;
250 while (code_ptr < code_end) {
251 const Instruction& instruction = *Instruction::At(code_ptr);
252 if (instruction.IsBranch()) {
253 int32_t target = instruction.GetTargetOffset() + dex_offset;
254 // Create a block for the target instruction.
255 if (FindBlockStartingAt(target) == nullptr) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100256 block = new (arena_) HBasicBlock(graph_, target);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000257 branch_targets_.Put(target, block);
258 }
259 dex_offset += instruction.SizeInCodeUnits();
260 code_ptr += instruction.SizeInCodeUnits();
261 if ((code_ptr < code_end) && (FindBlockStartingAt(dex_offset) == nullptr)) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100262 block = new (arena_) HBasicBlock(graph_, dex_offset);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000263 branch_targets_.Put(dex_offset, block);
264 }
265 } else {
266 code_ptr += instruction.SizeInCodeUnits();
267 dex_offset += instruction.SizeInCodeUnits();
268 }
269 }
270}
271
272HBasicBlock* HGraphBuilder::FindBlockStartingAt(int32_t index) const {
273 DCHECK_GE(index, 0);
274 return branch_targets_.Get(index);
275}
276
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100277template<typename T>
Roland Levillain88cb1752014-10-20 16:36:47 +0100278void HGraphBuilder::Unop_12x(const Instruction& instruction, Primitive::Type type) {
279 HInstruction* first = LoadLocal(instruction.VRegB(), type);
280 current_block_->AddInstruction(new (arena_) T(type, first));
281 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
282}
283
Roland Levillaindff1f282014-11-05 14:15:05 +0000284void HGraphBuilder::Conversion_12x(const Instruction& instruction,
285 Primitive::Type input_type,
286 Primitive::Type result_type) {
287 HInstruction* first = LoadLocal(instruction.VRegB(), input_type);
288 current_block_->AddInstruction(new (arena_) HTypeConversion(result_type, first));
289 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
290}
291
Roland Levillain88cb1752014-10-20 16:36:47 +0100292template<typename T>
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100293void HGraphBuilder::Binop_23x(const Instruction& instruction, Primitive::Type type) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100294 HInstruction* first = LoadLocal(instruction.VRegB(), type);
295 HInstruction* second = LoadLocal(instruction.VRegC(), 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>
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100301void HGraphBuilder::Binop_12x(const Instruction& instruction, Primitive::Type type) {
302 HInstruction* first = LoadLocal(instruction.VRegA(), type);
303 HInstruction* second = LoadLocal(instruction.VRegB(), type);
304 current_block_->AddInstruction(new (arena_) T(type, first, second));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100305 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
306}
307
308template<typename T>
309void HGraphBuilder::Binop_22s(const Instruction& instruction, bool reverse) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100310 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
311 HInstruction* second = GetIntConstant(instruction.VRegC_22s());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100312 if (reverse) {
313 std::swap(first, second);
314 }
315 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second));
316 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
317}
318
319template<typename T>
320void HGraphBuilder::Binop_22b(const Instruction& instruction, bool reverse) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100321 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
322 HInstruction* second = GetIntConstant(instruction.VRegC_22b());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100323 if (reverse) {
324 std::swap(first, second);
325 }
326 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second));
327 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
328}
329
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100330void HGraphBuilder::BuildReturn(const Instruction& instruction, Primitive::Type type) {
331 if (type == Primitive::kPrimVoid) {
332 current_block_->AddInstruction(new (arena_) HReturnVoid());
333 } else {
334 HInstruction* value = LoadLocal(instruction.VRegA(), type);
335 current_block_->AddInstruction(new (arena_) HReturn(value));
336 }
337 current_block_->AddSuccessor(exit_block_);
338 current_block_ = nullptr;
339}
340
341bool HGraphBuilder::BuildInvoke(const Instruction& instruction,
342 uint32_t dex_offset,
343 uint32_t method_idx,
344 uint32_t number_of_vreg_arguments,
345 bool is_range,
346 uint32_t* args,
347 uint32_t register_index) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100348 Instruction::Code opcode = instruction.Opcode();
349 InvokeType invoke_type;
350 switch (opcode) {
351 case Instruction::INVOKE_STATIC:
352 case Instruction::INVOKE_STATIC_RANGE:
353 invoke_type = kStatic;
354 break;
355 case Instruction::INVOKE_DIRECT:
356 case Instruction::INVOKE_DIRECT_RANGE:
357 invoke_type = kDirect;
358 break;
359 case Instruction::INVOKE_VIRTUAL:
360 case Instruction::INVOKE_VIRTUAL_RANGE:
361 invoke_type = kVirtual;
362 break;
363 case Instruction::INVOKE_INTERFACE:
364 case Instruction::INVOKE_INTERFACE_RANGE:
365 invoke_type = kInterface;
366 break;
367 case Instruction::INVOKE_SUPER_RANGE:
368 case Instruction::INVOKE_SUPER:
369 invoke_type = kSuper;
370 break;
371 default:
372 LOG(FATAL) << "Unexpected invoke op: " << opcode;
373 return false;
374 }
375
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100376 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
377 const DexFile::ProtoId& proto_id = dex_file_->GetProtoId(method_id.proto_idx_);
378 const char* descriptor = dex_file_->StringDataByIdx(proto_id.shorty_idx_);
379 Primitive::Type return_type = Primitive::GetType(descriptor[0]);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100380 bool is_instance_call = invoke_type != kStatic;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100381 const size_t number_of_arguments = strlen(descriptor) - (is_instance_call ? 0 : 1);
382
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100383 HInvoke* invoke = nullptr;
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000384 if (invoke_type == kVirtual || invoke_type == kInterface) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100385 MethodReference target_method(dex_file_, method_idx);
386 uintptr_t direct_code;
387 uintptr_t direct_method;
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000388 int table_index;
389 InvokeType optimized_invoke_type = invoke_type;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100390 // TODO: Add devirtualization support.
391 compiler_driver_->ComputeInvokeInfo(dex_compilation_unit_, dex_offset, true, true,
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000392 &optimized_invoke_type, &target_method, &table_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100393 &direct_code, &direct_method);
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000394 if (table_index == -1) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100395 return false;
396 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000397
398 if (invoke_type == kVirtual) {
399 invoke = new (arena_) HInvokeVirtual(
400 arena_, number_of_arguments, return_type, dex_offset, table_index);
401 } else {
402 DCHECK_EQ(invoke_type, kInterface);
403 invoke = new (arena_) HInvokeInterface(
404 arena_, number_of_arguments, return_type, dex_offset, method_idx, table_index);
405 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100406 } else {
407 // Treat invoke-direct like static calls for now.
408 invoke = new (arena_) HInvokeStatic(
409 arena_, number_of_arguments, return_type, dex_offset, method_idx);
410 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100411
412 size_t start_index = 0;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100413 Temporaries temps(graph_, is_instance_call ? 1 : 0);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100414 if (is_instance_call) {
415 HInstruction* arg = LoadLocal(is_range ? register_index : args[0], Primitive::kPrimNot);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100416 HNullCheck* null_check = new (arena_) HNullCheck(arg, dex_offset);
417 current_block_->AddInstruction(null_check);
418 temps.Add(null_check);
419 invoke->SetArgumentAt(0, null_check);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100420 start_index = 1;
421 }
422
423 uint32_t descriptor_index = 1;
424 uint32_t argument_index = start_index;
425 for (size_t i = start_index; i < number_of_vreg_arguments; i++, argument_index++) {
426 Primitive::Type type = Primitive::GetType(descriptor[descriptor_index++]);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100427 bool is_wide = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble);
428 if (!is_range && is_wide && args[i] + 1 != args[i + 1]) {
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100429 LOG(WARNING) << "Non sequential register pair in " << dex_compilation_unit_->GetSymbol()
430 << " at " << dex_offset;
431 // We do not implement non sequential register pair.
432 return false;
433 }
434 HInstruction* arg = LoadLocal(is_range ? register_index + i : args[i], type);
435 invoke->SetArgumentAt(argument_index, arg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100436 if (is_wide) {
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100437 i++;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100438 }
439 }
440
441 DCHECK_EQ(argument_index, number_of_arguments);
442 current_block_->AddInstruction(invoke);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100443 latest_result_ = invoke;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100444 return true;
445}
446
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100447bool HGraphBuilder::BuildInstanceFieldAccess(const Instruction& instruction,
448 uint32_t dex_offset,
449 bool is_put) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100450 uint32_t source_or_dest_reg = instruction.VRegA_22c();
451 uint32_t obj_reg = instruction.VRegB_22c();
452 uint16_t field_index = instruction.VRegC_22c();
453
454 ScopedObjectAccess soa(Thread::Current());
455 StackHandleScope<1> hs(soa.Self());
456 Handle<mirror::ArtField> resolved_field(hs.NewHandle(
457 compiler_driver_->ComputeInstanceFieldInfo(field_index, dex_compilation_unit_, is_put, soa)));
458
459 if (resolved_field.Get() == nullptr) {
460 return false;
461 }
462 if (resolved_field->IsVolatile()) {
463 return false;
464 }
465
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100466 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
467 if (!IsTypeSupported(field_type)) {
468 return false;
469 }
470
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100471 HInstruction* object = LoadLocal(obj_reg, Primitive::kPrimNot);
472 current_block_->AddInstruction(new (arena_) HNullCheck(object, dex_offset));
473 if (is_put) {
474 Temporaries temps(graph_, 1);
475 HInstruction* null_check = current_block_->GetLastInstruction();
476 // We need one temporary for the null check.
477 temps.Add(null_check);
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100478 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100479 current_block_->AddInstruction(new (arena_) HInstanceFieldSet(
480 null_check,
481 value,
Nicolas Geoffray39468442014-09-02 15:17:15 +0100482 field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100483 resolved_field->GetOffset()));
484 } else {
485 current_block_->AddInstruction(new (arena_) HInstanceFieldGet(
486 current_block_->GetLastInstruction(),
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100487 field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100488 resolved_field->GetOffset()));
489
490 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
491 }
492 return true;
493}
494
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100495
496
497bool HGraphBuilder::BuildStaticFieldAccess(const Instruction& instruction,
498 uint32_t dex_offset,
499 bool is_put) {
500 uint32_t source_or_dest_reg = instruction.VRegA_21c();
501 uint16_t field_index = instruction.VRegB_21c();
502
503 uint32_t storage_index;
504 bool is_referrers_class;
505 bool is_initialized;
506 bool is_volatile;
507 MemberOffset field_offset(0u);
508 Primitive::Type field_type;
509
510 bool fast_path = compiler_driver_->ComputeStaticFieldInfo(field_index,
511 dex_compilation_unit_,
512 is_put,
513 &field_offset,
514 &storage_index,
515 &is_referrers_class,
516 &is_volatile,
517 &is_initialized,
518 &field_type);
519 if (!fast_path) {
520 return false;
521 }
522
523 if (is_volatile) {
524 return false;
525 }
526
527 if (!IsTypeSupported(field_type)) {
528 return false;
529 }
530
531 HLoadClass* constant = new (arena_) HLoadClass(
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000532 storage_index, is_referrers_class, dex_offset);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100533 current_block_->AddInstruction(constant);
534
535 HInstruction* cls = constant;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000536 if (!is_initialized) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100537 cls = new (arena_) HClinitCheck(constant, dex_offset);
538 current_block_->AddInstruction(cls);
539 }
540
541 if (is_put) {
542 // We need to keep the class alive before loading the value.
543 Temporaries temps(graph_, 1);
544 temps.Add(cls);
545 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
546 DCHECK_EQ(value->GetType(), field_type);
547 current_block_->AddInstruction(
548 new (arena_) HStaticFieldSet(cls, value, field_type, field_offset));
549 } else {
550 current_block_->AddInstruction(new (arena_) HStaticFieldGet(cls, field_type, field_offset));
551 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
552 }
553 return true;
554}
555
Calin Juravle865fc882014-11-06 17:09:03 +0000556void HGraphBuilder::BuildCheckedDiv(uint16_t out_reg,
557 uint16_t first_reg,
Calin Juravle0f00db72014-11-06 18:19:23 +0000558 int32_t second_reg,
Calin Juravled0d48522014-11-04 16:40:20 +0000559 uint32_t dex_offset,
560 Primitive::Type type,
561 bool second_is_lit) {
562 DCHECK(type == Primitive::kPrimInt);
563
Calin Juravle865fc882014-11-06 17:09:03 +0000564 HInstruction* first = LoadLocal(first_reg, type);
565 HInstruction* second = second_is_lit ? GetIntConstant(second_reg) : LoadLocal(second_reg, type);
Calin Juravled0d48522014-11-04 16:40:20 +0000566 if (!second->IsIntConstant() || (second->AsIntConstant()->GetValue() == 0)) {
567 second = new (arena_) HDivZeroCheck(second, dex_offset);
568 Temporaries temps(graph_, 1);
569 current_block_->AddInstruction(second);
570 temps.Add(current_block_->GetLastInstruction());
571 }
572
573 current_block_->AddInstruction(new (arena_) HDiv(type, first, second));
Calin Juravle865fc882014-11-06 17:09:03 +0000574 UpdateLocal(out_reg, current_block_->GetLastInstruction());
Calin Juravled0d48522014-11-04 16:40:20 +0000575}
576
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100577void HGraphBuilder::BuildArrayAccess(const Instruction& instruction,
578 uint32_t dex_offset,
579 bool is_put,
580 Primitive::Type anticipated_type) {
581 uint8_t source_or_dest_reg = instruction.VRegA_23x();
582 uint8_t array_reg = instruction.VRegB_23x();
583 uint8_t index_reg = instruction.VRegC_23x();
584
585 DCHECK(IsTypeSupported(anticipated_type));
586
587 // We need one temporary for the null check, one for the index, and one for the length.
588 Temporaries temps(graph_, 3);
589
590 HInstruction* object = LoadLocal(array_reg, Primitive::kPrimNot);
591 object = new (arena_) HNullCheck(object, dex_offset);
592 current_block_->AddInstruction(object);
593 temps.Add(object);
594
595 HInstruction* length = new (arena_) HArrayLength(object);
596 current_block_->AddInstruction(length);
597 temps.Add(length);
598 HInstruction* index = LoadLocal(index_reg, Primitive::kPrimInt);
599 index = new (arena_) HBoundsCheck(index, length, dex_offset);
600 current_block_->AddInstruction(index);
601 temps.Add(index);
602 if (is_put) {
603 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type);
604 // TODO: Insert a type check node if the type is Object.
Nicolas Geoffray39468442014-09-02 15:17:15 +0100605 current_block_->AddInstruction(new (arena_) HArraySet(
606 object, index, value, anticipated_type, dex_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100607 } else {
608 current_block_->AddInstruction(new (arena_) HArrayGet(object, index, anticipated_type));
609 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
610 }
611}
612
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100613void HGraphBuilder::BuildFilledNewArray(uint32_t dex_offset,
614 uint32_t type_index,
615 uint32_t number_of_vreg_arguments,
616 bool is_range,
617 uint32_t* args,
618 uint32_t register_index) {
619 HInstruction* length = GetIntConstant(number_of_vreg_arguments);
620 HInstruction* object = new (arena_) HNewArray(length, dex_offset, type_index);
621 current_block_->AddInstruction(object);
622
623 const char* descriptor = dex_file_->StringByTypeIdx(type_index);
624 DCHECK_EQ(descriptor[0], '[') << descriptor;
625 char primitive = descriptor[1];
626 DCHECK(primitive == 'I'
627 || primitive == 'L'
628 || primitive == '[') << descriptor;
629 bool is_reference_array = (primitive == 'L') || (primitive == '[');
630 Primitive::Type type = is_reference_array ? Primitive::kPrimNot : Primitive::kPrimInt;
631
632 Temporaries temps(graph_, 1);
633 temps.Add(object);
634 for (size_t i = 0; i < number_of_vreg_arguments; ++i) {
635 HInstruction* value = LoadLocal(is_range ? register_index + i : args[i], type);
636 HInstruction* index = GetIntConstant(i);
637 current_block_->AddInstruction(
638 new (arena_) HArraySet(object, index, value, type, dex_offset));
639 }
640 latest_result_ = object;
641}
642
643template <typename T>
644void HGraphBuilder::BuildFillArrayData(HInstruction* object,
645 const T* data,
646 uint32_t element_count,
647 Primitive::Type anticipated_type,
648 uint32_t dex_offset) {
649 for (uint32_t i = 0; i < element_count; ++i) {
650 HInstruction* index = GetIntConstant(i);
651 HInstruction* value = GetIntConstant(data[i]);
652 current_block_->AddInstruction(new (arena_) HArraySet(
653 object, index, value, anticipated_type, dex_offset));
654 }
655}
656
Calin Juravled0d48522014-11-04 16:40:20 +0000657void HGraphBuilder::BuildFillArrayData(const Instruction& instruction, uint32_t dex_offset) {
658 Temporaries temps(graph_, 1);
659 HInstruction* array = LoadLocal(instruction.VRegA_31t(), Primitive::kPrimNot);
660 HNullCheck* null_check = new (arena_) HNullCheck(array, dex_offset);
661 current_block_->AddInstruction(null_check);
662 temps.Add(null_check);
663
664 HInstruction* length = new (arena_) HArrayLength(null_check);
665 current_block_->AddInstruction(length);
666
667 int32_t payload_offset = instruction.VRegB_31t() + dex_offset;
668 const Instruction::ArrayDataPayload* payload =
669 reinterpret_cast<const Instruction::ArrayDataPayload*>(code_start_ + payload_offset);
670 const uint8_t* data = payload->data;
671 uint32_t element_count = payload->element_count;
672
673 // Implementation of this DEX instruction seems to be that the bounds check is
674 // done before doing any stores.
675 HInstruction* last_index = GetIntConstant(payload->element_count - 1);
676 current_block_->AddInstruction(new (arena_) HBoundsCheck(last_index, length, dex_offset));
677
678 switch (payload->element_width) {
679 case 1:
680 BuildFillArrayData(null_check,
681 reinterpret_cast<const int8_t*>(data),
682 element_count,
683 Primitive::kPrimByte,
684 dex_offset);
685 break;
686 case 2:
687 BuildFillArrayData(null_check,
688 reinterpret_cast<const int16_t*>(data),
689 element_count,
690 Primitive::kPrimShort,
691 dex_offset);
692 break;
693 case 4:
694 BuildFillArrayData(null_check,
695 reinterpret_cast<const int32_t*>(data),
696 element_count,
697 Primitive::kPrimInt,
698 dex_offset);
699 break;
700 case 8:
701 BuildFillWideArrayData(null_check,
702 reinterpret_cast<const int64_t*>(data),
703 element_count,
704 dex_offset);
705 break;
706 default:
707 LOG(FATAL) << "Unknown element width for " << payload->element_width;
708 }
709}
710
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100711void HGraphBuilder::BuildFillWideArrayData(HInstruction* object,
Nicolas Geoffray8d6ae522014-10-23 18:32:13 +0100712 const int64_t* data,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100713 uint32_t element_count,
714 uint32_t dex_offset) {
715 for (uint32_t i = 0; i < element_count; ++i) {
716 HInstruction* index = GetIntConstant(i);
717 HInstruction* value = GetLongConstant(data[i]);
718 current_block_->AddInstruction(new (arena_) HArraySet(
719 object, index, value, Primitive::kPrimLong, dex_offset));
720 }
721}
722
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000723void HGraphBuilder::PotentiallyAddSuspendCheck(int32_t target_offset, uint32_t dex_offset) {
724 if (target_offset <= 0) {
725 // Unconditionnally add a suspend check to backward branches. We can remove
726 // them after we recognize loops in the graph.
727 current_block_->AddInstruction(new (arena_) HSuspendCheck(dex_offset));
728 }
729}
730
731bool HGraphBuilder::AnalyzeDexInstruction(const Instruction& instruction, uint32_t dex_offset) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000732 if (current_block_ == nullptr) {
733 return true; // Dead code
734 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000735
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000736 switch (instruction.Opcode()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000737 case Instruction::CONST_4: {
738 int32_t register_index = instruction.VRegA();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100739 HIntConstant* constant = GetIntConstant(instruction.VRegB_11n());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000740 UpdateLocal(register_index, constant);
741 break;
742 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000743
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100744 case Instruction::CONST_16: {
745 int32_t register_index = instruction.VRegA();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100746 HIntConstant* constant = GetIntConstant(instruction.VRegB_21s());
747 UpdateLocal(register_index, constant);
748 break;
749 }
750
Dave Allison20dfc792014-06-16 20:44:29 -0700751 case Instruction::CONST: {
752 int32_t register_index = instruction.VRegA();
753 HIntConstant* constant = GetIntConstant(instruction.VRegB_31i());
754 UpdateLocal(register_index, constant);
755 break;
756 }
757
758 case Instruction::CONST_HIGH16: {
759 int32_t register_index = instruction.VRegA();
760 HIntConstant* constant = GetIntConstant(instruction.VRegB_21h() << 16);
761 UpdateLocal(register_index, constant);
762 break;
763 }
764
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100765 case Instruction::CONST_WIDE_16: {
766 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -0700767 // Get 16 bits of constant value, sign extended to 64 bits.
768 int64_t value = instruction.VRegB_21s();
769 value <<= 48;
770 value >>= 48;
771 HLongConstant* constant = GetLongConstant(value);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100772 UpdateLocal(register_index, constant);
773 break;
774 }
775
776 case Instruction::CONST_WIDE_32: {
777 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -0700778 // Get 32 bits of constant value, sign extended to 64 bits.
779 int64_t value = instruction.VRegB_31i();
780 value <<= 32;
781 value >>= 32;
782 HLongConstant* constant = GetLongConstant(value);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100783 UpdateLocal(register_index, constant);
784 break;
785 }
786
787 case Instruction::CONST_WIDE: {
788 int32_t register_index = instruction.VRegA();
789 HLongConstant* constant = GetLongConstant(instruction.VRegB_51l());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100790 UpdateLocal(register_index, constant);
791 break;
792 }
793
Dave Allison20dfc792014-06-16 20:44:29 -0700794 case Instruction::CONST_WIDE_HIGH16: {
795 int32_t register_index = instruction.VRegA();
796 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
797 HLongConstant* constant = GetLongConstant(value);
798 UpdateLocal(register_index, constant);
799 break;
800 }
801
802 // TODO: these instructions are also used to move floating point values, so what is
803 // the type (int or float)?
804 case Instruction::MOVE:
805 case Instruction::MOVE_FROM16:
806 case Instruction::MOVE_16: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100807 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100808 UpdateLocal(instruction.VRegA(), value);
809 break;
810 }
811
Dave Allison20dfc792014-06-16 20:44:29 -0700812 // TODO: these instructions are also used to move floating point values, so what is
813 // the type (long or double)?
814 case Instruction::MOVE_WIDE:
815 case Instruction::MOVE_WIDE_FROM16:
816 case Instruction::MOVE_WIDE_16: {
817 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimLong);
818 UpdateLocal(instruction.VRegA(), value);
819 break;
820 }
821
822 case Instruction::MOVE_OBJECT:
823 case Instruction::MOVE_OBJECT_16:
824 case Instruction::MOVE_OBJECT_FROM16: {
825 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimNot);
826 UpdateLocal(instruction.VRegA(), value);
827 break;
828 }
829
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000830 case Instruction::RETURN_VOID: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100831 BuildReturn(instruction, Primitive::kPrimVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000832 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000833 }
834
Dave Allison20dfc792014-06-16 20:44:29 -0700835#define IF_XX(comparison, cond) \
836 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_offset); break; \
837 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_offset); break
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100838
Dave Allison20dfc792014-06-16 20:44:29 -0700839 IF_XX(HEqual, EQ);
840 IF_XX(HNotEqual, NE);
841 IF_XX(HLessThan, LT);
842 IF_XX(HLessThanOrEqual, LE);
843 IF_XX(HGreaterThan, GT);
844 IF_XX(HGreaterThanOrEqual, GE);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000845
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000846 case Instruction::GOTO:
847 case Instruction::GOTO_16:
848 case Instruction::GOTO_32: {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000849 int32_t offset = instruction.GetTargetOffset();
850 PotentiallyAddSuspendCheck(offset, dex_offset);
851 HBasicBlock* target = FindBlockStartingAt(offset + dex_offset);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000852 DCHECK(target != nullptr);
853 current_block_->AddInstruction(new (arena_) HGoto());
854 current_block_->AddSuccessor(target);
855 current_block_ = nullptr;
856 break;
857 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000858
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100859 case Instruction::RETURN: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100860 DCHECK_NE(return_type_, Primitive::kPrimNot);
861 DCHECK_NE(return_type_, Primitive::kPrimLong);
862 DCHECK_NE(return_type_, Primitive::kPrimDouble);
863 BuildReturn(instruction, return_type_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100864 break;
865 }
866
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100867 case Instruction::RETURN_OBJECT: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100868 DCHECK(return_type_ == Primitive::kPrimNot);
869 BuildReturn(instruction, return_type_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100870 break;
871 }
872
873 case Instruction::RETURN_WIDE: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100874 DCHECK(return_type_ == Primitive::kPrimDouble || return_type_ == Primitive::kPrimLong);
875 BuildReturn(instruction, return_type_);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000876 break;
877 }
878
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100879 case Instruction::INVOKE_STATIC:
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100880 case Instruction::INVOKE_DIRECT:
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000881 case Instruction::INVOKE_VIRTUAL:
882 case Instruction::INVOKE_INTERFACE: {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000883 uint32_t method_idx = instruction.VRegB_35c();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100884 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100885 uint32_t args[5];
Ian Rogers29a26482014-05-02 15:27:29 -0700886 instruction.GetVarArgs(args);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100887 if (!BuildInvoke(instruction, dex_offset, method_idx, number_of_vreg_arguments, false, args, -1)) {
888 return false;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100889 }
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100890 break;
891 }
892
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100893 case Instruction::INVOKE_STATIC_RANGE:
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100894 case Instruction::INVOKE_DIRECT_RANGE:
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000895 case Instruction::INVOKE_VIRTUAL_RANGE:
896 case Instruction::INVOKE_INTERFACE_RANGE: {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100897 uint32_t method_idx = instruction.VRegB_3rc();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100898 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
899 uint32_t register_index = instruction.VRegC();
900 if (!BuildInvoke(instruction, dex_offset, method_idx,
901 number_of_vreg_arguments, true, nullptr, register_index)) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100902 return false;
903 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000904 break;
905 }
906
Roland Levillain88cb1752014-10-20 16:36:47 +0100907 case Instruction::NEG_INT: {
908 Unop_12x<HNeg>(instruction, Primitive::kPrimInt);
909 break;
910 }
911
Roland Levillain2e07b4f2014-10-23 18:12:09 +0100912 case Instruction::NEG_LONG: {
913 Unop_12x<HNeg>(instruction, Primitive::kPrimLong);
914 break;
915 }
916
Roland Levillain3dbcb382014-10-28 17:30:07 +0000917 case Instruction::NEG_FLOAT: {
918 Unop_12x<HNeg>(instruction, Primitive::kPrimFloat);
919 break;
920 }
921
922 case Instruction::NEG_DOUBLE: {
923 Unop_12x<HNeg>(instruction, Primitive::kPrimDouble);
924 break;
925 }
926
Roland Levillain1cc5f252014-10-22 18:06:21 +0100927 case Instruction::NOT_INT: {
928 Unop_12x<HNot>(instruction, Primitive::kPrimInt);
929 break;
930 }
931
Roland Levillain70566432014-10-24 16:20:17 +0100932 case Instruction::NOT_LONG: {
933 Unop_12x<HNot>(instruction, Primitive::kPrimLong);
934 break;
935 }
936
Roland Levillaindff1f282014-11-05 14:15:05 +0000937 case Instruction::INT_TO_LONG: {
938 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimLong);
939 break;
940 }
941
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000942 case Instruction::ADD_INT: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100943 Binop_23x<HAdd>(instruction, Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100944 break;
945 }
946
947 case Instruction::ADD_LONG: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100948 Binop_23x<HAdd>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100949 break;
950 }
951
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100952 case Instruction::ADD_DOUBLE: {
953 Binop_23x<HAdd>(instruction, Primitive::kPrimDouble);
954 break;
955 }
956
957 case Instruction::ADD_FLOAT: {
958 Binop_23x<HAdd>(instruction, Primitive::kPrimFloat);
959 break;
960 }
961
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100962 case Instruction::SUB_INT: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100963 Binop_23x<HSub>(instruction, Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100964 break;
965 }
966
967 case Instruction::SUB_LONG: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100968 Binop_23x<HSub>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000969 break;
970 }
971
Calin Juravle096cc022014-10-23 17:01:13 +0100972 case Instruction::SUB_FLOAT: {
973 Binop_23x<HSub>(instruction, Primitive::kPrimFloat);
974 break;
975 }
976
977 case Instruction::SUB_DOUBLE: {
978 Binop_23x<HSub>(instruction, Primitive::kPrimDouble);
979 break;
980 }
981
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000982 case Instruction::ADD_INT_2ADDR: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100983 Binop_12x<HAdd>(instruction, Primitive::kPrimInt);
984 break;
985 }
986
Calin Juravle34bacdf2014-10-07 20:23:36 +0100987 case Instruction::MUL_INT: {
988 Binop_23x<HMul>(instruction, Primitive::kPrimInt);
989 break;
990 }
991
992 case Instruction::MUL_LONG: {
993 Binop_23x<HMul>(instruction, Primitive::kPrimLong);
994 break;
995 }
996
Calin Juravleb5bfa962014-10-21 18:02:24 +0100997 case Instruction::MUL_FLOAT: {
998 Binop_23x<HMul>(instruction, Primitive::kPrimFloat);
999 break;
1000 }
1001
1002 case Instruction::MUL_DOUBLE: {
1003 Binop_23x<HMul>(instruction, Primitive::kPrimDouble);
1004 break;
1005 }
1006
Calin Juravled0d48522014-11-04 16:40:20 +00001007 case Instruction::DIV_INT: {
Calin Juravle865fc882014-11-06 17:09:03 +00001008 BuildCheckedDiv(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1009 dex_offset, Primitive::kPrimInt, false);
Calin Juravled0d48522014-11-04 16:40:20 +00001010 break;
1011 }
1012
Calin Juravle7c4954d2014-10-28 16:57:40 +00001013 case Instruction::DIV_FLOAT: {
1014 Binop_23x<HDiv>(instruction, Primitive::kPrimFloat);
1015 break;
1016 }
1017
1018 case Instruction::DIV_DOUBLE: {
1019 Binop_23x<HDiv>(instruction, Primitive::kPrimDouble);
1020 break;
1021 }
1022
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001023 case Instruction::ADD_LONG_2ADDR: {
1024 Binop_12x<HAdd>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001025 break;
1026 }
1027
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001028 case Instruction::ADD_DOUBLE_2ADDR: {
1029 Binop_12x<HAdd>(instruction, Primitive::kPrimDouble);
1030 break;
1031 }
1032
1033 case Instruction::ADD_FLOAT_2ADDR: {
1034 Binop_12x<HAdd>(instruction, Primitive::kPrimFloat);
1035 break;
1036 }
1037
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001038 case Instruction::SUB_INT_2ADDR: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001039 Binop_12x<HSub>(instruction, Primitive::kPrimInt);
1040 break;
1041 }
1042
1043 case Instruction::SUB_LONG_2ADDR: {
1044 Binop_12x<HSub>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001045 break;
1046 }
1047
Calin Juravle096cc022014-10-23 17:01:13 +01001048 case Instruction::SUB_FLOAT_2ADDR: {
1049 Binop_12x<HSub>(instruction, Primitive::kPrimFloat);
1050 break;
1051 }
1052
1053 case Instruction::SUB_DOUBLE_2ADDR: {
1054 Binop_12x<HSub>(instruction, Primitive::kPrimDouble);
1055 break;
1056 }
1057
Calin Juravle34bacdf2014-10-07 20:23:36 +01001058 case Instruction::MUL_INT_2ADDR: {
1059 Binop_12x<HMul>(instruction, Primitive::kPrimInt);
1060 break;
1061 }
1062
1063 case Instruction::MUL_LONG_2ADDR: {
1064 Binop_12x<HMul>(instruction, Primitive::kPrimLong);
1065 break;
1066 }
1067
Calin Juravleb5bfa962014-10-21 18:02:24 +01001068 case Instruction::MUL_FLOAT_2ADDR: {
1069 Binop_12x<HMul>(instruction, Primitive::kPrimFloat);
1070 break;
1071 }
1072
1073 case Instruction::MUL_DOUBLE_2ADDR: {
1074 Binop_12x<HMul>(instruction, Primitive::kPrimDouble);
1075 break;
1076 }
1077
Calin Juravle865fc882014-11-06 17:09:03 +00001078 case Instruction::DIV_INT_2ADDR: {
1079 BuildCheckedDiv(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
1080 dex_offset, Primitive::kPrimInt, false);
1081 break;
1082 }
1083
Calin Juravle7c4954d2014-10-28 16:57:40 +00001084 case Instruction::DIV_FLOAT_2ADDR: {
1085 Binop_12x<HDiv>(instruction, Primitive::kPrimFloat);
1086 break;
1087 }
1088
1089 case Instruction::DIV_DOUBLE_2ADDR: {
1090 Binop_12x<HDiv>(instruction, Primitive::kPrimDouble);
1091 break;
1092 }
1093
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001094 case Instruction::ADD_INT_LIT16: {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001095 Binop_22s<HAdd>(instruction, false);
1096 break;
1097 }
1098
1099 case Instruction::RSUB_INT: {
1100 Binop_22s<HSub>(instruction, true);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001101 break;
1102 }
1103
Calin Juravle34bacdf2014-10-07 20:23:36 +01001104 case Instruction::MUL_INT_LIT16: {
1105 Binop_22s<HMul>(instruction, false);
1106 break;
1107 }
1108
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001109 case Instruction::ADD_INT_LIT8: {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001110 Binop_22b<HAdd>(instruction, false);
1111 break;
1112 }
1113
1114 case Instruction::RSUB_INT_LIT8: {
1115 Binop_22b<HSub>(instruction, true);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001116 break;
1117 }
1118
Calin Juravle34bacdf2014-10-07 20:23:36 +01001119 case Instruction::MUL_INT_LIT8: {
1120 Binop_22b<HMul>(instruction, false);
1121 break;
1122 }
1123
Calin Juravled0d48522014-11-04 16:40:20 +00001124 case Instruction::DIV_INT_LIT16:
1125 case Instruction::DIV_INT_LIT8: {
Calin Juravle865fc882014-11-06 17:09:03 +00001126 BuildCheckedDiv(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1127 dex_offset, Primitive::kPrimInt, true);
Calin Juravled0d48522014-11-04 16:40:20 +00001128 break;
1129 }
1130
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001131 case Instruction::NEW_INSTANCE: {
1132 current_block_->AddInstruction(
1133 new (arena_) HNewInstance(dex_offset, instruction.VRegB_21c()));
1134 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
1135 break;
1136 }
1137
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001138 case Instruction::NEW_ARRAY: {
1139 HInstruction* length = LoadLocal(instruction.VRegB_22c(), Primitive::kPrimInt);
1140 current_block_->AddInstruction(
1141 new (arena_) HNewArray(length, dex_offset, instruction.VRegC_22c()));
1142 UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction());
1143 break;
1144 }
1145
1146 case Instruction::FILLED_NEW_ARRAY: {
1147 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
1148 uint32_t type_index = instruction.VRegB_35c();
1149 uint32_t args[5];
1150 instruction.GetVarArgs(args);
1151 BuildFilledNewArray(dex_offset, type_index, number_of_vreg_arguments, false, args, 0);
1152 break;
1153 }
1154
1155 case Instruction::FILLED_NEW_ARRAY_RANGE: {
1156 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
1157 uint32_t type_index = instruction.VRegB_3rc();
1158 uint32_t register_index = instruction.VRegC_3rc();
1159 BuildFilledNewArray(
1160 dex_offset, type_index, number_of_vreg_arguments, true, nullptr, register_index);
1161 break;
1162 }
1163
1164 case Instruction::FILL_ARRAY_DATA: {
Calin Juravled0d48522014-11-04 16:40:20 +00001165 BuildFillArrayData(instruction, dex_offset);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001166 break;
1167 }
1168
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001169 case Instruction::MOVE_RESULT:
Dave Allison20dfc792014-06-16 20:44:29 -07001170 case Instruction::MOVE_RESULT_WIDE:
1171 case Instruction::MOVE_RESULT_OBJECT:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001172 UpdateLocal(instruction.VRegA(), latest_result_);
1173 latest_result_ = nullptr;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001174 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001175
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001176 case Instruction::CMP_LONG: {
1177 Binop_23x<HCompare>(instruction, Primitive::kPrimLong);
1178 break;
1179 }
1180
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001181 case Instruction::NOP:
1182 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001183
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001184 case Instruction::IGET:
1185 case Instruction::IGET_WIDE:
1186 case Instruction::IGET_OBJECT:
1187 case Instruction::IGET_BOOLEAN:
1188 case Instruction::IGET_BYTE:
1189 case Instruction::IGET_CHAR:
1190 case Instruction::IGET_SHORT: {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001191 if (!BuildInstanceFieldAccess(instruction, dex_offset, false)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001192 return false;
1193 }
1194 break;
1195 }
1196
1197 case Instruction::IPUT:
1198 case Instruction::IPUT_WIDE:
1199 case Instruction::IPUT_OBJECT:
1200 case Instruction::IPUT_BOOLEAN:
1201 case Instruction::IPUT_BYTE:
1202 case Instruction::IPUT_CHAR:
1203 case Instruction::IPUT_SHORT: {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001204 if (!BuildInstanceFieldAccess(instruction, dex_offset, true)) {
1205 return false;
1206 }
1207 break;
1208 }
1209
1210 case Instruction::SGET:
1211 case Instruction::SGET_WIDE:
1212 case Instruction::SGET_OBJECT:
1213 case Instruction::SGET_BOOLEAN:
1214 case Instruction::SGET_BYTE:
1215 case Instruction::SGET_CHAR:
1216 case Instruction::SGET_SHORT: {
1217 if (!BuildStaticFieldAccess(instruction, dex_offset, false)) {
1218 return false;
1219 }
1220 break;
1221 }
1222
1223 case Instruction::SPUT:
1224 case Instruction::SPUT_WIDE:
1225 case Instruction::SPUT_OBJECT:
1226 case Instruction::SPUT_BOOLEAN:
1227 case Instruction::SPUT_BYTE:
1228 case Instruction::SPUT_CHAR:
1229 case Instruction::SPUT_SHORT: {
1230 if (!BuildStaticFieldAccess(instruction, dex_offset, true)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001231 return false;
1232 }
1233 break;
1234 }
1235
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001236#define ARRAY_XX(kind, anticipated_type) \
1237 case Instruction::AGET##kind: { \
1238 BuildArrayAccess(instruction, dex_offset, false, anticipated_type); \
1239 break; \
1240 } \
1241 case Instruction::APUT##kind: { \
1242 BuildArrayAccess(instruction, dex_offset, true, anticipated_type); \
1243 break; \
1244 }
1245
1246 ARRAY_XX(, Primitive::kPrimInt);
1247 ARRAY_XX(_WIDE, Primitive::kPrimLong);
1248 ARRAY_XX(_OBJECT, Primitive::kPrimNot);
1249 ARRAY_XX(_BOOLEAN, Primitive::kPrimBoolean);
1250 ARRAY_XX(_BYTE, Primitive::kPrimByte);
1251 ARRAY_XX(_CHAR, Primitive::kPrimChar);
1252 ARRAY_XX(_SHORT, Primitive::kPrimShort);
1253
Nicolas Geoffray39468442014-09-02 15:17:15 +01001254 case Instruction::ARRAY_LENGTH: {
1255 HInstruction* object = LoadLocal(instruction.VRegB_12x(), Primitive::kPrimNot);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001256 // No need for a temporary for the null check, it is the only input of the following
1257 // instruction.
1258 object = new (arena_) HNullCheck(object, dex_offset);
1259 current_block_->AddInstruction(object);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001260 current_block_->AddInstruction(new (arena_) HArrayLength(object));
1261 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction());
1262 break;
1263 }
1264
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001265 case Instruction::CONST_STRING: {
1266 current_block_->AddInstruction(new (arena_) HLoadString(instruction.VRegB_21c(), dex_offset));
1267 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
1268 break;
1269 }
1270
1271 case Instruction::CONST_STRING_JUMBO: {
1272 current_block_->AddInstruction(new (arena_) HLoadString(instruction.VRegB_31c(), dex_offset));
1273 UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction());
1274 break;
1275 }
1276
Nicolas Geoffray424f6762014-11-03 14:51:25 +00001277 case Instruction::CONST_CLASS: {
1278 uint16_t type_index = instruction.VRegB_21c();
1279 bool type_known_final;
1280 bool type_known_abstract;
1281 bool is_referrers_class;
1282 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
1283 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index,
1284 &type_known_final, &type_known_abstract, &is_referrers_class);
1285 if (!can_access) {
1286 return false;
1287 }
1288 current_block_->AddInstruction(
1289 new (arena_) HLoadClass(instruction.VRegB_21c(), is_referrers_class, dex_offset));
1290 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
1291 break;
1292 }
1293
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001294 case Instruction::MOVE_EXCEPTION: {
1295 current_block_->AddInstruction(new (arena_) HLoadException());
1296 UpdateLocal(instruction.VRegA_11x(), current_block_->GetLastInstruction());
1297 break;
1298 }
1299
1300 case Instruction::THROW: {
1301 HInstruction* exception = LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot);
1302 current_block_->AddInstruction(new (arena_) HThrow(exception, dex_offset));
1303 // A throw instruction must branch to the exit block.
1304 current_block_->AddSuccessor(exit_block_);
1305 // We finished building this block. Set the current block to null to avoid
1306 // adding dead instructions to it.
1307 current_block_ = nullptr;
1308 break;
1309 }
1310
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001311 default:
1312 return false;
1313 }
1314 return true;
1315}
1316
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001317HIntConstant* HGraphBuilder::GetIntConstant0() {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001318 if (constant0_ != nullptr) {
1319 return constant0_;
1320 }
1321 constant0_ = new(arena_) HIntConstant(0);
1322 entry_block_->AddInstruction(constant0_);
1323 return constant0_;
1324}
1325
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001326HIntConstant* HGraphBuilder::GetIntConstant1() {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001327 if (constant1_ != nullptr) {
1328 return constant1_;
1329 }
1330 constant1_ = new(arena_) HIntConstant(1);
1331 entry_block_->AddInstruction(constant1_);
1332 return constant1_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001333}
1334
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001335HIntConstant* HGraphBuilder::GetIntConstant(int32_t constant) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001336 switch (constant) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001337 case 0: return GetIntConstant0();
1338 case 1: return GetIntConstant1();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001339 default: {
1340 HIntConstant* instruction = new (arena_) HIntConstant(constant);
1341 entry_block_->AddInstruction(instruction);
1342 return instruction;
1343 }
1344 }
1345}
1346
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001347HLongConstant* HGraphBuilder::GetLongConstant(int64_t constant) {
1348 HLongConstant* instruction = new (arena_) HLongConstant(constant);
1349 entry_block_->AddInstruction(instruction);
1350 return instruction;
1351}
1352
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001353HLocal* HGraphBuilder::GetLocalAt(int register_index) const {
1354 return locals_.Get(register_index);
1355}
1356
1357void HGraphBuilder::UpdateLocal(int register_index, HInstruction* instruction) const {
1358 HLocal* local = GetLocalAt(register_index);
1359 current_block_->AddInstruction(new (arena_) HStoreLocal(local, instruction));
1360}
1361
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001362HInstruction* HGraphBuilder::LoadLocal(int register_index, Primitive::Type type) const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001363 HLocal* local = GetLocalAt(register_index);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001364 current_block_->AddInstruction(new (arena_) HLoadLocal(local, type));
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001365 return current_block_->GetLastInstruction();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001366}
1367
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001368} // namespace art