blob: 0a405c4bbe20ca351f354372b63909f862099864 [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * 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
17#include "code_generator.h"
18
19#include "code_generator_arm.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010020#include "code_generator_arm64.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000021#include "code_generator_x86.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010022#include "code_generator_x86_64.h"
Yevgeny Roubane3ea8382014-08-08 16:29:38 +070023#include "compiled_method.h"
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000024#include "dex/verified_method.h"
25#include "driver/dex_compilation_unit.h"
26#include "gc_map_builder.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000027#include "leb128.h"
28#include "mapping_table.h"
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010029#include "mirror/array-inl.h"
30#include "mirror/object_array-inl.h"
31#include "mirror/object_reference.h"
Nicolas Geoffray3c049742014-09-24 18:10:46 +010032#include "ssa_liveness_analysis.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000033#include "utils/assembler.h"
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000034#include "verifier/dex_gc_map.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000035#include "vmap_table.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000036
37namespace art {
38
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010039size_t CodeGenerator::GetCacheOffset(uint32_t index) {
40 return mirror::ObjectArray<mirror::Object>::OffsetOfElement(index).SizeValue();
41}
42
Nicolas Geoffray73e80c32014-07-22 17:47:56 +010043void CodeGenerator::CompileBaseline(CodeAllocator* allocator, bool is_leaf) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +010044 const GrowableArray<HBasicBlock*>& blocks = GetGraph()->GetBlocks();
45 DCHECK(blocks.Get(0) == GetGraph()->GetEntryBlock());
46 DCHECK(GoesToNextBlock(GetGraph()->GetEntryBlock(), blocks.Get(1)));
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010047 Initialize();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010048
49 DCHECK_EQ(frame_size_, kUninitializedFrameSize);
Nicolas Geoffray73e80c32014-07-22 17:47:56 +010050 if (!is_leaf) {
51 MarkNotLeaf();
52 }
Nicolas Geoffray39468442014-09-02 15:17:15 +010053 ComputeFrameSize(GetGraph()->GetNumberOfLocalVRegs()
Calin Juravlef97f9fb2014-11-11 15:38:19 +000054 + GetGraph()->GetTemporariesVRegSlots()
Nicolas Geoffray39468442014-09-02 15:17:15 +010055 + 1 /* filler */,
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +010056 0, /* the baseline compiler does not have live registers at slow path */
Mark Mendellf85a9ca2015-01-13 09:20:58 -050057 0, /* the baseline compiler does not have live registers at slow path */
Nicolas Geoffray39468442014-09-02 15:17:15 +010058 GetGraph()->GetMaximumNumberOfOutVRegs()
59 + 1 /* current method */);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +010060 GenerateFrameEntry();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010061
Nicolas Geoffray8a16d972014-09-11 10:30:02 +010062 HGraphVisitor* location_builder = GetLocationBuilder();
63 HGraphVisitor* instruction_visitor = GetInstructionVisitor();
Nicolas Geoffray804d0932014-05-02 08:46:00 +010064 for (size_t i = 0, e = blocks.Size(); i < e; ++i) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010065 HBasicBlock* block = blocks.Get(i);
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010066 Bind(block);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010067 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
68 HInstruction* current = it.Current();
69 current->Accept(location_builder);
70 InitLocations(current);
71 current->Accept(instruction_visitor);
72 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000073 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +010074 GenerateSlowPaths();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +000075 Finalize(allocator);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000076}
77
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010078void CodeGenerator::CompileOptimized(CodeAllocator* allocator) {
79 // The frame size has already been computed during register allocation.
80 DCHECK_NE(frame_size_, kUninitializedFrameSize);
81 const GrowableArray<HBasicBlock*>& blocks = GetGraph()->GetBlocks();
82 DCHECK(blocks.Get(0) == GetGraph()->GetEntryBlock());
83 DCHECK(GoesToNextBlock(GetGraph()->GetEntryBlock(), blocks.Get(1)));
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010084 Initialize();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010085
86 GenerateFrameEntry();
Nicolas Geoffray8a16d972014-09-11 10:30:02 +010087 HGraphVisitor* instruction_visitor = GetInstructionVisitor();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010088 for (size_t i = 0, e = blocks.Size(); i < e; ++i) {
89 HBasicBlock* block = blocks.Get(i);
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010090 Bind(block);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010091 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
92 HInstruction* current = it.Current();
93 current->Accept(instruction_visitor);
94 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000095 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +010096 GenerateSlowPaths();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +000097 Finalize(allocator);
98}
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010099
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000100void CodeGenerator::Finalize(CodeAllocator* allocator) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100101 size_t code_size = GetAssembler()->CodeSize();
102 uint8_t* buffer = allocator->Allocate(code_size);
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000103
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100104 MemoryRegion code(buffer, code_size);
105 GetAssembler()->FinalizeInstructions(code);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000106}
107
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100108void CodeGenerator::GenerateSlowPaths() {
109 for (size_t i = 0, e = slow_paths_.Size(); i < e; ++i) {
110 slow_paths_.Get(i)->EmitNativeCode(this);
111 }
112}
113
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100114size_t CodeGenerator::FindFreeEntry(bool* array, size_t length) {
115 for (size_t i = 0; i < length; ++i) {
116 if (!array[i]) {
117 array[i] = true;
118 return i;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100119 }
120 }
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100121 LOG(FATAL) << "Could not find a register in baseline register allocator";
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000122 UNREACHABLE();
123 return -1;
124}
125
Nicolas Geoffray3c035032014-10-28 10:46:40 +0000126size_t CodeGenerator::FindTwoFreeConsecutiveAlignedEntries(bool* array, size_t length) {
127 for (size_t i = 0; i < length - 1; i += 2) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000128 if (!array[i] && !array[i + 1]) {
129 array[i] = true;
130 array[i + 1] = true;
131 return i;
132 }
133 }
134 LOG(FATAL) << "Could not find a register in baseline register allocator";
135 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100136 return -1;
137}
138
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100139void CodeGenerator::ComputeFrameSize(size_t number_of_spill_slots,
Mark Mendellf85a9ca2015-01-13 09:20:58 -0500140 size_t maximum_number_of_live_core_registers,
141 size_t maximum_number_of_live_fp_registers,
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100142 size_t number_of_out_slots) {
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000143 ComputeSpillMask();
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100144 first_register_slot_in_slow_path_ = (number_of_out_slots + number_of_spill_slots) * kVRegSize;
145
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100146 SetFrameSize(RoundUp(
147 number_of_spill_slots * kVRegSize
Nicolas Geoffray39468442014-09-02 15:17:15 +0100148 + number_of_out_slots * kVRegSize
Mark Mendellf85a9ca2015-01-13 09:20:58 -0500149 + maximum_number_of_live_core_registers * GetWordSize()
150 + maximum_number_of_live_fp_registers * GetFloatingPointSpillSlotSize()
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100151 + FrameEntrySpillSize(),
152 kStackAlignment));
153}
154
155Location CodeGenerator::GetTemporaryLocation(HTemporary* temp) const {
156 uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs();
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000157 // The type of the previous instruction tells us if we need a single or double stack slot.
158 Primitive::Type type = temp->GetType();
159 int32_t temp_size = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble) ? 2 : 1;
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100160 // Use the temporary region (right below the dex registers).
161 int32_t slot = GetFrameSize() - FrameEntrySpillSize()
162 - kVRegSize // filler
163 - (number_of_locals * kVRegSize)
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000164 - ((temp_size + temp->GetIndex()) * kVRegSize);
165 return temp_size == 2 ? Location::DoubleStackSlot(slot) : Location::StackSlot(slot);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100166}
167
168int32_t CodeGenerator::GetStackSlot(HLocal* local) const {
169 uint16_t reg_number = local->GetRegNumber();
170 uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs();
171 if (reg_number >= number_of_locals) {
172 // Local is a parameter of the method. It is stored in the caller's frame.
173 return GetFrameSize() + kVRegSize // ART method
174 + (reg_number - number_of_locals) * kVRegSize;
175 } else {
176 // Local is a temporary in this method. It is stored in this method's frame.
177 return GetFrameSize() - FrameEntrySpillSize()
178 - kVRegSize // filler.
179 - (number_of_locals * kVRegSize)
180 + (reg_number * kVRegSize);
181 }
182}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100183
184void CodeGenerator::AllocateRegistersLocally(HInstruction* instruction) const {
185 LocationSummary* locations = instruction->GetLocations();
186 if (locations == nullptr) return;
187
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100188 for (size_t i = 0, e = GetNumberOfCoreRegisters(); i < e; ++i) {
189 blocked_core_registers_[i] = false;
190 }
191
192 for (size_t i = 0, e = GetNumberOfFloatingPointRegisters(); i < e; ++i) {
193 blocked_fpu_registers_[i] = false;
194 }
195
196 for (size_t i = 0, e = number_of_register_pairs_; i < e; ++i) {
197 blocked_register_pairs_[i] = false;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100198 }
199
200 // Mark all fixed input, temp and output registers as used.
201 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
202 Location loc = locations->InAt(i);
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000203 // The DCHECKS below check that a register is not specified twice in
204 // the summary.
205 if (loc.IsRegister()) {
206 DCHECK(!blocked_core_registers_[loc.reg()]);
207 blocked_core_registers_[loc.reg()] = true;
208 } else if (loc.IsFpuRegister()) {
209 DCHECK(!blocked_fpu_registers_[loc.reg()]);
210 blocked_fpu_registers_[loc.reg()] = true;
211 } else if (loc.IsFpuRegisterPair()) {
212 DCHECK(!blocked_fpu_registers_[loc.AsFpuRegisterPairLow<int>()]);
213 blocked_fpu_registers_[loc.AsFpuRegisterPairLow<int>()] = true;
214 DCHECK(!blocked_fpu_registers_[loc.AsFpuRegisterPairHigh<int>()]);
215 blocked_fpu_registers_[loc.AsFpuRegisterPairHigh<int>()] = true;
216 } else if (loc.IsRegisterPair()) {
217 DCHECK(!blocked_core_registers_[loc.AsRegisterPairLow<int>()]);
218 blocked_core_registers_[loc.AsRegisterPairLow<int>()] = true;
219 DCHECK(!blocked_core_registers_[loc.AsRegisterPairHigh<int>()]);
220 blocked_core_registers_[loc.AsRegisterPairHigh<int>()] = true;
221 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100222 }
223
224 for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) {
225 Location loc = locations->GetTemp(i);
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000226 // The DCHECKS below check that a register is not specified twice in
227 // the summary.
228 if (loc.IsRegister()) {
229 DCHECK(!blocked_core_registers_[loc.reg()]);
230 blocked_core_registers_[loc.reg()] = true;
231 } else if (loc.IsFpuRegister()) {
232 DCHECK(!blocked_fpu_registers_[loc.reg()]);
233 blocked_fpu_registers_[loc.reg()] = true;
234 } else {
235 DCHECK(loc.GetPolicy() == Location::kRequiresRegister
236 || loc.GetPolicy() == Location::kRequiresFpuRegister);
237 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100238 }
239
Nicolas Geoffray98893962015-01-21 12:32:32 +0000240 static constexpr bool kBaseline = true;
241 SetupBlockedRegisters(kBaseline);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100242
243 // Allocate all unallocated input locations.
244 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
245 Location loc = locations->InAt(i);
246 HInstruction* input = instruction->InputAt(i);
247 if (loc.IsUnallocated()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100248 if ((loc.GetPolicy() == Location::kRequiresRegister)
249 || (loc.GetPolicy() == Location::kRequiresFpuRegister)) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100250 loc = AllocateFreeRegister(input->GetType());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100251 } else {
252 DCHECK_EQ(loc.GetPolicy(), Location::kAny);
253 HLoadLocal* load = input->AsLoadLocal();
254 if (load != nullptr) {
255 loc = GetStackLocation(load);
256 } else {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100257 loc = AllocateFreeRegister(input->GetType());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100258 }
259 }
260 locations->SetInAt(i, loc);
261 }
262 }
263
264 // Allocate all unallocated temp locations.
265 for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) {
266 Location loc = locations->GetTemp(i);
267 if (loc.IsUnallocated()) {
Roland Levillain647b9ed2014-11-27 12:06:00 +0000268 switch (loc.GetPolicy()) {
269 case Location::kRequiresRegister:
270 // Allocate a core register (large enough to fit a 32-bit integer).
271 loc = AllocateFreeRegister(Primitive::kPrimInt);
272 break;
273
274 case Location::kRequiresFpuRegister:
275 // Allocate a core register (large enough to fit a 64-bit double).
276 loc = AllocateFreeRegister(Primitive::kPrimDouble);
277 break;
278
279 default:
280 LOG(FATAL) << "Unexpected policy for temporary location "
281 << loc.GetPolicy();
282 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100283 locations->SetTempAt(i, loc);
284 }
285 }
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000286 Location result_location = locations->Out();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100287 if (result_location.IsUnallocated()) {
288 switch (result_location.GetPolicy()) {
289 case Location::kAny:
290 case Location::kRequiresRegister:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100291 case Location::kRequiresFpuRegister:
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100292 result_location = AllocateFreeRegister(instruction->GetType());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100293 break;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100294 case Location::kSameAsFirstInput:
295 result_location = locations->InAt(0);
296 break;
297 }
298 locations->SetOut(result_location);
299 }
300}
301
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000302void CodeGenerator::InitLocations(HInstruction* instruction) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100303 if (instruction->GetLocations() == nullptr) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100304 if (instruction->IsTemporary()) {
305 HInstruction* previous = instruction->GetPrevious();
306 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
307 Move(previous, temp_location, instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100308 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100309 return;
310 }
311 AllocateRegistersLocally(instruction);
312 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000313 Location location = instruction->GetLocations()->InAt(i);
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000314 HInstruction* input = instruction->InputAt(i);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000315 if (location.IsValid()) {
316 // Move the input to the desired location.
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000317 if (input->GetNext()->IsTemporary()) {
318 // If the input was stored in a temporary, use that temporary to
319 // perform the move.
320 Move(input->GetNext(), location, instruction);
321 } else {
322 Move(input, location, instruction);
323 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000324 }
325 }
326}
327
328bool CodeGenerator::GoesToNextBlock(HBasicBlock* current, HBasicBlock* next) const {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000329 // We currently iterate over the block in insertion order.
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000330 return current->GetBlockId() + 1 == next->GetBlockId();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000331}
332
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000333CodeGenerator* CodeGenerator::Create(HGraph* graph,
Calin Juravle34166012014-12-19 17:22:29 +0000334 InstructionSet instruction_set,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000335 const InstructionSetFeatures& isa_features,
336 const CompilerOptions& compiler_options) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000337 switch (instruction_set) {
338 case kArm:
339 case kThumb2: {
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000340 return new arm::CodeGeneratorARM(graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000341 *isa_features.AsArmInstructionSetFeatures(),
342 compiler_options);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000343 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100344 case kArm64: {
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000345 return new arm64::CodeGeneratorARM64(graph, compiler_options);
Alexandre Rames5319def2014-10-23 10:03:10 +0100346 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000347 case kMips:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000348 return nullptr;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000349 case kX86: {
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000350 return new x86::CodeGeneratorX86(graph, compiler_options);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000351 }
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700352 case kX86_64: {
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000353 return new x86_64::CodeGeneratorX86_64(graph, compiler_options);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700354 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000355 default:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000356 return nullptr;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000357 }
358}
359
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000360void CodeGenerator::BuildNativeGCMap(
361 std::vector<uint8_t>* data, const DexCompilationUnit& dex_compilation_unit) const {
362 const std::vector<uint8_t>& gc_map_raw =
363 dex_compilation_unit.GetVerifiedMethod()->GetDexGcMap();
364 verifier::DexPcToReferenceMap dex_gc_map(&(gc_map_raw)[0]);
365
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000366 uint32_t max_native_offset = 0;
367 for (size_t i = 0; i < pc_infos_.Size(); i++) {
368 uint32_t native_offset = pc_infos_.Get(i).native_pc;
369 if (native_offset > max_native_offset) {
370 max_native_offset = native_offset;
371 }
372 }
373
374 GcMapBuilder builder(data, pc_infos_.Size(), max_native_offset, dex_gc_map.RegWidth());
375 for (size_t i = 0; i < pc_infos_.Size(); i++) {
376 struct PcInfo pc_info = pc_infos_.Get(i);
377 uint32_t native_offset = pc_info.native_pc;
378 uint32_t dex_pc = pc_info.dex_pc;
379 const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false);
Jean Christophe Beyler0ada95d2014-12-04 11:20:20 -0800380 CHECK(references != nullptr) << "Missing ref for dex pc 0x" << std::hex << dex_pc;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000381 builder.AddEntry(native_offset, references);
382 }
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000383}
384
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800385void CodeGenerator::BuildMappingTable(std::vector<uint8_t>* data, DefaultSrcMap* src_map) const {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000386 uint32_t pc2dex_data_size = 0u;
387 uint32_t pc2dex_entries = pc_infos_.Size();
388 uint32_t pc2dex_offset = 0u;
389 int32_t pc2dex_dalvik_offset = 0;
390 uint32_t dex2pc_data_size = 0u;
391 uint32_t dex2pc_entries = 0u;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000392 uint32_t dex2pc_offset = 0u;
393 int32_t dex2pc_dalvik_offset = 0;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000394
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700395 if (src_map != nullptr) {
396 src_map->reserve(pc2dex_entries);
397 }
398
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000399 for (size_t i = 0; i < pc2dex_entries; i++) {
400 struct PcInfo pc_info = pc_infos_.Get(i);
401 pc2dex_data_size += UnsignedLeb128Size(pc_info.native_pc - pc2dex_offset);
402 pc2dex_data_size += SignedLeb128Size(pc_info.dex_pc - pc2dex_dalvik_offset);
403 pc2dex_offset = pc_info.native_pc;
404 pc2dex_dalvik_offset = pc_info.dex_pc;
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700405 if (src_map != nullptr) {
406 src_map->push_back(SrcMapElem({pc2dex_offset, pc2dex_dalvik_offset}));
407 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000408 }
409
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000410 // Walk over the blocks and find which ones correspond to catch block entries.
411 for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) {
412 HBasicBlock* block = graph_->GetBlocks().Get(i);
413 if (block->IsCatchBlock()) {
414 intptr_t native_pc = GetAddressOf(block);
415 ++dex2pc_entries;
416 dex2pc_data_size += UnsignedLeb128Size(native_pc - dex2pc_offset);
417 dex2pc_data_size += SignedLeb128Size(block->GetDexPc() - dex2pc_dalvik_offset);
418 dex2pc_offset = native_pc;
419 dex2pc_dalvik_offset = block->GetDexPc();
420 }
421 }
422
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000423 uint32_t total_entries = pc2dex_entries + dex2pc_entries;
424 uint32_t hdr_data_size = UnsignedLeb128Size(total_entries) + UnsignedLeb128Size(pc2dex_entries);
425 uint32_t data_size = hdr_data_size + pc2dex_data_size + dex2pc_data_size;
426 data->resize(data_size);
427
428 uint8_t* data_ptr = &(*data)[0];
429 uint8_t* write_pos = data_ptr;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000430
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000431 write_pos = EncodeUnsignedLeb128(write_pos, total_entries);
432 write_pos = EncodeUnsignedLeb128(write_pos, pc2dex_entries);
433 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size);
434 uint8_t* write_pos2 = write_pos + pc2dex_data_size;
435
436 pc2dex_offset = 0u;
437 pc2dex_dalvik_offset = 0u;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000438 dex2pc_offset = 0u;
439 dex2pc_dalvik_offset = 0u;
440
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000441 for (size_t i = 0; i < pc2dex_entries; i++) {
442 struct PcInfo pc_info = pc_infos_.Get(i);
443 DCHECK(pc2dex_offset <= pc_info.native_pc);
444 write_pos = EncodeUnsignedLeb128(write_pos, pc_info.native_pc - pc2dex_offset);
445 write_pos = EncodeSignedLeb128(write_pos, pc_info.dex_pc - pc2dex_dalvik_offset);
446 pc2dex_offset = pc_info.native_pc;
447 pc2dex_dalvik_offset = pc_info.dex_pc;
448 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000449
450 for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) {
451 HBasicBlock* block = graph_->GetBlocks().Get(i);
452 if (block->IsCatchBlock()) {
453 intptr_t native_pc = GetAddressOf(block);
454 write_pos2 = EncodeUnsignedLeb128(write_pos2, native_pc - dex2pc_offset);
455 write_pos2 = EncodeSignedLeb128(write_pos2, block->GetDexPc() - dex2pc_dalvik_offset);
456 dex2pc_offset = native_pc;
457 dex2pc_dalvik_offset = block->GetDexPc();
458 }
459 }
460
461
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000462 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size + pc2dex_data_size);
463 DCHECK_EQ(static_cast<size_t>(write_pos2 - data_ptr), data_size);
464
465 if (kIsDebugBuild) {
466 // Verify the encoded table holds the expected data.
467 MappingTable table(data_ptr);
468 CHECK_EQ(table.TotalSize(), total_entries);
469 CHECK_EQ(table.PcToDexSize(), pc2dex_entries);
470 auto it = table.PcToDexBegin();
471 auto it2 = table.DexToPcBegin();
472 for (size_t i = 0; i < pc2dex_entries; i++) {
473 struct PcInfo pc_info = pc_infos_.Get(i);
474 CHECK_EQ(pc_info.native_pc, it.NativePcOffset());
475 CHECK_EQ(pc_info.dex_pc, it.DexPc());
476 ++it;
477 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000478 for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) {
479 HBasicBlock* block = graph_->GetBlocks().Get(i);
480 if (block->IsCatchBlock()) {
481 CHECK_EQ(GetAddressOf(block), it2.NativePcOffset());
482 CHECK_EQ(block->GetDexPc(), it2.DexPc());
483 ++it2;
484 }
485 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000486 CHECK(it == table.PcToDexEnd());
487 CHECK(it2 == table.DexToPcEnd());
488 }
489}
490
491void CodeGenerator::BuildVMapTable(std::vector<uint8_t>* data) const {
492 Leb128EncodingVector vmap_encoder;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100493 // We currently don't use callee-saved registers.
494 size_t size = 0 + 1 /* marker */ + 0;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000495 vmap_encoder.Reserve(size + 1u); // All values are likely to be one byte in ULEB128 (<128).
496 vmap_encoder.PushBackUnsigned(size);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000497 vmap_encoder.PushBackUnsigned(VmapTable::kAdjustedFpMarker);
498
499 *data = vmap_encoder.GetData();
500}
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000501
Nicolas Geoffray39468442014-09-02 15:17:15 +0100502void CodeGenerator::BuildStackMaps(std::vector<uint8_t>* data) {
503 uint32_t size = stack_map_stream_.ComputeNeededSize();
504 data->resize(size);
505 MemoryRegion region(data->data(), size);
506 stack_map_stream_.FillIn(region);
507}
508
509void CodeGenerator::RecordPcInfo(HInstruction* instruction, uint32_t dex_pc) {
Calin Juravled2ec87d2014-12-08 14:24:46 +0000510 if (instruction != nullptr) {
Roland Levillain624279f2014-12-04 11:54:28 +0000511 // The code generated for some type conversions may call the
512 // runtime, thus normally requiring a subsequent call to this
513 // method. However, the method verifier does not produce PC
Calin Juravled2ec87d2014-12-08 14:24:46 +0000514 // information for certain instructions, which are considered "atomic"
515 // (they cannot join a GC).
Roland Levillain624279f2014-12-04 11:54:28 +0000516 // Therefore we do not currently record PC information for such
517 // instructions. As this may change later, we added this special
518 // case so that code generators may nevertheless call
519 // CodeGenerator::RecordPcInfo without triggering an error in
520 // CodeGenerator::BuildNativeGCMap ("Missing ref for dex pc 0x")
521 // thereafter.
Calin Juravled2ec87d2014-12-08 14:24:46 +0000522 if (instruction->IsTypeConversion()) {
523 return;
524 }
525 if (instruction->IsRem()) {
526 Primitive::Type type = instruction->AsRem()->GetResultType();
527 if ((type == Primitive::kPrimFloat) || (type == Primitive::kPrimDouble)) {
528 return;
529 }
530 }
Roland Levillain624279f2014-12-04 11:54:28 +0000531 }
532
Nicolas Geoffray39468442014-09-02 15:17:15 +0100533 // Collect PC infos for the mapping table.
534 struct PcInfo pc_info;
535 pc_info.dex_pc = dex_pc;
536 pc_info.native_pc = GetAssembler()->CodeSize();
537 pc_infos_.Add(pc_info);
538
539 // Populate stack map information.
540
541 if (instruction == nullptr) {
542 // For stack overflow checks.
543 stack_map_stream_.AddStackMapEntry(dex_pc, pc_info.native_pc, 0, 0, 0, 0);
544 return;
545 }
546
547 LocationSummary* locations = instruction->GetLocations();
548 HEnvironment* environment = instruction->GetEnvironment();
549
550 size_t environment_size = instruction->EnvironmentSize();
551
Nicolas Geoffray39468442014-09-02 15:17:15 +0100552 size_t inlining_depth = 0;
Nicolas Geoffray98893962015-01-21 12:32:32 +0000553 uint32_t register_mask = locations->GetRegisterMask();
554 if (locations->OnlyCallsOnSlowPath()) {
555 // In case of slow path, we currently set the location of caller-save registers
556 // to register (instead of their stack location when pushed before the slow-path
557 // call). Therefore register_mask contains both callee-save and caller-save
558 // registers that hold objects. We must remove the caller-save from the mask, since
559 // they will be overwritten by the callee.
560 register_mask &= core_callee_save_mask_;
561 }
562 // The register mask must be a subset of callee-save registers.
563 DCHECK_EQ(register_mask & core_callee_save_mask_, register_mask);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100564 stack_map_stream_.AddStackMapEntry(
565 dex_pc, pc_info.native_pc, register_mask,
566 locations->GetStackMask(), environment_size, inlining_depth);
567
568 // Walk over the environment, and record the location of dex registers.
569 for (size_t i = 0; i < environment_size; ++i) {
570 HInstruction* current = environment->GetInstructionAt(i);
571 if (current == nullptr) {
572 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kNone, 0);
573 continue;
574 }
575
576 Location location = locations->GetEnvironmentAt(i);
577 switch (location.GetKind()) {
578 case Location::kConstant: {
579 DCHECK(current == location.GetConstant());
580 if (current->IsLongConstant()) {
581 int64_t value = current->AsLongConstant()->GetValue();
582 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, Low32Bits(value));
583 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, High32Bits(value));
584 ++i;
585 DCHECK_LT(i, environment_size);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000586 } else if (current->IsDoubleConstant()) {
587 int64_t value = bit_cast<double, int64_t>(current->AsDoubleConstant()->GetValue());
588 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, Low32Bits(value));
589 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, High32Bits(value));
590 ++i;
591 DCHECK_LT(i, environment_size);
592 } else if (current->IsIntConstant()) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100593 int32_t value = current->AsIntConstant()->GetValue();
594 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, value);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000595 } else {
596 DCHECK(current->IsFloatConstant());
597 int32_t value = bit_cast<float, int32_t>(current->AsFloatConstant()->GetValue());
598 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, value);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100599 }
600 break;
601 }
602
603 case Location::kStackSlot: {
604 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInStack, location.GetStackIndex());
605 break;
606 }
607
608 case Location::kDoubleStackSlot: {
609 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInStack, location.GetStackIndex());
610 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInStack,
611 location.GetHighStackIndex(kVRegSize));
612 ++i;
613 DCHECK_LT(i, environment_size);
614 break;
615 }
616
617 case Location::kRegister : {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100618 int id = location.reg();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100619 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInRegister, id);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100620 if (current->GetType() == Primitive::kPrimLong) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100621 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInRegister, id);
622 ++i;
623 DCHECK_LT(i, environment_size);
624 }
625 break;
626 }
627
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100628 case Location::kFpuRegister : {
629 int id = location.reg();
630 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInFpuRegister, id);
631 if (current->GetType() == Primitive::kPrimDouble) {
632 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInFpuRegister, id);
633 ++i;
634 DCHECK_LT(i, environment_size);
635 }
636 break;
637 }
638
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000639 case Location::kFpuRegisterPair : {
640 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInFpuRegister, location.low());
641 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInFpuRegister, location.high());
642 ++i;
643 DCHECK_LT(i, environment_size);
644 break;
645 }
646
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000647 case Location::kRegisterPair : {
648 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInRegister, location.low());
649 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInRegister, location.high());
650 ++i;
651 DCHECK_LT(i, environment_size);
652 break;
653 }
654
Nicolas Geoffray39468442014-09-02 15:17:15 +0100655 default:
656 LOG(FATAL) << "Unexpected kind " << location.GetKind();
657 }
658 }
659}
660
Calin Juravle77520bc2015-01-12 18:45:46 +0000661bool CodeGenerator::CanMoveNullCheckToUser(HNullCheck* null_check) {
662 HInstruction* first_next_not_move = null_check->GetNextDisregardingMoves();
663 return (first_next_not_move != nullptr) && first_next_not_move->CanDoImplicitNullCheck();
664}
665
666void CodeGenerator::MaybeRecordImplicitNullCheck(HInstruction* instr) {
667 // If we are from a static path don't record the pc as we can't throw NPE.
668 // NB: having the checks here makes the code much less verbose in the arch
669 // specific code generators.
670 if (instr->IsStaticFieldSet() || instr->IsStaticFieldGet()) {
671 return;
672 }
673
674 if (!compiler_options_.GetImplicitNullChecks()) {
675 return;
676 }
677
678 if (!instr->CanDoImplicitNullCheck()) {
679 return;
680 }
681
682 // Find the first previous instruction which is not a move.
683 HInstruction* first_prev_not_move = instr->GetPreviousDisregardingMoves();
684
685 // If the instruction is a null check it means that `instr` is the first user
686 // and needs to record the pc.
687 if (first_prev_not_move != nullptr && first_prev_not_move->IsNullCheck()) {
688 HNullCheck* null_check = first_prev_not_move->AsNullCheck();
689 // TODO: The parallel moves modify the environment. Their changes need to be reverted
690 // otherwise the stack maps at the throw point will not be correct.
691 RecordPcInfo(null_check, null_check->GetDexPc());
692 }
693}
694
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100695void CodeGenerator::SaveLiveRegisters(LocationSummary* locations) {
696 RegisterSet* register_set = locations->GetLiveRegisters();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100697 size_t stack_offset = first_register_slot_in_slow_path_;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100698 for (size_t i = 0, e = GetNumberOfCoreRegisters(); i < e; ++i) {
Nicolas Geoffray98893962015-01-21 12:32:32 +0000699 if (!IsCoreCalleeSaveRegister(i)) {
700 if (register_set->ContainsCoreRegister(i)) {
701 // If the register holds an object, update the stack mask.
702 if (locations->RegisterContainsObject(i)) {
703 locations->SetStackBit(stack_offset / kVRegSize);
704 }
705 DCHECK_LT(stack_offset, GetFrameSize() - FrameEntrySpillSize());
706 stack_offset += SaveCoreRegister(stack_offset, i);
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100707 }
708 }
709 }
710
711 for (size_t i = 0, e = GetNumberOfFloatingPointRegisters(); i < e; ++i) {
Nicolas Geoffray98893962015-01-21 12:32:32 +0000712 if (!IsFloatingPointCalleeSaveRegister(i)) {
713 if (register_set->ContainsFloatingPointRegister(i)) {
714 DCHECK_LT(stack_offset, GetFrameSize() - FrameEntrySpillSize());
715 stack_offset += SaveFloatingPointRegister(stack_offset, i);
716 }
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100717 }
718 }
719}
720
721void CodeGenerator::RestoreLiveRegisters(LocationSummary* locations) {
722 RegisterSet* register_set = locations->GetLiveRegisters();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100723 size_t stack_offset = first_register_slot_in_slow_path_;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100724 for (size_t i = 0, e = GetNumberOfCoreRegisters(); i < e; ++i) {
Nicolas Geoffray98893962015-01-21 12:32:32 +0000725 if (!IsCoreCalleeSaveRegister(i)) {
726 if (register_set->ContainsCoreRegister(i)) {
727 DCHECK_LT(stack_offset, GetFrameSize() - FrameEntrySpillSize());
728 stack_offset += RestoreCoreRegister(stack_offset, i);
729 }
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100730 }
731 }
732
733 for (size_t i = 0, e = GetNumberOfFloatingPointRegisters(); i < e; ++i) {
Nicolas Geoffray98893962015-01-21 12:32:32 +0000734 if (!IsFloatingPointCalleeSaveRegister(i)) {
735 if (register_set->ContainsFloatingPointRegister(i)) {
736 DCHECK_LT(stack_offset, GetFrameSize() - FrameEntrySpillSize());
737 stack_offset += RestoreFloatingPointRegister(stack_offset, i);
738 }
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100739 }
740 }
741}
742
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100743void CodeGenerator::ClearSpillSlotsFromLoopPhisInStackMap(HSuspendCheck* suspend_check) const {
744 LocationSummary* locations = suspend_check->GetLocations();
745 HBasicBlock* block = suspend_check->GetBlock();
746 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == suspend_check);
747 DCHECK(block->IsLoopHeader());
748
749 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
750 HInstruction* current = it.Current();
751 LiveInterval* interval = current->GetLiveInterval();
752 // We only need to clear bits of loop phis containing objects and allocated in register.
753 // Loop phis allocated on stack already have the object in the stack.
754 if (current->GetType() == Primitive::kPrimNot
755 && interval->HasRegister()
756 && interval->HasSpillSlot()) {
757 locations->ClearStackBit(interval->GetSpillSlot() / kVRegSize);
758 }
759 }
760}
761
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000762void CodeGenerator::EmitParallelMoves(Location from1, Location to1, Location from2, Location to2) {
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000763 HParallelMove parallel_move(GetGraph()->GetArena());
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +0000764 parallel_move.AddMove(from1, to1, nullptr);
765 parallel_move.AddMove(from2, to2, nullptr);
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000766 GetMoveResolver()->EmitNativeCode(&parallel_move);
767}
768
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000769} // namespace art