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