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" |
Alexey Frunze | 4dda337 | 2015-06-01 18:31:49 -0700 | [diff] [blame] | 23 | #include "code_generator_mips64.h" |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 24 | #include "compiled_method.h" |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 25 | #include "dex/verified_method.h" |
| 26 | #include "driver/dex_compilation_unit.h" |
| 27 | #include "gc_map_builder.h" |
Alexandre Rames | eb7b739 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 28 | #include "graph_visualizer.h" |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 29 | #include "leb128.h" |
| 30 | #include "mapping_table.h" |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 31 | #include "mirror/array-inl.h" |
| 32 | #include "mirror/object_array-inl.h" |
| 33 | #include "mirror/object_reference.h" |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 34 | #include "ssa_liveness_analysis.h" |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 35 | #include "utils/assembler.h" |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 36 | #include "verifier/dex_gc_map.h" |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 37 | #include "vmap_table.h" |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 38 | |
| 39 | namespace art { |
| 40 | |
Alexandre Rames | 88c13cd | 2015-04-14 17:35:39 +0100 | [diff] [blame] | 41 | // Return whether a location is consistent with a type. |
| 42 | static bool CheckType(Primitive::Type type, Location location) { |
| 43 | if (location.IsFpuRegister() |
| 44 | || (location.IsUnallocated() && (location.GetPolicy() == Location::kRequiresFpuRegister))) { |
| 45 | return (type == Primitive::kPrimFloat) || (type == Primitive::kPrimDouble); |
| 46 | } else if (location.IsRegister() || |
| 47 | (location.IsUnallocated() && (location.GetPolicy() == Location::kRequiresRegister))) { |
| 48 | return Primitive::IsIntegralType(type) || (type == Primitive::kPrimNot); |
| 49 | } else if (location.IsRegisterPair()) { |
| 50 | return type == Primitive::kPrimLong; |
| 51 | } else if (location.IsFpuRegisterPair()) { |
| 52 | return type == Primitive::kPrimDouble; |
| 53 | } else if (location.IsStackSlot()) { |
| 54 | return (Primitive::IsIntegralType(type) && type != Primitive::kPrimLong) |
| 55 | || (type == Primitive::kPrimFloat) |
| 56 | || (type == Primitive::kPrimNot); |
| 57 | } else if (location.IsDoubleStackSlot()) { |
| 58 | return (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble); |
| 59 | } else if (location.IsConstant()) { |
| 60 | if (location.GetConstant()->IsIntConstant()) { |
| 61 | return Primitive::IsIntegralType(type) && (type != Primitive::kPrimLong); |
| 62 | } else if (location.GetConstant()->IsNullConstant()) { |
| 63 | return type == Primitive::kPrimNot; |
| 64 | } else if (location.GetConstant()->IsLongConstant()) { |
| 65 | return type == Primitive::kPrimLong; |
| 66 | } else if (location.GetConstant()->IsFloatConstant()) { |
| 67 | return type == Primitive::kPrimFloat; |
| 68 | } else { |
| 69 | return location.GetConstant()->IsDoubleConstant() |
| 70 | && (type == Primitive::kPrimDouble); |
| 71 | } |
| 72 | } else { |
| 73 | return location.IsInvalid() || (location.GetPolicy() == Location::kAny); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | // Check that a location summary is consistent with an instruction. |
| 78 | static bool CheckTypeConsistency(HInstruction* instruction) { |
| 79 | LocationSummary* locations = instruction->GetLocations(); |
| 80 | if (locations == nullptr) { |
| 81 | return true; |
| 82 | } |
| 83 | |
| 84 | if (locations->Out().IsUnallocated() |
| 85 | && (locations->Out().GetPolicy() == Location::kSameAsFirstInput)) { |
| 86 | DCHECK(CheckType(instruction->GetType(), locations->InAt(0))) |
| 87 | << instruction->GetType() |
| 88 | << " " << locations->InAt(0); |
| 89 | } else { |
| 90 | DCHECK(CheckType(instruction->GetType(), locations->Out())) |
| 91 | << instruction->GetType() |
| 92 | << " " << locations->Out(); |
| 93 | } |
| 94 | |
| 95 | for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) { |
| 96 | DCHECK(CheckType(instruction->InputAt(i)->GetType(), locations->InAt(i))) |
| 97 | << instruction->InputAt(i)->GetType() |
| 98 | << " " << locations->InAt(i); |
| 99 | } |
| 100 | |
| 101 | HEnvironment* environment = instruction->GetEnvironment(); |
| 102 | for (size_t i = 0; i < instruction->EnvironmentSize(); ++i) { |
| 103 | if (environment->GetInstructionAt(i) != nullptr) { |
| 104 | Primitive::Type type = environment->GetInstructionAt(i)->GetType(); |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 105 | DCHECK(CheckType(type, environment->GetLocationAt(i))) |
| 106 | << type << " " << environment->GetLocationAt(i); |
Alexandre Rames | 88c13cd | 2015-04-14 17:35:39 +0100 | [diff] [blame] | 107 | } else { |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 108 | DCHECK(environment->GetLocationAt(i).IsInvalid()) |
| 109 | << environment->GetLocationAt(i); |
Alexandre Rames | 88c13cd | 2015-04-14 17:35:39 +0100 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | return true; |
| 113 | } |
| 114 | |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 115 | size_t CodeGenerator::GetCacheOffset(uint32_t index) { |
| 116 | return mirror::ObjectArray<mirror::Object>::OffsetOfElement(index).SizeValue(); |
| 117 | } |
| 118 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 119 | size_t CodeGenerator::GetCachePointerOffset(uint32_t index) { |
| 120 | auto pointer_size = InstructionSetPointerSize(GetInstructionSet()); |
| 121 | return mirror::Array::DataOffset(pointer_size).Uint32Value() + pointer_size * index; |
| 122 | } |
| 123 | |
Nicolas Geoffray | 73e80c3 | 2014-07-22 17:47:56 +0100 | [diff] [blame] | 124 | void CodeGenerator::CompileBaseline(CodeAllocator* allocator, bool is_leaf) { |
Nicolas Geoffray | 4c204ba | 2015-02-03 15:12:35 +0000 | [diff] [blame] | 125 | Initialize(); |
Nicolas Geoffray | 73e80c3 | 2014-07-22 17:47:56 +0100 | [diff] [blame] | 126 | if (!is_leaf) { |
| 127 | MarkNotLeaf(); |
| 128 | } |
Mathieu Chartier | e3b034a | 2015-05-31 14:29:23 -0700 | [diff] [blame] | 129 | const bool is_64_bit = Is64BitInstructionSet(GetInstructionSet()); |
Nicolas Geoffray | 4c204ba | 2015-02-03 15:12:35 +0000 | [diff] [blame] | 130 | InitializeCodeGeneration(GetGraph()->GetNumberOfLocalVRegs() |
| 131 | + GetGraph()->GetTemporariesVRegSlots() |
| 132 | + 1 /* filler */, |
| 133 | 0, /* the baseline compiler does not have live registers at slow path */ |
| 134 | 0, /* the baseline compiler does not have live registers at slow path */ |
| 135 | GetGraph()->GetMaximumNumberOfOutVRegs() |
Mathieu Chartier | e3b034a | 2015-05-31 14:29:23 -0700 | [diff] [blame] | 136 | + (is_64_bit ? 2 : 1) /* current method */, |
Nicolas Geoffray | 4c204ba | 2015-02-03 15:12:35 +0000 | [diff] [blame] | 137 | GetGraph()->GetBlocks()); |
| 138 | CompileInternal(allocator, /* is_baseline */ true); |
| 139 | } |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 140 | |
Nicolas Geoffray | dc23d83 | 2015-02-16 11:15:43 +0000 | [diff] [blame] | 141 | bool CodeGenerator::GoesToNextBlock(HBasicBlock* current, HBasicBlock* next) const { |
| 142 | DCHECK_EQ(block_order_->Get(current_block_index_), current); |
| 143 | return GetNextBlockToEmit() == FirstNonEmptyBlock(next); |
| 144 | } |
| 145 | |
| 146 | HBasicBlock* CodeGenerator::GetNextBlockToEmit() const { |
| 147 | for (size_t i = current_block_index_ + 1; i < block_order_->Size(); ++i) { |
| 148 | HBasicBlock* block = block_order_->Get(i); |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 149 | if (!block->IsSingleJump()) { |
Nicolas Geoffray | dc23d83 | 2015-02-16 11:15:43 +0000 | [diff] [blame] | 150 | return block; |
| 151 | } |
| 152 | } |
| 153 | return nullptr; |
| 154 | } |
| 155 | |
| 156 | HBasicBlock* CodeGenerator::FirstNonEmptyBlock(HBasicBlock* block) const { |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 157 | while (block->IsSingleJump()) { |
Nicolas Geoffray | dc23d83 | 2015-02-16 11:15:43 +0000 | [diff] [blame] | 158 | block = block->GetSuccessors().Get(0); |
| 159 | } |
| 160 | return block; |
| 161 | } |
| 162 | |
Alexandre Rames | eb7b739 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 163 | class DisassemblyScope { |
| 164 | public: |
| 165 | DisassemblyScope(HInstruction* instruction, const CodeGenerator& codegen) |
| 166 | : codegen_(codegen), instruction_(instruction), start_offset_(static_cast<size_t>(-1)) { |
| 167 | if (codegen_.GetDisassemblyInformation() != nullptr) { |
| 168 | start_offset_ = codegen_.GetAssembler().CodeSize(); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | ~DisassemblyScope() { |
| 173 | // We avoid building this data when we know it will not be used. |
| 174 | if (codegen_.GetDisassemblyInformation() != nullptr) { |
| 175 | codegen_.GetDisassemblyInformation()->AddInstructionInterval( |
| 176 | instruction_, start_offset_, codegen_.GetAssembler().CodeSize()); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | private: |
| 181 | const CodeGenerator& codegen_; |
| 182 | HInstruction* instruction_; |
| 183 | size_t start_offset_; |
| 184 | }; |
| 185 | |
| 186 | |
| 187 | void CodeGenerator::GenerateSlowPaths() { |
| 188 | size_t code_start = 0; |
| 189 | for (size_t i = 0, e = slow_paths_.Size(); i < e; ++i) { |
| 190 | if (disasm_info_ != nullptr) { |
| 191 | code_start = GetAssembler()->CodeSize(); |
| 192 | } |
| 193 | slow_paths_.Get(i)->EmitNativeCode(this); |
| 194 | if (disasm_info_ != nullptr) { |
| 195 | disasm_info_->AddSlowPathInterval(slow_paths_.Get(i), code_start, GetAssembler()->CodeSize()); |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
Nicolas Geoffray | 4c204ba | 2015-02-03 15:12:35 +0000 | [diff] [blame] | 200 | void CodeGenerator::CompileInternal(CodeAllocator* allocator, bool is_baseline) { |
Roland Levillain | 3e3d733 | 2015-04-28 11:00:54 +0100 | [diff] [blame] | 201 | is_baseline_ = is_baseline; |
Nicolas Geoffray | 8a16d97 | 2014-09-11 10:30:02 +0100 | [diff] [blame] | 202 | HGraphVisitor* instruction_visitor = GetInstructionVisitor(); |
Nicolas Geoffray | 4c204ba | 2015-02-03 15:12:35 +0000 | [diff] [blame] | 203 | DCHECK_EQ(current_block_index_, 0u); |
Alexandre Rames | eb7b739 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 204 | |
| 205 | size_t frame_start = GetAssembler()->CodeSize(); |
Nicolas Geoffray | 4c204ba | 2015-02-03 15:12:35 +0000 | [diff] [blame] | 206 | GenerateFrameEntry(); |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 207 | DCHECK_EQ(GetAssembler()->cfi().GetCurrentCFAOffset(), static_cast<int>(frame_size_)); |
Alexandre Rames | eb7b739 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 208 | if (disasm_info_ != nullptr) { |
| 209 | disasm_info_->SetFrameEntryInterval(frame_start, GetAssembler()->CodeSize()); |
| 210 | } |
| 211 | |
Nicolas Geoffray | 4c204ba | 2015-02-03 15:12:35 +0000 | [diff] [blame] | 212 | for (size_t e = block_order_->Size(); current_block_index_ < e; ++current_block_index_) { |
| 213 | HBasicBlock* block = block_order_->Get(current_block_index_); |
Nicolas Geoffray | dc23d83 | 2015-02-16 11:15:43 +0000 | [diff] [blame] | 214 | // Don't generate code for an empty block. Its predecessors will branch to its successor |
| 215 | // directly. Also, the label of that block will not be emitted, so this helps catch |
| 216 | // errors where we reference that label. |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 217 | if (block->IsSingleJump()) continue; |
Nicolas Geoffray | 92a73ae | 2014-10-16 11:12:52 +0100 | [diff] [blame] | 218 | Bind(block); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 219 | for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) { |
| 220 | HInstruction* current = it.Current(); |
Alexandre Rames | eb7b739 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 221 | DisassemblyScope disassembly_scope(current, *this); |
Nicolas Geoffray | 4c204ba | 2015-02-03 15:12:35 +0000 | [diff] [blame] | 222 | if (is_baseline) { |
Nicolas Geoffray | c0572a4 | 2015-02-06 14:35:25 +0000 | [diff] [blame] | 223 | InitLocationsBaseline(current); |
Nicolas Geoffray | 4c204ba | 2015-02-03 15:12:35 +0000 | [diff] [blame] | 224 | } |
Alexandre Rames | 88c13cd | 2015-04-14 17:35:39 +0100 | [diff] [blame] | 225 | DCHECK(CheckTypeConsistency(current)); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 226 | current->Accept(instruction_visitor); |
| 227 | } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 228 | } |
Nicolas Geoffray | 4c204ba | 2015-02-03 15:12:35 +0000 | [diff] [blame] | 229 | |
Alexandre Rames | eb7b739 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 230 | GenerateSlowPaths(); |
Nicolas Geoffray | 4c204ba | 2015-02-03 15:12:35 +0000 | [diff] [blame] | 231 | |
| 232 | // Finalize instructions in assember; |
Serban Constantinescu | 32f5b4d | 2014-11-25 20:05:46 +0000 | [diff] [blame] | 233 | Finalize(allocator); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 236 | void CodeGenerator::CompileOptimized(CodeAllocator* allocator) { |
Nicolas Geoffray | 4c204ba | 2015-02-03 15:12:35 +0000 | [diff] [blame] | 237 | // The register allocator already called `InitializeCodeGeneration`, |
| 238 | // where the frame size has been computed. |
Nicolas Geoffray | 4c204ba | 2015-02-03 15:12:35 +0000 | [diff] [blame] | 239 | DCHECK(block_order_ != nullptr); |
Nicolas Geoffray | 92a73ae | 2014-10-16 11:12:52 +0100 | [diff] [blame] | 240 | Initialize(); |
Nicolas Geoffray | 4c204ba | 2015-02-03 15:12:35 +0000 | [diff] [blame] | 241 | CompileInternal(allocator, /* is_baseline */ false); |
Serban Constantinescu | 32f5b4d | 2014-11-25 20:05:46 +0000 | [diff] [blame] | 242 | } |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 243 | |
Serban Constantinescu | 32f5b4d | 2014-11-25 20:05:46 +0000 | [diff] [blame] | 244 | void CodeGenerator::Finalize(CodeAllocator* allocator) { |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 245 | size_t code_size = GetAssembler()->CodeSize(); |
| 246 | uint8_t* buffer = allocator->Allocate(code_size); |
Serban Constantinescu | 32f5b4d | 2014-11-25 20:05:46 +0000 | [diff] [blame] | 247 | |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 248 | MemoryRegion code(buffer, code_size); |
| 249 | GetAssembler()->FinalizeInstructions(code); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Nicolas Geoffray | 71175b7 | 2014-10-09 22:13:55 +0100 | [diff] [blame] | 252 | size_t CodeGenerator::FindFreeEntry(bool* array, size_t length) { |
| 253 | for (size_t i = 0; i < length; ++i) { |
| 254 | if (!array[i]) { |
| 255 | array[i] = true; |
| 256 | return i; |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 257 | } |
| 258 | } |
Nicolas Geoffray | 71175b7 | 2014-10-09 22:13:55 +0100 | [diff] [blame] | 259 | LOG(FATAL) << "Could not find a register in baseline register allocator"; |
Nicolas Geoffray | 1ba0f59 | 2014-10-27 15:14:55 +0000 | [diff] [blame] | 260 | UNREACHABLE(); |
Nicolas Geoffray | 1ba0f59 | 2014-10-27 15:14:55 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Nicolas Geoffray | 3c03503 | 2014-10-28 10:46:40 +0000 | [diff] [blame] | 263 | size_t CodeGenerator::FindTwoFreeConsecutiveAlignedEntries(bool* array, size_t length) { |
| 264 | for (size_t i = 0; i < length - 1; i += 2) { |
Nicolas Geoffray | 1ba0f59 | 2014-10-27 15:14:55 +0000 | [diff] [blame] | 265 | if (!array[i] && !array[i + 1]) { |
| 266 | array[i] = true; |
| 267 | array[i + 1] = true; |
| 268 | return i; |
| 269 | } |
| 270 | } |
| 271 | LOG(FATAL) << "Could not find a register in baseline register allocator"; |
| 272 | UNREACHABLE(); |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 273 | } |
| 274 | |
Nicolas Geoffray | 4c204ba | 2015-02-03 15:12:35 +0000 | [diff] [blame] | 275 | void CodeGenerator::InitializeCodeGeneration(size_t number_of_spill_slots, |
| 276 | size_t maximum_number_of_live_core_registers, |
| 277 | size_t maximum_number_of_live_fp_registers, |
| 278 | size_t number_of_out_slots, |
| 279 | const GrowableArray<HBasicBlock*>& block_order) { |
| 280 | block_order_ = &block_order; |
| 281 | DCHECK(block_order_->Get(0) == GetGraph()->GetEntryBlock()); |
Nicolas Geoffray | 4dee636 | 2015-01-23 18:23:14 +0000 | [diff] [blame] | 282 | ComputeSpillMask(); |
Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 283 | first_register_slot_in_slow_path_ = (number_of_out_slots + number_of_spill_slots) * kVRegSize; |
| 284 | |
Nicolas Geoffray | c0572a4 | 2015-02-06 14:35:25 +0000 | [diff] [blame] | 285 | if (number_of_spill_slots == 0 |
| 286 | && !HasAllocatedCalleeSaveRegisters() |
| 287 | && IsLeafMethod() |
| 288 | && !RequiresCurrentMethod()) { |
| 289 | DCHECK_EQ(maximum_number_of_live_core_registers, 0u); |
| 290 | DCHECK_EQ(maximum_number_of_live_fp_registers, 0u); |
| 291 | SetFrameSize(CallPushesPC() ? GetWordSize() : 0); |
| 292 | } else { |
| 293 | SetFrameSize(RoundUp( |
| 294 | number_of_spill_slots * kVRegSize |
| 295 | + number_of_out_slots * kVRegSize |
| 296 | + maximum_number_of_live_core_registers * GetWordSize() |
| 297 | + maximum_number_of_live_fp_registers * GetFloatingPointSpillSlotSize() |
| 298 | + FrameEntrySpillSize(), |
| 299 | kStackAlignment)); |
| 300 | } |
Nicolas Geoffray | ab032bc | 2014-07-15 12:55:21 +0100 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | Location CodeGenerator::GetTemporaryLocation(HTemporary* temp) const { |
| 304 | uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs(); |
Calin Juravle | f97f9fb | 2014-11-11 15:38:19 +0000 | [diff] [blame] | 305 | // The type of the previous instruction tells us if we need a single or double stack slot. |
| 306 | Primitive::Type type = temp->GetType(); |
| 307 | int32_t temp_size = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble) ? 2 : 1; |
Nicolas Geoffray | ab032bc | 2014-07-15 12:55:21 +0100 | [diff] [blame] | 308 | // Use the temporary region (right below the dex registers). |
| 309 | int32_t slot = GetFrameSize() - FrameEntrySpillSize() |
| 310 | - kVRegSize // filler |
| 311 | - (number_of_locals * kVRegSize) |
Calin Juravle | f97f9fb | 2014-11-11 15:38:19 +0000 | [diff] [blame] | 312 | - ((temp_size + temp->GetIndex()) * kVRegSize); |
| 313 | return temp_size == 2 ? Location::DoubleStackSlot(slot) : Location::StackSlot(slot); |
Nicolas Geoffray | ab032bc | 2014-07-15 12:55:21 +0100 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | int32_t CodeGenerator::GetStackSlot(HLocal* local) const { |
| 317 | uint16_t reg_number = local->GetRegNumber(); |
| 318 | uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs(); |
| 319 | if (reg_number >= number_of_locals) { |
| 320 | // Local is a parameter of the method. It is stored in the caller's frame. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 321 | // TODO: Share this logic with StackVisitor::GetVRegOffsetFromQuickCode. |
| 322 | return GetFrameSize() + InstructionSetPointerSize(GetInstructionSet()) // ART method |
Nicolas Geoffray | ab032bc | 2014-07-15 12:55:21 +0100 | [diff] [blame] | 323 | + (reg_number - number_of_locals) * kVRegSize; |
| 324 | } else { |
| 325 | // Local is a temporary in this method. It is stored in this method's frame. |
| 326 | return GetFrameSize() - FrameEntrySpillSize() |
| 327 | - kVRegSize // filler. |
| 328 | - (number_of_locals * kVRegSize) |
| 329 | + (reg_number * kVRegSize); |
| 330 | } |
| 331 | } |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 332 | |
Nicolas Geoffray | fd88f16 | 2015-06-03 11:23:52 +0100 | [diff] [blame] | 333 | void CodeGenerator::CreateCommonInvokeLocationSummary( |
Nicolas Geoffray | 4e40c26 | 2015-06-03 12:02:38 +0100 | [diff] [blame] | 334 | HInvoke* invoke, InvokeDexCallingConventionVisitor* visitor) { |
Nicolas Geoffray | fd88f16 | 2015-06-03 11:23:52 +0100 | [diff] [blame] | 335 | ArenaAllocator* allocator = invoke->GetBlock()->GetGraph()->GetArena(); |
| 336 | LocationSummary* locations = new (allocator) LocationSummary(invoke, LocationSummary::kCall); |
Nicolas Geoffray | fd88f16 | 2015-06-03 11:23:52 +0100 | [diff] [blame] | 337 | |
| 338 | for (size_t i = 0; i < invoke->GetNumberOfArguments(); i++) { |
| 339 | HInstruction* input = invoke->InputAt(i); |
| 340 | locations->SetInAt(i, visitor->GetNextLocation(input->GetType())); |
| 341 | } |
| 342 | |
| 343 | locations->SetOut(visitor->GetReturnLocation(invoke->GetType())); |
Nicolas Geoffray | 94015b9 | 2015-06-04 18:21:04 +0100 | [diff] [blame] | 344 | |
| 345 | if (invoke->IsInvokeStaticOrDirect()) { |
| 346 | HInvokeStaticOrDirect* call = invoke->AsInvokeStaticOrDirect(); |
| 347 | if (call->IsStringInit()) { |
| 348 | locations->AddTemp(visitor->GetMethodLocation()); |
| 349 | } else if (call->IsRecursive()) { |
| 350 | locations->SetInAt(call->GetCurrentMethodInputIndex(), visitor->GetMethodLocation()); |
| 351 | } else { |
| 352 | locations->AddTemp(visitor->GetMethodLocation()); |
| 353 | locations->SetInAt(call->GetCurrentMethodInputIndex(), Location::RequiresRegister()); |
| 354 | } |
| 355 | } else { |
| 356 | locations->AddTemp(visitor->GetMethodLocation()); |
| 357 | } |
Nicolas Geoffray | fd88f16 | 2015-06-03 11:23:52 +0100 | [diff] [blame] | 358 | } |
| 359 | |
Mark Mendell | 5f87418 | 2015-03-04 15:42:45 -0500 | [diff] [blame] | 360 | void CodeGenerator::BlockIfInRegister(Location location, bool is_out) const { |
| 361 | // The DCHECKS below check that a register is not specified twice in |
| 362 | // the summary. The out location can overlap with an input, so we need |
| 363 | // to special case it. |
| 364 | if (location.IsRegister()) { |
| 365 | DCHECK(is_out || !blocked_core_registers_[location.reg()]); |
| 366 | blocked_core_registers_[location.reg()] = true; |
| 367 | } else if (location.IsFpuRegister()) { |
| 368 | DCHECK(is_out || !blocked_fpu_registers_[location.reg()]); |
| 369 | blocked_fpu_registers_[location.reg()] = true; |
| 370 | } else if (location.IsFpuRegisterPair()) { |
| 371 | DCHECK(is_out || !blocked_fpu_registers_[location.AsFpuRegisterPairLow<int>()]); |
| 372 | blocked_fpu_registers_[location.AsFpuRegisterPairLow<int>()] = true; |
| 373 | DCHECK(is_out || !blocked_fpu_registers_[location.AsFpuRegisterPairHigh<int>()]); |
| 374 | blocked_fpu_registers_[location.AsFpuRegisterPairHigh<int>()] = true; |
| 375 | } else if (location.IsRegisterPair()) { |
| 376 | DCHECK(is_out || !blocked_core_registers_[location.AsRegisterPairLow<int>()]); |
| 377 | blocked_core_registers_[location.AsRegisterPairLow<int>()] = true; |
| 378 | DCHECK(is_out || !blocked_core_registers_[location.AsRegisterPairHigh<int>()]); |
| 379 | blocked_core_registers_[location.AsRegisterPairHigh<int>()] = true; |
| 380 | } |
| 381 | } |
| 382 | |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 383 | void CodeGenerator::AllocateRegistersLocally(HInstruction* instruction) const { |
| 384 | LocationSummary* locations = instruction->GetLocations(); |
| 385 | if (locations == nullptr) return; |
| 386 | |
Nicolas Geoffray | 71175b7 | 2014-10-09 22:13:55 +0100 | [diff] [blame] | 387 | for (size_t i = 0, e = GetNumberOfCoreRegisters(); i < e; ++i) { |
| 388 | blocked_core_registers_[i] = false; |
| 389 | } |
| 390 | |
| 391 | for (size_t i = 0, e = GetNumberOfFloatingPointRegisters(); i < e; ++i) { |
| 392 | blocked_fpu_registers_[i] = false; |
| 393 | } |
| 394 | |
| 395 | for (size_t i = 0, e = number_of_register_pairs_; i < e; ++i) { |
| 396 | blocked_register_pairs_[i] = false; |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | // Mark all fixed input, temp and output registers as used. |
| 400 | for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) { |
Mark Mendell | 5f87418 | 2015-03-04 15:42:45 -0500 | [diff] [blame] | 401 | BlockIfInRegister(locations->InAt(i)); |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) { |
| 405 | Location loc = locations->GetTemp(i); |
Mark Mendell | 5f87418 | 2015-03-04 15:42:45 -0500 | [diff] [blame] | 406 | BlockIfInRegister(loc); |
| 407 | } |
| 408 | Location result_location = locations->Out(); |
| 409 | if (locations->OutputCanOverlapWithInputs()) { |
| 410 | BlockIfInRegister(result_location, /* is_out */ true); |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 411 | } |
| 412 | |
Mark Mendell | 5f87418 | 2015-03-04 15:42:45 -0500 | [diff] [blame] | 413 | SetupBlockedRegisters(/* is_baseline */ true); |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 414 | |
| 415 | // Allocate all unallocated input locations. |
| 416 | for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) { |
| 417 | Location loc = locations->InAt(i); |
| 418 | HInstruction* input = instruction->InputAt(i); |
| 419 | if (loc.IsUnallocated()) { |
Nicolas Geoffray | 56b9ee6 | 2014-10-09 11:47:51 +0100 | [diff] [blame] | 420 | if ((loc.GetPolicy() == Location::kRequiresRegister) |
| 421 | || (loc.GetPolicy() == Location::kRequiresFpuRegister)) { |
Nicolas Geoffray | 71175b7 | 2014-10-09 22:13:55 +0100 | [diff] [blame] | 422 | loc = AllocateFreeRegister(input->GetType()); |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 423 | } else { |
| 424 | DCHECK_EQ(loc.GetPolicy(), Location::kAny); |
| 425 | HLoadLocal* load = input->AsLoadLocal(); |
| 426 | if (load != nullptr) { |
| 427 | loc = GetStackLocation(load); |
| 428 | } else { |
Nicolas Geoffray | 71175b7 | 2014-10-09 22:13:55 +0100 | [diff] [blame] | 429 | loc = AllocateFreeRegister(input->GetType()); |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 430 | } |
| 431 | } |
| 432 | locations->SetInAt(i, loc); |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | // Allocate all unallocated temp locations. |
| 437 | for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) { |
| 438 | Location loc = locations->GetTemp(i); |
| 439 | if (loc.IsUnallocated()) { |
Roland Levillain | 647b9ed | 2014-11-27 12:06:00 +0000 | [diff] [blame] | 440 | switch (loc.GetPolicy()) { |
| 441 | case Location::kRequiresRegister: |
| 442 | // Allocate a core register (large enough to fit a 32-bit integer). |
| 443 | loc = AllocateFreeRegister(Primitive::kPrimInt); |
| 444 | break; |
| 445 | |
| 446 | case Location::kRequiresFpuRegister: |
| 447 | // Allocate a core register (large enough to fit a 64-bit double). |
| 448 | loc = AllocateFreeRegister(Primitive::kPrimDouble); |
| 449 | break; |
| 450 | |
| 451 | default: |
| 452 | LOG(FATAL) << "Unexpected policy for temporary location " |
| 453 | << loc.GetPolicy(); |
| 454 | } |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 455 | locations->SetTempAt(i, loc); |
| 456 | } |
| 457 | } |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 458 | if (result_location.IsUnallocated()) { |
| 459 | switch (result_location.GetPolicy()) { |
| 460 | case Location::kAny: |
| 461 | case Location::kRequiresRegister: |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 462 | case Location::kRequiresFpuRegister: |
Nicolas Geoffray | 71175b7 | 2014-10-09 22:13:55 +0100 | [diff] [blame] | 463 | result_location = AllocateFreeRegister(instruction->GetType()); |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 464 | break; |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 465 | case Location::kSameAsFirstInput: |
| 466 | result_location = locations->InAt(0); |
| 467 | break; |
| 468 | } |
Nicolas Geoffray | 829280c | 2015-01-28 10:20:37 +0000 | [diff] [blame] | 469 | locations->UpdateOut(result_location); |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 470 | } |
| 471 | } |
| 472 | |
Nicolas Geoffray | c0572a4 | 2015-02-06 14:35:25 +0000 | [diff] [blame] | 473 | void CodeGenerator::InitLocationsBaseline(HInstruction* instruction) { |
| 474 | AllocateLocations(instruction); |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 475 | if (instruction->GetLocations() == nullptr) { |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 476 | if (instruction->IsTemporary()) { |
| 477 | HInstruction* previous = instruction->GetPrevious(); |
| 478 | Location temp_location = GetTemporaryLocation(instruction->AsTemporary()); |
| 479 | Move(previous, temp_location, instruction); |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 480 | } |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 481 | return; |
| 482 | } |
| 483 | AllocateRegistersLocally(instruction); |
| 484 | for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 485 | Location location = instruction->GetLocations()->InAt(i); |
Nicolas Geoffray | f43083d | 2014-11-07 10:48:10 +0000 | [diff] [blame] | 486 | HInstruction* input = instruction->InputAt(i); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 487 | if (location.IsValid()) { |
| 488 | // Move the input to the desired location. |
Nicolas Geoffray | f43083d | 2014-11-07 10:48:10 +0000 | [diff] [blame] | 489 | if (input->GetNext()->IsTemporary()) { |
| 490 | // If the input was stored in a temporary, use that temporary to |
| 491 | // perform the move. |
| 492 | Move(input->GetNext(), location, instruction); |
| 493 | } else { |
| 494 | Move(input, location, instruction); |
| 495 | } |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 496 | } |
| 497 | } |
| 498 | } |
| 499 | |
Nicolas Geoffray | c0572a4 | 2015-02-06 14:35:25 +0000 | [diff] [blame] | 500 | void CodeGenerator::AllocateLocations(HInstruction* instruction) { |
| 501 | instruction->Accept(GetLocationBuilder()); |
Alexandre Rames | 88c13cd | 2015-04-14 17:35:39 +0100 | [diff] [blame] | 502 | DCHECK(CheckTypeConsistency(instruction)); |
Nicolas Geoffray | c0572a4 | 2015-02-06 14:35:25 +0000 | [diff] [blame] | 503 | LocationSummary* locations = instruction->GetLocations(); |
| 504 | if (!instruction->IsSuspendCheckEntry()) { |
| 505 | if (locations != nullptr && locations->CanCall()) { |
| 506 | MarkNotLeaf(); |
| 507 | } |
| 508 | if (instruction->NeedsCurrentMethod()) { |
| 509 | SetRequiresCurrentMethod(); |
| 510 | } |
| 511 | } |
| 512 | } |
| 513 | |
Nicolas Geoffray | 12df9eb | 2015-01-09 14:53:50 +0000 | [diff] [blame] | 514 | CodeGenerator* CodeGenerator::Create(HGraph* graph, |
Calin Juravle | 3416601 | 2014-12-19 17:22:29 +0000 | [diff] [blame] | 515 | InstructionSet instruction_set, |
Calin Juravle | cd6dffe | 2015-01-08 17:35:35 +0000 | [diff] [blame] | 516 | const InstructionSetFeatures& isa_features, |
| 517 | const CompilerOptions& compiler_options) { |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 518 | switch (instruction_set) { |
| 519 | case kArm: |
| 520 | case kThumb2: { |
Nicolas Geoffray | 12df9eb | 2015-01-09 14:53:50 +0000 | [diff] [blame] | 521 | return new arm::CodeGeneratorARM(graph, |
Calin Juravle | cd6dffe | 2015-01-08 17:35:35 +0000 | [diff] [blame] | 522 | *isa_features.AsArmInstructionSetFeatures(), |
| 523 | compiler_options); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 524 | } |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 525 | case kArm64: { |
Serban Constantinescu | 579885a | 2015-02-22 20:51:33 +0000 | [diff] [blame] | 526 | return new arm64::CodeGeneratorARM64(graph, |
| 527 | *isa_features.AsArm64InstructionSetFeatures(), |
| 528 | compiler_options); |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 529 | } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 530 | case kMips: |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 531 | return nullptr; |
Alexey Frunze | 4dda337 | 2015-06-01 18:31:49 -0700 | [diff] [blame] | 532 | case kMips64: { |
| 533 | return new mips64::CodeGeneratorMIPS64(graph, |
| 534 | *isa_features.AsMips64InstructionSetFeatures(), |
| 535 | compiler_options); |
| 536 | } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 537 | case kX86: { |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 538 | return new x86::CodeGeneratorX86(graph, |
| 539 | *isa_features.AsX86InstructionSetFeatures(), |
| 540 | compiler_options); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 541 | } |
Dmitry Petrochenko | 6a58cb1 | 2014-04-02 17:27:59 +0700 | [diff] [blame] | 542 | case kX86_64: { |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 543 | return new x86_64::CodeGeneratorX86_64(graph, |
| 544 | *isa_features.AsX86_64InstructionSetFeatures(), |
| 545 | compiler_options); |
Dmitry Petrochenko | 6a58cb1 | 2014-04-02 17:27:59 +0700 | [diff] [blame] | 546 | } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 547 | default: |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 548 | return nullptr; |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 549 | } |
| 550 | } |
| 551 | |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 552 | void CodeGenerator::BuildNativeGCMap( |
| 553 | std::vector<uint8_t>* data, const DexCompilationUnit& dex_compilation_unit) const { |
| 554 | const std::vector<uint8_t>& gc_map_raw = |
| 555 | dex_compilation_unit.GetVerifiedMethod()->GetDexGcMap(); |
| 556 | verifier::DexPcToReferenceMap dex_gc_map(&(gc_map_raw)[0]); |
| 557 | |
Vladimir Marko | bd8c725 | 2015-06-12 10:06:32 +0100 | [diff] [blame] | 558 | uint32_t max_native_offset = stack_map_stream_.ComputeMaxNativePcOffset(); |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 559 | |
Vladimir Marko | bd8c725 | 2015-06-12 10:06:32 +0100 | [diff] [blame] | 560 | size_t num_stack_maps = stack_map_stream_.GetNumberOfStackMaps(); |
| 561 | GcMapBuilder builder(data, num_stack_maps, max_native_offset, dex_gc_map.RegWidth()); |
| 562 | for (size_t i = 0; i != num_stack_maps; ++i) { |
| 563 | const StackMapStream::StackMapEntry& stack_map_entry = stack_map_stream_.GetStackMap(i); |
| 564 | uint32_t native_offset = stack_map_entry.native_pc_offset; |
| 565 | uint32_t dex_pc = stack_map_entry.dex_pc; |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 566 | const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false); |
Jean Christophe Beyler | 0ada95d | 2014-12-04 11:20:20 -0800 | [diff] [blame] | 567 | 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] | 568 | builder.AddEntry(native_offset, references); |
| 569 | } |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 570 | } |
| 571 | |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 572 | void CodeGenerator::BuildSourceMap(DefaultSrcMap* src_map) const { |
Vladimir Marko | bd8c725 | 2015-06-12 10:06:32 +0100 | [diff] [blame] | 573 | for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) { |
| 574 | const StackMapStream::StackMapEntry& stack_map_entry = stack_map_stream_.GetStackMap(i); |
| 575 | uint32_t pc2dex_offset = stack_map_entry.native_pc_offset; |
| 576 | int32_t pc2dex_dalvik_offset = stack_map_entry.dex_pc; |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 577 | src_map->push_back(SrcMapElem({pc2dex_offset, pc2dex_dalvik_offset})); |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | void CodeGenerator::BuildMappingTable(std::vector<uint8_t>* data) const { |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 582 | uint32_t pc2dex_data_size = 0u; |
Vladimir Marko | bd8c725 | 2015-06-12 10:06:32 +0100 | [diff] [blame] | 583 | uint32_t pc2dex_entries = stack_map_stream_.GetNumberOfStackMaps(); |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 584 | uint32_t pc2dex_offset = 0u; |
| 585 | int32_t pc2dex_dalvik_offset = 0; |
| 586 | uint32_t dex2pc_data_size = 0u; |
| 587 | uint32_t dex2pc_entries = 0u; |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 588 | uint32_t dex2pc_offset = 0u; |
| 589 | int32_t dex2pc_dalvik_offset = 0; |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 590 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 591 | for (size_t i = 0; i < pc2dex_entries; i++) { |
Vladimir Marko | bd8c725 | 2015-06-12 10:06:32 +0100 | [diff] [blame] | 592 | const StackMapStream::StackMapEntry& stack_map_entry = stack_map_stream_.GetStackMap(i); |
| 593 | pc2dex_data_size += UnsignedLeb128Size(stack_map_entry.native_pc_offset - pc2dex_offset); |
| 594 | pc2dex_data_size += SignedLeb128Size(stack_map_entry.dex_pc - pc2dex_dalvik_offset); |
| 595 | pc2dex_offset = stack_map_entry.native_pc_offset; |
| 596 | pc2dex_dalvik_offset = stack_map_entry.dex_pc; |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 597 | } |
| 598 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 599 | // Walk over the blocks and find which ones correspond to catch block entries. |
| 600 | for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) { |
| 601 | HBasicBlock* block = graph_->GetBlocks().Get(i); |
| 602 | if (block->IsCatchBlock()) { |
| 603 | intptr_t native_pc = GetAddressOf(block); |
| 604 | ++dex2pc_entries; |
| 605 | dex2pc_data_size += UnsignedLeb128Size(native_pc - dex2pc_offset); |
| 606 | dex2pc_data_size += SignedLeb128Size(block->GetDexPc() - dex2pc_dalvik_offset); |
| 607 | dex2pc_offset = native_pc; |
| 608 | dex2pc_dalvik_offset = block->GetDexPc(); |
| 609 | } |
| 610 | } |
| 611 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 612 | uint32_t total_entries = pc2dex_entries + dex2pc_entries; |
| 613 | uint32_t hdr_data_size = UnsignedLeb128Size(total_entries) + UnsignedLeb128Size(pc2dex_entries); |
| 614 | uint32_t data_size = hdr_data_size + pc2dex_data_size + dex2pc_data_size; |
| 615 | data->resize(data_size); |
| 616 | |
| 617 | uint8_t* data_ptr = &(*data)[0]; |
| 618 | uint8_t* write_pos = data_ptr; |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 619 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 620 | write_pos = EncodeUnsignedLeb128(write_pos, total_entries); |
| 621 | write_pos = EncodeUnsignedLeb128(write_pos, pc2dex_entries); |
| 622 | DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size); |
| 623 | uint8_t* write_pos2 = write_pos + pc2dex_data_size; |
| 624 | |
| 625 | pc2dex_offset = 0u; |
| 626 | pc2dex_dalvik_offset = 0u; |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 627 | dex2pc_offset = 0u; |
| 628 | dex2pc_dalvik_offset = 0u; |
| 629 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 630 | for (size_t i = 0; i < pc2dex_entries; i++) { |
Vladimir Marko | bd8c725 | 2015-06-12 10:06:32 +0100 | [diff] [blame] | 631 | const StackMapStream::StackMapEntry& stack_map_entry = stack_map_stream_.GetStackMap(i); |
| 632 | DCHECK(pc2dex_offset <= stack_map_entry.native_pc_offset); |
| 633 | write_pos = EncodeUnsignedLeb128(write_pos, stack_map_entry.native_pc_offset - pc2dex_offset); |
| 634 | write_pos = EncodeSignedLeb128(write_pos, stack_map_entry.dex_pc - pc2dex_dalvik_offset); |
| 635 | pc2dex_offset = stack_map_entry.native_pc_offset; |
| 636 | pc2dex_dalvik_offset = stack_map_entry.dex_pc; |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 637 | } |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 638 | |
| 639 | for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) { |
| 640 | HBasicBlock* block = graph_->GetBlocks().Get(i); |
| 641 | if (block->IsCatchBlock()) { |
| 642 | intptr_t native_pc = GetAddressOf(block); |
| 643 | write_pos2 = EncodeUnsignedLeb128(write_pos2, native_pc - dex2pc_offset); |
| 644 | write_pos2 = EncodeSignedLeb128(write_pos2, block->GetDexPc() - dex2pc_dalvik_offset); |
| 645 | dex2pc_offset = native_pc; |
| 646 | dex2pc_dalvik_offset = block->GetDexPc(); |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 651 | DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size + pc2dex_data_size); |
| 652 | DCHECK_EQ(static_cast<size_t>(write_pos2 - data_ptr), data_size); |
| 653 | |
| 654 | if (kIsDebugBuild) { |
| 655 | // Verify the encoded table holds the expected data. |
| 656 | MappingTable table(data_ptr); |
| 657 | CHECK_EQ(table.TotalSize(), total_entries); |
| 658 | CHECK_EQ(table.PcToDexSize(), pc2dex_entries); |
| 659 | auto it = table.PcToDexBegin(); |
| 660 | auto it2 = table.DexToPcBegin(); |
| 661 | for (size_t i = 0; i < pc2dex_entries; i++) { |
Vladimir Marko | bd8c725 | 2015-06-12 10:06:32 +0100 | [diff] [blame] | 662 | const StackMapStream::StackMapEntry& stack_map_entry = stack_map_stream_.GetStackMap(i); |
| 663 | CHECK_EQ(stack_map_entry.native_pc_offset, it.NativePcOffset()); |
| 664 | CHECK_EQ(stack_map_entry.dex_pc, it.DexPc()); |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 665 | ++it; |
| 666 | } |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 667 | for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) { |
| 668 | HBasicBlock* block = graph_->GetBlocks().Get(i); |
| 669 | if (block->IsCatchBlock()) { |
| 670 | CHECK_EQ(GetAddressOf(block), it2.NativePcOffset()); |
| 671 | CHECK_EQ(block->GetDexPc(), it2.DexPc()); |
| 672 | ++it2; |
| 673 | } |
| 674 | } |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 675 | CHECK(it == table.PcToDexEnd()); |
| 676 | CHECK(it2 == table.DexToPcEnd()); |
| 677 | } |
| 678 | } |
| 679 | |
| 680 | void CodeGenerator::BuildVMapTable(std::vector<uint8_t>* data) const { |
| 681 | Leb128EncodingVector vmap_encoder; |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 682 | // We currently don't use callee-saved registers. |
| 683 | size_t size = 0 + 1 /* marker */ + 0; |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 684 | vmap_encoder.Reserve(size + 1u); // All values are likely to be one byte in ULEB128 (<128). |
| 685 | vmap_encoder.PushBackUnsigned(size); |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 686 | vmap_encoder.PushBackUnsigned(VmapTable::kAdjustedFpMarker); |
| 687 | |
| 688 | *data = vmap_encoder.GetData(); |
| 689 | } |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 690 | |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 691 | void CodeGenerator::BuildStackMaps(std::vector<uint8_t>* data) { |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 692 | uint32_t size = stack_map_stream_.PrepareForFillIn(); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 693 | data->resize(size); |
| 694 | MemoryRegion region(data->data(), size); |
| 695 | stack_map_stream_.FillIn(region); |
| 696 | } |
| 697 | |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 698 | void CodeGenerator::RecordPcInfo(HInstruction* instruction, |
| 699 | uint32_t dex_pc, |
| 700 | SlowPathCode* slow_path) { |
Calin Juravle | d2ec87d | 2014-12-08 14:24:46 +0000 | [diff] [blame] | 701 | if (instruction != nullptr) { |
Alexey Frunze | 4dda337 | 2015-06-01 18:31:49 -0700 | [diff] [blame] | 702 | // The code generated for some type conversions and comparisons |
| 703 | // may call the runtime, thus normally requiring a subsequent |
| 704 | // call to this method. However, the method verifier does not |
| 705 | // produce PC information for certain instructions, which are |
| 706 | // considered "atomic" (they cannot join a GC). |
Roland Levillain | 624279f | 2014-12-04 11:54:28 +0000 | [diff] [blame] | 707 | // Therefore we do not currently record PC information for such |
| 708 | // instructions. As this may change later, we added this special |
| 709 | // case so that code generators may nevertheless call |
| 710 | // CodeGenerator::RecordPcInfo without triggering an error in |
| 711 | // CodeGenerator::BuildNativeGCMap ("Missing ref for dex pc 0x") |
| 712 | // thereafter. |
Alexey Frunze | 4dda337 | 2015-06-01 18:31:49 -0700 | [diff] [blame] | 713 | if (instruction->IsTypeConversion() || instruction->IsCompare()) { |
Calin Juravle | d2ec87d | 2014-12-08 14:24:46 +0000 | [diff] [blame] | 714 | return; |
| 715 | } |
| 716 | if (instruction->IsRem()) { |
| 717 | Primitive::Type type = instruction->AsRem()->GetResultType(); |
| 718 | if ((type == Primitive::kPrimFloat) || (type == Primitive::kPrimDouble)) { |
| 719 | return; |
| 720 | } |
| 721 | } |
Roland Levillain | 624279f | 2014-12-04 11:54:28 +0000 | [diff] [blame] | 722 | } |
| 723 | |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 724 | uint32_t outer_dex_pc = dex_pc; |
| 725 | uint32_t outer_environment_size = 0; |
| 726 | uint32_t inlining_depth = 0; |
| 727 | if (instruction != nullptr) { |
| 728 | for (HEnvironment* environment = instruction->GetEnvironment(); |
| 729 | environment != nullptr; |
| 730 | environment = environment->GetParent()) { |
| 731 | outer_dex_pc = environment->GetDexPc(); |
| 732 | outer_environment_size = environment->Size(); |
| 733 | if (environment != instruction->GetEnvironment()) { |
| 734 | inlining_depth++; |
| 735 | } |
| 736 | } |
| 737 | } |
| 738 | |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 739 | // Collect PC infos for the mapping table. |
Vladimir Marko | bd8c725 | 2015-06-12 10:06:32 +0100 | [diff] [blame] | 740 | uint32_t native_pc = GetAssembler()->CodeSize(); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 741 | |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 742 | if (instruction == nullptr) { |
| 743 | // For stack overflow checks. |
Vladimir Marko | bd8c725 | 2015-06-12 10:06:32 +0100 | [diff] [blame] | 744 | stack_map_stream_.BeginStackMapEntry(outer_dex_pc, native_pc, 0, 0, 0, 0); |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 745 | stack_map_stream_.EndStackMapEntry(); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 746 | return; |
| 747 | } |
| 748 | LocationSummary* locations = instruction->GetLocations(); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 749 | |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 750 | uint32_t register_mask = locations->GetRegisterMask(); |
| 751 | if (locations->OnlyCallsOnSlowPath()) { |
| 752 | // In case of slow path, we currently set the location of caller-save registers |
| 753 | // to register (instead of their stack location when pushed before the slow-path |
| 754 | // call). Therefore register_mask contains both callee-save and caller-save |
| 755 | // registers that hold objects. We must remove the caller-save from the mask, since |
| 756 | // they will be overwritten by the callee. |
| 757 | register_mask &= core_callee_save_mask_; |
| 758 | } |
| 759 | // The register mask must be a subset of callee-save registers. |
| 760 | DCHECK_EQ(register_mask & core_callee_save_mask_, register_mask); |
Vladimir Marko | bd8c725 | 2015-06-12 10:06:32 +0100 | [diff] [blame] | 761 | stack_map_stream_.BeginStackMapEntry(outer_dex_pc, |
| 762 | native_pc, |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 763 | register_mask, |
| 764 | locations->GetStackMask(), |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 765 | outer_environment_size, |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 766 | inlining_depth); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 767 | |
| 768 | EmitEnvironment(instruction->GetEnvironment(), slow_path); |
| 769 | stack_map_stream_.EndStackMapEntry(); |
| 770 | } |
| 771 | |
| 772 | void CodeGenerator::EmitEnvironment(HEnvironment* environment, SlowPathCode* slow_path) { |
| 773 | if (environment == nullptr) return; |
| 774 | |
| 775 | if (environment->GetParent() != nullptr) { |
| 776 | // We emit the parent environment first. |
| 777 | EmitEnvironment(environment->GetParent(), slow_path); |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 778 | stack_map_stream_.BeginInlineInfoEntry(environment->GetMethodIdx(), |
| 779 | environment->GetDexPc(), |
| 780 | environment->GetInvokeType(), |
| 781 | environment->Size()); |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 782 | } |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 783 | |
| 784 | // Walk over the environment, and record the location of dex registers. |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 785 | for (size_t i = 0, environment_size = environment->Size(); i < environment_size; ++i) { |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 786 | HInstruction* current = environment->GetInstructionAt(i); |
| 787 | if (current == nullptr) { |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 788 | stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kNone, 0); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 789 | continue; |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 790 | } |
| 791 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 792 | Location location = environment->GetLocationAt(i); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 793 | switch (location.GetKind()) { |
| 794 | case Location::kConstant: { |
| 795 | DCHECK_EQ(current, location.GetConstant()); |
| 796 | if (current->IsLongConstant()) { |
| 797 | int64_t value = current->AsLongConstant()->GetValue(); |
| 798 | stack_map_stream_.AddDexRegisterEntry( |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 799 | DexRegisterLocation::Kind::kConstant, Low32Bits(value)); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 800 | stack_map_stream_.AddDexRegisterEntry( |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 801 | DexRegisterLocation::Kind::kConstant, High32Bits(value)); |
| 802 | ++i; |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 803 | DCHECK_LT(i, environment_size); |
| 804 | } else if (current->IsDoubleConstant()) { |
Roland Levillain | da4d79b | 2015-03-24 14:36:11 +0000 | [diff] [blame] | 805 | int64_t value = bit_cast<int64_t, double>(current->AsDoubleConstant()->GetValue()); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 806 | stack_map_stream_.AddDexRegisterEntry( |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 807 | DexRegisterLocation::Kind::kConstant, Low32Bits(value)); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 808 | stack_map_stream_.AddDexRegisterEntry( |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 809 | DexRegisterLocation::Kind::kConstant, High32Bits(value)); |
| 810 | ++i; |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 811 | DCHECK_LT(i, environment_size); |
| 812 | } else if (current->IsIntConstant()) { |
| 813 | int32_t value = current->AsIntConstant()->GetValue(); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 814 | stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kConstant, value); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 815 | } else if (current->IsNullConstant()) { |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 816 | stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kConstant, 0); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 817 | } else { |
| 818 | DCHECK(current->IsFloatConstant()) << current->DebugName(); |
Roland Levillain | da4d79b | 2015-03-24 14:36:11 +0000 | [diff] [blame] | 819 | int32_t value = bit_cast<int32_t, float>(current->AsFloatConstant()->GetValue()); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 820 | stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kConstant, value); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 821 | } |
| 822 | break; |
| 823 | } |
| 824 | |
| 825 | case Location::kStackSlot: { |
| 826 | stack_map_stream_.AddDexRegisterEntry( |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 827 | DexRegisterLocation::Kind::kInStack, location.GetStackIndex()); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 828 | break; |
| 829 | } |
| 830 | |
| 831 | case Location::kDoubleStackSlot: { |
| 832 | stack_map_stream_.AddDexRegisterEntry( |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 833 | DexRegisterLocation::Kind::kInStack, location.GetStackIndex()); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 834 | stack_map_stream_.AddDexRegisterEntry( |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 835 | DexRegisterLocation::Kind::kInStack, location.GetHighStackIndex(kVRegSize)); |
| 836 | ++i; |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 837 | DCHECK_LT(i, environment_size); |
| 838 | break; |
| 839 | } |
| 840 | |
| 841 | case Location::kRegister : { |
| 842 | int id = location.reg(); |
| 843 | if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(id)) { |
| 844 | uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(id); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 845 | stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 846 | if (current->GetType() == Primitive::kPrimLong) { |
| 847 | stack_map_stream_.AddDexRegisterEntry( |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 848 | DexRegisterLocation::Kind::kInStack, offset + kVRegSize); |
| 849 | ++i; |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 850 | DCHECK_LT(i, environment_size); |
| 851 | } |
| 852 | } else { |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 853 | stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, id); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 854 | if (current->GetType() == Primitive::kPrimLong) { |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 855 | stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, id); |
| 856 | ++i; |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 857 | DCHECK_LT(i, environment_size); |
| 858 | } |
| 859 | } |
| 860 | break; |
| 861 | } |
| 862 | |
| 863 | case Location::kFpuRegister : { |
| 864 | int id = location.reg(); |
| 865 | if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(id)) { |
| 866 | uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(id); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 867 | stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 868 | if (current->GetType() == Primitive::kPrimDouble) { |
| 869 | stack_map_stream_.AddDexRegisterEntry( |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 870 | DexRegisterLocation::Kind::kInStack, offset + kVRegSize); |
| 871 | ++i; |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 872 | DCHECK_LT(i, environment_size); |
| 873 | } |
| 874 | } else { |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 875 | stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, id); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 876 | if (current->GetType() == Primitive::kPrimDouble) { |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 877 | stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, id); |
| 878 | ++i; |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 879 | DCHECK_LT(i, environment_size); |
| 880 | } |
| 881 | } |
| 882 | break; |
| 883 | } |
| 884 | |
| 885 | case Location::kFpuRegisterPair : { |
| 886 | int low = location.low(); |
| 887 | int high = location.high(); |
| 888 | if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(low)) { |
| 889 | uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(low); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 890 | stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 891 | } else { |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 892 | stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, low); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 893 | } |
| 894 | if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(high)) { |
| 895 | uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(high); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 896 | stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset); |
| 897 | ++i; |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 898 | } else { |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 899 | stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, high); |
| 900 | ++i; |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 901 | } |
| 902 | DCHECK_LT(i, environment_size); |
| 903 | break; |
| 904 | } |
| 905 | |
| 906 | case Location::kRegisterPair : { |
| 907 | int low = location.low(); |
| 908 | int high = location.high(); |
| 909 | if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(low)) { |
| 910 | uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(low); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 911 | stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 912 | } else { |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 913 | stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, low); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 914 | } |
| 915 | if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(high)) { |
| 916 | uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(high); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 917 | stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 918 | } else { |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 919 | stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, high); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 920 | } |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 921 | ++i; |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 922 | DCHECK_LT(i, environment_size); |
| 923 | break; |
| 924 | } |
| 925 | |
| 926 | case Location::kInvalid: { |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 927 | stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kNone, 0); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 928 | break; |
| 929 | } |
| 930 | |
| 931 | default: |
| 932 | LOG(FATAL) << "Unexpected kind " << location.GetKind(); |
| 933 | } |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 934 | } |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 935 | |
| 936 | if (environment->GetParent() != nullptr) { |
| 937 | stack_map_stream_.EndInlineInfoEntry(); |
| 938 | } |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 939 | } |
| 940 | |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 941 | bool CodeGenerator::CanMoveNullCheckToUser(HNullCheck* null_check) { |
| 942 | HInstruction* first_next_not_move = null_check->GetNextDisregardingMoves(); |
Calin Juravle | 641547a | 2015-04-21 22:08:51 +0100 | [diff] [blame] | 943 | |
| 944 | return (first_next_not_move != nullptr) |
| 945 | && first_next_not_move->CanDoImplicitNullCheckOn(null_check->InputAt(0)); |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | void CodeGenerator::MaybeRecordImplicitNullCheck(HInstruction* instr) { |
| 949 | // If we are from a static path don't record the pc as we can't throw NPE. |
| 950 | // NB: having the checks here makes the code much less verbose in the arch |
| 951 | // specific code generators. |
| 952 | if (instr->IsStaticFieldSet() || instr->IsStaticFieldGet()) { |
| 953 | return; |
| 954 | } |
| 955 | |
| 956 | if (!compiler_options_.GetImplicitNullChecks()) { |
| 957 | return; |
| 958 | } |
| 959 | |
Calin Juravle | 641547a | 2015-04-21 22:08:51 +0100 | [diff] [blame] | 960 | if (!instr->CanDoImplicitNullCheckOn(instr->InputAt(0))) { |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 961 | return; |
| 962 | } |
| 963 | |
| 964 | // Find the first previous instruction which is not a move. |
| 965 | HInstruction* first_prev_not_move = instr->GetPreviousDisregardingMoves(); |
| 966 | |
| 967 | // If the instruction is a null check it means that `instr` is the first user |
| 968 | // and needs to record the pc. |
| 969 | if (first_prev_not_move != nullptr && first_prev_not_move->IsNullCheck()) { |
| 970 | HNullCheck* null_check = first_prev_not_move->AsNullCheck(); |
| 971 | // TODO: The parallel moves modify the environment. Their changes need to be reverted |
| 972 | // otherwise the stack maps at the throw point will not be correct. |
| 973 | RecordPcInfo(null_check, null_check->GetDexPc()); |
| 974 | } |
| 975 | } |
| 976 | |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 977 | void CodeGenerator::ClearSpillSlotsFromLoopPhisInStackMap(HSuspendCheck* suspend_check) const { |
| 978 | LocationSummary* locations = suspend_check->GetLocations(); |
| 979 | HBasicBlock* block = suspend_check->GetBlock(); |
| 980 | DCHECK(block->GetLoopInformation()->GetSuspendCheck() == suspend_check); |
| 981 | DCHECK(block->IsLoopHeader()); |
| 982 | |
| 983 | for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) { |
| 984 | HInstruction* current = it.Current(); |
| 985 | LiveInterval* interval = current->GetLiveInterval(); |
| 986 | // We only need to clear bits of loop phis containing objects and allocated in register. |
| 987 | // Loop phis allocated on stack already have the object in the stack. |
| 988 | if (current->GetType() == Primitive::kPrimNot |
| 989 | && interval->HasRegister() |
| 990 | && interval->HasSpillSlot()) { |
| 991 | locations->ClearStackBit(interval->GetSpillSlot() / kVRegSize); |
| 992 | } |
| 993 | } |
| 994 | } |
| 995 | |
Nicolas Geoffray | 9021825 | 2015-04-15 11:56:51 +0100 | [diff] [blame] | 996 | void CodeGenerator::EmitParallelMoves(Location from1, |
| 997 | Location to1, |
| 998 | Primitive::Type type1, |
| 999 | Location from2, |
| 1000 | Location to2, |
| 1001 | Primitive::Type type2) { |
Nicolas Geoffray | f0e3937 | 2014-11-12 17:50:07 +0000 | [diff] [blame] | 1002 | HParallelMove parallel_move(GetGraph()->GetArena()); |
Nicolas Geoffray | 9021825 | 2015-04-15 11:56:51 +0100 | [diff] [blame] | 1003 | parallel_move.AddMove(from1, to1, type1, nullptr); |
| 1004 | parallel_move.AddMove(from2, to2, type2, nullptr); |
Nicolas Geoffray | f0e3937 | 2014-11-12 17:50:07 +0000 | [diff] [blame] | 1005 | GetMoveResolver()->EmitNativeCode(¶llel_move); |
| 1006 | } |
| 1007 | |
Nicolas Geoffray | a8ac913 | 2015-03-13 16:36:36 +0000 | [diff] [blame] | 1008 | void SlowPathCode::RecordPcInfo(CodeGenerator* codegen, HInstruction* instruction, uint32_t dex_pc) { |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 1009 | codegen->RecordPcInfo(instruction, dex_pc, this); |
Nicolas Geoffray | a8ac913 | 2015-03-13 16:36:36 +0000 | [diff] [blame] | 1010 | } |
| 1011 | |
| 1012 | void SlowPathCode::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) { |
| 1013 | RegisterSet* register_set = locations->GetLiveRegisters(); |
| 1014 | size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath(); |
| 1015 | for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) { |
| 1016 | if (!codegen->IsCoreCalleeSaveRegister(i)) { |
| 1017 | if (register_set->ContainsCoreRegister(i)) { |
| 1018 | // If the register holds an object, update the stack mask. |
| 1019 | if (locations->RegisterContainsObject(i)) { |
| 1020 | locations->SetStackBit(stack_offset / kVRegSize); |
| 1021 | } |
| 1022 | DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize()); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 1023 | DCHECK_LT(i, kMaximumNumberOfExpectedRegisters); |
| 1024 | saved_core_stack_offsets_[i] = stack_offset; |
Nicolas Geoffray | a8ac913 | 2015-03-13 16:36:36 +0000 | [diff] [blame] | 1025 | stack_offset += codegen->SaveCoreRegister(stack_offset, i); |
| 1026 | } |
| 1027 | } |
| 1028 | } |
| 1029 | |
| 1030 | for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) { |
| 1031 | if (!codegen->IsFloatingPointCalleeSaveRegister(i)) { |
| 1032 | if (register_set->ContainsFloatingPointRegister(i)) { |
| 1033 | DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize()); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 1034 | DCHECK_LT(i, kMaximumNumberOfExpectedRegisters); |
| 1035 | saved_fpu_stack_offsets_[i] = stack_offset; |
Nicolas Geoffray | a8ac913 | 2015-03-13 16:36:36 +0000 | [diff] [blame] | 1036 | stack_offset += codegen->SaveFloatingPointRegister(stack_offset, i); |
| 1037 | } |
| 1038 | } |
| 1039 | } |
| 1040 | } |
| 1041 | |
| 1042 | void SlowPathCode::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) { |
| 1043 | RegisterSet* register_set = locations->GetLiveRegisters(); |
| 1044 | size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath(); |
| 1045 | for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) { |
| 1046 | if (!codegen->IsCoreCalleeSaveRegister(i)) { |
| 1047 | if (register_set->ContainsCoreRegister(i)) { |
| 1048 | DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize()); |
| 1049 | stack_offset += codegen->RestoreCoreRegister(stack_offset, i); |
| 1050 | } |
| 1051 | } |
| 1052 | } |
| 1053 | |
| 1054 | for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) { |
| 1055 | if (!codegen->IsFloatingPointCalleeSaveRegister(i)) { |
| 1056 | if (register_set->ContainsFloatingPointRegister(i)) { |
| 1057 | DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize()); |
| 1058 | stack_offset += codegen->RestoreFloatingPointRegister(stack_offset, i); |
| 1059 | } |
| 1060 | } |
| 1061 | } |
| 1062 | } |
| 1063 | |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 1064 | } // namespace art |