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