blob: 5bcc65b03b106c384438f50e8c2da430f313b1f5 [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 *
3 * Copyright (C) 2014 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Nicolas Geoffraye5038322014-07-04 09:41:32 +010018#include "builder.h"
19
20#include "class_linker.h"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000021#include "dex_file.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000022#include "dex_file-inl.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000023#include "dex_instruction.h"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000024#include "dex_instruction-inl.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010025#include "driver/compiler_driver-inl.h"
26#include "mirror/art_field.h"
27#include "mirror/art_field-inl.h"
28#include "mirror/class_loader.h"
29#include "mirror/dex_cache.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000030#include "nodes.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000031#include "primitive.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010032#include "scoped_thread_state_change.h"
33#include "thread.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000034
35namespace art {
36
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010037/**
38 * Helper class to add HTemporary instructions. This class is used when
39 * converting a DEX instruction to multiple HInstruction, and where those
40 * instructions do not die at the following instruction, but instead spans
41 * multiple instructions.
42 */
43class Temporaries : public ValueObject {
44 public:
45 Temporaries(HGraph* graph, size_t count) : graph_(graph), count_(count), index_(0) {
46 graph_->UpdateNumberOfTemporaries(count_);
47 }
48
49 void Add(HInstruction* instruction) {
50 // We currently only support vreg size temps.
51 DCHECK(instruction->GetType() != Primitive::kPrimLong
52 && instruction->GetType() != Primitive::kPrimDouble);
53 HInstruction* temp = new (graph_->GetArena()) HTemporary(index_++);
54 instruction->GetBlock()->AddInstruction(temp);
55 DCHECK(temp->GetPrevious() == instruction);
56 }
57
58 private:
59 HGraph* const graph_;
60
61 // The total number of temporaries that will be used.
62 const size_t count_;
63
64 // Current index in the temporary stack, updated by `Add`.
65 size_t index_;
66};
67
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +010068static bool IsTypeSupported(Primitive::Type type) {
69 return type != Primitive::kPrimFloat && type != Primitive::kPrimDouble;
70}
71
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010072void HGraphBuilder::InitializeLocals(uint16_t count) {
73 graph_->SetNumberOfVRegs(count);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000074 locals_.SetSize(count);
75 for (int i = 0; i < count; i++) {
76 HLocal* local = new (arena_) HLocal(i);
77 entry_block_->AddInstruction(local);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000078 locals_.Put(i, local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000079 }
80}
81
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010082bool HGraphBuilder::InitializeParameters(uint16_t number_of_parameters) {
83 // dex_compilation_unit_ is null only when unit testing.
84 if (dex_compilation_unit_ == nullptr) {
85 return true;
86 }
87
88 graph_->SetNumberOfInVRegs(number_of_parameters);
89 const char* shorty = dex_compilation_unit_->GetShorty();
90 int locals_index = locals_.Size() - number_of_parameters;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010091 int parameter_index = 0;
92
93 if (!dex_compilation_unit_->IsStatic()) {
94 // Add the implicit 'this' argument, not expressed in the signature.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010095 HParameterValue* parameter =
96 new (arena_) HParameterValue(parameter_index++, Primitive::kPrimNot);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +010097 entry_block_->AddInstruction(parameter);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010098 HLocal* local = GetLocalAt(locals_index++);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +010099 entry_block_->AddInstruction(new (arena_) HStoreLocal(local, parameter));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100100 number_of_parameters--;
101 }
102
103 uint32_t pos = 1;
104 for (int i = 0; i < number_of_parameters; i++) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100105 HParameterValue* parameter =
106 new (arena_) HParameterValue(parameter_index++, Primitive::GetType(shorty[pos++]));
107 entry_block_->AddInstruction(parameter);
108 HLocal* local = GetLocalAt(locals_index++);
109 // Store the parameter value in the local that the dex code will use
110 // to reference that parameter.
111 entry_block_->AddInstruction(new (arena_) HStoreLocal(local, parameter));
112 bool is_wide = (parameter->GetType() == Primitive::kPrimLong)
113 || (parameter->GetType() == Primitive::kPrimDouble);
114 if (is_wide) {
115 i++;
116 locals_index++;
117 parameter_index++;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100118 }
119 }
120 return true;
121}
122
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000123static bool CanHandleCodeItem(const DexFile::CodeItem& code_item) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000124 if (code_item.tries_size_ > 0) {
125 return false;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000126 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000127 return true;
128}
129
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100130template<typename T>
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100131void HGraphBuilder::If_22t(const Instruction& instruction, uint32_t dex_offset) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000132 int32_t target_offset = instruction.GetTargetOffset();
133 PotentiallyAddSuspendCheck(target_offset, dex_offset);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100134 HInstruction* first = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
135 HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
Dave Allison20dfc792014-06-16 20:44:29 -0700136 T* comparison = new (arena_) T(first, second);
137 current_block_->AddInstruction(comparison);
138 HInstruction* ifinst = new (arena_) HIf(comparison);
139 current_block_->AddInstruction(ifinst);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000140 HBasicBlock* target = FindBlockStartingAt(dex_offset + target_offset);
Dave Allison20dfc792014-06-16 20:44:29 -0700141 DCHECK(target != nullptr);
142 current_block_->AddSuccessor(target);
143 target = FindBlockStartingAt(dex_offset + instruction.SizeInCodeUnits());
144 DCHECK(target != nullptr);
145 current_block_->AddSuccessor(target);
146 current_block_ = nullptr;
147}
148
149template<typename T>
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100150void HGraphBuilder::If_21t(const Instruction& instruction, uint32_t dex_offset) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000151 int32_t target_offset = instruction.GetTargetOffset();
152 PotentiallyAddSuspendCheck(target_offset, dex_offset);
Dave Allison20dfc792014-06-16 20:44:29 -0700153 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
154 T* comparison = new (arena_) T(value, GetIntConstant(0));
155 current_block_->AddInstruction(comparison);
156 HInstruction* ifinst = new (arena_) HIf(comparison);
157 current_block_->AddInstruction(ifinst);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000158 HBasicBlock* target = FindBlockStartingAt(dex_offset + target_offset);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100159 DCHECK(target != nullptr);
160 current_block_->AddSuccessor(target);
161 target = FindBlockStartingAt(dex_offset + instruction.SizeInCodeUnits());
162 DCHECK(target != nullptr);
163 current_block_->AddSuccessor(target);
164 current_block_ = nullptr;
165}
166
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000167HGraph* HGraphBuilder::BuildGraph(const DexFile::CodeItem& code_item) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000168 if (!CanHandleCodeItem(code_item)) {
169 return nullptr;
170 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000171
172 const uint16_t* code_ptr = code_item.insns_;
173 const uint16_t* code_end = code_item.insns_ + code_item.insns_size_in_code_units_;
174
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>
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100270void HGraphBuilder::Binop_23x(const Instruction& instruction, Primitive::Type type) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100271 HInstruction* first = LoadLocal(instruction.VRegB(), type);
272 HInstruction* second = LoadLocal(instruction.VRegC(), type);
273 current_block_->AddInstruction(new (arena_) T(type, first, second));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100274 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
275}
276
277template<typename T>
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100278void HGraphBuilder::Binop_12x(const Instruction& instruction, Primitive::Type type) {
279 HInstruction* first = LoadLocal(instruction.VRegA(), type);
280 HInstruction* second = LoadLocal(instruction.VRegB(), type);
281 current_block_->AddInstruction(new (arena_) T(type, first, second));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100282 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
283}
284
285template<typename T>
286void HGraphBuilder::Binop_22s(const Instruction& instruction, bool reverse) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100287 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
288 HInstruction* second = GetIntConstant(instruction.VRegC_22s());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100289 if (reverse) {
290 std::swap(first, second);
291 }
292 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second));
293 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
294}
295
296template<typename T>
297void HGraphBuilder::Binop_22b(const Instruction& instruction, bool reverse) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100298 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
299 HInstruction* second = GetIntConstant(instruction.VRegC_22b());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100300 if (reverse) {
301 std::swap(first, second);
302 }
303 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second));
304 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
305}
306
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100307void HGraphBuilder::BuildReturn(const Instruction& instruction, Primitive::Type type) {
308 if (type == Primitive::kPrimVoid) {
309 current_block_->AddInstruction(new (arena_) HReturnVoid());
310 } else {
311 HInstruction* value = LoadLocal(instruction.VRegA(), type);
312 current_block_->AddInstruction(new (arena_) HReturn(value));
313 }
314 current_block_->AddSuccessor(exit_block_);
315 current_block_ = nullptr;
316}
317
318bool HGraphBuilder::BuildInvoke(const Instruction& instruction,
319 uint32_t dex_offset,
320 uint32_t method_idx,
321 uint32_t number_of_vreg_arguments,
322 bool is_range,
323 uint32_t* args,
324 uint32_t register_index) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100325 Instruction::Code opcode = instruction.Opcode();
326 InvokeType invoke_type;
327 switch (opcode) {
328 case Instruction::INVOKE_STATIC:
329 case Instruction::INVOKE_STATIC_RANGE:
330 invoke_type = kStatic;
331 break;
332 case Instruction::INVOKE_DIRECT:
333 case Instruction::INVOKE_DIRECT_RANGE:
334 invoke_type = kDirect;
335 break;
336 case Instruction::INVOKE_VIRTUAL:
337 case Instruction::INVOKE_VIRTUAL_RANGE:
338 invoke_type = kVirtual;
339 break;
340 case Instruction::INVOKE_INTERFACE:
341 case Instruction::INVOKE_INTERFACE_RANGE:
342 invoke_type = kInterface;
343 break;
344 case Instruction::INVOKE_SUPER_RANGE:
345 case Instruction::INVOKE_SUPER:
346 invoke_type = kSuper;
347 break;
348 default:
349 LOG(FATAL) << "Unexpected invoke op: " << opcode;
350 return false;
351 }
352
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100353 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
354 const DexFile::ProtoId& proto_id = dex_file_->GetProtoId(method_id.proto_idx_);
355 const char* descriptor = dex_file_->StringDataByIdx(proto_id.shorty_idx_);
356 Primitive::Type return_type = Primitive::GetType(descriptor[0]);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100357 bool is_instance_call = invoke_type != kStatic;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100358 const size_t number_of_arguments = strlen(descriptor) - (is_instance_call ? 0 : 1);
359
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100360 HInvoke* invoke = nullptr;
361 if (invoke_type == kVirtual) {
362 MethodReference target_method(dex_file_, method_idx);
363 uintptr_t direct_code;
364 uintptr_t direct_method;
365 int vtable_index;
366 // TODO: Add devirtualization support.
367 compiler_driver_->ComputeInvokeInfo(dex_compilation_unit_, dex_offset, true, true,
368 &invoke_type, &target_method, &vtable_index,
369 &direct_code, &direct_method);
370 if (vtable_index == -1) {
371 return false;
372 }
373 invoke = new (arena_) HInvokeVirtual(
374 arena_, number_of_arguments, return_type, dex_offset, vtable_index);
375 } else {
376 // Treat invoke-direct like static calls for now.
377 invoke = new (arena_) HInvokeStatic(
378 arena_, number_of_arguments, return_type, dex_offset, method_idx);
379 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100380
381 size_t start_index = 0;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100382 Temporaries temps(graph_, is_instance_call ? 1 : 0);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100383 if (is_instance_call) {
384 HInstruction* arg = LoadLocal(is_range ? register_index : args[0], Primitive::kPrimNot);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100385 HNullCheck* null_check = new (arena_) HNullCheck(arg, dex_offset);
386 current_block_->AddInstruction(null_check);
387 temps.Add(null_check);
388 invoke->SetArgumentAt(0, null_check);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100389 start_index = 1;
390 }
391
392 uint32_t descriptor_index = 1;
393 uint32_t argument_index = start_index;
394 for (size_t i = start_index; i < number_of_vreg_arguments; i++, argument_index++) {
395 Primitive::Type type = Primitive::GetType(descriptor[descriptor_index++]);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100396 bool is_wide = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble);
397 if (!is_range && is_wide && args[i] + 1 != args[i + 1]) {
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100398 LOG(WARNING) << "Non sequential register pair in " << dex_compilation_unit_->GetSymbol()
399 << " at " << dex_offset;
400 // We do not implement non sequential register pair.
401 return false;
402 }
403 HInstruction* arg = LoadLocal(is_range ? register_index + i : args[i], type);
404 invoke->SetArgumentAt(argument_index, arg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100405 if (is_wide) {
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100406 i++;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100407 }
408 }
409
410 DCHECK_EQ(argument_index, number_of_arguments);
411 current_block_->AddInstruction(invoke);
412 return true;
413}
414
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100415bool HGraphBuilder::BuildFieldAccess(const Instruction& instruction,
416 uint32_t dex_offset,
417 bool is_put) {
418 uint32_t source_or_dest_reg = instruction.VRegA_22c();
419 uint32_t obj_reg = instruction.VRegB_22c();
420 uint16_t field_index = instruction.VRegC_22c();
421
422 ScopedObjectAccess soa(Thread::Current());
423 StackHandleScope<1> hs(soa.Self());
424 Handle<mirror::ArtField> resolved_field(hs.NewHandle(
425 compiler_driver_->ComputeInstanceFieldInfo(field_index, dex_compilation_unit_, is_put, soa)));
426
427 if (resolved_field.Get() == nullptr) {
428 return false;
429 }
430 if (resolved_field->IsVolatile()) {
431 return false;
432 }
433
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100434 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
435 if (!IsTypeSupported(field_type)) {
436 return false;
437 }
438
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100439 HInstruction* object = LoadLocal(obj_reg, Primitive::kPrimNot);
440 current_block_->AddInstruction(new (arena_) HNullCheck(object, dex_offset));
441 if (is_put) {
442 Temporaries temps(graph_, 1);
443 HInstruction* null_check = current_block_->GetLastInstruction();
444 // We need one temporary for the null check.
445 temps.Add(null_check);
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100446 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100447 current_block_->AddInstruction(new (arena_) HInstanceFieldSet(
448 null_check,
449 value,
Nicolas Geoffray39468442014-09-02 15:17:15 +0100450 field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100451 resolved_field->GetOffset()));
452 } else {
453 current_block_->AddInstruction(new (arena_) HInstanceFieldGet(
454 current_block_->GetLastInstruction(),
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100455 field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100456 resolved_field->GetOffset()));
457
458 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
459 }
460 return true;
461}
462
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100463void HGraphBuilder::BuildArrayAccess(const Instruction& instruction,
464 uint32_t dex_offset,
465 bool is_put,
466 Primitive::Type anticipated_type) {
467 uint8_t source_or_dest_reg = instruction.VRegA_23x();
468 uint8_t array_reg = instruction.VRegB_23x();
469 uint8_t index_reg = instruction.VRegC_23x();
470
471 DCHECK(IsTypeSupported(anticipated_type));
472
473 // We need one temporary for the null check, one for the index, and one for the length.
474 Temporaries temps(graph_, 3);
475
476 HInstruction* object = LoadLocal(array_reg, Primitive::kPrimNot);
477 object = new (arena_) HNullCheck(object, dex_offset);
478 current_block_->AddInstruction(object);
479 temps.Add(object);
480
481 HInstruction* length = new (arena_) HArrayLength(object);
482 current_block_->AddInstruction(length);
483 temps.Add(length);
484 HInstruction* index = LoadLocal(index_reg, Primitive::kPrimInt);
485 index = new (arena_) HBoundsCheck(index, length, dex_offset);
486 current_block_->AddInstruction(index);
487 temps.Add(index);
488 if (is_put) {
489 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type);
490 // TODO: Insert a type check node if the type is Object.
Nicolas Geoffray39468442014-09-02 15:17:15 +0100491 current_block_->AddInstruction(new (arena_) HArraySet(
492 object, index, value, anticipated_type, dex_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100493 } else {
494 current_block_->AddInstruction(new (arena_) HArrayGet(object, index, anticipated_type));
495 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
496 }
497}
498
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000499void HGraphBuilder::PotentiallyAddSuspendCheck(int32_t target_offset, uint32_t dex_offset) {
500 if (target_offset <= 0) {
501 // Unconditionnally add a suspend check to backward branches. We can remove
502 // them after we recognize loops in the graph.
503 current_block_->AddInstruction(new (arena_) HSuspendCheck(dex_offset));
504 }
505}
506
507bool HGraphBuilder::AnalyzeDexInstruction(const Instruction& instruction, uint32_t dex_offset) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000508 if (current_block_ == nullptr) {
509 return true; // Dead code
510 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000511
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000512 switch (instruction.Opcode()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000513 case Instruction::CONST_4: {
514 int32_t register_index = instruction.VRegA();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100515 HIntConstant* constant = GetIntConstant(instruction.VRegB_11n());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000516 UpdateLocal(register_index, constant);
517 break;
518 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000519
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100520 case Instruction::CONST_16: {
521 int32_t register_index = instruction.VRegA();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100522 HIntConstant* constant = GetIntConstant(instruction.VRegB_21s());
523 UpdateLocal(register_index, constant);
524 break;
525 }
526
Dave Allison20dfc792014-06-16 20:44:29 -0700527 case Instruction::CONST: {
528 int32_t register_index = instruction.VRegA();
529 HIntConstant* constant = GetIntConstant(instruction.VRegB_31i());
530 UpdateLocal(register_index, constant);
531 break;
532 }
533
534 case Instruction::CONST_HIGH16: {
535 int32_t register_index = instruction.VRegA();
536 HIntConstant* constant = GetIntConstant(instruction.VRegB_21h() << 16);
537 UpdateLocal(register_index, constant);
538 break;
539 }
540
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100541 case Instruction::CONST_WIDE_16: {
542 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -0700543 // Get 16 bits of constant value, sign extended to 64 bits.
544 int64_t value = instruction.VRegB_21s();
545 value <<= 48;
546 value >>= 48;
547 HLongConstant* constant = GetLongConstant(value);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100548 UpdateLocal(register_index, constant);
549 break;
550 }
551
552 case Instruction::CONST_WIDE_32: {
553 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -0700554 // Get 32 bits of constant value, sign extended to 64 bits.
555 int64_t value = instruction.VRegB_31i();
556 value <<= 32;
557 value >>= 32;
558 HLongConstant* constant = GetLongConstant(value);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100559 UpdateLocal(register_index, constant);
560 break;
561 }
562
563 case Instruction::CONST_WIDE: {
564 int32_t register_index = instruction.VRegA();
565 HLongConstant* constant = GetLongConstant(instruction.VRegB_51l());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100566 UpdateLocal(register_index, constant);
567 break;
568 }
569
Dave Allison20dfc792014-06-16 20:44:29 -0700570 case Instruction::CONST_WIDE_HIGH16: {
571 int32_t register_index = instruction.VRegA();
572 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
573 HLongConstant* constant = GetLongConstant(value);
574 UpdateLocal(register_index, constant);
575 break;
576 }
577
578 // TODO: these instructions are also used to move floating point values, so what is
579 // the type (int or float)?
580 case Instruction::MOVE:
581 case Instruction::MOVE_FROM16:
582 case Instruction::MOVE_16: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100583 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100584 UpdateLocal(instruction.VRegA(), value);
585 break;
586 }
587
Dave Allison20dfc792014-06-16 20:44:29 -0700588 // TODO: these instructions are also used to move floating point values, so what is
589 // the type (long or double)?
590 case Instruction::MOVE_WIDE:
591 case Instruction::MOVE_WIDE_FROM16:
592 case Instruction::MOVE_WIDE_16: {
593 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimLong);
594 UpdateLocal(instruction.VRegA(), value);
595 break;
596 }
597
598 case Instruction::MOVE_OBJECT:
599 case Instruction::MOVE_OBJECT_16:
600 case Instruction::MOVE_OBJECT_FROM16: {
601 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimNot);
602 UpdateLocal(instruction.VRegA(), value);
603 break;
604 }
605
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000606 case Instruction::RETURN_VOID: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100607 BuildReturn(instruction, Primitive::kPrimVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000608 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000609 }
610
Dave Allison20dfc792014-06-16 20:44:29 -0700611#define IF_XX(comparison, cond) \
612 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_offset); break; \
613 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_offset); break
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100614
Dave Allison20dfc792014-06-16 20:44:29 -0700615 IF_XX(HEqual, EQ);
616 IF_XX(HNotEqual, NE);
617 IF_XX(HLessThan, LT);
618 IF_XX(HLessThanOrEqual, LE);
619 IF_XX(HGreaterThan, GT);
620 IF_XX(HGreaterThanOrEqual, GE);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000621
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000622 case Instruction::GOTO:
623 case Instruction::GOTO_16:
624 case Instruction::GOTO_32: {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000625 int32_t offset = instruction.GetTargetOffset();
626 PotentiallyAddSuspendCheck(offset, dex_offset);
627 HBasicBlock* target = FindBlockStartingAt(offset + dex_offset);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000628 DCHECK(target != nullptr);
629 current_block_->AddInstruction(new (arena_) HGoto());
630 current_block_->AddSuccessor(target);
631 current_block_ = nullptr;
632 break;
633 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000634
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100635 case Instruction::RETURN: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100636 DCHECK_NE(return_type_, Primitive::kPrimNot);
637 DCHECK_NE(return_type_, Primitive::kPrimLong);
638 DCHECK_NE(return_type_, Primitive::kPrimDouble);
639 BuildReturn(instruction, return_type_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100640 break;
641 }
642
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100643 case Instruction::RETURN_OBJECT: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100644 DCHECK(return_type_ == Primitive::kPrimNot);
645 BuildReturn(instruction, return_type_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100646 break;
647 }
648
649 case Instruction::RETURN_WIDE: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100650 DCHECK(return_type_ == Primitive::kPrimDouble || return_type_ == Primitive::kPrimLong);
651 BuildReturn(instruction, return_type_);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000652 break;
653 }
654
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100655 case Instruction::INVOKE_STATIC:
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100656 case Instruction::INVOKE_DIRECT:
657 case Instruction::INVOKE_VIRTUAL: {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000658 uint32_t method_idx = instruction.VRegB_35c();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100659 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100660 uint32_t args[5];
Ian Rogers29a26482014-05-02 15:27:29 -0700661 instruction.GetVarArgs(args);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100662 if (!BuildInvoke(instruction, dex_offset, method_idx, number_of_vreg_arguments, false, args, -1)) {
663 return false;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100664 }
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100665 break;
666 }
667
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100668 case Instruction::INVOKE_STATIC_RANGE:
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100669 case Instruction::INVOKE_DIRECT_RANGE:
670 case Instruction::INVOKE_VIRTUAL_RANGE: {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100671 uint32_t method_idx = instruction.VRegB_3rc();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100672 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
673 uint32_t register_index = instruction.VRegC();
674 if (!BuildInvoke(instruction, dex_offset, method_idx,
675 number_of_vreg_arguments, true, nullptr, register_index)) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100676 return false;
677 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000678 break;
679 }
680
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000681 case Instruction::ADD_INT: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100682 Binop_23x<HAdd>(instruction, Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100683 break;
684 }
685
686 case Instruction::ADD_LONG: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100687 Binop_23x<HAdd>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100688 break;
689 }
690
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100691 case Instruction::ADD_DOUBLE: {
692 Binop_23x<HAdd>(instruction, Primitive::kPrimDouble);
693 break;
694 }
695
696 case Instruction::ADD_FLOAT: {
697 Binop_23x<HAdd>(instruction, Primitive::kPrimFloat);
698 break;
699 }
700
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100701 case Instruction::SUB_INT: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100702 Binop_23x<HSub>(instruction, Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100703 break;
704 }
705
706 case Instruction::SUB_LONG: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100707 Binop_23x<HSub>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000708 break;
709 }
710
711 case Instruction::ADD_INT_2ADDR: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100712 Binop_12x<HAdd>(instruction, Primitive::kPrimInt);
713 break;
714 }
715
716 case Instruction::ADD_LONG_2ADDR: {
717 Binop_12x<HAdd>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100718 break;
719 }
720
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100721 case Instruction::ADD_DOUBLE_2ADDR: {
722 Binop_12x<HAdd>(instruction, Primitive::kPrimDouble);
723 break;
724 }
725
726 case Instruction::ADD_FLOAT_2ADDR: {
727 Binop_12x<HAdd>(instruction, Primitive::kPrimFloat);
728 break;
729 }
730
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100731 case Instruction::SUB_INT_2ADDR: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100732 Binop_12x<HSub>(instruction, Primitive::kPrimInt);
733 break;
734 }
735
736 case Instruction::SUB_LONG_2ADDR: {
737 Binop_12x<HSub>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000738 break;
739 }
740
741 case Instruction::ADD_INT_LIT16: {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100742 Binop_22s<HAdd>(instruction, false);
743 break;
744 }
745
746 case Instruction::RSUB_INT: {
747 Binop_22s<HSub>(instruction, true);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000748 break;
749 }
750
751 case Instruction::ADD_INT_LIT8: {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100752 Binop_22b<HAdd>(instruction, false);
753 break;
754 }
755
756 case Instruction::RSUB_INT_LIT8: {
757 Binop_22b<HSub>(instruction, true);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000758 break;
759 }
760
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100761 case Instruction::NEW_INSTANCE: {
762 current_block_->AddInstruction(
763 new (arena_) HNewInstance(dex_offset, instruction.VRegB_21c()));
764 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
765 break;
766 }
767
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100768 case Instruction::MOVE_RESULT:
Dave Allison20dfc792014-06-16 20:44:29 -0700769 case Instruction::MOVE_RESULT_WIDE:
770 case Instruction::MOVE_RESULT_OBJECT:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100771 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
772 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100773
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100774 case Instruction::CMP_LONG: {
775 Binop_23x<HCompare>(instruction, Primitive::kPrimLong);
776 break;
777 }
778
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000779 case Instruction::NOP:
780 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000781
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100782 case Instruction::IGET:
783 case Instruction::IGET_WIDE:
784 case Instruction::IGET_OBJECT:
785 case Instruction::IGET_BOOLEAN:
786 case Instruction::IGET_BYTE:
787 case Instruction::IGET_CHAR:
788 case Instruction::IGET_SHORT: {
789 if (!BuildFieldAccess(instruction, dex_offset, false)) {
790 return false;
791 }
792 break;
793 }
794
795 case Instruction::IPUT:
796 case Instruction::IPUT_WIDE:
797 case Instruction::IPUT_OBJECT:
798 case Instruction::IPUT_BOOLEAN:
799 case Instruction::IPUT_BYTE:
800 case Instruction::IPUT_CHAR:
801 case Instruction::IPUT_SHORT: {
802 if (!BuildFieldAccess(instruction, dex_offset, true)) {
803 return false;
804 }
805 break;
806 }
807
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100808#define ARRAY_XX(kind, anticipated_type) \
809 case Instruction::AGET##kind: { \
810 BuildArrayAccess(instruction, dex_offset, false, anticipated_type); \
811 break; \
812 } \
813 case Instruction::APUT##kind: { \
814 BuildArrayAccess(instruction, dex_offset, true, anticipated_type); \
815 break; \
816 }
817
818 ARRAY_XX(, Primitive::kPrimInt);
819 ARRAY_XX(_WIDE, Primitive::kPrimLong);
820 ARRAY_XX(_OBJECT, Primitive::kPrimNot);
821 ARRAY_XX(_BOOLEAN, Primitive::kPrimBoolean);
822 ARRAY_XX(_BYTE, Primitive::kPrimByte);
823 ARRAY_XX(_CHAR, Primitive::kPrimChar);
824 ARRAY_XX(_SHORT, Primitive::kPrimShort);
825
Nicolas Geoffray39468442014-09-02 15:17:15 +0100826 case Instruction::ARRAY_LENGTH: {
827 HInstruction* object = LoadLocal(instruction.VRegB_12x(), Primitive::kPrimNot);
828 current_block_->AddInstruction(new (arena_) HArrayLength(object));
829 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction());
830 break;
831 }
832
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000833 default:
834 return false;
835 }
836 return true;
837}
838
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100839HIntConstant* HGraphBuilder::GetIntConstant0() {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000840 if (constant0_ != nullptr) {
841 return constant0_;
842 }
843 constant0_ = new(arena_) HIntConstant(0);
844 entry_block_->AddInstruction(constant0_);
845 return constant0_;
846}
847
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100848HIntConstant* HGraphBuilder::GetIntConstant1() {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000849 if (constant1_ != nullptr) {
850 return constant1_;
851 }
852 constant1_ = new(arena_) HIntConstant(1);
853 entry_block_->AddInstruction(constant1_);
854 return constant1_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000855}
856
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100857HIntConstant* HGraphBuilder::GetIntConstant(int32_t constant) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000858 switch (constant) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100859 case 0: return GetIntConstant0();
860 case 1: return GetIntConstant1();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000861 default: {
862 HIntConstant* instruction = new (arena_) HIntConstant(constant);
863 entry_block_->AddInstruction(instruction);
864 return instruction;
865 }
866 }
867}
868
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100869HLongConstant* HGraphBuilder::GetLongConstant(int64_t constant) {
870 HLongConstant* instruction = new (arena_) HLongConstant(constant);
871 entry_block_->AddInstruction(instruction);
872 return instruction;
873}
874
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000875HLocal* HGraphBuilder::GetLocalAt(int register_index) const {
876 return locals_.Get(register_index);
877}
878
879void HGraphBuilder::UpdateLocal(int register_index, HInstruction* instruction) const {
880 HLocal* local = GetLocalAt(register_index);
881 current_block_->AddInstruction(new (arena_) HStoreLocal(local, instruction));
882}
883
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100884HInstruction* HGraphBuilder::LoadLocal(int register_index, Primitive::Type type) const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000885 HLocal* local = GetLocalAt(register_index);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100886 current_block_->AddInstruction(new (arena_) HLoadLocal(local, type));
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000887 return current_block_->GetLastInstruction();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000888}
889
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000890} // namespace art