blob: bb02f7475118dcf7c51dc949a7cb0bceee899376 [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
17#include "arm_lir.h"
18#include "codegen_arm.h"
19#include "dex/quick/mir_to_lir-inl.h"
20
21namespace art {
22
23void ArmMir2Lir::GenArithOpFloat(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070024 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070025 int op = kThumbBkpt;
26 RegLocation rl_result;
27
28 /*
29 * Don't attempt to optimize register usage since these opcodes call out to
30 * the handlers.
31 */
32 switch (opcode) {
33 case Instruction::ADD_FLOAT_2ADDR:
34 case Instruction::ADD_FLOAT:
35 op = kThumb2Vadds;
36 break;
37 case Instruction::SUB_FLOAT_2ADDR:
38 case Instruction::SUB_FLOAT:
39 op = kThumb2Vsubs;
40 break;
41 case Instruction::DIV_FLOAT_2ADDR:
42 case Instruction::DIV_FLOAT:
43 op = kThumb2Vdivs;
44 break;
45 case Instruction::MUL_FLOAT_2ADDR:
46 case Instruction::MUL_FLOAT:
47 op = kThumb2Vmuls;
48 break;
49 case Instruction::REM_FLOAT_2ADDR:
50 case Instruction::REM_FLOAT:
51 FlushAllRegs(); // Send everything to home location
Ian Rogersdd7624d2014-03-14 17:43:00 -070052 CallRuntimeHelperRegLocationRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pFmodf), rl_src1, rl_src2,
Ian Rogers7655f292013-07-29 11:07:13 -070053 false);
Brian Carlstrom7940e442013-07-12 13:46:57 -070054 rl_result = GetReturn(true);
55 StoreValue(rl_dest, rl_result);
56 return;
57 case Instruction::NEG_FLOAT:
58 GenNegFloat(rl_dest, rl_src1);
59 return;
60 default:
61 LOG(FATAL) << "Unexpected opcode: " << opcode;
62 }
63 rl_src1 = LoadValue(rl_src1, kFPReg);
64 rl_src2 = LoadValue(rl_src2, kFPReg);
65 rl_result = EvalLoc(rl_dest, kFPReg, true);
Bill Buzbee00e1ec62014-02-27 23:44:13 +000066 NewLIR3(op, rl_result.reg.GetReg(), rl_src1.reg.GetReg(), rl_src2.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -070067 StoreValue(rl_dest, rl_result);
68}
69
70void ArmMir2Lir::GenArithOpDouble(Instruction::Code opcode,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070071 RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070072 int op = kThumbBkpt;
73 RegLocation rl_result;
74
75 switch (opcode) {
76 case Instruction::ADD_DOUBLE_2ADDR:
77 case Instruction::ADD_DOUBLE:
78 op = kThumb2Vaddd;
79 break;
80 case Instruction::SUB_DOUBLE_2ADDR:
81 case Instruction::SUB_DOUBLE:
82 op = kThumb2Vsubd;
83 break;
84 case Instruction::DIV_DOUBLE_2ADDR:
85 case Instruction::DIV_DOUBLE:
86 op = kThumb2Vdivd;
87 break;
88 case Instruction::MUL_DOUBLE_2ADDR:
89 case Instruction::MUL_DOUBLE:
90 op = kThumb2Vmuld;
91 break;
92 case Instruction::REM_DOUBLE_2ADDR:
93 case Instruction::REM_DOUBLE:
94 FlushAllRegs(); // Send everything to home location
Ian Rogersdd7624d2014-03-14 17:43:00 -070095 CallRuntimeHelperRegLocationRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pFmod), rl_src1, rl_src2,
Ian Rogers7655f292013-07-29 11:07:13 -070096 false);
Brian Carlstrom7940e442013-07-12 13:46:57 -070097 rl_result = GetReturnWide(true);
98 StoreValueWide(rl_dest, rl_result);
99 return;
100 case Instruction::NEG_DOUBLE:
101 GenNegDouble(rl_dest, rl_src1);
102 return;
103 default:
104 LOG(FATAL) << "Unexpected opcode: " << opcode;
105 }
106
107 rl_src1 = LoadValueWide(rl_src1, kFPReg);
108 DCHECK(rl_src1.wide);
109 rl_src2 = LoadValueWide(rl_src2, kFPReg);
110 DCHECK(rl_src2.wide);
111 rl_result = EvalLoc(rl_dest, kFPReg, true);
112 DCHECK(rl_dest.wide);
113 DCHECK(rl_result.wide);
buzbee091cc402014-03-31 10:14:40 -0700114 NewLIR3(op, rl_result.reg.GetReg(), rl_src1.reg.GetReg(), rl_src2.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700115 StoreValueWide(rl_dest, rl_result);
116}
117
buzbee091cc402014-03-31 10:14:40 -0700118void ArmMir2Lir::GenConversion(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700119 int op = kThumbBkpt;
120 int src_reg;
121 RegLocation rl_result;
122
123 switch (opcode) {
124 case Instruction::INT_TO_FLOAT:
125 op = kThumb2VcvtIF;
126 break;
127 case Instruction::FLOAT_TO_INT:
128 op = kThumb2VcvtFI;
129 break;
130 case Instruction::DOUBLE_TO_FLOAT:
131 op = kThumb2VcvtDF;
132 break;
133 case Instruction::FLOAT_TO_DOUBLE:
134 op = kThumb2VcvtFd;
135 break;
136 case Instruction::INT_TO_DOUBLE:
Zheng Xue19649a2014-02-27 13:30:55 +0000137 op = kThumb2VcvtF64S32;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700138 break;
139 case Instruction::DOUBLE_TO_INT:
140 op = kThumb2VcvtDI;
141 break;
Ian Rogersef6a7762013-12-19 17:58:05 -0800142 case Instruction::LONG_TO_DOUBLE: {
143 rl_src = LoadValueWide(rl_src, kFPReg);
buzbee091cc402014-03-31 10:14:40 -0700144 RegStorage src_low = rl_src.reg.DoubleToLowSingle();
145 RegStorage src_high = rl_src.reg.DoubleToHighSingle();
Ian Rogersef6a7762013-12-19 17:58:05 -0800146 rl_result = EvalLoc(rl_dest, kFPReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800147 RegStorage tmp1 = AllocTempDouble();
148 RegStorage tmp2 = AllocTempDouble();
Ian Rogersef6a7762013-12-19 17:58:05 -0800149
buzbee091cc402014-03-31 10:14:40 -0700150 NewLIR2(kThumb2VcvtF64S32, tmp1.GetReg(), src_high.GetReg());
151 NewLIR2(kThumb2VcvtF64U32, rl_result.reg.GetReg(), src_low.GetReg());
buzbee2700f7e2014-03-07 09:46:20 -0800152 LoadConstantWide(tmp2, 0x41f0000000000000LL);
buzbee091cc402014-03-31 10:14:40 -0700153 NewLIR3(kThumb2VmlaF64, rl_result.reg.GetReg(), tmp1.GetReg(), tmp2.GetReg());
Ian Rogersef6a7762013-12-19 17:58:05 -0800154 FreeTemp(tmp1);
155 FreeTemp(tmp2);
156 StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700157 return;
Ian Rogersef6a7762013-12-19 17:58:05 -0800158 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700159 case Instruction::FLOAT_TO_LONG:
Ian Rogersdd7624d2014-03-14 17:43:00 -0700160 GenConversionCall(QUICK_ENTRYPOINT_OFFSET(4, pF2l), rl_dest, rl_src);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700161 return;
Zheng Xuf0e6c9c2014-03-10 10:43:02 +0000162 case Instruction::LONG_TO_FLOAT: {
163 rl_src = LoadValueWide(rl_src, kFPReg);
buzbee091cc402014-03-31 10:14:40 -0700164 RegStorage src_low = rl_src.reg.DoubleToLowSingle();
165 RegStorage src_high = rl_src.reg.DoubleToHighSingle();
Zheng Xuf0e6c9c2014-03-10 10:43:02 +0000166 rl_result = EvalLoc(rl_dest, kFPReg, true);
167 // Allocate temp registers.
buzbee2700f7e2014-03-07 09:46:20 -0800168 RegStorage high_val = AllocTempDouble();
169 RegStorage low_val = AllocTempDouble();
170 RegStorage const_val = AllocTempDouble();
Zheng Xuf0e6c9c2014-03-10 10:43:02 +0000171 // Long to double.
buzbee091cc402014-03-31 10:14:40 -0700172 NewLIR2(kThumb2VcvtF64S32, high_val.GetReg(), src_high.GetReg());
173 NewLIR2(kThumb2VcvtF64U32, low_val.GetReg(), src_low.GetReg());
buzbee2700f7e2014-03-07 09:46:20 -0800174 LoadConstantWide(const_val, INT64_C(0x41f0000000000000));
buzbee091cc402014-03-31 10:14:40 -0700175 NewLIR3(kThumb2VmlaF64, low_val.GetReg(), high_val.GetReg(), const_val.GetReg());
Zheng Xuf0e6c9c2014-03-10 10:43:02 +0000176 // Double to float.
buzbee091cc402014-03-31 10:14:40 -0700177 NewLIR2(kThumb2VcvtDF, rl_result.reg.GetReg(), low_val.GetReg());
Zheng Xuf0e6c9c2014-03-10 10:43:02 +0000178 // Free temp registers.
179 FreeTemp(high_val);
180 FreeTemp(low_val);
181 FreeTemp(const_val);
182 // Store result.
183 StoreValue(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700184 return;
Zheng Xuf0e6c9c2014-03-10 10:43:02 +0000185 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700186 case Instruction::DOUBLE_TO_LONG:
Ian Rogersdd7624d2014-03-14 17:43:00 -0700187 GenConversionCall(QUICK_ENTRYPOINT_OFFSET(4, pD2l), rl_dest, rl_src);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700188 return;
189 default:
190 LOG(FATAL) << "Unexpected opcode: " << opcode;
191 }
192 if (rl_src.wide) {
193 rl_src = LoadValueWide(rl_src, kFPReg);
buzbee091cc402014-03-31 10:14:40 -0700194 src_reg = rl_src.reg.GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700195 } else {
196 rl_src = LoadValue(rl_src, kFPReg);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000197 src_reg = rl_src.reg.GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700198 }
199 if (rl_dest.wide) {
200 rl_result = EvalLoc(rl_dest, kFPReg, true);
buzbee091cc402014-03-31 10:14:40 -0700201 NewLIR2(op, rl_result.reg.GetReg(), src_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700202 StoreValueWide(rl_dest, rl_result);
203 } else {
204 rl_result = EvalLoc(rl_dest, kFPReg, true);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000205 NewLIR2(op, rl_result.reg.GetReg(), src_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700206 StoreValue(rl_dest, rl_result);
207 }
208}
209
210void ArmMir2Lir::GenFusedFPCmpBranch(BasicBlock* bb, MIR* mir, bool gt_bias,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700211 bool is_double) {
buzbee0d829482013-10-11 15:24:55 -0700212 LIR* target = &block_label_list_[bb->taken];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700213 RegLocation rl_src1;
214 RegLocation rl_src2;
215 if (is_double) {
216 rl_src1 = mir_graph_->GetSrcWide(mir, 0);
217 rl_src2 = mir_graph_->GetSrcWide(mir, 2);
218 rl_src1 = LoadValueWide(rl_src1, kFPReg);
219 rl_src2 = LoadValueWide(rl_src2, kFPReg);
buzbee091cc402014-03-31 10:14:40 -0700220 NewLIR2(kThumb2Vcmpd, rl_src1.reg.GetReg(), rl_src2.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700221 } else {
222 rl_src1 = mir_graph_->GetSrc(mir, 0);
223 rl_src2 = mir_graph_->GetSrc(mir, 1);
224 rl_src1 = LoadValue(rl_src1, kFPReg);
225 rl_src2 = LoadValue(rl_src2, kFPReg);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000226 NewLIR2(kThumb2Vcmps, rl_src1.reg.GetReg(), rl_src2.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700227 }
228 NewLIR0(kThumb2Fmstat);
Vladimir Markoa8946072014-01-22 10:30:44 +0000229 ConditionCode ccode = mir->meta.ccode;
Brian Carlstromdf629502013-07-17 22:39:56 -0700230 switch (ccode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700231 case kCondEq:
232 case kCondNe:
233 break;
234 case kCondLt:
235 if (gt_bias) {
236 ccode = kCondMi;
237 }
238 break;
239 case kCondLe:
240 if (gt_bias) {
241 ccode = kCondLs;
242 }
243 break;
244 case kCondGt:
245 if (gt_bias) {
246 ccode = kCondHi;
247 }
248 break;
249 case kCondGe:
250 if (gt_bias) {
Vladimir Marko58af1f92013-12-19 13:31:15 +0000251 ccode = kCondUge;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700252 }
253 break;
254 default:
255 LOG(FATAL) << "Unexpected ccode: " << ccode;
256 }
257 OpCondBranch(ccode, target);
258}
259
260
261void ArmMir2Lir::GenCmpFP(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700262 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700263 bool is_double = false;
264 int default_result = -1;
265 RegLocation rl_result;
266
267 switch (opcode) {
268 case Instruction::CMPL_FLOAT:
269 is_double = false;
270 default_result = -1;
271 break;
272 case Instruction::CMPG_FLOAT:
273 is_double = false;
274 default_result = 1;
275 break;
276 case Instruction::CMPL_DOUBLE:
277 is_double = true;
278 default_result = -1;
279 break;
280 case Instruction::CMPG_DOUBLE:
281 is_double = true;
282 default_result = 1;
283 break;
284 default:
285 LOG(FATAL) << "Unexpected opcode: " << opcode;
286 }
287 if (is_double) {
288 rl_src1 = LoadValueWide(rl_src1, kFPReg);
289 rl_src2 = LoadValueWide(rl_src2, kFPReg);
290 // In case result vreg is also a src vreg, break association to avoid useless copy by EvalLoc()
291 ClobberSReg(rl_dest.s_reg_low);
292 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800293 LoadConstant(rl_result.reg, default_result);
buzbee091cc402014-03-31 10:14:40 -0700294 NewLIR2(kThumb2Vcmpd, rl_src1.reg.GetReg(), rl_src2.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700295 } else {
296 rl_src1 = LoadValue(rl_src1, kFPReg);
297 rl_src2 = LoadValue(rl_src2, kFPReg);
298 // In case result vreg is also a srcvreg, break association to avoid useless copy by EvalLoc()
299 ClobberSReg(rl_dest.s_reg_low);
300 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800301 LoadConstant(rl_result.reg, default_result);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000302 NewLIR2(kThumb2Vcmps, rl_src1.reg.GetReg(), rl_src2.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700303 }
buzbee091cc402014-03-31 10:14:40 -0700304 DCHECK(!rl_result.reg.IsFloat());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700305 NewLIR0(kThumb2Fmstat);
306
Dave Allison3da67a52014-04-02 17:03:45 -0700307 LIR* it = OpIT((default_result == -1) ? kCondGt : kCondMi, "");
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000308 NewLIR2(kThumb2MovI8M, rl_result.reg.GetReg(),
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700309 ModifiedImmediate(-default_result)); // Must not alter ccodes
Dave Allison3da67a52014-04-02 17:03:45 -0700310 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700311
Dave Allison3da67a52014-04-02 17:03:45 -0700312 it = OpIT(kCondEq, "");
buzbee2700f7e2014-03-07 09:46:20 -0800313 LoadConstant(rl_result.reg, 0);
Dave Allison3da67a52014-04-02 17:03:45 -0700314 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700315
316 StoreValue(rl_dest, rl_result);
317}
318
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700319void ArmMir2Lir::GenNegFloat(RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700320 RegLocation rl_result;
321 rl_src = LoadValue(rl_src, kFPReg);
322 rl_result = EvalLoc(rl_dest, kFPReg, true);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000323 NewLIR2(kThumb2Vnegs, rl_result.reg.GetReg(), rl_src.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700324 StoreValue(rl_dest, rl_result);
325}
326
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700327void ArmMir2Lir::GenNegDouble(RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700328 RegLocation rl_result;
329 rl_src = LoadValueWide(rl_src, kFPReg);
330 rl_result = EvalLoc(rl_dest, kFPReg, true);
buzbee091cc402014-03-31 10:14:40 -0700331 NewLIR2(kThumb2Vnegd, rl_result.reg.GetReg(), rl_src.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700332 StoreValueWide(rl_dest, rl_result);
333}
334
335bool ArmMir2Lir::GenInlinedSqrt(CallInfo* info) {
336 DCHECK_EQ(cu_->instruction_set, kThumb2);
337 LIR *branch;
338 RegLocation rl_src = info->args[0];
339 RegLocation rl_dest = InlineTargetWide(info); // double place for result
340 rl_src = LoadValueWide(rl_src, kFPReg);
341 RegLocation rl_result = EvalLoc(rl_dest, kFPReg, true);
buzbee091cc402014-03-31 10:14:40 -0700342 NewLIR2(kThumb2Vsqrtd, rl_result.reg.GetReg(), rl_src.reg.GetReg());
343 NewLIR2(kThumb2Vcmpd, rl_result.reg.GetReg(), rl_result.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700344 NewLIR0(kThumb2Fmstat);
345 branch = NewLIR2(kThumbBCond, 0, kArmCondEq);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000346 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700347 LockCallTemps(); // Using fixed registers
Ian Rogersdd7624d2014-03-14 17:43:00 -0700348 RegStorage r_tgt = LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pSqrt));
buzbee091cc402014-03-31 10:14:40 -0700349 NewLIR3(kThumb2Fmrrd, rs_r0.GetReg(), rs_r1.GetReg(), rl_src.reg.GetReg());
buzbee2700f7e2014-03-07 09:46:20 -0800350 NewLIR1(kThumbBlxR, r_tgt.GetReg());
buzbee091cc402014-03-31 10:14:40 -0700351 NewLIR3(kThumb2Fmdrr, rl_result.reg.GetReg(), rs_r0.GetReg(), rs_r1.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700352 branch->target = NewLIR0(kPseudoTargetLabel);
353 StoreValueWide(rl_dest, rl_result);
354 return true;
355}
356
357
358} // namespace art