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