blob: 4d71cb780a9339735c94f6def559a7030f92ff7b [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 */
Nicolas Geoffray39468442014-09-02 15:17:15 +010057 GetGraph()->GetMaximumNumberOfOutVRegs()
58 + 1 /* current method */);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +010059 GenerateFrameEntry();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010060
Nicolas Geoffray8a16d972014-09-11 10:30:02 +010061 HGraphVisitor* location_builder = GetLocationBuilder();
62 HGraphVisitor* instruction_visitor = GetInstructionVisitor();
Nicolas Geoffray804d0932014-05-02 08:46:00 +010063 for (size_t i = 0, e = blocks.Size(); i < e; ++i) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010064 HBasicBlock* block = blocks.Get(i);
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010065 Bind(block);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010066 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
67 HInstruction* current = it.Current();
68 current->Accept(location_builder);
69 InitLocations(current);
70 current->Accept(instruction_visitor);
71 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000072 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +010073 GenerateSlowPaths();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010074
Nicolas Geoffray787c3072014-03-17 10:20:19 +000075 size_t code_size = GetAssembler()->CodeSize();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000076 uint8_t* buffer = allocator->Allocate(code_size);
77 MemoryRegion code(buffer, code_size);
Nicolas Geoffray787c3072014-03-17 10:20:19 +000078 GetAssembler()->FinalizeInstructions(code);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000079}
80
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010081void CodeGenerator::CompileOptimized(CodeAllocator* allocator) {
82 // The frame size has already been computed during register allocation.
83 DCHECK_NE(frame_size_, kUninitializedFrameSize);
84 const GrowableArray<HBasicBlock*>& blocks = GetGraph()->GetBlocks();
85 DCHECK(blocks.Get(0) == GetGraph()->GetEntryBlock());
86 DCHECK(GoesToNextBlock(GetGraph()->GetEntryBlock(), blocks.Get(1)));
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010087 Initialize();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010088
89 GenerateFrameEntry();
Nicolas Geoffray8a16d972014-09-11 10:30:02 +010090 HGraphVisitor* instruction_visitor = GetInstructionVisitor();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010091 for (size_t i = 0, e = blocks.Size(); i < e; ++i) {
92 HBasicBlock* block = blocks.Get(i);
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010093 Bind(block);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010094 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
95 HInstruction* current = it.Current();
96 current->Accept(instruction_visitor);
97 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000098 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +010099 GenerateSlowPaths();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100100
101 size_t code_size = GetAssembler()->CodeSize();
102 uint8_t* buffer = allocator->Allocate(code_size);
103 MemoryRegion code(buffer, code_size);
104 GetAssembler()->FinalizeInstructions(code);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000105}
106
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100107void CodeGenerator::GenerateSlowPaths() {
108 for (size_t i = 0, e = slow_paths_.Size(); i < e; ++i) {
109 slow_paths_.Get(i)->EmitNativeCode(this);
110 }
111}
112
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100113size_t CodeGenerator::FindFreeEntry(bool* array, size_t length) {
114 for (size_t i = 0; i < length; ++i) {
115 if (!array[i]) {
116 array[i] = true;
117 return i;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100118 }
119 }
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100120 LOG(FATAL) << "Could not find a register in baseline register allocator";
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000121 UNREACHABLE();
122 return -1;
123}
124
Nicolas Geoffray3c035032014-10-28 10:46:40 +0000125size_t CodeGenerator::FindTwoFreeConsecutiveAlignedEntries(bool* array, size_t length) {
126 for (size_t i = 0; i < length - 1; i += 2) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000127 if (!array[i] && !array[i + 1]) {
128 array[i] = true;
129 array[i + 1] = true;
130 return i;
131 }
132 }
133 LOG(FATAL) << "Could not find a register in baseline register allocator";
134 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100135 return -1;
136}
137
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100138void CodeGenerator::ComputeFrameSize(size_t number_of_spill_slots,
139 size_t maximum_number_of_live_registers,
140 size_t number_of_out_slots) {
141 first_register_slot_in_slow_path_ = (number_of_out_slots + number_of_spill_slots) * kVRegSize;
142
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100143 SetFrameSize(RoundUp(
144 number_of_spill_slots * kVRegSize
Nicolas Geoffray39468442014-09-02 15:17:15 +0100145 + number_of_out_slots * kVRegSize
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100146 + maximum_number_of_live_registers * GetWordSize()
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100147 + FrameEntrySpillSize(),
148 kStackAlignment));
149}
150
151Location CodeGenerator::GetTemporaryLocation(HTemporary* temp) const {
152 uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs();
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000153 // The type of the previous instruction tells us if we need a single or double stack slot.
154 Primitive::Type type = temp->GetType();
155 int32_t temp_size = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble) ? 2 : 1;
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100156 // Use the temporary region (right below the dex registers).
157 int32_t slot = GetFrameSize() - FrameEntrySpillSize()
158 - kVRegSize // filler
159 - (number_of_locals * kVRegSize)
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000160 - ((temp_size + temp->GetIndex()) * kVRegSize);
161 return temp_size == 2 ? Location::DoubleStackSlot(slot) : Location::StackSlot(slot);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100162}
163
164int32_t CodeGenerator::GetStackSlot(HLocal* local) const {
165 uint16_t reg_number = local->GetRegNumber();
166 uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs();
167 if (reg_number >= number_of_locals) {
168 // Local is a parameter of the method. It is stored in the caller's frame.
169 return GetFrameSize() + kVRegSize // ART method
170 + (reg_number - number_of_locals) * kVRegSize;
171 } else {
172 // Local is a temporary in this method. It is stored in this method's frame.
173 return GetFrameSize() - FrameEntrySpillSize()
174 - kVRegSize // filler.
175 - (number_of_locals * kVRegSize)
176 + (reg_number * kVRegSize);
177 }
178}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100179
180void CodeGenerator::AllocateRegistersLocally(HInstruction* instruction) const {
181 LocationSummary* locations = instruction->GetLocations();
182 if (locations == nullptr) return;
183
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100184 for (size_t i = 0, e = GetNumberOfCoreRegisters(); i < e; ++i) {
185 blocked_core_registers_[i] = false;
186 }
187
188 for (size_t i = 0, e = GetNumberOfFloatingPointRegisters(); i < e; ++i) {
189 blocked_fpu_registers_[i] = false;
190 }
191
192 for (size_t i = 0, e = number_of_register_pairs_; i < e; ++i) {
193 blocked_register_pairs_[i] = false;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100194 }
195
196 // Mark all fixed input, temp and output registers as used.
197 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
198 Location loc = locations->InAt(i);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100199 // The DCHECKS below check that a register is not specified twice in
200 // the summary.
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100201 if (loc.IsRegister()) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100202 DCHECK(!blocked_core_registers_[loc.reg()]);
203 blocked_core_registers_[loc.reg()] = true;
204 } else if (loc.IsFpuRegister()) {
205 DCHECK(!blocked_fpu_registers_[loc.reg()]);
206 blocked_fpu_registers_[loc.reg()] = true;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000207 } else if (loc.IsFpuRegisterPair()) {
208 DCHECK(!blocked_fpu_registers_[loc.AsFpuRegisterPairLow<int>()]);
209 blocked_fpu_registers_[loc.AsFpuRegisterPairLow<int>()] = true;
210 DCHECK(!blocked_fpu_registers_[loc.AsFpuRegisterPairHigh<int>()]);
211 blocked_fpu_registers_[loc.AsFpuRegisterPairHigh<int>()] = true;
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100212 } else if (loc.IsRegisterPair()) {
213 DCHECK(!blocked_core_registers_[loc.AsRegisterPairLow<int>()]);
214 blocked_core_registers_[loc.AsRegisterPairLow<int>()] = true;
215 DCHECK(!blocked_core_registers_[loc.AsRegisterPairHigh<int>()]);
216 blocked_core_registers_[loc.AsRegisterPairHigh<int>()] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100217 }
218 }
219
220 for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) {
221 Location loc = locations->GetTemp(i);
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000222 // The DCHECKS below check that a register is not specified twice in
223 // the summary.
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100224 if (loc.IsRegister()) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100225 DCHECK(!blocked_core_registers_[loc.reg()]);
226 blocked_core_registers_[loc.reg()] = true;
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000227 } else if (loc.IsFpuRegister()) {
228 DCHECK(!blocked_fpu_registers_[loc.reg()]);
229 blocked_fpu_registers_[loc.reg()] = true;
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100230 } else {
231 DCHECK_EQ(loc.GetPolicy(), Location::kRequiresRegister);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100232 }
233 }
234
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100235 SetupBlockedRegisters();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100236
237 // Allocate all unallocated input locations.
238 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
239 Location loc = locations->InAt(i);
240 HInstruction* input = instruction->InputAt(i);
241 if (loc.IsUnallocated()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100242 if ((loc.GetPolicy() == Location::kRequiresRegister)
243 || (loc.GetPolicy() == Location::kRequiresFpuRegister)) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100244 loc = AllocateFreeRegister(input->GetType());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100245 } else {
246 DCHECK_EQ(loc.GetPolicy(), Location::kAny);
247 HLoadLocal* load = input->AsLoadLocal();
248 if (load != nullptr) {
249 loc = GetStackLocation(load);
250 } else {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100251 loc = AllocateFreeRegister(input->GetType());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100252 }
253 }
254 locations->SetInAt(i, loc);
255 }
256 }
257
258 // Allocate all unallocated temp locations.
259 for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) {
260 Location loc = locations->GetTemp(i);
261 if (loc.IsUnallocated()) {
262 DCHECK_EQ(loc.GetPolicy(), Location::kRequiresRegister);
263 // TODO: Adjust handling of temps. We currently consider temps to use
264 // core registers. They may also use floating point registers at some point.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100265 loc = AllocateFreeRegister(Primitive::kPrimInt);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100266 locations->SetTempAt(i, loc);
267 }
268 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100269 Location result_location = locations->Out();
270 if (result_location.IsUnallocated()) {
271 switch (result_location.GetPolicy()) {
272 case Location::kAny:
273 case Location::kRequiresRegister:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100274 case Location::kRequiresFpuRegister:
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100275 result_location = AllocateFreeRegister(instruction->GetType());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100276 break;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100277 case Location::kSameAsFirstInput:
278 result_location = locations->InAt(0);
279 break;
280 }
281 locations->SetOut(result_location);
282 }
283}
284
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000285void CodeGenerator::InitLocations(HInstruction* instruction) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100286 if (instruction->GetLocations() == nullptr) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100287 if (instruction->IsTemporary()) {
288 HInstruction* previous = instruction->GetPrevious();
289 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
290 Move(previous, temp_location, instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100291 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100292 return;
293 }
294 AllocateRegistersLocally(instruction);
295 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000296 Location location = instruction->GetLocations()->InAt(i);
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000297 HInstruction* input = instruction->InputAt(i);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000298 if (location.IsValid()) {
299 // Move the input to the desired location.
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000300 if (input->GetNext()->IsTemporary()) {
301 // If the input was stored in a temporary, use that temporary to
302 // perform the move.
303 Move(input->GetNext(), location, instruction);
304 } else {
305 Move(input, location, instruction);
306 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000307 }
308 }
309}
310
311bool CodeGenerator::GoesToNextBlock(HBasicBlock* current, HBasicBlock* next) const {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000312 // We currently iterate over the block in insertion order.
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000313 return current->GetBlockId() + 1 == next->GetBlockId();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000314}
315
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000316CodeGenerator* CodeGenerator::Create(ArenaAllocator* allocator,
317 HGraph* graph,
318 InstructionSet instruction_set) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000319 switch (instruction_set) {
320 case kArm:
321 case kThumb2: {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000322 return new (allocator) arm::CodeGeneratorARM(graph);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000323 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100324 case kArm64: {
325 return new (allocator) arm64::CodeGeneratorARM64(graph);
326 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000327 case kMips:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000328 return nullptr;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000329 case kX86: {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000330 return new (allocator) x86::CodeGeneratorX86(graph);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000331 }
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700332 case kX86_64: {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100333 return new (allocator) x86_64::CodeGeneratorX86_64(graph);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700334 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000335 default:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000336 return nullptr;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000337 }
338}
339
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000340void CodeGenerator::BuildNativeGCMap(
341 std::vector<uint8_t>* data, const DexCompilationUnit& dex_compilation_unit) const {
342 const std::vector<uint8_t>& gc_map_raw =
343 dex_compilation_unit.GetVerifiedMethod()->GetDexGcMap();
344 verifier::DexPcToReferenceMap dex_gc_map(&(gc_map_raw)[0]);
345
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000346 uint32_t max_native_offset = 0;
347 for (size_t i = 0; i < pc_infos_.Size(); i++) {
348 uint32_t native_offset = pc_infos_.Get(i).native_pc;
349 if (native_offset > max_native_offset) {
350 max_native_offset = native_offset;
351 }
352 }
353
354 GcMapBuilder builder(data, pc_infos_.Size(), max_native_offset, dex_gc_map.RegWidth());
355 for (size_t i = 0; i < pc_infos_.Size(); i++) {
356 struct PcInfo pc_info = pc_infos_.Get(i);
357 uint32_t native_offset = pc_info.native_pc;
358 uint32_t dex_pc = pc_info.dex_pc;
359 const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false);
360 CHECK(references != NULL) << "Missing ref for dex pc 0x" << std::hex << dex_pc;
361 builder.AddEntry(native_offset, references);
362 }
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000363}
364
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700365void CodeGenerator::BuildMappingTable(std::vector<uint8_t>* data, SrcMap* src_map) const {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000366 uint32_t pc2dex_data_size = 0u;
367 uint32_t pc2dex_entries = pc_infos_.Size();
368 uint32_t pc2dex_offset = 0u;
369 int32_t pc2dex_dalvik_offset = 0;
370 uint32_t dex2pc_data_size = 0u;
371 uint32_t dex2pc_entries = 0u;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000372 uint32_t dex2pc_offset = 0u;
373 int32_t dex2pc_dalvik_offset = 0;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000374
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700375 if (src_map != nullptr) {
376 src_map->reserve(pc2dex_entries);
377 }
378
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000379 for (size_t i = 0; i < pc2dex_entries; i++) {
380 struct PcInfo pc_info = pc_infos_.Get(i);
381 pc2dex_data_size += UnsignedLeb128Size(pc_info.native_pc - pc2dex_offset);
382 pc2dex_data_size += SignedLeb128Size(pc_info.dex_pc - pc2dex_dalvik_offset);
383 pc2dex_offset = pc_info.native_pc;
384 pc2dex_dalvik_offset = pc_info.dex_pc;
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700385 if (src_map != nullptr) {
386 src_map->push_back(SrcMapElem({pc2dex_offset, pc2dex_dalvik_offset}));
387 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000388 }
389
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000390 // Walk over the blocks and find which ones correspond to catch block entries.
391 for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) {
392 HBasicBlock* block = graph_->GetBlocks().Get(i);
393 if (block->IsCatchBlock()) {
394 intptr_t native_pc = GetAddressOf(block);
395 ++dex2pc_entries;
396 dex2pc_data_size += UnsignedLeb128Size(native_pc - dex2pc_offset);
397 dex2pc_data_size += SignedLeb128Size(block->GetDexPc() - dex2pc_dalvik_offset);
398 dex2pc_offset = native_pc;
399 dex2pc_dalvik_offset = block->GetDexPc();
400 }
401 }
402
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000403 uint32_t total_entries = pc2dex_entries + dex2pc_entries;
404 uint32_t hdr_data_size = UnsignedLeb128Size(total_entries) + UnsignedLeb128Size(pc2dex_entries);
405 uint32_t data_size = hdr_data_size + pc2dex_data_size + dex2pc_data_size;
406 data->resize(data_size);
407
408 uint8_t* data_ptr = &(*data)[0];
409 uint8_t* write_pos = data_ptr;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000410
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000411 write_pos = EncodeUnsignedLeb128(write_pos, total_entries);
412 write_pos = EncodeUnsignedLeb128(write_pos, pc2dex_entries);
413 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size);
414 uint8_t* write_pos2 = write_pos + pc2dex_data_size;
415
416 pc2dex_offset = 0u;
417 pc2dex_dalvik_offset = 0u;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000418 dex2pc_offset = 0u;
419 dex2pc_dalvik_offset = 0u;
420
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000421 for (size_t i = 0; i < pc2dex_entries; i++) {
422 struct PcInfo pc_info = pc_infos_.Get(i);
423 DCHECK(pc2dex_offset <= pc_info.native_pc);
424 write_pos = EncodeUnsignedLeb128(write_pos, pc_info.native_pc - pc2dex_offset);
425 write_pos = EncodeSignedLeb128(write_pos, pc_info.dex_pc - pc2dex_dalvik_offset);
426 pc2dex_offset = pc_info.native_pc;
427 pc2dex_dalvik_offset = pc_info.dex_pc;
428 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000429
430 for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) {
431 HBasicBlock* block = graph_->GetBlocks().Get(i);
432 if (block->IsCatchBlock()) {
433 intptr_t native_pc = GetAddressOf(block);
434 write_pos2 = EncodeUnsignedLeb128(write_pos2, native_pc - dex2pc_offset);
435 write_pos2 = EncodeSignedLeb128(write_pos2, block->GetDexPc() - dex2pc_dalvik_offset);
436 dex2pc_offset = native_pc;
437 dex2pc_dalvik_offset = block->GetDexPc();
438 }
439 }
440
441
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000442 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size + pc2dex_data_size);
443 DCHECK_EQ(static_cast<size_t>(write_pos2 - data_ptr), data_size);
444
445 if (kIsDebugBuild) {
446 // Verify the encoded table holds the expected data.
447 MappingTable table(data_ptr);
448 CHECK_EQ(table.TotalSize(), total_entries);
449 CHECK_EQ(table.PcToDexSize(), pc2dex_entries);
450 auto it = table.PcToDexBegin();
451 auto it2 = table.DexToPcBegin();
452 for (size_t i = 0; i < pc2dex_entries; i++) {
453 struct PcInfo pc_info = pc_infos_.Get(i);
454 CHECK_EQ(pc_info.native_pc, it.NativePcOffset());
455 CHECK_EQ(pc_info.dex_pc, it.DexPc());
456 ++it;
457 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000458 for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) {
459 HBasicBlock* block = graph_->GetBlocks().Get(i);
460 if (block->IsCatchBlock()) {
461 CHECK_EQ(GetAddressOf(block), it2.NativePcOffset());
462 CHECK_EQ(block->GetDexPc(), it2.DexPc());
463 ++it2;
464 }
465 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000466 CHECK(it == table.PcToDexEnd());
467 CHECK(it2 == table.DexToPcEnd());
468 }
469}
470
471void CodeGenerator::BuildVMapTable(std::vector<uint8_t>* data) const {
472 Leb128EncodingVector vmap_encoder;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100473 // We currently don't use callee-saved registers.
474 size_t size = 0 + 1 /* marker */ + 0;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000475 vmap_encoder.Reserve(size + 1u); // All values are likely to be one byte in ULEB128 (<128).
476 vmap_encoder.PushBackUnsigned(size);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000477 vmap_encoder.PushBackUnsigned(VmapTable::kAdjustedFpMarker);
478
479 *data = vmap_encoder.GetData();
480}
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000481
Nicolas Geoffray39468442014-09-02 15:17:15 +0100482void CodeGenerator::BuildStackMaps(std::vector<uint8_t>* data) {
483 uint32_t size = stack_map_stream_.ComputeNeededSize();
484 data->resize(size);
485 MemoryRegion region(data->data(), size);
486 stack_map_stream_.FillIn(region);
487}
488
489void CodeGenerator::RecordPcInfo(HInstruction* instruction, uint32_t dex_pc) {
490 // Collect PC infos for the mapping table.
491 struct PcInfo pc_info;
492 pc_info.dex_pc = dex_pc;
493 pc_info.native_pc = GetAssembler()->CodeSize();
494 pc_infos_.Add(pc_info);
495
496 // Populate stack map information.
497
498 if (instruction == nullptr) {
499 // For stack overflow checks.
500 stack_map_stream_.AddStackMapEntry(dex_pc, pc_info.native_pc, 0, 0, 0, 0);
501 return;
502 }
503
504 LocationSummary* locations = instruction->GetLocations();
505 HEnvironment* environment = instruction->GetEnvironment();
506
507 size_t environment_size = instruction->EnvironmentSize();
508
509 size_t register_mask = 0;
510 size_t inlining_depth = 0;
511 stack_map_stream_.AddStackMapEntry(
512 dex_pc, pc_info.native_pc, register_mask,
513 locations->GetStackMask(), environment_size, inlining_depth);
514
515 // Walk over the environment, and record the location of dex registers.
516 for (size_t i = 0; i < environment_size; ++i) {
517 HInstruction* current = environment->GetInstructionAt(i);
518 if (current == nullptr) {
519 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kNone, 0);
520 continue;
521 }
522
523 Location location = locations->GetEnvironmentAt(i);
524 switch (location.GetKind()) {
525 case Location::kConstant: {
526 DCHECK(current == location.GetConstant());
527 if (current->IsLongConstant()) {
528 int64_t value = current->AsLongConstant()->GetValue();
529 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, Low32Bits(value));
530 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, High32Bits(value));
531 ++i;
532 DCHECK_LT(i, environment_size);
533 } else {
534 DCHECK(current->IsIntConstant());
535 int32_t value = current->AsIntConstant()->GetValue();
536 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, value);
537 }
538 break;
539 }
540
541 case Location::kStackSlot: {
542 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInStack, location.GetStackIndex());
543 break;
544 }
545
546 case Location::kDoubleStackSlot: {
547 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInStack, location.GetStackIndex());
548 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInStack,
549 location.GetHighStackIndex(kVRegSize));
550 ++i;
551 DCHECK_LT(i, environment_size);
552 break;
553 }
554
555 case Location::kRegister : {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100556 int id = location.reg();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100557 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInRegister, id);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100558 if (current->GetType() == Primitive::kPrimLong) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100559 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInRegister, id);
560 ++i;
561 DCHECK_LT(i, environment_size);
562 }
563 break;
564 }
565
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100566 case Location::kFpuRegister : {
567 int id = location.reg();
568 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInFpuRegister, id);
569 if (current->GetType() == Primitive::kPrimDouble) {
570 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInFpuRegister, id);
571 ++i;
572 DCHECK_LT(i, environment_size);
573 }
574 break;
575 }
576
Nicolas Geoffray39468442014-09-02 15:17:15 +0100577 default:
578 LOG(FATAL) << "Unexpected kind " << location.GetKind();
579 }
580 }
581}
582
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100583void CodeGenerator::SaveLiveRegisters(LocationSummary* locations) {
584 RegisterSet* register_set = locations->GetLiveRegisters();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100585 size_t stack_offset = first_register_slot_in_slow_path_;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100586 for (size_t i = 0, e = GetNumberOfCoreRegisters(); i < e; ++i) {
587 if (register_set->ContainsCoreRegister(i)) {
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100588 // If the register holds an object, update the stack mask.
589 if (locations->RegisterContainsObject(i)) {
590 locations->SetStackBit(stack_offset / kVRegSize);
591 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100592 stack_offset += SaveCoreRegister(stack_offset, i);
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100593 }
594 }
595
596 for (size_t i = 0, e = GetNumberOfFloatingPointRegisters(); i < e; ++i) {
597 if (register_set->ContainsFloatingPointRegister(i)) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100598 stack_offset += SaveFloatingPointRegister(stack_offset, i);
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100599 }
600 }
601}
602
603void CodeGenerator::RestoreLiveRegisters(LocationSummary* locations) {
604 RegisterSet* register_set = locations->GetLiveRegisters();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100605 size_t stack_offset = first_register_slot_in_slow_path_;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100606 for (size_t i = 0, e = GetNumberOfCoreRegisters(); i < e; ++i) {
607 if (register_set->ContainsCoreRegister(i)) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100608 stack_offset += RestoreCoreRegister(stack_offset, i);
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100609 }
610 }
611
612 for (size_t i = 0, e = GetNumberOfFloatingPointRegisters(); i < e; ++i) {
613 if (register_set->ContainsFloatingPointRegister(i)) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100614 stack_offset += RestoreFloatingPointRegister(stack_offset, i);
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100615 }
616 }
617}
618
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100619void CodeGenerator::ClearSpillSlotsFromLoopPhisInStackMap(HSuspendCheck* suspend_check) const {
620 LocationSummary* locations = suspend_check->GetLocations();
621 HBasicBlock* block = suspend_check->GetBlock();
622 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == suspend_check);
623 DCHECK(block->IsLoopHeader());
624
625 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
626 HInstruction* current = it.Current();
627 LiveInterval* interval = current->GetLiveInterval();
628 // We only need to clear bits of loop phis containing objects and allocated in register.
629 // Loop phis allocated on stack already have the object in the stack.
630 if (current->GetType() == Primitive::kPrimNot
631 && interval->HasRegister()
632 && interval->HasSpillSlot()) {
633 locations->ClearStackBit(interval->GetSpillSlot() / kVRegSize);
634 }
635 }
636}
637
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000638void CodeGenerator::EmitParallelMoves(Location from1, Location to1, Location from2, Location to2) {
639 MoveOperands move1(from1, to1, nullptr);
640 MoveOperands move2(from2, to2, nullptr);
641 HParallelMove parallel_move(GetGraph()->GetArena());
642 parallel_move.AddMove(&move1);
643 parallel_move.AddMove(&move2);
644 GetMoveResolver()->EmitNativeCode(&parallel_move);
645}
646
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000647} // namespace art