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 | |
Alex Light | 50fa993 | 2015-08-10 15:30:07 -0700 | [diff] [blame] | 19 | #ifdef ART_ENABLE_CODEGEN_arm |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 20 | #include "code_generator_arm.h" |
Alex Light | 50fa993 | 2015-08-10 15:30:07 -0700 | [diff] [blame] | 21 | #endif |
| 22 | |
| 23 | #ifdef ART_ENABLE_CODEGEN_arm64 |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 24 | #include "code_generator_arm64.h" |
Alex Light | 50fa993 | 2015-08-10 15:30:07 -0700 | [diff] [blame] | 25 | #endif |
| 26 | |
| 27 | #ifdef ART_ENABLE_CODEGEN_x86 |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 28 | #include "code_generator_x86.h" |
Alex Light | 50fa993 | 2015-08-10 15:30:07 -0700 | [diff] [blame] | 29 | #endif |
| 30 | |
| 31 | #ifdef ART_ENABLE_CODEGEN_x86_64 |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 32 | #include "code_generator_x86_64.h" |
Alex Light | 50fa993 | 2015-08-10 15:30:07 -0700 | [diff] [blame] | 33 | #endif |
| 34 | |
Goran Jakovljevic | f652cec | 2015-08-25 16:11:42 +0200 | [diff] [blame] | 35 | #ifdef ART_ENABLE_CODEGEN_mips |
| 36 | #include "code_generator_mips.h" |
| 37 | #endif |
| 38 | |
Alex Light | 50fa993 | 2015-08-10 15:30:07 -0700 | [diff] [blame] | 39 | #ifdef ART_ENABLE_CODEGEN_mips64 |
Alexey Frunze | 4dda337 | 2015-06-01 18:31:49 -0700 | [diff] [blame] | 40 | #include "code_generator_mips64.h" |
Alex Light | 50fa993 | 2015-08-10 15:30:07 -0700 | [diff] [blame] | 41 | #endif |
| 42 | |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 43 | #include "compiled_method.h" |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 44 | #include "dex/verified_method.h" |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 45 | #include "driver/compiler_driver.h" |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 46 | #include "gc_map_builder.h" |
Alexandre Rames | eb7b739 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 47 | #include "graph_visualizer.h" |
Nicolas Geoffray | 5bd05a5 | 2015-10-13 09:48:30 +0100 | [diff] [blame] | 48 | #include "intrinsics.h" |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 49 | #include "leb128.h" |
| 50 | #include "mapping_table.h" |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 51 | #include "mirror/array-inl.h" |
| 52 | #include "mirror/object_array-inl.h" |
| 53 | #include "mirror/object_reference.h" |
Alex Light | 50fa993 | 2015-08-10 15:30:07 -0700 | [diff] [blame] | 54 | #include "parallel_move_resolver.h" |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 55 | #include "ssa_liveness_analysis.h" |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 56 | #include "utils/assembler.h" |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 57 | #include "verifier/dex_gc_map.h" |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 58 | #include "vmap_table.h" |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 59 | |
| 60 | namespace art { |
| 61 | |
Alexandre Rames | 88c13cd | 2015-04-14 17:35:39 +0100 | [diff] [blame] | 62 | // Return whether a location is consistent with a type. |
| 63 | static bool CheckType(Primitive::Type type, Location location) { |
| 64 | if (location.IsFpuRegister() |
| 65 | || (location.IsUnallocated() && (location.GetPolicy() == Location::kRequiresFpuRegister))) { |
| 66 | return (type == Primitive::kPrimFloat) || (type == Primitive::kPrimDouble); |
| 67 | } else if (location.IsRegister() || |
| 68 | (location.IsUnallocated() && (location.GetPolicy() == Location::kRequiresRegister))) { |
| 69 | return Primitive::IsIntegralType(type) || (type == Primitive::kPrimNot); |
| 70 | } else if (location.IsRegisterPair()) { |
| 71 | return type == Primitive::kPrimLong; |
| 72 | } else if (location.IsFpuRegisterPair()) { |
| 73 | return type == Primitive::kPrimDouble; |
| 74 | } else if (location.IsStackSlot()) { |
| 75 | return (Primitive::IsIntegralType(type) && type != Primitive::kPrimLong) |
| 76 | || (type == Primitive::kPrimFloat) |
| 77 | || (type == Primitive::kPrimNot); |
| 78 | } else if (location.IsDoubleStackSlot()) { |
| 79 | return (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble); |
| 80 | } else if (location.IsConstant()) { |
| 81 | if (location.GetConstant()->IsIntConstant()) { |
| 82 | return Primitive::IsIntegralType(type) && (type != Primitive::kPrimLong); |
| 83 | } else if (location.GetConstant()->IsNullConstant()) { |
| 84 | return type == Primitive::kPrimNot; |
| 85 | } else if (location.GetConstant()->IsLongConstant()) { |
| 86 | return type == Primitive::kPrimLong; |
| 87 | } else if (location.GetConstant()->IsFloatConstant()) { |
| 88 | return type == Primitive::kPrimFloat; |
| 89 | } else { |
| 90 | return location.GetConstant()->IsDoubleConstant() |
| 91 | && (type == Primitive::kPrimDouble); |
| 92 | } |
| 93 | } else { |
| 94 | return location.IsInvalid() || (location.GetPolicy() == Location::kAny); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | // Check that a location summary is consistent with an instruction. |
| 99 | static bool CheckTypeConsistency(HInstruction* instruction) { |
| 100 | LocationSummary* locations = instruction->GetLocations(); |
| 101 | if (locations == nullptr) { |
| 102 | return true; |
| 103 | } |
| 104 | |
| 105 | if (locations->Out().IsUnallocated() |
| 106 | && (locations->Out().GetPolicy() == Location::kSameAsFirstInput)) { |
| 107 | DCHECK(CheckType(instruction->GetType(), locations->InAt(0))) |
| 108 | << instruction->GetType() |
| 109 | << " " << locations->InAt(0); |
| 110 | } else { |
| 111 | DCHECK(CheckType(instruction->GetType(), locations->Out())) |
| 112 | << instruction->GetType() |
| 113 | << " " << locations->Out(); |
| 114 | } |
| 115 | |
| 116 | for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) { |
| 117 | DCHECK(CheckType(instruction->InputAt(i)->GetType(), locations->InAt(i))) |
| 118 | << instruction->InputAt(i)->GetType() |
| 119 | << " " << locations->InAt(i); |
| 120 | } |
| 121 | |
| 122 | HEnvironment* environment = instruction->GetEnvironment(); |
| 123 | for (size_t i = 0; i < instruction->EnvironmentSize(); ++i) { |
| 124 | if (environment->GetInstructionAt(i) != nullptr) { |
| 125 | Primitive::Type type = environment->GetInstructionAt(i)->GetType(); |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 126 | DCHECK(CheckType(type, environment->GetLocationAt(i))) |
| 127 | << type << " " << environment->GetLocationAt(i); |
Alexandre Rames | 88c13cd | 2015-04-14 17:35:39 +0100 | [diff] [blame] | 128 | } else { |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 129 | DCHECK(environment->GetLocationAt(i).IsInvalid()) |
| 130 | << environment->GetLocationAt(i); |
Alexandre Rames | 88c13cd | 2015-04-14 17:35:39 +0100 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | return true; |
| 134 | } |
| 135 | |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 136 | size_t CodeGenerator::GetCacheOffset(uint32_t index) { |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 137 | return sizeof(GcRoot<mirror::Object>) * index; |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 138 | } |
| 139 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 140 | size_t CodeGenerator::GetCachePointerOffset(uint32_t index) { |
| 141 | auto pointer_size = InstructionSetPointerSize(GetInstructionSet()); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 142 | return pointer_size * index; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Nicolas Geoffray | dc23d83 | 2015-02-16 11:15:43 +0000 | [diff] [blame] | 145 | bool CodeGenerator::GoesToNextBlock(HBasicBlock* current, HBasicBlock* next) const { |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 146 | DCHECK_EQ((*block_order_)[current_block_index_], current); |
Nicolas Geoffray | dc23d83 | 2015-02-16 11:15:43 +0000 | [diff] [blame] | 147 | return GetNextBlockToEmit() == FirstNonEmptyBlock(next); |
| 148 | } |
| 149 | |
| 150 | HBasicBlock* CodeGenerator::GetNextBlockToEmit() const { |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 151 | for (size_t i = current_block_index_ + 1; i < block_order_->size(); ++i) { |
| 152 | HBasicBlock* block = (*block_order_)[i]; |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 153 | if (!block->IsSingleJump()) { |
Nicolas Geoffray | dc23d83 | 2015-02-16 11:15:43 +0000 | [diff] [blame] | 154 | return block; |
| 155 | } |
| 156 | } |
| 157 | return nullptr; |
| 158 | } |
| 159 | |
| 160 | HBasicBlock* CodeGenerator::FirstNonEmptyBlock(HBasicBlock* block) const { |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 161 | while (block->IsSingleJump()) { |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 162 | block = block->GetSuccessors()[0]; |
Nicolas Geoffray | dc23d83 | 2015-02-16 11:15:43 +0000 | [diff] [blame] | 163 | } |
| 164 | return block; |
| 165 | } |
| 166 | |
Alexandre Rames | eb7b739 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 167 | class DisassemblyScope { |
| 168 | public: |
| 169 | DisassemblyScope(HInstruction* instruction, const CodeGenerator& codegen) |
| 170 | : codegen_(codegen), instruction_(instruction), start_offset_(static_cast<size_t>(-1)) { |
| 171 | if (codegen_.GetDisassemblyInformation() != nullptr) { |
| 172 | start_offset_ = codegen_.GetAssembler().CodeSize(); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | ~DisassemblyScope() { |
| 177 | // We avoid building this data when we know it will not be used. |
| 178 | if (codegen_.GetDisassemblyInformation() != nullptr) { |
| 179 | codegen_.GetDisassemblyInformation()->AddInstructionInterval( |
| 180 | instruction_, start_offset_, codegen_.GetAssembler().CodeSize()); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | private: |
| 185 | const CodeGenerator& codegen_; |
| 186 | HInstruction* instruction_; |
| 187 | size_t start_offset_; |
| 188 | }; |
| 189 | |
| 190 | |
| 191 | void CodeGenerator::GenerateSlowPaths() { |
| 192 | size_t code_start = 0; |
Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 193 | for (SlowPathCode* slow_path : slow_paths_) { |
Vladimir Marko | 0f7dca4 | 2015-11-02 14:36:43 +0000 | [diff] [blame] | 194 | current_slow_path_ = slow_path; |
Alexandre Rames | eb7b739 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 195 | if (disasm_info_ != nullptr) { |
| 196 | code_start = GetAssembler()->CodeSize(); |
| 197 | } |
David Srbecky | 9cd6d37 | 2016-02-09 15:24:47 +0000 | [diff] [blame] | 198 | // Record the dex pc at start of slow path (required for java line number mapping). |
David Srbecky | d28f4a0 | 2016-03-14 17:14:24 +0000 | [diff] [blame] | 199 | MaybeRecordNativeDebugInfo(slow_path->GetInstruction(), slow_path->GetDexPc(), slow_path); |
Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 200 | slow_path->EmitNativeCode(this); |
Alexandre Rames | eb7b739 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 201 | if (disasm_info_ != nullptr) { |
Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 202 | disasm_info_->AddSlowPathInterval(slow_path, code_start, GetAssembler()->CodeSize()); |
Alexandre Rames | eb7b739 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 203 | } |
| 204 | } |
Vladimir Marko | 0f7dca4 | 2015-11-02 14:36:43 +0000 | [diff] [blame] | 205 | current_slow_path_ = nullptr; |
Alexandre Rames | eb7b739 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 206 | } |
| 207 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 208 | void CodeGenerator::Compile(CodeAllocator* allocator) { |
| 209 | // The register allocator already called `InitializeCodeGeneration`, |
| 210 | // where the frame size has been computed. |
| 211 | DCHECK(block_order_ != nullptr); |
| 212 | Initialize(); |
| 213 | |
Nicolas Geoffray | 8a16d97 | 2014-09-11 10:30:02 +0100 | [diff] [blame] | 214 | HGraphVisitor* instruction_visitor = GetInstructionVisitor(); |
Nicolas Geoffray | 4c204ba | 2015-02-03 15:12:35 +0000 | [diff] [blame] | 215 | DCHECK_EQ(current_block_index_, 0u); |
Alexandre Rames | eb7b739 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 216 | |
| 217 | size_t frame_start = GetAssembler()->CodeSize(); |
Nicolas Geoffray | 4c204ba | 2015-02-03 15:12:35 +0000 | [diff] [blame] | 218 | GenerateFrameEntry(); |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 219 | DCHECK_EQ(GetAssembler()->cfi().GetCurrentCFAOffset(), static_cast<int>(frame_size_)); |
Alexandre Rames | eb7b739 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 220 | if (disasm_info_ != nullptr) { |
| 221 | disasm_info_->SetFrameEntryInterval(frame_start, GetAssembler()->CodeSize()); |
| 222 | } |
| 223 | |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 224 | for (size_t e = block_order_->size(); current_block_index_ < e; ++current_block_index_) { |
| 225 | HBasicBlock* block = (*block_order_)[current_block_index_]; |
Nicolas Geoffray | dc23d83 | 2015-02-16 11:15:43 +0000 | [diff] [blame] | 226 | // Don't generate code for an empty block. Its predecessors will branch to its successor |
| 227 | // directly. Also, the label of that block will not be emitted, so this helps catch |
| 228 | // errors where we reference that label. |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 229 | if (block->IsSingleJump()) continue; |
Nicolas Geoffray | 92a73ae | 2014-10-16 11:12:52 +0100 | [diff] [blame] | 230 | Bind(block); |
David Srbecky | c7098ff | 2016-02-09 14:30:11 +0000 | [diff] [blame] | 231 | // This ensures that we have correct native line mapping for all native instructions. |
| 232 | // It is necessary to make stepping over a statement work. Otherwise, any initial |
| 233 | // instructions (e.g. moves) would be assumed to be the start of next statement. |
| 234 | MaybeRecordNativeDebugInfo(nullptr /* instruction */, block->GetDexPc()); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 235 | for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) { |
| 236 | HInstruction* current = it.Current(); |
David Srbecky | d28f4a0 | 2016-03-14 17:14:24 +0000 | [diff] [blame] | 237 | if (current->HasEnvironment()) { |
| 238 | // Create stackmap for HNativeDebugInfo or any instruction which calls native code. |
| 239 | // Note that we need correct mapping for the native PC of the call instruction, |
| 240 | // so the runtime's stackmap is not sufficient since it is at PC after the call. |
| 241 | MaybeRecordNativeDebugInfo(current, block->GetDexPc()); |
| 242 | } |
Alexandre Rames | eb7b739 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 243 | DisassemblyScope disassembly_scope(current, *this); |
Alexandre Rames | 88c13cd | 2015-04-14 17:35:39 +0100 | [diff] [blame] | 244 | DCHECK(CheckTypeConsistency(current)); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 245 | current->Accept(instruction_visitor); |
| 246 | } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 247 | } |
Nicolas Geoffray | 4c204ba | 2015-02-03 15:12:35 +0000 | [diff] [blame] | 248 | |
Alexandre Rames | eb7b739 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 249 | GenerateSlowPaths(); |
Nicolas Geoffray | 4c204ba | 2015-02-03 15:12:35 +0000 | [diff] [blame] | 250 | |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 251 | // Emit catch stack maps at the end of the stack map stream as expected by the |
| 252 | // runtime exception handler. |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 253 | if (graph_->HasTryCatch()) { |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 254 | RecordCatchBlockInfo(); |
| 255 | } |
| 256 | |
Nicolas Geoffray | 4c204ba | 2015-02-03 15:12:35 +0000 | [diff] [blame] | 257 | // Finalize instructions in assember; |
Serban Constantinescu | 32f5b4d | 2014-11-25 20:05:46 +0000 | [diff] [blame] | 258 | Finalize(allocator); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Serban Constantinescu | 32f5b4d | 2014-11-25 20:05:46 +0000 | [diff] [blame] | 261 | void CodeGenerator::Finalize(CodeAllocator* allocator) { |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 262 | size_t code_size = GetAssembler()->CodeSize(); |
| 263 | uint8_t* buffer = allocator->Allocate(code_size); |
Serban Constantinescu | 32f5b4d | 2014-11-25 20:05:46 +0000 | [diff] [blame] | 264 | |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 265 | MemoryRegion code(buffer, code_size); |
| 266 | GetAssembler()->FinalizeInstructions(code); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 269 | void CodeGenerator::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches ATTRIBUTE_UNUSED) { |
| 270 | // No linker patches by default. |
| 271 | } |
| 272 | |
Nicolas Geoffray | 4c204ba | 2015-02-03 15:12:35 +0000 | [diff] [blame] | 273 | void CodeGenerator::InitializeCodeGeneration(size_t number_of_spill_slots, |
| 274 | size_t maximum_number_of_live_core_registers, |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 275 | size_t maximum_number_of_live_fpu_registers, |
Nicolas Geoffray | 4c204ba | 2015-02-03 15:12:35 +0000 | [diff] [blame] | 276 | size_t number_of_out_slots, |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 277 | const ArenaVector<HBasicBlock*>& block_order) { |
Nicolas Geoffray | 4c204ba | 2015-02-03 15:12:35 +0000 | [diff] [blame] | 278 | block_order_ = &block_order; |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 279 | DCHECK(!block_order.empty()); |
| 280 | DCHECK(block_order[0] == GetGraph()->GetEntryBlock()); |
Nicolas Geoffray | 4dee636 | 2015-01-23 18:23:14 +0000 | [diff] [blame] | 281 | ComputeSpillMask(); |
Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 282 | first_register_slot_in_slow_path_ = (number_of_out_slots + number_of_spill_slots) * kVRegSize; |
| 283 | |
Nicolas Geoffray | c0572a4 | 2015-02-06 14:35:25 +0000 | [diff] [blame] | 284 | if (number_of_spill_slots == 0 |
| 285 | && !HasAllocatedCalleeSaveRegisters() |
| 286 | && IsLeafMethod() |
| 287 | && !RequiresCurrentMethod()) { |
| 288 | DCHECK_EQ(maximum_number_of_live_core_registers, 0u); |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 289 | DCHECK_EQ(maximum_number_of_live_fpu_registers, 0u); |
Nicolas Geoffray | c0572a4 | 2015-02-06 14:35:25 +0000 | [diff] [blame] | 290 | SetFrameSize(CallPushesPC() ? GetWordSize() : 0); |
| 291 | } else { |
| 292 | SetFrameSize(RoundUp( |
| 293 | number_of_spill_slots * kVRegSize |
| 294 | + number_of_out_slots * kVRegSize |
| 295 | + maximum_number_of_live_core_registers * GetWordSize() |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 296 | + maximum_number_of_live_fpu_registers * GetFloatingPointSpillSlotSize() |
Nicolas Geoffray | c0572a4 | 2015-02-06 14:35:25 +0000 | [diff] [blame] | 297 | + FrameEntrySpillSize(), |
| 298 | kStackAlignment)); |
| 299 | } |
Nicolas Geoffray | ab032bc | 2014-07-15 12:55:21 +0100 | [diff] [blame] | 300 | } |
| 301 | |
Nicolas Geoffray | ab032bc | 2014-07-15 12:55:21 +0100 | [diff] [blame] | 302 | int32_t CodeGenerator::GetStackSlot(HLocal* local) const { |
| 303 | uint16_t reg_number = local->GetRegNumber(); |
| 304 | uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs(); |
| 305 | if (reg_number >= number_of_locals) { |
| 306 | // Local is a parameter of the method. It is stored in the caller's frame. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 307 | // TODO: Share this logic with StackVisitor::GetVRegOffsetFromQuickCode. |
| 308 | return GetFrameSize() + InstructionSetPointerSize(GetInstructionSet()) // ART method |
Nicolas Geoffray | ab032bc | 2014-07-15 12:55:21 +0100 | [diff] [blame] | 309 | + (reg_number - number_of_locals) * kVRegSize; |
| 310 | } else { |
| 311 | // Local is a temporary in this method. It is stored in this method's frame. |
| 312 | return GetFrameSize() - FrameEntrySpillSize() |
| 313 | - kVRegSize // filler. |
| 314 | - (number_of_locals * kVRegSize) |
| 315 | + (reg_number * kVRegSize); |
| 316 | } |
| 317 | } |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 318 | |
Nicolas Geoffray | fd88f16 | 2015-06-03 11:23:52 +0100 | [diff] [blame] | 319 | void CodeGenerator::CreateCommonInvokeLocationSummary( |
Nicolas Geoffray | 4e40c26 | 2015-06-03 12:02:38 +0100 | [diff] [blame] | 320 | HInvoke* invoke, InvokeDexCallingConventionVisitor* visitor) { |
Nicolas Geoffray | fd88f16 | 2015-06-03 11:23:52 +0100 | [diff] [blame] | 321 | ArenaAllocator* allocator = invoke->GetBlock()->GetGraph()->GetArena(); |
| 322 | LocationSummary* locations = new (allocator) LocationSummary(invoke, LocationSummary::kCall); |
Nicolas Geoffray | fd88f16 | 2015-06-03 11:23:52 +0100 | [diff] [blame] | 323 | |
| 324 | for (size_t i = 0; i < invoke->GetNumberOfArguments(); i++) { |
| 325 | HInstruction* input = invoke->InputAt(i); |
| 326 | locations->SetInAt(i, visitor->GetNextLocation(input->GetType())); |
| 327 | } |
| 328 | |
| 329 | locations->SetOut(visitor->GetReturnLocation(invoke->GetType())); |
Nicolas Geoffray | 94015b9 | 2015-06-04 18:21:04 +0100 | [diff] [blame] | 330 | |
| 331 | if (invoke->IsInvokeStaticOrDirect()) { |
| 332 | HInvokeStaticOrDirect* call = invoke->AsInvokeStaticOrDirect(); |
Vladimir Marko | dc151b2 | 2015-10-15 18:02:30 +0100 | [diff] [blame] | 333 | switch (call->GetMethodLoadKind()) { |
| 334 | case HInvokeStaticOrDirect::MethodLoadKind::kRecursive: |
Vladimir Marko | c53c079 | 2015-11-19 15:48:33 +0000 | [diff] [blame] | 335 | locations->SetInAt(call->GetSpecialInputIndex(), visitor->GetMethodLocation()); |
Vladimir Marko | dc151b2 | 2015-10-15 18:02:30 +0100 | [diff] [blame] | 336 | break; |
| 337 | case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: |
| 338 | locations->AddTemp(visitor->GetMethodLocation()); |
Vladimir Marko | c53c079 | 2015-11-19 15:48:33 +0000 | [diff] [blame] | 339 | locations->SetInAt(call->GetSpecialInputIndex(), Location::RequiresRegister()); |
Vladimir Marko | dc151b2 | 2015-10-15 18:02:30 +0100 | [diff] [blame] | 340 | break; |
| 341 | default: |
| 342 | locations->AddTemp(visitor->GetMethodLocation()); |
| 343 | break; |
Nicolas Geoffray | 94015b9 | 2015-06-04 18:21:04 +0100 | [diff] [blame] | 344 | } |
| 345 | } else { |
| 346 | locations->AddTemp(visitor->GetMethodLocation()); |
| 347 | } |
Nicolas Geoffray | fd88f16 | 2015-06-03 11:23:52 +0100 | [diff] [blame] | 348 | } |
| 349 | |
Calin Juravle | 175dc73 | 2015-08-25 15:42:32 +0100 | [diff] [blame] | 350 | void CodeGenerator::GenerateInvokeUnresolvedRuntimeCall(HInvokeUnresolved* invoke) { |
| 351 | MoveConstant(invoke->GetLocations()->GetTemp(0), invoke->GetDexMethodIndex()); |
| 352 | |
| 353 | // Initialize to anything to silent compiler warnings. |
| 354 | QuickEntrypointEnum entrypoint = kQuickInvokeStaticTrampolineWithAccessCheck; |
| 355 | switch (invoke->GetOriginalInvokeType()) { |
| 356 | case kStatic: |
| 357 | entrypoint = kQuickInvokeStaticTrampolineWithAccessCheck; |
| 358 | break; |
| 359 | case kDirect: |
| 360 | entrypoint = kQuickInvokeDirectTrampolineWithAccessCheck; |
| 361 | break; |
| 362 | case kVirtual: |
| 363 | entrypoint = kQuickInvokeVirtualTrampolineWithAccessCheck; |
| 364 | break; |
| 365 | case kSuper: |
| 366 | entrypoint = kQuickInvokeSuperTrampolineWithAccessCheck; |
| 367 | break; |
| 368 | case kInterface: |
| 369 | entrypoint = kQuickInvokeInterfaceTrampolineWithAccessCheck; |
| 370 | break; |
| 371 | } |
| 372 | InvokeRuntime(entrypoint, invoke, invoke->GetDexPc(), nullptr); |
| 373 | } |
| 374 | |
Calin Juravle | e460d1d | 2015-09-29 04:52:17 +0100 | [diff] [blame] | 375 | void CodeGenerator::CreateUnresolvedFieldLocationSummary( |
| 376 | HInstruction* field_access, |
| 377 | Primitive::Type field_type, |
| 378 | const FieldAccessCallingConvention& calling_convention) { |
| 379 | bool is_instance = field_access->IsUnresolvedInstanceFieldGet() |
| 380 | || field_access->IsUnresolvedInstanceFieldSet(); |
| 381 | bool is_get = field_access->IsUnresolvedInstanceFieldGet() |
| 382 | || field_access->IsUnresolvedStaticFieldGet(); |
| 383 | |
| 384 | ArenaAllocator* allocator = field_access->GetBlock()->GetGraph()->GetArena(); |
| 385 | LocationSummary* locations = |
| 386 | new (allocator) LocationSummary(field_access, LocationSummary::kCall); |
| 387 | |
| 388 | locations->AddTemp(calling_convention.GetFieldIndexLocation()); |
| 389 | |
| 390 | if (is_instance) { |
| 391 | // Add the `this` object for instance field accesses. |
| 392 | locations->SetInAt(0, calling_convention.GetObjectLocation()); |
| 393 | } |
| 394 | |
| 395 | // Note that pSetXXStatic/pGetXXStatic always takes/returns an int or int64 |
| 396 | // regardless of the the type. Because of that we forced to special case |
| 397 | // the access to floating point values. |
| 398 | if (is_get) { |
| 399 | if (Primitive::IsFloatingPointType(field_type)) { |
| 400 | // The return value will be stored in regular registers while register |
| 401 | // allocator expects it in a floating point register. |
| 402 | // Note We don't need to request additional temps because the return |
| 403 | // register(s) are already blocked due the call and they may overlap with |
| 404 | // the input or field index. |
| 405 | // The transfer between the two will be done at codegen level. |
| 406 | locations->SetOut(calling_convention.GetFpuLocation(field_type)); |
| 407 | } else { |
| 408 | locations->SetOut(calling_convention.GetReturnLocation(field_type)); |
| 409 | } |
| 410 | } else { |
| 411 | size_t set_index = is_instance ? 1 : 0; |
| 412 | if (Primitive::IsFloatingPointType(field_type)) { |
| 413 | // The set value comes from a float location while the calling convention |
| 414 | // expects it in a regular register location. Allocate a temp for it and |
| 415 | // make the transfer at codegen. |
| 416 | AddLocationAsTemp(calling_convention.GetSetValueLocation(field_type, is_instance), locations); |
| 417 | locations->SetInAt(set_index, calling_convention.GetFpuLocation(field_type)); |
| 418 | } else { |
| 419 | locations->SetInAt(set_index, |
| 420 | calling_convention.GetSetValueLocation(field_type, is_instance)); |
| 421 | } |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | void CodeGenerator::GenerateUnresolvedFieldAccess( |
| 426 | HInstruction* field_access, |
| 427 | Primitive::Type field_type, |
| 428 | uint32_t field_index, |
| 429 | uint32_t dex_pc, |
| 430 | const FieldAccessCallingConvention& calling_convention) { |
| 431 | LocationSummary* locations = field_access->GetLocations(); |
| 432 | |
| 433 | MoveConstant(locations->GetTemp(0), field_index); |
| 434 | |
| 435 | bool is_instance = field_access->IsUnresolvedInstanceFieldGet() |
| 436 | || field_access->IsUnresolvedInstanceFieldSet(); |
| 437 | bool is_get = field_access->IsUnresolvedInstanceFieldGet() |
| 438 | || field_access->IsUnresolvedStaticFieldGet(); |
| 439 | |
| 440 | if (!is_get && Primitive::IsFloatingPointType(field_type)) { |
| 441 | // Copy the float value to be set into the calling convention register. |
| 442 | // Note that using directly the temp location is problematic as we don't |
| 443 | // support temp register pairs. To avoid boilerplate conversion code, use |
| 444 | // the location from the calling convention. |
| 445 | MoveLocation(calling_convention.GetSetValueLocation(field_type, is_instance), |
| 446 | locations->InAt(is_instance ? 1 : 0), |
| 447 | (Primitive::Is64BitType(field_type) ? Primitive::kPrimLong : Primitive::kPrimInt)); |
| 448 | } |
| 449 | |
| 450 | QuickEntrypointEnum entrypoint = kQuickSet8Static; // Initialize to anything to avoid warnings. |
| 451 | switch (field_type) { |
| 452 | case Primitive::kPrimBoolean: |
| 453 | entrypoint = is_instance |
| 454 | ? (is_get ? kQuickGetBooleanInstance : kQuickSet8Instance) |
| 455 | : (is_get ? kQuickGetBooleanStatic : kQuickSet8Static); |
| 456 | break; |
| 457 | case Primitive::kPrimByte: |
| 458 | entrypoint = is_instance |
| 459 | ? (is_get ? kQuickGetByteInstance : kQuickSet8Instance) |
| 460 | : (is_get ? kQuickGetByteStatic : kQuickSet8Static); |
| 461 | break; |
| 462 | case Primitive::kPrimShort: |
| 463 | entrypoint = is_instance |
| 464 | ? (is_get ? kQuickGetShortInstance : kQuickSet16Instance) |
| 465 | : (is_get ? kQuickGetShortStatic : kQuickSet16Static); |
| 466 | break; |
| 467 | case Primitive::kPrimChar: |
| 468 | entrypoint = is_instance |
| 469 | ? (is_get ? kQuickGetCharInstance : kQuickSet16Instance) |
| 470 | : (is_get ? kQuickGetCharStatic : kQuickSet16Static); |
| 471 | break; |
| 472 | case Primitive::kPrimInt: |
| 473 | case Primitive::kPrimFloat: |
| 474 | entrypoint = is_instance |
| 475 | ? (is_get ? kQuickGet32Instance : kQuickSet32Instance) |
| 476 | : (is_get ? kQuickGet32Static : kQuickSet32Static); |
| 477 | break; |
| 478 | case Primitive::kPrimNot: |
| 479 | entrypoint = is_instance |
| 480 | ? (is_get ? kQuickGetObjInstance : kQuickSetObjInstance) |
| 481 | : (is_get ? kQuickGetObjStatic : kQuickSetObjStatic); |
| 482 | break; |
| 483 | case Primitive::kPrimLong: |
| 484 | case Primitive::kPrimDouble: |
| 485 | entrypoint = is_instance |
| 486 | ? (is_get ? kQuickGet64Instance : kQuickSet64Instance) |
| 487 | : (is_get ? kQuickGet64Static : kQuickSet64Static); |
| 488 | break; |
| 489 | default: |
| 490 | LOG(FATAL) << "Invalid type " << field_type; |
| 491 | } |
| 492 | InvokeRuntime(entrypoint, field_access, dex_pc, nullptr); |
| 493 | |
| 494 | if (is_get && Primitive::IsFloatingPointType(field_type)) { |
| 495 | MoveLocation(locations->Out(), calling_convention.GetReturnLocation(field_type), field_type); |
| 496 | } |
| 497 | } |
| 498 | |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 499 | // TODO: Remove argument `code_generator_supports_read_barrier` when |
| 500 | // all code generators have read barrier support. |
Calin Juravle | 98893e1 | 2015-10-02 21:05:03 +0100 | [diff] [blame] | 501 | void CodeGenerator::CreateLoadClassLocationSummary(HLoadClass* cls, |
| 502 | Location runtime_type_index_location, |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 503 | Location runtime_return_location, |
| 504 | bool code_generator_supports_read_barrier) { |
Calin Juravle | 98893e1 | 2015-10-02 21:05:03 +0100 | [diff] [blame] | 505 | ArenaAllocator* allocator = cls->GetBlock()->GetGraph()->GetArena(); |
| 506 | LocationSummary::CallKind call_kind = cls->NeedsAccessCheck() |
| 507 | ? LocationSummary::kCall |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 508 | : (((code_generator_supports_read_barrier && kEmitCompilerReadBarrier) || |
| 509 | cls->CanCallRuntime()) |
| 510 | ? LocationSummary::kCallOnSlowPath |
| 511 | : LocationSummary::kNoCall); |
Calin Juravle | 98893e1 | 2015-10-02 21:05:03 +0100 | [diff] [blame] | 512 | LocationSummary* locations = new (allocator) LocationSummary(cls, call_kind); |
Calin Juravle | 98893e1 | 2015-10-02 21:05:03 +0100 | [diff] [blame] | 513 | if (cls->NeedsAccessCheck()) { |
Calin Juravle | 580b609 | 2015-10-06 17:35:58 +0100 | [diff] [blame] | 514 | locations->SetInAt(0, Location::NoLocation()); |
Calin Juravle | 98893e1 | 2015-10-02 21:05:03 +0100 | [diff] [blame] | 515 | locations->AddTemp(runtime_type_index_location); |
| 516 | locations->SetOut(runtime_return_location); |
| 517 | } else { |
Calin Juravle | 580b609 | 2015-10-06 17:35:58 +0100 | [diff] [blame] | 518 | locations->SetInAt(0, Location::RequiresRegister()); |
Calin Juravle | 98893e1 | 2015-10-02 21:05:03 +0100 | [diff] [blame] | 519 | locations->SetOut(Location::RequiresRegister()); |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | |
Mark Mendell | 5f87418 | 2015-03-04 15:42:45 -0500 | [diff] [blame] | 524 | void CodeGenerator::BlockIfInRegister(Location location, bool is_out) const { |
| 525 | // The DCHECKS below check that a register is not specified twice in |
| 526 | // the summary. The out location can overlap with an input, so we need |
| 527 | // to special case it. |
| 528 | if (location.IsRegister()) { |
| 529 | DCHECK(is_out || !blocked_core_registers_[location.reg()]); |
| 530 | blocked_core_registers_[location.reg()] = true; |
| 531 | } else if (location.IsFpuRegister()) { |
| 532 | DCHECK(is_out || !blocked_fpu_registers_[location.reg()]); |
| 533 | blocked_fpu_registers_[location.reg()] = true; |
| 534 | } else if (location.IsFpuRegisterPair()) { |
| 535 | DCHECK(is_out || !blocked_fpu_registers_[location.AsFpuRegisterPairLow<int>()]); |
| 536 | blocked_fpu_registers_[location.AsFpuRegisterPairLow<int>()] = true; |
| 537 | DCHECK(is_out || !blocked_fpu_registers_[location.AsFpuRegisterPairHigh<int>()]); |
| 538 | blocked_fpu_registers_[location.AsFpuRegisterPairHigh<int>()] = true; |
| 539 | } else if (location.IsRegisterPair()) { |
| 540 | DCHECK(is_out || !blocked_core_registers_[location.AsRegisterPairLow<int>()]); |
| 541 | blocked_core_registers_[location.AsRegisterPairLow<int>()] = true; |
| 542 | DCHECK(is_out || !blocked_core_registers_[location.AsRegisterPairHigh<int>()]); |
| 543 | blocked_core_registers_[location.AsRegisterPairHigh<int>()] = true; |
| 544 | } |
| 545 | } |
| 546 | |
Nicolas Geoffray | c0572a4 | 2015-02-06 14:35:25 +0000 | [diff] [blame] | 547 | void CodeGenerator::AllocateLocations(HInstruction* instruction) { |
| 548 | instruction->Accept(GetLocationBuilder()); |
Alexandre Rames | 88c13cd | 2015-04-14 17:35:39 +0100 | [diff] [blame] | 549 | DCHECK(CheckTypeConsistency(instruction)); |
Nicolas Geoffray | c0572a4 | 2015-02-06 14:35:25 +0000 | [diff] [blame] | 550 | LocationSummary* locations = instruction->GetLocations(); |
| 551 | if (!instruction->IsSuspendCheckEntry()) { |
Aart Bik | d1c4045 | 2016-03-02 16:06:13 -0800 | [diff] [blame] | 552 | if (locations != nullptr) { |
| 553 | if (locations->CanCall()) { |
| 554 | MarkNotLeaf(); |
| 555 | } else if (locations->Intrinsified() && |
| 556 | instruction->IsInvokeStaticOrDirect() && |
| 557 | !instruction->AsInvokeStaticOrDirect()->HasCurrentMethodInput()) { |
| 558 | // A static method call that has been fully intrinsified, and cannot call on the slow |
| 559 | // path or refer to the current method directly, no longer needs current method. |
| 560 | return; |
| 561 | } |
Nicolas Geoffray | c0572a4 | 2015-02-06 14:35:25 +0000 | [diff] [blame] | 562 | } |
| 563 | if (instruction->NeedsCurrentMethod()) { |
| 564 | SetRequiresCurrentMethod(); |
| 565 | } |
| 566 | } |
| 567 | } |
| 568 | |
Serban Constantinescu | ecc4366 | 2015-08-13 13:33:12 +0100 | [diff] [blame] | 569 | void CodeGenerator::MaybeRecordStat(MethodCompilationStat compilation_stat, size_t count) const { |
| 570 | if (stats_ != nullptr) { |
| 571 | stats_->RecordStat(compilation_stat, count); |
| 572 | } |
| 573 | } |
| 574 | |
Nicolas Geoffray | 12df9eb | 2015-01-09 14:53:50 +0000 | [diff] [blame] | 575 | CodeGenerator* CodeGenerator::Create(HGraph* graph, |
Calin Juravle | 3416601 | 2014-12-19 17:22:29 +0000 | [diff] [blame] | 576 | InstructionSet instruction_set, |
Calin Juravle | cd6dffe | 2015-01-08 17:35:35 +0000 | [diff] [blame] | 577 | const InstructionSetFeatures& isa_features, |
Serban Constantinescu | ecc4366 | 2015-08-13 13:33:12 +0100 | [diff] [blame] | 578 | const CompilerOptions& compiler_options, |
| 579 | OptimizingCompilerStats* stats) { |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 580 | switch (instruction_set) { |
Alex Light | 50fa993 | 2015-08-10 15:30:07 -0700 | [diff] [blame] | 581 | #ifdef ART_ENABLE_CODEGEN_arm |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 582 | case kArm: |
| 583 | case kThumb2: { |
Nicolas Geoffray | 12df9eb | 2015-01-09 14:53:50 +0000 | [diff] [blame] | 584 | return new arm::CodeGeneratorARM(graph, |
Serban Constantinescu | ecc4366 | 2015-08-13 13:33:12 +0100 | [diff] [blame] | 585 | *isa_features.AsArmInstructionSetFeatures(), |
| 586 | compiler_options, |
| 587 | stats); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 588 | } |
Alex Light | 50fa993 | 2015-08-10 15:30:07 -0700 | [diff] [blame] | 589 | #endif |
| 590 | #ifdef ART_ENABLE_CODEGEN_arm64 |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 591 | case kArm64: { |
Serban Constantinescu | 579885a | 2015-02-22 20:51:33 +0000 | [diff] [blame] | 592 | return new arm64::CodeGeneratorARM64(graph, |
Serban Constantinescu | ecc4366 | 2015-08-13 13:33:12 +0100 | [diff] [blame] | 593 | *isa_features.AsArm64InstructionSetFeatures(), |
| 594 | compiler_options, |
| 595 | stats); |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 596 | } |
Alex Light | 50fa993 | 2015-08-10 15:30:07 -0700 | [diff] [blame] | 597 | #endif |
| 598 | #ifdef ART_ENABLE_CODEGEN_mips |
Goran Jakovljevic | f652cec | 2015-08-25 16:11:42 +0200 | [diff] [blame] | 599 | case kMips: { |
| 600 | return new mips::CodeGeneratorMIPS(graph, |
| 601 | *isa_features.AsMipsInstructionSetFeatures(), |
| 602 | compiler_options, |
| 603 | stats); |
| 604 | } |
Alex Light | 50fa993 | 2015-08-10 15:30:07 -0700 | [diff] [blame] | 605 | #endif |
| 606 | #ifdef ART_ENABLE_CODEGEN_mips64 |
Alexey Frunze | 4dda337 | 2015-06-01 18:31:49 -0700 | [diff] [blame] | 607 | case kMips64: { |
| 608 | return new mips64::CodeGeneratorMIPS64(graph, |
Serban Constantinescu | ecc4366 | 2015-08-13 13:33:12 +0100 | [diff] [blame] | 609 | *isa_features.AsMips64InstructionSetFeatures(), |
| 610 | compiler_options, |
| 611 | stats); |
Alexey Frunze | 4dda337 | 2015-06-01 18:31:49 -0700 | [diff] [blame] | 612 | } |
Alex Light | 50fa993 | 2015-08-10 15:30:07 -0700 | [diff] [blame] | 613 | #endif |
| 614 | #ifdef ART_ENABLE_CODEGEN_x86 |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 615 | case kX86: { |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 616 | return new x86::CodeGeneratorX86(graph, |
Serban Constantinescu | ecc4366 | 2015-08-13 13:33:12 +0100 | [diff] [blame] | 617 | *isa_features.AsX86InstructionSetFeatures(), |
| 618 | compiler_options, |
| 619 | stats); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 620 | } |
Alex Light | 50fa993 | 2015-08-10 15:30:07 -0700 | [diff] [blame] | 621 | #endif |
| 622 | #ifdef ART_ENABLE_CODEGEN_x86_64 |
Dmitry Petrochenko | 6a58cb1 | 2014-04-02 17:27:59 +0700 | [diff] [blame] | 623 | case kX86_64: { |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 624 | return new x86_64::CodeGeneratorX86_64(graph, |
Serban Constantinescu | ecc4366 | 2015-08-13 13:33:12 +0100 | [diff] [blame] | 625 | *isa_features.AsX86_64InstructionSetFeatures(), |
| 626 | compiler_options, |
| 627 | stats); |
Dmitry Petrochenko | 6a58cb1 | 2014-04-02 17:27:59 +0700 | [diff] [blame] | 628 | } |
Alex Light | 50fa993 | 2015-08-10 15:30:07 -0700 | [diff] [blame] | 629 | #endif |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 630 | default: |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 631 | return nullptr; |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 632 | } |
| 633 | } |
| 634 | |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 635 | size_t CodeGenerator::ComputeStackMapsSize() { |
| 636 | return stack_map_stream_.PrepareForFillIn(); |
| 637 | } |
| 638 | |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 639 | static void CheckCovers(uint32_t dex_pc, |
| 640 | const HGraph& graph, |
| 641 | const CodeInfo& code_info, |
| 642 | const ArenaVector<HSuspendCheck*>& loop_headers, |
| 643 | ArenaVector<size_t>* covered) { |
| 644 | StackMapEncoding encoding = code_info.ExtractEncoding(); |
| 645 | for (size_t i = 0; i < loop_headers.size(); ++i) { |
| 646 | if (loop_headers[i]->GetDexPc() == dex_pc) { |
| 647 | if (graph.IsCompilingOsr()) { |
| 648 | DCHECK(code_info.GetOsrStackMapForDexPc(dex_pc, encoding).IsValid()); |
| 649 | } |
| 650 | ++(*covered)[i]; |
| 651 | } |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | // Debug helper to ensure loop entries in compiled code are matched by |
| 656 | // dex branch instructions. |
| 657 | static void CheckLoopEntriesCanBeUsedForOsr(const HGraph& graph, |
| 658 | const CodeInfo& code_info, |
| 659 | const DexFile::CodeItem& code_item) { |
| 660 | if (graph.HasTryCatch()) { |
| 661 | // One can write loops through try/catch, which we do not support for OSR anyway. |
| 662 | return; |
| 663 | } |
| 664 | ArenaVector<HSuspendCheck*> loop_headers(graph.GetArena()->Adapter(kArenaAllocMisc)); |
| 665 | for (HReversePostOrderIterator it(graph); !it.Done(); it.Advance()) { |
| 666 | if (it.Current()->IsLoopHeader()) { |
| 667 | HSuspendCheck* suspend_check = it.Current()->GetLoopInformation()->GetSuspendCheck(); |
| 668 | if (!suspend_check->GetEnvironment()->IsFromInlinedInvoke()) { |
| 669 | loop_headers.push_back(suspend_check); |
| 670 | } |
| 671 | } |
| 672 | } |
| 673 | ArenaVector<size_t> covered(loop_headers.size(), 0, graph.GetArena()->Adapter(kArenaAllocMisc)); |
| 674 | const uint16_t* code_ptr = code_item.insns_; |
| 675 | const uint16_t* code_end = code_item.insns_ + code_item.insns_size_in_code_units_; |
| 676 | |
| 677 | size_t dex_pc = 0; |
| 678 | while (code_ptr < code_end) { |
| 679 | const Instruction& instruction = *Instruction::At(code_ptr); |
| 680 | if (instruction.IsBranch()) { |
| 681 | uint32_t target = dex_pc + instruction.GetTargetOffset(); |
| 682 | CheckCovers(target, graph, code_info, loop_headers, &covered); |
| 683 | } else if (instruction.IsSwitch()) { |
| 684 | SwitchTable table(instruction, dex_pc, instruction.Opcode() == Instruction::SPARSE_SWITCH); |
| 685 | uint16_t num_entries = table.GetNumEntries(); |
| 686 | size_t offset = table.GetFirstValueIndex(); |
| 687 | |
| 688 | // Use a larger loop counter type to avoid overflow issues. |
| 689 | for (size_t i = 0; i < num_entries; ++i) { |
| 690 | // The target of the case. |
| 691 | uint32_t target = dex_pc + table.GetEntryAt(i + offset); |
| 692 | CheckCovers(target, graph, code_info, loop_headers, &covered); |
| 693 | } |
| 694 | } |
| 695 | dex_pc += instruction.SizeInCodeUnits(); |
| 696 | code_ptr += instruction.SizeInCodeUnits(); |
| 697 | } |
| 698 | |
| 699 | for (size_t i = 0; i < covered.size(); ++i) { |
| 700 | DCHECK_NE(covered[i], 0u) << "Loop in compiled code has no dex branch equivalent"; |
| 701 | } |
| 702 | } |
| 703 | |
| 704 | void CodeGenerator::BuildStackMaps(MemoryRegion region, const DexFile::CodeItem& code_item) { |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 705 | stack_map_stream_.FillIn(region); |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 706 | if (kIsDebugBuild) { |
| 707 | CheckLoopEntriesCanBeUsedForOsr(*graph_, CodeInfo(region), code_item); |
| 708 | } |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 709 | } |
| 710 | |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 711 | void CodeGenerator::RecordPcInfo(HInstruction* instruction, |
| 712 | uint32_t dex_pc, |
| 713 | SlowPathCode* slow_path) { |
Calin Juravle | d2ec87d | 2014-12-08 14:24:46 +0000 | [diff] [blame] | 714 | if (instruction != nullptr) { |
Roland Levillain | 1693a1f | 2016-03-15 14:57:31 +0000 | [diff] [blame] | 715 | // The code generated for some type conversions |
Alexey Frunze | 4dda337 | 2015-06-01 18:31:49 -0700 | [diff] [blame] | 716 | // may call the runtime, thus normally requiring a subsequent |
| 717 | // call to this method. However, the method verifier does not |
| 718 | // produce PC information for certain instructions, which are |
| 719 | // considered "atomic" (they cannot join a GC). |
Roland Levillain | 624279f | 2014-12-04 11:54:28 +0000 | [diff] [blame] | 720 | // Therefore we do not currently record PC information for such |
| 721 | // instructions. As this may change later, we added this special |
| 722 | // case so that code generators may nevertheless call |
| 723 | // CodeGenerator::RecordPcInfo without triggering an error in |
| 724 | // CodeGenerator::BuildNativeGCMap ("Missing ref for dex pc 0x") |
| 725 | // thereafter. |
Roland Levillain | 1693a1f | 2016-03-15 14:57:31 +0000 | [diff] [blame] | 726 | if (instruction->IsTypeConversion()) { |
Calin Juravle | d2ec87d | 2014-12-08 14:24:46 +0000 | [diff] [blame] | 727 | return; |
| 728 | } |
| 729 | if (instruction->IsRem()) { |
| 730 | Primitive::Type type = instruction->AsRem()->GetResultType(); |
| 731 | if ((type == Primitive::kPrimFloat) || (type == Primitive::kPrimDouble)) { |
| 732 | return; |
| 733 | } |
| 734 | } |
Roland Levillain | 624279f | 2014-12-04 11:54:28 +0000 | [diff] [blame] | 735 | } |
| 736 | |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 737 | uint32_t outer_dex_pc = dex_pc; |
| 738 | uint32_t outer_environment_size = 0; |
| 739 | uint32_t inlining_depth = 0; |
| 740 | if (instruction != nullptr) { |
| 741 | for (HEnvironment* environment = instruction->GetEnvironment(); |
| 742 | environment != nullptr; |
| 743 | environment = environment->GetParent()) { |
| 744 | outer_dex_pc = environment->GetDexPc(); |
| 745 | outer_environment_size = environment->Size(); |
| 746 | if (environment != instruction->GetEnvironment()) { |
| 747 | inlining_depth++; |
| 748 | } |
| 749 | } |
| 750 | } |
| 751 | |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 752 | // Collect PC infos for the mapping table. |
Vladimir Marko | bd8c725 | 2015-06-12 10:06:32 +0100 | [diff] [blame] | 753 | uint32_t native_pc = GetAssembler()->CodeSize(); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 754 | |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 755 | if (instruction == nullptr) { |
David Srbecky | c7098ff | 2016-02-09 14:30:11 +0000 | [diff] [blame] | 756 | // For stack overflow checks and native-debug-info entries without dex register |
| 757 | // mapping (i.e. start of basic block or start of slow path). |
Vladimir Marko | bd8c725 | 2015-06-12 10:06:32 +0100 | [diff] [blame] | 758 | stack_map_stream_.BeginStackMapEntry(outer_dex_pc, native_pc, 0, 0, 0, 0); |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 759 | stack_map_stream_.EndStackMapEntry(); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 760 | return; |
| 761 | } |
| 762 | LocationSummary* locations = instruction->GetLocations(); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 763 | |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 764 | uint32_t register_mask = locations->GetRegisterMask(); |
| 765 | if (locations->OnlyCallsOnSlowPath()) { |
| 766 | // In case of slow path, we currently set the location of caller-save registers |
| 767 | // to register (instead of their stack location when pushed before the slow-path |
| 768 | // call). Therefore register_mask contains both callee-save and caller-save |
| 769 | // registers that hold objects. We must remove the caller-save from the mask, since |
| 770 | // they will be overwritten by the callee. |
| 771 | register_mask &= core_callee_save_mask_; |
| 772 | } |
| 773 | // The register mask must be a subset of callee-save registers. |
| 774 | DCHECK_EQ(register_mask & core_callee_save_mask_, register_mask); |
Vladimir Marko | bd8c725 | 2015-06-12 10:06:32 +0100 | [diff] [blame] | 775 | stack_map_stream_.BeginStackMapEntry(outer_dex_pc, |
| 776 | native_pc, |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 777 | register_mask, |
| 778 | locations->GetStackMask(), |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 779 | outer_environment_size, |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 780 | inlining_depth); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 781 | |
| 782 | EmitEnvironment(instruction->GetEnvironment(), slow_path); |
| 783 | stack_map_stream_.EndStackMapEntry(); |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 784 | |
| 785 | HLoopInformation* info = instruction->GetBlock()->GetLoopInformation(); |
| 786 | if (instruction->IsSuspendCheck() && |
| 787 | (info != nullptr) && |
| 788 | graph_->IsCompilingOsr() && |
| 789 | (inlining_depth == 0)) { |
| 790 | DCHECK_EQ(info->GetSuspendCheck(), instruction); |
| 791 | // We duplicate the stack map as a marker that this stack map can be an OSR entry. |
| 792 | // Duplicating it avoids having the runtime recognize and skip an OSR stack map. |
| 793 | DCHECK(info->IsIrreducible()); |
| 794 | stack_map_stream_.BeginStackMapEntry( |
| 795 | dex_pc, native_pc, register_mask, locations->GetStackMask(), outer_environment_size, 0); |
| 796 | EmitEnvironment(instruction->GetEnvironment(), slow_path); |
| 797 | stack_map_stream_.EndStackMapEntry(); |
| 798 | if (kIsDebugBuild) { |
| 799 | HEnvironment* environment = instruction->GetEnvironment(); |
| 800 | for (size_t i = 0, environment_size = environment->Size(); i < environment_size; ++i) { |
| 801 | HInstruction* in_environment = environment->GetInstructionAt(i); |
| 802 | if (in_environment != nullptr) { |
| 803 | DCHECK(in_environment->IsPhi() || in_environment->IsConstant()); |
| 804 | Location location = environment->GetLocationAt(i); |
| 805 | DCHECK(location.IsStackSlot() || |
| 806 | location.IsDoubleStackSlot() || |
| 807 | location.IsConstant() || |
| 808 | location.IsInvalid()); |
| 809 | if (location.IsStackSlot() || location.IsDoubleStackSlot()) { |
| 810 | DCHECK_LT(location.GetStackIndex(), static_cast<int32_t>(GetFrameSize())); |
| 811 | } |
| 812 | } |
| 813 | } |
| 814 | } |
| 815 | } else if (kIsDebugBuild) { |
| 816 | // Ensure stack maps are unique, by checking that the native pc in the stack map |
| 817 | // last emitted is different than the native pc of the stack map just emitted. |
| 818 | size_t number_of_stack_maps = stack_map_stream_.GetNumberOfStackMaps(); |
| 819 | if (number_of_stack_maps > 1) { |
| 820 | DCHECK_NE(stack_map_stream_.GetStackMap(number_of_stack_maps - 1).native_pc_offset, |
| 821 | stack_map_stream_.GetStackMap(number_of_stack_maps - 2).native_pc_offset); |
| 822 | } |
| 823 | } |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 824 | } |
| 825 | |
David Srbecky | b7070a2 | 2016-01-08 18:13:53 +0000 | [diff] [blame] | 826 | bool CodeGenerator::HasStackMapAtCurrentPc() { |
| 827 | uint32_t pc = GetAssembler()->CodeSize(); |
| 828 | size_t count = stack_map_stream_.GetNumberOfStackMaps(); |
| 829 | return count > 0 && stack_map_stream_.GetStackMap(count - 1).native_pc_offset == pc; |
| 830 | } |
| 831 | |
David Srbecky | d28f4a0 | 2016-03-14 17:14:24 +0000 | [diff] [blame] | 832 | void CodeGenerator::MaybeRecordNativeDebugInfo(HInstruction* instruction, |
| 833 | uint32_t dex_pc, |
| 834 | SlowPathCode* slow_path) { |
David Srbecky | c7098ff | 2016-02-09 14:30:11 +0000 | [diff] [blame] | 835 | if (GetCompilerOptions().GetNativeDebuggable() && dex_pc != kNoDexPc) { |
| 836 | if (HasStackMapAtCurrentPc()) { |
| 837 | // Ensure that we do not collide with the stack map of the previous instruction. |
| 838 | GenerateNop(); |
| 839 | } |
David Srbecky | d28f4a0 | 2016-03-14 17:14:24 +0000 | [diff] [blame] | 840 | RecordPcInfo(instruction, dex_pc, slow_path); |
David Srbecky | c7098ff | 2016-02-09 14:30:11 +0000 | [diff] [blame] | 841 | } |
| 842 | } |
| 843 | |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 844 | void CodeGenerator::RecordCatchBlockInfo() { |
| 845 | ArenaAllocator* arena = graph_->GetArena(); |
| 846 | |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 847 | for (HBasicBlock* block : *block_order_) { |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 848 | if (!block->IsCatchBlock()) { |
| 849 | continue; |
| 850 | } |
| 851 | |
| 852 | uint32_t dex_pc = block->GetDexPc(); |
| 853 | uint32_t num_vregs = graph_->GetNumberOfVRegs(); |
| 854 | uint32_t inlining_depth = 0; // Inlining of catch blocks is not supported at the moment. |
| 855 | uint32_t native_pc = GetAddressOf(block); |
| 856 | uint32_t register_mask = 0; // Not used. |
| 857 | |
| 858 | // The stack mask is not used, so we leave it empty. |
Vladimir Marko | f6a35de | 2016-03-21 12:01:50 +0000 | [diff] [blame^] | 859 | ArenaBitVector* stack_mask = |
| 860 | ArenaBitVector::Create(arena, 0, /* expandable */ true, kArenaAllocCodeGenerator); |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 861 | |
| 862 | stack_map_stream_.BeginStackMapEntry(dex_pc, |
| 863 | native_pc, |
| 864 | register_mask, |
| 865 | stack_mask, |
| 866 | num_vregs, |
| 867 | inlining_depth); |
| 868 | |
| 869 | HInstruction* current_phi = block->GetFirstPhi(); |
| 870 | for (size_t vreg = 0; vreg < num_vregs; ++vreg) { |
| 871 | while (current_phi != nullptr && current_phi->AsPhi()->GetRegNumber() < vreg) { |
| 872 | HInstruction* next_phi = current_phi->GetNext(); |
| 873 | DCHECK(next_phi == nullptr || |
| 874 | current_phi->AsPhi()->GetRegNumber() <= next_phi->AsPhi()->GetRegNumber()) |
| 875 | << "Phis need to be sorted by vreg number to keep this a linear-time loop."; |
| 876 | current_phi = next_phi; |
| 877 | } |
| 878 | |
| 879 | if (current_phi == nullptr || current_phi->AsPhi()->GetRegNumber() != vreg) { |
| 880 | stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kNone, 0); |
| 881 | } else { |
| 882 | Location location = current_phi->GetLiveInterval()->ToLocation(); |
| 883 | switch (location.GetKind()) { |
| 884 | case Location::kStackSlot: { |
| 885 | stack_map_stream_.AddDexRegisterEntry( |
| 886 | DexRegisterLocation::Kind::kInStack, location.GetStackIndex()); |
| 887 | break; |
| 888 | } |
| 889 | case Location::kDoubleStackSlot: { |
| 890 | stack_map_stream_.AddDexRegisterEntry( |
| 891 | DexRegisterLocation::Kind::kInStack, location.GetStackIndex()); |
| 892 | stack_map_stream_.AddDexRegisterEntry( |
| 893 | DexRegisterLocation::Kind::kInStack, location.GetHighStackIndex(kVRegSize)); |
| 894 | ++vreg; |
| 895 | DCHECK_LT(vreg, num_vregs); |
| 896 | break; |
| 897 | } |
| 898 | default: { |
| 899 | // All catch phis must be allocated to a stack slot. |
| 900 | LOG(FATAL) << "Unexpected kind " << location.GetKind(); |
| 901 | UNREACHABLE(); |
| 902 | } |
| 903 | } |
| 904 | } |
| 905 | } |
| 906 | |
| 907 | stack_map_stream_.EndStackMapEntry(); |
| 908 | } |
| 909 | } |
| 910 | |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 911 | void CodeGenerator::EmitEnvironment(HEnvironment* environment, SlowPathCode* slow_path) { |
| 912 | if (environment == nullptr) return; |
| 913 | |
| 914 | if (environment->GetParent() != nullptr) { |
| 915 | // We emit the parent environment first. |
| 916 | EmitEnvironment(environment->GetParent(), slow_path); |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 917 | stack_map_stream_.BeginInlineInfoEntry(environment->GetMethodIdx(), |
| 918 | environment->GetDexPc(), |
| 919 | environment->GetInvokeType(), |
| 920 | environment->Size()); |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 921 | } |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 922 | |
| 923 | // Walk over the environment, and record the location of dex registers. |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 924 | for (size_t i = 0, environment_size = environment->Size(); i < environment_size; ++i) { |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 925 | HInstruction* current = environment->GetInstructionAt(i); |
| 926 | if (current == nullptr) { |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 927 | stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kNone, 0); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 928 | continue; |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 929 | } |
| 930 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 931 | Location location = environment->GetLocationAt(i); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 932 | switch (location.GetKind()) { |
| 933 | case Location::kConstant: { |
| 934 | DCHECK_EQ(current, location.GetConstant()); |
| 935 | if (current->IsLongConstant()) { |
| 936 | int64_t value = current->AsLongConstant()->GetValue(); |
| 937 | stack_map_stream_.AddDexRegisterEntry( |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 938 | DexRegisterLocation::Kind::kConstant, Low32Bits(value)); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 939 | stack_map_stream_.AddDexRegisterEntry( |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 940 | DexRegisterLocation::Kind::kConstant, High32Bits(value)); |
| 941 | ++i; |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 942 | DCHECK_LT(i, environment_size); |
| 943 | } else if (current->IsDoubleConstant()) { |
Roland Levillain | da4d79b | 2015-03-24 14:36:11 +0000 | [diff] [blame] | 944 | int64_t value = bit_cast<int64_t, double>(current->AsDoubleConstant()->GetValue()); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 945 | stack_map_stream_.AddDexRegisterEntry( |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 946 | DexRegisterLocation::Kind::kConstant, Low32Bits(value)); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 947 | stack_map_stream_.AddDexRegisterEntry( |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 948 | DexRegisterLocation::Kind::kConstant, High32Bits(value)); |
| 949 | ++i; |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 950 | DCHECK_LT(i, environment_size); |
| 951 | } else if (current->IsIntConstant()) { |
| 952 | int32_t value = current->AsIntConstant()->GetValue(); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 953 | stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kConstant, value); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 954 | } else if (current->IsNullConstant()) { |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 955 | stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kConstant, 0); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 956 | } else { |
| 957 | DCHECK(current->IsFloatConstant()) << current->DebugName(); |
Roland Levillain | da4d79b | 2015-03-24 14:36:11 +0000 | [diff] [blame] | 958 | int32_t value = bit_cast<int32_t, float>(current->AsFloatConstant()->GetValue()); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 959 | stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kConstant, value); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 960 | } |
| 961 | break; |
| 962 | } |
| 963 | |
| 964 | case Location::kStackSlot: { |
| 965 | stack_map_stream_.AddDexRegisterEntry( |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 966 | DexRegisterLocation::Kind::kInStack, location.GetStackIndex()); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 967 | break; |
| 968 | } |
| 969 | |
| 970 | case Location::kDoubleStackSlot: { |
| 971 | stack_map_stream_.AddDexRegisterEntry( |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 972 | DexRegisterLocation::Kind::kInStack, location.GetStackIndex()); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 973 | stack_map_stream_.AddDexRegisterEntry( |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 974 | DexRegisterLocation::Kind::kInStack, location.GetHighStackIndex(kVRegSize)); |
| 975 | ++i; |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 976 | DCHECK_LT(i, environment_size); |
| 977 | break; |
| 978 | } |
| 979 | |
| 980 | case Location::kRegister : { |
| 981 | int id = location.reg(); |
| 982 | if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(id)) { |
| 983 | uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(id); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 984 | stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 985 | if (current->GetType() == Primitive::kPrimLong) { |
| 986 | stack_map_stream_.AddDexRegisterEntry( |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 987 | DexRegisterLocation::Kind::kInStack, offset + kVRegSize); |
| 988 | ++i; |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 989 | DCHECK_LT(i, environment_size); |
| 990 | } |
| 991 | } else { |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 992 | stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, id); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 993 | if (current->GetType() == Primitive::kPrimLong) { |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 994 | stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegisterHigh, id); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 995 | ++i; |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 996 | DCHECK_LT(i, environment_size); |
| 997 | } |
| 998 | } |
| 999 | break; |
| 1000 | } |
| 1001 | |
| 1002 | case Location::kFpuRegister : { |
| 1003 | int id = location.reg(); |
| 1004 | if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(id)) { |
| 1005 | uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(id); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 1006 | stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 1007 | if (current->GetType() == Primitive::kPrimDouble) { |
| 1008 | stack_map_stream_.AddDexRegisterEntry( |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 1009 | DexRegisterLocation::Kind::kInStack, offset + kVRegSize); |
| 1010 | ++i; |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 1011 | DCHECK_LT(i, environment_size); |
| 1012 | } |
| 1013 | } else { |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 1014 | stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, id); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 1015 | if (current->GetType() == Primitive::kPrimDouble) { |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 1016 | stack_map_stream_.AddDexRegisterEntry( |
| 1017 | DexRegisterLocation::Kind::kInFpuRegisterHigh, id); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 1018 | ++i; |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 1019 | DCHECK_LT(i, environment_size); |
| 1020 | } |
| 1021 | } |
| 1022 | break; |
| 1023 | } |
| 1024 | |
| 1025 | case Location::kFpuRegisterPair : { |
| 1026 | int low = location.low(); |
| 1027 | int high = location.high(); |
| 1028 | if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(low)) { |
| 1029 | uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(low); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 1030 | stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 1031 | } else { |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 1032 | stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, low); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 1033 | } |
| 1034 | if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(high)) { |
| 1035 | uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(high); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 1036 | stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset); |
| 1037 | ++i; |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 1038 | } else { |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 1039 | stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, high); |
| 1040 | ++i; |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 1041 | } |
| 1042 | DCHECK_LT(i, environment_size); |
| 1043 | break; |
| 1044 | } |
| 1045 | |
| 1046 | case Location::kRegisterPair : { |
| 1047 | int low = location.low(); |
| 1048 | int high = location.high(); |
| 1049 | if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(low)) { |
| 1050 | uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(low); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 1051 | stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 1052 | } else { |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 1053 | stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, low); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 1054 | } |
| 1055 | if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(high)) { |
| 1056 | uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(high); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 1057 | stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 1058 | } else { |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 1059 | stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, high); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 1060 | } |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 1061 | ++i; |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 1062 | DCHECK_LT(i, environment_size); |
| 1063 | break; |
| 1064 | } |
| 1065 | |
| 1066 | case Location::kInvalid: { |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 1067 | stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kNone, 0); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 1068 | break; |
| 1069 | } |
| 1070 | |
| 1071 | default: |
| 1072 | LOG(FATAL) << "Unexpected kind " << location.GetKind(); |
| 1073 | } |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 1074 | } |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 1075 | |
| 1076 | if (environment->GetParent() != nullptr) { |
| 1077 | stack_map_stream_.EndInlineInfoEntry(); |
| 1078 | } |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 1079 | } |
| 1080 | |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 1081 | bool CodeGenerator::IsImplicitNullCheckAllowed(HNullCheck* null_check) const { |
| 1082 | return compiler_options_.GetImplicitNullChecks() && |
| 1083 | // Null checks which might throw into a catch block need to save live |
| 1084 | // registers and therefore cannot be done implicitly. |
| 1085 | !null_check->CanThrowIntoCatchBlock(); |
| 1086 | } |
| 1087 | |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 1088 | bool CodeGenerator::CanMoveNullCheckToUser(HNullCheck* null_check) { |
| 1089 | HInstruction* first_next_not_move = null_check->GetNextDisregardingMoves(); |
Calin Juravle | 641547a | 2015-04-21 22:08:51 +0100 | [diff] [blame] | 1090 | |
| 1091 | return (first_next_not_move != nullptr) |
| 1092 | && first_next_not_move->CanDoImplicitNullCheckOn(null_check->InputAt(0)); |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 1093 | } |
| 1094 | |
| 1095 | void CodeGenerator::MaybeRecordImplicitNullCheck(HInstruction* instr) { |
| 1096 | // If we are from a static path don't record the pc as we can't throw NPE. |
| 1097 | // NB: having the checks here makes the code much less verbose in the arch |
| 1098 | // specific code generators. |
| 1099 | if (instr->IsStaticFieldSet() || instr->IsStaticFieldGet()) { |
| 1100 | return; |
| 1101 | } |
| 1102 | |
Calin Juravle | 641547a | 2015-04-21 22:08:51 +0100 | [diff] [blame] | 1103 | if (!instr->CanDoImplicitNullCheckOn(instr->InputAt(0))) { |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 1104 | return; |
| 1105 | } |
| 1106 | |
| 1107 | // Find the first previous instruction which is not a move. |
| 1108 | HInstruction* first_prev_not_move = instr->GetPreviousDisregardingMoves(); |
| 1109 | |
| 1110 | // If the instruction is a null check it means that `instr` is the first user |
| 1111 | // and needs to record the pc. |
| 1112 | if (first_prev_not_move != nullptr && first_prev_not_move->IsNullCheck()) { |
| 1113 | HNullCheck* null_check = first_prev_not_move->AsNullCheck(); |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 1114 | if (IsImplicitNullCheckAllowed(null_check)) { |
| 1115 | // TODO: The parallel moves modify the environment. Their changes need to be |
| 1116 | // reverted otherwise the stack maps at the throw point will not be correct. |
| 1117 | RecordPcInfo(null_check, null_check->GetDexPc()); |
| 1118 | } |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 1119 | } |
| 1120 | } |
| 1121 | |
Calin Juravle | 2ae4818 | 2016-03-16 14:05:09 +0000 | [diff] [blame] | 1122 | void CodeGenerator::GenerateNullCheck(HNullCheck* instruction) { |
| 1123 | if (IsImplicitNullCheckAllowed(instruction)) { |
| 1124 | MaybeRecordStat(kImplicitNullCheckGenerated); |
| 1125 | GenerateImplicitNullCheck(instruction); |
| 1126 | } else { |
| 1127 | MaybeRecordStat(kExplicitNullCheckGenerated); |
| 1128 | GenerateExplicitNullCheck(instruction); |
| 1129 | } |
| 1130 | } |
| 1131 | |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 1132 | void CodeGenerator::ClearSpillSlotsFromLoopPhisInStackMap(HSuspendCheck* suspend_check) const { |
| 1133 | LocationSummary* locations = suspend_check->GetLocations(); |
| 1134 | HBasicBlock* block = suspend_check->GetBlock(); |
| 1135 | DCHECK(block->GetLoopInformation()->GetSuspendCheck() == suspend_check); |
| 1136 | DCHECK(block->IsLoopHeader()); |
| 1137 | |
| 1138 | for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) { |
| 1139 | HInstruction* current = it.Current(); |
| 1140 | LiveInterval* interval = current->GetLiveInterval(); |
| 1141 | // We only need to clear bits of loop phis containing objects and allocated in register. |
| 1142 | // Loop phis allocated on stack already have the object in the stack. |
| 1143 | if (current->GetType() == Primitive::kPrimNot |
| 1144 | && interval->HasRegister() |
| 1145 | && interval->HasSpillSlot()) { |
| 1146 | locations->ClearStackBit(interval->GetSpillSlot() / kVRegSize); |
| 1147 | } |
| 1148 | } |
| 1149 | } |
| 1150 | |
Nicolas Geoffray | 9021825 | 2015-04-15 11:56:51 +0100 | [diff] [blame] | 1151 | void CodeGenerator::EmitParallelMoves(Location from1, |
| 1152 | Location to1, |
| 1153 | Primitive::Type type1, |
| 1154 | Location from2, |
| 1155 | Location to2, |
| 1156 | Primitive::Type type2) { |
Nicolas Geoffray | f0e3937 | 2014-11-12 17:50:07 +0000 | [diff] [blame] | 1157 | HParallelMove parallel_move(GetGraph()->GetArena()); |
Nicolas Geoffray | 9021825 | 2015-04-15 11:56:51 +0100 | [diff] [blame] | 1158 | parallel_move.AddMove(from1, to1, type1, nullptr); |
| 1159 | parallel_move.AddMove(from2, to2, type2, nullptr); |
Nicolas Geoffray | f0e3937 | 2014-11-12 17:50:07 +0000 | [diff] [blame] | 1160 | GetMoveResolver()->EmitNativeCode(¶llel_move); |
| 1161 | } |
| 1162 | |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 1163 | void CodeGenerator::ValidateInvokeRuntime(HInstruction* instruction, SlowPathCode* slow_path) { |
| 1164 | // Ensure that the call kind indication given to the register allocator is |
| 1165 | // coherent with the runtime call generated, and that the GC side effect is |
| 1166 | // set when required. |
| 1167 | if (slow_path == nullptr) { |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 1168 | DCHECK(instruction->GetLocations()->WillCall()) |
| 1169 | << "instruction->DebugName()=" << instruction->DebugName(); |
Roland Levillain | df3f822 | 2015-08-13 12:31:44 +0100 | [diff] [blame] | 1170 | DCHECK(instruction->GetSideEffects().Includes(SideEffects::CanTriggerGC())) |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 1171 | << "instruction->DebugName()=" << instruction->DebugName() |
| 1172 | << " instruction->GetSideEffects().ToString()=" << instruction->GetSideEffects().ToString(); |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 1173 | } else { |
Roland Levillain | df3f822 | 2015-08-13 12:31:44 +0100 | [diff] [blame] | 1174 | DCHECK(instruction->GetLocations()->OnlyCallsOnSlowPath() || slow_path->IsFatal()) |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 1175 | << "instruction->DebugName()=" << instruction->DebugName() |
| 1176 | << " slow_path->GetDescription()=" << slow_path->GetDescription(); |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 1177 | DCHECK(instruction->GetSideEffects().Includes(SideEffects::CanTriggerGC()) || |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 1178 | // When read barriers are enabled, some instructions use a |
| 1179 | // slow path to emit a read barrier, which does not trigger |
| 1180 | // GC, is not fatal, nor is emitted by HDeoptimize |
| 1181 | // instructions. |
| 1182 | (kEmitCompilerReadBarrier && |
| 1183 | (instruction->IsInstanceFieldGet() || |
| 1184 | instruction->IsStaticFieldGet() || |
| 1185 | instruction->IsArraySet() || |
| 1186 | instruction->IsArrayGet() || |
| 1187 | instruction->IsLoadClass() || |
| 1188 | instruction->IsLoadString() || |
| 1189 | instruction->IsInstanceOf() || |
| 1190 | instruction->IsCheckCast()))) |
| 1191 | << "instruction->DebugName()=" << instruction->DebugName() |
| 1192 | << " instruction->GetSideEffects().ToString()=" << instruction->GetSideEffects().ToString() |
| 1193 | << " slow_path->GetDescription()=" << slow_path->GetDescription(); |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 1194 | } |
| 1195 | |
| 1196 | // Check the coherency of leaf information. |
| 1197 | DCHECK(instruction->IsSuspendCheck() |
| 1198 | || ((slow_path != nullptr) && slow_path->IsFatal()) |
| 1199 | || instruction->GetLocations()->CanCall() |
Roland Levillain | df3f822 | 2015-08-13 12:31:44 +0100 | [diff] [blame] | 1200 | || !IsLeafMethod()) |
| 1201 | << instruction->DebugName() << ((slow_path != nullptr) ? slow_path->GetDescription() : ""); |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 1202 | } |
| 1203 | |
Nicolas Geoffray | a8ac913 | 2015-03-13 16:36:36 +0000 | [diff] [blame] | 1204 | void SlowPathCode::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) { |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 1205 | RegisterSet* live_registers = locations->GetLiveRegisters(); |
Nicolas Geoffray | a8ac913 | 2015-03-13 16:36:36 +0000 | [diff] [blame] | 1206 | size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath(); |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 1207 | |
Nicolas Geoffray | a8ac913 | 2015-03-13 16:36:36 +0000 | [diff] [blame] | 1208 | for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) { |
| 1209 | if (!codegen->IsCoreCalleeSaveRegister(i)) { |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 1210 | if (live_registers->ContainsCoreRegister(i)) { |
Nicolas Geoffray | a8ac913 | 2015-03-13 16:36:36 +0000 | [diff] [blame] | 1211 | // If the register holds an object, update the stack mask. |
| 1212 | if (locations->RegisterContainsObject(i)) { |
| 1213 | locations->SetStackBit(stack_offset / kVRegSize); |
| 1214 | } |
| 1215 | DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize()); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 1216 | DCHECK_LT(i, kMaximumNumberOfExpectedRegisters); |
| 1217 | saved_core_stack_offsets_[i] = stack_offset; |
Nicolas Geoffray | a8ac913 | 2015-03-13 16:36:36 +0000 | [diff] [blame] | 1218 | stack_offset += codegen->SaveCoreRegister(stack_offset, i); |
| 1219 | } |
| 1220 | } |
| 1221 | } |
| 1222 | |
| 1223 | for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) { |
| 1224 | if (!codegen->IsFloatingPointCalleeSaveRegister(i)) { |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 1225 | if (live_registers->ContainsFloatingPointRegister(i)) { |
Nicolas Geoffray | a8ac913 | 2015-03-13 16:36:36 +0000 | [diff] [blame] | 1226 | DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize()); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 1227 | DCHECK_LT(i, kMaximumNumberOfExpectedRegisters); |
| 1228 | saved_fpu_stack_offsets_[i] = stack_offset; |
Nicolas Geoffray | a8ac913 | 2015-03-13 16:36:36 +0000 | [diff] [blame] | 1229 | stack_offset += codegen->SaveFloatingPointRegister(stack_offset, i); |
| 1230 | } |
| 1231 | } |
| 1232 | } |
| 1233 | } |
| 1234 | |
| 1235 | void SlowPathCode::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) { |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 1236 | RegisterSet* live_registers = locations->GetLiveRegisters(); |
Nicolas Geoffray | a8ac913 | 2015-03-13 16:36:36 +0000 | [diff] [blame] | 1237 | size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath(); |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 1238 | |
Nicolas Geoffray | a8ac913 | 2015-03-13 16:36:36 +0000 | [diff] [blame] | 1239 | for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) { |
| 1240 | if (!codegen->IsCoreCalleeSaveRegister(i)) { |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 1241 | if (live_registers->ContainsCoreRegister(i)) { |
Nicolas Geoffray | a8ac913 | 2015-03-13 16:36:36 +0000 | [diff] [blame] | 1242 | DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize()); |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 1243 | DCHECK_LT(i, kMaximumNumberOfExpectedRegisters); |
Nicolas Geoffray | a8ac913 | 2015-03-13 16:36:36 +0000 | [diff] [blame] | 1244 | stack_offset += codegen->RestoreCoreRegister(stack_offset, i); |
| 1245 | } |
| 1246 | } |
| 1247 | } |
| 1248 | |
| 1249 | for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) { |
| 1250 | if (!codegen->IsFloatingPointCalleeSaveRegister(i)) { |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 1251 | if (live_registers->ContainsFloatingPointRegister(i)) { |
Nicolas Geoffray | a8ac913 | 2015-03-13 16:36:36 +0000 | [diff] [blame] | 1252 | DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize()); |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 1253 | DCHECK_LT(i, kMaximumNumberOfExpectedRegisters); |
Nicolas Geoffray | a8ac913 | 2015-03-13 16:36:36 +0000 | [diff] [blame] | 1254 | stack_offset += codegen->RestoreFloatingPointRegister(stack_offset, i); |
| 1255 | } |
| 1256 | } |
| 1257 | } |
| 1258 | } |
| 1259 | |
Nicolas Geoffray | 5bd05a5 | 2015-10-13 09:48:30 +0100 | [diff] [blame] | 1260 | void CodeGenerator::CreateSystemArrayCopyLocationSummary(HInvoke* invoke) { |
| 1261 | // Check to see if we have known failures that will cause us to have to bail out |
| 1262 | // to the runtime, and just generate the runtime call directly. |
| 1263 | HIntConstant* src_pos = invoke->InputAt(1)->AsIntConstant(); |
| 1264 | HIntConstant* dest_pos = invoke->InputAt(3)->AsIntConstant(); |
| 1265 | |
| 1266 | // The positions must be non-negative. |
| 1267 | if ((src_pos != nullptr && src_pos->GetValue() < 0) || |
| 1268 | (dest_pos != nullptr && dest_pos->GetValue() < 0)) { |
| 1269 | // We will have to fail anyways. |
| 1270 | return; |
| 1271 | } |
| 1272 | |
| 1273 | // The length must be >= 0. |
| 1274 | HIntConstant* length = invoke->InputAt(4)->AsIntConstant(); |
| 1275 | if (length != nullptr) { |
| 1276 | int32_t len = length->GetValue(); |
| 1277 | if (len < 0) { |
| 1278 | // Just call as normal. |
| 1279 | return; |
| 1280 | } |
| 1281 | } |
| 1282 | |
| 1283 | SystemArrayCopyOptimizations optimizations(invoke); |
| 1284 | |
| 1285 | if (optimizations.GetDestinationIsSource()) { |
| 1286 | if (src_pos != nullptr && dest_pos != nullptr && src_pos->GetValue() < dest_pos->GetValue()) { |
| 1287 | // We only support backward copying if source and destination are the same. |
| 1288 | return; |
| 1289 | } |
| 1290 | } |
| 1291 | |
| 1292 | if (optimizations.GetDestinationIsPrimitiveArray() || optimizations.GetSourceIsPrimitiveArray()) { |
| 1293 | // We currently don't intrinsify primitive copying. |
| 1294 | return; |
| 1295 | } |
| 1296 | |
| 1297 | ArenaAllocator* allocator = invoke->GetBlock()->GetGraph()->GetArena(); |
| 1298 | LocationSummary* locations = new (allocator) LocationSummary(invoke, |
| 1299 | LocationSummary::kCallOnSlowPath, |
| 1300 | kIntrinsified); |
| 1301 | // arraycopy(Object src, int src_pos, Object dest, int dest_pos, int length). |
| 1302 | locations->SetInAt(0, Location::RequiresRegister()); |
| 1303 | locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1))); |
| 1304 | locations->SetInAt(2, Location::RequiresRegister()); |
| 1305 | locations->SetInAt(3, Location::RegisterOrConstant(invoke->InputAt(3))); |
| 1306 | locations->SetInAt(4, Location::RegisterOrConstant(invoke->InputAt(4))); |
| 1307 | |
| 1308 | locations->AddTemp(Location::RequiresRegister()); |
| 1309 | locations->AddTemp(Location::RequiresRegister()); |
| 1310 | locations->AddTemp(Location::RequiresRegister()); |
| 1311 | } |
| 1312 | |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 1313 | } // namespace art |