blob: 378b9a0e05796c576a9276b25fb726633faa2f91 [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001/*
2 * Copyright (C) 2011 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
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_COMPILER_DEX_QUICK_MIPS_CODEGEN_MIPS_H_
18#define ART_COMPILER_DEX_QUICK_MIPS_CODEGEN_MIPS_H_
Brian Carlstrom7940e442013-07-12 13:46:57 -070019
Goran Jakovljevic10957932015-03-24 18:42:56 +010020#include "dex/compiler_ir.h"
Andreas Gampe53c913b2014-08-12 23:19:23 -070021#include "dex/quick/mir_to_lir.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070022#include "mips_lir.h"
23
24namespace art {
25
Andreas Gampe0b9203e2015-01-22 20:39:27 -080026struct CompilationUnit;
27
Ian Rogerse2143c02014-03-28 08:47:16 -070028class MipsMir2Lir FINAL : public Mir2Lir {
Serguei Katkov717a3e42014-11-13 17:19:42 +060029 protected:
30 class InToRegStorageMipsMapper : public InToRegStorageMapper {
31 public:
Goran Jakovljevicff734982015-08-24 12:58:55 +000032 explicit InToRegStorageMipsMapper(Mir2Lir* m2l) : m2l_(m2l), cur_core_reg_(0), cur_fpu_reg_(0)
33 {}
Serguei Katkov717a3e42014-11-13 17:19:42 +060034 virtual RegStorage GetNextReg(ShortyArg arg);
35 virtual void Reset() OVERRIDE {
36 cur_core_reg_ = 0;
Goran Jakovljevicff734982015-08-24 12:58:55 +000037 cur_fpu_reg_ = 0;
Serguei Katkov717a3e42014-11-13 17:19:42 +060038 }
39 protected:
40 Mir2Lir* m2l_;
41 private:
42 size_t cur_core_reg_;
Goran Jakovljevicff734982015-08-24 12:58:55 +000043 size_t cur_fpu_reg_;
Serguei Katkov717a3e42014-11-13 17:19:42 +060044 };
45
Goran Jakovljevic10957932015-03-24 18:42:56 +010046 class InToRegStorageMips64Mapper : public InToRegStorageMapper {
47 public:
48 explicit InToRegStorageMips64Mapper(Mir2Lir* m2l) : m2l_(m2l), cur_arg_reg_(0) {}
49 virtual RegStorage GetNextReg(ShortyArg arg);
50 virtual void Reset() OVERRIDE {
51 cur_arg_reg_ = 0;
52 }
53 protected:
54 Mir2Lir* m2l_;
55 private:
56 size_t cur_arg_reg_;
57 };
58
59 InToRegStorageMips64Mapper in_to_reg_storage_mips64_mapper_;
Serguei Katkov717a3e42014-11-13 17:19:42 +060060 InToRegStorageMipsMapper in_to_reg_storage_mips_mapper_;
61 InToRegStorageMapper* GetResetedInToRegStorageMapper() OVERRIDE {
Goran Jakovljevic10957932015-03-24 18:42:56 +010062 InToRegStorageMapper* res;
63 if (cu_->target64) {
64 res = &in_to_reg_storage_mips64_mapper_;
65 } else {
66 res = &in_to_reg_storage_mips_mapper_;
67 }
68 res->Reset();
69 return res;
Serguei Katkov717a3e42014-11-13 17:19:42 +060070 }
71
Goran Jakovljevic10957932015-03-24 18:42:56 +010072 public:
73 MipsMir2Lir(CompilationUnit* cu, MIRGraph* mir_graph, ArenaAllocator* arena);
Brian Carlstrom7940e442013-07-12 13:46:57 -070074
Goran Jakovljevic10957932015-03-24 18:42:56 +010075 // Required for target - codegen utilities.
76 bool SmallLiteralDivRem(Instruction::Code dalvik_opcode, bool is_div, RegLocation rl_src,
77 RegLocation rl_dest, int lit);
78 bool EasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) OVERRIDE;
79 void GenMultiplyByConstantFloat(RegLocation rl_dest, RegLocation rl_src1, int32_t constant)
80 OVERRIDE;
81 void GenMultiplyByConstantDouble(RegLocation rl_dest, RegLocation rl_src1, int64_t constant)
82 OVERRIDE;
83 LIR* CheckSuspendUsingLoad() OVERRIDE;
84 RegStorage LoadHelper(QuickEntrypointEnum trampoline) OVERRIDE;
Douglas Leung22bb5a22015-07-02 16:42:08 -070085 void ForceImplicitNullCheck(RegStorage reg, int opt_flags, bool is_wide);
Goran Jakovljevic10957932015-03-24 18:42:56 +010086 LIR* LoadBaseDisp(RegStorage r_base, int displacement, RegStorage r_dest, OpSize size,
87 VolatileKind is_volatile) OVERRIDE;
88 LIR* LoadBaseIndexed(RegStorage r_base, RegStorage r_index, RegStorage r_dest, int scale,
89 OpSize size) OVERRIDE;
90 LIR* LoadConstantNoClobber(RegStorage r_dest, int value);
91 LIR* LoadConstantWideNoClobber(RegStorage r_dest, int64_t value);
92 LIR* LoadConstantWide(RegStorage r_dest, int64_t value);
93 LIR* StoreBaseDisp(RegStorage r_base, int displacement, RegStorage r_src, OpSize size,
94 VolatileKind is_volatile) OVERRIDE;
95 LIR* StoreBaseIndexed(RegStorage r_base, RegStorage r_index, RegStorage r_src, int scale,
96 OpSize size) OVERRIDE;
97 LIR* GenAtomic64Load(RegStorage r_base, int displacement, RegStorage r_dest);
98 LIR* GenAtomic64Store(RegStorage r_base, int displacement, RegStorage r_src);
Vladimir Markobf535be2014-11-19 18:52:35 +000099
Goran Jakovljevic10957932015-03-24 18:42:56 +0100100 /// @copydoc Mir2Lir::UnconditionallyMarkGCCard(RegStorage)
101 void UnconditionallyMarkGCCard(RegStorage tgt_addr_reg) OVERRIDE;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700102
Goran Jakovljevic10957932015-03-24 18:42:56 +0100103 // Required for target - register utilities.
104 RegStorage Solo64ToPair64(RegStorage reg);
105 RegStorage Fp64ToSolo32(RegStorage reg);
106 RegStorage TargetReg(SpecialTargetRegister reg);
107 RegStorage TargetReg(SpecialTargetRegister reg, WideKind wide_kind) OVERRIDE;
108 RegStorage TargetPtrReg(SpecialTargetRegister reg) OVERRIDE {
109 return TargetReg(reg, cu_->target64 ? kWide : kNotWide);
110 }
111 RegLocation GetReturnAlt();
112 RegLocation GetReturnWideAlt();
113 RegLocation LocCReturn();
114 RegLocation LocCReturnRef();
115 RegLocation LocCReturnDouble();
116 RegLocation LocCReturnFloat();
117 RegLocation LocCReturnWide();
118 ResourceMask GetRegMaskCommon(const RegStorage& reg) const OVERRIDE;
119 void AdjustSpillMask();
120 void ClobberCallerSave();
121 void FreeCallTemps();
122 void LockCallTemps();
123 void CompilerInitializeRegAlloc();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700124
Goran Jakovljevic10957932015-03-24 18:42:56 +0100125 // Required for target - miscellaneous.
126 void AssembleLIR();
127 int AssignInsnOffsets();
128 void AssignOffsets();
129 AssemblerStatus AssembleInstructions(CodeOffset start_addr);
130 void DumpResourceMask(LIR* lir, const ResourceMask& mask, const char* prefix) OVERRIDE;
131 void SetupTargetResourceMasks(LIR* lir, uint64_t flags, ResourceMask* use_mask,
132 ResourceMask* def_mask) OVERRIDE;
133 const char* GetTargetInstFmt(int opcode);
134 const char* GetTargetInstName(int opcode);
135 std::string BuildInsnString(const char* fmt, LIR* lir, unsigned char* base_addr);
136 ResourceMask GetPCUseDefEncoding() const OVERRIDE;
137 uint64_t GetTargetInstFlags(int opcode);
138 size_t GetInsnSize(LIR* lir) OVERRIDE;
139 bool IsUnconditionalBranch(LIR* lir);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700140
Goran Jakovljevic10957932015-03-24 18:42:56 +0100141 // Get the register class for load/store of a field.
142 RegisterClass RegClassForFieldLoadStore(OpSize size, bool is_volatile) OVERRIDE;
Vladimir Marko674744e2014-04-24 15:18:26 +0100143
Goran Jakovljevic10957932015-03-24 18:42:56 +0100144 // Required for target - Dalvik-level generators.
145 void GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
146 RegLocation lr_shift);
147 void GenArithImmOpLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
148 RegLocation rl_src2, int flags);
149 void GenArrayGet(int opt_flags, OpSize size, RegLocation rl_array, RegLocation rl_index,
150 RegLocation rl_dest, int scale);
151 void GenArrayPut(int opt_flags, OpSize size, RegLocation rl_array, RegLocation rl_index,
152 RegLocation rl_src, int scale, bool card_mark);
153 void GenShiftImmOpLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
154 RegLocation rl_shift, int flags);
155 void GenArithOpDouble(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
156 RegLocation rl_src2);
157 void GenArithOpFloat(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
158 RegLocation rl_src2);
159 void GenCmpFP(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
160 RegLocation rl_src2);
161 void GenConversion(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src);
162 bool GenInlinedAbsFloat(CallInfo* info) OVERRIDE;
163 bool GenInlinedAbsDouble(CallInfo* info) OVERRIDE;
164 bool GenInlinedCas(CallInfo* info, bool is_long, bool is_object);
165 bool GenInlinedMinMax(CallInfo* info, bool is_min, bool is_long);
166 bool GenInlinedSqrt(CallInfo* info);
167 bool GenInlinedPeek(CallInfo* info, OpSize size);
168 bool GenInlinedPoke(CallInfo* info, OpSize size);
169 void GenIntToLong(RegLocation rl_dest, RegLocation rl_src) OVERRIDE;
170 void GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
171 RegLocation rl_src2, int flags) OVERRIDE;
172 RegLocation GenDivRem(RegLocation rl_dest, RegStorage reg_lo, RegStorage reg_hi, bool is_div);
173 RegLocation GenDivRemLit(RegLocation rl_dest, RegStorage reg_lo, int lit, bool is_div);
174 void GenCmpLong(RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2);
175 void GenDivZeroCheckWide(RegStorage reg);
176 void GenEntrySequence(RegLocation* ArgLocs, RegLocation rl_method);
177 void GenExitSequence();
178 void GenSpecialExitSequence() OVERRIDE;
179 void GenSpecialEntryForSuspend() OVERRIDE;
180 void GenSpecialExitForSuspend() OVERRIDE;
181 void GenFusedFPCmpBranch(BasicBlock* bb, MIR* mir, bool gt_bias, bool is_double);
182 void GenFusedLongCmpBranch(BasicBlock* bb, MIR* mir);
183 void GenSelect(BasicBlock* bb, MIR* mir);
184 void GenSelectConst32(RegStorage left_op, RegStorage right_op, ConditionCode code,
185 int32_t true_val, int32_t false_val, RegStorage rs_dest,
186 RegisterClass dest_reg_class) OVERRIDE;
187 bool GenMemBarrier(MemBarrierKind barrier_kind);
188 void GenMoveException(RegLocation rl_dest);
189 void GenMultiplyByTwoBitMultiplier(RegLocation rl_src, RegLocation rl_result, int lit,
190 int first_bit, int second_bit);
191 void GenNegDouble(RegLocation rl_dest, RegLocation rl_src);
192 void GenNegFloat(RegLocation rl_dest, RegLocation rl_src);
193 void GenLargePackedSwitch(MIR* mir, uint32_t table_offset, RegLocation rl_src);
194 void GenLargeSparseSwitch(MIR* mir, uint32_t table_offset, RegLocation rl_src);
195 bool GenSpecialCase(BasicBlock* bb, MIR* mir, const InlineMethod& special);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700196
Goran Jakovljevic10957932015-03-24 18:42:56 +0100197 // Required for target - single operation generators.
198 LIR* OpUnconditionalBranch(LIR* target);
199 LIR* OpCmpBranch(ConditionCode cond, RegStorage src1, RegStorage src2, LIR* target);
200 LIR* OpCmpImmBranch(ConditionCode cond, RegStorage reg, int check_value, LIR* target);
201 LIR* OpCondBranch(ConditionCode cc, LIR* target);
202 LIR* OpDecAndBranch(ConditionCode c_code, RegStorage reg, LIR* target);
203 LIR* OpFpRegCopy(RegStorage r_dest, RegStorage r_src);
204 LIR* OpIT(ConditionCode cond, const char* guide);
205 void OpEndIT(LIR* it);
206 LIR* OpMem(OpKind op, RegStorage r_base, int disp);
207 void OpPcRelLoad(RegStorage reg, LIR* target);
208 LIR* OpReg(OpKind op, RegStorage r_dest_src);
209 void OpRegCopy(RegStorage r_dest, RegStorage r_src);
210 LIR* OpRegCopyNoInsert(RegStorage r_dest, RegStorage r_src);
211 LIR* OpRegImm(OpKind op, RegStorage r_dest_src1, int value);
212 LIR* OpRegReg(OpKind op, RegStorage r_dest_src1, RegStorage r_src2);
213 LIR* OpMovRegMem(RegStorage r_dest, RegStorage r_base, int offset, MoveType move_type);
214 LIR* OpMovMemReg(RegStorage r_base, int offset, RegStorage r_src, MoveType move_type);
215 LIR* OpCondRegReg(OpKind op, ConditionCode cc, RegStorage r_dest, RegStorage r_src);
216 LIR* OpRegRegImm(OpKind op, RegStorage r_dest, RegStorage r_src1, int value);
217 LIR* OpRegRegReg(OpKind op, RegStorage r_dest, RegStorage r_src1, RegStorage r_src2);
218 LIR* OpTestSuspend(LIR* target);
219 LIR* OpVldm(RegStorage r_base, int count);
220 LIR* OpVstm(RegStorage r_base, int count);
221 void OpRegCopyWide(RegStorage dest, RegStorage src);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700222
Goran Jakovljevic10957932015-03-24 18:42:56 +0100223 // TODO: collapse r_dest.
224 LIR* LoadBaseDispBody(RegStorage r_base, int displacement, RegStorage r_dest, OpSize size);
225 // TODO: collapse r_src.
226 LIR* StoreBaseDispBody(RegStorage r_base, int displacement, RegStorage r_src, OpSize size);
227 void SpillCoreRegs();
228 void UnSpillCoreRegs();
229 static const MipsEncodingMap EncodingMap[kMipsLast];
230 bool InexpensiveConstantInt(int32_t value);
231 bool InexpensiveConstantFloat(int32_t value);
232 bool InexpensiveConstantLong(int64_t value);
233 bool InexpensiveConstantDouble(int64_t value);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700234
Goran Jakovljevic10957932015-03-24 18:42:56 +0100235 bool WideGPRsAreAliases() const OVERRIDE {
236 return cu_->target64; // Wide GPRs are formed by pairing on mips32.
237 }
238 bool WideFPRsAreAliases() const OVERRIDE {
239 return cu_->target64; // Wide FPRs are formed by pairing on mips32.
240 }
241
242 LIR* InvokeTrampoline(OpKind op, RegStorage r_tgt, QuickEntrypointEnum trampoline) OVERRIDE;
243
244 RegLocation GenDivRem(RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2, bool is_div,
245 int flags) OVERRIDE;
246 RegLocation GenDivRemLit(RegLocation rl_dest, RegLocation rl_src1, int lit, bool is_div) OVERRIDE;
247 NextCallInsn GetNextSDCallInsn() OVERRIDE;
248 LIR* GenCallInsn(const MirMethodLoweringInfo& method_info) OVERRIDE;
249
250 // Unimplemented intrinsics.
251 bool GenInlinedCharAt(CallInfo* info ATTRIBUTE_UNUSED) OVERRIDE {
252 return false;
253 }
254 bool GenInlinedAbsInt(CallInfo* info ATTRIBUTE_UNUSED) OVERRIDE {
255 return false;
256 }
257 bool GenInlinedAbsLong(CallInfo* info ATTRIBUTE_UNUSED) OVERRIDE {
258 return false;
259 }
260 bool GenInlinedIndexOf(CallInfo* info ATTRIBUTE_UNUSED, bool zero_based ATTRIBUTE_UNUSED)
261 OVERRIDE {
262 return false;
263 }
264
265 // True if isa is rev R6.
266 const bool isaIsR6_;
267
268 // True if floating point unit is 32bits.
269 const bool fpuIs32Bit_;
270
271 private:
Vladimir Marko05792b92015-08-03 11:56:49 +0100272 static int MipsNextSDCallInsn(CompilationUnit* cu, CallInfo* info, int state,
273 const MethodReference& target_method, uint32_t,
274 uintptr_t direct_code, uintptr_t direct_method,
275 InvokeType type);
276
Goran Jakovljevic10957932015-03-24 18:42:56 +0100277 void GenNegLong(RegLocation rl_dest, RegLocation rl_src);
278 void GenAddLong(RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2);
279 void GenSubLong(RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2);
280
281 void ConvertShortToLongBranch(LIR* lir);
282
283 // Mips64 specific long gen methods:
284 void GenLongOp(OpKind op, RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2);
285 void GenNotLong(RegLocation rl_dest, RegLocation rl_src);
286 void GenMulLong(RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2);
287 void GenDivRemLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
288 RegLocation rl_src2, bool is_div, int flags);
289 void GenConversionCall(QuickEntrypointEnum trampoline, RegLocation rl_dest, RegLocation rl_src,
290 RegisterClass reg_class);
291 RegStorage AllocPtrSizeTemp(bool required = true);
292
293 /**
294 * @param reg #RegStorage containing a Solo64 input register (e.g. @c a1 or @c d0).
295 * @return A Solo32 with the same register number as the @p reg (e.g. @c a1 or @c f0).
296 * @see As64BitReg
297 */
298 RegStorage As32BitReg(RegStorage reg) {
299 DCHECK(!reg.IsPair());
300 if ((kFailOnSizeError || kReportSizeError) && !reg.Is64Bit()) {
301 if (kFailOnSizeError) {
302 LOG(FATAL) << "Expected 64b register";
303 } else {
304 LOG(WARNING) << "Expected 64b register";
305 return reg;
306 }
Serguei Katkov59a42af2014-07-05 00:55:46 +0700307 }
Goran Jakovljevic10957932015-03-24 18:42:56 +0100308 RegStorage ret_val = RegStorage(RegStorage::k32BitSolo,
309 reg.GetRawBits() & RegStorage::kRegTypeMask);
310 DCHECK_EQ(GetRegInfo(reg)->FindMatchingView(RegisterInfo::k32SoloStorageMask)
311 ->GetReg().GetReg(),
312 ret_val.GetReg());
313 return ret_val;
314 }
315
316 /**
317 * @param reg #RegStorage containing a Solo32 input register (e.g. @c a1 or @c f0).
318 * @return A Solo64 with the same register number as the @p reg (e.g. @c a1 or @c d0).
319 */
320 RegStorage As64BitReg(RegStorage reg) {
321 DCHECK(!reg.IsPair());
322 if ((kFailOnSizeError || kReportSizeError) && !reg.Is32Bit()) {
323 if (kFailOnSizeError) {
324 LOG(FATAL) << "Expected 32b register";
325 } else {
326 LOG(WARNING) << "Expected 32b register";
327 return reg;
328 }
Serguei Katkov59a42af2014-07-05 00:55:46 +0700329 }
Goran Jakovljevic10957932015-03-24 18:42:56 +0100330 RegStorage ret_val = RegStorage(RegStorage::k64BitSolo,
331 reg.GetRawBits() & RegStorage::kRegTypeMask);
332 DCHECK_EQ(GetRegInfo(reg)->FindMatchingView(RegisterInfo::k64SoloStorageMask)
333 ->GetReg().GetReg(),
334 ret_val.GetReg());
335 return ret_val;
336 }
Serguei Katkov59a42af2014-07-05 00:55:46 +0700337
Goran Jakovljevic10957932015-03-24 18:42:56 +0100338 RegStorage Check64BitReg(RegStorage reg) {
339 if ((kFailOnSizeError || kReportSizeError) && !reg.Is64Bit()) {
340 if (kFailOnSizeError) {
341 LOG(FATAL) << "Checked for 64b register";
342 } else {
343 LOG(WARNING) << "Checked for 64b register";
344 return As64BitReg(reg);
345 }
Andreas Gamped500b532015-01-16 22:09:55 -0800346 }
Goran Jakovljevic10957932015-03-24 18:42:56 +0100347 return reg;
348 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700349};
350
351} // namespace art
352
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700353#endif // ART_COMPILER_DEX_QUICK_MIPS_CODEGEN_MIPS_H_