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 | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 44 | DCHECK_EQ(frame_size_, kUninitializedFrameSize); |
Nicolas Geoffray | 4c204ba | 2015-02-03 15:12:35 +0000 | [diff] [blame^] | 45 | |
| 46 | Initialize(); |
Nicolas Geoffray | 73e80c3 | 2014-07-22 17:47:56 +0100 | [diff] [blame] | 47 | if (!is_leaf) { |
| 48 | MarkNotLeaf(); |
| 49 | } |
Nicolas Geoffray | 4c204ba | 2015-02-03 15:12:35 +0000 | [diff] [blame^] | 50 | InitializeCodeGeneration(GetGraph()->GetNumberOfLocalVRegs() |
| 51 | + GetGraph()->GetTemporariesVRegSlots() |
| 52 | + 1 /* filler */, |
| 53 | 0, /* the baseline compiler does not have live registers at slow path */ |
| 54 | 0, /* the baseline compiler does not have live registers at slow path */ |
| 55 | GetGraph()->GetMaximumNumberOfOutVRegs() |
| 56 | + 1 /* current method */, |
| 57 | GetGraph()->GetBlocks()); |
| 58 | CompileInternal(allocator, /* is_baseline */ true); |
| 59 | } |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 60 | |
Nicolas Geoffray | 4c204ba | 2015-02-03 15:12:35 +0000 | [diff] [blame^] | 61 | void CodeGenerator::CompileInternal(CodeAllocator* allocator, bool is_baseline) { |
Nicolas Geoffray | 8a16d97 | 2014-09-11 10:30:02 +0100 | [diff] [blame] | 62 | HGraphVisitor* location_builder = GetLocationBuilder(); |
| 63 | HGraphVisitor* instruction_visitor = GetInstructionVisitor(); |
Nicolas Geoffray | 4c204ba | 2015-02-03 15:12:35 +0000 | [diff] [blame^] | 64 | DCHECK_EQ(current_block_index_, 0u); |
| 65 | GenerateFrameEntry(); |
| 66 | for (size_t e = block_order_->Size(); current_block_index_ < e; ++current_block_index_) { |
| 67 | HBasicBlock* block = block_order_->Get(current_block_index_); |
Nicolas Geoffray | 92a73ae | 2014-10-16 11:12:52 +0100 | [diff] [blame] | 68 | Bind(block); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 69 | for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) { |
| 70 | HInstruction* current = it.Current(); |
Nicolas Geoffray | 4c204ba | 2015-02-03 15:12:35 +0000 | [diff] [blame^] | 71 | if (is_baseline) { |
| 72 | current->Accept(location_builder); |
| 73 | InitLocations(current); |
| 74 | } |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 75 | current->Accept(instruction_visitor); |
| 76 | } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 77 | } |
Nicolas Geoffray | 4c204ba | 2015-02-03 15:12:35 +0000 | [diff] [blame^] | 78 | |
| 79 | // Generate the slow paths. |
| 80 | for (size_t i = 0, e = slow_paths_.Size(); i < e; ++i) { |
| 81 | slow_paths_.Get(i)->EmitNativeCode(this); |
| 82 | } |
| 83 | |
| 84 | // Finalize instructions in assember; |
Serban Constantinescu | 32f5b4d | 2014-11-25 20:05:46 +0000 | [diff] [blame] | 85 | Finalize(allocator); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 88 | void CodeGenerator::CompileOptimized(CodeAllocator* allocator) { |
Nicolas Geoffray | 4c204ba | 2015-02-03 15:12:35 +0000 | [diff] [blame^] | 89 | // The register allocator already called `InitializeCodeGeneration`, |
| 90 | // where the frame size has been computed. |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 91 | DCHECK_NE(frame_size_, kUninitializedFrameSize); |
Nicolas Geoffray | 4c204ba | 2015-02-03 15:12:35 +0000 | [diff] [blame^] | 92 | DCHECK(block_order_ != nullptr); |
Nicolas Geoffray | 92a73ae | 2014-10-16 11:12:52 +0100 | [diff] [blame] | 93 | Initialize(); |
Nicolas Geoffray | 4c204ba | 2015-02-03 15:12:35 +0000 | [diff] [blame^] | 94 | CompileInternal(allocator, /* is_baseline */ false); |
Serban Constantinescu | 32f5b4d | 2014-11-25 20:05:46 +0000 | [diff] [blame] | 95 | } |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 96 | |
Serban Constantinescu | 32f5b4d | 2014-11-25 20:05:46 +0000 | [diff] [blame] | 97 | void CodeGenerator::Finalize(CodeAllocator* allocator) { |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 98 | size_t code_size = GetAssembler()->CodeSize(); |
| 99 | uint8_t* buffer = allocator->Allocate(code_size); |
Serban Constantinescu | 32f5b4d | 2014-11-25 20:05:46 +0000 | [diff] [blame] | 100 | |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 101 | MemoryRegion code(buffer, code_size); |
| 102 | GetAssembler()->FinalizeInstructions(code); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Nicolas Geoffray | 71175b7 | 2014-10-09 22:13:55 +0100 | [diff] [blame] | 105 | size_t CodeGenerator::FindFreeEntry(bool* array, size_t length) { |
| 106 | for (size_t i = 0; i < length; ++i) { |
| 107 | if (!array[i]) { |
| 108 | array[i] = true; |
| 109 | return i; |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 110 | } |
| 111 | } |
Nicolas Geoffray | 71175b7 | 2014-10-09 22:13:55 +0100 | [diff] [blame] | 112 | LOG(FATAL) << "Could not find a register in baseline register allocator"; |
Nicolas Geoffray | 1ba0f59 | 2014-10-27 15:14:55 +0000 | [diff] [blame] | 113 | UNREACHABLE(); |
| 114 | return -1; |
| 115 | } |
| 116 | |
Nicolas Geoffray | 3c03503 | 2014-10-28 10:46:40 +0000 | [diff] [blame] | 117 | size_t CodeGenerator::FindTwoFreeConsecutiveAlignedEntries(bool* array, size_t length) { |
| 118 | for (size_t i = 0; i < length - 1; i += 2) { |
Nicolas Geoffray | 1ba0f59 | 2014-10-27 15:14:55 +0000 | [diff] [blame] | 119 | if (!array[i] && !array[i + 1]) { |
| 120 | array[i] = true; |
| 121 | array[i + 1] = true; |
| 122 | return i; |
| 123 | } |
| 124 | } |
| 125 | LOG(FATAL) << "Could not find a register in baseline register allocator"; |
| 126 | UNREACHABLE(); |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 127 | return -1; |
| 128 | } |
| 129 | |
Nicolas Geoffray | 4c204ba | 2015-02-03 15:12:35 +0000 | [diff] [blame^] | 130 | void CodeGenerator::InitializeCodeGeneration(size_t number_of_spill_slots, |
| 131 | size_t maximum_number_of_live_core_registers, |
| 132 | size_t maximum_number_of_live_fp_registers, |
| 133 | size_t number_of_out_slots, |
| 134 | const GrowableArray<HBasicBlock*>& block_order) { |
| 135 | block_order_ = &block_order; |
| 136 | DCHECK(block_order_->Get(0) == GetGraph()->GetEntryBlock()); |
| 137 | DCHECK(GoesToNextBlock(GetGraph()->GetEntryBlock(), block_order_->Get(1))); |
Nicolas Geoffray | 4dee636 | 2015-01-23 18:23:14 +0000 | [diff] [blame] | 138 | ComputeSpillMask(); |
Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 139 | first_register_slot_in_slow_path_ = (number_of_out_slots + number_of_spill_slots) * kVRegSize; |
| 140 | |
Nicolas Geoffray | ab032bc | 2014-07-15 12:55:21 +0100 | [diff] [blame] | 141 | SetFrameSize(RoundUp( |
| 142 | number_of_spill_slots * kVRegSize |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 143 | + number_of_out_slots * kVRegSize |
Mark Mendell | f85a9ca | 2015-01-13 09:20:58 -0500 | [diff] [blame] | 144 | + maximum_number_of_live_core_registers * GetWordSize() |
| 145 | + maximum_number_of_live_fp_registers * GetFloatingPointSpillSlotSize() |
Nicolas Geoffray | ab032bc | 2014-07-15 12:55:21 +0100 | [diff] [blame] | 146 | + FrameEntrySpillSize(), |
| 147 | kStackAlignment)); |
| 148 | } |
| 149 | |
| 150 | Location CodeGenerator::GetTemporaryLocation(HTemporary* temp) const { |
| 151 | uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs(); |
Calin Juravle | f97f9fb | 2014-11-11 15:38:19 +0000 | [diff] [blame] | 152 | // The type of the previous instruction tells us if we need a single or double stack slot. |
| 153 | Primitive::Type type = temp->GetType(); |
| 154 | int32_t temp_size = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble) ? 2 : 1; |
Nicolas Geoffray | ab032bc | 2014-07-15 12:55:21 +0100 | [diff] [blame] | 155 | // Use the temporary region (right below the dex registers). |
| 156 | int32_t slot = GetFrameSize() - FrameEntrySpillSize() |
| 157 | - kVRegSize // filler |
| 158 | - (number_of_locals * kVRegSize) |
Calin Juravle | f97f9fb | 2014-11-11 15:38:19 +0000 | [diff] [blame] | 159 | - ((temp_size + temp->GetIndex()) * kVRegSize); |
| 160 | return temp_size == 2 ? Location::DoubleStackSlot(slot) : Location::StackSlot(slot); |
Nicolas Geoffray | ab032bc | 2014-07-15 12:55:21 +0100 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | int32_t CodeGenerator::GetStackSlot(HLocal* local) const { |
| 164 | uint16_t reg_number = local->GetRegNumber(); |
| 165 | uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs(); |
| 166 | if (reg_number >= number_of_locals) { |
| 167 | // Local is a parameter of the method. It is stored in the caller's frame. |
| 168 | return GetFrameSize() + kVRegSize // ART method |
| 169 | + (reg_number - number_of_locals) * kVRegSize; |
| 170 | } else { |
| 171 | // Local is a temporary in this method. It is stored in this method's frame. |
| 172 | return GetFrameSize() - FrameEntrySpillSize() |
| 173 | - kVRegSize // filler. |
| 174 | - (number_of_locals * kVRegSize) |
| 175 | + (reg_number * kVRegSize); |
| 176 | } |
| 177 | } |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 178 | |
| 179 | void CodeGenerator::AllocateRegistersLocally(HInstruction* instruction) const { |
| 180 | LocationSummary* locations = instruction->GetLocations(); |
| 181 | if (locations == nullptr) return; |
| 182 | |
Nicolas Geoffray | 71175b7 | 2014-10-09 22:13:55 +0100 | [diff] [blame] | 183 | for (size_t i = 0, e = GetNumberOfCoreRegisters(); i < e; ++i) { |
| 184 | blocked_core_registers_[i] = false; |
| 185 | } |
| 186 | |
| 187 | for (size_t i = 0, e = GetNumberOfFloatingPointRegisters(); i < e; ++i) { |
| 188 | blocked_fpu_registers_[i] = false; |
| 189 | } |
| 190 | |
| 191 | for (size_t i = 0, e = number_of_register_pairs_; i < e; ++i) { |
| 192 | blocked_register_pairs_[i] = false; |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | // Mark all fixed input, temp and output registers as used. |
| 196 | for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) { |
| 197 | Location loc = locations->InAt(i); |
Nicolas Geoffray | 5b4b898 | 2014-12-18 17:45:56 +0000 | [diff] [blame] | 198 | // The DCHECKS below check that a register is not specified twice in |
| 199 | // the summary. |
| 200 | if (loc.IsRegister()) { |
| 201 | DCHECK(!blocked_core_registers_[loc.reg()]); |
| 202 | blocked_core_registers_[loc.reg()] = true; |
| 203 | } else if (loc.IsFpuRegister()) { |
| 204 | DCHECK(!blocked_fpu_registers_[loc.reg()]); |
| 205 | blocked_fpu_registers_[loc.reg()] = true; |
| 206 | } else if (loc.IsFpuRegisterPair()) { |
| 207 | DCHECK(!blocked_fpu_registers_[loc.AsFpuRegisterPairLow<int>()]); |
| 208 | blocked_fpu_registers_[loc.AsFpuRegisterPairLow<int>()] = true; |
| 209 | DCHECK(!blocked_fpu_registers_[loc.AsFpuRegisterPairHigh<int>()]); |
| 210 | blocked_fpu_registers_[loc.AsFpuRegisterPairHigh<int>()] = true; |
| 211 | } else if (loc.IsRegisterPair()) { |
| 212 | DCHECK(!blocked_core_registers_[loc.AsRegisterPairLow<int>()]); |
| 213 | blocked_core_registers_[loc.AsRegisterPairLow<int>()] = true; |
| 214 | DCHECK(!blocked_core_registers_[loc.AsRegisterPairHigh<int>()]); |
| 215 | blocked_core_registers_[loc.AsRegisterPairHigh<int>()] = true; |
| 216 | } |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) { |
| 220 | Location loc = locations->GetTemp(i); |
Nicolas Geoffray | 5b4b898 | 2014-12-18 17:45:56 +0000 | [diff] [blame] | 221 | // The DCHECKS below check that a register is not specified twice in |
| 222 | // the summary. |
| 223 | if (loc.IsRegister()) { |
| 224 | DCHECK(!blocked_core_registers_[loc.reg()]); |
| 225 | blocked_core_registers_[loc.reg()] = true; |
| 226 | } else if (loc.IsFpuRegister()) { |
| 227 | DCHECK(!blocked_fpu_registers_[loc.reg()]); |
| 228 | blocked_fpu_registers_[loc.reg()] = true; |
| 229 | } else { |
| 230 | DCHECK(loc.GetPolicy() == Location::kRequiresRegister |
| 231 | || loc.GetPolicy() == Location::kRequiresFpuRegister); |
| 232 | } |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 233 | } |
| 234 | |
Nicolas Geoffray | 9889396 | 2015-01-21 12:32:32 +0000 | [diff] [blame] | 235 | static constexpr bool kBaseline = true; |
| 236 | SetupBlockedRegisters(kBaseline); |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 237 | |
| 238 | // Allocate all unallocated input locations. |
| 239 | for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) { |
| 240 | Location loc = locations->InAt(i); |
| 241 | HInstruction* input = instruction->InputAt(i); |
| 242 | if (loc.IsUnallocated()) { |
Nicolas Geoffray | 56b9ee6 | 2014-10-09 11:47:51 +0100 | [diff] [blame] | 243 | if ((loc.GetPolicy() == Location::kRequiresRegister) |
| 244 | || (loc.GetPolicy() == Location::kRequiresFpuRegister)) { |
Nicolas Geoffray | 71175b7 | 2014-10-09 22:13:55 +0100 | [diff] [blame] | 245 | loc = AllocateFreeRegister(input->GetType()); |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 246 | } else { |
| 247 | DCHECK_EQ(loc.GetPolicy(), Location::kAny); |
| 248 | HLoadLocal* load = input->AsLoadLocal(); |
| 249 | if (load != nullptr) { |
| 250 | loc = GetStackLocation(load); |
| 251 | } else { |
Nicolas Geoffray | 71175b7 | 2014-10-09 22:13:55 +0100 | [diff] [blame] | 252 | loc = AllocateFreeRegister(input->GetType()); |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 253 | } |
| 254 | } |
| 255 | locations->SetInAt(i, loc); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | // Allocate all unallocated temp locations. |
| 260 | for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) { |
| 261 | Location loc = locations->GetTemp(i); |
| 262 | if (loc.IsUnallocated()) { |
Roland Levillain | 647b9ed | 2014-11-27 12:06:00 +0000 | [diff] [blame] | 263 | switch (loc.GetPolicy()) { |
| 264 | case Location::kRequiresRegister: |
| 265 | // Allocate a core register (large enough to fit a 32-bit integer). |
| 266 | loc = AllocateFreeRegister(Primitive::kPrimInt); |
| 267 | break; |
| 268 | |
| 269 | case Location::kRequiresFpuRegister: |
| 270 | // Allocate a core register (large enough to fit a 64-bit double). |
| 271 | loc = AllocateFreeRegister(Primitive::kPrimDouble); |
| 272 | break; |
| 273 | |
| 274 | default: |
| 275 | LOG(FATAL) << "Unexpected policy for temporary location " |
| 276 | << loc.GetPolicy(); |
| 277 | } |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 278 | locations->SetTempAt(i, loc); |
| 279 | } |
| 280 | } |
Nicolas Geoffray | 5b4b898 | 2014-12-18 17:45:56 +0000 | [diff] [blame] | 281 | Location result_location = locations->Out(); |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 282 | if (result_location.IsUnallocated()) { |
| 283 | switch (result_location.GetPolicy()) { |
| 284 | case Location::kAny: |
| 285 | case Location::kRequiresRegister: |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 286 | case Location::kRequiresFpuRegister: |
Nicolas Geoffray | 71175b7 | 2014-10-09 22:13:55 +0100 | [diff] [blame] | 287 | result_location = AllocateFreeRegister(instruction->GetType()); |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 288 | break; |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 289 | case Location::kSameAsFirstInput: |
| 290 | result_location = locations->InAt(0); |
| 291 | break; |
| 292 | } |
| 293 | locations->SetOut(result_location); |
| 294 | } |
| 295 | } |
| 296 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 297 | void CodeGenerator::InitLocations(HInstruction* instruction) { |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 298 | if (instruction->GetLocations() == nullptr) { |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 299 | if (instruction->IsTemporary()) { |
| 300 | HInstruction* previous = instruction->GetPrevious(); |
| 301 | Location temp_location = GetTemporaryLocation(instruction->AsTemporary()); |
| 302 | Move(previous, temp_location, instruction); |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 303 | } |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 304 | return; |
| 305 | } |
| 306 | AllocateRegistersLocally(instruction); |
| 307 | for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 308 | Location location = instruction->GetLocations()->InAt(i); |
Nicolas Geoffray | f43083d | 2014-11-07 10:48:10 +0000 | [diff] [blame] | 309 | HInstruction* input = instruction->InputAt(i); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 310 | if (location.IsValid()) { |
| 311 | // Move the input to the desired location. |
Nicolas Geoffray | f43083d | 2014-11-07 10:48:10 +0000 | [diff] [blame] | 312 | if (input->GetNext()->IsTemporary()) { |
| 313 | // If the input was stored in a temporary, use that temporary to |
| 314 | // perform the move. |
| 315 | Move(input->GetNext(), location, instruction); |
| 316 | } else { |
| 317 | Move(input, location, instruction); |
| 318 | } |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 319 | } |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | bool CodeGenerator::GoesToNextBlock(HBasicBlock* current, HBasicBlock* next) const { |
Nicolas Geoffray | 4c204ba | 2015-02-03 15:12:35 +0000 | [diff] [blame^] | 324 | DCHECK_EQ(block_order_->Get(current_block_index_), current); |
| 325 | return (current_block_index_ < block_order_->Size() - 1) |
| 326 | && (block_order_->Get(current_block_index_ + 1) == next); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Nicolas Geoffray | 12df9eb | 2015-01-09 14:53:50 +0000 | [diff] [blame] | 329 | CodeGenerator* CodeGenerator::Create(HGraph* graph, |
Calin Juravle | 3416601 | 2014-12-19 17:22:29 +0000 | [diff] [blame] | 330 | InstructionSet instruction_set, |
Calin Juravle | cd6dffe | 2015-01-08 17:35:35 +0000 | [diff] [blame] | 331 | const InstructionSetFeatures& isa_features, |
| 332 | const CompilerOptions& compiler_options) { |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 333 | switch (instruction_set) { |
| 334 | case kArm: |
| 335 | case kThumb2: { |
Nicolas Geoffray | 12df9eb | 2015-01-09 14:53:50 +0000 | [diff] [blame] | 336 | return new arm::CodeGeneratorARM(graph, |
Calin Juravle | cd6dffe | 2015-01-08 17:35:35 +0000 | [diff] [blame] | 337 | *isa_features.AsArmInstructionSetFeatures(), |
| 338 | compiler_options); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 339 | } |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 340 | case kArm64: { |
Calin Juravle | cd6dffe | 2015-01-08 17:35:35 +0000 | [diff] [blame] | 341 | return new arm64::CodeGeneratorARM64(graph, compiler_options); |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 342 | } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 343 | case kMips: |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 344 | return nullptr; |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 345 | case kX86: { |
Calin Juravle | cd6dffe | 2015-01-08 17:35:35 +0000 | [diff] [blame] | 346 | return new x86::CodeGeneratorX86(graph, compiler_options); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 347 | } |
Dmitry Petrochenko | 6a58cb1 | 2014-04-02 17:27:59 +0700 | [diff] [blame] | 348 | case kX86_64: { |
Calin Juravle | cd6dffe | 2015-01-08 17:35:35 +0000 | [diff] [blame] | 349 | return new x86_64::CodeGeneratorX86_64(graph, compiler_options); |
Dmitry Petrochenko | 6a58cb1 | 2014-04-02 17:27:59 +0700 | [diff] [blame] | 350 | } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 351 | default: |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 352 | return nullptr; |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 353 | } |
| 354 | } |
| 355 | |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 356 | void CodeGenerator::BuildNativeGCMap( |
| 357 | std::vector<uint8_t>* data, const DexCompilationUnit& dex_compilation_unit) const { |
| 358 | const std::vector<uint8_t>& gc_map_raw = |
| 359 | dex_compilation_unit.GetVerifiedMethod()->GetDexGcMap(); |
| 360 | verifier::DexPcToReferenceMap dex_gc_map(&(gc_map_raw)[0]); |
| 361 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 362 | uint32_t max_native_offset = 0; |
| 363 | for (size_t i = 0; i < pc_infos_.Size(); i++) { |
| 364 | uint32_t native_offset = pc_infos_.Get(i).native_pc; |
| 365 | if (native_offset > max_native_offset) { |
| 366 | max_native_offset = native_offset; |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | GcMapBuilder builder(data, pc_infos_.Size(), max_native_offset, dex_gc_map.RegWidth()); |
| 371 | for (size_t i = 0; i < pc_infos_.Size(); i++) { |
| 372 | struct PcInfo pc_info = pc_infos_.Get(i); |
| 373 | uint32_t native_offset = pc_info.native_pc; |
| 374 | uint32_t dex_pc = pc_info.dex_pc; |
| 375 | const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false); |
Jean Christophe Beyler | 0ada95d | 2014-12-04 11:20:20 -0800 | [diff] [blame] | 376 | CHECK(references != nullptr) << "Missing ref for dex pc 0x" << std::hex << dex_pc; |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 377 | builder.AddEntry(native_offset, references); |
| 378 | } |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 379 | } |
| 380 | |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 381 | void CodeGenerator::BuildMappingTable(std::vector<uint8_t>* data, DefaultSrcMap* src_map) const { |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 382 | uint32_t pc2dex_data_size = 0u; |
| 383 | uint32_t pc2dex_entries = pc_infos_.Size(); |
| 384 | uint32_t pc2dex_offset = 0u; |
| 385 | int32_t pc2dex_dalvik_offset = 0; |
| 386 | uint32_t dex2pc_data_size = 0u; |
| 387 | uint32_t dex2pc_entries = 0u; |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 388 | uint32_t dex2pc_offset = 0u; |
| 389 | int32_t dex2pc_dalvik_offset = 0; |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 390 | |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 391 | if (src_map != nullptr) { |
| 392 | src_map->reserve(pc2dex_entries); |
| 393 | } |
| 394 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 395 | for (size_t i = 0; i < pc2dex_entries; i++) { |
| 396 | struct PcInfo pc_info = pc_infos_.Get(i); |
| 397 | pc2dex_data_size += UnsignedLeb128Size(pc_info.native_pc - pc2dex_offset); |
| 398 | pc2dex_data_size += SignedLeb128Size(pc_info.dex_pc - pc2dex_dalvik_offset); |
| 399 | pc2dex_offset = pc_info.native_pc; |
| 400 | pc2dex_dalvik_offset = pc_info.dex_pc; |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 401 | if (src_map != nullptr) { |
| 402 | src_map->push_back(SrcMapElem({pc2dex_offset, pc2dex_dalvik_offset})); |
| 403 | } |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 404 | } |
| 405 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 406 | // Walk over the blocks and find which ones correspond to catch block entries. |
| 407 | for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) { |
| 408 | HBasicBlock* block = graph_->GetBlocks().Get(i); |
| 409 | if (block->IsCatchBlock()) { |
| 410 | intptr_t native_pc = GetAddressOf(block); |
| 411 | ++dex2pc_entries; |
| 412 | dex2pc_data_size += UnsignedLeb128Size(native_pc - dex2pc_offset); |
| 413 | dex2pc_data_size += SignedLeb128Size(block->GetDexPc() - dex2pc_dalvik_offset); |
| 414 | dex2pc_offset = native_pc; |
| 415 | dex2pc_dalvik_offset = block->GetDexPc(); |
| 416 | } |
| 417 | } |
| 418 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 419 | uint32_t total_entries = pc2dex_entries + dex2pc_entries; |
| 420 | uint32_t hdr_data_size = UnsignedLeb128Size(total_entries) + UnsignedLeb128Size(pc2dex_entries); |
| 421 | uint32_t data_size = hdr_data_size + pc2dex_data_size + dex2pc_data_size; |
| 422 | data->resize(data_size); |
| 423 | |
| 424 | uint8_t* data_ptr = &(*data)[0]; |
| 425 | uint8_t* write_pos = data_ptr; |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 426 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 427 | write_pos = EncodeUnsignedLeb128(write_pos, total_entries); |
| 428 | write_pos = EncodeUnsignedLeb128(write_pos, pc2dex_entries); |
| 429 | DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size); |
| 430 | uint8_t* write_pos2 = write_pos + pc2dex_data_size; |
| 431 | |
| 432 | pc2dex_offset = 0u; |
| 433 | pc2dex_dalvik_offset = 0u; |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 434 | dex2pc_offset = 0u; |
| 435 | dex2pc_dalvik_offset = 0u; |
| 436 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 437 | for (size_t i = 0; i < pc2dex_entries; i++) { |
| 438 | struct PcInfo pc_info = pc_infos_.Get(i); |
| 439 | DCHECK(pc2dex_offset <= pc_info.native_pc); |
| 440 | write_pos = EncodeUnsignedLeb128(write_pos, pc_info.native_pc - pc2dex_offset); |
| 441 | write_pos = EncodeSignedLeb128(write_pos, pc_info.dex_pc - pc2dex_dalvik_offset); |
| 442 | pc2dex_offset = pc_info.native_pc; |
| 443 | pc2dex_dalvik_offset = pc_info.dex_pc; |
| 444 | } |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 445 | |
| 446 | for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) { |
| 447 | HBasicBlock* block = graph_->GetBlocks().Get(i); |
| 448 | if (block->IsCatchBlock()) { |
| 449 | intptr_t native_pc = GetAddressOf(block); |
| 450 | write_pos2 = EncodeUnsignedLeb128(write_pos2, native_pc - dex2pc_offset); |
| 451 | write_pos2 = EncodeSignedLeb128(write_pos2, block->GetDexPc() - dex2pc_dalvik_offset); |
| 452 | dex2pc_offset = native_pc; |
| 453 | dex2pc_dalvik_offset = block->GetDexPc(); |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 458 | DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size + pc2dex_data_size); |
| 459 | DCHECK_EQ(static_cast<size_t>(write_pos2 - data_ptr), data_size); |
| 460 | |
| 461 | if (kIsDebugBuild) { |
| 462 | // Verify the encoded table holds the expected data. |
| 463 | MappingTable table(data_ptr); |
| 464 | CHECK_EQ(table.TotalSize(), total_entries); |
| 465 | CHECK_EQ(table.PcToDexSize(), pc2dex_entries); |
| 466 | auto it = table.PcToDexBegin(); |
| 467 | auto it2 = table.DexToPcBegin(); |
| 468 | for (size_t i = 0; i < pc2dex_entries; i++) { |
| 469 | struct PcInfo pc_info = pc_infos_.Get(i); |
| 470 | CHECK_EQ(pc_info.native_pc, it.NativePcOffset()); |
| 471 | CHECK_EQ(pc_info.dex_pc, it.DexPc()); |
| 472 | ++it; |
| 473 | } |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 474 | for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) { |
| 475 | HBasicBlock* block = graph_->GetBlocks().Get(i); |
| 476 | if (block->IsCatchBlock()) { |
| 477 | CHECK_EQ(GetAddressOf(block), it2.NativePcOffset()); |
| 478 | CHECK_EQ(block->GetDexPc(), it2.DexPc()); |
| 479 | ++it2; |
| 480 | } |
| 481 | } |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 482 | CHECK(it == table.PcToDexEnd()); |
| 483 | CHECK(it2 == table.DexToPcEnd()); |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | void CodeGenerator::BuildVMapTable(std::vector<uint8_t>* data) const { |
| 488 | Leb128EncodingVector vmap_encoder; |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 489 | // We currently don't use callee-saved registers. |
| 490 | size_t size = 0 + 1 /* marker */ + 0; |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 491 | vmap_encoder.Reserve(size + 1u); // All values are likely to be one byte in ULEB128 (<128). |
| 492 | vmap_encoder.PushBackUnsigned(size); |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 493 | vmap_encoder.PushBackUnsigned(VmapTable::kAdjustedFpMarker); |
| 494 | |
| 495 | *data = vmap_encoder.GetData(); |
| 496 | } |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 497 | |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 498 | void CodeGenerator::BuildStackMaps(std::vector<uint8_t>* data) { |
| 499 | uint32_t size = stack_map_stream_.ComputeNeededSize(); |
| 500 | data->resize(size); |
| 501 | MemoryRegion region(data->data(), size); |
| 502 | stack_map_stream_.FillIn(region); |
| 503 | } |
| 504 | |
| 505 | void CodeGenerator::RecordPcInfo(HInstruction* instruction, uint32_t dex_pc) { |
Calin Juravle | d2ec87d | 2014-12-08 14:24:46 +0000 | [diff] [blame] | 506 | if (instruction != nullptr) { |
Roland Levillain | 624279f | 2014-12-04 11:54:28 +0000 | [diff] [blame] | 507 | // The code generated for some type conversions may call the |
| 508 | // runtime, thus normally requiring a subsequent call to this |
| 509 | // method. However, the method verifier does not produce PC |
Calin Juravle | d2ec87d | 2014-12-08 14:24:46 +0000 | [diff] [blame] | 510 | // information for certain instructions, which are considered "atomic" |
| 511 | // (they cannot join a GC). |
Roland Levillain | 624279f | 2014-12-04 11:54:28 +0000 | [diff] [blame] | 512 | // Therefore we do not currently record PC information for such |
| 513 | // instructions. As this may change later, we added this special |
| 514 | // case so that code generators may nevertheless call |
| 515 | // CodeGenerator::RecordPcInfo without triggering an error in |
| 516 | // CodeGenerator::BuildNativeGCMap ("Missing ref for dex pc 0x") |
| 517 | // thereafter. |
Calin Juravle | d2ec87d | 2014-12-08 14:24:46 +0000 | [diff] [blame] | 518 | if (instruction->IsTypeConversion()) { |
| 519 | return; |
| 520 | } |
| 521 | if (instruction->IsRem()) { |
| 522 | Primitive::Type type = instruction->AsRem()->GetResultType(); |
| 523 | if ((type == Primitive::kPrimFloat) || (type == Primitive::kPrimDouble)) { |
| 524 | return; |
| 525 | } |
| 526 | } |
Roland Levillain | 624279f | 2014-12-04 11:54:28 +0000 | [diff] [blame] | 527 | } |
| 528 | |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 529 | // Collect PC infos for the mapping table. |
| 530 | struct PcInfo pc_info; |
| 531 | pc_info.dex_pc = dex_pc; |
| 532 | pc_info.native_pc = GetAssembler()->CodeSize(); |
| 533 | pc_infos_.Add(pc_info); |
| 534 | |
| 535 | // Populate stack map information. |
| 536 | |
| 537 | if (instruction == nullptr) { |
| 538 | // For stack overflow checks. |
| 539 | stack_map_stream_.AddStackMapEntry(dex_pc, pc_info.native_pc, 0, 0, 0, 0); |
| 540 | return; |
| 541 | } |
| 542 | |
| 543 | LocationSummary* locations = instruction->GetLocations(); |
| 544 | HEnvironment* environment = instruction->GetEnvironment(); |
| 545 | |
| 546 | size_t environment_size = instruction->EnvironmentSize(); |
| 547 | |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 548 | size_t inlining_depth = 0; |
Nicolas Geoffray | 9889396 | 2015-01-21 12:32:32 +0000 | [diff] [blame] | 549 | uint32_t register_mask = locations->GetRegisterMask(); |
| 550 | if (locations->OnlyCallsOnSlowPath()) { |
| 551 | // In case of slow path, we currently set the location of caller-save registers |
| 552 | // to register (instead of their stack location when pushed before the slow-path |
| 553 | // call). Therefore register_mask contains both callee-save and caller-save |
| 554 | // registers that hold objects. We must remove the caller-save from the mask, since |
| 555 | // they will be overwritten by the callee. |
| 556 | register_mask &= core_callee_save_mask_; |
| 557 | } |
| 558 | // The register mask must be a subset of callee-save registers. |
| 559 | DCHECK_EQ(register_mask & core_callee_save_mask_, register_mask); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 560 | stack_map_stream_.AddStackMapEntry( |
| 561 | dex_pc, pc_info.native_pc, register_mask, |
| 562 | locations->GetStackMask(), environment_size, inlining_depth); |
| 563 | |
| 564 | // Walk over the environment, and record the location of dex registers. |
| 565 | for (size_t i = 0; i < environment_size; ++i) { |
| 566 | HInstruction* current = environment->GetInstructionAt(i); |
| 567 | if (current == nullptr) { |
| 568 | stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kNone, 0); |
| 569 | continue; |
| 570 | } |
| 571 | |
| 572 | Location location = locations->GetEnvironmentAt(i); |
| 573 | switch (location.GetKind()) { |
| 574 | case Location::kConstant: { |
| 575 | DCHECK(current == location.GetConstant()); |
| 576 | if (current->IsLongConstant()) { |
| 577 | int64_t value = current->AsLongConstant()->GetValue(); |
| 578 | stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, Low32Bits(value)); |
| 579 | stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, High32Bits(value)); |
| 580 | ++i; |
| 581 | DCHECK_LT(i, environment_size); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 582 | } else if (current->IsDoubleConstant()) { |
| 583 | int64_t value = bit_cast<double, int64_t>(current->AsDoubleConstant()->GetValue()); |
| 584 | stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, Low32Bits(value)); |
| 585 | stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, High32Bits(value)); |
| 586 | ++i; |
| 587 | DCHECK_LT(i, environment_size); |
| 588 | } else if (current->IsIntConstant()) { |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 589 | int32_t value = current->AsIntConstant()->GetValue(); |
| 590 | stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, value); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 591 | } else { |
| 592 | DCHECK(current->IsFloatConstant()); |
| 593 | int32_t value = bit_cast<float, int32_t>(current->AsFloatConstant()->GetValue()); |
| 594 | stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, value); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 595 | } |
| 596 | break; |
| 597 | } |
| 598 | |
| 599 | case Location::kStackSlot: { |
| 600 | stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInStack, location.GetStackIndex()); |
| 601 | break; |
| 602 | } |
| 603 | |
| 604 | case Location::kDoubleStackSlot: { |
| 605 | stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInStack, location.GetStackIndex()); |
| 606 | stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInStack, |
| 607 | location.GetHighStackIndex(kVRegSize)); |
| 608 | ++i; |
| 609 | DCHECK_LT(i, environment_size); |
| 610 | break; |
| 611 | } |
| 612 | |
| 613 | case Location::kRegister : { |
Nicolas Geoffray | 56b9ee6 | 2014-10-09 11:47:51 +0100 | [diff] [blame] | 614 | int id = location.reg(); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 615 | stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInRegister, id); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 616 | if (current->GetType() == Primitive::kPrimLong) { |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 617 | stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInRegister, id); |
| 618 | ++i; |
| 619 | DCHECK_LT(i, environment_size); |
| 620 | } |
| 621 | break; |
| 622 | } |
| 623 | |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 624 | case Location::kFpuRegister : { |
| 625 | int id = location.reg(); |
| 626 | stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInFpuRegister, id); |
| 627 | if (current->GetType() == Primitive::kPrimDouble) { |
| 628 | stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInFpuRegister, id); |
| 629 | ++i; |
| 630 | DCHECK_LT(i, environment_size); |
| 631 | } |
| 632 | break; |
| 633 | } |
| 634 | |
Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 635 | case Location::kFpuRegisterPair : { |
| 636 | stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInFpuRegister, location.low()); |
| 637 | stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInFpuRegister, location.high()); |
| 638 | ++i; |
| 639 | DCHECK_LT(i, environment_size); |
| 640 | break; |
| 641 | } |
| 642 | |
Nicolas Geoffray | 6c2dff8 | 2015-01-21 14:56:54 +0000 | [diff] [blame] | 643 | case Location::kRegisterPair : { |
| 644 | stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInRegister, location.low()); |
| 645 | stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInRegister, location.high()); |
| 646 | ++i; |
| 647 | DCHECK_LT(i, environment_size); |
| 648 | break; |
| 649 | } |
| 650 | |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 651 | default: |
| 652 | LOG(FATAL) << "Unexpected kind " << location.GetKind(); |
| 653 | } |
| 654 | } |
| 655 | } |
| 656 | |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 657 | bool CodeGenerator::CanMoveNullCheckToUser(HNullCheck* null_check) { |
| 658 | HInstruction* first_next_not_move = null_check->GetNextDisregardingMoves(); |
| 659 | return (first_next_not_move != nullptr) && first_next_not_move->CanDoImplicitNullCheck(); |
| 660 | } |
| 661 | |
| 662 | void CodeGenerator::MaybeRecordImplicitNullCheck(HInstruction* instr) { |
| 663 | // If we are from a static path don't record the pc as we can't throw NPE. |
| 664 | // NB: having the checks here makes the code much less verbose in the arch |
| 665 | // specific code generators. |
| 666 | if (instr->IsStaticFieldSet() || instr->IsStaticFieldGet()) { |
| 667 | return; |
| 668 | } |
| 669 | |
| 670 | if (!compiler_options_.GetImplicitNullChecks()) { |
| 671 | return; |
| 672 | } |
| 673 | |
| 674 | if (!instr->CanDoImplicitNullCheck()) { |
| 675 | return; |
| 676 | } |
| 677 | |
| 678 | // Find the first previous instruction which is not a move. |
| 679 | HInstruction* first_prev_not_move = instr->GetPreviousDisregardingMoves(); |
| 680 | |
| 681 | // If the instruction is a null check it means that `instr` is the first user |
| 682 | // and needs to record the pc. |
| 683 | if (first_prev_not_move != nullptr && first_prev_not_move->IsNullCheck()) { |
| 684 | HNullCheck* null_check = first_prev_not_move->AsNullCheck(); |
| 685 | // TODO: The parallel moves modify the environment. Their changes need to be reverted |
| 686 | // otherwise the stack maps at the throw point will not be correct. |
| 687 | RecordPcInfo(null_check, null_check->GetDexPc()); |
| 688 | } |
| 689 | } |
| 690 | |
Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 691 | void CodeGenerator::SaveLiveRegisters(LocationSummary* locations) { |
| 692 | RegisterSet* register_set = locations->GetLiveRegisters(); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 693 | size_t stack_offset = first_register_slot_in_slow_path_; |
Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 694 | for (size_t i = 0, e = GetNumberOfCoreRegisters(); i < e; ++i) { |
Nicolas Geoffray | 9889396 | 2015-01-21 12:32:32 +0000 | [diff] [blame] | 695 | if (!IsCoreCalleeSaveRegister(i)) { |
| 696 | if (register_set->ContainsCoreRegister(i)) { |
| 697 | // If the register holds an object, update the stack mask. |
| 698 | if (locations->RegisterContainsObject(i)) { |
| 699 | locations->SetStackBit(stack_offset / kVRegSize); |
| 700 | } |
| 701 | DCHECK_LT(stack_offset, GetFrameSize() - FrameEntrySpillSize()); |
| 702 | stack_offset += SaveCoreRegister(stack_offset, i); |
Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 703 | } |
| 704 | } |
| 705 | } |
| 706 | |
| 707 | for (size_t i = 0, e = GetNumberOfFloatingPointRegisters(); i < e; ++i) { |
Nicolas Geoffray | 9889396 | 2015-01-21 12:32:32 +0000 | [diff] [blame] | 708 | if (!IsFloatingPointCalleeSaveRegister(i)) { |
| 709 | if (register_set->ContainsFloatingPointRegister(i)) { |
| 710 | DCHECK_LT(stack_offset, GetFrameSize() - FrameEntrySpillSize()); |
| 711 | stack_offset += SaveFloatingPointRegister(stack_offset, i); |
| 712 | } |
Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 713 | } |
| 714 | } |
| 715 | } |
| 716 | |
| 717 | void CodeGenerator::RestoreLiveRegisters(LocationSummary* locations) { |
| 718 | RegisterSet* register_set = locations->GetLiveRegisters(); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 719 | size_t stack_offset = first_register_slot_in_slow_path_; |
Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 720 | for (size_t i = 0, e = GetNumberOfCoreRegisters(); i < e; ++i) { |
Nicolas Geoffray | 9889396 | 2015-01-21 12:32:32 +0000 | [diff] [blame] | 721 | if (!IsCoreCalleeSaveRegister(i)) { |
| 722 | if (register_set->ContainsCoreRegister(i)) { |
| 723 | DCHECK_LT(stack_offset, GetFrameSize() - FrameEntrySpillSize()); |
| 724 | stack_offset += RestoreCoreRegister(stack_offset, i); |
| 725 | } |
Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 726 | } |
| 727 | } |
| 728 | |
| 729 | for (size_t i = 0, e = GetNumberOfFloatingPointRegisters(); i < e; ++i) { |
Nicolas Geoffray | 9889396 | 2015-01-21 12:32:32 +0000 | [diff] [blame] | 730 | if (!IsFloatingPointCalleeSaveRegister(i)) { |
| 731 | if (register_set->ContainsFloatingPointRegister(i)) { |
| 732 | DCHECK_LT(stack_offset, GetFrameSize() - FrameEntrySpillSize()); |
| 733 | stack_offset += RestoreFloatingPointRegister(stack_offset, i); |
| 734 | } |
Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 735 | } |
| 736 | } |
| 737 | } |
| 738 | |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 739 | void CodeGenerator::ClearSpillSlotsFromLoopPhisInStackMap(HSuspendCheck* suspend_check) const { |
| 740 | LocationSummary* locations = suspend_check->GetLocations(); |
| 741 | HBasicBlock* block = suspend_check->GetBlock(); |
| 742 | DCHECK(block->GetLoopInformation()->GetSuspendCheck() == suspend_check); |
| 743 | DCHECK(block->IsLoopHeader()); |
| 744 | |
| 745 | for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) { |
| 746 | HInstruction* current = it.Current(); |
| 747 | LiveInterval* interval = current->GetLiveInterval(); |
| 748 | // We only need to clear bits of loop phis containing objects and allocated in register. |
| 749 | // Loop phis allocated on stack already have the object in the stack. |
| 750 | if (current->GetType() == Primitive::kPrimNot |
| 751 | && interval->HasRegister() |
| 752 | && interval->HasSpillSlot()) { |
| 753 | locations->ClearStackBit(interval->GetSpillSlot() / kVRegSize); |
| 754 | } |
| 755 | } |
| 756 | } |
| 757 | |
Nicolas Geoffray | f0e3937 | 2014-11-12 17:50:07 +0000 | [diff] [blame] | 758 | void CodeGenerator::EmitParallelMoves(Location from1, Location to1, Location from2, Location to2) { |
Nicolas Geoffray | f0e3937 | 2014-11-12 17:50:07 +0000 | [diff] [blame] | 759 | HParallelMove parallel_move(GetGraph()->GetArena()); |
Nicolas Geoffray | 42d1f5f | 2015-01-16 09:14:18 +0000 | [diff] [blame] | 760 | parallel_move.AddMove(from1, to1, nullptr); |
| 761 | parallel_move.AddMove(from2, to2, nullptr); |
Nicolas Geoffray | f0e3937 | 2014-11-12 17:50:07 +0000 | [diff] [blame] | 762 | GetMoveResolver()->EmitNativeCode(¶llel_move); |
| 763 | } |
| 764 | |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 765 | } // namespace art |