blob: 0af70f9b907675be3fa80b314096cec2c248f3bb [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) {
143 first_register_slot_in_slow_path_ = (number_of_out_slots + number_of_spill_slots) * kVRegSize;
144
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100145 SetFrameSize(RoundUp(
146 number_of_spill_slots * kVRegSize
Nicolas Geoffray39468442014-09-02 15:17:15 +0100147 + number_of_out_slots * kVRegSize
Mark Mendellf85a9ca2015-01-13 09:20:58 -0500148 + maximum_number_of_live_core_registers * GetWordSize()
149 + maximum_number_of_live_fp_registers * GetFloatingPointSpillSlotSize()
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100150 + FrameEntrySpillSize(),
151 kStackAlignment));
152}
153
154Location CodeGenerator::GetTemporaryLocation(HTemporary* temp) const {
155 uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs();
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000156 // The type of the previous instruction tells us if we need a single or double stack slot.
157 Primitive::Type type = temp->GetType();
158 int32_t temp_size = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble) ? 2 : 1;
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100159 // Use the temporary region (right below the dex registers).
160 int32_t slot = GetFrameSize() - FrameEntrySpillSize()
161 - kVRegSize // filler
162 - (number_of_locals * kVRegSize)
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000163 - ((temp_size + temp->GetIndex()) * kVRegSize);
164 return temp_size == 2 ? Location::DoubleStackSlot(slot) : Location::StackSlot(slot);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100165}
166
167int32_t CodeGenerator::GetStackSlot(HLocal* local) const {
168 uint16_t reg_number = local->GetRegNumber();
169 uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs();
170 if (reg_number >= number_of_locals) {
171 // Local is a parameter of the method. It is stored in the caller's frame.
172 return GetFrameSize() + kVRegSize // ART method
173 + (reg_number - number_of_locals) * kVRegSize;
174 } else {
175 // Local is a temporary in this method. It is stored in this method's frame.
176 return GetFrameSize() - FrameEntrySpillSize()
177 - kVRegSize // filler.
178 - (number_of_locals * kVRegSize)
179 + (reg_number * kVRegSize);
180 }
181}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100182
183void CodeGenerator::AllocateRegistersLocally(HInstruction* instruction) const {
184 LocationSummary* locations = instruction->GetLocations();
185 if (locations == nullptr) return;
186
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100187 for (size_t i = 0, e = GetNumberOfCoreRegisters(); i < e; ++i) {
188 blocked_core_registers_[i] = false;
189 }
190
191 for (size_t i = 0, e = GetNumberOfFloatingPointRegisters(); i < e; ++i) {
192 blocked_fpu_registers_[i] = false;
193 }
194
195 for (size_t i = 0, e = number_of_register_pairs_; i < e; ++i) {
196 blocked_register_pairs_[i] = false;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100197 }
198
199 // Mark all fixed input, temp and output registers as used.
200 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
201 Location loc = locations->InAt(i);
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000202 // The DCHECKS below check that a register is not specified twice in
203 // the summary.
204 if (loc.IsRegister()) {
205 DCHECK(!blocked_core_registers_[loc.reg()]);
206 blocked_core_registers_[loc.reg()] = true;
207 } else if (loc.IsFpuRegister()) {
208 DCHECK(!blocked_fpu_registers_[loc.reg()]);
209 blocked_fpu_registers_[loc.reg()] = true;
210 } else if (loc.IsFpuRegisterPair()) {
211 DCHECK(!blocked_fpu_registers_[loc.AsFpuRegisterPairLow<int>()]);
212 blocked_fpu_registers_[loc.AsFpuRegisterPairLow<int>()] = true;
213 DCHECK(!blocked_fpu_registers_[loc.AsFpuRegisterPairHigh<int>()]);
214 blocked_fpu_registers_[loc.AsFpuRegisterPairHigh<int>()] = true;
215 } else if (loc.IsRegisterPair()) {
216 DCHECK(!blocked_core_registers_[loc.AsRegisterPairLow<int>()]);
217 blocked_core_registers_[loc.AsRegisterPairLow<int>()] = true;
218 DCHECK(!blocked_core_registers_[loc.AsRegisterPairHigh<int>()]);
219 blocked_core_registers_[loc.AsRegisterPairHigh<int>()] = true;
220 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100221 }
222
223 for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) {
224 Location loc = locations->GetTemp(i);
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000225 // The DCHECKS below check that a register is not specified twice in
226 // the summary.
227 if (loc.IsRegister()) {
228 DCHECK(!blocked_core_registers_[loc.reg()]);
229 blocked_core_registers_[loc.reg()] = true;
230 } else if (loc.IsFpuRegister()) {
231 DCHECK(!blocked_fpu_registers_[loc.reg()]);
232 blocked_fpu_registers_[loc.reg()] = true;
233 } else {
234 DCHECK(loc.GetPolicy() == Location::kRequiresRegister
235 || loc.GetPolicy() == Location::kRequiresFpuRegister);
236 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100237 }
238
Nicolas Geoffray98893962015-01-21 12:32:32 +0000239 static constexpr bool kBaseline = true;
240 SetupBlockedRegisters(kBaseline);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100241
242 // Allocate all unallocated input locations.
243 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
244 Location loc = locations->InAt(i);
245 HInstruction* input = instruction->InputAt(i);
246 if (loc.IsUnallocated()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100247 if ((loc.GetPolicy() == Location::kRequiresRegister)
248 || (loc.GetPolicy() == Location::kRequiresFpuRegister)) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100249 loc = AllocateFreeRegister(input->GetType());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100250 } else {
251 DCHECK_EQ(loc.GetPolicy(), Location::kAny);
252 HLoadLocal* load = input->AsLoadLocal();
253 if (load != nullptr) {
254 loc = GetStackLocation(load);
255 } else {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100256 loc = AllocateFreeRegister(input->GetType());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100257 }
258 }
259 locations->SetInAt(i, loc);
260 }
261 }
262
263 // Allocate all unallocated temp locations.
264 for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) {
265 Location loc = locations->GetTemp(i);
266 if (loc.IsUnallocated()) {
Roland Levillain647b9ed2014-11-27 12:06:00 +0000267 switch (loc.GetPolicy()) {
268 case Location::kRequiresRegister:
269 // Allocate a core register (large enough to fit a 32-bit integer).
270 loc = AllocateFreeRegister(Primitive::kPrimInt);
271 break;
272
273 case Location::kRequiresFpuRegister:
274 // Allocate a core register (large enough to fit a 64-bit double).
275 loc = AllocateFreeRegister(Primitive::kPrimDouble);
276 break;
277
278 default:
279 LOG(FATAL) << "Unexpected policy for temporary location "
280 << loc.GetPolicy();
281 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100282 locations->SetTempAt(i, loc);
283 }
284 }
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000285 Location result_location = locations->Out();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100286 if (result_location.IsUnallocated()) {
287 switch (result_location.GetPolicy()) {
288 case Location::kAny:
289 case Location::kRequiresRegister:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100290 case Location::kRequiresFpuRegister:
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100291 result_location = AllocateFreeRegister(instruction->GetType());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100292 break;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100293 case Location::kSameAsFirstInput:
294 result_location = locations->InAt(0);
295 break;
296 }
297 locations->SetOut(result_location);
298 }
299}
300
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000301void CodeGenerator::InitLocations(HInstruction* instruction) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100302 if (instruction->GetLocations() == nullptr) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100303 if (instruction->IsTemporary()) {
304 HInstruction* previous = instruction->GetPrevious();
305 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
306 Move(previous, temp_location, instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100307 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100308 return;
309 }
310 AllocateRegistersLocally(instruction);
311 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000312 Location location = instruction->GetLocations()->InAt(i);
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000313 HInstruction* input = instruction->InputAt(i);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000314 if (location.IsValid()) {
315 // Move the input to the desired location.
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000316 if (input->GetNext()->IsTemporary()) {
317 // If the input was stored in a temporary, use that temporary to
318 // perform the move.
319 Move(input->GetNext(), location, instruction);
320 } else {
321 Move(input, location, instruction);
322 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000323 }
324 }
325}
326
327bool CodeGenerator::GoesToNextBlock(HBasicBlock* current, HBasicBlock* next) const {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000328 // We currently iterate over the block in insertion order.
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000329 return current->GetBlockId() + 1 == next->GetBlockId();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000330}
331
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000332CodeGenerator* CodeGenerator::Create(HGraph* graph,
Calin Juravle34166012014-12-19 17:22:29 +0000333 InstructionSet instruction_set,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000334 const InstructionSetFeatures& isa_features,
335 const CompilerOptions& compiler_options) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000336 switch (instruction_set) {
337 case kArm:
338 case kThumb2: {
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000339 return new arm::CodeGeneratorARM(graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000340 *isa_features.AsArmInstructionSetFeatures(),
341 compiler_options);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000342 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100343 case kArm64: {
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000344 return new arm64::CodeGeneratorARM64(graph, compiler_options);
Alexandre Rames5319def2014-10-23 10:03:10 +0100345 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000346 case kMips:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000347 return nullptr;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000348 case kX86: {
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000349 return new x86::CodeGeneratorX86(graph, compiler_options);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000350 }
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700351 case kX86_64: {
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000352 return new x86_64::CodeGeneratorX86_64(graph, compiler_options);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700353 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000354 default:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000355 return nullptr;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000356 }
357}
358
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000359void CodeGenerator::BuildNativeGCMap(
360 std::vector<uint8_t>* data, const DexCompilationUnit& dex_compilation_unit) const {
361 const std::vector<uint8_t>& gc_map_raw =
362 dex_compilation_unit.GetVerifiedMethod()->GetDexGcMap();
363 verifier::DexPcToReferenceMap dex_gc_map(&(gc_map_raw)[0]);
364
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000365 uint32_t max_native_offset = 0;
366 for (size_t i = 0; i < pc_infos_.Size(); i++) {
367 uint32_t native_offset = pc_infos_.Get(i).native_pc;
368 if (native_offset > max_native_offset) {
369 max_native_offset = native_offset;
370 }
371 }
372
373 GcMapBuilder builder(data, pc_infos_.Size(), max_native_offset, dex_gc_map.RegWidth());
374 for (size_t i = 0; i < pc_infos_.Size(); i++) {
375 struct PcInfo pc_info = pc_infos_.Get(i);
376 uint32_t native_offset = pc_info.native_pc;
377 uint32_t dex_pc = pc_info.dex_pc;
378 const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false);
Jean Christophe Beyler0ada95d2014-12-04 11:20:20 -0800379 CHECK(references != nullptr) << "Missing ref for dex pc 0x" << std::hex << dex_pc;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000380 builder.AddEntry(native_offset, references);
381 }
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000382}
383
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800384void CodeGenerator::BuildMappingTable(std::vector<uint8_t>* data, DefaultSrcMap* src_map) const {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000385 uint32_t pc2dex_data_size = 0u;
386 uint32_t pc2dex_entries = pc_infos_.Size();
387 uint32_t pc2dex_offset = 0u;
388 int32_t pc2dex_dalvik_offset = 0;
389 uint32_t dex2pc_data_size = 0u;
390 uint32_t dex2pc_entries = 0u;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000391 uint32_t dex2pc_offset = 0u;
392 int32_t dex2pc_dalvik_offset = 0;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000393
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700394 if (src_map != nullptr) {
395 src_map->reserve(pc2dex_entries);
396 }
397
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000398 for (size_t i = 0; i < pc2dex_entries; i++) {
399 struct PcInfo pc_info = pc_infos_.Get(i);
400 pc2dex_data_size += UnsignedLeb128Size(pc_info.native_pc - pc2dex_offset);
401 pc2dex_data_size += SignedLeb128Size(pc_info.dex_pc - pc2dex_dalvik_offset);
402 pc2dex_offset = pc_info.native_pc;
403 pc2dex_dalvik_offset = pc_info.dex_pc;
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700404 if (src_map != nullptr) {
405 src_map->push_back(SrcMapElem({pc2dex_offset, pc2dex_dalvik_offset}));
406 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000407 }
408
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000409 // Walk over the blocks and find which ones correspond to catch block entries.
410 for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) {
411 HBasicBlock* block = graph_->GetBlocks().Get(i);
412 if (block->IsCatchBlock()) {
413 intptr_t native_pc = GetAddressOf(block);
414 ++dex2pc_entries;
415 dex2pc_data_size += UnsignedLeb128Size(native_pc - dex2pc_offset);
416 dex2pc_data_size += SignedLeb128Size(block->GetDexPc() - dex2pc_dalvik_offset);
417 dex2pc_offset = native_pc;
418 dex2pc_dalvik_offset = block->GetDexPc();
419 }
420 }
421
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000422 uint32_t total_entries = pc2dex_entries + dex2pc_entries;
423 uint32_t hdr_data_size = UnsignedLeb128Size(total_entries) + UnsignedLeb128Size(pc2dex_entries);
424 uint32_t data_size = hdr_data_size + pc2dex_data_size + dex2pc_data_size;
425 data->resize(data_size);
426
427 uint8_t* data_ptr = &(*data)[0];
428 uint8_t* write_pos = data_ptr;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000429
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000430 write_pos = EncodeUnsignedLeb128(write_pos, total_entries);
431 write_pos = EncodeUnsignedLeb128(write_pos, pc2dex_entries);
432 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size);
433 uint8_t* write_pos2 = write_pos + pc2dex_data_size;
434
435 pc2dex_offset = 0u;
436 pc2dex_dalvik_offset = 0u;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000437 dex2pc_offset = 0u;
438 dex2pc_dalvik_offset = 0u;
439
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000440 for (size_t i = 0; i < pc2dex_entries; i++) {
441 struct PcInfo pc_info = pc_infos_.Get(i);
442 DCHECK(pc2dex_offset <= pc_info.native_pc);
443 write_pos = EncodeUnsignedLeb128(write_pos, pc_info.native_pc - pc2dex_offset);
444 write_pos = EncodeSignedLeb128(write_pos, pc_info.dex_pc - pc2dex_dalvik_offset);
445 pc2dex_offset = pc_info.native_pc;
446 pc2dex_dalvik_offset = pc_info.dex_pc;
447 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000448
449 for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) {
450 HBasicBlock* block = graph_->GetBlocks().Get(i);
451 if (block->IsCatchBlock()) {
452 intptr_t native_pc = GetAddressOf(block);
453 write_pos2 = EncodeUnsignedLeb128(write_pos2, native_pc - dex2pc_offset);
454 write_pos2 = EncodeSignedLeb128(write_pos2, block->GetDexPc() - dex2pc_dalvik_offset);
455 dex2pc_offset = native_pc;
456 dex2pc_dalvik_offset = block->GetDexPc();
457 }
458 }
459
460
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000461 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size + pc2dex_data_size);
462 DCHECK_EQ(static_cast<size_t>(write_pos2 - data_ptr), data_size);
463
464 if (kIsDebugBuild) {
465 // Verify the encoded table holds the expected data.
466 MappingTable table(data_ptr);
467 CHECK_EQ(table.TotalSize(), total_entries);
468 CHECK_EQ(table.PcToDexSize(), pc2dex_entries);
469 auto it = table.PcToDexBegin();
470 auto it2 = table.DexToPcBegin();
471 for (size_t i = 0; i < pc2dex_entries; i++) {
472 struct PcInfo pc_info = pc_infos_.Get(i);
473 CHECK_EQ(pc_info.native_pc, it.NativePcOffset());
474 CHECK_EQ(pc_info.dex_pc, it.DexPc());
475 ++it;
476 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000477 for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) {
478 HBasicBlock* block = graph_->GetBlocks().Get(i);
479 if (block->IsCatchBlock()) {
480 CHECK_EQ(GetAddressOf(block), it2.NativePcOffset());
481 CHECK_EQ(block->GetDexPc(), it2.DexPc());
482 ++it2;
483 }
484 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000485 CHECK(it == table.PcToDexEnd());
486 CHECK(it2 == table.DexToPcEnd());
487 }
488}
489
490void CodeGenerator::BuildVMapTable(std::vector<uint8_t>* data) const {
491 Leb128EncodingVector vmap_encoder;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100492 // We currently don't use callee-saved registers.
493 size_t size = 0 + 1 /* marker */ + 0;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000494 vmap_encoder.Reserve(size + 1u); // All values are likely to be one byte in ULEB128 (<128).
495 vmap_encoder.PushBackUnsigned(size);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000496 vmap_encoder.PushBackUnsigned(VmapTable::kAdjustedFpMarker);
497
498 *data = vmap_encoder.GetData();
499}
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000500
Nicolas Geoffray39468442014-09-02 15:17:15 +0100501void CodeGenerator::BuildStackMaps(std::vector<uint8_t>* data) {
502 uint32_t size = stack_map_stream_.ComputeNeededSize();
503 data->resize(size);
504 MemoryRegion region(data->data(), size);
505 stack_map_stream_.FillIn(region);
506}
507
508void CodeGenerator::RecordPcInfo(HInstruction* instruction, uint32_t dex_pc) {
Calin Juravled2ec87d2014-12-08 14:24:46 +0000509 if (instruction != nullptr) {
Roland Levillain624279f2014-12-04 11:54:28 +0000510 // The code generated for some type conversions may call the
511 // runtime, thus normally requiring a subsequent call to this
512 // method. However, the method verifier does not produce PC
Calin Juravled2ec87d2014-12-08 14:24:46 +0000513 // information for certain instructions, which are considered "atomic"
514 // (they cannot join a GC).
Roland Levillain624279f2014-12-04 11:54:28 +0000515 // Therefore we do not currently record PC information for such
516 // instructions. As this may change later, we added this special
517 // case so that code generators may nevertheless call
518 // CodeGenerator::RecordPcInfo without triggering an error in
519 // CodeGenerator::BuildNativeGCMap ("Missing ref for dex pc 0x")
520 // thereafter.
Calin Juravled2ec87d2014-12-08 14:24:46 +0000521 if (instruction->IsTypeConversion()) {
522 return;
523 }
524 if (instruction->IsRem()) {
525 Primitive::Type type = instruction->AsRem()->GetResultType();
526 if ((type == Primitive::kPrimFloat) || (type == Primitive::kPrimDouble)) {
527 return;
528 }
529 }
Roland Levillain624279f2014-12-04 11:54:28 +0000530 }
531
Nicolas Geoffray39468442014-09-02 15:17:15 +0100532 // Collect PC infos for the mapping table.
533 struct PcInfo pc_info;
534 pc_info.dex_pc = dex_pc;
535 pc_info.native_pc = GetAssembler()->CodeSize();
536 pc_infos_.Add(pc_info);
537
538 // Populate stack map information.
539
540 if (instruction == nullptr) {
541 // For stack overflow checks.
542 stack_map_stream_.AddStackMapEntry(dex_pc, pc_info.native_pc, 0, 0, 0, 0);
543 return;
544 }
545
546 LocationSummary* locations = instruction->GetLocations();
547 HEnvironment* environment = instruction->GetEnvironment();
548
549 size_t environment_size = instruction->EnvironmentSize();
550
Nicolas Geoffray39468442014-09-02 15:17:15 +0100551 size_t inlining_depth = 0;
Nicolas Geoffray98893962015-01-21 12:32:32 +0000552 uint32_t register_mask = locations->GetRegisterMask();
553 if (locations->OnlyCallsOnSlowPath()) {
554 // In case of slow path, we currently set the location of caller-save registers
555 // to register (instead of their stack location when pushed before the slow-path
556 // call). Therefore register_mask contains both callee-save and caller-save
557 // registers that hold objects. We must remove the caller-save from the mask, since
558 // they will be overwritten by the callee.
559 register_mask &= core_callee_save_mask_;
560 }
561 // The register mask must be a subset of callee-save registers.
562 DCHECK_EQ(register_mask & core_callee_save_mask_, register_mask);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100563 stack_map_stream_.AddStackMapEntry(
564 dex_pc, pc_info.native_pc, register_mask,
565 locations->GetStackMask(), environment_size, inlining_depth);
566
567 // Walk over the environment, and record the location of dex registers.
568 for (size_t i = 0; i < environment_size; ++i) {
569 HInstruction* current = environment->GetInstructionAt(i);
570 if (current == nullptr) {
571 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kNone, 0);
572 continue;
573 }
574
575 Location location = locations->GetEnvironmentAt(i);
576 switch (location.GetKind()) {
577 case Location::kConstant: {
578 DCHECK(current == location.GetConstant());
579 if (current->IsLongConstant()) {
580 int64_t value = current->AsLongConstant()->GetValue();
581 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, Low32Bits(value));
582 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, High32Bits(value));
583 ++i;
584 DCHECK_LT(i, environment_size);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000585 } else if (current->IsDoubleConstant()) {
586 int64_t value = bit_cast<double, int64_t>(current->AsDoubleConstant()->GetValue());
587 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, Low32Bits(value));
588 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, High32Bits(value));
589 ++i;
590 DCHECK_LT(i, environment_size);
591 } else if (current->IsIntConstant()) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100592 int32_t value = current->AsIntConstant()->GetValue();
593 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, value);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000594 } else {
595 DCHECK(current->IsFloatConstant());
596 int32_t value = bit_cast<float, int32_t>(current->AsFloatConstant()->GetValue());
597 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, value);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100598 }
599 break;
600 }
601
602 case Location::kStackSlot: {
603 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInStack, location.GetStackIndex());
604 break;
605 }
606
607 case Location::kDoubleStackSlot: {
608 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInStack, location.GetStackIndex());
609 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInStack,
610 location.GetHighStackIndex(kVRegSize));
611 ++i;
612 DCHECK_LT(i, environment_size);
613 break;
614 }
615
616 case Location::kRegister : {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100617 int id = location.reg();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100618 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInRegister, id);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100619 if (current->GetType() == Primitive::kPrimLong) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100620 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInRegister, id);
621 ++i;
622 DCHECK_LT(i, environment_size);
623 }
624 break;
625 }
626
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100627 case Location::kFpuRegister : {
628 int id = location.reg();
629 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInFpuRegister, id);
630 if (current->GetType() == Primitive::kPrimDouble) {
631 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInFpuRegister, id);
632 ++i;
633 DCHECK_LT(i, environment_size);
634 }
635 break;
636 }
637
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000638 case Location::kFpuRegisterPair : {
639 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInFpuRegister, location.low());
640 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInFpuRegister, location.high());
641 ++i;
642 DCHECK_LT(i, environment_size);
643 break;
644 }
645
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000646 case Location::kRegisterPair : {
647 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInRegister, location.low());
648 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInRegister, location.high());
649 ++i;
650 DCHECK_LT(i, environment_size);
651 break;
652 }
653
Nicolas Geoffray39468442014-09-02 15:17:15 +0100654 default:
655 LOG(FATAL) << "Unexpected kind " << location.GetKind();
656 }
657 }
658}
659
Calin Juravle77520bc2015-01-12 18:45:46 +0000660bool CodeGenerator::CanMoveNullCheckToUser(HNullCheck* null_check) {
661 HInstruction* first_next_not_move = null_check->GetNextDisregardingMoves();
662 return (first_next_not_move != nullptr) && first_next_not_move->CanDoImplicitNullCheck();
663}
664
665void CodeGenerator::MaybeRecordImplicitNullCheck(HInstruction* instr) {
666 // If we are from a static path don't record the pc as we can't throw NPE.
667 // NB: having the checks here makes the code much less verbose in the arch
668 // specific code generators.
669 if (instr->IsStaticFieldSet() || instr->IsStaticFieldGet()) {
670 return;
671 }
672
673 if (!compiler_options_.GetImplicitNullChecks()) {
674 return;
675 }
676
677 if (!instr->CanDoImplicitNullCheck()) {
678 return;
679 }
680
681 // Find the first previous instruction which is not a move.
682 HInstruction* first_prev_not_move = instr->GetPreviousDisregardingMoves();
683
684 // If the instruction is a null check it means that `instr` is the first user
685 // and needs to record the pc.
686 if (first_prev_not_move != nullptr && first_prev_not_move->IsNullCheck()) {
687 HNullCheck* null_check = first_prev_not_move->AsNullCheck();
688 // TODO: The parallel moves modify the environment. Their changes need to be reverted
689 // otherwise the stack maps at the throw point will not be correct.
690 RecordPcInfo(null_check, null_check->GetDexPc());
691 }
692}
693
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100694void CodeGenerator::SaveLiveRegisters(LocationSummary* locations) {
695 RegisterSet* register_set = locations->GetLiveRegisters();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100696 size_t stack_offset = first_register_slot_in_slow_path_;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100697 for (size_t i = 0, e = GetNumberOfCoreRegisters(); i < e; ++i) {
Nicolas Geoffray98893962015-01-21 12:32:32 +0000698 if (!IsCoreCalleeSaveRegister(i)) {
699 if (register_set->ContainsCoreRegister(i)) {
700 // If the register holds an object, update the stack mask.
701 if (locations->RegisterContainsObject(i)) {
702 locations->SetStackBit(stack_offset / kVRegSize);
703 }
704 DCHECK_LT(stack_offset, GetFrameSize() - FrameEntrySpillSize());
705 stack_offset += SaveCoreRegister(stack_offset, i);
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100706 }
707 }
708 }
709
710 for (size_t i = 0, e = GetNumberOfFloatingPointRegisters(); i < e; ++i) {
Nicolas Geoffray98893962015-01-21 12:32:32 +0000711 if (!IsFloatingPointCalleeSaveRegister(i)) {
712 if (register_set->ContainsFloatingPointRegister(i)) {
713 DCHECK_LT(stack_offset, GetFrameSize() - FrameEntrySpillSize());
714 stack_offset += SaveFloatingPointRegister(stack_offset, i);
715 }
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100716 }
717 }
718}
719
720void CodeGenerator::RestoreLiveRegisters(LocationSummary* locations) {
721 RegisterSet* register_set = locations->GetLiveRegisters();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100722 size_t stack_offset = first_register_slot_in_slow_path_;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100723 for (size_t i = 0, e = GetNumberOfCoreRegisters(); i < e; ++i) {
Nicolas Geoffray98893962015-01-21 12:32:32 +0000724 if (!IsCoreCalleeSaveRegister(i)) {
725 if (register_set->ContainsCoreRegister(i)) {
726 DCHECK_LT(stack_offset, GetFrameSize() - FrameEntrySpillSize());
727 stack_offset += RestoreCoreRegister(stack_offset, i);
728 }
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100729 }
730 }
731
732 for (size_t i = 0, e = GetNumberOfFloatingPointRegisters(); i < e; ++i) {
Nicolas Geoffray98893962015-01-21 12:32:32 +0000733 if (!IsFloatingPointCalleeSaveRegister(i)) {
734 if (register_set->ContainsFloatingPointRegister(i)) {
735 DCHECK_LT(stack_offset, GetFrameSize() - FrameEntrySpillSize());
736 stack_offset += RestoreFloatingPointRegister(stack_offset, i);
737 }
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100738 }
739 }
740}
741
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100742void CodeGenerator::ClearSpillSlotsFromLoopPhisInStackMap(HSuspendCheck* suspend_check) const {
743 LocationSummary* locations = suspend_check->GetLocations();
744 HBasicBlock* block = suspend_check->GetBlock();
745 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == suspend_check);
746 DCHECK(block->IsLoopHeader());
747
748 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
749 HInstruction* current = it.Current();
750 LiveInterval* interval = current->GetLiveInterval();
751 // We only need to clear bits of loop phis containing objects and allocated in register.
752 // Loop phis allocated on stack already have the object in the stack.
753 if (current->GetType() == Primitive::kPrimNot
754 && interval->HasRegister()
755 && interval->HasSpillSlot()) {
756 locations->ClearStackBit(interval->GetSpillSlot() / kVRegSize);
757 }
758 }
759}
760
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000761void CodeGenerator::EmitParallelMoves(Location from1, Location to1, Location from2, Location to2) {
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000762 HParallelMove parallel_move(GetGraph()->GetArena());
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +0000763 parallel_move.AddMove(from1, to1, nullptr);
764 parallel_move.AddMove(from2, to2, nullptr);
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000765 GetMoveResolver()->EmitNativeCode(&parallel_move);
766}
767
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000768} // namespace art