Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 1 | /* |
| 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 Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 21 | #include "code_generator_x86_64.h" |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 22 | #include "dex/verified_method.h" |
| 23 | #include "driver/dex_compilation_unit.h" |
| 24 | #include "gc_map_builder.h" |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 25 | #include "leb128.h" |
| 26 | #include "mapping_table.h" |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 27 | #include "utils/assembler.h" |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 28 | #include "verifier/dex_gc_map.h" |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 29 | #include "vmap_table.h" |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 30 | |
| 31 | namespace art { |
| 32 | |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 33 | void CodeGenerator::CompileBaseline(CodeAllocator* allocator) { |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 34 | const GrowableArray<HBasicBlock*>& blocks = GetGraph()->GetBlocks(); |
| 35 | DCHECK(blocks.Get(0) == GetGraph()->GetEntryBlock()); |
| 36 | DCHECK(GoesToNextBlock(GetGraph()->GetEntryBlock(), blocks.Get(1))); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 37 | block_labels_.SetSize(blocks.Size()); |
| 38 | |
| 39 | DCHECK_EQ(frame_size_, kUninitializedFrameSize); |
| 40 | ComputeFrameSize(GetGraph()->GetMaximumNumberOfOutVRegs() |
| 41 | + GetGraph()->GetNumberOfVRegs() |
| 42 | + 1 /* filler */); |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 43 | GenerateFrameEntry(); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 44 | |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 45 | for (size_t i = 0, e = blocks.Size(); i < e; ++i) { |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 46 | HBasicBlock* block = blocks.Get(i); |
| 47 | Bind(GetLabelOf(block)); |
| 48 | HGraphVisitor* location_builder = GetLocationBuilder(); |
| 49 | HGraphVisitor* instruction_visitor = GetInstructionVisitor(); |
| 50 | for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) { |
| 51 | HInstruction* current = it.Current(); |
| 52 | current->Accept(location_builder); |
| 53 | InitLocations(current); |
| 54 | current->Accept(instruction_visitor); |
| 55 | } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 56 | } |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 57 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 58 | size_t code_size = GetAssembler()->CodeSize(); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 59 | uint8_t* buffer = allocator->Allocate(code_size); |
| 60 | MemoryRegion code(buffer, code_size); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 61 | GetAssembler()->FinalizeInstructions(code); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 64 | void CodeGenerator::CompileOptimized(CodeAllocator* allocator) { |
| 65 | // The frame size has already been computed during register allocation. |
| 66 | DCHECK_NE(frame_size_, kUninitializedFrameSize); |
| 67 | const GrowableArray<HBasicBlock*>& blocks = GetGraph()->GetBlocks(); |
| 68 | DCHECK(blocks.Get(0) == GetGraph()->GetEntryBlock()); |
| 69 | DCHECK(GoesToNextBlock(GetGraph()->GetEntryBlock(), blocks.Get(1))); |
| 70 | block_labels_.SetSize(blocks.Size()); |
| 71 | |
| 72 | GenerateFrameEntry(); |
| 73 | for (size_t i = 0, e = blocks.Size(); i < e; ++i) { |
| 74 | HBasicBlock* block = blocks.Get(i); |
| 75 | Bind(GetLabelOf(block)); |
| 76 | HGraphVisitor* instruction_visitor = GetInstructionVisitor(); |
| 77 | for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) { |
| 78 | HInstruction* current = it.Current(); |
| 79 | current->Accept(instruction_visitor); |
| 80 | } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 81 | } |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 82 | |
| 83 | size_t code_size = GetAssembler()->CodeSize(); |
| 84 | uint8_t* buffer = allocator->Allocate(code_size); |
| 85 | MemoryRegion code(buffer, code_size); |
| 86 | GetAssembler()->FinalizeInstructions(code); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 89 | size_t CodeGenerator::AllocateFreeRegisterInternal( |
| 90 | bool* blocked_registers, size_t number_of_registers) const { |
| 91 | for (size_t regno = 0; regno < number_of_registers; regno++) { |
| 92 | if (!blocked_registers[regno]) { |
| 93 | blocked_registers[regno] = true; |
| 94 | return regno; |
| 95 | } |
| 96 | } |
| 97 | LOG(FATAL) << "Unreachable"; |
| 98 | return -1; |
| 99 | } |
| 100 | |
| 101 | |
| 102 | void CodeGenerator::AllocateRegistersLocally(HInstruction* instruction) const { |
| 103 | LocationSummary* locations = instruction->GetLocations(); |
| 104 | if (locations == nullptr) return; |
| 105 | |
| 106 | for (size_t i = 0, e = GetNumberOfRegisters(); i < e; ++i) { |
| 107 | blocked_registers_[i] = false; |
| 108 | } |
| 109 | |
| 110 | // Mark all fixed input, temp and output registers as used. |
| 111 | for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) { |
| 112 | Location loc = locations->InAt(i); |
| 113 | if (loc.IsRegister()) { |
| 114 | // Check that a register is not specified twice in the summary. |
| 115 | DCHECK(!blocked_registers_[loc.GetEncoding()]); |
| 116 | blocked_registers_[loc.GetEncoding()] = true; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) { |
| 121 | Location loc = locations->GetTemp(i); |
| 122 | if (loc.IsRegister()) { |
| 123 | // Check that a register is not specified twice in the summary. |
| 124 | DCHECK(!blocked_registers_[loc.GetEncoding()]); |
| 125 | blocked_registers_[loc.GetEncoding()] = true; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | SetupBlockedRegisters(blocked_registers_); |
| 130 | |
| 131 | // Allocate all unallocated input locations. |
| 132 | for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) { |
| 133 | Location loc = locations->InAt(i); |
| 134 | HInstruction* input = instruction->InputAt(i); |
| 135 | if (loc.IsUnallocated()) { |
| 136 | if (loc.GetPolicy() == Location::kRequiresRegister) { |
| 137 | loc = Location::RegisterLocation( |
| 138 | AllocateFreeRegister(input->GetType(), blocked_registers_)); |
| 139 | } else { |
| 140 | DCHECK_EQ(loc.GetPolicy(), Location::kAny); |
| 141 | HLoadLocal* load = input->AsLoadLocal(); |
| 142 | if (load != nullptr) { |
| 143 | loc = GetStackLocation(load); |
| 144 | } else { |
| 145 | loc = Location::RegisterLocation( |
| 146 | AllocateFreeRegister(input->GetType(), blocked_registers_)); |
| 147 | } |
| 148 | } |
| 149 | locations->SetInAt(i, loc); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | // Allocate all unallocated temp locations. |
| 154 | for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) { |
| 155 | Location loc = locations->GetTemp(i); |
| 156 | if (loc.IsUnallocated()) { |
| 157 | DCHECK_EQ(loc.GetPolicy(), Location::kRequiresRegister); |
| 158 | // TODO: Adjust handling of temps. We currently consider temps to use |
| 159 | // core registers. They may also use floating point registers at some point. |
| 160 | loc = Location::RegisterLocation(static_cast<ManagedRegister>( |
| 161 | AllocateFreeRegister(Primitive::kPrimInt, blocked_registers_))); |
| 162 | locations->SetTempAt(i, loc); |
| 163 | } |
| 164 | } |
| 165 | |
Nicolas Geoffray | f529d77 | 2014-05-02 11:03:30 +0100 | [diff] [blame] | 166 | // Make all registers available for the return value. |
| 167 | for (size_t i = 0, e = GetNumberOfRegisters(); i < e; ++i) { |
| 168 | blocked_registers_[i] = false; |
| 169 | } |
| 170 | SetupBlockedRegisters(blocked_registers_); |
| 171 | |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 172 | Location result_location = locations->Out(); |
| 173 | if (result_location.IsUnallocated()) { |
| 174 | switch (result_location.GetPolicy()) { |
| 175 | case Location::kAny: |
| 176 | case Location::kRequiresRegister: |
| 177 | result_location = Location::RegisterLocation( |
| 178 | AllocateFreeRegister(instruction->GetType(), blocked_registers_)); |
| 179 | break; |
| 180 | case Location::kSameAsFirstInput: |
| 181 | result_location = locations->InAt(0); |
| 182 | break; |
| 183 | } |
| 184 | locations->SetOut(result_location); |
| 185 | } |
| 186 | } |
| 187 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 188 | void CodeGenerator::InitLocations(HInstruction* instruction) { |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 189 | if (instruction->GetLocations() == nullptr) { |
| 190 | return; |
| 191 | } |
| 192 | AllocateRegistersLocally(instruction); |
| 193 | for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 194 | Location location = instruction->GetLocations()->InAt(i); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 195 | if (location.IsValid()) { |
| 196 | // Move the input to the desired location. |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 197 | Move(instruction->InputAt(i), location, instruction); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | bool CodeGenerator::GoesToNextBlock(HBasicBlock* current, HBasicBlock* next) const { |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 203 | // We currently iterate over the block in insertion order. |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 204 | return current->GetBlockId() + 1 == next->GetBlockId(); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | Label* CodeGenerator::GetLabelOf(HBasicBlock* block) const { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 208 | return block_labels_.GetRawStorage() + block->GetBlockId(); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 211 | CodeGenerator* CodeGenerator::Create(ArenaAllocator* allocator, |
| 212 | HGraph* graph, |
| 213 | InstructionSet instruction_set) { |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 214 | switch (instruction_set) { |
| 215 | case kArm: |
| 216 | case kThumb2: { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 217 | return new (allocator) arm::CodeGeneratorARM(graph); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 218 | } |
| 219 | case kMips: |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 220 | return nullptr; |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 221 | case kX86: { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 222 | return new (allocator) x86::CodeGeneratorX86(graph); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 223 | } |
Dmitry Petrochenko | 6a58cb1 | 2014-04-02 17:27:59 +0700 | [diff] [blame] | 224 | case kX86_64: { |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 225 | return new (allocator) x86_64::CodeGeneratorX86_64(graph); |
Dmitry Petrochenko | 6a58cb1 | 2014-04-02 17:27:59 +0700 | [diff] [blame] | 226 | } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 227 | default: |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 228 | return nullptr; |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 232 | void CodeGenerator::BuildNativeGCMap( |
| 233 | std::vector<uint8_t>* data, const DexCompilationUnit& dex_compilation_unit) const { |
| 234 | const std::vector<uint8_t>& gc_map_raw = |
| 235 | dex_compilation_unit.GetVerifiedMethod()->GetDexGcMap(); |
| 236 | verifier::DexPcToReferenceMap dex_gc_map(&(gc_map_raw)[0]); |
| 237 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 238 | uint32_t max_native_offset = 0; |
| 239 | for (size_t i = 0; i < pc_infos_.Size(); i++) { |
| 240 | uint32_t native_offset = pc_infos_.Get(i).native_pc; |
| 241 | if (native_offset > max_native_offset) { |
| 242 | max_native_offset = native_offset; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | GcMapBuilder builder(data, pc_infos_.Size(), max_native_offset, dex_gc_map.RegWidth()); |
| 247 | for (size_t i = 0; i < pc_infos_.Size(); i++) { |
| 248 | struct PcInfo pc_info = pc_infos_.Get(i); |
| 249 | uint32_t native_offset = pc_info.native_pc; |
| 250 | uint32_t dex_pc = pc_info.dex_pc; |
| 251 | const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false); |
| 252 | CHECK(references != NULL) << "Missing ref for dex pc 0x" << std::hex << dex_pc; |
| 253 | builder.AddEntry(native_offset, references); |
| 254 | } |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 257 | void CodeGenerator::BuildMappingTable(std::vector<uint8_t>* data) const { |
| 258 | uint32_t pc2dex_data_size = 0u; |
| 259 | uint32_t pc2dex_entries = pc_infos_.Size(); |
| 260 | uint32_t pc2dex_offset = 0u; |
| 261 | int32_t pc2dex_dalvik_offset = 0; |
| 262 | uint32_t dex2pc_data_size = 0u; |
| 263 | uint32_t dex2pc_entries = 0u; |
| 264 | |
| 265 | // We currently only have pc2dex entries. |
| 266 | for (size_t i = 0; i < pc2dex_entries; i++) { |
| 267 | struct PcInfo pc_info = pc_infos_.Get(i); |
| 268 | pc2dex_data_size += UnsignedLeb128Size(pc_info.native_pc - pc2dex_offset); |
| 269 | pc2dex_data_size += SignedLeb128Size(pc_info.dex_pc - pc2dex_dalvik_offset); |
| 270 | pc2dex_offset = pc_info.native_pc; |
| 271 | pc2dex_dalvik_offset = pc_info.dex_pc; |
| 272 | } |
| 273 | |
| 274 | uint32_t total_entries = pc2dex_entries + dex2pc_entries; |
| 275 | uint32_t hdr_data_size = UnsignedLeb128Size(total_entries) + UnsignedLeb128Size(pc2dex_entries); |
| 276 | uint32_t data_size = hdr_data_size + pc2dex_data_size + dex2pc_data_size; |
| 277 | data->resize(data_size); |
| 278 | |
| 279 | uint8_t* data_ptr = &(*data)[0]; |
| 280 | uint8_t* write_pos = data_ptr; |
| 281 | write_pos = EncodeUnsignedLeb128(write_pos, total_entries); |
| 282 | write_pos = EncodeUnsignedLeb128(write_pos, pc2dex_entries); |
| 283 | DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size); |
| 284 | uint8_t* write_pos2 = write_pos + pc2dex_data_size; |
| 285 | |
| 286 | pc2dex_offset = 0u; |
| 287 | pc2dex_dalvik_offset = 0u; |
| 288 | for (size_t i = 0; i < pc2dex_entries; i++) { |
| 289 | struct PcInfo pc_info = pc_infos_.Get(i); |
| 290 | DCHECK(pc2dex_offset <= pc_info.native_pc); |
| 291 | write_pos = EncodeUnsignedLeb128(write_pos, pc_info.native_pc - pc2dex_offset); |
| 292 | write_pos = EncodeSignedLeb128(write_pos, pc_info.dex_pc - pc2dex_dalvik_offset); |
| 293 | pc2dex_offset = pc_info.native_pc; |
| 294 | pc2dex_dalvik_offset = pc_info.dex_pc; |
| 295 | } |
| 296 | DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size + pc2dex_data_size); |
| 297 | DCHECK_EQ(static_cast<size_t>(write_pos2 - data_ptr), data_size); |
| 298 | |
| 299 | if (kIsDebugBuild) { |
| 300 | // Verify the encoded table holds the expected data. |
| 301 | MappingTable table(data_ptr); |
| 302 | CHECK_EQ(table.TotalSize(), total_entries); |
| 303 | CHECK_EQ(table.PcToDexSize(), pc2dex_entries); |
| 304 | auto it = table.PcToDexBegin(); |
| 305 | auto it2 = table.DexToPcBegin(); |
| 306 | for (size_t i = 0; i < pc2dex_entries; i++) { |
| 307 | struct PcInfo pc_info = pc_infos_.Get(i); |
| 308 | CHECK_EQ(pc_info.native_pc, it.NativePcOffset()); |
| 309 | CHECK_EQ(pc_info.dex_pc, it.DexPc()); |
| 310 | ++it; |
| 311 | } |
| 312 | CHECK(it == table.PcToDexEnd()); |
| 313 | CHECK(it2 == table.DexToPcEnd()); |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | void CodeGenerator::BuildVMapTable(std::vector<uint8_t>* data) const { |
| 318 | Leb128EncodingVector vmap_encoder; |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 319 | // We currently don't use callee-saved registers. |
| 320 | size_t size = 0 + 1 /* marker */ + 0; |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 321 | vmap_encoder.Reserve(size + 1u); // All values are likely to be one byte in ULEB128 (<128). |
| 322 | vmap_encoder.PushBackUnsigned(size); |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 323 | vmap_encoder.PushBackUnsigned(VmapTable::kAdjustedFpMarker); |
| 324 | |
| 325 | *data = vmap_encoder.GetData(); |
| 326 | } |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 327 | |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 328 | } // namespace art |