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