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