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 | 73e80c3 | 2014-07-22 17:47:56 +0100 | [diff] [blame] | 145 | void CodeGenerator::CompileBaseline(CodeAllocator* allocator, bool is_leaf) { |
Nicolas Geoffray | 4c204ba | 2015-02-03 15:12:35 +0000 | [diff] [blame] | 146 | Initialize(); |
Nicolas Geoffray | 73e80c3 | 2014-07-22 17:47:56 +0100 | [diff] [blame] | 147 | if (!is_leaf) { |
| 148 | MarkNotLeaf(); |
| 149 | } |
Mathieu Chartier | e3b034a | 2015-05-31 14:29:23 -0700 | [diff] [blame] | 150 | const bool is_64_bit = Is64BitInstructionSet(GetInstructionSet()); |
Nicolas Geoffray | 4c204ba | 2015-02-03 15:12:35 +0000 | [diff] [blame] | 151 | InitializeCodeGeneration(GetGraph()->GetNumberOfLocalVRegs() |
| 152 | + GetGraph()->GetTemporariesVRegSlots() |
| 153 | + 1 /* filler */, |
| 154 | 0, /* the baseline compiler does not have live registers at slow path */ |
| 155 | 0, /* the baseline compiler does not have live registers at slow path */ |
| 156 | GetGraph()->GetMaximumNumberOfOutVRegs() |
Mathieu Chartier | e3b034a | 2015-05-31 14:29:23 -0700 | [diff] [blame] | 157 | + (is_64_bit ? 2 : 1) /* current method */, |
Nicolas Geoffray | 4c204ba | 2015-02-03 15:12:35 +0000 | [diff] [blame] | 158 | GetGraph()->GetBlocks()); |
| 159 | CompileInternal(allocator, /* is_baseline */ true); |
| 160 | } |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 161 | |
Nicolas Geoffray | dc23d83 | 2015-02-16 11:15:43 +0000 | [diff] [blame] | 162 | bool CodeGenerator::GoesToNextBlock(HBasicBlock* current, HBasicBlock* next) const { |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 163 | DCHECK_EQ((*block_order_)[current_block_index_], current); |
Nicolas Geoffray | dc23d83 | 2015-02-16 11:15:43 +0000 | [diff] [blame] | 164 | return GetNextBlockToEmit() == FirstNonEmptyBlock(next); |
| 165 | } |
| 166 | |
| 167 | HBasicBlock* CodeGenerator::GetNextBlockToEmit() const { |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 168 | for (size_t i = current_block_index_ + 1; i < block_order_->size(); ++i) { |
| 169 | HBasicBlock* block = (*block_order_)[i]; |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 170 | if (!block->IsSingleJump()) { |
Nicolas Geoffray | dc23d83 | 2015-02-16 11:15:43 +0000 | [diff] [blame] | 171 | return block; |
| 172 | } |
| 173 | } |
| 174 | return nullptr; |
| 175 | } |
| 176 | |
| 177 | HBasicBlock* CodeGenerator::FirstNonEmptyBlock(HBasicBlock* block) const { |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 178 | while (block->IsSingleJump()) { |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 179 | block = block->GetSuccessors()[0]; |
Nicolas Geoffray | dc23d83 | 2015-02-16 11:15:43 +0000 | [diff] [blame] | 180 | } |
| 181 | return block; |
| 182 | } |
| 183 | |
Alexandre Rames | eb7b739 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 184 | class DisassemblyScope { |
| 185 | public: |
| 186 | DisassemblyScope(HInstruction* instruction, const CodeGenerator& codegen) |
| 187 | : codegen_(codegen), instruction_(instruction), start_offset_(static_cast<size_t>(-1)) { |
| 188 | if (codegen_.GetDisassemblyInformation() != nullptr) { |
| 189 | start_offset_ = codegen_.GetAssembler().CodeSize(); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | ~DisassemblyScope() { |
| 194 | // We avoid building this data when we know it will not be used. |
| 195 | if (codegen_.GetDisassemblyInformation() != nullptr) { |
| 196 | codegen_.GetDisassemblyInformation()->AddInstructionInterval( |
| 197 | instruction_, start_offset_, codegen_.GetAssembler().CodeSize()); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | private: |
| 202 | const CodeGenerator& codegen_; |
| 203 | HInstruction* instruction_; |
| 204 | size_t start_offset_; |
| 205 | }; |
| 206 | |
| 207 | |
| 208 | void CodeGenerator::GenerateSlowPaths() { |
| 209 | size_t code_start = 0; |
Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 210 | for (SlowPathCode* slow_path : slow_paths_) { |
Vladimir Marko | 0f7dca4 | 2015-11-02 14:36:43 +0000 | [diff] [blame] | 211 | current_slow_path_ = slow_path; |
Alexandre Rames | eb7b739 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 212 | if (disasm_info_ != nullptr) { |
| 213 | code_start = GetAssembler()->CodeSize(); |
| 214 | } |
Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 215 | slow_path->EmitNativeCode(this); |
Alexandre Rames | eb7b739 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 216 | if (disasm_info_ != nullptr) { |
Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 217 | disasm_info_->AddSlowPathInterval(slow_path, code_start, GetAssembler()->CodeSize()); |
Alexandre Rames | eb7b739 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 218 | } |
| 219 | } |
Vladimir Marko | 0f7dca4 | 2015-11-02 14:36:43 +0000 | [diff] [blame] | 220 | current_slow_path_ = nullptr; |
Alexandre Rames | eb7b739 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 221 | } |
| 222 | |
Nicolas Geoffray | 4c204ba | 2015-02-03 15:12:35 +0000 | [diff] [blame] | 223 | void CodeGenerator::CompileInternal(CodeAllocator* allocator, bool is_baseline) { |
Roland Levillain | 3e3d733 | 2015-04-28 11:00:54 +0100 | [diff] [blame] | 224 | is_baseline_ = is_baseline; |
Nicolas Geoffray | 8a16d97 | 2014-09-11 10:30:02 +0100 | [diff] [blame] | 225 | HGraphVisitor* instruction_visitor = GetInstructionVisitor(); |
Nicolas Geoffray | 4c204ba | 2015-02-03 15:12:35 +0000 | [diff] [blame] | 226 | DCHECK_EQ(current_block_index_, 0u); |
Alexandre Rames | eb7b739 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 227 | |
| 228 | size_t frame_start = GetAssembler()->CodeSize(); |
Nicolas Geoffray | 4c204ba | 2015-02-03 15:12:35 +0000 | [diff] [blame] | 229 | GenerateFrameEntry(); |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 230 | DCHECK_EQ(GetAssembler()->cfi().GetCurrentCFAOffset(), static_cast<int>(frame_size_)); |
Alexandre Rames | eb7b739 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 231 | if (disasm_info_ != nullptr) { |
| 232 | disasm_info_->SetFrameEntryInterval(frame_start, GetAssembler()->CodeSize()); |
| 233 | } |
| 234 | |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 235 | for (size_t e = block_order_->size(); current_block_index_ < e; ++current_block_index_) { |
| 236 | HBasicBlock* block = (*block_order_)[current_block_index_]; |
Nicolas Geoffray | dc23d83 | 2015-02-16 11:15:43 +0000 | [diff] [blame] | 237 | // Don't generate code for an empty block. Its predecessors will branch to its successor |
| 238 | // directly. Also, the label of that block will not be emitted, so this helps catch |
| 239 | // errors where we reference that label. |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 240 | if (block->IsSingleJump()) continue; |
Nicolas Geoffray | 92a73ae | 2014-10-16 11:12:52 +0100 | [diff] [blame] | 241 | Bind(block); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 242 | for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) { |
| 243 | HInstruction* current = it.Current(); |
Alexandre Rames | eb7b739 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 244 | DisassemblyScope disassembly_scope(current, *this); |
Nicolas Geoffray | 4c204ba | 2015-02-03 15:12:35 +0000 | [diff] [blame] | 245 | if (is_baseline) { |
Nicolas Geoffray | c0572a4 | 2015-02-06 14:35:25 +0000 | [diff] [blame] | 246 | InitLocationsBaseline(current); |
Nicolas Geoffray | 4c204ba | 2015-02-03 15:12:35 +0000 | [diff] [blame] | 247 | } |
Alexandre Rames | 88c13cd | 2015-04-14 17:35:39 +0100 | [diff] [blame] | 248 | DCHECK(CheckTypeConsistency(current)); |
Yevgeny Rouban | 2a7c1ef | 2015-07-22 18:36:24 +0600 | [diff] [blame] | 249 | uintptr_t native_pc_begin = GetAssembler()->CodeSize(); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 250 | current->Accept(instruction_visitor); |
Yevgeny Rouban | 2a7c1ef | 2015-07-22 18:36:24 +0600 | [diff] [blame] | 251 | uintptr_t native_pc_end = GetAssembler()->CodeSize(); |
| 252 | RecordNativeDebugInfo(current->GetDexPc(), native_pc_begin, native_pc_end); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 253 | } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 254 | } |
Nicolas Geoffray | 4c204ba | 2015-02-03 15:12:35 +0000 | [diff] [blame] | 255 | |
Alexandre Rames | eb7b739 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 256 | GenerateSlowPaths(); |
Nicolas Geoffray | 4c204ba | 2015-02-03 15:12:35 +0000 | [diff] [blame] | 257 | |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 258 | // Emit catch stack maps at the end of the stack map stream as expected by the |
| 259 | // runtime exception handler. |
| 260 | if (!is_baseline && graph_->HasTryCatch()) { |
| 261 | RecordCatchBlockInfo(); |
| 262 | } |
| 263 | |
Nicolas Geoffray | 4c204ba | 2015-02-03 15:12:35 +0000 | [diff] [blame] | 264 | // Finalize instructions in assember; |
Serban Constantinescu | 32f5b4d | 2014-11-25 20:05:46 +0000 | [diff] [blame] | 265 | Finalize(allocator); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 266 | } |
| 267 | |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 268 | void CodeGenerator::CompileOptimized(CodeAllocator* allocator) { |
Nicolas Geoffray | 4c204ba | 2015-02-03 15:12:35 +0000 | [diff] [blame] | 269 | // The register allocator already called `InitializeCodeGeneration`, |
| 270 | // where the frame size has been computed. |
Nicolas Geoffray | 4c204ba | 2015-02-03 15:12:35 +0000 | [diff] [blame] | 271 | DCHECK(block_order_ != nullptr); |
Nicolas Geoffray | 92a73ae | 2014-10-16 11:12:52 +0100 | [diff] [blame] | 272 | Initialize(); |
Nicolas Geoffray | 4c204ba | 2015-02-03 15:12:35 +0000 | [diff] [blame] | 273 | CompileInternal(allocator, /* is_baseline */ false); |
Serban Constantinescu | 32f5b4d | 2014-11-25 20:05:46 +0000 | [diff] [blame] | 274 | } |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 275 | |
Serban Constantinescu | 32f5b4d | 2014-11-25 20:05:46 +0000 | [diff] [blame] | 276 | void CodeGenerator::Finalize(CodeAllocator* allocator) { |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 277 | size_t code_size = GetAssembler()->CodeSize(); |
| 278 | uint8_t* buffer = allocator->Allocate(code_size); |
Serban Constantinescu | 32f5b4d | 2014-11-25 20:05:46 +0000 | [diff] [blame] | 279 | |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 280 | MemoryRegion code(buffer, code_size); |
| 281 | GetAssembler()->FinalizeInstructions(code); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 282 | } |
| 283 | |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 284 | void CodeGenerator::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches ATTRIBUTE_UNUSED) { |
| 285 | // No linker patches by default. |
| 286 | } |
| 287 | |
Nicolas Geoffray | 71175b7 | 2014-10-09 22:13:55 +0100 | [diff] [blame] | 288 | size_t CodeGenerator::FindFreeEntry(bool* array, size_t length) { |
| 289 | for (size_t i = 0; i < length; ++i) { |
| 290 | if (!array[i]) { |
| 291 | array[i] = true; |
| 292 | return i; |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 293 | } |
| 294 | } |
Nicolas Geoffray | 71175b7 | 2014-10-09 22:13:55 +0100 | [diff] [blame] | 295 | LOG(FATAL) << "Could not find a register in baseline register allocator"; |
Nicolas Geoffray | 1ba0f59 | 2014-10-27 15:14:55 +0000 | [diff] [blame] | 296 | UNREACHABLE(); |
Nicolas Geoffray | 1ba0f59 | 2014-10-27 15:14:55 +0000 | [diff] [blame] | 297 | } |
| 298 | |
Nicolas Geoffray | 3c03503 | 2014-10-28 10:46:40 +0000 | [diff] [blame] | 299 | size_t CodeGenerator::FindTwoFreeConsecutiveAlignedEntries(bool* array, size_t length) { |
| 300 | for (size_t i = 0; i < length - 1; i += 2) { |
Nicolas Geoffray | 1ba0f59 | 2014-10-27 15:14:55 +0000 | [diff] [blame] | 301 | if (!array[i] && !array[i + 1]) { |
| 302 | array[i] = true; |
| 303 | array[i + 1] = true; |
| 304 | return i; |
| 305 | } |
| 306 | } |
| 307 | LOG(FATAL) << "Could not find a register in baseline register allocator"; |
| 308 | UNREACHABLE(); |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 309 | } |
| 310 | |
Nicolas Geoffray | 4c204ba | 2015-02-03 15:12:35 +0000 | [diff] [blame] | 311 | void CodeGenerator::InitializeCodeGeneration(size_t number_of_spill_slots, |
| 312 | size_t maximum_number_of_live_core_registers, |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame^] | 313 | size_t maximum_number_of_live_fpu_registers, |
Nicolas Geoffray | 4c204ba | 2015-02-03 15:12:35 +0000 | [diff] [blame] | 314 | size_t number_of_out_slots, |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 315 | const ArenaVector<HBasicBlock*>& block_order) { |
Nicolas Geoffray | 4c204ba | 2015-02-03 15:12:35 +0000 | [diff] [blame] | 316 | block_order_ = &block_order; |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 317 | DCHECK(!block_order.empty()); |
| 318 | DCHECK(block_order[0] == GetGraph()->GetEntryBlock()); |
Nicolas Geoffray | 4dee636 | 2015-01-23 18:23:14 +0000 | [diff] [blame] | 319 | ComputeSpillMask(); |
Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 320 | first_register_slot_in_slow_path_ = (number_of_out_slots + number_of_spill_slots) * kVRegSize; |
| 321 | |
Nicolas Geoffray | c0572a4 | 2015-02-06 14:35:25 +0000 | [diff] [blame] | 322 | if (number_of_spill_slots == 0 |
| 323 | && !HasAllocatedCalleeSaveRegisters() |
| 324 | && IsLeafMethod() |
| 325 | && !RequiresCurrentMethod()) { |
| 326 | DCHECK_EQ(maximum_number_of_live_core_registers, 0u); |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame^] | 327 | DCHECK_EQ(maximum_number_of_live_fpu_registers, 0u); |
Nicolas Geoffray | c0572a4 | 2015-02-06 14:35:25 +0000 | [diff] [blame] | 328 | SetFrameSize(CallPushesPC() ? GetWordSize() : 0); |
| 329 | } else { |
| 330 | SetFrameSize(RoundUp( |
| 331 | number_of_spill_slots * kVRegSize |
| 332 | + number_of_out_slots * kVRegSize |
| 333 | + maximum_number_of_live_core_registers * GetWordSize() |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame^] | 334 | + maximum_number_of_live_fpu_registers * GetFloatingPointSpillSlotSize() |
Nicolas Geoffray | c0572a4 | 2015-02-06 14:35:25 +0000 | [diff] [blame] | 335 | + FrameEntrySpillSize(), |
| 336 | kStackAlignment)); |
| 337 | } |
Nicolas Geoffray | ab032bc | 2014-07-15 12:55:21 +0100 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | Location CodeGenerator::GetTemporaryLocation(HTemporary* temp) const { |
| 341 | uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs(); |
Calin Juravle | f97f9fb | 2014-11-11 15:38:19 +0000 | [diff] [blame] | 342 | // The type of the previous instruction tells us if we need a single or double stack slot. |
| 343 | Primitive::Type type = temp->GetType(); |
| 344 | int32_t temp_size = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble) ? 2 : 1; |
Nicolas Geoffray | ab032bc | 2014-07-15 12:55:21 +0100 | [diff] [blame] | 345 | // Use the temporary region (right below the dex registers). |
| 346 | int32_t slot = GetFrameSize() - FrameEntrySpillSize() |
| 347 | - kVRegSize // filler |
| 348 | - (number_of_locals * kVRegSize) |
Calin Juravle | f97f9fb | 2014-11-11 15:38:19 +0000 | [diff] [blame] | 349 | - ((temp_size + temp->GetIndex()) * kVRegSize); |
| 350 | return temp_size == 2 ? Location::DoubleStackSlot(slot) : Location::StackSlot(slot); |
Nicolas Geoffray | ab032bc | 2014-07-15 12:55:21 +0100 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | int32_t CodeGenerator::GetStackSlot(HLocal* local) const { |
| 354 | uint16_t reg_number = local->GetRegNumber(); |
| 355 | uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs(); |
| 356 | if (reg_number >= number_of_locals) { |
| 357 | // 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] | 358 | // TODO: Share this logic with StackVisitor::GetVRegOffsetFromQuickCode. |
| 359 | return GetFrameSize() + InstructionSetPointerSize(GetInstructionSet()) // ART method |
Nicolas Geoffray | ab032bc | 2014-07-15 12:55:21 +0100 | [diff] [blame] | 360 | + (reg_number - number_of_locals) * kVRegSize; |
| 361 | } else { |
| 362 | // Local is a temporary in this method. It is stored in this method's frame. |
| 363 | return GetFrameSize() - FrameEntrySpillSize() |
| 364 | - kVRegSize // filler. |
| 365 | - (number_of_locals * kVRegSize) |
| 366 | + (reg_number * kVRegSize); |
| 367 | } |
| 368 | } |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 369 | |
Nicolas Geoffray | fd88f16 | 2015-06-03 11:23:52 +0100 | [diff] [blame] | 370 | void CodeGenerator::CreateCommonInvokeLocationSummary( |
Nicolas Geoffray | 4e40c26 | 2015-06-03 12:02:38 +0100 | [diff] [blame] | 371 | HInvoke* invoke, InvokeDexCallingConventionVisitor* visitor) { |
Nicolas Geoffray | fd88f16 | 2015-06-03 11:23:52 +0100 | [diff] [blame] | 372 | ArenaAllocator* allocator = invoke->GetBlock()->GetGraph()->GetArena(); |
| 373 | LocationSummary* locations = new (allocator) LocationSummary(invoke, LocationSummary::kCall); |
Nicolas Geoffray | fd88f16 | 2015-06-03 11:23:52 +0100 | [diff] [blame] | 374 | |
| 375 | for (size_t i = 0; i < invoke->GetNumberOfArguments(); i++) { |
| 376 | HInstruction* input = invoke->InputAt(i); |
| 377 | locations->SetInAt(i, visitor->GetNextLocation(input->GetType())); |
| 378 | } |
| 379 | |
| 380 | locations->SetOut(visitor->GetReturnLocation(invoke->GetType())); |
Nicolas Geoffray | 94015b9 | 2015-06-04 18:21:04 +0100 | [diff] [blame] | 381 | |
| 382 | if (invoke->IsInvokeStaticOrDirect()) { |
| 383 | HInvokeStaticOrDirect* call = invoke->AsInvokeStaticOrDirect(); |
Vladimir Marko | dc151b2 | 2015-10-15 18:02:30 +0100 | [diff] [blame] | 384 | switch (call->GetMethodLoadKind()) { |
| 385 | case HInvokeStaticOrDirect::MethodLoadKind::kRecursive: |
| 386 | locations->SetInAt(call->GetCurrentMethodInputIndex(), visitor->GetMethodLocation()); |
| 387 | break; |
| 388 | case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: |
| 389 | locations->AddTemp(visitor->GetMethodLocation()); |
| 390 | locations->SetInAt(call->GetCurrentMethodInputIndex(), Location::RequiresRegister()); |
| 391 | break; |
| 392 | default: |
| 393 | locations->AddTemp(visitor->GetMethodLocation()); |
| 394 | break; |
Nicolas Geoffray | 94015b9 | 2015-06-04 18:21:04 +0100 | [diff] [blame] | 395 | } |
| 396 | } else { |
| 397 | locations->AddTemp(visitor->GetMethodLocation()); |
| 398 | } |
Nicolas Geoffray | fd88f16 | 2015-06-03 11:23:52 +0100 | [diff] [blame] | 399 | } |
| 400 | |
Calin Juravle | 175dc73 | 2015-08-25 15:42:32 +0100 | [diff] [blame] | 401 | void CodeGenerator::GenerateInvokeUnresolvedRuntimeCall(HInvokeUnresolved* invoke) { |
| 402 | MoveConstant(invoke->GetLocations()->GetTemp(0), invoke->GetDexMethodIndex()); |
| 403 | |
| 404 | // Initialize to anything to silent compiler warnings. |
| 405 | QuickEntrypointEnum entrypoint = kQuickInvokeStaticTrampolineWithAccessCheck; |
| 406 | switch (invoke->GetOriginalInvokeType()) { |
| 407 | case kStatic: |
| 408 | entrypoint = kQuickInvokeStaticTrampolineWithAccessCheck; |
| 409 | break; |
| 410 | case kDirect: |
| 411 | entrypoint = kQuickInvokeDirectTrampolineWithAccessCheck; |
| 412 | break; |
| 413 | case kVirtual: |
| 414 | entrypoint = kQuickInvokeVirtualTrampolineWithAccessCheck; |
| 415 | break; |
| 416 | case kSuper: |
| 417 | entrypoint = kQuickInvokeSuperTrampolineWithAccessCheck; |
| 418 | break; |
| 419 | case kInterface: |
| 420 | entrypoint = kQuickInvokeInterfaceTrampolineWithAccessCheck; |
| 421 | break; |
| 422 | } |
| 423 | InvokeRuntime(entrypoint, invoke, invoke->GetDexPc(), nullptr); |
| 424 | } |
| 425 | |
Calin Juravle | e460d1d | 2015-09-29 04:52:17 +0100 | [diff] [blame] | 426 | void CodeGenerator::CreateUnresolvedFieldLocationSummary( |
| 427 | HInstruction* field_access, |
| 428 | Primitive::Type field_type, |
| 429 | const FieldAccessCallingConvention& calling_convention) { |
| 430 | bool is_instance = field_access->IsUnresolvedInstanceFieldGet() |
| 431 | || field_access->IsUnresolvedInstanceFieldSet(); |
| 432 | bool is_get = field_access->IsUnresolvedInstanceFieldGet() |
| 433 | || field_access->IsUnresolvedStaticFieldGet(); |
| 434 | |
| 435 | ArenaAllocator* allocator = field_access->GetBlock()->GetGraph()->GetArena(); |
| 436 | LocationSummary* locations = |
| 437 | new (allocator) LocationSummary(field_access, LocationSummary::kCall); |
| 438 | |
| 439 | locations->AddTemp(calling_convention.GetFieldIndexLocation()); |
| 440 | |
| 441 | if (is_instance) { |
| 442 | // Add the `this` object for instance field accesses. |
| 443 | locations->SetInAt(0, calling_convention.GetObjectLocation()); |
| 444 | } |
| 445 | |
| 446 | // Note that pSetXXStatic/pGetXXStatic always takes/returns an int or int64 |
| 447 | // regardless of the the type. Because of that we forced to special case |
| 448 | // the access to floating point values. |
| 449 | if (is_get) { |
| 450 | if (Primitive::IsFloatingPointType(field_type)) { |
| 451 | // The return value will be stored in regular registers while register |
| 452 | // allocator expects it in a floating point register. |
| 453 | // Note We don't need to request additional temps because the return |
| 454 | // register(s) are already blocked due the call and they may overlap with |
| 455 | // the input or field index. |
| 456 | // The transfer between the two will be done at codegen level. |
| 457 | locations->SetOut(calling_convention.GetFpuLocation(field_type)); |
| 458 | } else { |
| 459 | locations->SetOut(calling_convention.GetReturnLocation(field_type)); |
| 460 | } |
| 461 | } else { |
| 462 | size_t set_index = is_instance ? 1 : 0; |
| 463 | if (Primitive::IsFloatingPointType(field_type)) { |
| 464 | // The set value comes from a float location while the calling convention |
| 465 | // expects it in a regular register location. Allocate a temp for it and |
| 466 | // make the transfer at codegen. |
| 467 | AddLocationAsTemp(calling_convention.GetSetValueLocation(field_type, is_instance), locations); |
| 468 | locations->SetInAt(set_index, calling_convention.GetFpuLocation(field_type)); |
| 469 | } else { |
| 470 | locations->SetInAt(set_index, |
| 471 | calling_convention.GetSetValueLocation(field_type, is_instance)); |
| 472 | } |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | void CodeGenerator::GenerateUnresolvedFieldAccess( |
| 477 | HInstruction* field_access, |
| 478 | Primitive::Type field_type, |
| 479 | uint32_t field_index, |
| 480 | uint32_t dex_pc, |
| 481 | const FieldAccessCallingConvention& calling_convention) { |
| 482 | LocationSummary* locations = field_access->GetLocations(); |
| 483 | |
| 484 | MoveConstant(locations->GetTemp(0), field_index); |
| 485 | |
| 486 | bool is_instance = field_access->IsUnresolvedInstanceFieldGet() |
| 487 | || field_access->IsUnresolvedInstanceFieldSet(); |
| 488 | bool is_get = field_access->IsUnresolvedInstanceFieldGet() |
| 489 | || field_access->IsUnresolvedStaticFieldGet(); |
| 490 | |
| 491 | if (!is_get && Primitive::IsFloatingPointType(field_type)) { |
| 492 | // Copy the float value to be set into the calling convention register. |
| 493 | // Note that using directly the temp location is problematic as we don't |
| 494 | // support temp register pairs. To avoid boilerplate conversion code, use |
| 495 | // the location from the calling convention. |
| 496 | MoveLocation(calling_convention.GetSetValueLocation(field_type, is_instance), |
| 497 | locations->InAt(is_instance ? 1 : 0), |
| 498 | (Primitive::Is64BitType(field_type) ? Primitive::kPrimLong : Primitive::kPrimInt)); |
| 499 | } |
| 500 | |
| 501 | QuickEntrypointEnum entrypoint = kQuickSet8Static; // Initialize to anything to avoid warnings. |
| 502 | switch (field_type) { |
| 503 | case Primitive::kPrimBoolean: |
| 504 | entrypoint = is_instance |
| 505 | ? (is_get ? kQuickGetBooleanInstance : kQuickSet8Instance) |
| 506 | : (is_get ? kQuickGetBooleanStatic : kQuickSet8Static); |
| 507 | break; |
| 508 | case Primitive::kPrimByte: |
| 509 | entrypoint = is_instance |
| 510 | ? (is_get ? kQuickGetByteInstance : kQuickSet8Instance) |
| 511 | : (is_get ? kQuickGetByteStatic : kQuickSet8Static); |
| 512 | break; |
| 513 | case Primitive::kPrimShort: |
| 514 | entrypoint = is_instance |
| 515 | ? (is_get ? kQuickGetShortInstance : kQuickSet16Instance) |
| 516 | : (is_get ? kQuickGetShortStatic : kQuickSet16Static); |
| 517 | break; |
| 518 | case Primitive::kPrimChar: |
| 519 | entrypoint = is_instance |
| 520 | ? (is_get ? kQuickGetCharInstance : kQuickSet16Instance) |
| 521 | : (is_get ? kQuickGetCharStatic : kQuickSet16Static); |
| 522 | break; |
| 523 | case Primitive::kPrimInt: |
| 524 | case Primitive::kPrimFloat: |
| 525 | entrypoint = is_instance |
| 526 | ? (is_get ? kQuickGet32Instance : kQuickSet32Instance) |
| 527 | : (is_get ? kQuickGet32Static : kQuickSet32Static); |
| 528 | break; |
| 529 | case Primitive::kPrimNot: |
| 530 | entrypoint = is_instance |
| 531 | ? (is_get ? kQuickGetObjInstance : kQuickSetObjInstance) |
| 532 | : (is_get ? kQuickGetObjStatic : kQuickSetObjStatic); |
| 533 | break; |
| 534 | case Primitive::kPrimLong: |
| 535 | case Primitive::kPrimDouble: |
| 536 | entrypoint = is_instance |
| 537 | ? (is_get ? kQuickGet64Instance : kQuickSet64Instance) |
| 538 | : (is_get ? kQuickGet64Static : kQuickSet64Static); |
| 539 | break; |
| 540 | default: |
| 541 | LOG(FATAL) << "Invalid type " << field_type; |
| 542 | } |
| 543 | InvokeRuntime(entrypoint, field_access, dex_pc, nullptr); |
| 544 | |
| 545 | if (is_get && Primitive::IsFloatingPointType(field_type)) { |
| 546 | MoveLocation(locations->Out(), calling_convention.GetReturnLocation(field_type), field_type); |
| 547 | } |
| 548 | } |
| 549 | |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame^] | 550 | // TODO: Remove argument `code_generator_supports_read_barrier` when |
| 551 | // all code generators have read barrier support. |
Calin Juravle | 98893e1 | 2015-10-02 21:05:03 +0100 | [diff] [blame] | 552 | void CodeGenerator::CreateLoadClassLocationSummary(HLoadClass* cls, |
| 553 | Location runtime_type_index_location, |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame^] | 554 | Location runtime_return_location, |
| 555 | bool code_generator_supports_read_barrier) { |
Calin Juravle | 98893e1 | 2015-10-02 21:05:03 +0100 | [diff] [blame] | 556 | ArenaAllocator* allocator = cls->GetBlock()->GetGraph()->GetArena(); |
| 557 | LocationSummary::CallKind call_kind = cls->NeedsAccessCheck() |
| 558 | ? LocationSummary::kCall |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame^] | 559 | : (((code_generator_supports_read_barrier && kEmitCompilerReadBarrier) || |
| 560 | cls->CanCallRuntime()) |
| 561 | ? LocationSummary::kCallOnSlowPath |
| 562 | : LocationSummary::kNoCall); |
Calin Juravle | 98893e1 | 2015-10-02 21:05:03 +0100 | [diff] [blame] | 563 | LocationSummary* locations = new (allocator) LocationSummary(cls, call_kind); |
Calin Juravle | 98893e1 | 2015-10-02 21:05:03 +0100 | [diff] [blame] | 564 | if (cls->NeedsAccessCheck()) { |
Calin Juravle | 580b609 | 2015-10-06 17:35:58 +0100 | [diff] [blame] | 565 | locations->SetInAt(0, Location::NoLocation()); |
Calin Juravle | 98893e1 | 2015-10-02 21:05:03 +0100 | [diff] [blame] | 566 | locations->AddTemp(runtime_type_index_location); |
| 567 | locations->SetOut(runtime_return_location); |
| 568 | } else { |
Calin Juravle | 580b609 | 2015-10-06 17:35:58 +0100 | [diff] [blame] | 569 | locations->SetInAt(0, Location::RequiresRegister()); |
Calin Juravle | 98893e1 | 2015-10-02 21:05:03 +0100 | [diff] [blame] | 570 | locations->SetOut(Location::RequiresRegister()); |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | |
Mark Mendell | 5f87418 | 2015-03-04 15:42:45 -0500 | [diff] [blame] | 575 | void CodeGenerator::BlockIfInRegister(Location location, bool is_out) const { |
| 576 | // The DCHECKS below check that a register is not specified twice in |
| 577 | // the summary. The out location can overlap with an input, so we need |
| 578 | // to special case it. |
| 579 | if (location.IsRegister()) { |
| 580 | DCHECK(is_out || !blocked_core_registers_[location.reg()]); |
| 581 | blocked_core_registers_[location.reg()] = true; |
| 582 | } else if (location.IsFpuRegister()) { |
| 583 | DCHECK(is_out || !blocked_fpu_registers_[location.reg()]); |
| 584 | blocked_fpu_registers_[location.reg()] = true; |
| 585 | } else if (location.IsFpuRegisterPair()) { |
| 586 | DCHECK(is_out || !blocked_fpu_registers_[location.AsFpuRegisterPairLow<int>()]); |
| 587 | blocked_fpu_registers_[location.AsFpuRegisterPairLow<int>()] = true; |
| 588 | DCHECK(is_out || !blocked_fpu_registers_[location.AsFpuRegisterPairHigh<int>()]); |
| 589 | blocked_fpu_registers_[location.AsFpuRegisterPairHigh<int>()] = true; |
| 590 | } else if (location.IsRegisterPair()) { |
| 591 | DCHECK(is_out || !blocked_core_registers_[location.AsRegisterPairLow<int>()]); |
| 592 | blocked_core_registers_[location.AsRegisterPairLow<int>()] = true; |
| 593 | DCHECK(is_out || !blocked_core_registers_[location.AsRegisterPairHigh<int>()]); |
| 594 | blocked_core_registers_[location.AsRegisterPairHigh<int>()] = true; |
| 595 | } |
| 596 | } |
| 597 | |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 598 | void CodeGenerator::AllocateRegistersLocally(HInstruction* instruction) const { |
| 599 | LocationSummary* locations = instruction->GetLocations(); |
| 600 | if (locations == nullptr) return; |
| 601 | |
Nicolas Geoffray | 71175b7 | 2014-10-09 22:13:55 +0100 | [diff] [blame] | 602 | for (size_t i = 0, e = GetNumberOfCoreRegisters(); i < e; ++i) { |
| 603 | blocked_core_registers_[i] = false; |
| 604 | } |
| 605 | |
| 606 | for (size_t i = 0, e = GetNumberOfFloatingPointRegisters(); i < e; ++i) { |
| 607 | blocked_fpu_registers_[i] = false; |
| 608 | } |
| 609 | |
| 610 | for (size_t i = 0, e = number_of_register_pairs_; i < e; ++i) { |
| 611 | blocked_register_pairs_[i] = false; |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | // Mark all fixed input, temp and output registers as used. |
| 615 | for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) { |
Mark Mendell | 5f87418 | 2015-03-04 15:42:45 -0500 | [diff] [blame] | 616 | BlockIfInRegister(locations->InAt(i)); |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) { |
| 620 | Location loc = locations->GetTemp(i); |
Mark Mendell | 5f87418 | 2015-03-04 15:42:45 -0500 | [diff] [blame] | 621 | BlockIfInRegister(loc); |
| 622 | } |
| 623 | Location result_location = locations->Out(); |
| 624 | if (locations->OutputCanOverlapWithInputs()) { |
| 625 | BlockIfInRegister(result_location, /* is_out */ true); |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 626 | } |
| 627 | |
Mark Mendell | 5f87418 | 2015-03-04 15:42:45 -0500 | [diff] [blame] | 628 | SetupBlockedRegisters(/* is_baseline */ true); |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 629 | |
| 630 | // Allocate all unallocated input locations. |
| 631 | for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) { |
| 632 | Location loc = locations->InAt(i); |
| 633 | HInstruction* input = instruction->InputAt(i); |
| 634 | if (loc.IsUnallocated()) { |
Nicolas Geoffray | 56b9ee6 | 2014-10-09 11:47:51 +0100 | [diff] [blame] | 635 | if ((loc.GetPolicy() == Location::kRequiresRegister) |
| 636 | || (loc.GetPolicy() == Location::kRequiresFpuRegister)) { |
Nicolas Geoffray | 71175b7 | 2014-10-09 22:13:55 +0100 | [diff] [blame] | 637 | loc = AllocateFreeRegister(input->GetType()); |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 638 | } else { |
| 639 | DCHECK_EQ(loc.GetPolicy(), Location::kAny); |
| 640 | HLoadLocal* load = input->AsLoadLocal(); |
| 641 | if (load != nullptr) { |
| 642 | loc = GetStackLocation(load); |
| 643 | } else { |
Nicolas Geoffray | 71175b7 | 2014-10-09 22:13:55 +0100 | [diff] [blame] | 644 | loc = AllocateFreeRegister(input->GetType()); |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 645 | } |
| 646 | } |
| 647 | locations->SetInAt(i, loc); |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | // Allocate all unallocated temp locations. |
| 652 | for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) { |
| 653 | Location loc = locations->GetTemp(i); |
| 654 | if (loc.IsUnallocated()) { |
Roland Levillain | 647b9ed | 2014-11-27 12:06:00 +0000 | [diff] [blame] | 655 | switch (loc.GetPolicy()) { |
| 656 | case Location::kRequiresRegister: |
| 657 | // Allocate a core register (large enough to fit a 32-bit integer). |
| 658 | loc = AllocateFreeRegister(Primitive::kPrimInt); |
| 659 | break; |
| 660 | |
| 661 | case Location::kRequiresFpuRegister: |
| 662 | // Allocate a core register (large enough to fit a 64-bit double). |
| 663 | loc = AllocateFreeRegister(Primitive::kPrimDouble); |
| 664 | break; |
| 665 | |
| 666 | default: |
| 667 | LOG(FATAL) << "Unexpected policy for temporary location " |
| 668 | << loc.GetPolicy(); |
| 669 | } |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 670 | locations->SetTempAt(i, loc); |
| 671 | } |
| 672 | } |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 673 | if (result_location.IsUnallocated()) { |
| 674 | switch (result_location.GetPolicy()) { |
| 675 | case Location::kAny: |
| 676 | case Location::kRequiresRegister: |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 677 | case Location::kRequiresFpuRegister: |
Nicolas Geoffray | 71175b7 | 2014-10-09 22:13:55 +0100 | [diff] [blame] | 678 | result_location = AllocateFreeRegister(instruction->GetType()); |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 679 | break; |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 680 | case Location::kSameAsFirstInput: |
| 681 | result_location = locations->InAt(0); |
| 682 | break; |
| 683 | } |
Nicolas Geoffray | 829280c | 2015-01-28 10:20:37 +0000 | [diff] [blame] | 684 | locations->UpdateOut(result_location); |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 685 | } |
| 686 | } |
| 687 | |
Nicolas Geoffray | c0572a4 | 2015-02-06 14:35:25 +0000 | [diff] [blame] | 688 | void CodeGenerator::InitLocationsBaseline(HInstruction* instruction) { |
| 689 | AllocateLocations(instruction); |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 690 | if (instruction->GetLocations() == nullptr) { |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 691 | if (instruction->IsTemporary()) { |
| 692 | HInstruction* previous = instruction->GetPrevious(); |
| 693 | Location temp_location = GetTemporaryLocation(instruction->AsTemporary()); |
| 694 | Move(previous, temp_location, instruction); |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 695 | } |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 696 | return; |
| 697 | } |
| 698 | AllocateRegistersLocally(instruction); |
| 699 | for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 700 | Location location = instruction->GetLocations()->InAt(i); |
Nicolas Geoffray | f43083d | 2014-11-07 10:48:10 +0000 | [diff] [blame] | 701 | HInstruction* input = instruction->InputAt(i); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 702 | if (location.IsValid()) { |
| 703 | // Move the input to the desired location. |
Nicolas Geoffray | f43083d | 2014-11-07 10:48:10 +0000 | [diff] [blame] | 704 | if (input->GetNext()->IsTemporary()) { |
| 705 | // If the input was stored in a temporary, use that temporary to |
| 706 | // perform the move. |
| 707 | Move(input->GetNext(), location, instruction); |
| 708 | } else { |
| 709 | Move(input, location, instruction); |
| 710 | } |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 711 | } |
| 712 | } |
| 713 | } |
| 714 | |
Nicolas Geoffray | c0572a4 | 2015-02-06 14:35:25 +0000 | [diff] [blame] | 715 | void CodeGenerator::AllocateLocations(HInstruction* instruction) { |
| 716 | instruction->Accept(GetLocationBuilder()); |
Alexandre Rames | 88c13cd | 2015-04-14 17:35:39 +0100 | [diff] [blame] | 717 | DCHECK(CheckTypeConsistency(instruction)); |
Nicolas Geoffray | c0572a4 | 2015-02-06 14:35:25 +0000 | [diff] [blame] | 718 | LocationSummary* locations = instruction->GetLocations(); |
| 719 | if (!instruction->IsSuspendCheckEntry()) { |
| 720 | if (locations != nullptr && locations->CanCall()) { |
| 721 | MarkNotLeaf(); |
| 722 | } |
| 723 | if (instruction->NeedsCurrentMethod()) { |
| 724 | SetRequiresCurrentMethod(); |
| 725 | } |
| 726 | } |
| 727 | } |
| 728 | |
Serban Constantinescu | ecc4366 | 2015-08-13 13:33:12 +0100 | [diff] [blame] | 729 | void CodeGenerator::MaybeRecordStat(MethodCompilationStat compilation_stat, size_t count) const { |
| 730 | if (stats_ != nullptr) { |
| 731 | stats_->RecordStat(compilation_stat, count); |
| 732 | } |
| 733 | } |
| 734 | |
Nicolas Geoffray | 12df9eb | 2015-01-09 14:53:50 +0000 | [diff] [blame] | 735 | CodeGenerator* CodeGenerator::Create(HGraph* graph, |
Calin Juravle | 3416601 | 2014-12-19 17:22:29 +0000 | [diff] [blame] | 736 | InstructionSet instruction_set, |
Calin Juravle | cd6dffe | 2015-01-08 17:35:35 +0000 | [diff] [blame] | 737 | const InstructionSetFeatures& isa_features, |
Serban Constantinescu | ecc4366 | 2015-08-13 13:33:12 +0100 | [diff] [blame] | 738 | const CompilerOptions& compiler_options, |
| 739 | OptimizingCompilerStats* stats) { |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 740 | switch (instruction_set) { |
Alex Light | 50fa993 | 2015-08-10 15:30:07 -0700 | [diff] [blame] | 741 | #ifdef ART_ENABLE_CODEGEN_arm |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 742 | case kArm: |
| 743 | case kThumb2: { |
Nicolas Geoffray | 12df9eb | 2015-01-09 14:53:50 +0000 | [diff] [blame] | 744 | return new arm::CodeGeneratorARM(graph, |
Serban Constantinescu | ecc4366 | 2015-08-13 13:33:12 +0100 | [diff] [blame] | 745 | *isa_features.AsArmInstructionSetFeatures(), |
| 746 | compiler_options, |
| 747 | stats); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 748 | } |
Alex Light | 50fa993 | 2015-08-10 15:30:07 -0700 | [diff] [blame] | 749 | #endif |
| 750 | #ifdef ART_ENABLE_CODEGEN_arm64 |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 751 | case kArm64: { |
Serban Constantinescu | 579885a | 2015-02-22 20:51:33 +0000 | [diff] [blame] | 752 | return new arm64::CodeGeneratorARM64(graph, |
Serban Constantinescu | ecc4366 | 2015-08-13 13:33:12 +0100 | [diff] [blame] | 753 | *isa_features.AsArm64InstructionSetFeatures(), |
| 754 | compiler_options, |
| 755 | stats); |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 756 | } |
Alex Light | 50fa993 | 2015-08-10 15:30:07 -0700 | [diff] [blame] | 757 | #endif |
| 758 | #ifdef ART_ENABLE_CODEGEN_mips |
Goran Jakovljevic | f652cec | 2015-08-25 16:11:42 +0200 | [diff] [blame] | 759 | case kMips: { |
| 760 | return new mips::CodeGeneratorMIPS(graph, |
| 761 | *isa_features.AsMipsInstructionSetFeatures(), |
| 762 | compiler_options, |
| 763 | stats); |
| 764 | } |
Alex Light | 50fa993 | 2015-08-10 15:30:07 -0700 | [diff] [blame] | 765 | #endif |
| 766 | #ifdef ART_ENABLE_CODEGEN_mips64 |
Alexey Frunze | 4dda337 | 2015-06-01 18:31:49 -0700 | [diff] [blame] | 767 | case kMips64: { |
| 768 | return new mips64::CodeGeneratorMIPS64(graph, |
Serban Constantinescu | ecc4366 | 2015-08-13 13:33:12 +0100 | [diff] [blame] | 769 | *isa_features.AsMips64InstructionSetFeatures(), |
| 770 | compiler_options, |
| 771 | stats); |
Alexey Frunze | 4dda337 | 2015-06-01 18:31:49 -0700 | [diff] [blame] | 772 | } |
Alex Light | 50fa993 | 2015-08-10 15:30:07 -0700 | [diff] [blame] | 773 | #endif |
| 774 | #ifdef ART_ENABLE_CODEGEN_x86 |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 775 | case kX86: { |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 776 | return new x86::CodeGeneratorX86(graph, |
Serban Constantinescu | ecc4366 | 2015-08-13 13:33:12 +0100 | [diff] [blame] | 777 | *isa_features.AsX86InstructionSetFeatures(), |
| 778 | compiler_options, |
| 779 | stats); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 780 | } |
Alex Light | 50fa993 | 2015-08-10 15:30:07 -0700 | [diff] [blame] | 781 | #endif |
| 782 | #ifdef ART_ENABLE_CODEGEN_x86_64 |
Dmitry Petrochenko | 6a58cb1 | 2014-04-02 17:27:59 +0700 | [diff] [blame] | 783 | case kX86_64: { |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 784 | return new x86_64::CodeGeneratorX86_64(graph, |
Serban Constantinescu | ecc4366 | 2015-08-13 13:33:12 +0100 | [diff] [blame] | 785 | *isa_features.AsX86_64InstructionSetFeatures(), |
| 786 | compiler_options, |
| 787 | stats); |
Dmitry Petrochenko | 6a58cb1 | 2014-04-02 17:27:59 +0700 | [diff] [blame] | 788 | } |
Alex Light | 50fa993 | 2015-08-10 15:30:07 -0700 | [diff] [blame] | 789 | #endif |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 790 | default: |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 791 | return nullptr; |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 792 | } |
| 793 | } |
| 794 | |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 795 | void CodeGenerator::BuildNativeGCMap( |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 796 | ArenaVector<uint8_t>* data, const CompilerDriver& compiler_driver) const { |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 797 | const std::vector<uint8_t>& gc_map_raw = |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 798 | compiler_driver.GetVerifiedMethod(&GetGraph()->GetDexFile(), GetGraph()->GetMethodIdx()) |
| 799 | ->GetDexGcMap(); |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 800 | verifier::DexPcToReferenceMap dex_gc_map(&(gc_map_raw)[0]); |
| 801 | |
Vladimir Marko | bd8c725 | 2015-06-12 10:06:32 +0100 | [diff] [blame] | 802 | uint32_t max_native_offset = stack_map_stream_.ComputeMaxNativePcOffset(); |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 803 | |
Vladimir Marko | bd8c725 | 2015-06-12 10:06:32 +0100 | [diff] [blame] | 804 | size_t num_stack_maps = stack_map_stream_.GetNumberOfStackMaps(); |
| 805 | GcMapBuilder builder(data, num_stack_maps, max_native_offset, dex_gc_map.RegWidth()); |
| 806 | for (size_t i = 0; i != num_stack_maps; ++i) { |
| 807 | const StackMapStream::StackMapEntry& stack_map_entry = stack_map_stream_.GetStackMap(i); |
| 808 | uint32_t native_offset = stack_map_entry.native_pc_offset; |
| 809 | uint32_t dex_pc = stack_map_entry.dex_pc; |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 810 | const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false); |
Jean Christophe Beyler | 0ada95d | 2014-12-04 11:20:20 -0800 | [diff] [blame] | 811 | CHECK(references != nullptr) << "Missing ref for dex pc 0x" << std::hex << dex_pc; |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 812 | builder.AddEntry(native_offset, references); |
| 813 | } |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 814 | } |
| 815 | |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 816 | void CodeGenerator::BuildMappingTable(ArenaVector<uint8_t>* data) const { |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 817 | uint32_t pc2dex_data_size = 0u; |
Vladimir Marko | bd8c725 | 2015-06-12 10:06:32 +0100 | [diff] [blame] | 818 | uint32_t pc2dex_entries = stack_map_stream_.GetNumberOfStackMaps(); |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 819 | uint32_t pc2dex_offset = 0u; |
| 820 | int32_t pc2dex_dalvik_offset = 0; |
| 821 | uint32_t dex2pc_data_size = 0u; |
| 822 | uint32_t dex2pc_entries = 0u; |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 823 | uint32_t dex2pc_offset = 0u; |
| 824 | int32_t dex2pc_dalvik_offset = 0; |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 825 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 826 | for (size_t i = 0; i < pc2dex_entries; i++) { |
Vladimir Marko | bd8c725 | 2015-06-12 10:06:32 +0100 | [diff] [blame] | 827 | const StackMapStream::StackMapEntry& stack_map_entry = stack_map_stream_.GetStackMap(i); |
| 828 | pc2dex_data_size += UnsignedLeb128Size(stack_map_entry.native_pc_offset - pc2dex_offset); |
| 829 | pc2dex_data_size += SignedLeb128Size(stack_map_entry.dex_pc - pc2dex_dalvik_offset); |
| 830 | pc2dex_offset = stack_map_entry.native_pc_offset; |
| 831 | pc2dex_dalvik_offset = stack_map_entry.dex_pc; |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 832 | } |
| 833 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 834 | // Walk over the blocks and find which ones correspond to catch block entries. |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 835 | for (HBasicBlock* block : graph_->GetBlocks()) { |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 836 | if (block->IsCatchBlock()) { |
| 837 | intptr_t native_pc = GetAddressOf(block); |
| 838 | ++dex2pc_entries; |
| 839 | dex2pc_data_size += UnsignedLeb128Size(native_pc - dex2pc_offset); |
| 840 | dex2pc_data_size += SignedLeb128Size(block->GetDexPc() - dex2pc_dalvik_offset); |
| 841 | dex2pc_offset = native_pc; |
| 842 | dex2pc_dalvik_offset = block->GetDexPc(); |
| 843 | } |
| 844 | } |
| 845 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 846 | uint32_t total_entries = pc2dex_entries + dex2pc_entries; |
| 847 | uint32_t hdr_data_size = UnsignedLeb128Size(total_entries) + UnsignedLeb128Size(pc2dex_entries); |
| 848 | uint32_t data_size = hdr_data_size + pc2dex_data_size + dex2pc_data_size; |
| 849 | data->resize(data_size); |
| 850 | |
| 851 | uint8_t* data_ptr = &(*data)[0]; |
| 852 | uint8_t* write_pos = data_ptr; |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 853 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 854 | write_pos = EncodeUnsignedLeb128(write_pos, total_entries); |
| 855 | write_pos = EncodeUnsignedLeb128(write_pos, pc2dex_entries); |
| 856 | DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size); |
| 857 | uint8_t* write_pos2 = write_pos + pc2dex_data_size; |
| 858 | |
| 859 | pc2dex_offset = 0u; |
| 860 | pc2dex_dalvik_offset = 0u; |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 861 | dex2pc_offset = 0u; |
| 862 | dex2pc_dalvik_offset = 0u; |
| 863 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 864 | for (size_t i = 0; i < pc2dex_entries; i++) { |
Vladimir Marko | bd8c725 | 2015-06-12 10:06:32 +0100 | [diff] [blame] | 865 | const StackMapStream::StackMapEntry& stack_map_entry = stack_map_stream_.GetStackMap(i); |
| 866 | DCHECK(pc2dex_offset <= stack_map_entry.native_pc_offset); |
| 867 | write_pos = EncodeUnsignedLeb128(write_pos, stack_map_entry.native_pc_offset - pc2dex_offset); |
| 868 | write_pos = EncodeSignedLeb128(write_pos, stack_map_entry.dex_pc - pc2dex_dalvik_offset); |
| 869 | pc2dex_offset = stack_map_entry.native_pc_offset; |
| 870 | pc2dex_dalvik_offset = stack_map_entry.dex_pc; |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 871 | } |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 872 | |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 873 | for (HBasicBlock* block : graph_->GetBlocks()) { |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 874 | if (block->IsCatchBlock()) { |
| 875 | intptr_t native_pc = GetAddressOf(block); |
| 876 | write_pos2 = EncodeUnsignedLeb128(write_pos2, native_pc - dex2pc_offset); |
| 877 | write_pos2 = EncodeSignedLeb128(write_pos2, block->GetDexPc() - dex2pc_dalvik_offset); |
| 878 | dex2pc_offset = native_pc; |
| 879 | dex2pc_dalvik_offset = block->GetDexPc(); |
| 880 | } |
| 881 | } |
| 882 | |
| 883 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 884 | DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size + pc2dex_data_size); |
| 885 | DCHECK_EQ(static_cast<size_t>(write_pos2 - data_ptr), data_size); |
| 886 | |
| 887 | if (kIsDebugBuild) { |
| 888 | // Verify the encoded table holds the expected data. |
| 889 | MappingTable table(data_ptr); |
| 890 | CHECK_EQ(table.TotalSize(), total_entries); |
| 891 | CHECK_EQ(table.PcToDexSize(), pc2dex_entries); |
| 892 | auto it = table.PcToDexBegin(); |
| 893 | auto it2 = table.DexToPcBegin(); |
| 894 | for (size_t i = 0; i < pc2dex_entries; i++) { |
Vladimir Marko | bd8c725 | 2015-06-12 10:06:32 +0100 | [diff] [blame] | 895 | const StackMapStream::StackMapEntry& stack_map_entry = stack_map_stream_.GetStackMap(i); |
| 896 | CHECK_EQ(stack_map_entry.native_pc_offset, it.NativePcOffset()); |
| 897 | CHECK_EQ(stack_map_entry.dex_pc, it.DexPc()); |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 898 | ++it; |
| 899 | } |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 900 | for (HBasicBlock* block : graph_->GetBlocks()) { |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 901 | if (block->IsCatchBlock()) { |
| 902 | CHECK_EQ(GetAddressOf(block), it2.NativePcOffset()); |
| 903 | CHECK_EQ(block->GetDexPc(), it2.DexPc()); |
| 904 | ++it2; |
| 905 | } |
| 906 | } |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 907 | CHECK(it == table.PcToDexEnd()); |
| 908 | CHECK(it2 == table.DexToPcEnd()); |
| 909 | } |
| 910 | } |
| 911 | |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 912 | void CodeGenerator::BuildVMapTable(ArenaVector<uint8_t>* data) const { |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 913 | Leb128Encoder<ArenaVector<uint8_t>> vmap_encoder(data); |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 914 | // We currently don't use callee-saved registers. |
| 915 | size_t size = 0 + 1 /* marker */ + 0; |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 916 | vmap_encoder.Reserve(size + 1u); // All values are likely to be one byte in ULEB128 (<128). |
| 917 | vmap_encoder.PushBackUnsigned(size); |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 918 | vmap_encoder.PushBackUnsigned(VmapTable::kAdjustedFpMarker); |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 919 | } |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 920 | |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 921 | size_t CodeGenerator::ComputeStackMapsSize() { |
| 922 | return stack_map_stream_.PrepareForFillIn(); |
| 923 | } |
| 924 | |
| 925 | void CodeGenerator::BuildStackMaps(MemoryRegion region) { |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 926 | stack_map_stream_.FillIn(region); |
| 927 | } |
| 928 | |
Yevgeny Rouban | 2a7c1ef | 2015-07-22 18:36:24 +0600 | [diff] [blame] | 929 | void CodeGenerator::RecordNativeDebugInfo(uint32_t dex_pc, |
| 930 | uintptr_t native_pc_begin, |
| 931 | uintptr_t native_pc_end) { |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 932 | if (compiler_options_.GetGenerateDebugInfo() && |
| 933 | dex_pc != kNoDexPc && |
| 934 | native_pc_begin != native_pc_end) { |
| 935 | src_map_.push_back(SrcMapElem({static_cast<uint32_t>(native_pc_begin), |
| 936 | static_cast<int32_t>(dex_pc)})); |
Yevgeny Rouban | 2a7c1ef | 2015-07-22 18:36:24 +0600 | [diff] [blame] | 937 | } |
| 938 | } |
| 939 | |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 940 | void CodeGenerator::RecordPcInfo(HInstruction* instruction, |
| 941 | uint32_t dex_pc, |
| 942 | SlowPathCode* slow_path) { |
Calin Juravle | d2ec87d | 2014-12-08 14:24:46 +0000 | [diff] [blame] | 943 | if (instruction != nullptr) { |
Alexey Frunze | 4dda337 | 2015-06-01 18:31:49 -0700 | [diff] [blame] | 944 | // The code generated for some type conversions and comparisons |
| 945 | // may call the runtime, thus normally requiring a subsequent |
| 946 | // call to this method. However, the method verifier does not |
| 947 | // produce PC information for certain instructions, which are |
| 948 | // considered "atomic" (they cannot join a GC). |
Roland Levillain | 624279f | 2014-12-04 11:54:28 +0000 | [diff] [blame] | 949 | // Therefore we do not currently record PC information for such |
| 950 | // instructions. As this may change later, we added this special |
| 951 | // case so that code generators may nevertheless call |
| 952 | // CodeGenerator::RecordPcInfo without triggering an error in |
| 953 | // CodeGenerator::BuildNativeGCMap ("Missing ref for dex pc 0x") |
| 954 | // thereafter. |
Alexey Frunze | 4dda337 | 2015-06-01 18:31:49 -0700 | [diff] [blame] | 955 | if (instruction->IsTypeConversion() || instruction->IsCompare()) { |
Calin Juravle | d2ec87d | 2014-12-08 14:24:46 +0000 | [diff] [blame] | 956 | return; |
| 957 | } |
| 958 | if (instruction->IsRem()) { |
| 959 | Primitive::Type type = instruction->AsRem()->GetResultType(); |
| 960 | if ((type == Primitive::kPrimFloat) || (type == Primitive::kPrimDouble)) { |
| 961 | return; |
| 962 | } |
| 963 | } |
Roland Levillain | 624279f | 2014-12-04 11:54:28 +0000 | [diff] [blame] | 964 | } |
| 965 | |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 966 | uint32_t outer_dex_pc = dex_pc; |
| 967 | uint32_t outer_environment_size = 0; |
| 968 | uint32_t inlining_depth = 0; |
| 969 | if (instruction != nullptr) { |
| 970 | for (HEnvironment* environment = instruction->GetEnvironment(); |
| 971 | environment != nullptr; |
| 972 | environment = environment->GetParent()) { |
| 973 | outer_dex_pc = environment->GetDexPc(); |
| 974 | outer_environment_size = environment->Size(); |
| 975 | if (environment != instruction->GetEnvironment()) { |
| 976 | inlining_depth++; |
| 977 | } |
| 978 | } |
| 979 | } |
| 980 | |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 981 | // Collect PC infos for the mapping table. |
Vladimir Marko | bd8c725 | 2015-06-12 10:06:32 +0100 | [diff] [blame] | 982 | uint32_t native_pc = GetAssembler()->CodeSize(); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 983 | |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 984 | if (instruction == nullptr) { |
| 985 | // For stack overflow checks. |
Vladimir Marko | bd8c725 | 2015-06-12 10:06:32 +0100 | [diff] [blame] | 986 | 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] | 987 | stack_map_stream_.EndStackMapEntry(); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 988 | return; |
| 989 | } |
| 990 | LocationSummary* locations = instruction->GetLocations(); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 991 | |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 992 | uint32_t register_mask = locations->GetRegisterMask(); |
| 993 | if (locations->OnlyCallsOnSlowPath()) { |
| 994 | // In case of slow path, we currently set the location of caller-save registers |
| 995 | // to register (instead of their stack location when pushed before the slow-path |
| 996 | // call). Therefore register_mask contains both callee-save and caller-save |
| 997 | // registers that hold objects. We must remove the caller-save from the mask, since |
| 998 | // they will be overwritten by the callee. |
| 999 | register_mask &= core_callee_save_mask_; |
| 1000 | } |
| 1001 | // The register mask must be a subset of callee-save registers. |
| 1002 | DCHECK_EQ(register_mask & core_callee_save_mask_, register_mask); |
Vladimir Marko | bd8c725 | 2015-06-12 10:06:32 +0100 | [diff] [blame] | 1003 | stack_map_stream_.BeginStackMapEntry(outer_dex_pc, |
| 1004 | native_pc, |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 1005 | register_mask, |
| 1006 | locations->GetStackMask(), |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 1007 | outer_environment_size, |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 1008 | inlining_depth); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 1009 | |
| 1010 | EmitEnvironment(instruction->GetEnvironment(), slow_path); |
| 1011 | stack_map_stream_.EndStackMapEntry(); |
| 1012 | } |
| 1013 | |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 1014 | void CodeGenerator::RecordCatchBlockInfo() { |
| 1015 | ArenaAllocator* arena = graph_->GetArena(); |
| 1016 | |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 1017 | for (HBasicBlock* block : *block_order_) { |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 1018 | if (!block->IsCatchBlock()) { |
| 1019 | continue; |
| 1020 | } |
| 1021 | |
| 1022 | uint32_t dex_pc = block->GetDexPc(); |
| 1023 | uint32_t num_vregs = graph_->GetNumberOfVRegs(); |
| 1024 | uint32_t inlining_depth = 0; // Inlining of catch blocks is not supported at the moment. |
| 1025 | uint32_t native_pc = GetAddressOf(block); |
| 1026 | uint32_t register_mask = 0; // Not used. |
| 1027 | |
| 1028 | // The stack mask is not used, so we leave it empty. |
| 1029 | ArenaBitVector* stack_mask = new (arena) ArenaBitVector(arena, 0, /* expandable */ true); |
| 1030 | |
| 1031 | stack_map_stream_.BeginStackMapEntry(dex_pc, |
| 1032 | native_pc, |
| 1033 | register_mask, |
| 1034 | stack_mask, |
| 1035 | num_vregs, |
| 1036 | inlining_depth); |
| 1037 | |
| 1038 | HInstruction* current_phi = block->GetFirstPhi(); |
| 1039 | for (size_t vreg = 0; vreg < num_vregs; ++vreg) { |
| 1040 | while (current_phi != nullptr && current_phi->AsPhi()->GetRegNumber() < vreg) { |
| 1041 | HInstruction* next_phi = current_phi->GetNext(); |
| 1042 | DCHECK(next_phi == nullptr || |
| 1043 | current_phi->AsPhi()->GetRegNumber() <= next_phi->AsPhi()->GetRegNumber()) |
| 1044 | << "Phis need to be sorted by vreg number to keep this a linear-time loop."; |
| 1045 | current_phi = next_phi; |
| 1046 | } |
| 1047 | |
| 1048 | if (current_phi == nullptr || current_phi->AsPhi()->GetRegNumber() != vreg) { |
| 1049 | stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kNone, 0); |
| 1050 | } else { |
| 1051 | Location location = current_phi->GetLiveInterval()->ToLocation(); |
| 1052 | switch (location.GetKind()) { |
| 1053 | case Location::kStackSlot: { |
| 1054 | stack_map_stream_.AddDexRegisterEntry( |
| 1055 | DexRegisterLocation::Kind::kInStack, location.GetStackIndex()); |
| 1056 | break; |
| 1057 | } |
| 1058 | case Location::kDoubleStackSlot: { |
| 1059 | stack_map_stream_.AddDexRegisterEntry( |
| 1060 | DexRegisterLocation::Kind::kInStack, location.GetStackIndex()); |
| 1061 | stack_map_stream_.AddDexRegisterEntry( |
| 1062 | DexRegisterLocation::Kind::kInStack, location.GetHighStackIndex(kVRegSize)); |
| 1063 | ++vreg; |
| 1064 | DCHECK_LT(vreg, num_vregs); |
| 1065 | break; |
| 1066 | } |
| 1067 | default: { |
| 1068 | // All catch phis must be allocated to a stack slot. |
| 1069 | LOG(FATAL) << "Unexpected kind " << location.GetKind(); |
| 1070 | UNREACHABLE(); |
| 1071 | } |
| 1072 | } |
| 1073 | } |
| 1074 | } |
| 1075 | |
| 1076 | stack_map_stream_.EndStackMapEntry(); |
| 1077 | } |
| 1078 | } |
| 1079 | |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 1080 | void CodeGenerator::EmitEnvironment(HEnvironment* environment, SlowPathCode* slow_path) { |
| 1081 | if (environment == nullptr) return; |
| 1082 | |
| 1083 | if (environment->GetParent() != nullptr) { |
| 1084 | // We emit the parent environment first. |
| 1085 | EmitEnvironment(environment->GetParent(), slow_path); |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 1086 | stack_map_stream_.BeginInlineInfoEntry(environment->GetMethodIdx(), |
| 1087 | environment->GetDexPc(), |
| 1088 | environment->GetInvokeType(), |
| 1089 | environment->Size()); |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1090 | } |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 1091 | |
| 1092 | // Walk over the environment, and record the location of dex registers. |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 1093 | 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] | 1094 | HInstruction* current = environment->GetInstructionAt(i); |
| 1095 | if (current == nullptr) { |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 1096 | stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kNone, 0); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 1097 | continue; |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 1098 | } |
| 1099 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1100 | Location location = environment->GetLocationAt(i); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 1101 | switch (location.GetKind()) { |
| 1102 | case Location::kConstant: { |
| 1103 | DCHECK_EQ(current, location.GetConstant()); |
| 1104 | if (current->IsLongConstant()) { |
| 1105 | int64_t value = current->AsLongConstant()->GetValue(); |
| 1106 | stack_map_stream_.AddDexRegisterEntry( |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 1107 | DexRegisterLocation::Kind::kConstant, Low32Bits(value)); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 1108 | stack_map_stream_.AddDexRegisterEntry( |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 1109 | DexRegisterLocation::Kind::kConstant, High32Bits(value)); |
| 1110 | ++i; |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 1111 | DCHECK_LT(i, environment_size); |
| 1112 | } else if (current->IsDoubleConstant()) { |
Roland Levillain | da4d79b | 2015-03-24 14:36:11 +0000 | [diff] [blame] | 1113 | int64_t value = bit_cast<int64_t, double>(current->AsDoubleConstant()->GetValue()); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 1114 | stack_map_stream_.AddDexRegisterEntry( |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 1115 | DexRegisterLocation::Kind::kConstant, Low32Bits(value)); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 1116 | stack_map_stream_.AddDexRegisterEntry( |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 1117 | DexRegisterLocation::Kind::kConstant, High32Bits(value)); |
| 1118 | ++i; |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 1119 | DCHECK_LT(i, environment_size); |
| 1120 | } else if (current->IsIntConstant()) { |
| 1121 | int32_t value = current->AsIntConstant()->GetValue(); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 1122 | stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kConstant, value); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 1123 | } else if (current->IsNullConstant()) { |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 1124 | stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kConstant, 0); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 1125 | } else { |
| 1126 | DCHECK(current->IsFloatConstant()) << current->DebugName(); |
Roland Levillain | da4d79b | 2015-03-24 14:36:11 +0000 | [diff] [blame] | 1127 | int32_t value = bit_cast<int32_t, float>(current->AsFloatConstant()->GetValue()); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 1128 | stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kConstant, value); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 1129 | } |
| 1130 | break; |
| 1131 | } |
| 1132 | |
| 1133 | case Location::kStackSlot: { |
| 1134 | stack_map_stream_.AddDexRegisterEntry( |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 1135 | DexRegisterLocation::Kind::kInStack, location.GetStackIndex()); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 1136 | break; |
| 1137 | } |
| 1138 | |
| 1139 | case Location::kDoubleStackSlot: { |
| 1140 | stack_map_stream_.AddDexRegisterEntry( |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 1141 | DexRegisterLocation::Kind::kInStack, location.GetStackIndex()); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 1142 | stack_map_stream_.AddDexRegisterEntry( |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 1143 | DexRegisterLocation::Kind::kInStack, location.GetHighStackIndex(kVRegSize)); |
| 1144 | ++i; |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 1145 | DCHECK_LT(i, environment_size); |
| 1146 | break; |
| 1147 | } |
| 1148 | |
| 1149 | case Location::kRegister : { |
| 1150 | int id = location.reg(); |
| 1151 | if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(id)) { |
| 1152 | uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(id); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 1153 | stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 1154 | if (current->GetType() == Primitive::kPrimLong) { |
| 1155 | stack_map_stream_.AddDexRegisterEntry( |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 1156 | DexRegisterLocation::Kind::kInStack, offset + kVRegSize); |
| 1157 | ++i; |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 1158 | DCHECK_LT(i, environment_size); |
| 1159 | } |
| 1160 | } else { |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 1161 | stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, id); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 1162 | if (current->GetType() == Primitive::kPrimLong) { |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 1163 | stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegisterHigh, id); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 1164 | ++i; |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 1165 | DCHECK_LT(i, environment_size); |
| 1166 | } |
| 1167 | } |
| 1168 | break; |
| 1169 | } |
| 1170 | |
| 1171 | case Location::kFpuRegister : { |
| 1172 | int id = location.reg(); |
| 1173 | if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(id)) { |
| 1174 | uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(id); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 1175 | stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 1176 | if (current->GetType() == Primitive::kPrimDouble) { |
| 1177 | stack_map_stream_.AddDexRegisterEntry( |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 1178 | DexRegisterLocation::Kind::kInStack, offset + kVRegSize); |
| 1179 | ++i; |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 1180 | DCHECK_LT(i, environment_size); |
| 1181 | } |
| 1182 | } else { |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 1183 | stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, id); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 1184 | if (current->GetType() == Primitive::kPrimDouble) { |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 1185 | stack_map_stream_.AddDexRegisterEntry( |
| 1186 | DexRegisterLocation::Kind::kInFpuRegisterHigh, id); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 1187 | ++i; |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 1188 | DCHECK_LT(i, environment_size); |
| 1189 | } |
| 1190 | } |
| 1191 | break; |
| 1192 | } |
| 1193 | |
| 1194 | case Location::kFpuRegisterPair : { |
| 1195 | int low = location.low(); |
| 1196 | int high = location.high(); |
| 1197 | if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(low)) { |
| 1198 | uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(low); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 1199 | stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 1200 | } else { |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 1201 | stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, low); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 1202 | } |
| 1203 | if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(high)) { |
| 1204 | uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(high); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 1205 | stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset); |
| 1206 | ++i; |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 1207 | } else { |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 1208 | stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, high); |
| 1209 | ++i; |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 1210 | } |
| 1211 | DCHECK_LT(i, environment_size); |
| 1212 | break; |
| 1213 | } |
| 1214 | |
| 1215 | case Location::kRegisterPair : { |
| 1216 | int low = location.low(); |
| 1217 | int high = location.high(); |
| 1218 | if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(low)) { |
| 1219 | uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(low); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 1220 | stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 1221 | } else { |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 1222 | stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, low); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 1223 | } |
| 1224 | if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(high)) { |
| 1225 | uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(high); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 1226 | stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 1227 | } else { |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 1228 | stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, high); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 1229 | } |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 1230 | ++i; |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 1231 | DCHECK_LT(i, environment_size); |
| 1232 | break; |
| 1233 | } |
| 1234 | |
| 1235 | case Location::kInvalid: { |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 1236 | stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kNone, 0); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 1237 | break; |
| 1238 | } |
| 1239 | |
| 1240 | default: |
| 1241 | LOG(FATAL) << "Unexpected kind " << location.GetKind(); |
| 1242 | } |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 1243 | } |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 1244 | |
| 1245 | if (environment->GetParent() != nullptr) { |
| 1246 | stack_map_stream_.EndInlineInfoEntry(); |
| 1247 | } |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 1248 | } |
| 1249 | |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 1250 | bool CodeGenerator::IsImplicitNullCheckAllowed(HNullCheck* null_check) const { |
| 1251 | return compiler_options_.GetImplicitNullChecks() && |
| 1252 | // Null checks which might throw into a catch block need to save live |
| 1253 | // registers and therefore cannot be done implicitly. |
| 1254 | !null_check->CanThrowIntoCatchBlock(); |
| 1255 | } |
| 1256 | |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 1257 | bool CodeGenerator::CanMoveNullCheckToUser(HNullCheck* null_check) { |
| 1258 | HInstruction* first_next_not_move = null_check->GetNextDisregardingMoves(); |
Calin Juravle | 641547a | 2015-04-21 22:08:51 +0100 | [diff] [blame] | 1259 | |
| 1260 | return (first_next_not_move != nullptr) |
| 1261 | && first_next_not_move->CanDoImplicitNullCheckOn(null_check->InputAt(0)); |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 1262 | } |
| 1263 | |
| 1264 | void CodeGenerator::MaybeRecordImplicitNullCheck(HInstruction* instr) { |
| 1265 | // If we are from a static path don't record the pc as we can't throw NPE. |
| 1266 | // NB: having the checks here makes the code much less verbose in the arch |
| 1267 | // specific code generators. |
| 1268 | if (instr->IsStaticFieldSet() || instr->IsStaticFieldGet()) { |
| 1269 | return; |
| 1270 | } |
| 1271 | |
Calin Juravle | 641547a | 2015-04-21 22:08:51 +0100 | [diff] [blame] | 1272 | if (!instr->CanDoImplicitNullCheckOn(instr->InputAt(0))) { |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 1273 | return; |
| 1274 | } |
| 1275 | |
| 1276 | // Find the first previous instruction which is not a move. |
| 1277 | HInstruction* first_prev_not_move = instr->GetPreviousDisregardingMoves(); |
| 1278 | |
| 1279 | // If the instruction is a null check it means that `instr` is the first user |
| 1280 | // and needs to record the pc. |
| 1281 | if (first_prev_not_move != nullptr && first_prev_not_move->IsNullCheck()) { |
| 1282 | HNullCheck* null_check = first_prev_not_move->AsNullCheck(); |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 1283 | if (IsImplicitNullCheckAllowed(null_check)) { |
| 1284 | // TODO: The parallel moves modify the environment. Their changes need to be |
| 1285 | // reverted otherwise the stack maps at the throw point will not be correct. |
| 1286 | RecordPcInfo(null_check, null_check->GetDexPc()); |
| 1287 | } |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 1288 | } |
| 1289 | } |
| 1290 | |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 1291 | void CodeGenerator::ClearSpillSlotsFromLoopPhisInStackMap(HSuspendCheck* suspend_check) const { |
| 1292 | LocationSummary* locations = suspend_check->GetLocations(); |
| 1293 | HBasicBlock* block = suspend_check->GetBlock(); |
| 1294 | DCHECK(block->GetLoopInformation()->GetSuspendCheck() == suspend_check); |
| 1295 | DCHECK(block->IsLoopHeader()); |
| 1296 | |
| 1297 | for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) { |
| 1298 | HInstruction* current = it.Current(); |
| 1299 | LiveInterval* interval = current->GetLiveInterval(); |
| 1300 | // We only need to clear bits of loop phis containing objects and allocated in register. |
| 1301 | // Loop phis allocated on stack already have the object in the stack. |
| 1302 | if (current->GetType() == Primitive::kPrimNot |
| 1303 | && interval->HasRegister() |
| 1304 | && interval->HasSpillSlot()) { |
| 1305 | locations->ClearStackBit(interval->GetSpillSlot() / kVRegSize); |
| 1306 | } |
| 1307 | } |
| 1308 | } |
| 1309 | |
Nicolas Geoffray | 9021825 | 2015-04-15 11:56:51 +0100 | [diff] [blame] | 1310 | void CodeGenerator::EmitParallelMoves(Location from1, |
| 1311 | Location to1, |
| 1312 | Primitive::Type type1, |
| 1313 | Location from2, |
| 1314 | Location to2, |
| 1315 | Primitive::Type type2) { |
Nicolas Geoffray | f0e3937 | 2014-11-12 17:50:07 +0000 | [diff] [blame] | 1316 | HParallelMove parallel_move(GetGraph()->GetArena()); |
Nicolas Geoffray | 9021825 | 2015-04-15 11:56:51 +0100 | [diff] [blame] | 1317 | parallel_move.AddMove(from1, to1, type1, nullptr); |
| 1318 | parallel_move.AddMove(from2, to2, type2, nullptr); |
Nicolas Geoffray | f0e3937 | 2014-11-12 17:50:07 +0000 | [diff] [blame] | 1319 | GetMoveResolver()->EmitNativeCode(¶llel_move); |
| 1320 | } |
| 1321 | |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 1322 | void CodeGenerator::ValidateInvokeRuntime(HInstruction* instruction, SlowPathCode* slow_path) { |
| 1323 | // Ensure that the call kind indication given to the register allocator is |
| 1324 | // coherent with the runtime call generated, and that the GC side effect is |
| 1325 | // set when required. |
| 1326 | if (slow_path == nullptr) { |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame^] | 1327 | DCHECK(instruction->GetLocations()->WillCall()) |
| 1328 | << "instruction->DebugName()=" << instruction->DebugName(); |
Roland Levillain | df3f822 | 2015-08-13 12:31:44 +0100 | [diff] [blame] | 1329 | DCHECK(instruction->GetSideEffects().Includes(SideEffects::CanTriggerGC())) |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame^] | 1330 | << "instruction->DebugName()=" << instruction->DebugName() |
| 1331 | << " instruction->GetSideEffects().ToString()=" << instruction->GetSideEffects().ToString(); |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 1332 | } else { |
Roland Levillain | df3f822 | 2015-08-13 12:31:44 +0100 | [diff] [blame] | 1333 | DCHECK(instruction->GetLocations()->OnlyCallsOnSlowPath() || slow_path->IsFatal()) |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame^] | 1334 | << "instruction->DebugName()=" << instruction->DebugName() |
| 1335 | << " slow_path->GetDescription()=" << slow_path->GetDescription(); |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 1336 | DCHECK(instruction->GetSideEffects().Includes(SideEffects::CanTriggerGC()) || |
| 1337 | // Control flow would not come back into the code if a fatal slow |
| 1338 | // path is taken, so we do not care if it triggers GC. |
| 1339 | slow_path->IsFatal() || |
| 1340 | // HDeoptimize is a special case: we know we are not coming back from |
| 1341 | // it into the code. |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame^] | 1342 | instruction->IsDeoptimize() || |
| 1343 | // When read barriers are enabled, some instructions use a |
| 1344 | // slow path to emit a read barrier, which does not trigger |
| 1345 | // GC, is not fatal, nor is emitted by HDeoptimize |
| 1346 | // instructions. |
| 1347 | (kEmitCompilerReadBarrier && |
| 1348 | (instruction->IsInstanceFieldGet() || |
| 1349 | instruction->IsStaticFieldGet() || |
| 1350 | instruction->IsArraySet() || |
| 1351 | instruction->IsArrayGet() || |
| 1352 | instruction->IsLoadClass() || |
| 1353 | instruction->IsLoadString() || |
| 1354 | instruction->IsInstanceOf() || |
| 1355 | instruction->IsCheckCast()))) |
| 1356 | << "instruction->DebugName()=" << instruction->DebugName() |
| 1357 | << " instruction->GetSideEffects().ToString()=" << instruction->GetSideEffects().ToString() |
| 1358 | << " slow_path->GetDescription()=" << slow_path->GetDescription(); |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 1359 | } |
| 1360 | |
| 1361 | // Check the coherency of leaf information. |
| 1362 | DCHECK(instruction->IsSuspendCheck() |
| 1363 | || ((slow_path != nullptr) && slow_path->IsFatal()) |
| 1364 | || instruction->GetLocations()->CanCall() |
Roland Levillain | df3f822 | 2015-08-13 12:31:44 +0100 | [diff] [blame] | 1365 | || !IsLeafMethod()) |
| 1366 | << instruction->DebugName() << ((slow_path != nullptr) ? slow_path->GetDescription() : ""); |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 1367 | } |
| 1368 | |
Nicolas Geoffray | a8ac913 | 2015-03-13 16:36:36 +0000 | [diff] [blame] | 1369 | void SlowPathCode::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) { |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame^] | 1370 | RegisterSet* live_registers = locations->GetLiveRegisters(); |
Nicolas Geoffray | a8ac913 | 2015-03-13 16:36:36 +0000 | [diff] [blame] | 1371 | size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath(); |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame^] | 1372 | |
Nicolas Geoffray | a8ac913 | 2015-03-13 16:36:36 +0000 | [diff] [blame] | 1373 | for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) { |
| 1374 | if (!codegen->IsCoreCalleeSaveRegister(i)) { |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame^] | 1375 | if (live_registers->ContainsCoreRegister(i)) { |
Nicolas Geoffray | a8ac913 | 2015-03-13 16:36:36 +0000 | [diff] [blame] | 1376 | // If the register holds an object, update the stack mask. |
| 1377 | if (locations->RegisterContainsObject(i)) { |
| 1378 | locations->SetStackBit(stack_offset / kVRegSize); |
| 1379 | } |
| 1380 | DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize()); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 1381 | DCHECK_LT(i, kMaximumNumberOfExpectedRegisters); |
| 1382 | saved_core_stack_offsets_[i] = stack_offset; |
Nicolas Geoffray | a8ac913 | 2015-03-13 16:36:36 +0000 | [diff] [blame] | 1383 | stack_offset += codegen->SaveCoreRegister(stack_offset, i); |
| 1384 | } |
| 1385 | } |
| 1386 | } |
| 1387 | |
| 1388 | for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) { |
| 1389 | if (!codegen->IsFloatingPointCalleeSaveRegister(i)) { |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame^] | 1390 | if (live_registers->ContainsFloatingPointRegister(i)) { |
Nicolas Geoffray | a8ac913 | 2015-03-13 16:36:36 +0000 | [diff] [blame] | 1391 | DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize()); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 1392 | DCHECK_LT(i, kMaximumNumberOfExpectedRegisters); |
| 1393 | saved_fpu_stack_offsets_[i] = stack_offset; |
Nicolas Geoffray | a8ac913 | 2015-03-13 16:36:36 +0000 | [diff] [blame] | 1394 | stack_offset += codegen->SaveFloatingPointRegister(stack_offset, i); |
| 1395 | } |
| 1396 | } |
| 1397 | } |
| 1398 | } |
| 1399 | |
| 1400 | void SlowPathCode::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) { |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame^] | 1401 | RegisterSet* live_registers = locations->GetLiveRegisters(); |
Nicolas Geoffray | a8ac913 | 2015-03-13 16:36:36 +0000 | [diff] [blame] | 1402 | size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath(); |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame^] | 1403 | |
Nicolas Geoffray | a8ac913 | 2015-03-13 16:36:36 +0000 | [diff] [blame] | 1404 | for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) { |
| 1405 | if (!codegen->IsCoreCalleeSaveRegister(i)) { |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame^] | 1406 | if (live_registers->ContainsCoreRegister(i)) { |
Nicolas Geoffray | a8ac913 | 2015-03-13 16:36:36 +0000 | [diff] [blame] | 1407 | DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize()); |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame^] | 1408 | DCHECK_LT(i, kMaximumNumberOfExpectedRegisters); |
Nicolas Geoffray | a8ac913 | 2015-03-13 16:36:36 +0000 | [diff] [blame] | 1409 | stack_offset += codegen->RestoreCoreRegister(stack_offset, i); |
| 1410 | } |
| 1411 | } |
| 1412 | } |
| 1413 | |
| 1414 | for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) { |
| 1415 | if (!codegen->IsFloatingPointCalleeSaveRegister(i)) { |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame^] | 1416 | if (live_registers->ContainsFloatingPointRegister(i)) { |
Nicolas Geoffray | a8ac913 | 2015-03-13 16:36:36 +0000 | [diff] [blame] | 1417 | DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize()); |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame^] | 1418 | DCHECK_LT(i, kMaximumNumberOfExpectedRegisters); |
Nicolas Geoffray | a8ac913 | 2015-03-13 16:36:36 +0000 | [diff] [blame] | 1419 | stack_offset += codegen->RestoreFloatingPointRegister(stack_offset, i); |
| 1420 | } |
| 1421 | } |
| 1422 | } |
| 1423 | } |
| 1424 | |
Nicolas Geoffray | 5bd05a5 | 2015-10-13 09:48:30 +0100 | [diff] [blame] | 1425 | void CodeGenerator::CreateSystemArrayCopyLocationSummary(HInvoke* invoke) { |
| 1426 | // Check to see if we have known failures that will cause us to have to bail out |
| 1427 | // to the runtime, and just generate the runtime call directly. |
| 1428 | HIntConstant* src_pos = invoke->InputAt(1)->AsIntConstant(); |
| 1429 | HIntConstant* dest_pos = invoke->InputAt(3)->AsIntConstant(); |
| 1430 | |
| 1431 | // The positions must be non-negative. |
| 1432 | if ((src_pos != nullptr && src_pos->GetValue() < 0) || |
| 1433 | (dest_pos != nullptr && dest_pos->GetValue() < 0)) { |
| 1434 | // We will have to fail anyways. |
| 1435 | return; |
| 1436 | } |
| 1437 | |
| 1438 | // The length must be >= 0. |
| 1439 | HIntConstant* length = invoke->InputAt(4)->AsIntConstant(); |
| 1440 | if (length != nullptr) { |
| 1441 | int32_t len = length->GetValue(); |
| 1442 | if (len < 0) { |
| 1443 | // Just call as normal. |
| 1444 | return; |
| 1445 | } |
| 1446 | } |
| 1447 | |
| 1448 | SystemArrayCopyOptimizations optimizations(invoke); |
| 1449 | |
| 1450 | if (optimizations.GetDestinationIsSource()) { |
| 1451 | if (src_pos != nullptr && dest_pos != nullptr && src_pos->GetValue() < dest_pos->GetValue()) { |
| 1452 | // We only support backward copying if source and destination are the same. |
| 1453 | return; |
| 1454 | } |
| 1455 | } |
| 1456 | |
| 1457 | if (optimizations.GetDestinationIsPrimitiveArray() || optimizations.GetSourceIsPrimitiveArray()) { |
| 1458 | // We currently don't intrinsify primitive copying. |
| 1459 | return; |
| 1460 | } |
| 1461 | |
| 1462 | ArenaAllocator* allocator = invoke->GetBlock()->GetGraph()->GetArena(); |
| 1463 | LocationSummary* locations = new (allocator) LocationSummary(invoke, |
| 1464 | LocationSummary::kCallOnSlowPath, |
| 1465 | kIntrinsified); |
| 1466 | // arraycopy(Object src, int src_pos, Object dest, int dest_pos, int length). |
| 1467 | locations->SetInAt(0, Location::RequiresRegister()); |
| 1468 | locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1))); |
| 1469 | locations->SetInAt(2, Location::RequiresRegister()); |
| 1470 | locations->SetInAt(3, Location::RegisterOrConstant(invoke->InputAt(3))); |
| 1471 | locations->SetInAt(4, Location::RegisterOrConstant(invoke->InputAt(4))); |
| 1472 | |
| 1473 | locations->AddTemp(Location::RequiresRegister()); |
| 1474 | locations->AddTemp(Location::RequiresRegister()); |
| 1475 | locations->AddTemp(Location::RequiresRegister()); |
| 1476 | } |
| 1477 | |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 1478 | } // namespace art |