blob: fd4e391470af189c3e3d55412a414f2e0c9cbc5a [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 Geoffray86dbb9a2014-06-04 11:12:39 +010044 DCHECK_EQ(frame_size_, kUninitializedFrameSize);
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +000045
46 Initialize();
Nicolas Geoffray73e80c32014-07-22 17:47:56 +010047 if (!is_leaf) {
48 MarkNotLeaf();
49 }
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +000050 InitializeCodeGeneration(GetGraph()->GetNumberOfLocalVRegs()
51 + GetGraph()->GetTemporariesVRegSlots()
52 + 1 /* filler */,
53 0, /* the baseline compiler does not have live registers at slow path */
54 0, /* the baseline compiler does not have live registers at slow path */
55 GetGraph()->GetMaximumNumberOfOutVRegs()
56 + 1 /* current method */,
57 GetGraph()->GetBlocks());
58 CompileInternal(allocator, /* is_baseline */ true);
59}
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010060
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +000061void CodeGenerator::CompileInternal(CodeAllocator* allocator, bool is_baseline) {
Nicolas Geoffray8a16d972014-09-11 10:30:02 +010062 HGraphVisitor* location_builder = GetLocationBuilder();
63 HGraphVisitor* instruction_visitor = GetInstructionVisitor();
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +000064 DCHECK_EQ(current_block_index_, 0u);
65 GenerateFrameEntry();
66 for (size_t e = block_order_->Size(); current_block_index_ < e; ++current_block_index_) {
67 HBasicBlock* block = block_order_->Get(current_block_index_);
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010068 Bind(block);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010069 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
70 HInstruction* current = it.Current();
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +000071 if (is_baseline) {
72 current->Accept(location_builder);
73 InitLocations(current);
74 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010075 current->Accept(instruction_visitor);
76 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000077 }
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +000078
79 // Generate the slow paths.
80 for (size_t i = 0, e = slow_paths_.Size(); i < e; ++i) {
81 slow_paths_.Get(i)->EmitNativeCode(this);
82 }
83
84 // Finalize instructions in assember;
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +000085 Finalize(allocator);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000086}
87
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010088void CodeGenerator::CompileOptimized(CodeAllocator* allocator) {
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +000089 // The register allocator already called `InitializeCodeGeneration`,
90 // where the frame size has been computed.
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010091 DCHECK_NE(frame_size_, kUninitializedFrameSize);
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +000092 DCHECK(block_order_ != nullptr);
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010093 Initialize();
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +000094 CompileInternal(allocator, /* is_baseline */ false);
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +000095}
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010096
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +000097void CodeGenerator::Finalize(CodeAllocator* allocator) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010098 size_t code_size = GetAssembler()->CodeSize();
99 uint8_t* buffer = allocator->Allocate(code_size);
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000100
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100101 MemoryRegion code(buffer, code_size);
102 GetAssembler()->FinalizeInstructions(code);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000103}
104
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100105size_t CodeGenerator::FindFreeEntry(bool* array, size_t length) {
106 for (size_t i = 0; i < length; ++i) {
107 if (!array[i]) {
108 array[i] = true;
109 return i;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100110 }
111 }
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100112 LOG(FATAL) << "Could not find a register in baseline register allocator";
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000113 UNREACHABLE();
114 return -1;
115}
116
Nicolas Geoffray3c035032014-10-28 10:46:40 +0000117size_t CodeGenerator::FindTwoFreeConsecutiveAlignedEntries(bool* array, size_t length) {
118 for (size_t i = 0; i < length - 1; i += 2) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000119 if (!array[i] && !array[i + 1]) {
120 array[i] = true;
121 array[i + 1] = true;
122 return i;
123 }
124 }
125 LOG(FATAL) << "Could not find a register in baseline register allocator";
126 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100127 return -1;
128}
129
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000130void CodeGenerator::InitializeCodeGeneration(size_t number_of_spill_slots,
131 size_t maximum_number_of_live_core_registers,
132 size_t maximum_number_of_live_fp_registers,
133 size_t number_of_out_slots,
134 const GrowableArray<HBasicBlock*>& block_order) {
135 block_order_ = &block_order;
136 DCHECK(block_order_->Get(0) == GetGraph()->GetEntryBlock());
137 DCHECK(GoesToNextBlock(GetGraph()->GetEntryBlock(), block_order_->Get(1)));
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000138 ComputeSpillMask();
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100139 first_register_slot_in_slow_path_ = (number_of_out_slots + number_of_spill_slots) * kVRegSize;
140
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100141 SetFrameSize(RoundUp(
142 number_of_spill_slots * kVRegSize
Nicolas Geoffray39468442014-09-02 15:17:15 +0100143 + number_of_out_slots * kVRegSize
Mark Mendellf85a9ca2015-01-13 09:20:58 -0500144 + maximum_number_of_live_core_registers * GetWordSize()
145 + maximum_number_of_live_fp_registers * GetFloatingPointSpillSlotSize()
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100146 + FrameEntrySpillSize(),
147 kStackAlignment));
148}
149
150Location CodeGenerator::GetTemporaryLocation(HTemporary* temp) const {
151 uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs();
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000152 // The type of the previous instruction tells us if we need a single or double stack slot.
153 Primitive::Type type = temp->GetType();
154 int32_t temp_size = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble) ? 2 : 1;
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100155 // Use the temporary region (right below the dex registers).
156 int32_t slot = GetFrameSize() - FrameEntrySpillSize()
157 - kVRegSize // filler
158 - (number_of_locals * kVRegSize)
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000159 - ((temp_size + temp->GetIndex()) * kVRegSize);
160 return temp_size == 2 ? Location::DoubleStackSlot(slot) : Location::StackSlot(slot);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100161}
162
163int32_t CodeGenerator::GetStackSlot(HLocal* local) const {
164 uint16_t reg_number = local->GetRegNumber();
165 uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs();
166 if (reg_number >= number_of_locals) {
167 // Local is a parameter of the method. It is stored in the caller's frame.
168 return GetFrameSize() + kVRegSize // ART method
169 + (reg_number - number_of_locals) * kVRegSize;
170 } else {
171 // Local is a temporary in this method. It is stored in this method's frame.
172 return GetFrameSize() - FrameEntrySpillSize()
173 - kVRegSize // filler.
174 - (number_of_locals * kVRegSize)
175 + (reg_number * kVRegSize);
176 }
177}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100178
179void CodeGenerator::AllocateRegistersLocally(HInstruction* instruction) const {
180 LocationSummary* locations = instruction->GetLocations();
181 if (locations == nullptr) return;
182
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100183 for (size_t i = 0, e = GetNumberOfCoreRegisters(); i < e; ++i) {
184 blocked_core_registers_[i] = false;
185 }
186
187 for (size_t i = 0, e = GetNumberOfFloatingPointRegisters(); i < e; ++i) {
188 blocked_fpu_registers_[i] = false;
189 }
190
191 for (size_t i = 0, e = number_of_register_pairs_; i < e; ++i) {
192 blocked_register_pairs_[i] = false;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100193 }
194
195 // Mark all fixed input, temp and output registers as used.
196 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
197 Location loc = locations->InAt(i);
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000198 // The DCHECKS below check that a register is not specified twice in
199 // the summary.
200 if (loc.IsRegister()) {
201 DCHECK(!blocked_core_registers_[loc.reg()]);
202 blocked_core_registers_[loc.reg()] = true;
203 } else if (loc.IsFpuRegister()) {
204 DCHECK(!blocked_fpu_registers_[loc.reg()]);
205 blocked_fpu_registers_[loc.reg()] = true;
206 } else if (loc.IsFpuRegisterPair()) {
207 DCHECK(!blocked_fpu_registers_[loc.AsFpuRegisterPairLow<int>()]);
208 blocked_fpu_registers_[loc.AsFpuRegisterPairLow<int>()] = true;
209 DCHECK(!blocked_fpu_registers_[loc.AsFpuRegisterPairHigh<int>()]);
210 blocked_fpu_registers_[loc.AsFpuRegisterPairHigh<int>()] = true;
211 } else if (loc.IsRegisterPair()) {
212 DCHECK(!blocked_core_registers_[loc.AsRegisterPairLow<int>()]);
213 blocked_core_registers_[loc.AsRegisterPairLow<int>()] = true;
214 DCHECK(!blocked_core_registers_[loc.AsRegisterPairHigh<int>()]);
215 blocked_core_registers_[loc.AsRegisterPairHigh<int>()] = true;
216 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100217 }
218
219 for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) {
220 Location loc = locations->GetTemp(i);
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000221 // The DCHECKS below check that a register is not specified twice in
222 // the summary.
223 if (loc.IsRegister()) {
224 DCHECK(!blocked_core_registers_[loc.reg()]);
225 blocked_core_registers_[loc.reg()] = true;
226 } else if (loc.IsFpuRegister()) {
227 DCHECK(!blocked_fpu_registers_[loc.reg()]);
228 blocked_fpu_registers_[loc.reg()] = true;
229 } else {
230 DCHECK(loc.GetPolicy() == Location::kRequiresRegister
231 || loc.GetPolicy() == Location::kRequiresFpuRegister);
232 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100233 }
234
Nicolas Geoffray98893962015-01-21 12:32:32 +0000235 static constexpr bool kBaseline = true;
236 SetupBlockedRegisters(kBaseline);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100237
238 // Allocate all unallocated input locations.
239 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
240 Location loc = locations->InAt(i);
241 HInstruction* input = instruction->InputAt(i);
242 if (loc.IsUnallocated()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100243 if ((loc.GetPolicy() == Location::kRequiresRegister)
244 || (loc.GetPolicy() == Location::kRequiresFpuRegister)) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100245 loc = AllocateFreeRegister(input->GetType());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100246 } else {
247 DCHECK_EQ(loc.GetPolicy(), Location::kAny);
248 HLoadLocal* load = input->AsLoadLocal();
249 if (load != nullptr) {
250 loc = GetStackLocation(load);
251 } else {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100252 loc = AllocateFreeRegister(input->GetType());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100253 }
254 }
255 locations->SetInAt(i, loc);
256 }
257 }
258
259 // Allocate all unallocated temp locations.
260 for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) {
261 Location loc = locations->GetTemp(i);
262 if (loc.IsUnallocated()) {
Roland Levillain647b9ed2014-11-27 12:06:00 +0000263 switch (loc.GetPolicy()) {
264 case Location::kRequiresRegister:
265 // Allocate a core register (large enough to fit a 32-bit integer).
266 loc = AllocateFreeRegister(Primitive::kPrimInt);
267 break;
268
269 case Location::kRequiresFpuRegister:
270 // Allocate a core register (large enough to fit a 64-bit double).
271 loc = AllocateFreeRegister(Primitive::kPrimDouble);
272 break;
273
274 default:
275 LOG(FATAL) << "Unexpected policy for temporary location "
276 << loc.GetPolicy();
277 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100278 locations->SetTempAt(i, loc);
279 }
280 }
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000281 Location result_location = locations->Out();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100282 if (result_location.IsUnallocated()) {
283 switch (result_location.GetPolicy()) {
284 case Location::kAny:
285 case Location::kRequiresRegister:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100286 case Location::kRequiresFpuRegister:
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100287 result_location = AllocateFreeRegister(instruction->GetType());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100288 break;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100289 case Location::kSameAsFirstInput:
290 result_location = locations->InAt(0);
291 break;
292 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000293 locations->UpdateOut(result_location);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100294 }
295}
296
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000297void CodeGenerator::InitLocations(HInstruction* instruction) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100298 if (instruction->GetLocations() == nullptr) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100299 if (instruction->IsTemporary()) {
300 HInstruction* previous = instruction->GetPrevious();
301 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
302 Move(previous, temp_location, instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100303 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100304 return;
305 }
306 AllocateRegistersLocally(instruction);
307 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000308 Location location = instruction->GetLocations()->InAt(i);
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000309 HInstruction* input = instruction->InputAt(i);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000310 if (location.IsValid()) {
311 // Move the input to the desired location.
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000312 if (input->GetNext()->IsTemporary()) {
313 // If the input was stored in a temporary, use that temporary to
314 // perform the move.
315 Move(input->GetNext(), location, instruction);
316 } else {
317 Move(input, location, instruction);
318 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000319 }
320 }
321}
322
323bool CodeGenerator::GoesToNextBlock(HBasicBlock* current, HBasicBlock* next) const {
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000324 DCHECK_EQ(block_order_->Get(current_block_index_), current);
325 return (current_block_index_ < block_order_->Size() - 1)
326 && (block_order_->Get(current_block_index_ + 1) == next);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000327}
328
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000329CodeGenerator* CodeGenerator::Create(HGraph* graph,
Calin Juravle34166012014-12-19 17:22:29 +0000330 InstructionSet instruction_set,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000331 const InstructionSetFeatures& isa_features,
332 const CompilerOptions& compiler_options) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000333 switch (instruction_set) {
334 case kArm:
335 case kThumb2: {
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000336 return new arm::CodeGeneratorARM(graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000337 *isa_features.AsArmInstructionSetFeatures(),
338 compiler_options);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000339 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100340 case kArm64: {
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000341 return new arm64::CodeGeneratorARM64(graph, compiler_options);
Alexandre Rames5319def2014-10-23 10:03:10 +0100342 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000343 case kMips:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000344 return nullptr;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000345 case kX86: {
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000346 return new x86::CodeGeneratorX86(graph, compiler_options);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000347 }
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700348 case kX86_64: {
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000349 return new x86_64::CodeGeneratorX86_64(graph, compiler_options);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700350 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000351 default:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000352 return nullptr;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000353 }
354}
355
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000356void CodeGenerator::BuildNativeGCMap(
357 std::vector<uint8_t>* data, const DexCompilationUnit& dex_compilation_unit) const {
358 const std::vector<uint8_t>& gc_map_raw =
359 dex_compilation_unit.GetVerifiedMethod()->GetDexGcMap();
360 verifier::DexPcToReferenceMap dex_gc_map(&(gc_map_raw)[0]);
361
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000362 uint32_t max_native_offset = 0;
363 for (size_t i = 0; i < pc_infos_.Size(); i++) {
364 uint32_t native_offset = pc_infos_.Get(i).native_pc;
365 if (native_offset > max_native_offset) {
366 max_native_offset = native_offset;
367 }
368 }
369
370 GcMapBuilder builder(data, pc_infos_.Size(), max_native_offset, dex_gc_map.RegWidth());
371 for (size_t i = 0; i < pc_infos_.Size(); i++) {
372 struct PcInfo pc_info = pc_infos_.Get(i);
373 uint32_t native_offset = pc_info.native_pc;
374 uint32_t dex_pc = pc_info.dex_pc;
375 const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false);
Jean Christophe Beyler0ada95d2014-12-04 11:20:20 -0800376 CHECK(references != nullptr) << "Missing ref for dex pc 0x" << std::hex << dex_pc;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000377 builder.AddEntry(native_offset, references);
378 }
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000379}
380
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800381void CodeGenerator::BuildMappingTable(std::vector<uint8_t>* data, DefaultSrcMap* src_map) const {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000382 uint32_t pc2dex_data_size = 0u;
383 uint32_t pc2dex_entries = pc_infos_.Size();
384 uint32_t pc2dex_offset = 0u;
385 int32_t pc2dex_dalvik_offset = 0;
386 uint32_t dex2pc_data_size = 0u;
387 uint32_t dex2pc_entries = 0u;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000388 uint32_t dex2pc_offset = 0u;
389 int32_t dex2pc_dalvik_offset = 0;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000390
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700391 if (src_map != nullptr) {
392 src_map->reserve(pc2dex_entries);
393 }
394
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000395 for (size_t i = 0; i < pc2dex_entries; i++) {
396 struct PcInfo pc_info = pc_infos_.Get(i);
397 pc2dex_data_size += UnsignedLeb128Size(pc_info.native_pc - pc2dex_offset);
398 pc2dex_data_size += SignedLeb128Size(pc_info.dex_pc - pc2dex_dalvik_offset);
399 pc2dex_offset = pc_info.native_pc;
400 pc2dex_dalvik_offset = pc_info.dex_pc;
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700401 if (src_map != nullptr) {
402 src_map->push_back(SrcMapElem({pc2dex_offset, pc2dex_dalvik_offset}));
403 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000404 }
405
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000406 // Walk over the blocks and find which ones correspond to catch block entries.
407 for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) {
408 HBasicBlock* block = graph_->GetBlocks().Get(i);
409 if (block->IsCatchBlock()) {
410 intptr_t native_pc = GetAddressOf(block);
411 ++dex2pc_entries;
412 dex2pc_data_size += UnsignedLeb128Size(native_pc - dex2pc_offset);
413 dex2pc_data_size += SignedLeb128Size(block->GetDexPc() - dex2pc_dalvik_offset);
414 dex2pc_offset = native_pc;
415 dex2pc_dalvik_offset = block->GetDexPc();
416 }
417 }
418
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000419 uint32_t total_entries = pc2dex_entries + dex2pc_entries;
420 uint32_t hdr_data_size = UnsignedLeb128Size(total_entries) + UnsignedLeb128Size(pc2dex_entries);
421 uint32_t data_size = hdr_data_size + pc2dex_data_size + dex2pc_data_size;
422 data->resize(data_size);
423
424 uint8_t* data_ptr = &(*data)[0];
425 uint8_t* write_pos = data_ptr;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000426
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000427 write_pos = EncodeUnsignedLeb128(write_pos, total_entries);
428 write_pos = EncodeUnsignedLeb128(write_pos, pc2dex_entries);
429 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size);
430 uint8_t* write_pos2 = write_pos + pc2dex_data_size;
431
432 pc2dex_offset = 0u;
433 pc2dex_dalvik_offset = 0u;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000434 dex2pc_offset = 0u;
435 dex2pc_dalvik_offset = 0u;
436
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000437 for (size_t i = 0; i < pc2dex_entries; i++) {
438 struct PcInfo pc_info = pc_infos_.Get(i);
439 DCHECK(pc2dex_offset <= pc_info.native_pc);
440 write_pos = EncodeUnsignedLeb128(write_pos, pc_info.native_pc - pc2dex_offset);
441 write_pos = EncodeSignedLeb128(write_pos, pc_info.dex_pc - pc2dex_dalvik_offset);
442 pc2dex_offset = pc_info.native_pc;
443 pc2dex_dalvik_offset = pc_info.dex_pc;
444 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000445
446 for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) {
447 HBasicBlock* block = graph_->GetBlocks().Get(i);
448 if (block->IsCatchBlock()) {
449 intptr_t native_pc = GetAddressOf(block);
450 write_pos2 = EncodeUnsignedLeb128(write_pos2, native_pc - dex2pc_offset);
451 write_pos2 = EncodeSignedLeb128(write_pos2, block->GetDexPc() - dex2pc_dalvik_offset);
452 dex2pc_offset = native_pc;
453 dex2pc_dalvik_offset = block->GetDexPc();
454 }
455 }
456
457
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000458 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size + pc2dex_data_size);
459 DCHECK_EQ(static_cast<size_t>(write_pos2 - data_ptr), data_size);
460
461 if (kIsDebugBuild) {
462 // Verify the encoded table holds the expected data.
463 MappingTable table(data_ptr);
464 CHECK_EQ(table.TotalSize(), total_entries);
465 CHECK_EQ(table.PcToDexSize(), pc2dex_entries);
466 auto it = table.PcToDexBegin();
467 auto it2 = table.DexToPcBegin();
468 for (size_t i = 0; i < pc2dex_entries; i++) {
469 struct PcInfo pc_info = pc_infos_.Get(i);
470 CHECK_EQ(pc_info.native_pc, it.NativePcOffset());
471 CHECK_EQ(pc_info.dex_pc, it.DexPc());
472 ++it;
473 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000474 for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) {
475 HBasicBlock* block = graph_->GetBlocks().Get(i);
476 if (block->IsCatchBlock()) {
477 CHECK_EQ(GetAddressOf(block), it2.NativePcOffset());
478 CHECK_EQ(block->GetDexPc(), it2.DexPc());
479 ++it2;
480 }
481 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000482 CHECK(it == table.PcToDexEnd());
483 CHECK(it2 == table.DexToPcEnd());
484 }
485}
486
487void CodeGenerator::BuildVMapTable(std::vector<uint8_t>* data) const {
488 Leb128EncodingVector vmap_encoder;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100489 // We currently don't use callee-saved registers.
490 size_t size = 0 + 1 /* marker */ + 0;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000491 vmap_encoder.Reserve(size + 1u); // All values are likely to be one byte in ULEB128 (<128).
492 vmap_encoder.PushBackUnsigned(size);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000493 vmap_encoder.PushBackUnsigned(VmapTable::kAdjustedFpMarker);
494
495 *data = vmap_encoder.GetData();
496}
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000497
Nicolas Geoffray39468442014-09-02 15:17:15 +0100498void CodeGenerator::BuildStackMaps(std::vector<uint8_t>* data) {
499 uint32_t size = stack_map_stream_.ComputeNeededSize();
500 data->resize(size);
501 MemoryRegion region(data->data(), size);
502 stack_map_stream_.FillIn(region);
503}
504
505void CodeGenerator::RecordPcInfo(HInstruction* instruction, uint32_t dex_pc) {
Calin Juravled2ec87d2014-12-08 14:24:46 +0000506 if (instruction != nullptr) {
Roland Levillain624279f2014-12-04 11:54:28 +0000507 // The code generated for some type conversions may call the
508 // runtime, thus normally requiring a subsequent call to this
509 // method. However, the method verifier does not produce PC
Calin Juravled2ec87d2014-12-08 14:24:46 +0000510 // information for certain instructions, which are considered "atomic"
511 // (they cannot join a GC).
Roland Levillain624279f2014-12-04 11:54:28 +0000512 // Therefore we do not currently record PC information for such
513 // instructions. As this may change later, we added this special
514 // case so that code generators may nevertheless call
515 // CodeGenerator::RecordPcInfo without triggering an error in
516 // CodeGenerator::BuildNativeGCMap ("Missing ref for dex pc 0x")
517 // thereafter.
Calin Juravled2ec87d2014-12-08 14:24:46 +0000518 if (instruction->IsTypeConversion()) {
519 return;
520 }
521 if (instruction->IsRem()) {
522 Primitive::Type type = instruction->AsRem()->GetResultType();
523 if ((type == Primitive::kPrimFloat) || (type == Primitive::kPrimDouble)) {
524 return;
525 }
526 }
Roland Levillain624279f2014-12-04 11:54:28 +0000527 }
528
Nicolas Geoffray39468442014-09-02 15:17:15 +0100529 // Collect PC infos for the mapping table.
530 struct PcInfo pc_info;
531 pc_info.dex_pc = dex_pc;
532 pc_info.native_pc = GetAssembler()->CodeSize();
533 pc_infos_.Add(pc_info);
534
535 // Populate stack map information.
536
537 if (instruction == nullptr) {
538 // For stack overflow checks.
539 stack_map_stream_.AddStackMapEntry(dex_pc, pc_info.native_pc, 0, 0, 0, 0);
540 return;
541 }
542
543 LocationSummary* locations = instruction->GetLocations();
544 HEnvironment* environment = instruction->GetEnvironment();
545
546 size_t environment_size = instruction->EnvironmentSize();
547
Nicolas Geoffray39468442014-09-02 15:17:15 +0100548 size_t inlining_depth = 0;
Nicolas Geoffray98893962015-01-21 12:32:32 +0000549 uint32_t register_mask = locations->GetRegisterMask();
550 if (locations->OnlyCallsOnSlowPath()) {
551 // In case of slow path, we currently set the location of caller-save registers
552 // to register (instead of their stack location when pushed before the slow-path
553 // call). Therefore register_mask contains both callee-save and caller-save
554 // registers that hold objects. We must remove the caller-save from the mask, since
555 // they will be overwritten by the callee.
556 register_mask &= core_callee_save_mask_;
557 }
558 // The register mask must be a subset of callee-save registers.
559 DCHECK_EQ(register_mask & core_callee_save_mask_, register_mask);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100560 stack_map_stream_.AddStackMapEntry(
561 dex_pc, pc_info.native_pc, register_mask,
562 locations->GetStackMask(), environment_size, inlining_depth);
563
564 // Walk over the environment, and record the location of dex registers.
565 for (size_t i = 0; i < environment_size; ++i) {
566 HInstruction* current = environment->GetInstructionAt(i);
567 if (current == nullptr) {
568 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kNone, 0);
569 continue;
570 }
571
572 Location location = locations->GetEnvironmentAt(i);
573 switch (location.GetKind()) {
574 case Location::kConstant: {
575 DCHECK(current == location.GetConstant());
576 if (current->IsLongConstant()) {
577 int64_t value = current->AsLongConstant()->GetValue();
578 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, Low32Bits(value));
579 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, High32Bits(value));
580 ++i;
581 DCHECK_LT(i, environment_size);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000582 } else if (current->IsDoubleConstant()) {
583 int64_t value = bit_cast<double, int64_t>(current->AsDoubleConstant()->GetValue());
584 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, Low32Bits(value));
585 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, High32Bits(value));
586 ++i;
587 DCHECK_LT(i, environment_size);
588 } else if (current->IsIntConstant()) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100589 int32_t value = current->AsIntConstant()->GetValue();
590 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, value);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000591 } else {
592 DCHECK(current->IsFloatConstant());
593 int32_t value = bit_cast<float, int32_t>(current->AsFloatConstant()->GetValue());
594 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, value);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100595 }
596 break;
597 }
598
599 case Location::kStackSlot: {
600 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInStack, location.GetStackIndex());
601 break;
602 }
603
604 case Location::kDoubleStackSlot: {
605 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInStack, location.GetStackIndex());
606 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInStack,
607 location.GetHighStackIndex(kVRegSize));
608 ++i;
609 DCHECK_LT(i, environment_size);
610 break;
611 }
612
613 case Location::kRegister : {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100614 int id = location.reg();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100615 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInRegister, id);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100616 if (current->GetType() == Primitive::kPrimLong) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100617 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInRegister, id);
618 ++i;
619 DCHECK_LT(i, environment_size);
620 }
621 break;
622 }
623
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100624 case Location::kFpuRegister : {
625 int id = location.reg();
626 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInFpuRegister, id);
627 if (current->GetType() == Primitive::kPrimDouble) {
628 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInFpuRegister, id);
629 ++i;
630 DCHECK_LT(i, environment_size);
631 }
632 break;
633 }
634
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000635 case Location::kFpuRegisterPair : {
636 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInFpuRegister, location.low());
637 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInFpuRegister, location.high());
638 ++i;
639 DCHECK_LT(i, environment_size);
640 break;
641 }
642
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000643 case Location::kRegisterPair : {
644 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInRegister, location.low());
645 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInRegister, location.high());
646 ++i;
647 DCHECK_LT(i, environment_size);
648 break;
649 }
650
Nicolas Geoffray39468442014-09-02 15:17:15 +0100651 default:
652 LOG(FATAL) << "Unexpected kind " << location.GetKind();
653 }
654 }
655}
656
Calin Juravle77520bc2015-01-12 18:45:46 +0000657bool CodeGenerator::CanMoveNullCheckToUser(HNullCheck* null_check) {
658 HInstruction* first_next_not_move = null_check->GetNextDisregardingMoves();
659 return (first_next_not_move != nullptr) && first_next_not_move->CanDoImplicitNullCheck();
660}
661
662void CodeGenerator::MaybeRecordImplicitNullCheck(HInstruction* instr) {
663 // If we are from a static path don't record the pc as we can't throw NPE.
664 // NB: having the checks here makes the code much less verbose in the arch
665 // specific code generators.
666 if (instr->IsStaticFieldSet() || instr->IsStaticFieldGet()) {
667 return;
668 }
669
670 if (!compiler_options_.GetImplicitNullChecks()) {
671 return;
672 }
673
674 if (!instr->CanDoImplicitNullCheck()) {
675 return;
676 }
677
678 // Find the first previous instruction which is not a move.
679 HInstruction* first_prev_not_move = instr->GetPreviousDisregardingMoves();
680
681 // If the instruction is a null check it means that `instr` is the first user
682 // and needs to record the pc.
683 if (first_prev_not_move != nullptr && first_prev_not_move->IsNullCheck()) {
684 HNullCheck* null_check = first_prev_not_move->AsNullCheck();
685 // TODO: The parallel moves modify the environment. Their changes need to be reverted
686 // otherwise the stack maps at the throw point will not be correct.
687 RecordPcInfo(null_check, null_check->GetDexPc());
688 }
689}
690
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100691void CodeGenerator::SaveLiveRegisters(LocationSummary* locations) {
692 RegisterSet* register_set = locations->GetLiveRegisters();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100693 size_t stack_offset = first_register_slot_in_slow_path_;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100694 for (size_t i = 0, e = GetNumberOfCoreRegisters(); i < e; ++i) {
Nicolas Geoffray98893962015-01-21 12:32:32 +0000695 if (!IsCoreCalleeSaveRegister(i)) {
696 if (register_set->ContainsCoreRegister(i)) {
697 // If the register holds an object, update the stack mask.
698 if (locations->RegisterContainsObject(i)) {
699 locations->SetStackBit(stack_offset / kVRegSize);
700 }
701 DCHECK_LT(stack_offset, GetFrameSize() - FrameEntrySpillSize());
702 stack_offset += SaveCoreRegister(stack_offset, i);
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100703 }
704 }
705 }
706
707 for (size_t i = 0, e = GetNumberOfFloatingPointRegisters(); i < e; ++i) {
Nicolas Geoffray98893962015-01-21 12:32:32 +0000708 if (!IsFloatingPointCalleeSaveRegister(i)) {
709 if (register_set->ContainsFloatingPointRegister(i)) {
710 DCHECK_LT(stack_offset, GetFrameSize() - FrameEntrySpillSize());
711 stack_offset += SaveFloatingPointRegister(stack_offset, i);
712 }
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100713 }
714 }
715}
716
717void CodeGenerator::RestoreLiveRegisters(LocationSummary* locations) {
718 RegisterSet* register_set = locations->GetLiveRegisters();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100719 size_t stack_offset = first_register_slot_in_slow_path_;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100720 for (size_t i = 0, e = GetNumberOfCoreRegisters(); i < e; ++i) {
Nicolas Geoffray98893962015-01-21 12:32:32 +0000721 if (!IsCoreCalleeSaveRegister(i)) {
722 if (register_set->ContainsCoreRegister(i)) {
723 DCHECK_LT(stack_offset, GetFrameSize() - FrameEntrySpillSize());
724 stack_offset += RestoreCoreRegister(stack_offset, i);
725 }
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100726 }
727 }
728
729 for (size_t i = 0, e = GetNumberOfFloatingPointRegisters(); i < e; ++i) {
Nicolas Geoffray98893962015-01-21 12:32:32 +0000730 if (!IsFloatingPointCalleeSaveRegister(i)) {
731 if (register_set->ContainsFloatingPointRegister(i)) {
732 DCHECK_LT(stack_offset, GetFrameSize() - FrameEntrySpillSize());
733 stack_offset += RestoreFloatingPointRegister(stack_offset, i);
734 }
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100735 }
736 }
737}
738
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100739void CodeGenerator::ClearSpillSlotsFromLoopPhisInStackMap(HSuspendCheck* suspend_check) const {
740 LocationSummary* locations = suspend_check->GetLocations();
741 HBasicBlock* block = suspend_check->GetBlock();
742 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == suspend_check);
743 DCHECK(block->IsLoopHeader());
744
745 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
746 HInstruction* current = it.Current();
747 LiveInterval* interval = current->GetLiveInterval();
748 // We only need to clear bits of loop phis containing objects and allocated in register.
749 // Loop phis allocated on stack already have the object in the stack.
750 if (current->GetType() == Primitive::kPrimNot
751 && interval->HasRegister()
752 && interval->HasSpillSlot()) {
753 locations->ClearStackBit(interval->GetSpillSlot() / kVRegSize);
754 }
755 }
756}
757
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000758void CodeGenerator::EmitParallelMoves(Location from1, Location to1, Location from2, Location to2) {
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000759 HParallelMove parallel_move(GetGraph()->GetArena());
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +0000760 parallel_move.AddMove(from1, to1, nullptr);
761 parallel_move.AddMove(from2, to2, nullptr);
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000762 GetMoveResolver()->EmitNativeCode(&parallel_move);
763}
764
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000765} // namespace art