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