blob: d0739a6de2b14dfe19bc371d1e539b01608c79f8 [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 Geoffray4c204ba2015-02-03 15:12:35 +000044 Initialize();
Nicolas Geoffray73e80c32014-07-22 17:47:56 +010045 if (!is_leaf) {
46 MarkNotLeaf();
47 }
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +000048 InitializeCodeGeneration(GetGraph()->GetNumberOfLocalVRegs()
49 + GetGraph()->GetTemporariesVRegSlots()
50 + 1 /* filler */,
51 0, /* the baseline compiler does not have live registers at slow path */
52 0, /* the baseline compiler does not have live registers at slow path */
53 GetGraph()->GetMaximumNumberOfOutVRegs()
54 + 1 /* current method */,
55 GetGraph()->GetBlocks());
56 CompileInternal(allocator, /* is_baseline */ true);
57}
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010058
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +000059void CodeGenerator::CompileInternal(CodeAllocator* allocator, bool is_baseline) {
Nicolas Geoffray8a16d972014-09-11 10:30:02 +010060 HGraphVisitor* instruction_visitor = GetInstructionVisitor();
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +000061 DCHECK_EQ(current_block_index_, 0u);
62 GenerateFrameEntry();
63 for (size_t e = block_order_->Size(); current_block_index_ < e; ++current_block_index_) {
64 HBasicBlock* block = block_order_->Get(current_block_index_);
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();
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +000068 if (is_baseline) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +000069 InitLocationsBaseline(current);
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +000070 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010071 current->Accept(instruction_visitor);
72 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000073 }
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +000074
75 // Generate the slow paths.
76 for (size_t i = 0, e = slow_paths_.Size(); i < e; ++i) {
77 slow_paths_.Get(i)->EmitNativeCode(this);
78 }
79
80 // Finalize instructions in assember;
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +000081 Finalize(allocator);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000082}
83
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010084void CodeGenerator::CompileOptimized(CodeAllocator* allocator) {
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +000085 // The register allocator already called `InitializeCodeGeneration`,
86 // where the frame size has been computed.
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +000087 DCHECK(block_order_ != nullptr);
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010088 Initialize();
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +000089 CompileInternal(allocator, /* is_baseline */ false);
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +000090}
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010091
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +000092void CodeGenerator::Finalize(CodeAllocator* allocator) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010093 size_t code_size = GetAssembler()->CodeSize();
94 uint8_t* buffer = allocator->Allocate(code_size);
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +000095
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010096 MemoryRegion code(buffer, code_size);
97 GetAssembler()->FinalizeInstructions(code);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000098}
99
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100100size_t CodeGenerator::FindFreeEntry(bool* array, size_t length) {
101 for (size_t i = 0; i < length; ++i) {
102 if (!array[i]) {
103 array[i] = true;
104 return i;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100105 }
106 }
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100107 LOG(FATAL) << "Could not find a register in baseline register allocator";
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000108 UNREACHABLE();
109 return -1;
110}
111
Nicolas Geoffray3c035032014-10-28 10:46:40 +0000112size_t CodeGenerator::FindTwoFreeConsecutiveAlignedEntries(bool* array, size_t length) {
113 for (size_t i = 0; i < length - 1; i += 2) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000114 if (!array[i] && !array[i + 1]) {
115 array[i] = true;
116 array[i + 1] = true;
117 return i;
118 }
119 }
120 LOG(FATAL) << "Could not find a register in baseline register allocator";
121 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100122 return -1;
123}
124
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000125void CodeGenerator::InitializeCodeGeneration(size_t number_of_spill_slots,
126 size_t maximum_number_of_live_core_registers,
127 size_t maximum_number_of_live_fp_registers,
128 size_t number_of_out_slots,
129 const GrowableArray<HBasicBlock*>& block_order) {
130 block_order_ = &block_order;
131 DCHECK(block_order_->Get(0) == GetGraph()->GetEntryBlock());
132 DCHECK(GoesToNextBlock(GetGraph()->GetEntryBlock(), block_order_->Get(1)));
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000133 ComputeSpillMask();
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100134 first_register_slot_in_slow_path_ = (number_of_out_slots + number_of_spill_slots) * kVRegSize;
135
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000136 if (number_of_spill_slots == 0
137 && !HasAllocatedCalleeSaveRegisters()
138 && IsLeafMethod()
139 && !RequiresCurrentMethod()) {
140 DCHECK_EQ(maximum_number_of_live_core_registers, 0u);
141 DCHECK_EQ(maximum_number_of_live_fp_registers, 0u);
142 SetFrameSize(CallPushesPC() ? GetWordSize() : 0);
143 } else {
144 SetFrameSize(RoundUp(
145 number_of_spill_slots * kVRegSize
146 + number_of_out_slots * kVRegSize
147 + maximum_number_of_live_core_registers * GetWordSize()
148 + maximum_number_of_live_fp_registers * GetFloatingPointSpillSlotSize()
149 + FrameEntrySpillSize(),
150 kStackAlignment));
151 }
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100152}
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 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000297 locations->UpdateOut(result_location);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100298 }
299}
300
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000301void CodeGenerator::InitLocationsBaseline(HInstruction* instruction) {
302 AllocateLocations(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
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000328void CodeGenerator::AllocateLocations(HInstruction* instruction) {
329 instruction->Accept(GetLocationBuilder());
330 LocationSummary* locations = instruction->GetLocations();
331 if (!instruction->IsSuspendCheckEntry()) {
332 if (locations != nullptr && locations->CanCall()) {
333 MarkNotLeaf();
334 }
335 if (instruction->NeedsCurrentMethod()) {
336 SetRequiresCurrentMethod();
337 }
338 }
339}
340
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000341bool CodeGenerator::GoesToNextBlock(HBasicBlock* current, HBasicBlock* next) const {
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000342 DCHECK_EQ(block_order_->Get(current_block_index_), current);
343 return (current_block_index_ < block_order_->Size() - 1)
344 && (block_order_->Get(current_block_index_ + 1) == next);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000345}
346
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000347CodeGenerator* CodeGenerator::Create(HGraph* graph,
Calin Juravle34166012014-12-19 17:22:29 +0000348 InstructionSet instruction_set,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000349 const InstructionSetFeatures& isa_features,
350 const CompilerOptions& compiler_options) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000351 switch (instruction_set) {
352 case kArm:
353 case kThumb2: {
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000354 return new arm::CodeGeneratorARM(graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000355 *isa_features.AsArmInstructionSetFeatures(),
356 compiler_options);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000357 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100358 case kArm64: {
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000359 return new arm64::CodeGeneratorARM64(graph, compiler_options);
Alexandre Rames5319def2014-10-23 10:03:10 +0100360 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000361 case kMips:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000362 return nullptr;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000363 case kX86: {
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000364 return new x86::CodeGeneratorX86(graph, compiler_options);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000365 }
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700366 case kX86_64: {
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000367 return new x86_64::CodeGeneratorX86_64(graph, compiler_options);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700368 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000369 default:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000370 return nullptr;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000371 }
372}
373
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000374void CodeGenerator::BuildNativeGCMap(
375 std::vector<uint8_t>* data, const DexCompilationUnit& dex_compilation_unit) const {
376 const std::vector<uint8_t>& gc_map_raw =
377 dex_compilation_unit.GetVerifiedMethod()->GetDexGcMap();
378 verifier::DexPcToReferenceMap dex_gc_map(&(gc_map_raw)[0]);
379
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000380 uint32_t max_native_offset = 0;
381 for (size_t i = 0; i < pc_infos_.Size(); i++) {
382 uint32_t native_offset = pc_infos_.Get(i).native_pc;
383 if (native_offset > max_native_offset) {
384 max_native_offset = native_offset;
385 }
386 }
387
388 GcMapBuilder builder(data, pc_infos_.Size(), max_native_offset, dex_gc_map.RegWidth());
389 for (size_t i = 0; i < pc_infos_.Size(); i++) {
390 struct PcInfo pc_info = pc_infos_.Get(i);
391 uint32_t native_offset = pc_info.native_pc;
392 uint32_t dex_pc = pc_info.dex_pc;
393 const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false);
Jean Christophe Beyler0ada95d2014-12-04 11:20:20 -0800394 CHECK(references != nullptr) << "Missing ref for dex pc 0x" << std::hex << dex_pc;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000395 builder.AddEntry(native_offset, references);
396 }
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000397}
398
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800399void CodeGenerator::BuildMappingTable(std::vector<uint8_t>* data, DefaultSrcMap* src_map) const {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000400 uint32_t pc2dex_data_size = 0u;
401 uint32_t pc2dex_entries = pc_infos_.Size();
402 uint32_t pc2dex_offset = 0u;
403 int32_t pc2dex_dalvik_offset = 0;
404 uint32_t dex2pc_data_size = 0u;
405 uint32_t dex2pc_entries = 0u;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000406 uint32_t dex2pc_offset = 0u;
407 int32_t dex2pc_dalvik_offset = 0;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000408
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700409 if (src_map != nullptr) {
410 src_map->reserve(pc2dex_entries);
411 }
412
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000413 for (size_t i = 0; i < pc2dex_entries; i++) {
414 struct PcInfo pc_info = pc_infos_.Get(i);
415 pc2dex_data_size += UnsignedLeb128Size(pc_info.native_pc - pc2dex_offset);
416 pc2dex_data_size += SignedLeb128Size(pc_info.dex_pc - pc2dex_dalvik_offset);
417 pc2dex_offset = pc_info.native_pc;
418 pc2dex_dalvik_offset = pc_info.dex_pc;
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700419 if (src_map != nullptr) {
420 src_map->push_back(SrcMapElem({pc2dex_offset, pc2dex_dalvik_offset}));
421 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000422 }
423
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000424 // Walk over the blocks and find which ones correspond to catch block entries.
425 for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) {
426 HBasicBlock* block = graph_->GetBlocks().Get(i);
427 if (block->IsCatchBlock()) {
428 intptr_t native_pc = GetAddressOf(block);
429 ++dex2pc_entries;
430 dex2pc_data_size += UnsignedLeb128Size(native_pc - dex2pc_offset);
431 dex2pc_data_size += SignedLeb128Size(block->GetDexPc() - dex2pc_dalvik_offset);
432 dex2pc_offset = native_pc;
433 dex2pc_dalvik_offset = block->GetDexPc();
434 }
435 }
436
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000437 uint32_t total_entries = pc2dex_entries + dex2pc_entries;
438 uint32_t hdr_data_size = UnsignedLeb128Size(total_entries) + UnsignedLeb128Size(pc2dex_entries);
439 uint32_t data_size = hdr_data_size + pc2dex_data_size + dex2pc_data_size;
440 data->resize(data_size);
441
442 uint8_t* data_ptr = &(*data)[0];
443 uint8_t* write_pos = data_ptr;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000444
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000445 write_pos = EncodeUnsignedLeb128(write_pos, total_entries);
446 write_pos = EncodeUnsignedLeb128(write_pos, pc2dex_entries);
447 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size);
448 uint8_t* write_pos2 = write_pos + pc2dex_data_size;
449
450 pc2dex_offset = 0u;
451 pc2dex_dalvik_offset = 0u;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000452 dex2pc_offset = 0u;
453 dex2pc_dalvik_offset = 0u;
454
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000455 for (size_t i = 0; i < pc2dex_entries; i++) {
456 struct PcInfo pc_info = pc_infos_.Get(i);
457 DCHECK(pc2dex_offset <= pc_info.native_pc);
458 write_pos = EncodeUnsignedLeb128(write_pos, pc_info.native_pc - pc2dex_offset);
459 write_pos = EncodeSignedLeb128(write_pos, pc_info.dex_pc - pc2dex_dalvik_offset);
460 pc2dex_offset = pc_info.native_pc;
461 pc2dex_dalvik_offset = pc_info.dex_pc;
462 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000463
464 for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) {
465 HBasicBlock* block = graph_->GetBlocks().Get(i);
466 if (block->IsCatchBlock()) {
467 intptr_t native_pc = GetAddressOf(block);
468 write_pos2 = EncodeUnsignedLeb128(write_pos2, native_pc - dex2pc_offset);
469 write_pos2 = EncodeSignedLeb128(write_pos2, block->GetDexPc() - dex2pc_dalvik_offset);
470 dex2pc_offset = native_pc;
471 dex2pc_dalvik_offset = block->GetDexPc();
472 }
473 }
474
475
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000476 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size + pc2dex_data_size);
477 DCHECK_EQ(static_cast<size_t>(write_pos2 - data_ptr), data_size);
478
479 if (kIsDebugBuild) {
480 // Verify the encoded table holds the expected data.
481 MappingTable table(data_ptr);
482 CHECK_EQ(table.TotalSize(), total_entries);
483 CHECK_EQ(table.PcToDexSize(), pc2dex_entries);
484 auto it = table.PcToDexBegin();
485 auto it2 = table.DexToPcBegin();
486 for (size_t i = 0; i < pc2dex_entries; i++) {
487 struct PcInfo pc_info = pc_infos_.Get(i);
488 CHECK_EQ(pc_info.native_pc, it.NativePcOffset());
489 CHECK_EQ(pc_info.dex_pc, it.DexPc());
490 ++it;
491 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000492 for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) {
493 HBasicBlock* block = graph_->GetBlocks().Get(i);
494 if (block->IsCatchBlock()) {
495 CHECK_EQ(GetAddressOf(block), it2.NativePcOffset());
496 CHECK_EQ(block->GetDexPc(), it2.DexPc());
497 ++it2;
498 }
499 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000500 CHECK(it == table.PcToDexEnd());
501 CHECK(it2 == table.DexToPcEnd());
502 }
503}
504
505void CodeGenerator::BuildVMapTable(std::vector<uint8_t>* data) const {
506 Leb128EncodingVector vmap_encoder;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100507 // We currently don't use callee-saved registers.
508 size_t size = 0 + 1 /* marker */ + 0;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000509 vmap_encoder.Reserve(size + 1u); // All values are likely to be one byte in ULEB128 (<128).
510 vmap_encoder.PushBackUnsigned(size);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000511 vmap_encoder.PushBackUnsigned(VmapTable::kAdjustedFpMarker);
512
513 *data = vmap_encoder.GetData();
514}
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000515
Nicolas Geoffray39468442014-09-02 15:17:15 +0100516void CodeGenerator::BuildStackMaps(std::vector<uint8_t>* data) {
517 uint32_t size = stack_map_stream_.ComputeNeededSize();
518 data->resize(size);
519 MemoryRegion region(data->data(), size);
520 stack_map_stream_.FillIn(region);
521}
522
523void CodeGenerator::RecordPcInfo(HInstruction* instruction, uint32_t dex_pc) {
Calin Juravled2ec87d2014-12-08 14:24:46 +0000524 if (instruction != nullptr) {
Roland Levillain624279f2014-12-04 11:54:28 +0000525 // The code generated for some type conversions may call the
526 // runtime, thus normally requiring a subsequent call to this
527 // method. However, the method verifier does not produce PC
Calin Juravled2ec87d2014-12-08 14:24:46 +0000528 // information for certain instructions, which are considered "atomic"
529 // (they cannot join a GC).
Roland Levillain624279f2014-12-04 11:54:28 +0000530 // Therefore we do not currently record PC information for such
531 // instructions. As this may change later, we added this special
532 // case so that code generators may nevertheless call
533 // CodeGenerator::RecordPcInfo without triggering an error in
534 // CodeGenerator::BuildNativeGCMap ("Missing ref for dex pc 0x")
535 // thereafter.
Calin Juravled2ec87d2014-12-08 14:24:46 +0000536 if (instruction->IsTypeConversion()) {
537 return;
538 }
539 if (instruction->IsRem()) {
540 Primitive::Type type = instruction->AsRem()->GetResultType();
541 if ((type == Primitive::kPrimFloat) || (type == Primitive::kPrimDouble)) {
542 return;
543 }
544 }
Roland Levillain624279f2014-12-04 11:54:28 +0000545 }
546
Nicolas Geoffray39468442014-09-02 15:17:15 +0100547 // Collect PC infos for the mapping table.
548 struct PcInfo pc_info;
549 pc_info.dex_pc = dex_pc;
550 pc_info.native_pc = GetAssembler()->CodeSize();
551 pc_infos_.Add(pc_info);
552
553 // Populate stack map information.
554
555 if (instruction == nullptr) {
556 // For stack overflow checks.
557 stack_map_stream_.AddStackMapEntry(dex_pc, pc_info.native_pc, 0, 0, 0, 0);
558 return;
559 }
560
561 LocationSummary* locations = instruction->GetLocations();
562 HEnvironment* environment = instruction->GetEnvironment();
563
564 size_t environment_size = instruction->EnvironmentSize();
565
Nicolas Geoffray39468442014-09-02 15:17:15 +0100566 size_t inlining_depth = 0;
Nicolas Geoffray98893962015-01-21 12:32:32 +0000567 uint32_t register_mask = locations->GetRegisterMask();
568 if (locations->OnlyCallsOnSlowPath()) {
569 // In case of slow path, we currently set the location of caller-save registers
570 // to register (instead of their stack location when pushed before the slow-path
571 // call). Therefore register_mask contains both callee-save and caller-save
572 // registers that hold objects. We must remove the caller-save from the mask, since
573 // they will be overwritten by the callee.
574 register_mask &= core_callee_save_mask_;
575 }
576 // The register mask must be a subset of callee-save registers.
577 DCHECK_EQ(register_mask & core_callee_save_mask_, register_mask);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100578 stack_map_stream_.AddStackMapEntry(
579 dex_pc, pc_info.native_pc, register_mask,
580 locations->GetStackMask(), environment_size, inlining_depth);
581
582 // Walk over the environment, and record the location of dex registers.
583 for (size_t i = 0; i < environment_size; ++i) {
584 HInstruction* current = environment->GetInstructionAt(i);
585 if (current == nullptr) {
586 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kNone, 0);
587 continue;
588 }
589
590 Location location = locations->GetEnvironmentAt(i);
591 switch (location.GetKind()) {
592 case Location::kConstant: {
593 DCHECK(current == location.GetConstant());
594 if (current->IsLongConstant()) {
595 int64_t value = current->AsLongConstant()->GetValue();
596 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, Low32Bits(value));
597 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, High32Bits(value));
598 ++i;
599 DCHECK_LT(i, environment_size);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000600 } else if (current->IsDoubleConstant()) {
601 int64_t value = bit_cast<double, int64_t>(current->AsDoubleConstant()->GetValue());
602 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, Low32Bits(value));
603 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, High32Bits(value));
604 ++i;
605 DCHECK_LT(i, environment_size);
606 } else if (current->IsIntConstant()) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100607 int32_t value = current->AsIntConstant()->GetValue();
608 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, value);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000609 } else {
610 DCHECK(current->IsFloatConstant());
611 int32_t value = bit_cast<float, int32_t>(current->AsFloatConstant()->GetValue());
612 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, value);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100613 }
614 break;
615 }
616
617 case Location::kStackSlot: {
618 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInStack, location.GetStackIndex());
619 break;
620 }
621
622 case Location::kDoubleStackSlot: {
623 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInStack, location.GetStackIndex());
624 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInStack,
625 location.GetHighStackIndex(kVRegSize));
626 ++i;
627 DCHECK_LT(i, environment_size);
628 break;
629 }
630
631 case Location::kRegister : {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100632 int id = location.reg();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100633 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInRegister, id);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100634 if (current->GetType() == Primitive::kPrimLong) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100635 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInRegister, id);
636 ++i;
637 DCHECK_LT(i, environment_size);
638 }
639 break;
640 }
641
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100642 case Location::kFpuRegister : {
643 int id = location.reg();
644 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInFpuRegister, id);
645 if (current->GetType() == Primitive::kPrimDouble) {
646 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInFpuRegister, id);
647 ++i;
648 DCHECK_LT(i, environment_size);
649 }
650 break;
651 }
652
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000653 case Location::kFpuRegisterPair : {
654 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInFpuRegister, location.low());
655 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInFpuRegister, location.high());
656 ++i;
657 DCHECK_LT(i, environment_size);
658 break;
659 }
660
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000661 case Location::kRegisterPair : {
662 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInRegister, location.low());
663 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInRegister, location.high());
664 ++i;
665 DCHECK_LT(i, environment_size);
666 break;
667 }
668
Nicolas Geoffray39468442014-09-02 15:17:15 +0100669 default:
670 LOG(FATAL) << "Unexpected kind " << location.GetKind();
671 }
672 }
673}
674
Calin Juravle77520bc2015-01-12 18:45:46 +0000675bool CodeGenerator::CanMoveNullCheckToUser(HNullCheck* null_check) {
676 HInstruction* first_next_not_move = null_check->GetNextDisregardingMoves();
677 return (first_next_not_move != nullptr) && first_next_not_move->CanDoImplicitNullCheck();
678}
679
680void CodeGenerator::MaybeRecordImplicitNullCheck(HInstruction* instr) {
681 // If we are from a static path don't record the pc as we can't throw NPE.
682 // NB: having the checks here makes the code much less verbose in the arch
683 // specific code generators.
684 if (instr->IsStaticFieldSet() || instr->IsStaticFieldGet()) {
685 return;
686 }
687
688 if (!compiler_options_.GetImplicitNullChecks()) {
689 return;
690 }
691
692 if (!instr->CanDoImplicitNullCheck()) {
693 return;
694 }
695
696 // Find the first previous instruction which is not a move.
697 HInstruction* first_prev_not_move = instr->GetPreviousDisregardingMoves();
698
699 // If the instruction is a null check it means that `instr` is the first user
700 // and needs to record the pc.
701 if (first_prev_not_move != nullptr && first_prev_not_move->IsNullCheck()) {
702 HNullCheck* null_check = first_prev_not_move->AsNullCheck();
703 // TODO: The parallel moves modify the environment. Their changes need to be reverted
704 // otherwise the stack maps at the throw point will not be correct.
705 RecordPcInfo(null_check, null_check->GetDexPc());
706 }
707}
708
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100709void CodeGenerator::SaveLiveRegisters(LocationSummary* locations) {
710 RegisterSet* register_set = locations->GetLiveRegisters();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100711 size_t stack_offset = first_register_slot_in_slow_path_;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100712 for (size_t i = 0, e = GetNumberOfCoreRegisters(); i < e; ++i) {
Nicolas Geoffray98893962015-01-21 12:32:32 +0000713 if (!IsCoreCalleeSaveRegister(i)) {
714 if (register_set->ContainsCoreRegister(i)) {
715 // If the register holds an object, update the stack mask.
716 if (locations->RegisterContainsObject(i)) {
717 locations->SetStackBit(stack_offset / kVRegSize);
718 }
719 DCHECK_LT(stack_offset, GetFrameSize() - FrameEntrySpillSize());
720 stack_offset += SaveCoreRegister(stack_offset, i);
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100721 }
722 }
723 }
724
725 for (size_t i = 0, e = GetNumberOfFloatingPointRegisters(); i < e; ++i) {
Nicolas Geoffray98893962015-01-21 12:32:32 +0000726 if (!IsFloatingPointCalleeSaveRegister(i)) {
727 if (register_set->ContainsFloatingPointRegister(i)) {
728 DCHECK_LT(stack_offset, GetFrameSize() - FrameEntrySpillSize());
729 stack_offset += SaveFloatingPointRegister(stack_offset, i);
730 }
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100731 }
732 }
733}
734
735void CodeGenerator::RestoreLiveRegisters(LocationSummary* locations) {
736 RegisterSet* register_set = locations->GetLiveRegisters();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100737 size_t stack_offset = first_register_slot_in_slow_path_;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100738 for (size_t i = 0, e = GetNumberOfCoreRegisters(); i < e; ++i) {
Nicolas Geoffray98893962015-01-21 12:32:32 +0000739 if (!IsCoreCalleeSaveRegister(i)) {
740 if (register_set->ContainsCoreRegister(i)) {
741 DCHECK_LT(stack_offset, GetFrameSize() - FrameEntrySpillSize());
742 stack_offset += RestoreCoreRegister(stack_offset, i);
743 }
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100744 }
745 }
746
747 for (size_t i = 0, e = GetNumberOfFloatingPointRegisters(); i < e; ++i) {
Nicolas Geoffray98893962015-01-21 12:32:32 +0000748 if (!IsFloatingPointCalleeSaveRegister(i)) {
749 if (register_set->ContainsFloatingPointRegister(i)) {
750 DCHECK_LT(stack_offset, GetFrameSize() - FrameEntrySpillSize());
751 stack_offset += RestoreFloatingPointRegister(stack_offset, i);
752 }
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100753 }
754 }
755}
756
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100757void CodeGenerator::ClearSpillSlotsFromLoopPhisInStackMap(HSuspendCheck* suspend_check) const {
758 LocationSummary* locations = suspend_check->GetLocations();
759 HBasicBlock* block = suspend_check->GetBlock();
760 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == suspend_check);
761 DCHECK(block->IsLoopHeader());
762
763 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
764 HInstruction* current = it.Current();
765 LiveInterval* interval = current->GetLiveInterval();
766 // We only need to clear bits of loop phis containing objects and allocated in register.
767 // Loop phis allocated on stack already have the object in the stack.
768 if (current->GetType() == Primitive::kPrimNot
769 && interval->HasRegister()
770 && interval->HasSpillSlot()) {
771 locations->ClearStackBit(interval->GetSpillSlot() / kVRegSize);
772 }
773 }
774}
775
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000776void CodeGenerator::EmitParallelMoves(Location from1, Location to1, Location from2, Location to2) {
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000777 HParallelMove parallel_move(GetGraph()->GetArena());
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +0000778 parallel_move.AddMove(from1, to1, nullptr);
779 parallel_move.AddMove(from2, to2, nullptr);
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000780 GetMoveResolver()->EmitNativeCode(&parallel_move);
781}
782
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000783} // namespace art