Goran Jakovljevic | f652cec | 2015-08-25 16:11:42 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | #ifndef ART_COMPILER_OPTIMIZING_CODE_GENERATOR_MIPS_H_ |
| 18 | #define ART_COMPILER_OPTIMIZING_CODE_GENERATOR_MIPS_H_ |
| 19 | |
| 20 | #include "code_generator.h" |
| 21 | #include "dex/compiler_enums.h" |
| 22 | #include "driver/compiler_options.h" |
| 23 | #include "nodes.h" |
| 24 | #include "parallel_move_resolver.h" |
| 25 | #include "utils/mips/assembler_mips.h" |
| 26 | |
| 27 | namespace art { |
| 28 | namespace mips { |
| 29 | |
| 30 | // InvokeDexCallingConvention registers |
| 31 | |
| 32 | static constexpr Register kParameterCoreRegisters[] = |
| 33 | { A1, A2, A3 }; |
| 34 | static constexpr size_t kParameterCoreRegistersLength = arraysize(kParameterCoreRegisters); |
| 35 | |
| 36 | static constexpr FRegister kParameterFpuRegisters[] = |
| 37 | { F12, F14 }; |
| 38 | static constexpr size_t kParameterFpuRegistersLength = arraysize(kParameterFpuRegisters); |
| 39 | |
| 40 | |
| 41 | // InvokeRuntimeCallingConvention registers |
| 42 | |
| 43 | static constexpr Register kRuntimeParameterCoreRegisters[] = |
| 44 | { A0, A1, A2, A3 }; |
| 45 | static constexpr size_t kRuntimeParameterCoreRegistersLength = |
| 46 | arraysize(kRuntimeParameterCoreRegisters); |
| 47 | |
| 48 | static constexpr FRegister kRuntimeParameterFpuRegisters[] = |
| 49 | { F12, F14}; |
| 50 | static constexpr size_t kRuntimeParameterFpuRegistersLength = |
| 51 | arraysize(kRuntimeParameterFpuRegisters); |
| 52 | |
| 53 | |
| 54 | static constexpr Register kCoreCalleeSaves[] = |
| 55 | { S0, S1, S2, S3, S4, S5, S6, S7, FP, RA }; |
| 56 | static constexpr FRegister kFpuCalleeSaves[] = |
| 57 | { F20, F22, F24, F26, F28, F30 }; |
| 58 | |
| 59 | |
| 60 | class CodeGeneratorMIPS; |
| 61 | |
| 62 | class InvokeDexCallingConvention : public CallingConvention<Register, FRegister> { |
| 63 | public: |
| 64 | InvokeDexCallingConvention() |
| 65 | : CallingConvention(kParameterCoreRegisters, |
| 66 | kParameterCoreRegistersLength, |
| 67 | kParameterFpuRegisters, |
| 68 | kParameterFpuRegistersLength, |
| 69 | kMipsPointerSize) {} |
| 70 | |
| 71 | private: |
| 72 | DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConvention); |
| 73 | }; |
| 74 | |
| 75 | class InvokeDexCallingConventionVisitorMIPS : public InvokeDexCallingConventionVisitor { |
| 76 | public: |
| 77 | InvokeDexCallingConventionVisitorMIPS() {} |
| 78 | virtual ~InvokeDexCallingConventionVisitorMIPS() {} |
| 79 | |
| 80 | Location GetNextLocation(Primitive::Type type) OVERRIDE; |
| 81 | Location GetReturnLocation(Primitive::Type type) const OVERRIDE; |
| 82 | Location GetMethodLocation() const OVERRIDE; |
| 83 | |
| 84 | private: |
| 85 | InvokeDexCallingConvention calling_convention; |
| 86 | |
| 87 | DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConventionVisitorMIPS); |
| 88 | }; |
| 89 | |
| 90 | class InvokeRuntimeCallingConvention : public CallingConvention<Register, FRegister> { |
| 91 | public: |
| 92 | InvokeRuntimeCallingConvention() |
| 93 | : CallingConvention(kRuntimeParameterCoreRegisters, |
| 94 | kRuntimeParameterCoreRegistersLength, |
| 95 | kRuntimeParameterFpuRegisters, |
| 96 | kRuntimeParameterFpuRegistersLength, |
| 97 | kMipsPointerSize) {} |
| 98 | |
| 99 | Location GetReturnLocation(Primitive::Type return_type); |
| 100 | |
| 101 | private: |
| 102 | DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention); |
| 103 | }; |
| 104 | |
| 105 | class FieldAccessCallingConventionMIPS : public FieldAccessCallingConvention { |
| 106 | public: |
| 107 | FieldAccessCallingConventionMIPS() {} |
| 108 | |
| 109 | Location GetObjectLocation() const OVERRIDE { |
| 110 | return Location::RegisterLocation(A1); |
| 111 | } |
| 112 | Location GetFieldIndexLocation() const OVERRIDE { |
| 113 | return Location::RegisterLocation(A0); |
| 114 | } |
| 115 | Location GetReturnLocation(Primitive::Type type) const OVERRIDE { |
| 116 | return Primitive::Is64BitType(type) |
| 117 | ? Location::RegisterPairLocation(V0, V1) |
| 118 | : Location::RegisterLocation(V0); |
| 119 | } |
| 120 | Location GetSetValueLocation(Primitive::Type type, bool is_instance) const OVERRIDE { |
| 121 | return Primitive::Is64BitType(type) |
| 122 | ? Location::RegisterPairLocation(A2, A3) |
| 123 | : (is_instance ? Location::RegisterLocation(A2) : Location::RegisterLocation(A1)); |
| 124 | } |
| 125 | Location GetFpuLocation(Primitive::Type type ATTRIBUTE_UNUSED) const OVERRIDE { |
| 126 | return Location::FpuRegisterLocation(F0); |
| 127 | } |
| 128 | |
| 129 | private: |
| 130 | DISALLOW_COPY_AND_ASSIGN(FieldAccessCallingConventionMIPS); |
| 131 | }; |
| 132 | |
| 133 | class ParallelMoveResolverMIPS : public ParallelMoveResolverWithSwap { |
| 134 | public: |
| 135 | ParallelMoveResolverMIPS(ArenaAllocator* allocator, CodeGeneratorMIPS* codegen) |
| 136 | : ParallelMoveResolverWithSwap(allocator), codegen_(codegen) {} |
| 137 | |
| 138 | void EmitMove(size_t index) OVERRIDE; |
| 139 | void EmitSwap(size_t index) OVERRIDE; |
| 140 | void SpillScratch(int reg) OVERRIDE; |
| 141 | void RestoreScratch(int reg) OVERRIDE; |
| 142 | |
| 143 | void Exchange(int index1, int index2, bool double_slot); |
| 144 | |
| 145 | MipsAssembler* GetAssembler() const; |
| 146 | |
| 147 | private: |
| 148 | CodeGeneratorMIPS* const codegen_; |
| 149 | |
| 150 | DISALLOW_COPY_AND_ASSIGN(ParallelMoveResolverMIPS); |
| 151 | }; |
| 152 | |
| 153 | class SlowPathCodeMIPS : public SlowPathCode { |
| 154 | public: |
David Srbecky | 9cd6d37 | 2016-02-09 15:24:47 +0000 | [diff] [blame] | 155 | explicit SlowPathCodeMIPS(HInstruction* instruction) |
| 156 | : SlowPathCode(instruction), entry_label_(), exit_label_() {} |
Goran Jakovljevic | f652cec | 2015-08-25 16:11:42 +0200 | [diff] [blame] | 157 | |
| 158 | MipsLabel* GetEntryLabel() { return &entry_label_; } |
| 159 | MipsLabel* GetExitLabel() { return &exit_label_; } |
| 160 | |
| 161 | private: |
| 162 | MipsLabel entry_label_; |
| 163 | MipsLabel exit_label_; |
| 164 | |
| 165 | DISALLOW_COPY_AND_ASSIGN(SlowPathCodeMIPS); |
| 166 | }; |
| 167 | |
| 168 | class LocationsBuilderMIPS : public HGraphVisitor { |
| 169 | public: |
| 170 | LocationsBuilderMIPS(HGraph* graph, CodeGeneratorMIPS* codegen) |
| 171 | : HGraphVisitor(graph), codegen_(codegen) {} |
| 172 | |
| 173 | #define DECLARE_VISIT_INSTRUCTION(name, super) \ |
| 174 | void Visit##name(H##name* instr) OVERRIDE; |
| 175 | |
| 176 | FOR_EACH_CONCRETE_INSTRUCTION_COMMON(DECLARE_VISIT_INSTRUCTION) |
| 177 | FOR_EACH_CONCRETE_INSTRUCTION_MIPS(DECLARE_VISIT_INSTRUCTION) |
| 178 | |
| 179 | #undef DECLARE_VISIT_INSTRUCTION |
| 180 | |
| 181 | void VisitInstruction(HInstruction* instruction) OVERRIDE { |
| 182 | LOG(FATAL) << "Unreachable instruction " << instruction->DebugName() |
| 183 | << " (id " << instruction->GetId() << ")"; |
| 184 | } |
| 185 | |
| 186 | private: |
| 187 | void HandleInvoke(HInvoke* invoke); |
| 188 | void HandleBinaryOp(HBinaryOperation* operation); |
Vladimir Marko | 5f7b58e | 2015-11-23 19:49:34 +0000 | [diff] [blame] | 189 | void HandleCondition(HCondition* instruction); |
Goran Jakovljevic | f652cec | 2015-08-25 16:11:42 +0200 | [diff] [blame] | 190 | void HandleShift(HBinaryOperation* operation); |
| 191 | void HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info); |
| 192 | void HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info); |
| 193 | |
| 194 | InvokeDexCallingConventionVisitorMIPS parameter_visitor_; |
| 195 | |
| 196 | CodeGeneratorMIPS* const codegen_; |
| 197 | |
| 198 | DISALLOW_COPY_AND_ASSIGN(LocationsBuilderMIPS); |
| 199 | }; |
| 200 | |
Aart Bik | 42249c3 | 2016-01-07 15:33:50 -0800 | [diff] [blame] | 201 | class InstructionCodeGeneratorMIPS : public InstructionCodeGenerator { |
Goran Jakovljevic | f652cec | 2015-08-25 16:11:42 +0200 | [diff] [blame] | 202 | public: |
| 203 | InstructionCodeGeneratorMIPS(HGraph* graph, CodeGeneratorMIPS* codegen); |
| 204 | |
| 205 | #define DECLARE_VISIT_INSTRUCTION(name, super) \ |
| 206 | void Visit##name(H##name* instr) OVERRIDE; |
| 207 | |
| 208 | FOR_EACH_CONCRETE_INSTRUCTION_COMMON(DECLARE_VISIT_INSTRUCTION) |
| 209 | FOR_EACH_CONCRETE_INSTRUCTION_MIPS(DECLARE_VISIT_INSTRUCTION) |
| 210 | |
| 211 | #undef DECLARE_VISIT_INSTRUCTION |
| 212 | |
| 213 | void VisitInstruction(HInstruction* instruction) OVERRIDE { |
| 214 | LOG(FATAL) << "Unreachable instruction " << instruction->DebugName() |
| 215 | << " (id " << instruction->GetId() << ")"; |
| 216 | } |
| 217 | |
| 218 | MipsAssembler* GetAssembler() const { return assembler_; } |
| 219 | |
| 220 | private: |
| 221 | void GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path, Register class_reg); |
| 222 | void GenerateMemoryBarrier(MemBarrierKind kind); |
| 223 | void GenerateSuspendCheck(HSuspendCheck* check, HBasicBlock* successor); |
| 224 | void HandleBinaryOp(HBinaryOperation* operation); |
Vladimir Marko | 5f7b58e | 2015-11-23 19:49:34 +0000 | [diff] [blame] | 225 | void HandleCondition(HCondition* instruction); |
Goran Jakovljevic | f652cec | 2015-08-25 16:11:42 +0200 | [diff] [blame] | 226 | void HandleShift(HBinaryOperation* operation); |
| 227 | void HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info, uint32_t dex_pc); |
| 228 | void HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info, uint32_t dex_pc); |
| 229 | void GenerateImplicitNullCheck(HNullCheck* instruction); |
| 230 | void GenerateExplicitNullCheck(HNullCheck* instruction); |
Alexey Frunze | cd7b0ee | 2015-12-03 16:46:38 -0800 | [diff] [blame] | 231 | void GenerateIntCompare(IfCondition cond, LocationSummary* locations); |
| 232 | void GenerateIntCompareAndBranch(IfCondition cond, |
| 233 | LocationSummary* locations, |
| 234 | MipsLabel* label); |
| 235 | void GenerateLongCompareAndBranch(IfCondition cond, |
| 236 | LocationSummary* locations, |
| 237 | MipsLabel* label); |
| 238 | void GenerateFpCompareAndBranch(IfCondition cond, |
| 239 | bool gt_bias, |
| 240 | Primitive::Type type, |
| 241 | LocationSummary* locations, |
| 242 | MipsLabel* label); |
Goran Jakovljevic | f652cec | 2015-08-25 16:11:42 +0200 | [diff] [blame] | 243 | void GenerateTestAndBranch(HInstruction* instruction, |
David Brazdil | 0debae7 | 2015-11-12 18:37:00 +0000 | [diff] [blame] | 244 | size_t condition_input_index, |
Goran Jakovljevic | f652cec | 2015-08-25 16:11:42 +0200 | [diff] [blame] | 245 | MipsLabel* true_target, |
David Brazdil | 0debae7 | 2015-11-12 18:37:00 +0000 | [diff] [blame] | 246 | MipsLabel* false_target); |
Alexey Frunze | 7e99e05 | 2015-11-24 19:28:01 -0800 | [diff] [blame] | 247 | void DivRemOneOrMinusOne(HBinaryOperation* instruction); |
| 248 | void DivRemByPowerOfTwo(HBinaryOperation* instruction); |
| 249 | void GenerateDivRemWithAnyConstant(HBinaryOperation* instruction); |
| 250 | void GenerateDivRemIntegral(HBinaryOperation* instruction); |
Goran Jakovljevic | f652cec | 2015-08-25 16:11:42 +0200 | [diff] [blame] | 251 | void HandleGoto(HInstruction* got, HBasicBlock* successor); |
| 252 | |
| 253 | MipsAssembler* const assembler_; |
| 254 | CodeGeneratorMIPS* const codegen_; |
| 255 | |
| 256 | DISALLOW_COPY_AND_ASSIGN(InstructionCodeGeneratorMIPS); |
| 257 | }; |
| 258 | |
| 259 | class CodeGeneratorMIPS : public CodeGenerator { |
| 260 | public: |
| 261 | CodeGeneratorMIPS(HGraph* graph, |
| 262 | const MipsInstructionSetFeatures& isa_features, |
| 263 | const CompilerOptions& compiler_options, |
| 264 | OptimizingCompilerStats* stats = nullptr); |
| 265 | virtual ~CodeGeneratorMIPS() {} |
| 266 | |
| 267 | void GenerateFrameEntry() OVERRIDE; |
| 268 | void GenerateFrameExit() OVERRIDE; |
| 269 | |
| 270 | void Bind(HBasicBlock* block) OVERRIDE; |
| 271 | |
Goran Jakovljevic | f652cec | 2015-08-25 16:11:42 +0200 | [diff] [blame] | 272 | void Move32(Location destination, Location source); |
| 273 | void Move64(Location destination, Location source); |
| 274 | void MoveConstant(Location location, HConstant* c); |
| 275 | |
| 276 | size_t GetWordSize() const OVERRIDE { return kMipsWordSize; } |
| 277 | |
| 278 | size_t GetFloatingPointSpillSlotSize() const OVERRIDE { return kMipsDoublewordSize; } |
| 279 | |
| 280 | uintptr_t GetAddressOf(HBasicBlock* block) const OVERRIDE { |
| 281 | return assembler_.GetLabelLocation(GetLabelOf(block)); |
| 282 | } |
| 283 | |
| 284 | HGraphVisitor* GetLocationBuilder() OVERRIDE { return &location_builder_; } |
| 285 | HGraphVisitor* GetInstructionVisitor() OVERRIDE { return &instruction_visitor_; } |
| 286 | MipsAssembler* GetAssembler() OVERRIDE { return &assembler_; } |
| 287 | const MipsAssembler& GetAssembler() const OVERRIDE { return assembler_; } |
| 288 | |
| 289 | void MarkGCCard(Register object, Register value); |
| 290 | |
| 291 | // Register allocation. |
| 292 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 293 | void SetupBlockedRegisters() const OVERRIDE; |
Goran Jakovljevic | f652cec | 2015-08-25 16:11:42 +0200 | [diff] [blame] | 294 | |
| 295 | Location GetStackLocation(HLoadLocal* load) const OVERRIDE; |
| 296 | |
| 297 | size_t SaveCoreRegister(size_t stack_index, uint32_t reg_id); |
| 298 | size_t RestoreCoreRegister(size_t stack_index, uint32_t reg_id); |
| 299 | size_t SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id); |
| 300 | size_t RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id); |
| 301 | |
| 302 | void DumpCoreRegister(std::ostream& stream, int reg) const OVERRIDE; |
| 303 | void DumpFloatingPointRegister(std::ostream& stream, int reg) const OVERRIDE; |
| 304 | |
| 305 | // Blocks all register pairs made out of blocked core registers. |
| 306 | void UpdateBlockedPairRegisters() const; |
| 307 | |
| 308 | InstructionSet GetInstructionSet() const OVERRIDE { return InstructionSet::kMips; } |
| 309 | |
| 310 | const MipsInstructionSetFeatures& GetInstructionSetFeatures() const { |
| 311 | return isa_features_; |
| 312 | } |
| 313 | |
| 314 | MipsLabel* GetLabelOf(HBasicBlock* block) const { |
| 315 | return CommonGetLabelOf<MipsLabel>(block_labels_, block); |
| 316 | } |
| 317 | |
| 318 | void Initialize() OVERRIDE { |
| 319 | block_labels_ = CommonInitializeLabels<MipsLabel>(); |
| 320 | } |
| 321 | |
| 322 | void Finalize(CodeAllocator* allocator) OVERRIDE; |
| 323 | |
| 324 | // Code generation helpers. |
| 325 | |
| 326 | void MoveLocation(Location dst, Location src, Primitive::Type dst_type) OVERRIDE; |
| 327 | |
| 328 | void MoveConstant(Location destination, int32_t value); |
| 329 | |
| 330 | void AddLocationAsTemp(Location location, LocationSummary* locations) OVERRIDE; |
| 331 | |
| 332 | // Generate code to invoke a runtime entry point. |
| 333 | void InvokeRuntime(QuickEntrypointEnum entrypoint, |
| 334 | HInstruction* instruction, |
| 335 | uint32_t dex_pc, |
| 336 | SlowPathCode* slow_path) OVERRIDE; |
| 337 | |
| 338 | void InvokeRuntime(int32_t offset, |
| 339 | HInstruction* instruction, |
| 340 | uint32_t dex_pc, |
| 341 | SlowPathCode* slow_path, |
| 342 | bool is_direct_entrypoint); |
| 343 | |
| 344 | ParallelMoveResolver* GetMoveResolver() OVERRIDE { return &move_resolver_; } |
| 345 | |
| 346 | bool NeedsTwoRegisters(Primitive::Type type) const { |
| 347 | return type == Primitive::kPrimLong; |
| 348 | } |
| 349 | |
Vladimir Marko | dc151b2 | 2015-10-15 18:02:30 +0100 | [diff] [blame] | 350 | // Check if the desired_dispatch_info is supported. If it is, return it, |
| 351 | // otherwise return a fall-back info that should be used instead. |
| 352 | HInvokeStaticOrDirect::DispatchInfo GetSupportedInvokeStaticOrDirectDispatch( |
| 353 | const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info, |
| 354 | MethodReference target_method) OVERRIDE; |
| 355 | |
Goran Jakovljevic | f652cec | 2015-08-25 16:11:42 +0200 | [diff] [blame] | 356 | void GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp); |
Chris Larsen | 3acee73 | 2015-11-18 13:31:08 -0800 | [diff] [blame] | 357 | void GenerateVirtualCall(HInvokeVirtual* invoke, Location temp) OVERRIDE; |
Goran Jakovljevic | f652cec | 2015-08-25 16:11:42 +0200 | [diff] [blame] | 358 | |
| 359 | void MoveFromReturnRegister(Location trg ATTRIBUTE_UNUSED, |
| 360 | Primitive::Type type ATTRIBUTE_UNUSED) OVERRIDE { |
| 361 | UNIMPLEMENTED(FATAL) << "Not implemented on MIPS"; |
| 362 | } |
| 363 | |
David Srbecky | c7098ff | 2016-02-09 14:30:11 +0000 | [diff] [blame] | 364 | void GenerateNop(); |
| 365 | |
Goran Jakovljevic | f652cec | 2015-08-25 16:11:42 +0200 | [diff] [blame] | 366 | private: |
| 367 | // Labels for each block that will be compiled. |
| 368 | MipsLabel* block_labels_; |
| 369 | MipsLabel frame_entry_label_; |
| 370 | LocationsBuilderMIPS location_builder_; |
| 371 | InstructionCodeGeneratorMIPS instruction_visitor_; |
| 372 | ParallelMoveResolverMIPS move_resolver_; |
| 373 | MipsAssembler assembler_; |
| 374 | const MipsInstructionSetFeatures& isa_features_; |
| 375 | |
| 376 | DISALLOW_COPY_AND_ASSIGN(CodeGeneratorMIPS); |
| 377 | }; |
| 378 | |
| 379 | } // namespace mips |
| 380 | } // namespace art |
| 381 | |
| 382 | #endif // ART_COMPILER_OPTIMIZING_CODE_GENERATOR_MIPS_H_ |