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" |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame^] | 20 | #include "code_generator_arm64.h" |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 21 | #include "code_generator_x86.h" |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 22 | #include "code_generator_x86_64.h" |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 23 | #include "compiled_method.h" |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 24 | #include "dex/verified_method.h" |
| 25 | #include "driver/dex_compilation_unit.h" |
| 26 | #include "gc_map_builder.h" |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 27 | #include "leb128.h" |
| 28 | #include "mapping_table.h" |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 29 | #include "ssa_liveness_analysis.h" |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 30 | #include "utils/assembler.h" |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 31 | #include "verifier/dex_gc_map.h" |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 32 | #include "vmap_table.h" |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 33 | |
| 34 | namespace art { |
| 35 | |
Nicolas Geoffray | 73e80c3 | 2014-07-22 17:47:56 +0100 | [diff] [blame] | 36 | void CodeGenerator::CompileBaseline(CodeAllocator* allocator, bool is_leaf) { |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 37 | const GrowableArray<HBasicBlock*>& blocks = GetGraph()->GetBlocks(); |
| 38 | DCHECK(blocks.Get(0) == GetGraph()->GetEntryBlock()); |
| 39 | DCHECK(GoesToNextBlock(GetGraph()->GetEntryBlock(), blocks.Get(1))); |
Nicolas Geoffray | 92a73ae | 2014-10-16 11:12:52 +0100 | [diff] [blame] | 40 | Initialize(); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 41 | |
| 42 | DCHECK_EQ(frame_size_, kUninitializedFrameSize); |
Nicolas Geoffray | 73e80c3 | 2014-07-22 17:47:56 +0100 | [diff] [blame] | 43 | if (!is_leaf) { |
| 44 | MarkNotLeaf(); |
| 45 | } |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 46 | ComputeFrameSize(GetGraph()->GetNumberOfLocalVRegs() |
| 47 | + GetGraph()->GetNumberOfTemporaries() |
| 48 | + 1 /* filler */, |
Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 49 | 0, /* the baseline compiler does not have live registers at slow path */ |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 50 | GetGraph()->GetMaximumNumberOfOutVRegs() |
| 51 | + 1 /* current method */); |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 52 | GenerateFrameEntry(); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 53 | |
Nicolas Geoffray | 8a16d97 | 2014-09-11 10:30:02 +0100 | [diff] [blame] | 54 | HGraphVisitor* location_builder = GetLocationBuilder(); |
| 55 | HGraphVisitor* instruction_visitor = GetInstructionVisitor(); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 56 | for (size_t i = 0, e = blocks.Size(); i < e; ++i) { |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 57 | HBasicBlock* block = blocks.Get(i); |
Nicolas Geoffray | 92a73ae | 2014-10-16 11:12:52 +0100 | [diff] [blame] | 58 | Bind(block); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 59 | for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) { |
| 60 | HInstruction* current = it.Current(); |
| 61 | current->Accept(location_builder); |
| 62 | InitLocations(current); |
| 63 | current->Accept(instruction_visitor); |
| 64 | } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 65 | } |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 66 | GenerateSlowPaths(); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 67 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 68 | size_t code_size = GetAssembler()->CodeSize(); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 69 | uint8_t* buffer = allocator->Allocate(code_size); |
| 70 | MemoryRegion code(buffer, code_size); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 71 | GetAssembler()->FinalizeInstructions(code); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 74 | void CodeGenerator::CompileOptimized(CodeAllocator* allocator) { |
| 75 | // The frame size has already been computed during register allocation. |
| 76 | DCHECK_NE(frame_size_, kUninitializedFrameSize); |
| 77 | const GrowableArray<HBasicBlock*>& blocks = GetGraph()->GetBlocks(); |
| 78 | DCHECK(blocks.Get(0) == GetGraph()->GetEntryBlock()); |
| 79 | DCHECK(GoesToNextBlock(GetGraph()->GetEntryBlock(), blocks.Get(1))); |
Nicolas Geoffray | 92a73ae | 2014-10-16 11:12:52 +0100 | [diff] [blame] | 80 | Initialize(); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 81 | |
| 82 | GenerateFrameEntry(); |
Nicolas Geoffray | 8a16d97 | 2014-09-11 10:30:02 +0100 | [diff] [blame] | 83 | HGraphVisitor* instruction_visitor = GetInstructionVisitor(); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 84 | for (size_t i = 0, e = blocks.Size(); i < e; ++i) { |
| 85 | HBasicBlock* block = blocks.Get(i); |
Nicolas Geoffray | 92a73ae | 2014-10-16 11:12:52 +0100 | [diff] [blame] | 86 | Bind(block); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 87 | for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) { |
| 88 | HInstruction* current = it.Current(); |
| 89 | current->Accept(instruction_visitor); |
| 90 | } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 91 | } |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 92 | GenerateSlowPaths(); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 93 | |
| 94 | size_t code_size = GetAssembler()->CodeSize(); |
| 95 | uint8_t* buffer = allocator->Allocate(code_size); |
| 96 | MemoryRegion code(buffer, code_size); |
| 97 | GetAssembler()->FinalizeInstructions(code); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 100 | void CodeGenerator::GenerateSlowPaths() { |
| 101 | for (size_t i = 0, e = slow_paths_.Size(); i < e; ++i) { |
| 102 | slow_paths_.Get(i)->EmitNativeCode(this); |
| 103 | } |
| 104 | } |
| 105 | |
Nicolas Geoffray | 71175b7 | 2014-10-09 22:13:55 +0100 | [diff] [blame] | 106 | size_t CodeGenerator::FindFreeEntry(bool* array, size_t length) { |
| 107 | for (size_t i = 0; i < length; ++i) { |
| 108 | if (!array[i]) { |
| 109 | array[i] = true; |
| 110 | return i; |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 111 | } |
| 112 | } |
Nicolas Geoffray | 71175b7 | 2014-10-09 22:13:55 +0100 | [diff] [blame] | 113 | LOG(FATAL) << "Could not find a register in baseline register allocator"; |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 114 | return -1; |
| 115 | } |
| 116 | |
Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 117 | void CodeGenerator::ComputeFrameSize(size_t number_of_spill_slots, |
| 118 | size_t maximum_number_of_live_registers, |
| 119 | size_t number_of_out_slots) { |
| 120 | first_register_slot_in_slow_path_ = (number_of_out_slots + number_of_spill_slots) * kVRegSize; |
| 121 | |
Nicolas Geoffray | ab032bc | 2014-07-15 12:55:21 +0100 | [diff] [blame] | 122 | SetFrameSize(RoundUp( |
| 123 | number_of_spill_slots * kVRegSize |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 124 | + number_of_out_slots * kVRegSize |
Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 125 | + maximum_number_of_live_registers * GetWordSize() |
Nicolas Geoffray | ab032bc | 2014-07-15 12:55:21 +0100 | [diff] [blame] | 126 | + FrameEntrySpillSize(), |
| 127 | kStackAlignment)); |
| 128 | } |
| 129 | |
| 130 | Location CodeGenerator::GetTemporaryLocation(HTemporary* temp) const { |
| 131 | uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs(); |
| 132 | // Use the temporary region (right below the dex registers). |
| 133 | int32_t slot = GetFrameSize() - FrameEntrySpillSize() |
| 134 | - kVRegSize // filler |
| 135 | - (number_of_locals * kVRegSize) |
| 136 | - ((1 + temp->GetIndex()) * kVRegSize); |
| 137 | return Location::StackSlot(slot); |
| 138 | } |
| 139 | |
| 140 | int32_t CodeGenerator::GetStackSlot(HLocal* local) const { |
| 141 | uint16_t reg_number = local->GetRegNumber(); |
| 142 | uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs(); |
| 143 | if (reg_number >= number_of_locals) { |
| 144 | // Local is a parameter of the method. It is stored in the caller's frame. |
| 145 | return GetFrameSize() + kVRegSize // ART method |
| 146 | + (reg_number - number_of_locals) * kVRegSize; |
| 147 | } else { |
| 148 | // Local is a temporary in this method. It is stored in this method's frame. |
| 149 | return GetFrameSize() - FrameEntrySpillSize() |
| 150 | - kVRegSize // filler. |
| 151 | - (number_of_locals * kVRegSize) |
| 152 | + (reg_number * kVRegSize); |
| 153 | } |
| 154 | } |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 155 | |
| 156 | void CodeGenerator::AllocateRegistersLocally(HInstruction* instruction) const { |
| 157 | LocationSummary* locations = instruction->GetLocations(); |
| 158 | if (locations == nullptr) return; |
| 159 | |
Nicolas Geoffray | 71175b7 | 2014-10-09 22:13:55 +0100 | [diff] [blame] | 160 | for (size_t i = 0, e = GetNumberOfCoreRegisters(); i < e; ++i) { |
| 161 | blocked_core_registers_[i] = false; |
| 162 | } |
| 163 | |
| 164 | for (size_t i = 0, e = GetNumberOfFloatingPointRegisters(); i < e; ++i) { |
| 165 | blocked_fpu_registers_[i] = false; |
| 166 | } |
| 167 | |
| 168 | for (size_t i = 0, e = number_of_register_pairs_; i < e; ++i) { |
| 169 | blocked_register_pairs_[i] = false; |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | // Mark all fixed input, temp and output registers as used. |
| 173 | for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) { |
| 174 | Location loc = locations->InAt(i); |
Nicolas Geoffray | 71175b7 | 2014-10-09 22:13:55 +0100 | [diff] [blame] | 175 | // The DCHECKS below check that a register is not specified twice in |
| 176 | // the summary. |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 177 | if (loc.IsRegister()) { |
Nicolas Geoffray | 71175b7 | 2014-10-09 22:13:55 +0100 | [diff] [blame] | 178 | DCHECK(!blocked_core_registers_[loc.reg()]); |
| 179 | blocked_core_registers_[loc.reg()] = true; |
| 180 | } else if (loc.IsFpuRegister()) { |
| 181 | DCHECK(!blocked_fpu_registers_[loc.reg()]); |
| 182 | blocked_fpu_registers_[loc.reg()] = true; |
| 183 | } else if (loc.IsRegisterPair()) { |
| 184 | DCHECK(!blocked_core_registers_[loc.AsRegisterPairLow<int>()]); |
| 185 | blocked_core_registers_[loc.AsRegisterPairLow<int>()] = true; |
| 186 | DCHECK(!blocked_core_registers_[loc.AsRegisterPairHigh<int>()]); |
| 187 | blocked_core_registers_[loc.AsRegisterPairHigh<int>()] = true; |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | |
| 191 | for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) { |
| 192 | Location loc = locations->GetTemp(i); |
| 193 | if (loc.IsRegister()) { |
| 194 | // Check that a register is not specified twice in the summary. |
Nicolas Geoffray | 71175b7 | 2014-10-09 22:13:55 +0100 | [diff] [blame] | 195 | DCHECK(!blocked_core_registers_[loc.reg()]); |
| 196 | blocked_core_registers_[loc.reg()] = true; |
| 197 | } else { |
| 198 | DCHECK_EQ(loc.GetPolicy(), Location::kRequiresRegister); |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | |
Nicolas Geoffray | 71175b7 | 2014-10-09 22:13:55 +0100 | [diff] [blame] | 202 | SetupBlockedRegisters(); |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 203 | |
| 204 | // Allocate all unallocated input locations. |
| 205 | for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) { |
| 206 | Location loc = locations->InAt(i); |
| 207 | HInstruction* input = instruction->InputAt(i); |
| 208 | if (loc.IsUnallocated()) { |
Nicolas Geoffray | 56b9ee6 | 2014-10-09 11:47:51 +0100 | [diff] [blame] | 209 | if ((loc.GetPolicy() == Location::kRequiresRegister) |
| 210 | || (loc.GetPolicy() == Location::kRequiresFpuRegister)) { |
Nicolas Geoffray | 71175b7 | 2014-10-09 22:13:55 +0100 | [diff] [blame] | 211 | loc = AllocateFreeRegister(input->GetType()); |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 212 | } else { |
| 213 | DCHECK_EQ(loc.GetPolicy(), Location::kAny); |
| 214 | HLoadLocal* load = input->AsLoadLocal(); |
| 215 | if (load != nullptr) { |
| 216 | loc = GetStackLocation(load); |
| 217 | } else { |
Nicolas Geoffray | 71175b7 | 2014-10-09 22:13:55 +0100 | [diff] [blame] | 218 | loc = AllocateFreeRegister(input->GetType()); |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | locations->SetInAt(i, loc); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | // Allocate all unallocated temp locations. |
| 226 | for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) { |
| 227 | Location loc = locations->GetTemp(i); |
| 228 | if (loc.IsUnallocated()) { |
| 229 | DCHECK_EQ(loc.GetPolicy(), Location::kRequiresRegister); |
| 230 | // TODO: Adjust handling of temps. We currently consider temps to use |
| 231 | // core registers. They may also use floating point registers at some point. |
Nicolas Geoffray | 71175b7 | 2014-10-09 22:13:55 +0100 | [diff] [blame] | 232 | loc = AllocateFreeRegister(Primitive::kPrimInt); |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 233 | locations->SetTempAt(i, loc); |
| 234 | } |
| 235 | } |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 236 | Location result_location = locations->Out(); |
| 237 | if (result_location.IsUnallocated()) { |
| 238 | switch (result_location.GetPolicy()) { |
| 239 | case Location::kAny: |
| 240 | case Location::kRequiresRegister: |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 241 | case Location::kRequiresFpuRegister: |
Nicolas Geoffray | 71175b7 | 2014-10-09 22:13:55 +0100 | [diff] [blame] | 242 | result_location = AllocateFreeRegister(instruction->GetType()); |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 243 | break; |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 244 | case Location::kSameAsFirstInput: |
| 245 | result_location = locations->InAt(0); |
| 246 | break; |
| 247 | } |
| 248 | locations->SetOut(result_location); |
| 249 | } |
| 250 | } |
| 251 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 252 | void CodeGenerator::InitLocations(HInstruction* instruction) { |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 253 | if (instruction->GetLocations() == nullptr) { |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 254 | if (instruction->IsTemporary()) { |
| 255 | HInstruction* previous = instruction->GetPrevious(); |
| 256 | Location temp_location = GetTemporaryLocation(instruction->AsTemporary()); |
| 257 | Move(previous, temp_location, instruction); |
| 258 | previous->GetLocations()->SetOut(temp_location); |
| 259 | } |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 260 | return; |
| 261 | } |
| 262 | AllocateRegistersLocally(instruction); |
| 263 | for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 264 | Location location = instruction->GetLocations()->InAt(i); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 265 | if (location.IsValid()) { |
| 266 | // Move the input to the desired location. |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 267 | Move(instruction->InputAt(i), location, instruction); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | bool CodeGenerator::GoesToNextBlock(HBasicBlock* current, HBasicBlock* next) const { |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 273 | // We currently iterate over the block in insertion order. |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 274 | return current->GetBlockId() + 1 == next->GetBlockId(); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 277 | CodeGenerator* CodeGenerator::Create(ArenaAllocator* allocator, |
| 278 | HGraph* graph, |
| 279 | InstructionSet instruction_set) { |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 280 | switch (instruction_set) { |
| 281 | case kArm: |
| 282 | case kThumb2: { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 283 | return new (allocator) arm::CodeGeneratorARM(graph); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 284 | } |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame^] | 285 | case kArm64: { |
| 286 | return new (allocator) arm64::CodeGeneratorARM64(graph); |
| 287 | } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 288 | case kMips: |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 289 | return nullptr; |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 290 | case kX86: { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 291 | return new (allocator) x86::CodeGeneratorX86(graph); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 292 | } |
Dmitry Petrochenko | 6a58cb1 | 2014-04-02 17:27:59 +0700 | [diff] [blame] | 293 | case kX86_64: { |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 294 | return new (allocator) x86_64::CodeGeneratorX86_64(graph); |
Dmitry Petrochenko | 6a58cb1 | 2014-04-02 17:27:59 +0700 | [diff] [blame] | 295 | } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 296 | default: |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 297 | return nullptr; |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 301 | void CodeGenerator::BuildNativeGCMap( |
| 302 | std::vector<uint8_t>* data, const DexCompilationUnit& dex_compilation_unit) const { |
| 303 | const std::vector<uint8_t>& gc_map_raw = |
| 304 | dex_compilation_unit.GetVerifiedMethod()->GetDexGcMap(); |
| 305 | verifier::DexPcToReferenceMap dex_gc_map(&(gc_map_raw)[0]); |
| 306 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 307 | uint32_t max_native_offset = 0; |
| 308 | for (size_t i = 0; i < pc_infos_.Size(); i++) { |
| 309 | uint32_t native_offset = pc_infos_.Get(i).native_pc; |
| 310 | if (native_offset > max_native_offset) { |
| 311 | max_native_offset = native_offset; |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | GcMapBuilder builder(data, pc_infos_.Size(), max_native_offset, dex_gc_map.RegWidth()); |
| 316 | for (size_t i = 0; i < pc_infos_.Size(); i++) { |
| 317 | struct PcInfo pc_info = pc_infos_.Get(i); |
| 318 | uint32_t native_offset = pc_info.native_pc; |
| 319 | uint32_t dex_pc = pc_info.dex_pc; |
| 320 | const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false); |
| 321 | CHECK(references != NULL) << "Missing ref for dex pc 0x" << std::hex << dex_pc; |
| 322 | builder.AddEntry(native_offset, references); |
| 323 | } |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 326 | void CodeGenerator::BuildMappingTable(std::vector<uint8_t>* data, SrcMap* src_map) const { |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 327 | uint32_t pc2dex_data_size = 0u; |
| 328 | uint32_t pc2dex_entries = pc_infos_.Size(); |
| 329 | uint32_t pc2dex_offset = 0u; |
| 330 | int32_t pc2dex_dalvik_offset = 0; |
| 331 | uint32_t dex2pc_data_size = 0u; |
| 332 | uint32_t dex2pc_entries = 0u; |
| 333 | |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 334 | if (src_map != nullptr) { |
| 335 | src_map->reserve(pc2dex_entries); |
| 336 | } |
| 337 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 338 | // We currently only have pc2dex entries. |
| 339 | for (size_t i = 0; i < pc2dex_entries; i++) { |
| 340 | struct PcInfo pc_info = pc_infos_.Get(i); |
| 341 | pc2dex_data_size += UnsignedLeb128Size(pc_info.native_pc - pc2dex_offset); |
| 342 | pc2dex_data_size += SignedLeb128Size(pc_info.dex_pc - pc2dex_dalvik_offset); |
| 343 | pc2dex_offset = pc_info.native_pc; |
| 344 | pc2dex_dalvik_offset = pc_info.dex_pc; |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 345 | if (src_map != nullptr) { |
| 346 | src_map->push_back(SrcMapElem({pc2dex_offset, pc2dex_dalvik_offset})); |
| 347 | } |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | uint32_t total_entries = pc2dex_entries + dex2pc_entries; |
| 351 | uint32_t hdr_data_size = UnsignedLeb128Size(total_entries) + UnsignedLeb128Size(pc2dex_entries); |
| 352 | uint32_t data_size = hdr_data_size + pc2dex_data_size + dex2pc_data_size; |
| 353 | data->resize(data_size); |
| 354 | |
| 355 | uint8_t* data_ptr = &(*data)[0]; |
| 356 | uint8_t* write_pos = data_ptr; |
| 357 | write_pos = EncodeUnsignedLeb128(write_pos, total_entries); |
| 358 | write_pos = EncodeUnsignedLeb128(write_pos, pc2dex_entries); |
| 359 | DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size); |
| 360 | uint8_t* write_pos2 = write_pos + pc2dex_data_size; |
| 361 | |
| 362 | pc2dex_offset = 0u; |
| 363 | pc2dex_dalvik_offset = 0u; |
| 364 | for (size_t i = 0; i < pc2dex_entries; i++) { |
| 365 | struct PcInfo pc_info = pc_infos_.Get(i); |
| 366 | DCHECK(pc2dex_offset <= pc_info.native_pc); |
| 367 | write_pos = EncodeUnsignedLeb128(write_pos, pc_info.native_pc - pc2dex_offset); |
| 368 | write_pos = EncodeSignedLeb128(write_pos, pc_info.dex_pc - pc2dex_dalvik_offset); |
| 369 | pc2dex_offset = pc_info.native_pc; |
| 370 | pc2dex_dalvik_offset = pc_info.dex_pc; |
| 371 | } |
| 372 | DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size + pc2dex_data_size); |
| 373 | DCHECK_EQ(static_cast<size_t>(write_pos2 - data_ptr), data_size); |
| 374 | |
| 375 | if (kIsDebugBuild) { |
| 376 | // Verify the encoded table holds the expected data. |
| 377 | MappingTable table(data_ptr); |
| 378 | CHECK_EQ(table.TotalSize(), total_entries); |
| 379 | CHECK_EQ(table.PcToDexSize(), pc2dex_entries); |
| 380 | auto it = table.PcToDexBegin(); |
| 381 | auto it2 = table.DexToPcBegin(); |
| 382 | for (size_t i = 0; i < pc2dex_entries; i++) { |
| 383 | struct PcInfo pc_info = pc_infos_.Get(i); |
| 384 | CHECK_EQ(pc_info.native_pc, it.NativePcOffset()); |
| 385 | CHECK_EQ(pc_info.dex_pc, it.DexPc()); |
| 386 | ++it; |
| 387 | } |
| 388 | CHECK(it == table.PcToDexEnd()); |
| 389 | CHECK(it2 == table.DexToPcEnd()); |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | void CodeGenerator::BuildVMapTable(std::vector<uint8_t>* data) const { |
| 394 | Leb128EncodingVector vmap_encoder; |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 395 | // We currently don't use callee-saved registers. |
| 396 | size_t size = 0 + 1 /* marker */ + 0; |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 397 | vmap_encoder.Reserve(size + 1u); // All values are likely to be one byte in ULEB128 (<128). |
| 398 | vmap_encoder.PushBackUnsigned(size); |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 399 | vmap_encoder.PushBackUnsigned(VmapTable::kAdjustedFpMarker); |
| 400 | |
| 401 | *data = vmap_encoder.GetData(); |
| 402 | } |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 403 | |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 404 | void CodeGenerator::BuildStackMaps(std::vector<uint8_t>* data) { |
| 405 | uint32_t size = stack_map_stream_.ComputeNeededSize(); |
| 406 | data->resize(size); |
| 407 | MemoryRegion region(data->data(), size); |
| 408 | stack_map_stream_.FillIn(region); |
| 409 | } |
| 410 | |
| 411 | void CodeGenerator::RecordPcInfo(HInstruction* instruction, uint32_t dex_pc) { |
| 412 | // Collect PC infos for the mapping table. |
| 413 | struct PcInfo pc_info; |
| 414 | pc_info.dex_pc = dex_pc; |
| 415 | pc_info.native_pc = GetAssembler()->CodeSize(); |
| 416 | pc_infos_.Add(pc_info); |
| 417 | |
| 418 | // Populate stack map information. |
| 419 | |
| 420 | if (instruction == nullptr) { |
| 421 | // For stack overflow checks. |
| 422 | stack_map_stream_.AddStackMapEntry(dex_pc, pc_info.native_pc, 0, 0, 0, 0); |
| 423 | return; |
| 424 | } |
| 425 | |
| 426 | LocationSummary* locations = instruction->GetLocations(); |
| 427 | HEnvironment* environment = instruction->GetEnvironment(); |
| 428 | |
| 429 | size_t environment_size = instruction->EnvironmentSize(); |
| 430 | |
| 431 | size_t register_mask = 0; |
| 432 | size_t inlining_depth = 0; |
| 433 | stack_map_stream_.AddStackMapEntry( |
| 434 | dex_pc, pc_info.native_pc, register_mask, |
| 435 | locations->GetStackMask(), environment_size, inlining_depth); |
| 436 | |
| 437 | // Walk over the environment, and record the location of dex registers. |
| 438 | for (size_t i = 0; i < environment_size; ++i) { |
| 439 | HInstruction* current = environment->GetInstructionAt(i); |
| 440 | if (current == nullptr) { |
| 441 | stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kNone, 0); |
| 442 | continue; |
| 443 | } |
| 444 | |
| 445 | Location location = locations->GetEnvironmentAt(i); |
| 446 | switch (location.GetKind()) { |
| 447 | case Location::kConstant: { |
| 448 | DCHECK(current == location.GetConstant()); |
| 449 | if (current->IsLongConstant()) { |
| 450 | int64_t value = current->AsLongConstant()->GetValue(); |
| 451 | stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, Low32Bits(value)); |
| 452 | stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, High32Bits(value)); |
| 453 | ++i; |
| 454 | DCHECK_LT(i, environment_size); |
| 455 | } else { |
| 456 | DCHECK(current->IsIntConstant()); |
| 457 | int32_t value = current->AsIntConstant()->GetValue(); |
| 458 | stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, value); |
| 459 | } |
| 460 | break; |
| 461 | } |
| 462 | |
| 463 | case Location::kStackSlot: { |
| 464 | stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInStack, location.GetStackIndex()); |
| 465 | break; |
| 466 | } |
| 467 | |
| 468 | case Location::kDoubleStackSlot: { |
| 469 | stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInStack, location.GetStackIndex()); |
| 470 | stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInStack, |
| 471 | location.GetHighStackIndex(kVRegSize)); |
| 472 | ++i; |
| 473 | DCHECK_LT(i, environment_size); |
| 474 | break; |
| 475 | } |
| 476 | |
| 477 | case Location::kRegister : { |
Nicolas Geoffray | 56b9ee6 | 2014-10-09 11:47:51 +0100 | [diff] [blame] | 478 | int id = location.reg(); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 479 | stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInRegister, id); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 480 | if (current->GetType() == Primitive::kPrimLong) { |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 481 | stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInRegister, id); |
| 482 | ++i; |
| 483 | DCHECK_LT(i, environment_size); |
| 484 | } |
| 485 | break; |
| 486 | } |
| 487 | |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 488 | case Location::kFpuRegister : { |
| 489 | int id = location.reg(); |
| 490 | stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInFpuRegister, id); |
| 491 | if (current->GetType() == Primitive::kPrimDouble) { |
| 492 | stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInFpuRegister, id); |
| 493 | ++i; |
| 494 | DCHECK_LT(i, environment_size); |
| 495 | } |
| 496 | break; |
| 497 | } |
| 498 | |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 499 | default: |
| 500 | LOG(FATAL) << "Unexpected kind " << location.GetKind(); |
| 501 | } |
| 502 | } |
| 503 | } |
| 504 | |
Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 505 | void CodeGenerator::SaveLiveRegisters(LocationSummary* locations) { |
| 506 | RegisterSet* register_set = locations->GetLiveRegisters(); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 507 | size_t stack_offset = first_register_slot_in_slow_path_; |
Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 508 | for (size_t i = 0, e = GetNumberOfCoreRegisters(); i < e; ++i) { |
| 509 | if (register_set->ContainsCoreRegister(i)) { |
Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 510 | // If the register holds an object, update the stack mask. |
| 511 | if (locations->RegisterContainsObject(i)) { |
| 512 | locations->SetStackBit(stack_offset / kVRegSize); |
| 513 | } |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 514 | stack_offset += SaveCoreRegister(stack_offset, i); |
Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 515 | } |
| 516 | } |
| 517 | |
| 518 | for (size_t i = 0, e = GetNumberOfFloatingPointRegisters(); i < e; ++i) { |
| 519 | if (register_set->ContainsFloatingPointRegister(i)) { |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 520 | stack_offset += SaveFloatingPointRegister(stack_offset, i); |
Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 521 | } |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | void CodeGenerator::RestoreLiveRegisters(LocationSummary* locations) { |
| 526 | RegisterSet* register_set = locations->GetLiveRegisters(); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 527 | size_t stack_offset = first_register_slot_in_slow_path_; |
Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 528 | for (size_t i = 0, e = GetNumberOfCoreRegisters(); i < e; ++i) { |
| 529 | if (register_set->ContainsCoreRegister(i)) { |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 530 | stack_offset += RestoreCoreRegister(stack_offset, i); |
Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 531 | } |
| 532 | } |
| 533 | |
| 534 | for (size_t i = 0, e = GetNumberOfFloatingPointRegisters(); i < e; ++i) { |
| 535 | if (register_set->ContainsFloatingPointRegister(i)) { |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 536 | stack_offset += RestoreFloatingPointRegister(stack_offset, i); |
Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 537 | } |
| 538 | } |
| 539 | } |
| 540 | |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 541 | void CodeGenerator::ClearSpillSlotsFromLoopPhisInStackMap(HSuspendCheck* suspend_check) const { |
| 542 | LocationSummary* locations = suspend_check->GetLocations(); |
| 543 | HBasicBlock* block = suspend_check->GetBlock(); |
| 544 | DCHECK(block->GetLoopInformation()->GetSuspendCheck() == suspend_check); |
| 545 | DCHECK(block->IsLoopHeader()); |
| 546 | |
| 547 | for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) { |
| 548 | HInstruction* current = it.Current(); |
| 549 | LiveInterval* interval = current->GetLiveInterval(); |
| 550 | // We only need to clear bits of loop phis containing objects and allocated in register. |
| 551 | // Loop phis allocated on stack already have the object in the stack. |
| 552 | if (current->GetType() == Primitive::kPrimNot |
| 553 | && interval->HasRegister() |
| 554 | && interval->HasSpillSlot()) { |
| 555 | locations->ClearStackBit(interval->GetSpillSlot() / kVRegSize); |
| 556 | } |
| 557 | } |
| 558 | } |
| 559 | |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 560 | } // namespace art |