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 | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 44 | const GrowableArray<HBasicBlock*>& blocks = GetGraph()->GetBlocks(); |
| 45 | DCHECK(blocks.Get(0) == GetGraph()->GetEntryBlock()); |
| 46 | DCHECK(GoesToNextBlock(GetGraph()->GetEntryBlock(), blocks.Get(1))); |
Nicolas Geoffray | 92a73ae | 2014-10-16 11:12:52 +0100 | [diff] [blame] | 47 | Initialize(); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 48 | |
| 49 | DCHECK_EQ(frame_size_, kUninitializedFrameSize); |
Nicolas Geoffray | 73e80c3 | 2014-07-22 17:47:56 +0100 | [diff] [blame] | 50 | if (!is_leaf) { |
| 51 | MarkNotLeaf(); |
| 52 | } |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 53 | ComputeFrameSize(GetGraph()->GetNumberOfLocalVRegs() |
Calin Juravle | f97f9fb | 2014-11-11 15:38:19 +0000 | [diff] [blame] | 54 | + GetGraph()->GetTemporariesVRegSlots() |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 55 | + 1 /* filler */, |
Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 56 | 0, /* the baseline compiler does not have live registers at slow path */ |
Mark Mendell | f85a9ca | 2015-01-13 09:20:58 -0500 | [diff] [blame] | 57 | 0, /* the baseline compiler does not have live registers at slow path */ |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 58 | GetGraph()->GetMaximumNumberOfOutVRegs() |
| 59 | + 1 /* current method */); |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 60 | GenerateFrameEntry(); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 61 | |
Nicolas Geoffray | 8a16d97 | 2014-09-11 10:30:02 +0100 | [diff] [blame] | 62 | HGraphVisitor* location_builder = GetLocationBuilder(); |
| 63 | HGraphVisitor* instruction_visitor = GetInstructionVisitor(); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 64 | for (size_t i = 0, e = blocks.Size(); i < e; ++i) { |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 65 | HBasicBlock* block = blocks.Get(i); |
Nicolas Geoffray | 92a73ae | 2014-10-16 11:12:52 +0100 | [diff] [blame] | 66 | Bind(block); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 67 | for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) { |
| 68 | HInstruction* current = it.Current(); |
| 69 | current->Accept(location_builder); |
| 70 | InitLocations(current); |
| 71 | current->Accept(instruction_visitor); |
| 72 | } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 73 | } |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 74 | GenerateSlowPaths(); |
Serban Constantinescu | 32f5b4d | 2014-11-25 20:05:46 +0000 | [diff] [blame] | 75 | Finalize(allocator); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 78 | void CodeGenerator::CompileOptimized(CodeAllocator* allocator) { |
| 79 | // The frame size has already been computed during register allocation. |
| 80 | DCHECK_NE(frame_size_, kUninitializedFrameSize); |
| 81 | const GrowableArray<HBasicBlock*>& blocks = GetGraph()->GetBlocks(); |
| 82 | DCHECK(blocks.Get(0) == GetGraph()->GetEntryBlock()); |
| 83 | DCHECK(GoesToNextBlock(GetGraph()->GetEntryBlock(), blocks.Get(1))); |
Nicolas Geoffray | 92a73ae | 2014-10-16 11:12:52 +0100 | [diff] [blame] | 84 | Initialize(); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 85 | |
| 86 | GenerateFrameEntry(); |
Nicolas Geoffray | 8a16d97 | 2014-09-11 10:30:02 +0100 | [diff] [blame] | 87 | HGraphVisitor* instruction_visitor = GetInstructionVisitor(); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 88 | for (size_t i = 0, e = blocks.Size(); i < e; ++i) { |
| 89 | HBasicBlock* block = blocks.Get(i); |
Nicolas Geoffray | 92a73ae | 2014-10-16 11:12:52 +0100 | [diff] [blame] | 90 | Bind(block); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 91 | for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) { |
| 92 | HInstruction* current = it.Current(); |
| 93 | current->Accept(instruction_visitor); |
| 94 | } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 95 | } |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 96 | GenerateSlowPaths(); |
Serban Constantinescu | 32f5b4d | 2014-11-25 20:05:46 +0000 | [diff] [blame] | 97 | Finalize(allocator); |
| 98 | } |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 99 | |
Serban Constantinescu | 32f5b4d | 2014-11-25 20:05:46 +0000 | [diff] [blame] | 100 | void CodeGenerator::Finalize(CodeAllocator* allocator) { |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 101 | size_t code_size = GetAssembler()->CodeSize(); |
| 102 | uint8_t* buffer = allocator->Allocate(code_size); |
Serban Constantinescu | 32f5b4d | 2014-11-25 20:05:46 +0000 | [diff] [blame] | 103 | |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 104 | MemoryRegion code(buffer, code_size); |
| 105 | GetAssembler()->FinalizeInstructions(code); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 108 | void CodeGenerator::GenerateSlowPaths() { |
| 109 | for (size_t i = 0, e = slow_paths_.Size(); i < e; ++i) { |
| 110 | slow_paths_.Get(i)->EmitNativeCode(this); |
| 111 | } |
| 112 | } |
| 113 | |
Nicolas Geoffray | 71175b7 | 2014-10-09 22:13:55 +0100 | [diff] [blame] | 114 | size_t CodeGenerator::FindFreeEntry(bool* array, size_t length) { |
| 115 | for (size_t i = 0; i < length; ++i) { |
| 116 | if (!array[i]) { |
| 117 | array[i] = true; |
| 118 | return i; |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 119 | } |
| 120 | } |
Nicolas Geoffray | 71175b7 | 2014-10-09 22:13:55 +0100 | [diff] [blame] | 121 | LOG(FATAL) << "Could not find a register in baseline register allocator"; |
Nicolas Geoffray | 1ba0f59 | 2014-10-27 15:14:55 +0000 | [diff] [blame] | 122 | UNREACHABLE(); |
| 123 | return -1; |
| 124 | } |
| 125 | |
Nicolas Geoffray | 3c03503 | 2014-10-28 10:46:40 +0000 | [diff] [blame] | 126 | size_t CodeGenerator::FindTwoFreeConsecutiveAlignedEntries(bool* array, size_t length) { |
| 127 | for (size_t i = 0; i < length - 1; i += 2) { |
Nicolas Geoffray | 1ba0f59 | 2014-10-27 15:14:55 +0000 | [diff] [blame] | 128 | if (!array[i] && !array[i + 1]) { |
| 129 | array[i] = true; |
| 130 | array[i + 1] = true; |
| 131 | return i; |
| 132 | } |
| 133 | } |
| 134 | LOG(FATAL) << "Could not find a register in baseline register allocator"; |
| 135 | UNREACHABLE(); |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 136 | return -1; |
| 137 | } |
| 138 | |
Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 139 | void CodeGenerator::ComputeFrameSize(size_t number_of_spill_slots, |
Mark Mendell | f85a9ca | 2015-01-13 09:20:58 -0500 | [diff] [blame] | 140 | size_t maximum_number_of_live_core_registers, |
| 141 | size_t maximum_number_of_live_fp_registers, |
Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 142 | size_t number_of_out_slots) { |
| 143 | first_register_slot_in_slow_path_ = (number_of_out_slots + number_of_spill_slots) * kVRegSize; |
| 144 | |
Nicolas Geoffray | ab032bc | 2014-07-15 12:55:21 +0100 | [diff] [blame] | 145 | SetFrameSize(RoundUp( |
| 146 | number_of_spill_slots * kVRegSize |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 147 | + number_of_out_slots * kVRegSize |
Mark Mendell | f85a9ca | 2015-01-13 09:20:58 -0500 | [diff] [blame] | 148 | + maximum_number_of_live_core_registers * GetWordSize() |
| 149 | + maximum_number_of_live_fp_registers * GetFloatingPointSpillSlotSize() |
Nicolas Geoffray | ab032bc | 2014-07-15 12:55:21 +0100 | [diff] [blame] | 150 | + FrameEntrySpillSize(), |
| 151 | kStackAlignment)); |
| 152 | } |
| 153 | |
| 154 | Location CodeGenerator::GetTemporaryLocation(HTemporary* temp) const { |
| 155 | uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs(); |
Calin Juravle | f97f9fb | 2014-11-11 15:38:19 +0000 | [diff] [blame] | 156 | // The type of the previous instruction tells us if we need a single or double stack slot. |
| 157 | Primitive::Type type = temp->GetType(); |
| 158 | int32_t temp_size = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble) ? 2 : 1; |
Nicolas Geoffray | ab032bc | 2014-07-15 12:55:21 +0100 | [diff] [blame] | 159 | // Use the temporary region (right below the dex registers). |
| 160 | int32_t slot = GetFrameSize() - FrameEntrySpillSize() |
| 161 | - kVRegSize // filler |
| 162 | - (number_of_locals * kVRegSize) |
Calin Juravle | f97f9fb | 2014-11-11 15:38:19 +0000 | [diff] [blame] | 163 | - ((temp_size + temp->GetIndex()) * kVRegSize); |
| 164 | return temp_size == 2 ? Location::DoubleStackSlot(slot) : Location::StackSlot(slot); |
Nicolas Geoffray | ab032bc | 2014-07-15 12:55:21 +0100 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | int32_t CodeGenerator::GetStackSlot(HLocal* local) const { |
| 168 | uint16_t reg_number = local->GetRegNumber(); |
| 169 | uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs(); |
| 170 | if (reg_number >= number_of_locals) { |
| 171 | // Local is a parameter of the method. It is stored in the caller's frame. |
| 172 | return GetFrameSize() + kVRegSize // ART method |
| 173 | + (reg_number - number_of_locals) * kVRegSize; |
| 174 | } else { |
| 175 | // Local is a temporary in this method. It is stored in this method's frame. |
| 176 | return GetFrameSize() - FrameEntrySpillSize() |
| 177 | - kVRegSize // filler. |
| 178 | - (number_of_locals * kVRegSize) |
| 179 | + (reg_number * kVRegSize); |
| 180 | } |
| 181 | } |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 182 | |
| 183 | void CodeGenerator::AllocateRegistersLocally(HInstruction* instruction) const { |
| 184 | LocationSummary* locations = instruction->GetLocations(); |
| 185 | if (locations == nullptr) return; |
| 186 | |
Nicolas Geoffray | 71175b7 | 2014-10-09 22:13:55 +0100 | [diff] [blame] | 187 | for (size_t i = 0, e = GetNumberOfCoreRegisters(); i < e; ++i) { |
| 188 | blocked_core_registers_[i] = false; |
| 189 | } |
| 190 | |
| 191 | for (size_t i = 0, e = GetNumberOfFloatingPointRegisters(); i < e; ++i) { |
| 192 | blocked_fpu_registers_[i] = false; |
| 193 | } |
| 194 | |
| 195 | for (size_t i = 0, e = number_of_register_pairs_; i < e; ++i) { |
| 196 | blocked_register_pairs_[i] = false; |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | // Mark all fixed input, temp and output registers as used. |
| 200 | for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) { |
| 201 | Location loc = locations->InAt(i); |
Nicolas Geoffray | 5b4b898 | 2014-12-18 17:45:56 +0000 | [diff] [blame] | 202 | // The DCHECKS below check that a register is not specified twice in |
| 203 | // the summary. |
| 204 | if (loc.IsRegister()) { |
| 205 | DCHECK(!blocked_core_registers_[loc.reg()]); |
| 206 | blocked_core_registers_[loc.reg()] = true; |
| 207 | } else if (loc.IsFpuRegister()) { |
| 208 | DCHECK(!blocked_fpu_registers_[loc.reg()]); |
| 209 | blocked_fpu_registers_[loc.reg()] = true; |
| 210 | } else if (loc.IsFpuRegisterPair()) { |
| 211 | DCHECK(!blocked_fpu_registers_[loc.AsFpuRegisterPairLow<int>()]); |
| 212 | blocked_fpu_registers_[loc.AsFpuRegisterPairLow<int>()] = true; |
| 213 | DCHECK(!blocked_fpu_registers_[loc.AsFpuRegisterPairHigh<int>()]); |
| 214 | blocked_fpu_registers_[loc.AsFpuRegisterPairHigh<int>()] = true; |
| 215 | } else if (loc.IsRegisterPair()) { |
| 216 | DCHECK(!blocked_core_registers_[loc.AsRegisterPairLow<int>()]); |
| 217 | blocked_core_registers_[loc.AsRegisterPairLow<int>()] = true; |
| 218 | DCHECK(!blocked_core_registers_[loc.AsRegisterPairHigh<int>()]); |
| 219 | blocked_core_registers_[loc.AsRegisterPairHigh<int>()] = true; |
| 220 | } |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) { |
| 224 | Location loc = locations->GetTemp(i); |
Nicolas Geoffray | 5b4b898 | 2014-12-18 17:45:56 +0000 | [diff] [blame] | 225 | // The DCHECKS below check that a register is not specified twice in |
| 226 | // the summary. |
| 227 | if (loc.IsRegister()) { |
| 228 | DCHECK(!blocked_core_registers_[loc.reg()]); |
| 229 | blocked_core_registers_[loc.reg()] = true; |
| 230 | } else if (loc.IsFpuRegister()) { |
| 231 | DCHECK(!blocked_fpu_registers_[loc.reg()]); |
| 232 | blocked_fpu_registers_[loc.reg()] = true; |
| 233 | } else { |
| 234 | DCHECK(loc.GetPolicy() == Location::kRequiresRegister |
| 235 | || loc.GetPolicy() == Location::kRequiresFpuRegister); |
| 236 | } |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 237 | } |
| 238 | |
Nicolas Geoffray | 71175b7 | 2014-10-09 22:13:55 +0100 | [diff] [blame] | 239 | SetupBlockedRegisters(); |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 240 | |
| 241 | // Allocate all unallocated input locations. |
| 242 | for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) { |
| 243 | Location loc = locations->InAt(i); |
| 244 | HInstruction* input = instruction->InputAt(i); |
| 245 | if (loc.IsUnallocated()) { |
Nicolas Geoffray | 56b9ee6 | 2014-10-09 11:47:51 +0100 | [diff] [blame] | 246 | if ((loc.GetPolicy() == Location::kRequiresRegister) |
| 247 | || (loc.GetPolicy() == Location::kRequiresFpuRegister)) { |
Nicolas Geoffray | 71175b7 | 2014-10-09 22:13:55 +0100 | [diff] [blame] | 248 | loc = AllocateFreeRegister(input->GetType()); |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 249 | } else { |
| 250 | DCHECK_EQ(loc.GetPolicy(), Location::kAny); |
| 251 | HLoadLocal* load = input->AsLoadLocal(); |
| 252 | if (load != nullptr) { |
| 253 | loc = GetStackLocation(load); |
| 254 | } else { |
Nicolas Geoffray | 71175b7 | 2014-10-09 22:13:55 +0100 | [diff] [blame] | 255 | loc = AllocateFreeRegister(input->GetType()); |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | locations->SetInAt(i, loc); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | // Allocate all unallocated temp locations. |
| 263 | for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) { |
| 264 | Location loc = locations->GetTemp(i); |
| 265 | if (loc.IsUnallocated()) { |
Roland Levillain | 647b9ed | 2014-11-27 12:06:00 +0000 | [diff] [blame] | 266 | switch (loc.GetPolicy()) { |
| 267 | case Location::kRequiresRegister: |
| 268 | // Allocate a core register (large enough to fit a 32-bit integer). |
| 269 | loc = AllocateFreeRegister(Primitive::kPrimInt); |
| 270 | break; |
| 271 | |
| 272 | case Location::kRequiresFpuRegister: |
| 273 | // Allocate a core register (large enough to fit a 64-bit double). |
| 274 | loc = AllocateFreeRegister(Primitive::kPrimDouble); |
| 275 | break; |
| 276 | |
| 277 | default: |
| 278 | LOG(FATAL) << "Unexpected policy for temporary location " |
| 279 | << loc.GetPolicy(); |
| 280 | } |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 281 | locations->SetTempAt(i, loc); |
| 282 | } |
| 283 | } |
Nicolas Geoffray | 5b4b898 | 2014-12-18 17:45:56 +0000 | [diff] [blame] | 284 | Location result_location = locations->Out(); |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 285 | if (result_location.IsUnallocated()) { |
| 286 | switch (result_location.GetPolicy()) { |
| 287 | case Location::kAny: |
| 288 | case Location::kRequiresRegister: |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 289 | case Location::kRequiresFpuRegister: |
Nicolas Geoffray | 71175b7 | 2014-10-09 22:13:55 +0100 | [diff] [blame] | 290 | result_location = AllocateFreeRegister(instruction->GetType()); |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 291 | break; |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 292 | case Location::kSameAsFirstInput: |
| 293 | result_location = locations->InAt(0); |
| 294 | break; |
| 295 | } |
| 296 | locations->SetOut(result_location); |
| 297 | } |
| 298 | } |
| 299 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 300 | void CodeGenerator::InitLocations(HInstruction* instruction) { |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 301 | if (instruction->GetLocations() == nullptr) { |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 302 | if (instruction->IsTemporary()) { |
| 303 | HInstruction* previous = instruction->GetPrevious(); |
| 304 | Location temp_location = GetTemporaryLocation(instruction->AsTemporary()); |
| 305 | Move(previous, temp_location, instruction); |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 306 | } |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 307 | return; |
| 308 | } |
| 309 | AllocateRegistersLocally(instruction); |
| 310 | for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 311 | Location location = instruction->GetLocations()->InAt(i); |
Nicolas Geoffray | f43083d | 2014-11-07 10:48:10 +0000 | [diff] [blame] | 312 | HInstruction* input = instruction->InputAt(i); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 313 | if (location.IsValid()) { |
| 314 | // Move the input to the desired location. |
Nicolas Geoffray | f43083d | 2014-11-07 10:48:10 +0000 | [diff] [blame] | 315 | if (input->GetNext()->IsTemporary()) { |
| 316 | // If the input was stored in a temporary, use that temporary to |
| 317 | // perform the move. |
| 318 | Move(input->GetNext(), location, instruction); |
| 319 | } else { |
| 320 | Move(input, location, instruction); |
| 321 | } |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 322 | } |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | bool CodeGenerator::GoesToNextBlock(HBasicBlock* current, HBasicBlock* next) const { |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 327 | // We currently iterate over the block in insertion order. |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 328 | return current->GetBlockId() + 1 == next->GetBlockId(); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Nicolas Geoffray | 12df9eb | 2015-01-09 14:53:50 +0000 | [diff] [blame] | 331 | CodeGenerator* CodeGenerator::Create(HGraph* graph, |
Calin Juravle | 3416601 | 2014-12-19 17:22:29 +0000 | [diff] [blame] | 332 | InstructionSet instruction_set, |
Calin Juravle | cd6dffe | 2015-01-08 17:35:35 +0000 | [diff] [blame] | 333 | const InstructionSetFeatures& isa_features, |
| 334 | const CompilerOptions& compiler_options) { |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 335 | switch (instruction_set) { |
| 336 | case kArm: |
| 337 | case kThumb2: { |
Nicolas Geoffray | 12df9eb | 2015-01-09 14:53:50 +0000 | [diff] [blame] | 338 | return new arm::CodeGeneratorARM(graph, |
Calin Juravle | cd6dffe | 2015-01-08 17:35:35 +0000 | [diff] [blame] | 339 | *isa_features.AsArmInstructionSetFeatures(), |
| 340 | compiler_options); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 341 | } |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 342 | case kArm64: { |
Calin Juravle | cd6dffe | 2015-01-08 17:35:35 +0000 | [diff] [blame] | 343 | return new arm64::CodeGeneratorARM64(graph, compiler_options); |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 344 | } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 345 | case kMips: |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 346 | return nullptr; |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 347 | case kX86: { |
Calin Juravle | cd6dffe | 2015-01-08 17:35:35 +0000 | [diff] [blame] | 348 | return new x86::CodeGeneratorX86(graph, compiler_options); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 349 | } |
Dmitry Petrochenko | 6a58cb1 | 2014-04-02 17:27:59 +0700 | [diff] [blame] | 350 | case kX86_64: { |
Calin Juravle | cd6dffe | 2015-01-08 17:35:35 +0000 | [diff] [blame] | 351 | return new x86_64::CodeGeneratorX86_64(graph, compiler_options); |
Dmitry Petrochenko | 6a58cb1 | 2014-04-02 17:27:59 +0700 | [diff] [blame] | 352 | } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 353 | default: |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 354 | return nullptr; |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 355 | } |
| 356 | } |
| 357 | |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 358 | void CodeGenerator::BuildNativeGCMap( |
| 359 | std::vector<uint8_t>* data, const DexCompilationUnit& dex_compilation_unit) const { |
| 360 | const std::vector<uint8_t>& gc_map_raw = |
| 361 | dex_compilation_unit.GetVerifiedMethod()->GetDexGcMap(); |
| 362 | verifier::DexPcToReferenceMap dex_gc_map(&(gc_map_raw)[0]); |
| 363 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 364 | uint32_t max_native_offset = 0; |
| 365 | for (size_t i = 0; i < pc_infos_.Size(); i++) { |
| 366 | uint32_t native_offset = pc_infos_.Get(i).native_pc; |
| 367 | if (native_offset > max_native_offset) { |
| 368 | max_native_offset = native_offset; |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | GcMapBuilder builder(data, pc_infos_.Size(), max_native_offset, dex_gc_map.RegWidth()); |
| 373 | for (size_t i = 0; i < pc_infos_.Size(); i++) { |
| 374 | struct PcInfo pc_info = pc_infos_.Get(i); |
| 375 | uint32_t native_offset = pc_info.native_pc; |
| 376 | uint32_t dex_pc = pc_info.dex_pc; |
| 377 | const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false); |
| 378 | CHECK(references != NULL) << "Missing ref for dex pc 0x" << std::hex << dex_pc; |
| 379 | builder.AddEntry(native_offset, references); |
| 380 | } |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 383 | void CodeGenerator::BuildMappingTable(std::vector<uint8_t>* data, DefaultSrcMap* src_map) const { |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 384 | uint32_t pc2dex_data_size = 0u; |
| 385 | uint32_t pc2dex_entries = pc_infos_.Size(); |
| 386 | uint32_t pc2dex_offset = 0u; |
| 387 | int32_t pc2dex_dalvik_offset = 0; |
| 388 | uint32_t dex2pc_data_size = 0u; |
| 389 | uint32_t dex2pc_entries = 0u; |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 390 | uint32_t dex2pc_offset = 0u; |
| 391 | int32_t dex2pc_dalvik_offset = 0; |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 392 | |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 393 | if (src_map != nullptr) { |
| 394 | src_map->reserve(pc2dex_entries); |
| 395 | } |
| 396 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 397 | for (size_t i = 0; i < pc2dex_entries; i++) { |
| 398 | struct PcInfo pc_info = pc_infos_.Get(i); |
| 399 | pc2dex_data_size += UnsignedLeb128Size(pc_info.native_pc - pc2dex_offset); |
| 400 | pc2dex_data_size += SignedLeb128Size(pc_info.dex_pc - pc2dex_dalvik_offset); |
| 401 | pc2dex_offset = pc_info.native_pc; |
| 402 | pc2dex_dalvik_offset = pc_info.dex_pc; |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 403 | if (src_map != nullptr) { |
| 404 | src_map->push_back(SrcMapElem({pc2dex_offset, pc2dex_dalvik_offset})); |
| 405 | } |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 406 | } |
| 407 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 408 | // Walk over the blocks and find which ones correspond to catch block entries. |
| 409 | for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) { |
| 410 | HBasicBlock* block = graph_->GetBlocks().Get(i); |
| 411 | if (block->IsCatchBlock()) { |
| 412 | intptr_t native_pc = GetAddressOf(block); |
| 413 | ++dex2pc_entries; |
| 414 | dex2pc_data_size += UnsignedLeb128Size(native_pc - dex2pc_offset); |
| 415 | dex2pc_data_size += SignedLeb128Size(block->GetDexPc() - dex2pc_dalvik_offset); |
| 416 | dex2pc_offset = native_pc; |
| 417 | dex2pc_dalvik_offset = block->GetDexPc(); |
| 418 | } |
| 419 | } |
| 420 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 421 | uint32_t total_entries = pc2dex_entries + dex2pc_entries; |
| 422 | uint32_t hdr_data_size = UnsignedLeb128Size(total_entries) + UnsignedLeb128Size(pc2dex_entries); |
| 423 | uint32_t data_size = hdr_data_size + pc2dex_data_size + dex2pc_data_size; |
| 424 | data->resize(data_size); |
| 425 | |
| 426 | uint8_t* data_ptr = &(*data)[0]; |
| 427 | uint8_t* write_pos = data_ptr; |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 428 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 429 | write_pos = EncodeUnsignedLeb128(write_pos, total_entries); |
| 430 | write_pos = EncodeUnsignedLeb128(write_pos, pc2dex_entries); |
| 431 | DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size); |
| 432 | uint8_t* write_pos2 = write_pos + pc2dex_data_size; |
| 433 | |
| 434 | pc2dex_offset = 0u; |
| 435 | pc2dex_dalvik_offset = 0u; |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 436 | dex2pc_offset = 0u; |
| 437 | dex2pc_dalvik_offset = 0u; |
| 438 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 439 | for (size_t i = 0; i < pc2dex_entries; i++) { |
| 440 | struct PcInfo pc_info = pc_infos_.Get(i); |
| 441 | DCHECK(pc2dex_offset <= pc_info.native_pc); |
| 442 | write_pos = EncodeUnsignedLeb128(write_pos, pc_info.native_pc - pc2dex_offset); |
| 443 | write_pos = EncodeSignedLeb128(write_pos, pc_info.dex_pc - pc2dex_dalvik_offset); |
| 444 | pc2dex_offset = pc_info.native_pc; |
| 445 | pc2dex_dalvik_offset = pc_info.dex_pc; |
| 446 | } |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 447 | |
| 448 | for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) { |
| 449 | HBasicBlock* block = graph_->GetBlocks().Get(i); |
| 450 | if (block->IsCatchBlock()) { |
| 451 | intptr_t native_pc = GetAddressOf(block); |
| 452 | write_pos2 = EncodeUnsignedLeb128(write_pos2, native_pc - dex2pc_offset); |
| 453 | write_pos2 = EncodeSignedLeb128(write_pos2, block->GetDexPc() - dex2pc_dalvik_offset); |
| 454 | dex2pc_offset = native_pc; |
| 455 | dex2pc_dalvik_offset = block->GetDexPc(); |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 460 | DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size + pc2dex_data_size); |
| 461 | DCHECK_EQ(static_cast<size_t>(write_pos2 - data_ptr), data_size); |
| 462 | |
| 463 | if (kIsDebugBuild) { |
| 464 | // Verify the encoded table holds the expected data. |
| 465 | MappingTable table(data_ptr); |
| 466 | CHECK_EQ(table.TotalSize(), total_entries); |
| 467 | CHECK_EQ(table.PcToDexSize(), pc2dex_entries); |
| 468 | auto it = table.PcToDexBegin(); |
| 469 | auto it2 = table.DexToPcBegin(); |
| 470 | for (size_t i = 0; i < pc2dex_entries; i++) { |
| 471 | struct PcInfo pc_info = pc_infos_.Get(i); |
| 472 | CHECK_EQ(pc_info.native_pc, it.NativePcOffset()); |
| 473 | CHECK_EQ(pc_info.dex_pc, it.DexPc()); |
| 474 | ++it; |
| 475 | } |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 476 | for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) { |
| 477 | HBasicBlock* block = graph_->GetBlocks().Get(i); |
| 478 | if (block->IsCatchBlock()) { |
| 479 | CHECK_EQ(GetAddressOf(block), it2.NativePcOffset()); |
| 480 | CHECK_EQ(block->GetDexPc(), it2.DexPc()); |
| 481 | ++it2; |
| 482 | } |
| 483 | } |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 484 | CHECK(it == table.PcToDexEnd()); |
| 485 | CHECK(it2 == table.DexToPcEnd()); |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | void CodeGenerator::BuildVMapTable(std::vector<uint8_t>* data) const { |
| 490 | Leb128EncodingVector vmap_encoder; |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 491 | // We currently don't use callee-saved registers. |
| 492 | size_t size = 0 + 1 /* marker */ + 0; |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 493 | vmap_encoder.Reserve(size + 1u); // All values are likely to be one byte in ULEB128 (<128). |
| 494 | vmap_encoder.PushBackUnsigned(size); |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 495 | vmap_encoder.PushBackUnsigned(VmapTable::kAdjustedFpMarker); |
| 496 | |
| 497 | *data = vmap_encoder.GetData(); |
| 498 | } |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 499 | |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 500 | void CodeGenerator::BuildStackMaps(std::vector<uint8_t>* data) { |
| 501 | uint32_t size = stack_map_stream_.ComputeNeededSize(); |
| 502 | data->resize(size); |
| 503 | MemoryRegion region(data->data(), size); |
| 504 | stack_map_stream_.FillIn(region); |
| 505 | } |
| 506 | |
| 507 | void CodeGenerator::RecordPcInfo(HInstruction* instruction, uint32_t dex_pc) { |
Calin Juravle | d2ec87d | 2014-12-08 14:24:46 +0000 | [diff] [blame] | 508 | if (instruction != nullptr) { |
Roland Levillain | 624279f | 2014-12-04 11:54:28 +0000 | [diff] [blame] | 509 | // The code generated for some type conversions may call the |
| 510 | // runtime, thus normally requiring a subsequent call to this |
| 511 | // method. However, the method verifier does not produce PC |
Calin Juravle | d2ec87d | 2014-12-08 14:24:46 +0000 | [diff] [blame] | 512 | // information for certain instructions, which are considered "atomic" |
| 513 | // (they cannot join a GC). |
Roland Levillain | 624279f | 2014-12-04 11:54:28 +0000 | [diff] [blame] | 514 | // Therefore we do not currently record PC information for such |
| 515 | // instructions. As this may change later, we added this special |
| 516 | // case so that code generators may nevertheless call |
| 517 | // CodeGenerator::RecordPcInfo without triggering an error in |
| 518 | // CodeGenerator::BuildNativeGCMap ("Missing ref for dex pc 0x") |
| 519 | // thereafter. |
Calin Juravle | d2ec87d | 2014-12-08 14:24:46 +0000 | [diff] [blame] | 520 | if (instruction->IsTypeConversion()) { |
| 521 | return; |
| 522 | } |
| 523 | if (instruction->IsRem()) { |
| 524 | Primitive::Type type = instruction->AsRem()->GetResultType(); |
| 525 | if ((type == Primitive::kPrimFloat) || (type == Primitive::kPrimDouble)) { |
| 526 | return; |
| 527 | } |
| 528 | } |
Roland Levillain | 624279f | 2014-12-04 11:54:28 +0000 | [diff] [blame] | 529 | } |
| 530 | |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 531 | // Collect PC infos for the mapping table. |
| 532 | struct PcInfo pc_info; |
| 533 | pc_info.dex_pc = dex_pc; |
| 534 | pc_info.native_pc = GetAssembler()->CodeSize(); |
| 535 | pc_infos_.Add(pc_info); |
| 536 | |
| 537 | // Populate stack map information. |
| 538 | |
| 539 | if (instruction == nullptr) { |
| 540 | // For stack overflow checks. |
| 541 | stack_map_stream_.AddStackMapEntry(dex_pc, pc_info.native_pc, 0, 0, 0, 0); |
| 542 | return; |
| 543 | } |
| 544 | |
| 545 | LocationSummary* locations = instruction->GetLocations(); |
| 546 | HEnvironment* environment = instruction->GetEnvironment(); |
| 547 | |
| 548 | size_t environment_size = instruction->EnvironmentSize(); |
| 549 | |
| 550 | size_t register_mask = 0; |
| 551 | size_t inlining_depth = 0; |
| 552 | stack_map_stream_.AddStackMapEntry( |
| 553 | dex_pc, pc_info.native_pc, register_mask, |
| 554 | locations->GetStackMask(), environment_size, inlining_depth); |
| 555 | |
| 556 | // Walk over the environment, and record the location of dex registers. |
| 557 | for (size_t i = 0; i < environment_size; ++i) { |
| 558 | HInstruction* current = environment->GetInstructionAt(i); |
| 559 | if (current == nullptr) { |
| 560 | stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kNone, 0); |
| 561 | continue; |
| 562 | } |
| 563 | |
| 564 | Location location = locations->GetEnvironmentAt(i); |
| 565 | switch (location.GetKind()) { |
| 566 | case Location::kConstant: { |
| 567 | DCHECK(current == location.GetConstant()); |
| 568 | if (current->IsLongConstant()) { |
| 569 | int64_t value = current->AsLongConstant()->GetValue(); |
| 570 | stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, Low32Bits(value)); |
| 571 | stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, High32Bits(value)); |
| 572 | ++i; |
| 573 | DCHECK_LT(i, environment_size); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 574 | } else if (current->IsDoubleConstant()) { |
| 575 | int64_t value = bit_cast<double, int64_t>(current->AsDoubleConstant()->GetValue()); |
| 576 | stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, Low32Bits(value)); |
| 577 | stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, High32Bits(value)); |
| 578 | ++i; |
| 579 | DCHECK_LT(i, environment_size); |
| 580 | } else if (current->IsIntConstant()) { |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 581 | int32_t value = current->AsIntConstant()->GetValue(); |
| 582 | stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, value); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 583 | } else { |
| 584 | DCHECK(current->IsFloatConstant()); |
| 585 | int32_t value = bit_cast<float, int32_t>(current->AsFloatConstant()->GetValue()); |
| 586 | stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, value); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 587 | } |
| 588 | break; |
| 589 | } |
| 590 | |
| 591 | case Location::kStackSlot: { |
| 592 | stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInStack, location.GetStackIndex()); |
| 593 | break; |
| 594 | } |
| 595 | |
| 596 | case Location::kDoubleStackSlot: { |
| 597 | stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInStack, location.GetStackIndex()); |
| 598 | stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInStack, |
| 599 | location.GetHighStackIndex(kVRegSize)); |
| 600 | ++i; |
| 601 | DCHECK_LT(i, environment_size); |
| 602 | break; |
| 603 | } |
| 604 | |
| 605 | case Location::kRegister : { |
Nicolas Geoffray | 56b9ee6 | 2014-10-09 11:47:51 +0100 | [diff] [blame] | 606 | int id = location.reg(); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 607 | stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInRegister, id); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 608 | if (current->GetType() == Primitive::kPrimLong) { |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 609 | stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInRegister, id); |
| 610 | ++i; |
| 611 | DCHECK_LT(i, environment_size); |
| 612 | } |
| 613 | break; |
| 614 | } |
| 615 | |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 616 | case Location::kFpuRegister : { |
| 617 | int id = location.reg(); |
| 618 | stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInFpuRegister, id); |
| 619 | if (current->GetType() == Primitive::kPrimDouble) { |
| 620 | stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInFpuRegister, id); |
| 621 | ++i; |
| 622 | DCHECK_LT(i, environment_size); |
| 623 | } |
| 624 | break; |
| 625 | } |
| 626 | |
Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 627 | case Location::kFpuRegisterPair : { |
| 628 | stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInFpuRegister, location.low()); |
| 629 | stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInFpuRegister, location.high()); |
| 630 | ++i; |
| 631 | DCHECK_LT(i, environment_size); |
| 632 | break; |
| 633 | } |
| 634 | |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 635 | default: |
| 636 | LOG(FATAL) << "Unexpected kind " << location.GetKind(); |
| 637 | } |
| 638 | } |
| 639 | } |
| 640 | |
Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 641 | void CodeGenerator::SaveLiveRegisters(LocationSummary* locations) { |
| 642 | RegisterSet* register_set = locations->GetLiveRegisters(); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 643 | size_t stack_offset = first_register_slot_in_slow_path_; |
Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 644 | for (size_t i = 0, e = GetNumberOfCoreRegisters(); i < e; ++i) { |
| 645 | if (register_set->ContainsCoreRegister(i)) { |
Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 646 | // If the register holds an object, update the stack mask. |
| 647 | if (locations->RegisterContainsObject(i)) { |
| 648 | locations->SetStackBit(stack_offset / kVRegSize); |
| 649 | } |
Nicolas Geoffray | 87d0376 | 2014-11-19 15:17:56 +0000 | [diff] [blame] | 650 | DCHECK_LT(stack_offset, GetFrameSize() - FrameEntrySpillSize()); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 651 | stack_offset += SaveCoreRegister(stack_offset, i); |
Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 652 | } |
| 653 | } |
| 654 | |
| 655 | for (size_t i = 0, e = GetNumberOfFloatingPointRegisters(); i < e; ++i) { |
| 656 | if (register_set->ContainsFloatingPointRegister(i)) { |
Nicolas Geoffray | 87d0376 | 2014-11-19 15:17:56 +0000 | [diff] [blame] | 657 | DCHECK_LT(stack_offset, GetFrameSize() - FrameEntrySpillSize()); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 658 | stack_offset += SaveFloatingPointRegister(stack_offset, i); |
Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 659 | } |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | void CodeGenerator::RestoreLiveRegisters(LocationSummary* locations) { |
| 664 | RegisterSet* register_set = locations->GetLiveRegisters(); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 665 | size_t stack_offset = first_register_slot_in_slow_path_; |
Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 666 | for (size_t i = 0, e = GetNumberOfCoreRegisters(); i < e; ++i) { |
| 667 | if (register_set->ContainsCoreRegister(i)) { |
Nicolas Geoffray | 87d0376 | 2014-11-19 15:17:56 +0000 | [diff] [blame] | 668 | DCHECK_LT(stack_offset, GetFrameSize() - FrameEntrySpillSize()); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 669 | stack_offset += RestoreCoreRegister(stack_offset, i); |
Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 670 | } |
| 671 | } |
| 672 | |
| 673 | for (size_t i = 0, e = GetNumberOfFloatingPointRegisters(); i < e; ++i) { |
| 674 | if (register_set->ContainsFloatingPointRegister(i)) { |
Nicolas Geoffray | 87d0376 | 2014-11-19 15:17:56 +0000 | [diff] [blame] | 675 | DCHECK_LT(stack_offset, GetFrameSize() - FrameEntrySpillSize()); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 676 | stack_offset += RestoreFloatingPointRegister(stack_offset, i); |
Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 677 | } |
| 678 | } |
| 679 | } |
| 680 | |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 681 | void CodeGenerator::ClearSpillSlotsFromLoopPhisInStackMap(HSuspendCheck* suspend_check) const { |
| 682 | LocationSummary* locations = suspend_check->GetLocations(); |
| 683 | HBasicBlock* block = suspend_check->GetBlock(); |
| 684 | DCHECK(block->GetLoopInformation()->GetSuspendCheck() == suspend_check); |
| 685 | DCHECK(block->IsLoopHeader()); |
| 686 | |
| 687 | for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) { |
| 688 | HInstruction* current = it.Current(); |
| 689 | LiveInterval* interval = current->GetLiveInterval(); |
| 690 | // We only need to clear bits of loop phis containing objects and allocated in register. |
| 691 | // Loop phis allocated on stack already have the object in the stack. |
| 692 | if (current->GetType() == Primitive::kPrimNot |
| 693 | && interval->HasRegister() |
| 694 | && interval->HasSpillSlot()) { |
| 695 | locations->ClearStackBit(interval->GetSpillSlot() / kVRegSize); |
| 696 | } |
| 697 | } |
| 698 | } |
| 699 | |
Nicolas Geoffray | f0e3937 | 2014-11-12 17:50:07 +0000 | [diff] [blame] | 700 | void CodeGenerator::EmitParallelMoves(Location from1, Location to1, Location from2, Location to2) { |
Nicolas Geoffray | f0e3937 | 2014-11-12 17:50:07 +0000 | [diff] [blame] | 701 | HParallelMove parallel_move(GetGraph()->GetArena()); |
Nicolas Geoffray | 42d1f5f | 2015-01-16 09:14:18 +0000 | [diff] [blame] | 702 | parallel_move.AddMove(from1, to1, nullptr); |
| 703 | parallel_move.AddMove(from2, to2, nullptr); |
Nicolas Geoffray | f0e3937 | 2014-11-12 17:50:07 +0000 | [diff] [blame] | 704 | GetMoveResolver()->EmitNativeCode(¶llel_move); |
| 705 | } |
| 706 | |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 707 | } // namespace art |