blob: 17ac62931d35f4eca0cb4553579a637a3de99fa1 [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001/*
2 * Copyright (C) 2012 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/* This file contains codegen for the Mips ISA */
18
19#include "codegen_mips.h"
Ian Rogersd582fa42014-11-05 23:46:43 -080020
Andreas Gampe0b9203e2015-01-22 20:39:27 -080021#include "base/logging.h"
22#include "dex/mir_graph.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070023#include "dex/quick/mir_to_lir-inl.h"
buzbeeb5860fb2014-06-21 15:31:01 -070024#include "dex/reg_storage_eq.h"
Ian Rogers166db042013-07-26 12:05:57 -070025#include "entrypoints/quick/quick_entrypoints.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070026#include "mips_lir.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070027#include "mirror/array-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070028
29namespace art {
30
31/*
32 * Compare two 64-bit values
33 * x = y return 0
34 * x < y return -1
35 * x > y return 1
36 *
37 * slt t0, x.hi, y.hi; # (x.hi < y.hi) ? 1:0
38 * sgt t1, x.hi, y.hi; # (y.hi > x.hi) ? 1:0
39 * subu res, t0, t1 # res = -1:1:0 for [ < > = ]
40 * bnez res, finish
41 * sltu t0, x.lo, y.lo
42 * sgtu r1, x.lo, y.lo
43 * subu res, t0, t1
44 * finish:
45 *
46 */
47void MipsMir2Lir::GenCmpLong(RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070048 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070049 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
50 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
buzbee091cc402014-03-31 10:14:40 -070051 RegStorage t0 = AllocTemp();
52 RegStorage t1 = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -070053 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee091cc402014-03-31 10:14:40 -070054 NewLIR3(kMipsSlt, t0.GetReg(), rl_src1.reg.GetHighReg(), rl_src2.reg.GetHighReg());
55 NewLIR3(kMipsSlt, t1.GetReg(), rl_src2.reg.GetHighReg(), rl_src1.reg.GetHighReg());
56 NewLIR3(kMipsSubu, rl_result.reg.GetReg(), t1.GetReg(), t0.GetReg());
buzbee2700f7e2014-03-07 09:46:20 -080057 LIR* branch = OpCmpImmBranch(kCondNe, rl_result.reg, 0, NULL);
buzbee091cc402014-03-31 10:14:40 -070058 NewLIR3(kMipsSltu, t0.GetReg(), rl_src1.reg.GetLowReg(), rl_src2.reg.GetLowReg());
59 NewLIR3(kMipsSltu, t1.GetReg(), rl_src2.reg.GetLowReg(), rl_src1.reg.GetLowReg());
60 NewLIR3(kMipsSubu, rl_result.reg.GetReg(), t1.GetReg(), t0.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -070061 FreeTemp(t0);
62 FreeTemp(t1);
63 LIR* target = NewLIR0(kPseudoTargetLabel);
64 branch->target = target;
65 StoreValue(rl_dest, rl_result);
66}
67
buzbee2700f7e2014-03-07 09:46:20 -080068LIR* MipsMir2Lir::OpCmpBranch(ConditionCode cond, RegStorage src1, RegStorage src2, LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070069 LIR* branch;
70 MipsOpCode slt_op;
71 MipsOpCode br_op;
72 bool cmp_zero = false;
73 bool swapped = false;
74 switch (cond) {
75 case kCondEq:
76 br_op = kMipsBeq;
77 cmp_zero = true;
78 break;
79 case kCondNe:
80 br_op = kMipsBne;
81 cmp_zero = true;
82 break;
Vladimir Marko58af1f92013-12-19 13:31:15 +000083 case kCondUlt:
Brian Carlstrom7940e442013-07-12 13:46:57 -070084 slt_op = kMipsSltu;
85 br_op = kMipsBnez;
86 break;
Vladimir Marko58af1f92013-12-19 13:31:15 +000087 case kCondUge:
Brian Carlstrom7940e442013-07-12 13:46:57 -070088 slt_op = kMipsSltu;
89 br_op = kMipsBeqz;
90 break;
91 case kCondGe:
92 slt_op = kMipsSlt;
93 br_op = kMipsBeqz;
94 break;
95 case kCondGt:
96 slt_op = kMipsSlt;
97 br_op = kMipsBnez;
98 swapped = true;
99 break;
100 case kCondLe:
101 slt_op = kMipsSlt;
102 br_op = kMipsBeqz;
103 swapped = true;
104 break;
105 case kCondLt:
106 slt_op = kMipsSlt;
107 br_op = kMipsBnez;
108 break;
109 case kCondHi: // Gtu
110 slt_op = kMipsSltu;
111 br_op = kMipsBnez;
112 swapped = true;
113 break;
114 default:
115 LOG(FATAL) << "No support for ConditionCode: " << cond;
116 return NULL;
117 }
118 if (cmp_zero) {
buzbee2700f7e2014-03-07 09:46:20 -0800119 branch = NewLIR2(br_op, src1.GetReg(), src2.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700120 } else {
buzbee091cc402014-03-31 10:14:40 -0700121 RegStorage t_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700122 if (swapped) {
buzbee091cc402014-03-31 10:14:40 -0700123 NewLIR3(slt_op, t_reg.GetReg(), src2.GetReg(), src1.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700124 } else {
buzbee091cc402014-03-31 10:14:40 -0700125 NewLIR3(slt_op, t_reg.GetReg(), src1.GetReg(), src2.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700126 }
buzbee091cc402014-03-31 10:14:40 -0700127 branch = NewLIR1(br_op, t_reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700128 FreeTemp(t_reg);
129 }
130 branch->target = target;
131 return branch;
132}
133
buzbee2700f7e2014-03-07 09:46:20 -0800134LIR* MipsMir2Lir::OpCmpImmBranch(ConditionCode cond, RegStorage reg, int check_value, LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700135 LIR* branch;
136 if (check_value != 0) {
137 // TUNING: handle s16 & kCondLt/Mi case using slti
buzbee2700f7e2014-03-07 09:46:20 -0800138 RegStorage t_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700139 LoadConstant(t_reg, check_value);
140 branch = OpCmpBranch(cond, reg, t_reg, target);
141 FreeTemp(t_reg);
142 return branch;
143 }
144 MipsOpCode opc;
145 switch (cond) {
146 case kCondEq: opc = kMipsBeqz; break;
147 case kCondGe: opc = kMipsBgez; break;
148 case kCondGt: opc = kMipsBgtz; break;
149 case kCondLe: opc = kMipsBlez; break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700150 // case KCondMi:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700151 case kCondLt: opc = kMipsBltz; break;
152 case kCondNe: opc = kMipsBnez; break;
153 default:
154 // Tuning: use slti when applicable
buzbee2700f7e2014-03-07 09:46:20 -0800155 RegStorage t_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700156 LoadConstant(t_reg, check_value);
157 branch = OpCmpBranch(cond, reg, t_reg, target);
158 FreeTemp(t_reg);
159 return branch;
160 }
buzbee2700f7e2014-03-07 09:46:20 -0800161 branch = NewLIR1(opc, reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700162 branch->target = target;
163 return branch;
164}
165
buzbee2700f7e2014-03-07 09:46:20 -0800166LIR* MipsMir2Lir::OpRegCopyNoInsert(RegStorage r_dest, RegStorage r_src) {
167 // If src or dest is a pair, we'll be using low reg.
168 if (r_dest.IsPair()) {
169 r_dest = r_dest.GetLow();
170 }
171 if (r_src.IsPair()) {
172 r_src = r_src.GetLow();
173 }
buzbee091cc402014-03-31 10:14:40 -0700174 if (r_dest.IsFloat() || r_src.IsFloat())
Brian Carlstrom7940e442013-07-12 13:46:57 -0700175 return OpFpRegCopy(r_dest, r_src);
176 LIR* res = RawLIR(current_dalvik_offset_, kMipsMove,
Andreas Gampe8ebdc2b2015-01-14 12:09:25 -0800177 r_dest.GetReg(), r_src.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700178 if (!(cu_->disable_opt & (1 << kSafeOptimizations)) && r_dest == r_src) {
179 res->flags.is_nop = true;
180 }
181 return res;
182}
183
buzbee7a11ab02014-04-28 20:02:38 -0700184void MipsMir2Lir::OpRegCopy(RegStorage r_dest, RegStorage r_src) {
185 if (r_dest != r_src) {
186 LIR *res = OpRegCopyNoInsert(r_dest, r_src);
187 AppendLIR(res);
188 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700189}
190
buzbee2700f7e2014-03-07 09:46:20 -0800191void MipsMir2Lir::OpRegCopyWide(RegStorage r_dest, RegStorage r_src) {
buzbee7a11ab02014-04-28 20:02:38 -0700192 if (r_dest != r_src) {
buzbee091cc402014-03-31 10:14:40 -0700193 bool dest_fp = r_dest.IsFloat();
194 bool src_fp = r_src.IsFloat();
buzbee7a11ab02014-04-28 20:02:38 -0700195 if (dest_fp) {
196 if (src_fp) {
buzbee091cc402014-03-31 10:14:40 -0700197 OpRegCopy(r_dest, r_src);
198 } else {
Andreas Gampe8ebdc2b2015-01-14 12:09:25 -0800199 /* note the operands are swapped for the mtc1 instr */
buzbee091cc402014-03-31 10:14:40 -0700200 NewLIR2(kMipsMtc1, r_src.GetLowReg(), r_dest.GetLowReg());
201 NewLIR2(kMipsMtc1, r_src.GetHighReg(), r_dest.GetHighReg());
buzbee7a11ab02014-04-28 20:02:38 -0700202 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700203 } else {
buzbee7a11ab02014-04-28 20:02:38 -0700204 if (src_fp) {
205 NewLIR2(kMipsMfc1, r_dest.GetLowReg(), r_src.GetLowReg());
206 NewLIR2(kMipsMfc1, r_dest.GetHighReg(), r_src.GetHighReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700207 } else {
buzbee7a11ab02014-04-28 20:02:38 -0700208 // Handle overlap
209 if (r_src.GetHighReg() == r_dest.GetLowReg()) {
210 OpRegCopy(r_dest.GetHigh(), r_src.GetHigh());
211 OpRegCopy(r_dest.GetLow(), r_src.GetLow());
212 } else {
213 OpRegCopy(r_dest.GetLow(), r_src.GetLow());
214 OpRegCopy(r_dest.GetHigh(), r_src.GetHigh());
215 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700216 }
217 }
218 }
219}
220
Andreas Gampe90969af2014-07-15 23:02:11 -0700221void MipsMir2Lir::GenSelectConst32(RegStorage left_op, RegStorage right_op, ConditionCode code,
222 int32_t true_val, int32_t false_val, RegStorage rs_dest,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700223 RegisterClass dest_reg_class) {
224 UNUSED(dest_reg_class);
Andreas Gampe90969af2014-07-15 23:02:11 -0700225 // Implement as a branch-over.
226 // TODO: Conditional move?
Andreas Gampe90969af2014-07-15 23:02:11 -0700227 LoadConstant(rs_dest, true_val);
Raghu Gandham08f8d4c2014-08-14 13:46:53 -0700228 LIR* ne_branchover = OpCmpBranch(code, left_op, right_op, NULL);
229 LoadConstant(rs_dest, false_val);
Andreas Gampe90969af2014-07-15 23:02:11 -0700230 LIR* target_label = NewLIR0(kPseudoTargetLabel);
231 ne_branchover->target = target_label;
232}
233
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700234void MipsMir2Lir::GenSelect(BasicBlock* bb, MIR* mir) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700235 UNUSED(bb, mir);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700236 UNIMPLEMENTED(FATAL) << "Need codegen for select";
237}
238
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700239void MipsMir2Lir::GenFusedLongCmpBranch(BasicBlock* bb, MIR* mir) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700240 UNUSED(bb, mir);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700241 UNIMPLEMENTED(FATAL) << "Need codegen for fused long cmp branch";
242}
243
buzbee2700f7e2014-03-07 09:46:20 -0800244RegLocation MipsMir2Lir::GenDivRem(RegLocation rl_dest, RegStorage reg1, RegStorage reg2,
Andreas Gampe8ebdc2b2015-01-14 12:09:25 -0800245 bool is_div) {
buzbee9da5c102014-03-28 12:59:18 -0700246 NewLIR2(kMipsDiv, reg1.GetReg(), reg2.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700247 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
248 if (is_div) {
buzbee9da5c102014-03-28 12:59:18 -0700249 NewLIR1(kMipsMflo, rl_result.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700250 } else {
buzbee9da5c102014-03-28 12:59:18 -0700251 NewLIR1(kMipsMfhi, rl_result.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700252 }
253 return rl_result;
254}
255
buzbee2700f7e2014-03-07 09:46:20 -0800256RegLocation MipsMir2Lir::GenDivRemLit(RegLocation rl_dest, RegStorage reg1, int lit,
Andreas Gampe8ebdc2b2015-01-14 12:09:25 -0800257 bool is_div) {
buzbee091cc402014-03-31 10:14:40 -0700258 RegStorage t_reg = AllocTemp();
259 NewLIR3(kMipsAddiu, t_reg.GetReg(), rZERO, lit);
260 NewLIR2(kMipsDiv, reg1.GetReg(), t_reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700261 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
262 if (is_div) {
buzbee9da5c102014-03-28 12:59:18 -0700263 NewLIR1(kMipsMflo, rl_result.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700264 } else {
buzbee9da5c102014-03-28 12:59:18 -0700265 NewLIR1(kMipsMfhi, rl_result.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700266 }
267 FreeTemp(t_reg);
268 return rl_result;
269}
270
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700271RegLocation MipsMir2Lir::GenDivRem(RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2,
272 bool is_div, int flags) {
273 UNUSED(rl_dest, rl_src1, rl_src2, is_div, flags);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800274 LOG(FATAL) << "Unexpected use of GenDivRem for Mips";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700275 UNREACHABLE();
Mark Mendell2bf31e62014-01-23 12:13:40 -0800276}
277
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700278RegLocation MipsMir2Lir::GenDivRemLit(RegLocation rl_dest, RegLocation rl_src1, int lit,
279 bool is_div) {
280 UNUSED(rl_dest, rl_src1, lit, is_div);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800281 LOG(FATAL) << "Unexpected use of GenDivRemLit for Mips";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700282 UNREACHABLE();
Mark Mendell2bf31e62014-01-23 12:13:40 -0800283}
284
Vladimir Marko1c282e22013-11-21 14:49:47 +0000285bool MipsMir2Lir::GenInlinedCas(CallInfo* info, bool is_long, bool is_object) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700286 UNUSED(info, is_long, is_object);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700287 return false;
288}
289
Vladimir Marko5030d3e2014-07-17 10:43:08 +0100290bool MipsMir2Lir::GenInlinedAbsFloat(CallInfo* info) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700291 UNUSED(info);
292 // TODO: add Mips implementation.
Vladimir Marko5030d3e2014-07-17 10:43:08 +0100293 return false;
294}
295
296bool MipsMir2Lir::GenInlinedAbsDouble(CallInfo* info) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700297 UNUSED(info);
298 // TODO: add Mips implementation.
Vladimir Marko5030d3e2014-07-17 10:43:08 +0100299 return false;
300}
301
Brian Carlstrom7940e442013-07-12 13:46:57 -0700302bool MipsMir2Lir::GenInlinedSqrt(CallInfo* info) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700303 UNUSED(info);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700304 return false;
305}
306
Vladimir Markoe508a202013-11-04 15:24:22 +0000307bool MipsMir2Lir::GenInlinedPeek(CallInfo* info, OpSize size) {
308 if (size != kSignedByte) {
309 // MIPS supports only aligned access. Defer unaligned access to JNI implementation.
310 return false;
311 }
312 RegLocation rl_src_address = info->args[0]; // long address
buzbee2700f7e2014-03-07 09:46:20 -0800313 rl_src_address = NarrowRegLoc(rl_src_address); // ignore high half in info->args[1]
Vladimir Markoe508a202013-11-04 15:24:22 +0000314 RegLocation rl_dest = InlineTarget(info);
315 RegLocation rl_address = LoadValue(rl_src_address, kCoreReg);
316 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
317 DCHECK(size == kSignedByte);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000318 LoadBaseDisp(rl_address.reg, 0, rl_result.reg, size, kNotVolatile);
Vladimir Markoe508a202013-11-04 15:24:22 +0000319 StoreValue(rl_dest, rl_result);
320 return true;
321}
322
323bool MipsMir2Lir::GenInlinedPoke(CallInfo* info, OpSize size) {
324 if (size != kSignedByte) {
325 // MIPS supports only aligned access. Defer unaligned access to JNI implementation.
326 return false;
327 }
328 RegLocation rl_src_address = info->args[0]; // long address
buzbee2700f7e2014-03-07 09:46:20 -0800329 rl_src_address = NarrowRegLoc(rl_src_address); // ignore high half in info->args[1]
Vladimir Markoe508a202013-11-04 15:24:22 +0000330 RegLocation rl_src_value = info->args[2]; // [size] value
331 RegLocation rl_address = LoadValue(rl_src_address, kCoreReg);
332 DCHECK(size == kSignedByte);
333 RegLocation rl_value = LoadValue(rl_src_value, kCoreReg);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000334 StoreBaseDisp(rl_address.reg, 0, rl_value.reg, size, kNotVolatile);
Vladimir Markoe508a202013-11-04 15:24:22 +0000335 return true;
336}
337
buzbee2700f7e2014-03-07 09:46:20 -0800338LIR* MipsMir2Lir::OpPcRelLoad(RegStorage reg, LIR* target) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700339 UNUSED(reg, target);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700340 LOG(FATAL) << "Unexpected use of OpPcRelLoad for Mips";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700341 UNREACHABLE();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700342}
343
buzbee2700f7e2014-03-07 09:46:20 -0800344LIR* MipsMir2Lir::OpVldm(RegStorage r_base, int count) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700345 UNUSED(r_base, count);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700346 LOG(FATAL) << "Unexpected use of OpVldm for Mips";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700347 UNREACHABLE();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700348}
349
buzbee2700f7e2014-03-07 09:46:20 -0800350LIR* MipsMir2Lir::OpVstm(RegStorage r_base, int count) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700351 UNUSED(r_base, count);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700352 LOG(FATAL) << "Unexpected use of OpVstm for Mips";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700353 UNREACHABLE();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700354}
355
356void MipsMir2Lir::GenMultiplyByTwoBitMultiplier(RegLocation rl_src,
357 RegLocation rl_result, int lit,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700358 int first_bit, int second_bit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700359 UNUSED(lit);
buzbee2700f7e2014-03-07 09:46:20 -0800360 RegStorage t_reg = AllocTemp();
361 OpRegRegImm(kOpLsl, t_reg, rl_src.reg, second_bit - first_bit);
362 OpRegRegReg(kOpAdd, rl_result.reg, rl_src.reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700363 FreeTemp(t_reg);
364 if (first_bit != 0) {
buzbee2700f7e2014-03-07 09:46:20 -0800365 OpRegRegImm(kOpLsl, rl_result.reg, rl_result.reg, first_bit);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700366 }
367}
368
Mingyao Yange643a172014-04-08 11:02:52 -0700369void MipsMir2Lir::GenDivZeroCheckWide(RegStorage reg) {
buzbee2700f7e2014-03-07 09:46:20 -0800370 DCHECK(reg.IsPair()); // TODO: support k64BitSolo.
371 RegStorage t_reg = AllocTemp();
372 OpRegRegReg(kOpOr, t_reg, reg.GetLow(), reg.GetHigh());
Mingyao Yangd15f4e22014-04-17 18:46:24 -0700373 GenDivZeroCheck(t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700374 FreeTemp(t_reg);
375}
376
377// Test suspend flag, return target of taken suspend branch
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700378LIR* MipsMir2Lir::OpTestSuspend(LIR* target) {
buzbee2700f7e2014-03-07 09:46:20 -0800379 OpRegImm(kOpSub, rs_rMIPS_SUSPEND, 1);
380 return OpCmpImmBranch((target == NULL) ? kCondEq : kCondNe, rs_rMIPS_SUSPEND, 0, target);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700381}
382
383// Decrement register and branch on condition
buzbee2700f7e2014-03-07 09:46:20 -0800384LIR* MipsMir2Lir::OpDecAndBranch(ConditionCode c_code, RegStorage reg, LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700385 OpRegImm(kOpSub, reg, 1);
386 return OpCmpImmBranch(c_code, reg, 0, target);
387}
388
buzbee11b63d12013-08-27 07:34:17 -0700389bool MipsMir2Lir::SmallLiteralDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700390 RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700391 UNUSED(dalvik_opcode, is_div, rl_src, rl_dest, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700392 LOG(FATAL) << "Unexpected use of smallLiteralDive in Mips";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700393 UNREACHABLE();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700394}
395
Ian Rogerse2143c02014-03-28 08:47:16 -0700396bool MipsMir2Lir::EasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700397 UNUSED(rl_src, rl_dest, lit);
Ian Rogerse2143c02014-03-28 08:47:16 -0700398 LOG(FATAL) << "Unexpected use of easyMultiply in Mips";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700399 UNREACHABLE();
Ian Rogerse2143c02014-03-28 08:47:16 -0700400}
401
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700402LIR* MipsMir2Lir::OpIT(ConditionCode cond, const char* guide) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700403 UNUSED(cond, guide);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700404 LOG(FATAL) << "Unexpected use of OpIT in Mips";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700405 UNREACHABLE();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700406}
407
Dave Allison3da67a52014-04-02 17:03:45 -0700408void MipsMir2Lir::OpEndIT(LIR* it) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700409 UNUSED(it);
Dave Allison3da67a52014-04-02 17:03:45 -0700410 LOG(FATAL) << "Unexpected use of OpEndIT in Mips";
411}
412
Mark Mendelle02d48f2014-01-15 11:19:23 -0800413void MipsMir2Lir::GenAddLong(Instruction::Code opcode, RegLocation rl_dest,
414 RegLocation rl_src1, RegLocation rl_src2) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700415 UNUSED(opcode);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700416 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
417 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
418 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
419 /*
420 * [v1 v0] = [a1 a0] + [a3 a2];
421 * addu v0,a2,a0
422 * addu t1,a3,a1
423 * sltu v1,v0,a2
424 * addu v1,v1,t1
425 */
426
buzbee2700f7e2014-03-07 09:46:20 -0800427 OpRegRegReg(kOpAdd, rl_result.reg.GetLow(), rl_src2.reg.GetLow(), rl_src1.reg.GetLow());
428 RegStorage t_reg = AllocTemp();
429 OpRegRegReg(kOpAdd, t_reg, rl_src2.reg.GetHigh(), rl_src1.reg.GetHigh());
430 NewLIR3(kMipsSltu, rl_result.reg.GetHighReg(), rl_result.reg.GetLowReg(), rl_src2.reg.GetLowReg());
431 OpRegRegReg(kOpAdd, rl_result.reg.GetHigh(), rl_result.reg.GetHigh(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700432 FreeTemp(t_reg);
433 StoreValueWide(rl_dest, rl_result);
434}
435
Mark Mendelle02d48f2014-01-15 11:19:23 -0800436void MipsMir2Lir::GenSubLong(Instruction::Code opcode, RegLocation rl_dest,
437 RegLocation rl_src1, RegLocation rl_src2) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700438 UNUSED(opcode);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700439 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
440 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
441 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
442 /*
443 * [v1 v0] = [a1 a0] - [a3 a2];
444 * sltu t1,a0,a2
445 * subu v0,a0,a2
446 * subu v1,a1,a3
447 * subu v1,v1,t1
448 */
449
buzbee2700f7e2014-03-07 09:46:20 -0800450 RegStorage t_reg = AllocTemp();
451 NewLIR3(kMipsSltu, t_reg.GetReg(), rl_src1.reg.GetLowReg(), rl_src2.reg.GetLowReg());
452 OpRegRegReg(kOpSub, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
453 OpRegRegReg(kOpSub, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
454 OpRegRegReg(kOpSub, rl_result.reg.GetHigh(), rl_result.reg.GetHigh(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700455 FreeTemp(t_reg);
456 StoreValueWide(rl_dest, rl_result);
457}
458
Andreas Gampec76c6142014-08-04 16:30:03 -0700459void MipsMir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -0700460 RegLocation rl_src2, int flags) {
Andreas Gampec76c6142014-08-04 16:30:03 -0700461 switch (opcode) {
462 case Instruction::ADD_LONG:
463 case Instruction::ADD_LONG_2ADDR:
464 GenAddLong(opcode, rl_dest, rl_src1, rl_src2);
465 return;
466 case Instruction::SUB_LONG:
467 case Instruction::SUB_LONG_2ADDR:
468 GenSubLong(opcode, rl_dest, rl_src1, rl_src2);
469 return;
470 case Instruction::NEG_LONG:
471 GenNegLong(rl_dest, rl_src2);
472 return;
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100473
Andreas Gampec76c6142014-08-04 16:30:03 -0700474 default:
475 break;
476 }
477
478 // Fallback for all other ops.
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -0700479 Mir2Lir::GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2, flags);
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100480}
481
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700482void MipsMir2Lir::GenNegLong(RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700483 rl_src = LoadValueWide(rl_src, kCoreReg);
484 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
485 /*
486 * [v1 v0] = -[a1 a0]
487 * negu v0,a0
488 * negu v1,a1
489 * sltu t1,r_zero
490 * subu v1,v1,t1
491 */
492
buzbee2700f7e2014-03-07 09:46:20 -0800493 OpRegReg(kOpNeg, rl_result.reg.GetLow(), rl_src.reg.GetLow());
494 OpRegReg(kOpNeg, rl_result.reg.GetHigh(), rl_src.reg.GetHigh());
495 RegStorage t_reg = AllocTemp();
496 NewLIR3(kMipsSltu, t_reg.GetReg(), rZERO, rl_result.reg.GetLowReg());
497 OpRegRegReg(kOpSub, rl_result.reg.GetHigh(), rl_result.reg.GetHigh(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700498 FreeTemp(t_reg);
499 StoreValueWide(rl_dest, rl_result);
500}
501
Brian Carlstrom7940e442013-07-12 13:46:57 -0700502/*
503 * Generate array load
504 */
505void MipsMir2Lir::GenArrayGet(int opt_flags, OpSize size, RegLocation rl_array,
Andreas Gampe8ebdc2b2015-01-14 12:09:25 -0800506 RegLocation rl_index, RegLocation rl_dest, int scale) {
buzbee091cc402014-03-31 10:14:40 -0700507 RegisterClass reg_class = RegClassBySize(size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700508 int len_offset = mirror::Array::LengthOffset().Int32Value();
509 int data_offset;
510 RegLocation rl_result;
Douglas Leung2db3e262014-06-25 16:02:55 -0700511 rl_array = LoadValue(rl_array, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700512 rl_index = LoadValue(rl_index, kCoreReg);
513
Douglas Leung2db3e262014-06-25 16:02:55 -0700514 // FIXME: need to add support for rl_index.is_const.
515
buzbee695d13a2014-04-19 13:32:20 -0700516 if (size == k64 || size == kDouble) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700517 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
518 } else {
519 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
520 }
521
522 /* null object? */
buzbee2700f7e2014-03-07 09:46:20 -0800523 GenNullCheck(rl_array.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700524
buzbee2700f7e2014-03-07 09:46:20 -0800525 RegStorage reg_ptr = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700526 bool needs_range_check = (!(opt_flags & MIR_IGNORE_RANGE_CHECK));
buzbee2700f7e2014-03-07 09:46:20 -0800527 RegStorage reg_len;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700528 if (needs_range_check) {
529 reg_len = AllocTemp();
530 /* Get len */
buzbee695d13a2014-04-19 13:32:20 -0700531 Load32Disp(rl_array.reg, len_offset, reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700532 }
533 /* reg_ptr -> array data */
buzbee2700f7e2014-03-07 09:46:20 -0800534 OpRegRegImm(kOpAdd, reg_ptr, rl_array.reg, data_offset);
buzbee091cc402014-03-31 10:14:40 -0700535 FreeTemp(rl_array.reg);
buzbee695d13a2014-04-19 13:32:20 -0700536 if ((size == k64) || (size == kDouble)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700537 if (scale) {
buzbee2700f7e2014-03-07 09:46:20 -0800538 RegStorage r_new_index = AllocTemp();
539 OpRegRegImm(kOpLsl, r_new_index, rl_index.reg, scale);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700540 OpRegReg(kOpAdd, reg_ptr, r_new_index);
541 FreeTemp(r_new_index);
542 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800543 OpRegReg(kOpAdd, reg_ptr, rl_index.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700544 }
buzbee2700f7e2014-03-07 09:46:20 -0800545 FreeTemp(rl_index.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700546 rl_result = EvalLoc(rl_dest, reg_class, true);
547
548 if (needs_range_check) {
Mingyao Yang80365d92014-04-18 12:10:58 -0700549 GenArrayBoundsCheck(rl_index.reg, reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700550 FreeTemp(reg_len);
551 }
Andreas Gampe3c12c512014-06-24 18:46:29 +0000552 LoadBaseDisp(reg_ptr, 0, rl_result.reg, size, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700553
554 FreeTemp(reg_ptr);
555 StoreValueWide(rl_dest, rl_result);
556 } else {
557 rl_result = EvalLoc(rl_dest, reg_class, true);
558
559 if (needs_range_check) {
Mingyao Yang80365d92014-04-18 12:10:58 -0700560 GenArrayBoundsCheck(rl_index.reg, reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700561 FreeTemp(reg_len);
562 }
buzbee2700f7e2014-03-07 09:46:20 -0800563 LoadBaseIndexed(reg_ptr, rl_index.reg, rl_result.reg, scale, size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700564
565 FreeTemp(reg_ptr);
566 StoreValue(rl_dest, rl_result);
567 }
568}
569
570/*
571 * Generate array store
572 *
573 */
574void MipsMir2Lir::GenArrayPut(int opt_flags, OpSize size, RegLocation rl_array,
Andreas Gampe8ebdc2b2015-01-14 12:09:25 -0800575 RegLocation rl_index, RegLocation rl_src, int scale, bool card_mark) {
buzbee091cc402014-03-31 10:14:40 -0700576 RegisterClass reg_class = RegClassBySize(size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700577 int len_offset = mirror::Array::LengthOffset().Int32Value();
578 int data_offset;
579
buzbee695d13a2014-04-19 13:32:20 -0700580 if (size == k64 || size == kDouble) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700581 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
582 } else {
583 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
584 }
585
Douglas Leung2db3e262014-06-25 16:02:55 -0700586 rl_array = LoadValue(rl_array, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700587 rl_index = LoadValue(rl_index, kCoreReg);
Douglas Leung2db3e262014-06-25 16:02:55 -0700588
589 // FIXME: need to add support for rl_index.is_const.
590
buzbee2700f7e2014-03-07 09:46:20 -0800591 RegStorage reg_ptr;
Ian Rogers773aab12013-10-14 13:50:10 -0700592 bool allocated_reg_ptr_temp = false;
buzbee091cc402014-03-31 10:14:40 -0700593 if (IsTemp(rl_array.reg) && !card_mark) {
594 Clobber(rl_array.reg);
buzbee2700f7e2014-03-07 09:46:20 -0800595 reg_ptr = rl_array.reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700596 } else {
597 reg_ptr = AllocTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800598 OpRegCopy(reg_ptr, rl_array.reg);
Ian Rogers773aab12013-10-14 13:50:10 -0700599 allocated_reg_ptr_temp = true;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700600 }
601
602 /* null object? */
buzbee2700f7e2014-03-07 09:46:20 -0800603 GenNullCheck(rl_array.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700604
605 bool needs_range_check = (!(opt_flags & MIR_IGNORE_RANGE_CHECK));
buzbee2700f7e2014-03-07 09:46:20 -0800606 RegStorage reg_len;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700607 if (needs_range_check) {
608 reg_len = AllocTemp();
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700609 // NOTE: max live temps(4) here.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700610 /* Get len */
buzbee695d13a2014-04-19 13:32:20 -0700611 Load32Disp(rl_array.reg, len_offset, reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700612 }
613 /* reg_ptr -> array data */
614 OpRegImm(kOpAdd, reg_ptr, data_offset);
615 /* at this point, reg_ptr points to array, 2 live temps */
buzbee695d13a2014-04-19 13:32:20 -0700616 if ((size == k64) || (size == kDouble)) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700617 // TUNING: specific wide routine that can handle fp regs
Brian Carlstrom7940e442013-07-12 13:46:57 -0700618 if (scale) {
buzbee2700f7e2014-03-07 09:46:20 -0800619 RegStorage r_new_index = AllocTemp();
620 OpRegRegImm(kOpLsl, r_new_index, rl_index.reg, scale);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700621 OpRegReg(kOpAdd, reg_ptr, r_new_index);
622 FreeTemp(r_new_index);
623 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800624 OpRegReg(kOpAdd, reg_ptr, rl_index.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700625 }
626 rl_src = LoadValueWide(rl_src, reg_class);
627
628 if (needs_range_check) {
Mingyao Yang80365d92014-04-18 12:10:58 -0700629 GenArrayBoundsCheck(rl_index.reg, reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700630 FreeTemp(reg_len);
631 }
632
Andreas Gampe3c12c512014-06-24 18:46:29 +0000633 StoreBaseDisp(reg_ptr, 0, rl_src.reg, size, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700634 } else {
635 rl_src = LoadValue(rl_src, reg_class);
636 if (needs_range_check) {
Andreas Gampe8ebdc2b2015-01-14 12:09:25 -0800637 GenArrayBoundsCheck(rl_index.reg, reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700638 FreeTemp(reg_len);
639 }
buzbee2700f7e2014-03-07 09:46:20 -0800640 StoreBaseIndexed(reg_ptr, rl_index.reg, rl_src.reg, scale, size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700641 }
Ian Rogers773aab12013-10-14 13:50:10 -0700642 if (allocated_reg_ptr_temp) {
643 FreeTemp(reg_ptr);
644 }
Ian Rogersa9a82542013-10-04 11:17:26 -0700645 if (card_mark) {
Vladimir Marko743b98c2014-11-24 19:45:41 +0000646 MarkGCCard(opt_flags, rl_src.reg, rl_array.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700647 }
648}
649
650void MipsMir2Lir::GenShiftImmOpLong(Instruction::Code opcode, RegLocation rl_dest,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -0700651 RegLocation rl_src1, RegLocation rl_shift, int flags) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700652 UNUSED(flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700653 // Default implementation is just to ignore the constant case.
654 GenShiftOpLong(opcode, rl_dest, rl_src1, rl_shift);
655}
656
657void MipsMir2Lir::GenArithImmOpLong(Instruction::Code opcode,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -0700658 RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2,
659 int flags) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700660 // Default - bail to non-const handler.
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -0700661 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2, flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700662}
663
664} // namespace art