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