blob: f05cb66abacd7d252d4e52019d69662e22004da2 [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"
20#include "code_generator_x86.h"
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000021#include "dex/verified_method.h"
22#include "driver/dex_compilation_unit.h"
23#include "gc_map_builder.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000024#include "leb128.h"
25#include "mapping_table.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000026#include "utils/assembler.h"
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000027#include "verifier/dex_gc_map.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000028#include "vmap_table.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000029
30namespace art {
31
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010032void CodeGenerator::CompileBaseline(CodeAllocator* allocator) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +010033 const GrowableArray<HBasicBlock*>& blocks = GetGraph()->GetBlocks();
34 DCHECK(blocks.Get(0) == GetGraph()->GetEntryBlock());
35 DCHECK(GoesToNextBlock(GetGraph()->GetEntryBlock(), blocks.Get(1)));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010036 block_labels_.SetSize(blocks.Size());
37
38 DCHECK_EQ(frame_size_, kUninitializedFrameSize);
39 ComputeFrameSize(GetGraph()->GetMaximumNumberOfOutVRegs()
40 + GetGraph()->GetNumberOfVRegs()
41 + 1 /* filler */);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +010042 GenerateFrameEntry();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010043
Nicolas Geoffray804d0932014-05-02 08:46:00 +010044 for (size_t i = 0, e = blocks.Size(); i < e; ++i) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010045 HBasicBlock* block = blocks.Get(i);
46 Bind(GetLabelOf(block));
47 HGraphVisitor* location_builder = GetLocationBuilder();
48 HGraphVisitor* instruction_visitor = GetInstructionVisitor();
49 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
50 HInstruction* current = it.Current();
51 current->Accept(location_builder);
52 InitLocations(current);
53 current->Accept(instruction_visitor);
54 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000055 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010056
Nicolas Geoffray787c3072014-03-17 10:20:19 +000057 size_t code_size = GetAssembler()->CodeSize();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000058 uint8_t* buffer = allocator->Allocate(code_size);
59 MemoryRegion code(buffer, code_size);
Nicolas Geoffray787c3072014-03-17 10:20:19 +000060 GetAssembler()->FinalizeInstructions(code);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000061}
62
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010063void CodeGenerator::CompileOptimized(CodeAllocator* allocator) {
64 // The frame size has already been computed during register allocation.
65 DCHECK_NE(frame_size_, kUninitializedFrameSize);
66 const GrowableArray<HBasicBlock*>& blocks = GetGraph()->GetBlocks();
67 DCHECK(blocks.Get(0) == GetGraph()->GetEntryBlock());
68 DCHECK(GoesToNextBlock(GetGraph()->GetEntryBlock(), blocks.Get(1)));
69 block_labels_.SetSize(blocks.Size());
70
71 GenerateFrameEntry();
72 for (size_t i = 0, e = blocks.Size(); i < e; ++i) {
73 HBasicBlock* block = blocks.Get(i);
74 Bind(GetLabelOf(block));
75 HGraphVisitor* instruction_visitor = GetInstructionVisitor();
76 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
77 HInstruction* current = it.Current();
78 current->Accept(instruction_visitor);
79 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000080 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010081
82 size_t code_size = GetAssembler()->CodeSize();
83 uint8_t* buffer = allocator->Allocate(code_size);
84 MemoryRegion code(buffer, code_size);
85 GetAssembler()->FinalizeInstructions(code);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000086}
87
Nicolas Geoffraya7aca372014-04-28 17:47:12 +010088size_t CodeGenerator::AllocateFreeRegisterInternal(
89 bool* blocked_registers, size_t number_of_registers) const {
90 for (size_t regno = 0; regno < number_of_registers; regno++) {
91 if (!blocked_registers[regno]) {
92 blocked_registers[regno] = true;
93 return regno;
94 }
95 }
96 LOG(FATAL) << "Unreachable";
97 return -1;
98}
99
100
101void CodeGenerator::AllocateRegistersLocally(HInstruction* instruction) const {
102 LocationSummary* locations = instruction->GetLocations();
103 if (locations == nullptr) return;
104
105 for (size_t i = 0, e = GetNumberOfRegisters(); i < e; ++i) {
106 blocked_registers_[i] = false;
107 }
108
109 // Mark all fixed input, temp and output registers as used.
110 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
111 Location loc = locations->InAt(i);
112 if (loc.IsRegister()) {
113 // Check that a register is not specified twice in the summary.
114 DCHECK(!blocked_registers_[loc.GetEncoding()]);
115 blocked_registers_[loc.GetEncoding()] = true;
116 }
117 }
118
119 for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) {
120 Location loc = locations->GetTemp(i);
121 if (loc.IsRegister()) {
122 // Check that a register is not specified twice in the summary.
123 DCHECK(!blocked_registers_[loc.GetEncoding()]);
124 blocked_registers_[loc.GetEncoding()] = true;
125 }
126 }
127
128 SetupBlockedRegisters(blocked_registers_);
129
130 // Allocate all unallocated input locations.
131 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
132 Location loc = locations->InAt(i);
133 HInstruction* input = instruction->InputAt(i);
134 if (loc.IsUnallocated()) {
135 if (loc.GetPolicy() == Location::kRequiresRegister) {
136 loc = Location::RegisterLocation(
137 AllocateFreeRegister(input->GetType(), blocked_registers_));
138 } else {
139 DCHECK_EQ(loc.GetPolicy(), Location::kAny);
140 HLoadLocal* load = input->AsLoadLocal();
141 if (load != nullptr) {
142 loc = GetStackLocation(load);
143 } else {
144 loc = Location::RegisterLocation(
145 AllocateFreeRegister(input->GetType(), blocked_registers_));
146 }
147 }
148 locations->SetInAt(i, loc);
149 }
150 }
151
152 // Allocate all unallocated temp locations.
153 for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) {
154 Location loc = locations->GetTemp(i);
155 if (loc.IsUnallocated()) {
156 DCHECK_EQ(loc.GetPolicy(), Location::kRequiresRegister);
157 // TODO: Adjust handling of temps. We currently consider temps to use
158 // core registers. They may also use floating point registers at some point.
159 loc = Location::RegisterLocation(static_cast<ManagedRegister>(
160 AllocateFreeRegister(Primitive::kPrimInt, blocked_registers_)));
161 locations->SetTempAt(i, loc);
162 }
163 }
164
Nicolas Geoffrayf529d772014-05-02 11:03:30 +0100165 // Make all registers available for the return value.
166 for (size_t i = 0, e = GetNumberOfRegisters(); i < e; ++i) {
167 blocked_registers_[i] = false;
168 }
169 SetupBlockedRegisters(blocked_registers_);
170
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100171 Location result_location = locations->Out();
172 if (result_location.IsUnallocated()) {
173 switch (result_location.GetPolicy()) {
174 case Location::kAny:
175 case Location::kRequiresRegister:
176 result_location = Location::RegisterLocation(
177 AllocateFreeRegister(instruction->GetType(), blocked_registers_));
178 break;
179 case Location::kSameAsFirstInput:
180 result_location = locations->InAt(0);
181 break;
182 }
183 locations->SetOut(result_location);
184 }
185}
186
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000187void CodeGenerator::InitLocations(HInstruction* instruction) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100188 if (instruction->GetLocations() == nullptr) {
189 return;
190 }
191 AllocateRegistersLocally(instruction);
192 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000193 Location location = instruction->GetLocations()->InAt(i);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000194 if (location.IsValid()) {
195 // Move the input to the desired location.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100196 Move(instruction->InputAt(i), location, instruction);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000197 }
198 }
199}
200
201bool CodeGenerator::GoesToNextBlock(HBasicBlock* current, HBasicBlock* next) const {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000202 // We currently iterate over the block in insertion order.
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000203 return current->GetBlockId() + 1 == next->GetBlockId();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000204}
205
206Label* CodeGenerator::GetLabelOf(HBasicBlock* block) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000207 return block_labels_.GetRawStorage() + block->GetBlockId();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000208}
209
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000210CodeGenerator* CodeGenerator::Create(ArenaAllocator* allocator,
211 HGraph* graph,
212 InstructionSet instruction_set) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000213 switch (instruction_set) {
214 case kArm:
215 case kThumb2: {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000216 return new (allocator) arm::CodeGeneratorARM(graph);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000217 }
218 case kMips:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000219 return nullptr;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000220 case kX86: {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000221 return new (allocator) x86::CodeGeneratorX86(graph);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000222 }
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700223 case kX86_64: {
224 return new (allocator) x86::CodeGeneratorX86(graph);
225 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000226 default:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000227 return nullptr;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000228 }
229}
230
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000231void CodeGenerator::BuildNativeGCMap(
232 std::vector<uint8_t>* data, const DexCompilationUnit& dex_compilation_unit) const {
233 const std::vector<uint8_t>& gc_map_raw =
234 dex_compilation_unit.GetVerifiedMethod()->GetDexGcMap();
235 verifier::DexPcToReferenceMap dex_gc_map(&(gc_map_raw)[0]);
236
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000237 uint32_t max_native_offset = 0;
238 for (size_t i = 0; i < pc_infos_.Size(); i++) {
239 uint32_t native_offset = pc_infos_.Get(i).native_pc;
240 if (native_offset > max_native_offset) {
241 max_native_offset = native_offset;
242 }
243 }
244
245 GcMapBuilder builder(data, pc_infos_.Size(), max_native_offset, dex_gc_map.RegWidth());
246 for (size_t i = 0; i < pc_infos_.Size(); i++) {
247 struct PcInfo pc_info = pc_infos_.Get(i);
248 uint32_t native_offset = pc_info.native_pc;
249 uint32_t dex_pc = pc_info.dex_pc;
250 const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false);
251 CHECK(references != NULL) << "Missing ref for dex pc 0x" << std::hex << dex_pc;
252 builder.AddEntry(native_offset, references);
253 }
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000254}
255
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000256void CodeGenerator::BuildMappingTable(std::vector<uint8_t>* data) const {
257 uint32_t pc2dex_data_size = 0u;
258 uint32_t pc2dex_entries = pc_infos_.Size();
259 uint32_t pc2dex_offset = 0u;
260 int32_t pc2dex_dalvik_offset = 0;
261 uint32_t dex2pc_data_size = 0u;
262 uint32_t dex2pc_entries = 0u;
263
264 // We currently only have pc2dex entries.
265 for (size_t i = 0; i < pc2dex_entries; i++) {
266 struct PcInfo pc_info = pc_infos_.Get(i);
267 pc2dex_data_size += UnsignedLeb128Size(pc_info.native_pc - pc2dex_offset);
268 pc2dex_data_size += SignedLeb128Size(pc_info.dex_pc - pc2dex_dalvik_offset);
269 pc2dex_offset = pc_info.native_pc;
270 pc2dex_dalvik_offset = pc_info.dex_pc;
271 }
272
273 uint32_t total_entries = pc2dex_entries + dex2pc_entries;
274 uint32_t hdr_data_size = UnsignedLeb128Size(total_entries) + UnsignedLeb128Size(pc2dex_entries);
275 uint32_t data_size = hdr_data_size + pc2dex_data_size + dex2pc_data_size;
276 data->resize(data_size);
277
278 uint8_t* data_ptr = &(*data)[0];
279 uint8_t* write_pos = data_ptr;
280 write_pos = EncodeUnsignedLeb128(write_pos, total_entries);
281 write_pos = EncodeUnsignedLeb128(write_pos, pc2dex_entries);
282 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size);
283 uint8_t* write_pos2 = write_pos + pc2dex_data_size;
284
285 pc2dex_offset = 0u;
286 pc2dex_dalvik_offset = 0u;
287 for (size_t i = 0; i < pc2dex_entries; i++) {
288 struct PcInfo pc_info = pc_infos_.Get(i);
289 DCHECK(pc2dex_offset <= pc_info.native_pc);
290 write_pos = EncodeUnsignedLeb128(write_pos, pc_info.native_pc - pc2dex_offset);
291 write_pos = EncodeSignedLeb128(write_pos, pc_info.dex_pc - pc2dex_dalvik_offset);
292 pc2dex_offset = pc_info.native_pc;
293 pc2dex_dalvik_offset = pc_info.dex_pc;
294 }
295 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size + pc2dex_data_size);
296 DCHECK_EQ(static_cast<size_t>(write_pos2 - data_ptr), data_size);
297
298 if (kIsDebugBuild) {
299 // Verify the encoded table holds the expected data.
300 MappingTable table(data_ptr);
301 CHECK_EQ(table.TotalSize(), total_entries);
302 CHECK_EQ(table.PcToDexSize(), pc2dex_entries);
303 auto it = table.PcToDexBegin();
304 auto it2 = table.DexToPcBegin();
305 for (size_t i = 0; i < pc2dex_entries; i++) {
306 struct PcInfo pc_info = pc_infos_.Get(i);
307 CHECK_EQ(pc_info.native_pc, it.NativePcOffset());
308 CHECK_EQ(pc_info.dex_pc, it.DexPc());
309 ++it;
310 }
311 CHECK(it == table.PcToDexEnd());
312 CHECK(it2 == table.DexToPcEnd());
313 }
314}
315
316void CodeGenerator::BuildVMapTable(std::vector<uint8_t>* data) const {
317 Leb128EncodingVector vmap_encoder;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100318 // We currently don't use callee-saved registers.
319 size_t size = 0 + 1 /* marker */ + 0;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000320 vmap_encoder.Reserve(size + 1u); // All values are likely to be one byte in ULEB128 (<128).
321 vmap_encoder.PushBackUnsigned(size);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000322 vmap_encoder.PushBackUnsigned(VmapTable::kAdjustedFpMarker);
323
324 *data = vmap_encoder.GetData();
325}
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000326
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000327} // namespace art