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